blob: 6f985eb0d556ecc52b74d72233e7db2d9c5bce15 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_GLOBALS_H_
29#define V8_GLOBALS_H_
30
31namespace v8 {
32namespace internal {
33
34// Processor architecture detection. For more info on what's defined, see:
35// http://msdn.microsoft.com/en-us/library/b0084kay.aspx
36// http://www.agner.org/optimize/calling_conventions.pdf
37// or with gcc, run: "echo | gcc -E -dM -"
38#if defined(_M_X64) || defined(__x86_64__)
39#define V8_HOST_ARCH_X64 1
40#define V8_HOST_ARCH_64_BIT 1
41#define V8_HOST_CAN_READ_UNALIGNED 1
42#elif defined(_M_IX86) || defined(__i386__)
43#define V8_HOST_ARCH_IA32 1
44#define V8_HOST_ARCH_32_BIT 1
45#define V8_HOST_CAN_READ_UNALIGNED 1
46#elif defined(__ARMEL__)
47#define V8_HOST_ARCH_ARM 1
48#define V8_HOST_ARCH_32_BIT 1
Kristian Monsen25f61362010-05-21 11:50:48 +010049// Some CPU-OS combinations allow unaligned access on ARM. We assume
50// that unaligned accesses are not allowed unless the build system
51// defines the CAN_USE_UNALIGNED_ACCESSES macro to be non-zero.
52#if CAN_USE_UNALIGNED_ACCESSES
53#define V8_HOST_CAN_READ_UNALIGNED 1
54#endif
Andrei Popescu31002712010-02-23 13:46:05 +000055#elif defined(_MIPS_ARCH_MIPS32R2)
56#define V8_HOST_ARCH_MIPS 1
57#define V8_HOST_ARCH_32_BIT 1
Steve Blocka7e24c12009-10-30 11:49:00 +000058#else
Steve Block6ded16b2010-05-10 14:33:55 +010059#error Host architecture was not detected as supported by v8
Steve Blocka7e24c12009-10-30 11:49:00 +000060#endif
61
Leon Clarkef7060e22010-06-03 12:02:55 +010062// Target architecture detection. This may be set externally. If not, detect
63// in the same way as the host architecture, that is, target the native
64// environment as presented by the compiler.
65#if !defined(V8_TARGET_ARCH_X64) && !defined(V8_TARGET_ARCH_IA32) && \
66 !defined(V8_TARGET_ARCH_ARM) && !defined(V8_TARGET_ARCH_MIPS)
67#if defined(_M_X64) || defined(__x86_64__)
68#define V8_TARGET_ARCH_X64 1
69#elif defined(_M_IX86) || defined(__i386__)
70#define V8_TARGET_ARCH_IA32 1
71#elif defined(__ARMEL__)
72#define V8_TARGET_ARCH_ARM 1
73#elif defined(_MIPS_ARCH_MIPS32R2)
74#define V8_TARGET_ARCH_MIPS 1
75#else
76#error Target architecture was not detected as supported by v8
77#endif
78#endif
79
Steve Block6ded16b2010-05-10 14:33:55 +010080// Check for supported combinations of host and target architectures.
81#if defined(V8_TARGET_ARCH_IA32) && !defined(V8_HOST_ARCH_IA32)
82#error Target architecture ia32 is only supported on ia32 host
83#endif
84#if defined(V8_TARGET_ARCH_X64) && !defined(V8_HOST_ARCH_X64)
85#error Target architecture x64 is only supported on x64 host
86#endif
87#if (defined(V8_TARGET_ARCH_ARM) && \
88 !(defined(V8_HOST_ARCH_IA32) || defined(V8_HOST_ARCH_ARM)))
89#error Target architecture arm is only supported on arm and ia32 host
90#endif
91#if (defined(V8_TARGET_ARCH_MIPS) && \
92 !(defined(V8_HOST_ARCH_IA32) || defined(V8_HOST_ARCH_MIPS)))
93#error Target architecture mips is only supported on mips and ia32 host
94#endif
95
96// Define unaligned read for the target architectures supporting it.
Steve Blocka7e24c12009-10-30 11:49:00 +000097#if defined(V8_TARGET_ARCH_X64) || defined(V8_TARGET_ARCH_IA32)
98#define V8_TARGET_CAN_READ_UNALIGNED 1
99#elif V8_TARGET_ARCH_ARM
Kristian Monsen25f61362010-05-21 11:50:48 +0100100// Some CPU-OS combinations allow unaligned access on ARM. We assume
101// that unaligned accesses are not allowed unless the build system
102// defines the CAN_USE_UNALIGNED_ACCESSES macro to be non-zero.
103#if CAN_USE_UNALIGNED_ACCESSES
104#define V8_TARGET_CAN_READ_UNALIGNED 1
105#endif
Andrei Popescu31002712010-02-23 13:46:05 +0000106#elif V8_TARGET_ARCH_MIPS
Steve Blocka7e24c12009-10-30 11:49:00 +0000107#else
Steve Block6ded16b2010-05-10 14:33:55 +0100108#error Target architecture is not supported by v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000109#endif
110
111// Support for alternative bool type. This is only enabled if the code is
112// compiled with USE_MYBOOL defined. This catches some nasty type bugs.
113// For instance, 'bool b = "false";' results in b == true! This is a hidden
114// source of bugs.
115// However, redefining the bool type does have some negative impact on some
116// platforms. It gives rise to compiler warnings (i.e. with
117// MSVC) in the API header files when mixing code that uses the standard
118// bool with code that uses the redefined version.
119// This does not actually belong in the platform code, but needs to be
120// defined here because the platform code uses bool, and platform.h is
121// include very early in the main include file.
122
123#ifdef USE_MYBOOL
124typedef unsigned int __my_bool__;
125#define bool __my_bool__ // use 'indirection' to avoid name clashes
126#endif
127
128typedef uint8_t byte;
129typedef byte* Address;
130
131// Define our own macros for writing 64-bit constants. This is less fragile
132// than defining __STDC_CONSTANT_MACROS before including <stdint.h>, and it
133// works on compilers that don't have it (like MSVC).
134#if V8_HOST_ARCH_64_BIT
135#ifdef _MSC_VER
136#define V8_UINT64_C(x) (x ## UI64)
137#define V8_INT64_C(x) (x ## I64)
138#define V8_PTR_PREFIX "ll"
139#else // _MSC_VER
140#define V8_UINT64_C(x) (x ## UL)
141#define V8_INT64_C(x) (x ## L)
142#define V8_PTR_PREFIX "l"
143#endif // _MSC_VER
144#else // V8_HOST_ARCH_64_BIT
145#define V8_PTR_PREFIX ""
146#endif // V8_HOST_ARCH_64_BIT
147
Steve Block6ded16b2010-05-10 14:33:55 +0100148// The following macro works on both 32 and 64-bit platforms.
149// Usage: instead of writing 0x1234567890123456
150// write V8_2PART_UINT64_C(0x12345678,90123456);
151#define V8_2PART_UINT64_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
152
Steve Blocka7e24c12009-10-30 11:49:00 +0000153#define V8PRIxPTR V8_PTR_PREFIX "x"
154#define V8PRIdPTR V8_PTR_PREFIX "d"
155
156// Fix for Mac OS X defining uintptr_t as "unsigned long":
157#if defined(__APPLE__) && defined(__MACH__)
158#undef V8PRIxPTR
159#define V8PRIxPTR "lx"
160#endif
161
Steve Block6ded16b2010-05-10 14:33:55 +0100162#if (defined(__APPLE__) && defined(__MACH__)) || \
163 defined(__FreeBSD__) || defined(__OpenBSD__)
164#define USING_BSD_ABI
Steve Blockd0582a62009-12-15 09:54:21 +0000165#endif
166
Steve Blocka7e24c12009-10-30 11:49:00 +0000167// Code-point values in Unicode 4.0 are 21 bits wide.
168typedef uint16_t uc16;
169typedef int32_t uc32;
170
171// -----------------------------------------------------------------------------
172// Constants
173
174const int KB = 1024;
175const int MB = KB * KB;
176const int GB = KB * KB * KB;
177const int kMaxInt = 0x7FFFFFFF;
178const int kMinInt = -kMaxInt - 1;
179
180const uint32_t kMaxUInt32 = 0xFFFFFFFFu;
181
182const int kCharSize = sizeof(char); // NOLINT
183const int kShortSize = sizeof(short); // NOLINT
184const int kIntSize = sizeof(int); // NOLINT
185const int kDoubleSize = sizeof(double); // NOLINT
186const int kPointerSize = sizeof(void*); // NOLINT
187const int kIntptrSize = sizeof(intptr_t); // NOLINT
188
189#if V8_HOST_ARCH_64_BIT
190const int kPointerSizeLog2 = 3;
191const intptr_t kIntptrSignBit = V8_INT64_C(0x8000000000000000);
192#else
193const int kPointerSizeLog2 = 2;
194const intptr_t kIntptrSignBit = 0x80000000;
195#endif
196
Steve Block6ded16b2010-05-10 14:33:55 +0100197// Mask for the sign bit in a smi.
198const intptr_t kSmiSignMask = kIntptrSignBit;
199
Steve Blocka7e24c12009-10-30 11:49:00 +0000200const int kObjectAlignmentBits = kPointerSizeLog2;
201const intptr_t kObjectAlignment = 1 << kObjectAlignmentBits;
202const intptr_t kObjectAlignmentMask = kObjectAlignment - 1;
203
204// Desired alignment for pointers.
205const intptr_t kPointerAlignment = (1 << kPointerSizeLog2);
206const intptr_t kPointerAlignmentMask = kPointerAlignment - 1;
207
Leon Clarkee46be812010-01-19 14:06:41 +0000208// Desired alignment for maps.
209#if V8_HOST_ARCH_64_BIT
210const intptr_t kMapAlignmentBits = kObjectAlignmentBits;
211#else
212const intptr_t kMapAlignmentBits = kObjectAlignmentBits + 3;
213#endif
214const intptr_t kMapAlignment = (1 << kMapAlignmentBits);
215const intptr_t kMapAlignmentMask = kMapAlignment - 1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000216
217// Tag information for Failure.
218const int kFailureTag = 3;
219const int kFailureTagSize = 2;
220const intptr_t kFailureTagMask = (1 << kFailureTagSize) - 1;
221
222
223const int kBitsPerByte = 8;
224const int kBitsPerByteLog2 = 3;
225const int kBitsPerPointer = kPointerSize * kBitsPerByte;
226const int kBitsPerInt = kIntSize * kBitsPerByte;
227
Steve Block6ded16b2010-05-10 14:33:55 +0100228// IEEE 754 single precision floating point number bit layout.
229const uint32_t kBinary32SignMask = 0x80000000u;
230const uint32_t kBinary32ExponentMask = 0x7f800000u;
231const uint32_t kBinary32MantissaMask = 0x007fffffu;
232const int kBinary32ExponentBias = 127;
233const int kBinary32MaxExponent = 0xFE;
234const int kBinary32MinExponent = 0x01;
235const int kBinary32MantissaBits = 23;
236const int kBinary32ExponentShift = 23;
Steve Blocka7e24c12009-10-30 11:49:00 +0000237
238// Zap-value: The value used for zapping dead objects.
239// Should be a recognizable hex value tagged as a heap object pointer.
240#ifdef V8_HOST_ARCH_64_BIT
241const Address kZapValue =
242 reinterpret_cast<Address>(V8_UINT64_C(0xdeadbeedbeadbeed));
243const Address kHandleZapValue =
244 reinterpret_cast<Address>(V8_UINT64_C(0x1baddead0baddead));
245const Address kFromSpaceZapValue =
246 reinterpret_cast<Address>(V8_UINT64_C(0x1beefdad0beefdad));
247#else
248const Address kZapValue = reinterpret_cast<Address>(0xdeadbeed);
249const Address kHandleZapValue = reinterpret_cast<Address>(0xbaddead);
250const Address kFromSpaceZapValue = reinterpret_cast<Address>(0xbeefdad);
251#endif
252
253
Leon Clarkee46be812010-01-19 14:06:41 +0000254// Number of bits to represent the page size for paged spaces. The value of 13
255// gives 8K bytes per page.
256const int kPageSizeBits = 13;
257
Steve Block6ded16b2010-05-10 14:33:55 +0100258// On Intel architecture, cache line size is 64 bytes.
259// On ARM it may be less (32 bytes), but as far this constant is
260// used for aligning data, it doesn't hurt to align on a greater value.
261const int kProcessorCacheLineSize = 64;
Leon Clarkee46be812010-01-19 14:06:41 +0000262
Steve Blockd0582a62009-12-15 09:54:21 +0000263// Constants relevant to double precision floating point numbers.
264
265// Quiet NaNs have bits 51 to 62 set, possibly the sign bit, and no
266// other bits set.
267const uint64_t kQuietNaNMask = static_cast<uint64_t>(0xfff) << 51;
268// If looking only at the top 32 bits, the QNaN mask is bits 19 to 30.
269const uint32_t kQuietNaNHighBitsMask = 0xfff << (51 - 32);
270
271
Steve Blocka7e24c12009-10-30 11:49:00 +0000272// -----------------------------------------------------------------------------
273// Forward declarations for frequently used classes
274// (sorted alphabetically)
275
276class AccessorInfo;
277class Allocation;
278class Arguments;
279class Assembler;
Leon Clarke4515c472010-02-03 11:58:03 +0000280class AssertNoAllocation;
Steve Blocka7e24c12009-10-30 11:49:00 +0000281class BreakableStatement;
282class Code;
283class CodeGenerator;
284class CodeStub;
285class Context;
286class Debug;
287class Debugger;
288class DebugInfo;
289class Descriptor;
290class DescriptorArray;
291class Expression;
292class ExternalReference;
293class FixedArray;
294class FunctionEntry;
295class FunctionLiteral;
296class FunctionTemplateInfo;
297class NumberDictionary;
298class StringDictionary;
299class FreeStoreAllocationPolicy;
300template <typename T> class Handle;
301class Heap;
302class HeapObject;
303class IC;
304class InterceptorInfo;
305class IterationStatement;
Steve Blocka7e24c12009-10-30 11:49:00 +0000306class JSArray;
307class JSFunction;
308class JSObject;
309class LargeObjectSpace;
310template <typename T, class P = FreeStoreAllocationPolicy> class List;
311class LookupResult;
312class MacroAssembler;
313class Map;
314class MapSpace;
315class MarkCompactCollector;
316class NewSpace;
317class NodeVisitor;
318class Object;
319class OldSpace;
320class Property;
321class Proxy;
322class RegExpNode;
323struct RegExpCompileData;
324class RegExpTree;
325class RegExpCompiler;
326class RegExpVisitor;
327class Scope;
328template<class Allocator = FreeStoreAllocationPolicy> class ScopeInfo;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100329class SerializedScopeInfo;
Steve Blocka7e24c12009-10-30 11:49:00 +0000330class Script;
331class Slot;
332class Smi;
Steve Block6ded16b2010-05-10 14:33:55 +0100333template <typename Config, class Allocator = FreeStoreAllocationPolicy>
334 class SplayTree;
Steve Blocka7e24c12009-10-30 11:49:00 +0000335class Statement;
336class String;
337class Struct;
338class SwitchStatement;
339class AstVisitor;
340class Variable;
341class VariableProxy;
342class RelocInfo;
343class Deserializer;
344class MessageLocation;
345class ObjectGroup;
346class TickSample;
347class VirtualMemory;
348class Mutex;
Steve Blocka7e24c12009-10-30 11:49:00 +0000349
350typedef bool (*WeakSlotCallback)(Object** pointer);
351
352// -----------------------------------------------------------------------------
353// Miscellaneous
354
355// NOTE: SpaceIterator depends on AllocationSpace enumeration values being
356// consecutive.
357enum AllocationSpace {
358 NEW_SPACE, // Semispaces collected with copying collector.
359 OLD_POINTER_SPACE, // May contain pointers to new space.
360 OLD_DATA_SPACE, // Must not have pointers to new space.
361 CODE_SPACE, // No pointers to new space, marked executable.
362 MAP_SPACE, // Only and all map objects.
363 CELL_SPACE, // Only and all cell objects.
364 LO_SPACE, // Promoted large objects.
365
366 FIRST_SPACE = NEW_SPACE,
Steve Blockd0582a62009-12-15 09:54:21 +0000367 LAST_SPACE = LO_SPACE,
368 FIRST_PAGED_SPACE = OLD_POINTER_SPACE,
369 LAST_PAGED_SPACE = CELL_SPACE
Steve Blocka7e24c12009-10-30 11:49:00 +0000370};
371const int kSpaceTagSize = 3;
372const int kSpaceTagMask = (1 << kSpaceTagSize) - 1;
373
374
375// A flag that indicates whether objects should be pretenured when
376// allocated (allocated directly into the old generation) or not
377// (allocated in the young generation if the object size and type
378// allows).
379enum PretenureFlag { NOT_TENURED, TENURED };
380
381enum GarbageCollector { SCAVENGER, MARK_COMPACTOR };
382
383enum Executability { NOT_EXECUTABLE, EXECUTABLE };
384
Leon Clarkee46be812010-01-19 14:06:41 +0000385enum VisitMode { VISIT_ALL, VISIT_ALL_IN_SCAVENGE, VISIT_ONLY_STRONG };
Steve Blockd0582a62009-12-15 09:54:21 +0000386
Steve Block6ded16b2010-05-10 14:33:55 +0100387// Flag indicating whether code is built into the VM (one of the natives files).
Andrei Popescu31002712010-02-23 13:46:05 +0000388enum NativesFlag { NOT_NATIVES_CODE, NATIVES_CODE };
389
Steve Blocka7e24c12009-10-30 11:49:00 +0000390
391// A CodeDesc describes a buffer holding instructions and relocation
392// information. The instructions start at the beginning of the buffer
393// and grow forward, the relocation information starts at the end of
394// the buffer and grows backward.
395//
396// |<--------------- buffer_size ---------------->|
397// |<-- instr_size -->| |<-- reloc_size -->|
398// +==================+========+==================+
399// | instructions | free | reloc info |
400// +==================+========+==================+
401// ^
402// |
403// buffer
404
405struct CodeDesc {
406 byte* buffer;
407 int buffer_size;
408 int instr_size;
409 int reloc_size;
410 Assembler* origin;
411};
412
413
414// Callback function on object slots, used for iterating heap object slots in
415// HeapObjects, global pointers to heap objects, etc. The callback allows the
416// callback function to change the value of the slot.
417typedef void (*ObjectSlotCallback)(HeapObject** pointer);
418
419
420// Callback function used for iterating objects in heap spaces,
421// for example, scanning heap objects.
422typedef int (*HeapObjectCallback)(HeapObject* obj);
423
424
425// Callback function used for checking constraints when copying/relocating
426// objects. Returns true if an object can be copied/relocated from its
427// old_addr to a new_addr.
428typedef bool (*ConstraintCallback)(Address new_addr, Address old_addr);
429
430
431// Callback function on inline caches, used for iterating over inline caches
432// in compiled code.
433typedef void (*InlineCacheCallback)(Code* code, Address ic);
434
435
436// State for inline cache call sites. Aliased as IC::State.
437enum InlineCacheState {
438 // Has never been executed.
439 UNINITIALIZED,
440 // Has been executed but monomorhic state has been delayed.
441 PREMONOMORPHIC,
442 // Has been executed and only one receiver type has been seen.
443 MONOMORPHIC,
444 // Like MONOMORPHIC but check failed due to prototype.
445 MONOMORPHIC_PROTOTYPE_FAILURE,
446 // Multiple receiver types have been seen.
447 MEGAMORPHIC,
448 // Special states for debug break or step in prepare stubs.
449 DEBUG_BREAK,
450 DEBUG_PREPARE_STEP_IN
451};
452
453
454enum InLoopFlag {
455 NOT_IN_LOOP,
456 IN_LOOP
457};
458
459
Leon Clarkee46be812010-01-19 14:06:41 +0000460enum CallFunctionFlags {
461 NO_CALL_FUNCTION_FLAGS = 0,
462 RECEIVER_MIGHT_BE_VALUE = 1 << 0 // Receiver might not be a JSObject.
463};
464
465
Steve Block8defd9f2010-07-08 12:39:36 +0100466enum InlineCacheHolderFlag {
467 OWN_MAP, // For fast properties objects.
468 PROTOTYPE_MAP // For slow properties objects (except GlobalObjects).
469};
470
471
Steve Blocka7e24c12009-10-30 11:49:00 +0000472// Type of properties.
473// Order of properties is significant.
474// Must fit in the BitField PropertyDetails::TypeField.
Andrei Popescu31002712010-02-23 13:46:05 +0000475// A copy of this is in mirror-debugger.js.
Steve Blocka7e24c12009-10-30 11:49:00 +0000476enum PropertyType {
477 NORMAL = 0, // only in slow mode
478 FIELD = 1, // only in fast mode
479 CONSTANT_FUNCTION = 2, // only in fast mode
480 CALLBACKS = 3,
481 INTERCEPTOR = 4, // only in lookup results, not in descriptors.
482 MAP_TRANSITION = 5, // only in fast mode
483 CONSTANT_TRANSITION = 6, // only in fast mode
484 NULL_DESCRIPTOR = 7, // only in fast mode
485 // All properties before MAP_TRANSITION are real.
Steve Block6ded16b2010-05-10 14:33:55 +0100486 FIRST_PHANTOM_PROPERTY_TYPE = MAP_TRANSITION,
487 // There are no IC stubs for NULL_DESCRIPTORS. Therefore,
488 // NULL_DESCRIPTOR can be used as the type flag for IC stubs for
489 // nonexistent properties.
490 NONEXISTENT = NULL_DESCRIPTOR
Steve Blocka7e24c12009-10-30 11:49:00 +0000491};
492
493
494// Whether to remove map transitions and constant transitions from a
495// DescriptorArray.
496enum TransitionFlag {
497 REMOVE_TRANSITIONS,
498 KEEP_TRANSITIONS
499};
500
501
502// Union used for fast testing of specific double values.
503union DoubleRepresentation {
504 double value;
505 int64_t bits;
506 DoubleRepresentation(double x) { value = x; }
507};
508
509
510// AccessorCallback
511struct AccessorDescriptor {
512 Object* (*getter)(Object* object, void* data);
513 Object* (*setter)(JSObject* object, Object* value, void* data);
514 void* data;
515};
516
517
518// Logging and profiling.
519// A StateTag represents a possible state of the VM. When compiled with
Steve Block6ded16b2010-05-10 14:33:55 +0100520// ENABLE_VMSTATE_TRACKING, the logger maintains a stack of these.
Steve Blocka7e24c12009-10-30 11:49:00 +0000521// Creating a VMState object enters a state by pushing on the stack, and
522// destroying a VMState object leaves a state by popping the current state
523// from the stack.
524
525#define STATE_TAG_LIST(V) \
526 V(JS) \
527 V(GC) \
528 V(COMPILER) \
529 V(OTHER) \
530 V(EXTERNAL)
531
532enum StateTag {
533#define DEF_STATE_TAG(name) name,
534 STATE_TAG_LIST(DEF_STATE_TAG)
535#undef DEF_STATE_TAG
536 // Pseudo-types.
537 state_tag_count
538};
539
540
541// -----------------------------------------------------------------------------
542// Macros
543
544// Testers for test.
545
546#define HAS_SMI_TAG(value) \
547 ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag)
548
549#define HAS_FAILURE_TAG(value) \
550 ((reinterpret_cast<intptr_t>(value) & kFailureTagMask) == kFailureTag)
551
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100552// OBJECT_POINTER_ALIGN returns the value aligned as a HeapObject pointer
553#define OBJECT_POINTER_ALIGN(value) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000554 (((value) + kObjectAlignmentMask) & ~kObjectAlignmentMask)
555
556// POINTER_SIZE_ALIGN returns the value aligned as a pointer.
557#define POINTER_SIZE_ALIGN(value) \
558 (((value) + kPointerAlignmentMask) & ~kPointerAlignmentMask)
559
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100560// MAP_POINTER_ALIGN returns the value aligned as a map pointer.
561#define MAP_POINTER_ALIGN(value) \
Leon Clarkee46be812010-01-19 14:06:41 +0000562 (((value) + kMapAlignmentMask) & ~kMapAlignmentMask)
563
Steve Blocka7e24c12009-10-30 11:49:00 +0000564// The expression OFFSET_OF(type, field) computes the byte-offset
565// of the specified field relative to the containing type. This
566// corresponds to 'offsetof' (in stddef.h), except that it doesn't
567// use 0 or NULL, which causes a problem with the compiler warnings
568// we have enabled (which is also why 'offsetof' doesn't seem to work).
569// Here we simply use the non-zero value 4, which seems to work.
570#define OFFSET_OF(type, field) \
571 (reinterpret_cast<intptr_t>(&(reinterpret_cast<type*>(4)->field)) - 4)
572
573
574// The expression ARRAY_SIZE(a) is a compile-time constant of type
575// size_t which represents the number of elements of the given
576// array. You should only use ARRAY_SIZE on statically allocated
577// arrays.
578#define ARRAY_SIZE(a) \
579 ((sizeof(a) / sizeof(*(a))) / \
580 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
581
582
583// The USE(x) template is used to silence C++ compiler warnings
584// issued for (yet) unused variables (typically parameters).
585template <typename T>
586static inline void USE(T) { }
587
588
589// FUNCTION_ADDR(f) gets the address of a C function f.
590#define FUNCTION_ADDR(f) \
591 (reinterpret_cast<v8::internal::Address>(reinterpret_cast<intptr_t>(f)))
592
593
594// FUNCTION_CAST<F>(addr) casts an address into a function
595// of type F. Used to invoke generated code from within C.
596template <typename F>
597F FUNCTION_CAST(Address addr) {
598 return reinterpret_cast<F>(reinterpret_cast<intptr_t>(addr));
599}
600
601
602// A macro to disallow the evil copy constructor and operator= functions
603// This should be used in the private: declarations for a class
604#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
605 TypeName(const TypeName&); \
606 void operator=(const TypeName&)
607
608
609// A macro to disallow all the implicit constructors, namely the
610// default constructor, copy constructor and operator= functions.
611//
612// This should be used in the private: declarations for a class
613// that wants to prevent anyone from instantiating it. This is
614// especially useful for classes containing only static methods.
615#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
616 TypeName(); \
617 DISALLOW_COPY_AND_ASSIGN(TypeName)
618
619
620// Support for tracking C++ memory allocation. Insert TRACK_MEMORY("Fisk")
621// inside a C++ class and new and delete will be overloaded so logging is
622// performed.
623// This file (globals.h) is included before log.h, so we use direct calls to
624// the Logger rather than the LOG macro.
625#ifdef DEBUG
626#define TRACK_MEMORY(name) \
627 void* operator new(size_t size) { \
628 void* result = ::operator new(size); \
629 Logger::NewEvent(name, result, size); \
630 return result; \
631 } \
632 void operator delete(void* object) { \
633 Logger::DeleteEvent(name, object); \
634 ::operator delete(object); \
635 }
636#else
637#define TRACK_MEMORY(name)
638#endif
639
640// define used for helping GCC to make better inlining. Don't bother for debug
641// builds. On GCC 3.4.5 using __attribute__((always_inline)) causes compilation
642// errors in debug build.
643#if defined(__GNUC__) && !defined(DEBUG)
644#if (__GNUC__ >= 4)
645#define INLINE(header) inline header __attribute__((always_inline))
646#else
647#define INLINE(header) inline __attribute__((always_inline)) header
648#endif
649#else
650#define INLINE(header) inline header
651#endif
652
Steve Blockd0582a62009-12-15 09:54:21 +0000653// Feature flags bit positions. They are mostly based on the CPUID spec.
654// (We assign CPUID itself to one of the currently reserved bits --
655// feel free to change this if needed.)
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100656// On X86/X64, values below 32 are bits in EDX, values above 32 are bits in ECX.
657enum CpuFeature { SSE4_1 = 32 + 19, // x86
658 SSE3 = 32 + 0, // x86
Steve Blockd0582a62009-12-15 09:54:21 +0000659 SSE2 = 26, // x86
660 CMOV = 15, // x86
661 RDTSC = 4, // x86
662 CPUID = 10, // x86
663 VFP3 = 1, // ARM
Andrei Popescu31002712010-02-23 13:46:05 +0000664 ARMv7 = 2, // ARM
Steve Blockd0582a62009-12-15 09:54:21 +0000665 SAHF = 0}; // x86
666
Steve Blocka7e24c12009-10-30 11:49:00 +0000667} } // namespace v8::internal
668
669#endif // V8_GLOBALS_H_