shithub: gpufs

Download patch

ref: 5781e4d2a29054abc5d659e9ca867604cba4513f
parent: 8b6238bf263ac5d1f357c0f4a4d0e113b83f1570
author: sirjofri <[email protected]>
date: Sat Feb 24 15:15:14 EST 2024

removes unneeded includes, makes "something" work (hopefully)

Ideally, the program runs now, at least for this single stack (no calls, etc).
However, it's hard to tell because my test function doesn't do anything:
OpFunction
OpLabel
OpFunctionEnd

--- a/cops.c
+++ b/cops.c
@@ -1,8 +1,8 @@
 #include <u.h>
 #include <libc.h>
 #include "objects.h"
-#include "vm.h"
 #include "ops.h"
+#include "vm.h"
 #include "names.h"
 
 extern int debug;
@@ -30,6 +30,12 @@
 }
 
 void
+OpTypeVoid(Frame *f, u32int)
+{
+	DPRINT("OpTypeVoid\n");
+}
+
+void
 OpTypeFloat(Frame *f, u32int)
 {
 	u32int varid = *(f->pc+1);
@@ -110,6 +116,7 @@
 		s->items[varid].type = TTYPE;
 		s->items[varid].t.optype = 33;
 	}
+	DPRINT("OpTypeFunction\n");
 	return;
 }
 
@@ -135,7 +142,8 @@
 		s->lastfunction = result;
 		return;
 	}
-	// TODO: new stack
+	// TODO: new stack should be done in OpCallFunction
+	DPRINT("OpFunction\n");
 	return;
 }
 
@@ -147,7 +155,10 @@
 	if (f->ctxt.type == COMPILE)
 		return;
 	
-	// TODO: pop stack
+	// pop stack (test)
+	*f->ctxt.r.frameptr = f->next;
+	free(f);
+
 	return;
 }
 
