Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(498)

Side by Side Diff: src/compiler.cc

Issue 2547483002: Store SharedFunctionInfos of a Script in a FixedArray indexed by their ID (Closed)
Patch Set: updates Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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
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 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
1531 1552
1532 1553
1533 Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfo( 1554 Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfo(
1534 FunctionLiteral* literal, Handle<Script> script, 1555 FunctionLiteral* literal, Handle<Script> script,
1535 CompilationInfo* outer_info) { 1556 CompilationInfo* outer_info) {
1536 // Precondition: code has been parsed and scopes have been analyzed. 1557 // Precondition: code has been parsed and scopes have been analyzed.
1537 Isolate* isolate = outer_info->isolate(); 1558 Isolate* isolate = outer_info->isolate();
1538 MaybeHandle<SharedFunctionInfo> maybe_existing; 1559 MaybeHandle<SharedFunctionInfo> maybe_existing;
1539 1560
1540 // Find any previously allocated shared function info for the given literal. 1561 // Find any previously allocated shared function info for the given literal.
1541 if (outer_info->shared_info()->never_compiled()) { 1562 maybe_existing = script->FindSharedFunctionInfo(isolate, literal);
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 }
1550 1563
1551 // We found an existing shared function info. If it has any sort of code 1564 // We found an existing shared function info. If it has any sort of code
1552 // attached, don't worry about compiling and simply return it. Otherwise, 1565 // attached, don't worry about compiling and simply return it. Otherwise,
1553 // continue to decide whether to eagerly compile. 1566 // continue to decide whether to eagerly compile.
1554 // Note that we also carry on if we are compiling eager to obtain code for 1567 // Note that we also carry on if we are compiling eager to obtain code for
1555 // debugging, unless we already have code with debug break slots. 1568 // debugging, unless we already have code with debug break slots.
1556 Handle<SharedFunctionInfo> existing; 1569 Handle<SharedFunctionInfo> existing;
1557 if (maybe_existing.ToHandle(&existing)) { 1570 if (maybe_existing.ToHandle(&existing)) {
1558 DCHECK(!existing->is_toplevel()); 1571 DCHECK(!existing->is_toplevel());
1559 if (existing->HasBaselineCode() || existing->HasBytecodeArray()) { 1572 if (existing->HasBaselineCode() || existing->HasBytecodeArray()) {
1560 if (!outer_info->is_debug() || existing->HasDebugCode()) { 1573 if (!outer_info->is_debug() || existing->HasDebugCode()) {
1561 return existing; 1574 return existing;
1562 } 1575 }
1563 } 1576 }
1564 } 1577 }
1565 1578
1566 // Allocate a shared function info object. 1579 // Allocate a shared function info object.
1567 Handle<SharedFunctionInfo> result; 1580 Handle<SharedFunctionInfo> result;
1568 if (!maybe_existing.ToHandle(&result)) { 1581 if (!maybe_existing.ToHandle(&result)) {
1569 result = 1582 result =
1570 isolate->factory()->NewSharedFunctionInfoForLiteral(literal, script); 1583 isolate->factory()->NewSharedFunctionInfoForLiteral(literal, script);
1571 result->set_is_toplevel(false); 1584 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());
1577 } 1585 }
1578 1586
1579 Zone zone(isolate->allocator(), ZONE_NAME); 1587 Zone zone(isolate->allocator(), ZONE_NAME);
1580 ParseInfo parse_info(&zone, script); 1588 ParseInfo parse_info(&zone, script);
1581 CompilationInfo info(&parse_info, Handle<JSFunction>::null()); 1589 CompilationInfo info(&parse_info, Handle<JSFunction>::null());
1582 parse_info.set_literal(literal); 1590 parse_info.set_literal(literal);
1583 parse_info.set_shared_info(result); 1591 parse_info.set_shared_info(result);
1584 parse_info.set_function_literal_id(result->function_literal_id()); 1592 parse_info.set_function_literal_id(result->function_literal_id());
1585 parse_info.set_language_mode(literal->scope()->language_mode()); 1593 parse_info.set_language_mode(literal->scope()->language_mode());
1586 parse_info.set_ast_value_factory( 1594 parse_info.set_ast_value_factory(
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
1721 DCHECK(shared->is_compiled()); 1729 DCHECK(shared->is_compiled());
1722 function->set_literals(cached.literals); 1730 function->set_literals(cached.literals);
1723 } else if (shared->is_compiled()) { 1731 } else if (shared->is_compiled()) {
1724 // TODO(mvstanton): pass pretenure flag to EnsureLiterals. 1732 // TODO(mvstanton): pass pretenure flag to EnsureLiterals.
1725 JSFunction::EnsureLiterals(function); 1733 JSFunction::EnsureLiterals(function);
1726 } 1734 }
1727 } 1735 }
1728 1736
1729 } // namespace internal 1737 } // namespace internal
1730 } // namespace v8 1738 } // namespace v8
OLDNEW
« no previous file with comments | « src/bootstrapper.cc ('k') | src/crankshaft/hydrogen.cc » ('j') | src/debug/liveedit.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698