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