| |
| |
| |
| |
| #ifndef P2S_AST_H |
| #define P2S_AST_H |
|
|
| #include "utils.h" |
| #include "lexer.h" |
|
|
| |
| typedef struct ASTNode ASTNode; |
| typedef struct ASTType ASTType; |
|
|
| VEC_TYPEDEF(ASTNode*, NodeVec); |
|
|
| |
| typedef enum { |
| TYPE_VOID, TYPE_CHAR, TYPE_SHORT, TYPE_INT, TYPE_LONG, |
| TYPE_FLOAT, TYPE_DOUBLE, |
| TYPE_UNSIGNED_CHAR, TYPE_UNSIGNED_SHORT, TYPE_UNSIGNED_INT, TYPE_UNSIGNED_LONG, |
| TYPE_POINTER, TYPE_ARRAY, TYPE_STRUCT, TYPE_UNION, TYPE_ENUM, TYPE_FUNC, |
| TYPE_UNKNOWN |
| } TypeKind; |
|
|
| struct ASTType { |
| TypeKind kind; |
| bool is_const; |
| bool is_volatile; |
| bool is_restrict; |
| bool is_static; |
| ASTType *base; |
| int array_size; |
| const char *name; |
| |
| ASTType **param_types; |
| int param_count; |
| ASTType *return_type; |
| }; |
|
|
| |
| typedef enum { |
| |
| NODE_PROGRAM, |
| NODE_FUNC_DECL, |
| NODE_VAR_DECL, |
| NODE_PARAM, |
| NODE_STRUCT_DECL, |
|
|
| |
| NODE_BLOCK, |
| NODE_EXPR_STMT, |
| NODE_IF, |
| NODE_WHILE, |
| NODE_DO_WHILE, |
| NODE_FOR, |
| NODE_RETURN, |
| NODE_BREAK, |
| NODE_CONTINUE, |
|
|
| |
| NODE_BINARY, |
| NODE_UNARY, |
| NODE_POSTFIX, |
| NODE_ASSIGN, |
| NODE_TERNARY, |
| NODE_CALL, |
| NODE_INDEX, |
| NODE_MEMBER, |
| NODE_ARROW, |
| NODE_CAST, |
| NODE_SIZEOF, |
| NODE_IDENT, |
| NODE_INT_LIT, |
| NODE_FLOAT_LIT, |
| NODE_STRING_LIT, |
| NODE_CHAR_LIT, |
| NODE_COMMA_EXPR, |
| NODE_ADDR_OF, |
| NODE_DEREF, |
|
|
| |
| NODE_PARALLEL_FOR, |
| NODE_PARALLEL_REDUCE, |
| NODE_PARALLEL_SCAN, |
| NODE_PARALLEL_MAP, |
| NODE_BARRIER, |
| NODE_SHARED_DECL, |
| NODE_TILE_HINT, |
| NODE_SIMD_HINT, |
| NODE_MEMORY_LAYOUT, |
| NODE_ATOMIC_ADD, |
| NODE_ATOMIC_CAS, |
|
|
| |
| NODE_PREPROC, |
|
|
| NODE_TYPE_COUNT |
| } NodeType; |
|
|
| |
| typedef enum { |
| OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_MOD, |
| OP_AND, OP_OR, OP_XOR, OP_NOT, OP_LNOT, |
| OP_LSHIFT, OP_RSHIFT, |
| OP_EQ, OP_NEQ, OP_LT, OP_GT, OP_LE, OP_GE, |
| OP_LAND, OP_LOR, |
| OP_NEG, OP_POS, |
| OP_INC, OP_DEC, |
| OP_POST_INC, OP_POST_DEC, |
| OP_ASSIGN, |
| OP_ADD_ASSIGN, OP_SUB_ASSIGN, OP_MUL_ASSIGN, OP_DIV_ASSIGN, |
| OP_MOD_ASSIGN, OP_AND_ASSIGN, OP_OR_ASSIGN, OP_XOR_ASSIGN, |
| OP_LSHIFT_ASSIGN, OP_RSHIFT_ASSIGN, |
| OP_COMMA, |
| } OpType; |
|
|
| |
| typedef enum { |
| REDUCE_ADD, REDUCE_MUL, REDUCE_MIN, REDUCE_MAX, |
| REDUCE_AND, REDUCE_OR, REDUCE_XOR |
| } ReduceOp; |
|
|
| |
| struct ASTNode { |
| NodeType type; |
| SourceLoc loc; |
| ASTType *expr_type; |
|
|
| union { |
| |
| struct { NodeVec decls; } program; |
|
|
| |
| struct { |
| const char *name; |
| ASTType *return_type; |
| NodeVec params; |
| ASTNode *body; |
| bool is_inline; |
| bool is_static; |
| } func; |
|
|
| |
| struct { |
| const char *name; |
| ASTType *var_type; |
| ASTNode *init; |
| } var; |
|
|
| |
| struct { |
| const char *name; |
| ASTType *param_type; |
| } param; |
|
|
| |
| struct { |
| const char *name; |
| NodeVec fields; |
| bool is_union; |
| } struc; |
|
|
| |
| struct { NodeVec stmts; } block; |
|
|
| |
| struct { |
| ASTNode *cond; |
| ASTNode *then_body; |
| ASTNode *else_body; |
| } if_stmt; |
|
|
| |
| struct { |
| ASTNode *cond; |
| ASTNode *body; |
| } while_stmt; |
|
|
| |
| struct { |
| ASTNode *init; |
| ASTNode *cond; |
| ASTNode *step; |
| ASTNode *body; |
| } for_stmt; |
|
|
| |
| struct { ASTNode *value; } ret; |
|
|
| |
| struct { ASTNode *expr; } expr_stmt; |
|
|
| |
| struct { |
| OpType op; |
| ASTNode *left; |
| ASTNode *right; |
| } binary; |
|
|
| |
| struct { |
| OpType op; |
| ASTNode *operand; |
| } unary; |
|
|
| |
| struct { |
| OpType op; |
| ASTNode *operand; |
| } postfix; |
|
|
| |
| struct { |
| OpType op; |
| ASTNode *target; |
| ASTNode *value; |
| } assign; |
|
|
| |
| struct { |
| ASTNode *cond; |
| ASTNode *then_expr; |
| ASTNode *else_expr; |
| } ternary; |
|
|
| |
| struct { |
| ASTNode *func; |
| NodeVec args; |
| } call; |
|
|
| |
| struct { |
| ASTNode *array; |
| ASTNode *index; |
| } index; |
|
|
| |
| struct { |
| ASTNode *object; |
| const char *field; |
| } member; |
|
|
| |
| struct { |
| ASTType *target_type; |
| ASTNode *expr; |
| } cast; |
|
|
| |
| struct { |
| ASTType *of_type; |
| ASTNode *of_expr; |
| } size_of; |
|
|
| |
| struct { const char *name; } ident; |
|
|
| |
| struct { int64_t value; } int_lit; |
|
|
| |
| struct { double value; } float_lit; |
|
|
| |
| struct { const char *value; } string_lit; |
|
|
| |
| struct { int64_t value; } char_lit; |
|
|
| |
| struct { ASTNode *operand; } unary_expr; |
|
|
| |
|
|
| |
| struct { |
| const char *iter_var; |
| ASTNode *lo; |
| ASTNode *hi; |
| ASTNode *step; |
| ASTNode *body; |
| } par_for; |
|
|
| |
| struct { |
| const char *accum_var; |
| ReduceOp op; |
| const char *iter_var; |
| ASTNode *lo; |
| ASTNode *hi; |
| ASTNode *body; |
| ASTNode *init_val; |
| } par_reduce; |
|
|
| |
| struct { |
| const char *output_var; |
| ReduceOp op; |
| const char *iter_var; |
| ASTNode *lo; |
| ASTNode *hi; |
| ASTNode *body; |
| } par_scan; |
|
|
| |
| struct { |
| const char *dst; |
| const char *src; |
| ASTNode *count; |
| const char *func_name; |
| ASTNode *body; |
| } par_map; |
|
|
| |
| struct { |
| const char *name; |
| ASTType *var_type; |
| ASTNode *size; |
| } shared; |
|
|
| |
| struct { |
| const char *target; |
| int tile_size; |
| } tile_hint; |
|
|
| |
| struct { |
| const char *target; |
| } simd_hint; |
|
|
| |
| struct { |
| const char *target; |
| bool soa; |
| } mem_layout; |
|
|
| |
| struct { |
| ASTNode *target; |
| ASTNode *value; |
| } atomic_add; |
|
|
| |
| struct { |
| ASTNode *target; |
| ASTNode *expected; |
| ASTNode *desired; |
| } atomic_cas; |
|
|
| |
| struct { const char *text; } preproc; |
| }; |
| }; |
|
|
| |
| ASTNode *ast_new(Arena *a, NodeType type, SourceLoc loc); |
| ASTType *ast_new_type(Arena *a, TypeKind kind); |
| ASTType *ast_ptr_type(Arena *a, ASTType *base); |
| ASTType *ast_array_type(Arena *a, ASTType *base, int size); |
|
|
| |
| void ast_print(ASTNode *node, int indent); |
| const char *node_type_str(NodeType t); |
| const char *op_type_str(OpType op); |
|
|
| #endif |
|
|