[backend] 删除了部分错误代码

This commit is contained in:
Lixuanwang
2025-07-18 01:37:29 +08:00
parent be8ca144d0
commit b90e4faa6a

View File

@ -237,6 +237,7 @@ std::string RISCv64CodeGen::basicBlock_gen(BasicBlock* bb, const RegAllocResult&
else {
ss << bb_name << ":\n"; // 基本块标签
}
if (DEBUG) std::cerr << "=== 生成基本块: " << bb_name << " ===\n";
// 构建当前基本块的 DAG
auto dag_nodes_for_bb = build_dag(bb);
@ -678,17 +679,27 @@ void RISCv64CodeGen::select_instructions(DAGNode* node, const RegAllocResult& al
// 获取分配的物理寄存器,若未分配则回退到 t0
auto get_preg_or_temp = [&](const std::string& vreg) {
if (vreg.empty()) { // 添加对空 vreg 的明确检查
if (DEBUG) std::cerr << "警告: 虚拟寄存器 (空字符串) 没有分配物理寄存器,使用临时寄存器 t0 代替。\n";
return reg_to_string(PhysicalReg::T0);
}
if (alloc.vreg_to_preg.count(vreg)) {
return reg_to_string(alloc.vreg_to_preg.at(vreg));
}
if (DEBUG) std::cerr << "警告: 虚拟寄存器 " << vreg << " 没有分配物理寄存器,使用临时寄存器 t0 代替。\n";
return reg_to_string(PhysicalReg::T0); // 回退到临时寄存器 t0
};
// 获取栈变量的内存偏移量
auto get_stack_offset = [&](Value* val) {
auto get_stack_offset = [&](Value* val) -> std::string { // 返回类型明确为 std::string
if (alloc.stack_map.count(val)) {
if (DEBUG) { // 避免在非DEBUG模式下打印大量内容
std::cout << "获取栈变量的内存偏移量,变量名: " << (val ? val->getName() : "unknown") << std::endl;
}
return std::to_string(alloc.stack_map.at(val));
}
if (DEBUG) std::cerr << "警告: 栈变量 " << (val ? val->getName() : "unknown") << " 没有在栈映射中找到,使用默认偏移 0。\n";
// 如果没有找到映射,返回默认偏移量 "0"
return std::string("0"); // 默认或错误情况
};
@ -762,6 +773,46 @@ void RISCv64CodeGen::select_instructions(DAGNode* node, const RegAllocResult& al
if (!bin) break;
std::string dest_reg = get_preg_or_temp(node->result_vreg);
// 检查是否是 base + offset 的地址计算
if (bin->getKind() == BinaryInst::kAdd) {
DAGNode* op0 = node->operands[0];
DAGNode* op1 = node->operands[1];
DAGNode* base_node = nullptr;
DAGNode* offset_node = nullptr;
bool is_alloca_base = false;
// 识别 base_address + byte_offset 模式
if (op0->kind == DAGNode::ALLOCA_ADDR) {
base_node = op0;
offset_node = op1;
is_alloca_base = true;
} else if (op1->kind == DAGNode::ALLOCA_ADDR) {
base_node = op1;
offset_node = op0;
is_alloca_base = true;
}
if (is_alloca_base) {
if (auto alloca_inst = dynamic_cast<AllocaInst*>(base_node->value)) {
std::string offset_str = get_stack_offset(alloca_inst);
// 将字符串偏移量转换为 int以便进行可能的调试和更清晰的逻辑
// 注意addi 指令可以直接接受字符串形式的立即数
std::string offset_reg = get_preg_or_temp(offset_node->result_vreg); // 获取索引偏移量的寄存器
// 生成两条指令来计算最终地址:
// 1. addi 将 s0 加上 offset 得到 b 的实际基地址(放入 dest_reg
// 2. addw 将 dest_reg 和索引偏移量寄存器相加,得到最终地址
ss_inst << "addi " << dest_reg << ", s0, " << offset_str << "\n"; // 使用字符串形式的偏移量
ss_inst << " addw " << dest_reg << ", " << dest_reg << ", " << offset_reg;
node->inst = ss_inst.str();
break;
}
}
}
std::string lhs_reg = get_preg_or_temp(node->operands[0]->result_vreg);
std::string rhs_reg = get_preg_or_temp(node->operands[1]->result_vreg);