forked from mozilla/pluotsorbet
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bytecodes.ts
1182 lines (1071 loc) · 49.9 KB
/
bytecodes.ts
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) 2009, 2011, 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.
*/
module J2ME.Bytecode {
import assert = Debug.assert;
export class Bytes {
/**
* Gets a signed 1-byte value.
*/
public static beS1(data: Uint8Array, bci: number) {
return (data[bci] << 24) >> 24;
}
/**
* Gets a signed 2-byte big-endian value.
*/
public static beS2(data: Uint8Array, bci: number) {
return ((data[bci] << 8) | (data[bci + 1] & 0xff)) << 16 >> 16;
}
/**
* Gets an unsigned 1-byte value.
*/
public static beU1(data: Uint8Array, bci: number) {
return data[bci] & 0xff;
}
/**
* Gets an unsigned 2-byte big-endian value.
*/
public static beU2(data: Uint8Array, bci: number) {
return ((data[bci] & 0xff) << 8) | (data[bci + 1] & 0xff);
}
/**
* Gets a signed 4-byte big-endian value.
*/
public static beS4(data: Uint8Array, bci: number) {
return (data[bci] << 24) | ((data[bci + 1] & 0xff) << 16) | ((data[bci + 2] & 0xff) << 8) | (data[bci + 3] & 0xff);
}
/**
* Gets either a signed 2-byte or a signed 4-byte big-endian value.
*/
public static beSVar(data: Uint8Array, bci: number, fourByte: boolean) {
if (fourByte) {
return Bytes.beS4(data, bci);
} else {
return Bytes.beS2(data, bci);
}
}
}
export enum Condition {
/**
* Equal.
*/
EQ,
/**
* Not equal.
*/
NE,
/**
* Signed less than.
*/
LT,
/**
* Signed less than or equal.
*/
LE,
/**
* Signed greater than.
*/
GT,
/**
* Signed greater than or equal.
*/
GE,
/**
* Unsigned greater than or equal ("above than or equal").
*/
AE,
/**
* Unsigned less than or equal ("below than or equal").
*/
BE,
/**
* Unsigned greater than ("above than").
*/
AT,
/**
* Unsigned less than ("below than").
*/
BT,
/**
* Operation produced an overflow.
*/
OF,
/**
* Operation did not produce an overflow.
*/
NOF
}
/**
* The definitions of the bytecodes that are valid input to the compiler and
* related utility methods. This comprises two groups: the standard Java
* bytecodes defined by <a href=
* "http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc.html">
* Java Virtual Machine Specification</a>, and a set of <i>extended</i>
* bytecodes that support low-level programming, for example, memory barriers.
*
* The extended bytecodes are one or three bytes in size. The one-byte bytecodes
* follow the values in the standard set, with no gap. The three-byte extended
* bytecodes share a common first byte and carry additional instruction-specific
* information in the second and third bytes.
*/
export enum Bytecodes {
NOP = 0, // 0x00
ACONST_NULL = 1, // 0x01
ICONST_M1 = 2, // 0x02
ICONST_0 = 3, // 0x03
ICONST_1 = 4, // 0x04
ICONST_2 = 5, // 0x05
ICONST_3 = 6, // 0x06
ICONST_4 = 7, // 0x07
ICONST_5 = 8, // 0x08
LCONST_0 = 9, // 0x09
LCONST_1 = 10, // 0x0A
FCONST_0 = 11, // 0x0B
FCONST_1 = 12, // 0x0C
FCONST_2 = 13, // 0x0D
DCONST_0 = 14, // 0x0E
DCONST_1 = 15, // 0x0F
BIPUSH = 16, // 0x10
SIPUSH = 17, // 0x11
LDC = 18, // 0x12
LDC_W = 19, // 0x13
LDC2_W = 20, // 0x14
ILOAD = 21, // 0x15
LLOAD = 22, // 0x16
FLOAD = 23, // 0x17
DLOAD = 24, // 0x18
ALOAD = 25, // 0x19
ILOAD_0 = 26, // 0x1A
ILOAD_1 = 27, // 0x1B
ILOAD_2 = 28, // 0x1C
ILOAD_3 = 29, // 0x1D
LLOAD_0 = 30, // 0x1E
LLOAD_1 = 31, // 0x1F
LLOAD_2 = 32, // 0x20
LLOAD_3 = 33, // 0x21
FLOAD_0 = 34, // 0x22
FLOAD_1 = 35, // 0x23
FLOAD_2 = 36, // 0x24
FLOAD_3 = 37, // 0x25
DLOAD_0 = 38, // 0x26
DLOAD_1 = 39, // 0x27
DLOAD_2 = 40, // 0x28
DLOAD_3 = 41, // 0x29
ALOAD_0 = 42, // 0x2A
ALOAD_1 = 43, // 0x2B
ALOAD_2 = 44, // 0x2C
ALOAD_3 = 45, // 0x2D
IALOAD = 46, // 0x2E
LALOAD = 47, // 0x2F
FALOAD = 48, // 0x30
DALOAD = 49, // 0x31
AALOAD = 50, // 0x32
BALOAD = 51, // 0x33
CALOAD = 52, // 0x34
SALOAD = 53, // 0x35
ISTORE = 54, // 0x36
LSTORE = 55, // 0x37
FSTORE = 56, // 0x38
DSTORE = 57, // 0x39
ASTORE = 58, // 0x3A
ISTORE_0 = 59, // 0x3B
ISTORE_1 = 60, // 0x3C
ISTORE_2 = 61, // 0x3D
ISTORE_3 = 62, // 0x3E
LSTORE_0 = 63, // 0x3F
LSTORE_1 = 64, // 0x40
LSTORE_2 = 65, // 0x41
LSTORE_3 = 66, // 0x42
FSTORE_0 = 67, // 0x43
FSTORE_1 = 68, // 0x44
FSTORE_2 = 69, // 0x45
FSTORE_3 = 70, // 0x46
DSTORE_0 = 71, // 0x47
DSTORE_1 = 72, // 0x48
DSTORE_2 = 73, // 0x49
DSTORE_3 = 74, // 0x4A
ASTORE_0 = 75, // 0x4B
ASTORE_1 = 76, // 0x4C
ASTORE_2 = 77, // 0x4D
ASTORE_3 = 78, // 0x4E
IASTORE = 79, // 0x4F
LASTORE = 80, // 0x50
FASTORE = 81, // 0x51
DASTORE = 82, // 0x52
AASTORE = 83, // 0x53
BASTORE = 84, // 0x54
CASTORE = 85, // 0x55
SASTORE = 86, // 0x56
POP = 87, // 0x57
POP2 = 88, // 0x58
DUP = 89, // 0x59
DUP_X1 = 90, // 0x5A
DUP_X2 = 91, // 0x5B
DUP2 = 92, // 0x5C
DUP2_X1 = 93, // 0x5D
DUP2_X2 = 94, // 0x5E
SWAP = 95, // 0x5F
IADD = 96, // 0x60
LADD = 97, // 0x61
FADD = 98, // 0x62
DADD = 99, // 0x63
ISUB = 100, // 0x64
LSUB = 101, // 0x65
FSUB = 102, // 0x66
DSUB = 103, // 0x67
IMUL = 104, // 0x68
LMUL = 105, // 0x69
FMUL = 106, // 0x6A
DMUL = 107, // 0x6B
IDIV = 108, // 0x6C
LDIV = 109, // 0x6D
FDIV = 110, // 0x6E
DDIV = 111, // 0x6F
IREM = 112, // 0x70
LREM = 113, // 0x71
FREM = 114, // 0x72
DREM = 115, // 0x73
INEG = 116, // 0x74
LNEG = 117, // 0x75
FNEG = 118, // 0x76
DNEG = 119, // 0x77
ISHL = 120, // 0x78
LSHL = 121, // 0x79
ISHR = 122, // 0x7A
LSHR = 123, // 0x7B
IUSHR = 124, // 0x7C
LUSHR = 125, // 0x7D
IAND = 126, // 0x7E
LAND = 127, // 0x7F
IOR = 128, // 0x80
LOR = 129, // 0x81
IXOR = 130, // 0x82
LXOR = 131, // 0x83
IINC = 132, // 0x84
I2L = 133, // 0x85
I2F = 134, // 0x86
I2D = 135, // 0x87
L2I = 136, // 0x88
L2F = 137, // 0x89
L2D = 138, // 0x8A
F2I = 139, // 0x8B
F2L = 140, // 0x8C
F2D = 141, // 0x8D
D2I = 142, // 0x8E
D2L = 143, // 0x8F
D2F = 144, // 0x90
I2B = 145, // 0x91
I2C = 146, // 0x92
I2S = 147, // 0x93
LCMP = 148, // 0x94
FCMPL = 149, // 0x95
FCMPG = 150, // 0x96
DCMPL = 151, // 0x97
DCMPG = 152, // 0x98
IFEQ = 153, // 0x99
IFNE = 154, // 0x9A
IFLT = 155, // 0x9B
IFGE = 156, // 0x9C
IFGT = 157, // 0x9D
IFLE = 158, // 0x9E
IF_ICMPEQ = 159, // 0x9F
IF_ICMPNE = 160, // 0xA0
IF_ICMPLT = 161, // 0xA1
IF_ICMPGE = 162, // 0xA2
IF_ICMPGT = 163, // 0xA3
IF_ICMPLE = 164, // 0xA4
IF_ACMPEQ = 165, // 0xA5
IF_ACMPNE = 166, // 0xA6
GOTO = 167, // 0xA7
JSR = 168, // 0xA8
RET = 169, // 0xA9
TABLESWITCH = 170, // 0xAA
LOOKUPSWITCH = 171, // 0xAB
IRETURN = 172, // 0xAC
LRETURN = 173, // 0xAD
FRETURN = 174, // 0xAE
DRETURN = 175, // 0xAF
ARETURN = 176, // 0xB0
RETURN = 177, // 0xB1
GETSTATIC = 178, // 0xB2
PUTSTATIC = 179, // 0xB3
GETFIELD = 180, // 0xB4
PUTFIELD = 181, // 0xB5
INVOKEVIRTUAL = 182, // 0xB6
INVOKESPECIAL = 183, // 0xB7
INVOKESTATIC = 184, // 0xB8
INVOKEINTERFACE = 185, // 0xB9
XXXUNUSEDXXX = 186, // 0xBA
NEW = 187, // 0xBB
NEWARRAY = 188, // 0xBC
ANEWARRAY = 189, // 0xBD
ARRAYLENGTH = 190, // 0xBE
ATHROW = 191, // 0xBF
CHECKCAST = 192, // 0xC0
INSTANCEOF = 193, // 0xC1
MONITORENTER = 194, // 0xC2
MONITOREXIT = 195, // 0xC3
WIDE = 196, // 0xC4
MULTIANEWARRAY = 197, // 0xC5
IFNULL = 198, // 0xC6
IFNONNULL = 199, // 0xC7
GOTO_W = 200, // 0xC8
JSR_W = 201, // 0xC9
BREAKPOINT = 202, // 0xCA
ALOAD_ILOAD = 210,
IINC_GOTO = 211,
ARRAYLENGTH_IF_ICMPGE = 212,
RESOLVED_GETFIELD = 213,
RESOLVED_PUTFIELD = 214,
RESOLVED_INVOKEVIRTUAL = 215,
ILLEGAL = 255,
END = 256,
/**
* The last opcode defined by the JVM specification. To iterate over all JVM bytecodes:
*/
LAST_JVM_OPCODE = Bytecodes.JSR_W
}
enum Flags {
/**
* Denotes an instruction that ends a basic block and does not let control flow fall through to its lexical successor.
*/
STOP = 0x00000001,
/**
* Denotes an instruction that ends a basic block and may let control flow fall through to its lexical successor.
* In practice this means it is a conditional branch.
*/
FALL_THROUGH = 0x00000002,
/**
* Denotes an instruction that has a 2 or 4 byte operand that is an offset to another instruction in the same method.
* This does not include the {@link Bytecodes#TABLESWITCH} or {@link Bytecodes#LOOKUPSWITCH} instructions.
*/
BRANCH = 0x00000004,
/**
* Denotes an instruction that reads the value of a static or instance field.
*/
FIELD_READ = 0x00000008,
/**
* Denotes an instruction that writes the value of a static or instance field.
*/
FIELD_WRITE = 0x00000010,
/**
* Denotes an instruction that is not defined in the JVM specification.
*/
EXTENSION = 0x00000020,
/**
* Denotes an instruction that can cause aFlags.TRAP.
*/
TRAP = 0x00000080,
/**
* Denotes an instruction that is commutative.
*/
COMMUTATIVE = 0x00000100,
/**
* Denotes an instruction that is associative.
*/
ASSOCIATIVE = 0x00000200,
/**
* Denotes an instruction that loads an operand.
*/
LOAD = 0x00000400,
/**
* Denotes an instruction that stores an operand.
*/
STORE = 0x00000800,
/**
* Denotes the 4 INVOKE* instructions.
*/
INVOKE = 0x00001000,
/**
* Denotes a return instruction that ends a basic block.
*/
RETURN = 0x00002000
}
/**
* A array that maps from a bytecode value to the set of {@link Flags} for the corresponding instruction.
*/
export var flags = new Uint32Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 128, 128, 128, 128, 128, 128, 128, 128, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 128, 128, 128, 128, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 768, 768, 768, 768, 0, 0, 0, 0, 768, 768, 768, 768, 128, 128, 0, 0, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 768, 768, 768, 768, 768, 768, 3072, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 262, 262, 6, 6, 6, 6, 262, 262, 5, 5, 1, 1, 1, 8321, 8321, 8321, 8321, 8321, 8321, 136, 144, 136, 144, 4224, 4224, 4224, 4224, 0, 128, 128, 128, 128, 129, 128, 128, 128, 128, 0, 128, 6, 6, 5, 5, 128, 0, 0, 0, 0, 0, 0, 0, 1024, 3077, 390, 136, 144, 4224, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
/**
* A array that maps from a bytecode value to the length in bytes for the corresponding instruction.
*/
export var length = new Uint32Array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 2, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 0, 0, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 5, 0, 3, 2, 3, 1, 1, 3, 3, 1, 1, 0, 4, 3, 3, 5, 5, 1, 0, 0, 0, 0, 0, 0, 0, 2, 3, 1, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
var writer = new IndentingWriter();
/**
* Assert that |flags| and |lengths| tables are up to date.
*/
if (!release) {
function define(opcode: number, name: string, format: string, flags_: Flags = 0) {
var instructionLength = format.length;
assert(length[opcode] === instructionLength, "Wrong instruction length");
assert(flags[opcode] === flags_, "Wrong flags");
assert(!isConditionalBranch(opcode) || isBranch(opcode), "a conditional branch must also be a branch");
}
/**
* Only call this before the compiler is used.
*/
function defineBytecodes() {
define(Bytecodes.NOP , "nop" , "b" );
define(Bytecodes.ACONST_NULL , "aconst_null" , "b" );
define(Bytecodes.ICONST_M1 , "iconst_m1" , "b" );
define(Bytecodes.ICONST_0 , "iconst_0" , "b" );
define(Bytecodes.ICONST_1 , "iconst_1" , "b" );
define(Bytecodes.ICONST_2 , "iconst_2" , "b" );
define(Bytecodes.ICONST_3 , "iconst_3" , "b" );
define(Bytecodes.ICONST_4 , "iconst_4" , "b" );
define(Bytecodes.ICONST_5 , "iconst_5" , "b" );
define(Bytecodes.LCONST_0 , "lconst_0" , "b" );
define(Bytecodes.LCONST_1 , "lconst_1" , "b" );
define(Bytecodes.FCONST_0 , "fconst_0" , "b" );
define(Bytecodes.FCONST_1 , "fconst_1" , "b" );
define(Bytecodes.FCONST_2 , "fconst_2" , "b" );
define(Bytecodes.DCONST_0 , "dconst_0" , "b" );
define(Bytecodes.DCONST_1 , "dconst_1" , "b" );
define(Bytecodes.BIPUSH , "bipush" , "bc" );
define(Bytecodes.SIPUSH , "sipush" , "bcc" );
define(Bytecodes.LDC , "ldc" , "bi" , Flags.TRAP);
define(Bytecodes.LDC_W , "ldc_w" , "bii" , Flags.TRAP);
define(Bytecodes.LDC2_W , "ldc2_w" , "bii" , Flags.TRAP);
define(Bytecodes.ILOAD , "iload" , "bi" , Flags.LOAD);
define(Bytecodes.LLOAD , "lload" , "bi" , Flags.LOAD);
define(Bytecodes.FLOAD , "fload" , "bi" , Flags.LOAD);
define(Bytecodes.DLOAD , "dload" , "bi" , Flags.LOAD);
define(Bytecodes.ALOAD , "aload" , "bi" , Flags.LOAD);
define(Bytecodes.ILOAD_0 , "iload_0" , "b" , Flags.LOAD);
define(Bytecodes.ILOAD_1 , "iload_1" , "b" , Flags.LOAD);
define(Bytecodes.ILOAD_2 , "iload_2" , "b" , Flags.LOAD);
define(Bytecodes.ILOAD_3 , "iload_3" , "b" , Flags.LOAD);
define(Bytecodes.LLOAD_0 , "lload_0" , "b" , Flags.LOAD);
define(Bytecodes.LLOAD_1 , "lload_1" , "b" , Flags.LOAD);
define(Bytecodes.LLOAD_2 , "lload_2" , "b" , Flags.LOAD);
define(Bytecodes.LLOAD_3 , "lload_3" , "b" , Flags.LOAD);
define(Bytecodes.FLOAD_0 , "fload_0" , "b" , Flags.LOAD);
define(Bytecodes.FLOAD_1 , "fload_1" , "b" , Flags.LOAD);
define(Bytecodes.FLOAD_2 , "fload_2" , "b" , Flags.LOAD);
define(Bytecodes.FLOAD_3 , "fload_3" , "b" , Flags.LOAD);
define(Bytecodes.DLOAD_0 , "dload_0" , "b" , Flags.LOAD);
define(Bytecodes.DLOAD_1 , "dload_1" , "b" , Flags.LOAD);
define(Bytecodes.DLOAD_2 , "dload_2" , "b" , Flags.LOAD);
define(Bytecodes.DLOAD_3 , "dload_3" , "b" , Flags.LOAD);
define(Bytecodes.ALOAD_0 , "aload_0" , "b" , Flags.LOAD);
define(Bytecodes.ALOAD_1 , "aload_1" , "b" , Flags.LOAD);
define(Bytecodes.ALOAD_2 , "aload_2" , "b" , Flags.LOAD);
define(Bytecodes.ALOAD_3 , "aload_3" , "b" , Flags.LOAD);
define(Bytecodes.IALOAD , "iaload" , "b" , Flags.TRAP);
define(Bytecodes.LALOAD , "laload" , "b" , Flags.TRAP);
define(Bytecodes.FALOAD , "faload" , "b" , Flags.TRAP);
define(Bytecodes.DALOAD , "daload" , "b" , Flags.TRAP);
define(Bytecodes.AALOAD , "aaload" , "b" , Flags.TRAP);
define(Bytecodes.BALOAD , "baload" , "b" , Flags.TRAP);
define(Bytecodes.CALOAD , "caload" , "b" , Flags.TRAP);
define(Bytecodes.SALOAD , "saload" , "b" , Flags.TRAP);
define(Bytecodes.ISTORE , "istore" , "bi" , Flags.STORE);
define(Bytecodes.LSTORE , "lstore" , "bi" , Flags.STORE);
define(Bytecodes.FSTORE , "fstore" , "bi" , Flags.STORE);
define(Bytecodes.DSTORE , "dstore" , "bi" , Flags.STORE);
define(Bytecodes.ASTORE , "astore" , "bi" , Flags.STORE);
define(Bytecodes.ISTORE_0 , "istore_0" , "b" , Flags.STORE);
define(Bytecodes.ISTORE_1 , "istore_1" , "b" , Flags.STORE);
define(Bytecodes.ISTORE_2 , "istore_2" , "b" , Flags.STORE);
define(Bytecodes.ISTORE_3 , "istore_3" , "b" , Flags.STORE);
define(Bytecodes.LSTORE_0 , "lstore_0" , "b" , Flags.STORE);
define(Bytecodes.LSTORE_1 , "lstore_1" , "b" , Flags.STORE);
define(Bytecodes.LSTORE_2 , "lstore_2" , "b" , Flags.STORE);
define(Bytecodes.LSTORE_3 , "lstore_3" , "b" , Flags.STORE);
define(Bytecodes.FSTORE_0 , "fstore_0" , "b" , Flags.STORE);
define(Bytecodes.FSTORE_1 , "fstore_1" , "b" , Flags.STORE);
define(Bytecodes.FSTORE_2 , "fstore_2" , "b" , Flags.STORE);
define(Bytecodes.FSTORE_3 , "fstore_3" , "b" , Flags.STORE);
define(Bytecodes.DSTORE_0 , "dstore_0" , "b" , Flags.STORE);
define(Bytecodes.DSTORE_1 , "dstore_1" , "b" , Flags.STORE);
define(Bytecodes.DSTORE_2 , "dstore_2" , "b" , Flags.STORE);
define(Bytecodes.DSTORE_3 , "dstore_3" , "b" , Flags.STORE);
define(Bytecodes.ASTORE_0 , "astore_0" , "b" , Flags.STORE);
define(Bytecodes.ASTORE_1 , "astore_1" , "b" , Flags.STORE);
define(Bytecodes.ASTORE_2 , "astore_2" , "b" , Flags.STORE);
define(Bytecodes.ASTORE_3 , "astore_3" , "b" , Flags.STORE);
define(Bytecodes.IASTORE , "iastore" , "b" , Flags.TRAP);
define(Bytecodes.LASTORE , "lastore" , "b" , Flags.TRAP);
define(Bytecodes.FASTORE , "fastore" , "b" , Flags.TRAP);
define(Bytecodes.DASTORE , "dastore" , "b" , Flags.TRAP);
define(Bytecodes.AASTORE , "aastore" , "b" , Flags.TRAP);
define(Bytecodes.BASTORE , "bastore" , "b" , Flags.TRAP);
define(Bytecodes.CASTORE , "castore" , "b" , Flags.TRAP);
define(Bytecodes.SASTORE , "sastore" , "b" , Flags.TRAP);
define(Bytecodes.POP , "pop" , "b" );
define(Bytecodes.POP2 , "pop2" , "b" );
define(Bytecodes.DUP , "dup" , "b" );
define(Bytecodes.DUP_X1 , "dup_x1" , "b" );
define(Bytecodes.DUP_X2 , "dup_x2" , "b" );
define(Bytecodes.DUP2 , "dup2" , "b" );
define(Bytecodes.DUP2_X1 , "dup2_x1" , "b" );
define(Bytecodes.DUP2_X2 , "dup2_x2" , "b" );
define(Bytecodes.SWAP , "swap" , "b" );
define(Bytecodes.IADD , "iadd" , "b" , Flags.COMMUTATIVE | Flags.ASSOCIATIVE);
define(Bytecodes.LADD , "ladd" , "b" , Flags.COMMUTATIVE | Flags.ASSOCIATIVE);
define(Bytecodes.FADD , "fadd" , "b" , Flags.COMMUTATIVE | Flags.ASSOCIATIVE);
define(Bytecodes.DADD , "dadd" , "b" , Flags.COMMUTATIVE | Flags.ASSOCIATIVE);
define(Bytecodes.ISUB , "isub" , "b" );
define(Bytecodes.LSUB , "lsub" , "b" );
define(Bytecodes.FSUB , "fsub" , "b" );
define(Bytecodes.DSUB , "dsub" , "b" );
define(Bytecodes.IMUL , "imul" , "b" , Flags.COMMUTATIVE | Flags.ASSOCIATIVE);
define(Bytecodes.LMUL , "lmul" , "b" , Flags.COMMUTATIVE | Flags.ASSOCIATIVE);
define(Bytecodes.FMUL , "fmul" , "b" , Flags.COMMUTATIVE | Flags.ASSOCIATIVE);
define(Bytecodes.DMUL , "dmul" , "b" , Flags.COMMUTATIVE | Flags.ASSOCIATIVE);
define(Bytecodes.IDIV , "idiv" , "b" , Flags.TRAP);
define(Bytecodes.LDIV , "ldiv" , "b" , Flags.TRAP);
define(Bytecodes.FDIV , "fdiv" , "b" );
define(Bytecodes.DDIV , "ddiv" , "b" );
define(Bytecodes.IREM , "irem" , "b" , Flags.TRAP);
define(Bytecodes.LREM , "lrem" , "b" , Flags.TRAP);
define(Bytecodes.FREM , "frem" , "b" );
define(Bytecodes.DREM , "drem" , "b" );
define(Bytecodes.INEG , "ineg" , "b" );
define(Bytecodes.LNEG , "lneg" , "b" );
define(Bytecodes.FNEG , "fneg" , "b" );
define(Bytecodes.DNEG , "dneg" , "b" );
define(Bytecodes.ISHL , "ishl" , "b" );
define(Bytecodes.LSHL , "lshl" , "b" );
define(Bytecodes.ISHR , "ishr" , "b" );
define(Bytecodes.LSHR , "lshr" , "b" );
define(Bytecodes.IUSHR , "iushr" , "b" );
define(Bytecodes.LUSHR , "lushr" , "b" );
define(Bytecodes.IAND , "iand" , "b" , Flags.COMMUTATIVE | Flags.ASSOCIATIVE);
define(Bytecodes.LAND , "land" , "b" , Flags.COMMUTATIVE | Flags.ASSOCIATIVE);
define(Bytecodes.IOR , "ior" , "b" , Flags.COMMUTATIVE | Flags.ASSOCIATIVE);
define(Bytecodes.LOR , "lor" , "b" , Flags.COMMUTATIVE | Flags.ASSOCIATIVE);
define(Bytecodes.IXOR , "ixor" , "b" , Flags.COMMUTATIVE | Flags.ASSOCIATIVE);
define(Bytecodes.LXOR , "lxor" , "b" , Flags.COMMUTATIVE | Flags.ASSOCIATIVE);
define(Bytecodes.IINC , "iinc" , "bic" , Flags.LOAD | Flags.STORE);
define(Bytecodes.I2L , "i2l" , "b" );
define(Bytecodes.I2F , "i2f" , "b" );
define(Bytecodes.I2D , "i2d" , "b" );
define(Bytecodes.L2I , "l2i" , "b" );
define(Bytecodes.L2F , "l2f" , "b" );
define(Bytecodes.L2D , "l2d" , "b" );
define(Bytecodes.F2I , "f2i" , "b" );
define(Bytecodes.F2L , "f2l" , "b" );
define(Bytecodes.F2D , "f2d" , "b" );
define(Bytecodes.D2I , "d2i" , "b" );
define(Bytecodes.D2L , "d2l" , "b" );
define(Bytecodes.D2F , "d2f" , "b" );
define(Bytecodes.I2B , "i2b" , "b" );
define(Bytecodes.I2C , "i2c" , "b" );
define(Bytecodes.I2S , "i2s" , "b" );
define(Bytecodes.LCMP , "lcmp" , "b" );
define(Bytecodes.FCMPL , "fcmpl" , "b" );
define(Bytecodes.FCMPG , "fcmpg" , "b" );
define(Bytecodes.DCMPL , "dcmpl" , "b" );
define(Bytecodes.DCMPG , "dcmpg" , "b" );
define(Bytecodes.IFEQ , "ifeq" , "boo" , Flags.FALL_THROUGH | Flags.BRANCH);
define(Bytecodes.IFNE , "ifne" , "boo" , Flags.FALL_THROUGH | Flags.BRANCH);
define(Bytecodes.IFLT , "iflt" , "boo" , Flags.FALL_THROUGH | Flags.BRANCH);
define(Bytecodes.IFGE , "ifge" , "boo" , Flags.FALL_THROUGH | Flags.BRANCH);
define(Bytecodes.IFGT , "ifgt" , "boo" , Flags.FALL_THROUGH | Flags.BRANCH);
define(Bytecodes.IFLE , "ifle" , "boo" , Flags.FALL_THROUGH | Flags.BRANCH);
define(Bytecodes.IF_ICMPEQ , "if_icmpeq" , "boo" , Flags.COMMUTATIVE | Flags.FALL_THROUGH | Flags.BRANCH);
define(Bytecodes.IF_ICMPNE , "if_icmpne" , "boo" , Flags.COMMUTATIVE | Flags.FALL_THROUGH | Flags.BRANCH);
define(Bytecodes.IF_ICMPLT , "if_icmplt" , "boo" , Flags.FALL_THROUGH | Flags.BRANCH);
define(Bytecodes.IF_ICMPGE , "if_icmpge" , "boo" , Flags.FALL_THROUGH | Flags.BRANCH);
define(Bytecodes.IF_ICMPGT , "if_icmpgt" , "boo" , Flags.FALL_THROUGH | Flags.BRANCH);
define(Bytecodes.IF_ICMPLE , "if_icmple" , "boo" , Flags.FALL_THROUGH | Flags.BRANCH);
define(Bytecodes.IF_ACMPEQ , "if_acmpeq" , "boo" , Flags.COMMUTATIVE | Flags.FALL_THROUGH | Flags.BRANCH);
define(Bytecodes.IF_ACMPNE , "if_acmpne" , "boo" , Flags.COMMUTATIVE | Flags.FALL_THROUGH | Flags.BRANCH);
define(Bytecodes.GOTO , "goto" , "boo" , Flags.STOP | Flags.BRANCH);
define(Bytecodes.JSR , "jsr" , "boo" , Flags.STOP | Flags.BRANCH);
define(Bytecodes.RET , "ret" , "bi" , Flags.STOP);
define(Bytecodes.TABLESWITCH , "tableswitch" , "" , Flags.STOP);
define(Bytecodes.LOOKUPSWITCH , "lookupswitch" , "" , Flags.STOP);
define(Bytecodes.IRETURN , "ireturn" , "b" , Flags.TRAP | Flags.STOP | Flags.RETURN);
define(Bytecodes.LRETURN , "lreturn" , "b" , Flags.TRAP | Flags.STOP | Flags.RETURN);
define(Bytecodes.FRETURN , "freturn" , "b" , Flags.TRAP | Flags.STOP | Flags.RETURN);
define(Bytecodes.DRETURN , "dreturn" , "b" , Flags.TRAP | Flags.STOP | Flags.RETURN);
define(Bytecodes.ARETURN , "areturn" , "b" , Flags.TRAP | Flags.STOP | Flags.RETURN);
define(Bytecodes.RETURN , "return" , "b" , Flags.TRAP | Flags.STOP | Flags.RETURN);
define(Bytecodes.GETSTATIC , "getstatic" , "bjj" , Flags.TRAP | Flags.FIELD_READ);
define(Bytecodes.PUTSTATIC , "putstatic" , "bjj" , Flags.TRAP | Flags.FIELD_WRITE);
define(Bytecodes.GETFIELD , "getfield" , "bjj" , Flags.TRAP | Flags.FIELD_READ);
define(Bytecodes.PUTFIELD , "putfield" , "bjj" , Flags.TRAP | Flags.FIELD_WRITE);
define(Bytecodes.RESOLVED_GETFIELD , "getfield" , "bjj" , Flags.TRAP | Flags.FIELD_READ);
define(Bytecodes.RESOLVED_PUTFIELD , "putfield" , "bjj" , Flags.TRAP | Flags.FIELD_WRITE);
define(Bytecodes.INVOKEVIRTUAL , "invokevirtual" , "bjj" , Flags.TRAP | Flags.INVOKE);
define(Bytecodes.RESOLVED_INVOKEVIRTUAL , "invokevirtual" , "bjj" , Flags.TRAP | Flags.INVOKE);
define(Bytecodes.INVOKESPECIAL , "invokespecial" , "bjj" , Flags.TRAP | Flags.INVOKE);
define(Bytecodes.INVOKESTATIC , "invokestatic" , "bjj" , Flags.TRAP | Flags.INVOKE);
define(Bytecodes.INVOKEINTERFACE , "invokeinterface" , "bjja_", Flags.TRAP | Flags.INVOKE);
define(Bytecodes.XXXUNUSEDXXX , "xxxunusedxxx" , "" );
define(Bytecodes.NEW , "new" , "bii" , Flags.TRAP);
define(Bytecodes.NEWARRAY , "newarray" , "bc" , Flags.TRAP);
define(Bytecodes.ANEWARRAY , "anewarray" , "bii" , Flags.TRAP);
define(Bytecodes.ARRAYLENGTH , "arraylength" , "b" , Flags.TRAP);
define(Bytecodes.ATHROW , "athrow" , "b" , Flags.TRAP | Flags.STOP);
define(Bytecodes.CHECKCAST , "checkcast" , "bii" , Flags.TRAP);
define(Bytecodes.INSTANCEOF , "instanceof" , "bii" , Flags.TRAP);
define(Bytecodes.MONITORENTER , "monitorenter" , "b" , Flags.TRAP);
define(Bytecodes.MONITOREXIT , "monitorexit" , "b" , Flags.TRAP);
define(Bytecodes.WIDE , "wide" , "" );
define(Bytecodes.MULTIANEWARRAY , "multianewarray" , "biic" , Flags.TRAP);
define(Bytecodes.IFNULL , "ifnull" , "boo" , Flags.FALL_THROUGH | Flags.BRANCH);
define(Bytecodes.IFNONNULL , "ifnonnull" , "boo" , Flags.FALL_THROUGH | Flags.BRANCH);
define(Bytecodes.GOTO_W , "goto_w" , "boooo", Flags.STOP | Flags.BRANCH);
define(Bytecodes.JSR_W , "jsr_w" , "boooo", Flags.STOP | Flags.BRANCH);
define(Bytecodes.BREAKPOINT , "breakpoint" , "b" , Flags.TRAP);
define(Bytecodes.ALOAD_ILOAD , "aload_iload" , "bi" , Flags.LOAD);
define(Bytecodes.IINC_GOTO , "iinc_goto" , "bic" , Flags.LOAD | Flags.STORE | Flags.STOP | Flags.BRANCH);
define(Bytecodes.ARRAYLENGTH_IF_ICMPGE , "arraylength_IF_ICMPGE" , "b" , Flags.COMMUTATIVE | Flags.FALL_THROUGH | Flags.BRANCH | Flags.TRAP);
}
defineBytecodes();
}
/**
* Gets the length of an instruction denoted by a given opcode.
*/
export function lengthOf(opcode: Bytecodes): number {
return length[opcode & 0xff];
}
export function lengthAt(code: Uint8Array, bci: number): number {
var opcode = Bytes.beU1(code, bci);
var _length = length[opcode & 0xff];
if (_length == 0) {
switch (opcode) {
case Bytecodes.TABLESWITCH: {
return new BytecodeTableSwitch(code, bci).size();
}
case Bytecodes.LOOKUPSWITCH: {
return new BytecodeLookupSwitch(code, bci).size();
}
case Bytecodes.WIDE: {
var opc = Bytes.beU1(code, bci + 1);
if (opc == Bytecodes.RET) {
return 4;
} else if (opc == Bytecodes.IINC) {
return 6;
} else {
return 4; // a load or store bytecode
}
}
default:
throw new Error("unknown variable-length bytecode: " + opcode);
}
}
return _length;
}
/**
* Determines if an opcode is commutative.
*/
function isCommutative(opcode: Bytecodes): boolean {
return (flags[opcode & 0xff] & Flags.COMMUTATIVE) != 0;
}
/**
* Determines if a given opcode denotes an instruction that can cause an implicit exception.
*/
export function canTrap(opcode: Bytecodes): boolean {
return (flags[opcode & 0xff] & Flags.TRAP) != 0;
}
/**
* Determines if a given opcode denotes an instruction that loads a local variable to the operand stack.
*/
function isLoad(opcode: Bytecodes): boolean {
return (flags[opcode & 0xff] & Flags.LOAD) != 0;
}
/**
* Determines if a given opcode denotes an instruction that ends a basic block and does not let control flow fall
* through to its lexical successor.
*/
export function isStop(opcode: Bytecodes): boolean {
return (flags[opcode & 0xff] & Flags.STOP) != 0;
}
/**
* Determines if a given opcode denotes an instruction that stores a value to a local variable
* after popping it from the operand stack.
*/
export function isInvoke(opcode: Bytecodes): boolean {
return (flags[opcode & 0xff] & Flags.INVOKE) != 0;
}
/**
* Determines if a given opcode denotes an instruction that stores a value to a local variable
* after popping it from the operand stack.
*/
export function isStore(opcode: Bytecodes): boolean {
return (flags[opcode & 0xff] & Flags.STORE) != 0;
}
/**
* Determines if a given opcode is an instruction that delimits a basic block.
*/
export function isBlockEnd(opcode: Bytecodes): boolean {
return (flags[opcode & 0xff] & (Flags.STOP | Flags.FALL_THROUGH)) != 0;
}
/**
* Determines if a given opcode is an instruction that has a 2 or 4 byte operand that is an offset to another
* instruction in the same method. This does not include the {@linkplain #TABLESWITCH switch} instructions.
*/
function isBranch(opcode: Bytecodes): boolean {
return (flags[opcode & 0xff] & Flags.BRANCH) != 0;
}
/**
* Determines if a given opcode denotes a conditional branch.
*/
function isConditionalBranch(opcode: Bytecodes): boolean {
return (flags[opcode & 0xff] & Flags.FALL_THROUGH) != 0;
}
/**
* Determines if a given opcode denotes a standard bytecode. A standard bytecode is
* defined in the JVM specification.
*/
function isStandard(opcode: Bytecodes): boolean {
return (flags[opcode & 0xff] & Flags.EXTENSION) == 0;
}
/**
* Determines if a given opcode denotes an extended bytecode.
*/
function isExtended(opcode: Bytecodes): boolean {
return (flags[opcode & 0xff] & Flags.EXTENSION) != 0;
}
/**
* Determines if a given opcode is a three-byte extended bytecode.
*/
function isThreeByteExtended(opcode: Bytecodes): boolean {
return (opcode & ~0xff) != 0;
}
/**
* Determines if a given opcode is a return bytecode.
*/
export function isReturn(opcode: Bytecodes): boolean {
return !!(flags[opcode & 0xff] & Flags.RETURN);
}
export class BytecodeSwitch {
/**
* The bytecode array or {@code null} if {@link #stream} is not {@code null}.
*/
private code: Uint8Array;
/**
* Index of start of switch instruction.
*/
protected bci: number;
/**
* Index of the start of the additional data for the switch instruction, aligned to a multiple of four from the method start.
*/
protected alignedBci: number;
/**
* Constructor for a bytecode array.
* @param code the bytecode array containing the switch instruction.
* @param bci the index in the array of the switch instruction
*/
constructor(code: Uint8Array, bci: number) {
this.alignedBci = (bci + 4) & 0xfffffffc;
this.code = code;
this.bci = bci;
}
/**
* Gets the index of the instruction denoted by the {@code i}'th switch target.
* @param i index of the switch target
* @return the index of the instruction denoted by the {@code i}'th switch target
*/
public targetAt(i: number): number {
return this.bci + this.offsetAt(i);
}
/**
* Gets the index of the instruction for the default switch target.
* @return the index of the instruction for the default switch target
*/
public defaultTarget(): number {
return this.bci + this.defaultOffset();
}
/**
* Gets the offset from the start of the switch instruction to the default switch target.
* @return the offset to the default switch target
*/
public defaultOffset(): number {
throw Debug.abstractMethod("defaultOffset");
}
/**
* Gets the key at {@code i}'th switch target index.
* @param i the switch target index
* @return the key at {@code i}'th switch target index
*/
public keyAt(i: number): number {
throw Debug.abstractMethod("keyAt");
}
/**
* Gets the offset from the start of the switch instruction for the {@code i}'th switch target.
* @param i the switch target index
* @return the offset to the {@code i}'th switch target
*/
public offsetAt(i: number): number {
throw Debug.abstractMethod("offsetAt");
}
/**
* Gets the number of switch targets.
* @return the number of switch targets
*/
public numberOfCases(): number {
throw Debug.abstractMethod("numberOfCases");
}
/**
* Gets the total size in bytes of the switch instruction.
* @return the total size in bytes of the switch instruction
*/
public size(): number {
throw Debug.abstractMethod("size");
}
/**
* Reads the signed value at given bytecode index.
* @param bci the start index of the value to retrieve
* @return the signed, 4-byte value in the bytecode array starting at {@code bci}
*/
protected readWord(bci: number): number {
return Bytes.beS4(this.code, bci);
}
}
export class BytecodeTableSwitch extends BytecodeSwitch {
private static OFFSET_TO_LOW_KEY = 4;
private static OFFSET_TO_HIGH_KEY = 8;
private static OFFSET_TO_FIRST_JUMP_OFFSET = 12;
private static JUMP_OFFSET_SIZE = 4;
/**
* Constructor for a bytecode array.
* @param code the bytecode array containing the switch instruction.
* @param bci the index in the array of the switch instruction
*/
constructor(code: Uint8Array, bci: number) {
super(code, bci);
}
/**
* Gets the low key of the table switch.
*/
public lowKey() {
return this.readWord(this.alignedBci + BytecodeTableSwitch.OFFSET_TO_LOW_KEY);
}
/**
* Gets the high key of the table switch.
*/
public highKey() {
return this.readWord(this.alignedBci + BytecodeTableSwitch.OFFSET_TO_HIGH_KEY);
}
public keyAt(i: number) {
return this.lowKey() + i;
}
public defaultOffset(): number {
return this.readWord(this.alignedBci);
}
public offsetAt(i: number): number {
return this.readWord(this.alignedBci + BytecodeTableSwitch.OFFSET_TO_FIRST_JUMP_OFFSET + BytecodeTableSwitch.JUMP_OFFSET_SIZE * i);
}
public numberOfCases(): number {
return this.highKey() - this.lowKey() + 1;
}
public size(): number {
return this.alignedBci + BytecodeTableSwitch.OFFSET_TO_FIRST_JUMP_OFFSET + BytecodeTableSwitch.JUMP_OFFSET_SIZE * this.numberOfCases() - this.bci;
}
}
export class BytecodeLookupSwitch extends BytecodeSwitch {
private static OFFSET_TO_NUMBER_PAIRS = 4;
private static OFFSET_TO_FIRST_PAIR_MATCH = 8;
private static OFFSET_TO_FIRST_PAIR_OFFSET = 12;
private static PAIR_SIZE = 8;
constructor(code: Uint8Array, bci: number) {
super(code, bci);
}
public defaultOffset(): number {
return this.readWord(this.alignedBci);
}
public offsetAt(i: number): number {
return this.readWord(this.alignedBci + BytecodeLookupSwitch.OFFSET_TO_FIRST_PAIR_OFFSET + BytecodeLookupSwitch.PAIR_SIZE * i);
}
public keyAt(i): number {
return this.readWord(this.alignedBci + BytecodeLookupSwitch.OFFSET_TO_FIRST_PAIR_MATCH + BytecodeLookupSwitch.PAIR_SIZE * i);
}
public numberOfCases(): number {
return this.readWord(this.alignedBci + BytecodeLookupSwitch.OFFSET_TO_NUMBER_PAIRS);
}
public size(): number {
return this.alignedBci + BytecodeLookupSwitch.OFFSET_TO_FIRST_PAIR_MATCH + BytecodeLookupSwitch.PAIR_SIZE * this.numberOfCases() - this.bci;
}
}
/**
* A utility class that makes iterating over bytecodes and reading operands
* simpler and less error prone. For example, it handles the {@link Bytecodes#WIDE} instruction
* and wide variants of instructions internally.
*/
export class BytecodeStream {
private _code: Uint8Array;
private _opcode: Bytecodes;
private _currentBCI: number;