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.
7 #include "src/code-factory.h"
8 #include "src/code-stubs.h"
9 #include "src/cpu-profiler.h"
10 #include "src/hydrogen-osr.h"
11 #include "src/ic/ic.h"
12 #include "src/ic/stub-cache.h"
13 #include "src/mips64/lithium-codegen-mips64.h"
14 #include "src/mips64/lithium-gap-resolver-mips64.h"
20 class SafepointGenerator final : public CallWrapper {
22 SafepointGenerator(LCodeGen* codegen,
23 LPointerMap* pointers,
24 Safepoint::DeoptMode mode)
28 virtual ~SafepointGenerator() {}
30 void BeforeCall(int call_size) const override {}
32 void AfterCall() const override {
33 codegen_->RecordSafepoint(pointers_, deopt_mode_);
38 LPointerMap* pointers_;
39 Safepoint::DeoptMode deopt_mode_;
45 bool LCodeGen::GenerateCode() {
46 LPhase phase("Z_Code generation", chunk());
50 // Open a frame scope to indicate that there is a frame on the stack. The
51 // NONE indicates that the scope shouldn't actually generate code to set up
52 // the frame (that is done in GeneratePrologue).
53 FrameScope frame_scope(masm_, StackFrame::NONE);
55 return GeneratePrologue() && GenerateBody() && GenerateDeferredCode() &&
56 GenerateJumpTable() && GenerateSafepointTable();
60 void LCodeGen::FinishCode(Handle<Code> code) {
62 code->set_stack_slots(GetStackSlotCount());
63 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
64 PopulateDeoptimizationData(code);
68 void LCodeGen::SaveCallerDoubles() {
69 DCHECK(info()->saves_caller_doubles());
70 DCHECK(NeedsEagerFrame());
71 Comment(";;; Save clobbered callee double registers");
73 BitVector* doubles = chunk()->allocated_double_registers();
74 BitVector::Iterator save_iterator(doubles);
75 while (!save_iterator.Done()) {
76 __ sdc1(DoubleRegister::FromAllocationIndex(save_iterator.Current()),
77 MemOperand(sp, count * kDoubleSize));
78 save_iterator.Advance();
84 void LCodeGen::RestoreCallerDoubles() {
85 DCHECK(info()->saves_caller_doubles());
86 DCHECK(NeedsEagerFrame());
87 Comment(";;; Restore clobbered callee double registers");
88 BitVector* doubles = chunk()->allocated_double_registers();
89 BitVector::Iterator save_iterator(doubles);
91 while (!save_iterator.Done()) {
92 __ ldc1(DoubleRegister::FromAllocationIndex(save_iterator.Current()),
93 MemOperand(sp, count * kDoubleSize));
94 save_iterator.Advance();
100 bool LCodeGen::GeneratePrologue() {
101 DCHECK(is_generating());
103 if (info()->IsOptimizing()) {
104 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
107 if (strlen(FLAG_stop_at) > 0 &&
108 info_->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
113 // a1: Callee's JS function.
114 // cp: Callee's context.
115 // fp: Caller's frame pointer.
118 // Sloppy mode functions and builtins need to replace the receiver with the
119 // global proxy when called as functions (without an explicit receiver
121 if (is_sloppy(info()->language_mode()) && info()->MayUseThis() &&
122 !info()->is_native() && info()->scope()->has_this_declaration()) {
124 int receiver_offset = info_->scope()->num_parameters() * kPointerSize;
125 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
126 __ ld(a2, MemOperand(sp, receiver_offset));
127 __ Branch(&ok, ne, a2, Operand(at));
129 __ ld(a2, GlobalObjectOperand());
130 __ ld(a2, FieldMemOperand(a2, GlobalObject::kGlobalProxyOffset));
132 __ sd(a2, MemOperand(sp, receiver_offset));
138 info()->set_prologue_offset(masm_->pc_offset());
139 if (NeedsEagerFrame()) {
140 if (info()->IsStub()) {
143 __ Prologue(info()->IsCodePreAgingActive());
145 frame_is_built_ = true;
146 info_->AddNoFrameRange(0, masm_->pc_offset());
149 // Reserve space for the stack slots needed by the code.
150 int slots = GetStackSlotCount();
152 if (FLAG_debug_code) {
153 __ Dsubu(sp, sp, Operand(slots * kPointerSize));
155 __ Daddu(a0, sp, Operand(slots * kPointerSize));
156 __ li(a1, Operand(kSlotsZapValue));
159 __ Dsubu(a0, a0, Operand(kPointerSize));
160 __ sd(a1, MemOperand(a0, 2 * kPointerSize));
161 __ Branch(&loop, ne, a0, Operand(sp));
164 __ Dsubu(sp, sp, Operand(slots * kPointerSize));
168 if (info()->saves_caller_doubles()) {
172 // Possibly allocate a local context.
173 int heap_slots = info()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
174 if (heap_slots > 0) {
175 Comment(";;; Allocate local context");
176 bool need_write_barrier = true;
177 // Argument to NewContext is the function, which is in a1.
178 DCHECK(!info()->scope()->is_script_scope());
179 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
180 FastNewContextStub stub(isolate(), heap_slots);
182 // Result of FastNewContextStub is always in new space.
183 need_write_barrier = false;
186 __ CallRuntime(Runtime::kNewFunctionContext, 1);
188 RecordSafepoint(Safepoint::kNoLazyDeopt);
189 // Context is returned in both v0. It replaces the context passed to us.
190 // It's saved in the stack and kept live in cp.
192 __ sd(v0, MemOperand(fp, StandardFrameConstants::kContextOffset));
193 // Copy any necessary parameters into the context.
194 int num_parameters = scope()->num_parameters();
195 int first_parameter = scope()->has_this_declaration() ? -1 : 0;
196 for (int i = first_parameter; i < num_parameters; i++) {
197 Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
198 if (var->IsContextSlot()) {
199 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
200 (num_parameters - 1 - i) * kPointerSize;
201 // Load parameter from stack.
202 __ ld(a0, MemOperand(fp, parameter_offset));
203 // Store it in the context.
204 MemOperand target = ContextOperand(cp, var->index());
206 // Update the write barrier. This clobbers a3 and a0.
207 if (need_write_barrier) {
208 __ RecordWriteContextSlot(
209 cp, target.offset(), a0, a3, GetRAState(), kSaveFPRegs);
210 } else if (FLAG_debug_code) {
212 __ JumpIfInNewSpace(cp, a0, &done);
213 __ Abort(kExpectedNewSpaceObject);
218 Comment(";;; End allocate local context");
222 if (FLAG_trace && info()->IsOptimizing()) {
223 // We have not executed any compiled code yet, so cp still holds the
225 __ CallRuntime(Runtime::kTraceEnter, 0);
227 return !is_aborted();
231 void LCodeGen::GenerateOsrPrologue() {
232 // Generate the OSR entry prologue at the first unknown OSR value, or if there
233 // are none, at the OSR entrypoint instruction.
234 if (osr_pc_offset_ >= 0) return;
236 osr_pc_offset_ = masm()->pc_offset();
238 // Adjust the frame size, subsuming the unoptimized frame into the
240 int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
242 __ Dsubu(sp, sp, Operand(slots * kPointerSize));
246 void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
247 if (instr->IsCall()) {
248 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
250 if (!instr->IsLazyBailout() && !instr->IsGap()) {
251 safepoints_.BumpLastLazySafepointIndex();
256 bool LCodeGen::GenerateDeferredCode() {
257 DCHECK(is_generating());
258 if (deferred_.length() > 0) {
259 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
260 LDeferredCode* code = deferred_[i];
263 instructions_->at(code->instruction_index())->hydrogen_value();
264 RecordAndWritePosition(
265 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
267 Comment(";;; <@%d,#%d> "
268 "-------------------- Deferred %s --------------------",
269 code->instruction_index(),
270 code->instr()->hydrogen_value()->id(),
271 code->instr()->Mnemonic());
272 __ bind(code->entry());
273 if (NeedsDeferredFrame()) {
274 Comment(";;; Build frame");
275 DCHECK(!frame_is_built_);
276 DCHECK(info()->IsStub());
277 frame_is_built_ = true;
278 __ MultiPush(cp.bit() | fp.bit() | ra.bit());
279 __ li(scratch0(), Operand(Smi::FromInt(StackFrame::STUB)));
282 Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
283 Comment(";;; Deferred code");
286 if (NeedsDeferredFrame()) {
287 Comment(";;; Destroy frame");
288 DCHECK(frame_is_built_);
290 __ MultiPop(cp.bit() | fp.bit() | ra.bit());
291 frame_is_built_ = false;
293 __ jmp(code->exit());
296 // Deferred code is the last part of the instruction sequence. Mark
297 // the generated code as done unless we bailed out.
298 if (!is_aborted()) status_ = DONE;
299 return !is_aborted();
303 bool LCodeGen::GenerateJumpTable() {
304 if (jump_table_.length() > 0) {
305 Comment(";;; -------------------- Jump table --------------------");
306 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
307 Label table_start, call_deopt_entry;
309 __ bind(&table_start);
311 Address base = jump_table_[0]->address;
312 for (int i = 0; i < jump_table_.length(); i++) {
313 Deoptimizer::JumpTableEntry* table_entry = jump_table_[i];
314 __ bind(&table_entry->label);
315 Address entry = table_entry->address;
316 DeoptComment(table_entry->deopt_info);
318 // Second-level deopt table entries are contiguous and small, so instead
319 // of loading the full, absolute address of each one, load the base
320 // address and add an immediate offset.
321 if (is_int16(entry - base)) {
322 if (table_entry->needs_frame) {
323 DCHECK(!info()->saves_caller_doubles());
324 Comment(";;; call deopt with frame");
325 __ MultiPush(cp.bit() | fp.bit() | ra.bit());
326 __ BranchAndLink(&needs_frame, USE_DELAY_SLOT);
327 __ li(t9, Operand(entry - base));
329 __ BranchAndLink(&call_deopt_entry, USE_DELAY_SLOT);
330 __ li(t9, Operand(entry - base));
334 __ li(t9, Operand(entry - base));
335 if (table_entry->needs_frame) {
336 DCHECK(!info()->saves_caller_doubles());
337 Comment(";;; call deopt with frame");
338 __ MultiPush(cp.bit() | fp.bit() | ra.bit());
339 __ BranchAndLink(&needs_frame);
341 __ BranchAndLink(&call_deopt_entry);
344 info()->LogDeoptCallPosition(masm()->pc_offset(),
345 table_entry->deopt_info.inlining_id);
347 if (needs_frame.is_linked()) {
348 __ bind(&needs_frame);
349 // This variant of deopt can only be used with stubs. Since we don't
350 // have a function pointer to install in the stack frame that we're
351 // building, install a special marker there instead.
352 DCHECK(info()->IsStub());
353 __ li(at, Operand(Smi::FromInt(StackFrame::STUB)));
355 __ Daddu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
358 Comment(";;; call deopt");
359 __ bind(&call_deopt_entry);
361 if (info()->saves_caller_doubles()) {
362 DCHECK(info()->IsStub());
363 RestoreCallerDoubles();
367 Operand(reinterpret_cast<int64_t>(base), RelocInfo::RUNTIME_ENTRY));
368 __ Daddu(t9, t9, Operand(at));
371 // The deoptimization jump table is the last part of the instruction
372 // sequence. Mark the generated code as done unless we bailed out.
373 if (!is_aborted()) status_ = DONE;
374 return !is_aborted();
378 bool LCodeGen::GenerateSafepointTable() {
380 safepoints_.Emit(masm(), GetStackSlotCount());
381 return !is_aborted();
385 Register LCodeGen::ToRegister(int index) const {
386 return Register::FromAllocationIndex(index);
390 DoubleRegister LCodeGen::ToDoubleRegister(int index) const {
391 return DoubleRegister::FromAllocationIndex(index);
395 Register LCodeGen::ToRegister(LOperand* op) const {
396 DCHECK(op->IsRegister());
397 return ToRegister(op->index());
401 Register LCodeGen::EmitLoadRegister(LOperand* op, Register scratch) {
402 if (op->IsRegister()) {
403 return ToRegister(op->index());
404 } else if (op->IsConstantOperand()) {
405 LConstantOperand* const_op = LConstantOperand::cast(op);
406 HConstant* constant = chunk_->LookupConstant(const_op);
407 Handle<Object> literal = constant->handle(isolate());
408 Representation r = chunk_->LookupLiteralRepresentation(const_op);
409 if (r.IsInteger32()) {
410 AllowDeferredHandleDereference get_number;
411 DCHECK(literal->IsNumber());
412 __ li(scratch, Operand(static_cast<int32_t>(literal->Number())));
413 } else if (r.IsSmi()) {
414 DCHECK(constant->HasSmiValue());
415 __ li(scratch, Operand(Smi::FromInt(constant->Integer32Value())));
416 } else if (r.IsDouble()) {
417 Abort(kEmitLoadRegisterUnsupportedDoubleImmediate);
419 DCHECK(r.IsSmiOrTagged());
420 __ li(scratch, literal);
423 } else if (op->IsStackSlot()) {
424 __ ld(scratch, ToMemOperand(op));
432 DoubleRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
433 DCHECK(op->IsDoubleRegister());
434 return ToDoubleRegister(op->index());
438 DoubleRegister LCodeGen::EmitLoadDoubleRegister(LOperand* op,
439 FloatRegister flt_scratch,
440 DoubleRegister dbl_scratch) {
441 if (op->IsDoubleRegister()) {
442 return ToDoubleRegister(op->index());
443 } else if (op->IsConstantOperand()) {
444 LConstantOperand* const_op = LConstantOperand::cast(op);
445 HConstant* constant = chunk_->LookupConstant(const_op);
446 Handle<Object> literal = constant->handle(isolate());
447 Representation r = chunk_->LookupLiteralRepresentation(const_op);
448 if (r.IsInteger32()) {
449 DCHECK(literal->IsNumber());
450 __ li(at, Operand(static_cast<int32_t>(literal->Number())));
451 __ mtc1(at, flt_scratch);
452 __ cvt_d_w(dbl_scratch, flt_scratch);
454 } else if (r.IsDouble()) {
455 Abort(kUnsupportedDoubleImmediate);
456 } else if (r.IsTagged()) {
457 Abort(kUnsupportedTaggedImmediate);
459 } else if (op->IsStackSlot()) {
460 MemOperand mem_op = ToMemOperand(op);
461 __ ldc1(dbl_scratch, mem_op);
469 Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
470 HConstant* constant = chunk_->LookupConstant(op);
471 DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
472 return constant->handle(isolate());
476 bool LCodeGen::IsInteger32(LConstantOperand* op) const {
477 return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
481 bool LCodeGen::IsSmi(LConstantOperand* op) const {
482 return chunk_->LookupLiteralRepresentation(op).IsSmi();
486 int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
487 // return ToRepresentation(op, Representation::Integer32());
488 HConstant* constant = chunk_->LookupConstant(op);
489 return constant->Integer32Value();
493 int64_t LCodeGen::ToRepresentation_donotuse(LConstantOperand* op,
494 const Representation& r) const {
495 HConstant* constant = chunk_->LookupConstant(op);
496 int32_t value = constant->Integer32Value();
497 if (r.IsInteger32()) return value;
498 DCHECK(r.IsSmiOrTagged());
499 return reinterpret_cast<int64_t>(Smi::FromInt(value));
503 Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
504 HConstant* constant = chunk_->LookupConstant(op);
505 return Smi::FromInt(constant->Integer32Value());
509 double LCodeGen::ToDouble(LConstantOperand* op) const {
510 HConstant* constant = chunk_->LookupConstant(op);
511 DCHECK(constant->HasDoubleValue());
512 return constant->DoubleValue();
516 Operand LCodeGen::ToOperand(LOperand* op) {
517 if (op->IsConstantOperand()) {
518 LConstantOperand* const_op = LConstantOperand::cast(op);
519 HConstant* constant = chunk()->LookupConstant(const_op);
520 Representation r = chunk_->LookupLiteralRepresentation(const_op);
522 DCHECK(constant->HasSmiValue());
523 return Operand(Smi::FromInt(constant->Integer32Value()));
524 } else if (r.IsInteger32()) {
525 DCHECK(constant->HasInteger32Value());
526 return Operand(constant->Integer32Value());
527 } else if (r.IsDouble()) {
528 Abort(kToOperandUnsupportedDoubleImmediate);
530 DCHECK(r.IsTagged());
531 return Operand(constant->handle(isolate()));
532 } else if (op->IsRegister()) {
533 return Operand(ToRegister(op));
534 } else if (op->IsDoubleRegister()) {
535 Abort(kToOperandIsDoubleRegisterUnimplemented);
536 return Operand((int64_t)0);
538 // Stack slots not implemented, use ToMemOperand instead.
540 return Operand((int64_t)0);
544 static int ArgumentsOffsetWithoutFrame(int index) {
546 return -(index + 1) * kPointerSize;
550 MemOperand LCodeGen::ToMemOperand(LOperand* op) const {
551 DCHECK(!op->IsRegister());
552 DCHECK(!op->IsDoubleRegister());
553 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
554 if (NeedsEagerFrame()) {
555 return MemOperand(fp, StackSlotOffset(op->index()));
557 // Retrieve parameter without eager stack-frame relative to the
559 return MemOperand(sp, ArgumentsOffsetWithoutFrame(op->index()));
564 MemOperand LCodeGen::ToHighMemOperand(LOperand* op) const {
565 DCHECK(op->IsDoubleStackSlot());
566 if (NeedsEagerFrame()) {
567 // return MemOperand(fp, StackSlotOffset(op->index()) + kPointerSize);
568 return MemOperand(fp, StackSlotOffset(op->index()) + kIntSize);
570 // Retrieve parameter without eager stack-frame relative to the
572 // return MemOperand(
573 // sp, ArgumentsOffsetWithoutFrame(op->index()) + kPointerSize);
575 sp, ArgumentsOffsetWithoutFrame(op->index()) + kIntSize);
580 void LCodeGen::WriteTranslation(LEnvironment* environment,
581 Translation* translation) {
582 if (environment == NULL) return;
584 // The translation includes one command per value in the environment.
585 int translation_size = environment->translation_size();
587 WriteTranslation(environment->outer(), translation);
588 WriteTranslationFrame(environment, translation);
590 int object_index = 0;
591 int dematerialized_index = 0;
592 for (int i = 0; i < translation_size; ++i) {
593 LOperand* value = environment->values()->at(i);
595 environment, translation, value, environment->HasTaggedValueAt(i),
596 environment->HasUint32ValueAt(i), &object_index, &dematerialized_index);
601 void LCodeGen::AddToTranslation(LEnvironment* environment,
602 Translation* translation,
606 int* object_index_pointer,
607 int* dematerialized_index_pointer) {
608 if (op == LEnvironment::materialization_marker()) {
609 int object_index = (*object_index_pointer)++;
610 if (environment->ObjectIsDuplicateAt(object_index)) {
611 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
612 translation->DuplicateObject(dupe_of);
615 int object_length = environment->ObjectLengthAt(object_index);
616 if (environment->ObjectIsArgumentsAt(object_index)) {
617 translation->BeginArgumentsObject(object_length);
619 translation->BeginCapturedObject(object_length);
621 int dematerialized_index = *dematerialized_index_pointer;
622 int env_offset = environment->translation_size() + dematerialized_index;
623 *dematerialized_index_pointer += object_length;
624 for (int i = 0; i < object_length; ++i) {
625 LOperand* value = environment->values()->at(env_offset + i);
626 AddToTranslation(environment,
629 environment->HasTaggedValueAt(env_offset + i),
630 environment->HasUint32ValueAt(env_offset + i),
631 object_index_pointer,
632 dematerialized_index_pointer);
637 if (op->IsStackSlot()) {
639 translation->StoreStackSlot(op->index());
640 } else if (is_uint32) {
641 translation->StoreUint32StackSlot(op->index());
643 translation->StoreInt32StackSlot(op->index());
645 } else if (op->IsDoubleStackSlot()) {
646 translation->StoreDoubleStackSlot(op->index());
647 } else if (op->IsRegister()) {
648 Register reg = ToRegister(op);
650 translation->StoreRegister(reg);
651 } else if (is_uint32) {
652 translation->StoreUint32Register(reg);
654 translation->StoreInt32Register(reg);
656 } else if (op->IsDoubleRegister()) {
657 DoubleRegister reg = ToDoubleRegister(op);
658 translation->StoreDoubleRegister(reg);
659 } else if (op->IsConstantOperand()) {
660 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
661 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
662 translation->StoreLiteral(src_index);
669 void LCodeGen::CallCode(Handle<Code> code,
670 RelocInfo::Mode mode,
671 LInstruction* instr) {
672 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT);
676 void LCodeGen::CallCodeGeneric(Handle<Code> code,
677 RelocInfo::Mode mode,
679 SafepointMode safepoint_mode) {
680 DCHECK(instr != NULL);
682 RecordSafepointWithLazyDeopt(instr, safepoint_mode);
686 void LCodeGen::CallRuntime(const Runtime::Function* function,
689 SaveFPRegsMode save_doubles) {
690 DCHECK(instr != NULL);
692 __ CallRuntime(function, num_arguments, save_doubles);
694 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
698 void LCodeGen::LoadContextFromDeferred(LOperand* context) {
699 if (context->IsRegister()) {
700 __ Move(cp, ToRegister(context));
701 } else if (context->IsStackSlot()) {
702 __ ld(cp, ToMemOperand(context));
703 } else if (context->IsConstantOperand()) {
704 HConstant* constant =
705 chunk_->LookupConstant(LConstantOperand::cast(context));
706 __ li(cp, Handle<Object>::cast(constant->handle(isolate())));
713 void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
717 LoadContextFromDeferred(context);
718 __ CallRuntimeSaveDoubles(id);
719 RecordSafepointWithRegisters(
720 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
724 void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
725 Safepoint::DeoptMode mode) {
726 environment->set_has_been_used();
727 if (!environment->HasBeenRegistered()) {
728 // Physical stack frame layout:
729 // -x ............. -4 0 ..................................... y
730 // [incoming arguments] [spill slots] [pushed outgoing arguments]
732 // Layout of the environment:
733 // 0 ..................................................... size-1
734 // [parameters] [locals] [expression stack including arguments]
736 // Layout of the translation:
737 // 0 ........................................................ size - 1 + 4
738 // [expression stack including arguments] [locals] [4 words] [parameters]
739 // |>------------ translation_size ------------<|
742 int jsframe_count = 0;
743 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
745 if (e->frame_type() == JS_FUNCTION) {
749 Translation translation(&translations_, frame_count, jsframe_count, zone());
750 WriteTranslation(environment, &translation);
751 int deoptimization_index = deoptimizations_.length();
752 int pc_offset = masm()->pc_offset();
753 environment->Register(deoptimization_index,
755 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
756 deoptimizations_.Add(environment, zone());
761 void LCodeGen::DeoptimizeIf(Condition condition, LInstruction* instr,
762 Deoptimizer::DeoptReason deopt_reason,
763 Deoptimizer::BailoutType bailout_type,
764 Register src1, const Operand& src2) {
765 LEnvironment* environment = instr->environment();
766 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
767 DCHECK(environment->HasBeenRegistered());
768 int id = environment->deoptimization_index();
769 DCHECK(info()->IsOptimizing() || info()->IsStub());
771 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
773 Abort(kBailoutWasNotPrepared);
777 if (FLAG_deopt_every_n_times != 0 && !info()->IsStub()) {
778 Register scratch = scratch0();
779 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
781 __ Push(a1, scratch);
782 __ li(scratch, Operand(count));
783 __ lw(a1, MemOperand(scratch));
784 __ Subu(a1, a1, Operand(1));
785 __ Branch(&no_deopt, ne, a1, Operand(zero_reg));
786 __ li(a1, Operand(FLAG_deopt_every_n_times));
787 __ sw(a1, MemOperand(scratch));
790 __ Call(entry, RelocInfo::RUNTIME_ENTRY);
792 __ sw(a1, MemOperand(scratch));
796 if (info()->ShouldTrapOnDeopt()) {
798 if (condition != al) {
799 __ Branch(&skip, NegateCondition(condition), src1, src2);
801 __ stop("trap_on_deopt");
805 Deoptimizer::DeoptInfo deopt_info = MakeDeoptInfo(instr, deopt_reason);
807 DCHECK(info()->IsStub() || frame_is_built_);
808 // Go through jump table if we need to handle condition, build frame, or
809 // restore caller doubles.
810 if (condition == al && frame_is_built_ &&
811 !info()->saves_caller_doubles()) {
812 DeoptComment(deopt_info);
813 __ Call(entry, RelocInfo::RUNTIME_ENTRY, condition, src1, src2);
814 info()->LogDeoptCallPosition(masm()->pc_offset(), deopt_info.inlining_id);
816 Deoptimizer::JumpTableEntry* table_entry =
817 new (zone()) Deoptimizer::JumpTableEntry(
818 entry, deopt_info, bailout_type, !frame_is_built_);
819 // We often have several deopts to the same entry, reuse the last
820 // jump entry if this is the case.
821 if (FLAG_trace_deopt || isolate()->cpu_profiler()->is_profiling() ||
822 jump_table_.is_empty() ||
823 !table_entry->IsEquivalentTo(*jump_table_.last())) {
824 jump_table_.Add(table_entry, zone());
826 __ Branch(&jump_table_.last()->label, condition, src1, src2);
831 void LCodeGen::DeoptimizeIf(Condition condition, LInstruction* instr,
832 Deoptimizer::DeoptReason deopt_reason,
833 Register src1, const Operand& src2) {
834 Deoptimizer::BailoutType bailout_type = info()->IsStub()
836 : Deoptimizer::EAGER;
837 DeoptimizeIf(condition, instr, deopt_reason, bailout_type, src1, src2);
841 void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
842 int length = deoptimizations_.length();
843 if (length == 0) return;
844 Handle<DeoptimizationInputData> data =
845 DeoptimizationInputData::New(isolate(), length, TENURED);
847 Handle<ByteArray> translations =
848 translations_.CreateByteArray(isolate()->factory());
849 data->SetTranslationByteArray(*translations);
850 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
851 data->SetOptimizationId(Smi::FromInt(info_->optimization_id()));
852 if (info_->IsOptimizing()) {
853 // Reference to shared function info does not change between phases.
854 AllowDeferredHandleDereference allow_handle_dereference;
855 data->SetSharedFunctionInfo(*info_->shared_info());
857 data->SetSharedFunctionInfo(Smi::FromInt(0));
859 data->SetWeakCellCache(Smi::FromInt(0));
861 Handle<FixedArray> literals =
862 factory()->NewFixedArray(deoptimization_literals_.length(), TENURED);
863 { AllowDeferredHandleDereference copy_handles;
864 for (int i = 0; i < deoptimization_literals_.length(); i++) {
865 literals->set(i, *deoptimization_literals_[i]);
867 data->SetLiteralArray(*literals);
870 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id().ToInt()));
871 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
873 // Populate the deoptimization entries.
874 for (int i = 0; i < length; i++) {
875 LEnvironment* env = deoptimizations_[i];
876 data->SetAstId(i, env->ast_id());
877 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
878 data->SetArgumentsStackHeight(i,
879 Smi::FromInt(env->arguments_stack_height()));
880 data->SetPc(i, Smi::FromInt(env->pc_offset()));
882 code->set_deoptimization_data(*data);
886 void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
887 DCHECK_EQ(0, deoptimization_literals_.length());
888 for (auto function : chunk()->inlined_functions()) {
889 DefineDeoptimizationLiteral(function);
891 inlined_function_count_ = deoptimization_literals_.length();
895 void LCodeGen::RecordSafepointWithLazyDeopt(
896 LInstruction* instr, SafepointMode safepoint_mode) {
897 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
898 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
900 DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
901 RecordSafepointWithRegisters(
902 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
907 void LCodeGen::RecordSafepoint(
908 LPointerMap* pointers,
909 Safepoint::Kind kind,
911 Safepoint::DeoptMode deopt_mode) {
912 DCHECK(expected_safepoint_kind_ == kind);
914 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
915 Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
916 kind, arguments, deopt_mode);
917 for (int i = 0; i < operands->length(); i++) {
918 LOperand* pointer = operands->at(i);
919 if (pointer->IsStackSlot()) {
920 safepoint.DefinePointerSlot(pointer->index(), zone());
921 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
922 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
928 void LCodeGen::RecordSafepoint(LPointerMap* pointers,
929 Safepoint::DeoptMode deopt_mode) {
930 RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
934 void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
935 LPointerMap empty_pointers(zone());
936 RecordSafepoint(&empty_pointers, deopt_mode);
940 void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
942 Safepoint::DeoptMode deopt_mode) {
944 pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
948 void LCodeGen::RecordAndWritePosition(int position) {
949 if (position == RelocInfo::kNoPosition) return;
950 masm()->positions_recorder()->RecordPosition(position);
951 masm()->positions_recorder()->WriteRecordedPositions();
955 static const char* LabelType(LLabel* label) {
956 if (label->is_loop_header()) return " (loop header)";
957 if (label->is_osr_entry()) return " (OSR entry)";
962 void LCodeGen::DoLabel(LLabel* label) {
963 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
964 current_instruction_,
965 label->hydrogen_value()->id(),
968 __ bind(label->label());
969 current_block_ = label->block_id();
974 void LCodeGen::DoParallelMove(LParallelMove* move) {
975 resolver_.Resolve(move);
979 void LCodeGen::DoGap(LGap* gap) {
980 for (int i = LGap::FIRST_INNER_POSITION;
981 i <= LGap::LAST_INNER_POSITION;
983 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
984 LParallelMove* move = gap->GetParallelMove(inner_pos);
985 if (move != NULL) DoParallelMove(move);
990 void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
995 void LCodeGen::DoParameter(LParameter* instr) {
1000 void LCodeGen::DoCallStub(LCallStub* instr) {
1001 DCHECK(ToRegister(instr->context()).is(cp));
1002 DCHECK(ToRegister(instr->result()).is(v0));
1003 switch (instr->hydrogen()->major_key()) {
1004 case CodeStub::RegExpExec: {
1005 RegExpExecStub stub(isolate());
1006 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1009 case CodeStub::SubString: {
1010 SubStringStub stub(isolate());
1011 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1014 case CodeStub::StringCompare: {
1015 StringCompareStub stub(isolate());
1016 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1025 void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
1026 GenerateOsrPrologue();
1030 void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
1031 Register dividend = ToRegister(instr->dividend());
1032 int32_t divisor = instr->divisor();
1033 DCHECK(dividend.is(ToRegister(instr->result())));
1035 // Theoretically, a variation of the branch-free code for integer division by
1036 // a power of 2 (calculating the remainder via an additional multiplication
1037 // (which gets simplified to an 'and') and subtraction) should be faster, and
1038 // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
1039 // indicate that positive dividends are heavily favored, so the branching
1040 // version performs better.
1041 HMod* hmod = instr->hydrogen();
1042 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1043 Label dividend_is_not_negative, done;
1045 if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
1046 __ Branch(÷nd_is_not_negative, ge, dividend, Operand(zero_reg));
1047 // Note: The code below even works when right contains kMinInt.
1048 __ dsubu(dividend, zero_reg, dividend);
1049 __ And(dividend, dividend, Operand(mask));
1050 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1051 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, dividend,
1054 __ Branch(USE_DELAY_SLOT, &done);
1055 __ dsubu(dividend, zero_reg, dividend);
1058 __ bind(÷nd_is_not_negative);
1059 __ And(dividend, dividend, Operand(mask));
1064 void LCodeGen::DoModByConstI(LModByConstI* instr) {
1065 Register dividend = ToRegister(instr->dividend());
1066 int32_t divisor = instr->divisor();
1067 Register result = ToRegister(instr->result());
1068 DCHECK(!dividend.is(result));
1071 DeoptimizeIf(al, instr, Deoptimizer::kDivisionByZero);
1075 __ TruncatingDiv(result, dividend, Abs(divisor));
1076 __ Dmul(result, result, Operand(Abs(divisor)));
1077 __ Dsubu(result, dividend, Operand(result));
1079 // Check for negative zero.
1080 HMod* hmod = instr->hydrogen();
1081 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1082 Label remainder_not_zero;
1083 __ Branch(&remainder_not_zero, ne, result, Operand(zero_reg));
1084 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, dividend,
1086 __ bind(&remainder_not_zero);
1091 void LCodeGen::DoModI(LModI* instr) {
1092 HMod* hmod = instr->hydrogen();
1093 const Register left_reg = ToRegister(instr->left());
1094 const Register right_reg = ToRegister(instr->right());
1095 const Register result_reg = ToRegister(instr->result());
1097 // div runs in the background while we check for special cases.
1098 __ Dmod(result_reg, left_reg, right_reg);
1101 // Check for x % 0, we have to deopt in this case because we can't return a
1103 if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
1104 DeoptimizeIf(eq, instr, Deoptimizer::kDivisionByZero, right_reg,
1108 // Check for kMinInt % -1, div will return kMinInt, which is not what we
1109 // want. We have to deopt if we care about -0, because we can't return that.
1110 if (hmod->CheckFlag(HValue::kCanOverflow)) {
1111 Label no_overflow_possible;
1112 __ Branch(&no_overflow_possible, ne, left_reg, Operand(kMinInt));
1113 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1114 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, right_reg, Operand(-1));
1116 __ Branch(&no_overflow_possible, ne, right_reg, Operand(-1));
1117 __ Branch(USE_DELAY_SLOT, &done);
1118 __ mov(result_reg, zero_reg);
1120 __ bind(&no_overflow_possible);
1123 // If we care about -0, test if the dividend is <0 and the result is 0.
1124 __ Branch(&done, ge, left_reg, Operand(zero_reg));
1126 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1127 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, result_reg,
1134 void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
1135 Register dividend = ToRegister(instr->dividend());
1136 int32_t divisor = instr->divisor();
1137 Register result = ToRegister(instr->result());
1138 DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
1139 DCHECK(!result.is(dividend));
1141 // Check for (0 / -x) that will produce negative zero.
1142 HDiv* hdiv = instr->hydrogen();
1143 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1144 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, dividend,
1147 // Check for (kMinInt / -1).
1148 if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
1149 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow, dividend, Operand(kMinInt));
1151 // Deoptimize if remainder will not be 0.
1152 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1153 divisor != 1 && divisor != -1) {
1154 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1155 __ And(at, dividend, Operand(mask));
1156 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision, at, Operand(zero_reg));
1159 if (divisor == -1) { // Nice shortcut, not needed for correctness.
1160 __ Dsubu(result, zero_reg, dividend);
1163 uint16_t shift = WhichPowerOf2Abs(divisor);
1165 __ Move(result, dividend);
1166 } else if (shift == 1) {
1167 __ dsrl32(result, dividend, 31);
1168 __ Daddu(result, dividend, Operand(result));
1170 __ dsra32(result, dividend, 31);
1171 __ dsrl32(result, result, 32 - shift);
1172 __ Daddu(result, dividend, Operand(result));
1174 if (shift > 0) __ dsra(result, result, shift);
1175 if (divisor < 0) __ Dsubu(result, zero_reg, result);
1179 void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
1180 Register dividend = ToRegister(instr->dividend());
1181 int32_t divisor = instr->divisor();
1182 Register result = ToRegister(instr->result());
1183 DCHECK(!dividend.is(result));
1186 DeoptimizeIf(al, instr, Deoptimizer::kDivisionByZero);
1190 // Check for (0 / -x) that will produce negative zero.
1191 HDiv* hdiv = instr->hydrogen();
1192 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1193 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, dividend,
1197 __ TruncatingDiv(result, dividend, Abs(divisor));
1198 if (divisor < 0) __ Subu(result, zero_reg, result);
1200 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1201 __ Dmul(scratch0(), result, Operand(divisor));
1202 __ Dsubu(scratch0(), scratch0(), dividend);
1203 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision, scratch0(),
1209 // TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
1210 void LCodeGen::DoDivI(LDivI* instr) {
1211 HBinaryOperation* hdiv = instr->hydrogen();
1212 Register dividend = ToRegister(instr->dividend());
1213 Register divisor = ToRegister(instr->divisor());
1214 const Register result = ToRegister(instr->result());
1216 // On MIPS div is asynchronous - it will run in the background while we
1217 // check for special cases.
1218 __ Ddiv(result, dividend, divisor);
1221 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1222 DeoptimizeIf(eq, instr, Deoptimizer::kDivisionByZero, divisor,
1226 // Check for (0 / -x) that will produce negative zero.
1227 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1228 Label left_not_zero;
1229 __ Branch(&left_not_zero, ne, dividend, Operand(zero_reg));
1230 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, divisor,
1232 __ bind(&left_not_zero);
1235 // Check for (kMinInt / -1).
1236 if (hdiv->CheckFlag(HValue::kCanOverflow) &&
1237 !hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1238 Label left_not_min_int;
1239 __ Branch(&left_not_min_int, ne, dividend, Operand(kMinInt));
1240 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow, divisor, Operand(-1));
1241 __ bind(&left_not_min_int);
1244 if (!hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1245 // Calculate remainder.
1246 Register remainder = ToRegister(instr->temp());
1247 if (kArchVariant != kMips64r6) {
1250 __ dmod(remainder, dividend, divisor);
1252 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision, remainder,
1258 void LCodeGen::DoMultiplyAddD(LMultiplyAddD* instr) {
1259 DoubleRegister addend = ToDoubleRegister(instr->addend());
1260 DoubleRegister multiplier = ToDoubleRegister(instr->multiplier());
1261 DoubleRegister multiplicand = ToDoubleRegister(instr->multiplicand());
1263 // This is computed in-place.
1264 DCHECK(addend.is(ToDoubleRegister(instr->result())));
1266 __ Madd_d(addend, addend, multiplier, multiplicand, double_scratch0());
1270 void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
1271 Register dividend = ToRegister(instr->dividend());
1272 Register result = ToRegister(instr->result());
1273 int32_t divisor = instr->divisor();
1274 Register scratch = result.is(dividend) ? scratch0() : dividend;
1275 DCHECK(!result.is(dividend) || !scratch.is(dividend));
1277 // If the divisor is 1, return the dividend.
1279 __ Move(result, dividend);
1283 // If the divisor is positive, things are easy: There can be no deopts and we
1284 // can simply do an arithmetic right shift.
1285 uint16_t shift = WhichPowerOf2Abs(divisor);
1287 __ dsra(result, dividend, shift);
1291 // If the divisor is negative, we have to negate and handle edge cases.
1292 // Dividend can be the same register as result so save the value of it
1293 // for checking overflow.
1294 __ Move(scratch, dividend);
1296 __ Dsubu(result, zero_reg, dividend);
1297 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1298 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, result, Operand(zero_reg));
1301 __ Xor(scratch, scratch, result);
1302 // Dividing by -1 is basically negation, unless we overflow.
1303 if (divisor == -1) {
1304 if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1305 DeoptimizeIf(gt, instr, Deoptimizer::kOverflow, result, Operand(kMaxInt));
1310 // If the negation could not overflow, simply shifting is OK.
1311 if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1312 __ dsra(result, result, shift);
1316 Label no_overflow, done;
1317 __ Branch(&no_overflow, lt, scratch, Operand(zero_reg));
1318 __ li(result, Operand(kMinInt / divisor), CONSTANT_SIZE);
1320 __ bind(&no_overflow);
1321 __ dsra(result, result, shift);
1326 void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
1327 Register dividend = ToRegister(instr->dividend());
1328 int32_t divisor = instr->divisor();
1329 Register result = ToRegister(instr->result());
1330 DCHECK(!dividend.is(result));
1333 DeoptimizeIf(al, instr, Deoptimizer::kDivisionByZero);
1337 // Check for (0 / -x) that will produce negative zero.
1338 HMathFloorOfDiv* hdiv = instr->hydrogen();
1339 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1340 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, dividend,
1344 // Easy case: We need no dynamic check for the dividend and the flooring
1345 // division is the same as the truncating division.
1346 if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
1347 (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
1348 __ TruncatingDiv(result, dividend, Abs(divisor));
1349 if (divisor < 0) __ Dsubu(result, zero_reg, result);
1353 // In the general case we may need to adjust before and after the truncating
1354 // division to get a flooring division.
1355 Register temp = ToRegister(instr->temp());
1356 DCHECK(!temp.is(dividend) && !temp.is(result));
1357 Label needs_adjustment, done;
1358 __ Branch(&needs_adjustment, divisor > 0 ? lt : gt,
1359 dividend, Operand(zero_reg));
1360 __ TruncatingDiv(result, dividend, Abs(divisor));
1361 if (divisor < 0) __ Dsubu(result, zero_reg, result);
1363 __ bind(&needs_adjustment);
1364 __ Daddu(temp, dividend, Operand(divisor > 0 ? 1 : -1));
1365 __ TruncatingDiv(result, temp, Abs(divisor));
1366 if (divisor < 0) __ Dsubu(result, zero_reg, result);
1367 __ Dsubu(result, result, Operand(1));
1372 // TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
1373 void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
1374 HBinaryOperation* hdiv = instr->hydrogen();
1375 Register dividend = ToRegister(instr->dividend());
1376 Register divisor = ToRegister(instr->divisor());
1377 const Register result = ToRegister(instr->result());
1379 // On MIPS div is asynchronous - it will run in the background while we
1380 // check for special cases.
1381 __ Ddiv(result, dividend, divisor);
1384 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1385 DeoptimizeIf(eq, instr, Deoptimizer::kDivisionByZero, divisor,
1389 // Check for (0 / -x) that will produce negative zero.
1390 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1391 Label left_not_zero;
1392 __ Branch(&left_not_zero, ne, dividend, Operand(zero_reg));
1393 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, divisor,
1395 __ bind(&left_not_zero);
1398 // Check for (kMinInt / -1).
1399 if (hdiv->CheckFlag(HValue::kCanOverflow) &&
1400 !hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1401 Label left_not_min_int;
1402 __ Branch(&left_not_min_int, ne, dividend, Operand(kMinInt));
1403 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow, divisor, Operand(-1));
1404 __ bind(&left_not_min_int);
1407 // We performed a truncating division. Correct the result if necessary.
1409 Register remainder = scratch0();
1410 if (kArchVariant != kMips64r6) {
1413 __ dmod(remainder, dividend, divisor);
1415 __ Branch(&done, eq, remainder, Operand(zero_reg), USE_DELAY_SLOT);
1416 __ Xor(remainder, remainder, Operand(divisor));
1417 __ Branch(&done, ge, remainder, Operand(zero_reg));
1418 __ Dsubu(result, result, Operand(1));
1423 void LCodeGen::DoMulS(LMulS* instr) {
1424 Register scratch = scratch0();
1425 Register result = ToRegister(instr->result());
1426 // Note that result may alias left.
1427 Register left = ToRegister(instr->left());
1428 LOperand* right_op = instr->right();
1430 bool bailout_on_minus_zero =
1431 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
1432 bool overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1434 if (right_op->IsConstantOperand()) {
1435 int32_t constant = ToInteger32(LConstantOperand::cast(right_op));
1437 if (bailout_on_minus_zero && (constant < 0)) {
1438 // The case of a null constant will be handled separately.
1439 // If constant is negative and left is null, the result should be -0.
1440 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, left, Operand(zero_reg));
1446 __ DsubuAndCheckForOverflow(result, zero_reg, left, scratch);
1447 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, scratch,
1450 __ Dsubu(result, zero_reg, left);
1454 if (bailout_on_minus_zero) {
1455 // If left is strictly negative and the constant is null, the
1456 // result is -0. Deoptimize if required, otherwise return 0.
1457 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, left,
1460 __ mov(result, zero_reg);
1464 __ Move(result, left);
1467 // Multiplying by powers of two and powers of two plus or minus
1468 // one can be done faster with shifted operands.
1469 // For other constants we emit standard code.
1470 int32_t mask = constant >> 31;
1471 uint32_t constant_abs = (constant + mask) ^ mask;
1473 if (base::bits::IsPowerOfTwo32(constant_abs)) {
1474 int32_t shift = WhichPowerOf2(constant_abs);
1475 __ dsll(result, left, shift);
1476 // Correct the sign of the result if the constant is negative.
1477 if (constant < 0) __ Dsubu(result, zero_reg, result);
1478 } else if (base::bits::IsPowerOfTwo32(constant_abs - 1)) {
1479 int32_t shift = WhichPowerOf2(constant_abs - 1);
1480 __ dsll(scratch, left, shift);
1481 __ Daddu(result, scratch, left);
1482 // Correct the sign of the result if the constant is negative.
1483 if (constant < 0) __ Dsubu(result, zero_reg, result);
1484 } else if (base::bits::IsPowerOfTwo32(constant_abs + 1)) {
1485 int32_t shift = WhichPowerOf2(constant_abs + 1);
1486 __ dsll(scratch, left, shift);
1487 __ Dsubu(result, scratch, left);
1488 // Correct the sign of the result if the constant is negative.
1489 if (constant < 0) __ Dsubu(result, zero_reg, result);
1491 // Generate standard code.
1492 __ li(at, constant);
1493 __ Dmul(result, left, at);
1497 DCHECK(right_op->IsRegister());
1498 Register right = ToRegister(right_op);
1501 // hi:lo = left * right.
1502 __ Dmulh(result, left, right);
1503 __ dsra32(scratch, result, 0);
1504 __ sra(at, result, 31);
1506 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow, scratch, Operand(at));
1508 __ SmiUntag(result, left);
1509 __ dmul(result, result, right);
1512 if (bailout_on_minus_zero) {
1514 __ Xor(at, left, right);
1515 __ Branch(&done, ge, at, Operand(zero_reg));
1516 // Bail out if the result is minus zero.
1517 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, result,
1525 void LCodeGen::DoMulI(LMulI* instr) {
1526 Register scratch = scratch0();
1527 Register result = ToRegister(instr->result());
1528 // Note that result may alias left.
1529 Register left = ToRegister(instr->left());
1530 LOperand* right_op = instr->right();
1532 bool bailout_on_minus_zero =
1533 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
1534 bool overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1536 if (right_op->IsConstantOperand()) {
1537 int32_t constant = ToInteger32(LConstantOperand::cast(right_op));
1539 if (bailout_on_minus_zero && (constant < 0)) {
1540 // The case of a null constant will be handled separately.
1541 // If constant is negative and left is null, the result should be -0.
1542 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, left, Operand(zero_reg));
1548 __ SubuAndCheckForOverflow(result, zero_reg, left, scratch);
1549 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, scratch,
1552 __ Subu(result, zero_reg, left);
1556 if (bailout_on_minus_zero) {
1557 // If left is strictly negative and the constant is null, the
1558 // result is -0. Deoptimize if required, otherwise return 0.
1559 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, left,
1562 __ mov(result, zero_reg);
1566 __ Move(result, left);
1569 // Multiplying by powers of two and powers of two plus or minus
1570 // one can be done faster with shifted operands.
1571 // For other constants we emit standard code.
1572 int32_t mask = constant >> 31;
1573 uint32_t constant_abs = (constant + mask) ^ mask;
1575 if (base::bits::IsPowerOfTwo32(constant_abs)) {
1576 int32_t shift = WhichPowerOf2(constant_abs);
1577 __ sll(result, left, shift);
1578 // Correct the sign of the result if the constant is negative.
1579 if (constant < 0) __ Subu(result, zero_reg, result);
1580 } else if (base::bits::IsPowerOfTwo32(constant_abs - 1)) {
1581 int32_t shift = WhichPowerOf2(constant_abs - 1);
1582 __ sll(scratch, left, shift);
1583 __ addu(result, scratch, left);
1584 // Correct the sign of the result if the constant is negative.
1585 if (constant < 0) __ Subu(result, zero_reg, result);
1586 } else if (base::bits::IsPowerOfTwo32(constant_abs + 1)) {
1587 int32_t shift = WhichPowerOf2(constant_abs + 1);
1588 __ sll(scratch, left, shift);
1589 __ Subu(result, scratch, left);
1590 // Correct the sign of the result if the constant is negative.
1591 if (constant < 0) __ Subu(result, zero_reg, result);
1593 // Generate standard code.
1594 __ li(at, constant);
1595 __ Mul(result, left, at);
1600 DCHECK(right_op->IsRegister());
1601 Register right = ToRegister(right_op);
1604 // hi:lo = left * right.
1605 __ Dmul(result, left, right);
1606 __ dsra32(scratch, result, 0);
1607 __ sra(at, result, 31);
1609 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow, scratch, Operand(at));
1611 __ mul(result, left, right);
1614 if (bailout_on_minus_zero) {
1616 __ Xor(at, left, right);
1617 __ Branch(&done, ge, at, Operand(zero_reg));
1618 // Bail out if the result is minus zero.
1619 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, result,
1627 void LCodeGen::DoBitI(LBitI* instr) {
1628 LOperand* left_op = instr->left();
1629 LOperand* right_op = instr->right();
1630 DCHECK(left_op->IsRegister());
1631 Register left = ToRegister(left_op);
1632 Register result = ToRegister(instr->result());
1633 Operand right(no_reg);
1635 if (right_op->IsStackSlot()) {
1636 right = Operand(EmitLoadRegister(right_op, at));
1638 DCHECK(right_op->IsRegister() || right_op->IsConstantOperand());
1639 right = ToOperand(right_op);
1642 switch (instr->op()) {
1643 case Token::BIT_AND:
1644 __ And(result, left, right);
1647 __ Or(result, left, right);
1649 case Token::BIT_XOR:
1650 if (right_op->IsConstantOperand() && right.immediate() == int32_t(~0)) {
1651 __ Nor(result, zero_reg, left);
1653 __ Xor(result, left, right);
1663 void LCodeGen::DoShiftI(LShiftI* instr) {
1664 // Both 'left' and 'right' are "used at start" (see LCodeGen::DoShift), so
1665 // result may alias either of them.
1666 LOperand* right_op = instr->right();
1667 Register left = ToRegister(instr->left());
1668 Register result = ToRegister(instr->result());
1670 if (right_op->IsRegister()) {
1671 // No need to mask the right operand on MIPS, it is built into the variable
1672 // shift instructions.
1673 switch (instr->op()) {
1675 __ Ror(result, left, Operand(ToRegister(right_op)));
1678 __ srav(result, left, ToRegister(right_op));
1681 __ srlv(result, left, ToRegister(right_op));
1682 if (instr->can_deopt()) {
1683 // TODO(yy): (-1) >>> 0. anything else?
1684 DeoptimizeIf(lt, instr, Deoptimizer::kNegativeValue, result,
1686 DeoptimizeIf(gt, instr, Deoptimizer::kNegativeValue, result,
1691 __ sllv(result, left, ToRegister(right_op));
1698 // Mask the right_op operand.
1699 int value = ToInteger32(LConstantOperand::cast(right_op));
1700 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1701 switch (instr->op()) {
1703 if (shift_count != 0) {
1704 __ Ror(result, left, Operand(shift_count));
1706 __ Move(result, left);
1710 if (shift_count != 0) {
1711 __ sra(result, left, shift_count);
1713 __ Move(result, left);
1717 if (shift_count != 0) {
1718 __ srl(result, left, shift_count);
1720 if (instr->can_deopt()) {
1721 __ And(at, left, Operand(0x80000000));
1722 DeoptimizeIf(ne, instr, Deoptimizer::kNegativeValue, at,
1725 __ Move(result, left);
1729 if (shift_count != 0) {
1730 if (instr->hydrogen_value()->representation().IsSmi()) {
1731 __ dsll(result, left, shift_count);
1733 __ sll(result, left, shift_count);
1736 __ Move(result, left);
1747 void LCodeGen::DoSubS(LSubS* instr) {
1748 LOperand* left = instr->left();
1749 LOperand* right = instr->right();
1750 LOperand* result = instr->result();
1751 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1753 if (!can_overflow) {
1754 DCHECK(right->IsRegister() || right->IsConstantOperand());
1755 __ Dsubu(ToRegister(result), ToRegister(left), ToOperand(right));
1756 } else { // can_overflow.
1757 Register overflow = scratch0();
1758 Register scratch = scratch1();
1759 DCHECK(right->IsRegister() || right->IsConstantOperand());
1760 __ DsubuAndCheckForOverflow(ToRegister(result), ToRegister(left),
1761 ToOperand(right), overflow, scratch);
1762 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, overflow,
1768 void LCodeGen::DoSubI(LSubI* instr) {
1769 LOperand* left = instr->left();
1770 LOperand* right = instr->right();
1771 LOperand* result = instr->result();
1772 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1774 if (!can_overflow) {
1775 DCHECK(right->IsRegister() || right->IsConstantOperand());
1776 __ Subu(ToRegister(result), ToRegister(left), ToOperand(right));
1777 } else { // can_overflow.
1778 Register overflow = scratch0();
1779 Register scratch = scratch1();
1780 DCHECK(right->IsRegister() || right->IsConstantOperand());
1781 __ SubuAndCheckForOverflow(ToRegister(result), ToRegister(left),
1782 ToOperand(right), overflow, scratch);
1783 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, overflow,
1789 void LCodeGen::DoConstantI(LConstantI* instr) {
1790 __ li(ToRegister(instr->result()), Operand(instr->value()));
1794 void LCodeGen::DoConstantS(LConstantS* instr) {
1795 __ li(ToRegister(instr->result()), Operand(instr->value()));
1799 void LCodeGen::DoConstantD(LConstantD* instr) {
1800 DCHECK(instr->result()->IsDoubleRegister());
1801 DoubleRegister result = ToDoubleRegister(instr->result());
1802 double v = instr->value();
1807 void LCodeGen::DoConstantE(LConstantE* instr) {
1808 __ li(ToRegister(instr->result()), Operand(instr->value()));
1812 void LCodeGen::DoConstantT(LConstantT* instr) {
1813 Handle<Object> object = instr->value(isolate());
1814 AllowDeferredHandleDereference smi_check;
1815 __ li(ToRegister(instr->result()), object);
1819 void LCodeGen::DoMapEnumLength(LMapEnumLength* instr) {
1820 Register result = ToRegister(instr->result());
1821 Register map = ToRegister(instr->value());
1822 __ EnumLength(result, map);
1826 void LCodeGen::DoDateField(LDateField* instr) {
1827 Register object = ToRegister(instr->date());
1828 Register result = ToRegister(instr->result());
1829 Register scratch = ToRegister(instr->temp());
1830 Smi* index = instr->index();
1831 DCHECK(object.is(a0));
1832 DCHECK(result.is(v0));
1833 DCHECK(!scratch.is(scratch0()));
1834 DCHECK(!scratch.is(object));
1836 if (index->value() == 0) {
1837 __ ld(result, FieldMemOperand(object, JSDate::kValueOffset));
1839 Label runtime, done;
1840 if (index->value() < JSDate::kFirstUncachedField) {
1841 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
1842 __ li(scratch, Operand(stamp));
1843 __ ld(scratch, MemOperand(scratch));
1844 __ ld(scratch0(), FieldMemOperand(object, JSDate::kCacheStampOffset));
1845 __ Branch(&runtime, ne, scratch, Operand(scratch0()));
1846 __ ld(result, FieldMemOperand(object, JSDate::kValueOffset +
1847 kPointerSize * index->value()));
1851 __ PrepareCallCFunction(2, scratch);
1852 __ li(a1, Operand(index));
1853 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
1859 MemOperand LCodeGen::BuildSeqStringOperand(Register string,
1861 String::Encoding encoding) {
1862 if (index->IsConstantOperand()) {
1863 int offset = ToInteger32(LConstantOperand::cast(index));
1864 if (encoding == String::TWO_BYTE_ENCODING) {
1865 offset *= kUC16Size;
1867 STATIC_ASSERT(kCharSize == 1);
1868 return FieldMemOperand(string, SeqString::kHeaderSize + offset);
1870 Register scratch = scratch0();
1871 DCHECK(!scratch.is(string));
1872 DCHECK(!scratch.is(ToRegister(index)));
1873 if (encoding == String::ONE_BYTE_ENCODING) {
1874 __ Daddu(scratch, string, ToRegister(index));
1876 STATIC_ASSERT(kUC16Size == 2);
1877 __ dsll(scratch, ToRegister(index), 1);
1878 __ Daddu(scratch, string, scratch);
1880 return FieldMemOperand(scratch, SeqString::kHeaderSize);
1884 void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
1885 String::Encoding encoding = instr->hydrogen()->encoding();
1886 Register string = ToRegister(instr->string());
1887 Register result = ToRegister(instr->result());
1889 if (FLAG_debug_code) {
1890 Register scratch = scratch0();
1891 __ ld(scratch, FieldMemOperand(string, HeapObject::kMapOffset));
1892 __ lbu(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1894 __ And(scratch, scratch,
1895 Operand(kStringRepresentationMask | kStringEncodingMask));
1896 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1897 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1898 __ Dsubu(at, scratch, Operand(encoding == String::ONE_BYTE_ENCODING
1899 ? one_byte_seq_type : two_byte_seq_type));
1900 __ Check(eq, kUnexpectedStringType, at, Operand(zero_reg));
1903 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1904 if (encoding == String::ONE_BYTE_ENCODING) {
1905 __ lbu(result, operand);
1907 __ lhu(result, operand);
1912 void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
1913 String::Encoding encoding = instr->hydrogen()->encoding();
1914 Register string = ToRegister(instr->string());
1915 Register value = ToRegister(instr->value());
1917 if (FLAG_debug_code) {
1918 Register scratch = scratch0();
1919 Register index = ToRegister(instr->index());
1920 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1921 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1923 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
1924 ? one_byte_seq_type : two_byte_seq_type;
1925 __ EmitSeqStringSetCharCheck(string, index, value, scratch, encoding_mask);
1928 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1929 if (encoding == String::ONE_BYTE_ENCODING) {
1930 __ sb(value, operand);
1932 __ sh(value, operand);
1937 void LCodeGen::DoAddE(LAddE* instr) {
1938 LOperand* result = instr->result();
1939 LOperand* left = instr->left();
1940 LOperand* right = instr->right();
1942 DCHECK(!instr->hydrogen()->CheckFlag(HValue::kCanOverflow));
1943 DCHECK(right->IsRegister() || right->IsConstantOperand());
1944 __ Daddu(ToRegister(result), ToRegister(left), ToOperand(right));
1948 void LCodeGen::DoAddS(LAddS* instr) {
1949 LOperand* left = instr->left();
1950 LOperand* right = instr->right();
1951 LOperand* result = instr->result();
1952 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1954 if (!can_overflow) {
1955 DCHECK(right->IsRegister() || right->IsConstantOperand());
1956 __ Daddu(ToRegister(result), ToRegister(left), ToOperand(right));
1957 } else { // can_overflow.
1958 Register overflow = scratch0();
1959 Register scratch = scratch1();
1960 DCHECK(right->IsRegister() || right->IsConstantOperand());
1961 __ DadduAndCheckForOverflow(ToRegister(result), ToRegister(left),
1962 ToOperand(right), overflow, scratch);
1963 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, overflow,
1969 void LCodeGen::DoAddI(LAddI* instr) {
1970 LOperand* left = instr->left();
1971 LOperand* right = instr->right();
1972 LOperand* result = instr->result();
1973 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1975 if (!can_overflow) {
1976 DCHECK(right->IsRegister() || right->IsConstantOperand());
1977 __ Addu(ToRegister(result), ToRegister(left), ToOperand(right));
1978 } else { // can_overflow.
1979 Register overflow = scratch0();
1980 Register scratch = scratch1();
1981 DCHECK(right->IsRegister() || right->IsConstantOperand());
1982 __ AdduAndCheckForOverflow(ToRegister(result), ToRegister(left),
1983 ToOperand(right), overflow, scratch);
1984 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, overflow,
1990 void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
1991 LOperand* left = instr->left();
1992 LOperand* right = instr->right();
1993 HMathMinMax::Operation operation = instr->hydrogen()->operation();
1994 Condition condition = (operation == HMathMinMax::kMathMin) ? le : ge;
1995 if (instr->hydrogen()->representation().IsSmiOrInteger32()) {
1996 Register left_reg = ToRegister(left);
1997 Register right_reg = EmitLoadRegister(right, scratch0());
1998 Register result_reg = ToRegister(instr->result());
1999 Label return_right, done;
2000 Register scratch = scratch1();
2001 __ Slt(scratch, left_reg, Operand(right_reg));
2002 if (condition == ge) {
2003 __ Movz(result_reg, left_reg, scratch);
2004 __ Movn(result_reg, right_reg, scratch);
2006 DCHECK(condition == le);
2007 __ Movn(result_reg, left_reg, scratch);
2008 __ Movz(result_reg, right_reg, scratch);
2011 DCHECK(instr->hydrogen()->representation().IsDouble());
2012 FPURegister left_reg = ToDoubleRegister(left);
2013 FPURegister right_reg = ToDoubleRegister(right);
2014 FPURegister result_reg = ToDoubleRegister(instr->result());
2015 Label check_nan_left, check_zero, return_left, return_right, done;
2016 __ BranchF(&check_zero, &check_nan_left, eq, left_reg, right_reg);
2017 __ BranchF(&return_left, NULL, condition, left_reg, right_reg);
2018 __ Branch(&return_right);
2020 __ bind(&check_zero);
2021 // left == right != 0.
2022 __ BranchF(&return_left, NULL, ne, left_reg, kDoubleRegZero);
2023 // At this point, both left and right are either 0 or -0.
2024 if (operation == HMathMinMax::kMathMin) {
2025 __ neg_d(left_reg, left_reg);
2026 __ sub_d(result_reg, left_reg, right_reg);
2027 __ neg_d(result_reg, result_reg);
2029 __ add_d(result_reg, left_reg, right_reg);
2033 __ bind(&check_nan_left);
2035 __ BranchF(NULL, &return_left, eq, left_reg, left_reg);
2036 __ bind(&return_right);
2037 if (!right_reg.is(result_reg)) {
2038 __ mov_d(result_reg, right_reg);
2042 __ bind(&return_left);
2043 if (!left_reg.is(result_reg)) {
2044 __ mov_d(result_reg, left_reg);
2051 void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
2052 DoubleRegister left = ToDoubleRegister(instr->left());
2053 DoubleRegister right = ToDoubleRegister(instr->right());
2054 DoubleRegister result = ToDoubleRegister(instr->result());
2055 switch (instr->op()) {
2057 __ add_d(result, left, right);
2060 __ sub_d(result, left, right);
2063 __ mul_d(result, left, right);
2066 __ div_d(result, left, right);
2069 // Save a0-a3 on the stack.
2070 RegList saved_regs = a0.bit() | a1.bit() | a2.bit() | a3.bit();
2071 __ MultiPush(saved_regs);
2073 __ PrepareCallCFunction(0, 2, scratch0());
2074 __ MovToFloatParameters(left, right);
2076 ExternalReference::mod_two_doubles_operation(isolate()),
2078 // Move the result in the double result register.
2079 __ MovFromFloatResult(result);
2081 // Restore saved register.
2082 __ MultiPop(saved_regs);
2092 void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
2093 DCHECK(ToRegister(instr->context()).is(cp));
2094 DCHECK(ToRegister(instr->left()).is(a1));
2095 DCHECK(ToRegister(instr->right()).is(a0));
2096 DCHECK(ToRegister(instr->result()).is(v0));
2099 CodeFactory::BinaryOpIC(isolate(), instr->op(), instr->strength()).code();
2100 CallCode(code, RelocInfo::CODE_TARGET, instr);
2101 // Other arch use a nop here, to signal that there is no inlined
2102 // patchable code. Mips does not need the nop, since our marker
2103 // instruction (andi zero_reg) will never be used in normal code.
2107 template<class InstrType>
2108 void LCodeGen::EmitBranch(InstrType instr,
2109 Condition condition,
2111 const Operand& src2) {
2112 int left_block = instr->TrueDestination(chunk_);
2113 int right_block = instr->FalseDestination(chunk_);
2115 int next_block = GetNextEmittedBlock();
2116 if (right_block == left_block || condition == al) {
2117 EmitGoto(left_block);
2118 } else if (left_block == next_block) {
2119 __ Branch(chunk_->GetAssemblyLabel(right_block),
2120 NegateCondition(condition), src1, src2);
2121 } else if (right_block == next_block) {
2122 __ Branch(chunk_->GetAssemblyLabel(left_block), condition, src1, src2);
2124 __ Branch(chunk_->GetAssemblyLabel(left_block), condition, src1, src2);
2125 __ Branch(chunk_->GetAssemblyLabel(right_block));
2130 template<class InstrType>
2131 void LCodeGen::EmitBranchF(InstrType instr,
2132 Condition condition,
2135 int right_block = instr->FalseDestination(chunk_);
2136 int left_block = instr->TrueDestination(chunk_);
2138 int next_block = GetNextEmittedBlock();
2139 if (right_block == left_block) {
2140 EmitGoto(left_block);
2141 } else if (left_block == next_block) {
2142 __ BranchF(chunk_->GetAssemblyLabel(right_block), NULL,
2143 NegateFpuCondition(condition), src1, src2);
2144 } else if (right_block == next_block) {
2145 __ BranchF(chunk_->GetAssemblyLabel(left_block), NULL,
2146 condition, src1, src2);
2148 __ BranchF(chunk_->GetAssemblyLabel(left_block), NULL,
2149 condition, src1, src2);
2150 __ Branch(chunk_->GetAssemblyLabel(right_block));
2155 template<class InstrType>
2156 void LCodeGen::EmitFalseBranch(InstrType instr,
2157 Condition condition,
2159 const Operand& src2) {
2160 int false_block = instr->FalseDestination(chunk_);
2161 __ Branch(chunk_->GetAssemblyLabel(false_block), condition, src1, src2);
2165 template<class InstrType>
2166 void LCodeGen::EmitFalseBranchF(InstrType instr,
2167 Condition condition,
2170 int false_block = instr->FalseDestination(chunk_);
2171 __ BranchF(chunk_->GetAssemblyLabel(false_block), NULL,
2172 condition, src1, src2);
2176 void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
2177 __ stop("LDebugBreak");
2181 void LCodeGen::DoBranch(LBranch* instr) {
2182 Representation r = instr->hydrogen()->value()->representation();
2183 if (r.IsInteger32() || r.IsSmi()) {
2184 DCHECK(!info()->IsStub());
2185 Register reg = ToRegister(instr->value());
2186 EmitBranch(instr, ne, reg, Operand(zero_reg));
2187 } else if (r.IsDouble()) {
2188 DCHECK(!info()->IsStub());
2189 DoubleRegister reg = ToDoubleRegister(instr->value());
2190 // Test the double value. Zero and NaN are false.
2191 EmitBranchF(instr, ogl, reg, kDoubleRegZero);
2193 DCHECK(r.IsTagged());
2194 Register reg = ToRegister(instr->value());
2195 HType type = instr->hydrogen()->value()->type();
2196 if (type.IsBoolean()) {
2197 DCHECK(!info()->IsStub());
2198 __ LoadRoot(at, Heap::kTrueValueRootIndex);
2199 EmitBranch(instr, eq, reg, Operand(at));
2200 } else if (type.IsSmi()) {
2201 DCHECK(!info()->IsStub());
2202 EmitBranch(instr, ne, reg, Operand(zero_reg));
2203 } else if (type.IsJSArray()) {
2204 DCHECK(!info()->IsStub());
2205 EmitBranch(instr, al, zero_reg, Operand(zero_reg));
2206 } else if (type.IsHeapNumber()) {
2207 DCHECK(!info()->IsStub());
2208 DoubleRegister dbl_scratch = double_scratch0();
2209 __ ldc1(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
2210 // Test the double value. Zero and NaN are false.
2211 EmitBranchF(instr, ogl, dbl_scratch, kDoubleRegZero);
2212 } else if (type.IsString()) {
2213 DCHECK(!info()->IsStub());
2214 __ ld(at, FieldMemOperand(reg, String::kLengthOffset));
2215 EmitBranch(instr, ne, at, Operand(zero_reg));
2217 ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
2218 // Avoid deopts in the case where we've never executed this path before.
2219 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
2221 if (expected.Contains(ToBooleanStub::UNDEFINED)) {
2222 // undefined -> false.
2223 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
2224 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
2226 if (expected.Contains(ToBooleanStub::BOOLEAN)) {
2227 // Boolean -> its value.
2228 __ LoadRoot(at, Heap::kTrueValueRootIndex);
2229 __ Branch(instr->TrueLabel(chunk_), eq, reg, Operand(at));
2230 __ LoadRoot(at, Heap::kFalseValueRootIndex);
2231 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
2233 if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
2235 __ LoadRoot(at, Heap::kNullValueRootIndex);
2236 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
2239 if (expected.Contains(ToBooleanStub::SMI)) {
2240 // Smis: 0 -> false, all other -> true.
2241 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(zero_reg));
2242 __ JumpIfSmi(reg, instr->TrueLabel(chunk_));
2243 } else if (expected.NeedsMap()) {
2244 // If we need a map later and have a Smi -> deopt.
2246 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
2249 const Register map = scratch0();
2250 if (expected.NeedsMap()) {
2251 __ ld(map, FieldMemOperand(reg, HeapObject::kMapOffset));
2252 if (expected.CanBeUndetectable()) {
2253 // Undetectable -> false.
2254 __ lbu(at, FieldMemOperand(map, Map::kBitFieldOffset));
2255 __ And(at, at, Operand(1 << Map::kIsUndetectable));
2256 __ Branch(instr->FalseLabel(chunk_), ne, at, Operand(zero_reg));
2260 if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
2261 // spec object -> true.
2262 __ lbu(at, FieldMemOperand(map, Map::kInstanceTypeOffset));
2263 __ Branch(instr->TrueLabel(chunk_),
2264 ge, at, Operand(FIRST_SPEC_OBJECT_TYPE));
2267 if (expected.Contains(ToBooleanStub::STRING)) {
2268 // String value -> false iff empty.
2270 __ lbu(at, FieldMemOperand(map, Map::kInstanceTypeOffset));
2271 __ Branch(¬_string, ge , at, Operand(FIRST_NONSTRING_TYPE));
2272 __ ld(at, FieldMemOperand(reg, String::kLengthOffset));
2273 __ Branch(instr->TrueLabel(chunk_), ne, at, Operand(zero_reg));
2274 __ Branch(instr->FalseLabel(chunk_));
2275 __ bind(¬_string);
2278 if (expected.Contains(ToBooleanStub::SYMBOL)) {
2279 // Symbol value -> true.
2280 const Register scratch = scratch1();
2281 __ lbu(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
2282 __ Branch(instr->TrueLabel(chunk_), eq, scratch, Operand(SYMBOL_TYPE));
2285 if (expected.Contains(ToBooleanStub::SIMD_VALUE)) {
2286 // Symbol value -> true.
2287 const Register scratch = scratch1();
2288 __ lbu(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
2289 __ Branch(instr->TrueLabel(chunk_), eq, scratch,
2290 Operand(FLOAT32X4_TYPE));
2293 if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
2294 // heap number -> false iff +0, -0, or NaN.
2295 DoubleRegister dbl_scratch = double_scratch0();
2296 Label not_heap_number;
2297 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
2298 __ Branch(¬_heap_number, ne, map, Operand(at));
2299 __ ldc1(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
2300 __ BranchF(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2301 ne, dbl_scratch, kDoubleRegZero);
2302 // Falls through if dbl_scratch == 0.
2303 __ Branch(instr->FalseLabel(chunk_));
2304 __ bind(¬_heap_number);
2307 if (!expected.IsGeneric()) {
2308 // We've seen something for the first time -> deopt.
2309 // This can only happen if we are not generic already.
2310 DeoptimizeIf(al, instr, Deoptimizer::kUnexpectedObject, zero_reg,
2318 void LCodeGen::EmitGoto(int block) {
2319 if (!IsNextEmittedBlock(block)) {
2320 __ jmp(chunk_->GetAssemblyLabel(LookupDestination(block)));
2325 void LCodeGen::DoGoto(LGoto* instr) {
2326 EmitGoto(instr->block_id());
2330 Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
2331 Condition cond = kNoCondition;
2334 case Token::EQ_STRICT:
2338 case Token::NE_STRICT:
2342 cond = is_unsigned ? lo : lt;
2345 cond = is_unsigned ? hi : gt;
2348 cond = is_unsigned ? ls : le;
2351 cond = is_unsigned ? hs : ge;
2354 case Token::INSTANCEOF:
2362 void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2363 LOperand* left = instr->left();
2364 LOperand* right = instr->right();
2366 instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2367 instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2368 Condition cond = TokenToCondition(instr->op(), is_unsigned);
2370 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2371 // We can statically evaluate the comparison.
2372 double left_val = ToDouble(LConstantOperand::cast(left));
2373 double right_val = ToDouble(LConstantOperand::cast(right));
2374 int next_block = EvalComparison(instr->op(), left_val, right_val) ?
2375 instr->TrueDestination(chunk_) : instr->FalseDestination(chunk_);
2376 EmitGoto(next_block);
2378 if (instr->is_double()) {
2379 // Compare left and right as doubles and load the
2380 // resulting flags into the normal status register.
2381 FPURegister left_reg = ToDoubleRegister(left);
2382 FPURegister right_reg = ToDoubleRegister(right);
2384 // If a NaN is involved, i.e. the result is unordered,
2385 // jump to false block label.
2386 __ BranchF(NULL, instr->FalseLabel(chunk_), eq,
2387 left_reg, right_reg);
2389 EmitBranchF(instr, cond, left_reg, right_reg);
2392 Operand cmp_right = Operand((int64_t)0);
2393 if (right->IsConstantOperand()) {
2394 int32_t value = ToInteger32(LConstantOperand::cast(right));
2395 if (instr->hydrogen_value()->representation().IsSmi()) {
2396 cmp_left = ToRegister(left);
2397 cmp_right = Operand(Smi::FromInt(value));
2399 cmp_left = ToRegister(left);
2400 cmp_right = Operand(value);
2402 } else if (left->IsConstantOperand()) {
2403 int32_t value = ToInteger32(LConstantOperand::cast(left));
2404 if (instr->hydrogen_value()->representation().IsSmi()) {
2405 cmp_left = ToRegister(right);
2406 cmp_right = Operand(Smi::FromInt(value));
2408 cmp_left = ToRegister(right);
2409 cmp_right = Operand(value);
2411 // We commuted the operands, so commute the condition.
2412 cond = CommuteCondition(cond);
2414 cmp_left = ToRegister(left);
2415 cmp_right = Operand(ToRegister(right));
2418 EmitBranch(instr, cond, cmp_left, cmp_right);
2424 void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
2425 Register left = ToRegister(instr->left());
2426 Register right = ToRegister(instr->right());
2428 EmitBranch(instr, eq, left, Operand(right));
2432 void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) {
2433 if (instr->hydrogen()->representation().IsTagged()) {
2434 Register input_reg = ToRegister(instr->object());
2435 __ li(at, Operand(factory()->the_hole_value()));
2436 EmitBranch(instr, eq, input_reg, Operand(at));
2440 DoubleRegister input_reg = ToDoubleRegister(instr->object());
2441 EmitFalseBranchF(instr, eq, input_reg, input_reg);
2443 Register scratch = scratch0();
2444 __ FmoveHigh(scratch, input_reg);
2445 EmitBranch(instr, eq, scratch,
2446 Operand(static_cast<int32_t>(kHoleNanUpper32)));
2450 void LCodeGen::DoCompareMinusZeroAndBranch(LCompareMinusZeroAndBranch* instr) {
2451 Representation rep = instr->hydrogen()->value()->representation();
2452 DCHECK(!rep.IsInteger32());
2453 Register scratch = ToRegister(instr->temp());
2455 if (rep.IsDouble()) {
2456 DoubleRegister value = ToDoubleRegister(instr->value());
2457 EmitFalseBranchF(instr, ne, value, kDoubleRegZero);
2458 __ FmoveHigh(scratch, value);
2459 // Only use low 32-bits of value.
2460 __ dsll32(scratch, scratch, 0);
2461 __ dsrl32(scratch, scratch, 0);
2462 __ li(at, 0x80000000);
2464 Register value = ToRegister(instr->value());
2467 Heap::kHeapNumberMapRootIndex,
2468 instr->FalseLabel(chunk()),
2470 __ lwu(scratch, FieldMemOperand(value, HeapNumber::kExponentOffset));
2471 EmitFalseBranch(instr, ne, scratch, Operand(0x80000000));
2472 __ lwu(scratch, FieldMemOperand(value, HeapNumber::kMantissaOffset));
2473 __ mov(at, zero_reg);
2475 EmitBranch(instr, eq, scratch, Operand(at));
2479 Condition LCodeGen::EmitIsObject(Register input,
2482 Label* is_not_object,
2484 __ JumpIfSmi(input, is_not_object);
2486 __ LoadRoot(temp2, Heap::kNullValueRootIndex);
2487 __ Branch(is_object, eq, input, Operand(temp2));
2490 __ ld(temp1, FieldMemOperand(input, HeapObject::kMapOffset));
2491 // Undetectable objects behave like undefined.
2492 __ lbu(temp2, FieldMemOperand(temp1, Map::kBitFieldOffset));
2493 __ And(temp2, temp2, Operand(1 << Map::kIsUndetectable));
2494 __ Branch(is_not_object, ne, temp2, Operand(zero_reg));
2496 // Load instance type and check that it is in object type range.
2497 __ lbu(temp2, FieldMemOperand(temp1, Map::kInstanceTypeOffset));
2498 __ Branch(is_not_object,
2499 lt, temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2505 void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) {
2506 Register reg = ToRegister(instr->value());
2507 Register temp1 = ToRegister(instr->temp());
2508 Register temp2 = scratch0();
2510 Condition true_cond =
2511 EmitIsObject(reg, temp1, temp2,
2512 instr->FalseLabel(chunk_), instr->TrueLabel(chunk_));
2514 EmitBranch(instr, true_cond, temp2,
2515 Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
2519 Condition LCodeGen::EmitIsString(Register input,
2521 Label* is_not_string,
2522 SmiCheck check_needed = INLINE_SMI_CHECK) {
2523 if (check_needed == INLINE_SMI_CHECK) {
2524 __ JumpIfSmi(input, is_not_string);
2526 __ GetObjectType(input, temp1, temp1);
2532 void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
2533 Register reg = ToRegister(instr->value());
2534 Register temp1 = ToRegister(instr->temp());
2536 SmiCheck check_needed =
2537 instr->hydrogen()->value()->type().IsHeapObject()
2538 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2539 Condition true_cond =
2540 EmitIsString(reg, temp1, instr->FalseLabel(chunk_), check_needed);
2542 EmitBranch(instr, true_cond, temp1,
2543 Operand(FIRST_NONSTRING_TYPE));
2547 void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
2548 Register input_reg = EmitLoadRegister(instr->value(), at);
2549 __ And(at, input_reg, kSmiTagMask);
2550 EmitBranch(instr, eq, at, Operand(zero_reg));
2554 void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
2555 Register input = ToRegister(instr->value());
2556 Register temp = ToRegister(instr->temp());
2558 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2559 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2561 __ ld(temp, FieldMemOperand(input, HeapObject::kMapOffset));
2562 __ lbu(temp, FieldMemOperand(temp, Map::kBitFieldOffset));
2563 __ And(at, temp, Operand(1 << Map::kIsUndetectable));
2564 EmitBranch(instr, ne, at, Operand(zero_reg));
2568 static Condition ComputeCompareCondition(Token::Value op) {
2570 case Token::EQ_STRICT:
2583 return kNoCondition;
2588 void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
2589 DCHECK(ToRegister(instr->context()).is(cp));
2590 Token::Value op = instr->op();
2593 CodeFactory::CompareIC(isolate(), op, Strength::WEAK).code();
2594 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2596 Condition condition = ComputeCompareCondition(op);
2598 EmitBranch(instr, condition, v0, Operand(zero_reg));
2602 static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
2603 InstanceType from = instr->from();
2604 InstanceType to = instr->to();
2605 if (from == FIRST_TYPE) return to;
2606 DCHECK(from == to || to == LAST_TYPE);
2611 static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
2612 InstanceType from = instr->from();
2613 InstanceType to = instr->to();
2614 if (from == to) return eq;
2615 if (to == LAST_TYPE) return hs;
2616 if (from == FIRST_TYPE) return ls;
2622 void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
2623 Register scratch = scratch0();
2624 Register input = ToRegister(instr->value());
2626 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2627 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2630 __ GetObjectType(input, scratch, scratch);
2632 BranchCondition(instr->hydrogen()),
2634 Operand(TestType(instr->hydrogen())));
2638 void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
2639 Register input = ToRegister(instr->value());
2640 Register result = ToRegister(instr->result());
2642 __ AssertString(input);
2644 __ lwu(result, FieldMemOperand(input, String::kHashFieldOffset));
2645 __ IndexFromHash(result, result);
2649 void LCodeGen::DoHasCachedArrayIndexAndBranch(
2650 LHasCachedArrayIndexAndBranch* instr) {
2651 Register input = ToRegister(instr->value());
2652 Register scratch = scratch0();
2655 FieldMemOperand(input, String::kHashFieldOffset));
2656 __ And(at, scratch, Operand(String::kContainsCachedArrayIndexMask));
2657 EmitBranch(instr, eq, at, Operand(zero_reg));
2661 // Branches to a label or falls through with the answer in flags. Trashes
2662 // the temp registers, but not the input.
2663 void LCodeGen::EmitClassOfTest(Label* is_true,
2665 Handle<String>class_name,
2669 DCHECK(!input.is(temp));
2670 DCHECK(!input.is(temp2));
2671 DCHECK(!temp.is(temp2));
2673 __ JumpIfSmi(input, is_false);
2675 if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
2676 // Assuming the following assertions, we can use the same compares to test
2677 // for both being a function type and being in the object type range.
2678 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
2679 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2680 FIRST_SPEC_OBJECT_TYPE + 1);
2681 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2682 LAST_SPEC_OBJECT_TYPE - 1);
2683 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
2685 __ GetObjectType(input, temp, temp2);
2686 __ Branch(is_false, lt, temp2, Operand(FIRST_SPEC_OBJECT_TYPE));
2687 __ Branch(is_true, eq, temp2, Operand(FIRST_SPEC_OBJECT_TYPE));
2688 __ Branch(is_true, eq, temp2, Operand(LAST_SPEC_OBJECT_TYPE));
2690 // Faster code path to avoid two compares: subtract lower bound from the
2691 // actual type and do a signed compare with the width of the type range.
2692 __ GetObjectType(input, temp, temp2);
2693 __ Dsubu(temp2, temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2694 __ Branch(is_false, gt, temp2, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE -
2695 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2698 // Now we are in the FIRST-LAST_NONCALLABLE_SPEC_OBJECT_TYPE range.
2699 // Check if the constructor in the map is a function.
2700 Register instance_type = scratch1();
2701 DCHECK(!instance_type.is(temp));
2702 __ GetMapConstructor(temp, temp, temp2, instance_type);
2704 // Objects with a non-function constructor have class 'Object'.
2705 if (String::Equals(class_name, isolate()->factory()->Object_string())) {
2706 __ Branch(is_true, ne, instance_type, Operand(JS_FUNCTION_TYPE));
2708 __ Branch(is_false, ne, instance_type, Operand(JS_FUNCTION_TYPE));
2711 // temp now contains the constructor function. Grab the
2712 // instance class name from there.
2713 __ ld(temp, FieldMemOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2714 __ ld(temp, FieldMemOperand(temp,
2715 SharedFunctionInfo::kInstanceClassNameOffset));
2716 // The class name we are testing against is internalized since it's a literal.
2717 // The name in the constructor is internalized because of the way the context
2718 // is booted. This routine isn't expected to work for random API-created
2719 // classes and it doesn't have to because you can't access it with natives
2720 // syntax. Since both sides are internalized it is sufficient to use an
2721 // identity comparison.
2723 // End with the address of this class_name instance in temp register.
2724 // On MIPS, the caller must do the comparison with Handle<String>class_name.
2728 void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
2729 Register input = ToRegister(instr->value());
2730 Register temp = scratch0();
2731 Register temp2 = ToRegister(instr->temp());
2732 Handle<String> class_name = instr->hydrogen()->class_name();
2734 EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2735 class_name, input, temp, temp2);
2737 EmitBranch(instr, eq, temp, Operand(class_name));
2741 void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
2742 Register reg = ToRegister(instr->value());
2743 Register temp = ToRegister(instr->temp());
2745 __ ld(temp, FieldMemOperand(reg, HeapObject::kMapOffset));
2746 EmitBranch(instr, eq, temp, Operand(instr->map()));
2750 void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
2751 DCHECK(ToRegister(instr->context()).is(cp));
2752 Label true_label, done;
2753 DCHECK(ToRegister(instr->left()).is(a0)); // Object is in a0.
2754 DCHECK(ToRegister(instr->right()).is(a1)); // Function is in a1.
2755 Register result = ToRegister(instr->result());
2756 DCHECK(result.is(v0));
2758 InstanceofStub stub(isolate(), InstanceofStub::kArgsInRegisters);
2759 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2761 __ Branch(&true_label, eq, result, Operand(zero_reg));
2762 __ li(result, Operand(factory()->false_value()));
2764 __ bind(&true_label);
2765 __ li(result, Operand(factory()->true_value()));
2770 void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) {
2771 class DeferredInstanceOfKnownGlobal final : public LDeferredCode {
2773 DeferredInstanceOfKnownGlobal(LCodeGen* codegen,
2774 LInstanceOfKnownGlobal* instr)
2775 : LDeferredCode(codegen), instr_(instr) { }
2776 void Generate() override {
2777 codegen()->DoDeferredInstanceOfKnownGlobal(instr_, &map_check_);
2779 LInstruction* instr() override { return instr_; }
2780 Label* map_check() { return &map_check_; }
2783 LInstanceOfKnownGlobal* instr_;
2787 DeferredInstanceOfKnownGlobal* deferred;
2788 deferred = new(zone()) DeferredInstanceOfKnownGlobal(this, instr);
2790 Label done, false_result;
2791 Register object = ToRegister(instr->value());
2792 Register temp = ToRegister(instr->temp());
2793 Register result = ToRegister(instr->result());
2795 DCHECK(object.is(a0));
2796 DCHECK(result.is(v0));
2798 // A Smi is not instance of anything.
2799 __ JumpIfSmi(object, &false_result);
2801 // This is the inlined call site instanceof cache. The two occurences of the
2802 // hole value will be patched to the last map/result pair generated by the
2805 Register map = temp;
2806 __ ld(map, FieldMemOperand(object, HeapObject::kMapOffset));
2808 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
2809 __ bind(deferred->map_check()); // Label for calculating code patching.
2810 // We use Factory::the_hole_value() on purpose instead of loading from the
2811 // root array to force relocation to be able to later patch with
2813 Handle<Cell> cell = factory()->NewCell(factory()->the_hole_value());
2814 __ li(at, Operand(cell));
2815 __ ld(at, FieldMemOperand(at, Cell::kValueOffset));
2816 __ BranchShort(&cache_miss, ne, map, Operand(at));
2817 // We use Factory::the_hole_value() on purpose instead of loading from the
2818 // root array to force relocation to be able to later patch
2819 // with true or false. The distance from map check has to be constant.
2820 __ li(result, Operand(factory()->the_hole_value()));
2823 // The inlined call site cache did not match. Check null and string before
2824 // calling the deferred code.
2825 __ bind(&cache_miss);
2826 // Null is not instance of anything.
2827 __ LoadRoot(temp, Heap::kNullValueRootIndex);
2828 __ Branch(&false_result, eq, object, Operand(temp));
2830 // String values is not instance of anything.
2831 Condition cc = __ IsObjectStringType(object, temp, temp);
2832 __ Branch(&false_result, cc, temp, Operand(zero_reg));
2834 // Go to the deferred code.
2835 __ Branch(deferred->entry());
2837 __ bind(&false_result);
2838 __ LoadRoot(result, Heap::kFalseValueRootIndex);
2840 // Here result has either true or false. Deferred code also produces true or
2842 __ bind(deferred->exit());
2847 void LCodeGen::DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
2849 Register result = ToRegister(instr->result());
2850 DCHECK(result.is(v0));
2852 InstanceofStub::Flags flags = InstanceofStub::kNoFlags;
2853 flags = static_cast<InstanceofStub::Flags>(
2854 flags | InstanceofStub::kArgsInRegisters);
2855 flags = static_cast<InstanceofStub::Flags>(
2856 flags | InstanceofStub::kCallSiteInlineCheck);
2857 flags = static_cast<InstanceofStub::Flags>(
2858 flags | InstanceofStub::kReturnTrueFalseObject);
2859 InstanceofStub stub(isolate(), flags);
2861 PushSafepointRegistersScope scope(this);
2862 LoadContextFromDeferred(instr->context());
2864 // Get the temp register reserved by the instruction. This needs to be a4 as
2865 // its slot of the pushing of safepoint registers is used to communicate the
2866 // offset to the location of the map check.
2867 Register temp = ToRegister(instr->temp());
2868 DCHECK(temp.is(a4));
2869 __ li(InstanceofStub::right(), instr->function());
2870 static const int kAdditionalDelta = 13;
2871 int delta = masm_->InstructionsGeneratedSince(map_check) + kAdditionalDelta;
2872 Label before_push_delta;
2873 __ bind(&before_push_delta);
2875 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
2876 __ li(temp, Operand(delta * kIntSize), CONSTANT_SIZE);
2877 __ StoreToSafepointRegisterSlot(temp, temp);
2879 CallCodeGeneric(stub.GetCode(),
2880 RelocInfo::CODE_TARGET,
2882 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
2883 LEnvironment* env = instr->GetDeferredLazyDeoptimizationEnvironment();
2884 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
2885 // Put the result value into the result register slot and
2886 // restore all registers.
2887 __ StoreToSafepointRegisterSlot(result, result);
2891 void LCodeGen::DoCmpT(LCmpT* instr) {
2892 DCHECK(ToRegister(instr->context()).is(cp));
2893 Token::Value op = instr->op();
2896 CodeFactory::CompareIC(isolate(), op, instr->strength()).code();
2897 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2898 // On MIPS there is no need for a "no inlined smi code" marker (nop).
2900 Condition condition = ComputeCompareCondition(op);
2901 // A minor optimization that relies on LoadRoot always emitting one
2903 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
2905 __ Branch(USE_DELAY_SLOT, &done, condition, v0, Operand(zero_reg));
2907 __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex);
2908 DCHECK_EQ(1, masm()->InstructionsGeneratedSince(&check));
2909 __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex);
2914 void LCodeGen::DoReturn(LReturn* instr) {
2915 if (FLAG_trace && info()->IsOptimizing()) {
2916 // Push the return value on the stack as the parameter.
2917 // Runtime::TraceExit returns its parameter in v0. We're leaving the code
2918 // managed by the register allocator and tearing down the frame, it's
2919 // safe to write to the context register.
2921 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2922 __ CallRuntime(Runtime::kTraceExit, 1);
2924 if (info()->saves_caller_doubles()) {
2925 RestoreCallerDoubles();
2927 int no_frame_start = -1;
2928 if (NeedsEagerFrame()) {
2930 no_frame_start = masm_->pc_offset();
2933 if (instr->has_constant_parameter_count()) {
2934 int parameter_count = ToInteger32(instr->constant_parameter_count());
2935 int32_t sp_delta = (parameter_count + 1) * kPointerSize;
2936 if (sp_delta != 0) {
2937 __ Daddu(sp, sp, Operand(sp_delta));
2940 DCHECK(info()->IsStub()); // Functions would need to drop one more value.
2941 Register reg = ToRegister(instr->parameter_count());
2942 // The argument count parameter is a smi
2944 __ dsll(at, reg, kPointerSizeLog2);
2945 __ Daddu(sp, sp, at);
2950 if (no_frame_start != -1) {
2951 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
2957 void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
2958 Register vector_register = ToRegister(instr->temp_vector());
2959 Register slot_register = LoadWithVectorDescriptor::SlotRegister();
2960 DCHECK(vector_register.is(LoadWithVectorDescriptor::VectorRegister()));
2961 DCHECK(slot_register.is(a0));
2963 AllowDeferredHandleDereference vector_structure_check;
2964 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2965 __ li(vector_register, vector);
2966 // No need to allocate this register.
2967 FeedbackVectorICSlot slot = instr->hydrogen()->slot();
2968 int index = vector->GetIndex(slot);
2969 __ li(slot_register, Operand(Smi::FromInt(index)));
2974 void LCodeGen::EmitVectorStoreICRegisters(T* instr) {
2975 Register vector_register = ToRegister(instr->temp_vector());
2976 Register slot_register = ToRegister(instr->temp_slot());
2978 AllowDeferredHandleDereference vector_structure_check;
2979 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2980 __ li(vector_register, vector);
2981 FeedbackVectorICSlot slot = instr->hydrogen()->slot();
2982 int index = vector->GetIndex(slot);
2983 __ li(slot_register, Operand(Smi::FromInt(index)));
2987 void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
2988 DCHECK(ToRegister(instr->context()).is(cp));
2989 DCHECK(ToRegister(instr->global_object())
2990 .is(LoadDescriptor::ReceiverRegister()));
2991 DCHECK(ToRegister(instr->result()).is(v0));
2993 __ li(LoadDescriptor::NameRegister(), Operand(instr->name()));
2994 EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
2996 CodeFactory::LoadICInOptimizedCode(isolate(), instr->typeof_mode(),
2997 SLOPPY, PREMONOMORPHIC).code();
2998 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3002 void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
3003 Register context = ToRegister(instr->context());
3004 Register result = ToRegister(instr->result());
3006 __ ld(result, ContextOperand(context, instr->slot_index()));
3007 if (instr->hydrogen()->RequiresHoleCheck()) {
3008 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
3010 if (instr->hydrogen()->DeoptimizesOnHole()) {
3011 DeoptimizeIf(eq, instr, Deoptimizer::kHole, result, Operand(at));
3014 __ Branch(&is_not_hole, ne, result, Operand(at));
3015 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3016 __ bind(&is_not_hole);
3022 void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
3023 Register context = ToRegister(instr->context());
3024 Register value = ToRegister(instr->value());
3025 Register scratch = scratch0();
3026 MemOperand target = ContextOperand(context, instr->slot_index());
3028 Label skip_assignment;
3030 if (instr->hydrogen()->RequiresHoleCheck()) {
3031 __ ld(scratch, target);
3032 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
3034 if (instr->hydrogen()->DeoptimizesOnHole()) {
3035 DeoptimizeIf(eq, instr, Deoptimizer::kHole, scratch, Operand(at));
3037 __ Branch(&skip_assignment, ne, scratch, Operand(at));
3041 __ sd(value, target);
3042 if (instr->hydrogen()->NeedsWriteBarrier()) {
3043 SmiCheck check_needed =
3044 instr->hydrogen()->value()->type().IsHeapObject()
3045 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
3046 __ RecordWriteContextSlot(context,
3052 EMIT_REMEMBERED_SET,
3056 __ bind(&skip_assignment);
3060 void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
3061 HObjectAccess access = instr->hydrogen()->access();
3062 int offset = access.offset();
3063 Register object = ToRegister(instr->object());
3064 if (access.IsExternalMemory()) {
3065 Register result = ToRegister(instr->result());
3066 MemOperand operand = MemOperand(object, offset);
3067 __ Load(result, operand, access.representation());
3071 if (instr->hydrogen()->representation().IsDouble()) {
3072 DoubleRegister result = ToDoubleRegister(instr->result());
3073 __ ldc1(result, FieldMemOperand(object, offset));
3077 Register result = ToRegister(instr->result());
3078 if (!access.IsInobject()) {
3079 __ ld(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
3083 Representation representation = access.representation();
3084 if (representation.IsSmi() && SmiValuesAre32Bits() &&
3085 instr->hydrogen()->representation().IsInteger32()) {
3086 if (FLAG_debug_code) {
3087 // Verify this is really an Smi.
3088 Register scratch = scratch0();
3089 __ Load(scratch, FieldMemOperand(object, offset), representation);
3090 __ AssertSmi(scratch);
3093 // Read int value directly from upper half of the smi.
3094 STATIC_ASSERT(kSmiTag == 0);
3095 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 32);
3096 offset += kPointerSize / 2;
3097 representation = Representation::Integer32();
3099 __ Load(result, FieldMemOperand(object, offset), representation);
3103 void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
3104 DCHECK(ToRegister(instr->context()).is(cp));
3105 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3106 DCHECK(ToRegister(instr->result()).is(v0));
3108 // Name is always in a2.
3109 __ li(LoadDescriptor::NameRegister(), Operand(instr->name()));
3110 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
3112 CodeFactory::LoadICInOptimizedCode(
3113 isolate(), NOT_INSIDE_TYPEOF, instr->hydrogen()->language_mode(),
3114 instr->hydrogen()->initialization_state()).code();
3115 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3119 void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
3120 Register scratch = scratch0();
3121 Register function = ToRegister(instr->function());
3122 Register result = ToRegister(instr->result());
3124 // Get the prototype or initial map from the function.
3126 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
3128 // Check that the function has a prototype or an initial map.
3129 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
3130 DeoptimizeIf(eq, instr, Deoptimizer::kHole, result, Operand(at));
3132 // If the function does not have an initial map, we're done.
3134 __ GetObjectType(result, scratch, scratch);
3135 __ Branch(&done, ne, scratch, Operand(MAP_TYPE));
3137 // Get the prototype from the initial map.
3138 __ ld(result, FieldMemOperand(result, Map::kPrototypeOffset));
3145 void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
3146 Register result = ToRegister(instr->result());
3147 __ LoadRoot(result, instr->index());
3151 void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
3152 Register arguments = ToRegister(instr->arguments());
3153 Register result = ToRegister(instr->result());
3154 // There are two words between the frame pointer and the last argument.
3155 // Subtracting from length accounts for one of them add one more.
3156 if (instr->length()->IsConstantOperand()) {
3157 int const_length = ToInteger32(LConstantOperand::cast(instr->length()));
3158 if (instr->index()->IsConstantOperand()) {
3159 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3160 int index = (const_length - const_index) + 1;
3161 __ ld(result, MemOperand(arguments, index * kPointerSize));
3163 Register index = ToRegister(instr->index());
3164 __ li(at, Operand(const_length + 1));
3165 __ Dsubu(result, at, index);
3166 __ dsll(at, result, kPointerSizeLog2);
3167 __ Daddu(at, arguments, at);
3168 __ ld(result, MemOperand(at));
3170 } else if (instr->index()->IsConstantOperand()) {
3171 Register length = ToRegister(instr->length());
3172 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3173 int loc = const_index - 1;
3175 __ Dsubu(result, length, Operand(loc));
3176 __ dsll(at, result, kPointerSizeLog2);
3177 __ Daddu(at, arguments, at);
3178 __ ld(result, MemOperand(at));
3180 __ dsll(at, length, kPointerSizeLog2);
3181 __ Daddu(at, arguments, at);
3182 __ ld(result, MemOperand(at));
3185 Register length = ToRegister(instr->length());
3186 Register index = ToRegister(instr->index());
3187 __ Dsubu(result, length, index);
3188 __ Daddu(result, result, 1);
3189 __ dsll(at, result, kPointerSizeLog2);
3190 __ Daddu(at, arguments, at);
3191 __ ld(result, MemOperand(at));
3196 void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
3197 Register external_pointer = ToRegister(instr->elements());
3198 Register key = no_reg;
3199 ElementsKind elements_kind = instr->elements_kind();
3200 bool key_is_constant = instr->key()->IsConstantOperand();
3201 int constant_key = 0;
3202 if (key_is_constant) {
3203 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3204 if (constant_key & 0xF0000000) {
3205 Abort(kArrayIndexConstantValueTooBig);
3208 key = ToRegister(instr->key());
3210 int element_size_shift = ElementsKindToShiftSize(elements_kind);
3211 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3212 ? (element_size_shift - (kSmiTagSize + kSmiShiftSize))
3213 : element_size_shift;
3214 int base_offset = instr->base_offset();
3216 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3217 elements_kind == FLOAT32_ELEMENTS ||
3218 elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
3219 elements_kind == FLOAT64_ELEMENTS) {
3220 FPURegister result = ToDoubleRegister(instr->result());
3221 if (key_is_constant) {
3222 __ Daddu(scratch0(), external_pointer,
3223 constant_key << element_size_shift);
3225 if (shift_size < 0) {
3226 if (shift_size == -32) {
3227 __ dsra32(scratch0(), key, 0);
3229 __ dsra(scratch0(), key, -shift_size);
3232 __ dsll(scratch0(), key, shift_size);
3234 __ Daddu(scratch0(), scratch0(), external_pointer);
3236 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3237 elements_kind == FLOAT32_ELEMENTS) {
3238 __ lwc1(result, MemOperand(scratch0(), base_offset));
3239 __ cvt_d_s(result, result);
3240 } else { // i.e. elements_kind == EXTERNAL_DOUBLE_ELEMENTS
3241 __ ldc1(result, MemOperand(scratch0(), base_offset));
3244 Register result = ToRegister(instr->result());
3245 MemOperand mem_operand = PrepareKeyedOperand(
3246 key, external_pointer, key_is_constant, constant_key,
3247 element_size_shift, shift_size, base_offset);
3248 switch (elements_kind) {
3249 case EXTERNAL_INT8_ELEMENTS:
3251 __ lb(result, mem_operand);
3253 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
3254 case EXTERNAL_UINT8_ELEMENTS:
3255 case UINT8_ELEMENTS:
3256 case UINT8_CLAMPED_ELEMENTS:
3257 __ lbu(result, mem_operand);
3259 case EXTERNAL_INT16_ELEMENTS:
3260 case INT16_ELEMENTS:
3261 __ lh(result, mem_operand);
3263 case EXTERNAL_UINT16_ELEMENTS:
3264 case UINT16_ELEMENTS:
3265 __ lhu(result, mem_operand);
3267 case EXTERNAL_INT32_ELEMENTS:
3268 case INT32_ELEMENTS:
3269 __ lw(result, mem_operand);
3271 case EXTERNAL_UINT32_ELEMENTS:
3272 case UINT32_ELEMENTS:
3273 __ lw(result, mem_operand);
3274 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
3275 DeoptimizeIf(Ugreater_equal, instr, Deoptimizer::kNegativeValue,
3276 result, Operand(0x80000000));
3279 case FLOAT32_ELEMENTS:
3280 case FLOAT64_ELEMENTS:
3281 case EXTERNAL_FLOAT32_ELEMENTS:
3282 case EXTERNAL_FLOAT64_ELEMENTS:
3283 case FAST_DOUBLE_ELEMENTS:
3285 case FAST_SMI_ELEMENTS:
3286 case FAST_HOLEY_DOUBLE_ELEMENTS:
3287 case FAST_HOLEY_ELEMENTS:
3288 case FAST_HOLEY_SMI_ELEMENTS:
3289 case DICTIONARY_ELEMENTS:
3290 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
3291 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
3299 void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
3300 Register elements = ToRegister(instr->elements());
3301 bool key_is_constant = instr->key()->IsConstantOperand();
3302 Register key = no_reg;
3303 DoubleRegister result = ToDoubleRegister(instr->result());
3304 Register scratch = scratch0();
3306 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
3308 int base_offset = instr->base_offset();
3309 if (key_is_constant) {
3310 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3311 if (constant_key & 0xF0000000) {
3312 Abort(kArrayIndexConstantValueTooBig);
3314 base_offset += constant_key * kDoubleSize;
3316 __ Daddu(scratch, elements, Operand(base_offset));
3318 if (!key_is_constant) {
3319 key = ToRegister(instr->key());
3320 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3321 ? (element_size_shift - (kSmiTagSize + kSmiShiftSize))
3322 : element_size_shift;
3323 if (shift_size > 0) {
3324 __ dsll(at, key, shift_size);
3325 } else if (shift_size == -32) {
3326 __ dsra32(at, key, 0);
3328 __ dsra(at, key, -shift_size);
3330 __ Daddu(scratch, scratch, at);
3333 __ ldc1(result, MemOperand(scratch));
3335 if (instr->hydrogen()->RequiresHoleCheck()) {
3336 __ FmoveHigh(scratch, result);
3337 DeoptimizeIf(eq, instr, Deoptimizer::kHole, scratch,
3338 Operand(static_cast<int32_t>(kHoleNanUpper32)));
3343 void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
3344 HLoadKeyed* hinstr = instr->hydrogen();
3345 Register elements = ToRegister(instr->elements());
3346 Register result = ToRegister(instr->result());
3347 Register scratch = scratch0();
3348 Register store_base = scratch;
3349 int offset = instr->base_offset();
3351 if (instr->key()->IsConstantOperand()) {
3352 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
3353 offset += ToInteger32(const_operand) * kPointerSize;
3354 store_base = elements;
3356 Register key = ToRegister(instr->key());
3357 // Even though the HLoadKeyed instruction forces the input
3358 // representation for the key to be an integer, the input gets replaced
3359 // during bound check elimination with the index argument to the bounds
3360 // check, which can be tagged, so that case must be handled here, too.
3361 if (instr->hydrogen()->key()->representation().IsSmi()) {
3362 __ SmiScale(scratch, key, kPointerSizeLog2);
3363 __ daddu(scratch, elements, scratch);
3365 __ dsll(scratch, key, kPointerSizeLog2);
3366 __ daddu(scratch, elements, scratch);
3370 Representation representation = hinstr->representation();
3371 if (representation.IsInteger32() && SmiValuesAre32Bits() &&
3372 hinstr->elements_kind() == FAST_SMI_ELEMENTS) {
3373 DCHECK(!hinstr->RequiresHoleCheck());
3374 if (FLAG_debug_code) {
3375 Register temp = scratch1();
3376 __ Load(temp, MemOperand(store_base, offset), Representation::Smi());
3380 // Read int value directly from upper half of the smi.
3381 STATIC_ASSERT(kSmiTag == 0);
3382 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 32);
3383 offset += kPointerSize / 2;
3386 __ Load(result, MemOperand(store_base, offset), representation);
3388 // Check for the hole value.
3389 if (hinstr->RequiresHoleCheck()) {
3390 if (IsFastSmiElementsKind(instr->hydrogen()->elements_kind())) {
3391 __ SmiTst(result, scratch);
3392 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi, scratch,
3395 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
3396 DeoptimizeIf(eq, instr, Deoptimizer::kHole, result, Operand(scratch));
3398 } else if (instr->hydrogen()->hole_mode() == CONVERT_HOLE_TO_UNDEFINED) {
3399 DCHECK(instr->hydrogen()->elements_kind() == FAST_HOLEY_ELEMENTS);
3401 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
3402 __ Branch(&done, ne, result, Operand(scratch));
3403 if (info()->IsStub()) {
3404 // A stub can safely convert the hole to undefined only if the array
3405 // protector cell contains (Smi) Isolate::kArrayProtectorValid. Otherwise
3406 // it needs to bail out.
3407 __ LoadRoot(result, Heap::kArrayProtectorRootIndex);
3408 // The comparison only needs LS bits of value, which is a smi.
3409 __ ld(result, FieldMemOperand(result, Cell::kValueOffset));
3410 DeoptimizeIf(ne, instr, Deoptimizer::kHole, result,
3411 Operand(Smi::FromInt(Isolate::kArrayProtectorValid)));
3413 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3419 void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
3420 if (instr->is_typed_elements()) {
3421 DoLoadKeyedExternalArray(instr);
3422 } else if (instr->hydrogen()->representation().IsDouble()) {
3423 DoLoadKeyedFixedDoubleArray(instr);
3425 DoLoadKeyedFixedArray(instr);
3430 MemOperand LCodeGen::PrepareKeyedOperand(Register key,
3432 bool key_is_constant,
3437 if (key_is_constant) {
3438 return MemOperand(base, (constant_key << element_size) + base_offset);
3441 if (base_offset == 0) {
3442 if (shift_size >= 0) {
3443 __ dsll(scratch0(), key, shift_size);
3444 __ Daddu(scratch0(), base, scratch0());
3445 return MemOperand(scratch0());
3447 if (shift_size == -32) {
3448 __ dsra32(scratch0(), key, 0);
3450 __ dsra(scratch0(), key, -shift_size);
3452 __ Daddu(scratch0(), base, scratch0());
3453 return MemOperand(scratch0());
3457 if (shift_size >= 0) {
3458 __ dsll(scratch0(), key, shift_size);
3459 __ Daddu(scratch0(), base, scratch0());
3460 return MemOperand(scratch0(), base_offset);
3462 if (shift_size == -32) {
3463 __ dsra32(scratch0(), key, 0);
3465 __ dsra(scratch0(), key, -shift_size);
3467 __ Daddu(scratch0(), base, scratch0());
3468 return MemOperand(scratch0(), base_offset);
3473 void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3474 DCHECK(ToRegister(instr->context()).is(cp));
3475 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3476 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
3478 if (instr->hydrogen()->HasVectorAndSlot()) {
3479 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
3482 Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(
3483 isolate(), instr->hydrogen()->language_mode(),
3484 instr->hydrogen()->initialization_state()).code();
3485 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3489 void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
3490 Register scratch = scratch0();
3491 Register temp = scratch1();
3492 Register result = ToRegister(instr->result());
3494 if (instr->hydrogen()->from_inlined()) {
3495 __ Dsubu(result, sp, 2 * kPointerSize);
3497 // Check if the calling frame is an arguments adaptor frame.
3498 Label done, adapted;
3499 __ ld(scratch, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3500 __ ld(result, MemOperand(scratch, StandardFrameConstants::kContextOffset));
3501 __ Xor(temp, result, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3503 // Result is the frame pointer for the frame if not adapted and for the real
3504 // frame below the adaptor frame if adapted.
3505 __ Movn(result, fp, temp); // Move only if temp is not equal to zero (ne).
3506 __ Movz(result, scratch, temp); // Move only if temp is equal to zero (eq).
3511 void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
3512 Register elem = ToRegister(instr->elements());
3513 Register result = ToRegister(instr->result());
3517 // If no arguments adaptor frame the number of arguments is fixed.
3518 __ Daddu(result, zero_reg, Operand(scope()->num_parameters()));
3519 __ Branch(&done, eq, fp, Operand(elem));
3521 // Arguments adaptor frame present. Get argument length from there.
3522 __ ld(result, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3524 MemOperand(result, ArgumentsAdaptorFrameConstants::kLengthOffset));
3525 __ SmiUntag(result);
3527 // Argument length is in result register.
3532 void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
3533 Register receiver = ToRegister(instr->receiver());
3534 Register function = ToRegister(instr->function());
3535 Register result = ToRegister(instr->result());
3536 Register scratch = scratch0();
3538 // If the receiver is null or undefined, we have to pass the global
3539 // object as a receiver to normal functions. Values have to be
3540 // passed unchanged to builtins and strict-mode functions.
3541 Label global_object, result_in_receiver;
3543 if (!instr->hydrogen()->known_function()) {
3544 // Do not transform the receiver to object for strict mode functions.
3546 FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
3548 // Do not transform the receiver to object for builtins.
3549 int32_t strict_mode_function_mask =
3550 1 << SharedFunctionInfo::kStrictModeBitWithinByte;
3551 int32_t native_mask = 1 << SharedFunctionInfo::kNativeBitWithinByte;
3554 FieldMemOperand(scratch, SharedFunctionInfo::kStrictModeByteOffset));
3555 __ And(at, at, Operand(strict_mode_function_mask));
3556 __ Branch(&result_in_receiver, ne, at, Operand(zero_reg));
3558 FieldMemOperand(scratch, SharedFunctionInfo::kNativeByteOffset));
3559 __ And(at, at, Operand(native_mask));
3560 __ Branch(&result_in_receiver, ne, at, Operand(zero_reg));
3563 // Normal function. Replace undefined or null with global receiver.
3564 __ LoadRoot(scratch, Heap::kNullValueRootIndex);
3565 __ Branch(&global_object, eq, receiver, Operand(scratch));
3566 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
3567 __ Branch(&global_object, eq, receiver, Operand(scratch));
3569 // Deoptimize if the receiver is not a JS object.
3570 __ SmiTst(receiver, scratch);
3571 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, scratch, Operand(zero_reg));
3573 __ GetObjectType(receiver, scratch, scratch);
3574 DeoptimizeIf(lt, instr, Deoptimizer::kNotAJavaScriptObject, scratch,
3575 Operand(FIRST_SPEC_OBJECT_TYPE));
3576 __ Branch(&result_in_receiver);
3578 __ bind(&global_object);
3579 __ ld(result, FieldMemOperand(function, JSFunction::kContextOffset));
3581 ContextOperand(result, Context::GLOBAL_OBJECT_INDEX));
3583 FieldMemOperand(result, GlobalObject::kGlobalProxyOffset));
3585 if (result.is(receiver)) {
3586 __ bind(&result_in_receiver);
3589 __ Branch(&result_ok);
3590 __ bind(&result_in_receiver);
3591 __ mov(result, receiver);
3592 __ bind(&result_ok);
3597 void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3598 Register receiver = ToRegister(instr->receiver());
3599 Register function = ToRegister(instr->function());
3600 Register length = ToRegister(instr->length());
3601 Register elements = ToRegister(instr->elements());
3602 Register scratch = scratch0();
3603 DCHECK(receiver.is(a0)); // Used for parameter count.
3604 DCHECK(function.is(a1)); // Required by InvokeFunction.
3605 DCHECK(ToRegister(instr->result()).is(v0));
3607 // Copy the arguments to this function possibly from the
3608 // adaptor frame below it.
3609 const uint32_t kArgumentsLimit = 1 * KB;
3610 DeoptimizeIf(hi, instr, Deoptimizer::kTooManyArguments, length,
3611 Operand(kArgumentsLimit));
3613 // Push the receiver and use the register to keep the original
3614 // number of arguments.
3616 __ Move(receiver, length);
3617 // The arguments are at a one pointer size offset from elements.
3618 __ Daddu(elements, elements, Operand(1 * kPointerSize));
3620 // Loop through the arguments pushing them onto the execution
3623 // length is a small non-negative integer, due to the test above.
3624 __ Branch(USE_DELAY_SLOT, &invoke, eq, length, Operand(zero_reg));
3625 __ dsll(scratch, length, kPointerSizeLog2);
3627 __ Daddu(scratch, elements, scratch);
3628 __ ld(scratch, MemOperand(scratch));
3630 __ Dsubu(length, length, Operand(1));
3631 __ Branch(USE_DELAY_SLOT, &loop, ne, length, Operand(zero_reg));
3632 __ dsll(scratch, length, kPointerSizeLog2);
3635 DCHECK(instr->HasPointerMap());
3636 LPointerMap* pointers = instr->pointer_map();
3637 SafepointGenerator safepoint_generator(
3638 this, pointers, Safepoint::kLazyDeopt);
3639 // The number of arguments is stored in receiver which is a0, as expected
3640 // by InvokeFunction.
3641 ParameterCount actual(receiver);
3642 __ InvokeFunction(function, actual, CALL_FUNCTION, safepoint_generator);
3646 void LCodeGen::DoPushArgument(LPushArgument* instr) {
3647 LOperand* argument = instr->value();
3648 if (argument->IsDoubleRegister() || argument->IsDoubleStackSlot()) {
3649 Abort(kDoPushArgumentNotImplementedForDoubleType);
3651 Register argument_reg = EmitLoadRegister(argument, at);
3652 __ push(argument_reg);
3657 void LCodeGen::DoDrop(LDrop* instr) {
3658 __ Drop(instr->count());
3662 void LCodeGen::DoThisFunction(LThisFunction* instr) {
3663 Register result = ToRegister(instr->result());
3664 __ ld(result, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3668 void LCodeGen::DoContext(LContext* instr) {
3669 // If there is a non-return use, the context must be moved to a register.
3670 Register result = ToRegister(instr->result());
3671 if (info()->IsOptimizing()) {
3672 __ ld(result, MemOperand(fp, StandardFrameConstants::kContextOffset));
3674 // If there is no frame, the context must be in cp.
3675 DCHECK(result.is(cp));
3680 void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
3681 DCHECK(ToRegister(instr->context()).is(cp));
3682 __ li(scratch0(), instr->hydrogen()->pairs());
3683 __ li(scratch1(), Operand(Smi::FromInt(instr->hydrogen()->flags())));
3684 // The context is the first argument.
3685 __ Push(cp, scratch0(), scratch1());
3686 CallRuntime(Runtime::kDeclareGlobals, 3, instr);
3690 void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
3691 int formal_parameter_count, int arity,
3692 LInstruction* instr) {
3693 bool dont_adapt_arguments =
3694 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3695 bool can_invoke_directly =
3696 dont_adapt_arguments || formal_parameter_count == arity;
3698 Register function_reg = a1;
3699 LPointerMap* pointers = instr->pointer_map();
3701 if (can_invoke_directly) {
3703 __ ld(cp, FieldMemOperand(function_reg, JSFunction::kContextOffset));
3705 // Set r0 to arguments count if adaption is not needed. Assumes that r0
3706 // is available to write to at this point.
3707 if (dont_adapt_arguments) {
3708 __ li(a0, Operand(arity));
3712 __ ld(at, FieldMemOperand(function_reg, JSFunction::kCodeEntryOffset));
3715 // Set up deoptimization.
3716 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3718 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3719 ParameterCount count(arity);
3720 ParameterCount expected(formal_parameter_count);
3721 __ InvokeFunction(function_reg, expected, count, CALL_FUNCTION, generator);
3726 void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3727 DCHECK(instr->context() != NULL);
3728 DCHECK(ToRegister(instr->context()).is(cp));
3729 Register input = ToRegister(instr->value());
3730 Register result = ToRegister(instr->result());
3731 Register scratch = scratch0();
3733 // Deoptimize if not a heap number.
3734 __ ld(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
3735 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
3736 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, scratch, Operand(at));
3739 Register exponent = scratch0();
3741 __ lwu(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3742 // Check the sign of the argument. If the argument is positive, just
3744 __ Move(result, input);
3745 __ And(at, exponent, Operand(HeapNumber::kSignMask));
3746 __ Branch(&done, eq, at, Operand(zero_reg));
3748 // Input is negative. Reverse its sign.
3749 // Preserve the value of all registers.
3751 PushSafepointRegistersScope scope(this);
3753 // Registers were saved at the safepoint, so we can use
3754 // many scratch registers.
3755 Register tmp1 = input.is(a1) ? a0 : a1;
3756 Register tmp2 = input.is(a2) ? a0 : a2;
3757 Register tmp3 = input.is(a3) ? a0 : a3;
3758 Register tmp4 = input.is(a4) ? a0 : a4;
3760 // exponent: floating point exponent value.
3762 Label allocated, slow;
3763 __ LoadRoot(tmp4, Heap::kHeapNumberMapRootIndex);
3764 __ AllocateHeapNumber(tmp1, tmp2, tmp3, tmp4, &slow);
3765 __ Branch(&allocated);
3767 // Slow case: Call the runtime system to do the number allocation.
3770 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr,
3772 // Set the pointer to the new heap number in tmp.
3775 // Restore input_reg after call to runtime.
3776 __ LoadFromSafepointRegisterSlot(input, input);
3777 __ lwu(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3779 __ bind(&allocated);
3780 // exponent: floating point exponent value.
3781 // tmp1: allocated heap number.
3782 __ And(exponent, exponent, Operand(~HeapNumber::kSignMask));
3783 __ sw(exponent, FieldMemOperand(tmp1, HeapNumber::kExponentOffset));
3784 __ lwu(tmp2, FieldMemOperand(input, HeapNumber::kMantissaOffset));
3785 __ sw(tmp2, FieldMemOperand(tmp1, HeapNumber::kMantissaOffset));
3787 __ StoreToSafepointRegisterSlot(tmp1, result);
3794 void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3795 Register input = ToRegister(instr->value());
3796 Register result = ToRegister(instr->result());
3797 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
3799 __ Branch(USE_DELAY_SLOT, &done, ge, input, Operand(zero_reg));
3800 __ mov(result, input);
3801 __ subu(result, zero_reg, input);
3802 // Overflow if result is still negative, i.e. 0x80000000.
3803 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, result, Operand(zero_reg));
3808 void LCodeGen::EmitSmiMathAbs(LMathAbs* instr) {
3809 Register input = ToRegister(instr->value());
3810 Register result = ToRegister(instr->result());
3811 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
3813 __ Branch(USE_DELAY_SLOT, &done, ge, input, Operand(zero_reg));
3814 __ mov(result, input);
3815 __ dsubu(result, zero_reg, input);
3816 // Overflow if result is still negative, i.e. 0x80000000 00000000.
3817 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, result, Operand(zero_reg));
3822 void LCodeGen::DoMathAbs(LMathAbs* instr) {
3823 // Class for deferred case.
3824 class DeferredMathAbsTaggedHeapNumber final : public LDeferredCode {
3826 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr)
3827 : LDeferredCode(codegen), instr_(instr) { }
3828 void Generate() override {
3829 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3831 LInstruction* instr() override { return instr_; }
3837 Representation r = instr->hydrogen()->value()->representation();
3839 FPURegister input = ToDoubleRegister(instr->value());
3840 FPURegister result = ToDoubleRegister(instr->result());
3841 __ abs_d(result, input);
3842 } else if (r.IsInteger32()) {
3843 EmitIntegerMathAbs(instr);
3844 } else if (r.IsSmi()) {
3845 EmitSmiMathAbs(instr);
3847 // Representation is tagged.
3848 DeferredMathAbsTaggedHeapNumber* deferred =
3849 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3850 Register input = ToRegister(instr->value());
3852 __ JumpIfNotSmi(input, deferred->entry());
3853 // If smi, handle it directly.
3854 EmitSmiMathAbs(instr);
3855 __ bind(deferred->exit());
3860 void LCodeGen::DoMathFloor(LMathFloor* instr) {
3861 DoubleRegister input = ToDoubleRegister(instr->value());
3862 Register result = ToRegister(instr->result());
3863 Register scratch1 = scratch0();
3864 Register except_flag = ToRegister(instr->temp());
3866 __ EmitFPUTruncate(kRoundToMinusInf,
3873 // Deopt if the operation did not succeed.
3874 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
3877 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3880 __ Branch(&done, ne, result, Operand(zero_reg));
3881 __ mfhc1(scratch1, input); // Get exponent/sign bits.
3882 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
3883 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
3890 void LCodeGen::DoMathRound(LMathRound* instr) {
3891 DoubleRegister input = ToDoubleRegister(instr->value());
3892 Register result = ToRegister(instr->result());
3893 DoubleRegister double_scratch1 = ToDoubleRegister(instr->temp());
3894 Register scratch = scratch0();
3895 Label done, check_sign_on_zero;
3897 // Extract exponent bits.
3898 __ mfhc1(result, input);
3901 HeapNumber::kExponentShift,
3902 HeapNumber::kExponentBits);
3904 // If the number is in ]-0.5, +0.5[, the result is +/- 0.
3906 __ Branch(&skip1, gt, scratch, Operand(HeapNumber::kExponentBias - 2));
3907 __ mov(result, zero_reg);
3908 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3909 __ Branch(&check_sign_on_zero);
3915 // The following conversion will not work with numbers
3916 // outside of ]-2^32, 2^32[.
3917 DeoptimizeIf(ge, instr, Deoptimizer::kOverflow, scratch,
3918 Operand(HeapNumber::kExponentBias + 32));
3920 // Save the original sign for later comparison.
3921 __ And(scratch, result, Operand(HeapNumber::kSignMask));
3923 __ Move(double_scratch0(), 0.5);
3924 __ add_d(double_scratch0(), input, double_scratch0());
3926 // Check sign of the result: if the sign changed, the input
3927 // value was in ]0.5, 0[ and the result should be -0.
3928 __ mfhc1(result, double_scratch0());
3929 // mfhc1 sign-extends, clear the upper bits.
3930 __ dsll32(result, result, 0);
3931 __ dsrl32(result, result, 0);
3932 __ Xor(result, result, Operand(scratch));
3933 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3934 // ARM uses 'mi' here, which is 'lt'
3935 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, result, Operand(zero_reg));
3938 // ARM uses 'mi' here, which is 'lt'
3939 // Negating it results in 'ge'
3940 __ Branch(&skip2, ge, result, Operand(zero_reg));
3941 __ mov(result, zero_reg);
3946 Register except_flag = scratch;
3947 __ EmitFPUTruncate(kRoundToMinusInf,
3954 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
3957 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3959 __ Branch(&done, ne, result, Operand(zero_reg));
3960 __ bind(&check_sign_on_zero);
3961 __ mfhc1(scratch, input); // Get exponent/sign bits.
3962 __ And(scratch, scratch, Operand(HeapNumber::kSignMask));
3963 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch,
3970 void LCodeGen::DoMathFround(LMathFround* instr) {
3971 DoubleRegister input = ToDoubleRegister(instr->value());
3972 DoubleRegister result = ToDoubleRegister(instr->result());
3973 __ cvt_s_d(result, input);
3974 __ cvt_d_s(result, result);
3978 void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3979 DoubleRegister input = ToDoubleRegister(instr->value());
3980 DoubleRegister result = ToDoubleRegister(instr->result());
3981 __ sqrt_d(result, input);
3985 void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3986 DoubleRegister input = ToDoubleRegister(instr->value());
3987 DoubleRegister result = ToDoubleRegister(instr->result());
3988 DoubleRegister temp = ToDoubleRegister(instr->temp());
3990 DCHECK(!input.is(result));
3992 // Note that according to ECMA-262 15.8.2.13:
3993 // Math.pow(-Infinity, 0.5) == Infinity
3994 // Math.sqrt(-Infinity) == NaN
3996 __ Move(temp, static_cast<double>(-V8_INFINITY));
3997 __ BranchF(USE_DELAY_SLOT, &done, NULL, eq, temp, input);
3998 // Set up Infinity in the delay slot.
3999 // result is overwritten if the branch is not taken.
4000 __ neg_d(result, temp);
4002 // Add +0 to convert -0 to +0.
4003 __ add_d(result, input, kDoubleRegZero);
4004 __ sqrt_d(result, result);
4009 void LCodeGen::DoPower(LPower* instr) {
4010 Representation exponent_type = instr->hydrogen()->right()->representation();
4011 // Having marked this as a call, we can use any registers.
4012 // Just make sure that the input/output registers are the expected ones.
4013 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
4014 DCHECK(!instr->right()->IsDoubleRegister() ||
4015 ToDoubleRegister(instr->right()).is(f4));
4016 DCHECK(!instr->right()->IsRegister() ||
4017 ToRegister(instr->right()).is(tagged_exponent));
4018 DCHECK(ToDoubleRegister(instr->left()).is(f2));
4019 DCHECK(ToDoubleRegister(instr->result()).is(f0));
4021 if (exponent_type.IsSmi()) {
4022 MathPowStub stub(isolate(), MathPowStub::TAGGED);
4024 } else if (exponent_type.IsTagged()) {
4026 __ JumpIfSmi(tagged_exponent, &no_deopt);
4027 DCHECK(!a7.is(tagged_exponent));
4028 __ lw(a7, FieldMemOperand(tagged_exponent, HeapObject::kMapOffset));
4029 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4030 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, a7, Operand(at));
4032 MathPowStub stub(isolate(), MathPowStub::TAGGED);
4034 } else if (exponent_type.IsInteger32()) {
4035 MathPowStub stub(isolate(), MathPowStub::INTEGER);
4038 DCHECK(exponent_type.IsDouble());
4039 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
4045 void LCodeGen::DoMathExp(LMathExp* instr) {
4046 DoubleRegister input = ToDoubleRegister(instr->value());
4047 DoubleRegister result = ToDoubleRegister(instr->result());
4048 DoubleRegister double_scratch1 = ToDoubleRegister(instr->double_temp());
4049 DoubleRegister double_scratch2 = double_scratch0();
4050 Register temp1 = ToRegister(instr->temp1());
4051 Register temp2 = ToRegister(instr->temp2());
4053 MathExpGenerator::EmitMathExp(
4054 masm(), input, result, double_scratch1, double_scratch2,
4055 temp1, temp2, scratch0());
4059 void LCodeGen::DoMathLog(LMathLog* instr) {
4060 __ PrepareCallCFunction(0, 1, scratch0());
4061 __ MovToFloatParameter(ToDoubleRegister(instr->value()));
4062 __ CallCFunction(ExternalReference::math_log_double_function(isolate()),
4064 __ MovFromFloatResult(ToDoubleRegister(instr->result()));
4068 void LCodeGen::DoMathClz32(LMathClz32* instr) {
4069 Register input = ToRegister(instr->value());
4070 Register result = ToRegister(instr->result());
4071 __ Clz(result, input);
4075 void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
4076 DCHECK(ToRegister(instr->context()).is(cp));
4077 DCHECK(ToRegister(instr->function()).is(a1));
4078 DCHECK(instr->HasPointerMap());
4080 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
4081 if (known_function.is_null()) {
4082 LPointerMap* pointers = instr->pointer_map();
4083 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
4084 ParameterCount count(instr->arity());
4085 __ InvokeFunction(a1, count, CALL_FUNCTION, generator);
4087 CallKnownFunction(known_function,
4088 instr->hydrogen()->formal_parameter_count(),
4089 instr->arity(), instr);
4094 void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
4095 DCHECK(ToRegister(instr->result()).is(v0));
4097 if (instr->hydrogen()->IsTailCall()) {
4098 if (NeedsEagerFrame()) __ LeaveFrame(StackFrame::INTERNAL);
4100 if (instr->target()->IsConstantOperand()) {
4101 LConstantOperand* target = LConstantOperand::cast(instr->target());
4102 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
4103 __ Jump(code, RelocInfo::CODE_TARGET);
4105 DCHECK(instr->target()->IsRegister());
4106 Register target = ToRegister(instr->target());
4107 __ Daddu(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
4111 LPointerMap* pointers = instr->pointer_map();
4112 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
4114 if (instr->target()->IsConstantOperand()) {
4115 LConstantOperand* target = LConstantOperand::cast(instr->target());
4116 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
4117 generator.BeforeCall(__ CallSize(code, RelocInfo::CODE_TARGET));
4118 __ Call(code, RelocInfo::CODE_TARGET);
4120 DCHECK(instr->target()->IsRegister());
4121 Register target = ToRegister(instr->target());
4122 generator.BeforeCall(__ CallSize(target));
4123 __ Daddu(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
4126 generator.AfterCall();
4131 void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
4132 DCHECK(ToRegister(instr->function()).is(a1));
4133 DCHECK(ToRegister(instr->result()).is(v0));
4135 if (instr->hydrogen()->pass_argument_count()) {
4136 __ li(a0, Operand(instr->arity()));
4140 __ ld(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
4142 // Load the code entry address
4143 __ ld(at, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
4146 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
4150 void LCodeGen::DoCallFunction(LCallFunction* instr) {
4151 DCHECK(ToRegister(instr->context()).is(cp));
4152 DCHECK(ToRegister(instr->function()).is(a1));
4153 DCHECK(ToRegister(instr->result()).is(v0));
4155 int arity = instr->arity();
4156 CallFunctionFlags flags = instr->hydrogen()->function_flags();
4157 if (instr->hydrogen()->HasVectorAndSlot()) {
4158 Register slot_register = ToRegister(instr->temp_slot());
4159 Register vector_register = ToRegister(instr->temp_vector());
4160 DCHECK(slot_register.is(a3));
4161 DCHECK(vector_register.is(a2));
4163 AllowDeferredHandleDereference vector_structure_check;
4164 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
4165 int index = vector->GetIndex(instr->hydrogen()->slot());
4167 __ li(vector_register, vector);
4168 __ li(slot_register, Operand(Smi::FromInt(index)));
4170 CallICState::CallType call_type =
4171 (flags & CALL_AS_METHOD) ? CallICState::METHOD : CallICState::FUNCTION;
4174 CodeFactory::CallICInOptimizedCode(isolate(), arity, call_type).code();
4175 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4177 CallFunctionStub stub(isolate(), arity, flags);
4178 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4183 void LCodeGen::DoCallNew(LCallNew* instr) {
4184 DCHECK(ToRegister(instr->context()).is(cp));
4185 DCHECK(ToRegister(instr->constructor()).is(a1));
4186 DCHECK(ToRegister(instr->result()).is(v0));
4188 __ li(a0, Operand(instr->arity()));
4189 // No cell in a2 for construct type feedback in optimized code
4190 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4191 CallConstructStub stub(isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
4192 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4196 void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
4197 DCHECK(ToRegister(instr->context()).is(cp));
4198 DCHECK(ToRegister(instr->constructor()).is(a1));
4199 DCHECK(ToRegister(instr->result()).is(v0));
4201 __ li(a0, Operand(instr->arity()));
4202 if (instr->arity() == 1) {
4203 // We only need the allocation site for the case we have a length argument.
4204 // The case may bail out to the runtime, which will determine the correct
4205 // elements kind with the site.
4206 __ li(a2, instr->hydrogen()->site());
4208 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4210 ElementsKind kind = instr->hydrogen()->elements_kind();
4211 AllocationSiteOverrideMode override_mode =
4212 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
4213 ? DISABLE_ALLOCATION_SITES
4216 if (instr->arity() == 0) {
4217 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
4218 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4219 } else if (instr->arity() == 1) {
4221 if (IsFastPackedElementsKind(kind)) {
4223 // We might need a change here,
4224 // look at the first argument.
4225 __ ld(a5, MemOperand(sp, 0));
4226 __ Branch(&packed_case, eq, a5, Operand(zero_reg));
4228 ElementsKind holey_kind = GetHoleyElementsKind(kind);
4229 ArraySingleArgumentConstructorStub stub(isolate(),
4232 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4234 __ bind(&packed_case);
4237 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
4238 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4241 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
4242 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4247 void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
4248 CallRuntime(instr->function(), instr->arity(), instr);
4252 void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
4253 Register function = ToRegister(instr->function());
4254 Register code_object = ToRegister(instr->code_object());
4255 __ Daddu(code_object, code_object,
4256 Operand(Code::kHeaderSize - kHeapObjectTag));
4258 FieldMemOperand(function, JSFunction::kCodeEntryOffset));
4262 void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
4263 Register result = ToRegister(instr->result());
4264 Register base = ToRegister(instr->base_object());
4265 if (instr->offset()->IsConstantOperand()) {
4266 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
4267 __ Daddu(result, base, Operand(ToInteger32(offset)));
4269 Register offset = ToRegister(instr->offset());
4270 __ Daddu(result, base, offset);
4275 void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
4276 Representation representation = instr->representation();
4278 Register object = ToRegister(instr->object());
4279 Register scratch2 = scratch1();
4280 Register scratch1 = scratch0();
4282 HObjectAccess access = instr->hydrogen()->access();
4283 int offset = access.offset();
4284 if (access.IsExternalMemory()) {
4285 Register value = ToRegister(instr->value());
4286 MemOperand operand = MemOperand(object, offset);
4287 __ Store(value, operand, representation);
4291 __ AssertNotSmi(object);
4293 DCHECK(!representation.IsSmi() ||
4294 !instr->value()->IsConstantOperand() ||
4295 IsSmi(LConstantOperand::cast(instr->value())));
4296 if (!FLAG_unbox_double_fields && representation.IsDouble()) {
4297 DCHECK(access.IsInobject());
4298 DCHECK(!instr->hydrogen()->has_transition());
4299 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4300 DoubleRegister value = ToDoubleRegister(instr->value());
4301 __ sdc1(value, FieldMemOperand(object, offset));
4305 if (instr->hydrogen()->has_transition()) {
4306 Handle<Map> transition = instr->hydrogen()->transition_map();
4307 AddDeprecationDependency(transition);
4308 __ li(scratch1, Operand(transition));
4309 __ sd(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
4310 if (instr->hydrogen()->NeedsWriteBarrierForMap()) {
4311 Register temp = ToRegister(instr->temp());
4312 // Update the write barrier for the map field.
4313 __ RecordWriteForMap(object,
4322 Register destination = object;
4323 if (!access.IsInobject()) {
4324 destination = scratch1;
4325 __ ld(destination, FieldMemOperand(object, JSObject::kPropertiesOffset));
4328 if (representation.IsSmi() && SmiValuesAre32Bits() &&
4329 instr->hydrogen()->value()->representation().IsInteger32()) {
4330 DCHECK(instr->hydrogen()->store_mode() == STORE_TO_INITIALIZED_ENTRY);
4331 if (FLAG_debug_code) {
4332 __ Load(scratch2, FieldMemOperand(destination, offset), representation);
4333 __ AssertSmi(scratch2);
4335 // Store int value directly to upper half of the smi.
4336 offset += kPointerSize / 2;
4337 representation = Representation::Integer32();
4339 MemOperand operand = FieldMemOperand(destination, offset);
4341 if (FLAG_unbox_double_fields && representation.IsDouble()) {
4342 DCHECK(access.IsInobject());
4343 DoubleRegister value = ToDoubleRegister(instr->value());
4344 __ sdc1(value, operand);
4346 DCHECK(instr->value()->IsRegister());
4347 Register value = ToRegister(instr->value());
4348 __ Store(value, operand, representation);
4351 if (instr->hydrogen()->NeedsWriteBarrier()) {
4352 // Update the write barrier for the object for in-object properties.
4353 Register value = ToRegister(instr->value());
4354 __ RecordWriteField(destination,
4360 EMIT_REMEMBERED_SET,
4361 instr->hydrogen()->SmiCheckForWriteBarrier(),
4362 instr->hydrogen()->PointersToHereCheckForValue());
4367 void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
4368 DCHECK(ToRegister(instr->context()).is(cp));
4369 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4370 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4372 if (instr->hydrogen()->HasVectorAndSlot()) {
4373 EmitVectorStoreICRegisters<LStoreNamedGeneric>(instr);
4376 __ li(StoreDescriptor::NameRegister(), Operand(instr->name()));
4377 Handle<Code> ic = CodeFactory::StoreICInOptimizedCode(
4378 isolate(), instr->language_mode(),
4379 instr->hydrogen()->initialization_state()).code();
4380 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4384 void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
4385 Condition cc = instr->hydrogen()->allow_equality() ? hi : hs;
4386 Operand operand((int64_t)0);
4388 if (instr->index()->IsConstantOperand()) {
4389 operand = ToOperand(instr->index());
4390 reg = ToRegister(instr->length());
4391 cc = CommuteCondition(cc);
4393 reg = ToRegister(instr->index());
4394 operand = ToOperand(instr->length());
4396 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
4398 __ Branch(&done, NegateCondition(cc), reg, operand);
4399 __ stop("eliminated bounds check failed");
4402 DeoptimizeIf(cc, instr, Deoptimizer::kOutOfBounds, reg, operand);
4407 void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
4408 Register external_pointer = ToRegister(instr->elements());
4409 Register key = no_reg;
4410 ElementsKind elements_kind = instr->elements_kind();
4411 bool key_is_constant = instr->key()->IsConstantOperand();
4412 int constant_key = 0;
4413 if (key_is_constant) {
4414 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4415 if (constant_key & 0xF0000000) {
4416 Abort(kArrayIndexConstantValueTooBig);
4419 key = ToRegister(instr->key());
4421 int element_size_shift = ElementsKindToShiftSize(elements_kind);
4422 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4423 ? (element_size_shift - (kSmiTagSize + kSmiShiftSize))
4424 : element_size_shift;
4425 int base_offset = instr->base_offset();
4427 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4428 elements_kind == FLOAT32_ELEMENTS ||
4429 elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
4430 elements_kind == FLOAT64_ELEMENTS) {
4431 Register address = scratch0();
4432 FPURegister value(ToDoubleRegister(instr->value()));
4433 if (key_is_constant) {
4434 if (constant_key != 0) {
4435 __ Daddu(address, external_pointer,
4436 Operand(constant_key << element_size_shift));
4438 address = external_pointer;
4441 if (shift_size < 0) {
4442 if (shift_size == -32) {
4443 __ dsra32(address, key, 0);
4445 __ dsra(address, key, -shift_size);
4448 __ dsll(address, key, shift_size);
4450 __ Daddu(address, external_pointer, address);
4453 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4454 elements_kind == FLOAT32_ELEMENTS) {
4455 __ cvt_s_d(double_scratch0(), value);
4456 __ swc1(double_scratch0(), MemOperand(address, base_offset));
4457 } else { // Storing doubles, not floats.
4458 __ sdc1(value, MemOperand(address, base_offset));
4461 Register value(ToRegister(instr->value()));
4462 MemOperand mem_operand = PrepareKeyedOperand(
4463 key, external_pointer, key_is_constant, constant_key,
4464 element_size_shift, shift_size,
4466 switch (elements_kind) {
4467 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
4468 case EXTERNAL_INT8_ELEMENTS:
4469 case EXTERNAL_UINT8_ELEMENTS:
4470 case UINT8_ELEMENTS:
4471 case UINT8_CLAMPED_ELEMENTS:
4473 __ sb(value, mem_operand);
4475 case EXTERNAL_INT16_ELEMENTS:
4476 case EXTERNAL_UINT16_ELEMENTS:
4477 case INT16_ELEMENTS:
4478 case UINT16_ELEMENTS:
4479 __ sh(value, mem_operand);
4481 case EXTERNAL_INT32_ELEMENTS:
4482 case EXTERNAL_UINT32_ELEMENTS:
4483 case INT32_ELEMENTS:
4484 case UINT32_ELEMENTS:
4485 __ sw(value, mem_operand);
4487 case FLOAT32_ELEMENTS:
4488 case FLOAT64_ELEMENTS:
4489 case EXTERNAL_FLOAT32_ELEMENTS:
4490 case EXTERNAL_FLOAT64_ELEMENTS:
4491 case FAST_DOUBLE_ELEMENTS:
4493 case FAST_SMI_ELEMENTS:
4494 case FAST_HOLEY_DOUBLE_ELEMENTS:
4495 case FAST_HOLEY_ELEMENTS:
4496 case FAST_HOLEY_SMI_ELEMENTS:
4497 case DICTIONARY_ELEMENTS:
4498 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
4499 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
4507 void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
4508 DoubleRegister value = ToDoubleRegister(instr->value());
4509 Register elements = ToRegister(instr->elements());
4510 Register scratch = scratch0();
4511 DoubleRegister double_scratch = double_scratch0();
4512 bool key_is_constant = instr->key()->IsConstantOperand();
4513 int base_offset = instr->base_offset();
4514 Label not_nan, done;
4516 // Calculate the effective address of the slot in the array to store the
4518 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
4519 if (key_is_constant) {
4520 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4521 if (constant_key & 0xF0000000) {
4522 Abort(kArrayIndexConstantValueTooBig);
4524 __ Daddu(scratch, elements,
4525 Operand((constant_key << element_size_shift) + base_offset));
4527 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4528 ? (element_size_shift - (kSmiTagSize + kSmiShiftSize))
4529 : element_size_shift;
4530 __ Daddu(scratch, elements, Operand(base_offset));
4531 DCHECK((shift_size == 3) || (shift_size == -29));
4532 if (shift_size == 3) {
4533 __ dsll(at, ToRegister(instr->key()), 3);
4534 } else if (shift_size == -29) {
4535 __ dsra(at, ToRegister(instr->key()), 29);
4537 __ Daddu(scratch, scratch, at);
4540 if (instr->NeedsCanonicalization()) {
4541 __ FPUCanonicalizeNaN(double_scratch, value);
4542 __ sdc1(double_scratch, MemOperand(scratch, 0));
4544 __ sdc1(value, MemOperand(scratch, 0));
4549 void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4550 Register value = ToRegister(instr->value());
4551 Register elements = ToRegister(instr->elements());
4552 Register key = instr->key()->IsRegister() ? ToRegister(instr->key())
4554 Register scratch = scratch0();
4555 Register store_base = scratch;
4556 int offset = instr->base_offset();
4559 if (instr->key()->IsConstantOperand()) {
4560 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4561 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
4562 offset += ToInteger32(const_operand) * kPointerSize;
4563 store_base = elements;
4565 // Even though the HLoadKeyed instruction forces the input
4566 // representation for the key to be an integer, the input gets replaced
4567 // during bound check elimination with the index argument to the bounds
4568 // check, which can be tagged, so that case must be handled here, too.
4569 if (instr->hydrogen()->key()->representation().IsSmi()) {
4570 __ SmiScale(scratch, key, kPointerSizeLog2);
4571 __ daddu(store_base, elements, scratch);
4573 __ dsll(scratch, key, kPointerSizeLog2);
4574 __ daddu(store_base, elements, scratch);
4578 Representation representation = instr->hydrogen()->value()->representation();
4579 if (representation.IsInteger32() && SmiValuesAre32Bits()) {
4580 DCHECK(instr->hydrogen()->store_mode() == STORE_TO_INITIALIZED_ENTRY);
4581 DCHECK(instr->hydrogen()->elements_kind() == FAST_SMI_ELEMENTS);
4582 if (FLAG_debug_code) {
4583 Register temp = scratch1();
4584 __ Load(temp, MemOperand(store_base, offset), Representation::Smi());
4588 // Store int value directly to upper half of the smi.
4589 STATIC_ASSERT(kSmiTag == 0);
4590 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 32);
4591 offset += kPointerSize / 2;
4592 representation = Representation::Integer32();
4595 __ Store(value, MemOperand(store_base, offset), representation);
4597 if (instr->hydrogen()->NeedsWriteBarrier()) {
4598 SmiCheck check_needed =
4599 instr->hydrogen()->value()->type().IsHeapObject()
4600 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4601 // Compute address of modified element and store it into key register.
4602 __ Daddu(key, store_base, Operand(offset));
4603 __ RecordWrite(elements,
4608 EMIT_REMEMBERED_SET,
4610 instr->hydrogen()->PointersToHereCheckForValue());
4615 void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4616 // By cases: external, fast double
4617 if (instr->is_typed_elements()) {
4618 DoStoreKeyedExternalArray(instr);
4619 } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4620 DoStoreKeyedFixedDoubleArray(instr);
4622 DoStoreKeyedFixedArray(instr);
4627 void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4628 DCHECK(ToRegister(instr->context()).is(cp));
4629 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4630 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4631 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4633 if (instr->hydrogen()->HasVectorAndSlot()) {
4634 EmitVectorStoreICRegisters<LStoreKeyedGeneric>(instr);
4637 Handle<Code> ic = CodeFactory::KeyedStoreICInOptimizedCode(
4638 isolate(), instr->language_mode(),
4639 instr->hydrogen()->initialization_state()).code();
4640 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4644 void LCodeGen::DoMaybeGrowElements(LMaybeGrowElements* instr) {
4645 class DeferredMaybeGrowElements final : public LDeferredCode {
4647 DeferredMaybeGrowElements(LCodeGen* codegen, LMaybeGrowElements* instr)
4648 : LDeferredCode(codegen), instr_(instr) {}
4649 void Generate() override { codegen()->DoDeferredMaybeGrowElements(instr_); }
4650 LInstruction* instr() override { return instr_; }
4653 LMaybeGrowElements* instr_;
4656 Register result = v0;
4657 DeferredMaybeGrowElements* deferred =
4658 new (zone()) DeferredMaybeGrowElements(this, instr);
4659 LOperand* key = instr->key();
4660 LOperand* current_capacity = instr->current_capacity();
4662 DCHECK(instr->hydrogen()->key()->representation().IsInteger32());
4663 DCHECK(instr->hydrogen()->current_capacity()->representation().IsInteger32());
4664 DCHECK(key->IsConstantOperand() || key->IsRegister());
4665 DCHECK(current_capacity->IsConstantOperand() ||
4666 current_capacity->IsRegister());
4668 if (key->IsConstantOperand() && current_capacity->IsConstantOperand()) {
4669 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4670 int32_t constant_capacity =
4671 ToInteger32(LConstantOperand::cast(current_capacity));
4672 if (constant_key >= constant_capacity) {
4674 __ jmp(deferred->entry());
4676 } else if (key->IsConstantOperand()) {
4677 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4678 __ Branch(deferred->entry(), le, ToRegister(current_capacity),
4679 Operand(constant_key));
4680 } else if (current_capacity->IsConstantOperand()) {
4681 int32_t constant_capacity =
4682 ToInteger32(LConstantOperand::cast(current_capacity));
4683 __ Branch(deferred->entry(), ge, ToRegister(key),
4684 Operand(constant_capacity));
4686 __ Branch(deferred->entry(), ge, ToRegister(key),
4687 Operand(ToRegister(current_capacity)));
4690 if (instr->elements()->IsRegister()) {
4691 __ mov(result, ToRegister(instr->elements()));
4693 __ ld(result, ToMemOperand(instr->elements()));
4696 __ bind(deferred->exit());
4700 void LCodeGen::DoDeferredMaybeGrowElements(LMaybeGrowElements* instr) {
4701 // TODO(3095996): Get rid of this. For now, we need to make the
4702 // result register contain a valid pointer because it is already
4703 // contained in the register pointer map.
4704 Register result = v0;
4705 __ mov(result, zero_reg);
4707 // We have to call a stub.
4709 PushSafepointRegistersScope scope(this);
4710 if (instr->object()->IsRegister()) {
4711 __ mov(result, ToRegister(instr->object()));
4713 __ ld(result, ToMemOperand(instr->object()));
4716 LOperand* key = instr->key();
4717 if (key->IsConstantOperand()) {
4718 __ li(a3, Operand(ToSmi(LConstantOperand::cast(key))));
4720 __ mov(a3, ToRegister(key));
4724 GrowArrayElementsStub stub(isolate(), instr->hydrogen()->is_js_array(),
4725 instr->hydrogen()->kind());
4728 RecordSafepointWithLazyDeopt(
4729 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
4730 __ StoreToSafepointRegisterSlot(result, result);
4733 // Deopt on smi, which means the elements array changed to dictionary mode.
4734 __ SmiTst(result, at);
4735 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
4739 void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4740 Register object_reg = ToRegister(instr->object());
4741 Register scratch = scratch0();
4743 Handle<Map> from_map = instr->original_map();
4744 Handle<Map> to_map = instr->transitioned_map();
4745 ElementsKind from_kind = instr->from_kind();
4746 ElementsKind to_kind = instr->to_kind();
4748 Label not_applicable;
4749 __ ld(scratch, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4750 __ Branch(¬_applicable, ne, scratch, Operand(from_map));
4752 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
4753 Register new_map_reg = ToRegister(instr->new_map_temp());
4754 __ li(new_map_reg, Operand(to_map));
4755 __ sd(new_map_reg, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4757 __ RecordWriteForMap(object_reg,
4763 DCHECK(object_reg.is(a0));
4764 DCHECK(ToRegister(instr->context()).is(cp));
4765 PushSafepointRegistersScope scope(this);
4766 __ li(a1, Operand(to_map));
4767 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4768 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4770 RecordSafepointWithRegisters(
4771 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
4773 __ bind(¬_applicable);
4777 void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4778 Register object = ToRegister(instr->object());
4779 Register temp = ToRegister(instr->temp());
4780 Label no_memento_found;
4781 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found,
4782 ne, &no_memento_found);
4783 DeoptimizeIf(al, instr, Deoptimizer::kMementoFound);
4784 __ bind(&no_memento_found);
4788 void LCodeGen::DoStringAdd(LStringAdd* instr) {
4789 DCHECK(ToRegister(instr->context()).is(cp));
4790 DCHECK(ToRegister(instr->left()).is(a1));
4791 DCHECK(ToRegister(instr->right()).is(a0));
4792 StringAddStub stub(isolate(),
4793 instr->hydrogen()->flags(),
4794 instr->hydrogen()->pretenure_flag());
4795 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4799 void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
4800 class DeferredStringCharCodeAt final : public LDeferredCode {
4802 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
4803 : LDeferredCode(codegen), instr_(instr) { }
4804 void Generate() override { codegen()->DoDeferredStringCharCodeAt(instr_); }
4805 LInstruction* instr() override { return instr_; }
4808 LStringCharCodeAt* instr_;
4811 DeferredStringCharCodeAt* deferred =
4812 new(zone()) DeferredStringCharCodeAt(this, instr);
4813 StringCharLoadGenerator::Generate(masm(),
4814 ToRegister(instr->string()),
4815 ToRegister(instr->index()),
4816 ToRegister(instr->result()),
4818 __ bind(deferred->exit());
4822 void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4823 Register string = ToRegister(instr->string());
4824 Register result = ToRegister(instr->result());
4825 Register scratch = scratch0();
4827 // TODO(3095996): Get rid of this. For now, we need to make the
4828 // result register contain a valid pointer because it is already
4829 // contained in the register pointer map.
4830 __ mov(result, zero_reg);
4832 PushSafepointRegistersScope scope(this);
4834 // Push the index as a smi. This is safe because of the checks in
4835 // DoStringCharCodeAt above.
4836 if (instr->index()->IsConstantOperand()) {
4837 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
4838 __ Daddu(scratch, zero_reg, Operand(Smi::FromInt(const_index)));
4841 Register index = ToRegister(instr->index());
4845 CallRuntimeFromDeferred(Runtime::kStringCharCodeAtRT, 2, instr,
4849 __ StoreToSafepointRegisterSlot(v0, result);
4853 void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
4854 class DeferredStringCharFromCode final : public LDeferredCode {
4856 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
4857 : LDeferredCode(codegen), instr_(instr) { }
4858 void Generate() override {
4859 codegen()->DoDeferredStringCharFromCode(instr_);
4861 LInstruction* instr() override { return instr_; }
4864 LStringCharFromCode* instr_;
4867 DeferredStringCharFromCode* deferred =
4868 new(zone()) DeferredStringCharFromCode(this, instr);
4870 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
4871 Register char_code = ToRegister(instr->char_code());
4872 Register result = ToRegister(instr->result());
4873 Register scratch = scratch0();
4874 DCHECK(!char_code.is(result));
4876 __ Branch(deferred->entry(), hi,
4877 char_code, Operand(String::kMaxOneByteCharCode));
4878 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
4879 __ dsll(scratch, char_code, kPointerSizeLog2);
4880 __ Daddu(result, result, scratch);
4881 __ ld(result, FieldMemOperand(result, FixedArray::kHeaderSize));
4882 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
4883 __ Branch(deferred->entry(), eq, result, Operand(scratch));
4884 __ bind(deferred->exit());
4888 void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4889 Register char_code = ToRegister(instr->char_code());
4890 Register result = ToRegister(instr->result());
4892 // TODO(3095996): Get rid of this. For now, we need to make the
4893 // result register contain a valid pointer because it is already
4894 // contained in the register pointer map.
4895 __ mov(result, zero_reg);
4897 PushSafepointRegistersScope scope(this);
4898 __ SmiTag(char_code);
4900 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr, instr->context());
4901 __ StoreToSafepointRegisterSlot(v0, result);
4905 void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
4906 LOperand* input = instr->value();
4907 DCHECK(input->IsRegister() || input->IsStackSlot());
4908 LOperand* output = instr->result();
4909 DCHECK(output->IsDoubleRegister());
4910 FPURegister single_scratch = double_scratch0().low();
4911 if (input->IsStackSlot()) {
4912 Register scratch = scratch0();
4913 __ ld(scratch, ToMemOperand(input));
4914 __ mtc1(scratch, single_scratch);
4916 __ mtc1(ToRegister(input), single_scratch);
4918 __ cvt_d_w(ToDoubleRegister(output), single_scratch);
4922 void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4923 LOperand* input = instr->value();
4924 LOperand* output = instr->result();
4926 FPURegister dbl_scratch = double_scratch0();
4927 __ mtc1(ToRegister(input), dbl_scratch);
4928 __ Cvt_d_uw(ToDoubleRegister(output), dbl_scratch, f22); // TODO(plind): f22?
4932 void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4933 class DeferredNumberTagU final : public LDeferredCode {
4935 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4936 : LDeferredCode(codegen), instr_(instr) { }
4937 void Generate() override {
4938 codegen()->DoDeferredNumberTagIU(instr_,
4944 LInstruction* instr() override { return instr_; }
4947 LNumberTagU* instr_;
4950 Register input = ToRegister(instr->value());
4951 Register result = ToRegister(instr->result());
4953 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4954 __ Branch(deferred->entry(), hi, input, Operand(Smi::kMaxValue));
4955 __ SmiTag(result, input);
4956 __ bind(deferred->exit());
4960 void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4964 IntegerSignedness signedness) {
4966 Register src = ToRegister(value);
4967 Register dst = ToRegister(instr->result());
4968 Register tmp1 = scratch0();
4969 Register tmp2 = ToRegister(temp1);
4970 Register tmp3 = ToRegister(temp2);
4971 DoubleRegister dbl_scratch = double_scratch0();
4973 if (signedness == SIGNED_INT32) {
4974 // There was overflow, so bits 30 and 31 of the original integer
4975 // disagree. Try to allocate a heap number in new space and store
4976 // the value in there. If that fails, call the runtime system.
4978 __ SmiUntag(src, dst);
4979 __ Xor(src, src, Operand(0x80000000));
4981 __ mtc1(src, dbl_scratch);
4982 __ cvt_d_w(dbl_scratch, dbl_scratch);
4984 __ mtc1(src, dbl_scratch);
4985 __ Cvt_d_uw(dbl_scratch, dbl_scratch, f22);
4988 if (FLAG_inline_new) {
4989 __ LoadRoot(tmp3, Heap::kHeapNumberMapRootIndex);
4990 __ AllocateHeapNumber(dst, tmp1, tmp2, tmp3, &slow, TAG_RESULT);
4994 // Slow case: Call the runtime system to do the number allocation.
4997 // TODO(3095996): Put a valid pointer value in the stack slot where the
4998 // result register is stored, as this register is in the pointer map, but
4999 // contains an integer value.
5000 __ mov(dst, zero_reg);
5001 // Preserve the value of all registers.
5002 PushSafepointRegistersScope scope(this);
5004 // NumberTagI and NumberTagD use the context from the frame, rather than
5005 // the environment's HContext or HInlinedContext value.
5006 // They only call Runtime::kAllocateHeapNumber.
5007 // The corresponding HChange instructions are added in a phase that does
5008 // not have easy access to the local context.
5009 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
5010 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
5011 RecordSafepointWithRegisters(
5012 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
5013 __ StoreToSafepointRegisterSlot(v0, dst);
5016 // Done. Put the value in dbl_scratch into the value of the allocated heap
5019 __ sdc1(dbl_scratch, FieldMemOperand(dst, HeapNumber::kValueOffset));
5023 void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
5024 class DeferredNumberTagD final : public LDeferredCode {
5026 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
5027 : LDeferredCode(codegen), instr_(instr) { }
5028 void Generate() override { codegen()->DoDeferredNumberTagD(instr_); }
5029 LInstruction* instr() override { return instr_; }
5032 LNumberTagD* instr_;
5035 DoubleRegister input_reg = ToDoubleRegister(instr->value());
5036 Register scratch = scratch0();
5037 Register reg = ToRegister(instr->result());
5038 Register temp1 = ToRegister(instr->temp());
5039 Register temp2 = ToRegister(instr->temp2());
5041 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
5042 if (FLAG_inline_new) {
5043 __ LoadRoot(scratch, Heap::kHeapNumberMapRootIndex);
5044 // We want the untagged address first for performance
5045 __ AllocateHeapNumber(reg, temp1, temp2, scratch, deferred->entry(),
5048 __ Branch(deferred->entry());
5050 __ bind(deferred->exit());
5051 __ sdc1(input_reg, MemOperand(reg, HeapNumber::kValueOffset));
5052 // Now that we have finished with the object's real address tag it
5053 __ Daddu(reg, reg, kHeapObjectTag);
5057 void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
5058 // TODO(3095996): Get rid of this. For now, we need to make the
5059 // result register contain a valid pointer because it is already
5060 // contained in the register pointer map.
5061 Register reg = ToRegister(instr->result());
5062 __ mov(reg, zero_reg);
5064 PushSafepointRegistersScope scope(this);
5065 // NumberTagI and NumberTagD use the context from the frame, rather than
5066 // the environment's HContext or HInlinedContext value.
5067 // They only call Runtime::kAllocateHeapNumber.
5068 // The corresponding HChange instructions are added in a phase that does
5069 // not have easy access to the local context.
5070 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
5071 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
5072 RecordSafepointWithRegisters(
5073 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
5074 __ Dsubu(v0, v0, kHeapObjectTag);
5075 __ StoreToSafepointRegisterSlot(v0, reg);
5079 void LCodeGen::DoSmiTag(LSmiTag* instr) {
5080 HChange* hchange = instr->hydrogen();
5081 Register input = ToRegister(instr->value());
5082 Register output = ToRegister(instr->result());
5083 if (hchange->CheckFlag(HValue::kCanOverflow) &&
5084 hchange->value()->CheckFlag(HValue::kUint32)) {
5085 __ And(at, input, Operand(0x80000000));
5086 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow, at, Operand(zero_reg));
5088 if (hchange->CheckFlag(HValue::kCanOverflow) &&
5089 !hchange->value()->CheckFlag(HValue::kUint32)) {
5090 __ SmiTagCheckOverflow(output, input, at);
5091 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, at, Operand(zero_reg));
5093 __ SmiTag(output, input);
5098 void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
5099 Register scratch = scratch0();
5100 Register input = ToRegister(instr->value());
5101 Register result = ToRegister(instr->result());
5102 if (instr->needs_check()) {
5103 STATIC_ASSERT(kHeapObjectTag == 1);
5104 // If the input is a HeapObject, value of scratch won't be zero.
5105 __ And(scratch, input, Operand(kHeapObjectTag));
5106 __ SmiUntag(result, input);
5107 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi, scratch, Operand(zero_reg));
5109 __ SmiUntag(result, input);
5114 void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
5115 DoubleRegister result_reg,
5116 NumberUntagDMode mode) {
5117 bool can_convert_undefined_to_nan =
5118 instr->hydrogen()->can_convert_undefined_to_nan();
5119 bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
5121 Register scratch = scratch0();
5122 Label convert, load_smi, done;
5123 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
5125 __ UntagAndJumpIfSmi(scratch, input_reg, &load_smi);
5126 // Heap number map check.
5127 __ ld(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
5128 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5129 if (can_convert_undefined_to_nan) {
5130 __ Branch(&convert, ne, scratch, Operand(at));
5132 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, scratch,
5135 // Load heap number.
5136 __ ldc1(result_reg, FieldMemOperand(input_reg, HeapNumber::kValueOffset));
5137 if (deoptimize_on_minus_zero) {
5138 __ mfc1(at, result_reg);
5139 __ Branch(&done, ne, at, Operand(zero_reg));
5140 __ mfhc1(scratch, result_reg); // Get exponent/sign bits.
5141 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, scratch,
5142 Operand(HeapNumber::kSignMask));
5145 if (can_convert_undefined_to_nan) {
5147 // Convert undefined (and hole) to NaN.
5148 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5149 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefined, input_reg,
5151 __ LoadRoot(scratch, Heap::kNanValueRootIndex);
5152 __ ldc1(result_reg, FieldMemOperand(scratch, HeapNumber::kValueOffset));
5156 __ SmiUntag(scratch, input_reg);
5157 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
5159 // Smi to double register conversion
5161 // scratch: untagged value of input_reg
5162 __ mtc1(scratch, result_reg);
5163 __ cvt_d_w(result_reg, result_reg);
5168 void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) {
5169 Register input_reg = ToRegister(instr->value());
5170 Register scratch1 = scratch0();
5171 Register scratch2 = ToRegister(instr->temp());
5172 DoubleRegister double_scratch = double_scratch0();
5173 DoubleRegister double_scratch2 = ToDoubleRegister(instr->temp2());
5175 DCHECK(!scratch1.is(input_reg) && !scratch1.is(scratch2));
5176 DCHECK(!scratch2.is(input_reg) && !scratch2.is(scratch1));
5180 // The input is a tagged HeapObject.
5181 // Heap number map check.
5182 __ ld(scratch1, FieldMemOperand(input_reg, HeapObject::kMapOffset));
5183 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5184 // This 'at' value and scratch1 map value are used for tests in both clauses
5187 if (instr->truncating()) {
5188 // Performs a truncating conversion of a floating point number as used by
5189 // the JS bitwise operations.
5190 Label no_heap_number, check_bools, check_false;
5191 // Check HeapNumber map.
5192 __ Branch(USE_DELAY_SLOT, &no_heap_number, ne, scratch1, Operand(at));
5193 __ mov(scratch2, input_reg); // In delay slot.
5194 __ TruncateHeapNumberToI(input_reg, scratch2);
5197 // Check for Oddballs. Undefined/False is converted to zero and True to one
5198 // for truncating conversions.
5199 __ bind(&no_heap_number);
5200 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5201 __ Branch(&check_bools, ne, input_reg, Operand(at));
5202 DCHECK(ToRegister(instr->result()).is(input_reg));
5203 __ Branch(USE_DELAY_SLOT, &done);
5204 __ mov(input_reg, zero_reg); // In delay slot.
5206 __ bind(&check_bools);
5207 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5208 __ Branch(&check_false, ne, scratch2, Operand(at));
5209 __ Branch(USE_DELAY_SLOT, &done);
5210 __ li(input_reg, Operand(1)); // In delay slot.
5212 __ bind(&check_false);
5213 __ LoadRoot(at, Heap::kFalseValueRootIndex);
5214 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefinedBoolean,
5215 scratch2, Operand(at));
5216 __ Branch(USE_DELAY_SLOT, &done);
5217 __ mov(input_reg, zero_reg); // In delay slot.
5219 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, scratch1,
5222 // Load the double value.
5223 __ ldc1(double_scratch,
5224 FieldMemOperand(input_reg, HeapNumber::kValueOffset));
5226 Register except_flag = scratch2;
5227 __ EmitFPUTruncate(kRoundToZero,
5233 kCheckForInexactConversion);
5235 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
5238 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5239 __ Branch(&done, ne, input_reg, Operand(zero_reg));
5241 __ mfhc1(scratch1, double_scratch); // Get exponent/sign bits.
5242 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
5243 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
5251 void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
5252 class DeferredTaggedToI final : public LDeferredCode {
5254 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
5255 : LDeferredCode(codegen), instr_(instr) { }
5256 void Generate() override { codegen()->DoDeferredTaggedToI(instr_); }
5257 LInstruction* instr() override { return instr_; }
5263 LOperand* input = instr->value();
5264 DCHECK(input->IsRegister());
5265 DCHECK(input->Equals(instr->result()));
5267 Register input_reg = ToRegister(input);
5269 if (instr->hydrogen()->value()->representation().IsSmi()) {
5270 __ SmiUntag(input_reg);
5272 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
5274 // Let the deferred code handle the HeapObject case.
5275 __ JumpIfNotSmi(input_reg, deferred->entry());
5277 // Smi to int32 conversion.
5278 __ SmiUntag(input_reg);
5279 __ bind(deferred->exit());
5284 void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
5285 LOperand* input = instr->value();
5286 DCHECK(input->IsRegister());
5287 LOperand* result = instr->result();
5288 DCHECK(result->IsDoubleRegister());
5290 Register input_reg = ToRegister(input);
5291 DoubleRegister result_reg = ToDoubleRegister(result);
5293 HValue* value = instr->hydrogen()->value();
5294 NumberUntagDMode mode = value->representation().IsSmi()
5295 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
5297 EmitNumberUntagD(instr, input_reg, result_reg, mode);
5301 void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
5302 Register result_reg = ToRegister(instr->result());
5303 Register scratch1 = scratch0();
5304 DoubleRegister double_input = ToDoubleRegister(instr->value());
5306 if (instr->truncating()) {
5307 __ TruncateDoubleToI(result_reg, double_input);
5309 Register except_flag = LCodeGen::scratch1();
5311 __ EmitFPUTruncate(kRoundToMinusInf,
5317 kCheckForInexactConversion);
5319 // Deopt if the operation did not succeed (except_flag != 0).
5320 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
5323 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5325 __ Branch(&done, ne, result_reg, Operand(zero_reg));
5326 __ mfhc1(scratch1, double_input); // Get exponent/sign bits.
5327 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
5328 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
5336 void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
5337 Register result_reg = ToRegister(instr->result());
5338 Register scratch1 = LCodeGen::scratch0();
5339 DoubleRegister double_input = ToDoubleRegister(instr->value());
5341 if (instr->truncating()) {
5342 __ TruncateDoubleToI(result_reg, double_input);
5344 Register except_flag = LCodeGen::scratch1();
5346 __ EmitFPUTruncate(kRoundToMinusInf,
5352 kCheckForInexactConversion);
5354 // Deopt if the operation did not succeed (except_flag != 0).
5355 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
5358 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5360 __ Branch(&done, ne, result_reg, Operand(zero_reg));
5361 __ mfhc1(scratch1, double_input); // Get exponent/sign bits.
5362 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
5363 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
5368 __ SmiTag(result_reg, result_reg);
5372 void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
5373 LOperand* input = instr->value();
5374 __ SmiTst(ToRegister(input), at);
5375 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi, at, Operand(zero_reg));
5379 void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
5380 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
5381 LOperand* input = instr->value();
5382 __ SmiTst(ToRegister(input), at);
5383 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
5388 void LCodeGen::DoCheckArrayBufferNotNeutered(
5389 LCheckArrayBufferNotNeutered* instr) {
5390 Register view = ToRegister(instr->view());
5391 Register scratch = scratch0();
5393 __ ld(scratch, FieldMemOperand(view, JSArrayBufferView::kBufferOffset));
5394 __ lw(scratch, FieldMemOperand(scratch, JSArrayBuffer::kBitFieldOffset));
5395 __ And(at, scratch, 1 << JSArrayBuffer::WasNeutered::kShift);
5396 DeoptimizeIf(ne, instr, Deoptimizer::kOutOfBounds, at, Operand(zero_reg));
5400 void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
5401 Register input = ToRegister(instr->value());
5402 Register scratch = scratch0();
5404 __ GetObjectType(input, scratch, scratch);
5406 if (instr->hydrogen()->is_interval_check()) {
5409 instr->hydrogen()->GetCheckInterval(&first, &last);
5411 // If there is only one type in the interval check for equality.
5412 if (first == last) {
5413 DeoptimizeIf(ne, instr, Deoptimizer::kWrongInstanceType, scratch,
5416 DeoptimizeIf(lo, instr, Deoptimizer::kWrongInstanceType, scratch,
5418 // Omit check for the last type.
5419 if (last != LAST_TYPE) {
5420 DeoptimizeIf(hi, instr, Deoptimizer::kWrongInstanceType, scratch,
5427 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
5429 if (base::bits::IsPowerOfTwo32(mask)) {
5430 DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
5431 __ And(at, scratch, mask);
5432 DeoptimizeIf(tag == 0 ? ne : eq, instr, Deoptimizer::kWrongInstanceType,
5433 at, Operand(zero_reg));
5435 __ And(scratch, scratch, Operand(mask));
5436 DeoptimizeIf(ne, instr, Deoptimizer::kWrongInstanceType, scratch,
5443 void LCodeGen::DoCheckValue(LCheckValue* instr) {
5444 Register reg = ToRegister(instr->value());
5445 Handle<HeapObject> object = instr->hydrogen()->object().handle();
5446 AllowDeferredHandleDereference smi_check;
5447 if (isolate()->heap()->InNewSpace(*object)) {
5448 Register reg = ToRegister(instr->value());
5449 Handle<Cell> cell = isolate()->factory()->NewCell(object);
5450 __ li(at, Operand(cell));
5451 __ ld(at, FieldMemOperand(at, Cell::kValueOffset));
5452 DeoptimizeIf(ne, instr, Deoptimizer::kValueMismatch, reg, Operand(at));
5454 DeoptimizeIf(ne, instr, Deoptimizer::kValueMismatch, reg, Operand(object));
5459 void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
5461 PushSafepointRegistersScope scope(this);
5463 __ mov(cp, zero_reg);
5464 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
5465 RecordSafepointWithRegisters(
5466 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
5467 __ StoreToSafepointRegisterSlot(v0, scratch0());
5469 __ SmiTst(scratch0(), at);
5470 DeoptimizeIf(eq, instr, Deoptimizer::kInstanceMigrationFailed, at,
5475 void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
5476 class DeferredCheckMaps final : public LDeferredCode {
5478 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
5479 : LDeferredCode(codegen), instr_(instr), object_(object) {
5480 SetExit(check_maps());
5482 void Generate() override {
5483 codegen()->DoDeferredInstanceMigration(instr_, object_);
5485 Label* check_maps() { return &check_maps_; }
5486 LInstruction* instr() override { return instr_; }
5494 if (instr->hydrogen()->IsStabilityCheck()) {
5495 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5496 for (int i = 0; i < maps->size(); ++i) {
5497 AddStabilityDependency(maps->at(i).handle());
5502 Register map_reg = scratch0();
5503 LOperand* input = instr->value();
5504 DCHECK(input->IsRegister());
5505 Register reg = ToRegister(input);
5506 __ ld(map_reg, FieldMemOperand(reg, HeapObject::kMapOffset));
5508 DeferredCheckMaps* deferred = NULL;
5509 if (instr->hydrogen()->HasMigrationTarget()) {
5510 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5511 __ bind(deferred->check_maps());
5514 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5516 for (int i = 0; i < maps->size() - 1; i++) {
5517 Handle<Map> map = maps->at(i).handle();
5518 __ CompareMapAndBranch(map_reg, map, &success, eq, &success);
5520 Handle<Map> map = maps->at(maps->size() - 1).handle();
5521 // Do the CompareMap() directly within the Branch() and DeoptimizeIf().
5522 if (instr->hydrogen()->HasMigrationTarget()) {
5523 __ Branch(deferred->entry(), ne, map_reg, Operand(map));
5525 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap, map_reg, Operand(map));
5532 void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
5533 DoubleRegister value_reg = ToDoubleRegister(instr->unclamped());
5534 Register result_reg = ToRegister(instr->result());
5535 DoubleRegister temp_reg = ToDoubleRegister(instr->temp());
5536 __ ClampDoubleToUint8(result_reg, value_reg, temp_reg);
5540 void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
5541 Register unclamped_reg = ToRegister(instr->unclamped());
5542 Register result_reg = ToRegister(instr->result());
5543 __ ClampUint8(result_reg, unclamped_reg);
5547 void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
5548 Register scratch = scratch0();
5549 Register input_reg = ToRegister(instr->unclamped());
5550 Register result_reg = ToRegister(instr->result());
5551 DoubleRegister temp_reg = ToDoubleRegister(instr->temp());
5552 Label is_smi, done, heap_number;
5554 // Both smi and heap number cases are handled.
5555 __ UntagAndJumpIfSmi(scratch, input_reg, &is_smi);
5557 // Check for heap number
5558 __ ld(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
5559 __ Branch(&heap_number, eq, scratch, Operand(factory()->heap_number_map()));
5561 // Check for undefined. Undefined is converted to zero for clamping
5563 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefined, input_reg,
5564 Operand(factory()->undefined_value()));
5565 __ mov(result_reg, zero_reg);
5569 __ bind(&heap_number);
5570 __ ldc1(double_scratch0(), FieldMemOperand(input_reg,
5571 HeapNumber::kValueOffset));
5572 __ ClampDoubleToUint8(result_reg, double_scratch0(), temp_reg);
5576 __ ClampUint8(result_reg, scratch);
5582 void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5583 DoubleRegister value_reg = ToDoubleRegister(instr->value());
5584 Register result_reg = ToRegister(instr->result());
5585 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5586 __ FmoveHigh(result_reg, value_reg);
5588 __ FmoveLow(result_reg, value_reg);
5593 void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5594 Register hi_reg = ToRegister(instr->hi());
5595 Register lo_reg = ToRegister(instr->lo());
5596 DoubleRegister result_reg = ToDoubleRegister(instr->result());
5597 __ Move(result_reg, lo_reg, hi_reg);
5601 void LCodeGen::DoAllocate(LAllocate* instr) {
5602 class DeferredAllocate final : public LDeferredCode {
5604 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
5605 : LDeferredCode(codegen), instr_(instr) { }
5606 void Generate() override { codegen()->DoDeferredAllocate(instr_); }
5607 LInstruction* instr() override { return instr_; }
5613 DeferredAllocate* deferred =
5614 new(zone()) DeferredAllocate(this, instr);
5616 Register result = ToRegister(instr->result());
5617 Register scratch = ToRegister(instr->temp1());
5618 Register scratch2 = ToRegister(instr->temp2());
5620 // Allocate memory for the object.
5621 AllocationFlags flags = TAG_OBJECT;
5622 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5623 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5625 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5626 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5627 flags = static_cast<AllocationFlags>(flags | PRETENURE);
5629 if (instr->size()->IsConstantOperand()) {
5630 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5631 if (size <= Page::kMaxRegularHeapObjectSize) {
5632 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5634 __ jmp(deferred->entry());
5637 Register size = ToRegister(instr->size());
5638 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5641 __ bind(deferred->exit());
5643 if (instr->hydrogen()->MustPrefillWithFiller()) {
5644 STATIC_ASSERT(kHeapObjectTag == 1);
5645 if (instr->size()->IsConstantOperand()) {
5646 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5647 __ li(scratch, Operand(size - kHeapObjectTag));
5649 __ Dsubu(scratch, ToRegister(instr->size()), Operand(kHeapObjectTag));
5651 __ li(scratch2, Operand(isolate()->factory()->one_pointer_filler_map()));
5654 __ Dsubu(scratch, scratch, Operand(kPointerSize));
5655 __ Daddu(at, result, Operand(scratch));
5656 __ sd(scratch2, MemOperand(at));
5657 __ Branch(&loop, ge, scratch, Operand(zero_reg));
5662 void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
5663 Register result = ToRegister(instr->result());
5665 // TODO(3095996): Get rid of this. For now, we need to make the
5666 // result register contain a valid pointer because it is already
5667 // contained in the register pointer map.
5668 __ mov(result, zero_reg);
5670 PushSafepointRegistersScope scope(this);
5671 if (instr->size()->IsRegister()) {
5672 Register size = ToRegister(instr->size());
5673 DCHECK(!size.is(result));
5677 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5678 if (size >= 0 && size <= Smi::kMaxValue) {
5679 __ li(v0, Operand(Smi::FromInt(size)));
5682 // We should never get here at runtime => abort
5683 __ stop("invalid allocation size");
5688 int flags = AllocateDoubleAlignFlag::encode(
5689 instr->hydrogen()->MustAllocateDoubleAligned());
5690 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5691 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5692 flags = AllocateTargetSpace::update(flags, OLD_SPACE);
5694 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5696 __ li(v0, Operand(Smi::FromInt(flags)));
5699 CallRuntimeFromDeferred(
5700 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
5701 __ StoreToSafepointRegisterSlot(v0, result);
5705 void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
5706 DCHECK(ToRegister(instr->value()).is(a0));
5707 DCHECK(ToRegister(instr->result()).is(v0));
5709 CallRuntime(Runtime::kToFastProperties, 1, instr);
5713 void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
5714 DCHECK(ToRegister(instr->context()).is(cp));
5716 // Registers will be used as follows:
5717 // a7 = literals array.
5718 // a1 = regexp literal.
5719 // a0 = regexp literal clone.
5720 // a2 and a4-a6 are used as temporaries.
5721 int literal_offset =
5722 FixedArray::OffsetOfElementAt(instr->hydrogen()->literal_index());
5723 __ li(a7, instr->hydrogen()->literals());
5724 __ ld(a1, FieldMemOperand(a7, literal_offset));
5725 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5726 __ Branch(&materialized, ne, a1, Operand(at));
5728 // Create regexp literal using runtime function
5729 // Result will be in v0.
5730 __ li(a6, Operand(Smi::FromInt(instr->hydrogen()->literal_index())));
5731 __ li(a5, Operand(instr->hydrogen()->pattern()));
5732 __ li(a4, Operand(instr->hydrogen()->flags()));
5733 __ Push(a7, a6, a5, a4);
5734 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
5737 __ bind(&materialized);
5738 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
5739 Label allocated, runtime_allocate;
5741 __ Allocate(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
5744 __ bind(&runtime_allocate);
5745 __ li(a0, Operand(Smi::FromInt(size)));
5747 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
5750 __ bind(&allocated);
5751 // Copy the content into the newly allocated memory.
5752 // (Unroll copy loop once for better throughput).
5753 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
5754 __ ld(a3, FieldMemOperand(a1, i));
5755 __ ld(a2, FieldMemOperand(a1, i + kPointerSize));
5756 __ sd(a3, FieldMemOperand(v0, i));
5757 __ sd(a2, FieldMemOperand(v0, i + kPointerSize));
5759 if ((size % (2 * kPointerSize)) != 0) {
5760 __ ld(a3, FieldMemOperand(a1, size - kPointerSize));
5761 __ sd(a3, FieldMemOperand(v0, size - kPointerSize));
5766 void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
5767 DCHECK(ToRegister(instr->context()).is(cp));
5768 // Use the fast case closure allocation code that allocates in new
5769 // space for nested functions that don't need literals cloning.
5770 bool pretenure = instr->hydrogen()->pretenure();
5771 if (!pretenure && instr->hydrogen()->has_no_literals()) {
5772 FastNewClosureStub stub(isolate(), instr->hydrogen()->language_mode(),
5773 instr->hydrogen()->kind());
5774 __ li(a2, Operand(instr->hydrogen()->shared_info()));
5775 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5777 __ li(a2, Operand(instr->hydrogen()->shared_info()));
5778 __ li(a1, Operand(pretenure ? factory()->true_value()
5779 : factory()->false_value()));
5780 __ Push(cp, a2, a1);
5781 CallRuntime(Runtime::kNewClosure, 3, instr);
5786 void LCodeGen::DoTypeof(LTypeof* instr) {
5787 DCHECK(ToRegister(instr->value()).is(a3));
5788 DCHECK(ToRegister(instr->result()).is(v0));
5790 Register value_register = ToRegister(instr->value());
5791 __ JumpIfNotSmi(value_register, &do_call);
5792 __ li(v0, Operand(isolate()->factory()->number_string()));
5795 TypeofStub stub(isolate());
5796 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5801 void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5802 Register input = ToRegister(instr->value());
5804 Register cmp1 = no_reg;
5805 Operand cmp2 = Operand(no_reg);
5807 Condition final_branch_condition = EmitTypeofIs(instr->TrueLabel(chunk_),
5808 instr->FalseLabel(chunk_),
5810 instr->type_literal(),
5814 DCHECK(cmp1.is_valid());
5815 DCHECK(!cmp2.is_reg() || cmp2.rm().is_valid());
5817 if (final_branch_condition != kNoCondition) {
5818 EmitBranch(instr, final_branch_condition, cmp1, cmp2);
5823 Condition LCodeGen::EmitTypeofIs(Label* true_label,
5826 Handle<String> type_name,
5829 // This function utilizes the delay slot heavily. This is used to load
5830 // values that are always usable without depending on the type of the input
5832 Condition final_branch_condition = kNoCondition;
5833 Register scratch = scratch0();
5834 Factory* factory = isolate()->factory();
5835 if (String::Equals(type_name, factory->number_string())) {
5836 __ JumpIfSmi(input, true_label);
5837 __ ld(input, FieldMemOperand(input, HeapObject::kMapOffset));
5838 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5840 *cmp2 = Operand(at);
5841 final_branch_condition = eq;
5843 } else if (String::Equals(type_name, factory->string_string())) {
5844 __ JumpIfSmi(input, false_label);
5845 __ GetObjectType(input, input, scratch);
5846 __ Branch(USE_DELAY_SLOT, false_label,
5847 ge, scratch, Operand(FIRST_NONSTRING_TYPE));
5848 // input is an object so we can load the BitFieldOffset even if we take the
5850 __ lbu(at, FieldMemOperand(input, Map::kBitFieldOffset));
5851 __ And(at, at, 1 << Map::kIsUndetectable);
5853 *cmp2 = Operand(zero_reg);
5854 final_branch_condition = eq;
5856 } else if (String::Equals(type_name, factory->symbol_string())) {
5857 __ JumpIfSmi(input, false_label);
5858 __ GetObjectType(input, input, scratch);
5860 *cmp2 = Operand(SYMBOL_TYPE);
5861 final_branch_condition = eq;
5863 } else if (String::Equals(type_name, factory->boolean_string())) {
5864 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5865 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5866 __ LoadRoot(at, Heap::kFalseValueRootIndex);
5868 *cmp2 = Operand(input);
5869 final_branch_condition = eq;
5871 } else if (String::Equals(type_name, factory->undefined_string())) {
5872 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5873 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5874 // The first instruction of JumpIfSmi is an And - it is safe in the delay
5876 __ JumpIfSmi(input, false_label);
5877 // Check for undetectable objects => true.
5878 __ ld(input, FieldMemOperand(input, HeapObject::kMapOffset));
5879 __ lbu(at, FieldMemOperand(input, Map::kBitFieldOffset));
5880 __ And(at, at, 1 << Map::kIsUndetectable);
5882 *cmp2 = Operand(zero_reg);
5883 final_branch_condition = ne;
5885 } else if (String::Equals(type_name, factory->function_string())) {
5886 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5887 __ JumpIfSmi(input, false_label);
5888 __ GetObjectType(input, scratch, input);
5889 __ Branch(true_label, eq, input, Operand(JS_FUNCTION_TYPE));
5891 *cmp2 = Operand(JS_FUNCTION_PROXY_TYPE);
5892 final_branch_condition = eq;
5894 } else if (String::Equals(type_name, factory->object_string())) {
5895 __ JumpIfSmi(input, false_label);
5896 __ LoadRoot(at, Heap::kNullValueRootIndex);
5897 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5898 Register map = input;
5899 __ GetObjectType(input, map, scratch);
5900 __ Branch(false_label,
5901 lt, scratch, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
5902 __ Branch(USE_DELAY_SLOT, false_label,
5903 gt, scratch, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
5904 // map is still valid, so the BitField can be loaded in delay slot.
5905 // Check for undetectable objects => false.
5906 __ lbu(at, FieldMemOperand(map, Map::kBitFieldOffset));
5907 __ And(at, at, 1 << Map::kIsUndetectable);
5909 *cmp2 = Operand(zero_reg);
5910 final_branch_condition = eq;
5914 *cmp2 = Operand(zero_reg); // Set to valid regs, to avoid caller assertion.
5915 __ Branch(false_label);
5918 return final_branch_condition;
5922 void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
5923 Register temp1 = ToRegister(instr->temp());
5925 EmitIsConstructCall(temp1, scratch0());
5927 EmitBranch(instr, eq, temp1,
5928 Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
5932 void LCodeGen::EmitIsConstructCall(Register temp1, Register temp2) {
5933 DCHECK(!temp1.is(temp2));
5934 // Get the frame pointer for the calling frame.
5935 __ ld(temp1, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5937 // Skip the arguments adaptor frame if it exists.
5938 Label check_frame_marker;
5939 __ ld(temp2, MemOperand(temp1, StandardFrameConstants::kContextOffset));
5940 __ Branch(&check_frame_marker, ne, temp2,
5941 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5942 __ ld(temp1, MemOperand(temp1, StandardFrameConstants::kCallerFPOffset));
5944 // Check the marker in the calling frame.
5945 __ bind(&check_frame_marker);
5946 __ ld(temp1, MemOperand(temp1, StandardFrameConstants::kMarkerOffset));
5950 void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5951 if (!info()->IsStub()) {
5952 // Ensure that we have enough space after the previous lazy-bailout
5953 // instruction for patching the code here.
5954 int current_pc = masm()->pc_offset();
5955 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5956 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5957 DCHECK_EQ(0, padding_size % Assembler::kInstrSize);
5958 while (padding_size > 0) {
5960 padding_size -= Assembler::kInstrSize;
5964 last_lazy_deopt_pc_ = masm()->pc_offset();
5968 void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
5969 last_lazy_deopt_pc_ = masm()->pc_offset();
5970 DCHECK(instr->HasEnvironment());
5971 LEnvironment* env = instr->environment();
5972 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5973 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5977 void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
5978 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5979 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5980 // needed return address), even though the implementation of LAZY and EAGER is
5981 // now identical. When LAZY is eventually completely folded into EAGER, remove
5982 // the special case below.
5983 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5984 type = Deoptimizer::LAZY;
5987 DeoptimizeIf(al, instr, instr->hydrogen()->reason(), type, zero_reg,
5992 void LCodeGen::DoDummy(LDummy* instr) {
5993 // Nothing to see here, move on!
5997 void LCodeGen::DoDummyUse(LDummyUse* instr) {
5998 // Nothing to see here, move on!
6002 void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
6003 PushSafepointRegistersScope scope(this);
6004 LoadContextFromDeferred(instr->context());
6005 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
6006 RecordSafepointWithLazyDeopt(
6007 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
6008 DCHECK(instr->HasEnvironment());
6009 LEnvironment* env = instr->environment();
6010 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
6014 void LCodeGen::DoStackCheck(LStackCheck* instr) {
6015 class DeferredStackCheck final : public LDeferredCode {
6017 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
6018 : LDeferredCode(codegen), instr_(instr) { }
6019 void Generate() override { codegen()->DoDeferredStackCheck(instr_); }
6020 LInstruction* instr() override { return instr_; }
6023 LStackCheck* instr_;
6026 DCHECK(instr->HasEnvironment());
6027 LEnvironment* env = instr->environment();
6028 // There is no LLazyBailout instruction for stack-checks. We have to
6029 // prepare for lazy deoptimization explicitly here.
6030 if (instr->hydrogen()->is_function_entry()) {
6031 // Perform stack overflow check.
6033 __ LoadRoot(at, Heap::kStackLimitRootIndex);
6034 __ Branch(&done, hs, sp, Operand(at));
6035 DCHECK(instr->context()->IsRegister());
6036 DCHECK(ToRegister(instr->context()).is(cp));
6037 CallCode(isolate()->builtins()->StackCheck(),
6038 RelocInfo::CODE_TARGET,
6042 DCHECK(instr->hydrogen()->is_backwards_branch());
6043 // Perform stack overflow check if this goto needs it before jumping.
6044 DeferredStackCheck* deferred_stack_check =
6045 new(zone()) DeferredStackCheck(this, instr);
6046 __ LoadRoot(at, Heap::kStackLimitRootIndex);
6047 __ Branch(deferred_stack_check->entry(), lo, sp, Operand(at));
6048 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
6049 __ bind(instr->done_label());
6050 deferred_stack_check->SetExit(instr->done_label());
6051 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
6052 // Don't record a deoptimization index for the safepoint here.
6053 // This will be done explicitly when emitting call and the safepoint in
6054 // the deferred code.
6059 void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
6060 // This is a pseudo-instruction that ensures that the environment here is
6061 // properly registered for deoptimization and records the assembler's PC
6063 LEnvironment* environment = instr->environment();
6065 // If the environment were already registered, we would have no way of
6066 // backpatching it with the spill slot operands.
6067 DCHECK(!environment->HasBeenRegistered());
6068 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
6070 GenerateOsrPrologue();
6074 void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
6075 Register result = ToRegister(instr->result());
6076 Register object = ToRegister(instr->object());
6078 __ And(at, object, kSmiTagMask);
6079 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
6081 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
6082 __ GetObjectType(object, a1, a1);
6083 DeoptimizeIf(le, instr, Deoptimizer::kNotAJavaScriptObject, a1,
6084 Operand(LAST_JS_PROXY_TYPE));
6086 Label use_cache, call_runtime;
6087 DCHECK(object.is(a0));
6088 Register null_value = a5;
6089 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
6090 __ CheckEnumCache(null_value, &call_runtime);
6092 __ ld(result, FieldMemOperand(object, HeapObject::kMapOffset));
6093 __ Branch(&use_cache);
6095 // Get the set of properties to enumerate.
6096 __ bind(&call_runtime);
6098 CallRuntime(Runtime::kGetPropertyNamesFast, 1, instr);
6100 __ ld(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
6101 DCHECK(result.is(v0));
6102 __ LoadRoot(at, Heap::kMetaMapRootIndex);
6103 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap, a1, Operand(at));
6104 __ bind(&use_cache);
6108 void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
6109 Register map = ToRegister(instr->map());
6110 Register result = ToRegister(instr->result());
6111 Label load_cache, done;
6112 __ EnumLength(result, map);
6113 __ Branch(&load_cache, ne, result, Operand(Smi::FromInt(0)));
6114 __ li(result, Operand(isolate()->factory()->empty_fixed_array()));
6117 __ bind(&load_cache);
6118 __ LoadInstanceDescriptors(map, result);
6120 FieldMemOperand(result, DescriptorArray::kEnumCacheOffset));
6122 FieldMemOperand(result, FixedArray::SizeFor(instr->idx())));
6123 DeoptimizeIf(eq, instr, Deoptimizer::kNoCache, result, Operand(zero_reg));
6129 void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
6130 Register object = ToRegister(instr->value());
6131 Register map = ToRegister(instr->map());
6132 __ ld(scratch0(), FieldMemOperand(object, HeapObject::kMapOffset));
6133 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap, map, Operand(scratch0()));
6137 void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
6141 PushSafepointRegistersScope scope(this);
6142 __ Push(object, index);
6143 __ mov(cp, zero_reg);
6144 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
6145 RecordSafepointWithRegisters(
6146 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
6147 __ StoreToSafepointRegisterSlot(v0, result);
6151 void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
6152 class DeferredLoadMutableDouble final : public LDeferredCode {
6154 DeferredLoadMutableDouble(LCodeGen* codegen,
6155 LLoadFieldByIndex* instr,
6159 : LDeferredCode(codegen),
6165 void Generate() override {
6166 codegen()->DoDeferredLoadMutableDouble(instr_, result_, object_, index_);
6168 LInstruction* instr() override { return instr_; }
6171 LLoadFieldByIndex* instr_;
6177 Register object = ToRegister(instr->object());
6178 Register index = ToRegister(instr->index());
6179 Register result = ToRegister(instr->result());
6180 Register scratch = scratch0();
6182 DeferredLoadMutableDouble* deferred;
6183 deferred = new(zone()) DeferredLoadMutableDouble(
6184 this, instr, result, object, index);
6186 Label out_of_object, done;
6188 __ And(scratch, index, Operand(Smi::FromInt(1)));
6189 __ Branch(deferred->entry(), ne, scratch, Operand(zero_reg));
6190 __ dsra(index, index, 1);
6192 __ Branch(USE_DELAY_SLOT, &out_of_object, lt, index, Operand(zero_reg));
6193 __ SmiScale(scratch, index, kPointerSizeLog2); // In delay slot.
6194 __ Daddu(scratch, object, scratch);
6195 __ ld(result, FieldMemOperand(scratch, JSObject::kHeaderSize));
6199 __ bind(&out_of_object);
6200 __ ld(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
6201 // Index is equal to negated out of object property index plus 1.
6202 __ Dsubu(scratch, result, scratch);
6203 __ ld(result, FieldMemOperand(scratch,
6204 FixedArray::kHeaderSize - kPointerSize));
6205 __ bind(deferred->exit());
6210 void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
6211 Register context = ToRegister(instr->context());
6212 __ sd(context, MemOperand(fp, StandardFrameConstants::kContextOffset));
6216 void LCodeGen::DoAllocateBlockContext(LAllocateBlockContext* instr) {
6217 Handle<ScopeInfo> scope_info = instr->scope_info();
6218 __ li(at, scope_info);
6219 __ Push(at, ToRegister(instr->function()));
6220 CallRuntime(Runtime::kPushBlockContext, 2, instr);
6221 RecordSafepoint(Safepoint::kNoLazyDeopt);
6227 } // namespace internal