• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * Dalvik instruction utility functions.
19  *
20  * IMPORTANT NOTE: Much of the contents of this file are generated
21  * automatically by the opcode-gen tool. Any edits to the generated
22  * sections will get wiped out the next time the tool is run.
23  */
24 
25 #include "InstrUtils.h"
26 #include <stdlib.h>
27 
28 /*
29  * Table that maps each opcode to the full width of instructions that
30  * use that opcode, in (16-bit) code units. Unimplemented opcodes as
31  * well as the "breakpoint" opcode have a width of zero.
32  */
33 static InstructionWidth gInstructionWidthTable[kNumPackedOpcodes] = {
34     // BEGIN(libdex-widths); GENERATED AUTOMATICALLY BY opcode-gen
35     1, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 1, 1, 1, 1, 1,
36     1, 1, 1, 2, 3, 2, 2, 3, 5, 2, 2, 3, 2, 1, 1, 2,
37     2, 1, 2, 2, 3, 3, 3, 1, 1, 2, 3, 3, 3, 2, 2, 2,
38     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0,
39     0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
40     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
41     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3,
42     3, 3, 3, 0, 3, 3, 3, 3, 3, 0, 0, 1, 1, 1, 1, 1,
43     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
44     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
45     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
46     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
47     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
48     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
49     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 3, 3,
50     3, 1, 2, 0, 0, 0, 0, 0, 0, 0, 4, 4, 3, 3, 2, 2,
51     // END(libdex-widths)
52 };
53 
54 /*
55  * Table that maps each opcode to the flags associated with that
56  * opcode.
57  */
58 static u1 gOpcodeFlagsTable[kNumPackedOpcodes] = {
59     // BEGIN(libdex-flags); GENERATED AUTOMATICALLY BY opcode-gen
60     kInstrCanContinue,
61     kInstrCanContinue,
62     kInstrCanContinue,
63     kInstrCanContinue,
64     kInstrCanContinue,
65     kInstrCanContinue,
66     kInstrCanContinue,
67     kInstrCanContinue,
68     kInstrCanContinue,
69     kInstrCanContinue,
70     kInstrCanContinue,
71     kInstrCanContinue,
72     kInstrCanContinue,
73     kInstrCanContinue,
74     kInstrCanReturn,
75     kInstrCanReturn,
76     kInstrCanReturn,
77     kInstrCanReturn,
78     kInstrCanContinue,
79     kInstrCanContinue,
80     kInstrCanContinue,
81     kInstrCanContinue,
82     kInstrCanContinue,
83     kInstrCanContinue,
84     kInstrCanContinue,
85     kInstrCanContinue,
86     kInstrCanContinue|kInstrCanThrow,
87     kInstrCanContinue|kInstrCanThrow,
88     kInstrCanContinue|kInstrCanThrow,
89     kInstrCanContinue|kInstrCanThrow,
90     kInstrCanContinue|kInstrCanThrow,
91     kInstrCanContinue|kInstrCanThrow,
92     kInstrCanContinue|kInstrCanThrow,
93     kInstrCanContinue|kInstrCanThrow,
94     kInstrCanContinue|kInstrCanThrow,
95     kInstrCanContinue|kInstrCanThrow,
96     kInstrCanContinue|kInstrCanThrow,
97     kInstrCanContinue|kInstrCanThrow,
98     kInstrCanContinue,
99     kInstrCanThrow,
100     kInstrCanBranch,
101     kInstrCanBranch,
102     kInstrCanBranch,
103     kInstrCanContinue|kInstrCanSwitch,
104     kInstrCanContinue|kInstrCanSwitch,
105     kInstrCanContinue,
106     kInstrCanContinue,
107     kInstrCanContinue,
108     kInstrCanContinue,
109     kInstrCanContinue,
110     kInstrCanContinue|kInstrCanBranch,
111     kInstrCanContinue|kInstrCanBranch,
112     kInstrCanContinue|kInstrCanBranch,
113     kInstrCanContinue|kInstrCanBranch,
114     kInstrCanContinue|kInstrCanBranch,
115     kInstrCanContinue|kInstrCanBranch,
116     kInstrCanContinue|kInstrCanBranch,
117     kInstrCanContinue|kInstrCanBranch,
118     kInstrCanContinue|kInstrCanBranch,
119     kInstrCanContinue|kInstrCanBranch,
120     kInstrCanContinue|kInstrCanBranch,
121     kInstrCanContinue|kInstrCanBranch,
122     0,
123     0,
124     0,
125     0,
126     0,
127     0,
128     kInstrCanContinue|kInstrCanThrow,
129     kInstrCanContinue|kInstrCanThrow,
130     kInstrCanContinue|kInstrCanThrow,
131     kInstrCanContinue|kInstrCanThrow,
132     kInstrCanContinue|kInstrCanThrow,
133     kInstrCanContinue|kInstrCanThrow,
134     kInstrCanContinue|kInstrCanThrow,
135     kInstrCanContinue|kInstrCanThrow,
136     kInstrCanContinue|kInstrCanThrow,
137     kInstrCanContinue|kInstrCanThrow,
138     kInstrCanContinue|kInstrCanThrow,
139     kInstrCanContinue|kInstrCanThrow,
140     kInstrCanContinue|kInstrCanThrow,
141     kInstrCanContinue|kInstrCanThrow,
142     kInstrCanContinue|kInstrCanThrow,
143     kInstrCanContinue|kInstrCanThrow,
144     kInstrCanContinue|kInstrCanThrow,
145     kInstrCanContinue|kInstrCanThrow,
146     kInstrCanContinue|kInstrCanThrow,
147     kInstrCanContinue|kInstrCanThrow,
148     kInstrCanContinue|kInstrCanThrow,
149     kInstrCanContinue|kInstrCanThrow,
150     kInstrCanContinue|kInstrCanThrow,
151     kInstrCanContinue|kInstrCanThrow,
152     kInstrCanContinue|kInstrCanThrow,
153     kInstrCanContinue|kInstrCanThrow,
154     kInstrCanContinue|kInstrCanThrow,
155     kInstrCanContinue|kInstrCanThrow,
156     kInstrCanContinue|kInstrCanThrow,
157     kInstrCanContinue|kInstrCanThrow,
158     kInstrCanContinue|kInstrCanThrow,
159     kInstrCanContinue|kInstrCanThrow,
160     kInstrCanContinue|kInstrCanThrow,
161     kInstrCanContinue|kInstrCanThrow,
162     kInstrCanContinue|kInstrCanThrow,
163     kInstrCanContinue|kInstrCanThrow,
164     kInstrCanContinue|kInstrCanThrow,
165     kInstrCanContinue|kInstrCanThrow,
166     kInstrCanContinue|kInstrCanThrow,
167     kInstrCanContinue|kInstrCanThrow,
168     kInstrCanContinue|kInstrCanThrow,
169     kInstrCanContinue|kInstrCanThrow,
170     kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
171     kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
172     kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
173     kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
174     kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
175     0,
176     kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
177     kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
178     kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
179     kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
180     kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
181     0,
182     0,
183     kInstrCanContinue,
184     kInstrCanContinue,
185     kInstrCanContinue,
186     kInstrCanContinue,
187     kInstrCanContinue,
188     kInstrCanContinue,
189     kInstrCanContinue,
190     kInstrCanContinue,
191     kInstrCanContinue,
192     kInstrCanContinue,
193     kInstrCanContinue,
194     kInstrCanContinue,
195     kInstrCanContinue,
196     kInstrCanContinue,
197     kInstrCanContinue,
198     kInstrCanContinue,
199     kInstrCanContinue,
200     kInstrCanContinue,
201     kInstrCanContinue,
202     kInstrCanContinue,
203     kInstrCanContinue,
204     kInstrCanContinue,
205     kInstrCanContinue,
206     kInstrCanContinue,
207     kInstrCanContinue|kInstrCanThrow,
208     kInstrCanContinue|kInstrCanThrow,
209     kInstrCanContinue,
210     kInstrCanContinue,
211     kInstrCanContinue,
212     kInstrCanContinue,
213     kInstrCanContinue,
214     kInstrCanContinue,
215     kInstrCanContinue,
216     kInstrCanContinue,
217     kInstrCanContinue,
218     kInstrCanContinue|kInstrCanThrow,
219     kInstrCanContinue|kInstrCanThrow,
220     kInstrCanContinue,
221     kInstrCanContinue,
222     kInstrCanContinue,
223     kInstrCanContinue,
224     kInstrCanContinue,
225     kInstrCanContinue,
226     kInstrCanContinue,
227     kInstrCanContinue,
228     kInstrCanContinue,
229     kInstrCanContinue,
230     kInstrCanContinue,
231     kInstrCanContinue,
232     kInstrCanContinue,
233     kInstrCanContinue,
234     kInstrCanContinue,
235     kInstrCanContinue,
236     kInstrCanContinue,
237     kInstrCanContinue,
238     kInstrCanContinue,
239     kInstrCanContinue|kInstrCanThrow,
240     kInstrCanContinue|kInstrCanThrow,
241     kInstrCanContinue,
242     kInstrCanContinue,
243     kInstrCanContinue,
244     kInstrCanContinue,
245     kInstrCanContinue,
246     kInstrCanContinue,
247     kInstrCanContinue,
248     kInstrCanContinue,
249     kInstrCanContinue,
250     kInstrCanContinue|kInstrCanThrow,
251     kInstrCanContinue|kInstrCanThrow,
252     kInstrCanContinue,
253     kInstrCanContinue,
254     kInstrCanContinue,
255     kInstrCanContinue,
256     kInstrCanContinue,
257     kInstrCanContinue,
258     kInstrCanContinue,
259     kInstrCanContinue,
260     kInstrCanContinue,
261     kInstrCanContinue,
262     kInstrCanContinue,
263     kInstrCanContinue,
264     kInstrCanContinue,
265     kInstrCanContinue,
266     kInstrCanContinue,
267     kInstrCanContinue,
268     kInstrCanContinue,
269     kInstrCanContinue,
270     kInstrCanContinue,
271     kInstrCanContinue|kInstrCanThrow,
272     kInstrCanContinue|kInstrCanThrow,
273     kInstrCanContinue,
274     kInstrCanContinue,
275     kInstrCanContinue,
276     kInstrCanContinue,
277     kInstrCanContinue,
278     kInstrCanContinue,
279     kInstrCanContinue|kInstrCanThrow,
280     kInstrCanContinue|kInstrCanThrow,
281     kInstrCanContinue,
282     kInstrCanContinue,
283     kInstrCanContinue,
284     kInstrCanContinue,
285     kInstrCanContinue,
286     kInstrCanContinue,
287     kInstrCanContinue|kInstrCanThrow,
288     kInstrCanContinue|kInstrCanThrow,
289     kInstrCanContinue|kInstrCanThrow,
290     kInstrCanContinue|kInstrCanThrow,
291     kInstrCanContinue|kInstrCanThrow,
292     kInstrCanContinue|kInstrCanThrow,
293     kInstrCanContinue|kInstrCanThrow,
294     kInstrCanContinue|kInstrCanThrow,
295     kInstrCanContinue|kInstrCanThrow,
296     0,
297     kInstrCanThrow,
298     kInstrCanContinue|kInstrCanThrow,
299     kInstrCanContinue|kInstrCanThrow,
300     kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
301     kInstrCanReturn,
302     kInstrCanContinue|kInstrCanThrow,
303     0,
304     0,
305     0,
306     0,
307     0,
308     0,
309     0,
310     kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
311     kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
312     kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
313     kInstrCanContinue|kInstrCanThrow|kInstrInvoke,
314     kInstrCanContinue|kInstrCanThrow,
315     kInstrCanContinue|kInstrCanThrow,
316     // END(libdex-flags)
317 };
318 
319 /*
320  * Table that maps each opcode to the instruction format associated
321  * that opcode.
322  */
323 static u1 gInstructionFormatTable[kNumPackedOpcodes] = {
324     // BEGIN(libdex-formats); GENERATED AUTOMATICALLY BY opcode-gen
325     kFmt10x,  kFmt12x,  kFmt22x,  kFmt32x,  kFmt12x,  kFmt22x,  kFmt32x,
326     kFmt12x,  kFmt22x,  kFmt32x,  kFmt11x,  kFmt11x,  kFmt11x,  kFmt11x,
327     kFmt10x,  kFmt11x,  kFmt11x,  kFmt11x,  kFmt11n,  kFmt21s,  kFmt31i,
328     kFmt21h,  kFmt21s,  kFmt31i,  kFmt51l,  kFmt21h,  kFmt21c,  kFmt31c,
329     kFmt21c,  kFmt11x,  kFmt11x,  kFmt21c,  kFmt22c,  kFmt12x,  kFmt21c,
330     kFmt22c,  kFmt35c,  kFmt3rc,  kFmt31t,  kFmt11x,  kFmt10t,  kFmt20t,
331     kFmt30t,  kFmt31t,  kFmt31t,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,
332     kFmt23x,  kFmt22t,  kFmt22t,  kFmt22t,  kFmt22t,  kFmt22t,  kFmt22t,
333     kFmt21t,  kFmt21t,  kFmt21t,  kFmt21t,  kFmt21t,  kFmt21t,  kFmt00x,
334     kFmt00x,  kFmt00x,  kFmt00x,  kFmt00x,  kFmt00x,  kFmt23x,  kFmt23x,
335     kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,
336     kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt22c,  kFmt22c,
337     kFmt22c,  kFmt22c,  kFmt22c,  kFmt22c,  kFmt22c,  kFmt22c,  kFmt22c,
338     kFmt22c,  kFmt22c,  kFmt22c,  kFmt22c,  kFmt22c,  kFmt21c,  kFmt21c,
339     kFmt21c,  kFmt21c,  kFmt21c,  kFmt21c,  kFmt21c,  kFmt21c,  kFmt21c,
340     kFmt21c,  kFmt21c,  kFmt21c,  kFmt21c,  kFmt21c,  kFmt35c,  kFmt35c,
341     kFmt35c,  kFmt35c,  kFmt35c,  kFmt00x,  kFmt3rc,  kFmt3rc,  kFmt3rc,
342     kFmt3rc,  kFmt3rc,  kFmt00x,  kFmt00x,  kFmt12x,  kFmt12x,  kFmt12x,
343     kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,
344     kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,
345     kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt23x,  kFmt23x,  kFmt23x,
346     kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,
347     kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,
348     kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,
349     kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,  kFmt23x,
350     kFmt23x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,
351     kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,
352     kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,
353     kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,
354     kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt12x,  kFmt22s,  kFmt22s,
355     kFmt22s,  kFmt22s,  kFmt22s,  kFmt22s,  kFmt22s,  kFmt22s,  kFmt22b,
356     kFmt22b,  kFmt22b,  kFmt22b,  kFmt22b,  kFmt22b,  kFmt22b,  kFmt22b,
357     kFmt22b,  kFmt22b,  kFmt22b,  kFmt22c,  kFmt22c,  kFmt21c,  kFmt21c,
358     kFmt22c,  kFmt22c,  kFmt22c,  kFmt21c,  kFmt21c,  kFmt00x,  kFmt20bc,
359     kFmt35mi, kFmt3rmi, kFmt35c,  kFmt10x,  kFmt22cs, kFmt00x,  kFmt00x,
360     kFmt00x,  kFmt00x,  kFmt00x,  kFmt00x,  kFmt00x,  kFmt45cc, kFmt4rcc,
361     kFmt35c,  kFmt3rc,  kFmt21c,  kFmt21c,
362     // END(libdex-formats)
363 };
364 
365 /*
366  * Table that maps each opcode to the index type implied by that
367  * opcode.
368  */
369 static u1 gInstructionIndexTypeTable[kNumPackedOpcodes] = {
370     // BEGIN(libdex-index-types); GENERATED AUTOMATICALLY BY opcode-gen
371     kIndexNone,         kIndexNone,         kIndexNone,
372     kIndexNone,         kIndexNone,         kIndexNone,
373     kIndexNone,         kIndexNone,         kIndexNone,
374     kIndexNone,         kIndexNone,         kIndexNone,
375     kIndexNone,         kIndexNone,         kIndexNone,
376     kIndexNone,         kIndexNone,         kIndexNone,
377     kIndexNone,         kIndexNone,         kIndexNone,
378     kIndexNone,         kIndexNone,         kIndexNone,
379     kIndexNone,         kIndexNone,         kIndexStringRef,
380     kIndexStringRef,    kIndexTypeRef,      kIndexNone,
381     kIndexNone,         kIndexTypeRef,      kIndexTypeRef,
382     kIndexNone,         kIndexTypeRef,      kIndexTypeRef,
383     kIndexTypeRef,      kIndexTypeRef,      kIndexNone,
384     kIndexNone,         kIndexNone,         kIndexNone,
385     kIndexNone,         kIndexNone,         kIndexNone,
386     kIndexNone,         kIndexNone,         kIndexNone,
387     kIndexNone,         kIndexNone,         kIndexNone,
388     kIndexNone,         kIndexNone,         kIndexNone,
389     kIndexNone,         kIndexNone,         kIndexNone,
390     kIndexNone,         kIndexNone,         kIndexNone,
391     kIndexNone,         kIndexNone,         kIndexUnknown,
392     kIndexUnknown,      kIndexUnknown,      kIndexUnknown,
393     kIndexUnknown,      kIndexUnknown,      kIndexNone,
394     kIndexNone,         kIndexNone,         kIndexNone,
395     kIndexNone,         kIndexNone,         kIndexNone,
396     kIndexNone,         kIndexNone,         kIndexNone,
397     kIndexNone,         kIndexNone,         kIndexNone,
398     kIndexNone,         kIndexFieldRef,     kIndexFieldRef,
399     kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
400     kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
401     kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
402     kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
403     kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
404     kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
405     kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
406     kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
407     kIndexFieldRef,     kIndexFieldRef,     kIndexMethodRef,
408     kIndexMethodRef,    kIndexMethodRef,    kIndexMethodRef,
409     kIndexMethodRef,    kIndexUnknown,      kIndexMethodRef,
410     kIndexMethodRef,    kIndexMethodRef,    kIndexMethodRef,
411     kIndexMethodRef,    kIndexUnknown,      kIndexUnknown,
412     kIndexNone,         kIndexNone,         kIndexNone,
413     kIndexNone,         kIndexNone,         kIndexNone,
414     kIndexNone,         kIndexNone,         kIndexNone,
415     kIndexNone,         kIndexNone,         kIndexNone,
416     kIndexNone,         kIndexNone,         kIndexNone,
417     kIndexNone,         kIndexNone,         kIndexNone,
418     kIndexNone,         kIndexNone,         kIndexNone,
419     kIndexNone,         kIndexNone,         kIndexNone,
420     kIndexNone,         kIndexNone,         kIndexNone,
421     kIndexNone,         kIndexNone,         kIndexNone,
422     kIndexNone,         kIndexNone,         kIndexNone,
423     kIndexNone,         kIndexNone,         kIndexNone,
424     kIndexNone,         kIndexNone,         kIndexNone,
425     kIndexNone,         kIndexNone,         kIndexNone,
426     kIndexNone,         kIndexNone,         kIndexNone,
427     kIndexNone,         kIndexNone,         kIndexNone,
428     kIndexNone,         kIndexNone,         kIndexNone,
429     kIndexNone,         kIndexNone,         kIndexNone,
430     kIndexNone,         kIndexNone,         kIndexNone,
431     kIndexNone,         kIndexNone,         kIndexNone,
432     kIndexNone,         kIndexNone,         kIndexNone,
433     kIndexNone,         kIndexNone,         kIndexNone,
434     kIndexNone,         kIndexNone,         kIndexNone,
435     kIndexNone,         kIndexNone,         kIndexNone,
436     kIndexNone,         kIndexNone,         kIndexNone,
437     kIndexNone,         kIndexNone,         kIndexNone,
438     kIndexNone,         kIndexNone,         kIndexNone,
439     kIndexNone,         kIndexNone,         kIndexNone,
440     kIndexNone,         kIndexNone,         kIndexNone,
441     kIndexNone,         kIndexNone,         kIndexNone,
442     kIndexNone,         kIndexNone,         kIndexNone,
443     kIndexNone,         kIndexNone,         kIndexNone,
444     kIndexNone,         kIndexNone,         kIndexNone,
445     kIndexNone,         kIndexNone,         kIndexNone,
446     kIndexNone,         kIndexNone,         kIndexFieldRef,
447     kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
448     kIndexFieldRef,     kIndexFieldRef,     kIndexFieldRef,
449     kIndexFieldRef,     kIndexFieldRef,     kIndexUnknown,
450     kIndexVaries,       kIndexInlineMethod, kIndexInlineMethod,
451     kIndexMethodRef,    kIndexNone,         kIndexFieldOffset,
452     kIndexUnknown,      kIndexUnknown,      kIndexUnknown,
453     kIndexUnknown,      kIndexUnknown,      kIndexUnknown,
454     kIndexUnknown,      kIndexMethodAndProtoRef, kIndexMethodAndProtoRef,
455     kIndexCallSiteRef,  kIndexCallSiteRef,  kIndexMethodHandleRef,
456     kIndexProtoRef,
457     // END(libdex-index-types)
458 };
459 
460 /*
461  * Global InstructionInfoTables struct.
462  */
463 InstructionInfoTables gDexOpcodeInfo = {
464     gInstructionFormatTable,
465     gInstructionIndexTypeTable,
466     gOpcodeFlagsTable,
467     gInstructionWidthTable
468 };
469 
470 /*
471  * Handy macros for helping decode instructions.
472  */
473 #define FETCH(_offset)      (insns[(_offset)])
474 #define FETCH_u4(_offset)   (fetch_u4_impl((_offset), insns))
475 #define INST_A(_inst)       (((u2)(_inst) >> 8) & 0x0f)
476 #define INST_B(_inst)       ((u2)(_inst) >> 12)
477 #define INST_AA(_inst)      ((_inst) >> 8)
478 
479 /* Helper for FETCH_u4, above. */
fetch_u4_impl(u4 offset,const u2 * insns)480 static inline u4 fetch_u4_impl(u4 offset, const u2* insns) {
481     return insns[offset] | ((u4) insns[offset+1] << 16);
482 }
483 
484 /*
485  * Decode the instruction pointed to by "insns".
486  *
487  * Fills out the pieces of "pDec" that are affected by the current
488  * instruction.  Does not touch anything else.
489  */
dexDecodeInstruction(const u2 * insns,DecodedInstruction * pDec)490 void dexDecodeInstruction(const u2* insns, DecodedInstruction* pDec)
491 {
492     u2 inst = *insns;
493     Opcode opcode = dexOpcodeFromCodeUnit(inst);
494     InstructionFormat format = dexGetFormatFromOpcode(opcode);
495 
496     pDec->opcode = opcode;
497     pDec->indexType = dexGetIndexTypeFromOpcode(opcode);
498 
499     switch (format) {
500     case kFmt10x:       // op
501         /* nothing to do; copy the AA bits out for the verifier */
502         pDec->vA = INST_AA(inst);
503         break;
504     case kFmt12x:       // op vA, vB
505         pDec->vA = INST_A(inst);
506         pDec->vB = INST_B(inst);
507         break;
508     case kFmt11n:       // op vA, #+B
509         pDec->vA = INST_A(inst);
510         pDec->vB = (s4) (INST_B(inst) << 28) >> 28; // sign extend 4-bit value
511         break;
512     case kFmt11x:       // op vAA
513         pDec->vA = INST_AA(inst);
514         break;
515     case kFmt10t:       // op +AA
516         pDec->vA = (s1) INST_AA(inst);              // sign-extend 8-bit value
517         break;
518     case kFmt20t:       // op +AAAA
519         pDec->vA = (s2) FETCH(1);                   // sign-extend 16-bit value
520         break;
521     case kFmt20bc:      // [opt] op AA, thing@BBBB
522     case kFmt21c:       // op vAA, thing@BBBB
523     case kFmt22x:       // op vAA, vBBBB
524         pDec->vA = INST_AA(inst);
525         pDec->vB = FETCH(1);
526         break;
527     case kFmt21s:       // op vAA, #+BBBB
528     case kFmt21t:       // op vAA, +BBBB
529         pDec->vA = INST_AA(inst);
530         pDec->vB = (s2) FETCH(1);                   // sign-extend 16-bit value
531         break;
532     case kFmt21h:       // op vAA, #+BBBB0000[00000000]
533         pDec->vA = INST_AA(inst);
534         /*
535          * The value should be treated as right-zero-extended, but we don't
536          * actually do that here. Among other things, we don't know if it's
537          * the top bits of a 32- or 64-bit value.
538          */
539         pDec->vB = FETCH(1);
540         break;
541     case kFmt23x:       // op vAA, vBB, vCC
542         pDec->vA = INST_AA(inst);
543         pDec->vB = FETCH(1) & 0xff;
544         pDec->vC = FETCH(1) >> 8;
545         break;
546     case kFmt22b:       // op vAA, vBB, #+CC
547         pDec->vA = INST_AA(inst);
548         pDec->vB = FETCH(1) & 0xff;
549         pDec->vC = (s1) (FETCH(1) >> 8);            // sign-extend 8-bit value
550         break;
551     case kFmt22s:       // op vA, vB, #+CCCC
552     case kFmt22t:       // op vA, vB, +CCCC
553         pDec->vA = INST_A(inst);
554         pDec->vB = INST_B(inst);
555         pDec->vC = (s2) FETCH(1);                   // sign-extend 16-bit value
556         break;
557     case kFmt22c:       // op vA, vB, thing@CCCC
558     case kFmt22cs:      // [opt] op vA, vB, field offset CCCC
559         pDec->vA = INST_A(inst);
560         pDec->vB = INST_B(inst);
561         pDec->vC = FETCH(1);
562         break;
563     case kFmt30t:       // op +AAAAAAAA
564         pDec->vA = FETCH_u4(1);                     // signed 32-bit value
565         break;
566     case kFmt31t:       // op vAA, +BBBBBBBB
567     case kFmt31c:       // op vAA, string@BBBBBBBB
568         pDec->vA = INST_AA(inst);
569         pDec->vB = FETCH_u4(1);                     // 32-bit value
570         break;
571     case kFmt32x:       // op vAAAA, vBBBB
572         pDec->vA = FETCH(1);
573         pDec->vB = FETCH(2);
574         break;
575     case kFmt31i:       // op vAA, #+BBBBBBBB
576         pDec->vA = INST_AA(inst);
577         pDec->vB = FETCH_u4(1);                     // signed 32-bit value
578         break;
579     case kFmt35c:       // op {vC, vD, vE, vF, vG}, thing@BBBB
580     case kFmt35ms:      // [opt] invoke-virtual+super
581     case kFmt35mi:      // [opt] inline invoke
582         {
583             /*
584              * Note that the fields mentioned in the spec don't appear in
585              * their "usual" positions here compared to most formats. This
586              * was done so that the field names for the argument count and
587              * reference index match between this format and the corresponding
588              * range formats (3rc and friends).
589              *
590              * Bottom line: The argument count is always in vA, and the
591              * method constant (or equivalent) is always in vB.
592              */
593             u2 regList;
594             int count;
595 
596             pDec->vA = INST_B(inst); // This is labeled A in the spec.
597             pDec->vB = FETCH(1);
598             regList = FETCH(2);
599 
600             count = pDec->vA;
601 
602             /*
603              * Copy the argument registers into the arg[] array, and
604              * also copy the first argument (if any) into vC. (The
605              * DecodedInstruction structure doesn't have separate
606              * fields for {vD, vE, vF, vG}, so there's no need to make
607              * copies of those.) Note that cases 5..2 fall through.
608              */
609             switch (count) {
610             case 5: {
611                 if (format == kFmt35mi) {
612                     /* A fifth arg is verboten for inline invokes. */
613                     ALOGW("Invalid arg count in 35mi (5)");
614                     goto bail;
615                 }
616                 /*
617                  * Per note at the top of this format decoder, the
618                  * fifth argument comes from the A field in the
619                  * instruction, but it's labeled G in the spec.
620                  */
621                 pDec->arg[4] = INST_A(inst);
622                 FALLTHROUGH_INTENDED;
623             }
624             case 4: pDec->arg[3] = (regList >> 12) & 0x0f; FALLTHROUGH_INTENDED;
625             case 3: pDec->arg[2] = (regList >> 8) & 0x0f; FALLTHROUGH_INTENDED;
626             case 2: pDec->arg[1] = (regList >> 4) & 0x0f; FALLTHROUGH_INTENDED;
627             case 1: pDec->vC = pDec->arg[0] = regList & 0x0f; break;
628             case 0: break; // Valid, but no need to do anything.
629             default:
630                 ALOGW("Invalid arg count in 35c/35ms/35mi (%d)", count);
631                 goto bail;
632             }
633         }
634         break;
635     case kFmt3rc:       // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
636     case kFmt3rms:      // [opt] invoke-virtual+super/range
637     case kFmt3rmi:      // [opt] execute-inline/range
638         pDec->vA = INST_AA(inst);
639         pDec->vB = FETCH(1);
640         pDec->vC = FETCH(2);
641         break;
642     case kFmt51l:       // op vAA, #+BBBBBBBBBBBBBBBB
643         pDec->vA = INST_AA(inst);
644         pDec->vB_wide = FETCH_u4(1) | ((u8) FETCH_u4(3) << 32);
645         break;
646     case kFmt45cc:
647         {
648             // AG op BBBB FEDC HHHH
649             pDec->vA = INST_B(inst);  // This is labelled A in the spec.
650             pDec->vB = FETCH(1);  // vB meth@BBBB
651             u2 fedc  = FETCH(2);
652             pDec->vC = fedc & 0xf;
653             pDec->arg[0] = (fedc >> 4) & 0xf;  // vD
654             pDec->arg[1] = (fedc >> 8) & 0xf;  // vE
655             pDec->arg[2] = (fedc >> 12);       // vF
656             pDec->arg[3] = INST_A(inst);       // vG
657             pDec->arg[4] = FETCH(3);           // vH proto@HHHH
658         }
659         break;
660     case kFmt4rcc:
661         {
662             // AA op BBBB CCCC HHHH
663             pDec->vA = INST_AA(inst);
664             pDec->vB = FETCH(1);
665             pDec->vC = FETCH(2);
666             pDec->arg[4] = FETCH(3);  // vH proto@HHHH
667         }
668         break;
669     default:
670         ALOGW("Can't decode unexpected format %d (op=%d)", format, opcode);
671         assert(false);
672         break;
673     }
674 
675 bail:
676     ;
677 }
678 
679 /*
680  * Return the width of the specified instruction, or 0 if not defined.  Also
681  * works for special OP_NOP entries, including switch statement data tables
682  * and array data.
683  */
dexGetWidthFromInstruction(const u2 * insns)684 size_t dexGetWidthFromInstruction(const u2* insns)
685 {
686     size_t width;
687 
688     if (*insns == kPackedSwitchSignature) {
689         width = 4 + insns[1] * 2;
690     } else if (*insns == kSparseSwitchSignature) {
691         width = 2 + insns[1] * 4;
692     } else if (*insns == kArrayDataSignature) {
693         u2 elemWidth = insns[1];
694         u4 len = insns[2] | (((u4)insns[3]) << 16);
695         // The plus 1 is to round up for odd size and width.
696         width = 4 + (elemWidth * len + 1) / 2;
697     } else {
698         width = dexGetWidthFromOpcode(dexOpcodeFromCodeUnit(insns[0]));
699     }
700 
701     return width;
702 }
703