-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForkVisitor.java
458 lines (412 loc) · 17.5 KB
/
ForkVisitor.java
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package bridge.mvn;
import bridge.asm.Linked;
import bridge.asm.QueuedVisitor;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.ModuleVisitor;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import static org.objectweb.asm.Opcodes.*;
public final class ForkVisitor extends ClassVisitor {
static final byte NO_LINE_NUMBERS = 0b00001;
static final byte NO_MODULE_VERSIONS = 0b00010;
static final byte NO_SOURCE_NAMES = 0b00100;
static final byte NO_SOURCE_EXT = 0b01000;
static final byte NO_NAMED_LOCALS = 0b10000;
private final BridgeVisitor caller;
private final boolean names, lines;
private final int version, flags;
ForkVisitor(ClassVisitor delegate, BridgeVisitor caller, int version, int flags) {
super(ASM9, delegate);
this.caller = caller;
this.version = version;
this.flags = flags;
this.names = (flags & NO_NAMED_LOCALS) == 0;
this.lines = (flags & NO_LINE_NUMBERS) == 0;
}
@Override
public ModuleVisitor visitModule(String name, int access, String version) {
return super.visitModule(name, access, ((flags & NO_MODULE_VERSIONS) == 0)? version : null);
}
@Override
public void visitSource(String source, String debug) {
final int flags;
if (((flags = this.flags) & NO_SOURCE_NAMES) == 0 || (flags & NO_SOURCE_EXT) == 0) {
super.visitSource(((flags & NO_SOURCE_NAMES) == 0)? source : null, ((flags & NO_SOURCE_EXT) == 0)? debug : null);
}
}
@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
return new Fork(name, new Clean(super.visitMethod(access, name, descriptor, signature, exceptions)));
}
private final class Fork extends MethodVisitor implements Linked<MethodVisitor> {
private final String method;
private int line;
private Fork(String name, MethodVisitor delegate) {
super(ASM9, delegate);
this.method = name;
}
@Override
public <T extends MethodVisitor> T setDelegate(T value) {
mv = value;
return value;
}
@Override
public MethodVisitor getParent() {
return null;
}
@Override
public <T extends MethodVisitor> T setParent(T value) {
return value;
}
@Override
public void visitLineNumber(int line, Label start) {
super.visitLineNumber(this.line = line, start);
}
private RuntimeException exception(String message) {
StringBuilder str = new StringBuilder().append(message).append(": at ")
.append(caller.name.replace('/', '.')).append('.').append(method)
.append('(').append(caller.src);
if (line != 0) str.append(':').append(line);
return new IllegalStateException(str.append(") [target:").append(version).append(']').toString());
}
@Override
public void visitEnd() {
try {
super.visitEnd();
} catch (RuntimeException thrown) {
Throwable root = thrown;
for (Throwable cause = root.getCause(); cause != null; cause = cause.getCause()) {
root = cause;
}
root.initCause(root = exception("Failed recompiling method"));
root.setStackTrace(new StackTraceElement[0]);
throw thrown;
}
}
@Override
public void visitFieldInsn(int opcode, String owner, String name, String descriptor) {
if (opcode == GETSTATIC && "bridge/Invocation".equals(owner) && name.equals("LANGUAGE_LEVEL")) {
final class LANGUAGE_LEVEL extends MethodVisitor implements Linked<MethodVisitor> {
private MethodVisitor parent;
private int version;
@SuppressWarnings("unchecked")
private LANGUAGE_LEVEL(MethodVisitor delegate) {
super(ASM9, delegate);
if (delegate instanceof Linked) {
((Linked<MethodVisitor>) delegate).setParent(this);
}
}
@Override
public <T extends MethodVisitor> T setDelegate(T value) {
mv = value;
return value;
}
@Override
public MethodVisitor getParent() {
return parent;
}
@Override
public <T extends MethodVisitor> T setParent(T value) {
parent = value;
return value;
}
@Override
public void visitIntInsn(int opcode, int operand) {
if (opcode == BIPUSH || opcode == SIPUSH) {
version = operand;
} else {
super.visitIntInsn(opcode, operand);
}
}
@Override
public void visitLdcInsn(Object value) {
if (value instanceof Integer) {
version = (int) value;
} else {
super.visitLdcInsn(value);
}
}
@SuppressWarnings("unchecked")
@Override
public void visitJumpInsn(int opcode, Label label) {
switch (opcode) {
case IF_ICMPEQ:
if (ForkVisitor.this.version == version) super.visitJumpInsn(GOTO, label);
break;
case IF_ICMPNE:
if (ForkVisitor.this.version != version) super.visitJumpInsn(GOTO, label);
break;
case IF_ICMPGT:
if (ForkVisitor.this.version > version) super.visitJumpInsn(GOTO, label);
break;
case IF_ICMPLE:
if (ForkVisitor.this.version <= version) super.visitJumpInsn(GOTO, label);
break;
case IF_ICMPLT:
if (ForkVisitor.this.version < version) super.visitJumpInsn(GOTO, label);
break;
case IF_ICMPGE:
if (ForkVisitor.this.version >= version) super.visitJumpInsn(GOTO, label);
break;
default:
super.visitJumpInsn(opcode, label);
return;
}
if (((Linked<MethodVisitor>) parent).setDelegate(mv) instanceof Linked) {
((Linked<MethodVisitor>) mv).setParent(parent);
}
}
@Override
public void visitFrame(int type, int numLocal, Object[] local, int numStack, Object[] stack) {}
public void visitMaxs(int maxStack, int maxLocals) {}
public void visitEnd() {
throw exception("Method ended despite incomplete fork operation");
}
}
setDelegate(new LANGUAGE_LEVEL(mv)).setParent(this);
} else {
super.visitFieldInsn(opcode, owner, name, descriptor);
}
}
}
// --- general code cleanup ---
// > rewrites branches
// > merges labels
// > removes unreachable bytecode
// > removes user-specified debugging elements
private final class Clean extends QueuedVisitor {
private final HashMap<Label, LinkedHashMap<Runnable, Block>> handlers = new HashMap<>();
private final LinkedList<Block> blocks = new LinkedList<>();
private Block block;
private Runnable line;
private final class Block {
private LinkedList<Label> labels;
private LinkedList<Block> branches;
private LinkedList<Runnable> handlers, ops, attrs;
private Block merge, next;
private boolean used;
private int fop;
private Block dereference() {
Block block = this;
while (block.merge != null) block = block.merge;
return block;
}
private void merge(Block next) {
if (next.labels != null) {
if (labels == null) {
labels = next.labels;
} else {
labels.addAll(next.labels);
}
}
if (next.branches != null) {
if (branches == null) {
branches = next.branches;
} else {
branches.addAll(next.branches);
}
}
if (next.attrs != null) {
if (attrs == null) {
attrs = next.attrs;
} else {
attrs.addAll(next.attrs);
}
}
if (next.ops != null) ops = next.ops;
this.fop = next.fop;
this.next = next.next;
next.merge = this;
used = next.used;
}
private boolean merged(Block next) {
for (Block block = this;;) {
if ((block = block.dereference()) == next) return true;
if ((block.ops.size() != 0)) return false;
if ((block = block.next) == null) return true;
}
}
}
private Clean(MethodVisitor delegate) {
super(ASM9, delegate);
blocks.add(this.block = new Block());
block.ops = ops;
}
@Override
public void visitMaxs(int maxStack, int maxLocals) {
final Block block;
blocks.add(this.block = block = new Block());
block.ops = ops = new LinkedList<>();
block.used = true;
super.visitMaxs(maxStack, maxLocals);
}
@Override
public void visitLineNumber(int line, Label start) {
if (lines) this.line = () -> super.visitLineNumber(line, start);
}
@Override
public void visitOperation(int opcode, Runnable op) {
final Runnable line;
if ((line = this.line) != null) {
this.line = null;
line.run();
}
super.visitOperation(opcode, op);
}
@Override
public void visitParameter(String name, int access) {
if (names) super.visitParameter(name, access);
}
@Override
public void visitLocalVariable(String name, String descriptor, String signature, Label start, Label end, int index) {
if (names) {
final Block block = (end.info != null)? (Block) end.info : new Block();
if (block.attrs == null) block.attrs = new LinkedList<>();
block.attrs.add(() -> {
if (!((Block) start.info).merged(block.dereference())) {
mv.visitLocalVariable(name, descriptor, signature, start, end, index);
}
});
end.info = block;
}
}
@Override
public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
final Block sb = (start.info != null)? (Block) start.info : new Block();
if (sb.handlers == null) sb.handlers = new LinkedList<>();
sb.handlers.add(() -> {
final Block hb = (handler.info != null)? (Block) handler.info : new Block();
handlers.computeIfAbsent(end, k -> new LinkedHashMap<>()).put(() -> {
if (!sb.merged(((Block) end.info).dereference())) {
mv.visitTryCatchBlock(start, end, handler, type);
}
}, hb);
handler.info = hb;
});
start.info = sb;
}
@Override
public void visitLabel(Label label) {
LinkedHashMap<Runnable, Block> handlers = this.handlers.remove(label);
Block block = this.block;
if (label.info != null) {
final Block info = (Block) label.info;
if (info.handlers != null) for (Runnable handler : info.handlers) {
handler.run();
}
if (ops.size() != 0 || block.fop != 0) {
blocks.add(this.block = block = block.next = info);
block.ops = ops = new LinkedList<>();
}
else block.merge(info);
} else {
if (ops.size() != 0 || block.fop != 0) {
blocks.add(this.block = block = block.next = new Block());
block.ops = ops = new LinkedList<>();
}
}
if (this.handlers.size() != 0) {
if (block.branches == null) block.branches = new LinkedList<>();
for (Iterator<LinkedHashMap<Runnable, Block>> it = this.handlers.values().iterator(); it.hasNext();) {
block.branches.addAll(it.next().values());
}
}
if (handlers != null) {
if (block.attrs == null) block.attrs = new LinkedList<>();
block.attrs.addAll(handlers.keySet());
}
if (block.labels == null) block.labels = new LinkedList<>();
block.labels.add(label);
label.info = block;
}
@Override
public void visitInsn(int opcode) {
if (opcode >= IRETURN && opcode <= RETURN || opcode == ATHROW) {
block.fop = opcode;
blocks.add(this.block = new Block());
block.ops = ops = new LinkedList<>();
return;
}
super.visitInsn(opcode);
}
@Override
public void visitJumpInsn(int opcode, Label label) {
if (opcode == GOTO) {
label.info = block.next = (label.info != null)? (Block) label.info : new Block();
blocks.add(this.block = new Block());
block.ops = ops = new LinkedList<>();
return;
}
super.visitJumpInsn(opcode, label);
branch(label);
}
@Override
public void visitTableSwitchInsn(int min, int max, Label dflt, Label... labels) {
super.visitTableSwitchInsn(min, max, dflt, labels);
for (Label label : labels) branch(label);
branch(dflt);
blocks.add(this.block = new Block());
block.ops = ops = new LinkedList<>();
}
@Override
public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
super.visitLookupSwitchInsn(dflt, keys, labels);
for (Label label : labels) branch(label);
branch(dflt);
blocks.add(this.block = new Block());
block.ops = ops = new LinkedList<>();
}
private void branch(Label label) {
final Block block, info;
if ((block = this.block).branches == null) block.branches = new LinkedList<>();
block.branches.add(info = (label.info != null)? (Block) label.info : new Block());
label.info = info;
}
@Override
public void visitEnd() {
LinkedList<Block> branches = new LinkedList<>();
for (Block block = blocks.getFirst();;) {
if (!block.used) {
block.used = true;
if (block.branches != null && block.ops.size() != 0) {
branches.addAll(block.branches);
}
if ((block = block.next) != null) {
block = block.dereference();
continue;
}
}
if (branches.size() == 0) break;
block = branches.removeFirst().dereference();
}
Block next = null;
for (Iterator<Block> it = blocks.iterator(); it.hasNext();) {
Block block = it.next();
while (!block.used) {
block.merge(it.next());
}
if (next != null && (next = next.dereference()) != block) {
mv.visitJumpInsn(GOTO, next.labels.getFirst());
}
if (block.labels != null) for (Label label : block.labels) {
mv.visitLabel(label);
}
if (block.attrs != null) for (Runnable attr : block.attrs) {
attr.run();
}
for (Runnable op : block.ops) {
op.run();
}
if (block.fop != 0) {
mv.visitInsn(block.fop);
}
next = block.next;
}
mv.visitEnd();
}
}
}