1// Copyright 2010 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
31#include "../include/v8stdint.h"
32
33namespace v8 {
34namespace internal {
35
36// 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
51// 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
57#elif defined(__MIPSEL__)
58#define V8_HOST_ARCH_MIPS 1
59#define V8_HOST_ARCH_32_BIT 1
60#else
61#error Host architecture was not detected as supported by v8
62#endif
63
64// 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
75#elif defined(__MIPSEL__)
76#define V8_TARGET_ARCH_MIPS 1
77#else
78#error Target architecture was not detected as supported by v8
79#endif
80#endif
81
82// 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
98// 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
110// Define unaligned read for the target architectures supporting it.
111#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
114// 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
120#elif V8_TARGET_ARCH_MIPS
121#else
122#error Target architecture is not supported by v8
123#endif
124
125// 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
137#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
145// 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
149#ifdef _MSC_VER
150#define V8_UINT64_C(x)  (x ## UI64)
151#define V8_INT64_C(x)   (x ## I64)
152#define V8_INTPTR_C(x)  (x ## I64)
153#define V8_PTR_PREFIX "ll"
154#else  // _MSC_VER
155#define V8_UINT64_C(x)  (x ## UL)
156#define V8_INT64_C(x)   (x ## L)
157#define V8_INTPTR_C(x)  (x ## L)
158#define V8_PTR_PREFIX "l"
159#endif  // _MSC_VER
160#else  // V8_HOST_ARCH_64_BIT
161#define V8_INTPTR_C(x)  (x)
162#define V8_PTR_PREFIX ""
163#endif  // V8_HOST_ARCH_64_BIT
164
165// 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
170#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
178
179#if (defined(__APPLE__) && defined(__MACH__)) || \
180    defined(__FreeBSD__) || defined(__OpenBSD__)
181#define USING_BSD_ABI
182#endif
183
184// -----------------------------------------------------------------------------
185// Constants
186
187const int KB = 1024;
188const int MB = KB * KB;
189const int GB = KB * KB * KB;
190const int kMaxInt = 0x7FFFFFFF;
191const int kMinInt = -kMaxInt - 1;
192
193const uint32_t kMaxUInt32 = 0xFFFFFFFFu;
194
195const int kCharSize     = sizeof(char);      // NOLINT
196const int kShortSize    = sizeof(short);     // NOLINT
197const int kIntSize      = sizeof(int);       // NOLINT
198const int kDoubleSize   = sizeof(double);    // NOLINT
199const int kIntptrSize   = sizeof(intptr_t);  // NOLINT
200const int kPointerSize  = sizeof(void*);     // NOLINT
201
202#if V8_HOST_ARCH_64_BIT
203const int kPointerSizeLog2 = 3;
204const intptr_t kIntptrSignBit = V8_INT64_C(0x8000000000000000);
205const uintptr_t kUintptrAllBitsSet = V8_UINT64_C(0xFFFFFFFFFFFFFFFF);
206#else
207const int kPointerSizeLog2 = 2;
208const intptr_t kIntptrSignBit = 0x80000000;
209const uintptr_t kUintptrAllBitsSet = 0xFFFFFFFFu;
210#endif
211
212const int kBitsPerByte = 8;
213const int kBitsPerByteLog2 = 3;
214const int kBitsPerPointer = kPointerSize * kBitsPerByte;
215const int kBitsPerInt = kIntSize * kBitsPerByte;
216
217// IEEE 754 single precision floating point number bit layout.
218const uint32_t kBinary32SignMask = 0x80000000u;
219const uint32_t kBinary32ExponentMask = 0x7f800000u;
220const uint32_t kBinary32MantissaMask = 0x007fffffu;
221const int kBinary32ExponentBias = 127;
222const int kBinary32MaxExponent  = 0xFE;
223const int kBinary32MinExponent  = 0x01;
224const int kBinary32MantissaBits = 23;
225const int kBinary32ExponentShift = 23;
226
227// ASCII/UC16 constants
228// Code-point values in Unicode 4.0 are 21 bits wide.
229typedef uint16_t uc16;
230typedef int32_t uc32;
231const int kASCIISize    = kCharSize;
232const int kUC16Size     = sizeof(uc16);      // NOLINT
233const uc32 kMaxAsciiCharCode = 0x7f;
234const uint32_t kMaxAsciiCharCodeU = 0x7fu;
235
236
237// The expression OFFSET_OF(type, field) computes the byte-offset
238// of the specified field relative to the containing type. This
239// corresponds to 'offsetof' (in stddef.h), except that it doesn't
240// use 0 or NULL, which causes a problem with the compiler warnings
241// we have enabled (which is also why 'offsetof' doesn't seem to work).
242// Here we simply use the non-zero value 4, which seems to work.
243#define OFFSET_OF(type, field)                                          \
244  (reinterpret_cast<intptr_t>(&(reinterpret_cast<type*>(4)->field)) - 4)
245
246
247// The expression ARRAY_SIZE(a) is a compile-time constant of type
248// size_t which represents the number of elements of the given
249// array. You should only use ARRAY_SIZE on statically allocated
250// arrays.
251#define ARRAY_SIZE(a)                                   \
252  ((sizeof(a) / sizeof(*(a))) /                         \
253  static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
254
255
256// The USE(x) template is used to silence C++ compiler warnings
257// issued for (yet) unused variables (typically parameters).
258template <typename T>
259static inline void USE(T) { }
260
261
262// FUNCTION_ADDR(f) gets the address of a C function f.
263#define FUNCTION_ADDR(f)                                        \
264  (reinterpret_cast<v8::internal::Address>(reinterpret_cast<intptr_t>(f)))
265
266
267// FUNCTION_CAST<F>(addr) casts an address into a function
268// of type F. Used to invoke generated code from within C.
269template <typename F>
270F FUNCTION_CAST(Address addr) {
271  return reinterpret_cast<F>(reinterpret_cast<intptr_t>(addr));
272}
273
274
275// A macro to disallow the evil copy constructor and operator= functions
276// This should be used in the private: declarations for a class
277#define DISALLOW_COPY_AND_ASSIGN(TypeName)      \
278  TypeName(const TypeName&);                    \
279  void operator=(const TypeName&)
280
281
282// A macro to disallow all the implicit constructors, namely the
283// default constructor, copy constructor and operator= functions.
284//
285// This should be used in the private: declarations for a class
286// that wants to prevent anyone from instantiating it. This is
287// especially useful for classes containing only static methods.
288#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
289  TypeName();                                    \
290  DISALLOW_COPY_AND_ASSIGN(TypeName)
291
292
293// Define used for helping GCC to make better inlining. Don't bother for debug
294// builds. On GCC 3.4.5 using __attribute__((always_inline)) causes compilation
295// errors in debug build.
296#if defined(__GNUC__) && !defined(DEBUG)
297#if (__GNUC__ >= 4)
298#define INLINE(header) inline header  __attribute__((always_inline))
299#define NO_INLINE(header) header __attribute__((noinline))
300#else
301#define INLINE(header) inline __attribute__((always_inline)) header
302#define NO_INLINE(header) __attribute__((noinline)) header
303#endif
304#else
305#define INLINE(header) inline header
306#define NO_INLINE(header) header
307#endif
308
309
310#if defined(__GNUC__) && __GNUC__ >= 4
311#define MUST_USE_RESULT __attribute__ ((warn_unused_result))
312#else
313#define MUST_USE_RESULT
314#endif
315
316// -----------------------------------------------------------------------------
317// Forward declarations for frequently used classes
318// (sorted alphabetically)
319
320class FreeStoreAllocationPolicy;
321template <typename T, class P = FreeStoreAllocationPolicy> class List;
322
323} }  // namespace v8::internal
324
325#endif  // V8_GLOBALS_H_
326