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 */ 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); 228 MAP.put(Rops.NOP, Dops.NOP); 229 MAP.put(Rops.MOVE_INT, Dops.MOVE); 230 MAP.put(Rops.MOVE_LONG, Dops.MOVE_WIDE); 231 MAP.put(Rops.MOVE_FLOAT, Dops.MOVE); 232 MAP.put(Rops.MOVE_DOUBLE, Dops.MOVE_WIDE); 233 MAP.put(Rops.MOVE_OBJECT, Dops.MOVE_OBJECT); 234 MAP.put(Rops.MOVE_PARAM_INT, Dops.MOVE); 235 MAP.put(Rops.MOVE_PARAM_LONG, Dops.MOVE_WIDE); 236 MAP.put(Rops.MOVE_PARAM_FLOAT, Dops.MOVE); 237 MAP.put(Rops.MOVE_PARAM_DOUBLE, Dops.MOVE_WIDE); 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 246 MAP.put(Rops.CONST_INT, Dops.CONST_4); 247 MAP.put(Rops.CONST_LONG, Dops.CONST_WIDE_16); 248 MAP.put(Rops.CONST_FLOAT, Dops.CONST_4); 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 */ 260 MAP.put(Rops.CONST_OBJECT_NOTHROW, Dops.CONST_4); 261 262 MAP.put(Rops.GOTO, Dops.GOTO); 263 MAP.put(Rops.IF_EQZ_INT, Dops.IF_EQZ); 264 MAP.put(Rops.IF_NEZ_INT, Dops.IF_NEZ); 265 MAP.put(Rops.IF_LTZ_INT, Dops.IF_LTZ); 266 MAP.put(Rops.IF_GEZ_INT, Dops.IF_GEZ); 267 MAP.put(Rops.IF_LEZ_INT, Dops.IF_LEZ); 268 MAP.put(Rops.IF_GTZ_INT, Dops.IF_GTZ); 269 MAP.put(Rops.IF_EQZ_OBJECT, Dops.IF_EQZ); 270 MAP.put(Rops.IF_NEZ_OBJECT, Dops.IF_NEZ); 271 MAP.put(Rops.IF_EQ_INT, Dops.IF_EQ); 272 MAP.put(Rops.IF_NE_INT, Dops.IF_NE); 273 MAP.put(Rops.IF_LT_INT, Dops.IF_LT); 274 MAP.put(Rops.IF_GE_INT, Dops.IF_GE); 275 MAP.put(Rops.IF_LE_INT, Dops.IF_LE); 276 MAP.put(Rops.IF_GT_INT, Dops.IF_GT); 277 MAP.put(Rops.IF_EQ_OBJECT, Dops.IF_EQ); 278 MAP.put(Rops.IF_NE_OBJECT, Dops.IF_NE); 279 MAP.put(Rops.SWITCH, Dops.SPARSE_SWITCH); 280 MAP.put(Rops.ADD_INT, Dops.ADD_INT_2ADDR); 281 MAP.put(Rops.ADD_LONG, Dops.ADD_LONG_2ADDR); 282 MAP.put(Rops.ADD_FLOAT, Dops.ADD_FLOAT_2ADDR); 283 MAP.put(Rops.ADD_DOUBLE, Dops.ADD_DOUBLE_2ADDR); 284 MAP.put(Rops.SUB_INT, Dops.SUB_INT_2ADDR); 285 MAP.put(Rops.SUB_LONG, Dops.SUB_LONG_2ADDR); 286 MAP.put(Rops.SUB_FLOAT, Dops.SUB_FLOAT_2ADDR); 287 MAP.put(Rops.SUB_DOUBLE, Dops.SUB_DOUBLE_2ADDR); 288 MAP.put(Rops.MUL_INT, Dops.MUL_INT_2ADDR); 289 MAP.put(Rops.MUL_LONG, Dops.MUL_LONG_2ADDR); 290 MAP.put(Rops.MUL_FLOAT, Dops.MUL_FLOAT_2ADDR); 291 MAP.put(Rops.MUL_DOUBLE, Dops.MUL_DOUBLE_2ADDR); 292 MAP.put(Rops.DIV_INT, Dops.DIV_INT_2ADDR); 293 MAP.put(Rops.DIV_LONG, Dops.DIV_LONG_2ADDR); 294 MAP.put(Rops.DIV_FLOAT, Dops.DIV_FLOAT_2ADDR); 295 MAP.put(Rops.DIV_DOUBLE, Dops.DIV_DOUBLE_2ADDR); 296 MAP.put(Rops.REM_INT, Dops.REM_INT_2ADDR); 297 MAP.put(Rops.REM_LONG, Dops.REM_LONG_2ADDR); 298 MAP.put(Rops.REM_FLOAT, Dops.REM_FLOAT_2ADDR); 299 MAP.put(Rops.REM_DOUBLE, Dops.REM_DOUBLE_2ADDR); 300 MAP.put(Rops.NEG_INT, Dops.NEG_INT); 301 MAP.put(Rops.NEG_LONG, Dops.NEG_LONG); 302 MAP.put(Rops.NEG_FLOAT, Dops.NEG_FLOAT); 303 MAP.put(Rops.NEG_DOUBLE, Dops.NEG_DOUBLE); 304 MAP.put(Rops.AND_INT, Dops.AND_INT_2ADDR); 305 MAP.put(Rops.AND_LONG, Dops.AND_LONG_2ADDR); 306 MAP.put(Rops.OR_INT, Dops.OR_INT_2ADDR); 307 MAP.put(Rops.OR_LONG, Dops.OR_LONG_2ADDR); 308 MAP.put(Rops.XOR_INT, Dops.XOR_INT_2ADDR); 309 MAP.put(Rops.XOR_LONG, Dops.XOR_LONG_2ADDR); 310 MAP.put(Rops.SHL_INT, Dops.SHL_INT_2ADDR); 311 MAP.put(Rops.SHL_LONG, Dops.SHL_LONG_2ADDR); 312 MAP.put(Rops.SHR_INT, Dops.SHR_INT_2ADDR); 313 MAP.put(Rops.SHR_LONG, Dops.SHR_LONG_2ADDR); 314 MAP.put(Rops.USHR_INT, Dops.USHR_INT_2ADDR); 315 MAP.put(Rops.USHR_LONG, Dops.USHR_LONG_2ADDR); 316 MAP.put(Rops.NOT_INT, Dops.NOT_INT); 317 MAP.put(Rops.NOT_LONG, Dops.NOT_LONG); 318 319 MAP.put(Rops.ADD_CONST_INT, Dops.ADD_INT_LIT8); 320 // Note: No dalvik ops for other types of add_const. 321 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 328 MAP.put(Rops.MUL_CONST_INT, Dops.MUL_INT_LIT8); 329 // Note: No dalvik ops for other types of mul_const. 330 331 MAP.put(Rops.DIV_CONST_INT, Dops.DIV_INT_LIT8); 332 // Note: No dalvik ops for other types of div_const. 333 334 MAP.put(Rops.REM_CONST_INT, Dops.REM_INT_LIT8); 335 // Note: No dalvik ops for other types of rem_const. 336 337 MAP.put(Rops.AND_CONST_INT, Dops.AND_INT_LIT8); 338 // Note: No dalvik op for and_const_long. 339 340 MAP.put(Rops.OR_CONST_INT, Dops.OR_INT_LIT8); 341 // Note: No dalvik op for or_const_long. 342 343 MAP.put(Rops.XOR_CONST_INT, Dops.XOR_INT_LIT8); 344 // Note: No dalvik op for xor_const_long. 345 346 MAP.put(Rops.SHL_CONST_INT, Dops.SHL_INT_LIT8); 347 // Note: No dalvik op for shl_const_long. 348 349 MAP.put(Rops.SHR_CONST_INT, Dops.SHR_INT_LIT8); 350 // Note: No dalvik op for shr_const_long. 351 352 MAP.put(Rops.USHR_CONST_INT, Dops.USHR_INT_LIT8); 353 // Note: No dalvik op for shr_const_long. 354 355 MAP.put(Rops.CMPL_LONG, Dops.CMP_LONG); 356 MAP.put(Rops.CMPL_FLOAT, Dops.CMPL_FLOAT); 357 MAP.put(Rops.CMPL_DOUBLE, Dops.CMPL_DOUBLE); 358 MAP.put(Rops.CMPG_FLOAT, Dops.CMPG_FLOAT); 359 MAP.put(Rops.CMPG_DOUBLE, Dops.CMPG_DOUBLE); 360 MAP.put(Rops.CONV_L2I, Dops.LONG_TO_INT); 361 MAP.put(Rops.CONV_F2I, Dops.FLOAT_TO_INT); 362 MAP.put(Rops.CONV_D2I, Dops.DOUBLE_TO_INT); 363 MAP.put(Rops.CONV_I2L, Dops.INT_TO_LONG); 364 MAP.put(Rops.CONV_F2L, Dops.FLOAT_TO_LONG); 365 MAP.put(Rops.CONV_D2L, Dops.DOUBLE_TO_LONG); 366 MAP.put(Rops.CONV_I2F, Dops.INT_TO_FLOAT); 367 MAP.put(Rops.CONV_L2F, Dops.LONG_TO_FLOAT); 368 MAP.put(Rops.CONV_D2F, Dops.DOUBLE_TO_FLOAT); 369 MAP.put(Rops.CONV_I2D, Dops.INT_TO_DOUBLE); 370 MAP.put(Rops.CONV_L2D, Dops.LONG_TO_DOUBLE); 371 MAP.put(Rops.CONV_F2D, Dops.FLOAT_TO_DOUBLE); 372 MAP.put(Rops.TO_BYTE, Dops.INT_TO_BYTE); 373 MAP.put(Rops.TO_CHAR, Dops.INT_TO_CHAR); 374 MAP.put(Rops.TO_SHORT, Dops.INT_TO_SHORT); 375 MAP.put(Rops.RETURN_VOID, Dops.RETURN_VOID); 376 MAP.put(Rops.RETURN_INT, Dops.RETURN); 377 MAP.put(Rops.RETURN_LONG, Dops.RETURN_WIDE); 378 MAP.put(Rops.RETURN_FLOAT, Dops.RETURN); 379 MAP.put(Rops.RETURN_DOUBLE, Dops.RETURN_WIDE); 380 MAP.put(Rops.RETURN_OBJECT, Dops.RETURN_OBJECT); 381 MAP.put(Rops.ARRAY_LENGTH, Dops.ARRAY_LENGTH); 382 MAP.put(Rops.THROW, Dops.THROW); 383 MAP.put(Rops.MONITOR_ENTER, Dops.MONITOR_ENTER); 384 MAP.put(Rops.MONITOR_EXIT, Dops.MONITOR_EXIT); 385 MAP.put(Rops.AGET_INT, Dops.AGET); 386 MAP.put(Rops.AGET_LONG, Dops.AGET_WIDE); 387 MAP.put(Rops.AGET_FLOAT, Dops.AGET); 388 MAP.put(Rops.AGET_DOUBLE, Dops.AGET_WIDE); 389 MAP.put(Rops.AGET_OBJECT, Dops.AGET_OBJECT); 390 MAP.put(Rops.AGET_BOOLEAN, Dops.AGET_BOOLEAN); 391 MAP.put(Rops.AGET_BYTE, Dops.AGET_BYTE); 392 MAP.put(Rops.AGET_CHAR, Dops.AGET_CHAR); 393 MAP.put(Rops.AGET_SHORT, Dops.AGET_SHORT); 394 MAP.put(Rops.APUT_INT, Dops.APUT); 395 MAP.put(Rops.APUT_LONG, Dops.APUT_WIDE); 396 MAP.put(Rops.APUT_FLOAT, Dops.APUT); 397 MAP.put(Rops.APUT_DOUBLE, Dops.APUT_WIDE); 398 MAP.put(Rops.APUT_OBJECT, Dops.APUT_OBJECT); 399 MAP.put(Rops.APUT_BOOLEAN, Dops.APUT_BOOLEAN); 400 MAP.put(Rops.APUT_BYTE, Dops.APUT_BYTE); 401 MAP.put(Rops.APUT_CHAR, Dops.APUT_CHAR); 402 MAP.put(Rops.APUT_SHORT, Dops.APUT_SHORT); 403 MAP.put(Rops.NEW_INSTANCE, Dops.NEW_INSTANCE); 404 MAP.put(Rops.CHECK_CAST, Dops.CHECK_CAST); 405 MAP.put(Rops.INSTANCE_OF, Dops.INSTANCE_OF); 406 407 MAP.put(Rops.GET_FIELD_LONG, Dops.IGET_WIDE); 408 MAP.put(Rops.GET_FIELD_FLOAT, Dops.IGET); 409 MAP.put(Rops.GET_FIELD_DOUBLE, Dops.IGET_WIDE); 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 416 MAP.put(Rops.GET_STATIC_LONG, Dops.SGET_WIDE); 417 MAP.put(Rops.GET_STATIC_FLOAT, Dops.SGET); 418 MAP.put(Rops.GET_STATIC_DOUBLE, Dops.SGET_WIDE); 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 425 MAP.put(Rops.PUT_FIELD_LONG, Dops.IPUT_WIDE); 426 MAP.put(Rops.PUT_FIELD_FLOAT, Dops.IPUT); 427 MAP.put(Rops.PUT_FIELD_DOUBLE, Dops.IPUT_WIDE); 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 434 MAP.put(Rops.PUT_STATIC_LONG, Dops.SPUT_WIDE); 435 MAP.put(Rops.PUT_STATIC_FLOAT, Dops.SPUT); 436 MAP.put(Rops.PUT_STATIC_DOUBLE, Dops.SPUT_WIDE); 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 */ 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.INVOKE_CUSTOM: return Dops.INVOKE_CUSTOM; 496 case RegOps.NEW_ARRAY: return Dops.NEW_ARRAY; 497 case RegOps.FILLED_NEW_ARRAY: return Dops.FILLED_NEW_ARRAY; 498 case RegOps.FILL_ARRAY_DATA: return Dops.FILL_ARRAY_DATA; 499 case RegOps.MOVE_RESULT: { 500 RegisterSpec resultReg = insn.getResult(); 501 502 if (resultReg == null) { 503 return Dops.NOP; 504 } else { 505 switch (resultReg.getBasicType()) { 506 case Type.BT_INT: 507 case Type.BT_FLOAT: 508 case Type.BT_BOOLEAN: 509 case Type.BT_BYTE: 510 case Type.BT_CHAR: 511 case Type.BT_SHORT: 512 return Dops.MOVE_RESULT; 513 case Type.BT_LONG: 514 case Type.BT_DOUBLE: 515 return Dops.MOVE_RESULT_WIDE; 516 case Type.BT_OBJECT: 517 return Dops.MOVE_RESULT_OBJECT; 518 default: { 519 throw new RuntimeException("Unexpected basic type"); 520 } 521 } 522 } 523 } 524 525 case RegOps.GET_FIELD: { 526 CstFieldRef ref = 527 (CstFieldRef) ((ThrowingCstInsn) insn).getConstant(); 528 int basicType = ref.getBasicType(); 529 switch (basicType) { 530 case Type.BT_BOOLEAN: return Dops.IGET_BOOLEAN; 531 case Type.BT_BYTE: return Dops.IGET_BYTE; 532 case Type.BT_CHAR: return Dops.IGET_CHAR; 533 case Type.BT_SHORT: return Dops.IGET_SHORT; 534 case Type.BT_INT: return Dops.IGET; 535 } 536 break; 537 } 538 case RegOps.PUT_FIELD: { 539 CstFieldRef ref = 540 (CstFieldRef) ((ThrowingCstInsn) insn).getConstant(); 541 int basicType = ref.getBasicType(); 542 switch (basicType) { 543 case Type.BT_BOOLEAN: return Dops.IPUT_BOOLEAN; 544 case Type.BT_BYTE: return Dops.IPUT_BYTE; 545 case Type.BT_CHAR: return Dops.IPUT_CHAR; 546 case Type.BT_SHORT: return Dops.IPUT_SHORT; 547 case Type.BT_INT: return Dops.IPUT; 548 } 549 break; 550 } 551 case RegOps.GET_STATIC: { 552 CstFieldRef ref = 553 (CstFieldRef) ((ThrowingCstInsn) insn).getConstant(); 554 int basicType = ref.getBasicType(); 555 switch (basicType) { 556 case Type.BT_BOOLEAN: return Dops.SGET_BOOLEAN; 557 case Type.BT_BYTE: return Dops.SGET_BYTE; 558 case Type.BT_CHAR: return Dops.SGET_CHAR; 559 case Type.BT_SHORT: return Dops.SGET_SHORT; 560 case Type.BT_INT: return Dops.SGET; 561 } 562 break; 563 } 564 case RegOps.PUT_STATIC: { 565 CstFieldRef ref = 566 (CstFieldRef) ((ThrowingCstInsn) insn).getConstant(); 567 int basicType = ref.getBasicType(); 568 switch (basicType) { 569 case Type.BT_BOOLEAN: return Dops.SPUT_BOOLEAN; 570 case Type.BT_BYTE: return Dops.SPUT_BYTE; 571 case Type.BT_CHAR: return Dops.SPUT_CHAR; 572 case Type.BT_SHORT: return Dops.SPUT_SHORT; 573 case Type.BT_INT: return Dops.SPUT; 574 } 575 break; 576 } 577 case RegOps.CONST: { 578 Constant cst = ((ThrowingCstInsn) insn).getConstant(); 579 if (cst instanceof CstType) { 580 return Dops.CONST_CLASS; 581 } else if (cst instanceof CstString) { 582 return Dops.CONST_STRING; 583 } 584 break; 585 } 586 } 587 588 throw new RuntimeException("unknown rop: " + rop); 589 } 590 } 591