From d3c63dab5c4ffab4ce8a360f46f0c30c10acd66a Mon Sep 17 00:00:00 2001 From: zzy <2450266535@qq.com> Date: Sun, 16 Aug 2026 10:51:45 +0800 Subject: [PATCH] =?UTF-8?q?stage0=20=E9=87=8D=E5=91=BD=E5=90=8D=20stage1?= =?UTF-8?q?=20ast=E9=87=8D=E6=96=B0=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stage0/spl_disasm.c | 2 +- stage0/spl_mcode.c | 4 ++-- stage0/spl_mcode.h | 4 ++-- stage0/spl_vm.c | 13 +++++++------ stage1/spl_ast.c | 15 ++++++++++----- stage1/spl_ast.h | 10 +++++----- 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/stage0/spl_disasm.c b/stage0/spl_disasm.c index f4a4cd1..6e50728 100644 --- a/stage0/spl_disasm.c +++ b/stage0/spl_disasm.c @@ -54,7 +54,7 @@ int main(int argc, const char **argv) { spl_vm_ins_t *ins = &vec_at(prog.insns, i); printf("%4zd: %s", i, spl_vm_opcode_name((spl_vm_opcode_t)ins->opcode)); if (ins->type != SPL_VOID) - printf(" %s", spl_vm_type_kind_name((spl_type_t)ins->type)); + printf(" %s", spl_vm_type_kind_name((spl_vm_kind_t)ins->type)); printf(" %zd", ins->imm); /* annotate jumps / calls */ diff --git a/stage0/spl_mcode.c b/stage0/spl_mcode.c index 22fb91f..249d15d 100644 --- a/stage0/spl_mcode.c +++ b/stage0/spl_mcode.c @@ -427,7 +427,7 @@ const char *opcode_name[] = { #undef X }; const char *spl_vm_opcode_name(spl_vm_opcode_t opcode) { return opcode_name[opcode]; } -const char *spl_vm_type_kind_name(spl_type_t type) { +const char *spl_vm_type_kind_name(spl_vm_kind_t type) { switch (type) { case SPL_VOID: return "void"; @@ -467,7 +467,7 @@ const char *spl_vm_type_kind_name(spl_type_t type) { void spl_vm_ins_dump(spl_vm_ins_t *ins, spl_vm_val_t addr) { printf("%4zu: %s", addr, spl_vm_opcode_name((spl_vm_opcode_t)ins->opcode)); if (ins->type != SPL_VOID) - printf(" %s", spl_vm_type_kind_name((spl_type_t)ins->type)); + printf(" %s", spl_vm_type_kind_name((spl_vm_kind_t)ins->type)); printf(" %zd:%zx", ins->imm, ins->imm); printf("\n"); } diff --git a/stage0/spl_mcode.h b/stage0/spl_mcode.h index 67e6e99..c5bbeab 100644 --- a/stage0/spl_mcode.h +++ b/stage0/spl_mcode.h @@ -29,7 +29,7 @@ typedef enum { SPL_F64, SPL_PTR, SPL_TYPE_COUNT, -} spl_type_t; +} spl_vm_kind_t; /* clang-format off */ #define SPL_OPCODES(X) \ @@ -213,7 +213,7 @@ spl_vm_native_t *spl_prog_get_native(spl_prog_t *prog, const char *name); /* Opcode name lookup for debugging/dumping */ const char *spl_vm_opcode_name(spl_vm_opcode_t opcode); -const char *spl_vm_type_kind_name(spl_type_t type); +const char *spl_vm_type_kind_name(spl_vm_kind_t type); void spl_vm_ins_dump(spl_vm_ins_t *ins, spl_vm_val_t addr); diff --git a/stage0/spl_vm.c b/stage0/spl_vm.c index f7d0db2..c208f36 100644 --- a/stage0/spl_vm.c +++ b/stage0/spl_vm.c @@ -28,8 +28,8 @@ * Type helpers * ================================================================ */ -static int spl_is_float(spl_type_t t) { return t == SPL_F32 || t == SPL_F64; } -static int spl_is_signed(spl_type_t t) { +static int spl_is_float(spl_vm_kind_t t) { return t == SPL_F32 || t == SPL_F64; } +static int spl_is_signed(spl_vm_kind_t t) { switch (t) { case SPL_I8: case SPL_I16: @@ -41,7 +41,7 @@ static int spl_is_signed(spl_type_t t) { return 0; } } -static int spl_type_size(spl_type_t t) { +static int spl_type_size(spl_vm_kind_t t) { switch (t) { case SPL_VOID: return 0; @@ -123,7 +123,7 @@ static int spl_type_size(spl_type_t t) { do { \ spl_vm_val_t _b = POP(), _a = POP(); \ spl_vm_val_t _r = 0; \ - if (spl_is_float((spl_type_t)ins->type)) { \ + if (spl_is_float((spl_vm_kind_t)ins->type)) { \ double _da, _db, _dr; \ if (ins->type == SPL_F32) { \ float _fa, _fb; \ @@ -185,7 +185,7 @@ static int spl_type_size(spl_type_t t) { if (_b == 0) \ VM_ERROR("division by zero"); \ spl_vm_val_t _r = 0; \ - if (spl_is_float((spl_type_t)ins->type)) { \ + if (spl_is_float((spl_vm_kind_t)ins->type)) { \ double _da, _db, _dr; \ if (ins->type == SPL_F32) { \ float _fa, _fb; \ @@ -545,6 +545,7 @@ LONG WINAPI UnhandledExceptionFilterImpl(EXCEPTION_POINTERS *pExceptionInfo) { * ================================================================ */ void spl_vm_init_ex(spl_vm_t *vm, int stack_size, int call_depth) { #ifdef _WIN32 + SetErrorMode(SEM_FAILCRITICALERRORS); SetUnhandledExceptionFilter(UnhandledExceptionFilterImpl); SetConsoleOutputCP(CP_UTF8); SetConsoleCP(CP_UTF8); @@ -843,7 +844,7 @@ int spl_vm_run_once(spl_vm_t *vm) { case SPL_NEG: { spl_vm_val_t _a = POP(); - if (spl_is_float((spl_type_t)ins->type)) { + if (spl_is_float((spl_vm_kind_t)ins->type)) { double _d; if (ins->type == SPL_F32) { float _f; diff --git a/stage1/spl_ast.c b/stage1/spl_ast.c index ceebb04..0524496 100644 --- a/stage1/spl_ast.c +++ b/stage1/spl_ast.c @@ -1792,8 +1792,8 @@ void spl_ast_prase(spl_ast_t *ast) { if (d) vec_push(members, d); } - spl_ast_node_ref_t root = new_node(&p, SPL_AST_CONTAINER_ITEM, begin); - node_at(ast, root)->container_item.members = members; + spl_ast_node_ref_t root = new_node(&p, SPL_AST_CONTAINER_MEMBERS, begin); + node_at(ast, root)->container_members = members; ast->root = root; ast->parsed = p.failed ? -1 : 1; } @@ -1809,6 +1809,12 @@ void spl_ast_drop(spl_ast_t *ast) { ast->root = 0; } +spl_ast_node_t *spl_ast_node(spl_ast_t *ast, spl_ast_node_ref_t node) { + if (!ast || node == 0) + return NULL; + return &vec_at(ast->node_buckets, node); +} + void spl_ast_valid(spl_ast_t *ast) { if (!ast) return; @@ -1851,9 +1857,8 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t UNREACHABLE(); break; - case SPL_AST_CONTAINER_ITEM: - dump_vec(ast, node->container_item.attr_list, stack); - dump_vec(ast, node->container_item.members, stack); + case SPL_AST_CONTAINER_MEMBERS: + dump_vec(ast, node->container_members, stack); break; case SPL_AST_FN_DECL: diff --git a/stage1/spl_ast.h b/stage1/spl_ast.h index f989f21..d991210 100644 --- a/stage1/spl_ast.h +++ b/stage1/spl_ast.h @@ -9,7 +9,7 @@ /* clang-format off */ #define SPL_AST_KIND_TABLE \ X(SPL_AST_NONE, V0, none) \ - X(SPL_AST_CONTAINER_ITEM, V0, container_item) \ + X(SPL_AST_CONTAINER_MEMBERS, V0, container_members) \ X(SPL_AST_FN_DECL, V0, fn_decl) \ X(SPL_AST_FN_DEFINE, V0, fn_define) \ X(SPL_AST_TYPE_DECL, V0, type_decl) \ @@ -97,6 +97,7 @@ X(SPL_AST_TYPE_SLICE, V0, type_slice) \ X(SPL_AST_TYPE_STRUCT, V0, type_struct) \ X(SPL_AST_TYPE_UNION, V0, type_union) \ + X(SPL_AST_TYPE_SHAPE, V0, type_shape) \ X(SPL_AST_TYPE_ENUM, V0, type_enum) \ X(SPL_AST_TYPE_VOID, V0, type_void) \ X(SPL_AST_TYPE_BOOL, V0, type_bool) \ @@ -140,10 +141,7 @@ struct spl_ast_node { usize resolved_def_id; union { - struct { - spl_ast_node_ref_vec_t attr_list; /* attr_item */ - spl_ast_node_ref_vec_t members; /* container_decl 列表 */ - } container_item; + spl_ast_node_ref_vec_t container_members; /* container_decl */ struct { const char *ident; @@ -314,6 +312,8 @@ typedef struct { void spl_ast_init(spl_ast_t *ast, const spl_tok_vec_t *tok_vec /*move*/); void spl_ast_drop(spl_ast_t *ast); +spl_ast_node_t* spl_ast_node(spl_ast_t *ast, spl_ast_node_ref_t node); + void spl_ast_prase(spl_ast_t *ast); void spl_ast_valid(spl_ast_t *ast); void spl_ast_dump(spl_ast_t *ast, spl_ast_node_ref_t node);