blob: 30a4e1ca771a3e90b8ebe19569a391307ce783cf [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
2 * This file was generated automatically by gen-mterp.py for 'x86'.
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
Ben Chengba4fc8b2009-06-01 13:00:29 -070029#include "mterp/common/FindInterface.h"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080030
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
The Android Open Source Project99409882009-03-18 22:20:24 -070057#ifdef WITH_INSTR_CHECKS /* instruction-level paranoia (slow!) */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080058# 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/*
The Android Open Source Project99409882009-03-18 22:20:24 -070097 * 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/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800109 * 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; \
The Android Open Source Project99409882009-03-18 22:20:24 -0700132 EXPORT_EXTRA_PC(); \
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800133 } while (false)
134#else
The Android Open Source Project99409882009-03-18 22:20:24 -0700135# define ADJUST_PC(_offset) do { \
136 pc += _offset; \
137 EXPORT_EXTRA_PC(); \
138 } while (false)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800139#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 McFadden96516932009-10-28 17:39:02 -0700305 * Replace the opcode (used when handling breakpoints). _opcode is a u1.
306 */
307#define INST_REPLACE_OP(_inst, _opcode) (((_inst) & 0xff00) | _opcode)
308
309/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800310 * 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 *
The Android Open Source Project99409882009-03-18 22:20:24 -0700328 * This is also used to determine the address for precise GC.
329 *
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800330 * 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)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700343#if defined(WITH_JIT)
344# define NEED_INTERP_SWITCH(_current) ( \
345 (_current == INTERP_STD) ? \
346 dvmJitDebuggerOrProfilerActive(interpState->jitState) : \
347 !dvmJitDebuggerOrProfilerActive(interpState->jitState) )
348#else
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800349# define NEED_INTERP_SWITCH(_current) ( \
350 (_current == INTERP_STD) ? \
351 dvmDebuggerOrProfilerActive() : !dvmDebuggerOrProfilerActive() )
Ben Chengba4fc8b2009-06-01 13:00:29 -0700352#endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800353#else
354# define NEED_INTERP_SWITCH(_current) (false)
355#endif
356
357/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800358 * Check to see if "obj" is NULL. If so, throw an exception. Assumes the
359 * pc has already been exported to the stack.
360 *
361 * Perform additional checks on debug builds.
362 *
363 * Use this to check for NULL when the instruction handler calls into
364 * something that could throw an exception (so we have already called
365 * EXPORT_PC at the top).
366 */
367static inline bool checkForNull(Object* obj)
368{
369 if (obj == NULL) {
370 dvmThrowException("Ljava/lang/NullPointerException;", NULL);
371 return false;
372 }
373#ifdef WITH_EXTRA_OBJECT_VALIDATION
374 if (!dvmIsValidObject(obj)) {
375 LOGE("Invalid object %p\n", obj);
376 dvmAbort();
377 }
378#endif
379#ifndef NDEBUG
380 if (obj->clazz == NULL || ((u4) obj->clazz) <= 65536) {
381 /* probable heap corruption */
382 LOGE("Invalid object class %p (in %p)\n", obj->clazz, obj);
383 dvmAbort();
384 }
385#endif
386 return true;
387}
388
389/*
390 * Check to see if "obj" is NULL. If so, export the PC into the stack
391 * frame and throw an exception.
392 *
393 * Perform additional checks on debug builds.
394 *
395 * Use this to check for NULL when the instruction handler doesn't do
396 * anything else that can throw an exception.
397 */
398static inline bool checkForNullExportPC(Object* obj, u4* fp, const u2* pc)
399{
400 if (obj == NULL) {
401 EXPORT_PC();
402 dvmThrowException("Ljava/lang/NullPointerException;", NULL);
403 return false;
404 }
405#ifdef WITH_EXTRA_OBJECT_VALIDATION
406 if (!dvmIsValidObject(obj)) {
407 LOGE("Invalid object %p\n", obj);
408 dvmAbort();
409 }
410#endif
411#ifndef NDEBUG
412 if (obj->clazz == NULL || ((u4) obj->clazz) <= 65536) {
413 /* probable heap corruption */
414 LOGE("Invalid object class %p (in %p)\n", obj->clazz, obj);
415 dvmAbort();
416 }
417#endif
418 return true;
419}
420
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800421/* File: cstubs/stubdefs.c */
422/* this is a standard (no debug support) interpreter */
423#define INTERP_TYPE INTERP_STD
424#define CHECK_DEBUG_AND_PROF() ((void)0)
425# define CHECK_TRACKED_REFS() ((void)0)
426
427/*
428 * In the C mterp stubs, "goto" is a function call followed immediately
429 * by a return.
430 */
431
432#define GOTO_TARGET_DECL(_target, ...) \
433 void dvmMterp_##_target(MterpGlue* glue, ## __VA_ARGS__);
434
435#define GOTO_TARGET(_target, ...) \
436 void dvmMterp_##_target(MterpGlue* glue, ## __VA_ARGS__) { \
437 u2 ref, vsrc1, vsrc2, vdst; \
438 u2 inst = FETCH(0); \
439 const Method* methodToCall; \
440 StackSaveArea* debugSaveArea;
441
442#define GOTO_TARGET_END }
443
444/*
445 * Redefine what used to be local variable accesses into MterpGlue struct
446 * references. (These are undefined down in "footer.c".)
447 */
448#define retval glue->retval
449#define pc glue->pc
450#define fp glue->fp
451#define curMethod glue->method
452#define methodClassDex glue->methodClassDex
453#define self glue->self
454#define debugTrackedRefStart glue->debugTrackedRefStart
455
456/* ugh */
457#define STUB_HACK(x) x
458
459
460/*
461 * Opcode handler framing macros. Here, each opcode is a separate function
462 * that takes a "glue" argument and returns void. We can't declare
463 * these "static" because they may be called from an assembly stub.
464 */
465#define HANDLE_OPCODE(_op) \
466 void dvmMterp_##_op(MterpGlue* glue) { \
467 u2 ref, vsrc1, vsrc2, vdst; \
468 u2 inst = FETCH(0);
469
470#define OP_END }
471
472/*
473 * Like the "portable" FINISH, but don't reload "inst", and return to caller
474 * when done.
475 */
476#define FINISH(_offset) { \
477 ADJUST_PC(_offset); \
478 CHECK_DEBUG_AND_PROF(); \
479 CHECK_TRACKED_REFS(); \
480 return; \
481 }
482
483
484/*
485 * The "goto label" statements turn into function calls followed by
486 * return statements. Some of the functions take arguments, which in the
487 * portable interpreter are handled by assigning values to globals.
488 */
489
490#define GOTO_exceptionThrown() \
491 do { \
492 dvmMterp_exceptionThrown(glue); \
493 return; \
494 } while(false)
495
496#define GOTO_returnFromMethod() \
497 do { \
498 dvmMterp_returnFromMethod(glue); \
499 return; \
500 } while(false)
501
502#define GOTO_invoke(_target, _methodCallRange) \
503 do { \
504 dvmMterp_##_target(glue, _methodCallRange); \
505 return; \
506 } while(false)
507
508#define GOTO_invokeMethod(_methodCallRange, _methodToCall, _vsrc1, _vdst) \
509 do { \
510 dvmMterp_invokeMethod(glue, _methodCallRange, _methodToCall, \
511 _vsrc1, _vdst); \
512 return; \
513 } while(false)
514
515/*
516 * As a special case, "goto bail" turns into a longjmp. Use "bail_switch"
517 * if we need to switch to the other interpreter upon our return.
518 */
519#define GOTO_bail() \
520 dvmMterpStdBail(glue, false);
521#define GOTO_bail_switch() \
522 dvmMterpStdBail(glue, true);
523
524/*
525 * Periodically check for thread suspension.
526 *
527 * While we're at it, see if a debugger has attached or the profiler has
528 * started. If so, switch to a different "goto" table.
529 */
530#define PERIODIC_CHECKS(_entryPoint, _pcadj) { \
The Android Open Source Project99409882009-03-18 22:20:24 -0700531 if (dvmCheckSuspendQuick(self)) { \
532 EXPORT_PC(); /* need for precise GC */ \
533 dvmCheckSuspendPending(self); \
534 } \
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800535 if (NEED_INTERP_SWITCH(INTERP_TYPE)) { \
536 ADJUST_PC(_pcadj); \
537 glue->entryPoint = _entryPoint; \
538 LOGVV("threadid=%d: switch to STD ep=%d adj=%d\n", \
Andy McFadden080ca4a2009-08-05 13:20:16 -0700539 self->threadId, (_entryPoint), (_pcadj)); \
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800540 GOTO_bail_switch(); \
541 } \
542 }
543
544
545/* File: c/opcommon.c */
546/* forward declarations of goto targets */
547GOTO_TARGET_DECL(filledNewArray, bool methodCallRange);
548GOTO_TARGET_DECL(invokeVirtual, bool methodCallRange);
549GOTO_TARGET_DECL(invokeSuper, bool methodCallRange);
550GOTO_TARGET_DECL(invokeInterface, bool methodCallRange);
551GOTO_TARGET_DECL(invokeDirect, bool methodCallRange);
552GOTO_TARGET_DECL(invokeStatic, bool methodCallRange);
553GOTO_TARGET_DECL(invokeVirtualQuick, bool methodCallRange);
554GOTO_TARGET_DECL(invokeSuperQuick, bool methodCallRange);
555GOTO_TARGET_DECL(invokeMethod, bool methodCallRange, const Method* methodToCall,
556 u2 count, u2 regs);
557GOTO_TARGET_DECL(returnFromMethod);
558GOTO_TARGET_DECL(exceptionThrown);
559
560/*
561 * ===========================================================================
562 *
563 * What follows are opcode definitions shared between multiple opcodes with
564 * minor substitutions handled by the C pre-processor. These should probably
565 * use the mterp substitution mechanism instead, with the code here moved
566 * into common fragment files (like the asm "binop.S"), although it's hard
567 * to give up the C preprocessor in favor of the much simpler text subst.
568 *
569 * ===========================================================================
570 */
571
572#define HANDLE_NUMCONV(_opcode, _opname, _fromtype, _totype) \
573 HANDLE_OPCODE(_opcode /*vA, vB*/) \
574 vdst = INST_A(inst); \
575 vsrc1 = INST_B(inst); \
576 ILOGV("|%s v%d,v%d", (_opname), vdst, vsrc1); \
577 SET_REGISTER##_totype(vdst, \
578 GET_REGISTER##_fromtype(vsrc1)); \
579 FINISH(1);
580
581#define HANDLE_FLOAT_TO_INT(_opcode, _opname, _fromvtype, _fromrtype, \
582 _tovtype, _tortype) \
583 HANDLE_OPCODE(_opcode /*vA, vB*/) \
584 { \
585 /* spec defines specific handling for +/- inf and NaN values */ \
586 _fromvtype val; \
587 _tovtype intMin, intMax, result; \
588 vdst = INST_A(inst); \
589 vsrc1 = INST_B(inst); \
590 ILOGV("|%s v%d,v%d", (_opname), vdst, vsrc1); \
591 val = GET_REGISTER##_fromrtype(vsrc1); \
592 intMin = (_tovtype) 1 << (sizeof(_tovtype) * 8 -1); \
593 intMax = ~intMin; \
594 result = (_tovtype) val; \
595 if (val >= intMax) /* +inf */ \
596 result = intMax; \
597 else if (val <= intMin) /* -inf */ \
598 result = intMin; \
599 else if (val != val) /* NaN */ \
600 result = 0; \
601 else \
602 result = (_tovtype) val; \
603 SET_REGISTER##_tortype(vdst, result); \
604 } \
605 FINISH(1);
606
607#define HANDLE_INT_TO_SMALL(_opcode, _opname, _type) \
608 HANDLE_OPCODE(_opcode /*vA, vB*/) \
609 vdst = INST_A(inst); \
610 vsrc1 = INST_B(inst); \
611 ILOGV("|int-to-%s v%d,v%d", (_opname), vdst, vsrc1); \
612 SET_REGISTER(vdst, (_type) GET_REGISTER(vsrc1)); \
613 FINISH(1);
614
615/* NOTE: the comparison result is always a signed 4-byte integer */
616#define HANDLE_OP_CMPX(_opcode, _opname, _varType, _type, _nanVal) \
617 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \
618 { \
619 int result; \
620 u2 regs; \
621 _varType val1, val2; \
622 vdst = INST_AA(inst); \
623 regs = FETCH(1); \
624 vsrc1 = regs & 0xff; \
625 vsrc2 = regs >> 8; \
626 ILOGV("|cmp%s v%d,v%d,v%d", (_opname), vdst, vsrc1, vsrc2); \
627 val1 = GET_REGISTER##_type(vsrc1); \
628 val2 = GET_REGISTER##_type(vsrc2); \
629 if (val1 == val2) \
630 result = 0; \
631 else if (val1 < val2) \
632 result = -1; \
633 else if (val1 > val2) \
634 result = 1; \
635 else \
636 result = (_nanVal); \
637 ILOGV("+ result=%d\n", result); \
638 SET_REGISTER(vdst, result); \
639 } \
640 FINISH(2);
641
642#define HANDLE_OP_IF_XX(_opcode, _opname, _cmp) \
643 HANDLE_OPCODE(_opcode /*vA, vB, +CCCC*/) \
644 vsrc1 = INST_A(inst); \
645 vsrc2 = INST_B(inst); \
646 if ((s4) GET_REGISTER(vsrc1) _cmp (s4) GET_REGISTER(vsrc2)) { \
647 int branchOffset = (s2)FETCH(1); /* sign-extended */ \
648 ILOGV("|if-%s v%d,v%d,+0x%04x", (_opname), vsrc1, vsrc2, \
649 branchOffset); \
650 ILOGV("> branch taken"); \
651 if (branchOffset < 0) \
652 PERIODIC_CHECKS(kInterpEntryInstr, branchOffset); \
653 FINISH(branchOffset); \
654 } else { \
655 ILOGV("|if-%s v%d,v%d,-", (_opname), vsrc1, vsrc2); \
656 FINISH(2); \
657 }
658
659#define HANDLE_OP_IF_XXZ(_opcode, _opname, _cmp) \
660 HANDLE_OPCODE(_opcode /*vAA, +BBBB*/) \
661 vsrc1 = INST_AA(inst); \
662 if ((s4) GET_REGISTER(vsrc1) _cmp 0) { \
663 int branchOffset = (s2)FETCH(1); /* sign-extended */ \
664 ILOGV("|if-%s v%d,+0x%04x", (_opname), vsrc1, branchOffset); \
665 ILOGV("> branch taken"); \
666 if (branchOffset < 0) \
667 PERIODIC_CHECKS(kInterpEntryInstr, branchOffset); \
668 FINISH(branchOffset); \
669 } else { \
670 ILOGV("|if-%s v%d,-", (_opname), vsrc1); \
671 FINISH(2); \
672 }
673
674#define HANDLE_UNOP(_opcode, _opname, _pfx, _sfx, _type) \
675 HANDLE_OPCODE(_opcode /*vA, vB*/) \
676 vdst = INST_A(inst); \
677 vsrc1 = INST_B(inst); \
678 ILOGV("|%s v%d,v%d", (_opname), vdst, vsrc1); \
679 SET_REGISTER##_type(vdst, _pfx GET_REGISTER##_type(vsrc1) _sfx); \
680 FINISH(1);
681
682#define HANDLE_OP_X_INT(_opcode, _opname, _op, _chkdiv) \
683 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \
684 { \
685 u2 srcRegs; \
686 vdst = INST_AA(inst); \
687 srcRegs = FETCH(1); \
688 vsrc1 = srcRegs & 0xff; \
689 vsrc2 = srcRegs >> 8; \
690 ILOGV("|%s-int v%d,v%d", (_opname), vdst, vsrc1); \
691 if (_chkdiv != 0) { \
692 s4 firstVal, secondVal, result; \
693 firstVal = GET_REGISTER(vsrc1); \
694 secondVal = GET_REGISTER(vsrc2); \
695 if (secondVal == 0) { \
696 EXPORT_PC(); \
697 dvmThrowException("Ljava/lang/ArithmeticException;", \
698 "divide by zero"); \
699 GOTO_exceptionThrown(); \
700 } \
701 if ((u4)firstVal == 0x80000000 && secondVal == -1) { \
702 if (_chkdiv == 1) \
703 result = firstVal; /* division */ \
704 else \
705 result = 0; /* remainder */ \
706 } else { \
707 result = firstVal _op secondVal; \
708 } \
709 SET_REGISTER(vdst, result); \
710 } else { \
711 /* non-div/rem case */ \
712 SET_REGISTER(vdst, \
713 (s4) GET_REGISTER(vsrc1) _op (s4) GET_REGISTER(vsrc2)); \
714 } \
715 } \
716 FINISH(2);
717
718#define HANDLE_OP_SHX_INT(_opcode, _opname, _cast, _op) \
719 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \
720 { \
721 u2 srcRegs; \
722 vdst = INST_AA(inst); \
723 srcRegs = FETCH(1); \
724 vsrc1 = srcRegs & 0xff; \
725 vsrc2 = srcRegs >> 8; \
726 ILOGV("|%s-int v%d,v%d", (_opname), vdst, vsrc1); \
727 SET_REGISTER(vdst, \
728 _cast GET_REGISTER(vsrc1) _op (GET_REGISTER(vsrc2) & 0x1f)); \
729 } \
730 FINISH(2);
731
732#define HANDLE_OP_X_INT_LIT16(_opcode, _opname, _op, _chkdiv) \
733 HANDLE_OPCODE(_opcode /*vA, vB, #+CCCC*/) \
734 vdst = INST_A(inst); \
735 vsrc1 = INST_B(inst); \
736 vsrc2 = FETCH(1); \
737 ILOGV("|%s-int/lit16 v%d,v%d,#+0x%04x", \
738 (_opname), vdst, vsrc1, vsrc2); \
739 if (_chkdiv != 0) { \
740 s4 firstVal, result; \
741 firstVal = GET_REGISTER(vsrc1); \
742 if ((s2) vsrc2 == 0) { \
743 EXPORT_PC(); \
744 dvmThrowException("Ljava/lang/ArithmeticException;", \
745 "divide by zero"); \
746 GOTO_exceptionThrown(); \
747 } \
748 if ((u4)firstVal == 0x80000000 && ((s2) vsrc2) == -1) { \
749 /* won't generate /lit16 instr for this; check anyway */ \
750 if (_chkdiv == 1) \
751 result = firstVal; /* division */ \
752 else \
753 result = 0; /* remainder */ \
754 } else { \
755 result = firstVal _op (s2) vsrc2; \
756 } \
757 SET_REGISTER(vdst, result); \
758 } else { \
759 /* non-div/rem case */ \
760 SET_REGISTER(vdst, GET_REGISTER(vsrc1) _op (s2) vsrc2); \
761 } \
762 FINISH(2);
763
764#define HANDLE_OP_X_INT_LIT8(_opcode, _opname, _op, _chkdiv) \
765 HANDLE_OPCODE(_opcode /*vAA, vBB, #+CC*/) \
766 { \
767 u2 litInfo; \
768 vdst = INST_AA(inst); \
769 litInfo = FETCH(1); \
770 vsrc1 = litInfo & 0xff; \
771 vsrc2 = litInfo >> 8; /* constant */ \
772 ILOGV("|%s-int/lit8 v%d,v%d,#+0x%02x", \
773 (_opname), vdst, vsrc1, vsrc2); \
774 if (_chkdiv != 0) { \
775 s4 firstVal, result; \
776 firstVal = GET_REGISTER(vsrc1); \
777 if ((s1) vsrc2 == 0) { \
778 EXPORT_PC(); \
779 dvmThrowException("Ljava/lang/ArithmeticException;", \
780 "divide by zero"); \
781 GOTO_exceptionThrown(); \
782 } \
783 if ((u4)firstVal == 0x80000000 && ((s1) vsrc2) == -1) { \
784 if (_chkdiv == 1) \
785 result = firstVal; /* division */ \
786 else \
787 result = 0; /* remainder */ \
788 } else { \
789 result = firstVal _op ((s1) vsrc2); \
790 } \
791 SET_REGISTER(vdst, result); \
792 } else { \
793 SET_REGISTER(vdst, \
794 (s4) GET_REGISTER(vsrc1) _op (s1) vsrc2); \
795 } \
796 } \
797 FINISH(2);
798
799#define HANDLE_OP_SHX_INT_LIT8(_opcode, _opname, _cast, _op) \
800 HANDLE_OPCODE(_opcode /*vAA, vBB, #+CC*/) \
801 { \
802 u2 litInfo; \
803 vdst = INST_AA(inst); \
804 litInfo = FETCH(1); \
805 vsrc1 = litInfo & 0xff; \
806 vsrc2 = litInfo >> 8; /* constant */ \
807 ILOGV("|%s-int/lit8 v%d,v%d,#+0x%02x", \
808 (_opname), vdst, vsrc1, vsrc2); \
809 SET_REGISTER(vdst, \
810 _cast GET_REGISTER(vsrc1) _op (vsrc2 & 0x1f)); \
811 } \
812 FINISH(2);
813
814#define HANDLE_OP_X_INT_2ADDR(_opcode, _opname, _op, _chkdiv) \
815 HANDLE_OPCODE(_opcode /*vA, vB*/) \
816 vdst = INST_A(inst); \
817 vsrc1 = INST_B(inst); \
818 ILOGV("|%s-int-2addr v%d,v%d", (_opname), vdst, vsrc1); \
819 if (_chkdiv != 0) { \
820 s4 firstVal, secondVal, result; \
821 firstVal = GET_REGISTER(vdst); \
822 secondVal = GET_REGISTER(vsrc1); \
823 if (secondVal == 0) { \
824 EXPORT_PC(); \
825 dvmThrowException("Ljava/lang/ArithmeticException;", \
826 "divide by zero"); \
827 GOTO_exceptionThrown(); \
828 } \
829 if ((u4)firstVal == 0x80000000 && secondVal == -1) { \
830 if (_chkdiv == 1) \
831 result = firstVal; /* division */ \
832 else \
833 result = 0; /* remainder */ \
834 } else { \
835 result = firstVal _op secondVal; \
836 } \
837 SET_REGISTER(vdst, result); \
838 } else { \
839 SET_REGISTER(vdst, \
840 (s4) GET_REGISTER(vdst) _op (s4) GET_REGISTER(vsrc1)); \
841 } \
842 FINISH(1);
843
844#define HANDLE_OP_SHX_INT_2ADDR(_opcode, _opname, _cast, _op) \
845 HANDLE_OPCODE(_opcode /*vA, vB*/) \
846 vdst = INST_A(inst); \
847 vsrc1 = INST_B(inst); \
848 ILOGV("|%s-int-2addr v%d,v%d", (_opname), vdst, vsrc1); \
849 SET_REGISTER(vdst, \
850 _cast GET_REGISTER(vdst) _op (GET_REGISTER(vsrc1) & 0x1f)); \
851 FINISH(1);
852
853#define HANDLE_OP_X_LONG(_opcode, _opname, _op, _chkdiv) \
854 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \
855 { \
856 u2 srcRegs; \
857 vdst = INST_AA(inst); \
858 srcRegs = FETCH(1); \
859 vsrc1 = srcRegs & 0xff; \
860 vsrc2 = srcRegs >> 8; \
861 ILOGV("|%s-long v%d,v%d,v%d", (_opname), vdst, vsrc1, vsrc2); \
862 if (_chkdiv != 0) { \
863 s8 firstVal, secondVal, result; \
864 firstVal = GET_REGISTER_WIDE(vsrc1); \
865 secondVal = GET_REGISTER_WIDE(vsrc2); \
866 if (secondVal == 0LL) { \
867 EXPORT_PC(); \
868 dvmThrowException("Ljava/lang/ArithmeticException;", \
869 "divide by zero"); \
870 GOTO_exceptionThrown(); \
871 } \
872 if ((u8)firstVal == 0x8000000000000000ULL && \
873 secondVal == -1LL) \
874 { \
875 if (_chkdiv == 1) \
876 result = firstVal; /* division */ \
877 else \
878 result = 0; /* remainder */ \
879 } else { \
880 result = firstVal _op secondVal; \
881 } \
882 SET_REGISTER_WIDE(vdst, result); \
883 } else { \
884 SET_REGISTER_WIDE(vdst, \
885 (s8) GET_REGISTER_WIDE(vsrc1) _op (s8) GET_REGISTER_WIDE(vsrc2)); \
886 } \
887 } \
888 FINISH(2);
889
890#define HANDLE_OP_SHX_LONG(_opcode, _opname, _cast, _op) \
891 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \
892 { \
893 u2 srcRegs; \
894 vdst = INST_AA(inst); \
895 srcRegs = FETCH(1); \
896 vsrc1 = srcRegs & 0xff; \
897 vsrc2 = srcRegs >> 8; \
898 ILOGV("|%s-long v%d,v%d,v%d", (_opname), vdst, vsrc1, vsrc2); \
899 SET_REGISTER_WIDE(vdst, \
900 _cast GET_REGISTER_WIDE(vsrc1) _op (GET_REGISTER(vsrc2) & 0x3f)); \
901 } \
902 FINISH(2);
903
904#define HANDLE_OP_X_LONG_2ADDR(_opcode, _opname, _op, _chkdiv) \
905 HANDLE_OPCODE(_opcode /*vA, vB*/) \
906 vdst = INST_A(inst); \
907 vsrc1 = INST_B(inst); \
908 ILOGV("|%s-long-2addr v%d,v%d", (_opname), vdst, vsrc1); \
909 if (_chkdiv != 0) { \
910 s8 firstVal, secondVal, result; \
911 firstVal = GET_REGISTER_WIDE(vdst); \
912 secondVal = GET_REGISTER_WIDE(vsrc1); \
913 if (secondVal == 0LL) { \
914 EXPORT_PC(); \
915 dvmThrowException("Ljava/lang/ArithmeticException;", \
916 "divide by zero"); \
917 GOTO_exceptionThrown(); \
918 } \
919 if ((u8)firstVal == 0x8000000000000000ULL && \
920 secondVal == -1LL) \
921 { \
922 if (_chkdiv == 1) \
923 result = firstVal; /* division */ \
924 else \
925 result = 0; /* remainder */ \
926 } else { \
927 result = firstVal _op secondVal; \
928 } \
929 SET_REGISTER_WIDE(vdst, result); \
930 } else { \
931 SET_REGISTER_WIDE(vdst, \
932 (s8) GET_REGISTER_WIDE(vdst) _op (s8)GET_REGISTER_WIDE(vsrc1));\
933 } \
934 FINISH(1);
935
936#define HANDLE_OP_SHX_LONG_2ADDR(_opcode, _opname, _cast, _op) \
937 HANDLE_OPCODE(_opcode /*vA, vB*/) \
938 vdst = INST_A(inst); \
939 vsrc1 = INST_B(inst); \
940 ILOGV("|%s-long-2addr v%d,v%d", (_opname), vdst, vsrc1); \
941 SET_REGISTER_WIDE(vdst, \
942 _cast GET_REGISTER_WIDE(vdst) _op (GET_REGISTER(vsrc1) & 0x3f)); \
943 FINISH(1);
944
945#define HANDLE_OP_X_FLOAT(_opcode, _opname, _op) \
946 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \
947 { \
948 u2 srcRegs; \
949 vdst = INST_AA(inst); \
950 srcRegs = FETCH(1); \
951 vsrc1 = srcRegs & 0xff; \
952 vsrc2 = srcRegs >> 8; \
953 ILOGV("|%s-float v%d,v%d,v%d", (_opname), vdst, vsrc1, vsrc2); \
954 SET_REGISTER_FLOAT(vdst, \
955 GET_REGISTER_FLOAT(vsrc1) _op GET_REGISTER_FLOAT(vsrc2)); \
956 } \
957 FINISH(2);
958
959#define HANDLE_OP_X_DOUBLE(_opcode, _opname, _op) \
960 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \
961 { \
962 u2 srcRegs; \
963 vdst = INST_AA(inst); \
964 srcRegs = FETCH(1); \
965 vsrc1 = srcRegs & 0xff; \
966 vsrc2 = srcRegs >> 8; \
967 ILOGV("|%s-double v%d,v%d,v%d", (_opname), vdst, vsrc1, vsrc2); \
968 SET_REGISTER_DOUBLE(vdst, \
969 GET_REGISTER_DOUBLE(vsrc1) _op GET_REGISTER_DOUBLE(vsrc2)); \
970 } \
971 FINISH(2);
972
973#define HANDLE_OP_X_FLOAT_2ADDR(_opcode, _opname, _op) \
974 HANDLE_OPCODE(_opcode /*vA, vB*/) \
975 vdst = INST_A(inst); \
976 vsrc1 = INST_B(inst); \
977 ILOGV("|%s-float-2addr v%d,v%d", (_opname), vdst, vsrc1); \
978 SET_REGISTER_FLOAT(vdst, \
979 GET_REGISTER_FLOAT(vdst) _op GET_REGISTER_FLOAT(vsrc1)); \
980 FINISH(1);
981
982#define HANDLE_OP_X_DOUBLE_2ADDR(_opcode, _opname, _op) \
983 HANDLE_OPCODE(_opcode /*vA, vB*/) \
984 vdst = INST_A(inst); \
985 vsrc1 = INST_B(inst); \
986 ILOGV("|%s-double-2addr v%d,v%d", (_opname), vdst, vsrc1); \
987 SET_REGISTER_DOUBLE(vdst, \
988 GET_REGISTER_DOUBLE(vdst) _op GET_REGISTER_DOUBLE(vsrc1)); \
989 FINISH(1);
990
991#define HANDLE_OP_AGET(_opcode, _opname, _type, _regsize) \
992 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \
993 { \
994 ArrayObject* arrayObj; \
995 u2 arrayInfo; \
996 EXPORT_PC(); \
997 vdst = INST_AA(inst); \
998 arrayInfo = FETCH(1); \
999 vsrc1 = arrayInfo & 0xff; /* array ptr */ \
1000 vsrc2 = arrayInfo >> 8; /* index */ \
1001 ILOGV("|aget%s v%d,v%d,v%d", (_opname), vdst, vsrc1, vsrc2); \
1002 arrayObj = (ArrayObject*) GET_REGISTER(vsrc1); \
1003 if (!checkForNull((Object*) arrayObj)) \
1004 GOTO_exceptionThrown(); \
1005 if (GET_REGISTER(vsrc2) >= arrayObj->length) { \
1006 LOGV("Invalid array access: %p %d (len=%d)\n", \
1007 arrayObj, vsrc2, arrayObj->length); \
1008 dvmThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", \
1009 NULL); \
1010 GOTO_exceptionThrown(); \
1011 } \
1012 SET_REGISTER##_regsize(vdst, \
1013 ((_type*) arrayObj->contents)[GET_REGISTER(vsrc2)]); \
1014 ILOGV("+ AGET[%d]=0x%x", GET_REGISTER(vsrc2), GET_REGISTER(vdst)); \
1015 } \
1016 FINISH(2);
1017
1018#define HANDLE_OP_APUT(_opcode, _opname, _type, _regsize) \
1019 HANDLE_OPCODE(_opcode /*vAA, vBB, vCC*/) \
1020 { \
1021 ArrayObject* arrayObj; \
1022 u2 arrayInfo; \
1023 EXPORT_PC(); \
1024 vdst = INST_AA(inst); /* AA: source value */ \
1025 arrayInfo = FETCH(1); \
1026 vsrc1 = arrayInfo & 0xff; /* BB: array ptr */ \
1027 vsrc2 = arrayInfo >> 8; /* CC: index */ \
1028 ILOGV("|aput%s v%d,v%d,v%d", (_opname), vdst, vsrc1, vsrc2); \
1029 arrayObj = (ArrayObject*) GET_REGISTER(vsrc1); \
1030 if (!checkForNull((Object*) arrayObj)) \
1031 GOTO_exceptionThrown(); \
1032 if (GET_REGISTER(vsrc2) >= arrayObj->length) { \
1033 dvmThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", \
1034 NULL); \
1035 GOTO_exceptionThrown(); \
1036 } \
1037 ILOGV("+ APUT[%d]=0x%08x", GET_REGISTER(vsrc2), GET_REGISTER(vdst));\
1038 ((_type*) arrayObj->contents)[GET_REGISTER(vsrc2)] = \
1039 GET_REGISTER##_regsize(vdst); \
1040 } \
1041 FINISH(2);
1042
1043/*
1044 * It's possible to get a bad value out of a field with sub-32-bit stores
1045 * because the -quick versions always operate on 32 bits. Consider:
1046 * short foo = -1 (sets a 32-bit register to 0xffffffff)
1047 * iput-quick foo (writes all 32 bits to the field)
1048 * short bar = 1 (sets a 32-bit register to 0x00000001)
1049 * iput-short (writes the low 16 bits to the field)
1050 * iget-quick foo (reads all 32 bits from the field, yielding 0xffff0001)
1051 * This can only happen when optimized and non-optimized code has interleaved
1052 * access to the same field. This is unlikely but possible.
1053 *
1054 * The easiest way to fix this is to always read/write 32 bits at a time. On
1055 * a device with a 16-bit data bus this is sub-optimal. (The alternative
1056 * approach is to have sub-int versions of iget-quick, but now we're wasting
1057 * Dalvik instruction space and making it less likely that handler code will
1058 * already be in the CPU i-cache.)
1059 */
1060#define HANDLE_IGET_X(_opcode, _opname, _ftype, _regsize) \
1061 HANDLE_OPCODE(_opcode /*vA, vB, field@CCCC*/) \
1062 { \
1063 InstField* ifield; \
1064 Object* obj; \
1065 EXPORT_PC(); \
1066 vdst = INST_A(inst); \
1067 vsrc1 = INST_B(inst); /* object ptr */ \
1068 ref = FETCH(1); /* field ref */ \
1069 ILOGV("|iget%s v%d,v%d,field@0x%04x", (_opname), vdst, vsrc1, ref); \
1070 obj = (Object*) GET_REGISTER(vsrc1); \
1071 if (!checkForNull(obj)) \
1072 GOTO_exceptionThrown(); \
1073 ifield = (InstField*) dvmDexGetResolvedField(methodClassDex, ref); \
1074 if (ifield == NULL) { \
1075 ifield = dvmResolveInstField(curMethod->clazz, ref); \
1076 if (ifield == NULL) \
1077 GOTO_exceptionThrown(); \
1078 } \
1079 SET_REGISTER##_regsize(vdst, \
1080 dvmGetField##_ftype(obj, ifield->byteOffset)); \
1081 ILOGV("+ IGET '%s'=0x%08llx", ifield->field.name, \
1082 (u8) GET_REGISTER##_regsize(vdst)); \
1083 UPDATE_FIELD_GET(&ifield->field); \
1084 } \
1085 FINISH(2);
1086
1087#define HANDLE_IGET_X_QUICK(_opcode, _opname, _ftype, _regsize) \
1088 HANDLE_OPCODE(_opcode /*vA, vB, field@CCCC*/) \
1089 { \
1090 Object* obj; \
1091 vdst = INST_A(inst); \
1092 vsrc1 = INST_B(inst); /* object ptr */ \
1093 ref = FETCH(1); /* field offset */ \
1094 ILOGV("|iget%s-quick v%d,v%d,field@+%u", \
1095 (_opname), vdst, vsrc1, ref); \
1096 obj = (Object*) GET_REGISTER(vsrc1); \
1097 if (!checkForNullExportPC(obj, fp, pc)) \
1098 GOTO_exceptionThrown(); \
1099 SET_REGISTER##_regsize(vdst, dvmGetField##_ftype(obj, ref)); \
1100 ILOGV("+ IGETQ %d=0x%08llx", ref, \
1101 (u8) GET_REGISTER##_regsize(vdst)); \
1102 } \
1103 FINISH(2);
1104
1105#define HANDLE_IPUT_X(_opcode, _opname, _ftype, _regsize) \
1106 HANDLE_OPCODE(_opcode /*vA, vB, field@CCCC*/) \
1107 { \
1108 InstField* ifield; \
1109 Object* obj; \
1110 EXPORT_PC(); \
1111 vdst = INST_A(inst); \
1112 vsrc1 = INST_B(inst); /* object ptr */ \
1113 ref = FETCH(1); /* field ref */ \
1114 ILOGV("|iput%s v%d,v%d,field@0x%04x", (_opname), vdst, vsrc1, ref); \
1115 obj = (Object*) GET_REGISTER(vsrc1); \
1116 if (!checkForNull(obj)) \
1117 GOTO_exceptionThrown(); \
1118 ifield = (InstField*) dvmDexGetResolvedField(methodClassDex, ref); \
1119 if (ifield == NULL) { \
1120 ifield = dvmResolveInstField(curMethod->clazz, ref); \
1121 if (ifield == NULL) \
1122 GOTO_exceptionThrown(); \
1123 } \
1124 dvmSetField##_ftype(obj, ifield->byteOffset, \
1125 GET_REGISTER##_regsize(vdst)); \
1126 ILOGV("+ IPUT '%s'=0x%08llx", ifield->field.name, \
1127 (u8) GET_REGISTER##_regsize(vdst)); \
1128 UPDATE_FIELD_PUT(&ifield->field); \
1129 } \
1130 FINISH(2);
1131
1132#define HANDLE_IPUT_X_QUICK(_opcode, _opname, _ftype, _regsize) \
1133 HANDLE_OPCODE(_opcode /*vA, vB, field@CCCC*/) \
1134 { \
1135 Object* obj; \
1136 vdst = INST_A(inst); \
1137 vsrc1 = INST_B(inst); /* object ptr */ \
1138 ref = FETCH(1); /* field offset */ \
1139 ILOGV("|iput%s-quick v%d,v%d,field@0x%04x", \
1140 (_opname), vdst, vsrc1, ref); \
1141 obj = (Object*) GET_REGISTER(vsrc1); \
1142 if (!checkForNullExportPC(obj, fp, pc)) \
1143 GOTO_exceptionThrown(); \
1144 dvmSetField##_ftype(obj, ref, GET_REGISTER##_regsize(vdst)); \
1145 ILOGV("+ IPUTQ %d=0x%08llx", ref, \
1146 (u8) GET_REGISTER##_regsize(vdst)); \
1147 } \
1148 FINISH(2);
1149
1150#define HANDLE_SGET_X(_opcode, _opname, _ftype, _regsize) \
1151 HANDLE_OPCODE(_opcode /*vAA, field@BBBB*/) \
1152 { \
1153 StaticField* sfield; \
1154 vdst = INST_AA(inst); \
1155 ref = FETCH(1); /* field ref */ \
1156 ILOGV("|sget%s v%d,sfield@0x%04x", (_opname), vdst, ref); \
1157 sfield = (StaticField*)dvmDexGetResolvedField(methodClassDex, ref); \
1158 if (sfield == NULL) { \
1159 EXPORT_PC(); \
1160 sfield = dvmResolveStaticField(curMethod->clazz, ref); \
1161 if (sfield == NULL) \
1162 GOTO_exceptionThrown(); \
1163 } \
1164 SET_REGISTER##_regsize(vdst, dvmGetStaticField##_ftype(sfield)); \
1165 ILOGV("+ SGET '%s'=0x%08llx", \
1166 sfield->field.name, (u8)GET_REGISTER##_regsize(vdst)); \
1167 UPDATE_FIELD_GET(&sfield->field); \
1168 } \
1169 FINISH(2);
1170
1171#define HANDLE_SPUT_X(_opcode, _opname, _ftype, _regsize) \
1172 HANDLE_OPCODE(_opcode /*vAA, field@BBBB*/) \
1173 { \
1174 StaticField* sfield; \
1175 vdst = INST_AA(inst); \
1176 ref = FETCH(1); /* field ref */ \
1177 ILOGV("|sput%s v%d,sfield@0x%04x", (_opname), vdst, ref); \
1178 sfield = (StaticField*)dvmDexGetResolvedField(methodClassDex, ref); \
1179 if (sfield == NULL) { \
1180 EXPORT_PC(); \
1181 sfield = dvmResolveStaticField(curMethod->clazz, ref); \
1182 if (sfield == NULL) \
1183 GOTO_exceptionThrown(); \
1184 } \
1185 dvmSetStaticField##_ftype(sfield, GET_REGISTER##_regsize(vdst)); \
1186 ILOGV("+ SPUT '%s'=0x%08llx", \
1187 sfield->field.name, (u8)GET_REGISTER##_regsize(vdst)); \
1188 UPDATE_FIELD_PUT(&sfield->field); \
1189 } \
1190 FINISH(2);
1191
1192
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001193/* File: c/gotoTargets.c */
1194/*
1195 * C footer. This has some common code shared by the various targets.
1196 */
1197
1198/*
1199 * Everything from here on is a "goto target". In the basic interpreter
1200 * we jump into these targets and then jump directly to the handler for
1201 * next instruction. Here, these are subroutines that return to the caller.
1202 */
1203
1204GOTO_TARGET(filledNewArray, bool methodCallRange)
1205 {
1206 ClassObject* arrayClass;
1207 ArrayObject* newArray;
1208 u4* contents;
1209 char typeCh;
1210 int i;
1211 u4 arg5;
1212
1213 EXPORT_PC();
1214
1215 ref = FETCH(1); /* class ref */
1216 vdst = FETCH(2); /* first 4 regs -or- range base */
1217
1218 if (methodCallRange) {
1219 vsrc1 = INST_AA(inst); /* #of elements */
1220 arg5 = -1; /* silence compiler warning */
1221 ILOGV("|filled-new-array-range args=%d @0x%04x {regs=v%d-v%d}",
1222 vsrc1, ref, vdst, vdst+vsrc1-1);
1223 } else {
1224 arg5 = INST_A(inst);
1225 vsrc1 = INST_B(inst); /* #of elements */
1226 ILOGV("|filled-new-array args=%d @0x%04x {regs=0x%04x %x}",
1227 vsrc1, ref, vdst, arg5);
1228 }
1229
1230 /*
1231 * Resolve the array class.
1232 */
1233 arrayClass = dvmDexGetResolvedClass(methodClassDex, ref);
1234 if (arrayClass == NULL) {
1235 arrayClass = dvmResolveClass(curMethod->clazz, ref, false);
1236 if (arrayClass == NULL)
1237 GOTO_exceptionThrown();
1238 }
1239 /*
1240 if (!dvmIsArrayClass(arrayClass)) {
1241 dvmThrowException("Ljava/lang/RuntimeError;",
1242 "filled-new-array needs array class");
1243 GOTO_exceptionThrown();
1244 }
1245 */
1246 /* verifier guarantees this is an array class */
1247 assert(dvmIsArrayClass(arrayClass));
1248 assert(dvmIsClassInitialized(arrayClass));
1249
1250 /*
1251 * Create an array of the specified type.
1252 */
1253 LOGVV("+++ filled-new-array type is '%s'\n", arrayClass->descriptor);
1254 typeCh = arrayClass->descriptor[1];
1255 if (typeCh == 'D' || typeCh == 'J') {
1256 /* category 2 primitives not allowed */
1257 dvmThrowException("Ljava/lang/RuntimeError;",
1258 "bad filled array req");
1259 GOTO_exceptionThrown();
1260 } else if (typeCh != 'L' && typeCh != '[' && typeCh != 'I') {
1261 /* TODO: requires multiple "fill in" loops with different widths */
1262 LOGE("non-int primitives not implemented\n");
1263 dvmThrowException("Ljava/lang/InternalError;",
1264 "filled-new-array not implemented for anything but 'int'");
1265 GOTO_exceptionThrown();
1266 }
1267
1268 newArray = dvmAllocArrayByClass(arrayClass, vsrc1, ALLOC_DONT_TRACK);
1269 if (newArray == NULL)
1270 GOTO_exceptionThrown();
1271
1272 /*
1273 * Fill in the elements. It's legal for vsrc1 to be zero.
1274 */
1275 contents = (u4*) newArray->contents;
1276 if (methodCallRange) {
1277 for (i = 0; i < vsrc1; i++)
1278 contents[i] = GET_REGISTER(vdst+i);
1279 } else {
1280 assert(vsrc1 <= 5);
1281 if (vsrc1 == 5) {
1282 contents[4] = GET_REGISTER(arg5);
1283 vsrc1--;
1284 }
1285 for (i = 0; i < vsrc1; i++) {
1286 contents[i] = GET_REGISTER(vdst & 0x0f);
1287 vdst >>= 4;
1288 }
1289 }
1290
1291 retval.l = newArray;
1292 }
1293 FINISH(3);
1294GOTO_TARGET_END
1295
1296
1297GOTO_TARGET(invokeVirtual, bool methodCallRange)
1298 {
1299 Method* baseMethod;
1300 Object* thisPtr;
1301
1302 EXPORT_PC();
1303
1304 vsrc1 = INST_AA(inst); /* AA (count) or BA (count + arg 5) */
1305 ref = FETCH(1); /* method ref */
1306 vdst = FETCH(2); /* 4 regs -or- first reg */
1307
1308 /*
1309 * The object against which we are executing a method is always
1310 * in the first argument.
1311 */
1312 if (methodCallRange) {
1313 assert(vsrc1 > 0);
1314 ILOGV("|invoke-virtual-range args=%d @0x%04x {regs=v%d-v%d}",
1315 vsrc1, ref, vdst, vdst+vsrc1-1);
1316 thisPtr = (Object*) GET_REGISTER(vdst);
1317 } else {
1318 assert((vsrc1>>4) > 0);
1319 ILOGV("|invoke-virtual args=%d @0x%04x {regs=0x%04x %x}",
1320 vsrc1 >> 4, ref, vdst, vsrc1 & 0x0f);
1321 thisPtr = (Object*) GET_REGISTER(vdst & 0x0f);
1322 }
1323
1324 if (!checkForNull(thisPtr))
1325 GOTO_exceptionThrown();
1326
1327 /*
1328 * Resolve the method. This is the correct method for the static
1329 * type of the object. We also verify access permissions here.
1330 */
1331 baseMethod = dvmDexGetResolvedMethod(methodClassDex, ref);
1332 if (baseMethod == NULL) {
1333 baseMethod = dvmResolveMethod(curMethod->clazz, ref,METHOD_VIRTUAL);
1334 if (baseMethod == NULL) {
1335 ILOGV("+ unknown method or access denied\n");
1336 GOTO_exceptionThrown();
1337 }
1338 }
1339
1340 /*
1341 * Combine the object we found with the vtable offset in the
1342 * method.
1343 */
1344 assert(baseMethod->methodIndex < thisPtr->clazz->vtableCount);
1345 methodToCall = thisPtr->clazz->vtable[baseMethod->methodIndex];
1346
1347#if 0
1348 if (dvmIsAbstractMethod(methodToCall)) {
1349 /*
1350 * This can happen if you create two classes, Base and Sub, where
1351 * Sub is a sub-class of Base. Declare a protected abstract
1352 * method foo() in Base, and invoke foo() from a method in Base.
1353 * Base is an "abstract base class" and is never instantiated
1354 * directly. Now, Override foo() in Sub, and use Sub. This
1355 * Works fine unless Sub stops providing an implementation of
1356 * the method.
1357 */
1358 dvmThrowException("Ljava/lang/AbstractMethodError;",
1359 "abstract method not implemented");
1360 GOTO_exceptionThrown();
1361 }
1362#else
1363 assert(!dvmIsAbstractMethod(methodToCall) ||
1364 methodToCall->nativeFunc != NULL);
1365#endif
1366
1367 LOGVV("+++ base=%s.%s virtual[%d]=%s.%s\n",
1368 baseMethod->clazz->descriptor, baseMethod->name,
1369 (u4) baseMethod->methodIndex,
1370 methodToCall->clazz->descriptor, methodToCall->name);
1371 assert(methodToCall != NULL);
1372
1373#if 0
1374 if (vsrc1 != methodToCall->insSize) {
1375 LOGW("WRONG METHOD: base=%s.%s virtual[%d]=%s.%s\n",
1376 baseMethod->clazz->descriptor, baseMethod->name,
1377 (u4) baseMethod->methodIndex,
1378 methodToCall->clazz->descriptor, methodToCall->name);
1379 //dvmDumpClass(baseMethod->clazz);
1380 //dvmDumpClass(methodToCall->clazz);
1381 dvmDumpAllClasses(0);
1382 }
1383#endif
1384
1385 GOTO_invokeMethod(methodCallRange, methodToCall, vsrc1, vdst);
1386 }
1387GOTO_TARGET_END
1388
1389GOTO_TARGET(invokeSuper, bool methodCallRange)
1390 {
1391 Method* baseMethod;
1392 u2 thisReg;
1393
1394 EXPORT_PC();
1395
1396 vsrc1 = INST_AA(inst); /* AA (count) or BA (count + arg 5) */
1397 ref = FETCH(1); /* method ref */
1398 vdst = FETCH(2); /* 4 regs -or- first reg */
1399
1400 if (methodCallRange) {
1401 ILOGV("|invoke-super-range args=%d @0x%04x {regs=v%d-v%d}",
1402 vsrc1, ref, vdst, vdst+vsrc1-1);
1403 thisReg = vdst;
1404 } else {
1405 ILOGV("|invoke-super args=%d @0x%04x {regs=0x%04x %x}",
1406 vsrc1 >> 4, ref, vdst, vsrc1 & 0x0f);
1407 thisReg = vdst & 0x0f;
1408 }
1409 /* impossible in well-formed code, but we must check nevertheless */
1410 if (!checkForNull((Object*) GET_REGISTER(thisReg)))
1411 GOTO_exceptionThrown();
1412
1413 /*
1414 * Resolve the method. This is the correct method for the static
1415 * type of the object. We also verify access permissions here.
1416 * The first arg to dvmResolveMethod() is just the referring class
1417 * (used for class loaders and such), so we don't want to pass
1418 * the superclass into the resolution call.
1419 */
1420 baseMethod = dvmDexGetResolvedMethod(methodClassDex, ref);
1421 if (baseMethod == NULL) {
1422 baseMethod = dvmResolveMethod(curMethod->clazz, ref,METHOD_VIRTUAL);
1423 if (baseMethod == NULL) {
1424 ILOGV("+ unknown method or access denied\n");
1425 GOTO_exceptionThrown();
1426 }
1427 }
1428
1429 /*
1430 * Combine the object we found with the vtable offset in the
1431 * method's class.
1432 *
1433 * We're using the current method's class' superclass, not the
1434 * superclass of "this". This is because we might be executing
1435 * in a method inherited from a superclass, and we want to run
1436 * in that class' superclass.
1437 */
1438 if (baseMethod->methodIndex >= curMethod->clazz->super->vtableCount) {
1439 /*
1440 * Method does not exist in the superclass. Could happen if
1441 * superclass gets updated.
1442 */
1443 dvmThrowException("Ljava/lang/NoSuchMethodError;",
1444 baseMethod->name);
1445 GOTO_exceptionThrown();
1446 }
1447 methodToCall = curMethod->clazz->super->vtable[baseMethod->methodIndex];
1448#if 0
1449 if (dvmIsAbstractMethod(methodToCall)) {
1450 dvmThrowException("Ljava/lang/AbstractMethodError;",
1451 "abstract method not implemented");
1452 GOTO_exceptionThrown();
1453 }
1454#else
1455 assert(!dvmIsAbstractMethod(methodToCall) ||
1456 methodToCall->nativeFunc != NULL);
1457#endif
1458 LOGVV("+++ base=%s.%s super-virtual=%s.%s\n",
1459 baseMethod->clazz->descriptor, baseMethod->name,
1460 methodToCall->clazz->descriptor, methodToCall->name);
1461 assert(methodToCall != NULL);
1462
1463 GOTO_invokeMethod(methodCallRange, methodToCall, vsrc1, vdst);
1464 }
1465GOTO_TARGET_END
1466
1467GOTO_TARGET(invokeInterface, bool methodCallRange)
1468 {
1469 Object* thisPtr;
1470 ClassObject* thisClass;
1471
1472 EXPORT_PC();
1473
1474 vsrc1 = INST_AA(inst); /* AA (count) or BA (count + arg 5) */
1475 ref = FETCH(1); /* method ref */
1476 vdst = FETCH(2); /* 4 regs -or- first reg */
1477
1478 /*
1479 * The object against which we are executing a method is always
1480 * in the first argument.
1481 */
1482 if (methodCallRange) {
1483 assert(vsrc1 > 0);
1484 ILOGV("|invoke-interface-range args=%d @0x%04x {regs=v%d-v%d}",
1485 vsrc1, ref, vdst, vdst+vsrc1-1);
1486 thisPtr = (Object*) GET_REGISTER(vdst);
1487 } else {
1488 assert((vsrc1>>4) > 0);
1489 ILOGV("|invoke-interface args=%d @0x%04x {regs=0x%04x %x}",
1490 vsrc1 >> 4, ref, vdst, vsrc1 & 0x0f);
1491 thisPtr = (Object*) GET_REGISTER(vdst & 0x0f);
1492 }
1493 if (!checkForNull(thisPtr))
1494 GOTO_exceptionThrown();
1495
1496 thisClass = thisPtr->clazz;
1497
1498 /*
1499 * Given a class and a method index, find the Method* with the
1500 * actual code we want to execute.
1501 */
1502 methodToCall = dvmFindInterfaceMethodInCache(thisClass, ref, curMethod,
1503 methodClassDex);
1504 if (methodToCall == NULL) {
1505 assert(dvmCheckException(self));
1506 GOTO_exceptionThrown();
1507 }
1508
1509 GOTO_invokeMethod(methodCallRange, methodToCall, vsrc1, vdst);
1510 }
1511GOTO_TARGET_END
1512
1513GOTO_TARGET(invokeDirect, bool methodCallRange)
1514 {
1515 u2 thisReg;
1516
1517 vsrc1 = INST_AA(inst); /* AA (count) or BA (count + arg 5) */
1518 ref = FETCH(1); /* method ref */
1519 vdst = FETCH(2); /* 4 regs -or- first reg */
1520
1521 EXPORT_PC();
1522
1523 if (methodCallRange) {
1524 ILOGV("|invoke-direct-range args=%d @0x%04x {regs=v%d-v%d}",
1525 vsrc1, ref, vdst, vdst+vsrc1-1);
1526 thisReg = vdst;
1527 } else {
1528 ILOGV("|invoke-direct args=%d @0x%04x {regs=0x%04x %x}",
1529 vsrc1 >> 4, ref, vdst, vsrc1 & 0x0f);
1530 thisReg = vdst & 0x0f;
1531 }
1532 if (!checkForNull((Object*) GET_REGISTER(thisReg)))
1533 GOTO_exceptionThrown();
1534
1535 methodToCall = dvmDexGetResolvedMethod(methodClassDex, ref);
1536 if (methodToCall == NULL) {
1537 methodToCall = dvmResolveMethod(curMethod->clazz, ref,
1538 METHOD_DIRECT);
1539 if (methodToCall == NULL) {
1540 ILOGV("+ unknown direct method\n"); // should be impossible
1541 GOTO_exceptionThrown();
1542 }
1543 }
1544 GOTO_invokeMethod(methodCallRange, methodToCall, vsrc1, vdst);
1545 }
1546GOTO_TARGET_END
1547
1548GOTO_TARGET(invokeStatic, bool methodCallRange)
1549 vsrc1 = INST_AA(inst); /* AA (count) or BA (count + arg 5) */
1550 ref = FETCH(1); /* method ref */
1551 vdst = FETCH(2); /* 4 regs -or- first reg */
1552
1553 EXPORT_PC();
1554
1555 if (methodCallRange)
1556 ILOGV("|invoke-static-range args=%d @0x%04x {regs=v%d-v%d}",
1557 vsrc1, ref, vdst, vdst+vsrc1-1);
1558 else
1559 ILOGV("|invoke-static args=%d @0x%04x {regs=0x%04x %x}",
1560 vsrc1 >> 4, ref, vdst, vsrc1 & 0x0f);
1561
1562 methodToCall = dvmDexGetResolvedMethod(methodClassDex, ref);
1563 if (methodToCall == NULL) {
1564 methodToCall = dvmResolveMethod(curMethod->clazz, ref, METHOD_STATIC);
1565 if (methodToCall == NULL) {
1566 ILOGV("+ unknown method\n");
1567 GOTO_exceptionThrown();
1568 }
1569 }
1570 GOTO_invokeMethod(methodCallRange, methodToCall, vsrc1, vdst);
1571GOTO_TARGET_END
1572
1573GOTO_TARGET(invokeVirtualQuick, bool methodCallRange)
1574 {
1575 Object* thisPtr;
1576
1577 EXPORT_PC();
1578
1579 vsrc1 = INST_AA(inst); /* AA (count) or BA (count + arg 5) */
1580 ref = FETCH(1); /* vtable index */
1581 vdst = FETCH(2); /* 4 regs -or- first reg */
1582
1583 /*
1584 * The object against which we are executing a method is always
1585 * in the first argument.
1586 */
1587 if (methodCallRange) {
1588 assert(vsrc1 > 0);
1589 ILOGV("|invoke-virtual-quick-range args=%d @0x%04x {regs=v%d-v%d}",
1590 vsrc1, ref, vdst, vdst+vsrc1-1);
1591 thisPtr = (Object*) GET_REGISTER(vdst);
1592 } else {
1593 assert((vsrc1>>4) > 0);
1594 ILOGV("|invoke-virtual-quick args=%d @0x%04x {regs=0x%04x %x}",
1595 vsrc1 >> 4, ref, vdst, vsrc1 & 0x0f);
1596 thisPtr = (Object*) GET_REGISTER(vdst & 0x0f);
1597 }
1598
1599 if (!checkForNull(thisPtr))
1600 GOTO_exceptionThrown();
1601
1602 /*
1603 * Combine the object we found with the vtable offset in the
1604 * method.
1605 */
1606 assert(ref < thisPtr->clazz->vtableCount);
1607 methodToCall = thisPtr->clazz->vtable[ref];
1608
1609#if 0
1610 if (dvmIsAbstractMethod(methodToCall)) {
1611 dvmThrowException("Ljava/lang/AbstractMethodError;",
1612 "abstract method not implemented");
1613 GOTO_exceptionThrown();
1614 }
1615#else
1616 assert(!dvmIsAbstractMethod(methodToCall) ||
1617 methodToCall->nativeFunc != NULL);
1618#endif
1619
1620 LOGVV("+++ virtual[%d]=%s.%s\n",
1621 ref, methodToCall->clazz->descriptor, methodToCall->name);
1622 assert(methodToCall != NULL);
1623
1624 GOTO_invokeMethod(methodCallRange, methodToCall, vsrc1, vdst);
1625 }
1626GOTO_TARGET_END
1627
1628GOTO_TARGET(invokeSuperQuick, bool methodCallRange)
1629 {
1630 u2 thisReg;
1631
1632 EXPORT_PC();
1633
1634 vsrc1 = INST_AA(inst); /* AA (count) or BA (count + arg 5) */
1635 ref = FETCH(1); /* vtable index */
1636 vdst = FETCH(2); /* 4 regs -or- first reg */
1637
1638 if (methodCallRange) {
1639 ILOGV("|invoke-super-quick-range args=%d @0x%04x {regs=v%d-v%d}",
1640 vsrc1, ref, vdst, vdst+vsrc1-1);
1641 thisReg = vdst;
1642 } else {
1643 ILOGV("|invoke-super-quick args=%d @0x%04x {regs=0x%04x %x}",
1644 vsrc1 >> 4, ref, vdst, vsrc1 & 0x0f);
1645 thisReg = vdst & 0x0f;
1646 }
1647 /* impossible in well-formed code, but we must check nevertheless */
1648 if (!checkForNull((Object*) GET_REGISTER(thisReg)))
1649 GOTO_exceptionThrown();
1650
1651#if 0 /* impossible in optimized + verified code */
1652 if (ref >= curMethod->clazz->super->vtableCount) {
1653 dvmThrowException("Ljava/lang/NoSuchMethodError;", NULL);
1654 GOTO_exceptionThrown();
1655 }
1656#else
1657 assert(ref < curMethod->clazz->super->vtableCount);
1658#endif
1659
1660 /*
1661 * Combine the object we found with the vtable offset in the
1662 * method's class.
1663 *
1664 * We're using the current method's class' superclass, not the
1665 * superclass of "this". This is because we might be executing
1666 * in a method inherited from a superclass, and we want to run
1667 * in the method's class' superclass.
1668 */
1669 methodToCall = curMethod->clazz->super->vtable[ref];
1670
1671#if 0
1672 if (dvmIsAbstractMethod(methodToCall)) {
1673 dvmThrowException("Ljava/lang/AbstractMethodError;",
1674 "abstract method not implemented");
1675 GOTO_exceptionThrown();
1676 }
1677#else
1678 assert(!dvmIsAbstractMethod(methodToCall) ||
1679 methodToCall->nativeFunc != NULL);
1680#endif
1681 LOGVV("+++ super-virtual[%d]=%s.%s\n",
1682 ref, methodToCall->clazz->descriptor, methodToCall->name);
1683 assert(methodToCall != NULL);
1684
1685 GOTO_invokeMethod(methodCallRange, methodToCall, vsrc1, vdst);
1686 }
1687GOTO_TARGET_END
1688
1689
1690
1691 /*
1692 * General handling for return-void, return, and return-wide. Put the
1693 * return value in "retval" before jumping here.
1694 */
1695GOTO_TARGET(returnFromMethod)
1696 {
1697 StackSaveArea* saveArea;
1698
1699 /*
1700 * We must do this BEFORE we pop the previous stack frame off, so
1701 * that the GC can see the return value (if any) in the local vars.
1702 *
1703 * Since this is now an interpreter switch point, we must do it before
1704 * we do anything at all.
1705 */
1706 PERIODIC_CHECKS(kInterpEntryReturn, 0);
1707
1708 ILOGV("> retval=0x%llx (leaving %s.%s %s)",
1709 retval.j, curMethod->clazz->descriptor, curMethod->name,
Mike Lockwood85745e12009-07-08 12:39:37 -04001710 curMethod->shorty);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001711 //DUMP_REGS(curMethod, fp);
1712
1713 saveArea = SAVEAREA_FROM_FP(fp);
1714
1715#ifdef EASY_GDB
1716 debugSaveArea = saveArea;
1717#endif
1718#if (INTERP_TYPE == INTERP_DBG) && defined(WITH_PROFILER)
1719 TRACE_METHOD_EXIT(self, curMethod);
1720#endif
1721
1722 /* back up to previous frame and see if we hit a break */
1723 fp = saveArea->prevFrame;
1724 assert(fp != NULL);
1725 if (dvmIsBreakFrame(fp)) {
1726 /* bail without popping the method frame from stack */
1727 LOGVV("+++ returned into break frame\n");
Bill Buzbeed7269912009-11-10 14:31:32 -08001728#if defined(WITH_JIT)
1729 /* Let the Jit know the return is terminating normally */
1730 CHECK_JIT();
1731#endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001732 GOTO_bail();
1733 }
1734
1735 /* update thread FP, and reset local variables */
1736 self->curFrame = fp;
1737 curMethod = SAVEAREA_FROM_FP(fp)->method;
1738 //methodClass = curMethod->clazz;
1739 methodClassDex = curMethod->clazz->pDvmDex;
1740 pc = saveArea->savedPc;
1741 ILOGD("> (return to %s.%s %s)", curMethod->clazz->descriptor,
Mike Lockwood85745e12009-07-08 12:39:37 -04001742 curMethod->name, curMethod->shorty);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001743
1744 /* use FINISH on the caller's invoke instruction */
1745 //u2 invokeInstr = INST_INST(FETCH(0));
1746 if (true /*invokeInstr >= OP_INVOKE_VIRTUAL &&
1747 invokeInstr <= OP_INVOKE_INTERFACE*/)
1748 {
1749 FINISH(3);
1750 } else {
1751 //LOGE("Unknown invoke instr %02x at %d\n",
1752 // invokeInstr, (int) (pc - curMethod->insns));
1753 assert(false);
1754 }
1755 }
1756GOTO_TARGET_END
1757
1758
1759 /*
1760 * Jump here when the code throws an exception.
1761 *
1762 * By the time we get here, the Throwable has been created and the stack
1763 * trace has been saved off.
1764 */
1765GOTO_TARGET(exceptionThrown)
1766 {
1767 Object* exception;
1768 int catchRelPc;
1769
1770 /*
1771 * Since this is now an interpreter switch point, we must do it before
1772 * we do anything at all.
1773 */
1774 PERIODIC_CHECKS(kInterpEntryThrow, 0);
1775
Ben Cheng79d173c2009-09-29 16:12:51 -07001776#if defined(WITH_JIT)
1777 // Something threw during trace selection - abort the current trace
Bill Buzbeed7269912009-11-10 14:31:32 -08001778 dvmJitAbortTraceSelect(interpState);
Ben Cheng79d173c2009-09-29 16:12:51 -07001779#endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001780 /*
1781 * We save off the exception and clear the exception status. While
1782 * processing the exception we might need to load some Throwable
1783 * classes, and we don't want class loader exceptions to get
1784 * confused with this one.
1785 */
1786 assert(dvmCheckException(self));
1787 exception = dvmGetException(self);
1788 dvmAddTrackedAlloc(exception, self);
1789 dvmClearException(self);
1790
1791 LOGV("Handling exception %s at %s:%d\n",
1792 exception->clazz->descriptor, curMethod->name,
1793 dvmLineNumFromPC(curMethod, pc - curMethod->insns));
1794
1795#if (INTERP_TYPE == INTERP_DBG) && defined(WITH_DEBUGGER)
1796 /*
1797 * Tell the debugger about it.
1798 *
1799 * TODO: if the exception was thrown by interpreted code, control
1800 * fell through native, and then back to us, we will report the
1801 * exception at the point of the throw and again here. We can avoid
1802 * this by not reporting exceptions when we jump here directly from
1803 * the native call code above, but then we won't report exceptions
1804 * that were thrown *from* the JNI code (as opposed to *through* it).
1805 *
1806 * The correct solution is probably to ignore from-native exceptions
1807 * here, and have the JNI exception code do the reporting to the
1808 * debugger.
1809 */
1810 if (gDvm.debuggerActive) {
1811 void* catchFrame;
1812 catchRelPc = dvmFindCatchBlock(self, pc - curMethod->insns,
1813 exception, true, &catchFrame);
1814 dvmDbgPostException(fp, pc - curMethod->insns, catchFrame,
1815 catchRelPc, exception);
1816 }
1817#endif
1818
1819 /*
1820 * We need to unroll to the catch block or the nearest "break"
1821 * frame.
1822 *
1823 * A break frame could indicate that we have reached an intermediate
1824 * native call, or have gone off the top of the stack and the thread
1825 * needs to exit. Either way, we return from here, leaving the
1826 * exception raised.
1827 *
1828 * If we do find a catch block, we want to transfer execution to
1829 * that point.
1830 */
1831 catchRelPc = dvmFindCatchBlock(self, pc - curMethod->insns,
1832 exception, false, (void*)&fp);
1833
1834 /*
1835 * Restore the stack bounds after an overflow. This isn't going to
1836 * be correct in all circumstances, e.g. if JNI code devours the
1837 * exception this won't happen until some other exception gets
1838 * thrown. If the code keeps pushing the stack bounds we'll end
1839 * up aborting the VM.
1840 *
1841 * Note we want to do this *after* the call to dvmFindCatchBlock,
1842 * because that may need extra stack space to resolve exception
1843 * classes (e.g. through a class loader).
1844 */
1845 if (self->stackOverflowed)
1846 dvmCleanupStackOverflow(self);
1847
1848 if (catchRelPc < 0) {
1849 /* falling through to JNI code or off the bottom of the stack */
1850#if DVM_SHOW_EXCEPTION >= 2
1851 LOGD("Exception %s from %s:%d not caught locally\n",
1852 exception->clazz->descriptor, dvmGetMethodSourceFile(curMethod),
1853 dvmLineNumFromPC(curMethod, pc - curMethod->insns));
1854#endif
1855 dvmSetException(self, exception);
1856 dvmReleaseTrackedAlloc(exception, self);
1857 GOTO_bail();
1858 }
1859
1860#if DVM_SHOW_EXCEPTION >= 3
1861 {
1862 const Method* catchMethod = SAVEAREA_FROM_FP(fp)->method;
1863 LOGD("Exception %s thrown from %s:%d to %s:%d\n",
1864 exception->clazz->descriptor, dvmGetMethodSourceFile(curMethod),
1865 dvmLineNumFromPC(curMethod, pc - curMethod->insns),
1866 dvmGetMethodSourceFile(catchMethod),
1867 dvmLineNumFromPC(catchMethod, catchRelPc));
1868 }
1869#endif
1870
1871 /*
1872 * Adjust local variables to match self->curFrame and the
1873 * updated PC.
1874 */
1875 //fp = (u4*) self->curFrame;
1876 curMethod = SAVEAREA_FROM_FP(fp)->method;
1877 //methodClass = curMethod->clazz;
1878 methodClassDex = curMethod->clazz->pDvmDex;
1879 pc = curMethod->insns + catchRelPc;
1880 ILOGV("> pc <-- %s.%s %s", curMethod->clazz->descriptor,
Mike Lockwood85745e12009-07-08 12:39:37 -04001881 curMethod->name, curMethod->shorty);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001882 DUMP_REGS(curMethod, fp, false); // show all regs
1883
1884 /*
1885 * Restore the exception if the handler wants it.
1886 *
1887 * The Dalvik spec mandates that, if an exception handler wants to
1888 * do something with the exception, the first instruction executed
1889 * must be "move-exception". We can pass the exception along
1890 * through the thread struct, and let the move-exception instruction
1891 * clear it for us.
1892 *
1893 * If the handler doesn't call move-exception, we don't want to
1894 * finish here with an exception still pending.
1895 */
1896 if (INST_INST(FETCH(0)) == OP_MOVE_EXCEPTION)
1897 dvmSetException(self, exception);
1898
1899 dvmReleaseTrackedAlloc(exception, self);
1900 FINISH(0);
1901 }
1902GOTO_TARGET_END
1903
1904
1905 /*
1906 * General handling for invoke-{virtual,super,direct,static,interface},
1907 * including "quick" variants.
1908 *
1909 * Set "methodToCall" to the Method we're calling, and "methodCallRange"
1910 * depending on whether this is a "/range" instruction.
1911 *
1912 * For a range call:
1913 * "vsrc1" holds the argument count (8 bits)
1914 * "vdst" holds the first argument in the range
1915 * For a non-range call:
1916 * "vsrc1" holds the argument count (4 bits) and the 5th argument index
1917 * "vdst" holds four 4-bit register indices
1918 *
1919 * The caller must EXPORT_PC before jumping here, because any method
1920 * call can throw a stack overflow exception.
1921 */
1922GOTO_TARGET(invokeMethod, bool methodCallRange, const Method* _methodToCall,
1923 u2 count, u2 regs)
1924 {
1925 STUB_HACK(vsrc1 = count; vdst = regs; methodToCall = _methodToCall;);
1926
1927 //printf("range=%d call=%p count=%d regs=0x%04x\n",
1928 // methodCallRange, methodToCall, count, regs);
1929 //printf(" --> %s.%s %s\n", methodToCall->clazz->descriptor,
Mike Lockwood85745e12009-07-08 12:39:37 -04001930 // methodToCall->name, methodToCall->shorty);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001931
1932 u4* outs;
1933 int i;
1934
1935 /*
1936 * Copy args. This may corrupt vsrc1/vdst.
1937 */
1938 if (methodCallRange) {
1939 // could use memcpy or a "Duff's device"; most functions have
1940 // so few args it won't matter much
1941 assert(vsrc1 <= curMethod->outsSize);
1942 assert(vsrc1 == methodToCall->insSize);
1943 outs = OUTS_FROM_FP(fp, vsrc1);
1944 for (i = 0; i < vsrc1; i++)
1945 outs[i] = GET_REGISTER(vdst+i);
1946 } else {
1947 u4 count = vsrc1 >> 4;
1948
1949 assert(count <= curMethod->outsSize);
1950 assert(count == methodToCall->insSize);
1951 assert(count <= 5);
1952
1953 outs = OUTS_FROM_FP(fp, count);
1954#if 0
1955 if (count == 5) {
1956 outs[4] = GET_REGISTER(vsrc1 & 0x0f);
1957 count--;
1958 }
1959 for (i = 0; i < (int) count; i++) {
1960 outs[i] = GET_REGISTER(vdst & 0x0f);
1961 vdst >>= 4;
1962 }
1963#else
1964 // This version executes fewer instructions but is larger
1965 // overall. Seems to be a teensy bit faster.
1966 assert((vdst >> 16) == 0); // 16 bits -or- high 16 bits clear
1967 switch (count) {
1968 case 5:
1969 outs[4] = GET_REGISTER(vsrc1 & 0x0f);
1970 case 4:
1971 outs[3] = GET_REGISTER(vdst >> 12);
1972 case 3:
1973 outs[2] = GET_REGISTER((vdst & 0x0f00) >> 8);
1974 case 2:
1975 outs[1] = GET_REGISTER((vdst & 0x00f0) >> 4);
1976 case 1:
1977 outs[0] = GET_REGISTER(vdst & 0x0f);
1978 default:
1979 ;
1980 }
1981#endif
1982 }
1983 }
1984
1985 /*
1986 * (This was originally a "goto" target; I've kept it separate from the
1987 * stuff above in case we want to refactor things again.)
1988 *
1989 * At this point, we have the arguments stored in the "outs" area of
1990 * the current method's stack frame, and the method to call in
1991 * "methodToCall". Push a new stack frame.
1992 */
1993 {
1994 StackSaveArea* newSaveArea;
1995 u4* newFp;
1996
1997 ILOGV("> %s%s.%s %s",
1998 dvmIsNativeMethod(methodToCall) ? "(NATIVE) " : "",
1999 methodToCall->clazz->descriptor, methodToCall->name,
Mike Lockwood85745e12009-07-08 12:39:37 -04002000 methodToCall->shorty);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002001
2002 newFp = (u4*) SAVEAREA_FROM_FP(fp) - methodToCall->registersSize;
2003 newSaveArea = SAVEAREA_FROM_FP(newFp);
2004
2005 /* verify that we have enough space */
2006 if (true) {
2007 u1* bottom;
2008 bottom = (u1*) newSaveArea - methodToCall->outsSize * sizeof(u4);
2009 if (bottom < self->interpStackEnd) {
2010 /* stack overflow */
Andy McFadden6ed1a0f2009-09-10 15:34:19 -07002011 LOGV("Stack overflow on method call (start=%p end=%p newBot=%p(%d) size=%d '%s')\n",
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002012 self->interpStackStart, self->interpStackEnd, bottom,
Andy McFadden6ed1a0f2009-09-10 15:34:19 -07002013 (u1*) fp - bottom, self->interpStackSize,
2014 methodToCall->name);
2015 dvmHandleStackOverflow(self, methodToCall);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002016 assert(dvmCheckException(self));
2017 GOTO_exceptionThrown();
2018 }
2019 //LOGD("+++ fp=%p newFp=%p newSave=%p bottom=%p\n",
2020 // fp, newFp, newSaveArea, bottom);
2021 }
2022
2023#ifdef LOG_INSTR
2024 if (methodToCall->registersSize > methodToCall->insSize) {
2025 /*
2026 * This makes valgrind quiet when we print registers that
2027 * haven't been initialized. Turn it off when the debug
2028 * messages are disabled -- we want valgrind to report any
2029 * used-before-initialized issues.
2030 */
2031 memset(newFp, 0xcc,
2032 (methodToCall->registersSize - methodToCall->insSize) * 4);
2033 }
2034#endif
2035
2036#ifdef EASY_GDB
2037 newSaveArea->prevSave = SAVEAREA_FROM_FP(fp);
2038#endif
2039 newSaveArea->prevFrame = fp;
2040 newSaveArea->savedPc = pc;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002041#if defined(WITH_JIT)
2042 newSaveArea->returnAddr = 0;
2043#endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002044 newSaveArea->method = methodToCall;
2045
2046 if (!dvmIsNativeMethod(methodToCall)) {
2047 /*
2048 * "Call" interpreted code. Reposition the PC, update the
2049 * frame pointer and other local state, and continue.
2050 */
2051 curMethod = methodToCall;
2052 methodClassDex = curMethod->clazz->pDvmDex;
2053 pc = methodToCall->insns;
2054 fp = self->curFrame = newFp;
2055#ifdef EASY_GDB
2056 debugSaveArea = SAVEAREA_FROM_FP(newFp);
2057#endif
2058#if INTERP_TYPE == INTERP_DBG
2059 debugIsMethodEntry = true; // profiling, debugging
2060#endif
2061 ILOGD("> pc <-- %s.%s %s", curMethod->clazz->descriptor,
Mike Lockwood85745e12009-07-08 12:39:37 -04002062 curMethod->name, curMethod->shorty);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002063 DUMP_REGS(curMethod, fp, true); // show input args
2064 FINISH(0); // jump to method start
2065 } else {
2066 /* set this up for JNI locals, even if not a JNI native */
Andy McFaddend5ab7262009-08-25 07:19:34 -07002067#ifdef USE_INDIRECT_REF
2068 newSaveArea->xtra.localRefCookie = self->jniLocalRefTable.segmentState.all;
2069#else
2070 newSaveArea->xtra.localRefCookie = self->jniLocalRefTable.nextEntry;
2071#endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002072
2073 self->curFrame = newFp;
2074
2075 DUMP_REGS(methodToCall, newFp, true); // show input args
2076
2077#if (INTERP_TYPE == INTERP_DBG) && defined(WITH_DEBUGGER)
2078 if (gDvm.debuggerActive) {
2079 dvmDbgPostLocationEvent(methodToCall, -1,
2080 dvmGetThisPtr(curMethod, fp), DBG_METHOD_ENTRY);
2081 }
2082#endif
2083#if (INTERP_TYPE == INTERP_DBG) && defined(WITH_PROFILER)
2084 TRACE_METHOD_ENTER(self, methodToCall);
2085#endif
2086
2087 ILOGD("> native <-- %s.%s %s", methodToCall->clazz->descriptor,
Mike Lockwood85745e12009-07-08 12:39:37 -04002088 methodToCall->name, methodToCall->shorty);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002089
Bill Buzbeed7269912009-11-10 14:31:32 -08002090#if defined(WITH_JIT)
2091 /* Allow the Jit to end any pending trace building */
2092 CHECK_JIT();
2093#endif
2094
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002095 /*
2096 * Jump through native call bridge. Because we leave no
2097 * space for locals on native calls, "newFp" points directly
2098 * to the method arguments.
2099 */
2100 (*methodToCall->nativeFunc)(newFp, &retval, methodToCall, self);
2101
2102#if (INTERP_TYPE == INTERP_DBG) && defined(WITH_DEBUGGER)
2103 if (gDvm.debuggerActive) {
2104 dvmDbgPostLocationEvent(methodToCall, -1,
2105 dvmGetThisPtr(curMethod, fp), DBG_METHOD_EXIT);
2106 }
2107#endif
2108#if (INTERP_TYPE == INTERP_DBG) && defined(WITH_PROFILER)
2109 TRACE_METHOD_EXIT(self, methodToCall);
2110#endif
2111
2112 /* pop frame off */
2113 dvmPopJniLocals(self, newSaveArea);
2114 self->curFrame = fp;
2115
2116 /*
2117 * If the native code threw an exception, or interpreted code
2118 * invoked by the native call threw one and nobody has cleared
2119 * it, jump to our local exception handling.
2120 */
2121 if (dvmCheckException(self)) {
2122 LOGV("Exception thrown by/below native code\n");
2123 GOTO_exceptionThrown();
2124 }
2125
2126 ILOGD("> retval=0x%llx (leaving native)", retval.j);
2127 ILOGD("> (return from native %s.%s to %s.%s %s)",
2128 methodToCall->clazz->descriptor, methodToCall->name,
2129 curMethod->clazz->descriptor, curMethod->name,
Mike Lockwood85745e12009-07-08 12:39:37 -04002130 curMethod->shorty);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002131
2132 //u2 invokeInstr = INST_INST(FETCH(0));
2133 if (true /*invokeInstr >= OP_INVOKE_VIRTUAL &&
2134 invokeInstr <= OP_INVOKE_INTERFACE*/)
2135 {
2136 FINISH(3);
2137 } else {
2138 //LOGE("Unknown invoke instr %02x at %d\n",
2139 // invokeInstr, (int) (pc - curMethod->insns));
2140 assert(false);
2141 }
2142 }
2143 }
2144 assert(false); // should not get here
2145GOTO_TARGET_END
2146
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08002147/* File: cstubs/enddefs.c */
2148
2149/* undefine "magic" name remapping */
2150#undef retval
2151#undef pc
2152#undef fp
2153#undef curMethod
2154#undef methodClassDex
2155#undef self
2156#undef debugTrackedRefStart
2157