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