stage1 重构语义分析 ast2ir ir部分
This commit is contained in:
159
stage1/spl_ir.h
159
stage1/spl_ir.h
@@ -5,7 +5,6 @@
|
||||
#include "spl_dbg.h"
|
||||
#include "spl_type.h"
|
||||
|
||||
|
||||
/* clang-format off */
|
||||
#define SPL_IR_FN_TABLE \
|
||||
X(arith.add, V0, SPL_IR_ARITH_ADD) \
|
||||
@@ -46,7 +45,6 @@
|
||||
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(mem.fence, V0, SPL_IR_MEM_FENCE) \
|
||||
X(type.const, V0, SPL_IR_TYPE_CONST) \
|
||||
X(type.bitsizeof, V0, SPL_IR_TYPE_BITSIZEOF) \
|
||||
X(type.sizeof, V0, SPL_IR_TYPE_SIZEOF) \
|
||||
@@ -56,15 +54,6 @@
|
||||
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(atomic.load, V0, SPL_IR_ATOMIC_LOAD) \
|
||||
X(atomic.store, V0, SPL_IR_ATOMIC_STORE) \
|
||||
X(atomic.rmw_add, V0, SPL_IR_ATOMIC_RMW_ADD) \
|
||||
X(atomic.rmw_sub, V0, SPL_IR_ATOMIC_RMW_SUB) \
|
||||
X(atomic.rmw_and, V0, SPL_IR_ATOMIC_RMW_AND) \
|
||||
X(atomic.rmw_or, V0, SPL_IR_ATOMIC_RMW_OR) \
|
||||
X(atomic.rmw_xor, V0, SPL_IR_ATOMIC_RMW_XOR) \
|
||||
X(atomic.rmw_xchg, V0, SPL_IR_ATOMIC_RMW_XCHG) \
|
||||
X(atomic.cmpxchg, V0, SPL_IR_ATOMIC_CMPXCHG) \
|
||||
X(control.select, V0, SPL_IR_CONTROL_SELECT) \
|
||||
X(control.br, V0, SPL_IR_CONTROL_BR) \
|
||||
X(control.jmp, V0, SPL_IR_CONTROL_JMP) \
|
||||
@@ -74,8 +63,7 @@
|
||||
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.declare, V0, SPL_IR_DBG_DECLARE)
|
||||
|
||||
|
||||
typedef enum {
|
||||
#ifdef X
|
||||
#undef X
|
||||
@@ -95,6 +83,7 @@ typedef struct {
|
||||
spl_ir_kind_t kind;
|
||||
spl_dbg_node_t dbg;
|
||||
union {
|
||||
spl_type_id_t tid;
|
||||
struct {
|
||||
spl_type_id_t tid;
|
||||
spl_ir_node_ref_t left;
|
||||
@@ -149,9 +138,6 @@ typedef struct {
|
||||
spl_ir_node_ref_t val;
|
||||
spl_ir_node_ref_t size;
|
||||
} mem_set;
|
||||
struct {
|
||||
spl_ir_node_ref_t ordering;
|
||||
} mem_fence;
|
||||
struct {
|
||||
spl_type_id_t tid;
|
||||
union {
|
||||
@@ -267,4 +253,145 @@ spl_ir_func_t *spl_ir_func(spl_ir_t *ir, spl_ir_func_ref_t fn_id);
|
||||
void spl_ir_dump(spl_ir_t *ir, const spl_type_t *ty);
|
||||
const char *spl_ir_kind_name(spl_ir_kind_t kind);
|
||||
|
||||
typedef struct {
|
||||
spl_ir_t ir;
|
||||
spl_ir_func_ref_t current_fn;
|
||||
spl_ir_node_ref_t current_node;
|
||||
spl_ir_node_ref_vec_t reloc_label;
|
||||
spl_dbg_node_t dbg;
|
||||
} 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_new(spl_ir_builder_t *b, const char *name,
|
||||
spl_type_id_t fn_tid);
|
||||
spl_ir_func_ref_t spl_ir_builder_cur_fn(const spl_ir_builder_t *b);
|
||||
void spl_ir_builder_set_dbg(spl_ir_builder_t *b, spl_dbg_node_t dbg);
|
||||
|
||||
spl_ir_node_ref_t spl_ir_builder_block_new(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
|
||||
|
||||
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);
|
||||
|
||||
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_cstr(spl_ir_builder_t *b, spl_type_id_t tid,
|
||||
const char *val);
|
||||
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);
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
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,
|
||||
spl_ir_node_ref_t true_label,
|
||||
spl_ir_node_ref_t false_label);
|
||||
spl_ir_node_ref_t spl_ir_builder_control_jmp(spl_ir_builder_t *b, spl_ir_node_ref_t label);
|
||||
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 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);
|
||||
|
||||
#endif /* __SPL_IR_H__ */
|
||||
|
||||
Reference in New Issue
Block a user