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