blob: 8153372fbbafb1dde15f8cfc687da6a117f00bad [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
Steve Block44f0eee2011-05-26 01:26:41 +010030#include "isolate.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000031#include "bootstrapper.h"
32#include "debug.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010033#include "deoptimizer.h"
34#include "heap-profiler.h"
35#include "hydrogen.h"
36#include "lithium-allocator.h"
37#include "log.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010038#include "runtime-profiler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000039#include "serialize.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000040
41namespace v8 {
42namespace internal {
43
44bool V8::is_running_ = false;
45bool V8::has_been_setup_ = false;
46bool V8::has_been_disposed_ = false;
47bool V8::has_fatal_error_ = false;
Ben Murdochb0fe1622011-05-05 13:52:32 +010048bool V8::use_crankshaft_ = true;
Steve Blocka7e24c12009-10-30 11:49:00 +000049
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080050
Steve Block6ded16b2010-05-10 14:33:55 +010051bool V8::Initialize(Deserializer* des) {
Steve Block44f0eee2011-05-26 01:26:41 +010052 // The current thread may not yet had entered an isolate to run.
53 // Note the Isolate::Current() may be non-null because for various
54 // initialization purposes an initializing thread may be assigned an isolate
55 // but not actually enter it.
56 if (i::Isolate::CurrentPerIsolateThreadData() == NULL) {
57 i::Isolate::EnterDefaultIsolate();
58 }
59
60 ASSERT(i::Isolate::CurrentPerIsolateThreadData() != NULL);
61 ASSERT(i::Isolate::CurrentPerIsolateThreadData()->thread_id() ==
62 i::Thread::GetThreadLocalInt(i::Isolate::thread_id_key()));
63 ASSERT(i::Isolate::CurrentPerIsolateThreadData()->isolate() ==
64 i::Isolate::Current());
65
66 if (IsDead()) return false;
67
68 Isolate* isolate = Isolate::Current();
69 if (isolate->IsInitialized()) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +000070
Ben Murdoche0cee9b2011-05-25 10:26:03 +010071#if defined(V8_TARGET_ARCH_ARM) && !defined(USE_ARM_EABI)
72 use_crankshaft_ = false;
73#else
Ben Murdochb0fe1622011-05-05 13:52:32 +010074 use_crankshaft_ = FLAG_crankshaft;
Ben Murdoche0cee9b2011-05-25 10:26:03 +010075#endif
76
Ben Murdochb0fe1622011-05-05 13:52:32 +010077 // Peephole optimization might interfere with deoptimization.
78 FLAG_peephole_optimization = !use_crankshaft_;
Steve Block44f0eee2011-05-26 01:26:41 +010079
Steve Blocka7e24c12009-10-30 11:49:00 +000080 is_running_ = true;
81 has_been_setup_ = true;
82 has_fatal_error_ = false;
83 has_been_disposed_ = false;
Steve Blocka7e24c12009-10-30 11:49:00 +000084
Steve Block44f0eee2011-05-26 01:26:41 +010085 return isolate->Init(des);
Steve Blocka7e24c12009-10-30 11:49:00 +000086}
87
88
89void V8::SetFatalError() {
90 is_running_ = false;
91 has_fatal_error_ = true;
92}
93
94
95void V8::TearDown() {
Steve Block44f0eee2011-05-26 01:26:41 +010096 Isolate* isolate = Isolate::Current();
97 ASSERT(isolate->IsDefaultIsolate());
98
Steve Blocka7e24c12009-10-30 11:49:00 +000099 if (!has_been_setup_ || has_been_disposed_) return;
Steve Block44f0eee2011-05-26 01:26:41 +0100100 isolate->TearDown();
Steve Blocka7e24c12009-10-30 11:49:00 +0000101
102 is_running_ = false;
103 has_been_disposed_ = true;
104}
105
106
Steve Block6ded16b2010-05-10 14:33:55 +0100107static uint32_t random_seed() {
108 if (FLAG_random_seed == 0) {
109 return random();
110 }
111 return FLAG_random_seed;
112}
113
114
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800115typedef struct {
116 uint32_t hi;
117 uint32_t lo;
118} random_state;
Steve Blocka7e24c12009-10-30 11:49:00 +0000119
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800120
121// Random number generator using George Marsaglia's MWC algorithm.
122static uint32_t random_base(random_state *state) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000123 // Initialize seed using the system random(). If one of the seeds
124 // should ever become zero again, or if random() returns zero, we
125 // avoid getting stuck with zero bits in hi or lo by re-initializing
126 // them on demand.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800127 if (state->hi == 0) state->hi = random_seed();
128 if (state->lo == 0) state->lo = random_seed();
Steve Blocka7e24c12009-10-30 11:49:00 +0000129
130 // Mix the bits.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800131 state->hi = 36969 * (state->hi & 0xFFFF) + (state->hi >> 16);
132 state->lo = 18273 * (state->lo & 0xFFFF) + (state->lo >> 16);
133 return (state->hi << 16) + (state->lo & 0xFFFF);
134}
135
136
137// Used by JavaScript APIs
Steve Block44f0eee2011-05-26 01:26:41 +0100138uint32_t V8::Random(Isolate* isolate) {
139 ASSERT(isolate == Isolate::Current());
140 // TODO(isolates): move lo and hi to isolate
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800141 static random_state state = {0, 0};
142 return random_base(&state);
143}
144
145
146// Used internally by the JIT and memory allocator for security
147// purposes. So, we keep a different state to prevent informations
148// leaks that could be used in an exploit.
Steve Block44f0eee2011-05-26 01:26:41 +0100149uint32_t V8::RandomPrivate(Isolate* isolate) {
150 ASSERT(isolate == Isolate::Current());
151 // TODO(isolates): move lo and hi to isolate
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800152 static random_state state = {0, 0};
153 return random_base(&state);
Steve Blocka7e24c12009-10-30 11:49:00 +0000154}
155
156
Steve Block3ce2e202009-11-05 08:53:23 +0000157bool V8::IdleNotification() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000158 // Returning true tells the caller that there is no need to call
159 // IdleNotification again.
160 if (!FLAG_use_idle_notification) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000161
162 // Tell the heap that it may want to adjust.
Steve Block44f0eee2011-05-26 01:26:41 +0100163 return HEAP->IdleNotification();
Steve Blocka7e24c12009-10-30 11:49:00 +0000164}
165
166
Steve Block6ded16b2010-05-10 14:33:55 +0100167// Use a union type to avoid type-aliasing optimizations in GCC.
168typedef union {
169 double double_value;
170 uint64_t uint64_t_value;
171} double_int_union;
172
173
174Object* V8::FillHeapNumberWithRandom(Object* heap_number) {
Steve Block44f0eee2011-05-26 01:26:41 +0100175 uint64_t random_bits = Random(Isolate::Current());
Steve Block6ded16b2010-05-10 14:33:55 +0100176 // Make a double* from address (heap_number + sizeof(double)).
177 double_int_union* r = reinterpret_cast<double_int_union*>(
178 reinterpret_cast<char*>(heap_number) +
179 HeapNumber::kValueOffset - kHeapObjectTag);
180 // Convert 32 random bits to 0.(32 random bits) in a double
181 // by computing:
182 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
183 const double binary_million = 1048576.0;
184 r->double_value = binary_million;
185 r->uint64_t_value |= random_bits;
186 r->double_value -= binary_million;
187
188 return heap_number;
Steve Blocka7e24c12009-10-30 11:49:00 +0000189}
190
191} } // namespace v8::internal