blob: 3bec827aa6ff280e6414ad0668046b7880a24cd8 [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"
35#include "oprofile-agent.h"
Steve Blockd0582a62009-12-15 09:54:21 +000036#include "log.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000037
38namespace v8 {
39namespace internal {
40
41bool V8::is_running_ = false;
42bool V8::has_been_setup_ = false;
43bool V8::has_been_disposed_ = false;
44bool V8::has_fatal_error_ = false;
45
46bool V8::Initialize(Deserializer *des) {
47 bool create_heap_objects = des == NULL;
48 if (has_been_disposed_ || has_fatal_error_) return false;
49 if (IsRunning()) return true;
50
51 is_running_ = true;
52 has_been_setup_ = true;
53 has_fatal_error_ = false;
54 has_been_disposed_ = false;
55#ifdef DEBUG
56 // The initialization process does not handle memory exhaustion.
57 DisallowAllocationFailure disallow_allocation_failure;
58#endif
59
60 // Enable logging before setting up the heap
61 Logger::Setup();
Steve Blocka7e24c12009-10-30 11:49:00 +000062
63 // Setup the platform OS support.
64 OS::Setup();
65
66 // Initialize other runtime facilities
67#if !V8_HOST_ARCH_ARM && V8_TARGET_ARCH_ARM
68 ::assembler::arm::Simulator::Initialize();
69#endif
70
71 { // NOLINT
72 // Ensure that the thread has a valid stack guard. The v8::Locker object
73 // will ensure this too, but we don't have to use lockers if we are only
74 // using one thread.
75 ExecutionAccess lock;
76 StackGuard::InitThread(lock);
77 }
78
79 // Setup the object heap
80 ASSERT(!Heap::HasBeenSetup());
81 if (!Heap::Setup(create_heap_objects)) {
82 SetFatalError();
83 return false;
84 }
85
86 Bootstrapper::Initialize(create_heap_objects);
87 Builtins::Setup(create_heap_objects);
88 Top::Initialize();
89
90 if (FLAG_preemption) {
91 v8::Locker locker;
92 v8::Locker::StartPreemption(100);
93 }
94
95#ifdef ENABLE_DEBUGGER_SUPPORT
96 Debug::Setup(create_heap_objects);
97#endif
98 StubCache::Initialize(create_heap_objects);
99
100 // If we are deserializing, read the state into the now-empty heap.
101 if (des != NULL) {
102 des->Deserialize();
103 StubCache::Clear();
104 }
105
106 // Deserializing may put strange things in the root array's copy of the
107 // stack guard.
Steve Blockd0582a62009-12-15 09:54:21 +0000108 Heap::SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000109
110 // Setup the CPU support. Must be done after heap setup and after
111 // any deserialization because we have to have the initial heap
112 // objects in place for creating the code object used for probing.
113 CPU::Setup();
114
115 OProfileAgent::Initialize();
116
Steve Blockd0582a62009-12-15 09:54:21 +0000117 if (FLAG_log_code) {
118 HandleScope scope;
119 LOG(LogCompiledFunctions());
120 }
121
Steve Blocka7e24c12009-10-30 11:49:00 +0000122 return true;
123}
124
125
126void V8::SetFatalError() {
127 is_running_ = false;
128 has_fatal_error_ = true;
129}
130
131
132void V8::TearDown() {
133 if (!has_been_setup_ || has_been_disposed_) return;
134
135 OProfileAgent::TearDown();
136
137 if (FLAG_preemption) {
138 v8::Locker locker;
139 v8::Locker::StopPreemption();
140 }
141
142 Builtins::TearDown();
143 Bootstrapper::TearDown();
144
145 Top::TearDown();
146
147 Heap::TearDown();
148 Logger::TearDown();
149
150 is_running_ = false;
151 has_been_disposed_ = true;
152}
153
154
155uint32_t V8::Random() {
156 // Random number generator using George Marsaglia's MWC algorithm.
157 static uint32_t hi = 0;
158 static uint32_t lo = 0;
159
160 // Initialize seed using the system random(). If one of the seeds
161 // should ever become zero again, or if random() returns zero, we
162 // avoid getting stuck with zero bits in hi or lo by re-initializing
163 // them on demand.
164 if (hi == 0) hi = random();
165 if (lo == 0) lo = random();
166
167 // Mix the bits.
168 hi = 36969 * (hi & 0xFFFF) + (hi >> 16);
169 lo = 18273 * (lo & 0xFFFF) + (lo >> 16);
170 return (hi << 16) + (lo & 0xFFFF);
171}
172
173
Steve Block3ce2e202009-11-05 08:53:23 +0000174bool V8::IdleNotification() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000175 // Returning true tells the caller that there is no need to call
176 // IdleNotification again.
177 if (!FLAG_use_idle_notification) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000178
179 // Tell the heap that it may want to adjust.
180 return Heap::IdleNotification();
181}
182
Steve Block3ce2e202009-11-05 08:53:23 +0000183static const uint32_t kRandomPositiveSmiMax = 0x3fffffff;
Steve Blocka7e24c12009-10-30 11:49:00 +0000184
185Smi* V8::RandomPositiveSmi() {
186 uint32_t random = Random();
Steve Block3ce2e202009-11-05 08:53:23 +0000187 ASSERT(static_cast<uint32_t>(Smi::kMaxValue) >= kRandomPositiveSmiMax);
188 // kRandomPositiveSmiMax must match the value being divided
189 // by in math.js.
190 return Smi::FromInt(random & kRandomPositiveSmiMax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000191}
192
193} } // namespace v8::internal