Skip to content

Commit 5c6f0f6

Browse files
committed
Done
1 parent 64f3999 commit 5c6f0f6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1085
-468
lines changed

Block.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Structure for the liveness analysis. Here are the information about DEF, USE, IN and OUT stored
3+
*/
4+
15
import java.util.LinkedList;
26

37
public class Block {
@@ -59,9 +63,6 @@ public LinkedList<Block> getSuccessor() {
5963
public String getDef() {
6064
return def;
6165
}
62-
public Block getSuccessor(int i) {
63-
return successor.get(i);
64-
}
6566
public boolean hasSuccessor() {
6667
return hasSuccessor;
6768
}

CodeGenerator.class

-80 Bytes
Binary file not shown.

CodeGenerator.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ public void caseAAssignmentExpr(AAssignmentExpr node) {
121121
@Override
122122
public void caseANumberExpr(ANumberExpr node) {
123123
int number = Integer.parseInt(node.getNumber().toString().replaceAll(" ", ""));
124-
if (number > 255)
124+
// if (number > 255)
125125
code += "\tldc "+number+"\n";
126-
else
127-
code += "\tbipush "+number+"\n";
126+
// else
127+
// code += "\tbipush "+number+"\n";
128128
stackHeight++;
129129
type = "integer";
130130
}

GraphVisitor.java

+15-25
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public class GraphVisitor extends DepthFirstAdapter{
88
private LinkedList<Block> blocks = new LinkedList<Block>();
99
private Block lastBlock;
1010
private Block currentBlock;
11-
private Block startBlock;
1211
private int blockID = 1;
1312
private HashMap<Integer, Integer> addSuccessors = new HashMap<Integer, Integer>();
1413

@@ -22,7 +21,7 @@ public void caseAAssignmentExpr(AAssignmentExpr node) {
2221
String identifier = node.getIdentifier().toString().toLowerCase().replaceAll(" ","");
2322
currentBlock = new Block(identifier, blockID++);
2423
node.getExpr().apply(this); // evaluate the expression
25-
setStartAndSuccessor();
24+
setNextSuccessor();
2625
}
2726
/**
2827
* For writeln(). Nearly same as Assignment
@@ -31,7 +30,7 @@ public void caseAAssignmentExpr(AAssignmentExpr node) {
3130
public void caseAPrintExpr(APrintExpr node) {
3231
currentBlock = new Block(blockID++);
3332
node.getExpr().apply(this); // evaluate the expression
34-
setStartAndSuccessor();
33+
setNextSuccessor();
3534
}
3635
/**
3736
* If then
@@ -41,7 +40,7 @@ public void caseAIfThenExpr(AIfThenExpr node) {
4140
int temp = blockID;
4241
currentBlock = new Block(blockID++);
4342
node.getLeft().apply(this); // evaluate the expression
44-
setStartAndSuccessor();
43+
setNextSuccessor();
4544
node.getRight().apply(this);
4645
addSuccessors.put(temp, blockID);
4746
}
@@ -54,7 +53,7 @@ public void caseAIfThenElseExpr(AIfThenElseExpr node) {
5453
int ifPointer = blockID;
5554
currentBlock = new Block(blockID++);
5655
node.getIf().apply(this); // evaluate the expression
57-
setStartAndSuccessor();
56+
setNextSuccessor();
5857
node.getThen().apply(this);
5958
thenPointer = blockID-1; // last element of then
6059
addSuccessors.put(ifPointer, blockID);
@@ -63,7 +62,6 @@ public void caseAIfThenElseExpr(AIfThenElseExpr node) {
6362

6463
// because of the normal algorithm according to setStartAndSuccessor() I need to remove the
6564
// first successor, because this one points to else, which is simply wrong at this point.
66-
// blocks.get(thenPointer-1).getSuccessor().removeFirst();
6765
removeSuccessors.add(thenPointer-1);
6866
addSuccessors.put(thenPointer, flow); // Add normal program flow after the then block
6967
}
@@ -75,29 +73,12 @@ public void caseAWhileExpr(AWhileExpr node) {
7573
int temp = blockID;
7674
currentBlock = new Block(blockID++);
7775
node.getLeft().apply(this); // evaluate the expression
78-
setStartAndSuccessor();
76+
setNextSuccessor();
7977
node.getRight().apply(this);
8078
addSuccessors.put(temp, blockID); // Add second successor for while loop
8179
addSuccessors.put(blockID - 1, temp);
82-
// blocks.get(blockID-2).getSuccessor().removeFirst();
8380
removeSuccessors.add(blockID - 1); // Remove later the first successor
8481
}
85-
86-
// @Override
87-
// public void caseAStartExpr(AStartExpr node) {
88-
// currentBlock = new Block(blockID++);
89-
// node.getStatementList().apply(this);
90-
// setStartAndSuccessor();
91-
// }
92-
93-
private void setStartAndSuccessor() {
94-
if (lastBlock != null) // if it is the first block, do nothing
95-
lastBlock.addSuccessor(currentBlock);
96-
else
97-
startBlock = currentBlock;
98-
blocks.add(currentBlock); // Add currentBlock to the global list of blocks
99-
lastBlock = currentBlock;
100-
}
10182
/**
10283
* Add to the current visited block a new item to the Use list
10384
*/
@@ -117,7 +98,16 @@ public void caseAIdentifierExpr(AIdentifierExpr node) {
11798
}
11899
public void exitNode() {
119100
currentBlock = new Block(blockID++);
120-
setStartAndSuccessor();
101+
setNextSuccessor();
102+
}
103+
/**
104+
* Method to set the standard successor for one node
105+
*/
106+
private void setNextSuccessor() {
107+
if (lastBlock != null) // if it is the first block, do nothing
108+
lastBlock.addSuccessor(currentBlock);
109+
blocks.add(currentBlock); // Add currentBlock to the global list of blocks
110+
lastBlock = currentBlock;
121111
}
122112

123113
/************************************************************************************************/

Liveness.java

+108-39
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1+
/**
2+
* Evaluating the information, I stored about IN and OUT.
3+
*/
4+
15
import java.util.HashMap;
26
import java.util.LinkedList;
37

48
public class Liveness {
59
private GraphVisitor analysis;
10+
private TypeChecker typeChecker;
611
private LinkedList<Block> blocks;
712

8-
public Liveness(GraphVisitor analysis) {
13+
public Liveness(GraphVisitor analysis, TypeChecker typeChecker) {
914
this.analysis = analysis;
1015
this.blocks = analysis.getBlocks();
16+
this.analysis.exitNode();
17+
this.typeChecker = typeChecker;
1118

1219
addSuccessors();
1320
removeSuccessors();
1421

1522
startLiveness();
16-
debug();
23+
// debug();
24+
25+
evaluateLiveness();
1726
}
1827

1928
/**
@@ -30,9 +39,8 @@ private void addSuccessors() {
3039
}
3140
}
3241
private void removeSuccessors() {
33-
for (Integer i : analysis.getRemoveSuccessors()) {
42+
for (Integer i : analysis.getRemoveSuccessors())
3443
blocks.get(i-1).getSuccessor().removeFirst();
35-
}
3644
}
3745

3846
/**
@@ -43,22 +51,22 @@ private void debug() {
4351

4452
Block currentBlock;
4553

46-
for (int i = 0; i < blocks.size(); i++) {
47-
currentBlock = blocks.get(i);
48-
System.out.print("\t#" + currentBlock.getBlockID() + "\tDef: " + currentBlock.getDef() + "\t\tUse: " + currentBlock.getUse()+"\t\tSuccessor: ");
49-
if (currentBlock.hasSuccessor()) {
50-
for (Block block : currentBlock.getSuccessor()) {
51-
System.out.print(block.getBlockID() + " ");
52-
}
53-
}
54+
for (Block block : blocks) {
55+
currentBlock = block;
56+
System.out.print("\t#" + currentBlock.getBlockID() + "\tDef: " + currentBlock.getDef() + "\t\tUse: " + currentBlock.getUse() + "\t\tSuccessor: ");
57+
58+
if (currentBlock.hasSuccessor())
59+
for (Block element : currentBlock.getSuccessor())
60+
System.out.print(element.getBlockID() + " ");
61+
5462
System.out.print("\t\t IN: ");
55-
for (String element : currentBlock.getIn()) {
63+
for (String element : currentBlock.getIn())
5664
System.out.print(element + " ");
57-
}
65+
5866
System.out.print("\t\t OUT: ");
59-
for (String element : currentBlock.getOut()) {
60-
System.out.print(element+" ");
61-
}
67+
for (String element : currentBlock.getOut())
68+
System.out.print(element + " ");
69+
6270
System.out.println();
6371
}
6472
}
@@ -72,51 +80,52 @@ private void startLiveness() {
7280
Block currentBlock = blocks.getFirst();
7381
boolean changed = true;
7482

75-
calcFirstIteration(); // Copy use to in in all the blocks!
83+
calcFirstIteration(); // Copy USE to IN in all the blocks!
7684
LinkedList<String> diff;
7785
LinkedList<String> inSuccessor;
7886

7987
while (changed) {
8088
changed = false;
81-
for (int i = 0; i < blocks.size(); i++) {
89+
for (Block block : blocks) {
8290
// Get IN
8391
diff = calcDiff(currentBlock);
84-
for (String element : diff) {
92+
for (String element : diff)
8593
if (!currentBlock.getIn().contains(element)) {
8694
currentBlock.addIn(element);
8795
changed = true;
8896
}
89-
}
9097
// Get OUT
9198
inSuccessor = calcInOfSuccessor(currentBlock);
92-
for (String element : inSuccessor) {
99+
for (String element : inSuccessor)
93100
if (!currentBlock.getOut().contains(element)) {
94101
currentBlock.addOut(element);
95102
changed = true;
96103
}
97-
}
98-
currentBlock = blocks.get(i);
104+
currentBlock = block;
99105
}
100106
}
101107
}
102108
}
103109

110+
/**
111+
* Copy USE to IN of each Block. First step of algorithm.
112+
*/
104113
private void calcFirstIteration() {
105-
for (Block block : blocks) {
106-
if (block.hasUse()) {
107-
for (String element : block.getUse()) {
114+
for (Block block : blocks)
115+
if (block.hasUse())
116+
for (String element : block.getUse())
108117
block.addIn(element);
109-
}
110-
}
111-
}
112118
}
119+
120+
/**
121+
* Calculate OUT[n] - DEF[n]
122+
*/
113123
private LinkedList<String> calcDiff(Block currentBlock) {
114124
LinkedList<String> output = new LinkedList<String>();
115125
boolean duplicate = false;
116126
for (String out : currentBlock.getOut()) {
117-
if (out.equals(currentBlock.getDef())) {
127+
if (out.equals(currentBlock.getDef()))
118128
duplicate = true;
119-
}
120129
if (!duplicate) {
121130
output.add(out);
122131
duplicate = false;
@@ -126,17 +135,77 @@ private LinkedList<String> calcDiff(Block currentBlock) {
126135
}
127136
private LinkedList<String> calcInOfSuccessor(Block currentBlock) {
128137
LinkedList<String> inSuccessor = new LinkedList<String>();
129-
if (currentBlock.hasSuccessor()) {
130-
for (Block successor : currentBlock.getSuccessor()) {
131-
for (String element : successor.getIn()) {
138+
if (currentBlock.hasSuccessor())
139+
for (Block successor : currentBlock.getSuccessor())
140+
for (String element : successor.getIn())
132141
inSuccessor.add(element);
133-
}
134-
}
135-
}
136142
return inSuccessor;
137143
}
138144

139-
private void printAdjacencyMatrix() {
145+
/****************************************** Create Adjacency Matrix ******************************************/
146+
private void evaluateLiveness() {
147+
int registers = calcMaximum();
148+
System.out.println("Registers: "+registers);
149+
if (registers != 0)
150+
createAdjacencyMatrix(); // if it is zero, there are no variables
151+
}
152+
153+
/**
154+
* Calculate maximum of all the OUTs to get the number of registers needed
155+
*/
156+
private int calcMaximum() {
157+
int max = 0;
158+
for (Block block : blocks)
159+
if (max < block.getOut().size())
160+
max = block.getOut().size();
161+
return max;
162+
}
163+
164+
/**
165+
* This method creates a Node for each identifier and calls a method to add edges to
166+
* the other identifiers according to the liveness analysis.
167+
*/
168+
private void createAdjacencyMatrix() {
169+
LinkedList<Nodes> nodes = new LinkedList<Nodes>();
170+
171+
// Make a new Node for all identifier found by the typeChecker
172+
for (String element : typeChecker.getSymbolTable().keySet())
173+
nodes.add(new Nodes(element));
174+
175+
// Add edges to those Nodes
176+
createEdges(nodes);
177+
178+
// Print those Edges
179+
System.out.println();
180+
for (Nodes node : nodes)
181+
System.out.println("ID " + node.getIdentifier() + " -> " + node.getEdge());
182+
System.out.println();
183+
System.out.println("How to read it: ID x has edges to (->) all the IDs in Brackets.\n");
184+
}
185+
186+
/**
187+
* Creates an edge for all identifiers in conflict
188+
*/
189+
private void createEdges(LinkedList<Nodes> nodes) {
190+
Nodes node;
191+
// find for each block
192+
for (Block block : blocks)
193+
for (String element : block.getOut()) { // those variables in OUT
194+
node = nodes.get(findNode(element, nodes)); // set current node
195+
// and add new edges to node
196+
for (String addOut : block.getOut())
197+
if (!node.getEdge().contains(addOut) && !node.getIdentifier().equals(addOut))
198+
node.addEdge(addOut);
199+
}
200+
}
140201

202+
private int findNode(String element, LinkedList<Nodes> nodes) {
203+
int i = 0;
204+
for (Nodes node : nodes)
205+
if (node.getIdentifier().equals(element))
206+
return i;
207+
else
208+
i++;
209+
return -1;
141210
}
142211
}

Nodes.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.LinkedList;
2+
3+
public class Nodes {
4+
private String identifier;
5+
private LinkedList<String> edge = new LinkedList<String>();
6+
7+
public Nodes(String identifier) {
8+
this.identifier = identifier;
9+
}
10+
public void addEdge(String other) {
11+
edge.add(other);
12+
}
13+
public LinkedList<String> getEdge() {
14+
return edge;
15+
}
16+
public String getIdentifier() {
17+
return identifier;
18+
}
19+
}

Pascal.class

54 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)