1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-06-02 00:41:42 +00:00

Implemented fib - and fixed alias elimination issue

This commit is contained in:
jespergravgaard 2017-05-15 21:54:32 +02:00
parent 62533afc6a
commit 7e043de594
5 changed files with 64 additions and 13 deletions

View File

@ -1,6 +1,7 @@
package dk.camelot64.kickc.icl;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/** Compiler Pass eliminating alias assignments */
@ -26,11 +27,48 @@ public class Pass2AliasElimination extends Pass2Optimization {
return (aliases.size()>0);
}
private Map<Variable, Variable> findAliases() {
Map<Variable, Variable> candidates = findAliasesCandidates();
cleanupCandidates(candidates);
return candidates;
}
// Remove all candidates that are used after assignment in phi blocks
private void cleanupCandidates(Map<Variable, Variable> candidates) {
Iterator<Variable> aliasIt = candidates.keySet().iterator();
while (aliasIt.hasNext()) {
final Variable alias = aliasIt.next();
final Variable variable = candidates.get(alias);
final Boolean[] rMatch = {false};
final Boolean[] lMatch = {false};
ControlFlowGraphBaseVisitor<Void> candidateEliminator = new ControlFlowGraphBaseVisitor<Void>() {
@Override
public Void visitPhi(StatementPhi phi) {
for (StatementPhi.PreviousSymbol previousSymbol : phi.getPreviousVersions()) {
if(previousSymbol.getRValue().equals(variable)) {
rMatch[0] = true;
break;
}
}
if(phi.getLValue().equals(alias)) {
lMatch[0] = true;
}
return null;
}
};
candidateEliminator.visitGraph(getGraph());
if(rMatch[0] && lMatch[0]) {
System.out.println("Alias candidate removed " + alias + " " + variable);
aliasIt.remove();
}
}
}
/**
* Find variables that have constant values.
* @return Map from Variable to the Constant value
*/
private Map<Variable, Variable> findAliases() {
private Map<Variable, Variable> findAliasesCandidates() {
final Map<Variable, Variable> aliases = new HashMap<>();
ControlFlowGraphBaseVisitor<Void> visitor = new ControlFlowGraphBaseVisitor<Void>() {
@Override

View File

@ -56,7 +56,7 @@ public class Pass4CodeGeneration {
asm.addAsm(" // " + statement + " // " + asmFragment.getSignature());
asmFragment.generateAsm(asm);
} else {
asm.addAsm(" // TODO: " + statement);
throw new RuntimeException("Statement not supported "+statement);
}
}
@ -79,7 +79,7 @@ public class Pass4CodeGeneration {
StatementPhi phi = (StatementPhi) statement;
for (StatementPhi.PreviousSymbol previousSymbol : phi.getPreviousVersions()) {
if (previousSymbol.getBlock().equals(predecessor)) {
genAsmMove(asm, previousSymbol.getRValue(), phi.getLValue());
genAsmMove(asm, phi.getLValue(), previousSymbol.getRValue());
break;
}
}
@ -87,16 +87,9 @@ public class Pass4CodeGeneration {
genAsmJump(asm, block.getLabel().getName());
}
/**
* Generate an assembler move from an Rvalue to an LValue
*
* @param asm The assembler sequence
* @param rValue The rValue
* @param lValue The lValue
*/
private void genAsmMove(AsmSequence asm, RValue rValue, LValue lValue) {
private void genAsmMove(AsmSequence asm, LValue lValue, RValue rValue) {
AsmFragment asmFragment = new AsmFragment(lValue, rValue, symbols);
asm.addAsm(" // " + rValue + " = " + lValue + " // " + asmFragment.getSignature());
asm.addAsm(" // " + lValue + " = " + rValue + " // " + asmFragment.getSignature());
asmFragment.generateAsm(asm);
}

View File

@ -0,0 +1,8 @@
lda {zpby1}
cmp #{coby1}
bcc !t+
beq !t+
!f: lda #0
jmp !d+
!t: lda #$ff
!d: sta {zpbo1}

View File

@ -14,7 +14,7 @@ import java.util.List;
/** Test my KickC Grammar */
public class Main {
public static void main(String[] args) throws IOException {
CharStream input = CharStreams.fromFileName("src/dk/camelot64/kickc/test/test.kc");
CharStream input = CharStreams.fromFileName("src/dk/camelot64/kickc/test/fib.kc");
System.out.println(input.toString());
KickCLexer lexer = new KickCLexer(input);
KickCParser parser = new KickCParser(new CommonTokenStream(lexer));
@ -51,6 +51,8 @@ public class Main {
if (stepOptimized) {
System.out.println("Succesful optimization "+optimization);
optimized = true;
System.out.println("CONTROL FLOW GRAPH");
System.out.println(controlFlowGraph.toString());
}
}
}

View File

@ -0,0 +1,10 @@
byte num = 0;
byte num2 = 1;
byte i = 0;
byte fib = 0;
while(i<=10) {
fib = num + num2;
num = num2;
num2 = fib;
i = i + 1;
}