shithub: gpufs

ref: 2ea9cb1ccca882e47c316cde52302cd108310649
dir: /dops.c/

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

extern int debug;

void
printresult(int should, uint varid)
{
	char *s;
	
	if (!should) {
		print("%10s   ", " ");
		return;
	}
	s = smprint("$%d", varid);
	print("%10s = ", s);
	free(s);
}

void
OpTypeVoid(Frame *f, u32int)
{
	u32int varid = f->pc[1];
	
	printresult(1, varid);
	print("OpTypeVoid\n");
}

void
OpTypeBool(Frame *f, u32int)
{
	u32int varid = f->pc[1];
	
	printresult(1, varid);
	print("OpTypeBool\n");
}

void
OpTypeInt(Frame *f, u32int)
{
	u32int varid = f->pc[1];
	u32int tlen = f->pc[2];
	
	printresult(1, varid);
	print("OpTypeInt %d\n", tlen);
}

void
OpTypeFloat(Frame *f, u32int)
{
	u32int varid = f->pc[1];
	u32int tlen = f->pc[2];
	
	printresult(1, varid);
	print("OpTypeFloat %d\n", tlen);
}

void
OpTypeVector(Frame *f, u32int)
{
	u32int varid = f->pc[1];
	u32int ttype = f->pc[2];
	u32int tlen = f->pc[3];
	
	printresult(1, varid);
	print("OpTypeFloat $%d %d\n", ttype, tlen);
}

void
OpTypeMatrix(Frame *f, u32int)
{
	u32int varid = f->pc[1];
	u32int ctype = f->pc[2];
	u32int tlen = f->pc[3];
	
	printresult(1, varid);
	print("OpTypeFloat $%d %d\n", ctype, tlen);
}

void
OpName(Frame *f, u32int)
{
	u32int varid = f->pc[1];
	char *varname = (char*)&f->pc[2];
	
	nameadd(varid, varname);
	
	printresult(0, 0);
	print("OpName $%d \"%s\"\n", varid, varname);
}

void
OpNop(Frame*, u32int)
{
	printresult(0, 0);
	print("OpNop\n");
}

void
OpUndef(Frame*, u32int)
{
	printresult(0, 0);
	print("OpUndef\n");
}

void
OpSource(Frame* f, u32int len)
{
	u32int slang = f->pc[1];
	u32int version = f->pc[2];
	u32int file = 0;
	short hasfile = 0;
	char* filename;
	
	if (len > 3) {
		hasfile = 1;
		file = f->pc[3];
	}
	
	printresult(0, 0);
	print("OpSource %d %d", slang, version);
	if (hasfile) {
		filename = strlookup(file);
		print(" %s", filename);
	}
	print("\n");
}

void
OpTypeFunction(Frame *f, u32int len)
{
	u32int varid = f->pc[1];
	u32int rettype = f->pc[2];
	u32int* argv = &f->pc[3];
	u32int argc = len - 2;
	
	printresult(1, varid);
	print("OpTypeFunction $%d", rettype);
	
	for (int i = 0; i < argc; i++) {
		u32int v = argv[i];
		print(" $%d", v);
	}
	print("\n");
}

Op oplist[] = {
	{  1, OpUndef },
	{  3, OpSource },
	{  5, OpName },
	{ 19, OpTypeVoid },
	{ 20, OpTypeBool },
	{ 21, OpTypeInt },
	{ 22, OpTypeFloat },
	{ 23, OpTypeVector },
	{ 24, OpTypeMatrix },
	{ 33, OpTypeFunction },
	{ nil, OpNop },
};

int
oplookup(u32int code, void (**f)(Frame*,u32int))
{
	Op *o;
	u32int c = code & 0x0000ffff;
	if (c == 0) {
		*f = OpNop;
		return 1;
	}
	
	for (o = oplist; o->opcode; o++) {
		if (c == o->opcode) {
			*f = o->f;
			return 1;
		}
	}
	
	werrstr("operation (%d) not supported!", c);
	return 0;
}