2012-02-08 09:56:33 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
2010-10-01 14:10:47 +00:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "bootstrapper.h"
|
2011-04-07 14:42:37 +00:00
|
|
|
#include "codegen.h"
|
2008-09-11 10:51:52 +00:00
|
|
|
#include "compilation-cache.h"
|
2013-07-03 15:39:18 +00:00
|
|
|
#include "cpu-profiler.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "debug.h"
|
2013-03-12 18:03:18 +00:00
|
|
|
#include "deoptimizer.h"
|
2010-01-20 16:28:21 +00:00
|
|
|
#include "full-codegen.h"
|
2011-01-18 16:11:01 +00:00
|
|
|
#include "gdb-jit.h"
|
2013-05-27 13:59:20 +00:00
|
|
|
#include "typing.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "hydrogen.h"
|
2011-10-03 11:13:20 +00:00
|
|
|
#include "isolate-inl.h"
|
2011-01-19 13:55:56 +00:00
|
|
|
#include "lithium.h"
|
2010-03-08 13:01:24 +00:00
|
|
|
#include "liveedit.h"
|
2010-10-01 14:10:47 +00:00
|
|
|
#include "parser.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "rewriter.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "runtime-profiler.h"
|
2011-09-08 13:06:44 +00:00
|
|
|
#include "scanner-character-streams.h"
|
2010-07-13 13:06:33 +00:00
|
|
|
#include "scopeinfo.h"
|
2010-10-01 14:10:47 +00:00
|
|
|
#include "scopes.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "vm-state-inl.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-10-04 11:35:46 +00:00
|
|
|
|
2013-06-24 13:37:46 +00:00
|
|
|
CompilationInfo::CompilationInfo(Handle<Script> script,
|
2013-06-27 13:03:01 +00:00
|
|
|
Zone* zone)
|
2014-03-11 14:41:22 +00:00
|
|
|
: flags_(StrictModeField::encode(SLOPPY)),
|
2010-10-04 11:35:46 +00:00
|
|
|
script_(script),
|
2013-09-04 12:55:59 +00:00
|
|
|
osr_ast_id_(BailoutId::None()),
|
2014-01-14 12:04:10 +00:00
|
|
|
parameter_count_(0),
|
2014-02-13 16:09:28 +00:00
|
|
|
this_has_uses_(true),
|
|
|
|
optimization_id_(-1) {
|
2013-06-27 13:03:01 +00:00
|
|
|
Initialize(script->GetIsolate(), BASE, zone);
|
2010-10-04 11:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-20 08:58:41 +00:00
|
|
|
CompilationInfo::CompilationInfo(Handle<SharedFunctionInfo> shared_info,
|
2013-06-27 13:03:01 +00:00
|
|
|
Zone* zone)
|
2014-03-11 14:41:22 +00:00
|
|
|
: flags_(StrictModeField::encode(SLOPPY) | IsLazy::encode(true)),
|
2010-10-04 11:35:46 +00:00
|
|
|
shared_info_(shared_info),
|
|
|
|
script_(Handle<Script>(Script::cast(shared_info->script()))),
|
2013-09-04 12:55:59 +00:00
|
|
|
osr_ast_id_(BailoutId::None()),
|
2014-01-14 12:04:10 +00:00
|
|
|
parameter_count_(0),
|
2014-02-13 16:09:28 +00:00
|
|
|
this_has_uses_(true),
|
|
|
|
optimization_id_(-1) {
|
2013-06-27 13:03:01 +00:00
|
|
|
Initialize(script_->GetIsolate(), BASE, zone);
|
2010-10-04 11:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-24 13:37:46 +00:00
|
|
|
CompilationInfo::CompilationInfo(Handle<JSFunction> closure,
|
2013-06-27 13:03:01 +00:00
|
|
|
Zone* zone)
|
2014-03-11 14:41:22 +00:00
|
|
|
: flags_(StrictModeField::encode(SLOPPY) | IsLazy::encode(true)),
|
2010-10-04 11:35:46 +00:00
|
|
|
closure_(closure),
|
|
|
|
shared_info_(Handle<SharedFunctionInfo>(closure->shared())),
|
|
|
|
script_(Handle<Script>(Script::cast(shared_info_->script()))),
|
2012-08-28 11:25:08 +00:00
|
|
|
context_(closure->context()),
|
2013-09-04 12:55:59 +00:00
|
|
|
osr_ast_id_(BailoutId::None()),
|
2014-01-14 12:04:10 +00:00
|
|
|
parameter_count_(0),
|
2014-02-13 16:09:28 +00:00
|
|
|
this_has_uses_(true),
|
|
|
|
optimization_id_(-1) {
|
2013-06-27 13:03:01 +00:00
|
|
|
Initialize(script_->GetIsolate(), BASE, zone);
|
2012-11-29 07:38:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-18 16:25:45 +00:00
|
|
|
CompilationInfo::CompilationInfo(HydrogenCodeStub* stub,
|
2013-06-24 13:37:46 +00:00
|
|
|
Isolate* isolate,
|
2013-06-27 13:03:01 +00:00
|
|
|
Zone* zone)
|
2014-03-11 14:41:22 +00:00
|
|
|
: flags_(StrictModeField::encode(SLOPPY) | IsLazy::encode(true)),
|
2013-09-04 12:55:59 +00:00
|
|
|
osr_ast_id_(BailoutId::None()),
|
2014-01-14 12:04:10 +00:00
|
|
|
parameter_count_(0),
|
2014-02-13 16:09:28 +00:00
|
|
|
this_has_uses_(true),
|
|
|
|
optimization_id_(-1) {
|
2013-06-27 13:03:01 +00:00
|
|
|
Initialize(isolate, STUB, zone);
|
2012-12-18 16:25:45 +00:00
|
|
|
code_stub_ = stub;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-24 13:37:46 +00:00
|
|
|
void CompilationInfo::Initialize(Isolate* isolate,
|
|
|
|
Mode mode,
|
2013-06-27 13:03:01 +00:00
|
|
|
Zone* zone) {
|
2012-12-18 16:25:45 +00:00
|
|
|
isolate_ = isolate;
|
2012-11-29 07:38:00 +00:00
|
|
|
function_ = NULL;
|
|
|
|
scope_ = NULL;
|
|
|
|
global_scope_ = NULL;
|
|
|
|
extension_ = NULL;
|
2014-03-19 13:24:13 +00:00
|
|
|
cached_data_ = NULL;
|
|
|
|
cached_data_mode_ = NO_CACHED_DATA;
|
2012-11-29 07:38:00 +00:00
|
|
|
zone_ = zone;
|
|
|
|
deferred_handles_ = NULL;
|
2012-12-18 16:25:45 +00:00
|
|
|
code_stub_ = NULL;
|
2013-10-23 13:48:04 +00:00
|
|
|
prologue_offset_ = Code::kPrologueOffsetNotSet;
|
2013-01-23 13:52:00 +00:00
|
|
|
opt_count_ = shared_info().is_null() ? 0 : shared_info()->opt_count();
|
2013-05-14 22:51:33 +00:00
|
|
|
no_frame_ranges_ = isolate->cpu_profiler()->is_profiling()
|
|
|
|
? new List<OffsetRange>(2) : NULL;
|
2013-06-12 09:43:22 +00:00
|
|
|
for (int i = 0; i < DependentCode::kGroupCount; i++) {
|
2013-06-26 16:17:12 +00:00
|
|
|
dependencies_[i] = NULL;
|
2013-06-12 09:43:22 +00:00
|
|
|
}
|
2012-12-18 16:25:45 +00:00
|
|
|
if (mode == STUB) {
|
|
|
|
mode_ = STUB;
|
|
|
|
return;
|
|
|
|
}
|
2013-10-23 08:57:54 +00:00
|
|
|
mode_ = mode;
|
2013-08-12 14:10:25 +00:00
|
|
|
abort_due_to_dependency_ = false;
|
2012-11-29 07:38:00 +00:00
|
|
|
if (script_->type()->value() == Script::TYPE_NATIVE) {
|
|
|
|
MarkAsNative();
|
|
|
|
}
|
|
|
|
if (!shared_info_.is_null()) {
|
2014-03-11 14:41:22 +00:00
|
|
|
ASSERT(strict_mode() == SLOPPY);
|
|
|
|
SetStrictMode(shared_info_->strict_mode());
|
2012-11-29 07:38:00 +00:00
|
|
|
}
|
2013-08-02 09:53:11 +00:00
|
|
|
set_bailout_reason(kUnknown);
|
2010-10-04 11:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-06 09:31:31 +00:00
|
|
|
CompilationInfo::~CompilationInfo() {
|
|
|
|
delete deferred_handles_;
|
2013-05-14 22:51:33 +00:00
|
|
|
delete no_frame_ranges_;
|
2013-06-12 09:43:22 +00:00
|
|
|
#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++) {
|
2013-06-26 16:17:12 +00:00
|
|
|
ASSERT_EQ(NULL, dependencies_[i]);
|
2013-06-12 09:43:22 +00:00
|
|
|
}
|
|
|
|
#endif // DEBUG
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-26 16:17:12 +00:00
|
|
|
void CompilationInfo::CommitDependencies(Handle<Code> code) {
|
2013-06-12 09:43:22 +00:00
|
|
|
for (int i = 0; i < DependentCode::kGroupCount; i++) {
|
2013-06-26 16:17:12 +00:00
|
|
|
ZoneList<Handle<HeapObject> >* group_objects = dependencies_[i];
|
|
|
|
if (group_objects == NULL) continue;
|
2013-06-12 09:43:22 +00:00
|
|
|
ASSERT(!object_wrapper_.is_null());
|
2013-06-26 16:17:12 +00:00
|
|
|
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);
|
2013-06-12 09:43:22 +00:00
|
|
|
}
|
2013-06-26 16:17:12 +00:00
|
|
|
dependencies_[i] = NULL; // Zone-allocated, no need to delete.
|
2013-06-12 09:43:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-26 16:17:12 +00:00
|
|
|
void CompilationInfo::RollbackDependencies() {
|
2013-06-12 09:43:22 +00:00
|
|
|
// Unregister from all dependent maps if not yet committed.
|
|
|
|
for (int i = 0; i < DependentCode::kGroupCount; i++) {
|
2013-06-26 16:17:12 +00:00
|
|
|
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);
|
2013-06-12 09:43:22 +00:00
|
|
|
}
|
2013-06-26 16:17:12 +00:00
|
|
|
dependencies_[i] = NULL; // Zone-allocated, no need to delete.
|
2013-06-12 09:43:22 +00:00
|
|
|
}
|
2012-07-06 09:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-18 16:25:45 +00:00
|
|
|
int CompilationInfo::num_parameters() const {
|
2013-11-15 10:52:05 +00:00
|
|
|
if (IsStub()) {
|
|
|
|
ASSERT(parameter_count_ > 0);
|
|
|
|
return parameter_count_;
|
|
|
|
} else {
|
|
|
|
return scope()->num_parameters();
|
|
|
|
}
|
2012-12-18 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CompilationInfo::num_heap_slots() const {
|
|
|
|
if (IsStub()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return scope()->num_heap_slots();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Code::Flags CompilationInfo::flags() const {
|
|
|
|
if (IsStub()) {
|
2013-04-18 09:50:46 +00:00
|
|
|
return Code::ComputeFlags(code_stub()->GetCodeKind(),
|
|
|
|
code_stub()->GetICState(),
|
|
|
|
code_stub()->GetExtraICState(),
|
2014-02-14 15:15:08 +00:00
|
|
|
code_stub()->GetStubType());
|
2012-12-18 16:25:45 +00:00
|
|
|
} else {
|
|
|
|
return Code::ComputeFlags(Code::OPTIMIZED_FUNCTION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-19 13:15:57 +00:00
|
|
|
// Disable optimization for the rest of the compilation pipeline.
|
2011-01-17 08:11:03 +00:00
|
|
|
void CompilationInfo::DisableOptimization() {
|
2011-05-06 11:02:23 +00:00
|
|
|
bool is_optimizable_closure =
|
|
|
|
FLAG_optimize_closures &&
|
|
|
|
closure_.is_null() &&
|
|
|
|
!scope_->HasTrivialOuterContext() &&
|
2014-03-11 14:39:08 +00:00
|
|
|
!scope_->outer_scope_calls_sloppy_eval() &&
|
2011-05-06 11:02:23 +00:00
|
|
|
!scope_->inside_with();
|
|
|
|
SetMode(is_optimizable_closure ? BASE : NONOPT);
|
2011-01-17 08:11:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-09 10:19:46 +00:00
|
|
|
// Primitive functions are unlikely to be picked up by the stack-walking
|
|
|
|
// profiler, so they trigger their own optimization when they're called
|
|
|
|
// for the SharedFunctionInfo::kCallsUntilPrimitiveOptimization-th time.
|
|
|
|
bool CompilationInfo::ShouldSelfOptimize() {
|
2013-12-06 09:52:40 +00:00
|
|
|
return FLAG_crankshaft &&
|
2012-02-09 10:19:46 +00:00
|
|
|
!function()->flags()->Contains(kDontSelfOptimize) &&
|
2013-09-05 13:20:51 +00:00
|
|
|
!function()->dont_optimize() &&
|
2012-06-19 14:29:48 +00:00
|
|
|
function()->scope()->AllowsLazyCompilation() &&
|
2012-02-09 10:19:46 +00:00
|
|
|
(shared_info().is_null() || !shared_info()->optimization_disabled());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-10 21:38:17 +00:00
|
|
|
void CompilationInfo::PrepareForCompilation(Scope* scope) {
|
|
|
|
ASSERT(scope_ == NULL);
|
|
|
|
scope_ = scope;
|
|
|
|
function()->ProcessFeedbackSlots(isolate_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-29 10:02:32 +00:00
|
|
|
class HOptimizedGraphBuilderWithPositions: public HOptimizedGraphBuilder {
|
2013-10-21 13:35:48 +00:00
|
|
|
public:
|
2013-11-29 10:02:32 +00:00
|
|
|
explicit HOptimizedGraphBuilderWithPositions(CompilationInfo* info)
|
2013-10-21 13:35:48 +00:00
|
|
|
: HOptimizedGraphBuilder(info) {
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DEF_VISIT(type) \
|
|
|
|
virtual void Visit##type(type* node) V8_OVERRIDE { \
|
|
|
|
if (node->position() != RelocInfo::kNoPosition) { \
|
|
|
|
SetSourcePosition(node->position()); \
|
|
|
|
} \
|
|
|
|
HOptimizedGraphBuilder::Visit##type(node); \
|
|
|
|
}
|
|
|
|
EXPRESSION_NODE_LIST(DEF_VISIT)
|
|
|
|
#undef DEF_VISIT
|
|
|
|
|
|
|
|
#define DEF_VISIT(type) \
|
|
|
|
virtual void Visit##type(type* node) V8_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) V8_OVERRIDE { \
|
|
|
|
HOptimizedGraphBuilder::Visit##type(node); \
|
|
|
|
}
|
|
|
|
MODULE_NODE_LIST(DEF_VISIT)
|
|
|
|
DECLARATION_NODE_LIST(DEF_VISIT)
|
|
|
|
#undef DEF_VISIT
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
// 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 IsDebuggerActive(Isolate* isolate) {
|
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
|
|
|
return isolate->use_crankshaft() ?
|
|
|
|
isolate->debug()->has_break_points() :
|
|
|
|
isolate->debugger()->IsDebuggerActive();
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OptimizedCompileJob::Status OptimizedCompileJob::CreateGraph() {
|
2013-09-03 08:49:44 +00:00
|
|
|
ASSERT(isolate()->use_crankshaft());
|
2012-07-17 16:24:40 +00:00
|
|
|
ASSERT(info()->IsOptimizing());
|
|
|
|
ASSERT(!info()->IsCompilingForDebugging());
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2012-07-17 16:24:40 +00:00
|
|
|
// We should never arrive here if there is no code object on the
|
2010-12-07 11:31:57 +00:00
|
|
|
// shared function object.
|
2013-08-20 08:53:34 +00:00
|
|
|
ASSERT(info()->shared_info()->code()->kind() == Code::FUNCTION);
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2011-03-28 05:57:27 +00:00
|
|
|
// We should never arrive here if optimization has been disabled on the
|
|
|
|
// shared function info.
|
2012-07-17 16:24:40 +00:00
|
|
|
ASSERT(!info()->shared_info()->optimization_disabled());
|
2011-03-28 05:57:27 +00:00
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
// 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.
|
2013-12-23 14:30:35 +00:00
|
|
|
if (FLAG_always_full_compiler) return AbortOptimization();
|
|
|
|
if (IsDebuggerActive(isolate())) return AbortOptimization(kDebuggerIsActive);
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
// Limit the number of times we re-compile a functions with
|
|
|
|
// the optimizing compiler.
|
2011-01-18 13:43:48 +00:00
|
|
|
const int kMaxOptCount =
|
2012-08-16 11:40:03 +00:00
|
|
|
FLAG_deopt_every_n_times == 0 ? FLAG_max_opt_count : 1000;
|
2013-01-23 13:52:00 +00:00
|
|
|
if (info()->opt_count() > kMaxOptCount) {
|
2013-12-23 14:30:35 +00:00
|
|
|
return AbortAndDisableOptimization(kOptimizedTooManyTimes);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Due to an encoding limit on LUnallocated operands in the Lithium
|
|
|
|
// language, we cannot optimize functions with too many formal parameters
|
|
|
|
// or perform on-stack replacement for function with too many
|
|
|
|
// stack-allocated local variables.
|
|
|
|
//
|
2011-02-28 13:20:10 +00:00
|
|
|
// The encoding is as a signed value, with parameters and receiver using
|
|
|
|
// the negative indices and locals the non-negative ones.
|
2013-05-02 09:51:07 +00:00
|
|
|
const int parameter_limit = -LUnallocated::kMinFixedSlotIndex;
|
2012-07-17 16:24:40 +00:00
|
|
|
Scope* scope = info()->scope();
|
2012-08-28 07:18:06 +00:00
|
|
|
if ((scope->num_parameters() + 1) > parameter_limit) {
|
2013-12-23 14:30:35 +00:00
|
|
|
return AbortAndDisableOptimization(kTooManyParameters);
|
2012-08-28 07:18:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-02 09:51:07 +00:00
|
|
|
const int locals_limit = LUnallocated::kMaxFixedSlotIndex;
|
2013-09-09 16:34:40 +00:00
|
|
|
if (info()->is_osr() &&
|
2012-08-28 07:18:06 +00:00
|
|
|
scope->num_parameters() + 1 + scope->num_stack_slots() > locals_limit) {
|
2013-12-23 14:30:35 +00:00
|
|
|
return AbortAndDisableOptimization(kTooManyParametersLocals);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
2014-03-31 16:45:46 +00:00
|
|
|
if (scope->HasIllegalRedeclaration()) {
|
|
|
|
return AbortAndDisableOptimization(kFunctionWithIllegalRedeclaration);
|
|
|
|
}
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
// Take --hydrogen-filter into account.
|
2013-08-23 13:30:02 +00:00
|
|
|
if (!info()->closure()->PassesFilter(FLAG_hydrogen_filter)) {
|
2013-12-23 14:30:35 +00:00
|
|
|
return AbortOptimization(kHydrogenFilter);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2012-07-17 16:24:40 +00:00
|
|
|
bool should_recompile = !info()->shared_info()->has_deoptimization_support();
|
2011-03-10 13:26:51 +00:00
|
|
|
if (should_recompile || FLAG_hydrogen_stats) {
|
2013-08-29 09:15:13 +00:00
|
|
|
ElapsedTimer timer;
|
2013-06-24 13:25:44 +00:00
|
|
|
if (FLAG_hydrogen_stats) {
|
2013-08-29 09:15:13 +00:00
|
|
|
timer.Start();
|
2013-06-24 13:25:44 +00:00
|
|
|
}
|
2012-07-17 16:24:40 +00:00
|
|
|
CompilationInfoWithZone unoptimized(info()->shared_info());
|
2010-12-07 11:31:57 +00:00
|
|
|
// Note that we use the same AST that we will use for generating the
|
|
|
|
// optimized code.
|
2012-07-17 16:24:40 +00:00
|
|
|
unoptimized.SetFunction(info()->function());
|
2014-02-10 21:38:17 +00:00
|
|
|
unoptimized.PrepareForCompilation(info()->scope());
|
2012-08-28 11:25:08 +00:00
|
|
|
unoptimized.SetContext(info()->context());
|
2010-12-07 11:31:57 +00:00
|
|
|
if (should_recompile) unoptimized.EnableDeoptimizationSupport();
|
|
|
|
bool succeeded = FullCodeGenerator::MakeCode(&unoptimized);
|
|
|
|
if (should_recompile) {
|
2012-07-17 16:24:40 +00:00
|
|
|
if (!succeeded) return SetLastStatus(FAILED);
|
|
|
|
Handle<SharedFunctionInfo> shared = info()->shared_info();
|
2010-12-07 11:31:57 +00:00
|
|
|
shared->EnableDeoptimizationSupport(*unoptimized.code());
|
|
|
|
// The existing unoptimized code was replaced with the new one.
|
2011-02-22 16:31:24 +00:00
|
|
|
Compiler::RecordFunctionCompilation(
|
|
|
|
Logger::LAZY_COMPILE_TAG, &unoptimized, shared);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
2013-06-24 13:25:44 +00:00
|
|
|
if (FLAG_hydrogen_stats) {
|
2013-08-29 09:15:13 +00:00
|
|
|
isolate()->GetHStatistics()->IncrementFullCodeGen(timer.Elapsed());
|
2013-06-24 13:25:44 +00:00
|
|
|
}
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2013-08-20 08:53:34 +00:00
|
|
|
ASSERT(FLAG_always_opt || info()->shared_info()->code()->optimizable());
|
2012-07-17 16:24:40 +00:00
|
|
|
ASSERT(info()->shared_info()->has_deoptimization_support());
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
if (FLAG_trace_hydrogen) {
|
2013-05-13 11:10:31 +00:00
|
|
|
Handle<String> name = info()->function()->debug_name();
|
2010-12-07 11:31:57 +00:00
|
|
|
PrintF("-----------------------------------------------------------\n");
|
2013-12-09 07:41:20 +00:00
|
|
|
PrintF("Compiling method %s using hydrogen\n", name->ToCString().get());
|
2013-03-06 07:25:46 +00:00
|
|
|
isolate()->GetHTracer()->TraceCompilation(info());
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
2013-05-27 13:59:20 +00:00
|
|
|
|
|
|
|
// Type-check the function.
|
2013-06-12 17:20:37 +00:00
|
|
|
AstTyper::Run(info());
|
2013-05-27 13:59:20 +00:00
|
|
|
|
2014-02-13 16:09:28 +00:00
|
|
|
graph_builder_ = FLAG_hydrogen_track_positions
|
2013-11-29 10:02:32 +00:00
|
|
|
? new(info()->zone()) HOptimizedGraphBuilderWithPositions(info())
|
2013-10-21 13:35:48 +00:00
|
|
|
: new(info()->zone()) HOptimizedGraphBuilder(info());
|
2012-11-19 14:20:57 +00:00
|
|
|
|
|
|
|
Timer t(this, &time_taken_to_create_graph_);
|
2014-01-14 12:04:10 +00:00
|
|
|
info()->set_this_has_uses(false);
|
2012-07-17 16:24:40 +00:00
|
|
|
graph_ = graph_builder_->CreateGraph();
|
|
|
|
|
2013-03-06 07:25:46 +00:00
|
|
|
if (isolate()->has_pending_exception()) {
|
2012-07-17 16:24:40 +00:00
|
|
|
return SetLastStatus(FAILED);
|
2011-02-11 14:26:56 +00:00
|
|
|
}
|
|
|
|
|
2012-07-17 16:24:40 +00:00
|
|
|
// The function being compiled may have bailed out due to an inline
|
|
|
|
// candidate bailing out. In such a case, we don't disable
|
|
|
|
// optimization on the shared_info.
|
|
|
|
ASSERT(!graph_builder_->inline_bailout() || graph_ == NULL);
|
|
|
|
if (graph_ == NULL) {
|
|
|
|
if (graph_builder_->inline_bailout()) {
|
|
|
|
return AbortOptimization();
|
2013-12-23 14:30:35 +00:00
|
|
|
} else {
|
|
|
|
return AbortAndDisableOptimization();
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 14:10:25 +00:00
|
|
|
if (info()->HasAbortedDueToDependencyChange()) {
|
2013-12-23 14:30:35 +00:00
|
|
|
return AbortOptimization(kBailedOutDueToDependencyChange);
|
2013-08-12 14:10:25 +00:00
|
|
|
}
|
|
|
|
|
2012-07-17 16:24:40 +00:00
|
|
|
return SetLastStatus(SUCCEEDED);
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
OptimizedCompileJob::Status OptimizedCompileJob::OptimizeGraph() {
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_allocation;
|
|
|
|
DisallowHandleAllocation no_handles;
|
|
|
|
DisallowHandleDereference no_deref;
|
2013-08-12 14:10:25 +00:00
|
|
|
DisallowCodeDependencyChange no_dependency_change;
|
2012-07-19 18:58:23 +00:00
|
|
|
|
2012-07-17 16:24:40 +00:00
|
|
|
ASSERT(last_status() == SUCCEEDED);
|
|
|
|
Timer t(this, &time_taken_to_optimize_);
|
|
|
|
ASSERT(graph_ != NULL);
|
2013-08-02 09:53:11 +00:00
|
|
|
BailoutReason bailout_reason = kNoReason;
|
2013-12-23 14:30:35 +00:00
|
|
|
|
|
|
|
if (graph_->Optimize(&bailout_reason)) {
|
2012-07-17 16:24:40 +00:00
|
|
|
chunk_ = LChunk::NewChunk(graph_);
|
2013-12-23 14:30:35 +00:00
|
|
|
if (chunk_ != NULL) return SetLastStatus(SUCCEEDED);
|
|
|
|
} else if (bailout_reason != kNoReason) {
|
|
|
|
graph_builder_->Bailout(bailout_reason);
|
2011-05-19 13:15:57 +00:00
|
|
|
}
|
2013-12-23 14:30:35 +00:00
|
|
|
|
|
|
|
return AbortOptimization();
|
2012-07-17 16:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
OptimizedCompileJob::Status OptimizedCompileJob::GenerateCode() {
|
2012-07-17 16:24:40 +00:00
|
|
|
ASSERT(last_status() == SUCCEEDED);
|
2013-08-12 14:10:25 +00:00
|
|
|
ASSERT(!info()->HasAbortedDueToDependencyChange());
|
|
|
|
DisallowCodeDependencyChange no_dependency_change;
|
2012-11-19 14:20:57 +00:00
|
|
|
{ // Scope for timer.
|
|
|
|
Timer timer(this, &time_taken_to_codegen_);
|
|
|
|
ASSERT(chunk_ != NULL);
|
|
|
|
ASSERT(graph_ != NULL);
|
2013-04-23 09:23:07 +00:00
|
|
|
// 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.
|
2013-06-03 16:01:51 +00:00
|
|
|
DisallowDeferredHandleDereference no_deferred_handle_deref;
|
2013-04-18 09:50:46 +00:00
|
|
|
Handle<Code> optimized_code = chunk_->Codegen();
|
2012-11-19 14:20:57 +00:00
|
|
|
if (optimized_code.is_null()) {
|
2013-08-09 15:10:06 +00:00
|
|
|
if (info()->bailout_reason() == kNoReason) {
|
2013-12-23 14:30:35 +00:00
|
|
|
info_->set_bailout_reason(kCodeGenerationFailed);
|
2013-08-02 09:53:11 +00:00
|
|
|
}
|
2013-12-23 14:30:35 +00:00
|
|
|
return AbortAndDisableOptimization();
|
2012-11-19 14:20:57 +00:00
|
|
|
}
|
|
|
|
info()->SetCode(optimized_code);
|
2012-08-28 07:18:06 +00:00
|
|
|
}
|
2012-07-17 16:24:40 +00:00
|
|
|
RecordOptimizationStats();
|
2013-09-04 13:53:24 +00:00
|
|
|
// Add to the weak list of optimized code objects.
|
|
|
|
info()->context()->native_context()->AddOptimizedCode(*info()->code());
|
2012-07-17 16:24:40 +00:00
|
|
|
return SetLastStatus(SUCCEEDED);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
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);
|
2012-07-05 13:11:57 +00:00
|
|
|
}
|
2013-12-23 14:30:35 +00:00
|
|
|
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);
|
2010-07-13 13:06:33 +00:00
|
|
|
}
|
2013-12-23 14:30:35 +00:00
|
|
|
if (FLAG_trace_opt_stats) {
|
|
|
|
static double compilation_time = 0.0;
|
|
|
|
static int compiled_functions = 0;
|
|
|
|
static int code_size = 0;
|
2010-03-05 22:08:58 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
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_);
|
|
|
|
}
|
2013-01-15 10:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-16 11:52:03 +00:00
|
|
|
// Sets the expected number of properties based on estimate from compiler.
|
|
|
|
void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared,
|
|
|
|
int estimate) {
|
|
|
|
// See the comment in SetExpectedNofProperties.
|
|
|
|
if (shared->live_objects_may_exist()) return;
|
|
|
|
|
|
|
|
// If no properties are added in the constructor, they are more likely
|
|
|
|
// to be added later.
|
|
|
|
if (estimate == 0) estimate = 2;
|
|
|
|
|
|
|
|
// 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 (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 {
|
|
|
|
estimate += 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
shared->set_expected_nof_properties(estimate);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
static void UpdateSharedFunctionInfo(CompilationInfo* info) {
|
|
|
|
// 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<SharedFunctionInfo> shared = info->shared_info();
|
|
|
|
Handle<ScopeInfo> scope_info =
|
|
|
|
ScopeInfo::Create(info->scope(), info->zone());
|
|
|
|
shared->set_scope_info(*scope_info);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
Handle<Code> code = info->code();
|
|
|
|
CHECK(code->kind() == Code::FUNCTION);
|
|
|
|
shared->ReplaceCode(*code);
|
|
|
|
if (shared->optimization_disabled()) code->set_optimizable(false);
|
2009-06-08 10:47:49 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
// Set the expected number of properties for instances.
|
|
|
|
FunctionLiteral* lit = info->function();
|
|
|
|
int expected = lit->expected_property_count();
|
|
|
|
SetExpectedNofPropertiesFromEstimate(shared, expected);
|
2009-06-08 10:47:49 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
// Check the function has compiled code.
|
|
|
|
ASSERT(shared->is_compiled());
|
|
|
|
shared->set_dont_optimize_reason(lit->dont_optimize_reason());
|
|
|
|
shared->set_dont_inline(lit->flags()->Contains(kDontInline));
|
|
|
|
shared->set_ast_node_count(lit->ast_node_count());
|
2014-03-11 14:41:22 +00:00
|
|
|
shared->set_strict_mode(lit->strict_mode());
|
2013-12-23 14:30:35 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
|
|
|
|
// Sets the function info on a function.
|
|
|
|
// 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).
|
|
|
|
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);
|
|
|
|
function_info->set_function_token_position(lit->function_token_position());
|
|
|
|
function_info->set_start_position(lit->start_position());
|
|
|
|
function_info->set_end_position(lit->end_position());
|
|
|
|
function_info->set_is_expression(lit->is_expression());
|
|
|
|
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->set_allows_lazy_compilation(lit->AllowsLazyCompilation());
|
|
|
|
function_info->set_allows_lazy_compilation_without_context(
|
|
|
|
lit->AllowsLazyCompilationWithoutContext());
|
2014-03-11 14:41:22 +00:00
|
|
|
function_info->set_strict_mode(lit->strict_mode());
|
2013-12-23 14:30:35 +00:00
|
|
|
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_reason(lit->dont_optimize_reason());
|
|
|
|
function_info->set_dont_inline(lit->flags()->Contains(kDontInline));
|
|
|
|
function_info->set_dont_cache(lit->flags()->Contains(kDontCache));
|
|
|
|
function_info->set_is_generator(lit->is_generator());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool CompileUnoptimizedCode(CompilationInfo* info) {
|
|
|
|
ASSERT(info->function() != NULL);
|
|
|
|
if (!Rewriter::Rewrite(info)) return false;
|
|
|
|
if (!Scope::Analyze(info)) return false;
|
|
|
|
ASSERT(info->scope() != NULL);
|
|
|
|
|
|
|
|
if (!FullCodeGenerator::MakeCode(info)) {
|
|
|
|
Isolate* isolate = info->isolate();
|
|
|
|
if (!isolate->has_pending_exception()) isolate->StackOverflow();
|
|
|
|
return false;
|
2011-11-25 09:36:31 +00:00
|
|
|
}
|
2013-12-23 14:30:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-07-16 07:07:30 +00:00
|
|
|
|
2013-10-28 17:54:43 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
static Handle<Code> GetUnoptimizedCodeCommon(CompilationInfo* info) {
|
|
|
|
VMState<COMPILER> state(info->isolate());
|
|
|
|
PostponeInterruptsScope postpone(info->isolate());
|
|
|
|
if (!Parser::Parse(info)) return Handle<Code>::null();
|
2014-03-11 14:41:22 +00:00
|
|
|
info->SetStrictMode(info->function()->strict_mode());
|
2013-10-28 17:54:43 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
if (!CompileUnoptimizedCode(info)) return Handle<Code>::null();
|
|
|
|
Compiler::RecordFunctionCompilation(
|
|
|
|
Logger::LAZY_COMPILE_TAG, info, info->shared_info());
|
|
|
|
UpdateSharedFunctionInfo(info);
|
|
|
|
ASSERT_EQ(Code::FUNCTION, info->code()->kind());
|
|
|
|
return info->code();
|
|
|
|
}
|
2013-10-28 17:54:43 +00:00
|
|
|
|
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
Handle<Code> Compiler::GetUnoptimizedCode(Handle<JSFunction> function) {
|
|
|
|
ASSERT(!function->GetIsolate()->has_pending_exception());
|
|
|
|
ASSERT(!function->is_compiled());
|
|
|
|
if (function->shared()->is_compiled()) {
|
|
|
|
return Handle<Code>(function->shared()->code());
|
|
|
|
}
|
2013-10-28 17:54:43 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
CompilationInfoWithZone info(function);
|
|
|
|
Handle<Code> result = GetUnoptimizedCodeCommon(&info);
|
|
|
|
ASSERT_EQ(result.is_null(), info.isolate()->has_pending_exception());
|
|
|
|
|
|
|
|
if (FLAG_always_opt &&
|
|
|
|
!result.is_null() &&
|
|
|
|
info.isolate()->use_crankshaft() &&
|
|
|
|
!info.shared_info()->optimization_disabled() &&
|
|
|
|
!info.isolate()->DebuggerHasBreakPoints()) {
|
|
|
|
Handle<Code> opt_code = Compiler::GetOptimizedCode(
|
|
|
|
function, result, Compiler::NOT_CONCURRENT);
|
|
|
|
if (!opt_code.is_null()) result = opt_code;
|
2013-10-28 17:54:43 +00:00
|
|
|
}
|
2012-03-15 11:51:26 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
return result;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-04-28 11:38:43 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
Handle<Code> Compiler::GetUnoptimizedCode(Handle<SharedFunctionInfo> shared) {
|
|
|
|
ASSERT(!shared->GetIsolate()->has_pending_exception());
|
|
|
|
ASSERT(!shared->is_compiled());
|
|
|
|
|
|
|
|
CompilationInfoWithZone info(shared);
|
|
|
|
Handle<Code> result = GetUnoptimizedCodeCommon(&info);
|
|
|
|
ASSERT_EQ(result.is_null(), info.isolate()->has_pending_exception());
|
2010-03-23 06:04:44 +00:00
|
|
|
return result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
bool Compiler::EnsureCompiled(Handle<JSFunction> function,
|
|
|
|
ClearExceptionFlag flag) {
|
|
|
|
if (function->is_compiled()) return true;
|
|
|
|
Handle<Code> code = Compiler::GetUnoptimizedCode(function);
|
|
|
|
if (code.is_null()) {
|
|
|
|
if (flag == CLEAR_EXCEPTION) {
|
|
|
|
function->GetIsolate()->clear_pending_exception();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
function->ReplaceCode(*code);
|
|
|
|
ASSERT(function->is_compiled());
|
|
|
|
return true;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
Handle<Code> Compiler::GetCodeForDebugging(Handle<JSFunction> function) {
|
|
|
|
CompilationInfoWithZone info(function);
|
|
|
|
Isolate* isolate = info.isolate();
|
2013-04-24 14:44:08 +00:00
|
|
|
VMState<COMPILER> state(isolate);
|
2011-03-18 20:35:07 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
ASSERT(!isolate->has_pending_exception());
|
|
|
|
Handle<Code> old_code(function->shared()->code());
|
|
|
|
ASSERT(old_code->kind() == Code::FUNCTION);
|
|
|
|
ASSERT(!old_code->has_debug_break_slots());
|
|
|
|
|
|
|
|
info.MarkCompilingForDebugging();
|
|
|
|
if (old_code->is_compiled_optimizable()) {
|
|
|
|
info.EnableDeoptimizationSupport();
|
|
|
|
} else {
|
|
|
|
info.MarkNonOptimizable();
|
|
|
|
}
|
|
|
|
Handle<Code> new_code = GetUnoptimizedCodeCommon(&info);
|
|
|
|
if (new_code.is_null()) {
|
|
|
|
isolate->clear_pending_exception();
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(old_code->is_compiled_optimizable(),
|
|
|
|
new_code->is_compiled_optimizable());
|
|
|
|
}
|
|
|
|
return new_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
|
|
|
void Compiler::CompileForLiveEdit(Handle<Script> script) {
|
|
|
|
// TODO(635): support extensions.
|
|
|
|
CompilationInfoWithZone info(script);
|
2013-12-23 15:41:44 +00:00
|
|
|
PostponeInterruptsScope postpone(info.isolate());
|
2013-12-23 14:30:35 +00:00
|
|
|
VMState<COMPILER> state(info.isolate());
|
|
|
|
|
|
|
|
info.MarkAsGlobal();
|
|
|
|
if (!Parser::Parse(&info)) return;
|
2014-03-11 14:41:22 +00:00
|
|
|
info.SetStrictMode(info.function()->strict_mode());
|
2013-12-23 14:30:35 +00:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static bool DebuggerWantsEagerCompilation(CompilationInfo* info,
|
|
|
|
bool allow_lazy_without_ctx = false) {
|
|
|
|
return LiveEditFunctionTracker::IsActive(info->isolate()) ||
|
|
|
|
(info->isolate()->DebuggerHasBreakPoints() && !allow_lazy_without_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Handle<SharedFunctionInfo> CompileToplevel(CompilationInfo* info) {
|
|
|
|
Isolate* isolate = info->isolate();
|
2013-12-23 15:41:44 +00:00
|
|
|
PostponeInterruptsScope postpone(isolate);
|
2013-12-23 14:30:35 +00:00
|
|
|
ASSERT(!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));
|
|
|
|
|
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
|
|
|
isolate->debugger()->OnBeforeCompile(script);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ASSERT(info->is_eval() || info->is_global());
|
|
|
|
|
|
|
|
bool parse_allow_lazy =
|
2014-03-19 13:24:13 +00:00
|
|
|
(info->cached_data_mode() == CONSUME_CACHED_DATA ||
|
2013-12-23 14:30:35 +00:00
|
|
|
String::cast(script->source())->length() > FLAG_min_preparse_length) &&
|
|
|
|
!DebuggerWantsEagerCompilation(info);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-03-19 13:24:13 +00:00
|
|
|
if (!parse_allow_lazy && info->cached_data_mode() != NO_CACHED_DATA) {
|
|
|
|
// 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. We cannot use
|
|
|
|
// the existing data, since it won't contain all the symbols we need for
|
|
|
|
// eager parsing. In addition, it doesn't make sense to produce the data
|
|
|
|
// when parsing eagerly. That data would contain all symbols, but no
|
|
|
|
// functions, so it cannot be used to aid lazy parsing later.
|
|
|
|
info->SetCachedData(NULL, NO_CACHED_DATA);
|
2014-02-24 17:48:09 +00:00
|
|
|
}
|
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
Handle<SharedFunctionInfo> result;
|
|
|
|
|
|
|
|
{ VMState<COMPILER> state(info->isolate());
|
|
|
|
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.
|
|
|
|
ASSERT(!info->code().is_null());
|
|
|
|
result = isolate->factory()->NewSharedFunctionInfo(
|
|
|
|
lit->name(),
|
|
|
|
lit->materialized_literal_count(),
|
|
|
|
lit->is_generator(),
|
|
|
|
info->code(),
|
2014-03-17 08:31:21 +00:00
|
|
|
ScopeInfo::Create(info->scope(), info->zone()));
|
2013-12-23 14:30:35 +00:00
|
|
|
|
|
|
|
ASSERT_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());
|
|
|
|
|
|
|
|
script->set_compilation_state(Script::COMPILATION_STATE_COMPILED);
|
|
|
|
|
|
|
|
live_edit_tracker.RecordFunctionInfo(result, lit, info->zone());
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
|
|
|
isolate->debugger()->OnAfterCompile(script, Debugger::NO_AFTER_COMPILE_FLAGS);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSFunction> Compiler::GetFunctionFromEval(Handle<String> source,
|
|
|
|
Handle<Context> context,
|
2014-03-11 14:41:22 +00:00
|
|
|
StrictMode strict_mode,
|
2013-12-23 14:30:35 +00:00
|
|
|
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();
|
|
|
|
Handle<SharedFunctionInfo> shared_info = compilation_cache->LookupEval(
|
2014-03-11 14:41:22 +00:00
|
|
|
source, context, strict_mode, scope_position);
|
2013-12-23 14:30:35 +00:00
|
|
|
|
|
|
|
if (shared_info.is_null()) {
|
|
|
|
Handle<Script> script = isolate->factory()->NewScript(source);
|
|
|
|
CompilationInfoWithZone info(script);
|
|
|
|
info.MarkAsEval();
|
|
|
|
if (context->IsNativeContext()) info.MarkAsGlobal();
|
2014-03-11 14:41:22 +00:00
|
|
|
info.SetStrictMode(strict_mode);
|
2013-12-23 14:30:35 +00:00
|
|
|
info.SetParseRestriction(restriction);
|
|
|
|
info.SetContext(context);
|
|
|
|
|
|
|
|
#if ENABLE_DEBUGGER_SUPPORT
|
|
|
|
Debug::RecordEvalCaller(script);
|
|
|
|
#endif // ENABLE_DEBUGGER_SUPPORT
|
|
|
|
|
|
|
|
shared_info = CompileToplevel(&info);
|
|
|
|
|
|
|
|
if (shared_info.is_null()) {
|
|
|
|
return Handle<JSFunction>::null();
|
|
|
|
} else {
|
|
|
|
// Explicitly disable optimization for eval code. We're not yet prepared
|
|
|
|
// to handle eval-code in the optimizing compiler.
|
|
|
|
shared_info->DisableOptimization(kEval);
|
|
|
|
|
2014-03-11 14:41:22 +00:00
|
|
|
// If caller is strict mode, the result must be in strict mode as well.
|
|
|
|
ASSERT(strict_mode == SLOPPY || shared_info->strict_mode() == STRICT);
|
2013-12-23 14:30:35 +00:00
|
|
|
if (!shared_info->dont_cache()) {
|
|
|
|
compilation_cache->PutEval(
|
|
|
|
source, 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-19 13:24:13 +00:00
|
|
|
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,
|
|
|
|
ScriptDataImpl** cached_data,
|
|
|
|
CachedDataMode cached_data_mode,
|
|
|
|
NativesFlag natives) {
|
|
|
|
if (cached_data_mode == NO_CACHED_DATA) {
|
|
|
|
cached_data = NULL;
|
|
|
|
} else if (cached_data_mode == PRODUCE_CACHED_DATA) {
|
|
|
|
ASSERT(cached_data && !*cached_data);
|
|
|
|
} else {
|
|
|
|
ASSERT(cached_data_mode == CONSUME_CACHED_DATA);
|
|
|
|
ASSERT(cached_data && *cached_data);
|
|
|
|
}
|
2013-12-23 14:30:35 +00:00
|
|
|
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.
|
2010-03-23 06:04:44 +00:00
|
|
|
Handle<SharedFunctionInfo> result;
|
2008-09-11 10:51:52 +00:00
|
|
|
if (extension == NULL) {
|
2011-03-18 20:35:07 +00:00
|
|
|
result = compilation_cache->LookupScript(source,
|
|
|
|
script_name,
|
|
|
|
line_offset,
|
2012-08-28 10:49:23 +00:00
|
|
|
column_offset,
|
2013-07-30 17:05:50 +00:00
|
|
|
is_shared_cross_origin,
|
2012-08-28 10:49:23 +00:00
|
|
|
context);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2008-09-11 10:51:52 +00:00
|
|
|
if (result.is_null()) {
|
2014-04-07 07:40:18 +00:00
|
|
|
// No cache entry found. Compile the script.
|
2008-09-11 10:51:52 +00:00
|
|
|
|
|
|
|
// Create a script object describing the script to be compiled.
|
2013-06-04 10:30:05 +00:00
|
|
|
Handle<Script> script = isolate->factory()->NewScript(source);
|
2010-03-17 08:14:59 +00:00
|
|
|
if (natives == NATIVES_CODE) {
|
|
|
|
script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
|
|
|
|
}
|
2008-09-11 10:51:52 +00:00
|
|
|
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));
|
|
|
|
}
|
2013-07-30 17:05:50 +00:00
|
|
|
script->set_is_shared_cross_origin(is_shared_cross_origin);
|
2008-09-11 10:51:52 +00:00
|
|
|
|
|
|
|
// Compile the function and add it to the cache.
|
2012-06-20 08:58:41 +00:00
|
|
|
CompilationInfoWithZone info(script);
|
2010-10-04 11:35:46 +00:00
|
|
|
info.MarkAsGlobal();
|
|
|
|
info.SetExtension(extension);
|
2014-03-19 13:24:13 +00:00
|
|
|
info.SetCachedData(cached_data, cached_data_mode);
|
2012-08-28 11:25:08 +00:00
|
|
|
info.SetContext(context);
|
2014-03-11 14:41:22 +00:00
|
|
|
if (FLAG_use_strict) info.SetStrictMode(STRICT);
|
2013-12-23 14:30:35 +00:00
|
|
|
result = CompileToplevel(&info);
|
2012-07-09 08:59:03 +00:00
|
|
|
if (extension == NULL && !result.is_null() && !result->dont_cache()) {
|
2012-08-28 10:49:23 +00:00
|
|
|
compilation_cache->PutScript(source, context, result);
|
2008-09-11 10:51:52 +00:00
|
|
|
}
|
2013-12-23 14:30:35 +00:00
|
|
|
} else if (result->ic_age() != isolate->heap()->global_ic_age()) {
|
2013-09-10 14:30:36 +00:00
|
|
|
result->ResetForNewContext(isolate->heap()->global_ic_age());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
if (result.is_null()) isolate->ReportPendingMessages();
|
2008-07-03 15:10:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
Handle<SharedFunctionInfo> Compiler::BuildFunctionInfo(FunctionLiteral* literal,
|
|
|
|
Handle<Script> script) {
|
|
|
|
// Precondition: code has been parsed and scopes have been analyzed.
|
|
|
|
CompilationInfoWithZone info(script);
|
|
|
|
info.SetFunction(literal);
|
2014-02-10 21:38:17 +00:00
|
|
|
info.PrepareForCompilation(literal->scope());
|
2014-03-11 14:41:22 +00:00
|
|
|
info.SetStrictMode(literal->scope()->strict_mode());
|
2009-08-13 10:25:35 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
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);
|
2012-02-14 14:14:51 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
// Generate code
|
|
|
|
Handle<ScopeInfo> scope_info;
|
|
|
|
if (FLAG_lazy && allow_lazy && !literal->is_parenthesized()) {
|
|
|
|
Handle<Code> code = isolate->builtins()->CompileUnoptimized();
|
|
|
|
info.SetCode(code);
|
|
|
|
scope_info = Handle<ScopeInfo>(ScopeInfo::Empty(isolate));
|
|
|
|
} else if (FullCodeGenerator::MakeCode(&info)) {
|
|
|
|
ASSERT(!info.code().is_null());
|
|
|
|
scope_info = ScopeInfo::Create(info.scope(), info.zone());
|
2012-03-26 13:08:08 +00:00
|
|
|
} else {
|
2013-12-23 14:30:35 +00:00
|
|
|
return Handle<SharedFunctionInfo>::null();
|
2008-09-11 10:51:52 +00:00
|
|
|
}
|
2008-12-05 08:35:52 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
// Create a shared function info object.
|
|
|
|
Handle<SharedFunctionInfo> result =
|
|
|
|
factory->NewSharedFunctionInfo(literal->name(),
|
|
|
|
literal->materialized_literal_count(),
|
|
|
|
literal->is_generator(),
|
|
|
|
info.code(),
|
2014-03-17 08:31:21 +00:00
|
|
|
scope_info);
|
2013-12-23 14:30:35 +00:00
|
|
|
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());
|
2008-09-11 10:51:52 +00:00
|
|
|
return result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-23 14:42:42 +00:00
|
|
|
static Handle<Code> GetCodeFromOptimizedCodeMap(Handle<JSFunction> function,
|
|
|
|
BailoutId osr_ast_id) {
|
2013-12-23 14:30:35 +00:00
|
|
|
if (FLAG_cache_optimized_code) {
|
|
|
|
Handle<SharedFunctionInfo> shared(function->shared());
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
int index = shared->SearchOptimizedCodeMap(
|
2013-12-23 14:42:42 +00:00
|
|
|
function->context()->native_context(), osr_ast_id);
|
2013-12-23 14:30:35 +00:00
|
|
|
if (index > 0) {
|
|
|
|
if (FLAG_trace_opt) {
|
|
|
|
PrintF("[found optimized code for ");
|
|
|
|
function->ShortPrint();
|
2013-12-23 14:42:42 +00:00
|
|
|
if (!osr_ast_id.IsNone()) {
|
|
|
|
PrintF(" at OSR AST id %d", osr_ast_id.ToInt());
|
|
|
|
}
|
2013-12-23 14:30:35 +00:00
|
|
|
PrintF("]\n");
|
|
|
|
}
|
|
|
|
FixedArray* literals = shared->GetLiteralsFromOptimizedCodeMap(index);
|
|
|
|
if (literals != NULL) function->set_literals(literals);
|
2014-03-03 11:11:39 +00:00
|
|
|
return Handle<Code>(shared->GetCodeFromOptimizedCodeMap(index));
|
2012-07-19 18:58:23 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-23 14:30:35 +00:00
|
|
|
return Handle<Code>::null();
|
2012-07-19 18:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void InsertCodeIntoOptimizedCodeMap(CompilationInfo* info) {
|
|
|
|
Handle<Code> code = info->code();
|
2013-09-04 13:53:24 +00:00
|
|
|
if (code->kind() != Code::OPTIMIZED_FUNCTION) return; // Nothing to do.
|
|
|
|
|
2013-12-23 14:42:42 +00:00
|
|
|
// Cache optimized code.
|
|
|
|
if (FLAG_cache_optimized_code) {
|
2012-09-14 10:41:31 +00:00
|
|
|
Handle<JSFunction> function = info->closure();
|
2012-07-19 18:58:23 +00:00
|
|
|
Handle<SharedFunctionInfo> shared(function->shared());
|
|
|
|
Handle<FixedArray> literals(function->literals());
|
2012-08-17 09:03:08 +00:00
|
|
|
Handle<Context> native_context(function->context()->native_context());
|
2012-07-19 18:58:23 +00:00
|
|
|
SharedFunctionInfo::AddToOptimizedCodeMap(
|
2013-12-23 14:42:42 +00:00
|
|
|
shared, native_context, code, literals, info->osr_ast_id());
|
2012-07-19 18:58:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
static bool CompileOptimizedPrologue(CompilationInfo* info) {
|
|
|
|
if (!Parser::Parse(info)) return false;
|
2014-03-11 14:41:22 +00:00
|
|
|
info->SetStrictMode(info->function()->strict_mode());
|
2013-09-04 13:53:24 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
if (!Rewriter::Rewrite(info)) return false;
|
|
|
|
if (!Scope::Analyze(info)) return false;
|
|
|
|
ASSERT(info->scope() != NULL);
|
|
|
|
return true;
|
2012-07-19 18:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
static bool GetOptimizedCodeNow(CompilationInfo* info) {
|
|
|
|
if (!CompileOptimizedPrologue(info)) return false;
|
2011-04-08 14:30:10 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
Logger::TimerEventScope timer(
|
|
|
|
info->isolate(), Logger::TimerEventScope::v8_recompile_synchronous);
|
|
|
|
|
|
|
|
OptimizedCompileJob job(info);
|
|
|
|
if (job.CreateGraph() != OptimizedCompileJob::SUCCEEDED) return false;
|
|
|
|
if (job.OptimizeGraph() != OptimizedCompileJob::SUCCEEDED) return false;
|
|
|
|
if (job.GenerateCode() != OptimizedCompileJob::SUCCEEDED) return false;
|
|
|
|
|
|
|
|
// Success!
|
|
|
|
ASSERT(!info->isolate()->has_pending_exception());
|
|
|
|
InsertCodeIntoOptimizedCodeMap(info);
|
|
|
|
Compiler::RecordFunctionCompilation(
|
|
|
|
Logger::LAZY_COMPILE_TAG, info, info->shared_info());
|
|
|
|
return true;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
static bool GetOptimizedCodeLater(CompilationInfo* info) {
|
|
|
|
Isolate* isolate = info->isolate();
|
2012-07-19 18:58:23 +00:00
|
|
|
if (!isolate->optimizing_compiler_thread()->IsQueueAvailable()) {
|
2013-08-22 16:14:37 +00:00
|
|
|
if (FLAG_trace_concurrent_recompilation) {
|
2013-08-07 09:33:09 +00:00
|
|
|
PrintF(" ** Compilation queue full, will retry optimizing ");
|
2013-12-23 14:30:35 +00:00
|
|
|
info->closure()->PrintName();
|
|
|
|
PrintF(" later.\n");
|
2012-07-19 18:58:23 +00:00
|
|
|
}
|
2013-09-04 12:55:59 +00:00
|
|
|
return false;
|
2012-07-19 18:58:23 +00:00
|
|
|
}
|
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
CompilationHandleScope handle_scope(info);
|
|
|
|
if (!CompileOptimizedPrologue(info)) return false;
|
|
|
|
info->SaveHandles(); // Copy handles to the compilation handle scope.
|
|
|
|
|
|
|
|
Logger::TimerEventScope timer(
|
|
|
|
isolate, Logger::TimerEventScope::v8_recompile_synchronous);
|
2013-09-04 12:55:59 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
OptimizedCompileJob* job = new(info->zone()) OptimizedCompileJob(info);
|
|
|
|
OptimizedCompileJob::Status status = job->CreateGraph();
|
|
|
|
if (status != OptimizedCompileJob::SUCCEEDED) return false;
|
|
|
|
isolate->optimizing_compiler_thread()->QueueForOptimization(job);
|
2013-09-04 12:55:59 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
if (FLAG_trace_concurrent_recompilation) {
|
|
|
|
PrintF(" ** Queued ");
|
|
|
|
info->closure()->PrintName();
|
|
|
|
if (info->is_osr()) {
|
|
|
|
PrintF(" for concurrent OSR at %d.\n", info->osr_ast_id().ToInt());
|
|
|
|
} else {
|
|
|
|
PrintF(" for concurrent optimization.\n");
|
2013-09-04 12:55:59 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-23 14:30:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-04 12:55:59 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
Handle<Code> Compiler::GetOptimizedCode(Handle<JSFunction> function,
|
|
|
|
Handle<Code> current_code,
|
|
|
|
ConcurrencyMode mode,
|
|
|
|
BailoutId osr_ast_id) {
|
2013-12-23 14:42:42 +00:00
|
|
|
Handle<Code> cached_code = GetCodeFromOptimizedCodeMap(function, osr_ast_id);
|
|
|
|
if (!cached_code.is_null()) return cached_code;
|
2013-12-23 14:30:35 +00:00
|
|
|
|
|
|
|
SmartPointer<CompilationInfo> info(new CompilationInfoWithZone(function));
|
|
|
|
Isolate* isolate = info->isolate();
|
2013-04-24 14:44:08 +00:00
|
|
|
VMState<COMPILER> state(isolate);
|
2013-12-23 14:30:35 +00:00
|
|
|
ASSERT(!isolate->has_pending_exception());
|
2012-07-19 18:58:23 +00:00
|
|
|
PostponeInterruptsScope postpone(isolate);
|
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
Handle<SharedFunctionInfo> shared = info->shared_info();
|
|
|
|
ASSERT_NE(ScopeInfo::Empty(isolate), shared->scope_info());
|
2012-07-19 18:58:23 +00:00
|
|
|
int compiled_size = shared->end_position() - shared->start_position();
|
|
|
|
isolate->counters()->total_compile_size()->Increment(compiled_size);
|
2013-12-23 14:30:35 +00:00
|
|
|
current_code->set_profiler_ticks(0);
|
2012-07-19 18:58:23 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
info->SetOptimizing(osr_ast_id, current_code);
|
2012-07-19 18:58:23 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
if (mode == CONCURRENT) {
|
|
|
|
if (GetOptimizedCodeLater(info.get())) {
|
|
|
|
info.Detach(); // The background recompile job owns this now.
|
|
|
|
return isolate->builtins()->InOptimizationQueue();
|
2012-11-16 14:24:19 +00:00
|
|
|
}
|
2013-12-23 14:30:35 +00:00
|
|
|
} else {
|
|
|
|
if (GetOptimizedCodeNow(info.get())) return info->code();
|
|
|
|
}
|
2012-07-19 18:58:23 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
// Failed.
|
|
|
|
if (FLAG_trace_opt) {
|
|
|
|
PrintF("[failed to optimize ");
|
|
|
|
function->PrintName();
|
2014-02-12 18:48:12 +00:00
|
|
|
PrintF(": %s]\n", GetBailoutReason(info->bailout_reason()));
|
2012-07-19 18:58:23 +00:00
|
|
|
}
|
|
|
|
|
2013-03-12 18:03:18 +00:00
|
|
|
if (isolate->has_pending_exception()) isolate->clear_pending_exception();
|
2013-12-23 14:30:35 +00:00
|
|
|
return Handle<Code>::null();
|
2012-07-19 18:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
Handle<Code> Compiler::GetConcurrentlyOptimizedCode(OptimizedCompileJob* job) {
|
|
|
|
// Take ownership of compilation info. Deleting compilation info
|
|
|
|
// also tears down the zone and the recompile job.
|
2013-09-25 10:01:18 +00:00
|
|
|
SmartPointer<CompilationInfo> info(job->info());
|
2012-11-22 13:04:11 +00:00
|
|
|
Isolate* isolate = info->isolate();
|
2013-12-23 14:30:35 +00:00
|
|
|
|
2013-04-24 14:44:08 +00:00
|
|
|
VMState<COMPILER> state(isolate);
|
2012-11-22 13:04:11 +00:00
|
|
|
Logger::TimerEventScope timer(
|
2012-11-26 08:56:59 +00:00
|
|
|
isolate, Logger::TimerEventScope::v8_recompile_synchronous);
|
2012-07-19 18:58:23 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
Handle<SharedFunctionInfo> shared = info->shared_info();
|
|
|
|
shared->code()->set_profiler_ticks(0);
|
|
|
|
|
|
|
|
// 1) Optimization 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.
|
|
|
|
|
|
|
|
if (job->last_status() != OptimizedCompileJob::SUCCEEDED ||
|
|
|
|
shared->optimization_disabled() ||
|
|
|
|
info->HasAbortedDueToDependencyChange() ||
|
|
|
|
isolate->DebuggerHasBreakPoints()) {
|
|
|
|
return Handle<Code>::null();
|
2012-07-19 18:58:23 +00:00
|
|
|
}
|
2013-09-04 12:55:59 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
if (job->GenerateCode() != OptimizedCompileJob::SUCCEEDED) {
|
|
|
|
return Handle<Code>::null();
|
2009-11-04 17:59:24 +00:00
|
|
|
}
|
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
Compiler::RecordFunctionCompilation(
|
|
|
|
Logger::LAZY_COMPILE_TAG, info.get(), shared);
|
|
|
|
if (info->shared_info()->SearchOptimizedCodeMap(
|
2013-12-23 14:42:42 +00:00
|
|
|
info->context()->native_context(), info->osr_ast_id()) == -1) {
|
2013-12-23 14:30:35 +00:00
|
|
|
InsertCodeIntoOptimizedCodeMap(info.get());
|
|
|
|
}
|
2009-11-04 17:59:24 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
if (FLAG_trace_concurrent_recompilation) {
|
|
|
|
PrintF(" ** Optimized code for ");
|
|
|
|
info->closure()->PrintName();
|
|
|
|
PrintF(" generated.\n");
|
|
|
|
}
|
2009-11-04 17:59:24 +00:00
|
|
|
|
2013-12-23 14:30:35 +00:00
|
|
|
return Handle<Code>(*info->code());
|
2009-11-04 17:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-25 14:55:53 +00:00
|
|
|
void Compiler::RecordFunctionCompilation(Logger::LogEventsAndTags tag,
|
2011-02-22 16:31:24 +00:00
|
|
|
CompilationInfo* info,
|
|
|
|
Handle<SharedFunctionInfo> shared) {
|
|
|
|
// SharedFunctionInfo is passed separately, because if CompilationInfo
|
|
|
|
// was created using Script object, it will not have it.
|
|
|
|
|
2010-10-04 14:30:43 +00:00
|
|
|
// 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.
|
2012-08-28 14:43:28 +00:00
|
|
|
if (info->isolate()->logger()->is_logging_code_events() ||
|
2013-04-02 07:53:50 +00:00
|
|
|
info->isolate()->cpu_profiler()->is_profiling()) {
|
2010-10-04 14:30:43 +00:00
|
|
|
Handle<Script> script = info->script();
|
|
|
|
Handle<Code> code = info->code();
|
2013-12-23 14:30:35 +00:00
|
|
|
if (code.is_identical_to(info->isolate()->builtins()->CompileUnoptimized()))
|
2011-03-18 20:35:07 +00:00
|
|
|
return;
|
2013-07-07 11:42:30 +00:00
|
|
|
int line_num = GetScriptLineNumber(script, shared->start_position()) + 1;
|
2013-08-23 17:20:54 +00:00
|
|
|
int column_num =
|
|
|
|
GetScriptColumnNumber(script, shared->start_position()) + 1;
|
2013-07-07 11:42:30 +00:00
|
|
|
USE(line_num);
|
2013-12-23 14:30:35 +00:00
|
|
|
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));
|
2010-02-04 15:35:42 +00:00
|
|
|
}
|
2011-01-18 16:11:01 +00:00
|
|
|
|
2011-04-08 11:25:19 +00:00
|
|
|
GDBJIT(AddCode(Handle<String>(shared->DebugName()),
|
2011-01-18 16:11:01 +00:00
|
|
|
Handle<Script>(info->script()),
|
2011-06-30 11:52:00 +00:00
|
|
|
Handle<Code>(info->code()),
|
|
|
|
info));
|
2010-02-04 15:35:42 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 12:22:26 +00:00
|
|
|
|
2013-06-27 13:03:01 +00:00
|
|
|
CompilationPhase::CompilationPhase(const char* name, CompilationInfo* info)
|
|
|
|
: name_(name), info_(info), zone_(info->isolate()) {
|
2013-06-25 12:22:26 +00:00
|
|
|
if (FLAG_hydrogen_stats) {
|
2013-06-27 13:03:01 +00:00
|
|
|
info_zone_start_allocation_size_ = info->zone()->allocation_size();
|
2013-08-29 09:15:13 +00:00
|
|
|
timer_.Start();
|
2013-06-25 12:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CompilationPhase::~CompilationPhase() {
|
|
|
|
if (FLAG_hydrogen_stats) {
|
2013-06-27 13:03:01 +00:00
|
|
|
unsigned size = zone()->allocation_size();
|
|
|
|
size += info_->zone()->allocation_size() - info_zone_start_allocation_size_;
|
2013-08-29 09:15:13 +00:00
|
|
|
isolate()->GetHStatistics()->SaveTiming(name_, timer_.Elapsed(), size);
|
2013-06-25 12:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CompilationPhase::ShouldProduceTraceOutput() const {
|
2013-07-24 12:38:52 +00:00
|
|
|
// Trace if the appropriate trace flag is set and the phase name's first
|
|
|
|
// character is in the FLAG_trace_phase command line parameter.
|
2013-08-27 08:49:27 +00:00
|
|
|
AllowHandleDereference allow_deref;
|
2013-08-23 13:30:02 +00:00
|
|
|
bool tracing_on = info()->IsStub()
|
|
|
|
? FLAG_trace_hydrogen_stubs
|
|
|
|
: (FLAG_trace_hydrogen &&
|
|
|
|
info()->closure()->PassesFilter(FLAG_trace_hydrogen_filter));
|
2013-07-24 12:38:52 +00:00
|
|
|
return (tracing_on &&
|
|
|
|
OS::StrChr(const_cast<char*>(FLAG_trace_phase), name_[0]) != NULL);
|
2013-06-25 12:22:26 +00:00
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|