1/*
2 * Copyright (C) 2007 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
17package com.android.dx.dex.code;
18
19import com.android.dx.rop.code.Insn;
20import com.android.dx.rop.code.RegOps;
21import com.android.dx.rop.code.RegisterSpec;
22import com.android.dx.rop.code.Rop;
23import com.android.dx.rop.code.Rops;
24import com.android.dx.rop.code.ThrowingCstInsn;
25import com.android.dx.rop.cst.Constant;
26import com.android.dx.rop.cst.CstFieldRef;
27import com.android.dx.rop.cst.CstString;
28import com.android.dx.rop.cst.CstType;
29import com.android.dx.rop.type.Type;
30import java.util.HashMap;
31
32/**
33 * Translator from rop-level {@link Insn} instances to corresponding
34 * {@link Dop} instances.
35 */
36public final class RopToDop {
37    /** {@code non-null;} map from all the common rops to dalvik opcodes */
38    private static final HashMap<Rop, Dop> MAP;
39
40    /**
41     * This class is uninstantiable.
42     */
43    private RopToDop() {
44        // This space intentionally left blank.
45    }
46
47    /*
48     * The following comment lists each opcode that should be considered
49     * the "head" of an opcode chain, in terms of the process of fitting
50     * an instruction's arguments to an actual opcode. This list is
51     * automatically generated and may be of use in double-checking the
52     * manually-generated static initialization code for this class.
53     *
54     * TODO: Make opcode-gen produce useful code in this case instead
55     * of just a comment.
56     */
57
58    // BEGIN(first-opcodes); GENERATED AUTOMATICALLY BY opcode-gen
59    //     Opcodes.NOP
60    //     Opcodes.MOVE
61    //     Opcodes.MOVE_WIDE
62    //     Opcodes.MOVE_OBJECT
63    //     Opcodes.MOVE_RESULT
64    //     Opcodes.MOVE_RESULT_WIDE
65    //     Opcodes.MOVE_RESULT_OBJECT
66    //     Opcodes.MOVE_EXCEPTION
67    //     Opcodes.RETURN_VOID
68    //     Opcodes.RETURN
69    //     Opcodes.RETURN_WIDE
70    //     Opcodes.RETURN_OBJECT
71    //     Opcodes.CONST_4
72    //     Opcodes.CONST_WIDE_16
73    //     Opcodes.CONST_STRING
74    //     Opcodes.CONST_CLASS
75    //     Opcodes.MONITOR_ENTER
76    //     Opcodes.MONITOR_EXIT
77    //     Opcodes.CHECK_CAST
78    //     Opcodes.INSTANCE_OF
79    //     Opcodes.ARRAY_LENGTH
80    //     Opcodes.NEW_INSTANCE
81    //     Opcodes.NEW_ARRAY
82    //     Opcodes.FILLED_NEW_ARRAY
83    //     Opcodes.FILL_ARRAY_DATA
84    //     Opcodes.THROW
85    //     Opcodes.GOTO
86    //     Opcodes.PACKED_SWITCH
87    //     Opcodes.SPARSE_SWITCH
88    //     Opcodes.CMPL_FLOAT
89    //     Opcodes.CMPG_FLOAT
90    //     Opcodes.CMPL_DOUBLE
91    //     Opcodes.CMPG_DOUBLE
92    //     Opcodes.CMP_LONG
93    //     Opcodes.IF_EQ
94    //     Opcodes.IF_NE
95    //     Opcodes.IF_LT
96    //     Opcodes.IF_GE
97    //     Opcodes.IF_GT
98    //     Opcodes.IF_LE
99    //     Opcodes.IF_EQZ
100    //     Opcodes.IF_NEZ
101    //     Opcodes.IF_LTZ
102    //     Opcodes.IF_GEZ
103    //     Opcodes.IF_GTZ
104    //     Opcodes.IF_LEZ
105    //     Opcodes.AGET
106    //     Opcodes.AGET_WIDE
107    //     Opcodes.AGET_OBJECT
108    //     Opcodes.AGET_BOOLEAN
109    //     Opcodes.AGET_BYTE
110    //     Opcodes.AGET_CHAR
111    //     Opcodes.AGET_SHORT
112    //     Opcodes.APUT
113    //     Opcodes.APUT_WIDE
114    //     Opcodes.APUT_OBJECT
115    //     Opcodes.APUT_BOOLEAN
116    //     Opcodes.APUT_BYTE
117    //     Opcodes.APUT_CHAR
118    //     Opcodes.APUT_SHORT
119    //     Opcodes.IGET
120    //     Opcodes.IGET_WIDE
121    //     Opcodes.IGET_OBJECT
122    //     Opcodes.IGET_BOOLEAN
123    //     Opcodes.IGET_BYTE
124    //     Opcodes.IGET_CHAR
125    //     Opcodes.IGET_SHORT
126    //     Opcodes.IPUT
127    //     Opcodes.IPUT_WIDE
128    //     Opcodes.IPUT_OBJECT
129    //     Opcodes.IPUT_BOOLEAN
130    //     Opcodes.IPUT_BYTE
131    //     Opcodes.IPUT_CHAR
132    //     Opcodes.IPUT_SHORT
133    //     Opcodes.SGET
134    //     Opcodes.SGET_WIDE
135    //     Opcodes.SGET_OBJECT
136    //     Opcodes.SGET_BOOLEAN
137    //     Opcodes.SGET_BYTE
138    //     Opcodes.SGET_CHAR
139    //     Opcodes.SGET_SHORT
140    //     Opcodes.SPUT
141    //     Opcodes.SPUT_WIDE
142    //     Opcodes.SPUT_OBJECT
143    //     Opcodes.SPUT_BOOLEAN
144    //     Opcodes.SPUT_BYTE
145    //     Opcodes.SPUT_CHAR
146    //     Opcodes.SPUT_SHORT
147    //     Opcodes.INVOKE_VIRTUAL
148    //     Opcodes.INVOKE_SUPER
149    //     Opcodes.INVOKE_DIRECT
150    //     Opcodes.INVOKE_STATIC
151    //     Opcodes.INVOKE_INTERFACE
152    //     Opcodes.NEG_INT
153    //     Opcodes.NOT_INT
154    //     Opcodes.NEG_LONG
155    //     Opcodes.NOT_LONG
156    //     Opcodes.NEG_FLOAT
157    //     Opcodes.NEG_DOUBLE
158    //     Opcodes.INT_TO_LONG
159    //     Opcodes.INT_TO_FLOAT
160    //     Opcodes.INT_TO_DOUBLE
161    //     Opcodes.LONG_TO_INT
162    //     Opcodes.LONG_TO_FLOAT
163    //     Opcodes.LONG_TO_DOUBLE
164    //     Opcodes.FLOAT_TO_INT
165    //     Opcodes.FLOAT_TO_LONG
166    //     Opcodes.FLOAT_TO_DOUBLE
167    //     Opcodes.DOUBLE_TO_INT
168    //     Opcodes.DOUBLE_TO_LONG
169    //     Opcodes.DOUBLE_TO_FLOAT
170    //     Opcodes.INT_TO_BYTE
171    //     Opcodes.INT_TO_CHAR
172    //     Opcodes.INT_TO_SHORT
173    //     Opcodes.ADD_INT_2ADDR
174    //     Opcodes.SUB_INT_2ADDR
175    //     Opcodes.MUL_INT_2ADDR
176    //     Opcodes.DIV_INT_2ADDR
177    //     Opcodes.REM_INT_2ADDR
178    //     Opcodes.AND_INT_2ADDR
179    //     Opcodes.OR_INT_2ADDR
180    //     Opcodes.XOR_INT_2ADDR
181    //     Opcodes.SHL_INT_2ADDR
182    //     Opcodes.SHR_INT_2ADDR
183    //     Opcodes.USHR_INT_2ADDR
184    //     Opcodes.ADD_LONG_2ADDR
185    //     Opcodes.SUB_LONG_2ADDR
186    //     Opcodes.MUL_LONG_2ADDR
187    //     Opcodes.DIV_LONG_2ADDR
188    //     Opcodes.REM_LONG_2ADDR
189    //     Opcodes.AND_LONG_2ADDR
190    //     Opcodes.OR_LONG_2ADDR
191    //     Opcodes.XOR_LONG_2ADDR
192    //     Opcodes.SHL_LONG_2ADDR
193    //     Opcodes.SHR_LONG_2ADDR
194    //     Opcodes.USHR_LONG_2ADDR
195    //     Opcodes.ADD_FLOAT_2ADDR
196    //     Opcodes.SUB_FLOAT_2ADDR
197    //     Opcodes.MUL_FLOAT_2ADDR
198    //     Opcodes.DIV_FLOAT_2ADDR
199    //     Opcodes.REM_FLOAT_2ADDR
200    //     Opcodes.ADD_DOUBLE_2ADDR
201    //     Opcodes.SUB_DOUBLE_2ADDR
202    //     Opcodes.MUL_DOUBLE_2ADDR
203    //     Opcodes.DIV_DOUBLE_2ADDR
204    //     Opcodes.REM_DOUBLE_2ADDR
205    //     Opcodes.ADD_INT_LIT8
206    //     Opcodes.RSUB_INT_LIT8
207    //     Opcodes.MUL_INT_LIT8
208    //     Opcodes.DIV_INT_LIT8
209    //     Opcodes.REM_INT_LIT8
210    //     Opcodes.AND_INT_LIT8
211    //     Opcodes.OR_INT_LIT8
212    //     Opcodes.XOR_INT_LIT8
213    //     Opcodes.SHL_INT_LIT8
214    //     Opcodes.SHR_INT_LIT8
215    //     Opcodes.USHR_INT_LIT8
216    // END(first-opcodes)
217
218    static {
219        /*
220         * Note: The choices made here are to pick the optimistically
221         * smallest Dalvik opcode, and leave it to later processing to
222         * pessimize. See the automatically-generated comment above
223         * for reference.
224         */
225        MAP = new HashMap<Rop, Dop>(400);
226        MAP.put(Rops.NOP,               Dops.NOP);
227        MAP.put(Rops.MOVE_INT,          Dops.MOVE);
228        MAP.put(Rops.MOVE_LONG,         Dops.MOVE_WIDE);
229        MAP.put(Rops.MOVE_FLOAT,        Dops.MOVE);
230        MAP.put(Rops.MOVE_DOUBLE,       Dops.MOVE_WIDE);
231        MAP.put(Rops.MOVE_OBJECT,       Dops.MOVE_OBJECT);
232        MAP.put(Rops.MOVE_PARAM_INT,    Dops.MOVE);
233        MAP.put(Rops.MOVE_PARAM_LONG,   Dops.MOVE_WIDE);
234        MAP.put(Rops.MOVE_PARAM_FLOAT,  Dops.MOVE);
235        MAP.put(Rops.MOVE_PARAM_DOUBLE, Dops.MOVE_WIDE);
236        MAP.put(Rops.MOVE_PARAM_OBJECT, Dops.MOVE_OBJECT);
237
238        /*
239         * Note: No entry for MOVE_EXCEPTION, since it varies by
240         * exception type. (That is, there is no unique instance to
241         * add to the map.)
242         */
243
244        MAP.put(Rops.CONST_INT,         Dops.CONST_4);
245        MAP.put(Rops.CONST_LONG,        Dops.CONST_WIDE_16);
246        MAP.put(Rops.CONST_FLOAT,       Dops.CONST_4);
247        MAP.put(Rops.CONST_DOUBLE,      Dops.CONST_WIDE_16);
248
249        /*
250         * Note: No entry for CONST_OBJECT, since it needs to turn
251         * into either CONST_STRING or CONST_CLASS.
252         */
253
254        /*
255         * TODO: I think the only case of this is for null, and
256         * const/4 should cover that.
257         */
258        MAP.put(Rops.CONST_OBJECT_NOTHROW, Dops.CONST_4);
259
260        MAP.put(Rops.GOTO,                 Dops.GOTO);
261        MAP.put(Rops.IF_EQZ_INT,           Dops.IF_EQZ);
262        MAP.put(Rops.IF_NEZ_INT,           Dops.IF_NEZ);
263        MAP.put(Rops.IF_LTZ_INT,           Dops.IF_LTZ);
264        MAP.put(Rops.IF_GEZ_INT,           Dops.IF_GEZ);
265        MAP.put(Rops.IF_LEZ_INT,           Dops.IF_LEZ);
266        MAP.put(Rops.IF_GTZ_INT,           Dops.IF_GTZ);
267        MAP.put(Rops.IF_EQZ_OBJECT,        Dops.IF_EQZ);
268        MAP.put(Rops.IF_NEZ_OBJECT,        Dops.IF_NEZ);
269        MAP.put(Rops.IF_EQ_INT,            Dops.IF_EQ);
270        MAP.put(Rops.IF_NE_INT,            Dops.IF_NE);
271        MAP.put(Rops.IF_LT_INT,            Dops.IF_LT);
272        MAP.put(Rops.IF_GE_INT,            Dops.IF_GE);
273        MAP.put(Rops.IF_LE_INT,            Dops.IF_LE);
274        MAP.put(Rops.IF_GT_INT,            Dops.IF_GT);
275        MAP.put(Rops.IF_EQ_OBJECT,         Dops.IF_EQ);
276        MAP.put(Rops.IF_NE_OBJECT,         Dops.IF_NE);
277        MAP.put(Rops.SWITCH,               Dops.SPARSE_SWITCH);
278        MAP.put(Rops.ADD_INT,              Dops.ADD_INT_2ADDR);
279        MAP.put(Rops.ADD_LONG,             Dops.ADD_LONG_2ADDR);
280        MAP.put(Rops.ADD_FLOAT,            Dops.ADD_FLOAT_2ADDR);
281        MAP.put(Rops.ADD_DOUBLE,           Dops.ADD_DOUBLE_2ADDR);
282        MAP.put(Rops.SUB_INT,              Dops.SUB_INT_2ADDR);
283        MAP.put(Rops.SUB_LONG,             Dops.SUB_LONG_2ADDR);
284        MAP.put(Rops.SUB_FLOAT,            Dops.SUB_FLOAT_2ADDR);
285        MAP.put(Rops.SUB_DOUBLE,           Dops.SUB_DOUBLE_2ADDR);
286        MAP.put(Rops.MUL_INT,              Dops.MUL_INT_2ADDR);
287        MAP.put(Rops.MUL_LONG,             Dops.MUL_LONG_2ADDR);
288        MAP.put(Rops.MUL_FLOAT,            Dops.MUL_FLOAT_2ADDR);
289        MAP.put(Rops.MUL_DOUBLE,           Dops.MUL_DOUBLE_2ADDR);
290        MAP.put(Rops.DIV_INT,              Dops.DIV_INT_2ADDR);
291        MAP.put(Rops.DIV_LONG,             Dops.DIV_LONG_2ADDR);
292        MAP.put(Rops.DIV_FLOAT,            Dops.DIV_FLOAT_2ADDR);
293        MAP.put(Rops.DIV_DOUBLE,           Dops.DIV_DOUBLE_2ADDR);
294        MAP.put(Rops.REM_INT,              Dops.REM_INT_2ADDR);
295        MAP.put(Rops.REM_LONG,             Dops.REM_LONG_2ADDR);
296        MAP.put(Rops.REM_FLOAT,            Dops.REM_FLOAT_2ADDR);
297        MAP.put(Rops.REM_DOUBLE,           Dops.REM_DOUBLE_2ADDR);
298        MAP.put(Rops.NEG_INT,              Dops.NEG_INT);
299        MAP.put(Rops.NEG_LONG,             Dops.NEG_LONG);
300        MAP.put(Rops.NEG_FLOAT,            Dops.NEG_FLOAT);
301        MAP.put(Rops.NEG_DOUBLE,           Dops.NEG_DOUBLE);
302        MAP.put(Rops.AND_INT,              Dops.AND_INT_2ADDR);
303        MAP.put(Rops.AND_LONG,             Dops.AND_LONG_2ADDR);
304        MAP.put(Rops.OR_INT,               Dops.OR_INT_2ADDR);
305        MAP.put(Rops.OR_LONG,              Dops.OR_LONG_2ADDR);
306        MAP.put(Rops.XOR_INT,              Dops.XOR_INT_2ADDR);
307        MAP.put(Rops.XOR_LONG,             Dops.XOR_LONG_2ADDR);
308        MAP.put(Rops.SHL_INT,              Dops.SHL_INT_2ADDR);
309        MAP.put(Rops.SHL_LONG,             Dops.SHL_LONG_2ADDR);
310        MAP.put(Rops.SHR_INT,              Dops.SHR_INT_2ADDR);
311        MAP.put(Rops.SHR_LONG,             Dops.SHR_LONG_2ADDR);
312        MAP.put(Rops.USHR_INT,             Dops.USHR_INT_2ADDR);
313        MAP.put(Rops.USHR_LONG,            Dops.USHR_LONG_2ADDR);
314        MAP.put(Rops.NOT_INT,              Dops.NOT_INT);
315        MAP.put(Rops.NOT_LONG,             Dops.NOT_LONG);
316
317        MAP.put(Rops.ADD_CONST_INT,        Dops.ADD_INT_LIT8);
318        // Note: No dalvik ops for other types of add_const.
319
320        MAP.put(Rops.SUB_CONST_INT,        Dops.RSUB_INT_LIT8);
321        /*
322         * Note: No dalvik ops for any type of sub_const; instead
323         * there's a *reverse* sub (constant - reg) for ints only.
324         */
325
326        MAP.put(Rops.MUL_CONST_INT,        Dops.MUL_INT_LIT8);
327        // Note: No dalvik ops for other types of mul_const.
328
329        MAP.put(Rops.DIV_CONST_INT,        Dops.DIV_INT_LIT8);
330        // Note: No dalvik ops for other types of div_const.
331
332        MAP.put(Rops.REM_CONST_INT,        Dops.REM_INT_LIT8);
333        // Note: No dalvik ops for other types of rem_const.
334
335        MAP.put(Rops.AND_CONST_INT,        Dops.AND_INT_LIT8);
336        // Note: No dalvik op for and_const_long.
337
338        MAP.put(Rops.OR_CONST_INT,         Dops.OR_INT_LIT8);
339        // Note: No dalvik op for or_const_long.
340
341        MAP.put(Rops.XOR_CONST_INT,        Dops.XOR_INT_LIT8);
342        // Note: No dalvik op for xor_const_long.
343
344        MAP.put(Rops.SHL_CONST_INT,        Dops.SHL_INT_LIT8);
345        // Note: No dalvik op for shl_const_long.
346
347        MAP.put(Rops.SHR_CONST_INT,        Dops.SHR_INT_LIT8);
348        // Note: No dalvik op for shr_const_long.
349
350        MAP.put(Rops.USHR_CONST_INT,       Dops.USHR_INT_LIT8);
351        // Note: No dalvik op for shr_const_long.
352
353        MAP.put(Rops.CMPL_LONG,            Dops.CMP_LONG);
354        MAP.put(Rops.CMPL_FLOAT,           Dops.CMPL_FLOAT);
355        MAP.put(Rops.CMPL_DOUBLE,          Dops.CMPL_DOUBLE);
356        MAP.put(Rops.CMPG_FLOAT,           Dops.CMPG_FLOAT);
357        MAP.put(Rops.CMPG_DOUBLE,          Dops.CMPG_DOUBLE);
358        MAP.put(Rops.CONV_L2I,             Dops.LONG_TO_INT);
359        MAP.put(Rops.CONV_F2I,             Dops.FLOAT_TO_INT);
360        MAP.put(Rops.CONV_D2I,             Dops.DOUBLE_TO_INT);
361        MAP.put(Rops.CONV_I2L,             Dops.INT_TO_LONG);
362        MAP.put(Rops.CONV_F2L,             Dops.FLOAT_TO_LONG);
363        MAP.put(Rops.CONV_D2L,             Dops.DOUBLE_TO_LONG);
364        MAP.put(Rops.CONV_I2F,             Dops.INT_TO_FLOAT);
365        MAP.put(Rops.CONV_L2F,             Dops.LONG_TO_FLOAT);
366        MAP.put(Rops.CONV_D2F,             Dops.DOUBLE_TO_FLOAT);
367        MAP.put(Rops.CONV_I2D,             Dops.INT_TO_DOUBLE);
368        MAP.put(Rops.CONV_L2D,             Dops.LONG_TO_DOUBLE);
369        MAP.put(Rops.CONV_F2D,             Dops.FLOAT_TO_DOUBLE);
370        MAP.put(Rops.TO_BYTE,              Dops.INT_TO_BYTE);
371        MAP.put(Rops.TO_CHAR,              Dops.INT_TO_CHAR);
372        MAP.put(Rops.TO_SHORT,             Dops.INT_TO_SHORT);
373        MAP.put(Rops.RETURN_VOID,          Dops.RETURN_VOID);
374        MAP.put(Rops.RETURN_INT,           Dops.RETURN);
375        MAP.put(Rops.RETURN_LONG,          Dops.RETURN_WIDE);
376        MAP.put(Rops.RETURN_FLOAT,         Dops.RETURN);
377        MAP.put(Rops.RETURN_DOUBLE,        Dops.RETURN_WIDE);
378        MAP.put(Rops.RETURN_OBJECT,        Dops.RETURN_OBJECT);
379        MAP.put(Rops.ARRAY_LENGTH,         Dops.ARRAY_LENGTH);
380        MAP.put(Rops.THROW,                Dops.THROW);
381        MAP.put(Rops.MONITOR_ENTER,        Dops.MONITOR_ENTER);
382        MAP.put(Rops.MONITOR_EXIT,         Dops.MONITOR_EXIT);
383        MAP.put(Rops.AGET_INT,             Dops.AGET);
384        MAP.put(Rops.AGET_LONG,            Dops.AGET_WIDE);
385        MAP.put(Rops.AGET_FLOAT,           Dops.AGET);
386        MAP.put(Rops.AGET_DOUBLE,          Dops.AGET_WIDE);
387        MAP.put(Rops.AGET_OBJECT,          Dops.AGET_OBJECT);
388        MAP.put(Rops.AGET_BOOLEAN,         Dops.AGET_BOOLEAN);
389        MAP.put(Rops.AGET_BYTE,            Dops.AGET_BYTE);
390        MAP.put(Rops.AGET_CHAR,            Dops.AGET_CHAR);
391        MAP.put(Rops.AGET_SHORT,           Dops.AGET_SHORT);
392        MAP.put(Rops.APUT_INT,             Dops.APUT);
393        MAP.put(Rops.APUT_LONG,            Dops.APUT_WIDE);
394        MAP.put(Rops.APUT_FLOAT,           Dops.APUT);
395        MAP.put(Rops.APUT_DOUBLE,          Dops.APUT_WIDE);
396        MAP.put(Rops.APUT_OBJECT,          Dops.APUT_OBJECT);
397        MAP.put(Rops.APUT_BOOLEAN,         Dops.APUT_BOOLEAN);
398        MAP.put(Rops.APUT_BYTE,            Dops.APUT_BYTE);
399        MAP.put(Rops.APUT_CHAR,            Dops.APUT_CHAR);
400        MAP.put(Rops.APUT_SHORT,           Dops.APUT_SHORT);
401        MAP.put(Rops.NEW_INSTANCE,         Dops.NEW_INSTANCE);
402        MAP.put(Rops.CHECK_CAST,           Dops.CHECK_CAST);
403        MAP.put(Rops.INSTANCE_OF,          Dops.INSTANCE_OF);
404
405        MAP.put(Rops.GET_FIELD_LONG,       Dops.IGET_WIDE);
406        MAP.put(Rops.GET_FIELD_FLOAT,      Dops.IGET);
407        MAP.put(Rops.GET_FIELD_DOUBLE,     Dops.IGET_WIDE);
408        MAP.put(Rops.GET_FIELD_OBJECT,     Dops.IGET_OBJECT);
409        /*
410         * Note: No map entries for get_field_* for non-long integral types,
411         * since they need to be handled specially (see dopFor() below).
412         */
413
414        MAP.put(Rops.GET_STATIC_LONG,      Dops.SGET_WIDE);
415        MAP.put(Rops.GET_STATIC_FLOAT,     Dops.SGET);
416        MAP.put(Rops.GET_STATIC_DOUBLE,    Dops.SGET_WIDE);
417        MAP.put(Rops.GET_STATIC_OBJECT,    Dops.SGET_OBJECT);
418        /*
419         * Note: No map entries for get_static* for non-long integral types,
420         * since they need to be handled specially (see dopFor() below).
421         */
422
423        MAP.put(Rops.PUT_FIELD_LONG,       Dops.IPUT_WIDE);
424        MAP.put(Rops.PUT_FIELD_FLOAT,      Dops.IPUT);
425        MAP.put(Rops.PUT_FIELD_DOUBLE,     Dops.IPUT_WIDE);
426        MAP.put(Rops.PUT_FIELD_OBJECT,     Dops.IPUT_OBJECT);
427        /*
428         * Note: No map entries for put_field_* for non-long integral types,
429         * since they need to be handled specially (see dopFor() below).
430         */
431
432        MAP.put(Rops.PUT_STATIC_LONG,      Dops.SPUT_WIDE);
433        MAP.put(Rops.PUT_STATIC_FLOAT,     Dops.SPUT);
434        MAP.put(Rops.PUT_STATIC_DOUBLE,    Dops.SPUT_WIDE);
435        MAP.put(Rops.PUT_STATIC_OBJECT,    Dops.SPUT_OBJECT);
436        /*
437         * Note: No map entries for put_static* for non-long integral types,
438         * since they need to be handled specially (see dopFor() below).
439         */
440
441        /*
442         * Note: No map entries for invoke*, new_array, and
443         * filled_new_array, since they need to be handled specially
444         * (see dopFor() below).
445         */
446    }
447
448    /**
449     * Returns the dalvik opcode appropriate for the given register-based
450     * instruction.
451     *
452     * @param insn {@code non-null;} the original instruction
453     * @return the corresponding dalvik opcode; one of the constants in
454     * {@link Dops}
455     */
456    public static Dop dopFor(Insn insn) {
457        Rop rop = insn.getOpcode();
458
459        /*
460         * First, just try looking up the rop in the MAP of easy
461         * cases.
462         */
463        Dop result = MAP.get(rop);
464        if (result != null) {
465            return result;
466        }
467
468        /*
469         * There was no easy case for the rop, so look up the opcode, and
470         * do something special for each:
471         *
472         * The move_exception, new_array, filled_new_array, and
473         * invoke* opcodes won't be found in MAP, since they'll each
474         * have different source and/or result register types / lists.
475         *
476         * The get* and put* opcodes for (non-long) integral types
477         * aren't in the map, since the type signatures aren't
478         * sufficient to distinguish between the types (the salient
479         * source or result will always be just "int").
480         *
481         * And const instruction need to distinguish between strings and
482         * classes.
483         */
484
485        switch (rop.getOpcode()) {
486            case RegOps.MOVE_EXCEPTION:   return Dops.MOVE_EXCEPTION;
487            case RegOps.INVOKE_STATIC:    return Dops.INVOKE_STATIC;
488            case RegOps.INVOKE_VIRTUAL:   return Dops.INVOKE_VIRTUAL;
489            case RegOps.INVOKE_SUPER:     return Dops.INVOKE_SUPER;
490            case RegOps.INVOKE_DIRECT:    return Dops.INVOKE_DIRECT;
491            case RegOps.INVOKE_INTERFACE: return Dops.INVOKE_INTERFACE;
492            case RegOps.NEW_ARRAY:        return Dops.NEW_ARRAY;
493            case RegOps.FILLED_NEW_ARRAY: return Dops.FILLED_NEW_ARRAY;
494            case RegOps.FILL_ARRAY_DATA:  return Dops.FILL_ARRAY_DATA;
495            case RegOps.MOVE_RESULT: {
496                RegisterSpec resultReg = insn.getResult();
497
498                if (resultReg == null) {
499                    return Dops.NOP;
500                } else {
501                    switch (resultReg.getBasicType()) {
502                        case Type.BT_INT:
503                        case Type.BT_FLOAT:
504                        case Type.BT_BOOLEAN:
505                        case Type.BT_BYTE:
506                        case Type.BT_CHAR:
507                        case Type.BT_SHORT:
508                            return Dops.MOVE_RESULT;
509                        case Type.BT_LONG:
510                        case Type.BT_DOUBLE:
511                            return Dops.MOVE_RESULT_WIDE;
512                        case Type.BT_OBJECT:
513                            return Dops.MOVE_RESULT_OBJECT;
514                        default: {
515                            throw new RuntimeException("Unexpected basic type");
516                        }
517                    }
518                }
519            }
520
521            case RegOps.GET_FIELD: {
522                CstFieldRef ref =
523                    (CstFieldRef) ((ThrowingCstInsn) insn).getConstant();
524                int basicType = ref.getBasicType();
525                switch (basicType) {
526                    case Type.BT_BOOLEAN: return Dops.IGET_BOOLEAN;
527                    case Type.BT_BYTE:    return Dops.IGET_BYTE;
528                    case Type.BT_CHAR:    return Dops.IGET_CHAR;
529                    case Type.BT_SHORT:   return Dops.IGET_SHORT;
530                    case Type.BT_INT:     return Dops.IGET;
531                }
532                break;
533            }
534            case RegOps.PUT_FIELD: {
535                CstFieldRef ref =
536                    (CstFieldRef) ((ThrowingCstInsn) insn).getConstant();
537                int basicType = ref.getBasicType();
538                switch (basicType) {
539                    case Type.BT_BOOLEAN: return Dops.IPUT_BOOLEAN;
540                    case Type.BT_BYTE:    return Dops.IPUT_BYTE;
541                    case Type.BT_CHAR:    return Dops.IPUT_CHAR;
542                    case Type.BT_SHORT:   return Dops.IPUT_SHORT;
543                    case Type.BT_INT:     return Dops.IPUT;
544                }
545                break;
546            }
547            case RegOps.GET_STATIC: {
548                CstFieldRef ref =
549                    (CstFieldRef) ((ThrowingCstInsn) insn).getConstant();
550                int basicType = ref.getBasicType();
551                switch (basicType) {
552                    case Type.BT_BOOLEAN: return Dops.SGET_BOOLEAN;
553                    case Type.BT_BYTE:    return Dops.SGET_BYTE;
554                    case Type.BT_CHAR:    return Dops.SGET_CHAR;
555                    case Type.BT_SHORT:   return Dops.SGET_SHORT;
556                    case Type.BT_INT:     return Dops.SGET;
557                }
558                break;
559            }
560            case RegOps.PUT_STATIC: {
561                CstFieldRef ref =
562                    (CstFieldRef) ((ThrowingCstInsn) insn).getConstant();
563                int basicType = ref.getBasicType();
564                switch (basicType) {
565                    case Type.BT_BOOLEAN: return Dops.SPUT_BOOLEAN;
566                    case Type.BT_BYTE:    return Dops.SPUT_BYTE;
567                    case Type.BT_CHAR:    return Dops.SPUT_CHAR;
568                    case Type.BT_SHORT:   return Dops.SPUT_SHORT;
569                    case Type.BT_INT:     return Dops.SPUT;
570                }
571                break;
572            }
573            case RegOps.CONST: {
574                Constant cst = ((ThrowingCstInsn) insn).getConstant();
575                if (cst instanceof CstType) {
576                    return Dops.CONST_CLASS;
577                } else if (cst instanceof CstString) {
578                    return Dops.CONST_STRING;
579                }
580                break;
581            }
582        }
583
584        throw new RuntimeException("unknown rop: " + rop);
585    }
586}
587