diff --git a/src/dk/camelot64/kickc/asm/fragment/zpby1=coby1_plus_zpby2.asm b/src/dk/camelot64/kickc/asm/fragment/zpby1=coby1_plus_zpby2.asm new file mode 100644 index 000000000..4354f71c0 --- /dev/null +++ b/src/dk/camelot64/kickc/asm/fragment/zpby1=coby1_plus_zpby2.asm @@ -0,0 +1,4 @@ +lda #{coby1} +clc +adc {zpby2} +sta {zpby1} \ No newline at end of file diff --git a/src/dk/camelot64/kickc/icl/Pass3CodeGeneration.java b/src/dk/camelot64/kickc/icl/Pass3CodeGeneration.java index 088eca458..d7e327aa7 100644 --- a/src/dk/camelot64/kickc/icl/Pass3CodeGeneration.java +++ b/src/dk/camelot64/kickc/icl/Pass3CodeGeneration.java @@ -23,7 +23,7 @@ public class Pass3CodeGeneration { // Generate entry points (if needed) genBlockEntryPoints(asm, block); // Generate label - asm.addLabel(block.getLabel().getFullName().replace('@', 'B')); + asm.addLabel(block.getLabel().getFullName().replace('@', 'B').replace(':','_')); // Generate statements genStatements(asm, block); // Generate exit @@ -32,7 +32,7 @@ public class Pass3CodeGeneration { if (defaultSuccessor.hasPhiStatements()) { genBlockPhiTransition(asm, block, defaultSuccessor); } - asm.addInstruction("JMP", AsmAddressingMode.ABS, defaultSuccessor.getLabel().getFullName().replace('@', 'B')); + asm.addInstruction("JMP", AsmAddressingMode.ABS, defaultSuccessor.getLabel().getFullName().replace('@', 'B').replace(':','_')); } } return asm; @@ -93,14 +93,14 @@ public class Pass3CodeGeneration { for (ControlFlowBlock predecessor : graph.getPredecessors(block)) { if (block.getLabel().equals(predecessor.getConditionalSuccessor())) { genBlockPhiTransition(asm, predecessor, block); - asm.addInstruction("JMP", AsmAddressingMode.ABS, block.getLabel().getFullName().replace('@', 'B')); + asm.addInstruction("JMP", AsmAddressingMode.ABS, block.getLabel().getFullName().replace('@', 'B').replace(':','_')); } } } } private void genBlockPhiTransition(AsmProgram asm, ControlFlowBlock fromBlock, ControlFlowBlock toBlock) { - asm.addLabel((toBlock.getLabel().getFullName() + "_from_" + fromBlock.getLabel().getLocalName()).replace('@', 'B')); + asm.addLabel((toBlock.getLabel().getFullName() + "_from_" + fromBlock.getLabel().getLocalName()).replace('@', 'B').replace(':','_')); for (Statement statement : toBlock.getStatements()) { if (!(statement instanceof StatementPhi)) { // No more phi statements to handle diff --git a/src/dk/camelot64/kickc/icl/Pass3RegisterAllocation.java b/src/dk/camelot64/kickc/icl/Pass3RegisterAllocation.java index f0aa11a7b..81f4f5afb 100644 --- a/src/dk/camelot64/kickc/icl/Pass3RegisterAllocation.java +++ b/src/dk/camelot64/kickc/icl/Pass3RegisterAllocation.java @@ -1,10 +1,13 @@ package dk.camelot64.kickc.icl; -/** Register Allocation for variables */ +/** + * Register Allocation for variables + */ public class Pass3RegisterAllocation { private ControlFlowGraph graph; private Scope symbols; + int currentZp = 2; public Pass3RegisterAllocation(ControlFlowGraph graph, Scope symbols) { this.graph = graph; @@ -13,23 +16,7 @@ public class Pass3RegisterAllocation { public void allocate() { RegisterAllocation allocation = new RegisterAllocation(); - int currentZp = 2; - for (Variable var : symbols.getAllVariables()) { - if(var instanceof VariableIntermediate || var instanceof VariableVersion) - if(var.getType().equals(SymbolTypeBasic.BYTE)) { - allocation.allocate(var, new RegisterAllocation.RegisterZpByte(currentZp++)); - } else if(var.getType().equals(SymbolTypeBasic.WORD)) { - allocation.allocate(var, new RegisterAllocation.RegisterZpWord(currentZp)); - currentZp = currentZp +2; - } else if(var.getType().equals(SymbolTypeBasic.BOOLEAN)) { - allocation.allocate(var, new RegisterAllocation.RegisterZpBool(currentZp++)); - } else if(var.getType() instanceof SymbolTypePointer) { - allocation.allocate(var, new RegisterAllocation.RegisterZpPointerByte(currentZp)); - currentZp = currentZp +2; - } else { - throw new RuntimeException("Unhandled variable type "+var); - } - } + performAllocation(symbols, allocation); allocation.allocate(symbols.getVariable("i#0"), RegisterAllocation.getRegisterX()); allocation.allocate(symbols.getVariable("i#1"), RegisterAllocation.getRegisterX()); allocation.allocate(symbols.getVariable("i#2"), RegisterAllocation.getRegisterX()); @@ -71,10 +58,33 @@ public class Pass3RegisterAllocation { allocation.allocate(symbols.getVariable("$4"), new RegisterAllocation.RegisterAByte()); allocation.allocate(symbols.getVariable("$6"), new RegisterAllocation.RegisterALUByte()); allocation.allocate(symbols.getVariable("$7"), new RegisterAllocation.RegisterAByte()); - allocation.allocate(symbols.getVariable("inc::a#2"), new RegisterAllocation.RegisterAByte()); - allocation.allocate(symbols.getVariable("bv#0"), new RegisterAllocation.RegisterAByte()); + //allocation.allocate(symbols.getVariable("inc::a#2"), new RegisterAllocation.RegisterAByte()); + //allocation.allocate(symbols.getVariable("bv#0"), new RegisterAllocation.RegisterAByte()); symbols.setAllocation(allocation); } + private void performAllocation(Scope scope, RegisterAllocation allocation) { + for (Symbol symbol : scope.getSymbols()) { + if (symbol instanceof Scope) { + performAllocation((Scope) symbol, allocation); + } else if (symbol instanceof VariableIntermediate || symbol instanceof VariableVersion) { + Variable var = (Variable) symbol; + if (symbol.getType().equals(SymbolTypeBasic.BYTE)) { + allocation.allocate(var, new RegisterAllocation.RegisterZpByte(currentZp++)); + } else if (symbol.getType().equals(SymbolTypeBasic.WORD)) { + allocation.allocate(var, new RegisterAllocation.RegisterZpWord(currentZp)); + currentZp = currentZp + 2; + } else if (symbol.getType().equals(SymbolTypeBasic.BOOLEAN)) { + allocation.allocate(var, new RegisterAllocation.RegisterZpBool(currentZp++)); + } else if (symbol.getType() instanceof SymbolTypePointer) { + allocation.allocate(var, new RegisterAllocation.RegisterZpPointerByte(currentZp)); + currentZp = currentZp + 2; + } else { + throw new RuntimeException("Unhandled variable type " + symbol); + } + } + } + } + } diff --git a/src/dk/camelot64/kickc/icl/Scope.java b/src/dk/camelot64/kickc/icl/Scope.java index b9d9d5482..4f877feb4 100644 --- a/src/dk/camelot64/kickc/icl/Scope.java +++ b/src/dk/camelot64/kickc/icl/Scope.java @@ -207,4 +207,7 @@ public class Scope implements Symbol { return res.toString(); } + public Collection getSymbols() { + return symbols.values(); + } } diff --git a/src/dk/camelot64/kickc/test/Main.java b/src/dk/camelot64/kickc/test/Main.java index 2f1131405..5c9942634 100644 --- a/src/dk/camelot64/kickc/test/Main.java +++ b/src/dk/camelot64/kickc/test/Main.java @@ -13,7 +13,7 @@ import java.util.List; /** Test my KickC Grammar */ public class Main { public static void main(String[] args) throws IOException { - final String fileName = "src/dk/camelot64/kickc/test/callmin.kc"; + final String fileName = "src/dk/camelot64/kickc/test/callsum.kc"; final CharStream input = CharStreams.fromFileName(fileName); System.out.println(input.toString()); KickCLexer lexer = new KickCLexer(input); diff --git a/src/dk/camelot64/kickc/test/callsum.kc b/src/dk/camelot64/kickc/test/callsum.kc index bf122278b..a8f766cf8 100644 --- a/src/dk/camelot64/kickc/test/callsum.kc +++ b/src/dk/camelot64/kickc/test/callsum.kc @@ -1,12 +1,10 @@ byte a = 12; byte s = sum(5,a); -a = a+s; +a = a+s; q a = sum(s, a); -return; byte sum(byte b1, byte b2) { byte b = b1+b2; - byte inc( byte c) { return c+1;} - return inc(b); + return b; }