blob: 07b8355eb3fc5511bd907eedcf63da19d67b637e [file] [log] [blame]
// Copyright 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/crankshaft/arm64/lithium-codegen-arm64.h"
#include "src/arm64/frames-arm64.h"
#include "src/base/bits.h"
#include "src/builtins/builtins-constructor.h"
#include "src/code-factory.h"
#include "src/code-stubs.h"
#include "src/crankshaft/arm64/lithium-gap-resolver-arm64.h"
#include "src/crankshaft/hydrogen-osr.h"
#include "src/ic/ic.h"
#include "src/ic/stub-cache.h"
namespace v8 {
namespace internal {
class SafepointGenerator final : public CallWrapper {
public:
SafepointGenerator(LCodeGen* codegen,
LPointerMap* pointers,
Safepoint::DeoptMode mode)
: codegen_(codegen),
pointers_(pointers),
deopt_mode_(mode) { }
virtual ~SafepointGenerator() { }
virtual void BeforeCall(int call_size) const { }
virtual void AfterCall() const {
codegen_->RecordSafepoint(pointers_, deopt_mode_);
}
private:
LCodeGen* codegen_;
LPointerMap* pointers_;
Safepoint::DeoptMode deopt_mode_;
};
LCodeGen::PushSafepointRegistersScope::PushSafepointRegistersScope(
LCodeGen* codegen)
: codegen_(codegen) {
DCHECK(codegen_->info()->is_calling());
DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kSimple);
codegen_->expected_safepoint_kind_ = Safepoint::kWithRegisters;
UseScratchRegisterScope temps(codegen_->masm_);
// Preserve the value of lr which must be saved on the stack (the call to
// the stub will clobber it).
Register to_be_pushed_lr =
temps.UnsafeAcquire(StoreRegistersStateStub::to_be_pushed_lr());
codegen_->masm_->Mov(to_be_pushed_lr, lr);
StoreRegistersStateStub stub(codegen_->isolate());
codegen_->masm_->CallStub(&stub);
}
LCodeGen::PushSafepointRegistersScope::~PushSafepointRegistersScope() {
DCHECK(codegen_->expected_safepoint_kind_ == Safepoint::kWithRegisters);
RestoreRegistersStateStub stub(codegen_->isolate());
codegen_->masm_->CallStub(&stub);
codegen_->expected_safepoint_kind_ = Safepoint::kSimple;
}
#define __ masm()->
// Emit code to branch if the given condition holds.
// The code generated here doesn't modify the flags and they must have
// been set by some prior instructions.
//
// The EmitInverted function simply inverts the condition.
class BranchOnCondition : public BranchGenerator {
public:
BranchOnCondition(LCodeGen* codegen, Condition cond)
: BranchGenerator(codegen),
cond_(cond) { }
virtual void Emit(Label* label) const {
__ B(cond_, label);
}
virtual void EmitInverted(Label* label) const {
if (cond_ != al) {
__ B(NegateCondition(cond_), label);
}
}
private:
Condition cond_;
};
// Emit code to compare lhs and rhs and branch if the condition holds.
// This uses MacroAssembler's CompareAndBranch function so it will handle
// converting the comparison to Cbz/Cbnz if the right-hand side is 0.
//
// EmitInverted still compares the two operands but inverts the condition.
class CompareAndBranch : public BranchGenerator {
public:
CompareAndBranch(LCodeGen* codegen,
Condition cond,
const Register& lhs,
const Operand& rhs)
: BranchGenerator(codegen),
cond_(cond),
lhs_(lhs),
rhs_(rhs) { }
virtual void Emit(Label* label) const {
__ CompareAndBranch(lhs_, rhs_, cond_, label);
}
virtual void EmitInverted(Label* label) const {
__ CompareAndBranch(lhs_, rhs_, NegateCondition(cond_), label);
}
private:
Condition cond_;
const Register& lhs_;
const Operand& rhs_;
};
// Test the input with the given mask and branch if the condition holds.
// If the condition is 'eq' or 'ne' this will use MacroAssembler's
// TestAndBranchIfAllClear and TestAndBranchIfAnySet so it will handle the
// conversion to Tbz/Tbnz when possible.
class TestAndBranch : public BranchGenerator {
public:
TestAndBranch(LCodeGen* codegen,
Condition cond,
const Register& value,
uint64_t mask)
: BranchGenerator(codegen),
cond_(cond),
value_(value),
mask_(mask) { }
virtual void Emit(Label* label) const {
switch (cond_) {
case eq:
__ TestAndBranchIfAllClear(value_, mask_, label);
break;
case ne:
__ TestAndBranchIfAnySet(value_, mask_, label);
break;
default:
__ Tst(value_, mask_);
__ B(cond_, label);
}
}
virtual void EmitInverted(Label* label) const {
// The inverse of "all clear" is "any set" and vice versa.
switch (cond_) {
case eq:
__ TestAndBranchIfAnySet(value_, mask_, label);
break;
case ne:
__ TestAndBranchIfAllClear(value_, mask_, label);
break;
default:
__ Tst(value_, mask_);
__ B(NegateCondition(cond_), label);
}
}
private:
Condition cond_;
const Register& value_;
uint64_t mask_;
};
// Test the input and branch if it is non-zero and not a NaN.
class BranchIfNonZeroNumber : public BranchGenerator {
public:
BranchIfNonZeroNumber(LCodeGen* codegen, const FPRegister& value,
const FPRegister& scratch)
: BranchGenerator(codegen), value_(value), scratch_(scratch) { }
virtual void Emit(Label* label) const {
__ Fabs(scratch_, value_);
// Compare with 0.0. Because scratch_ is positive, the result can be one of
// nZCv (equal), nzCv (greater) or nzCV (unordered).
__ Fcmp(scratch_, 0.0);
__ B(gt, label);
}
virtual void EmitInverted(Label* label) const {
__ Fabs(scratch_, value_);
__ Fcmp(scratch_, 0.0);
__ B(le, label);
}
private:
const FPRegister& value_;
const FPRegister& scratch_;
};
// Test the input and branch if it is a heap number.
class BranchIfHeapNumber : public BranchGenerator {
public:
BranchIfHeapNumber(LCodeGen* codegen, const Register& value)
: BranchGenerator(codegen), value_(value) { }
virtual void Emit(Label* label) const {
__ JumpIfHeapNumber(value_, label);
}
virtual void EmitInverted(Label* label) const {
__ JumpIfNotHeapNumber(value_, label);
}
private:
const Register& value_;
};
// Test the input and branch if it is the specified root value.
class BranchIfRoot : public BranchGenerator {
public:
BranchIfRoot(LCodeGen* codegen, const Register& value,
Heap::RootListIndex index)
: BranchGenerator(codegen), value_(value), index_(index) { }
virtual void Emit(Label* label) const {
__ JumpIfRoot(value_, index_, label);
}
virtual void EmitInverted(Label* label) const {
__ JumpIfNotRoot(value_, index_, label);
}
private:
const Register& value_;
const Heap::RootListIndex index_;
};
void LCodeGen::WriteTranslation(LEnvironment* environment,
Translation* translation) {
if (environment == NULL) return;
// The translation includes one command per value in the environment.
int translation_size = environment->translation_size();
WriteTranslation(environment->outer(), translation);
WriteTranslationFrame(environment, translation);
int object_index = 0;
int dematerialized_index = 0;
for (int i = 0; i < translation_size; ++i) {
LOperand* value = environment->values()->at(i);
AddToTranslation(
environment, translation, value, environment->HasTaggedValueAt(i),
environment->HasUint32ValueAt(i), &object_index, &dematerialized_index);
}
}
void LCodeGen::AddToTranslation(LEnvironment* environment,
Translation* translation,
LOperand* op,
bool is_tagged,
bool is_uint32,
int* object_index_pointer,
int* dematerialized_index_pointer) {
if (op == LEnvironment::materialization_marker()) {
int object_index = (*object_index_pointer)++;
if (environment->ObjectIsDuplicateAt(object_index)) {
int dupe_of = environment->ObjectDuplicateOfAt(object_index);
translation->DuplicateObject(dupe_of);
return;
}
int object_length = environment->ObjectLengthAt(object_index);
if (environment->ObjectIsArgumentsAt(object_index)) {
translation->BeginArgumentsObject(object_length);
} else {
translation->BeginCapturedObject(object_length);
}
int dematerialized_index = *dematerialized_index_pointer;
int env_offset = environment->translation_size() + dematerialized_index;
*dematerialized_index_pointer += object_length;
for (int i = 0; i < object_length; ++i) {
LOperand* value = environment->values()->at(env_offset + i);
AddToTranslation(environment,
translation,
value,
environment->HasTaggedValueAt(env_offset + i),
environment->HasUint32ValueAt(env_offset + i),
object_index_pointer,
dematerialized_index_pointer);
}
return;
}
if (op->IsStackSlot()) {
int index = op->index();
if (is_tagged) {
translation->StoreStackSlot(index);
} else if (is_uint32) {
translation->StoreUint32StackSlot(index);
} else {
translation->StoreInt32StackSlot(index);
}
} else if (op->IsDoubleStackSlot()) {
int index = op->index();
translation->StoreDoubleStackSlot(index);
} else if (op->IsRegister()) {
Register reg = ToRegister(op);
if (is_tagged) {
translation->StoreRegister(reg);
} else if (is_uint32) {
translation->StoreUint32Register(reg);
} else {
translation->StoreInt32Register(reg);
}
} else if (op->IsDoubleRegister()) {
DoubleRegister reg = ToDoubleRegister(op);
translation->StoreDoubleRegister(reg);
} else if (op->IsConstantOperand()) {
HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
translation->StoreLiteral(src_index);
} else {
UNREACHABLE();
}
}
void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
Safepoint::DeoptMode mode) {
environment->set_has_been_used();
if (!environment->HasBeenRegistered()) {
int frame_count = 0;
int jsframe_count = 0;
for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
++frame_count;
if (e->frame_type() == JS_FUNCTION) {
++jsframe_count;
}
}
Translation translation(&translations_, frame_count, jsframe_count, zone());
WriteTranslation(environment, &translation);
int deoptimization_index = deoptimizations_.length();
int pc_offset = masm()->pc_offset();
environment->Register(deoptimization_index,
translation.index(),
(mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
deoptimizations_.Add(environment, zone());
}
}
void LCodeGen::CallCode(Handle<Code> code,
RelocInfo::Mode mode,
LInstruction* instr) {
CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT);
}
void LCodeGen::CallCodeGeneric(Handle<Code> code,
RelocInfo::Mode mode,
LInstruction* instr,
SafepointMode safepoint_mode) {
DCHECK(instr != NULL);
Assembler::BlockPoolsScope scope(masm_);
__ Call(code, mode);
RecordSafepointWithLazyDeopt(instr, safepoint_mode);
if ((code->kind() == Code::BINARY_OP_IC) ||
(code->kind() == Code::COMPARE_IC)) {
// Signal that we don't inline smi code before these stubs in the
// optimizing code generator.
InlineSmiCheckInfo::EmitNotInlined(masm());
}
}
void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
DCHECK(instr->IsMarkedAsCall());
DCHECK(ToRegister(instr->context()).is(cp));
DCHECK(ToRegister(instr->constructor()).is(x1));
__ Mov(x0, Operand(instr->arity()));
__ Mov(x2, instr->hydrogen()->site());
ElementsKind kind = instr->hydrogen()->elements_kind();
AllocationSiteOverrideMode override_mode =
(AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
? DISABLE_ALLOCATION_SITES
: DONT_OVERRIDE;
if (instr->arity() == 0) {
ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
} else if (instr->arity() == 1) {
Label done;
if (IsFastPackedElementsKind(kind)) {
Label packed_case;
// We might need to create a holey array; look at the first argument.
__ Peek(x10, 0);
__ Cbz(x10, &packed_case);
ElementsKind holey_kind = GetHoleyElementsKind(kind);
ArraySingleArgumentConstructorStub stub(isolate(),
holey_kind,
override_mode);
CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
__ B(&done);
__ Bind(&packed_case);
}
ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
__ Bind(&done);
} else {
ArrayNArgumentsConstructorStub stub(isolate());
CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
}
RecordPushedArgumentsDelta(instr->hydrogen()->argument_delta());
DCHECK(ToRegister(instr->result()).is(x0));
}
void LCodeGen::CallRuntime(const Runtime::Function* function,
int num_arguments,
LInstruction* instr,
SaveFPRegsMode save_doubles) {
DCHECK(instr != NULL);
__ CallRuntime(function, num_arguments, save_doubles);
RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
}
void LCodeGen::LoadContextFromDeferred(LOperand* context) {
if (context->IsRegister()) {
__ Mov(cp, ToRegister(context));
} else if (context->IsStackSlot()) {
__ Ldr(cp, ToMemOperand(context, kMustUseFramePointer));
} else if (context->IsConstantOperand()) {
HConstant* constant =
chunk_->LookupConstant(LConstantOperand::cast(context));
__ LoadHeapObject(cp,
Handle<HeapObject>::cast(constant->handle(isolate())));
} else {
UNREACHABLE();
}
}
void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
int argc,
LInstruction* instr,
LOperand* context) {
if (context != nullptr) LoadContextFromDeferred(context);
__ CallRuntimeSaveDoubles(id);
RecordSafepointWithRegisters(
instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
}
void LCodeGen::RecordSafepointWithLazyDeopt(LInstruction* instr,
SafepointMode safepoint_mode) {
if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
} else {
DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
RecordSafepointWithRegisters(
instr->pointer_map(), 0, Safepoint::kLazyDeopt);
}
}
void LCodeGen::RecordSafepoint(LPointerMap* pointers,
Safepoint::Kind kind,
int arguments,
Safepoint::DeoptMode deopt_mode) {
DCHECK(expected_safepoint_kind_ == kind);
const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
Safepoint safepoint = safepoints_.DefineSafepoint(
masm(), kind, arguments, deopt_mode);
for (int i = 0; i < operands->length(); i++) {
LOperand* pointer = operands->at(i);
if (pointer->IsStackSlot()) {
safepoint.DefinePointerSlot(pointer->index(), zone());
} else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
safepoint.DefinePointerRegister(ToRegister(pointer), zone());
}
}
}
void LCodeGen::RecordSafepoint(LPointerMap* pointers,
Safepoint::DeoptMode deopt_mode) {
RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
}
void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
LPointerMap empty_pointers(zone());
RecordSafepoint(&empty_pointers, deopt_mode);
}
void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
int arguments,
Safepoint::DeoptMode deopt_mode) {
RecordSafepoint(pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
}
bool LCodeGen::GenerateCode() {
LPhase phase("Z_Code generation", chunk());
DCHECK(is_unused());
status_ = GENERATING;
// Open a frame scope to indicate that there is a frame on the stack. The
// NONE indicates that the scope shouldn't actually generate code to set up
// the frame (that is done in GeneratePrologue).
FrameScope frame_scope(masm_, StackFrame::NONE);
return GeneratePrologue() && GenerateBody() && GenerateDeferredCode() &&
GenerateJumpTable() && GenerateSafepointTable();
}
void LCodeGen::SaveCallerDoubles() {
DCHECK(info()->saves_caller_doubles());
DCHECK(NeedsEagerFrame());
Comment(";;; Save clobbered callee double registers");
BitVector* doubles = chunk()->allocated_double_registers();
BitVector::Iterator iterator(doubles);
int count = 0;
while (!iterator.Done()) {
// TODO(all): Is this supposed to save just the callee-saved doubles? It
// looks like it's saving all of them.
FPRegister value = FPRegister::from_code(iterator.Current());
__ Poke(value, count * kDoubleSize);
iterator.Advance();
count++;
}
}
void LCodeGen::RestoreCallerDoubles() {
DCHECK(info()->saves_caller_doubles());
DCHECK(NeedsEagerFrame());
Comment(";;; Restore clobbered callee double registers");
BitVector* doubles = chunk()->allocated_double_registers();
BitVector::Iterator iterator(doubles);
int count = 0;
while (!iterator.Done()) {
// TODO(all): Is this supposed to restore just the callee-saved doubles? It
// looks like it's restoring all of them.
FPRegister value = FPRegister::from_code(iterator.Current());
__ Peek(value, count * kDoubleSize);
iterator.Advance();
count++;
}
}
bool LCodeGen::GeneratePrologue() {
DCHECK(is_generating());
if (info()->IsOptimizing()) {
ProfileEntryHookStub::MaybeCallEntryHook(masm_);
}
DCHECK(__ StackPointer().Is(jssp));
info()->set_prologue_offset(masm_->pc_offset());
if (NeedsEagerFrame()) {
if (info()->IsStub()) {
__ StubPrologue(
StackFrame::STUB,
GetStackSlotCount() + TypedFrameConstants::kFixedSlotCount);
} else {
__ Prologue(info()->GeneratePreagedPrologue());
// Reserve space for the stack slots needed by the code.
int slots = GetStackSlotCount();
if (slots > 0) {
__ Claim(slots, kPointerSize);
}
}
frame_is_built_ = true;
}
if (info()->saves_caller_doubles()) {
SaveCallerDoubles();
}
return !is_aborted();
}
void LCodeGen::DoPrologue(LPrologue* instr) {
Comment(";;; Prologue begin");
// Allocate a local context if needed.
if (info()->scope()->NeedsContext()) {
Comment(";;; Allocate local context");
bool need_write_barrier = true;
// Argument to NewContext is the function, which is in x1.
int slots = info()->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
Safepoint::DeoptMode deopt_mode = Safepoint::kNoLazyDeopt;
if (info()->scope()->is_script_scope()) {
__ Mov(x10, Operand(info()->scope()->scope_info()));
__ Push(x1, x10);
__ CallRuntime(Runtime::kNewScriptContext);
deopt_mode = Safepoint::kLazyDeopt;
} else {
if (slots <=
ConstructorBuiltinsAssembler::MaximumFunctionContextSlots()) {
Callable callable = CodeFactory::FastNewFunctionContext(
isolate(), info()->scope()->scope_type());
__ Mov(FastNewFunctionContextDescriptor::SlotsRegister(), slots);
__ Call(callable.code(), RelocInfo::CODE_TARGET);
// Result of the FastNewFunctionContext builtin is always in new space.
need_write_barrier = false;
} else {
__ Push(x1);
__ Push(Smi::FromInt(info()->scope()->scope_type()));
__ CallRuntime(Runtime::kNewFunctionContext);
}
}
RecordSafepoint(deopt_mode);
// Context is returned in x0. It replaces the context passed to us. It's
// saved in the stack and kept live in cp.
__ Mov(cp, x0);
__ Str(x0, MemOperand(fp, StandardFrameConstants::kContextOffset));
// Copy any necessary parameters into the context.
int num_parameters = info()->scope()->num_parameters();
int first_parameter = info()->scope()->has_this_declaration() ? -1 : 0;
for (int i = first_parameter; i < num_parameters; i++) {
Variable* var = (i == -1) ? info()->scope()->receiver()
: info()->scope()->parameter(i);
if (var->IsContextSlot()) {
Register value = x0;
Register scratch = x3;
int parameter_offset = StandardFrameConstants::kCallerSPOffset +
(num_parameters - 1 - i) * kPointerSize;
// Load parameter from stack.
__ Ldr(value, MemOperand(fp, parameter_offset));
// Store it in the context.
MemOperand target = ContextMemOperand(cp, var->index());
__ Str(value, target);
// Update the write barrier. This clobbers value and scratch.
if (need_write_barrier) {
__ RecordWriteContextSlot(cp, static_cast<int>(target.offset()),
value, scratch, GetLinkRegisterState(),
kSaveFPRegs);
} else if (FLAG_debug_code) {
Label done;
__ JumpIfInNewSpace(cp, &done);
__ Abort(kExpectedNewSpaceObject);
__ bind(&done);
}
}
}
Comment(";;; End allocate local context");
}
Comment(";;; Prologue end");
}
void LCodeGen::GenerateOsrPrologue() {
// Generate the OSR entry prologue at the first unknown OSR value, or if there
// are none, at the OSR entrypoint instruction.
if (osr_pc_offset_ >= 0) return;
osr_pc_offset_ = masm()->pc_offset();
// Adjust the frame size, subsuming the unoptimized frame into the
// optimized frame.
int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
DCHECK(slots >= 0);
__ Claim(slots);
}
void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
if (instr->IsCall()) {
EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
}
if (!instr->IsLazyBailout() && !instr->IsGap()) {
safepoints_.BumpLastLazySafepointIndex();
}
}
bool LCodeGen::GenerateDeferredCode() {
DCHECK(is_generating());
if (deferred_.length() > 0) {
for (int i = 0; !is_aborted() && (i < deferred_.length()); i++) {
LDeferredCode* code = deferred_[i];
HValue* value =
instructions_->at(code->instruction_index())->hydrogen_value();
RecordAndWritePosition(value->position());
Comment(";;; <@%d,#%d> "
"-------------------- Deferred %s --------------------",
code->instruction_index(),
code->instr()->hydrogen_value()->id(),
code->instr()->Mnemonic());
__ Bind(code->entry());
if (NeedsDeferredFrame()) {
Comment(";;; Build frame");
DCHECK(!frame_is_built_);
DCHECK(info()->IsStub());
frame_is_built_ = true;
__ Push(lr, fp);
__ Mov(fp, Smi::FromInt(StackFrame::STUB));
__ Push(fp);
__ Add(fp, __ StackPointer(),
TypedFrameConstants::kFixedFrameSizeFromFp);
Comment(";;; Deferred code");
}
code->Generate();
if (NeedsDeferredFrame()) {
Comment(";;; Destroy frame");
DCHECK(frame_is_built_);
__ Pop(xzr, fp, lr);
frame_is_built_ = false;
}
__ B(code->exit());
}
}
// Force constant pool emission at the end of the deferred code to make
// sure that no constant pools are emitted after deferred code because
// deferred code generation is the last step which generates code. The two
// following steps will only output data used by crakshaft.
masm()->CheckConstPool(true, false);
return !is_aborted();
}
bool LCodeGen::GenerateJumpTable() {
Label needs_frame, call_deopt_entry;
if (jump_table_.length() > 0) {
Comment(";;; -------------------- Jump table --------------------");
Address base = jump_table_[0]->address;
UseScratchRegisterScope temps(masm());
Register entry_offset = temps.AcquireX();
int length = jump_table_.length();
for (int i = 0; i < length; i++) {
Deoptimizer::JumpTableEntry* table_entry = jump_table_[i];
__ Bind(&table_entry->label);
Address entry = table_entry->address;
DeoptComment(table_entry->deopt_info);
// Second-level deopt table entries are contiguous and small, so instead
// of loading the full, absolute address of each one, load the base
// address and add an immediate offset.
__ Mov(entry_offset, entry - base);
if (table_entry->needs_frame) {
DCHECK(!info()->saves_caller_doubles());
Comment(";;; call deopt with frame");
// Save lr before Bl, fp will be adjusted in the needs_frame code.
__ Push(lr, fp);
// Reuse the existing needs_frame code.
__ Bl(&needs_frame);
} else {
// There is nothing special to do, so just continue to the second-level
// table.
__ Bl(&call_deopt_entry);
}
masm()->CheckConstPool(false, false);
}
if (needs_frame.is_linked()) {
// This variant of deopt can only be used with stubs. Since we don't
// have a function pointer to install in the stack frame that we're
// building, install a special marker there instead.
DCHECK(info()->IsStub());
Comment(";;; needs_frame common code");
UseScratchRegisterScope temps(masm());
Register stub_marker = temps.AcquireX();
__ Bind(&needs_frame);
__ Mov(stub_marker, Smi::FromInt(StackFrame::STUB));
__ Push(cp, stub_marker);
__ Add(fp, __ StackPointer(), 2 * kPointerSize);
}
// Generate common code for calling the second-level deopt table.
__ Bind(&call_deopt_entry);
if (info()->saves_caller_doubles()) {
DCHECK(info()->IsStub());
RestoreCallerDoubles();
}
Register deopt_entry = temps.AcquireX();
__ Mov(deopt_entry, Operand(reinterpret_cast<uint64_t>(base),
RelocInfo::RUNTIME_ENTRY));
__ Add(deopt_entry, deopt_entry, entry_offset);
__ Br(deopt_entry);
}
// Force constant pool emission at the end of the deopt jump table to make
// sure that no constant pools are emitted after.
masm()->CheckConstPool(true, false);
// The deoptimization jump table is the last part of the instruction
// sequence. Mark the generated code as done unless we bailed out.
if (!is_aborted()) status_ = DONE;
return !is_aborted();
}
bool LCodeGen::GenerateSafepointTable() {
DCHECK(is_done());
// We do not know how much data will be emitted for the safepoint table, so
// force emission of the veneer pool.
masm()->CheckVeneerPool(true, true);
safepoints_.Emit(masm(), GetTotalFrameSlotCount());
return !is_aborted();
}
void LCodeGen::FinishCode(Handle<Code> code) {
DCHECK(is_done());
code->set_stack_slots(GetTotalFrameSlotCount());
code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
PopulateDeoptimizationData(code);
}
void LCodeGen::DeoptimizeBranch(
LInstruction* instr, DeoptimizeReason deopt_reason, BranchType branch_type,
Register reg, int bit, Deoptimizer::BailoutType* override_bailout_type) {
LEnvironment* environment = instr->environment();
RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
Deoptimizer::BailoutType bailout_type =
info()->IsStub() ? Deoptimizer::LAZY : Deoptimizer::EAGER;
if (override_bailout_type != NULL) {
bailout_type = *override_bailout_type;
}
DCHECK(environment->HasBeenRegistered());
int id = environment->deoptimization_index();
Address entry =
Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
if (entry == NULL) {
Abort(kBailoutWasNotPrepared);
}
if (FLAG_deopt_every_n_times != 0 && !info()->IsStub()) {
Label not_zero;
ExternalReference count = ExternalReference::stress_deopt_count(isolate());
__ Push(x0, x1, x2);
__ Mrs(x2, NZCV);
__ Mov(x0, count);
__ Ldr(w1, MemOperand(x0));
__ Subs(x1, x1, 1);
__ B(gt, &not_zero);
__ Mov(w1, FLAG_deopt_every_n_times);
__ Str(w1, MemOperand(x0));
__ Pop(x2, x1, x0);
DCHECK(frame_is_built_);
__ Call(entry, RelocInfo::RUNTIME_ENTRY);
__ Unreachable();
__ Bind(&not_zero);
__ Str(w1, MemOperand(x0));
__ Msr(NZCV, x2);
__ Pop(x2, x1, x0);
}
if (info()->ShouldTrapOnDeopt()) {
Label dont_trap;
__ B(&dont_trap, InvertBranchType(branch_type), reg, bit);
__ Debug("trap_on_deopt", __LINE__, BREAK);
__ Bind(&dont_trap);
}
Deoptimizer::DeoptInfo deopt_info = MakeDeoptInfo(instr, deopt_reason, id);
DCHECK(info()->IsStub() || frame_is_built_);
// Go through jump table if we need to build frame, or restore caller doubles.
if (branch_type == always &&
frame_is_built_ && !info()->saves_caller_doubles()) {
DeoptComment(deopt_info);
__ Call(entry, RelocInfo::RUNTIME_ENTRY);
} else {
Deoptimizer::JumpTableEntry* table_entry =
new (zone()) Deoptimizer::JumpTableEntry(
entry, deopt_info, bailout_type, !frame_is_built_);
// We often have several deopts to the same entry, reuse the last
// jump entry if this is the case.
if (FLAG_trace_deopt || isolate()->is_profiling() ||
jump_table_.is_empty() ||
!table_entry->IsEquivalentTo(*jump_table_.last())) {
jump_table_.Add(table_entry, zone());
}
__ B(&jump_table_.last()->label, branch_type, reg, bit);
}
}
void LCodeGen::Deoptimize(LInstruction* instr, DeoptimizeReason deopt_reason,
Deoptimizer::BailoutType* override_bailout_type) {
DeoptimizeBranch(instr, deopt_reason, always, NoReg, -1,
override_bailout_type);
}
void LCodeGen::DeoptimizeIf(Condition cond, LInstruction* instr,
DeoptimizeReason deopt_reason) {
DeoptimizeBranch(instr, deopt_reason, static_cast<BranchType>(cond));
}
void LCodeGen::DeoptimizeIfZero(Register rt, LInstruction* instr,
DeoptimizeReason deopt_reason) {
DeoptimizeBranch(instr, deopt_reason, reg_zero, rt);
}
void LCodeGen::DeoptimizeIfNotZero(Register rt, LInstruction* instr,
DeoptimizeReason deopt_reason) {
DeoptimizeBranch(instr, deopt_reason, reg_not_zero, rt);
}
void LCodeGen::DeoptimizeIfNegative(Register rt, LInstruction* instr,
DeoptimizeReason deopt_reason) {
int sign_bit = rt.Is64Bits() ? kXSignBit : kWSignBit;
DeoptimizeIfBitSet(rt, sign_bit, instr, deopt_reason);
}
void LCodeGen::DeoptimizeIfSmi(Register rt, LInstruction* instr,
DeoptimizeReason deopt_reason) {
DeoptimizeIfBitClear(rt, MaskToBit(kSmiTagMask), instr, deopt_reason);
}
void LCodeGen::DeoptimizeIfNotSmi(Register rt, LInstruction* instr,
DeoptimizeReason deopt_reason) {
DeoptimizeIfBitSet(rt, MaskToBit(kSmiTagMask), instr, deopt_reason);
}
void LCodeGen::DeoptimizeIfRoot(Register rt, Heap::RootListIndex index,
LInstruction* instr,
DeoptimizeReason deopt_reason) {
__ CompareRoot(rt, index);
DeoptimizeIf(eq, instr, deopt_reason);
}
void LCodeGen::DeoptimizeIfNotRoot(Register rt, Heap::RootListIndex index,
LInstruction* instr,
DeoptimizeReason deopt_reason) {
__ CompareRoot(rt, index);
DeoptimizeIf(ne, instr, deopt_reason);
}
void LCodeGen::DeoptimizeIfMinusZero(DoubleRegister input, LInstruction* instr,
DeoptimizeReason deopt_reason) {
__ TestForMinusZero(input);
DeoptimizeIf(vs, instr, deopt_reason);
}
void LCodeGen::DeoptimizeIfNotHeapNumber(Register object, LInstruction* instr) {
__ CompareObjectMap(object, Heap::kHeapNumberMapRootIndex);
DeoptimizeIf(ne, instr, DeoptimizeReason::kNotAHeapNumber);
}
void LCodeGen::DeoptimizeIfBitSet(Register rt, int bit, LInstruction* instr,
DeoptimizeReason deopt_reason) {
DeoptimizeBranch(instr, deopt_reason, reg_bit_set, rt, bit);
}
void LCodeGen::DeoptimizeIfBitClear(Register rt, int bit, LInstruction* instr,
DeoptimizeReason deopt_reason) {
DeoptimizeBranch(instr, deopt_reason, reg_bit_clear, rt, bit);
}
void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
if (info()->ShouldEnsureSpaceForLazyDeopt()) {
// Ensure that we have enough space after the previous lazy-bailout
// instruction for patching the code here.
intptr_t current_pc = masm()->pc_offset();
if (current_pc < (last_lazy_deopt_pc_ + space_needed)) {
ptrdiff_t padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
DCHECK((padding_size % kInstructionSize) == 0);
InstructionAccurateScope instruction_accurate(
masm(), padding_size / kInstructionSize);
while (padding_size > 0) {
__ nop();
padding_size -= kInstructionSize;
}
}
}
last_lazy_deopt_pc_ = masm()->pc_offset();
}
Register LCodeGen::ToRegister(LOperand* op) const {
// TODO(all): support zero register results, as ToRegister32.
DCHECK((op != NULL) && op->IsRegister());
return Register::from_code(op->index());
}
Register LCodeGen::ToRegister32(LOperand* op) const {
DCHECK(op != NULL);
if (op->IsConstantOperand()) {
// If this is a constant operand, the result must be the zero register.
DCHECK(ToInteger32(LConstantOperand::cast(op)) == 0);
return wzr;
} else {
return ToRegister(op).W();
}
}
Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
HConstant* constant = chunk_->LookupConstant(op);
return Smi::FromInt(constant->Integer32Value());
}
DoubleRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
DCHECK((op != NULL) && op->IsDoubleRegister());
return DoubleRegister::from_code(op->index());
}
Operand LCodeGen::ToOperand(LOperand* op) {
DCHECK(op != NULL);
if (op->IsConstantOperand()) {
LConstantOperand* const_op = LConstantOperand::cast(op);
HConstant* constant = chunk()->LookupConstant(const_op);
Representation r = chunk_->LookupLiteralRepresentation(const_op);
if (r.IsSmi()) {
DCHECK(constant->HasSmiValue());
return Operand(Smi::FromInt(constant->Integer32Value()));
} else if (r.IsInteger32()) {
DCHECK(constant->HasInteger32Value());
return Operand(constant->Integer32Value());
} else if (r.IsDouble()) {
Abort(kToOperandUnsupportedDoubleImmediate);
}
DCHECK(r.IsTagged());
return Operand(constant->handle(isolate()));
} else if (op->IsRegister()) {
return Operand(ToRegister(op));
} else if (op->IsDoubleRegister()) {
Abort(kToOperandIsDoubleRegisterUnimplemented);
return Operand(0);
}
// Stack slots not implemented, use ToMemOperand instead.
UNREACHABLE();
return Operand(0);
}
Operand LCodeGen::ToOperand32(LOperand* op) {
DCHECK(op != NULL);
if (op->IsRegister()) {
return Operand(ToRegister32(op));
} else if (op->IsConstantOperand()) {
LConstantOperand* const_op = LConstantOperand::cast(op);
HConstant* constant = chunk()->LookupConstant(const_op);
Representation r = chunk_->LookupLiteralRepresentation(const_op);
if (r.IsInteger32()) {
return Operand(constant->Integer32Value());
} else {
// Other constants not implemented.
Abort(kToOperand32UnsupportedImmediate);
}
}
// Other cases are not implemented.
UNREACHABLE();
return Operand(0);
}
static int64_t ArgumentsOffsetWithoutFrame(int index) {
DCHECK(index < 0);
return -(index + 1) * kPointerSize;
}
MemOperand LCodeGen::ToMemOperand(LOperand* op, StackMode stack_mode) const {
DCHECK(op != NULL);
DCHECK(!op->IsRegister());
DCHECK(!op->IsDoubleRegister());
DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
if (NeedsEagerFrame()) {
int fp_offset = FrameSlotToFPOffset(op->index());
// Loads and stores have a bigger reach in positive offset than negative.
// We try to access using jssp (positive offset) first, then fall back to
// fp (negative offset) if that fails.
//
// We can reference a stack slot from jssp only if we know how much we've
// put on the stack. We don't know this in the following cases:
// - stack_mode != kCanUseStackPointer: this is the case when deferred
// code has saved the registers.
// - saves_caller_doubles(): some double registers have been pushed, jssp
// references the end of the double registers and not the end of the stack
// slots.
// In both of the cases above, we _could_ add the tracking information
// required so that we can use jssp here, but in practice it isn't worth it.
if ((stack_mode == kCanUseStackPointer) &&
!info()->saves_caller_doubles()) {
int jssp_offset_to_fp =
(pushed_arguments_ + GetTotalFrameSlotCount()) * kPointerSize -
StandardFrameConstants::kFixedFrameSizeAboveFp;
int jssp_offset = fp_offset + jssp_offset_to_fp;
if (masm()->IsImmLSScaled(jssp_offset, LSDoubleWord)) {
return MemOperand(masm()->StackPointer(), jssp_offset);
}
}
return MemOperand(fp, fp_offset);
} else {
// Retrieve parameter without eager stack-frame relative to the
// stack-pointer.
return MemOperand(masm()->StackPointer(),
ArgumentsOffsetWithoutFrame(op->index()));
}
}
Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
HConstant* constant = chunk_->LookupConstant(op);
DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
return constant->handle(isolate());
}
template <class LI>
Operand LCodeGen::ToShiftedRightOperand32(LOperand* right, LI* shift_info) {
if (shift_info->shift() == NO_SHIFT) {
return ToOperand32(right);
} else {
return Operand(
ToRegister32(right),
shift_info->shift(),
JSShiftAmountFromLConstant(shift_info->shift_amount()));
}
}
bool LCodeGen::IsSmi(LConstantOperand* op) const {
return chunk_->LookupLiteralRepresentation(op).IsSmi();
}
bool LCodeGen::IsInteger32Constant(LConstantOperand* op) const {
return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
}
int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
HConstant* constant = chunk_->LookupConstant(op);
return constant->Integer32Value();
}
double LCodeGen::ToDouble(LConstantOperand* op) const {
HConstant* constant = chunk_->LookupConstant(op);
DCHECK(constant->HasDoubleValue());
return constant->DoubleValue();
}
Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
Condition cond = nv;
switch (op) {
case Token::EQ:
case Token::EQ_STRICT:
cond = eq;
break;
case Token::NE:
case Token::NE_STRICT:
cond = ne;
break;
case Token::LT:
cond = is_unsigned ? lo : lt;
break;
case Token::GT:
cond = is_unsigned ? hi : gt;
break;
case Token::LTE:
cond = is_unsigned ? ls : le;
break;
case Token::GTE:
cond = is_unsigned ? hs : ge;
break;
case Token::IN:
case Token::INSTANCEOF:
default:
UNREACHABLE();
}
return cond;
}
template<class InstrType>
void LCodeGen::EmitBranchGeneric(InstrType instr,
const BranchGenerator& branch) {
int left_block = instr->TrueDestination(chunk_);
int right_block = instr->FalseDestination(chunk_);
int next_block = GetNextEmittedBlock();
if (right_block == left_block) {
EmitGoto(left_block);
} else if (left_block == next_block) {
branch.EmitInverted(chunk_->GetAssemblyLabel(right_block));
} else {
branch.Emit(chunk_->GetAssemblyLabel(left_block));
if (right_block != next_block) {
__ B(chunk_->GetAssemblyLabel(right_block));
}
}
}
template<class InstrType>
void LCodeGen::EmitBranch(InstrType instr, Condition condition) {
DCHECK((condition != al) && (condition != nv));
BranchOnCondition branch(this, condition);
EmitBranchGeneric(instr, branch);
}
template<class InstrType>
void LCodeGen::EmitCompareAndBranch(InstrType instr,
Condition condition,
const Register& lhs,
const Operand& rhs) {
DCHECK((condition != al) && (condition != nv));
CompareAndBranch branch(this, condition, lhs, rhs);
EmitBranchGeneric(instr, branch);
}
template<class InstrType>
void LCodeGen::EmitTestAndBranch(InstrType instr,
Condition condition,
const Register& value,
uint64_t mask) {
DCHECK((condition != al) && (condition != nv));
TestAndBranch branch(this, condition, value, mask);
EmitBranchGeneric(instr, branch);
}
template<class InstrType>
void LCodeGen::EmitBranchIfNonZeroNumber(InstrType instr,
const FPRegister& value,
const FPRegister& scratch) {
BranchIfNonZeroNumber branch(this, value, scratch);
EmitBranchGeneric(instr, branch);
}
template<class InstrType>
void LCodeGen::EmitBranchIfHeapNumber(InstrType instr,
const Register& value) {
BranchIfHeapNumber branch(this, value);
EmitBranchGeneric(instr, branch);
}
template<class InstrType>
void LCodeGen::EmitBranchIfRoot(InstrType instr,
const Register& value,
Heap::RootListIndex index) {
BranchIfRoot branch(this, value, index);
EmitBranchGeneric(instr, branch);
}
void LCodeGen::DoGap(LGap* gap) {
for (int i = LGap::FIRST_INNER_POSITION;
i <= LGap::LAST_INNER_POSITION;
i++) {
LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
LParallelMove* move = gap->GetParallelMove(inner_pos);
if (move != NULL) {
resolver_.Resolve(move);
}
}
}
void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
Register arguments = ToRegister(instr->arguments());
Register result = ToRegister(instr->result());
// The pointer to the arguments array come from DoArgumentsElements.
// It does not point directly to the arguments and there is an offest of
// two words that we must take into account when accessing an argument.
// Subtracting the index from length accounts for one, so we add one more.
if (instr->length()->IsConstantOperand() &&
instr->index()->IsConstantOperand()) {
int index = ToInteger32(LConstantOperand::cast(instr->index()));
int length = ToInteger32(LConstantOperand::cast(instr->length()));
int offset = ((length - index) + 1) * kPointerSize;
__ Ldr(result, MemOperand(arguments, offset));
} else if (instr->index()->IsConstantOperand()) {
Register length = ToRegister32(instr->length());
int index = ToInteger32(LConstantOperand::cast(instr->index()));
int loc = index - 1;
if (loc != 0) {
__ Sub(result.W(), length, loc);
__ Ldr(result, MemOperand(arguments, result, UXTW, kPointerSizeLog2));
} else {
__ Ldr(result, MemOperand(arguments, length, UXTW, kPointerSizeLog2));
}
} else {
Register length = ToRegister32(instr->length());
Operand index = ToOperand32(instr->index());
__ Sub(result.W(), length, index);
__ Add(result.W(), result.W(), 1);
__ Ldr(result, MemOperand(arguments, result, UXTW, kPointerSizeLog2));
}
}
void LCodeGen::DoAddE(LAddE* instr) {
Register result = ToRegister(instr->result());
Register left = ToRegister(instr->left());
Operand right = Operand(x0); // Dummy initialization.
if (instr->hydrogen()->external_add_type() == AddOfExternalAndTagged) {
right = Operand(ToRegister(instr->right()));
} else if (instr->right()->IsConstantOperand()) {
right = ToInteger32(LConstantOperand::cast(instr->right()));
} else {
right = Operand(ToRegister32(instr->right()), SXTW);
}
DCHECK(!instr->hydrogen()->CheckFlag(HValue::kCanOverflow));
__ Add(result, left, right);
}
void LCodeGen::DoAddI(LAddI* instr) {
bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
Register result = ToRegister32(instr->result());
Register left = ToRegister32(instr->left());
Operand right = ToShiftedRightOperand32(instr->right(), instr);
if (can_overflow) {
__ Adds(result, left, right);
DeoptimizeIf(vs, instr, DeoptimizeReason::kOverflow);
} else {
__ Add(result, left, right);
}
}
void LCodeGen::DoAddS(LAddS* instr) {
bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
Register result = ToRegister(instr->result());
Register left = ToRegister(instr->left());
Operand right = ToOperand(instr->right());
if (can_overflow) {
__ Adds(result, left, right);
DeoptimizeIf(vs, instr, DeoptimizeReason::kOverflow);
} else {
__ Add(result, left, right);
}
}
void LCodeGen::DoAllocate(LAllocate* instr) {
class DeferredAllocate: public LDeferredCode {
public:
DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
: LDeferredCode(codegen), instr_(instr) { }
virtual void Generate() { codegen()->DoDeferredAllocate(instr_); }
virtual LInstruction* instr() { return instr_; }
private:
LAllocate* instr_;
};
DeferredAllocate* deferred = new(zone()) DeferredAllocate(this, instr);
Register result = ToRegister(instr->result());
Register temp1 = ToRegister(instr->temp1());
Register temp2 = ToRegister(instr->temp2());
// Allocate memory for the object.
AllocationFlags flags = NO_ALLOCATION_FLAGS;
if (instr->hydrogen()->MustAllocateDoubleAligned()) {
flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
}
if (instr->hydrogen()->IsOldSpaceAllocation()) {
DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
flags = static_cast<AllocationFlags>(flags | PRETENURE);
}
if (instr->hydrogen()->IsAllocationFoldingDominator()) {
flags = static_cast<AllocationFlags>(flags | ALLOCATION_FOLDING_DOMINATOR);
}
DCHECK(!instr->hydrogen()->IsAllocationFolded());
if (instr->size()->IsConstantOperand()) {
int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
CHECK(size <= kMaxRegularHeapObjectSize);
__ Allocate(size, result, temp1, temp2, deferred->entry(), flags);
} else {
Register size = ToRegister32(instr->size());
__ Sxtw(size.X(), size);
__ Allocate(size.X(), result, temp1, temp2, deferred->entry(), flags);
}
__ Bind(deferred->exit());
if (instr->hydrogen()->MustPrefillWithFiller()) {
Register start = temp1;
Register end = temp2;
Register filler = ToRegister(instr->temp3());
__ Sub(start, result, kHeapObjectTag);
if (instr->size()->IsConstantOperand()) {
int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
__ Add(end, start, size);
} else {
__ Add(end, start, ToRegister(instr->size()));
}
__ LoadRoot(filler, Heap::kOnePointerFillerMapRootIndex);
__ InitializeFieldsWithFiller(start, end, filler);
} else {
DCHECK(instr->temp3() == NULL);
}
}
void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
// TODO(3095996): Get rid of this. For now, we need to make the
// result register contain a valid pointer because it is already
// contained in the register pointer map.
__ Mov(ToRegister(instr->result()), Smi::kZero);
PushSafepointRegistersScope scope(this);
LoadContextFromDeferred(instr->context());
// We're in a SafepointRegistersScope so we can use any scratch registers.
Register size = x0;
if (instr->size()->IsConstantOperand()) {
__ Mov(size, ToSmi(LConstantOperand::cast(instr->size())));
} else {
__ SmiTag(size, ToRegister32(instr->size()).X());
}
int flags = AllocateDoubleAlignFlag::encode(
instr->hydrogen()->MustAllocateDoubleAligned());
if (instr->hydrogen()->IsOldSpaceAllocation()) {
DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
flags = AllocateTargetSpace::update(flags, OLD_SPACE);
} else {
flags = AllocateTargetSpace::update(flags, NEW_SPACE);
}
__ Mov(x10, Smi::FromInt(flags));
__ Push(size, x10);
CallRuntimeFromDeferred(Runtime::kAllocateInTargetSpace, 2, instr, nullptr);
__ StoreToSafepointRegisterSlot(x0, ToRegister(instr->result()));
if (instr->hydrogen()->IsAllocationFoldingDominator()) {
AllocationFlags allocation_flags = NO_ALLOCATION_FLAGS;
if (instr->hydrogen()->IsOldSpaceAllocation()) {
DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
allocation_flags = static_cast<AllocationFlags>(flags | PRETENURE);
}
// If the allocation folding dominator allocate triggered a GC, allocation
// happend in the runtime. We have to reset the top pointer to virtually
// undo the allocation.
ExternalReference allocation_top =
AllocationUtils::GetAllocationTopReference(isolate(), allocation_flags);
Register top_address = x10;
__ Sub(x0, x0, Operand(kHeapObjectTag));
__ Mov(top_address, Operand(allocation_top));
__ Str(x0, MemOperand(top_address));
__ Add(x0, x0, Operand(kHeapObjectTag));
}
}
void LCodeGen::DoFastAllocate(LFastAllocate* instr) {
DCHECK(instr->hydrogen()->IsAllocationFolded());
DCHECK(!instr->hydrogen()->IsAllocationFoldingDominator());
Register result = ToRegister(instr->result());
Register scratch1 = ToRegister(instr->temp1());
Register scratch2 = ToRegister(instr->temp2());
AllocationFlags flags = ALLOCATION_FOLDED;
if (instr->hydrogen()->MustAllocateDoubleAligned()) {
flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
}
if (instr->hydrogen()->IsOldSpaceAllocation()) {
DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
flags = static_cast<AllocationFlags>(flags | PRETENURE);
}
if (instr->size()->IsConstantOperand()) {
int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
CHECK(size <= kMaxRegularHeapObjectSize);
__ FastAllocate(size, result, scratch1, scratch2, flags);
} else {
Register size = ToRegister(instr->size());
__ FastAllocate(size, result, scratch1, scratch2, flags);
}
}
void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
Register receiver = ToRegister(instr->receiver());
Register function = ToRegister(instr->function());
Register length = ToRegister32(instr->length());
Register elements = ToRegister(instr->elements());
Register scratch = x5;
DCHECK(receiver.Is(x0)); // Used for parameter count.
DCHECK(function.Is(x1)); // Required by InvokeFunction.
DCHECK(ToRegister(instr->result()).Is(x0));
DCHECK(instr->IsMarkedAsCall());
// Copy the arguments to this function possibly from the
// adaptor frame below it.
const uint32_t kArgumentsLimit = 1 * KB;
__ Cmp(length, kArgumentsLimit);
DeoptimizeIf(hi, instr, DeoptimizeReason::kTooManyArguments);
// Push the receiver and use the register to keep the original
// number of arguments.
__ Push(receiver);
Register argc = receiver;
receiver = NoReg;
__ Sxtw(argc, length);
// The arguments are at a one pointer size offset from elements.
__ Add(elements, elements, 1 * kPointerSize);
// Loop through the arguments pushing them onto the execution
// stack.
Label invoke, loop;
// length is a small non-negative integer, due to the test above.
__ Cbz(length, &invoke);
__ Bind(&loop);
__ Ldr(scratch, MemOperand(elements, length, SXTW, kPointerSizeLog2));
__ Push(scratch);
__ Subs(length, length, 1);
__ B(ne, &loop);
__ Bind(&invoke);
InvokeFlag flag = CALL_FUNCTION;
if (instr->hydrogen()->tail_call_mode() == TailCallMode::kAllow) {
DCHECK(!info()->saves_caller_doubles());
// TODO(ishell): drop current frame before pushing arguments to the stack.
flag = JUMP_FUNCTION;
ParameterCount actual(x0);
// It is safe to use x3, x4 and x5 as scratch registers here given that
// 1) we are not going to return to caller function anyway,
// 2) x3 (new.target) will be initialized below.
PrepareForTailCall(actual, x3, x4, x5);
}
DCHECK(instr->HasPointerMap());
LPointerMap* pointers = instr->pointer_map();
SafepointGenerator safepoint_generator(this, pointers, Safepoint::kLazyDeopt);
// The number of arguments is stored in argc (receiver) which is x0, as
// expected by InvokeFunction.
ParameterCount actual(argc);
__ InvokeFunction(function, no_reg, actual, flag, safepoint_generator);
}
void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
Register result = ToRegister(instr->result());
if (instr->hydrogen()->from_inlined()) {
// When we are inside an inlined function, the arguments are the last things
// that have been pushed on the stack. Therefore the arguments array can be
// accessed directly from jssp.
// However in the normal case, it is accessed via fp but there are two words
// on the stack between fp and the arguments (the saved lr and fp) and the
// LAccessArgumentsAt implementation take that into account.
// In the inlined case we need to subtract the size of 2 words to jssp to
// get a pointer which will work well with LAccessArgumentsAt.
DCHECK(masm()->StackPointer().Is(jssp));
__ Sub(result, jssp, 2 * kPointerSize);
} else if (instr->hydrogen()->arguments_adaptor()) {
DCHECK(instr->temp() != NULL);
Register previous_fp = ToRegister(instr->temp());
__ Ldr(previous_fp,
MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
__ Ldr(result, MemOperand(previous_fp,
CommonFrameConstants::kContextOrFrameTypeOffset));
__ Cmp(result, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
__ Csel(result, fp, previous_fp, ne);
} else {
__ Mov(result, fp);
}
}
void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
Register elements = ToRegister(instr->elements());
Register result = ToRegister32(instr->result());
Label done;
// If no arguments adaptor frame the number of arguments is fixed.
__ Cmp(fp, elements);
__ Mov(result, scope()->num_parameters());
__ B(eq, &done);
// Arguments adaptor frame present. Get argument length from there.
__ Ldr(result.X(), MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
__ Ldr(result,
UntagSmiMemOperand(result.X(),
ArgumentsAdaptorFrameConstants::kLengthOffset));
// Argument length is in result register.
__ Bind(&done);
}
void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
DoubleRegister left = ToDoubleRegister(instr->left());
DoubleRegister right = ToDoubleRegister(instr->right());
DoubleRegister result = ToDoubleRegister(instr->result());
switch (instr->op()) {
case Token::ADD: __ Fadd(result, left, right); break;
case Token::SUB: __ Fsub(result, left, right); break;
case Token::MUL: __ Fmul(result, left, right); break;
case Token::DIV: __ Fdiv(result, left, right); break;
case Token::MOD: {
// The ECMA-262 remainder operator is the remainder from a truncating
// (round-towards-zero) division. Note that this differs from IEEE-754.
//
// TODO(jbramley): See if it's possible to do this inline, rather than by
// calling a helper function. With frintz (to produce the intermediate
// quotient) and fmsub (to calculate the remainder without loss of
// precision), it should be possible. However, we would need support for
// fdiv in round-towards-zero mode, and the ARM64 simulator doesn't
// support that yet.
DCHECK(left.Is(d0));
DCHECK(right.Is(d1));
__ CallCFunction(
ExternalReference::mod_two_doubles_operation(isolate()),
0, 2);
DCHECK(result.Is(d0));
break;
}
default:
UNREACHABLE();
break;
}
}
void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
DCHECK(ToRegister(instr->context()).is(cp));
DCHECK(ToRegister(instr->left()).is(x1));
DCHECK(ToRegister(instr->right()).is(x0));
DCHECK(ToRegister(instr->result()).is(x0));
Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), instr->op()).code();
CallCode(code, RelocInfo::CODE_TARGET, instr);
}
void LCodeGen::DoBitI(LBitI* instr) {
Register result = ToRegister32(instr->result());
Register left = ToRegister32(instr->left());
Operand right = ToShiftedRightOperand32(instr->right(), instr);
switch (instr->op()) {
case Token::BIT_AND: __ And(result, left, right); break;
case Token::BIT_OR: __ Orr(result, left, right); break;
case Token::BIT_XOR: __ Eor(result, left, right); break;
default:
UNREACHABLE();
break;
}
}
void LCodeGen::DoBitS(LBitS* instr) {
Register result = ToRegister(instr->result());
Register left = ToRegister(instr->left());
Operand right = ToOperand(instr->right());
switch (instr->op()) {
case Token::BIT_AND: __ And(result, left, right); break;
case Token::BIT_OR: __ Orr(result, left, right); break;
case Token::BIT_XOR: __ Eor(result, left, right); break;
default:
UNREACHABLE();
break;
}
}
void LCodeGen::DoBoundsCheck(LBoundsCheck *instr) {
Condition cond = instr->hydrogen()->allow_equality() ? hi : hs;
DCHECK(instr->hydrogen()->index()->representation().IsInteger32());
DCHECK(instr->hydrogen()->length()->representation().IsInteger32());
if (instr->index()->IsConstantOperand()) {
Operand index = ToOperand32(instr->index());
Register length = ToRegister32(instr->length());
__ Cmp(length, index);
cond = CommuteCondition(cond);
} else {
Register index = ToRegister32(instr->index());
Operand length = ToOperand32(instr->length());
__ Cmp(index, length);
}
if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
__ Assert(NegateCondition(cond), kEliminatedBoundsCheckFailed);
} else {
DeoptimizeIf(cond, instr, DeoptimizeReason::kOutOfBounds);
}
}
void LCodeGen::DoBranch(LBranch* instr) {
Representation r = instr->hydrogen()->value()->representation();
Label* true_label = instr->TrueLabel(chunk_);
Label* false_label = instr->FalseLabel(chunk_);
if (r.IsInteger32()) {
DCHECK(!info()->IsStub());
EmitCompareAndBranch(instr, ne, ToRegister32(instr->value()), 0);
} else if (r.IsSmi()) {
DCHECK(!info()->IsStub());
STATIC_ASSERT(kSmiTag == 0);
EmitCompareAndBranch(instr, ne, ToRegister(instr->value()), 0);
} else if (r.IsDouble()) {
DoubleRegister value = ToDoubleRegister(instr->value());
// Test the double value. Zero and NaN are false.
EmitBranchIfNonZeroNumber(instr, value, double_scratch());
} else {
DCHECK(r.IsTagged());
Register value = ToRegister(instr->value());
HType type = instr->hydrogen()->value()->type();
if (type.IsBoolean()) {
DCHECK(!info()->IsStub());
__ CompareRoot(value, Heap::kTrueValueRootIndex);
EmitBranch(instr, eq);
} else if (type.IsSmi()) {
DCHECK(!info()->IsStub());
EmitCompareAndBranch(instr, ne, value, Smi::kZero);
} else if (type.IsJSArray()) {
DCHECK(!info()->IsStub());
EmitGoto(instr->TrueDestination(chunk()));
} else if (type.IsHeapNumber()) {
DCHECK(!info()->IsStub());
__ Ldr(double_scratch(), FieldMemOperand(value,
HeapNumber::kValueOffset));
// Test the double value. Zero and NaN are false.
EmitBranchIfNonZeroNumber(instr, double_scratch(), double_scratch());
} else if (type.IsString()) {
DCHECK(!info()->IsStub());
Register temp = ToRegister(instr->temp1());
__ Ldr(temp, FieldMemOperand(value, String::kLengthOffset));
EmitCompareAndBranch(instr, ne, temp, 0);
} else {
ToBooleanHints expected = instr->hydrogen()->expected_input_types();
// Avoid deopts in the case where we've never executed this path before.
if (expected == ToBooleanHint::kNone) expected = ToBooleanHint::kAny;
if (expected & ToBooleanHint::kUndefined) {
// undefined -> false.
__ JumpIfRoot(
value, Heap::kUndefinedValueRootIndex, false_label);
}
if (expected & ToBooleanHint::kBoolean) {
// Boolean -> its value.
__ JumpIfRoot(
value, Heap::kTrueValueRootIndex, true_label);
__ JumpIfRoot(
value, Heap::kFalseValueRootIndex, false_label);
}
if (expected & ToBooleanHint::kNull) {
// 'null' -> false.
__ JumpIfRoot(
value, Heap::kNullValueRootIndex, false_label);
}
if (expected & ToBooleanHint::kSmallInteger) {
// Smis: 0 -> false, all other -> true.
DCHECK(Smi::kZero == 0);
__ Cbz(value, false_label);
__ JumpIfSmi(value, true_label);
} else if (expected & ToBooleanHint::kNeedsMap) {
// If we need a map later and have a smi, deopt.
DeoptimizeIfSmi(value, instr, DeoptimizeReason::kSmi);
}
Register map = NoReg;
Register scratch = NoReg;
if (expected & ToBooleanHint::kNeedsMap) {
DCHECK((instr->temp1() != NULL) && (instr->temp2() != NULL));
map = ToRegister(instr->temp1());
scratch = ToRegister(instr->temp2());
__ Ldr(map, FieldMemOperand(value, HeapObject::kMapOffset));
if (expected & ToBooleanHint::kCanBeUndetectable) {
// Undetectable -> false.
__ Ldrb(scratch, FieldMemOperand(map, Map::kBitFieldOffset));
__ TestAndBranchIfAnySet(
scratch, 1 << Map::kIsUndetectable, false_label);
}
}
if (expected & ToBooleanHint::kReceiver) {
// spec object -> true.
__ CompareInstanceType(map, scratch, FIRST_JS_RECEIVER_TYPE);
__ B(ge, true_label);
}
if (expected & ToBooleanHint::kString) {
// String value -> false iff empty.
Label not_string;
__ CompareInstanceType(map, scratch, FIRST_NONSTRING_TYPE);
__ B(ge, &not_string);
__ Ldr(scratch, FieldMemOperand(value, String::kLengthOffset));
__ Cbz(scratch, false_label);
__ B(true_label);
__ Bind(&not_string);
}
if (expected & ToBooleanHint::kSymbol) {
// Symbol value -> true.
__ CompareInstanceType(map, scratch, SYMBOL_TYPE);
__ B(eq, true_label);
}
if (expected & ToBooleanHint::kSimdValue) {
// SIMD value -> true.
__ CompareInstanceType(map, scratch, SIMD128_VALUE_TYPE);
__ B(eq, true_label);
}
if (expected & ToBooleanHint::kHeapNumber) {
Label not_heap_number;
__ JumpIfNotRoot(map, Heap::kHeapNumberMapRootIndex, &not_heap_number);
__ Ldr(double_scratch(),
FieldMemOperand(value, HeapNumber::kValueOffset));
__ Fcmp(double_scratch(), 0.0);
// If we got a NaN (overflow bit is set), jump to the false branch.
__ B(vs, false_label);
__ B(eq, false_label);
__ B(true_label);
__ Bind(&not_heap_number);
}
if (expected != ToBooleanHint::kAny) {
// We've seen something for the first time -> deopt.
// This can only happen if we are not generic already.
Deoptimize(instr, DeoptimizeReason::kUnexpectedObject);
}
}
}
}
void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
int formal_parameter_count, int arity,
bool is_tail_call, LInstruction* instr) {
bool dont_adapt_arguments =
formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
bool can_invoke_directly =
dont_adapt_arguments || formal_parameter_count == arity;
// The function interface relies on the following register assignments.
Register function_reg = x1;
Register arity_reg = x0;
LPointerMap* pointers = instr->pointer_map();
if (FLAG_debug_code) {
Label is_not_smi;
// Try to confirm that function_reg (x1) is a tagged pointer.
__ JumpIfNotSmi(function_reg, &is_not_smi);
__ Abort(kExpectedFunctionObject);
__ Bind(&is_not_smi);
}
if (can_invoke_directly) {
// Change context.
__ Ldr(cp, FieldMemOperand(function_reg, JSFunction::kContextOffset));
// Always initialize new target and number of actual arguments.
__ LoadRoot(x3, Heap::kUndefinedValueRootIndex);
__ Mov(arity_reg, arity);
bool is_self_call = function.is_identical_to(info()->closure());
// Invoke function.
if (is_self_call) {
Handle<Code> self(reinterpret_cast<Code**>(__ CodeObject().location()));
if (is_tail_call) {
__ Jump(self, RelocInfo::CODE_TARGET);
} else {
__ Call(self, RelocInfo::CODE_TARGET);
}
} else {
__ Ldr(x10, FieldMemOperand(function_reg, JSFunction::kCodeEntryOffset));
if (is_tail_call) {
__ Jump(x10);
} else {
__ Call(x10);
}
}
if (!is_tail_call) {
// Set up deoptimization.
RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
}
} else {
SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
ParameterCount actual(arity);
ParameterCount expected(formal_parameter_count);
InvokeFlag flag = is_tail_call ? JUMP_FUNCTION : CALL_FUNCTION;
__ InvokeFunction(function_reg, expected, actual, flag, generator);
}
}
void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
DCHECK(instr->IsMarkedAsCall());
DCHECK(ToRegister(instr->result()).Is(x0));
if (instr->hydrogen()->IsTailCall()) {
if (NeedsEagerFrame()) __ LeaveFrame(StackFrame::INTERNAL);
if (instr->target()->IsConstantOperand()) {
LConstantOperand* target = LConstantOperand::cast(instr->target());
Handle<Code> code = Handle<Code>::cast(ToHandle(target));
// TODO(all): on ARM we use a call descriptor to specify a storage mode
// but on ARM64 we only have one storage mode so it isn't necessary. Check
// this understanding is correct.
__ Jump(code, RelocInfo::CODE_TARGET);
} else {
DCHECK(instr->target()->IsRegister());
Register target = ToRegister(instr->target());
__ Add(target, target, Code::kHeaderSize - kHeapObjectTag);
__ Br(target);
}
} else {
LPointerMap* pointers = instr->pointer_map();
SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
if (instr->target()->IsConstantOperand()) {
LConstantOperand* target = LConstantOperand::cast(instr->target());
Handle<Code> code = Handle<Code>::cast(ToHandle(target));
generator.BeforeCall(__ CallSize(code, RelocInfo::CODE_TARGET));
// TODO(all): on ARM we use a call descriptor to specify a storage mode
// but on ARM64 we only have one storage mode so it isn't necessary. Check
// this understanding is correct.
__ Call(code, RelocInfo::CODE_TARGET, TypeFeedbackId::None());
} else {
DCHECK(instr->target()->IsRegister());
Register target = ToRegister(instr->target());
generator.BeforeCall(__ CallSize(target));
__ Add(target, target, Code::kHeaderSize - kHeapObjectTag);
__ Call(target);
}
generator.AfterCall();
}
HCallWithDescriptor* hinstr = instr->hydrogen();
RecordPushedArgumentsDelta(hinstr->argument_delta());
// HCallWithDescriptor instruction is translated to zero or more
// LPushArguments (they handle parameters passed on the stack) followed by
// a LCallWithDescriptor. Each LPushArguments instruction generated records
// the number of arguments pushed thus we need to offset them here.
// The |argument_delta()| used above "knows" only about JS parameters while
// we are dealing here with particular calling convention details.
RecordPushedArgumentsDelta(-hinstr->descriptor().GetStackParameterCount());
}
void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
CallRuntime(instr->function(), instr->arity(), instr);
RecordPushedArgumentsDelta(instr->hydrogen()->argument_delta());
}
void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
GenerateOsrPrologue();
}
void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
Register temp = ToRegister(instr->temp());
{
PushSafepointRegistersScope scope(this);
__ Push(object);
__ Mov(cp, 0);
__ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
RecordSafepointWithRegisters(
instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
__ StoreToSafepointRegisterSlot(x0, temp);
}
DeoptimizeIfSmi(temp, instr, DeoptimizeReason::kInstanceMigrationFailed);
}
void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
class DeferredCheckMaps: public LDeferredCode {
public:
DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
: LDeferredCode(codegen), instr_(instr), object_(object) {
SetExit(check_maps());
}
virtual void Generate() {
codegen()->DoDeferredInstanceMigration(instr_, object_);
}
Label* check_maps() { return &check_maps_; }
virtual LInstruction* instr() { return instr_; }
private:
LCheckMaps* instr_;
Label check_maps_;
Register object_;
};
if (instr->hydrogen()->IsStabilityCheck()) {
const UniqueSet<Map>* maps = instr->hydrogen()->maps();
for (int i = 0; i < maps->size(); ++i) {
AddStabilityDependency(maps->at(i).handle());
}
return;
}
Register object = ToRegister(instr->value());
Register map_reg = ToRegister(instr->temp());
__ Ldr(map_reg, FieldMemOperand(object, HeapObject::kMapOffset));
DeferredCheckMaps* deferred = NULL;
if (instr->hydrogen()->HasMigrationTarget()) {
deferred = new(zone()) DeferredCheckMaps(this, instr, object);
__ Bind(deferred->check_maps());
}
const UniqueSet<Map>* maps = instr->hydrogen()->maps();
Label success;
for (int i = 0; i < maps->size() - 1; i++) {
Handle<Map> map = maps->at(i).handle();
__ CompareMap(map_reg, map);
__ B(eq, &success);
}
Handle<Map> map = maps->at(maps->size() - 1).handle();
__ CompareMap(map_reg, map);
// We didn't match a map.
if (instr->hydrogen()->HasMigrationTarget()) {
__ B(ne, deferred->entry());
} else {
DeoptimizeIf(ne, instr, DeoptimizeReason::kWrongMap);
}
__ Bind(&success);
}
void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
if (!instr->hydrogen()->value()->type().IsHeapObject()) {
DeoptimizeIfSmi(ToRegister(instr->value()), instr, DeoptimizeReason::kSmi);
}
}
void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
Register value = ToRegister(instr->value());
DCHECK(!instr->result() || ToRegister(instr->result()).Is(value));
DeoptimizeIfNotSmi(value, instr, DeoptimizeReason::kNotASmi);
}
void LCodeGen::DoCheckArrayBufferNotNeutered(
LCheckArrayBufferNotNeutered* instr) {
UseScratchRegisterScope temps(masm());
Register view = ToRegister(instr->view());
Register scratch = temps.AcquireX();
__ Ldr(scratch, FieldMemOperand(view, JSArrayBufferView::kBufferOffset));
__ Ldr(scratch, FieldMemOperand(scratch, JSArrayBuffer::kBitFieldOffset));
__ Tst(scratch, Operand(1 << JSArrayBuffer::WasNeutered::kShift));
DeoptimizeIf(ne, instr, DeoptimizeReason::kOutOfBounds);
}
void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
Register input = ToRegister(instr->value());
Register scratch = ToRegister(instr->temp());
__ Ldr(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
__ Ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
if (instr->hydrogen()->is_interval_check()) {
InstanceType first, last;
instr->hydrogen()->GetCheckInterval(&first, &last);
__ Cmp(scratch, first);
if (first == last) {
// If there is only one type in the interval check for equality.
DeoptimizeIf(ne, instr, DeoptimizeReason::kWrongInstanceType);
} else if (last == LAST_TYPE) {
// We don't need to compare with the higher bound of the interval.
DeoptimizeIf(lo, instr, DeoptimizeReason::kWrongInstanceType);
} else {
// If we are below the lower bound, set the C flag and clear the Z flag
// to force a deopt.
__ Ccmp(scratch, last, CFlag, hs);
DeoptimizeIf(hi, instr, DeoptimizeReason::kWrongInstanceType);
}
} else {
uint8_t mask;
uint8_t tag;
instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
if (base::bits::IsPowerOfTwo32(mask)) {
DCHECK((tag == 0) || (tag == mask));
if (tag == 0) {
DeoptimizeIfBitSet(scratch, MaskToBit(mask), instr,
DeoptimizeReason::kWrongInstanceType);
} else {
DeoptimizeIfBitClear(scratch, MaskToBit(mask), instr,
DeoptimizeReason::kWrongInstanceType);
}
} else {
if (tag == 0) {
__ Tst(scratch, mask);
} else {
__ And(scratch, scratch, mask);
__ Cmp(scratch, tag);
}
DeoptimizeIf(ne, instr, DeoptimizeReason::kWrongInstanceType);
}
}
}
void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
DoubleRegister input = ToDoubleRegister(instr->unclamped());
Register result = ToRegister32(instr->result());
__ ClampDoubleToUint8(result, input, double_scratch());
}
void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
Register input = ToRegister32(instr->unclamped());
Register result = ToRegister32(instr->result());
__ ClampInt32ToUint8(result, input);
}
void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
Register input = ToRegister(instr->unclamped());
Register result = ToRegister32(instr->result());
Label done;
// Both smi and heap number cases are handled.
Label is_not_smi;
__ JumpIfNotSmi(input, &is_not_smi);
__ SmiUntag(result.X(), input);
__ ClampInt32ToUint8(result);
__ B(&done);
__ Bind(&is_not_smi);
// Check for heap number.
Label is_heap_number;
__ JumpIfHeapNumber(input, &is_heap_number);
// Check for undefined. Undefined is coverted to zero for clamping conversion.
DeoptimizeIfNotRoot(input, Heap::kUndefinedValueRootIndex, instr,
DeoptimizeReason::kNotAHeapNumberUndefined);
__ Mov(result, 0);
__ B(&done);
// Heap number case.
__ Bind(&is_heap_number);
DoubleRegister dbl_scratch = double_scratch();
DoubleRegister dbl_scratch2 = ToDoubleRegister(instr->temp1());
__ Ldr(dbl_scratch, FieldMemOperand(input, HeapNumber::kValueOffset));
__ ClampDoubleToUint8(result, dbl_scratch, dbl_scratch2);
__ Bind(&done);
}
void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
Handle<String> class_name = instr->hydrogen()->class_name();
Label* true_label = instr->TrueLabel(chunk_);
Label* false_label = instr->FalseLabel(chunk_);
Register input = ToRegister(instr->value());
Register scratch1 = ToRegister(instr->temp1());
Register scratch2 = ToRegister(instr->temp2());
__ JumpIfSmi(input, false_label);
Register map = scratch2;
__ CompareObjectType(input, map, scratch1, FIRST_FUNCTION_TYPE);
STATIC_ASSERT(LAST_FUNCTION_TYPE == LAST_TYPE);
if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
__ B(hs, true_label);
} else {
__ B(hs, false_label);
}
// Check if the constructor in the map is a function.
{
UseScratchRegisterScope temps(masm());
Register instance_type = temps.AcquireX();
__ GetMapConstructor(scratch1, map, scratch2, instance_type);
__ Cmp(instance_type, JS_FUNCTION_TYPE);
}
// Objects with a non-function constructor have class 'Object'.
if (String::Equals(class_name, isolate()->factory()->Object_string())) {
__ B(ne, true_label);
} else {
__ B(ne, false_label);
}
// The constructor function is in scratch1. Get its instance class name.
__ Ldr(scratch1,
FieldMemOperand(scratch1, JSFunction::kSharedFunctionInfoOffset));
__ Ldr(scratch1,
FieldMemOperand(scratch1,
SharedFunctionInfo::kInstanceClassNameOffset));
// The class name we are testing against is internalized since it's a literal.
// The name in the constructor is internalized because of the way the context
// is booted. This routine isn't expected to work for random API-created
// classes and it doesn't have to because you can't access it with natives
// syntax. Since both sides are internalized it is sufficient to use an
// identity comparison.
EmitCompareAndBranch(instr, eq, scratch1, Operand(class_name));
}
void LCodeGen::DoCmpHoleAndBranchD(LCmpHoleAndBranchD* instr) {
DCHECK(instr->hydrogen()->representation().IsDouble());
FPRegister object = ToDoubleRegister(instr->object());
Register temp = ToRegister(instr->temp());
// If we don't have a NaN, we don't have the hole, so branch now to avoid the
// (relatively expensive) hole-NaN check.
__ Fcmp(object, object);
__ B(vc, instr->FalseLabel(chunk_));
// We have a NaN, but is it the hole?
__ Fmov(temp, object);
EmitCompareAndBranch(instr, eq, temp, kHoleNanInt64);
}
void LCodeGen::DoCmpHoleAndBranchT(LCmpHoleAndBranchT* instr) {
DCHECK(instr->hydrogen()->representation().IsTagged());
Register object = ToRegister(instr->object());
EmitBranchIfRoot(instr, object, Heap::kTheHoleValueRootIndex);
}
void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
Register value = ToRegister(instr->value());
Register map = ToRegister(instr->temp());
__ Ldr(map, FieldMemOperand(value, HeapObject::kMapOffset));
EmitCompareAndBranch(instr, eq, map, Operand(instr->map()));
}
void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
LOperand* left = instr->left();
LOperand* right = instr->right();
bool is_unsigned =
instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
Condition cond = TokenToCondition(instr->op(), is_unsigned);
if (left->IsConstantOperand() && right->IsConstantOperand()) {
// We can statically evaluate the comparison.
double left_val = ToDouble(LConstantOperand::cast(left));
double right_val = ToDouble(LConstantOperand::cast(right));
int next_block = Token::EvalComparison(instr->op(), left_val, right_val)
? instr->TrueDestination(chunk_)
: instr->FalseDestination(chunk_);
EmitGoto(next_block);
} else {
if (instr->is_double()) {
__ Fcmp(ToDoubleRegister(left), ToDoubleRegister(right));
// If a NaN is involved, i.e. the result is unordered (V set),
// jump to false block label.
__ B(vs, instr->FalseLabel(chunk_));
EmitBranch(instr, cond);
} else {
if (instr->hydrogen_value()->representation().IsInteger32()) {
if (right->IsConstantOperand()) {
EmitCompareAndBranch(instr, cond, ToRegister32(left),
ToOperand32(right));
} else {
// Commute the operands and the condition.
EmitCompareAndBranch(instr, CommuteCondition(cond),
ToRegister32(right), ToOperand32(left));
}
} else {
DCHECK(instr->hydrogen_value()->representation().IsSmi());
if (right->IsConstantOperand()) {
int32_t value = ToInteger32(LConstantOperand::cast(right));
EmitCompareAndBranch(instr,
cond,
ToRegister(left),
Operand(Smi::FromInt(value)));
} else if (left->IsConstantOperand()) {
// Commute the operands and the condition.
int32_t value = ToInteger32(LConstantOperand::cast(left));
EmitCompareAndBranch(instr,
CommuteCondition(cond),
ToRegister(right),
Operand(Smi::FromInt(value)));
} else {
EmitCompareAndBranch(instr,
cond,
ToRegister(left),
ToRegister(right));
}
}
}
}
}
void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
Register left = ToRegister(instr->left());
Register right = ToRegister(instr->right());
EmitCompareAndBranch(instr, eq, left, right);
}
void LCodeGen::DoCmpT(LCmpT* instr) {
DCHECK(ToRegister(instr->context()).is(cp));
Token::Value op = instr->op();
Condition cond = TokenToCondition(op, false);
DCHECK(ToRegister(instr->left()).Is(x1));
DCHECK(ToRegister(instr->right()).Is(x0));
Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
CallCode(ic, RelocInfo::CODE_TARGET, instr);
// Signal that we don't inline smi code before this stub.
InlineSmiCheckInfo::EmitNotInlined(masm());
// Return true or false depending on CompareIC result.
// This instruction is marked as call. We can clobber any register.
DCHECK(instr->IsMarkedAsCall());
__ LoadTrueFalseRoots(x1, x2);
__ Cmp(x0, 0);
__ Csel(ToRegister(instr->result()), x1, x2, cond);
}
void LCodeGen::DoConstantD(LConstantD* instr) {
DCHECK(instr->result()->IsDoubleRegister());
DoubleRegister result = ToDoubleRegister(instr->result());
if (instr->value() == 0) {
if (copysign(1.0, instr->value()) == 1.0) {
__ Fmov(result, fp_zero);
} else {
__ Fneg(result, fp_zero);
}
} else {
__ Fmov(result, instr->value());
}
}
void LCodeGen::DoConstantE(LConstantE* instr) {
__ Mov(ToRegister(instr->result()), Operand(instr->value()));
}
void LCodeGen::DoConstantI(LConstantI* instr) {
DCHECK(is_int32(instr->value()));
// Cast the value here to ensure that the value isn't sign extended by the
// implicit Operand constructor.
__ Mov(ToRegister32(instr->result()), static_cast<uint32_t>(instr->value()));
}
void LCodeGen::DoConstantS(LConstantS* instr) {
__ Mov(ToRegister(instr->result()), Operand(instr->value()));
}
void LCodeGen::DoConstantT(LConstantT* instr) {
Handle<Object> object = instr->value(isolate());
AllowDeferredHandleDereference smi_check;
__ LoadObject(ToRegister(instr->result()), object);
}
void LCodeGen::DoContext(LContext* instr) {
// If there is a non-return use, the context must be moved to a register.
Register result = ToRegister(instr->result());
if (info()->IsOptimizing()) {
__ Ldr(result, MemOperand(fp, StandardFrameConstants::kContextOffset));
} else {
// If there is no frame, the context must be in cp.
DCHECK(result.is(cp));
}
}
void LCodeGen::DoCheckValue(LCheckValue* instr) {
Register reg = ToRegister(instr->value());
Handle<HeapObject> object = instr->hydrogen()->object().handle();
AllowDeferredHandleDereference smi_check;
if (isolate()->heap()->InNewSpace(*object)) {
UseScratchRegisterScope temps(masm());
Register temp = temps.AcquireX();
Handle<Cell> cell = isolate()->factory()->NewCell(object);
__ Mov(temp, Operand(cell));
__ Ldr(temp, FieldMemOperand(temp, Cell::kValueOffset));
__ Cmp(reg, temp);
} else {
__ Cmp(reg, Operand(object));
}
DeoptimizeIf(ne, instr, DeoptimizeReason::kValueMismatch);
}
void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
last_lazy_deopt_pc_ = masm()->pc_offset();
DCHECK(instr->HasEnvironment());
LEnvironment* env = instr->environment();
RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
}
void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
Deoptimizer::BailoutType type = instr->hydrogen()->type();
// TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
// needed return address), even though the implementation of LAZY and EAGER is
// now identical. When LAZY is eventually completely folded into EAGER, remove
// the special case below.
if (info()->IsStub() && (type == Deoptimizer::EAGER)) {
type = Deoptimizer::LAZY;
}
Deoptimize(instr, instr->hydrogen()->reason(), &type);
}
void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
Register dividend = ToRegister32(instr->dividend());
int32_t divisor = instr->divisor();
Register result = ToRegister32(instr->result());
DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
DCHECK(!result.is(dividend));
// Check for (0 / -x) that will produce negative zero.
HDiv* hdiv = instr->hydrogen();
if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
DeoptimizeIfZero(dividend, instr, DeoptimizeReason::kDivisionByZero);
}
// Check for (kMinInt / -1).
if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
// Test dividend for kMinInt by subtracting one (cmp) and checking for
// overflow.
__ Cmp(dividend, 1);
DeoptimizeIf(vs, instr, DeoptimizeReason::kOverflow);
}
// Deoptimize if remainder will not be 0.
if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
divisor != 1 && divisor != -1) {
int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
__ Tst(dividend, mask);
DeoptimizeIf(ne, instr, DeoptimizeReason::kLostPrecision);
}
if (divisor == -1) { // Nice shortcut, not needed for correctness.
__ Neg(result, dividend);
return;
}
int32_t shift = WhichPowerOf2Abs(divisor);
if (shift == 0) {
__ Mov(result, dividend);
} else if (shift == 1) {
__ Add(result, dividend, Operand(dividend, LSR, 31));
} else {
__ Mov(result, Operand(dividend, ASR, 31));
__ Add(result, dividend, Operand(result, LSR, 32 - shift));
}
if (shift > 0) __ Mov(result, Operand(result, ASR, shift));
if (divisor < 0) __ Neg(result, result);
}
void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
Register dividend = ToRegister32(instr->dividend());
int32_t divisor = instr->divisor();
Register result = ToRegister32(instr->result());
DCHECK(!AreAliased(dividend, result));
if (divisor == 0) {
Deoptimize(instr, DeoptimizeReason::kDivisionByZero);
return;
}
// Check for (0 / -x) that will produce negative zero.
HDiv* hdiv = instr->hydrogen();
if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
DeoptimizeIfZero(dividend, instr, DeoptimizeReason::kMinusZero);
}
__ TruncatingDiv(result, dividend, Abs(divisor));
if (divisor < 0) __ Neg(result, result);
if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
Register temp = ToRegister32(instr->temp());
DCHECK(!AreAliased(dividend, result, temp));
__ Sxtw(dividend.X(), dividend);
__ Mov(temp, divisor);
__ Smsubl(temp.X(), result, temp, dividend.X());
DeoptimizeIfNotZero(temp, instr, DeoptimizeReason::kLostPrecision);
}
}
// TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
void LCodeGen::DoDivI(LDivI* instr) {
HBinaryOperation* hdiv = instr->hydrogen();
Register dividend = ToRegister32(instr->dividend());
Register divisor = ToRegister32(instr->divisor());
Register result = ToRegister32(instr->result());
// Issue the division first, and then check for any deopt cases whilst the
// result is computed.
__ Sdiv(result, dividend, divisor);
if (hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
DCHECK(!instr->temp());
return;
}
// Check for x / 0.
if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
DeoptimizeIfZero(divisor, instr, DeoptimizeReason::kDivisionByZero);
}
// Check for (0 / -x) as that will produce negative zero.
if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
__ Cmp(divisor, 0);
// If the divisor < 0 (mi), compare the dividend, and deopt if it is
// zero, ie. zero dividend with negative divisor deopts.
// If the divisor >= 0 (pl, the opposite of mi) set the flags to
// condition ne, so we don't deopt, ie. positive divisor doesn't deopt.
__ Ccmp(dividend, 0, NoFlag, mi);
DeoptimizeIf(eq, instr, DeoptimizeReason::kMinusZero);
}
// Check for (kMinInt / -1).
if (hdiv->CheckFlag(HValue::kCanOverflow)) {
// Test dividend for kMinInt by subtracting one (cmp) and checking for
// overflow.
__ Cmp(dividend, 1);
// If overflow is set, ie. dividend = kMinInt, compare the divisor with
// -1. If overflow is clear, set the flags for condition ne, as the
// dividend isn't -1, and thus we shouldn't deopt.
__ Ccmp(divisor, -1, NoFlag, vs);
DeoptimizeIf(eq, instr, DeoptimizeReason::kOverflow);
}
// Compute remainder and deopt if it's not zero.
Register remainder = ToRegister32(instr->temp());
__ Msub(remainder, result, divisor, dividend);
DeoptimizeIfNotZero(remainder, instr, DeoptimizeReason::kLostPrecision);
}
void LCodeGen::DoDoubleToIntOrSmi(LDoubleToIntOrSmi* instr) {
DoubleRegister input = ToDoubleRegister(instr->value());
Register result = ToRegister32(instr->result());
if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
DeoptimizeIfMinusZero(input, instr, DeoptimizeReason::kMinusZero);
}
__ TryRepresentDoubleAsInt32(result, input, double_scratch());
DeoptimizeIf(ne, instr, DeoptimizeReason::kLostPrecisionOrNaN);
if (instr->tag_result()) {
__ SmiTag(result.X());
}
}
void LCodeGen::DoDrop(LDrop* instr) {
__ Drop(instr->count());
RecordPushedArgumentsDelta(instr->hydrogen_value()->argument_delta());
}
void LCodeGen::DoDummy(LDummy* instr) {
// Nothing to see here, move on!
}
void LCodeGen::DoDummyUse(LDummyUse* instr) {
// Nothing to see here, move on!
}
void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
Register map = ToRegister(instr->map());
Register result = ToRegister(instr->result());
Label load_cache, done;
__ EnumLengthUntagged(result, map);
__ Cbnz(result, &load_cache);
__ Mov(result, Operand(isolate()->factory()->empty_fixed_array()));
__ B(&done);
__ Bind(&load_cache);
__ LoadInstanceDescriptors(map, result);
__ Ldr(result, FieldMemOperand(result, DescriptorArray::kEnumCacheOffset));
__ Ldr(result, FieldMemOperand(result, FixedArray::SizeFor(instr->idx())));
DeoptimizeIfZero(result, instr, DeoptimizeReason::kNoCache);
__ Bind(&done);
}
void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
Register object = ToRegister(instr->object());
DCHECK(instr->IsMarkedAsCall());
DCHECK(object.Is(x0));
Label use_cache, call_runtime;
__ CheckEnumCache(object, x5, x1, x2, x3, x4, &call_runtime);
__ Ldr(object, FieldMemOperand(object, HeapObject::kMapOffset));
__ B(&use_cache);
// Get the set of properties to enumerate.
__ Bind(&call_runtime);
__ Push(object);
CallRuntime(Runtime::kForInEnumerate, instr);
__ Bind(&use_cache);
}
void LCodeGen::EmitGoto(int block) {
// Do not emit jump if we are emitting a goto to the next block.
if (!IsNextEmittedBlock(block)) {
__ B(chunk_->GetAssemblyLabel(LookupDestination(block)));
}
}
void LCodeGen::DoGoto(LGoto* instr) {
EmitGoto(instr->block_id());
}
// HHasInstanceTypeAndBranch instruction is built with an interval of type
// to test but is only used in very restricted ways. The only possible kinds
// of intervals are:
// - [ FIRST_TYPE, instr->to() ]
// - [ instr->form(), LAST_TYPE ]
// - instr->from() == instr->to()
//
// These kinds of intervals can be check with only one compare instruction
// providing the correct value and test condition are used.
//
// TestType() will return the value to use in the compare instruction and
// BranchCondition() will return the condition to use depending on the kind
// of interval actually specified in the instruction.
static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
InstanceType from = instr->from();
InstanceType to = instr->to();
if (from == FIRST_TYPE) return to;
DCHECK((from == to) || (to == LAST_TYPE));
return from;
}
// See comment above TestType function for what this function does.
static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
InstanceType from = instr->from();
InstanceType to = instr->to();
if (from == to) return eq;
if (to == LAST_TYPE) return hs;
if (from == FIRST_TYPE) return ls;
UNREACHABLE();
return eq;
}
void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
Register input = ToRegister(instr->value());
Register scratch = ToRegister(instr->temp());
if (!instr->hydrogen()->value()->type().IsHeapObject()) {
__ JumpIfSmi(input, instr->FalseLabel(chunk_));
}
__ CompareObjectType(input, scratch, scratch, TestType(instr->hydrogen()));
EmitBranch(instr, BranchCondition(instr->hydrogen()));
}
void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
Register result = ToRegister(instr->result());
Register base = ToRegister(instr->base_object());
if (instr->offset()->IsConstantOperand()) {
__ Add(result, base, ToOperand32(instr->offset()));
} else {
__ Add(result, base, Operand(ToRegister32(instr->offset()), SXTW));
}
}
void LCodeGen::DoHasInPrototypeChainAndBranch(
LHasInPrototypeChainAndBranch* instr) {
Register const object = ToRegister(instr->object());
Register const object_map = ToRegister(instr->scratch1());
Register const object_instance_type = ToRegister(instr->scratch2());
Register const object_prototype = objec