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 @@ -41,6 +41,7 @@ public CompiledFunction(Symbol.FunctionTypeSymbol functionSymbol, TypeDictionary
AST.FuncDecl funcDecl = (AST.FuncDecl) functionSymbol.functionDecl;
this.functionType = (Type.TypeFunction) functionSymbol.type;
this.registerPool = new RegisterPool();
// Incremental SSA is an optional feature
this.issa = (options != null && options.contains(Options.ISSA)) ? new IncrementalSSABraun(this) : new NoopIncrementalSSA();
setVirtualRegisters(funcDecl.scope);
this.BID = 0;
Expand All @@ -49,6 +50,7 @@ public CompiledFunction(Symbol.FunctionTypeSymbol functionSymbol, TypeDictionary
this.currentBreakTarget = null;
this.currentContinueTarget = null;
this.typeDictionary = typeDictionary;
issa.sealBlock(entry); // Incremental SSA is an optional feature
generateArgInstructions(funcDecl.scope);
compileStatement(funcDecl.block);
exitBlockIfNeeded();
Expand Down Expand Up @@ -135,7 +137,7 @@ else if (virtualStack.size() > 1)

public void code(Instruction instruction) {
currentBlock.add(instruction);
instruction.block = currentBlock;
assert instruction.block == currentBlock;
}

private void compileStatement(AST.Stmt statement) {
Expand Down Expand Up @@ -225,12 +227,12 @@ private void compileWhile(AST.WhileStmt whileStmt) {
codeIndexedLoad();
codeCBR(currentBlock, pop(), bodyBlock, exitBlock);
assert vstackEmpty();
startSealedBlock(bodyBlock); // ISSA If we seal this here fib test fails, wrong code generated, why?
startSealedBlock(bodyBlock); // ISSA Body is immediately sealed as no new predecessors possible
compileStatement(whileStmt.stmt);
if (!isBlockTerminated(currentBlock))
jumpTo(loopHead);
issa.sealBlock(loopHead);
startSealedBlock(exitBlock);
startSealedBlock(exitBlock); // ISSA seal exit block (breaks already done)
currentContinueTarget = savedContinueTarget;
currentBreakTarget = savedBreakTarget;
}
Expand Down Expand Up @@ -450,7 +452,7 @@ private boolean codeBoolean(AST.BinaryExpr binaryExpr) {
}
startSealedBlock(l1); // ISSA seal immediately
compileExpr(binaryExpr.expr2);
var temp = ensureTemp();
var temp = ensureTemp(); // Normally temps are SSA but this temp gets two assignments, thus must be included during SSA conversion
jumpTo(l3);
startSealedBlock(l2); // ISSA seal immediately
// Below we must write to the same temp
Expand All @@ -464,7 +466,7 @@ private boolean codeBoolean(AST.BinaryExpr binaryExpr) {
private boolean compileBinaryExpr(AST.BinaryExpr binaryExpr) {
String opCode = binaryExpr.op.str;
if (opCode.equals("&&") ||
opCode.equals("||")) {
opCode.equals("||")) {
return codeBoolean(binaryExpr);
}
boolean indexed = compileExpr(binaryExpr.expr1);
Expand All @@ -476,7 +478,7 @@ private boolean compileBinaryExpr(AST.BinaryExpr binaryExpr) {
Operand right = pop();
Operand left = pop();
if (left instanceof Operand.NullConstantOperand &&
right instanceof Operand.NullConstantOperand) {
right instanceof Operand.NullConstantOperand) {
long value = 0;
switch (opCode) {
case "==": value = 1; break;
Expand Down Expand Up @@ -602,15 +604,17 @@ private Operand top() {
return virtualStack.getLast();
}

private boolean vstackEmpty() {
return virtualStack.isEmpty();
}

private Operand.TempRegisterOperand codeIndexedLoad() {
Operand indexed = pop();
var temp = createTemp(indexed.type);
if (indexed instanceof Operand.LoadIndexedOperand loadIndexedOperand) {
if (indexed instanceof Operand.LoadIndexedOperand loadIndexedOperand)
codeArrayLoad(loadIndexedOperand, temp);
}
else if (indexed instanceof Operand.LoadFieldOperand loadFieldOperand) {
else if (indexed instanceof Operand.LoadFieldOperand loadFieldOperand)
codeGetField(loadFieldOperand, temp);
}
else
codeMove(indexed, temp);
return temp;
Expand All @@ -619,12 +623,10 @@ else if (indexed instanceof Operand.LoadFieldOperand loadFieldOperand) {
private void codeIndexedStore() {
Operand value = pop();
Operand indexed = pop();
if (indexed instanceof Operand.LoadIndexedOperand loadIndexedOperand) {
if (indexed instanceof Operand.LoadIndexedOperand loadIndexedOperand)
codeArrayStore(value, loadIndexedOperand);
}
else if (indexed instanceof Operand.LoadFieldOperand loadFieldOperand) {
else if (indexed instanceof Operand.LoadFieldOperand loadFieldOperand)
codeSetField(value, loadFieldOperand);
}
else
codeMove(value, indexed);
}
Expand All @@ -638,6 +640,15 @@ else if (type instanceof Type.TypeStruct typeStruct)
throw new CompilerException("Unexpected type: " + type);
}

private void codeStoreAppend() {
var operand = issa.read(pop());
Operand.RegisterOperand arrayOperand = (Operand.RegisterOperand) issa.read(top());
var insn = new Instruction.AStoreAppend(arrayOperand, operand);
issa.recordUse(arrayOperand, insn);
issa.recordUse(operand, insn);
code(insn);
}

private void codeNewArray(Type.TypeArray typeArray) {
var temp = createTemp(typeArray);
var target = (Operand.RegisterOperand) issa.write(temp);
Expand All @@ -654,15 +665,6 @@ private void codeNewStruct(Type.TypeStruct typeStruct) {
code(insn);
}

private void codeStoreAppend() {
var operand = issa.read(pop());
Operand.RegisterOperand arrayOperand = (Operand.RegisterOperand) issa.read(top());
var insn = new Instruction.AStoreAppend(arrayOperand, operand);
issa.recordUse(arrayOperand, insn);
issa.recordUse(operand, insn);
code(insn);
}

private void codeArg(Operand.LocalRegisterOperand target) {
var newtarget = (Operand.RegisterOperand) issa.write(target);
var insn = new Instruction.ArgInstruction(newtarget);
Expand Down Expand Up @@ -774,10 +776,6 @@ private void codeSetField(Operand value, Operand.LoadFieldOperand loadFieldOpera
code(insn);
}

private boolean vstackEmpty() {
return virtualStack.isEmpty();
}

public StringBuilder toStr(StringBuilder sb, boolean verbose) {
if (verbose) {
sb.append(this.functionType.describe()).append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ void insertPhis() {
while (b != null) {
visited.set(b.bid);
for (BasicBlock d: b.dominationFrontier) {
if (d == function.exit) // The exit block does not need any phis as it has no instructions
continue;
// insert phi for x in d
d.insertPhiFor(x);
if (!visited.get(d.bid))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public enum Options {

public static final EnumSet<Options> NONE = EnumSet.noneOf(Options.class);
public static final EnumSet<Options> OPT = EnumSet.of(Options.OPTIMIZE,Options.SCCP,Options.CCP,Options.REGALLOC);
public static final EnumSet<Options> OPT_ISSA = EnumSet.of(Options.OPTIMIZE,Options.ISSA,Options.SCCP,Options.CCP,Options.REGALLOC);
public static final EnumSet<Options> VERBOSE = EnumSet.range(DUMP_INITIAL_IR, DUMP_POST_CHAITIN_IR);
public static final EnumSet<Options> OPT_VERBOSE = EnumSet.range(OPTIMIZE, DUMP_POST_CHAITIN_IR);
}
Loading