OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/compiler.h" | 5 #include "src/compiler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "src/asmjs/asm-js.h" | 10 #include "src/asmjs/asm-js.h" |
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 DCHECK(AllowCompilation::IsAllowed(info->isolate())); | 453 DCHECK(AllowCompilation::IsAllowed(info->isolate())); |
454 if (!Compiler::Analyze(info->parse_info()) || | 454 if (!Compiler::Analyze(info->parse_info()) || |
455 !GenerateUnoptimizedCode(info)) { | 455 !GenerateUnoptimizedCode(info)) { |
456 Isolate* isolate = info->isolate(); | 456 Isolate* isolate = info->isolate(); |
457 if (!isolate->has_pending_exception()) isolate->StackOverflow(); | 457 if (!isolate->has_pending_exception()) isolate->StackOverflow(); |
458 return false; | 458 return false; |
459 } | 459 } |
460 return true; | 460 return true; |
461 } | 461 } |
462 | 462 |
463 void EnsureSharedFunctionInfosArrayOnScript(ParseInfo* info) { | |
464 DCHECK(info->is_toplevel()); | |
465 DCHECK(!info->script().is_null()); | |
466 if (info->script()->shared_function_infos()->length() > 0) { | |
467 DCHECK_EQ(info->script()->shared_function_infos()->length(), | |
468 info->max_function_literal_id() + 1); | |
469 return; | |
470 } | |
471 Isolate* isolate = info->isolate(); | |
472 Handle<FixedArray> infos( | |
473 isolate->factory()->NewFixedArray(info->max_function_literal_id() + 1)); | |
474 info->script()->set_shared_function_infos(*infos); | |
475 } | |
476 | |
477 MUST_USE_RESULT MaybeHandle<Code> GetUnoptimizedCode(CompilationInfo* info) { | 463 MUST_USE_RESULT MaybeHandle<Code> GetUnoptimizedCode(CompilationInfo* info) { |
478 VMState<COMPILER> state(info->isolate()); | 464 VMState<COMPILER> state(info->isolate()); |
479 PostponeInterruptsScope postpone(info->isolate()); | 465 PostponeInterruptsScope postpone(info->isolate()); |
480 | 466 |
481 // Parse and update CompilationInfo with the results. | 467 // Parse and update CompilationInfo with the results. |
482 if (!parsing::ParseAny(info->parse_info())) return MaybeHandle<Code>(); | 468 if (!parsing::ParseAny(info->parse_info())) return MaybeHandle<Code>(); |
483 if (info->parse_info()->is_toplevel()) { | |
484 EnsureSharedFunctionInfosArrayOnScript(info->parse_info()); | |
485 } | |
486 DCHECK_EQ(info->shared_info()->language_mode(), | 469 DCHECK_EQ(info->shared_info()->language_mode(), |
487 info->literal()->language_mode()); | 470 info->literal()->language_mode()); |
488 | 471 |
489 // Compile either unoptimized code or bytecode for the interpreter. | 472 // Compile either unoptimized code or bytecode for the interpreter. |
490 if (!CompileUnoptimizedCode(info)) return MaybeHandle<Code>(); | 473 if (!CompileUnoptimizedCode(info)) return MaybeHandle<Code>(); |
491 | 474 |
492 // Record the function compilation event. | 475 // Record the function compilation event. |
493 RecordFunctionCompilation(CodeEventListener::LAZY_COMPILE_TAG, info); | 476 RecordFunctionCompilation(CodeEventListener::LAZY_COMPILE_TAG, info); |
494 | 477 |
495 return info->code(); | 478 return info->code(); |
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
980 script->set_context_data(array->get(v8::Context::kDebugIdIndex)); | 963 script->set_context_data(array->get(v8::Context::kDebugIdIndex)); |
981 | 964 |
982 Handle<SharedFunctionInfo> result; | 965 Handle<SharedFunctionInfo> result; |
983 | 966 |
984 { VMState<COMPILER> state(info->isolate()); | 967 { VMState<COMPILER> state(info->isolate()); |
985 if (parse_info->literal() == nullptr && | 968 if (parse_info->literal() == nullptr && |
986 !parsing::ParseProgram(parse_info)) { | 969 !parsing::ParseProgram(parse_info)) { |
987 return Handle<SharedFunctionInfo>::null(); | 970 return Handle<SharedFunctionInfo>::null(); |
988 } | 971 } |
989 | 972 |
990 EnsureSharedFunctionInfosArrayOnScript(parse_info); | |
991 | |
992 FunctionLiteral* lit = parse_info->literal(); | 973 FunctionLiteral* lit = parse_info->literal(); |
993 | 974 |
994 // Measure how long it takes to do the compilation; only take the | 975 // Measure how long it takes to do the compilation; only take the |
995 // rest of the function into account to avoid overlap with the | 976 // rest of the function into account to avoid overlap with the |
996 // parsing statistics. | 977 // parsing statistics. |
997 RuntimeCallTimerScope runtimeTimer( | 978 RuntimeCallTimerScope runtimeTimer( |
998 isolate, parse_info->is_eval() ? &RuntimeCallStats::CompileEval | 979 isolate, parse_info->is_eval() ? &RuntimeCallStats::CompileEval |
999 : &RuntimeCallStats::Compile); | 980 : &RuntimeCallStats::Compile); |
1000 HistogramTimer* rate = parse_info->is_eval() | 981 HistogramTimer* rate = parse_info->is_eval() |
1001 ? info->isolate()->counters()->compile_eval() | 982 ? info->isolate()->counters()->compile_eval() |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1044 DCHECK_NOT_NULL(info->literal()); | 1025 DCHECK_NOT_NULL(info->literal()); |
1045 if (!Rewriter::Rewrite(info)) return false; | 1026 if (!Rewriter::Rewrite(info)) return false; |
1046 DeclarationScope::Analyze(info, AnalyzeMode::kRegular); | 1027 DeclarationScope::Analyze(info, AnalyzeMode::kRegular); |
1047 if (!Renumber(info)) return false; | 1028 if (!Renumber(info)) return false; |
1048 DCHECK_NOT_NULL(info->scope()); | 1029 DCHECK_NOT_NULL(info->scope()); |
1049 return true; | 1030 return true; |
1050 } | 1031 } |
1051 | 1032 |
1052 bool Compiler::ParseAndAnalyze(ParseInfo* info) { | 1033 bool Compiler::ParseAndAnalyze(ParseInfo* info) { |
1053 if (!parsing::ParseAny(info)) return false; | 1034 if (!parsing::ParseAny(info)) return false; |
1054 if (info->is_toplevel()) EnsureSharedFunctionInfosArrayOnScript(info); | |
1055 if (!Compiler::Analyze(info)) return false; | 1035 if (!Compiler::Analyze(info)) return false; |
1056 DCHECK_NOT_NULL(info->literal()); | 1036 DCHECK_NOT_NULL(info->literal()); |
1057 DCHECK_NOT_NULL(info->scope()); | 1037 DCHECK_NOT_NULL(info->scope()); |
1058 return true; | 1038 return true; |
1059 } | 1039 } |
1060 | 1040 |
1061 bool Compiler::Compile(Handle<JSFunction> function, ClearExceptionFlag flag) { | 1041 bool Compiler::Compile(Handle<JSFunction> function, ClearExceptionFlag flag) { |
1062 if (function->is_compiled()) return true; | 1042 if (function->is_compiled()) return true; |
1063 Isolate* isolate = function->GetIsolate(); | 1043 Isolate* isolate = function->GetIsolate(); |
1064 DCHECK(AllowCompilation::IsAllowed(isolate)); | 1044 DCHECK(AllowCompilation::IsAllowed(isolate)); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1165 return true; | 1145 return true; |
1166 } | 1146 } |
1167 | 1147 |
1168 MaybeHandle<JSArray> Compiler::CompileForLiveEdit(Handle<Script> script) { | 1148 MaybeHandle<JSArray> Compiler::CompileForLiveEdit(Handle<Script> script) { |
1169 Isolate* isolate = script->GetIsolate(); | 1149 Isolate* isolate = script->GetIsolate(); |
1170 DCHECK(AllowCompilation::IsAllowed(isolate)); | 1150 DCHECK(AllowCompilation::IsAllowed(isolate)); |
1171 | 1151 |
1172 // In order to ensure that live edit function info collection finds the newly | 1152 // In order to ensure that live edit function info collection finds the newly |
1173 // generated shared function infos, clear the script's list temporarily | 1153 // generated shared function infos, clear the script's list temporarily |
1174 // and restore it at the end of this method. | 1154 // and restore it at the end of this method. |
1175 Handle<FixedArray> old_function_infos(script->shared_function_infos(), | 1155 Handle<Object> old_function_infos(script->shared_function_infos(), isolate); |
1176 isolate); | 1156 script->set_shared_function_infos(Smi::kZero); |
1177 script->set_shared_function_infos(isolate->heap()->empty_fixed_array()); | |
1178 | 1157 |
1179 // Start a compilation. | 1158 // Start a compilation. |
1180 Zone zone(isolate->allocator(), ZONE_NAME); | 1159 Zone zone(isolate->allocator(), ZONE_NAME); |
1181 ParseInfo parse_info(&zone, script); | 1160 ParseInfo parse_info(&zone, script); |
1182 CompilationInfo info(&parse_info, Handle<JSFunction>::null()); | 1161 CompilationInfo info(&parse_info, Handle<JSFunction>::null()); |
1183 info.MarkAsDebug(); | 1162 info.MarkAsDebug(); |
1184 | 1163 |
1185 // TODO(635): support extensions. | 1164 // TODO(635): support extensions. |
1186 const bool compilation_succeeded = !CompileToplevel(&info).is_null(); | 1165 const bool compilation_succeeded = !CompileToplevel(&info).is_null(); |
1187 Handle<JSArray> infos; | 1166 Handle<JSArray> infos; |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1552 | 1531 |
1553 | 1532 |
1554 Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfo( | 1533 Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfo( |
1555 FunctionLiteral* literal, Handle<Script> script, | 1534 FunctionLiteral* literal, Handle<Script> script, |
1556 CompilationInfo* outer_info) { | 1535 CompilationInfo* outer_info) { |
1557 // Precondition: code has been parsed and scopes have been analyzed. | 1536 // Precondition: code has been parsed and scopes have been analyzed. |
1558 Isolate* isolate = outer_info->isolate(); | 1537 Isolate* isolate = outer_info->isolate(); |
1559 MaybeHandle<SharedFunctionInfo> maybe_existing; | 1538 MaybeHandle<SharedFunctionInfo> maybe_existing; |
1560 | 1539 |
1561 // Find any previously allocated shared function info for the given literal. | 1540 // Find any previously allocated shared function info for the given literal. |
1562 maybe_existing = script->FindSharedFunctionInfo(isolate, literal); | 1541 if (outer_info->shared_info()->never_compiled()) { |
| 1542 // On the first compile, there are no existing shared function info for |
| 1543 // inner functions yet, so do not try to find them. All bets are off for |
| 1544 // live edit though. |
| 1545 SLOW_DCHECK(script->FindSharedFunctionInfo(literal).is_null() || |
| 1546 isolate->debug()->live_edit_enabled()); |
| 1547 } else { |
| 1548 maybe_existing = script->FindSharedFunctionInfo(literal); |
| 1549 } |
1563 | 1550 |
1564 // We found an existing shared function info. If it has any sort of code | 1551 // We found an existing shared function info. If it has any sort of code |
1565 // attached, don't worry about compiling and simply return it. Otherwise, | 1552 // attached, don't worry about compiling and simply return it. Otherwise, |
1566 // continue to decide whether to eagerly compile. | 1553 // continue to decide whether to eagerly compile. |
1567 // Note that we also carry on if we are compiling eager to obtain code for | 1554 // Note that we also carry on if we are compiling eager to obtain code for |
1568 // debugging, unless we already have code with debug break slots. | 1555 // debugging, unless we already have code with debug break slots. |
1569 Handle<SharedFunctionInfo> existing; | 1556 Handle<SharedFunctionInfo> existing; |
1570 if (maybe_existing.ToHandle(&existing)) { | 1557 if (maybe_existing.ToHandle(&existing)) { |
1571 DCHECK(!existing->is_toplevel()); | 1558 DCHECK(!existing->is_toplevel()); |
1572 if (existing->HasBaselineCode() || existing->HasBytecodeArray()) { | 1559 if (existing->HasBaselineCode() || existing->HasBytecodeArray()) { |
1573 if (!outer_info->is_debug() || existing->HasDebugCode()) { | 1560 if (!outer_info->is_debug() || existing->HasDebugCode()) { |
1574 return existing; | 1561 return existing; |
1575 } | 1562 } |
1576 } | 1563 } |
1577 } | 1564 } |
1578 | 1565 |
1579 // Allocate a shared function info object. | 1566 // Allocate a shared function info object. |
1580 Handle<SharedFunctionInfo> result; | 1567 Handle<SharedFunctionInfo> result; |
1581 if (!maybe_existing.ToHandle(&result)) { | 1568 if (!maybe_existing.ToHandle(&result)) { |
1582 result = | 1569 result = |
1583 isolate->factory()->NewSharedFunctionInfoForLiteral(literal, script); | 1570 isolate->factory()->NewSharedFunctionInfoForLiteral(literal, script); |
1584 result->set_is_toplevel(false); | 1571 result->set_is_toplevel(false); |
| 1572 |
| 1573 // If the outer function has been compiled before, we cannot be sure that |
| 1574 // shared function info for this function literal has been created for the |
| 1575 // first time. It may have already been compiled previously. |
| 1576 result->set_never_compiled(outer_info->shared_info()->never_compiled()); |
1585 } | 1577 } |
1586 | 1578 |
1587 Zone zone(isolate->allocator(), ZONE_NAME); | 1579 Zone zone(isolate->allocator(), ZONE_NAME); |
1588 ParseInfo parse_info(&zone, script); | 1580 ParseInfo parse_info(&zone, script); |
1589 CompilationInfo info(&parse_info, Handle<JSFunction>::null()); | 1581 CompilationInfo info(&parse_info, Handle<JSFunction>::null()); |
1590 parse_info.set_literal(literal); | 1582 parse_info.set_literal(literal); |
1591 parse_info.set_shared_info(result); | 1583 parse_info.set_shared_info(result); |
1592 parse_info.set_function_literal_id(result->function_literal_id()); | 1584 parse_info.set_function_literal_id(result->function_literal_id()); |
1593 parse_info.set_language_mode(literal->scope()->language_mode()); | 1585 parse_info.set_language_mode(literal->scope()->language_mode()); |
1594 parse_info.set_ast_value_factory( | 1586 parse_info.set_ast_value_factory( |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1729 DCHECK(shared->is_compiled()); | 1721 DCHECK(shared->is_compiled()); |
1730 function->set_literals(cached.literals); | 1722 function->set_literals(cached.literals); |
1731 } else if (shared->is_compiled()) { | 1723 } else if (shared->is_compiled()) { |
1732 // TODO(mvstanton): pass pretenure flag to EnsureLiterals. | 1724 // TODO(mvstanton): pass pretenure flag to EnsureLiterals. |
1733 JSFunction::EnsureLiterals(function); | 1725 JSFunction::EnsureLiterals(function); |
1734 } | 1726 } |
1735 } | 1727 } |
1736 | 1728 |
1737 } // namespace internal | 1729 } // namespace internal |
1738 } // namespace v8 | 1730 } // namespace v8 |
OLD | NEW |