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