aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-11-01 22:08:06 +0000
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-11-01 22:08:06 +0000
commit90ee9b492db7bf2f8efcf07c30faa719aecec920 (patch)
tree479cf5323250f62f53e5e0369d8e50314436f618
parent0649b3f3804fa1b8924eed9214f33bce6a2c06e8 (diff)
downloadovm-90ee9b492db7bf2f8efcf07c30faa719aecec920.tar.gz
ovm-90ee9b492db7bf2f8efcf07c30faa719aecec920.tar.bz2
ovm-90ee9b492db7bf2f8efcf07c30faa719aecec920.zip
Implemented MGET_STACK and MSET_STACK in the runtime
Very easy, they just pop a word then defer to their normal versions. This is probably the best case where a macro works directly so I didn't even need to write a function form for them first. One thing is that then names for the macros are quite bad right now, probably need renaming.
-rw-r--r--vm/runtime.c21
-rw-r--r--vm/runtime.h110
2 files changed, 92 insertions, 39 deletions
diff --git a/vm/runtime.c b/vm/runtime.c
index 67c288c..4c4d9ff 100644
--- a/vm/runtime.c
+++ b/vm/runtime.c
@@ -60,7 +60,7 @@ const char *err_as_cstr(err_t err)
err_t vm_execute(vm_t *vm)
{
- static_assert(NUMBER_OF_OPCODES == 84, "vm_execute: Out of date");
+ static_assert(NUMBER_OF_OPCODES == 90, "vm_execute: Out of date");
struct Program *prog = &vm->program;
if (prog->ptr >= prog->max)
return ERR_END_OF_PROGRAM;
@@ -113,6 +113,8 @@ err_t vm_execute(vm_t *vm)
OPCODE_IS_TYPE(instruction.opcode, OP_GTE) ||
OPCODE_IS_TYPE(instruction.opcode, OP_PLUS) ||
OPCODE_IS_TYPE(instruction.opcode, OP_MULT) ||
+ OPCODE_IS_TYPE(instruction.opcode, OP_MSET_STACK) ||
+ OPCODE_IS_TYPE(instruction.opcode, OP_MGET_STACK) ||
instruction.opcode == OP_MDELETE || instruction.opcode == OP_MSIZE)
{
prog->ptr++;
@@ -804,6 +806,23 @@ err_t vm_pop_word(vm_t *vm, data_t *ret)
return ERR_OK;
}
+#define VM_MEMORY_STACK_CONSTR(ACTION, TYPE) \
+ err_t vm_##ACTION##_stack_##TYPE(vm_t *vm) \
+ { \
+ data_t n = {0}; \
+ err_t err = vm_pop_word(vm, &n); \
+ if (err) \
+ return err; \
+ return vm_##ACTION##_##TYPE(vm, n.as_word); \
+ }
+
+VM_MEMORY_STACK_CONSTR(mset, byte)
+VM_MEMORY_STACK_CONSTR(mset, hword)
+VM_MEMORY_STACK_CONSTR(mset, word)
+VM_MEMORY_STACK_CONSTR(mget, byte)
+VM_MEMORY_STACK_CONSTR(mget, hword)
+VM_MEMORY_STACK_CONSTR(mget, word)
+
err_t vm_mdelete(vm_t *vm)
{
data_t ptr = {0};
diff --git a/vm/runtime.h b/vm/runtime.h
index 9bb61bc..0f8c41e 100644
--- a/vm/runtime.h
+++ b/vm/runtime.h
@@ -137,6 +137,14 @@ static const word_f WORD_ROUTINES[] = {
[OP_MSET_WORD] = vm_mset_word,
};
+err_t vm_mset_stack_byte(vm_t *);
+err_t vm_mset_stack_hword(vm_t *);
+err_t vm_mset_stack_word(vm_t *);
+
+err_t vm_mget_stack_byte(vm_t *);
+err_t vm_mget_stack_hword(vm_t *);
+err_t vm_mget_stack_word(vm_t *);
+
err_t vm_mdelete(vm_t *);
err_t vm_msize(vm_t *);
@@ -201,44 +209,70 @@ err_t vm_mult_word(vm_t *);
typedef err_t (*stack_f)(vm_t *);
static const stack_f STACK_ROUTINES[] = {
- [OP_MDELETE] = vm_mdelete, [OP_MSIZE] = vm_msize,
-
- [OP_NOT_BYTE] = vm_not_byte, [OP_NOT_HWORD] = vm_not_hword,
- [OP_NOT_WORD] = vm_not_word,
-
- [OP_OR_BYTE] = vm_or_byte, [OP_OR_HWORD] = vm_or_hword,
- [OP_OR_WORD] = vm_or_word,
-
- [OP_AND_BYTE] = vm_and_byte, [OP_AND_HWORD] = vm_and_hword,
- [OP_AND_WORD] = vm_and_word,
-
- [OP_XOR_BYTE] = vm_xor_byte, [OP_XOR_HWORD] = vm_xor_hword,
- [OP_XOR_WORD] = vm_xor_word,
-
- [OP_EQ_BYTE] = vm_eq_byte, [OP_EQ_HWORD] = vm_eq_hword,
- [OP_EQ_WORD] = vm_eq_word,
-
- [OP_LT_BYTE] = vm_lt_byte, [OP_LT_CHAR] = vm_lt_char,
- [OP_LT_INT] = vm_lt_int, [OP_LT_HWORD] = vm_lt_hword,
- [OP_LT_LONG] = vm_lt_long, [OP_LT_WORD] = vm_lt_word,
-
- [OP_LTE_BYTE] = vm_lte_byte, [OP_LTE_CHAR] = vm_lte_char,
- [OP_LTE_INT] = vm_lte_int, [OP_LTE_HWORD] = vm_lte_hword,
- [OP_LTE_LONG] = vm_lte_long, [OP_LTE_WORD] = vm_lte_word,
-
- [OP_GT_BYTE] = vm_gt_byte, [OP_GT_CHAR] = vm_gt_char,
- [OP_GT_INT] = vm_gt_int, [OP_GT_HWORD] = vm_gt_hword,
- [OP_GT_LONG] = vm_gt_long, [OP_GT_WORD] = vm_gt_word,
-
- [OP_GTE_BYTE] = vm_gte_byte, [OP_GTE_CHAR] = vm_gte_char,
- [OP_GTE_INT] = vm_gte_int, [OP_GTE_HWORD] = vm_gte_hword,
- [OP_GTE_LONG] = vm_gte_long, [OP_GTE_WORD] = vm_gte_word,
-
- [OP_PLUS_BYTE] = vm_plus_byte, [OP_PLUS_HWORD] = vm_plus_hword,
- [OP_PLUS_WORD] = vm_plus_word,
-
- [OP_MULT_BYTE] = vm_mult_byte, [OP_MULT_HWORD] = vm_mult_hword,
- [OP_MULT_WORD] = vm_mult_word,
+ [OP_MGET_STACK_BYTE] = vm_mget_stack_byte,
+ [OP_MGET_STACK_HWORD] = vm_mget_stack_hword,
+ [OP_MGET_STACK_WORD] = vm_mget_stack_word,
+ [OP_MSET_STACK_BYTE] = vm_mset_stack_byte,
+ [OP_MSET_STACK_HWORD] = vm_mset_stack_hword,
+ [OP_MSET_STACK_WORD] = vm_mset_stack_word,
+ [OP_MDELETE] = vm_mdelete,
+ [OP_MSIZE] = vm_msize,
+
+ [OP_NOT_BYTE] = vm_not_byte,
+ [OP_NOT_HWORD] = vm_not_hword,
+ [OP_NOT_WORD] = vm_not_word,
+
+ [OP_OR_BYTE] = vm_or_byte,
+ [OP_OR_HWORD] = vm_or_hword,
+ [OP_OR_WORD] = vm_or_word,
+
+ [OP_AND_BYTE] = vm_and_byte,
+ [OP_AND_HWORD] = vm_and_hword,
+ [OP_AND_WORD] = vm_and_word,
+
+ [OP_XOR_BYTE] = vm_xor_byte,
+ [OP_XOR_HWORD] = vm_xor_hword,
+ [OP_XOR_WORD] = vm_xor_word,
+
+ [OP_EQ_BYTE] = vm_eq_byte,
+ [OP_EQ_HWORD] = vm_eq_hword,
+ [OP_EQ_WORD] = vm_eq_word,
+
+ [OP_LT_BYTE] = vm_lt_byte,
+ [OP_LT_CHAR] = vm_lt_char,
+ [OP_LT_INT] = vm_lt_int,
+ [OP_LT_HWORD] = vm_lt_hword,
+ [OP_LT_LONG] = vm_lt_long,
+ [OP_LT_WORD] = vm_lt_word,
+
+ [OP_LTE_BYTE] = vm_lte_byte,
+ [OP_LTE_CHAR] = vm_lte_char,
+ [OP_LTE_INT] = vm_lte_int,
+ [OP_LTE_HWORD] = vm_lte_hword,
+ [OP_LTE_LONG] = vm_lte_long,
+ [OP_LTE_WORD] = vm_lte_word,
+
+ [OP_GT_BYTE] = vm_gt_byte,
+ [OP_GT_CHAR] = vm_gt_char,
+ [OP_GT_INT] = vm_gt_int,
+ [OP_GT_HWORD] = vm_gt_hword,
+ [OP_GT_LONG] = vm_gt_long,
+ [OP_GT_WORD] = vm_gt_word,
+
+ [OP_GTE_BYTE] = vm_gte_byte,
+ [OP_GTE_CHAR] = vm_gte_char,
+ [OP_GTE_INT] = vm_gte_int,
+ [OP_GTE_HWORD] = vm_gte_hword,
+ [OP_GTE_LONG] = vm_gte_long,
+ [OP_GTE_WORD] = vm_gte_word,
+
+ [OP_PLUS_BYTE] = vm_plus_byte,
+ [OP_PLUS_HWORD] = vm_plus_hword,
+ [OP_PLUS_WORD] = vm_plus_word,
+
+ [OP_MULT_BYTE] = vm_mult_byte,
+ [OP_MULT_HWORD] = vm_mult_hword,
+ [OP_MULT_WORD] = vm_mult_word,
};
#endif