blob: ad0539f4609bfb4b795e489776dbf1d78c05b1ff [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
49#else
50#error Your host architecture was not detected as supported by v8
51#endif
52
53#if defined(V8_TARGET_ARCH_X64) || defined(V8_TARGET_ARCH_IA32)
54#define V8_TARGET_CAN_READ_UNALIGNED 1
55#elif V8_TARGET_ARCH_ARM
56#else
57#error Your target architecture is not supported by v8
58#endif
59
60// Support for alternative bool type. This is only enabled if the code is
61// compiled with USE_MYBOOL defined. This catches some nasty type bugs.
62// For instance, 'bool b = "false";' results in b == true! This is a hidden
63// source of bugs.
64// However, redefining the bool type does have some negative impact on some
65// platforms. It gives rise to compiler warnings (i.e. with
66// MSVC) in the API header files when mixing code that uses the standard
67// bool with code that uses the redefined version.
68// This does not actually belong in the platform code, but needs to be
69// defined here because the platform code uses bool, and platform.h is
70// include very early in the main include file.
71
72#ifdef USE_MYBOOL
73typedef unsigned int __my_bool__;
74#define bool __my_bool__ // use 'indirection' to avoid name clashes
75#endif
76
77typedef uint8_t byte;
78typedef byte* Address;
79
80// Define our own macros for writing 64-bit constants. This is less fragile
81// than defining __STDC_CONSTANT_MACROS before including <stdint.h>, and it
82// works on compilers that don't have it (like MSVC).
83#if V8_HOST_ARCH_64_BIT
84#ifdef _MSC_VER
85#define V8_UINT64_C(x) (x ## UI64)
86#define V8_INT64_C(x) (x ## I64)
87#define V8_PTR_PREFIX "ll"
88#else // _MSC_VER
89#define V8_UINT64_C(x) (x ## UL)
90#define V8_INT64_C(x) (x ## L)
91#define V8_PTR_PREFIX "l"
92#endif // _MSC_VER
93#else // V8_HOST_ARCH_64_BIT
94#define V8_PTR_PREFIX ""
95#endif // V8_HOST_ARCH_64_BIT
96
97#define V8PRIxPTR V8_PTR_PREFIX "x"
98#define V8PRIdPTR V8_PTR_PREFIX "d"
99
100// Fix for Mac OS X defining uintptr_t as "unsigned long":
101#if defined(__APPLE__) && defined(__MACH__)
102#undef V8PRIxPTR
103#define V8PRIxPTR "lx"
104#endif
105
Steve Blockd0582a62009-12-15 09:54:21 +0000106#if defined(__APPLE__) && defined(__MACH__)
107#define USING_MAC_ABI
108#endif
109
Steve Blocka7e24c12009-10-30 11:49:00 +0000110// Code-point values in Unicode 4.0 are 21 bits wide.
111typedef uint16_t uc16;
112typedef int32_t uc32;
113
114// -----------------------------------------------------------------------------
115// Constants
116
117const int KB = 1024;
118const int MB = KB * KB;
119const int GB = KB * KB * KB;
120const int kMaxInt = 0x7FFFFFFF;
121const int kMinInt = -kMaxInt - 1;
122
123const uint32_t kMaxUInt32 = 0xFFFFFFFFu;
124
125const int kCharSize = sizeof(char); // NOLINT
126const int kShortSize = sizeof(short); // NOLINT
127const int kIntSize = sizeof(int); // NOLINT
128const int kDoubleSize = sizeof(double); // NOLINT
129const int kPointerSize = sizeof(void*); // NOLINT
130const int kIntptrSize = sizeof(intptr_t); // NOLINT
131
132#if V8_HOST_ARCH_64_BIT
133const int kPointerSizeLog2 = 3;
134const intptr_t kIntptrSignBit = V8_INT64_C(0x8000000000000000);
135#else
136const int kPointerSizeLog2 = 2;
137const intptr_t kIntptrSignBit = 0x80000000;
138#endif
139
140const int kObjectAlignmentBits = kPointerSizeLog2;
141const intptr_t kObjectAlignment = 1 << kObjectAlignmentBits;
142const intptr_t kObjectAlignmentMask = kObjectAlignment - 1;
143
144// Desired alignment for pointers.
145const intptr_t kPointerAlignment = (1 << kPointerSizeLog2);
146const intptr_t kPointerAlignmentMask = kPointerAlignment - 1;
147
148
149// Tag information for Failure.
150const int kFailureTag = 3;
151const int kFailureTagSize = 2;
152const intptr_t kFailureTagMask = (1 << kFailureTagSize) - 1;
153
154
155const int kBitsPerByte = 8;
156const int kBitsPerByteLog2 = 3;
157const int kBitsPerPointer = kPointerSize * kBitsPerByte;
158const int kBitsPerInt = kIntSize * kBitsPerByte;
159
160
161// Zap-value: The value used for zapping dead objects.
162// Should be a recognizable hex value tagged as a heap object pointer.
163#ifdef V8_HOST_ARCH_64_BIT
164const Address kZapValue =
165 reinterpret_cast<Address>(V8_UINT64_C(0xdeadbeedbeadbeed));
166const Address kHandleZapValue =
167 reinterpret_cast<Address>(V8_UINT64_C(0x1baddead0baddead));
168const Address kFromSpaceZapValue =
169 reinterpret_cast<Address>(V8_UINT64_C(0x1beefdad0beefdad));
170#else
171const Address kZapValue = reinterpret_cast<Address>(0xdeadbeed);
172const Address kHandleZapValue = reinterpret_cast<Address>(0xbaddead);
173const Address kFromSpaceZapValue = reinterpret_cast<Address>(0xbeefdad);
174#endif
175
176
Steve Blockd0582a62009-12-15 09:54:21 +0000177// Constants relevant to double precision floating point numbers.
178
179// Quiet NaNs have bits 51 to 62 set, possibly the sign bit, and no
180// other bits set.
181const uint64_t kQuietNaNMask = static_cast<uint64_t>(0xfff) << 51;
182// If looking only at the top 32 bits, the QNaN mask is bits 19 to 30.
183const uint32_t kQuietNaNHighBitsMask = 0xfff << (51 - 32);
184
185
Steve Blocka7e24c12009-10-30 11:49:00 +0000186// -----------------------------------------------------------------------------
187// Forward declarations for frequently used classes
188// (sorted alphabetically)
189
190class AccessorInfo;
191class Allocation;
192class Arguments;
193class Assembler;
194class BreakableStatement;
195class Code;
196class CodeGenerator;
197class CodeStub;
198class Context;
199class Debug;
200class Debugger;
201class DebugInfo;
202class Descriptor;
203class DescriptorArray;
204class Expression;
205class ExternalReference;
206class FixedArray;
207class FunctionEntry;
208class FunctionLiteral;
209class FunctionTemplateInfo;
210class NumberDictionary;
211class StringDictionary;
212class FreeStoreAllocationPolicy;
213template <typename T> class Handle;
214class Heap;
215class HeapObject;
216class IC;
217class InterceptorInfo;
218class IterationStatement;
219class Array;
220class JSArray;
221class JSFunction;
222class JSObject;
223class LargeObjectSpace;
224template <typename T, class P = FreeStoreAllocationPolicy> class List;
225class LookupResult;
226class MacroAssembler;
227class Map;
228class MapSpace;
229class MarkCompactCollector;
230class NewSpace;
231class NodeVisitor;
232class Object;
233class OldSpace;
234class Property;
235class Proxy;
236class RegExpNode;
237struct RegExpCompileData;
238class RegExpTree;
239class RegExpCompiler;
240class RegExpVisitor;
241class Scope;
242template<class Allocator = FreeStoreAllocationPolicy> class ScopeInfo;
243class Script;
244class Slot;
245class Smi;
246class Statement;
247class String;
248class Struct;
249class SwitchStatement;
250class AstVisitor;
251class Variable;
252class VariableProxy;
253class RelocInfo;
254class Deserializer;
255class MessageLocation;
256class ObjectGroup;
257class TickSample;
258class VirtualMemory;
259class Mutex;
260class ZoneScopeInfo;
261
262typedef bool (*WeakSlotCallback)(Object** pointer);
263
264// -----------------------------------------------------------------------------
265// Miscellaneous
266
267// NOTE: SpaceIterator depends on AllocationSpace enumeration values being
268// consecutive.
269enum AllocationSpace {
270 NEW_SPACE, // Semispaces collected with copying collector.
271 OLD_POINTER_SPACE, // May contain pointers to new space.
272 OLD_DATA_SPACE, // Must not have pointers to new space.
273 CODE_SPACE, // No pointers to new space, marked executable.
274 MAP_SPACE, // Only and all map objects.
275 CELL_SPACE, // Only and all cell objects.
276 LO_SPACE, // Promoted large objects.
277
278 FIRST_SPACE = NEW_SPACE,
Steve Blockd0582a62009-12-15 09:54:21 +0000279 LAST_SPACE = LO_SPACE,
280 FIRST_PAGED_SPACE = OLD_POINTER_SPACE,
281 LAST_PAGED_SPACE = CELL_SPACE
Steve Blocka7e24c12009-10-30 11:49:00 +0000282};
283const int kSpaceTagSize = 3;
284const int kSpaceTagMask = (1 << kSpaceTagSize) - 1;
285
286
287// A flag that indicates whether objects should be pretenured when
288// allocated (allocated directly into the old generation) or not
289// (allocated in the young generation if the object size and type
290// allows).
291enum PretenureFlag { NOT_TENURED, TENURED };
292
293enum GarbageCollector { SCAVENGER, MARK_COMPACTOR };
294
295enum Executability { NOT_EXECUTABLE, EXECUTABLE };
296
Steve Blockd0582a62009-12-15 09:54:21 +0000297enum VisitMode { VISIT_ALL, VISIT_ONLY_STRONG };
298
Steve Blocka7e24c12009-10-30 11:49:00 +0000299
300// A CodeDesc describes a buffer holding instructions and relocation
301// information. The instructions start at the beginning of the buffer
302// and grow forward, the relocation information starts at the end of
303// the buffer and grows backward.
304//
305// |<--------------- buffer_size ---------------->|
306// |<-- instr_size -->| |<-- reloc_size -->|
307// +==================+========+==================+
308// | instructions | free | reloc info |
309// +==================+========+==================+
310// ^
311// |
312// buffer
313
314struct CodeDesc {
315 byte* buffer;
316 int buffer_size;
317 int instr_size;
318 int reloc_size;
319 Assembler* origin;
320};
321
322
323// Callback function on object slots, used for iterating heap object slots in
324// HeapObjects, global pointers to heap objects, etc. The callback allows the
325// callback function to change the value of the slot.
326typedef void (*ObjectSlotCallback)(HeapObject** pointer);
327
328
329// Callback function used for iterating objects in heap spaces,
330// for example, scanning heap objects.
331typedef int (*HeapObjectCallback)(HeapObject* obj);
332
333
334// Callback function used for checking constraints when copying/relocating
335// objects. Returns true if an object can be copied/relocated from its
336// old_addr to a new_addr.
337typedef bool (*ConstraintCallback)(Address new_addr, Address old_addr);
338
339
340// Callback function on inline caches, used for iterating over inline caches
341// in compiled code.
342typedef void (*InlineCacheCallback)(Code* code, Address ic);
343
344
345// State for inline cache call sites. Aliased as IC::State.
346enum InlineCacheState {
347 // Has never been executed.
348 UNINITIALIZED,
349 // Has been executed but monomorhic state has been delayed.
350 PREMONOMORPHIC,
351 // Has been executed and only one receiver type has been seen.
352 MONOMORPHIC,
353 // Like MONOMORPHIC but check failed due to prototype.
354 MONOMORPHIC_PROTOTYPE_FAILURE,
355 // Multiple receiver types have been seen.
356 MEGAMORPHIC,
357 // Special states for debug break or step in prepare stubs.
358 DEBUG_BREAK,
359 DEBUG_PREPARE_STEP_IN
360};
361
362
363enum InLoopFlag {
364 NOT_IN_LOOP,
365 IN_LOOP
366};
367
368
369// Type of properties.
370// Order of properties is significant.
371// Must fit in the BitField PropertyDetails::TypeField.
372// A copy of this is in mirror-delay.js.
373enum PropertyType {
374 NORMAL = 0, // only in slow mode
375 FIELD = 1, // only in fast mode
376 CONSTANT_FUNCTION = 2, // only in fast mode
377 CALLBACKS = 3,
378 INTERCEPTOR = 4, // only in lookup results, not in descriptors.
379 MAP_TRANSITION = 5, // only in fast mode
380 CONSTANT_TRANSITION = 6, // only in fast mode
381 NULL_DESCRIPTOR = 7, // only in fast mode
382 // All properties before MAP_TRANSITION are real.
383 FIRST_PHANTOM_PROPERTY_TYPE = MAP_TRANSITION
384};
385
386
387// Whether to remove map transitions and constant transitions from a
388// DescriptorArray.
389enum TransitionFlag {
390 REMOVE_TRANSITIONS,
391 KEEP_TRANSITIONS
392};
393
394
395// Union used for fast testing of specific double values.
396union DoubleRepresentation {
397 double value;
398 int64_t bits;
399 DoubleRepresentation(double x) { value = x; }
400};
401
402
403// AccessorCallback
404struct AccessorDescriptor {
405 Object* (*getter)(Object* object, void* data);
406 Object* (*setter)(JSObject* object, Object* value, void* data);
407 void* data;
408};
409
410
411// Logging and profiling.
412// A StateTag represents a possible state of the VM. When compiled with
413// ENABLE_LOGGING_AND_PROFILING, the logger maintains a stack of these.
414// Creating a VMState object enters a state by pushing on the stack, and
415// destroying a VMState object leaves a state by popping the current state
416// from the stack.
417
418#define STATE_TAG_LIST(V) \
419 V(JS) \
420 V(GC) \
421 V(COMPILER) \
422 V(OTHER) \
423 V(EXTERNAL)
424
425enum StateTag {
426#define DEF_STATE_TAG(name) name,
427 STATE_TAG_LIST(DEF_STATE_TAG)
428#undef DEF_STATE_TAG
429 // Pseudo-types.
430 state_tag_count
431};
432
433
434// -----------------------------------------------------------------------------
435// Macros
436
437// Testers for test.
438
439#define HAS_SMI_TAG(value) \
440 ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag)
441
442#define HAS_FAILURE_TAG(value) \
443 ((reinterpret_cast<intptr_t>(value) & kFailureTagMask) == kFailureTag)
444
445// OBJECT_SIZE_ALIGN returns the value aligned HeapObject size
446#define OBJECT_SIZE_ALIGN(value) \
447 (((value) + kObjectAlignmentMask) & ~kObjectAlignmentMask)
448
449// POINTER_SIZE_ALIGN returns the value aligned as a pointer.
450#define POINTER_SIZE_ALIGN(value) \
451 (((value) + kPointerAlignmentMask) & ~kPointerAlignmentMask)
452
453// The expression OFFSET_OF(type, field) computes the byte-offset
454// of the specified field relative to the containing type. This
455// corresponds to 'offsetof' (in stddef.h), except that it doesn't
456// use 0 or NULL, which causes a problem with the compiler warnings
457// we have enabled (which is also why 'offsetof' doesn't seem to work).
458// Here we simply use the non-zero value 4, which seems to work.
459#define OFFSET_OF(type, field) \
460 (reinterpret_cast<intptr_t>(&(reinterpret_cast<type*>(4)->field)) - 4)
461
462
463// The expression ARRAY_SIZE(a) is a compile-time constant of type
464// size_t which represents the number of elements of the given
465// array. You should only use ARRAY_SIZE on statically allocated
466// arrays.
467#define ARRAY_SIZE(a) \
468 ((sizeof(a) / sizeof(*(a))) / \
469 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
470
471
472// The USE(x) template is used to silence C++ compiler warnings
473// issued for (yet) unused variables (typically parameters).
474template <typename T>
475static inline void USE(T) { }
476
477
478// FUNCTION_ADDR(f) gets the address of a C function f.
479#define FUNCTION_ADDR(f) \
480 (reinterpret_cast<v8::internal::Address>(reinterpret_cast<intptr_t>(f)))
481
482
483// FUNCTION_CAST<F>(addr) casts an address into a function
484// of type F. Used to invoke generated code from within C.
485template <typename F>
486F FUNCTION_CAST(Address addr) {
487 return reinterpret_cast<F>(reinterpret_cast<intptr_t>(addr));
488}
489
490
491// A macro to disallow the evil copy constructor and operator= functions
492// This should be used in the private: declarations for a class
493#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
494 TypeName(const TypeName&); \
495 void operator=(const TypeName&)
496
497
498// A macro to disallow all the implicit constructors, namely the
499// default constructor, copy constructor and operator= functions.
500//
501// This should be used in the private: declarations for a class
502// that wants to prevent anyone from instantiating it. This is
503// especially useful for classes containing only static methods.
504#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
505 TypeName(); \
506 DISALLOW_COPY_AND_ASSIGN(TypeName)
507
508
509// Support for tracking C++ memory allocation. Insert TRACK_MEMORY("Fisk")
510// inside a C++ class and new and delete will be overloaded so logging is
511// performed.
512// This file (globals.h) is included before log.h, so we use direct calls to
513// the Logger rather than the LOG macro.
514#ifdef DEBUG
515#define TRACK_MEMORY(name) \
516 void* operator new(size_t size) { \
517 void* result = ::operator new(size); \
518 Logger::NewEvent(name, result, size); \
519 return result; \
520 } \
521 void operator delete(void* object) { \
522 Logger::DeleteEvent(name, object); \
523 ::operator delete(object); \
524 }
525#else
526#define TRACK_MEMORY(name)
527#endif
528
529// define used for helping GCC to make better inlining. Don't bother for debug
530// builds. On GCC 3.4.5 using __attribute__((always_inline)) causes compilation
531// errors in debug build.
532#if defined(__GNUC__) && !defined(DEBUG)
533#if (__GNUC__ >= 4)
534#define INLINE(header) inline header __attribute__((always_inline))
535#else
536#define INLINE(header) inline __attribute__((always_inline)) header
537#endif
538#else
539#define INLINE(header) inline header
540#endif
541
542// The type-based aliasing rule allows the compiler to assume that pointers of
543// different types (for some definition of different) never alias each other.
544// Thus the following code does not work:
545//
546// float f = foo();
547// int fbits = *(int*)(&f);
548//
549// The compiler 'knows' that the int pointer can't refer to f since the types
550// don't match, so the compiler may cache f in a register, leaving random data
551// in fbits. Using C++ style casts makes no difference, however a pointer to
552// char data is assumed to alias any other pointer. This is the 'memcpy
553// exception'.
554//
555// Bit_cast uses the memcpy exception to move the bits from a variable of one
556// type of a variable of another type. Of course the end result is likely to
557// be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005)
558// will completely optimize bit_cast away.
559//
560// There is an additional use for bit_cast.
561// Recent gccs will warn when they see casts that may result in breakage due to
562// the type-based aliasing rule. If you have checked that there is no breakage
563// you can use bit_cast to cast one pointer type to another. This confuses gcc
564// enough that it can no longer see that you have cast one pointer type to
565// another thus avoiding the warning.
566template <class Dest, class Source>
567inline Dest bit_cast(const Source& source) {
568 // Compile time assertion: sizeof(Dest) == sizeof(Source)
569 // A compile error here means your Dest and Source have different sizes.
570 typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1];
571
572 Dest dest;
573 memcpy(&dest, &source, sizeof(dest));
574 return dest;
575}
576
577
Steve Blockd0582a62009-12-15 09:54:21 +0000578// Feature flags bit positions. They are mostly based on the CPUID spec.
579// (We assign CPUID itself to one of the currently reserved bits --
580// feel free to change this if needed.)
581enum CpuFeature { SSE3 = 32, // x86
582 SSE2 = 26, // x86
583 CMOV = 15, // x86
584 RDTSC = 4, // x86
585 CPUID = 10, // x86
586 VFP3 = 1, // ARM
587 SAHF = 0}; // x86
588
Steve Blocka7e24c12009-10-30 11:49:00 +0000589} } // namespace v8::internal
590
591#endif // V8_GLOBALS_H_