summaryrefslogtreecommitdiffstats
path: root/libdex/InstrUtils.c
blob: d0dea5f61d222d8bebb4e4b9bdec23970440a3f0 (plain)
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
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * 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.
 */

/*
 * Dalvik instruction utility functions.
 *
 * IMPORTANT NOTE: Much of the contents of this file are generated
 * automatically by the opcode-gen tool. Any edits to the generated
 * sections will get wiped out the next time the tool is run.
 */

#include "InstrUtils.h"
#include <stdlib.h>

/*
 * Table that maps each opcode to the full width of instructions that
 * use that opcode, in (16-bit) code units. Unimplemented opcodes as
 * well as the "breakpoint" opcode have a width of zero.
 */
static InstructionWidth gOpcodeWidthTable[kNumDalvikInstructions] = {
    // BEGIN(libdex-widths); GENERATED AUTOMATICALLY BY opcode-gen
    1, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 2, 3, 2, 2, 3, 5, 2, 2, 3, 2, 1, 1, 2,
    2, 1, 2, 2, 3, 3, 3, 1, 1, 2, 3, 3, 3, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0,
    0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3,
    3, 3, 3, 0, 3, 3, 3, 3, 3, 0, 0, 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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 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,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 3, 3,
    3, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 0,
    // END(libdex-widths)
};

/*
 * Table that maps each opcode to the flags associated with that
 * opcode.
 */
static InstructionFlags gOpcodeFlagsTable[kNumDalvikInstructions] = {
    // BEGIN(libdex-flags); GENERATED AUTOMATICALLY BY opcode-gen
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanReturn,
    kInstrCanReturn,
    kInstrCanReturn,
    kInstrCanReturn,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue,
    kInstrCanThrow,
    kInstrCanBranch,
    kInstrCanBranch,
    kInstrCanBranch,
    kInstrCanContinue|kInstrCanSwitch,
    kInstrCanContinue|kInstrCanSwitch,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue|kInstrCanBranch,
    kInstrCanContinue|kInstrCanBranch,
    kInstrCanContinue|kInstrCanBranch,
    kInstrCanContinue|kInstrCanBranch,
    kInstrCanContinue|kInstrCanBranch,
    kInstrCanContinue|kInstrCanBranch,
    kInstrCanContinue|kInstrCanBranch,
    kInstrCanContinue|kInstrCanBranch,
    kInstrCanContinue|kInstrCanBranch,
    kInstrCanContinue|kInstrCanBranch,
    kInstrCanContinue|kInstrCanBranch,
    kInstrCanContinue|kInstrCanBranch,
    0,
    0,
    0,
    0,
    0,
    0,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
    kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
    kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
    kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
    kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
    0,
    kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
    kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
    kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
    kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
    kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
    0,
    0,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    0,
    kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue,
    kInstrCanReturn,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
    kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
    kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
    kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    kInstrCanContinue|kInstrCanThrow,
    0,
    // END(libdex-flags)
};

/*
 * Table that maps each opcode to the instruction format associated
 * that opcode.
 */
