Object.h revision c35a2ef53d0cccd6f924eeba36633220ec67c32e
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * Declaration of the fundamental Object type and refinements thereof, plus
19 * some functions for manipulating them.
20 */
21#ifndef _DALVIK_OO_OBJECT
22#define _DALVIK_OO_OBJECT
23
24#include <Atomic.h>
25
26#include <stddef.h>
27
28/* fwd decl */
29struct DataObject;
30struct InitiatingLoaderList;
31struct ClassObject;
32struct StringObject;
33struct ArrayObject;
34struct Method;
35struct ExceptionEntry;
36struct LineNumEntry;
37struct StaticField;
38struct InstField;
39struct Field;
40struct RegisterMap;
41typedef struct DataObject DataObject;
42typedef struct InitiatingLoaderList InitiatingLoaderList;
43typedef struct ClassObject ClassObject;
44typedef struct StringObject StringObject;
45typedef struct ArrayObject ArrayObject;
46typedef struct Method Method;
47typedef struct ExceptionEntry ExceptionEntry;
48typedef struct LineNumEntry LineNumEntry;
49typedef struct StaticField StaticField;
50typedef struct InstField InstField;
51typedef struct Field Field;
52typedef struct RegisterMap RegisterMap;
53
54/*
55 * Native function pointer type.
56 *
57 * "args[0]" holds the "this" pointer for virtual methods.
58 *
59 * The "Bridge" form is a super-set of the "Native" form; in many places
60 * they are used interchangeably.  Currently, all functions have all
61 * arguments passed in, but some functions only care about the first two.
62 * Passing extra arguments to a C function is (mostly) harmless.
63 */
64typedef void (*DalvikBridgeFunc)(const u4* args, JValue* pResult,
65    const Method* method, struct Thread* self);
66typedef void (*DalvikNativeFunc)(const u4* args, JValue* pResult);
67
68
69/* vm-internal access flags and related definitions */
70typedef enum AccessFlags {
71    ACC_MIRANDA         = 0x8000,       // method (internal to VM)
72    JAVA_FLAGS_MASK     = 0xffff,       // bits set from Java sources (low 16)
73} AccessFlags;
74
75/* Use the top 16 bits of the access flags field for
76 * other class flags.  Code should use the *CLASS_FLAG*()
77 * macros to set/get these flags.
78 */
79typedef enum ClassFlags {
80    CLASS_ISFINALIZABLE     = (1<<31),  // class/ancestor overrides finalize()
81    CLASS_ISARRAY           = (1<<30),  // class is a "[*"
82    CLASS_ISOBJECTARRAY     = (1<<29),  // class is a "[L*" or "[[*"
83    CLASS_ISREFERENCE       = (1<<28),  // class is a soft/weak/phantom ref
84                                        // only ISREFERENCE is set --> soft
85    CLASS_ISWEAKREFERENCE   = (1<<27),  // class is a weak reference
86    CLASS_ISPHANTOMREFERENCE = (1<<26), // class is a phantom reference
87
88    CLASS_MULTIPLE_DEFS     = (1<<25),  // DEX verifier: defs in multiple DEXs
89
90    /* unlike the others, these can be present in the optimized DEX file */
91    CLASS_ISOPTIMIZED       = (1<<17),  // class may contain opt instrs
92    CLASS_ISPREVERIFIED     = (1<<16),  // class has been pre-verified
93} ClassFlags;
94
95/* bits we can reasonably expect to see set in a DEX access flags field */
96#define EXPECTED_FILE_FLAGS \
97    (ACC_CLASS_MASK | CLASS_ISPREVERIFIED | CLASS_ISOPTIMIZED)
98
99/*
100 * Get/set class flags.
101 */
102#define SET_CLASS_FLAG(clazz, flag) \
103    do { (clazz)->accessFlags |= (flag); } while (0)
104
105#define CLEAR_CLASS_FLAG(clazz, flag) \
106    do { (clazz)->accessFlags &= ~(flag); } while (0)
107
108#define IS_CLASS_FLAG_SET(clazz, flag) \
109    (((clazz)->accessFlags & (flag)) != 0)
110
111#define GET_CLASS_FLAG_GROUP(clazz, flags) \
112    ((u4)((clazz)->accessFlags & (flags)))
113
114/*
115 * Use the top 16 bits of the access flags field for other method flags.
116 * Code should use the *METHOD_FLAG*() macros to set/get these flags.
117 */
118typedef enum MethodFlags {
119    METHOD_ISWRITABLE       = (1<<31),  // the method's code is writable
120} MethodFlags;
121
122/*
123 * Get/set method flags.
124 */
125#define SET_METHOD_FLAG(method, flag) \
126    do { (method)->accessFlags |= (flag); } while (0)
127
128#define CLEAR_METHOD_FLAG(method, flag) \
129    do { (method)->accessFlags &= ~(flag); } while (0)
130
131#define IS_METHOD_FLAG_SET(method, flag) \
132    (((method)->accessFlags & (flag)) != 0)
133
134#define GET_METHOD_FLAG_GROUP(method, flags) \
135    ((u4)((method)->accessFlags & (flags)))
136
137/* current state of the class, increasing as we progress */
138typedef enum ClassStatus {
139    CLASS_ERROR         = -1,
140
141    CLASS_NOTREADY      = 0,
142    CLASS_IDX           = 1,    /* loaded, DEX idx in super or ifaces */
143    CLASS_LOADED        = 2,    /* DEX idx values resolved */
144    CLASS_RESOLVED      = 3,    /* part of linking */
145    CLASS_VERIFYING     = 4,    /* in the process of being verified */
146    CLASS_VERIFIED      = 5,    /* logically part of linking; done pre-init */
147    CLASS_INITIALIZING  = 6,    /* class init in progress */
148    CLASS_INITIALIZED   = 7,    /* ready to go */
149} ClassStatus;
150
151/*
152 * Primitive type identifiers.  We use these values as indexes into an
153 * array of synthesized classes, so these start at zero and count up.
154 * The order is arbitrary (mimics table in doc for newarray opcode),
155 * but can't be changed without shuffling some reflection tables.
156 *
157 * PRIM_VOID can't be used as an array type, but we include it here for
158 * other uses (e.g. Void.TYPE).
159 */
160typedef enum PrimitiveType {
161    PRIM_NOT        = -1,       /* value is not a primitive type */
162    PRIM_BOOLEAN    = 0,
163    PRIM_CHAR       = 1,
164    PRIM_FLOAT      = 2,
165    PRIM_DOUBLE     = 3,
166    PRIM_BYTE       = 4,
167    PRIM_SHORT      = 5,
168    PRIM_INT        = 6,
169    PRIM_LONG       = 7,
170    PRIM_VOID       = 8,
171
172    PRIM_MAX
173} PrimitiveType;
174#define PRIM_TYPE_TO_LETTER "ZCFDBSIJV"     /* must match order in enum */
175
176/*
177 * Definitions for packing refOffsets in ClassObject.
178 */
179/*
180 * A magic value for refOffsets. Ignore the bits and walk the super
181 * chain when this is the value.
182 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
183 * fields followed by 2 ref instance fields.]
184 */
185#define CLASS_WALK_SUPER ((unsigned int)(3))
186#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
187#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
188#define CLASS_OFFSET_ALIGNMENT 4
189#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
190/*
191 * Given an offset, return the bit number which would encode that offset.
192 * Local use only.
193 */
194#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
195    (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
196     CLASS_OFFSET_ALIGNMENT)
197/*
198 * Is the given offset too large to be encoded?
199 */
200#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
201    (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
202/*
203 * Return a single bit, encoding the offset.
204 * Undefined if the offset is too large, as defined above.
205 */
206#define CLASS_BIT_FROM_OFFSET(byteOffset) \
207    (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
208/*
209 * Return an offset, given a bit number as returned from CLZ.
210 */
211#define CLASS_OFFSET_FROM_CLZ(rshift) \
212    (((int)(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
213
214
215/*
216 * Used for iftable in ClassObject.
217 */
218typedef struct InterfaceEntry {
219    /* pointer to interface class */
220    ClassObject*    clazz;
221
222    /*
223     * Index into array of vtable offsets.  This points into the ifviPool,
224     * which holds the vtables for all interfaces declared by this class.
225     */
226    int*            methodIndexArray;
227} InterfaceEntry;
228
229
230
231/*
232 * There are three types of objects:
233 *  Class objects - an instance of java.lang.Class
234 *  Array objects - an object created with a "new array" instruction
235 *  Data objects - an object that is neither of the above
236 *
237 * We also define String objects.  At present they're equivalent to
238 * DataObject, but that may change.  (Either way, they make some of the
239 * code more obvious.)
240 *
241 * All objects have an Object header followed by type-specific data.
242 */
243typedef struct Object {
244    /* ptr to class object */
245    ClassObject*    clazz;
246
247    /*
248     * A word containing either a "thin" lock or a "fat" monitor.  See
249     * the comments in Sync.c for a description of its layout.
250     */
251    u4              lock;
252} Object;
253
254/*
255 * Properly initialize an Object.
256 * void DVM_OBJECT_INIT(Object *obj, ClassObject *clazz_)
257 */
258#define DVM_OBJECT_INIT(obj, clazz_) \
259    do { (obj)->clazz = (clazz_); DVM_LOCK_INIT(&(obj)->lock); } while (0)
260
261/*
262 * Data objects have an Object header followed by their instance data.
263 */
264struct DataObject {
265    Object          obj;                /* MUST be first item */
266
267    /* variable #of u4 slots; u8 uses 2 slots */
268    u4              instanceData[1];
269};
270
271/*
272 * Strings are used frequently enough that we may want to give them their
273 * own unique type.
274 *
275 * Using a dedicated type object to access the instance data provides a
276 * performance advantage but makes the java/lang/String.java implementation
277 * fragile.
278 *
279 * Currently this is just equal to DataObject, and we pull the fields out
280 * like we do for any other object.
281 */
282struct StringObject {
283    Object          obj;                /* MUST be first item */
284
285    /* variable #of u4 slots; u8 uses 2 slots */
286    u4              instanceData[1];
287};
288
289
290/*
291 * Array objects have these additional fields.
292 *
293 * We don't currently store the size of each element.  Usually it's implied
294 * by the instruction.  If necessary, the width can be derived from
295 * the first char of obj->clazz->descriptor.
296 */
297struct ArrayObject {
298    Object          obj;                /* MUST be first item */
299
300    /* number of elements; immutable after init */
301    u4              length;
302
303    /*
304     * Array contents; actual size is (length * sizeof(type)).  This is
305     * declared as u8 so that the compiler inserts any necessary padding
306     * (e.g. for EABI); the actual allocation may be smaller than 8 bytes.
307     */
308    u8              contents[1];
309};
310
311/*
312 * For classes created early and thus probably in the zygote, the
313 * InitiatingLoaderList is kept in gDvm. Later classes use the structure in
314 * Object Class. This helps keep zygote pages shared.
315 */
316struct InitiatingLoaderList {
317    /* a list of initiating loader Objects; grown and initialized on demand */
318    Object**  initiatingLoaders;
319    /* count of loaders in the above list */
320    int       initiatingLoaderCount;
321};
322
323/*
324 * Generic field header.  We pass this around when we want a generic Field
325 * pointer (e.g. for reflection stuff).  Testing the accessFlags for
326 * ACC_STATIC allows a proper up-cast.
327 */
328struct Field {
329    ClassObject*    clazz;          /* class in which the field is declared */
330    const char*     name;
331    const char*     signature;      /* e.g. "I", "[C", "Landroid/os/Debug;" */
332    u4              accessFlags;
333#ifdef PROFILE_FIELD_ACCESS
334    u4              gets;
335    u4              puts;
336#endif
337};
338
339/*
340 * Static field.
341 */
342struct StaticField {
343    Field           field;          /* MUST be first item */
344    JValue          value;          /* initially set from DEX for primitives */
345};
346
347/*
348 * Instance field.
349 */
350struct InstField {
351    Field           field;          /* MUST be first item */
352
353    /*
354     * This field indicates the byte offset from the beginning of the
355     * (Object *) to the actual instance data; e.g., byteOffset==0 is
356     * the same as the object pointer (bug!), and byteOffset==4 is 4
357     * bytes farther.
358     */
359    int             byteOffset;
360};
361
362/*
363 * This defines the amount of space we leave for field slots in the
364 * java.lang.Class definition.  If we alter the class to have more than
365 * this many fields, the VM will abort at startup.
366 */
367#define CLASS_FIELD_SLOTS   4
368
369/*
370 * Class objects have many additional fields.  This is used for both
371 * classes and interfaces, including synthesized classes (arrays and
372 * primitive types).
373 *
374 * Class objects are unusual in that they have some fields allocated with
375 * the system malloc (or LinearAlloc), rather than on the GC heap.  This is
376 * handy during initialization, but does require special handling when
377 * discarding java.lang.Class objects.
378 *
379 * The separation of methods (direct vs. virtual) and fields (class vs.
380 * instance) used in Dalvik works out pretty well.  The only time it's
381 * annoying is when enumerating or searching for things with reflection.
382 */
383struct ClassObject {
384    Object          obj;                /* MUST be first item */
385
386    /* leave space for instance data; we could access fields directly if we
387       freeze the definition of java/lang/Class */
388    u4              instanceData[CLASS_FIELD_SLOTS];
389
390    /* UTF-8 descriptor for the class; from constant pool, or on heap
391       if generated ("[C") */
392    const char*     descriptor;
393    char*           descriptorAlloc;
394
395    /* access flags; low 16 bits are defined by VM spec */
396    u4              accessFlags;
397
398    /* VM-unique class serial number, nonzero, set very early */
399    u4              serialNumber;
400
401    /* DexFile from which we came; needed to resolve constant pool entries */
402    /* (will be NULL for VM-generated, e.g. arrays and primitive classes) */
403    DvmDex*         pDvmDex;
404
405    /* state of class initialization */
406    ClassStatus     status;
407
408    /* if class verify fails, we must return same error on subsequent tries */
409    ClassObject*    verifyErrorClass;
410
411    /* threadId, used to check for recursive <clinit> invocation */
412    u4              initThreadId;
413
414    /*
415     * Total object size; used when allocating storage on gc heap.  (For
416     * interfaces and abstract classes this will be zero.)
417     */
418    size_t          objectSize;
419
420    /* arrays only: class object for base element, for instanceof/checkcast
421       (for String[][][], this will be String) */
422    ClassObject*    elementClass;
423
424    /* arrays only: number of dimensions, e.g. int[][] is 2 */
425    int             arrayDim;
426
427    /* primitive type index, or PRIM_NOT (-1); set for generated prim classes */
428    PrimitiveType   primitiveType;
429
430    /* superclass, or NULL if this is java.lang.Object */
431    ClassObject*    super;
432
433    /* defining class loader, or NULL for the "bootstrap" system loader */
434    Object*         classLoader;
435
436    /* initiating class loader list */
437    /* NOTE: for classes with low serialNumber, these are unused, and the
438       values are kept in a table in gDvm. */
439    InitiatingLoaderList initiatingLoaderList;
440
441    /* array of interfaces this class implements directly */
442    int             interfaceCount;
443    ClassObject**   interfaces;
444
445    /* static, private, and <init> methods */
446    int             directMethodCount;
447    Method*         directMethods;
448
449    /* virtual methods defined in this class; invoked through vtable */
450    int             virtualMethodCount;
451    Method*         virtualMethods;
452
453    /*
454     * Virtual method table (vtable), for use by "invoke-virtual".  The
455     * vtable from the superclass is copied in, and virtual methods from
456     * our class either replace those from the super or are appended.
457     */
458    int             vtableCount;
459    Method**        vtable;
460
461    /*
462     * Interface table (iftable), one entry per interface supported by
463     * this class.  That means one entry for each interface we support
464     * directly, indirectly via superclass, or indirectly via
465     * superinterface.  This will be null if neither we nor our superclass
466     * implement any interfaces.
467     *
468     * Why we need this: given "class Foo implements Face", declare
469     * "Face faceObj = new Foo()".  Invoke faceObj.blah(), where "blah" is
470     * part of the Face interface.  We can't easily use a single vtable.
471     *
472     * For every interface a concrete class implements, we create a list of
473     * virtualMethod indices for the methods in the interface.
474     */
475    int             iftableCount;
476    InterfaceEntry* iftable;
477
478    /*
479     * The interface vtable indices for iftable get stored here.  By placing
480     * them all in a single pool for each class that implements interfaces,
481     * we decrease the number of allocations.
482     */
483    int             ifviPoolCount;
484    int*            ifviPool;
485
486    /* instance fields
487     *
488     * These describe the layout of the contents of a DataObject-compatible
489     * Object.  Note that only the fields directly defined by this class
490     * are listed in ifields;  fields defined by a superclass are listed
491     * in the superclass's ClassObject.ifields.
492     *
493     * All instance fields that refer to objects are guaranteed to be
494     * at the beginning of the field list.  ifieldRefCount specifies
495     * the number of reference fields.
496     */
497    int             ifieldCount;
498    int             ifieldRefCount; // number of fields that are object refs
499    InstField*      ifields;
500
501    /* bitmap of offsets of ifields */
502    u4 refOffsets;
503
504    /* source file name, if known */
505    const char*     sourceFile;
506
507    /* static fields */
508    int             sfieldCount;
509    StaticField     sfields[]; /* MUST be last item */
510};
511
512/*
513 * A method.  We create one of these for every method in every class
514 * we load, so try to keep the size to a minimum.
515 *
516 * Much of this comes from and could be accessed in the data held in shared
517 * memory.  We hold it all together here for speed.  Everything but the
518 * pointers could be held in a shared table generated by the optimizer;
519 * if we're willing to convert them to offsets and take the performance
520 * hit (e.g. "meth->insns" becomes "baseAddr + meth->insnsOffset") we
521 * could move everything but "nativeFunc".
522 */
523struct Method {
524    /* the class we are a part of */
525    ClassObject*    clazz;
526
527    /* access flags; low 16 bits are defined by spec (could be u2?) */
528    u4              accessFlags;
529
530    /*
531     * For concrete virtual methods, this is the offset of the method
532     * in "vtable".
533     *
534     * For abstract methods in an interface class, this is the offset
535     * of the method in "iftable[n]->methodIndexArray".
536     */
537    u2             methodIndex;
538
539    /*
540     * Method bounds; not needed for an abstract method.
541     *
542     * For a native method, we compute the size of the argument list, and
543     * set "insSize" and "registerSize" equal to it.
544     */
545    u2              registersSize;  /* ins + locals */
546    u2              outsSize;
547    u2              insSize;
548
549    /* method name, e.g. "<init>" or "eatLunch" */
550    const char*     name;
551
552    /*
553     * Method prototype descriptor string (return and argument types).
554     *
555     * TODO: This currently must specify the DexFile as well as the proto_ids
556     * index, because generated Proxy classes don't have a DexFile.  We can
557     * remove the DexFile* and reduce the size of this struct if we generate
558     * a DEX for proxies.
559     */
560    DexProto        prototype;
561
562    /* short-form method descriptor string */
563    const char*     shorty;
564
565    /*
566     * The remaining items are not used for abstract or native methods.
567     * (JNI is currently hijacking "insns" as a function pointer, set
568     * after the first call.  For internal-native this stays null.)
569     */
570
571    /* the actual code */
572    const u2*       insns;          /* instructions, in memory-mapped .dex */
573
574    /* cached JNI argument and return-type hints */
575    int             jniArgInfo;
576
577    /*
578     * Native method ptr; could be actual function or a JNI bridge.  We
579     * don't currently discriminate between DalvikBridgeFunc and
580     * DalvikNativeFunc; the former takes an argument superset (i.e. two
581     * extra args) which will be ignored.  If necessary we can use
582     * insns==NULL to detect JNI bridge vs. internal native.
583     */
584    DalvikBridgeFunc nativeFunc;
585
586    /*
587     * Register map data, if available.  This will point into the DEX file
588     * if the data was computed during pre-verification, or into the
589     * linear alloc area if not.
590     */
591    const RegisterMap* registerMap;
592
593#ifdef WITH_PROFILER
594    bool            inProfile;
595#endif
596};
597
598
599/*
600 * Find a method within a class.  The superclass is not searched.
601 */
602Method* dvmFindDirectMethodByDescriptor(const ClassObject* clazz,
603    const char* methodName, const char* signature);
604Method* dvmFindVirtualMethodByDescriptor(const ClassObject* clazz,
605    const char* methodName, const char* signature);
606Method* dvmFindVirtualMethodByName(const ClassObject* clazz,
607    const char* methodName);
608Method* dvmFindDirectMethod(const ClassObject* clazz, const char* methodName,
609    const DexProto* proto);
610Method* dvmFindVirtualMethod(const ClassObject* clazz, const char* methodName,
611    const DexProto* proto);
612
613
614/*
615 * Find a method within a class hierarchy.
616 */
617Method* dvmFindDirectMethodHierByDescriptor(const ClassObject* clazz,
618    const char* methodName, const char* descriptor);
619Method* dvmFindVirtualMethodHierByDescriptor(const ClassObject* clazz,
620    const char* methodName, const char* signature);
621Method* dvmFindDirectMethodHier(const ClassObject* clazz,
622    const char* methodName, const DexProto* proto);
623Method* dvmFindVirtualMethodHier(const ClassObject* clazz,
624    const char* methodName, const DexProto* proto);
625Method* dvmFindMethodHier(const ClassObject* clazz, const char* methodName,
626    const DexProto* proto);
627
628/*
629 * Find the implementation of "meth" in "clazz".
630 *
631 * Returns NULL and throws an exception if not found.
632 */
633const Method* dvmGetVirtualizedMethod(const ClassObject* clazz,
634    const Method* meth);
635
636/*
637 * Get the source file associated with a method.
638 */
639const char* dvmGetMethodSourceFile(const Method* meth);
640
641/*
642 * Find a field within a class.  The superclass is not searched.
643 */
644InstField* dvmFindInstanceField(const ClassObject* clazz,
645    const char* fieldName, const char* signature);
646StaticField* dvmFindStaticField(const ClassObject* clazz,
647    const char* fieldName, const char* signature);
648
649/*
650 * Find a field in a class/interface hierarchy.
651 */
652InstField* dvmFindInstanceFieldHier(const ClassObject* clazz,
653    const char* fieldName, const char* signature);
654StaticField* dvmFindStaticFieldHier(const ClassObject* clazz,
655    const char* fieldName, const char* signature);
656Field* dvmFindFieldHier(const ClassObject* clazz, const char* fieldName,
657    const char* signature);
658
659/*
660 * Find a field and return the byte offset from the object pointer.  Only
661 * searches the specified class, not the superclass.
662 *
663 * Returns -1 on failure.
664 */
665INLINE int dvmFindFieldOffset(const ClassObject* clazz,
666    const char* fieldName, const char* signature)
667{
668    InstField* pField = dvmFindInstanceField(clazz, fieldName, signature);
669    if (pField == NULL)
670        return -1;
671    else
672        return pField->byteOffset;
673}
674
675/*
676 * Field access functions.  Pass in the word offset from Field->byteOffset.
677 *
678 * We guarantee that long/double field data is 64-bit aligned, so it's safe
679 * to access them with ldrd/strd on ARM.
680 *
681 * The VM treats all fields as 32 or 64 bits, so the field set functions
682 * write 32 bits even if the underlying type is smaller.
683 */
684#define BYTE_OFFSET(_ptr, _offset)  ((void*) (((u1*)(_ptr)) + (_offset)))
685
686INLINE JValue* dvmFieldPtr(const Object* obj, int offset) {
687    return ((JValue*)BYTE_OFFSET(obj, offset));
688}
689
690INLINE bool dvmGetFieldBoolean(const Object* obj, int offset) {
691    return ((JValue*)BYTE_OFFSET(obj, offset))->z;
692}
693INLINE s1 dvmGetFieldByte(const Object* obj, int offset) {
694    return ((JValue*)BYTE_OFFSET(obj, offset))->b;
695}
696INLINE s2 dvmGetFieldShort(const Object* obj, int offset) {
697    return ((JValue*)BYTE_OFFSET(obj, offset))->s;
698}
699INLINE u2 dvmGetFieldChar(const Object* obj, int offset) {
700    return ((JValue*)BYTE_OFFSET(obj, offset))->c;
701}
702INLINE s4 dvmGetFieldInt(const Object* obj, int offset) {
703    return ((JValue*)BYTE_OFFSET(obj, offset))->i;
704}
705INLINE s8 dvmGetFieldLong(const Object* obj, int offset) {
706    return ((JValue*)BYTE_OFFSET(obj, offset))->j;
707}
708INLINE float dvmGetFieldFloat(const Object* obj, int offset) {
709    return ((JValue*)BYTE_OFFSET(obj, offset))->f;
710}
711INLINE double dvmGetFieldDouble(const Object* obj, int offset) {
712    return ((JValue*)BYTE_OFFSET(obj, offset))->d;
713}
714INLINE Object* dvmGetFieldObject(const Object* obj, int offset) {
715    return ((JValue*)BYTE_OFFSET(obj, offset))->l;
716}
717INLINE s4 dvmGetFieldIntVolatile(const Object* obj, int offset) {
718    s4* ptr = &((JValue*)BYTE_OFFSET(obj, offset))->i;
719    return android_atomic_acquire_load(ptr);
720}
721INLINE Object* dvmGetFieldObjectVolatile(const Object* obj, int offset) {
722    void** ptr = &((JValue*)BYTE_OFFSET(obj, offset))->l;
723    return (Object*) android_atomic_acquire_load((int32_t*)ptr);
724}
725INLINE s8 dvmGetFieldLongVolatile(const Object* obj, int offset) {
726    const s8* addr = BYTE_OFFSET(obj, offset);
727    s8 val = dvmQuasiAtomicRead64(addr);
728    ANDROID_MEMBAR_FULL();
729    return val;
730}
731
732INLINE void dvmSetFieldBoolean(Object* obj, int offset, bool val) {
733    ((JValue*)BYTE_OFFSET(obj, offset))->i = val;
734}
735INLINE void dvmSetFieldByte(Object* obj, int offset, s1 val) {
736    ((JValue*)BYTE_OFFSET(obj, offset))->i = val;
737}
738INLINE void dvmSetFieldShort(Object* obj, int offset, s2 val) {
739    ((JValue*)BYTE_OFFSET(obj, offset))->i = val;
740}
741INLINE void dvmSetFieldChar(Object* obj, int offset, u2 val) {
742    ((JValue*)BYTE_OFFSET(obj, offset))->i = val;
743}
744INLINE void dvmSetFieldInt(Object* obj, int offset, s4 val) {
745    ((JValue*)BYTE_OFFSET(obj, offset))->i = val;
746}
747INLINE void dvmSetFieldLong(Object* obj, int offset, s8 val) {
748    ((JValue*)BYTE_OFFSET(obj, offset))->j = val;
749}
750INLINE void dvmSetFieldFloat(Object* obj, int offset, float val) {
751    ((JValue*)BYTE_OFFSET(obj, offset))->f = val;
752}
753INLINE void dvmSetFieldDouble(Object* obj, int offset, double val) {
754    ((JValue*)BYTE_OFFSET(obj, offset))->d = val;
755}
756INLINE void dvmSetFieldObject(Object* obj, int offset, Object* val) {
757    ((JValue*)BYTE_OFFSET(obj, offset))->l = val;
758}
759INLINE void dvmSetFieldIntVolatile(Object* obj, int offset, s4 val) {
760    s4* ptr = &((JValue*)BYTE_OFFSET(obj, offset))->i;
761    android_atomic_release_store(val, ptr);
762}
763INLINE void dvmSetFieldObjectVolatile(Object* obj, int offset, Object* val) {
764    void** ptr = &((JValue*)BYTE_OFFSET(obj, offset))->l;
765    android_atomic_release_store((int32_t)val, (int32_t*)ptr);
766}
767INLINE void dvmSetFieldLongVolatile(Object* obj, int offset, s8 val) {
768    s8* addr = BYTE_OFFSET(obj, offset);
769    ANDROID_MEMBAR_FULL();
770    dvmQuasiAtomicSwap64(val, addr);
771}
772
773/*
774 * Static field access functions.
775 */
776INLINE JValue* dvmStaticFieldPtr(const StaticField* sfield) {
777    return (JValue*)&sfield->value;
778}
779
780INLINE bool dvmGetStaticFieldBoolean(const StaticField* sfield) {
781    return sfield->value.z;
782}
783INLINE s1 dvmGetStaticFieldByte(const StaticField* sfield) {
784    return sfield->value.b;
785}
786INLINE s2 dvmGetStaticFieldShort(const StaticField* sfield) {
787    return sfield->value.s;
788}
789INLINE u2 dvmGetStaticFieldChar(const StaticField* sfield) {
790    return sfield->value.c;
791}
792INLINE s4 dvmGetStaticFieldInt(const StaticField* sfield) {
793    return sfield->value.i;
794}
795INLINE s8 dvmGetStaticFieldLong(const StaticField* sfield) {
796    return sfield->value.j;
797}
798INLINE float dvmGetStaticFieldFloat(const StaticField* sfield) {
799    return sfield->value.f;
800}
801INLINE double dvmGetStaticFieldDouble(const StaticField* sfield) {
802    return sfield->value.d;
803}
804INLINE Object* dvmGetStaticFieldObject(const StaticField* sfield) {
805    return sfield->value.l;
806}
807INLINE s4 dvmGetStaticFieldIntVolatile(const StaticField* sfield) {
808    const s4* ptr = &(sfield->value.i);
809    return android_atomic_acquire_load((s4*)ptr);
810}
811INLINE Object* dvmGetStaticFieldObjectVolatile(const StaticField* sfield) {
812    void* const* ptr = &(sfield->value.l);
813    return (Object*) android_atomic_acquire_load((int32_t*)ptr);
814}
815INLINE s8 dvmGetStaticFieldLongVolatile(const StaticField* sfield) {
816    const s8* addr = &sfield->value.j;
817    s8 val = dvmQuasiAtomicRead64(addr);
818    ANDROID_MEMBAR_FULL();
819    return val;
820}
821
822INLINE void dvmSetStaticFieldBoolean(StaticField* sfield, bool val) {
823    sfield->value.i = val;
824}
825INLINE void dvmSetStaticFieldByte(StaticField* sfield, s1 val) {
826    sfield->value.i = val;
827}
828INLINE void dvmSetStaticFieldShort(StaticField* sfield, s2 val) {
829    sfield->value.i = val;
830}
831INLINE void dvmSetStaticFieldChar(StaticField* sfield, u2 val) {
832    sfield->value.i = val;
833}
834INLINE void dvmSetStaticFieldInt(StaticField* sfield, s4 val) {
835    sfield->value.i = val;
836}
837INLINE void dvmSetStaticFieldLong(StaticField* sfield, s8 val) {
838    sfield->value.j = val;
839}
840INLINE void dvmSetStaticFieldFloat(StaticField* sfield, float val) {
841    sfield->value.f = val;
842}
843INLINE void dvmSetStaticFieldDouble(StaticField* sfield, double val) {
844    sfield->value.d = val;
845}
846INLINE void dvmSetStaticFieldObject(StaticField* sfield, Object* val) {
847    sfield->value.l = val;
848}
849INLINE void dvmSetStaticFieldIntVolatile(StaticField* sfield, s4 val) {
850    s4* ptr = &(sfield->value.i);
851    android_atomic_release_store(val, ptr);
852}
853INLINE void dvmSetStaticFieldObjectVolatile(StaticField* sfield, Object* val) {
854    void** ptr = &(sfield->value.l);
855    android_atomic_release_store((int32_t)val, (int32_t*)ptr);
856}
857INLINE void dvmSetStaticFieldLongVolatile(StaticField* sfield, s8 val) {
858    s8* addr = &sfield->value.j;
859    ANDROID_MEMBAR_FULL();
860    dvmQuasiAtomicSwap64(val, addr);
861}
862
863/*
864 * Helpers.
865 */
866INLINE bool dvmIsPublicMethod(const Method* method) {
867    return (method->accessFlags & ACC_PUBLIC) != 0;
868}
869INLINE bool dvmIsPrivateMethod(const Method* method) {
870    return (method->accessFlags & ACC_PRIVATE) != 0;
871}
872INLINE bool dvmIsStaticMethod(const Method* method) {
873    return (method->accessFlags & ACC_STATIC) != 0;
874}
875INLINE bool dvmIsSynchronizedMethod(const Method* method) {
876    return (method->accessFlags & ACC_SYNCHRONIZED) != 0;
877}
878INLINE bool dvmIsDeclaredSynchronizedMethod(const Method* method) {
879    return (method->accessFlags & ACC_DECLARED_SYNCHRONIZED) != 0;
880}
881INLINE bool dvmIsFinalMethod(const Method* method) {
882    return (method->accessFlags & ACC_FINAL) != 0;
883}
884INLINE bool dvmIsNativeMethod(const Method* method) {
885    return (method->accessFlags & ACC_NATIVE) != 0;
886}
887INLINE bool dvmIsAbstractMethod(const Method* method) {
888    return (method->accessFlags & ACC_ABSTRACT) != 0;
889}
890INLINE bool dvmIsMirandaMethod(const Method* method) {
891    return (method->accessFlags & ACC_MIRANDA) != 0;
892}
893INLINE bool dvmIsConstructorMethod(const Method* method) {
894    return *method->name == '<';
895}
896/* Dalvik puts private, static, and constructors into non-virtual table */
897INLINE bool dvmIsDirectMethod(const Method* method) {
898    return dvmIsPrivateMethod(method) ||
899           dvmIsStaticMethod(method) ||
900           dvmIsConstructorMethod(method);
901}
902/* Get whether the given method has associated bytecode. This is the
903 * case for methods which are neither native nor abstract. */
904INLINE bool dvmIsBytecodeMethod(const Method* method) {
905    return (method->accessFlags & (ACC_NATIVE | ACC_ABSTRACT)) == 0;
906}
907
908INLINE bool dvmIsProtectedField(const Field* field) {
909    return (field->accessFlags & ACC_PROTECTED) != 0;
910}
911INLINE bool dvmIsStaticField(const Field* field) {
912    return (field->accessFlags & ACC_STATIC) != 0;
913}
914INLINE bool dvmIsFinalField(const Field* field) {
915    return (field->accessFlags & ACC_FINAL) != 0;
916}
917INLINE bool dvmIsVolatileField(const Field* field) {
918    return (field->accessFlags & ACC_VOLATILE) != 0;
919}
920
921INLINE bool dvmIsInterfaceClass(const ClassObject* clazz) {
922    return (clazz->accessFlags & ACC_INTERFACE) != 0;
923}
924INLINE bool dvmIsPublicClass(const ClassObject* clazz) {
925    return (clazz->accessFlags & ACC_PUBLIC) != 0;
926}
927INLINE bool dvmIsFinalClass(const ClassObject* clazz) {
928    return (clazz->accessFlags & ACC_FINAL) != 0;
929}
930INLINE bool dvmIsAbstractClass(const ClassObject* clazz) {
931    return (clazz->accessFlags & ACC_ABSTRACT) != 0;
932}
933INLINE bool dvmIsAnnotationClass(const ClassObject* clazz) {
934    return (clazz->accessFlags & ACC_ANNOTATION) != 0;
935}
936INLINE bool dvmIsPrimitiveClass(const ClassObject* clazz) {
937    return clazz->primitiveType != PRIM_NOT;
938}
939
940/* linked, here meaning prepared and resolved */
941INLINE bool dvmIsClassLinked(const ClassObject* clazz) {
942    return clazz->status >= CLASS_RESOLVED;
943}
944/* has class been verified? */
945INLINE bool dvmIsClassVerified(const ClassObject* clazz) {
946    return clazz->status >= CLASS_VERIFIED;
947}
948
949/*
950 * Get the associated code struct for a method. This returns NULL
951 * for non-bytecode methods.
952 */
953INLINE const DexCode* dvmGetMethodCode(const Method* meth) {
954    if (dvmIsBytecodeMethod(meth)) {
955        /*
956         * The insns field for a bytecode method actually points at
957         * &(DexCode.insns), so we can subtract back to get at the
958         * DexCode in front.
959         */
960        return (const DexCode*)
961            (((const u1*) meth->insns) - offsetof(DexCode, insns));
962    } else {
963        return NULL;
964    }
965}
966
967/*
968 * Get the size of the insns associated with a method. This returns 0
969 * for non-bytecode methods.
970 */
971INLINE u4 dvmGetMethodInsnsSize(const Method* meth) {
972    const DexCode* pCode = dvmGetMethodCode(meth);
973    return (pCode == NULL) ? 0 : pCode->insnsSize;
974}
975
976/* debugging */
977void dvmDumpObject(const Object* obj);
978
979#endif /*_DALVIK_OO_OBJECT*/
980