From 268fdf0ca06238e4c450e01ad014c4ac7151180c Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sun, 15 Oct 2023 05:23:38 +0100 Subject: Added floats (64 bit floating point) push and pop routines Not machine independent, uses the underlying memcpy routine. --- src/main.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 1269fe7..3abc801 100644 --- a/src/main.c +++ b/src/main.c @@ -24,16 +24,19 @@ typedef double f64; typedef uint8_t byte; typedef u64 word; -#define WORD_SIZE sizeof(word) - typedef union { byte as_byte; word as_word; + f64 as_float; } data_t; -#define DBYTE(BYTE) ((data_t){.as_byte = (BYTE)}) -#define DWORD(WORD) ((data_t){.as_word = (WORD)}) +#define DBYTE(BYTE) ((data_t){.as_byte = (BYTE)}) +#define DWORD(WORD) ((data_t){.as_word = (WORD)}) +#define DFLOAT(FLOAT) ((data_t){.as_float = (FLOAT)}) + +#define WORD_SIZE sizeof(word) +#define FLOAT_SIZE sizeof(f64) #define VM_STACK_MAX 1024 @@ -56,7 +59,6 @@ void vm_push_byte(vm_t *vm, data_t b) void vm_push_word(vm_t *vm, data_t w) { - // NOTE: Relies on sizeof measuring in bytes if (vm->stack.pointer + WORD_SIZE >= VM_STACK_MAX) // TODO: Error STACK_OVERFLOW return; @@ -70,6 +72,17 @@ void vm_push_word(vm_t *vm, data_t w) } } +void vm_push_float(vm_t *vm, data_t f) +{ + if (vm->stack.pointer + FLOAT_SIZE >= VM_STACK_MAX) + // TODO: Error STACK_OVERFLOW + return; + // TODO: Make this machine independent (encode IEEE754 floats + // yourself?) + memcpy(vm->stack.data + vm->stack.pointer, &f.as_float, FLOAT_SIZE); + vm->stack.pointer += FLOAT_SIZE; +} + byte vm_pop_byte(vm_t *vm) { if (vm->stack.pointer == 0) @@ -93,6 +106,19 @@ word vm_pop_word(vm_t *vm) return w; } +f64 vm_pop_float(vm_t *vm) +{ + if (vm->stack.pointer < FLOAT_SIZE) + // TODO: Error STACK_UNDERFLOW + return 0; + f64 f = 0; + // TODO: Make this machine independent (encode IEEE754 floats + // yourself?) + memcpy(&f, vm->stack.data + vm->stack.pointer - FLOAT_SIZE, FLOAT_SIZE); + vm->stack.pointer -= FLOAT_SIZE; + return f; +} + int main(void) { puts("Hello, world!"); -- cgit v1.2.3-13-gbd6f