1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-09-08 17:54:40 +00:00

Fixed for callsum

This commit is contained in:
jespergravgaard 2017-06-13 23:11:56 +02:00
parent 790feb1dcc
commit 5254d7d3a6
6 changed files with 44 additions and 29 deletions

View File

@ -0,0 +1,4 @@
lda #{coby1}
clc
adc {zpby2}
sta {zpby1}

View File

@ -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

View File

@ -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);
}
}
}
}
}

View File

@ -207,4 +207,7 @@ public class Scope implements Symbol {
return res.toString();
}
public Collection<Symbol> getSymbols() {
return symbols.values();
}
}

View File

@ -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);

View File

@ -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;
}