1 // Copyright 2012 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_V8GLOBALS_H_ 29 #define V8_V8GLOBALS_H_ 30 31 #include "globals.h" 32 #include "checks.h" 33 34 namespace v8 { 35 namespace internal { 36 37 // This file contains constants and global declarations related to the 38 // V8 system. 39 40 // Mask for the sign bit in a smi. 41 const intptr_t kSmiSignMask = kIntptrSignBit; 42 43 const int kObjectAlignmentBits = kPointerSizeLog2; 44 const intptr_t kObjectAlignment = 1 << kObjectAlignmentBits; 45 const intptr_t kObjectAlignmentMask = kObjectAlignment - 1; 46 47 // Desired alignment for pointers. 48 const intptr_t kPointerAlignment = (1 << kPointerSizeLog2); 49 const intptr_t kPointerAlignmentMask = kPointerAlignment - 1; 50 51 // Desired alignment for maps. 52 #if V8_HOST_ARCH_64_BIT 53 const intptr_t kMapAlignmentBits = kObjectAlignmentBits; 54 #else 55 const intptr_t kMapAlignmentBits = kObjectAlignmentBits + 3; 56 #endif 57 const intptr_t kMapAlignment = (1 << kMapAlignmentBits); 58 const intptr_t kMapAlignmentMask = kMapAlignment - 1; 59 60 // Desired alignment for generated code is 32 bytes (to improve cache line 61 // utilization). 62 const int kCodeAlignmentBits = 5; 63 const intptr_t kCodeAlignment = 1 << kCodeAlignmentBits; 64 const intptr_t kCodeAlignmentMask = kCodeAlignment - 1; 65 66 // Tag information for Failure. 67 const int kFailureTag = 3; 68 const int kFailureTagSize = 2; 69 const intptr_t kFailureTagMask = (1 << kFailureTagSize) - 1; 70 71 72 // Zap-value: The value used for zapping dead objects. 73 // Should be a recognizable hex value tagged as a failure. 74 #ifdef V8_HOST_ARCH_64_BIT 75 const Address kZapValue = 76 reinterpret_cast<Address>(V8_UINT64_C(0xdeadbeedbeadbeef)); 77 const Address kHandleZapValue = 78 reinterpret_cast<Address>(V8_UINT64_C(0x1baddead0baddeaf)); 79 const Address kFromSpaceZapValue = 80 reinterpret_cast<Address>(V8_UINT64_C(0x1beefdad0beefdaf)); 81 const uint64_t kDebugZapValue = V8_UINT64_C(0xbadbaddbbadbaddb); 82 const uint64_t kSlotsZapValue = V8_UINT64_C(0xbeefdeadbeefdeef); 83 const uint64_t kFreeListZapValue = 0xfeed1eaffeed1eaf; 84 #else 85 const Address kZapValue = reinterpret_cast<Address>(0xdeadbeef); 86 const Address kHandleZapValue = reinterpret_cast<Address>(0xbaddeaf); 87 const Address kFromSpaceZapValue = reinterpret_cast<Address>(0xbeefdaf); 88 const uint32_t kSlotsZapValue = 0xbeefdeef; 89 const uint32_t kDebugZapValue = 0xbadbaddb; 90 const uint32_t kFreeListZapValue = 0xfeed1eaf; 91 #endif 92 93 94 // Number of bits to represent the page size for paged spaces. The value of 20 95 // gives 1Mb bytes per page. 96 const int kPageSizeBits = 20; 97 98 // On Intel architecture, cache line size is 64 bytes. 99 // On ARM it may be less (32 bytes), but as far this constant is 100 // used for aligning data, it doesn't hurt to align on a greater value. 101 const int kProcessorCacheLineSize = 64; 102 103 // Constants relevant to double precision floating point numbers. 104 // If looking only at the top 32 bits, the QNaN mask is bits 19 to 30. 105 const uint32_t kQuietNaNHighBitsMask = 0xfff << (51 - 32); 106 107 108 // ----------------------------------------------------------------------------- 109 // Forward declarations for frequently used classes 110 111 class AccessorInfo; 112 class Allocation; 113 class Arguments; 114 class Assembler; 115 class AssertNoAllocation; 116 class Code; 117 class CodeGenerator; 118 class CodeStub; 119 class Context; 120 class Debug; 121 class Debugger; 122 class DebugInfo; 123 class Descriptor; 124 class DescriptorArray; 125 class ExternalReference; 126 class FixedArray; 127 class FunctionTemplateInfo; 128 class MemoryChunk; 129 class SeededNumberDictionary; 130 class UnseededNumberDictionary; 131 class StringDictionary; 132 template <typename T> class Handle; 133 class Heap; 134 class HeapObject; 135 class IC; 136 class InterceptorInfo; 137 class JSArray; 138 class JSFunction; 139 class JSObject; 140 class LargeObjectSpace; 141 class LookupResult; 142 class MacroAssembler; 143 class Map; 144 class MapSpace; 145 class MarkCompactCollector; 146 class NewSpace; 147 class Object; 148 class MaybeObject; 149 class OldSpace; 150 class Foreign; 151 class Scope; 152 class ScopeInfo; 153 class Script; 154 class Smi; 155 template <typename Config, class Allocator = FreeStoreAllocationPolicy> 156 class SplayTree; 157 class String; 158 class Struct; 159 class Variable; 160 class RelocInfo; 161 class Deserializer; 162 class MessageLocation; 163 class ObjectGroup; 164 class TickSample; 165 class VirtualMemory; 166 class Mutex; 167 168 typedef bool (*WeakSlotCallback)(Object** pointer); 169 170 typedef bool (*WeakSlotCallbackWithHeap)(Heap* heap, Object** pointer); 171 172 // ----------------------------------------------------------------------------- 173 // Miscellaneous 174 175 // NOTE: SpaceIterator depends on AllocationSpace enumeration values being 176 // consecutive. 177 enum AllocationSpace { 178 NEW_SPACE, // Semispaces collected with copying collector. 179 OLD_POINTER_SPACE, // May contain pointers to new space. 180 OLD_DATA_SPACE, // Must not have pointers to new space. 181 CODE_SPACE, // No pointers to new space, marked executable. 182 MAP_SPACE, // Only and all map objects. 183 CELL_SPACE, // Only and all cell objects. 184 LO_SPACE, // Promoted large objects. 185 186 FIRST_SPACE = NEW_SPACE, 187 LAST_SPACE = LO_SPACE, 188 FIRST_PAGED_SPACE = OLD_POINTER_SPACE, 189 LAST_PAGED_SPACE = CELL_SPACE 190 }; 191 const int kSpaceTagSize = 3; 192 const int kSpaceTagMask = (1 << kSpaceTagSize) - 1; 193 194 195 // A flag that indicates whether objects should be pretenured when 196 // allocated (allocated directly into the old generation) or not 197 // (allocated in the young generation if the object size and type 198 // allows). 199 enum PretenureFlag { NOT_TENURED, TENURED }; 200 201 enum GarbageCollector { SCAVENGER, MARK_COMPACTOR }; 202 203 enum Executability { NOT_EXECUTABLE, EXECUTABLE }; 204 205 enum VisitMode { 206 VISIT_ALL, 207 VISIT_ALL_IN_SCAVENGE, 208 VISIT_ALL_IN_SWEEP_NEWSPACE, 209 VISIT_ONLY_STRONG 210 }; 211 212 // Flag indicating whether code is built into the VM (one of the natives files). 213 enum NativesFlag { NOT_NATIVES_CODE, NATIVES_CODE }; 214 215 216 // A CodeDesc describes a buffer holding instructions and relocation 217 // information. The instructions start at the beginning of the buffer 218 // and grow forward, the relocation information starts at the end of 219 // the buffer and grows backward. 220 // 221 // |<--------------- buffer_size ---------------->| 222 // |<-- instr_size -->| |<-- reloc_size -->| 223 // +==================+========+==================+ 224 // | instructions | free | reloc info | 225 // +==================+========+==================+ 226 // ^ 227 // | 228 // buffer 229 230 struct CodeDesc { 231 byte* buffer; 232 int buffer_size; 233 int instr_size; 234 int reloc_size; 235 Assembler* origin; 236 }; 237 238 239 // Callback function used for iterating objects in heap spaces, 240 // for example, scanning heap objects. 241 typedef int (*HeapObjectCallback)(HeapObject* obj); 242 243 244 // Callback function used for checking constraints when copying/relocating 245 // objects. Returns true if an object can be copied/relocated from its 246 // old_addr to a new_addr. 247 typedef bool (*ConstraintCallback)(Address new_addr, Address old_addr); 248 249 250 // Callback function on inline caches, used for iterating over inline caches 251 // in compiled code. 252 typedef void (*InlineCacheCallback)(Code* code, Address ic); 253 254 255 // State for inline cache call sites. Aliased as IC::State. 256 enum InlineCacheState { 257 // Has never been executed. 258 UNINITIALIZED, 259 // Has been executed but monomorhic state has been delayed. 260 PREMONOMORPHIC, 261 // Has been executed and only one receiver type has been seen. 262 MONOMORPHIC, 263 // Like MONOMORPHIC but check failed due to prototype. 264 MONOMORPHIC_PROTOTYPE_FAILURE, 265 // Multiple receiver types have been seen. 266 MEGAMORPHIC, 267 // Special states for debug break or step in prepare stubs. 268 DEBUG_BREAK, 269 DEBUG_PREPARE_STEP_IN 270 }; 271 272 273 enum CheckType { 274 RECEIVER_MAP_CHECK, 275 STRING_CHECK, 276 NUMBER_CHECK, 277 BOOLEAN_CHECK 278 }; 279 280 281 enum CallFunctionFlags { 282 NO_CALL_FUNCTION_FLAGS = 0, 283 // Receiver might implicitly be the global objects. If it is, the 284 // hole is passed to the call function stub. 285 RECEIVER_MIGHT_BE_IMPLICIT = 1 << 0, 286 // The call target is cached in the instruction stream. 287 RECORD_CALL_TARGET = 1 << 1 288 }; 289 290 291 enum InlineCacheHolderFlag { 292 OWN_MAP, // For fast properties objects. 293 PROTOTYPE_MAP // For slow properties objects (except GlobalObjects). 294 }; 295 296 297 // The Store Buffer (GC). 298 typedef enum { 299 kStoreBufferFullEvent, 300 kStoreBufferStartScanningPagesEvent, 301 kStoreBufferScanningPageEvent 302 } StoreBufferEvent; 303 304 305 typedef void (*StoreBufferCallback)(Heap* heap, 306 MemoryChunk* page, 307 StoreBufferEvent event); 308 309 310 // Whether to remove map transitions and constant transitions from a 311 // DescriptorArray. 312 enum TransitionFlag { 313 REMOVE_TRANSITIONS, 314 KEEP_TRANSITIONS 315 }; 316 317 318 // Union used for fast testing of specific double values. 319 union DoubleRepresentation { 320 double value; 321 int64_t bits; DoubleRepresentation(double x)322 DoubleRepresentation(double x) { value = x; } 323 }; 324 325 326 // Union used for customized checking of the IEEE double types 327 // inlined within v8 runtime, rather than going to the underlying 328 // platform headers and libraries 329 union IeeeDoubleLittleEndianArchType { 330 double d; 331 struct { 332 unsigned int man_low :32; 333 unsigned int man_high :20; 334 unsigned int exp :11; 335 unsigned int sign :1; 336 } bits; 337 }; 338 339 340 union IeeeDoubleBigEndianArchType { 341 double d; 342 struct { 343 unsigned int sign :1; 344 unsigned int exp :11; 345 unsigned int man_high :20; 346 unsigned int man_low :32; 347 } bits; 348 }; 349 350 351 // AccessorCallback 352 struct AccessorDescriptor { 353 MaybeObject* (*getter)(Object* object, void* data); 354 MaybeObject* (*setter)(JSObject* object, Object* value, void* data); 355 void* data; 356 }; 357 358 359 // Logging and profiling. A StateTag represents a possible state of 360 // the VM. The logger maintains a stack of these. Creating a VMState 361 // object enters a state by pushing on the stack, and destroying a 362 // VMState object leaves a state by popping the current state from the 363 // stack. 364 365 #define STATE_TAG_LIST(V) \ 366 V(JS) \ 367 V(GC) \ 368 V(COMPILER) \ 369 V(OTHER) \ 370 V(EXTERNAL) 371 372 enum StateTag { 373 #define DEF_STATE_TAG(name) name, 374 STATE_TAG_LIST(DEF_STATE_TAG) 375 #undef DEF_STATE_TAG 376 // Pseudo-types. 377 state_tag_count 378 }; 379 380 381 // ----------------------------------------------------------------------------- 382 // Macros 383 384 // Testers for test. 385 386 #define HAS_SMI_TAG(value) \ 387 ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag) 388 389 #define HAS_FAILURE_TAG(value) \ 390 ((reinterpret_cast<intptr_t>(value) & kFailureTagMask) == kFailureTag) 391 392 // OBJECT_POINTER_ALIGN returns the value aligned as a HeapObject pointer 393 #define OBJECT_POINTER_ALIGN(value) \ 394 (((value) + kObjectAlignmentMask) & ~kObjectAlignmentMask) 395 396 // POINTER_SIZE_ALIGN returns the value aligned as a pointer. 397 #define POINTER_SIZE_ALIGN(value) \ 398 (((value) + kPointerAlignmentMask) & ~kPointerAlignmentMask) 399 400 // MAP_POINTER_ALIGN returns the value aligned as a map pointer. 401 #define MAP_POINTER_ALIGN(value) \ 402 (((value) + kMapAlignmentMask) & ~kMapAlignmentMask) 403 404 // CODE_POINTER_ALIGN returns the value aligned as a generated code segment. 405 #define CODE_POINTER_ALIGN(value) \ 406 (((value) + kCodeAlignmentMask) & ~kCodeAlignmentMask) 407 408 // Support for tracking C++ memory allocation. Insert TRACK_MEMORY("Fisk") 409 // inside a C++ class and new and delete will be overloaded so logging is 410 // performed. 411 // This file (globals.h) is included before log.h, so we use direct calls to 412 // the Logger rather than the LOG macro. 413 #ifdef DEBUG 414 #define TRACK_MEMORY(name) \ 415 void* operator new(size_t size) { \ 416 void* result = ::operator new(size); \ 417 Logger::NewEventStatic(name, result, size); \ 418 return result; \ 419 } \ 420 void operator delete(void* object) { \ 421 Logger::DeleteEventStatic(name, object); \ 422 ::operator delete(object); \ 423 } 424 #else 425 #define TRACK_MEMORY(name) 426 #endif 427 428 429 // Feature flags bit positions. They are mostly based on the CPUID spec. 430 // (We assign CPUID itself to one of the currently reserved bits -- 431 // feel free to change this if needed.) 432 // On X86/X64, values below 32 are bits in EDX, values above 32 are bits in ECX. 433 enum CpuFeature { SSE4_1 = 32 + 19, // x86 434 SSE3 = 32 + 0, // x86 435 SSE2 = 26, // x86 436 CMOV = 15, // x86 437 RDTSC = 4, // x86 438 CPUID = 10, // x86 439 VFP3 = 1, // ARM 440 ARMv7 = 2, // ARM 441 SAHF = 0, // x86 442 FPU = 1}; // MIPS 443 444 445 // Used to specify if a macro instruction must perform a smi check on tagged 446 // values. 447 enum SmiCheckType { 448 DONT_DO_SMI_CHECK, 449 DO_SMI_CHECK 450 }; 451 452 453 // Used to specify whether a receiver is implicitly or explicitly 454 // provided to a call. 455 enum CallKind { 456 CALL_AS_METHOD, 457 CALL_AS_FUNCTION 458 }; 459 460 461 enum ScopeType { 462 EVAL_SCOPE, // The top-level scope for an eval source. 463 FUNCTION_SCOPE, // The top-level scope for a function. 464 MODULE_SCOPE, // The scope introduced by a module literal 465 GLOBAL_SCOPE, // The top-level scope for a program or a top-level eval. 466 CATCH_SCOPE, // The scope introduced by catch. 467 BLOCK_SCOPE, // The scope introduced by a new block. 468 WITH_SCOPE // The scope introduced by with. 469 }; 470 471 472 const uint32_t kHoleNanUpper32 = 0x7FFFFFFF; 473 const uint32_t kHoleNanLower32 = 0xFFFFFFFF; 474 const uint32_t kNaNOrInfinityLowerBoundUpper32 = 0x7FF00000; 475 476 const uint64_t kHoleNanInt64 = 477 (static_cast<uint64_t>(kHoleNanUpper32) << 32) | kHoleNanLower32; 478 const uint64_t kLastNonNaNInt64 = 479 (static_cast<uint64_t>(kNaNOrInfinityLowerBoundUpper32) << 32); 480 481 482 enum VariableMode { 483 // User declared variables: 484 VAR, // declared via 'var', and 'function' declarations 485 486 CONST, // declared via 'const' declarations 487 488 CONST_HARMONY, // declared via 'const' declarations in harmony mode 489 490 LET, // declared via 'let' declarations 491 492 // Variables introduced by the compiler: 493 DYNAMIC, // always require dynamic lookup (we don't know 494 // the declaration) 495 496 DYNAMIC_GLOBAL, // requires dynamic lookup, but we know that the 497 // variable is global unless it has been shadowed 498 // by an eval-introduced variable 499 500 DYNAMIC_LOCAL, // requires dynamic lookup, but we know that the 501 // variable is local and where it is unless it 502 // has been shadowed by an eval-introduced 503 // variable 504 505 INTERNAL, // like VAR, but not user-visible (may or may not 506 // be in a context) 507 508 TEMPORARY // temporary variables (not user-visible), never 509 // in a context 510 }; 511 512 513 // ES6 Draft Rev3 10.2 specifies declarative environment records with mutable 514 // and immutable bindings that can be in two states: initialized and 515 // uninitialized. In ES5 only immutable bindings have these two states. When 516 // accessing a binding, it needs to be checked for initialization. However in 517 // the following cases the binding is initialized immediately after creation 518 // so the initialization check can always be skipped: 519 // 1. Var declared local variables. 520 // var foo; 521 // 2. A local variable introduced by a function declaration. 522 // function foo() {} 523 // 3. Parameters 524 // function x(foo) {} 525 // 4. Catch bound variables. 526 // try {} catch (foo) {} 527 // 6. Function variables of named function expressions. 528 // var x = function foo() {} 529 // 7. Implicit binding of 'this'. 530 // 8. Implicit binding of 'arguments' in functions. 531 // 532 // ES5 specified object environment records which are introduced by ES elements 533 // such as Program and WithStatement that associate identifier bindings with the 534 // properties of some object. In the specification only mutable bindings exist 535 // (which may be non-writable) and have no distinct initialization step. However 536 // V8 allows const declarations in global code with distinct creation and 537 // initialization steps which are represented by non-writable properties in the 538 // global object. As a result also these bindings need to be checked for 539 // initialization. 540 // 541 // The following enum specifies a flag that indicates if the binding needs a 542 // distinct initialization step (kNeedsInitialization) or if the binding is 543 // immediately initialized upon creation (kCreatedInitialized). 544 enum InitializationFlag { 545 kNeedsInitialization, 546 kCreatedInitialized 547 }; 548 549 550 enum ClearExceptionFlag { 551 KEEP_EXCEPTION, 552 CLEAR_EXCEPTION 553 }; 554 555 556 } } // namespace v8::internal 557 558 #endif // V8_V8GLOBALS_H_ 559