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