1 // Copyright 2012 the V8 project authors. All rights reserved.7
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "src/base/bits.h"
31 #include "src/code-factory.h"
32 #include "src/code-stubs.h"
33 #include "src/cpu-profiler.h"
34 #include "src/hydrogen-osr.h"
35 #include "src/ic/ic.h"
36 #include "src/ic/stub-cache.h"
37 #include "src/mips/lithium-codegen-mips.h"
38 #include "src/mips/lithium-gap-resolver-mips.h"
45 class SafepointGenerator final : public CallWrapper {
47 SafepointGenerator(LCodeGen* codegen,
48 LPointerMap* pointers,
49 Safepoint::DeoptMode mode)
53 virtual ~SafepointGenerator() {}
55 void BeforeCall(int call_size) const override {}
57 void AfterCall() const override {
58 codegen_->RecordSafepoint(pointers_, deopt_mode_);
63 LPointerMap* pointers_;
64 Safepoint::DeoptMode deopt_mode_;
70 bool LCodeGen::GenerateCode() {
71 LPhase phase("Z_Code generation", chunk());
75 // Open a frame scope to indicate that there is a frame on the stack. The
76 // NONE indicates that the scope shouldn't actually generate code to set up
77 // the frame (that is done in GeneratePrologue).
78 FrameScope frame_scope(masm_, StackFrame::NONE);
80 return GeneratePrologue() && GenerateBody() && GenerateDeferredCode() &&
81 GenerateJumpTable() && GenerateSafepointTable();
85 void LCodeGen::FinishCode(Handle<Code> code) {
87 code->set_stack_slots(GetStackSlotCount());
88 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
89 PopulateDeoptimizationData(code);
93 void LCodeGen::SaveCallerDoubles() {
94 DCHECK(info()->saves_caller_doubles());
95 DCHECK(NeedsEagerFrame());
96 Comment(";;; Save clobbered callee double registers");
98 BitVector* doubles = chunk()->allocated_double_registers();
99 BitVector::Iterator save_iterator(doubles);
100 while (!save_iterator.Done()) {
101 __ sdc1(DoubleRegister::FromAllocationIndex(save_iterator.Current()),
102 MemOperand(sp, count * kDoubleSize));
103 save_iterator.Advance();
109 void LCodeGen::RestoreCallerDoubles() {
110 DCHECK(info()->saves_caller_doubles());
111 DCHECK(NeedsEagerFrame());
112 Comment(";;; Restore clobbered callee double registers");
113 BitVector* doubles = chunk()->allocated_double_registers();
114 BitVector::Iterator save_iterator(doubles);
116 while (!save_iterator.Done()) {
117 __ ldc1(DoubleRegister::FromAllocationIndex(save_iterator.Current()),
118 MemOperand(sp, count * kDoubleSize));
119 save_iterator.Advance();
125 bool LCodeGen::GeneratePrologue() {
126 DCHECK(is_generating());
128 if (info()->IsOptimizing()) {
129 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
132 if (strlen(FLAG_stop_at) > 0 &&
133 info_->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
138 // a1: Callee's JS function.
139 // cp: Callee's context.
140 // fp: Caller's frame pointer.
143 // Sloppy mode functions and builtins need to replace the receiver with the
144 // global proxy when called as functions (without an explicit receiver
146 if (is_sloppy(info()->language_mode()) && info()->MayUseThis() &&
147 !info()->is_native() && info()->scope()->has_this_declaration()) {
149 int receiver_offset = info_->scope()->num_parameters() * kPointerSize;
150 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
151 __ lw(a2, MemOperand(sp, receiver_offset));
152 __ Branch(&ok, ne, a2, Operand(at));
154 __ lw(a2, GlobalObjectOperand());
155 __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalProxyOffset));
157 __ sw(a2, MemOperand(sp, receiver_offset));
163 info()->set_prologue_offset(masm_->pc_offset());
164 if (NeedsEagerFrame()) {
165 if (info()->IsStub()) {
168 __ Prologue(info()->IsCodePreAgingActive());
170 frame_is_built_ = true;
171 info_->AddNoFrameRange(0, masm_->pc_offset());
174 // Reserve space for the stack slots needed by the code.
175 int slots = GetStackSlotCount();
177 if (FLAG_debug_code) {
178 __ Subu(sp, sp, Operand(slots * kPointerSize));
180 __ Addu(a0, sp, Operand(slots * kPointerSize));
181 __ li(a1, Operand(kSlotsZapValue));
184 __ Subu(a0, a0, Operand(kPointerSize));
185 __ sw(a1, MemOperand(a0, 2 * kPointerSize));
186 __ Branch(&loop, ne, a0, Operand(sp));
189 __ Subu(sp, sp, Operand(slots * kPointerSize));
193 if (info()->saves_caller_doubles()) {
197 // Possibly allocate a local context.
198 int heap_slots = info()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
199 if (heap_slots > 0) {
200 Comment(";;; Allocate local context");
201 bool need_write_barrier = true;
202 // Argument to NewContext is the function, which is in a1.
203 DCHECK(!info()->scope()->is_script_scope());
204 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
205 FastNewContextStub stub(isolate(), heap_slots);
207 // Result of FastNewContextStub is always in new space.
208 need_write_barrier = false;
211 __ CallRuntime(Runtime::kNewFunctionContext, 1);
213 RecordSafepoint(Safepoint::kNoLazyDeopt);
214 // Context is returned in both v0. It replaces the context passed to us.
215 // It's saved in the stack and kept live in cp.
217 __ sw(v0, MemOperand(fp, StandardFrameConstants::kContextOffset));
218 // Copy any necessary parameters into the context.
219 int num_parameters = scope()->num_parameters();
220 int first_parameter = scope()->has_this_declaration() ? -1 : 0;
221 for (int i = first_parameter; i < num_parameters; i++) {
222 Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
223 if (var->IsContextSlot()) {
224 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
225 (num_parameters - 1 - i) * kPointerSize;
226 // Load parameter from stack.
227 __ lw(a0, MemOperand(fp, parameter_offset));
228 // Store it in the context.
229 MemOperand target = ContextOperand(cp, var->index());
231 // Update the write barrier. This clobbers a3 and a0.
232 if (need_write_barrier) {
233 __ RecordWriteContextSlot(
234 cp, target.offset(), a0, a3, GetRAState(), kSaveFPRegs);
235 } else if (FLAG_debug_code) {
237 __ JumpIfInNewSpace(cp, a0, &done);
238 __ Abort(kExpectedNewSpaceObject);
243 Comment(";;; End allocate local context");
247 if (FLAG_trace && info()->IsOptimizing()) {
248 // We have not executed any compiled code yet, so cp still holds the
250 __ CallRuntime(Runtime::kTraceEnter, 0);
252 return !is_aborted();
256 void LCodeGen::GenerateOsrPrologue() {
257 // Generate the OSR entry prologue at the first unknown OSR value, or if there
258 // are none, at the OSR entrypoint instruction.
259 if (osr_pc_offset_ >= 0) return;
261 osr_pc_offset_ = masm()->pc_offset();
263 // Adjust the frame size, subsuming the unoptimized frame into the
265 int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
267 __ Subu(sp, sp, Operand(slots * kPointerSize));
271 void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
272 if (instr->IsCall()) {
273 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
275 if (!instr->IsLazyBailout() && !instr->IsGap()) {
276 safepoints_.BumpLastLazySafepointIndex();
281 bool LCodeGen::GenerateDeferredCode() {
282 DCHECK(is_generating());
283 if (deferred_.length() > 0) {
284 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
285 LDeferredCode* code = deferred_[i];
288 instructions_->at(code->instruction_index())->hydrogen_value();
289 RecordAndWritePosition(
290 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
292 Comment(";;; <@%d,#%d> "
293 "-------------------- Deferred %s --------------------",
294 code->instruction_index(),
295 code->instr()->hydrogen_value()->id(),
296 code->instr()->Mnemonic());
297 __ bind(code->entry());
298 if (NeedsDeferredFrame()) {
299 Comment(";;; Build frame");
300 DCHECK(!frame_is_built_);
301 DCHECK(info()->IsStub());
302 frame_is_built_ = true;
303 __ MultiPush(cp.bit() | fp.bit() | ra.bit());
304 __ li(scratch0(), Operand(Smi::FromInt(StackFrame::STUB)));
306 __ Addu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
307 Comment(";;; Deferred code");
310 if (NeedsDeferredFrame()) {
311 Comment(";;; Destroy frame");
312 DCHECK(frame_is_built_);
314 __ MultiPop(cp.bit() | fp.bit() | ra.bit());
315 frame_is_built_ = false;
317 __ jmp(code->exit());
320 // Deferred code is the last part of the instruction sequence. Mark
321 // the generated code as done unless we bailed out.
322 if (!is_aborted()) status_ = DONE;
323 return !is_aborted();
327 bool LCodeGen::GenerateJumpTable() {
328 if (jump_table_.length() > 0) {
329 Label needs_frame, call_deopt_entry;
331 Comment(";;; -------------------- Jump table --------------------");
332 Address base = jump_table_[0].address;
334 Register entry_offset = t9;
336 int length = jump_table_.length();
337 for (int i = 0; i < length; i++) {
338 Deoptimizer::JumpTableEntry* table_entry = &jump_table_[i];
339 __ bind(&table_entry->label);
341 DCHECK(table_entry->bailout_type == jump_table_[0].bailout_type);
342 Address entry = table_entry->address;
343 DeoptComment(table_entry->deopt_info);
345 // Second-level deopt table entries are contiguous and small, so instead
346 // of loading the full, absolute address of each one, load an immediate
347 // offset which will be added to the base address later.
348 __ li(entry_offset, Operand(entry - base));
350 if (table_entry->needs_frame) {
351 DCHECK(!info()->saves_caller_doubles());
352 Comment(";;; call deopt with frame");
353 __ MultiPush(cp.bit() | fp.bit() | ra.bit());
354 __ Call(&needs_frame);
356 __ Call(&call_deopt_entry);
358 info()->LogDeoptCallPosition(masm()->pc_offset(),
359 table_entry->deopt_info.inlining_id);
362 if (needs_frame.is_linked()) {
363 __ bind(&needs_frame);
364 // This variant of deopt can only be used with stubs. Since we don't
365 // have a function pointer to install in the stack frame that we're
366 // building, install a special marker there instead.
367 DCHECK(info()->IsStub());
368 __ li(at, Operand(Smi::FromInt(StackFrame::STUB)));
370 __ Addu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
373 Comment(";;; call deopt");
374 __ bind(&call_deopt_entry);
376 if (info()->saves_caller_doubles()) {
377 DCHECK(info()->IsStub());
378 RestoreCallerDoubles();
381 // Add the base address to the offset previously loaded in entry_offset.
382 __ Addu(entry_offset, entry_offset,
383 Operand(ExternalReference::ForDeoptEntry(base)));
384 __ Jump(entry_offset);
386 __ RecordComment("]");
388 // The deoptimization jump table is the last part of the instruction
389 // sequence. Mark the generated code as done unless we bailed out.
390 if (!is_aborted()) status_ = DONE;
391 return !is_aborted();
395 bool LCodeGen::GenerateSafepointTable() {
397 safepoints_.Emit(masm(), GetStackSlotCount());
398 return !is_aborted();
402 Register LCodeGen::ToRegister(int index) const {
403 return Register::FromAllocationIndex(index);
407 DoubleRegister LCodeGen::ToDoubleRegister(int index) const {
408 return DoubleRegister::FromAllocationIndex(index);
412 Register LCodeGen::ToRegister(LOperand* op) const {
413 DCHECK(op->IsRegister());
414 return ToRegister(op->index());
418 Register LCodeGen::EmitLoadRegister(LOperand* op, Register scratch) {
419 if (op->IsRegister()) {
420 return ToRegister(op->index());
421 } else if (op->IsConstantOperand()) {
422 LConstantOperand* const_op = LConstantOperand::cast(op);
423 HConstant* constant = chunk_->LookupConstant(const_op);
424 Handle<Object> literal = constant->handle(isolate());
425 Representation r = chunk_->LookupLiteralRepresentation(const_op);
426 if (r.IsInteger32()) {
427 AllowDeferredHandleDereference get_number;
428 DCHECK(literal->IsNumber());
429 __ li(scratch, Operand(static_cast<int32_t>(literal->Number())));
430 } else if (r.IsSmi()) {
431 DCHECK(constant->HasSmiValue());
432 __ li(scratch, Operand(Smi::FromInt(constant->Integer32Value())));
433 } else if (r.IsDouble()) {
434 Abort(kEmitLoadRegisterUnsupportedDoubleImmediate);
436 DCHECK(r.IsSmiOrTagged());
437 __ li(scratch, literal);
440 } else if (op->IsStackSlot()) {
441 __ lw(scratch, ToMemOperand(op));
449 DoubleRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
450 DCHECK(op->IsDoubleRegister());
451 return ToDoubleRegister(op->index());
455 DoubleRegister LCodeGen::EmitLoadDoubleRegister(LOperand* op,
456 FloatRegister flt_scratch,
457 DoubleRegister dbl_scratch) {
458 if (op->IsDoubleRegister()) {
459 return ToDoubleRegister(op->index());
460 } else if (op->IsConstantOperand()) {
461 LConstantOperand* const_op = LConstantOperand::cast(op);
462 HConstant* constant = chunk_->LookupConstant(const_op);
463 Handle<Object> literal = constant->handle(isolate());
464 Representation r = chunk_->LookupLiteralRepresentation(const_op);
465 if (r.IsInteger32()) {
466 DCHECK(literal->IsNumber());
467 __ li(at, Operand(static_cast<int32_t>(literal->Number())));
468 __ mtc1(at, flt_scratch);
469 __ cvt_d_w(dbl_scratch, flt_scratch);
471 } else if (r.IsDouble()) {
472 Abort(kUnsupportedDoubleImmediate);
473 } else if (r.IsTagged()) {
474 Abort(kUnsupportedTaggedImmediate);
476 } else if (op->IsStackSlot()) {
477 MemOperand mem_op = ToMemOperand(op);
478 __ ldc1(dbl_scratch, mem_op);
486 Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
487 HConstant* constant = chunk_->LookupConstant(op);
488 DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
489 return constant->handle(isolate());
493 bool LCodeGen::IsInteger32(LConstantOperand* op) const {
494 return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
498 bool LCodeGen::IsSmi(LConstantOperand* op) const {
499 return chunk_->LookupLiteralRepresentation(op).IsSmi();
503 int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
504 return ToRepresentation(op, Representation::Integer32());
508 int32_t LCodeGen::ToRepresentation(LConstantOperand* op,
509 const Representation& r) const {
510 HConstant* constant = chunk_->LookupConstant(op);
511 int32_t value = constant->Integer32Value();
512 if (r.IsInteger32()) return value;
513 DCHECK(r.IsSmiOrTagged());
514 return reinterpret_cast<int32_t>(Smi::FromInt(value));
518 Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
519 HConstant* constant = chunk_->LookupConstant(op);
520 return Smi::FromInt(constant->Integer32Value());
524 double LCodeGen::ToDouble(LConstantOperand* op) const {
525 HConstant* constant = chunk_->LookupConstant(op);
526 DCHECK(constant->HasDoubleValue());
527 return constant->DoubleValue();
531 Operand LCodeGen::ToOperand(LOperand* op) {
532 if (op->IsConstantOperand()) {
533 LConstantOperand* const_op = LConstantOperand::cast(op);
534 HConstant* constant = chunk()->LookupConstant(const_op);
535 Representation r = chunk_->LookupLiteralRepresentation(const_op);
537 DCHECK(constant->HasSmiValue());
538 return Operand(Smi::FromInt(constant->Integer32Value()));
539 } else if (r.IsInteger32()) {
540 DCHECK(constant->HasInteger32Value());
541 return Operand(constant->Integer32Value());
542 } else if (r.IsDouble()) {
543 Abort(kToOperandUnsupportedDoubleImmediate);
545 DCHECK(r.IsTagged());
546 return Operand(constant->handle(isolate()));
547 } else if (op->IsRegister()) {
548 return Operand(ToRegister(op));
549 } else if (op->IsDoubleRegister()) {
550 Abort(kToOperandIsDoubleRegisterUnimplemented);
553 // Stack slots not implemented, use ToMemOperand instead.
559 static int ArgumentsOffsetWithoutFrame(int index) {
561 return -(index + 1) * kPointerSize;
565 MemOperand LCodeGen::ToMemOperand(LOperand* op) const {
566 DCHECK(!op->IsRegister());
567 DCHECK(!op->IsDoubleRegister());
568 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
569 if (NeedsEagerFrame()) {
570 return MemOperand(fp, StackSlotOffset(op->index()));
572 // Retrieve parameter without eager stack-frame relative to the
574 return MemOperand(sp, ArgumentsOffsetWithoutFrame(op->index()));
579 MemOperand LCodeGen::ToHighMemOperand(LOperand* op) const {
580 DCHECK(op->IsDoubleStackSlot());
581 if (NeedsEagerFrame()) {
582 return MemOperand(fp, StackSlotOffset(op->index()) + kPointerSize);
584 // Retrieve parameter without eager stack-frame relative to the
587 sp, ArgumentsOffsetWithoutFrame(op->index()) + kPointerSize);
592 void LCodeGen::WriteTranslation(LEnvironment* environment,
593 Translation* translation) {
594 if (environment == NULL) return;
596 // The translation includes one command per value in the environment.
597 int translation_size = environment->translation_size();
599 WriteTranslation(environment->outer(), translation);
600 WriteTranslationFrame(environment, translation);
602 int object_index = 0;
603 int dematerialized_index = 0;
604 for (int i = 0; i < translation_size; ++i) {
605 LOperand* value = environment->values()->at(i);
607 environment, translation, value, environment->HasTaggedValueAt(i),
608 environment->HasUint32ValueAt(i), &object_index, &dematerialized_index);
613 void LCodeGen::AddToTranslation(LEnvironment* environment,
614 Translation* translation,
618 int* object_index_pointer,
619 int* dematerialized_index_pointer) {
620 if (op == LEnvironment::materialization_marker()) {
621 int object_index = (*object_index_pointer)++;
622 if (environment->ObjectIsDuplicateAt(object_index)) {
623 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
624 translation->DuplicateObject(dupe_of);
627 int object_length = environment->ObjectLengthAt(object_index);
628 if (environment->ObjectIsArgumentsAt(object_index)) {
629 translation->BeginArgumentsObject(object_length);
631 translation->BeginCapturedObject(object_length);
633 int dematerialized_index = *dematerialized_index_pointer;
634 int env_offset = environment->translation_size() + dematerialized_index;
635 *dematerialized_index_pointer += object_length;
636 for (int i = 0; i < object_length; ++i) {
637 LOperand* value = environment->values()->at(env_offset + i);
638 AddToTranslation(environment,
641 environment->HasTaggedValueAt(env_offset + i),
642 environment->HasUint32ValueAt(env_offset + i),
643 object_index_pointer,
644 dematerialized_index_pointer);
649 if (op->IsStackSlot()) {
651 translation->StoreStackSlot(op->index());
652 } else if (is_uint32) {
653 translation->StoreUint32StackSlot(op->index());
655 translation->StoreInt32StackSlot(op->index());
657 } else if (op->IsDoubleStackSlot()) {
658 translation->StoreDoubleStackSlot(op->index());
659 } else if (op->IsRegister()) {
660 Register reg = ToRegister(op);
662 translation->StoreRegister(reg);
663 } else if (is_uint32) {
664 translation->StoreUint32Register(reg);
666 translation->StoreInt32Register(reg);
668 } else if (op->IsDoubleRegister()) {
669 DoubleRegister reg = ToDoubleRegister(op);
670 translation->StoreDoubleRegister(reg);
671 } else if (op->IsConstantOperand()) {
672 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
673 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
674 translation->StoreLiteral(src_index);
681 void LCodeGen::CallCode(Handle<Code> code,
682 RelocInfo::Mode mode,
683 LInstruction* instr) {
684 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT);
688 void LCodeGen::CallCodeGeneric(Handle<Code> code,
689 RelocInfo::Mode mode,
691 SafepointMode safepoint_mode) {
692 DCHECK(instr != NULL);
694 RecordSafepointWithLazyDeopt(instr, safepoint_mode);
698 void LCodeGen::CallRuntime(const Runtime::Function* function,
701 SaveFPRegsMode save_doubles) {
702 DCHECK(instr != NULL);
704 __ CallRuntime(function, num_arguments, save_doubles);
706 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
710 void LCodeGen::LoadContextFromDeferred(LOperand* context) {
711 if (context->IsRegister()) {
712 __ Move(cp, ToRegister(context));
713 } else if (context->IsStackSlot()) {
714 __ lw(cp, ToMemOperand(context));
715 } else if (context->IsConstantOperand()) {
716 HConstant* constant =
717 chunk_->LookupConstant(LConstantOperand::cast(context));
718 __ li(cp, Handle<Object>::cast(constant->handle(isolate())));
725 void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
729 LoadContextFromDeferred(context);
730 __ CallRuntimeSaveDoubles(id);
731 RecordSafepointWithRegisters(
732 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
736 void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
737 Safepoint::DeoptMode mode) {
738 environment->set_has_been_used();
739 if (!environment->HasBeenRegistered()) {
740 // Physical stack frame layout:
741 // -x ............. -4 0 ..................................... y
742 // [incoming arguments] [spill slots] [pushed outgoing arguments]
744 // Layout of the environment:
745 // 0 ..................................................... size-1
746 // [parameters] [locals] [expression stack including arguments]
748 // Layout of the translation:
749 // 0 ........................................................ size - 1 + 4
750 // [expression stack including arguments] [locals] [4 words] [parameters]
751 // |>------------ translation_size ------------<|
754 int jsframe_count = 0;
755 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
757 if (e->frame_type() == JS_FUNCTION) {
761 Translation translation(&translations_, frame_count, jsframe_count, zone());
762 WriteTranslation(environment, &translation);
763 int deoptimization_index = deoptimizations_.length();
764 int pc_offset = masm()->pc_offset();
765 environment->Register(deoptimization_index,
767 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
768 deoptimizations_.Add(environment, zone());
773 void LCodeGen::DeoptimizeIf(Condition condition, LInstruction* instr,
774 Deoptimizer::DeoptReason deopt_reason,
775 Deoptimizer::BailoutType bailout_type,
776 Register src1, const Operand& src2) {
777 LEnvironment* environment = instr->environment();
778 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
779 DCHECK(environment->HasBeenRegistered());
780 int id = environment->deoptimization_index();
781 DCHECK(info()->IsOptimizing() || info()->IsStub());
783 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
785 Abort(kBailoutWasNotPrepared);
789 if (FLAG_deopt_every_n_times != 0 && !info()->IsStub()) {
790 Register scratch = scratch0();
791 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
793 __ Push(a1, scratch);
794 __ li(scratch, Operand(count));
795 __ lw(a1, MemOperand(scratch));
796 __ Subu(a1, a1, Operand(1));
797 __ Branch(&no_deopt, ne, a1, Operand(zero_reg));
798 __ li(a1, Operand(FLAG_deopt_every_n_times));
799 __ sw(a1, MemOperand(scratch));
802 __ Call(entry, RelocInfo::RUNTIME_ENTRY);
804 __ sw(a1, MemOperand(scratch));
808 if (info()->ShouldTrapOnDeopt()) {
810 if (condition != al) {
811 __ Branch(&skip, NegateCondition(condition), src1, src2);
813 __ stop("trap_on_deopt");
817 Deoptimizer::DeoptInfo deopt_info = MakeDeoptInfo(instr, deopt_reason);
819 DCHECK(info()->IsStub() || frame_is_built_);
820 // Go through jump table if we need to handle condition, build frame, or
821 // restore caller doubles.
822 if (condition == al && frame_is_built_ &&
823 !info()->saves_caller_doubles()) {
824 DeoptComment(deopt_info);
825 __ Call(entry, RelocInfo::RUNTIME_ENTRY, condition, src1, src2);
826 info()->LogDeoptCallPosition(masm()->pc_offset(), deopt_info.inlining_id);
828 Deoptimizer::JumpTableEntry table_entry(entry, deopt_info, bailout_type,
830 // We often have several deopts to the same entry, reuse the last
831 // jump entry if this is the case.
832 if (FLAG_trace_deopt || isolate()->cpu_profiler()->is_profiling() ||
833 jump_table_.is_empty() ||
834 !table_entry.IsEquivalentTo(jump_table_.last())) {
835 jump_table_.Add(table_entry, zone());
837 __ Branch(&jump_table_.last().label, condition, src1, src2);
842 void LCodeGen::DeoptimizeIf(Condition condition, LInstruction* instr,
843 Deoptimizer::DeoptReason deopt_reason,
844 Register src1, const Operand& src2) {
845 Deoptimizer::BailoutType bailout_type = info()->IsStub()
847 : Deoptimizer::EAGER;
848 DeoptimizeIf(condition, instr, deopt_reason, bailout_type, src1, src2);
852 void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
853 int length = deoptimizations_.length();
854 if (length == 0) return;
855 Handle<DeoptimizationInputData> data =
856 DeoptimizationInputData::New(isolate(), length, TENURED);
858 Handle<ByteArray> translations =
859 translations_.CreateByteArray(isolate()->factory());
860 data->SetTranslationByteArray(*translations);
861 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
862 data->SetOptimizationId(Smi::FromInt(info_->optimization_id()));
863 if (info_->IsOptimizing()) {
864 // Reference to shared function info does not change between phases.
865 AllowDeferredHandleDereference allow_handle_dereference;
866 data->SetSharedFunctionInfo(*info_->shared_info());
868 data->SetSharedFunctionInfo(Smi::FromInt(0));
870 data->SetWeakCellCache(Smi::FromInt(0));
872 Handle<FixedArray> literals =
873 factory()->NewFixedArray(deoptimization_literals_.length(), TENURED);
874 { AllowDeferredHandleDereference copy_handles;
875 for (int i = 0; i < deoptimization_literals_.length(); i++) {
876 literals->set(i, *deoptimization_literals_[i]);
878 data->SetLiteralArray(*literals);
881 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id().ToInt()));
882 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
884 // Populate the deoptimization entries.
885 for (int i = 0; i < length; i++) {
886 LEnvironment* env = deoptimizations_[i];
887 data->SetAstId(i, env->ast_id());
888 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
889 data->SetArgumentsStackHeight(i,
890 Smi::FromInt(env->arguments_stack_height()));
891 data->SetPc(i, Smi::FromInt(env->pc_offset()));
893 code->set_deoptimization_data(*data);
897 void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
898 DCHECK_EQ(0, deoptimization_literals_.length());
899 for (auto function : chunk()->inlined_functions()) {
900 DefineDeoptimizationLiteral(function);
902 inlined_function_count_ = deoptimization_literals_.length();
906 void LCodeGen::RecordSafepointWithLazyDeopt(
907 LInstruction* instr, SafepointMode safepoint_mode) {
908 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
909 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
911 DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
912 RecordSafepointWithRegisters(
913 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
918 void LCodeGen::RecordSafepoint(
919 LPointerMap* pointers,
920 Safepoint::Kind kind,
922 Safepoint::DeoptMode deopt_mode) {
923 DCHECK(expected_safepoint_kind_ == kind);
925 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
926 Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
927 kind, arguments, deopt_mode);
928 for (int i = 0; i < operands->length(); i++) {
929 LOperand* pointer = operands->at(i);
930 if (pointer->IsStackSlot()) {
931 safepoint.DefinePointerSlot(pointer->index(), zone());
932 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
933 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
939 void LCodeGen::RecordSafepoint(LPointerMap* pointers,
940 Safepoint::DeoptMode deopt_mode) {
941 RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
945 void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
946 LPointerMap empty_pointers(zone());
947 RecordSafepoint(&empty_pointers, deopt_mode);
951 void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
953 Safepoint::DeoptMode deopt_mode) {
955 pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
959 void LCodeGen::RecordAndWritePosition(int position) {
960 if (position == RelocInfo::kNoPosition) return;
961 masm()->positions_recorder()->RecordPosition(position);
962 masm()->positions_recorder()->WriteRecordedPositions();
966 static const char* LabelType(LLabel* label) {
967 if (label->is_loop_header()) return " (loop header)";
968 if (label->is_osr_entry()) return " (OSR entry)";
973 void LCodeGen::DoLabel(LLabel* label) {
974 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
975 current_instruction_,
976 label->hydrogen_value()->id(),
979 __ bind(label->label());
980 current_block_ = label->block_id();
985 void LCodeGen::DoParallelMove(LParallelMove* move) {
986 resolver_.Resolve(move);
990 void LCodeGen::DoGap(LGap* gap) {
991 for (int i = LGap::FIRST_INNER_POSITION;
992 i <= LGap::LAST_INNER_POSITION;
994 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
995 LParallelMove* move = gap->GetParallelMove(inner_pos);
996 if (move != NULL) DoParallelMove(move);
1001 void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
1006 void LCodeGen::DoParameter(LParameter* instr) {
1011 void LCodeGen::DoCallStub(LCallStub* instr) {
1012 DCHECK(ToRegister(instr->context()).is(cp));
1013 DCHECK(ToRegister(instr->result()).is(v0));
1014 switch (instr->hydrogen()->major_key()) {
1015 case CodeStub::RegExpExec: {
1016 RegExpExecStub stub(isolate());
1017 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1020 case CodeStub::SubString: {
1021 SubStringStub stub(isolate());
1022 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1025 case CodeStub::StringCompare: {
1026 StringCompareStub stub(isolate());
1027 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1036 void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
1037 GenerateOsrPrologue();
1041 void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
1042 Register dividend = ToRegister(instr->dividend());
1043 int32_t divisor = instr->divisor();
1044 DCHECK(dividend.is(ToRegister(instr->result())));
1046 // Theoretically, a variation of the branch-free code for integer division by
1047 // a power of 2 (calculating the remainder via an additional multiplication
1048 // (which gets simplified to an 'and') and subtraction) should be faster, and
1049 // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
1050 // indicate that positive dividends are heavily favored, so the branching
1051 // version performs better.
1052 HMod* hmod = instr->hydrogen();
1053 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1054 Label dividend_is_not_negative, done;
1056 if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
1057 __ Branch(÷nd_is_not_negative, ge, dividend, Operand(zero_reg));
1058 // Note: The code below even works when right contains kMinInt.
1059 __ subu(dividend, zero_reg, dividend);
1060 __ And(dividend, dividend, Operand(mask));
1061 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1062 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, dividend,
1065 __ Branch(USE_DELAY_SLOT, &done);
1066 __ subu(dividend, zero_reg, dividend);
1069 __ bind(÷nd_is_not_negative);
1070 __ And(dividend, dividend, Operand(mask));
1075 void LCodeGen::DoModByConstI(LModByConstI* instr) {
1076 Register dividend = ToRegister(instr->dividend());
1077 int32_t divisor = instr->divisor();
1078 Register result = ToRegister(instr->result());
1079 DCHECK(!dividend.is(result));
1082 DeoptimizeIf(al, instr);
1086 __ TruncatingDiv(result, dividend, Abs(divisor));
1087 __ Mul(result, result, Operand(Abs(divisor)));
1088 __ Subu(result, dividend, Operand(result));
1090 // Check for negative zero.
1091 HMod* hmod = instr->hydrogen();
1092 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1093 Label remainder_not_zero;
1094 __ Branch(&remainder_not_zero, ne, result, Operand(zero_reg));
1095 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, dividend,
1097 __ bind(&remainder_not_zero);
1102 void LCodeGen::DoModI(LModI* instr) {
1103 HMod* hmod = instr->hydrogen();
1104 const Register left_reg = ToRegister(instr->left());
1105 const Register right_reg = ToRegister(instr->right());
1106 const Register result_reg = ToRegister(instr->result());
1108 // div runs in the background while we check for special cases.
1109 __ Mod(result_reg, left_reg, right_reg);
1112 // Check for x % 0, we have to deopt in this case because we can't return a
1114 if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
1115 DeoptimizeIf(eq, instr, Deoptimizer::kDivisionByZero, right_reg,
1119 // Check for kMinInt % -1, div will return kMinInt, which is not what we
1120 // want. We have to deopt if we care about -0, because we can't return that.
1121 if (hmod->CheckFlag(HValue::kCanOverflow)) {
1122 Label no_overflow_possible;
1123 __ Branch(&no_overflow_possible, ne, left_reg, Operand(kMinInt));
1124 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1125 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, right_reg, Operand(-1));
1127 __ Branch(&no_overflow_possible, ne, right_reg, Operand(-1));
1128 __ Branch(USE_DELAY_SLOT, &done);
1129 __ mov(result_reg, zero_reg);
1131 __ bind(&no_overflow_possible);
1134 // If we care about -0, test if the dividend is <0 and the result is 0.
1135 __ Branch(&done, ge, left_reg, Operand(zero_reg));
1136 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1137 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, result_reg,
1144 void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
1145 Register dividend = ToRegister(instr->dividend());
1146 int32_t divisor = instr->divisor();
1147 Register result = ToRegister(instr->result());
1148 DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
1149 DCHECK(!result.is(dividend));
1151 // Check for (0 / -x) that will produce negative zero.
1152 HDiv* hdiv = instr->hydrogen();
1153 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1154 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, dividend,
1157 // Check for (kMinInt / -1).
1158 if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
1159 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow, dividend, Operand(kMinInt));
1161 // Deoptimize if remainder will not be 0.
1162 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1163 divisor != 1 && divisor != -1) {
1164 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1165 __ And(at, dividend, Operand(mask));
1166 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision, at, Operand(zero_reg));
1169 if (divisor == -1) { // Nice shortcut, not needed for correctness.
1170 __ Subu(result, zero_reg, dividend);
1173 uint16_t shift = WhichPowerOf2Abs(divisor);
1175 __ Move(result, dividend);
1176 } else if (shift == 1) {
1177 __ srl(result, dividend, 31);
1178 __ Addu(result, dividend, Operand(result));
1180 __ sra(result, dividend, 31);
1181 __ srl(result, result, 32 - shift);
1182 __ Addu(result, dividend, Operand(result));
1184 if (shift > 0) __ sra(result, result, shift);
1185 if (divisor < 0) __ Subu(result, zero_reg, result);
1189 void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
1190 Register dividend = ToRegister(instr->dividend());
1191 int32_t divisor = instr->divisor();
1192 Register result = ToRegister(instr->result());
1193 DCHECK(!dividend.is(result));
1196 DeoptimizeIf(al, instr);
1200 // Check for (0 / -x) that will produce negative zero.
1201 HDiv* hdiv = instr->hydrogen();
1202 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1203 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, dividend,
1207 __ TruncatingDiv(result, dividend, Abs(divisor));
1208 if (divisor < 0) __ Subu(result, zero_reg, result);
1210 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1211 __ Mul(scratch0(), result, Operand(divisor));
1212 __ Subu(scratch0(), scratch0(), dividend);
1213 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision, scratch0(),
1219 // TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
1220 void LCodeGen::DoDivI(LDivI* instr) {
1221 HBinaryOperation* hdiv = instr->hydrogen();
1222 Register dividend = ToRegister(instr->dividend());
1223 Register divisor = ToRegister(instr->divisor());
1224 const Register result = ToRegister(instr->result());
1225 Register remainder = ToRegister(instr->temp());
1227 // On MIPS div is asynchronous - it will run in the background while we
1228 // check for special cases.
1229 __ Div(remainder, result, dividend, divisor);
1232 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1233 DeoptimizeIf(eq, instr, Deoptimizer::kDivisionByZero, divisor,
1237 // Check for (0 / -x) that will produce negative zero.
1238 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1239 Label left_not_zero;
1240 __ Branch(&left_not_zero, ne, dividend, Operand(zero_reg));
1241 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, divisor,
1243 __ bind(&left_not_zero);
1246 // Check for (kMinInt / -1).
1247 if (hdiv->CheckFlag(HValue::kCanOverflow) &&
1248 !hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1249 Label left_not_min_int;
1250 __ Branch(&left_not_min_int, ne, dividend, Operand(kMinInt));
1251 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow, divisor, Operand(-1));
1252 __ bind(&left_not_min_int);
1255 if (!hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1256 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision, remainder,
1262 void LCodeGen::DoMultiplyAddD(LMultiplyAddD* instr) {
1263 DoubleRegister addend = ToDoubleRegister(instr->addend());
1264 DoubleRegister multiplier = ToDoubleRegister(instr->multiplier());
1265 DoubleRegister multiplicand = ToDoubleRegister(instr->multiplicand());
1267 // This is computed in-place.
1268 DCHECK(addend.is(ToDoubleRegister(instr->result())));
1270 __ madd_d(addend, addend, multiplier, multiplicand);
1274 void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
1275 Register dividend = ToRegister(instr->dividend());
1276 Register result = ToRegister(instr->result());
1277 int32_t divisor = instr->divisor();
1278 Register scratch = result.is(dividend) ? scratch0() : dividend;
1279 DCHECK(!result.is(dividend) || !scratch.is(dividend));
1281 // If the divisor is 1, return the dividend.
1283 __ Move(result, dividend);
1287 // If the divisor is positive, things are easy: There can be no deopts and we
1288 // can simply do an arithmetic right shift.
1289 uint16_t shift = WhichPowerOf2Abs(divisor);
1291 __ sra(result, dividend, shift);
1295 // If the divisor is negative, we have to negate and handle edge cases.
1297 // dividend can be the same register as result so save the value of it
1298 // for checking overflow.
1299 __ Move(scratch, dividend);
1301 __ Subu(result, zero_reg, dividend);
1302 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1303 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, result, Operand(zero_reg));
1306 // Dividing by -1 is basically negation, unless we overflow.
1307 __ Xor(scratch, scratch, result);
1308 if (divisor == -1) {
1309 if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1310 DeoptimizeIf(ge, instr, Deoptimizer::kOverflow, scratch,
1316 // If the negation could not overflow, simply shifting is OK.
1317 if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1318 __ sra(result, result, shift);
1322 Label no_overflow, done;
1323 __ Branch(&no_overflow, lt, scratch, Operand(zero_reg));
1324 __ li(result, Operand(kMinInt / divisor));
1326 __ bind(&no_overflow);
1327 __ sra(result, result, shift);
1332 void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
1333 Register dividend = ToRegister(instr->dividend());
1334 int32_t divisor = instr->divisor();
1335 Register result = ToRegister(instr->result());
1336 DCHECK(!dividend.is(result));
1339 DeoptimizeIf(al, instr);
1343 // Check for (0 / -x) that will produce negative zero.
1344 HMathFloorOfDiv* hdiv = instr->hydrogen();
1345 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1346 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, dividend,
1350 // Easy case: We need no dynamic check for the dividend and the flooring
1351 // division is the same as the truncating division.
1352 if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
1353 (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
1354 __ TruncatingDiv(result, dividend, Abs(divisor));
1355 if (divisor < 0) __ Subu(result, zero_reg, result);
1359 // In the general case we may need to adjust before and after the truncating
1360 // division to get a flooring division.
1361 Register temp = ToRegister(instr->temp());
1362 DCHECK(!temp.is(dividend) && !temp.is(result));
1363 Label needs_adjustment, done;
1364 __ Branch(&needs_adjustment, divisor > 0 ? lt : gt,
1365 dividend, Operand(zero_reg));
1366 __ TruncatingDiv(result, dividend, Abs(divisor));
1367 if (divisor < 0) __ Subu(result, zero_reg, result);
1369 __ bind(&needs_adjustment);
1370 __ Addu(temp, dividend, Operand(divisor > 0 ? 1 : -1));
1371 __ TruncatingDiv(result, temp, Abs(divisor));
1372 if (divisor < 0) __ Subu(result, zero_reg, result);
1373 __ Subu(result, result, Operand(1));
1378 // TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
1379 void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
1380 HBinaryOperation* hdiv = instr->hydrogen();
1381 Register dividend = ToRegister(instr->dividend());
1382 Register divisor = ToRegister(instr->divisor());
1383 const Register result = ToRegister(instr->result());
1384 Register remainder = scratch0();
1385 // On MIPS div is asynchronous - it will run in the background while we
1386 // check for special cases.
1387 __ Div(remainder, result, dividend, divisor);
1390 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1391 DeoptimizeIf(eq, instr, Deoptimizer::kDivisionByZero, divisor,
1395 // Check for (0 / -x) that will produce negative zero.
1396 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1397 Label left_not_zero;
1398 __ Branch(&left_not_zero, ne, dividend, Operand(zero_reg));
1399 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, divisor,
1401 __ bind(&left_not_zero);
1404 // Check for (kMinInt / -1).
1405 if (hdiv->CheckFlag(HValue::kCanOverflow) &&
1406 !hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1407 Label left_not_min_int;
1408 __ Branch(&left_not_min_int, ne, dividend, Operand(kMinInt));
1409 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow, divisor, Operand(-1));
1410 __ bind(&left_not_min_int);
1413 // We performed a truncating division. Correct the result if necessary.
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 __ Subu(result, result, Operand(1));
1423 void LCodeGen::DoMulI(LMulI* 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 __ SubuAndCheckForOverflow(result, zero_reg, left, scratch);
1447 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, scratch,
1450 __ Subu(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 __ sll(result, left, shift);
1476 // Correct the sign of the result if the constant is negative.
1477 if (constant < 0) __ Subu(result, zero_reg, result);
1478 } else if (base::bits::IsPowerOfTwo32(constant_abs - 1)) {
1479 int32_t shift = WhichPowerOf2(constant_abs - 1);
1480 __ sll(scratch, left, shift);
1481 __ Addu(result, scratch, left);
1482 // Correct the sign of the result if the constant is negative.
1483 if (constant < 0) __ Subu(result, zero_reg, result);
1484 } else if (base::bits::IsPowerOfTwo32(constant_abs + 1)) {
1485 int32_t shift = WhichPowerOf2(constant_abs + 1);
1486 __ sll(scratch, left, shift);
1487 __ Subu(result, scratch, left);
1488 // Correct the sign of the result if the constant is negative.
1489 if (constant < 0) __ Subu(result, zero_reg, result);
1491 // Generate standard code.
1492 __ li(at, constant);
1493 __ Mul(result, left, at);
1498 DCHECK(right_op->IsRegister());
1499 Register right = ToRegister(right_op);
1502 // hi:lo = left * right.
1503 if (instr->hydrogen()->representation().IsSmi()) {
1504 __ SmiUntag(result, left);
1505 __ Mul(scratch, result, result, right);
1507 __ Mul(scratch, result, left, right);
1509 __ sra(at, result, 31);
1510 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow, scratch, Operand(at));
1512 if (instr->hydrogen()->representation().IsSmi()) {
1513 __ SmiUntag(result, left);
1514 __ Mul(result, result, right);
1516 __ Mul(result, left, right);
1520 if (bailout_on_minus_zero) {
1522 __ Xor(at, left, right);
1523 __ Branch(&done, ge, at, Operand(zero_reg));
1524 // Bail out if the result is minus zero.
1525 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, result,
1533 void LCodeGen::DoBitI(LBitI* instr) {
1534 LOperand* left_op = instr->left();
1535 LOperand* right_op = instr->right();
1536 DCHECK(left_op->IsRegister());
1537 Register left = ToRegister(left_op);
1538 Register result = ToRegister(instr->result());
1539 Operand right(no_reg);
1541 if (right_op->IsStackSlot()) {
1542 right = Operand(EmitLoadRegister(right_op, at));
1544 DCHECK(right_op->IsRegister() || right_op->IsConstantOperand());
1545 right = ToOperand(right_op);
1548 switch (instr->op()) {
1549 case Token::BIT_AND:
1550 __ And(result, left, right);
1553 __ Or(result, left, right);
1555 case Token::BIT_XOR:
1556 if (right_op->IsConstantOperand() && right.immediate() == int32_t(~0)) {
1557 __ Nor(result, zero_reg, left);
1559 __ Xor(result, left, right);
1569 void LCodeGen::DoShiftI(LShiftI* instr) {
1570 // Both 'left' and 'right' are "used at start" (see LCodeGen::DoShift), so
1571 // result may alias either of them.
1572 LOperand* right_op = instr->right();
1573 Register left = ToRegister(instr->left());
1574 Register result = ToRegister(instr->result());
1575 Register scratch = scratch0();
1577 if (right_op->IsRegister()) {
1578 // No need to mask the right operand on MIPS, it is built into the variable
1579 // shift instructions.
1580 switch (instr->op()) {
1582 __ Ror(result, left, Operand(ToRegister(right_op)));
1585 __ srav(result, left, ToRegister(right_op));
1588 __ srlv(result, left, ToRegister(right_op));
1589 if (instr->can_deopt()) {
1590 DeoptimizeIf(lt, instr, Deoptimizer::kNegativeValue, result,
1595 __ sllv(result, left, ToRegister(right_op));
1602 // Mask the right_op operand.
1603 int value = ToInteger32(LConstantOperand::cast(right_op));
1604 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1605 switch (instr->op()) {
1607 if (shift_count != 0) {
1608 __ Ror(result, left, Operand(shift_count));
1610 __ Move(result, left);
1614 if (shift_count != 0) {
1615 __ sra(result, left, shift_count);
1617 __ Move(result, left);
1621 if (shift_count != 0) {
1622 __ srl(result, left, shift_count);
1624 if (instr->can_deopt()) {
1625 __ And(at, left, Operand(0x80000000));
1626 DeoptimizeIf(ne, instr, Deoptimizer::kNegativeValue, at,
1629 __ Move(result, left);
1633 if (shift_count != 0) {
1634 if (instr->hydrogen_value()->representation().IsSmi() &&
1635 instr->can_deopt()) {
1636 if (shift_count != 1) {
1637 __ sll(result, left, shift_count - 1);
1638 __ SmiTagCheckOverflow(result, result, scratch);
1640 __ SmiTagCheckOverflow(result, left, scratch);
1642 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, scratch,
1645 __ sll(result, left, shift_count);
1648 __ Move(result, left);
1659 void LCodeGen::DoSubI(LSubI* instr) {
1660 LOperand* left = instr->left();
1661 LOperand* right = instr->right();
1662 LOperand* result = instr->result();
1663 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1665 if (!can_overflow) {
1666 if (right->IsStackSlot()) {
1667 Register right_reg = EmitLoadRegister(right, at);
1668 __ Subu(ToRegister(result), ToRegister(left), Operand(right_reg));
1670 DCHECK(right->IsRegister() || right->IsConstantOperand());
1671 __ Subu(ToRegister(result), ToRegister(left), ToOperand(right));
1673 } else { // can_overflow.
1674 Register overflow = scratch0();
1675 Register scratch = scratch1();
1676 if (right->IsStackSlot()) {
1677 Register right_reg = EmitLoadRegister(right, scratch);
1678 __ SubuAndCheckForOverflow(ToRegister(result),
1681 overflow); // Reg at also used as scratch.
1683 DCHECK(right->IsRegister() || right->IsConstantOperand());
1684 __ SubuAndCheckForOverflow(ToRegister(result), ToRegister(left),
1685 ToOperand(right), overflow, scratch);
1687 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, overflow,
1693 void LCodeGen::DoConstantI(LConstantI* instr) {
1694 __ li(ToRegister(instr->result()), Operand(instr->value()));
1698 void LCodeGen::DoConstantS(LConstantS* instr) {
1699 __ li(ToRegister(instr->result()), Operand(instr->value()));
1703 void LCodeGen::DoConstantD(LConstantD* instr) {
1704 DCHECK(instr->result()->IsDoubleRegister());
1705 DoubleRegister result = ToDoubleRegister(instr->result());
1706 #if V8_HOST_ARCH_IA32
1707 // Need some crappy work-around for x87 sNaN -> qNaN breakage in simulator
1709 uint64_t bits = instr->bits();
1710 if ((bits & V8_UINT64_C(0x7FF8000000000000)) ==
1711 V8_UINT64_C(0x7FF0000000000000)) {
1712 uint32_t lo = static_cast<uint32_t>(bits);
1713 uint32_t hi = static_cast<uint32_t>(bits >> 32);
1714 __ li(at, Operand(lo));
1715 __ li(scratch0(), Operand(hi));
1716 __ Move(result, at, scratch0());
1720 double v = instr->value();
1725 void LCodeGen::DoConstantE(LConstantE* instr) {
1726 __ li(ToRegister(instr->result()), Operand(instr->value()));
1730 void LCodeGen::DoConstantT(LConstantT* instr) {
1731 Handle<Object> object = instr->value(isolate());
1732 AllowDeferredHandleDereference smi_check;
1733 __ li(ToRegister(instr->result()), object);
1737 void LCodeGen::DoMapEnumLength(LMapEnumLength* instr) {
1738 Register result = ToRegister(instr->result());
1739 Register map = ToRegister(instr->value());
1740 __ EnumLength(result, map);
1744 void LCodeGen::DoDateField(LDateField* instr) {
1745 Register object = ToRegister(instr->date());
1746 Register result = ToRegister(instr->result());
1747 Register scratch = ToRegister(instr->temp());
1748 Smi* index = instr->index();
1749 DCHECK(object.is(a0));
1750 DCHECK(result.is(v0));
1751 DCHECK(!scratch.is(scratch0()));
1752 DCHECK(!scratch.is(object));
1754 if (index->value() == 0) {
1755 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset));
1757 Label runtime, done;
1758 if (index->value() < JSDate::kFirstUncachedField) {
1759 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
1760 __ li(scratch, Operand(stamp));
1761 __ lw(scratch, MemOperand(scratch));
1762 __ lw(scratch0(), FieldMemOperand(object, JSDate::kCacheStampOffset));
1763 __ Branch(&runtime, ne, scratch, Operand(scratch0()));
1764 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset +
1765 kPointerSize * index->value()));
1769 __ PrepareCallCFunction(2, scratch);
1770 __ li(a1, Operand(index));
1771 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
1777 MemOperand LCodeGen::BuildSeqStringOperand(Register string,
1779 String::Encoding encoding) {
1780 if (index->IsConstantOperand()) {
1781 int offset = ToInteger32(LConstantOperand::cast(index));
1782 if (encoding == String::TWO_BYTE_ENCODING) {
1783 offset *= kUC16Size;
1785 STATIC_ASSERT(kCharSize == 1);
1786 return FieldMemOperand(string, SeqString::kHeaderSize + offset);
1788 Register scratch = scratch0();
1789 DCHECK(!scratch.is(string));
1790 DCHECK(!scratch.is(ToRegister(index)));
1791 if (encoding == String::ONE_BYTE_ENCODING) {
1792 __ Addu(scratch, string, ToRegister(index));
1794 STATIC_ASSERT(kUC16Size == 2);
1795 __ sll(scratch, ToRegister(index), 1);
1796 __ Addu(scratch, string, scratch);
1798 return FieldMemOperand(scratch, SeqString::kHeaderSize);
1802 void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
1803 String::Encoding encoding = instr->hydrogen()->encoding();
1804 Register string = ToRegister(instr->string());
1805 Register result = ToRegister(instr->result());
1807 if (FLAG_debug_code) {
1808 Register scratch = scratch0();
1809 __ lw(scratch, FieldMemOperand(string, HeapObject::kMapOffset));
1810 __ lbu(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1812 __ And(scratch, scratch,
1813 Operand(kStringRepresentationMask | kStringEncodingMask));
1814 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1815 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1816 __ Subu(at, scratch, Operand(encoding == String::ONE_BYTE_ENCODING
1817 ? one_byte_seq_type : two_byte_seq_type));
1818 __ Check(eq, kUnexpectedStringType, at, Operand(zero_reg));
1821 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1822 if (encoding == String::ONE_BYTE_ENCODING) {
1823 __ lbu(result, operand);
1825 __ lhu(result, operand);
1830 void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
1831 String::Encoding encoding = instr->hydrogen()->encoding();
1832 Register string = ToRegister(instr->string());
1833 Register value = ToRegister(instr->value());
1835 if (FLAG_debug_code) {
1836 Register scratch = scratch0();
1837 Register index = ToRegister(instr->index());
1838 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1839 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1841 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
1842 ? one_byte_seq_type : two_byte_seq_type;
1843 __ EmitSeqStringSetCharCheck(string, index, value, scratch, encoding_mask);
1846 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1847 if (encoding == String::ONE_BYTE_ENCODING) {
1848 __ sb(value, operand);
1850 __ sh(value, operand);
1855 void LCodeGen::DoAddI(LAddI* instr) {
1856 LOperand* left = instr->left();
1857 LOperand* right = instr->right();
1858 LOperand* result = instr->result();
1859 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1861 if (!can_overflow) {
1862 if (right->IsStackSlot()) {
1863 Register right_reg = EmitLoadRegister(right, at);
1864 __ Addu(ToRegister(result), ToRegister(left), Operand(right_reg));
1866 DCHECK(right->IsRegister() || right->IsConstantOperand());
1867 __ Addu(ToRegister(result), ToRegister(left), ToOperand(right));
1869 } else { // can_overflow.
1870 Register overflow = scratch0();
1871 Register scratch = scratch1();
1872 if (right->IsStackSlot()) {
1873 Register right_reg = EmitLoadRegister(right, scratch);
1874 __ AdduAndCheckForOverflow(ToRegister(result),
1877 overflow); // Reg at also used as scratch.
1879 DCHECK(right->IsRegister() || right->IsConstantOperand());
1880 __ AdduAndCheckForOverflow(ToRegister(result), ToRegister(left),
1881 ToOperand(right), overflow, scratch);
1883 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, overflow,
1889 void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
1890 LOperand* left = instr->left();
1891 LOperand* right = instr->right();
1892 HMathMinMax::Operation operation = instr->hydrogen()->operation();
1893 Condition condition = (operation == HMathMinMax::kMathMin) ? le : ge;
1894 if (instr->hydrogen()->representation().IsSmiOrInteger32()) {
1895 Register left_reg = ToRegister(left);
1896 Register right_reg = EmitLoadRegister(right, scratch0());
1897 Register result_reg = ToRegister(instr->result());
1898 Label return_right, done;
1899 Register scratch = scratch1();
1900 __ Slt(scratch, left_reg, Operand(right_reg));
1901 if (condition == ge) {
1902 __ Movz(result_reg, left_reg, scratch);
1903 __ Movn(result_reg, right_reg, scratch);
1905 DCHECK(condition == le);
1906 __ Movn(result_reg, left_reg, scratch);
1907 __ Movz(result_reg, right_reg, scratch);
1910 DCHECK(instr->hydrogen()->representation().IsDouble());
1911 FPURegister left_reg = ToDoubleRegister(left);
1912 FPURegister right_reg = ToDoubleRegister(right);
1913 FPURegister result_reg = ToDoubleRegister(instr->result());
1914 Label check_nan_left, check_zero, return_left, return_right, done;
1915 __ BranchF(&check_zero, &check_nan_left, eq, left_reg, right_reg);
1916 __ BranchF(&return_left, NULL, condition, left_reg, right_reg);
1917 __ Branch(&return_right);
1919 __ bind(&check_zero);
1920 // left == right != 0.
1921 __ BranchF(&return_left, NULL, ne, left_reg, kDoubleRegZero);
1922 // At this point, both left and right are either 0 or -0.
1923 if (operation == HMathMinMax::kMathMin) {
1924 __ neg_d(left_reg, left_reg);
1925 __ sub_d(result_reg, left_reg, right_reg);
1926 __ neg_d(result_reg, result_reg);
1928 __ add_d(result_reg, left_reg, right_reg);
1932 __ bind(&check_nan_left);
1934 __ BranchF(NULL, &return_left, eq, left_reg, left_reg);
1935 __ bind(&return_right);
1936 if (!right_reg.is(result_reg)) {
1937 __ mov_d(result_reg, right_reg);
1941 __ bind(&return_left);
1942 if (!left_reg.is(result_reg)) {
1943 __ mov_d(result_reg, left_reg);
1950 void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
1951 DoubleRegister left = ToDoubleRegister(instr->left());
1952 DoubleRegister right = ToDoubleRegister(instr->right());
1953 DoubleRegister result = ToDoubleRegister(instr->result());
1954 switch (instr->op()) {
1956 __ add_d(result, left, right);
1959 __ sub_d(result, left, right);
1962 __ mul_d(result, left, right);
1965 __ div_d(result, left, right);
1968 // Save a0-a3 on the stack.
1969 RegList saved_regs = a0.bit() | a1.bit() | a2.bit() | a3.bit();
1970 __ MultiPush(saved_regs);
1972 __ PrepareCallCFunction(0, 2, scratch0());
1973 __ MovToFloatParameters(left, right);
1975 ExternalReference::mod_two_doubles_operation(isolate()),
1977 // Move the result in the double result register.
1978 __ MovFromFloatResult(result);
1980 // Restore saved register.
1981 __ MultiPop(saved_regs);
1991 void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
1992 DCHECK(ToRegister(instr->context()).is(cp));
1993 DCHECK(ToRegister(instr->left()).is(a1));
1994 DCHECK(ToRegister(instr->right()).is(a0));
1995 DCHECK(ToRegister(instr->result()).is(v0));
1998 CodeFactory::BinaryOpIC(isolate(), instr->op(), instr->strength()).code();
1999 CallCode(code, RelocInfo::CODE_TARGET, instr);
2000 // Other arch use a nop here, to signal that there is no inlined
2001 // patchable code. Mips does not need the nop, since our marker
2002 // instruction (andi zero_reg) will never be used in normal code.
2006 template<class InstrType>
2007 void LCodeGen::EmitBranch(InstrType instr,
2008 Condition condition,
2010 const Operand& src2) {
2011 int left_block = instr->TrueDestination(chunk_);
2012 int right_block = instr->FalseDestination(chunk_);
2014 int next_block = GetNextEmittedBlock();
2015 if (right_block == left_block || condition == al) {
2016 EmitGoto(left_block);
2017 } else if (left_block == next_block) {
2018 __ Branch(chunk_->GetAssemblyLabel(right_block),
2019 NegateCondition(condition), src1, src2);
2020 } else if (right_block == next_block) {
2021 __ Branch(chunk_->GetAssemblyLabel(left_block), condition, src1, src2);
2023 __ Branch(chunk_->GetAssemblyLabel(left_block), condition, src1, src2);
2024 __ Branch(chunk_->GetAssemblyLabel(right_block));
2029 template<class InstrType>
2030 void LCodeGen::EmitBranchF(InstrType instr,
2031 Condition condition,
2034 int right_block = instr->FalseDestination(chunk_);
2035 int left_block = instr->TrueDestination(chunk_);
2037 int next_block = GetNextEmittedBlock();
2038 if (right_block == left_block) {
2039 EmitGoto(left_block);
2040 } else if (left_block == next_block) {
2041 __ BranchF(chunk_->GetAssemblyLabel(right_block), NULL,
2042 NegateFpuCondition(condition), src1, src2);
2043 } else if (right_block == next_block) {
2044 __ BranchF(chunk_->GetAssemblyLabel(left_block), NULL,
2045 condition, src1, src2);
2047 __ BranchF(chunk_->GetAssemblyLabel(left_block), NULL,
2048 condition, src1, src2);
2049 __ Branch(chunk_->GetAssemblyLabel(right_block));
2054 template<class InstrType>
2055 void LCodeGen::EmitFalseBranch(InstrType instr,
2056 Condition condition,
2058 const Operand& src2) {
2059 int false_block = instr->FalseDestination(chunk_);
2060 __ Branch(chunk_->GetAssemblyLabel(false_block), condition, src1, src2);
2064 template<class InstrType>
2065 void LCodeGen::EmitFalseBranchF(InstrType instr,
2066 Condition condition,
2069 int false_block = instr->FalseDestination(chunk_);
2070 __ BranchF(chunk_->GetAssemblyLabel(false_block), NULL,
2071 condition, src1, src2);
2075 void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
2076 __ stop("LDebugBreak");
2080 void LCodeGen::DoBranch(LBranch* instr) {
2081 Representation r = instr->hydrogen()->value()->representation();
2082 if (r.IsInteger32() || r.IsSmi()) {
2083 DCHECK(!info()->IsStub());
2084 Register reg = ToRegister(instr->value());
2085 EmitBranch(instr, ne, reg, Operand(zero_reg));
2086 } else if (r.IsDouble()) {
2087 DCHECK(!info()->IsStub());
2088 DoubleRegister reg = ToDoubleRegister(instr->value());
2089 // Test the double value. Zero and NaN are false.
2090 EmitBranchF(instr, ogl, reg, kDoubleRegZero);
2092 DCHECK(r.IsTagged());
2093 Register reg = ToRegister(instr->value());
2094 HType type = instr->hydrogen()->value()->type();
2095 if (type.IsBoolean()) {
2096 DCHECK(!info()->IsStub());
2097 __ LoadRoot(at, Heap::kTrueValueRootIndex);
2098 EmitBranch(instr, eq, reg, Operand(at));
2099 } else if (type.IsSmi()) {
2100 DCHECK(!info()->IsStub());
2101 EmitBranch(instr, ne, reg, Operand(zero_reg));
2102 } else if (type.IsJSArray()) {
2103 DCHECK(!info()->IsStub());
2104 EmitBranch(instr, al, zero_reg, Operand(zero_reg));
2105 } else if (type.IsHeapNumber()) {
2106 DCHECK(!info()->IsStub());
2107 DoubleRegister dbl_scratch = double_scratch0();
2108 __ ldc1(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
2109 // Test the double value. Zero and NaN are false.
2110 EmitBranchF(instr, ogl, dbl_scratch, kDoubleRegZero);
2111 } else if (type.IsString()) {
2112 DCHECK(!info()->IsStub());
2113 __ lw(at, FieldMemOperand(reg, String::kLengthOffset));
2114 EmitBranch(instr, ne, at, Operand(zero_reg));
2116 ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
2117 // Avoid deopts in the case where we've never executed this path before.
2118 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
2120 if (expected.Contains(ToBooleanStub::UNDEFINED)) {
2121 // undefined -> false.
2122 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
2123 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
2125 if (expected.Contains(ToBooleanStub::BOOLEAN)) {
2126 // Boolean -> its value.
2127 __ LoadRoot(at, Heap::kTrueValueRootIndex);
2128 __ Branch(instr->TrueLabel(chunk_), eq, reg, Operand(at));
2129 __ LoadRoot(at, Heap::kFalseValueRootIndex);
2130 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
2132 if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
2134 __ LoadRoot(at, Heap::kNullValueRootIndex);
2135 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
2138 if (expected.Contains(ToBooleanStub::SMI)) {
2139 // Smis: 0 -> false, all other -> true.
2140 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(zero_reg));
2141 __ JumpIfSmi(reg, instr->TrueLabel(chunk_));
2142 } else if (expected.NeedsMap()) {
2143 // If we need a map later and have a Smi -> deopt.
2145 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
2148 const Register map = scratch0();
2149 if (expected.NeedsMap()) {
2150 __ lw(map, FieldMemOperand(reg, HeapObject::kMapOffset));
2151 if (expected.CanBeUndetectable()) {
2152 // Undetectable -> false.
2153 __ lbu(at, FieldMemOperand(map, Map::kBitFieldOffset));
2154 __ And(at, at, Operand(1 << Map::kIsUndetectable));
2155 __ Branch(instr->FalseLabel(chunk_), ne, at, Operand(zero_reg));
2159 if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
2160 // spec object -> true.
2161 __ lbu(at, FieldMemOperand(map, Map::kInstanceTypeOffset));
2162 __ Branch(instr->TrueLabel(chunk_),
2163 ge, at, Operand(FIRST_SPEC_OBJECT_TYPE));
2166 if (expected.Contains(ToBooleanStub::STRING)) {
2167 // String value -> false iff empty.
2169 __ lbu(at, FieldMemOperand(map, Map::kInstanceTypeOffset));
2170 __ Branch(¬_string, ge , at, Operand(FIRST_NONSTRING_TYPE));
2171 __ lw(at, FieldMemOperand(reg, String::kLengthOffset));
2172 __ Branch(instr->TrueLabel(chunk_), ne, at, Operand(zero_reg));
2173 __ Branch(instr->FalseLabel(chunk_));
2174 __ bind(¬_string);
2177 if (expected.Contains(ToBooleanStub::SYMBOL)) {
2178 // Symbol value -> true.
2179 const Register scratch = scratch1();
2180 __ lbu(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
2181 __ Branch(instr->TrueLabel(chunk_), eq, scratch, Operand(SYMBOL_TYPE));
2184 if (expected.Contains(ToBooleanStub::SIMD_VALUE)) {
2185 // SIMD value -> true.
2186 const Register scratch = scratch1();
2187 __ lbu(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
2188 __ Branch(instr->TrueLabel(chunk_), eq, scratch,
2189 Operand(FLOAT32X4_TYPE));
2192 if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
2193 // heap number -> false iff +0, -0, or NaN.
2194 DoubleRegister dbl_scratch = double_scratch0();
2195 Label not_heap_number;
2196 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
2197 __ Branch(¬_heap_number, ne, map, Operand(at));
2198 __ ldc1(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
2199 __ BranchF(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2200 ne, dbl_scratch, kDoubleRegZero);
2201 // Falls through if dbl_scratch == 0.
2202 __ Branch(instr->FalseLabel(chunk_));
2203 __ bind(¬_heap_number);
2206 if (!expected.IsGeneric()) {
2207 // We've seen something for the first time -> deopt.
2208 // This can only happen if we are not generic already.
2209 DeoptimizeIf(al, instr, Deoptimizer::kUnexpectedObject, zero_reg,
2217 void LCodeGen::EmitGoto(int block) {
2218 if (!IsNextEmittedBlock(block)) {
2219 __ jmp(chunk_->GetAssemblyLabel(LookupDestination(block)));
2224 void LCodeGen::DoGoto(LGoto* instr) {
2225 EmitGoto(instr->block_id());
2229 Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
2230 Condition cond = kNoCondition;
2233 case Token::EQ_STRICT:
2237 case Token::NE_STRICT:
2241 cond = is_unsigned ? lo : lt;
2244 cond = is_unsigned ? hi : gt;
2247 cond = is_unsigned ? ls : le;
2250 cond = is_unsigned ? hs : ge;
2253 case Token::INSTANCEOF:
2261 void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2262 LOperand* left = instr->left();
2263 LOperand* right = instr->right();
2265 instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2266 instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2267 Condition cond = TokenToCondition(instr->op(), is_unsigned);
2269 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2270 // We can statically evaluate the comparison.
2271 double left_val = ToDouble(LConstantOperand::cast(left));
2272 double right_val = ToDouble(LConstantOperand::cast(right));
2273 int next_block = EvalComparison(instr->op(), left_val, right_val) ?
2274 instr->TrueDestination(chunk_) : instr->FalseDestination(chunk_);
2275 EmitGoto(next_block);
2277 if (instr->is_double()) {
2278 // Compare left and right as doubles and load the
2279 // resulting flags into the normal status register.
2280 FPURegister left_reg = ToDoubleRegister(left);
2281 FPURegister right_reg = ToDoubleRegister(right);
2283 // If a NaN is involved, i.e. the result is unordered,
2284 // jump to false block label.
2285 __ BranchF(NULL, instr->FalseLabel(chunk_), eq,
2286 left_reg, right_reg);
2288 EmitBranchF(instr, cond, left_reg, right_reg);
2291 Operand cmp_right = Operand(0);
2293 if (right->IsConstantOperand()) {
2294 int32_t value = ToInteger32(LConstantOperand::cast(right));
2295 if (instr->hydrogen_value()->representation().IsSmi()) {
2296 cmp_left = ToRegister(left);
2297 cmp_right = Operand(Smi::FromInt(value));
2299 cmp_left = ToRegister(left);
2300 cmp_right = Operand(value);
2302 } else if (left->IsConstantOperand()) {
2303 int32_t value = ToInteger32(LConstantOperand::cast(left));
2304 if (instr->hydrogen_value()->representation().IsSmi()) {
2305 cmp_left = ToRegister(right);
2306 cmp_right = Operand(Smi::FromInt(value));
2308 cmp_left = ToRegister(right);
2309 cmp_right = Operand(value);
2311 // We commuted the operands, so commute the condition.
2312 cond = CommuteCondition(cond);
2314 cmp_left = ToRegister(left);
2315 cmp_right = Operand(ToRegister(right));
2318 EmitBranch(instr, cond, cmp_left, cmp_right);
2324 void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
2325 Register left = ToRegister(instr->left());
2326 Register right = ToRegister(instr->right());
2328 EmitBranch(instr, eq, left, Operand(right));
2332 void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) {
2333 if (instr->hydrogen()->representation().IsTagged()) {
2334 Register input_reg = ToRegister(instr->object());
2335 __ li(at, Operand(factory()->the_hole_value()));
2336 EmitBranch(instr, eq, input_reg, Operand(at));
2340 DoubleRegister input_reg = ToDoubleRegister(instr->object());
2341 EmitFalseBranchF(instr, eq, input_reg, input_reg);
2343 Register scratch = scratch0();
2344 __ FmoveHigh(scratch, input_reg);
2345 EmitBranch(instr, eq, scratch, Operand(kHoleNanUpper32));
2349 void LCodeGen::DoCompareMinusZeroAndBranch(LCompareMinusZeroAndBranch* instr) {
2350 Representation rep = instr->hydrogen()->value()->representation();
2351 DCHECK(!rep.IsInteger32());
2352 Register scratch = ToRegister(instr->temp());
2354 if (rep.IsDouble()) {
2355 DoubleRegister value = ToDoubleRegister(instr->value());
2356 EmitFalseBranchF(instr, ne, value, kDoubleRegZero);
2357 __ FmoveHigh(scratch, value);
2358 __ li(at, 0x80000000);
2360 Register value = ToRegister(instr->value());
2363 Heap::kHeapNumberMapRootIndex,
2364 instr->FalseLabel(chunk()),
2366 __ lw(scratch, FieldMemOperand(value, HeapNumber::kExponentOffset));
2367 EmitFalseBranch(instr, ne, scratch, Operand(0x80000000));
2368 __ lw(scratch, FieldMemOperand(value, HeapNumber::kMantissaOffset));
2369 __ mov(at, zero_reg);
2371 EmitBranch(instr, eq, scratch, Operand(at));
2375 Condition LCodeGen::EmitIsObject(Register input,
2378 Label* is_not_object,
2380 __ JumpIfSmi(input, is_not_object);
2382 __ LoadRoot(temp2, Heap::kNullValueRootIndex);
2383 __ Branch(is_object, eq, input, Operand(temp2));
2386 __ lw(temp1, FieldMemOperand(input, HeapObject::kMapOffset));
2387 // Undetectable objects behave like undefined.
2388 __ lbu(temp2, FieldMemOperand(temp1, Map::kBitFieldOffset));
2389 __ And(temp2, temp2, Operand(1 << Map::kIsUndetectable));
2390 __ Branch(is_not_object, ne, temp2, Operand(zero_reg));
2392 // Load instance type and check that it is in object type range.
2393 __ lbu(temp2, FieldMemOperand(temp1, Map::kInstanceTypeOffset));
2394 __ Branch(is_not_object,
2395 lt, temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2401 void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) {
2402 Register reg = ToRegister(instr->value());
2403 Register temp1 = ToRegister(instr->temp());
2404 Register temp2 = scratch0();
2406 Condition true_cond =
2407 EmitIsObject(reg, temp1, temp2,
2408 instr->FalseLabel(chunk_), instr->TrueLabel(chunk_));
2410 EmitBranch(instr, true_cond, temp2,
2411 Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
2415 Condition LCodeGen::EmitIsString(Register input,
2417 Label* is_not_string,
2418 SmiCheck check_needed = INLINE_SMI_CHECK) {
2419 if (check_needed == INLINE_SMI_CHECK) {
2420 __ JumpIfSmi(input, is_not_string);
2422 __ GetObjectType(input, temp1, temp1);
2428 void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
2429 Register reg = ToRegister(instr->value());
2430 Register temp1 = ToRegister(instr->temp());
2432 SmiCheck check_needed =
2433 instr->hydrogen()->value()->type().IsHeapObject()
2434 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2435 Condition true_cond =
2436 EmitIsString(reg, temp1, instr->FalseLabel(chunk_), check_needed);
2438 EmitBranch(instr, true_cond, temp1,
2439 Operand(FIRST_NONSTRING_TYPE));
2443 void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
2444 Register input_reg = EmitLoadRegister(instr->value(), at);
2445 __ And(at, input_reg, kSmiTagMask);
2446 EmitBranch(instr, eq, at, Operand(zero_reg));
2450 void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
2451 Register input = ToRegister(instr->value());
2452 Register temp = ToRegister(instr->temp());
2454 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2455 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2457 __ lw(temp, FieldMemOperand(input, HeapObject::kMapOffset));
2458 __ lbu(temp, FieldMemOperand(temp, Map::kBitFieldOffset));
2459 __ And(at, temp, Operand(1 << Map::kIsUndetectable));
2460 EmitBranch(instr, ne, at, Operand(zero_reg));
2464 static Condition ComputeCompareCondition(Token::Value op) {
2466 case Token::EQ_STRICT:
2479 return kNoCondition;
2484 void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
2485 DCHECK(ToRegister(instr->context()).is(cp));
2486 Token::Value op = instr->op();
2489 CodeFactory::CompareIC(isolate(), op, Strength::WEAK).code();
2490 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2492 Condition condition = ComputeCompareCondition(op);
2494 EmitBranch(instr, condition, v0, Operand(zero_reg));
2498 static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
2499 InstanceType from = instr->from();
2500 InstanceType to = instr->to();
2501 if (from == FIRST_TYPE) return to;
2502 DCHECK(from == to || to == LAST_TYPE);
2507 static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
2508 InstanceType from = instr->from();
2509 InstanceType to = instr->to();
2510 if (from == to) return eq;
2511 if (to == LAST_TYPE) return hs;
2512 if (from == FIRST_TYPE) return ls;
2518 void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
2519 Register scratch = scratch0();
2520 Register input = ToRegister(instr->value());
2522 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2523 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2526 __ GetObjectType(input, scratch, scratch);
2528 BranchCondition(instr->hydrogen()),
2530 Operand(TestType(instr->hydrogen())));
2534 void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
2535 Register input = ToRegister(instr->value());
2536 Register result = ToRegister(instr->result());
2538 __ AssertString(input);
2540 __ lw(result, FieldMemOperand(input, String::kHashFieldOffset));
2541 __ IndexFromHash(result, result);
2545 void LCodeGen::DoHasCachedArrayIndexAndBranch(
2546 LHasCachedArrayIndexAndBranch* instr) {
2547 Register input = ToRegister(instr->value());
2548 Register scratch = scratch0();
2551 FieldMemOperand(input, String::kHashFieldOffset));
2552 __ And(at, scratch, Operand(String::kContainsCachedArrayIndexMask));
2553 EmitBranch(instr, eq, at, Operand(zero_reg));
2557 // Branches to a label or falls through with the answer in flags. Trashes
2558 // the temp registers, but not the input.
2559 void LCodeGen::EmitClassOfTest(Label* is_true,
2561 Handle<String>class_name,
2565 DCHECK(!input.is(temp));
2566 DCHECK(!input.is(temp2));
2567 DCHECK(!temp.is(temp2));
2569 __ JumpIfSmi(input, is_false);
2571 if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
2572 // Assuming the following assertions, we can use the same compares to test
2573 // for both being a function type and being in the object type range.
2574 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
2575 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2576 FIRST_SPEC_OBJECT_TYPE + 1);
2577 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2578 LAST_SPEC_OBJECT_TYPE - 1);
2579 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
2581 __ GetObjectType(input, temp, temp2);
2582 __ Branch(is_false, lt, temp2, Operand(FIRST_SPEC_OBJECT_TYPE));
2583 __ Branch(is_true, eq, temp2, Operand(FIRST_SPEC_OBJECT_TYPE));
2584 __ Branch(is_true, eq, temp2, Operand(LAST_SPEC_OBJECT_TYPE));
2586 // Faster code path to avoid two compares: subtract lower bound from the
2587 // actual type and do a signed compare with the width of the type range.
2588 __ GetObjectType(input, temp, temp2);
2589 __ Subu(temp2, temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2590 __ Branch(is_false, gt, temp2, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE -
2591 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2594 // Now we are in the FIRST-LAST_NONCALLABLE_SPEC_OBJECT_TYPE range.
2595 // Check if the constructor in the map is a function.
2596 Register instance_type = scratch1();
2597 DCHECK(!instance_type.is(temp));
2598 __ GetMapConstructor(temp, temp, temp2, instance_type);
2600 // Objects with a non-function constructor have class 'Object'.
2601 if (String::Equals(class_name, isolate()->factory()->Object_string())) {
2602 __ Branch(is_true, ne, instance_type, Operand(JS_FUNCTION_TYPE));
2604 __ Branch(is_false, ne, instance_type, Operand(JS_FUNCTION_TYPE));
2607 // temp now contains the constructor function. Grab the
2608 // instance class name from there.
2609 __ lw(temp, FieldMemOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2610 __ lw(temp, FieldMemOperand(temp,
2611 SharedFunctionInfo::kInstanceClassNameOffset));
2612 // The class name we are testing against is internalized since it's a literal.
2613 // The name in the constructor is internalized because of the way the context
2614 // is booted. This routine isn't expected to work for random API-created
2615 // classes and it doesn't have to because you can't access it with natives
2616 // syntax. Since both sides are internalized it is sufficient to use an
2617 // identity comparison.
2619 // End with the address of this class_name instance in temp register.
2620 // On MIPS, the caller must do the comparison with Handle<String>class_name.
2624 void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
2625 Register input = ToRegister(instr->value());
2626 Register temp = scratch0();
2627 Register temp2 = ToRegister(instr->temp());
2628 Handle<String> class_name = instr->hydrogen()->class_name();
2630 EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2631 class_name, input, temp, temp2);
2633 EmitBranch(instr, eq, temp, Operand(class_name));
2637 void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
2638 Register reg = ToRegister(instr->value());
2639 Register temp = ToRegister(instr->temp());
2641 __ lw(temp, FieldMemOperand(reg, HeapObject::kMapOffset));
2642 EmitBranch(instr, eq, temp, Operand(instr->map()));
2646 void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
2647 DCHECK(ToRegister(instr->context()).is(cp));
2648 Label true_label, done;
2649 DCHECK(ToRegister(instr->left()).is(a0)); // Object is in a0.
2650 DCHECK(ToRegister(instr->right()).is(a1)); // Function is in a1.
2651 Register result = ToRegister(instr->result());
2652 DCHECK(result.is(v0));
2654 InstanceofStub stub(isolate(), InstanceofStub::kArgsInRegisters);
2655 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2657 __ Branch(&true_label, eq, result, Operand(zero_reg));
2658 __ li(result, Operand(factory()->false_value()));
2660 __ bind(&true_label);
2661 __ li(result, Operand(factory()->true_value()));
2666 void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) {
2667 class DeferredInstanceOfKnownGlobal final : public LDeferredCode {
2669 DeferredInstanceOfKnownGlobal(LCodeGen* codegen,
2670 LInstanceOfKnownGlobal* instr)
2671 : LDeferredCode(codegen), instr_(instr) { }
2672 void Generate() override {
2673 codegen()->DoDeferredInstanceOfKnownGlobal(instr_, &map_check_);
2675 LInstruction* instr() override { return instr_; }
2676 Label* map_check() { return &map_check_; }
2679 LInstanceOfKnownGlobal* instr_;
2683 DeferredInstanceOfKnownGlobal* deferred;
2684 deferred = new(zone()) DeferredInstanceOfKnownGlobal(this, instr);
2686 Label done, false_result;
2687 Register object = ToRegister(instr->value());
2688 Register temp = ToRegister(instr->temp());
2689 Register result = ToRegister(instr->result());
2691 DCHECK(object.is(a0));
2692 DCHECK(result.is(v0));
2694 // A Smi is not instance of anything.
2695 __ JumpIfSmi(object, &false_result);
2697 // This is the inlined call site instanceof cache. The two occurences of the
2698 // hole value will be patched to the last map/result pair generated by the
2701 Register map = temp;
2702 __ lw(map, FieldMemOperand(object, HeapObject::kMapOffset));
2704 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
2705 __ bind(deferred->map_check()); // Label for calculating code patching.
2706 // We use Factory::the_hole_value() on purpose instead of loading from the
2707 // root array to force relocation to be able to later patch with
2709 Handle<Cell> cell = factory()->NewCell(factory()->the_hole_value());
2710 __ li(at, Operand(cell));
2711 __ lw(at, FieldMemOperand(at, Cell::kValueOffset));
2712 __ BranchShort(&cache_miss, ne, map, Operand(at));
2713 // We use Factory::the_hole_value() on purpose instead of loading from the
2714 // root array to force relocation to be able to later patch
2715 // with true or false. The distance from map check has to be constant.
2716 __ li(result, Operand(factory()->the_hole_value()), CONSTANT_SIZE);
2719 // The inlined call site cache did not match. Check null and string before
2720 // calling the deferred code.
2721 __ bind(&cache_miss);
2722 // Null is not instance of anything.
2723 __ LoadRoot(temp, Heap::kNullValueRootIndex);
2724 __ Branch(&false_result, eq, object, Operand(temp));
2726 // String values is not instance of anything.
2727 Condition cc = __ IsObjectStringType(object, temp, temp);
2728 __ Branch(&false_result, cc, temp, Operand(zero_reg));
2730 // Go to the deferred code.
2731 __ Branch(deferred->entry());
2733 __ bind(&false_result);
2734 __ LoadRoot(result, Heap::kFalseValueRootIndex);
2736 // Here result has either true or false. Deferred code also produces true or
2738 __ bind(deferred->exit());
2743 void LCodeGen::DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
2745 Register result = ToRegister(instr->result());
2746 DCHECK(result.is(v0));
2748 InstanceofStub::Flags flags = InstanceofStub::kNoFlags;
2749 flags = static_cast<InstanceofStub::Flags>(
2750 flags | InstanceofStub::kArgsInRegisters);
2751 flags = static_cast<InstanceofStub::Flags>(
2752 flags | InstanceofStub::kCallSiteInlineCheck);
2753 flags = static_cast<InstanceofStub::Flags>(
2754 flags | InstanceofStub::kReturnTrueFalseObject);
2755 InstanceofStub stub(isolate(), flags);
2757 PushSafepointRegistersScope scope(this);
2758 LoadContextFromDeferred(instr->context());
2760 // Get the temp register reserved by the instruction. This needs to be t0 as
2761 // its slot of the pushing of safepoint registers is used to communicate the
2762 // offset to the location of the map check.
2763 Register temp = ToRegister(instr->temp());
2764 DCHECK(temp.is(t0));
2765 __ li(InstanceofStub::right(), instr->function());
2766 static const int kAdditionalDelta = 7;
2767 int delta = masm_->InstructionsGeneratedSince(map_check) + kAdditionalDelta;
2768 Label before_push_delta;
2769 __ bind(&before_push_delta);
2771 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
2772 __ li(temp, Operand(delta * kPointerSize), CONSTANT_SIZE);
2773 __ StoreToSafepointRegisterSlot(temp, temp);
2775 CallCodeGeneric(stub.GetCode(),
2776 RelocInfo::CODE_TARGET,
2778 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
2779 LEnvironment* env = instr->GetDeferredLazyDeoptimizationEnvironment();
2780 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
2781 // Put the result value into the result register slot and
2782 // restore all registers.
2783 __ StoreToSafepointRegisterSlot(result, result);
2787 void LCodeGen::DoCmpT(LCmpT* instr) {
2788 DCHECK(ToRegister(instr->context()).is(cp));
2789 Token::Value op = instr->op();
2792 CodeFactory::CompareIC(isolate(), op, instr->strength()).code();
2793 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2794 // On MIPS there is no need for a "no inlined smi code" marker (nop).
2796 Condition condition = ComputeCompareCondition(op);
2797 // A minor optimization that relies on LoadRoot always emitting one
2799 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
2801 __ Branch(USE_DELAY_SLOT, &done, condition, v0, Operand(zero_reg));
2803 __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex);
2804 DCHECK_EQ(1, masm()->InstructionsGeneratedSince(&check));
2805 __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex);
2810 void LCodeGen::DoReturn(LReturn* instr) {
2811 if (FLAG_trace && info()->IsOptimizing()) {
2812 // Push the return value on the stack as the parameter.
2813 // Runtime::TraceExit returns its parameter in v0. We're leaving the code
2814 // managed by the register allocator and tearing down the frame, it's
2815 // safe to write to the context register.
2817 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2818 __ CallRuntime(Runtime::kTraceExit, 1);
2820 if (info()->saves_caller_doubles()) {
2821 RestoreCallerDoubles();
2823 int no_frame_start = -1;
2824 if (NeedsEagerFrame()) {
2826 no_frame_start = masm_->pc_offset();
2829 if (instr->has_constant_parameter_count()) {
2830 int parameter_count = ToInteger32(instr->constant_parameter_count());
2831 int32_t sp_delta = (parameter_count + 1) * kPointerSize;
2832 if (sp_delta != 0) {
2833 __ Addu(sp, sp, Operand(sp_delta));
2836 DCHECK(info()->IsStub()); // Functions would need to drop one more value.
2837 Register reg = ToRegister(instr->parameter_count());
2838 // The argument count parameter is a smi
2840 __ sll(at, reg, kPointerSizeLog2);
2841 __ Addu(sp, sp, at);
2846 if (no_frame_start != -1) {
2847 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
2853 void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
2854 Register vector_register = ToRegister(instr->temp_vector());
2855 Register slot_register = LoadWithVectorDescriptor::SlotRegister();
2856 DCHECK(vector_register.is(LoadWithVectorDescriptor::VectorRegister()));
2857 DCHECK(slot_register.is(a0));
2859 AllowDeferredHandleDereference vector_structure_check;
2860 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2861 __ li(vector_register, vector);
2862 // No need to allocate this register.
2863 FeedbackVectorICSlot slot = instr->hydrogen()->slot();
2864 int index = vector->GetIndex(slot);
2865 __ li(slot_register, Operand(Smi::FromInt(index)));
2870 void LCodeGen::EmitVectorStoreICRegisters(T* instr) {
2871 Register vector_register = ToRegister(instr->temp_vector());
2872 Register slot_register = ToRegister(instr->temp_slot());
2874 AllowDeferredHandleDereference vector_structure_check;
2875 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2876 __ li(vector_register, vector);
2877 FeedbackVectorICSlot slot = instr->hydrogen()->slot();
2878 int index = vector->GetIndex(slot);
2879 __ li(slot_register, Operand(Smi::FromInt(index)));
2883 void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
2884 DCHECK(ToRegister(instr->context()).is(cp));
2885 DCHECK(ToRegister(instr->global_object())
2886 .is(LoadDescriptor::ReceiverRegister()));
2887 DCHECK(ToRegister(instr->result()).is(v0));
2889 __ li(LoadDescriptor::NameRegister(), Operand(instr->name()));
2890 EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
2892 CodeFactory::LoadICInOptimizedCode(isolate(), instr->typeof_mode(),
2893 SLOPPY, PREMONOMORPHIC).code();
2894 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2898 void LCodeGen::DoLoadGlobalViaContext(LLoadGlobalViaContext* instr) {
2899 DCHECK(ToRegister(instr->context()).is(cp));
2900 DCHECK(ToRegister(instr->result()).is(v0));
2902 __ li(LoadGlobalViaContextDescriptor::DepthRegister(),
2903 Operand(Smi::FromInt(instr->depth())));
2904 __ li(LoadGlobalViaContextDescriptor::SlotRegister(),
2905 Operand(Smi::FromInt(instr->slot_index())));
2906 __ li(LoadGlobalViaContextDescriptor::NameRegister(), Operand(instr->name()));
2909 CodeFactory::LoadGlobalViaContext(isolate(), instr->depth()).code();
2910 CallCode(stub, RelocInfo::CODE_TARGET, instr);
2914 void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
2915 Register context = ToRegister(instr->context());
2916 Register result = ToRegister(instr->result());
2918 __ lw(result, ContextOperand(context, instr->slot_index()));
2919 if (instr->hydrogen()->RequiresHoleCheck()) {
2920 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2922 if (instr->hydrogen()->DeoptimizesOnHole()) {
2923 DeoptimizeIf(eq, instr, Deoptimizer::kHole, result, Operand(at));
2926 __ Branch(&is_not_hole, ne, result, Operand(at));
2927 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2928 __ bind(&is_not_hole);
2934 void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
2935 Register context = ToRegister(instr->context());
2936 Register value = ToRegister(instr->value());
2937 Register scratch = scratch0();
2938 MemOperand target = ContextOperand(context, instr->slot_index());
2940 Label skip_assignment;
2942 if (instr->hydrogen()->RequiresHoleCheck()) {
2943 __ lw(scratch, target);
2944 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2946 if (instr->hydrogen()->DeoptimizesOnHole()) {
2947 DeoptimizeIf(eq, instr, Deoptimizer::kHole, scratch, Operand(at));
2949 __ Branch(&skip_assignment, ne, scratch, Operand(at));
2953 __ sw(value, target);
2954 if (instr->hydrogen()->NeedsWriteBarrier()) {
2955 SmiCheck check_needed =
2956 instr->hydrogen()->value()->type().IsHeapObject()
2957 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2958 __ RecordWriteContextSlot(context,
2964 EMIT_REMEMBERED_SET,
2968 __ bind(&skip_assignment);
2972 void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
2973 HObjectAccess access = instr->hydrogen()->access();
2974 int offset = access.offset();
2975 Register object = ToRegister(instr->object());
2977 if (access.IsExternalMemory()) {
2978 Register result = ToRegister(instr->result());
2979 MemOperand operand = MemOperand(object, offset);
2980 __ Load(result, operand, access.representation());
2984 if (instr->hydrogen()->representation().IsDouble()) {
2985 DoubleRegister result = ToDoubleRegister(instr->result());
2986 __ ldc1(result, FieldMemOperand(object, offset));
2990 Register result = ToRegister(instr->result());
2991 if (!access.IsInobject()) {
2992 __ lw(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
2995 MemOperand operand = FieldMemOperand(object, offset);
2996 __ Load(result, operand, access.representation());
3000 void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
3001 DCHECK(ToRegister(instr->context()).is(cp));
3002 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3003 DCHECK(ToRegister(instr->result()).is(v0));
3005 // Name is always in a2.
3006 __ li(LoadDescriptor::NameRegister(), Operand(instr->name()));
3007 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
3009 CodeFactory::LoadICInOptimizedCode(
3010 isolate(), NOT_INSIDE_TYPEOF, instr->hydrogen()->language_mode(),
3011 instr->hydrogen()->initialization_state()).code();
3012 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3016 void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
3017 Register scratch = scratch0();
3018 Register function = ToRegister(instr->function());
3019 Register result = ToRegister(instr->result());
3021 // Get the prototype or initial map from the function.
3023 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
3025 // Check that the function has a prototype or an initial map.
3026 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
3027 DeoptimizeIf(eq, instr, Deoptimizer::kHole, result, Operand(at));
3029 // If the function does not have an initial map, we're done.
3031 __ GetObjectType(result, scratch, scratch);
3032 __ Branch(&done, ne, scratch, Operand(MAP_TYPE));
3034 // Get the prototype from the initial map.
3035 __ lw(result, FieldMemOperand(result, Map::kPrototypeOffset));
3042 void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
3043 Register result = ToRegister(instr->result());
3044 __ LoadRoot(result, instr->index());
3048 void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
3049 Register arguments = ToRegister(instr->arguments());
3050 Register result = ToRegister(instr->result());
3051 // There are two words between the frame pointer and the last argument.
3052 // Subtracting from length accounts for one of them add one more.
3053 if (instr->length()->IsConstantOperand()) {
3054 int const_length = ToInteger32(LConstantOperand::cast(instr->length()));
3055 if (instr->index()->IsConstantOperand()) {
3056 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3057 int index = (const_length - const_index) + 1;
3058 __ lw(result, MemOperand(arguments, index * kPointerSize));
3060 Register index = ToRegister(instr->index());
3061 __ li(at, Operand(const_length + 1));
3062 __ Subu(result, at, index);
3063 __ sll(at, result, kPointerSizeLog2);
3064 __ Addu(at, arguments, at);
3065 __ lw(result, MemOperand(at));
3067 } else if (instr->index()->IsConstantOperand()) {
3068 Register length = ToRegister(instr->length());
3069 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3070 int loc = const_index - 1;
3072 __ Subu(result, length, Operand(loc));
3073 __ sll(at, result, kPointerSizeLog2);
3074 __ Addu(at, arguments, at);
3075 __ lw(result, MemOperand(at));
3077 __ sll(at, length, kPointerSizeLog2);
3078 __ Addu(at, arguments, at);
3079 __ lw(result, MemOperand(at));
3082 Register length = ToRegister(instr->length());
3083 Register index = ToRegister(instr->index());
3084 __ Subu(result, length, index);
3085 __ Addu(result, result, 1);
3086 __ sll(at, result, kPointerSizeLog2);
3087 __ Addu(at, arguments, at);
3088 __ lw(result, MemOperand(at));
3093 void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
3094 Register external_pointer = ToRegister(instr->elements());
3095 Register key = no_reg;
3096 ElementsKind elements_kind = instr->elements_kind();
3097 bool key_is_constant = instr->key()->IsConstantOperand();
3098 int constant_key = 0;
3099 if (key_is_constant) {
3100 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3101 if (constant_key & 0xF0000000) {
3102 Abort(kArrayIndexConstantValueTooBig);
3105 key = ToRegister(instr->key());
3107 int element_size_shift = ElementsKindToShiftSize(elements_kind);
3108 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3109 ? (element_size_shift - kSmiTagSize) : element_size_shift;
3110 int base_offset = instr->base_offset();
3112 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3113 elements_kind == FLOAT32_ELEMENTS ||
3114 elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
3115 elements_kind == FLOAT64_ELEMENTS) {
3116 FPURegister result = ToDoubleRegister(instr->result());
3117 if (key_is_constant) {
3118 __ Addu(scratch0(), external_pointer, constant_key << element_size_shift);
3120 __ sll(scratch0(), key, shift_size);
3121 __ Addu(scratch0(), scratch0(), external_pointer);
3123 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3124 elements_kind == FLOAT32_ELEMENTS) {
3125 __ lwc1(result, MemOperand(scratch0(), base_offset));
3126 __ cvt_d_s(result, result);
3127 } else { // i.e. elements_kind == EXTERNAL_DOUBLE_ELEMENTS
3128 __ ldc1(result, MemOperand(scratch0(), base_offset));
3131 Register result = ToRegister(instr->result());
3132 MemOperand mem_operand = PrepareKeyedOperand(
3133 key, external_pointer, key_is_constant, constant_key,
3134 element_size_shift, shift_size, base_offset);
3135 switch (elements_kind) {
3136 case EXTERNAL_INT8_ELEMENTS:
3138 __ lb(result, mem_operand);
3140 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
3141 case EXTERNAL_UINT8_ELEMENTS:
3142 case UINT8_ELEMENTS:
3143 case UINT8_CLAMPED_ELEMENTS:
3144 __ lbu(result, mem_operand);
3146 case EXTERNAL_INT16_ELEMENTS:
3147 case INT16_ELEMENTS:
3148 __ lh(result, mem_operand);
3150 case EXTERNAL_UINT16_ELEMENTS:
3151 case UINT16_ELEMENTS:
3152 __ lhu(result, mem_operand);
3154 case EXTERNAL_INT32_ELEMENTS:
3155 case INT32_ELEMENTS:
3156 __ lw(result, mem_operand);
3158 case EXTERNAL_UINT32_ELEMENTS:
3159 case UINT32_ELEMENTS:
3160 __ lw(result, mem_operand);
3161 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
3162 DeoptimizeIf(Ugreater_equal, instr, Deoptimizer::kNegativeValue,
3163 result, Operand(0x80000000));
3166 case FLOAT32_ELEMENTS:
3167 case FLOAT64_ELEMENTS:
3168 case EXTERNAL_FLOAT32_ELEMENTS:
3169 case EXTERNAL_FLOAT64_ELEMENTS:
3170 case FAST_DOUBLE_ELEMENTS:
3172 case FAST_SMI_ELEMENTS:
3173 case FAST_HOLEY_DOUBLE_ELEMENTS:
3174 case FAST_HOLEY_ELEMENTS:
3175 case FAST_HOLEY_SMI_ELEMENTS:
3176 case DICTIONARY_ELEMENTS:
3177 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
3178 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
3186 void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
3187 Register elements = ToRegister(instr->elements());
3188 bool key_is_constant = instr->key()->IsConstantOperand();
3189 Register key = no_reg;
3190 DoubleRegister result = ToDoubleRegister(instr->result());
3191 Register scratch = scratch0();
3193 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
3195 int base_offset = instr->base_offset();
3196 if (key_is_constant) {
3197 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3198 if (constant_key & 0xF0000000) {
3199 Abort(kArrayIndexConstantValueTooBig);
3201 base_offset += constant_key * kDoubleSize;
3203 __ Addu(scratch, elements, Operand(base_offset));
3205 if (!key_is_constant) {
3206 key = ToRegister(instr->key());
3207 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3208 ? (element_size_shift - kSmiTagSize) : element_size_shift;
3209 __ sll(at, key, shift_size);
3210 __ Addu(scratch, scratch, at);
3213 __ ldc1(result, MemOperand(scratch));
3215 if (instr->hydrogen()->RequiresHoleCheck()) {
3216 __ lw(scratch, MemOperand(scratch, kHoleNanUpper32Offset));
3217 DeoptimizeIf(eq, instr, Deoptimizer::kHole, scratch,
3218 Operand(kHoleNanUpper32));
3223 void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
3224 Register elements = ToRegister(instr->elements());
3225 Register result = ToRegister(instr->result());
3226 Register scratch = scratch0();
3227 Register store_base = scratch;
3228 int offset = instr->base_offset();
3230 if (instr->key()->IsConstantOperand()) {
3231 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
3232 offset += ToInteger32(const_operand) * kPointerSize;
3233 store_base = elements;
3235 Register key = ToRegister(instr->key());
3236 // Even though the HLoadKeyed instruction forces the input
3237 // representation for the key to be an integer, the input gets replaced
3238 // during bound check elimination with the index argument to the bounds
3239 // check, which can be tagged, so that case must be handled here, too.
3240 if (instr->hydrogen()->key()->representation().IsSmi()) {
3241 __ sll(scratch, key, kPointerSizeLog2 - kSmiTagSize);
3242 __ addu(scratch, elements, scratch);
3244 __ sll(scratch, key, kPointerSizeLog2);
3245 __ addu(scratch, elements, scratch);
3248 __ lw(result, MemOperand(store_base, offset));
3250 // Check for the hole value.
3251 if (instr->hydrogen()->RequiresHoleCheck()) {
3252 if (IsFastSmiElementsKind(instr->hydrogen()->elements_kind())) {
3253 __ SmiTst(result, scratch);
3254 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi, scratch,
3257 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
3258 DeoptimizeIf(eq, instr, Deoptimizer::kHole, result, Operand(scratch));
3260 } else if (instr->hydrogen()->hole_mode() == CONVERT_HOLE_TO_UNDEFINED) {
3261 DCHECK(instr->hydrogen()->elements_kind() == FAST_HOLEY_ELEMENTS);
3263 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
3264 __ Branch(&done, ne, result, Operand(scratch));
3265 if (info()->IsStub()) {
3266 // A stub can safely convert the hole to undefined only if the array
3267 // protector cell contains (Smi) Isolate::kArrayProtectorValid. Otherwise
3268 // it needs to bail out.
3269 __ LoadRoot(result, Heap::kArrayProtectorRootIndex);
3270 __ lw(result, FieldMemOperand(result, Cell::kValueOffset));
3271 DeoptimizeIf(ne, instr, Deoptimizer::kHole, result,
3272 Operand(Smi::FromInt(Isolate::kArrayProtectorValid)));
3274 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3280 void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
3281 if (instr->is_typed_elements()) {
3282 DoLoadKeyedExternalArray(instr);
3283 } else if (instr->hydrogen()->representation().IsDouble()) {
3284 DoLoadKeyedFixedDoubleArray(instr);
3286 DoLoadKeyedFixedArray(instr);
3291 MemOperand LCodeGen::PrepareKeyedOperand(Register key,
3293 bool key_is_constant,
3298 if (key_is_constant) {
3299 return MemOperand(base, (constant_key << element_size) + base_offset);
3302 if (base_offset == 0) {
3303 if (shift_size >= 0) {
3304 __ sll(scratch0(), key, shift_size);
3305 __ Addu(scratch0(), base, scratch0());
3306 return MemOperand(scratch0());
3308 DCHECK_EQ(-1, shift_size);
3309 __ srl(scratch0(), key, 1);
3310 __ Addu(scratch0(), base, scratch0());
3311 return MemOperand(scratch0());
3315 if (shift_size >= 0) {
3316 __ sll(scratch0(), key, shift_size);
3317 __ Addu(scratch0(), base, scratch0());
3318 return MemOperand(scratch0(), base_offset);
3320 DCHECK_EQ(-1, shift_size);
3321 __ sra(scratch0(), key, 1);
3322 __ Addu(scratch0(), base, scratch0());
3323 return MemOperand(scratch0(), base_offset);
3328 void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3329 DCHECK(ToRegister(instr->context()).is(cp));
3330 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3331 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
3333 if (instr->hydrogen()->HasVectorAndSlot()) {
3334 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
3337 Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(
3338 isolate(), instr->hydrogen()->language_mode(),
3339 instr->hydrogen()->initialization_state()).code();
3340 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3344 void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
3345 Register scratch = scratch0();
3346 Register temp = scratch1();
3347 Register result = ToRegister(instr->result());
3349 if (instr->hydrogen()->from_inlined()) {
3350 __ Subu(result, sp, 2 * kPointerSize);
3352 // Check if the calling frame is an arguments adaptor frame.
3353 Label done, adapted;
3354 __ lw(scratch, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3355 __ lw(result, MemOperand(scratch, StandardFrameConstants::kContextOffset));
3356 __ Xor(temp, result, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3358 // Result is the frame pointer for the frame if not adapted and for the real
3359 // frame below the adaptor frame if adapted.
3360 __ Movn(result, fp, temp); // Move only if temp is not equal to zero (ne).
3361 __ Movz(result, scratch, temp); // Move only if temp is equal to zero (eq).
3366 void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
3367 Register elem = ToRegister(instr->elements());
3368 Register result = ToRegister(instr->result());
3372 // If no arguments adaptor frame the number of arguments is fixed.
3373 __ Addu(result, zero_reg, Operand(scope()->num_parameters()));
3374 __ Branch(&done, eq, fp, Operand(elem));
3376 // Arguments adaptor frame present. Get argument length from there.
3377 __ lw(result, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3379 MemOperand(result, ArgumentsAdaptorFrameConstants::kLengthOffset));
3380 __ SmiUntag(result);
3382 // Argument length is in result register.
3387 void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
3388 Register receiver = ToRegister(instr->receiver());
3389 Register function = ToRegister(instr->function());
3390 Register result = ToRegister(instr->result());
3391 Register scratch = scratch0();
3393 // If the receiver is null or undefined, we have to pass the global
3394 // object as a receiver to normal functions. Values have to be
3395 // passed unchanged to builtins and strict-mode functions.
3396 Label global_object, result_in_receiver;
3398 if (!instr->hydrogen()->known_function()) {
3399 // Do not transform the receiver to object for strict mode
3402 FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
3404 FieldMemOperand(scratch, SharedFunctionInfo::kCompilerHintsOffset));
3406 // Do not transform the receiver to object for builtins.
3407 int32_t strict_mode_function_mask =
3408 1 << (SharedFunctionInfo::kStrictModeFunction + kSmiTagSize);
3409 int32_t native_mask = 1 << (SharedFunctionInfo::kNative + kSmiTagSize);
3410 __ And(scratch, scratch, Operand(strict_mode_function_mask | native_mask));
3411 __ Branch(&result_in_receiver, ne, scratch, Operand(zero_reg));
3414 // Normal function. Replace undefined or null with global receiver.
3415 __ LoadRoot(scratch, Heap::kNullValueRootIndex);
3416 __ Branch(&global_object, eq, receiver, Operand(scratch));
3417 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
3418 __ Branch(&global_object, eq, receiver, Operand(scratch));
3420 // Deoptimize if the receiver is not a JS object.
3421 __ SmiTst(receiver, scratch);
3422 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, scratch, Operand(zero_reg));
3424 __ GetObjectType(receiver, scratch, scratch);
3425 DeoptimizeIf(lt, instr, Deoptimizer::kNotAJavaScriptObject, scratch,
3426 Operand(FIRST_SPEC_OBJECT_TYPE));
3428 __ Branch(&result_in_receiver);
3429 __ bind(&global_object);
3430 __ lw(result, FieldMemOperand(function, JSFunction::kContextOffset));
3432 ContextOperand(result, Context::GLOBAL_OBJECT_INDEX));
3434 FieldMemOperand(result, GlobalObject::kGlobalProxyOffset));
3436 if (result.is(receiver)) {
3437 __ bind(&result_in_receiver);
3440 __ Branch(&result_ok);
3441 __ bind(&result_in_receiver);
3442 __ mov(result, receiver);
3443 __ bind(&result_ok);
3448 void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3449 Register receiver = ToRegister(instr->receiver());
3450 Register function = ToRegister(instr->function());
3451 Register length = ToRegister(instr->length());
3452 Register elements = ToRegister(instr->elements());
3453 Register scratch = scratch0();
3454 DCHECK(receiver.is(a0)); // Used for parameter count.
3455 DCHECK(function.is(a1)); // Required by InvokeFunction.
3456 DCHECK(ToRegister(instr->result()).is(v0));
3458 // Copy the arguments to this function possibly from the
3459 // adaptor frame below it.
3460 const uint32_t kArgumentsLimit = 1 * KB;
3461 DeoptimizeIf(hi, instr, Deoptimizer::kTooManyArguments, length,
3462 Operand(kArgumentsLimit));
3464 // Push the receiver and use the register to keep the original
3465 // number of arguments.
3467 __ Move(receiver, length);
3468 // The arguments are at a one pointer size offset from elements.
3469 __ Addu(elements, elements, Operand(1 * kPointerSize));
3471 // Loop through the arguments pushing them onto the execution
3474 // length is a small non-negative integer, due to the test above.
3475 __ Branch(USE_DELAY_SLOT, &invoke, eq, length, Operand(zero_reg));
3476 __ sll(scratch, length, 2);
3478 __ Addu(scratch, elements, scratch);
3479 __ lw(scratch, MemOperand(scratch));
3481 __ Subu(length, length, Operand(1));
3482 __ Branch(USE_DELAY_SLOT, &loop, ne, length, Operand(zero_reg));
3483 __ sll(scratch, length, 2);
3486 DCHECK(instr->HasPointerMap());
3487 LPointerMap* pointers = instr->pointer_map();
3488 SafepointGenerator safepoint_generator(
3489 this, pointers, Safepoint::kLazyDeopt);
3490 // The number of arguments is stored in receiver which is a0, as expected
3491 // by InvokeFunction.
3492 ParameterCount actual(receiver);
3493 __ InvokeFunction(function, actual, CALL_FUNCTION, safepoint_generator);
3497 void LCodeGen::DoPushArgument(LPushArgument* instr) {
3498 LOperand* argument = instr->value();
3499 if (argument->IsDoubleRegister() || argument->IsDoubleStackSlot()) {
3500 Abort(kDoPushArgumentNotImplementedForDoubleType);
3502 Register argument_reg = EmitLoadRegister(argument, at);
3503 __ push(argument_reg);
3508 void LCodeGen::DoDrop(LDrop* instr) {
3509 __ Drop(instr->count());
3513 void LCodeGen::DoThisFunction(LThisFunction* instr) {
3514 Register result = ToRegister(instr->result());
3515 __ lw(result, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3519 void LCodeGen::DoContext(LContext* instr) {
3520 // If there is a non-return use, the context must be moved to a register.
3521 Register result = ToRegister(instr->result());
3522 if (info()->IsOptimizing()) {
3523 __ lw(result, MemOperand(fp, StandardFrameConstants::kContextOffset));
3525 // If there is no frame, the context must be in cp.
3526 DCHECK(result.is(cp));
3531 void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
3532 DCHECK(ToRegister(instr->context()).is(cp));
3533 __ li(scratch0(), instr->hydrogen()->pairs());
3534 __ li(scratch1(), Operand(Smi::FromInt(instr->hydrogen()->flags())));
3535 // The context is the first argument.
3536 __ Push(cp, scratch0(), scratch1());
3537 CallRuntime(Runtime::kDeclareGlobals, 3, instr);
3541 void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
3542 int formal_parameter_count, int arity,
3543 LInstruction* instr) {
3544 bool dont_adapt_arguments =
3545 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3546 bool can_invoke_directly =
3547 dont_adapt_arguments || formal_parameter_count == arity;
3549 Register function_reg = a1;
3550 LPointerMap* pointers = instr->pointer_map();
3552 if (can_invoke_directly) {
3554 __ lw(cp, FieldMemOperand(function_reg, JSFunction::kContextOffset));
3556 // Set r0 to arguments count if adaption is not needed. Assumes that r0
3557 // is available to write to at this point.
3558 if (dont_adapt_arguments) {
3559 __ li(a0, Operand(arity));
3563 __ lw(at, FieldMemOperand(function_reg, JSFunction::kCodeEntryOffset));
3566 // Set up deoptimization.
3567 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3569 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3570 ParameterCount count(arity);
3571 ParameterCount expected(formal_parameter_count);
3572 __ InvokeFunction(function_reg, expected, count, CALL_FUNCTION, generator);
3577 void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3578 DCHECK(instr->context() != NULL);
3579 DCHECK(ToRegister(instr->context()).is(cp));
3580 Register input = ToRegister(instr->value());
3581 Register result = ToRegister(instr->result());
3582 Register scratch = scratch0();
3584 // Deoptimize if not a heap number.
3585 __ lw(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
3586 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
3587 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, scratch, Operand(at));
3590 Register exponent = scratch0();
3592 __ lw(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3593 // Check the sign of the argument. If the argument is positive, just
3595 __ Move(result, input);
3596 __ And(at, exponent, Operand(HeapNumber::kSignMask));
3597 __ Branch(&done, eq, at, Operand(zero_reg));
3599 // Input is negative. Reverse its sign.
3600 // Preserve the value of all registers.
3602 PushSafepointRegistersScope scope(this);
3604 // Registers were saved at the safepoint, so we can use
3605 // many scratch registers.
3606 Register tmp1 = input.is(a1) ? a0 : a1;
3607 Register tmp2 = input.is(a2) ? a0 : a2;
3608 Register tmp3 = input.is(a3) ? a0 : a3;
3609 Register tmp4 = input.is(t0) ? a0 : t0;
3611 // exponent: floating point exponent value.
3613 Label allocated, slow;
3614 __ LoadRoot(tmp4, Heap::kHeapNumberMapRootIndex);
3615 __ AllocateHeapNumber(tmp1, tmp2, tmp3, tmp4, &slow);
3616 __ Branch(&allocated);
3618 // Slow case: Call the runtime system to do the number allocation.
3621 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr,
3623 // Set the pointer to the new heap number in tmp.
3626 // Restore input_reg after call to runtime.
3627 __ LoadFromSafepointRegisterSlot(input, input);
3628 __ lw(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3630 __ bind(&allocated);
3631 // exponent: floating point exponent value.
3632 // tmp1: allocated heap number.
3633 __ And(exponent, exponent, Operand(~HeapNumber::kSignMask));
3634 __ sw(exponent, FieldMemOperand(tmp1, HeapNumber::kExponentOffset));
3635 __ lw(tmp2, FieldMemOperand(input, HeapNumber::kMantissaOffset));
3636 __ sw(tmp2, FieldMemOperand(tmp1, HeapNumber::kMantissaOffset));
3638 __ StoreToSafepointRegisterSlot(tmp1, result);
3645 void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3646 Register input = ToRegister(instr->value());
3647 Register result = ToRegister(instr->result());
3648 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
3650 __ Branch(USE_DELAY_SLOT, &done, ge, input, Operand(zero_reg));
3651 __ mov(result, input);
3652 __ subu(result, zero_reg, input);
3653 // Overflow if result is still negative, i.e. 0x80000000.
3654 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, result, Operand(zero_reg));
3659 void LCodeGen::DoMathAbs(LMathAbs* instr) {
3660 // Class for deferred case.
3661 class DeferredMathAbsTaggedHeapNumber final : public LDeferredCode {
3663 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr)
3664 : LDeferredCode(codegen), instr_(instr) { }
3665 void Generate() override {
3666 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3668 LInstruction* instr() override { return instr_; }
3674 Representation r = instr->hydrogen()->value()->representation();
3676 FPURegister input = ToDoubleRegister(instr->value());
3677 FPURegister result = ToDoubleRegister(instr->result());
3678 __ abs_d(result, input);
3679 } else if (r.IsSmiOrInteger32()) {
3680 EmitIntegerMathAbs(instr);
3682 // Representation is tagged.
3683 DeferredMathAbsTaggedHeapNumber* deferred =
3684 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3685 Register input = ToRegister(instr->value());
3687 __ JumpIfNotSmi(input, deferred->entry());
3688 // If smi, handle it directly.
3689 EmitIntegerMathAbs(instr);
3690 __ bind(deferred->exit());
3695 void LCodeGen::DoMathFloor(LMathFloor* instr) {
3696 DoubleRegister input = ToDoubleRegister(instr->value());
3697 Register result = ToRegister(instr->result());
3698 Register scratch1 = scratch0();
3699 Register except_flag = ToRegister(instr->temp());
3701 __ EmitFPUTruncate(kRoundToMinusInf,
3708 // Deopt if the operation did not succeed.
3709 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
3712 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3715 __ Branch(&done, ne, result, Operand(zero_reg));
3716 __ Mfhc1(scratch1, input);
3717 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
3718 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
3725 void LCodeGen::DoMathRound(LMathRound* instr) {
3726 DoubleRegister input = ToDoubleRegister(instr->value());
3727 Register result = ToRegister(instr->result());
3728 DoubleRegister double_scratch1 = ToDoubleRegister(instr->temp());
3729 Register scratch = scratch0();
3730 Label done, check_sign_on_zero;
3732 // Extract exponent bits.
3733 __ Mfhc1(result, input);
3736 HeapNumber::kExponentShift,
3737 HeapNumber::kExponentBits);
3739 // If the number is in ]-0.5, +0.5[, the result is +/- 0.
3741 __ Branch(&skip1, gt, scratch, Operand(HeapNumber::kExponentBias - 2));
3742 __ mov(result, zero_reg);
3743 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3744 __ Branch(&check_sign_on_zero);
3750 // The following conversion will not work with numbers
3751 // outside of ]-2^32, 2^32[.
3752 DeoptimizeIf(ge, instr, Deoptimizer::kOverflow, scratch,
3753 Operand(HeapNumber::kExponentBias + 32));
3755 // Save the original sign for later comparison.
3756 __ And(scratch, result, Operand(HeapNumber::kSignMask));
3758 __ Move(double_scratch0(), 0.5);
3759 __ add_d(double_scratch0(), input, double_scratch0());
3761 // Check sign of the result: if the sign changed, the input
3762 // value was in ]0.5, 0[ and the result should be -0.
3763 __ Mfhc1(result, double_scratch0());
3764 __ Xor(result, result, Operand(scratch));
3765 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3766 // ARM uses 'mi' here, which is 'lt'
3767 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, result, Operand(zero_reg));
3770 // ARM uses 'mi' here, which is 'lt'
3771 // Negating it results in 'ge'
3772 __ Branch(&skip2, ge, result, Operand(zero_reg));
3773 __ mov(result, zero_reg);
3778 Register except_flag = scratch;
3779 __ EmitFPUTruncate(kRoundToMinusInf,
3786 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
3789 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3791 __ Branch(&done, ne, result, Operand(zero_reg));
3792 __ bind(&check_sign_on_zero);
3793 __ Mfhc1(scratch, input);
3794 __ And(scratch, scratch, Operand(HeapNumber::kSignMask));
3795 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch,
3802 void LCodeGen::DoMathFround(LMathFround* instr) {
3803 DoubleRegister input = ToDoubleRegister(instr->value());
3804 DoubleRegister result = ToDoubleRegister(instr->result());
3805 __ cvt_s_d(result.low(), input);
3806 __ cvt_d_s(result, result.low());
3810 void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3811 DoubleRegister input = ToDoubleRegister(instr->value());
3812 DoubleRegister result = ToDoubleRegister(instr->result());
3813 __ sqrt_d(result, input);
3817 void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3818 DoubleRegister input = ToDoubleRegister(instr->value());
3819 DoubleRegister result = ToDoubleRegister(instr->result());
3820 DoubleRegister temp = ToDoubleRegister(instr->temp());
3822 DCHECK(!input.is(result));
3824 // Note that according to ECMA-262 15.8.2.13:
3825 // Math.pow(-Infinity, 0.5) == Infinity
3826 // Math.sqrt(-Infinity) == NaN
3828 __ Move(temp, static_cast<double>(-V8_INFINITY));
3829 __ BranchF(USE_DELAY_SLOT, &done, NULL, eq, temp, input);
3830 // Set up Infinity in the delay slot.
3831 // result is overwritten if the branch is not taken.
3832 __ neg_d(result, temp);
3834 // Add +0 to convert -0 to +0.
3835 __ add_d(result, input, kDoubleRegZero);
3836 __ sqrt_d(result, result);
3841 void LCodeGen::DoPower(LPower* instr) {
3842 Representation exponent_type = instr->hydrogen()->right()->representation();
3843 // Having marked this as a call, we can use any registers.
3844 // Just make sure that the input/output registers are the expected ones.
3845 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3846 DCHECK(!instr->right()->IsDoubleRegister() ||
3847 ToDoubleRegister(instr->right()).is(f4));
3848 DCHECK(!instr->right()->IsRegister() ||
3849 ToRegister(instr->right()).is(tagged_exponent));
3850 DCHECK(ToDoubleRegister(instr->left()).is(f2));
3851 DCHECK(ToDoubleRegister(instr->result()).is(f0));
3853 if (exponent_type.IsSmi()) {
3854 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3856 } else if (exponent_type.IsTagged()) {
3858 __ JumpIfSmi(tagged_exponent, &no_deopt);
3859 DCHECK(!t3.is(tagged_exponent));
3860 __ lw(t3, FieldMemOperand(tagged_exponent, HeapObject::kMapOffset));
3861 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
3862 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, t3, Operand(at));
3864 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3866 } else if (exponent_type.IsInteger32()) {
3867 MathPowStub stub(isolate(), MathPowStub::INTEGER);
3870 DCHECK(exponent_type.IsDouble());
3871 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
3877 void LCodeGen::DoMathExp(LMathExp* instr) {
3878 DoubleRegister input = ToDoubleRegister(instr->value());
3879 DoubleRegister result = ToDoubleRegister(instr->result());
3880 DoubleRegister double_scratch1 = ToDoubleRegister(instr->double_temp());
3881 DoubleRegister double_scratch2 = double_scratch0();
3882 Register temp1 = ToRegister(instr->temp1());
3883 Register temp2 = ToRegister(instr->temp2());
3885 MathExpGenerator::EmitMathExp(
3886 masm(), input, result, double_scratch1, double_scratch2,
3887 temp1, temp2, scratch0());
3891 void LCodeGen::DoMathLog(LMathLog* instr) {
3892 __ PrepareCallCFunction(0, 1, scratch0());
3893 __ MovToFloatParameter(ToDoubleRegister(instr->value()));
3894 __ CallCFunction(ExternalReference::math_log_double_function(isolate()),
3896 __ MovFromFloatResult(ToDoubleRegister(instr->result()));
3900 void LCodeGen::DoMathClz32(LMathClz32* instr) {
3901 Register input = ToRegister(instr->value());
3902 Register result = ToRegister(instr->result());
3903 __ Clz(result, input);
3907 void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
3908 DCHECK(ToRegister(instr->context()).is(cp));
3909 DCHECK(ToRegister(instr->function()).is(a1));
3910 DCHECK(instr->HasPointerMap());
3912 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
3913 if (known_function.is_null()) {
3914 LPointerMap* pointers = instr->pointer_map();
3915 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3916 ParameterCount count(instr->arity());
3917 __ InvokeFunction(a1, count, CALL_FUNCTION, generator);
3919 CallKnownFunction(known_function,
3920 instr->hydrogen()->formal_parameter_count(),
3921 instr->arity(), instr);
3926 void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
3927 DCHECK(ToRegister(instr->result()).is(v0));
3929 if (instr->hydrogen()->IsTailCall()) {
3930 if (NeedsEagerFrame()) __ LeaveFrame(StackFrame::INTERNAL);
3932 if (instr->target()->IsConstantOperand()) {
3933 LConstantOperand* target = LConstantOperand::cast(instr->target());
3934 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3935 __ Jump(code, RelocInfo::CODE_TARGET);
3937 DCHECK(instr->target()->IsRegister());
3938 Register target = ToRegister(instr->target());
3939 __ Addu(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
3943 LPointerMap* pointers = instr->pointer_map();
3944 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3946 if (instr->target()->IsConstantOperand()) {
3947 LConstantOperand* target = LConstantOperand::cast(instr->target());
3948 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3949 generator.BeforeCall(__ CallSize(code, RelocInfo::CODE_TARGET));
3950 __ Call(code, RelocInfo::CODE_TARGET);
3952 DCHECK(instr->target()->IsRegister());
3953 Register target = ToRegister(instr->target());
3954 generator.BeforeCall(__ CallSize(target));
3955 __ Addu(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
3958 generator.AfterCall();
3963 void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
3964 DCHECK(ToRegister(instr->function()).is(a1));
3965 DCHECK(ToRegister(instr->result()).is(v0));
3967 if (instr->hydrogen()->pass_argument_count()) {
3968 __ li(a0, Operand(instr->arity()));
3972 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
3974 // Load the code entry address
3975 __ lw(at, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
3978 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3982 void LCodeGen::DoCallFunction(LCallFunction* instr) {
3983 DCHECK(ToRegister(instr->context()).is(cp));
3984 DCHECK(ToRegister(instr->function()).is(a1));
3985 DCHECK(ToRegister(instr->result()).is(v0));
3987 int arity = instr->arity();
3988 CallFunctionFlags flags = instr->hydrogen()->function_flags();
3989 if (instr->hydrogen()->HasVectorAndSlot()) {
3990 Register slot_register = ToRegister(instr->temp_slot());
3991 Register vector_register = ToRegister(instr->temp_vector());
3992 DCHECK(slot_register.is(a3));
3993 DCHECK(vector_register.is(a2));
3995 AllowDeferredHandleDereference vector_structure_check;
3996 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
3997 int index = vector->GetIndex(instr->hydrogen()->slot());
3999 __ li(vector_register, vector);
4000 __ li(slot_register, Operand(Smi::FromInt(index)));
4002 CallICState::CallType call_type =
4003 (flags & CALL_AS_METHOD) ? CallICState::METHOD : CallICState::FUNCTION;
4006 CodeFactory::CallICInOptimizedCode(isolate(), arity, call_type).code();
4007 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4009 CallFunctionStub stub(isolate(), arity, flags);
4010 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4015 void LCodeGen::DoCallNew(LCallNew* instr) {
4016 DCHECK(ToRegister(instr->context()).is(cp));
4017 DCHECK(ToRegister(instr->constructor()).is(a1));
4018 DCHECK(ToRegister(instr->result()).is(v0));
4020 __ li(a0, Operand(instr->arity()));
4021 // No cell in a2 for construct type feedback in optimized code
4022 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4023 CallConstructStub stub(isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
4024 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4028 void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
4029 DCHECK(ToRegister(instr->context()).is(cp));
4030 DCHECK(ToRegister(instr->constructor()).is(a1));
4031 DCHECK(ToRegister(instr->result()).is(v0));
4033 __ li(a0, Operand(instr->arity()));
4034 if (instr->arity() == 1) {
4035 // We only need the allocation site for the case we have a length argument.
4036 // The case may bail out to the runtime, which will determine the correct
4037 // elements kind with the site.
4038 __ li(a2, instr->hydrogen()->site());
4040 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4042 ElementsKind kind = instr->hydrogen()->elements_kind();
4043 AllocationSiteOverrideMode override_mode =
4044 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
4045 ? DISABLE_ALLOCATION_SITES
4048 if (instr->arity() == 0) {
4049 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
4050 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4051 } else if (instr->arity() == 1) {
4053 if (IsFastPackedElementsKind(kind)) {
4055 // We might need a change here,
4056 // look at the first argument.
4057 __ lw(t1, MemOperand(sp, 0));
4058 __ Branch(&packed_case, eq, t1, Operand(zero_reg));
4060 ElementsKind holey_kind = GetHoleyElementsKind(kind);
4061 ArraySingleArgumentConstructorStub stub(isolate(),
4064 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4066 __ bind(&packed_case);
4069 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
4070 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4073 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
4074 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4079 void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
4080 CallRuntime(instr->function(), instr->arity(), instr);
4084 void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
4085 Register function = ToRegister(instr->function());
4086 Register code_object = ToRegister(instr->code_object());
4087 __ Addu(code_object, code_object,
4088 Operand(Code::kHeaderSize - kHeapObjectTag));
4090 FieldMemOperand(function, JSFunction::kCodeEntryOffset));
4094 void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
4095 Register result = ToRegister(instr->result());
4096 Register base = ToRegister(instr->base_object());
4097 if (instr->offset()->IsConstantOperand()) {
4098 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
4099 __ Addu(result, base, Operand(ToInteger32(offset)));
4101 Register offset = ToRegister(instr->offset());
4102 __ Addu(result, base, offset);
4107 void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
4108 Representation representation = instr->representation();
4110 Register object = ToRegister(instr->object());
4111 Register scratch = scratch0();
4112 HObjectAccess access = instr->hydrogen()->access();
4113 int offset = access.offset();
4115 if (access.IsExternalMemory()) {
4116 Register value = ToRegister(instr->value());
4117 MemOperand operand = MemOperand(object, offset);
4118 __ Store(value, operand, representation);
4122 __ AssertNotSmi(object);
4124 DCHECK(!representation.IsSmi() ||
4125 !instr->value()->IsConstantOperand() ||
4126 IsSmi(LConstantOperand::cast(instr->value())));
4127 if (representation.IsDouble()) {
4128 DCHECK(access.IsInobject());
4129 DCHECK(!instr->hydrogen()->has_transition());
4130 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4131 DoubleRegister value = ToDoubleRegister(instr->value());
4132 __ sdc1(value, FieldMemOperand(object, offset));
4136 if (instr->hydrogen()->has_transition()) {
4137 Handle<Map> transition = instr->hydrogen()->transition_map();
4138 AddDeprecationDependency(transition);
4139 __ li(scratch, Operand(transition));
4140 __ sw(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
4141 if (instr->hydrogen()->NeedsWriteBarrierForMap()) {
4142 Register temp = ToRegister(instr->temp());
4143 // Update the write barrier for the map field.
4144 __ RecordWriteForMap(object,
4153 Register value = ToRegister(instr->value());
4154 if (access.IsInobject()) {
4155 MemOperand operand = FieldMemOperand(object, offset);
4156 __ Store(value, operand, representation);
4157 if (instr->hydrogen()->NeedsWriteBarrier()) {
4158 // Update the write barrier for the object for in-object properties.
4159 __ RecordWriteField(object,
4165 EMIT_REMEMBERED_SET,
4166 instr->hydrogen()->SmiCheckForWriteBarrier(),
4167 instr->hydrogen()->PointersToHereCheckForValue());
4170 __ lw(scratch, FieldMemOperand(object, JSObject::kPropertiesOffset));
4171 MemOperand operand = FieldMemOperand(scratch, offset);
4172 __ Store(value, operand, representation);
4173 if (instr->hydrogen()->NeedsWriteBarrier()) {
4174 // Update the write barrier for the properties array.
4175 // object is used as a scratch register.
4176 __ RecordWriteField(scratch,
4182 EMIT_REMEMBERED_SET,
4183 instr->hydrogen()->SmiCheckForWriteBarrier(),
4184 instr->hydrogen()->PointersToHereCheckForValue());
4190 void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
4191 DCHECK(ToRegister(instr->context()).is(cp));
4192 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4193 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4195 if (instr->hydrogen()->HasVectorAndSlot()) {
4196 EmitVectorStoreICRegisters<LStoreNamedGeneric>(instr);
4199 __ li(StoreDescriptor::NameRegister(), Operand(instr->name()));
4200 Handle<Code> ic = CodeFactory::StoreICInOptimizedCode(
4201 isolate(), instr->language_mode(),
4202 instr->hydrogen()->initialization_state()).code();
4203 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4207 void LCodeGen::DoStoreGlobalViaContext(LStoreGlobalViaContext* instr) {
4208 DCHECK(ToRegister(instr->context()).is(cp));
4209 DCHECK(ToRegister(instr->value())
4210 .is(StoreGlobalViaContextDescriptor::ValueRegister()));
4212 __ li(StoreGlobalViaContextDescriptor::DepthRegister(),
4213 Operand(Smi::FromInt(instr->depth())));
4214 __ li(StoreGlobalViaContextDescriptor::SlotRegister(),
4215 Operand(Smi::FromInt(instr->slot_index())));
4216 __ li(StoreGlobalViaContextDescriptor::NameRegister(),
4217 Operand(instr->name()));
4220 CodeFactory::StoreGlobalViaContext(isolate(), instr->depth(),
4221 instr->language_mode()).code();
4222 CallCode(stub, RelocInfo::CODE_TARGET, instr);
4226 void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
4227 Condition cc = instr->hydrogen()->allow_equality() ? hi : hs;
4230 if (instr->index()->IsConstantOperand()) {
4231 operand = ToOperand(instr->index());
4232 reg = ToRegister(instr->length());
4233 cc = CommuteCondition(cc);
4235 reg = ToRegister(instr->index());
4236 operand = ToOperand(instr->length());
4238 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
4240 __ Branch(&done, NegateCondition(cc), reg, operand);
4241 __ stop("eliminated bounds check failed");
4244 DeoptimizeIf(cc, instr, Deoptimizer::kOutOfBounds, reg, operand);
4249 void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
4250 Register external_pointer = ToRegister(instr->elements());
4251 Register key = no_reg;
4252 ElementsKind elements_kind = instr->elements_kind();
4253 bool key_is_constant = instr->key()->IsConstantOperand();
4254 int constant_key = 0;
4255 if (key_is_constant) {
4256 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4257 if (constant_key & 0xF0000000) {
4258 Abort(kArrayIndexConstantValueTooBig);
4261 key = ToRegister(instr->key());
4263 int element_size_shift = ElementsKindToShiftSize(elements_kind);
4264 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4265 ? (element_size_shift - kSmiTagSize) : element_size_shift;
4266 int base_offset = instr->base_offset();
4268 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4269 elements_kind == FLOAT32_ELEMENTS ||
4270 elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
4271 elements_kind == FLOAT64_ELEMENTS) {
4272 Register address = scratch0();
4273 FPURegister value(ToDoubleRegister(instr->value()));
4274 if (key_is_constant) {
4275 if (constant_key != 0) {
4276 __ Addu(address, external_pointer,
4277 Operand(constant_key << element_size_shift));
4279 address = external_pointer;
4282 __ sll(address, key, shift_size);
4283 __ Addu(address, external_pointer, address);
4286 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4287 elements_kind == FLOAT32_ELEMENTS) {
4288 __ cvt_s_d(double_scratch0(), value);
4289 __ swc1(double_scratch0(), MemOperand(address, base_offset));
4290 } else { // Storing doubles, not floats.
4291 __ sdc1(value, MemOperand(address, base_offset));
4294 Register value(ToRegister(instr->value()));
4295 MemOperand mem_operand = PrepareKeyedOperand(
4296 key, external_pointer, key_is_constant, constant_key,
4297 element_size_shift, shift_size,
4299 switch (elements_kind) {
4300 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
4301 case EXTERNAL_INT8_ELEMENTS:
4302 case EXTERNAL_UINT8_ELEMENTS:
4303 case UINT8_ELEMENTS:
4304 case UINT8_CLAMPED_ELEMENTS:
4306 __ sb(value, mem_operand);
4308 case EXTERNAL_INT16_ELEMENTS:
4309 case EXTERNAL_UINT16_ELEMENTS:
4310 case INT16_ELEMENTS:
4311 case UINT16_ELEMENTS:
4312 __ sh(value, mem_operand);
4314 case EXTERNAL_INT32_ELEMENTS:
4315 case EXTERNAL_UINT32_ELEMENTS:
4316 case INT32_ELEMENTS:
4317 case UINT32_ELEMENTS:
4318 __ sw(value, mem_operand);
4320 case FLOAT32_ELEMENTS:
4321 case FLOAT64_ELEMENTS:
4322 case EXTERNAL_FLOAT32_ELEMENTS:
4323 case EXTERNAL_FLOAT64_ELEMENTS:
4324 case FAST_DOUBLE_ELEMENTS:
4326 case FAST_SMI_ELEMENTS:
4327 case FAST_HOLEY_DOUBLE_ELEMENTS:
4328 case FAST_HOLEY_ELEMENTS:
4329 case FAST_HOLEY_SMI_ELEMENTS:
4330 case DICTIONARY_ELEMENTS:
4331 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
4332 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
4340 void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
4341 DoubleRegister value = ToDoubleRegister(instr->value());
4342 Register elements = ToRegister(instr->elements());
4343 Register scratch = scratch0();
4344 DoubleRegister double_scratch = double_scratch0();
4345 bool key_is_constant = instr->key()->IsConstantOperand();
4346 int base_offset = instr->base_offset();
4347 Label not_nan, done;
4349 // Calculate the effective address of the slot in the array to store the
4351 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
4352 if (key_is_constant) {
4353 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4354 if (constant_key & 0xF0000000) {
4355 Abort(kArrayIndexConstantValueTooBig);
4357 __ Addu(scratch, elements,
4358 Operand((constant_key << element_size_shift) + base_offset));
4360 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4361 ? (element_size_shift - kSmiTagSize) : element_size_shift;
4362 __ Addu(scratch, elements, Operand(base_offset));
4363 __ sll(at, ToRegister(instr->key()), shift_size);
4364 __ Addu(scratch, scratch, at);
4367 if (instr->NeedsCanonicalization()) {
4369 // Check for NaN. All NaNs must be canonicalized.
4370 __ BranchF(NULL, &is_nan, eq, value, value);
4371 __ Branch(¬_nan);
4373 // Only load canonical NaN if the comparison above set the overflow.
4375 __ LoadRoot(at, Heap::kNanValueRootIndex);
4376 __ ldc1(double_scratch, FieldMemOperand(at, HeapNumber::kValueOffset));
4377 __ sdc1(double_scratch, MemOperand(scratch, 0));
4382 __ sdc1(value, MemOperand(scratch, 0));
4387 void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4388 Register value = ToRegister(instr->value());
4389 Register elements = ToRegister(instr->elements());
4390 Register key = instr->key()->IsRegister() ? ToRegister(instr->key())
4392 Register scratch = scratch0();
4393 Register store_base = scratch;
4394 int offset = instr->base_offset();
4397 if (instr->key()->IsConstantOperand()) {
4398 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4399 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
4400 offset += ToInteger32(const_operand) * kPointerSize;
4401 store_base = elements;
4403 // Even though the HLoadKeyed instruction forces the input
4404 // representation for the key to be an integer, the input gets replaced
4405 // during bound check elimination with the index argument to the bounds
4406 // check, which can be tagged, so that case must be handled here, too.
4407 if (instr->hydrogen()->key()->representation().IsSmi()) {
4408 __ sll(scratch, key, kPointerSizeLog2 - kSmiTagSize);
4409 __ addu(scratch, elements, scratch);
4411 __ sll(scratch, key, kPointerSizeLog2);
4412 __ addu(scratch, elements, scratch);
4415 __ sw(value, MemOperand(store_base, offset));
4417 if (instr->hydrogen()->NeedsWriteBarrier()) {
4418 SmiCheck check_needed =
4419 instr->hydrogen()->value()->type().IsHeapObject()
4420 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4421 // Compute address of modified element and store it into key register.
4422 __ Addu(key, store_base, Operand(offset));
4423 __ RecordWrite(elements,
4428 EMIT_REMEMBERED_SET,
4430 instr->hydrogen()->PointersToHereCheckForValue());
4435 void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4436 // By cases: external, fast double
4437 if (instr->is_typed_elements()) {
4438 DoStoreKeyedExternalArray(instr);
4439 } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4440 DoStoreKeyedFixedDoubleArray(instr);
4442 DoStoreKeyedFixedArray(instr);
4447 void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4448 DCHECK(ToRegister(instr->context()).is(cp));
4449 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4450 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4451 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4453 if (instr->hydrogen()->HasVectorAndSlot()) {
4454 EmitVectorStoreICRegisters<LStoreKeyedGeneric>(instr);
4457 Handle<Code> ic = CodeFactory::KeyedStoreICInOptimizedCode(
4458 isolate(), instr->language_mode(),
4459 instr->hydrogen()->initialization_state()).code();
4460 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4464 void LCodeGen::DoMaybeGrowElements(LMaybeGrowElements* instr) {
4465 class DeferredMaybeGrowElements final : public LDeferredCode {
4467 DeferredMaybeGrowElements(LCodeGen* codegen, LMaybeGrowElements* instr)
4468 : LDeferredCode(codegen), instr_(instr) {}
4469 void Generate() override { codegen()->DoDeferredMaybeGrowElements(instr_); }
4470 LInstruction* instr() override { return instr_; }
4473 LMaybeGrowElements* instr_;
4476 Register result = v0;
4477 DeferredMaybeGrowElements* deferred =
4478 new (zone()) DeferredMaybeGrowElements(this, instr);
4479 LOperand* key = instr->key();
4480 LOperand* current_capacity = instr->current_capacity();
4482 DCHECK(instr->hydrogen()->key()->representation().IsInteger32());
4483 DCHECK(instr->hydrogen()->current_capacity()->representation().IsInteger32());
4484 DCHECK(key->IsConstantOperand() || key->IsRegister());
4485 DCHECK(current_capacity->IsConstantOperand() ||
4486 current_capacity->IsRegister());
4488 if (key->IsConstantOperand() && current_capacity->IsConstantOperand()) {
4489 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4490 int32_t constant_capacity =
4491 ToInteger32(LConstantOperand::cast(current_capacity));
4492 if (constant_key >= constant_capacity) {
4494 __ jmp(deferred->entry());
4496 } else if (key->IsConstantOperand()) {
4497 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4498 __ Branch(deferred->entry(), le, ToRegister(current_capacity),
4499 Operand(constant_key));
4500 } else if (current_capacity->IsConstantOperand()) {
4501 int32_t constant_capacity =
4502 ToInteger32(LConstantOperand::cast(current_capacity));
4503 __ Branch(deferred->entry(), ge, ToRegister(key),
4504 Operand(constant_capacity));
4506 __ Branch(deferred->entry(), ge, ToRegister(key),
4507 Operand(ToRegister(current_capacity)));
4510 if (instr->elements()->IsRegister()) {
4511 __ mov(result, ToRegister(instr->elements()));
4513 __ lw(result, ToMemOperand(instr->elements()));
4516 __ bind(deferred->exit());
4520 void LCodeGen::DoDeferredMaybeGrowElements(LMaybeGrowElements* instr) {
4521 // TODO(3095996): Get rid of this. For now, we need to make the
4522 // result register contain a valid pointer because it is already
4523 // contained in the register pointer map.
4524 Register result = v0;
4525 __ mov(result, zero_reg);
4527 // We have to call a stub.
4529 PushSafepointRegistersScope scope(this);
4530 if (instr->object()->IsRegister()) {
4531 __ mov(result, ToRegister(instr->object()));
4533 __ lw(result, ToMemOperand(instr->object()));
4536 LOperand* key = instr->key();
4537 if (key->IsConstantOperand()) {
4538 __ li(a3, Operand(ToSmi(LConstantOperand::cast(key))));
4540 __ mov(a3, ToRegister(key));
4544 GrowArrayElementsStub stub(isolate(), instr->hydrogen()->is_js_array(),
4545 instr->hydrogen()->kind());
4548 RecordSafepointWithLazyDeopt(
4549 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
4550 __ StoreToSafepointRegisterSlot(result, result);
4553 // Deopt on smi, which means the elements array changed to dictionary mode.
4554 __ SmiTst(result, at);
4555 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
4559 void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4560 Register object_reg = ToRegister(instr->object());
4561 Register scratch = scratch0();
4563 Handle<Map> from_map = instr->original_map();
4564 Handle<Map> to_map = instr->transitioned_map();
4565 ElementsKind from_kind = instr->from_kind();
4566 ElementsKind to_kind = instr->to_kind();
4568 Label not_applicable;
4569 __ lw(scratch, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4570 __ Branch(¬_applicable, ne, scratch, Operand(from_map));
4572 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
4573 Register new_map_reg = ToRegister(instr->new_map_temp());
4574 __ li(new_map_reg, Operand(to_map));
4575 __ sw(new_map_reg, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4577 __ RecordWriteForMap(object_reg,
4583 DCHECK(object_reg.is(a0));
4584 DCHECK(ToRegister(instr->context()).is(cp));
4585 PushSafepointRegistersScope scope(this);
4586 __ li(a1, Operand(to_map));
4587 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4588 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4590 RecordSafepointWithRegisters(
4591 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
4593 __ bind(¬_applicable);
4597 void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4598 Register object = ToRegister(instr->object());
4599 Register temp = ToRegister(instr->temp());
4600 Label no_memento_found;
4601 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found,
4602 ne, &no_memento_found);
4603 DeoptimizeIf(al, instr);
4604 __ bind(&no_memento_found);
4608 void LCodeGen::DoStringAdd(LStringAdd* instr) {
4609 DCHECK(ToRegister(instr->context()).is(cp));
4610 DCHECK(ToRegister(instr->left()).is(a1));
4611 DCHECK(ToRegister(instr->right()).is(a0));
4612 StringAddStub stub(isolate(),
4613 instr->hydrogen()->flags(),
4614 instr->hydrogen()->pretenure_flag());
4615 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4619 void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
4620 class DeferredStringCharCodeAt final : public LDeferredCode {
4622 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
4623 : LDeferredCode(codegen), instr_(instr) { }
4624 void Generate() override { codegen()->DoDeferredStringCharCodeAt(instr_); }
4625 LInstruction* instr() override { return instr_; }
4628 LStringCharCodeAt* instr_;
4631 DeferredStringCharCodeAt* deferred =
4632 new(zone()) DeferredStringCharCodeAt(this, instr);
4633 StringCharLoadGenerator::Generate(masm(),
4634 ToRegister(instr->string()),
4635 ToRegister(instr->index()),
4636 ToRegister(instr->result()),
4638 __ bind(deferred->exit());
4642 void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4643 Register string = ToRegister(instr->string());
4644 Register result = ToRegister(instr->result());
4645 Register scratch = scratch0();
4647 // TODO(3095996): Get rid of this. For now, we need to make the
4648 // result register contain a valid pointer because it is already
4649 // contained in the register pointer map.
4650 __ mov(result, zero_reg);
4652 PushSafepointRegistersScope scope(this);
4654 // Push the index as a smi. This is safe because of the checks in
4655 // DoStringCharCodeAt above.
4656 if (instr->index()->IsConstantOperand()) {
4657 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
4658 __ Addu(scratch, zero_reg, Operand(Smi::FromInt(const_index)));
4661 Register index = ToRegister(instr->index());
4665 CallRuntimeFromDeferred(Runtime::kStringCharCodeAtRT, 2, instr,
4669 __ StoreToSafepointRegisterSlot(v0, result);
4673 void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
4674 class DeferredStringCharFromCode final : public LDeferredCode {
4676 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
4677 : LDeferredCode(codegen), instr_(instr) { }
4678 void Generate() override {
4679 codegen()->DoDeferredStringCharFromCode(instr_);
4681 LInstruction* instr() override { return instr_; }
4684 LStringCharFromCode* instr_;
4687 DeferredStringCharFromCode* deferred =
4688 new(zone()) DeferredStringCharFromCode(this, instr);
4690 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
4691 Register char_code = ToRegister(instr->char_code());
4692 Register result = ToRegister(instr->result());
4693 Register scratch = scratch0();
4694 DCHECK(!char_code.is(result));
4696 __ Branch(deferred->entry(), hi,
4697 char_code, Operand(String::kMaxOneByteCharCode));
4698 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
4699 __ sll(scratch, char_code, kPointerSizeLog2);
4700 __ Addu(result, result, scratch);
4701 __ lw(result, FieldMemOperand(result, FixedArray::kHeaderSize));
4702 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
4703 __ Branch(deferred->entry(), eq, result, Operand(scratch));
4704 __ bind(deferred->exit());
4708 void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4709 Register char_code = ToRegister(instr->char_code());
4710 Register result = ToRegister(instr->result());
4712 // TODO(3095996): Get rid of this. For now, we need to make the
4713 // result register contain a valid pointer because it is already
4714 // contained in the register pointer map.
4715 __ mov(result, zero_reg);
4717 PushSafepointRegistersScope scope(this);
4718 __ SmiTag(char_code);
4720 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr, instr->context());
4721 __ StoreToSafepointRegisterSlot(v0, result);
4725 void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
4726 LOperand* input = instr->value();
4727 DCHECK(input->IsRegister() || input->IsStackSlot());
4728 LOperand* output = instr->result();
4729 DCHECK(output->IsDoubleRegister());
4730 FPURegister single_scratch = double_scratch0().low();
4731 if (input->IsStackSlot()) {
4732 Register scratch = scratch0();
4733 __ lw(scratch, ToMemOperand(input));
4734 __ mtc1(scratch, single_scratch);
4736 __ mtc1(ToRegister(input), single_scratch);
4738 __ cvt_d_w(ToDoubleRegister(output), single_scratch);
4742 void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4743 LOperand* input = instr->value();
4744 LOperand* output = instr->result();
4746 FPURegister dbl_scratch = double_scratch0();
4747 __ mtc1(ToRegister(input), dbl_scratch);
4748 __ Cvt_d_uw(ToDoubleRegister(output), dbl_scratch, f22);
4752 void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
4753 class DeferredNumberTagI final : public LDeferredCode {
4755 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
4756 : LDeferredCode(codegen), instr_(instr) { }
4757 void Generate() override {
4758 codegen()->DoDeferredNumberTagIU(instr_,
4764 LInstruction* instr() override { return instr_; }
4767 LNumberTagI* instr_;
4770 Register src = ToRegister(instr->value());
4771 Register dst = ToRegister(instr->result());
4772 Register overflow = scratch0();
4774 DeferredNumberTagI* deferred = new(zone()) DeferredNumberTagI(this, instr);
4775 __ SmiTagCheckOverflow(dst, src, overflow);
4776 __ BranchOnOverflow(deferred->entry(), overflow);
4777 __ bind(deferred->exit());
4781 void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4782 class DeferredNumberTagU final : public LDeferredCode {
4784 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4785 : LDeferredCode(codegen), instr_(instr) { }
4786 void Generate() override {
4787 codegen()->DoDeferredNumberTagIU(instr_,
4793 LInstruction* instr() override { return instr_; }
4796 LNumberTagU* instr_;
4799 Register input = ToRegister(instr->value());
4800 Register result = ToRegister(instr->result());
4802 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4803 __ Branch(deferred->entry(), hi, input, Operand(Smi::kMaxValue));
4804 __ SmiTag(result, input);
4805 __ bind(deferred->exit());
4809 void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4813 IntegerSignedness signedness) {
4815 Register src = ToRegister(value);
4816 Register dst = ToRegister(instr->result());
4817 Register tmp1 = scratch0();
4818 Register tmp2 = ToRegister(temp1);
4819 Register tmp3 = ToRegister(temp2);
4820 DoubleRegister dbl_scratch = double_scratch0();
4822 if (signedness == SIGNED_INT32) {
4823 // There was overflow, so bits 30 and 31 of the original integer
4824 // disagree. Try to allocate a heap number in new space and store
4825 // the value in there. If that fails, call the runtime system.
4827 __ SmiUntag(src, dst);
4828 __ Xor(src, src, Operand(0x80000000));
4830 __ mtc1(src, dbl_scratch);
4831 __ cvt_d_w(dbl_scratch, dbl_scratch);
4833 __ mtc1(src, dbl_scratch);
4834 __ Cvt_d_uw(dbl_scratch, dbl_scratch, f22);
4837 if (FLAG_inline_new) {
4838 __ LoadRoot(tmp3, Heap::kHeapNumberMapRootIndex);
4839 __ AllocateHeapNumber(dst, tmp1, tmp2, tmp3, &slow, DONT_TAG_RESULT);
4843 // Slow case: Call the runtime system to do the number allocation.
4846 // TODO(3095996): Put a valid pointer value in the stack slot where the
4847 // result register is stored, as this register is in the pointer map, but
4848 // contains an integer value.
4849 __ mov(dst, zero_reg);
4851 // Preserve the value of all registers.
4852 PushSafepointRegistersScope scope(this);
4854 // NumberTagI and NumberTagD use the context from the frame, rather than
4855 // the environment's HContext or HInlinedContext value.
4856 // They only call Runtime::kAllocateHeapNumber.
4857 // The corresponding HChange instructions are added in a phase that does
4858 // not have easy access to the local context.
4859 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4860 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4861 RecordSafepointWithRegisters(
4862 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4863 __ Subu(v0, v0, kHeapObjectTag);
4864 __ StoreToSafepointRegisterSlot(v0, dst);
4868 // Done. Put the value in dbl_scratch into the value of the allocated heap
4871 __ sdc1(dbl_scratch, MemOperand(dst, HeapNumber::kValueOffset));
4872 __ Addu(dst, dst, kHeapObjectTag);
4876 void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
4877 class DeferredNumberTagD final : public LDeferredCode {
4879 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4880 : LDeferredCode(codegen), instr_(instr) { }
4881 void Generate() override { codegen()->DoDeferredNumberTagD(instr_); }
4882 LInstruction* instr() override { return instr_; }
4885 LNumberTagD* instr_;
4888 DoubleRegister input_reg = ToDoubleRegister(instr->value());
4889 Register scratch = scratch0();
4890 Register reg = ToRegister(instr->result());
4891 Register temp1 = ToRegister(instr->temp());
4892 Register temp2 = ToRegister(instr->temp2());
4894 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
4895 if (FLAG_inline_new) {
4896 __ LoadRoot(scratch, Heap::kHeapNumberMapRootIndex);
4897 // We want the untagged address first for performance
4898 __ AllocateHeapNumber(reg, temp1, temp2, scratch, deferred->entry(),
4901 __ Branch(deferred->entry());
4903 __ bind(deferred->exit());
4904 __ sdc1(input_reg, MemOperand(reg, HeapNumber::kValueOffset));
4905 // Now that we have finished with the object's real address tag it
4906 __ Addu(reg, reg, kHeapObjectTag);
4910 void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4911 // TODO(3095996): Get rid of this. For now, we need to make the
4912 // result register contain a valid pointer because it is already
4913 // contained in the register pointer map.
4914 Register reg = ToRegister(instr->result());
4915 __ mov(reg, zero_reg);
4917 PushSafepointRegistersScope scope(this);
4918 // NumberTagI and NumberTagD use the context from the frame, rather than
4919 // the environment's HContext or HInlinedContext value.
4920 // They only call Runtime::kAllocateHeapNumber.
4921 // The corresponding HChange instructions are added in a phase that does
4922 // not have easy access to the local context.
4923 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4924 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4925 RecordSafepointWithRegisters(
4926 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4927 __ Subu(v0, v0, kHeapObjectTag);
4928 __ StoreToSafepointRegisterSlot(v0, reg);
4932 void LCodeGen::DoSmiTag(LSmiTag* instr) {
4933 HChange* hchange = instr->hydrogen();
4934 Register input = ToRegister(instr->value());
4935 Register output = ToRegister(instr->result());
4936 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4937 hchange->value()->CheckFlag(HValue::kUint32)) {
4938 __ And(at, input, Operand(0xc0000000));
4939 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow, at, Operand(zero_reg));
4941 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4942 !hchange->value()->CheckFlag(HValue::kUint32)) {
4943 __ SmiTagCheckOverflow(output, input, at);
4944 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, at, Operand(zero_reg));
4946 __ SmiTag(output, input);
4951 void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4952 Register scratch = scratch0();
4953 Register input = ToRegister(instr->value());
4954 Register result = ToRegister(instr->result());
4955 if (instr->needs_check()) {
4956 STATIC_ASSERT(kHeapObjectTag == 1);
4957 // If the input is a HeapObject, value of scratch won't be zero.
4958 __ And(scratch, input, Operand(kHeapObjectTag));
4959 __ SmiUntag(result, input);
4960 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi, scratch, Operand(zero_reg));
4962 __ SmiUntag(result, input);
4967 void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
4968 DoubleRegister result_reg,
4969 NumberUntagDMode mode) {
4970 bool can_convert_undefined_to_nan =
4971 instr->hydrogen()->can_convert_undefined_to_nan();
4972 bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
4974 Register scratch = scratch0();
4975 Label convert, load_smi, done;
4976 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4978 __ UntagAndJumpIfSmi(scratch, input_reg, &load_smi);
4979 // Heap number map check.
4980 __ lw(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
4981 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4982 if (can_convert_undefined_to_nan) {
4983 __ Branch(&convert, ne, scratch, Operand(at));
4985 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, scratch,
4988 // Load heap number.
4989 __ ldc1(result_reg, FieldMemOperand(input_reg, HeapNumber::kValueOffset));
4990 if (deoptimize_on_minus_zero) {
4991 __ mfc1(at, result_reg.low());
4992 __ Branch(&done, ne, at, Operand(zero_reg));
4993 __ Mfhc1(scratch, result_reg);
4994 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, scratch,
4995 Operand(HeapNumber::kSignMask));
4998 if (can_convert_undefined_to_nan) {
5000 // Convert undefined (and hole) to NaN.
5001 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5002 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefined, input_reg,
5004 __ LoadRoot(scratch, Heap::kNanValueRootIndex);
5005 __ ldc1(result_reg, FieldMemOperand(scratch, HeapNumber::kValueOffset));
5009 __ SmiUntag(scratch, input_reg);
5010 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
5012 // Smi to double register conversion
5014 // scratch: untagged value of input_reg
5015 __ mtc1(scratch, result_reg);
5016 __ cvt_d_w(result_reg, result_reg);
5021 void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) {
5022 Register input_reg = ToRegister(instr->value());
5023 Register scratch1 = scratch0();
5024 Register scratch2 = ToRegister(instr->temp());
5025 DoubleRegister double_scratch = double_scratch0();
5026 DoubleRegister double_scratch2 = ToDoubleRegister(instr->temp2());
5028 DCHECK(!scratch1.is(input_reg) && !scratch1.is(scratch2));
5029 DCHECK(!scratch2.is(input_reg) && !scratch2.is(scratch1));
5033 // The input is a tagged HeapObject.
5034 // Heap number map check.
5035 __ lw(scratch1, FieldMemOperand(input_reg, HeapObject::kMapOffset));
5036 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5037 // This 'at' value and scratch1 map value are used for tests in both clauses
5040 if (instr->truncating()) {
5041 // Performs a truncating conversion of a floating point number as used by
5042 // the JS bitwise operations.
5043 Label no_heap_number, check_bools, check_false;
5044 // Check HeapNumber map.
5045 __ Branch(USE_DELAY_SLOT, &no_heap_number, ne, scratch1, Operand(at));
5046 __ mov(scratch2, input_reg); // In delay slot.
5047 __ TruncateHeapNumberToI(input_reg, scratch2);
5050 // Check for Oddballs. Undefined/False is converted to zero and True to one
5051 // for truncating conversions.
5052 __ bind(&no_heap_number);
5053 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5054 __ Branch(&check_bools, ne, input_reg, Operand(at));
5055 DCHECK(ToRegister(instr->result()).is(input_reg));
5056 __ Branch(USE_DELAY_SLOT, &done);
5057 __ mov(input_reg, zero_reg); // In delay slot.
5059 __ bind(&check_bools);
5060 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5061 __ Branch(&check_false, ne, scratch2, Operand(at));
5062 __ Branch(USE_DELAY_SLOT, &done);
5063 __ li(input_reg, Operand(1)); // In delay slot.
5065 __ bind(&check_false);
5066 __ LoadRoot(at, Heap::kFalseValueRootIndex);
5067 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefinedBoolean,
5068 scratch2, Operand(at));
5069 __ Branch(USE_DELAY_SLOT, &done);
5070 __ mov(input_reg, zero_reg); // In delay slot.
5072 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, scratch1,
5075 // Load the double value.
5076 __ ldc1(double_scratch,
5077 FieldMemOperand(input_reg, HeapNumber::kValueOffset));
5079 Register except_flag = scratch2;
5080 __ EmitFPUTruncate(kRoundToZero,
5086 kCheckForInexactConversion);
5088 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
5091 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5092 __ Branch(&done, ne, input_reg, Operand(zero_reg));
5094 __ Mfhc1(scratch1, double_scratch);
5095 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
5096 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
5104 void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
5105 class DeferredTaggedToI final : public LDeferredCode {
5107 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
5108 : LDeferredCode(codegen), instr_(instr) { }
5109 void Generate() override { codegen()->DoDeferredTaggedToI(instr_); }
5110 LInstruction* instr() override { return instr_; }
5116 LOperand* input = instr->value();
5117 DCHECK(input->IsRegister());
5118 DCHECK(input->Equals(instr->result()));
5120 Register input_reg = ToRegister(input);
5122 if (instr->hydrogen()->value()->representation().IsSmi()) {
5123 __ SmiUntag(input_reg);
5125 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
5127 // Let the deferred code handle the HeapObject case.
5128 __ JumpIfNotSmi(input_reg, deferred->entry());
5130 // Smi to int32 conversion.
5131 __ SmiUntag(input_reg);
5132 __ bind(deferred->exit());
5137 void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
5138 LOperand* input = instr->value();
5139 DCHECK(input->IsRegister());
5140 LOperand* result = instr->result();
5141 DCHECK(result->IsDoubleRegister());
5143 Register input_reg = ToRegister(input);
5144 DoubleRegister result_reg = ToDoubleRegister(result);
5146 HValue* value = instr->hydrogen()->value();
5147 NumberUntagDMode mode = value->representation().IsSmi()
5148 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
5150 EmitNumberUntagD(instr, input_reg, result_reg, mode);
5154 void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
5155 Register result_reg = ToRegister(instr->result());
5156 Register scratch1 = scratch0();
5157 DoubleRegister double_input = ToDoubleRegister(instr->value());
5159 if (instr->truncating()) {
5160 __ TruncateDoubleToI(result_reg, double_input);
5162 Register except_flag = LCodeGen::scratch1();
5164 __ EmitFPUTruncate(kRoundToMinusInf,
5170 kCheckForInexactConversion);
5172 // Deopt if the operation did not succeed (except_flag != 0).
5173 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
5176 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5178 __ Branch(&done, ne, result_reg, Operand(zero_reg));
5179 __ Mfhc1(scratch1, double_input);
5180 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
5181 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
5189 void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
5190 Register result_reg = ToRegister(instr->result());
5191 Register scratch1 = LCodeGen::scratch0();
5192 DoubleRegister double_input = ToDoubleRegister(instr->value());
5194 if (instr->truncating()) {
5195 __ TruncateDoubleToI(result_reg, double_input);
5197 Register except_flag = LCodeGen::scratch1();
5199 __ EmitFPUTruncate(kRoundToMinusInf,
5205 kCheckForInexactConversion);
5207 // Deopt if the operation did not succeed (except_flag != 0).
5208 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
5211 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5213 __ Branch(&done, ne, result_reg, Operand(zero_reg));
5214 __ Mfhc1(scratch1, double_input);
5215 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
5216 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
5221 __ SmiTagCheckOverflow(result_reg, result_reg, scratch1);
5222 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, scratch1, Operand(zero_reg));
5226 void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
5227 LOperand* input = instr->value();
5228 __ SmiTst(ToRegister(input), at);
5229 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi, at, Operand(zero_reg));
5233 void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
5234 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
5235 LOperand* input = instr->value();
5236 __ SmiTst(ToRegister(input), at);
5237 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
5242 void LCodeGen::DoCheckArrayBufferNotNeutered(
5243 LCheckArrayBufferNotNeutered* instr) {
5244 Register view = ToRegister(instr->view());
5245 Register scratch = scratch0();
5247 __ lw(scratch, FieldMemOperand(view, JSArrayBufferView::kBufferOffset));
5248 __ lw(scratch, FieldMemOperand(scratch, JSArrayBuffer::kBitFieldOffset));
5249 __ And(at, scratch, 1 << JSArrayBuffer::WasNeutered::kShift);
5250 DeoptimizeIf(ne, instr, Deoptimizer::kOutOfBounds, at, Operand(zero_reg));
5254 void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
5255 Register input = ToRegister(instr->value());
5256 Register scratch = scratch0();
5258 __ GetObjectType(input, scratch, scratch);
5260 if (instr->hydrogen()->is_interval_check()) {
5263 instr->hydrogen()->GetCheckInterval(&first, &last);
5265 // If there is only one type in the interval check for equality.
5266 if (first == last) {
5267 DeoptimizeIf(ne, instr, Deoptimizer::kWrongInstanceType, scratch,
5270 DeoptimizeIf(lo, instr, Deoptimizer::kWrongInstanceType, scratch,
5272 // Omit check for the last type.
5273 if (last != LAST_TYPE) {
5274 DeoptimizeIf(hi, instr, Deoptimizer::kWrongInstanceType, scratch,
5281 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
5283 if (base::bits::IsPowerOfTwo32(mask)) {
5284 DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
5285 __ And(at, scratch, mask);
5286 DeoptimizeIf(tag == 0 ? ne : eq, instr, Deoptimizer::kWrongInstanceType,
5287 at, Operand(zero_reg));
5289 __ And(scratch, scratch, Operand(mask));
5290 DeoptimizeIf(ne, instr, Deoptimizer::kWrongInstanceType, scratch,
5297 void LCodeGen::DoCheckValue(LCheckValue* instr) {
5298 Register reg = ToRegister(instr->value());
5299 Handle<HeapObject> object = instr->hydrogen()->object().handle();
5300 AllowDeferredHandleDereference smi_check;
5301 if (isolate()->heap()->InNewSpace(*object)) {
5302 Register reg = ToRegister(instr->value());
5303 Handle<Cell> cell = isolate()->factory()->NewCell(object);
5304 __ li(at, Operand(cell));
5305 __ lw(at, FieldMemOperand(at, Cell::kValueOffset));
5306 DeoptimizeIf(ne, instr, Deoptimizer::kValueMismatch, reg, Operand(at));
5308 DeoptimizeIf(ne, instr, Deoptimizer::kValueMismatch, reg, Operand(object));
5313 void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
5315 PushSafepointRegistersScope scope(this);
5317 __ mov(cp, zero_reg);
5318 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
5319 RecordSafepointWithRegisters(
5320 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
5321 __ StoreToSafepointRegisterSlot(v0, scratch0());
5323 __ SmiTst(scratch0(), at);
5324 DeoptimizeIf(eq, instr, Deoptimizer::kInstanceMigrationFailed, at,
5329 void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
5330 class DeferredCheckMaps final : public LDeferredCode {
5332 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
5333 : LDeferredCode(codegen), instr_(instr), object_(object) {
5334 SetExit(check_maps());
5336 void Generate() override {
5337 codegen()->DoDeferredInstanceMigration(instr_, object_);
5339 Label* check_maps() { return &check_maps_; }
5340 LInstruction* instr() override { return instr_; }
5348 if (instr->hydrogen()->IsStabilityCheck()) {
5349 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5350 for (int i = 0; i < maps->size(); ++i) {
5351 AddStabilityDependency(maps->at(i).handle());
5356 Register map_reg = scratch0();
5357 LOperand* input = instr->value();
5358 DCHECK(input->IsRegister());
5359 Register reg = ToRegister(input);
5360 __ lw(map_reg, FieldMemOperand(reg, HeapObject::kMapOffset));
5362 DeferredCheckMaps* deferred = NULL;
5363 if (instr->hydrogen()->HasMigrationTarget()) {
5364 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5365 __ bind(deferred->check_maps());
5368 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5370 for (int i = 0; i < maps->size() - 1; i++) {
5371 Handle<Map> map = maps->at(i).handle();
5372 __ CompareMapAndBranch(map_reg, map, &success, eq, &success);
5374 Handle<Map> map = maps->at(maps->size() - 1).handle();
5375 // Do the CompareMap() directly within the Branch() and DeoptimizeIf().
5376 if (instr->hydrogen()->HasMigrationTarget()) {
5377 __ Branch(deferred->entry(), ne, map_reg, Operand(map));
5379 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap, map_reg, Operand(map));
5386 void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
5387 DoubleRegister value_reg = ToDoubleRegister(instr->unclamped());
5388 Register result_reg = ToRegister(instr->result());
5389 DoubleRegister temp_reg = ToDoubleRegister(instr->temp());
5390 __ ClampDoubleToUint8(result_reg, value_reg, temp_reg);
5394 void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
5395 Register unclamped_reg = ToRegister(instr->unclamped());
5396 Register result_reg = ToRegister(instr->result());
5397 __ ClampUint8(result_reg, unclamped_reg);
5401 void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
5402 Register scratch = scratch0();
5403 Register input_reg = ToRegister(instr->unclamped());
5404 Register result_reg = ToRegister(instr->result());
5405 DoubleRegister temp_reg = ToDoubleRegister(instr->temp());
5406 Label is_smi, done, heap_number;
5408 // Both smi and heap number cases are handled.
5409 __ UntagAndJumpIfSmi(scratch, input_reg, &is_smi);
5411 // Check for heap number
5412 __ lw(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
5413 __ Branch(&heap_number, eq, scratch, Operand(factory()->heap_number_map()));
5415 // Check for undefined. Undefined is converted to zero for clamping
5417 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefined, input_reg,
5418 Operand(factory()->undefined_value()));
5419 __ mov(result_reg, zero_reg);
5423 __ bind(&heap_number);
5424 __ ldc1(double_scratch0(), FieldMemOperand(input_reg,
5425 HeapNumber::kValueOffset));
5426 __ ClampDoubleToUint8(result_reg, double_scratch0(), temp_reg);
5430 __ ClampUint8(result_reg, scratch);
5436 void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5437 DoubleRegister value_reg = ToDoubleRegister(instr->value());
5438 Register result_reg = ToRegister(instr->result());
5439 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5440 __ FmoveHigh(result_reg, value_reg);
5442 __ FmoveLow(result_reg, value_reg);
5447 void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5448 Register hi_reg = ToRegister(instr->hi());
5449 Register lo_reg = ToRegister(instr->lo());
5450 DoubleRegister result_reg = ToDoubleRegister(instr->result());
5451 __ Move(result_reg, lo_reg, hi_reg);
5455 void LCodeGen::DoAllocate(LAllocate* instr) {
5456 class DeferredAllocate final : public LDeferredCode {
5458 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
5459 : LDeferredCode(codegen), instr_(instr) { }
5460 void Generate() override { codegen()->DoDeferredAllocate(instr_); }
5461 LInstruction* instr() override { return instr_; }
5467 DeferredAllocate* deferred =
5468 new(zone()) DeferredAllocate(this, instr);
5470 Register result = ToRegister(instr->result());
5471 Register scratch = ToRegister(instr->temp1());
5472 Register scratch2 = ToRegister(instr->temp2());
5474 // Allocate memory for the object.
5475 AllocationFlags flags = TAG_OBJECT;
5476 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5477 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5479 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5480 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5481 flags = static_cast<AllocationFlags>(flags | PRETENURE);
5483 if (instr->size()->IsConstantOperand()) {
5484 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5485 if (size <= Page::kMaxRegularHeapObjectSize) {
5486 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5488 __ jmp(deferred->entry());
5491 Register size = ToRegister(instr->size());
5492 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5495 __ bind(deferred->exit());
5497 if (instr->hydrogen()->MustPrefillWithFiller()) {
5498 STATIC_ASSERT(kHeapObjectTag == 1);
5499 if (instr->size()->IsConstantOperand()) {
5500 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5501 __ li(scratch, Operand(size - kHeapObjectTag));
5503 __ Subu(scratch, ToRegister(instr->size()), Operand(kHeapObjectTag));
5505 __ li(scratch2, Operand(isolate()->factory()->one_pointer_filler_map()));
5508 __ Subu(scratch, scratch, Operand(kPointerSize));
5509 __ Addu(at, result, Operand(scratch));
5510 __ sw(scratch2, MemOperand(at));
5511 __ Branch(&loop, ge, scratch, Operand(zero_reg));
5516 void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
5517 Register result = ToRegister(instr->result());
5519 // TODO(3095996): Get rid of this. For now, we need to make the
5520 // result register contain a valid pointer because it is already
5521 // contained in the register pointer map.
5522 __ mov(result, zero_reg);
5524 PushSafepointRegistersScope scope(this);
5525 if (instr->size()->IsRegister()) {
5526 Register size = ToRegister(instr->size());
5527 DCHECK(!size.is(result));
5531 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5532 if (size >= 0 && size <= Smi::kMaxValue) {
5533 __ Push(Smi::FromInt(size));
5535 // We should never get here at runtime => abort
5536 __ stop("invalid allocation size");
5541 int flags = AllocateDoubleAlignFlag::encode(
5542 instr->hydrogen()->MustAllocateDoubleAligned());
5543 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5544 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5545 flags = AllocateTargetSpace::update(flags, OLD_SPACE);
5547 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5549 __ Push(Smi::FromInt(flags));
5551 CallRuntimeFromDeferred(
5552 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
5553 __ StoreToSafepointRegisterSlot(v0, result);
5557 void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
5558 DCHECK(ToRegister(instr->value()).is(a0));
5559 DCHECK(ToRegister(instr->result()).is(v0));
5561 CallRuntime(Runtime::kToFastProperties, 1, instr);
5565 void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
5566 DCHECK(ToRegister(instr->context()).is(cp));
5568 // Registers will be used as follows:
5569 // t3 = literals array.
5570 // a1 = regexp literal.
5571 // a0 = regexp literal clone.
5572 // a2 and t0-t2 are used as temporaries.
5573 int literal_offset =
5574 FixedArray::OffsetOfElementAt(instr->hydrogen()->literal_index());
5575 __ li(t3, instr->hydrogen()->literals());
5576 __ lw(a1, FieldMemOperand(t3, literal_offset));
5577 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5578 __ Branch(&materialized, ne, a1, Operand(at));
5580 // Create regexp literal using runtime function
5581 // Result will be in v0.
5582 __ li(t2, Operand(Smi::FromInt(instr->hydrogen()->literal_index())));
5583 __ li(t1, Operand(instr->hydrogen()->pattern()));
5584 __ li(t0, Operand(instr->hydrogen()->flags()));
5585 __ Push(t3, t2, t1, t0);
5586 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
5589 __ bind(&materialized);
5590 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
5591 Label allocated, runtime_allocate;
5593 __ Allocate(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
5596 __ bind(&runtime_allocate);
5597 __ li(a0, Operand(Smi::FromInt(size)));
5599 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
5602 __ bind(&allocated);
5603 // Copy the content into the newly allocated memory.
5604 // (Unroll copy loop once for better throughput).
5605 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
5606 __ lw(a3, FieldMemOperand(a1, i));
5607 __ lw(a2, FieldMemOperand(a1, i + kPointerSize));
5608 __ sw(a3, FieldMemOperand(v0, i));
5609 __ sw(a2, FieldMemOperand(v0, i + kPointerSize));
5611 if ((size % (2 * kPointerSize)) != 0) {
5612 __ lw(a3, FieldMemOperand(a1, size - kPointerSize));
5613 __ sw(a3, FieldMemOperand(v0, size - kPointerSize));
5618 void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
5619 DCHECK(ToRegister(instr->context()).is(cp));
5620 // Use the fast case closure allocation code that allocates in new
5621 // space for nested functions that don't need literals cloning.
5622 bool pretenure = instr->hydrogen()->pretenure();
5623 if (!pretenure && instr->hydrogen()->has_no_literals()) {
5624 FastNewClosureStub stub(isolate(), instr->hydrogen()->language_mode(),
5625 instr->hydrogen()->kind());
5626 __ li(a2, Operand(instr->hydrogen()->shared_info()));
5627 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5629 __ li(a2, Operand(instr->hydrogen()->shared_info()));
5630 __ li(a1, Operand(pretenure ? factory()->true_value()
5631 : factory()->false_value()));
5632 __ Push(cp, a2, a1);
5633 CallRuntime(Runtime::kNewClosure, 3, instr);
5638 void LCodeGen::DoTypeof(LTypeof* instr) {
5639 DCHECK(ToRegister(instr->value()).is(a3));
5640 DCHECK(ToRegister(instr->result()).is(v0));
5642 Register value_register = ToRegister(instr->value());
5643 __ JumpIfNotSmi(value_register, &do_call);
5644 __ li(v0, Operand(isolate()->factory()->number_string()));
5647 TypeofStub stub(isolate());
5648 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5653 void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5654 Register input = ToRegister(instr->value());
5656 Register cmp1 = no_reg;
5657 Operand cmp2 = Operand(no_reg);
5659 Condition final_branch_condition = EmitTypeofIs(instr->TrueLabel(chunk_),
5660 instr->FalseLabel(chunk_),
5662 instr->type_literal(),
5666 DCHECK(cmp1.is_valid());
5667 DCHECK(!cmp2.is_reg() || cmp2.rm().is_valid());
5669 if (final_branch_condition != kNoCondition) {
5670 EmitBranch(instr, final_branch_condition, cmp1, cmp2);
5675 Condition LCodeGen::EmitTypeofIs(Label* true_label,
5678 Handle<String> type_name,
5681 // This function utilizes the delay slot heavily. This is used to load
5682 // values that are always usable without depending on the type of the input
5684 Condition final_branch_condition = kNoCondition;
5685 Register scratch = scratch0();
5686 Factory* factory = isolate()->factory();
5687 if (String::Equals(type_name, factory->number_string())) {
5688 __ JumpIfSmi(input, true_label);
5689 __ lw(input, FieldMemOperand(input, HeapObject::kMapOffset));
5690 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5692 *cmp2 = Operand(at);
5693 final_branch_condition = eq;
5695 } else if (String::Equals(type_name, factory->string_string())) {
5696 __ JumpIfSmi(input, false_label);
5697 __ GetObjectType(input, input, scratch);
5698 __ Branch(USE_DELAY_SLOT, false_label,
5699 ge, scratch, Operand(FIRST_NONSTRING_TYPE));
5700 // input is an object so we can load the BitFieldOffset even if we take the
5702 __ lbu(at, FieldMemOperand(input, Map::kBitFieldOffset));
5703 __ And(at, at, 1 << Map::kIsUndetectable);
5705 *cmp2 = Operand(zero_reg);
5706 final_branch_condition = eq;
5708 } else if (String::Equals(type_name, factory->symbol_string())) {
5709 __ JumpIfSmi(input, false_label);
5710 __ GetObjectType(input, input, scratch);
5712 *cmp2 = Operand(SYMBOL_TYPE);
5713 final_branch_condition = eq;
5715 } else if (String::Equals(type_name, factory->boolean_string())) {
5716 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5717 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5718 __ LoadRoot(at, Heap::kFalseValueRootIndex);
5720 *cmp2 = Operand(input);
5721 final_branch_condition = eq;
5723 } else if (String::Equals(type_name, factory->undefined_string())) {
5724 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5725 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5726 // The first instruction of JumpIfSmi is an And - it is safe in the delay
5728 __ JumpIfSmi(input, false_label);
5729 // Check for undetectable objects => true.
5730 __ lw(input, FieldMemOperand(input, HeapObject::kMapOffset));
5731 __ lbu(at, FieldMemOperand(input, Map::kBitFieldOffset));
5732 __ And(at, at, 1 << Map::kIsUndetectable);
5734 *cmp2 = Operand(zero_reg);
5735 final_branch_condition = ne;
5737 } else if (String::Equals(type_name, factory->function_string())) {
5738 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5739 __ JumpIfSmi(input, false_label);
5740 __ GetObjectType(input, scratch, input);
5741 __ Branch(true_label, eq, input, Operand(JS_FUNCTION_TYPE));
5743 *cmp2 = Operand(JS_FUNCTION_PROXY_TYPE);
5744 final_branch_condition = eq;
5746 } else if (String::Equals(type_name, factory->object_string())) {
5747 __ JumpIfSmi(input, false_label);
5748 __ LoadRoot(at, Heap::kNullValueRootIndex);
5749 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5750 Register map = input;
5751 __ GetObjectType(input, map, scratch);
5752 __ Branch(false_label,
5753 lt, scratch, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
5754 __ Branch(USE_DELAY_SLOT, false_label,
5755 gt, scratch, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
5756 // map is still valid, so the BitField can be loaded in delay slot.
5757 // Check for undetectable objects => false.
5758 __ lbu(at, FieldMemOperand(map, Map::kBitFieldOffset));
5759 __ And(at, at, 1 << Map::kIsUndetectable);
5761 *cmp2 = Operand(zero_reg);
5762 final_branch_condition = eq;
5764 } else if (String::Equals(type_name, factory->float32x4_string())) {
5765 __ JumpIfSmi(input, false_label);
5766 __ GetObjectType(input, input, scratch);
5768 *cmp2 = Operand(FLOAT32X4_TYPE);
5769 final_branch_condition = eq;
5773 *cmp2 = Operand(zero_reg); // Set to valid regs, to avoid caller assertion.
5774 __ Branch(false_label);
5777 return final_branch_condition;
5781 void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
5782 Register temp1 = ToRegister(instr->temp());
5784 EmitIsConstructCall(temp1, scratch0());
5786 EmitBranch(instr, eq, temp1,
5787 Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
5791 void LCodeGen::EmitIsConstructCall(Register temp1, Register temp2) {
5792 DCHECK(!temp1.is(temp2));
5793 // Get the frame pointer for the calling frame.
5794 __ lw(temp1, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5796 // Skip the arguments adaptor frame if it exists.
5797 Label check_frame_marker;
5798 __ lw(temp2, MemOperand(temp1, StandardFrameConstants::kContextOffset));
5799 __ Branch(&check_frame_marker, ne, temp2,
5800 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5801 __ lw(temp1, MemOperand(temp1, StandardFrameConstants::kCallerFPOffset));
5803 // Check the marker in the calling frame.
5804 __ bind(&check_frame_marker);
5805 __ lw(temp1, MemOperand(temp1, StandardFrameConstants::kMarkerOffset));
5809 void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5810 if (!info()->IsStub()) {
5811 // Ensure that we have enough space after the previous lazy-bailout
5812 // instruction for patching the code here.
5813 int current_pc = masm()->pc_offset();
5814 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5815 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5816 DCHECK_EQ(0, padding_size % Assembler::kInstrSize);
5817 while (padding_size > 0) {
5819 padding_size -= Assembler::kInstrSize;
5823 last_lazy_deopt_pc_ = masm()->pc_offset();
5827 void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
5828 last_lazy_deopt_pc_ = masm()->pc_offset();
5829 DCHECK(instr->HasEnvironment());
5830 LEnvironment* env = instr->environment();
5831 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5832 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5836 void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
5837 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5838 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5839 // needed return address), even though the implementation of LAZY and EAGER is
5840 // now identical. When LAZY is eventually completely folded into EAGER, remove
5841 // the special case below.
5842 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5843 type = Deoptimizer::LAZY;
5846 DeoptimizeIf(al, instr, instr->hydrogen()->reason(), type, zero_reg,
5851 void LCodeGen::DoDummy(LDummy* instr) {
5852 // Nothing to see here, move on!
5856 void LCodeGen::DoDummyUse(LDummyUse* instr) {
5857 // Nothing to see here, move on!
5861 void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
5862 PushSafepointRegistersScope scope(this);
5863 LoadContextFromDeferred(instr->context());
5864 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5865 RecordSafepointWithLazyDeopt(
5866 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
5867 DCHECK(instr->HasEnvironment());
5868 LEnvironment* env = instr->environment();
5869 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5873 void LCodeGen::DoStackCheck(LStackCheck* instr) {
5874 class DeferredStackCheck final : public LDeferredCode {
5876 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5877 : LDeferredCode(codegen), instr_(instr) { }
5878 void Generate() override { codegen()->DoDeferredStackCheck(instr_); }
5879 LInstruction* instr() override { return instr_; }
5882 LStackCheck* instr_;
5885 DCHECK(instr->HasEnvironment());
5886 LEnvironment* env = instr->environment();
5887 // There is no LLazyBailout instruction for stack-checks. We have to
5888 // prepare for lazy deoptimization explicitly here.
5889 if (instr->hydrogen()->is_function_entry()) {
5890 // Perform stack overflow check.
5892 __ LoadRoot(at, Heap::kStackLimitRootIndex);
5893 __ Branch(&done, hs, sp, Operand(at));
5894 DCHECK(instr->context()->IsRegister());
5895 DCHECK(ToRegister(instr->context()).is(cp));
5896 CallCode(isolate()->builtins()->StackCheck(),
5897 RelocInfo::CODE_TARGET,
5901 DCHECK(instr->hydrogen()->is_backwards_branch());
5902 // Perform stack overflow check if this goto needs it before jumping.
5903 DeferredStackCheck* deferred_stack_check =
5904 new(zone()) DeferredStackCheck(this, instr);
5905 __ LoadRoot(at, Heap::kStackLimitRootIndex);
5906 __ Branch(deferred_stack_check->entry(), lo, sp, Operand(at));
5907 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
5908 __ bind(instr->done_label());
5909 deferred_stack_check->SetExit(instr->done_label());
5910 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5911 // Don't record a deoptimization index for the safepoint here.
5912 // This will be done explicitly when emitting call and the safepoint in
5913 // the deferred code.
5918 void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
5919 // This is a pseudo-instruction that ensures that the environment here is
5920 // properly registered for deoptimization and records the assembler's PC
5922 LEnvironment* environment = instr->environment();
5924 // If the environment were already registered, we would have no way of
5925 // backpatching it with the spill slot operands.
5926 DCHECK(!environment->HasBeenRegistered());
5927 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
5929 GenerateOsrPrologue();
5933 void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
5934 Register result = ToRegister(instr->result());
5935 Register object = ToRegister(instr->object());
5936 __ And(at, object, kSmiTagMask);
5937 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
5939 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
5940 __ GetObjectType(object, a1, a1);
5941 DeoptimizeIf(le, instr, Deoptimizer::kNotAJavaScriptObject, a1,
5942 Operand(LAST_JS_PROXY_TYPE));
5944 Label use_cache, call_runtime;
5945 DCHECK(object.is(a0));
5946 Register null_value = t1;
5947 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
5948 __ CheckEnumCache(null_value, &call_runtime);
5950 __ lw(result, FieldMemOperand(object, HeapObject::kMapOffset));
5951 __ Branch(&use_cache);
5953 // Get the set of properties to enumerate.
5954 __ bind(&call_runtime);
5956 CallRuntime(Runtime::kGetPropertyNamesFast, 1, instr);
5958 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
5959 DCHECK(result.is(v0));
5960 __ LoadRoot(at, Heap::kMetaMapRootIndex);
5961 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap, a1, Operand(at));
5962 __ bind(&use_cache);
5966 void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5967 Register map = ToRegister(instr->map());
5968 Register result = ToRegister(instr->result());
5969 Label load_cache, done;
5970 __ EnumLength(result, map);
5971 __ Branch(&load_cache, ne, result, Operand(Smi::FromInt(0)));
5972 __ li(result, Operand(isolate()->factory()->empty_fixed_array()));
5975 __ bind(&load_cache);
5976 __ LoadInstanceDescriptors(map, result);
5978 FieldMemOperand(result, DescriptorArray::kEnumCacheOffset));
5980 FieldMemOperand(result, FixedArray::SizeFor(instr->idx())));
5981 DeoptimizeIf(eq, instr, Deoptimizer::kNoCache, result, Operand(zero_reg));
5987 void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5988 Register object = ToRegister(instr->value());
5989 Register map = ToRegister(instr->map());
5990 __ lw(scratch0(), FieldMemOperand(object, HeapObject::kMapOffset));
5991 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap, map, Operand(scratch0()));
5995 void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5999 PushSafepointRegistersScope scope(this);
6000 __ Push(object, index);
6001 __ mov(cp, zero_reg);
6002 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
6003 RecordSafepointWithRegisters(
6004 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
6005 __ StoreToSafepointRegisterSlot(v0, result);
6009 void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
6010 class DeferredLoadMutableDouble final : public LDeferredCode {
6012 DeferredLoadMutableDouble(LCodeGen* codegen,
6013 LLoadFieldByIndex* instr,
6017 : LDeferredCode(codegen),
6023 void Generate() override {
6024 codegen()->DoDeferredLoadMutableDouble(instr_, result_, object_, index_);
6026 LInstruction* instr() override { return instr_; }
6029 LLoadFieldByIndex* instr_;
6035 Register object = ToRegister(instr->object());
6036 Register index = ToRegister(instr->index());
6037 Register result = ToRegister(instr->result());
6038 Register scratch = scratch0();
6040 DeferredLoadMutableDouble* deferred;
6041 deferred = new(zone()) DeferredLoadMutableDouble(
6042 this, instr, result, object, index);
6044 Label out_of_object, done;
6046 __ And(scratch, index, Operand(Smi::FromInt(1)));
6047 __ Branch(deferred->entry(), ne, scratch, Operand(zero_reg));
6048 __ sra(index, index, 1);
6050 __ Branch(USE_DELAY_SLOT, &out_of_object, lt, index, Operand(zero_reg));
6051 __ sll(scratch, index, kPointerSizeLog2 - kSmiTagSize); // In delay slot.
6053 STATIC_ASSERT(kPointerSizeLog2 > kSmiTagSize);
6054 __ Addu(scratch, object, scratch);
6055 __ lw(result, FieldMemOperand(scratch, JSObject::kHeaderSize));
6059 __ bind(&out_of_object);
6060 __ lw(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
6061 // Index is equal to negated out of object property index plus 1.
6062 __ Subu(scratch, result, scratch);
6063 __ lw(result, FieldMemOperand(scratch,
6064 FixedArray::kHeaderSize - kPointerSize));
6065 __ bind(deferred->exit());
6070 void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
6071 Register context = ToRegister(instr->context());
6072 __ sw(context, MemOperand(fp, StandardFrameConstants::kContextOffset));
6076 void LCodeGen::DoAllocateBlockContext(LAllocateBlockContext* instr) {
6077 Handle<ScopeInfo> scope_info = instr->scope_info();
6078 __ li(at, scope_info);
6079 __ Push(at, ToRegister(instr->function()));
6080 CallRuntime(Runtime::kPushBlockContext, 2, instr);
6081 RecordSafepoint(Safepoint::kNoLazyDeopt);
6087 } // namespace internal