[backend]修复了一个栈管理问题

This commit is contained in:
Lixuanwang
2025-07-30 20:40:56 +08:00
parent 8e94f89931
commit 03b62b138f
4 changed files with 66 additions and 90 deletions

View File

@@ -12,11 +12,10 @@ std::string RISCv64CodeGen::code_gen() {
return module_gen();
}
// 模块级代码生成
std::string RISCv64CodeGen::module_gen() {
std::stringstream ss;
// --- 步骤1将全局变量分为.data和.bss两组 ---
// --- 步骤1将全局变量(GlobalValue)分为.data和.bss两组 ---
std::vector<GlobalValue*> data_globals;
std::vector<GlobalValue*> bss_globals;
@@ -41,7 +40,7 @@ std::string RISCv64CodeGen::module_gen() {
}
}
// --- 步骤2生成 .bss 段的代码 ---
// --- 步骤2生成 .bss 段的代码 (这部分不变) ---
if (!bss_globals.empty()) {
ss << ".bss\n";
for (GlobalValue* global : bss_globals) {
@@ -57,9 +56,12 @@ std::string RISCv64CodeGen::module_gen() {
}
}
// --- 步骤3生成 .data 段的代码 ---
if (!data_globals.empty()) {
ss << ".data\n"; // 切换到 .data 段
// --- [修改] 步骤3生成 .data 段的代码 ---
// 我们需要检查 data_globals 和 常量列表是否都为空
if (!data_globals.empty() || !module->getConsts().empty()) {
ss << ".data\n";
// a. 先处理普通的全局变量 (GlobalValue)
for (GlobalValue* global : data_globals) {
ss << ".globl " << global->getName() << "\n";
ss << global->getName() << ":\n";
@@ -106,7 +108,7 @@ std::string RISCv64CodeGen::module_gen() {
}
}
// --- 处理函数 (.text段) ---
// --- 处理函数 (.text段) 的逻辑保持不变 ---
if (!module->getFunctions().empty()) {
ss << ".text\n";
for (const auto& func_pair : module->getFunctions()) {