blob: 88c3e780d94e017b07b48f419dd7f07f78c1d6f0 [file] [log] [blame]
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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
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
John Reck59135872010-11-02 12:39:01 -070096// Determine whether we are running in a simulated environment.
97// Setting USE_SIMULATOR explicitly from the build script will force
98// the use of a simulated environment.
99#if !defined(USE_SIMULATOR)
100#if (defined(V8_TARGET_ARCH_ARM) && !defined(V8_HOST_ARCH_ARM))
101#define USE_SIMULATOR 1
102#endif
103#if (defined(V8_TARGET_ARCH_MIPS) && !defined(V8_HOST_ARCH_MIPS))
104#define USE_SIMULATOR 1
105#endif
106#endif
107
Steve Block6ded16b2010-05-10 14:33:55 +0100108// Define unaligned read for the target architectures supporting it.
Steve Blocka7e24c12009-10-30 11:49:00 +0000109#if defined(V8_TARGET_ARCH_X64) || defined(V8_TARGET_ARCH_IA32)
110#define V8_TARGET_CAN_READ_UNALIGNED 1
111#elif V8_TARGET_ARCH_ARM
Kristian Monsen25f61362010-05-21 11:50:48 +0100112// Some CPU-OS combinations allow unaligned access on ARM. We assume
113// that unaligned accesses are not allowed unless the build system
114// defines the CAN_USE_UNALIGNED_ACCESSES macro to be non-zero.
115#if CAN_USE_UNALIGNED_ACCESSES
116#define V8_TARGET_CAN_READ_UNALIGNED 1
117#endif
Andrei Popescu31002712010-02-23 13:46:05 +0000118#elif V8_TARGET_ARCH_MIPS
Steve Blocka7e24c12009-10-30 11:49:00 +0000119#else
Steve Block6ded16b2010-05-10 14:33:55 +0100120#error Target architecture is not supported by v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000121#endif
122
123// Support for alternative bool type. This is only enabled if the code is
124// compiled with USE_MYBOOL defined. This catches some nasty type bugs.
125// For instance, 'bool b = "false";' results in b == true! This is a hidden
126// source of bugs.
127// However, redefining the bool type does have some negative impact on some
128// platforms. It gives rise to compiler warnings (i.e. with
129// MSVC) in the API header files when mixing code that uses the standard
130// bool with code that uses the redefined version.
131// This does not actually belong in the platform code, but needs to be
132// defined here because the platform code uses bool, and platform.h is
133// include very early in the main include file.
134
135#ifdef USE_MYBOOL
136typedef unsigned int __my_bool__;
137#define bool __my_bool__ // use 'indirection' to avoid name clashes
138#endif
139
140typedef uint8_t byte;
141typedef byte* Address;
142
143// Define our own macros for writing 64-bit constants. This is less fragile
144// than defining __STDC_CONSTANT_MACROS before including <stdint.h>, and it
145// works on compilers that don't have it (like MSVC).
146#if V8_HOST_ARCH_64_BIT
147#ifdef _MSC_VER
148#define V8_UINT64_C(x) (x ## UI64)
149#define V8_INT64_C(x) (x ## I64)
150#define V8_PTR_PREFIX "ll"
151#else // _MSC_VER
152#define V8_UINT64_C(x) (x ## UL)
153#define V8_INT64_C(x) (x ## L)
154#define V8_PTR_PREFIX "l"
155#endif // _MSC_VER
156#else // V8_HOST_ARCH_64_BIT
157#define V8_PTR_PREFIX ""
158#endif // V8_HOST_ARCH_64_BIT
159
Steve Block6ded16b2010-05-10 14:33:55 +0100160// The following macro works on both 32 and 64-bit platforms.
161// Usage: instead of writing 0x1234567890123456
162// write V8_2PART_UINT64_C(0x12345678,90123456);
163#define V8_2PART_UINT64_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
164
Steve Blocka7e24c12009-10-30 11:49:00 +0000165#define V8PRIxPTR V8_PTR_PREFIX "x"
166#define V8PRIdPTR V8_PTR_PREFIX "d"
167
168// Fix for Mac OS X defining uintptr_t as "unsigned long":
169#if defined(__APPLE__) && defined(__MACH__)
170#undef V8PRIxPTR
171#define V8PRIxPTR "lx"
172#endif
173
Steve Block6ded16b2010-05-10 14:33:55 +0100174#if (defined(__APPLE__) && defined(__MACH__)) || \
175 defined(__FreeBSD__) || defined(__OpenBSD__)
176#define USING_BSD_ABI
Steve Blockd0582a62009-12-15 09:54:21 +0000177#endif
178
Steve Blocka7e24c12009-10-30 11:49:00 +0000179// Code-point values in Unicode 4.0 are 21 bits wide.
180typedef uint16_t uc16;
181typedef int32_t uc32;
182
183// -----------------------------------------------------------------------------
184// Constants
185
186const int KB = 1024;
187const int MB = KB * KB;
188const int GB = KB * KB * KB;
189const int kMaxInt = 0x7FFFFFFF;
190const int kMinInt = -kMaxInt - 1;
191
192const uint32_t kMaxUInt32 = 0xFFFFFFFFu;
193
194const int kCharSize = sizeof(char); // NOLINT
195const int kShortSize = sizeof(short); // NOLINT
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800196const int kIntSize = sizeof(int); // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +0000197const int kDoubleSize = sizeof(double); // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +0000198const int kIntptrSize = sizeof(intptr_t); // NOLINT
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800199const int kPointerSize = sizeof(void*); // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +0000200
201#if V8_HOST_ARCH_64_BIT
202const int kPointerSizeLog2 = 3;
203const intptr_t kIntptrSignBit = V8_INT64_C(0x8000000000000000);
John Reck59135872010-11-02 12:39:01 -0700204const uintptr_t kUintptrAllBitsSet = V8_UINT64_C(0xFFFFFFFFFFFFFFFF);
Steve Blocka7e24c12009-10-30 11:49:00 +0000205#else
206const int kPointerSizeLog2 = 2;
207const intptr_t kIntptrSignBit = 0x80000000;
John Reck59135872010-11-02 12:39:01 -0700208const uintptr_t kUintptrAllBitsSet = 0xFFFFFFFFu;
Steve Blocka7e24c12009-10-30 11:49:00 +0000209#endif
210
Steve Blocka7e24c12009-10-30 11:49:00 +0000211const int kBitsPerByte = 8;
212const int kBitsPerByteLog2 = 3;
213const int kBitsPerPointer = kPointerSize * kBitsPerByte;
214const int kBitsPerInt = kIntSize * kBitsPerByte;
215
Steve Block6ded16b2010-05-10 14:33:55 +0100216// IEEE 754 single precision floating point number bit layout.
217const uint32_t kBinary32SignMask = 0x80000000u;
218const uint32_t kBinary32ExponentMask = 0x7f800000u;
219const uint32_t kBinary32MantissaMask = 0x007fffffu;
220const int kBinary32ExponentBias = 127;
221const int kBinary32MaxExponent = 0xFE;
222const int kBinary32MinExponent = 0x01;
223const int kBinary32MantissaBits = 23;
224const int kBinary32ExponentShift = 23;
Steve Blocka7e24c12009-10-30 11:49:00 +0000225
Steve Blocka7e24c12009-10-30 11:49:00 +0000226// The expression OFFSET_OF(type, field) computes the byte-offset
227// of the specified field relative to the containing type. This
228// corresponds to 'offsetof' (in stddef.h), except that it doesn't
229// use 0 or NULL, which causes a problem with the compiler warnings
230// we have enabled (which is also why 'offsetof' doesn't seem to work).
231// Here we simply use the non-zero value 4, which seems to work.
232#define OFFSET_OF(type, field) \
233 (reinterpret_cast<intptr_t>(&(reinterpret_cast<type*>(4)->field)) - 4)
234
235
236// The expression ARRAY_SIZE(a) is a compile-time constant of type
237// size_t which represents the number of elements of the given
238// array. You should only use ARRAY_SIZE on statically allocated
239// arrays.
240#define ARRAY_SIZE(a) \
241 ((sizeof(a) / sizeof(*(a))) / \
242 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
243
244
245// The USE(x) template is used to silence C++ compiler warnings
246// issued for (yet) unused variables (typically parameters).
247template <typename T>
248static inline void USE(T) { }
249
250
251// FUNCTION_ADDR(f) gets the address of a C function f.
252#define FUNCTION_ADDR(f) \
253 (reinterpret_cast<v8::internal::Address>(reinterpret_cast<intptr_t>(f)))
254
255
256// FUNCTION_CAST<F>(addr) casts an address into a function
257// of type F. Used to invoke generated code from within C.
258template <typename F>
259F FUNCTION_CAST(Address addr) {
260 return reinterpret_cast<F>(reinterpret_cast<intptr_t>(addr));
261}
262
263
264// A macro to disallow the evil copy constructor and operator= functions
265// This should be used in the private: declarations for a class
266#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
267 TypeName(const TypeName&); \
268 void operator=(const TypeName&)
269
270
271// A macro to disallow all the implicit constructors, namely the
272// default constructor, copy constructor and operator= functions.
273//
274// This should be used in the private: declarations for a class
275// that wants to prevent anyone from instantiating it. This is
276// especially useful for classes containing only static methods.
277#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
278 TypeName(); \
279 DISALLOW_COPY_AND_ASSIGN(TypeName)
280
281
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100282// Define used for helping GCC to make better inlining. Don't bother for debug
Steve Blocka7e24c12009-10-30 11:49:00 +0000283// builds. On GCC 3.4.5 using __attribute__((always_inline)) causes compilation
284// errors in debug build.
285#if defined(__GNUC__) && !defined(DEBUG)
286#if (__GNUC__ >= 4)
287#define INLINE(header) inline header __attribute__((always_inline))
Ben Murdochbb769b22010-08-11 14:56:33 +0100288#define NO_INLINE(header) header __attribute__((noinline))
Steve Blocka7e24c12009-10-30 11:49:00 +0000289#else
290#define INLINE(header) inline __attribute__((always_inline)) header
Ben Murdochbb769b22010-08-11 14:56:33 +0100291#define NO_INLINE(header) __attribute__((noinline)) header
Steve Blocka7e24c12009-10-30 11:49:00 +0000292#endif
293#else
294#define INLINE(header) inline header
Ben Murdochbb769b22010-08-11 14:56:33 +0100295#define NO_INLINE(header) header
Steve Blocka7e24c12009-10-30 11:49:00 +0000296#endif
297
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100298
299#if defined(__GNUC__) && __GNUC__ >= 4
300#define MUST_USE_RESULT __attribute__ ((warn_unused_result))
301#else
302#define MUST_USE_RESULT
303#endif
304
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800305// -----------------------------------------------------------------------------
306// Forward declarations for frequently used classes
307// (sorted alphabetically)
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100308
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800309class FreeStoreAllocationPolicy;
310template <typename T, class P = FreeStoreAllocationPolicy> class List;
Steve Blockd0582a62009-12-15 09:54:21 +0000311
Steve Blocka7e24c12009-10-30 11:49:00 +0000312} } // namespace v8::internal
313
314#endif // V8_GLOBALS_H_