1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
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.
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.
32 #include "bootstrapper.h"
34 #include "compilation-cache.h"
36 #include "full-codegen.h"
39 #include "isolate-inl.h"
44 #include "runtime-profiler.h"
45 #include "scanner-character-streams.h"
46 #include "scopeinfo.h"
48 #include "vm-state-inl.h"
54 CompilationInfo::CompilationInfo(Handle<Script> script)
55 : isolate_(script->GetIsolate()),
56 flags_(LanguageModeField::encode(CLASSIC_MODE)),
62 pre_parse_data_(NULL),
63 osr_ast_id_(AstNode::kNoNumber) {
68 CompilationInfo::CompilationInfo(Handle<SharedFunctionInfo> shared_info)
69 : isolate_(shared_info->GetIsolate()),
70 flags_(LanguageModeField::encode(CLASSIC_MODE) |
71 IsLazy::encode(true)),
75 shared_info_(shared_info),
76 script_(Handle<Script>(Script::cast(shared_info->script()))),
78 pre_parse_data_(NULL),
79 osr_ast_id_(AstNode::kNoNumber) {
84 CompilationInfo::CompilationInfo(Handle<JSFunction> closure)
85 : isolate_(closure->GetIsolate()),
86 flags_(LanguageModeField::encode(CLASSIC_MODE) |
87 IsLazy::encode(true)),
92 shared_info_(Handle<SharedFunctionInfo>(closure->shared())),
93 script_(Handle<Script>(Script::cast(shared_info_->script()))),
95 pre_parse_data_(NULL),
96 osr_ast_id_(AstNode::kNoNumber) {
101 // Disable optimization for the rest of the compilation pipeline.
102 void CompilationInfo::DisableOptimization() {
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);
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 &&
119 !function()->flags()->Contains(kDontSelfOptimize) &&
120 !function()->flags()->Contains(kDontOptimize) &&
121 function()->scope()->allows_lazy_recompilation() &&
122 (shared_info().is_null() || !shared_info()->optimization_disabled());
126 void CompilationInfo::AbortOptimization() {
127 Handle<Code> code(shared_info()->code());
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.
140 static bool is_debugging_active() {
141 #ifdef ENABLE_DEBUGGER_SUPPORT
142 Isolate* isolate = Isolate::Current();
143 return V8::UseCrankshaft() ?
144 isolate->debug()->has_break_points() :
145 isolate->debugger()->IsDebuggerActive();
152 static bool AlwaysFullCompiler() {
153 return FLAG_always_full_compiler || is_debugging_active();
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);
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;
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",
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.
186 if (!V8::UseCrankshaft()) {
187 info->DisableOptimization();
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);
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);
201 // We should never arrive here if optimization has been disabled on the
202 // shared function info.
203 ASSERT(!info->shared_info()->optimization_disabled());
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.
208 if (AlwaysFullCompiler()) {
213 // Limit the number of times we re-compile a functions with
214 // the optimizing compiler.
215 const int kMaxOptCount =
216 FLAG_deopt_every_n_times == 0 ? Compiler::kDefaultMaxOptCount : 1000;
217 if (info->shared_info()->opt_count() > kMaxOptCount) {
218 info->AbortOptimization();
219 info->shared_info()->DisableOptimization();
220 // True indicates the compilation pipeline is still going, not
221 // necessarily that we optimized the code.
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.
230 // The encoding is as a signed value, with parameters and receiver using
231 // the negative indices and locals the non-negative ones.
232 const int parameter_limit = -LUnallocated::kMinFixedIndex;
233 const int locals_limit = LUnallocated::kMaxFixedIndex;
234 Scope* scope = info->scope();
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)) {
238 info->AbortOptimization();
239 info->shared_info()->DisableOptimization();
240 // True indicates the compilation pipeline is still going, not
241 // necessarily that we optimized the code.
245 // Take --hydrogen-filter into account.
246 Handle<String> name = info->function()->debug_name();
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))) {
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();
263 if (should_recompile || FLAG_hydrogen_stats) {
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
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.
277 Compiler::RecordFunctionCompilation(
278 Logger::LAZY_COMPILE_TAG, &unoptimized, shared);
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
287 ASSERT(FLAG_always_opt || code->optimizable());
288 ASSERT(info->shared_info()->has_deoptimization_support());
290 if (FLAG_trace_hydrogen) {
291 PrintF("-----------------------------------------------------------\n");
292 PrintF("Compiling method %s using hydrogen\n", *name->ToCString());
293 HTracer::Instance()->TraceCompilation(info->function());
296 Handle<Context> global_context(info->closure()->context()->global_context());
297 TypeFeedbackOracle oracle(code, global_context, info->isolate());
298 HGraphBuilder builder(info, &oracle);
299 HPhase phase(HPhase::kTotal);
300 HGraph* graph = builder.CreateGraph();
301 if (info->isolate()->has_pending_exception()) {
302 info->SetCode(Handle<Code>::null());
307 Handle<Code> optimized_code = graph->Compile(info);
308 if (!optimized_code.is_null()) {
309 info->SetCode(optimized_code);
310 FinishOptimization(info->closure(), start);
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.
320 info->shared_info()->DisableOptimization();
322 // True indicates the compilation pipeline is still going, not necessarily
323 // that we optimized the code.
328 static bool GenerateCode(CompilationInfo* info) {
329 return info->IsCompilingForDebugging() || !V8::UseCrankshaft() ?
330 FullCodeGenerator::MakeCode(info) :
331 MakeCrankshaftCode(info);
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);
339 return Rewriter::Rewrite(info) && Scope::Analyze(info) && GenerateCode(info);
343 #ifdef ENABLE_DEBUGGER_SUPPORT
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);
348 if (!info->shared_info().is_null()) {
349 Handle<ScopeInfo> scope_info = ScopeInfo::Create(info->scope());
350 info->shared_info()->set_scope_info(*scope_info);
357 static Handle<SharedFunctionInfo> MakeFunctionInfo(CompilationInfo* info) {
358 Isolate* isolate = info->isolate();
359 ZoneScope zone_scope(isolate, DELETE_ON_EXIT);
360 PostponeInterruptsScope postpone(isolate);
362 ASSERT(!isolate->global_context().is_null());
363 Handle<Script> script = info->script();
364 script->set_context_data((*isolate->global_context())->data());
366 #ifdef ENABLE_DEBUGGER_SUPPORT
367 if (info->is_eval()) {
368 Script::CompilationType compilation_type = Script::COMPILATION_TYPE_EVAL;
369 script->set_compilation_type(Smi::FromInt(compilation_type));
370 // For eval scripts add information on the function from which eval was
372 if (info->is_eval()) {
373 StackTraceFrameIterator it(isolate);
375 script->set_eval_from_shared(
376 JSFunction::cast(it.frame()->function())->shared());
377 Code* code = it.frame()->LookupCode();
378 int offset = static_cast<int>(
379 it.frame()->pc() - code->instruction_start());
380 script->set_eval_from_instructions_offset(Smi::FromInt(offset));
386 isolate->debugger()->OnBeforeCompile(script);
389 // Only allow non-global compiles for eval.
390 ASSERT(info->is_eval() || info->is_global());
391 ParsingFlags flags = kNoParsingFlags;
392 if (info->pre_parse_data() != NULL ||
393 String::cast(script->source())->length() > FLAG_min_preparse_length) {
396 if (!ParserApi::Parse(info, flags)) {
397 return Handle<SharedFunctionInfo>::null();
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.
403 HistogramTimer* rate = info->is_eval()
404 ? info->isolate()->counters()->compile_eval()
405 : info->isolate()->counters()->compile();
406 HistogramTimerScope timer(rate);
409 FunctionLiteral* lit = info->function();
410 LiveEditFunctionTracker live_edit_tracker(isolate, lit);
411 if (!MakeCode(info)) {
412 if (!isolate->has_pending_exception()) isolate->StackOverflow();
413 return Handle<SharedFunctionInfo>::null();
416 // Allocate function.
417 ASSERT(!info->code().is_null());
418 Handle<SharedFunctionInfo> result =
419 isolate->factory()->NewSharedFunctionInfo(
421 lit->materialized_literal_count(),
423 ScopeInfo::Create(info->scope()));
425 ASSERT_EQ(RelocInfo::kNoPosition, lit->function_token_position());
426 Compiler::SetFunctionInfo(result, lit, true, script);
428 if (script->name()->IsString()) {
429 PROFILE(isolate, CodeCreateEvent(
432 : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script),
435 String::cast(script->name())));
436 GDBJIT(AddCode(Handle<String>(String::cast(script->name())),
441 PROFILE(isolate, CodeCreateEvent(
444 : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script),
447 isolate->heap()->empty_string()));
448 GDBJIT(AddCode(Handle<String>(), script, info->code(), info));
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.
454 SetExpectedNofPropertiesFromEstimate(result, lit->expected_property_count());
456 script->set_compilation_state(
457 Smi::FromInt(Script::COMPILATION_STATE_COMPILED));
459 #ifdef ENABLE_DEBUGGER_SUPPORT
461 isolate->debugger()->OnAfterCompile(
462 script, Debugger::NO_AFTER_COMPILE_FLAGS);
465 live_edit_tracker.RecordFunctionInfo(result, lit);
471 Handle<SharedFunctionInfo> Compiler::Compile(Handle<String> source,
472 Handle<Object> script_name,
475 v8::Extension* extension,
476 ScriptDataImpl* pre_data,
477 Handle<Object> script_data,
479 v8::Script::CompileFlags compile_flags) {
480 Isolate* isolate = source->GetIsolate();
481 int source_length = source->length();
482 isolate->counters()->total_load_size()->Increment(source_length);
483 isolate->counters()->total_compile_size()->Increment(source_length);
485 // The VM is in the COMPILER state until exiting this function.
486 VMState state(isolate, COMPILER);
488 CompilationCache* compilation_cache = isolate->compilation_cache();
490 // Do a lookup in the compilation cache but not for extensions.
491 Handle<SharedFunctionInfo> result;
492 if (extension == NULL) {
493 result = compilation_cache->LookupScript(source,
499 if (result.is_null()) {
500 // No cache entry found. Do pre-parsing, if it makes sense, and compile
502 // Building preparse data that is only used immediately after is only a
503 // saving if we might skip building the AST for lazily compiled functions.
504 // I.e., preparse data isn't relevant when the lazy flag is off, and
505 // for small sources, odds are that there aren't many functions
506 // that would be compiled lazily anyway, so we skip the preparse step
509 // Create a script object describing the script to be compiled.
510 Handle<Script> script = FACTORY->NewScript(source);
511 if (natives == NATIVES_CODE) {
512 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
514 if (!script_name.is_null()) {
515 script->set_name(*script_name);
516 script->set_line_offset(Smi::FromInt(line_offset));
517 script->set_column_offset(Smi::FromInt(column_offset));
520 script->set_data(script_data.is_null() ? HEAP->undefined_value()
523 // Compile the function and add it to the cache.
524 CompilationInfo info(script);
526 info.SetExtension(extension);
527 info.SetPreParseData(pre_data);
528 if (FLAG_use_strict) {
529 info.SetLanguageMode(FLAG_harmony_scoping ? EXTENDED_MODE : STRICT_MODE);
531 if (compile_flags & v8::Script::QmlMode) info.MarkAsQmlMode();
532 result = MakeFunctionInfo(&info);
533 if (extension == NULL && !result.is_null()) {
534 compilation_cache->PutScript(source, result);
537 if (result->ic_age() != HEAP->global_ic_age()) {
538 result->ResetForNewContext(HEAP->global_ic_age());
542 if (result.is_null()) isolate->ReportPendingMessages();
547 Handle<SharedFunctionInfo> Compiler::CompileEval(Handle<String> source,
548 Handle<Context> context,
550 LanguageMode language_mode,
553 Isolate* isolate = source->GetIsolate();
554 int source_length = source->length();
555 isolate->counters()->total_eval_size()->Increment(source_length);
556 isolate->counters()->total_compile_size()->Increment(source_length);
558 // The VM is in the COMPILER state until exiting this function.
559 VMState state(isolate, COMPILER);
561 // Do a lookup in the compilation cache; if the entry is not there, invoke
562 // the compiler and add the result to the cache.
563 Handle<SharedFunctionInfo> result;
564 CompilationCache* compilation_cache = isolate->compilation_cache();
565 result = compilation_cache->LookupEval(source,
571 if (result.is_null()) {
572 // Create a script object describing the script to be compiled.
573 Handle<Script> script = isolate->factory()->NewScript(source);
574 CompilationInfo info(script);
576 if (is_global) info.MarkAsGlobal();
577 info.SetLanguageMode(language_mode);
578 if (qml_mode) info.MarkAsQmlMode();
579 info.SetCallingContext(context);
580 result = MakeFunctionInfo(&info);
581 if (!result.is_null()) {
582 // Explicitly disable optimization for eval code. We're not yet prepared
583 // to handle eval-code in the optimizing compiler.
584 result->DisableOptimization();
586 // If caller is strict mode, the result must be in strict mode or
587 // extended mode as well, but not the other way around. Consider:
588 // eval("'use strict'; ...");
589 ASSERT(language_mode != STRICT_MODE || !result->is_classic_mode());
590 // If caller is in extended mode, the result must also be in
592 ASSERT(language_mode != EXTENDED_MODE ||
593 result->is_extended_mode());
594 compilation_cache->PutEval(
595 source, context, is_global, result, scope_position);
598 if (result->ic_age() != HEAP->global_ic_age()) {
599 result->ResetForNewContext(HEAP->global_ic_age());
607 bool Compiler::CompileLazy(CompilationInfo* info) {
608 Isolate* isolate = info->isolate();
610 ZoneScope zone_scope(isolate, DELETE_ON_EXIT);
612 // The VM is in the COMPILER state until exiting this function.
613 VMState state(isolate, COMPILER);
615 PostponeInterruptsScope postpone(isolate);
617 Handle<SharedFunctionInfo> shared = info->shared_info();
618 int compiled_size = shared->end_position() - shared->start_position();
619 isolate->counters()->total_compile_size()->Increment(compiled_size);
621 // Generate the AST for the lazily compiled function.
622 if (ParserApi::Parse(info, kNoParsingFlags)) {
623 // Measure how long it takes to do the lazy compilation; only take the
624 // rest of the function into account to avoid overlap with the lazy
625 // parsing statistics.
626 HistogramTimerScope timer(isolate->counters()->compile_lazy());
628 // After parsing we know the function's language mode. Remember it.
629 LanguageMode language_mode = info->function()->language_mode();
630 info->SetLanguageMode(language_mode);
631 shared->set_language_mode(language_mode);
633 // After parsing we know function's qml mode. Remember it.
634 if (info->function()->qml_mode()) {
635 shared->set_qml_mode(true);
636 info->MarkAsQmlMode();
640 if (!MakeCode(info)) {
641 if (!isolate->has_pending_exception()) {
642 isolate->StackOverflow();
645 ASSERT(!info->code().is_null());
646 Handle<Code> code = info->code();
647 // Set optimizable to false if this is disallowed by the shared
648 // function info, e.g., we might have flushed the code and must
649 // reset this bit when lazy compiling the code again.
650 if (shared->optimization_disabled()) code->set_optimizable(false);
652 Handle<JSFunction> function = info->closure();
653 RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, info, shared);
655 if (info->IsOptimizing()) {
656 ASSERT(shared->scope_info() != ScopeInfo::Empty());
657 function->ReplaceCode(*code);
659 // Update the shared function info with the compiled code and the
660 // scope info. Please note, that the order of the shared function
661 // info initialization is important since set_scope_info might
662 // trigger a GC, causing the ASSERT below to be invalid if the code
663 // was flushed. By setting the code object last we avoid this.
664 Handle<ScopeInfo> scope_info = ScopeInfo::Create(info->scope());
665 shared->set_scope_info(*scope_info);
666 shared->set_code(*code);
667 if (!function.is_null()) {
668 function->ReplaceCode(*code);
669 ASSERT(!function->IsOptimized());
672 // Set the expected number of properties for instances.
673 FunctionLiteral* lit = info->function();
674 int expected = lit->expected_property_count();
675 SetExpectedNofPropertiesFromEstimate(shared, expected);
677 // Set the optimization hints after performing lazy compilation, as
678 // these are not set when the function is set up as a lazily
679 // compiled function.
680 shared->SetThisPropertyAssignmentsInfo(
681 lit->has_only_simple_this_property_assignments(),
682 *lit->this_property_assignments());
684 // Check the function has compiled code.
685 ASSERT(shared->is_compiled());
686 shared->set_code_age(0);
687 shared->set_dont_optimize(lit->flags()->Contains(kDontOptimize));
688 shared->set_dont_inline(lit->flags()->Contains(kDontInline));
689 shared->set_ast_node_count(lit->ast_node_count());
691 if (V8::UseCrankshaft()&&
692 !function.is_null() &&
693 !shared->optimization_disabled()) {
694 // If we're asked to always optimize, we compile the optimized
695 // version of the function right away - unless the debugger is
696 // active as it makes no sense to compile optimized code then.
697 if (FLAG_always_opt &&
698 !Isolate::Current()->DebuggerHasBreakPoints()) {
699 CompilationInfo optimized(function);
700 optimized.SetOptimizing(AstNode::kNoNumber);
701 return CompileLazy(&optimized);
710 ASSERT(info->code().is_null());
715 Handle<SharedFunctionInfo> Compiler::BuildFunctionInfo(FunctionLiteral* literal,
716 Handle<Script> script) {
717 // Precondition: code has been parsed and scopes have been analyzed.
718 CompilationInfo info(script);
719 info.SetFunction(literal);
720 info.SetScope(literal->scope());
721 info.SetLanguageMode(literal->scope()->language_mode());
723 LiveEditFunctionTracker live_edit_tracker(info.isolate(), literal);
724 // Determine if the function can be lazily compiled. This is necessary to
725 // allow some of our builtin JS files to be lazily compiled. These
726 // builtins cannot be handled lazily by the parser, since we have to know
727 // if a function uses the special natives syntax, which is something the
729 bool allow_lazy = literal->AllowsLazyCompilation() &&
730 !LiveEditFunctionTracker::IsActive(info.isolate());
732 Handle<ScopeInfo> scope_info(ScopeInfo::Empty());
735 if (FLAG_lazy && allow_lazy) {
736 Handle<Code> code = info.isolate()->builtins()->LazyCompile();
738 } else if ((V8::UseCrankshaft() && MakeCrankshaftCode(&info)) ||
739 (!V8::UseCrankshaft() && FullCodeGenerator::MakeCode(&info))) {
740 ASSERT(!info.code().is_null());
741 scope_info = ScopeInfo::Create(info.scope());
743 return Handle<SharedFunctionInfo>::null();
746 // Create a shared function info object.
747 Handle<SharedFunctionInfo> result =
748 FACTORY->NewSharedFunctionInfo(literal->name(),
749 literal->materialized_literal_count(),
752 SetFunctionInfo(result, literal, false, script);
753 RecordFunctionCompilation(Logger::FUNCTION_TAG, &info, result);
754 result->set_allows_lazy_compilation(allow_lazy);
756 // Set the expected number of properties for instances and return
757 // the resulting function.
758 SetExpectedNofPropertiesFromEstimate(result,
759 literal->expected_property_count());
760 live_edit_tracker.RecordFunctionInfo(result, literal);
765 // Sets the function info on a function.
766 // The start_position points to the first '(' character after the function name
767 // in the full script source. When counting characters in the script source the
768 // the first character is number 0 (not 1).
769 void Compiler::SetFunctionInfo(Handle<SharedFunctionInfo> function_info,
770 FunctionLiteral* lit,
772 Handle<Script> script) {
773 function_info->set_length(lit->parameter_count());
774 function_info->set_formal_parameter_count(lit->parameter_count());
775 function_info->set_script(*script);
776 function_info->set_function_token_position(lit->function_token_position());
777 function_info->set_start_position(lit->start_position());
778 function_info->set_end_position(lit->end_position());
779 function_info->set_is_expression(lit->is_expression());
780 function_info->set_is_anonymous(lit->is_anonymous());
781 function_info->set_is_toplevel(is_toplevel);
782 function_info->set_inferred_name(*lit->inferred_name());
783 function_info->SetThisPropertyAssignmentsInfo(
784 lit->has_only_simple_this_property_assignments(),
785 *lit->this_property_assignments());
786 function_info->set_allows_lazy_compilation(lit->AllowsLazyCompilation());
787 function_info->set_language_mode(lit->language_mode());
788 function_info->set_qml_mode(lit->qml_mode());
789 function_info->set_uses_arguments(lit->scope()->arguments() != NULL);
790 function_info->set_has_duplicate_parameters(lit->has_duplicate_parameters());
791 function_info->set_ast_node_count(lit->ast_node_count());
792 function_info->set_is_function(lit->is_function());
793 function_info->set_dont_optimize(lit->flags()->Contains(kDontOptimize));
794 function_info->set_dont_inline(lit->flags()->Contains(kDontInline));
798 void Compiler::RecordFunctionCompilation(Logger::LogEventsAndTags tag,
799 CompilationInfo* info,
800 Handle<SharedFunctionInfo> shared) {
801 // SharedFunctionInfo is passed separately, because if CompilationInfo
802 // was created using Script object, it will not have it.
804 // Log the code generation. If source information is available include
805 // script name and line number. Check explicitly whether logging is
806 // enabled as finding the line number is not free.
807 if (info->isolate()->logger()->is_logging() ||
808 CpuProfiler::is_profiling(info->isolate())) {
809 Handle<Script> script = info->script();
810 Handle<Code> code = info->code();
811 if (*code == info->isolate()->builtins()->builtin(Builtins::kLazyCompile))
813 if (script->name()->IsString()) {
814 int line_num = GetScriptLineNumber(script, shared->start_position()) + 1;
816 PROFILE(info->isolate(),
817 CodeCreateEvent(Logger::ToNativeByScript(tag, *script),
820 String::cast(script->name()),
823 PROFILE(info->isolate(),
824 CodeCreateEvent(Logger::ToNativeByScript(tag, *script),
827 shared->DebugName()));
831 GDBJIT(AddCode(Handle<String>(shared->DebugName()),
832 Handle<Script>(info->script()),
833 Handle<Code>(info->code()),
837 } } // namespace v8::internal