diff options
| author | Mistivia <i@mistivia.com> | 2024-11-17 12:29:47 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2024-11-17 12:29:47 +0800 |
| commit | 04f8a72b1436a91cfecf73567a59626c7bdc664f (patch) | |
| tree | 18b060712715e032e02a951e2d5368a1fba0a111 /src | |
| parent | 789ebeb7d097d2824ff62c73438af4cb77f882bc (diff) | |
add test case for basic opcodes
Diffstat (limited to 'src')
| -rw-r--r-- | src/fvm.c | 18 | ||||
| -rw-r--r-- | src/fvm.h | 11 |
2 files changed, 21 insertions, 8 deletions
@@ -10,6 +10,7 @@ int fvm_init(struct fvm *vm, void *code, int64_t stack_size) { vm->sp = (fvm_word_t)(stack_vm + stack_size); vm->bp = vm->sp; vm->pc = (fvm_word_t)code; + vm->rv = 0; return 1; } @@ -74,14 +75,15 @@ void fvm_store8(struct fvm *vm, fvm_word_t addr, int8_t value) { } -int fvm_execute(struct fvm *vm) { +int64_t fvm_execute(struct fvm *vm) { fvm_word_t a, b, c; fvm_float_t x, y, z; while (1) { enum fvm_op op = (enum fvm_op)(uint8_t)fvm_load8(vm, vm->pc); switch (op) { case FVM_OP_SP: - fvm_push(vm, vm->sp); + a = vm->sp; + fvm_push(vm, a); vm->pc++; break; case FVM_OP_SSP: @@ -96,6 +98,15 @@ int fvm_execute(struct fvm *vm) { vm->bp = fvm_pop(vm); vm->pc++; break; + case FVM_OP_RV: + a = vm->rv; + fvm_push(vm, a); + vm->pc++; + break; + case FVM_OP_SRV: + vm->rv = fvm_pop(vm); + vm->pc++; + break; case FVM_OP_PC: fvm_push(vm, vm->pc); vm->pc++; @@ -410,8 +421,7 @@ int fvm_execute(struct fvm *vm) { vm->pc++; break; case FVM_OP_EXIT: - a = fvm_pop(vm); - return a; + return vm->rv; default: fprintf(stderr, "unknown opcode.\n"); break; @@ -12,9 +12,10 @@ struct fvm; typedef int64_t (*fvm_syscall_fn_t)(struct fvm* vm); struct fvm { - fvm_word_t sp; - fvm_word_t bp; - fvm_word_t pc; + fvm_word_t sp; // stack pointer + fvm_word_t bp; // base pointer + fvm_word_t pc; // programm counter + fvm_word_t rv; // return value fvm_syscall_fn_t *syscall_table; }; @@ -24,6 +25,8 @@ enum fvm_op { FVM_OP_BP, FVM_OP_SBP, FVM_OP_PC, + FVM_OP_RV, + FVM_OP_SRV, FVM_OP_IMM, @@ -95,7 +98,7 @@ enum fvm_op { }; int fvm_init(struct fvm *vm, void *code, int64_t stack_size); -int fvm_execute(struct fvm *vm); +int64_t fvm_execute(struct fvm *vm); void fvm_push(struct fvm *vm, fvm_word_t value); int64_t fvm_pop(struct fvm *vm); |
