From 744f2d526b8e8adb1533012c348ce7e79d95993b Mon Sep 17 00:00:00 2001 From: zongor Date: Sun, 31 Aug 2025 10:14:32 -0700 Subject: [PATCH] Add OP_GET_PC to implement coroutines/yield in stdlib later. --- src/opcodes.h | 97 ++++++++++++++++++++++++++------------------------- src/vm.c | 5 ++- 2 files changed, 53 insertions(+), 49 deletions(-) diff --git a/src/opcodes.h b/src/opcodes.h index 0e4ccaa..02ebd7d 100644 --- a/src/opcodes.h +++ b/src/opcodes.h @@ -4,54 +4,55 @@ #include "common.h" typedef enum { - OP_HALT, /* halt : terminate execution */ - OP_JMP, /* jump : jump to address src1 unconditionally */ - OP_CALL, /* call : creates a new frame */ - OP_RETURN, /* retn : returns from a frame to the parent frame */ - OP_LOAD, /* load : dest = &[next memory location] */ - OP_STORE, /* stor : next memory location = src1 as float */ - OP_PUSH, /* push : push str ref from register onto the stack and copy str */ - OP_POP, /* pop : pop int from stack onto the register */ - OP_REG_MOV, /* rmov : dest = src1 */ - OP_REG_SWAP, /* rswp : dest = src1, src1 = dest */ - OP_MEM_SWAP, /* mswp : &dest = &src1, &src1 = &dest */ - OP_MEM_MOV, /* mmov : &dest = &src1 */ - OP_MEM_ALLOC, /* aloc : dest [next memory location as size] */ - OP_GET, /* get : dest = ptr : dest = memory[ptr] */ - OP_PUT, /* put : ptr = src1 : memory[ptr] = src */ - OP_OFFSET, /* offs : dest = ptr + src1 : dest = p + o */ - OP_SYSCALL, /* sysc : */ - OP_ADD_INT, /* addi : dest = src1 + src2 */ - OP_SUB_INT, /* subi : dest = src1 - src2 */ - OP_MUL_INT, /* muli : dest = src1 * src2 */ - OP_DIV_INT, /* divi : dest = src1 / src2 */ - OP_ADD_UINT, /* addu : dest = src1 + src2 */ - OP_SUB_UINT, /* subu : dest = src1 - src2 */ - OP_MUL_UINT, /* mulu : dest = src1 * src2 */ - OP_DIV_UINT, /* divu : dest = src1 / src2 */ - OP_ADD_REAL, /* addr : dest = src1 + src2 */ - OP_SUB_REAL, /* subr : dest = src1 - src2 */ - OP_MUL_REAL, /* mulr : dest = src1 * src2 */ - OP_DIV_REAL, /* divr : dest = src1 / src2 */ - OP_INT_TO_REAL, /* itor : dest = src1 as real */ - OP_UINT_TO_REAL, /* utor : dest = src1 as real */ - OP_REAL_TO_INT, /* rtoi : dest = src1 as int */ - OP_REAL_TO_UINT, /* rtou : dest = src1 as uint */ - OP_JEQ_INT, /* jeqi : jump to address dest if src1 as int == src2 as int */ - OP_JGT_INT, /* jgti : jump to address dest if src1 as int > src2 as int*/ - OP_JLT_INT, /* jlti : jump to address dest if src1 as int < src2 as int */ - OP_JLE_INT, /* jlei : jump to address dest if src1 as int <= src2 as int */ - OP_JGE_INT, /* jgei : jump to address dest if src1 as int >= src2 as int*/ - OP_JEQ_UINT, /* jequ : jump to address dest if src1 as int == src2 as uint */ - OP_JGT_UINT, /* jgtu : jump to address dest if src1 as int > src2 as uint*/ - OP_JLT_UINT, /* jltu : jump to address dest if src1 as int < src2 as uint */ - OP_JLE_UINT, /* jleu : jump to address dest if src1 as int <= src2 as uint */ - OP_JGE_UINT, /* jgeu : jump to address dest if src1 as int >= src2 as uint*/ - OP_JEQ_REAL, /* jeqr : jump to address dest if src1 as real == src2 as real */ - OP_JGE_REAL, /* jgtr : jump to address dest if src1 as real >= src2 as real */ - OP_JGT_REAL, /* jltr : jump to address dest if src1 as real > src2 as real */ - OP_JLT_REAL, /* jler : jump to address dest if src1 as real < src2 as real */ - OP_JLE_REAL, /* jger : jump to address dest if src1 as real <= src2 as real */ + OP_HALT, /* halt : terminate execution */ + OP_JMP, /* jump : jump to address dest unconditionally */ + OP_GET_PC, /* pc : dest = current program counter */ + OP_CALL, /* call : creates a new frame */ + OP_RETURN, /* retn : returns from a frame to the parent frame */ + OP_LOAD, /* load : dest = &[next memory location] */ + OP_STORE, /* stor : next memory location = src1 as float */ + OP_PUSH, /* push : push str ref from register onto the stack and copy str */ + OP_POP, /* pop : pop int from stack onto the register */ + OP_REG_MOV, /* rmov : dest = src1 */ + OP_REG_SWAP, /* rswp : dest = src1, src1 = dest */ + OP_MEM_SWAP, /* mswp : &dest = &src1, &src1 = &dest */ + OP_MEM_MOV, /* mmov : &dest = &src1 */ + OP_MEM_ALLOC, /* aloc : dest [next memory location as size] */ + OP_GET, /* get : dest = ptr : dest = memory[ptr] */ + OP_PUT, /* put : ptr = src1 : memory[ptr] = src */ + OP_OFFSET, /* offs : dest = ptr + src1 : dest = p + o */ + OP_SYSCALL, /* sysc : */ + OP_ADD_INT, /* addi : dest = src1 + src2 */ + OP_SUB_INT, /* subi : dest = src1 - src2 */ + OP_MUL_INT, /* muli : dest = src1 * src2 */ + OP_DIV_INT, /* divi : dest = src1 / src2 */ + OP_ADD_UINT, /* addu : dest = src1 + src2 */ + OP_SUB_UINT, /* subu : dest = src1 - src2 */ + OP_MUL_UINT, /* mulu : dest = src1 * src2 */ + OP_DIV_UINT, /* divu : dest = src1 / src2 */ + OP_ADD_REAL, /* addr : dest = src1 + src2 */ + OP_SUB_REAL, /* subr : dest = src1 - src2 */ + OP_MUL_REAL, /* mulr : dest = src1 * src2 */ + OP_DIV_REAL, /* divr : dest = src1 / src2 */ + OP_INT_TO_REAL, /* itor : dest = src1 as real */ + OP_UINT_TO_REAL, /* utor : dest = src1 as real */ + OP_REAL_TO_INT, /* rtoi : dest = src1 as int */ + OP_REAL_TO_UINT, /* rtou : dest = src1 as uint */ + OP_JEQ_INT, /* jeqi : jump to address dest if src1 as int == src2 as int */ + OP_JGT_INT, /* jgti : jump to address dest if src1 as int > src2 as int*/ + OP_JLT_INT, /* jlti : jump to address dest if src1 as int < src2 as int */ + OP_JLE_INT, /* jlei : jump to address dest if src1 as int <= src2 as int */ + OP_JGE_INT, /* jgei : jump to address dest if src1 as int >= src2 as int*/ + OP_JEQ_UINT, /* jequ : jump to address dest if src1 as int == src2 as uint */ + OP_JGT_UINT, /* jgtu : jump to address dest if src1 as int > src2 as uint*/ + OP_JLT_UINT, /* jltu : jump to address dest if src1 as int < src2 as uint */ + OP_JLE_UINT, /* jleu : jump to address dest if src1 as int <= src2 as uint */ + OP_JGE_UINT, /* jgeu : jump to address dest if src1 as int >= src2 as uint*/ + OP_JEQ_REAL, /* jeqr : jump to address dest if src1 as real == src2 as real */ + OP_JGE_REAL, /* jgtr : jump to address dest if src1 as real >= src2 as real */ + OP_JGT_REAL, /* jltr : jump to address dest if src1 as real > src2 as real */ + OP_JLT_REAL, /* jler : jump to address dest if src1 as real < src2 as real */ + OP_JLE_REAL, /* jger : jump to address dest if src1 as real <= src2 as real */ OP_INT_TO_STRING, /* itos : dest = src1 as str */ OP_UINT_TO_STRING, /* utos : dest = src1 as str */ OP_REAL_TO_STRING, /* rtos : dest = src1 as str */ diff --git a/src/vm.c b/src/vm.c index f2cb1fe..fa2fa3b 100644 --- a/src/vm.c +++ b/src/vm.c @@ -1,5 +1,4 @@ #include "vm.h" -#include "debug.h" #include /* no inline fn in ANSI C :( */ @@ -221,6 +220,10 @@ bool step_vm(VM *vm) { vm->pc = vm->frames[vm->fp].registers[dest].u; /* Jump to address */ return true; } + case OP_GET_PC: { + vm->frames[vm->fp].registers[dest].u = vm->pc; + return true; + } case OP_JEQ_UINT: { COMPARE_AND_JUMP(uint32_t, u, ==); }