ai: 重新设计stage1的spl 让ai重构代码

This commit is contained in:
zzy committed 2026-12-02 12:40:36 +08:00
1 parent 032102f1d7
commit e531aab2d3
44 files changed
+708 -422

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
#ifndef __IR_OPT_H__
#define __IR_OPT_H__
/* IR 层优化 pass:输入输出都是 spl_ir_t(节点 arena),纯函数式、可开关。
* 由 splc0 --opt 在 ast2ir 之后、ir2vm 之前调用。 */
#include "spl_ir.h"
void spl_ir_opt(spl_ir_t *ir, const spl_attr_t *attr);
#endif /* __IR_OPT_H__ */
+500
View File
@@ -0,0 +1,500 @@
#ifndef __SPL_AST_H__
#define __SPL_AST_H__
#include "stage0/include/utils.h"
#include "spl_attr.h"
#include "spl_dbg.h"
#include "spl_tok.h"
/* clang-format off */
#define SPL_AST_KIND_TABLE \
X(SPL_AST_NONE, V0, none) \
X(SPL_AST_ROOT, V0, root) \
X(SPL_AST_CONTAINER_MEMBERS, V0, container_members) \
X(SPL_AST_EXTERNAL_TAG_ITEM, V0, external_tag_item) \
X(SPL_AST_TAG_ITEM, V0, tag_item) \
X(SPL_AST_MOD_DECL, V0, mod_decl) \
X(SPL_AST_IMPORT_DECL, V0, import_decl) \
X(SPL_AST_TOP_LEVEL_BLOCK, V0, top_level_block) \
X(SPL_AST_FN_DECL, V0, fn_decl) \
X(SPL_AST_VAR_DECL, V0, var_decl) \
X(SPL_AST_CONST_DECL, V0, const_decl) \
X(SPL_AST_TYPE_DECL, V0, type_decl) \
X(SPL_AST_MEMBER_DECL, V0, member_decl) \
X(SPL_AST_PARAM_DECL, V0, param_decl) \
X(SPL_AST_STATEMENT_RET, V0, statement_ret) \
X(SPL_AST_STATEMENT_BREAK, V0, statement_break) \
X(SPL_AST_STATEMENT_CONTINUE, V0, statement_continue) \
X(SPL_AST_STATEMENT_DEFER, V0, statement_defer) \
X(SPL_AST_STATEMENT_DECL, V0, statement_decl) \
X(SPL_AST_STATEMENT_EXPR, V0, statement_expr) \
X(SPL_AST_AGGREGATE_MEMBER, V0, aggregate_member) \
X(SPL_AST_STRUCT_INIT_ITEM, V0, struct_init_item) \
X(SPL_AST_MATCH_ARM, V0, match_arm) \
X(SPL_AST_LABEL, V0, label) \
X(SPL_AST_EXPR_INT_LIT, V0, int_lit) \
X(SPL_AST_EXPR_FLOAT_LIT, V0, float_lit) \
X(SPL_AST_EXPR_CHAR_LIT, V0, char_lit) \
X(SPL_AST_EXPR_STRING_LIT, V0, string_lit) \
X(SPL_AST_EXPR_BOOL_LIT, V0, bool_lit) \
X(SPL_AST_EXPR_NULL_LIT, V0, null_lit) \
X(SPL_AST_EXPR_UNDEFINED_LIT, V0, undefined_lit) \
X(SPL_AST_EXPR_IDENT, V0, ident) \
X(SPL_AST_EXPR_UNARY, V0, unary) \
X(SPL_AST_EXPR_BINARY, V0, binary) \
X(SPL_AST_EXPR_CALL, V0, call) \
X(SPL_AST_EXPR_FIELD, V0, field) \
X(SPL_AST_EXPR_INDEX, V0, index) \
X(SPL_AST_EXPR_SLICE, V0, slice) \
X(SPL_AST_EXPR_CAST, V0, cast) \
X(SPL_AST_EXPR_TUPLE, V0, tuple) \
X(SPL_AST_EXPR_ARRAY_LIT, V0, array_literal) \
X(SPL_AST_EXPR_AGGREGATE_LIT, V0, aggregate_literal) \
X(SPL_AST_EXPR_COMPILE_CALL, V0, compile_call) \
X(SPL_AST_EXPR_BLOCK, V0, block) \
X(SPL_AST_EXPR_IF, V0, if_expr) \
X(SPL_AST_EXPR_IFVAR, V0, ifvar_expr) \
X(SPL_AST_EXPR_WHILE, V0, while_expr) \
X(SPL_AST_EXPR_LOOP, V0, loop_expr) \
X(SPL_AST_EXPR_FOR, V0, for_expr) \
X(SPL_AST_EXPR_MATCH, V0, match_expr) \
X(SPL_AST_EXPR_FN_TYPE, V0, fn_type) \
X(SPL_AST_TYPE_PATH, V0, type_path) \
X(SPL_AST_TYPE_POINTER, V0, type_pointer) \
X(SPL_AST_TYPE_ARRAY, V0, type_array) \
X(SPL_AST_TYPE_SLICE, V0, type_slice) \
X(SPL_AST_TYPE_FN, V0, type_fn) \
X(SPL_AST_TYPE_PAREN, V0, type_paren) \
X(SPL_AST_TYPE_AGGREGATE, V0, type_aggregate) \
X(SPL_AST_TYPE_COMPILE_CALL, V0, type_compile_call) \
X(SPL_AST_PATTERN_IDENT, V0, pattern_ident) \
X(SPL_AST_PATTERN_UNDERSCORE, V0, pattern_underscore) \
X(SPL_AST_PATTERN_DOT, V0, pattern_dot) \
X(SPL_AST_PATTERN_TUPLE, V0, pattern_tuple) \
X(SPL_AST_PATTERN_LIST, V0, pattern_list) \
X(SPL_AST_PATTERN_AGGREGATE, V0, pattern_aggregate) \
X(SPL_AST_PATTERN_FIELD, V0, pattern_field) \
X(SPL_AST_PATTERN_REF, V0, pattern_ref)
/* clang-format on */
typedef enum {
#ifdef X
#undef X
#endif
#define X(name, ...) name,
SPL_AST_KIND_TABLE
#undef X
SPL_AST_COUNT,
} spl_ast_node_kind_t;
/* clang-format off */
#define SPL_BINARY_OP_TABLE \
X(SPL_OP_ASSIGN, 1, 'R', =) \
X(SPL_OP_ASSIGN_ADD, 1, 'R', +=) \
X(SPL_OP_ASSIGN_SUB, 1, 'R', -=) \
X(SPL_OP_ASSIGN_MUL, 1, 'R', *=) \
X(SPL_OP_ASSIGN_DIV, 1, 'R', /=) \
X(SPL_OP_ASSIGN_MOD, 1, 'R', %=) \
X(SPL_OP_ASSIGN_AND, 1, 'R', &=) \
X(SPL_OP_ASSIGN_OR, 1, 'R', |=) \
X(SPL_OP_ASSIGN_XOR, 1, 'R', ^=) \
X(SPL_OP_ASSIGN_LSHIFT, 1, 'R', <<=) \
X(SPL_OP_ASSIGN_RSHIFT, 1, 'R', >>=) \
X(SPL_OP_BOOL_OR, 2, 'L', ||) \
X(SPL_OP_BOOL_AND, 3, 'L', &&) \
X(SPL_OP_BIT_OR, 4, 'L', |) \
X(SPL_OP_BIT_XOR, 5, 'L', ^) \
X(SPL_OP_BIT_AND, 6, 'L', &) \
X(SPL_OP_CMP_EQ, 7, 'L', ==) \
X(SPL_OP_CMP_NE, 7, 'L', !=) \
X(SPL_OP_CMP_LE, 8, 'L', <=) \
X(SPL_OP_CMP_GE, 8, 'L', >=) \
X(SPL_OP_CMP_LT, 8, 'L', <) \
X(SPL_OP_CMP_GT, 8, 'L', >) \
X(SPL_OP_ARROW_LEFT, 9, 'L', <-) \
X(SPL_OP_ARROW_RIGHT, 9, 'L', ->) \
X(SPL_OP_ARROW_FAT_BOTH, 9, 'L', <=>) \
X(SPL_OP_ARROW_THIN_BOTH, 9, 'L', <->) \
X(SPL_OP_ARROW_FAT_RIGHT, 9, 'L', =>) \
X(SPL_OP_RANGE, 10, 'L', ..) \
X(SPL_OP_LSHIFT, 11, 'L', <<) \
X(SPL_OP_RSHIFT, 11, 'L', >>) \
X(SPL_OP_ADD, 12, 'L', +) \
X(SPL_OP_SUB, 12, 'L', -) \
X(SPL_OP_MUL, 13, 'L', *) \
X(SPL_OP_DIV, 13, 'L', /) \
X(SPL_OP_MOD, 13, 'L', %)
/* clang-format on */
typedef enum {
#ifdef X
#undef X
#endif
#define X(name, ...) name,
SPL_BINARY_OP_TABLE
#undef X
} spl_binary_op_t;
typedef enum {
SPL_UNARY_OP_MINUS,
SPL_UNARY_OP_NOT,
SPL_UNARY_OP_BIT_NOT,
SPL_UNARY_OP_ADDRESS,
SPL_UNARY_OP_DEREF,
} spl_unary_op_t;
typedef enum {
SPL_FN_KIND_NORMAL,
SPL_FN_KIND_META,
SPL_FN_KIND_MACRO,
} spl_fn_kind_t;
struct spl_ast_node;
typedef struct spl_ast_node spl_ast_node_t;
typedef usize spl_ast_node_ref_t;
typedef VEC(spl_ast_node_ref_t) spl_ast_node_ref_vec_t;
struct spl_ast_node {
spl_ast_node_kind_t kind;
spl_dbg_node_t dbg;
usize resolved_def_id;
usize resolved_lang_tid;
usize expected_lang_tid;
spl_attr_id_vec_t attrs;
union {
struct {
spl_ast_node_ref_t members;
} root;
struct {
spl_ast_node_ref_vec_t external_tags;
spl_ast_node_ref_t module;
spl_ast_node_ref_vec_t declarations;
} container_members;
struct {
spl_ast_node_ref_vec_t decorator_list;
spl_ast_node_ref_vec_t attr_list;
spl_fn_kind_t fn_kind;
const char *name;
spl_ast_node_ref_vec_t params;
spl_ast_node_ref_t ret_type;
spl_ast_node_ref_t body;
} fn_decl;
struct {
spl_ast_node_ref_vec_t decorator_list;
spl_ast_node_ref_vec_t attr_list;
spl_ast_node_ref_t pattern;
spl_ast_node_ref_t type;
spl_ast_node_ref_t init_expr;
} var_const_decl;
struct {
spl_ast_node_ref_vec_t decorator_list;
spl_ast_node_ref_vec_t attr_list;
spl_ast_node_ref_t pattern;
spl_ast_node_ref_t type_expr;
} type_decl;
struct {
spl_ast_node_ref_vec_t decorator_list;
spl_ast_node_ref_vec_t attr_list;
const char *name;
spl_ast_node_ref_t type_expr;
spl_ast_node_ref_t init_expr;
} member_decl;
struct {
spl_ast_node_ref_vec_t attr_list;
const char *name;
spl_ast_node_ref_t type_expr;
} param_decl;
struct {
bool is_at;
bool is_external;
const char *name;
spl_ast_node_ref_vec_t args;
} tag_item;
struct {
spl_ast_node_ref_vec_t decorator_list;
const char *path;
bool has_body;
spl_ast_node_ref_vec_t decls;
} mod_decl;
struct {
spl_ast_node_ref_vec_t decorator_list;
bool is_path;
const char *path;
const char *alias;
} import_decl;
struct {
spl_ast_node_ref_vec_t decorator_list;
spl_ast_node_ref_vec_t attr_list;
spl_ast_node_ref_t block;
} top_level_block;
struct {
spl_ast_node_ref_t expr;
} statement_ret;
struct {
spl_ast_node_ref_t label;
spl_ast_node_ref_t expr;
} statement_break;
struct {
spl_ast_node_ref_t label;
} statement_continue;
struct {
spl_ast_node_ref_t body;
} statement_defer;
struct {
spl_ast_node_ref_t decl;
} statement_decl;
struct {
spl_ast_node_ref_t expr;
} statement_expr;
struct {
spl_ast_node_ref_vec_t attr_list;
const char *name;
spl_ast_node_ref_t type_expr;
spl_ast_node_ref_t init_expr;
} aggregate_member;
struct {
const char *name;
spl_ast_node_ref_t expr;
} struct_init_item;
struct {
spl_ast_node_ref_t pattern;
spl_ast_node_ref_t body;
} match_arm;
struct {
const char *name;
} label;
spl_mpa_int_t int_lit;
spl_mpa_float_t float_lit;
struct {
char val;
} char_lit;
struct {
const char *data;
usize len;
} string_lit;
struct {
bool val;
} bool_lit;
struct {
const char *name;
} ident;
struct {
spl_unary_op_t op;
spl_ast_node_ref_t operand;
} unary;
struct {
spl_binary_op_t op;
spl_ast_node_ref_t left;
spl_ast_node_ref_t right;
} binary;
struct {
spl_ast_node_ref_t callee;
spl_ast_node_ref_vec_t args;
} call;
struct {
spl_ast_node_ref_t obj;
const char *field;
} field;
struct {
spl_ast_node_ref_t obj;
spl_ast_node_ref_t index;
} index;
struct {
spl_ast_node_ref_t obj;
spl_ast_node_ref_t begin;
spl_ast_node_ref_t end;
} slice;
struct {
spl_ast_node_ref_t expr;
spl_ast_node_ref_t type;
} cast;
struct {
spl_ast_node_ref_vec_t elems;
} tuple;
struct {
bool has_len;
usize len;
bool is_underscore_len;
spl_ast_node_ref_t elem_type;
spl_ast_node_ref_vec_t elems;
} array_literal;
struct {
const char *name;
spl_ast_node_ref_vec_t fields;
} aggregate_literal;
struct {
const char *name;
spl_ast_node_ref_vec_t args;
} compile_call;
struct {
spl_ast_node_ref_vec_t stmts;
spl_ast_node_ref_t tail_expr;
} block;
struct {
spl_ast_node_ref_t cond;
spl_ast_node_ref_t then_block;
spl_ast_node_ref_t else_branch;
} if_expr;
struct {
spl_ast_node_ref_t var_decl;
spl_ast_node_ref_t then_block;
spl_ast_node_ref_t else_branch;
} ifvar_expr;
struct {
spl_ast_node_ref_t label;
spl_ast_node_ref_t cond;
spl_ast_node_ref_t body;
spl_ast_node_ref_t else_branch;
} while_expr;
struct {
spl_ast_node_ref_t label;
spl_ast_node_ref_t body;
} loop_expr;
struct {
spl_ast_node_ref_t label;
spl_ast_node_ref_vec_t iter_exprs;
spl_ast_node_ref_t pattern;
spl_ast_node_ref_t body;
spl_ast_node_ref_t else_branch;
} for_expr;
struct {
spl_ast_node_ref_t matched_expr;
spl_ast_node_ref_vec_t arms;
} match_expr;
struct {
spl_ast_node_ref_vec_t params;
spl_ast_node_ref_t ret_type;
} fn_type;
struct {
const char *name;
spl_ast_node_ref_vec_t generic_args;
} type_path;
struct {
spl_ast_node_ref_t pointee;
} type_pointer;
struct {
bool has_len;
isize len;
bool is_underscore_len;
spl_ast_node_ref_t elem_type;
} type_array;
struct {
spl_ast_node_ref_t elem_type;
} type_slice;
struct {
spl_ast_node_ref_vec_t params;
spl_ast_node_ref_t ret_type;
} type_fn;
struct {
spl_ast_node_ref_vec_t types;
} type_paren;
struct {
const char *kind_name;
spl_ast_node_ref_vec_t members;
} type_aggregate;
struct {
const char *name;
spl_ast_node_ref_vec_t args;
} type_compile_call;
struct {
const char *name;
} pattern_ident;
struct {
const char *name;
spl_ast_node_ref_t sub_pattern;
} pattern_dot;
struct {
spl_ast_node_ref_vec_t patterns;
} pattern_tuple;
struct {
spl_ast_node_ref_vec_t patterns;
} pattern_list;
struct {
spl_ast_node_ref_vec_t fields;
} pattern_aggregate;
struct {
const char *name;
spl_ast_node_ref_t pattern;
} pattern_field;
struct {
spl_ast_node_ref_t pat;
} pattern_ref;
};
};
typedef VEC(spl_ast_node_t) spl_ast_node_vec_t;
typedef VEC(char *) spl_ast_cstr_ref_vec_t;
typedef struct spl_ast {
spl_tok_vec_t input;
spl_ast_node_vec_t node_buckets;
spl_ast_cstr_ref_vec_t str_buckets;
spl_ast_node_ref_t root;
} spl_ast_t;
typedef VEC(spl_ast_t *) spl_ast_ptr_vec_t;
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_dump(spl_ast_t *ast, spl_ast_node_ref_t node);
const char *spl_ast_kind_name(spl_ast_node_kind_t kind);
const char *spl_binary_op_name(spl_binary_op_t op);
const char *spl_unary_op_name(spl_unary_op_t op);
#endif /* __SPL_AST_H__ */
+42
View File
@@ -0,0 +1,42 @@
#ifndef __SPL_AST2IR_H__
#define __SPL_AST2IR_H__
#include "spl_diag.h"
#include "spl_ir.h"
#include "spl_sema.h"
typedef MAP(spl_def_id_t, spl_ir_node_ref_t) spl_def2ir_map_t;
typedef struct {
usize cont_blk;
usize defer_base;
const char *label;
spl_ir_node_ref_vec_t breaks;
spl_ir_node_ref_t result_slot;
spl_type_id_t result_tid;
} spl_loop_ctx_t;
typedef VEC(spl_loop_ctx_t) spl_loop_ctx_vec_t;
typedef VEC(spl_ast_node_ref_t) spl_defer_stack_t;
typedef struct {
const spl_sema_t *sema;
spl_ir_builder_t *ir;
spl_ast_t *ast;
spl_layout_t *layout; /* 借用 splc0 的 layout */
spl_def2ir_map_t def2ir_map;
spl_loop_ctx_vec_t loop_stack;
spl_defer_stack_t defer_stack;
spl_lang_type_id_t cur_ret_lang;
usize cur_defer_base;
spl_diag_t *diag;
} spl_ast2ir_t;
void spl_ast2ir_init(spl_ast2ir_t *ast2ir, spl_ir_builder_t *ir, spl_layout_t *layout,
const spl_sema_t *sema);
void spl_ast2ir_drop(spl_ast2ir_t *ast2ir);
void spl_ast2ir_run(spl_ast2ir_t *ast2ir, spl_ast_ptr_vec_t ast_vec);
#endif /* __SPL_AST2IR_H__ */
+85
View File
@@ -0,0 +1,85 @@
#ifndef __SPL_ATTR_H__
#define __SPL_ATTR_H__
#include "stage0/include/utils.h"
#include "spl_mpa.h"
typedef enum {
SPL_ATTR_STAGE_SEMA,
SPL_ATTR_STAGE_LAYOUT,
SPL_ATTR_STAGE_IR,
SPL_ATTR_STAGE_OPT,
SPL_ATTR_STAGE_BACKEND,
} spl_attr_stage_t;
typedef enum {
SPL_ATTR_TARGET_DECL,
SPL_ATTR_TARGET_TYPE,
SPL_ATTR_TARGET_EXPR,
SPL_ATTR_TARGET_STMT,
} spl_attr_target_t;
typedef enum {
SPL_ATTR_OPTIONAL,
SPL_ATTR_REQUIRED,
} spl_attr_demand_t;
typedef struct {
const char *ns;
const char *name;
spl_attr_stage_t stage;
spl_attr_target_t target;
spl_attr_demand_t demand;
int min_args;
int max_args;
} spl_attr_info_t;
typedef enum {
SPL_ATTR_ARG_INT,
SPL_ATTR_ARG_STR,
SPL_ATTR_ARG_DEF,
SPL_ATTR_ARG_TYPE,
} spl_attr_arg_kind_t;
typedef struct {
spl_attr_arg_kind_t kind;
union {
isize i;
const char *s;
usize d;
usize t;
} value;
} spl_attr_arg_t;
typedef VEC(spl_attr_arg_t) spl_attr_arg_vec_t;
typedef usize spl_attr_id_t;
typedef struct {
spl_attr_info_t *info;
spl_attr_arg_vec_t args;
uint32_t consumed_mask;
} spl_attr_node_t;
typedef VEC(spl_attr_node_t) spl_attr_node_vec_t;
typedef VEC(spl_attr_id_t) spl_attr_id_vec_t;
typedef VEC(spl_attr_info_t *) spl_attr_registry_t;
typedef struct {
spl_attr_node_vec_t table;
spl_attr_registry_t registry;
} spl_attr_t;
void spl_attr_init(spl_attr_t *ctx);
void spl_attr_drop(spl_attr_t *ctx);
spl_attr_id_t spl_attr_alloc(spl_attr_t *ctx, spl_attr_info_t *info, spl_attr_arg_vec_t args);
spl_attr_node_t *spl_attr_node(const spl_attr_t *ctx, spl_attr_id_t id);
void spl_attr_registry_add(spl_attr_t *ctx, spl_attr_info_t *info);
spl_attr_info_t *spl_attr_lookup(spl_attr_t *ctx, const char *ns, const char *name);
void spl_attr_register_builtins(spl_attr_t *ctx);
void spl_attr_dump(const spl_attr_t *ctx);
void spl_attr_dump_id(const spl_attr_t *ctx, spl_attr_id_t id);
#endif /* __SPL_ATTR_H__ */
+22
View File
@@ -0,0 +1,22 @@
#ifndef __SPL_DBG_H__
#define __SPL_DBG_H__
typedef struct {
const char *fname;
int line;
int col;
} spl_dbg_node_t;
#define SPL_FATAL(dbg, fmt, ...) \
LOG_FATAL("fatal at %s:%d:%d " fmt, ((dbg) && (dbg)->fname ? (dbg)->fname : "<null>"), \
((dbg) ? (dbg)->line : 0), ((dbg) ? (dbg)->col : 0), ##__VA_ARGS__)
#define SPL_ERROR(dbg, fmt, ...) \
LOG_ERROR("error at %s:%d:%d " fmt, ((dbg) && (dbg)->fname ? (dbg)->fname : "<null>"), \
((dbg) ? (dbg)->line : 0), ((dbg) ? (dbg)->col : 0), ##__VA_ARGS__)
#define SPL_WARN(dbg, fmt, ...) \
LOG_WARN("warn at %s:%d:%d " fmt, ((dbg) && (dbg)->fname ? (dbg)->fname : "<null>"), \
((dbg) ? (dbg)->line : 0), ((dbg) ? (dbg)->col : 0), ##__VA_ARGS__)
#endif /* __SPL_DBG_H__ */
+144
View File
@@ -0,0 +1,144 @@
#ifndef __SPL_DIAG_H__
#define __SPL_DIAG_H__
#include "stage0/include/utils.h"
#include "spl_dbg.h"
typedef enum {
SPL_DIAG_LEVEL_NOTE = 0,
SPL_DIAG_LEVEL_WARN,
SPL_DIAG_LEVEL_ERROR,
SPL_DIAG_LEVEL_FATAL,
} spl_diag_level_t;
typedef enum {
SPL_STAGE_NONE = 0,
SPL_STAGE_LEX,
SPL_STAGE_PARSE,
SPL_STAGE_SEMA,
SPL_STAGE_AST2IR,
SPL_STAGE_IR2VM,
SPL_STAGE_DRIVER,
} spl_diag_stage_t;
/* kind 枚举:边写边补。新增时放对应分组末尾即可。 */
typedef enum {
SPL_DIAG_KIND_NONE = 0,
/* 词法 / 语法 */
SPL_DIAG_KIND_LEX_UNEXPECTED,
SPL_DIAG_KIND_PARSE_EXPECTED,
SPL_DIAG_KIND_PARSE_UNRECOGNIZED,
/* sema */
SPL_DIAG_KIND_UNDEFINED_SYMBOL,
SPL_DIAG_KIND_DUPLICATE_SYMBOL,
SPL_DIAG_KIND_TYPE_MISMATCH,
SPL_DIAG_KIND_ARG_COUNT_MISMATCH,
SPL_DIAG_KIND_NOT_A_FUNCTION,
SPL_DIAG_KIND_NO_FIELD,
SPL_DIAG_KIND_NO_METHOD,
SPL_DIAG_KIND_INDEX_BASE_INVALID,
SPL_DIAG_KIND_TUPLE_INDEX_OOB,
SPL_DIAG_KIND_FITS_OVERFLOW,
SPL_DIAG_KIND_CIRCULAR_DEP,
SPL_DIAG_KIND_BREAK_OUTSIDE_LOOP,
SPL_DIAG_KIND_CONTINUE_OUTSIDE_LOOP,
SPL_DIAG_KIND_UNDEFINED_LABEL,
SPL_DIAG_KIND_CAST_INVALID,
SPL_DIAG_KIND_UNKNOWN_ATTR,
SPL_DIAG_KIND_UNKNOWN_INTRIN,
SPL_DIAG_KIND_INTRIN_ARG_COUNT,
SPL_DIAG_KIND_VAR_UNINITIALIZED,
SPL_DIAG_KIND_CANNOT_INFER,
SPL_DIAG_KIND_MUST_RETURN_VALUE,
/* ast2ir */
SPL_DIAG_KIND_UNHANDLED_NODE,
SPL_DIAG_KIND_IMPLICIT_CAST_FAIL,
SPL_DIAG_KIND_NO_LVALUE,
SPL_DIAG_KIND_BAD_FIELD_INDEX,
SPL_DIAG_KIND_BAD_BLOCK,
/* ir2vm */
SPL_DIAG_KIND_IR_UNKNOWN_KIND,
SPL_DIAG_KIND_FRAME_OVERFLOW,
/* 通用 */
SPL_DIAG_KIND_INTERNAL,
} spl_diag_kind_t;
typedef struct {
spl_dbg_node_t loc;
char *fn_name;
char *label;
} spl_diag_frame_t;
typedef VEC(spl_diag_frame_t) spl_diag_frame_vec_t;
typedef struct {
spl_dbg_node_t loc;
char *message;
} spl_diag_note_t;
typedef VEC(spl_diag_note_t) spl_diag_note_vec_t;
typedef struct {
spl_diag_level_t level;
spl_diag_stage_t stage;
spl_diag_kind_t kind;
spl_dbg_node_t loc;
char *message;
spl_diag_frame_vec_t ctx;
spl_diag_note_vec_t notes;
} spl_diag_record_t;
typedef VEC(spl_diag_record_t) spl_diag_record_vec_t;
typedef struct spl_diag {
spl_diag_frame_vec_t ctx;
spl_diag_record_vec_t records;
usize num_printed;
bool collect;
usize n_errors;
usize n_warns;
usize n_fatals;
spl_diag_stage_t stage;
void *fout;
bool color;
} spl_diag_t;
void spl_diag_init(spl_diag_t *d, void *fout);
void spl_diag_drop(spl_diag_t *d);
void spl_diag_reset(spl_diag_t *d);
void spl_diag_set_stage(spl_diag_t *d, spl_diag_stage_t stage);
void spl_diag_push(spl_diag_t *d, const spl_dbg_node_t *loc, const char *fn_name,
const char *label);
void spl_diag_pop(spl_diag_t *d);
usize spl_diag_depth(const spl_diag_t *d);
/* 每个级别生成独立 record;error/fatal 计入 n_errors,warn 计入 n_warns;
非 NULL 时立刻输出到 out。note/warn 不阻塞,error/fatal 要求调用者停止。 */
void spl_diag_note(spl_diag_t *d, spl_diag_kind_t kind, const spl_dbg_node_t *loc, const char *fmt,
...);
void spl_diag_warn(spl_diag_t *d, spl_diag_kind_t kind, const spl_dbg_node_t *loc, const char *fmt,
...);
void spl_diag_error(spl_diag_t *d, spl_diag_kind_t kind, const spl_dbg_node_t *loc, const char *fmt,
...);
void spl_diag_fatal(spl_diag_t *d, spl_diag_kind_t kind, const spl_dbg_node_t *loc, const char *fmt,
...);
/* 挂到最近一条记录(records 末尾)的 notes 下,用于辅助说明前一条诊断。
不生成独立记录、不计入计数;records 为空时静默丢弃。 */
void spl_diag_attach_note(spl_diag_t *d, const spl_dbg_node_t *loc, const char *fmt, ...);
bool spl_diag_has_error(const spl_diag_t *d);
bool spl_diag_has_fatal(const spl_diag_t *d);
usize spl_diag_error_count(const spl_diag_t *d);
usize spl_diag_warn_count(const spl_diag_t *d);
void spl_diag_summary(const spl_diag_t *d);
const char *spl_diag_kind_name(spl_diag_kind_t k);
const char *spl_diag_stage_name(spl_diag_stage_t s);
const char *spl_diag_level_name(spl_diag_level_t l);
#endif /* __SPL_DIAG_H__ */
+36
View File
@@ -0,0 +1,36 @@
#ifndef __SPL_FOLD_H__
#define __SPL_FOLD_H__
#include "stage0/include/utils.h"
/* 通用 intrusive 哈希集合,对应 LLVM FoldingSet 的桶链部分。
- 节点表由调用方持有,节点自带 next 字段;
- hash 只用于选择桶(本实现以完整 hash 值作桶 map 的 key),
相等始终由 eq 回调做结构比较;
- 只增不删,适合类型 intern。 */
typedef usize spl_fold_id_t;
/* 相等返回非零;probe 指向调用方的临时(尚未入表)节点。 */
typedef int (*spl_fold_eq_fn)(void *ctx, spl_fold_id_t node, const void *probe);
typedef spl_fold_id_t (*spl_fold_next_fn)(void *ctx, spl_fold_id_t node);
typedef void (*spl_fold_set_next_fn)(void *ctx, spl_fold_id_t node, spl_fold_id_t next);
typedef struct {
MAP(usize, spl_fold_id_t) buckets;
void *ctx;
spl_fold_eq_fn eq;
spl_fold_next_fn next;
spl_fold_set_next_fn set_next;
} spl_fold_t;
void spl_fold_init(spl_fold_t *fold, void *ctx, spl_fold_eq_fn eq, spl_fold_next_fn next,
spl_fold_set_next_fn set_next);
void spl_fold_drop(spl_fold_t *fold);
/* 命中返回已存在的 id,否则返回 0。 */
spl_fold_id_t spl_fold_find(const spl_fold_t *fold, usize hash, const void *probe);
/* 把已分配 id 的节点挂到对应桶头。 */
void spl_fold_insert(spl_fold_t *fold, usize hash, spl_fold_id_t id);
#endif /* __SPL_FOLD_H__ */
+65
View File
@@ -0,0 +1,65 @@
#ifndef __SPL_INTRIN_H__
#define __SPL_INTRIN_H__
#include "stage0/include/utils.h"
#include "spl_lang_type.h"
typedef enum {
SPL_INTRIN_NONE = 0,
SPL_INTRIN_BITSIZEOF,
SPL_INTRIN_SIZEOF,
SPL_INTRIN_ALIGNOF,
SPL_INTRIN_ASSERT,
SPL_INTRIN_BREAKPOINT,
SPL_INTRIN_LEN,
SPL_INTRIN_INT_CAST,
SPL_INTRIN_TRUNCATE,
SPL_INTRIN_BIT_CAST,
SPL_INTRIN_FLOAT_CAST,
SPL_INTRIN_FLOAT2INT,
SPL_INTRIN_INT2FLOAT,
SPL_INTRIN_BOOL2INT,
SPL_INTRIN_PTR2INT,
SPL_INTRIN_INT2PTR,
SPL_INTRIN_PTR_CAST,
SPL_INTRIN_ENUM2INT,
SPL_INTRIN_OFFSETOF,
SPL_INTRIN_FIELD_COUNT,
/* 调用点位置信息(供日志/性能分析库使用) */
SPL_INTRIN_FILE_NAME,
SPL_INTRIN_LINE,
SPL_INTRIN_COL,
SPL_INTRIN_FN_NAME,
} spl_intrin_t;
typedef enum {
SPL_INTRIN_FORM_CALL = 1, /* @sizeof(...) @assert(...) */
SPL_INTRIN_FORM_FIELD = 2, /* recv.len */
} spl_intrin_form_t;
typedef enum {
SPL_INTRIN_RESULT_UNIT,
SPL_INTRIN_RESULT_BOOL,
SPL_INTRIN_RESULT_USIZE,
} spl_intrin_result_t;
typedef struct {
const char *name;
spl_intrin_t kind;
spl_intrin_form_t form;
int min_args; /* FIELD 形态忽略 */
int max_args; /* -1 = variadic */
spl_intrin_result_t result;
} spl_intrin_info_t;
spl_intrin_t spl_intrin_lookup_call(const char *name);
spl_intrin_t spl_intrin_lookup_field(spl_lang_type_kind_t recv_kind, const char *field);
spl_intrin_t spl_intrin_lookup_cast(spl_type_t *type, spl_type_id_t ft, spl_type_id_t tt);
const spl_intrin_info_t *spl_intrin_info(spl_intrin_t kind);
/* ---- 与 IR 侧共享的类型布局约定 ---- */
#define SPL_SLICE_PTR_FIELD_IDX 0
#define SPL_SLICE_LEN_FIELD_IDX 1
#endif /* __SPL_INTRIN_H__ */
+428
View File
@@ -0,0 +1,428 @@
#ifndef __SPL_IR_H__
#define __SPL_IR_H__
#include "stage0/include/utils.h"
#include "spl_attr.h"
#include "spl_dbg.h"
#include "spl_type.h"
/* clang-format off */
#define SPL_IR_FN_TABLE \
X(arith.add, V0, SPL_IR_ARITH_ADD) \
X(arith.sub, V0, SPL_IR_ARITH_SUB) \
X(arith.mul, V0, SPL_IR_ARITH_MUL) \
X(arith.div, V0, SPL_IR_ARITH_DIV) \
X(arith.rem, V0, SPL_IR_ARITH_REM) \
X(arith.neg, V0, SPL_IR_ARITH_NEG) \
X(arith.abs, V0, SPL_IR_ARITH_ABS) \
X(arith.and, V0, SPL_IR_ARITH_AND) \
X(arith.or, V0, SPL_IR_ARITH_OR) \
X(arith.xor, V0, SPL_IR_ARITH_XOR) \
X(arith.shl, V0, SPL_IR_ARITH_SHL) \
X(arith.shr, V0, SPL_IR_ARITH_SHR) \
X(arith.not, V0, SPL_IR_ARITH_NOT) \
X(cmp.eq, V0, SPL_IR_CMP_EQ) \
X(cmp.ne, V0, SPL_IR_CMP_NE) \
X(cmp.lt, V0, SPL_IR_CMP_LT) \
X(cmp.le, V0, SPL_IR_CMP_LE) \
X(cmp.gt, V0, SPL_IR_CMP_GT) \
X(cmp.ge, V0, SPL_IR_CMP_GE) \
X(cast.trunc, V0, SPL_IR_CAST_TRUNC) \
X(cast.zext, V0, SPL_IR_CAST_ZEXT) \
X(cast.sext, V0, SPL_IR_CAST_SEXT) \
X(cast.fext, V0, SPL_IR_CAST_FEXT) \
X(cast.ftrunc, V0, SPL_IR_CAST_FTRUNC) \
X(cast.bitcast, V0, SPL_IR_CAST_BITCAST) \
X(cast.ptr2int, V0, SPL_IR_CAST_PTR2INT) \
X(cast.int2ptr, V0, SPL_IR_CAST_INT2PTR) \
X(cast.bool2int, V0, SPL_IR_CAST_BOOL2INT) \
X(case.int2float, V0, SPL_IR_CASE_INT2FLOAT) \
X(case.float2int, V0, SPL_IR_CASE_FLOAT2INT) \
X(mem.alloca, V0, SPL_IR_MEM_ALLOCA) \
X(mem.global_alloc, V0, SPL_IR_MEM_GLOBAL_ALLOC) \
X(mem.load, V0, SPL_IR_MEM_LOAD) \
X(mem.store, V0, SPL_IR_MEM_STORE) \
X(mem.offset, V0, SPL_IR_MEM_OFFSET) \
X(mem.field, V0, SPL_IR_MEM_FIELD_PTR) \
X(mem.copy, V0, SPL_IR_MEM_COPY) \
X(mem.set, V0, SPL_IR_MEM_SET) \
X(type.const, V0, SPL_IR_TYPE_CONST) \
X(type.bitsizeof, V0, SPL_IR_TYPE_BITSIZEOF) \
X(type.sizeof, V0, SPL_IR_TYPE_SIZEOF) \
X(type.alignof, V0, SPL_IR_TYPE_ALIGNOF) \
X(type.offsetof, V0, SPL_IR_TYPE_OFFSETOF) \
X(type.field_count, V0, SPL_IR_TYPE_FIELD_COUNT) \
X(agg.construct, V0, SPL_IR_AGG_CONSTRUCT) \
X(agg.extract, V0, SPL_IR_AGG_EXTRACT) \
X(agg.insert, V0, SPL_IR_AGG_INSERT) \
X(control.select, V0, SPL_IR_CONTROL_SELECT) \
X(control.br, V0, SPL_IR_CONTROL_BR) \
X(control.jmp, V0, SPL_IR_CONTROL_JMP) \
X(control.call, V0, SPL_IR_CONTROL_CALL) \
X(control.param, V0, SPL_IR_CONTROL_PARAM) \
X(control.ret, V0, SPL_IR_CONTROL_RET) \
X(control.unreachable, V0, SPL_IR_CONTROL_UNREACHABLE) \
X(control.trap, V0, SPL_IR_CONTROL_TRAP) \
X(dbg.breakpoint, V0, SPL_IR_DBG_BREAKPOINT) \
X(dbg.panic, V0, SPL_IR_DBG_PANIC) \
typedef enum {
#ifdef X
#undef X
#endif
#define X(a, b, c) c,
SPL_IR_FN_TABLE
#undef X
} spl_ir_kind_t;
/* clang-format on */
typedef usize spl_ir_node_ref_t; /* 0 is error */
typedef VEC(spl_ir_node_ref_t) spl_ir_node_ref_vec_t;
typedef usize spl_ir_func_ref_t; /* 0 is error */
#define SPL_ENUM_TAG_FIELD_IDX 0
typedef struct {
spl_ir_kind_t kind;
spl_dbg_node_t dbg;
spl_attr_id_vec_t attrs;
union {
spl_type_id_t tid;
struct {
spl_type_id_t tid;
spl_ir_node_ref_t left;
spl_ir_node_ref_t right;
} arith;
struct {
spl_type_id_t tid;
spl_ir_node_ref_t a;
spl_ir_node_ref_t b;
} cmp;
struct {
spl_type_id_t from_tid;
spl_type_id_t to_tid;
spl_ir_node_ref_t val;
} cast;
struct {
spl_type_id_t tid;
spl_ir_node_ref_t count;
} mem_alloc;
struct {
spl_type_id_t tid;
spl_ir_node_ref_t const_node;
} mem_global_alloc;
struct {
spl_type_id_t tid;
spl_ir_node_ref_t ptr;
} mem_load;
struct {
spl_type_id_t tid;
spl_ir_node_ref_t ptr;
spl_ir_node_ref_t val;
} mem_store;
struct {
spl_type_id_t tid;
spl_ir_node_ref_t ptr;
spl_ir_node_ref_t offset;
} mem_offset;
struct {
spl_type_id_t tid;
spl_ir_node_ref_t agg;
usize field_idx;
} mem_field_ptr;
struct {
spl_type_id_t tid;
spl_ir_node_ref_t dst;
spl_ir_node_ref_t src;
spl_ir_node_ref_t size;
} mem_copy;
struct {
spl_type_id_t tid;
spl_ir_node_ref_t dst;
spl_ir_node_ref_t val;
spl_ir_node_ref_t size;
} mem_set;
struct {
spl_type_id_t tid;
union {
bool bool_lit;
void *ptr_lit;
usize int_lit;
double float_lit;
// const char *cstr_lit;
char ch_lit;
spl_ir_func_ref_t fn;
};
} type_const;
struct {
spl_type_id_t tid;
} bitsizeof;
struct {
spl_type_id_t tid;
} ir_sizeof;
struct {
spl_type_id_t tid;
} ir_alignof;
struct {
spl_type_id_t tid;
spl_ir_node_ref_t field_idx;
} ir_offsetof;
struct {
spl_type_id_t tid;
} field_count;
struct {
spl_type_id_t tid;
spl_ir_node_ref_vec_t fields;
} agg_construct;
struct {
spl_type_id_t tid;
spl_type_id_t field_tid;
usize field_idx;
spl_ir_node_ref_t val;
} agg_extract;
struct {
spl_type_id_t tid;
usize field_idx;
spl_ir_node_ref_t agg;
spl_ir_node_ref_t field;
} agg_insert;
struct {
spl_type_id_t tid;
spl_ir_node_ref_t cond;
spl_ir_node_ref_t true_val;
spl_ir_node_ref_t false_val;
} control_select;
struct {
spl_ir_node_ref_t cond;
spl_ir_node_ref_t true_label;
spl_ir_node_ref_t false_label;
} control_br;
struct {
spl_ir_node_ref_t label;
} control_jmp;
struct {
spl_type_id_t tid;
spl_ir_node_ref_t func;
spl_ir_node_ref_vec_t params;
} control_call;
struct {
spl_type_id_t tid;
spl_ir_node_ref_t idx;
} control_param;
struct {
spl_type_id_t tid;
spl_ir_node_ref_t val;
} control_ret;
struct {
const char *msg;
usize len;
} dbg_panic;
};
} spl_ir_node_t;
typedef VEC(spl_ir_node_t) spl_ir_node_vec_t;
typedef struct {
const char *name;
spl_attr_id_vec_t attrs;
spl_type_id_t fn_tid;
spl_ir_node_vec_t nodes;
} spl_ir_func_t;
typedef VEC(spl_ir_func_t) spl_ir_func_vec_t;
typedef spl_ir_node_ref_t spl_ir_block_t;
typedef VEC(spl_ir_block_t) spl_ir_block_vec_t;
typedef struct {
spl_ir_node_ref_t node;
enum { SPL_IR_FIX_BR_TRUE, SPL_IR_FIX_BR_FALSE, SPL_IR_FIX_JMP } slot;
spl_ir_node_ref_t block;
} spl_ir_fix_t;
typedef VEC(spl_ir_fix_t) spl_ir_fix_vec_t;
typedef struct {
spl_ir_func_vec_t funcs;
} spl_ir_t;
void spl_ir_init(spl_ir_t *ir);
void spl_ir_drop(spl_ir_t *ir);
spl_ir_node_ref_t spl_ir_alloc_node(spl_ir_t *ir, spl_ir_func_ref_t fn_id);
spl_ir_func_ref_t spl_ir_alloc_fn(spl_ir_t *ir);
spl_ir_node_t *spl_ir_node(spl_ir_t *ir, spl_ir_func_ref_t fn_id, spl_ir_node_ref_t node_id);
spl_ir_func_t *spl_ir_func(spl_ir_t *ir, spl_ir_func_ref_t fn_id);
/* f 是否声明为 extern(属性表内名为 "extern" 的属性);命中返回该属性节点,否则 NULL。
供各后端复用,避免 "extern" 字符串判断散落多处。 */
spl_attr_node_t *spl_ir_fn_extern_attr(const spl_attr_t *attr, const spl_ir_func_t *f);
void spl_ir_dump(spl_ir_t *ir, const spl_type_t *ty, const spl_attr_t *attr);
const char *spl_ir_kind_name(spl_ir_kind_t kind);
typedef struct {
spl_ir_t ir;
spl_dbg_node_t dbg;
spl_ir_func_ref_t current_fn; /* 当前函数 */
spl_ir_node_ref_t current_block; /* 当前发射块(0 = 未设) */
spl_ir_block_vec_t blocks; /* 当前函数的基本块表 */
spl_ir_fix_vec_t fixups; /* 跳转回填表 */
bool terminated; /* 当前块已以 br/jmp/ret 终止 */
} spl_ir_builder_t;
/* clang-format off */
#define SPL_IR_BIN_ARITH_TABLE \
X(arith_add, SPL_IR_ARITH_ADD) \
X(arith_sub, SPL_IR_ARITH_SUB) \
X(arith_mul, SPL_IR_ARITH_MUL) \
X(arith_div, SPL_IR_ARITH_DIV) \
X(arith_rem, SPL_IR_ARITH_REM) \
X(arith_and, SPL_IR_ARITH_AND) \
X(arith_or, SPL_IR_ARITH_OR) \
X(arith_xor, SPL_IR_ARITH_XOR) \
X(arith_shl, SPL_IR_ARITH_SHL) \
X(arith_shr, SPL_IR_ARITH_SHR)
#define SPL_IR_UN_ARITH_TABLE \
X(arith_neg, SPL_IR_ARITH_NEG) \
X(arith_abs, SPL_IR_ARITH_ABS) \
X(arith_not, SPL_IR_ARITH_NOT)
#define SPL_IR_CMP_TABLE \
X(cmp_eq, SPL_IR_CMP_EQ) \
X(cmp_ne, SPL_IR_CMP_NE) \
X(cmp_lt, SPL_IR_CMP_LT) \
X(cmp_le, SPL_IR_CMP_LE) \
X(cmp_gt, SPL_IR_CMP_GT) \
X(cmp_ge, SPL_IR_CMP_GE)
#define SPL_IR_CAST_TABLE \
X(cast_trunc, SPL_IR_CAST_TRUNC) \
X(cast_zext, SPL_IR_CAST_ZEXT) \
X(cast_sext, SPL_IR_CAST_SEXT) \
X(cast_fext, SPL_IR_CAST_FEXT) \
X(cast_ftrunc, SPL_IR_CAST_FTRUNC) \
X(cast_bitcast, SPL_IR_CAST_BITCAST) \
X(cast_ptr2int, SPL_IR_CAST_PTR2INT) \
X(cast_int2ptr, SPL_IR_CAST_INT2PTR) \
X(cast_bool2int, SPL_IR_CAST_BOOL2INT) \
X(case_int2float, SPL_IR_CASE_INT2FLOAT) \
X(case_float2int, SPL_IR_CASE_FLOAT2INT)
/* clang-format on */
void spl_ir_builder_init(spl_ir_builder_t *b);
void spl_ir_builder_drop(spl_ir_builder_t *b);
spl_ir_func_ref_t spl_ir_builder_fn_begin(spl_ir_builder_t *b, const char *name,
spl_type_id_t fn_tid);
spl_ir_func_ref_t spl_ir_builder_fn_reserve(spl_ir_builder_t *b, const char *name,
spl_type_id_t fn_tid);
void spl_ir_builder_fn_begin_at(spl_ir_builder_t *b, spl_ir_func_ref_t fid);
void spl_ir_builder_fn_end(spl_ir_builder_t *b);
usize spl_ir_builder_block_begin(spl_ir_builder_t *b);
void spl_ir_builder_block_end(spl_ir_builder_t *b);
#define X(name, kind) \
spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *b, spl_type_id_t tid, \
spl_ir_node_ref_t left, spl_ir_node_ref_t right);
SPL_IR_BIN_ARITH_TABLE
#undef X
#define X(name, kind) \
spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *b, spl_type_id_t tid, \
spl_ir_node_ref_t val);
SPL_IR_UN_ARITH_TABLE
#undef X
#define X(name, kind) \
spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *builder, spl_type_id_t tid, \
spl_ir_node_ref_t a, spl_ir_node_ref_t b);
SPL_IR_CMP_TABLE
#undef X
#define X(name, kind) \
spl_ir_node_ref_t spl_ir_builder_##name(spl_ir_builder_t *b, spl_type_id_t from_tid, \
spl_type_id_t to_tid, spl_ir_node_ref_t val);
SPL_IR_CAST_TABLE
#undef X
/* memory */
spl_ir_node_ref_t spl_ir_builder_mem_alloca(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t count);
spl_ir_node_ref_t spl_ir_builder_mem_global_alloc(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t const_node);
spl_ir_node_ref_t spl_ir_builder_mem_load(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t ptr);
spl_ir_node_ref_t spl_ir_builder_mem_store(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t ptr, spl_ir_node_ref_t val);
spl_ir_node_ref_t spl_ir_builder_mem_offset(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t ptr, spl_ir_node_ref_t offset);
spl_ir_node_ref_t spl_ir_builder_mem_field(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t agg, usize field_idx);
spl_ir_node_ref_t spl_ir_builder_mem_copy(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t dst, spl_ir_node_ref_t src,
spl_ir_node_ref_t size);
spl_ir_node_ref_t spl_ir_builder_mem_set(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t dst, spl_ir_node_ref_t val,
spl_ir_node_ref_t size);
/* type const */
spl_ir_node_ref_t spl_ir_builder_type_const_ptr(spl_ir_builder_t *b, spl_type_id_t tid, void *val);
spl_ir_node_ref_t spl_ir_builder_type_const_bool(spl_ir_builder_t *b, spl_type_id_t tid, bool val);
spl_ir_node_ref_t spl_ir_builder_type_const_int(spl_ir_builder_t *b, spl_type_id_t tid, usize val);
spl_ir_node_ref_t spl_ir_builder_type_const_float(spl_ir_builder_t *b, spl_type_id_t tid,
double val);
spl_ir_node_ref_t spl_ir_builder_type_const_bytes(spl_ir_builder_t *b, spl_type_id_t tid,
const void *data, usize len);
spl_ir_node_ref_t spl_ir_builder_type_const_char(spl_ir_builder_t *b, spl_type_id_t tid, char val);
spl_ir_node_ref_t spl_ir_builder_type_const_fn(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_func_ref_t val);
/* type */
spl_ir_node_ref_t spl_ir_builder_type_bitsizeof(spl_ir_builder_t *b, spl_type_id_t tid);
spl_ir_node_ref_t spl_ir_builder_type_sizeof(spl_ir_builder_t *b, spl_type_id_t tid);
spl_ir_node_ref_t spl_ir_builder_type_alignof(spl_ir_builder_t *b, spl_type_id_t tid);
spl_ir_node_ref_t spl_ir_builder_type_offsetof(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t field_idx);
spl_ir_node_ref_t spl_ir_builder_type_field_count(spl_ir_builder_t *b, spl_type_id_t tid);
/* agg */
spl_ir_node_ref_t spl_ir_builder_agg_construct(spl_ir_builder_t *b, spl_type_id_t tid);
void spl_ir_builder_agg_construct_field(spl_ir_builder_t *b, spl_ir_node_ref_t node,
spl_ir_node_ref_t field);
spl_ir_node_ref_t spl_ir_builder_agg_extract(spl_ir_builder_t *b, spl_type_id_t tid,
spl_type_id_t field_tid, usize field_idx,
spl_ir_node_ref_t val);
spl_ir_node_ref_t spl_ir_builder_agg_insert(spl_ir_builder_t *b, spl_type_id_t tid, usize field_idx,
spl_ir_node_ref_t agg, spl_ir_node_ref_t field);
/* control */
spl_ir_node_ref_t spl_ir_builder_control_select(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t cond, spl_ir_node_ref_t true_val,
spl_ir_node_ref_t false_val);
spl_ir_node_ref_t spl_ir_builder_control_br(spl_ir_builder_t *b, spl_ir_node_ref_t cond);
void spl_ir_builder_control_br_fixup(spl_ir_builder_t *b, spl_ir_node_ref_t ref, usize true_block,
usize false_block);
spl_ir_node_ref_t spl_ir_builder_control_jmp(spl_ir_builder_t *b);
void spl_ir_builder_control_jmp_fixup(spl_ir_builder_t *b, spl_ir_node_ref_t ref,
usize target_block);
spl_ir_node_ref_t spl_ir_builder_control_call(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t func);
void spl_ir_builder_control_call_param(spl_ir_builder_t *b, spl_ir_node_ref_t call_node,
spl_ir_node_ref_t arg);
spl_ir_node_ref_t spl_ir_builder_control_param(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t idx);
spl_ir_node_ref_t spl_ir_builder_control_ret(spl_ir_builder_t *b, spl_type_id_t tid,
spl_ir_node_ref_t val);
spl_ir_node_ref_t spl_ir_builder_control_unreachable(spl_ir_builder_t *b);
spl_ir_node_ref_t spl_ir_builder_control_trap(spl_ir_builder_t *b);
spl_ir_node_ref_t spl_ir_builder_dbg_breakpoint(spl_ir_builder_t *b);
spl_ir_node_ref_t spl_ir_builder_dbg_panic(spl_ir_builder_t *b, const char *msg, usize len);
#endif /* __SPL_IR_H__ */
+16
View File
@@ -0,0 +1,16 @@
#ifndef __SPL_IR2C_H__
#define __SPL_IR2C_H__
#include "spl_attr.h"
#include "spl_ir.h"
#include "spl_layout.h"
#include "spl_type.h"
/* 生成结果的字节缓冲(C 源码文本);调用方负责初始化与释放。 */
typedef VEC(char) spl_ir2c_buf_t;
/* 生成 C99 源码追加到 out;返回 false 表示遇到不支持的结构(已在输出中就地标注)。 */
bool spl_ir2c_run(const spl_ir_t *ir, const spl_type_t *type, spl_layout_t *layout,
const spl_attr_t *attr, spl_ir2c_buf_t *out);
#endif /* __SPL_IR2C_H__ */
+31
View File
@@ -0,0 +1,31 @@
#ifndef __SPL_IR2VM_H__
#define __SPL_IR2VM_H__
#include "spl_diag.h"
#include "spl_ir.h"
#include "spl_layout.h"
#include "spl_type.h"
#include "stage0/spl_mcode.h"
typedef MAP(spl_ir_func_ref_t, isize) spl_extern_idx_map_t;
typedef struct {
const spl_ir_t *ir;
const spl_type_t *type;
const spl_attr_t *attr;
spl_extern_idx_map_t extern_native;
spl_prog_t prog;
spl_layout_t *layout; /* 借用 splc0 的 layout(size/align/offset 唯一来源) */
spl_diag_t *diag;
} spl_ir2vm_t;
void spl_ir2vm_init(spl_ir2vm_t *ctx, const spl_ir_t *ir, const spl_type_t *type,
spl_layout_t *layout, const spl_attr_t *attr);
void spl_ir2vm_drop(spl_ir2vm_t *ctx);
int spl_ir2vm_run(spl_ir2vm_t *ctx);
void spl_ir2vm_dump(spl_ir2vm_t *ctx);
#endif /* __SPL_IR2VM_H__ */
+153
View File
@@ -0,0 +1,153 @@
#ifndef __SPL_LANG_TYPE_H__
#define __SPL_LANG_TYPE_H__
#include "stage0/include/utils.h"
#include "spl_dbg.h"
#include "spl_mpa.h"
#include "spl_type.h"
typedef usize spl_lang_type_id_t;
typedef VEC(spl_lang_type_id_t) spl_lang_type_id_vec_t;
typedef usize spl_def_id_t;
typedef VEC(spl_def_id_t) spl_def_id_vec_t;
typedef enum {
SPL_LANG_TYPE_ERROR,
SPL_LANG_TYPE_INFER,
SPL_LANG_TYPE_NEVER,
SPL_LANG_TYPE_ANY,
SPL_LANG_TYPE_UNIT,
SPL_LANG_TYPE_BOOL,
SPL_LANG_TYPE_INT,
SPL_LANG_TYPE_FLOAT,
SPL_LANG_TYPE_PTR,
SPL_LANG_TYPE_ARRAY,
SPL_LANG_TYPE_SLICE,
SPL_LANG_TYPE_TUPLE,
SPL_LANG_TYPE_RANGE,
SPL_LANG_TYPE_FN,
SPL_LANG_TYPE_STRUCT,
SPL_LANG_TYPE_UNION,
SPL_LANG_TYPE_ENUM,
SPL_LANG_TYPE_SHAPE,
SPL_LANG_TYPE_VARARGS,
} spl_lang_type_kind_t;
typedef struct {
const char *name;
spl_def_id_t def_id;
} spl_lang_method_t;
typedef VEC(spl_lang_method_t) spl_lang_method_vec_t;
typedef struct {
spl_lang_type_kind_t kind;
spl_type_id_t ir_type_id;
spl_lang_method_vec_t methods;
union {
spl_lang_type_id_t elem;
spl_lang_type_id_vec_t elems;
struct {
spl_lang_type_id_vec_t params;
spl_lang_type_id_t ret;
} fn;
} extra;
usize hash_next; /* 同 hash 桶内下一个类型 id;0 表示链尾。不参与相等比较。 */
} spl_lang_type_node_t;
typedef VEC(spl_lang_type_node_t) spl_lang_type_vec_t;
typedef enum {
SPL_DEF_ERROR,
SPL_DEF_SCALAR,
SPL_DEF_MEMBER,
SPL_DEF_VAR,
SPL_DEF_FN,
SPL_DEF_AGG,
SPL_DEF_DISTINCT,
SPL_DEF_ALIAS,
SPL_DEF_BUILTIN,
} spl_def_node_kind_t;
typedef enum {
SPL_DECL_UNRESOLVED,
SPL_DECL_RESOLVING,
SPL_DECL_RESOLVED,
SPL_DECL_ERROR,
} spl_decl_state_t;
struct spl_ast;
typedef struct {
spl_def_node_kind_t kind;
const char *name;
spl_dbg_node_t dbg_node;
spl_lang_type_id_t lang_id;
struct spl_ast *owner_ast;
usize ast_node_ref;
spl_decl_state_t state;
spl_def_id_t owner_def;
} spl_def_node_t;
typedef VEC(spl_def_node_t) spl_def_node_vec_t;
typedef struct {
spl_type_t ir;
spl_lang_type_vec_t lang_table;
spl_fold_t lang_fold;
spl_def_node_vec_t def_table;
} spl_lang_type_t;
void spl_lang_type_init(spl_lang_type_t *type);
void spl_lang_type_drop(spl_lang_type_t *type);
spl_lang_type_node_t *spl_lang_type_node(const spl_lang_type_t *type, spl_lang_type_id_t id);
spl_lang_type_id_t spl_lang_node_push(spl_lang_type_t *type, spl_lang_type_node_t *node);
spl_type_id_t spl_lang_type_get_ir_type(spl_lang_type_t *type, spl_lang_type_id_t lang_id);
/* 前向引用支持:reserve 一个 type_table 空槽位(kind=ERROR)作为身份锚点,
让 *Node / []Node 这类成员能拿到稳定 pointee;成员解析完后再调 define
把真实内容填进去。占位槽位**不写 type_fold**,不参与 hash-consing——
后续相同形状的类型走 spl_type_node_push 会生成独立 id,语义等价。 */
spl_type_id_t spl_lang_reserve_ir(spl_lang_type_t *type);
void spl_lang_define_ir(spl_lang_type_t *type, spl_type_id_t id, spl_type_node_t *node);
spl_lang_type_id_t spl_lang_make_error(spl_lang_type_t *type);
spl_lang_type_id_t spl_lang_make_infer(spl_lang_type_t *type);
spl_lang_type_id_t spl_lang_make_never(spl_lang_type_t *type);
spl_lang_type_id_t spl_lang_make_any(spl_lang_type_t *type);
spl_lang_type_id_t spl_lang_make_unit(spl_lang_type_t *type);
spl_lang_type_id_t spl_lang_make_bool(spl_lang_type_t *type);
spl_lang_type_id_t spl_lang_make_int(spl_lang_type_t *type, usize bits, int is_signed);
spl_lang_type_id_t spl_lang_make_float(spl_lang_type_t *type, usize bits);
spl_lang_type_id_t spl_lang_make_ptr(spl_lang_type_t *type, spl_lang_type_id_t pointee);
spl_lang_type_id_t spl_lang_make_array(spl_lang_type_t *type, spl_lang_type_id_t elem, usize len);
spl_lang_type_id_t spl_lang_make_slice(spl_lang_type_t *type, spl_lang_type_id_t elem);
spl_lang_type_id_t spl_lang_make_tuple(spl_lang_type_t *type, const spl_lang_type_id_t *elems,
usize count);
spl_lang_type_id_t spl_lang_make_range(spl_lang_type_t *type, spl_lang_type_id_t elem);
spl_lang_type_id_t spl_lang_make_fn(spl_lang_type_t *type, spl_lang_type_id_t ret,
const spl_lang_type_id_t *params, usize nparams);
spl_lang_type_id_t spl_lang_make_aggregate(spl_lang_type_t *type, spl_lang_type_kind_t kind,
const char *name, const spl_lang_type_id_t *members,
usize nmembers);
spl_lang_type_id_t spl_lang_make_varargs(spl_lang_type_t *type);
spl_lang_type_id_t spl_type_lookup_builtin(spl_lang_type_t *type, const char *name);
bool spl_lang_type_is_subtype(const spl_lang_type_t *type, spl_lang_type_id_t sub,
spl_lang_type_id_t super);
spl_lang_type_id_t spl_lang_type_join(const spl_lang_type_t *type, spl_lang_type_id_t a,
spl_lang_type_id_t b);
bool spl_lang_mpa_int_fits(const spl_lang_type_t *type, spl_lang_type_id_t target, spl_mpa_int_t v);
spl_def_id_t spl_def_alloc(spl_lang_type_t *type, spl_def_node_kind_t kind, const char *name,
spl_dbg_node_t dbg, struct spl_ast *owner_ast, usize ast_node_ref);
void spl_def_resolve(spl_lang_type_t *type, spl_def_id_t id, spl_lang_type_id_t lang_id);
spl_def_node_t *spl_def_node(const spl_lang_type_t *type, spl_def_id_t id);
spl_lang_type_id_t spl_def_get_lang_id(spl_lang_type_t *type, spl_def_id_t def_id);
spl_type_id_t spl_def_get_type_id(spl_lang_type_t *type, spl_def_id_t def_id);
void spl_lang_type_dump(spl_lang_type_t *type, spl_lang_type_id_t id);
void spl_type_def_dump(spl_lang_type_t *type, spl_def_id_t id);
#endif /* __SPL_LANG_TYPE_H__ */
+57
View File
@@ -0,0 +1,57 @@
#ifndef __SPL_LAYOUT_H__
#define __SPL_LAYOUT_H__
#include "spl_type.h"
typedef struct {
usize size;
usize abi_align;
usize pref_align;
VEC(usize) invalid_values;
} spl_layout_scalar_t;
typedef struct {
usize add_space;
usize ptr_size;
usize ptr_align;
} spl_layout_ptr_t;
typedef struct {
bool allow_field_reordering;
enum { ALIGN_MAX, ALIGN_FIXED, ALIGN_MAX_WITH_MIN } align_rule;
usize fixed_align;
} spl_layout_agg_t;
typedef struct {
usize size_bits;
usize align_bits;
VEC(usize) field_bit_offsets;
VEC(usize) field_bit_sizes;
bool resolved;
} spl_layout_info_t;
typedef struct {
enum { SPL_BIG_ENDIAN, SPL_LITTLE_ENDIAN } endian;
usize ptr_size;
usize ptr_align;
} spl_layout_target_t;
typedef struct {
VEC(spl_layout_scalar_t) scalars;
VEC(spl_layout_ptr_t) pointers;
spl_layout_agg_t aggregate;
VEC(spl_layout_info_t) infos;
spl_layout_target_t *target;
} spl_layout_t;
void spl_layout_init(spl_layout_t *layout, spl_layout_target_t *target);
void spl_layout_drop(spl_layout_t *layout);
void spl_layout_resolve(spl_layout_t *layout, spl_type_t *type);
void spl_layout_add_scalar(spl_layout_t *layout, usize size, usize abi_align);
void spl_layout_add_ptr(spl_layout_t *layout, usize addr_space, usize size, usize align);
const spl_layout_info_t *spl_layout_info_of(spl_layout_t *layout, spl_type_id_t id);
#endif /* __SPL_LAYOUT_H__ */
+13
View File
@@ -0,0 +1,13 @@
/* spl_lexer.h - 独立词法分析器 */
#ifndef __SPL_LEXER_H__
#define __SPL_LEXER_H__
#include "spl_tok.h"
/* Lexer entry point */
spl_tok_vec_t spl_lex(const char *source, const char *fname);
/* Decode escape sequence, advance *s past it. Returns 0 on success. */
int spl_decode_escape(const char **s, char *out);
#endif /* __SPL_LEXER_H__ */
+14
View File
@@ -0,0 +1,14 @@
#ifndef __SPL_MPA_H__
#define __SPL_MPA_H__
#include <stdint.h>
typedef struct {
uint64_t bits;
} spl_mpa_int_t;
typedef struct {
double bits;
} spl_mpa_float_t;
#endif /* __SPL_MPA_H__ */
+22
View File
@@ -0,0 +1,22 @@
#ifndef __SPL_PARSER_H__
#define __SPL_PARSER_H__
#include "spl_ast.h"
#include "spl_diag.h"
typedef struct spl_parser {
spl_ast_t *ast;
spl_diag_t *diag;
usize pos;
usize last_tok_line;
int failed;
int stop_agg;
usize error_count;
} spl_parser_t;
void spl_parser_init(spl_parser_t *p);
void spl_parser_drop(spl_parser_t *p);
void spl_ast_parse(spl_parser_t *p, spl_ast_t *ast);
#endif /* __SPL_PARSER_H__ */
+42
View File
@@ -0,0 +1,42 @@
#ifndef __SPL_SCOPE_H__
#define __SPL_SCOPE_H__
#include "spl_lang_type.h"
typedef usize spl_scope_id_t;
typedef enum {
SPL_SYMBOL_KIND_ERROR,
SPL_SYMBOL_KIND_VAR,
SPL_SYMBOL_KIND_MEMBER,
SPL_SYMBOL_KIND_FN,
SPL_SYMBOL_KIND_TYPE,
} spl_symbol_kind_t;
typedef struct {
const char *name;
spl_symbol_kind_t kind;
spl_def_id_t node;
} spl_symbol_t;
typedef VEC(const char *) spl_symbol_path_t;
typedef struct {
spl_scope_id_t parent;
MAP(const char *, spl_symbol_t) symbols;
} spl_scope_node_t;
typedef VEC(spl_scope_node_t) spl_scope_node_vec_t;
typedef struct {
spl_scope_node_vec_t scopes;
spl_scope_id_t root_scope;
spl_scope_id_t current_scope;
} spl_scope_t;
void spl_scope_init(spl_scope_t *scope);
void spl_scope_drop(spl_scope_t *scope);
spl_scope_id_t spl_scope_alloc(spl_scope_t *scope);
bool spl_scope_insert(spl_scope_t *scope, spl_scope_id_t id, spl_symbol_t symbol);
bool spl_scope_find(spl_scope_t *scop, const char *symbol_name, spl_symbol_t *out);
#endif /* __SPL_SCOPE_H__ */
+45
View File
@@ -0,0 +1,45 @@
#ifndef __SPL_SEMA_H__
#define __SPL_SEMA_H__
#include "spl_ast.h"
#include "spl_attr.h"
#include "spl_diag.h"
#include "spl_lang_type.h"
#include "spl_layout.h"
#include "spl_mpa.h"
#include "spl_scope.h"
#include "spl_type.h"
typedef struct spl_module_loader spl_module_loader_t;
struct spl_module_loader {
/* 从 from_fname 所在目录解析 path;返回该文件的 ast(已 init + parse)。
失败(文件不存在 / parse 错误 / 环)返回 NULL。
loader 拥有 ast 的生命周期;sema 不负责释放。 */
spl_ast_t *(*load)(void *user, const char *from_fname, const char *path, spl_diag_t *diag);
void *user;
};
typedef struct {
spl_ast_t *ast;
spl_lang_type_t *type;
spl_layout_t *layout; /* 由 splc0 拥有并解析;sema 仅借用 */
spl_scope_t *scope;
spl_symbol_t root;
spl_attr_t *attr;
spl_lang_type_id_t current_aggregate_lang;
spl_lang_type_id_t current_fn_ret_tid;
spl_lang_type_id_t current_loop_break_tid;
spl_symbol_path_t label_stack;
const spl_module_loader_t *loader;
spl_diag_t *diag;
} spl_sema_t;
void spl_sema_init(spl_sema_t *sema, spl_ast_t *ast, spl_lang_type_t *type,
spl_layout_t *layout, spl_scope_t *scope, spl_attr_t *attr);
void spl_sema_drop(spl_sema_t *sema);
void spl_sema_run_module(spl_sema_t *sema, spl_ast_node_ref_t module_ref);
void spl_sema_run(spl_sema_t *sema);
void spl_sema_dump(spl_sema_t *sema);
#endif /* __SPL_SEMA_H__ */
+130
View File
@@ -0,0 +1,130 @@
/* spl_tok.h - Token type definitions (extracted from spl_lexer.h) */
#ifndef __SPL_TOK_H__
#define __SPL_TOK_H__
#include "stage0/include/utils.h"
/* clang-format off */
#define KEYWORD_TABLE \
X(any , KW_ANY , SPL_V0) \
X(as , KW_AS , SPL_V0) \
X(bool , KW_BOOL , SPL_V0) \
X(break , KW_BREAK , SPL_V0) \
X(const , KW_CONST , SPL_V0) \
X(continue , KW_CONTINUE , SPL_V0) \
X(defer , KW_DEFER , SPL_V0) \
X(else , KW_ELSE , SPL_V0) \
X(enum , KW_ENUM , SPL_V0) \
X(false , KW_FALSE , SPL_V0) \
X(fn , KW_FN , SPL_V0) \
X(for , KW_FOR , SPL_V0) \
X(if , KW_IF , SPL_V0) \
X(import , KW_IMPORT , SPL_V0) \
X(loop , KW_LOOP , SPL_V0) \
X(macro , KW_MACRO , SPL_V0) \
X(match , KW_MATCH , SPL_V0) \
X(meta , KW_META , SPL_V0) \
X(mod , KW_MOD , SPL_V0) \
X(null , KW_NULL , SPL_V0) \
X(ret , KW_RET , SPL_V0) \
X(shape , KW_SHAPE , SPL_V0) \
X(struct , KW_STRUCT , SPL_V0) \
X(true , KW_TRUE , SPL_V0) \
X(type , KW_TYPE , SPL_V0) \
X(undefined , KW_UNDEFINED , SPL_V0) \
X(union , KW_UNION , SPL_V0) \
X(var , KW_VAR , SPL_V0) \
X(while , KW_WHILE , SPL_V0) \
X(_ , KW_UNDERSCORE , SPL_V0) \
// KEYWORD_TABLE
#define TOKEN_TABLE \
X(unknown , TOK_UNKNOWN , SPL_V0 ) \
X(EOF , TOK_EOF , SPL_V0 ) \
X(blank , TOK_BLANK , SPL_V0 ) \
X(endline , TOK_ENDLINE , SPL_V0 ) \
X("#" , TOK_SHARP , SPL_V0 ) \
X("@" , TOK_AT , SPL_V0 ) \
X("$" , TOK_DOLLAR , SPL_V0 ) \
X("==" , TOK_EQ , SPL_V0 ) \
X("=" , TOK_ASSIGN , SPL_V0 ) \
X("+=" , TOK_ASSIGN_ADD , SPL_V0 ) \
X("+" , TOK_ADD , SPL_V0 ) \
X("-=" , TOK_ASSIGN_SUB , SPL_V0 ) \
X("->" , TOK_RIGHT_ARROW , SPL_V0 ) \
X("<-" , TOK_LEFT_ARROW , SPL_V0 ) \
X("<->" , TOK_ARROW_BOTH , SPL_V0 ) \
X("<=>" , TOK_FAT_ARROW_BOTH , SPL_V0 ) \
X("=>" , TOK_RIGHT_FAT_ARROW , SPL_V0 ) \
X("-" , TOK_SUB , SPL_V0 ) \
X("*=" , TOK_ASSIGN_MUL , SPL_V0 ) \
X("*" , TOK_MUL , SPL_V0 ) \
X("/=" , TOK_ASSIGN_DIV , SPL_V0 ) \
X("/" , TOK_DIV , SPL_V0 ) \
X("//" , TOK_LINE_COMMENT , SPL_V0 ) \
X("/* */" , TOK_BLOCK_COMMENT , SPL_V0 ) \
X("%=" , TOK_ASSIGN_MOD , SPL_V0 ) \
X("%" , TOK_MOD , SPL_V0 ) \
X("&&" , TOK_AND_AND , SPL_V0 ) \
X("&=" , TOK_ASSIGN_AND , SPL_V0 ) \
X("&" , TOK_AND , SPL_V0 ) \
X("||" , TOK_OR_OR , SPL_V0 ) \
X("|=" , TOK_ASSIGN_OR , SPL_V0 ) \
X("|" , TOK_OR , SPL_V0 ) \
X("^=" , TOK_ASSIGN_XOR , SPL_V0 ) \
X("^" , TOK_XOR , SPL_V0 ) \
X("<<=" , TOK_ASSIGN_L_SH , SPL_V0 ) \
X("<<" , TOK_L_SH , SPL_V0 ) \
X("<=" , TOK_LE , SPL_V0 ) \
X("<" , TOK_LT , SPL_V0 ) \
X(">>=" , TOK_ASSIGN_R_SH , SPL_V0 ) \
X(">>" , TOK_R_SH , SPL_V0 ) \
X(">=" , TOK_GE , SPL_V0 ) \
X(">" , TOK_GT , SPL_V0 ) \
X("!" , TOK_NOT , SPL_V0 ) \
X("!=" , TOK_NEQ , SPL_V0 ) \
X("~" , TOK_BIT_NOT , SPL_V0 ) \
X("[" , TOK_L_BRACKET , SPL_V0 ) \
X("]" , TOK_R_BRACKET , SPL_V0 ) \
X("(" , TOK_L_PAREN , SPL_V0 ) \
X(")" , TOK_R_PAREN , SPL_V0 ) \
X("{" , TOK_L_BRACE , SPL_V0 ) \
X("}" , TOK_R_BRACE , SPL_V0 ) \
X(";" , TOK_SEMICOLON , SPL_V0 ) \
X("," , TOK_COMMA , SPL_V0 ) \
X(":" , TOK_COLON , SPL_V0 ) \
X("." , TOK_DOT , SPL_V0 ) \
X(".." , TOK_RANGE , SPL_V0 ) \
X("..." , TOK_ELLIPSIS , SPL_V0 ) \
X(ident , TOK_IDENT , SPL_V0 ) \
X(int , TOK_INT_LITERAL , SPL_V0 ) \
X(float , TOK_FLOAT_LITERAL , SPL_V0 ) \
X(char , TOK_CHAR_LITERAL , SPL_V0 ) \
X(string , TOK_STRING_LITERAL , SPL_V0 ) \
// TOKEN_TABLE
/* clang-format on */
typedef enum {
#define X(name, enum_name, dummy) enum_name,
KEYWORD_TABLE
#undef X
#define X(name, enum_name, dummy) enum_name,
TOKEN_TABLE
#undef X
} spl_tok_type_t;
typedef struct {
spl_tok_type_t type;
const char *lexeme;
usize len;
const char *fname;
usize offset;
usize line;
usize col;
} spl_tok_t;
typedef VEC(spl_tok_t) spl_tok_vec_t;
const char *spl_tok_type_name(spl_tok_type_t t);
#endif /* __SPL_TOK_H__ */
+129
View File
@@ -0,0 +1,129 @@
#ifndef __SPL_TYPE_H__
#define __SPL_TYPE_H__
#include "stage0/include/utils.h"
#include "spl_fold.h"
/* 类型 intern 用的哈希混合(FNV-1a 风格,顺序敏感)。
hash 只用于选择桶;相等判定始终由结构比较完成。 */
static inline usize spl_hash_mix(usize h, usize x) {
h ^= x;
h *= (usize)1099511628211ULL;
return h;
}
static inline usize spl_hash_mix_str(usize h, const char *s) {
if (!s) {
return spl_hash_mix(h, 0);
}
while (*s) {
h = spl_hash_mix(h, (usize)(unsigned char)*s);
s++;
}
return spl_hash_mix(h, 0); /* 终止符参与混合,避免名称拼接歧义 */
}
static inline usize spl_hash_seed(void) { return (usize)14695981039346656037ULL; }
typedef usize spl_type_id_t;
typedef VEC(spl_type_id_t) spl_type_id_vec_t;
typedef enum {
SPL_TYPE_ERROR,
SPL_TYPE_NEVER, // bottom type
SPL_TYPE_ANY, // top type
SPL_TYPE_UNIT,
SPL_TYPE_INFER,
SPL_TYPE_BOOL,
SPL_TYPE_INT,
SPL_TYPE_FLOAT,
SPL_TYPE_PTR,
SPL_TYPE_ARRAY,
SPL_TYPE_STRUCT,
SPL_TYPE_UNION,
SPL_TYPE_ENUM,
SPL_TYPE_FN,
SPL_TYPE_SHAPE,
} spl_type_node_kind_t;
typedef enum {
SPL_TYPE_LAYOUT_AUTO,
SPL_TYPE_LAYOUT_EXTERN_C,
SPL_TYPE_LAYOUT_PACKED,
} spl_type_layout_mode_t;
typedef struct {
spl_type_layout_mode_t mode;
usize fixed_align_bits;
} spl_type_layout_t;
typedef struct {
const char *name;
spl_type_id_t type_id;
usize ast_node_ref;
} spl_type_member_t;
typedef VEC(spl_type_member_t) spl_type_member_vec_t;
typedef struct {
spl_type_node_kind_t kind;
spl_type_layout_t layout;
union {
struct {
usize bits;
int is_signed;
} int_type;
struct {
usize bits;
} float_type;
struct {
spl_type_id_t pointee;
} ptr_type;
struct {
spl_type_id_t element;
usize len;
} array_type;
struct {
spl_type_member_vec_t variants;
spl_type_id_t tag_type;
} agg_type;
struct {
spl_type_id_vec_t params;
spl_type_id_t ret;
} fn_type;
struct {
} shape_type;
};
usize hash_next; /* 同 hash 桶内下一个类型 id;0 表示链尾。不参与相等比较。 */
} spl_type_node_t;
typedef VEC(spl_type_node_t) spl_type_node_vec_t;
typedef struct {
spl_type_node_vec_t type_table;
spl_fold_t type_fold;
} spl_type_t;
void spl_type_init(spl_type_t *type);
void spl_type_drop(spl_type_t *type);
spl_type_node_t *spl_type_node(const spl_type_t *type, spl_type_id_t id);
spl_type_id_t spl_type_node_push(spl_type_t *type, spl_type_node_t *type_node);
spl_type_id_t spl_type_builder_simple(spl_type_t *type, spl_type_node_kind_t kind);
spl_type_id_t spl_type_builder_int(spl_type_t *type, usize bits, int is_signed);
spl_type_id_t spl_type_builder_float(spl_type_t *type, usize bits);
spl_type_id_t spl_type_builder_ptr(spl_type_t *type, spl_type_id_t pointee);
spl_type_id_t spl_type_builder_array(spl_type_t *type, spl_type_id_t element, usize len);
spl_type_id_t spl_type_builder_fn(spl_type_t *type, spl_type_id_t ret, const spl_type_id_t *params,
usize nparams);
spl_type_id_t spl_type_builder_agg(spl_type_t *type, spl_type_node_kind_t tk,
spl_type_member_vec_t *variants, spl_type_id_t tag_type,
spl_type_layout_t layout);
void spl_type_pure_dump(spl_type_t *type, spl_type_id_t id);
bool spl_type_is_subtype(const spl_type_t *type, spl_type_id_t sub, spl_type_id_t super);
const spl_type_member_t *spl_type_agg_member(spl_type_t *type, spl_type_id_t tid, const char *name);
bool spl_type_agg_member_idx(spl_type_t *type, spl_type_id_t tid, const char *name, usize *out_idx);
#endif /* __SPL_TYPE_H__ */
+11
View File
@@ -0,0 +1,11 @@
#ifndef __VM_OPT_H__
#define __VM_OPT_H__
/* 字节码(VM 层)优化 pass:输入输出都是 spl_prog_t,纯函数式、可开关。 */
#include "stage0/spl_mcode.h"
void spl_prog_peephole(spl_prog_t *prog);
void spl_prog_opt(spl_prog_t *prog);
#endif /* __VM_OPT_H__ */