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