Upgrade to 3.29
Update V8 to 3.29.88.17 and update makefiles to support building on
all the relevant platforms.
Bug: 17370214
Change-Id: Ia3407c157fd8d72a93e23d8318ccaf6ecf77fa4e
diff --git a/src/compiler.cc b/src/compiler.cc
index ecac5cb..ea604c9 100644
--- a/src/compiler.cc
+++ b/src/compiler.cc
@@ -1,112 +1,269 @@
// Copyright 2012 the V8 project authors. All rights reserved.
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following
-// disclaimer in the documentation and/or other materials provided
-// with the distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived
-// from this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
-#include "v8.h"
+#include "src/v8.h"
-#include "compiler.h"
+#include "src/compiler.h"
-#include "bootstrapper.h"
-#include "codegen.h"
-#include "compilation-cache.h"
-#include "debug.h"
-#include "full-codegen.h"
-#include "gdb-jit.h"
-#include "hydrogen.h"
-#include "isolate-inl.h"
-#include "lithium.h"
-#include "liveedit.h"
-#include "parser.h"
-#include "rewriter.h"
-#include "runtime-profiler.h"
-#include "scanner-character-streams.h"
-#include "scopeinfo.h"
-#include "scopes.h"
-#include "vm-state-inl.h"
+#include "src/bootstrapper.h"
+#include "src/codegen.h"
+#include "src/compilation-cache.h"
+#include "src/compiler/pipeline.h"
+#include "src/cpu-profiler.h"
+#include "src/debug.h"
+#include "src/deoptimizer.h"
+#include "src/full-codegen.h"
+#include "src/gdb-jit.h"
+#include "src/hydrogen.h"
+#include "src/isolate-inl.h"
+#include "src/lithium.h"
+#include "src/liveedit.h"
+#include "src/parser.h"
+#include "src/rewriter.h"
+#include "src/runtime-profiler.h"
+#include "src/scanner-character-streams.h"
+#include "src/scopeinfo.h"
+#include "src/scopes.h"
+#include "src/typing.h"
+#include "src/vm-state-inl.h"
namespace v8 {
namespace internal {
-CompilationInfo::CompilationInfo(Handle<Script> script)
- : isolate_(script->GetIsolate()),
- flags_(LanguageModeField::encode(CLASSIC_MODE)),
- function_(NULL),
- scope_(NULL),
- global_scope_(NULL),
- script_(script),
- extension_(NULL),
- pre_parse_data_(NULL),
- osr_ast_id_(AstNode::kNoNumber) {
- Initialize(BASE);
+ScriptData::ScriptData(const byte* data, int length)
+ : owns_data_(false), data_(data), length_(length) {
+ if (!IsAligned(reinterpret_cast<intptr_t>(data), kPointerAlignment)) {
+ byte* copy = NewArray<byte>(length);
+ DCHECK(IsAligned(reinterpret_cast<intptr_t>(copy), kPointerAlignment));
+ CopyBytes(copy, data, length);
+ data_ = copy;
+ AcquireDataOwnership();
+ }
}
-CompilationInfo::CompilationInfo(Handle<SharedFunctionInfo> shared_info)
- : isolate_(shared_info->GetIsolate()),
- flags_(LanguageModeField::encode(CLASSIC_MODE) |
- IsLazy::encode(true)),
- function_(NULL),
- scope_(NULL),
- global_scope_(NULL),
+CompilationInfo::CompilationInfo(Handle<Script> script, Zone* zone)
+ : flags_(kThisHasUses),
+ script_(script),
+ source_stream_(NULL),
+ osr_ast_id_(BailoutId::None()),
+ parameter_count_(0),
+ optimization_id_(-1),
+ ast_value_factory_(NULL),
+ ast_value_factory_owned_(false),
+ aborted_due_to_dependency_change_(false) {
+ Initialize(script->GetIsolate(), BASE, zone);
+}
+
+
+CompilationInfo::CompilationInfo(Isolate* isolate, Zone* zone)
+ : flags_(kThisHasUses),
+ script_(Handle<Script>::null()),
+ source_stream_(NULL),
+ osr_ast_id_(BailoutId::None()),
+ parameter_count_(0),
+ optimization_id_(-1),
+ ast_value_factory_(NULL),
+ ast_value_factory_owned_(false),
+ aborted_due_to_dependency_change_(false) {
+ Initialize(isolate, STUB, zone);
+}
+
+
+CompilationInfo::CompilationInfo(Handle<SharedFunctionInfo> shared_info,
+ Zone* zone)
+ : flags_(kLazy | kThisHasUses),
shared_info_(shared_info),
script_(Handle<Script>(Script::cast(shared_info->script()))),
- extension_(NULL),
- pre_parse_data_(NULL),
- osr_ast_id_(AstNode::kNoNumber) {
- Initialize(BASE);
+ source_stream_(NULL),
+ osr_ast_id_(BailoutId::None()),
+ parameter_count_(0),
+ optimization_id_(-1),
+ ast_value_factory_(NULL),
+ ast_value_factory_owned_(false),
+ aborted_due_to_dependency_change_(false) {
+ Initialize(script_->GetIsolate(), BASE, zone);
}
-CompilationInfo::CompilationInfo(Handle<JSFunction> closure)
- : isolate_(closure->GetIsolate()),
- flags_(LanguageModeField::encode(CLASSIC_MODE) |
- IsLazy::encode(true)),
- function_(NULL),
- scope_(NULL),
- global_scope_(NULL),
+CompilationInfo::CompilationInfo(Handle<JSFunction> closure, Zone* zone)
+ : flags_(kLazy | kThisHasUses),
closure_(closure),
shared_info_(Handle<SharedFunctionInfo>(closure->shared())),
script_(Handle<Script>(Script::cast(shared_info_->script()))),
- extension_(NULL),
- pre_parse_data_(NULL),
- osr_ast_id_(AstNode::kNoNumber) {
- Initialize(BASE);
+ source_stream_(NULL),
+ context_(closure->context()),
+ osr_ast_id_(BailoutId::None()),
+ parameter_count_(0),
+ optimization_id_(-1),
+ ast_value_factory_(NULL),
+ ast_value_factory_owned_(false),
+ aborted_due_to_dependency_change_(false) {
+ Initialize(script_->GetIsolate(), BASE, zone);
}
-// Disable optimization for the rest of the compilation pipeline.
-void CompilationInfo::DisableOptimization() {
- bool is_optimizable_closure =
- FLAG_optimize_closures &&
- closure_.is_null() &&
- !scope_->HasTrivialOuterContext() &&
- !scope_->outer_scope_calls_non_strict_eval() &&
- !scope_->inside_with();
- SetMode(is_optimizable_closure ? BASE : NONOPT);
+CompilationInfo::CompilationInfo(HydrogenCodeStub* stub, Isolate* isolate,
+ Zone* zone)
+ : flags_(kLazy | kThisHasUses),
+ source_stream_(NULL),
+ osr_ast_id_(BailoutId::None()),
+ parameter_count_(0),
+ optimization_id_(-1),
+ ast_value_factory_(NULL),
+ ast_value_factory_owned_(false),
+ aborted_due_to_dependency_change_(false) {
+ Initialize(isolate, STUB, zone);
+ code_stub_ = stub;
+}
+
+
+CompilationInfo::CompilationInfo(
+ ScriptCompiler::ExternalSourceStream* stream,
+ ScriptCompiler::StreamedSource::Encoding encoding, Isolate* isolate,
+ Zone* zone)
+ : flags_(kThisHasUses),
+ source_stream_(stream),
+ source_stream_encoding_(encoding),
+ osr_ast_id_(BailoutId::None()),
+ parameter_count_(0),
+ optimization_id_(-1),
+ ast_value_factory_(NULL),
+ ast_value_factory_owned_(false),
+ aborted_due_to_dependency_change_(false) {
+ Initialize(isolate, BASE, zone);
+}
+
+
+void CompilationInfo::Initialize(Isolate* isolate,
+ Mode mode,
+ Zone* zone) {
+ isolate_ = isolate;
+ function_ = NULL;
+ scope_ = NULL;
+ global_scope_ = NULL;
+ extension_ = NULL;
+ cached_data_ = NULL;
+ compile_options_ = ScriptCompiler::kNoCompileOptions;
+ zone_ = zone;
+ deferred_handles_ = NULL;
+ code_stub_ = NULL;
+ prologue_offset_ = Code::kPrologueOffsetNotSet;
+ opt_count_ = shared_info().is_null() ? 0 : shared_info()->opt_count();
+ no_frame_ranges_ = isolate->cpu_profiler()->is_profiling()
+ ? new List<OffsetRange>(2) : NULL;
+ for (int i = 0; i < DependentCode::kGroupCount; i++) {
+ dependencies_[i] = NULL;
+ }
+ if (mode == STUB) {
+ mode_ = STUB;
+ return;
+ }
+ mode_ = mode;
+ if (!script_.is_null() && script_->type()->value() == Script::TYPE_NATIVE) {
+ MarkAsNative();
+ }
+ if (isolate_->debug()->is_active()) MarkAsDebug();
+ if (FLAG_context_specialization) MarkAsContextSpecializing();
+ if (FLAG_turbo_inlining) MarkAsInliningEnabled();
+ if (FLAG_turbo_types) MarkAsTypingEnabled();
+
+ if (!shared_info_.is_null()) {
+ DCHECK(strict_mode() == SLOPPY);
+ SetStrictMode(shared_info_->strict_mode());
+ }
+ bailout_reason_ = kUnknown;
+
+ if (!shared_info().is_null() && shared_info()->is_compiled()) {
+ // We should initialize the CompilationInfo feedback vector from the
+ // passed in shared info, rather than creating a new one.
+ feedback_vector_ =
+ Handle<TypeFeedbackVector>(shared_info()->feedback_vector(), isolate);
+ }
+}
+
+
+CompilationInfo::~CompilationInfo() {
+ if (GetFlag(kDisableFutureOptimization)) {
+ shared_info()->DisableOptimization(bailout_reason());
+ }
+ delete deferred_handles_;
+ delete no_frame_ranges_;
+ if (ast_value_factory_owned_) delete ast_value_factory_;
+#ifdef DEBUG
+ // Check that no dependent maps have been added or added dependent maps have
+ // been rolled back or committed.
+ for (int i = 0; i < DependentCode::kGroupCount; i++) {
+ DCHECK_EQ(NULL, dependencies_[i]);
+ }
+#endif // DEBUG
+}
+
+
+void CompilationInfo::CommitDependencies(Handle<Code> code) {
+ for (int i = 0; i < DependentCode::kGroupCount; i++) {
+ ZoneList<Handle<HeapObject> >* group_objects = dependencies_[i];
+ if (group_objects == NULL) continue;
+ DCHECK(!object_wrapper_.is_null());
+ for (int j = 0; j < group_objects->length(); j++) {
+ DependentCode::DependencyGroup group =
+ static_cast<DependentCode::DependencyGroup>(i);
+ DependentCode* dependent_code =
+ DependentCode::ForObject(group_objects->at(j), group);
+ dependent_code->UpdateToFinishedCode(group, this, *code);
+ }
+ dependencies_[i] = NULL; // Zone-allocated, no need to delete.
+ }
+}
+
+
+void CompilationInfo::RollbackDependencies() {
+ // Unregister from all dependent maps if not yet committed.
+ for (int i = 0; i < DependentCode::kGroupCount; i++) {
+ ZoneList<Handle<HeapObject> >* group_objects = dependencies_[i];
+ if (group_objects == NULL) continue;
+ for (int j = 0; j < group_objects->length(); j++) {
+ DependentCode::DependencyGroup group =
+ static_cast<DependentCode::DependencyGroup>(i);
+ DependentCode* dependent_code =
+ DependentCode::ForObject(group_objects->at(j), group);
+ dependent_code->RemoveCompilationInfo(group, this);
+ }
+ dependencies_[i] = NULL; // Zone-allocated, no need to delete.
+ }
+}
+
+
+int CompilationInfo::num_parameters() const {
+ if (IsStub()) {
+ DCHECK(parameter_count_ > 0);
+ return parameter_count_;
+ } else {
+ return scope()->num_parameters();
+ }
+}
+
+
+int CompilationInfo::num_heap_slots() const {
+ if (IsStub()) {
+ return 0;
+ } else {
+ return scope()->num_heap_slots();
+ }
+}
+
+
+Code::Flags CompilationInfo::flags() const {
+ if (IsStub()) {
+ return Code::ComputeFlags(code_stub()->GetCodeKind(),
+ code_stub()->GetICState(),
+ code_stub()->GetExtraICState(),
+ code_stub()->GetStubType());
+ } else {
+ return Code::ComputeFlags(Code::OPTIMIZED_FUNCTION);
+ }
}
@@ -114,112 +271,83 @@
// profiler, so they trigger their own optimization when they're called
// for the SharedFunctionInfo::kCallsUntilPrimitiveOptimization-th time.
bool CompilationInfo::ShouldSelfOptimize() {
- return FLAG_self_optimization &&
- FLAG_crankshaft &&
+ return FLAG_crankshaft &&
!function()->flags()->Contains(kDontSelfOptimize) &&
- !function()->flags()->Contains(kDontOptimize) &&
- function()->scope()->AllowsLazyRecompilation() &&
+ !function()->dont_optimize() &&
+ function()->scope()->AllowsLazyCompilation() &&
(shared_info().is_null() || !shared_info()->optimization_disabled());
}
-void CompilationInfo::AbortOptimization() {
- Handle<Code> code(shared_info()->code());
- SetCode(code);
+void CompilationInfo::PrepareForCompilation(Scope* scope) {
+ DCHECK(scope_ == NULL);
+ scope_ = scope;
+
+ int length = function()->slot_count();
+ if (feedback_vector_.is_null()) {
+ // Allocate the feedback vector too.
+ feedback_vector_ = isolate()->factory()->NewTypeFeedbackVector(length);
+ }
+ DCHECK(feedback_vector_->length() == length);
}
-// Determine whether to use the full compiler for all code. If the flag
-// --always-full-compiler is specified this is the case. For the virtual frame
-// based compiler the full compiler is also used if a debugger is connected, as
-// the code from the full compiler supports mode precise break points. For the
-// crankshaft adaptive compiler debugging the optimized code is not possible at
-// all. However crankshaft support recompilation of functions, so in this case
-// the full compiler need not be be used if a debugger is attached, but only if
-// break points has actually been set.
-static bool is_debugging_active() {
-#ifdef ENABLE_DEBUGGER_SUPPORT
- Isolate* isolate = Isolate::Current();
- return V8::UseCrankshaft() ?
- isolate->debug()->has_break_points() :
- isolate->debugger()->IsDebuggerActive();
-#else
- return false;
-#endif
-}
-
-
-static bool AlwaysFullCompiler() {
- return FLAG_always_full_compiler || is_debugging_active();
-}
-
-
-static void FinishOptimization(Handle<JSFunction> function, int64_t start) {
- int opt_count = function->shared()->opt_count();
- function->shared()->set_opt_count(opt_count + 1);
- double ms = static_cast<double>(OS::Ticks() - start) / 1000;
- if (FLAG_trace_opt) {
- PrintF("[optimizing: ");
- function->PrintName();
- PrintF(" / %" V8PRIxPTR, reinterpret_cast<intptr_t>(*function));
- PrintF(" - took %0.3f ms]\n", ms);
- }
- if (FLAG_trace_opt_stats) {
- static double compilation_time = 0.0;
- static int compiled_functions = 0;
- static int code_size = 0;
-
- compilation_time += ms;
- compiled_functions++;
- code_size += function->shared()->SourceSize();
- PrintF("Compiled: %d functions with %d byte source size in %fms.\n",
- compiled_functions,
- code_size,
- compilation_time);
- }
-}
-
-
-static bool MakeCrankshaftCode(CompilationInfo* info) {
- // Test if we can optimize this function when asked to. We can only
- // do this after the scopes are computed.
- if (!V8::UseCrankshaft()) {
- info->DisableOptimization();
+class HOptimizedGraphBuilderWithPositions: public HOptimizedGraphBuilder {
+ public:
+ explicit HOptimizedGraphBuilderWithPositions(CompilationInfo* info)
+ : HOptimizedGraphBuilder(info) {
}
- // In case we are not optimizing simply return the code from
- // the full code generator.
- if (!info->IsOptimizing()) {
- return FullCodeGenerator::MakeCode(info);
+#define DEF_VISIT(type) \
+ virtual void Visit##type(type* node) OVERRIDE { \
+ if (node->position() != RelocInfo::kNoPosition) { \
+ SetSourcePosition(node->position()); \
+ } \
+ HOptimizedGraphBuilder::Visit##type(node); \
}
+ EXPRESSION_NODE_LIST(DEF_VISIT)
+#undef DEF_VISIT
- // We should never arrive here if there is not code object on the
- // shared function object.
- Handle<Code> code(info->shared_info()->code());
- ASSERT(code->kind() == Code::FUNCTION);
+#define DEF_VISIT(type) \
+ virtual void Visit##type(type* node) OVERRIDE { \
+ if (node->position() != RelocInfo::kNoPosition) { \
+ SetSourcePosition(node->position()); \
+ } \
+ HOptimizedGraphBuilder::Visit##type(node); \
+ }
+ STATEMENT_NODE_LIST(DEF_VISIT)
+#undef DEF_VISIT
+
+#define DEF_VISIT(type) \
+ virtual void Visit##type(type* node) OVERRIDE { \
+ HOptimizedGraphBuilder::Visit##type(node); \
+ }
+ MODULE_NODE_LIST(DEF_VISIT)
+ DECLARATION_NODE_LIST(DEF_VISIT)
+#undef DEF_VISIT
+};
+
+
+OptimizedCompileJob::Status OptimizedCompileJob::CreateGraph() {
+ DCHECK(isolate()->use_crankshaft());
+ DCHECK(info()->IsOptimizing());
+ DCHECK(!info()->IsCompilingForDebugging());
// We should never arrive here if optimization has been disabled on the
// shared function info.
- ASSERT(!info->shared_info()->optimization_disabled());
+ DCHECK(!info()->shared_info()->optimization_disabled());
- // Fall back to using the full code generator if it's not possible
- // to use the Hydrogen-based optimizing compiler. We already have
- // generated code for this from the shared function object.
- if (AlwaysFullCompiler()) {
- info->SetCode(code);
- return true;
+ // Do not use crankshaft if we need to be able to set break points.
+ if (isolate()->DebuggerHasBreakPoints()) {
+ return RetryOptimization(kDebuggerHasBreakPoints);
}
// Limit the number of times we re-compile a functions with
// the optimizing compiler.
const int kMaxOptCount =
- FLAG_deopt_every_n_times == 0 ? Compiler::kDefaultMaxOptCount : 1000;
- if (info->shared_info()->opt_count() > kMaxOptCount) {
- info->AbortOptimization();
- info->shared_info()->DisableOptimization();
- // True indicates the compilation pipeline is still going, not
- // necessarily that we optimized the code.
- return true;
+ FLAG_deopt_every_n_times == 0 ? FLAG_max_opt_count : 1000;
+ if (info()->opt_count() > kMaxOptCount) {
+ return AbortOptimization(kOptimizedTooManyTimes);
}
// Due to an encoding limit on LUnallocated operands in the Lithium
@@ -229,526 +357,215 @@
//
// The encoding is as a signed value, with parameters and receiver using
// the negative indices and locals the non-negative ones.
- const int parameter_limit = -LUnallocated::kMinFixedIndex;
- const int locals_limit = LUnallocated::kMaxFixedIndex;
- Scope* scope = info->scope();
- if ((scope->num_parameters() + 1) > parameter_limit ||
- (info->osr_ast_id() != AstNode::kNoNumber &&
- scope->num_parameters() + 1 + scope->num_stack_slots() > locals_limit)) {
- info->AbortOptimization();
- info->shared_info()->DisableOptimization();
- // True indicates the compilation pipeline is still going, not
- // necessarily that we optimized the code.
- return true;
+ const int parameter_limit = -LUnallocated::kMinFixedSlotIndex;
+ Scope* scope = info()->scope();
+ if ((scope->num_parameters() + 1) > parameter_limit) {
+ return AbortOptimization(kTooManyParameters);
}
- // Take --hydrogen-filter into account.
- Handle<String> name = info->function()->debug_name();
- if (*FLAG_hydrogen_filter != '\0') {
- Vector<const char> filter = CStrVector(FLAG_hydrogen_filter);
- if ((filter[0] == '-'
- && name->IsEqualTo(filter.SubVector(1, filter.length())))
- || (filter[0] != '-' && !name->IsEqualTo(filter))) {
- info->SetCode(code);
- return true;
- }
+ const int locals_limit = LUnallocated::kMaxFixedSlotIndex;
+ if (info()->is_osr() &&
+ scope->num_parameters() + 1 + scope->num_stack_slots() > locals_limit) {
+ return AbortOptimization(kTooManyParametersLocals);
}
+ if (scope->HasIllegalRedeclaration()) {
+ return AbortOptimization(kFunctionWithIllegalRedeclaration);
+ }
+
+ // Check the whitelist for Crankshaft.
+ if (!info()->closure()->PassesFilter(FLAG_hydrogen_filter)) {
+ return AbortOptimization(kHydrogenFilter);
+ }
+
+ // Crankshaft requires a version of fullcode with deoptimization support.
// Recompile the unoptimized version of the code if the current version
- // doesn't have deoptimization support. Alternatively, we may decide to
- // run the full code generator to get a baseline for the compile-time
- // performance of the hydrogen-based compiler.
- int64_t start = OS::Ticks();
- bool should_recompile = !info->shared_info()->has_deoptimization_support();
+ // doesn't have deoptimization support already.
+ // Otherwise, if we are gathering compilation time and space statistics
+ // for hydrogen, gather baseline statistics for a fullcode compilation.
+ bool should_recompile = !info()->shared_info()->has_deoptimization_support();
if (should_recompile || FLAG_hydrogen_stats) {
- HPhase phase(HPhase::kFullCodeGen);
- CompilationInfo unoptimized(info->shared_info());
- // Note that we use the same AST that we will use for generating the
- // optimized code.
- unoptimized.SetFunction(info->function());
- unoptimized.SetScope(info->scope());
- if (should_recompile) unoptimized.EnableDeoptimizationSupport();
- bool succeeded = FullCodeGenerator::MakeCode(&unoptimized);
- if (should_recompile) {
- if (!succeeded) return false;
- Handle<SharedFunctionInfo> shared = info->shared_info();
- shared->EnableDeoptimizationSupport(*unoptimized.code());
- // The existing unoptimized code was replaced with the new one.
- Compiler::RecordFunctionCompilation(
- Logger::LAZY_COMPILE_TAG, &unoptimized, shared);
+ base::ElapsedTimer timer;
+ if (FLAG_hydrogen_stats) {
+ timer.Start();
+ }
+ if (!Compiler::EnsureDeoptimizationSupport(info())) {
+ return SetLastStatus(FAILED);
+ }
+ if (FLAG_hydrogen_stats) {
+ isolate()->GetHStatistics()->IncrementFullCodeGen(timer.Elapsed());
}
}
- // Check that the unoptimized, shared code is ready for
- // optimizations. When using the always_opt flag we disregard the
- // optimizable marker in the code object and optimize anyway. This
- // is safe as long as the unoptimized code has deoptimization
- // support.
- ASSERT(FLAG_always_opt || code->optimizable());
- ASSERT(info->shared_info()->has_deoptimization_support());
+ DCHECK(info()->shared_info()->has_deoptimization_support());
+
+ // Check the whitelist for TurboFan.
+ if ((FLAG_turbo_asm && info()->shared_info()->asm_function()) ||
+ info()->closure()->PassesFilter(FLAG_turbo_filter)) {
+ compiler::Pipeline pipeline(info());
+ pipeline.GenerateCode();
+ if (!info()->code().is_null()) {
+ if (FLAG_turbo_deoptimization) {
+ info()->context()->native_context()->AddOptimizedCode(*info()->code());
+ }
+ return SetLastStatus(SUCCEEDED);
+ }
+ }
if (FLAG_trace_hydrogen) {
+ Handle<String> name = info()->function()->debug_name();
PrintF("-----------------------------------------------------------\n");
- PrintF("Compiling method %s using hydrogen\n", *name->ToCString());
- HTracer::Instance()->TraceCompilation(info->function());
+ PrintF("Compiling method %s using hydrogen\n", name->ToCString().get());
+ isolate()->GetHTracer()->TraceCompilation(info());
}
- Handle<Context> global_context(info->closure()->context()->global_context());
- TypeFeedbackOracle oracle(code, global_context, info->isolate());
- HGraphBuilder builder(info, &oracle);
- HPhase phase(HPhase::kTotal);
- HGraph* graph = builder.CreateGraph();
- if (info->isolate()->has_pending_exception()) {
- info->SetCode(Handle<Code>::null());
- return false;
+ // Type-check the function.
+ AstTyper::Run(info());
+
+ graph_builder_ = (FLAG_hydrogen_track_positions || FLAG_trace_ic)
+ ? new(info()->zone()) HOptimizedGraphBuilderWithPositions(info())
+ : new(info()->zone()) HOptimizedGraphBuilder(info());
+
+ Timer t(this, &time_taken_to_create_graph_);
+ info()->set_this_has_uses(false);
+ graph_ = graph_builder_->CreateGraph();
+
+ if (isolate()->has_pending_exception()) {
+ return SetLastStatus(FAILED);
}
- if (graph != NULL) {
- Handle<Code> optimized_code = graph->Compile(info);
- if (!optimized_code.is_null()) {
- info->SetCode(optimized_code);
- FinishOptimization(info->closure(), start);
- return true;
- }
+ if (graph_ == NULL) return SetLastStatus(BAILED_OUT);
+
+ if (info()->HasAbortedDueToDependencyChange()) {
+ // Dependency has changed during graph creation. Let's try again later.
+ return RetryOptimization(kBailedOutDueToDependencyChange);
}
- // Keep using the shared code.
- info->AbortOptimization();
- if (!builder.inline_bailout()) {
- // Mark the shared code as unoptimizable unless it was an inlined
- // function that bailed out.
- info->shared_info()->DisableOptimization();
- }
- // True indicates the compilation pipeline is still going, not necessarily
- // that we optimized the code.
- return true;
+ return SetLastStatus(SUCCEEDED);
}
-static bool GenerateCode(CompilationInfo* info) {
- return info->IsCompilingForDebugging() || !V8::UseCrankshaft() ?
- FullCodeGenerator::MakeCode(info) :
- MakeCrankshaftCode(info);
-}
+OptimizedCompileJob::Status OptimizedCompileJob::OptimizeGraph() {
+ DisallowHeapAllocation no_allocation;
+ DisallowHandleAllocation no_handles;
+ DisallowHandleDereference no_deref;
+ DisallowCodeDependencyChange no_dependency_change;
-
-static bool MakeCode(CompilationInfo* info) {
- // Precondition: code has been parsed. Postcondition: the code field in
- // the compilation info is set if compilation succeeded.
- ASSERT(info->function() != NULL);
- return Rewriter::Rewrite(info) && Scope::Analyze(info) && GenerateCode(info);
-}
-
-
-#ifdef ENABLE_DEBUGGER_SUPPORT
-bool Compiler::MakeCodeForLiveEdit(CompilationInfo* info) {
- // Precondition: code has been parsed. Postcondition: the code field in
- // the compilation info is set if compilation succeeded.
- bool succeeded = MakeCode(info);
- if (!info->shared_info().is_null()) {
- Handle<ScopeInfo> scope_info = ScopeInfo::Create(info->scope());
- info->shared_info()->set_scope_info(*scope_info);
+ DCHECK(last_status() == SUCCEEDED);
+ // TODO(turbofan): Currently everything is done in the first phase.
+ if (!info()->code().is_null()) {
+ return last_status();
}
- return succeeded;
+
+ Timer t(this, &time_taken_to_optimize_);
+ DCHECK(graph_ != NULL);
+ BailoutReason bailout_reason = kNoReason;
+
+ if (graph_->Optimize(&bailout_reason)) {
+ chunk_ = LChunk::NewChunk(graph_);
+ if (chunk_ != NULL) return SetLastStatus(SUCCEEDED);
+ } else if (bailout_reason != kNoReason) {
+ graph_builder_->Bailout(bailout_reason);
+ }
+
+ return SetLastStatus(BAILED_OUT);
}
-#endif
-static Handle<SharedFunctionInfo> MakeFunctionInfo(CompilationInfo* info) {
- Isolate* isolate = info->isolate();
- ZoneScope zone_scope(isolate, DELETE_ON_EXIT);
- PostponeInterruptsScope postpone(isolate);
+OptimizedCompileJob::Status OptimizedCompileJob::GenerateCode() {
+ DCHECK(last_status() == SUCCEEDED);
+ // TODO(turbofan): Currently everything is done in the first phase.
+ if (!info()->code().is_null()) {
+ RecordOptimizationStats();
+ return last_status();
+ }
- ASSERT(!isolate->global_context().is_null());
- Handle<Script> script = info->script();
- script->set_context_data((*isolate->global_context())->data());
-
-#ifdef ENABLE_DEBUGGER_SUPPORT
- if (info->is_eval()) {
- Script::CompilationType compilation_type = Script::COMPILATION_TYPE_EVAL;
- script->set_compilation_type(Smi::FromInt(compilation_type));
- // For eval scripts add information on the function from which eval was
- // called.
- if (info->is_eval()) {
- StackTraceFrameIterator it(isolate);
- if (!it.done()) {
- script->set_eval_from_shared(
- JSFunction::cast(it.frame()->function())->shared());
- Code* code = it.frame()->LookupCode();
- int offset = static_cast<int>(
- it.frame()->pc() - code->instruction_start());
- script->set_eval_from_instructions_offset(Smi::FromInt(offset));
+ DCHECK(!info()->HasAbortedDueToDependencyChange());
+ DisallowCodeDependencyChange no_dependency_change;
+ DisallowJavascriptExecution no_js(isolate());
+ { // Scope for timer.
+ Timer timer(this, &time_taken_to_codegen_);
+ DCHECK(chunk_ != NULL);
+ DCHECK(graph_ != NULL);
+ // Deferred handles reference objects that were accessible during
+ // graph creation. To make sure that we don't encounter inconsistencies
+ // between graph creation and code generation, we disallow accessing
+ // objects through deferred handles during the latter, with exceptions.
+ DisallowDeferredHandleDereference no_deferred_handle_deref;
+ Handle<Code> optimized_code = chunk_->Codegen();
+ if (optimized_code.is_null()) {
+ if (info()->bailout_reason() == kNoReason) {
+ return AbortOptimization(kCodeGenerationFailed);
}
+ return SetLastStatus(BAILED_OUT);
}
+ info()->SetCode(optimized_code);
}
-
- // Notify debugger
- isolate->debugger()->OnBeforeCompile(script);
-#endif
-
- // Only allow non-global compiles for eval.
- ASSERT(info->is_eval() || info->is_global());
- ParsingFlags flags = kNoParsingFlags;
- if (info->pre_parse_data() != NULL ||
- String::cast(script->source())->length() > FLAG_min_preparse_length) {
- flags = kAllowLazy;
- }
- if (!ParserApi::Parse(info, flags)) {
- return Handle<SharedFunctionInfo>::null();
- }
-
- // 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.
- HistogramTimer* rate = info->is_eval()
- ? info->isolate()->counters()->compile_eval()
- : info->isolate()->counters()->compile();
- HistogramTimerScope timer(rate);
-
- // Compile the code.
- FunctionLiteral* lit = info->function();
- LiveEditFunctionTracker live_edit_tracker(isolate, lit);
- if (!MakeCode(info)) {
- if (!isolate->has_pending_exception()) isolate->StackOverflow();
- return Handle<SharedFunctionInfo>::null();
- }
-
- // Allocate function.
- ASSERT(!info->code().is_null());
- Handle<SharedFunctionInfo> result =
- isolate->factory()->NewSharedFunctionInfo(
- lit->name(),
- lit->materialized_literal_count(),
- info->code(),
- ScopeInfo::Create(info->scope()));
-
- ASSERT_EQ(RelocInfo::kNoPosition, lit->function_token_position());
- Compiler::SetFunctionInfo(result, lit, true, script);
-
- if (script->name()->IsString()) {
- PROFILE(isolate, CodeCreateEvent(
- info->is_eval()
- ? Logger::EVAL_TAG
- : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script),
- *info->code(),
- *result,
- String::cast(script->name())));
- GDBJIT(AddCode(Handle<String>(String::cast(script->name())),
- script,
- info->code(),
- info));
- } else {
- PROFILE(isolate, CodeCreateEvent(
- info->is_eval()
- ? Logger::EVAL_TAG
- : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script),
- *info->code(),
- *result,
- isolate->heap()->empty_string()));
- GDBJIT(AddCode(Handle<String>(), script, info->code(), info));
- }
-
- // Hint to the runtime system used when allocating space for initial
- // property space by setting the expected number of properties for
- // the instances of the function.
- SetExpectedNofPropertiesFromEstimate(result, lit->expected_property_count());
-
- script->set_compilation_state(
- Smi::FromInt(Script::COMPILATION_STATE_COMPILED));
-
-#ifdef ENABLE_DEBUGGER_SUPPORT
- // Notify debugger
- isolate->debugger()->OnAfterCompile(
- script, Debugger::NO_AFTER_COMPILE_FLAGS);
-#endif
-
- live_edit_tracker.RecordFunctionInfo(result, lit);
-
- return result;
+ RecordOptimizationStats();
+ // Add to the weak list of optimized code objects.
+ info()->context()->native_context()->AddOptimizedCode(*info()->code());
+ return SetLastStatus(SUCCEEDED);
}
-Handle<SharedFunctionInfo> Compiler::Compile(Handle<String> source,
- Handle<Object> script_name,
- int line_offset,
- int column_offset,
- v8::Extension* extension,
- ScriptDataImpl* pre_data,
- Handle<Object> script_data,
- NativesFlag natives) {
- Isolate* isolate = source->GetIsolate();
- int source_length = source->length();
- isolate->counters()->total_load_size()->Increment(source_length);
- isolate->counters()->total_compile_size()->Increment(source_length);
-
- // The VM is in the COMPILER state until exiting this function.
- VMState state(isolate, COMPILER);
-
- CompilationCache* compilation_cache = isolate->compilation_cache();
-
- // Do a lookup in the compilation cache but not for extensions.
- Handle<SharedFunctionInfo> result;
- if (extension == NULL) {
- result = compilation_cache->LookupScript(source,
- script_name,
- line_offset,
- column_offset);
+void OptimizedCompileJob::RecordOptimizationStats() {
+ Handle<JSFunction> function = info()->closure();
+ if (!function->IsOptimized()) {
+ // Concurrent recompilation and OSR may race. Increment only once.
+ int opt_count = function->shared()->opt_count();
+ function->shared()->set_opt_count(opt_count + 1);
}
-
- if (result.is_null()) {
- // No cache entry found. Do pre-parsing, if it makes sense, and compile
- // the script.
- // Building preparse data that is only used immediately after is only a
- // saving if we might skip building the AST for lazily compiled functions.
- // I.e., preparse data isn't relevant when the lazy flag is off, and
- // for small sources, odds are that there aren't many functions
- // that would be compiled lazily anyway, so we skip the preparse step
- // in that case too.
-
- // Create a script object describing the script to be compiled.
- Handle<Script> script = FACTORY->NewScript(source);
- if (natives == NATIVES_CODE) {
- script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
- }
- if (!script_name.is_null()) {
- script->set_name(*script_name);
- script->set_line_offset(Smi::FromInt(line_offset));
- script->set_column_offset(Smi::FromInt(column_offset));
- }
-
- script->set_data(script_data.is_null() ? HEAP->undefined_value()
- : *script_data);
-
- // Compile the function and add it to the cache.
- CompilationInfo info(script);
- info.MarkAsGlobal();
- info.SetExtension(extension);
- info.SetPreParseData(pre_data);
- if (FLAG_use_strict) {
- info.SetLanguageMode(FLAG_harmony_scoping ? EXTENDED_MODE : STRICT_MODE);
- }
- result = MakeFunctionInfo(&info);
- if (extension == NULL && !result.is_null()) {
- compilation_cache->PutScript(source, result);
- }
- } else {
- if (result->ic_age() != HEAP->global_ic_age()) {
- result->ResetForNewContext(HEAP->global_ic_age());
- }
+ double ms_creategraph = time_taken_to_create_graph_.InMillisecondsF();
+ double ms_optimize = time_taken_to_optimize_.InMillisecondsF();
+ double ms_codegen = time_taken_to_codegen_.InMillisecondsF();
+ if (FLAG_trace_opt) {
+ PrintF("[optimizing ");
+ function->ShortPrint();
+ PrintF(" - took %0.3f, %0.3f, %0.3f ms]\n", ms_creategraph, ms_optimize,
+ ms_codegen);
}
+ if (FLAG_trace_opt_stats) {
+ static double compilation_time = 0.0;
+ static int compiled_functions = 0;
+ static int code_size = 0;
- if (result.is_null()) isolate->ReportPendingMessages();
- return result;
+ compilation_time += (ms_creategraph + ms_optimize + ms_codegen);
+ compiled_functions++;
+ code_size += function->shared()->SourceSize();
+ PrintF("Compiled: %d functions with %d byte source size in %fms.\n",
+ compiled_functions,
+ code_size,
+ compilation_time);
+ }
+ if (FLAG_hydrogen_stats) {
+ isolate()->GetHStatistics()->IncrementSubtotals(time_taken_to_create_graph_,
+ time_taken_to_optimize_,
+ time_taken_to_codegen_);
+ }
}
-Handle<SharedFunctionInfo> Compiler::CompileEval(Handle<String> source,
- Handle<Context> context,
- bool is_global,
- LanguageMode language_mode,
- int scope_position) {
- Isolate* isolate = source->GetIsolate();
- int source_length = source->length();
- isolate->counters()->total_eval_size()->Increment(source_length);
- isolate->counters()->total_compile_size()->Increment(source_length);
+// Sets the expected number of properties based on estimate from compiler.
+void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared,
+ int estimate) {
+ // If no properties are added in the constructor, they are more likely
+ // to be added later.
+ if (estimate == 0) estimate = 2;
- // The VM is in the COMPILER state until exiting this function.
- VMState state(isolate, COMPILER);
-
- // Do a lookup in the compilation cache; if the entry is not there, invoke
- // the compiler and add the result to the cache.
- Handle<SharedFunctionInfo> result;
- CompilationCache* compilation_cache = isolate->compilation_cache();
- result = compilation_cache->LookupEval(source,
- context,
- is_global,
- language_mode,
- scope_position);
-
- if (result.is_null()) {
- // Create a script object describing the script to be compiled.
- Handle<Script> script = isolate->factory()->NewScript(source);
- CompilationInfo info(script);
- info.MarkAsEval();
- if (is_global) info.MarkAsGlobal();
- info.SetLanguageMode(language_mode);
- info.SetCallingContext(context);
- result = MakeFunctionInfo(&info);
- if (!result.is_null()) {
- // Explicitly disable optimization for eval code. We're not yet prepared
- // to handle eval-code in the optimizing compiler.
- result->DisableOptimization();
-
- // If caller is strict mode, the result must be in strict mode or
- // extended mode as well, but not the other way around. Consider:
- // eval("'use strict'; ...");
- ASSERT(language_mode != STRICT_MODE || !result->is_classic_mode());
- // If caller is in extended mode, the result must also be in
- // extended mode.
- ASSERT(language_mode != EXTENDED_MODE ||
- result->is_extended_mode());
- compilation_cache->PutEval(
- source, context, is_global, result, scope_position);
- }
+ // TODO(yangguo): check whether those heuristics are still up-to-date.
+ // We do not shrink objects that go into a snapshot (yet), so we adjust
+ // the estimate conservatively.
+ if (shared->GetIsolate()->serializer_enabled()) {
+ estimate += 2;
+ } else if (FLAG_clever_optimizations) {
+ // Inobject slack tracking will reclaim redundant inobject space later,
+ // so we can afford to adjust the estimate generously.
+ estimate += 8;
} else {
- if (result->ic_age() != HEAP->global_ic_age()) {
- result->ResetForNewContext(HEAP->global_ic_age());
- }
+ estimate += 3;
}
- return result;
-}
-
-
-bool Compiler::CompileLazy(CompilationInfo* info) {
- Isolate* isolate = info->isolate();
-
- ZoneScope zone_scope(isolate, DELETE_ON_EXIT);
-
- // The VM is in the COMPILER state until exiting this function.
- VMState state(isolate, COMPILER);
-
- PostponeInterruptsScope postpone(isolate);
-
- Handle<SharedFunctionInfo> shared = info->shared_info();
- int compiled_size = shared->end_position() - shared->start_position();
- isolate->counters()->total_compile_size()->Increment(compiled_size);
-
- // Generate the AST for the lazily compiled function.
- if (ParserApi::Parse(info, kNoParsingFlags)) {
- // Measure how long it takes to do the lazy compilation; only take the
- // rest of the function into account to avoid overlap with the lazy
- // parsing statistics.
- HistogramTimerScope timer(isolate->counters()->compile_lazy());
-
- // After parsing we know the function's language mode. Remember it.
- LanguageMode language_mode = info->function()->language_mode();
- info->SetLanguageMode(language_mode);
- shared->set_language_mode(language_mode);
-
- // Compile the code.
- if (!MakeCode(info)) {
- if (!isolate->has_pending_exception()) {
- isolate->StackOverflow();
- }
- } else {
- ASSERT(!info->code().is_null());
- Handle<Code> code = info->code();
- // Set optimizable to false if this is disallowed by the shared
- // function info, e.g., we might have flushed the code and must
- // reset this bit when lazy compiling the code again.
- if (shared->optimization_disabled()) code->set_optimizable(false);
-
- Handle<JSFunction> function = info->closure();
- RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, info, shared);
-
- if (info->IsOptimizing()) {
- ASSERT(shared->scope_info() != ScopeInfo::Empty());
- function->ReplaceCode(*code);
- } else {
- // Update the shared function info with the compiled code and the
- // scope info. Please note, that the order of the shared function
- // info initialization is important since set_scope_info might
- // trigger a GC, causing the ASSERT below to be invalid if the code
- // was flushed. By setting the code object last we avoid this.
- Handle<ScopeInfo> scope_info = ScopeInfo::Create(info->scope());
- shared->set_scope_info(*scope_info);
- shared->set_code(*code);
- if (!function.is_null()) {
- function->ReplaceCode(*code);
- ASSERT(!function->IsOptimized());
- }
-
- // Set the expected number of properties for instances.
- FunctionLiteral* lit = info->function();
- int expected = lit->expected_property_count();
- SetExpectedNofPropertiesFromEstimate(shared, expected);
-
- // Set the optimization hints after performing lazy compilation, as
- // these are not set when the function is set up as a lazily
- // compiled function.
- shared->SetThisPropertyAssignmentsInfo(
- lit->has_only_simple_this_property_assignments(),
- *lit->this_property_assignments());
-
- // Check the function has compiled code.
- ASSERT(shared->is_compiled());
- shared->set_code_age(0);
- shared->set_dont_optimize(lit->flags()->Contains(kDontOptimize));
- shared->set_dont_inline(lit->flags()->Contains(kDontInline));
- shared->set_ast_node_count(lit->ast_node_count());
-
- if (V8::UseCrankshaft()&&
- !function.is_null() &&
- !shared->optimization_disabled()) {
- // If we're asked to always optimize, we compile the optimized
- // version of the function right away - unless the debugger is
- // active as it makes no sense to compile optimized code then.
- if (FLAG_always_opt &&
- !Isolate::Current()->DebuggerHasBreakPoints()) {
- CompilationInfo optimized(function);
- optimized.SetOptimizing(AstNode::kNoNumber);
- return CompileLazy(&optimized);
- }
- }
- }
-
- return true;
- }
- }
-
- ASSERT(info->code().is_null());
- return false;
-}
-
-
-Handle<SharedFunctionInfo> Compiler::BuildFunctionInfo(FunctionLiteral* literal,
- Handle<Script> script) {
- // Precondition: code has been parsed and scopes have been analyzed.
- CompilationInfo info(script);
- info.SetFunction(literal);
- info.SetScope(literal->scope());
- info.SetLanguageMode(literal->scope()->language_mode());
-
- LiveEditFunctionTracker live_edit_tracker(info.isolate(), literal);
- // Determine if the function can be lazily compiled. This is necessary to
- // allow some of our builtin JS files to be lazily compiled. These
- // builtins cannot be handled lazily by the parser, since we have to know
- // if a function uses the special natives syntax, which is something the
- // parser records.
- bool allow_lazy = literal->AllowsLazyCompilation() &&
- !LiveEditFunctionTracker::IsActive(info.isolate());
-
- Handle<ScopeInfo> scope_info(ScopeInfo::Empty());
-
- // Generate code
- if (FLAG_lazy && allow_lazy) {
- Handle<Code> code = info.isolate()->builtins()->LazyCompile();
- info.SetCode(code);
- } else if ((V8::UseCrankshaft() && MakeCrankshaftCode(&info)) ||
- (!V8::UseCrankshaft() && FullCodeGenerator::MakeCode(&info))) {
- ASSERT(!info.code().is_null());
- scope_info = ScopeInfo::Create(info.scope());
- } else {
- return Handle<SharedFunctionInfo>::null();
- }
-
- // Create a shared function info object.
- Handle<SharedFunctionInfo> result =
- FACTORY->NewSharedFunctionInfo(literal->name(),
- literal->materialized_literal_count(),
- info.code(),
- scope_info);
- SetFunctionInfo(result, literal, false, script);
- RecordFunctionCompilation(Logger::FUNCTION_TAG, &info, result);
- result->set_allows_lazy_compilation(allow_lazy);
-
- // Set the expected number of properties for instances and return
- // the resulting function.
- SetExpectedNofPropertiesFromEstimate(result,
- literal->expected_property_count());
- live_edit_tracker.RecordFunctionInfo(result, literal);
- return result;
+ shared->set_expected_nof_properties(estimate);
}
@@ -756,10 +573,10 @@
// The start_position points to the first '(' character after the function name
// in the full script source. When counting characters in the script source the
// the first character is number 0 (not 1).
-void Compiler::SetFunctionInfo(Handle<SharedFunctionInfo> function_info,
- FunctionLiteral* lit,
- bool is_toplevel,
- Handle<Script> script) {
+static void SetFunctionInfo(Handle<SharedFunctionInfo> function_info,
+ FunctionLiteral* lit,
+ bool is_toplevel,
+ Handle<Script> script) {
function_info->set_length(lit->parameter_count());
function_info->set_formal_parameter_count(lit->parameter_count());
function_info->set_script(*script);
@@ -770,57 +587,851 @@
function_info->set_is_anonymous(lit->is_anonymous());
function_info->set_is_toplevel(is_toplevel);
function_info->set_inferred_name(*lit->inferred_name());
- function_info->SetThisPropertyAssignmentsInfo(
- lit->has_only_simple_this_property_assignments(),
- *lit->this_property_assignments());
function_info->set_allows_lazy_compilation(lit->AllowsLazyCompilation());
- function_info->set_language_mode(lit->language_mode());
+ function_info->set_allows_lazy_compilation_without_context(
+ lit->AllowsLazyCompilationWithoutContext());
+ function_info->set_strict_mode(lit->strict_mode());
function_info->set_uses_arguments(lit->scope()->arguments() != NULL);
function_info->set_has_duplicate_parameters(lit->has_duplicate_parameters());
function_info->set_ast_node_count(lit->ast_node_count());
function_info->set_is_function(lit->is_function());
- function_info->set_dont_optimize(lit->flags()->Contains(kDontOptimize));
- function_info->set_dont_inline(lit->flags()->Contains(kDontInline));
+ function_info->set_bailout_reason(lit->dont_optimize_reason());
+ function_info->set_dont_cache(lit->flags()->Contains(kDontCache));
+ function_info->set_kind(lit->kind());
+ function_info->set_asm_function(lit->scope()->asm_function());
}
-void Compiler::RecordFunctionCompilation(Logger::LogEventsAndTags tag,
- CompilationInfo* info,
- Handle<SharedFunctionInfo> shared) {
+static void RecordFunctionCompilation(Logger::LogEventsAndTags tag,
+ CompilationInfo* info,
+ Handle<SharedFunctionInfo> shared) {
// SharedFunctionInfo is passed separately, because if CompilationInfo
// was created using Script object, it will not have it.
// Log the code generation. If source information is available include
// script name and line number. Check explicitly whether logging is
// enabled as finding the line number is not free.
- if (info->isolate()->logger()->is_logging() ||
- CpuProfiler::is_profiling(info->isolate())) {
+ if (info->isolate()->logger()->is_logging_code_events() ||
+ info->isolate()->cpu_profiler()->is_profiling()) {
Handle<Script> script = info->script();
Handle<Code> code = info->code();
- if (*code == info->isolate()->builtins()->builtin(Builtins::kLazyCompile))
+ if (code.is_identical_to(info->isolate()->builtins()->CompileLazy())) {
return;
- if (script->name()->IsString()) {
- int line_num = GetScriptLineNumber(script, shared->start_position()) + 1;
- USE(line_num);
- PROFILE(info->isolate(),
- CodeCreateEvent(Logger::ToNativeByScript(tag, *script),
- *code,
- *shared,
- String::cast(script->name()),
- line_num));
- } else {
- PROFILE(info->isolate(),
- CodeCreateEvent(Logger::ToNativeByScript(tag, *script),
- *code,
- *shared,
- shared->DebugName()));
}
+ int line_num = Script::GetLineNumber(script, shared->start_position()) + 1;
+ int column_num =
+ Script::GetColumnNumber(script, shared->start_position()) + 1;
+ String* script_name = script->name()->IsString()
+ ? String::cast(script->name())
+ : info->isolate()->heap()->empty_string();
+ Logger::LogEventsAndTags log_tag = Logger::ToNativeByScript(tag, *script);
+ PROFILE(info->isolate(),
+ CodeCreateEvent(log_tag, *code, *shared, info, script_name,
+ line_num, column_num));
}
GDBJIT(AddCode(Handle<String>(shared->DebugName()),
- Handle<Script>(info->script()),
- Handle<Code>(info->code()),
+ Handle<Script>(info->script()), Handle<Code>(info->code()),
info));
}
+
+static bool CompileUnoptimizedCode(CompilationInfo* info) {
+ DCHECK(AllowCompilation::IsAllowed(info->isolate()));
+ DCHECK(info->function() != NULL);
+ if (!Rewriter::Rewrite(info)) return false;
+ if (!Scope::Analyze(info)) return false;
+ DCHECK(info->scope() != NULL);
+
+ if (!FullCodeGenerator::MakeCode(info)) {
+ Isolate* isolate = info->isolate();
+ if (!isolate->has_pending_exception()) isolate->StackOverflow();
+ return false;
+ }
+ return true;
+}
+
+
+MUST_USE_RESULT static MaybeHandle<Code> GetUnoptimizedCodeCommon(
+ CompilationInfo* info) {
+ VMState<COMPILER> state(info->isolate());
+ PostponeInterruptsScope postpone(info->isolate());
+
+ // Parse and update CompilationInfo with the results.
+ if (!Parser::Parse(info)) return MaybeHandle<Code>();
+ Handle<SharedFunctionInfo> shared = info->shared_info();
+ FunctionLiteral* lit = info->function();
+ shared->set_strict_mode(lit->strict_mode());
+ SetExpectedNofPropertiesFromEstimate(shared, lit->expected_property_count());
+ shared->set_bailout_reason(lit->dont_optimize_reason());
+ shared->set_ast_node_count(lit->ast_node_count());
+
+ // Compile unoptimized code.
+ if (!CompileUnoptimizedCode(info)) return MaybeHandle<Code>();
+
+ CHECK_EQ(Code::FUNCTION, info->code()->kind());
+ RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, info, shared);
+
+ // Update the shared function info with the scope info. Allocating the
+ // ScopeInfo object may cause a GC.
+ Handle<ScopeInfo> scope_info = ScopeInfo::Create(info->scope(), info->zone());
+ shared->set_scope_info(*scope_info);
+
+ // Update the code and feedback vector for the shared function info.
+ shared->ReplaceCode(*info->code());
+ if (shared->optimization_disabled()) info->code()->set_optimizable(false);
+ shared->set_feedback_vector(*info->feedback_vector());
+
+ return info->code();
+}
+
+
+MUST_USE_RESULT static MaybeHandle<Code> GetCodeFromOptimizedCodeMap(
+ Handle<JSFunction> function, BailoutId osr_ast_id) {
+ if (FLAG_cache_optimized_code) {
+ Handle<SharedFunctionInfo> shared(function->shared());
+ // Bound functions are not cached.
+ if (shared->bound()) return MaybeHandle<Code>();
+ DisallowHeapAllocation no_gc;
+ int index = shared->SearchOptimizedCodeMap(
+ function->context()->native_context(), osr_ast_id);
+ if (index > 0) {
+ if (FLAG_trace_opt) {
+ PrintF("[found optimized code for ");
+ function->ShortPrint();
+ if (!osr_ast_id.IsNone()) {
+ PrintF(" at OSR AST id %d", osr_ast_id.ToInt());
+ }
+ PrintF("]\n");
+ }
+ FixedArray* literals = shared->GetLiteralsFromOptimizedCodeMap(index);
+ if (literals != NULL) function->set_literals(literals);
+ return Handle<Code>(shared->GetCodeFromOptimizedCodeMap(index));
+ }
+ }
+ return MaybeHandle<Code>();
+}
+
+
+static void InsertCodeIntoOptimizedCodeMap(CompilationInfo* info) {
+ Handle<Code> code = info->code();
+ if (code->kind() != Code::OPTIMIZED_FUNCTION) return; // Nothing to do.
+
+ // Context specialization folds-in the context, so no sharing can occur.
+ if (code->is_turbofanned() && info->is_context_specializing()) return;
+
+ // Cache optimized code.
+ if (FLAG_cache_optimized_code) {
+ Handle<JSFunction> function = info->closure();
+ Handle<SharedFunctionInfo> shared(function->shared());
+ // Do not cache bound functions.
+ if (shared->bound()) return;
+ Handle<FixedArray> literals(function->literals());
+ Handle<Context> native_context(function->context()->native_context());
+ SharedFunctionInfo::AddToOptimizedCodeMap(shared, native_context, code,
+ literals, info->osr_ast_id());
+ }
+}
+
+
+static bool CompileOptimizedPrologue(CompilationInfo* info) {
+ if (!Parser::Parse(info)) return false;
+ if (!Rewriter::Rewrite(info)) return false;
+ if (!Scope::Analyze(info)) return false;
+ DCHECK(info->scope() != NULL);
+ return true;
+}
+
+
+static bool GetOptimizedCodeNow(CompilationInfo* info) {
+ if (!CompileOptimizedPrologue(info)) return false;
+
+ TimerEventScope<TimerEventRecompileSynchronous> timer(info->isolate());
+
+ OptimizedCompileJob job(info);
+ if (job.CreateGraph() != OptimizedCompileJob::SUCCEEDED ||
+ job.OptimizeGraph() != OptimizedCompileJob::SUCCEEDED ||
+ job.GenerateCode() != OptimizedCompileJob::SUCCEEDED) {
+ if (FLAG_trace_opt) {
+ PrintF("[aborted optimizing ");
+ info->closure()->ShortPrint();
+ PrintF(" because: %s]\n", GetBailoutReason(info->bailout_reason()));
+ }
+ return false;
+ }
+
+ // Success!
+ DCHECK(!info->isolate()->has_pending_exception());
+ InsertCodeIntoOptimizedCodeMap(info);
+ RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, info,
+ info->shared_info());
+ if (FLAG_trace_opt) {
+ PrintF("[completed optimizing ");
+ info->closure()->ShortPrint();
+ PrintF("]\n");
+ }
+ return true;
+}
+
+
+static bool GetOptimizedCodeLater(CompilationInfo* info) {
+ Isolate* isolate = info->isolate();
+ if (!isolate->optimizing_compiler_thread()->IsQueueAvailable()) {
+ if (FLAG_trace_concurrent_recompilation) {
+ PrintF(" ** Compilation queue full, will retry optimizing ");
+ info->closure()->ShortPrint();
+ PrintF(" later.\n");
+ }
+ return false;
+ }
+
+ CompilationHandleScope handle_scope(info);
+ if (!CompileOptimizedPrologue(info)) return false;
+ info->SaveHandles(); // Copy handles to the compilation handle scope.
+
+ TimerEventScope<TimerEventRecompileSynchronous> timer(info->isolate());
+
+ OptimizedCompileJob* job = new (info->zone()) OptimizedCompileJob(info);
+ OptimizedCompileJob::Status status = job->CreateGraph();
+ if (status != OptimizedCompileJob::SUCCEEDED) return false;
+ isolate->optimizing_compiler_thread()->QueueForOptimization(job);
+
+ if (FLAG_trace_concurrent_recompilation) {
+ PrintF(" ** Queued ");
+ info->closure()->ShortPrint();
+ if (info->is_osr()) {
+ PrintF(" for concurrent OSR at %d.\n", info->osr_ast_id().ToInt());
+ } else {
+ PrintF(" for concurrent optimization.\n");
+ }
+ }
+ return true;
+}
+
+
+MaybeHandle<Code> Compiler::GetUnoptimizedCode(Handle<JSFunction> function) {
+ DCHECK(!function->GetIsolate()->has_pending_exception());
+ DCHECK(!function->is_compiled());
+ if (function->shared()->is_compiled()) {
+ return Handle<Code>(function->shared()->code());
+ }
+
+ CompilationInfoWithZone info(function);
+ Handle<Code> result;
+ ASSIGN_RETURN_ON_EXCEPTION(info.isolate(), result,
+ GetUnoptimizedCodeCommon(&info),
+ Code);
+ return result;
+}
+
+
+MaybeHandle<Code> Compiler::GetLazyCode(Handle<JSFunction> function) {
+ DCHECK(!function->GetIsolate()->has_pending_exception());
+ DCHECK(!function->is_compiled());
+
+ if (FLAG_turbo_asm && function->shared()->asm_function()) {
+ CompilationInfoWithZone info(function);
+
+ VMState<COMPILER> state(info.isolate());
+ PostponeInterruptsScope postpone(info.isolate());
+
+ info.SetOptimizing(BailoutId::None(),
+ Handle<Code>(function->shared()->code()));
+
+ info.MarkAsContextSpecializing();
+ info.MarkAsTypingEnabled();
+ info.MarkAsInliningDisabled();
+
+ if (GetOptimizedCodeNow(&info)) return info.code();
+ }
+
+ if (function->shared()->is_compiled()) {
+ return Handle<Code>(function->shared()->code());
+ }
+
+ CompilationInfoWithZone info(function);
+ Handle<Code> result;
+ ASSIGN_RETURN_ON_EXCEPTION(info.isolate(), result,
+ GetUnoptimizedCodeCommon(&info), Code);
+
+ if (FLAG_always_opt &&
+ info.isolate()->use_crankshaft() &&
+ !info.shared_info()->optimization_disabled() &&
+ !info.isolate()->DebuggerHasBreakPoints()) {
+ Handle<Code> opt_code;
+ if (Compiler::GetOptimizedCode(
+ function, result,
+ Compiler::NOT_CONCURRENT).ToHandle(&opt_code)) {
+ result = opt_code;
+ }
+ }
+
+ return result;
+}
+
+
+MaybeHandle<Code> Compiler::GetUnoptimizedCode(
+ Handle<SharedFunctionInfo> shared) {
+ DCHECK(!shared->GetIsolate()->has_pending_exception());
+ DCHECK(!shared->is_compiled());
+
+ CompilationInfoWithZone info(shared);
+ return GetUnoptimizedCodeCommon(&info);
+}
+
+
+bool Compiler::EnsureCompiled(Handle<JSFunction> function,
+ ClearExceptionFlag flag) {
+ if (function->is_compiled()) return true;
+ MaybeHandle<Code> maybe_code = Compiler::GetLazyCode(function);
+ Handle<Code> code;
+ if (!maybe_code.ToHandle(&code)) {
+ if (flag == CLEAR_EXCEPTION) {
+ function->GetIsolate()->clear_pending_exception();
+ }
+ return false;
+ }
+ function->ReplaceCode(*code);
+ DCHECK(function->is_compiled());
+ return true;
+}
+
+
+// TODO(turbofan): In the future, unoptimized code with deopt support could
+// be generated lazily once deopt is triggered.
+bool Compiler::EnsureDeoptimizationSupport(CompilationInfo* info) {
+ if (!info->shared_info()->has_deoptimization_support()) {
+ CompilationInfoWithZone unoptimized(info->shared_info());
+ // Note that we use the same AST that we will use for generating the
+ // optimized code.
+ unoptimized.SetFunction(info->function());
+ unoptimized.PrepareForCompilation(info->scope());
+ unoptimized.SetContext(info->context());
+ unoptimized.EnableDeoptimizationSupport();
+ if (!FullCodeGenerator::MakeCode(&unoptimized)) return false;
+
+ Handle<SharedFunctionInfo> shared = info->shared_info();
+ shared->EnableDeoptimizationSupport(*unoptimized.code());
+ shared->set_feedback_vector(*unoptimized.feedback_vector());
+
+ // The scope info might not have been set if a lazily compiled
+ // function is inlined before being called for the first time.
+ if (shared->scope_info() == ScopeInfo::Empty(info->isolate())) {
+ Handle<ScopeInfo> target_scope_info =
+ ScopeInfo::Create(info->scope(), info->zone());
+ shared->set_scope_info(*target_scope_info);
+ }
+
+ // The existing unoptimized code was replaced with the new one.
+ RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, &unoptimized, shared);
+ }
+ return true;
+}
+
+
+// Compile full code for debugging. This code will have debug break slots
+// and deoptimization information. Deoptimization information is required
+// in case that an optimized version of this function is still activated on
+// the stack. It will also make sure that the full code is compiled with
+// the same flags as the previous version, that is flags which can change
+// the code generated. The current method of mapping from already compiled
+// full code without debug break slots to full code with debug break slots
+// depends on the generated code is otherwise exactly the same.
+// If compilation fails, just keep the existing code.
+MaybeHandle<Code> Compiler::GetDebugCode(Handle<JSFunction> function) {
+ CompilationInfoWithZone info(function);
+ Isolate* isolate = info.isolate();
+ VMState<COMPILER> state(isolate);
+
+ info.MarkAsDebug();
+
+ DCHECK(!isolate->has_pending_exception());
+ Handle<Code> old_code(function->shared()->code());
+ DCHECK(old_code->kind() == Code::FUNCTION);
+ DCHECK(!old_code->has_debug_break_slots());
+
+ info.MarkCompilingForDebugging();
+ if (old_code->is_compiled_optimizable()) {
+ info.EnableDeoptimizationSupport();
+ } else {
+ info.MarkNonOptimizable();
+ }
+ MaybeHandle<Code> maybe_new_code = GetUnoptimizedCodeCommon(&info);
+ Handle<Code> new_code;
+ if (!maybe_new_code.ToHandle(&new_code)) {
+ isolate->clear_pending_exception();
+ } else {
+ DCHECK_EQ(old_code->is_compiled_optimizable(),
+ new_code->is_compiled_optimizable());
+ }
+ return maybe_new_code;
+}
+
+
+void Compiler::CompileForLiveEdit(Handle<Script> script) {
+ // TODO(635): support extensions.
+ CompilationInfoWithZone info(script);
+ PostponeInterruptsScope postpone(info.isolate());
+ VMState<COMPILER> state(info.isolate());
+
+ info.MarkAsGlobal();
+ if (!Parser::Parse(&info)) return;
+
+ LiveEditFunctionTracker tracker(info.isolate(), info.function());
+ if (!CompileUnoptimizedCode(&info)) return;
+ if (!info.shared_info().is_null()) {
+ Handle<ScopeInfo> scope_info = ScopeInfo::Create(info.scope(),
+ info.zone());
+ info.shared_info()->set_scope_info(*scope_info);
+ }
+ tracker.RecordRootFunctionInfo(info.code());
+}
+
+
+static Handle<SharedFunctionInfo> CompileToplevel(CompilationInfo* info) {
+ Isolate* isolate = info->isolate();
+ PostponeInterruptsScope postpone(isolate);
+ DCHECK(!isolate->native_context().is_null());
+ Handle<Script> script = 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(0));
+
+ isolate->debug()->OnBeforeCompile(script);
+
+ DCHECK(info->is_eval() || info->is_global());
+
+ Handle<SharedFunctionInfo> result;
+
+ { VMState<COMPILER> state(info->isolate());
+ if (info->function() == NULL) {
+ // Parse the script if needed (if it's already parsed, function() is
+ // non-NULL).
+ bool parse_allow_lazy =
+ (info->compile_options() == ScriptCompiler::kConsumeParserCache ||
+ String::cast(script->source())->length() >
+ FLAG_min_preparse_length) &&
+ !Compiler::DebuggerWantsEagerCompilation(info);
+
+ if (!parse_allow_lazy &&
+ (info->compile_options() == ScriptCompiler::kProduceParserCache ||
+ info->compile_options() == ScriptCompiler::kConsumeParserCache)) {
+ // We are going to parse eagerly, but we either 1) have cached data
+ // produced by lazy parsing or 2) are asked to generate cached data.
+ // Eager parsing cannot benefit from cached data, and producing cached
+ // data while parsing eagerly is not implemented.
+ info->SetCachedData(NULL, ScriptCompiler::kNoCompileOptions);
+ }
+ if (!Parser::Parse(info, parse_allow_lazy)) {
+ return Handle<SharedFunctionInfo>::null();
+ }
+ }
+
+ FunctionLiteral* lit = info->function();
+ LiveEditFunctionTracker live_edit_tracker(isolate, lit);
+
+ // 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.
+ HistogramTimer* rate = info->is_eval()
+ ? info->isolate()->counters()->compile_eval()
+ : info->isolate()->counters()->compile();
+ HistogramTimerScope timer(rate);
+
+ // Compile the code.
+ if (!CompileUnoptimizedCode(info)) {
+ return Handle<SharedFunctionInfo>::null();
+ }
+
+ // Allocate function.
+ DCHECK(!info->code().is_null());
+ result = isolate->factory()->NewSharedFunctionInfo(
+ lit->name(), lit->materialized_literal_count(), lit->kind(),
+ info->code(), ScopeInfo::Create(info->scope(), info->zone()),
+ info->feedback_vector());
+
+ DCHECK_EQ(RelocInfo::kNoPosition, lit->function_token_position());
+ SetFunctionInfo(result, lit, true, script);
+
+ Handle<String> script_name = script->name()->IsString()
+ ? Handle<String>(String::cast(script->name()))
+ : isolate->factory()->empty_string();
+ Logger::LogEventsAndTags log_tag = info->is_eval()
+ ? Logger::EVAL_TAG
+ : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script);
+
+ PROFILE(isolate, CodeCreateEvent(
+ log_tag, *info->code(), *result, info, *script_name));
+ GDBJIT(AddCode(script_name, script, info->code(), info));
+
+ // Hint to the runtime system used when allocating space for initial
+ // property space by setting the expected number of properties for
+ // the instances of the function.
+ SetExpectedNofPropertiesFromEstimate(result,
+ lit->expected_property_count());
+
+ if (!script.is_null())
+ script->set_compilation_state(Script::COMPILATION_STATE_COMPILED);
+
+ live_edit_tracker.RecordFunctionInfo(result, lit, info->zone());
+ }
+
+ isolate->debug()->OnAfterCompile(script);
+
+ return result;
+}
+
+
+MaybeHandle<JSFunction> Compiler::GetFunctionFromEval(
+ Handle<String> source, Handle<SharedFunctionInfo> outer_info,
+ Handle<Context> context, StrictMode strict_mode,
+ ParseRestriction restriction, int scope_position) {
+ Isolate* isolate = source->GetIsolate();
+ int source_length = source->length();
+ isolate->counters()->total_eval_size()->Increment(source_length);
+ isolate->counters()->total_compile_size()->Increment(source_length);
+
+ CompilationCache* compilation_cache = isolate->compilation_cache();
+ MaybeHandle<SharedFunctionInfo> maybe_shared_info =
+ compilation_cache->LookupEval(source, outer_info, context, strict_mode,
+ scope_position);
+ Handle<SharedFunctionInfo> shared_info;
+
+ if (!maybe_shared_info.ToHandle(&shared_info)) {
+ Handle<Script> script = isolate->factory()->NewScript(source);
+ CompilationInfoWithZone info(script);
+ info.MarkAsEval();
+ if (context->IsNativeContext()) info.MarkAsGlobal();
+ info.SetStrictMode(strict_mode);
+ info.SetParseRestriction(restriction);
+ info.SetContext(context);
+
+ Debug::RecordEvalCaller(script);
+
+ shared_info = CompileToplevel(&info);
+
+ if (shared_info.is_null()) {
+ return MaybeHandle<JSFunction>();
+ } else {
+ // Explicitly disable optimization for eval code. We're not yet prepared
+ // to handle eval-code in the optimizing compiler.
+ shared_info->DisableOptimization(kEval);
+
+ // If caller is strict mode, the result must be in strict mode as well.
+ DCHECK(strict_mode == SLOPPY || shared_info->strict_mode() == STRICT);
+ if (!shared_info->dont_cache()) {
+ compilation_cache->PutEval(source, outer_info, context, shared_info,
+ scope_position);
+ }
+ }
+ } else if (shared_info->ic_age() != isolate->heap()->global_ic_age()) {
+ shared_info->ResetForNewContext(isolate->heap()->global_ic_age());
+ }
+
+ return isolate->factory()->NewFunctionFromSharedFunctionInfo(
+ shared_info, context, NOT_TENURED);
+}
+
+
+Handle<SharedFunctionInfo> Compiler::CompileScript(
+ Handle<String> source, Handle<Object> script_name, int line_offset,
+ int column_offset, bool is_shared_cross_origin, Handle<Context> context,
+ v8::Extension* extension, ScriptData** cached_data,
+ ScriptCompiler::CompileOptions compile_options, NativesFlag natives) {
+ if (compile_options == ScriptCompiler::kNoCompileOptions) {
+ cached_data = NULL;
+ } else if (compile_options == ScriptCompiler::kProduceParserCache ||
+ compile_options == ScriptCompiler::kProduceCodeCache) {
+ DCHECK(cached_data && !*cached_data);
+ DCHECK(extension == NULL);
+ } else {
+ DCHECK(compile_options == ScriptCompiler::kConsumeParserCache ||
+ compile_options == ScriptCompiler::kConsumeCodeCache);
+ DCHECK(cached_data && *cached_data);
+ DCHECK(extension == NULL);
+ }
+ Isolate* isolate = source->GetIsolate();
+ int source_length = source->length();
+ isolate->counters()->total_load_size()->Increment(source_length);
+ isolate->counters()->total_compile_size()->Increment(source_length);
+
+ CompilationCache* compilation_cache = isolate->compilation_cache();
+
+ // Do a lookup in the compilation cache but not for extensions.
+ MaybeHandle<SharedFunctionInfo> maybe_result;
+ Handle<SharedFunctionInfo> result;
+ if (extension == NULL) {
+ if (FLAG_serialize_toplevel &&
+ compile_options == ScriptCompiler::kConsumeCodeCache &&
+ !isolate->debug()->is_loaded()) {
+ HistogramTimerScope timer(isolate->counters()->compile_deserialize());
+ return CodeSerializer::Deserialize(isolate, *cached_data, source);
+ } else {
+ maybe_result = compilation_cache->LookupScript(
+ source, script_name, line_offset, column_offset,
+ is_shared_cross_origin, context);
+ }
+ }
+
+ base::ElapsedTimer timer;
+ if (FLAG_profile_deserialization && FLAG_serialize_toplevel &&
+ compile_options == ScriptCompiler::kProduceCodeCache) {
+ timer.Start();
+ }
+
+ if (!maybe_result.ToHandle(&result)) {
+ // No cache entry found. Compile the script.
+
+ // Create a script object describing the script to be compiled.
+ Handle<Script> script = isolate->factory()->NewScript(source);
+ if (natives == NATIVES_CODE) {
+ script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
+ }
+ if (!script_name.is_null()) {
+ script->set_name(*script_name);
+ script->set_line_offset(Smi::FromInt(line_offset));
+ script->set_column_offset(Smi::FromInt(column_offset));
+ }
+ script->set_is_shared_cross_origin(is_shared_cross_origin);
+
+ // Compile the function and add it to the cache.
+ CompilationInfoWithZone info(script);
+ info.MarkAsGlobal();
+ info.SetCachedData(cached_data, compile_options);
+ info.SetExtension(extension);
+ info.SetContext(context);
+ if (FLAG_serialize_toplevel &&
+ compile_options == ScriptCompiler::kProduceCodeCache) {
+ info.PrepareForSerializing();
+ }
+ if (FLAG_use_strict) info.SetStrictMode(STRICT);
+
+ result = CompileToplevel(&info);
+ if (extension == NULL && !result.is_null() && !result->dont_cache()) {
+ compilation_cache->PutScript(source, context, result);
+ if (FLAG_serialize_toplevel &&
+ compile_options == ScriptCompiler::kProduceCodeCache) {
+ HistogramTimerScope histogram_timer(
+ isolate->counters()->compile_serialize());
+ *cached_data = CodeSerializer::Serialize(isolate, result, source);
+ if (FLAG_profile_deserialization) {
+ PrintF("[Compiling and serializing %d bytes took %0.3f ms]\n",
+ (*cached_data)->length(), timer.Elapsed().InMillisecondsF());
+ }
+ }
+ }
+
+ if (result.is_null()) isolate->ReportPendingMessages();
+ } else if (result->ic_age() != isolate->heap()->global_ic_age()) {
+ result->ResetForNewContext(isolate->heap()->global_ic_age());
+ }
+ return result;
+}
+
+
+Handle<SharedFunctionInfo> Compiler::CompileStreamedScript(
+ CompilationInfo* info, int source_length) {
+ Isolate* isolate = info->isolate();
+ isolate->counters()->total_load_size()->Increment(source_length);
+ isolate->counters()->total_compile_size()->Increment(source_length);
+
+ if (FLAG_use_strict) info->SetStrictMode(STRICT);
+ // TODO(marja): FLAG_serialize_toplevel is not honoured and won't be; when the
+ // real code caching lands, streaming needs to be adapted to use it.
+ return CompileToplevel(info);
+}
+
+
+Handle<SharedFunctionInfo> Compiler::BuildFunctionInfo(
+ FunctionLiteral* literal, Handle<Script> script,
+ CompilationInfo* outer_info) {
+ // Precondition: code has been parsed and scopes have been analyzed.
+ CompilationInfoWithZone info(script);
+ info.SetFunction(literal);
+ info.PrepareForCompilation(literal->scope());
+ info.SetStrictMode(literal->scope()->strict_mode());
+ if (outer_info->will_serialize()) info.PrepareForSerializing();
+
+ Isolate* isolate = info.isolate();
+ Factory* factory = isolate->factory();
+ LiveEditFunctionTracker live_edit_tracker(isolate, literal);
+ // Determine if the function can be lazily compiled. This is necessary to
+ // allow some of our builtin JS files to be lazily compiled. These
+ // builtins cannot be handled lazily by the parser, since we have to know
+ // if a function uses the special natives syntax, which is something the
+ // parser records.
+ // If the debugger requests compilation for break points, we cannot be
+ // aggressive about lazy compilation, because it might trigger compilation
+ // of functions without an outer context when setting a breakpoint through
+ // Debug::FindSharedFunctionInfoInScript.
+ bool allow_lazy_without_ctx = literal->AllowsLazyCompilationWithoutContext();
+ bool allow_lazy = literal->AllowsLazyCompilation() &&
+ !DebuggerWantsEagerCompilation(&info, allow_lazy_without_ctx);
+
+ // Generate code
+ Handle<ScopeInfo> scope_info;
+ if (FLAG_lazy && allow_lazy && !literal->is_parenthesized()) {
+ Handle<Code> code = isolate->builtins()->CompileLazy();
+ info.SetCode(code);
+ scope_info = Handle<ScopeInfo>(ScopeInfo::Empty(isolate));
+ } else if (FullCodeGenerator::MakeCode(&info)) {
+ DCHECK(!info.code().is_null());
+ scope_info = ScopeInfo::Create(info.scope(), info.zone());
+ } else {
+ return Handle<SharedFunctionInfo>::null();
+ }
+
+ // Create a shared function info object.
+ Handle<SharedFunctionInfo> result = factory->NewSharedFunctionInfo(
+ literal->name(), literal->materialized_literal_count(), literal->kind(),
+ info.code(), scope_info, info.feedback_vector());
+ SetFunctionInfo(result, literal, false, script);
+ RecordFunctionCompilation(Logger::FUNCTION_TAG, &info, result);
+ result->set_allows_lazy_compilation(allow_lazy);
+ result->set_allows_lazy_compilation_without_context(allow_lazy_without_ctx);
+
+ // Set the expected number of properties for instances and return
+ // the resulting function.
+ SetExpectedNofPropertiesFromEstimate(result,
+ literal->expected_property_count());
+ live_edit_tracker.RecordFunctionInfo(result, literal, info.zone());
+ return result;
+}
+
+
+MaybeHandle<Code> Compiler::GetOptimizedCode(Handle<JSFunction> function,
+ Handle<Code> current_code,
+ ConcurrencyMode mode,
+ BailoutId osr_ast_id) {
+ Handle<Code> cached_code;
+ if (GetCodeFromOptimizedCodeMap(
+ function, osr_ast_id).ToHandle(&cached_code)) {
+ return cached_code;
+ }
+
+ SmartPointer<CompilationInfo> info(new CompilationInfoWithZone(function));
+ Isolate* isolate = info->isolate();
+ DCHECK(AllowCompilation::IsAllowed(isolate));
+ VMState<COMPILER> state(isolate);
+ DCHECK(!isolate->has_pending_exception());
+ PostponeInterruptsScope postpone(isolate);
+
+ Handle<SharedFunctionInfo> shared = info->shared_info();
+ if (shared->code()->kind() != Code::FUNCTION ||
+ ScopeInfo::Empty(isolate) == shared->scope_info()) {
+ // The function was never compiled. Compile it unoptimized first.
+ // TODO(titzer): reuse the AST and scope info from this compile.
+ CompilationInfoWithZone nested(function);
+ nested.EnableDeoptimizationSupport();
+ if (!GetUnoptimizedCodeCommon(&nested).ToHandle(¤t_code)) {
+ return MaybeHandle<Code>();
+ }
+ shared->ReplaceCode(*current_code);
+ }
+ current_code->set_profiler_ticks(0);
+
+ info->SetOptimizing(osr_ast_id, current_code);
+
+ if (mode == CONCURRENT) {
+ if (GetOptimizedCodeLater(info.get())) {
+ info.Detach(); // The background recompile job owns this now.
+ return isolate->builtins()->InOptimizationQueue();
+ }
+ } else {
+ if (GetOptimizedCodeNow(info.get())) return info->code();
+ }
+
+ if (isolate->has_pending_exception()) isolate->clear_pending_exception();
+ return MaybeHandle<Code>();
+}
+
+
+Handle<Code> Compiler::GetConcurrentlyOptimizedCode(OptimizedCompileJob* job) {
+ // Take ownership of compilation info. Deleting compilation info
+ // also tears down the zone and the recompile job.
+ SmartPointer<CompilationInfo> info(job->info());
+ Isolate* isolate = info->isolate();
+
+ VMState<COMPILER> state(isolate);
+ TimerEventScope<TimerEventRecompileSynchronous> timer(info->isolate());
+
+ Handle<SharedFunctionInfo> shared = info->shared_info();
+ shared->code()->set_profiler_ticks(0);
+
+ // 1) Optimization on the concurrent thread may have failed.
+ // 2) The function may have already been optimized by OSR. Simply continue.
+ // Except when OSR already disabled optimization for some reason.
+ // 3) The code may have already been invalidated due to dependency change.
+ // 4) Debugger may have been activated.
+ // 5) Code generation may have failed.
+ if (job->last_status() == OptimizedCompileJob::SUCCEEDED) {
+ if (shared->optimization_disabled()) {
+ job->RetryOptimization(kOptimizationDisabled);
+ } else if (info->HasAbortedDueToDependencyChange()) {
+ job->RetryOptimization(kBailedOutDueToDependencyChange);
+ } else if (isolate->DebuggerHasBreakPoints()) {
+ job->RetryOptimization(kDebuggerHasBreakPoints);
+ } else if (job->GenerateCode() == OptimizedCompileJob::SUCCEEDED) {
+ RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, info.get(), shared);
+ if (info->shared_info()->SearchOptimizedCodeMap(
+ info->context()->native_context(), info->osr_ast_id()) == -1) {
+ InsertCodeIntoOptimizedCodeMap(info.get());
+ }
+ if (FLAG_trace_opt) {
+ PrintF("[completed optimizing ");
+ info->closure()->ShortPrint();
+ PrintF("]\n");
+ }
+ return Handle<Code>(*info->code());
+ }
+ }
+
+ DCHECK(job->last_status() != OptimizedCompileJob::SUCCEEDED);
+ if (FLAG_trace_opt) {
+ PrintF("[aborted optimizing ");
+ info->closure()->ShortPrint();
+ PrintF(" because: %s]\n", GetBailoutReason(info->bailout_reason()));
+ }
+ return Handle<Code>::null();
+}
+
+
+bool Compiler::DebuggerWantsEagerCompilation(CompilationInfo* info,
+ bool allow_lazy_without_ctx) {
+ return LiveEditFunctionTracker::IsActive(info->isolate()) ||
+ (info->isolate()->DebuggerHasBreakPoints() && !allow_lazy_without_ctx);
+}
+
+
+CompilationPhase::CompilationPhase(const char* name, CompilationInfo* info)
+ : name_(name), info_(info), zone_(info->isolate()) {
+ if (FLAG_hydrogen_stats) {
+ info_zone_start_allocation_size_ = info->zone()->allocation_size();
+ timer_.Start();
+ }
+}
+
+
+CompilationPhase::~CompilationPhase() {
+ if (FLAG_hydrogen_stats) {
+ unsigned size = zone()->allocation_size();
+ size += info_->zone()->allocation_size() - info_zone_start_allocation_size_;
+ isolate()->GetHStatistics()->SaveTiming(name_, timer_.Elapsed(), size);
+ }
+}
+
+
+bool CompilationPhase::ShouldProduceTraceOutput() const {
+ // Trace if the appropriate trace flag is set and the phase name's first
+ // character is in the FLAG_trace_phase command line parameter.
+ AllowHandleDereference allow_deref;
+ bool tracing_on = info()->IsStub()
+ ? FLAG_trace_hydrogen_stubs
+ : (FLAG_trace_hydrogen &&
+ info()->closure()->PassesFilter(FLAG_trace_hydrogen_filter));
+ return (tracing_on &&
+ base::OS::StrChr(const_cast<char*>(FLAG_trace_phase), name_[0]) != NULL);
+}
+
} } // namespace v8::internal