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