feat: optimize runtime (2x+ faster)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user