fix: 修复函数末尾 CALL 指令的未解析符号问题
- 在函数开始时登记函数入口地址 - 在函数末尾强制添加 RET 或 HALT 指令 - 优化了代码注释和格式
This commit is contained in:
parent
70feb1fe5e
commit
1605390f08
@ -1,6 +1,7 @@
|
||||
package org.jcnc.snow.compiler.backend.builder;
|
||||
|
||||
import org.jcnc.snow.compiler.backend.core.InstructionGenerator;
|
||||
import org.jcnc.snow.compiler.backend.utils.OpHelper;
|
||||
import org.jcnc.snow.compiler.ir.core.IRFunction;
|
||||
import org.jcnc.snow.compiler.ir.core.IRInstruction;
|
||||
import org.jcnc.snow.compiler.ir.value.IRVirtualRegister;
|
||||
@ -74,18 +75,26 @@ public final class VMCodeGenerator {
|
||||
*/
|
||||
public void generate(IRFunction fn) {
|
||||
this.currentFn = fn.name();
|
||||
out.beginFunction(currentFn); // 输出函数起始
|
||||
|
||||
/* 登记函数入口地址 —— 解决 CALL 未解析符号问题 */
|
||||
out.beginFunction(currentFn);
|
||||
|
||||
/* 逐条分发 IR 指令给对应的生成器 */
|
||||
for (IRInstruction ins : fn.body()) {
|
||||
@SuppressWarnings("unchecked")
|
||||
// 取得与当前 IR 指令类型匹配的生成器(泛型强转消除类型警告)
|
||||
InstructionGenerator<IRInstruction> gen =
|
||||
(InstructionGenerator<IRInstruction>) registry.get(ins.getClass());
|
||||
if (gen == null) {
|
||||
throw new IllegalStateException("Unsupported IR: " + ins);
|
||||
}
|
||||
// 通过多态分发到实际生成器
|
||||
gen.generate(ins, out, slotMap, currentFn);
|
||||
}
|
||||
out.endFunction(); // 输出函数结束
|
||||
|
||||
/* 强制补上函数结尾的返回/终止指令 */
|
||||
String retOpcode = "main".equals(currentFn) ? "HALT" : "RET";
|
||||
out.emit(OpHelper.opcode(retOpcode));
|
||||
|
||||
/* 结束函数 */
|
||||
out.endFunction();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user