// SPDX-License-Identifier: MIT // Copyright (c) 2023 John Watts and the LuminaSensum contributors #include "bytecode.h" #include "boolean.h" #include "error.h" #include "number.h" #include "object.h" #include "string.h" #include "vm.h" // Opcode definitions, see bytecode_run for further documentation enum opcodes { OP_END = 0x00, OP_NUM = 0x01, OP_RET = 0x03, OP_DROP_ONE = 0x02, // Unused old variant of OP_DROP OP_CALL = 0x04, OP_NONE = 0x05, // Formerly OP_NULL OP_GET = 0x06, OP_SET = 0x07, OP_DROP = 0x08, OP_DEPTH_CHECK = 0x09, OP_SELF = 0x0A, OP_BOOLEAN = 0x0B, OP_JUMP_FALSE = 0x0C, OP_JUMP_ALWAYS = 0x0D, OP_TAIL_CALL = 0x0E, }; void bytecode_run(VmState state, Object obj, const unsigned char *bytecode) { const unsigned char *pos_code = &bytecode[0]; 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 case OP_NUM: { int num = 0; num |= *pos_code++ << 0; num |= *pos_code++ << 8; num |= *pos_code++ << 16; num |= *pos_code++ << 24; vm_stack_push(state, number_create(state, num)); break; } // OP_BOOLEAN pushes a boolean on to the stack // First argument is a 1-byte boolean case OP_BOOLEAN: { int value = *pos_code++; abort_if(state, value != 0 && value != 1, "invalid boolean"); vm_stack_push(state, boolean_create(state, value)); break; } // OP_CALL dispatches a call to the top of stack object // First argument is a 1-byte argument count // Second argument is the call string case OP_CALL: { int arg_count = *pos_code++; const char *dispatch = (const char *)pos_code; pos_code += strlen(dispatch) + 1; // Skip NULL too Object obj = vm_stack_pop(state); vm_call(state, obj, dispatch, arg_count); object_drop(state, &obj); break; } // OP_TAIL_CALL queues a call and returns // First argument is a 1-byte argument count // Second argument is the call string case OP_TAIL_CALL: { int arg_count = *pos_code++; const char *dispatch = (const char *)pos_code; pos_code += strlen(dispatch) + 1; // Skip NULL too Object obj = vm_stack_pop(state); int args_start = vm_stack_depth(state) - arg_count; for (int i = 0; i < arg_count; ++i) { int stack_pos = args_start + i; Object arg = vm_stack_get(state, stack_pos); vm_stack_set(state, i, arg); } int remaining = vm_stack_depth(state) - arg_count; if (remaining) { vm_stack_drop(state, remaining); } vm_tailcall(state, obj, dispatch); object_drop(state, &obj); return; } // OP_NONE pushes object_none on to the stack case OP_NONE: { vm_stack_push(state, object_none()); break; } // OP_GET duplicates an element from the stack and stores // the result at the top // First argument is the stack index case OP_GET: { Object obj = vm_stack_get(state, *pos_code++); vm_stack_push(state, obj); break; } // OP_SET stores the stack top element at a stack index // The stack top element is dropped in the process // First argument is the stack index case OP_SET: { Object obj = vm_stack_pop(state); vm_stack_set(state, *pos_code++, obj); break; } // OP_DROP drops multiple stack elements starting at the top // First argument is the number of elements to drop case OP_DROP: { vm_stack_drop(state, *pos_code++); break; } // OP_DEPTH_CHECK checks that the stack is a certain size // First argument is the expected depth case OP_DEPTH_CHECK: { int depth = *pos_code++; abort_if(state, vm_stack_depth(state) != depth, "stack depth mismatch"); break; } // OP_SELF pushes obj on to the stack case OP_SELF: { object_hold(state, obj); vm_stack_push(state, obj); break; } // OP_JUMP_FALSE jumps if the top of stack is false case OP_JUMP_FALSE: { int offset = *pos_code++; Object test = vm_stack_pop(state); if (boolean_value(state, test) == false) { pos_code += offset; } object_drop(state, &test); break; } // OP_JUMP_ALWAYS jumps case OP_JUMP_ALWAYS: { int offset = *pos_code++; pos_code += offset; break; } // OP_RET returns case OP_RET: { return; } // Unknown op, error out default: abort_msg(state, "bytecode_run: Unknown op"); } } // Uh-oh, no more bytecode? abort_msg(state, "bytecode_run: Ran out of bytecode to run!"); }