feat(ir2mcode): 添加帧分配器框架和Windows 64位实现
添加了frame_alloc.h头文件定义帧分配器操作接口,实现了Windows 64位 平台的帧分配器实现,包括槽位分配、偏移计算和栈帧布局管理功能。 BREAKING CHANGE: 移除了旧的frame_manager.h接口,采用新的frame_alloc_ops_t 抽象接口。 fix(ast2ir): 修复字符串字面量表达式返回值问题 修复了AST到IR转换过程中字符串字面量表达式的处理,确保正确返回 创建的常量字符串值引用而非直接跳出。 fix(ir): 修复内置memcpy函数参数验证和类型比较逻辑 在IR构建器中为builtin_memcpy函数添加参数空指针检查,在类型比较 函数中添加未知类型的边界条件处理,增强系统稳定性。 refactor(ir2mcode): 重构寄存器分配器接口以支持帧分配器集成 修改寄存器分配器接口以接受帧分配器参数,统一节点到位置的映射表 命名,并提供便捷的栈大小和偏移获取接口。
This commit is contained in:
21
libs/ir2mcode/include/frame_alloc.h
Normal file
21
libs/ir2mcode/include/frame_alloc.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef __SCC_FRAME_ALLOC_H__
|
||||
#define __SCC_FRAME_ALLOC_H__
|
||||
|
||||
#include <scc_ir.h>
|
||||
|
||||
typedef struct scc_frame_alloc_ops scc_frame_alloc_ops_t;
|
||||
struct scc_frame_alloc_ops {
|
||||
/// maybe have direct function to new
|
||||
scc_frame_alloc_ops_t *(*new)(scc_ir_func_t *func);
|
||||
int (*drop)(scc_frame_alloc_ops_t *alloc);
|
||||
|
||||
int (*alloc_spill_slot)(scc_frame_alloc_ops_t *frame, int size, int align);
|
||||
int (*alloc_local_slot)(scc_frame_alloc_ops_t *frame, int size, int align,
|
||||
const char *name);
|
||||
|
||||
void (*finalize)(scc_frame_alloc_ops_t *frame);
|
||||
int (*get_slot_offset)(scc_frame_alloc_ops_t *frame, int slot_id);
|
||||
int (*get_frame_size)(scc_frame_alloc_ops_t *frame);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,27 +0,0 @@
|
||||
#ifndef __SCC_FRAME_MANAGER_H__
|
||||
#define __SCC_FRAME_MANAGER_H__
|
||||
|
||||
typedef struct frame_manager frame_manager_t;
|
||||
|
||||
// 初始化帧管理器,传入 ABI 参数(影子空间大小、栈对齐等)
|
||||
void frame_manager_init(frame_manager_t *fm, int shadow_space, int align);
|
||||
|
||||
// 分配一个栈槽(用于局部变量或临时值),返回虚拟槽索引(从0开始)
|
||||
int frame_alloc_slot(frame_manager_t *fm, int size);
|
||||
|
||||
// 分配一个保存的寄存器槽(用于被调用者保存的寄存器),返回虚拟槽索引
|
||||
int frame_alloc_saved_reg(frame_manager_t *fm, int reg_width);
|
||||
|
||||
// 计算最终栈帧总大小(已对齐)
|
||||
int frame_total_size(frame_manager_t *fm);
|
||||
|
||||
// 将虚拟槽索引转换为相对于 RBP 的偏移(正数,表示从 RBP 向下的距离)
|
||||
int frame_slot_offset(frame_manager_t *fm, int slot_idx);
|
||||
|
||||
// 获取影子空间大小(固定)
|
||||
int frame_shadow_space(frame_manager_t *fm);
|
||||
|
||||
// 获取保存寄存器区域的总大小
|
||||
int frame_saved_reg_size(frame_manager_t *fm);
|
||||
|
||||
#endif /* __SCC_FRAME_MANAGER_H__ */
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef __SCC_REG_ALLOC_H__
|
||||
#define __SCC_REG_ALLOC_H__
|
||||
|
||||
#include "frame_alloc.h"
|
||||
#include <scc_core.h>
|
||||
#include <scc_ir.h>
|
||||
#include <scc_utils.h>
|
||||
@@ -18,33 +19,59 @@ typedef enum {
|
||||
|
||||
typedef struct {
|
||||
scc_reg_kind_t kind;
|
||||
usize idx;
|
||||
union {
|
||||
usize data;
|
||||
int slot_idx;
|
||||
int gpr_idx;
|
||||
int fpr_idx;
|
||||
} data;
|
||||
} scc_reg_loc_t;
|
||||
typedef SCC_VEC(scc_reg_loc_t) scc_reg_loc_vec_t;
|
||||
|
||||
struct scc_reg_alloc;
|
||||
typedef struct scc_reg_alloc scc_reg_alloc_t;
|
||||
typedef scc_hashtable_t *(*scc_reg_alloc_func_t)(
|
||||
scc_reg_alloc_t *ctx, ///< @param [in] 上下文
|
||||
scc_ir_func_t *func ///< @param [in] 待处理的 IR 函数
|
||||
);
|
||||
typedef void (*scc_reg_alloc_func_t)(scc_reg_alloc_t *ctx, scc_ir_func_t *func,
|
||||
scc_frame_alloc_ops_t *frame_alloc);
|
||||
|
||||
typedef struct scc_reg_alloc {
|
||||
scc_ir_module_t *ir_module; ///< IR存储节点
|
||||
scc_hashtable_t node_ref2reg_loc; ///< 输出结果哈希表
|
||||
scc_reg_loc_vec_t reg_loc_vec;
|
||||
int gpr_caller_saved; ///< 函数可以随意修改,调用者如果在意需自行保护.
|
||||
int gpr_callee_saved; ///< 函数必须保护这些寄存器的值.
|
||||
scc_frame_alloc_ops_t *frame_alloc;
|
||||
scc_ir_module_t *ir_module; ///< IR存储节点
|
||||
scc_hashtable_t node2loc; ///< 输出结果哈希表
|
||||
scc_reg_loc_vec_t loc_vec;
|
||||
scc_reg_alloc_func_t reg_alloc_func;
|
||||
int alloc_stack_size;
|
||||
int init_stack_size;
|
||||
} scc_reg_alloc_t;
|
||||
|
||||
#define scc_reg_alloc(ctx, func) ((ctx)->reg_alloc_func(ctx, func))
|
||||
|
||||
void scc_reg_alloc_init(scc_reg_alloc_t *ctx, scc_reg_alloc_func_t func,
|
||||
void scc_reg_alloc_init(scc_reg_alloc_t *ctx, scc_reg_alloc_func_t strategy,
|
||||
scc_ir_module_t *ir_module);
|
||||
scc_hashtable_t *scc_reg_alloc_with_stack(scc_reg_alloc_t *ctx,
|
||||
scc_ir_func_t *func);
|
||||
static inline scc_hashtable_t *
|
||||
scc_reg_alloc_run(scc_reg_alloc_t *ctx, scc_ir_func_t *func,
|
||||
const scc_frame_alloc_ops_t *frame_alloc) {
|
||||
if (ctx->frame_alloc != nullptr) {
|
||||
ctx->frame_alloc->drop(ctx->frame_alloc);
|
||||
ctx->frame_alloc = nullptr;
|
||||
}
|
||||
Assert(ctx->frame_alloc == nullptr);
|
||||
ctx->frame_alloc = frame_alloc->new(func);
|
||||
Assert(ctx->frame_alloc != nullptr);
|
||||
ctx->reg_alloc_func(ctx, func, ctx->frame_alloc);
|
||||
ctx->frame_alloc->finalize(ctx->frame_alloc);
|
||||
return &ctx->node2loc;
|
||||
}
|
||||
|
||||
static inline usize scc_reg_stack_size(scc_reg_alloc_t *ctx) {
|
||||
return ctx->frame_alloc->get_frame_size(ctx->frame_alloc);
|
||||
}
|
||||
|
||||
static inline int scc_reg_stack_offset(scc_reg_alloc_t *ctx,
|
||||
scc_reg_loc_t *loc) {
|
||||
Assert(loc->kind == SCC_REG_KIND_STACK ||
|
||||
loc->kind == SCC_REG_KIND_STACK_ADDR);
|
||||
return ctx->frame_alloc->get_slot_offset(ctx->frame_alloc,
|
||||
loc->data.slot_idx);
|
||||
}
|
||||
|
||||
void scc_reg_alloc_strategy_pure_stack(scc_reg_alloc_t *ctx,
|
||||
scc_ir_func_t *func,
|
||||
scc_frame_alloc_ops_t *frame_alloc);
|
||||
|
||||
#endif /* __SCC_REG_ALLOC_H__ */
|
||||
|
||||
Reference in New Issue
Block a user