-
Notifications
You must be signed in to change notification settings - Fork 61
/
VMAdviceTemplateGenerator.java
1561 lines (1387 loc) · 58 KB
/
VMAdviceTemplateGenerator.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 (c) 2014, Andrey Rodchenko. All rights reserved.
* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code 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 Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.max.vma.tools.gen.t1x;
import static com.oracle.max.vm.ext.t1x.T1XTemplateTag.*;
import java.io.*;
import java.util.*;
import com.oracle.max.vm.ext.t1x.*;
import com.oracle.max.vm.ext.vma.*;
import com.oracle.max.vm.ext.vma.run.java.*;
import com.oracle.max.vm.ext.vma.t1x.VMAdviceAfterTemplateSource;
import com.oracle.max.vm.ext.vma.t1x.VMAdviceBeforeAfterTemplateSource;
import com.oracle.max.vm.ext.vma.t1x.VMAdviceBeforeTemplateSource;
import com.oracle.max.vma.tools.gen.vma.*;
import com.sun.max.annotate.*;
import com.sun.max.program.*;
import com.sun.max.vm.actor.member.*;
import com.sun.max.vm.type.*;
/**
* Template generation that supports the {@link VMAdviceHandler} interface.
* Note that not all templates support both before/after advice but every one supports
* at least one of them. Accordingly, this program can generate several output variants
* so as to provide runtime flexibility and avoid unnecessary duplication:
* <ul>
* <li>All templates generated with whatever before/advice is available. This essentially parallels what is
* produced by {@link T1XTemplateGenerator} in normal mode.
* <li>Only the templates that provide before advice, with only before advice generated.
* <li>Only the templates that provide after advice, with only after advice generated.
* <li>Only the templates that provide before and after advice, with before and advice generated.
* </ul>
*
* Unfortunately, we cannot reuse the standard templates for INVOKE as we need consistent access to the
* {@link MethodActor} to pass to the advice handler. So we override the standard definitions in this class.
* {@link T1X} doesn't use templates for many of the simple bytecodes, or some of the INVOKE bytecodes,
* any more, which complicates the implementation as we have to generate templates for those directly.
*
* We also override some enabling methods to achieve a similar effect for GET/PUTFIELD, GET/PUTSTATIC,
* where the changes are much smaller.
*
* Two different mechanisms can be generated for testing whether the advice methods should be invoked.
* All ultimately test bit zero of the {@klink VMAJavaRunScheme#VM_ADVISING} thread local.
* The first mechanism is to include the body of {@link VMAJavaRunScheme#isVMAdvising} which is an
* {@code INLINE} method that tests the bit using standard Java. The reason we don't just rely on the
* inlining mechanism is that the generated code is less efficient than the manually inlined version
* owing to a C1X limitation.
*
* The second, and default, mechanism uses an {@code INTRINSIC} method that tests the bit explicitly
* and branches directly on its value. This is preferable because it avoids the use of a register to
* load the value for a standard comparison .
*/
@HOSTED_ONLY
public class VMAdviceTemplateGenerator extends T1XTemplateGenerator {
/**
* This records the advice capability for each {@link T1XTemplateTag}.
*/
private static boolean[][] tagAdviceCapabilities = new boolean[T1XTemplateTag.values().length][AdviceType.values().length];
/**
* This hook is used in an initial pass to discover which templates have before/after advice,
* which it records in {@link VMAdviceTemplateGenerator#tagAdviceCapabilities}.
*/
private static class DiscoverCapabilitiesHook implements AdviceHook {
@Override
public void startMethodGeneration() {
}
@Override
public void generate(T1XTemplateTag tag, AdviceType at, Object... args) {
tagAdviceCapabilities[tag.ordinal()][at.ordinal()] = true;
}
@Override
public String suffixParams(boolean comma) {
return "";
}
@Override
public String suffixArgs(boolean comma) {
return "";
}
@Override
public void endTemplateMethodGeneration(T1XTemplateTag tag) {
}
}
private static class MethodStatus {
int offset; // in the ByteArrayOutputStream array
boolean output; // are we outputting this method
T1XTemplateTag tag; // that this method is associated with
EnumSet<AdviceType> atSet = EnumSet.noneOf(AdviceType.class);
MethodStatus(int offset) {
this.offset = offset;
this.output = true;
}
@Override
public String toString() {
return tag + ": " + atSet + ": " + offset + ": " + output;
}
}
private static class ThisByteArrayOutputStream extends ByteArrayOutputStream {
ArrayList<MethodStatus> methodStatusList;
void writeOut() {
System.out.print(toString());
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
int listLength = methodStatusList.size();
for (int i = 0; i < listLength; i++) {
MethodStatus methodStatus = methodStatusList.get(i);
if (methodStatus.output) {
sb.append(new String(buf, methodStatus.offset, (i == listLength - 1 ? size() : methodStatusList.get(i + 1).offset) - methodStatus.offset));
}
}
return sb.toString();
}
void addMethodStatus() {
methodStatusList.add(new MethodStatus(size()));
}
/**
* Discard this and any intervening untagged (helper) methods if we only added advice
* of the type that we are not generating in this run.
*/
void discard(T1XTemplateTag tag) {
final int listLength = methodStatusList.size();
MethodStatus ms = methodStatusList.get(listLength - 1);
// ok to destroy ms.asSet
ms.atSet.retainAll(generating);
// throw it away if we generated advice type that was not wanted.
if (ms.atSet.isEmpty()) {
ms.output = false;
// Checkstyle: resume Indentation check
int index = listLength - 2;
while (index >= 0) {
ms = methodStatusList.get(index);
if (ms.tag == null) {
ms.output = false;
} else if (ms.tag != tag) {
return;
}
index--;
}
}
}
void setTag(T1XTemplateTag tag) {
MethodStatus ms = methodStatusList.get(methodStatusList.size() - 1);
ms.tag = tag;
}
void addAdviceType(AdviceType at) {
MethodStatus ms = methodStatusList.get(methodStatusList.size() - 1);
ms.atSet.add(at);
}
void removeAdviceType(AdviceType at) {
MethodStatus ms = methodStatusList.get(methodStatusList.size() - 1);
ms.atSet.remove(at);
}
@Override
public void reset() {
super.reset();
methodStatusList = new ArrayList<MethodStatus>();
}
}
private static final String INDENT8_PREFIX = " ";
private static final String INDENT12_PREFIX = " ";
private static final String ADVISE_PREFIX_NOARG = "VMAStaticBytecodeAdvice.advise%s%s(bci";
private static final String ADVISE_PREFIX = ADVISE_PREFIX_NOARG + ", ";
private static final String INDENT12_ADVISE_PREFIX = INDENT12_PREFIX + ADVISE_PREFIX;
private static final String INDENT12_ADVISE_NOARG_PREFIX = INDENT12_PREFIX + ADVISE_PREFIX_NOARG;
private static AdviceType adviceType;
private static String methodName;
private static ThisByteArrayOutputStream byteArrayOut;
private static final String ISADVISING = "VMAJavaRunScheme.isAdvising()";
/**
* We explicitly inline the above as the automatic inlining currently generates sub-optimal code.
*/
private static final String INLINE_ISADVISING = "VmThread.currentTLA().getWord(VMAJavaRunScheme.VM_ADVISING.index) != Word.zero()";
/**
* This is the version that gets optimal code via the BT instruction.
*/
private static final String ALT_ISADVISING = "Intrinsics.readLatchBit(VMAJavaRunScheme.VM_ADVISING.offset, 0)";
/**
* If {@code true}, generate bt instruction variant of the advice guard.
*/
private static boolean bitGuard = true;
/**
* Records whether we want to generate a particular advice type.
*/
private static EnumSet<AdviceType> generating;
private static boolean hasBeforeAndAfter(T1XTemplateTag tag) {
for (AdviceType at : AdviceType.values()) {
if (!tagAdviceCapabilities[tag.ordinal()][at.ordinal()]) {
return false;
}
}
return true;
}
private class VMAT1XAdvice implements AdviceHook {
private static final String BCI_NOARG = "bci";
private static final String BCI = ", " + BCI_NOARG;
private static final String INT_BCI_NOARG = "int " + BCI_NOARG;
private static final String INT_BCI = ", " + INT_BCI_NOARG;
/**
* Start of a template method generation.
* Don't know what tag yet, but that's ok, we just need to record
* where we are in the output stream in case we want to discard it later.
*/
@Override
public void startMethodGeneration() {
byteArrayOut.addMethodStatus();
}
@Override
public void endTemplateMethodGeneration(T1XTemplateTag tag) {
// N.B. we discard the entire method if it only contains advice of the kind we are not generating.
byteArrayOut.discard(tag);
}
@Override
public String suffixParams(boolean comma) {
return comma ? INT_BCI : INT_BCI_NOARG;
}
@Override
public String suffixArgs(boolean comma) {
return comma ? BCI : BCI_NOARG;
}
private boolean generateTag(T1XTemplateTag tag, AdviceType at) {
// We only generate if the tag (a) has the capability for the requested advice type
// and (b) the program is being run to generate that kind of advice.
return tagAdviceCapabilities[tag.ordinal()][at.ordinal()] && generating.contains(at);
}
/**
* Called to generate the specific type of advice for {@code tag}.
* Code will already have been output since the call to {@link #startMethodGeneration()},
* and we may decide here to throw it away if we are not generating that advice.
*/
@Override
@SuppressWarnings("fallthrough")
public void generate(T1XTemplateTag tag, AdviceType at, Object... args) {
byteArrayOut.setTag(tag);
if (!generateTag(tag, at)) {
return;
}
byteArrayOut.addAdviceType(at); // Record that we did generate "at" advice
adviceType = at;
methodName = tag.opcode >= 0 ? AdviceGeneratorHelper.codeMap.get(tag.opcode).methodName : "???";
String k = null;
if (args.length > 0) {
if (args[0] instanceof Kind) {
k = j((Kind) args[0]);
} else {
assert args[0] instanceof String;
k = (String) args[0];
}
}
switch (tag) {
case GETFIELD$byte:
case GETFIELD$boolean:
case GETFIELD$char:
case GETFIELD$short:
case GETFIELD$int:
case GETFIELD$float:
case GETFIELD$long:
case GETFIELD$double:
case GETFIELD$reference:
case GETFIELD$word:
assert adviceType == AdviceType.BEFORE;
generateGetField(k, false);
break;
case GETFIELD$byte$resolved:
case GETFIELD$boolean$resolved:
case GETFIELD$char$resolved:
case GETFIELD$short$resolved:
case GETFIELD$int$resolved:
case GETFIELD$float$resolved:
case GETFIELD$long$resolved:
case GETFIELD$double$resolved:
case GETFIELD$reference$resolved:
case GETFIELD$word$resolved:
assert adviceType == AdviceType.BEFORE;
generateGetField(k, true);
break;
case PUTFIELD$byte:
case PUTFIELD$boolean:
case PUTFIELD$char:
case PUTFIELD$short:
case PUTFIELD$int:
case PUTFIELD$float:
case PUTFIELD$long:
case PUTFIELD$double:
case PUTFIELD$reference:
case PUTFIELD$word:
assert adviceType == AdviceType.BEFORE;
generatePutField(k, false);
break;
case PUTFIELD$byte$resolved:
case PUTFIELD$boolean$resolved:
case PUTFIELD$char$resolved:
case PUTFIELD$short$resolved:
case PUTFIELD$int$resolved:
case PUTFIELD$float$resolved:
case PUTFIELD$long$resolved:
case PUTFIELD$double$resolved:
case PUTFIELD$reference$resolved:
case PUTFIELD$word$resolved:
assert adviceType == AdviceType.BEFORE;
generatePutField(k, true);
break;
case GETSTATIC$byte:
case GETSTATIC$boolean:
case GETSTATIC$char:
case GETSTATIC$short:
case GETSTATIC$int:
case GETSTATIC$float:
case GETSTATIC$long:
case GETSTATIC$double:
case GETSTATIC$reference:
case GETSTATIC$word:
assert adviceType == AdviceType.BEFORE;
generateGetStatic(k, false);
break;
case GETSTATIC$byte$init:
case GETSTATIC$boolean$init:
case GETSTATIC$char$init:
case GETSTATIC$short$init:
case GETSTATIC$int$init:
case GETSTATIC$float$init:
case GETSTATIC$long$init:
case GETSTATIC$double$init:
case GETSTATIC$reference$init:
case GETSTATIC$word$init:
assert adviceType == AdviceType.BEFORE;
generateGetStatic(k, true);
break;
case PUTSTATIC$byte:
case PUTSTATIC$boolean:
case PUTSTATIC$char:
case PUTSTATIC$short:
case PUTSTATIC$int:
case PUTSTATIC$float:
case PUTSTATIC$long:
case PUTSTATIC$double:
case PUTSTATIC$reference:
case PUTSTATIC$word:
assert adviceType == AdviceType.BEFORE;
generatePutStatic(k, false);
break;
case PUTSTATIC$byte$init:
case PUTSTATIC$boolean$init:
case PUTSTATIC$char$init:
case PUTSTATIC$short$init:
case PUTSTATIC$int$init:
case PUTSTATIC$float$init:
case PUTSTATIC$long$init:
case PUTSTATIC$double$init:
case PUTSTATIC$reference$init:
case PUTSTATIC$word$init:
assert adviceType == AdviceType.BEFORE;
generatePutStatic(k, true);
break;
case IALOAD:
case LALOAD:
case FALOAD:
case DALOAD:
case AALOAD:
case BALOAD:
case CALOAD:
case SALOAD:
if (adviceType == AdviceType.BEFORE || k.equals("Object")) {
generateArrayLoad(k);
} else {
// Undo add which will cause method to be discarded
byteArrayOut.removeAdviceType(at);
}
break;
case IASTORE:
case LASTORE:
case FASTORE:
case DASTORE:
case AASTORE:
case BASTORE:
case CASTORE:
case SASTORE:
assert adviceType == AdviceType.BEFORE;
generateArrayStore(k);
break;
case ACONST_NULL:
assert adviceType == AdviceType.BEFORE;
generateLoadConst(tag, k, null);
break;
case ICONST:
case FCONST:
case DCONST:
case LCONST:
assert adviceType == AdviceType.BEFORE;
generateLoadConst(tag, k, null);
break;
case ILOAD:
case LLOAD:
case FLOAD:
case DLOAD:
case ALOAD:
assert adviceType == AdviceType.BEFORE || tag == ALOAD;
generateLoad(k);
break;
case ISTORE:
case LSTORE:
case FSTORE:
case DSTORE:
case ASTORE:
assert adviceType == AdviceType.BEFORE;
generateStore(k);
break;
case BIPUSH:
case SIPUSH:
assert adviceType == AdviceType.BEFORE;
generateIPush(k);
break;
case IADD:
case LADD:
case FADD:
case DADD:
case ISUB:
case LSUB:
case FSUB:
case DSUB:
case IMUL:
case LMUL:
case FMUL:
case DMUL:
case IDIV:
case LDIV:
case FDIV:
case DDIV:
case IREM:
case LREM:
case FREM:
case DREM:
case INEG:
case LNEG:
case FNEG:
case DNEG:
case ISHL:
case LSHL:
case ISHR:
case LSHR:
case IUSHR:
case LUSHR:
case IAND:
case LAND:
case IOR:
case LOR:
case IXOR:
case LXOR:
case LCMP:
case FCMPL:
case FCMPG:
case DCMPL:
case DCMPG:
assert adviceType == AdviceType.BEFORE;
generateOperation(tag, k);
break;
case IINC:
assert adviceType == AdviceType.BEFORE;
generateIInc();
break;
case I2L:
case I2F:
case I2D:
case L2I:
case L2F:
case L2D:
case F2I:
case F2L:
case F2D:
case D2I:
case D2L:
case D2F:
case I2B:
case I2C:
case I2S:
assert adviceType == AdviceType.BEFORE;
generateConversion(tag, k);
break;
case IFEQ:
case IFNE:
case IFLT:
case IFGE:
case IFGT:
case IFLE:
case IFNULL:
case IFNONNULL:
case IF_ICMPEQ:
case IF_ICMPNE:
case IF_ICMPLT:
case IF_ICMPGE:
case IF_ICMPGT:
case IF_ICMPLE:
case IF_ACMPEQ:
case IF_ACMPNE:
assert adviceType == AdviceType.BEFORE;
generateIf(tag);
break;
case GOTO:
case GOTO_W:
assert adviceType == AdviceType.BEFORE;
generateGoto(tag);
break;
case CHECKCAST:
case CHECKCAST$resolved:
case CHECKCAST$instrumented:
case INSTANCEOF:
case INSTANCEOF$resolved:
case INSTANCEOF$instrumented:
assert adviceType == AdviceType.BEFORE;
generateTypeCheck(tag);
break;
case NEW_HYBRID:
methodName = "New";
// fall through
case NEW:
case NEW$init:
assert adviceType == AdviceType.AFTER;
generateNew();
break;
case NEWARRAY:
case ANEWARRAY:
case ANEWARRAY$resolved:
assert adviceType == AdviceType.AFTER;
generateNewUniArray();
break;
case MULTIANEWARRAY:
case MULTIANEWARRAY$resolved:
assert adviceType == AdviceType.AFTER;
generateNewMultiArray();
break;
case INVOKEVIRTUAL$void:
case INVOKEVIRTUAL$float:
case INVOKEVIRTUAL$long:
case INVOKEVIRTUAL$double:
case INVOKEVIRTUAL$word:
case INVOKEVIRTUAL$reference:
case INVOKEVIRTUAL$void$resolved:
case INVOKEVIRTUAL$float$resolved:
case INVOKEVIRTUAL$long$resolved:
case INVOKEVIRTUAL$double$resolved:
case INVOKEVIRTUAL$word$resolved:
case INVOKEVIRTUAL$reference$resolved:
case INVOKEVIRTUAL$void$instrumented:
case INVOKEVIRTUAL$float$instrumented:
case INVOKEVIRTUAL$long$instrumented:
case INVOKEVIRTUAL$double$instrumented:
case INVOKEVIRTUAL$word$instrumented:
case INVOKEVIRTUAL$reference$instrumented:
if (adviceType == AdviceType.BEFORE) {
generateInvokeVirtual();
}
break;
case INVOKESPECIAL$void:
case INVOKESPECIAL$float:
case INVOKESPECIAL$long:
case INVOKESPECIAL$double:
case INVOKESPECIAL$word:
case INVOKESPECIAL$reference:
case INVOKESPECIAL$void$resolved:
case INVOKESPECIAL$float$resolved:
case INVOKESPECIAL$long$resolved:
case INVOKESPECIAL$double$resolved:
case INVOKESPECIAL$word$resolved:
case INVOKESPECIAL$reference$resolved:
if (adviceType == AdviceType.BEFORE) {
generateInvokeSpecial();
}
break;
case INVOKESTATIC$void:
case INVOKESTATIC$float:
case INVOKESTATIC$long:
case INVOKESTATIC$double:
case INVOKESTATIC$word:
case INVOKESTATIC$reference:
case INVOKESTATIC$void$init:
case INVOKESTATIC$float$init:
case INVOKESTATIC$long$init:
case INVOKESTATIC$double$init:
case INVOKESTATIC$word$init:
case INVOKESTATIC$reference$init:
if (adviceType == AdviceType.BEFORE) {
generateInvokeStatic();
}
break;
case INVOKEINTERFACE$void:
case INVOKEINTERFACE$float:
case INVOKEINTERFACE$long:
case INVOKEINTERFACE$double:
case INVOKEINTERFACE$word:
case INVOKEINTERFACE$reference:
case INVOKEINTERFACE$void$resolved:
case INVOKEINTERFACE$float$resolved:
case INVOKEINTERFACE$long$resolved:
case INVOKEINTERFACE$double$resolved:
case INVOKEINTERFACE$word$resolved:
case INVOKEINTERFACE$reference$resolved:
case INVOKEINTERFACE$void$instrumented:
case INVOKEINTERFACE$float$instrumented:
case INVOKEINTERFACE$long$instrumented:
case INVOKEINTERFACE$double$instrumented:
case INVOKEINTERFACE$word$instrumented:
case INVOKEINTERFACE$reference$instrumented:
if (adviceType == AdviceType.BEFORE) {
generateInvokeInterface();
}
break;
case ATHROW:
generateThrow();
break;
case RETHROW_EXCEPTION:
generateReThrow();
break;
case ARRAYLENGTH:
generateArrayLength();
break;
case MONITORENTER:
case MONITOREXIT:
generateMonitor(tag);
break;
case POP:
case POP2:
case DUP:
case DUP_X1:
case DUP_X2:
case DUP2:
case DUP2_X1:
case DUP2_X2:
case SWAP:
generateDefault(tag);
break;
case LDC$int:
case LDC$long:
case LDC$float:
case LDC$double:
case LDC$reference:
case LDC$reference$resolved:
generateLDC(tag, k);
break;
case IRETURN:
case LRETURN:
case FRETURN:
case DRETURN:
case ARETURN:
case RETURN:
case IRETURN$unlock:
case LRETURN$unlock:
case FRETURN$unlock:
case DRETURN$unlock:
case ARETURN$unlock:
case RETURN$unlock:
case RETURN$registerFinalizer:
generateReturn(tag, k);
break;
case LOCK:
methodName = "MonitorEnter";
generateMonitor(tag);
break;
case UNLOCK:
methodName = "MonitorExit";
generateMonitor(tag);
break;
// No special treatment for the following codes
case LOAD_EXCEPTION:
break;
case TRACE_METHOD_ENTRY:
generateTraceMethodEntry();
break;
default:
ProgramError.unexpected("tag " + tag + " not implemented");
}
}
}
private void startGuardAdvice() {
out.printf(" if (%s) {%n", bitGuard ? ALT_ISADVISING : INLINE_ISADVISING);
}
private void endGuardAdvice() {
out.printf(" }%n");
}
private void generatePutField(String k, boolean resolved) {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "object, f, %s);%n", adviceType.methodNameComponent, methodName, putValue(k, ""));
endGuardAdvice();
}
private void generateGetField(String k, boolean resolved) {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "object, f);%n", adviceType.methodNameComponent, methodName);
endGuardAdvice();
}
private void generatePutStatic(String k, boolean init) {
String args = init ? "staticTuple, f" : "f.holder().staticTuple(), f";
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "%s, %s);%n", adviceType.methodNameComponent, methodName, args, putValue(k, ""));
endGuardAdvice();
}
private void generateGetStatic(String k, boolean init) {
String args = init ? "staticTuple, f" : "f.holder().staticTuple(), f";
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "%s);%n", adviceType.methodNameComponent, methodName, args);
endGuardAdvice();
}
private void generateArrayLoad(String k) {
startGuardAdvice();
if (adviceType == AdviceType.BEFORE) {
out.printf(INDENT12_ADVISE_PREFIX + "array, index);%n", adviceType.methodNameComponent, methodName);
} else {
out.printf(INDENT12_ADVISE_PREFIX + "array, index, result);%n", adviceType.methodNameComponent, methodName);
}
endGuardAdvice();
}
private void generateArrayStore(String k) {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "array, index, %s);%n", adviceType.methodNameComponent, methodName, putValue(k, ""));
endGuardAdvice();
}
private void generateLoadConst(T1XTemplateTag tag, String k, String v) {
String value = k.equals("Reference") ? v : (k.equals("Word") ? "0" : "constant");
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "%s);%n", adviceType.methodNameComponent, methodName, value);
endGuardAdvice();
}
private void generateLDC(T1XTemplateTag tag, String k) {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "constant);%n", adviceType.methodNameComponent, methodName);
endGuardAdvice();
}
private void generateLoad(String k) {
startGuardAdvice();
String value = adviceType == AdviceType.AFTER ? (", " + putValue(k)) : "";
out.printf(INDENT12_ADVISE_PREFIX + "index%s);%n", adviceType.methodNameComponent, methodName, value);
endGuardAdvice();
}
private void generateStore(String k) {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "index, %s);%n", adviceType.methodNameComponent, methodName, putValue(k));
endGuardAdvice();
}
private void generateIPush(String k) {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "value);%n", adviceType.methodNameComponent, methodName);
endGuardAdvice();
}
private boolean isNeg(T1XTemplateTag tag) {
return tag == INEG || tag == FNEG || tag == LNEG || tag == DNEG;
}
private void generateOperation(T1XTemplateTag tag, String k) {
String value1 = isNeg(tag) ? "value" : "value1";
String value2 = isNeg(tag) ? "zero" : "value2";
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "%d, %s, %s);%n", adviceType.methodNameComponent, methodName, tag.opcode, value1, value2);
endGuardAdvice();
}
private void generateConversion(T1XTemplateTag tag, String k) {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "%d, value);%n", adviceType.methodNameComponent, methodName, tag.opcode);
endGuardAdvice();
}
private void generateNew() {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "object);%n", adviceType.methodNameComponent, methodName);
endGuardAdvice();
}
private void generateNewUniArray() {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "array, length);%n", adviceType.methodNameComponent, methodName);
endGuardAdvice();
}
private void generateNewMultiArray() {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "array, lengths);%n", adviceType.methodNameComponent, methodName);
endGuardAdvice();
}
private void generateInvokeVirtual() {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "receiver, methodActor);%n", adviceType.methodNameComponent, methodName);
endGuardAdvice();
}
private void generateInvokeInterface() {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "receiver, methodActor);%n", adviceType.methodNameComponent, methodName);
endGuardAdvice();
}
private void generateInvokeSpecial() {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "receiver, methodActor);%n", adviceType.methodNameComponent, methodName);
endGuardAdvice();
}
private void generateInvokeStatic() {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "null, methodActor);%n", adviceType.methodNameComponent, methodName);
endGuardAdvice();
}
private void generateTraceMethodEntry() {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "receiver, methodActor);%n", adviceType.methodNameComponent, "MethodEntry");
endGuardAdvice();
}
private void generateGoto(T1XTemplateTag tag) {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "targetBci);%n", adviceType.methodNameComponent, methodName);
endGuardAdvice();
}
private void generateIf(T1XTemplateTag tag) {
boolean isNull = tag == T1XTemplateTag.IFNULL || tag == T1XTemplateTag.IFNONNULL;
String value2 = isNull ? "null" : (IF_TEMPLATE_TAGS.contains(tag) ? "0" : "value2");
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "%d, value1, %s, targetBci);%n", adviceType.methodNameComponent, methodName, tag.opcode, value2);
endGuardAdvice();
}
private void generateTypeCheck(T1XTemplateTag tag) {
String name = CHECKCAST_TEMPLATE_TAGS.contains(tag) ? "CheckCast" : "InstanceOf";
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "object, classActor);%n", adviceType.methodNameComponent, methodName, name);
endGuardAdvice();
}
private void generateThrow() {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "object);%n", adviceType.methodNameComponent, methodName);
endGuardAdvice();
}
private void generateReThrow() {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "throwable);%n", adviceType.methodNameComponent, "Throw");
endGuardAdvice();
}
private void generateArrayLength() {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "array, length);%n", adviceType.methodNameComponent, methodName);
endGuardAdvice();
}
private void generateMonitor(T1XTemplateTag tag) {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "object);%n", adviceType.methodNameComponent, methodName);
endGuardAdvice();
}
private void generateReturn(T1XTemplateTag tag, String k) {
boolean vReturn = AdviceGeneratorHelper.codeMap.get(tag.opcode) == VMABytecodes.RETURN;
String arg = vReturn ? "" : putArg("value", k, "");
startGuardAdvice();
out.printf((vReturn ? INDENT12_ADVISE_NOARG_PREFIX : INDENT12_ADVISE_PREFIX) + "%s);%n", adviceType.methodNameComponent, methodName, arg);
endGuardAdvice();
}
private void generateIInc() {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "%d, index, increment);%n", adviceType.methodNameComponent, methodName, IINC.opcode);
endGuardAdvice();
}
private void generateDefault(T1XTemplateTag tag) {
startGuardAdvice();
out.printf(INDENT12_ADVISE_PREFIX + "%d);%n", adviceType.methodNameComponent, methodName, tag.opcode);
endGuardAdvice();
}
private String putValue(String k) {
return putValue(k, "");
}
private static String putValue(String k, String vSuffix) {
return putArg("value", k, vSuffix);
}
private static String putArg(String name, String k, String vSuffix) {
String value = name + vSuffix;
if (k.equals("Word")) {
return value + ".asAddress().toLong()";
} else {
return value;
}
}
private static String getDefault(String k) {
if (k.equals("float")) {
return "0.0f";
} else if (k.equals("double")) {