static InstructionFormat gOpcodeFormatTable[kNumDalvikInstructions] = {
    // BEGIN(libdex-formats); GENERATED AUTOMATICALLY BY opcode-gen
    kFmt10x,  kFmt12x,  kFmt22x,  kFmt32x,  kFmt12x,  kFmt22x,  kFmt32x,
    kFmt12x,  kFmt22x,  kFmt32x,  kFmt11x,  kFmt11x,  kFmt11x,  kFmt11x,
    kFmt10x,  kFmt11x,  kFmt11x,  kFmt11x,  kFmt11n,  kFmt21s,  kFmt31i,
    kFmt21h,  kFmt21s,  kFmt31i,  kFmt51l,  kFmt21h,  kFmt21c,  kFmt31c,
    kFmt21c,  kFmt11x,  kFmt11x,  kFmt21c,  kFmt22c,  kFmt12x,  kFmt21c,
    kFmt22c,  kFmt35c,  kFmt3rc,  kFmt31t,  kFmt11x,  kFmt10t,  kFmt20t,
    kFmt30t,  kFmt31t,  kFmt31t,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,
    kFmt23x,  kFmt22t,  kFmt22t,  kFmt22t,  kFmt22t,  kFmt22t,  kFmt22t,
    kFmt21t,  kFmt21t,  kFmt21t,  kFmt21t,  kFmt21t,  kFmt21t,  kFmt00x,
    kFmt00x,  kFmt00x,  kFmt00x,  kFmt00x,  kFmt00x,  kFmt23x,  kFmt23x,
    kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,
    kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt22c,  kFmt22c,
    kFmt22c,  kFmt22c,  kFmt22c,  kFmt22c,  kFmt22c,  kFmt22c,  kFmt22c,
    kFmt22c,  kFmt22c,  kFmt22c,  kFmt22c,  kFmt22c,  kFmt21c,  kFmt21c,
    kFmt21c,  kFmt21c,  kFmt21c,  kFmt21c,  kFmt21c,  kFmt21c,  kFmt21c,
    kFmt21c,  kFmt21c,  kFmt21c,  kFmt21c,  kFmt21c,  kFmt35c,  kFmt35c,
    kFmt35c,  kFmt35c,  kFmt35c,  kFmt00x,  kFmt3rc,  kFmt3rc,  kFmt3rc,
    kFmt3rc,  kFmt3rc,  kFmt00x,  kFmt00x,  kFmt12x,  kFmt12x,  kFmt12x,
    kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,
    kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,
    kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt23x,  kFmt23x,  kFmt23x,
    kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,
    kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,
    kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,
    kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,
    kFmt23x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,
    kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,
    kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,
    kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,
    kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt22s,  kFmt22s,
    kFmt22s,  kFmt22s,  kFmt22s,  kFmt22s,  kFmt22s,  kFmt22s,  kFmt22b,
    kFmt22b,  kFmt22b,  kFmt22b,  kFmt22b,  kFmt22b,  kFmt22b,  kFmt22b,
    kFmt22b,  kFmt22b,  kFmt22b,  kFmt22c,  kFmt22c,  kFmt21c,  kFmt21c,
    kFmt22c,  kFmt22c,  kFmt22c,  kFmt21c,  kFmt21c,  kFmt00x,  kFmt20bc,
    kFmt35mi, kFmt3rmi, kFmt35c,  kFmt10x,  kFmt22cs, kFmt22cs, kFmt22cs,
    kFmt22cs, kFmt22cs, kFmt22cs, kFmt35ms, kFmt3rms, kFmt35ms, kFmt3rms,
    kFmt22c,  kFmt21c,  kFmt21c,  kFmt00x,
    // END(libdex-formats)
};

/*
 * Table that maps each opcode to the index type implied by that
 * opcode.
 */
static InstructionIndexType gOpcodeIndexTypeTable[kNumDalvikInstructions] = {
    // BEGIN(libdex-index-types); GENERATED AUTOMATICALLY BY opcode-gen
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexStringRef,
    kIndexStringRef,    kIndexTypeRef,      kIndexNone,
    kIndexNone,         kIndexTypeRef,      kIndexTypeRef,
    kIndexNone,         kIndexTypeRef,      kIndexTypeRef,
    kIndexTypeRef,      kIndexTypeRef,      kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexUnknown,
    kIndexUnknown,      kIndexUnknown,      kIndexUnknown,
    kIndexUnknown,      kIndexUnknown,      kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexFieldRef,     kIndexFieldRef,
    kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
    kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
    kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
    kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
    kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
    kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
    kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
    kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
    kIndexFieldRef,     kIndexFieldRef,     kIndexMethodRef,
    kIndexMethodRef,    kIndexMethodRef,    kIndexMethodRef,
    kIndexMethodRef,    kIndexUnknown,      kIndexMethodRef,
    kIndexMethodRef,    kIndexMethodRef,    kIndexMethodRef,
    kIndexMethodRef,    kIndexUnknown,      kIndexUnknown,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexNone,
    kIndexNone,         kIndexNone,         kIndexFieldRef,
    kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
    kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
    kIndexFieldRef,     kIndexFieldRef,     kIndexUnknown,
    kIndexVaries,       kIndexInlineMethod, kIndexInlineMethod,
    kIndexMethodRef,    kIndexNone,         kIndexFieldOffset,
    kIndexFieldOffset,  kIndexFieldOffset,  kIndexFieldOffset,
    kIndexFieldOffset,  kIndexFieldOffset,  kIndexVtableOffset,
    kIndexVtableOffset, kIndexVtableOffset, kIndexVtableOffset,
    kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
    kIndexUnknown,
    // END(libdex-index-types)
};

/*
 * Global InstructionInfoTables struct.
 */
InstructionInfoTables gDexOpcodeInfo = {
    gOpcodeFormatTable,
    gOpcodeIndexTypeTable,
    gOpcodeFlagsTable,
    gOpcodeWidthTable
};

/*
 * Handy macros for helping decode instructions.
 */
#define FETCH(_offset)      (insns[(_offset)])
#define FETCH_u4(_offset)   (fetch_u4_impl((_offset), insns))
#define INST_A(_inst)       (((u2)(_inst) >> 8) & 0x0f)
#define INST_B(_inst)       ((u2)(_inst) >> 12)
#define INST_AA(_inst)      ((_inst) >> 8)

