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 | |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 30 | #include "compiler.h" |
| 31 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 32 | #include "bootstrapper.h" |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 33 | #include "codegen.h" |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 34 | #include "compilation-cache.h" |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 35 | #include "cpu-profiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | #include "debug.h" |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 37 | #include "deoptimizer.h" |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 38 | #include "full-codegen.h" |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 39 | #include "gdb-jit.h" |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 40 | #include "typing.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 41 | #include "hydrogen.h" |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 42 | #include "isolate-inl.h" |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 43 | #include "lithium.h" |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 44 | #include "liveedit.h" |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 45 | #include "parser.h" |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 46 | #include "rewriter.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 47 | #include "runtime-profiler.h" |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 48 | #include "scanner-character-streams.h" |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 49 | #include "scopeinfo.h" |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 50 | #include "scopes.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 51 | #include "vm-state-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 53 | namespace v8 { |
| 54 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 55 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 56 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 57 | CompilationInfo::CompilationInfo(Handle<Script> script, |
| 58 | Zone* zone) |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 59 | : flags_(LanguageModeField::encode(CLASSIC_MODE)), |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 60 | script_(script), |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 61 | osr_ast_id_(BailoutId::None()), |
| 62 | osr_pc_offset_(0) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 63 | Initialize(script->GetIsolate(), BASE, zone); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 67 | CompilationInfo::CompilationInfo(Handle<SharedFunctionInfo> shared_info, |
| 68 | Zone* zone) |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 69 | : flags_(LanguageModeField::encode(CLASSIC_MODE) | IsLazy::encode(true)), |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 70 | shared_info_(shared_info), |
| 71 | script_(Handle<Script>(Script::cast(shared_info->script()))), |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 72 | osr_ast_id_(BailoutId::None()), |
| 73 | osr_pc_offset_(0) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 74 | Initialize(script_->GetIsolate(), BASE, zone); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 78 | CompilationInfo::CompilationInfo(Handle<JSFunction> closure, |
| 79 | Zone* zone) |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 80 | : flags_(LanguageModeField::encode(CLASSIC_MODE) | IsLazy::encode(true)), |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 81 | closure_(closure), |
| 82 | shared_info_(Handle<SharedFunctionInfo>(closure->shared())), |
| 83 | script_(Handle<Script>(Script::cast(shared_info_->script()))), |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 84 | context_(closure->context()), |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 85 | osr_ast_id_(BailoutId::None()), |
| 86 | osr_pc_offset_(0) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 87 | Initialize(script_->GetIsolate(), BASE, zone); |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 91 | CompilationInfo::CompilationInfo(HydrogenCodeStub* stub, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 92 | Isolate* isolate, |
| 93 | Zone* zone) |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 94 | : flags_(LanguageModeField::encode(CLASSIC_MODE) | |
| 95 | IsLazy::encode(true)), |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 96 | osr_ast_id_(BailoutId::None()), |
| 97 | osr_pc_offset_(0) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 98 | Initialize(isolate, STUB, zone); |
| 99 | code_stub_ = stub; |
| 100 | } |
| 101 | |
| 102 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 103 | void CompilationInfo::Initialize(Isolate* isolate, |
| 104 | Mode mode, |
| 105 | Zone* zone) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 106 | isolate_ = isolate; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 107 | function_ = NULL; |
| 108 | scope_ = NULL; |
| 109 | global_scope_ = NULL; |
| 110 | extension_ = NULL; |
| 111 | pre_parse_data_ = NULL; |
| 112 | zone_ = zone; |
| 113 | deferred_handles_ = NULL; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 114 | code_stub_ = NULL; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 115 | prologue_offset_ = kPrologueOffsetNotSet; |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 116 | opt_count_ = shared_info().is_null() ? 0 : shared_info()->opt_count(); |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 117 | no_frame_ranges_ = isolate->cpu_profiler()->is_profiling() |
| 118 | ? new List<OffsetRange>(2) : NULL; |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 119 | for (int i = 0; i < DependentCode::kGroupCount; i++) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 120 | dependencies_[i] = NULL; |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 121 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 122 | if (mode == STUB) { |
| 123 | mode_ = STUB; |
| 124 | return; |
| 125 | } |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 126 | mode_ = isolate->use_crankshaft() ? mode : NONOPT; |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 127 | abort_due_to_dependency_ = false; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 128 | if (script_->type()->value() == Script::TYPE_NATIVE) { |
| 129 | MarkAsNative(); |
| 130 | } |
| 131 | if (!shared_info_.is_null()) { |
| 132 | ASSERT(language_mode() == CLASSIC_MODE); |
| 133 | SetLanguageMode(shared_info_->language_mode()); |
| 134 | } |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 135 | set_bailout_reason(kUnknown); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 139 | CompilationInfo::~CompilationInfo() { |
| 140 | delete deferred_handles_; |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 141 | delete no_frame_ranges_; |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 142 | #ifdef DEBUG |
| 143 | // Check that no dependent maps have been added or added dependent maps have |
| 144 | // been rolled back or committed. |
| 145 | for (int i = 0; i < DependentCode::kGroupCount; i++) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 146 | ASSERT_EQ(NULL, dependencies_[i]); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 147 | } |
| 148 | #endif // DEBUG |
| 149 | } |
| 150 | |
| 151 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 152 | void CompilationInfo::CommitDependencies(Handle<Code> code) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 153 | for (int i = 0; i < DependentCode::kGroupCount; i++) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 154 | ZoneList<Handle<HeapObject> >* group_objects = dependencies_[i]; |
| 155 | if (group_objects == NULL) continue; |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 156 | ASSERT(!object_wrapper_.is_null()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 157 | for (int j = 0; j < group_objects->length(); j++) { |
| 158 | DependentCode::DependencyGroup group = |
| 159 | static_cast<DependentCode::DependencyGroup>(i); |
| 160 | DependentCode* dependent_code = |
| 161 | DependentCode::ForObject(group_objects->at(j), group); |
| 162 | dependent_code->UpdateToFinishedCode(group, this, *code); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 163 | } |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 164 | dependencies_[i] = NULL; // Zone-allocated, no need to delete. |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 169 | void CompilationInfo::RollbackDependencies() { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 170 | // Unregister from all dependent maps if not yet committed. |
| 171 | for (int i = 0; i < DependentCode::kGroupCount; i++) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 172 | ZoneList<Handle<HeapObject> >* group_objects = dependencies_[i]; |
| 173 | if (group_objects == NULL) continue; |
| 174 | for (int j = 0; j < group_objects->length(); j++) { |
| 175 | DependentCode::DependencyGroup group = |
| 176 | static_cast<DependentCode::DependencyGroup>(i); |
| 177 | DependentCode* dependent_code = |
| 178 | DependentCode::ForObject(group_objects->at(j), group); |
| 179 | dependent_code->RemoveCompilationInfo(group, this); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 180 | } |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 181 | dependencies_[i] = NULL; // Zone-allocated, no need to delete. |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 182 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 186 | int CompilationInfo::num_parameters() const { |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 187 | ASSERT(!IsStub()); |
| 188 | return scope()->num_parameters(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
| 192 | int CompilationInfo::num_heap_slots() const { |
| 193 | if (IsStub()) { |
| 194 | return 0; |
| 195 | } else { |
| 196 | return scope()->num_heap_slots(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | |
| 201 | Code::Flags CompilationInfo::flags() const { |
| 202 | if (IsStub()) { |
mstarzinger@chromium.org | b228be0 | 2013-04-18 14:56:59 +0000 | [diff] [blame] | 203 | return Code::ComputeFlags(code_stub()->GetCodeKind(), |
| 204 | code_stub()->GetICState(), |
| 205 | code_stub()->GetExtraICState(), |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 206 | code_stub()->GetStubType(), |
| 207 | code_stub()->GetStubFlags()); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 208 | } else { |
| 209 | return Code::ComputeFlags(Code::OPTIMIZED_FUNCTION); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 214 | // Disable optimization for the rest of the compilation pipeline. |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 215 | void CompilationInfo::DisableOptimization() { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 216 | bool is_optimizable_closure = |
| 217 | FLAG_optimize_closures && |
| 218 | closure_.is_null() && |
| 219 | !scope_->HasTrivialOuterContext() && |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 220 | !scope_->outer_scope_calls_non_strict_eval() && |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 221 | !scope_->inside_with(); |
| 222 | SetMode(is_optimizable_closure ? BASE : NONOPT); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 226 | // Primitive functions are unlikely to be picked up by the stack-walking |
| 227 | // profiler, so they trigger their own optimization when they're called |
| 228 | // for the SharedFunctionInfo::kCallsUntilPrimitiveOptimization-th time. |
| 229 | bool CompilationInfo::ShouldSelfOptimize() { |
| 230 | return FLAG_self_optimization && |
| 231 | FLAG_crankshaft && |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 232 | !function()->flags()->Contains(kDontSelfOptimize) && |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 233 | !function()->dont_optimize() && |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 234 | function()->scope()->AllowsLazyCompilation() && |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 235 | (shared_info().is_null() || !shared_info()->optimization_disabled()); |
| 236 | } |
| 237 | |
| 238 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 239 | // Determine whether to use the full compiler for all code. If the flag |
| 240 | // --always-full-compiler is specified this is the case. For the virtual frame |
| 241 | // based compiler the full compiler is also used if a debugger is connected, as |
| 242 | // the code from the full compiler supports mode precise break points. For the |
| 243 | // crankshaft adaptive compiler debugging the optimized code is not possible at |
| 244 | // all. However crankshaft support recompilation of functions, so in this case |
| 245 | // the full compiler need not be be used if a debugger is attached, but only if |
| 246 | // break points has actually been set. |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 247 | static bool IsDebuggerActive(Isolate* isolate) { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 248 | #ifdef ENABLE_DEBUGGER_SUPPORT |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 249 | return isolate->use_crankshaft() ? |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 250 | isolate->debug()->has_break_points() : |
| 251 | isolate->debugger()->IsDebuggerActive(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 252 | #else |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 253 | return false; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 254 | #endif |
| 255 | } |
| 256 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 257 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 258 | static bool AlwaysFullCompiler(Isolate* isolate) { |
| 259 | return FLAG_always_full_compiler || IsDebuggerActive(isolate); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 263 | void RecompileJob::RecordOptimizationStats() { |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 264 | Handle<JSFunction> function = info()->closure(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 265 | int opt_count = function->shared()->opt_count(); |
| 266 | function->shared()->set_opt_count(opt_count + 1); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 267 | double ms_creategraph = time_taken_to_create_graph_.InMillisecondsF(); |
| 268 | double ms_optimize = time_taken_to_optimize_.InMillisecondsF(); |
| 269 | double ms_codegen = time_taken_to_codegen_.InMillisecondsF(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 270 | if (FLAG_trace_opt) { |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 271 | PrintF("[optimizing "); |
| 272 | function->ShortPrint(); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 273 | PrintF(" - took %0.3f, %0.3f, %0.3f ms]\n", ms_creategraph, ms_optimize, |
| 274 | ms_codegen); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 275 | } |
| 276 | if (FLAG_trace_opt_stats) { |
| 277 | static double compilation_time = 0.0; |
| 278 | static int compiled_functions = 0; |
| 279 | static int code_size = 0; |
| 280 | |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 281 | compilation_time += (ms_creategraph + ms_optimize + ms_codegen); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 282 | compiled_functions++; |
| 283 | code_size += function->shared()->SourceSize(); |
| 284 | PrintF("Compiled: %d functions with %d byte source size in %fms.\n", |
| 285 | compiled_functions, |
| 286 | code_size, |
| 287 | compilation_time); |
| 288 | } |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 289 | if (FLAG_hydrogen_stats) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 290 | isolate()->GetHStatistics()->IncrementSubtotals(time_taken_to_create_graph_, |
| 291 | time_taken_to_optimize_, |
| 292 | time_taken_to_codegen_); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 293 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 297 | // A return value of true indicates the compilation pipeline is still |
| 298 | // going, not necessarily that we optimized the code. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 299 | static bool MakeCrankshaftCode(CompilationInfo* info) { |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 300 | RecompileJob job(info); |
| 301 | RecompileJob::Status status = job.CreateGraph(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 302 | |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 303 | if (status != RecompileJob::SUCCEEDED) { |
| 304 | return status != RecompileJob::FAILED; |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 305 | } |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 306 | status = job.OptimizeGraph(); |
| 307 | if (status != RecompileJob::SUCCEEDED) { |
| 308 | status = job.AbortOptimization(); |
| 309 | return status != RecompileJob::FAILED; |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 310 | } |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 311 | status = job.GenerateAndInstallCode(); |
| 312 | return status != RecompileJob::FAILED; |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 316 | RecompileJob::Status RecompileJob::CreateGraph() { |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 317 | ASSERT(isolate()->use_crankshaft()); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 318 | ASSERT(info()->IsOptimizing()); |
| 319 | ASSERT(!info()->IsCompilingForDebugging()); |
| 320 | |
| 321 | // We should never arrive here if there is no code object on the |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 322 | // shared function object. |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 323 | ASSERT(info()->shared_info()->code()->kind() == Code::FUNCTION); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 324 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 325 | // We should never arrive here if optimization has been disabled on the |
| 326 | // shared function info. |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 327 | ASSERT(!info()->shared_info()->optimization_disabled()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 328 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 329 | // Fall back to using the full code generator if it's not possible |
| 330 | // to use the Hydrogen-based optimizing compiler. We already have |
| 331 | // generated code for this from the shared function object. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 332 | if (AlwaysFullCompiler(isolate())) { |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 333 | info()->AbortOptimization(); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 334 | return SetLastStatus(BAILED_OUT); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // Limit the number of times we re-compile a functions with |
| 338 | // the optimizing compiler. |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 339 | const int kMaxOptCount = |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 340 | FLAG_deopt_every_n_times == 0 ? FLAG_max_opt_count : 1000; |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 341 | if (info()->opt_count() > kMaxOptCount) { |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 342 | info()->set_bailout_reason(kOptimizedTooManyTimes); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 343 | return AbortOptimization(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | // Due to an encoding limit on LUnallocated operands in the Lithium |
| 347 | // language, we cannot optimize functions with too many formal parameters |
| 348 | // or perform on-stack replacement for function with too many |
| 349 | // stack-allocated local variables. |
| 350 | // |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 351 | // The encoding is as a signed value, with parameters and receiver using |
| 352 | // the negative indices and locals the non-negative ones. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 353 | const int parameter_limit = -LUnallocated::kMinFixedSlotIndex; |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 354 | Scope* scope = info()->scope(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 355 | if ((scope->num_parameters() + 1) > parameter_limit) { |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 356 | info()->set_bailout_reason(kTooManyParameters); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 357 | return AbortOptimization(); |
| 358 | } |
| 359 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 360 | const int locals_limit = LUnallocated::kMaxFixedSlotIndex; |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 361 | if (info()->is_osr() && |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 362 | scope->num_parameters() + 1 + scope->num_stack_slots() > locals_limit) { |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 363 | info()->set_bailout_reason(kTooManyParametersLocals); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 364 | return AbortOptimization(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // Take --hydrogen-filter into account. |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 368 | if (!info()->closure()->PassesFilter(FLAG_hydrogen_filter)) { |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 369 | info()->AbortOptimization(); |
| 370 | return SetLastStatus(BAILED_OUT); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | // Recompile the unoptimized version of the code if the current version |
| 374 | // doesn't have deoptimization support. Alternatively, we may decide to |
| 375 | // run the full code generator to get a baseline for the compile-time |
| 376 | // performance of the hydrogen-based compiler. |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 377 | bool should_recompile = !info()->shared_info()->has_deoptimization_support(); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 378 | if (should_recompile || FLAG_hydrogen_stats) { |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 379 | ElapsedTimer timer; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 380 | if (FLAG_hydrogen_stats) { |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 381 | timer.Start(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 382 | } |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 383 | CompilationInfoWithZone unoptimized(info()->shared_info()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 384 | // Note that we use the same AST that we will use for generating the |
| 385 | // optimized code. |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 386 | unoptimized.SetFunction(info()->function()); |
| 387 | unoptimized.SetScope(info()->scope()); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 388 | unoptimized.SetContext(info()->context()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 389 | if (should_recompile) unoptimized.EnableDeoptimizationSupport(); |
| 390 | bool succeeded = FullCodeGenerator::MakeCode(&unoptimized); |
| 391 | if (should_recompile) { |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 392 | if (!succeeded) return SetLastStatus(FAILED); |
| 393 | Handle<SharedFunctionInfo> shared = info()->shared_info(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 394 | shared->EnableDeoptimizationSupport(*unoptimized.code()); |
| 395 | // The existing unoptimized code was replaced with the new one. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 396 | Compiler::RecordFunctionCompilation( |
| 397 | Logger::LAZY_COMPILE_TAG, &unoptimized, shared); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 398 | } |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 399 | if (FLAG_hydrogen_stats) { |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 400 | isolate()->GetHStatistics()->IncrementFullCodeGen(timer.Elapsed()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 401 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | // Check that the unoptimized, shared code is ready for |
| 405 | // optimizations. When using the always_opt flag we disregard the |
| 406 | // optimizable marker in the code object and optimize anyway. This |
| 407 | // is safe as long as the unoptimized code has deoptimization |
| 408 | // support. |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 409 | ASSERT(FLAG_always_opt || info()->shared_info()->code()->optimizable()); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 410 | ASSERT(info()->shared_info()->has_deoptimization_support()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 411 | |
| 412 | if (FLAG_trace_hydrogen) { |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 413 | Handle<String> name = info()->function()->debug_name(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 414 | PrintF("-----------------------------------------------------------\n"); |
| 415 | PrintF("Compiling method %s using hydrogen\n", *name->ToCString()); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 416 | isolate()->GetHTracer()->TraceCompilation(info()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 417 | } |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 418 | |
| 419 | // Type-check the function. |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 420 | AstTyper::Run(info()); |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 421 | |
| 422 | graph_builder_ = new(info()->zone()) HOptimizedGraphBuilder(info()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 423 | |
| 424 | Timer t(this, &time_taken_to_create_graph_); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 425 | graph_ = graph_builder_->CreateGraph(); |
| 426 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 427 | if (isolate()->has_pending_exception()) { |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 428 | info()->SetCode(Handle<Code>::null()); |
| 429 | return SetLastStatus(FAILED); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 430 | } |
| 431 | |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 432 | // The function being compiled may have bailed out due to an inline |
| 433 | // candidate bailing out. In such a case, we don't disable |
| 434 | // optimization on the shared_info. |
| 435 | ASSERT(!graph_builder_->inline_bailout() || graph_ == NULL); |
| 436 | if (graph_ == NULL) { |
| 437 | if (graph_builder_->inline_bailout()) { |
| 438 | info_->AbortOptimization(); |
| 439 | return SetLastStatus(BAILED_OUT); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 440 | } else { |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 441 | return AbortOptimization(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 445 | if (info()->HasAbortedDueToDependencyChange()) { |
| 446 | info_->set_bailout_reason(kBailedOutDueToDependencyChange); |
| 447 | info_->AbortOptimization(); |
| 448 | return SetLastStatus(BAILED_OUT); |
| 449 | } |
| 450 | |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 451 | return SetLastStatus(SUCCEEDED); |
| 452 | } |
| 453 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 454 | |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 455 | RecompileJob::Status RecompileJob::OptimizeGraph() { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 456 | DisallowHeapAllocation no_allocation; |
| 457 | DisallowHandleAllocation no_handles; |
| 458 | DisallowHandleDereference no_deref; |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 459 | DisallowCodeDependencyChange no_dependency_change; |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 460 | |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 461 | ASSERT(last_status() == SUCCEEDED); |
| 462 | Timer t(this, &time_taken_to_optimize_); |
| 463 | ASSERT(graph_ != NULL); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 464 | BailoutReason bailout_reason = kNoReason; |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 465 | if (!graph_->Optimize(&bailout_reason)) { |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 466 | if (bailout_reason == kNoReason) graph_builder_->Bailout(bailout_reason); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 467 | return SetLastStatus(BAILED_OUT); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 468 | } else { |
| 469 | chunk_ = LChunk::NewChunk(graph_); |
| 470 | if (chunk_ == NULL) { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 471 | return SetLastStatus(BAILED_OUT); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 472 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 473 | } |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 474 | return SetLastStatus(SUCCEEDED); |
| 475 | } |
| 476 | |
| 477 | |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 478 | RecompileJob::Status RecompileJob::GenerateAndInstallCode() { |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 479 | ASSERT(last_status() == SUCCEEDED); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 480 | ASSERT(!info()->HasAbortedDueToDependencyChange()); |
| 481 | DisallowCodeDependencyChange no_dependency_change; |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 482 | { // Scope for timer. |
| 483 | Timer timer(this, &time_taken_to_codegen_); |
| 484 | ASSERT(chunk_ != NULL); |
| 485 | ASSERT(graph_ != NULL); |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 486 | // Deferred handles reference objects that were accessible during |
| 487 | // graph creation. To make sure that we don't encounter inconsistencies |
| 488 | // between graph creation and code generation, we disallow accessing |
| 489 | // objects through deferred handles during the latter, with exceptions. |
yangguo@chromium.org | 2030124 | 2013-06-03 16:06:25 +0000 | [diff] [blame] | 490 | DisallowDeferredHandleDereference no_deferred_handle_deref; |
mstarzinger@chromium.org | b228be0 | 2013-04-18 14:56:59 +0000 | [diff] [blame] | 491 | Handle<Code> optimized_code = chunk_->Codegen(); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 492 | if (optimized_code.is_null()) { |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 493 | if (info()->bailout_reason() == kNoReason) { |
| 494 | info()->set_bailout_reason(kCodeGenerationFailed); |
| 495 | } |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 496 | return AbortOptimization(); |
| 497 | } |
| 498 | info()->SetCode(optimized_code); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 499 | } |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 500 | RecordOptimizationStats(); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 501 | // Add to the weak list of optimized code objects. |
| 502 | info()->context()->native_context()->AddOptimizedCode(*info()->code()); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 503 | return SetLastStatus(SUCCEEDED); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 507 | static bool GenerateCode(CompilationInfo* info) { |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 508 | bool is_optimizing = info->isolate()->use_crankshaft() && |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 509 | !info->IsCompilingForDebugging() && |
| 510 | info->IsOptimizing(); |
| 511 | if (is_optimizing) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 512 | Logger::TimerEventScope timer( |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 513 | info->isolate(), Logger::TimerEventScope::v8_recompile_synchronous); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 514 | return MakeCrankshaftCode(info); |
| 515 | } else { |
| 516 | if (info->IsOptimizing()) { |
| 517 | // Have the CompilationInfo decide if the compilation should be |
| 518 | // BASE or NONOPT. |
| 519 | info->DisableOptimization(); |
| 520 | } |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 521 | Logger::TimerEventScope timer( |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 522 | info->isolate(), Logger::TimerEventScope::v8_compile_full_code); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 523 | return FullCodeGenerator::MakeCode(info); |
| 524 | } |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 528 | static bool MakeCode(CompilationInfo* info) { |
| 529 | // Precondition: code has been parsed. Postcondition: the code field in |
| 530 | // the compilation info is set if compilation succeeded. |
| 531 | ASSERT(info->function() != NULL); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 532 | return Rewriter::Rewrite(info) && Scope::Analyze(info) && GenerateCode(info); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 536 | #ifdef ENABLE_DEBUGGER_SUPPORT |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 537 | bool Compiler::MakeCodeForLiveEdit(CompilationInfo* info) { |
| 538 | // Precondition: code has been parsed. Postcondition: the code field in |
| 539 | // the compilation info is set if compilation succeeded. |
| 540 | bool succeeded = MakeCode(info); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 541 | if (!info->shared_info().is_null()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 542 | Handle<ScopeInfo> scope_info = ScopeInfo::Create(info->scope(), |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 543 | info->zone()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 544 | info->shared_info()->set_scope_info(*scope_info); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 545 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 546 | return succeeded; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 547 | } |
| 548 | #endif |
| 549 | |
| 550 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 551 | static bool DebuggerWantsEagerCompilation(CompilationInfo* info, |
| 552 | bool allow_lazy_without_ctx = false) { |
| 553 | return LiveEditFunctionTracker::IsActive(info->isolate()) || |
| 554 | (info->isolate()->DebuggerHasBreakPoints() && !allow_lazy_without_ctx); |
| 555 | } |
| 556 | |
| 557 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 558 | static Handle<SharedFunctionInfo> MakeFunctionInfo(CompilationInfo* info) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 559 | Isolate* isolate = info->isolate(); |
| 560 | PostponeInterruptsScope postpone(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 561 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 562 | ASSERT(!isolate->native_context().is_null()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 563 | Handle<Script> script = info->script(); |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 564 | // TODO(svenpanne) Obscure place for this, perhaps move to OnBeforeCompile? |
| 565 | FixedArray* array = isolate->native_context()->embedder_data(); |
| 566 | script->set_context_data(array->get(0)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 567 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 568 | #ifdef ENABLE_DEBUGGER_SUPPORT |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 569 | if (info->is_eval()) { |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 570 | script->set_compilation_type(Script::COMPILATION_TYPE_EVAL); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 571 | // For eval scripts add information on the function from which eval was |
| 572 | // called. |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 573 | if (info->is_eval()) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 574 | StackTraceFrameIterator it(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 575 | if (!it.done()) { |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 576 | script->set_eval_from_shared(it.frame()->function()->shared()); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 577 | Code* code = it.frame()->LookupCode(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 578 | int offset = static_cast<int>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 579 | it.frame()->pc() - code->instruction_start()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 580 | script->set_eval_from_instructions_offset(Smi::FromInt(offset)); |
| 581 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 585 | // Notify debugger |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 586 | isolate->debugger()->OnBeforeCompile(script); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 587 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 588 | |
| 589 | // Only allow non-global compiles for eval. |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 590 | ASSERT(info->is_eval() || info->is_global()); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 591 | { |
| 592 | Parser parser(info); |
| 593 | if ((info->pre_parse_data() != NULL || |
| 594 | String::cast(script->source())->length() > FLAG_min_preparse_length) && |
| 595 | !DebuggerWantsEagerCompilation(info)) |
| 596 | parser.set_allow_lazy(true); |
| 597 | if (!parser.Parse()) { |
| 598 | return Handle<SharedFunctionInfo>::null(); |
| 599 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 600 | } |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 601 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 602 | // Measure how long it takes to do the compilation; only take the |
| 603 | // rest of the function into account to avoid overlap with the |
| 604 | // parsing statistics. |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 605 | HistogramTimer* rate = info->is_eval() |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 606 | ? info->isolate()->counters()->compile_eval() |
| 607 | : info->isolate()->counters()->compile(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 608 | HistogramTimerScope timer(rate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 609 | |
| 610 | // Compile the code. |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 611 | FunctionLiteral* lit = info->function(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 612 | LiveEditFunctionTracker live_edit_tracker(isolate, lit); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 613 | if (!MakeCode(info)) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 614 | if (!isolate->has_pending_exception()) isolate->StackOverflow(); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 615 | return Handle<SharedFunctionInfo>::null(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 616 | } |
| 617 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 618 | // Allocate function. |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 619 | ASSERT(!info->code().is_null()); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 620 | Handle<SharedFunctionInfo> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 621 | isolate->factory()->NewSharedFunctionInfo( |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 622 | lit->name(), |
| 623 | lit->materialized_literal_count(), |
mstarzinger@chromium.org | b228be0 | 2013-04-18 14:56:59 +0000 | [diff] [blame] | 624 | lit->is_generator(), |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 625 | info->code(), |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 626 | ScopeInfo::Create(info->scope(), info->zone())); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 627 | |
| 628 | ASSERT_EQ(RelocInfo::kNoPosition, lit->function_token_position()); |
| 629 | Compiler::SetFunctionInfo(result, lit, true, script); |
| 630 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 631 | if (script->name()->IsString()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 632 | PROFILE(isolate, CodeCreateEvent( |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 633 | info->is_eval() |
| 634 | ? Logger::EVAL_TAG |
| 635 | : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script), |
| 636 | *info->code(), |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 637 | *result, |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 638 | info, |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 639 | String::cast(script->name()))); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 640 | GDBJIT(AddCode(Handle<String>(String::cast(script->name())), |
| 641 | script, |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 642 | info->code(), |
| 643 | info)); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 644 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 645 | PROFILE(isolate, CodeCreateEvent( |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 646 | info->is_eval() |
| 647 | ? Logger::EVAL_TAG |
| 648 | : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script), |
| 649 | *info->code(), |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 650 | *result, |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 651 | info, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 652 | isolate->heap()->empty_string())); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 653 | GDBJIT(AddCode(Handle<String>(), script, info->code(), info)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 654 | } |
| 655 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 656 | // Hint to the runtime system used when allocating space for initial |
| 657 | // property space by setting the expected number of properties for |
| 658 | // the instances of the function. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 659 | SetExpectedNofPropertiesFromEstimate(result, lit->expected_property_count()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 660 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 661 | script->set_compilation_state(Script::COMPILATION_STATE_COMPILED); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 662 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 663 | #ifdef ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 664 | // Notify debugger |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 665 | isolate->debugger()->OnAfterCompile( |
| 666 | script, Debugger::NO_AFTER_COMPILE_FLAGS); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 667 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 668 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 669 | live_edit_tracker.RecordFunctionInfo(result, lit, info->zone()); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 670 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 671 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 675 | Handle<SharedFunctionInfo> Compiler::Compile(Handle<String> source, |
| 676 | Handle<Object> script_name, |
| 677 | int line_offset, |
| 678 | int column_offset, |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 679 | bool is_shared_cross_origin, |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 680 | Handle<Context> context, |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 681 | v8::Extension* extension, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 682 | ScriptDataImpl* pre_data, |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 683 | Handle<Object> script_data, |
| 684 | NativesFlag natives) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 685 | Isolate* isolate = source->GetIsolate(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 686 | int source_length = source->length(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 687 | isolate->counters()->total_load_size()->Increment(source_length); |
| 688 | isolate->counters()->total_compile_size()->Increment(source_length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 689 | |
| 690 | // The VM is in the COMPILER state until exiting this function. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 691 | VMState<COMPILER> state(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 692 | |
| 693 | CompilationCache* compilation_cache = isolate->compilation_cache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 694 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 695 | // Do a lookup in the compilation cache but not for extensions. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 696 | Handle<SharedFunctionInfo> result; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 697 | if (extension == NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 698 | result = compilation_cache->LookupScript(source, |
| 699 | script_name, |
| 700 | line_offset, |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 701 | column_offset, |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 702 | is_shared_cross_origin, |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 703 | context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 704 | } |
| 705 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 706 | if (result.is_null()) { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 707 | // No cache entry found. Do pre-parsing, if it makes sense, and compile |
| 708 | // the script. |
| 709 | // Building preparse data that is only used immediately after is only a |
| 710 | // saving if we might skip building the AST for lazily compiled functions. |
| 711 | // I.e., preparse data isn't relevant when the lazy flag is off, and |
| 712 | // for small sources, odds are that there aren't many functions |
| 713 | // that would be compiled lazily anyway, so we skip the preparse step |
| 714 | // in that case too. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 715 | |
| 716 | // Create a script object describing the script to be compiled. |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 717 | Handle<Script> script = isolate->factory()->NewScript(source); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 718 | if (natives == NATIVES_CODE) { |
| 719 | script->set_type(Smi::FromInt(Script::TYPE_NATIVE)); |
| 720 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 721 | if (!script_name.is_null()) { |
| 722 | script->set_name(*script_name); |
| 723 | script->set_line_offset(Smi::FromInt(line_offset)); |
| 724 | script->set_column_offset(Smi::FromInt(column_offset)); |
| 725 | } |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 726 | script->set_is_shared_cross_origin(is_shared_cross_origin); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 727 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 728 | script->set_data(script_data.is_null() ? isolate->heap()->undefined_value() |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 729 | : *script_data); |
| 730 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 731 | // Compile the function and add it to the cache. |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 732 | CompilationInfoWithZone info(script); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 733 | info.MarkAsGlobal(); |
| 734 | info.SetExtension(extension); |
| 735 | info.SetPreParseData(pre_data); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 736 | info.SetContext(context); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 737 | if (FLAG_use_strict) { |
| 738 | info.SetLanguageMode(FLAG_harmony_scoping ? EXTENDED_MODE : STRICT_MODE); |
| 739 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 740 | result = MakeFunctionInfo(&info); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 741 | if (extension == NULL && !result.is_null() && !result->dont_cache()) { |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 742 | compilation_cache->PutScript(source, context, result); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 743 | } |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 744 | } else { |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 745 | if (result->ic_age() != isolate->heap()->global_ic_age()) { |
| 746 | result->ResetForNewContext(isolate->heap()->global_ic_age()); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 747 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 748 | } |
| 749 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 750 | if (result.is_null()) isolate->ReportPendingMessages(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 751 | return result; |
| 752 | } |
| 753 | |
| 754 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 755 | Handle<SharedFunctionInfo> Compiler::CompileEval(Handle<String> source, |
| 756 | Handle<Context> context, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 757 | bool is_global, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 758 | LanguageMode language_mode, |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 759 | ParseRestriction restriction, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 760 | int scope_position) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 761 | Isolate* isolate = source->GetIsolate(); |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 762 | int source_length = source->length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 763 | isolate->counters()->total_eval_size()->Increment(source_length); |
| 764 | isolate->counters()->total_compile_size()->Increment(source_length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 765 | |
| 766 | // The VM is in the COMPILER state until exiting this function. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 767 | VMState<COMPILER> state(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 768 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 769 | // Do a lookup in the compilation cache; if the entry is not there, invoke |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 770 | // the compiler and add the result to the cache. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 771 | Handle<SharedFunctionInfo> result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 772 | CompilationCache* compilation_cache = isolate->compilation_cache(); |
| 773 | result = compilation_cache->LookupEval(source, |
| 774 | context, |
| 775 | is_global, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 776 | language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 777 | scope_position); |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 778 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 779 | if (result.is_null()) { |
| 780 | // Create a script object describing the script to be compiled. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 781 | Handle<Script> script = isolate->factory()->NewScript(source); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 782 | CompilationInfoWithZone info(script); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 783 | info.MarkAsEval(); |
| 784 | if (is_global) info.MarkAsGlobal(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 785 | info.SetLanguageMode(language_mode); |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 786 | info.SetParseRestriction(restriction); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 787 | info.SetContext(context); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 788 | result = MakeFunctionInfo(&info); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 789 | if (!result.is_null()) { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 790 | // Explicitly disable optimization for eval code. We're not yet prepared |
| 791 | // to handle eval-code in the optimizing compiler. |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 792 | result->DisableOptimization(kEval); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 793 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 794 | // If caller is strict mode, the result must be in strict mode or |
| 795 | // extended mode as well, but not the other way around. Consider: |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 796 | // eval("'use strict'; ..."); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 797 | ASSERT(language_mode != STRICT_MODE || !result->is_classic_mode()); |
| 798 | // If caller is in extended mode, the result must also be in |
| 799 | // extended mode. |
| 800 | ASSERT(language_mode != EXTENDED_MODE || |
| 801 | result->is_extended_mode()); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 802 | if (!result->dont_cache()) { |
| 803 | compilation_cache->PutEval( |
| 804 | source, context, is_global, result, scope_position); |
| 805 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 806 | } |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 807 | } else { |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 808 | if (result->ic_age() != isolate->heap()->global_ic_age()) { |
| 809 | result->ResetForNewContext(isolate->heap()->global_ic_age()); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 810 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 811 | } |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 812 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 813 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 817 | static bool InstallFullCode(CompilationInfo* info) { |
| 818 | // Update the shared function info with the compiled code and the |
| 819 | // scope info. Please note, that the order of the shared function |
| 820 | // info initialization is important since set_scope_info might |
| 821 | // trigger a GC, causing the ASSERT below to be invalid if the code |
| 822 | // was flushed. By setting the code object last we avoid this. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 823 | Handle<SharedFunctionInfo> shared = info->shared_info(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 824 | Handle<Code> code = info->code(); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 825 | CHECK(code->kind() == Code::FUNCTION); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 826 | Handle<JSFunction> function = info->closure(); |
| 827 | Handle<ScopeInfo> scope_info = |
| 828 | ScopeInfo::Create(info->scope(), info->zone()); |
| 829 | shared->set_scope_info(*scope_info); |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 830 | shared->ReplaceCode(*code); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 831 | if (!function.is_null()) { |
| 832 | function->ReplaceCode(*code); |
| 833 | ASSERT(!function->IsOptimized()); |
| 834 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 835 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 836 | // Set the expected number of properties for instances. |
| 837 | FunctionLiteral* lit = info->function(); |
| 838 | int expected = lit->expected_property_count(); |
| 839 | SetExpectedNofPropertiesFromEstimate(shared, expected); |
| 840 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 841 | // Check the function has compiled code. |
| 842 | ASSERT(shared->is_compiled()); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 843 | shared->set_dont_optimize_reason(lit->dont_optimize_reason()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 844 | shared->set_dont_inline(lit->flags()->Contains(kDontInline)); |
| 845 | shared->set_ast_node_count(lit->ast_node_count()); |
| 846 | |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 847 | if (info->isolate()->use_crankshaft() && |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 848 | !function.is_null() && |
| 849 | !shared->optimization_disabled()) { |
| 850 | // If we're asked to always optimize, we compile the optimized |
| 851 | // version of the function right away - unless the debugger is |
| 852 | // active as it makes no sense to compile optimized code then. |
| 853 | if (FLAG_always_opt && |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 854 | !info->isolate()->DebuggerHasBreakPoints()) { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 855 | CompilationInfoWithZone optimized(function); |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 856 | optimized.SetOptimizing(BailoutId::None()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 857 | return Compiler::CompileLazy(&optimized); |
| 858 | } |
| 859 | } |
| 860 | return true; |
| 861 | } |
| 862 | |
| 863 | |
| 864 | static void InstallCodeCommon(CompilationInfo* info) { |
| 865 | Handle<SharedFunctionInfo> shared = info->shared_info(); |
| 866 | Handle<Code> code = info->code(); |
| 867 | ASSERT(!code.is_null()); |
| 868 | |
| 869 | // Set optimizable to false if this is disallowed by the shared |
| 870 | // function info, e.g., we might have flushed the code and must |
| 871 | // reset this bit when lazy compiling the code again. |
| 872 | if (shared->optimization_disabled()) code->set_optimizable(false); |
| 873 | |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 874 | if (shared->code() == *code) { |
| 875 | // Do not send compilation event for the same code twice. |
| 876 | return; |
| 877 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 878 | Compiler::RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, info, shared); |
| 879 | } |
| 880 | |
| 881 | |
| 882 | static void InsertCodeIntoOptimizedCodeMap(CompilationInfo* info) { |
| 883 | Handle<Code> code = info->code(); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 884 | if (code->kind() != Code::OPTIMIZED_FUNCTION) return; // Nothing to do. |
| 885 | |
| 886 | // Cache non-OSR optimized code. |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 887 | if (FLAG_cache_optimized_code && !info->is_osr()) { |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 888 | Handle<JSFunction> function = info->closure(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 889 | Handle<SharedFunctionInfo> shared(function->shared()); |
| 890 | Handle<FixedArray> literals(function->literals()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 891 | Handle<Context> native_context(function->context()->native_context()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 892 | SharedFunctionInfo::AddToOptimizedCodeMap( |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 893 | shared, native_context, code, literals); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
| 897 | |
| 898 | static bool InstallCodeFromOptimizedCodeMap(CompilationInfo* info) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 899 | if (!info->IsOptimizing()) return false; // Nothing to look up. |
| 900 | |
| 901 | // Lookup non-OSR optimized code. |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 902 | if (FLAG_cache_optimized_code && !info->is_osr()) { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 903 | Handle<SharedFunctionInfo> shared = info->shared_info(); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 904 | Handle<JSFunction> function = info->closure(); |
| 905 | ASSERT(!function.is_null()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 906 | Handle<Context> native_context(function->context()->native_context()); |
| 907 | int index = shared->SearchOptimizedCodeMap(*native_context); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 908 | if (index > 0) { |
| 909 | if (FLAG_trace_opt) { |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 910 | PrintF("[found optimized code for "); |
| 911 | function->ShortPrint(); |
| 912 | PrintF("]\n"); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 913 | } |
ulan@chromium.org | d9e468a | 2012-06-25 09:47:40 +0000 | [diff] [blame] | 914 | // Caching of optimized code enabled and optimized code found. |
| 915 | shared->InstallFromOptimizedCodeMap(*function, index); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 916 | return true; |
| 917 | } |
| 918 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 919 | return false; |
| 920 | } |
| 921 | |
| 922 | |
| 923 | bool Compiler::CompileLazy(CompilationInfo* info) { |
| 924 | Isolate* isolate = info->isolate(); |
| 925 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 926 | // The VM is in the COMPILER state until exiting this function. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 927 | VMState<COMPILER> state(isolate); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 928 | |
| 929 | PostponeInterruptsScope postpone(isolate); |
| 930 | |
| 931 | Handle<SharedFunctionInfo> shared = info->shared_info(); |
| 932 | int compiled_size = shared->end_position() - shared->start_position(); |
| 933 | isolate->counters()->total_compile_size()->Increment(compiled_size); |
| 934 | |
| 935 | if (InstallCodeFromOptimizedCodeMap(info)) return true; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 936 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 937 | // Generate the AST for the lazily compiled function. |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 938 | if (Parser::Parse(info)) { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 939 | // Measure how long it takes to do the lazy compilation; only take the |
| 940 | // rest of the function into account to avoid overlap with the lazy |
| 941 | // parsing statistics. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 942 | HistogramTimerScope timer(isolate->counters()->compile_lazy()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 943 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 944 | // After parsing we know the function's language mode. Remember it. |
| 945 | LanguageMode language_mode = info->function()->language_mode(); |
| 946 | info->SetLanguageMode(language_mode); |
| 947 | shared->set_language_mode(language_mode); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 948 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 949 | // Compile the code. |
| 950 | if (!MakeCode(info)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 951 | if (!isolate->has_pending_exception()) { |
| 952 | isolate->StackOverflow(); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 953 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 954 | } else { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 955 | InstallCodeCommon(info); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 956 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 957 | if (info->IsOptimizing()) { |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 958 | // Optimized code successfully created. |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 959 | Handle<Code> code = info->code(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 960 | ASSERT(shared->scope_info() != ScopeInfo::Empty(isolate)); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 961 | // TODO(titzer): Only replace the code if it was not an OSR compile. |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 962 | info->closure()->ReplaceCode(*code); |
| 963 | InsertCodeIntoOptimizedCodeMap(info); |
| 964 | return true; |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 965 | } else if (!info->is_osr()) { |
| 966 | // Compilation failed. Replace with full code if not OSR compile. |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 967 | return InstallFullCode(info); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 968 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 969 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 970 | } |
| 971 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 972 | ASSERT(info->code().is_null()); |
| 973 | return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 977 | bool Compiler::RecompileConcurrent(Handle<JSFunction> closure, |
| 978 | uint32_t osr_pc_offset) { |
| 979 | bool compiling_for_osr = (osr_pc_offset != 0); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 980 | |
| 981 | Isolate* isolate = closure->GetIsolate(); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 982 | // Here we prepare compile data for the concurrent recompilation thread, but |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 983 | // this still happens synchronously and interrupts execution. |
| 984 | Logger::TimerEventScope timer( |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 985 | isolate, Logger::TimerEventScope::v8_recompile_synchronous); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 986 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 987 | if (!isolate->optimizing_compiler_thread()->IsQueueAvailable()) { |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 988 | if (FLAG_trace_concurrent_recompilation) { |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 989 | PrintF(" ** Compilation queue full, will retry optimizing "); |
| 990 | closure->PrintName(); |
| 991 | PrintF(" on next run.\n"); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 992 | } |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 993 | return false; |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | SmartPointer<CompilationInfo> info(new CompilationInfoWithZone(closure)); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 997 | Handle<SharedFunctionInfo> shared = info->shared_info(); |
| 998 | |
| 999 | if (compiling_for_osr) { |
| 1000 | BailoutId osr_ast_id = |
| 1001 | shared->code()->TranslatePcOffsetToAstId(osr_pc_offset); |
| 1002 | ASSERT(!osr_ast_id.IsNone()); |
| 1003 | info->SetOptimizing(osr_ast_id); |
| 1004 | info->set_osr_pc_offset(osr_pc_offset); |
| 1005 | |
| 1006 | if (FLAG_trace_osr) { |
| 1007 | PrintF("[COSR - attempt to queue "); |
| 1008 | closure->PrintName(); |
| 1009 | PrintF(" at AST id %d]\n", osr_ast_id.ToInt()); |
| 1010 | } |
| 1011 | } else { |
| 1012 | info->SetOptimizing(BailoutId::None()); |
| 1013 | } |
| 1014 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 1015 | VMState<COMPILER> state(isolate); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1016 | PostponeInterruptsScope postpone(isolate); |
| 1017 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1018 | int compiled_size = shared->end_position() - shared->start_position(); |
| 1019 | isolate->counters()->total_compile_size()->Increment(compiled_size); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1020 | |
| 1021 | { |
| 1022 | CompilationHandleScope handle_scope(*info); |
| 1023 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1024 | if (!compiling_for_osr && InstallCodeFromOptimizedCodeMap(*info)) { |
| 1025 | return true; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 1026 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1027 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1028 | if (Parser::Parse(*info)) { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1029 | LanguageMode language_mode = info->function()->language_mode(); |
| 1030 | info->SetLanguageMode(language_mode); |
| 1031 | shared->set_language_mode(language_mode); |
| 1032 | info->SaveHandles(); |
| 1033 | |
| 1034 | if (Rewriter::Rewrite(*info) && Scope::Analyze(*info)) { |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1035 | RecompileJob* job = new(info->zone()) RecompileJob(*info); |
| 1036 | RecompileJob::Status status = job->CreateGraph(); |
| 1037 | if (status == RecompileJob::SUCCEEDED) { |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 1038 | info.Detach(); |
| 1039 | shared->code()->set_profiler_ticks(0); |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1040 | isolate->optimizing_compiler_thread()->QueueForOptimization(job); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1041 | ASSERT(!isolate->has_pending_exception()); |
| 1042 | return true; |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1043 | } else if (status == RecompileJob::BAILED_OUT) { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1044 | isolate->clear_pending_exception(); |
| 1045 | InstallFullCode(*info); |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 1051 | if (isolate->has_pending_exception()) isolate->clear_pending_exception(); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1052 | return false; |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1056 | Handle<Code> Compiler::InstallOptimizedCode(RecompileJob* job) { |
| 1057 | SmartPointer<CompilationInfo> info(job->info()); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1058 | // The function may have already been optimized by OSR. Simply continue. |
| 1059 | // Except when OSR already disabled optimization for some reason. |
| 1060 | if (info->shared_info()->optimization_disabled()) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 1061 | info->AbortOptimization(); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1062 | InstallFullCode(*info); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 1063 | if (FLAG_trace_concurrent_recompilation) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1064 | PrintF(" ** aborting optimization for "); |
| 1065 | info->closure()->PrintName(); |
| 1066 | PrintF(" as it has been disabled.\n"); |
| 1067 | } |
dslomov@chromium.org | 4a35c5a | 2013-09-13 07:28:52 +0000 | [diff] [blame] | 1068 | ASSERT(!info->closure()->IsInRecompileQueue()); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 1069 | return Handle<Code>::null(); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1070 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1071 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1072 | Isolate* isolate = info->isolate(); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 1073 | VMState<COMPILER> state(isolate); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1074 | Logger::TimerEventScope timer( |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 1075 | isolate, Logger::TimerEventScope::v8_recompile_synchronous); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1076 | // If crankshaft succeeded, install the optimized code else install |
| 1077 | // the unoptimized code. |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1078 | RecompileJob::Status status = job->last_status(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1079 | if (info->HasAbortedDueToDependencyChange()) { |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 1080 | info->set_bailout_reason(kBailedOutDueToDependencyChange); |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1081 | status = job->AbortOptimization(); |
| 1082 | } else if (status != RecompileJob::SUCCEEDED) { |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 1083 | info->set_bailout_reason(kFailedBailedOutLastTime); |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1084 | status = job->AbortOptimization(); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 1085 | } else if (isolate->DebuggerHasBreakPoints()) { |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 1086 | info->set_bailout_reason(kDebuggerIsActive); |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1087 | status = job->AbortOptimization(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1088 | } else { |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1089 | status = job->GenerateAndInstallCode(); |
| 1090 | ASSERT(status == RecompileJob::SUCCEEDED || |
| 1091 | status == RecompileJob::BAILED_OUT); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | InstallCodeCommon(*info); |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1095 | if (status == RecompileJob::SUCCEEDED) { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1096 | Handle<Code> code = info->code(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 1097 | ASSERT(info->shared_info()->scope_info() != ScopeInfo::Empty(isolate)); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1098 | info->closure()->ReplaceCode(*code); |
| 1099 | if (info->shared_info()->SearchOptimizedCodeMap( |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1100 | info->closure()->context()->native_context()) == -1) { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1101 | InsertCodeIntoOptimizedCodeMap(*info); |
| 1102 | } |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 1103 | if (FLAG_trace_concurrent_recompilation) { |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 1104 | PrintF(" ** Optimized code for "); |
| 1105 | info->closure()->PrintName(); |
| 1106 | PrintF(" installed.\n"); |
| 1107 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1108 | } else { |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 1109 | info->AbortOptimization(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1110 | InstallFullCode(*info); |
| 1111 | } |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 1112 | // Optimized code is finally replacing unoptimized code. Reset the latter's |
| 1113 | // profiler ticks to prevent too soon re-opt after a deopt. |
| 1114 | info->shared_info()->code()->set_profiler_ticks(0); |
dslomov@chromium.org | 4a35c5a | 2013-09-13 07:28:52 +0000 | [diff] [blame] | 1115 | ASSERT(!info->closure()->IsInRecompileQueue()); |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1116 | return (status == RecompileJob::SUCCEEDED) ? info->code() |
| 1117 | : Handle<Code>::null(); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1121 | Handle<SharedFunctionInfo> Compiler::BuildFunctionInfo(FunctionLiteral* literal, |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1122 | Handle<Script> script) { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1123 | // Precondition: code has been parsed and scopes have been analyzed. |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1124 | CompilationInfoWithZone info(script); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1125 | info.SetFunction(literal); |
| 1126 | info.SetScope(literal->scope()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1127 | info.SetLanguageMode(literal->scope()->language_mode()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1128 | |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 1129 | Isolate* isolate = info.isolate(); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 1130 | Factory* factory = isolate->factory(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 1131 | LiveEditFunctionTracker live_edit_tracker(isolate, literal); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1132 | // Determine if the function can be lazily compiled. This is necessary to |
| 1133 | // allow some of our builtin JS files to be lazily compiled. These |
| 1134 | // builtins cannot be handled lazily by the parser, since we have to know |
| 1135 | // if a function uses the special natives syntax, which is something the |
| 1136 | // parser records. |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1137 | // If the debugger requests compilation for break points, we cannot be |
| 1138 | // aggressive about lazy compilation, because it might trigger compilation |
| 1139 | // of functions without an outer context when setting a breakpoint through |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 1140 | // Debug::FindSharedFunctionInfoInScript. |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1141 | bool allow_lazy_without_ctx = literal->AllowsLazyCompilationWithoutContext(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1142 | bool allow_lazy = literal->AllowsLazyCompilation() && |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 1143 | !DebuggerWantsEagerCompilation(&info, allow_lazy_without_ctx); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1144 | |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 1145 | Handle<ScopeInfo> scope_info(ScopeInfo::Empty(isolate)); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1146 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1147 | // Generate code |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 1148 | if (FLAG_lazy && allow_lazy && !literal->is_parenthesized()) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 1149 | Handle<Code> code = isolate->builtins()->LazyCompile(); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1150 | info.SetCode(code); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 1151 | } else if (GenerateCode(&info)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1152 | ASSERT(!info.code().is_null()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1153 | scope_info = ScopeInfo::Create(info.scope(), info.zone()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1154 | } else { |
| 1155 | return Handle<SharedFunctionInfo>::null(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1158 | // Create a shared function info object. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1159 | Handle<SharedFunctionInfo> result = |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 1160 | factory->NewSharedFunctionInfo(literal->name(), |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1161 | literal->materialized_literal_count(), |
mstarzinger@chromium.org | b228be0 | 2013-04-18 14:56:59 +0000 | [diff] [blame] | 1162 | literal->is_generator(), |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1163 | info.code(), |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1164 | scope_info); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1165 | SetFunctionInfo(result, literal, false, script); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1166 | RecordFunctionCompilation(Logger::FUNCTION_TAG, &info, result); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1167 | result->set_allows_lazy_compilation(allow_lazy); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1168 | result->set_allows_lazy_compilation_without_context(allow_lazy_without_ctx); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1169 | |
| 1170 | // Set the expected number of properties for instances and return |
| 1171 | // the resulting function. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1172 | SetExpectedNofPropertiesFromEstimate(result, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1173 | literal->expected_property_count()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1174 | live_edit_tracker.RecordFunctionInfo(result, literal, info.zone()); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1175 | return result; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | |
| 1179 | // Sets the function info on a function. |
| 1180 | // The start_position points to the first '(' character after the function name |
| 1181 | // in the full script source. When counting characters in the script source the |
| 1182 | // the first character is number 0 (not 1). |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1183 | void Compiler::SetFunctionInfo(Handle<SharedFunctionInfo> function_info, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1184 | FunctionLiteral* lit, |
| 1185 | bool is_toplevel, |
| 1186 | Handle<Script> script) { |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1187 | function_info->set_length(lit->parameter_count()); |
| 1188 | function_info->set_formal_parameter_count(lit->parameter_count()); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1189 | function_info->set_script(*script); |
| 1190 | function_info->set_function_token_position(lit->function_token_position()); |
| 1191 | function_info->set_start_position(lit->start_position()); |
| 1192 | function_info->set_end_position(lit->end_position()); |
| 1193 | function_info->set_is_expression(lit->is_expression()); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1194 | function_info->set_is_anonymous(lit->is_anonymous()); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1195 | function_info->set_is_toplevel(is_toplevel); |
| 1196 | function_info->set_inferred_name(*lit->inferred_name()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1197 | function_info->set_allows_lazy_compilation(lit->AllowsLazyCompilation()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1198 | function_info->set_allows_lazy_compilation_without_context( |
| 1199 | lit->AllowsLazyCompilationWithoutContext()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1200 | function_info->set_language_mode(lit->language_mode()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1201 | function_info->set_uses_arguments(lit->scope()->arguments() != NULL); |
| 1202 | function_info->set_has_duplicate_parameters(lit->has_duplicate_parameters()); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1203 | function_info->set_ast_node_count(lit->ast_node_count()); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 1204 | function_info->set_is_function(lit->is_function()); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 1205 | function_info->set_dont_optimize_reason(lit->dont_optimize_reason()); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1206 | function_info->set_dont_inline(lit->flags()->Contains(kDontInline)); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 1207 | function_info->set_dont_cache(lit->flags()->Contains(kDontCache)); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1208 | function_info->set_is_generator(lit->is_generator()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 1212 | void Compiler::RecordFunctionCompilation(Logger::LogEventsAndTags tag, |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1213 | CompilationInfo* info, |
| 1214 | Handle<SharedFunctionInfo> shared) { |
| 1215 | // SharedFunctionInfo is passed separately, because if CompilationInfo |
| 1216 | // was created using Script object, it will not have it. |
| 1217 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1218 | // Log the code generation. If source information is available include |
| 1219 | // script name and line number. Check explicitly whether logging is |
| 1220 | // enabled as finding the line number is not free. |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1221 | if (info->isolate()->logger()->is_logging_code_events() || |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1222 | info->isolate()->cpu_profiler()->is_profiling()) { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1223 | Handle<Script> script = info->script(); |
| 1224 | Handle<Code> code = info->code(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1225 | if (*code == info->isolate()->builtins()->builtin(Builtins::kLazyCompile)) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1226 | return; |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 1227 | int line_num = GetScriptLineNumber(script, shared->start_position()) + 1; |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 1228 | int column_num = |
| 1229 | GetScriptColumnNumber(script, shared->start_position()) + 1; |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 1230 | USE(line_num); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1231 | if (script->name()->IsString()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1232 | PROFILE(info->isolate(), |
| 1233 | CodeCreateEvent(Logger::ToNativeByScript(tag, *script), |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1234 | *code, |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1235 | *shared, |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 1236 | info, |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 1237 | String::cast(script->name()), |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 1238 | line_num, |
| 1239 | column_num)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1240 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1241 | PROFILE(info->isolate(), |
| 1242 | CodeCreateEvent(Logger::ToNativeByScript(tag, *script), |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1243 | *code, |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1244 | *shared, |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 1245 | info, |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 1246 | info->isolate()->heap()->empty_string(), |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 1247 | line_num, |
| 1248 | column_num)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1249 | } |
| 1250 | } |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1251 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 1252 | GDBJIT(AddCode(Handle<String>(shared->DebugName()), |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1253 | Handle<Script>(info->script()), |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1254 | Handle<Code>(info->code()), |
| 1255 | info)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1256 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1257 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1258 | |
| 1259 | CompilationPhase::CompilationPhase(const char* name, CompilationInfo* info) |
| 1260 | : name_(name), info_(info), zone_(info->isolate()) { |
| 1261 | if (FLAG_hydrogen_stats) { |
| 1262 | info_zone_start_allocation_size_ = info->zone()->allocation_size(); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 1263 | timer_.Start(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | |
| 1268 | CompilationPhase::~CompilationPhase() { |
| 1269 | if (FLAG_hydrogen_stats) { |
| 1270 | unsigned size = zone()->allocation_size(); |
| 1271 | size += info_->zone()->allocation_size() - info_zone_start_allocation_size_; |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 1272 | isolate()->GetHStatistics()->SaveTiming(name_, timer_.Elapsed(), size); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | |
| 1277 | bool CompilationPhase::ShouldProduceTraceOutput() const { |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 1278 | // Trace if the appropriate trace flag is set and the phase name's first |
| 1279 | // character is in the FLAG_trace_phase command line parameter. |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1280 | AllowHandleDereference allow_deref; |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 1281 | bool tracing_on = info()->IsStub() |
| 1282 | ? FLAG_trace_hydrogen_stubs |
| 1283 | : (FLAG_trace_hydrogen && |
| 1284 | info()->closure()->PassesFilter(FLAG_trace_hydrogen_filter)); |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 1285 | return (tracing_on && |
| 1286 | OS::StrChr(const_cast<char*>(FLAG_trace_phase), name_[0]) != NULL); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1289 | } } // namespace v8::internal |