1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-08-02 09:29:35 +00:00

Implemented Control flow Graph Sequence Planner. Implemented working flipper.

This commit is contained in:
Jesper Gravgaard 2017-07-11 01:06:13 +02:00
parent 01a26b4710
commit b25be31ba1
5 changed files with 72 additions and 8 deletions

View File

@ -12,6 +12,7 @@ public class ControlFlowGraph {
private Map<Symbol, ControlFlowBlock> blocks;
private ControlFlowBlock firstBlock;
private List<ControlFlowBlock> sequence;
public ControlFlowGraph(Map<Symbol, ControlFlowBlock> blocks, ControlFlowBlock firstBlock) {
this.blocks = blocks;
@ -77,6 +78,14 @@ public class ControlFlowGraph {
}
}
public ControlFlowBlock getConditionalSuccessor(ControlFlowBlock block) {
if(block.getConditionalSuccessor()!=null) {
return blocks.get(block.getConditionalSuccessor());
} else {
return null;
}
}
public List<ControlFlowBlock> getPredecessors(ControlFlowBlock block) {
ArrayList<ControlFlowBlock> predecessorBlocks = new ArrayList<>();
@ -93,4 +102,13 @@ public class ControlFlowGraph {
}
return predecessorBlocks;
}
public void setBlockSequence(List<ControlFlowBlock> sequence) {
this.sequence = sequence;
}
public List<ControlFlowBlock> getBlockSequence() {
return sequence;
}
}

View File

@ -0,0 +1,44 @@
package dk.camelot64.kickc.icl;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/** Plan the optimal sequence for the blocks of the control flow graph */
public class Pass3BlockSequencePlanner {
private ControlFlowGraph controlFlowGraph;
private Scope programScope;
public Pass3BlockSequencePlanner(ControlFlowGraph controlFlowGraph, Scope programScope) {
this.controlFlowGraph = controlFlowGraph;
this.programScope = programScope;
}
public void plan() {
Stack<ControlFlowBlock> todo = new Stack<>();
todo.push(controlFlowGraph.getFirstBlock());
List<ControlFlowBlock> sequence = new ArrayList<>();
while(!todo.empty()){
ControlFlowBlock block = todo.pop();
if(block==null) {
break;
}
if(sequence.contains(block)) {
// already handled - move on
break;
}
sequence.add(block);
if(block.getCallSuccessor()!=null) {
todo.push(controlFlowGraph.getCallSuccessor(block));
}
if(block.getConditionalSuccessor()!=null) {
todo.push(controlFlowGraph.getConditionalSuccessor(block));
}
if(controlFlowGraph.getDefaultSuccessor(block)!=null) {
todo.push(controlFlowGraph.getDefaultSuccessor(block));
}
}
controlFlowGraph.setBlockSequence(sequence);
}
}

View File

@ -19,7 +19,7 @@ public class Pass3CodeGeneration {
public AsmProgram generate() {
AsmProgram asm = new AsmProgram();
for (ControlFlowBlock block : graph.getAllBlocks()) {
for (ControlFlowBlock block : graph.getBlockSequence()) {
// Generate entry points (if needed)
genBlockEntryPoints(asm, block);
// Generate label

View File

@ -45,11 +45,11 @@ public class Pass3RegisterAllocation {
allocation.allocate(symbols.getVariable("ptr#1"), new RegisterAllocation.RegisterZpPointerByte(135));
allocation.allocate(symbols.getVariable("ptr#2"), new RegisterAllocation.RegisterZpPointerByte(135));
allocation.allocate(symbols.getVariable("ptr#3"), new RegisterAllocation.RegisterZpPointerByte(135));
allocation.allocate(symbols.getVariable("v#1"), new RegisterAllocation.RegisterAByte());
allocation.allocate(symbols.getVariable("v#2"), new RegisterAllocation.RegisterAByte());
allocation.allocate(symbols.getVariable("v#3"), new RegisterAllocation.RegisterAByte());
allocation.allocate(symbols.getVariable("v#4"), new RegisterAllocation.RegisterAByte());
allocation.allocate(symbols.getVariable("v#5"), new RegisterAllocation.RegisterAByte());
//allocation.allocate(symbols.getVariable("v#1"), new RegisterAllocation.RegisterAByte());
//allocation.allocate(symbols.getVariable("v#2"), new RegisterAllocation.RegisterAByte());
//allocation.allocate(symbols.getVariable("v#3"), new RegisterAllocation.RegisterAByte());
//allocation.allocate(symbols.getVariable("v#4"), new RegisterAllocation.RegisterAByte());
//allocation.allocate(symbols.getVariable("v#5"), new RegisterAllocation.RegisterAByte());
//allocation.allocate(symbols.getVariable("$0"), new RegisterAllocation.RegisterAByte());
//allocation.allocate(symbols.getVariable("$2"), new RegisterAllocation.RegisterAByte());
//allocation.allocate(symbols.getVariable("$3"), new RegisterAllocation.RegisterAByte());
@ -62,7 +62,8 @@ public class Pass3RegisterAllocation {
//allocation.allocate(symbols.getVariable("bv#0"), new RegisterAllocation.RegisterAByte());
allocation.allocate(symbols.getVariable("sum::b#0"), new RegisterAllocation.RegisterAByte());
allocation.allocate(symbols.getVariable("inc::b#1"), new RegisterAllocation.RegisterAByte());
allocation.allocate(symbols.getVariable("a#0"), new RegisterAllocation.RegisterAByte());
//allocation.allocate(symbols.getVariable("a#1"), new RegisterAllocation.RegisterAByte());
//allocation.allocate(symbols.getVariable("a#0"), new RegisterAllocation.RegisterAByte());
symbols.setAllocation(allocation);
}

View File

@ -70,7 +70,6 @@ public class Main {
optimizations.add(new Pass2SelfPhiElimination(controlFlowGraph, programScope));
optimizations.add(new Pass2ConditionalJumpSimplification(controlFlowGraph, programScope));
boolean ssaOptimized = true;
while (ssaOptimized) {
ssaOptimized = false;
@ -85,6 +84,8 @@ public class Main {
}
}
Pass3BlockSequencePlanner pass3BlockSequencePlanner = new Pass3BlockSequencePlanner(controlFlowGraph, programScope);
pass3BlockSequencePlanner.plan();
Pass3RegisterAllocation pass3RegisterAllocation = new Pass3RegisterAllocation(controlFlowGraph, programScope);
pass3RegisterAllocation.allocate();
Pass3CodeGeneration pass3CodeGeneration = new Pass3CodeGeneration(controlFlowGraph, programScope);