blob: 0623400abbd372665103fd027d049b111145f2cd [file] [log] [blame]
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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"
ager@chromium.orgc4c92722009-11-18 14:12:51 +000033#include "simulator.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034#include "stub-cache.h"
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000035#include "heap-profiler.h"
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000036#include "oprofile-agent.h"
ager@chromium.orgc4c92722009-11-18 14:12:51 +000037#include "log.h"
ager@chromium.orgeadaf222009-06-16 09:43:10 +000038
kasperl@chromium.org71affb52009-05-26 05:44:31 +000039namespace v8 {
40namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
kasperl@chromium.org71affb52009-05-26 05:44:31 +000042bool V8::is_running_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043bool V8::has_been_setup_ = false;
44bool V8::has_been_disposed_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000045bool V8::has_fatal_error_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000047bool V8::Initialize(Deserializer* des) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048 bool create_heap_objects = des == NULL;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000049 if (has_been_disposed_ || has_fatal_error_) return false;
50 if (IsRunning()) return true;
51
52 is_running_ = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053 has_been_setup_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000054 has_fatal_error_ = false;
55 has_been_disposed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056#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();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000063
lrn@chromium.org25156de2010-04-06 13:10:27 +000064 CpuProfiler::Setup();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000065 HeapProfiler::Setup();
lrn@chromium.org25156de2010-04-06 13:10:27 +000066
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000067 // Setup the platform OS support.
68 OS::Setup();
69
ager@chromium.orgeadaf222009-06-16 09:43:10 +000070 // Initialize other runtime facilities
lrn@chromium.org303ada72010-10-27 09:33:13 +000071#if defined(USE_SIMULATOR)
72#if defined(V8_TARGET_ARCH_ARM)
ager@chromium.orgeadaf222009-06-16 09:43:10 +000073 ::assembler::arm::Simulator::Initialize();
lrn@chromium.org303ada72010-10-27 09:33:13 +000074#elif defined(V8_TARGET_ARCH_MIPS)
75 ::assembler::mips::Simulator::Initialize();
76#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +000077#endif
78
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000079 { // 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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000087 // Setup the object heap
88 ASSERT(!Heap::HasBeenSetup());
89 if (!Heap::Setup(create_heap_objects)) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +000090 SetFatalError();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091 return false;
92 }
93
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094 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
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000103#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104 Debug::Setup(create_heap_objects);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000105#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000106 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
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000114 // Deserializing may put strange things in the root array's copy of the
115 // stack guard.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000116 Heap::SetStackLimits();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000117
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000118 // 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
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000123 OProfileAgent::Initialize();
124
ager@chromium.org5c838252010-02-19 08:53:10 +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) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000128 HandleScope scope;
ager@chromium.org5c838252010-02-19 08:53:10 +0000129 LOG(LogCodeObjects());
ager@chromium.org01beca72009-11-24 14:29:16 +0000130 LOG(LogCompiledFunctions());
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000131 }
132
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133 return true;
134}
135
136
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000137void V8::SetFatalError() {
138 is_running_ = false;
139 has_fatal_error_ = true;
140}
141
142
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143void V8::TearDown() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000144 if (!has_been_setup_ || has_been_disposed_) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000146 OProfileAgent::TearDown();
147
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148 if (FLAG_preemption) {
149 v8::Locker locker;
150 v8::Locker::StopPreemption();
151 }
152
153 Builtins::TearDown();
154 Bootstrapper::TearDown();
155
kasper.lundaf4734f2008-07-28 12:50:18 +0000156 Top::TearDown();
157
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000158 HeapProfiler::TearDown();
159
lrn@chromium.org25156de2010-04-06 13:10:27 +0000160 CpuProfiler::TearDown();
161
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000162 Heap::TearDown();
163
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164 Logger::TearDown();
165
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000166 is_running_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 has_been_disposed_ = true;
168}
169
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000170
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000171static uint32_t random_seed() {
172 if (FLAG_random_seed == 0) {
173 return random();
174 }
175 return FLAG_random_seed;
176}
177
178
ager@chromium.orgeadaf222009-06-16 09:43:10 +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.
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000188 if (hi == 0) hi = random_seed();
189 if (lo == 0) lo = random_seed();
ager@chromium.orgeadaf222009-06-16 09:43:10 +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
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000197
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000198bool V8::IdleNotification() {
199 // Returning true tells the caller that there is no need to call
200 // IdleNotification again.
201 if (!FLAG_use_idle_notification) return true;
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000202
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000203 // Tell the heap that it may want to adjust.
204 return Heap::IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000205}
206
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000207
ager@chromium.org357bf652010-04-12 11:30:10 +0000208// 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;
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000230}
231
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000232} } // namespace v8::internal