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