blob: e2fb2a9f226439d88f2b96144a8d17a6de12fe66 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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
ager@chromium.org3bf7b912008-11-17 09:09:45 +000028#ifndef V8_GLOBALS_H_
29#define V8_GLOBALS_H_
30
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000031// -----------------------------------------------------------------------------
32// Types
33// Windows is missing the stdint.h header file. Instead we define standard
34// integer types for Windows here.
35
36#ifdef WIN32
37typedef signed char int8_t;
38typedef unsigned char uint8_t;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000039typedef short int16_t; // NOLINT
40typedef unsigned short uint16_t; // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041typedef int int32_t;
42typedef unsigned int uint32_t;
43typedef __int64 int64_t;
44typedef unsigned __int64 uint64_t;
45#else
46#include <stdint.h> // for intptr_t
47#endif
48
49
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050namespace v8 { namespace internal {
51
52// Support for alternative bool type. This is only enabled if the code is
53// compiled with USE_MYBOOL defined. This catches some nasty type bugs.
54// For instance, 'bool b = "false";' results in b == true! This is a hidden
55// source of bugs.
56// However, redefining the bool type does have some negative impact on some
57// platforms. It gives rise to compiler warnings (i.e. with
58// MSVC) in the API header files when mixing code that uses the standard
59// bool with code that uses the redefined version.
60// This does not actually belong in the platform code, but needs to be
61// defined here because the platform code uses bool, and platform.h is
62// include very early in the main include file.
63
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000064#ifdef USE_MYBOOL
65typedef unsigned int __my_bool__;
66#define bool __my_bool__ // use 'indirection' to avoid name clashes
67#endif
68
69typedef uint8_t byte;
70typedef byte* Address;
71
72// Code-point values in Unicode 4.0 are 21 bits wide.
73typedef uint16_t uc16;
74typedef signed int uc32;
75
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000076#ifndef ARM
77#define CAN_READ_UNALIGNED 1
78#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000079
80// -----------------------------------------------------------------------------
81// Constants
82
83#ifdef DEBUG
84const bool kDebug = true;
85#else
86const bool kDebug = false;
87#endif // DEBUG
88
89const int KB = 1024;
90const int MB = KB * KB;
91const int GB = KB * KB * KB;
92const int kMaxInt = 0x7FFFFFFF;
93const int kMinInt = -kMaxInt - 1;
94
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000095const int kCharSize = sizeof(char); // NOLINT
96const int kShortSize = sizeof(short); // NOLINT
97const int kIntSize = sizeof(int); // NOLINT
98const int kDoubleSize = sizeof(double); // NOLINT
99const int kPointerSize = sizeof(void*); // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000100
101const int kPointerSizeLog2 = 2;
102
103const int kObjectAlignmentBits = 2;
104const int kObjectAlignmentMask = (1 << kObjectAlignmentBits) - 1;
105const int kObjectAlignment = 1 << kObjectAlignmentBits;
106
107
108// Tag information for HeapObject.
109const int kHeapObjectTag = 1;
110const int kHeapObjectTagSize = 2;
111const int kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
112
113
114// Tag information for Smi.
115const int kSmiTag = 0;
116const int kSmiTagSize = 1;
117const int kSmiTagMask = (1 << kSmiTagSize) - 1;
118
119
120// Tag information for Failure.
121const int kFailureTag = 3;
122const int kFailureTagSize = 2;
123const int kFailureTagMask = (1 << kFailureTagSize) - 1;
124
125
126const int kBitsPerByte = 8;
127const int kBitsPerByteLog2 = 3;
128const int kBitsPerPointer = kPointerSize * kBitsPerByte;
129const int kBitsPerInt = kIntSize * kBitsPerByte;
130
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131
132// Zap-value: The value used for zapping dead objects. Should be a recognizable
133// illegal heap object pointer.
134const Address kZapValue = reinterpret_cast<Address>(0xdeadbeed);
135const Address kHandleZapValue = reinterpret_cast<Address>(0xbaddead);
136const Address kFromSpaceZapValue = reinterpret_cast<Address>(0xbeefdad);
137
138// -----------------------------------------------------------------------------
139// Forward declarations for frequently used classes
140// (sorted alphabetically)
141
142class AccessorInfo;
143class Allocation;
144class Assembler;
145class BreakableStatement;
146class Code;
147class CodeGenerator;
148class CodeStub;
149class Context;
150class Debug;
151class Debugger;
152class DebugInfo;
153class Descriptor;
154class DescriptorArray;
155class Expression;
156class ExternalReference;
157class FixedArray;
158class FunctionEntry;
159class FunctionLiteral;
160class FunctionTemplateInfo;
161class Dictionary;
162class FreeStoreAllocationPolicy;
163template <typename T> class Handle;
164class Heap;
165class HeapObject;
166class IC;
167class InterceptorInfo;
168class IterationStatement;
169class JSArray;
170class JSFunction;
171class JSObject;
172class LabelCollector;
173class LargeObjectSpace;
174template <typename T, class P = FreeStoreAllocationPolicy> class List;
175class LookupResult;
176class MacroAssembler;
177class Map;
178class MapSpace;
179class MarkCompactCollector;
180class NewSpace;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000181class NodeVisitor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182class Object;
183class OldSpace;
184class Property;
185class Proxy;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000186class RegExpNode;
187struct RegExpParseResult;
188class RegExpTree;
189class RegExpCompiler;
190class RegExpVisitor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191class Scope;
192template<class Allocator = FreeStoreAllocationPolicy> class ScopeInfo;
193class Script;
194class Slot;
195class Smi;
196class Statement;
197class String;
198class Struct;
199class SwitchStatement;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000200class AstVisitor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201class Variable;
202class VariableProxy;
203class RelocInfo;
204class Deserializer;
205class MessageLocation;
206class ObjectGroup;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000207class TickSample;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208class VirtualMemory;
209class Mutex;
210
211typedef bool (*WeakSlotCallback)(Object** pointer);
212
213// -----------------------------------------------------------------------------
214// Miscellaneous
215
216// NOTE: SpaceIterator depends on AllocationSpace enumeration values being
kasper.lund7276f142008-07-30 08:49:36 +0000217// consecutive.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000218enum AllocationSpace {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000219 NEW_SPACE, // Semispaces collected with copying collector.
220 OLD_POINTER_SPACE, // Must be first of the paged spaces - see PagedSpaces.
221 OLD_DATA_SPACE, // May not have pointers to new space.
222 CODE_SPACE, // Also one of the old spaces. Marked executable.
223 MAP_SPACE, // Only map objects.
224 LO_SPACE, // Large objects.
kasper.lund7276f142008-07-30 08:49:36 +0000225 FIRST_SPACE = NEW_SPACE,
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000226 LAST_SPACE = LO_SPACE // <= 5 (see kSpaceBits and kLOSpacePointer)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227};
228const int kSpaceTagSize = 3;
229const int kSpaceTagMask = (1 << kSpaceTagSize) - 1;
230
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000231
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000232// A flag that indicates whether objects should be pretenured when
233// allocated (allocated directly into the old generation) or not
234// (allocated in the young generation if the object size and type
235// allows).
236enum PretenureFlag { NOT_TENURED, TENURED };
237
238enum GarbageCollector { SCAVENGER, MARK_COMPACTOR };
239
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000240enum Executability { NOT_EXECUTABLE, EXECUTABLE };
241
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000242
243// A CodeDesc describes a buffer holding instructions and relocation
244// information. The instructions start at the beginning of the buffer
245// and grow forward, the relocation information starts at the end of
246// the buffer and grows backward.
247//
248// |<--------------- buffer_size ---------------->|
249// |<-- instr_size -->| |<-- reloc_size -->|
250// +==================+========+==================+
251// | instructions | free | reloc info |
252// +==================+========+==================+
253// ^
254// |
255// buffer
256
257struct CodeDesc {
258 byte* buffer;
259 int buffer_size;
260 int instr_size;
261 int reloc_size;
262};
263
264
265// Callback function on object slots, used for iterating heap object slots in
266// HeapObjects, global pointers to heap objects, etc. The callback allows the
267// callback function to change the value of the slot.
268typedef void (*ObjectSlotCallback)(HeapObject** pointer);
269
270
271// Callback function used for iterating objects in heap spaces,
272// for example, scanning heap objects.
273typedef int (*HeapObjectCallback)(HeapObject* obj);
274
275
276// Callback function used for checking constraints when copying/relocating
277// objects. Returns true if an object can be copied/relocated from its
278// old_addr to a new_addr.
279typedef bool (*ConstraintCallback)(Address new_addr, Address old_addr);
280
281
282// Callback function on inline caches, used for iterating over inline caches
283// in compiled code.
284typedef void (*InlineCacheCallback)(Code* code, Address ic);
285
286
287// State for inline cache call sites. Aliased as IC::State.
288enum InlineCacheState {
289 // Has never been executed.
290 UNINITIALIZED,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000291 // Has never been executed, but is in a loop.
292 UNINITIALIZED_IN_LOOP,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 // Has been executed but monomorhic state has been delayed.
294 PREMONOMORPHIC,
295 // Has been executed and only one receiver type has been seen.
296 MONOMORPHIC,
297 // Like MONOMORPHIC but check failed due to prototype.
298 MONOMORPHIC_PROTOTYPE_FAILURE,
299 // Multiple receiver types have been seen.
300 MEGAMORPHIC,
301 // Special states for debug break or step in prepare stubs.
302 DEBUG_BREAK,
303 DEBUG_PREPARE_STEP_IN
304};
305
306
307// Type of properties.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000308// Order of properties is significant.
309// Must fit in the BitField PropertyDetails::TypeField.
310// A copy of this is in mirror-delay.js.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311enum PropertyType {
312 NORMAL = 0, // only in slow mode
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000313 FIELD = 1, // only in fast mode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 CONSTANT_FUNCTION = 2, // only in fast mode
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000315 CALLBACKS = 3,
316 INTERCEPTOR = 4, // only in lookup results, not in descriptors.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000317 MAP_TRANSITION = 5, // only in fast mode
318 CONSTANT_TRANSITION = 6, // only in fast mode
ager@chromium.org236ad962008-09-25 09:45:57 +0000319 NULL_DESCRIPTOR = 7, // only in fast mode
320 // All properties before MAP_TRANSITION are real.
321 FIRST_PHANTOM_PROPERTY_TYPE = MAP_TRANSITION
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000322};
323
324
325// Whether to remove map transitions and constant transitions from a
326// DescriptorArray.
327enum TransitionFlag {
328 REMOVE_TRANSITIONS,
329 KEEP_TRANSITIONS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330};
331
332
333// Union used for fast testing of specific double values.
334union DoubleRepresentation {
335 double value;
336 int64_t bits;
337 DoubleRepresentation(double x) { value = x; }
338};
339
340
341// AccessorCallback
342struct AccessorDescriptor {
343 Object* (*getter)(Object* object, void* data);
344 Object* (*setter)(JSObject* object, Object* value, void* data);
345 void* data;
346};
347
348
349// Logging and profiling.
350// A StateTag represents a possible state of the VM. When compiled with
351// ENABLE_LOGGING_AND_PROFILING, the logger maintains a stack of these.
352// Creating a VMState object enters a state by pushing on the stack, and
353// destroying a VMState object leaves a state by popping the current state
354// from the stack.
355
356#define STATE_TAG_LIST(V) \
357 V(JS) \
358 V(GC) \
359 V(COMPILER) \
360 V(OTHER)
361
362enum StateTag {
363#define DEF_STATE_TAG(name) name,
364 STATE_TAG_LIST(DEF_STATE_TAG)
365#undef DEF_STATE_TAG
366 // Pseudo-types.
367 state_tag_count
368};
369
370
371// -----------------------------------------------------------------------------
372// Macros
373
374// Testers for test.
375
376#define HAS_SMI_TAG(value) \
377 ((reinterpret_cast<int>(value) & kSmiTagMask) == kSmiTag)
378
379#define HAS_FAILURE_TAG(value) \
380 ((reinterpret_cast<int>(value) & kFailureTagMask) == kFailureTag)
381
382#define HAS_HEAP_OBJECT_TAG(value) \
383 ((reinterpret_cast<int>(value) & kHeapObjectTagMask) == kHeapObjectTag)
384
385// OBJECT_SIZE_ALIGN returns the value aligned HeapObject size
386#define OBJECT_SIZE_ALIGN(value) \
387 ((value + kObjectAlignmentMask) & ~kObjectAlignmentMask)
388
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000389// The expression OFFSET_OF(type, field) computes the byte-offset
390// of the specified field relative to the containing type. This
391// corresponds to 'offsetof' (in stddef.h), except that it doesn't
392// use 0 or NULL, which causes a problem with the compiler warnings
393// we have enabled (which is also why 'offsetof' doesn't seem to work).
394// Here we simply use the non-zero value 4, which seems to work.
395#define OFFSET_OF(type, field) \
396 (reinterpret_cast<intptr_t>(&(reinterpret_cast<type*>(4)->field)) - 4)
397
398
399// The expression ARRAY_SIZE(a) is a compile-time constant of type
400// size_t which represents the number of elements of the given
401// array. You should only use ARRAY_SIZE on statically allocated
402// arrays.
403#define ARRAY_SIZE(a) \
404 ((sizeof(a) / sizeof(*(a))) / \
405 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
406
407
408// The USE(x) template is used to silence C++ compiler warnings
409// issued for (yet) unused variables (typically parameters).
410template <typename T>
411static inline void USE(T) { }
412
413
414// FUNCTION_ADDR(f) gets the address of a C function f.
415#define FUNCTION_ADDR(f) \
416 (reinterpret_cast<v8::internal::Address>(reinterpret_cast<intptr_t>(f)))
417
418
419// FUNCTION_CAST<F>(addr) casts an address into a function
420// of type F. Used to invoke generated code from within C.
421template <typename F>
422F FUNCTION_CAST(Address addr) {
423 return reinterpret_cast<F>(reinterpret_cast<intptr_t>(addr));
424}
425
426
427// A macro to disallow the evil copy constructor and operator= functions
428// This should be used in the private: declarations for a class
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000429#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000430 TypeName(const TypeName&); \
431 void operator=(const TypeName&)
432
433
434// A macro to disallow all the implicit constructors, namely the
435// default constructor, copy constructor and operator= functions.
436//
437// This should be used in the private: declarations for a class
438// that wants to prevent anyone from instantiating it. This is
439// especially useful for classes containing only static methods.
440#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
441 TypeName(); \
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000442 DISALLOW_COPY_AND_ASSIGN(TypeName)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443
444
445// Support for tracking C++ memory allocation. Insert TRACK_MEMORY("Fisk")
446// inside a C++ class and new and delete will be overloaded so logging is
447// performed.
448// This file (globals.h) is included before log.h, so we use direct calls to
449// the Logger rather than the LOG macro.
450#ifdef DEBUG
451#define TRACK_MEMORY(name) \
452 void* operator new(size_t size) { \
453 void* result = ::operator new(size); \
454 Logger::NewEvent(name, result, size); \
455 return result; \
456 } \
457 void operator delete(void* object) { \
458 Logger::DeleteEvent(name, object); \
459 ::operator delete(object); \
460 }
461#else
462#define TRACK_MEMORY(name)
463#endif
464
465// define used for helping GCC to make better inlining.
466#ifdef __GNUC__
467#if (__GNUC__ >= 4)
468#define INLINE(header) inline header __attribute__((always_inline))
469#else
470#define INLINE(header) inline __attribute__((always_inline)) header
471#endif
472#else
473#define INLINE(header) inline header
474#endif
475
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000476// The type-based aliasing rule allows the compiler to assume that pointers of
477// different types (for some definition of different) never alias each other.
478// Thus the following code does not work:
479//
480// float f = foo();
481// int fbits = *(int*)(&f);
482//
483// The compiler 'knows' that the int pointer can't refer to f since the types
484// don't match, so the compiler may cache f in a register, leaving random data
485// in fbits. Using C++ style casts makes no difference, however a pointer to
486// char data is assumed to alias any other pointer. This is the 'memcpy
487// exception'.
488//
489// Bit_cast uses the memcpy exception to move the bits from a variable of one
490// type o a variable of another type. Of course the end result is likely to
491// be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005)
492// will completely optimize bit_cast away.
493//
494// There is an additional use for bit_cast.
495// Recent gccs will warn when they see casts that may result in breakage due to
496// the type-based aliasing rule. If you have checked that there is no breakage
497// you can use bit_cast to cast one pointer type to another. This confuses gcc
498// enough that it can no longer see that you have cast one pointer type to
499// another thus avoiding the warning.
500template <class Dest, class Source>
501inline Dest bit_cast(const Source& source) {
502 // Compile time assertion: sizeof(Dest) == sizeof(Source)
503 // A compile error here means your Dest and Source have different sizes.
504 typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1];
505
506 Dest dest;
507 memcpy(&dest, &source, sizeof(dest));
508 return dest;
509}
510
511
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512} } // namespace v8::internal
513
514#endif // V8_GLOBALS_H_