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