working recursion

This commit is contained in:
zongor 2026-01-10 19:57:10 -08:00
parent eb23d26731
commit 3eb121d5b8
2 changed files with 21 additions and 17 deletions

View File

@ -23,7 +23,8 @@ u32 syscall(u32 id, u32 size, u32 mem_ptr) {
USED(size);
switch(id) {
case SYSCALL_DBG_PRINT: {
printf("%d\n", mem[mem_ptr]);
u32 val = READ_U32(mem_ptr);
printf("%d\n", val);
return 0;
}
}
@ -64,6 +65,7 @@ void test_fibonacci() {
code[cp++] = ENCODE_A(OP_SYSCALL, SYSCALL_DBG_PRINT, 1, 2);
code[cp++] = ENCODE_A(OP_HALT, 0, 0, 0);
/* function fib (int n) int */
//code[cp++] = ENCODE_A(OP_SYSCALL, SYSCALL_DBG_PRINT, 1, 0);
code[cp++] = ENCODE_B(OP_LOAD_IMM, 8, fib);
code[cp++] = ENCODE_B(OP_LOAD_IMM, 1, 2);
code[cp++] = ENCODE_B(OP_LOAD_IMM, 2, base_case);

32
vm/vm.c
View File

@ -30,6 +30,7 @@ bool step_vm() {
u32 r2 = fp + (src2 * 4);/* local child return value to*/
fn_ptr = READ_U32(rd);
sp = 0;
/* push parents frame value to reset the heap to */
WRITE_U32(mp, fp);
@ -96,6 +97,22 @@ bool step_vm() {
flag = syscall(id, size, rd);
return true;
}
case OP_PUSH: {
DECODE_B(instruction)
u32 rd = fp + (dest * 4);
u32 val = READ_U32(rd);
USED(imm);
WRITE_U32((mp + ((4 * sp) + FRAME_HEADER_SIZE)), val);
sp++;
return true;
}
case OP_POP: {
DECODE_C(instruction)
USED(imm);
mp -= 4;
sp--;
return true;
}
case OP_LOAD_IMM: {
DECODE_B(instruction)
u32 rd = fp + (dest * 4);
@ -335,21 +352,6 @@ bool step_vm() {
WRITE_U32(rd, value);
return true;
}
case OP_PUSH: {
DECODE_B(instruction)
u32 rd = fp + (dest * 4);
u32 val = READ_U32(rd);
USED(imm);
WRITE_U32((mp + (4 * (sp + 3))), val);
sp++;
return true;
}
case OP_POP: {
DECODE_C(instruction)
USED(imm);
mp -= 4;
return true;
}
case OP_ADD_INT: {
MATH_OP(i32, +);
}