Skip to content

Commit c3853df

Browse files
Merge pull request #10 from CompilerProgramming/refactor
Revise the return instruction
2 parents 8ee62df + 239ce67 commit c3853df

File tree

7 files changed

+277
-267
lines changed

7 files changed

+277
-267
lines changed

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class CompiledFunction {
3939
public CompiledFunction(Symbol.FunctionTypeSymbol functionSymbol) {
4040
AST.FuncDecl funcDecl = (AST.FuncDecl) functionSymbol.functionDecl;
4141
this.functionType = (Type.TypeFunction) functionSymbol.type;
42-
this.registerPool = new RegisterPool("%ret", functionType == null?null:functionType.returnType);
42+
this.registerPool = new RegisterPool();
4343
setVirtualRegisters(funcDecl.scope);
4444
this.bid = 0;
4545
this.entry = this.currentBlock = createBlock();
@@ -54,7 +54,7 @@ public CompiledFunction(Symbol.FunctionTypeSymbol functionSymbol) {
5454

5555
public CompiledFunction(Type.TypeFunction functionType) {
5656
this.functionType = (Type.TypeFunction) functionType;
57-
this.registerPool = new RegisterPool("%ret", functionType == null?null:functionType.returnType);
57+
this.registerPool = new RegisterPool();
5858
this.bid = 0;
5959
this.entry = this.currentBlock = createBlock();
6060
this.exit = createBlock();
@@ -115,7 +115,7 @@ private void compileReturn(AST.ReturnStmt returnStmt) {
115115
if (isIndexed)
116116
codeIndexedLoad();
117117
if (virtualStack.size() == 1)
118-
code(new Instruction.Return(pop(), registerPool.returnRegister));
118+
code(new Instruction.Ret(pop()));
119119
else if (virtualStack.size() > 1)
120120
throw new CompilerException("Virtual stack has more than one item at return");
121121
}

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,32 @@ public StringBuilder toStr(StringBuilder sb) {
347347
.append(sourceOperand);
348348
}
349349
}
350-
public static class Return extends Move {
351-
public Return(Operand from, Register reg) {
352-
super(from, new Operand.ReturnRegisterOperand(reg));
350+
public static class Ret extends Instruction {
351+
public final Operand value;
352+
public Ret(Operand value) {
353+
this.value = value;
354+
}
355+
@Override
356+
public boolean usesVars() {
357+
return value != null && value instanceof Operand.RegisterOperand registerOperand;
358+
}
359+
@Override
360+
public List<Register> uses() {
361+
if (value != null && value instanceof Operand.RegisterOperand registerOperand)
362+
return List.of(registerOperand.reg);
363+
return Collections.emptyList();
364+
}
365+
@Override
366+
public void replaceUses(Register[] newUses) {
367+
if (newUses.length > 0)
368+
value.replaceRegister(newUses[0]);
369+
}
370+
@Override
371+
public StringBuilder toStr(StringBuilder sb) {
372+
sb.append("ret");
373+
if (value != null)
374+
sb.append(" ").append(value);
375+
return sb;
353376
}
354377
}
355378
public static class Unary extends Instruction {

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/RegisterPool.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,7 @@
1414
*/
1515
public class RegisterPool {
1616
private final ArrayList<Register> registers = new ArrayList<>();
17-
public final Register returnRegister;
1817

19-
public RegisterPool(String returnVarName, Type type) {
20-
if (type != null)
21-
returnRegister = newReg(returnVarName, type);
22-
else
23-
returnRegister = null;
24-
}
2518
public Register getReg(int regNumber) {
2619
return registers.get(regNumber);
2720
}

optvm/src/main/java/com/compilerprogramming/ezlang/interpreter/Interpreter.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,17 @@ public Value interpret(ExecutionStack execStack, Frame frame) {
4444
ip++;
4545
instruction = currentBlock.instructions.get(ip);
4646
switch (instruction) {
47-
case Instruction.Return returnInst -> {
48-
if (returnInst.from instanceof Operand.ConstantOperand constantOperand) {
47+
case Instruction.Ret retInst -> {
48+
if (retInst.value instanceof Operand.ConstantOperand constantOperand) {
4949
execStack.stack[base] = new Value.IntegerValue(constantOperand.value);
5050
}
51-
else if (returnInst.from instanceof Operand.RegisterOperand registerOperand) {
51+
else if (retInst.value instanceof Operand.RegisterOperand registerOperand) {
5252
execStack.stack[base] = execStack.stack[base+registerOperand.slot()];
5353
}
5454
else throw new IllegalStateException();
5555
returnValue = execStack.stack[base];
5656
}
57+
5758
case Instruction.Move moveInst -> {
5859
if (moveInst.to instanceof Operand.RegisterOperand toReg) {
5960
if (moveInst.from instanceof Operand.RegisterOperand fromReg) {
@@ -98,11 +99,7 @@ else if (cbrInst.condition instanceof Operand.ConstantOperand constantOperand) {
9899
case Instruction.Call callInst -> {
99100
// Copy args to new frame
100101
int baseReg = base+currentFunction.frameSize();
101-
int offset = 0;
102-
// In this version return reg
103-
if (!(callInst.callee.returnType instanceof Type.TypeVoid))
104-
offset = 1;
105-
int reg = baseReg + offset;
102+
int reg = baseReg;
106103
for (Operand.RegisterOperand arg: callInst.args) {
107104
execStack.stack[base + reg] = execStack.stack[base + arg.slot()];
108105
reg += 1;

0 commit comments

Comments
 (0)