InterpC-x86.c revision e92aa412a6779376911d415d4b790dc14f5f6bdd
1/* 2 * This file was generated automatically by gen-mterp.py for 'x86'. 3 * 4 * --> DO NOT EDIT <-- 5 */ 6 7/* File: c/header.c */ 8/* 9 * Copyright (C) 2008 The Android Open Source Project 10 * 11 * Licensed under the Apache License, Version 2.0 (the "License"); 12 * you may not use this file except in compliance with the License. 13 * You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, software 18 * distributed under the License is distributed on an "AS IS" BASIS, 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 * See the License for the specific language governing permissions and 21 * limitations under the License. 22 */ 23 24/* common includes */ 25#include "Dalvik.h" 26#include "interp/InterpDefs.h" 27#include "mterp/Mterp.h" 28#include <math.h> // needed for fmod, fmodf 29#include "mterp/common/FindInterface.h" 30 31/* 32 * Configuration defines. These affect the C implementations, i.e. the 33 * portable interpreter(s) and C stubs. 34 * 35 * Some defines are controlled by the Makefile, e.g.: 36 * WITH_PROFILER 37 * WITH_DEBUGGER 38 * WITH_INSTR_CHECKS 39 * WITH_TRACKREF_CHECKS 40 * EASY_GDB 41 * NDEBUG 42 * 43 * If THREADED_INTERP is not defined, we use a classic "while true / switch" 44 * interpreter. If it is defined, then the tail end of each instruction 45 * handler fetches the next instruction and jumps directly to the handler. 46 * This increases the size of the "Std" interpreter by about 10%, but 47 * provides a speedup of about the same magnitude. 48 * 49 * There's a "hybrid" approach that uses a goto table instead of a switch 50 * statement, avoiding the "is the opcode in range" tests required for switch. 51 * The performance is close to the threaded version, and without the 10% 52 * size increase, but the benchmark results are off enough that it's not 53 * worth adding as a third option. 54 */ 55#define THREADED_INTERP /* threaded vs. while-loop interpreter */ 56 57#ifdef WITH_INSTR_CHECKS /* instruction-level paranoia (slow!) */ 58# define CHECK_BRANCH_OFFSETS 59# define CHECK_REGISTER_INDICES 60#endif 61 62/* 63 * ARM EABI requires 64-bit alignment for access to 64-bit data types. We 64 * can't just use pointers to copy 64-bit values out of our interpreted 65 * register set, because gcc will generate ldrd/strd. 66 * 67 * The __UNION version copies data in and out of a union. The __MEMCPY 68 * version uses a memcpy() call to do the transfer; gcc is smart enough to 69 * not actually call memcpy(). The __UNION version is very bad on ARM; 70 * it only uses one more instruction than __MEMCPY, but for some reason 71 * gcc thinks it needs separate storage for every instance of the union. 72 * On top of that, it feels the need to zero them out at the start of the 73 * method. Net result is we zero out ~700 bytes of stack space at the top 74 * of the interpreter using ARM STM instructions. 75 */ 76#if defined(__ARM_EABI__) 77//# define NO_UNALIGN_64__UNION 78# define NO_UNALIGN_64__MEMCPY 79#endif 80 81//#define LOG_INSTR /* verbose debugging */ 82/* set and adjust ANDROID_LOG_TAGS='*:i jdwp:i dalvikvm:i dalvikvmi:i' */ 83 84/* 85 * Keep a tally of accesses to fields. Currently only works if full DEX 86 * optimization is disabled. 87 */ 88#ifdef PROFILE_FIELD_ACCESS 89# define UPDATE_FIELD_GET(_field) { (_field)->gets++; } 90# define UPDATE_FIELD_PUT(_field) { (_field)->puts++; } 91#else 92# define UPDATE_FIELD_GET(_field) ((void)0) 93# define UPDATE_FIELD_PUT(_field) ((void)0) 94#endif 95 96/* 97 * Export another copy of the PC on every instruction; this is largely 98 * redundant with EXPORT_PC and the debugger code. This value can be 99 * compared against what we have stored on the stack with EXPORT_PC to 100 * help ensure that we aren't missing any export calls. 101 */ 102#if WITH_EXTRA_GC_CHECKS > 1 103# define EXPORT_EXTRA_PC() (self->currentPc2 = pc) 104#else 105# define EXPORT_EXTRA_PC() 106#endif 107 108/* 109 * Adjust the program counter. "_offset" is a signed int, in 16-bit units. 110 * 111 * Assumes the existence of "const u2* pc" and "const u2* curMethod->insns". 112 * 113 * We don't advance the program counter until we finish an instruction or 114 * branch, because we do want to have to unroll the PC if there's an 115 * exception. 116 */ 117#ifdef CHECK_BRANCH_OFFSETS 118# define ADJUST_PC(_offset) do { \ 119 int myoff = _offset; /* deref only once */ \ 120 if (pc + myoff < curMethod->insns || \ 121 pc + myoff >= curMethod->insns + dvmGetMethodInsnsSize(curMethod)) \ 122 { \ 123 char* desc; \ 124 desc = dexProtoCopyMethodDescriptor(&curMethod->prototype); \ 125 LOGE("Invalid branch %d at 0x%04x in %s.%s %s\n", \ 126 myoff, (int) (pc - curMethod->insns), \ 127 curMethod->clazz->descriptor, curMethod->name, desc); \ 128 free(desc); \ 129 dvmAbort(); \ 130 } \ 131 pc += myoff; \ 132 EXPORT_EXTRA_PC(); \ 133 } while (false) 134#else 135# define ADJUST_PC(_offset) do { \ 136 pc += _offset; \ 137 EXPORT_EXTRA_PC(); \ 138 } while (false) 139#endif 140 141/* 142 * If enabled, log instructions as we execute them. 143 */ 144#ifdef LOG_INSTR 145# define ILOGD(...) ILOG(LOG_DEBUG, __VA_ARGS__) 146# define ILOGV(...) ILOG(LOG_VERBOSE, __VA_ARGS__) 147# define ILOG(_level, ...) do { \ 148 char debugStrBuf[128]; \ 149 snprintf(debugStrBuf, sizeof(debugStrBuf), __VA_ARGS__); \ 150 if (curMethod != NULL) \ 151 LOG(_level, LOG_TAG"i", "%-2d|%04x%s\n", \ 152 self->threadId, (int)(pc - curMethod->insns), debugStrBuf); \ 153 else \ 154 LOG(_level, LOG_TAG"i", "%-2d|####%s\n", \ 155 self->threadId, debugStrBuf); \ 156 } while(false) 157void dvmDumpRegs(const Method* method, const u4* framePtr, bool inOnly); 158# define DUMP_REGS(_meth, _frame, _inOnly) dvmDumpRegs(_meth, _frame, _inOnly) 159static const char kSpacing[] = " "; 160#else 161# define ILOGD(...) ((void)0) 162# define ILOGV(...) ((void)0) 163# define DUMP_REGS(_meth, _frame, _inOnly) ((void)0) 164#endif 165 166/* get a long from an array of u4 */ 167static inline s8 getLongFromArray(const u4* ptr, int idx) 168{ 169#if defined(NO_UNALIGN_64__UNION) 170 union { s8 ll; u4 parts[2]; } conv; 171 172 ptr += idx; 173 conv.parts[0] = ptr[0]; 174 conv.parts[1] = ptr[1]; 175 return conv.ll; 176#elif defined(NO_UNALIGN_64__MEMCPY) 177 s8 val; 178 memcpy(&val, &ptr[idx], 8); 179 return val; 180#else 181 return *((s8*) &ptr[idx]); 182#endif 183} 184 185/* store a long into an array of u4 */ 186static inline void putLongToArray(u4* ptr, int idx, s8 val) 187{ 188#if defined(NO_UNALIGN_64__UNION) 189 union { s8 ll; u4 parts[2]; } conv; 190 191 ptr += idx; 192 conv.ll = val; 193 ptr[0] = conv.parts[0]; 194 ptr[1] = conv.parts[1]; 195#elif defined(NO_UNALIGN_64__MEMCPY) 196 memcpy(&ptr[idx], &val, 8); 197#else 198 *((s8*) &ptr[idx]) = val; 199#endif 200} 201 202/* get a double from an array of u4 */ 203static inline double getDoubleFromArray(const u4* ptr, int idx) 204{ 205#if defined(NO_UNALIGN_64__UNION) 206 union { double d; u4 parts[2]; } conv; 207 208 ptr += idx; 209 conv.parts[0] = ptr[0]; 210 conv.parts[1] = ptr[1]; 211 return conv.d; 212#elif defined(NO_UNALIGN_64__MEMCPY) 213 double dval; 214 memcpy(&dval, &ptr[idx], 8); 215 return dval; 216#else 217 return *((double*) &ptr[idx]); 218#endif 219} 220 221/* store a double into an array of u4 */ 222static inline void putDoubleToArray(u4* ptr, int idx, double dval) 223{ 224#if defined(NO_UNALIGN_64__UNION) 225 union { double d; u4 parts[2]; } conv; 226 227 ptr += idx; 228 conv.d = dval; 229 ptr[0] = conv.parts[0]; 230 ptr[1] = conv.parts[1]; 231#elif defined(NO_UNALIGN_64__MEMCPY) 232 memcpy(&ptr[idx], &dval, 8); 233#else 234 *((double*) &ptr[idx]) = dval; 235#endif 236} 237 238/* 239 * If enabled, validate the register number on every access. Otherwise, 240 * just do an array access. 241 * 242 * Assumes the existence of "u4* fp". 243 * 244 * "_idx" may be referenced more than once. 245 */ 246#ifdef CHECK_REGISTER_INDICES 247# define GET_REGISTER(_idx) \ 248 ( (_idx) < curMethod->registersSize ? \ 249 (fp[(_idx)]) : (assert(!"bad reg"),1969) ) 250# define SET_REGISTER(_idx, _val) \ 251 ( (_idx) < curMethod->registersSize ? \ 252 (fp[(_idx)] = (u4)(_val)) : (assert(!"bad reg"),1969) ) 253# define GET_REGISTER_AS_OBJECT(_idx) ((Object *)GET_REGISTER(_idx)) 254# define SET_REGISTER_AS_OBJECT(_idx, _val) SET_REGISTER(_idx, (s4)_val) 255# define GET_REGISTER_INT(_idx) ((s4) GET_REGISTER(_idx)) 256# define SET_REGISTER_INT(_idx, _val) SET_REGISTER(_idx, (s4)_val) 257# define GET_REGISTER_WIDE(_idx) \ 258 ( (_idx) < curMethod->registersSize-1 ? \ 259 getLongFromArray(fp, (_idx)) : (assert(!"bad reg"),1969) ) 260# define SET_REGISTER_WIDE(_idx, _val) \ 261 ( (_idx) < curMethod->registersSize-1 ? \ 262 putLongToArray(fp, (_idx), (_val)) : (assert(!"bad reg"),1969) ) 263# define GET_REGISTER_FLOAT(_idx) \ 264 ( (_idx) < curMethod->registersSize ? \ 265 (*((float*) &fp[(_idx)])) : (assert(!"bad reg"),1969.0f) ) 266# define SET_REGISTER_FLOAT(_idx, _val) \ 267 ( (_idx) < curMethod->registersSize ? \ 268 (*((float*) &fp[(_idx)]) = (_val)) : (assert(!"bad reg"),1969.0f) ) 269# define GET_REGISTER_DOUBLE(_idx) \ 270 ( (_idx) < curMethod->registersSize-1 ? \ 271 getDoubleFromArray(fp, (_idx)) : (assert(!"bad reg"),1969.0) ) 272# define SET_REGISTER_DOUBLE(_idx, _val) \ 273 ( (_idx) < curMethod->registersSize-1 ? \ 274 putDoubleToArray(fp, (_idx), (_val)) : (assert(!"bad reg"),1969.0) ) 275#else 276# define GET_REGISTER(_idx) (fp[(_idx)]) 277# define SET_REGISTER(_idx, _val) (fp[(_idx)] = (_val)) 278# define GET_REGISTER_AS_OBJECT(_idx) ((Object*) fp[(_idx)]) 279# define SET_REGISTER_AS_OBJECT(_idx, _val) (fp[(_idx)] = (u4)(_val)) 280# define GET_REGISTER_INT(_idx) ((s4)GET_REGISTER(_idx)) 281# define SET_REGISTER_INT(_idx, _val) SET_REGISTER(_idx, (s4)_val) 282# define GET_REGISTER_WIDE(_idx) getLongFromArray(fp, (_idx)) 283# define SET_REGISTER_WIDE(_idx, _val) putLongToArray(fp, (_idx), (_val)) 284# define GET_REGISTER_FLOAT(_idx) (*((float*) &fp[(_idx)])) 285# define SET_REGISTER_FLOAT(_idx, _val) (*((float*) &fp[(_idx)]) = (_val)) 286# define GET_REGISTER_DOUBLE(_idx) getDoubleFromArray(fp, (_idx)) 287# define SET_REGISTER_DOUBLE(_idx, _val) putDoubleToArray(fp, (_idx), (_val)) 288#endif 289 290/* 291 * Get 16 bits from the specified offset of the program counter. We always 292 * want to load 16 bits at a time from the instruction stream -- it's more 293 * efficient than 8 and won't have the alignment problems that 32 might. 294 * 295 * Assumes existence of "const u2* pc". 296 */ 297#define FETCH(_offset) (pc[(_offset)]) 298 299/* 300 * Extract instruction byte from 16-bit fetch (_inst is a u2). 301 */ 302#define INST_INST(_inst) ((_inst) & 0xff) 303 304/* 305 * Replace the opcode (used when handling breakpoints). _opcode is a u1. 306 */ 307#define INST_REPLACE_OP(_inst, _opcode) (((_inst) & 0xff00) | _opcode) 308 309/* 310 * Extract the "vA, vB" 4-bit registers from the instruction word (_inst is u2). 311 */ 312#define INST_A(_inst) (((_inst) >> 8) & 0x0f) 313#define INST_B(_inst) ((_inst) >> 12) 314 315/* 316 * Get the 8-bit "vAA" 8-bit register index from the instruction word. 317 * (_inst is u2) 318 */ 319#define INST_AA(_inst) ((_inst) >> 8) 320 321/* 322 * The current PC must be available to Throwable constructors, e.g. 323 * those created by dvmThrowException(), so that the exception stack 324 * trace can be generated correctly. If we don't do this, the offset 325 * within the current method won't be shown correctly. See the notes 326 * in Exception.c. 327 * 328 * This is also used to determine the address for precise GC. 329 * 330 * Assumes existence of "u4* fp" and "const u2* pc". 331 */ 332#define EXPORT_PC() (SAVEAREA_FROM_FP(fp)->xtra.currentPc = pc) 333 334/* 335 * Determine if we need to switch to a different interpreter. "_current" 336 * is either INTERP_STD or INTERP_DBG. It should be fixed for a given 337 * interpreter generation file, which should remove the outer conditional 338 * from the following. 339 * 340 * If we're building without debug and profiling support, we never switch. 341 */ 342#if defined(WITH_PROFILER) || defined(WITH_DEBUGGER) 343#if defined(WITH_JIT) 344# define NEED_INTERP_SWITCH(_current) ( \ 345 (_current == INTERP_STD) ? \ 346 dvmJitDebuggerOrProfilerActive() : !dvmJitDebuggerOrProfilerActive() ) 347#else 348# define NEED_INTERP_SWITCH(_current) ( \ 349 (_current == INTERP_STD) ? \ 350 dvmDebuggerOrProfilerActive() : !dvmDebuggerOrProfilerActive() ) 351#endif 352#else 353# define NEED_INTERP_SWITCH(_current) (false) 354#endif 355 356/* 357 * Check to see if "obj" is NULL. If so, throw an exception. Assumes the 358 * pc has already been exported to the stack. 359 * 360 * Perform additional checks on debug builds. 361 * 362 * Use this to check for NULL when the instruction handler calls into 363 * something that could throw an exception (so we have already called 364 * EXPORT_PC at the top). 365 */ 366static inline bool checkForNull(Object* obj) 367{ 368 if (obj == NULL) { 369 dvmThrowException("Ljava/lang/NullPointerException;", NULL); 370 return false; 371 } 372#ifdef WITH_EXTRA_OBJECT_VALIDATION 373 if (!dvmIsValidObject(obj)) { 374 LOGE("Invalid object %p\n", obj); 375 dvmAbort(); 376 } 377#endif 378#ifndef NDEBUG 379 if (obj->clazz == NULL || ((u4) obj->clazz) <= 65536) { 380 /* probable heap corruption */ 381 LOGE("Invalid object class %p (in %p)\n", obj->clazz, obj); 382 dvmAbort(); 383 } 384#endif 385 return true; 386} 387 388/* 389 * Check to see if "obj" is NULL. If so, export the PC into the stack 390 * frame and throw an exception. 391 * 392 * Perform additional checks on debug builds. 393 * 394 * Use this to check for NULL when the instruction handler doesn't do 395 * anything else that can throw an exception. 396 */ 397static inline bool checkForNullExportPC(Object* obj, u4* fp, const u2* pc) 398{ 399 if (obj == NULL) { 400 EXPORT_PC(); 401 dvmThrowException("Ljava/lang/NullPointerException;", NULL); 402 return false; 403 } 404#ifdef WITH_EXTRA_OBJECT_VALIDATION 405 if (!dvmIsValidObject(obj)) { 406 LOGE("Invalid object %p\n", obj); 407 dvmAbort(); 408 } 409#endif 410#ifndef NDEBUG 411 if (obj->clazz == NULL || ((u4) obj->clazz) <= 65536) { 412 /* probable heap corruption */ 413 LOGE("Invalid object class %p (in %p)\n", obj->clazz, obj); 414 dvmAbort(); 415 } 416#endif 417 return true; 418} 419 420/* File: cstubs/stubdefs.c */ 421/* this is a standard (no debug support) interpreter */ 422#define INTERP_TYPE INTERP_STD 423#define CHECK_DEBUG_AND_PROF() ((void)0) 424# define CHECK_TRACKED_REFS() ((void)0) 425#if defined(WITH_JIT) 426#define CHECK_JIT() (0) 427#define ABORT_JIT_TSELECT() ((void)0) 428#endif 429 430/* 431 * In the C mterp stubs, "goto" is a function call followed immediately 432 * by a return. 433 */ 434 435#define GOTO_TARGET_DECL(_target, ...) \ 436 void dvmMterp_##_target(MterpGlue* glue, ## __VA_ARGS__); 437 438#define GOTO_TARGET(_target, ...) \ 439 void dvmMterp_##_target(MterpGlue* glue, ## __VA_ARGS__) { \ 440 u2 ref, vsrc1, vsrc2, vdst; \ 441 u2 inst = FETCH(0); \ 442 const Method* methodToCall; \ 443 StackSaveArea* debugSaveArea; 444 445#define GOTO_TARGET_END } 446 447/* 448 * Redefine what used to be local variable accesses into MterpGlue struct 449 * references. (These are undefined down in "footer.c".) 450 */ 451#define retval glue->retval 452#define pc glue->pc 453#define fp glue->fp 454#define curMethod glue->method 455#define methodClassDex glue->methodClassDex 456#define self glue->self 457#define debugTrackedRefStart glue->debugTrackedRefStart 458 459/* ugh */ 460#define STUB_HACK(x) x 461 462 463/* 464 * Opcode handler framing macros. Here, each opcode is a separate function 465 * that takes a "glue" argument and returns void. We can't declare 466 * these "static" because they may be called from an assembly stub. 467 */ 468#define HANDLE_OPCODE(_op) \ 469 void dvmMterp_##_op(MterpGlue* glue) { \ 470 u2 ref, vsrc1, vsrc2, vdst; \ 471 u2 inst = FETCH(0); 472 473#define OP_END } 474 475/* 476 * Like the "portable" FINISH, but don't reload "inst", and return to caller 477 * when done. 478 */ 479#define FINISH(_offset) { \ 480 ADJUST_PC(_offset); \ 481 CHECK_DEBUG_AND_PROF(); \ 482 CHECK_TRACKED_REFS(); \ 483 return; \ 484 } 485 486 487/* 488 * The "goto label" statements turn into function calls followed by 489 * return statements. Some of the functions take arguments, which in the 490 * portable interpreter are handled by assigning values to globals. 491 */ 492 493#define GOTO_exceptionThrown() \ 494 do { \ 495 dvmMterp_exceptionThrown(glue); \ 496 return; \ 497 } while(false) 498 499#define GOTO_returnFromMethod() \ 500 do { \ 501 dvmMterp_returnFromMethod(glue); \ 502 return; \ 503 } while(false) 504 505#define GOTO_invoke(_target, _methodCallRange) \ 506 do { \ 507 dvmMterp_##_target(glue, _methodCallRange); \ 508 return; \ 509 } while(false) 510 511#define GOTO_invokeMethod(_methodCallRange, _methodToCall, _vsrc1, _vdst) \ 512 do { \ 513 dvmMterp_invokeMethod(glue, _methodCallRange, _methodToCall, \ 514 _vsrc1, _vdst); \ 515 return; \ 516 } while(false) 517 518/* 519 * As a special case, "goto bail" turns into a longjmp. Use "bail_switch" 520 * if we need to switch to the other interpreter upon our return. 521 */ 522#define GOTO_bail() \ 523 dvmMterpStdBail(glue, false); 524#define GOTO_bail_switch() \ 525 dvmMterpStdBail(glue, true); 526 527/* 528 * Periodically check for thread suspension. 529 * 530 * While we're at it, see if a debugger has attached or the profiler has 531 * started. If so, switch to a different "goto" table. 532 */ 533#define PERIODIC_CHECKS(_entryPoint, _pcadj) { \ 534 if (dvmCheckSuspendQuick(self)) { \ 535 EXPORT_PC(); /* need for precise GC */ \ 536 dvmCheckSuspendPending(self); \ 537 } \ 538 if (NEED_INTERP_SWITCH(INTERP_TYPE)) { \ 539 ADJUST_PC(_pcadj); \ 540 glue->entryPoint = _entryPoint; \ 541 LOGVV("threadid=%d: switch to STD ep=%d adj=%d\n", \ 542 self->threadId, (_entryPoint), (_pcadj)); \ 543 GOTO_bail_switch(); \ 544 } \ 545 } 546 547/* File: c/opcommon.c */ 548/* forward declarations of goto targets */ 549GOTO_TARGET_DECL(filledNewArray, bool methodCallRange); 550GOTO_TARGET_DECL(invokeVirtual, bool methodCallRange); 551GOTO_TARGET_DECL(invokeSuper, bool methodCallRange); 552GOTO_TARGET_DECL(invokeInterface, bool methodCallRange); 553GOTO_TARGET_DECL(invokeDirect, bool methodCallRange); 554GOTO_TARGET_DECL(invokeStatic, bool methodCallRange); 555GOTO_TARGET_DECL(invokeVirtualQuick, bool methodCallRange); 556GOTO_TARGET_DECL(invokeSuperQuick, bool methodCallRange); 557GOTO_TARGET_DECL(invokeMethod, bool methodCallRange, const Method* methodToCall, 558 u2 count, u2 regs); 559GOTO_TARGET_DECL(returnFromMethod); 560GOTO_TARGET_DECL(exceptionThrown); 561 562/* 563 * =========================================================================== 564 * 565 * What follows are opcode definitions shared between multiple opcodes with 566 * minor substitutions handled by the C pre-processor. These should probably 567 * use the mterp substitution mechanism instead, with the code here moved 568 * into common fragment files (like the asm "binop.S"), although it's hard 569 * to give up the C preprocessor in favor of the much simpler text subst. 570 * 571 * =========================================================================== 572 */ 573 574#define HANDLE_NUMCONV(_opcode, _opname, _fromtype, _totype) \ 575 HANDLE_OPCODE(_opcode /*vA, vB*/) \ 576 vdst = INST_A(inst); \ 577 vsrc1 = INST_B(inst); \ 578 ILOGV("|%s v%d,v%d", (_opname), vdst, vsrc1); \ 579 SET_REGISTER##_totype(vdst, \ 580 GET_REGISTER##_fromtype(vsrc1)); \ 581 FINISH(1); 582 583#define HANDLE_FLOAT_TO_INT(_opcode, _opname, _fromvtype, _fromrtype, \ 584 _tovtype, _tortype) \ 585 HANDLE_OPCODE(_opcode /*vA, vB*/) \ 586 { \ 587 /* spec defines specific handling for +/- inf and NaN values */ \ 588 _fromvtype val; \ 589 _tovtype intMin, intMax, result; \ 590 vdst = INST_A(inst); \ 591 vsrc1 = INST_B(inst); \ 592 ILOGV("|%s v%d,v%d", (_opname), vdst, vsrc1); \ 593 val = GET_REGISTER##_fromrtype(vsrc1); \ 594 intMin = (_tovtype) 1 << (sizeof(_tovtype) * 8 -1); \ 595 intMax = ~intMin; \ 596 result = (_tovtype) val; \ 597 if (val >= intMax) /* +inf */ \ 598 result = intMax; \ 599 else if (val <= intMin) /* -inf */ \ 600 result = intMin; \ 601 else if (val != val) /* NaN */ \ 602 result = 0; \ 603 else \ 604 result = (_tovtype) val; \ 605 SET_REGISTER##_tortype(vdst, result); \ 606 } \ 607 FINISH(1); 608 609#define HANDLE_INT_TO_SMALL(_opcode, _opname, _type) \ 610 HANDLE_OPCODE(_opcode /*vA, vB*/) \ 611 vdst = INST_A(inst); \ 612 vsrc1 = INST_B(inst); \ 613 ILOGV("|int-to-%s v%d,v%d", (_opname), vdst, vsrc1); \ 614 SET_REGISTER(vdst, (_type) GET_REGISTER(vsrc1)); \ 615 FINISH(1); 616 617/* NOTE: the comparison result is always a signed 4-byte integer */ 618#define HANDLE_OP_CMPX(_opcode, _opname, _varType, _type, _nanVal) \ 619 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \ 620 { \ 621 int result; \ 622 u2 regs; \ 623 _varType val1, val2; \ 624 vdst = INST_AA(inst); \ 625 regs = FETCH(1); \ 626 vsrc1 = regs & 0xff; \ 627 vsrc2 = regs >> 8; \ 628 ILOGV("|cmp%s v%d,v%d,v%d", (_opname), vdst, vsrc1, vsrc2); \ 629 val1 = GET_REGISTER##_type(vsrc1); \ 630 val2 = GET_REGISTER##_type(vsrc2); \ 631 if (val1 == val2) \ 632 result = 0; \ 633 else if (val1 < val2) \ 634 result = -1; \ 635 else if (val1 > val2) \ 636 result = 1; \ 637 else \ 638 result = (_nanVal); \ 639 ILOGV("+ result=%d\n", result); \ 640 SET_REGISTER(vdst, result); \ 641 } \ 642 FINISH(2); 643 644#define HANDLE_OP_IF_XX(_opcode, _opname, _cmp) \ 645 HANDLE_OPCODE(_opcode /*vA, vB, +CCCC*/) \ 646 vsrc1 = INST_A(inst); \ 647 vsrc2 = INST_B(inst); \ 648 if ((s4) GET_REGISTER(vsrc1) _cmp (s4) GET_REGISTER(vsrc2)) { \ 649 int branchOffset = (s2)FETCH(1); /* sign-extended */ \ 650 ILOGV("|if-%s v%d,v%d,+0x%04x", (_opname), vsrc1, vsrc2, \ 651 branchOffset); \ 652 ILOGV("> branch taken"); \ 653 if (branchOffset < 0) \ 654 PERIODIC_CHECKS(kInterpEntryInstr, branchOffset); \ 655 FINISH(branchOffset); \ 656 } else { \ 657 ILOGV("|if-%s v%d,v%d,-", (_opname), vsrc1, vsrc2); \ 658 FINISH(2); \ 659 } 660 661#define HANDLE_OP_IF_XXZ(_opcode, _opname, _cmp) \ 662 HANDLE_OPCODE(_opcode /*vAA, +BBBB*/) \ 663 vsrc1 = INST_AA(inst); \ 664 if ((s4) GET_REGISTER(vsrc1) _cmp 0) { \ 665 int branchOffset = (s2)FETCH(1); /* sign-extended */ \ 666 ILOGV("|if-%s v%d,+0x%04x", (_opname), vsrc1, branchOffset); \ 667 ILOGV("> branch taken"); \ 668 if (branchOffset < 0) \ 669 PERIODIC_CHECKS(kInterpEntryInstr, branchOffset); \ 670 FINISH(branchOffset); \ 671 } else { \ 672 ILOGV("|if-%s v%d,-", (_opname), vsrc1); \ 673 FINISH(2); \ 674 } 675 676#define HANDLE_UNOP(_opcode, _opname, _pfx, _sfx, _type) \ 677 HANDLE_OPCODE(_opcode /*vA, vB*/) \ 678 vdst = INST_A(inst); \ 679 vsrc1 = INST_B(inst); \ 680 ILOGV("|%s v%d,v%d", (_opname), vdst, vsrc1); \ 681 SET_REGISTER##_type(vdst, _pfx GET_REGISTER##_type(vsrc1) _sfx); \ 682 FINISH(1); 683 684#define HANDLE_OP_X_INT(_opcode, _opname, _op, _chkdiv) \ 685 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \ 686 { \ 687 u2 srcRegs; \ 688 vdst = INST_AA(inst); \ 689 srcRegs = FETCH(1); \ 690 vsrc1 = srcRegs & 0xff; \ 691 vsrc2 = srcRegs >> 8; \ 692 ILOGV("|%s-int v%d,v%d", (_opname), vdst, vsrc1); \ 693 if (_chkdiv != 0) { \ 694 s4 firstVal, secondVal, result; \ 695 firstVal = GET_REGISTER(vsrc1); \ 696 secondVal = GET_REGISTER(vsrc2); \ 697 if (secondVal == 0) { \ 698 EXPORT_PC(); \ 699 dvmThrowException("Ljava/lang/ArithmeticException;", \ 700 "divide by zero"); \ 701 GOTO_exceptionThrown(); \ 702 } \ 703 if ((u4)firstVal == 0x80000000 && secondVal == -1) { \ 704 if (_chkdiv == 1) \ 705 result = firstVal; /* division */ \ 706 else \ 707 result = 0; /* remainder */ \ 708 } else { \ 709 result = firstVal _op secondVal; \ 710 } \ 711 SET_REGISTER(vdst, result); \ 712 } else { \ 713 /* non-div/rem case */ \ 714 SET_REGISTER(vdst, \ 715 (s4) GET_REGISTER(vsrc1) _op (s4) GET_REGISTER(vsrc2)); \ 716 } \ 717 } \ 718 FINISH(2); 719 720#define HANDLE_OP_SHX_INT(_opcode, _opname, _cast, _op) \ 721 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \ 722 { \ 723 u2 srcRegs; \ 724 vdst = INST_AA(inst); \ 725 srcRegs = FETCH(1); \ 726 vsrc1 = srcRegs & 0xff; \ 727 vsrc2 = srcRegs >> 8; \ 728 ILOGV("|%s-int v%d,v%d", (_opname), vdst, vsrc1); \ 729 SET_REGISTER(vdst, \ 730 _cast GET_REGISTER(vsrc1) _op (GET_REGISTER(vsrc2) & 0x1f)); \ 731 } \ 732 FINISH(2); 733 734#define HANDLE_OP_X_INT_LIT16(_opcode, _opname, _op, _chkdiv) \ 735 HANDLE_OPCODE(_opcode /*vA, vB, #+CCCC*/) \ 736 vdst = INST_A(inst); \ 737 vsrc1 = INST_B(inst); \ 738 vsrc2 = FETCH(1); \ 739 ILOGV("|%s-int/lit16 v%d,v%d,#+0x%04x", \ 740 (_opname), vdst, vsrc1, vsrc2); \ 741 if (_chkdiv != 0) { \ 742 s4 firstVal, result; \ 743 firstVal = GET_REGISTER(vsrc1); \ 744 if ((s2) vsrc2 == 0) { \ 745 EXPORT_PC(); \ 746 dvmThrowException("Ljava/lang/ArithmeticException;", \ 747 "divide by zero"); \ 748 GOTO_exceptionThrown(); \ 749 } \ 750 if ((u4)firstVal == 0x80000000 && ((s2) vsrc2) == -1) { \ 751 /* won't generate /lit16 instr for this; check anyway */ \ 752 if (_chkdiv == 1) \ 753 result = firstVal; /* division */ \ 754 else \ 755 result = 0; /* remainder */ \ 756 } else { \ 757 result = firstVal _op (s2) vsrc2; \ 758 } \ 759 SET_REGISTER(vdst, result); \ 760 } else { \ 761 /* non-div/rem case */ \ 762 SET_REGISTER(vdst, GET_REGISTER(vsrc1) _op (s2) vsrc2); \ 763 } \ 764 FINISH(2); 765 766#define HANDLE_OP_X_INT_LIT8(_opcode, _opname, _op, _chkdiv) \ 767 HANDLE_OPCODE(_opcode /*vAA, vBB, #+CC*/) \ 768 { \ 769 u2 litInfo; \ 770 vdst = INST_AA(inst); \ 771 litInfo = FETCH(1); \ 772 vsrc1 = litInfo & 0xff; \ 773 vsrc2 = litInfo >> 8; /* constant */ \ 774 ILOGV("|%s-int/lit8 v%d,v%d,#+0x%02x", \ 775 (_opname), vdst, vsrc1, vsrc2); \ 776 if (_chkdiv != 0) { \ 777 s4 firstVal, result; \ 778 firstVal = GET_REGISTER(vsrc1); \ 779 if ((s1) vsrc2 == 0) { \ 780 EXPORT_PC(); \ 781 dvmThrowException("Ljava/lang/ArithmeticException;", \ 782 "divide by zero"); \ 783 GOTO_exceptionThrown(); \ 784 } \ 785 if ((u4)firstVal == 0x80000000 && ((s1) vsrc2) == -1) { \ 786 if (_chkdiv == 1) \ 787 result = firstVal; /* division */ \ 788 else \ 789 result = 0; /* remainder */ \ 790 } else { \ 791 result = firstVal _op ((s1) vsrc2); \ 792 } \ 793 SET_REGISTER(vdst, result); \ 794 } else { \ 795 SET_REGISTER(vdst, \ 796 (s4) GET_REGISTER(vsrc1) _op (s1) vsrc2); \ 797 } \ 798 } \ 799 FINISH(2); 800 801#define HANDLE_OP_SHX_INT_LIT8(_opcode, _opname, _cast, _op) \ 802 HANDLE_OPCODE(_opcode /*vAA, vBB, #+CC*/) \ 803 { \ 804 u2 litInfo; \ 805 vdst = INST_AA(inst); \ 806 litInfo = FETCH(1); \ 807 vsrc1 = litInfo & 0xff; \ 808 vsrc2 = litInfo >> 8; /* constant */ \ 809 ILOGV("|%s-int/lit8 v%d,v%d,#+0x%02x", \ 810 (_opname), vdst, vsrc1, vsrc2); \ 811 SET_REGISTER(vdst, \ 812 _cast GET_REGISTER(vsrc1) _op (vsrc2 & 0x1f)); \ 813 } \ 814 FINISH(2); 815 816#define HANDLE_OP_X_INT_2ADDR(_opcode, _opname, _op, _chkdiv) \ 817 HANDLE_OPCODE(_opcode /*vA, vB*/) \ 818 vdst = INST_A(inst); \ 819 vsrc1 = INST_B(inst); \ 820 ILOGV("|%s-int-2addr v%d,v%d", (_opname), vdst, vsrc1); \ 821 if (_chkdiv != 0) { \ 822 s4 firstVal, secondVal, result; \ 823 firstVal = GET_REGISTER(vdst); \ 824 secondVal = GET_REGISTER(vsrc1); \ 825 if (secondVal == 0) { \ 826 EXPORT_PC(); \ 827 dvmThrowException("Ljava/lang/ArithmeticException;", \ 828 "divide by zero"); \ 829 GOTO_exceptionThrown(); \ 830 } \ 831 if ((u4)firstVal == 0x80000000 && secondVal == -1) { \ 832 if (_chkdiv == 1) \ 833 result = firstVal; /* division */ \ 834 else \ 835 result = 0; /* remainder */ \ 836 } else { \ 837 result = firstVal _op secondVal; \ 838 } \ 839 SET_REGISTER(vdst, result); \ 840 } else { \ 841 SET_REGISTER(vdst, \ 842 (s4) GET_REGISTER(vdst) _op (s4) GET_REGISTER(vsrc1)); \ 843 } \ 844 FINISH(1); 845 846#define HANDLE_OP_SHX_INT_2ADDR(_opcode, _opname, _cast, _op) \ 847 HANDLE_OPCODE(_opcode /*vA, vB*/) \ 848 vdst = INST_A(inst); \ 849 vsrc1 = INST_B(inst); \ 850 ILOGV("|%s-int-2addr v%d,v%d", (_opname), vdst, vsrc1); \ 851 SET_REGISTER(vdst, \ 852 _cast GET_REGISTER(vdst) _op (GET_REGISTER(vsrc1) & 0x1f)); \ 853 FINISH(1); 854 855#define HANDLE_OP_X_LONG(_opcode, _opname, _op, _chkdiv) \ 856 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \ 857 { \ 858 u2 srcRegs; \ 859 vdst = INST_AA(inst); \ 860 srcRegs = FETCH(1); \ 861 vsrc1 = srcRegs & 0xff; \ 862 vsrc2 = srcRegs >> 8; \ 863 ILOGV("|%s-long v%d,v%d,v%d", (_opname), vdst, vsrc1, vsrc2); \ 864 if (_chkdiv != 0) { \ 865 s8 firstVal, secondVal, result; \ 866 firstVal = GET_REGISTER_WIDE(vsrc1); \ 867 secondVal = GET_REGISTER_WIDE(vsrc2); \ 868 if (secondVal == 0LL) { \ 869 EXPORT_PC(); \ 870 dvmThrowException("Ljava/lang/ArithmeticException;", \ 871 "divide by zero"); \ 872 GOTO_exceptionThrown(); \ 873 } \ 874 if ((u8)firstVal == 0x8000000000000000ULL && \ 875 secondVal == -1LL) \ 876 { \ 877 if (_chkdiv == 1) \ 878 result = firstVal; /* division */ \ 879 else \ 880 result = 0; /* remainder */ \ 881 } else { \ 882 result = firstVal _op secondVal; \ 883 } \ 884 SET_REGISTER_WIDE(vdst, result); \ 885 } else { \ 886 SET_REGISTER_WIDE(vdst, \ 887 (s8) GET_REGISTER_WIDE(vsrc1) _op (s8) GET_REGISTER_WIDE(vsrc2)); \ 888 } \ 889 } \ 890 FINISH(2); 891 892#define HANDLE_OP_SHX_LONG(_opcode, _opname, _cast, _op) \ 893 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \ 894 { \ 895 u2 srcRegs; \ 896 vdst = INST_AA(inst); \ 897 srcRegs = FETCH(1); \ 898 vsrc1 = srcRegs & 0xff; \ 899 vsrc2 = srcRegs >> 8; \ 900 ILOGV("|%s-long v%d,v%d,v%d", (_opname), vdst, vsrc1, vsrc2); \ 901 SET_REGISTER_WIDE(vdst, \ 902 _cast GET_REGISTER_WIDE(vsrc1) _op (GET_REGISTER(vsrc2) & 0x3f)); \ 903 } \ 904 FINISH(2); 905 906#define HANDLE_OP_X_LONG_2ADDR(_opcode, _opname, _op, _chkdiv) \ 907 HANDLE_OPCODE(_opcode /*vA, vB*/) \ 908 vdst = INST_A(inst); \ 909 vsrc1 = INST_B(inst); \ 910 ILOGV("|%s-long-2addr v%d,v%d", (_opname), vdst, vsrc1); \ 911 if (_chkdiv != 0) { \ 912 s8 firstVal, secondVal, result; \ 913 firstVal = GET_REGISTER_WIDE(vdst); \ 914 secondVal = GET_REGISTER_WIDE(vsrc1); \ 915 if (secondVal == 0LL) { \ 916 EXPORT_PC(); \ 917 dvmThrowException("Ljava/lang/ArithmeticException;", \ 918 "divide by zero"); \ 919 GOTO_exceptionThrown(); \ 920 } \ 921 if ((u8)firstVal == 0x8000000000000000ULL && \ 922 secondVal == -1LL) \ 923 { \ 924 if (_chkdiv == 1) \ 925 result = firstVal; /* division */ \ 926 else \ 927 result = 0; /* remainder */ \ 928 } else { \ 929 result = firstVal _op secondVal; \ 930 } \ 931 SET_REGISTER_WIDE(vdst, result); \ 932 } else { \ 933 SET_REGISTER_WIDE(vdst, \ 934 (s8) GET_REGISTER_WIDE(vdst) _op (s8)GET_REGISTER_WIDE(vsrc1));\ 935 } \ 936 FINISH(1); 937 938#define HANDLE_OP_SHX_LONG_2ADDR(_opcode, _opname, _cast, _op) \ 939 HANDLE_OPCODE(_opcode /*vA, vB*/) \ 940 vdst = INST_A(inst); \ 941 vsrc1 = INST_B(inst); \ 942 ILOGV("|%s-long-2addr v%d,v%d", (_opname), vdst, vsrc1); \ 943 SET_REGISTER_WIDE(vdst, \ 944 _cast GET_REGISTER_WIDE(vdst) _op (GET_REGISTER(vsrc1) & 0x3f)); \ 945 FINISH(1); 946 947#define HANDLE_OP_X_FLOAT(_opcode, _opname, _op) \ 948 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \ 949 { \ 950 u2 srcRegs; \ 951 vdst = INST_AA(inst); \ 952 srcRegs = FETCH(1); \ 953 vsrc1 = srcRegs & 0xff; \ 954 vsrc2 = srcRegs >> 8; \ 955 ILOGV("|%s-float v%d,v%d,v%d", (_opname), vdst, vsrc1, vsrc2); \ 956 SET_REGISTER_FLOAT(vdst, \ 957 GET_REGISTER_FLOAT(vsrc1) _op GET_REGISTER_FLOAT(vsrc2)); \ 958 } \ 959 FINISH(2); 960 961#define HANDLE_OP_X_DOUBLE(_opcode, _opname, _op) \ 962 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \ 963 { \ 964 u2 srcRegs; \ 965 vdst = INST_AA(inst); \ 966 srcRegs = FETCH(1); \ 967 vsrc1 = srcRegs & 0xff; \ 968 vsrc2 = srcRegs >> 8; \ 969 ILOGV("|%s-double v%d,v%d,v%d", (_opname), vdst, vsrc1, vsrc2); \ 970 SET_REGISTER_DOUBLE(vdst, \ 971 GET_REGISTER_DOUBLE(vsrc1) _op GET_REGISTER_DOUBLE(vsrc2)); \ 972 } \ 973 FINISH(2); 974 975#define HANDLE_OP_X_FLOAT_2ADDR(_opcode, _opname, _op) \ 976 HANDLE_OPCODE(_opcode /*vA, vB*/) \ 977 vdst = INST_A(inst); \ 978 vsrc1 = INST_B(inst); \ 979 ILOGV("|%s-float-2addr v%d,v%d", (_opname), vdst, vsrc1); \ 980 SET_REGISTER_FLOAT(vdst, \ 981 GET_REGISTER_FLOAT(vdst) _op GET_REGISTER_FLOAT(vsrc1)); \ 982 FINISH(1); 983 984#define HANDLE_OP_X_DOUBLE_2ADDR(_opcode, _opname, _op) \ 985 HANDLE_OPCODE(_opcode /*vA, vB*/) \ 986 vdst = INST_A(inst); \ 987 vsrc1 = INST_B(inst); \ 988 ILOGV("|%s-double-2addr v%d,v%d", (_opname), vdst, vsrc1); \ 989 SET_REGISTER_DOUBLE(vdst, \ 990 GET_REGISTER_DOUBLE(vdst) _op GET_REGISTER_DOUBLE(vsrc1)); \ 991 FINISH(1); 992 993#define HANDLE_OP_AGET(_opcode, _opname, _type, _regsize) \ 994 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \ 995 { \ 996 ArrayObject* arrayObj; \ 997 u2 arrayInfo; \ 998 EXPORT_PC(); \ 999 vdst = INST_AA(inst); \ 1000 arrayInfo = FETCH(1); \ 1001 vsrc1 = arrayInfo & 0xff; /* array ptr */ \ 1002 vsrc2 = arrayInfo >> 8; /* index */ \ 1003 ILOGV("|aget%s v%d,v%d,v%d", (_opname), vdst, vsrc1, vsrc2); \ 1004 arrayObj = (ArrayObject*) GET_REGISTER(vsrc1); \ 1005 if (!checkForNull((Object*) arrayObj)) \ 1006 GOTO_exceptionThrown(); \ 1007 if (GET_REGISTER(vsrc2) >= arrayObj->length) { \ 1008 LOGV("Invalid array access: %p %d (len=%d)\n", \ 1009 arrayObj, vsrc2, arrayObj->length); \ 1010 dvmThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", \ 1011 NULL); \ 1012 GOTO_exceptionThrown(); \ 1013 } \ 1014 SET_REGISTER##_regsize(vdst, \ 1015 ((_type*) arrayObj->contents)[GET_REGISTER(vsrc2)]); \ 1016 ILOGV("+ AGET[%d]=0x%x", GET_REGISTER(vsrc2), GET_REGISTER(vdst)); \ 1017 } \ 1018 FINISH(2); 1019 1020#define HANDLE_OP_APUT(_opcode, _opname, _type, _regsize) \ 1021 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \ 1022 { \ 1023 ArrayObject* arrayObj; \ 1024 u2 arrayInfo; \ 1025 EXPORT_PC(); \ 1026 vdst = INST_AA(inst); /* AA: source value */ \ 1027 arrayInfo = FETCH(1); \ 1028 vsrc1 = arrayInfo & 0xff; /* BB: array ptr */ \ 1029 vsrc2 = arrayInfo >> 8; /* CC: index */ \ 1030 ILOGV("|aput%s v%d,v%d,v%d", (_opname), vdst, vsrc1, vsrc2); \ 1031 arrayObj = (ArrayObject*) GET_REGISTER(vsrc1); \ 1032 if (!checkForNull((Object*) arrayObj)) \ 1033 GOTO_exceptionThrown(); \ 1034 if (GET_REGISTER(vsrc2) >= arrayObj->length) { \ 1035 dvmThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", \ 1036 NULL); \ 1037 GOTO_exceptionThrown(); \ 1038 } \ 1039 ILOGV("+ APUT[%d]=0x%08x", GET_REGISTER(vsrc2), GET_REGISTER(vdst));\ 1040 ((_type*) arrayObj->contents)[GET_REGISTER(vsrc2)] = \ 1041 GET_REGISTER##_regsize(vdst); \ 1042 } \ 1043 FINISH(2); 1044 1045/* 1046 * It's possible to get a bad value out of a field with sub-32-bit stores 1047 * because the -quick versions always operate on 32 bits. Consider: 1048 * short foo = -1 (sets a 32-bit register to 0xffffffff) 1049 * iput-quick foo (writes all 32 bits to the field) 1050 * short bar = 1 (sets a 32-bit register to 0x00000001) 1051 * iput-short (writes the low 16 bits to the field) 1052 * iget-quick foo (reads all 32 bits from the field, yielding 0xffff0001) 1053 * This can only happen when optimized and non-optimized code has interleaved 1054 * access to the same field. This is unlikely but possible. 1055 * 1056 * The easiest way to fix this is to always read/write 32 bits at a time. On 1057 * a device with a 16-bit data bus this is sub-optimal. (The alternative 1058 * approach is to have sub-int versions of iget-quick, but now we're wasting 1059 * Dalvik instruction space and making it less likely that handler code will 1060 * already be in the CPU i-cache.) 1061 */ 1062#define HANDLE_IGET_X(_opcode, _opname, _ftype, _regsize) \ 1063 HANDLE_OPCODE(_opcode /*vA, vB, field@CCCC*/) \ 1064 { \ 1065 InstField* ifield; \ 1066 Object* obj; \ 1067 EXPORT_PC(); \ 1068 vdst = INST_A(inst); \ 1069 vsrc1 = INST_B(inst); /* object ptr */ \ 1070 ref = FETCH(1); /* field ref */ \ 1071 ILOGV("|iget%s v%d,v%d,field@0x%04x", (_opname), vdst, vsrc1, ref); \ 1072 obj = (Object*) GET_REGISTER(vsrc1); \ 1073 if (!checkForNull(obj)) \ 1074 GOTO_exceptionThrown(); \ 1075 ifield = (InstField*) dvmDexGetResolvedField(methodClassDex, ref); \ 1076 if (ifield == NULL) { \ 1077 ifield = dvmResolveInstField(curMethod->clazz, ref); \ 1078 if (ifield == NULL) \ 1079 GOTO_exceptionThrown(); \ 1080 } \ 1081 SET_REGISTER##_regsize(vdst, \ 1082 dvmGetField##_ftype(obj, ifield->byteOffset)); \ 1083 ILOGV("+ IGET '%s'=0x%08llx", ifield->field.name, \ 1084 (u8) GET_REGISTER##_regsize(vdst)); \ 1085 UPDATE_FIELD_GET(&ifield->field); \ 1086 } \ 1087 FINISH(2); 1088 1089#define HANDLE_IGET_X_QUICK(_opcode, _opname, _ftype, _regsize) \ 1090 HANDLE_OPCODE(_opcode /*vA, vB, field@CCCC*/) \ 1091 { \ 1092 Object* obj; \ 1093 vdst = INST_A(inst); \ 1094 vsrc1 = INST_B(inst); /* object ptr */ \ 1095 ref = FETCH(1); /* field offset */ \ 1096 ILOGV("|iget%s-quick v%d,v%d,field@+%u", \ 1097 (_opname), vdst, vsrc1, ref); \ 1098 obj = (Object*) GET_REGISTER(vsrc1); \ 1099 if (!checkForNullExportPC(obj, fp, pc)) \ 1100 GOTO_exceptionThrown(); \ 1101 SET_REGISTER##_regsize(vdst, dvmGetField##_ftype(obj, ref)); \ 1102 ILOGV("+ IGETQ %d=0x%08llx", ref, \ 1103 (u8) GET_REGISTER##_regsize(vdst)); \ 1104 } \ 1105 FINISH(2); 1106 1107#define HANDLE_IPUT_X(_opcode, _opname, _ftype, _regsize) \ 1108 HANDLE_OPCODE(_opcode /*vA, vB, field@CCCC*/) \ 1109 { \ 1110 InstField* ifield; \ 1111 Object* obj; \ 1112 EXPORT_PC(); \ 1113 vdst = INST_A(inst); \ 1114 vsrc1 = INST_B(inst); /* object ptr */ \ 1115 ref = FETCH(1); /* field ref */ \ 1116 ILOGV("|iput%s v%d,v%d,field@0x%04x", (_opname), vdst, vsrc1, ref); \ 1117 obj = (Object*) GET_REGISTER(vsrc1); \ 1118 if (!checkForNull(obj)) \ 1119 GOTO_exceptionThrown(); \ 1120 ifield = (InstField*) dvmDexGetResolvedField(methodClassDex, ref); \ 1121 if (ifield == NULL) { \ 1122 ifield = dvmResolveInstField(curMethod->clazz, ref); \ 1123 if (ifield == NULL) \ 1124 GOTO_exceptionThrown(); \ 1125 } \ 1126 dvmSetField##_ftype(obj, ifield->byteOffset, \ 1127 GET_REGISTER##_regsize(vdst)); \ 1128 ILOGV("+ IPUT '%s'=0x%08llx", ifield->field.name, \ 1129 (u8) GET_REGISTER##_regsize(vdst)); \ 1130 UPDATE_FIELD_PUT(&ifield->field); \ 1131 } \ 1132 FINISH(2); 1133 1134#define HANDLE_IPUT_X_QUICK(_opcode, _opname, _ftype, _regsize) \ 1135 HANDLE_OPCODE(_opcode /*vA, vB, field@CCCC*/) \ 1136 { \ 1137 Object* obj; \ 1138 vdst = INST_A(inst); \ 1139 vsrc1 = INST_B(inst); /* object ptr */ \ 1140 ref = FETCH(1); /* field offset */ \ 1141 ILOGV("|iput%s-quick v%d,v%d,field@0x%04x", \ 1142 (_opname), vdst, vsrc1, ref); \ 1143 obj = (Object*) GET_REGISTER(vsrc1); \ 1144 if (!checkForNullExportPC(obj, fp, pc)) \ 1145 GOTO_exceptionThrown(); \ 1146 dvmSetField##_ftype(obj, ref, GET_REGISTER##_regsize(vdst)); \ 1147 ILOGV("+ IPUTQ %d=0x%08llx", ref, \ 1148 (u8) GET_REGISTER##_regsize(vdst)); \ 1149 } \ 1150 FINISH(2); 1151 1152#define HANDLE_SGET_X(_opcode, _opname, _ftype, _regsize) \ 1153 HANDLE_OPCODE(_opcode /*vAA, field@BBBB*/) \ 1154 { \ 1155 StaticField* sfield; \ 1156 vdst = INST_AA(inst); \ 1157 ref = FETCH(1); /* field ref */ \ 1158 ILOGV("|sget%s v%d,sfield@0x%04x", (_opname), vdst, ref); \ 1159 sfield = (StaticField*)dvmDexGetResolvedField(methodClassDex, ref); \ 1160 if (sfield == NULL) { \ 1161 EXPORT_PC(); \ 1162 sfield = dvmResolveStaticField(curMethod->clazz, ref); \ 1163 if (sfield == NULL) \ 1164 GOTO_exceptionThrown(); \ 1165 } \ 1166 SET_REGISTER##_regsize(vdst, dvmGetStaticField##_ftype(sfield)); \ 1167 ILOGV("+ SGET '%s'=0x%08llx", \ 1168 sfield->field.name, (u8)GET_REGISTER##_regsize(vdst)); \ 1169 UPDATE_FIELD_GET(&sfield->field); \ 1170 } \ 1171 FINISH(2); 1172 1173#define HANDLE_SPUT_X(_opcode, _opname, _ftype, _regsize) \ 1174 HANDLE_OPCODE(_opcode /*vAA, field@BBBB*/) \ 1175 { \ 1176 StaticField* sfield; \ 1177 vdst = INST_AA(inst); \ 1178 ref = FETCH(1); /* field ref */ \ 1179 ILOGV("|sput%s v%d,sfield@0x%04x", (_opname), vdst, ref); \ 1180 sfield = (StaticField*)dvmDexGetResolvedField(methodClassDex, ref); \ 1181 if (sfield == NULL) { \ 1182 EXPORT_PC(); \ 1183 sfield = dvmResolveStaticField(curMethod->clazz, ref); \ 1184 if (sfield == NULL) \ 1185 GOTO_exceptionThrown(); \ 1186 } \ 1187 dvmSetStaticField##_ftype(sfield, GET_REGISTER##_regsize(vdst)); \ 1188 ILOGV("+ SPUT '%s'=0x%08llx", \ 1189 sfield->field.name, (u8)GET_REGISTER##_regsize(vdst)); \ 1190 UPDATE_FIELD_PUT(&sfield->field); \ 1191 } \ 1192 FINISH(2); 1193 1194 1195/* File: c/OP_EXECUTE_INLINE_RANGE.c */ 1196HANDLE_OPCODE(OP_EXECUTE_INLINE_RANGE /*{vCCCC..v(CCCC+AA-1)}, inline@BBBB*/) 1197 { 1198 u4 arg0, arg1, arg2, arg3; 1199 arg0 = arg1 = arg2 = arg3 = 0; /* placate gcc */ 1200 1201 EXPORT_PC(); 1202 1203 vsrc1 = INST_AA(inst); /* #of args */ 1204 ref = FETCH(1); /* inline call "ref" */ 1205 vdst = FETCH(2); /* range base */ 1206 ILOGV("|execute-inline-range args=%d @%d {regs=v%d-v%d}", 1207 vsrc1, ref, vdst, vdst+vsrc1-1); 1208 1209 assert((vdst >> 16) == 0); // 16-bit type -or- high 16 bits clear 1210 assert(vsrc1 <= 4); 1211 1212 switch (vsrc1) { 1213 case 4: 1214 arg3 = GET_REGISTER(vdst+3); 1215 /* fall through */ 1216 case 3: 1217 arg2 = GET_REGISTER(vdst+2); 1218 /* fall through */ 1219 case 2: 1220 arg1 = GET_REGISTER(vdst+1); 1221 /* fall through */ 1222 case 1: 1223 arg0 = GET_REGISTER(vdst+0); 1224 /* fall through */ 1225 default: // case 0 1226 ; 1227 } 1228 1229#if INTERP_TYPE == INTERP_DBG 1230 if (!dvmPerformInlineOp4Dbg(arg0, arg1, arg2, arg3, &retval, ref)) 1231 GOTO_exceptionThrown(); 1232#else 1233 if (!dvmPerformInlineOp4Std(arg0, arg1, arg2, arg3, &retval, ref)) 1234 GOTO_exceptionThrown(); 1235#endif 1236 } 1237 FINISH(3); 1238OP_END 1239 1240/* File: c/gotoTargets.c */ 1241/* 1242 * C footer. This has some common code shared by the various targets. 1243 */ 1244 1245/* 1246 * Everything from here on is a "goto target". In the basic interpreter 1247 * we jump into these targets and then jump directly to the handler for 1248 * next instruction. Here, these are subroutines that return to the caller. 1249 */ 1250 1251GOTO_TARGET(filledNewArray, bool methodCallRange) 1252 { 1253 ClassObject* arrayClass; 1254 ArrayObject* newArray; 1255 u4* contents; 1256 char typeCh; 1257 int i; 1258 u4 arg5; 1259 1260 EXPORT_PC(); 1261 1262 ref = FETCH(1); /* class ref */ 1263 vdst = FETCH(2); /* first 4 regs -or- range base */ 1264 1265 if (methodCallRange) { 1266 vsrc1 = INST_AA(inst); /* #of elements */ 1267 arg5 = -1; /* silence compiler warning */ 1268 ILOGV("|filled-new-array-range args=%d @0x%04x {regs=v%d-v%d}", 1269 vsrc1, ref, vdst, vdst+vsrc1-1); 1270 } else { 1271 arg5 = INST_A(inst); 1272 vsrc1 = INST_B(inst); /* #of elements */ 1273 ILOGV("|filled-new-array args=%d @0x%04x {regs=0x%04x %x}", 1274 vsrc1, ref, vdst, arg5); 1275 } 1276 1277 /* 1278 * Resolve the array class. 1279 */ 1280 arrayClass = dvmDexGetResolvedClass(methodClassDex, ref); 1281 if (arrayClass == NULL) { 1282 arrayClass = dvmResolveClass(curMethod->clazz, ref, false); 1283 if (arrayClass == NULL) 1284 GOTO_exceptionThrown(); 1285 } 1286 /* 1287 if (!dvmIsArrayClass(arrayClass)) { 1288 dvmThrowException("Ljava/lang/RuntimeError;", 1289 "filled-new-array needs array class"); 1290 GOTO_exceptionThrown(); 1291 } 1292 */ 1293 /* verifier guarantees this is an array class */ 1294 assert(dvmIsArrayClass(arrayClass)); 1295 assert(dvmIsClassInitialized(arrayClass)); 1296 1297 /* 1298 * Create an array of the specified type. 1299 */ 1300 LOGVV("+++ filled-new-array type is '%s'\n", arrayClass->descriptor); 1301 typeCh = arrayClass->descriptor[1]; 1302 if (typeCh == 'D' || typeCh == 'J') { 1303 /* category 2 primitives not allowed */ 1304 dvmThrowException("Ljava/lang/RuntimeError;", 1305 "bad filled array req"); 1306 GOTO_exceptionThrown(); 1307 } else if (typeCh != 'L' && typeCh != '[' && typeCh != 'I') { 1308 /* TODO: requires multiple "fill in" loops with different widths */ 1309 LOGE("non-int primitives not implemented\n"); 1310 dvmThrowException("Ljava/lang/InternalError;", 1311 "filled-new-array not implemented for anything but 'int'"); 1312 GOTO_exceptionThrown(); 1313 } 1314 1315 newArray = dvmAllocArrayByClass(arrayClass, vsrc1, ALLOC_DONT_TRACK); 1316 if (newArray == NULL) 1317 GOTO_exceptionThrown(); 1318 1319 /* 1320 * Fill in the elements. It's legal for vsrc1 to be zero. 1321 */ 1322 contents = (u4*) newArray->contents; 1323 if (methodCallRange) { 1324 for (i = 0; i < vsrc1; i++) 1325 contents[i] = GET_REGISTER(vdst+i); 1326 } else { 1327 assert(vsrc1 <= 5); 1328 if (vsrc1 == 5) { 1329 contents[4] = GET_REGISTER(arg5); 1330 vsrc1--; 1331 } 1332 for (i = 0; i < vsrc1; i++) { 1333 contents[i] = GET_REGISTER(vdst & 0x0f); 1334 vdst >>= 4; 1335 } 1336 } 1337 1338 retval.l = newArray; 1339 } 1340 FINISH(3); 1341GOTO_TARGET_END 1342 1343 1344GOTO_TARGET(invokeVirtual, bool methodCallRange) 1345 { 1346 Method* baseMethod; 1347 Object* thisPtr; 1348 1349 EXPORT_PC(); 1350 1351 vsrc1 = INST_AA(inst); /* AA (count) or BA (count + arg 5) */ 1352 ref = FETCH(1); /* method ref */ 1353 vdst = FETCH(2); /* 4 regs -or- first reg */ 1354 1355 /* 1356 * The object against which we are executing a method is always 1357 * in the first argument. 1358 */ 1359 if (methodCallRange) { 1360 assert(vsrc1 > 0); 1361 ILOGV("|invoke-virtual-range args=%d @0x%04x {regs=v%d-v%d}", 1362 vsrc1, ref, vdst, vdst+vsrc1-1); 1363 thisPtr = (Object*) GET_REGISTER(vdst); 1364 } else { 1365 assert((vsrc1>>4) > 0); 1366 ILOGV("|invoke-virtual args=%d @0x%04x {regs=0x%04x %x}", 1367 vsrc1 >> 4, ref, vdst, vsrc1 & 0x0f); 1368 thisPtr = (Object*) GET_REGISTER(vdst & 0x0f); 1369 } 1370 1371 if (!checkForNull(thisPtr)) 1372 GOTO_exceptionThrown(); 1373 1374 /* 1375 * Resolve the method. This is the correct method for the static 1376 * type of the object. We also verify access permissions here. 1377 */ 1378 baseMethod = dvmDexGetResolvedMethod(methodClassDex, ref); 1379 if (baseMethod == NULL) { 1380 baseMethod = dvmResolveMethod(curMethod->clazz, ref,METHOD_VIRTUAL); 1381 if (baseMethod == NULL) { 1382 ILOGV("+ unknown method or access denied\n"); 1383 GOTO_exceptionThrown(); 1384 } 1385 } 1386 1387 /* 1388 * Combine the object we found with the vtable offset in the 1389 * method. 1390 */ 1391 assert(baseMethod->methodIndex < thisPtr->clazz->vtableCount); 1392 methodToCall = thisPtr->clazz->vtable[baseMethod->methodIndex]; 1393 1394#if 0 1395 if (dvmIsAbstractMethod(methodToCall)) { 1396 /* 1397 * This can happen if you create two classes, Base and Sub, where 1398 * Sub is a sub-class of Base. Declare a protected abstract 1399 * method foo() in Base, and invoke foo() from a method in Base. 1400 * Base is an "abstract base class" and is never instantiated 1401 * directly. Now, Override foo() in Sub, and use Sub. This 1402 * Works fine unless Sub stops providing an implementation of 1403 * the method. 1404 */ 1405 dvmThrowException("Ljava/lang/AbstractMethodError;", 1406 "abstract method not implemented"); 1407 GOTO_exceptionThrown(); 1408 } 1409#else 1410 assert(!dvmIsAbstractMethod(methodToCall) || 1411 methodToCall->nativeFunc != NULL); 1412#endif 1413 1414 LOGVV("+++ base=%s.%s virtual[%d]=%s.%s\n", 1415 baseMethod->clazz->descriptor, baseMethod->name, 1416 (u4) baseMethod->methodIndex, 1417 methodToCall->clazz->descriptor, methodToCall->name); 1418 assert(methodToCall != NULL); 1419 1420#if 0 1421 if (vsrc1 != methodToCall->insSize) { 1422 LOGW("WRONG METHOD: base=%s.%s virtual[%d]=%s.%s\n", 1423 baseMethod->clazz->descriptor, baseMethod->name, 1424 (u4) baseMethod->methodIndex, 1425 methodToCall->clazz->descriptor, methodToCall->name); 1426 //dvmDumpClass(baseMethod->clazz); 1427 //dvmDumpClass(methodToCall->clazz); 1428 dvmDumpAllClasses(0); 1429 } 1430#endif 1431 1432 GOTO_invokeMethod(methodCallRange, methodToCall, vsrc1, vdst); 1433 } 1434GOTO_TARGET_END 1435 1436GOTO_TARGET(invokeSuper, bool methodCallRange) 1437 { 1438 Method* baseMethod; 1439 u2 thisReg; 1440 1441 EXPORT_PC(); 1442 1443 vsrc1 = INST_AA(inst); /* AA (count) or BA (count + arg 5) */ 1444 ref = FETCH(1); /* method ref */ 1445 vdst = FETCH(2); /* 4 regs -or- first reg */ 1446 1447 if (methodCallRange) { 1448 ILOGV("|invoke-super-range args=%d @0x%04x {regs=v%d-v%d}", 1449 vsrc1, ref, vdst, vdst+vsrc1-1); 1450 thisReg = vdst; 1451 } else { 1452 ILOGV("|invoke-super args=%d @0x%04x {regs=0x%04x %x}", 1453 vsrc1 >> 4, ref, vdst, vsrc1 & 0x0f); 1454 thisReg = vdst & 0x0f; 1455 } 1456 /* impossible in well-formed code, but we must check nevertheless */ 1457 if (!checkForNull((Object*) GET_REGISTER(thisReg))) 1458 GOTO_exceptionThrown(); 1459 1460 /* 1461 * Resolve the method. This is the correct method for the static 1462 * type of the object. We also verify access permissions here. 1463 * The first arg to dvmResolveMethod() is just the referring class 1464 * (used for class loaders and such), so we don't want to pass 1465 * the superclass into the resolution call. 1466 */ 1467 baseMethod = dvmDexGetResolvedMethod(methodClassDex, ref); 1468 if (baseMethod == NULL) { 1469 baseMethod = dvmResolveMethod(curMethod->clazz, ref,METHOD_VIRTUAL); 1470 if (baseMethod == NULL) { 1471 ILOGV("+ unknown method or access denied\n"); 1472 GOTO_exceptionThrown(); 1473 } 1474 } 1475 1476 /* 1477 * Combine the object we found with the vtable offset in the 1478 * method's class. 1479 * 1480 * We're using the current method's class' superclass, not the 1481 * superclass of "this". This is because we might be executing 1482 * in a method inherited from a superclass, and we want to run 1483 * in that class' superclass. 1484 */ 1485 if (baseMethod->methodIndex >= curMethod->clazz->super->vtableCount) { 1486 /* 1487 * Method does not exist in the superclass. Could happen if 1488 * superclass gets updated. 1489 */ 1490 dvmThrowException("Ljava/lang/NoSuchMethodError;", 1491 baseMethod->name); 1492 GOTO_exceptionThrown(); 1493 } 1494 methodToCall = curMethod->clazz->super->vtable[baseMethod->methodIndex]; 1495#if 0 1496 if (dvmIsAbstractMethod(methodToCall)) { 1497 dvmThrowException("Ljava/lang/AbstractMethodError;", 1498 "abstract method not implemented"); 1499 GOTO_exceptionThrown(); 1500 } 1501#else 1502 assert(!dvmIsAbstractMethod(methodToCall) || 1503 methodToCall->nativeFunc != NULL); 1504#endif 1505 LOGVV("+++ base=%s.%s super-virtual=%s.%s\n", 1506 baseMethod->clazz->descriptor, baseMethod->name, 1507 methodToCall->clazz->descriptor, methodToCall->name); 1508 assert(methodToCall != NULL); 1509 1510 GOTO_invokeMethod(methodCallRange, methodToCall, vsrc1, vdst); 1511 } 1512GOTO_TARGET_END 1513 1514GOTO_TARGET(invokeInterface, bool methodCallRange) 1515 { 1516 Object* thisPtr; 1517 ClassObject* thisClass; 1518 1519 EXPORT_PC(); 1520 1521 vsrc1 = INST_AA(inst); /* AA (count) or BA (count + arg 5) */ 1522 ref = FETCH(1); /* method ref */ 1523 vdst = FETCH(2); /* 4 regs -or- first reg */ 1524 1525 /* 1526 * The object against which we are executing a method is always 1527 * in the first argument. 1528 */ 1529 if (methodCallRange) { 1530 assert(vsrc1 > 0); 1531 ILOGV("|invoke-interface-range args=%d @0x%04x {regs=v%d-v%d}", 1532 vsrc1, ref, vdst, vdst+vsrc1-1); 1533 thisPtr = (Object*) GET_REGISTER(vdst); 1534 } else { 1535 assert((vsrc1>>4) > 0); 1536 ILOGV("|invoke-interface args=%d @0x%04x {regs=0x%04x %x}", 1537 vsrc1 >> 4, ref, vdst, vsrc1 & 0x0f); 1538 thisPtr = (Object*) GET_REGISTER(vdst & 0x0f); 1539 } 1540 if (!checkForNull(thisPtr)) 1541 GOTO_exceptionThrown(); 1542 1543 thisClass = thisPtr->clazz; 1544 1545 /* 1546 * Given a class and a method index, find the Method* with the 1547 * actual code we want to execute. 1548 */ 1549 methodToCall = dvmFindInterfaceMethodInCache(thisClass, ref, curMethod, 1550 methodClassDex); 1551 if (methodToCall == NULL) { 1552 assert(dvmCheckException(self)); 1553 GOTO_exceptionThrown(); 1554 } 1555 1556 GOTO_invokeMethod(methodCallRange, methodToCall, vsrc1, vdst); 1557 } 1558GOTO_TARGET_END 1559 1560GOTO_TARGET(invokeDirect, bool methodCallRange) 1561 { 1562 u2 thisReg; 1563 1564 vsrc1 = INST_AA(inst); /* AA (count) or BA (count + arg 5) */ 1565 ref = FETCH(1); /* method ref */ 1566 vdst = FETCH(2); /* 4 regs -or- first reg */ 1567 1568 EXPORT_PC(); 1569 1570 if (methodCallRange) { 1571 ILOGV("|invoke-direct-range args=%d @0x%04x {regs=v%d-v%d}", 1572 vsrc1, ref, vdst, vdst+vsrc1-1); 1573 thisReg = vdst; 1574 } else { 1575 ILOGV("|invoke-direct args=%d @0x%04x {regs=0x%04x %x}", 1576 vsrc1 >> 4, ref, vdst, vsrc1 & 0x0f); 1577 thisReg = vdst & 0x0f; 1578 } 1579 if (!checkForNull((Object*) GET_REGISTER(thisReg))) 1580 GOTO_exceptionThrown(); 1581 1582 methodToCall = dvmDexGetResolvedMethod(methodClassDex, ref); 1583 if (methodToCall == NULL) { 1584 methodToCall = dvmResolveMethod(curMethod->clazz, ref, 1585 METHOD_DIRECT); 1586 if (methodToCall == NULL) { 1587 ILOGV("+ unknown direct method\n"); // should be impossible 1588 GOTO_exceptionThrown(); 1589 } 1590 } 1591 GOTO_invokeMethod(methodCallRange, methodToCall, vsrc1, vdst); 1592 } 1593GOTO_TARGET_END 1594 1595GOTO_TARGET(invokeStatic, bool methodCallRange) 1596 vsrc1 = INST_AA(inst); /* AA (count) or BA (count + arg 5) */ 1597 ref = FETCH(1); /* method ref */ 1598 vdst = FETCH(2); /* 4 regs -or- first reg */ 1599 1600 EXPORT_PC(); 1601 1602 if (methodCallRange) 1603 ILOGV("|invoke-static-range args=%d @0x%04x {regs=v%d-v%d}", 1604 vsrc1, ref, vdst, vdst+vsrc1-1); 1605 else 1606 ILOGV("|invoke-static args=%d @0x%04x {regs=0x%04x %x}", 1607 vsrc1 >> 4, ref, vdst, vsrc1 & 0x0f); 1608 1609 methodToCall = dvmDexGetResolvedMethod(methodClassDex, ref); 1610 if (methodToCall == NULL) { 1611 methodToCall = dvmResolveMethod(curMethod->clazz, ref, METHOD_STATIC); 1612 if (methodToCall == NULL) { 1613 ILOGV("+ unknown method\n"); 1614 GOTO_exceptionThrown(); 1615 } 1616 } 1617 GOTO_invokeMethod(methodCallRange, methodToCall, vsrc1, vdst); 1618GOTO_TARGET_END 1619 1620GOTO_TARGET(invokeVirtualQuick, bool methodCallRange) 1621 { 1622 Object* thisPtr; 1623 1624 EXPORT_PC(); 1625 1626 vsrc1 = INST_AA(inst); /* AA (count) or BA (count + arg 5) */ 1627 ref = FETCH(1); /* vtable index */ 1628 vdst = FETCH(2); /* 4 regs -or- first reg */ 1629 1630 /* 1631 * The object against which we are executing a method is always 1632 * in the first argument. 1633 */ 1634 if (methodCallRange) { 1635 assert(vsrc1 > 0); 1636 ILOGV("|invoke-virtual-quick-range args=%d @0x%04x {regs=v%d-v%d}", 1637 vsrc1, ref, vdst, vdst+vsrc1-1); 1638 thisPtr = (Object*) GET_REGISTER(vdst); 1639 } else { 1640 assert((vsrc1>>4) > 0); 1641 ILOGV("|invoke-virtual-quick args=%d @0x%04x {regs=0x%04x %x}", 1642 vsrc1 >> 4, ref, vdst, vsrc1 & 0x0f); 1643 thisPtr = (Object*) GET_REGISTER(vdst & 0x0f); 1644 } 1645 1646 if (!checkForNull(thisPtr)) 1647 GOTO_exceptionThrown(); 1648 1649 /* 1650 * Combine the object we found with the vtable offset in the 1651 * method. 1652 */ 1653 assert(ref < thisPtr->clazz->vtableCount); 1654 methodToCall = thisPtr->clazz->vtable[ref]; 1655 1656#if 0 1657 if (dvmIsAbstractMethod(methodToCall)) { 1658 dvmThrowException("Ljava/lang/AbstractMethodError;", 1659 "abstract method not implemented"); 1660 GOTO_exceptionThrown(); 1661 } 1662#else 1663 assert(!dvmIsAbstractMethod(methodToCall) || 1664 methodToCall->nativeFunc != NULL); 1665#endif 1666 1667 LOGVV("+++ virtual[%d]=%s.%s\n", 1668 ref, methodToCall->clazz->descriptor, methodToCall->name); 1669 assert(methodToCall != NULL); 1670 1671 GOTO_invokeMethod(methodCallRange, methodToCall, vsrc1, vdst); 1672 } 1673GOTO_TARGET_END 1674 1675GOTO_TARGET(invokeSuperQuick, bool methodCallRange) 1676 { 1677 u2 thisReg; 1678 1679 EXPORT_PC(); 1680 1681 vsrc1 = INST_AA(inst); /* AA (count) or BA (count + arg 5) */ 1682 ref = FETCH(1); /* vtable index */ 1683 vdst = FETCH(2); /* 4 regs -or- first reg */ 1684 1685 if (methodCallRange) { 1686 ILOGV("|invoke-super-quick-range args=%d @0x%04x {regs=v%d-v%d}", 1687 vsrc1, ref, vdst, vdst+vsrc1-1); 1688 thisReg = vdst; 1689 } else { 1690 ILOGV("|invoke-super-quick args=%d @0x%04x {regs=0x%04x %x}", 1691 vsrc1 >> 4, ref, vdst, vsrc1 & 0x0f); 1692 thisReg = vdst & 0x0f; 1693 } 1694 /* impossible in well-formed code, but we must check nevertheless */ 1695 if (!checkForNull((Object*) GET_REGISTER(thisReg))) 1696 GOTO_exceptionThrown(); 1697 1698#if 0 /* impossible in optimized + verified code */ 1699 if (ref >= curMethod->clazz->super->vtableCount) { 1700 dvmThrowException("Ljava/lang/NoSuchMethodError;", NULL); 1701 GOTO_exceptionThrown(); 1702 } 1703#else 1704 assert(ref < curMethod->clazz->super->vtableCount); 1705#endif 1706 1707 /* 1708 * Combine the object we found with the vtable offset in the 1709 * method's class. 1710 * 1711 * We're using the current method's class' superclass, not the 1712 * superclass of "this". This is because we might be executing 1713 * in a method inherited from a superclass, and we want to run 1714 * in the method's class' superclass. 1715 */ 1716 methodToCall = curMethod->clazz->super->vtable[ref]; 1717 1718#if 0 1719 if (dvmIsAbstractMethod(methodToCall)) { 1720 dvmThrowException("Ljava/lang/AbstractMethodError;", 1721 "abstract method not implemented"); 1722 GOTO_exceptionThrown(); 1723 } 1724#else 1725 assert(!dvmIsAbstractMethod(methodToCall) || 1726 methodToCall->nativeFunc != NULL); 1727#endif 1728 LOGVV("+++ super-virtual[%d]=%s.%s\n", 1729 ref, methodToCall->clazz->descriptor, methodToCall->name); 1730 assert(methodToCall != NULL); 1731 1732 GOTO_invokeMethod(methodCallRange, methodToCall, vsrc1, vdst); 1733 } 1734GOTO_TARGET_END 1735 1736 1737 1738 /* 1739 * General handling for return-void, return, and return-wide. Put the 1740 * return value in "retval" before jumping here. 1741 */ 1742GOTO_TARGET(returnFromMethod) 1743 { 1744 StackSaveArea* saveArea; 1745 1746 /* 1747 * We must do this BEFORE we pop the previous stack frame off, so 1748 * that the GC can see the return value (if any) in the local vars. 1749 * 1750 * Since this is now an interpreter switch point, we must do it before 1751 * we do anything at all. 1752 */ 1753 PERIODIC_CHECKS(kInterpEntryReturn, 0); 1754 1755 ILOGV("> retval=0x%llx (leaving %s.%s %s)", 1756 retval.j, curMethod->clazz->descriptor, curMethod->name, 1757 curMethod->shorty); 1758 //DUMP_REGS(curMethod, fp); 1759 1760 saveArea = SAVEAREA_FROM_FP(fp); 1761 1762#ifdef EASY_GDB 1763 debugSaveArea = saveArea; 1764#endif 1765#if (INTERP_TYPE == INTERP_DBG) && defined(WITH_PROFILER) 1766 TRACE_METHOD_EXIT(self, curMethod); 1767#endif 1768 1769 /* back up to previous frame and see if we hit a break */ 1770 fp = saveArea->prevFrame; 1771 assert(fp != NULL); 1772 if (dvmIsBreakFrame(fp)) { 1773 /* bail without popping the method frame from stack */ 1774 LOGVV("+++ returned into break frame\n"); 1775#if defined(WITH_JIT) 1776 /* Let the Jit know the return is terminating normally */ 1777 CHECK_JIT(); 1778#endif 1779 GOTO_bail(); 1780 } 1781 1782 /* update thread FP, and reset local variables */ 1783 self->curFrame = fp; 1784 curMethod = SAVEAREA_FROM_FP(fp)->method; 1785 //methodClass = curMethod->clazz; 1786 methodClassDex = curMethod->clazz->pDvmDex; 1787 pc = saveArea->savedPc; 1788 ILOGD("> (return to %s.%s %s)", curMethod->clazz->descriptor, 1789 curMethod->name, curMethod->shorty); 1790 1791 /* use FINISH on the caller's invoke instruction */ 1792 //u2 invokeInstr = INST_INST(FETCH(0)); 1793 if (true /*invokeInstr >= OP_INVOKE_VIRTUAL && 1794 invokeInstr <= OP_INVOKE_INTERFACE*/) 1795 { 1796 FINISH(3); 1797 } else { 1798 //LOGE("Unknown invoke instr %02x at %d\n", 1799 // invokeInstr, (int) (pc - curMethod->insns)); 1800 assert(false); 1801 } 1802 } 1803GOTO_TARGET_END 1804 1805 1806 /* 1807 * Jump here when the code throws an exception. 1808 * 1809 * By the time we get here, the Throwable has been created and the stack 1810 * trace has been saved off. 1811 */ 1812GOTO_TARGET(exceptionThrown) 1813 { 1814 Object* exception; 1815 int catchRelPc; 1816 1817 /* 1818 * Since this is now an interpreter switch point, we must do it before 1819 * we do anything at all. 1820 */ 1821 PERIODIC_CHECKS(kInterpEntryThrow, 0); 1822 1823#if defined(WITH_JIT) 1824 // Something threw during trace selection - abort the current trace 1825 ABORT_JIT_TSELECT(); 1826#endif 1827 /* 1828 * We save off the exception and clear the exception status. While 1829 * processing the exception we might need to load some Throwable 1830 * classes, and we don't want class loader exceptions to get 1831 * confused with this one. 1832 */ 1833 assert(dvmCheckException(self)); 1834 exception = dvmGetException(self); 1835 dvmAddTrackedAlloc(exception, self); 1836 dvmClearException(self); 1837 1838 LOGV("Handling exception %s at %s:%d\n", 1839 exception->clazz->descriptor, curMethod->name, 1840 dvmLineNumFromPC(curMethod, pc - curMethod->insns)); 1841 1842#if (INTERP_TYPE == INTERP_DBG) && defined(WITH_DEBUGGER) 1843 /* 1844 * Tell the debugger about it. 1845 * 1846 * TODO: if the exception was thrown by interpreted code, control 1847 * fell through native, and then back to us, we will report the 1848 * exception at the point of the throw and again here. We can avoid 1849 * this by not reporting exceptions when we jump here directly from 1850 * the native call code above, but then we won't report exceptions 1851 * that were thrown *from* the JNI code (as opposed to *through* it). 1852 * 1853 * The correct solution is probably to ignore from-native exceptions 1854 * here, and have the JNI exception code do the reporting to the 1855 * debugger. 1856 */ 1857 if (gDvm.debuggerActive) { 1858 void* catchFrame; 1859 catchRelPc = dvmFindCatchBlock(self, pc - curMethod->insns, 1860 exception, true, &catchFrame); 1861 dvmDbgPostException(fp, pc - curMethod->insns, catchFrame, 1862 catchRelPc, exception); 1863 } 1864#endif 1865 1866 /* 1867 * We need to unroll to the catch block or the nearest "break" 1868 * frame. 1869 * 1870 * A break frame could indicate that we have reached an intermediate 1871 * native call, or have gone off the top of the stack and the thread 1872 * needs to exit. Either way, we return from here, leaving the 1873 * exception raised. 1874 * 1875 * If we do find a catch block, we want to transfer execution to 1876 * that point. 1877 */ 1878 catchRelPc = dvmFindCatchBlock(self, pc - curMethod->insns, 1879 exception, false, (void*)&fp); 1880 1881 /* 1882 * Restore the stack bounds after an overflow. This isn't going to 1883 * be correct in all circumstances, e.g. if JNI code devours the 1884 * exception this won't happen until some other exception gets 1885 * thrown. If the code keeps pushing the stack bounds we'll end 1886 * up aborting the VM. 1887 * 1888 * Note we want to do this *after* the call to dvmFindCatchBlock, 1889 * because that may need extra stack space to resolve exception 1890 * classes (e.g. through a class loader). 1891 */ 1892 if (self->stackOverflowed) 1893 dvmCleanupStackOverflow(self); 1894 1895 if (catchRelPc < 0) { 1896 /* falling through to JNI code or off the bottom of the stack */ 1897#if DVM_SHOW_EXCEPTION >= 2 1898 LOGD("Exception %s from %s:%d not caught locally\n", 1899 exception->clazz->descriptor, dvmGetMethodSourceFile(curMethod), 1900 dvmLineNumFromPC(curMethod, pc - curMethod->insns)); 1901#endif 1902 dvmSetException(self, exception); 1903 dvmReleaseTrackedAlloc(exception, self); 1904 GOTO_bail(); 1905 } 1906 1907#if DVM_SHOW_EXCEPTION >= 3 1908 { 1909 const Method* catchMethod = SAVEAREA_FROM_FP(fp)->method; 1910 LOGD("Exception %s thrown from %s:%d to %s:%d\n", 1911 exception->clazz->descriptor, dvmGetMethodSourceFile(curMethod), 1912 dvmLineNumFromPC(curMethod, pc - curMethod->insns), 1913 dvmGetMethodSourceFile(catchMethod), 1914 dvmLineNumFromPC(catchMethod, catchRelPc)); 1915 } 1916#endif 1917 1918 /* 1919 * Adjust local variables to match self->curFrame and the 1920 * updated PC. 1921 */ 1922 //fp = (u4*) self->curFrame; 1923 curMethod = SAVEAREA_FROM_FP(fp)->method; 1924 //methodClass = curMethod->clazz; 1925 methodClassDex = curMethod->clazz->pDvmDex; 1926 pc = curMethod->insns + catchRelPc; 1927 ILOGV("> pc <-- %s.%s %s", curMethod->clazz->descriptor, 1928 curMethod->name, curMethod->shorty); 1929 DUMP_REGS(curMethod, fp, false); // show all regs 1930 1931 /* 1932 * Restore the exception if the handler wants it. 1933 * 1934 * The Dalvik spec mandates that, if an exception handler wants to 1935 * do something with the exception, the first instruction executed 1936 * must be "move-exception". We can pass the exception along 1937 * through the thread struct, and let the move-exception instruction 1938 * clear it for us. 1939 * 1940 * If the handler doesn't call move-exception, we don't want to 1941 * finish here with an exception still pending. 1942 */ 1943 if (INST_INST(FETCH(0)) == OP_MOVE_EXCEPTION) 1944 dvmSetException(self, exception); 1945 1946 dvmReleaseTrackedAlloc(exception, self); 1947 FINISH(0); 1948 } 1949GOTO_TARGET_END 1950 1951 1952 /* 1953 * General handling for invoke-{virtual,super,direct,static,interface}, 1954 * including "quick" variants. 1955 * 1956 * Set "methodToCall" to the Method we're calling, and "methodCallRange" 1957 * depending on whether this is a "/range" instruction. 1958 * 1959 * For a range call: 1960 * "vsrc1" holds the argument count (8 bits) 1961 * "vdst" holds the first argument in the range 1962 * For a non-range call: 1963 * "vsrc1" holds the argument count (4 bits) and the 5th argument index 1964 * "vdst" holds four 4-bit register indices 1965 * 1966 * The caller must EXPORT_PC before jumping here, because any method 1967 * call can throw a stack overflow exception. 1968 */ 1969GOTO_TARGET(invokeMethod, bool methodCallRange, const Method* _methodToCall, 1970 u2 count, u2 regs) 1971 { 1972 STUB_HACK(vsrc1 = count; vdst = regs; methodToCall = _methodToCall;); 1973 1974 //printf("range=%d call=%p count=%d regs=0x%04x\n", 1975 // methodCallRange, methodToCall, count, regs); 1976 //printf(" --> %s.%s %s\n", methodToCall->clazz->descriptor, 1977 // methodToCall->name, methodToCall->shorty); 1978 1979 u4* outs; 1980 int i; 1981 1982 /* 1983 * Copy args. This may corrupt vsrc1/vdst. 1984 */ 1985 if (methodCallRange) { 1986 // could use memcpy or a "Duff's device"; most functions have 1987 // so few args it won't matter much 1988 assert(vsrc1 <= curMethod->outsSize); 1989 assert(vsrc1 == methodToCall->insSize); 1990 outs = OUTS_FROM_FP(fp, vsrc1); 1991 for (i = 0; i < vsrc1; i++) 1992 outs[i] = GET_REGISTER(vdst+i); 1993 } else { 1994 u4 count = vsrc1 >> 4; 1995 1996 assert(count <= curMethod->outsSize); 1997 assert(count == methodToCall->insSize); 1998 assert(count <= 5); 1999 2000 outs = OUTS_FROM_FP(fp, count); 2001#if 0 2002 if (count == 5) { 2003 outs[4] = GET_REGISTER(vsrc1 & 0x0f); 2004 count--; 2005 } 2006 for (i = 0; i < (int) count; i++) { 2007 outs[i] = GET_REGISTER(vdst & 0x0f); 2008 vdst >>= 4; 2009 } 2010#else 2011 // This version executes fewer instructions but is larger 2012 // overall. Seems to be a teensy bit faster. 2013 assert((vdst >> 16) == 0); // 16 bits -or- high 16 bits clear 2014 switch (count) { 2015 case 5: 2016 outs[4] = GET_REGISTER(vsrc1 & 0x0f); 2017 case 4: 2018 outs[3] = GET_REGISTER(vdst >> 12); 2019 case 3: 2020 outs[2] = GET_REGISTER((vdst & 0x0f00) >> 8); 2021 case 2: 2022 outs[1] = GET_REGISTER((vdst & 0x00f0) >> 4); 2023 case 1: 2024 outs[0] = GET_REGISTER(vdst & 0x0f); 2025 default: 2026 ; 2027 } 2028#endif 2029 } 2030 } 2031 2032 /* 2033 * (This was originally a "goto" target; I've kept it separate from the 2034 * stuff above in case we want to refactor things again.) 2035 * 2036 * At this point, we have the arguments stored in the "outs" area of 2037 * the current method's stack frame, and the method to call in 2038 * "methodToCall". Push a new stack frame. 2039 */ 2040 { 2041 StackSaveArea* newSaveArea; 2042 u4* newFp; 2043 2044 ILOGV("> %s%s.%s %s", 2045 dvmIsNativeMethod(methodToCall) ? "(NATIVE) " : "", 2046 methodToCall->clazz->descriptor, methodToCall->name, 2047 methodToCall->shorty); 2048 2049 newFp = (u4*) SAVEAREA_FROM_FP(fp) - methodToCall->registersSize; 2050 newSaveArea = SAVEAREA_FROM_FP(newFp); 2051 2052 /* verify that we have enough space */ 2053 if (true) { 2054 u1* bottom; 2055 bottom = (u1*) newSaveArea - methodToCall->outsSize * sizeof(u4); 2056 if (bottom < self->interpStackEnd) { 2057 /* stack overflow */ 2058 LOGV("Stack overflow on method call (start=%p end=%p newBot=%p(%d) size=%d '%s')\n", 2059 self->interpStackStart, self->interpStackEnd, bottom, 2060 (u1*) fp - bottom, self->interpStackSize, 2061 methodToCall->name); 2062 dvmHandleStackOverflow(self, methodToCall); 2063 assert(dvmCheckException(self)); 2064 GOTO_exceptionThrown(); 2065 } 2066 //LOGD("+++ fp=%p newFp=%p newSave=%p bottom=%p\n", 2067 // fp, newFp, newSaveArea, bottom); 2068 } 2069 2070#ifdef LOG_INSTR 2071 if (methodToCall->registersSize > methodToCall->insSize) { 2072 /* 2073 * This makes valgrind quiet when we print registers that 2074 * haven't been initialized. Turn it off when the debug 2075 * messages are disabled -- we want valgrind to report any 2076 * used-before-initialized issues. 2077 */ 2078 memset(newFp, 0xcc, 2079 (methodToCall->registersSize - methodToCall->insSize) * 4); 2080 } 2081#endif 2082 2083#ifdef EASY_GDB 2084 newSaveArea->prevSave = SAVEAREA_FROM_FP(fp); 2085#endif 2086 newSaveArea->prevFrame = fp; 2087 newSaveArea->savedPc = pc; 2088#if defined(WITH_JIT) 2089 newSaveArea->returnAddr = 0; 2090#endif 2091 newSaveArea->method = methodToCall; 2092 2093 if (!dvmIsNativeMethod(methodToCall)) { 2094 /* 2095 * "Call" interpreted code. Reposition the PC, update the 2096 * frame pointer and other local state, and continue. 2097 */ 2098 curMethod = methodToCall; 2099 methodClassDex = curMethod->clazz->pDvmDex; 2100 pc = methodToCall->insns; 2101 fp = self->curFrame = newFp; 2102#ifdef EASY_GDB 2103 debugSaveArea = SAVEAREA_FROM_FP(newFp); 2104#endif 2105#if INTERP_TYPE == INTERP_DBG 2106 debugIsMethodEntry = true; // profiling, debugging 2107#endif 2108 ILOGD("> pc <-- %s.%s %s", curMethod->clazz->descriptor, 2109 curMethod->name, curMethod->shorty); 2110 DUMP_REGS(curMethod, fp, true); // show input args 2111 FINISH(0); // jump to method start 2112 } else { 2113 /* set this up for JNI locals, even if not a JNI native */ 2114#ifdef USE_INDIRECT_REF 2115 newSaveArea->xtra.localRefCookie = self->jniLocalRefTable.segmentState.all; 2116#else 2117 newSaveArea->xtra.localRefCookie = self->jniLocalRefTable.nextEntry; 2118#endif 2119 2120 self->curFrame = newFp; 2121 2122 DUMP_REGS(methodToCall, newFp, true); // show input args 2123 2124#if (INTERP_TYPE == INTERP_DBG) && defined(WITH_DEBUGGER) 2125 if (gDvm.debuggerActive) { 2126 dvmDbgPostLocationEvent(methodToCall, -1, 2127 dvmGetThisPtr(curMethod, fp), DBG_METHOD_ENTRY); 2128 } 2129#endif 2130#if (INTERP_TYPE == INTERP_DBG) && defined(WITH_PROFILER) 2131 TRACE_METHOD_ENTER(self, methodToCall); 2132#endif 2133 2134 ILOGD("> native <-- %s.%s %s", methodToCall->clazz->descriptor, 2135 methodToCall->name, methodToCall->shorty); 2136 2137#if defined(WITH_JIT) 2138 /* Allow the Jit to end any pending trace building */ 2139 CHECK_JIT(); 2140#endif 2141 2142 /* 2143 * Jump through native call bridge. Because we leave no 2144 * space for locals on native calls, "newFp" points directly 2145 * to the method arguments. 2146 */ 2147 (*methodToCall->nativeFunc)(newFp, &retval, methodToCall, self); 2148 2149#if (INTERP_TYPE == INTERP_DBG) && defined(WITH_DEBUGGER) 2150 if (gDvm.debuggerActive) { 2151 dvmDbgPostLocationEvent(methodToCall, -1, 2152 dvmGetThisPtr(curMethod, fp), DBG_METHOD_EXIT); 2153 } 2154#endif 2155#if (INTERP_TYPE == INTERP_DBG) && defined(WITH_PROFILER) 2156 TRACE_METHOD_EXIT(self, methodToCall); 2157#endif 2158 2159 /* pop frame off */ 2160 dvmPopJniLocals(self, newSaveArea); 2161 self->curFrame = fp; 2162 2163 /* 2164 * If the native code threw an exception, or interpreted code 2165 * invoked by the native call threw one and nobody has cleared 2166 * it, jump to our local exception handling. 2167 */ 2168 if (dvmCheckException(self)) { 2169 LOGV("Exception thrown by/below native code\n"); 2170 GOTO_exceptionThrown(); 2171 } 2172 2173 ILOGD("> retval=0x%llx (leaving native)", retval.j); 2174 ILOGD("> (return from native %s.%s to %s.%s %s)", 2175 methodToCall->clazz->descriptor, methodToCall->name, 2176 curMethod->clazz->descriptor, curMethod->name, 2177 curMethod->shorty); 2178 2179 //u2 invokeInstr = INST_INST(FETCH(0)); 2180 if (true /*invokeInstr >= OP_INVOKE_VIRTUAL && 2181 invokeInstr <= OP_INVOKE_INTERFACE*/) 2182 { 2183 FINISH(3); 2184 } else { 2185 //LOGE("Unknown invoke instr %02x at %d\n", 2186 // invokeInstr, (int) (pc - curMethod->insns)); 2187 assert(false); 2188 } 2189 } 2190 } 2191 assert(false); // should not get here 2192GOTO_TARGET_END 2193 2194/* File: cstubs/enddefs.c */ 2195 2196/* undefine "magic" name remapping */ 2197#undef retval 2198#undef pc 2199#undef fp 2200#undef curMethod 2201#undef methodClassDex 2202#undef self 2203#undef debugTrackedRefStart 2204 2205