diff --git a/lang/bytecode.c b/lang/bytecode.c index dcb4b3d..bebd129 100644 --- a/lang/bytecode.c +++ b/lang/bytecode.c @@ -10,7 +10,7 @@ // Opcode definitions, see bytecode_run for further documentation enum opcodes { - // 0x00 is unused + OP_END = 0x00, OP_NUM = 0x01, OP_RET = 0x03, // 0x02 was an earlier variant of OP_DROP @@ -23,10 +23,12 @@ OP_SELF = 0x0A }; +#include + void bytecode_run(VmState state, Object obj, const unsigned char *bytecode) { const unsigned char *pos_code = &bytecode[0]; - unsigned char op = OP_RET; - while ((op = *pos_code++) != OP_RET) { + unsigned char op = OP_END; + while ((op = *pos_code++) != OP_END) { switch (op) { // OP_NUM pushes a number on to the stack // First argument is a 4-byte little endian number @@ -92,9 +94,15 @@ vm_stack_push(state, obj); break; } + // OP_RET returns + case OP_RET: { + return; + } // Unknown op, error out default: abort_msg("bytecode_run: Unknown op"); } } + // Uh-oh, no more bytecode? + abort_msg("bytecode_run: Ran out of bytecode to run!"); } diff --git a/lang/compile.py b/lang/compile.py index c386064..560bd50 100755 --- a/lang/compile.py +++ b/lang/compile.py @@ -429,6 +429,7 @@ else: print("Unknown bytecode node: %s" % (node)) return None + bytes += b"\x00" # OP_END return bytes ## C file output