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