blob: 23139670c3e58f55facc4fc3efdd5a1baf24c44b [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 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#include "v8.h"
29
30#include "bootstrapper.h"
31#include "debug.h"
32#include "serialize.h"
Steve Blockd0582a62009-12-15 09:54:21 +000033#include "simulator.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "stub-cache.h"
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010035#include "heap-profiler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036#include "oprofile-agent.h"
Steve Blockd0582a62009-12-15 09:54:21 +000037#include "log.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000038
39namespace v8 {
40namespace internal {
41
42bool V8::is_running_ = false;
43bool V8::has_been_setup_ = false;
44bool V8::has_been_disposed_ = false;
45bool V8::has_fatal_error_ = false;
46
Steve Block6ded16b2010-05-10 14:33:55 +010047bool V8::Initialize(Deserializer* des) {
Steve Blocka7e24c12009-10-30 11:49:00 +000048 bool create_heap_objects = des == NULL;
49 if (has_been_disposed_ || has_fatal_error_) return false;
50 if (IsRunning()) return true;
51
52 is_running_ = true;
53 has_been_setup_ = true;
54 has_fatal_error_ = false;
55 has_been_disposed_ = false;
56#ifdef DEBUG
57 // The initialization process does not handle memory exhaustion.
58 DisallowAllocationFailure disallow_allocation_failure;
59#endif
60
61 // Enable logging before setting up the heap
62 Logger::Setup();
Steve Blocka7e24c12009-10-30 11:49:00 +000063
Steve Block6ded16b2010-05-10 14:33:55 +010064 CpuProfiler::Setup();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010065 HeapProfiler::Setup();
Steve Block6ded16b2010-05-10 14:33:55 +010066
Steve Blocka7e24c12009-10-30 11:49:00 +000067 // Setup the platform OS support.
68 OS::Setup();
69
70 // Initialize other runtime facilities
71#if !V8_HOST_ARCH_ARM && V8_TARGET_ARCH_ARM
72 ::assembler::arm::Simulator::Initialize();
73#endif
74
75 { // NOLINT
76 // Ensure that the thread has a valid stack guard. The v8::Locker object
77 // will ensure this too, but we don't have to use lockers if we are only
78 // using one thread.
79 ExecutionAccess lock;
80 StackGuard::InitThread(lock);
81 }
82
83 // Setup the object heap
84 ASSERT(!Heap::HasBeenSetup());
85 if (!Heap::Setup(create_heap_objects)) {
86 SetFatalError();
87 return false;
88 }
89
90 Bootstrapper::Initialize(create_heap_objects);
91 Builtins::Setup(create_heap_objects);
92 Top::Initialize();
93
94 if (FLAG_preemption) {
95 v8::Locker locker;
96 v8::Locker::StartPreemption(100);
97 }
98
99#ifdef ENABLE_DEBUGGER_SUPPORT
100 Debug::Setup(create_heap_objects);
101#endif
102 StubCache::Initialize(create_heap_objects);
103
104 // If we are deserializing, read the state into the now-empty heap.
105 if (des != NULL) {
106 des->Deserialize();
107 StubCache::Clear();
108 }
109
110 // Deserializing may put strange things in the root array's copy of the
111 // stack guard.
Steve Blockd0582a62009-12-15 09:54:21 +0000112 Heap::SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000113
114 // Setup the CPU support. Must be done after heap setup and after
115 // any deserialization because we have to have the initial heap
116 // objects in place for creating the code object used for probing.
117 CPU::Setup();
118
119 OProfileAgent::Initialize();
120
Andrei Popescu31002712010-02-23 13:46:05 +0000121 // If we are deserializing, log non-function code objects and compiled
122 // functions found in the snapshot.
123 if (des != NULL && FLAG_log_code) {
Steve Blockd0582a62009-12-15 09:54:21 +0000124 HandleScope scope;
Andrei Popescu31002712010-02-23 13:46:05 +0000125 LOG(LogCodeObjects());
Steve Blockd0582a62009-12-15 09:54:21 +0000126 LOG(LogCompiledFunctions());
127 }
128
Steve Blocka7e24c12009-10-30 11:49:00 +0000129 return true;
130}
131
132
133void V8::SetFatalError() {
134 is_running_ = false;
135 has_fatal_error_ = true;
136}
137
138
139void V8::TearDown() {
140 if (!has_been_setup_ || has_been_disposed_) return;
141
142 OProfileAgent::TearDown();
143
144 if (FLAG_preemption) {
145 v8::Locker locker;
146 v8::Locker::StopPreemption();
147 }
148
149 Builtins::TearDown();
150 Bootstrapper::TearDown();
151
152 Top::TearDown();
153
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100154 HeapProfiler::TearDown();
155
Steve Block6ded16b2010-05-10 14:33:55 +0100156 CpuProfiler::TearDown();
157
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100158 Heap::TearDown();
159
Steve Blocka7e24c12009-10-30 11:49:00 +0000160 Logger::TearDown();
161
162 is_running_ = false;
163 has_been_disposed_ = true;
164}
165
166
Steve Block6ded16b2010-05-10 14:33:55 +0100167static uint32_t random_seed() {
168 if (FLAG_random_seed == 0) {
169 return random();
170 }
171 return FLAG_random_seed;
172}
173
174
Steve Blocka7e24c12009-10-30 11:49:00 +0000175uint32_t V8::Random() {
176 // Random number generator using George Marsaglia's MWC algorithm.
177 static uint32_t hi = 0;
178 static uint32_t lo = 0;
179
180 // Initialize seed using the system random(). If one of the seeds
181 // should ever become zero again, or if random() returns zero, we
182 // avoid getting stuck with zero bits in hi or lo by re-initializing
183 // them on demand.
Steve Block6ded16b2010-05-10 14:33:55 +0100184 if (hi == 0) hi = random_seed();
185 if (lo == 0) lo = random_seed();
Steve Blocka7e24c12009-10-30 11:49:00 +0000186
187 // Mix the bits.
188 hi = 36969 * (hi & 0xFFFF) + (hi >> 16);
189 lo = 18273 * (lo & 0xFFFF) + (lo >> 16);
190 return (hi << 16) + (lo & 0xFFFF);
191}
192
193
Steve Block3ce2e202009-11-05 08:53:23 +0000194bool V8::IdleNotification() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000195 // Returning true tells the caller that there is no need to call
196 // IdleNotification again.
197 if (!FLAG_use_idle_notification) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000198
199 // Tell the heap that it may want to adjust.
200 return Heap::IdleNotification();
201}
202
203
Steve Block6ded16b2010-05-10 14:33:55 +0100204// Use a union type to avoid type-aliasing optimizations in GCC.
205typedef union {
206 double double_value;
207 uint64_t uint64_t_value;
208} double_int_union;
209
210
211Object* V8::FillHeapNumberWithRandom(Object* heap_number) {
212 uint64_t random_bits = Random();
213 // Make a double* from address (heap_number + sizeof(double)).
214 double_int_union* r = reinterpret_cast<double_int_union*>(
215 reinterpret_cast<char*>(heap_number) +
216 HeapNumber::kValueOffset - kHeapObjectTag);
217 // Convert 32 random bits to 0.(32 random bits) in a double
218 // by computing:
219 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
220 const double binary_million = 1048576.0;
221 r->double_value = binary_million;
222 r->uint64_t_value |= random_bits;
223 r->double_value -= binary_million;
224
225 return heap_number;
Steve Blocka7e24c12009-10-30 11:49:00 +0000226}
227
228} } // namespace v8::internal