[backend]重构了后端
This commit is contained in:
38
src/include/RISCv64AsmPrinter.h
Normal file
38
src/include/RISCv64AsmPrinter.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef RISCV64_ASMPRINTER_H
|
||||
#define RISCV64_ASMPRINTER_H
|
||||
|
||||
#include "RISCv64LLIR.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace sysy {
|
||||
|
||||
class RISCv64AsmPrinter {
|
||||
public:
|
||||
// 主入口,将整个MachineFunction打印到指定的输出流
|
||||
void runOnMachineFunction(MachineFunction* mfunc, std::ostream& os);
|
||||
|
||||
private:
|
||||
// 打印单个基本块
|
||||
void printBasicBlock(MachineBasicBlock* mbb);
|
||||
|
||||
// 打印单条指令
|
||||
void printInstruction(MachineInstr* instr, MachineBasicBlock* parent_bb);
|
||||
|
||||
// 打印函数序言
|
||||
void printPrologue(MachineFunction* mfunc);
|
||||
|
||||
// 打印函数尾声
|
||||
void printEpilogue(MachineFunction* mfunc);
|
||||
|
||||
// 将物理寄存器枚举转换为字符串 (从原RISCv64Backend迁移)
|
||||
std::string regToString(PhysicalReg reg);
|
||||
|
||||
// 打印单个操作数
|
||||
void printOperand(MachineOperand* op);
|
||||
|
||||
std::ostream* OS; // 指向当前输出流
|
||||
};
|
||||
|
||||
} // namespace sysy
|
||||
|
||||
#endif // RISCV64_ASMPRINTER_H
|
||||
@@ -1,130 +1,29 @@
|
||||
#ifndef RISCV64_BACKEND_H
|
||||
#define RISCV64_BACKEND_H
|
||||
|
||||
#include "IR.h"
|
||||
#include "IR.h" // 只需包含高层IR定义
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
#include <functional> // For std::function
|
||||
|
||||
extern int DEBUG;
|
||||
extern int DEEPDEBUG;
|
||||
|
||||
|
||||
|
||||
namespace sysy {
|
||||
|
||||
// 为活跃性分析的结果定义一个结构体,以同时持有 live_in 和 live_out 集合
|
||||
struct LivenessResult {
|
||||
std::map<Instruction*, std::set<std::string>> live_in;
|
||||
std::map<Instruction*, std::set<std::string>> live_out;
|
||||
};
|
||||
|
||||
// RISCv64CodeGen 现在是一个高层驱动器
|
||||
class RISCv64CodeGen {
|
||||
public:
|
||||
enum class PhysicalReg {
|
||||
ZERO, RA, SP, GP, TP, T0, T1, T2, S0, S1, A0, A1, A2, A3, A4, A5, A6, A7, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, T3, T4, T5, T6,
|
||||
F0, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15,F16, F17, F18, F19, F20, F21, F22, F23, F24, F25, F26, F27, F28, F29, F30, F31
|
||||
};
|
||||
|
||||
// Move DAGNode and RegAllocResult to public section
|
||||
struct DAGNode {
|
||||
enum NodeKind { CONSTANT, LOAD, STORE, BINARY, CALL, RETURN, BRANCH, ALLOCA_ADDR, UNARY, MEMSET };
|
||||
NodeKind kind;
|
||||
Value* value = nullptr; // For IR Value
|
||||
std::string inst; // Generated RISC-V instruction(s) for this node
|
||||
std::string result_vreg; // Virtual register assigned to this node's result
|
||||
std::vector<DAGNode*> operands;
|
||||
std::vector<DAGNode*> users; // For debugging and potentially optimizations
|
||||
DAGNode(NodeKind k) : kind(k) {}
|
||||
|
||||
// Debugging / helper
|
||||
std::string getNodeKindString() const {
|
||||
switch (kind) {
|
||||
case CONSTANT: return "CONSTANT";
|
||||
case LOAD: return "LOAD";
|
||||
case STORE: return "STORE";
|
||||
case BINARY: return "BINARY";
|
||||
case CALL: return "CALL";
|
||||
case RETURN: return "RETURN";
|
||||
case BRANCH: return "BRANCH";
|
||||
case ALLOCA_ADDR: return "ALLOCA_ADDR";
|
||||
case UNARY: return "UNARY";
|
||||
case MEMSET: return "MEMSET";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct RegAllocResult {
|
||||
std::map<std::string, PhysicalReg> vreg_to_preg; // Virtual register to Physical Register mapping
|
||||
std::map<Value*, int> stack_map; // Value (AllocaInst) to stack offset
|
||||
int stack_size = 0; // Total stack frame size for locals and spills
|
||||
};
|
||||
|
||||
RISCv64CodeGen(Module* mod) : module(mod) {}
|
||||
|
||||
// 唯一的公共入口点
|
||||
std::string code_gen();
|
||||
std::string module_gen();
|
||||
std::string function_gen(Function* func);
|
||||
// 修改 basicBlock_gen 的声明,添加 int block_idx 参数
|
||||
std::string basicBlock_gen(BasicBlock* bb, const RegAllocResult& alloc, int block_idx);
|
||||
|
||||
// DAG related
|
||||
std::vector<std::unique_ptr<DAGNode>> build_dag(BasicBlock* bb);
|
||||
void select_instructions(DAGNode* node, const RegAllocResult& alloc);
|
||||
// 改变 emit_instructions 的参数,使其可以直接添加汇编指令到 main ss
|
||||
void emit_instructions(DAGNode* node, std::stringstream& ss, const RegAllocResult& alloc, std::set<DAGNode*>& emitted_nodes);
|
||||
|
||||
// Register Allocation related
|
||||
LivenessResult liveness_analysis(Function* func);
|
||||
std::map<std::string, std::set<std::string>> build_interference_graph(const LivenessResult& liveness);
|
||||
void color_graph(std::map<std::string, PhysicalReg>& vreg_to_preg,
|
||||
const std::map<std::string, std::set<std::string>>& interference_graph);
|
||||
RegAllocResult register_allocation(Function* func);
|
||||
void eliminate_phi(Function* func); // Phi elimination is typically done before DAG building
|
||||
|
||||
// Utility
|
||||
std::string reg_to_string(PhysicalReg reg);
|
||||
void print_dag(const std::vector<std::unique_ptr<DAGNode>>& dag, const std::string& bb_name);
|
||||
|
||||
private:
|
||||
static const std::vector<PhysicalReg> allocable_regs;
|
||||
std::map<Value*, std::string> value_vreg_map; // Maps IR Value* to its virtual register name
|
||||
// 模块级代码生成 (处理全局变量和驱动函数生成)
|
||||
std::string module_gen();
|
||||
|
||||
// 函数级代码生成 (实现新的流水线)
|
||||
std::string function_gen(Function* func);
|
||||
|
||||
Module* module;
|
||||
int vreg_counter = 0; // Counter for unique virtual register names
|
||||
int alloca_offset_counter = 0; // Counter for alloca offsets
|
||||
|
||||
// 新增一个成员变量来存储当前函数的所有 DAGNode,以确保其生命周期贯穿整个函数代码生成
|
||||
// 这样可以在多个 BasicBlock_gen 调用中访问到完整的 DAG 节点
|
||||
std::vector<std::unique_ptr<DAGNode>> current_function_dag_nodes;
|
||||
|
||||
// 为空标签定义一个伪名称前缀,加上块索引以确保唯一性
|
||||
const std::string ENTRY_BLOCK_PSEUDO_NAME = "entry_block_";
|
||||
|
||||
int local_label_counter = 0; // 用于生成唯一的本地标签 (如 memset 循环, 匿名块跳转等)
|
||||
|
||||
// !!! 修改:get_operand_node 辅助函数现在需要传入 value_to_node 和 nodes_storage 的引用
|
||||
// 因为它们是 build_dag 局部管理的
|
||||
DAGNode* get_operand_node(
|
||||
Value* val_ir,
|
||||
std::map<Value*, DAGNode*>& value_to_node,
|
||||
std::vector<std::unique_ptr<DAGNode>>& nodes_storage
|
||||
);
|
||||
|
||||
// !!! 新增:create_node 辅助函数也需要传入 value_to_node 和 nodes_storage 的引用
|
||||
// 并且它应该不再是 lambda,而是一个真正的成员函数
|
||||
DAGNode* create_node(
|
||||
DAGNode::NodeKind kind,
|
||||
Value* val,
|
||||
std::map<Value*, DAGNode*>& value_to_node,
|
||||
std::vector<std::unique_ptr<DAGNode>>& nodes_storage
|
||||
);
|
||||
|
||||
std::vector<std::unique_ptr<Instruction>> temp_instructions_storage; // 用于存储 build_dag 中创建的临时 BinaryInst
|
||||
};
|
||||
|
||||
} // namespace sysy
|
||||
|
||||
61
src/include/RISCv64ISel.h
Normal file
61
src/include/RISCv64ISel.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#ifndef RISCV64_ISEL_H
|
||||
#define RISCV64_ISEL_H
|
||||
|
||||
#include "IR.h"
|
||||
#include "RISCv64LLIR.h"
|
||||
#include <memory>
|
||||
#include <map>
|
||||
|
||||
namespace sysy {
|
||||
|
||||
class RISCv64ISel {
|
||||
public:
|
||||
RISCv64ISel();
|
||||
// 模块主入口:将一个高层IR函数转换为底层LLIR函数
|
||||
std::unique_ptr<MachineFunction> runOnFunction(Function* func);
|
||||
|
||||
private:
|
||||
// DAG节点定义,作为ISel的内部实现细节
|
||||
struct DAGNode {
|
||||
enum NodeKind { CONSTANT, LOAD, STORE, BINARY, CALL, RETURN, BRANCH, ALLOCA_ADDR, UNARY, MEMSET };
|
||||
NodeKind kind;
|
||||
Value* value = nullptr;
|
||||
std::vector<DAGNode*> operands;
|
||||
DAGNode(NodeKind k) : kind(k) {}
|
||||
};
|
||||
|
||||
// 为当前函数生成LLIR
|
||||
void select();
|
||||
|
||||
// 为单个基本块生成指令
|
||||
void selectBasicBlock(BasicBlock* bb);
|
||||
|
||||
// 核心函数:为DAG节点选择并生成MachineInstr
|
||||
void selectNode(DAGNode* node);
|
||||
|
||||
// --- DAG 构建相关函数 (从原RISCv64Backend迁移) ---
|
||||
std::vector<std::unique_ptr<DAGNode>> build_dag(BasicBlock* bb);
|
||||
DAGNode* get_operand_node(Value* val_ir, std::map<Value*, DAGNode*>& value_to_node, std::vector<std::unique_ptr<DAGNode>>& nodes_storage);
|
||||
DAGNode* create_node(DAGNode::NodeKind kind, Value* val, std::map<Value*, DAGNode*>& value_to_node, std::vector<std::unique_ptr<DAGNode>>& nodes_storage);
|
||||
|
||||
// --- 辅助函数 ---
|
||||
// 为一个IR Value获取/分配一个虚拟寄存器号
|
||||
unsigned getVReg(Value* val);
|
||||
|
||||
Function* F; // 当前处理的高层IR函数
|
||||
std::unique_ptr<MachineFunction> MFunc; // 正在构建的底层LLIR函数
|
||||
|
||||
MachineBasicBlock* CurMBB; // 当前正在处理的机器基本块
|
||||
|
||||
// 映射关系
|
||||
std::map<Value*, unsigned> vreg_map;
|
||||
std::map<const BasicBlock*, MachineBasicBlock*> bb_map;
|
||||
std::map<Value*, DAGNode*> value_to_node_map; // 用于selectNode中查找
|
||||
|
||||
unsigned vreg_counter;
|
||||
int local_label_counter;
|
||||
};
|
||||
|
||||
} // namespace sysy
|
||||
|
||||
#endif // RISCV64_ISEL_H
|
||||
@@ -163,8 +163,11 @@ public:
|
||||
MachineInstr(RVOpcodes opcode) : opcode(opcode) {}
|
||||
|
||||
RVOpcodes getOpcode() const { return opcode; }
|
||||
// 注意:返回const引用,因为通常不直接修改指令的操作数列表
|
||||
const std::vector<std::unique_ptr<MachineOperand>>& getOperands() const { return operands; }
|
||||
|
||||
// 提供一个非const版本,用于内部修改
|
||||
std::vector<std::unique_ptr<MachineOperand>>& getOperands() { return operands; }
|
||||
|
||||
void addOperand(std::unique_ptr<MachineOperand> operand) {
|
||||
operands.push_back(std::move(operand));
|
||||
}
|
||||
@@ -181,9 +184,12 @@ public:
|
||||
: name(name), parent(parent) {}
|
||||
|
||||
const std::string& getName() const { return name; }
|
||||
const std::vector<std::unique_ptr<MachineInstr>>& getInstructions() const { return instructions; }
|
||||
MachineFunction* getParent() const { return parent; }
|
||||
|
||||
// 同时提供 const 和 non-const 版本
|
||||
const std::vector<std::unique_ptr<MachineInstr>>& getInstructions() const { return instructions; }
|
||||
std::vector<std::unique_ptr<MachineInstr>>& getInstructions() { return instructions; }
|
||||
|
||||
void addInstruction(std::unique_ptr<MachineInstr> instr) {
|
||||
instructions.push_back(std::move(instr));
|
||||
}
|
||||
@@ -210,9 +216,12 @@ public:
|
||||
MachineFunction(const std::string& name) : name(name) {}
|
||||
|
||||
const std::string& getName() const { return name; }
|
||||
const std::vector<std::unique_ptr<MachineBasicBlock>>& getBlocks() const { return blocks; }
|
||||
StackFrameInfo& getFrameInfo() { return frame_info; }
|
||||
|
||||
// 同时提供 const 和 non-const 版本
|
||||
const std::vector<std::unique_ptr<MachineBasicBlock>>& getBlocks() const { return blocks; }
|
||||
std::vector<std::unique_ptr<MachineBasicBlock>>& getBlocks() { return blocks; }
|
||||
|
||||
void addBlock(std::unique_ptr<MachineBasicBlock> block) {
|
||||
blocks.push_back(std::move(block));
|
||||
}
|
||||
|
||||
57
src/include/RISCv64RegAlloc.h
Normal file
57
src/include/RISCv64RegAlloc.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#ifndef RISCV64_REGALLOC_H
|
||||
#define RISCV64_REGALLOC_H
|
||||
|
||||
#include "RISCv64LLIR.h"
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
namespace sysy {
|
||||
|
||||
class RISCv64RegAlloc {
|
||||
public:
|
||||
RISCv64RegAlloc(MachineFunction* mfunc);
|
||||
|
||||
// 模块主入口
|
||||
void run();
|
||||
|
||||
private:
|
||||
using LiveSet = std::set<unsigned>; // 活跃虚拟寄存器集合
|
||||
using InterferenceGraph = std::map<unsigned, std::set<unsigned>>;
|
||||
|
||||
// 活跃性分析
|
||||
void analyzeLiveness();
|
||||
|
||||
// 构建干扰图
|
||||
void buildInterferenceGraph();
|
||||
|
||||
// 图着色分配寄存器
|
||||
void colorGraph();
|
||||
|
||||
// 重写函数,将虚拟寄存器替换为物理寄存器,并插入溢出代码
|
||||
void rewriteFunction();
|
||||
|
||||
// 辅助函数,获取指令的Use/Def集合
|
||||
void getInstrUseDef(MachineInstr* instr, LiveSet& use, LiveSet& def);
|
||||
|
||||
MachineFunction* MFunc;
|
||||
|
||||
// 活跃性分析结果
|
||||
std::map<MachineInstr*, LiveSet> live_in_map;
|
||||
std::map<MachineInstr*, LiveSet> live_out_map;
|
||||
|
||||
// 干扰图
|
||||
InterferenceGraph interference_graph;
|
||||
|
||||
// 图着色结果
|
||||
std::map<unsigned, PhysicalReg> color_map; // vreg -> preg
|
||||
std::set<unsigned> spilled_vregs; // 被溢出的vreg集合
|
||||
|
||||
// 可用的物理寄存器池
|
||||
std::vector<PhysicalReg> allocable_int_regs;
|
||||
std::vector<PhysicalReg> allocable_float_regs; // (为未来浮点支持预留)
|
||||
};
|
||||
|
||||
} // namespace sysy
|
||||
|
||||
#endif // RISCV64_REGALLOC_H
|
||||
Reference in New Issue
Block a user