package ru bmstu iu9 compiler ir import com sun org apache bcel intern

  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
package ru.bmstu.iu9.compiler.ir;
import com.sun.org.apache.bcel.internal.generic.GOTO;
import ru.bmstu.iu9.compiler.syntax.tree.IntegerConstantLeaf;
import ru.bmstu.iu9.compiler.syntax.tree.StructDeclNode;
import java.util.LinkedList;
import java.util.List;
import java.util.Hashtable;
/**
* Created by natalia on 20.03.14.
*/
class Edge<V> {
int true_false;//1 -true, 0 -false, -1 -none
V in_vertex, out_vertex;
public Edge(int i, V in, V out) {
true_false = i;
in_vertex = in;
out_vertex = out;
}
int getTrue_false() { return true_false; }
}
class BaseBlock<E> {
List<Statement> statements;
public BaseBlock() {
statements = new LinkedList<Statement>();
}
public BaseBlock(Statement s) {
statements = new LinkedList<Statement>();
statements.add(s);
}
public void AddStatement(Statement s) {
statements.add(s);
}
public String toString() {
String result = "next BB: \n";
int i = 0;
for(Statement s: statements) {
result += ("\t" + i + ": " + s.toString() + "\n");
i++;
}
return result;
}
}
public class CFG {
Digraph<BaseBlock, Edge> G = new Digraph<BaseBlock, Edge>();
public CFG(List<Statement> statements) {
Hashtable<Long, Integer> StToBB = new Hashtable<Long, Integer>();//match index of statement to index of base block in vBB
List<BaseBlock> vBB = new LinkedList<BaseBlock>();
StToBB.put((long) 0, 0);//нулевому statement соответствует нулевой base block в vBB
BaseBlock bb = new BaseBlock();
vBB.add(bb);
G.add(bb);
for (Statement s: statements) {
if(s.baseOperation == Statement.Operation.IF_GOTO) {
IfGoToStatement a = (IfGoToStatement)s;
long ltrue = a.labelTrue.index();
long lfalse = a.labelFalse.index();
if(!StToBB.containsKey(ltrue)) {
StToBB.put(ltrue, vBB.size());
BaseBlock b = new BaseBlock();
vBB.add(b);
G.add(b);
}
if(!StToBB.containsKey(lfalse)) {
StToBB.put(lfalse, vBB.size());
BaseBlock b = new BaseBlock();
vBB.add(b);
G.add(b);
}
} else if (s.baseOperation == Statement.Operation.GOTO) {
GoToStatement a = (GoToStatement)s;
long l = a.label.index();
if(!StToBB.containsKey(l)) {
StToBB.put(l, vBB.size());
BaseBlock b = new BaseBlock();
//System.out.println("got found: l=" + l + "; vBB.size()=" + vBB.size() + ";\n");
vBB.add(b);
//System.out.println("got found: l=" + l + "; vBB.size()=" + vBB.size() + ";\n");
G.add(b);
}
}
}
//System.out.println("Started\n");
long i = 0;
BaseBlock curBB = vBB.get(0);
//System.out.println("contains: " + G.contains(curBB));
for (Statement s: statements) {
curBB.AddStatement(s);
if(s.baseOperation == Statement.Operation.IF_GOTO) {
IfGoToStatement igt = (IfGoToStatement)s;
G.add(curBB, vBB.get(StToBB.get(igt.labelTrue.index())));
G.add(curBB, vBB.get(StToBB.get(igt.labelFalse.index())));
//System.out.println("contains ifgoto: " + G.contains(curBB) + " and i+1=" + (i+1));
if(StToBB.containsKey((long)(i+1))) {
//cur = ;
curBB = vBB.get(StToBB.get((long)(i+1)));
} else {
//cur = vBB.size();
BaseBlock b = new BaseBlock();
vBB.add(b);
G.add(curBB);
curBB = b;
}
} else if (s.baseOperation == Statement.Operation.GOTO) {
GoToStatement gt = (GoToStatement)s;
//System.out.println("contains goto: " + vBB.get(StToBB.get(gt.label.index())).toString() + " and i+1=" + (i+1));
G.add(curBB, vBB.get(StToBB.get(gt.label.index())));
if(StToBB.containsKey((long)(i+1))) {
//System.out.println("ok");
curBB = vBB.get(StToBB.get((long)(i+1)));
} else {
//cur = vBB.size();
BaseBlock b = new BaseBlock();
vBB.add(b);
G.add(curBB);
curBB = b;
}
} else if(s.baseOperation == Statement.Operation.RETURN) {
//System.out.println("contains return: " + G.contains(curBB) + " and " + i + "; St.keys: " + StToBB.keySet());
if(StToBB.containsKey((long)(i+1))) {
//cur = ;
System.out.println("ok2");
curBB = vBB.get(StToBB.get((long)(i+1)));
} else {
//cur = vBB.size();
BaseBlock b = new BaseBlock();
vBB.add(b);
G.add(curBB);
curBB = b;
}
//System.out.println("finished");
} else if(StToBB.containsKey((long)(i+1))) {
G.add(curBB, vBB.get(StToBB.get((long)(i+1))));
curBB = vBB.get(StToBB.get((long)(i+1)));
}
i++;
}
//System.out.println("BB: " + vBB.get(StToBB.get((long)(12))).toString() + "; " + G.toString());
}
public String toString() {
return G.toString();
}
}