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