nctref/src/ast.h
2024-11-20 16:36:17 +02:00

292 lines
4.5 KiB
C

#ifndef NCTREF_AST_H
#define NCTREF_AST_H
#include"types.h"
#include"lexer.h"
#include"vartable.h"
// VISITORS APPEAR IN varify_change_usedefs,ast_expr_is_equal,ast_stmt_is_after,
// dumben_chunk, cg_go, loop_second_pass AND OTHERS
//
// THEY ***MUST*** BE CHANGED ACCORDINGLY IF AST IS ALTERED
#pragma pack(push, 1)
#ifdef __GNUC__
#define ENUMPAK __attribute__((packed))
#else
#define ENUMPAK
#endif
#define GEN_ENUM(x) x,
#define GEN_STRI(x) #x,
#define AST_KINDS(K) \
K(AST_CHUNK) \
K(AST_STMT_DECL) \
K(AST_TYPE_IDENTIFIER) \
K(AST_EXPR_PRIMITIVE) \
K(AST_STMT_IF) \
K(AST_EXPR_BINARY_OP) \
K(AST_EXPR_VAR) \
K(AST_EXPR_STACK_POINTER) \
K(AST_TYPE_POINTER) \
K(AST_EXPR_UNARY_OP) \
K(AST_STMT_LOOP) \
K(AST_STMT_BREAK) \
K(AST_STMT_CONTINUE) \
K(AST_EXPR_CALL) \
K(AST_STMT_EXPR) \
K(AST_STMT_ASSIGN) \
K(AST_STMT_EXT_ALIGN) \
K(AST_EXPR_STRING_LITERAL) \
K(AST_EXPR_CAST) \
K(AST_EXPR_ARRAY) \
K(AST_STMT_EXT_ORG) \
K(AST_STMT_EXT_SECTION)
typedef enum ENUMPAK { AST_KINDS(GEN_ENUM) } ASTKind;
extern const char *AST_KIND_STR[];
typedef enum ENUMPAK {
BINOP_ADD = 0,
BINOP_SUB = 1,
BINOP_BITWISE_AND = 2,
BINOP_BITWISE_OR = 3,
BINOP_BITWISE_XOR = 4,
BINOP_SIMPLES = 5,
BINOP_MUL = 5,
BINOP_DIV = 6,
BINOP_EQUAL = 7,
BINOP_NEQUAL = 8,
BINOP_WTF = 999,
} BinaryOp;
static inline int binop_is_comparison(BinaryOp op) {
return op == BINOP_EQUAL || op == BINOP_NEQUAL;
}
static inline BinaryOp binop_comp_opposite(BinaryOp op) {
if(op == BINOP_EQUAL) {
return BINOP_NEQUAL;
} else if(op == BINOP_NEQUAL) {
return BINOP_EQUAL;
}
return BINOP_WTF;
}
typedef enum ENUMPAK {
UNOP_DEREF = 0,
UNOP_NEGATE = 1,
UNOP_BITWISE_NOT = 2,
UNOP_REF = 3,
} UnaryOp;
union AST;
typedef struct {
ASTKind nodeKind;
Type *type;
} ASTExpr;
typedef struct {
ASTExpr;
int val;
} ASTExprPrimitive;
typedef struct {
ASTExpr;
union AST *operands[2];
BinaryOp operator;
} ASTExprBinaryOp;
typedef struct {
ASTExpr;
UnaryOp operator;
union AST *operand;
} ASTExprUnaryOp;
typedef struct {
ASTExpr;
VarTableEntry *thing;
} ASTExprVar;
typedef struct {
ASTExpr;
union AST *what;
union AST **args;
} ASTExprCall;
typedef struct {
ASTExpr;
size_t length;
char *data;
} ASTExprStringLiteral;
typedef struct {
ASTExpr;
} ASTExprStackPointer;
typedef struct {
ASTKind nodeKind;
size_t size;
} ASTType;
typedef struct {
ASTType;
Token identifier;
} ASTTypeIdentifier;
typedef struct {
ASTType;
union AST *child;
int levels;
} ASTTypePointer;
typedef struct {
ASTKind nodeKind;
union AST *next;
} ASTStmt;
typedef struct {
ASTStmt;
VarTableEntry *thing;
union AST *expression;
} ASTStmtDecl;
typedef struct {
ASTKind nodeKind;
/* Flattened variable array for global register allocation */
size_t varCount;
VarTableEntry **vars;
union AST *statementFirst;
union AST *statementLast;
size_t stackReservation;
} ASTChunk;
typedef struct {
ASTStmt;
union AST *expression;
union AST *then;
} ASTStmtIf;
typedef struct {
ASTStmt;
union AST *body;
} ASTStmtLoop;
typedef struct {
ASTStmt;
} ASTStmtBreak;
typedef struct {
ASTStmt;
} ASTStmtContinue;
typedef struct {
ASTStmt;
union AST *expr;
} ASTStmtExpr;
typedef struct {
ASTStmt;
union AST *what;
union AST *to;
} ASTStmtAssign;
typedef struct {
ASTStmt;
int val;
} ASTStmtExtAlign;
typedef struct {
ASTExpr;
union AST *what;
Type *to;
char reinterpretation; /* 1 = as, 0 = to */
} ASTExprCast;
typedef struct {
ASTExpr;
union AST **items;
} ASTExprArray;
typedef struct {
ASTStmt;
size_t val;
} ASTStmtExtOrg;
typedef struct {
ASTStmt;
Token name;
} ASTStmtExtSection;
typedef union AST {
ASTKind nodeKind;
ASTChunk chunk;
ASTStmt statement;
ASTStmtDecl stmtDecl;
ASTStmtIf stmtIf;
ASTStmtLoop stmtLoop;
ASTStmtBreak stmtBreak;
ASTStmtContinue stmtContinue;
ASTStmtExpr stmtExpr;
ASTStmtAssign stmtAssign;
ASTExpr expression;
ASTExprPrimitive exprPrim;
ASTExprBinaryOp exprBinOp;
ASTExprUnaryOp exprUnOp;
ASTExprVar exprVar;
ASTExprCall exprCall;
ASTStmtExtAlign stmtExtAlign;
ASTExprStringLiteral exprStrLit;
ASTExprCast exprCast;
ASTExprArray exprArray;
ASTStmtExtOrg stmtExtOrg;
ASTStmtExtSection stmtExtSection;
} AST;
#pragma pack(pop)
AST *ast_expression_optimize(AST*);
int ast_expression_equal(AST*, AST*);
int ast_stmt_is_after(const AST *chunk, const AST *s1, const AST *s2);
void ast_usedef_reset(AST *chu);
char *ast_dump(AST *tlc);
AST *ast_deep_copy(AST*);
#endif