summaryrefslogtreecommitdiff
path: root/dx/src/com/android/dx/dex/code/RopToDop.java
blob: d8fa1cc5f81f138fa1a315d1a4ab0544fe491f18 (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
/*
 * Copyright (C) 2007 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.
 */

package com.android.dx.dex.code;

import com.android.dx.rop.code.Insn;
import com.android.dx.rop.code.RegOps;
import com.android.dx.rop.code.Rop;
import com.android.dx.rop.code.Rops;
import com.android.dx.rop.code.ThrowingCstInsn;
import com.android.dx.rop.code.RegisterSpec;
import com.android.dx.rop.cst.Constant;
import com.android.dx.rop.cst.CstFieldRef;
import com.android.dx.rop.cst.CstString;
import com.android.dx.rop.cst.CstType;
import com.android.dx.rop.type.Type;

import java.util.HashMap;

/**
 * Translator from rop-level {@link Insn} instances to corresponding
 * {@link Dop} instances.
 */
public final class RopToDop {
    /** {@code non-null;} map from all the common rops to dalvik opcodes */
    private static final HashMap<Rop, Dop> MAP;

    /**
     * This class is uninstantiable.
     */
    private RopToDop() {
        // This space intentionally left blank.
    }

    static {
        /*
         * Note: The choices made here are to pick the optimistically
         * smallest Dalvik opcode, and leave it to later processing to
         * pessimize.
         */
        MAP = new HashMap<Rop, Dop>(400);
        MAP.put(Rops.NOP,               Dops.NOP);
        MAP.put(Rops.MOVE_INT,          Dops.MOVE);
        MAP.put(Rops.MOVE_LONG,         Dops.MOVE_WIDE);
        MAP.put(Rops.MOVE_FLOAT,        Dops.MOVE);
        MAP.put(Rops.MOVE_DOUBLE,       Dops.MOVE_WIDE);
        MAP.put(Rops.MOVE_OBJECT,       Dops.MOVE_OBJECT);
        MAP.put(Rops.MOVE_PARAM_INT,    Dops.MOVE);
        MAP.put(Rops.MOVE_PARAM_LONG,   Dops.MOVE_WIDE);
        MAP.put(Rops.MOVE_PARAM_FLOAT,  Dops.MOVE);
        MAP.put(Rops.MOVE_PARAM_DOUBLE, Dops.MOVE_WIDE);
        MAP.put(Rops.MOVE_PARAM_OBJECT, Dops.MOVE_OBJECT);

        /*
         * Note: No entry for MOVE_EXCEPTION, since it varies by
         * exception type. (That is, there is no unique instance to
         * add to the map.)
         */

        MAP.put(Rops.CONST_INT,         Dops.CONST_4);
        MAP.put(Rops.CONST_LONG,        Dops.CONST_WIDE_16);
        MAP.put(Rops.CONST_FLOAT,       Dops.CONST_4);
        MAP.put(Rops.CONST_DOUBLE,      Dops.CONST_WIDE_16);

        /*
         * Note: No entry for CONST_OBJECT, since it needs to turn
         * into either CONST_STRING or CONST_CLASS.
         */

        /*
         * TODO: I think the only case of this is for null, and
         * const/4 should cover that.
         */
        MAP.put(Rops.CONST_OBJECT_NOTHROW, Dops.CONST_4);

        MAP.put(Rops.GOTO,                 Dops.GOTO);
        MAP.put(Rops.IF_EQZ_INT,           Dops.IF_EQZ);
        MAP.put(Rops.IF_NEZ_INT,           Dops.IF_NEZ);
        MAP.put(Rops.IF_LTZ_INT,           Dops.IF_LTZ);
        MAP.put(Rops.IF_GEZ_INT,           Dops.IF_GEZ);
        MAP.put(Rops.IF_LEZ_INT,           Dops.IF_LEZ);
        MAP.put(Rops.IF_GTZ_INT,           Dops.IF_GTZ);
        MAP.put(Rops.IF_EQZ_OBJECT,        Dops.IF_EQZ);
        MAP.put(Rops.IF_NEZ_OBJECT,        Dops.IF_NEZ);
        MAP.put(Rops.IF_EQ_INT,            Dops.IF_EQ);
        MAP.put(Rops.IF_NE_INT,            Dops.IF_NE);
        MAP.put(Rops.IF_LT_INT,            Dops.IF_LT);
        MAP.put(Rops.IF_GE_INT,            Dops.IF_GE);
        MAP.put(Rops.IF_LE_INT,            Dops.IF_LE);
        MAP.put(Rops.IF_GT_INT,            Dops.IF_GT);
        MAP.put(Rops.IF_EQ_OBJECT,         Dops.IF_EQ);
        MAP.put(Rops.IF_NE_OBJECT,         Dops.IF_NE);
        MAP.put(Rops.SWITCH,               Dops.SPARSE_SWITCH);
        MAP.put(Rops.ADD_INT,              Dops.ADD_INT_2ADDR);
        MAP.put(Rops.ADD_LONG,             Dops.ADD_LONG_2ADDR);
        MAP.put(Rops.ADD_FLOAT,            Dops.ADD_FLOAT_2ADDR);
        MAP.put(Rops.ADD_DOUBLE,           Dops.ADD_DOUBLE_2ADDR);
        MAP.put(Rops.SUB_INT,              Dops.SUB_INT_2ADDR);
        MAP.put(Rops.SUB_LONG,             Dops.SUB_LONG_2ADDR);
        MAP.put(Rops.SUB_FLOAT,            Dops.SUB_FLOAT_2ADDR);
        MAP.put(Rops.SUB_DOUBLE,           Dops.SUB_DOUBLE_2ADDR);
        MAP.put(Rops.MUL_INT,              Dops.MUL_INT_2ADDR);
        MAP.put(Rops.MUL_LONG,             Dops.MUL_LONG_2ADDR);
        MAP.put(Rops.MUL_FLOAT,            Dops.MUL_FLOAT_2ADDR);
        MAP.put(Rops.MUL_DOUBLE,           Dops.MUL_DOUBLE_2ADDR);
        MAP.put(Rops.DIV_INT,              Dops.DIV_INT_2ADDR);
        MAP.put(Rops.DIV_LONG,             Dops.DIV_LONG_2ADDR);
        MAP.put(Rops.DIV_FLOAT,            Dops.DIV_FLOAT_2ADDR);
        MAP.put(Rops.DIV_DOUBLE,           Dops.DIV_DOUBLE_2ADDR);
        MAP.put(Rops.REM_INT,              Dops.REM_INT_2ADDR);
        MAP.put(Rops.REM_LONG,             Dops.REM_LONG_2ADDR);
        MAP.put(Rops.REM_FLOAT,            Dops.REM_FLOAT_2ADDR);
        MAP.put(Rops.REM_DOUBLE,           Dops.REM_DOUBLE_2ADDR);
        MAP.put(Rops.NEG_INT,              Dops.NEG_INT);
        MAP.put(Rops.NEG_LONG,             Dops.NEG_LONG);
        MAP.put(Rops.NEG_FLOAT,            Dops.NEG_FLOAT);
        MAP.put(Rops.NEG_DOUBLE,           Dops.NEG_DOUBLE);
        MAP.put(Rops.AND_INT,              Dops.AND_INT_2ADDR);
        MAP.put(Rops.AND_LONG,             Dops.AND_LONG_2ADDR);
        MAP.put(Rops.OR_INT,               Dops.OR_INT_2ADDR);
        MAP.put(Rops.OR_LONG,              Dops.OR_LONG_2ADDR);
        MAP.put(Rops.XOR_INT,              Dops.XOR_INT_2ADDR);
        MAP.put(Rops.XOR_LONG,             Dops.XOR_LONG_2ADDR);
        MAP.put(Rops.SHL_INT,              Dops.SHL_INT_2ADDR);
        MAP.put(Rops.SHL_LONG,             Dops.SHL_LONG_2ADDR);
        MAP.put(Rops.SHR_INT,              Dops.SHR_INT_2ADDR);
        MAP.put(Rops.SHR_LONG,             Dops.SHR_LONG_2ADDR);
        MAP.put(Rops.USHR_INT,             Dops.USHR_INT_2ADDR);
        MAP.put(Rops.USHR_LONG,            Dops.USHR_LONG_2ADDR);
        MAP.put(Rops.NOT_INT,              Dops.NOT_INT);
        MAP.put(Rops.NOT_LONG,             Dops.NOT_LONG);

        MAP.put(Rops.ADD_CONST_INT,        Dops.ADD_INT_LIT8);
        // Note: No dalvik ops for other types of add_const.

        /*
         * Note: No dalvik ops for any type of sub_const; there's a
         * *reverse* sub (constant - reg) for ints, though, but that
         * should end up getting handled at optimization time.
         */

        MAP.put(Rops.MUL_CONST_INT,        Dops.MUL_INT_LIT8);
        // Note: No dalvik ops for other types of mul_const.

        MAP.put(Rops.DIV_CONST_INT,        Dops.DIV_INT_LIT8);
        // Note: No dalvik ops for other types of div_const.

        MAP.put(Rops.REM_CONST_INT,        Dops.REM_INT_LIT8);
        // Note: No dalvik ops for other types of rem_const.

        MAP.put(Rops.AND_CONST_INT,        Dops.AND_INT_LIT8);
        // Note: No dalvik op for and_const_long.

        MAP.put(Rops.OR_CONST_INT,         Dops.OR_INT_LIT8);
        // Note: No dalvik op for or_const_long.

        MAP.put(Rops.XOR_CONST_INT,        Dops.XOR_INT_LIT8);
        // Note: No dalvik op for xor_const_long.

        MAP.put(Rops.SHL_CONST_INT,        Dops.SHL_INT_LIT8);
        // Note: No dalvik op for shl_const_long.

        MAP.put(Rops.SHR_CONST_INT,        Dops.SHR_INT_LIT8);
        // Note: No dalvik op for shr_const_long.

        MAP.put(Rops.USHR_CONST_INT,       Dops.USHR_INT_LIT8);
        // Note: No dalvik op for shr_const_long.

        MAP.put(Rops.CMPL_LONG,            Dops.CMP_LONG);
        MAP.put(Rops.CMPL_FLOAT,           Dops.CMPL_FLOAT);
        MAP.put(Rops.CMPL_DOUBLE,          Dops.CMPL_DOUBLE);
        MAP.put(Rops.CMPG_FLOAT,           Dops.CMPG_FLOAT);
        MAP.put(Rops.CMPG_DOUBLE,          Dops.CMPG_DOUBLE);
        MAP.put(Rops.CONV_L2I,             Dops.LONG_TO_INT);
        MAP.put(Rops.CONV_F2I,             Dops.FLOAT_TO_INT);
        MAP.put(Rops.CONV_D2I,             Dops.DOUBLE_TO_INT);
        MAP.put(Rops.CONV_I2L,             Dops.INT_TO_LONG);
        MAP.put(Rops.CONV_F2L,             Dops.FLOAT_TO_LONG);
        MAP.put(Rops.CONV_D2L,             Dops.DOUBLE_TO_LONG);
        MAP.put(Rops.CONV_I2F,             Dops.INT_TO_FLOAT);
        MAP.put(Rops.CONV_L2F,             Dops.LONG_TO_FLOAT);
        MAP.put(Rops.CONV_D2F,             Dops.DOUBLE_TO_FLOAT);
        MAP.put(Rops.CONV_I2D,             Dops.INT_TO_DOUBLE);
        MAP.put(Rops.CONV_L2D,             Dops.LONG_TO_DOUBLE);
        MAP.put(Rops.CONV_F2D,             Dops.FLOAT_TO_DOUBLE);
        MAP.put(Rops.TO_BYTE,              Dops.INT_TO_BYTE);
        MAP.put(Rops.TO_CHAR,              Dops.INT_TO_CHAR);
        MAP.put(Rops.TO_SHORT,             Dops.INT_TO_SHORT);
        MAP.put(Rops.RETURN_VOID,          Dops.RETURN_VOID);
        MAP.put(Rops.RETURN_INT,           Dops.RETURN);
        MAP.put(Rops.RETURN_LONG,          Dops.RETURN_WIDE);
        MAP.put(Rops.RETURN_FLOAT,         Dops.RETURN);
        MAP.put(Rops.RETURN_DOUBLE,        Dops.RETURN_WIDE);
        MAP.put(Rops.RETURN_OBJECT,        Dops.RETURN_OBJECT);
        MAP.put(Rops.ARRAY_LENGTH,         Dops.ARRAY_LENGTH);
        MAP.put(Rops.THROW,                Dops.THROW);
        MAP.put(Rops.MONITOR_ENTER,        Dops.MONITOR_ENTER);
        MAP.put(Rops.MONITOR_EXIT,         Dops.MONITOR_EXIT);
        MAP.put(Rops.AGET_INT,             Dops.AGET);
        MAP.put(Rops.AGET_LONG,            Dops.AGET_WIDE);
        MAP.put(Rops.AGET_FLOAT,           Dops.AGET);
        MAP.put(Rops.AGET_DOUBLE,          Dops.AGET_WIDE);
        MAP.put(Rops.AGET_OBJECT,          Dops.AGET_OBJECT);
        MAP.put(Rops.AGET_BOOLEAN,         Dops.AGET_BOOLEAN);
        MAP.put(Rops.AGET_BYTE,            Dops.AGET_BYTE);
        MAP.put(Rops.AGET_CHAR,            Dops.AGET_CHAR);
        MAP.put(Rops.AGET_SHORT,           Dops.AGET_SHORT);
        MAP.put(Rops.APUT_INT,             Dops.APUT);
        MAP.put(Rops.APUT_LONG,            Dops.APUT_WIDE);
        MAP.put(Rops.APUT_FLOAT,           Dops.APUT);
        MAP.put(Rops.APUT_DOUBLE,          Dops.APUT_WIDE);
        MAP.put(Rops.APUT_OBJECT,          Dops.APUT_OBJECT);
        MAP.put(Rops.APUT_BOOLEAN,         Dops.APUT_BOOLEAN);
        MAP.put(Rops.APUT_BYTE,            Dops.APUT_BYTE);
        MAP.put(Rops.APUT_CHAR,            Dops.APUT_CHAR);
        MAP.put(Rops.APUT_SHORT,           Dops.APUT_SHORT);
        MAP.put(Rops.NEW_INSTANCE,         Dops.NEW_INSTANCE);
        MAP.put(Rops.CHECK_CAST,           Dops.CHECK_CAST);
        MAP.put(Rops.INSTANCE_OF,          Dops.INSTANCE_OF);

        MAP.put(Rops.GET_FIELD_LONG,       Dops.IGET_WIDE);
        MAP.put(Rops.GET_FIELD_FLOAT,      Dops.IGET);
        MAP.put(Rops.GET_FIELD_DOUBLE,     Dops.IGET_WIDE);
        MAP.put(Rops.GET_FIELD_OBJECT,     Dops.IGET_OBJECT);
        /*
         * Note: No map entries for get_field_* for non-long integral types,
         * since they need to be handled specially (see dopFor() below).
         */

        MAP.put(Rops.GET_STATIC_LONG,      Dops.SGET_WIDE);
        MAP.put(Rops.GET_STATIC_FLOAT,     Dops.SGET);
        MAP.put(Rops.GET_STATIC_DOUBLE,    Dops.SGET_WIDE);
        MAP.put(Rops.GET_STATIC_OBJECT,    Dops.SGET_OBJECT);
        /*
         * Note: No map entries for get_static* for non-long integral types,
         * since they need to be handled specially (see dopFor() below).
         */

        MAP.put(Rops.PUT_FIELD_LONG,       Dops.IPUT_WIDE);
        MAP.put(Rops.PUT_FIELD_FLOAT,      Dops.IPUT);
        MAP.put(Rops.PUT_FIELD_DOUBLE,     Dops.IPUT_WIDE);
        MAP.put(Rops.PUT_FIELD_OBJECT,     Dops.IPUT_OBJECT);
        /*
         * Note: No map entries for put_field_* for non-long integral types,
         * since they need to be handled specially (see dopFor() below).
         */

        MAP.put(Rops.PUT_STATIC_LONG,      Dops.SPUT_WIDE);
        MAP.put(Rops.PUT_STATIC_FLOAT,     Dops.SPUT);
        MAP.put(Rops.PUT_STATIC_DOUBLE,    Dops.SPUT_WIDE);
        MAP.put(Rops.PUT_STATIC_OBJECT,    Dops.SPUT_OBJECT);
        /*
         * Note: No map entries for put_static* for non-long integral types,
         * since they need to be handled specially (see dopFor() below).
         */

        /*
         * Note: No map entries for invoke*, new_array, and
         * filled_new_array, since they need to be handled specially
         * (see dopFor() below).
         */
    }

    /**
     * Returns the dalvik opcode appropriate for the given register-based
     * instruction.
     *
     * @param insn {@code non-null;} the original instruction
     * @return the corresponding dalvik opcode; one of the constants in
     * {@link Dops}
     */
    public static Dop dopFor(Insn insn) {
        Rop rop = insn.getOpcode();

        /*
         * First, just try looking up the rop in the MAP of easy
         * cases.
         */
        Dop result = MAP.get(rop);
        if (result != null) {
            return result;
        }

        /*
         * There was no easy case for the rop, so look up the opcode, and
         * do something special for each:
         *
         * The move_exception, new_array, filled_new_array, and
         * invoke* opcodes won't be found in MAP, since they'll each
         * have different source and/or result register types / lists.
         *
         * The get* and put* opcodes for (non-long) integral types
         * aren't in the map, since the type signatures aren't
         * sufficient to distinguish between the types (the salient
         * source or result will always be just "int").
         *
         * And const instruction need to distinguish between strings and
         * classes.
         */

        switch (rop.getOpcode()) {
            case RegOps.MOVE_EXCEPTION:   return Dops.MOVE_EXCEPTION;
            case RegOps.INVOKE_STATIC:    return Dops.INVOKE_STATIC;
            case RegOps.INVOKE_VIRTUAL:   return Dops.INVOKE_VIRTUAL;
            case RegOps.INVOKE_SUPER:     return Dops.INVOKE_SUPER;
            case RegOps.INVOKE_DIRECT:    return Dops.INVOKE_DIRECT;
            case RegOps.INVOKE_INTERFACE: return Dops.INVOKE_INTERFACE;
            case RegOps.NEW_ARRAY:        return Dops.NEW_ARRAY;
            case RegOps.FILLED_NEW_ARRAY: return Dops.FILLED_NEW_ARRAY;
            case RegOps.FILL_ARRAY_DATA:  return Dops.FILL_ARRAY_DATA;
            case RegOps.MOVE_RESULT: {
                RegisterSpec resultReg = insn.getResult();

                if (resultReg == null) {
                    return Dops.NOP;
                } else {
                    switch (resultReg.getBasicType()) {
                        case Type.BT_INT:
                        case Type.BT_FLOAT:
                        case Type.BT_BOOLEAN:
                        case Type.BT_BYTE:
                        case Type.BT_CHAR:
                        case Type.BT_SHORT:
                            return Dops.MOVE_RESULT;
                        case Type.BT_LONG:
                        case Type.BT_DOUBLE:
                            return Dops.MOVE_RESULT_WIDE;
                        case Type.BT_OBJECT:
                            return Dops.MOVE_RESULT_OBJECT;
                        default: {
                            throw new RuntimeException("Unexpected basic type");
                        }
                    }
                }
            }

            case RegOps.GET_FIELD: {
                CstFieldRef ref =
                    (CstFieldRef) ((ThrowingCstInsn) insn).getConstant();
                int basicType = ref.getBasicType();
                switch (basicType) {
                    case Type.BT_BOOLEAN: return Dops.IGET_BOOLEAN;
                    case Type.BT_BYTE:    return Dops.IGET_BYTE;
                    case Type.BT_CHAR:    return Dops.IGET_CHAR;
                    case Type.BT_SHORT:   return Dops.IGET_SHORT;
                    case Type.BT_INT:     return Dops.IGET;
                }
                break;
            }
            case RegOps.PUT_FIELD: {
                CstFieldRef ref =
                    (CstFieldRef) ((ThrowingCstInsn) insn).getConstant();
                int basicType = ref.getBasicType();
                switch (basicType) {
                    case Type.BT_BOOLEAN: return Dops.IPUT_BOOLEAN;
                    case Type.BT_BYTE:    return Dops.IPUT_BYTE;
                    case Type.BT_CHAR:    return Dops.IPUT_CHAR;
                    case Type.BT_SHORT:   return Dops.IPUT_SHORT;
                    case Type.BT_INT:     return Dops.IPUT;
                }
                break;
            }
            case RegOps.GET_STATIC: {
                CstFieldRef ref =
                    (CstFieldRef) ((ThrowingCstInsn) insn).getConstant();
                int basicType = ref.getBasicType();
                switch (basicType) {
                    case Type.BT_BOOLEAN: return Dops.SGET_BOOLEAN;
                    case Type.BT_BYTE:    return Dops.SGET_BYTE;
                    case Type.BT_CHAR:    return Dops.SGET_CHAR;
                    case Type.BT_SHORT:   return Dops.SGET_SHORT;
                    case Type.BT_INT:     return Dops.SGET;
                }
                break;
            }
            case RegOps.PUT_STATIC: {
                CstFieldRef ref =
                    (CstFieldRef) ((ThrowingCstInsn) insn).getConstant();
                int basicType = ref.getBasicType();
                switch (basicType) {
                    case Type.BT_BOOLEAN: return Dops.SPUT_BOOLEAN;
                    case Type.BT_BYTE:    return Dops.SPUT_BYTE;
                    case Type.BT_CHAR:    return Dops.SPUT_CHAR;
                    case Type.BT_SHORT:   return Dops.SPUT_SHORT;
                    case Type.BT_INT:     return Dops.SPUT;
                }
                break;
            }
            case RegOps.CONST: {
                Constant cst = ((ThrowingCstInsn) insn).getConstant();
                if (cst instanceof CstType) {
                    return Dops.CONST_CLASS;
                } else if (cst instanceof CstString) {
                    return Dops.CONST_STRING;
                }
                break;
            }
        }

        throw new RuntimeException("unknown rop: " + rop);
    }
}