-
Notifications
You must be signed in to change notification settings - Fork 183
/
Code.java
614 lines (493 loc) · 14.9 KB
/
Code.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
package org.python.compiler;
import java.util.BitSet;
import java.util.Vector;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
public class Code extends MethodVisitor implements Opcodes {
MethodVisitor mv;
String sig;
String locals[];
int nlocals;
int argcount;
int returnLocal;
BitSet finallyLocals = new java.util.BitSet();
//XXX: I'd really like to get sig and access out of here since MethodVistitor
// should already have this information.
public Code(MethodVisitor mv, String sig, int access) {
super(ASM7);
this.mv = mv;
this.sig = sig;
nlocals = -sigSize(sig, false);
if ((access & ACC_STATIC) != ACC_STATIC) nlocals = nlocals+1;
argcount = nlocals;
locals = new String[nlocals+128];
}
public int getLocal(String type) {
//Could optimize this to skip arguments?
for(int l = argcount; l<nlocals; l++) {
if (locals[l] == null) {
locals[l] = type;
return l;
}
}
if (nlocals >= locals.length) {
String[] new_locals = new String[locals.length*2];
System.arraycopy(locals, 0, new_locals, 0, locals.length);
locals = new_locals;
}
locals[nlocals] = type;
nlocals += 1;
return nlocals-1;
}
public void freeLocal(int l) {
if (locals[l] == null) {
System.out.println("Double free:" + l);
}
locals[l] = null;
}
public int getFinallyLocal(String type) {
int l = getLocal(type);
finallyLocals.set(l);
return l;
}
public void freeFinallyLocal(int l) {
finallyLocals.clear(l);
freeLocal(l);
}
public int getReturnLocal() {
if (returnLocal == 0)
returnLocal = getLocal("return");
return returnLocal;
}
public Vector<String> getActiveLocals() {
Vector<String> ret = new Vector<String>();
ret.setSize(nlocals);
for (int l = argcount; l<nlocals; l++) {
if (l == returnLocal || finallyLocals.get(l))
continue;
ret.setElementAt(locals[l], l);
}
return ret;
}
public AnnotationVisitor visitAnnotation(String arg0, boolean arg1) {
return mv.visitAnnotation(arg0, arg1);
}
public AnnotationVisitor visitAnnotationDefault() {
return mv.visitAnnotationDefault();
}
public void visitAttribute(Attribute arg0) {
mv.visitAttribute(arg0);
}
public void visitCode() {
mv.visitCode();
}
public void visitEnd() {
mv.visitEnd();
}
public void visitFieldInsn(int arg0, String arg1, String arg2, String arg3) {
mv.visitFieldInsn(arg0, arg1, arg2, arg3);
}
public void visitFrame(int arg0, int arg1, Object[] arg2, int arg3, Object[] arg4) {
mv.visitFrame(arg0, arg1, arg2, arg3, arg4);
}
public void visitIincInsn(int arg0, int arg1) {
mv.visitIincInsn(arg0, arg1);
}
public void visitInsn(int arg0) {
mv.visitInsn(arg0);
}
public void visitIntInsn(int arg0, int arg1) {
mv.visitIntInsn(arg0, arg1);
}
public void visitJumpInsn(int arg0, Label arg1) {
mv.visitJumpInsn(arg0, arg1);
}
public void visitLabel(Label arg0) {
mv.visitLabel(arg0);
}
public void visitLdcInsn(Object arg0) {
mv.visitLdcInsn(arg0);
}
public void visitLineNumber(int arg0, Label arg1) {
mv.visitLineNumber(arg0, arg1);
}
public void visitLocalVariable(String arg0, String arg1, String arg2, Label arg3, Label arg4, int arg5) {
mv.visitLocalVariable(arg0, arg1, arg2, arg3, arg4, arg5);
}
public void visitLookupSwitchInsn(Label arg0, int[] arg1, Label[] arg2) {
mv.visitLookupSwitchInsn(arg0, arg1, arg2);
}
public void visitMaxs(int arg0, int arg1) {
mv.visitMaxs(arg0, arg1);
}
public void visitMethodInsn(int arg0, String arg1, String arg2, String arg3, boolean itf) {
mv.visitMethodInsn(arg0, arg1, arg2, arg3, itf);
}
public void visitMultiANewArrayInsn(String arg0, int arg1) {
mv.visitMultiANewArrayInsn(arg0, arg1);
}
public AnnotationVisitor visitParameterAnnotation(int arg0, String arg1, boolean arg2) {
return mv.visitParameterAnnotation(arg0, arg1, arg2);
}
public void visitTableSwitchInsn(int arg0, int arg1, Label arg2, Label... arg3) {
mv.visitTableSwitchInsn(arg0, arg1, arg2, arg3);
}
public void visitTryCatchBlock(Label arg0, Label arg1, Label arg2, String arg3) {
mv.visitTryCatchBlock(arg0, arg1, arg2, arg3);
}
public void visitTypeInsn(int arg0, String arg1) {
mv.visitTypeInsn(arg0, arg1);
}
public void visitVarInsn(int arg0, int arg1) {
mv.visitVarInsn(arg0, arg1);
}
@SuppressWarnings("fallthrough")
private int sigSize(String sig, boolean includeReturn) {
int stack = 0;
int i = 0;
char[] c = sig.toCharArray();
int n = c.length;
boolean ret=false;
boolean array=false;
while (++i<n) {
switch (c[i]) {
case ')':
if (!includeReturn)
return stack;
ret=true;
continue;
case '[':
array=true;
continue;
case 'V':
continue;
case 'D':
case 'J':
if (array) {
if (ret) stack += 1;
else stack -=1;
array = false;
} else {
if (ret) stack += 2;
else stack -=2;
}
break;
case 'L':
while (c[++i] != ';') {}
default:
if (ret) stack++;
else stack--;
array = false;
}
}
return stack;
}
public void aaload() {
mv.visitInsn(AALOAD);
}
public void aastore() {
mv.visitInsn(AASTORE);
}
public void aconst_null() {
mv.visitInsn(ACONST_NULL);
}
public void aload(int index) {
mv.visitVarInsn(ALOAD, index);
}
public void anewarray(String index) {
mv.visitTypeInsn(ANEWARRAY, index);
}
public void areturn() {
mv.visitInsn(ARETURN);
}
public void arraylength() {
mv.visitInsn(ARRAYLENGTH);
}
public void astore(int index) {
mv.visitVarInsn(ASTORE, index);
}
public void athrow() {
mv.visitInsn(ATHROW);
}
public void baload() {
mv.visitInsn(BALOAD);
}
public void bastore() {
mv.visitInsn(BASTORE);
}
public void bipush(int value) {
mv.visitIntInsn(BIPUSH, value);
}
public void checkcast(String type) {
mv.visitTypeInsn(CHECKCAST, type);
}
public void dconst_0() {
mv.visitInsn(DCONST_0);
}
public void dload(int index) {
mv.visitVarInsn(DLOAD, index);
}
public void dreturn() {
mv.visitInsn(DRETURN);
}
public void dup() {
mv.visitInsn(DUP);
}
public void dup2() {
mv.visitInsn(DUP2);
}
public void dup_x1() {
mv.visitInsn(DUP_X1);
}
public void dup_x2() {
mv.visitInsn(DUP_X2);
}
public void dup2_x1() {
mv.visitInsn(DUP2_X1);
}
public void dup2_x2() {
mv.visitInsn(DUP2_X2);
}
public void fconst_0() {
mv.visitInsn(FCONST_0);
}
public void fload(int index) {
mv.visitVarInsn(FLOAD, index);
}
public void freturn() {
mv.visitInsn(FRETURN);
}
public void getfield(String owner, String name, String type) {
mv.visitFieldInsn(GETFIELD, owner, name, type);
}
public void getstatic(String owner, String name, String type) {
mv.visitFieldInsn(GETSTATIC, owner, name, type);
}
public void goto_(Label label) {
mv.visitJumpInsn(GOTO, label);
}
public void iconst(int value) {
if (value <= Byte.MAX_VALUE && value >= Byte.MIN_VALUE) {
switch (value) {
case -1:
iconst_m1();
break;
case 0:
iconst_0();
break;
case 1:
iconst_1();
break;
case 2:
iconst_2();
break;
case 3:
iconst_3();
break;
case 4:
iconst_4();
break;
case 5:
iconst_5();
break;
default:
bipush(value);
break;
}
} else if (value <= Short.MAX_VALUE && value >= Short.MIN_VALUE) {
sipush(value);
} else {
ldc(value);
}
}
public void iconst_m1() {
mv.visitInsn(ICONST_M1);
}
public void iconst_0() {
mv.visitInsn(ICONST_0);
}
public void iconst_1() {
mv.visitInsn(ICONST_1);
}
public void iconst_2() {
mv.visitInsn(ICONST_2);
}
public void iconst_3() {
mv.visitInsn(ICONST_3);
}
public void iconst_4() {
mv.visitInsn(ICONST_4);
}
public void iconst_5() {
mv.visitInsn(ICONST_5);
}
public void ifeq(Label label) {
mv.visitJumpInsn(IFEQ, label);
}
public void ifle(Label label) {
mv.visitJumpInsn(IFLE, label);
}
public void ifne(Label label) {
mv.visitJumpInsn(IFNE, label);
}
public void ifnull(Label label) {
mv.visitJumpInsn(IFNULL, label);
}
public void ifnonnull(Label label) {
mv.visitJumpInsn(IFNONNULL, label);
}
public void if_acmpne(Label label) {
mv.visitJumpInsn(IF_ACMPNE, label);
}
public void if_acmpeq(Label label) {
mv.visitJumpInsn(IF_ACMPEQ, label);
}
public void if_icmple(Label label) {
mv.visitJumpInsn(IF_ICMPLE, label);
}
public void if_icmpgt(Label label) {
mv.visitJumpInsn(IF_ICMPGT, label);
}
public void if_icmplt(Label label) {
mv.visitJumpInsn(IF_ICMPLT, label);
}
public void if_icmpne(Label label) {
mv.visitJumpInsn(IF_ICMPNE, label);
}
public void if_icmpeq(Label label) {
mv.visitJumpInsn(IF_ICMPEQ, label);
}
public void iadd() {
mv.visitInsn(IADD);
}
public void iaload() {
mv.visitInsn(IALOAD);
}
public void iinc() {
mv.visitInsn(IINC);
}
public void iload(int index) {
mv.visitVarInsn(ILOAD, index);
}
public void instanceof_(String type) {
mv.visitTypeInsn(INSTANCEOF, type);
}
public void invokeinterface(String owner, String name, String type) {
mv.visitMethodInsn(INVOKEINTERFACE, owner, name, type, false);
}
public void invokeinterface(String owner, String name, String type, boolean itf) {
mv.visitMethodInsn(INVOKEINTERFACE, owner, name, type, itf);
}
public void invokespecial(String owner, String name, String type) {
mv.visitMethodInsn(INVOKESPECIAL, owner, name, type, false);
}
public void invokestatic(String owner, String name, String type) {
mv.visitMethodInsn(INVOKESTATIC, owner, name, type, false);
}
public void invokevirtual(String owner, String name, String type) {
mv.visitMethodInsn(INVOKEVIRTUAL, owner, name, type, false);
}
public void ireturn() {
mv.visitInsn(IRETURN);
}
public void istore(int index) {
mv.visitVarInsn(ISTORE, index);
}
public void isub() {
mv.visitInsn(ISUB);
}
public void label(Label label) {
mv.visitLabel(label);
}
public void lconst_0() {
mv.visitInsn(LCONST_0);
}
public void ldc(Object cst) {
if (cst instanceof String) {
String value = (String) cst;
final int len = value.length();
// 65535 / 4 (max utf-8 expansion for non BMP characters)
final int maxlen = 16000;
if (len > maxlen) {
new_("java/lang/StringBuilder");
dup();
iconst(len);
invokespecial("java/lang/StringBuilder", "<init>", "(I)V");
for (int i = 0; i < len; i += maxlen) {
int j = i + maxlen;
if (j > len) {
j = len;
}
mv.visitLdcInsn(value.substring(i, j));
invokevirtual("java/lang/StringBuilder", "append", "(Ljava/lang/String;)" + "Ljava/lang/StringBuilder;");
}
invokevirtual("java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
} else {
mv.visitLdcInsn(value);
}
} else {
mv.visitLdcInsn(cst);
}
}
public void lload(int index) {
mv.visitVarInsn(LLOAD, index);
}
public void lreturn() {
mv.visitInsn(LRETURN);
}
public void newarray(int atype) {
mv.visitIntInsn(NEWARRAY, atype);
}
public void new_(String type) {
mv.visitTypeInsn(NEW, type);
}
public void nop() {
mv.visitInsn(NOP);
}
public void pop() {
mv.visitInsn(POP);
}
public void pop2() {
mv.visitInsn(POP2);
}
public void putstatic(String owner, String name, String type) {
mv.visitFieldInsn(PUTSTATIC, owner, name, type);
}
public void putfield(String owner, String name, String type) {
mv.visitFieldInsn(PUTFIELD, owner, name, type);
}
public void ret(int index) {
mv.visitVarInsn(RET, index);
}
public void return_() {
mv.visitInsn(RETURN);
}
public void sipush(int value) {
mv.visitIntInsn(SIPUSH, value);
}
public void swap() {
mv.visitInsn(SWAP);
}
public void swap2() {
dup2_x2();
pop2();
}
public void tableswitch(int arg0, int arg1, Label arg2, Label[] arg3) {
mv.visitTableSwitchInsn(arg0, arg1, arg2, arg3);
}
public void trycatch(Label start, Label end, Label handlerStart, String type) {
mv.visitTryCatchBlock(start, end, handlerStart, type);
}
public void setline(int line) {
mv.visitLineNumber(line, new Label());
}
@Override
public void visitInvokeDynamicInsn(String name, String descriptor, Handle bsmHandle,
Object... bmsArgs) {
mv.visitInvokeDynamicInsn(name, descriptor, bsmHandle, bmsArgs);
}
}