Tardis / lang / bytecode.c
// SPDX-License-Identifier: MIT
// Copyright (c) 2023 John Watts and the LuminaSensum contributors

#include "bytecode.h"
#include "error.h"
#include "number.h"
#include "object.h"
#include "vm.h"

// Opcode definitions, see bytecode_run for further documentation
enum opcodes {
	// 0x00 is reserved for inline strings
	OP_NUM = 0x01,
	OP_RET = 0x03,
	// 0x02 was an earlier variant of OP_DROP
	OP_CALL = 0x04,
	OP_NULL = 0x05,
	OP_GET = 0x06,
	OP_SET = 0x07,
	OP_DROP = 0x08,
	// 0x60 onwards are reserved for inline strings

// clang-format off
static const unsigned char bytecode[] = {
0x09, 0x01, 0x05, 0x05, 0x05, 0x01, 0x6a, 0x02, 0x00, 0x00,
0x07, 0x01, 0x05, 0x06, 0x01, 0x06, 0x01, 0x04, 0x02, 0x41,
0x64, 0x64, 0x00, 0x09, 0x05, 0x07, 0x02, 0x05, 0x01, 0x02,
0x00, 0x00, 0x00, 0x06, 0x02, 0x04, 0x02, 0x4d, 0x69, 0x6e,
0x75, 0x73, 0x00, 0x09, 0x05, 0x07, 0x03, 0x06, 0x03, 0x07,
0x00, 0x08, 0x03, 0x03,
// clang-format on

void bytecode_run(VmState state) {
	const unsigned char *pos_code = &bytecode[0];
	unsigned char op = OP_RET;
	while ((op = *pos_code++) != OP_RET) {
		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(num));
		// 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;
			Object obj = vm_stack_pop(state);
			vm_call(state, obj, dispatch, arg_count);
		// OP_NULL pushes object_none on to the stack
		case OP_NULL: {
			vm_stack_push(state, object_none());
		// 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);
		// 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);
		// 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++);
		// 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(vm_stack_depth(state) != depth,
				"stack depth mismatch");
		// Skip over strings and unknown OPs