-
Notifications
You must be signed in to change notification settings - Fork 41
/
BytecodeCreatorImpl.java
1862 lines (1631 loc) · 70.4 KB
/
BytecodeCreatorImpl.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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2018 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.quarkus.gizmo;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
class BytecodeCreatorImpl implements BytecodeCreator {
private static final MethodDescriptor THREAD_CURRENT_THREAD = MethodDescriptor.ofMethod(Thread.class, "currentThread",
Thread.class);
private static final MethodDescriptor THREAD_GET_TCCL = MethodDescriptor.ofMethod(Thread.class, "getContextClassLoader",
ClassLoader.class);
private static final MethodDescriptor CL_FOR_NAME = MethodDescriptor.ofMethod(Class.class, "forName", Class.class,
String.class,
boolean.class, ClassLoader.class);
private static final boolean DEBUG_SCOPES = Boolean.getBoolean("io.quarkus.gizmo.debug-scopes");
private static final Map<String, AtomicInteger> functionCountersByClass = new ConcurrentHashMap<>();
private static final String FUNCTION = "$$function$$";
protected final List<Operation> operations = new ArrayList<>();
private final MethodCreatorImpl method;
private final BytecodeCreatorImpl owner;
private final Label top = new Label();
private final Label bottom = new Label();
private final StackTraceElement[] stack;
private static final Map<String, String> boxingMap;
private static final Map<String, String> boxingMethodMap;
private ResultHandle cachedTccl;
static {
Map<String, String> b = new HashMap<>();
b.put("Z", Type.getInternalName(Boolean.class));
b.put("B", Type.getInternalName(Byte.class));
b.put("C", Type.getInternalName(Character.class));
b.put("S", Type.getInternalName(Short.class));
b.put("I", Type.getInternalName(Integer.class));
b.put("J", Type.getInternalName(Long.class));
b.put("F", Type.getInternalName(Float.class));
b.put("D", Type.getInternalName(Double.class));
boxingMap = Collections.unmodifiableMap(b);
b = new HashMap<>();
b.put("Z", "booleanValue");
b.put("B", "byteValue");
b.put("C", "charValue");
b.put("S", "shortValue");
b.put("I", "intValue");
b.put("J", "longValue");
b.put("F", "floatValue");
b.put("D", "doubleValue");
boxingMethodMap = Collections.unmodifiableMap(b);
}
Label getTop() {
return top;
}
Label getBottom() {
return bottom;
}
BytecodeCreatorImpl(BytecodeCreatorImpl enclosing, MethodCreatorImpl methodCreator) {
this.method = methodCreator;
this.owner = enclosing;
stack = DEBUG_SCOPES ? new Throwable().getStackTrace() : null;
}
BytecodeCreatorImpl(BytecodeCreatorImpl enclosing, boolean useThisMethod) {
this.method = useThisMethod ? (MethodCreatorImpl) this : enclosing.getMethod();
this.owner = enclosing;
stack = DEBUG_SCOPES ? new Throwable().getStackTrace() : null;
}
BytecodeCreatorImpl(BytecodeCreatorImpl enclosing) {
this(enclosing, enclosing.getMethod());
}
@Override
public ResultHandle getThis() {
ResultHandle resultHandle = new ResultHandle("L" + getMethod().getDeclaringClassName().replace('.', '/') + ";", this);
resultHandle.setNo(0);
return resultHandle;
}
@Override
public ResultHandle invokeVirtualMethod(MethodDescriptor descriptor, ResultHandle object, ResultHandle... args) {
Objects.requireNonNull(descriptor);
Objects.requireNonNull(object);
ResultHandle ret = allocateResult(descriptor.getReturnType());
operations.add(new InvokeOperation(ret, descriptor, resolve(checkScope(object)), resolve(checkScope(args)), false, false));
return ret;
}
@Override
public ResultHandle invokeInterfaceMethod(MethodDescriptor descriptor, ResultHandle object, ResultHandle... args) {
Objects.requireNonNull(descriptor);
Objects.requireNonNull(object);
ResultHandle ret = allocateResult(descriptor.getReturnType());
operations.add(new InvokeOperation(ret, descriptor, resolve(checkScope(object)), resolve(checkScope(args)), true, false));
return ret;
}
@Override
public ResultHandle invokeStaticMethod(MethodDescriptor descriptor, ResultHandle... args) {
Objects.requireNonNull(descriptor);
ResultHandle ret = allocateResult(descriptor.getReturnType());
operations.add(new InvokeOperation(ret, descriptor, resolve(checkScope(args)), false));
return ret;
}
public ResultHandle invokeStaticInterfaceMethod(MethodDescriptor descriptor, ResultHandle... args) {
Objects.requireNonNull(descriptor);
ResultHandle ret = allocateResult(descriptor.getReturnType());
operations.add(new InvokeOperation(ret, descriptor, resolve(checkScope(args)), true));
return ret;
}
@Override
public ResultHandle invokeSpecialMethod(MethodDescriptor descriptor, ResultHandle object, ResultHandle... args) {
Objects.requireNonNull(descriptor);
Objects.requireNonNull(object);
ResultHandle ret = allocateResult(descriptor.getReturnType());
operations.add(new InvokeOperation(ret, descriptor, resolve(checkScope(object)), resolve(checkScope(args)), false, true));
return ret;
}
@Override
public ResultHandle invokeSpecialInterfaceMethod(MethodDescriptor descriptor, ResultHandle object, ResultHandle... args) {
Objects.requireNonNull(descriptor);
Objects.requireNonNull(object);
ResultHandle ret = allocateResult(descriptor.getReturnType());
operations.add(new InvokeOperation(ret, descriptor, resolve(checkScope(object)), resolve(checkScope(args)), true, true));
return ret;
}
@Override
public ResultHandle newInstance(MethodDescriptor descriptor, ResultHandle... args) {
Objects.requireNonNull(descriptor);
ResultHandle ret = allocateResult("L" + descriptor.getDeclaringClass() + ";");
operations.add(new NewInstanceOperation(ret, descriptor, resolve(checkScope(args))));
return ret;
}
@Override
public ResultHandle newArray(String type, ResultHandle length) {
Objects.requireNonNull(length);
String resultType;
if (!type.startsWith("[")) {
//assume a single dimension array
resultType = "[" + DescriptorUtils.objectToDescriptor(type);
} else {
resultType = DescriptorUtils.objectToDescriptor(type);
}
if (resultType.startsWith("[[")) {
throw new RuntimeException("Multidimensional arrays not supported yet");
}
final ResultHandle resolvedLength = resolve(checkScope(length));
char typeChar = resultType.charAt(1);
if (typeChar != 'L') {
//primitive arrays
int opcode;
switch (typeChar) {
case 'Z':
opcode = Opcodes.T_BOOLEAN;
break;
case 'B':
opcode = Opcodes.T_BYTE;
break;
case 'C':
opcode = Opcodes.T_CHAR;
break;
case 'S':
opcode = Opcodes.T_SHORT;
break;
case 'I':
opcode = Opcodes.T_INT;
break;
case 'J':
opcode = Opcodes.T_LONG;
break;
case 'F':
opcode = Opcodes.T_FLOAT;
break;
case 'D':
opcode = Opcodes.T_DOUBLE;
break;
default:
throw new RuntimeException("Unknown type " + type);
}
ResultHandle ret = allocateResult(resultType);
operations.add(new Operation() {
@Override
public void writeBytecode(MethodVisitor methodVisitor) {
loadResultHandle(methodVisitor, resolvedLength, BytecodeCreatorImpl.this, "I");
methodVisitor.visitIntInsn(Opcodes.NEWARRAY, opcode);
storeResultHandle(methodVisitor, ret);
}
@Override
Set<ResultHandle> getInputResultHandles() {
return Collections.singleton(resolvedLength);
}
@Override
ResultHandle getTopResultHandle() {
return resolvedLength;
}
@Override
ResultHandle getOutgoingResultHandle() {
return ret;
}
});
return ret;
} else {
//object arrays
String arrayType = resultType.substring(2, resultType.length() - 1);
ResultHandle ret = allocateResult(resultType);
operations.add(new Operation() {
@Override
public void writeBytecode(MethodVisitor methodVisitor) {
loadResultHandle(methodVisitor, resolvedLength, BytecodeCreatorImpl.this, "I");
methodVisitor.visitTypeInsn(Opcodes.ANEWARRAY, arrayType);
storeResultHandle(methodVisitor, ret);
}
@Override
Set<ResultHandle> getInputResultHandles() {
return Collections.singleton(resolvedLength);
}
@Override
ResultHandle getTopResultHandle() {
return resolvedLength;
}
@Override
ResultHandle getOutgoingResultHandle() {
return ret;
}
});
return ret;
}
}
@Override
public ResultHandle load(String val) {
Objects.requireNonNull(val);
if (val.length() > 65535) {
//TODO: we could auto split this, but I don't think we really want strings this long
throw new IllegalArgumentException("Cannot load strings larger than " + 65535 + " bytes");
}
return new ResultHandle("Ljava/lang/String;", this, val);
}
@Override
public ResultHandle load(byte val) {
return new ResultHandle("B", this, val);
}
@Override
public ResultHandle load(short val) {
return new ResultHandle("S", this, val);
}
@Override
public ResultHandle load(char val) {
return new ResultHandle("C", this, val);
}
@Override
public ResultHandle load(int val) {
return new ResultHandle("I", this, val);
}
@Override
public ResultHandle load(long val) {
return new ResultHandle("J", this, val);
}
@Override
public ResultHandle load(float val) {
return new ResultHandle("F", this, val);
}
@Override
public ResultHandle load(double val) {
return new ResultHandle("D", this, val);
}
@Override
public ResultHandle load(boolean val) {
return new ResultHandle("Z", this, val);
}
@Override
public ResultHandle loadClass(String className) {
return loadClass(className, false);
}
@Override
public ResultHandle loadClassFromTCCL(String className) {
return loadClass(className, true);
}
private ResultHandle loadClass(final String className, final boolean useTccl) {
Objects.requireNonNull(className);
final Class<?> primitiveType = matchPossiblyPrimitive(className);
if (primitiveType == null) {
if (useTccl) {
if (cachedTccl == null) {
ResultHandle currentThread = invokeStaticMethod(THREAD_CURRENT_THREAD);
cachedTccl = invokeVirtualMethod(THREAD_GET_TCCL, currentThread);
}
return invokeStaticMethod(CL_FOR_NAME,
load(className), load(false), cachedTccl);
} else {
return new ResultHandle("Ljava/lang/Class;", this, Type.getObjectType(className.replace('.', '/')));
}
} else {
Class<?> pt = primitiveType;
ResultHandle ret = new ResultHandle("Ljava/lang/Class;", this);
operations.add(new Operation() {
@Override
void writeBytecode(MethodVisitor methodVisitor) {
methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, Type.getInternalName(pt), "TYPE", "Ljava/lang/Class;");
storeResultHandle(methodVisitor, ret);
}
@Override
Set<ResultHandle> getInputResultHandles() {
return Collections.emptySet();
}
@Override
ResultHandle getTopResultHandle() {
return null;
}
@Override
ResultHandle getOutgoingResultHandle() {
return ret;
}
});
return ret;
}
}
private Class<?> matchPossiblyPrimitive(final String className) {
switch (className) {
case "boolean" : return Boolean.class;
case "byte" : return Byte.class;
case "char" : return Character.class;
case "short" : return Short.class;
case "int" : return Integer.class;
case "long" : return Long.class;
case "float" : return Float.class;
case "double" : return Double.class;
case "void" : return Void.class;
default: return null;
}
}
@Override
public ResultHandle loadNull() {
return ResultHandle.NULL;
}
@Override
public void writeInstanceField(FieldDescriptor fieldDescriptor, ResultHandle instance, ResultHandle value) {
Objects.requireNonNull(instance);
Objects.requireNonNull(value);
final ResultHandle resolvedInstance = resolve(checkScope(instance));
final ResultHandle resolvedValue = resolve(checkScope(value));
operations.add(new Operation() {
@Override
void writeBytecode(MethodVisitor methodVisitor) {
loadResultHandle(methodVisitor, resolvedInstance, BytecodeCreatorImpl.this, "L" + fieldDescriptor.getDeclaringClass() + ";");
loadResultHandle(methodVisitor, resolvedValue, BytecodeCreatorImpl.this, fieldDescriptor.getType());
methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, fieldDescriptor.getDeclaringClass(), fieldDescriptor.getName(), fieldDescriptor.getType());
}
@Override
Set<ResultHandle> getInputResultHandles() {
return new LinkedHashSet<>(Arrays.asList(resolvedInstance, resolvedValue));
}
@Override
ResultHandle getTopResultHandle() {
return resolvedInstance;
}
@Override
ResultHandle getOutgoingResultHandle() {
return null;
}
});
}
@Override
public ResultHandle readInstanceField(FieldDescriptor fieldDescriptor, ResultHandle instance) {
Objects.requireNonNull(fieldDescriptor);
Objects.requireNonNull(instance);
ResultHandle resultHandle = allocateResult(fieldDescriptor.getType());
ResultHandle resolvedInstance = resolve(checkScope(instance));
operations.add(new Operation() {
@Override
void writeBytecode(MethodVisitor methodVisitor) {
loadResultHandle(methodVisitor, resolvedInstance, BytecodeCreatorImpl.this, "L" + fieldDescriptor.getDeclaringClass() + ";");
methodVisitor.visitFieldInsn(Opcodes.GETFIELD, fieldDescriptor.getDeclaringClass(), fieldDescriptor.getName(), fieldDescriptor.getType());
storeResultHandle(methodVisitor, resultHandle);
}
@Override
Set<ResultHandle> getInputResultHandles() {
return Collections.singleton(resolvedInstance);
}
@Override
ResultHandle getTopResultHandle() {
return resolvedInstance;
}
@Override
ResultHandle getOutgoingResultHandle() {
return resultHandle;
}
});
return resultHandle;
}
@Override
public void writeStaticField(FieldDescriptor fieldDescriptor, ResultHandle value) {
Objects.requireNonNull(fieldDescriptor);
Objects.requireNonNull(value);
ResultHandle resolvedValue = resolve(checkScope(value));
operations.add(new Operation() {
@Override
public void writeBytecode(MethodVisitor methodVisitor) {
loadResultHandle(methodVisitor, resolvedValue, BytecodeCreatorImpl.this, fieldDescriptor.getType());
methodVisitor.visitFieldInsn(Opcodes.PUTSTATIC, fieldDescriptor.getDeclaringClass(), fieldDescriptor.getName(), fieldDescriptor.getType());
}
@Override
Set<ResultHandle> getInputResultHandles() {
return Collections.singleton(resolvedValue);
}
@Override
ResultHandle getTopResultHandle() {
return resolvedValue;
}
@Override
ResultHandle getOutgoingResultHandle() {
return null;
}
});
}
@Override
public ResultHandle readStaticField(FieldDescriptor fieldDescriptor) {
Objects.requireNonNull(fieldDescriptor);
ResultHandle result = allocateResult(fieldDescriptor.getType());
operations.add(new Operation() {
@Override
public void writeBytecode(MethodVisitor methodVisitor) {
methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, fieldDescriptor.getDeclaringClass(), fieldDescriptor.getName(), fieldDescriptor.getType());
storeResultHandle(methodVisitor, result);
}
@Override
Set<ResultHandle> getInputResultHandles() {
return Collections.emptySet();
}
@Override
ResultHandle getTopResultHandle() {
return null;
}
@Override
ResultHandle getOutgoingResultHandle() {
return result;
}
});
return result;
}
@Override
public ResultHandle arrayLength(ResultHandle array) {
ResultHandle result = new ResultHandle("I", this);
ResultHandle resolvedArray = resolve(checkScope(array));
operations.add(new Operation() {
@Override
public void writeBytecode(MethodVisitor methodVisitor) {
loadResultHandle(methodVisitor, resolvedArray, BytecodeCreatorImpl.this, resolvedArray.getType());
methodVisitor.visitInsn(Opcodes.ARRAYLENGTH);
storeResultHandle(methodVisitor, result);
}
@Override
Set<ResultHandle> getInputResultHandles() {
return Collections.singleton(resolvedArray);
}
@Override
ResultHandle getTopResultHandle() {
return resolvedArray;
}
@Override
ResultHandle getOutgoingResultHandle() {
return result;
}
});
return result;
}
@Override
public ResultHandle readArrayValue(ResultHandle array, ResultHandle index) {
if (!array.getType().startsWith("[")) {
throw new IllegalArgumentException("Not array type: " + array.getType());
}
ResultHandle result = allocateResult(array.getType().substring(1));
ResultHandle resolvedArray = resolve(checkScope(array));
ResultHandle resolvedIndex = resolve(checkScope(index));
operations.add(new Operation() {
@Override
public void writeBytecode(MethodVisitor methodVisitor) {
loadResultHandle(methodVisitor, resolvedArray, BytecodeCreatorImpl.this, resolvedArray.getType());
loadResultHandle(methodVisitor, resolvedIndex, BytecodeCreatorImpl.this, "I");
switch (result.getType()) {
case "I":
methodVisitor.visitInsn(Opcodes.IALOAD);
break;
case "J":
methodVisitor.visitInsn(Opcodes.LALOAD);
break;
case "F":
methodVisitor.visitInsn(Opcodes.FALOAD);
break;
case "D":
methodVisitor.visitInsn(Opcodes.DALOAD);
break;
case "B":
case "Z":
methodVisitor.visitInsn(Opcodes.BALOAD);
break;
case "C":
methodVisitor.visitInsn(Opcodes.CALOAD);
break;
case "S":
methodVisitor.visitInsn(Opcodes.SALOAD);
break;
default:
methodVisitor.visitInsn(Opcodes.AALOAD);
}
storeResultHandle(methodVisitor, result);
}
@Override
Set<ResultHandle> getInputResultHandles() {
return new LinkedHashSet<>(Arrays.asList(resolvedArray, resolvedIndex));
}
@Override
ResultHandle getTopResultHandle() {
return resolvedArray;
}
@Override
ResultHandle getOutgoingResultHandle() {
return result;
}
});
return result;
}
@Override
public void writeArrayValue(ResultHandle array, ResultHandle index, ResultHandle value) {
ResultHandle resolvedArray = resolve(checkScope(array));
ResultHandle resolvedIndex = resolve(checkScope(index));
ResultHandle resolvedValue = resolve(checkScope(value));
operations.add(new Operation() {
@Override
public void writeBytecode(MethodVisitor methodVisitor) {
loadResultHandle(methodVisitor, resolvedArray, BytecodeCreatorImpl.this, resolvedArray.getType());
loadResultHandle(methodVisitor, resolvedIndex, BytecodeCreatorImpl.this, "I");
String arrayType = resolvedArray.getType().substring(1);
loadResultHandle(methodVisitor, resolvedValue, BytecodeCreatorImpl.this, arrayType);
if (arrayType.equals("Z") || arrayType.equals("B")) {
methodVisitor.visitInsn(Opcodes.BASTORE);
} else if (arrayType.equals("S")) {
methodVisitor.visitInsn(Opcodes.SASTORE);
} else if (arrayType.equals("I")) {
methodVisitor.visitInsn(Opcodes.IASTORE);
} else if (arrayType.equals("C")) {
methodVisitor.visitInsn(Opcodes.CASTORE);
} else if (arrayType.equals("J")) {
methodVisitor.visitInsn(Opcodes.LASTORE);
} else if (arrayType.equals("F")) {
methodVisitor.visitInsn(Opcodes.FASTORE);
} else if (arrayType.equals("D")) {
methodVisitor.visitInsn(Opcodes.DASTORE);
} else {
methodVisitor.visitInsn(Opcodes.AASTORE);
}
}
@Override
Set<ResultHandle> getInputResultHandles() {
return new LinkedHashSet<>(Arrays.asList(resolvedArray, resolvedIndex, resolvedValue));
}
@Override
ResultHandle getTopResultHandle() {
return resolvedArray;
}
@Override
ResultHandle getOutgoingResultHandle() {
return null;
}
});
}
@Override
public AssignableResultHandle createVariable(final String typeDescr) {
Objects.requireNonNull(typeDescr);
return new AssignableResultHandle(typeDescr, this);
}
@Override
public void assign(final AssignableResultHandle target, final ResultHandle value) {
Objects.requireNonNull(target);
Objects.requireNonNull(value);
ResultHandle resolvedTarget = resolve(checkScope(target));
ResultHandle resolvedValue = resolve(checkScope(value));
if (resolvedTarget instanceof AssignableResultHandle) {
operations.add(new AssignOperation(resolvedValue, resolvedTarget));
} else {
throw new IllegalArgumentException("Cannot assign to captured variables");
}
}
@Override
public ResultHandle checkCast(final ResultHandle resultHandle, final String castTarget) {
Objects.requireNonNull(resultHandle);
Objects.requireNonNull(castTarget);
final ResultHandle result;
String intName = castTarget.replace('.', '/');
if (intName.startsWith("[") || intName.endsWith(";")) {
result = allocateResult(intName);
} else {
result = allocateResult("L" + intName + ";");
}
// seems like a waste of local vars but it's the safest approach since result type can't be mutated
final ResultHandle resolvedResultHandle = resolve(checkScope(resultHandle));
assert result != null;
operations.add(new Operation() {
@Override
void writeBytecode(final MethodVisitor methodVisitor) {
loadResultHandle(methodVisitor, resolvedResultHandle, BytecodeCreatorImpl.this, result.getType());
storeResultHandle(methodVisitor, result);
}
@Override
Set<ResultHandle> getInputResultHandles() {
return Collections.singleton(resolvedResultHandle);
}
@Override
ResultHandle getTopResultHandle() {
return resolvedResultHandle;
}
@Override
ResultHandle getOutgoingResultHandle() {
return result;
}
});
return result;
}
@Override
public boolean isScopedWithin(final BytecodeCreator other) {
return other == this || owner != null && owner.isScopedWithin(other);
}
@Override
public void continueScope(final BytecodeCreator scope) {
if (!isScopedWithin(scope)) {
throw new IllegalArgumentException("Cannot continue non-enclosing scope");
}
operations.add(new JumpOperation(((BytecodeCreatorImpl) scope).top));
}
@Override
public void breakScope(final BytecodeCreator scope) {
if (!isScopedWithin(scope)) {
throw new IllegalArgumentException("Cannot break non-enclosing scope");
}
operations.add(new JumpOperation(((BytecodeCreatorImpl) scope).bottom));
}
@Override
public BytecodeCreator createScope() {
final BytecodeCreatorImpl enclosed = new BytecodeCreatorImpl(this);
operations.add(new BlockOperation(enclosed));
return enclosed;
}
static void storeResultHandle(MethodVisitor methodVisitor, ResultHandle handle) {
if (handle.getResultType() == ResultHandle.ResultType.UNUSED) {
if (handle.getType().equals("J") || handle.getType().equals("D")) {
methodVisitor.visitInsn(Opcodes.POP2);
} else {
methodVisitor.visitInsn(Opcodes.POP);
}
} else if (handle.getResultType() == ResultHandle.ResultType.LOCAL_VARIABLE) {
if (handle.getType().equals("S") || handle.getType().equals("Z") || handle.getType().equals("I") || handle.getType().equals("B") || handle.getType().equals("C")) {
methodVisitor.visitVarInsn(Opcodes.ISTORE, handle.getNo());
} else if (handle.getType().equals("J")) {
methodVisitor.visitVarInsn(Opcodes.LSTORE, handle.getNo());
} else if (handle.getType().equals("F")) {
methodVisitor.visitVarInsn(Opcodes.FSTORE, handle.getNo());
} else if (handle.getType().equals("D")) {
methodVisitor.visitVarInsn(Opcodes.DSTORE, handle.getNo());
} else {
methodVisitor.visitVarInsn(Opcodes.ASTORE, handle.getNo());
}
}
}
void loadResultHandle(MethodVisitor methodVisitor, ResultHandle handle, BytecodeCreatorImpl bc, String expectedType) {
loadResultHandle(methodVisitor, handle, bc, expectedType, false);
}
void loadResultHandle(MethodVisitor methodVisitor, ResultHandle handle, BytecodeCreatorImpl bc, String expectedType, boolean dontCast) {
if (handle.getResultType() == ResultHandle.ResultType.CONSTANT) {
if (handle.getConstant() == null) {
methodVisitor.visitInsn(Opcodes.ACONST_NULL);
} else {
emitLoadConstant(methodVisitor, handle.getConstant());
if (!dontCast && !expectedType.equals(handle.getType())) {
if (expectedType.length() > 1 && handle.getType().length() > 1) {
// both objects, we just do a checkcast
if (!expectedType.equals("Ljava/lang/Object;")) {
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, DescriptorUtils.getTypeStringFromDescriptorFormat(expectedType));
}
} else if (expectedType.length() == 1 && handle.getType().length() == 1) {
// both primitives, ignore
} else if (expectedType.length() == 1) {
// expected primitive, must auto-unbox
String type = boxingMap.get(expectedType);
if (type == null) {
throw new RuntimeException("Unknown primitive type " + expectedType);
}
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, type);
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, type, boxingMethodMap.get(expectedType), "()" + expectedType, false);
} else {
// expected primitive wrapper, must auto-box
String type = boxingMap.get(handle.getType());
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, type, "valueOf", "(" + handle.getType() + ")L" + type + ";", false);
}
}
}
return;
}
if (!isScopedWithin(handle.getOwner())) {
// throw new IllegalStateException("Wrong owner for ResultHandle " + handle);
}
if (handle.getResultType() != ResultHandle.ResultType.SINGLE_USE) {
if (handle.getType().equals("S") || handle.getType().equals("Z") || handle.getType().equals("I") || handle.getType().equals("B") || handle.getType().equals("C")) {
methodVisitor.visitVarInsn(Opcodes.ILOAD, handle.getNo());
} else if (handle.getType().equals("J")) {
methodVisitor.visitVarInsn(Opcodes.LLOAD, handle.getNo());
} else if (handle.getType().equals("F")) {
methodVisitor.visitVarInsn(Opcodes.FLOAD, handle.getNo());
} else if (handle.getType().equals("D")) {
methodVisitor.visitVarInsn(Opcodes.DLOAD, handle.getNo());
} else {
methodVisitor.visitVarInsn(Opcodes.ALOAD, handle.getNo());
}
}
if (!dontCast && !expectedType.equals(handle.getType())) {
if (expectedType.length() > 1 && handle.getType().length() > 1) {
// both objects, we just do a checkcast
if (!expectedType.equals("Ljava/lang/Object;")) {
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, DescriptorUtils.getTypeStringFromDescriptorFormat(expectedType));
}
} else if (expectedType.length() == 1 && handle.getType().length() == 1) {
// both primitives, ignore
} else if (expectedType.length() == 1) {
// expected primitive, must auto-unbox
String type = boxingMap.get(expectedType);
if (type == null) {
throw new RuntimeException("Unknown primitive type " + expectedType);
}
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, type);
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, type, boxingMethodMap.get(expectedType), "()" + expectedType, false);
} else {
// expected primitive wrapper, must auto-box
String type = boxingMap.get(handle.getType());
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, type, "valueOf", "(" + handle.getType() + ")L" + type + ";", false);
}
}
}
private static void emitLoadConstant(MethodVisitor methodVisitor, Object constant) {
if (constant instanceof Byte || constant instanceof Short) {
constant = ((Number) constant).intValue();
}
if (Boolean.FALSE.equals(constant)) {
methodVisitor.visitInsn(Opcodes.ICONST_0);
} else if (Boolean.TRUE.equals(constant)) {
methodVisitor.visitInsn(Opcodes.ICONST_1);
} else if (Integer.valueOf(-1).equals(constant)) {
methodVisitor.visitInsn(Opcodes.ICONST_M1);
} else if (Integer.valueOf(0).equals(constant)) {
methodVisitor.visitInsn(Opcodes.ICONST_0);
} else if (Integer.valueOf(1).equals(constant)) {
methodVisitor.visitInsn(Opcodes.ICONST_1);
} else if (Integer.valueOf(2).equals(constant)) {
methodVisitor.visitInsn(Opcodes.ICONST_2);
} else if (Integer.valueOf(3).equals(constant)) {
methodVisitor.visitInsn(Opcodes.ICONST_3);
} else if (Integer.valueOf(4).equals(constant)) {
methodVisitor.visitInsn(Opcodes.ICONST_4);
} else if (Integer.valueOf(5).equals(constant)) {
methodVisitor.visitInsn(Opcodes.ICONST_5);
} else if (Long.valueOf(0L).equals(constant)) {
methodVisitor.visitInsn(Opcodes.LCONST_0);
} else if (Long.valueOf(1L).equals(constant)) {
methodVisitor.visitInsn(Opcodes.LCONST_1);
} else if (Float.valueOf(0.0F).equals(constant)) {
methodVisitor.visitInsn(Opcodes.FCONST_0);
} else if (Float.valueOf(1.0F).equals(constant)) {
methodVisitor.visitInsn(Opcodes.FCONST_1);
} else if (Double.valueOf(0.0).equals(constant)) {
methodVisitor.visitInsn(Opcodes.DCONST_0);
} else if (Double.valueOf(1.0).equals(constant)) {
methodVisitor.visitInsn(Opcodes.DCONST_1);
} else {
// if original constant was `byte` or `short`, it is `int` now,
// but that makes no difference
methodVisitor.visitLdcInsn(constant);
}
}
@Override
public TryBlock tryBlock() {
final TryBlockImpl tryBlock = new TryBlockImpl(this);
operations.add(new BlockOperation(tryBlock));
return tryBlock;
}
@Override
public ResultHandle compareLong(ResultHandle value1, ResultHandle value2) {
return emitCompare(Opcodes.LCMP, "J", value1, value2);
}
@Override
public ResultHandle compareFloat(ResultHandle value1, ResultHandle value2, boolean nanComparesAsGreater) {
return emitCompare(nanComparesAsGreater ? Opcodes.FCMPG : Opcodes.FCMPL, "F", value1, value2);
}
@Override
public ResultHandle compareDouble(ResultHandle value1, ResultHandle value2, boolean nanComparesAsGreater) {
return emitCompare(nanComparesAsGreater ? Opcodes.DCMPG : Opcodes.DCMPL, "D", value1, value2);
}
private ResultHandle emitCompare(int opcode, String expectedType, ResultHandle value1, ResultHandle value2) {
Objects.requireNonNull(value1);
Objects.requireNonNull(value2);
if (!expectedType.equals(value1.getType()) || !expectedType.equals(value2.getType())) {
throw new IllegalArgumentException("Arguments must both be of type " + expectedType);
}
final ResultHandle resolved1 = resolve(checkScope(value1));
final ResultHandle resolved2 = resolve(checkScope(value2));
final ResultHandle result = allocateResult("I");
assert result != null;
operations.add(new Operation() {
@Override
void writeBytecode(final MethodVisitor methodVisitor) {
loadResultHandle(methodVisitor, resolved1, BytecodeCreatorImpl.this, resolved1.getType(), true);
loadResultHandle(methodVisitor, resolved2, BytecodeCreatorImpl.this, resolved2.getType(), true);
methodVisitor.visitInsn(opcode);
storeResultHandle(methodVisitor, result);
}
@Override
Set<ResultHandle> getInputResultHandles() {
return new LinkedHashSet<>(Arrays.asList(resolved1, resolved2));
}
@Override
ResultHandle getTopResultHandle() {
return resolved1;
}
@Override
ResultHandle getOutgoingResultHandle() {
return result;
}
});
return result;
}
@Override
public BranchResult ifNonZero(ResultHandle resultHandle) {
return ifValue(resultHandle, Opcodes.IFNE, "I");
}
@Override
public BranchResult ifNull(ResultHandle resultHandle) {
return ifValue(resultHandle, Opcodes.IFNULL, "Ljava/lang/Object;");
}
@Override
public BranchResult ifZero(ResultHandle resultHandle) {
return ifValue(resultHandle, Opcodes.IFEQ, "I");
}
@Override
public BranchResult ifTrue(ResultHandle resultHandle) {
return ifNonZero(resultHandle);
}
@Override
public BranchResult ifFalse(ResultHandle resultHandle) {
return ifZero(resultHandle);
}
@Override
public BranchResult ifNotNull(ResultHandle resultHandle) {
return ifValue(resultHandle, Opcodes.IFNONNULL, "Ljava/lang/Object;");
}
@Override
public BranchResult ifGreaterThanZero(ResultHandle resultHandle) {
return ifValue(resultHandle, Opcodes.IFGT, "I");
}
@Override
public BranchResult ifGreaterEqualZero(ResultHandle resultHandle) {
return ifValue(resultHandle, Opcodes.IFGE, "I");
}
@Override
public BranchResult ifLessThanZero(ResultHandle resultHandle) {
return ifValue(resultHandle, Opcodes.IFLT, "I");
}