Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class CompiledFunction {
public CompiledFunction(Symbol.FunctionTypeSymbol functionSymbol) {
AST.FuncDecl funcDecl = (AST.FuncDecl) functionSymbol.functionDecl;
this.functionType = (Type.TypeFunction) functionSymbol.type;
this.registerPool = new RegisterPool("%ret", functionType == null?null:functionType.returnType);
this.registerPool = new RegisterPool();
setVirtualRegisters(funcDecl.scope);
this.bid = 0;
this.entry = this.currentBlock = createBlock();
Expand All @@ -54,7 +54,7 @@ public CompiledFunction(Symbol.FunctionTypeSymbol functionSymbol) {

public CompiledFunction(Type.TypeFunction functionType) {
this.functionType = (Type.TypeFunction) functionType;
this.registerPool = new RegisterPool("%ret", functionType == null?null:functionType.returnType);
this.registerPool = new RegisterPool();
this.bid = 0;
this.entry = this.currentBlock = createBlock();
this.exit = createBlock();
Expand Down Expand Up @@ -115,7 +115,7 @@ private void compileReturn(AST.ReturnStmt returnStmt) {
if (isIndexed)
codeIndexedLoad();
if (virtualStack.size() == 1)
code(new Instruction.Return(pop(), registerPool.returnRegister));
code(new Instruction.Ret(pop()));
else if (virtualStack.size() > 1)
throw new CompilerException("Virtual stack has more than one item at return");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,32 @@ public StringBuilder toStr(StringBuilder sb) {
.append(sourceOperand);
}
}
public static class Return extends Move {
public Return(Operand from, Register reg) {
super(from, new Operand.ReturnRegisterOperand(reg));
public static class Ret extends Instruction {
public final Operand value;
public Ret(Operand value) {
this.value = value;
}
@Override
public boolean usesVars() {
return value != null && value instanceof Operand.RegisterOperand registerOperand;
}
@Override
public List<Register> uses() {
if (value != null && value instanceof Operand.RegisterOperand registerOperand)
return List.of(registerOperand.reg);
return Collections.emptyList();
}
@Override
public void replaceUses(Register[] newUses) {
if (newUses.length > 0)
value.replaceRegister(newUses[0]);
}
@Override
public StringBuilder toStr(StringBuilder sb) {
sb.append("ret");
if (value != null)
sb.append(" ").append(value);
return sb;
}
}
public static class Unary extends Instruction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@
*/
public class RegisterPool {
private final ArrayList<Register> registers = new ArrayList<>();
public final Register returnRegister;

public RegisterPool(String returnVarName, Type type) {
if (type != null)
returnRegister = newReg(returnVarName, type);
else
returnRegister = null;
}
public Register getReg(int regNumber) {
return registers.get(regNumber);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@ public Value interpret(ExecutionStack execStack, Frame frame) {
ip++;
instruction = currentBlock.instructions.get(ip);
switch (instruction) {
case Instruction.Return returnInst -> {
if (returnInst.from instanceof Operand.ConstantOperand constantOperand) {
case Instruction.Ret retInst -> {
if (retInst.value instanceof Operand.ConstantOperand constantOperand) {
execStack.stack[base] = new Value.IntegerValue(constantOperand.value);
}
else if (returnInst.from instanceof Operand.RegisterOperand registerOperand) {
else if (retInst.value instanceof Operand.RegisterOperand registerOperand) {
execStack.stack[base] = execStack.stack[base+registerOperand.slot()];
}
else throw new IllegalStateException();
returnValue = execStack.stack[base];
}

case Instruction.Move moveInst -> {
if (moveInst.to instanceof Operand.RegisterOperand toReg) {
if (moveInst.from instanceof Operand.RegisterOperand fromReg) {
Expand Down Expand Up @@ -98,11 +99,7 @@ else if (cbrInst.condition instanceof Operand.ConstantOperand constantOperand) {
case Instruction.Call callInst -> {
// Copy args to new frame
int baseReg = base+currentFunction.frameSize();
int offset = 0;
// In this version return reg
if (!(callInst.callee.returnType instanceof Type.TypeVoid))
offset = 1;
int reg = baseReg + offset;
int reg = baseReg;
for (Operand.RegisterOperand arg: callInst.args) {
execStack.stack[base + reg] = execStack.stack[base + arg.slot()];
reg += 1;
Expand Down
Loading
Loading