refactor(ir): 将IR节点重构为值引用并添加字符串字面量支持

- 将scc_ir_node_ref_t重命名为scc_ir_value_ref_t,并更新所有相关API
- 修改IR定义中的节点标签枚举为值标签枚举(scc_ir_node_tag_t -> scc_ir_value_tag_t)
- 更新AST到IR转换器中所有节点引用的类型声明
- 添加对内置类型(VOID, CHAR, INT等)的完整映射实现
- 实现字符串字面量的常量数组创建功能
- 更新项目名称从"Simple Modual C Compiler"为"Simple C Compiler"
- 在Doxyfile中排除external目录的文档生成
- 移除未使用的strpool字段并重命名decl到IR的映射表

BREAKING CHANGE: IR节点引用类型已更改,所有使用scc_ir_node_ref_t的地方需
替换为scc_ir_value_ref_t。
This commit is contained in:
zzy
2026-03-31 11:42:14 +08:00
parent 4ddad7b456
commit 78e7c800ba
22 changed files with 992 additions and 727 deletions

View File

@@ -8,9 +8,9 @@
typedef struct {
scc_ir_builder_t builder;
scc_hashtable_t node2ir; ///< decl to ir_ref
scc_hashtable_t decl2ir_ref; ///< decl to ir_ref
scc_hashtable_t symtab; ///< symbol to ir_ref
scc_strpool_t strpool; ///< string pool
// scc_strpool_t strpool; ///< string pool
const scc_type_abi_t *abi;
} scc_ast2ir_ctx_t;
@@ -21,8 +21,8 @@ void scc_ast2ir_ctx_drop(scc_ast2ir_ctx_t *ctx);
void scc_ast2ir_translation_unit(scc_ast2ir_ctx_t *ctx,
scc_ast_translation_unit_t *tu);
void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, scc_ast_decl_t *decl);
scc_ir_node_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
cbool is_lvalue);
scc_ir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
cbool is_lvalue);
void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, scc_ast_stmt_t *stmt);
scc_ir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
scc_ast_type_t *ast_type);

View File

