160 lines
3.7 KiB
C
160 lines
3.7 KiB
C
// ir_core.h
|
|
#ifndef IR_CORE_H
|
|
#define IR_CORE_H
|
|
|
|
#include "../../libcore/vector.h"
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
// 错误码定义
|
|
typedef enum {
|
|
IR_EC_SUCCESS = 0, // 成功
|
|
IR_EC_MEMORY_ERROR, // 内存分配失败
|
|
IR_EC_TYPE_MISMATCH, // 类型不匹配
|
|
IR_EC_INVALID_OPERAND, // 无效操作数
|
|
IR_EC_DUPLICATE_SYMBOL, // 符号重定义
|
|
} ir_ecode_t;
|
|
|
|
typedef struct {
|
|
enum {
|
|
IR_TYPE_INT32,
|
|
IR_TYPE_PTR,
|
|
IR_TYPE_ARRAY,
|
|
IR_TYPE_FUNC,
|
|
IR_TYPE_VOID,
|
|
} tag;
|
|
union {
|
|
struct {
|
|
struct ir_type *base;
|
|
size_t len;
|
|
} arr;
|
|
struct {
|
|
struct ir_type *ret;
|
|
struct ir_type **params;
|
|
size_t param_cnt;
|
|
} func;
|
|
};
|
|
} ir_type_t;
|
|
|
|
typedef struct ir_node ir_node_t;
|
|
|
|
typedef struct ir_bblock {
|
|
const char *label;
|
|
vector_header(instrs, ir_node_t*);
|
|
// ir_arr_t used_by;
|
|
} ir_bblock_t; // basic block
|
|
|
|
typedef struct {
|
|
const char *name;
|
|
ir_type_t *type;
|
|
vector_header(params, ir_node_t*);
|
|
vector_header(bblocks, ir_bblock_t*);
|
|
} ir_func_t;
|
|
|
|
typedef struct {
|
|
vector_header(global, ir_node_t*);
|
|
vector_header(funcs, ir_func_t*);
|
|
vector_header(extern_funcs, ir_func_t*);
|
|
} ir_prog_t;
|
|
|
|
typedef enum ir_node_tag {
|
|
IR_NODE_NULL,
|
|
IR_NODE_CONST_INT,
|
|
IR_NODE_ALLOC,
|
|
IR_NODE_LOAD,
|
|
IR_NODE_STORE,
|
|
IR_NODE_GET_PTR,
|
|
IR_NODE_OP,
|
|
IR_NODE_BRANCH,
|
|
IR_NODE_JUMP,
|
|
IR_NODE_CALL,
|
|
IR_NODE_RET,
|
|
} ir_node_tag_t;
|
|
|
|
struct ir_node {
|
|
const ir_type_t* type;
|
|
const char* name;
|
|
vector_header(used_by, ir_node_t*);
|
|
ir_node_tag_t tag;
|
|
union {
|
|
struct {
|
|
int32_t val;
|
|
} const_int;
|
|
struct {
|
|
ir_node_t* target;
|
|
} load;
|
|
struct {
|
|
ir_node_t* target;
|
|
ir_node_t* value;
|
|
} store;
|
|
struct {
|
|
ir_node_t* src_addr;
|
|
ir_node_t* offset;
|
|
} get_ptr;
|
|
struct {
|
|
enum {
|
|
/// Not equal to.
|
|
IR_OP_NEQ,
|
|
/// Equal to.
|
|
IR_OP_EQ,
|
|
/// Greater than.
|
|
IR_OP_GT,
|
|
/// Less than.
|
|
IR_OP_LT,
|
|
/// Greater than or equal to.
|
|
IR_OP_GE,
|
|
/// Less than or equal to.
|
|
IR_OP_LE,
|
|
/// Addition.
|
|
IR_OP_ADD,
|
|
/// Subtraction.
|
|
IR_OP_SUB,
|
|
/// Multiplication.
|
|
IR_OP_MUL,
|
|
/// Division.
|
|
IR_OP_DIV,
|
|
/// Modulo.
|
|
IR_OP_MOD,
|
|
/// Bitwise AND.
|
|
IR_OP_AND,
|
|
/// Bitwise OR.
|
|
IR_OP_OR,
|
|
/// Bitwise XOR.
|
|
IR_OP_XOR,
|
|
/// Bitwise NOT.
|
|
IR_OP_NOT,
|
|
/// Shift left logical.
|
|
IR_OP_SHL,
|
|
/// Shift right logical.
|
|
IR_OP_SHR,
|
|
/// Shift right arithmetic.
|
|
IR_OP_SAR,
|
|
} op;
|
|
ir_node_t* lhs;
|
|
ir_node_t* rhs;
|
|
} op;
|
|
struct {
|
|
ir_node_t* cond;
|
|
ir_bblock_t* true_bblock;
|
|
ir_bblock_t* false_bblock;
|
|
} branch;
|
|
struct {
|
|
ir_bblock_t* target_bblock;
|
|
} jump;
|
|
struct {
|
|
ir_func_t* callee;
|
|
vector_header(args, ir_node_t*);
|
|
} call;
|
|
struct {
|
|
ir_node_t* ret_val;
|
|
} ret;
|
|
} data;
|
|
};
|
|
|
|
extern ir_prog_t prog;
|
|
struct ASTNode;
|
|
void gen_ir_from_ast(struct ASTNode* node);
|
|
|
|
|
|
#endif // IR_CORE_H
|