220 lines
5.5 KiB
C
220 lines
5.5 KiB
C
#ifndef VETY_VM_H
|
|
#define VETY_VM_H
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
|
|
|
|
#define MEMORY_SIZE 1024 * 1024 // 虚拟机内存大小
|
|
#define REGISTER_COUNT 64 // 虚拟机寄存器数量
|
|
#define STACK_SIZE 64 // 虚拟机栈大小
|
|
#define STACK_FRAME_SIZE 64 // 虚拟机栈帧大小
|
|
#define STACK_LOCAL_SIZE 64
|
|
#define CONSTANT_POOL_SIZE 64 // 虚拟机常量池大小
|
|
#define GLOBAL_VAR_COUNT 64 // 虚拟机全局变量数量
|
|
|
|
enum {
|
|
OP_PUSH,
|
|
OP_POP,
|
|
OP_MOV, // 移动寄存器到寄存器
|
|
OP_MOV_IMM, // 移动立即数到寄存器
|
|
OP_LOAD_CONST, // 加载常量到寄存器
|
|
OP_STORE_CONST, // 存储寄存器值到常量池
|
|
OP_GLOBAL_LOAD, // 加载全局变量到寄存器
|
|
OP_GLOBAL_STORE, // 存储寄存器值到全局变量
|
|
OP_LOAD_LOCAL, // 加载局部变量到寄存器
|
|
OP_STORE_LOCAL, // 存储寄存器值到局部变量
|
|
|
|
OP_FUNC_DEF, // 函数定义
|
|
OP_CALL, // 调用函数
|
|
OP_NATIVE_CALL, // 调用本地函数
|
|
OP_RETURN, // 函数返回
|
|
OP_ADD, // 加法运算
|
|
OP_SUB, // 减法运算
|
|
OP_MUL, // 乘法运算
|
|
OP_DIV, // 除法运算
|
|
OP_JMP, // 无条件跳转
|
|
OP_JMP_IF_FALSE, // 条件跳转(如果为假)
|
|
OP_JMP_IF_TRUE, // 条件跳转(如果为真)
|
|
OP_CMP_EQ, // 比较相等
|
|
OP_CMP_NE, // 比较不相等
|
|
OP_CMP_LT, // 比较小于
|
|
OP_CMP_LE, // 比较小于等于
|
|
OP_CMP_GT, // 比较大于
|
|
OP_CMP_GE, // 比较大于等于
|
|
OP_BLOCK_START, // 块开始
|
|
OP_BLOCK_END, // 块结束
|
|
OP_END_IF, // 结束if语句
|
|
OP_HALT, // 停止执行
|
|
OP_IF_START, // if块开始
|
|
OP_ELSE, // else块
|
|
OP_IF_END // if块结束
|
|
};
|
|
|
|
typedef enum {
|
|
TYPE_INT8,
|
|
TYPE_INT16,
|
|
TYPE_INT32,
|
|
TYPE_INT64,
|
|
TYPE_FLOAT32,
|
|
TYPE_FLOAT64,
|
|
TYPE_STRING,
|
|
TYPE_BOOL,
|
|
TYPE_CHAR,
|
|
TYPE_OBJECT,
|
|
} VarType;
|
|
|
|
typedef union {
|
|
int8_t int8_val;
|
|
int16_t int16_val;
|
|
int32_t int32_val;
|
|
int64_t int64_val;
|
|
float float32_val;
|
|
double float64_val;
|
|
uint8_t bool_val;
|
|
char char_val;
|
|
void* object_val;
|
|
struct {
|
|
uint64_t len;
|
|
char* value;
|
|
} string_val;
|
|
struct {
|
|
uint64_t code_offset;
|
|
uint16_t* arity;
|
|
} func_val;
|
|
} Value;
|
|
|
|
|
|
typedef struct {
|
|
VarType type;
|
|
Value value;
|
|
} Constant;
|
|
|
|
typedef struct {
|
|
Constant* entries;
|
|
uint64_t count;
|
|
uint64_t capacity;
|
|
} ConstantPool;
|
|
|
|
typedef struct Frame {
|
|
uint64_t ip; // 指令指针
|
|
uint64_t* locals;
|
|
uint64_t *stack;
|
|
uint64_t sp;
|
|
struct Frame* parent; // 父作用域
|
|
uint8_t scope_depth; // 作用域深度
|
|
} Frame;
|
|
|
|
typedef struct {
|
|
char* name;
|
|
Value value;
|
|
VarType type;
|
|
} GlobalVar;
|
|
|
|
|
|
typedef struct VM VM;
|
|
|
|
typedef struct {
|
|
char* name;
|
|
Value (*func)(VM*, Value* args, uint8_t argc);
|
|
} NativeFunction;
|
|
|
|
typedef struct {
|
|
char* name;
|
|
uint64_t code_offset;
|
|
uint8_t param_count;
|
|
uint8_t local_count;
|
|
uint8_t instructions_count;
|
|
} Function;
|
|
|
|
typedef struct {
|
|
uint8_t* code;
|
|
size_t code_size;
|
|
ConstantPool constants;
|
|
size_t constants_size;
|
|
} Program;
|
|
|
|
enum {R0, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15, R16, R17, R18, R19, R20, R21, R22, R23, R24, R25, R26, R27, R28, R29, R30, R31};
|
|
|
|
// 错误类型定义
|
|
typedef enum {
|
|
VM_SUCCESS = 0,
|
|
VM_ERROR_STACK_OVERFLOW,
|
|
VM_ERROR_STACK_UNDERFLOW,
|
|
VM_ERROR_INVALID_OPCODE,
|
|
VM_ERROR_INVALID_REGISTER,
|
|
VM_ERROR_INVALID_CONSTANT,
|
|
VM_ERROR_INVALID_JUMP,
|
|
VM_ERROR_INVALID_NATIVE_FUNCTION,
|
|
VM_ERROR_MEMORY_ALLOCATION,
|
|
VM_ERROR_DIVISION_BY_ZERO,
|
|
VM_ERROR_INVALID_FRAME,
|
|
} VMError;
|
|
|
|
// 修改虚拟机结构体
|
|
typedef struct BlockInfo {
|
|
uint64_t start_ip;
|
|
uint64_t end_ip;
|
|
} BlockInfo;
|
|
|
|
typedef struct VM {
|
|
Program* program;
|
|
ConstantPool constants;
|
|
uint64_t registers[REGISTER_COUNT];
|
|
size_t frame_count;
|
|
Frame* frames;
|
|
uint64_t stack[STACK_SIZE];
|
|
uint64_t sp;
|
|
uint64_t ip;
|
|
|
|
// 全局变量表
|
|
GlobalVar globals[GLOBAL_VAR_COUNT];
|
|
size_t global_count;
|
|
|
|
// 本地函数表
|
|
NativeFunction* native_functions;
|
|
size_t native_function_count;
|
|
|
|
// 用户定义函数表
|
|
Function* functions;
|
|
size_t function_count;
|
|
|
|
// 错误处理
|
|
VMError error;
|
|
char error_message[256];
|
|
uint64_t error_ip; // 错误发生的指令位置
|
|
uint8_t error_opcode; // 错误发生时的操作码
|
|
|
|
// 块追踪
|
|
BlockInfo block_stack[STACK_SIZE];
|
|
uint64_t block_count;
|
|
|
|
// if语句跟踪
|
|
uint64_t if_stack[256]; // 存储if语句开始位置
|
|
uint64_t else_stack[256]; // 存储else语句位置
|
|
int if_sp; // if栈指针
|
|
} VM;
|
|
|
|
|
|
|
|
// 虚拟机创建
|
|
VM *vm_create();
|
|
void vm_destroy(VM *vm);
|
|
void vm_load(VM* vm, Program *program);
|
|
void vm_eval(VM *vm);
|
|
|
|
// 栈
|
|
void vm_push(VM *vm, uint64_t value);
|
|
uint64_t vm_pop(VM *vm);
|
|
uint64_t vm_peek(VM *vm, uint64_t offset);
|
|
int vm_register_native_function(VM* vm, const char* name, Value (*func)(VM*, Value* args, uint8_t argc)); // 更新函数声明
|
|
|
|
// 错误处理函数
|
|
const char* vm_error_to_string(VMError error);
|
|
void vm_set_error(VM* vm, VMError error, const char* message, uint64_t ip, uint8_t opcode);
|
|
VMError vm_get_error(VM* vm);
|
|
const char* vm_get_error_message(VM* vm);
|
|
void print_bytecode(Program program);
|
|
#endif //VETY_VM_H
|