ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 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 | |
| 30 | #include "bootstrapper.h" |
| 31 | #include "debug.h" |
| 32 | #include "serialize.h" |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 33 | #include "simulator.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 34 | #include "stub-cache.h" |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 35 | #include "heap-profiler.h" |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 36 | #include "oprofile-agent.h" |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 37 | #include "log.h" |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 38 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 39 | namespace v8 { |
| 40 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 42 | bool V8::is_running_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | bool V8::has_been_setup_ = false; |
| 44 | bool V8::has_been_disposed_ = false; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 45 | bool V8::has_fatal_error_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 47 | bool V8::Initialize(Deserializer* des) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | bool create_heap_objects = des == NULL; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 49 | if (has_been_disposed_ || has_fatal_error_) return false; |
| 50 | if (IsRunning()) return true; |
| 51 | |
| 52 | is_running_ = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 53 | has_been_setup_ = true; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 54 | has_fatal_error_ = false; |
| 55 | has_been_disposed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 56 | #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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 63 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 64 | CpuProfiler::Setup(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 65 | HeapProfiler::Setup(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 66 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 67 | // Setup the platform OS support. |
| 68 | OS::Setup(); |
| 69 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 70 | // Initialize other runtime facilities |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 71 | #if defined(USE_SIMULATOR) |
| 72 | #if defined(V8_TARGET_ARCH_ARM) |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 73 | ::assembler::arm::Simulator::Initialize(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 74 | #elif defined(V8_TARGET_ARCH_MIPS) |
| 75 | ::assembler::mips::Simulator::Initialize(); |
| 76 | #endif |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 77 | #endif |
| 78 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 79 | { // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 87 | // Setup the object heap |
| 88 | ASSERT(!Heap::HasBeenSetup()); |
| 89 | if (!Heap::Setup(create_heap_objects)) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 90 | SetFatalError(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 94 | 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.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 103 | #ifdef ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | Debug::Setup(create_heap_objects); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 105 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 106 | 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.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 114 | // Deserializing may put strange things in the root array's copy of the |
| 115 | // stack guard. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 116 | Heap::SetStackLimits(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 117 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 118 | // 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.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 123 | OProfileAgent::Initialize(); |
| 124 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 125 | // 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.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 128 | HandleScope scope; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 129 | LOG(LogCodeObjects()); |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 130 | LOG(LogCompiledFunctions()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 131 | } |
| 132 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | |
| 136 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 137 | void V8::SetFatalError() { |
| 138 | is_running_ = false; |
| 139 | has_fatal_error_ = true; |
| 140 | } |
| 141 | |
| 142 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 143 | void V8::TearDown() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 144 | if (!has_been_setup_ || has_been_disposed_) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 145 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 146 | OProfileAgent::TearDown(); |
| 147 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 148 | if (FLAG_preemption) { |
| 149 | v8::Locker locker; |
| 150 | v8::Locker::StopPreemption(); |
| 151 | } |
| 152 | |
| 153 | Builtins::TearDown(); |
| 154 | Bootstrapper::TearDown(); |
| 155 | |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 156 | Top::TearDown(); |
| 157 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 158 | HeapProfiler::TearDown(); |
| 159 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 160 | CpuProfiler::TearDown(); |
| 161 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 162 | Heap::TearDown(); |
| 163 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 164 | Logger::TearDown(); |
| 165 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 166 | is_running_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 167 | has_been_disposed_ = true; |
| 168 | } |
| 169 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 170 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 171 | static uint32_t random_seed() { |
| 172 | if (FLAG_random_seed == 0) { |
| 173 | return random(); |
| 174 | } |
| 175 | return FLAG_random_seed; |
| 176 | } |
| 177 | |
| 178 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 179 | uint32_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.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 188 | if (hi == 0) hi = random_seed(); |
| 189 | if (lo == 0) lo = random_seed(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 190 | |
| 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.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 197 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 198 | bool 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.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 202 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 203 | // Tell the heap that it may want to adjust. |
| 204 | return Heap::IdleNotification(); |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 205 | } |
| 206 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 207 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 208 | // Use a union type to avoid type-aliasing optimizations in GCC. |
| 209 | typedef union { |
| 210 | double double_value; |
| 211 | uint64_t uint64_t_value; |
| 212 | } double_int_union; |
| 213 | |
| 214 | |
| 215 | Object* 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.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 230 | } |
| 231 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 232 | } } // namespace v8::internal |