stage1 完整ast 部分ir 定义
This commit is contained in:
@@ -1 +1,2 @@
|
||||
// 使用 LL(1) 解析整个AST
|
||||
#include "spl_ast.h"
|
||||
|
||||
121
stage1/spl_ast.h
121
stage1/spl_ast.h
@@ -9,6 +9,7 @@
|
||||
typedef enum {
|
||||
SPL_AST_CONTAINER_MEMBER,
|
||||
SPL_AST_FN_DECL,
|
||||
SPL_AST_FN_DEFINE,
|
||||
SPL_AST_TYPE_DECL,
|
||||
SPL_AST_VAR_DECL,
|
||||
SPL_AST_CONST_DECL,
|
||||
@@ -16,7 +17,6 @@ typedef enum {
|
||||
SPL_AST__COMPTIME_STMT, /*不实现*/
|
||||
SPL_AST__DIRECTIVE_BLOCK, /*不实现*/
|
||||
|
||||
SPL_AST_BLOCK,
|
||||
SPL_AST_BLOCK_ITEM,
|
||||
SPL_AST_EXPR,
|
||||
SPL_AST_TYPE_EXPR,
|
||||
@@ -39,56 +39,57 @@ struct spl_ast_node {
|
||||
union {
|
||||
spl_ast_node_ref_vec_t container_member;
|
||||
|
||||
spl_ast_node_ref_vec_t attr_list;
|
||||
struct {
|
||||
const char *ident;
|
||||
spl_ast_node_ref_vec_t expr_list;
|
||||
} attr_item;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_t attr_list;
|
||||
spl_ast_node_ref_vec_t attr_list; /* attr_item */
|
||||
const char *name;
|
||||
spl_ast_node_ref_t param_list;
|
||||
spl_ast_node_ref_vec_t param_list; /* param_decl */
|
||||
spl_ast_node_ref_t type_expr;
|
||||
spl_ast_node_ref_t block;
|
||||
spl_ast_node_ref_vec_t block; /* block_item */
|
||||
} fn_decl;
|
||||
spl_ast_node_ref_vec_t param_list;
|
||||
struct {
|
||||
spl_ast_node_ref_t attr_list;
|
||||
spl_ast_node_ref_vec_t attr_list; /* attr_item */
|
||||
const char *name;
|
||||
spl_ast_node_ref_t type_expr;
|
||||
} param_decl;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_t attr_list;
|
||||
spl_ast_node_ref_vec_t attr_list; /* attr_item */
|
||||
const char *name;
|
||||
enum {
|
||||
SPL_AST_TYPE_STRUCT,
|
||||
SPL_AST_TYPE_UNION,
|
||||
SPL_AST_TYPE_ENUM,
|
||||
SPL_AST_TYPE_TYPE_EXPR,
|
||||
};
|
||||
} kind;
|
||||
union {
|
||||
spl_ast_node_ref_t type_expr;
|
||||
spl_ast_node_ref_t aggregate_list;
|
||||
};
|
||||
} type_decl;
|
||||
struct {
|
||||
spl_ast_node_ref_t attr_list;
|
||||
spl_ast_node_ref_vec_t attr_list; /* attr_item */
|
||||
const char *name;
|
||||
spl_ast_node_ref_t type_expr;
|
||||
} member_decl;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_t attr_list;
|
||||
spl_ast_node_ref_vec_t attr_list; /* attr_item */
|
||||
const char *name;
|
||||
spl_ast_node_ref_t type_expr;
|
||||
spl_ast_node_ref_t expr;
|
||||
} var_decl;
|
||||
struct {
|
||||
spl_ast_node_ref_t attr_list;
|
||||
spl_ast_node_ref_vec_t attr_list; /* attr_item */
|
||||
spl_ast_node_ref_t type_expr;
|
||||
const char *name;
|
||||
spl_ast_node_ref_t expr;
|
||||
} const_decl;
|
||||
|
||||
spl_ast_node_ref_vec_t block;
|
||||
struct {
|
||||
enum {
|
||||
SPL_AST_IF_STATEMENT,
|
||||
@@ -104,30 +105,29 @@ struct spl_ast_node {
|
||||
SPL_AST_VARDECL,
|
||||
SPL_AST_TYPEDECL,
|
||||
SPL_AST_EXPR_STATEMENT,
|
||||
SPL_PACED_EXPR,
|
||||
} kind;
|
||||
union {
|
||||
struct {
|
||||
spl_ast_node_ref_t expr;
|
||||
spl_ast_node_ref_t if_block;
|
||||
spl_ast_node_ref_t else_block;
|
||||
spl_ast_node_ref_vec_t if_block; /* block_item */
|
||||
spl_ast_node_ref_vec_t else_block; /* block_item */
|
||||
} if_statement;
|
||||
struct {
|
||||
spl_ast_node_ref_t packed_expr;
|
||||
spl_ast_node_ref_t if_block;
|
||||
spl_ast_node_ref_t else_block;
|
||||
spl_ast_node_ref_vec_t if_block; /* block_item */
|
||||
spl_ast_node_ref_vec_t else_block; /* block_item */
|
||||
} ifvar_statement;
|
||||
struct {
|
||||
spl_ast_node_ref_t expr;
|
||||
spl_ast_node_ref_t while_block;
|
||||
spl_ast_node_ref_vec_t while_block; /* block_item */
|
||||
} while_statement;
|
||||
struct {
|
||||
spl_ast_node_ref_t loop_block;
|
||||
spl_ast_node_ref_vec_t loop_block; /* block_item */
|
||||
} loop_statement;
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t expr_vec;
|
||||
VEC(char *) ident_vec;
|
||||
spl_ast_node_ref_t block;
|
||||
spl_ast_node_ref_vec_t block; /* block_item */
|
||||
} for_statement;
|
||||
struct {
|
||||
spl_ast_node_ref_t expr;
|
||||
@@ -142,35 +142,60 @@ struct spl_ast_node {
|
||||
struct {
|
||||
} continue_statement;
|
||||
struct {
|
||||
spl_ast_node_ref_t block_or_statement;
|
||||
spl_ast_node_ref_vec_t block_or_statement; /* block_item/statement */
|
||||
} defer_statement;
|
||||
spl_ast_node_ref_t var_decl;
|
||||
spl_ast_node_ref_t type_decl;
|
||||
spl_ast_node_ref_t expr_statement;
|
||||
|
||||
};
|
||||
} block_item;
|
||||
struct {
|
||||
const char *ident;
|
||||
const char *bind_ident;
|
||||
spl_ast_node_ref_t expr;
|
||||
} packed_expr;
|
||||
};
|
||||
} block_item;
|
||||
|
||||
struct {
|
||||
enum {
|
||||
SPL_AST_ASSIGN_EXPR,
|
||||
SPL_AST_ASSIGN_ADD_EXPR,
|
||||
SPL_AST_ASSIGN_sUB_EXPR,
|
||||
SPL_AST_ASSIGN_MUL_EXPR,
|
||||
SPL_AST_ASSIGN_DIV_EXPR,
|
||||
SPL_AST_ASSIGN_MOD_EXPR,
|
||||
SPL_AST_ASSIGN_AND_EXPR,
|
||||
SPL_AST_ASSIGN_OR_EXPR,
|
||||
SPL_AST_ASSIGN_XOR_EXPR,
|
||||
SPL_AST_ASSIGN_LSHIFT_EXPR,
|
||||
SPL_AST_ASSIGN_USHIFT_EXPR,
|
||||
|
||||
SPL_AST_BOOLOR_EXPR,
|
||||
SPL_AST_BOOLAND_EXPR,
|
||||
SPL_AST_BITOR_EXPR,
|
||||
SPL_AST_BITXOR_EXPR,
|
||||
SPL_AST_BITAND_EXPR,
|
||||
|
||||
SPL_AST_CMPEQ_EXPR,
|
||||
SPL_AST_CMP_EXPR,
|
||||
SPL_AST_CMPNE_EXPR,
|
||||
|
||||
SPL_AST_CMP_LE_EXPR,
|
||||
SPL_AST_CMP_GE_EXPR,
|
||||
SPL_AST_CMP_LT_EXPR,
|
||||
SPL_AST_CMP_GT_EXPR,
|
||||
|
||||
SPL_AST_RANGE_EXPR,
|
||||
SPL_AST_SHIFT_EXPR,
|
||||
|
||||
SPL_AST_LSHIFT_EXPR,
|
||||
SPL_AST_RSHIFT_EXPR,
|
||||
|
||||
SPL_AST_ADD_EXPR,
|
||||
SPL_AST_SUB_EXPR,
|
||||
|
||||
SPL_AST_MUL_EXPR,
|
||||
SPL_AST_PREFIX_EXPR,
|
||||
SPL_AST_DIV_EXPR,
|
||||
SPL_AST_MOD_EXPR,
|
||||
|
||||
SPL_AST_PREFIX_EXPR, /* left ref node */
|
||||
SPL_AST_POSTFIX_EXPR, /* left ref node */
|
||||
SPL_AST_PRIMARY_EXPR, /* left ref node */
|
||||
} op;
|
||||
@@ -179,18 +204,29 @@ struct spl_ast_node {
|
||||
spl_ast_node_ref_t right;
|
||||
} op_expr;
|
||||
} expr;
|
||||
struct {
|
||||
enum {
|
||||
SPL_AST_MINUS_EXPR,
|
||||
SPL_AST_BANG_EXPR,
|
||||
SPL_AST_TILDE_EXPR,
|
||||
SPL_AST_AMPERSAND_EXPR,
|
||||
SPL_AST_ASTERISK_EXPR,
|
||||
} kind;
|
||||
spl_ast_node_ref_t postfix_expr;
|
||||
} prefix_expr;
|
||||
struct {
|
||||
enum {
|
||||
SPL_AST_CALL_EXPR,
|
||||
SPL_AST_IDENT_EXPR,
|
||||
SPL_AST_FIELD_EXPR,
|
||||
SPL_AST_DEREF_EXPR,
|
||||
SPL_AST_INDEX_EXPR,
|
||||
SPL_AST_SLICE_EXPR,
|
||||
SPL_AST_AS_EXPR,
|
||||
} kind;
|
||||
spl_ast_node_ref_t primary_expr;
|
||||
union {
|
||||
spl_ast_node_ref_vec_t call_expr;
|
||||
const char *indet_expr;
|
||||
const char *field_expr; /* field/method */
|
||||
spl_ast_node_ref_t index_expr;
|
||||
struct {
|
||||
spl_ast_node_ref_t begin;
|
||||
@@ -214,16 +250,15 @@ struct spl_ast_node {
|
||||
SPL_AST_ARRAY_LIT,
|
||||
SPL_AST_BUILTIN_EXPR,
|
||||
SPL_AST_BLOCK_EXPR,
|
||||
};
|
||||
} kind;
|
||||
union {
|
||||
isize integer_expr;
|
||||
double float_expr;
|
||||
char char_lit_expr;
|
||||
const char *string_lit_expr;
|
||||
const char *string_lit_expr; /* parsed c string */
|
||||
const char *ident;
|
||||
|
||||
/*aggregate_init_item*/
|
||||
spl_ast_node_ref_vec_t aggregate_init_expr;
|
||||
spl_ast_node_ref_vec_t aggregate_init_expr; /* aggregate_init_item */
|
||||
|
||||
spl_ast_node_ref_t expr;
|
||||
struct {
|
||||
@@ -235,7 +270,7 @@ struct spl_ast_node {
|
||||
const char *ident;
|
||||
spl_ast_node_ref_vec_t expr_list;
|
||||
} builtin_expr;
|
||||
spl_ast_node_ref_t block_expr;
|
||||
spl_ast_node_ref_vec_t block_expr; /* block_item */
|
||||
};
|
||||
} primary_expr;
|
||||
struct {
|
||||
@@ -244,10 +279,8 @@ struct spl_ast_node {
|
||||
} aggregate_init_item;
|
||||
|
||||
struct {
|
||||
/* ASTERISK = 1, [] = 2, else = 0*/
|
||||
int pointer;
|
||||
/* if array_size != 0 then pointer == 2 */
|
||||
int array_size;
|
||||
spl_ast_node_ref_vec_t type_prefixs; /* prefix_type */
|
||||
|
||||
enum {
|
||||
SPL_AST_BASE_TYPE_FN,
|
||||
SPL_AST_BASE_TYPE_PATH,
|
||||
@@ -255,13 +288,19 @@ struct spl_ast_node {
|
||||
|
||||
const char *spl_base_type;
|
||||
union {
|
||||
spl_ast_node_ref_vec_t type_path;
|
||||
spl_ast_node_ref_vec_t type_path; /* type_atom */
|
||||
struct {
|
||||
spl_ast_node_ref_t param_list;
|
||||
spl_ast_node_ref_vec_t param_list; /* param_decl */
|
||||
spl_ast_node_ref_t type_expr;
|
||||
} fn_type;
|
||||
};
|
||||
} type_expr;
|
||||
struct {
|
||||
/* ASTERISK = 1, [] = 2, else = 0*/
|
||||
int pointer;
|
||||
/* if array_size != 0 then pointer == 2 */
|
||||
int array_size;
|
||||
} prefix_type;
|
||||
struct {
|
||||
enum {
|
||||
SPL_AST_TYPE_VOID,
|
||||
|
||||
1
stage1/spl_ir.c
Normal file
1
stage1/spl_ir.c
Normal file
@@ -0,0 +1 @@
|
||||
#include "spl_ir.h"
|
||||
100
stage1/spl_ir.h
Normal file
100
stage1/spl_ir.h
Normal file
@@ -0,0 +1,100 @@
|
||||
#ifndef __SPL_IR_H__
|
||||
#define __SPL_IR_H__
|
||||
|
||||
#include "../stage0/include/utils.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.load, V0, SPL_IR_MEM_LOAD) \
|
||||
X(mem.store, V0, SPL_IR_MEM_STORE) \
|
||||
X(mem.offset, V0, SPL_IR_MEM_OFFSET) \
|
||||
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) \
|
||||
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(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) \
|
||||
X(control.call, V0, SPL_IR_CONTROL_CALL) \
|
||||
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.declare, V0, SPL_IR_DBG_DECLARE)
|
||||
|
||||
/* clang-format on */
|
||||
|
||||
typedef struct {
|
||||
enum {
|
||||
|
||||
} kind;
|
||||
} spl_ir_node_t;
|
||||
typedef VEC(spl_ir_node_t) spl_ir_node_vec_t;
|
||||
|
||||
typedef usize spl_ir_node_ref_t;
|
||||
typedef VEC(spl_ir_node_ref_t) spl_ir_node_ref_vec_t;
|
||||
typedef struct {
|
||||
const char *name;
|
||||
spl_ir_node_vec_t nodes;
|
||||
spl_ir_node_ref_vec_t labels;
|
||||
} spl_ir_func_t;
|
||||
|
||||
typedef VEC(spl_ir_func_t) spl_ir_func_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);
|
||||
|
||||
void spl_ir_dump(spl_ir_t *ir);
|
||||
|
||||
#endif /* __SPL_IR_H__ */
|
||||
@@ -1,6 +1,7 @@
|
||||
/* spl_lexer.c — SPL lexical analyzer */
|
||||
|
||||
#include "spl_lexer.h"
|
||||
#include "spl_tok.h"
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -395,6 +396,7 @@ spl_tok_vec_t spl_lex(const char *source, const char *fname) {
|
||||
|
||||
/* Two-char operators */
|
||||
TRY_OP2('=', '=', TOK_EQ, TOK_ASSIGN)
|
||||
TRY_OP2('=', '>', TOK_FAT_ARROW, TOK_ASSIGN)
|
||||
TRY_OP2('!', '=', TOK_NEQ, TOK_NOT)
|
||||
TRY_OP2('<', '=', TOK_LE, TOK_LT)
|
||||
TRY_OP2('>', '=', TOK_GE, TOK_GT)
|
||||
|
||||
Reference in New Issue
Block a user