svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 30 | #include "assembler.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 31 | #include "isolate.h" |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 32 | #include "elements.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "bootstrapper.h" |
| 34 | #include "debug.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 35 | #include "deoptimizer.h" |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 36 | #include "frames.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 37 | #include "heap-profiler.h" |
| 38 | #include "hydrogen.h" |
| 39 | #include "lithium-allocator.h" |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 40 | #include "objects.h" |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 41 | #include "once.h" |
| 42 | #include "platform.h" |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 43 | #include "sampler.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 44 | #include "runtime-profiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | #include "serialize.h" |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 46 | #include "store-buffer.h" |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 47 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 48 | namespace v8 { |
| 49 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 51 | V8_DECLARE_ONCE(init_once); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 52 | |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 53 | List<CallCompletedCallback>* V8::call_completed_callbacks_ = NULL; |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 54 | v8::ArrayBuffer::Allocator* V8::array_buffer_allocator_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 55 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 56 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 57 | bool V8::Initialize(Deserializer* des) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 58 | InitializeOncePerProcess(); |
| 59 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 60 | // The current thread may not yet had entered an isolate to run. |
| 61 | // Note the Isolate::Current() may be non-null because for various |
| 62 | // initialization purposes an initializing thread may be assigned an isolate |
| 63 | // but not actually enter it. |
| 64 | if (i::Isolate::CurrentPerIsolateThreadData() == NULL) { |
| 65 | i::Isolate::EnterDefaultIsolate(); |
| 66 | } |
| 67 | |
| 68 | ASSERT(i::Isolate::CurrentPerIsolateThreadData() != NULL); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 69 | ASSERT(i::Isolate::CurrentPerIsolateThreadData()->thread_id().Equals( |
| 70 | i::ThreadId::Current())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 71 | ASSERT(i::Isolate::CurrentPerIsolateThreadData()->isolate() == |
| 72 | i::Isolate::Current()); |
| 73 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 74 | Isolate* isolate = Isolate::Current(); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 75 | if (isolate->IsDead()) return false; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 76 | if (isolate->IsInitialized()) return true; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 77 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 78 | return isolate->Init(des); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
| 82 | void V8::TearDown() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 83 | Isolate* isolate = Isolate::Current(); |
| 84 | ASSERT(isolate->IsDefaultIsolate()); |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 85 | if (!isolate->IsInitialized()) return; |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 86 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 87 | // The isolate has to be torn down before clearing the LOperand |
| 88 | // caches so that the optimizing compiler thread (if running) |
| 89 | // doesn't see an inconsistent view of the lithium instructions. |
| 90 | isolate->TearDown(); |
| 91 | delete isolate; |
| 92 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 93 | ElementsAccessor::TearDown(); |
| 94 | LOperand::TearDownCaches(); |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 95 | ExternalReference::TearDownMathExpData(); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 96 | RegisteredExtension::UnregisterAll(); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 97 | Isolate::GlobalTearDown(); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 98 | |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 99 | delete call_completed_callbacks_; |
| 100 | call_completed_callbacks_ = NULL; |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 101 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 102 | Sampler::TearDown(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | } |
| 104 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 105 | |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 106 | void V8::SetReturnAddressLocationResolver( |
| 107 | ReturnAddressLocationResolver resolver) { |
| 108 | StackFrame::SetReturnAddressLocationResolver(resolver); |
| 109 | } |
| 110 | |
| 111 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 112 | // Used by JavaScript APIs |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 113 | uint32_t V8::Random(Context* context) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 114 | ASSERT(context->IsNativeContext()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 115 | ByteArray* seed = context->random_seed(); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 116 | uint32_t* state = reinterpret_cast<uint32_t*>(seed->GetDataStartAddress()); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 117 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 118 | // When we get here, the RNG must have been initialized, |
| 119 | // see the Genesis constructor in file bootstrapper.cc. |
| 120 | ASSERT_NE(0, state[0]); |
| 121 | ASSERT_NE(0, state[1]); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 122 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 123 | // Mix the bits. Never replaces state[i] with 0 if it is nonzero. |
| 124 | state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16); |
| 125 | state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16); |
| 126 | |
| 127 | return (state[0] << 14) + (state[1] & 0x3FFFF); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 128 | } |
| 129 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 130 | |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 131 | void V8::AddCallCompletedCallback(CallCompletedCallback callback) { |
| 132 | if (call_completed_callbacks_ == NULL) { // Lazy init. |
| 133 | call_completed_callbacks_ = new List<CallCompletedCallback>(); |
| 134 | } |
| 135 | for (int i = 0; i < call_completed_callbacks_->length(); i++) { |
| 136 | if (callback == call_completed_callbacks_->at(i)) return; |
| 137 | } |
| 138 | call_completed_callbacks_->Add(callback); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | void V8::RemoveCallCompletedCallback(CallCompletedCallback callback) { |
| 143 | if (call_completed_callbacks_ == NULL) return; |
| 144 | for (int i = 0; i < call_completed_callbacks_->length(); i++) { |
| 145 | if (callback == call_completed_callbacks_->at(i)) { |
| 146 | call_completed_callbacks_->Remove(i); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | |
| 152 | void V8::FireCallCompletedCallback(Isolate* isolate) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 153 | bool has_call_completed_callbacks = call_completed_callbacks_ != NULL; |
| 154 | bool observer_delivery_pending = |
| 155 | FLAG_harmony_observation && isolate->observer_delivery_pending(); |
| 156 | if (!has_call_completed_callbacks && !observer_delivery_pending) return; |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 157 | HandleScopeImplementer* handle_scope_implementer = |
| 158 | isolate->handle_scope_implementer(); |
| 159 | if (!handle_scope_implementer->CallDepthIsZero()) return; |
| 160 | // Fire callbacks. Increase call depth to prevent recursive callbacks. |
| 161 | handle_scope_implementer->IncrementCallDepth(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 162 | if (observer_delivery_pending) { |
| 163 | JSObject::DeliverChangeRecords(isolate); |
| 164 | } |
| 165 | if (has_call_completed_callbacks) { |
| 166 | for (int i = 0; i < call_completed_callbacks_->length(); i++) { |
| 167 | call_completed_callbacks_->at(i)(); |
| 168 | } |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 169 | } |
| 170 | handle_scope_implementer->DecrementCallDepth(); |
| 171 | } |
| 172 | |
| 173 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 174 | // Use a union type to avoid type-aliasing optimizations in GCC. |
| 175 | typedef union { |
| 176 | double double_value; |
| 177 | uint64_t uint64_t_value; |
| 178 | } double_int_union; |
| 179 | |
| 180 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 181 | Object* V8::FillHeapNumberWithRandom(Object* heap_number, |
| 182 | Context* context) { |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 183 | double_int_union r; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 184 | uint64_t random_bits = Random(context); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 185 | // Convert 32 random bits to 0.(32 random bits) in a double |
| 186 | // by computing: |
| 187 | // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 188 | static const double binary_million = 1048576.0; |
| 189 | r.double_value = binary_million; |
| 190 | r.uint64_t_value |= random_bits; |
| 191 | r.double_value -= binary_million; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 192 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 193 | HeapNumber::cast(heap_number)->set_value(r.double_value); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 194 | return heap_number; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 195 | } |
| 196 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 197 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 198 | void V8::InitializeOncePerProcessImpl() { |
yangguo@chromium.org | 28381b4 | 2013-01-21 14:39:38 +0000 | [diff] [blame] | 199 | FlagList::EnforceFlagImplications(); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 200 | if (FLAG_stress_compaction) { |
| 201 | FLAG_force_marking_deque_overflows = true; |
| 202 | FLAG_gc_global = true; |
| 203 | FLAG_max_new_space_size = (1 << (kPageSizeBits - 10)) * 2; |
| 204 | } |
danno@chromium.org | c16e828 | 2013-08-12 16:17:40 +0000 | [diff] [blame] | 205 | |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 206 | if (FLAG_concurrent_recompilation && |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 207 | (FLAG_trace_hydrogen || FLAG_trace_hydrogen_stubs)) { |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 208 | FLAG_concurrent_recompilation = false; |
| 209 | PrintF("Concurrent recompilation has been disabled for tracing.\n"); |
danno@chromium.org | c16e828 | 2013-08-12 16:17:40 +0000 | [diff] [blame] | 210 | } |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 211 | |
| 212 | if (FLAG_sweeper_threads <= 0) { |
| 213 | if (FLAG_concurrent_sweeping) { |
| 214 | FLAG_sweeper_threads = SystemThreadManager:: |
| 215 | NumberOfParallelSystemThreads( |
| 216 | SystemThreadManager::CONCURRENT_SWEEPING); |
| 217 | } else if (FLAG_parallel_sweeping) { |
| 218 | FLAG_sweeper_threads = SystemThreadManager:: |
| 219 | NumberOfParallelSystemThreads( |
| 220 | SystemThreadManager::PARALLEL_SWEEPING); |
| 221 | } |
| 222 | if (FLAG_sweeper_threads == 0) { |
| 223 | FLAG_concurrent_sweeping = false; |
| 224 | FLAG_parallel_sweeping = false; |
| 225 | } |
| 226 | } else if (!FLAG_concurrent_sweeping && !FLAG_parallel_sweeping) { |
| 227 | FLAG_sweeper_threads = 0; |
| 228 | } |
| 229 | |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 230 | if (FLAG_concurrent_recompilation && |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 231 | SystemThreadManager::NumberOfParallelSystemThreads( |
| 232 | SystemThreadManager::PARALLEL_RECOMPILATION) == 0) { |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 233 | FLAG_concurrent_recompilation = false; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 234 | } |
| 235 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 236 | Sampler::SetUp(); |
yangguo@chromium.org | 28381b4 | 2013-01-21 14:39:38 +0000 | [diff] [blame] | 237 | CPU::SetUp(); |
yangguo@chromium.org | 28381b4 | 2013-01-21 14:39:38 +0000 | [diff] [blame] | 238 | OS::PostSetUp(); |
yangguo@chromium.org | 28381b4 | 2013-01-21 14:39:38 +0000 | [diff] [blame] | 239 | ElementsAccessor::InitializeOncePerProcess(); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 240 | LOperand::SetUpCaches(); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 241 | SetUpJSCallerSavedCodeData(); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 242 | ExternalReference::SetUp(); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 243 | Bootstrapper::InitializeOncePerProcess(); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 244 | } |
| 245 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 246 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 247 | void V8::InitializeOncePerProcess() { |
| 248 | CallOnce(&init_once, &InitializeOncePerProcessImpl); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 249 | } |
| 250 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 251 | } } // namespace v8::internal |