feat: optimize runtime (2x+ faster)

This commit is contained in:
samsonium
2026-10-29 05:21:00 +03:00
parent eeca5e653d
commit 7b804b8809
9 changed files with 189 additions and 25 deletions
+17 -1
View File
@@ -1,4 +1,4 @@
#include "common/fzl_opcodes.h"
#include <common/fzl_opcodes.h>
#include <stdbool.h>
#include <stdio.h>
#include <vm/fzl_vm.h>
@@ -20,6 +20,22 @@ void fzl_vm_print_ops(fzl_vm_t *vm)
switch (vm->ops[i])
{
case FZL_OPCODE_STORE_LOCAL:
{
size_t depth = vm->ops[++i];
size_t slot = vm->ops[++i];
printf("STORE_LOCAL D=%zu S=%zu\n", depth, slot);
break;
}
case FZL_OPCODE_LOAD_LOCAL:
{
size_t depth = vm->ops[++i];
size_t slot = vm->ops[++i];
printf("LOAD_LOCAL D=%zu S=%zu\n", depth, slot);
break;
}
case FZL_OPCODE_BUILD_STR:
printf("BUILD_STR %zu\n", vm->ops[++i]);
break;
+61
View File
@@ -1,4 +1,7 @@
#include <common/fzl_value.h>
#include <stdio.h>
#include <vm/fzl_vm.h>
#include <vm/fzl_vm_env.h>
#include <vm/internals/opcode.h>
// Allocate environment table and increment scope level
@@ -125,3 +128,61 @@ int op_write(fzl_vm_t *vm)
return 0;
}
int op_load_local(fzl_vm_t *vm)
{
size_t depth = vm->ops[++vm->ip];
size_t slot = vm->ops[++vm->ip];
if (depth >= vm->etop)
{
snprintf(last_err, sizeof(last_err), "runtime error - local depth out of range");
print_err_data(vm);
return -1;
}
fzl_vm_env_t *env = vm->estack[vm->etop - 1 - depth];
if (env->size <= slot)
{
snprintf(last_err, sizeof(last_err), "runtime error - local slot out of range");
print_err_data(vm);
return -1;
}
vm->dstack[vm->dtop++] = env->table[slot].value;
return 0;
}
int op_store_local(fzl_vm_t *vm)
{
if (vm->dtop < 1)
{
snprintf(last_err, sizeof(last_err),
"runtime error - expected value in data stack");
print_err_data(vm);
return -1;
}
fzl_value_t value = vm->dstack[--vm->dtop];
size_t depth = vm->ops[++vm->ip];
size_t slot = vm->ops[++vm->ip];
if (depth >= vm->etop)
{
snprintf(last_err, sizeof(last_err), "runtime error - local depth out of range");
print_err_data(vm);
return -1;
}
fzl_vm_env_t *env = vm->estack[vm->etop - 1 - depth];
if (env->size <= slot)
{
snprintf(last_err, sizeof(last_err), "runtime error - local slot out of range");
print_err_data(vm);
return -1;
}
env->table[slot].value = value;
return 0;
}
+4 -4
View File
@@ -9,10 +9,10 @@ const opcode_handler group_stack[] = {
};
// Context-relative opcodes group
const opcode_handler group_ctx[] = {
[1] = op_mk_envtable, [2] = op_rm_envtable, [3] = op_store,
[4] = op_lookup_env, [5] = op_write,
};
const opcode_handler group_ctx[] = {[1] = op_mk_envtable, [2] = op_rm_envtable,
[3] = op_store, [4] = op_lookup_env,
[5] = op_write, [6] = op_load_local,
[7] = op_store_local};
// Flow-relative opcodes group
const opcode_handler group_flow[] = {