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" |
| 33 | #include "stub-cache.h" |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 34 | #include "oprofile-agent.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 36 | #if V8_TARGET_ARCH_ARM |
| 37 | #include "arm/simulator-arm.h" |
| 38 | #endif |
| 39 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 40 | namespace v8 { |
| 41 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 43 | bool V8::is_running_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | bool V8::has_been_setup_ = false; |
| 45 | bool V8::has_been_disposed_ = false; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 46 | bool V8::has_fatal_error_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 47 | |
| 48 | bool V8::Initialize(Deserializer *des) { |
| 49 | bool create_heap_objects = des == NULL; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 50 | if (has_been_disposed_ || has_fatal_error_) return false; |
| 51 | if (IsRunning()) return true; |
| 52 | |
| 53 | is_running_ = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 54 | has_been_setup_ = true; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 55 | has_fatal_error_ = false; |
| 56 | has_been_disposed_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 57 | #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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 66 | // Setup the platform OS support. |
| 67 | OS::Setup(); |
| 68 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 69 | // Initialize other runtime facilities |
| 70 | #if !V8_HOST_ARCH_ARM && V8_TARGET_ARCH_ARM |
| 71 | ::assembler::arm::Simulator::Initialize(); |
| 72 | #endif |
| 73 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 74 | // Setup the object heap |
| 75 | ASSERT(!Heap::HasBeenSetup()); |
| 76 | if (!Heap::Setup(create_heap_objects)) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 77 | SetFatalError(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 81 | 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.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 90 | #ifdef ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 91 | Debug::Setup(create_heap_objects); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 92 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 93 | 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.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 101 | // 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.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 106 | OProfileAgent::Initialize(); |
| 107 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 108 | return true; |
| 109 | } |
| 110 | |
| 111 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 112 | void V8::SetFatalError() { |
| 113 | is_running_ = false; |
| 114 | has_fatal_error_ = true; |
| 115 | } |
| 116 | |
| 117 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 118 | void V8::TearDown() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 119 | if (!has_been_setup_ || has_been_disposed_) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 120 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 121 | OProfileAgent::TearDown(); |
| 122 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 123 | if (FLAG_preemption) { |
| 124 | v8::Locker locker; |
| 125 | v8::Locker::StopPreemption(); |
| 126 | } |
| 127 | |
| 128 | Builtins::TearDown(); |
| 129 | Bootstrapper::TearDown(); |
| 130 | |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 131 | Top::TearDown(); |
| 132 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | Heap::TearDown(); |
| 134 | Logger::TearDown(); |
| 135 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 136 | is_running_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | has_been_disposed_ = true; |
| 138 | } |
| 139 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 140 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 141 | uint32_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.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame^] | 159 | void 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.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 168 | |
| 169 | Smi* V8::RandomPositiveSmi() { |
| 170 | uint32_t random = Random(); |
| 171 | ASSERT(IsPowerOf2(Smi::kMaxValue + 1)); |
| 172 | return Smi::FromInt(random & Smi::kMaxValue); |
| 173 | } |
| 174 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 175 | } } // namespace v8::internal |