feat: optimize VM using dispatch table
This commit is contained in:
+5
-1
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <common/fzl_value.h>
|
||||
#include <compiler/utils/fzl_const_pool.h> // NOTE: move outside compiler scope to common
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <vm/fzl_vm_env.h>
|
||||
@@ -23,6 +24,9 @@ typedef struct
|
||||
|
||||
size_t ip;
|
||||
|
||||
bool allow_infinite;
|
||||
size_t max_iter;
|
||||
|
||||
// Data stack
|
||||
fzl_value_t dstack[256];
|
||||
size_t dtop;
|
||||
@@ -40,7 +44,7 @@ typedef struct
|
||||
fzl_vm_t *fzl_mkvm(const char *filepath);
|
||||
|
||||
// Load and run code
|
||||
int fzl_vm_run(fzl_vm_t *vm, bool allow_infinite, uint64_t max_iter);
|
||||
int fzl_vm_run(fzl_vm_t *vm);
|
||||
|
||||
// Get last VM error message
|
||||
char *fzl_vm_geterror();
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
#ifndef INTERNALS_OPCODE_H
|
||||
#define INTERNALS_OPCODE_H
|
||||
|
||||
#include "vm/fzl_vm.h"
|
||||
#include <stddef.h>
|
||||
|
||||
// Opcode handler descriptor
|
||||
typedef int (*opcode_handler)(fzl_vm_t *);
|
||||
|
||||
extern char last_err[256];
|
||||
extern size_t last_jmp_ip;
|
||||
extern size_t total_jmp_on_same;
|
||||
|
||||
void print_object(fzl_vm_object_t *obj);
|
||||
void print_err_data(fzl_vm_t *vm);
|
||||
|
||||
// --- stack ---
|
||||
|
||||
extern const opcode_handler group_stack[];
|
||||
|
||||
int op_loadconst(fzl_vm_t *vm);
|
||||
int op_call_builtin(fzl_vm_t *vm);
|
||||
int op_pushnum(fzl_vm_t *vm);
|
||||
int op_pushbool(fzl_vm_t *vm);
|
||||
|
||||
// --- context ---
|
||||
|
||||
extern const opcode_handler group_ctx[];
|
||||
|
||||
int op_mk_envtable(fzl_vm_t *vm);
|
||||
int op_rm_envtable(fzl_vm_t *vm);
|
||||
int op_store(fzl_vm_t *vm);
|
||||
int op_lookup_env(fzl_vm_t *vm);
|
||||
int op_write(fzl_vm_t *vm);
|
||||
|
||||
// --- flow ---
|
||||
|
||||
extern const opcode_handler group_flow[];
|
||||
|
||||
int op_jmp(fzl_vm_t *vm);
|
||||
int op_jz(fzl_vm_t *vm);
|
||||
|
||||
// --- functions ---
|
||||
|
||||
extern const opcode_handler group_fn[];
|
||||
|
||||
int op_call(fzl_vm_t *vm);
|
||||
int op_ret(fzl_vm_t *vm);
|
||||
int op_call_extern(fzl_vm_t *vm);
|
||||
|
||||
// --- objects ---
|
||||
|
||||
extern const opcode_handler group_obj[];
|
||||
|
||||
int op_alloc_object(fzl_vm_t *vm);
|
||||
int op_get_field(fzl_vm_t *vm);
|
||||
int op_set_field(fzl_vm_t *vm);
|
||||
int op_escape_object(fzl_vm_t *vm);
|
||||
int op_dereference(fzl_vm_t *vm);
|
||||
|
||||
// --- math ---
|
||||
|
||||
extern const opcode_handler group_math[];
|
||||
|
||||
int op_add(fzl_vm_t *vm);
|
||||
int op_sub(fzl_vm_t *vm);
|
||||
int op_mul(fzl_vm_t *vm);
|
||||
int op_div(fzl_vm_t *vm);
|
||||
int op_pow(fzl_vm_t *vm);
|
||||
int op_mod(fzl_vm_t *vm);
|
||||
int op_neg(fzl_vm_t *vm);
|
||||
|
||||
// --- strings ---
|
||||
|
||||
extern const opcode_handler group_str[];
|
||||
|
||||
int op_build_str(fzl_vm_t *vm);
|
||||
|
||||
// --- conditions ---
|
||||
|
||||
extern const opcode_handler group_cmp[];
|
||||
int op_cmp_eq(fzl_vm_t *vm);
|
||||
int op_cmp_neq(fzl_vm_t *vm);
|
||||
int op_cmp_lt(fzl_vm_t *vm);
|
||||
int op_cmp_lteq(fzl_vm_t *vm);
|
||||
int op_cmp_gt(fzl_vm_t *vm);
|
||||
int op_cmp_gteq(fzl_vm_t *vm);
|
||||
|
||||
extern const opcode_handler group_strcmp[];
|
||||
int op_cmp_eq_str(fzl_vm_t *vm);
|
||||
int op_cmp_neq_str(fzl_vm_t *vm);
|
||||
|
||||
extern const opcode_handler group_objcmp[];
|
||||
int op_cmp_eq_obj(fzl_vm_t *vm);
|
||||
int op_cmp_neq_obj(fzl_vm_t *vm);
|
||||
|
||||
extern const opcode_handler group_and_or[];
|
||||
int op_and(fzl_vm_t *vm);
|
||||
int op_or(fzl_vm_t *vm);
|
||||
|
||||
extern const opcode_handler group_not[];
|
||||
int op_not(fzl_vm_t *vm);
|
||||
|
||||
// ------------------------
|
||||
|
||||
extern const opcode_handler *const master_tbl[];
|
||||
|
||||
#endif // INTERNALS_OPCODE_H
|
||||
Reference in New Issue
Block a user