shithub: gpufs

ref: 77e627174769161f3284c854073f21ea9181d982
dir: /spirvd.c/

View raw version
#include <u.h>
#include <libc.h>
#include "vm.h"
#include "ops.h"

int debug = 0;
int disassemble = 1;

u32int magic = 0x07230203;

u32int testprog[2048];

Frame stack;

u32int
runinst(u32int *ptr)
{
	u32int len, opcode;
	void (*func)(Frame*,u32int);
	
	opcode = (*ptr) & 0x0000ffff;
	len = ((*ptr) & 0xffff0000) >> 16;
	
	if (oplookup(opcode, &func)) {
		func(&stack, len);
		
		// if (func) changes pc, ignore it
		if (ptr == stack.pc) {
			stack.pc += len;
		}
		return len;
	}
	fprint(2, "error: %r\n");
	return 0;
}

void
dasm(u32int *ptr)
{
	stack.pc = ptr;
	stack.next = nil;
	
	while (runinst(stack.pc)) {
		;
	}
}

void
main(int argc, char **argv)
{
	int r = 0;
	
	ARGBEGIN{
	case 'd':
		debug++;
		break;
	}ARGEND;
	
	if (argc) {
		r = open(argv[0], OREAD);
	}
	
	read(r, testprog, 2048);
	
	if (*testprog != magic) {
		fprint(2, "bad magic! got: %x\n", *testprog);
		return;
	}
	
	dasm(testprog+1);
}