/* Helper for FETCH_u4, above. */
static inline u4 fetch_u4_impl(u4 offset, const u2* insns) {
    return insns[offset] | ((u4) insns[offset+1] << 16);
}

/*
 * Decode the instruction pointed to by "insns".
 *
 * Fills out the pieces of "pDec" that are affected by the current
 * instruction.  Does not touch anything else.
 */
void dexDecodeInstruction(const u2* insns, DecodedInstruction* pDec)
{
    u2 inst = *insns;
    OpCode opCode = dexOpCodeFromCodeUnit(inst);
    InstructionFormat format = dexGetInstrFormat(opCode);

    pDec->opCode = opCode;
    pDec->indexType = dexGetInstrIndexType(opCode);

    switch (format) {
    case kFmt10x:       // op
        /* nothing to do; copy the AA bits out for the verifier */
        pDec->vA = INST_AA(inst);
        break;
    case kFmt12x:       // op vA, vB
        pDec->vA = INST_A(inst);
        pDec->vB = INST_B(inst);
        break;
    case kFmt11n:       // op vA, #+B
        pDec->vA = INST_A(inst);
        pDec->vB = (s4) (INST_B(inst) << 28) >> 28; // sign extend 4-bit value
        break;
    case kFmt11x:       // op vAA
        pDec->vA = INST_AA(inst);
        break;
    case kFmt10t:       // op +AA
        pDec->vA = (s1) INST_AA(inst);              // sign-extend 8-bit value
        break;
    case kFmt20t:       // op +AAAA
        pDec->vA = (s2) FETCH(1);                   // sign-extend 16-bit value
        break;
    case kFmt20bc:      // [opt] op AA, thing@BBBB
    case kFmt21c:       // op vAA, thing@BBBB
    case kFmt22x:       // op vAA, vBBBB
        pDec->vA = INST_AA(inst);
        pDec->vB = FETCH(1);
        break;
    case kFmt21s:       // op vAA, #+BBBB
    case kFmt21t:       // op vAA, +BBBB
        pDec->vA = INST_AA(inst);
        pDec->vB = (s2) FETCH(1);                   // sign-extend 16-bit value
        break;
    case kFmt21h:       // op vAA, #+BBBB0000[00000000]
        pDec->vA = INST_AA(inst);
        /*
         * The value should be treated as right-zero-extended, but we don't
         * actually do that here. Among other things, we don't know if it's
         * the top bits of a 32- or 64-bit value.
         */
        pDec->vB = FETCH(1);
        break;
    case kFmt23x:       // op vAA, vBB, vCC
        pDec->vA = INST_AA(inst);
        pDec->vB = FETCH(1) & 0xff;
        pDec->vC = FETCH(1) >> 8;
        break;
    case kFmt22b:       // op vAA, vBB, #+CC
        pDec->vA = INST_AA(inst);
        pDec->vB = FETCH(1) & 0xff;
        pDec->vC = (s1) (FETCH(1) >> 8);            // sign-extend 8-bit value
        break;
    case kFmt22s:       // op vA, vB, #+CCCC
    case kFmt22t:       // op vA, vB, +CCCC
        pDec->vA = INST_A(inst);
        pDec->vB = INST_B(inst);
        pDec->vC = (s2) FETCH(1);                   // sign-extend 16-bit value
        break;
    case kFmt22c:       // op vA, vB, thing@CCCC
    case kFmt22cs:      // [opt] op vA, vB, field offset CCCC
        pDec->vA = INST_A(inst);
        pDec->vB = INST_B(inst);
        pDec->vC = FETCH(1);
        break;
    case kFmt30t:       // op +AAAAAAAA
        pDec->vA = FETCH_u4(1);                     // signed 32-bit value
        break;
    case kFmt31t:       // op vAA, +BBBBBBBB
    case kFmt31c:       // op vAA, string@BBBBBBBB
        pDec->vA = INST_AA(inst);
        pDec->vB = FETCH_u4(1);                     // 32-bit value
        break;
    case kFmt32x:       // op vAAAA, vBBBB
        pDec->vA = FETCH(1);
        pDec->vB = FETCH(2);
        break;
    case kFmt31i:       // op vAA, #+BBBBBBBB
        pDec->vA = INST_AA(inst);
        pDec->vB = FETCH_u4(1);                     // signed 32-bit value
        break;
    case kFmt35c:       // op {vC, vD, vE, vF, vG}, thing@BBBB
    case kFmt35ms:      // [opt] invoke-virtual+super
    case kFmt35mi:      // [opt] inline invoke
        {
            /*
             * Note that the fields mentioned in the spec don't appear in
             * their "usual" positions here compared to most formats. This
             * was done so that the field names for the argument count and
             * reference index match between this format and the corresponding
             * range formats (3rc and friends).
             *
             * Bottom line: The argument count is always in vA, and the
             * method constant (or equivalent) is always in vB.
             */
            u2 regList;
            int i, count;

            pDec->vA = INST_B(inst); // This is labeled A in the spec.
            pDec->vB = FETCH(1);
            regList = FETCH(2);

            count = pDec->vA;

            /*
             * Copy the argument registers into the arg[] array, and
             * also copy the first argument (if any) into vC. (The
             * DecodedInstruction structure doesn't have separate
             * fields for {vD, vE, vF, vG}, so there's no need to make
             * copies of those.) Note that cases 5..2 fall through.
             */
            switch (count) {
            case 5: {
                if (format == kFmt35mi) {
                    /* A fifth arg is verboten for inline invokes. */
                    LOGW("Invalid arg count in 35mi (5)\n");
                    goto bail;
                }
                /*
                 * Per note at the top of this format decoder, the
                 * fifth argument comes from the A field in the
                 * instruction, but it's labeled G in the spec.
                 */
                pDec->arg[4] = INST_A(inst);
            }
            case 4: pDec->arg[3] = (regList >> 12) & 0x0f;
            case 3: pDec->arg[2] = (regList >> 8) & 0x0f;
            case 2: pDec->arg[1] = (regList >> 4) & 0x0f;
            case 1: pDec->vC = pDec->arg[0] = regList & 0x0f; break;
            case 0: break; // Valid, but no need to do anything.
            default:
                LOGW("Invalid arg count in 35c/35ms/35mi (%d)\n", count);
                goto bail;
            }
        }
        break;
    case kFmt3rc:       // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
    case kFmt3rms:      // [opt] invoke-virtual+super/range
    case kFmt3rmi:      // [opt] execute-inline/range
        pDec->vA = INST_AA(inst);
        pDec->vB = FETCH(1);
        pDec->vC = FETCH(2);
        break;
    case kFmt51l:       // op vAA, #+BBBBBBBBBBBBBBBB
        pDec->vA = INST_AA(inst);
        pDec->vB_wide = FETCH_u4(1) | ((u8) FETCH_u4(3) << 32);
        break;
    case kFmt33x:       // exop vAA, vBB, vCCCC
        pDec->vA = FETCH(1) & 0xff;
        pDec->vB = FETCH(1) >> 8;
        pDec->vC = FETCH(2);
        break;
    case kFmt32s:       // exop vAA, vBB, #+CCCC
        pDec->vA = FETCH(1) & 0xff;
        pDec->vB = FETCH(1) >> 8;
        pDec->vC = (s2) FETCH(2);                   // sign-extend 16-bit value
        break;
    case kFmt41c:       // exop vAAAA, thing@BBBBBBBB
        /*
         * The order of fields for this format in the spec is {B, A},
         * to match formats 21c and 31c.
         */
        pDec->vB = FETCH_u4(1);                     // 32-bit value
        pDec->vA = FETCH(3);
        break;
    case kFmt52c:       // exop vAAAA, vBBBB, thing@CCCCCCCC
        /*
         * The order of fields for this format in the spec is {C, A, B},
         * to match formats 22c and 22cs.
         */
        pDec->vC = FETCH_u4(1);                     // 32-bit value
        pDec->vA = FETCH(3);
        pDec->vB = FETCH(4);
        break;
    case kFmt5rc:       // exop {vCCCC .. v(CCCC+AAAA-1)}, meth@BBBBBBBB
        /*
         * The order of fields for this format in the spec is {B, A, C},
         * to match formats 3rc and friends.
         */
        pDec->vB = FETCH_u4(1);                     // 32-bit value
        pDec->vA = FETCH(3);
        pDec->vC = FETCH(4);
        break;
    default:
        LOGW("Can't decode unexpected format %d (op=%d)\n", format, opCode);
        assert(false);
        break;
    }

bail:
    ;
}

/*
 * Return the width of the specified instruction, or 0 if not defined.  Also
 * works for special OP_NOP entries, including switch statement data tables
 * and array data.
 */
size_t dexGetInstrOrTableWidth(const u2* insns)
{
    size_t width;

    if (*insns == kPackedSwitchSignature) {
        width = 4 + insns[1] * 2;
    } else if (*insns == kSparseSwitchSignature) {
        width = 2 + insns[1] * 4;
    } else if (*insns == kArrayDataSignature) {
        u2 elemWidth = insns[1];
        u4 len = insns[2] | (((u4)insns[3]) << 16);
        // The plus 1 is to round up for odd size and width.
        width = 4 + (elemWidth * len + 1) / 2;
    } else {
        width = dexGetInstrWidth(dexOpCodeFromCodeUnit(insns[0]));
    }
    return width;
}