优化ir
This commit is contained in:
parent
de2190c211
commit
2884159f3e
@ -1,6 +1,9 @@
|
|||||||
package org.jcnc.snow.compiler.cli;
|
package org.jcnc.snow.compiler.cli;
|
||||||
|
|
||||||
|
import org.jcnc.snow.compiler.backend.RegisterAllocator;
|
||||||
|
import org.jcnc.snow.compiler.backend.VMCodeGenerator;
|
||||||
import org.jcnc.snow.compiler.ir.builder.ProgramBuilder;
|
import org.jcnc.snow.compiler.ir.builder.ProgramBuilder;
|
||||||
|
import org.jcnc.snow.compiler.ir.core.IRFunction;
|
||||||
import org.jcnc.snow.compiler.ir.core.IRProgram;
|
import org.jcnc.snow.compiler.ir.core.IRProgram;
|
||||||
import org.jcnc.snow.compiler.lexer.core.LexerEngine;
|
import org.jcnc.snow.compiler.lexer.core.LexerEngine;
|
||||||
import org.jcnc.snow.compiler.lexer.token.Token;
|
import org.jcnc.snow.compiler.lexer.token.Token;
|
||||||
@ -9,6 +12,8 @@ import org.jcnc.snow.compiler.parser.context.ParserContext;
|
|||||||
import org.jcnc.snow.compiler.parser.core.ParserEngine;
|
import org.jcnc.snow.compiler.parser.core.ParserEngine;
|
||||||
import org.jcnc.snow.compiler.parser.function.ASTPrinter;
|
import org.jcnc.snow.compiler.parser.function.ASTPrinter;
|
||||||
import org.jcnc.snow.compiler.semantic.core.SemanticAnalyzerRunner;
|
import org.jcnc.snow.compiler.semantic.core.SemanticAnalyzerRunner;
|
||||||
|
import org.jcnc.snow.vm.engine.VMMode;
|
||||||
|
import org.jcnc.snow.vm.engine.VirtualMachineEngine;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
@ -56,27 +61,27 @@ public class SnowCompiler {
|
|||||||
|
|
||||||
|
|
||||||
/* 5. 后端:寄存器分配 & 代码生成 + VM 执行 */
|
/* 5. 后端:寄存器分配 & 代码生成 + VM 执行 */
|
||||||
// for (IRFunction fn : program.functions()) {
|
for (IRFunction fn : program.functions()) {
|
||||||
// var alloc = new RegisterAllocator();
|
var alloc = new RegisterAllocator();
|
||||||
// var slotM = alloc.allocate(fn);
|
var slotM = alloc.allocate(fn);
|
||||||
//
|
|
||||||
// var gen = new VMCodeGenerator(slotM);
|
var gen = new VMCodeGenerator(slotM);
|
||||||
// var code = gen.generate(fn);
|
var code = gen.generate(fn);
|
||||||
//
|
|
||||||
// System.out.println("== VM code for " + fn.name() + " ==");
|
System.out.println("== VM code for " + fn.name() + " ==");
|
||||||
// code.forEach(System.out::println);
|
code.forEach(System.out::println);
|
||||||
//
|
|
||||||
//
|
|
||||||
// /* 只执行 main 函数 */
|
/* 只执行 main 函数 */
|
||||||
// if ("main".equals(fn.name())) {
|
if ("main".equals(fn.name())) {
|
||||||
// VirtualMachineEngine vm = new VirtualMachineEngine(VMMode.RUN);
|
VirtualMachineEngine vm = new VirtualMachineEngine(VMMode.RUN);
|
||||||
//
|
|
||||||
// vm.execute(code); // 运行指令
|
vm.execute(code); // 运行指令
|
||||||
// vm.printStack(); // 打印 Operand-/Call-Stack
|
vm.printStack(); // 打印 Operand-/Call-Stack
|
||||||
// vm.printLocalVariables(); // 打印局部变量槽
|
vm.printLocalVariables(); // 打印局部变量槽
|
||||||
//
|
|
||||||
// System.out.println("Process has ended");
|
System.out.println("Process has ended");
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
4
test
4
test
@ -1,5 +1,5 @@
|
|||||||
module: CommonTasks
|
module: CommonTasks
|
||||||
function: main1
|
function: main
|
||||||
parameter:
|
parameter:
|
||||||
declare num1: int
|
declare num1: int
|
||||||
declare num2: int
|
declare num2: int
|
||||||
@ -7,8 +7,6 @@ module: CommonTasks
|
|||||||
|
|
||||||
body:
|
body:
|
||||||
num1 = 10
|
num1 = 10
|
||||||
num2 = 20
|
|
||||||
declare result:int = num1+num2+1
|
|
||||||
end body
|
end body
|
||||||
end function
|
end function
|
||||||
end module
|
end module
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user