blob: 00e0e6e6d5e2dc709ea0811fa2046fc17bc4243a [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"
33#include "stub-cache.h"
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000034#include "oprofile-agent.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035
ager@chromium.orgeadaf222009-06-16 09:43:10 +000036#if V8_TARGET_ARCH_ARM
37#include "arm/simulator-arm.h"
38#endif
39
kasperl@chromium.org71affb52009-05-26 05:44:31 +000040namespace v8 {
41namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
kasperl@chromium.org71affb52009-05-26 05:44:31 +000043bool V8::is_running_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044bool V8::has_been_setup_ = false;
45bool V8::has_been_disposed_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000046bool V8::has_fatal_error_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047
48bool V8::Initialize(Deserializer *des) {
49 bool create_heap_objects = des == NULL;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000050 if (has_been_disposed_ || has_fatal_error_) return false;
51 if (IsRunning()) return true;
52
53 is_running_ = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000054 has_been_setup_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000055 has_fatal_error_ = false;
56 has_been_disposed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000057#ifdef DEBUG
58 // The initialization process does not handle memory exhaustion.
59 DisallowAllocationFailure disallow_allocation_failure;
60#endif
61
62 // Enable logging before setting up the heap
63 Logger::Setup();
64 if (des) des->GetLog();
65
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000066 // Setup the platform OS support.
67 OS::Setup();
68
ager@chromium.orgeadaf222009-06-16 09:43:10 +000069 // Initialize other runtime facilities
70#if !V8_HOST_ARCH_ARM && V8_TARGET_ARCH_ARM
71 ::assembler::arm::Simulator::Initialize();
72#endif
73
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000074 // Setup the object heap
75 ASSERT(!Heap::HasBeenSetup());
76 if (!Heap::Setup(create_heap_objects)) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +000077 SetFatalError();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078 return false;
79 }
80
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000081 Bootstrapper::Initialize(create_heap_objects);
82 Builtins::Setup(create_heap_objects);
83 Top::Initialize();
84
85 if (FLAG_preemption) {
86 v8::Locker locker;
87 v8::Locker::StartPreemption(100);
88 }
89
ager@chromium.org65dad4b2009-04-23 08:48:43 +000090#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091 Debug::Setup(create_heap_objects);
ager@chromium.org65dad4b2009-04-23 08:48:43 +000092#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093 StubCache::Initialize(create_heap_objects);
94
95 // If we are deserializing, read the state into the now-empty heap.
96 if (des != NULL) {
97 des->Deserialize();
98 StubCache::Clear();
99 }
100
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000101 // Setup the CPU support. Must be done after heap setup and after
102 // any deserialization because we have to have the initial heap
103 // objects in place for creating the code object used for probing.
104 CPU::Setup();
105
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000106 OProfileAgent::Initialize();
107
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108 return true;
109}
110
111
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000112void V8::SetFatalError() {
113 is_running_ = false;
114 has_fatal_error_ = true;
115}
116
117
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118void V8::TearDown() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000119 if (!has_been_setup_ || has_been_disposed_) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000120
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000121 OProfileAgent::TearDown();
122
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123 if (FLAG_preemption) {
124 v8::Locker locker;
125 v8::Locker::StopPreemption();
126 }
127
128 Builtins::TearDown();
129 Bootstrapper::TearDown();
130
kasper.lundaf4734f2008-07-28 12:50:18 +0000131 Top::TearDown();
132
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133 Heap::TearDown();
134 Logger::TearDown();
135
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000136 is_running_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137 has_been_disposed_ = true;
138}
139
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000140
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000141uint32_t V8::Random() {
142 // Random number generator using George Marsaglia's MWC algorithm.
143 static uint32_t hi = 0;
144 static uint32_t lo = 0;
145
146 // Initialize seed using the system random(). If one of the seeds
147 // should ever become zero again, or if random() returns zero, we
148 // avoid getting stuck with zero bits in hi or lo by re-initializing
149 // them on demand.
150 if (hi == 0) hi = random();
151 if (lo == 0) lo = random();
152
153 // Mix the bits.
154 hi = 36969 * (hi & 0xFFFF) + (hi >> 16);
155 lo = 18273 * (lo & 0xFFFF) + (lo >> 16);
156 return (hi << 16) + (lo & 0xFFFF);
157}
158
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000159void V8::IdleNotification(bool is_high_priority) {
160 if (!FLAG_use_idle_notification) return;
161 // Ignore high priority instances of V8.
162 if (is_high_priority) return;
163
164 // Uncommit unused memory in new space.
165 Heap::UncommitFromSpace();
166}
167
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000168
169Smi* V8::RandomPositiveSmi() {
170 uint32_t random = Random();
171 ASSERT(IsPowerOf2(Smi::kMaxValue + 1));
172 return Smi::FromInt(random & Smi::kMaxValue);
173}
174
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175} } // namespace v8::internal