ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 28 | #ifndef V8_GLOBALS_H_ |
| 29 | #define V8_GLOBALS_H_ |
| 30 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 31 | // ----------------------------------------------------------------------------- |
| 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 |
| 37 | typedef signed char int8_t; |
| 38 | typedef unsigned char uint8_t; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 39 | typedef short int16_t; // NOLINT |
| 40 | typedef unsigned short uint16_t; // NOLINT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | typedef int int32_t; |
| 42 | typedef unsigned int uint32_t; |
| 43 | typedef __int64 int64_t; |
| 44 | typedef unsigned __int64 uint64_t; |
| 45 | #else |
| 46 | #include <stdint.h> // for intptr_t |
| 47 | #endif |
| 48 | |
| 49 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | namespace 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 64 | #ifdef USE_MYBOOL |
| 65 | typedef unsigned int __my_bool__; |
| 66 | #define bool __my_bool__ // use 'indirection' to avoid name clashes |
| 67 | #endif |
| 68 | |
| 69 | typedef uint8_t byte; |
| 70 | typedef byte* Address; |
| 71 | |
| 72 | // Code-point values in Unicode 4.0 are 21 bits wide. |
| 73 | typedef uint16_t uc16; |
| 74 | typedef signed int uc32; |
| 75 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 76 | #ifndef ARM |
| 77 | #define CAN_READ_UNALIGNED 1 |
| 78 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 79 | |
| 80 | // ----------------------------------------------------------------------------- |
| 81 | // Constants |
| 82 | |
| 83 | #ifdef DEBUG |
| 84 | const bool kDebug = true; |
| 85 | #else |
| 86 | const bool kDebug = false; |
| 87 | #endif // DEBUG |
| 88 | |
| 89 | const int KB = 1024; |
| 90 | const int MB = KB * KB; |
| 91 | const int GB = KB * KB * KB; |
| 92 | const int kMaxInt = 0x7FFFFFFF; |
| 93 | const int kMinInt = -kMaxInt - 1; |
| 94 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 95 | const int kCharSize = sizeof(char); // NOLINT |
| 96 | const int kShortSize = sizeof(short); // NOLINT |
| 97 | const int kIntSize = sizeof(int); // NOLINT |
| 98 | const int kDoubleSize = sizeof(double); // NOLINT |
| 99 | const int kPointerSize = sizeof(void*); // NOLINT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 100 | |
| 101 | const int kPointerSizeLog2 = 2; |
| 102 | |
| 103 | const int kObjectAlignmentBits = 2; |
| 104 | const int kObjectAlignmentMask = (1 << kObjectAlignmentBits) - 1; |
| 105 | const int kObjectAlignment = 1 << kObjectAlignmentBits; |
| 106 | |
| 107 | |
| 108 | // Tag information for HeapObject. |
| 109 | const int kHeapObjectTag = 1; |
| 110 | const int kHeapObjectTagSize = 2; |
| 111 | const int kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1; |
| 112 | |
| 113 | |
| 114 | // Tag information for Smi. |
| 115 | const int kSmiTag = 0; |
| 116 | const int kSmiTagSize = 1; |
| 117 | const int kSmiTagMask = (1 << kSmiTagSize) - 1; |
| 118 | |
| 119 | |
| 120 | // Tag information for Failure. |
| 121 | const int kFailureTag = 3; |
| 122 | const int kFailureTagSize = 2; |
| 123 | const int kFailureTagMask = (1 << kFailureTagSize) - 1; |
| 124 | |
| 125 | |
| 126 | const int kBitsPerByte = 8; |
| 127 | const int kBitsPerByteLog2 = 3; |
| 128 | const int kBitsPerPointer = kPointerSize * kBitsPerByte; |
| 129 | const int kBitsPerInt = kIntSize * kBitsPerByte; |
| 130 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 131 | |
| 132 | // Zap-value: The value used for zapping dead objects. Should be a recognizable |
| 133 | // illegal heap object pointer. |
| 134 | const Address kZapValue = reinterpret_cast<Address>(0xdeadbeed); |
| 135 | const Address kHandleZapValue = reinterpret_cast<Address>(0xbaddead); |
| 136 | const Address kFromSpaceZapValue = reinterpret_cast<Address>(0xbeefdad); |
| 137 | |
| 138 | // ----------------------------------------------------------------------------- |
| 139 | // Forward declarations for frequently used classes |
| 140 | // (sorted alphabetically) |
| 141 | |
| 142 | class AccessorInfo; |
| 143 | class Allocation; |
| 144 | class Assembler; |
| 145 | class BreakableStatement; |
| 146 | class Code; |
| 147 | class CodeGenerator; |
| 148 | class CodeStub; |
| 149 | class Context; |
| 150 | class Debug; |
| 151 | class Debugger; |
| 152 | class DebugInfo; |
| 153 | class Descriptor; |
| 154 | class DescriptorArray; |
| 155 | class Expression; |
| 156 | class ExternalReference; |
| 157 | class FixedArray; |
| 158 | class FunctionEntry; |
| 159 | class FunctionLiteral; |
| 160 | class FunctionTemplateInfo; |
| 161 | class Dictionary; |
| 162 | class FreeStoreAllocationPolicy; |
| 163 | template <typename T> class Handle; |
| 164 | class Heap; |
| 165 | class HeapObject; |
| 166 | class IC; |
| 167 | class InterceptorInfo; |
| 168 | class IterationStatement; |
| 169 | class JSArray; |
| 170 | class JSFunction; |
| 171 | class JSObject; |
| 172 | class LabelCollector; |
| 173 | class LargeObjectSpace; |
| 174 | template <typename T, class P = FreeStoreAllocationPolicy> class List; |
| 175 | class LookupResult; |
| 176 | class MacroAssembler; |
| 177 | class Map; |
| 178 | class MapSpace; |
| 179 | class MarkCompactCollector; |
| 180 | class NewSpace; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 181 | class NodeVisitor; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 182 | class Object; |
| 183 | class OldSpace; |
| 184 | class Property; |
| 185 | class Proxy; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 186 | class RegExpNode; |
| 187 | struct RegExpParseResult; |
| 188 | class RegExpTree; |
| 189 | class RegExpCompiler; |
| 190 | class RegExpVisitor; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 191 | class Scope; |
| 192 | template<class Allocator = FreeStoreAllocationPolicy> class ScopeInfo; |
| 193 | class Script; |
| 194 | class Slot; |
| 195 | class Smi; |
| 196 | class Statement; |
| 197 | class String; |
| 198 | class Struct; |
| 199 | class SwitchStatement; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 200 | class AstVisitor; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 201 | class Variable; |
| 202 | class VariableProxy; |
| 203 | class RelocInfo; |
| 204 | class Deserializer; |
| 205 | class MessageLocation; |
| 206 | class ObjectGroup; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 207 | class TickSample; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 208 | class VirtualMemory; |
| 209 | class Mutex; |
| 210 | |
| 211 | typedef bool (*WeakSlotCallback)(Object** pointer); |
| 212 | |
| 213 | // ----------------------------------------------------------------------------- |
| 214 | // Miscellaneous |
| 215 | |
| 216 | // NOTE: SpaceIterator depends on AllocationSpace enumeration values being |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 217 | // consecutive. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 218 | enum AllocationSpace { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 219 | 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.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 225 | FIRST_SPACE = NEW_SPACE, |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 226 | LAST_SPACE = LO_SPACE // <= 5 (see kSpaceBits and kLOSpacePointer) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 227 | }; |
| 228 | const int kSpaceTagSize = 3; |
| 229 | const int kSpaceTagMask = (1 << kSpaceTagSize) - 1; |
| 230 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 231 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 232 | // 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). |
| 236 | enum PretenureFlag { NOT_TENURED, TENURED }; |
| 237 | |
| 238 | enum GarbageCollector { SCAVENGER, MARK_COMPACTOR }; |
| 239 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 240 | enum Executability { NOT_EXECUTABLE, EXECUTABLE }; |
| 241 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 242 | |
| 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 | |
| 257 | struct 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. |
| 268 | typedef void (*ObjectSlotCallback)(HeapObject** pointer); |
| 269 | |
| 270 | |
| 271 | // Callback function used for iterating objects in heap spaces, |
| 272 | // for example, scanning heap objects. |
| 273 | typedef 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. |
| 279 | typedef 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. |
| 284 | typedef void (*InlineCacheCallback)(Code* code, Address ic); |
| 285 | |
| 286 | |
| 287 | // State for inline cache call sites. Aliased as IC::State. |
| 288 | enum InlineCacheState { |
| 289 | // Has never been executed. |
| 290 | UNINITIALIZED, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 291 | // Has never been executed, but is in a loop. |
| 292 | UNINITIALIZED_IN_LOOP, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 293 | // 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.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 308 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 311 | enum PropertyType { |
| 312 | NORMAL = 0, // only in slow mode |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 313 | FIELD = 1, // only in fast mode |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 314 | CONSTANT_FUNCTION = 2, // only in fast mode |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 315 | CALLBACKS = 3, |
| 316 | INTERCEPTOR = 4, // only in lookup results, not in descriptors. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 317 | MAP_TRANSITION = 5, // only in fast mode |
| 318 | CONSTANT_TRANSITION = 6, // only in fast mode |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 319 | 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.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | |
| 325 | // Whether to remove map transitions and constant transitions from a |
| 326 | // DescriptorArray. |
| 327 | enum TransitionFlag { |
| 328 | REMOVE_TRANSITIONS, |
| 329 | KEEP_TRANSITIONS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | |
| 333 | // Union used for fast testing of specific double values. |
| 334 | union DoubleRepresentation { |
| 335 | double value; |
| 336 | int64_t bits; |
| 337 | DoubleRepresentation(double x) { value = x; } |
| 338 | }; |
| 339 | |
| 340 | |
| 341 | // AccessorCallback |
| 342 | struct 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 | |
| 362 | enum 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 389 | // 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). |
| 410 | template <typename T> |
| 411 | static 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. |
| 421 | template <typename F> |
| 422 | F 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.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 429 | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 430 | 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.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 442 | DISALLOW_COPY_AND_ASSIGN(TypeName) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 443 | |
| 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.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 476 | // 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. |
| 500 | template <class Dest, class Source> |
| 501 | inline 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 512 | } } // namespace v8::internal |
| 513 | |
| 514 | #endif // V8_GLOBALS_H_ |