forked from mrhappyasthma/MiniJava-Compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssemFlowGraph.java
More file actions
183 lines (151 loc) · 6.03 KB
/
Copy pathAssemFlowGraph.java
File metadata and controls
183 lines (151 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package regalloc.flowgraph;
import IR.*;
import regalloc.graph.Node;
import helper.Label;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import symboltable.Variable;
import regalloc.Liveness;
public class AssemFlowGraph{
List<Quadruple> instr;
Hashtable<Quadruple, List<Label>> labels;
List<Node> graph;
//hashtable that keeps the Node of the label
Hashtable<String,Integer> labelNode;
List<List<Node>> func;
//int countInstr;
public AssemFlowGraph(List<Quadruple> i ,Hashtable<Quadruple, List<Label>> l){
labels = l;
instr = i;
graph = new ArrayList<Node>();
labelNode = new Hashtable<String,Integer>();
func = new ArrayList<List<Node>>();
//countInstr=0;
}
public List<List<Node>> buildCFG(){
buildNodes();
boolean isNewFunction = false;
//String labelNewFunc;
List<Node> auxList = new ArrayList<Node>();
for (int i = 0; i < graph.size(); i++) {
Node n = graph.get(i);
if(isNewFunction){
isNewFunction = false;
//add the actual CFG (actual function) in the List of Lists
func.add(auxList);
//create a new list to keep the new function
auxList = new ArrayList<Node>();
}
auxList.add(n);
if(n.getExitFunction()){
if(i!=graph.size()-1){
//the next instruction will be a new function
isNewFunction = true;
}
//it's the last node
else{
func.add(auxList);
}
n.setNextNull();
}
else{
//if nextLabel() is null it means that is the instruction that follows
if(n.nextLabel()==null){
//if is not the last node
if(i != graph.size()-2){
Node aux = graph.get(i+1);
n.addNext(aux);
}
}
else{
List<String> strAux = n.nextLabel();
for (int j = 0; j < strAux.size(); j++) {
if( (strAux.get(j)).equals("next")){
Node aux = graph.get(i+1);
n.addNext(aux);
}
else{
int numLabel = labelNode.get(strAux.get(j));
n.addNext(graph.get(numLabel));
}
}
}
}
}
return func;
}
public void buildNodes(){
Node n = null;
Node aux = null;
for (int i = 0; i < instr.size(); i++) {
Quadruple q = instr.get(i);
List<Label> labelList = labels.get(q);
//first node
n = new Node(q,i);
if(labelList!=null){
for (Label l : labelList) {
if(l.printBefore){
//save label and its number
labelNode.put(l.getName(), i);
}
//the label can be in one line but actually represents the next one
else{
labelNode.put(l.getName(),i+1);
}
}
}
if(q instanceof UnaryAssignmentIR){
if(q.getOp()==null){
Variable arg1 = (Variable) q.getArg1();
Variable arg2 = (Variable) q.getArg2();
if(arg1.getName().equals(arg2.getName())){
n.setMove();
}
}
}
if(q instanceof ConditionalJumpIR ){
String nameLabel = ((Label)q.getResult()).getName();
//iffalse
n.addJumpTo(nameLabel);
//if true
n.addJumpTo("next");
}
if(q instanceof UnconditionalJumpIR){
String nameLabel = ((Label)q.getResult()).getName();
n.addJumpTo(nameLabel);
}
if(q instanceof ReturnIR){
n.setExitFunction();
}
if(q instanceof CallIR){
String arg1 = (String)q.getArg1();
if(!arg1.equals("_system_out_println") && !arg1.equals("_system_exit")){
n.addJumpTo(arg1);
n.setJumpToFunction();
n.addJumpTo("next");
}
if(arg1.equals("_system_exit")){
n.setExitFunction();
}
}
graph.add(n);
}
}
public void printGraph(List<Node> graph){
System.out.println("New Function");
for (int i = 0; i < graph.size(); i++) {
Node n = graph.get(i);
System.out.print(n.getNum()+ " ");
System.out.println(n.getInstr().toString());
List<Node> listN = n.nextNode();
if(listN.size()!=0){
System.out.println("Nexts:");
for (int j = 0; j < listN.size(); j++) {
System.out.print(listN.get(j).getNum()+ " ");
System.out.println(listN.get(j).getInstr().toString());
}
}
}
}
}