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