1 // Copyright 2012 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/code-factory.h"
6 #include "src/code-stubs.h"
7 #include "src/cpu-profiler.h"
8 #include "src/hydrogen-osr.h"
10 #include "src/ic/stub-cache.h"
11 #include "src/mips64/lithium-codegen-mips64.h"
12 #include "src/mips64/lithium-gap-resolver-mips64.h"
18 class SafepointGenerator final : public CallWrapper {
20 SafepointGenerator(LCodeGen* codegen,
21 LPointerMap* pointers,
22 Safepoint::DeoptMode mode)
26 virtual ~SafepointGenerator() {}
28 void BeforeCall(int call_size) const override {}
30 void AfterCall() const override {
31 codegen_->RecordSafepoint(pointers_, deopt_mode_);
36 LPointerMap* pointers_;
37 Safepoint::DeoptMode deopt_mode_;
43 bool LCodeGen::GenerateCode() {
44 LPhase phase("Z_Code generation", chunk());
48 // Open a frame scope to indicate that there is a frame on the stack. The
49 // NONE indicates that the scope shouldn't actually generate code to set up
50 // the frame (that is done in GeneratePrologue).
51 FrameScope frame_scope(masm_, StackFrame::NONE);
53 return GeneratePrologue() && GenerateBody() && GenerateDeferredCode() &&
54 GenerateJumpTable() && GenerateSafepointTable();
58 void LCodeGen::FinishCode(Handle<Code> code) {
60 code->set_stack_slots(GetStackSlotCount());
61 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
62 PopulateDeoptimizationData(code);
66 void LCodeGen::SaveCallerDoubles() {
67 DCHECK(info()->saves_caller_doubles());
68 DCHECK(NeedsEagerFrame());
69 Comment(";;; Save clobbered callee double registers");
71 BitVector* doubles = chunk()->allocated_double_registers();
72 BitVector::Iterator save_iterator(doubles);
73 while (!save_iterator.Done()) {
74 __ sdc1(DoubleRegister::FromAllocationIndex(save_iterator.Current()),
75 MemOperand(sp, count * kDoubleSize));
76 save_iterator.Advance();
82 void LCodeGen::RestoreCallerDoubles() {
83 DCHECK(info()->saves_caller_doubles());
84 DCHECK(NeedsEagerFrame());
85 Comment(";;; Restore clobbered callee double registers");
86 BitVector* doubles = chunk()->allocated_double_registers();
87 BitVector::Iterator save_iterator(doubles);
89 while (!save_iterator.Done()) {
90 __ ldc1(DoubleRegister::FromAllocationIndex(save_iterator.Current()),
91 MemOperand(sp, count * kDoubleSize));
92 save_iterator.Advance();
98 bool LCodeGen::GeneratePrologue() {
99 DCHECK(is_generating());
101 if (info()->IsOptimizing()) {
102 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
105 if (strlen(FLAG_stop_at) > 0 &&
106 info_->literal()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
111 // a1: Callee's JS function.
112 // cp: Callee's context.
113 // fp: Caller's frame pointer.
116 // Sloppy mode functions and builtins need to replace the receiver with the
117 // global proxy when called as functions (without an explicit receiver
119 if (info()->MustReplaceUndefinedReceiverWithGlobalProxy()) {
121 int receiver_offset = info_->scope()->num_parameters() * kPointerSize;
122 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
123 __ ld(a2, MemOperand(sp, receiver_offset));
124 __ Branch(&ok, ne, a2, Operand(at));
126 __ ld(a2, GlobalObjectOperand());
127 __ ld(a2, FieldMemOperand(a2, GlobalObject::kGlobalProxyOffset));
129 __ sd(a2, MemOperand(sp, receiver_offset));
135 info()->set_prologue_offset(masm_->pc_offset());
136 if (NeedsEagerFrame()) {
137 if (info()->IsStub()) {
140 __ Prologue(info()->IsCodePreAgingActive());
142 frame_is_built_ = true;
143 info_->AddNoFrameRange(0, masm_->pc_offset());
146 // Reserve space for the stack slots needed by the code.
147 int slots = GetStackSlotCount();
149 if (FLAG_debug_code) {
150 __ Dsubu(sp, sp, Operand(slots * kPointerSize));
152 __ Daddu(a0, sp, Operand(slots * kPointerSize));
153 __ li(a1, Operand(kSlotsZapValue));
156 __ Dsubu(a0, a0, Operand(kPointerSize));
157 __ sd(a1, MemOperand(a0, 2 * kPointerSize));
158 __ Branch(&loop, ne, a0, Operand(sp));
161 __ Dsubu(sp, sp, Operand(slots * kPointerSize));
165 if (info()->saves_caller_doubles()) {
168 return !is_aborted();
172 void LCodeGen::DoPrologue(LPrologue* instr) {
173 Comment(";;; Prologue begin");
175 // Possibly allocate a local context.
176 if (info()->scope()->num_heap_slots() > 0) {
177 Comment(";;; Allocate local context");
178 bool need_write_barrier = true;
179 // Argument to NewContext is the function, which is in a1.
180 int slots = info()->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
181 Safepoint::DeoptMode deopt_mode = Safepoint::kNoLazyDeopt;
182 if (info()->scope()->is_script_scope()) {
184 __ Push(info()->scope()->GetScopeInfo(info()->isolate()));
185 __ CallRuntime(Runtime::kNewScriptContext, 2);
186 deopt_mode = Safepoint::kLazyDeopt;
187 } else if (slots <= FastNewContextStub::kMaximumSlots) {
188 FastNewContextStub stub(isolate(), slots);
190 // Result of FastNewContextStub is always in new space.
191 need_write_barrier = false;
194 __ CallRuntime(Runtime::kNewFunctionContext, 1);
196 RecordSafepoint(deopt_mode);
198 // Context is returned in both v0. It replaces the context passed to us.
199 // It's saved in the stack and kept live in cp.
201 __ sd(v0, MemOperand(fp, StandardFrameConstants::kContextOffset));
202 // Copy any necessary parameters into the context.
203 int num_parameters = scope()->num_parameters();
204 int first_parameter = scope()->has_this_declaration() ? -1 : 0;
205 for (int i = first_parameter; i < num_parameters; i++) {
206 Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
207 if (var->IsContextSlot()) {
208 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
209 (num_parameters - 1 - i) * kPointerSize;
210 // Load parameter from stack.
211 __ ld(a0, MemOperand(fp, parameter_offset));
212 // Store it in the context.
213 MemOperand target = ContextOperand(cp, var->index());
215 // Update the write barrier. This clobbers a3 and a0.
216 if (need_write_barrier) {
217 __ RecordWriteContextSlot(
218 cp, target.offset(), a0, a3, GetRAState(), kSaveFPRegs);
219 } else if (FLAG_debug_code) {
221 __ JumpIfInNewSpace(cp, a0, &done);
222 __ Abort(kExpectedNewSpaceObject);
227 Comment(";;; End allocate local context");
230 Comment(";;; Prologue end");
234 void LCodeGen::GenerateOsrPrologue() {
235 // Generate the OSR entry prologue at the first unknown OSR value, or if there
236 // are none, at the OSR entrypoint instruction.
237 if (osr_pc_offset_ >= 0) return;
239 osr_pc_offset_ = masm()->pc_offset();
241 // Adjust the frame size, subsuming the unoptimized frame into the
243 int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
245 __ Dsubu(sp, sp, Operand(slots * kPointerSize));
249 void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
250 if (instr->IsCall()) {
251 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
253 if (!instr->IsLazyBailout() && !instr->IsGap()) {
254 safepoints_.BumpLastLazySafepointIndex();
259 bool LCodeGen::GenerateDeferredCode() {
260 DCHECK(is_generating());
261 if (deferred_.length() > 0) {
262 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
263 LDeferredCode* code = deferred_[i];
266 instructions_->at(code->instruction_index())->hydrogen_value();
267 RecordAndWritePosition(
268 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
270 Comment(";;; <@%d,#%d> "
271 "-------------------- Deferred %s --------------------",
272 code->instruction_index(),
273 code->instr()->hydrogen_value()->id(),
274 code->instr()->Mnemonic());
275 __ bind(code->entry());
276 if (NeedsDeferredFrame()) {
277 Comment(";;; Build frame");
278 DCHECK(!frame_is_built_);
279 DCHECK(info()->IsStub());
280 frame_is_built_ = true;
281 __ MultiPush(cp.bit() | fp.bit() | ra.bit());
282 __ li(scratch0(), Operand(Smi::FromInt(StackFrame::STUB)));
285 Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
286 Comment(";;; Deferred code");
289 if (NeedsDeferredFrame()) {
290 Comment(";;; Destroy frame");
291 DCHECK(frame_is_built_);
293 __ MultiPop(cp.bit() | fp.bit() | ra.bit());
294 frame_is_built_ = false;
296 __ jmp(code->exit());
299 // Deferred code is the last part of the instruction sequence. Mark
300 // the generated code as done unless we bailed out.
301 if (!is_aborted()) status_ = DONE;
302 return !is_aborted();
306 bool LCodeGen::GenerateJumpTable() {
307 if (jump_table_.length() > 0) {
308 Comment(";;; -------------------- Jump table --------------------");
309 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
310 Label table_start, call_deopt_entry;
312 __ bind(&table_start);
314 Address base = jump_table_[0]->address;
315 for (int i = 0; i < jump_table_.length(); i++) {
316 Deoptimizer::JumpTableEntry* table_entry = jump_table_[i];
317 __ bind(&table_entry->label);
318 Address entry = table_entry->address;
319 DeoptComment(table_entry->deopt_info);
321 // Second-level deopt table entries are contiguous and small, so instead
322 // of loading the full, absolute address of each one, load the base
323 // address and add an immediate offset.
324 if (is_int16(entry - base)) {
325 if (table_entry->needs_frame) {
326 DCHECK(!info()->saves_caller_doubles());
327 Comment(";;; call deopt with frame");
328 __ MultiPush(cp.bit() | fp.bit() | ra.bit());
329 __ BranchAndLink(&needs_frame, USE_DELAY_SLOT);
330 __ li(t9, Operand(entry - base));
332 __ BranchAndLink(&call_deopt_entry, USE_DELAY_SLOT);
333 __ li(t9, Operand(entry - base));
337 __ li(t9, Operand(entry - base));
338 if (table_entry->needs_frame) {
339 DCHECK(!info()->saves_caller_doubles());
340 Comment(";;; call deopt with frame");
341 __ MultiPush(cp.bit() | fp.bit() | ra.bit());
342 __ BranchAndLink(&needs_frame);
344 __ BranchAndLink(&call_deopt_entry);
347 info()->LogDeoptCallPosition(masm()->pc_offset(),
348 table_entry->deopt_info.inlining_id);
350 if (needs_frame.is_linked()) {
351 __ bind(&needs_frame);
352 // This variant of deopt can only be used with stubs. Since we don't
353 // have a function pointer to install in the stack frame that we're
354 // building, install a special marker there instead.
355 DCHECK(info()->IsStub());
356 __ li(at, Operand(Smi::FromInt(StackFrame::STUB)));
358 __ Daddu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
361 Comment(";;; call deopt");
362 __ bind(&call_deopt_entry);
364 if (info()->saves_caller_doubles()) {
365 DCHECK(info()->IsStub());
366 RestoreCallerDoubles();
370 Operand(reinterpret_cast<int64_t>(base), RelocInfo::RUNTIME_ENTRY));
371 __ Daddu(t9, t9, Operand(at));
374 // The deoptimization jump table is the last part of the instruction
375 // sequence. Mark the generated code as done unless we bailed out.
376 if (!is_aborted()) status_ = DONE;
377 return !is_aborted();
381 bool LCodeGen::GenerateSafepointTable() {
383 safepoints_.Emit(masm(), GetStackSlotCount());
384 return !is_aborted();
388 Register LCodeGen::ToRegister(int index) const {
389 return Register::FromAllocationIndex(index);
393 DoubleRegister LCodeGen::ToDoubleRegister(int index) const {
394 return DoubleRegister::FromAllocationIndex(index);
398 Register LCodeGen::ToRegister(LOperand* op) const {
399 DCHECK(op->IsRegister());
400 return ToRegister(op->index());
404 Register LCodeGen::EmitLoadRegister(LOperand* op, Register scratch) {
405 if (op->IsRegister()) {
406 return ToRegister(op->index());
407 } else if (op->IsConstantOperand()) {
408 LConstantOperand* const_op = LConstantOperand::cast(op);
409 HConstant* constant = chunk_->LookupConstant(const_op);
410 Handle<Object> literal = constant->handle(isolate());
411 Representation r = chunk_->LookupLiteralRepresentation(const_op);
412 if (r.IsInteger32()) {
413 AllowDeferredHandleDereference get_number;
414 DCHECK(literal->IsNumber());
415 __ li(scratch, Operand(static_cast<int32_t>(literal->Number())));
416 } else if (r.IsSmi()) {
417 DCHECK(constant->HasSmiValue());
418 __ li(scratch, Operand(Smi::FromInt(constant->Integer32Value())));
419 } else if (r.IsDouble()) {
420 Abort(kEmitLoadRegisterUnsupportedDoubleImmediate);
422 DCHECK(r.IsSmiOrTagged());
423 __ li(scratch, literal);
426 } else if (op->IsStackSlot()) {
427 __ ld(scratch, ToMemOperand(op));
435 DoubleRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
436 DCHECK(op->IsDoubleRegister());
437 return ToDoubleRegister(op->index());
441 DoubleRegister LCodeGen::EmitLoadDoubleRegister(LOperand* op,
442 FloatRegister flt_scratch,
443 DoubleRegister dbl_scratch) {
444 if (op->IsDoubleRegister()) {
445 return ToDoubleRegister(op->index());
446 } else if (op->IsConstantOperand()) {
447 LConstantOperand* const_op = LConstantOperand::cast(op);
448 HConstant* constant = chunk_->LookupConstant(const_op);
449 Handle<Object> literal = constant->handle(isolate());
450 Representation r = chunk_->LookupLiteralRepresentation(const_op);
451 if (r.IsInteger32()) {
452 DCHECK(literal->IsNumber());
453 __ li(at, Operand(static_cast<int32_t>(literal->Number())));
454 __ mtc1(at, flt_scratch);
455 __ cvt_d_w(dbl_scratch, flt_scratch);
457 } else if (r.IsDouble()) {
458 Abort(kUnsupportedDoubleImmediate);
459 } else if (r.IsTagged()) {
460 Abort(kUnsupportedTaggedImmediate);
462 } else if (op->IsStackSlot()) {
463 MemOperand mem_op = ToMemOperand(op);
464 __ ldc1(dbl_scratch, mem_op);
472 Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
473 HConstant* constant = chunk_->LookupConstant(op);
474 DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
475 return constant->handle(isolate());
479 bool LCodeGen::IsInteger32(LConstantOperand* op) const {
480 return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
484 bool LCodeGen::IsSmi(LConstantOperand* op) const {
485 return chunk_->LookupLiteralRepresentation(op).IsSmi();
489 int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
490 // return ToRepresentation(op, Representation::Integer32());
491 HConstant* constant = chunk_->LookupConstant(op);
492 return constant->Integer32Value();
496 int64_t LCodeGen::ToRepresentation_donotuse(LConstantOperand* op,
497 const Representation& r) const {
498 HConstant* constant = chunk_->LookupConstant(op);
499 int32_t value = constant->Integer32Value();
500 if (r.IsInteger32()) return value;
501 DCHECK(r.IsSmiOrTagged());
502 return reinterpret_cast<int64_t>(Smi::FromInt(value));
506 Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
507 HConstant* constant = chunk_->LookupConstant(op);
508 return Smi::FromInt(constant->Integer32Value());
512 double LCodeGen::ToDouble(LConstantOperand* op) const {
513 HConstant* constant = chunk_->LookupConstant(op);
514 DCHECK(constant->HasDoubleValue());
515 return constant->DoubleValue();
519 Operand LCodeGen::ToOperand(LOperand* op) {
520 if (op->IsConstantOperand()) {
521 LConstantOperand* const_op = LConstantOperand::cast(op);
522 HConstant* constant = chunk()->LookupConstant(const_op);
523 Representation r = chunk_->LookupLiteralRepresentation(const_op);
525 DCHECK(constant->HasSmiValue());
526 return Operand(Smi::FromInt(constant->Integer32Value()));
527 } else if (r.IsInteger32()) {
528 DCHECK(constant->HasInteger32Value());
529 return Operand(constant->Integer32Value());
530 } else if (r.IsDouble()) {
531 Abort(kToOperandUnsupportedDoubleImmediate);
533 DCHECK(r.IsTagged());
534 return Operand(constant->handle(isolate()));
535 } else if (op->IsRegister()) {
536 return Operand(ToRegister(op));
537 } else if (op->IsDoubleRegister()) {
538 Abort(kToOperandIsDoubleRegisterUnimplemented);
539 return Operand((int64_t)0);
541 // Stack slots not implemented, use ToMemOperand instead.
543 return Operand((int64_t)0);
547 static int ArgumentsOffsetWithoutFrame(int index) {
549 return -(index + 1) * kPointerSize;
553 MemOperand LCodeGen::ToMemOperand(LOperand* op) const {
554 DCHECK(!op->IsRegister());
555 DCHECK(!op->IsDoubleRegister());
556 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
557 if (NeedsEagerFrame()) {
558 return MemOperand(fp, StackSlotOffset(op->index()));
560 // Retrieve parameter without eager stack-frame relative to the
562 return MemOperand(sp, ArgumentsOffsetWithoutFrame(op->index()));
567 MemOperand LCodeGen::ToHighMemOperand(LOperand* op) const {
568 DCHECK(op->IsDoubleStackSlot());
569 if (NeedsEagerFrame()) {
570 // return MemOperand(fp, StackSlotOffset(op->index()) + kPointerSize);
571 return MemOperand(fp, StackSlotOffset(op->index()) + kIntSize);
573 // Retrieve parameter without eager stack-frame relative to the
575 // return MemOperand(
576 // sp, ArgumentsOffsetWithoutFrame(op->index()) + kPointerSize);
578 sp, ArgumentsOffsetWithoutFrame(op->index()) + kIntSize);
583 void LCodeGen::WriteTranslation(LEnvironment* environment,
584 Translation* translation) {
585 if (environment == NULL) return;
587 // The translation includes one command per value in the environment.
588 int translation_size = environment->translation_size();
590 WriteTranslation(environment->outer(), translation);
591 WriteTranslationFrame(environment, translation);
593 int object_index = 0;
594 int dematerialized_index = 0;
595 for (int i = 0; i < translation_size; ++i) {
596 LOperand* value = environment->values()->at(i);
598 environment, translation, value, environment->HasTaggedValueAt(i),
599 environment->HasUint32ValueAt(i), &object_index, &dematerialized_index);
604 void LCodeGen::AddToTranslation(LEnvironment* environment,
605 Translation* translation,
609 int* object_index_pointer,
610 int* dematerialized_index_pointer) {
611 if (op == LEnvironment::materialization_marker()) {
612 int object_index = (*object_index_pointer)++;
613 if (environment->ObjectIsDuplicateAt(object_index)) {
614 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
615 translation->DuplicateObject(dupe_of);
618 int object_length = environment->ObjectLengthAt(object_index);
619 if (environment->ObjectIsArgumentsAt(object_index)) {
620 translation->BeginArgumentsObject(object_length);
622 translation->BeginCapturedObject(object_length);
624 int dematerialized_index = *dematerialized_index_pointer;
625 int env_offset = environment->translation_size() + dematerialized_index;
626 *dematerialized_index_pointer += object_length;
627 for (int i = 0; i < object_length; ++i) {
628 LOperand* value = environment->values()->at(env_offset + i);
629 AddToTranslation(environment,
632 environment->HasTaggedValueAt(env_offset + i),
633 environment->HasUint32ValueAt(env_offset + i),
634 object_index_pointer,
635 dematerialized_index_pointer);
640 if (op->IsStackSlot()) {
641 int index = op->index();
643 index += StandardFrameConstants::kFixedFrameSize / kPointerSize;
646 translation->StoreStackSlot(index);
647 } else if (is_uint32) {
648 translation->StoreUint32StackSlot(index);
650 translation->StoreInt32StackSlot(index);
652 } else if (op->IsDoubleStackSlot()) {
653 int index = op->index();
655 index += StandardFrameConstants::kFixedFrameSize / kPointerSize;
657 translation->StoreDoubleStackSlot(index);
658 } else if (op->IsRegister()) {
659 Register reg = ToRegister(op);
661 translation->StoreRegister(reg);
662 } else if (is_uint32) {
663 translation->StoreUint32Register(reg);
665 translation->StoreInt32Register(reg);
667 } else if (op->IsDoubleRegister()) {
668 DoubleRegister reg = ToDoubleRegister(op);
669 translation->StoreDoubleRegister(reg);
670 } else if (op->IsConstantOperand()) {
671 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
672 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
673 translation->StoreLiteral(src_index);
680 void LCodeGen::CallCode(Handle<Code> code,
681 RelocInfo::Mode mode,
682 LInstruction* instr) {
683 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT);
687 void LCodeGen::CallCodeGeneric(Handle<Code> code,
688 RelocInfo::Mode mode,
690 SafepointMode safepoint_mode) {
691 DCHECK(instr != NULL);
693 RecordSafepointWithLazyDeopt(instr, safepoint_mode);
697 void LCodeGen::CallRuntime(const Runtime::Function* function,
700 SaveFPRegsMode save_doubles) {
701 DCHECK(instr != NULL);
703 __ CallRuntime(function, num_arguments, save_doubles);
705 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
709 void LCodeGen::LoadContextFromDeferred(LOperand* context) {
710 if (context->IsRegister()) {
711 __ Move(cp, ToRegister(context));
712 } else if (context->IsStackSlot()) {
713 __ ld(cp, ToMemOperand(context));
714 } else if (context->IsConstantOperand()) {
715 HConstant* constant =
716 chunk_->LookupConstant(LConstantOperand::cast(context));
717 __ li(cp, Handle<Object>::cast(constant->handle(isolate())));
724 void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
728 LoadContextFromDeferred(context);
729 __ CallRuntimeSaveDoubles(id);
730 RecordSafepointWithRegisters(
731 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
735 void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
736 Safepoint::DeoptMode mode) {
737 environment->set_has_been_used();
738 if (!environment->HasBeenRegistered()) {
739 // Physical stack frame layout:
740 // -x ............. -4 0 ..................................... y
741 // [incoming arguments] [spill slots] [pushed outgoing arguments]
743 // Layout of the environment:
744 // 0 ..................................................... size-1
745 // [parameters] [locals] [expression stack including arguments]
747 // Layout of the translation:
748 // 0 ........................................................ size - 1 + 4
749 // [expression stack including arguments] [locals] [4 words] [parameters]
750 // |>------------ translation_size ------------<|
753 int jsframe_count = 0;
754 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
756 if (e->frame_type() == JS_FUNCTION) {
760 Translation translation(&translations_, frame_count, jsframe_count, zone());
761 WriteTranslation(environment, &translation);
762 int deoptimization_index = deoptimizations_.length();
763 int pc_offset = masm()->pc_offset();
764 environment->Register(deoptimization_index,
766 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
767 deoptimizations_.Add(environment, zone());
772 void LCodeGen::DeoptimizeIf(Condition condition, LInstruction* instr,
773 Deoptimizer::DeoptReason deopt_reason,
774 Deoptimizer::BailoutType bailout_type,
775 Register src1, const Operand& src2) {
776 LEnvironment* environment = instr->environment();
777 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
778 DCHECK(environment->HasBeenRegistered());
779 int id = environment->deoptimization_index();
781 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
783 Abort(kBailoutWasNotPrepared);
787 if (FLAG_deopt_every_n_times != 0 && !info()->IsStub()) {
788 Register scratch = scratch0();
789 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
791 __ Push(a1, scratch);
792 __ li(scratch, Operand(count));
793 __ lw(a1, MemOperand(scratch));
794 __ Subu(a1, a1, Operand(1));
795 __ Branch(&no_deopt, ne, a1, Operand(zero_reg));
796 __ li(a1, Operand(FLAG_deopt_every_n_times));
797 __ sw(a1, MemOperand(scratch));
800 __ Call(entry, RelocInfo::RUNTIME_ENTRY);
802 __ sw(a1, MemOperand(scratch));
806 if (info()->ShouldTrapOnDeopt()) {
808 if (condition != al) {
809 __ Branch(&skip, NegateCondition(condition), src1, src2);
811 __ stop("trap_on_deopt");
815 Deoptimizer::DeoptInfo deopt_info = MakeDeoptInfo(instr, deopt_reason);
817 DCHECK(info()->IsStub() || frame_is_built_);
818 // Go through jump table if we need to handle condition, build frame, or
819 // restore caller doubles.
820 if (condition == al && frame_is_built_ &&
821 !info()->saves_caller_doubles()) {
822 DeoptComment(deopt_info);
823 __ Call(entry, RelocInfo::RUNTIME_ENTRY, condition, src1, src2);
824 info()->LogDeoptCallPosition(masm()->pc_offset(), deopt_info.inlining_id);
826 Deoptimizer::JumpTableEntry* table_entry =
827 new (zone()) Deoptimizer::JumpTableEntry(
828 entry, deopt_info, bailout_type, !frame_is_built_);
829 // We often have several deopts to the same entry, reuse the last
830 // jump entry if this is the case.
831 if (FLAG_trace_deopt || isolate()->cpu_profiler()->is_profiling() ||
832 jump_table_.is_empty() ||
833 !table_entry->IsEquivalentTo(*jump_table_.last())) {
834 jump_table_.Add(table_entry, zone());
836 __ Branch(&jump_table_.last()->label, condition, src1, src2);
841 void LCodeGen::DeoptimizeIf(Condition condition, LInstruction* instr,
842 Deoptimizer::DeoptReason deopt_reason,
843 Register src1, const Operand& src2) {
844 Deoptimizer::BailoutType bailout_type = info()->IsStub()
846 : Deoptimizer::EAGER;
847 DeoptimizeIf(condition, instr, deopt_reason, bailout_type, src1, src2);
851 void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
852 int length = deoptimizations_.length();
853 if (length == 0) return;
854 Handle<DeoptimizationInputData> data =
855 DeoptimizationInputData::New(isolate(), length, TENURED);
857 Handle<ByteArray> translations =
858 translations_.CreateByteArray(isolate()->factory());
859 data->SetTranslationByteArray(*translations);
860 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
861 data->SetOptimizationId(Smi::FromInt(info_->optimization_id()));
862 if (info_->IsOptimizing()) {
863 // Reference to shared function info does not change between phases.
864 AllowDeferredHandleDereference allow_handle_dereference;
865 data->SetSharedFunctionInfo(*info_->shared_info());
867 data->SetSharedFunctionInfo(Smi::FromInt(0));
869 data->SetWeakCellCache(Smi::FromInt(0));
871 Handle<FixedArray> literals =
872 factory()->NewFixedArray(deoptimization_literals_.length(), TENURED);
873 { AllowDeferredHandleDereference copy_handles;
874 for (int i = 0; i < deoptimization_literals_.length(); i++) {
875 literals->set(i, *deoptimization_literals_[i]);
877 data->SetLiteralArray(*literals);
880 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id().ToInt()));
881 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
883 // Populate the deoptimization entries.
884 for (int i = 0; i < length; i++) {
885 LEnvironment* env = deoptimizations_[i];
886 data->SetAstId(i, env->ast_id());
887 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
888 data->SetArgumentsStackHeight(i,
889 Smi::FromInt(env->arguments_stack_height()));
890 data->SetPc(i, Smi::FromInt(env->pc_offset()));
892 code->set_deoptimization_data(*data);
896 void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
897 DCHECK_EQ(0, deoptimization_literals_.length());
898 for (auto function : chunk()->inlined_functions()) {
899 DefineDeoptimizationLiteral(function);
901 inlined_function_count_ = deoptimization_literals_.length();
905 void LCodeGen::RecordSafepointWithLazyDeopt(
906 LInstruction* instr, SafepointMode safepoint_mode) {
907 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
908 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
910 DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
911 RecordSafepointWithRegisters(
912 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
917 void LCodeGen::RecordSafepoint(
918 LPointerMap* pointers,
919 Safepoint::Kind kind,
921 Safepoint::DeoptMode deopt_mode) {
922 DCHECK(expected_safepoint_kind_ == kind);
924 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
925 Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
926 kind, arguments, deopt_mode);
927 for (int i = 0; i < operands->length(); i++) {
928 LOperand* pointer = operands->at(i);
929 if (pointer->IsStackSlot()) {
930 safepoint.DefinePointerSlot(pointer->index(), zone());
931 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
932 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
938 void LCodeGen::RecordSafepoint(LPointerMap* pointers,
939 Safepoint::DeoptMode deopt_mode) {
940 RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
944 void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
945 LPointerMap empty_pointers(zone());
946 RecordSafepoint(&empty_pointers, deopt_mode);
950 void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
952 Safepoint::DeoptMode deopt_mode) {
954 pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
958 void LCodeGen::RecordAndWritePosition(int position) {
959 if (position == RelocInfo::kNoPosition) return;
960 masm()->positions_recorder()->RecordPosition(position);
961 masm()->positions_recorder()->WriteRecordedPositions();
965 static const char* LabelType(LLabel* label) {
966 if (label->is_loop_header()) return " (loop header)";
967 if (label->is_osr_entry()) return " (OSR entry)";
972 void LCodeGen::DoLabel(LLabel* label) {
973 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
974 current_instruction_,
975 label->hydrogen_value()->id(),
978 __ bind(label->label());
979 current_block_ = label->block_id();
984 void LCodeGen::DoParallelMove(LParallelMove* move) {
985 resolver_.Resolve(move);
989 void LCodeGen::DoGap(LGap* gap) {
990 for (int i = LGap::FIRST_INNER_POSITION;
991 i <= LGap::LAST_INNER_POSITION;
993 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
994 LParallelMove* move = gap->GetParallelMove(inner_pos);
995 if (move != NULL) DoParallelMove(move);
1000 void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
1005 void LCodeGen::DoParameter(LParameter* instr) {
1010 void LCodeGen::DoCallStub(LCallStub* instr) {
1011 DCHECK(ToRegister(instr->context()).is(cp));
1012 DCHECK(ToRegister(instr->result()).is(v0));
1013 switch (instr->hydrogen()->major_key()) {
1014 case CodeStub::RegExpExec: {
1015 RegExpExecStub stub(isolate());
1016 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1019 case CodeStub::SubString: {
1020 SubStringStub stub(isolate());
1021 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1024 case CodeStub::StringCompare: {
1025 StringCompareStub stub(isolate());
1026 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1035 void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
1036 GenerateOsrPrologue();
1040 void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
1041 Register dividend = ToRegister(instr->dividend());
1042 int32_t divisor = instr->divisor();
1043 DCHECK(dividend.is(ToRegister(instr->result())));
1045 // Theoretically, a variation of the branch-free code for integer division by
1046 // a power of 2 (calculating the remainder via an additional multiplication
1047 // (which gets simplified to an 'and') and subtraction) should be faster, and
1048 // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
1049 // indicate that positive dividends are heavily favored, so the branching
1050 // version performs better.
1051 HMod* hmod = instr->hydrogen();
1052 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1053 Label dividend_is_not_negative, done;
1055 if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
1056 __ Branch(÷nd_is_not_negative, ge, dividend, Operand(zero_reg));
1057 // Note: The code below even works when right contains kMinInt.
1058 __ dsubu(dividend, zero_reg, dividend);
1059 __ And(dividend, dividend, Operand(mask));
1060 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1061 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, dividend,
1064 __ Branch(USE_DELAY_SLOT, &done);
1065 __ dsubu(dividend, zero_reg, dividend);
1068 __ bind(÷nd_is_not_negative);
1069 __ And(dividend, dividend, Operand(mask));
1074 void LCodeGen::DoModByConstI(LModByConstI* instr) {
1075 Register dividend = ToRegister(instr->dividend());
1076 int32_t divisor = instr->divisor();
1077 Register result = ToRegister(instr->result());
1078 DCHECK(!dividend.is(result));
1081 DeoptimizeIf(al, instr, Deoptimizer::kDivisionByZero);
1085 __ TruncatingDiv(result, dividend, Abs(divisor));
1086 __ Dmul(result, result, Operand(Abs(divisor)));
1087 __ Dsubu(result, dividend, Operand(result));
1089 // Check for negative zero.
1090 HMod* hmod = instr->hydrogen();
1091 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1092 Label remainder_not_zero;
1093 __ Branch(&remainder_not_zero, ne, result, Operand(zero_reg));
1094 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, dividend,
1096 __ bind(&remainder_not_zero);
1101 void LCodeGen::DoModI(LModI* instr) {
1102 HMod* hmod = instr->hydrogen();
1103 const Register left_reg = ToRegister(instr->left());
1104 const Register right_reg = ToRegister(instr->right());
1105 const Register result_reg = ToRegister(instr->result());
1107 // div runs in the background while we check for special cases.
1108 __ Dmod(result_reg, left_reg, right_reg);
1111 // Check for x % 0, we have to deopt in this case because we can't return a
1113 if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
1114 DeoptimizeIf(eq, instr, Deoptimizer::kDivisionByZero, right_reg,
1118 // Check for kMinInt % -1, div will return kMinInt, which is not what we
1119 // want. We have to deopt if we care about -0, because we can't return that.
1120 if (hmod->CheckFlag(HValue::kCanOverflow)) {
1121 Label no_overflow_possible;
1122 __ Branch(&no_overflow_possible, ne, left_reg, Operand(kMinInt));
1123 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1124 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, right_reg, Operand(-1));
1126 __ Branch(&no_overflow_possible, ne, right_reg, Operand(-1));
1127 __ Branch(USE_DELAY_SLOT, &done);
1128 __ mov(result_reg, zero_reg);
1130 __ bind(&no_overflow_possible);
1133 // If we care about -0, test if the dividend is <0 and the result is 0.
1134 __ Branch(&done, ge, left_reg, Operand(zero_reg));
1136 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1137 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, result_reg,
1144 void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
1145 Register dividend = ToRegister(instr->dividend());
1146 int32_t divisor = instr->divisor();
1147 Register result = ToRegister(instr->result());
1148 DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
1149 DCHECK(!result.is(dividend));
1151 // Check for (0 / -x) that will produce negative zero.
1152 HDiv* hdiv = instr->hydrogen();
1153 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1154 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, dividend,
1157 // Check for (kMinInt / -1).
1158 if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
1159 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow, dividend, Operand(kMinInt));
1161 // Deoptimize if remainder will not be 0.
1162 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1163 divisor != 1 && divisor != -1) {
1164 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1165 __ And(at, dividend, Operand(mask));
1166 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision, at, Operand(zero_reg));
1169 if (divisor == -1) { // Nice shortcut, not needed for correctness.
1170 __ Dsubu(result, zero_reg, dividend);
1173 uint16_t shift = WhichPowerOf2Abs(divisor);
1175 __ Move(result, dividend);
1176 } else if (shift == 1) {
1177 __ dsrl32(result, dividend, 31);
1178 __ Daddu(result, dividend, Operand(result));
1180 __ dsra32(result, dividend, 31);
1181 __ dsrl32(result, result, 32 - shift);
1182 __ Daddu(result, dividend, Operand(result));
1184 if (shift > 0) __ dsra(result, result, shift);
1185 if (divisor < 0) __ Dsubu(result, zero_reg, result);
1189 void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
1190 Register dividend = ToRegister(instr->dividend());
1191 int32_t divisor = instr->divisor();
1192 Register result = ToRegister(instr->result());
1193 DCHECK(!dividend.is(result));
1196 DeoptimizeIf(al, instr, Deoptimizer::kDivisionByZero);
1200 // Check for (0 / -x) that will produce negative zero.
1201 HDiv* hdiv = instr->hydrogen();
1202 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1203 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, dividend,
1207 __ TruncatingDiv(result, dividend, Abs(divisor));
1208 if (divisor < 0) __ Subu(result, zero_reg, result);
1210 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1211 __ Dmul(scratch0(), result, Operand(divisor));
1212 __ Dsubu(scratch0(), scratch0(), dividend);
1213 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision, scratch0(),
1219 // TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
1220 void LCodeGen::DoDivI(LDivI* instr) {
1221 HBinaryOperation* hdiv = instr->hydrogen();
1222 Register dividend = ToRegister(instr->dividend());
1223 Register divisor = ToRegister(instr->divisor());
1224 const Register result = ToRegister(instr->result());
1226 // On MIPS div is asynchronous - it will run in the background while we
1227 // check for special cases.
1228 __ Div(result, dividend, divisor);
1231 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1232 DeoptimizeIf(eq, instr, Deoptimizer::kDivisionByZero, divisor,
1236 // Check for (0 / -x) that will produce negative zero.
1237 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1238 Label left_not_zero;
1239 __ Branch(&left_not_zero, ne, dividend, Operand(zero_reg));
1240 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, divisor,
1242 __ bind(&left_not_zero);
1245 // Check for (kMinInt / -1).
1246 if (hdiv->CheckFlag(HValue::kCanOverflow) &&
1247 !hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1248 Label left_not_min_int;
1249 __ Branch(&left_not_min_int, ne, dividend, Operand(kMinInt));
1250 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow, divisor, Operand(-1));
1251 __ bind(&left_not_min_int);
1254 if (!hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1255 // Calculate remainder.
1256 Register remainder = ToRegister(instr->temp());
1257 if (kArchVariant != kMips64r6) {
1260 __ dmod(remainder, dividend, divisor);
1262 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision, remainder,
1268 void LCodeGen::DoMultiplyAddD(LMultiplyAddD* instr) {
1269 DoubleRegister addend = ToDoubleRegister(instr->addend());
1270 DoubleRegister multiplier = ToDoubleRegister(instr->multiplier());
1271 DoubleRegister multiplicand = ToDoubleRegister(instr->multiplicand());
1273 // This is computed in-place.
1274 DCHECK(addend.is(ToDoubleRegister(instr->result())));
1276 __ Madd_d(addend, addend, multiplier, multiplicand, double_scratch0());
1280 void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
1281 Register dividend = ToRegister(instr->dividend());
1282 Register result = ToRegister(instr->result());
1283 int32_t divisor = instr->divisor();
1284 Register scratch = result.is(dividend) ? scratch0() : dividend;
1285 DCHECK(!result.is(dividend) || !scratch.is(dividend));
1287 // If the divisor is 1, return the dividend.
1289 __ Move(result, dividend);
1293 // If the divisor is positive, things are easy: There can be no deopts and we
1294 // can simply do an arithmetic right shift.
1295 uint16_t shift = WhichPowerOf2Abs(divisor);
1297 __ dsra(result, dividend, shift);
1301 // If the divisor is negative, we have to negate and handle edge cases.
1302 // Dividend can be the same register as result so save the value of it
1303 // for checking overflow.
1304 __ Move(scratch, dividend);
1306 __ Dsubu(result, zero_reg, dividend);
1307 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1308 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, result, Operand(zero_reg));
1311 __ Xor(scratch, scratch, result);
1312 // Dividing by -1 is basically negation, unless we overflow.
1313 if (divisor == -1) {
1314 if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1315 DeoptimizeIf(gt, instr, Deoptimizer::kOverflow, result, Operand(kMaxInt));
1320 // If the negation could not overflow, simply shifting is OK.
1321 if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1322 __ dsra(result, result, shift);
1326 Label no_overflow, done;
1327 __ Branch(&no_overflow, lt, scratch, Operand(zero_reg));
1328 __ li(result, Operand(kMinInt / divisor), CONSTANT_SIZE);
1330 __ bind(&no_overflow);
1331 __ dsra(result, result, shift);
1336 void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
1337 Register dividend = ToRegister(instr->dividend());
1338 int32_t divisor = instr->divisor();
1339 Register result = ToRegister(instr->result());
1340 DCHECK(!dividend.is(result));
1343 DeoptimizeIf(al, instr, Deoptimizer::kDivisionByZero);
1347 // Check for (0 / -x) that will produce negative zero.
1348 HMathFloorOfDiv* hdiv = instr->hydrogen();
1349 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1350 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, dividend,
1354 // Easy case: We need no dynamic check for the dividend and the flooring
1355 // division is the same as the truncating division.
1356 if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
1357 (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
1358 __ TruncatingDiv(result, dividend, Abs(divisor));
1359 if (divisor < 0) __ Dsubu(result, zero_reg, result);
1363 // In the general case we may need to adjust before and after the truncating
1364 // division to get a flooring division.
1365 Register temp = ToRegister(instr->temp());
1366 DCHECK(!temp.is(dividend) && !temp.is(result));
1367 Label needs_adjustment, done;
1368 __ Branch(&needs_adjustment, divisor > 0 ? lt : gt,
1369 dividend, Operand(zero_reg));
1370 __ TruncatingDiv(result, dividend, Abs(divisor));
1371 if (divisor < 0) __ Dsubu(result, zero_reg, result);
1373 __ bind(&needs_adjustment);
1374 __ Daddu(temp, dividend, Operand(divisor > 0 ? 1 : -1));
1375 __ TruncatingDiv(result, temp, Abs(divisor));
1376 if (divisor < 0) __ Dsubu(result, zero_reg, result);
1377 __ Dsubu(result, result, Operand(1));
1382 // TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
1383 void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
1384 HBinaryOperation* hdiv = instr->hydrogen();
1385 Register dividend = ToRegister(instr->dividend());
1386 Register divisor = ToRegister(instr->divisor());
1387 const Register result = ToRegister(instr->result());
1389 // On MIPS div is asynchronous - it will run in the background while we
1390 // check for special cases.
1391 __ Ddiv(result, dividend, divisor);
1394 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1395 DeoptimizeIf(eq, instr, Deoptimizer::kDivisionByZero, divisor,
1399 // Check for (0 / -x) that will produce negative zero.
1400 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1401 Label left_not_zero;
1402 __ Branch(&left_not_zero, ne, dividend, Operand(zero_reg));
1403 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, divisor,
1405 __ bind(&left_not_zero);
1408 // Check for (kMinInt / -1).
1409 if (hdiv->CheckFlag(HValue::kCanOverflow) &&
1410 !hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1411 Label left_not_min_int;
1412 __ Branch(&left_not_min_int, ne, dividend, Operand(kMinInt));
1413 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow, divisor, Operand(-1));
1414 __ bind(&left_not_min_int);
1417 // We performed a truncating division. Correct the result if necessary.
1419 Register remainder = scratch0();
1420 if (kArchVariant != kMips64r6) {
1423 __ dmod(remainder, dividend, divisor);
1425 __ Branch(&done, eq, remainder, Operand(zero_reg), USE_DELAY_SLOT);
1426 __ Xor(remainder, remainder, Operand(divisor));
1427 __ Branch(&done, ge, remainder, Operand(zero_reg));
1428 __ Dsubu(result, result, Operand(1));
1433 void LCodeGen::DoMulS(LMulS* instr) {
1434 Register scratch = scratch0();
1435 Register result = ToRegister(instr->result());
1436 // Note that result may alias left.
1437 Register left = ToRegister(instr->left());
1438 LOperand* right_op = instr->right();
1440 bool bailout_on_minus_zero =
1441 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
1442 bool overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1444 if (right_op->IsConstantOperand()) {
1445 int32_t constant = ToInteger32(LConstantOperand::cast(right_op));
1447 if (bailout_on_minus_zero && (constant < 0)) {
1448 // The case of a null constant will be handled separately.
1449 // If constant is negative and left is null, the result should be -0.
1450 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, left, Operand(zero_reg));
1456 __ DsubuAndCheckForOverflow(result, zero_reg, left, scratch);
1457 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, scratch,
1460 __ Dsubu(result, zero_reg, left);
1464 if (bailout_on_minus_zero) {
1465 // If left is strictly negative and the constant is null, the
1466 // result is -0. Deoptimize if required, otherwise return 0.
1467 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, left,
1470 __ mov(result, zero_reg);
1474 __ Move(result, left);
1477 // Multiplying by powers of two and powers of two plus or minus
1478 // one can be done faster with shifted operands.
1479 // For other constants we emit standard code.
1480 int32_t mask = constant >> 31;
1481 uint32_t constant_abs = (constant + mask) ^ mask;
1483 if (base::bits::IsPowerOfTwo32(constant_abs)) {
1484 int32_t shift = WhichPowerOf2(constant_abs);
1485 __ dsll(result, left, shift);
1486 // Correct the sign of the result if the constant is negative.
1487 if (constant < 0) __ Dsubu(result, zero_reg, result);
1488 } else if (base::bits::IsPowerOfTwo32(constant_abs - 1)) {
1489 int32_t shift = WhichPowerOf2(constant_abs - 1);
1490 __ dsll(scratch, left, shift);
1491 __ Daddu(result, scratch, left);
1492 // Correct the sign of the result if the constant is negative.
1493 if (constant < 0) __ Dsubu(result, zero_reg, result);
1494 } else if (base::bits::IsPowerOfTwo32(constant_abs + 1)) {
1495 int32_t shift = WhichPowerOf2(constant_abs + 1);
1496 __ dsll(scratch, left, shift);
1497 __ Dsubu(result, scratch, left);
1498 // Correct the sign of the result if the constant is negative.
1499 if (constant < 0) __ Dsubu(result, zero_reg, result);
1501 // Generate standard code.
1502 __ li(at, constant);
1503 __ Dmul(result, left, at);
1507 DCHECK(right_op->IsRegister());
1508 Register right = ToRegister(right_op);
1511 // hi:lo = left * right.
1512 __ Dmulh(result, left, right);
1513 __ dsra32(scratch, result, 0);
1514 __ sra(at, result, 31);
1516 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow, scratch, Operand(at));
1518 __ SmiUntag(result, left);
1519 __ dmul(result, result, right);
1522 if (bailout_on_minus_zero) {
1524 __ Xor(at, left, right);
1525 __ Branch(&done, ge, at, Operand(zero_reg));
1526 // Bail out if the result is minus zero.
1527 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, result,
1535 void LCodeGen::DoMulI(LMulI* instr) {
1536 Register scratch = scratch0();
1537 Register result = ToRegister(instr->result());
1538 // Note that result may alias left.
1539 Register left = ToRegister(instr->left());
1540 LOperand* right_op = instr->right();
1542 bool bailout_on_minus_zero =
1543 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
1544 bool overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1546 if (right_op->IsConstantOperand()) {
1547 int32_t constant = ToInteger32(LConstantOperand::cast(right_op));
1549 if (bailout_on_minus_zero && (constant < 0)) {
1550 // The case of a null constant will be handled separately.
1551 // If constant is negative and left is null, the result should be -0.
1552 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, left, Operand(zero_reg));
1558 __ SubuAndCheckForOverflow(result, zero_reg, left, scratch);
1559 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, scratch,
1562 __ Subu(result, zero_reg, left);
1566 if (bailout_on_minus_zero) {
1567 // If left is strictly negative and the constant is null, the
1568 // result is -0. Deoptimize if required, otherwise return 0.
1569 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, left,
1572 __ mov(result, zero_reg);
1576 __ Move(result, left);
1579 // Multiplying by powers of two and powers of two plus or minus
1580 // one can be done faster with shifted operands.
1581 // For other constants we emit standard code.
1582 int32_t mask = constant >> 31;
1583 uint32_t constant_abs = (constant + mask) ^ mask;
1585 if (base::bits::IsPowerOfTwo32(constant_abs)) {
1586 int32_t shift = WhichPowerOf2(constant_abs);
1587 __ sll(result, left, shift);
1588 // Correct the sign of the result if the constant is negative.
1589 if (constant < 0) __ Subu(result, zero_reg, result);
1590 } else if (base::bits::IsPowerOfTwo32(constant_abs - 1)) {
1591 int32_t shift = WhichPowerOf2(constant_abs - 1);
1592 __ sll(scratch, left, shift);
1593 __ addu(result, scratch, left);
1594 // Correct the sign of the result if the constant is negative.
1595 if (constant < 0) __ Subu(result, zero_reg, result);
1596 } else if (base::bits::IsPowerOfTwo32(constant_abs + 1)) {
1597 int32_t shift = WhichPowerOf2(constant_abs + 1);
1598 __ sll(scratch, left, shift);
1599 __ Subu(result, scratch, left);
1600 // Correct the sign of the result if the constant is negative.
1601 if (constant < 0) __ Subu(result, zero_reg, result);
1603 // Generate standard code.
1604 __ li(at, constant);
1605 __ Mul(result, left, at);
1610 DCHECK(right_op->IsRegister());
1611 Register right = ToRegister(right_op);
1614 // hi:lo = left * right.
1615 __ Dmul(result, left, right);
1616 __ dsra32(scratch, result, 0);
1617 __ sra(at, result, 31);
1619 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow, scratch, Operand(at));
1621 __ mul(result, left, right);
1624 if (bailout_on_minus_zero) {
1626 __ Xor(at, left, right);
1627 __ Branch(&done, ge, at, Operand(zero_reg));
1628 // Bail out if the result is minus zero.
1629 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, result,
1637 void LCodeGen::DoBitI(LBitI* instr) {
1638 LOperand* left_op = instr->left();
1639 LOperand* right_op = instr->right();
1640 DCHECK(left_op->IsRegister());
1641 Register left = ToRegister(left_op);
1642 Register result = ToRegister(instr->result());
1643 Operand right(no_reg);
1645 if (right_op->IsStackSlot()) {
1646 right = Operand(EmitLoadRegister(right_op, at));
1648 DCHECK(right_op->IsRegister() || right_op->IsConstantOperand());
1649 right = ToOperand(right_op);
1652 switch (instr->op()) {
1653 case Token::BIT_AND:
1654 __ And(result, left, right);
1657 __ Or(result, left, right);
1659 case Token::BIT_XOR:
1660 if (right_op->IsConstantOperand() && right.immediate() == int32_t(~0)) {
1661 __ Nor(result, zero_reg, left);
1663 __ Xor(result, left, right);
1673 void LCodeGen::DoShiftI(LShiftI* instr) {
1674 // Both 'left' and 'right' are "used at start" (see LCodeGen::DoShift), so
1675 // result may alias either of them.
1676 LOperand* right_op = instr->right();
1677 Register left = ToRegister(instr->left());
1678 Register result = ToRegister(instr->result());
1680 if (right_op->IsRegister()) {
1681 // No need to mask the right operand on MIPS, it is built into the variable
1682 // shift instructions.
1683 switch (instr->op()) {
1685 __ Ror(result, left, Operand(ToRegister(right_op)));
1688 __ srav(result, left, ToRegister(right_op));
1691 __ srlv(result, left, ToRegister(right_op));
1692 if (instr->can_deopt()) {
1693 // TODO(yy): (-1) >>> 0. anything else?
1694 DeoptimizeIf(lt, instr, Deoptimizer::kNegativeValue, result,
1696 DeoptimizeIf(gt, instr, Deoptimizer::kNegativeValue, result,
1701 __ sllv(result, left, ToRegister(right_op));
1708 // Mask the right_op operand.
1709 int value = ToInteger32(LConstantOperand::cast(right_op));
1710 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1711 switch (instr->op()) {
1713 if (shift_count != 0) {
1714 __ Ror(result, left, Operand(shift_count));
1716 __ Move(result, left);
1720 if (shift_count != 0) {
1721 __ sra(result, left, shift_count);
1723 __ Move(result, left);
1727 if (shift_count != 0) {
1728 __ srl(result, left, shift_count);
1730 if (instr->can_deopt()) {
1731 __ And(at, left, Operand(0x80000000));
1732 DeoptimizeIf(ne, instr, Deoptimizer::kNegativeValue, at,
1735 __ Move(result, left);
1739 if (shift_count != 0) {
1740 if (instr->hydrogen_value()->representation().IsSmi()) {
1741 __ dsll(result, left, shift_count);
1743 __ sll(result, left, shift_count);
1746 __ Move(result, left);
1757 void LCodeGen::DoSubS(LSubS* instr) {
1758 LOperand* left = instr->left();
1759 LOperand* right = instr->right();
1760 LOperand* result = instr->result();
1761 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1763 if (!can_overflow) {
1764 DCHECK(right->IsRegister() || right->IsConstantOperand());
1765 __ Dsubu(ToRegister(result), ToRegister(left), ToOperand(right));
1766 } else { // can_overflow.
1767 Register overflow = scratch0();
1768 Register scratch = scratch1();
1769 DCHECK(right->IsRegister() || right->IsConstantOperand());
1770 __ DsubuAndCheckForOverflow(ToRegister(result), ToRegister(left),
1771 ToOperand(right), overflow, scratch);
1772 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, overflow,
1778 void LCodeGen::DoSubI(LSubI* instr) {
1779 LOperand* left = instr->left();
1780 LOperand* right = instr->right();
1781 LOperand* result = instr->result();
1782 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1784 if (!can_overflow) {
1785 DCHECK(right->IsRegister() || right->IsConstantOperand());
1786 __ Subu(ToRegister(result), ToRegister(left), ToOperand(right));
1787 } else { // can_overflow.
1788 Register overflow = scratch0();
1789 Register scratch = scratch1();
1790 DCHECK(right->IsRegister() || right->IsConstantOperand());
1791 __ SubuAndCheckForOverflow(ToRegister(result), ToRegister(left),
1792 ToOperand(right), overflow, scratch);
1793 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, overflow,
1799 void LCodeGen::DoConstantI(LConstantI* instr) {
1800 __ li(ToRegister(instr->result()), Operand(instr->value()));
1804 void LCodeGen::DoConstantS(LConstantS* instr) {
1805 __ li(ToRegister(instr->result()), Operand(instr->value()));
1809 void LCodeGen::DoConstantD(LConstantD* instr) {
1810 DCHECK(instr->result()->IsDoubleRegister());
1811 DoubleRegister result = ToDoubleRegister(instr->result());
1812 double v = instr->value();
1817 void LCodeGen::DoConstantE(LConstantE* instr) {
1818 __ li(ToRegister(instr->result()), Operand(instr->value()));
1822 void LCodeGen::DoConstantT(LConstantT* instr) {
1823 Handle<Object> object = instr->value(isolate());
1824 AllowDeferredHandleDereference smi_check;
1825 __ li(ToRegister(instr->result()), object);
1829 void LCodeGen::DoMapEnumLength(LMapEnumLength* instr) {
1830 Register result = ToRegister(instr->result());
1831 Register map = ToRegister(instr->value());
1832 __ EnumLength(result, map);
1836 void LCodeGen::DoDateField(LDateField* instr) {
1837 Register object = ToRegister(instr->date());
1838 Register result = ToRegister(instr->result());
1839 Register scratch = ToRegister(instr->temp());
1840 Smi* index = instr->index();
1841 DCHECK(object.is(a0));
1842 DCHECK(result.is(v0));
1843 DCHECK(!scratch.is(scratch0()));
1844 DCHECK(!scratch.is(object));
1846 if (index->value() == 0) {
1847 __ ld(result, FieldMemOperand(object, JSDate::kValueOffset));
1849 Label runtime, done;
1850 if (index->value() < JSDate::kFirstUncachedField) {
1851 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
1852 __ li(scratch, Operand(stamp));
1853 __ ld(scratch, MemOperand(scratch));
1854 __ ld(scratch0(), FieldMemOperand(object, JSDate::kCacheStampOffset));
1855 __ Branch(&runtime, ne, scratch, Operand(scratch0()));
1856 __ ld(result, FieldMemOperand(object, JSDate::kValueOffset +
1857 kPointerSize * index->value()));
1861 __ PrepareCallCFunction(2, scratch);
1862 __ li(a1, Operand(index));
1863 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
1869 MemOperand LCodeGen::BuildSeqStringOperand(Register string,
1871 String::Encoding encoding) {
1872 if (index->IsConstantOperand()) {
1873 int offset = ToInteger32(LConstantOperand::cast(index));
1874 if (encoding == String::TWO_BYTE_ENCODING) {
1875 offset *= kUC16Size;
1877 STATIC_ASSERT(kCharSize == 1);
1878 return FieldMemOperand(string, SeqString::kHeaderSize + offset);
1880 Register scratch = scratch0();
1881 DCHECK(!scratch.is(string));
1882 DCHECK(!scratch.is(ToRegister(index)));
1883 if (encoding == String::ONE_BYTE_ENCODING) {
1884 __ Daddu(scratch, string, ToRegister(index));
1886 STATIC_ASSERT(kUC16Size == 2);
1887 __ dsll(scratch, ToRegister(index), 1);
1888 __ Daddu(scratch, string, scratch);
1890 return FieldMemOperand(scratch, SeqString::kHeaderSize);
1894 void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
1895 String::Encoding encoding = instr->hydrogen()->encoding();
1896 Register string = ToRegister(instr->string());
1897 Register result = ToRegister(instr->result());
1899 if (FLAG_debug_code) {
1900 Register scratch = scratch0();
1901 __ ld(scratch, FieldMemOperand(string, HeapObject::kMapOffset));
1902 __ lbu(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1904 __ And(scratch, scratch,
1905 Operand(kStringRepresentationMask | kStringEncodingMask));
1906 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1907 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1908 __ Dsubu(at, scratch, Operand(encoding == String::ONE_BYTE_ENCODING
1909 ? one_byte_seq_type : two_byte_seq_type));
1910 __ Check(eq, kUnexpectedStringType, at, Operand(zero_reg));
1913 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1914 if (encoding == String::ONE_BYTE_ENCODING) {
1915 __ lbu(result, operand);
1917 __ lhu(result, operand);
1922 void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
1923 String::Encoding encoding = instr->hydrogen()->encoding();
1924 Register string = ToRegister(instr->string());
1925 Register value = ToRegister(instr->value());
1927 if (FLAG_debug_code) {
1928 Register scratch = scratch0();
1929 Register index = ToRegister(instr->index());
1930 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1931 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1933 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
1934 ? one_byte_seq_type : two_byte_seq_type;
1935 __ EmitSeqStringSetCharCheck(string, index, value, scratch, encoding_mask);
1938 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1939 if (encoding == String::ONE_BYTE_ENCODING) {
1940 __ sb(value, operand);
1942 __ sh(value, operand);
1947 void LCodeGen::DoAddE(LAddE* instr) {
1948 LOperand* result = instr->result();
1949 LOperand* left = instr->left();
1950 LOperand* right = instr->right();
1952 DCHECK(!instr->hydrogen()->CheckFlag(HValue::kCanOverflow));
1953 DCHECK(right->IsRegister() || right->IsConstantOperand());
1954 __ Daddu(ToRegister(result), ToRegister(left), ToOperand(right));
1958 void LCodeGen::DoAddS(LAddS* instr) {
1959 LOperand* left = instr->left();
1960 LOperand* right = instr->right();
1961 LOperand* result = instr->result();
1962 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1964 if (!can_overflow) {
1965 DCHECK(right->IsRegister() || right->IsConstantOperand());
1966 __ Daddu(ToRegister(result), ToRegister(left), ToOperand(right));
1967 } else { // can_overflow.
1968 Register overflow = scratch0();
1969 Register scratch = scratch1();
1970 DCHECK(right->IsRegister() || right->IsConstantOperand());
1971 __ DadduAndCheckForOverflow(ToRegister(result), ToRegister(left),
1972 ToOperand(right), overflow, scratch);
1973 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, overflow,
1979 void LCodeGen::DoAddI(LAddI* instr) {
1980 LOperand* left = instr->left();
1981 LOperand* right = instr->right();
1982 LOperand* result = instr->result();
1983 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1985 if (!can_overflow) {
1986 DCHECK(right->IsRegister() || right->IsConstantOperand());
1987 __ Addu(ToRegister(result), ToRegister(left), ToOperand(right));
1988 } else { // can_overflow.
1989 Register overflow = scratch0();
1990 Register scratch = scratch1();
1991 DCHECK(right->IsRegister() || right->IsConstantOperand());
1992 __ AdduAndCheckForOverflow(ToRegister(result), ToRegister(left),
1993 ToOperand(right), overflow, scratch);
1994 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, overflow,
2000 void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
2001 LOperand* left = instr->left();
2002 LOperand* right = instr->right();
2003 HMathMinMax::Operation operation = instr->hydrogen()->operation();
2004 Condition condition = (operation == HMathMinMax::kMathMin) ? le : ge;
2005 if (instr->hydrogen()->representation().IsSmiOrInteger32()) {
2006 Register left_reg = ToRegister(left);
2007 Register right_reg = EmitLoadRegister(right, scratch0());
2008 Register result_reg = ToRegister(instr->result());
2009 Label return_right, done;
2010 Register scratch = scratch1();
2011 __ Slt(scratch, left_reg, Operand(right_reg));
2012 if (condition == ge) {
2013 __ Movz(result_reg, left_reg, scratch);
2014 __ Movn(result_reg, right_reg, scratch);
2016 DCHECK(condition == le);
2017 __ Movn(result_reg, left_reg, scratch);
2018 __ Movz(result_reg, right_reg, scratch);
2021 DCHECK(instr->hydrogen()->representation().IsDouble());
2022 FPURegister left_reg = ToDoubleRegister(left);
2023 FPURegister right_reg = ToDoubleRegister(right);
2024 FPURegister result_reg = ToDoubleRegister(instr->result());
2025 Label check_nan_left, check_zero, return_left, return_right, done;
2026 __ BranchF(&check_zero, &check_nan_left, eq, left_reg, right_reg);
2027 __ BranchF(&return_left, NULL, condition, left_reg, right_reg);
2028 __ Branch(&return_right);
2030 __ bind(&check_zero);
2031 // left == right != 0.
2032 __ BranchF(&return_left, NULL, ne, left_reg, kDoubleRegZero);
2033 // At this point, both left and right are either 0 or -0.
2034 if (operation == HMathMinMax::kMathMin) {
2035 __ neg_d(left_reg, left_reg);
2036 __ sub_d(result_reg, left_reg, right_reg);
2037 __ neg_d(result_reg, result_reg);
2039 __ add_d(result_reg, left_reg, right_reg);
2043 __ bind(&check_nan_left);
2045 __ BranchF(NULL, &return_left, eq, left_reg, left_reg);
2046 __ bind(&return_right);
2047 if (!right_reg.is(result_reg)) {
2048 __ mov_d(result_reg, right_reg);
2052 __ bind(&return_left);
2053 if (!left_reg.is(result_reg)) {
2054 __ mov_d(result_reg, left_reg);
2061 void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
2062 DoubleRegister left = ToDoubleRegister(instr->left());
2063 DoubleRegister right = ToDoubleRegister(instr->right());
2064 DoubleRegister result = ToDoubleRegister(instr->result());
2065 switch (instr->op()) {
2067 __ add_d(result, left, right);
2070 __ sub_d(result, left, right);
2073 __ mul_d(result, left, right);
2076 __ div_d(result, left, right);
2079 // Save a0-a3 on the stack.
2080 RegList saved_regs = a0.bit() | a1.bit() | a2.bit() | a3.bit();
2081 __ MultiPush(saved_regs);
2083 __ PrepareCallCFunction(0, 2, scratch0());
2084 __ MovToFloatParameters(left, right);
2086 ExternalReference::mod_two_doubles_operation(isolate()),
2088 // Move the result in the double result register.
2089 __ MovFromFloatResult(result);
2091 // Restore saved register.
2092 __ MultiPop(saved_regs);
2102 void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
2103 DCHECK(ToRegister(instr->context()).is(cp));
2104 DCHECK(ToRegister(instr->left()).is(a1));
2105 DCHECK(ToRegister(instr->right()).is(a0));
2106 DCHECK(ToRegister(instr->result()).is(v0));
2109 CodeFactory::BinaryOpIC(isolate(), instr->op(), instr->strength()).code();
2110 CallCode(code, RelocInfo::CODE_TARGET, instr);
2111 // Other arch use a nop here, to signal that there is no inlined
2112 // patchable code. Mips does not need the nop, since our marker
2113 // instruction (andi zero_reg) will never be used in normal code.
2117 template<class InstrType>
2118 void LCodeGen::EmitBranch(InstrType instr,
2119 Condition condition,
2121 const Operand& src2) {
2122 int left_block = instr->TrueDestination(chunk_);
2123 int right_block = instr->FalseDestination(chunk_);
2125 int next_block = GetNextEmittedBlock();
2126 if (right_block == left_block || condition == al) {
2127 EmitGoto(left_block);
2128 } else if (left_block == next_block) {
2129 __ Branch(chunk_->GetAssemblyLabel(right_block),
2130 NegateCondition(condition), src1, src2);
2131 } else if (right_block == next_block) {
2132 __ Branch(chunk_->GetAssemblyLabel(left_block), condition, src1, src2);
2134 __ Branch(chunk_->GetAssemblyLabel(left_block), condition, src1, src2);
2135 __ Branch(chunk_->GetAssemblyLabel(right_block));
2140 template<class InstrType>
2141 void LCodeGen::EmitBranchF(InstrType instr,
2142 Condition condition,
2145 int right_block = instr->FalseDestination(chunk_);
2146 int left_block = instr->TrueDestination(chunk_);
2148 int next_block = GetNextEmittedBlock();
2149 if (right_block == left_block) {
2150 EmitGoto(left_block);
2151 } else if (left_block == next_block) {
2152 __ BranchF(chunk_->GetAssemblyLabel(right_block), NULL,
2153 NegateFpuCondition(condition), src1, src2);
2154 } else if (right_block == next_block) {
2155 __ BranchF(chunk_->GetAssemblyLabel(left_block), NULL,
2156 condition, src1, src2);
2158 __ BranchF(chunk_->GetAssemblyLabel(left_block), NULL,
2159 condition, src1, src2);
2160 __ Branch(chunk_->GetAssemblyLabel(right_block));
2165 template <class InstrType>
2166 void LCodeGen::EmitTrueBranch(InstrType instr, Condition condition,
2167 Register src1, const Operand& src2) {
2168 int true_block = instr->TrueDestination(chunk_);
2169 __ Branch(chunk_->GetAssemblyLabel(true_block), condition, src1, src2);
2173 template <class InstrType>
2174 void LCodeGen::EmitFalseBranch(InstrType instr, Condition condition,
2175 Register src1, const Operand& src2) {
2176 int false_block = instr->FalseDestination(chunk_);
2177 __ Branch(chunk_->GetAssemblyLabel(false_block), condition, src1, src2);
2181 template<class InstrType>
2182 void LCodeGen::EmitFalseBranchF(InstrType instr,
2183 Condition condition,
2186 int false_block = instr->FalseDestination(chunk_);
2187 __ BranchF(chunk_->GetAssemblyLabel(false_block), NULL,
2188 condition, src1, src2);
2192 void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
2193 __ stop("LDebugBreak");
2197 void LCodeGen::DoBranch(LBranch* instr) {
2198 Representation r = instr->hydrogen()->value()->representation();
2199 if (r.IsInteger32() || r.IsSmi()) {
2200 DCHECK(!info()->IsStub());
2201 Register reg = ToRegister(instr->value());
2202 EmitBranch(instr, ne, reg, Operand(zero_reg));
2203 } else if (r.IsDouble()) {
2204 DCHECK(!info()->IsStub());
2205 DoubleRegister reg = ToDoubleRegister(instr->value());
2206 // Test the double value. Zero and NaN are false.
2207 EmitBranchF(instr, ogl, reg, kDoubleRegZero);
2209 DCHECK(r.IsTagged());
2210 Register reg = ToRegister(instr->value());
2211 HType type = instr->hydrogen()->value()->type();
2212 if (type.IsBoolean()) {
2213 DCHECK(!info()->IsStub());
2214 __ LoadRoot(at, Heap::kTrueValueRootIndex);
2215 EmitBranch(instr, eq, reg, Operand(at));
2216 } else if (type.IsSmi()) {
2217 DCHECK(!info()->IsStub());
2218 EmitBranch(instr, ne, reg, Operand(zero_reg));
2219 } else if (type.IsJSArray()) {
2220 DCHECK(!info()->IsStub());
2221 EmitBranch(instr, al, zero_reg, Operand(zero_reg));
2222 } else if (type.IsHeapNumber()) {
2223 DCHECK(!info()->IsStub());
2224 DoubleRegister dbl_scratch = double_scratch0();
2225 __ ldc1(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
2226 // Test the double value. Zero and NaN are false.
2227 EmitBranchF(instr, ogl, dbl_scratch, kDoubleRegZero);
2228 } else if (type.IsString()) {
2229 DCHECK(!info()->IsStub());
2230 __ ld(at, FieldMemOperand(reg, String::kLengthOffset));
2231 EmitBranch(instr, ne, at, Operand(zero_reg));
2233 ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
2234 // Avoid deopts in the case where we've never executed this path before.
2235 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
2237 if (expected.Contains(ToBooleanStub::UNDEFINED)) {
2238 // undefined -> false.
2239 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
2240 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
2242 if (expected.Contains(ToBooleanStub::BOOLEAN)) {
2243 // Boolean -> its value.
2244 __ LoadRoot(at, Heap::kTrueValueRootIndex);
2245 __ Branch(instr->TrueLabel(chunk_), eq, reg, Operand(at));
2246 __ LoadRoot(at, Heap::kFalseValueRootIndex);
2247 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
2249 if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
2251 __ LoadRoot(at, Heap::kNullValueRootIndex);
2252 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
2255 if (expected.Contains(ToBooleanStub::SMI)) {
2256 // Smis: 0 -> false, all other -> true.
2257 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(zero_reg));
2258 __ JumpIfSmi(reg, instr->TrueLabel(chunk_));
2259 } else if (expected.NeedsMap()) {
2260 // If we need a map later and have a Smi -> deopt.
2262 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
2265 const Register map = scratch0();
2266 if (expected.NeedsMap()) {
2267 __ ld(map, FieldMemOperand(reg, HeapObject::kMapOffset));
2268 if (expected.CanBeUndetectable()) {
2269 // Undetectable -> false.
2270 __ lbu(at, FieldMemOperand(map, Map::kBitFieldOffset));
2271 __ And(at, at, Operand(1 << Map::kIsUndetectable));
2272 __ Branch(instr->FalseLabel(chunk_), ne, at, Operand(zero_reg));
2276 if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
2277 // spec object -> true.
2278 __ lbu(at, FieldMemOperand(map, Map::kInstanceTypeOffset));
2279 __ Branch(instr->TrueLabel(chunk_),
2280 ge, at, Operand(FIRST_SPEC_OBJECT_TYPE));
2283 if (expected.Contains(ToBooleanStub::STRING)) {
2284 // String value -> false iff empty.
2286 __ lbu(at, FieldMemOperand(map, Map::kInstanceTypeOffset));
2287 __ Branch(¬_string, ge , at, Operand(FIRST_NONSTRING_TYPE));
2288 __ ld(at, FieldMemOperand(reg, String::kLengthOffset));
2289 __ Branch(instr->TrueLabel(chunk_), ne, at, Operand(zero_reg));
2290 __ Branch(instr->FalseLabel(chunk_));
2291 __ bind(¬_string);
2294 if (expected.Contains(ToBooleanStub::SYMBOL)) {
2295 // Symbol value -> true.
2296 const Register scratch = scratch1();
2297 __ lbu(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
2298 __ Branch(instr->TrueLabel(chunk_), eq, scratch, Operand(SYMBOL_TYPE));
2301 if (expected.Contains(ToBooleanStub::SIMD_VALUE)) {
2302 // SIMD value -> true.
2303 const Register scratch = scratch1();
2304 __ lbu(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
2305 __ Branch(instr->TrueLabel(chunk_), eq, scratch,
2306 Operand(SIMD128_VALUE_TYPE));
2309 if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
2310 // heap number -> false iff +0, -0, or NaN.
2311 DoubleRegister dbl_scratch = double_scratch0();
2312 Label not_heap_number;
2313 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
2314 __ Branch(¬_heap_number, ne, map, Operand(at));
2315 __ ldc1(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
2316 __ BranchF(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2317 ne, dbl_scratch, kDoubleRegZero);
2318 // Falls through if dbl_scratch == 0.
2319 __ Branch(instr->FalseLabel(chunk_));
2320 __ bind(¬_heap_number);
2323 if (!expected.IsGeneric()) {
2324 // We've seen something for the first time -> deopt.
2325 // This can only happen if we are not generic already.
2326 DeoptimizeIf(al, instr, Deoptimizer::kUnexpectedObject, zero_reg,
2334 void LCodeGen::EmitGoto(int block) {
2335 if (!IsNextEmittedBlock(block)) {
2336 __ jmp(chunk_->GetAssemblyLabel(LookupDestination(block)));
2341 void LCodeGen::DoGoto(LGoto* instr) {
2342 EmitGoto(instr->block_id());
2346 Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
2347 Condition cond = kNoCondition;
2350 case Token::EQ_STRICT:
2354 case Token::NE_STRICT:
2358 cond = is_unsigned ? lo : lt;
2361 cond = is_unsigned ? hi : gt;
2364 cond = is_unsigned ? ls : le;
2367 cond = is_unsigned ? hs : ge;
2370 case Token::INSTANCEOF:
2378 void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2379 LOperand* left = instr->left();
2380 LOperand* right = instr->right();
2382 instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2383 instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2384 Condition cond = TokenToCondition(instr->op(), is_unsigned);
2386 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2387 // We can statically evaluate the comparison.
2388 double left_val = ToDouble(LConstantOperand::cast(left));
2389 double right_val = ToDouble(LConstantOperand::cast(right));
2390 int next_block = EvalComparison(instr->op(), left_val, right_val) ?
2391 instr->TrueDestination(chunk_) : instr->FalseDestination(chunk_);
2392 EmitGoto(next_block);
2394 if (instr->is_double()) {
2395 // Compare left and right as doubles and load the
2396 // resulting flags into the normal status register.
2397 FPURegister left_reg = ToDoubleRegister(left);
2398 FPURegister right_reg = ToDoubleRegister(right);
2400 // If a NaN is involved, i.e. the result is unordered,
2401 // jump to false block label.
2402 __ BranchF(NULL, instr->FalseLabel(chunk_), eq,
2403 left_reg, right_reg);
2405 EmitBranchF(instr, cond, left_reg, right_reg);
2408 Operand cmp_right = Operand((int64_t)0);
2409 if (right->IsConstantOperand()) {
2410 int32_t value = ToInteger32(LConstantOperand::cast(right));
2411 if (instr->hydrogen_value()->representation().IsSmi()) {
2412 cmp_left = ToRegister(left);
2413 cmp_right = Operand(Smi::FromInt(value));
2415 cmp_left = ToRegister(left);
2416 cmp_right = Operand(value);
2418 } else if (left->IsConstantOperand()) {
2419 int32_t value = ToInteger32(LConstantOperand::cast(left));
2420 if (instr->hydrogen_value()->representation().IsSmi()) {
2421 cmp_left = ToRegister(right);
2422 cmp_right = Operand(Smi::FromInt(value));
2424 cmp_left = ToRegister(right);
2425 cmp_right = Operand(value);
2427 // We commuted the operands, so commute the condition.
2428 cond = CommuteCondition(cond);
2430 cmp_left = ToRegister(left);
2431 cmp_right = Operand(ToRegister(right));
2434 EmitBranch(instr, cond, cmp_left, cmp_right);
2440 void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
2441 Register left = ToRegister(instr->left());
2442 Register right = ToRegister(instr->right());
2444 EmitBranch(instr, eq, left, Operand(right));
2448 void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) {
2449 if (instr->hydrogen()->representation().IsTagged()) {
2450 Register input_reg = ToRegister(instr->object());
2451 __ li(at, Operand(factory()->the_hole_value()));
2452 EmitBranch(instr, eq, input_reg, Operand(at));
2456 DoubleRegister input_reg = ToDoubleRegister(instr->object());
2457 EmitFalseBranchF(instr, eq, input_reg, input_reg);
2459 Register scratch = scratch0();
2460 __ FmoveHigh(scratch, input_reg);
2461 EmitBranch(instr, eq, scratch,
2462 Operand(static_cast<int32_t>(kHoleNanUpper32)));
2466 void LCodeGen::DoCompareMinusZeroAndBranch(LCompareMinusZeroAndBranch* instr) {
2467 Representation rep = instr->hydrogen()->value()->representation();
2468 DCHECK(!rep.IsInteger32());
2469 Register scratch = ToRegister(instr->temp());
2471 if (rep.IsDouble()) {
2472 DoubleRegister value = ToDoubleRegister(instr->value());
2473 EmitFalseBranchF(instr, ne, value, kDoubleRegZero);
2474 __ FmoveHigh(scratch, value);
2475 // Only use low 32-bits of value.
2476 __ dsll32(scratch, scratch, 0);
2477 __ dsrl32(scratch, scratch, 0);
2478 __ li(at, 0x80000000);
2480 Register value = ToRegister(instr->value());
2483 Heap::kHeapNumberMapRootIndex,
2484 instr->FalseLabel(chunk()),
2486 __ lwu(scratch, FieldMemOperand(value, HeapNumber::kExponentOffset));
2487 EmitFalseBranch(instr, ne, scratch, Operand(0x80000000));
2488 __ lwu(scratch, FieldMemOperand(value, HeapNumber::kMantissaOffset));
2489 __ mov(at, zero_reg);
2491 EmitBranch(instr, eq, scratch, Operand(at));
2495 Condition LCodeGen::EmitIsString(Register input,
2497 Label* is_not_string,
2498 SmiCheck check_needed = INLINE_SMI_CHECK) {
2499 if (check_needed == INLINE_SMI_CHECK) {
2500 __ JumpIfSmi(input, is_not_string);
2502 __ GetObjectType(input, temp1, temp1);
2508 void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
2509 Register reg = ToRegister(instr->value());
2510 Register temp1 = ToRegister(instr->temp());
2512 SmiCheck check_needed =
2513 instr->hydrogen()->value()->type().IsHeapObject()
2514 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2515 Condition true_cond =
2516 EmitIsString(reg, temp1, instr->FalseLabel(chunk_), check_needed);
2518 EmitBranch(instr, true_cond, temp1,
2519 Operand(FIRST_NONSTRING_TYPE));
2523 void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
2524 Register input_reg = EmitLoadRegister(instr->value(), at);
2525 __ And(at, input_reg, kSmiTagMask);
2526 EmitBranch(instr, eq, at, Operand(zero_reg));
2530 void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
2531 Register input = ToRegister(instr->value());
2532 Register temp = ToRegister(instr->temp());
2534 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2535 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2537 __ ld(temp, FieldMemOperand(input, HeapObject::kMapOffset));
2538 __ lbu(temp, FieldMemOperand(temp, Map::kBitFieldOffset));
2539 __ And(at, temp, Operand(1 << Map::kIsUndetectable));
2540 EmitBranch(instr, ne, at, Operand(zero_reg));
2544 static Condition ComputeCompareCondition(Token::Value op) {
2546 case Token::EQ_STRICT:
2559 return kNoCondition;
2564 void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
2565 DCHECK(ToRegister(instr->context()).is(cp));
2566 Token::Value op = instr->op();
2569 CodeFactory::CompareIC(isolate(), op, Strength::WEAK).code();
2570 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2572 Condition condition = ComputeCompareCondition(op);
2574 EmitBranch(instr, condition, v0, Operand(zero_reg));
2578 static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
2579 InstanceType from = instr->from();
2580 InstanceType to = instr->to();
2581 if (from == FIRST_TYPE) return to;
2582 DCHECK(from == to || to == LAST_TYPE);
2587 static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
2588 InstanceType from = instr->from();
2589 InstanceType to = instr->to();
2590 if (from == to) return eq;
2591 if (to == LAST_TYPE) return hs;
2592 if (from == FIRST_TYPE) return ls;
2598 void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
2599 Register scratch = scratch0();
2600 Register input = ToRegister(instr->value());
2602 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2603 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2606 __ GetObjectType(input, scratch, scratch);
2608 BranchCondition(instr->hydrogen()),
2610 Operand(TestType(instr->hydrogen())));
2614 void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
2615 Register input = ToRegister(instr->value());
2616 Register result = ToRegister(instr->result());
2618 __ AssertString(input);
2620 __ lwu(result, FieldMemOperand(input, String::kHashFieldOffset));
2621 __ IndexFromHash(result, result);
2625 void LCodeGen::DoHasCachedArrayIndexAndBranch(
2626 LHasCachedArrayIndexAndBranch* instr) {
2627 Register input = ToRegister(instr->value());
2628 Register scratch = scratch0();
2631 FieldMemOperand(input, String::kHashFieldOffset));
2632 __ And(at, scratch, Operand(String::kContainsCachedArrayIndexMask));
2633 EmitBranch(instr, eq, at, Operand(zero_reg));
2637 // Branches to a label or falls through with the answer in flags. Trashes
2638 // the temp registers, but not the input.
2639 void LCodeGen::EmitClassOfTest(Label* is_true,
2641 Handle<String>class_name,
2645 DCHECK(!input.is(temp));
2646 DCHECK(!input.is(temp2));
2647 DCHECK(!temp.is(temp2));
2649 __ JumpIfSmi(input, is_false);
2651 if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
2652 // Assuming the following assertions, we can use the same compares to test
2653 // for both being a function type and being in the object type range.
2654 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
2655 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2656 FIRST_SPEC_OBJECT_TYPE + 1);
2657 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2658 LAST_SPEC_OBJECT_TYPE - 1);
2659 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
2661 __ GetObjectType(input, temp, temp2);
2662 __ Branch(is_false, lt, temp2, Operand(FIRST_SPEC_OBJECT_TYPE));
2663 __ Branch(is_true, eq, temp2, Operand(FIRST_SPEC_OBJECT_TYPE));
2664 __ Branch(is_true, eq, temp2, Operand(LAST_SPEC_OBJECT_TYPE));
2666 // Faster code path to avoid two compares: subtract lower bound from the
2667 // actual type and do a signed compare with the width of the type range.
2668 __ GetObjectType(input, temp, temp2);
2669 __ Dsubu(temp2, temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2670 __ Branch(is_false, gt, temp2, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE -
2671 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2674 // Now we are in the FIRST-LAST_NONCALLABLE_SPEC_OBJECT_TYPE range.
2675 // Check if the constructor in the map is a function.
2676 Register instance_type = scratch1();
2677 DCHECK(!instance_type.is(temp));
2678 __ GetMapConstructor(temp, temp, temp2, instance_type);
2680 // Objects with a non-function constructor have class 'Object'.
2681 if (String::Equals(class_name, isolate()->factory()->Object_string())) {
2682 __ Branch(is_true, ne, instance_type, Operand(JS_FUNCTION_TYPE));
2684 __ Branch(is_false, ne, instance_type, Operand(JS_FUNCTION_TYPE));
2687 // temp now contains the constructor function. Grab the
2688 // instance class name from there.
2689 __ ld(temp, FieldMemOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2690 __ ld(temp, FieldMemOperand(temp,
2691 SharedFunctionInfo::kInstanceClassNameOffset));
2692 // The class name we are testing against is internalized since it's a literal.
2693 // The name in the constructor is internalized because of the way the context
2694 // is booted. This routine isn't expected to work for random API-created
2695 // classes and it doesn't have to because you can't access it with natives
2696 // syntax. Since both sides are internalized it is sufficient to use an
2697 // identity comparison.
2699 // End with the address of this class_name instance in temp register.
2700 // On MIPS, the caller must do the comparison with Handle<String>class_name.
2704 void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
2705 Register input = ToRegister(instr->value());
2706 Register temp = scratch0();
2707 Register temp2 = ToRegister(instr->temp());
2708 Handle<String> class_name = instr->hydrogen()->class_name();
2710 EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2711 class_name, input, temp, temp2);
2713 EmitBranch(instr, eq, temp, Operand(class_name));
2717 void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
2718 Register reg = ToRegister(instr->value());
2719 Register temp = ToRegister(instr->temp());
2721 __ ld(temp, FieldMemOperand(reg, HeapObject::kMapOffset));
2722 EmitBranch(instr, eq, temp, Operand(instr->map()));
2726 void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
2727 DCHECK(ToRegister(instr->context()).is(cp));
2728 Label true_label, done;
2729 DCHECK(ToRegister(instr->left()).is(InstanceOfDescriptor::LeftRegister()));
2730 DCHECK(ToRegister(instr->right()).is(InstanceOfDescriptor::RightRegister()));
2731 DCHECK(ToRegister(instr->result()).is(v0));
2733 InstanceOfStub stub(isolate());
2734 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2738 void LCodeGen::DoHasInPrototypeChainAndBranch(
2739 LHasInPrototypeChainAndBranch* instr) {
2740 Register const object = ToRegister(instr->object());
2741 Register const object_map = scratch0();
2742 Register const object_prototype = object_map;
2743 Register const prototype = ToRegister(instr->prototype());
2745 // The {object} must be a spec object. It's sufficient to know that {object}
2746 // is not a smi, since all other non-spec objects have {null} prototypes and
2747 // will be ruled out below.
2748 if (instr->hydrogen()->ObjectNeedsSmiCheck()) {
2749 __ SmiTst(object, at);
2750 EmitFalseBranch(instr, eq, at, Operand(zero_reg));
2753 // Loop through the {object}s prototype chain looking for the {prototype}.
2754 __ ld(object_map, FieldMemOperand(object, HeapObject::kMapOffset));
2757 __ ld(object_prototype, FieldMemOperand(object_map, Map::kPrototypeOffset));
2758 EmitTrueBranch(instr, eq, object_prototype, Operand(prototype));
2759 __ LoadRoot(at, Heap::kNullValueRootIndex);
2760 EmitFalseBranch(instr, eq, object_prototype, Operand(at));
2761 __ Branch(&loop, USE_DELAY_SLOT);
2762 __ ld(object_map, FieldMemOperand(object_prototype,
2763 HeapObject::kMapOffset)); // In delay slot.
2767 void LCodeGen::DoCmpT(LCmpT* instr) {
2768 DCHECK(ToRegister(instr->context()).is(cp));
2769 Token::Value op = instr->op();
2772 CodeFactory::CompareIC(isolate(), op, instr->strength()).code();
2773 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2774 // On MIPS there is no need for a "no inlined smi code" marker (nop).
2776 Condition condition = ComputeCompareCondition(op);
2777 // A minor optimization that relies on LoadRoot always emitting one
2779 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
2781 __ Branch(USE_DELAY_SLOT, &done, condition, v0, Operand(zero_reg));
2783 __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex);
2784 DCHECK_EQ(1, masm()->InstructionsGeneratedSince(&check));
2785 __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex);
2790 void LCodeGen::DoReturn(LReturn* instr) {
2791 if (FLAG_trace && info()->IsOptimizing()) {
2792 // Push the return value on the stack as the parameter.
2793 // Runtime::TraceExit returns its parameter in v0. We're leaving the code
2794 // managed by the register allocator and tearing down the frame, it's
2795 // safe to write to the context register.
2797 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2798 __ CallRuntime(Runtime::kTraceExit, 1);
2800 if (info()->saves_caller_doubles()) {
2801 RestoreCallerDoubles();
2803 int no_frame_start = -1;
2804 if (NeedsEagerFrame()) {
2806 no_frame_start = masm_->pc_offset();
2809 if (instr->has_constant_parameter_count()) {
2810 int parameter_count = ToInteger32(instr->constant_parameter_count());
2811 int32_t sp_delta = (parameter_count + 1) * kPointerSize;
2812 if (sp_delta != 0) {
2813 __ Daddu(sp, sp, Operand(sp_delta));
2816 DCHECK(info()->IsStub()); // Functions would need to drop one more value.
2817 Register reg = ToRegister(instr->parameter_count());
2818 // The argument count parameter is a smi
2820 __ dsll(at, reg, kPointerSizeLog2);
2821 __ Daddu(sp, sp, at);
2826 if (no_frame_start != -1) {
2827 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
2833 void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
2834 Register vector_register = ToRegister(instr->temp_vector());
2835 Register slot_register = LoadWithVectorDescriptor::SlotRegister();
2836 DCHECK(vector_register.is(LoadWithVectorDescriptor::VectorRegister()));
2837 DCHECK(slot_register.is(a0));
2839 AllowDeferredHandleDereference vector_structure_check;
2840 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2841 __ li(vector_register, vector);
2842 // No need to allocate this register.
2843 FeedbackVectorICSlot slot = instr->hydrogen()->slot();
2844 int index = vector->GetIndex(slot);
2845 __ li(slot_register, Operand(Smi::FromInt(index)));
2850 void LCodeGen::EmitVectorStoreICRegisters(T* instr) {
2851 Register vector_register = ToRegister(instr->temp_vector());
2852 Register slot_register = ToRegister(instr->temp_slot());
2854 AllowDeferredHandleDereference vector_structure_check;
2855 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2856 __ li(vector_register, vector);
2857 FeedbackVectorICSlot slot = instr->hydrogen()->slot();
2858 int index = vector->GetIndex(slot);
2859 __ li(slot_register, Operand(Smi::FromInt(index)));
2863 void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
2864 DCHECK(ToRegister(instr->context()).is(cp));
2865 DCHECK(ToRegister(instr->global_object())
2866 .is(LoadDescriptor::ReceiverRegister()));
2867 DCHECK(ToRegister(instr->result()).is(v0));
2869 __ li(LoadDescriptor::NameRegister(), Operand(instr->name()));
2870 EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
2872 CodeFactory::LoadICInOptimizedCode(isolate(), instr->typeof_mode(),
2873 SLOPPY, PREMONOMORPHIC).code();
2874 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2878 void LCodeGen::DoLoadGlobalViaContext(LLoadGlobalViaContext* instr) {
2879 DCHECK(ToRegister(instr->context()).is(cp));
2880 DCHECK(ToRegister(instr->result()).is(v0));
2882 int const slot = instr->slot_index();
2883 int const depth = instr->depth();
2884 if (depth <= LoadGlobalViaContextStub::kMaximumDepth) {
2885 __ li(LoadGlobalViaContextDescriptor::SlotRegister(), Operand(slot));
2887 CodeFactory::LoadGlobalViaContext(isolate(), depth).code();
2888 CallCode(stub, RelocInfo::CODE_TARGET, instr);
2890 __ Push(Smi::FromInt(slot));
2891 __ CallRuntime(Runtime::kLoadGlobalViaContext, 1);
2896 void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
2897 Register context = ToRegister(instr->context());
2898 Register result = ToRegister(instr->result());
2900 __ ld(result, ContextOperand(context, instr->slot_index()));
2901 if (instr->hydrogen()->RequiresHoleCheck()) {
2902 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2904 if (instr->hydrogen()->DeoptimizesOnHole()) {
2905 DeoptimizeIf(eq, instr, Deoptimizer::kHole, result, Operand(at));
2908 __ Branch(&is_not_hole, ne, result, Operand(at));
2909 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2910 __ bind(&is_not_hole);
2916 void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
2917 Register context = ToRegister(instr->context());
2918 Register value = ToRegister(instr->value());
2919 Register scratch = scratch0();
2920 MemOperand target = ContextOperand(context, instr->slot_index());
2922 Label skip_assignment;
2924 if (instr->hydrogen()->RequiresHoleCheck()) {
2925 __ ld(scratch, target);
2926 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2928 if (instr->hydrogen()->DeoptimizesOnHole()) {
2929 DeoptimizeIf(eq, instr, Deoptimizer::kHole, scratch, Operand(at));
2931 __ Branch(&skip_assignment, ne, scratch, Operand(at));
2935 __ sd(value, target);
2936 if (instr->hydrogen()->NeedsWriteBarrier()) {
2937 SmiCheck check_needed =
2938 instr->hydrogen()->value()->type().IsHeapObject()
2939 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2940 __ RecordWriteContextSlot(context,
2946 EMIT_REMEMBERED_SET,
2950 __ bind(&skip_assignment);
2954 void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
2955 HObjectAccess access = instr->hydrogen()->access();
2956 int offset = access.offset();
2957 Register object = ToRegister(instr->object());
2958 if (access.IsExternalMemory()) {
2959 Register result = ToRegister(instr->result());
2960 MemOperand operand = MemOperand(object, offset);
2961 __ Load(result, operand, access.representation());
2965 if (instr->hydrogen()->representation().IsDouble()) {
2966 DoubleRegister result = ToDoubleRegister(instr->result());
2967 __ ldc1(result, FieldMemOperand(object, offset));
2971 Register result = ToRegister(instr->result());
2972 if (!access.IsInobject()) {
2973 __ ld(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
2977 Representation representation = access.representation();
2978 if (representation.IsSmi() && SmiValuesAre32Bits() &&
2979 instr->hydrogen()->representation().IsInteger32()) {
2980 if (FLAG_debug_code) {
2981 // Verify this is really an Smi.
2982 Register scratch = scratch0();
2983 __ Load(scratch, FieldMemOperand(object, offset), representation);
2984 __ AssertSmi(scratch);
2987 // Read int value directly from upper half of the smi.
2988 STATIC_ASSERT(kSmiTag == 0);
2989 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 32);
2990 offset += kPointerSize / 2;
2991 representation = Representation::Integer32();
2993 __ Load(result, FieldMemOperand(object, offset), representation);
2997 void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
2998 DCHECK(ToRegister(instr->context()).is(cp));
2999 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3000 DCHECK(ToRegister(instr->result()).is(v0));
3002 // Name is always in a2.
3003 __ li(LoadDescriptor::NameRegister(), Operand(instr->name()));
3004 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
3006 CodeFactory::LoadICInOptimizedCode(
3007 isolate(), NOT_INSIDE_TYPEOF, instr->hydrogen()->language_mode(),
3008 instr->hydrogen()->initialization_state()).code();
3009 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3013 void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
3014 Register scratch = scratch0();
3015 Register function = ToRegister(instr->function());
3016 Register result = ToRegister(instr->result());
3018 // Get the prototype or initial map from the function.
3020 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
3022 // Check that the function has a prototype or an initial map.
3023 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
3024 DeoptimizeIf(eq, instr, Deoptimizer::kHole, result, Operand(at));
3026 // If the function does not have an initial map, we're done.
3028 __ GetObjectType(result, scratch, scratch);
3029 __ Branch(&done, ne, scratch, Operand(MAP_TYPE));
3031 // Get the prototype from the initial map.
3032 __ ld(result, FieldMemOperand(result, Map::kPrototypeOffset));
3039 void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
3040 Register result = ToRegister(instr->result());
3041 __ LoadRoot(result, instr->index());
3045 void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
3046 Register arguments = ToRegister(instr->arguments());
3047 Register result = ToRegister(instr->result());
3048 // There are two words between the frame pointer and the last argument.
3049 // Subtracting from length accounts for one of them add one more.
3050 if (instr->length()->IsConstantOperand()) {
3051 int const_length = ToInteger32(LConstantOperand::cast(instr->length()));
3052 if (instr->index()->IsConstantOperand()) {
3053 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3054 int index = (const_length - const_index) + 1;
3055 __ ld(result, MemOperand(arguments, index * kPointerSize));
3057 Register index = ToRegister(instr->index());
3058 __ li(at, Operand(const_length + 1));
3059 __ Dsubu(result, at, index);
3060 __ dsll(at, result, kPointerSizeLog2);
3061 __ Daddu(at, arguments, at);
3062 __ ld(result, MemOperand(at));
3064 } else if (instr->index()->IsConstantOperand()) {
3065 Register length = ToRegister(instr->length());
3066 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3067 int loc = const_index - 1;
3069 __ Dsubu(result, length, Operand(loc));
3070 __ dsll(at, result, kPointerSizeLog2);
3071 __ Daddu(at, arguments, at);
3072 __ ld(result, MemOperand(at));
3074 __ dsll(at, length, kPointerSizeLog2);
3075 __ Daddu(at, arguments, at);
3076 __ ld(result, MemOperand(at));
3079 Register length = ToRegister(instr->length());
3080 Register index = ToRegister(instr->index());
3081 __ Dsubu(result, length, index);
3082 __ Daddu(result, result, 1);
3083 __ dsll(at, result, kPointerSizeLog2);
3084 __ Daddu(at, arguments, at);
3085 __ ld(result, MemOperand(at));
3090 void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
3091 Register external_pointer = ToRegister(instr->elements());
3092 Register key = no_reg;
3093 ElementsKind elements_kind = instr->elements_kind();
3094 bool key_is_constant = instr->key()->IsConstantOperand();
3095 int constant_key = 0;
3096 if (key_is_constant) {
3097 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3098 if (constant_key & 0xF0000000) {
3099 Abort(kArrayIndexConstantValueTooBig);
3102 key = ToRegister(instr->key());
3104 int element_size_shift = ElementsKindToShiftSize(elements_kind);
3105 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3106 ? (element_size_shift - (kSmiTagSize + kSmiShiftSize))
3107 : element_size_shift;
3108 int base_offset = instr->base_offset();
3110 if (elements_kind == FLOAT32_ELEMENTS || elements_kind == FLOAT64_ELEMENTS) {
3111 FPURegister result = ToDoubleRegister(instr->result());
3112 if (key_is_constant) {
3113 __ Daddu(scratch0(), external_pointer,
3114 constant_key << element_size_shift);
3116 if (shift_size < 0) {
3117 if (shift_size == -32) {
3118 __ dsra32(scratch0(), key, 0);
3120 __ dsra(scratch0(), key, -shift_size);
3123 __ dsll(scratch0(), key, shift_size);
3125 __ Daddu(scratch0(), scratch0(), external_pointer);
3127 if (elements_kind == FLOAT32_ELEMENTS) {
3128 __ lwc1(result, MemOperand(scratch0(), base_offset));
3129 __ cvt_d_s(result, result);
3130 } else { // i.e. elements_kind == EXTERNAL_DOUBLE_ELEMENTS
3131 __ ldc1(result, MemOperand(scratch0(), base_offset));
3134 Register result = ToRegister(instr->result());
3135 MemOperand mem_operand = PrepareKeyedOperand(
3136 key, external_pointer, key_is_constant, constant_key,
3137 element_size_shift, shift_size, base_offset);
3138 switch (elements_kind) {
3140 __ lb(result, mem_operand);
3142 case UINT8_ELEMENTS:
3143 case UINT8_CLAMPED_ELEMENTS:
3144 __ lbu(result, mem_operand);
3146 case INT16_ELEMENTS:
3147 __ lh(result, mem_operand);
3149 case UINT16_ELEMENTS:
3150 __ lhu(result, mem_operand);
3152 case INT32_ELEMENTS:
3153 __ lw(result, mem_operand);
3155 case UINT32_ELEMENTS:
3156 __ lw(result, mem_operand);
3157 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
3158 DeoptimizeIf(Ugreater_equal, instr, Deoptimizer::kNegativeValue,
3159 result, Operand(0x80000000));
3162 case FLOAT32_ELEMENTS:
3163 case FLOAT64_ELEMENTS:
3164 case FAST_DOUBLE_ELEMENTS:
3166 case FAST_SMI_ELEMENTS:
3167 case FAST_HOLEY_DOUBLE_ELEMENTS:
3168 case FAST_HOLEY_ELEMENTS:
3169 case FAST_HOLEY_SMI_ELEMENTS:
3170 case DICTIONARY_ELEMENTS:
3171 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
3172 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
3180 void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
3181 Register elements = ToRegister(instr->elements());
3182 bool key_is_constant = instr->key()->IsConstantOperand();
3183 Register key = no_reg;
3184 DoubleRegister result = ToDoubleRegister(instr->result());
3185 Register scratch = scratch0();
3187 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
3189 int base_offset = instr->base_offset();
3190 if (key_is_constant) {
3191 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3192 if (constant_key & 0xF0000000) {
3193 Abort(kArrayIndexConstantValueTooBig);
3195 base_offset += constant_key * kDoubleSize;
3197 __ Daddu(scratch, elements, Operand(base_offset));
3199 if (!key_is_constant) {
3200 key = ToRegister(instr->key());
3201 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3202 ? (element_size_shift - (kSmiTagSize + kSmiShiftSize))
3203 : element_size_shift;
3204 if (shift_size > 0) {
3205 __ dsll(at, key, shift_size);
3206 } else if (shift_size == -32) {
3207 __ dsra32(at, key, 0);
3209 __ dsra(at, key, -shift_size);
3211 __ Daddu(scratch, scratch, at);
3214 __ ldc1(result, MemOperand(scratch));
3216 if (instr->hydrogen()->RequiresHoleCheck()) {
3217 __ FmoveHigh(scratch, result);
3218 DeoptimizeIf(eq, instr, Deoptimizer::kHole, scratch,
3219 Operand(static_cast<int32_t>(kHoleNanUpper32)));
3224 void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
3225 HLoadKeyed* hinstr = instr->hydrogen();
3226 Register elements = ToRegister(instr->elements());
3227 Register result = ToRegister(instr->result());
3228 Register scratch = scratch0();
3229 Register store_base = scratch;
3230 int offset = instr->base_offset();
3232 if (instr->key()->IsConstantOperand()) {
3233 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
3234 offset += ToInteger32(const_operand) * kPointerSize;
3235 store_base = elements;
3237 Register key = ToRegister(instr->key());
3238 // Even though the HLoadKeyed instruction forces the input
3239 // representation for the key to be an integer, the input gets replaced
3240 // during bound check elimination with the index argument to the bounds
3241 // check, which can be tagged, so that case must be handled here, too.
3242 if (instr->hydrogen()->key()->representation().IsSmi()) {
3243 __ SmiScale(scratch, key, kPointerSizeLog2);
3244 __ daddu(scratch, elements, scratch);
3246 __ dsll(scratch, key, kPointerSizeLog2);
3247 __ daddu(scratch, elements, scratch);
3251 Representation representation = hinstr->representation();
3252 if (representation.IsInteger32() && SmiValuesAre32Bits() &&
3253 hinstr->elements_kind() == FAST_SMI_ELEMENTS) {
3254 DCHECK(!hinstr->RequiresHoleCheck());
3255 if (FLAG_debug_code) {
3256 Register temp = scratch1();
3257 __ Load(temp, MemOperand(store_base, offset), Representation::Smi());
3261 // Read int value directly from upper half of the smi.
3262 STATIC_ASSERT(kSmiTag == 0);
3263 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 32);
3264 offset += kPointerSize / 2;
3267 __ Load(result, MemOperand(store_base, offset), representation);
3269 // Check for the hole value.
3270 if (hinstr->RequiresHoleCheck()) {
3271 if (IsFastSmiElementsKind(instr->hydrogen()->elements_kind())) {
3272 __ SmiTst(result, scratch);
3273 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi, scratch,
3276 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
3277 DeoptimizeIf(eq, instr, Deoptimizer::kHole, result, Operand(scratch));
3279 } else if (instr->hydrogen()->hole_mode() == CONVERT_HOLE_TO_UNDEFINED) {
3280 DCHECK(instr->hydrogen()->elements_kind() == FAST_HOLEY_ELEMENTS);
3282 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
3283 __ Branch(&done, ne, result, Operand(scratch));
3284 if (info()->IsStub()) {
3285 // A stub can safely convert the hole to undefined only if the array
3286 // protector cell contains (Smi) Isolate::kArrayProtectorValid. Otherwise
3287 // it needs to bail out.
3288 __ LoadRoot(result, Heap::kArrayProtectorRootIndex);
3289 // The comparison only needs LS bits of value, which is a smi.
3290 __ ld(result, FieldMemOperand(result, Cell::kValueOffset));
3291 DeoptimizeIf(ne, instr, Deoptimizer::kHole, result,
3292 Operand(Smi::FromInt(Isolate::kArrayProtectorValid)));
3294 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3300 void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
3301 if (instr->is_fixed_typed_array()) {
3302 DoLoadKeyedExternalArray(instr);
3303 } else if (instr->hydrogen()->representation().IsDouble()) {
3304 DoLoadKeyedFixedDoubleArray(instr);
3306 DoLoadKeyedFixedArray(instr);
3311 MemOperand LCodeGen::PrepareKeyedOperand(Register key,
3313 bool key_is_constant,
3318 if (key_is_constant) {
3319 return MemOperand(base, (constant_key << element_size) + base_offset);
3322 if (base_offset == 0) {
3323 if (shift_size >= 0) {
3324 __ dsll(scratch0(), key, shift_size);
3325 __ Daddu(scratch0(), base, scratch0());
3326 return MemOperand(scratch0());
3328 if (shift_size == -32) {
3329 __ dsra32(scratch0(), key, 0);
3331 __ dsra(scratch0(), key, -shift_size);
3333 __ Daddu(scratch0(), base, scratch0());
3334 return MemOperand(scratch0());
3338 if (shift_size >= 0) {
3339 __ dsll(scratch0(), key, shift_size);
3340 __ Daddu(scratch0(), base, scratch0());
3341 return MemOperand(scratch0(), base_offset);
3343 if (shift_size == -32) {
3344 __ dsra32(scratch0(), key, 0);
3346 __ dsra(scratch0(), key, -shift_size);
3348 __ Daddu(scratch0(), base, scratch0());
3349 return MemOperand(scratch0(), base_offset);
3354 void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3355 DCHECK(ToRegister(instr->context()).is(cp));
3356 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3357 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
3359 if (instr->hydrogen()->HasVectorAndSlot()) {
3360 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
3363 Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(
3364 isolate(), instr->hydrogen()->language_mode(),
3365 instr->hydrogen()->initialization_state()).code();
3366 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3370 void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
3371 Register scratch = scratch0();
3372 Register temp = scratch1();
3373 Register result = ToRegister(instr->result());
3375 if (instr->hydrogen()->from_inlined()) {
3376 __ Dsubu(result, sp, 2 * kPointerSize);
3378 // Check if the calling frame is an arguments adaptor frame.
3379 Label done, adapted;
3380 __ ld(scratch, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3381 __ ld(result, MemOperand(scratch, StandardFrameConstants::kContextOffset));
3382 __ Xor(temp, result, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3384 // Result is the frame pointer for the frame if not adapted and for the real
3385 // frame below the adaptor frame if adapted.
3386 __ Movn(result, fp, temp); // Move only if temp is not equal to zero (ne).
3387 __ Movz(result, scratch, temp); // Move only if temp is equal to zero (eq).
3392 void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
3393 Register elem = ToRegister(instr->elements());
3394 Register result = ToRegister(instr->result());
3398 // If no arguments adaptor frame the number of arguments is fixed.
3399 __ Daddu(result, zero_reg, Operand(scope()->num_parameters()));
3400 __ Branch(&done, eq, fp, Operand(elem));
3402 // Arguments adaptor frame present. Get argument length from there.
3403 __ ld(result, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3405 MemOperand(result, ArgumentsAdaptorFrameConstants::kLengthOffset));
3406 __ SmiUntag(result);
3408 // Argument length is in result register.
3413 void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
3414 Register receiver = ToRegister(instr->receiver());
3415 Register function = ToRegister(instr->function());
3416 Register result = ToRegister(instr->result());
3417 Register scratch = scratch0();
3419 // If the receiver is null or undefined, we have to pass the global
3420 // object as a receiver to normal functions. Values have to be
3421 // passed unchanged to builtins and strict-mode functions.
3422 Label global_object, result_in_receiver;
3424 if (!instr->hydrogen()->known_function()) {
3425 // Do not transform the receiver to object for strict mode functions.
3427 FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
3429 // Do not transform the receiver to object for builtins.
3430 int32_t strict_mode_function_mask =
3431 1 << SharedFunctionInfo::kStrictModeBitWithinByte;
3432 int32_t native_mask = 1 << SharedFunctionInfo::kNativeBitWithinByte;
3435 FieldMemOperand(scratch, SharedFunctionInfo::kStrictModeByteOffset));
3436 __ And(at, at, Operand(strict_mode_function_mask));
3437 __ Branch(&result_in_receiver, ne, at, Operand(zero_reg));
3439 FieldMemOperand(scratch, SharedFunctionInfo::kNativeByteOffset));
3440 __ And(at, at, Operand(native_mask));
3441 __ Branch(&result_in_receiver, ne, at, Operand(zero_reg));
3444 // Normal function. Replace undefined or null with global receiver.
3445 __ LoadRoot(scratch, Heap::kNullValueRootIndex);
3446 __ Branch(&global_object, eq, receiver, Operand(scratch));
3447 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
3448 __ Branch(&global_object, eq, receiver, Operand(scratch));
3450 // Deoptimize if the receiver is not a JS object.
3451 __ SmiTst(receiver, scratch);
3452 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, scratch, Operand(zero_reg));
3454 __ GetObjectType(receiver, scratch, scratch);
3455 DeoptimizeIf(lt, instr, Deoptimizer::kNotAJavaScriptObject, scratch,
3456 Operand(FIRST_SPEC_OBJECT_TYPE));
3457 __ Branch(&result_in_receiver);
3459 __ bind(&global_object);
3460 __ ld(result, FieldMemOperand(function, JSFunction::kContextOffset));
3462 ContextOperand(result, Context::GLOBAL_OBJECT_INDEX));
3464 FieldMemOperand(result, GlobalObject::kGlobalProxyOffset));
3466 if (result.is(receiver)) {
3467 __ bind(&result_in_receiver);
3470 __ Branch(&result_ok);
3471 __ bind(&result_in_receiver);
3472 __ mov(result, receiver);
3473 __ bind(&result_ok);
3478 void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3479 Register receiver = ToRegister(instr->receiver());
3480 Register function = ToRegister(instr->function());
3481 Register length = ToRegister(instr->length());
3482 Register elements = ToRegister(instr->elements());
3483 Register scratch = scratch0();
3484 DCHECK(receiver.is(a0)); // Used for parameter count.
3485 DCHECK(function.is(a1)); // Required by InvokeFunction.
3486 DCHECK(ToRegister(instr->result()).is(v0));
3488 // Copy the arguments to this function possibly from the
3489 // adaptor frame below it.
3490 const uint32_t kArgumentsLimit = 1 * KB;
3491 DeoptimizeIf(hi, instr, Deoptimizer::kTooManyArguments, length,
3492 Operand(kArgumentsLimit));
3494 // Push the receiver and use the register to keep the original
3495 // number of arguments.
3497 __ Move(receiver, length);
3498 // The arguments are at a one pointer size offset from elements.
3499 __ Daddu(elements, elements, Operand(1 * kPointerSize));
3501 // Loop through the arguments pushing them onto the execution
3504 // length is a small non-negative integer, due to the test above.
3505 __ Branch(USE_DELAY_SLOT, &invoke, eq, length, Operand(zero_reg));
3506 __ dsll(scratch, length, kPointerSizeLog2);
3508 __ Daddu(scratch, elements, scratch);
3509 __ ld(scratch, MemOperand(scratch));
3511 __ Dsubu(length, length, Operand(1));
3512 __ Branch(USE_DELAY_SLOT, &loop, ne, length, Operand(zero_reg));
3513 __ dsll(scratch, length, kPointerSizeLog2);
3516 DCHECK(instr->HasPointerMap());
3517 LPointerMap* pointers = instr->pointer_map();
3518 SafepointGenerator safepoint_generator(
3519 this, pointers, Safepoint::kLazyDeopt);
3520 // The number of arguments is stored in receiver which is a0, as expected
3521 // by InvokeFunction.
3522 ParameterCount actual(receiver);
3523 __ InvokeFunction(function, actual, CALL_FUNCTION, safepoint_generator);
3527 void LCodeGen::DoPushArgument(LPushArgument* instr) {
3528 LOperand* argument = instr->value();
3529 if (argument->IsDoubleRegister() || argument->IsDoubleStackSlot()) {
3530 Abort(kDoPushArgumentNotImplementedForDoubleType);
3532 Register argument_reg = EmitLoadRegister(argument, at);
3533 __ push(argument_reg);
3538 void LCodeGen::DoDrop(LDrop* instr) {
3539 __ Drop(instr->count());
3543 void LCodeGen::DoThisFunction(LThisFunction* instr) {
3544 Register result = ToRegister(instr->result());
3545 __ ld(result, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3549 void LCodeGen::DoContext(LContext* instr) {
3550 // If there is a non-return use, the context must be moved to a register.
3551 Register result = ToRegister(instr->result());
3552 if (info()->IsOptimizing()) {
3553 __ ld(result, MemOperand(fp, StandardFrameConstants::kContextOffset));
3555 // If there is no frame, the context must be in cp.
3556 DCHECK(result.is(cp));
3561 void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
3562 DCHECK(ToRegister(instr->context()).is(cp));
3563 __ li(scratch0(), instr->hydrogen()->pairs());
3564 __ li(scratch1(), Operand(Smi::FromInt(instr->hydrogen()->flags())));
3565 __ Push(scratch0(), scratch1());
3566 CallRuntime(Runtime::kDeclareGlobals, 2, instr);
3570 void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
3571 int formal_parameter_count, int arity,
3572 LInstruction* instr) {
3573 bool dont_adapt_arguments =
3574 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3575 bool can_invoke_directly =
3576 dont_adapt_arguments || formal_parameter_count == arity;
3578 Register function_reg = a1;
3579 LPointerMap* pointers = instr->pointer_map();
3581 if (can_invoke_directly) {
3583 __ ld(cp, FieldMemOperand(function_reg, JSFunction::kContextOffset));
3585 // Always initialize a0 to the number of actual arguments.
3586 __ li(a0, Operand(arity));
3589 __ ld(at, FieldMemOperand(function_reg, JSFunction::kCodeEntryOffset));
3592 // Set up deoptimization.
3593 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3595 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3596 ParameterCount count(arity);
3597 ParameterCount expected(formal_parameter_count);
3598 __ InvokeFunction(function_reg, expected, count, CALL_FUNCTION, generator);
3603 void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3604 DCHECK(instr->context() != NULL);
3605 DCHECK(ToRegister(instr->context()).is(cp));
3606 Register input = ToRegister(instr->value());
3607 Register result = ToRegister(instr->result());
3608 Register scratch = scratch0();
3610 // Deoptimize if not a heap number.
3611 __ ld(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
3612 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
3613 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, scratch, Operand(at));
3616 Register exponent = scratch0();
3618 __ lwu(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3619 // Check the sign of the argument. If the argument is positive, just
3621 __ Move(result, input);
3622 __ And(at, exponent, Operand(HeapNumber::kSignMask));
3623 __ Branch(&done, eq, at, Operand(zero_reg));
3625 // Input is negative. Reverse its sign.
3626 // Preserve the value of all registers.
3628 PushSafepointRegistersScope scope(this);
3630 // Registers were saved at the safepoint, so we can use
3631 // many scratch registers.
3632 Register tmp1 = input.is(a1) ? a0 : a1;
3633 Register tmp2 = input.is(a2) ? a0 : a2;
3634 Register tmp3 = input.is(a3) ? a0 : a3;
3635 Register tmp4 = input.is(a4) ? a0 : a4;
3637 // exponent: floating point exponent value.
3639 Label allocated, slow;
3640 __ LoadRoot(tmp4, Heap::kHeapNumberMapRootIndex);
3641 __ AllocateHeapNumber(tmp1, tmp2, tmp3, tmp4, &slow);
3642 __ Branch(&allocated);
3644 // Slow case: Call the runtime system to do the number allocation.
3647 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr,
3649 // Set the pointer to the new heap number in tmp.
3652 // Restore input_reg after call to runtime.
3653 __ LoadFromSafepointRegisterSlot(input, input);
3654 __ lwu(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3656 __ bind(&allocated);
3657 // exponent: floating point exponent value.
3658 // tmp1: allocated heap number.
3659 __ And(exponent, exponent, Operand(~HeapNumber::kSignMask));
3660 __ sw(exponent, FieldMemOperand(tmp1, HeapNumber::kExponentOffset));
3661 __ lwu(tmp2, FieldMemOperand(input, HeapNumber::kMantissaOffset));
3662 __ sw(tmp2, FieldMemOperand(tmp1, HeapNumber::kMantissaOffset));
3664 __ StoreToSafepointRegisterSlot(tmp1, result);
3671 void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3672 Register input = ToRegister(instr->value());
3673 Register result = ToRegister(instr->result());
3674 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
3676 __ Branch(USE_DELAY_SLOT, &done, ge, input, Operand(zero_reg));
3677 __ mov(result, input);
3678 __ subu(result, zero_reg, input);
3679 // Overflow if result is still negative, i.e. 0x80000000.
3680 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, result, Operand(zero_reg));
3685 void LCodeGen::EmitSmiMathAbs(LMathAbs* instr) {
3686 Register input = ToRegister(instr->value());
3687 Register result = ToRegister(instr->result());
3688 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
3690 __ Branch(USE_DELAY_SLOT, &done, ge, input, Operand(zero_reg));
3691 __ mov(result, input);
3692 __ dsubu(result, zero_reg, input);
3693 // Overflow if result is still negative, i.e. 0x80000000 00000000.
3694 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, result, Operand(zero_reg));
3699 void LCodeGen::DoMathAbs(LMathAbs* instr) {
3700 // Class for deferred case.
3701 class DeferredMathAbsTaggedHeapNumber final : public LDeferredCode {
3703 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr)
3704 : LDeferredCode(codegen), instr_(instr) { }
3705 void Generate() override {
3706 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3708 LInstruction* instr() override { return instr_; }
3714 Representation r = instr->hydrogen()->value()->representation();
3716 FPURegister input = ToDoubleRegister(instr->value());
3717 FPURegister result = ToDoubleRegister(instr->result());
3718 __ abs_d(result, input);
3719 } else if (r.IsInteger32()) {
3720 EmitIntegerMathAbs(instr);
3721 } else if (r.IsSmi()) {
3722 EmitSmiMathAbs(instr);
3724 // Representation is tagged.
3725 DeferredMathAbsTaggedHeapNumber* deferred =
3726 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3727 Register input = ToRegister(instr->value());
3729 __ JumpIfNotSmi(input, deferred->entry());
3730 // If smi, handle it directly.
3731 EmitSmiMathAbs(instr);
3732 __ bind(deferred->exit());
3737 void LCodeGen::DoMathFloor(LMathFloor* instr) {
3738 DoubleRegister input = ToDoubleRegister(instr->value());
3739 Register result = ToRegister(instr->result());
3740 Register scratch1 = scratch0();
3741 Register except_flag = ToRegister(instr->temp());
3743 __ EmitFPUTruncate(kRoundToMinusInf,
3750 // Deopt if the operation did not succeed.
3751 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
3754 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3757 __ Branch(&done, ne, result, Operand(zero_reg));
3758 __ mfhc1(scratch1, input); // Get exponent/sign bits.
3759 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
3760 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
3767 void LCodeGen::DoMathRound(LMathRound* instr) {
3768 DoubleRegister input = ToDoubleRegister(instr->value());
3769 Register result = ToRegister(instr->result());
3770 DoubleRegister double_scratch1 = ToDoubleRegister(instr->temp());
3771 Register scratch = scratch0();
3772 Label done, check_sign_on_zero;
3774 // Extract exponent bits.
3775 __ mfhc1(result, input);
3778 HeapNumber::kExponentShift,
3779 HeapNumber::kExponentBits);
3781 // If the number is in ]-0.5, +0.5[, the result is +/- 0.
3783 __ Branch(&skip1, gt, scratch, Operand(HeapNumber::kExponentBias - 2));
3784 __ mov(result, zero_reg);
3785 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3786 __ Branch(&check_sign_on_zero);
3792 // The following conversion will not work with numbers
3793 // outside of ]-2^32, 2^32[.
3794 DeoptimizeIf(ge, instr, Deoptimizer::kOverflow, scratch,
3795 Operand(HeapNumber::kExponentBias + 32));
3797 // Save the original sign for later comparison.
3798 __ And(scratch, result, Operand(HeapNumber::kSignMask));
3800 __ Move(double_scratch0(), 0.5);
3801 __ add_d(double_scratch0(), input, double_scratch0());
3803 // Check sign of the result: if the sign changed, the input
3804 // value was in ]0.5, 0[ and the result should be -0.
3805 __ mfhc1(result, double_scratch0());
3806 // mfhc1 sign-extends, clear the upper bits.
3807 __ dsll32(result, result, 0);
3808 __ dsrl32(result, result, 0);
3809 __ Xor(result, result, Operand(scratch));
3810 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3811 // ARM uses 'mi' here, which is 'lt'
3812 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, result, Operand(zero_reg));
3815 // ARM uses 'mi' here, which is 'lt'
3816 // Negating it results in 'ge'
3817 __ Branch(&skip2, ge, result, Operand(zero_reg));
3818 __ mov(result, zero_reg);
3823 Register except_flag = scratch;
3824 __ EmitFPUTruncate(kRoundToMinusInf,
3831 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
3834 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3836 __ Branch(&done, ne, result, Operand(zero_reg));
3837 __ bind(&check_sign_on_zero);
3838 __ mfhc1(scratch, input); // Get exponent/sign bits.
3839 __ And(scratch, scratch, Operand(HeapNumber::kSignMask));
3840 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch,
3847 void LCodeGen::DoMathFround(LMathFround* instr) {
3848 DoubleRegister input = ToDoubleRegister(instr->value());
3849 DoubleRegister result = ToDoubleRegister(instr->result());
3850 __ cvt_s_d(result, input);
3851 __ cvt_d_s(result, result);
3855 void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3856 DoubleRegister input = ToDoubleRegister(instr->value());
3857 DoubleRegister result = ToDoubleRegister(instr->result());
3858 __ sqrt_d(result, input);
3862 void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3863 DoubleRegister input = ToDoubleRegister(instr->value());
3864 DoubleRegister result = ToDoubleRegister(instr->result());
3865 DoubleRegister temp = ToDoubleRegister(instr->temp());
3867 DCHECK(!input.is(result));
3869 // Note that according to ECMA-262 15.8.2.13:
3870 // Math.pow(-Infinity, 0.5) == Infinity
3871 // Math.sqrt(-Infinity) == NaN
3873 __ Move(temp, static_cast<double>(-V8_INFINITY));
3874 __ BranchF(USE_DELAY_SLOT, &done, NULL, eq, temp, input);
3875 // Set up Infinity in the delay slot.
3876 // result is overwritten if the branch is not taken.
3877 __ neg_d(result, temp);
3879 // Add +0 to convert -0 to +0.
3880 __ add_d(result, input, kDoubleRegZero);
3881 __ sqrt_d(result, result);
3886 void LCodeGen::DoPower(LPower* instr) {
3887 Representation exponent_type = instr->hydrogen()->right()->representation();
3888 // Having marked this as a call, we can use any registers.
3889 // Just make sure that the input/output registers are the expected ones.
3890 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3891 DCHECK(!instr->right()->IsDoubleRegister() ||
3892 ToDoubleRegister(instr->right()).is(f4));
3893 DCHECK(!instr->right()->IsRegister() ||
3894 ToRegister(instr->right()).is(tagged_exponent));
3895 DCHECK(ToDoubleRegister(instr->left()).is(f2));
3896 DCHECK(ToDoubleRegister(instr->result()).is(f0));
3898 if (exponent_type.IsSmi()) {
3899 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3901 } else if (exponent_type.IsTagged()) {
3903 __ JumpIfSmi(tagged_exponent, &no_deopt);
3904 DCHECK(!a7.is(tagged_exponent));
3905 __ lw(a7, FieldMemOperand(tagged_exponent, HeapObject::kMapOffset));
3906 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
3907 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, a7, Operand(at));
3909 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3911 } else if (exponent_type.IsInteger32()) {
3912 MathPowStub stub(isolate(), MathPowStub::INTEGER);
3915 DCHECK(exponent_type.IsDouble());
3916 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
3922 void LCodeGen::DoMathExp(LMathExp* instr) {
3923 DoubleRegister input = ToDoubleRegister(instr->value());
3924 DoubleRegister result = ToDoubleRegister(instr->result());
3925 DoubleRegister double_scratch1 = ToDoubleRegister(instr->double_temp());
3926 DoubleRegister double_scratch2 = double_scratch0();
3927 Register temp1 = ToRegister(instr->temp1());
3928 Register temp2 = ToRegister(instr->temp2());
3930 MathExpGenerator::EmitMathExp(
3931 masm(), input, result, double_scratch1, double_scratch2,
3932 temp1, temp2, scratch0());
3936 void LCodeGen::DoMathLog(LMathLog* instr) {
3937 __ PrepareCallCFunction(0, 1, scratch0());
3938 __ MovToFloatParameter(ToDoubleRegister(instr->value()));
3939 __ CallCFunction(ExternalReference::math_log_double_function(isolate()),
3941 __ MovFromFloatResult(ToDoubleRegister(instr->result()));
3945 void LCodeGen::DoMathClz32(LMathClz32* instr) {
3946 Register input = ToRegister(instr->value());
3947 Register result = ToRegister(instr->result());
3948 __ Clz(result, input);
3952 void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
3953 DCHECK(ToRegister(instr->context()).is(cp));
3954 DCHECK(ToRegister(instr->function()).is(a1));
3955 DCHECK(instr->HasPointerMap());
3957 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
3958 if (known_function.is_null()) {
3959 LPointerMap* pointers = instr->pointer_map();
3960 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3961 ParameterCount count(instr->arity());
3962 __ InvokeFunction(a1, count, CALL_FUNCTION, generator);
3964 CallKnownFunction(known_function,
3965 instr->hydrogen()->formal_parameter_count(),
3966 instr->arity(), instr);
3971 void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
3972 DCHECK(ToRegister(instr->result()).is(v0));
3974 if (instr->hydrogen()->IsTailCall()) {
3975 if (NeedsEagerFrame()) __ LeaveFrame(StackFrame::INTERNAL);
3977 if (instr->target()->IsConstantOperand()) {
3978 LConstantOperand* target = LConstantOperand::cast(instr->target());
3979 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3980 __ Jump(code, RelocInfo::CODE_TARGET);
3982 DCHECK(instr->target()->IsRegister());
3983 Register target = ToRegister(instr->target());
3984 __ Daddu(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
3988 LPointerMap* pointers = instr->pointer_map();
3989 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3991 if (instr->target()->IsConstantOperand()) {
3992 LConstantOperand* target = LConstantOperand::cast(instr->target());
3993 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3994 generator.BeforeCall(__ CallSize(code, RelocInfo::CODE_TARGET));
3995 __ Call(code, RelocInfo::CODE_TARGET);
3997 DCHECK(instr->target()->IsRegister());
3998 Register target = ToRegister(instr->target());
3999 generator.BeforeCall(__ CallSize(target));
4000 __ Daddu(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
4003 generator.AfterCall();
4008 void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
4009 DCHECK(ToRegister(instr->function()).is(a1));
4010 DCHECK(ToRegister(instr->result()).is(v0));
4012 __ li(a0, Operand(instr->arity()));
4015 __ ld(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
4017 // Load the code entry address
4018 __ ld(at, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
4021 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
4025 void LCodeGen::DoCallFunction(LCallFunction* instr) {
4026 DCHECK(ToRegister(instr->context()).is(cp));
4027 DCHECK(ToRegister(instr->function()).is(a1));
4028 DCHECK(ToRegister(instr->result()).is(v0));
4030 int arity = instr->arity();
4031 CallFunctionFlags flags = instr->hydrogen()->function_flags();
4032 if (instr->hydrogen()->HasVectorAndSlot()) {
4033 Register slot_register = ToRegister(instr->temp_slot());
4034 Register vector_register = ToRegister(instr->temp_vector());
4035 DCHECK(slot_register.is(a3));
4036 DCHECK(vector_register.is(a2));
4038 AllowDeferredHandleDereference vector_structure_check;
4039 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
4040 int index = vector->GetIndex(instr->hydrogen()->slot());
4042 __ li(vector_register, vector);
4043 __ li(slot_register, Operand(Smi::FromInt(index)));
4045 CallICState::CallType call_type =
4046 (flags & CALL_AS_METHOD) ? CallICState::METHOD : CallICState::FUNCTION;
4049 CodeFactory::CallICInOptimizedCode(isolate(), arity, call_type).code();
4050 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4052 CallFunctionStub stub(isolate(), arity, flags);
4053 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4058 void LCodeGen::DoCallNew(LCallNew* instr) {
4059 DCHECK(ToRegister(instr->context()).is(cp));
4060 DCHECK(ToRegister(instr->constructor()).is(a1));
4061 DCHECK(ToRegister(instr->result()).is(v0));
4063 __ li(a0, Operand(instr->arity()));
4064 // No cell in a2 for construct type feedback in optimized code
4065 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4066 CallConstructStub stub(isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
4067 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4071 void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
4072 DCHECK(ToRegister(instr->context()).is(cp));
4073 DCHECK(ToRegister(instr->constructor()).is(a1));
4074 DCHECK(ToRegister(instr->result()).is(v0));
4076 __ li(a0, Operand(instr->arity()));
4077 if (instr->arity() == 1) {
4078 // We only need the allocation site for the case we have a length argument.
4079 // The case may bail out to the runtime, which will determine the correct
4080 // elements kind with the site.
4081 __ li(a2, instr->hydrogen()->site());
4083 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4085 ElementsKind kind = instr->hydrogen()->elements_kind();
4086 AllocationSiteOverrideMode override_mode =
4087 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
4088 ? DISABLE_ALLOCATION_SITES
4091 if (instr->arity() == 0) {
4092 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
4093 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4094 } else if (instr->arity() == 1) {
4096 if (IsFastPackedElementsKind(kind)) {
4098 // We might need a change here,
4099 // look at the first argument.
4100 __ ld(a5, MemOperand(sp, 0));
4101 __ Branch(&packed_case, eq, a5, Operand(zero_reg));
4103 ElementsKind holey_kind = GetHoleyElementsKind(kind);
4104 ArraySingleArgumentConstructorStub stub(isolate(),
4107 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4109 __ bind(&packed_case);
4112 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
4113 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4116 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
4117 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4122 void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
4123 CallRuntime(instr->function(), instr->arity(), instr);
4127 void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
4128 Register function = ToRegister(instr->function());
4129 Register code_object = ToRegister(instr->code_object());
4130 __ Daddu(code_object, code_object,
4131 Operand(Code::kHeaderSize - kHeapObjectTag));
4133 FieldMemOperand(function, JSFunction::kCodeEntryOffset));
4137 void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
4138 Register result = ToRegister(instr->result());
4139 Register base = ToRegister(instr->base_object());
4140 if (instr->offset()->IsConstantOperand()) {
4141 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
4142 __ Daddu(result, base, Operand(ToInteger32(offset)));
4144 Register offset = ToRegister(instr->offset());
4145 __ Daddu(result, base, offset);
4150 void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
4151 Representation representation = instr->representation();
4153 Register object = ToRegister(instr->object());
4154 Register scratch2 = scratch1();
4155 Register scratch1 = scratch0();
4157 HObjectAccess access = instr->hydrogen()->access();
4158 int offset = access.offset();
4159 if (access.IsExternalMemory()) {
4160 Register value = ToRegister(instr->value());
4161 MemOperand operand = MemOperand(object, offset);
4162 __ Store(value, operand, representation);
4166 __ AssertNotSmi(object);
4168 DCHECK(!representation.IsSmi() ||
4169 !instr->value()->IsConstantOperand() ||
4170 IsSmi(LConstantOperand::cast(instr->value())));
4171 if (!FLAG_unbox_double_fields && representation.IsDouble()) {
4172 DCHECK(access.IsInobject());
4173 DCHECK(!instr->hydrogen()->has_transition());
4174 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4175 DoubleRegister value = ToDoubleRegister(instr->value());
4176 __ sdc1(value, FieldMemOperand(object, offset));
4180 if (instr->hydrogen()->has_transition()) {
4181 Handle<Map> transition = instr->hydrogen()->transition_map();
4182 AddDeprecationDependency(transition);
4183 __ li(scratch1, Operand(transition));
4184 __ sd(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
4185 if (instr->hydrogen()->NeedsWriteBarrierForMap()) {
4186 Register temp = ToRegister(instr->temp());
4187 // Update the write barrier for the map field.
4188 __ RecordWriteForMap(object,
4197 Register destination = object;
4198 if (!access.IsInobject()) {
4199 destination = scratch1;
4200 __ ld(destination, FieldMemOperand(object, JSObject::kPropertiesOffset));
4203 if (representation.IsSmi() && SmiValuesAre32Bits() &&
4204 instr->hydrogen()->value()->representation().IsInteger32()) {
4205 DCHECK(instr->hydrogen()->store_mode() == STORE_TO_INITIALIZED_ENTRY);
4206 if (FLAG_debug_code) {
4207 __ Load(scratch2, FieldMemOperand(destination, offset), representation);
4208 __ AssertSmi(scratch2);
4210 // Store int value directly to upper half of the smi.
4211 offset += kPointerSize / 2;
4212 representation = Representation::Integer32();
4214 MemOperand operand = FieldMemOperand(destination, offset);
4216 if (FLAG_unbox_double_fields && representation.IsDouble()) {
4217 DCHECK(access.IsInobject());
4218 DoubleRegister value = ToDoubleRegister(instr->value());
4219 __ sdc1(value, operand);
4221 DCHECK(instr->value()->IsRegister());
4222 Register value = ToRegister(instr->value());
4223 __ Store(value, operand, representation);
4226 if (instr->hydrogen()->NeedsWriteBarrier()) {
4227 // Update the write barrier for the object for in-object properties.
4228 Register value = ToRegister(instr->value());
4229 __ RecordWriteField(destination,
4235 EMIT_REMEMBERED_SET,
4236 instr->hydrogen()->SmiCheckForWriteBarrier(),
4237 instr->hydrogen()->PointersToHereCheckForValue());
4242 void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
4243 DCHECK(ToRegister(instr->context()).is(cp));
4244 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4245 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4247 if (instr->hydrogen()->HasVectorAndSlot()) {
4248 EmitVectorStoreICRegisters<LStoreNamedGeneric>(instr);
4251 __ li(StoreDescriptor::NameRegister(), Operand(instr->name()));
4252 Handle<Code> ic = CodeFactory::StoreICInOptimizedCode(
4253 isolate(), instr->language_mode(),
4254 instr->hydrogen()->initialization_state()).code();
4255 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4259 void LCodeGen::DoStoreGlobalViaContext(LStoreGlobalViaContext* instr) {
4260 DCHECK(ToRegister(instr->context()).is(cp));
4261 DCHECK(ToRegister(instr->value())
4262 .is(StoreGlobalViaContextDescriptor::ValueRegister()));
4264 int const slot = instr->slot_index();
4265 int const depth = instr->depth();
4266 if (depth <= StoreGlobalViaContextStub::kMaximumDepth) {
4267 __ li(StoreGlobalViaContextDescriptor::SlotRegister(), Operand(slot));
4268 Handle<Code> stub = CodeFactory::StoreGlobalViaContext(
4269 isolate(), depth, instr->language_mode())
4271 CallCode(stub, RelocInfo::CODE_TARGET, instr);
4273 __ Push(Smi::FromInt(slot));
4274 __ Push(StoreGlobalViaContextDescriptor::ValueRegister());
4275 __ CallRuntime(is_strict(language_mode())
4276 ? Runtime::kStoreGlobalViaContext_Strict
4277 : Runtime::kStoreGlobalViaContext_Sloppy,
4283 void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
4284 Condition cc = instr->hydrogen()->allow_equality() ? hi : hs;
4285 Operand operand((int64_t)0);
4287 if (instr->index()->IsConstantOperand()) {
4288 operand = ToOperand(instr->index());
4289 reg = ToRegister(instr->length());
4290 cc = CommuteCondition(cc);
4292 reg = ToRegister(instr->index());
4293 operand = ToOperand(instr->length());
4295 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
4297 __ Branch(&done, NegateCondition(cc), reg, operand);
4298 __ stop("eliminated bounds check failed");
4301 DeoptimizeIf(cc, instr, Deoptimizer::kOutOfBounds, reg, operand);
4306 void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
4307 Register external_pointer = ToRegister(instr->elements());
4308 Register key = no_reg;
4309 ElementsKind elements_kind = instr->elements_kind();
4310 bool key_is_constant = instr->key()->IsConstantOperand();
4311 int constant_key = 0;
4312 if (key_is_constant) {
4313 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4314 if (constant_key & 0xF0000000) {
4315 Abort(kArrayIndexConstantValueTooBig);
4318 key = ToRegister(instr->key());
4320 int element_size_shift = ElementsKindToShiftSize(elements_kind);
4321 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4322 ? (element_size_shift - (kSmiTagSize + kSmiShiftSize))
4323 : element_size_shift;
4324 int base_offset = instr->base_offset();
4326 if (elements_kind == FLOAT32_ELEMENTS || elements_kind == FLOAT64_ELEMENTS) {
4327 Register address = scratch0();
4328 FPURegister value(ToDoubleRegister(instr->value()));
4329 if (key_is_constant) {
4330 if (constant_key != 0) {
4331 __ Daddu(address, external_pointer,
4332 Operand(constant_key << element_size_shift));
4334 address = external_pointer;
4337 if (shift_size < 0) {
4338 if (shift_size == -32) {
4339 __ dsra32(address, key, 0);
4341 __ dsra(address, key, -shift_size);
4344 __ dsll(address, key, shift_size);
4346 __ Daddu(address, external_pointer, address);
4349 if (elements_kind == FLOAT32_ELEMENTS) {
4350 __ cvt_s_d(double_scratch0(), value);
4351 __ swc1(double_scratch0(), MemOperand(address, base_offset));
4352 } else { // Storing doubles, not floats.
4353 __ sdc1(value, MemOperand(address, base_offset));
4356 Register value(ToRegister(instr->value()));
4357 MemOperand mem_operand = PrepareKeyedOperand(
4358 key, external_pointer, key_is_constant, constant_key,
4359 element_size_shift, shift_size,
4361 switch (elements_kind) {
4362 case UINT8_ELEMENTS:
4363 case UINT8_CLAMPED_ELEMENTS:
4365 __ sb(value, mem_operand);
4367 case INT16_ELEMENTS:
4368 case UINT16_ELEMENTS:
4369 __ sh(value, mem_operand);
4371 case INT32_ELEMENTS:
4372 case UINT32_ELEMENTS:
4373 __ sw(value, mem_operand);
4375 case FLOAT32_ELEMENTS:
4376 case FLOAT64_ELEMENTS:
4377 case FAST_DOUBLE_ELEMENTS:
4379 case FAST_SMI_ELEMENTS:
4380 case FAST_HOLEY_DOUBLE_ELEMENTS:
4381 case FAST_HOLEY_ELEMENTS:
4382 case FAST_HOLEY_SMI_ELEMENTS:
4383 case DICTIONARY_ELEMENTS:
4384 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
4385 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
4393 void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
4394 DoubleRegister value = ToDoubleRegister(instr->value());
4395 Register elements = ToRegister(instr->elements());
4396 Register scratch = scratch0();
4397 DoubleRegister double_scratch = double_scratch0();
4398 bool key_is_constant = instr->key()->IsConstantOperand();
4399 int base_offset = instr->base_offset();
4400 Label not_nan, done;
4402 // Calculate the effective address of the slot in the array to store the
4404 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
4405 if (key_is_constant) {
4406 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4407 if (constant_key & 0xF0000000) {
4408 Abort(kArrayIndexConstantValueTooBig);
4410 __ Daddu(scratch, elements,
4411 Operand((constant_key << element_size_shift) + base_offset));
4413 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4414 ? (element_size_shift - (kSmiTagSize + kSmiShiftSize))
4415 : element_size_shift;
4416 __ Daddu(scratch, elements, Operand(base_offset));
4417 DCHECK((shift_size == 3) || (shift_size == -29));
4418 if (shift_size == 3) {
4419 __ dsll(at, ToRegister(instr->key()), 3);
4420 } else if (shift_size == -29) {
4421 __ dsra(at, ToRegister(instr->key()), 29);
4423 __ Daddu(scratch, scratch, at);
4426 if (instr->NeedsCanonicalization()) {
4427 __ FPUCanonicalizeNaN(double_scratch, value);
4428 __ sdc1(double_scratch, MemOperand(scratch, 0));
4430 __ sdc1(value, MemOperand(scratch, 0));
4435 void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4436 Register value = ToRegister(instr->value());
4437 Register elements = ToRegister(instr->elements());
4438 Register key = instr->key()->IsRegister() ? ToRegister(instr->key())
4440 Register scratch = scratch0();
4441 Register store_base = scratch;
4442 int offset = instr->base_offset();
4445 if (instr->key()->IsConstantOperand()) {
4446 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4447 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
4448 offset += ToInteger32(const_operand) * kPointerSize;
4449 store_base = elements;
4451 // Even though the HLoadKeyed instruction forces the input
4452 // representation for the key to be an integer, the input gets replaced
4453 // during bound check elimination with the index argument to the bounds
4454 // check, which can be tagged, so that case must be handled here, too.
4455 if (instr->hydrogen()->key()->representation().IsSmi()) {
4456 __ SmiScale(scratch, key, kPointerSizeLog2);
4457 __ daddu(store_base, elements, scratch);
4459 __ dsll(scratch, key, kPointerSizeLog2);
4460 __ daddu(store_base, elements, scratch);
4464 Representation representation = instr->hydrogen()->value()->representation();
4465 if (representation.IsInteger32() && SmiValuesAre32Bits()) {
4466 DCHECK(instr->hydrogen()->store_mode() == STORE_TO_INITIALIZED_ENTRY);
4467 DCHECK(instr->hydrogen()->elements_kind() == FAST_SMI_ELEMENTS);
4468 if (FLAG_debug_code) {
4469 Register temp = scratch1();
4470 __ Load(temp, MemOperand(store_base, offset), Representation::Smi());
4474 // Store int value directly to upper half of the smi.
4475 STATIC_ASSERT(kSmiTag == 0);
4476 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 32);
4477 offset += kPointerSize / 2;
4478 representation = Representation::Integer32();
4481 __ Store(value, MemOperand(store_base, offset), representation);
4483 if (instr->hydrogen()->NeedsWriteBarrier()) {
4484 SmiCheck check_needed =
4485 instr->hydrogen()->value()->type().IsHeapObject()
4486 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4487 // Compute address of modified element and store it into key register.
4488 __ Daddu(key, store_base, Operand(offset));
4489 __ RecordWrite(elements,
4494 EMIT_REMEMBERED_SET,
4496 instr->hydrogen()->PointersToHereCheckForValue());
4501 void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4502 // By cases: external, fast double
4503 if (instr->is_fixed_typed_array()) {
4504 DoStoreKeyedExternalArray(instr);
4505 } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4506 DoStoreKeyedFixedDoubleArray(instr);
4508 DoStoreKeyedFixedArray(instr);
4513 void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4514 DCHECK(ToRegister(instr->context()).is(cp));
4515 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4516 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4517 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4519 if (instr->hydrogen()->HasVectorAndSlot()) {
4520 EmitVectorStoreICRegisters<LStoreKeyedGeneric>(instr);
4523 Handle<Code> ic = CodeFactory::KeyedStoreICInOptimizedCode(
4524 isolate(), instr->language_mode(),
4525 instr->hydrogen()->initialization_state()).code();
4526 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4530 void LCodeGen::DoMaybeGrowElements(LMaybeGrowElements* instr) {
4531 class DeferredMaybeGrowElements final : public LDeferredCode {
4533 DeferredMaybeGrowElements(LCodeGen* codegen, LMaybeGrowElements* instr)
4534 : LDeferredCode(codegen), instr_(instr) {}
4535 void Generate() override { codegen()->DoDeferredMaybeGrowElements(instr_); }
4536 LInstruction* instr() override { return instr_; }
4539 LMaybeGrowElements* instr_;
4542 Register result = v0;
4543 DeferredMaybeGrowElements* deferred =
4544 new (zone()) DeferredMaybeGrowElements(this, instr);
4545 LOperand* key = instr->key();
4546 LOperand* current_capacity = instr->current_capacity();
4548 DCHECK(instr->hydrogen()->key()->representation().IsInteger32());
4549 DCHECK(instr->hydrogen()->current_capacity()->representation().IsInteger32());
4550 DCHECK(key->IsConstantOperand() || key->IsRegister());
4551 DCHECK(current_capacity->IsConstantOperand() ||
4552 current_capacity->IsRegister());
4554 if (key->IsConstantOperand() && current_capacity->IsConstantOperand()) {
4555 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4556 int32_t constant_capacity =
4557 ToInteger32(LConstantOperand::cast(current_capacity));
4558 if (constant_key >= constant_capacity) {
4560 __ jmp(deferred->entry());
4562 } else if (key->IsConstantOperand()) {
4563 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4564 __ Branch(deferred->entry(), le, ToRegister(current_capacity),
4565 Operand(constant_key));
4566 } else if (current_capacity->IsConstantOperand()) {
4567 int32_t constant_capacity =
4568 ToInteger32(LConstantOperand::cast(current_capacity));
4569 __ Branch(deferred->entry(), ge, ToRegister(key),
4570 Operand(constant_capacity));
4572 __ Branch(deferred->entry(), ge, ToRegister(key),
4573 Operand(ToRegister(current_capacity)));
4576 if (instr->elements()->IsRegister()) {
4577 __ mov(result, ToRegister(instr->elements()));
4579 __ ld(result, ToMemOperand(instr->elements()));
4582 __ bind(deferred->exit());
4586 void LCodeGen::DoDeferredMaybeGrowElements(LMaybeGrowElements* instr) {
4587 // TODO(3095996): Get rid of this. For now, we need to make the
4588 // result register contain a valid pointer because it is already
4589 // contained in the register pointer map.
4590 Register result = v0;
4591 __ mov(result, zero_reg);
4593 // We have to call a stub.
4595 PushSafepointRegistersScope scope(this);
4596 if (instr->object()->IsRegister()) {
4597 __ mov(result, ToRegister(instr->object()));
4599 __ ld(result, ToMemOperand(instr->object()));
4602 LOperand* key = instr->key();
4603 if (key->IsConstantOperand()) {
4604 __ li(a3, Operand(ToSmi(LConstantOperand::cast(key))));
4606 __ mov(a3, ToRegister(key));
4610 GrowArrayElementsStub stub(isolate(), instr->hydrogen()->is_js_array(),
4611 instr->hydrogen()->kind());
4614 RecordSafepointWithLazyDeopt(
4615 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
4616 __ StoreToSafepointRegisterSlot(result, result);
4619 // Deopt on smi, which means the elements array changed to dictionary mode.
4620 __ SmiTst(result, at);
4621 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
4625 void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4626 Register object_reg = ToRegister(instr->object());
4627 Register scratch = scratch0();
4629 Handle<Map> from_map = instr->original_map();
4630 Handle<Map> to_map = instr->transitioned_map();
4631 ElementsKind from_kind = instr->from_kind();
4632 ElementsKind to_kind = instr->to_kind();
4634 Label not_applicable;
4635 __ ld(scratch, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4636 __ Branch(¬_applicable, ne, scratch, Operand(from_map));
4638 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
4639 Register new_map_reg = ToRegister(instr->new_map_temp());
4640 __ li(new_map_reg, Operand(to_map));
4641 __ sd(new_map_reg, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4643 __ RecordWriteForMap(object_reg,
4649 DCHECK(object_reg.is(a0));
4650 DCHECK(ToRegister(instr->context()).is(cp));
4651 PushSafepointRegistersScope scope(this);
4652 __ li(a1, Operand(to_map));
4653 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4654 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4656 RecordSafepointWithRegisters(
4657 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
4659 __ bind(¬_applicable);
4663 void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4664 Register object = ToRegister(instr->object());
4665 Register temp = ToRegister(instr->temp());
4666 Label no_memento_found;
4667 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found,
4668 ne, &no_memento_found);
4669 DeoptimizeIf(al, instr, Deoptimizer::kMementoFound);
4670 __ bind(&no_memento_found);
4674 void LCodeGen::DoStringAdd(LStringAdd* instr) {
4675 DCHECK(ToRegister(instr->context()).is(cp));
4676 DCHECK(ToRegister(instr->left()).is(a1));
4677 DCHECK(ToRegister(instr->right()).is(a0));
4678 StringAddStub stub(isolate(),
4679 instr->hydrogen()->flags(),
4680 instr->hydrogen()->pretenure_flag());
4681 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4685 void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
4686 class DeferredStringCharCodeAt final : public LDeferredCode {
4688 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
4689 : LDeferredCode(codegen), instr_(instr) { }
4690 void Generate() override { codegen()->DoDeferredStringCharCodeAt(instr_); }
4691 LInstruction* instr() override { return instr_; }
4694 LStringCharCodeAt* instr_;
4697 DeferredStringCharCodeAt* deferred =
4698 new(zone()) DeferredStringCharCodeAt(this, instr);
4699 StringCharLoadGenerator::Generate(masm(),
4700 ToRegister(instr->string()),
4701 ToRegister(instr->index()),
4702 ToRegister(instr->result()),
4704 __ bind(deferred->exit());
4708 void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4709 Register string = ToRegister(instr->string());
4710 Register result = ToRegister(instr->result());
4711 Register scratch = scratch0();
4713 // TODO(3095996): Get rid of this. For now, we need to make the
4714 // result register contain a valid pointer because it is already
4715 // contained in the register pointer map.
4716 __ mov(result, zero_reg);
4718 PushSafepointRegistersScope scope(this);
4720 // Push the index as a smi. This is safe because of the checks in
4721 // DoStringCharCodeAt above.
4722 if (instr->index()->IsConstantOperand()) {
4723 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
4724 __ Daddu(scratch, zero_reg, Operand(Smi::FromInt(const_index)));
4727 Register index = ToRegister(instr->index());
4731 CallRuntimeFromDeferred(Runtime::kStringCharCodeAtRT, 2, instr,
4735 __ StoreToSafepointRegisterSlot(v0, result);
4739 void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
4740 class DeferredStringCharFromCode final : public LDeferredCode {
4742 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
4743 : LDeferredCode(codegen), instr_(instr) { }
4744 void Generate() override {
4745 codegen()->DoDeferredStringCharFromCode(instr_);
4747 LInstruction* instr() override { return instr_; }
4750 LStringCharFromCode* instr_;
4753 DeferredStringCharFromCode* deferred =
4754 new(zone()) DeferredStringCharFromCode(this, instr);
4756 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
4757 Register char_code = ToRegister(instr->char_code());
4758 Register result = ToRegister(instr->result());
4759 Register scratch = scratch0();
4760 DCHECK(!char_code.is(result));
4762 __ Branch(deferred->entry(), hi,
4763 char_code, Operand(String::kMaxOneByteCharCode));
4764 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
4765 __ dsll(scratch, char_code, kPointerSizeLog2);
4766 __ Daddu(result, result, scratch);
4767 __ ld(result, FieldMemOperand(result, FixedArray::kHeaderSize));
4768 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
4769 __ Branch(deferred->entry(), eq, result, Operand(scratch));
4770 __ bind(deferred->exit());
4774 void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4775 Register char_code = ToRegister(instr->char_code());
4776 Register result = ToRegister(instr->result());
4778 // TODO(3095996): Get rid of this. For now, we need to make the
4779 // result register contain a valid pointer because it is already
4780 // contained in the register pointer map.
4781 __ mov(result, zero_reg);
4783 PushSafepointRegistersScope scope(this);
4784 __ SmiTag(char_code);
4786 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr, instr->context());
4787 __ StoreToSafepointRegisterSlot(v0, result);
4791 void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
4792 LOperand* input = instr->value();
4793 DCHECK(input->IsRegister() || input->IsStackSlot());
4794 LOperand* output = instr->result();
4795 DCHECK(output->IsDoubleRegister());
4796 FPURegister single_scratch = double_scratch0().low();
4797 if (input->IsStackSlot()) {
4798 Register scratch = scratch0();
4799 __ ld(scratch, ToMemOperand(input));
4800 __ mtc1(scratch, single_scratch);
4802 __ mtc1(ToRegister(input), single_scratch);
4804 __ cvt_d_w(ToDoubleRegister(output), single_scratch);
4808 void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4809 LOperand* input = instr->value();
4810 LOperand* output = instr->result();
4812 FPURegister dbl_scratch = double_scratch0();
4813 __ mtc1(ToRegister(input), dbl_scratch);
4814 __ Cvt_d_uw(ToDoubleRegister(output), dbl_scratch, f22); // TODO(plind): f22?
4818 void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4819 class DeferredNumberTagU final : public LDeferredCode {
4821 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4822 : LDeferredCode(codegen), instr_(instr) { }
4823 void Generate() override {
4824 codegen()->DoDeferredNumberTagIU(instr_,
4830 LInstruction* instr() override { return instr_; }
4833 LNumberTagU* instr_;
4836 Register input = ToRegister(instr->value());
4837 Register result = ToRegister(instr->result());
4839 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4840 __ Branch(deferred->entry(), hi, input, Operand(Smi::kMaxValue));
4841 __ SmiTag(result, input);
4842 __ bind(deferred->exit());
4846 void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4850 IntegerSignedness signedness) {
4852 Register src = ToRegister(value);
4853 Register dst = ToRegister(instr->result());
4854 Register tmp1 = scratch0();
4855 Register tmp2 = ToRegister(temp1);
4856 Register tmp3 = ToRegister(temp2);
4857 DoubleRegister dbl_scratch = double_scratch0();
4859 if (signedness == SIGNED_INT32) {
4860 // There was overflow, so bits 30 and 31 of the original integer
4861 // disagree. Try to allocate a heap number in new space and store
4862 // the value in there. If that fails, call the runtime system.
4864 __ SmiUntag(src, dst);
4865 __ Xor(src, src, Operand(0x80000000));
4867 __ mtc1(src, dbl_scratch);
4868 __ cvt_d_w(dbl_scratch, dbl_scratch);
4870 __ mtc1(src, dbl_scratch);
4871 __ Cvt_d_uw(dbl_scratch, dbl_scratch, f22);
4874 if (FLAG_inline_new) {
4875 __ LoadRoot(tmp3, Heap::kHeapNumberMapRootIndex);
4876 __ AllocateHeapNumber(dst, tmp1, tmp2, tmp3, &slow, TAG_RESULT);
4880 // Slow case: Call the runtime system to do the number allocation.
4883 // TODO(3095996): Put a valid pointer value in the stack slot where the
4884 // result register is stored, as this register is in the pointer map, but
4885 // contains an integer value.
4886 __ mov(dst, zero_reg);
4887 // Preserve the value of all registers.
4888 PushSafepointRegistersScope scope(this);
4890 // NumberTagI and NumberTagD use the context from the frame, rather than
4891 // the environment's HContext or HInlinedContext value.
4892 // They only call Runtime::kAllocateHeapNumber.
4893 // The corresponding HChange instructions are added in a phase that does
4894 // not have easy access to the local context.
4895 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4896 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4897 RecordSafepointWithRegisters(
4898 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4899 __ StoreToSafepointRegisterSlot(v0, dst);
4902 // Done. Put the value in dbl_scratch into the value of the allocated heap
4905 __ sdc1(dbl_scratch, FieldMemOperand(dst, HeapNumber::kValueOffset));
4909 void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
4910 class DeferredNumberTagD final : public LDeferredCode {
4912 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4913 : LDeferredCode(codegen), instr_(instr) { }
4914 void Generate() override { codegen()->DoDeferredNumberTagD(instr_); }
4915 LInstruction* instr() override { return instr_; }
4918 LNumberTagD* instr_;
4921 DoubleRegister input_reg = ToDoubleRegister(instr->value());
4922 Register scratch = scratch0();
4923 Register reg = ToRegister(instr->result());
4924 Register temp1 = ToRegister(instr->temp());
4925 Register temp2 = ToRegister(instr->temp2());
4927 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
4928 if (FLAG_inline_new) {
4929 __ LoadRoot(scratch, Heap::kHeapNumberMapRootIndex);
4930 // We want the untagged address first for performance
4931 __ AllocateHeapNumber(reg, temp1, temp2, scratch, deferred->entry(),
4934 __ Branch(deferred->entry());
4936 __ bind(deferred->exit());
4937 __ sdc1(input_reg, MemOperand(reg, HeapNumber::kValueOffset));
4938 // Now that we have finished with the object's real address tag it
4939 __ Daddu(reg, reg, kHeapObjectTag);
4943 void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4944 // TODO(3095996): Get rid of this. For now, we need to make the
4945 // result register contain a valid pointer because it is already
4946 // contained in the register pointer map.
4947 Register reg = ToRegister(instr->result());
4948 __ mov(reg, zero_reg);
4950 PushSafepointRegistersScope scope(this);
4951 // NumberTagI and NumberTagD use the context from the frame, rather than
4952 // the environment's HContext or HInlinedContext value.
4953 // They only call Runtime::kAllocateHeapNumber.
4954 // The corresponding HChange instructions are added in a phase that does
4955 // not have easy access to the local context.
4956 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4957 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4958 RecordSafepointWithRegisters(
4959 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4960 __ Dsubu(v0, v0, kHeapObjectTag);
4961 __ StoreToSafepointRegisterSlot(v0, reg);
4965 void LCodeGen::DoSmiTag(LSmiTag* instr) {
4966 HChange* hchange = instr->hydrogen();
4967 Register input = ToRegister(instr->value());
4968 Register output = ToRegister(instr->result());
4969 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4970 hchange->value()->CheckFlag(HValue::kUint32)) {
4971 __ And(at, input, Operand(0x80000000));
4972 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow, at, Operand(zero_reg));
4974 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4975 !hchange->value()->CheckFlag(HValue::kUint32)) {
4976 __ SmiTagCheckOverflow(output, input, at);
4977 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, at, Operand(zero_reg));
4979 __ SmiTag(output, input);
4984 void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4985 Register scratch = scratch0();
4986 Register input = ToRegister(instr->value());
4987 Register result = ToRegister(instr->result());
4988 if (instr->needs_check()) {
4989 STATIC_ASSERT(kHeapObjectTag == 1);
4990 // If the input is a HeapObject, value of scratch won't be zero.
4991 __ And(scratch, input, Operand(kHeapObjectTag));
4992 __ SmiUntag(result, input);
4993 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi, scratch, Operand(zero_reg));
4995 __ SmiUntag(result, input);
5000 void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
5001 DoubleRegister result_reg,
5002 NumberUntagDMode mode) {
5003 bool can_convert_undefined_to_nan =
5004 instr->hydrogen()->can_convert_undefined_to_nan();
5005 bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
5007 Register scratch = scratch0();
5008 Label convert, load_smi, done;
5009 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
5011 __ UntagAndJumpIfSmi(scratch, input_reg, &load_smi);
5012 // Heap number map check.
5013 __ ld(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
5014 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5015 if (can_convert_undefined_to_nan) {
5016 __ Branch(&convert, ne, scratch, Operand(at));
5018 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, scratch,
5021 // Load heap number.
5022 __ ldc1(result_reg, FieldMemOperand(input_reg, HeapNumber::kValueOffset));
5023 if (deoptimize_on_minus_zero) {
5024 __ mfc1(at, result_reg);
5025 __ Branch(&done, ne, at, Operand(zero_reg));
5026 __ mfhc1(scratch, result_reg); // Get exponent/sign bits.
5027 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, scratch,
5028 Operand(HeapNumber::kSignMask));
5031 if (can_convert_undefined_to_nan) {
5033 // Convert undefined (and hole) to NaN.
5034 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5035 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefined, input_reg,
5037 __ LoadRoot(scratch, Heap::kNanValueRootIndex);
5038 __ ldc1(result_reg, FieldMemOperand(scratch, HeapNumber::kValueOffset));
5042 __ SmiUntag(scratch, input_reg);
5043 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
5045 // Smi to double register conversion
5047 // scratch: untagged value of input_reg
5048 __ mtc1(scratch, result_reg);
5049 __ cvt_d_w(result_reg, result_reg);
5054 void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) {
5055 Register input_reg = ToRegister(instr->value());
5056 Register scratch1 = scratch0();
5057 Register scratch2 = ToRegister(instr->temp());
5058 DoubleRegister double_scratch = double_scratch0();
5059 DoubleRegister double_scratch2 = ToDoubleRegister(instr->temp2());
5061 DCHECK(!scratch1.is(input_reg) && !scratch1.is(scratch2));
5062 DCHECK(!scratch2.is(input_reg) && !scratch2.is(scratch1));
5066 // The input is a tagged HeapObject.
5067 // Heap number map check.
5068 __ ld(scratch1, FieldMemOperand(input_reg, HeapObject::kMapOffset));
5069 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5070 // This 'at' value and scratch1 map value are used for tests in both clauses
5073 if (instr->truncating()) {
5074 // Performs a truncating conversion of a floating point number as used by
5075 // the JS bitwise operations.
5076 Label no_heap_number, check_bools, check_false;
5077 // Check HeapNumber map.
5078 __ Branch(USE_DELAY_SLOT, &no_heap_number, ne, scratch1, Operand(at));
5079 __ mov(scratch2, input_reg); // In delay slot.
5080 __ TruncateHeapNumberToI(input_reg, scratch2);
5083 // Check for Oddballs. Undefined/False is converted to zero and True to one
5084 // for truncating conversions.
5085 __ bind(&no_heap_number);
5086 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5087 __ Branch(&check_bools, ne, input_reg, Operand(at));
5088 DCHECK(ToRegister(instr->result()).is(input_reg));
5089 __ Branch(USE_DELAY_SLOT, &done);
5090 __ mov(input_reg, zero_reg); // In delay slot.
5092 __ bind(&check_bools);
5093 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5094 __ Branch(&check_false, ne, scratch2, Operand(at));
5095 __ Branch(USE_DELAY_SLOT, &done);
5096 __ li(input_reg, Operand(1)); // In delay slot.
5098 __ bind(&check_false);
5099 __ LoadRoot(at, Heap::kFalseValueRootIndex);
5100 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefinedBoolean,
5101 scratch2, Operand(at));
5102 __ Branch(USE_DELAY_SLOT, &done);
5103 __ mov(input_reg, zero_reg); // In delay slot.
5105 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, scratch1,
5108 // Load the double value.
5109 __ ldc1(double_scratch,
5110 FieldMemOperand(input_reg, HeapNumber::kValueOffset));
5112 Register except_flag = scratch2;
5113 __ EmitFPUTruncate(kRoundToZero,
5119 kCheckForInexactConversion);
5121 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
5124 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5125 __ Branch(&done, ne, input_reg, Operand(zero_reg));
5127 __ mfhc1(scratch1, double_scratch); // Get exponent/sign bits.
5128 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
5129 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
5137 void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
5138 class DeferredTaggedToI final : public LDeferredCode {
5140 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
5141 : LDeferredCode(codegen), instr_(instr) { }
5142 void Generate() override { codegen()->DoDeferredTaggedToI(instr_); }
5143 LInstruction* instr() override { return instr_; }
5149 LOperand* input = instr->value();
5150 DCHECK(input->IsRegister());
5151 DCHECK(input->Equals(instr->result()));
5153 Register input_reg = ToRegister(input);
5155 if (instr->hydrogen()->value()->representation().IsSmi()) {
5156 __ SmiUntag(input_reg);
5158 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
5160 // Let the deferred code handle the HeapObject case.
5161 __ JumpIfNotSmi(input_reg, deferred->entry());
5163 // Smi to int32 conversion.
5164 __ SmiUntag(input_reg);
5165 __ bind(deferred->exit());
5170 void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
5171 LOperand* input = instr->value();
5172 DCHECK(input->IsRegister());
5173 LOperand* result = instr->result();
5174 DCHECK(result->IsDoubleRegister());
5176 Register input_reg = ToRegister(input);
5177 DoubleRegister result_reg = ToDoubleRegister(result);
5179 HValue* value = instr->hydrogen()->value();
5180 NumberUntagDMode mode = value->representation().IsSmi()
5181 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
5183 EmitNumberUntagD(instr, input_reg, result_reg, mode);
5187 void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
5188 Register result_reg = ToRegister(instr->result());
5189 Register scratch1 = scratch0();
5190 DoubleRegister double_input = ToDoubleRegister(instr->value());
5192 if (instr->truncating()) {
5193 __ TruncateDoubleToI(result_reg, double_input);
5195 Register except_flag = LCodeGen::scratch1();
5197 __ EmitFPUTruncate(kRoundToMinusInf,
5203 kCheckForInexactConversion);
5205 // Deopt if the operation did not succeed (except_flag != 0).
5206 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
5209 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5211 __ Branch(&done, ne, result_reg, Operand(zero_reg));
5212 __ mfhc1(scratch1, double_input); // Get exponent/sign bits.
5213 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
5214 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
5222 void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
5223 Register result_reg = ToRegister(instr->result());
5224 Register scratch1 = LCodeGen::scratch0();
5225 DoubleRegister double_input = ToDoubleRegister(instr->value());
5227 if (instr->truncating()) {
5228 __ TruncateDoubleToI(result_reg, double_input);
5230 Register except_flag = LCodeGen::scratch1();
5232 __ EmitFPUTruncate(kRoundToMinusInf,
5238 kCheckForInexactConversion);
5240 // Deopt if the operation did not succeed (except_flag != 0).
5241 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
5244 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5246 __ Branch(&done, ne, result_reg, Operand(zero_reg));
5247 __ mfhc1(scratch1, double_input); // Get exponent/sign bits.
5248 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
5249 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
5254 __ SmiTag(result_reg, result_reg);
5258 void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
5259 LOperand* input = instr->value();
5260 __ SmiTst(ToRegister(input), at);
5261 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi, at, Operand(zero_reg));
5265 void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
5266 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
5267 LOperand* input = instr->value();
5268 __ SmiTst(ToRegister(input), at);
5269 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
5274 void LCodeGen::DoCheckArrayBufferNotNeutered(
5275 LCheckArrayBufferNotNeutered* instr) {
5276 Register view = ToRegister(instr->view());
5277 Register scratch = scratch0();
5279 __ ld(scratch, FieldMemOperand(view, JSArrayBufferView::kBufferOffset));
5280 __ lw(scratch, FieldMemOperand(scratch, JSArrayBuffer::kBitFieldOffset));
5281 __ And(at, scratch, 1 << JSArrayBuffer::WasNeutered::kShift);
5282 DeoptimizeIf(ne, instr, Deoptimizer::kOutOfBounds, at, Operand(zero_reg));
5286 void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
5287 Register input = ToRegister(instr->value());
5288 Register scratch = scratch0();
5290 __ GetObjectType(input, scratch, scratch);
5292 if (instr->hydrogen()->is_interval_check()) {
5295 instr->hydrogen()->GetCheckInterval(&first, &last);
5297 // If there is only one type in the interval check for equality.
5298 if (first == last) {
5299 DeoptimizeIf(ne, instr, Deoptimizer::kWrongInstanceType, scratch,
5302 DeoptimizeIf(lo, instr, Deoptimizer::kWrongInstanceType, scratch,
5304 // Omit check for the last type.
5305 if (last != LAST_TYPE) {
5306 DeoptimizeIf(hi, instr, Deoptimizer::kWrongInstanceType, scratch,
5313 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
5315 if (base::bits::IsPowerOfTwo32(mask)) {
5316 DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
5317 __ And(at, scratch, mask);
5318 DeoptimizeIf(tag == 0 ? ne : eq, instr, Deoptimizer::kWrongInstanceType,
5319 at, Operand(zero_reg));
5321 __ And(scratch, scratch, Operand(mask));
5322 DeoptimizeIf(ne, instr, Deoptimizer::kWrongInstanceType, scratch,
5329 void LCodeGen::DoCheckValue(LCheckValue* instr) {
5330 Register reg = ToRegister(instr->value());
5331 Handle<HeapObject> object = instr->hydrogen()->object().handle();
5332 AllowDeferredHandleDereference smi_check;
5333 if (isolate()->heap()->InNewSpace(*object)) {
5334 Register reg = ToRegister(instr->value());
5335 Handle<Cell> cell = isolate()->factory()->NewCell(object);
5336 __ li(at, Operand(cell));
5337 __ ld(at, FieldMemOperand(at, Cell::kValueOffset));
5338 DeoptimizeIf(ne, instr, Deoptimizer::kValueMismatch, reg, Operand(at));
5340 DeoptimizeIf(ne, instr, Deoptimizer::kValueMismatch, reg, Operand(object));
5345 void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
5347 PushSafepointRegistersScope scope(this);
5349 __ mov(cp, zero_reg);
5350 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
5351 RecordSafepointWithRegisters(
5352 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
5353 __ StoreToSafepointRegisterSlot(v0, scratch0());
5355 __ SmiTst(scratch0(), at);
5356 DeoptimizeIf(eq, instr, Deoptimizer::kInstanceMigrationFailed, at,
5361 void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
5362 class DeferredCheckMaps final : public LDeferredCode {
5364 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
5365 : LDeferredCode(codegen), instr_(instr), object_(object) {
5366 SetExit(check_maps());
5368 void Generate() override {
5369 codegen()->DoDeferredInstanceMigration(instr_, object_);
5371 Label* check_maps() { return &check_maps_; }
5372 LInstruction* instr() override { return instr_; }
5380 if (instr->hydrogen()->IsStabilityCheck()) {
5381 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5382 for (int i = 0; i < maps->size(); ++i) {
5383 AddStabilityDependency(maps->at(i).handle());
5388 Register map_reg = scratch0();
5389 LOperand* input = instr->value();
5390 DCHECK(input->IsRegister());
5391 Register reg = ToRegister(input);
5392 __ ld(map_reg, FieldMemOperand(reg, HeapObject::kMapOffset));
5394 DeferredCheckMaps* deferred = NULL;
5395 if (instr->hydrogen()->HasMigrationTarget()) {
5396 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5397 __ bind(deferred->check_maps());
5400 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5402 for (int i = 0; i < maps->size() - 1; i++) {
5403 Handle<Map> map = maps->at(i).handle();
5404 __ CompareMapAndBranch(map_reg, map, &success, eq, &success);
5406 Handle<Map> map = maps->at(maps->size() - 1).handle();
5407 // Do the CompareMap() directly within the Branch() and DeoptimizeIf().
5408 if (instr->hydrogen()->HasMigrationTarget()) {
5409 __ Branch(deferred->entry(), ne, map_reg, Operand(map));
5411 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap, map_reg, Operand(map));
5418 void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
5419 DoubleRegister value_reg = ToDoubleRegister(instr->unclamped());
5420 Register result_reg = ToRegister(instr->result());
5421 DoubleRegister temp_reg = ToDoubleRegister(instr->temp());
5422 __ ClampDoubleToUint8(result_reg, value_reg, temp_reg);
5426 void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
5427 Register unclamped_reg = ToRegister(instr->unclamped());
5428 Register result_reg = ToRegister(instr->result());
5429 __ ClampUint8(result_reg, unclamped_reg);
5433 void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
5434 Register scratch = scratch0();
5435 Register input_reg = ToRegister(instr->unclamped());
5436 Register result_reg = ToRegister(instr->result());
5437 DoubleRegister temp_reg = ToDoubleRegister(instr->temp());
5438 Label is_smi, done, heap_number;
5440 // Both smi and heap number cases are handled.
5441 __ UntagAndJumpIfSmi(scratch, input_reg, &is_smi);
5443 // Check for heap number
5444 __ ld(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
5445 __ Branch(&heap_number, eq, scratch, Operand(factory()->heap_number_map()));
5447 // Check for undefined. Undefined is converted to zero for clamping
5449 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefined, input_reg,
5450 Operand(factory()->undefined_value()));
5451 __ mov(result_reg, zero_reg);
5455 __ bind(&heap_number);
5456 __ ldc1(double_scratch0(), FieldMemOperand(input_reg,
5457 HeapNumber::kValueOffset));
5458 __ ClampDoubleToUint8(result_reg, double_scratch0(), temp_reg);
5462 __ ClampUint8(result_reg, scratch);
5468 void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5469 DoubleRegister value_reg = ToDoubleRegister(instr->value());
5470 Register result_reg = ToRegister(instr->result());
5471 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5472 __ FmoveHigh(result_reg, value_reg);
5474 __ FmoveLow(result_reg, value_reg);
5479 void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5480 Register hi_reg = ToRegister(instr->hi());
5481 Register lo_reg = ToRegister(instr->lo());
5482 DoubleRegister result_reg = ToDoubleRegister(instr->result());
5483 __ Move(result_reg, lo_reg, hi_reg);
5487 void LCodeGen::DoAllocate(LAllocate* instr) {
5488 class DeferredAllocate final : public LDeferredCode {
5490 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
5491 : LDeferredCode(codegen), instr_(instr) { }
5492 void Generate() override { codegen()->DoDeferredAllocate(instr_); }
5493 LInstruction* instr() override { return instr_; }
5499 DeferredAllocate* deferred =
5500 new(zone()) DeferredAllocate(this, instr);
5502 Register result = ToRegister(instr->result());
5503 Register scratch = ToRegister(instr->temp1());
5504 Register scratch2 = ToRegister(instr->temp2());
5506 // Allocate memory for the object.
5507 AllocationFlags flags = TAG_OBJECT;
5508 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5509 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5511 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5512 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5513 flags = static_cast<AllocationFlags>(flags | PRETENURE);
5515 if (instr->size()->IsConstantOperand()) {
5516 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5517 if (size <= Page::kMaxRegularHeapObjectSize) {
5518 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5520 __ jmp(deferred->entry());
5523 Register size = ToRegister(instr->size());
5524 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5527 __ bind(deferred->exit());
5529 if (instr->hydrogen()->MustPrefillWithFiller()) {
5530 STATIC_ASSERT(kHeapObjectTag == 1);
5531 if (instr->size()->IsConstantOperand()) {
5532 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5533 __ li(scratch, Operand(size - kHeapObjectTag));
5535 __ Dsubu(scratch, ToRegister(instr->size()), Operand(kHeapObjectTag));
5537 __ li(scratch2, Operand(isolate()->factory()->one_pointer_filler_map()));
5540 __ Dsubu(scratch, scratch, Operand(kPointerSize));
5541 __ Daddu(at, result, Operand(scratch));
5542 __ sd(scratch2, MemOperand(at));
5543 __ Branch(&loop, ge, scratch, Operand(zero_reg));
5548 void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
5549 Register result = ToRegister(instr->result());
5551 // TODO(3095996): Get rid of this. For now, we need to make the
5552 // result register contain a valid pointer because it is already
5553 // contained in the register pointer map.
5554 __ mov(result, zero_reg);
5556 PushSafepointRegistersScope scope(this);
5557 if (instr->size()->IsRegister()) {
5558 Register size = ToRegister(instr->size());
5559 DCHECK(!size.is(result));
5563 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5564 if (size >= 0 && size <= Smi::kMaxValue) {
5565 __ li(v0, Operand(Smi::FromInt(size)));
5568 // We should never get here at runtime => abort
5569 __ stop("invalid allocation size");
5574 int flags = AllocateDoubleAlignFlag::encode(
5575 instr->hydrogen()->MustAllocateDoubleAligned());
5576 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5577 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5578 flags = AllocateTargetSpace::update(flags, OLD_SPACE);
5580 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5582 __ li(v0, Operand(Smi::FromInt(flags)));
5585 CallRuntimeFromDeferred(
5586 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
5587 __ StoreToSafepointRegisterSlot(v0, result);
5591 void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
5592 DCHECK(ToRegister(instr->value()).is(a0));
5593 DCHECK(ToRegister(instr->result()).is(v0));
5595 CallRuntime(Runtime::kToFastProperties, 1, instr);
5599 void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
5600 DCHECK(ToRegister(instr->context()).is(cp));
5602 // Registers will be used as follows:
5603 // a7 = literals array.
5604 // a1 = regexp literal.
5605 // a0 = regexp literal clone.
5606 // a2 and a4-a6 are used as temporaries.
5607 int literal_offset =
5608 FixedArray::OffsetOfElementAt(instr->hydrogen()->literal_index());
5609 __ li(a7, instr->hydrogen()->literals());
5610 __ ld(a1, FieldMemOperand(a7, literal_offset));
5611 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5612 __ Branch(&materialized, ne, a1, Operand(at));
5614 // Create regexp literal using runtime function
5615 // Result will be in v0.
5616 __ li(a6, Operand(Smi::FromInt(instr->hydrogen()->literal_index())));
5617 __ li(a5, Operand(instr->hydrogen()->pattern()));
5618 __ li(a4, Operand(instr->hydrogen()->flags()));
5619 __ Push(a7, a6, a5, a4);
5620 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
5623 __ bind(&materialized);
5624 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
5625 Label allocated, runtime_allocate;
5627 __ Allocate(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
5630 __ bind(&runtime_allocate);
5631 __ li(a0, Operand(Smi::FromInt(size)));
5633 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
5636 __ bind(&allocated);
5637 // Copy the content into the newly allocated memory.
5638 // (Unroll copy loop once for better throughput).
5639 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
5640 __ ld(a3, FieldMemOperand(a1, i));
5641 __ ld(a2, FieldMemOperand(a1, i + kPointerSize));
5642 __ sd(a3, FieldMemOperand(v0, i));
5643 __ sd(a2, FieldMemOperand(v0, i + kPointerSize));
5645 if ((size % (2 * kPointerSize)) != 0) {
5646 __ ld(a3, FieldMemOperand(a1, size - kPointerSize));
5647 __ sd(a3, FieldMemOperand(v0, size - kPointerSize));
5652 void LCodeGen::DoTypeof(LTypeof* instr) {
5653 DCHECK(ToRegister(instr->value()).is(a3));
5654 DCHECK(ToRegister(instr->result()).is(v0));
5656 Register value_register = ToRegister(instr->value());
5657 __ JumpIfNotSmi(value_register, &do_call);
5658 __ li(v0, Operand(isolate()->factory()->number_string()));
5661 TypeofStub stub(isolate());
5662 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5667 void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5668 Register input = ToRegister(instr->value());
5670 Register cmp1 = no_reg;
5671 Operand cmp2 = Operand(no_reg);
5673 Condition final_branch_condition = EmitTypeofIs(instr->TrueLabel(chunk_),
5674 instr->FalseLabel(chunk_),
5676 instr->type_literal(),
5680 DCHECK(cmp1.is_valid());
5681 DCHECK(!cmp2.is_reg() || cmp2.rm().is_valid());
5683 if (final_branch_condition != kNoCondition) {
5684 EmitBranch(instr, final_branch_condition, cmp1, cmp2);
5689 Condition LCodeGen::EmitTypeofIs(Label* true_label,
5692 Handle<String> type_name,
5695 // This function utilizes the delay slot heavily. This is used to load
5696 // values that are always usable without depending on the type of the input
5698 Condition final_branch_condition = kNoCondition;
5699 Register scratch = scratch0();
5700 Factory* factory = isolate()->factory();
5701 if (String::Equals(type_name, factory->number_string())) {
5702 __ JumpIfSmi(input, true_label);
5703 __ ld(input, FieldMemOperand(input, HeapObject::kMapOffset));
5704 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5706 *cmp2 = Operand(at);
5707 final_branch_condition = eq;
5709 } else if (String::Equals(type_name, factory->string_string())) {
5710 __ JumpIfSmi(input, false_label);
5711 __ GetObjectType(input, input, scratch);
5713 *cmp2 = Operand(FIRST_NONSTRING_TYPE);
5714 final_branch_condition = lt;
5716 } else if (String::Equals(type_name, factory->symbol_string())) {
5717 __ JumpIfSmi(input, false_label);
5718 __ GetObjectType(input, input, scratch);
5720 *cmp2 = Operand(SYMBOL_TYPE);
5721 final_branch_condition = eq;
5723 } else if (String::Equals(type_name, factory->boolean_string())) {
5724 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5725 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5726 __ LoadRoot(at, Heap::kFalseValueRootIndex);
5728 *cmp2 = Operand(input);
5729 final_branch_condition = eq;
5731 } else if (String::Equals(type_name, factory->undefined_string())) {
5732 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5733 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5734 // The first instruction of JumpIfSmi is an And - it is safe in the delay
5736 __ JumpIfSmi(input, false_label);
5737 // Check for undetectable objects => true.
5738 __ ld(input, FieldMemOperand(input, HeapObject::kMapOffset));
5739 __ lbu(at, FieldMemOperand(input, Map::kBitFieldOffset));
5740 __ And(at, at, 1 << Map::kIsUndetectable);
5742 *cmp2 = Operand(zero_reg);
5743 final_branch_condition = ne;
5745 } else if (String::Equals(type_name, factory->function_string())) {
5746 __ JumpIfSmi(input, false_label);
5747 __ ld(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
5748 __ lbu(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5749 __ And(scratch, scratch,
5750 Operand((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable)));
5752 *cmp2 = Operand(1 << Map::kIsCallable);
5753 final_branch_condition = eq;
5755 } else if (String::Equals(type_name, factory->object_string())) {
5756 __ JumpIfSmi(input, false_label);
5757 __ LoadRoot(at, Heap::kNullValueRootIndex);
5758 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5759 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
5760 __ GetObjectType(input, scratch, scratch1());
5761 __ Branch(false_label, lt, scratch1(), Operand(FIRST_SPEC_OBJECT_TYPE));
5762 // Check for callable or undetectable objects => false.
5763 __ lbu(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5765 Operand((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable)));
5767 *cmp2 = Operand(zero_reg);
5768 final_branch_condition = eq;
5771 #define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
5772 } else if (String::Equals(type_name, factory->type##_string())) { \
5773 __ JumpIfSmi(input, false_label); \
5774 __ ld(input, FieldMemOperand(input, HeapObject::kMapOffset)); \
5775 __ LoadRoot(at, Heap::k##Type##MapRootIndex); \
5777 *cmp2 = Operand(at); \
5778 final_branch_condition = eq;
5779 SIMD128_TYPES(SIMD128_TYPE)
5786 *cmp2 = Operand(zero_reg); // Set to valid regs, to avoid caller assertion.
5787 __ Branch(false_label);
5790 return final_branch_condition;
5794 void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
5795 Register temp1 = ToRegister(instr->temp());
5797 EmitIsConstructCall(temp1, scratch0());
5799 EmitBranch(instr, eq, temp1,
5800 Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
5804 void LCodeGen::EmitIsConstructCall(Register temp1, Register temp2) {
5805 DCHECK(!temp1.is(temp2));
5806 // Get the frame pointer for the calling frame.
5807 __ ld(temp1, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5809 // Skip the arguments adaptor frame if it exists.
5810 Label check_frame_marker;
5811 __ ld(temp2, MemOperand(temp1, StandardFrameConstants::kContextOffset));
5812 __ Branch(&check_frame_marker, ne, temp2,
5813 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5814 __ ld(temp1, MemOperand(temp1, StandardFrameConstants::kCallerFPOffset));
5816 // Check the marker in the calling frame.
5817 __ bind(&check_frame_marker);
5818 __ ld(temp1, MemOperand(temp1, StandardFrameConstants::kMarkerOffset));
5822 void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5823 if (info()->ShouldEnsureSpaceForLazyDeopt()) {
5824 // Ensure that we have enough space after the previous lazy-bailout
5825 // instruction for patching the code here.
5826 int current_pc = masm()->pc_offset();
5827 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5828 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5829 DCHECK_EQ(0, padding_size % Assembler::kInstrSize);
5830 while (padding_size > 0) {
5832 padding_size -= Assembler::kInstrSize;
5836 last_lazy_deopt_pc_ = masm()->pc_offset();
5840 void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
5841 last_lazy_deopt_pc_ = masm()->pc_offset();
5842 DCHECK(instr->HasEnvironment());
5843 LEnvironment* env = instr->environment();
5844 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5845 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5849 void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
5850 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5851 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5852 // needed return address), even though the implementation of LAZY and EAGER is
5853 // now identical. When LAZY is eventually completely folded into EAGER, remove
5854 // the special case below.
5855 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5856 type = Deoptimizer::LAZY;
5859 DeoptimizeIf(al, instr, instr->hydrogen()->reason(), type, zero_reg,
5864 void LCodeGen::DoDummy(LDummy* instr) {
5865 // Nothing to see here, move on!
5869 void LCodeGen::DoDummyUse(LDummyUse* instr) {
5870 // Nothing to see here, move on!
5874 void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
5875 PushSafepointRegistersScope scope(this);
5876 LoadContextFromDeferred(instr->context());
5877 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5878 RecordSafepointWithLazyDeopt(
5879 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
5880 DCHECK(instr->HasEnvironment());
5881 LEnvironment* env = instr->environment();
5882 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5886 void LCodeGen::DoStackCheck(LStackCheck* instr) {
5887 class DeferredStackCheck final : public LDeferredCode {
5889 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5890 : LDeferredCode(codegen), instr_(instr) { }
5891 void Generate() override { codegen()->DoDeferredStackCheck(instr_); }
5892 LInstruction* instr() override { return instr_; }
5895 LStackCheck* instr_;
5898 DCHECK(instr->HasEnvironment());
5899 LEnvironment* env = instr->environment();
5900 // There is no LLazyBailout instruction for stack-checks. We have to
5901 // prepare for lazy deoptimization explicitly here.
5902 if (instr->hydrogen()->is_function_entry()) {
5903 // Perform stack overflow check.
5905 __ LoadRoot(at, Heap::kStackLimitRootIndex);
5906 __ Branch(&done, hs, sp, Operand(at));
5907 DCHECK(instr->context()->IsRegister());
5908 DCHECK(ToRegister(instr->context()).is(cp));
5909 CallCode(isolate()->builtins()->StackCheck(),
5910 RelocInfo::CODE_TARGET,
5914 DCHECK(instr->hydrogen()->is_backwards_branch());
5915 // Perform stack overflow check if this goto needs it before jumping.
5916 DeferredStackCheck* deferred_stack_check =
5917 new(zone()) DeferredStackCheck(this, instr);
5918 __ LoadRoot(at, Heap::kStackLimitRootIndex);
5919 __ Branch(deferred_stack_check->entry(), lo, sp, Operand(at));
5920 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
5921 __ bind(instr->done_label());
5922 deferred_stack_check->SetExit(instr->done_label());
5923 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5924 // Don't record a deoptimization index for the safepoint here.
5925 // This will be done explicitly when emitting call and the safepoint in
5926 // the deferred code.
5931 void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
5932 // This is a pseudo-instruction that ensures that the environment here is
5933 // properly registered for deoptimization and records the assembler's PC
5935 LEnvironment* environment = instr->environment();
5937 // If the environment were already registered, we would have no way of
5938 // backpatching it with the spill slot operands.
5939 DCHECK(!environment->HasBeenRegistered());
5940 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
5942 GenerateOsrPrologue();
5946 void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
5947 Register result = ToRegister(instr->result());
5948 Register object = ToRegister(instr->object());
5950 __ And(at, object, kSmiTagMask);
5951 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
5953 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
5954 __ GetObjectType(object, a1, a1);
5955 DeoptimizeIf(le, instr, Deoptimizer::kNotAJavaScriptObject, a1,
5956 Operand(LAST_JS_PROXY_TYPE));
5958 Label use_cache, call_runtime;
5959 DCHECK(object.is(a0));
5960 Register null_value = a5;
5961 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
5962 __ CheckEnumCache(null_value, &call_runtime);
5964 __ ld(result, FieldMemOperand(object, HeapObject::kMapOffset));
5965 __ Branch(&use_cache);
5967 // Get the set of properties to enumerate.
5968 __ bind(&call_runtime);
5970 CallRuntime(Runtime::kGetPropertyNamesFast, 1, instr);
5972 __ ld(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
5973 DCHECK(result.is(v0));
5974 __ LoadRoot(at, Heap::kMetaMapRootIndex);
5975 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap, a1, Operand(at));
5976 __ bind(&use_cache);
5980 void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5981 Register map = ToRegister(instr->map());
5982 Register result = ToRegister(instr->result());
5983 Label load_cache, done;
5984 __ EnumLength(result, map);
5985 __ Branch(&load_cache, ne, result, Operand(Smi::FromInt(0)));
5986 __ li(result, Operand(isolate()->factory()->empty_fixed_array()));
5989 __ bind(&load_cache);
5990 __ LoadInstanceDescriptors(map, result);
5992 FieldMemOperand(result, DescriptorArray::kEnumCacheOffset));
5994 FieldMemOperand(result, FixedArray::SizeFor(instr->idx())));
5995 DeoptimizeIf(eq, instr, Deoptimizer::kNoCache, result, Operand(zero_reg));
6001 void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
6002 Register object = ToRegister(instr->value());
6003 Register map = ToRegister(instr->map());
6004 __ ld(scratch0(), FieldMemOperand(object, HeapObject::kMapOffset));
6005 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap, map, Operand(scratch0()));
6009 void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
6013 PushSafepointRegistersScope scope(this);
6014 __ Push(object, index);
6015 __ mov(cp, zero_reg);
6016 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
6017 RecordSafepointWithRegisters(
6018 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
6019 __ StoreToSafepointRegisterSlot(v0, result);
6023 void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
6024 class DeferredLoadMutableDouble final : public LDeferredCode {
6026 DeferredLoadMutableDouble(LCodeGen* codegen,
6027 LLoadFieldByIndex* instr,
6031 : LDeferredCode(codegen),
6037 void Generate() override {
6038 codegen()->DoDeferredLoadMutableDouble(instr_, result_, object_, index_);
6040 LInstruction* instr() override { return instr_; }
6043 LLoadFieldByIndex* instr_;
6049 Register object = ToRegister(instr->object());
6050 Register index = ToRegister(instr->index());
6051 Register result = ToRegister(instr->result());
6052 Register scratch = scratch0();
6054 DeferredLoadMutableDouble* deferred;
6055 deferred = new(zone()) DeferredLoadMutableDouble(
6056 this, instr, result, object, index);
6058 Label out_of_object, done;
6060 __ And(scratch, index, Operand(Smi::FromInt(1)));
6061 __ Branch(deferred->entry(), ne, scratch, Operand(zero_reg));
6062 __ dsra(index, index, 1);
6064 __ Branch(USE_DELAY_SLOT, &out_of_object, lt, index, Operand(zero_reg));
6065 __ SmiScale(scratch, index, kPointerSizeLog2); // In delay slot.
6066 __ Daddu(scratch, object, scratch);
6067 __ ld(result, FieldMemOperand(scratch, JSObject::kHeaderSize));
6071 __ bind(&out_of_object);
6072 __ ld(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
6073 // Index is equal to negated out of object property index plus 1.
6074 __ Dsubu(scratch, result, scratch);
6075 __ ld(result, FieldMemOperand(scratch,
6076 FixedArray::kHeaderSize - kPointerSize));
6077 __ bind(deferred->exit());
6082 void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
6083 Register context = ToRegister(instr->context());
6084 __ sd(context, MemOperand(fp, StandardFrameConstants::kContextOffset));
6088 void LCodeGen::DoAllocateBlockContext(LAllocateBlockContext* instr) {
6089 Handle<ScopeInfo> scope_info = instr->scope_info();
6090 __ li(at, scope_info);
6091 __ Push(at, ToRegister(instr->function()));
6092 CallRuntime(Runtime::kPushBlockContext, 2, instr);
6093 RecordSafepoint(Safepoint::kNoLazyDeopt);
6099 } // namespace internal