summaryrefslogtreecommitdiff
path: root/deps/v8/src/compiler.cc
diff options
context:
space:
mode:
Diffstat (limited to 'deps/v8/src/compiler.cc')
-rw-r--r--deps/v8/src/compiler.cc415
1 files changed, 232 insertions, 183 deletions
diff --git a/deps/v8/src/compiler.cc b/deps/v8/src/compiler.cc
index 3435f530c2..5e23be5e5c 100644
--- a/deps/v8/src/compiler.cc
+++ b/deps/v8/src/compiler.cc
@@ -28,7 +28,7 @@
#include "src/isolate-inl.h"
#include "src/log-inl.h"
#include "src/messages.h"
-#include "src/parsing/parser.h"
+#include "src/parsing/parsing.h"
#include "src/parsing/rewriter.h"
#include "src/parsing/scanner-character-streams.h"
#include "src/runtime-profiler.h"
@@ -38,8 +38,6 @@
namespace v8 {
namespace internal {
-
-
// A wrapper around a CompilationInfo that detaches the Handles from
// the underlying DeferredHandleScope and stores them in info_ on
// destruction.
@@ -70,6 +68,15 @@ struct ScopedTimer {
// ----------------------------------------------------------------------------
// Implementation of CompilationJob
+CompilationJob::CompilationJob(Isolate* isolate, CompilationInfo* info,
+ const char* compiler_name, State initial_state)
+ : info_(info),
+ isolate_thread_id_(isolate->thread_id()),
+ compiler_name_(compiler_name),
+ state_(initial_state),
+ stack_limit_(isolate->stack_guard()->real_climit()),
+ executed_on_background_thread_(false) {}
+
CompilationJob::Status CompilationJob::PrepareJob() {
DCHECK(ThreadId::Current().Equals(info()->isolate()->thread_id()));
DisallowJavascriptExecution no_js(isolate());
@@ -98,8 +105,10 @@ CompilationJob::Status CompilationJob::ExecuteJob() {
no_handles.reset(new DisallowHandleAllocation());
no_deref.reset(new DisallowHandleDereference());
no_dependency_change.reset(new DisallowCodeDependencyChange());
+ executed_on_background_thread_ =
+ !ThreadId::Current().Equals(isolate_thread_id_);
} else {
- DCHECK(ThreadId::Current().Equals(info()->isolate()->thread_id()));
+ DCHECK(ThreadId::Current().Equals(isolate_thread_id_));
}
// Delegate to the underlying implementation.
@@ -284,7 +293,7 @@ void RecordFunctionCompilation(CodeEventListener::LogEventsAndTags tag,
void EnsureFeedbackMetadata(CompilationInfo* info) {
DCHECK(info->has_shared_info());
- // If no type feedback metadata exists, we create it now. At this point the
+ // If no type feedback metadata exists, create it. At this point the
// AstNumbering pass has already run. Note the snapshot can contain outdated
// vectors for a different configuration, hence we also recreate a new vector
// when the function is not compiled (i.e. no code was serialized).
@@ -305,7 +314,7 @@ void EnsureFeedbackMetadata(CompilationInfo* info) {
bool UseTurboFan(Handle<SharedFunctionInfo> shared) {
bool optimization_disabled = shared->optimization_disabled();
- bool dont_crankshaft = shared->dont_crankshaft();
+ bool must_use_ignition_turbo = shared->must_use_ignition_turbo();
// Check the enabling conditions for Turbofan.
// 1. "use asm" code.
@@ -314,7 +323,7 @@ bool UseTurboFan(Handle<SharedFunctionInfo> shared) {
// 2. Fallback for features unsupported by Crankshaft.
bool is_unsupported_by_crankshaft_but_turbofanable =
- dont_crankshaft && strcmp(FLAG_turbo_filter, "~~") == 0 &&
+ must_use_ignition_turbo && strcmp(FLAG_turbo_filter, "~~") == 0 &&
!optimization_disabled;
// 3. Explicitly enabled by the command-line filter.
@@ -326,32 +335,44 @@ bool UseTurboFan(Handle<SharedFunctionInfo> shared) {
bool ShouldUseIgnition(CompilationInfo* info) {
DCHECK(info->has_shared_info());
+ Handle<SharedFunctionInfo> shared = info->shared_info();
+
+ // Code which can't be supported by the old pipeline should use Ignition.
+ if (shared->must_use_ignition_turbo()) return true;
+
+ // Resumable functions are not supported by {FullCodeGenerator}, suspended
+ // activations stored as {JSGeneratorObject} on the heap always assume the
+ // underlying code to be based on the bytecode array.
+ DCHECK(!IsResumableFunction(shared->kind()));
// Skip Ignition for asm.js functions.
- if (info->shared_info()->asm_function()) {
+ if (shared->asm_function()) return false;
+
+ // Skip Ignition for asm wasm code.
+ if (FLAG_validate_asm && shared->HasAsmWasmData()) {
return false;
}
// When requesting debug code as a replacement for existing code, we provide
// the same kind as the existing code (to prevent implicit tier-change).
- if (info->is_debug() && info->shared_info()->is_compiled()) {
- return !info->shared_info()->HasBaselineCode();
+ if (info->is_debug() && shared->is_compiled()) {
+ return !shared->HasBaselineCode();
}
// Code destined for TurboFan should be compiled with Ignition first.
- if (UseTurboFan(info->shared_info())) return true;
+ if (UseTurboFan(shared)) return true;
// Only use Ignition for any other function if FLAG_ignition is true.
if (!FLAG_ignition) return false;
// Checks whether top level functions should be passed by the filter.
- if (info->shared_info()->is_toplevel()) {
+ if (shared->is_toplevel()) {
Vector<const char> filter = CStrVector(FLAG_ignition_filter);
return (filter.length() == 0) || (filter.length() == 1 && filter[0] == '*');
}
// Finally respect the filter.
- return info->shared_info()->PassesFilter(FLAG_ignition_filter);
+ return shared->PassesFilter(FLAG_ignition_filter);
}
CompilationJob* GetUnoptimizedCompilationJob(CompilationInfo* info) {
@@ -360,7 +381,6 @@ CompilationJob* GetUnoptimizedCompilationJob(CompilationInfo* info) {
DCHECK_NOT_NULL(info->literal());
DCHECK_NOT_NULL(info->scope());
- EnsureFeedbackMetadata(info);
if (ShouldUseIgnition(info)) {
return interpreter::Interpreter::NewCompilationJob(info);
} else {
@@ -407,18 +427,42 @@ void InstallUnoptimizedCode(CompilationInfo* info) {
CompilationJob::Status FinalizeUnoptimizedCompilationJob(CompilationJob* job) {
CompilationJob::Status status = job->FinalizeJob();
if (status == CompilationJob::SUCCEEDED) {
+ EnsureFeedbackMetadata(job->info());
InstallUnoptimizedCode(job->info());
job->RecordUnoptimizedCompilationStats();
}
return status;
}
+bool Renumber(ParseInfo* parse_info,
+ Compiler::EagerInnerFunctionLiterals* eager_literals) {
+ RuntimeCallTimerScope runtimeTimer(parse_info->isolate(),
+ &RuntimeCallStats::CompileRenumber);
+ if (!AstNumbering::Renumber(
+ parse_info->isolate()->stack_guard()->real_climit(),
+ parse_info->zone(), parse_info->literal(), eager_literals)) {
+ return false;
+ }
+ Handle<SharedFunctionInfo> shared_info = parse_info->shared_info();
+ if (!shared_info.is_null()) {
+ FunctionLiteral* lit = parse_info->literal();
+ shared_info->set_ast_node_count(lit->ast_node_count());
+ if (lit->dont_optimize_reason() != kNoReason) {
+ shared_info->DisableOptimization(lit->dont_optimize_reason());
+ }
+ if (lit->flags() & AstProperties::kMustUseIgnitionTurbo) {
+ shared_info->set_must_use_ignition_turbo(true);
+ }
+ }
+ return true;
+}
+
bool GenerateUnoptimizedCode(CompilationInfo* info) {
if (FLAG_validate_asm && info->scope()->asm_module() &&
!info->shared_info()->is_asm_wasm_broken() && !info->is_debug()) {
EnsureFeedbackMetadata(info);
MaybeHandle<FixedArray> wasm_data;
- wasm_data = AsmJs::ConvertAsmToWasm(info->parse_info());
+ wasm_data = AsmJs::CompileAsmViaWasm(info);
if (!wasm_data.is_null()) {
info->shared_info()->set_asm_wasm_data(*wasm_data.ToHandleChecked());
info->SetCode(info->isolate()->builtins()->InstantiateAsmJs());
@@ -437,23 +481,104 @@ bool GenerateUnoptimizedCode(CompilationInfo* info) {
return true;
}
+bool CompileUnoptimizedInnerFunctionsRecursively(
+ ThreadedList<ThreadedListZoneEntry<FunctionLiteral*>>* literals,
+ CompilationInfo* outer_info) {
+ Isolate* isolate = outer_info->isolate();
+ Handle<Script> script = outer_info->script();
+ RuntimeCallTimerScope runtimeTimer(isolate,
+ &RuntimeCallStats::CompileInnerFunction);
+
+ for (auto it : *literals) {
+ FunctionLiteral* literal = it->value();
+
+ // Find any previously allocated shared function info for the given literal.
+ Handle<SharedFunctionInfo> shared;
+ MaybeHandle<SharedFunctionInfo> maybe_existing =
+ script->FindSharedFunctionInfo(isolate, literal);
+ if (maybe_existing.ToHandle(&shared)) {
+ DCHECK(!shared->is_toplevel());
+ // If we found an existing shared function info with compiled code,
+ // we are done.
+ if (shared->is_compiled()) continue;
+ } else {
+ shared =
+ isolate->factory()->NewSharedFunctionInfoForLiteral(literal, script);
+ shared->set_is_toplevel(false);
+ }
+
+ Zone zone(isolate->allocator(), ZONE_NAME);
+ ParseInfo parse_info(&zone, script);
+ parse_info.set_literal(literal);
+ parse_info.set_shared_info(shared);
+ parse_info.set_function_literal_id(shared->function_literal_id());
+ parse_info.set_language_mode(literal->scope()->language_mode());
+ parse_info.set_ast_value_factory(
+ outer_info->parse_info()->ast_value_factory());
+ parse_info.set_ast_value_factory_owned(false);
+
+ CompilationInfo info(&parse_info, Handle<JSFunction>::null());
+ if (outer_info->will_serialize()) info.PrepareForSerializing();
+ if (outer_info->is_debug()) info.MarkAsDebug();
+
+ Compiler::EagerInnerFunctionLiterals inner_literals;
+ if (!Renumber(&parse_info, &inner_literals) ||
+ !CompileUnoptimizedInnerFunctionsRecursively(&inner_literals,
+ outer_info) ||
+ !GenerateUnoptimizedCode(&info)) {
+ if (!isolate->has_pending_exception()) isolate->StackOverflow();
+ return false;
+ }
+
+ DCHECK(!info.code().is_null());
+ RecordFunctionCompilation(CodeEventListener::FUNCTION_TAG, &info);
+ if (literal->should_be_used_once_hint()) {
+ info.code()->MarkToBeExecutedOnce(isolate);
+ }
+ }
+ return true;
+}
+
bool CompileUnoptimizedCode(CompilationInfo* info) {
- DCHECK(AllowCompilation::IsAllowed(info->isolate()));
- if (!Compiler::Analyze(info->parse_info()) ||
+ Isolate* isolate = info->isolate();
+ DCHECK(AllowCompilation::IsAllowed(isolate));
+
+ Compiler::EagerInnerFunctionLiterals inner_literals;
+ if (!Compiler::Analyze(info->parse_info(), &inner_literals) ||
+ !CompileUnoptimizedInnerFunctionsRecursively(&inner_literals, info) ||
!GenerateUnoptimizedCode(info)) {
- Isolate* isolate = info->isolate();
if (!isolate->has_pending_exception()) isolate->StackOverflow();
return false;
}
+
return true;
}
+void EnsureSharedFunctionInfosArrayOnScript(ParseInfo* info) {
+ DCHECK(info->is_toplevel());
+ DCHECK(!info->script().is_null());
+ if (info->script()->shared_function_infos()->length() > 0) {
+ DCHECK_EQ(info->script()->shared_function_infos()->length(),
+ info->max_function_literal_id() + 1);
+ return;
+ }
+ Isolate* isolate = info->isolate();
+ Handle<FixedArray> infos(
+ isolate->factory()->NewFixedArray(info->max_function_literal_id() + 1));
+ info->script()->set_shared_function_infos(*infos);
+}
+
MUST_USE_RESULT MaybeHandle<Code> GetUnoptimizedCode(CompilationInfo* info) {
+ RuntimeCallTimerScope runtimeTimer(
+ info->isolate(), &RuntimeCallStats::CompileGetUnoptimizedCode);
VMState<COMPILER> state(info->isolate());
PostponeInterruptsScope postpone(info->isolate());
// Parse and update CompilationInfo with the results.
- if (!Parser::ParseStatic(info->parse_info())) return MaybeHandle<Code>();
+ if (!parsing::ParseAny(info->parse_info())) return MaybeHandle<Code>();
+ if (info->parse_info()->is_toplevel()) {
+ EnsureSharedFunctionInfosArrayOnScript(info->parse_info());
+ }
DCHECK_EQ(info->shared_info()->language_mode(),
info->literal()->language_mode());
@@ -468,6 +593,9 @@ MUST_USE_RESULT MaybeHandle<Code> GetUnoptimizedCode(CompilationInfo* info) {
MUST_USE_RESULT MaybeHandle<Code> GetCodeFromOptimizedCodeMap(
Handle<JSFunction> function, BailoutId osr_ast_id) {
+ RuntimeCallTimerScope runtimeTimer(
+ function->GetIsolate(),
+ &RuntimeCallStats::CompileGetFromOptimizedCodeMap);
Handle<SharedFunctionInfo> shared(function->shared());
DisallowHeapAllocation no_gc;
CodeAndLiterals cached = shared->SearchOptimizedCodeMap(
@@ -505,25 +633,6 @@ void InsertCodeIntoOptimizedCodeMap(CompilationInfo* info) {
literals, info->osr_ast_id());
}
-bool Renumber(ParseInfo* parse_info) {
- if (!AstNumbering::Renumber(parse_info->isolate(), parse_info->zone(),
- parse_info->literal())) {
- return false;
- }
- Handle<SharedFunctionInfo> shared_info = parse_info->shared_info();
- if (!shared_info.is_null()) {
- FunctionLiteral* lit = parse_info->literal();
- shared_info->set_ast_node_count(lit->ast_node_count());
- if (lit->dont_optimize_reason() != kNoReason) {
- shared_info->DisableOptimization(lit->dont_optimize_reason());
- }
- if (lit->flags() & AstProperties::kDontCrankshaft) {
- shared_info->set_dont_crankshaft(true);
- }
- }
- return true;
-}
-
bool GetOptimizedCodeNow(CompilationJob* job) {
CompilationInfo* info = job->info();
Isolate* isolate = info->isolate();
@@ -640,6 +749,7 @@ MaybeHandle<Code> GetOptimizedCode(Handle<JSFunction> function,
}
// Reset profiler ticks, function is no longer considered hot.
+ DCHECK(shared->is_compiled());
if (shared->HasBaselineCode()) {
shared->code()->set_profiler_ticks(0);
} else if (shared->HasBytecodeArray()) {
@@ -668,7 +778,7 @@ MaybeHandle<Code> GetOptimizedCode(Handle<JSFunction> function,
const int kMaxOptCount =
FLAG_deopt_every_n_times == 0 ? FLAG_max_opt_count : 1000;
if (info->shared_info()->opt_count() > kMaxOptCount) {
- info->AbortOptimization(kOptimizedTooManyTimes);
+ info->AbortOptimization(kDeoptimizedTooManyTimes);
return MaybeHandle<Code>();
}
@@ -679,10 +789,7 @@ MaybeHandle<Code> GetOptimizedCode(Handle<JSFunction> function,
// TurboFan can optimize directly from existing bytecode.
if (use_turbofan && ShouldUseIgnition(info)) {
if (info->is_osr() && !ignition_osr) return MaybeHandle<Code>();
- if (!Compiler::EnsureBytecode(info)) {
- if (isolate->has_pending_exception()) isolate->clear_pending_exception();
- return MaybeHandle<Code>();
- }
+ DCHECK(shared->HasBytecodeArray());
info->MarkAsOptimizeFromBytecode();
}
@@ -793,6 +900,8 @@ MaybeHandle<Code> GetBaselineCode(Handle<JSFunction> function) {
ParseInfo parse_info(&zone, handle(function->shared()));
CompilationInfo info(&parse_info, function);
+ DCHECK(function->shared()->is_compiled());
+
// Function no longer needs to be tiered up
function->shared()->set_marked_for_tier_up(false);
@@ -812,13 +921,11 @@ MaybeHandle<Code> GetBaselineCode(Handle<JSFunction> function) {
return MaybeHandle<Code>();
}
- // TODO(4280): For now we do not switch generators or async functions to
- // baseline code because there might be suspended activations stored in
- // generator objects on the heap. We could eventually go directly to
- // TurboFan in this case.
- if (IsResumableFunction(function->shared()->kind())) {
+ // Don't generate full-codegen code for functions it can't support.
+ if (function->shared()->must_use_ignition_turbo()) {
return MaybeHandle<Code>();
}
+ DCHECK(!IsResumableFunction(function->shared()->kind()));
if (FLAG_trace_opt) {
OFStream os(stdout);
@@ -827,7 +934,7 @@ MaybeHandle<Code> GetBaselineCode(Handle<JSFunction> function) {
}
// Parse and update CompilationInfo with the results.
- if (!Parser::ParseStatic(info.parse_info())) return MaybeHandle<Code>();
+ if (!parsing::ParseFunction(info.parse_info())) return MaybeHandle<Code>();
Handle<SharedFunctionInfo> shared = info.shared_info();
DCHECK_EQ(shared->language_mode(), info.literal()->language_mode());
@@ -856,7 +963,7 @@ MaybeHandle<Code> GetLazyCode(Handle<JSFunction> function) {
DCHECK(!function->is_compiled());
TimerEventScope<TimerEventCompileCode> compile_timer(isolate);
RuntimeCallTimerScope runtimeTimer(isolate,
- &RuntimeCallStats::CompileCodeLazy);
+ &RuntimeCallStats::CompileFunction);
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.CompileCode");
AggregatedHistogramTimerScope timer(isolate->counters()->compile_lazy());
@@ -872,7 +979,8 @@ MaybeHandle<Code> GetLazyCode(Handle<JSFunction> function) {
return cached_code;
}
- if (function->shared()->marked_for_tier_up()) {
+ if (function->shared()->is_compiled() &&
+ function->shared()->marked_for_tier_up()) {
DCHECK(FLAG_mark_shared_functions_for_tier_up);
function->shared()->set_marked_for_tier_up(false);
@@ -928,7 +1036,7 @@ MaybeHandle<Code> GetLazyCode(Handle<JSFunction> function) {
Handle<Code> result;
ASSIGN_RETURN_ON_EXCEPTION(isolate, result, GetUnoptimizedCode(&info), Code);
- if (FLAG_always_opt) {
+ if (FLAG_always_opt && !info.shared_info()->HasAsmWasmData()) {
Handle<Code> opt_code;
if (GetOptimizedCode(function, Compiler::NOT_CONCURRENT)
.ToHandle(&opt_code)) {
@@ -940,49 +1048,37 @@ MaybeHandle<Code> GetLazyCode(Handle<JSFunction> function) {
}
-Handle<SharedFunctionInfo> NewSharedFunctionInfoForLiteral(
- Isolate* isolate, FunctionLiteral* literal, Handle<Script> script) {
- Handle<Code> code = isolate->builtins()->CompileLazy();
- Handle<ScopeInfo> scope_info = handle(ScopeInfo::Empty(isolate));
- Handle<SharedFunctionInfo> result = isolate->factory()->NewSharedFunctionInfo(
- literal->name(), literal->materialized_literal_count(), literal->kind(),
- code, scope_info);
- SharedFunctionInfo::InitFromFunctionLiteral(result, literal);
- SharedFunctionInfo::SetScript(result, script);
- return result;
-}
-
Handle<SharedFunctionInfo> CompileToplevel(CompilationInfo* info) {
Isolate* isolate = info->isolate();
TimerEventScope<TimerEventCompileCode> timer(isolate);
- RuntimeCallTimerScope runtimeTimer(isolate, &RuntimeCallStats::CompileCode);
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.CompileCode");
PostponeInterruptsScope postpone(isolate);
DCHECK(!isolate->native_context().is_null());
ParseInfo* parse_info = info->parse_info();
+
+ RuntimeCallTimerScope runtimeTimer(
+ isolate, parse_info->is_eval() ? &RuntimeCallStats::CompileEval
+ : &RuntimeCallStats::CompileScript);
+
Handle<Script> script = parse_info->script();
// TODO(svenpanne) Obscure place for this, perhaps move to OnBeforeCompile?
FixedArray* array = isolate->native_context()->embedder_data();
script->set_context_data(array->get(v8::Context::kDebugIdIndex));
- isolate->debug()->OnBeforeCompile(script);
-
Handle<SharedFunctionInfo> result;
{ VMState<COMPILER> state(info->isolate());
- if (parse_info->literal() == nullptr && !Parser::ParseStatic(parse_info)) {
+ if (parse_info->literal() == nullptr &&
+ !parsing::ParseProgram(parse_info)) {
return Handle<SharedFunctionInfo>::null();
}
- FunctionLiteral* lit = parse_info->literal();
+ EnsureSharedFunctionInfosArrayOnScript(parse_info);
// Measure how long it takes to do the compilation; only take the
// rest of the function into account to avoid overlap with the
// parsing statistics.
- RuntimeCallTimerScope runtimeTimer(
- isolate, parse_info->is_eval() ? &RuntimeCallStats::CompileEval
- : &RuntimeCallStats::Compile);
HistogramTimer* rate = parse_info->is_eval()
? info->isolate()->counters()->compile_eval()
: info->isolate()->counters()->compile();
@@ -991,10 +1087,12 @@ Handle<SharedFunctionInfo> CompileToplevel(CompilationInfo* info) {
parse_info->is_eval() ? "V8.CompileEval" : "V8.Compile");
// Allocate a shared function info object.
+ FunctionLiteral* lit = parse_info->literal();
DCHECK_EQ(kNoSourcePosition, lit->function_token_position());
- result = NewSharedFunctionInfoForLiteral(isolate, lit, script);
+ result = isolate->factory()->NewSharedFunctionInfoForLiteral(lit, script);
result->set_is_toplevel(true);
parse_info->set_shared_info(result);
+ parse_info->set_function_literal_id(result->function_literal_id());
// Compile the code.
if (!CompileUnoptimizedCode(info)) {
@@ -1025,17 +1123,21 @@ Handle<SharedFunctionInfo> CompileToplevel(CompilationInfo* info) {
// ----------------------------------------------------------------------------
// Implementation of Compiler
-bool Compiler::Analyze(ParseInfo* info) {
+bool Compiler::Analyze(ParseInfo* info,
+ EagerInnerFunctionLiterals* eager_literals) {
DCHECK_NOT_NULL(info->literal());
+ RuntimeCallTimerScope runtimeTimer(info->isolate(),
+ &RuntimeCallStats::CompileAnalyse);
if (!Rewriter::Rewrite(info)) return false;
DeclarationScope::Analyze(info, AnalyzeMode::kRegular);
- if (!Renumber(info)) return false;
+ if (!Renumber(info, eager_literals)) return false;
DCHECK_NOT_NULL(info->scope());
return true;
}
bool Compiler::ParseAndAnalyze(ParseInfo* info) {
- if (!Parser::ParseStatic(info)) return false;
+ if (!parsing::ParseAny(info)) return false;
+ if (info->is_toplevel()) EnsureSharedFunctionInfosArrayOnScript(info);
if (!Compiler::Analyze(info)) return false;
DCHECK_NOT_NULL(info->literal());
DCHECK_NOT_NULL(info->scope());
@@ -1156,8 +1258,9 @@ MaybeHandle<JSArray> Compiler::CompileForLiveEdit(Handle<Script> script) {
// In order to ensure that live edit function info collection finds the newly
// generated shared function infos, clear the script's list temporarily
// and restore it at the end of this method.
- Handle<Object> old_function_infos(script->shared_function_infos(), isolate);
- script->set_shared_function_infos(Smi::kZero);
+ Handle<FixedArray> old_function_infos(script->shared_function_infos(),
+ isolate);
+ script->set_shared_function_infos(isolate->heap()->empty_fixed_array());
// Start a compilation.
Zone zone(isolate->allocator(), ZONE_NAME);
@@ -1184,21 +1287,15 @@ MaybeHandle<JSArray> Compiler::CompileForLiveEdit(Handle<Script> script) {
}
bool Compiler::EnsureBytecode(CompilationInfo* info) {
- if (!ShouldUseIgnition(info)) return false;
- if (!info->shared_info()->HasBytecodeArray()) {
- Handle<Code> original_code(info->shared_info()->code());
+ if (!info->shared_info()->is_compiled()) {
if (GetUnoptimizedCode(info).is_null()) return false;
- if (info->shared_info()->HasAsmWasmData()) return false;
- DCHECK(info->shared_info()->is_compiled());
- if (original_code->kind() == Code::FUNCTION) {
- // Generating bytecode will install the {InterpreterEntryTrampoline} as
- // shared code on the function. To avoid an implicit tier down we restore
- // original baseline code in case it existed beforehand.
- info->shared_info()->ReplaceCode(*original_code);
- }
}
- DCHECK(info->shared_info()->HasBytecodeArray());
- return true;
+ DCHECK(info->shared_info()->is_compiled());
+
+ if (info->shared_info()->HasAsmWasmData()) return false;
+
+ DCHECK_EQ(ShouldUseIgnition(info), info->shared_info()->HasBytecodeArray());
+ return info->shared_info()->HasBytecodeArray();
}
// TODO(turbofan): In the future, unoptimized code with deopt support could
@@ -1212,11 +1309,9 @@ bool Compiler::EnsureDeoptimizationSupport(CompilationInfo* info) {
CompilationInfo unoptimized(info->parse_info(), info->closure());
unoptimized.EnableDeoptimizationSupport();
- // TODO(4280): For now we do not switch generators or async functions to
- // baseline code because there might be suspended activations stored in
- // generator objects on the heap. We could eventually go directly to
- // TurboFan in this case.
- if (IsResumableFunction(shared->kind())) return false;
+ // Don't generate full-codegen code for functions it can't support.
+ if (shared->must_use_ignition_turbo()) return false;
+ DCHECK(!IsResumableFunction(shared->kind()));
// When we call PrepareForSerializing below, we will change the shared
// ParseInfo. Make sure to reset it.
@@ -1230,6 +1325,14 @@ bool Compiler::EnsureDeoptimizationSupport(CompilationInfo* info) {
unoptimized.PrepareForSerializing();
}
EnsureFeedbackMetadata(&unoptimized);
+
+ // Ensure we generate and install bytecode first if the function should use
+ // Ignition to avoid implicit tier-down.
+ if (!shared->is_compiled() && ShouldUseIgnition(info) &&
+ !GenerateUnoptimizedCode(info)) {
+ return false;
+ }
+
if (!FullCodeGenerator::MakeCode(&unoptimized)) return false;
info->parse_info()->set_will_serialize(old_will_serialize_value);
@@ -1284,7 +1387,9 @@ MaybeHandle<JSFunction> Compiler::GetFunctionFromEval(
Handle<Script> script;
if (!maybe_shared_info.ToHandle(&shared_info)) {
script = isolate->factory()->NewScript(source);
- if (FLAG_trace_deopt) Script::InitLineEnds(script);
+ if (isolate->NeedsSourcePositionsForProfiling()) {
+ Script::InitLineEnds(script);
+ }
if (!script_name.is_null()) {
script->set_name(*script_name);
script->set_line_offset(line_offset);
@@ -1347,6 +1452,15 @@ bool CodeGenerationFromStringsAllowed(Isolate* isolate,
}
}
+bool ContainsAsmModule(Handle<Script> script) {
+ DisallowHeapAllocation no_gc;
+ SharedFunctionInfo::ScriptIterator iter(script);
+ while (SharedFunctionInfo* info = iter.Next()) {
+ if (info->HasAsmWasmData()) return true;
+ }
+ return false;
+}
+
} // namespace
MaybeHandle<JSFunction> Compiler::GetFunctionFromString(
@@ -1444,13 +1558,15 @@ Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfoForScript(
// Create a script object describing the script to be compiled.
Handle<Script> script = isolate->factory()->NewScript(source);
- if (FLAG_trace_deopt) Script::InitLineEnds(script);
+ if (isolate->NeedsSourcePositionsForProfiling()) {
+ Script::InitLineEnds(script);
+ }
if (natives == NATIVES_CODE) {
script->set_type(Script::TYPE_NATIVE);
- script->set_hide_source(true);
} else if (natives == EXTENSION_CODE) {
script->set_type(Script::TYPE_EXTENSION);
- script->set_hide_source(true);
+ } else if (natives == INSPECTOR_CODE) {
+ script->set_type(Script::TYPE_INSPECTOR);
}
if (!script_name.is_null()) {
script->set_name(*script_name);
@@ -1486,7 +1602,8 @@ Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfoForScript(
if (extension == NULL && !result.is_null()) {
compilation_cache->PutScript(source, context, language_mode, result);
if (FLAG_serialize_toplevel &&
- compile_options == ScriptCompiler::kProduceCodeCache) {
+ compile_options == ScriptCompiler::kProduceCodeCache &&
+ !ContainsAsmModule(script)) {
HistogramTimerScope histogram_timer(
isolate->counters()->compile_serialize());
RuntimeCallTimerScope runtimeTimer(isolate,
@@ -1502,7 +1619,7 @@ Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfoForScript(
}
if (result.is_null()) {
- isolate->ReportPendingMessages();
+ if (natives != EXTENSION_CODE) isolate->ReportPendingMessages();
} else {
isolate->debug()->OnAfterCompile(script);
}
@@ -1533,7 +1650,6 @@ Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfoForStreamedScript(
return result;
}
-
Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfo(
FunctionLiteral* literal, Handle<Script> script,
CompilationInfo* outer_info) {
@@ -1542,92 +1658,23 @@ Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfo(
MaybeHandle<SharedFunctionInfo> maybe_existing;
// Find any previously allocated shared function info for the given literal.
- if (outer_info->shared_info()->never_compiled()) {
- // On the first compile, there are no existing shared function info for
- // inner functions yet, so do not try to find them. All bets are off for
- // live edit though.
- SLOW_DCHECK(script->FindSharedFunctionInfo(literal).is_null() ||
- isolate->debug()->live_edit_enabled());
- } else {
- maybe_existing = script->FindSharedFunctionInfo(literal);
- }
+ maybe_existing = script->FindSharedFunctionInfo(isolate, literal);
- // We found an existing shared function info. If it has any sort of code
- // attached, don't worry about compiling and simply return it. Otherwise,
- // continue to decide whether to eagerly compile.
- // Note that we also carry on if we are compiling eager to obtain code for
- // debugging, unless we already have code with debug break slots.
+ // If we found an existing shared function info, return it.
Handle<SharedFunctionInfo> existing;
if (maybe_existing.ToHandle(&existing)) {
DCHECK(!existing->is_toplevel());
- if (existing->HasBaselineCode() || existing->HasBytecodeArray()) {
- if (!outer_info->is_debug() || existing->HasDebugCode()) {
- return existing;
- }
- }
+ return existing;
}
- // Allocate a shared function info object.
- Handle<SharedFunctionInfo> result;
- if (!maybe_existing.ToHandle(&result)) {
- result = NewSharedFunctionInfoForLiteral(isolate, literal, script);
- result->set_is_toplevel(false);
-
- // If the outer function has been compiled before, we cannot be sure that
- // shared function info for this function literal has been created for the
- // first time. It may have already been compiled previously.
- result->set_never_compiled(outer_info->shared_info()->never_compiled());
- }
-
- Zone zone(isolate->allocator(), ZONE_NAME);
- ParseInfo parse_info(&zone, script);
- CompilationInfo info(&parse_info, Handle<JSFunction>::null());
- parse_info.set_literal(literal);
- parse_info.set_shared_info(result);
- parse_info.set_language_mode(literal->scope()->language_mode());
- parse_info.set_ast_value_factory(
- outer_info->parse_info()->ast_value_factory());
- parse_info.set_ast_value_factory_owned(false);
-
- if (outer_info->will_serialize()) info.PrepareForSerializing();
- if (outer_info->is_debug()) info.MarkAsDebug();
-
- // If this inner function is already compiled, we don't need to compile
- // again. When compiling for debug, we are not interested in having debug
- // break slots in inner functions, neither for setting break points nor
- // for revealing inner functions.
- // This is especially important for generators. We must not replace the
- // code for generators, as there may be suspended generator objects.
- if (!result->is_compiled()) {
- if (!literal->ShouldEagerCompile()) {
- info.SetCode(isolate->builtins()->CompileLazy());
- Scope* outer_scope = literal->scope()->GetOuterScopeWithContext();
- if (outer_scope) {
- result->set_outer_scope_info(*outer_scope->scope_info());
- }
- } else {
- // Generate code
- TimerEventScope<TimerEventCompileCode> timer(isolate);
- RuntimeCallTimerScope runtimeTimer(isolate,
- &RuntimeCallStats::CompileCode);
- TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.CompileCode");
- if (Renumber(info.parse_info()) && GenerateUnoptimizedCode(&info)) {
- // Code generation will ensure that the feedback vector is present and
- // appropriately sized.
- DCHECK(!info.code().is_null());
- if (literal->should_be_used_once_hint()) {
- info.code()->MarkToBeExecutedOnce(isolate);
- }
- } else {
- return Handle<SharedFunctionInfo>::null();
- }
- }
- }
-
- if (maybe_existing.is_null()) {
- RecordFunctionCompilation(CodeEventListener::FUNCTION_TAG, &info);
+ // Allocate a shared function info object which will be compiled lazily.
+ Handle<SharedFunctionInfo> result =
+ isolate->factory()->NewSharedFunctionInfoForLiteral(literal, script);
+ result->set_is_toplevel(false);
+ Scope* outer_scope = literal->scope()->GetOuterScopeWithContext();
+ if (outer_scope) {
+ result->set_outer_scope_info(*outer_scope->scope_info());
}
-
return result;
}
@@ -1704,7 +1751,9 @@ void Compiler::PostInstantiation(Handle<JSFunction> function,
PretenureFlag pretenure) {
Handle<SharedFunctionInfo> shared(function->shared());
- if (FLAG_always_opt && shared->allows_lazy_compilation()) {
+ if (FLAG_always_opt && shared->allows_lazy_compilation() &&
+ !function->shared()->HasAsmWasmData() &&
+ function->shared()->is_compiled()) {
function->MarkForOptimization();
}