2013-10-02 11:43:41 +00:00
|
|
|
// Copyright 2013 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2013-10-02 11:43:41 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/lithium-codegen.h"
|
2013-10-02 11:43:41 +00:00
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "src/v8.h"
|
|
|
|
|
2013-10-02 11:43:41 +00:00
|
|
|
#if V8_TARGET_ARCH_IA32
|
2014-06-20 08:40:11 +00:00
|
|
|
#include "src/ia32/lithium-ia32.h" // NOLINT
|
|
|
|
#include "src/ia32/lithium-codegen-ia32.h" // NOLINT
|
2013-10-02 11:43:41 +00:00
|
|
|
#elif V8_TARGET_ARCH_X64
|
2014-06-20 08:40:11 +00:00
|
|
|
#include "src/x64/lithium-x64.h" // NOLINT
|
|
|
|
#include "src/x64/lithium-codegen-x64.h" // NOLINT
|
2013-10-02 11:43:41 +00:00
|
|
|
#elif V8_TARGET_ARCH_ARM
|
2014-06-20 08:40:11 +00:00
|
|
|
#include "src/arm/lithium-arm.h" // NOLINT
|
|
|
|
#include "src/arm/lithium-codegen-arm.h" // NOLINT
|
2014-03-21 09:28:26 +00:00
|
|
|
#elif V8_TARGET_ARCH_ARM64
|
2014-06-20 08:40:11 +00:00
|
|
|
#include "src/arm64/lithium-arm64.h" // NOLINT
|
|
|
|
#include "src/arm64/lithium-codegen-arm64.h" // NOLINT
|
2013-10-02 11:43:41 +00:00
|
|
|
#elif V8_TARGET_ARCH_MIPS
|
2014-06-20 08:40:11 +00:00
|
|
|
#include "src/mips/lithium-mips.h" // NOLINT
|
|
|
|
#include "src/mips/lithium-codegen-mips.h" // NOLINT
|
2014-07-09 11:08:26 +00:00
|
|
|
#elif V8_TARGET_ARCH_MIPS64
|
|
|
|
#include "src/mips64/lithium-mips64.h" // NOLINT
|
|
|
|
#include "src/mips64/lithium-codegen-mips64.h" // NOLINT
|
2014-05-23 16:37:27 +00:00
|
|
|
#elif V8_TARGET_ARCH_X87
|
2014-06-20 08:40:11 +00:00
|
|
|
#include "src/x87/lithium-x87.h" // NOLINT
|
|
|
|
#include "src/x87/lithium-codegen-x87.h" // NOLINT
|
2015-01-16 07:42:00 +00:00
|
|
|
#elif V8_TARGET_ARCH_PPC
|
|
|
|
#include "src/ppc/lithium-ppc.h" // NOLINT
|
|
|
|
#include "src/ppc/lithium-codegen-ppc.h" // NOLINT
|
2013-10-02 11:43:41 +00:00
|
|
|
#else
|
|
|
|
#error Unsupported target architecture.
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
|
|
HGraph* LCodeGenBase::graph() const {
|
|
|
|
return chunk()->graph();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LCodeGenBase::LCodeGenBase(LChunk* chunk,
|
|
|
|
MacroAssembler* assembler,
|
|
|
|
CompilationInfo* info)
|
|
|
|
: chunk_(static_cast<LPlatformChunk*>(chunk)),
|
|
|
|
masm_(assembler),
|
|
|
|
info_(info),
|
|
|
|
zone_(info->zone()),
|
|
|
|
status_(UNUSED),
|
|
|
|
current_block_(-1),
|
|
|
|
current_instruction_(-1),
|
|
|
|
instructions_(chunk->instructions()),
|
|
|
|
last_lazy_deopt_pc_(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LCodeGenBase::GenerateBody() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(is_generating());
|
2013-10-02 11:43:41 +00:00
|
|
|
bool emit_instructions = true;
|
|
|
|
LCodeGen* codegen = static_cast<LCodeGen*>(this);
|
|
|
|
for (current_instruction_ = 0;
|
|
|
|
!is_aborted() && current_instruction_ < instructions_->length();
|
|
|
|
current_instruction_++) {
|
|
|
|
LInstruction* instr = instructions_->at(current_instruction_);
|
|
|
|
|
|
|
|
// Don't emit code for basic blocks with a replacement.
|
|
|
|
if (instr->IsLabel()) {
|
|
|
|
emit_instructions = !LLabel::cast(instr)->HasReplacement() &&
|
|
|
|
(!FLAG_unreachable_code_elimination ||
|
|
|
|
instr->hydrogen_value()->block()->IsReachable());
|
|
|
|
if (FLAG_code_comments && !emit_instructions) {
|
|
|
|
Comment(
|
|
|
|
";;; <@%d,#%d> -------------------- B%d (unreachable/replaced) "
|
|
|
|
"--------------------",
|
|
|
|
current_instruction_,
|
|
|
|
instr->hydrogen_value()->id(),
|
|
|
|
instr->hydrogen_value()->block()->block_id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!emit_instructions) continue;
|
|
|
|
|
|
|
|
if (FLAG_code_comments && instr->HasInterestingComment(codegen)) {
|
|
|
|
Comment(";;; <@%d,#%d> %s",
|
|
|
|
current_instruction_,
|
|
|
|
instr->hydrogen_value()->id(),
|
|
|
|
instr->Mnemonic());
|
|
|
|
}
|
|
|
|
|
|
|
|
GenerateBodyInstructionPre(instr);
|
|
|
|
|
2013-10-21 13:35:48 +00:00
|
|
|
HValue* value = instr->hydrogen_value();
|
2014-02-13 16:09:28 +00:00
|
|
|
if (!value->position().IsUnknown()) {
|
|
|
|
RecordAndWritePosition(
|
|
|
|
chunk()->graph()->SourcePositionToScriptPosition(value->position()));
|
2013-10-21 13:35:48 +00:00
|
|
|
}
|
2013-10-02 11:43:41 +00:00
|
|
|
|
|
|
|
instr->CompileToNative(codegen);
|
|
|
|
|
|
|
|
GenerateBodyInstructionPost(instr);
|
|
|
|
}
|
|
|
|
EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
|
|
|
|
last_lazy_deopt_pc_ = masm()->pc_offset();
|
|
|
|
return !is_aborted();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-02 11:30:13 +00:00
|
|
|
void LCodeGenBase::CheckEnvironmentUsage() {
|
|
|
|
#ifdef DEBUG
|
2014-04-03 10:39:04 +00:00
|
|
|
bool dead_block = false;
|
2014-04-02 11:30:13 +00:00
|
|
|
for (int i = 0; i < instructions_->length(); i++) {
|
|
|
|
LInstruction* instr = instructions_->at(i);
|
2014-04-03 10:39:04 +00:00
|
|
|
HValue* hval = instr->hydrogen_value();
|
|
|
|
if (instr->IsLabel()) dead_block = LLabel::cast(instr)->HasReplacement();
|
|
|
|
if (dead_block || !hval->block()->IsReachable()) continue;
|
|
|
|
|
|
|
|
HInstruction* hinstr = HInstruction::cast(hval);
|
|
|
|
if (!hinstr->CanDeoptimize() && instr->HasEnvironment()) {
|
2014-08-04 08:47:15 +00:00
|
|
|
V8_Fatal(__FILE__, __LINE__, "CanDeoptimize is wrong for %s (%s)",
|
2014-04-03 10:39:04 +00:00
|
|
|
hinstr->Mnemonic(), instr->Mnemonic());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instr->HasEnvironment() && !instr->environment()->has_been_used()) {
|
2014-08-04 08:47:15 +00:00
|
|
|
V8_Fatal(__FILE__, __LINE__, "unused environment for %s (%s)",
|
2014-04-03 10:39:04 +00:00
|
|
|
hinstr->Mnemonic(), instr->Mnemonic());
|
2014-04-02 11:30:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-02 11:43:41 +00:00
|
|
|
void LCodeGenBase::Comment(const char* format, ...) {
|
|
|
|
if (!FLAG_code_comments) return;
|
|
|
|
char buffer[4 * KB];
|
2014-08-26 09:19:24 +00:00
|
|
|
StringBuilder builder(buffer, arraysize(buffer));
|
2013-10-02 11:43:41 +00:00
|
|
|
va_list arguments;
|
|
|
|
va_start(arguments, format);
|
|
|
|
builder.AddFormattedList(format, arguments);
|
|
|
|
va_end(arguments);
|
|
|
|
|
|
|
|
// Copy the string before recording it in the assembler to avoid
|
|
|
|
// issues when the stack allocated buffer goes out of scope.
|
|
|
|
size_t length = builder.position();
|
2013-10-02 15:27:51 +00:00
|
|
|
Vector<char> copy = Vector<char>::New(static_cast<int>(length) + 1);
|
2014-05-27 07:57:22 +00:00
|
|
|
MemCopy(copy.start(), builder.Finalize(), copy.length());
|
2013-10-02 11:43:41 +00:00
|
|
|
masm()->RecordComment(copy.start());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-10 14:32:42 +00:00
|
|
|
void LCodeGenBase::DeoptComment(const Deoptimizer::DeoptInfo& deopt_info) {
|
2015-02-27 13:34:23 +00:00
|
|
|
masm()->RecordDeoptReason(deopt_info.deopt_reason, deopt_info.position);
|
2014-09-19 06:27:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-02 11:43:41 +00:00
|
|
|
int LCodeGenBase::GetNextEmittedBlock() const {
|
|
|
|
for (int i = current_block_ + 1; i < graph()->blocks()->length(); ++i) {
|
2014-03-10 07:42:09 +00:00
|
|
|
if (!graph()->blocks()->at(i)->IsReachable()) continue;
|
2013-10-02 11:43:41 +00:00
|
|
|
if (!chunk_->GetLabel(i)->HasReplacement()) return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-02 08:08:23 +00:00
|
|
|
void LCodeGenBase::Abort(BailoutReason reason) {
|
2014-09-24 07:08:27 +00:00
|
|
|
info()->AbortOptimization(reason);
|
|
|
|
status_ = ABORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LCodeGenBase::Retry(BailoutReason reason) {
|
|
|
|
info()->RetryOptimization(reason);
|
2014-05-02 08:08:23 +00:00
|
|
|
status_ = ABORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-20 13:03:25 +00:00
|
|
|
void LCodeGenBase::AddDeprecationDependency(Handle<Map> map) {
|
2014-09-24 07:08:27 +00:00
|
|
|
if (map->is_deprecated()) return Retry(kMapBecameDeprecated);
|
2014-05-20 13:03:25 +00:00
|
|
|
chunk_->AddDeprecationDependency(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-05 11:03:14 +00:00
|
|
|
void LCodeGenBase::AddStabilityDependency(Handle<Map> map) {
|
2014-09-24 07:08:27 +00:00
|
|
|
if (!map->is_stable()) return Retry(kMapBecameUnstable);
|
2014-05-05 11:03:14 +00:00
|
|
|
chunk_->AddStabilityDependency(map);
|
|
|
|
}
|
|
|
|
|
2015-03-17 09:37:26 +00:00
|
|
|
|
|
|
|
Deoptimizer::DeoptInfo LCodeGenBase::MakeDeoptInfo(
|
|
|
|
LInstruction* instr, Deoptimizer::DeoptReason deopt_reason) {
|
|
|
|
Deoptimizer::DeoptInfo deopt_info(instr->hydrogen_value()->position(),
|
|
|
|
instr->Mnemonic(), deopt_reason);
|
|
|
|
HEnterInlined* enter_inlined = instr->environment()->entry();
|
|
|
|
deopt_info.inlining_id = enter_inlined ? enter_inlined->inlining_id() : 0;
|
|
|
|
return deopt_info;
|
|
|
|
}
|
2013-10-02 11:43:41 +00:00
|
|
|
} } // namespace v8::internal
|