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