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
|
/* *******************************************************************
* Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Common Public License v1.0
* which accompanies this distribution and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* PARC initial implementation
* ******************************************************************/
package org.aspectj.weaver.bcel;
import org.aspectj.apache.bcel.generic.BranchInstruction;
import org.aspectj.apache.bcel.generic.IndexedInstruction;
import org.aspectj.apache.bcel.generic.Instruction;
import org.aspectj.apache.bcel.generic.InstructionFactory;
import org.aspectj.apache.bcel.generic.InstructionHandle;
import org.aspectj.apache.bcel.generic.InstructionList;
import org.aspectj.apache.bcel.generic.InstructionTargeter;
import org.aspectj.apache.bcel.generic.LocalVariableInstruction;
import org.aspectj.apache.bcel.generic.RET;
import org.aspectj.apache.bcel.generic.Select;
import org.aspectj.apache.bcel.generic.TargetLostException;
import org.aspectj.weaver.BCException;
import org.aspectj.weaver.IntMap;
import org.aspectj.weaver.Shadow;
final class ShadowRange extends Range {
private BcelShadow shadow;
// ---- initialization
/**
* After this constructor is called, this range is not well situated unless both
* {@link #associateWithTargets} and {@link #associateWithShadow} are called.
*/
public ShadowRange(InstructionList body) {
super(body);
}
protected void associateWithTargets(InstructionHandle start, InstructionHandle end) {
// assert body.contains(start) && body.contains(end);
this.start = start;
this.end = end;
start.addTargeter(this);
end.addTargeter(this);
}
public void associateWithShadow(BcelShadow shadow) {
this.shadow = shadow;
shadow.setRange(this);
}
// ----
public Shadow.Kind getKind() {
return shadow.getKind();
}
public String toString() {
return shadow.toString();
}
void extractInstructionsInto(LazyMethodGen freshMethod, IntMap remap, boolean addReturn) {
LazyMethodGen.assertGoodBody(getBody(), toString());
freshMethod.assertGoodBody();
InstructionList freshBody = freshMethod.getBody();
for (InstructionHandle oldIh = start.getNext(); oldIh != end; oldIh = oldIh.getNext()) {
// first we copy the instruction itself.
Instruction oldI = oldIh.getInstruction();
Instruction freshI = (oldI == RANGEINSTRUCTION) ? oldI : Utility.copyInstruction(oldI);
// Now we add it to the new instruction list.
InstructionHandle freshIh;
if (freshI instanceof BranchInstruction) {
//If it's a targeting instruction,
// update the target(s) to point to the new copy instead of the old copy.
BranchInstruction oldBranch = (BranchInstruction) oldI;
BranchInstruction freshBranch = (BranchInstruction) freshI;
InstructionHandle oldTarget = oldBranch.getTarget();
oldTarget.removeTargeter(oldBranch);
oldTarget.addTargeter(freshBranch);
if (freshBranch instanceof Select) {
Select oldSelect = (Select) oldI;
Select freshSelect = (Select) freshI;
InstructionHandle[] oldTargets = freshSelect.getTargets();
for (int k = oldTargets.length - 1; k >= 0; k--) {
oldTargets[k].removeTargeter(oldSelect);
oldTargets[k].addTargeter(freshSelect);
}
}
freshIh = freshBody.append(freshBranch);
} else {
freshIh = freshBody.append(freshI);
}
// if source comes before target:
// source <--> target
// --> [process: target.removeTargeter(source); target.addTargeter(sourcecopy)]
// source ---------\
// v
// sourcecopy <--> target
// --> [ process: sourcecopy.updateTarget(target, targetcopy) ]
// source ----> target
// sourcecopy <--> targetcopy
// if target comes before source
// target <--> source
// --> [process: source.updateTarget(target, targetcopy) ]
// target
// targetcopy <--> source
// --> [process: targetcopy.removeTargeter(source); targetcopy.addTargeter(sourcecopy)]
// target source
// v
// targetcopy <--> sourcecopy
// now deal with the old instruction's targeters. Update them all to point to us
// instead of the old instruction. We use updateTarget to do this. One goal is
// to make sure we remove all targeters from the old guy, so we can successfully
// delete it.
InstructionTargeter[] sources = oldIh.getTargeters();
if (sources != null) {
for (int j = sources.length - 1; j >= 0; j--) {
InstructionTargeter source = sources[j];
if (source instanceof LocalVariableTag) {
Shadow.Kind kind = getKind();
if (kind == Shadow.AdviceExecution ||
kind == Shadow.ConstructorExecution ||
kind == Shadow.MethodExecution ||
kind == Shadow.PreInitialization ||
kind == Shadow.Initialization ||
kind == Shadow.StaticInitialization) {
// if we're extracting a whole block we can do this...
source.updateTarget(oldIh, freshIh);
} else {
// XXX destroying local variable info
// but only for a call or get join point, so no big deal
source.updateTarget(oldIh, null);
}
} else if (source instanceof Range) {
// exceptions and shadows are just moved
((Range)source).updateTarget(oldIh, freshIh, freshBody);
} else {
// line numbers can be shared,
// branches will be copied along with us.
source.updateTarget(oldIh, freshIh);
}
}
}
// we're now done with the old instruction entirely, and will ignore them through
// the rest of this loop. The only time we'll see them again is a second pass to
// delete them.
// now deal with local variable instructions. If this points to a remapped
// frame location, update the instruction's index. If this doesn't,
// do compaction/expansion: allocate a new local variable, and modify the remap
// to handle it. XXX We're doing the safe thing and allocating ALL these local variables
// as double-wides, in case the location is found to hold a double-wide later.
if (freshI instanceof LocalVariableInstruction || freshI instanceof RET) {
IndexedInstruction indexedI = (IndexedInstruction) freshI;
int oldIndex = indexedI.getIndex();
int freshIndex;
if (! remap.hasKey(oldIndex)) {
freshIndex = freshMethod.allocateLocal(2);
remap.put(oldIndex, freshIndex);
} else {
freshIndex = remap.get(oldIndex);
}
indexedI.setIndex(freshIndex);
}
// System.err.println("JUST COPIED: " + oldIh.getInstruction().toString(freshMethod.getEnclosingClass().getConstantPoolGen().getConstantPool())
// + " INTO " + freshIh.getInstruction().toString(freshMethod.getEnclosingClass().getConstantPoolGen().getConstantPool()));
}
// now go through again and update variable slots that have been altered as a result
// of remapping...
for (InstructionHandle newIh = freshBody.getStart(); newIh != freshBody.getEnd(); newIh = newIh.getNext()) {
InstructionTargeter[] sources = newIh.getTargeters();
if (sources != null) {
for (int i = sources.length - 1; i >= 0; i--) {
if (sources[i] instanceof LocalVariableTag) {
LocalVariableTag lvt = (LocalVariableTag) sources[i];
if (!lvt.isRemapped() && remap.hasKey(lvt.getSlot())) {
lvt.updateSlot(remap.get(lvt.getSlot()));
}
}
}
}
}
// we've now copied out all the instructions.
// now delete the instructions... we've already taken care of the damn
// targets, but since TargetLostException is checked, we have to do this stuff.
try {
for (InstructionHandle oldIh = start.getNext(); oldIh != end; ) {
InstructionHandle next = oldIh.getNext();
body.delete(oldIh);
oldIh = next;
}
} catch (TargetLostException e) {
throw new BCException("shouldn't have gotten a target lost");
}
// now add the return, if one is warranted.
InstructionHandle ret = null;
if (addReturn) {
// we really should pull this out somewhere...
ret = freshBody.append(
InstructionFactory.createReturn(freshMethod.getReturnType()));
}
// and remap all the old targeters of the end handle of the range to the return.
InstructionTargeter[] ts = end.getTargeters();
if (ts != null) { // shouldn't be the case, but let's test for paranoia
for (int j = ts.length - 1; j >= 0; j--) {
InstructionTargeter t = ts[j];
if (t == this) continue;
if (! addReturn) {
throw new BCException("range has target, but we aren't adding a return");
} else {
t.updateTarget(end, ret);
}
}
}
LazyMethodGen.assertGoodBody(getBody(), toString());
freshMethod.assertGoodBody();
}
public BcelShadow getShadow() {
return shadow;
}
}
|