@@ -154,12 +165,11 @@
 void
 OpLabel(Frame *f, u32int len)
 {
+	DPRINT("OpLabel\n");
 	if (f->ctxt.type == COMPILE) {
 		Shader *s = f->ctxt.c.shader;
 		u32int retvar = f->pc[1];
 		
-		DPRINT("OpLabel\n");
-		
 		if (!updatenitems(s, retvar))
 			return;
 		
@@ -166,6 +176,10 @@
 		s->items[retvar].type = TLABEL;
 		s->items[retvar].l.ptr = &f->pc[len + 1];
 		
+		if (debug) {
+			fprint(2, "Label: %p\n", s->items[retvar].l.ptr);
+		}
+		
 		if (s->lastfunction >= 0) {
 			s->items[s->lastfunction].f.label = retvar;
 			s->lastfunction = -1;
@@ -178,6 +192,7 @@
 Op oplist[] = {
 	{    5, OpName },
 	{   15, OpEntryPoint },
+	{   19, OpTypeVoid },
 	{   21, OpTypeInt },
 	{   22, OpTypeFloat },
 	{   33, OpTypeFunction },
--- a/dops.c
+++ b/dops.c
@@ -1,7 +1,6 @@
 #include <u.h>
 #include <libc.h>
 #include "objects.h"
-#include "vm.h"
 #include "ops.h"
 #include "names.h"
 
--- a/gpufs.c
+++ b/gpufs.c
@@ -4,6 +4,8 @@
 #include <thread.h>
 #include <9p.h>
 #include <String.h>
+#include "objects.h"
+#include "ops.h"
 #include "vm.h"
 
 void
@@ -473,6 +475,7 @@
 				responderr(r);
 				return;
 			}
+			respond(r, nil);
 			return;
 		}
 		if (strcmp(argv[0], "b") == 0) {
--- a/ops.h
+++ b/ops.h
@@ -1,8 +1,16 @@
+typedef struct Frame Frame;
+
 typedef struct CompileTimeContext CompileTimeContext;
 struct CompileTimeContext {
 	Shader *shader;
 };
 
+typedef struct RunTimeContext RunTimeContext;
+struct RunTimeContext {
+	Shader *shader;
+	Frame **frameptr;
+};
+
 enum {
 	COMPILE,
 	RUNTIME,
@@ -13,10 +21,10 @@
 	short type;
 	union {
 		CompileTimeContext c;
+		RunTimeContext r;
 	};
 };
 
-typedef struct Frame Frame;
 struct Frame {
 	u32int *pc;
 	FrameContext ctxt;
--- a/spirvd.c
+++ b/spirvd.c
@@ -1,7 +1,6 @@
 #include <u.h>
 #include <libc.h>
 #include "objects.h"
-#include "vm.h"
 #include "ops.h"
 
 int debug = 0;
--- a/vm.c
+++ b/vm.c
@@ -14,20 +14,39 @@
 
 #define IDVALID(c) (c < numobjects)
 
-Frame *stack = nil;
+typedef struct PidStack PidStack;
+struct PidStack {
+	int pid;
+	Frame *stack;
+};
 
+#define NSTACKS 10
+int nstacks = NSTACKS;
+PidStack stacks[NSTACKS];
+
 u32int
-runinst(u32int *ptr)
+runinst(Frame *stack, u32int *ptr)
 {
 	u32int len, opcode;
 	void (*func)(Frame*,u32int);
+	Frame **fptr;
 	
+	fptr = stack->ctxt.r.frameptr;
+	
 	opcode = (*ptr) & 0x0000ffff;
 	len = ((*ptr) & 0xffff0000) >> 16;
 	
+	fprint(2, "op: %ux\n", opcode);
+	
 	if (oplookup(opcode, &func)) {
 		func(stack, len);
 		
+		// if frame changed, return (and quit)
+		if (*fptr != stack) {
+			fprint(2, "stack changed\n");
+			return 0;
+		}
+		
 		// if (func) changes pc, ignore it
 		if (ptr == stack->pc) {
 			stack->pc += len;
@@ -39,43 +58,64 @@
 }
 
 void
-runstack(u32int *ptr)
+runstack(Frame *frame, u32int *ptr)
 {
-	Frame *n = malloc(sizeof(Frame));
+	Frame **fptr = frame->ctxt.r.frameptr;
 	
-	n->next = stack;
-	stack = n;
-	stack->pc = ptr;
-	
-	while (runinst(stack->pc)) {
-		;
+	while (runinst(frame, frame->pc)) {
+		if (1) {
+			Frame *cs;
+			for (cs = *fptr; cs; cs = cs->next) {
+				fprint(2, "Stack: %p (PC: %p)\n", cs, cs->pc);
+			}
+		}
 	}
 }
 
 void
-retstack(void)
+vmrun(Shader *s, u32int *ptr)
 {
-	Frame *p, *c;
+	Frame *n;
 	
-	p = stack->next;
-	c = stack;
+	int pid = getpid();
+	int sid = -1;
+	for (int i = 0; i < nstacks; i++) {
+		if (stacks[i].pid <= 0) {
+			stacks[i].pid = pid;
+			sid = i;
+			break;
+		}
+	}
 	
-	if (!p)
-		goto fin;
+	if (sid < 0) {
+		fprint(2, "no free pid stack found!");
+		exits(nil);
+		return;
+	}
 	
-	stack = p;
-
-fin:
-	free(c);
+	n = malloc(sizeof(Frame));
+	
+	n->next = stacks[sid].stack;
+	stacks[sid].stack = n;
+	n->pc = ptr;
+	n->ctxt.type = RUNTIME;
+	n->ctxt.r.shader = s;
+	n->ctxt.r.frameptr = &stacks[sid].stack;
+	
+	runstack(n, ptr);
+	
+	// free all stacks for this PID
+	for (n = stacks[sid].stack; n; ) {
+		Frame *ns = n->next;
+		free(n);
+		n = ns;
+	}
+	stacks[sid].stack = nil;
+	stacks[sid].pid = -1;
+	
+	exits(nil);
 }
 
-void
-vmrun(u32int *ptr)
-{
-	stack = nil; // TODO clean stack
-	runstack(ptr);
-}
-
 typedef struct Object Object;
 struct Object {
 	int id;
@@ -159,8 +199,23 @@
 	
 	u32int *ptr = s->items[label].l.ptr;
 	
-	werrstr("passed tests, but not implemented yet");
-	return 0;
+	if (debug) {
+		fprint(2, "running from %p\n", ptr);
+		fprint(2, "ptr: %ux\n", *ptr);
+	}
+	
+	switch (rfork(RFFDG|RFREND|RFPROC|RFMEM)) {
+	case 0:
+		// child
+		vmrun(s, ptr);
+		return 0;
+	case -1:
+		// error
+		werrstr("error spawning child process");
+		return 0;
+	}
+	// parent
+	return 1;
 }
 
 String*
@@ -405,6 +460,8 @@
 		
 		if (oplookup(opcode, &func)) {
 			func(&f, len);
+		} else {
+			fprint(2, "error: %r\n");
 		}
 			
 		if (oldpc == f.pc) {
@@ -412,8 +469,9 @@
 		}
 	}
 	
+	sh->compiled = 1;
 	updateinfostring(id);
-	return sh->compiled = 1;
+	return sh->compiled;
 }
 
 int
--- a/vm.h
+++ b/vm.h
@@ -1,5 +1,4 @@
-void vmrun(u32int *ptr);
-void runstack(u32int *ptr);
+void runstack(Frame *frame, u32int *pc);
 void retstack(void);
 int runshader(vlong id, char *entrypoint);