ref: 747cddf7870c548043f44adec04b62440e5abd21
dir: /vm.c/
#include <u.h>
#include <libc.h>
#include "vm.h"
#include "ops.h"
extern int debug;
Frame *stack = nil;
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
runstack(u32int *ptr)
{
Frame *n = malloc(sizeof(Frame));
n->next = stack;
stack = n;
stack->pc = ptr;
while (runinst(stack->pc)) {
;
}
}
void
retstack(void)
{
Frame *p, *c;
p = stack->next;
c = stack;
if (!p)
goto fin;
stack = p;
fin:
free(c);
}
void
vmrun(u32int *ptr)
{
stack = nil; // TODO clean stack
runstack(ptr);
}