[backend] test01 passed
This commit is contained in:
@ -117,6 +117,11 @@ std::vector<std::string> RISCv32CodeGen::instruction_gen(Instruction* inst, cons
|
||||
if (lhs_const->isInt()) {
|
||||
insts.push_back("li " + lhs_reg + ", " + std::to_string(lhs_const->getInt()));
|
||||
}
|
||||
} else if (auto lhs_load = dynamic_cast<LoadInst*>(bin->getLhs())) {
|
||||
auto ptr_it = alloc.stack_map.find(lhs_load->getPointer());
|
||||
if (ptr_it != alloc.stack_map.end()) {
|
||||
insts.push_back("lw " + lhs_reg + ", " + std::to_string(ptr_it->second) + "(s0)");
|
||||
}
|
||||
} else {
|
||||
auto lhs_it = alloc.stack_map.find(bin->getLhs());
|
||||
if (lhs_it != alloc.stack_map.end()) {
|
||||
@ -128,6 +133,11 @@ std::vector<std::string> RISCv32CodeGen::instruction_gen(Instruction* inst, cons
|
||||
if (rhs_const->isInt()) {
|
||||
insts.push_back("li " + rhs_reg + ", " + std::to_string(rhs_const->getInt()));
|
||||
}
|
||||
} else if (auto rhs_load = dynamic_cast<LoadInst*>(bin->getRhs())) {
|
||||
auto ptr_it = alloc.stack_map.find(rhs_load->getPointer());
|
||||
if (ptr_it != alloc.stack_map.end()) {
|
||||
insts.push_back("lw " + rhs_reg + ", " + std::to_string(ptr_it->second) + "(s0)");
|
||||
}
|
||||
} else {
|
||||
auto rhs_it = alloc.stack_map.find(bin->getRhs());
|
||||
if (rhs_it != alloc.stack_map.end()) {
|
||||
@ -153,19 +163,16 @@ std::vector<std::string> RISCv32CodeGen::instruction_gen(Instruction* inst, cons
|
||||
if (ptr_it != alloc.stack_map.end()) {
|
||||
// 局部变量:直接从栈加载
|
||||
insts.push_back("lw " + dst_reg + ", " + std::to_string(ptr_it->second) + "(s0)");
|
||||
auto dst_it = alloc.stack_map.find(load);
|
||||
if (dst_it != alloc.stack_map.end()) {
|
||||
insts.push_back("sw " + dst_reg + ", " + std::to_string(dst_it->second) + "(s0)");
|
||||
}
|
||||
} else {
|
||||
// 全局变量:加载地址并读取
|
||||
std::string ptr_reg = "t0";
|
||||
insts.push_back("la " + ptr_reg + ", " + load->getPointer()->getName());
|
||||
insts.push_back("lw " + dst_reg + ", 0(" + ptr_reg + ")");
|
||||
auto dst_it = alloc.stack_map.find(load);
|
||||
if (dst_it != alloc.stack_map.end()) {
|
||||
insts.push_back("sw " + dst_reg + ", " + std::to_string(dst_it->second) + "(s0)");
|
||||
}
|
||||
}
|
||||
// 仅在需要时存储
|
||||
auto dst_it = alloc.stack_map.find(load);
|
||||
if (dst_it != alloc.stack_map.end()) {
|
||||
insts.push_back("sw " + dst_reg + ", " + std::to_string(dst_it->second) + "(s0)");
|
||||
}
|
||||
} else if (auto store = dynamic_cast<StoreInst*>(inst)) {
|
||||
std::string val_reg = "t0";
|
||||
@ -223,6 +230,13 @@ RISCv32CodeGen::RegAllocResult RISCv32CodeGen::register_allocation(Function* fun
|
||||
stack_offset += 4;
|
||||
allocated.insert(ptr);
|
||||
}
|
||||
} else if (auto load = dynamic_cast<LoadInst*>(inst.get())) {
|
||||
// 为 load 结果分配栈空间(如果后续使用)
|
||||
if (result.stack_map.find(load) == result.stack_map.end() && allocated.find(load) == allocated.end()) {
|
||||
result.stack_map[load] = stack_offset;
|
||||
stack_offset += 4;
|
||||
allocated.insert(load);
|
||||
}
|
||||
} else if (auto bin = dynamic_cast<BinaryInst*>(inst.get())) {
|
||||
// 为二元操作结果分配栈空间
|
||||
if (result.stack_map.find(bin) == result.stack_map.end() && allocated.find(bin) == allocated.end()) {
|
||||
@ -230,16 +244,6 @@ RISCv32CodeGen::RegAllocResult RISCv32CodeGen::register_allocation(Function* fun
|
||||
stack_offset += 4;
|
||||
allocated.insert(bin);
|
||||
}
|
||||
} else if (auto ret = dynamic_cast<ReturnInst*>(inst.get())) {
|
||||
// 为返回值分配栈空间(如果需要)
|
||||
if (ret->hasReturnValue() && !dynamic_cast<ConstantValue*>(ret->getReturnValue())) {
|
||||
auto ret_val = ret->getReturnValue();
|
||||
if (result.stack_map.find(ret_val) == result.stack_map.end() && allocated.find(ret_val) == allocated.end()) {
|
||||
result.stack_map[ret_val] = stack_offset;
|
||||
stack_offset += 4;
|
||||
allocated.insert(ret_val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ using namespace antlr4;
|
||||
// #include "Backend.h"
|
||||
#include "SysYIRGenerator.h"
|
||||
#include "SysYIRPrinter.h"
|
||||
#include "RISCv32Backend.h"
|
||||
// #include "LLVMIRGenerator.h"
|
||||
using namespace sysy;
|
||||
|
||||
@ -72,14 +73,22 @@ int main(int argc, char **argv) {
|
||||
|
||||
|
||||
// visit AST to generate IR
|
||||
|
||||
SysYIRGenerator generator;
|
||||
generator.visitCompUnit(moduleAST);
|
||||
if (argStopAfter == "ir") {
|
||||
SysYIRGenerator generator;
|
||||
generator.visitCompUnit(moduleAST);
|
||||
auto moduleIR = generator.get();
|
||||
SysYPrinter printer(moduleIR);
|
||||
printer.printIR();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// generate assembly
|
||||
auto module = generator.get();
|
||||
sysy::RISCv32CodeGen codegen(module);
|
||||
string asmCode = codegen.code_gen();
|
||||
if (argStopAfter == "asm") {
|
||||
cout << asmCode << endl;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user