修改标识符
This commit is contained in:
parent
919f68550f
commit
375a62dc0b
@ -26,22 +26,22 @@ public class FunctionBuilder {
|
|||||||
*/
|
*/
|
||||||
public IRFunction build(FunctionNode fn) {
|
public IRFunction build(FunctionNode fn) {
|
||||||
// 创建 IRFunction 并包装到上下文
|
// 创建 IRFunction 并包装到上下文
|
||||||
IRFunction irFunc = new IRFunction(fn.name());
|
IRFunction irFunction = new IRFunction(fn.name());
|
||||||
IRContext ctx = new IRContext(irFunc);
|
IRContext irContext = new IRContext(irFunction);
|
||||||
|
|
||||||
// 声明函数参数
|
// 声明函数参数
|
||||||
for (ParameterNode p : fn.parameters()) {
|
for (ParameterNode parameterNode : fn.parameters()) {
|
||||||
// 在当前作用域中声明参数名称对应的虚拟寄存器
|
// 在当前作用域中声明参数名称对应的虚拟寄存器
|
||||||
ctx.getScope().declare(p.name());
|
irContext.getScope().declare(parameterNode.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用 StatementBuilder 遍历并生成函数体所有语句的 IR
|
// 使用 StatementBuilder 遍历并生成函数体所有语句的 IR
|
||||||
StatementBuilder sb = new StatementBuilder(ctx);
|
StatementBuilder sb = new StatementBuilder(irContext);
|
||||||
for (StatementNode s : fn.body()) {
|
for (StatementNode statementNode : fn.body()) {
|
||||||
sb.build(s);
|
sb.build(statementNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 返回构建好的 IRFunction
|
// 返回构建好的 IRFunction
|
||||||
return irFunc;
|
return irFunction;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,18 +30,18 @@ public final class IRProgramBuilder {
|
|||||||
*/
|
*/
|
||||||
public IRProgram buildProgram(List<Node> roots) {
|
public IRProgram buildProgram(List<Node> roots) {
|
||||||
IRProgram irProgram = new IRProgram();
|
IRProgram irProgram = new IRProgram();
|
||||||
for (Node n : roots) {
|
for (Node node : roots) {
|
||||||
switch (n) {
|
switch (node) {
|
||||||
case ModuleNode m ->
|
case ModuleNode moduleNode ->
|
||||||
// 模块节点:将模块内的所有函数加入程序
|
// 模块节点:将模块内的所有函数加入程序
|
||||||
m.functions().forEach(f -> irProgram.add(buildFunction(f)));
|
moduleNode.functions().forEach(f -> irProgram.add(buildFunction(f)));
|
||||||
case FunctionNode f ->
|
case FunctionNode f ->
|
||||||
// 顶层函数节点
|
// 顶层函数节点
|
||||||
irProgram.add(buildFunction(f));
|
irProgram.add(buildFunction(f));
|
||||||
case StatementNode s ->
|
case StatementNode statementNode ->
|
||||||
// 顶层脚本语句,包装为 _start 函数
|
// 顶层脚本语句,包装为 _start 函数
|
||||||
irProgram.add(buildFunction(wrapTopLevel(s)));
|
irProgram.add(buildFunction(wrapTopLevel(statementNode)));
|
||||||
case null, default -> throw new IllegalStateException("Unsupported top-level node: " + n);
|
case null, default -> throw new IllegalStateException("Unsupported top-level node: " + node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return irProgram;
|
return irProgram;
|
||||||
@ -50,11 +50,11 @@ public final class IRProgramBuilder {
|
|||||||
/**
|
/**
|
||||||
* 使用 FunctionBuilder 构建 IRFunction。
|
* 使用 FunctionBuilder 构建 IRFunction。
|
||||||
*
|
*
|
||||||
* @param fn AST 中的 FunctionNode
|
* @param functionNode AST 中的 FunctionNode
|
||||||
* @return 构建完成的 IRFunction
|
* @return 构建完成的 IRFunction
|
||||||
*/
|
*/
|
||||||
private IRFunction buildFunction(FunctionNode fn) {
|
private IRFunction buildFunction(FunctionNode functionNode) {
|
||||||
return new FunctionBuilder().build(fn);
|
return new FunctionBuilder().build(functionNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -22,43 +22,43 @@ public class InstructionFactory {
|
|||||||
/**
|
/**
|
||||||
* 生成加载整数常量的指令,并返回对应的结果寄存器。
|
* 生成加载整数常量的指令,并返回对应的结果寄存器。
|
||||||
*
|
*
|
||||||
* @param ctx 当前 IR 构建上下文
|
* @param irContext 当前 IR 构建上下文
|
||||||
* @param value 要加载的整数值
|
* @param value 要加载的整数值
|
||||||
* @return 存放常量的虚拟寄存器
|
* @return 存放常量的虚拟寄存器
|
||||||
*/
|
*/
|
||||||
public static IRVirtualRegister loadConst(IRContext ctx, int value) {
|
public static IRVirtualRegister loadConst(IRContext irContext, int value) {
|
||||||
IRVirtualRegister vr = ctx.newRegister();
|
IRVirtualRegister irVirtualRegister = irContext.newRegister();
|
||||||
LoadConstInstruction instr = new LoadConstInstruction(vr, new IRConstant(value));
|
LoadConstInstruction instr = new LoadConstInstruction(irVirtualRegister, new IRConstant(value));
|
||||||
ctx.addInstruction(instr);
|
irContext.addInstruction(instr);
|
||||||
return vr;
|
return irVirtualRegister;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成二元运算指令,并返回运算结果寄存器。
|
* 生成二元运算指令,并返回运算结果寄存器。
|
||||||
*
|
*
|
||||||
* @param ctx 当前 IR 构建上下文
|
* @param irContext 当前 IR 构建上下文
|
||||||
* @param op 要执行的 IR 运算符
|
* @param irOpCode 要执行的 IR 运算符
|
||||||
* @param left 左操作数寄存器
|
* @param left 左操作数寄存器
|
||||||
* @param right 右操作数寄存器
|
* @param right 右操作数寄存器
|
||||||
* @return 存放运算结果的虚拟寄存器
|
* @return 存放运算结果的虚拟寄存器
|
||||||
*/
|
*/
|
||||||
public static IRVirtualRegister binOp(IRContext ctx, IROpCode op,
|
public static IRVirtualRegister binOp(IRContext irContext, IROpCode irOpCode,
|
||||||
IRVirtualRegister left,
|
IRVirtualRegister left,
|
||||||
IRVirtualRegister right) {
|
IRVirtualRegister right) {
|
||||||
IRVirtualRegister dest = ctx.newRegister();
|
IRVirtualRegister dest = irContext.newRegister();
|
||||||
BinaryOperationInstruction instr = new BinaryOperationInstruction(op, dest, left, right);
|
BinaryOperationInstruction binaryOperationInstruction = new BinaryOperationInstruction(irOpCode, dest, left, right);
|
||||||
ctx.addInstruction(instr);
|
irContext.addInstruction(binaryOperationInstruction);
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成带返回值的返回指令。
|
* 生成带返回值的返回指令。
|
||||||
*
|
*
|
||||||
* @param ctx 当前 IR 构建上下文
|
* @param irContext 当前 IR 构建上下文
|
||||||
* @param value 需要返回的虚拟寄存器
|
* @param value 需要返回的虚拟寄存器
|
||||||
*/
|
*/
|
||||||
public static void ret(IRContext ctx, IRVirtualRegister value) {
|
public static void ret(IRContext irContext, IRVirtualRegister value) {
|
||||||
ctx.addInstruction(new ReturnInstruction(value));
|
irContext.addInstruction(new ReturnInstruction(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -45,44 +45,44 @@ public class StatementBuilder {
|
|||||||
* <li>ReturnNode:如带返回表达式,则生成带返回值的指令;否则生成无返回值指令</li>
|
* <li>ReturnNode:如带返回表达式,则生成带返回值的指令;否则生成无返回值指令</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @param stmt 要构建的语句 AST 节点
|
* @param statementNode 要构建的语句 AST 节点
|
||||||
* @throws IllegalStateException 如果遇到不支持的语句类型,则抛出此异常
|
* @throws IllegalStateException 如果遇到不支持的语句类型,则抛出此异常
|
||||||
*/
|
*/
|
||||||
public void build(StatementNode stmt) {
|
public void build(StatementNode statementNode) {
|
||||||
// 表达式语句: 仅生成表达式对应的 IR 指令,不关注结果
|
// 表达式语句: 仅生成表达式对应的 IR 指令,不关注结果
|
||||||
if (stmt instanceof ExpressionStatementNode(
|
if (statementNode instanceof ExpressionStatementNode(
|
||||||
org.jcnc.snow.compiler.parser.ast.base.ExpressionNode expression)) {
|
org.jcnc.snow.compiler.parser.ast.base.ExpressionNode expression)) {
|
||||||
exprBuilder.build(expression);
|
exprBuilder.build(expression);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 赋值语句: 先生成右侧表达式指令,获取寄存器,再声明或更新变量的值绑定
|
// 赋值语句: 先生成右侧表达式指令,获取寄存器,再声明或更新变量的值绑定
|
||||||
if (stmt instanceof AssignmentNode(
|
if (statementNode instanceof AssignmentNode(
|
||||||
String variable, org.jcnc.snow.compiler.parser.ast.base.ExpressionNode value
|
String variable, org.jcnc.snow.compiler.parser.ast.base.ExpressionNode value
|
||||||
)) {
|
)) {
|
||||||
IRVirtualRegister vr = exprBuilder.build(value);
|
IRVirtualRegister irVirtualRegister = exprBuilder.build(value);
|
||||||
if (ctx.getScope().lookup(variable) == null) {
|
if (ctx.getScope().lookup(variable) == null) {
|
||||||
// 首次赋值:声明变量并绑定寄存器
|
// 首次赋值:声明变量并绑定寄存器
|
||||||
ctx.getScope().declare(variable, vr);
|
ctx.getScope().declare(variable, irVirtualRegister);
|
||||||
} else {
|
} else {
|
||||||
// 变量已存在:更新绑定的寄存器
|
// 变量已存在:更新绑定的寄存器
|
||||||
ctx.getScope().put(variable, vr);
|
ctx.getScope().put(variable, irVirtualRegister);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 声明语句: 如有初始化表达式,则先构建初始化值并声明;否则直接声明变量
|
// 声明语句: 如有初始化表达式,则先构建初始化值并声明;否则直接声明变量
|
||||||
if (stmt instanceof DeclarationNode dn) {
|
if (statementNode instanceof DeclarationNode declarationNode) {
|
||||||
if (dn.getInitializer().isPresent()) {
|
if (declarationNode.getInitializer().isPresent()) {
|
||||||
IRVirtualRegister init = exprBuilder.build(dn.getInitializer().get());
|
IRVirtualRegister irVirtualRegister = exprBuilder.build(declarationNode.getInitializer().get());
|
||||||
ctx.getScope().declare(dn.getName(), init);
|
ctx.getScope().declare(declarationNode.getName(), irVirtualRegister);
|
||||||
} else {
|
} else {
|
||||||
ctx.getScope().declare(dn.getName());
|
ctx.getScope().declare(declarationNode.getName());
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 返回语句: 区分有返回值和无返回值两种情况
|
// 返回语句: 区分有返回值和无返回值两种情况
|
||||||
if (stmt instanceof ReturnNode rn) {
|
if (statementNode instanceof ReturnNode returnNode) {
|
||||||
if (rn.getExpression().isPresent()) {
|
if (returnNode.getExpression().isPresent()) {
|
||||||
IRVirtualRegister vr = exprBuilder.build(rn.getExpression().get());
|
IRVirtualRegister vr = exprBuilder.build(returnNode.getExpression().get());
|
||||||
InstructionFactory.ret(ctx, vr);
|
InstructionFactory.ret(ctx, vr);
|
||||||
} else {
|
} else {
|
||||||
InstructionFactory.retVoid(ctx);
|
InstructionFactory.retVoid(ctx);
|
||||||
@ -90,6 +90,6 @@ public class StatementBuilder {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 不支持的语句类型
|
// 不支持的语句类型
|
||||||
throw new IllegalStateException("Unsupported statement: " + stmt.getClass().getSimpleName());
|
throw new IllegalStateException("Unsupported statement: " + statementNode.getClass().getSimpleName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -53,10 +53,10 @@ public class IRFunction {
|
|||||||
/**
|
/**
|
||||||
* 向函数体中添加一条中间表示指令。
|
* 向函数体中添加一条中间表示指令。
|
||||||
*
|
*
|
||||||
* @param inst 要添加的 IRInstruction 对象
|
* @param irInstruction 要添加的 IRInstruction 对象
|
||||||
*/
|
*/
|
||||||
public void add(IRInstruction inst) {
|
public void add(IRInstruction irInstruction) {
|
||||||
body.add(inst);
|
body.add(irInstruction);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -17,10 +17,10 @@ public final class IRProgram {
|
|||||||
/**
|
/**
|
||||||
* 向 IRProgram 中添加一个函数。
|
* 向 IRProgram 中添加一个函数。
|
||||||
*
|
*
|
||||||
* @param fn 要添加的 IRFunction 对象
|
* @param irFunction 要添加的 IRFunction 对象
|
||||||
*/
|
*/
|
||||||
public void add(IRFunction fn) {
|
public void add(IRFunction irFunction) {
|
||||||
functions.add(fn);
|
functions.add(irFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -13,7 +13,7 @@ import org.jcnc.snow.compiler.ir.value.IRVirtualRegister;
|
|||||||
* <li>{@link IRConstant} —— 常量值,表示字面量或计算常量</li>
|
* <li>{@link IRConstant} —— 常量值,表示字面量或计算常量</li>
|
||||||
* <li>{@link IRLabel} —— 跳转标签,用于控制流跳转目标</li>
|
* <li>{@link IRLabel} —— 跳转标签,用于控制流跳转目标</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
* <p>
|
||||||
* 本接口使用 sealed 限定,明确列出了所有允许的实现子类型,
|
* 本接口使用 sealed 限定,明确列出了所有允许的实现子类型,
|
||||||
* 有助于在编译期进行穷尽性对齐和类型安全检查。
|
* 有助于在编译期进行穷尽性对齐和类型安全检查。
|
||||||
*/
|
*/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user