@@ -13,6 +13,23 @@ scc_ir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
// 映射内置类型
scc_ir_type_init(&ir_type, SCC_IR_TYPE_i32);
// TODO: 根据具体内置类型设置
switch (ast_type->builtin.type) {
case SCC_AST_BUILTIN_TYPE_VOID:
return scc_ir_builder_type_void(&ctx->builder);
case SCC_AST_BUILTIN_TYPE_CHAR:
case SCC_AST_BUILTIN_TYPE_UNSIGNED_CHAR:
return scc_ir_builder_type_u8(&ctx->builder);
case SCC_AST_BUILTIN_TYPE_SIGNED_CHAR:
return scc_ir_builder_type_i8(&ctx->builder);
case SCC_AST_BUILTIN_TYPE_INT:
case SCC_AST_BUILTIN_TYPE_SIGNED_INT:
return scc_ir_builder_type_i32(&ctx->builder);
case SCC_AST_BUILTIN_TYPE_UNSIGNED_INT:
return scc_ir_builder_type_u32(&ctx->builder);
default:
Panic("Unsupported AST type: %d", ast_type->builtin.type);
break;
}
break;
}
@@ -81,10 +98,10 @@ scc_ir_type_ref_t scc_ast2ir_type(scc_ast2ir_ctx_t *ctx,
*
* @param ctx
* @param expr
* @return scc_ir_node_ref_t
* @return scc_ir_value_ref_t
*/
scc_ir_node_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
cbool is_lvalue) {
scc_ir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
cbool is_lvalue) {
if (ctx == null || expr == null) {
LOG_ERROR("args is null");
return 0;
@@ -94,7 +111,7 @@ scc_ir_node_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
switch (expr->base.type) {
case SCC_AST_EXPR_BINARY: {
scc_ast_expr_t tmp_expr;
scc_ir_node_ref_t lhs, rhs;
scc_ir_value_ref_t lhs, rhs;
switch (expr->binary.op) {
case SCC_AST_OP_ASSIGN: // =
rhs = scc_ast2ir_expr(ctx, expr->binary.rhs, false);
@@ -212,7 +229,7 @@ scc_ir_node_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
}
case SCC_AST_EXPR_UNARY: {
scc_ir_node_ref_t operand =
scc_ir_value_ref_t operand =
scc_ast2ir_expr(ctx, expr->unary.operand, is_lvalue);
// /* 一元操作符 */
// SCC_AST_OP_UNARY_PLUS, // + (一元)
@@ -232,7 +249,7 @@ scc_ir_node_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
scc_ir_const_int_t value;
scc_ir_type_ref_t type_ref = scc_ir_builder_type_i32(&ctx->builder);
value.int32 = 0;
scc_ir_node_ref_t zero_ref =
scc_ir_value_ref_t zero_ref =
scc_ir_builder_const_int(&ctx->builder, type_ref, value);
return scc_ir_builder_binop(&ctx->builder, SCC_IR_OP_SUB, zero_ref,
operand);
@@ -247,7 +264,7 @@ scc_ir_node_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
scc_ir_const_int_t value;
scc_ir_type_ref_t type_ref = scc_ir_builder_type_i32(&ctx->builder);
value.int32 = 0;
scc_ir_node_ref_t zero_ref =
scc_ir_value_ref_t zero_ref =
scc_ir_builder_const_int(&ctx->builder, type_ref, value);
return scc_ir_builder_binop(&ctx->builder, SCC_IR_OP_EQ, zero_ref,
operand);
@@ -270,19 +287,19 @@ scc_ir_node_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
scc_vec_foreach(expr->call.args, i) {
scc_ast_expr_t *arg_expr = scc_vec_at(expr->call.args, i);
scc_ir_node_ref_t arg_node;
scc_ir_value_ref_t arg_node;
arg_node = scc_ast2ir_expr(ctx, arg_expr, false);
scc_vec_push(args, arg_node);
}
// 创建调用节点(需要查找函数定义)
scc_ir_func_ref_t func = (scc_ir_node_ref_t)(usize)scc_hashtable_get(
scc_ir_func_ref_t func = (scc_ir_value_ref_t)(usize)scc_hashtable_get(
&ctx->symtab, expr->call.callee->identifier._target->name);
if (!func) {
LOG_ERROR("Function %s not found",
expr->call.callee->identifier._target->name);
}
scc_ir_node_ref_t node =
scc_ir_value_ref_t node =
scc_ir_builder_call(&ctx->builder, func, args.data, args.size);
scc_vec_free(args);
return node;
@@ -310,20 +327,25 @@ scc_ir_node_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
return scc_ir_builder_const_int(&ctx->builder, type_ref, value);
}
// SCC_AST_EXPR_INT_LITERAL, // 整数字面量
// SCC_AST_EXPR_FLOAT_LITERAL, // 浮点字面量
// SCC_AST_EXPR_CHAR_LITERAL, // 字符字面量
// case SCC_AST_EXPR_STRING_LITERAL: {
// return scc_ir_builder_store();
// }
// SCC_AST_EXPR_INT_LITERAL, // 整数字面量
// SCC_AST_EXPR_FLOAT_LITERAL, // 浮点字面量
// SCC_AST_EXPR_CHAR_LITERAL, // 字符字面量
case SCC_AST_EXPR_STRING_LITERAL: {
// FIXME
scc_ir_builder_const_string(&ctx->builder, expr->literal.lexme,
scc_strlen(expr->literal.lexme));
if (is_lvalue)
TODO();
break;
}
case SCC_AST_EXPR_IDENTIFIER: {
if (expr->identifier._target == null) {
LOG_ERROR("unknown identifier %s", expr->identifier.name);
}
// FIXME hack hashtable
scc_ir_node_ref_t in = (scc_ir_node_ref_t)(usize)scc_hashtable_get(
&ctx->node2ir, expr->identifier._target);
scc_ir_value_ref_t in = (scc_ir_value_ref_t)(usize)scc_hashtable_get(
&ctx->decl2ir_ref, expr->identifier._target);
Assert(in != 0);
if (is_lvalue) {
return in;
@@ -389,7 +411,7 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, scc_ast_stmt_t *stmt) {
scc_ir_bblock_ref_t merge_block =
scc_ir_builder_bblock(&ctx->builder, "if_merge");
scc_ir_node_ref_t cond_node =
scc_ir_value_ref_t cond_node =
scc_ast2ir_expr(ctx, stmt->if_stmt.cond, false);
scc_ir_builder_branch(&ctx->builder, cond_node, true_block,
false_block);
@@ -421,7 +443,7 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, scc_ast_stmt_t *stmt) {
scc_ir_builder_jump(&ctx->builder, cond_block);
scc_ir_builder_set_current_bblock(&ctx->builder, cond_block);
scc_ir_node_ref_t cond_node =
scc_ir_value_ref_t cond_node =
scc_ast2ir_expr(ctx, stmt->while_stmt.cond, false);
scc_ir_builder_branch(&ctx->builder, cond_node, body_block, exit_block);
@@ -448,7 +470,7 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, scc_ast_stmt_t *stmt) {
scc_ir_builder_jump(&ctx->builder, cond_block);
scc_ir_builder_set_current_bblock(&ctx->builder, cond_block);
scc_ir_node_ref_t cond_node =
scc_ir_value_ref_t cond_node =
scc_ast2ir_expr(ctx, stmt->while_stmt.cond, false);
scc_ir_builder_branch(&ctx->builder, cond_node, body_block, exit_block);
@@ -481,7 +503,7 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, scc_ast_stmt_t *stmt) {
scc_ir_builder_set_current_bblock(&ctx->builder, cond_block);
if (stmt->for_stmt.cond) {
scc_ir_node_ref_t cond_node =
scc_ir_value_ref_t cond_node =
scc_ast2ir_expr(ctx, stmt->for_stmt.cond, false);
scc_ir_builder_branch(&ctx->builder, cond_node, body_block,
exit_block);
@@ -507,7 +529,7 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, scc_ast_stmt_t *stmt) {
// SCC_AST_STMT_CONTINUE, // continue 语句
case SCC_AST_STMT_RETURN: {
if (stmt->return_stmt.expr) {
scc_ir_node_ref_t ret_val_node =
scc_ir_value_ref_t ret_val_node =
scc_ast2ir_expr(ctx, stmt->return_stmt.expr, false);
scc_ir_builder_ret(&ctx->builder, ret_val_node);
} else {
@@ -541,16 +563,17 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, scc_ast_decl_t *decl) {
scc_ir_type_ref_t ir_type = scc_ast2ir_type(ctx, decl->var.type);
// 创建分配节点
scc_ir_node_ref_t alloc_val_node =
scc_ir_value_ref_t alloc_val_node =
scc_ir_builder_alloca(&ctx->builder, ir_type, decl->name);
scc_hashtable_set(&ctx->node2ir, decl, (void *)(usize)alloc_val_node);
scc_hashtable_set(&ctx->decl2ir_ref, decl,
(void *)(usize)alloc_val_node);
// 如果有初始化表达式
if (!decl->var.init) {
break;
}
scc_ir_node_ref_t init_val_node =
scc_ir_value_ref_t init_val_node =
scc_ast2ir_expr(ctx, decl->var.init, false);
scc_ir_builder_store(&ctx->builder, alloc_val_node, init_val_node);
break;
@@ -581,12 +604,12 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, scc_ast_decl_t *decl) {
scc_ast_decl_t *param =
scc_vec_at(decl->func.type->function.params, i);
scc_ir_node_ref_t param_node_ref = scc_vec_at(func->params, i);
scc_ir_node_t *param_node =
scc_ir_module_get_node(ctx->builder.ctx.module, param_node_ref);
scc_ir_value_ref_t param_node_ref = scc_vec_at(func->params, i);
scc_ir_value_t *param_node = scc_ir_module_get_value(
ctx->builder.ctx.module, param_node_ref);
Assert(param_node != null);
param_node->name = param->name;
scc_hashtable_set(&ctx->node2ir, param,
scc_hashtable_set(&ctx->decl2ir_ref, param,
(void *)(usize)param_node_ref);
}
scc_ast2ir_stmt(ctx, decl->func.body);
@@ -640,13 +663,13 @@ void scc_ast2ir_ctx_init(scc_ast2ir_ctx_t *ctx, const scc_type_abi_t *abi,
Assert(ctx != null);
ctx->abi = abi;
scc_ir_builder_init(&ctx->builder, cprog);
scc_hashtable_init(&ctx->node2ir, scc_hash_node, scc_cmp_node);
scc_hashtable_init(&ctx->decl2ir_ref, scc_hash_node, scc_cmp_node);
scc_hashtable_init(&ctx->symtab, (scc_hashtable_hash_func_t)scc_strhash32,
(scc_hashtable_equal_func_t)scc_strcmp);
}
void scc_ast2ir_ctx_drop(scc_ast2ir_ctx_t *ctx) {
scc_ir_builder_drop(&ctx->builder);
scc_hashtable_drop(&ctx->node2ir);
scc_hashtable_drop(&ctx->decl2ir_ref);
scc_hashtable_drop(&ctx->symtab);
}

View File

@@ -39,16 +39,8 @@ scc_ir_func_ref_t scc_ir_builder_func(scc_ir_builder_t *builder,
scc_ir_type_ref_t scc_ir_builder_type(scc_ir_builder_t *builder,
const scc_ir_type_t *type_desc);
// TODO
static inline scc_ir_node_ref_t
scc_ir_builder_const_int(scc_ir_builder_t *builder, scc_ir_type_ref_t type,
scc_ir_const_int_t value) {
scc_ir_node_t node;
scc_ir_node_init(&node, null, SCC_IR_NODE_CONST_INT);
node.data.const_int = value;
node.type = type;
return scc_ir_module_add_node(&builder->cprog->module, &node);
}
void scc_ir_builder_add_instr(scc_ir_builder_t *builder,
scc_ir_value_ref_t instr);
#define SCC_IR_BUILDER_TYPE_FUNC(scc_type) \
[[maybe_unused]] static inline scc_ir_type_ref_t \
@@ -76,6 +68,78 @@ SCC_IR_BUILDER_TYPE_FUNC(f32)
SCC_IR_BUILDER_TYPE_FUNC(f64)
SCC_IR_BUILDER_TYPE_FUNC(f128)
// TODO
static inline scc_ir_value_ref_t
scc_ir_builder_const_int(scc_ir_builder_t *builder, scc_ir_type_ref_t type,
scc_ir_const_int_t value) {
scc_ir_value_t node;
scc_ir_node_init(&node, null, SCC_IR_VALUE_TAG_CONST_INT);
node.data.const_int = value;
node.type = type;
return scc_ir_module_add_value(&builder->cprog->module, &node);
}
static inline scc_ir_value_ref_t
scc_ir_builder_const_string(scc_ir_builder_t *builder, const char *str,
usize len) {
scc_ir_type_ref_t u8_type = scc_ir_builder_type_u8(builder);
scc_ir_type_t array_type = {
.tag = SCC_IR_TYPE_ARRAY,
.data.array.base = u8_type,
.data.array.len = len + 1, // 包含 null 结尾
};
scc_ir_type_ref_t array_type_ref =
scc_ir_ctx_get_type(&builder->ctx, &array_type);
// 2. 创建指针类型:指向 array_type
scc_ir_type_t ptr_type = {.tag = SCC_IR_TYPE_PTR,
.data.pointer.base = u8_type};
scc_ir_type_ref_t ptr_type_ref =
scc_ir_ctx_get_type(&builder->ctx, &ptr_type);
// 5. 创建聚合节点
scc_ir_value_t const_array_value = {
.tag = SCC_IR_VALUE_TAG_CONST_ARRAY,
.type = array_type_ref,
.data.const_array.base_type = u8_type,
};
char *buff = scc_malloc(len + 1);
Assert(buff);
for (usize i = 0; i < len; i++) {
buff[i] = str[i];
}
buff[len] = '\0';
scc_vec_unsafe_from_buffer(const_array_value.data.const_array.elements,
buff, len + 1);
scc_ir_value_ref_t const_array_ref =
scc_ir_module_add_value(builder->ctx.module, &const_array_value);
Assert(const_array_ref != SCC_IR_REF_NULL);
// 3. 创建全局变量节点,类型为指针,初始值指向常量数组
char *name = scc_malloc(32);
// FIXME MAYBE MEMORY LEAK
scc_ir_value_ref_t global_value_ref = scc_ir_module_add_value(
builder->ctx.module, &(scc_ir_value_t){
.name = name,
.tag = SCC_IR_VALUE_TAG_GLOBAL_ALLOC,
.type = ptr_type_ref,
.data.global_alloc.value = const_array_ref,
});
scc_snprintf(name, 32, "$G%u", global_value_ref);
scc_vec_push(builder->cprog->global_vals, global_value_ref);
// scc_hashtable_insert(builder);
scc_ir_value_ref_t pointer_to_global_value = scc_ir_module_add_value(
builder->ctx.module, &(scc_ir_value_t){
.tag = SCC_IR_VALUE_TAG_GET_PTR,
.data.get_ptr.src_addr = global_value_ref,
.data.get_ptr.index = SCC_IR_VALUE_TAG_NULL,
});
scc_ir_builder_add_instr(builder, pointer_to_global_value);
return pointer_to_global_value;
}
/**
* @brief 开始构建函数
* @param func_ref 函数引用
@@ -129,38 +193,38 @@ void scc_ir_builder_set_current_bblock(scc_ir_builder_t *builder,
* @param type 分配的类型
* @param name 变量名可为NULL
*/
scc_ir_node_ref_t scc_ir_builder_alloca(scc_ir_builder_t *builder,
scc_ir_type_ref_t type,
const char *name);
scc_ir_value_ref_t scc_ir_builder_alloca(scc_ir_builder_t *builder,
scc_ir_type_ref_t type,
const char *name);
scc_ir_node_ref_t scc_ir_builder_func_arg_ref(scc_ir_builder_t *builder,
scc_ir_type_ref_t type,
const char *name, usize arg_idx);
scc_ir_value_ref_t scc_ir_builder_func_arg_ref(scc_ir_builder_t *builder,
scc_ir_type_ref_t type,
const char *name, usize arg_idx);
/**
* @brief 创建load指令
* @param ptr 指针操作数
*/
scc_ir_node_ref_t scc_ir_builder_load(scc_ir_builder_t *builder,
scc_ir_node_ref_t ptr);
scc_ir_value_ref_t scc_ir_builder_load(scc_ir_builder_t *builder,
scc_ir_value_ref_t ptr);
/**
* @brief 创建store指令
* @param ptr 目标指针
* @param value 要存储的值
*/
scc_ir_node_ref_t scc_ir_builder_store(scc_ir_builder_t *builder,
scc_ir_node_ref_t ptr,
scc_ir_node_ref_t value);
scc_ir_value_ref_t scc_ir_builder_store(scc_ir_builder_t *builder,
scc_ir_value_ref_t ptr,
scc_ir_value_ref_t value);
/**
* @brief 创建getptr指令指针运算
* @param ptr 基础指针
* @param index 索引值
*/
scc_ir_node_ref_t scc_ir_builder_get_ptr(scc_ir_builder_t *builder,
scc_ir_node_ref_t ptr,
scc_ir_node_ref_t index);
scc_ir_value_ref_t scc_ir_builder_get_ptr(scc_ir_builder_t *builder,
scc_ir_value_ref_t ptr,
scc_ir_value_ref_t index);
/**
* @brief 创建二元运算指令
@@ -168,10 +232,10 @@ scc_ir_node_ref_t scc_ir_builder_get_ptr(scc_ir_builder_t *builder,
* @param lhs 左操作数
* @param rhs 右操作数
*/
scc_ir_node_ref_t scc_ir_builder_binop(scc_ir_builder_t *builder,
scc_ir_op_type_t op,
scc_ir_node_ref_t lhs,
scc_ir_node_ref_t rhs);
scc_ir_value_ref_t scc_ir_builder_binop(scc_ir_builder_t *builder,
scc_ir_op_type_t op,
scc_ir_value_ref_t lhs,
scc_ir_value_ref_t rhs);
/**
* @brief 创建比较指令
@@ -179,16 +243,17 @@ scc_ir_node_ref_t scc_ir_builder_binop(scc_ir_builder_t *builder,
* @param lhs 左操作数
* @param rhs 右操作数
*/
scc_ir_node_ref_t scc_ir_builder_cmp(scc_ir_builder_t *builder,
scc_ir_op_type_t op, scc_ir_node_ref_t lhs,
scc_ir_node_ref_t rhs);
scc_ir_value_ref_t scc_ir_builder_cmp(scc_ir_builder_t *builder,
scc_ir_op_type_t op,
scc_ir_value_ref_t lhs,
scc_ir_value_ref_t rhs);
/**
* @brief 创建跳转指令(无条件)
* @param target 目标基本块
*/
scc_ir_node_ref_t scc_ir_builder_jump(scc_ir_builder_t *builder,
scc_ir_bblock_ref_t target);
scc_ir_value_ref_t scc_ir_builder_jump(scc_ir_builder_t *builder,
scc_ir_bblock_ref_t target);
/**
* @brief 创建条件分支指令
@@ -196,10 +261,10 @@ scc_ir_node_ref_t scc_ir_builder_jump(scc_ir_builder_t *builder,
* @param true_target 条件为真时的目标
* @param false_target 条件为假时的目标
*/
scc_ir_node_ref_t scc_ir_builder_branch(scc_ir_builder_t *builder,
scc_ir_node_ref_t cond,
scc_ir_bblock_ref_t true_target,
scc_ir_bblock_ref_t false_target);
scc_ir_value_ref_t scc_ir_builder_branch(scc_ir_builder_t *builder,
scc_ir_value_ref_t cond,
scc_ir_bblock_ref_t true_target,
scc_ir_bblock_ref_t false_target);
/**
* @brief 创建函数调用指令
@@ -207,21 +272,21 @@ scc_ir_node_ref_t scc_ir_builder_branch(scc_ir_builder_t *builder,
* @param args 参数列表
* @param arg_count 参数数量
*/
scc_ir_node_ref_t scc_ir_builder_call(scc_ir_builder_t *builder,
scc_ir_func_ref_t callee,
const scc_ir_node_ref_t *args,
usize arg_count);
scc_ir_value_ref_t scc_ir_builder_call(scc_ir_builder_t *builder,
scc_ir_func_ref_t callee,
const scc_ir_value_ref_t *args,
usize arg_count);
/**
* @brief 创建返回指令(带返回值)
* @param value 返回值
*/
scc_ir_node_ref_t scc_ir_builder_ret(scc_ir_builder_t *builder,
scc_ir_node_ref_t value);
scc_ir_value_ref_t scc_ir_builder_ret(scc_ir_builder_t *builder,
scc_ir_value_ref_t value);
/**
* @brief 创建返回指令void返回
*/
scc_ir_node_ref_t scc_ir_builder_ret_void(scc_ir_builder_t *builder);
scc_ir_value_ref_t scc_ir_builder_ret_void(scc_ir_builder_t *builder);
#endif /* __SCC_IR_BUILDER_H__ */

View File

@@ -19,11 +19,6 @@ void scc_ir_ctx_drop(scc_ir_ctx_t *ctx);
scc_ir_type_ref_t scc_ir_ctx_get_type(scc_ir_ctx_t *ctx,
const scc_ir_type_t *type_desc);
// 获取唯一常量(例如整数常量)
// scc_ir_node_ref_t scc_ir_ctx_get_const_int(scc_ir_ctx_t *ctx,
// scc_ir_type_ref_t type, i64
// value);
// 注册函数声明,若已存在则返回已有引用
scc_ir_func_ref_t scc_ir_ctx_declare_func(scc_ir_ctx_t *ctx,
scc_ir_type_ref_t type,

View File

@@ -8,9 +8,11 @@
typedef unsigned int ir_handle_t;
typedef const char *scc_ir_label_t;
typedef struct scc_ir_node scc_ir_node_t;
typedef ir_handle_t scc_ir_node_ref_t;
typedef SCC_VEC(scc_ir_node_ref_t) scc_ir_node_ref_vec_t;
typedef SCC_VEC(u8) scc_ir_buffer_t;
typedef struct scc_ir_value scc_ir_value_t;
typedef ir_handle_t scc_ir_value_ref_t;
typedef SCC_VEC(scc_ir_value_ref_t) scc_ir_node_ref_vec_t;
typedef struct scc_ir_type scc_ir_type_t;
typedef ir_handle_t scc_ir_type_ref_t;
@@ -41,7 +43,6 @@ typedef enum scc_ir_type_tag {
SCC_IR_TYPE_f32,
SCC_IR_TYPE_f64,
SCC_IR_TYPE_f128,
SCC_IR_TYPE_PTR,
SCC_IR_TYPE_ARRAY,
SCC_IR_TYPE_FUNC,
@@ -84,26 +85,28 @@ struct scc_ir_func {
scc_ir_bblock_ref_vec_t bblocks;
};
typedef enum scc_ir_node_tag {
SCC_IR_NODE_NULL,
SCC_IR_NODE_CONST_INT,
SCC_IR_NODE_CONST_UINT,
SCC_IR_NODE_CONST_FLOAT,
SCC_IR_NODE_CONV, ///< 类型转换
SCC_IR_NODE_FUNC_ARG_REF, ///< 函数参数引用
SCC_IR_NODE_BLOCK_ARG_REF, ///< 基本块参数引用
SCC_IR_NODE_ALLOC, ///< 分配内存(stack)
SCC_IR_NODE_GLOBAL_ALLOC, ///< 全局分配(bss)
SCC_IR_NODE_LOAD, ///< 加载数据
SCC_IR_NODE_STORE, ///< 存储数据
SCC_IR_NODE_GET_PTR, ///< 获取指针
SCC_IR_NODE_GET_ELEM_PTR, ///< 获取元素指针(used by array)
SCC_IR_NODE_OP, ///< 二元运算
SCC_IR_NODE_BRANCH, ///< 有条件分支
SCC_IR_NODE_JUMP, ///< 无条件跳转
SCC_IR_NODE_CALL, ///< 调用函数
SCC_IR_NODE_RET, ///< 函数返回
} scc_ir_node_tag_t;
typedef enum scc_ir_value_tag {
SCC_IR_VALUE_TAG_NULL,
SCC_IR_VALUE_TAG_CONST_INT,
SCC_IR_VALUE_TAG_CONST_UINT,
SCC_IR_VALUE_TAG_CONST_FLOAT,
SCC_IR_VALUE_TAG_CONST_ARRAY,
SCC_IR_VALUE_TAG_AGGREGATE, ///< 聚合值
SCC_IR_VALUE_TAG_CONV, ///< 类型转换
SCC_IR_VALUE_TAG_FUNC_ARG_REF, ///< 函数参数引用
SCC_IR_VALUE_TAG_BLOCK_ARG_REF, ///< 基本块参数引用
SCC_IR_VALUE_TAG_ALLOC, ///< 分配内存
SCC_IR_VALUE_TAG_GLOBAL_ALLOC, ///< 全局分配
SCC_IR_VALUE_TAG_LOAD, ///< 加载数据
SCC_IR_VALUE_TAG_STORE, ///< 存储数据
SCC_IR_VALUE_TAG_GET_PTR, ///< 获取指针
SCC_IR_VALUE_TAG_GET_ELEM_PTR, ///< 获取元素指针
SCC_IR_VALUE_TAG_OP, ///< 二元运算
SCC_IR_VALUE_TAG_BRANCH, ///< 有条件分支
SCC_IR_VALUE_TAG_JUMP, ///< 无条件跳转
SCC_IR_VALUE_TAG_CALL, ///< 调用函数
SCC_IR_VALUE_TAG_RET, ///< 函数返回
} scc_ir_value_tag_t;
typedef enum {
/// Empty op for init or nop
@@ -172,49 +175,55 @@ typedef union {
u8 float_any[16];
} scc_ir_const_float_t;
struct scc_ir_node {
struct scc_ir_value {
scc_ir_type_ref_t type;
scc_ir_label_t name;
scc_ir_node_ref_vec_t used_by;
scc_ir_node_tag_t tag;
scc_ir_value_tag_t tag;
union {
scc_ir_const_int_t const_int;
scc_ir_const_uint_t const_uint;
scc_ir_const_float_t const_float;
// aggregate;
struct {
scc_ir_value_ref_t base_type;
scc_ir_buffer_t elements;
} const_array;
struct {
scc_ir_node_ref_vec_t elements;
} aggregate;
struct {
usize idx;
} arg_ref;
struct {
scc_ir_node_ref_vec_t elements;
scc_ir_value_ref_t value;
} global_alloc;
struct {
scc_ir_node_ref_t operand;
scc_ir_value_ref_t operand;
scc_ir_type_ref_t target_type; // 目标类型
enum { CONV_SEXT, CONV_ZEXT, CONV_TRUNC } conv_type;
} conv;
struct {
scc_ir_node_ref_t target;
scc_ir_value_ref_t target;
} load;
struct {
scc_ir_node_ref_t target;
scc_ir_node_ref_t value;
scc_ir_value_ref_t target;
scc_ir_value_ref_t value;
} store;
struct {
scc_ir_node_ref_t src_addr;
scc_ir_node_ref_t index;
scc_ir_value_ref_t src_addr;
scc_ir_value_ref_t index;
} get_ptr;
struct {
scc_ir_node_ref_t src_addr;
scc_ir_node_ref_t index;
scc_ir_value_ref_t src_addr;
scc_ir_value_ref_t index;
} get_elem_ptr;
struct {
scc_ir_op_type_t op;
scc_ir_node_ref_t lhs;
scc_ir_node_ref_t rhs;
scc_ir_value_ref_t lhs;
scc_ir_value_ref_t rhs;
} op;
struct {
scc_ir_node_ref_t cond;
scc_ir_value_ref_t cond;
scc_ir_bblock_ref_t true_bblock;
scc_ir_bblock_ref_t false_bblock;
} branch;
@@ -226,7 +235,7 @@ struct scc_ir_node {
scc_ir_node_ref_vec_t args;
} call;
struct {
scc_ir_node_ref_t ret_val;
scc_ir_value_ref_t ret_val;
} ret;
} data;
};

View File

@@ -12,7 +12,7 @@ typedef struct {
void scc_ir_dump_ctx_init(scc_ir_dump_ctx_t *ctx,
scc_tree_dump_ctx_t *tree_dump,
scc_ir_cprog_t *cprog);
void scc_ir_dump_node(scc_ir_dump_ctx_t *ctx, scc_ir_node_ref_t node_ref);
void scc_ir_dump_value(scc_ir_dump_ctx_t *ctx, scc_ir_value_ref_t node_ref);
void scc_ir_dump_type(scc_ir_dump_ctx_t *ctx, scc_ir_type_ref_t type_ref);
void scc_ir_dump_bblock(scc_ir_dump_ctx_t *ctx, scc_ir_bblock_ref_t bblock_ref);
void scc_ir_dump_func(scc_ir_dump_ctx_t *ctx, scc_ir_func_ref_t func_ref);

View File

@@ -0,0 +1,42 @@
#ifndef __SCC_IR_MODULE_H__
#define __SCC_IR_MODULE_H__
#include "ir_def.h"
#include <scc_hashtable.h>
typedef struct {
unsigned int value_uid;
unsigned int type_uid;
unsigned int bblock_uid;
unsigned int func_uid;
SCC_VEC(scc_ir_value_t) values;
SCC_VEC(scc_ir_type_t) types;
SCC_VEC(scc_ir_bblock_t) bblocks;
SCC_VEC(scc_ir_func_t) funcs;
// UID -> ref index
scc_hashtable_t uid2value;
scc_hashtable_t uid2type;
scc_hashtable_t uid2bblock;
scc_hashtable_t uid2func;
} scc_ir_module_t;
void scc_ir_module_init(scc_ir_module_t *ctx);
void scc_ir_module_drop(scc_ir_module_t *ctx);
scc_ir_type_ref_t scc_ir_module_add_type(scc_ir_module_t *ctx,
const scc_ir_type_t *type);
scc_ir_value_ref_t scc_ir_module_add_value(scc_ir_module_t *ctx,
const scc_ir_value_t *node);
scc_ir_bblock_ref_t scc_ir_module_add_bblock(scc_ir_module_t *ctx,
const scc_ir_bblock_t *bblock);
scc_ir_func_ref_t scc_ir_module_add_func(scc_ir_module_t *ctx,
const scc_ir_func_t *func);
scc_ir_type_t *scc_ir_module_get_type(scc_ir_module_t *ctx,
scc_ir_type_ref_t ref);
scc_ir_value_t *scc_ir_module_get_value(scc_ir_module_t *ctx,
scc_ir_value_ref_t ref);
scc_ir_bblock_t *scc_ir_module_get_bblock(scc_ir_module_t *ctx,
scc_ir_bblock_ref_t ref);
scc_ir_func_t *scc_ir_module_get_func(scc_ir_module_t *ctx,
scc_ir_func_ref_t ref);
#endif /* __SCC_IR_MODULE_H__ */

View File

@@ -2,42 +2,7 @@
#define __SCC_IR_PROG_H__
#include "ir_def.h"
#include <scc_utils.h>
typedef struct {
unsigned int node_uid;
unsigned int type_uid;
unsigned int bblock_uid;
unsigned int func_uid;
SCC_VEC(scc_ir_node_t) nodes;
SCC_VEC(scc_ir_type_t) types;
SCC_VEC(scc_ir_bblock_t) bblocks;
SCC_VEC(scc_ir_func_t) funcs;
// UID -> ref index
scc_hashtable_t uid2nodes;
scc_hashtable_t uid2types;
scc_hashtable_t uid2bblocks;
scc_hashtable_t uid2funcs;
} scc_ir_module_t;
void scc_ir_module_init(scc_ir_module_t *ctx);
void scc_ir_module_drop(scc_ir_module_t *ctx);
scc_ir_type_ref_t scc_ir_module_add_type(scc_ir_module_t *ctx,
const scc_ir_type_t *type);
scc_ir_node_ref_t scc_ir_module_add_node(scc_ir_module_t *ctx,
const scc_ir_node_t *node);
scc_ir_bblock_ref_t scc_ir_module_add_bblock(scc_ir_module_t *ctx,
const scc_ir_bblock_t *bblock);
scc_ir_func_ref_t scc_ir_module_add_func(scc_ir_module_t *ctx,
const scc_ir_func_t *func);
scc_ir_type_t *scc_ir_module_get_type(scc_ir_module_t *ctx,
scc_ir_type_ref_t ref);
scc_ir_node_t *scc_ir_module_get_node(scc_ir_module_t *ctx,
scc_ir_node_ref_t ref);
scc_ir_bblock_t *scc_ir_module_get_bblock(scc_ir_module_t *ctx,
scc_ir_bblock_ref_t ref);
scc_ir_func_t *scc_ir_module_get_func(scc_ir_module_t *ctx,
scc_ir_func_ref_t ref);
#include "ir_module.h"
typedef struct scc_ir_cprog {
scc_ir_module_t module;

View File

@@ -9,7 +9,7 @@ void scc_ir_bblock_init(scc_ir_bblock_t *in, const char *label);
void scc_ir_func_init(scc_ir_func_t *in, const char *name);
// node name can be null ptr
void scc_ir_node_init(scc_ir_node_t *in, const char *name,
scc_ir_node_tag_t tag);
void scc_ir_node_init(scc_ir_value_t *in, const char *name,
scc_ir_value_tag_t tag);
#endif /* __SCC_IR_H__ */

View File

@@ -60,15 +60,15 @@ void scc_ir_builder_begin_func(scc_ir_builder_t *builder,
scc_ir_type_ref_t param_type =
scc_vec_at(func_type->data.function.params, i);
scc_ir_node_t param_node = {0};
param_node.tag = SCC_IR_NODE_FUNC_ARG_REF; // 参数节点标记
scc_ir_value_t param_node = {0};
param_node.tag = SCC_IR_VALUE_TAG_FUNC_ARG_REF; // 参数节点标记
param_node.type = param_type;
param_node.name = param_names ? param_names[i] : null;
param_node.data.arg_ref.idx = i;
scc_vec_init(param_node.used_by);
scc_ir_node_ref_t param_ref =
scc_ir_module_add_node(GET_MODULE(builder), &param_node);
scc_ir_value_ref_t param_ref =
scc_ir_module_add_value(GET_MODULE(builder), &param_node);
scc_vec_push(func_ptr->params, param_ref);
}
@@ -129,29 +129,29 @@ void scc_ir_builder_set_current_bblock(scc_ir_builder_t *builder,
builder->current_bblock = bblock;
}
static void scc_ir_builder_add_instr(scc_ir_builder_t *builder,
scc_ir_node_ref_t node) {
void scc_ir_builder_add_instr(scc_ir_builder_t *builder,
scc_ir_value_ref_t instr) {
scc_ir_bblock_t *current_bblock =
scc_ir_module_get_bblock(GET_MODULE(builder), builder->current_bblock);
if (current_bblock) {
scc_vec_push(current_bblock->instrs, node);
scc_vec_push(current_bblock->instrs, instr);
} else {
LOG_ERROR("Current basic block is not set");
}
}
scc_ir_node_ref_t scc_ir_builder_alloca(scc_ir_builder_t *builder,
scc_ir_type_ref_t type,
const char *name) {
scc_ir_node_t alloc_node = {0};
alloc_node.tag = SCC_IR_NODE_ALLOC;
scc_ir_value_ref_t scc_ir_builder_alloca(scc_ir_builder_t *builder,
scc_ir_type_ref_t type,
const char *name) {
scc_ir_value_t alloc_node = {0};
alloc_node.tag = SCC_IR_VALUE_TAG_ALLOC;
alloc_node.type = scc_ir_module_add_type(
GET_MODULE(builder),
&(scc_ir_type_t){.tag = SCC_IR_TYPE_PTR, .data.pointer.base = type});
alloc_node.name = name;
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &alloc_node);
scc_ir_value_ref_t node_ref =
scc_ir_module_add_value(GET_MODULE(builder), &alloc_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -159,31 +159,32 @@ scc_ir_node_ref_t scc_ir_builder_alloca(scc_ir_builder_t *builder,
return node_ref;
}
scc_ir_node_ref_t scc_ir_builder_func_arg_ref(scc_ir_builder_t *builder,
scc_ir_type_ref_t type,
const char *name, usize arg_idx) {
scc_ir_node_t node = {0};
node.tag = SCC_IR_NODE_FUNC_ARG_REF;
scc_ir_value_ref_t scc_ir_builder_func_arg_ref(scc_ir_builder_t *builder,
scc_ir_type_ref_t type,
const char *name,
usize arg_idx) {
scc_ir_value_t node = {0};
node.tag = SCC_IR_VALUE_TAG_FUNC_ARG_REF;
node.type = type;
node.name = name;
node.data.arg_ref.idx = arg_idx;
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &node);
scc_ir_value_ref_t node_ref =
scc_ir_module_add_value(GET_MODULE(builder), &node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
return node_ref;
}
scc_ir_node_ref_t scc_ir_builder_load(scc_ir_builder_t *builder,
scc_ir_node_ref_t target) {
scc_ir_node_t load_node = {0};
load_node.tag = SCC_IR_NODE_LOAD;
scc_ir_value_ref_t scc_ir_builder_load(scc_ir_builder_t *builder,
scc_ir_value_ref_t target) {
scc_ir_value_t load_node = {0};
load_node.tag = SCC_IR_VALUE_TAG_LOAD;
load_node.data.load.target = target;
// 设置类型为指针指向的类型
scc_ir_node_t *ptr_node =
scc_ir_module_get_node(GET_MODULE(builder), target);
scc_ir_value_t *ptr_node =
scc_ir_module_get_value(GET_MODULE(builder), target);
if (ptr_node) {
scc_ir_type_t *ptr_type =
scc_ir_module_get_type(GET_MODULE(builder), ptr_node->type);
@@ -192,8 +193,8 @@ scc_ir_node_ref_t scc_ir_builder_load(scc_ir_builder_t *builder,
}
}
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &load_node);
scc_ir_value_ref_t node_ref =
scc_ir_module_add_value(GET_MODULE(builder), &load_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -201,16 +202,16 @@ scc_ir_node_ref_t scc_ir_builder_load(scc_ir_builder_t *builder,
return node_ref;
}
scc_ir_node_ref_t scc_ir_builder_store(scc_ir_builder_t *builder,
scc_ir_node_ref_t target,
scc_ir_node_ref_t value) {
scc_ir_node_t store_node = {0};
store_node.tag = SCC_IR_NODE_STORE;
scc_ir_value_ref_t scc_ir_builder_store(scc_ir_builder_t *builder,
scc_ir_value_ref_t target,
scc_ir_value_ref_t value) {
scc_ir_value_t store_node = {0};
store_node.tag = SCC_IR_VALUE_TAG_STORE;
store_node.data.store.target = target;
store_node.data.store.value = value;
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &store_node);
scc_ir_value_ref_t node_ref =
scc_ir_module_add_value(GET_MODULE(builder), &store_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -218,23 +219,23 @@ scc_ir_node_ref_t scc_ir_builder_store(scc_ir_builder_t *builder,
return node_ref;
}
scc_ir_node_ref_t scc_ir_builder_get_ptr(scc_ir_builder_t *builder,
scc_ir_node_ref_t target,
scc_ir_node_ref_t index) {
scc_ir_node_t get_ptr_node = {0};
get_ptr_node.tag = SCC_IR_NODE_GET_PTR;
scc_ir_value_ref_t scc_ir_builder_get_ptr(scc_ir_builder_t *builder,
scc_ir_value_ref_t target,
scc_ir_value_ref_t index) {
scc_ir_value_t get_ptr_node = {0};
get_ptr_node.tag = SCC_IR_VALUE_TAG_GET_PTR;
get_ptr_node.data.get_ptr.src_addr = target;
get_ptr_node.data.get_ptr.index = index;
// 类型应与源地址相同(都是指针)
scc_ir_node_t *src_node =
scc_ir_module_get_node(GET_MODULE(builder), target);
scc_ir_value_t *src_node =
scc_ir_module_get_value(GET_MODULE(builder), target);
if (src_node) {
get_ptr_node.type = src_node->type;
}
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &get_ptr_node);
scc_ir_value_ref_t node_ref =
scc_ir_module_add_value(GET_MODULE(builder), &get_ptr_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -242,24 +243,25 @@ scc_ir_node_ref_t scc_ir_builder_get_ptr(scc_ir_builder_t *builder,
return node_ref;
}
scc_ir_node_ref_t scc_ir_builder_binop(scc_ir_builder_t *builder,
scc_ir_op_type_t op,
scc_ir_node_ref_t lhs,
scc_ir_node_ref_t rhs) {
scc_ir_node_t binop_node = {0};
binop_node.tag = SCC_IR_NODE_OP;
scc_ir_value_ref_t scc_ir_builder_binop(scc_ir_builder_t *builder,
scc_ir_op_type_t op,
scc_ir_value_ref_t lhs,
scc_ir_value_ref_t rhs) {
scc_ir_value_t binop_node = {0};
binop_node.tag = SCC_IR_VALUE_TAG_OP;
binop_node.data.op.op = op;
binop_node.data.op.lhs = lhs;
binop_node.data.op.rhs = rhs;
// 类型通常与操作数相同(对于算术运算)
scc_ir_node_t *lhs_node = scc_ir_module_get_node(GET_MODULE(builder), lhs);
scc_ir_value_t *lhs_node =
scc_ir_module_get_value(GET_MODULE(builder), lhs);
if (lhs_node) {
binop_node.type = lhs_node->type;
}
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &binop_node);
scc_ir_value_ref_t node_ref =
scc_ir_module_add_value(GET_MODULE(builder), &binop_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -267,11 +269,12 @@ scc_ir_node_ref_t scc_ir_builder_binop(scc_ir_builder_t *builder,
return node_ref;
}
scc_ir_node_ref_t scc_ir_builder_cmp(scc_ir_builder_t *builder,
scc_ir_op_type_t op, scc_ir_node_ref_t lhs,
scc_ir_node_ref_t rhs) {
scc_ir_node_t cmp_node = {0};
cmp_node.tag = SCC_IR_NODE_OP;
scc_ir_value_ref_t scc_ir_builder_cmp(scc_ir_builder_t *builder,
scc_ir_op_type_t op,
scc_ir_value_ref_t lhs,
scc_ir_value_ref_t rhs) {
scc_ir_value_t cmp_node = {0};
cmp_node.tag = SCC_IR_VALUE_TAG_OP;
cmp_node.data.op.op = op;
cmp_node.data.op.lhs = lhs;
cmp_node.data.op.rhs = rhs;
@@ -280,8 +283,8 @@ scc_ir_node_ref_t scc_ir_builder_cmp(scc_ir_builder_t *builder,
cmp_node.type =
0; // FIXME scc_ir_module_get_builtin_i32(GET_MODULE(builder));
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &cmp_node);
scc_ir_value_ref_t node_ref =
scc_ir_module_add_value(GET_MODULE(builder), &cmp_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -289,14 +292,14 @@ scc_ir_node_ref_t scc_ir_builder_cmp(scc_ir_builder_t *builder,
return node_ref;
}
scc_ir_node_ref_t scc_ir_builder_jump(scc_ir_builder_t *builder,
scc_ir_bblock_ref_t target) {
scc_ir_node_t jump_node = {0};
jump_node.tag = SCC_IR_NODE_JUMP;
scc_ir_value_ref_t scc_ir_builder_jump(scc_ir_builder_t *builder,
scc_ir_bblock_ref_t target) {
scc_ir_value_t jump_node = {0};
jump_node.tag = SCC_IR_VALUE_TAG_JUMP;
jump_node.data.jump.target_bblock = target;
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &jump_node);
scc_ir_value_ref_t node_ref =
scc_ir_module_add_value(GET_MODULE(builder), &jump_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -304,18 +307,18 @@ scc_ir_node_ref_t scc_ir_builder_jump(scc_ir_builder_t *builder,
return node_ref;
}
scc_ir_node_ref_t scc_ir_builder_branch(scc_ir_builder_t *builder,
scc_ir_node_ref_t cond,
scc_ir_bblock_ref_t true_target,
scc_ir_bblock_ref_t false_target) {
scc_ir_node_t branch_node = {0};
branch_node.tag = SCC_IR_NODE_BRANCH;
scc_ir_value_ref_t scc_ir_builder_branch(scc_ir_builder_t *builder,
scc_ir_value_ref_t cond,
scc_ir_bblock_ref_t true_target,
scc_ir_bblock_ref_t false_target) {
scc_ir_value_t branch_node = {0};
branch_node.tag = SCC_IR_VALUE_TAG_BRANCH;
branch_node.data.branch.cond = cond;
branch_node.data.branch.true_bblock = true_target;
branch_node.data.branch.false_bblock = false_target;
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &branch_node);
scc_ir_value_ref_t node_ref =
scc_ir_module_add_value(GET_MODULE(builder), &branch_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -323,12 +326,12 @@ scc_ir_node_ref_t scc_ir_builder_branch(scc_ir_builder_t *builder,
return node_ref;
}
scc_ir_node_ref_t scc_ir_builder_call(scc_ir_builder_t *builder,
scc_ir_func_ref_t callee,
const scc_ir_node_ref_t *args,
usize arg_count) {
scc_ir_node_t call_node = {0};
call_node.tag = SCC_IR_NODE_CALL;
scc_ir_value_ref_t scc_ir_builder_call(scc_ir_builder_t *builder,
scc_ir_func_ref_t callee,
const scc_ir_value_ref_t *args,
usize arg_count) {
scc_ir_value_t call_node = {0};
call_node.tag = SCC_IR_VALUE_TAG_CALL;
call_node.data.call.callee = callee;
scc_vec_init(call_node.data.call.args);
@@ -347,8 +350,8 @@ scc_ir_node_ref_t scc_ir_builder_call(scc_ir_builder_t *builder,
}
}
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &call_node);
scc_ir_value_ref_t node_ref =
scc_ir_module_add_value(GET_MODULE(builder), &call_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -356,14 +359,14 @@ scc_ir_node_ref_t scc_ir_builder_call(scc_ir_builder_t *builder,
return node_ref;
}
scc_ir_node_ref_t scc_ir_builder_ret(scc_ir_builder_t *builder,
scc_ir_node_ref_t value) {
scc_ir_node_t ret_node = {0};
ret_node.tag = SCC_IR_NODE_RET;
scc_ir_value_ref_t scc_ir_builder_ret(scc_ir_builder_t *builder,
scc_ir_value_ref_t value) {
scc_ir_value_t ret_node = {0};
ret_node.tag = SCC_IR_VALUE_TAG_RET;
ret_node.data.ret.ret_val = value;
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &ret_node);
scc_ir_value_ref_t node_ref =
scc_ir_module_add_value(GET_MODULE(builder), &ret_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);
@@ -371,13 +374,13 @@ scc_ir_node_ref_t scc_ir_builder_ret(scc_ir_builder_t *builder,
return node_ref;
}
scc_ir_node_ref_t scc_ir_builder_ret_void(scc_ir_builder_t *builder) {
scc_ir_node_t ret_node = {0};
ret_node.tag = SCC_IR_NODE_RET;
scc_ir_value_ref_t scc_ir_builder_ret_void(scc_ir_builder_t *builder) {
scc_ir_value_t ret_node = {0};
ret_node.tag = SCC_IR_VALUE_TAG_RET;
ret_node.data.ret.ret_val = 0; // 无返回值
scc_ir_node_ref_t node_ref =
scc_ir_module_add_node(GET_MODULE(builder), &ret_node);
scc_ir_value_ref_t node_ref =
scc_ir_module_add_value(GET_MODULE(builder), &ret_node);
// 添加到当前基本块
scc_ir_builder_add_instr(builder, node_ref);

View File

@@ -149,10 +149,10 @@ scc_ir_type_ref_t scc_ir_ctx_get_type(scc_ir_ctx_t *ctx,
return new_ref;
}
// scc_ir_node_ref_t scc_ir_ctx_get_const_int(scc_ir_ctx_t *ctx,
// scc_ir_value_ref_t scc_ir_ctx_get_const_int(scc_ir_ctx_t *ctx,
// scc_ir_type_ref_t type, i64 value)
// {
// return scc_ir_node_ref_t();
// return scc_ir_value_ref_t();
// }
scc_ir_func_ref_t scc_ir_ctx_declare_func(scc_ir_ctx_t *ctx,

View File

@@ -13,27 +13,29 @@
SCC_TREE_DUMP_PRINT_AROUND(ctx, ctx->value_color, "'", "%s", str)
// 获取IR节点类型的字符串表示
static const char *get_node_type_str(scc_ir_node_tag_t tag) {
static const char *get_node_type_str(scc_ir_value_tag_t tag) {
static const char *node_types[] = {
[SCC_IR_NODE_NULL] = "Null",
[SCC_IR_NODE_CONST_INT] = "ConstInt",
[SCC_IR_NODE_CONST_UINT] = "ConstUint",
[SCC_IR_NODE_CONST_FLOAT] = "ConstFloat",
[SCC_IR_NODE_CONV] = "Convert", ///< 类型转换
[SCC_IR_NODE_FUNC_ARG_REF] = "FuncArgRef", ///< 函数参数引用
[SCC_IR_NODE_BLOCK_ARG_REF] = "BlockArgRef", ///< 基本块参数引用
[SCC_IR_NODE_ALLOC] = "Alloc", ///< 分配内存(stack)
[SCC_IR_NODE_GLOBAL_ALLOC] = "GlobalAlloc", ///< 全局分配(bss)
[SCC_IR_NODE_LOAD] = "Load", ///< 加载数据
[SCC_IR_NODE_STORE] = "Store", ///< 存储数据
[SCC_IR_NODE_GET_PTR] = "GetPtr", ///< 获取指针
[SCC_IR_NODE_GET_ELEM_PTR] =
"GetElemPtr", ///< 获取元素指针(used by array)
[SCC_IR_NODE_OP] = "Op", ///< 二元运算
[SCC_IR_NODE_BRANCH] = "Branch", ///< 有条件分支
[SCC_IR_NODE_JUMP] = "Jump", ///< 无条件跳转
[SCC_IR_NODE_CALL] = "Call", ///< 调用函数
[SCC_IR_NODE_RET] = "Ret", ///< 函数返回
[SCC_IR_VALUE_TAG_NULL] = "Null",
[SCC_IR_VALUE_TAG_CONST_INT] = "ConstInt",
[SCC_IR_VALUE_TAG_CONST_UINT] = "ConstUint",
[SCC_IR_VALUE_TAG_CONST_FLOAT] = "ConstFloat",
[SCC_IR_VALUE_TAG_CONST_ARRAY] = "ConstArray",
[SCC_IR_VALUE_TAG_AGGREGATE] = "Aggregate",
[SCC_IR_VALUE_TAG_CONV] = "Convert", ///< 类型转换
[SCC_IR_VALUE_TAG_FUNC_ARG_REF] = "FuncArgRef", ///< 函数参数引用
[SCC_IR_VALUE_TAG_BLOCK_ARG_REF] = "BlockArgRef", ///< 基本块参数引用
[SCC_IR_VALUE_TAG_ALLOC] = "Alloc", ///< 分配内存(stack)
[SCC_IR_VALUE_TAG_GLOBAL_ALLOC] = "GlobalAlloc", ///< 全局分配(bss)
[SCC_IR_VALUE_TAG_LOAD] = "Load", ///< 加载数据
[SCC_IR_VALUE_TAG_STORE] = "Store", ///< 存储数据
[SCC_IR_VALUE_TAG_GET_PTR] = "GetPtr", ///< 获取指针
[SCC_IR_VALUE_TAG_GET_ELEM_PTR] =
"GetElemPtr", ///< 获取元素指针(used by array)
[SCC_IR_VALUE_TAG_OP] = "Op", ///< 二元运算
[SCC_IR_VALUE_TAG_BRANCH] = "Branch", ///< 有条件分支
[SCC_IR_VALUE_TAG_JUMP] = "Jump", ///< 无条件跳转
[SCC_IR_VALUE_TAG_CALL] = "Call", ///< 调用函数
[SCC_IR_VALUE_TAG_RET] = "Ret", ///< 函数返回
};
if (tag >= 0 && (usize)tag < sizeof(node_types) / sizeof(node_types[0]) &&
@@ -89,18 +91,18 @@ static const char *get_type_tag_str(scc_ir_type_tag_t tag) {
// 递归转储辅助函数(使用引用)
static inline void dump_child_node_ref(scc_ir_dump_ctx_t *ctx,
scc_ir_node_ref_t child_ref,
scc_ir_value_ref_t child_ref,
cbool is_last) {
if (!child_ref)
return;
scc_tree_dump_push_level(ctx->dump_ctx, is_last);
scc_ir_dump_node(ctx, child_ref);
scc_ir_dump_value(ctx, child_ref);
scc_tree_dump_pop_level(ctx->dump_ctx);
}
// 转储常量整数节点
static void dump_const_int_node(scc_ir_dump_ctx_t *ctx,
const scc_ir_node_t *node) {
const scc_ir_value_t *node) {
scc_tree_dump_push_level(ctx->dump_ctx, true);
scc_tree_print_indent(ctx->dump_ctx);
scc_tree_dump_printf(ctx->dump_ctx, "%d\n", node->data.const_int.int32);
@@ -108,7 +110,7 @@ static void dump_const_int_node(scc_ir_dump_ctx_t *ctx,
}
// 转储操作节点
static void dump_op_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
static void dump_op_node(scc_ir_dump_ctx_t *ctx, const scc_ir_value_t *node) {
// 打印操作符
scc_tree_dump_push_level(ctx->dump_ctx, false);
scc_tree_print_indent(ctx->dump_ctx);
@@ -129,7 +131,7 @@ static void dump_op_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
}
// 转储加载节点
static void dump_load_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
static void dump_load_node(scc_ir_dump_ctx_t *ctx, const scc_ir_value_t *node) {
// PRINT_NODE(ctx->dump_ctx, "load");
if (node->data.load.target) {
dump_child_node_ref(ctx, node->data.load.target, true);
@@ -137,7 +139,8 @@ static void dump_load_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
}
// 转储存储节点
static void dump_store_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
static void dump_store_node(scc_ir_dump_ctx_t *ctx,
const scc_ir_value_t *node) {
// PRINT_NODE(ctx->dump_ctx, "store");
// 输出存储位置
@@ -153,7 +156,7 @@ static void dump_store_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
// 转储获取指针节点
static void dump_get_ptr_node(scc_ir_dump_ctx_t *ctx,
const scc_ir_node_t *node) {
const scc_ir_value_t *node) {
// PRINT_NODE(ctx->dump_ctx, "get_ptr");
// 输出源地址
if (node->data.get_ptr.src_addr) {
@@ -168,7 +171,7 @@ static void dump_get_ptr_node(scc_ir_dump_ctx_t *ctx,
// 转储分支节点
static void dump_branch_node(scc_ir_dump_ctx_t *ctx,
const scc_ir_node_t *node) {
const scc_ir_value_t *node) {
// PRINT_NODE(ctx->dump_ctx, "branch");
// 输出条件
@@ -204,7 +207,7 @@ static void dump_branch_node(scc_ir_dump_ctx_t *ctx,
}
// 转储跳转节点
static void dump_jump_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
static void dump_jump_node(scc_ir_dump_ctx_t *ctx, const scc_ir_value_t *node) {
// PRINT_NODE(ctx->dump_ctx, "jump");
if (node->data.jump.target_bblock) {
scc_ir_bblock_t *target_bblock = scc_ir_module_get_bblock(
@@ -222,7 +225,7 @@ static void dump_jump_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
}
// 转储调用节点
static void dump_call_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
static void dump_call_node(scc_ir_dump_ctx_t *ctx, const scc_ir_value_t *node) {
// PRINT_NODE(ctx->dump_ctx, "call");
if (node->data.call.callee) {
scc_ir_func_t *callee =
@@ -246,7 +249,7 @@ static void dump_call_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
cbool is_last = (i + 1 == scc_vec_size(node->data.call.args));
scc_tree_dump_push_level(ctx->dump_ctx, is_last);
scc_ir_node_ref_t arg_ref = scc_vec_at(node->data.call.args, i);
scc_ir_value_ref_t arg_ref = scc_vec_at(node->data.call.args, i);
dump_child_node_ref(ctx, arg_ref, is_last);
scc_tree_dump_pop_level(ctx->dump_ctx);
@@ -254,7 +257,7 @@ static void dump_call_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
}
// 转储返回节点
static void dump_ret_node(scc_ir_dump_ctx_t *ctx, const scc_ir_node_t *node) {
static void dump_ret_node(scc_ir_dump_ctx_t *ctx, const scc_ir_value_t *node) {
// PRINT_NODE(ctx->dump_ctx, "ret");
if (node->data.ret.ret_val) {
dump_child_node_ref(ctx, node->data.ret.ret_val, true);
@@ -268,8 +271,8 @@ void scc_ir_dump_ctx_init(scc_ir_dump_ctx_t *ctx,
ctx->dump_ctx = tree_dump;
}
void scc_ir_dump_node(scc_ir_dump_ctx_t *ctx, scc_ir_node_ref_t node_ref) {
scc_ir_node_t *node = scc_ir_module_get_node(GET_MODULE(ctx), node_ref);
void scc_ir_dump_value(scc_ir_dump_ctx_t *ctx, scc_ir_value_ref_t node_ref) {
scc_ir_value_t *node = scc_ir_module_get_value(GET_MODULE(ctx), node_ref);
if (!node) {
LOG_ERROR("Invalid node ref");
return;
@@ -295,35 +298,35 @@ void scc_ir_dump_node(scc_ir_dump_ctx_t *ctx, scc_ir_node_ref_t node_ref) {
// 根据节点类型输出特定信息
switch (node->tag) {
case SCC_IR_NODE_NULL:
case SCC_IR_VALUE_TAG_NULL:
break;
case SCC_IR_NODE_CONST_INT:
case SCC_IR_VALUE_TAG_CONST_INT:
dump_const_int_node(ctx, node);
break;
case SCC_IR_NODE_ALLOC:
case SCC_IR_VALUE_TAG_ALLOC:
break;
case SCC_IR_NODE_LOAD:
case SCC_IR_VALUE_TAG_LOAD:
dump_load_node(ctx, node);
break;
case SCC_IR_NODE_STORE:
case SCC_IR_VALUE_TAG_STORE:
dump_store_node(ctx, node);
break;
case SCC_IR_NODE_GET_PTR:
case SCC_IR_VALUE_TAG_GET_PTR:
dump_get_ptr_node(ctx, node);
break;
case SCC_IR_NODE_OP:
case SCC_IR_VALUE_TAG_OP:
dump_op_node(ctx, node);
break;
case SCC_IR_NODE_BRANCH:
case SCC_IR_VALUE_TAG_BRANCH:
dump_branch_node(ctx, node);
break;
case SCC_IR_NODE_JUMP:
case SCC_IR_VALUE_TAG_JUMP:
dump_jump_node(ctx, node);
break;
case SCC_IR_NODE_CALL:
case SCC_IR_VALUE_TAG_CALL:
dump_call_node(ctx, node);
break;
case SCC_IR_NODE_RET:
case SCC_IR_VALUE_TAG_RET:
dump_ret_node(ctx, node);
break;
default:
@@ -429,8 +432,8 @@ void scc_ir_dump_bblock(scc_ir_dump_ctx_t *ctx,
cbool is_last = (i + 1 == scc_vec_size(bblock->instrs));
scc_tree_dump_push_level(ctx->dump_ctx, is_last);
scc_ir_node_ref_t instr_ref = scc_vec_at(bblock->instrs, i);
scc_ir_dump_node(ctx, instr_ref);
scc_ir_value_ref_t instr_ref = scc_vec_at(bblock->instrs, i);
scc_ir_dump_value(ctx, instr_ref);
scc_tree_dump_pop_level(ctx->dump_ctx);
}
}
@@ -460,8 +463,8 @@ void scc_ir_dump_func(scc_ir_dump_ctx_t *ctx, scc_ir_func_ref_t func_ref) {
cbool is_last = (i + 1 == scc_vec_size(func->params));
scc_tree_dump_push_level(ctx->dump_ctx, is_last);
scc_ir_node_ref_t param_ref = scc_vec_at(func->params, i);
scc_ir_dump_node(ctx, param_ref);
scc_ir_value_ref_t param_ref = scc_vec_at(func->params, i);
scc_ir_dump_value(ctx, param_ref);
scc_tree_dump_pop_level(ctx->dump_ctx);
}
@@ -489,10 +492,10 @@ void scc_ir_dump_cprog(scc_ir_dump_ctx_t *ctx) {
// scc_vec_size(program->global_vals));
// scc_tree_dump_push_level(ctx->dump_ctx, is_last);
// scc_ir_node_ref_t global_ref =
// scc_vec_at(program->global_vals, i); scc_ir_node_t
// scc_ir_value_ref_t global_ref =
// scc_vec_at(program->global_vals, i); scc_ir_value_t
// *global_node =
// scc_ir_module_get_node(ir_ctx, global_ref);
// scc_ir_module_get_value(ir_ctx, global_ref);
// if (global_node) {
// scc_ir_dump_node(ctx, ir_ctx, global_node);
// }
@@ -536,11 +539,23 @@ void scc_ir_dump_type_linear(scc_ir_dump_ctx_t *ctx,
}
#define PRINT_TYPE(ctx, name) PRINT_VALUE(ctx, "%s", name)
switch (type->tag) {
case SCC_IR_TYPE_i32:
PRINT_TYPE(ctx->dump_ctx, "i32");
break;
case SCC_IR_TYPE_unknown:
case SCC_IR_TYPE_void:
PRINT_TYPE(ctx->dump_ctx, "void");
case SCC_IR_TYPE_i8:
case SCC_IR_TYPE_i16:
case SCC_IR_TYPE_i32:
case SCC_IR_TYPE_i64:
case SCC_IR_TYPE_i128:
case SCC_IR_TYPE_u8:
case SCC_IR_TYPE_u16:
case SCC_IR_TYPE_u32:
case SCC_IR_TYPE_u64:
case SCC_IR_TYPE_u128:
case SCC_IR_TYPE_f16:
case SCC_IR_TYPE_f32:
case SCC_IR_TYPE_f64:
case SCC_IR_TYPE_f128:
PRINT_TYPE(ctx->dump_ctx, get_type_tag_str(type->tag));
break;
case SCC_IR_TYPE_ARRAY:
PRINT_TYPE(ctx->dump_ctx, "[");
@@ -579,14 +594,14 @@ void scc_ir_dump_type_linear(scc_ir_dump_ctx_t *ctx,
// 辅助函数:输出节点引用或值到缓冲区
static usize format_node_ref_or_value(scc_ir_dump_ctx_t *ctx, char *buf,
usize size, scc_ir_node_ref_t node_ref) {
scc_ir_node_t *node = scc_ir_module_get_node(GET_MODULE(ctx), node_ref);
usize size, scc_ir_value_ref_t node_ref) {
scc_ir_value_t *node = scc_ir_module_get_value(GET_MODULE(ctx), node_ref);
if (!node) {
return scc_snprintf(buf, size, "%%%u", node_ref);
}
// 如果是常量整数,直接输出值
if (node->tag == SCC_IR_NODE_CONST_INT) {
if (node->tag == SCC_IR_VALUE_TAG_CONST_INT) {
return scc_snprintf(buf, size, "%d", node->data.const_int.int32);
}
@@ -600,8 +615,8 @@ static usize format_node_ref_or_value(scc_ir_dump_ctx_t *ctx, char *buf,
// 线性输出节点信息SSA IR风格
void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
scc_ir_node_ref_t node_ref) {
scc_ir_node_t *node = scc_ir_module_get_node(GET_MODULE(ctx), node_ref);
scc_ir_value_ref_t node_ref) {
scc_ir_value_t *node = scc_ir_module_get_value(GET_MODULE(ctx), node_ref);
if (node == null) {
LOG_ERROR("invalid node ref");
return;
@@ -612,9 +627,10 @@ void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
usize remaining = sizeof(buff);
// 判断是否需要输出等号
cbool needs_equals =
(node->tag != SCC_IR_NODE_BRANCH && node->tag != SCC_IR_NODE_JUMP &&
node->tag != SCC_IR_NODE_RET && node->tag != SCC_IR_NODE_STORE);
cbool needs_equals = (node->tag != SCC_IR_VALUE_TAG_BRANCH &&
node->tag != SCC_IR_VALUE_TAG_JUMP &&
node->tag != SCC_IR_VALUE_TAG_RET &&
node->tag != SCC_IR_VALUE_TAG_STORE);
if (needs_equals) {
// 输出左值和类型
@@ -641,16 +657,38 @@ void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
// 构建操作部分
switch (node->tag) {
case SCC_IR_NODE_CONST_INT:
case SCC_IR_VALUE_TAG_CONST_INT:
// 常量节点定义,直接输出值
p += scc_snprintf(p, remaining, "%d", node->data.const_int.int32);
break;
case SCC_IR_VALUE_TAG_CONST_UINT:
scc_ir_type_t *type =
scc_ir_module_get_type(GET_MODULE(ctx), node->type);
Assert(type != null);
if (type->tag == SCC_IR_TYPE_u8) {
p += scc_snprintf(p, remaining, "%c", node->data.const_uint.uint8);
} else {
p += scc_snprintf(p, remaining, "%u", node->data.const_uint.uint32);
}
break;
case SCC_IR_VALUE_TAG_CONST_FLOAT:
p += scc_snprintf(p, remaining, "%f", node->data.const_float.float32);
break;
case SCC_IR_NODE_ALLOC:
case SCC_IR_VALUE_TAG_AGGREGATE:
scc_vec_foreach(node->data.aggregate.elements, i) {
scc_ir_value_ref_t elem_node =
scc_vec_at(node->data.aggregate.elements, i);
scc_ir_dump_node_linear(ctx, elem_node);
PRINT_NODE(ctx->dump_ctx, "\n");
}
return;
case SCC_IR_VALUE_TAG_ALLOC:
p += scc_snprintf(p, remaining, "alloc");
break;
case SCC_IR_NODE_LOAD: {
case SCC_IR_VALUE_TAG_LOAD: {
char operand_buf[64];
format_node_ref_or_value(ctx, operand_buf, sizeof(operand_buf),
node->data.load.target);
@@ -658,7 +696,7 @@ void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
break;
}
case SCC_IR_NODE_STORE: {
case SCC_IR_VALUE_TAG_STORE: {
char value_buf[64], target_buf[64];
format_node_ref_or_value(ctx, value_buf, sizeof(value_buf),
node->data.store.value);
@@ -669,17 +707,21 @@ void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
break;
}
case SCC_IR_NODE_GET_PTR: {
case SCC_IR_VALUE_TAG_GET_ELEM_PTR:
case SCC_IR_VALUE_TAG_GET_PTR: {
char src_buf[64], idx_buf[64];
format_node_ref_or_value(ctx, src_buf, sizeof(src_buf),
node->data.get_ptr.src_addr);
format_node_ref_or_value(ctx, idx_buf, sizeof(idx_buf),
node->data.get_ptr.index);
p += scc_snprintf(p, remaining, "getelemptr %s, %s", src_buf, idx_buf);
p += scc_snprintf(p, remaining, "%s %s, %s",
node->tag == SCC_IR_VALUE_TAG_GET_PTR ? "getptr"
: "getelemptr",
src_buf, idx_buf);
break;
}
case SCC_IR_NODE_OP: {
case SCC_IR_VALUE_TAG_OP: {
char lhs_buf[64], rhs_buf[64];
format_node_ref_or_value(ctx, lhs_buf, sizeof(lhs_buf),
node->data.op.lhs);
@@ -690,7 +732,7 @@ void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
break;
}
case SCC_IR_NODE_BRANCH:
case SCC_IR_VALUE_TAG_BRANCH:
if (node->data.branch.cond) {
char cond_buf[64];
format_node_ref_or_value(ctx, cond_buf, sizeof(cond_buf),
@@ -704,12 +746,12 @@ void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
}
break;
case SCC_IR_NODE_JUMP:
case SCC_IR_VALUE_TAG_JUMP:
p += scc_snprintf(p, remaining, "jmp label %%%u",
node->data.jump.target_bblock);
break;
case SCC_IR_NODE_CALL: {
case SCC_IR_VALUE_TAG_CALL: {
char node_name[256] = {0};
char args_buf[256] = {0};
char *args_p = args_buf;
@@ -736,7 +778,7 @@ void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
break;
}
case SCC_IR_NODE_RET:
case SCC_IR_VALUE_TAG_RET:
if (node->data.ret.ret_val != 0) {
char ret_buf[64];
format_node_ref_or_value(ctx, ret_buf, sizeof(ret_buf),
@@ -747,13 +789,33 @@ void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
}
break;
case SCC_IR_NODE_FUNC_ARG_REF: {
case SCC_IR_VALUE_TAG_FUNC_ARG_REF: {
p += scc_snprintf(p, remaining, "arg[%zu]", node->data.arg_ref.idx);
break;
}
case SCC_IR_VALUE_TAG_GLOBAL_ALLOC: {
scc_snprintf(p, remaining, "global %s\n", node->name);
PRINT_NODE(ctx->dump_ctx, buff);
scc_ir_dump_node_linear(ctx, node->data.global_alloc.value);
return;
}
case SCC_IR_VALUE_TAG_CONST_ARRAY: {
PRINT_NODE(ctx->dump_ctx, "const_array ");
scc_ir_dump_type_linear(ctx, node->data.const_array.base_type);
PRINT_NODE(ctx->dump_ctx, " [");
scc_vec_foreach(node->data.const_array.elements, i) {
u8 ch = scc_vec_at(node->data.const_array.elements, i);
p += scc_snprintf(p, remaining, " `%c`, ", ch ? ch : ' ');
remaining = sizeof(buff) - (p - buff);
}
p += scc_snprintf(p, remaining, " ]\n");
break;
}
default:
p += scc_snprintf(p, remaining, "<%s node %u>",
p += scc_snprintf(p, remaining, "<%s node %u>\n",
get_node_type_str(node->tag), node_ref);
break;
}
@@ -786,15 +848,15 @@ void scc_ir_dump_bblock_linear(scc_ir_dump_ctx_t *ctx,
// 打印基本块中的每条指令
for (usize i = 0; i < scc_vec_size(bblock->instrs); i++) {
scc_ir_node_ref_t node_ref = scc_vec_at(bblock->instrs, i);
scc_ir_value_ref_t node_ref = scc_vec_at(bblock->instrs, i);
PRINT_NODE(ctx->dump_ctx, "\n ");
scc_ir_dump_node_linear(ctx, node_ref);
}
}
// 线性输出函数信息
void scc_ir_dump_func_linear(scc_ir_dump_ctx_t *ctx,
scc_ir_func_ref_t func_ref) {
void scc_ir_dump_func_linear(scc_ir_dump_ctx_t *ctx, scc_ir_func_ref_t func_ref,
int is_decl) {
scc_ir_func_t *func = scc_ir_module_get_func(GET_MODULE(ctx), func_ref);
if (!func) {
LOG_ERROR("invalid function reference");
@@ -817,10 +879,10 @@ void scc_ir_dump_func_linear(scc_ir_dump_ctx_t *ctx,
for (usize i = 0; i < scc_vec_size(func->params); i++) {
if (i > 0)
PRINT_NODE(ctx->dump_ctx, ", ");
scc_ir_node_ref_t param_ref = scc_vec_at(func->params, i);
scc_ir_value_ref_t param_ref = scc_vec_at(func->params, i);
PRINT_NODE(ctx->dump_ctx, "%");
scc_ir_node_t *param_node =
scc_ir_module_get_node(GET_MODULE(ctx), param_ref);
scc_ir_value_t *param_node =
scc_ir_module_get_value(GET_MODULE(ctx), param_ref);
if (param_node && param_node->name && param_node->name[0] != '\0') {
scc_snprintf(buff, sizeof(buff), "%u[%s]", param_ref,
param_node->name);
@@ -838,6 +900,11 @@ void scc_ir_dump_func_linear(scc_ir_dump_ctx_t *ctx,
PRINT_NODE(ctx->dump_ctx, ": ");
scc_ir_dump_type_linear(ctx, func->type);
if (is_decl) {
PRINT_NODE(ctx->dump_ctx, ";\n");
return;
}
PRINT_NODE(ctx->dump_ctx, " {\n");
// 打印基本块
@@ -852,15 +919,20 @@ void scc_ir_dump_func_linear(scc_ir_dump_ctx_t *ctx,
// 线性输出整个程序
void scc_ir_dump_cprog_linear(scc_ir_dump_ctx_t *ctx) {
scc_vec_foreach(ctx->cprog->global_vals, i) {
scc_ir_value_ref_t node_ref = scc_vec_at(ctx->cprog->global_vals, i);
scc_ir_dump_node_linear(ctx, node_ref);
}
scc_vec_foreach(ctx->cprog->func_decls, i) {
scc_ir_func_ref_t func_decl = scc_vec_at(ctx->cprog->func_decls, i);
scc_ir_func_t *func =
scc_ir_module_get_func(GET_MODULE(ctx), func_decl);
Assert(func != null);
if (scc_vec_size(func->bblocks) == 0)
scc_ir_dump_func_linear(ctx, func_decl);
scc_ir_dump_func_linear(ctx, func_decl, true);
}
scc_vec_foreach(ctx->cprog->func_defs, i) {
scc_ir_dump_func_linear(ctx, scc_vec_at(ctx->cprog->func_defs, i));
scc_ir_dump_func_linear(ctx, scc_vec_at(ctx->cprog->func_defs, i),
false);
}
}

137
libs/ir/src/ir_module.c Normal file
View File

@@ -0,0 +1,137 @@
#include <ir_module.h>
static u32 hash_key(const void *key) { return (u32)(usize)key; }
static int cmp_key(const void *key1, const void *key2) {
return (u32)(usize)key1 != (u32)(usize)key2;
}
void scc_ir_module_init(scc_ir_module_t *ctx) {
scc_vec_init(ctx->values);
scc_vec_init(ctx->types);
scc_vec_init(ctx->bblocks);
scc_vec_init(ctx->funcs);
scc_hashtable_init(&ctx->uid2value, hash_key, cmp_key);
scc_hashtable_init(&ctx->uid2type, hash_key, cmp_key);
scc_hashtable_init(&ctx->uid2bblock, hash_key, cmp_key);
scc_hashtable_init(&ctx->uid2func, hash_key, cmp_key);
// 预留UID 0 作为无效引用
ctx->value_uid = 1;
ctx->type_uid = 1;
ctx->bblock_uid = 1;
ctx->func_uid = 1;
}
void scc_ir_module_drop(scc_ir_module_t *ctx) {
// 释放所有实体的内部内存
for (usize i = 0; i < ctx->values.size; i++) {
scc_ir_value_t *node = &ctx->values.data[i];
scc_vec_free(node->used_by);
if (node->tag == SCC_IR_VALUE_TAG_CALL) {
scc_vec_free(node->data.call.args);
}
}
for (usize i = 0; i < ctx->types.size; i++) {
scc_ir_type_t *type = &ctx->types.data[i];
if (type->tag == SCC_IR_TYPE_FUNC) {
scc_vec_free(type->data.function.params);
}
}
for (usize i = 0; i < ctx->bblocks.size; i++) {
scc_ir_bblock_t *bblock = &ctx->bblocks.data[i];
scc_vec_free(bblock->instrs);
}
for (usize i = 0; i < ctx->funcs.size; i++) {
scc_ir_func_t *func = &ctx->funcs.data[i];
scc_vec_free(func->params);
scc_vec_free(func->bblocks);
}
scc_vec_free(ctx->values);
scc_vec_free(ctx->types);
scc_vec_free(ctx->bblocks);
scc_vec_free(ctx->funcs);
scc_hashtable_drop(&ctx->uid2value);
scc_hashtable_drop(&ctx->uid2type);
scc_hashtable_drop(&ctx->uid2bblock);
scc_hashtable_drop(&ctx->uid2func);
}
// 辅助宏:创建实体并添加到哈希表
#define CREATE_ENTITY(ctx, vec, uid, data, hashtable) \
do { \
/* 分配新UID */ \
unsigned new_uid = (ctx)->uid++; \
/* 添加到向量 */ \
scc_vec_push((vec), *(data)); \
/* 添加到哈希表 */ \
scc_hashtable_set(&(ctx)->hashtable, (const void *)(usize)new_uid, \
(void *)(usize)(scc_vec_size(vec) - 1)); \
return new_uid; \
} while (0)
scc_ir_type_ref_t scc_ir_module_add_type(scc_ir_module_t *ctx,
const scc_ir_type_t *type) {
CREATE_ENTITY(ctx, ctx->types, type_uid, type, uid2type);
}
scc_ir_value_ref_t scc_ir_module_add_value(scc_ir_module_t *ctx,
const scc_ir_value_t *node) {
CREATE_ENTITY(ctx, ctx->values, value_uid, node, uid2value);
}
scc_ir_bblock_ref_t scc_ir_module_add_bblock(scc_ir_module_t *ctx,
const scc_ir_bblock_t *bblock) {
CREATE_ENTITY(ctx, ctx->bblocks, bblock_uid, bblock, uid2bblock);
}
scc_ir_func_ref_t scc_ir_module_add_func(scc_ir_module_t *ctx,
const scc_ir_func_t *func) {
CREATE_ENTITY(ctx, ctx->funcs, func_uid, func, uid2func);
}
// 辅助宏:从哈希表获取索引
#define GET_ENTITY_INDEX(ctx, ref, hashtable) \
((usize)scc_hashtable_get(&(ctx)->hashtable, (void *)(usize)ref))
scc_ir_type_t *scc_ir_module_get_type(scc_ir_module_t *ctx,
scc_ir_type_ref_t ref) {
if (ref == 0)
return null;
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2type);
if (idx >= ctx->types.size)
return null;
return &ctx->types.data[idx];
}
scc_ir_value_t *scc_ir_module_get_value(scc_ir_module_t *ctx,
scc_ir_value_ref_t ref) {
if (ref == 0)
return null;
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2value);
if (idx >= ctx->values.size)
return null;
return &ctx->values.data[idx];
}
scc_ir_bblock_t *scc_ir_module_get_bblock(scc_ir_module_t *ctx,
scc_ir_bblock_ref_t ref) {
if (ref == 0)
return null;
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2bblock);
if (idx >= ctx->bblocks.size)
return null;
return &ctx->bblocks.data[idx];
}
scc_ir_func_t *scc_ir_module_get_func(scc_ir_module_t *ctx,
scc_ir_func_ref_t ref) {
if (ref == 0)
return null;
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2func);
if (idx >= ctx->funcs.size)
return null;
return &ctx->funcs.data[idx];
}

View File

@@ -13,139 +13,3 @@ void scc_ir_cprog_drop(scc_ir_cprog_t *in) {
scc_vec_free(in->global_vals);
scc_ir_module_drop(&in->module);
}
static u32 hash_key(const void *key) { return (u32)(usize)key; }
static int cmp_key(const void *key1, const void *key2) {
return (u32)(usize)key1 != (u32)(usize)key2;
}
void scc_ir_module_init(scc_ir_module_t *ctx) {
scc_vec_init(ctx->nodes);
scc_vec_init(ctx->types);
scc_vec_init(ctx->bblocks);
scc_vec_init(ctx->funcs);
scc_hashtable_init(&ctx->uid2nodes, hash_key, cmp_key);
scc_hashtable_init(&ctx->uid2types, hash_key, cmp_key);
scc_hashtable_init(&ctx->uid2bblocks, hash_key, cmp_key);
scc_hashtable_init(&ctx->uid2funcs, hash_key, cmp_key);
// 预留UID 0 作为无效引用
ctx->node_uid = 1;
ctx->type_uid = 1;
ctx->bblock_uid = 1;
ctx->func_uid = 1;
}
void scc_ir_module_drop(scc_ir_module_t *ctx) {
// 释放所有实体的内部内存
for (usize i = 0; i < ctx->nodes.size; i++) {
scc_ir_node_t *node = &ctx->nodes.data[i];
scc_vec_free(node->used_by);
if (node->tag == SCC_IR_NODE_CALL) {
scc_vec_free(node->data.call.args);
}
}
for (usize i = 0; i < ctx->types.size; i++) {
scc_ir_type_t *type = &ctx->types.data[i];
if (type->tag == SCC_IR_TYPE_FUNC) {
scc_vec_free(type->data.function.params);
}
}
for (usize i = 0; i < ctx->bblocks.size; i++) {
scc_ir_bblock_t *bblock = &ctx->bblocks.data[i];
scc_vec_free(bblock->instrs);
}
for (usize i = 0; i < ctx->funcs.size; i++) {
scc_ir_func_t *func = &ctx->funcs.data[i];
scc_vec_free(func->params);
scc_vec_free(func->bblocks);
}
scc_vec_free(ctx->nodes);
scc_vec_free(ctx->types);
scc_vec_free(ctx->bblocks);
scc_vec_free(ctx->funcs);
scc_hashtable_drop(&ctx->uid2nodes);
scc_hashtable_drop(&ctx->uid2types);
scc_hashtable_drop(&ctx->uid2bblocks);
scc_hashtable_drop(&ctx->uid2funcs);
}
// 辅助宏:创建实体并添加到哈希表
#define CREATE_ENTITY(ctx, vec, uid, data, hashtable) \
do { \
/* 分配新UID */ \
unsigned new_uid = (ctx)->uid++; \
/* 添加到向量 */ \
scc_vec_push((vec), *(data)); \
/* 添加到哈希表 */ \
scc_hashtable_set(&(ctx)->hashtable, (const void *)(usize)new_uid, \
(void *)(usize)(scc_vec_size(vec) - 1)); \
return new_uid; \
} while (0)
scc_ir_type_ref_t scc_ir_module_add_type(scc_ir_module_t *ctx,
const scc_ir_type_t *type) {
CREATE_ENTITY(ctx, ctx->types, type_uid, type, uid2types);
}
scc_ir_node_ref_t scc_ir_module_add_node(scc_ir_module_t *ctx,
const scc_ir_node_t *node) {
CREATE_ENTITY(ctx, ctx->nodes, node_uid, node, uid2nodes);
}
scc_ir_bblock_ref_t scc_ir_module_add_bblock(scc_ir_module_t *ctx,
const scc_ir_bblock_t *bblock) {
CREATE_ENTITY(ctx, ctx->bblocks, bblock_uid, bblock, uid2bblocks);
}
scc_ir_func_ref_t scc_ir_module_add_func(scc_ir_module_t *ctx,
const scc_ir_func_t *func) {
CREATE_ENTITY(ctx, ctx->funcs, func_uid, func, uid2funcs);
}
// 辅助宏:从哈希表获取索引
#define GET_ENTITY_INDEX(ctx, ref, hashtable) \
((usize)scc_hashtable_get(&(ctx)->hashtable, (void *)(usize)ref))
scc_ir_type_t *scc_ir_module_get_type(scc_ir_module_t *ctx,
scc_ir_type_ref_t ref) {
if (ref == 0)
return null;
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2types);
if (idx >= ctx->types.size)
return null;
return &ctx->types.data[idx];
}
scc_ir_node_t *scc_ir_module_get_node(scc_ir_module_t *ctx,
scc_ir_node_ref_t ref) {
if (ref == 0)
return null;
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2nodes);
if (idx >= ctx->nodes.size)
return null;
return &ctx->nodes.data[idx];
}
scc_ir_bblock_t *scc_ir_module_get_bblock(scc_ir_module_t *ctx,
scc_ir_bblock_ref_t ref) {
if (ref == 0)
return null;
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2bblocks);
if (idx >= ctx->bblocks.size)
return null;
return &ctx->bblocks.data[idx];
}
scc_ir_func_t *scc_ir_module_get_func(scc_ir_module_t *ctx,
scc_ir_func_ref_t ref) {
if (ref == 0)
return null;
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2funcs);
if (idx >= ctx->funcs.size)
return null;
return &ctx->funcs.data[idx];
}

View File

@@ -55,8 +55,8 @@ void scc_ir_func_init(scc_ir_func_t *in, const char *name) {
scc_vec_init(in->params);
}
void scc_ir_node_init(scc_ir_node_t *in, const char *name,
scc_ir_node_tag_t tag) {
void scc_ir_node_init(scc_ir_value_t *in, const char *name,
scc_ir_value_tag_t tag) {
Assert(in != null);
in->name = name;
in->tag = tag;
@@ -64,44 +64,44 @@ void scc_ir_node_init(scc_ir_node_t *in, const char *name,
in->type = 0;
switch (tag) {
case SCC_IR_NODE_NULL:
case SCC_IR_VALUE_TAG_NULL:
break;
case SCC_IR_NODE_CONST_INT:
case SCC_IR_VALUE_TAG_CONST_INT:
// TODO
in->data.const_int.int64 = 0;
break;
case SCC_IR_NODE_ALLOC:
case SCC_IR_VALUE_TAG_ALLOC:
// TODO();
break;
case SCC_IR_NODE_LOAD:
case SCC_IR_VALUE_TAG_LOAD:
in->data.load.target = 0;
break;
case SCC_IR_NODE_STORE:
case SCC_IR_VALUE_TAG_STORE:
in->data.store.target = 0;
in->data.store.value = 0;
break;
case SCC_IR_NODE_GET_PTR:
case SCC_IR_VALUE_TAG_GET_PTR:
in->data.get_ptr.src_addr = 0;
in->data.get_ptr.index = 0;
break;
case SCC_IR_NODE_OP:
case SCC_IR_VALUE_TAG_OP:
in->data.op.op = SCC_IR_OP_EMPTY;
in->data.op.lhs = 0;
in->data.op.rhs = 0;
break;
case SCC_IR_NODE_BRANCH:
case SCC_IR_VALUE_TAG_BRANCH:
in->data.branch.cond = 0;
in->data.branch.true_bblock = 0;
in->data.branch.false_bblock = 0;
break;
case SCC_IR_NODE_JUMP:
case SCC_IR_VALUE_TAG_JUMP:
in->data.jump.target_bblock = 0;
break;
case SCC_IR_NODE_CALL:
case SCC_IR_VALUE_TAG_CALL:
scc_vec_init(in->data.call.args);
in->data.call.callee = 0;
break;
case SCC_IR_NODE_RET:
case SCC_IR_VALUE_TAG_RET:
in->data.ret.ret_val = 0;
break;
default:

View File

@@ -6,9 +6,9 @@
#define GET_MODULE(ctx) (&(ctx->cprog->module))
static void parse_location(scc_ir2mcode_ctx_t *ctx, scc_reg_loc_t *loc,
scc_ir_node_ref_t node_ref) {
scc_ir_value_ref_t node_ref) {
Assert(ctx != null && loc != null);
scc_ir_node_t *node = scc_ir_module_get_node(GET_MODULE(ctx), node_ref);
scc_ir_value_t *node = scc_ir_module_get_value(GET_MODULE(ctx), node_ref);
if (node == null) {
LOG_FATAL("invalid node ref");
UNREACHABLE();
@@ -16,7 +16,7 @@ static void parse_location(scc_ir2mcode_ctx_t *ctx, scc_reg_loc_t *loc,
}
usize idx = 0;
switch (node->tag) {
case SCC_IR_NODE_CONST_INT:
case SCC_IR_VALUE_TAG_CONST_INT:
scc_ir_type_t *type =
scc_ir_module_get_type(GET_MODULE(ctx), node->type);
Assert(type != 0);
@@ -26,11 +26,14 @@ static void parse_location(scc_ir2mcode_ctx_t *ctx, scc_reg_loc_t *loc,
.idx = (usize)node->data.const_int.int32,
};
return;
case SCC_IR_NODE_CONST_UINT:
case SCC_IR_NODE_CONST_FLOAT:
case SCC_IR_VALUE_TAG_CONST_UINT:
case SCC_IR_VALUE_TAG_CONST_FLOAT:
TODO();
break;
case SCC_IR_NODE_FUNC_ARG_REF: {
case SCC_IR_VALUE_TAG_GLOBAL_ALLOC:
TODO();
break;
case SCC_IR_VALUE_TAG_FUNC_ARG_REF: {
scc_ir_type_t *type =
scc_ir_module_get_type(GET_MODULE(ctx), node->type);
Assert(type != 0);
@@ -97,34 +100,34 @@ static void store_value_from_reg(scc_mcode_t *mcode, scc_reg_loc_t *loc,
}
// 临时存储待修补条目
typedef struct {
typedef struct patch {
usize pos;
usize target_bb_ref;
} patch_t;
typedef SCC_VEC(patch_t) patch_vec_t;
static void parse_node(scc_ir2mcode_ctx_t *ctx, scc_ir_bblock_ref_t node_ref,
patch_vec_t *patches) {
scc_ir_node_t *node = scc_ir_module_get_node(GET_MODULE(ctx), node_ref);
static void parse_value(scc_ir2mcode_ctx_t *ctx, scc_ir_bblock_ref_t node_ref,
patch_vec_t *patches) {
scc_ir_value_t *node = scc_ir_module_get_value(GET_MODULE(ctx), node_ref);
if (node == null) {
LOG_ERROR("invalid node ref");
return;
}
switch (node->tag) {
case SCC_IR_NODE_CONV: ///< 类型转换
case SCC_IR_VALUE_TAG_CONV: ///< 类型转换
LOG_FATAL("Unsupported node type: %d", node->tag);
break;
///< 函数参数引用
case SCC_IR_NODE_FUNC_ARG_REF:
case SCC_IR_VALUE_TAG_FUNC_ARG_REF:
///< ABI
break;
case SCC_IR_NODE_BLOCK_ARG_REF: ///< 基本块参数引用
case SCC_IR_NODE_ALLOC: ///< 分配内存(stack)
case SCC_IR_NODE_GLOBAL_ALLOC: ///< 全局分配(bss)
case SCC_IR_VALUE_TAG_BLOCK_ARG_REF: ///< 基本块参数引用
case SCC_IR_VALUE_TAG_ALLOC: ///< 分配内存(stack)
case SCC_IR_VALUE_TAG_GLOBAL_ALLOC: ///< 全局分配(bss)
break;
case SCC_IR_NODE_LOAD: ///< 加载数据
{
///< 加载数据
case SCC_IR_VALUE_TAG_LOAD: {
// node->data.load.target
scc_reg_loc_t from;
scc_reg_loc_t to;
@@ -134,8 +137,8 @@ static void parse_node(scc_ir2mcode_ctx_t *ctx, scc_ir_bblock_ref_t node_ref,
store_value_from_reg(&ctx->sect_mcode, &to, SCC_AMD64_RAX);
break;
}
case SCC_IR_NODE_STORE: ///< 存储数据
{
///< 存储数据
case SCC_IR_VALUE_TAG_STORE: {
scc_reg_loc_t from;
scc_reg_loc_t to;
parse_location(ctx, &from, node->data.store.value);
@@ -144,11 +147,36 @@ static void parse_node(scc_ir2mcode_ctx_t *ctx, scc_ir_bblock_ref_t node_ref,
store_value_from_reg(&ctx->sect_mcode, &to, SCC_AMD64_RAX);
break;
}
case SCC_IR_NODE_GET_PTR: ///< 获取指针
case SCC_IR_NODE_GET_ELEM_PTR: ///< 获取元素指针(used by array)
///< 获取指针
case SCC_IR_VALUE_TAG_GET_PTR: {
scc_reg_loc_t loc;
scc_ir_value_t *src_addr = scc_ir_module_get_value(
GET_MODULE(ctx), node->data.get_ptr.src_addr);
Assert(src_addr != null);
if (src_addr->tag != SCC_IR_VALUE_TAG_GLOBAL_ALLOC) {
Panic();
}
scc_mcode_amd64_lea_r64_rip_rel32(&ctx->sect_mcode, SCC_AMD64_RAX, 0);
usize sym_idx =
sccf_builder_get_symbol_idx(ctx->builder, src_addr->name);
Assert(sym_idx != 0);
sccf_builder_add_reloc(
ctx->builder, (sccf_reloc_t){
.reloc_type = SCCF_RELOC_TYPE_REL,
.offset = scc_vec_size(ctx->sect_mcode.mcode) - 4,
.addend = 4,
.sect_type = SCCF_SECT_DATA,
.sym_idx = sym_idx,
});
parse_location(ctx, &loc, node_ref);
store_value_from_reg(&ctx->sect_mcode, &loc, SCC_AMD64_RAX);
break;
}
case SCC_IR_VALUE_TAG_GET_ELEM_PTR: ///< 获取元素指针(used by array)
TODO();
///< 二元运算
case SCC_IR_NODE_OP: {
case SCC_IR_VALUE_TAG_OP: {
scc_reg_loc_t loc_lhs;
parse_location(ctx, &loc_lhs, node->data.op.lhs);
scc_reg_loc_t loc_rhs;
@@ -239,7 +267,7 @@ static void parse_node(scc_ir2mcode_ctx_t *ctx, scc_ir_bblock_ref_t node_ref,
break;
}
///< 有条件分支
case SCC_IR_NODE_BRANCH: {
case SCC_IR_VALUE_TAG_BRANCH: {
scc_reg_loc_t loc;
parse_location(ctx, &loc, node->data.branch.cond);
// (void)loc;
@@ -259,7 +287,7 @@ static void parse_node(scc_ir2mcode_ctx_t *ctx, scc_ir_bblock_ref_t node_ref,
break;
}
///< 无条件跳转
case SCC_IR_NODE_JUMP: {
case SCC_IR_VALUE_TAG_JUMP: {
scc_mcode_amd64_jmp_rel32(&ctx->sect_mcode, 0);
usize pos = scc_vec_size(ctx->sect_mcode.mcode);
patch_t patch = {.pos = pos,
@@ -268,7 +296,7 @@ static void parse_node(scc_ir2mcode_ctx_t *ctx, scc_ir_bblock_ref_t node_ref,
break;
}
///< 调用函数
case SCC_IR_NODE_CALL: {
case SCC_IR_VALUE_TAG_CALL: {
scc_reg_loc_t loc;
/*
ABI
@@ -326,7 +354,7 @@ static void parse_node(scc_ir2mcode_ctx_t *ctx, scc_ir_bblock_ref_t node_ref,
break;
}
///< 函数返回
case SCC_IR_NODE_RET: {
case SCC_IR_VALUE_TAG_RET: {
if (node->data.ret.ret_val) {
scc_reg_loc_t loc;
parse_location(ctx, &loc, node->data.ret.ret_val);
@@ -349,8 +377,8 @@ static void parse_bblock(scc_ir2mcode_ctx_t *ctx, scc_ir_bblock_t *bblock,
// 打印基本块中的每条指令
for (usize i = 0; i < scc_vec_size(bblock->instrs); i++) {
scc_ir_node_ref_t node_ref = scc_vec_at(bblock->instrs, i);
parse_node(ctx, node_ref, patches);
scc_ir_value_ref_t node_ref = scc_vec_at(bblock->instrs, i);
parse_value(ctx, node_ref, patches);
}
}
@@ -385,9 +413,9 @@ static void parse_function(scc_ir2mcode_ctx_t *ctx, scc_ir_func_t *func) {
SCC_AMD64_RSP, ctx->stack_size);
scc_reg_loc_t loc;
scc_vec_foreach(func->params, i) {
// scc_ir_node_t *param =
// scc_ir_module_get_node(GET_MODULE(ctx), );
scc_ir_node_ref_t node_ref = scc_vec_at(func->params, i);
// scc_ir_value_t *param =
// scc_ir_module_get_value(GET_MODULE(ctx), );
scc_ir_value_ref_t node_ref = scc_vec_at(func->params, i);
parse_location(ctx, &loc, node_ref);
if (i == 0) {
store_value_from_reg(&ctx->sect_mcode, &loc, SCC_AMD64_RCX);
@@ -438,8 +466,36 @@ void scc_ir2amd64(scc_ir2mcode_ctx_t *ctx) {
scc_reg_alloc_init(&ctx->reg_alloc, scc_reg_alloc_with_stack,
GET_MODULE(ctx));
sccf_sect_data_t data_section;
scc_vec_init(data_section);
scc_vec_foreach(ctx->cprog->global_vals, i) {
scc_ir_value_t *galloc = scc_ir_module_get_value(
GET_MODULE(ctx), scc_vec_at(ctx->cprog->global_vals, i));
Assert(galloc->tag == SCC_IR_VALUE_TAG_GLOBAL_ALLOC);
scc_ir_value_t *value = scc_ir_module_get_value(
GET_MODULE(ctx), galloc->data.global_alloc.value);
Assert(value != null);
sccf_sym_t sym = (sccf_sym_t){
.sccf_sect_offset = scc_vec_size(ctx->sect_data),
.sccf_sect_type = SCCF_SECT_DATA,
.sccf_sym_bind =
galloc->name ? SCCF_SYM_BIND_GLOBAL : SCCF_SYM_BIND_LOCAL,
.sccf_sym_size =
4, // FIXME on windows using rel32, on linux using ?
.sccf_sym_type = SCCF_SYM_TYPE_DATA,
.sccf_sym_vis = SCCF_SYM_VIS_DEFAULT,
};
scc_vec_foreach(value->data.const_array.elements, j) {
scc_vec_push(data_section,
scc_vec_at(value->data.const_array.elements, j));
}
usize sym_idx =
sccf_builder_add_symbol(ctx->builder, galloc->name, &sym);
Assert(sym_idx != 0);
}
scc_vec_foreach(ctx->cprog->func_decls, i) {
scc_ir_node_ref_t func_ref = scc_vec_at(ctx->cprog->func_decls, i);
scc_ir_value_ref_t func_ref = scc_vec_at(ctx->cprog->func_decls, i);
scc_ir_func_t *func = scc_ir_module_get_func(GET_MODULE(ctx), func_ref);
if (!func) {
LOG_ERROR("invalid function reference");
@@ -471,7 +527,7 @@ void scc_ir2amd64(scc_ir2mcode_ctx_t *ctx) {
}
scc_vec_foreach(ctx->cprog->func_defs, i) {
scc_ir_node_ref_t func_ref = scc_vec_at(ctx->cprog->func_defs, i);
scc_ir_value_ref_t func_ref = scc_vec_at(ctx->cprog->func_defs, i);
scc_ir_func_t *func = scc_ir_module_get_func(GET_MODULE(ctx), func_ref);
if (!func) {
LOG_ERROR("invalid function reference");
@@ -494,15 +550,16 @@ void scc_ir2amd64(scc_ir2mcode_ctx_t *ctx) {
if (sym->sccf_sym_type != SCCF_SYM_TYPE_EXTERN) {
Assert(reloc->reloc_type == SCCF_RELOC_TYPE_REL);
Assert(sym->sccf_sect_type == SCCF_SECT_CODE);
Assert(sym->sccf_sym_type == SCCF_SYM_TYPE_FUNC);
i64 target_off = sym->sccf_sect_offset;
i64 next_off = reloc->offset + reloc->addend;
i32 rel = (i32)(target_off - next_off);
// FIXME 写入到指令的偏移字段(小端)
*(i32 *)(&buf[reloc->offset]) = rel;
if (sym->sccf_sect_type == SCCF_SECT_CODE &&
sym->sccf_sym_type == SCCF_SYM_TYPE_FUNC) {
i64 target_off = sym->sccf_sect_offset;
i64 next_off = reloc->offset + reloc->addend;
i32 rel = (i32)(target_off - next_off);
// FIXME 写入到指令的偏移字段(小端)
*(i32 *)(&buf[reloc->offset]) = rel;
reloc->reloc_type = SCCF_RELOC_TYPE_EMPTY;
reloc->reloc_type = SCCF_RELOC_TYPE_EMPTY;
}
}
}
@@ -511,8 +568,7 @@ void scc_ir2amd64(scc_ir2mcode_ctx_t *ctx) {
scc_vec_unsafe_get_data(ctx->sect_mcode.mcode),
scc_vec_size(ctx->sect_mcode.mcode));
sccf_builder_add_text_section(ctx->builder, &text_section);
sccf_sect_data_t data_section;
scc_vec_init(data_section);
sccf_builder_add_data_section(ctx->builder, &data_section);
// FIXME
ctx->builder->entry_symbol_name = "main";

View File

@@ -26,7 +26,7 @@ scc_hashtable_t *scc_reg_alloc_with_stack(scc_reg_alloc_t *ctx,
scc_reg_loc_t loc;
scc_vec_foreach(func->params, i) {
scc_ir_node_ref_t node_ref = scc_vec_at(func->params, i);
scc_ir_value_ref_t node_ref = scc_vec_at(func->params, i);
loc.kind = SCC_REG_KIND_FUNC_ARG;
loc.idx = i;
ctx->alloc_stack_size += 8;
@@ -40,16 +40,17 @@ scc_hashtable_t *scc_reg_alloc_with_stack(scc_reg_alloc_t *ctx,
scc_ir_module_get_bblock(ctx->ir_module, bblock_ref);
Assert(bblock != null);
scc_vec_foreach(bblock->instrs, j) {
scc_ir_node_ref_t node_ref = scc_vec_at(bblock->instrs, j);
scc_ir_node_t *node =
scc_ir_module_get_node(ctx->ir_module, node_ref);
scc_ir_value_ref_t node_ref = scc_vec_at(bblock->instrs, j);
scc_ir_value_t *node =
scc_ir_module_get_value(ctx->ir_module, node_ref);
Assert(node != null);
loc.kind = SCC_REG_KIND_UNDEF;
switch (node->tag) {
case SCC_IR_NODE_LOAD:
case SCC_IR_NODE_OP:
case SCC_IR_NODE_ALLOC: {
case SCC_IR_VALUE_TAG_LOAD:
case SCC_IR_VALUE_TAG_OP:
case SCC_IR_VALUE_TAG_GET_PTR:
case SCC_IR_VALUE_TAG_GET_ELEM_PTR:
case SCC_IR_VALUE_TAG_ALLOC: {
loc.kind = SCC_REG_KIND_STACK;
loc.idx = ctx->alloc_stack_size;
@@ -60,7 +61,7 @@ scc_hashtable_t *scc_reg_alloc_with_stack(scc_reg_alloc_t *ctx,
(void *)scc_vec_size(ctx->reg_loc_vec));
break;
}
case SCC_IR_NODE_CALL: {
case SCC_IR_VALUE_TAG_CALL: {
// 处理返回值
scc_ir_type_t *func_type =
scc_ir_module_get_type(ctx->ir_module, func->type);

View File

@@ -52,6 +52,8 @@
#define SCC_AMD64_COND_LE 14 // less or equal (signed)
#define SCC_AMD64_COND_G 15 // greater (signed)
#define SCC_MCODE_FUNC [[maybe_unused]] static inline
// ==================== REX 前缀辅助 ====================
/**
* @brief 生成 REX 前缀(若不为 0x40 则写入)
@@ -62,8 +64,8 @@
* @param x REX.X 位
* @param b REX.B 位
*/
static inline void scc_mcode_amd64_rex(scc_mcode_t *mcode, u8 w, u8 r, u8 x,
u8 b) {
SCC_MCODE_FUNC void scc_mcode_amd64_rex(scc_mcode_t *mcode, u8 w, u8 r, u8 x,
u8 b) {
u8 rex = 0x40 | (w << 3) | (r << 2) | (x << 1) | b;
if (rex != 0x40)
scc_mcode_add_u8(mcode, rex);
@@ -82,9 +84,9 @@ static inline void scc_mcode_amd64_rex(scc_mcode_t *mcode, u8 w, u8 r, u8 x,
* @param p_src_low 输出 src 的低 3 位
* @return 是否需要写入 REX 前缀0 表示不需要)
*/
static inline int scc_mcode_amd64_prepare_rex(int dst_reg, int src_reg,
int use_w, u8 *p_rex,
u8 *p_dst_low, u8 *p_src_low) {
SCC_MCODE_FUNC int scc_mcode_amd64_prepare_rex(int dst_reg, int src_reg,
int use_w, u8 *p_rex,
u8 *p_dst_low, u8 *p_src_low) {
u8 rex = 0x40;
if (use_w)
rex |= 0x08;
@@ -105,8 +107,8 @@ static inline int scc_mcode_amd64_prepare_rex(int dst_reg, int src_reg,
* @param mcode 机器码缓冲区
* @param width 宽度1=8位2=16位4=32位8=64位
*/
static inline void scc_mcode_amd64_emit_width_prefix(scc_mcode_t *mcode,
int width) {
SCC_MCODE_FUNC void scc_mcode_amd64_emit_width_prefix(scc_mcode_t *mcode,
int width) {
if (width == 2) {
scc_mcode_add_u8(mcode, 0x66);
}
@@ -121,7 +123,7 @@ static inline void scc_mcode_amd64_emit_width_prefix(scc_mcode_t *mcode,
* @param mcode 机器码缓冲区
* @param reg 寄存器编号
*/
static inline void scc_mcode_amd64_push_r64(scc_mcode_t *mcode, int reg) {
SCC_MCODE_FUNC void scc_mcode_amd64_push_r64(scc_mcode_t *mcode, int reg) {
if (reg < 8) {
scc_mcode_add_u8(mcode, 0x50 + reg);
} else {
@@ -136,7 +138,7 @@ static inline void scc_mcode_amd64_push_r64(scc_mcode_t *mcode, int reg) {
* @param mcode 机器码缓冲区
* @param reg 寄存器编号
*/
static inline void scc_mcode_amd64_pop_r64(scc_mcode_t *mcode, int reg) {
SCC_MCODE_FUNC void scc_mcode_amd64_pop_r64(scc_mcode_t *mcode, int reg) {
if (reg < 8) {
scc_mcode_add_u8(mcode, 0x58 + reg);
} else {
@@ -151,7 +153,7 @@ static inline void scc_mcode_amd64_pop_r64(scc_mcode_t *mcode, int reg) {
* @param mcode 机器码缓冲区
* @param imm 32 位立即数
*/
static inline void scc_mcode_amd64_push_imm32(scc_mcode_t *mcode, u32 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_push_imm32(scc_mcode_t *mcode, u32 imm) {
scc_mcode_add_u8(mcode, 0x68);
scc_mcode_add_u32(mcode, imm);
}
@@ -167,8 +169,8 @@ static inline void scc_mcode_amd64_push_imm32(scc_mcode_t *mcode, u32 imm) {
* @param reg 目标寄存器
* @param imm 64 位立即数
*/
static inline void scc_mcode_amd64_mov_r64_imm64(scc_mcode_t *mcode, int reg,
u64 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_r64_imm64(scc_mcode_t *mcode, int reg,
u64 imm) {
u8 rex = 0x48; // REX.W
if (reg >= 8) {
rex |= 0x01; // REX.B
@@ -186,8 +188,8 @@ static inline void scc_mcode_amd64_mov_r64_imm64(scc_mcode_t *mcode, int reg,
* @param reg 目标寄存器
* @param imm 32 位立即数
*/
static inline void scc_mcode_amd64_mov_r64_imm32(scc_mcode_t *mcode, int reg,
u32 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_r64_imm32(scc_mcode_t *mcode, int reg,
u32 imm) {
u8 rex = 0x40; // REX.W=0
if (reg >= 8) {
rex |= 0x01; // REX.B
@@ -205,8 +207,8 @@ static inline void scc_mcode_amd64_mov_r64_imm32(scc_mcode_t *mcode, int reg,
* @param dst 目标寄存器
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_mov_r64_r64(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_r64_r64(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x48; // REX.W
if (src >= 8)
rex |= 0x04; // REX.R
@@ -225,8 +227,8 @@ static inline void scc_mcode_amd64_mov_r64_r64(scc_mcode_t *mcode, int dst,
* @param base 基址寄存器
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_mov_m64_r64(scc_mcode_t *mcode, int base,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_m64_r64(scc_mcode_t *mcode, int base,
int src) {
u8 rex = 0x48;
if (src >= 8)
rex |= 0x04;
@@ -251,8 +253,8 @@ static inline void scc_mcode_amd64_mov_m64_r64(scc_mcode_t *mcode, int base,
* @param dst 目标寄存器
* @param base 基址寄存器
*/
static inline void scc_mcode_amd64_mov_r64_m64(scc_mcode_t *mcode, int dst,
int base) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_r64_m64(scc_mcode_t *mcode, int dst,
int base) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x04;
@@ -278,9 +280,9 @@ static inline void scc_mcode_amd64_mov_r64_m64(scc_mcode_t *mcode, int dst,
* @param disp 32 位偏移
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_mov_m64_disp32_r64(scc_mcode_t *mcode,
int base, u32 disp,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_m64_disp32_r64(scc_mcode_t *mcode,
int base, u32 disp,
int src) {
u8 rex = 0x48;
if (src >= 8)
rex |= 0x04;
@@ -307,9 +309,9 @@ static inline void scc_mcode_amd64_mov_m64_disp32_r64(scc_mcode_t *mcode,
* @param base 基址寄存器
* @param disp 32 位偏移
*/
static inline void scc_mcode_amd64_mov_r64_m64_disp32(scc_mcode_t *mcode,
int dst, int base,
u32 disp) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_r64_m64_disp32(scc_mcode_t *mcode,
int dst, int base,
u32 disp) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x04;
@@ -328,6 +330,32 @@ static inline void scc_mcode_amd64_mov_r64_m64_disp32(scc_mcode_t *mcode,
scc_mcode_add_u32(mcode, disp);
}
/**
* @brief 加载 RIP 相对地址到 64 位寄存器 (lea r64, [rip+disp32])
*
* @param mcode 机器码缓冲区
* @param dst 目标寄存器 (0-15)
* @param disp 32 位相对偏移量 (有符号)
*/
SCC_MCODE_FUNC void scc_mcode_amd64_lea_r64_rip_rel32(scc_mcode_t *mcode,
int dst, u32 disp) {
// REX.W = 1, 无额外扩展位RIP 不是通用寄存器,不涉及 REX.B/R/X
u8 rex = 0x48;
if (dst >= 8) {
rex |= 0x04; // REX.R 位,因为 dst 放在 reg 字段
dst -= 8;
}
scc_mcode_add_u8(mcode, rex);
scc_mcode_add_u8(mcode, 0x8D); // LEA opcode
// ModRM: mod=00 (无位移), reg=dst, r/m=101 (RIP 相对寻址)
u8 modrm = 0x05 | ((dst & 7) << 3);
scc_mcode_add_u8(mcode, modrm);
// disp32 (有符号)
scc_mcode_add_u32(mcode, (u32)disp);
}
/**
* @brief 加载有效地址到 64 位寄存器 (lea r64, [base+disp32])
*
@@ -336,9 +364,9 @@ static inline void scc_mcode_amd64_mov_r64_m64_disp32(scc_mcode_t *mcode,
* @param base 基址寄存器
* @param disp 32 位偏移
*/
static inline void scc_mcode_amd64_lea_r64_m64_disp32(scc_mcode_t *mcode,
int dst, int base,
u32 disp) {
SCC_MCODE_FUNC void scc_mcode_amd64_lea_r64_m64_disp32(scc_mcode_t *mcode,
int dst, int base,
u32 disp) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x04;
@@ -366,8 +394,8 @@ static inline void scc_mcode_amd64_lea_r64_m64_disp32(scc_mcode_t *mcode,
* @param dst 目标寄存器(低 32 位)
* @param src 源寄存器(低 32 位)
*/
static inline void scc_mcode_amd64_mov_r32_r32(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_r32_r32(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x40; // REX.W=0
if (src >= 8)
rex |= 0x04;
@@ -386,8 +414,8 @@ static inline void scc_mcode_amd64_mov_r32_r32(scc_mcode_t *mcode, int dst,
* @param reg 目标寄存器
* @param imm 32 位立即数
*/
static inline void scc_mcode_amd64_mov_r32_imm32(scc_mcode_t *mcode, int reg,
u32 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_r32_imm32(scc_mcode_t *mcode, int reg,
u32 imm) {
scc_mcode_amd64_mov_r64_imm32(mcode, reg, imm); // 与 64 位 imm32 版本相同
}
@@ -398,8 +426,8 @@ static inline void scc_mcode_amd64_mov_r32_imm32(scc_mcode_t *mcode, int reg,
* @param base 基址寄存器
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_mov_m32_r32(scc_mcode_t *mcode, int base,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_m32_r32(scc_mcode_t *mcode, int base,
int src) {
u8 rex = 0x40; // REX.W=0
if (src >= 8)
rex |= 0x04;
@@ -424,8 +452,8 @@ static inline void scc_mcode_amd64_mov_m32_r32(scc_mcode_t *mcode, int base,
* @param dst 目标寄存器
* @param base 基址寄存器
*/
static inline void scc_mcode_amd64_mov_r32_m32(scc_mcode_t *mcode, int dst,
int base) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_r32_m32(scc_mcode_t *mcode, int dst,
int base) {
u8 rex = 0x40; // REX.W=0
if (dst >= 8)
rex |= 0x04;
@@ -452,8 +480,8 @@ static inline void scc_mcode_amd64_mov_r32_m32(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器(低 16 位)
* @param src 源寄存器(低 16 位)
*/
static inline void scc_mcode_amd64_mov_r16_r16(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_r16_r16(scc_mcode_t *mcode, int dst,
int src) {
scc_mcode_amd64_emit_width_prefix(mcode, 2);
u8 rex = 0x40; // REX.W=0
if (src >= 8)
@@ -473,8 +501,8 @@ static inline void scc_mcode_amd64_mov_r16_r16(scc_mcode_t *mcode, int dst,
* @param reg 目标寄存器
* @param imm 16 位立即数
*/
static inline void scc_mcode_amd64_mov_r16_imm16(scc_mcode_t *mcode, int reg,
u16 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_r16_imm16(scc_mcode_t *mcode, int reg,
u16 imm) {
scc_mcode_amd64_emit_width_prefix(mcode, 2);
u8 rex = 0x40; // REX.W=0
if (reg >= 8) {
@@ -493,8 +521,8 @@ static inline void scc_mcode_amd64_mov_r16_imm16(scc_mcode_t *mcode, int reg,
* @param base 基址寄存器
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_mov_m16_r16(scc_mcode_t *mcode, int base,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_m16_r16(scc_mcode_t *mcode, int base,
int src) {
scc_mcode_amd64_emit_width_prefix(mcode, 2);
u8 rex = 0x40;
if (src >= 8)
@@ -520,8 +548,8 @@ static inline void scc_mcode_amd64_mov_m16_r16(scc_mcode_t *mcode, int base,
* @param dst 目标寄存器
* @param base 基址寄存器
*/
static inline void scc_mcode_amd64_mov_r16_m16(scc_mcode_t *mcode, int dst,
int base) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_r16_m16(scc_mcode_t *mcode, int dst,
int base) {
scc_mcode_amd64_emit_width_prefix(mcode, 2);
u8 rex = 0x40;
if (dst >= 8)
@@ -550,7 +578,7 @@ static inline void scc_mcode_amd64_mov_r16_m16(scc_mcode_t *mcode, int dst,
* @param reg 寄存器编号
* @return 是否需要 REX 前缀
*/
static inline int scc_mcode_amd64_is_byte_reg_needs_rex(int reg) {
SCC_MCODE_FUNC int scc_mcode_amd64_is_byte_reg_needs_rex(int reg) {
return (reg >= 4 && reg <= 7) || reg >= 8;
}
@@ -561,8 +589,8 @@ static inline int scc_mcode_amd64_is_byte_reg_needs_rex(int reg) {
* @param dst 目标寄存器(低 8 位)
* @param src 源寄存器(低 8 位)
*/
static inline void scc_mcode_amd64_mov_r8_r8(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_r8_r8(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x40;
int need_rex = scc_mcode_amd64_is_byte_reg_needs_rex(dst) ||
scc_mcode_amd64_is_byte_reg_needs_rex(src);
@@ -585,8 +613,8 @@ static inline void scc_mcode_amd64_mov_r8_r8(scc_mcode_t *mcode, int dst,
* @param reg 目标寄存器
* @param imm 8 位立即数
*/
static inline void scc_mcode_amd64_mov_r8_imm8(scc_mcode_t *mcode, int reg,
u8 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_r8_imm8(scc_mcode_t *mcode, int reg,
u8 imm) {
u8 rex = 0x40;
int need_rex = scc_mcode_amd64_is_byte_reg_needs_rex(reg);
if (reg >= 8) {
@@ -607,8 +635,8 @@ static inline void scc_mcode_amd64_mov_r8_imm8(scc_mcode_t *mcode, int reg,
* @param base 基址寄存器
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_mov_m8_r8(scc_mcode_t *mcode, int base,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_m8_r8(scc_mcode_t *mcode, int base,
int src) {
u8 rex = 0x40;
int need_rex = scc_mcode_amd64_is_byte_reg_needs_rex(src) ||
scc_mcode_amd64_is_byte_reg_needs_rex(base);
@@ -637,8 +665,8 @@ static inline void scc_mcode_amd64_mov_m8_r8(scc_mcode_t *mcode, int base,
* @param dst 目标寄存器
* @param base 基址寄存器
*/
static inline void scc_mcode_amd64_mov_r8_m8(scc_mcode_t *mcode, int dst,
int base) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_r8_m8(scc_mcode_t *mcode, int dst,
int base) {
u8 rex = 0x40;
int need_rex = scc_mcode_amd64_is_byte_reg_needs_rex(dst) ||
scc_mcode_amd64_is_byte_reg_needs_rex(base);
@@ -669,8 +697,8 @@ static inline void scc_mcode_amd64_mov_r8_m8(scc_mcode_t *mcode, int dst,
* @param dst 64 位目标寄存器
* @param src 8 位源寄存器
*/
static inline void scc_mcode_amd64_movsx_r64_r8(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_movsx_r64_r8(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x48; // REX.W
if (dst >= 8)
rex |= 0x04;
@@ -690,8 +718,8 @@ static inline void scc_mcode_amd64_movsx_r64_r8(scc_mcode_t *mcode, int dst,
* @param dst 64 位目标寄存器
* @param src 16 位源寄存器
*/
static inline void scc_mcode_amd64_movsx_r64_r16(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_movsx_r64_r16(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x04;
@@ -711,8 +739,8 @@ static inline void scc_mcode_amd64_movsx_r64_r16(scc_mcode_t *mcode, int dst,
* @param dst 64 位目标寄存器
* @param src 32 位源寄存器
*/
static inline void scc_mcode_amd64_movsx_r64_r32(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_movsx_r64_r32(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x04;
@@ -731,8 +759,8 @@ static inline void scc_mcode_amd64_movsx_r64_r32(scc_mcode_t *mcode, int dst,
* @param dst 64 位目标寄存器
* @param src 8 位源寄存器
*/
static inline void scc_mcode_amd64_movzx_r64_r8(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_movzx_r64_r8(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x04;
@@ -752,8 +780,8 @@ static inline void scc_mcode_amd64_movzx_r64_r8(scc_mcode_t *mcode, int dst,
* @param dst 64 位目标寄存器
* @param src 16 位源寄存器
*/
static inline void scc_mcode_amd64_movzx_r64_r16(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_movzx_r64_r16(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x04;
@@ -777,8 +805,8 @@ static inline void scc_mcode_amd64_movzx_r64_r16(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_add_r64_r64(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_add_r64_r64(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x48;
if (src >= 8)
rex |= 0x04;
@@ -797,8 +825,8 @@ static inline void scc_mcode_amd64_add_r64_r64(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param imm 32 位立即数
*/
static inline void scc_mcode_amd64_add_r64_imm32(scc_mcode_t *mcode, int dst,
u32 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_add_r64_imm32(scc_mcode_t *mcode, int dst,
u32 imm) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x01;
@@ -816,8 +844,8 @@ static inline void scc_mcode_amd64_add_r64_imm32(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_sub_r64_r64(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_sub_r64_r64(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x48;
if (src >= 8)
rex |= 0x04;
@@ -836,8 +864,8 @@ static inline void scc_mcode_amd64_sub_r64_r64(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param imm 32 位立即数
*/
static inline void scc_mcode_amd64_sub_r64_imm32(scc_mcode_t *mcode, int dst,
u32 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_sub_r64_imm32(scc_mcode_t *mcode, int dst,
u32 imm) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x01;
@@ -854,7 +882,7 @@ static inline void scc_mcode_amd64_sub_r64_imm32(scc_mcode_t *mcode, int dst,
* @param mcode 机器码缓冲区
* @param imm 32 位立即数
*/
static inline void scc_mcode_amd64_sub_rsp_imm32(scc_mcode_t *mcode, u32 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_sub_rsp_imm32(scc_mcode_t *mcode, u32 imm) {
scc_mcode_amd64_sub_r64_imm32(mcode, SCC_AMD64_RSP, imm);
}
@@ -864,7 +892,7 @@ static inline void scc_mcode_amd64_sub_rsp_imm32(scc_mcode_t *mcode, u32 imm) {
* @param mcode 机器码缓冲区
* @param imm 32 位立即数
*/
static inline void scc_mcode_amd64_add_rsp_imm32(scc_mcode_t *mcode, u32 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_add_rsp_imm32(scc_mcode_t *mcode, u32 imm) {
scc_mcode_amd64_add_r64_imm32(mcode, SCC_AMD64_RSP, imm);
}
@@ -875,8 +903,8 @@ static inline void scc_mcode_amd64_add_rsp_imm32(scc_mcode_t *mcode, u32 imm) {
* @param dst 左操作数寄存器
* @param src 右操作数寄存器
*/
static inline void scc_mcode_amd64_cmp_r64_r64(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_cmp_r64_r64(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x48;
if (src >= 8)
rex |= 0x04;
@@ -895,8 +923,8 @@ static inline void scc_mcode_amd64_cmp_r64_r64(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param imm 32 位立即数
*/
static inline void scc_mcode_amd64_cmp_r64_imm32(scc_mcode_t *mcode, int dst,
u32 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_cmp_r64_imm32(scc_mcode_t *mcode, int dst,
u32 imm) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x01;
@@ -914,8 +942,8 @@ static inline void scc_mcode_amd64_cmp_r64_imm32(scc_mcode_t *mcode, int dst,
* @param dst 目标/左操作数寄存器
* @param src 右操作数寄存器
*/
static inline void scc_mcode_amd64_imul_r64_r64(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_imul_r64_r64(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x04; // dst in reg field
@@ -936,9 +964,9 @@ static inline void scc_mcode_amd64_imul_r64_r64(scc_mcode_t *mcode, int dst,
* @param src 源寄存器或内存(此处为寄存器版本)
* @param imm 32 位立即数
*/
static inline void scc_mcode_amd64_imul_r64_r64_imm32(scc_mcode_t *mcode,
int dst, int src,
u32 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_imul_r64_r64_imm32(scc_mcode_t *mcode,
int dst, int src,
u32 imm) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x04;
@@ -960,8 +988,9 @@ static inline void scc_mcode_amd64_imul_r64_r64_imm32(scc_mcode_t *mcode,
* @param src 源寄存器或内存(此处为寄存器版本)
* @param imm 8 位立即数
*/
static inline void scc_mcode_amd64_imul_r64_r64_imm8(scc_mcode_t *mcode,
int dst, int src, u8 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_imul_r64_r64_imm8(scc_mcode_t *mcode,
int dst, int src,
u8 imm) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x04;
@@ -983,8 +1012,8 @@ static inline void scc_mcode_amd64_imul_r64_r64_imm8(scc_mcode_t *mcode,
* @param dst 目标寄存器
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_add_r32_r32(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_add_r32_r32(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x40; // REX.W=0
if (src >= 8)
rex |= 0x04;
@@ -1003,8 +1032,8 @@ static inline void scc_mcode_amd64_add_r32_r32(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param imm 32 位立即数
*/
static inline void scc_mcode_amd64_add_r32_imm32(scc_mcode_t *mcode, int dst,
u32 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_add_r32_imm32(scc_mcode_t *mcode, int dst,
u32 imm) {
u8 rex = 0x40;
if (dst >= 8)
rex |= 0x01;
@@ -1022,8 +1051,8 @@ static inline void scc_mcode_amd64_add_r32_imm32(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_sub_r32_r32(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_sub_r32_r32(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x40;
if (src >= 8)
rex |= 0x04;
@@ -1042,8 +1071,8 @@ static inline void scc_mcode_amd64_sub_r32_r32(scc_mcode_t *mcode, int dst,
* @param dst 左操作数寄存器
* @param src 右操作数寄存器
*/
static inline void scc_mcode_amd64_cmp_r32_r32(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_cmp_r32_r32(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x40;
if (src >= 8)
rex |= 0x04;
@@ -1064,8 +1093,8 @@ static inline void scc_mcode_amd64_cmp_r32_r32(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_add_r16_r16(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_add_r16_r16(scc_mcode_t *mcode, int dst,
int src) {
scc_mcode_amd64_emit_width_prefix(mcode, 2);
u8 rex = 0x40;
if (src >= 8)
@@ -1085,8 +1114,8 @@ static inline void scc_mcode_amd64_add_r16_r16(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param imm 16 位立即数
*/
static inline void scc_mcode_amd64_add_r16_imm16(scc_mcode_t *mcode, int dst,
u16 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_add_r16_imm16(scc_mcode_t *mcode, int dst,
u16 imm) {
scc_mcode_amd64_emit_width_prefix(mcode, 2);
u8 rex = 0x40;
if (dst >= 8)
@@ -1105,8 +1134,8 @@ static inline void scc_mcode_amd64_add_r16_imm16(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_sub_r16_r16(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_sub_r16_r16(scc_mcode_t *mcode, int dst,
int src) {
scc_mcode_amd64_emit_width_prefix(mcode, 2);
u8 rex = 0x40;
if (src >= 8)
@@ -1126,8 +1155,8 @@ static inline void scc_mcode_amd64_sub_r16_r16(scc_mcode_t *mcode, int dst,
* @param dst 左操作数寄存器
* @param src 右操作数寄存器
*/
static inline void scc_mcode_amd64_cmp_r16_r16(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_cmp_r16_r16(scc_mcode_t *mcode, int dst,
int src) {
scc_mcode_amd64_emit_width_prefix(mcode, 2);
u8 rex = 0x40;
if (src >= 8)
@@ -1149,8 +1178,8 @@ static inline void scc_mcode_amd64_cmp_r16_r16(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_add_r8_r8(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_add_r8_r8(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x40;
int need_rex = scc_mcode_amd64_is_byte_reg_needs_rex(dst) ||
scc_mcode_amd64_is_byte_reg_needs_rex(src);
@@ -1173,8 +1202,8 @@ static inline void scc_mcode_amd64_add_r8_r8(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param imm 8 位立即数
*/
static inline void scc_mcode_amd64_add_r8_imm8(scc_mcode_t *mcode, int dst,
u8 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_add_r8_imm8(scc_mcode_t *mcode, int dst,
u8 imm) {
u8 rex = 0x40;
int need_rex = scc_mcode_amd64_is_byte_reg_needs_rex(dst);
if (dst >= 8) {
@@ -1197,8 +1226,8 @@ static inline void scc_mcode_amd64_add_r8_imm8(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_sub_r8_r8(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_sub_r8_r8(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x40;
int need_rex = scc_mcode_amd64_is_byte_reg_needs_rex(dst) ||
scc_mcode_amd64_is_byte_reg_needs_rex(src);
@@ -1221,8 +1250,8 @@ static inline void scc_mcode_amd64_sub_r8_r8(scc_mcode_t *mcode, int dst,
* @param dst 左操作数寄存器
* @param src 右操作数寄存器
*/
static inline void scc_mcode_amd64_cmp_r8_r8(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_cmp_r8_r8(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x40;
int need_rex = scc_mcode_amd64_is_byte_reg_needs_rex(dst) ||
scc_mcode_amd64_is_byte_reg_needs_rex(src);
@@ -1248,7 +1277,7 @@ static inline void scc_mcode_amd64_cmp_r8_r8(scc_mcode_t *mcode, int dst,
* @param mcode 机器码缓冲区
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_mul_r64(scc_mcode_t *mcode, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_mul_r64(scc_mcode_t *mcode, int src) {
u8 rex = 0x48;
if (src >= 8)
rex |= 0x01;
@@ -1264,7 +1293,7 @@ static inline void scc_mcode_amd64_mul_r64(scc_mcode_t *mcode, int src) {
* @param mcode 机器码缓冲区
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_mul_r32(scc_mcode_t *mcode, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_mul_r32(scc_mcode_t *mcode, int src) {
u8 rex = 0x40;
if (src >= 8)
rex |= 0x01;
@@ -1280,7 +1309,7 @@ static inline void scc_mcode_amd64_mul_r32(scc_mcode_t *mcode, int src) {
* @param mcode 机器码缓冲区
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_mul_r16(scc_mcode_t *mcode, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_mul_r16(scc_mcode_t *mcode, int src) {
scc_mcode_amd64_emit_width_prefix(mcode, 2);
u8 rex = 0x40;
if (src >= 8)
@@ -1297,7 +1326,7 @@ static inline void scc_mcode_amd64_mul_r16(scc_mcode_t *mcode, int src) {
* @param mcode 机器码缓冲区
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_mul_r8(scc_mcode_t *mcode, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_mul_r8(scc_mcode_t *mcode, int src) {
u8 rex = 0x40;
int need_rex = scc_mcode_amd64_is_byte_reg_needs_rex(src);
if (src >= 8)
@@ -1318,7 +1347,7 @@ static inline void scc_mcode_amd64_mul_r8(scc_mcode_t *mcode, int src) {
* @param mcode 机器码缓冲区
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_imul_r64_1op(scc_mcode_t *mcode, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_imul_r64_1op(scc_mcode_t *mcode, int src) {
u8 rex = 0x48;
if (src >= 8)
rex |= 0x01;
@@ -1334,7 +1363,7 @@ static inline void scc_mcode_amd64_imul_r64_1op(scc_mcode_t *mcode, int src) {
* @param mcode 机器码缓冲区
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_imul_r32_1op(scc_mcode_t *mcode, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_imul_r32_1op(scc_mcode_t *mcode, int src) {
u8 rex = 0x40;
if (src >= 8)
rex |= 0x01;
@@ -1350,7 +1379,7 @@ static inline void scc_mcode_amd64_imul_r32_1op(scc_mcode_t *mcode, int src) {
* @param mcode 机器码缓冲区
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_imul_r16_1op(scc_mcode_t *mcode, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_imul_r16_1op(scc_mcode_t *mcode, int src) {
scc_mcode_amd64_emit_width_prefix(mcode, 2);
u8 rex = 0x40;
if (src >= 8)
@@ -1367,7 +1396,7 @@ static inline void scc_mcode_amd64_imul_r16_1op(scc_mcode_t *mcode, int src) {
* @param mcode 机器码缓冲区
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_imul_r8_1op(scc_mcode_t *mcode, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_imul_r8_1op(scc_mcode_t *mcode, int src) {
u8 rex = 0x40;
int need_rex = scc_mcode_amd64_is_byte_reg_needs_rex(src);
if (src >= 8)
@@ -1388,7 +1417,7 @@ static inline void scc_mcode_amd64_imul_r8_1op(scc_mcode_t *mcode, int src) {
* @param mcode 机器码缓冲区
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_div_r64(scc_mcode_t *mcode, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_div_r64(scc_mcode_t *mcode, int src) {
u8 rex = 0x48;
if (src >= 8)
rex |= 0x01;
@@ -1404,7 +1433,7 @@ static inline void scc_mcode_amd64_div_r64(scc_mcode_t *mcode, int src) {
* @param mcode 机器码缓冲区
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_div_r32(scc_mcode_t *mcode, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_div_r32(scc_mcode_t *mcode, int src) {
u8 rex = 0x40;
if (src >= 8)
rex |= 0x01;
@@ -1420,7 +1449,7 @@ static inline void scc_mcode_amd64_div_r32(scc_mcode_t *mcode, int src) {
* @param mcode 机器码缓冲区
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_div_r16(scc_mcode_t *mcode, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_div_r16(scc_mcode_t *mcode, int src) {
scc_mcode_amd64_emit_width_prefix(mcode, 2);
u8 rex = 0x40;
if (src >= 8)
@@ -1437,7 +1466,7 @@ static inline void scc_mcode_amd64_div_r16(scc_mcode_t *mcode, int src) {
* @param mcode 机器码缓冲区
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_div_r8(scc_mcode_t *mcode, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_div_r8(scc_mcode_t *mcode, int src) {
u8 rex = 0x40;
int need_rex = scc_mcode_amd64_is_byte_reg_needs_rex(src);
if (src >= 8)
@@ -1458,7 +1487,7 @@ static inline void scc_mcode_amd64_div_r8(scc_mcode_t *mcode, int src) {
* @param mcode 机器码缓冲区
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_idiv_r64(scc_mcode_t *mcode, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_idiv_r64(scc_mcode_t *mcode, int src) {
u8 rex = 0x48;
if (src >= 8)
rex |= 0x01;
@@ -1474,7 +1503,7 @@ static inline void scc_mcode_amd64_idiv_r64(scc_mcode_t *mcode, int src) {
* @param mcode 机器码缓冲区
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_idiv_r32(scc_mcode_t *mcode, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_idiv_r32(scc_mcode_t *mcode, int src) {
u8 rex = 0x40;
if (src >= 8)
rex |= 0x01;
@@ -1490,7 +1519,7 @@ static inline void scc_mcode_amd64_idiv_r32(scc_mcode_t *mcode, int src) {
* @param mcode 机器码缓冲区
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_idiv_r16(scc_mcode_t *mcode, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_idiv_r16(scc_mcode_t *mcode, int src) {
scc_mcode_amd64_emit_width_prefix(mcode, 2);
u8 rex = 0x40;
if (src >= 8)
@@ -1507,7 +1536,7 @@ static inline void scc_mcode_amd64_idiv_r16(scc_mcode_t *mcode, int src) {
* @param mcode 机器码缓冲区
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_idiv_r8(scc_mcode_t *mcode, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_idiv_r8(scc_mcode_t *mcode, int src) {
u8 rex = 0x40;
int need_rex = scc_mcode_amd64_is_byte_reg_needs_rex(src);
if (src >= 8)
@@ -1531,8 +1560,8 @@ static inline void scc_mcode_amd64_idiv_r8(scc_mcode_t *mcode, int src) {
* @param dst 目标寄存器
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_and_r64_r64(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_and_r64_r64(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x48;
if (src >= 8)
rex |= 0x04;
@@ -1551,8 +1580,8 @@ static inline void scc_mcode_amd64_and_r64_r64(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param imm 32 位立即数
*/
static inline void scc_mcode_amd64_and_r64_imm32(scc_mcode_t *mcode, int dst,
u32 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_and_r64_imm32(scc_mcode_t *mcode, int dst,
u32 imm) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x01;
@@ -1570,8 +1599,8 @@ static inline void scc_mcode_amd64_and_r64_imm32(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_or_r64_r64(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_or_r64_r64(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x48;
if (src >= 8)
rex |= 0x04;
@@ -1590,8 +1619,8 @@ static inline void scc_mcode_amd64_or_r64_r64(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param imm 32 位立即数
*/
static inline void scc_mcode_amd64_or_r64_imm32(scc_mcode_t *mcode, int dst,
u32 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_or_r64_imm32(scc_mcode_t *mcode, int dst,
u32 imm) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x01;
@@ -1609,8 +1638,8 @@ static inline void scc_mcode_amd64_or_r64_imm32(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_xor_r64_r64(scc_mcode_t *mcode, int dst,
int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_xor_r64_r64(scc_mcode_t *mcode, int dst,
int src) {
u8 rex = 0x48;
if (src >= 8)
rex |= 0x04;
@@ -1629,8 +1658,8 @@ static inline void scc_mcode_amd64_xor_r64_r64(scc_mcode_t *mcode, int dst,
* @param dst 目标寄存器
* @param imm 32 位立即数
*/
static inline void scc_mcode_amd64_xor_r64_imm32(scc_mcode_t *mcode, int dst,
u32 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_xor_r64_imm32(scc_mcode_t *mcode, int dst,
u32 imm) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x01;
@@ -1651,7 +1680,7 @@ static inline void scc_mcode_amd64_xor_r64_imm32(scc_mcode_t *mcode, int dst,
* @param mcode 机器码缓冲区
* @param dst 目标寄存器
*/
static inline void scc_mcode_amd64_shl_r64_1(scc_mcode_t *mcode, int dst) {
SCC_MCODE_FUNC void scc_mcode_amd64_shl_r64_1(scc_mcode_t *mcode, int dst) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x01;
@@ -1668,8 +1697,8 @@ static inline void scc_mcode_amd64_shl_r64_1(scc_mcode_t *mcode, int dst) {
* @param dst 目标寄存器
* @param imm 移位次数
*/
static inline void scc_mcode_amd64_shl_r64_imm8(scc_mcode_t *mcode, int dst,
u8 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_shl_r64_imm8(scc_mcode_t *mcode, int dst,
u8 imm) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x01;
@@ -1686,7 +1715,7 @@ static inline void scc_mcode_amd64_shl_r64_imm8(scc_mcode_t *mcode, int dst,
* @param mcode 机器码缓冲区
* @param dst 目标寄存器
*/
static inline void scc_mcode_amd64_shr_r64_1(scc_mcode_t *mcode, int dst) {
SCC_MCODE_FUNC void scc_mcode_amd64_shr_r64_1(scc_mcode_t *mcode, int dst) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x01;
@@ -1703,8 +1732,8 @@ static inline void scc_mcode_amd64_shr_r64_1(scc_mcode_t *mcode, int dst) {
* @param dst 目标寄存器
* @param imm 移位次数
*/
static inline void scc_mcode_amd64_shr_r64_imm8(scc_mcode_t *mcode, int dst,
u8 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_shr_r64_imm8(scc_mcode_t *mcode, int dst,
u8 imm) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x01;
@@ -1721,7 +1750,7 @@ static inline void scc_mcode_amd64_shr_r64_imm8(scc_mcode_t *mcode, int dst,
* @param mcode 机器码缓冲区
* @param dst 目标寄存器
*/
static inline void scc_mcode_amd64_sar_r64_1(scc_mcode_t *mcode, int dst) {
SCC_MCODE_FUNC void scc_mcode_amd64_sar_r64_1(scc_mcode_t *mcode, int dst) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x01;
@@ -1738,8 +1767,8 @@ static inline void scc_mcode_amd64_sar_r64_1(scc_mcode_t *mcode, int dst) {
* @param dst 目标寄存器
* @param imm 移位次数
*/
static inline void scc_mcode_amd64_sar_r64_imm8(scc_mcode_t *mcode, int dst,
u8 imm) {
SCC_MCODE_FUNC void scc_mcode_amd64_sar_r64_imm8(scc_mcode_t *mcode, int dst,
u8 imm) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x01;
@@ -1759,8 +1788,8 @@ static inline void scc_mcode_amd64_sar_r64_imm8(scc_mcode_t *mcode, int dst,
* @param cond 条件码0-15
* @param reg 目标 8 位寄存器
*/
static inline void scc_mcode_amd64_setcc_r8(scc_mcode_t *mcode, int cond,
int reg) {
SCC_MCODE_FUNC void scc_mcode_amd64_setcc_r8(scc_mcode_t *mcode, int cond,
int reg) {
u8 rex = 0x40;
int need_rex = scc_mcode_amd64_is_byte_reg_needs_rex(reg);
if (reg >= 8) {
@@ -1783,8 +1812,8 @@ static inline void scc_mcode_amd64_setcc_r8(scc_mcode_t *mcode, int cond,
* @param cond 条件码
* @param base 基址寄存器
*/
static inline void scc_mcode_amd64_setcc_m8(scc_mcode_t *mcode, int cond,
int base) {
SCC_MCODE_FUNC void scc_mcode_amd64_setcc_m8(scc_mcode_t *mcode, int cond,
int base) {
u8 rex = 0x40;
int need_rex = scc_mcode_amd64_is_byte_reg_needs_rex(base);
if (base >= 8) {
@@ -1814,7 +1843,7 @@ static inline void scc_mcode_amd64_setcc_m8(scc_mcode_t *mcode, int cond,
* @param mcode 机器码缓冲区
* @param rel32 32 位相对偏移
*/
static inline void scc_mcode_amd64_jmp_rel32(scc_mcode_t *mcode, u32 rel32) {
SCC_MCODE_FUNC void scc_mcode_amd64_jmp_rel32(scc_mcode_t *mcode, u32 rel32) {
scc_mcode_add_u8(mcode, 0xE9);
scc_mcode_add_u32(mcode, rel32);
}
@@ -1826,8 +1855,8 @@ static inline void scc_mcode_amd64_jmp_rel32(scc_mcode_t *mcode, u32 rel32) {
* @param cond 条件码
* @param rel32 32 位相对偏移
*/
static inline void scc_mcode_amd64_jcc_rel32(scc_mcode_t *mcode, int cond,
u32 rel32) {
SCC_MCODE_FUNC void scc_mcode_amd64_jcc_rel32(scc_mcode_t *mcode, int cond,
u32 rel32) {
scc_mcode_add_u8(mcode, 0x0F);
scc_mcode_add_u8(mcode, 0x80 + cond);
scc_mcode_add_u32(mcode, rel32);
@@ -1839,7 +1868,7 @@ static inline void scc_mcode_amd64_jcc_rel32(scc_mcode_t *mcode, int cond,
* @param mcode 机器码缓冲区
* @param rel32 32 位相对偏移
*/
static inline void scc_mcode_amd64_call_rel32(scc_mcode_t *mcode, u32 rel32) {
SCC_MCODE_FUNC void scc_mcode_amd64_call_rel32(scc_mcode_t *mcode, u32 rel32) {
scc_mcode_add_u8(mcode, 0xE8);
scc_mcode_add_u32(mcode, rel32);
}
@@ -1850,7 +1879,7 @@ static inline void scc_mcode_amd64_call_rel32(scc_mcode_t *mcode, u32 rel32) {
* @param mcode 机器码缓冲区
* @param reg 目标寄存器(存储跳转地址)
*/
static inline void scc_mcode_amd64_jmp_r64(scc_mcode_t *mcode, int reg) {
SCC_MCODE_FUNC void scc_mcode_amd64_jmp_r64(scc_mcode_t *mcode, int reg) {
u8 rex = 0x40;
if (reg >= 8)
rex |= 0x01;
@@ -1866,7 +1895,7 @@ static inline void scc_mcode_amd64_jmp_r64(scc_mcode_t *mcode, int reg) {
* @param mcode 机器码缓冲区
* @param base 基址寄存器
*/
static inline void scc_mcode_amd64_jmp_m64(scc_mcode_t *mcode, int base) {
SCC_MCODE_FUNC void scc_mcode_amd64_jmp_m64(scc_mcode_t *mcode, int base) {
u8 rex = 0x48;
if (base >= 8)
rex |= 0x01;
@@ -1888,7 +1917,7 @@ static inline void scc_mcode_amd64_jmp_m64(scc_mcode_t *mcode, int base) {
* @param mcode 机器码缓冲区
* @param reg 目标寄存器
*/
static inline void scc_mcode_amd64_call_r64(scc_mcode_t *mcode, int reg) {
SCC_MCODE_FUNC void scc_mcode_amd64_call_r64(scc_mcode_t *mcode, int reg) {
u8 rex = 0x40;
if (reg >= 8)
rex |= 0x01;
@@ -1904,7 +1933,7 @@ static inline void scc_mcode_amd64_call_r64(scc_mcode_t *mcode, int reg) {
* @param mcode 机器码缓冲区
* @param base 基址寄存器
*/
static inline void scc_mcode_amd64_call_m64(scc_mcode_t *mcode, int base) {
SCC_MCODE_FUNC void scc_mcode_amd64_call_m64(scc_mcode_t *mcode, int base) {
u8 rex = 0x48;
if (base >= 8)
rex |= 0x01;
@@ -1925,7 +1954,7 @@ static inline void scc_mcode_amd64_call_m64(scc_mcode_t *mcode, int base) {
*
* @param mcode 机器码缓冲区
*/
static inline void scc_mcode_amd64_ret(scc_mcode_t *mcode) {
SCC_MCODE_FUNC void scc_mcode_amd64_ret(scc_mcode_t *mcode) {
scc_mcode_add_u8(mcode, 0xC3);
}
@@ -1936,7 +1965,7 @@ static inline void scc_mcode_amd64_ret(scc_mcode_t *mcode) {
*
* @param mcode 机器码缓冲区
*/
static inline void scc_mcode_amd64_syscall(scc_mcode_t *mcode) {
SCC_MCODE_FUNC void scc_mcode_amd64_syscall(scc_mcode_t *mcode) {
scc_mcode_add_u8(mcode, 0x0F);
scc_mcode_add_u8(mcode, 0x05);
}
@@ -1946,10 +1975,10 @@ static inline void scc_mcode_amd64_syscall(scc_mcode_t *mcode) {
// 辅助函数:根据 base, index, scale, disp 生成 ModRM 和 SIB 字节并写入
// 返回需要写入 disp 的字节数0,1,4调用者需写入 disp
static inline int scc_mcode_amd64_emit_sib_address(scc_mcode_t *mcode, int base,
int index, int scale,
u32 disp, int has_disp,
int disp8) {
SCC_MCODE_FUNC int scc_mcode_amd64_emit_sib_address(scc_mcode_t *mcode,
int base, int index,
int scale, u32 disp,
int has_disp, int disp8) {
// scale 编码: 0=1, 1=2, 2=4, 3=8
u8 scale_code = 0;
switch (scale) {
@@ -2013,7 +2042,7 @@ static inline int scc_mcode_amd64_emit_sib_address(scc_mcode_t *mcode, int base,
// * @param disp 位移量
// * @param disp_size 位移大小0=无位移1=8位4=32位
// */
// static inline void scc_mcode_amd64_mov_r64_m64_sib(scc_mcode_t *mcode, int
// SCC_MCODE_FUNC void scc_mcode_amd64_mov_r64_m64_sib(scc_mcode_t *mcode, int
// dst,
// int base, int index, int
// scale, u32 disp, int
@@ -2049,9 +2078,9 @@ static inline int scc_mcode_amd64_emit_sib_address(scc_mcode_t *mcode, int base,
* @param scale 比例因子1,2,4,8
* @param disp 位移量
*/
static inline void scc_mcode_amd64_mov_r64_m64_sib(scc_mcode_t *mcode, int dst,
int base, int index,
int scale, u32 disp) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_r64_m64_sib(scc_mcode_t *mcode, int dst,
int base, int index,
int scale, u32 disp) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x04;
@@ -2133,9 +2162,10 @@ static inline void scc_mcode_amd64_mov_r64_m64_sib(scc_mcode_t *mcode, int dst,
* @param disp 位移量
* @param src 源寄存器
*/
static inline void scc_mcode_amd64_mov_m64_sib_r64(scc_mcode_t *mcode, int base,
int index, int scale,
u32 disp, int src) {
SCC_MCODE_FUNC void scc_mcode_amd64_mov_m64_sib_r64(scc_mcode_t *mcode,
int base, int index,
int scale, u32 disp,
int src) {
u8 rex = 0x48;
if (src >= 8)
rex |= 0x04;
@@ -2214,9 +2244,9 @@ static inline void scc_mcode_amd64_mov_m64_sib_r64(scc_mcode_t *mcode, int base,
* @param scale 比例因子
* @param disp 位移量
*/
static inline void scc_mcode_amd64_lea_r64_m64_sib(scc_mcode_t *mcode, int dst,
int base, int index,
int scale, u32 disp) {
SCC_MCODE_FUNC void scc_mcode_amd64_lea_r64_m64_sib(scc_mcode_t *mcode, int dst,
int base, int index,
int scale, u32 disp) {
u8 rex = 0x48;
if (dst >= 8)
rex |= 0x04;

View File

@@ -5,6 +5,7 @@
typedef struct {
scc_hashtable_t str2libsym;
scc_pe_idata_lib_vec_t idata_libs;
const char *find_path;
} pe_idata_lib_ctx_t;
static void load_from_def(pe_idata_lib_ctx_t *ctx, const char *file_path,
@@ -57,14 +58,15 @@ static void load_from_def(pe_idata_lib_ctx_t *ctx, const char *file_path,
}
}
static void pe_idata_lib_init(pe_idata_lib_ctx_t *ctx) {
static void pe_idata_lib_init(pe_idata_lib_ctx_t *ctx, const char *find_path) {
// Got .dll.def
scc_hashtable_init(&ctx->str2libsym,
(scc_hashtable_hash_func_t)scc_strhash32,
(scc_hashtable_equal_func_t)scc_strcmp);
scc_vec_init(ctx->idata_libs);
load_from_def(ctx, __FILE__ "/../../.dll_def", "ucrtbase.dll");
ctx->find_path = find_path;
load_from_def(ctx, ctx->find_path, "ucrtbase.dll");
}
static cbool pe_idata_get(pe_idata_lib_ctx_t *ctx, const char *name) {
@@ -149,7 +151,7 @@ void sccf2pe(scc_pe_builder_t *builder, const sccf_t *sccf) {
}
pe_idata_lib_ctx_t idata_lib_ctx;
pe_idata_lib_init(&idata_lib_ctx);
pe_idata_lib_init(&idata_lib_ctx, __FILE__ "/../../.dll_def");
scc_vec_foreach(symtab, i) {
sccf_sym_t *sym = &scc_vec_at(symtab, i);
if (sym->sccf_sym_type == SCCF_SYM_TYPE_EXTERN) {
@@ -218,7 +220,6 @@ void sccf2pe(scc_pe_builder_t *builder, const sccf_t *sccf) {
if (reloc->reloc_type == SCCF_RELOC_TYPE_ABS) {
TODO();
}
Assert(reloc->sect_type == SCCF_SECT_CODE);
rva -= code_range.virual_address + reloc->offset + reloc->addend;
Assert(code_data != null);
// FIXME 需要确保宿主机与目标机器大小端一致