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