blob: 0623400abbd372665103fd027d049b111145f2cd [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
John Reck59135872010-11-02 12:39:01 -070071#if defined(USE_SIMULATOR)
72#if defined(V8_TARGET_ARCH_ARM)
Steve Blocka7e24c12009-10-30 11:49:00 +000073 ::assembler::arm::Simulator::Initialize();
John Reck59135872010-11-02 12:39:01 -070074#elif defined(V8_TARGET_ARCH_MIPS)
75 ::assembler::mips::Simulator::Initialize();
76#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000077#endif
78
79 { // NOLINT
80 // Ensure that the thread has a valid stack guard. The v8::Locker object
81 // will ensure this too, but we don't have to use lockers if we are only
82 // using one thread.
83 ExecutionAccess lock;
84 StackGuard::InitThread(lock);
85 }
86
87 // Setup the object heap
88 ASSERT(!Heap::HasBeenSetup());
89 if (!Heap::Setup(create_heap_objects)) {
90 SetFatalError();
91 return false;
92 }
93
94 Bootstrapper::Initialize(create_heap_objects);
95 Builtins::Setup(create_heap_objects);
96 Top::Initialize();
97
98 if (FLAG_preemption) {
99 v8::Locker locker;
100 v8::Locker::StartPreemption(100);
101 }
102
103#ifdef ENABLE_DEBUGGER_SUPPORT
104 Debug::Setup(create_heap_objects);
105#endif
106 StubCache::Initialize(create_heap_objects);
107
108 // If we are deserializing, read the state into the now-empty heap.
109 if (des != NULL) {
110 des->Deserialize();
111 StubCache::Clear();
112 }
113
114 // Deserializing may put strange things in the root array's copy of the
115 // stack guard.
Steve Blockd0582a62009-12-15 09:54:21 +0000116 Heap::SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000117
118 // Setup the CPU support. Must be done after heap setup and after
119 // any deserialization because we have to have the initial heap
120 // objects in place for creating the code object used for probing.
121 CPU::Setup();
122
123 OProfileAgent::Initialize();
124
Andrei Popescu31002712010-02-23 13:46:05 +0000125 // If we are deserializing, log non-function code objects and compiled
126 // functions found in the snapshot.
127 if (des != NULL && FLAG_log_code) {
Steve Blockd0582a62009-12-15 09:54:21 +0000128 HandleScope scope;
Andrei Popescu31002712010-02-23 13:46:05 +0000129 LOG(LogCodeObjects());
Steve Blockd0582a62009-12-15 09:54:21 +0000130 LOG(LogCompiledFunctions());
131 }
132
Steve Blocka7e24c12009-10-30 11:49:00 +0000133 return true;
134}
135
136
137void V8::SetFatalError() {
138 is_running_ = false;
139 has_fatal_error_ = true;
140}
141
142
143void V8::TearDown() {
144 if (!has_been_setup_ || has_been_disposed_) return;
145
146 OProfileAgent::TearDown();
147
148 if (FLAG_preemption) {
149 v8::Locker locker;
150 v8::Locker::StopPreemption();
151 }
152
153 Builtins::TearDown();
154 Bootstrapper::TearDown();
155
156 Top::TearDown();
157
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100158 HeapProfiler::TearDown();
159
Steve Block6ded16b2010-05-10 14:33:55 +0100160 CpuProfiler::TearDown();
161
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100162 Heap::TearDown();
163
Steve Blocka7e24c12009-10-30 11:49:00 +0000164 Logger::TearDown();
165
166 is_running_ = false;
167 has_been_disposed_ = true;
168}
169
170
Steve Block6ded16b2010-05-10 14:33:55 +0100171static uint32_t random_seed() {
172 if (FLAG_random_seed == 0) {
173 return random();
174 }
175 return FLAG_random_seed;
176}
177
178
Steve Blocka7e24c12009-10-30 11:49:00 +0000179uint32_t V8::Random() {
180 // Random number generator using George Marsaglia's MWC algorithm.
181 static uint32_t hi = 0;
182 static uint32_t lo = 0;
183
184 // Initialize seed using the system random(). If one of the seeds
185 // should ever become zero again, or if random() returns zero, we
186 // avoid getting stuck with zero bits in hi or lo by re-initializing
187 // them on demand.
Steve Block6ded16b2010-05-10 14:33:55 +0100188 if (hi == 0) hi = random_seed();
189 if (lo == 0) lo = random_seed();
Steve Blocka7e24c12009-10-30 11:49:00 +0000190
191 // Mix the bits.
192 hi = 36969 * (hi & 0xFFFF) + (hi >> 16);
193 lo = 18273 * (lo & 0xFFFF) + (lo >> 16);
194 return (hi << 16) + (lo & 0xFFFF);
195}
196
197
Steve Block3ce2e202009-11-05 08:53:23 +0000198bool V8::IdleNotification() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000199 // Returning true tells the caller that there is no need to call
200 // IdleNotification again.
201 if (!FLAG_use_idle_notification) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000202
203 // Tell the heap that it may want to adjust.
204 return Heap::IdleNotification();
205}
206
207
Steve Block6ded16b2010-05-10 14:33:55 +0100208// Use a union type to avoid type-aliasing optimizations in GCC.
209typedef union {
210 double double_value;
211 uint64_t uint64_t_value;
212} double_int_union;
213
214
215Object* V8::FillHeapNumberWithRandom(Object* heap_number) {
216 uint64_t random_bits = Random();
217 // Make a double* from address (heap_number + sizeof(double)).
218 double_int_union* r = reinterpret_cast<double_int_union*>(
219 reinterpret_cast<char*>(heap_number) +
220 HeapNumber::kValueOffset - kHeapObjectTag);
221 // Convert 32 random bits to 0.(32 random bits) in a double
222 // by computing:
223 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
224 const double binary_million = 1048576.0;
225 r->double_value = binary_million;
226 r->uint64_t_value |= random_bits;
227 r->double_value -= binary_million;
228
229 return heap_number;
Steve Blocka7e24c12009-10-30 11:49:00 +0000230}
231
232} } // namespace v8::internal