From 028c8270f75ac0d6648470774a5e4b8408cc5e5e Mon Sep 17 00:00:00 2001 From: samsonium Date: Sun, 11 Oct 2026 06:00:26 +0300 Subject: [PATCH] ref: merge stack and arg_stack into single stack --- include/common/fzl_opcodes.h | 1 - include/common/fzl_types.h | 10 ++++++ include/vm/fzl_vm.h | 3 -- src/compiler/fzl_walker.c | 4 --- src/vm/fzl_vm.c | 68 +++++++++++++++--------------------- 5 files changed, 38 insertions(+), 48 deletions(-) create mode 100644 include/common/fzl_types.h diff --git a/include/common/fzl_opcodes.h b/include/common/fzl_opcodes.h index 53976d3..625883b 100644 --- a/include/common/fzl_opcodes.h +++ b/include/common/fzl_opcodes.h @@ -5,7 +5,6 @@ typedef enum { FZL_OPCODE_LOADCONST, // Load const into T (temp) reg - FZL_OPCODE_PUSHARG, // Push argument from T into args stack FZL_OPCODE_CALL_BUILTIN, // Call builtin function FZL_OPCODE_PUSHNUM, // Push number into stack diff --git a/include/common/fzl_types.h b/include/common/fzl_types.h new file mode 100644 index 0000000..82fe549 --- /dev/null +++ b/include/common/fzl_types.h @@ -0,0 +1,10 @@ +#ifndef FZL_TYPES_H +#define FZL_TYPES_H + +typedef enum +{ + FZL_TYPE_STRING, + FZL_TYPE_NUMBER +} fzl_type_t; + +#endif // FZL_TYPES_H diff --git a/include/vm/fzl_vm.h b/include/vm/fzl_vm.h index 811c3ea..6d8e54e 100644 --- a/include/vm/fzl_vm.h +++ b/include/vm/fzl_vm.h @@ -18,9 +18,6 @@ typedef struct fzl_const_t stack[256]; size_t stack_top; - - fzl_const_t arg_stack[256]; - size_t arg_top; } fzl_vm_t; // Allocate FZL VM diff --git a/src/compiler/fzl_walker.c b/src/compiler/fzl_walker.c index 8b20abf..87aff38 100644 --- a/src/compiler/fzl_walker.c +++ b/src/compiler/fzl_walker.c @@ -113,7 +113,6 @@ bool walk(fzl_walker_t *walker, fzl_node_t *node) { if (!walk(walker, node->call.args[i])) return false; - push_op(walker, FZL_OPCODE_PUSHARG); } } // NOTE: you need to implement dynamic names call @@ -157,11 +156,9 @@ bool walk(fzl_walker_t *walker, fzl_node_t *node) case FZL_NODETYPE_MOD: if (!walk(walker, node->binary_op.left)) return false; - push_op(walker, FZL_OPCODE_PUSHARG); if (!walk(walker, node->binary_op.right)) return false; - push_op(walker, FZL_OPCODE_PUSHARG); switch (node->type) { @@ -193,7 +190,6 @@ bool walk(fzl_walker_t *walker, fzl_node_t *node) if (!walk(walker, node->unary_op.operand)) return false; - push_op(walker, FZL_OPCODE_PUSHARG); push_op(walker, FZL_OPCODE_NEG); break; } diff --git a/src/vm/fzl_vm.c b/src/vm/fzl_vm.c index dd4c14f..d666bd2 100644 --- a/src/vm/fzl_vm.c +++ b/src/vm/fzl_vm.c @@ -27,8 +27,6 @@ fzl_vm_t *fzl_mkvm(const char *filepath) vm->ip = 0; memset(vm->stack, 0, sizeof(vm->stack)); vm->stack_top = 0; - memset(vm->arg_stack, 0, sizeof(vm->arg_stack)); - vm->arg_top = 0; FILE *fd = fopen(filepath, "rb"); if (!fd) @@ -119,7 +117,7 @@ int fzl_vm_run(fzl_vm_t *vm) uint64_t fn_id = vm->ops[++vm->ip]; if (fn_id == FZL_BUILTIN_PRINTLINE) { - fzl_const_t arg = vm->arg_stack[--vm->arg_top]; + fzl_const_t arg = vm->stack[--vm->stack_top]; if (arg.type == FZL_CONSTTYPE_STRING) printf("%s\n", arg.as.string.chars); else if (arg.type == FZL_CONSTTYPE_NUMBER) @@ -145,81 +143,71 @@ int fzl_vm_run(fzl_vm_t *vm) vm->stack[vm->stack_top++] = c; break; - case FZL_OPCODE_PUSHARG: - if (vm->stack_top < 1) - { - snprintf(last_err, sizeof(last_err), - "runtime error - cannot push argument: stack is empty"); - return -1; - } - vm->arg_stack[vm->arg_top++] = vm->stack[--vm->stack_top]; - break; - case FZL_OPCODE_ADD: - if (vm->arg_top < 2) + if (vm->stack_top < 2) { snprintf(last_err, sizeof(last_err), - "runtime error - expected 2 values in argstack"); + "runtime error - expected 2 values in stack"); return -1; } - fzl_const_t add1 = vm->arg_stack[--vm->arg_top]; - fzl_const_t add2 = vm->arg_stack[--vm->arg_top]; + fzl_const_t add1 = vm->stack[--vm->stack_top]; + fzl_const_t add2 = vm->stack[--vm->stack_top]; vm->stack[vm->stack_top++] = (fzl_const_t){.type = FZL_CONSTTYPE_NUMBER, .as.num.value = add1.as.num.value + add2.as.num.value}; break; case FZL_OPCODE_SUB: - if (vm->arg_top < 2) + if (vm->stack_top < 2) { snprintf(last_err, sizeof(last_err), - "runtime error - expected 2 values in argstack"); + "runtime error - expected 2 values in stack"); return -1; } - fzl_const_t sub1 = vm->arg_stack[--vm->arg_top]; - fzl_const_t sub2 = vm->arg_stack[--vm->arg_top]; + fzl_const_t sub1 = vm->stack[--vm->stack_top]; + fzl_const_t sub2 = vm->stack[--vm->stack_top]; vm->stack[vm->stack_top++] = (fzl_const_t){.type = FZL_CONSTTYPE_NUMBER, .as.num.value = sub1.as.num.value - sub2.as.num.value}; break; case FZL_OPCODE_MUL: - if (vm->arg_top < 2) + if (vm->stack_top < 2) { snprintf(last_err, sizeof(last_err), - "runtime error - expected 2 values in argstack"); + "runtime error - expected 2 values in stack"); return -1; } - fzl_const_t mul1 = vm->arg_stack[--vm->arg_top]; - fzl_const_t mul2 = vm->arg_stack[--vm->arg_top]; + fzl_const_t mul1 = vm->stack[--vm->stack_top]; + fzl_const_t mul2 = vm->stack[--vm->stack_top]; vm->stack[vm->stack_top++] = (fzl_const_t){.type = FZL_CONSTTYPE_NUMBER, .as.num.value = mul1.as.num.value * mul2.as.num.value}; break; case FZL_OPCODE_DIV: - if (vm->arg_top < 2) + if (vm->stack_top < 2) { snprintf(last_err, sizeof(last_err), - "runtime error - expected 2 values in argstack"); + "runtime error - expected 2 values in stack"); return -1; } - fzl_const_t div1 = vm->arg_stack[--vm->arg_top]; - fzl_const_t div2 = vm->arg_stack[--vm->arg_top]; + fzl_const_t div1 = vm->stack[--vm->stack_top]; + fzl_const_t div2 = vm->stack[--vm->stack_top]; vm->stack[vm->stack_top++] = (fzl_const_t){.type = FZL_CONSTTYPE_NUMBER, .as.num.value = div2.as.num.value / div1.as.num.value}; break; case FZL_OPCODE_POW: - if (vm->arg_top < 2) + if (vm->stack_top < 2) { snprintf(last_err, sizeof(last_err), - "runtime error - expected 2 values in argstack"); + "runtime error - expected 2 values in stack"); return -1; } - fzl_const_t pow1 = vm->arg_stack[--vm->arg_top]; - fzl_const_t pow2 = vm->arg_stack[--vm->arg_top]; + fzl_const_t pow1 = vm->stack[--vm->stack_top]; + fzl_const_t pow2 = vm->stack[--vm->stack_top]; uint64_t pow_n = pow1.as.num.value; uint64_t pow_res = pow_n; @@ -232,27 +220,27 @@ int fzl_vm_run(fzl_vm_t *vm) break; case FZL_OPCODE_MOD: - if (vm->arg_top < 2) + if (vm->stack_top < 2) { snprintf(last_err, sizeof(last_err), - "runtime error - expected 2 values in argstack"); + "runtime error - expected 2 values in stack"); return -1; } - fzl_const_t mod1 = vm->arg_stack[--vm->arg_top]; - fzl_const_t mod2 = vm->arg_stack[--vm->arg_top]; + fzl_const_t mod1 = vm->stack[--vm->stack_top]; + fzl_const_t mod2 = vm->stack[--vm->stack_top]; vm->stack[vm->stack_top++] = (fzl_const_t){.type = FZL_CONSTTYPE_NUMBER, .as.num.value = mod2.as.num.value % mod1.as.num.value}; break; case FZL_OPCODE_NEG: - if (vm->arg_top < 1) + if (vm->stack_top < 1) { snprintf(last_err, sizeof(last_err), - "runtime error - expected value in argstack"); + "runtime error - expected value in stack"); return -1; } - fzl_const_t neg_n = vm->arg_stack[--vm->arg_top]; + fzl_const_t neg_n = vm->stack[--vm->stack_top]; vm->stack[vm->stack_top++] = (fzl_const_t){ .type = FZL_CONSTTYPE_NUMBER, .as.num.value = -neg_n.as.num.value}; break;