blob: bb9d0a8e8bb5578ddb0b6819b7bfc939f538c00e [file] [log] [blame]
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001// Copyright 2011 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
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000031#include "../include/v8stdint.h"
32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035
ager@chromium.org9085a012009-05-11 19:22:57 +000036// Processor architecture detection. For more info on what's defined, see:
37// http://msdn.microsoft.com/en-us/library/b0084kay.aspx
38// http://www.agner.org/optimize/calling_conventions.pdf
39// or with gcc, run: "echo | gcc -E -dM -"
40#if defined(_M_X64) || defined(__x86_64__)
41#define V8_HOST_ARCH_X64 1
42#define V8_HOST_ARCH_64_BIT 1
43#define V8_HOST_CAN_READ_UNALIGNED 1
44#elif defined(_M_IX86) || defined(__i386__)
45#define V8_HOST_ARCH_IA32 1
46#define V8_HOST_ARCH_32_BIT 1
47#define V8_HOST_CAN_READ_UNALIGNED 1
48#elif defined(__ARMEL__)
49#define V8_HOST_ARCH_ARM 1
50#define V8_HOST_ARCH_32_BIT 1
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000051// Some CPU-OS combinations allow unaligned access on ARM. We assume
52// that unaligned accesses are not allowed unless the build system
53// defines the CAN_USE_UNALIGNED_ACCESSES macro to be non-zero.
54#if CAN_USE_UNALIGNED_ACCESSES
55#define V8_HOST_CAN_READ_UNALIGNED 1
56#endif
lrn@chromium.org7516f052011-03-30 08:52:27 +000057#elif defined(__MIPSEL__)
ager@chromium.org5c838252010-02-19 08:53:10 +000058#define V8_HOST_ARCH_MIPS 1
59#define V8_HOST_ARCH_32_BIT 1
ager@chromium.org9085a012009-05-11 19:22:57 +000060#else
fschneider@chromium.org013f3e12010-04-26 13:27:52 +000061#error Host architecture was not detected as supported by v8
ager@chromium.org18ad94b2009-09-02 08:22:29 +000062#endif
63
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000064// Target architecture detection. This may be set externally. If not, detect
65// in the same way as the host architecture, that is, target the native
66// environment as presented by the compiler.
67#if !defined(V8_TARGET_ARCH_X64) && !defined(V8_TARGET_ARCH_IA32) && \
68 !defined(V8_TARGET_ARCH_ARM) && !defined(V8_TARGET_ARCH_MIPS)
69#if defined(_M_X64) || defined(__x86_64__)
70#define V8_TARGET_ARCH_X64 1
71#elif defined(_M_IX86) || defined(__i386__)
72#define V8_TARGET_ARCH_IA32 1
73#elif defined(__ARMEL__)
74#define V8_TARGET_ARCH_ARM 1
lrn@chromium.org7516f052011-03-30 08:52:27 +000075#elif defined(__MIPSEL__)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000076#define V8_TARGET_ARCH_MIPS 1
77#else
78#error Target architecture was not detected as supported by v8
79#endif
80#endif
81
fschneider@chromium.org013f3e12010-04-26 13:27:52 +000082// Check for supported combinations of host and target architectures.
83#if defined(V8_TARGET_ARCH_IA32) && !defined(V8_HOST_ARCH_IA32)
84#error Target architecture ia32 is only supported on ia32 host
85#endif
86#if defined(V8_TARGET_ARCH_X64) && !defined(V8_HOST_ARCH_X64)
87#error Target architecture x64 is only supported on x64 host
88#endif
89#if (defined(V8_TARGET_ARCH_ARM) && \
90 !(defined(V8_HOST_ARCH_IA32) || defined(V8_HOST_ARCH_ARM)))
91#error Target architecture arm is only supported on arm and ia32 host
92#endif
93#if (defined(V8_TARGET_ARCH_MIPS) && \
94 !(defined(V8_HOST_ARCH_IA32) || defined(V8_HOST_ARCH_MIPS)))
95#error Target architecture mips is only supported on mips and ia32 host
96#endif
97
lrn@chromium.org303ada72010-10-27 09:33:13 +000098// Determine whether we are running in a simulated environment.
99// Setting USE_SIMULATOR explicitly from the build script will force
100// the use of a simulated environment.
101#if !defined(USE_SIMULATOR)
102#if (defined(V8_TARGET_ARCH_ARM) && !defined(V8_HOST_ARCH_ARM))
103#define USE_SIMULATOR 1
104#endif
105#if (defined(V8_TARGET_ARCH_MIPS) && !defined(V8_HOST_ARCH_MIPS))
106#define USE_SIMULATOR 1
107#endif
108#endif
109
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000110// Define unaligned read for the target architectures supporting it.
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000111#if defined(V8_TARGET_ARCH_X64) || defined(V8_TARGET_ARCH_IA32)
112#define V8_TARGET_CAN_READ_UNALIGNED 1
113#elif V8_TARGET_ARCH_ARM
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000114// Some CPU-OS combinations allow unaligned access on ARM. We assume
115// that unaligned accesses are not allowed unless the build system
116// defines the CAN_USE_UNALIGNED_ACCESSES macro to be non-zero.
117#if CAN_USE_UNALIGNED_ACCESSES
118#define V8_TARGET_CAN_READ_UNALIGNED 1
119#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000120#elif V8_TARGET_ARCH_MIPS
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000121#else
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000122#error Target architecture is not supported by v8
ager@chromium.org9085a012009-05-11 19:22:57 +0000123#endif
124
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125// Support for alternative bool type. This is only enabled if the code is
126// compiled with USE_MYBOOL defined. This catches some nasty type bugs.
127// For instance, 'bool b = "false";' results in b == true! This is a hidden
128// source of bugs.
129// However, redefining the bool type does have some negative impact on some
130// platforms. It gives rise to compiler warnings (i.e. with
131// MSVC) in the API header files when mixing code that uses the standard
132// bool with code that uses the redefined version.
133// This does not actually belong in the platform code, but needs to be
134// defined here because the platform code uses bool, and platform.h is
135// include very early in the main include file.
136
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137#ifdef USE_MYBOOL
138typedef unsigned int __my_bool__;
139#define bool __my_bool__ // use 'indirection' to avoid name clashes
140#endif
141
142typedef uint8_t byte;
143typedef byte* Address;
144
ager@chromium.org9085a012009-05-11 19:22:57 +0000145// Define our own macros for writing 64-bit constants. This is less fragile
146// than defining __STDC_CONSTANT_MACROS before including <stdint.h>, and it
147// works on compilers that don't have it (like MSVC).
148#if V8_HOST_ARCH_64_BIT
ager@chromium.org5ec48922009-05-05 07:25:34 +0000149#ifdef _MSC_VER
ager@chromium.org9085a012009-05-11 19:22:57 +0000150#define V8_UINT64_C(x) (x ## UI64)
151#define V8_INT64_C(x) (x ## I64)
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000152#define V8_INTPTR_C(x) (x ## I64)
ager@chromium.org9085a012009-05-11 19:22:57 +0000153#define V8_PTR_PREFIX "ll"
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000154#else // _MSC_VER
ager@chromium.org9085a012009-05-11 19:22:57 +0000155#define V8_UINT64_C(x) (x ## UL)
156#define V8_INT64_C(x) (x ## L)
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000157#define V8_INTPTR_C(x) (x ## L)
ager@chromium.org9085a012009-05-11 19:22:57 +0000158#define V8_PTR_PREFIX "l"
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000159#endif // _MSC_VER
ager@chromium.org9085a012009-05-11 19:22:57 +0000160#else // V8_HOST_ARCH_64_BIT
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000161#define V8_INTPTR_C(x) (x)
ager@chromium.org9085a012009-05-11 19:22:57 +0000162#define V8_PTR_PREFIX ""
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000163#endif // V8_HOST_ARCH_64_BIT
ager@chromium.org9085a012009-05-11 19:22:57 +0000164
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000165// The following macro works on both 32 and 64-bit platforms.
166// Usage: instead of writing 0x1234567890123456
167// write V8_2PART_UINT64_C(0x12345678,90123456);
168#define V8_2PART_UINT64_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
169
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000170#define V8PRIxPTR V8_PTR_PREFIX "x"
171#define V8PRIdPTR V8_PTR_PREFIX "d"
172
173// Fix for Mac OS X defining uintptr_t as "unsigned long":
174#if defined(__APPLE__) && defined(__MACH__)
175#undef V8PRIxPTR
176#define V8PRIxPTR "lx"
177#endif
ager@chromium.org5ec48922009-05-05 07:25:34 +0000178
lrn@chromium.org25156de2010-04-06 13:10:27 +0000179#if (defined(__APPLE__) && defined(__MACH__)) || \
180 defined(__FreeBSD__) || defined(__OpenBSD__)
181#define USING_BSD_ABI
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000182#endif
183
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184// -----------------------------------------------------------------------------
185// Constants
186
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187const int KB = 1024;
188const int MB = KB * KB;
189const int GB = KB * KB * KB;
190const int kMaxInt = 0x7FFFFFFF;
191const int kMinInt = -kMaxInt - 1;
192
ager@chromium.org5ec48922009-05-05 07:25:34 +0000193const uint32_t kMaxUInt32 = 0xFFFFFFFFu;
194
ager@chromium.orge2902be2009-06-08 12:21:35 +0000195const int kCharSize = sizeof(char); // NOLINT
196const int kShortSize = sizeof(short); // NOLINT
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000197const int kIntSize = sizeof(int); // NOLINT
ager@chromium.orge2902be2009-06-08 12:21:35 +0000198const int kDoubleSize = sizeof(double); // NOLINT
ager@chromium.orge2902be2009-06-08 12:21:35 +0000199const int kIntptrSize = sizeof(intptr_t); // NOLINT
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000200const int kPointerSize = sizeof(void*); // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000202const int kDoubleSizeLog2 = 3;
203
ager@chromium.org9085a012009-05-11 19:22:57 +0000204#if V8_HOST_ARCH_64_BIT
ager@chromium.org5ec48922009-05-05 07:25:34 +0000205const int kPointerSizeLog2 = 3;
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000206const intptr_t kIntptrSignBit = V8_INT64_C(0x8000000000000000);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000207const uintptr_t kUintptrAllBitsSet = V8_UINT64_C(0xFFFFFFFFFFFFFFFF);
ager@chromium.org5ec48922009-05-05 07:25:34 +0000208#else
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209const int kPointerSizeLog2 = 2;
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000210const intptr_t kIntptrSignBit = 0x80000000;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000211const uintptr_t kUintptrAllBitsSet = 0xFFFFFFFFu;
ager@chromium.org5ec48922009-05-05 07:25:34 +0000212#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000214const int kBitsPerByte = 8;
215const int kBitsPerByteLog2 = 3;
216const int kBitsPerPointer = kPointerSize * kBitsPerByte;
217const int kBitsPerInt = kIntSize * kBitsPerByte;
218
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000219// IEEE 754 single precision floating point number bit layout.
220const uint32_t kBinary32SignMask = 0x80000000u;
221const uint32_t kBinary32ExponentMask = 0x7f800000u;
222const uint32_t kBinary32MantissaMask = 0x007fffffu;
223const int kBinary32ExponentBias = 127;
224const int kBinary32MaxExponent = 0xFE;
225const int kBinary32MinExponent = 0x01;
226const int kBinary32MantissaBits = 23;
227const int kBinary32ExponentShift = 23;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000228
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000229// ASCII/UC16 constants
230// Code-point values in Unicode 4.0 are 21 bits wide.
231typedef uint16_t uc16;
232typedef int32_t uc32;
233const int kASCIISize = kCharSize;
234const int kUC16Size = sizeof(uc16); // NOLINT
235const uc32 kMaxAsciiCharCode = 0x7f;
236const uint32_t kMaxAsciiCharCodeU = 0x7fu;
237
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000238
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239// The expression OFFSET_OF(type, field) computes the byte-offset
240// of the specified field relative to the containing type. This
241// corresponds to 'offsetof' (in stddef.h), except that it doesn't
242// use 0 or NULL, which causes a problem with the compiler warnings
243// we have enabled (which is also why 'offsetof' doesn't seem to work).
244// Here we simply use the non-zero value 4, which seems to work.
245#define OFFSET_OF(type, field) \
246 (reinterpret_cast<intptr_t>(&(reinterpret_cast<type*>(4)->field)) - 4)
247
248
249// The expression ARRAY_SIZE(a) is a compile-time constant of type
250// size_t which represents the number of elements of the given
251// array. You should only use ARRAY_SIZE on statically allocated
252// arrays.
253#define ARRAY_SIZE(a) \
254 ((sizeof(a) / sizeof(*(a))) / \
255 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
256
257
258// The USE(x) template is used to silence C++ compiler warnings
259// issued for (yet) unused variables (typically parameters).
260template <typename T>
261static inline void USE(T) { }
262
263
264// FUNCTION_ADDR(f) gets the address of a C function f.
265#define FUNCTION_ADDR(f) \
266 (reinterpret_cast<v8::internal::Address>(reinterpret_cast<intptr_t>(f)))
267
268
269// FUNCTION_CAST<F>(addr) casts an address into a function
270// of type F. Used to invoke generated code from within C.
271template <typename F>
272F FUNCTION_CAST(Address addr) {
273 return reinterpret_cast<F>(reinterpret_cast<intptr_t>(addr));
274}
275
276
277// A macro to disallow the evil copy constructor and operator= functions
278// This should be used in the private: declarations for a class
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000279#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280 TypeName(const TypeName&); \
281 void operator=(const TypeName&)
282
283
284// A macro to disallow all the implicit constructors, namely the
285// default constructor, copy constructor and operator= functions.
286//
287// This should be used in the private: declarations for a class
288// that wants to prevent anyone from instantiating it. This is
289// especially useful for classes containing only static methods.
290#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
291 TypeName(); \
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000292 DISALLOW_COPY_AND_ASSIGN(TypeName)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293
294
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000295// Define used for helping GCC to make better inlining. Don't bother for debug
iposva@chromium.org245aa852009-02-10 00:49:54 +0000296// builds. On GCC 3.4.5 using __attribute__((always_inline)) causes compilation
297// errors in debug build.
298#if defined(__GNUC__) && !defined(DEBUG)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299#if (__GNUC__ >= 4)
300#define INLINE(header) inline header __attribute__((always_inline))
sgjesse@chromium.org76ae6992010-08-05 15:54:25 +0000301#define NO_INLINE(header) header __attribute__((noinline))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000302#else
303#define INLINE(header) inline __attribute__((always_inline)) header
sgjesse@chromium.org76ae6992010-08-05 15:54:25 +0000304#define NO_INLINE(header) __attribute__((noinline)) header
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305#endif
306#else
307#define INLINE(header) inline header
sgjesse@chromium.org76ae6992010-08-05 15:54:25 +0000308#define NO_INLINE(header) header
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309#endif
310
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000311
312#if defined(__GNUC__) && __GNUC__ >= 4
313#define MUST_USE_RESULT __attribute__ ((warn_unused_result))
314#else
315#define MUST_USE_RESULT
316#endif
317
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000318// -----------------------------------------------------------------------------
319// Forward declarations for frequently used classes
320// (sorted alphabetically)
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000321
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000322class FreeStoreAllocationPolicy;
323template <typename T, class P = FreeStoreAllocationPolicy> class List;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000324
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325} } // namespace v8::internal
326
327#endif // V8_GLOBALS_H_