forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGrimpBody.java
More file actions
280 lines (244 loc) · 8.42 KB
/
GrimpBody.java
File metadata and controls
280 lines (244 loc) · 8.42 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package soot.grimp;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 1999 Patrick Lam
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import soot.Body;
import soot.Local;
import soot.PackManager;
import soot.SootMethod;
import soot.Trap;
import soot.Unit;
import soot.ValueBox;
import soot.jimple.AbstractStmtSwitch;
import soot.jimple.AssignStmt;
import soot.jimple.BreakpointStmt;
import soot.jimple.EnterMonitorStmt;
import soot.jimple.ExitMonitorStmt;
import soot.jimple.GotoStmt;
import soot.jimple.IdentityStmt;
import soot.jimple.IfStmt;
import soot.jimple.InvokeStmt;
import soot.jimple.JimpleBody;
import soot.jimple.LookupSwitchStmt;
import soot.jimple.NopStmt;
import soot.jimple.ReturnStmt;
import soot.jimple.ReturnVoidStmt;
import soot.jimple.Stmt;
import soot.jimple.StmtBody;
import soot.jimple.TableSwitchStmt;
import soot.jimple.ThrowStmt;
import soot.jimple.internal.StmtBox;
import soot.options.Options;
import soot.tagkit.LineNumberTag;
import soot.tagkit.SourceLnPosTag;
import soot.tagkit.Tag;
import soot.util.Chain;
/**
* Implementation of the Body class for the Grimp IR.
*/
public class GrimpBody extends StmtBody {
private static final Logger logger = LoggerFactory.getLogger(GrimpBody.class);
/**
* Construct an empty GrimpBody
*/
GrimpBody(SootMethod m) {
super(m);
}
@Override
public Object clone() {
Body b = Grimp.v().newBody(getMethodUnsafe());
b.importBodyContentsFrom(this);
return b;
}
@Override
public Object clone(boolean noLocalsClone) {
Body b = Grimp.v().newBody(getMethod());
b.importBodyContentsFrom(this, true);
return b;
}
/**
* Constructs a GrimpBody from the given Body.
*/
GrimpBody(Body body) {
super(body.getMethod());
if (Options.v().verbose()) {
logger.debug("[" + getMethod().getName() + "] Constructing GrimpBody...");
}
if (!(body instanceof JimpleBody)) {
throw new RuntimeException("Can only construct GrimpBody's from JimpleBody's (for now)");
}
final JimpleBody jBody = (JimpleBody) body;
{
final Chain<Local> thisLocals = this.getLocals();
for (Local loc : jBody.getLocals()) {
thisLocals.add(loc);
// thisLocals.add((Local)loc.clone());
}
}
final Grimp grimp = Grimp.v();
final Chain<Unit> thisUnits = getUnits();
final HashMap<Stmt, Stmt> oldToNew = new HashMap<Stmt, Stmt>(thisUnits.size() * 2 + 1, 0.7f);
final ArrayList<Unit> updates = new ArrayList<Unit>();
/* we should Grimpify the Stmt's here... */
for (Unit u : jBody.getUnits()) {
Stmt oldStmt = (Stmt) u;
final StmtBox newStmtBox = (StmtBox) grimp.newStmtBox(null);
final StmtBox updateStmtBox = (StmtBox) grimp.newStmtBox(null);
/* we can't have a general StmtSwapper on Grimp.v() */
/* because we need to collect a list of updates */
oldStmt.apply(new AbstractStmtSwitch() {
@Override
public void caseAssignStmt(AssignStmt s) {
newStmtBox.setUnit(grimp.newAssignStmt(s));
}
@Override
public void caseIdentityStmt(IdentityStmt s) {
newStmtBox.setUnit(grimp.newIdentityStmt(s));
}
@Override
public void caseBreakpointStmt(BreakpointStmt s) {
newStmtBox.setUnit(grimp.newBreakpointStmt(s));
}
@Override
public void caseInvokeStmt(InvokeStmt s) {
newStmtBox.setUnit(grimp.newInvokeStmt(s));
}
@Override
public void caseEnterMonitorStmt(EnterMonitorStmt s) {
newStmtBox.setUnit(grimp.newEnterMonitorStmt(s));
}
@Override
public void caseExitMonitorStmt(ExitMonitorStmt s) {
newStmtBox.setUnit(grimp.newExitMonitorStmt(s));
}
@Override
public void caseGotoStmt(GotoStmt s) {
newStmtBox.setUnit(grimp.newGotoStmt(s));
updateStmtBox.setUnit(s);
}
@Override
public void caseIfStmt(IfStmt s) {
newStmtBox.setUnit(grimp.newIfStmt(s));
updateStmtBox.setUnit(s);
}
@Override
public void caseLookupSwitchStmt(LookupSwitchStmt s) {
newStmtBox.setUnit(grimp.newLookupSwitchStmt(s));
updateStmtBox.setUnit(s);
}
@Override
public void caseNopStmt(NopStmt s) {
newStmtBox.setUnit(grimp.newNopStmt(s));
}
@Override
public void caseReturnStmt(ReturnStmt s) {
newStmtBox.setUnit(grimp.newReturnStmt(s));
}
@Override
public void caseReturnVoidStmt(ReturnVoidStmt s) {
newStmtBox.setUnit(grimp.newReturnVoidStmt(s));
}
@Override
public void caseTableSwitchStmt(TableSwitchStmt s) {
newStmtBox.setUnit(grimp.newTableSwitchStmt(s));
updateStmtBox.setUnit(s);
}
@Override
public void caseThrowStmt(ThrowStmt s) {
newStmtBox.setUnit(grimp.newThrowStmt(s));
}
});
/* map old Expr's to new Expr's. */
Stmt newStmt = (Stmt) newStmtBox.getUnit();
for (ValueBox box : newStmt.getUseBoxes()) {
box.setValue(grimp.newExpr(box.getValue()));
}
for (ValueBox box : newStmt.getDefBoxes()) {
box.setValue(grimp.newExpr(box.getValue()));
}
thisUnits.add(newStmt);
oldToNew.put(oldStmt, newStmt);
if (updateStmtBox.getUnit() != null) {
updates.add(updateStmtBox.getUnit());
}
final Tag lnTag = oldStmt.getTag(LineNumberTag.NAME);
if (lnTag != null) {
newStmt.addTag(lnTag);
}
final Tag slpTag = oldStmt.getTag(SourceLnPosTag.NAME);
if (slpTag != null) {
newStmt.addTag(slpTag);
}
}
/* fixup stmt's which have had moved targets */
{
final AbstractStmtSwitch tgtUpdateSwitch = new AbstractStmtSwitch() {
@Override
public void caseGotoStmt(GotoStmt s) {
GotoStmt newStmt = (GotoStmt) oldToNew.get(s);
newStmt.setTarget(oldToNew.get((Stmt) newStmt.getTarget()));
}
@Override
public void caseIfStmt(IfStmt s) {
IfStmt newStmt = (IfStmt) oldToNew.get(s);
newStmt.setTarget(oldToNew.get(newStmt.getTarget()));
}
@Override
public void caseLookupSwitchStmt(LookupSwitchStmt s) {
LookupSwitchStmt newStmt = (LookupSwitchStmt) oldToNew.get(s);
newStmt.setDefaultTarget(oldToNew.get((Stmt) newStmt.getDefaultTarget()));
Unit[] newTargList = new Unit[newStmt.getTargetCount()];
for (int i = 0; i < newTargList.length; i++) {
newTargList[i] = oldToNew.get((Stmt) newStmt.getTarget(i));
}
newStmt.setTargets(newTargList);
}
@Override
public void caseTableSwitchStmt(TableSwitchStmt s) {
TableSwitchStmt newStmt = (TableSwitchStmt) oldToNew.get(s);
newStmt.setDefaultTarget(oldToNew.get((Stmt) newStmt.getDefaultTarget()));
int tc = newStmt.getHighIndex() - newStmt.getLowIndex() + 1;
LinkedList<Unit> newTargList = new LinkedList<Unit>();
for (int i = 0; i < tc; i++) {
newTargList.add(oldToNew.get((Stmt) newStmt.getTarget(i)));
}
newStmt.setTargets(newTargList);
}
};
for (Unit u : updates) {
u.apply(tgtUpdateSwitch);
}
}
{
final Chain<Trap> thisTraps = getTraps();
for (Trap oldTrap : jBody.getTraps()) {
thisTraps.add(grimp.newTrap(oldTrap.getException(), oldToNew.get((Stmt) oldTrap.getBeginUnit()),
oldToNew.get((Stmt) oldTrap.getEndUnit()), oldToNew.get((Stmt) oldTrap.getHandlerUnit())));
}
}
PackManager.v().getPack("gb").apply(this);
}
}