aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-10-15 21:12:53 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-10-15 21:12:53 +0100
commit6927557d3f31cd5a75269bb058c5c75022e4f524 (patch)
tree751734011d6375ccbb928ee432da752877d44c28
parent00517fddd68c3d46ad09bebe2c3ac38e791576f7 (diff)
downloadovm-6927557d3f31cd5a75269bb058c5c75022e4f524.tar.gz
ovm-6927557d3f31cd5a75269bb058c5c75022e4f524.tar.bz2
ovm-6927557d3f31cd5a75269bb058c5c75022e4f524.zip
Made vm_pop_* routines more uniform by returning a data_t
-rw-r--r--src/main.c24
1 files 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);