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