From d54e595644fcaf6756d53d368213ad3129c49327 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Mon, 25 Oct 2021 15:46:48 +0200 Subject: Add initial `fun` declaration compilation --- src/bytecode/vm.h | 61 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 29 deletions(-) (limited to 'src/bytecode/vm.h') diff --git a/src/bytecode/vm.h b/src/bytecode/vm.h index 23c3d89..f7852de 100755 --- a/src/bytecode/vm.h +++ b/src/bytecode/vm.h @@ -13,17 +13,20 @@ typedef struct CallFrame { // Current code being run. - Object procedure; + Chunk *chunk; // Current program counter for this call. - u8 *pc; // Ref to stack. Is this really needed? - // Object *stack; + // Object *locals; + // Return counter. + u8 *rp; } CallFrame; typedef struct VM { CallFrame *frames; // Stack. Object *stack; + // Program counter. + u8 *pc; // Global variables. HashTable *globals; } VM; @@ -37,9 +40,6 @@ void vm_init(VM *vm) { *vm = (VM){0}; array_init(vm->frames, VM_FRAMES_CAP); - for (size_t i = 0; i < array_cap(vm->frames); i++) { - vm->frames[i] = (CallFrame){0}; - } array_init(vm->stack, VM_STACK_CAP); vm->globals = ht_init(); } @@ -62,16 +62,16 @@ vm_reset(VM *vm) { error_push((Error){ \ .type = ERR_TYPE_RUNTIME, \ .value = ERR_WRONG_ARG_TYPE, \ - .line = chunk->lines[frame->pc - chunk->code - 1].line, \ - .col = chunk->lines[frame->pc - chunk->code - 1].col, \ + .line = frame->chunk->lines[vm->pc - frame->chunk->code - 1].line, \ + .col = frame->chunk->lines[vm->pc - frame->chunk->code - 1].col, \ }) #define SYMBOL_NOT_FOUND_ERR() \ error_push((Error){ \ .type = ERR_TYPE_RUNTIME, \ .value = ERR_SYMBOL_NOT_FOUND, \ - .line = chunk->lines[frame->pc - chunk->code - 1].line, \ - .col = chunk->lines[frame->pc - chunk->code - 1].col, \ + .line = frame->chunk->lines[vm->pc - frame->chunk->code - 1].line, \ + .col = frame->chunk->lines[vm->pc - frame->chunk->code - 1].col, \ }) #define FIXNUM_ARITHMETIC_OP(OP) \ @@ -137,9 +137,9 @@ vm_reset(VM *vm) { void vm_interpret(VM *vm) { CallFrame *frame = &vm->frames[array_size(vm->frames) - 1]; - Chunk *chunk = frame->procedure.chunk; + vm->pc = frame->chunk->code; - if (chunk->code == NULL || array_size(chunk->code) == 0) { + if (frame->chunk->code == NULL || array_size(frame->chunk->code) == 0) { error_push((Error){ .type = ERR_TYPE_RUNTIME, .value = ERR_EMPTY_CHUNK, @@ -147,8 +147,8 @@ vm_interpret(VM *vm) { return; } - u8 *last = chunk->code + array_size(chunk->code); - while (frame->pc < last) { + u8 *last = frame->chunk->code + array_size(frame->chunk->code); + while (vm->pc < last) { #ifdef DEBUG_TRACE_EXECUTION printf("stack: [ "); for (size_t i = 0; i < array_size(vm->stack); i++) { @@ -158,13 +158,13 @@ vm_interpret(VM *vm) { } } printf(" ]\nop: "); - disassemble_instruction(chunk, (frame->pc - chunk->code)); + disassemble_instruction(frame->chunk, (vm->pc - frame->chunk->code)); #endif - u8 instruction = *frame->pc++; + u8 instruction = *vm->pc++; switch (instruction) { case OP_CONSTANT: { - u8 constant = *frame->pc++; - Object obj = chunk->constants[constant]; + u8 constant = *vm->pc++; + Object obj = frame->chunk->constants[constant]; array_push(vm->stack, obj); } break; case OP_DEF: { @@ -209,17 +209,17 @@ vm_interpret(VM *vm) { case OP_LESS_EQUAL: { FIXNUM_COMPARE_OP(<=); } break; case OP_GREATER_EQUAL: { FIXNUM_COMPARE_OP(>=); } break; case OP_JUMP: { - u16 a = *frame->pc++; - u16 b = *frame->pc++; + u16 a = *vm->pc++; + u16 b = *vm->pc++; s16 offset = (a << 8) | b; - frame->pc += offset; + vm->pc += offset; } break; case OP_JUMP_IF_FALSE: { - u16 a = *frame->pc++; - u16 b = *frame->pc++; + u16 a = *vm->pc++; + u16 b = *vm->pc++; s16 offset = (a << 8) | b; if (IS_FALSE(array_pop(vm->stack))) { - frame->pc += offset; + vm->pc += offset; } } break; case OP_DISPLAY: { @@ -253,15 +253,18 @@ vm_interpret(VM *vm) { printf("\n"); } break; case OP_RETURN: { - printf("\n"); + if (frame->rp != NULL) { + // TODO: restore previous call frame. + vm->pc = frame->rp; + } return; } break; default: { error_push((Error){ .type = ERR_TYPE_RUNTIME, .value = ERR_NOT_IMPLEMENTED, - .line = chunk->lines[frame->pc - chunk->code - 1].line, - .col = chunk->lines[frame->pc - chunk->code - 1].col, + .line = frame->chunk->lines[vm->pc - frame->chunk->code - 1].line, + .col = frame->chunk->lines[vm->pc - frame->chunk->code - 1].col, }); return; } break; @@ -271,8 +274,8 @@ vm_interpret(VM *vm) { error_push((Error){ .type = ERR_TYPE_RUNTIME, .value = ERR_PC_OOB, - .line = chunk->lines[0].line, - .col = chunk->lines[0].col, + .line = frame->chunk->lines[0].line, + .col = frame->chunk->lines[0].col, }); } -- cgit v1.2.1