#ifndef __SCC_REG_ALLOC_H__ #define __SCC_REG_ALLOC_H__ #include "frame_alloc.h" #include #include #include typedef enum { SCC_REG_KIND_UNDEF, SCC_REG_KIND_FUNC_ARG, SCC_REG_KIND_GPR, ///< 通用寄存器(整数) SCC_REG_KIND_FPR, ///< 浮点数寄存器 SCC_REG_KIND_STACK, ///< 栈 SCC_REG_KIND_STACK_ADDR, ///< 栈地址(如 alloc 节点) SCC_REG_KIND_IMM, ///< 整数立即数 SCC_REG_KIND_IMM_FP, ///< 浮点数常量 } scc_reg_kind_t; typedef struct { scc_reg_kind_t kind; 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 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_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; } scc_reg_alloc_t; void scc_reg_alloc_init(scc_reg_alloc_t *ctx, scc_reg_alloc_func_t strategy, scc_ir_module_t *ir_module); 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__ */