Skip to content

Commit 64f3999

Browse files
committed
Working liveness analysis
1 parent fbdc01d commit 64f3999

File tree

9 files changed

+91
-90
lines changed

9 files changed

+91
-90
lines changed

Block.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class Block {
55
public LinkedList<String> use;
66
private LinkedList<String> in = new LinkedList<String>();
77
private LinkedList<String> out = new LinkedList<String>();
8-
private LinkedList<Block> successor = new LinkedList<Block>();
8+
private LinkedList<Block> successor;
99
private boolean hasSuccessor = false;
1010

1111
private boolean hasUse = false;
@@ -35,7 +35,10 @@ public void addOut(String input) {
3535
out.add(input);
3636
}
3737
public void addSuccessor(Block input) {
38-
hasSuccessor = true;
38+
if (!hasSuccessor) {
39+
successor = new LinkedList<Block>();
40+
hasSuccessor = true;
41+
}
3942
successor.add(input);
4043
}
4144

GraphVisitor.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public void caseAIfThenElseExpr(AIfThenElseExpr node) {
6363

6464
// because of the normal algorithm according to setStartAndSuccessor() I need to remove the
6565
// first successor, because this one points to else, which is simply wrong at this point.
66-
blocks.get(thenPointer-1).getSuccessor().removeFirst();
66+
// blocks.get(thenPointer-1).getSuccessor().removeFirst();
67+
removeSuccessors.add(thenPointer-1);
6768
addSuccessors.put(thenPointer, flow); // Add normal program flow after the then block
6869
}
6970
/**
@@ -79,9 +80,16 @@ public void caseAWhileExpr(AWhileExpr node) {
7980
addSuccessors.put(temp, blockID); // Add second successor for while loop
8081
addSuccessors.put(blockID - 1, temp);
8182
// blocks.get(blockID-2).getSuccessor().removeFirst();
82-
removeSuccessors.add(blockID-1); // Remove later the first successor
83+
removeSuccessors.add(blockID - 1); // Remove later the first successor
8384
}
8485

86+
// @Override
87+
// public void caseAStartExpr(AStartExpr node) {
88+
// currentBlock = new Block(blockID++);
89+
// node.getStatementList().apply(this);
90+
// setStartAndSuccessor();
91+
// }
92+
8593
private void setStartAndSuccessor() {
8694
if (lastBlock != null) // if it is the first block, do nothing
8795
lastBlock.addSuccessor(currentBlock);
@@ -107,6 +115,10 @@ public void caseAIdentifierExpr(AIdentifierExpr node) {
107115
}
108116

109117
}
118+
public void exitNode() {
119+
currentBlock = new Block(blockID++);
120+
setStartAndSuccessor();
121+
}
110122

111123
/************************************************************************************************/
112124
public HashMap<Integer, Integer> getAddSuccessors() {
@@ -118,4 +130,5 @@ public LinkedList<Integer> getRemoveSuccessors() {
118130
public LinkedList<Block> getBlocks() {
119131
return blocks;
120132
}
133+
121134
}

Liveness.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class Liveness {
88
public Liveness(GraphVisitor analysis) {
99
this.analysis = analysis;
1010
this.blocks = analysis.getBlocks();
11+
1112
addSuccessors();
1213
removeSuccessors();
1314

@@ -24,9 +25,8 @@ private void addSuccessors() {
2425
int value;
2526
for (int key : map.keySet()) {
2627
value = map.get(key);
27-
if (value-1 < blocks.size()) { // protecting to run out of the index of blocks
28+
if (value-1 < blocks.size()) // protecting to run out of the index of blocks
2829
blocks.get(key-1).addSuccessor(blocks.get(value-1));
29-
}
3030
}
3131
}
3232
private void removeSuccessors() {
@@ -46,8 +46,10 @@ private void debug() {
4646
for (int i = 0; i < blocks.size(); i++) {
4747
currentBlock = blocks.get(i);
4848
System.out.print("\t#" + currentBlock.getBlockID() + "\tDef: " + currentBlock.getDef() + "\t\tUse: " + currentBlock.getUse()+"\t\tSuccessor: ");
49-
for (Block block : currentBlock.getSuccessor()) {
50-
System.out.print(block.getBlockID() + " ");
49+
if (currentBlock.hasSuccessor()) {
50+
for (Block block : currentBlock.getSuccessor()) {
51+
System.out.print(block.getBlockID() + " ");
52+
}
5153
}
5254
System.out.print("\t\t IN: ");
5355
for (String element : currentBlock.getIn()) {
@@ -73,43 +75,32 @@ private void startLiveness() {
7375
calcFirstIteration(); // Copy use to in in all the blocks!
7476
LinkedList<String> diff;
7577
LinkedList<String> inSuccessor;
76-
boolean duplicate = false;
7778

7879
while (changed) {
7980
changed = false;
8081
for (int i = 0; i < blocks.size(); i++) {
8182
// Get IN
8283
diff = calcDiff(currentBlock);
8384
for (String element : diff) {
84-
for (String in : currentBlock.getIn()) {
85-
if (in.equals(element)) {
86-
duplicate = true;
87-
}
88-
}
89-
if (!duplicate) {
85+
if (!currentBlock.getIn().contains(element)) {
9086
currentBlock.addIn(element);
9187
changed = true;
9288
}
9389
}
94-
duplicate = false;
9590
// Get OUT
9691
inSuccessor = calcInOfSuccessor(currentBlock);
9792
for (String element : inSuccessor) {
98-
for (String out : currentBlock.getOut())
99-
if (out.equals(element)) {
100-
duplicate = true;
101-
}
102-
if (!duplicate) {
93+
if (!currentBlock.getOut().contains(element)) {
10394
currentBlock.addOut(element);
10495
changed = true;
10596
}
10697
}
107-
duplicate = false;
10898
currentBlock = blocks.get(i);
10999
}
110100
}
111101
}
112102
}
103+
113104
private void calcFirstIteration() {
114105
for (Block block : blocks) {
115106
if (block.hasUse()) {
@@ -121,20 +112,31 @@ private void calcFirstIteration() {
121112
}
122113
private LinkedList<String> calcDiff(Block currentBlock) {
123114
LinkedList<String> output = new LinkedList<String>();
115+
boolean duplicate = false;
124116
for (String out : currentBlock.getOut()) {
125-
if (!out.equals(currentBlock.getDef())) {
117+
if (out.equals(currentBlock.getDef())) {
118+
duplicate = true;
119+
}
120+
if (!duplicate) {
126121
output.add(out);
122+
duplicate = false;
127123
}
128124
}
129125
return output;
130126
}
131127
private LinkedList<String> calcInOfSuccessor(Block currentBlock) {
132128
LinkedList<String> inSuccessor = new LinkedList<String>();
133-
for (Block successor : currentBlock.getSuccessor()) {
134-
for (String element : successor.getIn()) {
135-
inSuccessor.add(element);
129+
if (currentBlock.hasSuccessor()) {
130+
for (Block successor : currentBlock.getSuccessor()) {
131+
for (String element : successor.getIn()) {
132+
inSuccessor.add(element);
133+
}
136134
}
137135
}
138136
return inSuccessor;
139137
}
138+
139+
private void printAdjacencyMatrix() {
140+
141+
}
140142
}

Pascal.pas

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
1-
Program IlovePriNum;
2-
Var i, n: Integer;
3-
Var prim: Boolean;
4-
Begin
5-
n := 42;
6-
i := 2;
7-
prim := true;
8-
while true do
9-
BEGIN
10-
if (n mod i)=0 then
11-
BEGIN
12-
writeln(i);
13-
prim := false;
14-
break;
15-
END else
16-
i := i + 1;
17-
if i=(n-1) then
18-
BEGIN
19-
prim := true;
20-
break;
21-
END;
22-
END;
23-
writeln(42)
24-
End.
1+
program fibonacci ;
2+
var a : integer ;
3+
var b: integer;
4+
var temp : integer ;
5+
begin
6+
a := 1;
7+
b := 1;
8+
while (True and (a < 100)) do begin
9+
writeln(a);
10+
temp := b;
11+
b := a + b;
12+
a := temp ;
13+
end
14+
end .

StupsCompiler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ private static void parseLiveness(String input) throws ParserException, LexerExc
135135

136136
GraphVisitor analysis = new GraphVisitor();
137137
start.apply(analysis);
138+
analysis.exitNode();
138139

139140
new Liveness(analysis); // Start Liveness analysis
140141
}

out/production/projekt/Pascal.pas

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
1-
Program IlovePriNum;
2-
Var i, n: Integer;
3-
Var prim: Boolean;
4-
Begin
5-
n := 42;
6-
i := 2;
7-
prim := true;
8-
while true do
9-
BEGIN
10-
if (n mod i)=0 then
11-
BEGIN
12-
writeln(i);
13-
prim := false;
14-
break;
15-
END else
16-
i := i + 1;
17-
if i=(n-1) then
18-
BEGIN
19-
prim := true;
20-
break;
21-
END;
22-
END;
23-
writeln(42)
24-
End.
1+
program fibonacci ;
2+
var a : integer ;
3+
var b: integer;
4+
var temp : integer ;
5+
begin
6+
a := 1;
7+
b := 1;
8+
while (True and (a < 100)) do begin
9+
writeln(a);
10+
temp := b;
11+
b := a + b;
12+
a := temp ;
13+
end
14+
end .
30 Bytes
Binary file not shown.

out/production/projekt/testCases/3.pas

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
var b : integer ;
44
var temp : integer;
55
begin
6-
a := 1 ;
7-
b := 1 ;
8-
while a < 1000 do begin
9-
writeln ( a ) ;
10-
temp := b ;
11-
b := a + b ;
12-
a := temp ;
13-
end
6+
a := 1 ;
7+
b := 1 ;
8+
while a < 1000 do
9+
begin
10+
writeln ( a ) ;
11+
temp := b ;
12+
b := a + b ;
13+
a := temp ;
14+
end
1415
end.

testCases/3.pas

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
var b : integer ;
44
var temp : integer;
55
begin
6-
a := 1 ;
7-
b := 1 ;
8-
while a < 1000 do begin
9-
writeln ( a ) ;
10-
temp := b ;
11-
b := a + b ;
12-
a := temp ;
13-
end
6+
a := 1 ;
7+
b := 1 ;
8+
while a < 1000 do
9+
begin
10+
writeln ( a ) ;
11+
temp := b ;
12+
b := a + b ;
13+
a := temp ;
14+
end
1415
end.

0 commit comments

Comments
 (0)