From 6927557d3f31cd5a75269bb058c5c75022e4f524 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sun, 15 Oct 2023 21:12:53 +0100 Subject: Made vm_pop_* routines more uniform by returning a data_t --- src/main.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main.c b/src/main.c index 16059e7..d2c003b 100644 --- a/src/main.c +++ b/src/main.c @@ -71,7 +71,6 @@ void vm_push_word(vm_t *vm, data_t w) { const word mask = ((word)0b11111111) << (i - 8); byte b = (w.as_word & mask) >> (i - 8); - printf("PUSH(%lu): pushed byte %X\n", i, b); vm_push_byte(vm, DBYTE(b)); } } @@ -111,40 +110,39 @@ void vm_mov_float(vm_t *vm, data_t f, word reg) vm->registers.f[reg] = f.as_float; } -byte vm_pop_byte(vm_t *vm) +data_t vm_pop_byte(vm_t *vm) { if (vm->stack.ptr == 0) // TODO: Error STACK_UNDERFLOW - return 0; - return vm->stack.data[--vm->stack.ptr]; + return DBYTE(0); + return DBYTE(vm->stack.data[--vm->stack.ptr]); } -word vm_pop_word(vm_t *vm) +data_t vm_pop_word(vm_t *vm) { if (vm->stack.ptr < WORD_SIZE) // TODO: Error STACK_UNDERFLOW - return 0; + return DWORD(0); word w = 0; for (size_t i = 0; i < WORD_SIZE; ++i) { - byte b = vm_pop_byte(vm); - printf("POP(%lu): popped byte %X\n", i, b); - w = w | ((word)b << (i * 8)); + data_t b = vm_pop_byte(vm); + w = w | ((word)(b.as_byte) << (i * 8)); } - return w; + return DWORD(w); } -f64 vm_pop_float(vm_t *vm) +data_t vm_pop_float(vm_t *vm) { if (vm->stack.ptr < FLOAT_SIZE) // TODO: Error STACK_UNDERFLOW - return 0; + return DFLOAT(0); f64 f = 0; // TODO: Make this machine independent (encode IEEE754 floats // yourself?) memcpy(&f, vm->stack.data + vm->stack.ptr - FLOAT_SIZE, FLOAT_SIZE); vm->stack.ptr -= FLOAT_SIZE; - return f; + return DFLOAT(f); } typedef void (*push_f)(vm_t *, data_t); -- cgit v1.2.3-13-gbd6f