diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-15 05:23:38 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-15 05:23:38 +0100 |
commit | 268fdf0ca06238e4c450e01ad014c4ac7151180c (patch) | |
tree | e4cfabfdb74e9ed02db892bfbfea3caf8e7edceb /src/main.c | |
parent | 724f5e02585990b70185e3ccd43904611e21a5f6 (diff) | |
download | ovm-268fdf0ca06238e4c450e01ad014c4ac7151180c.tar.gz ovm-268fdf0ca06238e4c450e01ad014c4ac7151180c.tar.bz2 ovm-268fdf0ca06238e4c450e01ad014c4ac7151180c.zip |
Added floats (64 bit floating point) push and pop routines
Not machine independent, uses the underlying memcpy routine.
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 36 |
1 files changed, 31 insertions, 5 deletions
@@ -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!"); |