1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "src/arm64/frames-arm64.h"
6 #include "src/arm64/lithium-codegen-arm64.h"
7 #include "src/arm64/lithium-gap-resolver-arm64.h"
8 #include "src/base/bits.h"
9 #include "src/code-factory.h"
10 #include "src/code-stubs.h"
11 #include "src/cpu-profiler.h"
12 #include "src/hydrogen-osr.h"
13 #include "src/ic/ic.h"
14 #include "src/ic/stub-cache.h"
20 class SafepointGenerator final : public CallWrapper {
22 SafepointGenerator(LCodeGen* codegen,
23 LPointerMap* pointers,
24 Safepoint::DeoptMode mode)
28 virtual ~SafepointGenerator() { }
30 virtual void BeforeCall(int call_size) const { }
32 virtual void AfterCall() const {
33 codegen_->RecordSafepoint(pointers_, deopt_mode_);
38 LPointerMap* pointers_;
39 Safepoint::DeoptMode deopt_mode_;
45 // Emit code to branch if the given condition holds.
46 // The code generated here doesn't modify the flags and they must have
47 // been set by some prior instructions.
49 // The EmitInverted function simply inverts the condition.
50 class BranchOnCondition : public BranchGenerator {
52 BranchOnCondition(LCodeGen* codegen, Condition cond)
53 : BranchGenerator(codegen),
56 virtual void Emit(Label* label) const {
60 virtual void EmitInverted(Label* label) const {
62 __ B(NegateCondition(cond_), label);
71 // Emit code to compare lhs and rhs and branch if the condition holds.
72 // This uses MacroAssembler's CompareAndBranch function so it will handle
73 // converting the comparison to Cbz/Cbnz if the right-hand side is 0.
75 // EmitInverted still compares the two operands but inverts the condition.
76 class CompareAndBranch : public BranchGenerator {
78 CompareAndBranch(LCodeGen* codegen,
82 : BranchGenerator(codegen),
87 virtual void Emit(Label* label) const {
88 __ CompareAndBranch(lhs_, rhs_, cond_, label);
91 virtual void EmitInverted(Label* label) const {
92 __ CompareAndBranch(lhs_, rhs_, NegateCondition(cond_), label);
102 // Test the input with the given mask and branch if the condition holds.
103 // If the condition is 'eq' or 'ne' this will use MacroAssembler's
104 // TestAndBranchIfAllClear and TestAndBranchIfAnySet so it will handle the
105 // conversion to Tbz/Tbnz when possible.
106 class TestAndBranch : public BranchGenerator {
108 TestAndBranch(LCodeGen* codegen,
110 const Register& value,
112 : BranchGenerator(codegen),
117 virtual void Emit(Label* label) const {
120 __ TestAndBranchIfAllClear(value_, mask_, label);
123 __ TestAndBranchIfAnySet(value_, mask_, label);
126 __ Tst(value_, mask_);
131 virtual void EmitInverted(Label* label) const {
132 // The inverse of "all clear" is "any set" and vice versa.
135 __ TestAndBranchIfAnySet(value_, mask_, label);
138 __ TestAndBranchIfAllClear(value_, mask_, label);
141 __ Tst(value_, mask_);
142 __ B(NegateCondition(cond_), label);
148 const Register& value_;
153 // Test the input and branch if it is non-zero and not a NaN.
154 class BranchIfNonZeroNumber : public BranchGenerator {
156 BranchIfNonZeroNumber(LCodeGen* codegen, const FPRegister& value,
157 const FPRegister& scratch)
158 : BranchGenerator(codegen), value_(value), scratch_(scratch) { }
160 virtual void Emit(Label* label) const {
161 __ Fabs(scratch_, value_);
162 // Compare with 0.0. Because scratch_ is positive, the result can be one of
163 // nZCv (equal), nzCv (greater) or nzCV (unordered).
164 __ Fcmp(scratch_, 0.0);
168 virtual void EmitInverted(Label* label) const {
169 __ Fabs(scratch_, value_);
170 __ Fcmp(scratch_, 0.0);
175 const FPRegister& value_;
176 const FPRegister& scratch_;
180 // Test the input and branch if it is a heap number.
181 class BranchIfHeapNumber : public BranchGenerator {
183 BranchIfHeapNumber(LCodeGen* codegen, const Register& value)
184 : BranchGenerator(codegen), value_(value) { }
186 virtual void Emit(Label* label) const {
187 __ JumpIfHeapNumber(value_, label);
190 virtual void EmitInverted(Label* label) const {
191 __ JumpIfNotHeapNumber(value_, label);
195 const Register& value_;
199 // Test the input and branch if it is the specified root value.
200 class BranchIfRoot : public BranchGenerator {
202 BranchIfRoot(LCodeGen* codegen, const Register& value,
203 Heap::RootListIndex index)
204 : BranchGenerator(codegen), value_(value), index_(index) { }
206 virtual void Emit(Label* label) const {
207 __ JumpIfRoot(value_, index_, label);
210 virtual void EmitInverted(Label* label) const {
211 __ JumpIfNotRoot(value_, index_, label);
215 const Register& value_;
216 const Heap::RootListIndex index_;
220 void LCodeGen::WriteTranslation(LEnvironment* environment,
221 Translation* translation) {
222 if (environment == NULL) return;
224 // The translation includes one command per value in the environment.
225 int translation_size = environment->translation_size();
227 WriteTranslation(environment->outer(), translation);
228 WriteTranslationFrame(environment, translation);
230 int object_index = 0;
231 int dematerialized_index = 0;
232 for (int i = 0; i < translation_size; ++i) {
233 LOperand* value = environment->values()->at(i);
235 environment, translation, value, environment->HasTaggedValueAt(i),
236 environment->HasUint32ValueAt(i), &object_index, &dematerialized_index);
241 void LCodeGen::AddToTranslation(LEnvironment* environment,
242 Translation* translation,
246 int* object_index_pointer,
247 int* dematerialized_index_pointer) {
248 if (op == LEnvironment::materialization_marker()) {
249 int object_index = (*object_index_pointer)++;
250 if (environment->ObjectIsDuplicateAt(object_index)) {
251 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
252 translation->DuplicateObject(dupe_of);
255 int object_length = environment->ObjectLengthAt(object_index);
256 if (environment->ObjectIsArgumentsAt(object_index)) {
257 translation->BeginArgumentsObject(object_length);
259 translation->BeginCapturedObject(object_length);
261 int dematerialized_index = *dematerialized_index_pointer;
262 int env_offset = environment->translation_size() + dematerialized_index;
263 *dematerialized_index_pointer += object_length;
264 for (int i = 0; i < object_length; ++i) {
265 LOperand* value = environment->values()->at(env_offset + i);
266 AddToTranslation(environment,
269 environment->HasTaggedValueAt(env_offset + i),
270 environment->HasUint32ValueAt(env_offset + i),
271 object_index_pointer,
272 dematerialized_index_pointer);
277 if (op->IsStackSlot()) {
278 int index = op->index();
280 index += StandardFrameConstants::kFixedFrameSize / kPointerSize;
283 translation->StoreStackSlot(index);
284 } else if (is_uint32) {
285 translation->StoreUint32StackSlot(index);
287 translation->StoreInt32StackSlot(index);
289 } else if (op->IsDoubleStackSlot()) {
290 int index = op->index();
292 index += StandardFrameConstants::kFixedFrameSize / kPointerSize;
294 translation->StoreDoubleStackSlot(index);
295 } else if (op->IsRegister()) {
296 Register reg = ToRegister(op);
298 translation->StoreRegister(reg);
299 } else if (is_uint32) {
300 translation->StoreUint32Register(reg);
302 translation->StoreInt32Register(reg);
304 } else if (op->IsDoubleRegister()) {
305 DoubleRegister reg = ToDoubleRegister(op);
306 translation->StoreDoubleRegister(reg);
307 } else if (op->IsConstantOperand()) {
308 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
309 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
310 translation->StoreLiteral(src_index);
317 void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
318 Safepoint::DeoptMode mode) {
319 environment->set_has_been_used();
320 if (!environment->HasBeenRegistered()) {
322 int jsframe_count = 0;
323 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
325 if (e->frame_type() == JS_FUNCTION) {
329 Translation translation(&translations_, frame_count, jsframe_count, zone());
330 WriteTranslation(environment, &translation);
331 int deoptimization_index = deoptimizations_.length();
332 int pc_offset = masm()->pc_offset();
333 environment->Register(deoptimization_index,
335 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
336 deoptimizations_.Add(environment, zone());
341 void LCodeGen::CallCode(Handle<Code> code,
342 RelocInfo::Mode mode,
343 LInstruction* instr) {
344 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT);
348 void LCodeGen::CallCodeGeneric(Handle<Code> code,
349 RelocInfo::Mode mode,
351 SafepointMode safepoint_mode) {
352 DCHECK(instr != NULL);
354 Assembler::BlockPoolsScope scope(masm_);
356 RecordSafepointWithLazyDeopt(instr, safepoint_mode);
358 if ((code->kind() == Code::BINARY_OP_IC) ||
359 (code->kind() == Code::COMPARE_IC)) {
360 // Signal that we don't inline smi code before these stubs in the
361 // optimizing code generator.
362 InlineSmiCheckInfo::EmitNotInlined(masm());
367 void LCodeGen::DoCallFunction(LCallFunction* instr) {
368 DCHECK(ToRegister(instr->context()).is(cp));
369 DCHECK(ToRegister(instr->function()).Is(x1));
370 DCHECK(ToRegister(instr->result()).Is(x0));
372 int arity = instr->arity();
373 CallFunctionFlags flags = instr->hydrogen()->function_flags();
374 if (instr->hydrogen()->HasVectorAndSlot()) {
375 Register slot_register = ToRegister(instr->temp_slot());
376 Register vector_register = ToRegister(instr->temp_vector());
377 DCHECK(slot_register.is(x3));
378 DCHECK(vector_register.is(x2));
380 AllowDeferredHandleDereference vector_structure_check;
381 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
382 int index = vector->GetIndex(instr->hydrogen()->slot());
384 __ Mov(vector_register, vector);
385 __ Mov(slot_register, Operand(Smi::FromInt(index)));
387 CallICState::CallType call_type =
388 (flags & CALL_AS_METHOD) ? CallICState::METHOD : CallICState::FUNCTION;
391 CodeFactory::CallICInOptimizedCode(isolate(), arity, call_type).code();
392 CallCode(ic, RelocInfo::CODE_TARGET, instr);
394 CallFunctionStub stub(isolate(), arity, flags);
395 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
397 RecordPushedArgumentsDelta(instr->hydrogen()->argument_delta());
401 void LCodeGen::DoCallNew(LCallNew* instr) {
402 DCHECK(ToRegister(instr->context()).is(cp));
403 DCHECK(instr->IsMarkedAsCall());
404 DCHECK(ToRegister(instr->constructor()).is(x1));
406 __ Mov(x0, instr->arity());
407 // No cell in x2 for construct type feedback in optimized code.
408 __ LoadRoot(x2, Heap::kUndefinedValueRootIndex);
410 CallConstructStub stub(isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
411 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
412 RecordPushedArgumentsDelta(instr->hydrogen()->argument_delta());
414 DCHECK(ToRegister(instr->result()).is(x0));
418 void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
419 DCHECK(instr->IsMarkedAsCall());
420 DCHECK(ToRegister(instr->context()).is(cp));
421 DCHECK(ToRegister(instr->constructor()).is(x1));
423 __ Mov(x0, Operand(instr->arity()));
424 if (instr->arity() == 1) {
425 // We only need the allocation site for the case we have a length argument.
426 // The case may bail out to the runtime, which will determine the correct
427 // elements kind with the site.
428 __ Mov(x2, instr->hydrogen()->site());
430 __ LoadRoot(x2, Heap::kUndefinedValueRootIndex);
434 ElementsKind kind = instr->hydrogen()->elements_kind();
435 AllocationSiteOverrideMode override_mode =
436 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
437 ? DISABLE_ALLOCATION_SITES
440 if (instr->arity() == 0) {
441 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
442 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
443 } else if (instr->arity() == 1) {
445 if (IsFastPackedElementsKind(kind)) {
448 // We might need to create a holey array; look at the first argument.
450 __ Cbz(x10, &packed_case);
452 ElementsKind holey_kind = GetHoleyElementsKind(kind);
453 ArraySingleArgumentConstructorStub stub(isolate(),
456 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
458 __ Bind(&packed_case);
461 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
462 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
465 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
466 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
468 RecordPushedArgumentsDelta(instr->hydrogen()->argument_delta());
470 DCHECK(ToRegister(instr->result()).is(x0));
474 void LCodeGen::CallRuntime(const Runtime::Function* function,
477 SaveFPRegsMode save_doubles) {
478 DCHECK(instr != NULL);
480 __ CallRuntime(function, num_arguments, save_doubles);
482 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
486 void LCodeGen::LoadContextFromDeferred(LOperand* context) {
487 if (context->IsRegister()) {
488 __ Mov(cp, ToRegister(context));
489 } else if (context->IsStackSlot()) {
490 __ Ldr(cp, ToMemOperand(context, kMustUseFramePointer));
491 } else if (context->IsConstantOperand()) {
492 HConstant* constant =
493 chunk_->LookupConstant(LConstantOperand::cast(context));
494 __ LoadHeapObject(cp,
495 Handle<HeapObject>::cast(constant->handle(isolate())));
502 void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
506 LoadContextFromDeferred(context);
507 __ CallRuntimeSaveDoubles(id);
508 RecordSafepointWithRegisters(
509 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
513 void LCodeGen::RecordAndWritePosition(int position) {
514 if (position == RelocInfo::kNoPosition) return;
515 masm()->positions_recorder()->RecordPosition(position);
516 masm()->positions_recorder()->WriteRecordedPositions();
520 void LCodeGen::RecordSafepointWithLazyDeopt(LInstruction* instr,
521 SafepointMode safepoint_mode) {
522 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
523 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
525 DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
526 RecordSafepointWithRegisters(
527 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
532 void LCodeGen::RecordSafepoint(LPointerMap* pointers,
533 Safepoint::Kind kind,
535 Safepoint::DeoptMode deopt_mode) {
536 DCHECK(expected_safepoint_kind_ == kind);
538 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
539 Safepoint safepoint = safepoints_.DefineSafepoint(
540 masm(), kind, arguments, deopt_mode);
542 for (int i = 0; i < operands->length(); i++) {
543 LOperand* pointer = operands->at(i);
544 if (pointer->IsStackSlot()) {
545 safepoint.DefinePointerSlot(pointer->index(), zone());
546 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
547 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
552 void LCodeGen::RecordSafepoint(LPointerMap* pointers,
553 Safepoint::DeoptMode deopt_mode) {
554 RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
558 void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
559 LPointerMap empty_pointers(zone());
560 RecordSafepoint(&empty_pointers, deopt_mode);
564 void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
566 Safepoint::DeoptMode deopt_mode) {
567 RecordSafepoint(pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
571 bool LCodeGen::GenerateCode() {
572 LPhase phase("Z_Code generation", chunk());
574 status_ = GENERATING;
576 // Open a frame scope to indicate that there is a frame on the stack. The
577 // NONE indicates that the scope shouldn't actually generate code to set up
578 // the frame (that is done in GeneratePrologue).
579 FrameScope frame_scope(masm_, StackFrame::NONE);
581 return GeneratePrologue() && GenerateBody() && GenerateDeferredCode() &&
582 GenerateJumpTable() && GenerateSafepointTable();
586 void LCodeGen::SaveCallerDoubles() {
587 DCHECK(info()->saves_caller_doubles());
588 DCHECK(NeedsEagerFrame());
589 Comment(";;; Save clobbered callee double registers");
590 BitVector* doubles = chunk()->allocated_double_registers();
591 BitVector::Iterator iterator(doubles);
593 while (!iterator.Done()) {
594 // TODO(all): Is this supposed to save just the callee-saved doubles? It
595 // looks like it's saving all of them.
596 FPRegister value = FPRegister::FromAllocationIndex(iterator.Current());
597 __ Poke(value, count * kDoubleSize);
604 void LCodeGen::RestoreCallerDoubles() {
605 DCHECK(info()->saves_caller_doubles());
606 DCHECK(NeedsEagerFrame());
607 Comment(";;; Restore clobbered callee double registers");
608 BitVector* doubles = chunk()->allocated_double_registers();
609 BitVector::Iterator iterator(doubles);
611 while (!iterator.Done()) {
612 // TODO(all): Is this supposed to restore just the callee-saved doubles? It
613 // looks like it's restoring all of them.
614 FPRegister value = FPRegister::FromAllocationIndex(iterator.Current());
615 __ Peek(value, count * kDoubleSize);
622 bool LCodeGen::GeneratePrologue() {
623 DCHECK(is_generating());
625 if (info()->IsOptimizing()) {
626 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
628 // TODO(all): Add support for stop_t FLAG in DEBUG mode.
630 // Sloppy mode functions and builtins need to replace the receiver with the
631 // global proxy when called as functions (without an explicit receiver
633 if (info()->MustReplaceUndefinedReceiverWithGlobalProxy()) {
635 int receiver_offset = info_->scope()->num_parameters() * kXRegSize;
636 __ Peek(x10, receiver_offset);
637 __ JumpIfNotRoot(x10, Heap::kUndefinedValueRootIndex, &ok);
639 __ Ldr(x10, GlobalObjectMemOperand());
640 __ Ldr(x10, FieldMemOperand(x10, GlobalObject::kGlobalProxyOffset));
641 __ Poke(x10, receiver_offset);
647 DCHECK(__ StackPointer().Is(jssp));
648 info()->set_prologue_offset(masm_->pc_offset());
649 if (NeedsEagerFrame()) {
650 if (info()->IsStub()) {
653 __ Prologue(info()->IsCodePreAgingActive());
655 frame_is_built_ = true;
656 info_->AddNoFrameRange(0, masm_->pc_offset());
659 // Reserve space for the stack slots needed by the code.
660 int slots = GetStackSlotCount();
662 __ Claim(slots, kPointerSize);
665 if (info()->saves_caller_doubles()) {
668 return !is_aborted();
672 void LCodeGen::DoPrologue(LPrologue* instr) {
673 Comment(";;; Prologue begin");
675 // Allocate a local context if needed.
676 if (info()->num_heap_slots() > 0) {
677 Comment(";;; Allocate local context");
678 bool need_write_barrier = true;
679 // Argument to NewContext is the function, which is in x1.
680 int slots = info()->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
681 Safepoint::DeoptMode deopt_mode = Safepoint::kNoLazyDeopt;
682 if (info()->scope()->is_script_scope()) {
683 __ Mov(x10, Operand(info()->scope()->GetScopeInfo(info()->isolate())));
685 __ CallRuntime(Runtime::kNewScriptContext, 2);
686 deopt_mode = Safepoint::kLazyDeopt;
687 } else if (slots <= FastNewContextStub::kMaximumSlots) {
688 FastNewContextStub stub(isolate(), slots);
690 // Result of FastNewContextStub is always in new space.
691 need_write_barrier = false;
694 __ CallRuntime(Runtime::kNewFunctionContext, 1);
696 RecordSafepoint(deopt_mode);
697 // Context is returned in x0. It replaces the context passed to us. It's
698 // saved in the stack and kept live in cp.
700 __ Str(x0, MemOperand(fp, StandardFrameConstants::kContextOffset));
701 // Copy any necessary parameters into the context.
702 int num_parameters = scope()->num_parameters();
703 int first_parameter = scope()->has_this_declaration() ? -1 : 0;
704 for (int i = first_parameter; i < num_parameters; i++) {
705 Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
706 if (var->IsContextSlot()) {
708 Register scratch = x3;
710 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
711 (num_parameters - 1 - i) * kPointerSize;
712 // Load parameter from stack.
713 __ Ldr(value, MemOperand(fp, parameter_offset));
714 // Store it in the context.
715 MemOperand target = ContextMemOperand(cp, var->index());
716 __ Str(value, target);
717 // Update the write barrier. This clobbers value and scratch.
718 if (need_write_barrier) {
719 __ RecordWriteContextSlot(cp, static_cast<int>(target.offset()),
720 value, scratch, GetLinkRegisterState(),
722 } else if (FLAG_debug_code) {
724 __ JumpIfInNewSpace(cp, &done);
725 __ Abort(kExpectedNewSpaceObject);
730 Comment(";;; End allocate local context");
733 Comment(";;; Prologue end");
737 void LCodeGen::GenerateOsrPrologue() {
738 // Generate the OSR entry prologue at the first unknown OSR value, or if there
739 // are none, at the OSR entrypoint instruction.
740 if (osr_pc_offset_ >= 0) return;
742 osr_pc_offset_ = masm()->pc_offset();
744 // Adjust the frame size, subsuming the unoptimized frame into the
746 int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
752 void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
753 if (instr->IsCall()) {
754 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
756 if (!instr->IsLazyBailout() && !instr->IsGap()) {
757 safepoints_.BumpLastLazySafepointIndex();
762 bool LCodeGen::GenerateDeferredCode() {
763 DCHECK(is_generating());
764 if (deferred_.length() > 0) {
765 for (int i = 0; !is_aborted() && (i < deferred_.length()); i++) {
766 LDeferredCode* code = deferred_[i];
769 instructions_->at(code->instruction_index())->hydrogen_value();
770 RecordAndWritePosition(
771 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
773 Comment(";;; <@%d,#%d> "
774 "-------------------- Deferred %s --------------------",
775 code->instruction_index(),
776 code->instr()->hydrogen_value()->id(),
777 code->instr()->Mnemonic());
779 __ Bind(code->entry());
781 if (NeedsDeferredFrame()) {
782 Comment(";;; Build frame");
783 DCHECK(!frame_is_built_);
784 DCHECK(info()->IsStub());
785 frame_is_built_ = true;
787 __ Mov(fp, Smi::FromInt(StackFrame::STUB));
789 __ Add(fp, __ StackPointer(),
790 StandardFrameConstants::kFixedFrameSizeFromFp);
791 Comment(";;; Deferred code");
796 if (NeedsDeferredFrame()) {
797 Comment(";;; Destroy frame");
798 DCHECK(frame_is_built_);
799 __ Pop(xzr, cp, fp, lr);
800 frame_is_built_ = false;
807 // Force constant pool emission at the end of the deferred code to make
808 // sure that no constant pools are emitted after deferred code because
809 // deferred code generation is the last step which generates code. The two
810 // following steps will only output data used by crakshaft.
811 masm()->CheckConstPool(true, false);
813 return !is_aborted();
817 bool LCodeGen::GenerateJumpTable() {
818 Label needs_frame, call_deopt_entry;
820 if (jump_table_.length() > 0) {
821 Comment(";;; -------------------- Jump table --------------------");
822 Address base = jump_table_[0]->address;
824 UseScratchRegisterScope temps(masm());
825 Register entry_offset = temps.AcquireX();
827 int length = jump_table_.length();
828 for (int i = 0; i < length; i++) {
829 Deoptimizer::JumpTableEntry* table_entry = jump_table_[i];
830 __ Bind(&table_entry->label);
832 Address entry = table_entry->address;
833 DeoptComment(table_entry->deopt_info);
835 // Second-level deopt table entries are contiguous and small, so instead
836 // of loading the full, absolute address of each one, load the base
837 // address and add an immediate offset.
838 __ Mov(entry_offset, entry - base);
840 if (table_entry->needs_frame) {
841 DCHECK(!info()->saves_caller_doubles());
842 Comment(";;; call deopt with frame");
843 // Save lr before Bl, fp will be adjusted in the needs_frame code.
845 // Reuse the existing needs_frame code.
848 // There is nothing special to do, so just continue to the second-level
850 __ Bl(&call_deopt_entry);
852 info()->LogDeoptCallPosition(masm()->pc_offset(),
853 table_entry->deopt_info.inlining_id);
855 masm()->CheckConstPool(false, false);
858 if (needs_frame.is_linked()) {
859 // This variant of deopt can only be used with stubs. Since we don't
860 // have a function pointer to install in the stack frame that we're
861 // building, install a special marker there instead.
862 DCHECK(info()->IsStub());
864 Comment(";;; needs_frame common code");
865 UseScratchRegisterScope temps(masm());
866 Register stub_marker = temps.AcquireX();
867 __ Bind(&needs_frame);
868 __ Mov(stub_marker, Smi::FromInt(StackFrame::STUB));
869 __ Push(cp, stub_marker);
870 __ Add(fp, __ StackPointer(), 2 * kPointerSize);
873 // Generate common code for calling the second-level deopt table.
874 __ Bind(&call_deopt_entry);
876 if (info()->saves_caller_doubles()) {
877 DCHECK(info()->IsStub());
878 RestoreCallerDoubles();
881 Register deopt_entry = temps.AcquireX();
882 __ Mov(deopt_entry, Operand(reinterpret_cast<uint64_t>(base),
883 RelocInfo::RUNTIME_ENTRY));
884 __ Add(deopt_entry, deopt_entry, entry_offset);
888 // Force constant pool emission at the end of the deopt jump table to make
889 // sure that no constant pools are emitted after.
890 masm()->CheckConstPool(true, false);
892 // The deoptimization jump table is the last part of the instruction
893 // sequence. Mark the generated code as done unless we bailed out.
894 if (!is_aborted()) status_ = DONE;
895 return !is_aborted();
899 bool LCodeGen::GenerateSafepointTable() {
901 // We do not know how much data will be emitted for the safepoint table, so
902 // force emission of the veneer pool.
903 masm()->CheckVeneerPool(true, true);
904 safepoints_.Emit(masm(), GetStackSlotCount());
905 return !is_aborted();
909 void LCodeGen::FinishCode(Handle<Code> code) {
911 code->set_stack_slots(GetStackSlotCount());
912 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
913 PopulateDeoptimizationData(code);
917 void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
918 int length = deoptimizations_.length();
919 if (length == 0) return;
921 Handle<DeoptimizationInputData> data =
922 DeoptimizationInputData::New(isolate(), length, TENURED);
924 Handle<ByteArray> translations =
925 translations_.CreateByteArray(isolate()->factory());
926 data->SetTranslationByteArray(*translations);
927 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
928 data->SetOptimizationId(Smi::FromInt(info_->optimization_id()));
929 if (info_->IsOptimizing()) {
930 // Reference to shared function info does not change between phases.
931 AllowDeferredHandleDereference allow_handle_dereference;
932 data->SetSharedFunctionInfo(*info_->shared_info());
934 data->SetSharedFunctionInfo(Smi::FromInt(0));
936 data->SetWeakCellCache(Smi::FromInt(0));
938 Handle<FixedArray> literals =
939 factory()->NewFixedArray(deoptimization_literals_.length(), TENURED);
940 { AllowDeferredHandleDereference copy_handles;
941 for (int i = 0; i < deoptimization_literals_.length(); i++) {
942 literals->set(i, *deoptimization_literals_[i]);
944 data->SetLiteralArray(*literals);
947 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id().ToInt()));
948 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
950 // Populate the deoptimization entries.
951 for (int i = 0; i < length; i++) {
952 LEnvironment* env = deoptimizations_[i];
953 data->SetAstId(i, env->ast_id());
954 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
955 data->SetArgumentsStackHeight(i,
956 Smi::FromInt(env->arguments_stack_height()));
957 data->SetPc(i, Smi::FromInt(env->pc_offset()));
960 code->set_deoptimization_data(*data);
964 void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
965 DCHECK_EQ(0, deoptimization_literals_.length());
966 for (auto function : chunk()->inlined_functions()) {
967 DefineDeoptimizationLiteral(function);
969 inlined_function_count_ = deoptimization_literals_.length();
973 void LCodeGen::DeoptimizeBranch(
974 LInstruction* instr, Deoptimizer::DeoptReason deopt_reason,
975 BranchType branch_type, Register reg, int bit,
976 Deoptimizer::BailoutType* override_bailout_type) {
977 LEnvironment* environment = instr->environment();
978 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
979 Deoptimizer::BailoutType bailout_type =
980 info()->IsStub() ? Deoptimizer::LAZY : Deoptimizer::EAGER;
982 if (override_bailout_type != NULL) {
983 bailout_type = *override_bailout_type;
986 DCHECK(environment->HasBeenRegistered());
987 int id = environment->deoptimization_index();
989 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
992 Abort(kBailoutWasNotPrepared);
995 if (FLAG_deopt_every_n_times != 0 && !info()->IsStub()) {
997 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
1002 __ Ldr(w1, MemOperand(x0));
1004 __ B(gt, ¬_zero);
1005 __ Mov(w1, FLAG_deopt_every_n_times);
1006 __ Str(w1, MemOperand(x0));
1008 DCHECK(frame_is_built_);
1009 __ Call(entry, RelocInfo::RUNTIME_ENTRY);
1013 __ Str(w1, MemOperand(x0));
1018 if (info()->ShouldTrapOnDeopt()) {
1020 __ B(&dont_trap, InvertBranchType(branch_type), reg, bit);
1021 __ Debug("trap_on_deopt", __LINE__, BREAK);
1022 __ Bind(&dont_trap);
1025 Deoptimizer::DeoptInfo deopt_info = MakeDeoptInfo(instr, deopt_reason);
1027 DCHECK(info()->IsStub() || frame_is_built_);
1028 // Go through jump table if we need to build frame, or restore caller doubles.
1029 if (branch_type == always &&
1030 frame_is_built_ && !info()->saves_caller_doubles()) {
1031 DeoptComment(deopt_info);
1032 __ Call(entry, RelocInfo::RUNTIME_ENTRY);
1033 info()->LogDeoptCallPosition(masm()->pc_offset(), deopt_info.inlining_id);
1035 Deoptimizer::JumpTableEntry* table_entry =
1036 new (zone()) Deoptimizer::JumpTableEntry(
1037 entry, deopt_info, bailout_type, !frame_is_built_);
1038 // We often have several deopts to the same entry, reuse the last
1039 // jump entry if this is the case.
1040 if (FLAG_trace_deopt || isolate()->cpu_profiler()->is_profiling() ||
1041 jump_table_.is_empty() ||
1042 !table_entry->IsEquivalentTo(*jump_table_.last())) {
1043 jump_table_.Add(table_entry, zone());
1045 __ B(&jump_table_.last()->label, branch_type, reg, bit);
1050 void LCodeGen::Deoptimize(LInstruction* instr,
1051 Deoptimizer::DeoptReason deopt_reason,
1052 Deoptimizer::BailoutType* override_bailout_type) {
1053 DeoptimizeBranch(instr, deopt_reason, always, NoReg, -1,
1054 override_bailout_type);
1058 void LCodeGen::DeoptimizeIf(Condition cond, LInstruction* instr,
1059 Deoptimizer::DeoptReason deopt_reason) {
1060 DeoptimizeBranch(instr, deopt_reason, static_cast<BranchType>(cond));
1064 void LCodeGen::DeoptimizeIfZero(Register rt, LInstruction* instr,
1065 Deoptimizer::DeoptReason deopt_reason) {
1066 DeoptimizeBranch(instr, deopt_reason, reg_zero, rt);
1070 void LCodeGen::DeoptimizeIfNotZero(Register rt, LInstruction* instr,
1071 Deoptimizer::DeoptReason deopt_reason) {
1072 DeoptimizeBranch(instr, deopt_reason, reg_not_zero, rt);
1076 void LCodeGen::DeoptimizeIfNegative(Register rt, LInstruction* instr,
1077 Deoptimizer::DeoptReason deopt_reason) {
1078 int sign_bit = rt.Is64Bits() ? kXSignBit : kWSignBit;
1079 DeoptimizeIfBitSet(rt, sign_bit, instr, deopt_reason);
1083 void LCodeGen::DeoptimizeIfSmi(Register rt, LInstruction* instr,
1084 Deoptimizer::DeoptReason deopt_reason) {
1085 DeoptimizeIfBitClear(rt, MaskToBit(kSmiTagMask), instr, deopt_reason);
1089 void LCodeGen::DeoptimizeIfNotSmi(Register rt, LInstruction* instr,
1090 Deoptimizer::DeoptReason deopt_reason) {
1091 DeoptimizeIfBitSet(rt, MaskToBit(kSmiTagMask), instr, deopt_reason);
1095 void LCodeGen::DeoptimizeIfRoot(Register rt, Heap::RootListIndex index,
1096 LInstruction* instr,
1097 Deoptimizer::DeoptReason deopt_reason) {
1098 __ CompareRoot(rt, index);
1099 DeoptimizeIf(eq, instr, deopt_reason);
1103 void LCodeGen::DeoptimizeIfNotRoot(Register rt, Heap::RootListIndex index,
1104 LInstruction* instr,
1105 Deoptimizer::DeoptReason deopt_reason) {
1106 __ CompareRoot(rt, index);
1107 DeoptimizeIf(ne, instr, deopt_reason);
1111 void LCodeGen::DeoptimizeIfMinusZero(DoubleRegister input, LInstruction* instr,
1112 Deoptimizer::DeoptReason deopt_reason) {
1113 __ TestForMinusZero(input);
1114 DeoptimizeIf(vs, instr, deopt_reason);
1118 void LCodeGen::DeoptimizeIfNotHeapNumber(Register object, LInstruction* instr) {
1119 __ CompareObjectMap(object, Heap::kHeapNumberMapRootIndex);
1120 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber);
1124 void LCodeGen::DeoptimizeIfBitSet(Register rt, int bit, LInstruction* instr,
1125 Deoptimizer::DeoptReason deopt_reason) {
1126 DeoptimizeBranch(instr, deopt_reason, reg_bit_set, rt, bit);
1130 void LCodeGen::DeoptimizeIfBitClear(Register rt, int bit, LInstruction* instr,
1131 Deoptimizer::DeoptReason deopt_reason) {
1132 DeoptimizeBranch(instr, deopt_reason, reg_bit_clear, rt, bit);
1136 void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
1137 if (info()->ShouldEnsureSpaceForLazyDeopt()) {
1138 // Ensure that we have enough space after the previous lazy-bailout
1139 // instruction for patching the code here.
1140 intptr_t current_pc = masm()->pc_offset();
1142 if (current_pc < (last_lazy_deopt_pc_ + space_needed)) {
1143 ptrdiff_t padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
1144 DCHECK((padding_size % kInstructionSize) == 0);
1145 InstructionAccurateScope instruction_accurate(
1146 masm(), padding_size / kInstructionSize);
1148 while (padding_size > 0) {
1150 padding_size -= kInstructionSize;
1154 last_lazy_deopt_pc_ = masm()->pc_offset();
1158 Register LCodeGen::ToRegister(LOperand* op) const {
1159 // TODO(all): support zero register results, as ToRegister32.
1160 DCHECK((op != NULL) && op->IsRegister());
1161 return Register::FromAllocationIndex(op->index());
1165 Register LCodeGen::ToRegister32(LOperand* op) const {
1167 if (op->IsConstantOperand()) {
1168 // If this is a constant operand, the result must be the zero register.
1169 DCHECK(ToInteger32(LConstantOperand::cast(op)) == 0);
1172 return ToRegister(op).W();
1177 Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
1178 HConstant* constant = chunk_->LookupConstant(op);
1179 return Smi::FromInt(constant->Integer32Value());
1183 DoubleRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
1184 DCHECK((op != NULL) && op->IsDoubleRegister());
1185 return DoubleRegister::FromAllocationIndex(op->index());
1189 Operand LCodeGen::ToOperand(LOperand* op) {
1191 if (op->IsConstantOperand()) {
1192 LConstantOperand* const_op = LConstantOperand::cast(op);
1193 HConstant* constant = chunk()->LookupConstant(const_op);
1194 Representation r = chunk_->LookupLiteralRepresentation(const_op);
1196 DCHECK(constant->HasSmiValue());
1197 return Operand(Smi::FromInt(constant->Integer32Value()));
1198 } else if (r.IsInteger32()) {
1199 DCHECK(constant->HasInteger32Value());
1200 return Operand(constant->Integer32Value());
1201 } else if (r.IsDouble()) {
1202 Abort(kToOperandUnsupportedDoubleImmediate);
1204 DCHECK(r.IsTagged());
1205 return Operand(constant->handle(isolate()));
1206 } else if (op->IsRegister()) {
1207 return Operand(ToRegister(op));
1208 } else if (op->IsDoubleRegister()) {
1209 Abort(kToOperandIsDoubleRegisterUnimplemented);
1212 // Stack slots not implemented, use ToMemOperand instead.
1218 Operand LCodeGen::ToOperand32(LOperand* op) {
1220 if (op->IsRegister()) {
1221 return Operand(ToRegister32(op));
1222 } else if (op->IsConstantOperand()) {
1223 LConstantOperand* const_op = LConstantOperand::cast(op);
1224 HConstant* constant = chunk()->LookupConstant(const_op);
1225 Representation r = chunk_->LookupLiteralRepresentation(const_op);
1226 if (r.IsInteger32()) {
1227 return Operand(constant->Integer32Value());
1229 // Other constants not implemented.
1230 Abort(kToOperand32UnsupportedImmediate);
1233 // Other cases are not implemented.
1239 static int64_t ArgumentsOffsetWithoutFrame(int index) {
1241 return -(index + 1) * kPointerSize;
1245 MemOperand LCodeGen::ToMemOperand(LOperand* op, StackMode stack_mode) const {
1247 DCHECK(!op->IsRegister());
1248 DCHECK(!op->IsDoubleRegister());
1249 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
1250 if (NeedsEagerFrame()) {
1251 int fp_offset = StackSlotOffset(op->index());
1252 // Loads and stores have a bigger reach in positive offset than negative.
1253 // We try to access using jssp (positive offset) first, then fall back to
1254 // fp (negative offset) if that fails.
1256 // We can reference a stack slot from jssp only if we know how much we've
1257 // put on the stack. We don't know this in the following cases:
1258 // - stack_mode != kCanUseStackPointer: this is the case when deferred
1259 // code has saved the registers.
1260 // - saves_caller_doubles(): some double registers have been pushed, jssp
1261 // references the end of the double registers and not the end of the stack
1263 // In both of the cases above, we _could_ add the tracking information
1264 // required so that we can use jssp here, but in practice it isn't worth it.
1265 if ((stack_mode == kCanUseStackPointer) &&
1266 !info()->saves_caller_doubles()) {
1267 int jssp_offset_to_fp =
1268 StandardFrameConstants::kFixedFrameSizeFromFp +
1269 (pushed_arguments_ + GetStackSlotCount()) * kPointerSize;
1270 int jssp_offset = fp_offset + jssp_offset_to_fp;
1271 if (masm()->IsImmLSScaled(jssp_offset, LSDoubleWord)) {
1272 return MemOperand(masm()->StackPointer(), jssp_offset);
1275 return MemOperand(fp, fp_offset);
1277 // Retrieve parameter without eager stack-frame relative to the
1279 return MemOperand(masm()->StackPointer(),
1280 ArgumentsOffsetWithoutFrame(op->index()));
1285 Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
1286 HConstant* constant = chunk_->LookupConstant(op);
1287 DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
1288 return constant->handle(isolate());
1293 Operand LCodeGen::ToShiftedRightOperand32(LOperand* right, LI* shift_info) {
1294 if (shift_info->shift() == NO_SHIFT) {
1295 return ToOperand32(right);
1298 ToRegister32(right),
1299 shift_info->shift(),
1300 JSShiftAmountFromLConstant(shift_info->shift_amount()));
1305 bool LCodeGen::IsSmi(LConstantOperand* op) const {
1306 return chunk_->LookupLiteralRepresentation(op).IsSmi();
1310 bool LCodeGen::IsInteger32Constant(LConstantOperand* op) const {
1311 return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
1315 int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
1316 HConstant* constant = chunk_->LookupConstant(op);
1317 return constant->Integer32Value();
1321 double LCodeGen::ToDouble(LConstantOperand* op) const {
1322 HConstant* constant = chunk_->LookupConstant(op);
1323 DCHECK(constant->HasDoubleValue());
1324 return constant->DoubleValue();
1328 Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
1329 Condition cond = nv;
1332 case Token::EQ_STRICT:
1336 case Token::NE_STRICT:
1340 cond = is_unsigned ? lo : lt;
1343 cond = is_unsigned ? hi : gt;
1346 cond = is_unsigned ? ls : le;
1349 cond = is_unsigned ? hs : ge;
1352 case Token::INSTANCEOF:
1360 template<class InstrType>
1361 void LCodeGen::EmitBranchGeneric(InstrType instr,
1362 const BranchGenerator& branch) {
1363 int left_block = instr->TrueDestination(chunk_);
1364 int right_block = instr->FalseDestination(chunk_);
1366 int next_block = GetNextEmittedBlock();
1368 if (right_block == left_block) {
1369 EmitGoto(left_block);
1370 } else if (left_block == next_block) {
1371 branch.EmitInverted(chunk_->GetAssemblyLabel(right_block));
1373 branch.Emit(chunk_->GetAssemblyLabel(left_block));
1374 if (right_block != next_block) {
1375 __ B(chunk_->GetAssemblyLabel(right_block));
1381 template<class InstrType>
1382 void LCodeGen::EmitBranch(InstrType instr, Condition condition) {
1383 DCHECK((condition != al) && (condition != nv));
1384 BranchOnCondition branch(this, condition);
1385 EmitBranchGeneric(instr, branch);
1389 template<class InstrType>
1390 void LCodeGen::EmitCompareAndBranch(InstrType instr,
1391 Condition condition,
1392 const Register& lhs,
1393 const Operand& rhs) {
1394 DCHECK((condition != al) && (condition != nv));
1395 CompareAndBranch branch(this, condition, lhs, rhs);
1396 EmitBranchGeneric(instr, branch);
1400 template<class InstrType>
1401 void LCodeGen::EmitTestAndBranch(InstrType instr,
1402 Condition condition,
1403 const Register& value,
1405 DCHECK((condition != al) && (condition != nv));
1406 TestAndBranch branch(this, condition, value, mask);
1407 EmitBranchGeneric(instr, branch);
1411 template<class InstrType>
1412 void LCodeGen::EmitBranchIfNonZeroNumber(InstrType instr,
1413 const FPRegister& value,
1414 const FPRegister& scratch) {
1415 BranchIfNonZeroNumber branch(this, value, scratch);
1416 EmitBranchGeneric(instr, branch);
1420 template<class InstrType>
1421 void LCodeGen::EmitBranchIfHeapNumber(InstrType instr,
1422 const Register& value) {
1423 BranchIfHeapNumber branch(this, value);
1424 EmitBranchGeneric(instr, branch);
1428 template<class InstrType>
1429 void LCodeGen::EmitBranchIfRoot(InstrType instr,
1430 const Register& value,
1431 Heap::RootListIndex index) {
1432 BranchIfRoot branch(this, value, index);
1433 EmitBranchGeneric(instr, branch);
1437 void LCodeGen::DoGap(LGap* gap) {
1438 for (int i = LGap::FIRST_INNER_POSITION;
1439 i <= LGap::LAST_INNER_POSITION;
1441 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
1442 LParallelMove* move = gap->GetParallelMove(inner_pos);
1444 resolver_.Resolve(move);
1450 void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
1451 Register arguments = ToRegister(instr->arguments());
1452 Register result = ToRegister(instr->result());
1454 // The pointer to the arguments array come from DoArgumentsElements.
1455 // It does not point directly to the arguments and there is an offest of
1456 // two words that we must take into account when accessing an argument.
1457 // Subtracting the index from length accounts for one, so we add one more.
1459 if (instr->length()->IsConstantOperand() &&
1460 instr->index()->IsConstantOperand()) {
1461 int index = ToInteger32(LConstantOperand::cast(instr->index()));
1462 int length = ToInteger32(LConstantOperand::cast(instr->length()));
1463 int offset = ((length - index) + 1) * kPointerSize;
1464 __ Ldr(result, MemOperand(arguments, offset));
1465 } else if (instr->index()->IsConstantOperand()) {
1466 Register length = ToRegister32(instr->length());
1467 int index = ToInteger32(LConstantOperand::cast(instr->index()));
1468 int loc = index - 1;
1470 __ Sub(result.W(), length, loc);
1471 __ Ldr(result, MemOperand(arguments, result, UXTW, kPointerSizeLog2));
1473 __ Ldr(result, MemOperand(arguments, length, UXTW, kPointerSizeLog2));
1476 Register length = ToRegister32(instr->length());
1477 Operand index = ToOperand32(instr->index());
1478 __ Sub(result.W(), length, index);
1479 __ Add(result.W(), result.W(), 1);
1480 __ Ldr(result, MemOperand(arguments, result, UXTW, kPointerSizeLog2));
1485 void LCodeGen::DoAddE(LAddE* instr) {
1486 Register result = ToRegister(instr->result());
1487 Register left = ToRegister(instr->left());
1488 Operand right = Operand(x0); // Dummy initialization.
1489 if (instr->hydrogen()->external_add_type() == AddOfExternalAndTagged) {
1490 right = Operand(ToRegister(instr->right()));
1491 } else if (instr->right()->IsConstantOperand()) {
1492 right = ToInteger32(LConstantOperand::cast(instr->right()));
1494 right = Operand(ToRegister32(instr->right()), SXTW);
1497 DCHECK(!instr->hydrogen()->CheckFlag(HValue::kCanOverflow));
1498 __ Add(result, left, right);
1502 void LCodeGen::DoAddI(LAddI* instr) {
1503 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1504 Register result = ToRegister32(instr->result());
1505 Register left = ToRegister32(instr->left());
1506 Operand right = ToShiftedRightOperand32(instr->right(), instr);
1509 __ Adds(result, left, right);
1510 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
1512 __ Add(result, left, right);
1517 void LCodeGen::DoAddS(LAddS* instr) {
1518 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1519 Register result = ToRegister(instr->result());
1520 Register left = ToRegister(instr->left());
1521 Operand right = ToOperand(instr->right());
1523 __ Adds(result, left, right);
1524 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
1526 __ Add(result, left, right);
1531 void LCodeGen::DoAllocate(LAllocate* instr) {
1532 class DeferredAllocate: public LDeferredCode {
1534 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
1535 : LDeferredCode(codegen), instr_(instr) { }
1536 virtual void Generate() { codegen()->DoDeferredAllocate(instr_); }
1537 virtual LInstruction* instr() { return instr_; }
1542 DeferredAllocate* deferred = new(zone()) DeferredAllocate(this, instr);
1544 Register result = ToRegister(instr->result());
1545 Register temp1 = ToRegister(instr->temp1());
1546 Register temp2 = ToRegister(instr->temp2());
1548 // Allocate memory for the object.
1549 AllocationFlags flags = TAG_OBJECT;
1550 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
1551 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
1554 if (instr->hydrogen()->IsOldSpaceAllocation()) {
1555 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
1556 flags = static_cast<AllocationFlags>(flags | PRETENURE);
1559 if (instr->size()->IsConstantOperand()) {
1560 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
1561 if (size <= Page::kMaxRegularHeapObjectSize) {
1562 __ Allocate(size, result, temp1, temp2, deferred->entry(), flags);
1564 __ B(deferred->entry());
1567 Register size = ToRegister32(instr->size());
1568 __ Sxtw(size.X(), size);
1569 __ Allocate(size.X(), result, temp1, temp2, deferred->entry(), flags);
1572 __ Bind(deferred->exit());
1574 if (instr->hydrogen()->MustPrefillWithFiller()) {
1575 Register filler_count = temp1;
1576 Register filler = temp2;
1577 Register untagged_result = ToRegister(instr->temp3());
1579 if (instr->size()->IsConstantOperand()) {
1580 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
1581 __ Mov(filler_count, size / kPointerSize);
1583 __ Lsr(filler_count.W(), ToRegister32(instr->size()), kPointerSizeLog2);
1586 __ Sub(untagged_result, result, kHeapObjectTag);
1587 __ Mov(filler, Operand(isolate()->factory()->one_pointer_filler_map()));
1588 __ FillFields(untagged_result, filler_count, filler);
1590 DCHECK(instr->temp3() == NULL);
1595 void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
1596 // TODO(3095996): Get rid of this. For now, we need to make the
1597 // result register contain a valid pointer because it is already
1598 // contained in the register pointer map.
1599 __ Mov(ToRegister(instr->result()), Smi::FromInt(0));
1601 PushSafepointRegistersScope scope(this);
1602 // We're in a SafepointRegistersScope so we can use any scratch registers.
1604 if (instr->size()->IsConstantOperand()) {
1605 __ Mov(size, ToSmi(LConstantOperand::cast(instr->size())));
1607 __ SmiTag(size, ToRegister32(instr->size()).X());
1609 int flags = AllocateDoubleAlignFlag::encode(
1610 instr->hydrogen()->MustAllocateDoubleAligned());
1611 if (instr->hydrogen()->IsOldSpaceAllocation()) {
1612 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
1613 flags = AllocateTargetSpace::update(flags, OLD_SPACE);
1615 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
1617 __ Mov(x10, Smi::FromInt(flags));
1620 CallRuntimeFromDeferred(
1621 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
1622 __ StoreToSafepointRegisterSlot(x0, ToRegister(instr->result()));
1626 void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
1627 Register receiver = ToRegister(instr->receiver());
1628 Register function = ToRegister(instr->function());
1629 Register length = ToRegister32(instr->length());
1631 Register elements = ToRegister(instr->elements());
1632 Register scratch = x5;
1633 DCHECK(receiver.Is(x0)); // Used for parameter count.
1634 DCHECK(function.Is(x1)); // Required by InvokeFunction.
1635 DCHECK(ToRegister(instr->result()).Is(x0));
1636 DCHECK(instr->IsMarkedAsCall());
1638 // Copy the arguments to this function possibly from the
1639 // adaptor frame below it.
1640 const uint32_t kArgumentsLimit = 1 * KB;
1641 __ Cmp(length, kArgumentsLimit);
1642 DeoptimizeIf(hi, instr, Deoptimizer::kTooManyArguments);
1644 // Push the receiver and use the register to keep the original
1645 // number of arguments.
1647 Register argc = receiver;
1649 __ Sxtw(argc, length);
1650 // The arguments are at a one pointer size offset from elements.
1651 __ Add(elements, elements, 1 * kPointerSize);
1653 // Loop through the arguments pushing them onto the execution
1656 // length is a small non-negative integer, due to the test above.
1657 __ Cbz(length, &invoke);
1659 __ Ldr(scratch, MemOperand(elements, length, SXTW, kPointerSizeLog2));
1661 __ Subs(length, length, 1);
1665 DCHECK(instr->HasPointerMap());
1666 LPointerMap* pointers = instr->pointer_map();
1667 SafepointGenerator safepoint_generator(this, pointers, Safepoint::kLazyDeopt);
1668 // The number of arguments is stored in argc (receiver) which is x0, as
1669 // expected by InvokeFunction.
1670 ParameterCount actual(argc);
1671 __ InvokeFunction(function, actual, CALL_FUNCTION, safepoint_generator);
1675 void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
1676 Register result = ToRegister(instr->result());
1678 if (instr->hydrogen()->from_inlined()) {
1679 // When we are inside an inlined function, the arguments are the last things
1680 // that have been pushed on the stack. Therefore the arguments array can be
1681 // accessed directly from jssp.
1682 // However in the normal case, it is accessed via fp but there are two words
1683 // on the stack between fp and the arguments (the saved lr and fp) and the
1684 // LAccessArgumentsAt implementation take that into account.
1685 // In the inlined case we need to subtract the size of 2 words to jssp to
1686 // get a pointer which will work well with LAccessArgumentsAt.
1687 DCHECK(masm()->StackPointer().Is(jssp));
1688 __ Sub(result, jssp, 2 * kPointerSize);
1690 DCHECK(instr->temp() != NULL);
1691 Register previous_fp = ToRegister(instr->temp());
1694 MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1696 MemOperand(previous_fp, StandardFrameConstants::kContextOffset));
1697 __ Cmp(result, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
1698 __ Csel(result, fp, previous_fp, ne);
1703 void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
1704 Register elements = ToRegister(instr->elements());
1705 Register result = ToRegister32(instr->result());
1708 // If no arguments adaptor frame the number of arguments is fixed.
1709 __ Cmp(fp, elements);
1710 __ Mov(result, scope()->num_parameters());
1713 // Arguments adaptor frame present. Get argument length from there.
1714 __ Ldr(result.X(), MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1716 UntagSmiMemOperand(result.X(),
1717 ArgumentsAdaptorFrameConstants::kLengthOffset));
1719 // Argument length is in result register.
1724 void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
1725 DoubleRegister left = ToDoubleRegister(instr->left());
1726 DoubleRegister right = ToDoubleRegister(instr->right());
1727 DoubleRegister result = ToDoubleRegister(instr->result());
1729 switch (instr->op()) {
1730 case Token::ADD: __ Fadd(result, left, right); break;
1731 case Token::SUB: __ Fsub(result, left, right); break;
1732 case Token::MUL: __ Fmul(result, left, right); break;
1733 case Token::DIV: __ Fdiv(result, left, right); break;
1735 // The ECMA-262 remainder operator is the remainder from a truncating
1736 // (round-towards-zero) division. Note that this differs from IEEE-754.
1738 // TODO(jbramley): See if it's possible to do this inline, rather than by
1739 // calling a helper function. With frintz (to produce the intermediate
1740 // quotient) and fmsub (to calculate the remainder without loss of
1741 // precision), it should be possible. However, we would need support for
1742 // fdiv in round-towards-zero mode, and the ARM64 simulator doesn't
1743 // support that yet.
1744 DCHECK(left.Is(d0));
1745 DCHECK(right.Is(d1));
1747 ExternalReference::mod_two_doubles_operation(isolate()),
1749 DCHECK(result.Is(d0));
1759 void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
1760 DCHECK(ToRegister(instr->context()).is(cp));
1761 DCHECK(ToRegister(instr->left()).is(x1));
1762 DCHECK(ToRegister(instr->right()).is(x0));
1763 DCHECK(ToRegister(instr->result()).is(x0));
1766 CodeFactory::BinaryOpIC(isolate(), instr->op(), instr->strength()).code();
1767 CallCode(code, RelocInfo::CODE_TARGET, instr);
1771 void LCodeGen::DoBitI(LBitI* instr) {
1772 Register result = ToRegister32(instr->result());
1773 Register left = ToRegister32(instr->left());
1774 Operand right = ToShiftedRightOperand32(instr->right(), instr);
1776 switch (instr->op()) {
1777 case Token::BIT_AND: __ And(result, left, right); break;
1778 case Token::BIT_OR: __ Orr(result, left, right); break;
1779 case Token::BIT_XOR: __ Eor(result, left, right); break;
1787 void LCodeGen::DoBitS(LBitS* instr) {
1788 Register result = ToRegister(instr->result());
1789 Register left = ToRegister(instr->left());
1790 Operand right = ToOperand(instr->right());
1792 switch (instr->op()) {
1793 case Token::BIT_AND: __ And(result, left, right); break;
1794 case Token::BIT_OR: __ Orr(result, left, right); break;
1795 case Token::BIT_XOR: __ Eor(result, left, right); break;
1803 void LCodeGen::DoBoundsCheck(LBoundsCheck *instr) {
1804 Condition cond = instr->hydrogen()->allow_equality() ? hi : hs;
1805 DCHECK(instr->hydrogen()->index()->representation().IsInteger32());
1806 DCHECK(instr->hydrogen()->length()->representation().IsInteger32());
1807 if (instr->index()->IsConstantOperand()) {
1808 Operand index = ToOperand32(instr->index());
1809 Register length = ToRegister32(instr->length());
1810 __ Cmp(length, index);
1811 cond = CommuteCondition(cond);
1813 Register index = ToRegister32(instr->index());
1814 Operand length = ToOperand32(instr->length());
1815 __ Cmp(index, length);
1817 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
1818 __ Assert(NegateCondition(cond), kEliminatedBoundsCheckFailed);
1820 DeoptimizeIf(cond, instr, Deoptimizer::kOutOfBounds);
1825 void LCodeGen::DoBranch(LBranch* instr) {
1826 Representation r = instr->hydrogen()->value()->representation();
1827 Label* true_label = instr->TrueLabel(chunk_);
1828 Label* false_label = instr->FalseLabel(chunk_);
1830 if (r.IsInteger32()) {
1831 DCHECK(!info()->IsStub());
1832 EmitCompareAndBranch(instr, ne, ToRegister32(instr->value()), 0);
1833 } else if (r.IsSmi()) {
1834 DCHECK(!info()->IsStub());
1835 STATIC_ASSERT(kSmiTag == 0);
1836 EmitCompareAndBranch(instr, ne, ToRegister(instr->value()), 0);
1837 } else if (r.IsDouble()) {
1838 DoubleRegister value = ToDoubleRegister(instr->value());
1839 // Test the double value. Zero and NaN are false.
1840 EmitBranchIfNonZeroNumber(instr, value, double_scratch());
1842 DCHECK(r.IsTagged());
1843 Register value = ToRegister(instr->value());
1844 HType type = instr->hydrogen()->value()->type();
1846 if (type.IsBoolean()) {
1847 DCHECK(!info()->IsStub());
1848 __ CompareRoot(value, Heap::kTrueValueRootIndex);
1849 EmitBranch(instr, eq);
1850 } else if (type.IsSmi()) {
1851 DCHECK(!info()->IsStub());
1852 EmitCompareAndBranch(instr, ne, value, Smi::FromInt(0));
1853 } else if (type.IsJSArray()) {
1854 DCHECK(!info()->IsStub());
1855 EmitGoto(instr->TrueDestination(chunk()));
1856 } else if (type.IsHeapNumber()) {
1857 DCHECK(!info()->IsStub());
1858 __ Ldr(double_scratch(), FieldMemOperand(value,
1859 HeapNumber::kValueOffset));
1860 // Test the double value. Zero and NaN are false.
1861 EmitBranchIfNonZeroNumber(instr, double_scratch(), double_scratch());
1862 } else if (type.IsString()) {
1863 DCHECK(!info()->IsStub());
1864 Register temp = ToRegister(instr->temp1());
1865 __ Ldr(temp, FieldMemOperand(value, String::kLengthOffset));
1866 EmitCompareAndBranch(instr, ne, temp, 0);
1868 ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
1869 // Avoid deopts in the case where we've never executed this path before.
1870 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
1872 if (expected.Contains(ToBooleanStub::UNDEFINED)) {
1873 // undefined -> false.
1875 value, Heap::kUndefinedValueRootIndex, false_label);
1878 if (expected.Contains(ToBooleanStub::BOOLEAN)) {
1879 // Boolean -> its value.
1881 value, Heap::kTrueValueRootIndex, true_label);
1883 value, Heap::kFalseValueRootIndex, false_label);
1886 if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
1889 value, Heap::kNullValueRootIndex, false_label);
1892 if (expected.Contains(ToBooleanStub::SMI)) {
1893 // Smis: 0 -> false, all other -> true.
1894 DCHECK(Smi::FromInt(0) == 0);
1895 __ Cbz(value, false_label);
1896 __ JumpIfSmi(value, true_label);
1897 } else if (expected.NeedsMap()) {
1898 // If we need a map later and have a smi, deopt.
1899 DeoptimizeIfSmi(value, instr, Deoptimizer::kSmi);
1902 Register map = NoReg;
1903 Register scratch = NoReg;
1905 if (expected.NeedsMap()) {
1906 DCHECK((instr->temp1() != NULL) && (instr->temp2() != NULL));
1907 map = ToRegister(instr->temp1());
1908 scratch = ToRegister(instr->temp2());
1910 __ Ldr(map, FieldMemOperand(value, HeapObject::kMapOffset));
1912 if (expected.CanBeUndetectable()) {
1913 // Undetectable -> false.
1914 __ Ldrb(scratch, FieldMemOperand(map, Map::kBitFieldOffset));
1915 __ TestAndBranchIfAnySet(
1916 scratch, 1 << Map::kIsUndetectable, false_label);
1920 if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
1921 // spec object -> true.
1922 __ CompareInstanceType(map, scratch, FIRST_SPEC_OBJECT_TYPE);
1923 __ B(ge, true_label);
1926 if (expected.Contains(ToBooleanStub::STRING)) {
1927 // String value -> false iff empty.
1929 __ CompareInstanceType(map, scratch, FIRST_NONSTRING_TYPE);
1930 __ B(ge, ¬_string);
1931 __ Ldr(scratch, FieldMemOperand(value, String::kLengthOffset));
1932 __ Cbz(scratch, false_label);
1934 __ Bind(¬_string);
1937 if (expected.Contains(ToBooleanStub::SYMBOL)) {
1938 // Symbol value -> true.
1939 __ CompareInstanceType(map, scratch, SYMBOL_TYPE);
1940 __ B(eq, true_label);
1943 if (expected.Contains(ToBooleanStub::SIMD_VALUE)) {
1944 // SIMD value -> true.
1945 __ CompareInstanceType(map, scratch, SIMD128_VALUE_TYPE);
1946 __ B(eq, true_label);
1949 if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
1950 Label not_heap_number;
1951 __ JumpIfNotRoot(map, Heap::kHeapNumberMapRootIndex, ¬_heap_number);
1953 __ Ldr(double_scratch(),
1954 FieldMemOperand(value, HeapNumber::kValueOffset));
1955 __ Fcmp(double_scratch(), 0.0);
1956 // If we got a NaN (overflow bit is set), jump to the false branch.
1957 __ B(vs, false_label);
1958 __ B(eq, false_label);
1960 __ Bind(¬_heap_number);
1963 if (!expected.IsGeneric()) {
1964 // We've seen something for the first time -> deopt.
1965 // This can only happen if we are not generic already.
1966 Deoptimize(instr, Deoptimizer::kUnexpectedObject);
1973 void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
1974 int formal_parameter_count, int arity,
1975 LInstruction* instr) {
1976 bool dont_adapt_arguments =
1977 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1978 bool can_invoke_directly =
1979 dont_adapt_arguments || formal_parameter_count == arity;
1981 // The function interface relies on the following register assignments.
1982 Register function_reg = x1;
1983 Register arity_reg = x0;
1985 LPointerMap* pointers = instr->pointer_map();
1987 if (FLAG_debug_code) {
1989 // Try to confirm that function_reg (x1) is a tagged pointer.
1990 __ JumpIfNotSmi(function_reg, &is_not_smi);
1991 __ Abort(kExpectedFunctionObject);
1992 __ Bind(&is_not_smi);
1995 if (can_invoke_directly) {
1997 __ Ldr(cp, FieldMemOperand(function_reg, JSFunction::kContextOffset));
1999 // Always initialize x0 to the number of actual arguments.
2000 __ Mov(arity_reg, arity);
2003 __ Ldr(x10, FieldMemOperand(function_reg, JSFunction::kCodeEntryOffset));
2006 // Set up deoptimization.
2007 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
2009 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
2010 ParameterCount count(arity);
2011 ParameterCount expected(formal_parameter_count);
2012 __ InvokeFunction(function_reg, expected, count, CALL_FUNCTION, generator);
2017 void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
2018 DCHECK(instr->IsMarkedAsCall());
2019 DCHECK(ToRegister(instr->result()).Is(x0));
2021 if (instr->hydrogen()->IsTailCall()) {
2022 if (NeedsEagerFrame()) __ LeaveFrame(StackFrame::INTERNAL);
2024 if (instr->target()->IsConstantOperand()) {
2025 LConstantOperand* target = LConstantOperand::cast(instr->target());
2026 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
2027 // TODO(all): on ARM we use a call descriptor to specify a storage mode
2028 // but on ARM64 we only have one storage mode so it isn't necessary. Check
2029 // this understanding is correct.
2030 __ Jump(code, RelocInfo::CODE_TARGET);
2032 DCHECK(instr->target()->IsRegister());
2033 Register target = ToRegister(instr->target());
2034 __ Add(target, target, Code::kHeaderSize - kHeapObjectTag);
2038 LPointerMap* pointers = instr->pointer_map();
2039 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
2041 if (instr->target()->IsConstantOperand()) {
2042 LConstantOperand* target = LConstantOperand::cast(instr->target());
2043 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
2044 generator.BeforeCall(__ CallSize(code, RelocInfo::CODE_TARGET));
2045 // TODO(all): on ARM we use a call descriptor to specify a storage mode
2046 // but on ARM64 we only have one storage mode so it isn't necessary. Check
2047 // this understanding is correct.
2048 __ Call(code, RelocInfo::CODE_TARGET, TypeFeedbackId::None());
2050 DCHECK(instr->target()->IsRegister());
2051 Register target = ToRegister(instr->target());
2052 generator.BeforeCall(__ CallSize(target));
2053 __ Add(target, target, Code::kHeaderSize - kHeapObjectTag);
2056 generator.AfterCall();
2059 RecordPushedArgumentsDelta(instr->hydrogen()->argument_delta());
2063 void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
2064 DCHECK(instr->IsMarkedAsCall());
2065 DCHECK(ToRegister(instr->function()).is(x1));
2067 __ Mov(x0, Operand(instr->arity()));
2070 __ Ldr(cp, FieldMemOperand(x1, JSFunction::kContextOffset));
2072 // Load the code entry address
2073 __ Ldr(x10, FieldMemOperand(x1, JSFunction::kCodeEntryOffset));
2076 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
2077 RecordPushedArgumentsDelta(instr->hydrogen()->argument_delta());
2081 void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
2082 CallRuntime(instr->function(), instr->arity(), instr);
2083 RecordPushedArgumentsDelta(instr->hydrogen()->argument_delta());
2087 void LCodeGen::DoCallStub(LCallStub* instr) {
2088 DCHECK(ToRegister(instr->context()).is(cp));
2089 DCHECK(ToRegister(instr->result()).is(x0));
2090 switch (instr->hydrogen()->major_key()) {
2091 case CodeStub::RegExpExec: {
2092 RegExpExecStub stub(isolate());
2093 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2096 case CodeStub::SubString: {
2097 SubStringStub stub(isolate());
2098 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2104 RecordPushedArgumentsDelta(instr->hydrogen()->argument_delta());
2108 void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
2109 GenerateOsrPrologue();
2113 void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
2114 Register temp = ToRegister(instr->temp());
2116 PushSafepointRegistersScope scope(this);
2119 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
2120 RecordSafepointWithRegisters(
2121 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
2122 __ StoreToSafepointRegisterSlot(x0, temp);
2124 DeoptimizeIfSmi(temp, instr, Deoptimizer::kInstanceMigrationFailed);
2128 void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
2129 class DeferredCheckMaps: public LDeferredCode {
2131 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
2132 : LDeferredCode(codegen), instr_(instr), object_(object) {
2133 SetExit(check_maps());
2135 virtual void Generate() {
2136 codegen()->DoDeferredInstanceMigration(instr_, object_);
2138 Label* check_maps() { return &check_maps_; }
2139 virtual LInstruction* instr() { return instr_; }
2146 if (instr->hydrogen()->IsStabilityCheck()) {
2147 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
2148 for (int i = 0; i < maps->size(); ++i) {
2149 AddStabilityDependency(maps->at(i).handle());
2154 Register object = ToRegister(instr->value());
2155 Register map_reg = ToRegister(instr->temp());
2157 __ Ldr(map_reg, FieldMemOperand(object, HeapObject::kMapOffset));
2159 DeferredCheckMaps* deferred = NULL;
2160 if (instr->hydrogen()->HasMigrationTarget()) {
2161 deferred = new(zone()) DeferredCheckMaps(this, instr, object);
2162 __ Bind(deferred->check_maps());
2165 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
2167 for (int i = 0; i < maps->size() - 1; i++) {
2168 Handle<Map> map = maps->at(i).handle();
2169 __ CompareMap(map_reg, map);
2172 Handle<Map> map = maps->at(maps->size() - 1).handle();
2173 __ CompareMap(map_reg, map);
2175 // We didn't match a map.
2176 if (instr->hydrogen()->HasMigrationTarget()) {
2177 __ B(ne, deferred->entry());
2179 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap);
2186 void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
2187 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2188 DeoptimizeIfSmi(ToRegister(instr->value()), instr, Deoptimizer::kSmi);
2193 void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
2194 Register value = ToRegister(instr->value());
2195 DCHECK(!instr->result() || ToRegister(instr->result()).Is(value));
2196 DeoptimizeIfNotSmi(value, instr, Deoptimizer::kNotASmi);
2200 void LCodeGen::DoCheckArrayBufferNotNeutered(
2201 LCheckArrayBufferNotNeutered* instr) {
2202 UseScratchRegisterScope temps(masm());
2203 Register view = ToRegister(instr->view());
2204 Register scratch = temps.AcquireX();
2206 __ Ldr(scratch, FieldMemOperand(view, JSArrayBufferView::kBufferOffset));
2207 __ Ldr(scratch, FieldMemOperand(scratch, JSArrayBuffer::kBitFieldOffset));
2208 __ Tst(scratch, Operand(1 << JSArrayBuffer::WasNeutered::kShift));
2209 DeoptimizeIf(ne, instr, Deoptimizer::kOutOfBounds);
2213 void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
2214 Register input = ToRegister(instr->value());
2215 Register scratch = ToRegister(instr->temp());
2217 __ Ldr(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
2218 __ Ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
2220 if (instr->hydrogen()->is_interval_check()) {
2221 InstanceType first, last;
2222 instr->hydrogen()->GetCheckInterval(&first, &last);
2224 __ Cmp(scratch, first);
2225 if (first == last) {
2226 // If there is only one type in the interval check for equality.
2227 DeoptimizeIf(ne, instr, Deoptimizer::kWrongInstanceType);
2228 } else if (last == LAST_TYPE) {
2229 // We don't need to compare with the higher bound of the interval.
2230 DeoptimizeIf(lo, instr, Deoptimizer::kWrongInstanceType);
2232 // If we are below the lower bound, set the C flag and clear the Z flag
2233 // to force a deopt.
2234 __ Ccmp(scratch, last, CFlag, hs);
2235 DeoptimizeIf(hi, instr, Deoptimizer::kWrongInstanceType);
2240 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
2242 if (base::bits::IsPowerOfTwo32(mask)) {
2243 DCHECK((tag == 0) || (tag == mask));
2245 DeoptimizeIfBitSet(scratch, MaskToBit(mask), instr,
2246 Deoptimizer::kWrongInstanceType);
2248 DeoptimizeIfBitClear(scratch, MaskToBit(mask), instr,
2249 Deoptimizer::kWrongInstanceType);
2253 __ Tst(scratch, mask);
2255 __ And(scratch, scratch, mask);
2256 __ Cmp(scratch, tag);
2258 DeoptimizeIf(ne, instr, Deoptimizer::kWrongInstanceType);
2264 void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
2265 DoubleRegister input = ToDoubleRegister(instr->unclamped());
2266 Register result = ToRegister32(instr->result());
2267 __ ClampDoubleToUint8(result, input, double_scratch());
2271 void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
2272 Register input = ToRegister32(instr->unclamped());
2273 Register result = ToRegister32(instr->result());
2274 __ ClampInt32ToUint8(result, input);
2278 void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
2279 Register input = ToRegister(instr->unclamped());
2280 Register result = ToRegister32(instr->result());
2283 // Both smi and heap number cases are handled.
2285 __ JumpIfNotSmi(input, &is_not_smi);
2286 __ SmiUntag(result.X(), input);
2287 __ ClampInt32ToUint8(result);
2290 __ Bind(&is_not_smi);
2292 // Check for heap number.
2293 Label is_heap_number;
2294 __ JumpIfHeapNumber(input, &is_heap_number);
2296 // Check for undefined. Undefined is coverted to zero for clamping conversion.
2297 DeoptimizeIfNotRoot(input, Heap::kUndefinedValueRootIndex, instr,
2298 Deoptimizer::kNotAHeapNumberUndefined);
2302 // Heap number case.
2303 __ Bind(&is_heap_number);
2304 DoubleRegister dbl_scratch = double_scratch();
2305 DoubleRegister dbl_scratch2 = ToDoubleRegister(instr->temp1());
2306 __ Ldr(dbl_scratch, FieldMemOperand(input, HeapNumber::kValueOffset));
2307 __ ClampDoubleToUint8(result, dbl_scratch, dbl_scratch2);
2313 void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
2314 DoubleRegister value_reg = ToDoubleRegister(instr->value());
2315 Register result_reg = ToRegister(instr->result());
2316 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
2317 __ Fmov(result_reg, value_reg);
2318 __ Lsr(result_reg, result_reg, 32);
2320 __ Fmov(result_reg.W(), value_reg.S());
2325 void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
2326 Register hi_reg = ToRegister(instr->hi());
2327 Register lo_reg = ToRegister(instr->lo());
2328 DoubleRegister result_reg = ToDoubleRegister(instr->result());
2330 // Insert the least significant 32 bits of hi_reg into the most significant
2331 // 32 bits of lo_reg, and move to a floating point register.
2332 __ Bfi(lo_reg, hi_reg, 32, 32);
2333 __ Fmov(result_reg, lo_reg);
2337 void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
2338 Handle<String> class_name = instr->hydrogen()->class_name();
2339 Label* true_label = instr->TrueLabel(chunk_);
2340 Label* false_label = instr->FalseLabel(chunk_);
2341 Register input = ToRegister(instr->value());
2342 Register scratch1 = ToRegister(instr->temp1());
2343 Register scratch2 = ToRegister(instr->temp2());
2345 __ JumpIfSmi(input, false_label);
2347 Register map = scratch2;
2348 if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
2349 // Assuming the following assertions, we can use the same compares to test
2350 // for both being a function type and being in the object type range.
2351 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
2352 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2353 FIRST_SPEC_OBJECT_TYPE + 1);
2354 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2355 LAST_SPEC_OBJECT_TYPE - 1);
2356 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
2358 // We expect CompareObjectType to load the object instance type in scratch1.
2359 __ CompareObjectType(input, map, scratch1, FIRST_SPEC_OBJECT_TYPE);
2360 __ B(lt, false_label);
2361 __ B(eq, true_label);
2362 __ Cmp(scratch1, LAST_SPEC_OBJECT_TYPE);
2363 __ B(eq, true_label);
2365 __ IsObjectJSObjectType(input, map, scratch1, false_label);
2368 // Now we are in the FIRST-LAST_NONCALLABLE_SPEC_OBJECT_TYPE range.
2369 // Check if the constructor in the map is a function.
2371 UseScratchRegisterScope temps(masm());
2372 Register instance_type = temps.AcquireX();
2373 __ GetMapConstructor(scratch1, map, scratch2, instance_type);
2374 __ Cmp(instance_type, JS_FUNCTION_TYPE);
2376 // Objects with a non-function constructor have class 'Object'.
2377 if (String::Equals(class_name, isolate()->factory()->Object_string())) {
2378 __ B(ne, true_label);
2380 __ B(ne, false_label);
2383 // The constructor function is in scratch1. Get its instance class name.
2385 FieldMemOperand(scratch1, JSFunction::kSharedFunctionInfoOffset));
2387 FieldMemOperand(scratch1,
2388 SharedFunctionInfo::kInstanceClassNameOffset));
2390 // The class name we are testing against is internalized since it's a literal.
2391 // The name in the constructor is internalized because of the way the context
2392 // is booted. This routine isn't expected to work for random API-created
2393 // classes and it doesn't have to because you can't access it with natives
2394 // syntax. Since both sides are internalized it is sufficient to use an
2395 // identity comparison.
2396 EmitCompareAndBranch(instr, eq, scratch1, Operand(class_name));
2400 void LCodeGen::DoCmpHoleAndBranchD(LCmpHoleAndBranchD* instr) {
2401 DCHECK(instr->hydrogen()->representation().IsDouble());
2402 FPRegister object = ToDoubleRegister(instr->object());
2403 Register temp = ToRegister(instr->temp());
2405 // If we don't have a NaN, we don't have the hole, so branch now to avoid the
2406 // (relatively expensive) hole-NaN check.
2407 __ Fcmp(object, object);
2408 __ B(vc, instr->FalseLabel(chunk_));
2410 // We have a NaN, but is it the hole?
2411 __ Fmov(temp, object);
2412 EmitCompareAndBranch(instr, eq, temp, kHoleNanInt64);
2416 void LCodeGen::DoCmpHoleAndBranchT(LCmpHoleAndBranchT* instr) {
2417 DCHECK(instr->hydrogen()->representation().IsTagged());
2418 Register object = ToRegister(instr->object());
2420 EmitBranchIfRoot(instr, object, Heap::kTheHoleValueRootIndex);
2424 void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
2425 Register value = ToRegister(instr->value());
2426 Register map = ToRegister(instr->temp());
2428 __ Ldr(map, FieldMemOperand(value, HeapObject::kMapOffset));
2429 EmitCompareAndBranch(instr, eq, map, Operand(instr->map()));
2433 void LCodeGen::DoCompareMinusZeroAndBranch(LCompareMinusZeroAndBranch* instr) {
2434 Representation rep = instr->hydrogen()->value()->representation();
2435 DCHECK(!rep.IsInteger32());
2436 Register scratch = ToRegister(instr->temp());
2438 if (rep.IsDouble()) {
2439 __ JumpIfMinusZero(ToDoubleRegister(instr->value()),
2440 instr->TrueLabel(chunk()));
2442 Register value = ToRegister(instr->value());
2443 __ JumpIfNotHeapNumber(value, instr->FalseLabel(chunk()), DO_SMI_CHECK);
2444 __ Ldr(scratch, FieldMemOperand(value, HeapNumber::kValueOffset));
2445 __ JumpIfMinusZero(scratch, instr->TrueLabel(chunk()));
2447 EmitGoto(instr->FalseDestination(chunk()));
2451 void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2452 LOperand* left = instr->left();
2453 LOperand* right = instr->right();
2455 instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2456 instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2457 Condition cond = TokenToCondition(instr->op(), is_unsigned);
2459 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2460 // We can statically evaluate the comparison.
2461 double left_val = ToDouble(LConstantOperand::cast(left));
2462 double right_val = ToDouble(LConstantOperand::cast(right));
2463 int next_block = EvalComparison(instr->op(), left_val, right_val) ?
2464 instr->TrueDestination(chunk_) : instr->FalseDestination(chunk_);
2465 EmitGoto(next_block);
2467 if (instr->is_double()) {
2468 __ Fcmp(ToDoubleRegister(left), ToDoubleRegister(right));
2470 // If a NaN is involved, i.e. the result is unordered (V set),
2471 // jump to false block label.
2472 __ B(vs, instr->FalseLabel(chunk_));
2473 EmitBranch(instr, cond);
2475 if (instr->hydrogen_value()->representation().IsInteger32()) {
2476 if (right->IsConstantOperand()) {
2477 EmitCompareAndBranch(instr, cond, ToRegister32(left),
2478 ToOperand32(right));
2480 // Commute the operands and the condition.
2481 EmitCompareAndBranch(instr, CommuteCondition(cond),
2482 ToRegister32(right), ToOperand32(left));
2485 DCHECK(instr->hydrogen_value()->representation().IsSmi());
2486 if (right->IsConstantOperand()) {
2487 int32_t value = ToInteger32(LConstantOperand::cast(right));
2488 EmitCompareAndBranch(instr,
2491 Operand(Smi::FromInt(value)));
2492 } else if (left->IsConstantOperand()) {
2493 // Commute the operands and the condition.
2494 int32_t value = ToInteger32(LConstantOperand::cast(left));
2495 EmitCompareAndBranch(instr,
2496 CommuteCondition(cond),
2498 Operand(Smi::FromInt(value)));
2500 EmitCompareAndBranch(instr,
2511 void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
2512 Register left = ToRegister(instr->left());
2513 Register right = ToRegister(instr->right());
2514 EmitCompareAndBranch(instr, eq, left, right);
2518 void LCodeGen::DoCmpT(LCmpT* instr) {
2519 DCHECK(ToRegister(instr->context()).is(cp));
2520 Token::Value op = instr->op();
2521 Condition cond = TokenToCondition(op, false);
2523 DCHECK(ToRegister(instr->left()).Is(x1));
2524 DCHECK(ToRegister(instr->right()).Is(x0));
2526 CodeFactory::CompareIC(isolate(), op, instr->strength()).code();
2527 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2528 // Signal that we don't inline smi code before this stub.
2529 InlineSmiCheckInfo::EmitNotInlined(masm());
2531 // Return true or false depending on CompareIC result.
2532 // This instruction is marked as call. We can clobber any register.
2533 DCHECK(instr->IsMarkedAsCall());
2534 __ LoadTrueFalseRoots(x1, x2);
2536 __ Csel(ToRegister(instr->result()), x1, x2, cond);
2540 void LCodeGen::DoConstantD(LConstantD* instr) {
2541 DCHECK(instr->result()->IsDoubleRegister());
2542 DoubleRegister result = ToDoubleRegister(instr->result());
2543 if (instr->value() == 0) {
2544 if (copysign(1.0, instr->value()) == 1.0) {
2545 __ Fmov(result, fp_zero);
2547 __ Fneg(result, fp_zero);
2550 __ Fmov(result, instr->value());
2555 void LCodeGen::DoConstantE(LConstantE* instr) {
2556 __ Mov(ToRegister(instr->result()), Operand(instr->value()));
2560 void LCodeGen::DoConstantI(LConstantI* instr) {
2561 DCHECK(is_int32(instr->value()));
2562 // Cast the value here to ensure that the value isn't sign extended by the
2563 // implicit Operand constructor.
2564 __ Mov(ToRegister32(instr->result()), static_cast<uint32_t>(instr->value()));
2568 void LCodeGen::DoConstantS(LConstantS* instr) {
2569 __ Mov(ToRegister(instr->result()), Operand(instr->value()));
2573 void LCodeGen::DoConstantT(LConstantT* instr) {
2574 Handle<Object> object = instr->value(isolate());
2575 AllowDeferredHandleDereference smi_check;
2576 __ LoadObject(ToRegister(instr->result()), object);
2580 void LCodeGen::DoContext(LContext* instr) {
2581 // If there is a non-return use, the context must be moved to a register.
2582 Register result = ToRegister(instr->result());
2583 if (info()->IsOptimizing()) {
2584 __ Ldr(result, MemOperand(fp, StandardFrameConstants::kContextOffset));
2586 // If there is no frame, the context must be in cp.
2587 DCHECK(result.is(cp));
2592 void LCodeGen::DoCheckValue(LCheckValue* instr) {
2593 Register reg = ToRegister(instr->value());
2594 Handle<HeapObject> object = instr->hydrogen()->object().handle();
2595 AllowDeferredHandleDereference smi_check;
2596 if (isolate()->heap()->InNewSpace(*object)) {
2597 UseScratchRegisterScope temps(masm());
2598 Register temp = temps.AcquireX();
2599 Handle<Cell> cell = isolate()->factory()->NewCell(object);
2600 __ Mov(temp, Operand(cell));
2601 __ Ldr(temp, FieldMemOperand(temp, Cell::kValueOffset));
2604 __ Cmp(reg, Operand(object));
2606 DeoptimizeIf(ne, instr, Deoptimizer::kValueMismatch);
2610 void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
2611 last_lazy_deopt_pc_ = masm()->pc_offset();
2612 DCHECK(instr->HasEnvironment());
2613 LEnvironment* env = instr->environment();
2614 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
2615 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
2619 void LCodeGen::DoDateField(LDateField* instr) {
2620 Register object = ToRegister(instr->date());
2621 Register result = ToRegister(instr->result());
2622 Register temp1 = x10;
2623 Register temp2 = x11;
2624 Smi* index = instr->index();
2626 DCHECK(object.is(result) && object.Is(x0));
2627 DCHECK(instr->IsMarkedAsCall());
2629 if (index->value() == 0) {
2630 __ Ldr(result, FieldMemOperand(object, JSDate::kValueOffset));
2632 Label runtime, done;
2633 if (index->value() < JSDate::kFirstUncachedField) {
2634 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
2635 __ Mov(temp1, Operand(stamp));
2636 __ Ldr(temp1, MemOperand(temp1));
2637 __ Ldr(temp2, FieldMemOperand(object, JSDate::kCacheStampOffset));
2638 __ Cmp(temp1, temp2);
2640 __ Ldr(result, FieldMemOperand(object, JSDate::kValueOffset +
2641 kPointerSize * index->value()));
2646 __ Mov(x1, Operand(index));
2647 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
2653 void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
2654 Deoptimizer::BailoutType type = instr->hydrogen()->type();
2655 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
2656 // needed return address), even though the implementation of LAZY and EAGER is
2657 // now identical. When LAZY is eventually completely folded into EAGER, remove
2658 // the special case below.
2659 if (info()->IsStub() && (type == Deoptimizer::EAGER)) {
2660 type = Deoptimizer::LAZY;
2663 Deoptimize(instr, instr->hydrogen()->reason(), &type);
2667 void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
2668 Register dividend = ToRegister32(instr->dividend());
2669 int32_t divisor = instr->divisor();
2670 Register result = ToRegister32(instr->result());
2671 DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
2672 DCHECK(!result.is(dividend));
2674 // Check for (0 / -x) that will produce negative zero.
2675 HDiv* hdiv = instr->hydrogen();
2676 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
2677 DeoptimizeIfZero(dividend, instr, Deoptimizer::kDivisionByZero);
2679 // Check for (kMinInt / -1).
2680 if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
2681 // Test dividend for kMinInt by subtracting one (cmp) and checking for
2683 __ Cmp(dividend, 1);
2684 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
2686 // Deoptimize if remainder will not be 0.
2687 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
2688 divisor != 1 && divisor != -1) {
2689 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
2690 __ Tst(dividend, mask);
2691 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision);
2694 if (divisor == -1) { // Nice shortcut, not needed for correctness.
2695 __ Neg(result, dividend);
2698 int32_t shift = WhichPowerOf2Abs(divisor);
2700 __ Mov(result, dividend);
2701 } else if (shift == 1) {
2702 __ Add(result, dividend, Operand(dividend, LSR, 31));
2704 __ Mov(result, Operand(dividend, ASR, 31));
2705 __ Add(result, dividend, Operand(result, LSR, 32 - shift));
2707 if (shift > 0) __ Mov(result, Operand(result, ASR, shift));
2708 if (divisor < 0) __ Neg(result, result);
2712 void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
2713 Register dividend = ToRegister32(instr->dividend());
2714 int32_t divisor = instr->divisor();
2715 Register result = ToRegister32(instr->result());
2716 DCHECK(!AreAliased(dividend, result));
2719 Deoptimize(instr, Deoptimizer::kDivisionByZero);
2723 // Check for (0 / -x) that will produce negative zero.
2724 HDiv* hdiv = instr->hydrogen();
2725 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
2726 DeoptimizeIfZero(dividend, instr, Deoptimizer::kMinusZero);
2729 __ TruncatingDiv(result, dividend, Abs(divisor));
2730 if (divisor < 0) __ Neg(result, result);
2732 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
2733 Register temp = ToRegister32(instr->temp());
2734 DCHECK(!AreAliased(dividend, result, temp));
2735 __ Sxtw(dividend.X(), dividend);
2736 __ Mov(temp, divisor);
2737 __ Smsubl(temp.X(), result, temp, dividend.X());
2738 DeoptimizeIfNotZero(temp, instr, Deoptimizer::kLostPrecision);
2743 // TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
2744 void LCodeGen::DoDivI(LDivI* instr) {
2745 HBinaryOperation* hdiv = instr->hydrogen();
2746 Register dividend = ToRegister32(instr->dividend());
2747 Register divisor = ToRegister32(instr->divisor());
2748 Register result = ToRegister32(instr->result());
2750 // Issue the division first, and then check for any deopt cases whilst the
2751 // result is computed.
2752 __ Sdiv(result, dividend, divisor);
2754 if (hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
2755 DCHECK(!instr->temp());
2760 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
2761 DeoptimizeIfZero(divisor, instr, Deoptimizer::kDivisionByZero);
2764 // Check for (0 / -x) as that will produce negative zero.
2765 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
2768 // If the divisor < 0 (mi), compare the dividend, and deopt if it is
2769 // zero, ie. zero dividend with negative divisor deopts.
2770 // If the divisor >= 0 (pl, the opposite of mi) set the flags to
2771 // condition ne, so we don't deopt, ie. positive divisor doesn't deopt.
2772 __ Ccmp(dividend, 0, NoFlag, mi);
2773 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
2776 // Check for (kMinInt / -1).
2777 if (hdiv->CheckFlag(HValue::kCanOverflow)) {
2778 // Test dividend for kMinInt by subtracting one (cmp) and checking for
2780 __ Cmp(dividend, 1);
2781 // If overflow is set, ie. dividend = kMinInt, compare the divisor with
2782 // -1. If overflow is clear, set the flags for condition ne, as the
2783 // dividend isn't -1, and thus we shouldn't deopt.
2784 __ Ccmp(divisor, -1, NoFlag, vs);
2785 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow);
2788 // Compute remainder and deopt if it's not zero.
2789 Register remainder = ToRegister32(instr->temp());
2790 __ Msub(remainder, result, divisor, dividend);
2791 DeoptimizeIfNotZero(remainder, instr, Deoptimizer::kLostPrecision);
2795 void LCodeGen::DoDoubleToIntOrSmi(LDoubleToIntOrSmi* instr) {
2796 DoubleRegister input = ToDoubleRegister(instr->value());
2797 Register result = ToRegister32(instr->result());
2799 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
2800 DeoptimizeIfMinusZero(input, instr, Deoptimizer::kMinusZero);
2803 __ TryRepresentDoubleAsInt32(result, input, double_scratch());
2804 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN);
2806 if (instr->tag_result()) {
2807 __ SmiTag(result.X());
2812 void LCodeGen::DoDrop(LDrop* instr) {
2813 __ Drop(instr->count());
2817 void LCodeGen::DoDummy(LDummy* instr) {
2818 // Nothing to see here, move on!
2822 void LCodeGen::DoDummyUse(LDummyUse* instr) {
2823 // Nothing to see here, move on!
2827 void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
2828 Register map = ToRegister(instr->map());
2829 Register result = ToRegister(instr->result());
2830 Label load_cache, done;
2832 __ EnumLengthUntagged(result, map);
2833 __ Cbnz(result, &load_cache);
2835 __ Mov(result, Operand(isolate()->factory()->empty_fixed_array()));
2838 __ Bind(&load_cache);
2839 __ LoadInstanceDescriptors(map, result);
2840 __ Ldr(result, FieldMemOperand(result, DescriptorArray::kEnumCacheOffset));
2841 __ Ldr(result, FieldMemOperand(result, FixedArray::SizeFor(instr->idx())));
2842 DeoptimizeIfZero(result, instr, Deoptimizer::kNoCache);
2848 void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
2849 Register object = ToRegister(instr->object());
2850 Register null_value = x5;
2852 DCHECK(instr->IsMarkedAsCall());
2853 DCHECK(object.Is(x0));
2855 DeoptimizeIfSmi(object, instr, Deoptimizer::kSmi);
2857 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
2858 __ CompareObjectType(object, x1, x1, LAST_JS_PROXY_TYPE);
2859 DeoptimizeIf(le, instr, Deoptimizer::kNotAJavaScriptObject);
2861 Label use_cache, call_runtime;
2862 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
2863 __ CheckEnumCache(object, null_value, x1, x2, x3, x4, &call_runtime);
2865 __ Ldr(object, FieldMemOperand(object, HeapObject::kMapOffset));
2868 // Get the set of properties to enumerate.
2869 __ Bind(&call_runtime);
2871 CallRuntime(Runtime::kGetPropertyNamesFast, 1, instr);
2873 __ Ldr(x1, FieldMemOperand(object, HeapObject::kMapOffset));
2874 DeoptimizeIfNotRoot(x1, Heap::kMetaMapRootIndex, instr,
2875 Deoptimizer::kWrongMap);
2877 __ Bind(&use_cache);
2881 void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
2882 Register input = ToRegister(instr->value());
2883 Register result = ToRegister(instr->result());
2885 __ AssertString(input);
2887 // Assert that we can use a W register load to get the hash.
2888 DCHECK((String::kHashShift + String::kArrayIndexValueBits) < kWRegSizeInBits);
2889 __ Ldr(result.W(), FieldMemOperand(input, String::kHashFieldOffset));
2890 __ IndexFromHash(result, result);
2894 void LCodeGen::EmitGoto(int block) {
2895 // Do not emit jump if we are emitting a goto to the next block.
2896 if (!IsNextEmittedBlock(block)) {
2897 __ B(chunk_->GetAssemblyLabel(LookupDestination(block)));
2902 void LCodeGen::DoGoto(LGoto* instr) {
2903 EmitGoto(instr->block_id());
2907 void LCodeGen::DoHasCachedArrayIndexAndBranch(
2908 LHasCachedArrayIndexAndBranch* instr) {
2909 Register input = ToRegister(instr->value());
2910 Register temp = ToRegister32(instr->temp());
2912 // Assert that the cache status bits fit in a W register.
2913 DCHECK(is_uint32(String::kContainsCachedArrayIndexMask));
2914 __ Ldr(temp, FieldMemOperand(input, String::kHashFieldOffset));
2915 __ Tst(temp, String::kContainsCachedArrayIndexMask);
2916 EmitBranch(instr, eq);
2920 // HHasInstanceTypeAndBranch instruction is built with an interval of type
2921 // to test but is only used in very restricted ways. The only possible kinds
2922 // of intervals are:
2923 // - [ FIRST_TYPE, instr->to() ]
2924 // - [ instr->form(), LAST_TYPE ]
2925 // - instr->from() == instr->to()
2927 // These kinds of intervals can be check with only one compare instruction
2928 // providing the correct value and test condition are used.
2930 // TestType() will return the value to use in the compare instruction and
2931 // BranchCondition() will return the condition to use depending on the kind
2932 // of interval actually specified in the instruction.
2933 static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
2934 InstanceType from = instr->from();
2935 InstanceType to = instr->to();
2936 if (from == FIRST_TYPE) return to;
2937 DCHECK((from == to) || (to == LAST_TYPE));
2942 // See comment above TestType function for what this function does.
2943 static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
2944 InstanceType from = instr->from();
2945 InstanceType to = instr->to();
2946 if (from == to) return eq;
2947 if (to == LAST_TYPE) return hs;
2948 if (from == FIRST_TYPE) return ls;
2954 void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
2955 Register input = ToRegister(instr->value());
2956 Register scratch = ToRegister(instr->temp());
2958 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2959 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2961 __ CompareObjectType(input, scratch, scratch, TestType(instr->hydrogen()));
2962 EmitBranch(instr, BranchCondition(instr->hydrogen()));
2966 void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
2967 Register result = ToRegister(instr->result());
2968 Register base = ToRegister(instr->base_object());
2969 if (instr->offset()->IsConstantOperand()) {
2970 __ Add(result, base, ToOperand32(instr->offset()));
2972 __ Add(result, base, Operand(ToRegister32(instr->offset()), SXTW));
2977 void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
2978 DCHECK(ToRegister(instr->context()).is(cp));
2979 DCHECK(ToRegister(instr->left()).is(InstanceOfDescriptor::LeftRegister()));
2980 DCHECK(ToRegister(instr->right()).is(InstanceOfDescriptor::RightRegister()));
2981 DCHECK(ToRegister(instr->result()).is(x0));
2982 InstanceOfStub stub(isolate());
2983 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2987 void LCodeGen::DoHasInPrototypeChainAndBranch(
2988 LHasInPrototypeChainAndBranch* instr) {
2989 Register const object = ToRegister(instr->object());
2990 Register const object_map = ToRegister(instr->scratch());
2991 Register const object_prototype = object_map;
2992 Register const prototype = ToRegister(instr->prototype());
2994 // The {object} must be a spec object. It's sufficient to know that {object}
2995 // is not a smi, since all other non-spec objects have {null} prototypes and
2996 // will be ruled out below.
2997 if (instr->hydrogen()->ObjectNeedsSmiCheck()) {
2998 __ JumpIfSmi(object, instr->FalseLabel(chunk_));
3001 // Loop through the {object}s prototype chain looking for the {prototype}.
3002 __ Ldr(object_map, FieldMemOperand(object, HeapObject::kMapOffset));
3005 __ Ldr(object_prototype, FieldMemOperand(object_map, Map::kPrototypeOffset));
3006 __ Cmp(object_prototype, prototype);
3007 __ B(eq, instr->TrueLabel(chunk_));
3008 __ CompareRoot(object_prototype, Heap::kNullValueRootIndex);
3009 __ B(eq, instr->FalseLabel(chunk_));
3010 __ Ldr(object_map, FieldMemOperand(object_prototype, HeapObject::kMapOffset));
3015 void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
3020 void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
3021 Register value = ToRegister32(instr->value());
3022 DoubleRegister result = ToDoubleRegister(instr->result());
3023 __ Scvtf(result, value);
3027 void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
3028 DCHECK(ToRegister(instr->context()).is(cp));
3029 // The function is required to be in x1.
3030 DCHECK(ToRegister(instr->function()).is(x1));
3031 DCHECK(instr->HasPointerMap());
3033 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
3034 if (known_function.is_null()) {
3035 LPointerMap* pointers = instr->pointer_map();
3036 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3037 ParameterCount count(instr->arity());
3038 __ InvokeFunction(x1, count, CALL_FUNCTION, generator);
3040 CallKnownFunction(known_function,
3041 instr->hydrogen()->formal_parameter_count(),
3042 instr->arity(), instr);
3044 RecordPushedArgumentsDelta(instr->hydrogen()->argument_delta());
3048 void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
3049 Register temp1 = ToRegister(instr->temp1());
3050 Register temp2 = ToRegister(instr->temp2());
3052 // Get the frame pointer for the calling frame.
3053 __ Ldr(temp1, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3055 // Skip the arguments adaptor frame if it exists.
3056 Label check_frame_marker;
3057 __ Ldr(temp2, MemOperand(temp1, StandardFrameConstants::kContextOffset));
3058 __ Cmp(temp2, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
3059 __ B(ne, &check_frame_marker);
3060 __ Ldr(temp1, MemOperand(temp1, StandardFrameConstants::kCallerFPOffset));
3062 // Check the marker in the calling frame.
3063 __ Bind(&check_frame_marker);
3064 __ Ldr(temp1, MemOperand(temp1, StandardFrameConstants::kMarkerOffset));
3066 EmitCompareAndBranch(
3067 instr, eq, temp1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
3071 Condition LCodeGen::EmitIsString(Register input,
3073 Label* is_not_string,
3074 SmiCheck check_needed = INLINE_SMI_CHECK) {
3075 if (check_needed == INLINE_SMI_CHECK) {
3076 __ JumpIfSmi(input, is_not_string);
3078 __ CompareObjectType(input, temp1, temp1, FIRST_NONSTRING_TYPE);
3084 void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
3085 Register val = ToRegister(instr->value());
3086 Register scratch = ToRegister(instr->temp());
3088 SmiCheck check_needed =
3089 instr->hydrogen()->value()->type().IsHeapObject()
3090 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
3091 Condition true_cond =
3092 EmitIsString(val, scratch, instr->FalseLabel(chunk_), check_needed);
3094 EmitBranch(instr, true_cond);
3098 void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
3099 Register value = ToRegister(instr->value());
3100 STATIC_ASSERT(kSmiTag == 0);
3101 EmitTestAndBranch(instr, eq, value, kSmiTagMask);
3105 void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
3106 Register input = ToRegister(instr->value());
3107 Register temp = ToRegister(instr->temp());
3109 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
3110 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
3112 __ Ldr(temp, FieldMemOperand(input, HeapObject::kMapOffset));
3113 __ Ldrb(temp, FieldMemOperand(temp, Map::kBitFieldOffset));
3115 EmitTestAndBranch(instr, ne, temp, 1 << Map::kIsUndetectable);
3119 static const char* LabelType(LLabel* label) {
3120 if (label->is_loop_header()) return " (loop header)";
3121 if (label->is_osr_entry()) return " (OSR entry)";
3126 void LCodeGen::DoLabel(LLabel* label) {
3127 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
3128 current_instruction_,
3129 label->hydrogen_value()->id(),
3133 // Inherit pushed_arguments_ from the predecessor's argument count.
3134 if (label->block()->HasPredecessor()) {
3135 pushed_arguments_ = label->block()->predecessors()->at(0)->argument_count();
3137 for (auto p : *label->block()->predecessors()) {
3138 DCHECK_EQ(p->argument_count(), pushed_arguments_);
3143 __ Bind(label->label());
3144 current_block_ = label->block_id();
3149 void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
3150 Register context = ToRegister(instr->context());
3151 Register result = ToRegister(instr->result());
3152 __ Ldr(result, ContextMemOperand(context, instr->slot_index()));
3153 if (instr->hydrogen()->RequiresHoleCheck()) {
3154 if (instr->hydrogen()->DeoptimizesOnHole()) {
3155 DeoptimizeIfRoot(result, Heap::kTheHoleValueRootIndex, instr,
3156 Deoptimizer::kHole);
3159 __ JumpIfNotRoot(result, Heap::kTheHoleValueRootIndex, ¬_the_hole);
3160 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3161 __ Bind(¬_the_hole);
3167 void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
3168 Register function = ToRegister(instr->function());
3169 Register result = ToRegister(instr->result());
3170 Register temp = ToRegister(instr->temp());
3172 // Get the prototype or initial map from the function.
3173 __ Ldr(result, FieldMemOperand(function,
3174 JSFunction::kPrototypeOrInitialMapOffset));
3176 // Check that the function has a prototype or an initial map.
3177 DeoptimizeIfRoot(result, Heap::kTheHoleValueRootIndex, instr,
3178 Deoptimizer::kHole);
3180 // If the function does not have an initial map, we're done.
3182 __ CompareObjectType(result, temp, temp, MAP_TYPE);
3185 // Get the prototype from the initial map.
3186 __ Ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
3194 void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
3195 Register vector_register = ToRegister(instr->temp_vector());
3196 Register slot_register = LoadWithVectorDescriptor::SlotRegister();
3197 DCHECK(vector_register.is(LoadWithVectorDescriptor::VectorRegister()));
3198 DCHECK(slot_register.is(x0));
3200 AllowDeferredHandleDereference vector_structure_check;
3201 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
3202 __ Mov(vector_register, vector);
3203 // No need to allocate this register.
3204 FeedbackVectorICSlot slot = instr->hydrogen()->slot();
3205 int index = vector->GetIndex(slot);
3206 __ Mov(slot_register, Smi::FromInt(index));
3211 void LCodeGen::EmitVectorStoreICRegisters(T* instr) {
3212 Register vector_register = ToRegister(instr->temp_vector());
3213 Register slot_register = ToRegister(instr->temp_slot());
3215 AllowDeferredHandleDereference vector_structure_check;
3216 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
3217 __ Mov(vector_register, vector);
3218 FeedbackVectorICSlot slot = instr->hydrogen()->slot();
3219 int index = vector->GetIndex(slot);
3220 __ Mov(slot_register, Smi::FromInt(index));
3224 void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
3225 DCHECK(ToRegister(instr->context()).is(cp));
3226 DCHECK(ToRegister(instr->global_object())
3227 .is(LoadDescriptor::ReceiverRegister()));
3228 DCHECK(ToRegister(instr->result()).Is(x0));
3229 __ Mov(LoadDescriptor::NameRegister(), Operand(instr->name()));
3230 EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
3232 CodeFactory::LoadICInOptimizedCode(isolate(), instr->typeof_mode(),
3233 SLOPPY, PREMONOMORPHIC).code();
3234 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3238 void LCodeGen::DoLoadGlobalViaContext(LLoadGlobalViaContext* instr) {
3239 DCHECK(ToRegister(instr->context()).is(cp));
3240 DCHECK(ToRegister(instr->result()).is(x0));
3242 int const slot = instr->slot_index();
3243 int const depth = instr->depth();
3244 if (depth <= LoadGlobalViaContextStub::kMaximumDepth) {
3245 __ Mov(LoadGlobalViaContextDescriptor::SlotRegister(), Operand(slot));
3247 CodeFactory::LoadGlobalViaContext(isolate(), depth).code();
3248 CallCode(stub, RelocInfo::CODE_TARGET, instr);
3250 __ Push(Smi::FromInt(slot));
3251 __ CallRuntime(Runtime::kLoadGlobalViaContext, 1);
3256 MemOperand LCodeGen::PrepareKeyedExternalArrayOperand(
3261 bool key_is_constant,
3263 ElementsKind elements_kind,
3265 int element_size_shift = ElementsKindToShiftSize(elements_kind);
3267 if (key_is_constant) {
3268 int key_offset = constant_key << element_size_shift;
3269 return MemOperand(base, key_offset + base_offset);
3273 __ Add(scratch, base, Operand::UntagSmiAndScale(key, element_size_shift));
3274 return MemOperand(scratch, base_offset);
3277 if (base_offset == 0) {
3278 return MemOperand(base, key, SXTW, element_size_shift);
3281 DCHECK(!AreAliased(scratch, key));
3282 __ Add(scratch, base, base_offset);
3283 return MemOperand(scratch, key, SXTW, element_size_shift);
3287 void LCodeGen::DoLoadKeyedExternal(LLoadKeyedExternal* instr) {
3288 Register ext_ptr = ToRegister(instr->elements());
3290 ElementsKind elements_kind = instr->elements_kind();
3292 bool key_is_smi = instr->hydrogen()->key()->representation().IsSmi();
3293 bool key_is_constant = instr->key()->IsConstantOperand();
3294 Register key = no_reg;
3295 int constant_key = 0;
3296 if (key_is_constant) {
3297 DCHECK(instr->temp() == NULL);
3298 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3299 if (constant_key & 0xf0000000) {
3300 Abort(kArrayIndexConstantValueTooBig);
3303 scratch = ToRegister(instr->temp());
3304 key = ToRegister(instr->key());
3308 PrepareKeyedExternalArrayOperand(key, ext_ptr, scratch, key_is_smi,
3309 key_is_constant, constant_key,
3311 instr->base_offset());
3313 if (elements_kind == FLOAT32_ELEMENTS) {
3314 DoubleRegister result = ToDoubleRegister(instr->result());
3315 __ Ldr(result.S(), mem_op);
3316 __ Fcvt(result, result.S());
3317 } else if (elements_kind == FLOAT64_ELEMENTS) {
3318 DoubleRegister result = ToDoubleRegister(instr->result());
3319 __ Ldr(result, mem_op);
3321 Register result = ToRegister(instr->result());
3323 switch (elements_kind) {
3325 __ Ldrsb(result, mem_op);
3327 case UINT8_ELEMENTS:
3328 case UINT8_CLAMPED_ELEMENTS:
3329 __ Ldrb(result, mem_op);
3331 case INT16_ELEMENTS:
3332 __ Ldrsh(result, mem_op);
3334 case UINT16_ELEMENTS:
3335 __ Ldrh(result, mem_op);
3337 case INT32_ELEMENTS:
3338 __ Ldrsw(result, mem_op);
3340 case UINT32_ELEMENTS:
3341 __ Ldr(result.W(), mem_op);
3342 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
3343 // Deopt if value > 0x80000000.
3344 __ Tst(result, 0xFFFFFFFF80000000);
3345 DeoptimizeIf(ne, instr, Deoptimizer::kNegativeValue);
3348 case FLOAT32_ELEMENTS:
3349 case FLOAT64_ELEMENTS:
3350 case FAST_HOLEY_DOUBLE_ELEMENTS:
3351 case FAST_HOLEY_ELEMENTS:
3352 case FAST_HOLEY_SMI_ELEMENTS:
3353 case FAST_DOUBLE_ELEMENTS:
3355 case FAST_SMI_ELEMENTS:
3356 case DICTIONARY_ELEMENTS:
3357 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
3358 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
3366 MemOperand LCodeGen::PrepareKeyedArrayOperand(Register base,
3370 ElementsKind elements_kind,
3371 Representation representation,
3373 STATIC_ASSERT(static_cast<unsigned>(kSmiValueSize) == kWRegSizeInBits);
3374 STATIC_ASSERT(kSmiTag == 0);
3375 int element_size_shift = ElementsKindToShiftSize(elements_kind);
3377 // Even though the HLoad/StoreKeyed instructions force the input
3378 // representation for the key to be an integer, the input gets replaced during
3379 // bounds check elimination with the index argument to the bounds check, which
3380 // can be tagged, so that case must be handled here, too.
3381 if (key_is_tagged) {
3382 __ Add(base, elements, Operand::UntagSmiAndScale(key, element_size_shift));
3383 if (representation.IsInteger32()) {
3384 DCHECK(elements_kind == FAST_SMI_ELEMENTS);
3385 // Read or write only the smi payload in the case of fast smi arrays.
3386 return UntagSmiMemOperand(base, base_offset);
3388 return MemOperand(base, base_offset);
3391 // Sign extend key because it could be a 32-bit negative value or contain
3392 // garbage in the top 32-bits. The address computation happens in 64-bit.
3393 DCHECK((element_size_shift >= 0) && (element_size_shift <= 4));
3394 if (representation.IsInteger32()) {
3395 DCHECK(elements_kind == FAST_SMI_ELEMENTS);
3396 // Read or write only the smi payload in the case of fast smi arrays.
3397 __ Add(base, elements, Operand(key, SXTW, element_size_shift));
3398 return UntagSmiMemOperand(base, base_offset);
3400 __ Add(base, elements, base_offset);
3401 return MemOperand(base, key, SXTW, element_size_shift);
3407 void LCodeGen::DoLoadKeyedFixedDouble(LLoadKeyedFixedDouble* instr) {
3408 Register elements = ToRegister(instr->elements());
3409 DoubleRegister result = ToDoubleRegister(instr->result());
3412 if (instr->key()->IsConstantOperand()) {
3413 DCHECK(instr->hydrogen()->RequiresHoleCheck() ||
3414 (instr->temp() == NULL));
3416 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3417 if (constant_key & 0xf0000000) {
3418 Abort(kArrayIndexConstantValueTooBig);
3420 int offset = instr->base_offset() + constant_key * kDoubleSize;
3421 mem_op = MemOperand(elements, offset);
3423 Register load_base = ToRegister(instr->temp());
3424 Register key = ToRegister(instr->key());
3425 bool key_is_tagged = instr->hydrogen()->key()->representation().IsSmi();
3426 mem_op = PrepareKeyedArrayOperand(load_base, elements, key, key_is_tagged,
3427 instr->hydrogen()->elements_kind(),
3428 instr->hydrogen()->representation(),
3429 instr->base_offset());
3432 __ Ldr(result, mem_op);
3434 if (instr->hydrogen()->RequiresHoleCheck()) {
3435 Register scratch = ToRegister(instr->temp());
3436 __ Fmov(scratch, result);
3437 __ Eor(scratch, scratch, kHoleNanInt64);
3438 DeoptimizeIfZero(scratch, instr, Deoptimizer::kHole);
3443 void LCodeGen::DoLoadKeyedFixed(LLoadKeyedFixed* instr) {
3444 Register elements = ToRegister(instr->elements());
3445 Register result = ToRegister(instr->result());
3448 Representation representation = instr->hydrogen()->representation();
3449 if (instr->key()->IsConstantOperand()) {
3450 DCHECK(instr->temp() == NULL);
3451 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
3452 int offset = instr->base_offset() +
3453 ToInteger32(const_operand) * kPointerSize;
3454 if (representation.IsInteger32()) {
3455 DCHECK(instr->hydrogen()->elements_kind() == FAST_SMI_ELEMENTS);
3456 STATIC_ASSERT(static_cast<unsigned>(kSmiValueSize) == kWRegSizeInBits);
3457 STATIC_ASSERT(kSmiTag == 0);
3458 mem_op = UntagSmiMemOperand(elements, offset);
3460 mem_op = MemOperand(elements, offset);
3463 Register load_base = ToRegister(instr->temp());
3464 Register key = ToRegister(instr->key());
3465 bool key_is_tagged = instr->hydrogen()->key()->representation().IsSmi();
3467 mem_op = PrepareKeyedArrayOperand(load_base, elements, key, key_is_tagged,
3468 instr->hydrogen()->elements_kind(),
3469 representation, instr->base_offset());
3472 __ Load(result, mem_op, representation);
3474 if (instr->hydrogen()->RequiresHoleCheck()) {
3475 if (IsFastSmiElementsKind(instr->hydrogen()->elements_kind())) {
3476 DeoptimizeIfNotSmi(result, instr, Deoptimizer::kNotASmi);
3478 DeoptimizeIfRoot(result, Heap::kTheHoleValueRootIndex, instr,
3479 Deoptimizer::kHole);
3481 } else if (instr->hydrogen()->hole_mode() == CONVERT_HOLE_TO_UNDEFINED) {
3482 DCHECK(instr->hydrogen()->elements_kind() == FAST_HOLEY_ELEMENTS);
3484 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
3486 if (info()->IsStub()) {
3487 // A stub can safely convert the hole to undefined only if the array
3488 // protector cell contains (Smi) Isolate::kArrayProtectorValid. Otherwise
3489 // it needs to bail out.
3490 __ LoadRoot(result, Heap::kArrayProtectorRootIndex);
3491 __ Ldr(result, FieldMemOperand(result, Cell::kValueOffset));
3492 __ Cmp(result, Operand(Smi::FromInt(Isolate::kArrayProtectorValid)));
3493 DeoptimizeIf(ne, instr, Deoptimizer::kHole);
3495 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3501 void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3502 DCHECK(ToRegister(instr->context()).is(cp));
3503 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3504 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
3506 if (instr->hydrogen()->HasVectorAndSlot()) {
3507 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
3510 Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(
3511 isolate(), instr->hydrogen()->language_mode(),
3512 instr->hydrogen()->initialization_state()).code();
3513 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3515 DCHECK(ToRegister(instr->result()).Is(x0));
3519 void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
3520 HObjectAccess access = instr->hydrogen()->access();
3521 int offset = access.offset();
3522 Register object = ToRegister(instr->object());
3524 if (access.IsExternalMemory()) {
3525 Register result = ToRegister(instr->result());
3526 __ Load(result, MemOperand(object, offset), access.representation());
3530 if (instr->hydrogen()->representation().IsDouble()) {
3531 DCHECK(access.IsInobject());
3532 FPRegister result = ToDoubleRegister(instr->result());
3533 __ Ldr(result, FieldMemOperand(object, offset));
3537 Register result = ToRegister(instr->result());
3539 if (access.IsInobject()) {
3542 // Load the properties array, using result as a scratch register.
3543 __ Ldr(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
3547 if (access.representation().IsSmi() &&
3548 instr->hydrogen()->representation().IsInteger32()) {
3549 // Read int value directly from upper half of the smi.
3550 STATIC_ASSERT(static_cast<unsigned>(kSmiValueSize) == kWRegSizeInBits);
3551 STATIC_ASSERT(kSmiTag == 0);
3552 __ Load(result, UntagSmiFieldMemOperand(source, offset),
3553 Representation::Integer32());
3555 __ Load(result, FieldMemOperand(source, offset), access.representation());
3560 void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
3561 DCHECK(ToRegister(instr->context()).is(cp));
3562 // LoadIC expects name and receiver in registers.
3563 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3564 __ Mov(LoadDescriptor::NameRegister(), Operand(instr->name()));
3565 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
3567 CodeFactory::LoadICInOptimizedCode(
3568 isolate(), NOT_INSIDE_TYPEOF, instr->hydrogen()->language_mode(),
3569 instr->hydrogen()->initialization_state()).code();
3570 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3572 DCHECK(ToRegister(instr->result()).is(x0));
3576 void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
3577 Register result = ToRegister(instr->result());
3578 __ LoadRoot(result, instr->index());
3582 void LCodeGen::DoMapEnumLength(LMapEnumLength* instr) {
3583 Register result = ToRegister(instr->result());
3584 Register map = ToRegister(instr->value());
3585 __ EnumLengthSmi(result, map);
3589 void LCodeGen::DoMathAbs(LMathAbs* instr) {
3590 Representation r = instr->hydrogen()->value()->representation();
3592 DoubleRegister input = ToDoubleRegister(instr->value());
3593 DoubleRegister result = ToDoubleRegister(instr->result());
3594 __ Fabs(result, input);
3595 } else if (r.IsSmi() || r.IsInteger32()) {
3596 Register input = r.IsSmi() ? ToRegister(instr->value())
3597 : ToRegister32(instr->value());
3598 Register result = r.IsSmi() ? ToRegister(instr->result())
3599 : ToRegister32(instr->result());
3600 __ Abs(result, input);
3601 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
3606 void LCodeGen::DoDeferredMathAbsTagged(LMathAbsTagged* instr,
3608 Label* allocation_entry) {
3609 // Handle the tricky cases of MathAbsTagged:
3610 // - HeapNumber inputs.
3611 // - Negative inputs produce a positive result, so a new HeapNumber is
3612 // allocated to hold it.
3613 // - Positive inputs are returned as-is, since there is no need to allocate
3614 // a new HeapNumber for the result.
3615 // - The (smi) input -0x80000000, produces +0x80000000, which does not fit
3616 // a smi. In this case, the inline code sets the result and jumps directly
3617 // to the allocation_entry label.
3618 DCHECK(instr->context() != NULL);
3619 DCHECK(ToRegister(instr->context()).is(cp));
3620 Register input = ToRegister(instr->value());
3621 Register temp1 = ToRegister(instr->temp1());
3622 Register temp2 = ToRegister(instr->temp2());
3623 Register result_bits = ToRegister(instr->temp3());
3624 Register result = ToRegister(instr->result());
3626 Label runtime_allocation;
3628 // Deoptimize if the input is not a HeapNumber.
3629 DeoptimizeIfNotHeapNumber(input, instr);
3631 // If the argument is positive, we can return it as-is, without any need to
3632 // allocate a new HeapNumber for the result. We have to do this in integer
3633 // registers (rather than with fabs) because we need to be able to distinguish
3635 __ Ldr(result_bits, FieldMemOperand(input, HeapNumber::kValueOffset));
3636 __ Mov(result, input);
3637 __ Tbz(result_bits, kXSignBit, exit);
3639 // Calculate abs(input) by clearing the sign bit.
3640 __ Bic(result_bits, result_bits, kXSignMask);
3642 // Allocate a new HeapNumber to hold the result.
3643 // result_bits The bit representation of the (double) result.
3644 __ Bind(allocation_entry);
3645 __ AllocateHeapNumber(result, &runtime_allocation, temp1, temp2);
3646 // The inline (non-deferred) code will store result_bits into result.
3649 __ Bind(&runtime_allocation);
3650 if (FLAG_debug_code) {
3651 // Because result is in the pointer map, we need to make sure it has a valid
3652 // tagged value before we call the runtime. We speculatively set it to the
3653 // input (for abs(+x)) or to a smi (for abs(-SMI_MIN)), so it should already
3656 Register input = ToRegister(instr->value());
3657 __ JumpIfSmi(result, &result_ok);
3658 __ Cmp(input, result);
3659 __ Assert(eq, kUnexpectedValue);
3660 __ Bind(&result_ok);
3663 { PushSafepointRegistersScope scope(this);
3664 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr,
3666 __ StoreToSafepointRegisterSlot(x0, result);
3668 // The inline (non-deferred) code will store result_bits into result.
3672 void LCodeGen::DoMathAbsTagged(LMathAbsTagged* instr) {
3673 // Class for deferred case.
3674 class DeferredMathAbsTagged: public LDeferredCode {
3676 DeferredMathAbsTagged(LCodeGen* codegen, LMathAbsTagged* instr)
3677 : LDeferredCode(codegen), instr_(instr) { }
3678 virtual void Generate() {
3679 codegen()->DoDeferredMathAbsTagged(instr_, exit(),
3680 allocation_entry());
3682 virtual LInstruction* instr() { return instr_; }
3683 Label* allocation_entry() { return &allocation; }
3685 LMathAbsTagged* instr_;
3689 // TODO(jbramley): The early-exit mechanism would skip the new frame handling
3690 // in GenerateDeferredCode. Tidy this up.
3691 DCHECK(!NeedsDeferredFrame());
3693 DeferredMathAbsTagged* deferred =
3694 new(zone()) DeferredMathAbsTagged(this, instr);
3696 DCHECK(instr->hydrogen()->value()->representation().IsTagged() ||
3697 instr->hydrogen()->value()->representation().IsSmi());
3698 Register input = ToRegister(instr->value());
3699 Register result_bits = ToRegister(instr->temp3());
3700 Register result = ToRegister(instr->result());
3703 // Handle smis inline.
3704 // We can treat smis as 64-bit integers, since the (low-order) tag bits will
3705 // never get set by the negation. This is therefore the same as the Integer32
3706 // case in DoMathAbs, except that it operates on 64-bit values.
3707 STATIC_ASSERT((kSmiValueSize == 32) && (kSmiShift == 32) && (kSmiTag == 0));
3709 __ JumpIfNotSmi(input, deferred->entry());
3711 __ Abs(result, input, NULL, &done);
3713 // The result is the magnitude (abs) of the smallest value a smi can
3714 // represent, encoded as a double.
3715 __ Mov(result_bits, double_to_rawbits(0x80000000));
3716 __ B(deferred->allocation_entry());
3718 __ Bind(deferred->exit());
3719 __ Str(result_bits, FieldMemOperand(result, HeapNumber::kValueOffset));
3725 void LCodeGen::DoMathExp(LMathExp* instr) {
3726 DoubleRegister input = ToDoubleRegister(instr->value());
3727 DoubleRegister result = ToDoubleRegister(instr->result());
3728 DoubleRegister double_temp1 = ToDoubleRegister(instr->double_temp1());
3729 DoubleRegister double_temp2 = double_scratch();
3730 Register temp1 = ToRegister(instr->temp1());
3731 Register temp2 = ToRegister(instr->temp2());
3732 Register temp3 = ToRegister(instr->temp3());
3734 MathExpGenerator::EmitMathExp(masm(), input, result,
3735 double_temp1, double_temp2,
3736 temp1, temp2, temp3);
3740 void LCodeGen::DoMathFloorD(LMathFloorD* instr) {
3741 DoubleRegister input = ToDoubleRegister(instr->value());
3742 DoubleRegister result = ToDoubleRegister(instr->result());
3744 __ Frintm(result, input);
3748 void LCodeGen::DoMathFloorI(LMathFloorI* instr) {
3749 DoubleRegister input = ToDoubleRegister(instr->value());
3750 Register result = ToRegister(instr->result());
3752 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3753 DeoptimizeIfMinusZero(input, instr, Deoptimizer::kMinusZero);
3756 __ Fcvtms(result, input);
3758 // Check that the result fits into a 32-bit integer.
3759 // - The result did not overflow.
3760 __ Cmp(result, Operand(result, SXTW));
3761 // - The input was not NaN.
3762 __ Fccmp(input, input, NoFlag, eq);
3763 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN);
3767 void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
3768 Register dividend = ToRegister32(instr->dividend());
3769 Register result = ToRegister32(instr->result());
3770 int32_t divisor = instr->divisor();
3772 // If the divisor is 1, return the dividend.
3774 __ Mov(result, dividend, kDiscardForSameWReg);
3778 // If the divisor is positive, things are easy: There can be no deopts and we
3779 // can simply do an arithmetic right shift.
3780 int32_t shift = WhichPowerOf2Abs(divisor);
3782 __ Mov(result, Operand(dividend, ASR, shift));
3786 // If the divisor is negative, we have to negate and handle edge cases.
3787 __ Negs(result, dividend);
3788 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3789 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
3792 // Dividing by -1 is basically negation, unless we overflow.
3793 if (divisor == -1) {
3794 if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
3795 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
3800 // If the negation could not overflow, simply shifting is OK.
3801 if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
3802 __ Mov(result, Operand(dividend, ASR, shift));
3806 __ Asr(result, result, shift);
3807 __ Csel(result, result, kMinInt / divisor, vc);
3811 void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
3812 Register dividend = ToRegister32(instr->dividend());
3813 int32_t divisor = instr->divisor();
3814 Register result = ToRegister32(instr->result());
3815 DCHECK(!AreAliased(dividend, result));
3818 Deoptimize(instr, Deoptimizer::kDivisionByZero);
3822 // Check for (0 / -x) that will produce negative zero.
3823 HMathFloorOfDiv* hdiv = instr->hydrogen();
3824 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
3825 DeoptimizeIfZero(dividend, instr, Deoptimizer::kMinusZero);
3828 // Easy case: We need no dynamic check for the dividend and the flooring
3829 // division is the same as the truncating division.
3830 if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
3831 (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
3832 __ TruncatingDiv(result, dividend, Abs(divisor));
3833 if (divisor < 0) __ Neg(result, result);
3837 // In the general case we may need to adjust before and after the truncating
3838 // division to get a flooring division.
3839 Register temp = ToRegister32(instr->temp());
3840 DCHECK(!AreAliased(temp, dividend, result));
3841 Label needs_adjustment, done;
3842 __ Cmp(dividend, 0);
3843 __ B(divisor > 0 ? lt : gt, &needs_adjustment);
3844 __ TruncatingDiv(result, dividend, Abs(divisor));
3845 if (divisor < 0) __ Neg(result, result);
3847 __ Bind(&needs_adjustment);
3848 __ Add(temp, dividend, Operand(divisor > 0 ? 1 : -1));
3849 __ TruncatingDiv(result, temp, Abs(divisor));
3850 if (divisor < 0) __ Neg(result, result);
3851 __ Sub(result, result, Operand(1));
3856 // TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
3857 void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
3858 Register dividend = ToRegister32(instr->dividend());
3859 Register divisor = ToRegister32(instr->divisor());
3860 Register remainder = ToRegister32(instr->temp());
3861 Register result = ToRegister32(instr->result());
3863 // This can't cause an exception on ARM, so we can speculatively
3864 // execute it already now.
3865 __ Sdiv(result, dividend, divisor);
3868 DeoptimizeIfZero(divisor, instr, Deoptimizer::kDivisionByZero);
3870 // Check for (kMinInt / -1).
3871 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
3872 // The V flag will be set iff dividend == kMinInt.
3873 __ Cmp(dividend, 1);
3874 __ Ccmp(divisor, -1, NoFlag, vs);
3875 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow);
3878 // Check for (0 / -x) that will produce negative zero.
3879 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3881 __ Ccmp(dividend, 0, ZFlag, mi);
3882 // "divisor" can't be null because the code would have already been
3883 // deoptimized. The Z flag is set only if (divisor < 0) and (dividend == 0).
3884 // In this case we need to deoptimize to produce a -0.
3885 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
3889 // If both operands have the same sign then we are done.
3890 __ Eor(remainder, dividend, divisor);
3891 __ Tbz(remainder, kWSignBit, &done);
3893 // Check if the result needs to be corrected.
3894 __ Msub(remainder, result, divisor, dividend);
3895 __ Cbz(remainder, &done);
3896 __ Sub(result, result, 1);
3902 void LCodeGen::DoMathLog(LMathLog* instr) {
3903 DCHECK(instr->IsMarkedAsCall());
3904 DCHECK(ToDoubleRegister(instr->value()).is(d0));
3905 __ CallCFunction(ExternalReference::math_log_double_function(isolate()),
3907 DCHECK(ToDoubleRegister(instr->result()).Is(d0));
3911 void LCodeGen::DoMathClz32(LMathClz32* instr) {
3912 Register input = ToRegister32(instr->value());
3913 Register result = ToRegister32(instr->result());
3914 __ Clz(result, input);
3918 void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3919 DoubleRegister input = ToDoubleRegister(instr->value());
3920 DoubleRegister result = ToDoubleRegister(instr->result());
3923 // Math.pow(x, 0.5) differs from fsqrt(x) in the following cases:
3924 // Math.pow(-Infinity, 0.5) == +Infinity
3925 // Math.pow(-0.0, 0.5) == +0.0
3927 // Catch -infinity inputs first.
3928 // TODO(jbramley): A constant infinity register would be helpful here.
3929 __ Fmov(double_scratch(), kFP64NegativeInfinity);
3930 __ Fcmp(double_scratch(), input);
3931 __ Fabs(result, input);
3934 // Add +0.0 to convert -0.0 to +0.0.
3935 __ Fadd(double_scratch(), input, fp_zero);
3936 __ Fsqrt(result, double_scratch());
3942 void LCodeGen::DoPower(LPower* instr) {
3943 Representation exponent_type = instr->hydrogen()->right()->representation();
3944 // Having marked this as a call, we can use any registers.
3945 // Just make sure that the input/output registers are the expected ones.
3946 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3947 Register integer_exponent = MathPowIntegerDescriptor::exponent();
3948 DCHECK(!instr->right()->IsDoubleRegister() ||
3949 ToDoubleRegister(instr->right()).is(d1));
3950 DCHECK(exponent_type.IsInteger32() || !instr->right()->IsRegister() ||
3951 ToRegister(instr->right()).is(tagged_exponent));
3952 DCHECK(!exponent_type.IsInteger32() ||
3953 ToRegister(instr->right()).is(integer_exponent));
3954 DCHECK(ToDoubleRegister(instr->left()).is(d0));
3955 DCHECK(ToDoubleRegister(instr->result()).is(d0));
3957 if (exponent_type.IsSmi()) {
3958 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3960 } else if (exponent_type.IsTagged()) {
3962 __ JumpIfSmi(tagged_exponent, &no_deopt);
3963 DeoptimizeIfNotHeapNumber(tagged_exponent, instr);
3965 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3967 } else if (exponent_type.IsInteger32()) {
3968 // Ensure integer exponent has no garbage in top 32-bits, as MathPowStub
3969 // supports large integer exponents.
3970 __ Sxtw(integer_exponent, integer_exponent);
3971 MathPowStub stub(isolate(), MathPowStub::INTEGER);
3974 DCHECK(exponent_type.IsDouble());
3975 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
3981 void LCodeGen::DoMathRoundD(LMathRoundD* instr) {
3982 DoubleRegister input = ToDoubleRegister(instr->value());
3983 DoubleRegister result = ToDoubleRegister(instr->result());
3984 DoubleRegister scratch_d = double_scratch();
3986 DCHECK(!AreAliased(input, result, scratch_d));
3990 __ Frinta(result, input);
3991 __ Fcmp(input, 0.0);
3992 __ Fccmp(result, input, ZFlag, lt);
3993 // The result is correct if the input was in [-0, +infinity], or was a
3994 // negative integral value.
3997 // Here the input is negative, non integral, with an exponent lower than 52.
3998 // We do not have to worry about the 0.49999999999999994 (0x3fdfffffffffffff)
3999 // case. So we can safely add 0.5.
4000 __ Fmov(scratch_d, 0.5);
4001 __ Fadd(result, input, scratch_d);
4002 __ Frintm(result, result);
4003 // The range [-0.5, -0.0[ yielded +0.0. Force the sign to negative.
4004 __ Fabs(result, result);
4005 __ Fneg(result, result);
4011 void LCodeGen::DoMathRoundI(LMathRoundI* instr) {
4012 DoubleRegister input = ToDoubleRegister(instr->value());
4013 DoubleRegister temp = ToDoubleRegister(instr->temp1());
4014 DoubleRegister dot_five = double_scratch();
4015 Register result = ToRegister(instr->result());
4018 // Math.round() rounds to the nearest integer, with ties going towards
4019 // +infinity. This does not match any IEEE-754 rounding mode.
4020 // - Infinities and NaNs are propagated unchanged, but cause deopts because
4021 // they can't be represented as integers.
4022 // - The sign of the result is the same as the sign of the input. This means
4023 // that -0.0 rounds to itself, and values -0.5 <= input < 0 also produce a
4026 // Add 0.5 and round towards -infinity.
4027 __ Fmov(dot_five, 0.5);
4028 __ Fadd(temp, input, dot_five);
4029 __ Fcvtms(result, temp);
4031 // The result is correct if:
4032 // result is not 0, as the input could be NaN or [-0.5, -0.0].
4033 // result is not 1, as 0.499...94 will wrongly map to 1.
4034 // result fits in 32 bits.
4035 __ Cmp(result, Operand(result.W(), SXTW));
4036 __ Ccmp(result, 1, ZFlag, eq);
4039 // At this point, we have to handle possible inputs of NaN or numbers in the
4040 // range [-0.5, 1.5[, or numbers larger than 32 bits.
4042 // Deoptimize if the result > 1, as it must be larger than 32 bits.
4044 DeoptimizeIf(hi, instr, Deoptimizer::kOverflow);
4046 // Deoptimize for negative inputs, which at this point are only numbers in
4047 // the range [-0.5, -0.0]
4048 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
4049 __ Fmov(result, input);
4050 DeoptimizeIfNegative(result, instr, Deoptimizer::kMinusZero);
4053 // Deoptimize if the input was NaN.
4054 __ Fcmp(input, dot_five);
4055 DeoptimizeIf(vs, instr, Deoptimizer::kNaN);
4057 // Now, the only unhandled inputs are in the range [0.0, 1.5[ (or [-0.5, 1.5[
4058 // if we didn't generate a -0.0 bailout). If input >= 0.5 then return 1,
4059 // else 0; we avoid dealing with 0.499...94 directly.
4060 __ Cset(result, ge);
4065 void LCodeGen::DoMathFround(LMathFround* instr) {
4066 DoubleRegister input = ToDoubleRegister(instr->value());
4067 DoubleRegister result = ToDoubleRegister(instr->result());
4068 __ Fcvt(result.S(), input);
4069 __ Fcvt(result, result.S());
4073 void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
4074 DoubleRegister input = ToDoubleRegister(instr->value());
4075 DoubleRegister result = ToDoubleRegister(instr->result());
4076 __ Fsqrt(result, input);
4080 void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
4081 HMathMinMax::Operation op = instr->hydrogen()->operation();
4082 if (instr->hydrogen()->representation().IsInteger32()) {
4083 Register result = ToRegister32(instr->result());
4084 Register left = ToRegister32(instr->left());
4085 Operand right = ToOperand32(instr->right());
4087 __ Cmp(left, right);
4088 __ Csel(result, left, right, (op == HMathMinMax::kMathMax) ? ge : le);
4089 } else if (instr->hydrogen()->representation().IsSmi()) {
4090 Register result = ToRegister(instr->result());
4091 Register left = ToRegister(instr->left());
4092 Operand right = ToOperand(instr->right());
4094 __ Cmp(left, right);
4095 __ Csel(result, left, right, (op == HMathMinMax::kMathMax) ? ge : le);
4097 DCHECK(instr->hydrogen()->representation().IsDouble());
4098 DoubleRegister result = ToDoubleRegister(instr->result());
4099 DoubleRegister left = ToDoubleRegister(instr->left());
4100 DoubleRegister right = ToDoubleRegister(instr->right());
4102 if (op == HMathMinMax::kMathMax) {
4103 __ Fmax(result, left, right);
4105 DCHECK(op == HMathMinMax::kMathMin);
4106 __ Fmin(result, left, right);
4112 void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
4113 Register dividend = ToRegister32(instr->dividend());
4114 int32_t divisor = instr->divisor();
4115 DCHECK(dividend.is(ToRegister32(instr->result())));
4117 // Theoretically, a variation of the branch-free code for integer division by
4118 // a power of 2 (calculating the remainder via an additional multiplication
4119 // (which gets simplified to an 'and') and subtraction) should be faster, and
4120 // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
4121 // indicate that positive dividends are heavily favored, so the branching
4122 // version performs better.
4123 HMod* hmod = instr->hydrogen();
4124 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
4125 Label dividend_is_not_negative, done;
4126 if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
4127 __ Tbz(dividend, kWSignBit, ÷nd_is_not_negative);
4128 // Note that this is correct even for kMinInt operands.
4129 __ Neg(dividend, dividend);
4130 __ And(dividend, dividend, mask);
4131 __ Negs(dividend, dividend);
4132 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
4133 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
4138 __ bind(÷nd_is_not_negative);
4139 __ And(dividend, dividend, mask);
4144 void LCodeGen::DoModByConstI(LModByConstI* instr) {
4145 Register dividend = ToRegister32(instr->dividend());
4146 int32_t divisor = instr->divisor();
4147 Register result = ToRegister32(instr->result());
4148 Register temp = ToRegister32(instr->temp());
4149 DCHECK(!AreAliased(dividend, result, temp));
4152 Deoptimize(instr, Deoptimizer::kDivisionByZero);
4156 __ TruncatingDiv(result, dividend, Abs(divisor));
4157 __ Sxtw(dividend.X(), dividend);
4158 __ Mov(temp, Abs(divisor));
4159 __ Smsubl(result.X(), result, temp, dividend.X());
4161 // Check for negative zero.
4162 HMod* hmod = instr->hydrogen();
4163 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
4164 Label remainder_not_zero;
4165 __ Cbnz(result, &remainder_not_zero);
4166 DeoptimizeIfNegative(dividend, instr, Deoptimizer::kMinusZero);
4167 __ bind(&remainder_not_zero);
4172 void LCodeGen::DoModI(LModI* instr) {
4173 Register dividend = ToRegister32(instr->left());
4174 Register divisor = ToRegister32(instr->right());
4175 Register result = ToRegister32(instr->result());
4178 // modulo = dividend - quotient * divisor
4179 __ Sdiv(result, dividend, divisor);
4180 if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) {
4181 DeoptimizeIfZero(divisor, instr, Deoptimizer::kDivisionByZero);
4183 __ Msub(result, result, divisor, dividend);
4184 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
4185 __ Cbnz(result, &done);
4186 DeoptimizeIfNegative(dividend, instr, Deoptimizer::kMinusZero);
4192 void LCodeGen::DoMulConstIS(LMulConstIS* instr) {
4193 DCHECK(instr->hydrogen()->representation().IsSmiOrInteger32());
4194 bool is_smi = instr->hydrogen()->representation().IsSmi();
4196 is_smi ? ToRegister(instr->result()) : ToRegister32(instr->result());
4198 is_smi ? ToRegister(instr->left()) : ToRegister32(instr->left()) ;
4199 int32_t right = ToInteger32(instr->right());
4200 DCHECK((right > -kMaxInt) && (right < kMaxInt));
4202 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
4203 bool bailout_on_minus_zero =
4204 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
4206 if (bailout_on_minus_zero) {
4208 // The result is -0 if right is negative and left is zero.
4209 DeoptimizeIfZero(left, instr, Deoptimizer::kMinusZero);
4210 } else if (right == 0) {
4211 // The result is -0 if the right is zero and the left is negative.
4212 DeoptimizeIfNegative(left, instr, Deoptimizer::kMinusZero);
4217 // Cases which can detect overflow.
4220 // Only 0x80000000 can overflow here.
4221 __ Negs(result, left);
4222 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
4224 __ Neg(result, left);
4228 // This case can never overflow.
4232 // This case can never overflow.
4233 __ Mov(result, left, kDiscardForSameWReg);
4237 __ Adds(result, left, left);
4238 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
4240 __ Add(result, left, left);
4245 // Multiplication by constant powers of two (and some related values)
4246 // can be done efficiently with shifted operands.
4247 int32_t right_abs = Abs(right);
4249 if (base::bits::IsPowerOfTwo32(right_abs)) {
4250 int right_log2 = WhichPowerOf2(right_abs);
4253 Register scratch = result;
4254 DCHECK(!AreAliased(scratch, left));
4255 __ Cls(scratch, left);
4256 __ Cmp(scratch, right_log2);
4257 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow);
4261 // result = left << log2(right)
4262 __ Lsl(result, left, right_log2);
4264 // result = -left << log2(-right)
4266 __ Negs(result, Operand(left, LSL, right_log2));
4267 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
4269 __ Neg(result, Operand(left, LSL, right_log2));
4276 // For the following cases, we could perform a conservative overflow check
4277 // with CLS as above. However the few cycles saved are likely not worth
4278 // the risk of deoptimizing more often than required.
4279 DCHECK(!can_overflow);
4282 if (base::bits::IsPowerOfTwo32(right - 1)) {
4283 // result = left + left << log2(right - 1)
4284 __ Add(result, left, Operand(left, LSL, WhichPowerOf2(right - 1)));
4285 } else if (base::bits::IsPowerOfTwo32(right + 1)) {
4286 // result = -left + left << log2(right + 1)
4287 __ Sub(result, left, Operand(left, LSL, WhichPowerOf2(right + 1)));
4288 __ Neg(result, result);
4293 if (base::bits::IsPowerOfTwo32(-right + 1)) {
4294 // result = left - left << log2(-right + 1)
4295 __ Sub(result, left, Operand(left, LSL, WhichPowerOf2(-right + 1)));
4296 } else if (base::bits::IsPowerOfTwo32(-right - 1)) {
4297 // result = -left - left << log2(-right - 1)
4298 __ Add(result, left, Operand(left, LSL, WhichPowerOf2(-right - 1)));
4299 __ Neg(result, result);
4308 void LCodeGen::DoMulI(LMulI* instr) {
4309 Register result = ToRegister32(instr->result());
4310 Register left = ToRegister32(instr->left());
4311 Register right = ToRegister32(instr->right());
4313 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
4314 bool bailout_on_minus_zero =
4315 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
4317 if (bailout_on_minus_zero && !left.Is(right)) {
4318 // If one operand is zero and the other is negative, the result is -0.
4319 // - Set Z (eq) if either left or right, or both, are 0.
4321 __ Ccmp(right, 0, ZFlag, ne);
4322 // - If so (eq), set N (mi) if left + right is negative.
4323 // - Otherwise, clear N.
4324 __ Ccmn(left, right, NoFlag, eq);
4325 DeoptimizeIf(mi, instr, Deoptimizer::kMinusZero);
4329 __ Smull(result.X(), left, right);
4330 __ Cmp(result.X(), Operand(result, SXTW));
4331 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow);
4333 __ Mul(result, left, right);
4338 void LCodeGen::DoMulS(LMulS* instr) {
4339 Register result = ToRegister(instr->result());
4340 Register left = ToRegister(instr->left());
4341 Register right = ToRegister(instr->right());
4343 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
4344 bool bailout_on_minus_zero =
4345 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
4347 if (bailout_on_minus_zero && !left.Is(right)) {
4348 // If one operand is zero and the other is negative, the result is -0.
4349 // - Set Z (eq) if either left or right, or both, are 0.
4351 __ Ccmp(right, 0, ZFlag, ne);
4352 // - If so (eq), set N (mi) if left + right is negative.
4353 // - Otherwise, clear N.
4354 __ Ccmn(left, right, NoFlag, eq);
4355 DeoptimizeIf(mi, instr, Deoptimizer::kMinusZero);
4358 STATIC_ASSERT((kSmiShift == 32) && (kSmiTag == 0));
4360 __ Smulh(result, left, right);
4361 __ Cmp(result, Operand(result.W(), SXTW));
4363 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow);
4365 if (AreAliased(result, left, right)) {
4366 // All three registers are the same: half untag the input and then
4367 // multiply, giving a tagged result.
4368 STATIC_ASSERT((kSmiShift % 2) == 0);
4369 __ Asr(result, left, kSmiShift / 2);
4370 __ Mul(result, result, result);
4371 } else if (result.Is(left) && !left.Is(right)) {
4372 // Registers result and left alias, right is distinct: untag left into
4373 // result, and then multiply by right, giving a tagged result.
4374 __ SmiUntag(result, left);
4375 __ Mul(result, result, right);
4377 DCHECK(!left.Is(result));
4378 // Registers result and right alias, left is distinct, or all registers
4379 // are distinct: untag right into result, and then multiply by left,
4380 // giving a tagged result.
4381 __ SmiUntag(result, right);
4382 __ Mul(result, left, result);
4388 void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4389 // TODO(3095996): Get rid of this. For now, we need to make the
4390 // result register contain a valid pointer because it is already
4391 // contained in the register pointer map.
4392 Register result = ToRegister(instr->result());
4395 PushSafepointRegistersScope scope(this);
4396 // NumberTagU and NumberTagD use the context from the frame, rather than
4397 // the environment's HContext or HInlinedContext value.
4398 // They only call Runtime::kAllocateHeapNumber.
4399 // The corresponding HChange instructions are added in a phase that does
4400 // not have easy access to the local context.
4401 __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4402 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4403 RecordSafepointWithRegisters(
4404 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4405 __ StoreToSafepointRegisterSlot(x0, result);
4409 void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
4410 class DeferredNumberTagD: public LDeferredCode {
4412 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4413 : LDeferredCode(codegen), instr_(instr) { }
4414 virtual void Generate() { codegen()->DoDeferredNumberTagD(instr_); }
4415 virtual LInstruction* instr() { return instr_; }
4417 LNumberTagD* instr_;
4420 DoubleRegister input = ToDoubleRegister(instr->value());
4421 Register result = ToRegister(instr->result());
4422 Register temp1 = ToRegister(instr->temp1());
4423 Register temp2 = ToRegister(instr->temp2());
4425 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
4426 if (FLAG_inline_new) {
4427 __ AllocateHeapNumber(result, deferred->entry(), temp1, temp2);
4429 __ B(deferred->entry());
4432 __ Bind(deferred->exit());
4433 __ Str(input, FieldMemOperand(result, HeapNumber::kValueOffset));
4437 void LCodeGen::DoDeferredNumberTagU(LInstruction* instr,
4441 Label slow, convert_and_store;
4442 Register src = ToRegister32(value);
4443 Register dst = ToRegister(instr->result());
4444 Register scratch1 = ToRegister(temp1);
4446 if (FLAG_inline_new) {
4447 Register scratch2 = ToRegister(temp2);
4448 __ AllocateHeapNumber(dst, &slow, scratch1, scratch2);
4449 __ B(&convert_and_store);
4452 // Slow case: call the runtime system to do the number allocation.
4454 // TODO(3095996): Put a valid pointer value in the stack slot where the result
4455 // register is stored, as this register is in the pointer map, but contains an
4459 // Preserve the value of all registers.
4460 PushSafepointRegistersScope scope(this);
4462 // NumberTagU and NumberTagD use the context from the frame, rather than
4463 // the environment's HContext or HInlinedContext value.
4464 // They only call Runtime::kAllocateHeapNumber.
4465 // The corresponding HChange instructions are added in a phase that does
4466 // not have easy access to the local context.
4467 __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4468 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4469 RecordSafepointWithRegisters(
4470 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4471 __ StoreToSafepointRegisterSlot(x0, dst);
4474 // Convert number to floating point and store in the newly allocated heap
4476 __ Bind(&convert_and_store);
4477 DoubleRegister dbl_scratch = double_scratch();
4478 __ Ucvtf(dbl_scratch, src);
4479 __ Str(dbl_scratch, FieldMemOperand(dst, HeapNumber::kValueOffset));
4483 void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4484 class DeferredNumberTagU: public LDeferredCode {
4486 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4487 : LDeferredCode(codegen), instr_(instr) { }
4488 virtual void Generate() {
4489 codegen()->DoDeferredNumberTagU(instr_,
4494 virtual LInstruction* instr() { return instr_; }
4496 LNumberTagU* instr_;
4499 Register value = ToRegister32(instr->value());
4500 Register result = ToRegister(instr->result());
4502 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4503 __ Cmp(value, Smi::kMaxValue);
4504 __ B(hi, deferred->entry());
4505 __ SmiTag(result, value.X());
4506 __ Bind(deferred->exit());
4510 void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
4511 Register input = ToRegister(instr->value());
4512 Register scratch = ToRegister(instr->temp());
4513 DoubleRegister result = ToDoubleRegister(instr->result());
4514 bool can_convert_undefined_to_nan =
4515 instr->hydrogen()->can_convert_undefined_to_nan();
4517 Label done, load_smi;
4519 // Work out what untag mode we're working with.
4520 HValue* value = instr->hydrogen()->value();
4521 NumberUntagDMode mode = value->representation().IsSmi()
4522 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
4524 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4525 __ JumpIfSmi(input, &load_smi);
4527 Label convert_undefined;
4529 // Heap number map check.
4530 if (can_convert_undefined_to_nan) {
4531 __ JumpIfNotHeapNumber(input, &convert_undefined);
4533 DeoptimizeIfNotHeapNumber(input, instr);
4536 // Load heap number.
4537 __ Ldr(result, FieldMemOperand(input, HeapNumber::kValueOffset));
4538 if (instr->hydrogen()->deoptimize_on_minus_zero()) {
4539 DeoptimizeIfMinusZero(result, instr, Deoptimizer::kMinusZero);
4543 if (can_convert_undefined_to_nan) {
4544 __ Bind(&convert_undefined);
4545 DeoptimizeIfNotRoot(input, Heap::kUndefinedValueRootIndex, instr,
4546 Deoptimizer::kNotAHeapNumberUndefined);
4548 __ LoadRoot(scratch, Heap::kNanValueRootIndex);
4549 __ Ldr(result, FieldMemOperand(scratch, HeapNumber::kValueOffset));
4554 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
4555 // Fall through to load_smi.
4558 // Smi to double register conversion.
4560 __ SmiUntagToDouble(result, input);
4566 void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
4567 // This is a pseudo-instruction that ensures that the environment here is
4568 // properly registered for deoptimization and records the assembler's PC
4570 LEnvironment* environment = instr->environment();
4572 // If the environment were already registered, we would have no way of
4573 // backpatching it with the spill slot operands.
4574 DCHECK(!environment->HasBeenRegistered());
4575 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
4577 GenerateOsrPrologue();
4581 void LCodeGen::DoParameter(LParameter* instr) {
4586 void LCodeGen::DoPreparePushArguments(LPreparePushArguments* instr) {
4587 __ PushPreamble(instr->argc(), kPointerSize);
4591 void LCodeGen::DoPushArguments(LPushArguments* instr) {
4592 MacroAssembler::PushPopQueue args(masm());
4594 for (int i = 0; i < instr->ArgumentCount(); ++i) {
4595 LOperand* arg = instr->argument(i);
4596 if (arg->IsDoubleRegister() || arg->IsDoubleStackSlot()) {
4597 Abort(kDoPushArgumentNotImplementedForDoubleType);
4600 args.Queue(ToRegister(arg));
4603 // The preamble was done by LPreparePushArguments.
4604 args.PushQueued(MacroAssembler::PushPopQueue::SKIP_PREAMBLE);
4606 RecordPushedArgumentsDelta(instr->ArgumentCount());
4610 void LCodeGen::DoReturn(LReturn* instr) {
4611 if (FLAG_trace && info()->IsOptimizing()) {
4612 // Push the return value on the stack as the parameter.
4613 // Runtime::TraceExit returns its parameter in x0. We're leaving the code
4614 // managed by the register allocator and tearing down the frame, it's
4615 // safe to write to the context register.
4617 __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4618 __ CallRuntime(Runtime::kTraceExit, 1);
4621 if (info()->saves_caller_doubles()) {
4622 RestoreCallerDoubles();
4625 int no_frame_start = -1;
4626 if (NeedsEagerFrame()) {
4627 Register stack_pointer = masm()->StackPointer();
4628 __ Mov(stack_pointer, fp);
4629 no_frame_start = masm_->pc_offset();
4633 if (instr->has_constant_parameter_count()) {
4634 int parameter_count = ToInteger32(instr->constant_parameter_count());
4635 __ Drop(parameter_count + 1);
4637 DCHECK(info()->IsStub()); // Functions would need to drop one more value.
4638 Register parameter_count = ToRegister(instr->parameter_count());
4639 __ DropBySMI(parameter_count);
4643 if (no_frame_start != -1) {
4644 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
4649 MemOperand LCodeGen::BuildSeqStringOperand(Register string,
4652 String::Encoding encoding) {
4653 if (index->IsConstantOperand()) {
4654 int offset = ToInteger32(LConstantOperand::cast(index));
4655 if (encoding == String::TWO_BYTE_ENCODING) {
4656 offset *= kUC16Size;
4658 STATIC_ASSERT(kCharSize == 1);
4659 return FieldMemOperand(string, SeqString::kHeaderSize + offset);
4662 __ Add(temp, string, SeqString::kHeaderSize - kHeapObjectTag);
4663 if (encoding == String::ONE_BYTE_ENCODING) {
4664 return MemOperand(temp, ToRegister32(index), SXTW);
4666 STATIC_ASSERT(kUC16Size == 2);
4667 return MemOperand(temp, ToRegister32(index), SXTW, 1);
4672 void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
4673 String::Encoding encoding = instr->hydrogen()->encoding();
4674 Register string = ToRegister(instr->string());
4675 Register result = ToRegister(instr->result());
4676 Register temp = ToRegister(instr->temp());
4678 if (FLAG_debug_code) {
4679 // Even though this lithium instruction comes with a temp register, we
4680 // can't use it here because we want to use "AtStart" constraints on the
4681 // inputs and the debug code here needs a scratch register.
4682 UseScratchRegisterScope temps(masm());
4683 Register dbg_temp = temps.AcquireX();
4685 __ Ldr(dbg_temp, FieldMemOperand(string, HeapObject::kMapOffset));
4686 __ Ldrb(dbg_temp, FieldMemOperand(dbg_temp, Map::kInstanceTypeOffset));
4688 __ And(dbg_temp, dbg_temp,
4689 Operand(kStringRepresentationMask | kStringEncodingMask));
4690 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
4691 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
4692 __ Cmp(dbg_temp, Operand(encoding == String::ONE_BYTE_ENCODING
4693 ? one_byte_seq_type : two_byte_seq_type));
4694 __ Check(eq, kUnexpectedStringType);
4697 MemOperand operand =
4698 BuildSeqStringOperand(string, temp, instr->index(), encoding);
4699 if (encoding == String::ONE_BYTE_ENCODING) {
4700 __ Ldrb(result, operand);
4702 __ Ldrh(result, operand);
4707 void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
4708 String::Encoding encoding = instr->hydrogen()->encoding();
4709 Register string = ToRegister(instr->string());
4710 Register value = ToRegister(instr->value());
4711 Register temp = ToRegister(instr->temp());
4713 if (FLAG_debug_code) {
4714 DCHECK(ToRegister(instr->context()).is(cp));
4715 Register index = ToRegister(instr->index());
4716 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
4717 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
4719 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
4720 ? one_byte_seq_type : two_byte_seq_type;
4721 __ EmitSeqStringSetCharCheck(string, index, kIndexIsInteger32, temp,
4724 MemOperand operand =
4725 BuildSeqStringOperand(string, temp, instr->index(), encoding);
4726 if (encoding == String::ONE_BYTE_ENCODING) {
4727 __ Strb(value, operand);
4729 __ Strh(value, operand);
4734 void LCodeGen::DoSmiTag(LSmiTag* instr) {
4735 HChange* hchange = instr->hydrogen();
4736 Register input = ToRegister(instr->value());
4737 Register output = ToRegister(instr->result());
4738 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4739 hchange->value()->CheckFlag(HValue::kUint32)) {
4740 DeoptimizeIfNegative(input.W(), instr, Deoptimizer::kOverflow);
4742 __ SmiTag(output, input);
4746 void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4747 Register input = ToRegister(instr->value());
4748 Register result = ToRegister(instr->result());
4751 if (instr->needs_check()) {
4752 DeoptimizeIfNotSmi(input, instr, Deoptimizer::kNotASmi);
4756 __ SmiUntag(result, input);
4761 void LCodeGen::DoShiftI(LShiftI* instr) {
4762 LOperand* right_op = instr->right();
4763 Register left = ToRegister32(instr->left());
4764 Register result = ToRegister32(instr->result());
4766 if (right_op->IsRegister()) {
4767 Register right = ToRegister32(instr->right());
4768 switch (instr->op()) {
4769 case Token::ROR: __ Ror(result, left, right); break;
4770 case Token::SAR: __ Asr(result, left, right); break;
4771 case Token::SHL: __ Lsl(result, left, right); break;
4773 __ Lsr(result, left, right);
4774 if (instr->can_deopt()) {
4775 // If `left >>> right` >= 0x80000000, the result is not representable
4776 // in a signed 32-bit smi.
4777 DeoptimizeIfNegative(result, instr, Deoptimizer::kNegativeValue);
4780 default: UNREACHABLE();
4783 DCHECK(right_op->IsConstantOperand());
4784 int shift_count = JSShiftAmountFromLConstant(right_op);
4785 if (shift_count == 0) {
4786 if ((instr->op() == Token::SHR) && instr->can_deopt()) {
4787 DeoptimizeIfNegative(left, instr, Deoptimizer::kNegativeValue);
4789 __ Mov(result, left, kDiscardForSameWReg);
4791 switch (instr->op()) {
4792 case Token::ROR: __ Ror(result, left, shift_count); break;
4793 case Token::SAR: __ Asr(result, left, shift_count); break;
4794 case Token::SHL: __ Lsl(result, left, shift_count); break;
4795 case Token::SHR: __ Lsr(result, left, shift_count); break;
4796 default: UNREACHABLE();
4803 void LCodeGen::DoShiftS(LShiftS* instr) {
4804 LOperand* right_op = instr->right();
4805 Register left = ToRegister(instr->left());
4806 Register result = ToRegister(instr->result());
4808 if (right_op->IsRegister()) {
4809 Register right = ToRegister(instr->right());
4811 // JavaScript shifts only look at the bottom 5 bits of the 'right' operand.
4812 // Since we're handling smis in X registers, we have to extract these bits
4814 __ Ubfx(result, right, kSmiShift, 5);
4816 switch (instr->op()) {
4818 // This is the only case that needs a scratch register. To keep things
4819 // simple for the other cases, borrow a MacroAssembler scratch register.
4820 UseScratchRegisterScope temps(masm());
4821 Register temp = temps.AcquireW();
4822 __ SmiUntag(temp, left);
4823 __ Ror(result.W(), temp.W(), result.W());
4828 __ Asr(result, left, result);
4829 __ Bic(result, result, kSmiShiftMask);
4832 __ Lsl(result, left, result);
4835 __ Lsr(result, left, result);
4836 __ Bic(result, result, kSmiShiftMask);
4837 if (instr->can_deopt()) {
4838 // If `left >>> right` >= 0x80000000, the result is not representable
4839 // in a signed 32-bit smi.
4840 DeoptimizeIfNegative(result, instr, Deoptimizer::kNegativeValue);
4843 default: UNREACHABLE();
4846 DCHECK(right_op->IsConstantOperand());
4847 int shift_count = JSShiftAmountFromLConstant(right_op);
4848 if (shift_count == 0) {
4849 if ((instr->op() == Token::SHR) && instr->can_deopt()) {
4850 DeoptimizeIfNegative(left, instr, Deoptimizer::kNegativeValue);
4852 __ Mov(result, left);
4854 switch (instr->op()) {
4856 __ SmiUntag(result, left);
4857 __ Ror(result.W(), result.W(), shift_count);
4861 __ Asr(result, left, shift_count);
4862 __ Bic(result, result, kSmiShiftMask);
4865 __ Lsl(result, left, shift_count);
4868 __ Lsr(result, left, shift_count);
4869 __ Bic(result, result, kSmiShiftMask);
4871 default: UNREACHABLE();
4878 void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
4879 __ Debug("LDebugBreak", 0, BREAK);
4883 void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
4884 DCHECK(ToRegister(instr->context()).is(cp));
4885 Register scratch1 = x5;
4886 Register scratch2 = x6;
4887 DCHECK(instr->IsMarkedAsCall());
4889 // TODO(all): if Mov could handle object in new space then it could be used
4891 __ LoadHeapObject(scratch1, instr->hydrogen()->pairs());
4892 __ Mov(scratch2, Smi::FromInt(instr->hydrogen()->flags()));
4893 __ Push(scratch1, scratch2);
4894 CallRuntime(Runtime::kDeclareGlobals, 2, instr);
4898 void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
4899 PushSafepointRegistersScope scope(this);
4900 LoadContextFromDeferred(instr->context());
4901 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
4902 RecordSafepointWithLazyDeopt(
4903 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
4904 DCHECK(instr->HasEnvironment());
4905 LEnvironment* env = instr->environment();
4906 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
4910 void LCodeGen::DoStackCheck(LStackCheck* instr) {
4911 class DeferredStackCheck: public LDeferredCode {
4913 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
4914 : LDeferredCode(codegen), instr_(instr) { }
4915 virtual void Generate() { codegen()->DoDeferredStackCheck(instr_); }
4916 virtual LInstruction* instr() { return instr_; }
4918 LStackCheck* instr_;
4921 DCHECK(instr->HasEnvironment());
4922 LEnvironment* env = instr->environment();
4923 // There is no LLazyBailout instruction for stack-checks. We have to
4924 // prepare for lazy deoptimization explicitly here.
4925 if (instr->hydrogen()->is_function_entry()) {
4926 // Perform stack overflow check.
4928 __ CompareRoot(masm()->StackPointer(), Heap::kStackLimitRootIndex);
4931 PredictableCodeSizeScope predictable(masm_,
4932 Assembler::kCallSizeWithRelocation);
4933 DCHECK(instr->context()->IsRegister());
4934 DCHECK(ToRegister(instr->context()).is(cp));
4935 CallCode(isolate()->builtins()->StackCheck(),
4936 RelocInfo::CODE_TARGET,
4940 DCHECK(instr->hydrogen()->is_backwards_branch());
4941 // Perform stack overflow check if this goto needs it before jumping.
4942 DeferredStackCheck* deferred_stack_check =
4943 new(zone()) DeferredStackCheck(this, instr);
4944 __ CompareRoot(masm()->StackPointer(), Heap::kStackLimitRootIndex);
4945 __ B(lo, deferred_stack_check->entry());
4947 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
4948 __ Bind(instr->done_label());
4949 deferred_stack_check->SetExit(instr->done_label());
4950 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
4951 // Don't record a deoptimization index for the safepoint here.
4952 // This will be done explicitly when emitting call and the safepoint in
4953 // the deferred code.
4958 void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
4959 Register function = ToRegister(instr->function());
4960 Register code_object = ToRegister(instr->code_object());
4961 Register temp = ToRegister(instr->temp());
4962 __ Add(temp, code_object, Code::kHeaderSize - kHeapObjectTag);
4963 __ Str(temp, FieldMemOperand(function, JSFunction::kCodeEntryOffset));
4967 void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
4968 Register context = ToRegister(instr->context());
4969 Register value = ToRegister(instr->value());
4970 Register scratch = ToRegister(instr->temp());
4971 MemOperand target = ContextMemOperand(context, instr->slot_index());
4973 Label skip_assignment;
4975 if (instr->hydrogen()->RequiresHoleCheck()) {
4976 __ Ldr(scratch, target);
4977 if (instr->hydrogen()->DeoptimizesOnHole()) {
4978 DeoptimizeIfRoot(scratch, Heap::kTheHoleValueRootIndex, instr,
4979 Deoptimizer::kHole);
4981 __ JumpIfNotRoot(scratch, Heap::kTheHoleValueRootIndex, &skip_assignment);
4985 __ Str(value, target);
4986 if (instr->hydrogen()->NeedsWriteBarrier()) {
4987 SmiCheck check_needed =
4988 instr->hydrogen()->value()->type().IsHeapObject()
4989 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4990 __ RecordWriteContextSlot(context, static_cast<int>(target.offset()), value,
4991 scratch, GetLinkRegisterState(), kSaveFPRegs,
4992 EMIT_REMEMBERED_SET, check_needed);
4994 __ Bind(&skip_assignment);
4998 void LCodeGen::DoStoreKeyedExternal(LStoreKeyedExternal* instr) {
4999 Register ext_ptr = ToRegister(instr->elements());
5000 Register key = no_reg;
5002 ElementsKind elements_kind = instr->elements_kind();
5004 bool key_is_smi = instr->hydrogen()->key()->representation().IsSmi();
5005 bool key_is_constant = instr->key()->IsConstantOperand();
5006 int constant_key = 0;
5007 if (key_is_constant) {
5008 DCHECK(instr->temp() == NULL);
5009 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
5010 if (constant_key & 0xf0000000) {
5011 Abort(kArrayIndexConstantValueTooBig);
5014 key = ToRegister(instr->key());
5015 scratch = ToRegister(instr->temp());
5019 PrepareKeyedExternalArrayOperand(key, ext_ptr, scratch, key_is_smi,
5020 key_is_constant, constant_key,
5022 instr->base_offset());
5024 if (elements_kind == FLOAT32_ELEMENTS) {
5025 DoubleRegister value = ToDoubleRegister(instr->value());
5026 DoubleRegister dbl_scratch = double_scratch();
5027 __ Fcvt(dbl_scratch.S(), value);
5028 __ Str(dbl_scratch.S(), dst);
5029 } else if (elements_kind == FLOAT64_ELEMENTS) {
5030 DoubleRegister value = ToDoubleRegister(instr->value());
5033 Register value = ToRegister(instr->value());
5035 switch (elements_kind) {
5036 case UINT8_ELEMENTS:
5037 case UINT8_CLAMPED_ELEMENTS:
5039 __ Strb(value, dst);
5041 case INT16_ELEMENTS:
5042 case UINT16_ELEMENTS:
5043 __ Strh(value, dst);
5045 case INT32_ELEMENTS:
5046 case UINT32_ELEMENTS:
5047 __ Str(value.W(), dst);
5049 case FLOAT32_ELEMENTS:
5050 case FLOAT64_ELEMENTS:
5051 case FAST_DOUBLE_ELEMENTS:
5053 case FAST_SMI_ELEMENTS:
5054 case FAST_HOLEY_DOUBLE_ELEMENTS:
5055 case FAST_HOLEY_ELEMENTS:
5056 case FAST_HOLEY_SMI_ELEMENTS:
5057 case DICTIONARY_ELEMENTS:
5058 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
5059 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
5067 void LCodeGen::DoStoreKeyedFixedDouble(LStoreKeyedFixedDouble* instr) {
5068 Register elements = ToRegister(instr->elements());
5069 DoubleRegister value = ToDoubleRegister(instr->value());
5072 if (instr->key()->IsConstantOperand()) {
5073 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
5074 if (constant_key & 0xf0000000) {
5075 Abort(kArrayIndexConstantValueTooBig);
5077 int offset = instr->base_offset() + constant_key * kDoubleSize;
5078 mem_op = MemOperand(elements, offset);
5080 Register store_base = ToRegister(instr->temp());
5081 Register key = ToRegister(instr->key());
5082 bool key_is_tagged = instr->hydrogen()->key()->representation().IsSmi();
5083 mem_op = PrepareKeyedArrayOperand(store_base, elements, key, key_is_tagged,
5084 instr->hydrogen()->elements_kind(),
5085 instr->hydrogen()->representation(),
5086 instr->base_offset());
5089 if (instr->NeedsCanonicalization()) {
5090 __ CanonicalizeNaN(double_scratch(), value);
5091 __ Str(double_scratch(), mem_op);
5093 __ Str(value, mem_op);
5098 void LCodeGen::DoStoreKeyedFixed(LStoreKeyedFixed* instr) {
5099 Register value = ToRegister(instr->value());
5100 Register elements = ToRegister(instr->elements());
5101 Register scratch = no_reg;
5102 Register store_base = no_reg;
5103 Register key = no_reg;
5106 if (!instr->key()->IsConstantOperand() ||
5107 instr->hydrogen()->NeedsWriteBarrier()) {
5108 scratch = ToRegister(instr->temp());
5111 Representation representation = instr->hydrogen()->value()->representation();
5112 if (instr->key()->IsConstantOperand()) {
5113 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
5114 int offset = instr->base_offset() +
5115 ToInteger32(const_operand) * kPointerSize;
5116 store_base = elements;
5117 if (representation.IsInteger32()) {
5118 DCHECK(instr->hydrogen()->store_mode() == STORE_TO_INITIALIZED_ENTRY);
5119 DCHECK(instr->hydrogen()->elements_kind() == FAST_SMI_ELEMENTS);
5120 STATIC_ASSERT(static_cast<unsigned>(kSmiValueSize) == kWRegSizeInBits);
5121 STATIC_ASSERT(kSmiTag == 0);
5122 mem_op = UntagSmiMemOperand(store_base, offset);
5124 mem_op = MemOperand(store_base, offset);
5127 store_base = scratch;
5128 key = ToRegister(instr->key());
5129 bool key_is_tagged = instr->hydrogen()->key()->representation().IsSmi();
5131 mem_op = PrepareKeyedArrayOperand(store_base, elements, key, key_is_tagged,
5132 instr->hydrogen()->elements_kind(),
5133 representation, instr->base_offset());
5136 __ Store(value, mem_op, representation);
5138 if (instr->hydrogen()->NeedsWriteBarrier()) {
5139 DCHECK(representation.IsTagged());
5140 // This assignment may cause element_addr to alias store_base.
5141 Register element_addr = scratch;
5142 SmiCheck check_needed =
5143 instr->hydrogen()->value()->type().IsHeapObject()
5144 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
5145 // Compute address of modified element and store it into key register.
5146 __ Add(element_addr, mem_op.base(), mem_op.OffsetAsOperand());
5147 __ RecordWrite(elements, element_addr, value, GetLinkRegisterState(),
5148 kSaveFPRegs, EMIT_REMEMBERED_SET, check_needed,
5149 instr->hydrogen()->PointersToHereCheckForValue());
5154 void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
5155 DCHECK(ToRegister(instr->context()).is(cp));
5156 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
5157 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
5158 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
5160 if (instr->hydrogen()->HasVectorAndSlot()) {
5161 EmitVectorStoreICRegisters<LStoreKeyedGeneric>(instr);
5164 Handle<Code> ic = CodeFactory::KeyedStoreICInOptimizedCode(
5165 isolate(), instr->language_mode(),
5166 instr->hydrogen()->initialization_state()).code();
5167 CallCode(ic, RelocInfo::CODE_TARGET, instr);
5171 void LCodeGen::DoMaybeGrowElements(LMaybeGrowElements* instr) {
5172 class DeferredMaybeGrowElements final : public LDeferredCode {
5174 DeferredMaybeGrowElements(LCodeGen* codegen, LMaybeGrowElements* instr)
5175 : LDeferredCode(codegen), instr_(instr) {}
5176 void Generate() override { codegen()->DoDeferredMaybeGrowElements(instr_); }
5177 LInstruction* instr() override { return instr_; }
5180 LMaybeGrowElements* instr_;
5183 Register result = x0;
5184 DeferredMaybeGrowElements* deferred =
5185 new (zone()) DeferredMaybeGrowElements(this, instr);
5186 LOperand* key = instr->key();
5187 LOperand* current_capacity = instr->current_capacity();
5189 DCHECK(instr->hydrogen()->key()->representation().IsInteger32());
5190 DCHECK(instr->hydrogen()->current_capacity()->representation().IsInteger32());
5191 DCHECK(key->IsConstantOperand() || key->IsRegister());
5192 DCHECK(current_capacity->IsConstantOperand() ||
5193 current_capacity->IsRegister());
5195 if (key->IsConstantOperand() && current_capacity->IsConstantOperand()) {
5196 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
5197 int32_t constant_capacity =
5198 ToInteger32(LConstantOperand::cast(current_capacity));
5199 if (constant_key >= constant_capacity) {
5201 __ B(deferred->entry());
5203 } else if (key->IsConstantOperand()) {
5204 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
5205 __ Cmp(ToRegister(current_capacity), Operand(constant_key));
5206 __ B(le, deferred->entry());
5207 } else if (current_capacity->IsConstantOperand()) {
5208 int32_t constant_capacity =
5209 ToInteger32(LConstantOperand::cast(current_capacity));
5210 __ Cmp(ToRegister(key), Operand(constant_capacity));
5211 __ B(ge, deferred->entry());
5213 __ Cmp(ToRegister(key), ToRegister(current_capacity));
5214 __ B(ge, deferred->entry());
5217 __ Mov(result, ToRegister(instr->elements()));
5219 __ Bind(deferred->exit());
5223 void LCodeGen::DoDeferredMaybeGrowElements(LMaybeGrowElements* instr) {
5224 // TODO(3095996): Get rid of this. For now, we need to make the
5225 // result register contain a valid pointer because it is already
5226 // contained in the register pointer map.
5227 Register result = x0;
5230 // We have to call a stub.
5232 PushSafepointRegistersScope scope(this);
5233 __ Move(result, ToRegister(instr->object()));
5235 LOperand* key = instr->key();
5236 if (key->IsConstantOperand()) {
5237 __ Mov(x3, Operand(ToSmi(LConstantOperand::cast(key))));
5239 __ Mov(x3, ToRegister(key));
5243 GrowArrayElementsStub stub(isolate(), instr->hydrogen()->is_js_array(),
5244 instr->hydrogen()->kind());
5246 RecordSafepointWithLazyDeopt(
5247 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
5248 __ StoreToSafepointRegisterSlot(result, result);
5251 // Deopt on smi, which means the elements array changed to dictionary mode.
5252 DeoptimizeIfSmi(result, instr, Deoptimizer::kSmi);
5256 void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
5257 Representation representation = instr->representation();
5259 Register object = ToRegister(instr->object());
5260 HObjectAccess access = instr->hydrogen()->access();
5261 int offset = access.offset();
5263 if (access.IsExternalMemory()) {
5264 DCHECK(!instr->hydrogen()->has_transition());
5265 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
5266 Register value = ToRegister(instr->value());
5267 __ Store(value, MemOperand(object, offset), representation);
5271 __ AssertNotSmi(object);
5273 if (!FLAG_unbox_double_fields && representation.IsDouble()) {
5274 DCHECK(access.IsInobject());
5275 DCHECK(!instr->hydrogen()->has_transition());
5276 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
5277 FPRegister value = ToDoubleRegister(instr->value());
5278 __ Str(value, FieldMemOperand(object, offset));
5282 DCHECK(!representation.IsSmi() ||
5283 !instr->value()->IsConstantOperand() ||
5284 IsInteger32Constant(LConstantOperand::cast(instr->value())));
5286 if (instr->hydrogen()->has_transition()) {
5287 Handle<Map> transition = instr->hydrogen()->transition_map();
5288 AddDeprecationDependency(transition);
5289 // Store the new map value.
5290 Register new_map_value = ToRegister(instr->temp0());
5291 __ Mov(new_map_value, Operand(transition));
5292 __ Str(new_map_value, FieldMemOperand(object, HeapObject::kMapOffset));
5293 if (instr->hydrogen()->NeedsWriteBarrierForMap()) {
5294 // Update the write barrier for the map field.
5295 __ RecordWriteForMap(object,
5297 ToRegister(instr->temp1()),
5298 GetLinkRegisterState(),
5304 Register destination;
5305 if (access.IsInobject()) {
5306 destination = object;
5308 Register temp0 = ToRegister(instr->temp0());
5309 __ Ldr(temp0, FieldMemOperand(object, JSObject::kPropertiesOffset));
5310 destination = temp0;
5313 if (FLAG_unbox_double_fields && representation.IsDouble()) {
5314 DCHECK(access.IsInobject());
5315 FPRegister value = ToDoubleRegister(instr->value());
5316 __ Str(value, FieldMemOperand(object, offset));
5317 } else if (representation.IsSmi() &&
5318 instr->hydrogen()->value()->representation().IsInteger32()) {
5319 DCHECK(instr->hydrogen()->store_mode() == STORE_TO_INITIALIZED_ENTRY);
5321 Register temp0 = ToRegister(instr->temp0());
5322 __ Ldr(temp0, FieldMemOperand(destination, offset));
5323 __ AssertSmi(temp0);
5324 // If destination aliased temp0, restore it to the address calculated
5326 if (destination.Is(temp0)) {
5327 DCHECK(!access.IsInobject());
5328 __ Ldr(destination, FieldMemOperand(object, JSObject::kPropertiesOffset));
5331 STATIC_ASSERT(static_cast<unsigned>(kSmiValueSize) == kWRegSizeInBits);
5332 STATIC_ASSERT(kSmiTag == 0);
5333 Register value = ToRegister(instr->value());
5334 __ Store(value, UntagSmiFieldMemOperand(destination, offset),
5335 Representation::Integer32());
5337 Register value = ToRegister(instr->value());
5338 __ Store(value, FieldMemOperand(destination, offset), representation);
5340 if (instr->hydrogen()->NeedsWriteBarrier()) {
5341 Register value = ToRegister(instr->value());
5342 __ RecordWriteField(destination,
5344 value, // Clobbered.
5345 ToRegister(instr->temp1()), // Clobbered.
5346 GetLinkRegisterState(),
5348 EMIT_REMEMBERED_SET,
5349 instr->hydrogen()->SmiCheckForWriteBarrier(),
5350 instr->hydrogen()->PointersToHereCheckForValue());
5355 void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
5356 DCHECK(ToRegister(instr->context()).is(cp));
5357 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
5358 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
5360 if (instr->hydrogen()->HasVectorAndSlot()) {
5361 EmitVectorStoreICRegisters<LStoreNamedGeneric>(instr);
5364 __ Mov(StoreDescriptor::NameRegister(), Operand(instr->name()));
5365 Handle<Code> ic = CodeFactory::StoreICInOptimizedCode(
5366 isolate(), instr->language_mode(),
5367 instr->hydrogen()->initialization_state()).code();
5368 CallCode(ic, RelocInfo::CODE_TARGET, instr);
5372 void LCodeGen::DoStoreGlobalViaContext(LStoreGlobalViaContext* instr) {
5373 DCHECK(ToRegister(instr->context()).is(cp));
5374 DCHECK(ToRegister(instr->value())
5375 .is(StoreGlobalViaContextDescriptor::ValueRegister()));
5377 int const slot = instr->slot_index();
5378 int const depth = instr->depth();
5379 if (depth <= StoreGlobalViaContextStub::kMaximumDepth) {
5380 __ Mov(StoreGlobalViaContextDescriptor::SlotRegister(), Operand(slot));
5381 Handle<Code> stub = CodeFactory::StoreGlobalViaContext(
5382 isolate(), depth, instr->language_mode())
5384 CallCode(stub, RelocInfo::CODE_TARGET, instr);
5386 __ Push(Smi::FromInt(slot));
5387 __ Push(StoreGlobalViaContextDescriptor::ValueRegister());
5388 __ CallRuntime(is_strict(instr->language_mode())
5389 ? Runtime::kStoreGlobalViaContext_Strict
5390 : Runtime::kStoreGlobalViaContext_Sloppy,
5396 void LCodeGen::DoStringAdd(LStringAdd* instr) {
5397 DCHECK(ToRegister(instr->context()).is(cp));
5398 DCHECK(ToRegister(instr->left()).Is(x1));
5399 DCHECK(ToRegister(instr->right()).Is(x0));
5400 StringAddStub stub(isolate(),
5401 instr->hydrogen()->flags(),
5402 instr->hydrogen()->pretenure_flag());
5403 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5407 void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
5408 class DeferredStringCharCodeAt: public LDeferredCode {
5410 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
5411 : LDeferredCode(codegen), instr_(instr) { }
5412 virtual void Generate() { codegen()->DoDeferredStringCharCodeAt(instr_); }
5413 virtual LInstruction* instr() { return instr_; }
5415 LStringCharCodeAt* instr_;
5418 DeferredStringCharCodeAt* deferred =
5419 new(zone()) DeferredStringCharCodeAt(this, instr);
5421 StringCharLoadGenerator::Generate(masm(),
5422 ToRegister(instr->string()),
5423 ToRegister32(instr->index()),
5424 ToRegister(instr->result()),
5426 __ Bind(deferred->exit());
5430 void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
5431 Register string = ToRegister(instr->string());
5432 Register result = ToRegister(instr->result());
5434 // TODO(3095996): Get rid of this. For now, we need to make the
5435 // result register contain a valid pointer because it is already
5436 // contained in the register pointer map.
5439 PushSafepointRegistersScope scope(this);
5441 // Push the index as a smi. This is safe because of the checks in
5442 // DoStringCharCodeAt above.
5443 Register index = ToRegister(instr->index());
5444 __ SmiTagAndPush(index);
5446 CallRuntimeFromDeferred(Runtime::kStringCharCodeAtRT, 2, instr,
5450 __ StoreToSafepointRegisterSlot(x0, result);
5454 void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
5455 class DeferredStringCharFromCode: public LDeferredCode {
5457 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
5458 : LDeferredCode(codegen), instr_(instr) { }
5459 virtual void Generate() { codegen()->DoDeferredStringCharFromCode(instr_); }
5460 virtual LInstruction* instr() { return instr_; }
5462 LStringCharFromCode* instr_;
5465 DeferredStringCharFromCode* deferred =
5466 new(zone()) DeferredStringCharFromCode(this, instr);
5468 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
5469 Register char_code = ToRegister32(instr->char_code());
5470 Register result = ToRegister(instr->result());
5472 __ Cmp(char_code, String::kMaxOneByteCharCode);
5473 __ B(hi, deferred->entry());
5474 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
5475 __ Add(result, result, FixedArray::kHeaderSize - kHeapObjectTag);
5476 __ Ldr(result, MemOperand(result, char_code, SXTW, kPointerSizeLog2));
5477 __ CompareRoot(result, Heap::kUndefinedValueRootIndex);
5478 __ B(eq, deferred->entry());
5479 __ Bind(deferred->exit());
5483 void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
5484 Register char_code = ToRegister(instr->char_code());
5485 Register result = ToRegister(instr->result());
5487 // TODO(3095996): Get rid of this. For now, we need to make the
5488 // result register contain a valid pointer because it is already
5489 // contained in the register pointer map.
5492 PushSafepointRegistersScope scope(this);
5493 __ SmiTagAndPush(char_code);
5494 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr, instr->context());
5495 __ StoreToSafepointRegisterSlot(x0, result);
5499 void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
5500 DCHECK(ToRegister(instr->context()).is(cp));
5501 DCHECK(ToRegister(instr->left()).is(x1));
5502 DCHECK(ToRegister(instr->right()).is(x0));
5504 Handle<Code> code = CodeFactory::StringCompare(isolate()).code();
5505 CallCode(code, RelocInfo::CODE_TARGET, instr);
5507 EmitCompareAndBranch(instr, TokenToCondition(instr->op(), false), x0, 0);
5511 void LCodeGen::DoSubI(LSubI* instr) {
5512 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
5513 Register result = ToRegister32(instr->result());
5514 Register left = ToRegister32(instr->left());
5515 Operand right = ToShiftedRightOperand32(instr->right(), instr);
5518 __ Subs(result, left, right);
5519 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
5521 __ Sub(result, left, right);
5526 void LCodeGen::DoSubS(LSubS* instr) {
5527 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
5528 Register result = ToRegister(instr->result());
5529 Register left = ToRegister(instr->left());
5530 Operand right = ToOperand(instr->right());
5532 __ Subs(result, left, right);
5533 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
5535 __ Sub(result, left, right);
5540 void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr,
5544 Register input = ToRegister(value);
5545 Register scratch1 = ToRegister(temp1);
5546 DoubleRegister dbl_scratch1 = double_scratch();
5550 if (instr->truncating()) {
5551 Register output = ToRegister(instr->result());
5554 // If it's not a heap number, jump to undefined check.
5555 __ JumpIfNotHeapNumber(input, &check_bools);
5557 // A heap number: load value and convert to int32 using truncating function.
5558 __ TruncateHeapNumberToI(output, input);
5561 __ Bind(&check_bools);
5563 Register true_root = output;
5564 Register false_root = scratch1;
5565 __ LoadTrueFalseRoots(true_root, false_root);
5566 __ Cmp(input, true_root);
5567 __ Cset(output, eq);
5568 __ Ccmp(input, false_root, ZFlag, ne);
5571 // Output contains zero, undefined is converted to zero for truncating
5573 DeoptimizeIfNotRoot(input, Heap::kUndefinedValueRootIndex, instr,
5574 Deoptimizer::kNotAHeapNumberUndefinedBoolean);
5576 Register output = ToRegister32(instr->result());
5577 DoubleRegister dbl_scratch2 = ToDoubleRegister(temp2);
5579 DeoptimizeIfNotHeapNumber(input, instr);
5581 // A heap number: load value and convert to int32 using non-truncating
5582 // function. If the result is out of range, branch to deoptimize.
5583 __ Ldr(dbl_scratch1, FieldMemOperand(input, HeapNumber::kValueOffset));
5584 __ TryRepresentDoubleAsInt32(output, dbl_scratch1, dbl_scratch2);
5585 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN);
5587 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5590 __ Fmov(scratch1, dbl_scratch1);
5591 DeoptimizeIfNegative(scratch1, instr, Deoptimizer::kMinusZero);
5598 void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
5599 class DeferredTaggedToI: public LDeferredCode {
5601 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
5602 : LDeferredCode(codegen), instr_(instr) { }
5603 virtual void Generate() {
5604 codegen()->DoDeferredTaggedToI(instr_, instr_->value(), instr_->temp1(),
5608 virtual LInstruction* instr() { return instr_; }
5613 Register input = ToRegister(instr->value());
5614 Register output = ToRegister(instr->result());
5616 if (instr->hydrogen()->value()->representation().IsSmi()) {
5617 __ SmiUntag(output, input);
5619 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
5621 __ JumpIfNotSmi(input, deferred->entry());
5622 __ SmiUntag(output, input);
5623 __ Bind(deferred->exit());
5628 void LCodeGen::DoThisFunction(LThisFunction* instr) {
5629 Register result = ToRegister(instr->result());
5630 __ Ldr(result, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5634 void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
5635 DCHECK(ToRegister(instr->value()).Is(x0));
5636 DCHECK(ToRegister(instr->result()).Is(x0));
5638 CallRuntime(Runtime::kToFastProperties, 1, instr);
5642 void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
5643 DCHECK(ToRegister(instr->context()).is(cp));
5645 // Registers will be used as follows:
5646 // x7 = literals array.
5647 // x1 = regexp literal.
5648 // x0 = regexp literal clone.
5649 // x10-x12 are used as temporaries.
5650 int literal_offset =
5651 FixedArray::OffsetOfElementAt(instr->hydrogen()->literal_index());
5652 __ LoadObject(x7, instr->hydrogen()->literals());
5653 __ Ldr(x1, FieldMemOperand(x7, literal_offset));
5654 __ JumpIfNotRoot(x1, Heap::kUndefinedValueRootIndex, &materialized);
5656 // Create regexp literal using runtime function
5657 // Result will be in x0.
5658 __ Mov(x12, Operand(Smi::FromInt(instr->hydrogen()->literal_index())));
5659 __ Mov(x11, Operand(instr->hydrogen()->pattern()));
5660 __ Mov(x10, Operand(instr->hydrogen()->flags()));
5661 __ Push(x7, x12, x11, x10);
5662 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
5665 __ Bind(&materialized);
5666 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
5667 Label allocated, runtime_allocate;
5669 __ Allocate(size, x0, x10, x11, &runtime_allocate, TAG_OBJECT);
5672 __ Bind(&runtime_allocate);
5673 __ Mov(x0, Smi::FromInt(size));
5675 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
5678 __ Bind(&allocated);
5679 // Copy the content into the newly allocated memory.
5680 __ CopyFields(x0, x1, CPURegList(x10, x11, x12), size / kPointerSize);
5684 void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
5685 Register object = ToRegister(instr->object());
5687 Handle<Map> from_map = instr->original_map();
5688 Handle<Map> to_map = instr->transitioned_map();
5689 ElementsKind from_kind = instr->from_kind();
5690 ElementsKind to_kind = instr->to_kind();
5692 Label not_applicable;
5694 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
5695 Register temp1 = ToRegister(instr->temp1());
5696 Register new_map = ToRegister(instr->temp2());
5697 __ CheckMap(object, temp1, from_map, ¬_applicable, DONT_DO_SMI_CHECK);
5698 __ Mov(new_map, Operand(to_map));
5699 __ Str(new_map, FieldMemOperand(object, HeapObject::kMapOffset));
5701 __ RecordWriteForMap(object, new_map, temp1, GetLinkRegisterState(),
5705 UseScratchRegisterScope temps(masm());
5706 // Use the temp register only in a restricted scope - the codegen checks
5707 // that we do not use any register across a call.
5708 __ CheckMap(object, temps.AcquireX(), from_map, ¬_applicable,
5711 DCHECK(object.is(x0));
5712 DCHECK(ToRegister(instr->context()).is(cp));
5713 PushSafepointRegistersScope scope(this);
5714 __ Mov(x1, Operand(to_map));
5715 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
5716 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
5718 RecordSafepointWithRegisters(
5719 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
5721 __ Bind(¬_applicable);
5725 void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
5726 Register object = ToRegister(instr->object());
5727 Register temp1 = ToRegister(instr->temp1());
5728 Register temp2 = ToRegister(instr->temp2());
5730 Label no_memento_found;
5731 __ TestJSArrayForAllocationMemento(object, temp1, temp2, &no_memento_found);
5732 DeoptimizeIf(eq, instr, Deoptimizer::kMementoFound);
5733 __ Bind(&no_memento_found);
5737 void LCodeGen::DoTruncateDoubleToIntOrSmi(LTruncateDoubleToIntOrSmi* instr) {
5738 DoubleRegister input = ToDoubleRegister(instr->value());
5739 Register result = ToRegister(instr->result());
5740 __ TruncateDoubleToI(result, input);
5741 if (instr->tag_result()) {
5742 __ SmiTag(result, result);
5747 void LCodeGen::DoTypeof(LTypeof* instr) {
5748 DCHECK(ToRegister(instr->value()).is(x3));
5749 DCHECK(ToRegister(instr->result()).is(x0));
5751 Register value_register = ToRegister(instr->value());
5752 __ JumpIfNotSmi(value_register, &do_call);
5753 __ Mov(x0, Immediate(isolate()->factory()->number_string()));
5756 TypeofStub stub(isolate());
5757 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5762 void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5763 Handle<String> type_name = instr->type_literal();
5764 Label* true_label = instr->TrueLabel(chunk_);
5765 Label* false_label = instr->FalseLabel(chunk_);
5766 Register value = ToRegister(instr->value());
5768 Factory* factory = isolate()->factory();
5769 if (String::Equals(type_name, factory->number_string())) {
5770 __ JumpIfSmi(value, true_label);
5772 int true_block = instr->TrueDestination(chunk_);
5773 int false_block = instr->FalseDestination(chunk_);
5774 int next_block = GetNextEmittedBlock();
5776 if (true_block == false_block) {
5777 EmitGoto(true_block);
5778 } else if (true_block == next_block) {
5779 __ JumpIfNotHeapNumber(value, chunk_->GetAssemblyLabel(false_block));
5781 __ JumpIfHeapNumber(value, chunk_->GetAssemblyLabel(true_block));
5782 if (false_block != next_block) {
5783 __ B(chunk_->GetAssemblyLabel(false_block));
5787 } else if (String::Equals(type_name, factory->string_string())) {
5788 DCHECK((instr->temp1() != NULL) && (instr->temp2() != NULL));
5789 Register map = ToRegister(instr->temp1());
5790 Register scratch = ToRegister(instr->temp2());
5792 __ JumpIfSmi(value, false_label);
5793 __ CompareObjectType(value, map, scratch, FIRST_NONSTRING_TYPE);
5794 EmitBranch(instr, lt);
5796 } else if (String::Equals(type_name, factory->symbol_string())) {
5797 DCHECK((instr->temp1() != NULL) && (instr->temp2() != NULL));
5798 Register map = ToRegister(instr->temp1());
5799 Register scratch = ToRegister(instr->temp2());
5801 __ JumpIfSmi(value, false_label);
5802 __ CompareObjectType(value, map, scratch, SYMBOL_TYPE);
5803 EmitBranch(instr, eq);
5805 } else if (String::Equals(type_name, factory->boolean_string())) {
5806 __ JumpIfRoot(value, Heap::kTrueValueRootIndex, true_label);
5807 __ CompareRoot(value, Heap::kFalseValueRootIndex);
5808 EmitBranch(instr, eq);
5810 } else if (String::Equals(type_name, factory->undefined_string())) {
5811 DCHECK(instr->temp1() != NULL);
5812 Register scratch = ToRegister(instr->temp1());
5814 __ JumpIfRoot(value, Heap::kUndefinedValueRootIndex, true_label);
5815 __ JumpIfSmi(value, false_label);
5816 // Check for undetectable objects and jump to the true branch in this case.
5817 __ Ldr(scratch, FieldMemOperand(value, HeapObject::kMapOffset));
5818 __ Ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5819 EmitTestAndBranch(instr, ne, scratch, 1 << Map::kIsUndetectable);
5821 } else if (String::Equals(type_name, factory->function_string())) {
5822 DCHECK(instr->temp1() != NULL);
5823 Register scratch = ToRegister(instr->temp1());
5825 __ JumpIfSmi(value, false_label);
5826 __ Ldr(scratch, FieldMemOperand(value, HeapObject::kMapOffset));
5827 __ Ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5828 __ And(scratch, scratch,
5829 (1 << Map::kIsCallable) | (1 << Map::kIsUndetectable));
5830 EmitCompareAndBranch(instr, eq, scratch, 1 << Map::kIsCallable);
5832 } else if (String::Equals(type_name, factory->object_string())) {
5833 DCHECK((instr->temp1() != NULL) && (instr->temp2() != NULL));
5834 Register map = ToRegister(instr->temp1());
5835 Register scratch = ToRegister(instr->temp2());
5837 __ JumpIfSmi(value, false_label);
5838 __ JumpIfRoot(value, Heap::kNullValueRootIndex, true_label);
5839 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
5840 __ JumpIfObjectType(value, map, scratch, FIRST_SPEC_OBJECT_TYPE,
5842 // Check for callable or undetectable objects => false.
5843 __ Ldrb(scratch, FieldMemOperand(map, Map::kBitFieldOffset));
5844 EmitTestAndBranch(instr, eq, scratch,
5845 (1 << Map::kIsCallable) | (1 << Map::kIsUndetectable));
5848 #define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
5849 } else if (String::Equals(type_name, factory->type##_string())) { \
5850 DCHECK((instr->temp1() != NULL) && (instr->temp2() != NULL)); \
5851 Register map = ToRegister(instr->temp1()); \
5853 __ JumpIfSmi(value, false_label); \
5854 __ Ldr(map, FieldMemOperand(value, HeapObject::kMapOffset)); \
5855 __ CompareRoot(map, Heap::k##Type##MapRootIndex); \
5856 EmitBranch(instr, eq);
5857 SIMD128_TYPES(SIMD128_TYPE)
5867 void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
5868 __ Ucvtf(ToDoubleRegister(instr->result()), ToRegister32(instr->value()));
5872 void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5873 Register object = ToRegister(instr->value());
5874 Register map = ToRegister(instr->map());
5875 Register temp = ToRegister(instr->temp());
5876 __ Ldr(temp, FieldMemOperand(object, HeapObject::kMapOffset));
5878 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap);
5882 void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
5883 Register receiver = ToRegister(instr->receiver());
5884 Register function = ToRegister(instr->function());
5885 Register result = ToRegister(instr->result());
5887 // If the receiver is null or undefined, we have to pass the global object as
5888 // a receiver to normal functions. Values have to be passed unchanged to
5889 // builtins and strict-mode functions.
5890 Label global_object, done, copy_receiver;
5892 if (!instr->hydrogen()->known_function()) {
5893 __ Ldr(result, FieldMemOperand(function,
5894 JSFunction::kSharedFunctionInfoOffset));
5896 // CompilerHints is an int32 field. See objects.h.
5898 FieldMemOperand(result, SharedFunctionInfo::kCompilerHintsOffset));
5900 // Do not transform the receiver to object for strict mode functions.
5901 __ Tbnz(result, SharedFunctionInfo::kStrictModeFunction, ©_receiver);
5903 // Do not transform the receiver to object for builtins.
5904 __ Tbnz(result, SharedFunctionInfo::kNative, ©_receiver);
5907 // Normal function. Replace undefined or null with global receiver.
5908 __ JumpIfRoot(receiver, Heap::kNullValueRootIndex, &global_object);
5909 __ JumpIfRoot(receiver, Heap::kUndefinedValueRootIndex, &global_object);
5911 // Deoptimize if the receiver is not a JS object.
5912 DeoptimizeIfSmi(receiver, instr, Deoptimizer::kSmi);
5913 __ CompareObjectType(receiver, result, result, FIRST_SPEC_OBJECT_TYPE);
5914 __ B(ge, ©_receiver);
5915 Deoptimize(instr, Deoptimizer::kNotAJavaScriptObject);
5917 __ Bind(&global_object);
5918 __ Ldr(result, FieldMemOperand(function, JSFunction::kContextOffset));
5919 __ Ldr(result, ContextMemOperand(result, Context::GLOBAL_OBJECT_INDEX));
5920 __ Ldr(result, FieldMemOperand(result, GlobalObject::kGlobalProxyOffset));
5923 __ Bind(©_receiver);
5924 __ Mov(result, receiver);
5929 void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5933 PushSafepointRegistersScope scope(this);
5937 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
5938 RecordSafepointWithRegisters(
5939 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
5940 __ StoreToSafepointRegisterSlot(x0, result);
5944 void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
5945 class DeferredLoadMutableDouble final : public LDeferredCode {
5947 DeferredLoadMutableDouble(LCodeGen* codegen,
5948 LLoadFieldByIndex* instr,
5952 : LDeferredCode(codegen),
5958 void Generate() override {
5959 codegen()->DoDeferredLoadMutableDouble(instr_, result_, object_, index_);
5961 LInstruction* instr() override { return instr_; }
5964 LLoadFieldByIndex* instr_;
5969 Register object = ToRegister(instr->object());
5970 Register index = ToRegister(instr->index());
5971 Register result = ToRegister(instr->result());
5973 __ AssertSmi(index);
5975 DeferredLoadMutableDouble* deferred;
5976 deferred = new(zone()) DeferredLoadMutableDouble(
5977 this, instr, result, object, index);
5979 Label out_of_object, done;
5981 __ TestAndBranchIfAnySet(
5982 index, reinterpret_cast<uint64_t>(Smi::FromInt(1)), deferred->entry());
5983 __ Mov(index, Operand(index, ASR, 1));
5985 __ Cmp(index, Smi::FromInt(0));
5986 __ B(lt, &out_of_object);
5988 STATIC_ASSERT(kPointerSizeLog2 > kSmiTagSize);
5989 __ Add(result, object, Operand::UntagSmiAndScale(index, kPointerSizeLog2));
5990 __ Ldr(result, FieldMemOperand(result, JSObject::kHeaderSize));
5994 __ Bind(&out_of_object);
5995 __ Ldr(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
5996 // Index is equal to negated out of object property index plus 1.
5997 __ Sub(result, result, Operand::UntagSmiAndScale(index, kPointerSizeLog2));
5998 __ Ldr(result, FieldMemOperand(result,
5999 FixedArray::kHeaderSize - kPointerSize));
6000 __ Bind(deferred->exit());
6005 void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
6006 Register context = ToRegister(instr->context());
6007 __ Str(context, MemOperand(fp, StandardFrameConstants::kContextOffset));
6011 void LCodeGen::DoAllocateBlockContext(LAllocateBlockContext* instr) {
6012 Handle<ScopeInfo> scope_info = instr->scope_info();
6013 __ Push(scope_info);
6014 __ Push(ToRegister(instr->function()));
6015 CallRuntime(Runtime::kPushBlockContext, 2, instr);
6016 RecordSafepoint(Safepoint::kNoLazyDeopt);
6020 } // namespace internal