2013-10-02 11:43:41 +00:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "lithium-codegen.h"
|
|
|
|
|
|
|
|
#if V8_TARGET_ARCH_IA32
|
|
|
|
#include "ia32/lithium-ia32.h"
|
|
|
|
#include "ia32/lithium-codegen-ia32.h"
|
|
|
|
#elif V8_TARGET_ARCH_X64
|
|
|
|
#include "x64/lithium-x64.h"
|
|
|
|
#include "x64/lithium-codegen-x64.h"
|
|
|
|
#elif V8_TARGET_ARCH_ARM
|
|
|
|
#include "arm/lithium-arm.h"
|
|
|
|
#include "arm/lithium-codegen-arm.h"
|
2014-03-21 09:28:26 +00:00
|
|
|
#elif V8_TARGET_ARCH_ARM64
|
|
|
|
#include "arm64/lithium-arm64.h"
|
|
|
|
#include "arm64/lithium-codegen-arm64.h"
|
2013-10-02 11:43:41 +00:00
|
|
|
#elif V8_TARGET_ARCH_MIPS
|
2013-10-02 16:58:37 +00:00
|
|
|
#include "mips/lithium-mips.h"
|
2013-10-02 11:43:41 +00:00
|
|
|
#include "mips/lithium-codegen-mips.h"
|
|
|
|
#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() {
|
|
|
|
ASSERT(is_generating());
|
|
|
|
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()) {
|
|
|
|
V8_Fatal(__FILE__, __LINE__, "CanDeoptimize is wrong for %s (%s)\n",
|
|
|
|
hinstr->Mnemonic(), instr->Mnemonic());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instr->HasEnvironment() && !instr->environment()->has_been_used()) {
|
|
|
|
V8_Fatal(__FILE__, __LINE__, "unused environment for %s (%s)\n",
|
|
|
|
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];
|
|
|
|
StringBuilder builder(buffer, ARRAY_SIZE(buffer));
|
|
|
|
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);
|
2013-10-02 11:43:41 +00:00
|
|
|
OS::MemCopy(copy.start(), builder.Finalize(), copy.length());
|
|
|
|
masm()->RecordComment(copy.start());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-04-16 13:28:11 +00:00
|
|
|
static void AddWeakObjectToCodeDependency(Isolate* isolate,
|
|
|
|
Handle<Object> object,
|
|
|
|
Handle<Code> code) {
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
heap->EnsureWeakObjectToCodeTable();
|
|
|
|
Handle<DependentCode> dep(heap->LookupWeakObjectToCodeDependency(*object));
|
|
|
|
dep = DependentCode::Insert(dep, DependentCode::kWeakCodeGroup, code);
|
2014-04-25 08:10:44 +00:00
|
|
|
heap->AddWeakObjectToCodeDependency(object, dep);
|
2014-04-16 13:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-19 14:03:48 +00:00
|
|
|
void LCodeGenBase::RegisterWeakObjectsInOptimizedCode(Handle<Code> code) {
|
|
|
|
ASSERT(code->is_optimized_code());
|
2013-12-03 12:32:35 +00:00
|
|
|
ZoneList<Handle<Map> > maps(1, zone());
|
|
|
|
ZoneList<Handle<JSObject> > objects(1, zone());
|
2014-01-15 11:42:19 +00:00
|
|
|
ZoneList<Handle<Cell> > cells(1, zone());
|
|
|
|
int mode_mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
|
|
|
|
RelocInfo::ModeMask(RelocInfo::CELL);
|
2013-12-03 12:32:35 +00:00
|
|
|
for (RelocIterator it(*code, mode_mask); !it.done(); it.next()) {
|
2014-01-15 11:42:19 +00:00
|
|
|
RelocInfo::Mode mode = it.rinfo()->rmode();
|
|
|
|
if (mode == RelocInfo::CELL &&
|
2014-02-19 14:03:48 +00:00
|
|
|
code->IsWeakObjectInOptimizedCode(it.rinfo()->target_cell())) {
|
2014-01-15 11:42:19 +00:00
|
|
|
Handle<Cell> cell(it.rinfo()->target_cell());
|
|
|
|
cells.Add(cell, zone());
|
|
|
|
} else if (mode == RelocInfo::EMBEDDED_OBJECT &&
|
2014-02-19 14:03:48 +00:00
|
|
|
code->IsWeakObjectInOptimizedCode(it.rinfo()->target_object())) {
|
2013-12-03 12:32:35 +00:00
|
|
|
if (it.rinfo()->target_object()->IsMap()) {
|
|
|
|
Handle<Map> map(Map::cast(it.rinfo()->target_object()));
|
|
|
|
maps.Add(map, zone());
|
|
|
|
} else if (it.rinfo()->target_object()->IsJSObject()) {
|
|
|
|
Handle<JSObject> object(JSObject::cast(it.rinfo()->target_object()));
|
|
|
|
objects.Add(object, zone());
|
2014-01-15 11:42:19 +00:00
|
|
|
} else if (it.rinfo()->target_object()->IsCell()) {
|
|
|
|
Handle<Cell> cell(Cell::cast(it.rinfo()->target_object()));
|
|
|
|
cells.Add(cell, zone());
|
2013-12-03 12:32:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-08 10:00:57 +00:00
|
|
|
if (FLAG_enable_ool_constant_pool) {
|
|
|
|
code->constant_pool()->set_weak_object_state(
|
|
|
|
ConstantPoolArray::WEAK_OBJECTS_IN_OPTIMIZED_CODE);
|
|
|
|
}
|
2013-12-03 12:32:35 +00:00
|
|
|
#ifdef VERIFY_HEAP
|
|
|
|
// This disables verification of weak embedded objects after full GC.
|
|
|
|
// AddDependentCode can cause a GC, which would observe the state where
|
|
|
|
// this code is not yet in the depended code lists of the embedded maps.
|
|
|
|
NoWeakObjectVerificationScope disable_verification_of_embedded_objects;
|
|
|
|
#endif
|
|
|
|
for (int i = 0; i < maps.length(); i++) {
|
2014-04-15 10:14:50 +00:00
|
|
|
Map::AddDependentCode(maps.at(i), DependentCode::kWeakCodeGroup, code);
|
2013-12-03 12:32:35 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < objects.length(); i++) {
|
2014-04-16 13:28:11 +00:00
|
|
|
AddWeakObjectToCodeDependency(isolate(), objects.at(i), code);
|
2013-12-03 12:32:35 +00:00
|
|
|
}
|
2014-01-15 11:42:19 +00:00
|
|
|
for (int i = 0; i < cells.length(); i++) {
|
2014-04-16 13:28:11 +00:00
|
|
|
AddWeakObjectToCodeDependency(isolate(), cells.at(i), code);
|
2014-01-15 11:42:19 +00:00
|
|
|
}
|
2013-12-03 12:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-02 11:43:41 +00:00
|
|
|
} } // namespace v8::internal
|