Initial code commit.
This commit is contained in:
parent
7aa60fe930
commit
6a7add3e56
13 changed files with 891 additions and 0 deletions
55
src/vm.h
Normal file
55
src/vm.h
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#ifndef FUN_VM_H
|
||||
#define FUN_VM_H
|
||||
|
||||
#include "bytecode.h"
|
||||
|
||||
#define VM_STACK_SIZE 1024
|
||||
#define VM_MAX_FRAMES 128
|
||||
#define VM_MAX_GLOBALS 128
|
||||
#define VM_OUTPUT_SIZE 1024
|
||||
#define FRAME_MAX_LOCALS 64
|
||||
|
||||
static const char *opcode_names[] = {
|
||||
"NOP","LOAD_CONST","LOAD_LOCAL","STORE_LOCAL",
|
||||
"LOAD_GLOBAL","STORE_GLOBAL","ADD","SUB","MUL","DIV",
|
||||
"LT","LTE","GT","GTE","EQ","NEQ","POP","JUMP",
|
||||
"JUMP_IF_FALSE","CALL","RETURN","PRINT","HALT",
|
||||
"MOD","AND","OR","NOT","DUP","SWAP"
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
Bytecode *fn;
|
||||
int ip;
|
||||
Value locals[FRAME_MAX_LOCALS];
|
||||
} Frame;
|
||||
|
||||
typedef struct {
|
||||
Value stack[VM_STACK_SIZE];
|
||||
int sp;
|
||||
|
||||
Frame frames[VM_MAX_FRAMES];
|
||||
int fp; // frame pointer, -1 when no frame
|
||||
|
||||
Value globals[VM_MAX_GLOBALS];
|
||||
|
||||
Value output[VM_OUTPUT_SIZE]; // store printed values
|
||||
int output_count;
|
||||
} VM;
|
||||
|
||||
// initialize VM (zero state)
|
||||
void vm_init(VM *vm);
|
||||
|
||||
// run entry Bytecode (pushes first frame)
|
||||
void vm_run(VM *vm, Bytecode *entry);
|
||||
|
||||
// helper function to clear the output
|
||||
void vm_clear_output(VM *vm);
|
||||
|
||||
void vm_free(VM *vm);
|
||||
|
||||
static inline int opcode_is_valid(int op) {
|
||||
return op >= OP_NOP && op <= OP_SWAP; // all current opcodes
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue