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 |
463 MUST_USE_RESULT MaybeHandle<Code> GetUnoptimizedCode(CompilationInfo* info) { | 477 MUST_USE_RESULT MaybeHandle<Code> GetUnoptimizedCode(CompilationInfo* info) { |
464 VMState<COMPILER> state(info->isolate()); | 478 VMState<COMPILER> state(info->isolate()); |
465 PostponeInterruptsScope postpone(info->isolate()); | 479 PostponeInterruptsScope postpone(info->isolate()); |
466 | 480 |
467 // Parse and update CompilationInfo with the results. | 481 // Parse and update CompilationInfo with the results. |
468 if (!parsing::ParseAny(info->parse_info())) return MaybeHandle<Code>(); | 482 if (!parsing::ParseAny(info->parse_info())) return MaybeHandle<Code>(); |
| 483 if (info->parse_info()->is_toplevel()) { |
| 484 EnsureSharedFunctionInfosArrayOnScript(info->parse_info()); |
| 485 } |
469 DCHECK_EQ(info->shared_info()->language_mode(), | 486 DCHECK_EQ(info->shared_info()->language_mode(), |
470 info->literal()->language_mode()); | 487 info->literal()->language_mode()); |
471 | 488 |
472 // Compile either unoptimized code or bytecode for the interpreter. | 489 // Compile either unoptimized code or bytecode for the interpreter. |
473 if (!CompileUnoptimizedCode(info)) return MaybeHandle<Code>(); | 490 if (!CompileUnoptimizedCode(info)) return MaybeHandle<Code>(); |
474 | 491 |
475 // Record the function compilation event. | 492 // Record the function compilation event. |
476 RecordFunctionCompilation(CodeEventListener::LAZY_COMPILE_TAG, info); | 493 RecordFunctionCompilation(CodeEventListener::LAZY_COMPILE_TAG, info); |
477 | 494 |
478 return info->code(); | 495 return info->code(); |
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
963 script->set_context_data(array->get(v8::Context::kDebugIdIndex)); | 980 script->set_context_data(array->get(v8::Context::kDebugIdIndex)); |
964 | 981 |
965 Handle<SharedFunctionInfo> result; | 982 Handle<SharedFunctionInfo> result; |
966 | 983 |
967 { VMState<COMPILER> state(info->isolate()); | 984 { VMState<COMPILER> state(info->isolate()); |
968 if (parse_info->literal() == nullptr && | 985 if (parse_info->literal() == nullptr && |
969 !parsing::ParseProgram(parse_info)) { | 986 !parsing::ParseProgram(parse_info)) { |
970 return Handle<SharedFunctionInfo>::null(); | 987 return Handle<SharedFunctionInfo>::null(); |
971 } | 988 } |
972 | 989 |
| 990 EnsureSharedFunctionInfosArrayOnScript(parse_info); |
| 991 |
973 FunctionLiteral* lit = parse_info->literal(); | 992 FunctionLiteral* lit = parse_info->literal(); |
974 | 993 |
975 // Measure how long it takes to do the compilation; only take the | 994 // Measure how long it takes to do the compilation; only take the |
976 // rest of the function into account to avoid overlap with the | 995 // rest of the function into account to avoid overlap with the |
977 // parsing statistics. | 996 // parsing statistics. |
978 RuntimeCallTimerScope runtimeTimer( | 997 RuntimeCallTimerScope runtimeTimer( |
979 isolate, parse_info->is_eval() ? &RuntimeCallStats::CompileEval | 998 isolate, parse_info->is_eval() ? &RuntimeCallStats::CompileEval |
980 : &RuntimeCallStats::Compile); | 999 : &RuntimeCallStats::Compile); |
981 HistogramTimer* rate = parse_info->is_eval() | 1000 HistogramTimer* rate = parse_info->is_eval() |
982 ? info->isolate()->counters()->compile_eval() | 1001 ? info->isolate()->counters()->compile_eval() |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1025 DCHECK_NOT_NULL(info->literal()); | 1044 DCHECK_NOT_NULL(info->literal()); |
1026 if (!Rewriter::Rewrite(info)) return false; | 1045 if (!Rewriter::Rewrite(info)) return false; |
1027 DeclarationScope::Analyze(info, AnalyzeMode::kRegular); | 1046 DeclarationScope::Analyze(info, AnalyzeMode::kRegular); |
1028 if (!Renumber(info)) return false; | 1047 if (!Renumber(info)) return false; |
1029 DCHECK_NOT_NULL(info->scope()); | 1048 DCHECK_NOT_NULL(info->scope()); |
1030 return true; | 1049 return true; |
1031 } | 1050 } |
1032 | 1051 |
1033 bool Compiler::ParseAndAnalyze(ParseInfo* info) { | 1052 bool Compiler::ParseAndAnalyze(ParseInfo* info) { |
1034 if (!parsing::ParseAny(info)) return false; | 1053 if (!parsing::ParseAny(info)) return false; |
| 1054 if (info->is_toplevel()) EnsureSharedFunctionInfosArrayOnScript(info); |
1035 if (!Compiler::Analyze(info)) return false; | 1055 if (!Compiler::Analyze(info)) return false; |
1036 DCHECK_NOT_NULL(info->literal()); | 1056 DCHECK_NOT_NULL(info->literal()); |
1037 DCHECK_NOT_NULL(info->scope()); | 1057 DCHECK_NOT_NULL(info->scope()); |
1038 return true; | 1058 return true; |
1039 } | 1059 } |
1040 | 1060 |
1041 bool Compiler::Compile(Handle<JSFunction> function, ClearExceptionFlag flag) { | 1061 bool Compiler::Compile(Handle<JSFunction> function, ClearExceptionFlag flag) { |
1042 if (function->is_compiled()) return true; | 1062 if (function->is_compiled()) return true; |
1043 Isolate* isolate = function->GetIsolate(); | 1063 Isolate* isolate = function->GetIsolate(); |
1044 DCHECK(AllowCompilation::IsAllowed(isolate)); | 1064 DCHECK(AllowCompilation::IsAllowed(isolate)); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1145 return true; | 1165 return true; |
1146 } | 1166 } |
1147 | 1167 |
1148 MaybeHandle<JSArray> Compiler::CompileForLiveEdit(Handle<Script> script) { | 1168 MaybeHandle<JSArray> Compiler::CompileForLiveEdit(Handle<Script> script) { |
1149 Isolate* isolate = script->GetIsolate(); | 1169 Isolate* isolate = script->GetIsolate(); |
1150 DCHECK(AllowCompilation::IsAllowed(isolate)); | 1170 DCHECK(AllowCompilation::IsAllowed(isolate)); |
1151 | 1171 |
1152 // In order to ensure that live edit function info collection finds the newly | 1172 // In order to ensure that live edit function info collection finds the newly |
1153 // generated shared function infos, clear the script's list temporarily | 1173 // generated shared function infos, clear the script's list temporarily |
1154 // and restore it at the end of this method. | 1174 // and restore it at the end of this method. |
1155 Handle<Object> old_function_infos(script->shared_function_infos(), isolate); | 1175 Handle<FixedArray> old_function_infos(script->shared_function_infos(), |
1156 script->set_shared_function_infos(Smi::kZero); | 1176 isolate); |
| 1177 script->set_shared_function_infos(isolate->heap()->empty_fixed_array()); |
1157 | 1178 |
1158 // Start a compilation. | 1179 // Start a compilation. |
1159 Zone zone(isolate->allocator(), ZONE_NAME); | 1180 Zone zone(isolate->allocator(), ZONE_NAME); |
1160 ParseInfo parse_info(&zone, script); | 1181 ParseInfo parse_info(&zone, script); |
1161 CompilationInfo info(&parse_info, Handle<JSFunction>::null()); | 1182 CompilationInfo info(&parse_info, Handle<JSFunction>::null()); |
1162 info.MarkAsDebug(); | 1183 info.MarkAsDebug(); |
1163 | 1184 |
1164 // TODO(635): support extensions. | 1185 // TODO(635): support extensions. |
1165 const bool compilation_succeeded = !CompileToplevel(&info).is_null(); | 1186 const bool compilation_succeeded = !CompileToplevel(&info).is_null(); |
1166 Handle<JSArray> infos; | 1187 Handle<JSArray> infos; |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1556 | 1577 |
1557 | 1578 |
1558 Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfo( | 1579 Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfo( |
1559 FunctionLiteral* literal, Handle<Script> script, | 1580 FunctionLiteral* literal, Handle<Script> script, |
1560 CompilationInfo* outer_info) { | 1581 CompilationInfo* outer_info) { |
1561 // Precondition: code has been parsed and scopes have been analyzed. | 1582 // Precondition: code has been parsed and scopes have been analyzed. |
1562 Isolate* isolate = outer_info->isolate(); | 1583 Isolate* isolate = outer_info->isolate(); |
1563 MaybeHandle<SharedFunctionInfo> maybe_existing; | 1584 MaybeHandle<SharedFunctionInfo> maybe_existing; |
1564 | 1585 |
1565 // Find any previously allocated shared function info for the given literal. | 1586 // Find any previously allocated shared function info for the given literal. |
1566 if (outer_info->shared_info()->never_compiled()) { | 1587 maybe_existing = script->FindSharedFunctionInfo(isolate, literal); |
1567 // On the first compile, there are no existing shared function info for | |
1568 // inner functions yet, so do not try to find them. All bets are off for | |
1569 // live edit though. | |
1570 SLOW_DCHECK(script->FindSharedFunctionInfo(literal).is_null() || | |
1571 isolate->debug()->live_edit_enabled()); | |
1572 } else { | |
1573 maybe_existing = script->FindSharedFunctionInfo(literal); | |
1574 } | |
1575 | 1588 |
1576 // We found an existing shared function info. If it has any sort of code | 1589 // We found an existing shared function info. If it has any sort of code |
1577 // attached, don't worry about compiling and simply return it. Otherwise, | 1590 // attached, don't worry about compiling and simply return it. Otherwise, |
1578 // continue to decide whether to eagerly compile. | 1591 // continue to decide whether to eagerly compile. |
1579 // Note that we also carry on if we are compiling eager to obtain code for | 1592 // Note that we also carry on if we are compiling eager to obtain code for |
1580 // debugging, unless we already have code with debug break slots. | 1593 // debugging, unless we already have code with debug break slots. |
1581 Handle<SharedFunctionInfo> existing; | 1594 Handle<SharedFunctionInfo> existing; |
1582 if (maybe_existing.ToHandle(&existing)) { | 1595 if (maybe_existing.ToHandle(&existing)) { |
1583 DCHECK(!existing->is_toplevel()); | 1596 DCHECK(!existing->is_toplevel()); |
1584 if (existing->HasBaselineCode() || existing->HasBytecodeArray()) { | 1597 if (existing->HasBaselineCode() || existing->HasBytecodeArray()) { |
1585 if (!outer_info->is_debug() || existing->HasDebugCode()) { | 1598 if (!outer_info->is_debug() || existing->HasDebugCode()) { |
1586 return existing; | 1599 return existing; |
1587 } | 1600 } |
1588 } | 1601 } |
1589 } | 1602 } |
1590 | 1603 |
1591 // Allocate a shared function info object. | 1604 // Allocate a shared function info object. |
1592 Handle<SharedFunctionInfo> result; | 1605 Handle<SharedFunctionInfo> result; |
1593 if (!maybe_existing.ToHandle(&result)) { | 1606 if (!maybe_existing.ToHandle(&result)) { |
1594 result = | 1607 result = |
1595 isolate->factory()->NewSharedFunctionInfoForLiteral(literal, script); | 1608 isolate->factory()->NewSharedFunctionInfoForLiteral(literal, script); |
1596 result->set_is_toplevel(false); | 1609 result->set_is_toplevel(false); |
1597 | |
1598 // If the outer function has been compiled before, we cannot be sure that | |
1599 // shared function info for this function literal has been created for the | |
1600 // first time. It may have already been compiled previously. | |
1601 result->set_never_compiled(outer_info->shared_info()->never_compiled()); | |
1602 } | 1610 } |
1603 | 1611 |
1604 Zone zone(isolate->allocator(), ZONE_NAME); | 1612 Zone zone(isolate->allocator(), ZONE_NAME); |
1605 ParseInfo parse_info(&zone, script); | 1613 ParseInfo parse_info(&zone, script); |
1606 CompilationInfo info(&parse_info, Handle<JSFunction>::null()); | 1614 CompilationInfo info(&parse_info, Handle<JSFunction>::null()); |
1607 parse_info.set_literal(literal); | 1615 parse_info.set_literal(literal); |
1608 parse_info.set_shared_info(result); | 1616 parse_info.set_shared_info(result); |
1609 parse_info.set_function_literal_id(result->function_literal_id()); | 1617 parse_info.set_function_literal_id(result->function_literal_id()); |
1610 parse_info.set_language_mode(literal->scope()->language_mode()); | 1618 parse_info.set_language_mode(literal->scope()->language_mode()); |
1611 parse_info.set_ast_value_factory( | 1619 parse_info.set_ast_value_factory( |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1746 DCHECK(shared->is_compiled()); | 1754 DCHECK(shared->is_compiled()); |
1747 function->set_literals(cached.literals); | 1755 function->set_literals(cached.literals); |
1748 } else if (shared->is_compiled()) { | 1756 } else if (shared->is_compiled()) { |
1749 // TODO(mvstanton): pass pretenure flag to EnsureLiterals. | 1757 // TODO(mvstanton): pass pretenure flag to EnsureLiterals. |
1750 JSFunction::EnsureLiterals(function); | 1758 JSFunction::EnsureLiterals(function); |
1751 } | 1759 } |
1752 } | 1760 } |
1753 | 1761 |
1754 } // namespace internal | 1762 } // namespace internal |
1755 } // namespace v8 | 1763 } // namespace v8 |
OLD | NEW |