1
+ /**
2
+ * Evaluating the information, I stored about IN and OUT.
3
+ */
4
+
1
5
import java .util .HashMap ;
2
6
import java .util .LinkedList ;
3
7
4
8
public class Liveness {
5
9
private GraphVisitor analysis ;
10
+ private TypeChecker typeChecker ;
6
11
private LinkedList <Block > blocks ;
7
12
8
- public Liveness (GraphVisitor analysis ) {
13
+ public Liveness (GraphVisitor analysis , TypeChecker typeChecker ) {
9
14
this .analysis = analysis ;
10
15
this .blocks = analysis .getBlocks ();
16
+ this .analysis .exitNode ();
17
+ this .typeChecker = typeChecker ;
11
18
12
19
addSuccessors ();
13
20
removeSuccessors ();
14
21
15
22
startLiveness ();
16
- debug ();
23
+ // debug();
24
+
25
+ evaluateLiveness ();
17
26
}
18
27
19
28
/**
@@ -30,9 +39,8 @@ private void addSuccessors() {
30
39
}
31
40
}
32
41
private void removeSuccessors () {
33
- for (Integer i : analysis .getRemoveSuccessors ()) {
42
+ for (Integer i : analysis .getRemoveSuccessors ())
34
43
blocks .get (i -1 ).getSuccessor ().removeFirst ();
35
- }
36
44
}
37
45
38
46
/**
@@ -43,22 +51,22 @@ private void debug() {
43
51
44
52
Block currentBlock ;
45
53
46
- for (int i = 0 ; i < blocks . size (); i ++ ) {
47
- currentBlock = blocks . get ( i ) ;
48
- System .out .print ("\t #" + currentBlock .getBlockID () + "\t Def: " + currentBlock .getDef () + "\t \t Use: " + currentBlock .getUse ()+ "\t \t Successor: " );
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 () + "\t Def: " + currentBlock .getDef () + "\t \t Use: " + currentBlock .getUse () + "\t \t Successor: " );
57
+
58
+ if ( currentBlock .hasSuccessor ())
59
+ for ( Block element : currentBlock . getSuccessor ())
60
+ System . out . print ( element . getBlockID () + " " );
61
+
54
62
System .out .print ("\t \t IN: " );
55
- for (String element : currentBlock .getIn ()) {
63
+ for (String element : currentBlock .getIn ())
56
64
System .out .print (element + " " );
57
- }
65
+
58
66
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
+
62
70
System .out .println ();
63
71
}
64
72
}
@@ -72,51 +80,52 @@ private void startLiveness() {
72
80
Block currentBlock = blocks .getFirst ();
73
81
boolean changed = true ;
74
82
75
- calcFirstIteration (); // Copy use to in in all the blocks!
83
+ calcFirstIteration (); // Copy USE to IN in all the blocks!
76
84
LinkedList <String > diff ;
77
85
LinkedList <String > inSuccessor ;
78
86
79
87
while (changed ) {
80
88
changed = false ;
81
- for (int i = 0 ; i < blocks . size (); i ++ ) {
89
+ for (Block block : blocks ) {
82
90
// Get IN
83
91
diff = calcDiff (currentBlock );
84
- for (String element : diff ) {
92
+ for (String element : diff )
85
93
if (!currentBlock .getIn ().contains (element )) {
86
94
currentBlock .addIn (element );
87
95
changed = true ;
88
96
}
89
- }
90
97
// Get OUT
91
98
inSuccessor = calcInOfSuccessor (currentBlock );
92
- for (String element : inSuccessor ) {
99
+ for (String element : inSuccessor )
93
100
if (!currentBlock .getOut ().contains (element )) {
94
101
currentBlock .addOut (element );
95
102
changed = true ;
96
103
}
97
- }
98
- currentBlock = blocks .get (i );
104
+ currentBlock = block ;
99
105
}
100
106
}
101
107
}
102
108
}
103
109
110
+ /**
111
+ * Copy USE to IN of each Block. First step of algorithm.
112
+ */
104
113
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 ())
108
117
block .addIn (element );
109
- }
110
- }
111
- }
112
118
}
119
+
120
+ /**
121
+ * Calculate OUT[n] - DEF[n]
122
+ */
113
123
private LinkedList <String > calcDiff (Block currentBlock ) {
114
124
LinkedList <String > output = new LinkedList <String >();
115
125
boolean duplicate = false ;
116
126
for (String out : currentBlock .getOut ()) {
117
- if (out .equals (currentBlock .getDef ())) {
127
+ if (out .equals (currentBlock .getDef ()))
118
128
duplicate = true ;
119
- }
120
129
if (!duplicate ) {
121
130
output .add (out );
122
131
duplicate = false ;
@@ -126,17 +135,77 @@ private LinkedList<String> calcDiff(Block currentBlock) {
126
135
}
127
136
private LinkedList <String > calcInOfSuccessor (Block currentBlock ) {
128
137
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 ())
132
141
inSuccessor .add (element );
133
- }
134
- }
135
- }
136
142
return inSuccessor ;
137
143
}
138
144
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
+ }
140
201
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 ;
141
210
}
142
211
}
0 commit comments