diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-15 05:03:09 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-15 05:03:09 +0100 |
commit | 54b836ba644bab7e80d948459a006e3053783d5a (patch) | |
tree | c6fc47a507dac73fc9f8e1ea46ed81ed95267b0c | |
parent | cfc9edebbadd0b079567058d5d78ba218f94be2b (diff) | |
download | ovm-54b836ba644bab7e80d948459a006e3053783d5a.tar.gz ovm-54b836ba644bab7e80d948459a006e3053783d5a.tar.bz2 ovm-54b836ba644bab7e80d948459a006e3053783d5a.zip |
Add macro for size of a word
Smaller number of changes necessary to change types.
-rw-r--r-- | src/main.c | 32 |
1 files changed, 17 insertions, 15 deletions
@@ -24,6 +24,17 @@ typedef double f64; typedef uint8_t byte; typedef u64 word; +#define WORD_SIZE sizeof(word) + +typedef union +{ + byte as_byte; + word as_word; +} data_t; + +#define DBYTE(BYTE) ((data_t){.as_byte = (BYTE)}) +#define DWORD(WORD) ((data_t){.as_word = (WORD)}) + #define VM_STACK_MAX 1024 typedef struct @@ -35,15 +46,6 @@ typedef struct } stack; } vm_t; -typedef union -{ - byte as_byte; - word as_word; -} data_t; - -#define DBYTE(BYTE) ((data_t){.as_byte = (BYTE)}) -#define DWORD(WORD) ((data_t){.as_word = (WORD)}) - void vm_push_byte(vm_t *vm, data_t b) { if (vm->stack.pointer >= VM_STACK_MAX) @@ -55,11 +57,11 @@ 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 + sizeof(w) >= VM_STACK_MAX) + if (vm->stack.pointer + WORD_SIZE >= VM_STACK_MAX) // TODO: Error STACK_OVERFLOW return; - memcpy(vm->stack.data + vm->stack.pointer, &w.as_word, sizeof(w.as_word)); - vm->stack.pointer += sizeof(w.as_word); + memcpy(vm->stack.data + vm->stack.pointer, &w.as_word, WORD_SIZE); + vm->stack.pointer += WORD_SIZE; } byte vm_pop_byte(vm_t *vm) @@ -72,12 +74,12 @@ byte vm_pop_byte(vm_t *vm) word vm_pop_word(vm_t *vm) { - if (vm->stack.pointer < sizeof(word)) + if (vm->stack.pointer < WORD_SIZE) // TODO: Error STACK_UNDERFLOW return 0; word w = 0; - memcpy(&w, vm->stack.data + vm->stack.pointer - sizeof(w), sizeof(w)); - vm->stack.pointer -= sizeof(w); + memcpy(&w, vm->stack.data + vm->stack.pointer - WORD_SIZE, WORD_SIZE); + vm->stack.pointer -= WORD_SIZE; return w; } |