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::DoLoadContextSlot(LLoadContextSlot* instr) {
2899 Register context = ToRegister(instr->context());
2900 Register result = ToRegister(instr->result());
2902 __ lw(result, ContextOperand(context, instr->slot_index()));
2903 if (instr->hydrogen()->RequiresHoleCheck()) {
2904 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2906 if (instr->hydrogen()->DeoptimizesOnHole()) {
2907 DeoptimizeIf(eq, instr, Deoptimizer::kHole, result, Operand(at));
2910 __ Branch(&is_not_hole, ne, result, Operand(at));
2911 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2912 __ bind(&is_not_hole);
2918 void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
2919 Register context = ToRegister(instr->context());
2920 Register value = ToRegister(instr->value());
2921 Register scratch = scratch0();
2922 MemOperand target = ContextOperand(context, instr->slot_index());
2924 Label skip_assignment;
2926 if (instr->hydrogen()->RequiresHoleCheck()) {
2927 __ lw(scratch, target);
2928 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2930 if (instr->hydrogen()->DeoptimizesOnHole()) {
2931 DeoptimizeIf(eq, instr, Deoptimizer::kHole, scratch, Operand(at));
2933 __ Branch(&skip_assignment, ne, scratch, Operand(at));
2937 __ sw(value, target);
2938 if (instr->hydrogen()->NeedsWriteBarrier()) {
2939 SmiCheck check_needed =
2940 instr->hydrogen()->value()->type().IsHeapObject()
2941 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2942 __ RecordWriteContextSlot(context,
2948 EMIT_REMEMBERED_SET,
2952 __ bind(&skip_assignment);
2956 void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
2957 HObjectAccess access = instr->hydrogen()->access();
2958 int offset = access.offset();
2959 Register object = ToRegister(instr->object());
2961 if (access.IsExternalMemory()) {
2962 Register result = ToRegister(instr->result());
2963 MemOperand operand = MemOperand(object, offset);
2964 __ Load(result, operand, access.representation());
2968 if (instr->hydrogen()->representation().IsDouble()) {
2969 DoubleRegister result = ToDoubleRegister(instr->result());
2970 __ ldc1(result, FieldMemOperand(object, offset));
2974 Register result = ToRegister(instr->result());
2975 if (!access.IsInobject()) {
2976 __ lw(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
2979 MemOperand operand = FieldMemOperand(object, offset);
2980 __ Load(result, operand, access.representation());
2984 void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
2985 DCHECK(ToRegister(instr->context()).is(cp));
2986 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
2987 DCHECK(ToRegister(instr->result()).is(v0));
2989 // Name is always in a2.
2990 __ li(LoadDescriptor::NameRegister(), Operand(instr->name()));
2991 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
2993 CodeFactory::LoadICInOptimizedCode(
2994 isolate(), NOT_INSIDE_TYPEOF, instr->hydrogen()->language_mode(),
2995 instr->hydrogen()->initialization_state()).code();
2996 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3000 void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
3001 Register scratch = scratch0();
3002 Register function = ToRegister(instr->function());
3003 Register result = ToRegister(instr->result());
3005 // Get the prototype or initial map from the function.
3007 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
3009 // Check that the function has a prototype or an initial map.
3010 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
3011 DeoptimizeIf(eq, instr, Deoptimizer::kHole, result, Operand(at));
3013 // If the function does not have an initial map, we're done.
3015 __ GetObjectType(result, scratch, scratch);
3016 __ Branch(&done, ne, scratch, Operand(MAP_TYPE));
3018 // Get the prototype from the initial map.
3019 __ lw(result, FieldMemOperand(result, Map::kPrototypeOffset));
3026 void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
3027 Register result = ToRegister(instr->result());
3028 __ LoadRoot(result, instr->index());
3032 void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
3033 Register arguments = ToRegister(instr->arguments());
3034 Register result = ToRegister(instr->result());
3035 // There are two words between the frame pointer and the last argument.
3036 // Subtracting from length accounts for one of them add one more.
3037 if (instr->length()->IsConstantOperand()) {
3038 int const_length = ToInteger32(LConstantOperand::cast(instr->length()));
3039 if (instr->index()->IsConstantOperand()) {
3040 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3041 int index = (const_length - const_index) + 1;
3042 __ lw(result, MemOperand(arguments, index * kPointerSize));
3044 Register index = ToRegister(instr->index());
3045 __ li(at, Operand(const_length + 1));
3046 __ Subu(result, at, index);
3047 __ sll(at, result, kPointerSizeLog2);
3048 __ Addu(at, arguments, at);
3049 __ lw(result, MemOperand(at));
3051 } else if (instr->index()->IsConstantOperand()) {
3052 Register length = ToRegister(instr->length());
3053 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3054 int loc = const_index - 1;
3056 __ Subu(result, length, Operand(loc));
3057 __ sll(at, result, kPointerSizeLog2);
3058 __ Addu(at, arguments, at);
3059 __ lw(result, MemOperand(at));
3061 __ sll(at, length, kPointerSizeLog2);
3062 __ Addu(at, arguments, at);
3063 __ lw(result, MemOperand(at));
3066 Register length = ToRegister(instr->length());
3067 Register index = ToRegister(instr->index());
3068 __ Subu(result, length, index);
3069 __ Addu(result, result, 1);
3070 __ sll(at, result, kPointerSizeLog2);
3071 __ Addu(at, arguments, at);
3072 __ lw(result, MemOperand(at));
3077 void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
3078 Register external_pointer = ToRegister(instr->elements());
3079 Register key = no_reg;
3080 ElementsKind elements_kind = instr->elements_kind();
3081 bool key_is_constant = instr->key()->IsConstantOperand();
3082 int constant_key = 0;
3083 if (key_is_constant) {
3084 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3085 if (constant_key & 0xF0000000) {
3086 Abort(kArrayIndexConstantValueTooBig);
3089 key = ToRegister(instr->key());
3091 int element_size_shift = ElementsKindToShiftSize(elements_kind);
3092 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3093 ? (element_size_shift - kSmiTagSize) : element_size_shift;
3094 int base_offset = instr->base_offset();
3096 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3097 elements_kind == FLOAT32_ELEMENTS ||
3098 elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
3099 elements_kind == FLOAT64_ELEMENTS) {
3100 FPURegister result = ToDoubleRegister(instr->result());
3101 if (key_is_constant) {
3102 __ Addu(scratch0(), external_pointer, constant_key << element_size_shift);
3104 __ sll(scratch0(), key, shift_size);
3105 __ Addu(scratch0(), scratch0(), external_pointer);
3107 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3108 elements_kind == FLOAT32_ELEMENTS) {
3109 __ lwc1(result, MemOperand(scratch0(), base_offset));
3110 __ cvt_d_s(result, result);
3111 } else { // i.e. elements_kind == EXTERNAL_DOUBLE_ELEMENTS
3112 __ ldc1(result, MemOperand(scratch0(), base_offset));
3115 Register result = ToRegister(instr->result());
3116 MemOperand mem_operand = PrepareKeyedOperand(
3117 key, external_pointer, key_is_constant, constant_key,
3118 element_size_shift, shift_size, base_offset);
3119 switch (elements_kind) {
3120 case EXTERNAL_INT8_ELEMENTS:
3122 __ lb(result, mem_operand);
3124 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
3125 case EXTERNAL_UINT8_ELEMENTS:
3126 case UINT8_ELEMENTS:
3127 case UINT8_CLAMPED_ELEMENTS:
3128 __ lbu(result, mem_operand);
3130 case EXTERNAL_INT16_ELEMENTS:
3131 case INT16_ELEMENTS:
3132 __ lh(result, mem_operand);
3134 case EXTERNAL_UINT16_ELEMENTS:
3135 case UINT16_ELEMENTS:
3136 __ lhu(result, mem_operand);
3138 case EXTERNAL_INT32_ELEMENTS:
3139 case INT32_ELEMENTS:
3140 __ lw(result, mem_operand);
3142 case EXTERNAL_UINT32_ELEMENTS:
3143 case UINT32_ELEMENTS:
3144 __ lw(result, mem_operand);
3145 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
3146 DeoptimizeIf(Ugreater_equal, instr, Deoptimizer::kNegativeValue,
3147 result, Operand(0x80000000));
3150 case FLOAT32_ELEMENTS:
3151 case FLOAT64_ELEMENTS:
3152 case EXTERNAL_FLOAT32_ELEMENTS:
3153 case EXTERNAL_FLOAT64_ELEMENTS:
3154 case FAST_DOUBLE_ELEMENTS:
3156 case FAST_SMI_ELEMENTS:
3157 case FAST_HOLEY_DOUBLE_ELEMENTS:
3158 case FAST_HOLEY_ELEMENTS:
3159 case FAST_HOLEY_SMI_ELEMENTS:
3160 case DICTIONARY_ELEMENTS:
3161 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
3162 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
3170 void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
3171 Register elements = ToRegister(instr->elements());
3172 bool key_is_constant = instr->key()->IsConstantOperand();
3173 Register key = no_reg;
3174 DoubleRegister result = ToDoubleRegister(instr->result());
3175 Register scratch = scratch0();
3177 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
3179 int base_offset = instr->base_offset();
3180 if (key_is_constant) {
3181 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3182 if (constant_key & 0xF0000000) {
3183 Abort(kArrayIndexConstantValueTooBig);
3185 base_offset += constant_key * kDoubleSize;
3187 __ Addu(scratch, elements, Operand(base_offset));
3189 if (!key_is_constant) {
3190 key = ToRegister(instr->key());
3191 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3192 ? (element_size_shift - kSmiTagSize) : element_size_shift;
3193 __ sll(at, key, shift_size);
3194 __ Addu(scratch, scratch, at);
3197 __ ldc1(result, MemOperand(scratch));
3199 if (instr->hydrogen()->RequiresHoleCheck()) {
3200 __ lw(scratch, MemOperand(scratch, kHoleNanUpper32Offset));
3201 DeoptimizeIf(eq, instr, Deoptimizer::kHole, scratch,
3202 Operand(kHoleNanUpper32));
3207 void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
3208 Register elements = ToRegister(instr->elements());
3209 Register result = ToRegister(instr->result());
3210 Register scratch = scratch0();
3211 Register store_base = scratch;
3212 int offset = instr->base_offset();
3214 if (instr->key()->IsConstantOperand()) {
3215 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
3216 offset += ToInteger32(const_operand) * kPointerSize;
3217 store_base = elements;
3219 Register key = ToRegister(instr->key());
3220 // Even though the HLoadKeyed instruction forces the input
3221 // representation for the key to be an integer, the input gets replaced
3222 // during bound check elimination with the index argument to the bounds
3223 // check, which can be tagged, so that case must be handled here, too.
3224 if (instr->hydrogen()->key()->representation().IsSmi()) {
3225 __ sll(scratch, key, kPointerSizeLog2 - kSmiTagSize);
3226 __ addu(scratch, elements, scratch);
3228 __ sll(scratch, key, kPointerSizeLog2);
3229 __ addu(scratch, elements, scratch);
3232 __ lw(result, MemOperand(store_base, offset));
3234 // Check for the hole value.
3235 if (instr->hydrogen()->RequiresHoleCheck()) {
3236 if (IsFastSmiElementsKind(instr->hydrogen()->elements_kind())) {
3237 __ SmiTst(result, scratch);
3238 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi, scratch,
3241 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
3242 DeoptimizeIf(eq, instr, Deoptimizer::kHole, result, Operand(scratch));
3244 } else if (instr->hydrogen()->hole_mode() == CONVERT_HOLE_TO_UNDEFINED) {
3245 DCHECK(instr->hydrogen()->elements_kind() == FAST_HOLEY_ELEMENTS);
3247 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
3248 __ Branch(&done, ne, result, Operand(scratch));
3249 if (info()->IsStub()) {
3250 // A stub can safely convert the hole to undefined only if the array
3251 // protector cell contains (Smi) Isolate::kArrayProtectorValid. Otherwise
3252 // it needs to bail out.
3253 __ LoadRoot(result, Heap::kArrayProtectorRootIndex);
3254 __ lw(result, FieldMemOperand(result, Cell::kValueOffset));
3255 DeoptimizeIf(ne, instr, Deoptimizer::kHole, result,
3256 Operand(Smi::FromInt(Isolate::kArrayProtectorValid)));
3258 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3264 void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
3265 if (instr->is_typed_elements()) {
3266 DoLoadKeyedExternalArray(instr);
3267 } else if (instr->hydrogen()->representation().IsDouble()) {
3268 DoLoadKeyedFixedDoubleArray(instr);
3270 DoLoadKeyedFixedArray(instr);
3275 MemOperand LCodeGen::PrepareKeyedOperand(Register key,
3277 bool key_is_constant,
3282 if (key_is_constant) {
3283 return MemOperand(base, (constant_key << element_size) + base_offset);
3286 if (base_offset == 0) {
3287 if (shift_size >= 0) {
3288 __ sll(scratch0(), key, shift_size);
3289 __ Addu(scratch0(), base, scratch0());
3290 return MemOperand(scratch0());
3292 DCHECK_EQ(-1, shift_size);
3293 __ srl(scratch0(), key, 1);
3294 __ Addu(scratch0(), base, scratch0());
3295 return MemOperand(scratch0());
3299 if (shift_size >= 0) {
3300 __ sll(scratch0(), key, shift_size);
3301 __ Addu(scratch0(), base, scratch0());
3302 return MemOperand(scratch0(), base_offset);
3304 DCHECK_EQ(-1, shift_size);
3305 __ sra(scratch0(), key, 1);
3306 __ Addu(scratch0(), base, scratch0());
3307 return MemOperand(scratch0(), base_offset);
3312 void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3313 DCHECK(ToRegister(instr->context()).is(cp));
3314 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3315 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
3317 if (instr->hydrogen()->HasVectorAndSlot()) {
3318 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
3321 Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(
3322 isolate(), instr->hydrogen()->language_mode(),
3323 instr->hydrogen()->initialization_state()).code();
3324 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3328 void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
3329 Register scratch = scratch0();
3330 Register temp = scratch1();
3331 Register result = ToRegister(instr->result());
3333 if (instr->hydrogen()->from_inlined()) {
3334 __ Subu(result, sp, 2 * kPointerSize);
3336 // Check if the calling frame is an arguments adaptor frame.
3337 Label done, adapted;
3338 __ lw(scratch, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3339 __ lw(result, MemOperand(scratch, StandardFrameConstants::kContextOffset));
3340 __ Xor(temp, result, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3342 // Result is the frame pointer for the frame if not adapted and for the real
3343 // frame below the adaptor frame if adapted.
3344 __ Movn(result, fp, temp); // Move only if temp is not equal to zero (ne).
3345 __ Movz(result, scratch, temp); // Move only if temp is equal to zero (eq).
3350 void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
3351 Register elem = ToRegister(instr->elements());
3352 Register result = ToRegister(instr->result());
3356 // If no arguments adaptor frame the number of arguments is fixed.
3357 __ Addu(result, zero_reg, Operand(scope()->num_parameters()));
3358 __ Branch(&done, eq, fp, Operand(elem));
3360 // Arguments adaptor frame present. Get argument length from there.
3361 __ lw(result, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3363 MemOperand(result, ArgumentsAdaptorFrameConstants::kLengthOffset));
3364 __ SmiUntag(result);
3366 // Argument length is in result register.
3371 void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
3372 Register receiver = ToRegister(instr->receiver());
3373 Register function = ToRegister(instr->function());
3374 Register result = ToRegister(instr->result());
3375 Register scratch = scratch0();
3377 // If the receiver is null or undefined, we have to pass the global
3378 // object as a receiver to normal functions. Values have to be
3379 // passed unchanged to builtins and strict-mode functions.
3380 Label global_object, result_in_receiver;
3382 if (!instr->hydrogen()->known_function()) {
3383 // Do not transform the receiver to object for strict mode
3386 FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
3388 FieldMemOperand(scratch, SharedFunctionInfo::kCompilerHintsOffset));
3390 // Do not transform the receiver to object for builtins.
3391 int32_t strict_mode_function_mask =
3392 1 << (SharedFunctionInfo::kStrictModeFunction + kSmiTagSize);
3393 int32_t native_mask = 1 << (SharedFunctionInfo::kNative + kSmiTagSize);
3394 __ And(scratch, scratch, Operand(strict_mode_function_mask | native_mask));
3395 __ Branch(&result_in_receiver, ne, scratch, Operand(zero_reg));
3398 // Normal function. Replace undefined or null with global receiver.
3399 __ LoadRoot(scratch, Heap::kNullValueRootIndex);
3400 __ Branch(&global_object, eq, receiver, Operand(scratch));
3401 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
3402 __ Branch(&global_object, eq, receiver, Operand(scratch));
3404 // Deoptimize if the receiver is not a JS object.
3405 __ SmiTst(receiver, scratch);
3406 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, scratch, Operand(zero_reg));
3408 __ GetObjectType(receiver, scratch, scratch);
3409 DeoptimizeIf(lt, instr, Deoptimizer::kNotAJavaScriptObject, scratch,
3410 Operand(FIRST_SPEC_OBJECT_TYPE));
3412 __ Branch(&result_in_receiver);
3413 __ bind(&global_object);
3414 __ lw(result, FieldMemOperand(function, JSFunction::kContextOffset));
3416 ContextOperand(result, Context::GLOBAL_OBJECT_INDEX));
3418 FieldMemOperand(result, GlobalObject::kGlobalProxyOffset));
3420 if (result.is(receiver)) {
3421 __ bind(&result_in_receiver);
3424 __ Branch(&result_ok);
3425 __ bind(&result_in_receiver);
3426 __ mov(result, receiver);
3427 __ bind(&result_ok);
3432 void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3433 Register receiver = ToRegister(instr->receiver());
3434 Register function = ToRegister(instr->function());
3435 Register length = ToRegister(instr->length());
3436 Register elements = ToRegister(instr->elements());
3437 Register scratch = scratch0();
3438 DCHECK(receiver.is(a0)); // Used for parameter count.
3439 DCHECK(function.is(a1)); // Required by InvokeFunction.
3440 DCHECK(ToRegister(instr->result()).is(v0));
3442 // Copy the arguments to this function possibly from the
3443 // adaptor frame below it.
3444 const uint32_t kArgumentsLimit = 1 * KB;
3445 DeoptimizeIf(hi, instr, Deoptimizer::kTooManyArguments, length,
3446 Operand(kArgumentsLimit));
3448 // Push the receiver and use the register to keep the original
3449 // number of arguments.
3451 __ Move(receiver, length);
3452 // The arguments are at a one pointer size offset from elements.
3453 __ Addu(elements, elements, Operand(1 * kPointerSize));
3455 // Loop through the arguments pushing them onto the execution
3458 // length is a small non-negative integer, due to the test above.
3459 __ Branch(USE_DELAY_SLOT, &invoke, eq, length, Operand(zero_reg));
3460 __ sll(scratch, length, 2);
3462 __ Addu(scratch, elements, scratch);
3463 __ lw(scratch, MemOperand(scratch));
3465 __ Subu(length, length, Operand(1));
3466 __ Branch(USE_DELAY_SLOT, &loop, ne, length, Operand(zero_reg));
3467 __ sll(scratch, length, 2);
3470 DCHECK(instr->HasPointerMap());
3471 LPointerMap* pointers = instr->pointer_map();
3472 SafepointGenerator safepoint_generator(
3473 this, pointers, Safepoint::kLazyDeopt);
3474 // The number of arguments is stored in receiver which is a0, as expected
3475 // by InvokeFunction.
3476 ParameterCount actual(receiver);
3477 __ InvokeFunction(function, actual, CALL_FUNCTION, safepoint_generator);
3481 void LCodeGen::DoPushArgument(LPushArgument* instr) {
3482 LOperand* argument = instr->value();
3483 if (argument->IsDoubleRegister() || argument->IsDoubleStackSlot()) {
3484 Abort(kDoPushArgumentNotImplementedForDoubleType);
3486 Register argument_reg = EmitLoadRegister(argument, at);
3487 __ push(argument_reg);
3492 void LCodeGen::DoDrop(LDrop* instr) {
3493 __ Drop(instr->count());
3497 void LCodeGen::DoThisFunction(LThisFunction* instr) {
3498 Register result = ToRegister(instr->result());
3499 __ lw(result, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3503 void LCodeGen::DoContext(LContext* instr) {
3504 // If there is a non-return use, the context must be moved to a register.
3505 Register result = ToRegister(instr->result());
3506 if (info()->IsOptimizing()) {
3507 __ lw(result, MemOperand(fp, StandardFrameConstants::kContextOffset));
3509 // If there is no frame, the context must be in cp.
3510 DCHECK(result.is(cp));
3515 void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
3516 DCHECK(ToRegister(instr->context()).is(cp));
3517 __ li(scratch0(), instr->hydrogen()->pairs());
3518 __ li(scratch1(), Operand(Smi::FromInt(instr->hydrogen()->flags())));
3519 // The context is the first argument.
3520 __ Push(cp, scratch0(), scratch1());
3521 CallRuntime(Runtime::kDeclareGlobals, 3, instr);
3525 void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
3526 int formal_parameter_count, int arity,
3527 LInstruction* instr) {
3528 bool dont_adapt_arguments =
3529 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3530 bool can_invoke_directly =
3531 dont_adapt_arguments || formal_parameter_count == arity;
3533 Register function_reg = a1;
3534 LPointerMap* pointers = instr->pointer_map();
3536 if (can_invoke_directly) {
3538 __ lw(cp, FieldMemOperand(function_reg, JSFunction::kContextOffset));
3540 // Set r0 to arguments count if adaption is not needed. Assumes that r0
3541 // is available to write to at this point.
3542 if (dont_adapt_arguments) {
3543 __ li(a0, Operand(arity));
3547 __ lw(at, FieldMemOperand(function_reg, JSFunction::kCodeEntryOffset));
3550 // Set up deoptimization.
3551 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3553 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3554 ParameterCount count(arity);
3555 ParameterCount expected(formal_parameter_count);
3556 __ InvokeFunction(function_reg, expected, count, CALL_FUNCTION, generator);
3561 void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3562 DCHECK(instr->context() != NULL);
3563 DCHECK(ToRegister(instr->context()).is(cp));
3564 Register input = ToRegister(instr->value());
3565 Register result = ToRegister(instr->result());
3566 Register scratch = scratch0();
3568 // Deoptimize if not a heap number.
3569 __ lw(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
3570 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
3571 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, scratch, Operand(at));
3574 Register exponent = scratch0();
3576 __ lw(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3577 // Check the sign of the argument. If the argument is positive, just
3579 __ Move(result, input);
3580 __ And(at, exponent, Operand(HeapNumber::kSignMask));
3581 __ Branch(&done, eq, at, Operand(zero_reg));
3583 // Input is negative. Reverse its sign.
3584 // Preserve the value of all registers.
3586 PushSafepointRegistersScope scope(this);
3588 // Registers were saved at the safepoint, so we can use
3589 // many scratch registers.
3590 Register tmp1 = input.is(a1) ? a0 : a1;
3591 Register tmp2 = input.is(a2) ? a0 : a2;
3592 Register tmp3 = input.is(a3) ? a0 : a3;
3593 Register tmp4 = input.is(t0) ? a0 : t0;
3595 // exponent: floating point exponent value.
3597 Label allocated, slow;
3598 __ LoadRoot(tmp4, Heap::kHeapNumberMapRootIndex);
3599 __ AllocateHeapNumber(tmp1, tmp2, tmp3, tmp4, &slow);
3600 __ Branch(&allocated);
3602 // Slow case: Call the runtime system to do the number allocation.
3605 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr,
3607 // Set the pointer to the new heap number in tmp.
3610 // Restore input_reg after call to runtime.
3611 __ LoadFromSafepointRegisterSlot(input, input);
3612 __ lw(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3614 __ bind(&allocated);
3615 // exponent: floating point exponent value.
3616 // tmp1: allocated heap number.
3617 __ And(exponent, exponent, Operand(~HeapNumber::kSignMask));
3618 __ sw(exponent, FieldMemOperand(tmp1, HeapNumber::kExponentOffset));
3619 __ lw(tmp2, FieldMemOperand(input, HeapNumber::kMantissaOffset));
3620 __ sw(tmp2, FieldMemOperand(tmp1, HeapNumber::kMantissaOffset));
3622 __ StoreToSafepointRegisterSlot(tmp1, result);
3629 void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3630 Register input = ToRegister(instr->value());
3631 Register result = ToRegister(instr->result());
3632 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
3634 __ Branch(USE_DELAY_SLOT, &done, ge, input, Operand(zero_reg));
3635 __ mov(result, input);
3636 __ subu(result, zero_reg, input);
3637 // Overflow if result is still negative, i.e. 0x80000000.
3638 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, result, Operand(zero_reg));
3643 void LCodeGen::DoMathAbs(LMathAbs* instr) {
3644 // Class for deferred case.
3645 class DeferredMathAbsTaggedHeapNumber final : public LDeferredCode {
3647 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr)
3648 : LDeferredCode(codegen), instr_(instr) { }
3649 void Generate() override {
3650 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3652 LInstruction* instr() override { return instr_; }
3658 Representation r = instr->hydrogen()->value()->representation();
3660 FPURegister input = ToDoubleRegister(instr->value());
3661 FPURegister result = ToDoubleRegister(instr->result());
3662 __ abs_d(result, input);
3663 } else if (r.IsSmiOrInteger32()) {
3664 EmitIntegerMathAbs(instr);
3666 // Representation is tagged.
3667 DeferredMathAbsTaggedHeapNumber* deferred =
3668 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3669 Register input = ToRegister(instr->value());
3671 __ JumpIfNotSmi(input, deferred->entry());
3672 // If smi, handle it directly.
3673 EmitIntegerMathAbs(instr);
3674 __ bind(deferred->exit());
3679 void LCodeGen::DoMathFloor(LMathFloor* instr) {
3680 DoubleRegister input = ToDoubleRegister(instr->value());
3681 Register result = ToRegister(instr->result());
3682 Register scratch1 = scratch0();
3683 Register except_flag = ToRegister(instr->temp());
3685 __ EmitFPUTruncate(kRoundToMinusInf,
3692 // Deopt if the operation did not succeed.
3693 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
3696 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3699 __ Branch(&done, ne, result, Operand(zero_reg));
3700 __ Mfhc1(scratch1, input);
3701 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
3702 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
3709 void LCodeGen::DoMathRound(LMathRound* instr) {
3710 DoubleRegister input = ToDoubleRegister(instr->value());
3711 Register result = ToRegister(instr->result());
3712 DoubleRegister double_scratch1 = ToDoubleRegister(instr->temp());
3713 Register scratch = scratch0();
3714 Label done, check_sign_on_zero;
3716 // Extract exponent bits.
3717 __ Mfhc1(result, input);
3720 HeapNumber::kExponentShift,
3721 HeapNumber::kExponentBits);
3723 // If the number is in ]-0.5, +0.5[, the result is +/- 0.
3725 __ Branch(&skip1, gt, scratch, Operand(HeapNumber::kExponentBias - 2));
3726 __ mov(result, zero_reg);
3727 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3728 __ Branch(&check_sign_on_zero);
3734 // The following conversion will not work with numbers
3735 // outside of ]-2^32, 2^32[.
3736 DeoptimizeIf(ge, instr, Deoptimizer::kOverflow, scratch,
3737 Operand(HeapNumber::kExponentBias + 32));
3739 // Save the original sign for later comparison.
3740 __ And(scratch, result, Operand(HeapNumber::kSignMask));
3742 __ Move(double_scratch0(), 0.5);
3743 __ add_d(double_scratch0(), input, double_scratch0());
3745 // Check sign of the result: if the sign changed, the input
3746 // value was in ]0.5, 0[ and the result should be -0.
3747 __ Mfhc1(result, double_scratch0());
3748 __ Xor(result, result, Operand(scratch));
3749 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3750 // ARM uses 'mi' here, which is 'lt'
3751 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, result, Operand(zero_reg));
3754 // ARM uses 'mi' here, which is 'lt'
3755 // Negating it results in 'ge'
3756 __ Branch(&skip2, ge, result, Operand(zero_reg));
3757 __ mov(result, zero_reg);
3762 Register except_flag = scratch;
3763 __ EmitFPUTruncate(kRoundToMinusInf,
3770 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
3773 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3775 __ Branch(&done, ne, result, Operand(zero_reg));
3776 __ bind(&check_sign_on_zero);
3777 __ Mfhc1(scratch, input);
3778 __ And(scratch, scratch, Operand(HeapNumber::kSignMask));
3779 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch,
3786 void LCodeGen::DoMathFround(LMathFround* instr) {
3787 DoubleRegister input = ToDoubleRegister(instr->value());
3788 DoubleRegister result = ToDoubleRegister(instr->result());
3789 __ cvt_s_d(result.low(), input);
3790 __ cvt_d_s(result, result.low());
3794 void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3795 DoubleRegister input = ToDoubleRegister(instr->value());
3796 DoubleRegister result = ToDoubleRegister(instr->result());
3797 __ sqrt_d(result, input);
3801 void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3802 DoubleRegister input = ToDoubleRegister(instr->value());
3803 DoubleRegister result = ToDoubleRegister(instr->result());
3804 DoubleRegister temp = ToDoubleRegister(instr->temp());
3806 DCHECK(!input.is(result));
3808 // Note that according to ECMA-262 15.8.2.13:
3809 // Math.pow(-Infinity, 0.5) == Infinity
3810 // Math.sqrt(-Infinity) == NaN
3812 __ Move(temp, static_cast<double>(-V8_INFINITY));
3813 __ BranchF(USE_DELAY_SLOT, &done, NULL, eq, temp, input);
3814 // Set up Infinity in the delay slot.
3815 // result is overwritten if the branch is not taken.
3816 __ neg_d(result, temp);
3818 // Add +0 to convert -0 to +0.
3819 __ add_d(result, input, kDoubleRegZero);
3820 __ sqrt_d(result, result);
3825 void LCodeGen::DoPower(LPower* instr) {
3826 Representation exponent_type = instr->hydrogen()->right()->representation();
3827 // Having marked this as a call, we can use any registers.
3828 // Just make sure that the input/output registers are the expected ones.
3829 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3830 DCHECK(!instr->right()->IsDoubleRegister() ||
3831 ToDoubleRegister(instr->right()).is(f4));
3832 DCHECK(!instr->right()->IsRegister() ||
3833 ToRegister(instr->right()).is(tagged_exponent));
3834 DCHECK(ToDoubleRegister(instr->left()).is(f2));
3835 DCHECK(ToDoubleRegister(instr->result()).is(f0));
3837 if (exponent_type.IsSmi()) {
3838 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3840 } else if (exponent_type.IsTagged()) {
3842 __ JumpIfSmi(tagged_exponent, &no_deopt);
3843 DCHECK(!t3.is(tagged_exponent));
3844 __ lw(t3, FieldMemOperand(tagged_exponent, HeapObject::kMapOffset));
3845 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
3846 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, t3, Operand(at));
3848 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3850 } else if (exponent_type.IsInteger32()) {
3851 MathPowStub stub(isolate(), MathPowStub::INTEGER);
3854 DCHECK(exponent_type.IsDouble());
3855 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
3861 void LCodeGen::DoMathExp(LMathExp* instr) {
3862 DoubleRegister input = ToDoubleRegister(instr->value());
3863 DoubleRegister result = ToDoubleRegister(instr->result());
3864 DoubleRegister double_scratch1 = ToDoubleRegister(instr->double_temp());
3865 DoubleRegister double_scratch2 = double_scratch0();
3866 Register temp1 = ToRegister(instr->temp1());
3867 Register temp2 = ToRegister(instr->temp2());
3869 MathExpGenerator::EmitMathExp(
3870 masm(), input, result, double_scratch1, double_scratch2,
3871 temp1, temp2, scratch0());
3875 void LCodeGen::DoMathLog(LMathLog* instr) {
3876 __ PrepareCallCFunction(0, 1, scratch0());
3877 __ MovToFloatParameter(ToDoubleRegister(instr->value()));
3878 __ CallCFunction(ExternalReference::math_log_double_function(isolate()),
3880 __ MovFromFloatResult(ToDoubleRegister(instr->result()));
3884 void LCodeGen::DoMathClz32(LMathClz32* instr) {
3885 Register input = ToRegister(instr->value());
3886 Register result = ToRegister(instr->result());
3887 __ Clz(result, input);
3891 void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
3892 DCHECK(ToRegister(instr->context()).is(cp));
3893 DCHECK(ToRegister(instr->function()).is(a1));
3894 DCHECK(instr->HasPointerMap());
3896 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
3897 if (known_function.is_null()) {
3898 LPointerMap* pointers = instr->pointer_map();
3899 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3900 ParameterCount count(instr->arity());
3901 __ InvokeFunction(a1, count, CALL_FUNCTION, generator);
3903 CallKnownFunction(known_function,
3904 instr->hydrogen()->formal_parameter_count(),
3905 instr->arity(), instr);
3910 void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
3911 DCHECK(ToRegister(instr->result()).is(v0));
3913 if (instr->hydrogen()->IsTailCall()) {
3914 if (NeedsEagerFrame()) __ LeaveFrame(StackFrame::INTERNAL);
3916 if (instr->target()->IsConstantOperand()) {
3917 LConstantOperand* target = LConstantOperand::cast(instr->target());
3918 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3919 __ Jump(code, RelocInfo::CODE_TARGET);
3921 DCHECK(instr->target()->IsRegister());
3922 Register target = ToRegister(instr->target());
3923 __ Addu(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
3927 LPointerMap* pointers = instr->pointer_map();
3928 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3930 if (instr->target()->IsConstantOperand()) {
3931 LConstantOperand* target = LConstantOperand::cast(instr->target());
3932 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3933 generator.BeforeCall(__ CallSize(code, RelocInfo::CODE_TARGET));
3934 __ Call(code, RelocInfo::CODE_TARGET);
3936 DCHECK(instr->target()->IsRegister());
3937 Register target = ToRegister(instr->target());
3938 generator.BeforeCall(__ CallSize(target));
3939 __ Addu(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
3942 generator.AfterCall();
3947 void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
3948 DCHECK(ToRegister(instr->function()).is(a1));
3949 DCHECK(ToRegister(instr->result()).is(v0));
3951 if (instr->hydrogen()->pass_argument_count()) {
3952 __ li(a0, Operand(instr->arity()));
3956 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
3958 // Load the code entry address
3959 __ lw(at, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
3962 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3966 void LCodeGen::DoCallFunction(LCallFunction* instr) {
3967 DCHECK(ToRegister(instr->context()).is(cp));
3968 DCHECK(ToRegister(instr->function()).is(a1));
3969 DCHECK(ToRegister(instr->result()).is(v0));
3971 int arity = instr->arity();
3972 CallFunctionFlags flags = instr->hydrogen()->function_flags();
3973 if (instr->hydrogen()->HasVectorAndSlot()) {
3974 Register slot_register = ToRegister(instr->temp_slot());
3975 Register vector_register = ToRegister(instr->temp_vector());
3976 DCHECK(slot_register.is(a3));
3977 DCHECK(vector_register.is(a2));
3979 AllowDeferredHandleDereference vector_structure_check;
3980 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
3981 int index = vector->GetIndex(instr->hydrogen()->slot());
3983 __ li(vector_register, vector);
3984 __ li(slot_register, Operand(Smi::FromInt(index)));
3986 CallICState::CallType call_type =
3987 (flags & CALL_AS_METHOD) ? CallICState::METHOD : CallICState::FUNCTION;
3990 CodeFactory::CallICInOptimizedCode(isolate(), arity, call_type).code();
3991 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3993 CallFunctionStub stub(isolate(), arity, flags);
3994 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3999 void LCodeGen::DoCallNew(LCallNew* instr) {
4000 DCHECK(ToRegister(instr->context()).is(cp));
4001 DCHECK(ToRegister(instr->constructor()).is(a1));
4002 DCHECK(ToRegister(instr->result()).is(v0));
4004 __ li(a0, Operand(instr->arity()));
4005 // No cell in a2 for construct type feedback in optimized code
4006 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4007 CallConstructStub stub(isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
4008 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4012 void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
4013 DCHECK(ToRegister(instr->context()).is(cp));
4014 DCHECK(ToRegister(instr->constructor()).is(a1));
4015 DCHECK(ToRegister(instr->result()).is(v0));
4017 __ li(a0, Operand(instr->arity()));
4018 if (instr->arity() == 1) {
4019 // We only need the allocation site for the case we have a length argument.
4020 // The case may bail out to the runtime, which will determine the correct
4021 // elements kind with the site.
4022 __ li(a2, instr->hydrogen()->site());
4024 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4026 ElementsKind kind = instr->hydrogen()->elements_kind();
4027 AllocationSiteOverrideMode override_mode =
4028 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
4029 ? DISABLE_ALLOCATION_SITES
4032 if (instr->arity() == 0) {
4033 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
4034 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4035 } else if (instr->arity() == 1) {
4037 if (IsFastPackedElementsKind(kind)) {
4039 // We might need a change here,
4040 // look at the first argument.
4041 __ lw(t1, MemOperand(sp, 0));
4042 __ Branch(&packed_case, eq, t1, Operand(zero_reg));
4044 ElementsKind holey_kind = GetHoleyElementsKind(kind);
4045 ArraySingleArgumentConstructorStub stub(isolate(),
4048 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4050 __ bind(&packed_case);
4053 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
4054 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4057 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
4058 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4063 void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
4064 CallRuntime(instr->function(), instr->arity(), instr);
4068 void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
4069 Register function = ToRegister(instr->function());
4070 Register code_object = ToRegister(instr->code_object());
4071 __ Addu(code_object, code_object,
4072 Operand(Code::kHeaderSize - kHeapObjectTag));
4074 FieldMemOperand(function, JSFunction::kCodeEntryOffset));
4078 void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
4079 Register result = ToRegister(instr->result());
4080 Register base = ToRegister(instr->base_object());
4081 if (instr->offset()->IsConstantOperand()) {
4082 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
4083 __ Addu(result, base, Operand(ToInteger32(offset)));
4085 Register offset = ToRegister(instr->offset());
4086 __ Addu(result, base, offset);
4091 void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
4092 Representation representation = instr->representation();
4094 Register object = ToRegister(instr->object());
4095 Register scratch = scratch0();
4096 HObjectAccess access = instr->hydrogen()->access();
4097 int offset = access.offset();
4099 if (access.IsExternalMemory()) {
4100 Register value = ToRegister(instr->value());
4101 MemOperand operand = MemOperand(object, offset);
4102 __ Store(value, operand, representation);
4106 __ AssertNotSmi(object);
4108 DCHECK(!representation.IsSmi() ||
4109 !instr->value()->IsConstantOperand() ||
4110 IsSmi(LConstantOperand::cast(instr->value())));
4111 if (representation.IsDouble()) {
4112 DCHECK(access.IsInobject());
4113 DCHECK(!instr->hydrogen()->has_transition());
4114 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4115 DoubleRegister value = ToDoubleRegister(instr->value());
4116 __ sdc1(value, FieldMemOperand(object, offset));
4120 if (instr->hydrogen()->has_transition()) {
4121 Handle<Map> transition = instr->hydrogen()->transition_map();
4122 AddDeprecationDependency(transition);
4123 __ li(scratch, Operand(transition));
4124 __ sw(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
4125 if (instr->hydrogen()->NeedsWriteBarrierForMap()) {
4126 Register temp = ToRegister(instr->temp());
4127 // Update the write barrier for the map field.
4128 __ RecordWriteForMap(object,
4137 Register value = ToRegister(instr->value());
4138 if (access.IsInobject()) {
4139 MemOperand operand = FieldMemOperand(object, offset);
4140 __ Store(value, operand, representation);
4141 if (instr->hydrogen()->NeedsWriteBarrier()) {
4142 // Update the write barrier for the object for in-object properties.
4143 __ RecordWriteField(object,
4149 EMIT_REMEMBERED_SET,
4150 instr->hydrogen()->SmiCheckForWriteBarrier(),
4151 instr->hydrogen()->PointersToHereCheckForValue());
4154 __ lw(scratch, FieldMemOperand(object, JSObject::kPropertiesOffset));
4155 MemOperand operand = FieldMemOperand(scratch, offset);
4156 __ Store(value, operand, representation);
4157 if (instr->hydrogen()->NeedsWriteBarrier()) {
4158 // Update the write barrier for the properties array.
4159 // object is used as a scratch register.
4160 __ RecordWriteField(scratch,
4166 EMIT_REMEMBERED_SET,
4167 instr->hydrogen()->SmiCheckForWriteBarrier(),
4168 instr->hydrogen()->PointersToHereCheckForValue());
4174 void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
4175 DCHECK(ToRegister(instr->context()).is(cp));
4176 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4177 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4179 if (instr->hydrogen()->HasVectorAndSlot()) {
4180 EmitVectorStoreICRegisters<LStoreNamedGeneric>(instr);
4183 __ li(StoreDescriptor::NameRegister(), Operand(instr->name()));
4184 Handle<Code> ic = CodeFactory::StoreICInOptimizedCode(
4185 isolate(), instr->language_mode(),
4186 instr->hydrogen()->initialization_state()).code();
4187 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4191 void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
4192 Condition cc = instr->hydrogen()->allow_equality() ? hi : hs;
4195 if (instr->index()->IsConstantOperand()) {
4196 operand = ToOperand(instr->index());
4197 reg = ToRegister(instr->length());
4198 cc = CommuteCondition(cc);
4200 reg = ToRegister(instr->index());
4201 operand = ToOperand(instr->length());
4203 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
4205 __ Branch(&done, NegateCondition(cc), reg, operand);
4206 __ stop("eliminated bounds check failed");
4209 DeoptimizeIf(cc, instr, Deoptimizer::kOutOfBounds, reg, operand);
4214 void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
4215 Register external_pointer = ToRegister(instr->elements());
4216 Register key = no_reg;
4217 ElementsKind elements_kind = instr->elements_kind();
4218 bool key_is_constant = instr->key()->IsConstantOperand();
4219 int constant_key = 0;
4220 if (key_is_constant) {
4221 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4222 if (constant_key & 0xF0000000) {
4223 Abort(kArrayIndexConstantValueTooBig);
4226 key = ToRegister(instr->key());
4228 int element_size_shift = ElementsKindToShiftSize(elements_kind);
4229 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4230 ? (element_size_shift - kSmiTagSize) : element_size_shift;
4231 int base_offset = instr->base_offset();
4233 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4234 elements_kind == FLOAT32_ELEMENTS ||
4235 elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
4236 elements_kind == FLOAT64_ELEMENTS) {
4237 Register address = scratch0();
4238 FPURegister value(ToDoubleRegister(instr->value()));
4239 if (key_is_constant) {
4240 if (constant_key != 0) {
4241 __ Addu(address, external_pointer,
4242 Operand(constant_key << element_size_shift));
4244 address = external_pointer;
4247 __ sll(address, key, shift_size);
4248 __ Addu(address, external_pointer, address);
4251 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4252 elements_kind == FLOAT32_ELEMENTS) {
4253 __ cvt_s_d(double_scratch0(), value);
4254 __ swc1(double_scratch0(), MemOperand(address, base_offset));
4255 } else { // Storing doubles, not floats.
4256 __ sdc1(value, MemOperand(address, base_offset));
4259 Register value(ToRegister(instr->value()));
4260 MemOperand mem_operand = PrepareKeyedOperand(
4261 key, external_pointer, key_is_constant, constant_key,
4262 element_size_shift, shift_size,
4264 switch (elements_kind) {
4265 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
4266 case EXTERNAL_INT8_ELEMENTS:
4267 case EXTERNAL_UINT8_ELEMENTS:
4268 case UINT8_ELEMENTS:
4269 case UINT8_CLAMPED_ELEMENTS:
4271 __ sb(value, mem_operand);
4273 case EXTERNAL_INT16_ELEMENTS:
4274 case EXTERNAL_UINT16_ELEMENTS:
4275 case INT16_ELEMENTS:
4276 case UINT16_ELEMENTS:
4277 __ sh(value, mem_operand);
4279 case EXTERNAL_INT32_ELEMENTS:
4280 case EXTERNAL_UINT32_ELEMENTS:
4281 case INT32_ELEMENTS:
4282 case UINT32_ELEMENTS:
4283 __ sw(value, mem_operand);
4285 case FLOAT32_ELEMENTS:
4286 case FLOAT64_ELEMENTS:
4287 case EXTERNAL_FLOAT32_ELEMENTS:
4288 case EXTERNAL_FLOAT64_ELEMENTS:
4289 case FAST_DOUBLE_ELEMENTS:
4291 case FAST_SMI_ELEMENTS:
4292 case FAST_HOLEY_DOUBLE_ELEMENTS:
4293 case FAST_HOLEY_ELEMENTS:
4294 case FAST_HOLEY_SMI_ELEMENTS:
4295 case DICTIONARY_ELEMENTS:
4296 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
4297 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
4305 void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
4306 DoubleRegister value = ToDoubleRegister(instr->value());
4307 Register elements = ToRegister(instr->elements());
4308 Register scratch = scratch0();
4309 DoubleRegister double_scratch = double_scratch0();
4310 bool key_is_constant = instr->key()->IsConstantOperand();
4311 int base_offset = instr->base_offset();
4312 Label not_nan, done;
4314 // Calculate the effective address of the slot in the array to store the
4316 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
4317 if (key_is_constant) {
4318 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4319 if (constant_key & 0xF0000000) {
4320 Abort(kArrayIndexConstantValueTooBig);
4322 __ Addu(scratch, elements,
4323 Operand((constant_key << element_size_shift) + base_offset));
4325 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4326 ? (element_size_shift - kSmiTagSize) : element_size_shift;
4327 __ Addu(scratch, elements, Operand(base_offset));
4328 __ sll(at, ToRegister(instr->key()), shift_size);
4329 __ Addu(scratch, scratch, at);
4332 if (instr->NeedsCanonicalization()) {
4334 // Check for NaN. All NaNs must be canonicalized.
4335 __ BranchF(NULL, &is_nan, eq, value, value);
4336 __ Branch(¬_nan);
4338 // Only load canonical NaN if the comparison above set the overflow.
4340 __ LoadRoot(at, Heap::kNanValueRootIndex);
4341 __ ldc1(double_scratch, FieldMemOperand(at, HeapNumber::kValueOffset));
4342 __ sdc1(double_scratch, MemOperand(scratch, 0));
4347 __ sdc1(value, MemOperand(scratch, 0));
4352 void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4353 Register value = ToRegister(instr->value());
4354 Register elements = ToRegister(instr->elements());
4355 Register key = instr->key()->IsRegister() ? ToRegister(instr->key())
4357 Register scratch = scratch0();
4358 Register store_base = scratch;
4359 int offset = instr->base_offset();
4362 if (instr->key()->IsConstantOperand()) {
4363 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4364 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
4365 offset += ToInteger32(const_operand) * kPointerSize;
4366 store_base = elements;
4368 // Even though the HLoadKeyed instruction forces the input
4369 // representation for the key to be an integer, the input gets replaced
4370 // during bound check elimination with the index argument to the bounds
4371 // check, which can be tagged, so that case must be handled here, too.
4372 if (instr->hydrogen()->key()->representation().IsSmi()) {
4373 __ sll(scratch, key, kPointerSizeLog2 - kSmiTagSize);
4374 __ addu(scratch, elements, scratch);
4376 __ sll(scratch, key, kPointerSizeLog2);
4377 __ addu(scratch, elements, scratch);
4380 __ sw(value, MemOperand(store_base, offset));
4382 if (instr->hydrogen()->NeedsWriteBarrier()) {
4383 SmiCheck check_needed =
4384 instr->hydrogen()->value()->type().IsHeapObject()
4385 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4386 // Compute address of modified element and store it into key register.
4387 __ Addu(key, store_base, Operand(offset));
4388 __ RecordWrite(elements,
4393 EMIT_REMEMBERED_SET,
4395 instr->hydrogen()->PointersToHereCheckForValue());
4400 void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4401 // By cases: external, fast double
4402 if (instr->is_typed_elements()) {
4403 DoStoreKeyedExternalArray(instr);
4404 } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4405 DoStoreKeyedFixedDoubleArray(instr);
4407 DoStoreKeyedFixedArray(instr);
4412 void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4413 DCHECK(ToRegister(instr->context()).is(cp));
4414 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4415 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4416 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4418 if (instr->hydrogen()->HasVectorAndSlot()) {
4419 EmitVectorStoreICRegisters<LStoreKeyedGeneric>(instr);
4422 Handle<Code> ic = CodeFactory::KeyedStoreICInOptimizedCode(
4423 isolate(), instr->language_mode(),
4424 instr->hydrogen()->initialization_state()).code();
4425 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4429 void LCodeGen::DoMaybeGrowElements(LMaybeGrowElements* instr) {
4430 class DeferredMaybeGrowElements final : public LDeferredCode {
4432 DeferredMaybeGrowElements(LCodeGen* codegen, LMaybeGrowElements* instr)
4433 : LDeferredCode(codegen), instr_(instr) {}
4434 void Generate() override { codegen()->DoDeferredMaybeGrowElements(instr_); }
4435 LInstruction* instr() override { return instr_; }
4438 LMaybeGrowElements* instr_;
4441 Register result = v0;
4442 DeferredMaybeGrowElements* deferred =
4443 new (zone()) DeferredMaybeGrowElements(this, instr);
4444 LOperand* key = instr->key();
4445 LOperand* current_capacity = instr->current_capacity();
4447 DCHECK(instr->hydrogen()->key()->representation().IsInteger32());
4448 DCHECK(instr->hydrogen()->current_capacity()->representation().IsInteger32());
4449 DCHECK(key->IsConstantOperand() || key->IsRegister());
4450 DCHECK(current_capacity->IsConstantOperand() ||
4451 current_capacity->IsRegister());
4453 if (key->IsConstantOperand() && current_capacity->IsConstantOperand()) {
4454 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4455 int32_t constant_capacity =
4456 ToInteger32(LConstantOperand::cast(current_capacity));
4457 if (constant_key >= constant_capacity) {
4459 __ jmp(deferred->entry());
4461 } else if (key->IsConstantOperand()) {
4462 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4463 __ Branch(deferred->entry(), le, ToRegister(current_capacity),
4464 Operand(constant_key));
4465 } else if (current_capacity->IsConstantOperand()) {
4466 int32_t constant_capacity =
4467 ToInteger32(LConstantOperand::cast(current_capacity));
4468 __ Branch(deferred->entry(), ge, ToRegister(key),
4469 Operand(constant_capacity));
4471 __ Branch(deferred->entry(), ge, ToRegister(key),
4472 Operand(ToRegister(current_capacity)));
4475 if (instr->elements()->IsRegister()) {
4476 __ mov(result, ToRegister(instr->elements()));
4478 __ lw(result, ToMemOperand(instr->elements()));
4481 __ bind(deferred->exit());
4485 void LCodeGen::DoDeferredMaybeGrowElements(LMaybeGrowElements* instr) {
4486 // TODO(3095996): Get rid of this. For now, we need to make the
4487 // result register contain a valid pointer because it is already
4488 // contained in the register pointer map.
4489 Register result = v0;
4490 __ mov(result, zero_reg);
4492 // We have to call a stub.
4494 PushSafepointRegistersScope scope(this);
4495 if (instr->object()->IsRegister()) {
4496 __ mov(result, ToRegister(instr->object()));
4498 __ lw(result, ToMemOperand(instr->object()));
4501 LOperand* key = instr->key();
4502 if (key->IsConstantOperand()) {
4503 __ li(a3, Operand(ToSmi(LConstantOperand::cast(key))));
4505 __ mov(a3, ToRegister(key));
4509 GrowArrayElementsStub stub(isolate(), instr->hydrogen()->is_js_array(),
4510 instr->hydrogen()->kind());
4513 RecordSafepointWithLazyDeopt(
4514 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
4515 __ StoreToSafepointRegisterSlot(result, result);
4518 // Deopt on smi, which means the elements array changed to dictionary mode.
4519 __ SmiTst(result, at);
4520 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
4524 void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4525 Register object_reg = ToRegister(instr->object());
4526 Register scratch = scratch0();
4528 Handle<Map> from_map = instr->original_map();
4529 Handle<Map> to_map = instr->transitioned_map();
4530 ElementsKind from_kind = instr->from_kind();
4531 ElementsKind to_kind = instr->to_kind();
4533 Label not_applicable;
4534 __ lw(scratch, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4535 __ Branch(¬_applicable, ne, scratch, Operand(from_map));
4537 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
4538 Register new_map_reg = ToRegister(instr->new_map_temp());
4539 __ li(new_map_reg, Operand(to_map));
4540 __ sw(new_map_reg, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4542 __ RecordWriteForMap(object_reg,
4548 DCHECK(object_reg.is(a0));
4549 DCHECK(ToRegister(instr->context()).is(cp));
4550 PushSafepointRegistersScope scope(this);
4551 __ li(a1, Operand(to_map));
4552 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4553 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4555 RecordSafepointWithRegisters(
4556 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
4558 __ bind(¬_applicable);
4562 void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4563 Register object = ToRegister(instr->object());
4564 Register temp = ToRegister(instr->temp());
4565 Label no_memento_found;
4566 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found,
4567 ne, &no_memento_found);
4568 DeoptimizeIf(al, instr);
4569 __ bind(&no_memento_found);
4573 void LCodeGen::DoStringAdd(LStringAdd* instr) {
4574 DCHECK(ToRegister(instr->context()).is(cp));
4575 DCHECK(ToRegister(instr->left()).is(a1));
4576 DCHECK(ToRegister(instr->right()).is(a0));
4577 StringAddStub stub(isolate(),
4578 instr->hydrogen()->flags(),
4579 instr->hydrogen()->pretenure_flag());
4580 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4584 void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
4585 class DeferredStringCharCodeAt final : public LDeferredCode {
4587 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
4588 : LDeferredCode(codegen), instr_(instr) { }
4589 void Generate() override { codegen()->DoDeferredStringCharCodeAt(instr_); }
4590 LInstruction* instr() override { return instr_; }
4593 LStringCharCodeAt* instr_;
4596 DeferredStringCharCodeAt* deferred =
4597 new(zone()) DeferredStringCharCodeAt(this, instr);
4598 StringCharLoadGenerator::Generate(masm(),
4599 ToRegister(instr->string()),
4600 ToRegister(instr->index()),
4601 ToRegister(instr->result()),
4603 __ bind(deferred->exit());
4607 void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4608 Register string = ToRegister(instr->string());
4609 Register result = ToRegister(instr->result());
4610 Register scratch = scratch0();
4612 // TODO(3095996): Get rid of this. For now, we need to make the
4613 // result register contain a valid pointer because it is already
4614 // contained in the register pointer map.
4615 __ mov(result, zero_reg);
4617 PushSafepointRegistersScope scope(this);
4619 // Push the index as a smi. This is safe because of the checks in
4620 // DoStringCharCodeAt above.
4621 if (instr->index()->IsConstantOperand()) {
4622 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
4623 __ Addu(scratch, zero_reg, Operand(Smi::FromInt(const_index)));
4626 Register index = ToRegister(instr->index());
4630 CallRuntimeFromDeferred(Runtime::kStringCharCodeAtRT, 2, instr,
4634 __ StoreToSafepointRegisterSlot(v0, result);
4638 void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
4639 class DeferredStringCharFromCode final : public LDeferredCode {
4641 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
4642 : LDeferredCode(codegen), instr_(instr) { }
4643 void Generate() override {
4644 codegen()->DoDeferredStringCharFromCode(instr_);
4646 LInstruction* instr() override { return instr_; }
4649 LStringCharFromCode* instr_;
4652 DeferredStringCharFromCode* deferred =
4653 new(zone()) DeferredStringCharFromCode(this, instr);
4655 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
4656 Register char_code = ToRegister(instr->char_code());
4657 Register result = ToRegister(instr->result());
4658 Register scratch = scratch0();
4659 DCHECK(!char_code.is(result));
4661 __ Branch(deferred->entry(), hi,
4662 char_code, Operand(String::kMaxOneByteCharCode));
4663 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
4664 __ sll(scratch, char_code, kPointerSizeLog2);
4665 __ Addu(result, result, scratch);
4666 __ lw(result, FieldMemOperand(result, FixedArray::kHeaderSize));
4667 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
4668 __ Branch(deferred->entry(), eq, result, Operand(scratch));
4669 __ bind(deferred->exit());
4673 void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4674 Register char_code = ToRegister(instr->char_code());
4675 Register result = ToRegister(instr->result());
4677 // TODO(3095996): Get rid of this. For now, we need to make the
4678 // result register contain a valid pointer because it is already
4679 // contained in the register pointer map.
4680 __ mov(result, zero_reg);
4682 PushSafepointRegistersScope scope(this);
4683 __ SmiTag(char_code);
4685 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr, instr->context());
4686 __ StoreToSafepointRegisterSlot(v0, result);
4690 void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
4691 LOperand* input = instr->value();
4692 DCHECK(input->IsRegister() || input->IsStackSlot());
4693 LOperand* output = instr->result();
4694 DCHECK(output->IsDoubleRegister());
4695 FPURegister single_scratch = double_scratch0().low();
4696 if (input->IsStackSlot()) {
4697 Register scratch = scratch0();
4698 __ lw(scratch, ToMemOperand(input));
4699 __ mtc1(scratch, single_scratch);
4701 __ mtc1(ToRegister(input), single_scratch);
4703 __ cvt_d_w(ToDoubleRegister(output), single_scratch);
4707 void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4708 LOperand* input = instr->value();
4709 LOperand* output = instr->result();
4711 FPURegister dbl_scratch = double_scratch0();
4712 __ mtc1(ToRegister(input), dbl_scratch);
4713 __ Cvt_d_uw(ToDoubleRegister(output), dbl_scratch, f22);
4717 void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
4718 class DeferredNumberTagI final : public LDeferredCode {
4720 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
4721 : LDeferredCode(codegen), instr_(instr) { }
4722 void Generate() override {
4723 codegen()->DoDeferredNumberTagIU(instr_,
4729 LInstruction* instr() override { return instr_; }
4732 LNumberTagI* instr_;
4735 Register src = ToRegister(instr->value());
4736 Register dst = ToRegister(instr->result());
4737 Register overflow = scratch0();
4739 DeferredNumberTagI* deferred = new(zone()) DeferredNumberTagI(this, instr);
4740 __ SmiTagCheckOverflow(dst, src, overflow);
4741 __ BranchOnOverflow(deferred->entry(), overflow);
4742 __ bind(deferred->exit());
4746 void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4747 class DeferredNumberTagU final : public LDeferredCode {
4749 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4750 : LDeferredCode(codegen), instr_(instr) { }
4751 void Generate() override {
4752 codegen()->DoDeferredNumberTagIU(instr_,
4758 LInstruction* instr() override { return instr_; }
4761 LNumberTagU* instr_;
4764 Register input = ToRegister(instr->value());
4765 Register result = ToRegister(instr->result());
4767 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4768 __ Branch(deferred->entry(), hi, input, Operand(Smi::kMaxValue));
4769 __ SmiTag(result, input);
4770 __ bind(deferred->exit());
4774 void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4778 IntegerSignedness signedness) {
4780 Register src = ToRegister(value);
4781 Register dst = ToRegister(instr->result());
4782 Register tmp1 = scratch0();
4783 Register tmp2 = ToRegister(temp1);
4784 Register tmp3 = ToRegister(temp2);
4785 DoubleRegister dbl_scratch = double_scratch0();
4787 if (signedness == SIGNED_INT32) {
4788 // There was overflow, so bits 30 and 31 of the original integer
4789 // disagree. Try to allocate a heap number in new space and store
4790 // the value in there. If that fails, call the runtime system.
4792 __ SmiUntag(src, dst);
4793 __ Xor(src, src, Operand(0x80000000));
4795 __ mtc1(src, dbl_scratch);
4796 __ cvt_d_w(dbl_scratch, dbl_scratch);
4798 __ mtc1(src, dbl_scratch);
4799 __ Cvt_d_uw(dbl_scratch, dbl_scratch, f22);
4802 if (FLAG_inline_new) {
4803 __ LoadRoot(tmp3, Heap::kHeapNumberMapRootIndex);
4804 __ AllocateHeapNumber(dst, tmp1, tmp2, tmp3, &slow, DONT_TAG_RESULT);
4808 // Slow case: Call the runtime system to do the number allocation.
4811 // TODO(3095996): Put a valid pointer value in the stack slot where the
4812 // result register is stored, as this register is in the pointer map, but
4813 // contains an integer value.
4814 __ mov(dst, zero_reg);
4816 // Preserve the value of all registers.
4817 PushSafepointRegistersScope scope(this);
4819 // NumberTagI and NumberTagD use the context from the frame, rather than
4820 // the environment's HContext or HInlinedContext value.
4821 // They only call Runtime::kAllocateHeapNumber.
4822 // The corresponding HChange instructions are added in a phase that does
4823 // not have easy access to the local context.
4824 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4825 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4826 RecordSafepointWithRegisters(
4827 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4828 __ Subu(v0, v0, kHeapObjectTag);
4829 __ StoreToSafepointRegisterSlot(v0, dst);
4833 // Done. Put the value in dbl_scratch into the value of the allocated heap
4836 __ sdc1(dbl_scratch, MemOperand(dst, HeapNumber::kValueOffset));
4837 __ Addu(dst, dst, kHeapObjectTag);
4841 void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
4842 class DeferredNumberTagD final : public LDeferredCode {
4844 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4845 : LDeferredCode(codegen), instr_(instr) { }
4846 void Generate() override { codegen()->DoDeferredNumberTagD(instr_); }
4847 LInstruction* instr() override { return instr_; }
4850 LNumberTagD* instr_;
4853 DoubleRegister input_reg = ToDoubleRegister(instr->value());
4854 Register scratch = scratch0();
4855 Register reg = ToRegister(instr->result());
4856 Register temp1 = ToRegister(instr->temp());
4857 Register temp2 = ToRegister(instr->temp2());
4859 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
4860 if (FLAG_inline_new) {
4861 __ LoadRoot(scratch, Heap::kHeapNumberMapRootIndex);
4862 // We want the untagged address first for performance
4863 __ AllocateHeapNumber(reg, temp1, temp2, scratch, deferred->entry(),
4866 __ Branch(deferred->entry());
4868 __ bind(deferred->exit());
4869 __ sdc1(input_reg, MemOperand(reg, HeapNumber::kValueOffset));
4870 // Now that we have finished with the object's real address tag it
4871 __ Addu(reg, reg, kHeapObjectTag);
4875 void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4876 // TODO(3095996): Get rid of this. For now, we need to make the
4877 // result register contain a valid pointer because it is already
4878 // contained in the register pointer map.
4879 Register reg = ToRegister(instr->result());
4880 __ mov(reg, zero_reg);
4882 PushSafepointRegistersScope scope(this);
4883 // NumberTagI and NumberTagD use the context from the frame, rather than
4884 // the environment's HContext or HInlinedContext value.
4885 // They only call Runtime::kAllocateHeapNumber.
4886 // The corresponding HChange instructions are added in a phase that does
4887 // not have easy access to the local context.
4888 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4889 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4890 RecordSafepointWithRegisters(
4891 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4892 __ Subu(v0, v0, kHeapObjectTag);
4893 __ StoreToSafepointRegisterSlot(v0, reg);
4897 void LCodeGen::DoSmiTag(LSmiTag* instr) {
4898 HChange* hchange = instr->hydrogen();
4899 Register input = ToRegister(instr->value());
4900 Register output = ToRegister(instr->result());
4901 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4902 hchange->value()->CheckFlag(HValue::kUint32)) {
4903 __ And(at, input, Operand(0xc0000000));
4904 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow, at, Operand(zero_reg));
4906 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4907 !hchange->value()->CheckFlag(HValue::kUint32)) {
4908 __ SmiTagCheckOverflow(output, input, at);
4909 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, at, Operand(zero_reg));
4911 __ SmiTag(output, input);
4916 void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4917 Register scratch = scratch0();
4918 Register input = ToRegister(instr->value());
4919 Register result = ToRegister(instr->result());
4920 if (instr->needs_check()) {
4921 STATIC_ASSERT(kHeapObjectTag == 1);
4922 // If the input is a HeapObject, value of scratch won't be zero.
4923 __ And(scratch, input, Operand(kHeapObjectTag));
4924 __ SmiUntag(result, input);
4925 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi, scratch, Operand(zero_reg));
4927 __ SmiUntag(result, input);
4932 void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
4933 DoubleRegister result_reg,
4934 NumberUntagDMode mode) {
4935 bool can_convert_undefined_to_nan =
4936 instr->hydrogen()->can_convert_undefined_to_nan();
4937 bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
4939 Register scratch = scratch0();
4940 Label convert, load_smi, done;
4941 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4943 __ UntagAndJumpIfSmi(scratch, input_reg, &load_smi);
4944 // Heap number map check.
4945 __ lw(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
4946 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4947 if (can_convert_undefined_to_nan) {
4948 __ Branch(&convert, ne, scratch, Operand(at));
4950 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, scratch,
4953 // Load heap number.
4954 __ ldc1(result_reg, FieldMemOperand(input_reg, HeapNumber::kValueOffset));
4955 if (deoptimize_on_minus_zero) {
4956 __ mfc1(at, result_reg.low());
4957 __ Branch(&done, ne, at, Operand(zero_reg));
4958 __ Mfhc1(scratch, result_reg);
4959 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, scratch,
4960 Operand(HeapNumber::kSignMask));
4963 if (can_convert_undefined_to_nan) {
4965 // Convert undefined (and hole) to NaN.
4966 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4967 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefined, input_reg,
4969 __ LoadRoot(scratch, Heap::kNanValueRootIndex);
4970 __ ldc1(result_reg, FieldMemOperand(scratch, HeapNumber::kValueOffset));
4974 __ SmiUntag(scratch, input_reg);
4975 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
4977 // Smi to double register conversion
4979 // scratch: untagged value of input_reg
4980 __ mtc1(scratch, result_reg);
4981 __ cvt_d_w(result_reg, result_reg);
4986 void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) {
4987 Register input_reg = ToRegister(instr->value());
4988 Register scratch1 = scratch0();
4989 Register scratch2 = ToRegister(instr->temp());
4990 DoubleRegister double_scratch = double_scratch0();
4991 DoubleRegister double_scratch2 = ToDoubleRegister(instr->temp2());
4993 DCHECK(!scratch1.is(input_reg) && !scratch1.is(scratch2));
4994 DCHECK(!scratch2.is(input_reg) && !scratch2.is(scratch1));
4998 // The input is a tagged HeapObject.
4999 // Heap number map check.
5000 __ lw(scratch1, FieldMemOperand(input_reg, HeapObject::kMapOffset));
5001 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5002 // This 'at' value and scratch1 map value are used for tests in both clauses
5005 if (instr->truncating()) {
5006 // Performs a truncating conversion of a floating point number as used by
5007 // the JS bitwise operations.
5008 Label no_heap_number, check_bools, check_false;
5009 // Check HeapNumber map.
5010 __ Branch(USE_DELAY_SLOT, &no_heap_number, ne, scratch1, Operand(at));
5011 __ mov(scratch2, input_reg); // In delay slot.
5012 __ TruncateHeapNumberToI(input_reg, scratch2);
5015 // Check for Oddballs. Undefined/False is converted to zero and True to one
5016 // for truncating conversions.
5017 __ bind(&no_heap_number);
5018 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5019 __ Branch(&check_bools, ne, input_reg, Operand(at));
5020 DCHECK(ToRegister(instr->result()).is(input_reg));
5021 __ Branch(USE_DELAY_SLOT, &done);
5022 __ mov(input_reg, zero_reg); // In delay slot.
5024 __ bind(&check_bools);
5025 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5026 __ Branch(&check_false, ne, scratch2, Operand(at));
5027 __ Branch(USE_DELAY_SLOT, &done);
5028 __ li(input_reg, Operand(1)); // In delay slot.
5030 __ bind(&check_false);
5031 __ LoadRoot(at, Heap::kFalseValueRootIndex);
5032 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefinedBoolean,
5033 scratch2, Operand(at));
5034 __ Branch(USE_DELAY_SLOT, &done);
5035 __ mov(input_reg, zero_reg); // In delay slot.
5037 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, scratch1,
5040 // Load the double value.
5041 __ ldc1(double_scratch,
5042 FieldMemOperand(input_reg, HeapNumber::kValueOffset));
5044 Register except_flag = scratch2;
5045 __ EmitFPUTruncate(kRoundToZero,
5051 kCheckForInexactConversion);
5053 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
5056 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5057 __ Branch(&done, ne, input_reg, Operand(zero_reg));
5059 __ Mfhc1(scratch1, double_scratch);
5060 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
5061 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
5069 void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
5070 class DeferredTaggedToI final : public LDeferredCode {
5072 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
5073 : LDeferredCode(codegen), instr_(instr) { }
5074 void Generate() override { codegen()->DoDeferredTaggedToI(instr_); }
5075 LInstruction* instr() override { return instr_; }
5081 LOperand* input = instr->value();
5082 DCHECK(input->IsRegister());
5083 DCHECK(input->Equals(instr->result()));
5085 Register input_reg = ToRegister(input);
5087 if (instr->hydrogen()->value()->representation().IsSmi()) {
5088 __ SmiUntag(input_reg);
5090 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
5092 // Let the deferred code handle the HeapObject case.
5093 __ JumpIfNotSmi(input_reg, deferred->entry());
5095 // Smi to int32 conversion.
5096 __ SmiUntag(input_reg);
5097 __ bind(deferred->exit());
5102 void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
5103 LOperand* input = instr->value();
5104 DCHECK(input->IsRegister());
5105 LOperand* result = instr->result();
5106 DCHECK(result->IsDoubleRegister());
5108 Register input_reg = ToRegister(input);
5109 DoubleRegister result_reg = ToDoubleRegister(result);
5111 HValue* value = instr->hydrogen()->value();
5112 NumberUntagDMode mode = value->representation().IsSmi()
5113 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
5115 EmitNumberUntagD(instr, input_reg, result_reg, mode);
5119 void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
5120 Register result_reg = ToRegister(instr->result());
5121 Register scratch1 = scratch0();
5122 DoubleRegister double_input = ToDoubleRegister(instr->value());
5124 if (instr->truncating()) {
5125 __ TruncateDoubleToI(result_reg, double_input);
5127 Register except_flag = LCodeGen::scratch1();
5129 __ EmitFPUTruncate(kRoundToMinusInf,
5135 kCheckForInexactConversion);
5137 // Deopt if the operation did not succeed (except_flag != 0).
5138 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
5141 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5143 __ Branch(&done, ne, result_reg, Operand(zero_reg));
5144 __ Mfhc1(scratch1, double_input);
5145 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
5146 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
5154 void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
5155 Register result_reg = ToRegister(instr->result());
5156 Register scratch1 = LCodeGen::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,
5186 __ SmiTagCheckOverflow(result_reg, result_reg, scratch1);
5187 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, scratch1, Operand(zero_reg));
5191 void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
5192 LOperand* input = instr->value();
5193 __ SmiTst(ToRegister(input), at);
5194 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi, at, Operand(zero_reg));
5198 void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
5199 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
5200 LOperand* input = instr->value();
5201 __ SmiTst(ToRegister(input), at);
5202 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
5207 void LCodeGen::DoCheckArrayBufferNotNeutered(
5208 LCheckArrayBufferNotNeutered* instr) {
5209 Register view = ToRegister(instr->view());
5210 Register scratch = scratch0();
5212 __ lw(scratch, FieldMemOperand(view, JSArrayBufferView::kBufferOffset));
5213 __ lw(scratch, FieldMemOperand(scratch, JSArrayBuffer::kBitFieldOffset));
5214 __ And(at, scratch, 1 << JSArrayBuffer::WasNeutered::kShift);
5215 DeoptimizeIf(ne, instr, Deoptimizer::kOutOfBounds, at, Operand(zero_reg));
5219 void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
5220 Register input = ToRegister(instr->value());
5221 Register scratch = scratch0();
5223 __ GetObjectType(input, scratch, scratch);
5225 if (instr->hydrogen()->is_interval_check()) {
5228 instr->hydrogen()->GetCheckInterval(&first, &last);
5230 // If there is only one type in the interval check for equality.
5231 if (first == last) {
5232 DeoptimizeIf(ne, instr, Deoptimizer::kWrongInstanceType, scratch,
5235 DeoptimizeIf(lo, instr, Deoptimizer::kWrongInstanceType, scratch,
5237 // Omit check for the last type.
5238 if (last != LAST_TYPE) {
5239 DeoptimizeIf(hi, instr, Deoptimizer::kWrongInstanceType, scratch,
5246 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
5248 if (base::bits::IsPowerOfTwo32(mask)) {
5249 DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
5250 __ And(at, scratch, mask);
5251 DeoptimizeIf(tag == 0 ? ne : eq, instr, Deoptimizer::kWrongInstanceType,
5252 at, Operand(zero_reg));
5254 __ And(scratch, scratch, Operand(mask));
5255 DeoptimizeIf(ne, instr, Deoptimizer::kWrongInstanceType, scratch,
5262 void LCodeGen::DoCheckValue(LCheckValue* instr) {
5263 Register reg = ToRegister(instr->value());
5264 Handle<HeapObject> object = instr->hydrogen()->object().handle();
5265 AllowDeferredHandleDereference smi_check;
5266 if (isolate()->heap()->InNewSpace(*object)) {
5267 Register reg = ToRegister(instr->value());
5268 Handle<Cell> cell = isolate()->factory()->NewCell(object);
5269 __ li(at, Operand(cell));
5270 __ lw(at, FieldMemOperand(at, Cell::kValueOffset));
5271 DeoptimizeIf(ne, instr, Deoptimizer::kValueMismatch, reg, Operand(at));
5273 DeoptimizeIf(ne, instr, Deoptimizer::kValueMismatch, reg, Operand(object));
5278 void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
5280 PushSafepointRegistersScope scope(this);
5282 __ mov(cp, zero_reg);
5283 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
5284 RecordSafepointWithRegisters(
5285 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
5286 __ StoreToSafepointRegisterSlot(v0, scratch0());
5288 __ SmiTst(scratch0(), at);
5289 DeoptimizeIf(eq, instr, Deoptimizer::kInstanceMigrationFailed, at,
5294 void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
5295 class DeferredCheckMaps final : public LDeferredCode {
5297 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
5298 : LDeferredCode(codegen), instr_(instr), object_(object) {
5299 SetExit(check_maps());
5301 void Generate() override {
5302 codegen()->DoDeferredInstanceMigration(instr_, object_);
5304 Label* check_maps() { return &check_maps_; }
5305 LInstruction* instr() override { return instr_; }
5313 if (instr->hydrogen()->IsStabilityCheck()) {
5314 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5315 for (int i = 0; i < maps->size(); ++i) {
5316 AddStabilityDependency(maps->at(i).handle());
5321 Register map_reg = scratch0();
5322 LOperand* input = instr->value();
5323 DCHECK(input->IsRegister());
5324 Register reg = ToRegister(input);
5325 __ lw(map_reg, FieldMemOperand(reg, HeapObject::kMapOffset));
5327 DeferredCheckMaps* deferred = NULL;
5328 if (instr->hydrogen()->HasMigrationTarget()) {
5329 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5330 __ bind(deferred->check_maps());
5333 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5335 for (int i = 0; i < maps->size() - 1; i++) {
5336 Handle<Map> map = maps->at(i).handle();
5337 __ CompareMapAndBranch(map_reg, map, &success, eq, &success);
5339 Handle<Map> map = maps->at(maps->size() - 1).handle();
5340 // Do the CompareMap() directly within the Branch() and DeoptimizeIf().
5341 if (instr->hydrogen()->HasMigrationTarget()) {
5342 __ Branch(deferred->entry(), ne, map_reg, Operand(map));
5344 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap, map_reg, Operand(map));
5351 void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
5352 DoubleRegister value_reg = ToDoubleRegister(instr->unclamped());
5353 Register result_reg = ToRegister(instr->result());
5354 DoubleRegister temp_reg = ToDoubleRegister(instr->temp());
5355 __ ClampDoubleToUint8(result_reg, value_reg, temp_reg);
5359 void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
5360 Register unclamped_reg = ToRegister(instr->unclamped());
5361 Register result_reg = ToRegister(instr->result());
5362 __ ClampUint8(result_reg, unclamped_reg);
5366 void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
5367 Register scratch = scratch0();
5368 Register input_reg = ToRegister(instr->unclamped());
5369 Register result_reg = ToRegister(instr->result());
5370 DoubleRegister temp_reg = ToDoubleRegister(instr->temp());
5371 Label is_smi, done, heap_number;
5373 // Both smi and heap number cases are handled.
5374 __ UntagAndJumpIfSmi(scratch, input_reg, &is_smi);
5376 // Check for heap number
5377 __ lw(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
5378 __ Branch(&heap_number, eq, scratch, Operand(factory()->heap_number_map()));
5380 // Check for undefined. Undefined is converted to zero for clamping
5382 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefined, input_reg,
5383 Operand(factory()->undefined_value()));
5384 __ mov(result_reg, zero_reg);
5388 __ bind(&heap_number);
5389 __ ldc1(double_scratch0(), FieldMemOperand(input_reg,
5390 HeapNumber::kValueOffset));
5391 __ ClampDoubleToUint8(result_reg, double_scratch0(), temp_reg);
5395 __ ClampUint8(result_reg, scratch);
5401 void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5402 DoubleRegister value_reg = ToDoubleRegister(instr->value());
5403 Register result_reg = ToRegister(instr->result());
5404 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5405 __ FmoveHigh(result_reg, value_reg);
5407 __ FmoveLow(result_reg, value_reg);
5412 void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5413 Register hi_reg = ToRegister(instr->hi());
5414 Register lo_reg = ToRegister(instr->lo());
5415 DoubleRegister result_reg = ToDoubleRegister(instr->result());
5416 __ Move(result_reg, lo_reg, hi_reg);
5420 void LCodeGen::DoAllocate(LAllocate* instr) {
5421 class DeferredAllocate final : public LDeferredCode {
5423 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
5424 : LDeferredCode(codegen), instr_(instr) { }
5425 void Generate() override { codegen()->DoDeferredAllocate(instr_); }
5426 LInstruction* instr() override { return instr_; }
5432 DeferredAllocate* deferred =
5433 new(zone()) DeferredAllocate(this, instr);
5435 Register result = ToRegister(instr->result());
5436 Register scratch = ToRegister(instr->temp1());
5437 Register scratch2 = ToRegister(instr->temp2());
5439 // Allocate memory for the object.
5440 AllocationFlags flags = TAG_OBJECT;
5441 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5442 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5444 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5445 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5446 flags = static_cast<AllocationFlags>(flags | PRETENURE);
5448 if (instr->size()->IsConstantOperand()) {
5449 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5450 if (size <= Page::kMaxRegularHeapObjectSize) {
5451 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5453 __ jmp(deferred->entry());
5456 Register size = ToRegister(instr->size());
5457 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5460 __ bind(deferred->exit());
5462 if (instr->hydrogen()->MustPrefillWithFiller()) {
5463 STATIC_ASSERT(kHeapObjectTag == 1);
5464 if (instr->size()->IsConstantOperand()) {
5465 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5466 __ li(scratch, Operand(size - kHeapObjectTag));
5468 __ Subu(scratch, ToRegister(instr->size()), Operand(kHeapObjectTag));
5470 __ li(scratch2, Operand(isolate()->factory()->one_pointer_filler_map()));
5473 __ Subu(scratch, scratch, Operand(kPointerSize));
5474 __ Addu(at, result, Operand(scratch));
5475 __ sw(scratch2, MemOperand(at));
5476 __ Branch(&loop, ge, scratch, Operand(zero_reg));
5481 void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
5482 Register result = ToRegister(instr->result());
5484 // TODO(3095996): Get rid of this. For now, we need to make the
5485 // result register contain a valid pointer because it is already
5486 // contained in the register pointer map.
5487 __ mov(result, zero_reg);
5489 PushSafepointRegistersScope scope(this);
5490 if (instr->size()->IsRegister()) {
5491 Register size = ToRegister(instr->size());
5492 DCHECK(!size.is(result));
5496 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5497 if (size >= 0 && size <= Smi::kMaxValue) {
5498 __ Push(Smi::FromInt(size));
5500 // We should never get here at runtime => abort
5501 __ stop("invalid allocation size");
5506 int flags = AllocateDoubleAlignFlag::encode(
5507 instr->hydrogen()->MustAllocateDoubleAligned());
5508 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5509 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5510 flags = AllocateTargetSpace::update(flags, OLD_SPACE);
5512 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5514 __ Push(Smi::FromInt(flags));
5516 CallRuntimeFromDeferred(
5517 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
5518 __ StoreToSafepointRegisterSlot(v0, result);
5522 void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
5523 DCHECK(ToRegister(instr->value()).is(a0));
5524 DCHECK(ToRegister(instr->result()).is(v0));
5526 CallRuntime(Runtime::kToFastProperties, 1, instr);
5530 void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
5531 DCHECK(ToRegister(instr->context()).is(cp));
5533 // Registers will be used as follows:
5534 // t3 = literals array.
5535 // a1 = regexp literal.
5536 // a0 = regexp literal clone.
5537 // a2 and t0-t2 are used as temporaries.
5538 int literal_offset =
5539 FixedArray::OffsetOfElementAt(instr->hydrogen()->literal_index());
5540 __ li(t3, instr->hydrogen()->literals());
5541 __ lw(a1, FieldMemOperand(t3, literal_offset));
5542 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5543 __ Branch(&materialized, ne, a1, Operand(at));
5545 // Create regexp literal using runtime function
5546 // Result will be in v0.
5547 __ li(t2, Operand(Smi::FromInt(instr->hydrogen()->literal_index())));
5548 __ li(t1, Operand(instr->hydrogen()->pattern()));
5549 __ li(t0, Operand(instr->hydrogen()->flags()));
5550 __ Push(t3, t2, t1, t0);
5551 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
5554 __ bind(&materialized);
5555 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
5556 Label allocated, runtime_allocate;
5558 __ Allocate(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
5561 __ bind(&runtime_allocate);
5562 __ li(a0, Operand(Smi::FromInt(size)));
5564 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
5567 __ bind(&allocated);
5568 // Copy the content into the newly allocated memory.
5569 // (Unroll copy loop once for better throughput).
5570 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
5571 __ lw(a3, FieldMemOperand(a1, i));
5572 __ lw(a2, FieldMemOperand(a1, i + kPointerSize));
5573 __ sw(a3, FieldMemOperand(v0, i));
5574 __ sw(a2, FieldMemOperand(v0, i + kPointerSize));
5576 if ((size % (2 * kPointerSize)) != 0) {
5577 __ lw(a3, FieldMemOperand(a1, size - kPointerSize));
5578 __ sw(a3, FieldMemOperand(v0, size - kPointerSize));
5583 void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
5584 DCHECK(ToRegister(instr->context()).is(cp));
5585 // Use the fast case closure allocation code that allocates in new
5586 // space for nested functions that don't need literals cloning.
5587 bool pretenure = instr->hydrogen()->pretenure();
5588 if (!pretenure && instr->hydrogen()->has_no_literals()) {
5589 FastNewClosureStub stub(isolate(), instr->hydrogen()->language_mode(),
5590 instr->hydrogen()->kind());
5591 __ li(a2, Operand(instr->hydrogen()->shared_info()));
5592 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5594 __ li(a2, Operand(instr->hydrogen()->shared_info()));
5595 __ li(a1, Operand(pretenure ? factory()->true_value()
5596 : factory()->false_value()));
5597 __ Push(cp, a2, a1);
5598 CallRuntime(Runtime::kNewClosure, 3, instr);
5603 void LCodeGen::DoTypeof(LTypeof* instr) {
5604 DCHECK(ToRegister(instr->value()).is(a3));
5605 DCHECK(ToRegister(instr->result()).is(v0));
5607 Register value_register = ToRegister(instr->value());
5608 __ JumpIfNotSmi(value_register, &do_call);
5609 __ li(v0, Operand(isolate()->factory()->number_string()));
5612 TypeofStub stub(isolate());
5613 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5618 void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5619 Register input = ToRegister(instr->value());
5621 Register cmp1 = no_reg;
5622 Operand cmp2 = Operand(no_reg);
5624 Condition final_branch_condition = EmitTypeofIs(instr->TrueLabel(chunk_),
5625 instr->FalseLabel(chunk_),
5627 instr->type_literal(),
5631 DCHECK(cmp1.is_valid());
5632 DCHECK(!cmp2.is_reg() || cmp2.rm().is_valid());
5634 if (final_branch_condition != kNoCondition) {
5635 EmitBranch(instr, final_branch_condition, cmp1, cmp2);
5640 Condition LCodeGen::EmitTypeofIs(Label* true_label,
5643 Handle<String> type_name,
5646 // This function utilizes the delay slot heavily. This is used to load
5647 // values that are always usable without depending on the type of the input
5649 Condition final_branch_condition = kNoCondition;
5650 Register scratch = scratch0();
5651 Factory* factory = isolate()->factory();
5652 if (String::Equals(type_name, factory->number_string())) {
5653 __ JumpIfSmi(input, true_label);
5654 __ lw(input, FieldMemOperand(input, HeapObject::kMapOffset));
5655 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5657 *cmp2 = Operand(at);
5658 final_branch_condition = eq;
5660 } else if (String::Equals(type_name, factory->string_string())) {
5661 __ JumpIfSmi(input, false_label);
5662 __ GetObjectType(input, input, scratch);
5663 __ Branch(USE_DELAY_SLOT, false_label,
5664 ge, scratch, Operand(FIRST_NONSTRING_TYPE));
5665 // input is an object so we can load the BitFieldOffset even if we take the
5667 __ lbu(at, FieldMemOperand(input, Map::kBitFieldOffset));
5668 __ And(at, at, 1 << Map::kIsUndetectable);
5670 *cmp2 = Operand(zero_reg);
5671 final_branch_condition = eq;
5673 } else if (String::Equals(type_name, factory->symbol_string())) {
5674 __ JumpIfSmi(input, false_label);
5675 __ GetObjectType(input, input, scratch);
5677 *cmp2 = Operand(SYMBOL_TYPE);
5678 final_branch_condition = eq;
5680 } else if (String::Equals(type_name, factory->boolean_string())) {
5681 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5682 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5683 __ LoadRoot(at, Heap::kFalseValueRootIndex);
5685 *cmp2 = Operand(input);
5686 final_branch_condition = eq;
5688 } else if (String::Equals(type_name, factory->undefined_string())) {
5689 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5690 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5691 // The first instruction of JumpIfSmi is an And - it is safe in the delay
5693 __ JumpIfSmi(input, false_label);
5694 // Check for undetectable objects => true.
5695 __ lw(input, FieldMemOperand(input, HeapObject::kMapOffset));
5696 __ lbu(at, FieldMemOperand(input, Map::kBitFieldOffset));
5697 __ And(at, at, 1 << Map::kIsUndetectable);
5699 *cmp2 = Operand(zero_reg);
5700 final_branch_condition = ne;
5702 } else if (String::Equals(type_name, factory->function_string())) {
5703 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5704 __ JumpIfSmi(input, false_label);
5705 __ GetObjectType(input, scratch, input);
5706 __ Branch(true_label, eq, input, Operand(JS_FUNCTION_TYPE));
5708 *cmp2 = Operand(JS_FUNCTION_PROXY_TYPE);
5709 final_branch_condition = eq;
5711 } else if (String::Equals(type_name, factory->object_string())) {
5712 __ JumpIfSmi(input, false_label);
5713 __ LoadRoot(at, Heap::kNullValueRootIndex);
5714 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5715 Register map = input;
5716 __ GetObjectType(input, map, scratch);
5717 __ Branch(false_label,
5718 lt, scratch, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
5719 __ Branch(USE_DELAY_SLOT, false_label,
5720 gt, scratch, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
5721 // map is still valid, so the BitField can be loaded in delay slot.
5722 // Check for undetectable objects => false.
5723 __ lbu(at, FieldMemOperand(map, Map::kBitFieldOffset));
5724 __ And(at, at, 1 << Map::kIsUndetectable);
5726 *cmp2 = Operand(zero_reg);
5727 final_branch_condition = eq;
5729 } else if (String::Equals(type_name, factory->float32x4_string())) {
5730 __ JumpIfSmi(input, false_label);
5731 __ GetObjectType(input, input, scratch);
5733 *cmp2 = Operand(FLOAT32X4_TYPE);
5734 final_branch_condition = eq;
5738 *cmp2 = Operand(zero_reg); // Set to valid regs, to avoid caller assertion.
5739 __ Branch(false_label);
5742 return final_branch_condition;
5746 void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
5747 Register temp1 = ToRegister(instr->temp());
5749 EmitIsConstructCall(temp1, scratch0());
5751 EmitBranch(instr, eq, temp1,
5752 Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
5756 void LCodeGen::EmitIsConstructCall(Register temp1, Register temp2) {
5757 DCHECK(!temp1.is(temp2));
5758 // Get the frame pointer for the calling frame.
5759 __ lw(temp1, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5761 // Skip the arguments adaptor frame if it exists.
5762 Label check_frame_marker;
5763 __ lw(temp2, MemOperand(temp1, StandardFrameConstants::kContextOffset));
5764 __ Branch(&check_frame_marker, ne, temp2,
5765 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5766 __ lw(temp1, MemOperand(temp1, StandardFrameConstants::kCallerFPOffset));
5768 // Check the marker in the calling frame.
5769 __ bind(&check_frame_marker);
5770 __ lw(temp1, MemOperand(temp1, StandardFrameConstants::kMarkerOffset));
5774 void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5775 if (!info()->IsStub()) {
5776 // Ensure that we have enough space after the previous lazy-bailout
5777 // instruction for patching the code here.
5778 int current_pc = masm()->pc_offset();
5779 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5780 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5781 DCHECK_EQ(0, padding_size % Assembler::kInstrSize);
5782 while (padding_size > 0) {
5784 padding_size -= Assembler::kInstrSize;
5788 last_lazy_deopt_pc_ = masm()->pc_offset();
5792 void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
5793 last_lazy_deopt_pc_ = masm()->pc_offset();
5794 DCHECK(instr->HasEnvironment());
5795 LEnvironment* env = instr->environment();
5796 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5797 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5801 void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
5802 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5803 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5804 // needed return address), even though the implementation of LAZY and EAGER is
5805 // now identical. When LAZY is eventually completely folded into EAGER, remove
5806 // the special case below.
5807 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5808 type = Deoptimizer::LAZY;
5811 DeoptimizeIf(al, instr, instr->hydrogen()->reason(), type, zero_reg,
5816 void LCodeGen::DoDummy(LDummy* instr) {
5817 // Nothing to see here, move on!
5821 void LCodeGen::DoDummyUse(LDummyUse* instr) {
5822 // Nothing to see here, move on!
5826 void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
5827 PushSafepointRegistersScope scope(this);
5828 LoadContextFromDeferred(instr->context());
5829 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5830 RecordSafepointWithLazyDeopt(
5831 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
5832 DCHECK(instr->HasEnvironment());
5833 LEnvironment* env = instr->environment();
5834 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5838 void LCodeGen::DoStackCheck(LStackCheck* instr) {
5839 class DeferredStackCheck final : public LDeferredCode {
5841 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5842 : LDeferredCode(codegen), instr_(instr) { }
5843 void Generate() override { codegen()->DoDeferredStackCheck(instr_); }
5844 LInstruction* instr() override { return instr_; }
5847 LStackCheck* instr_;
5850 DCHECK(instr->HasEnvironment());
5851 LEnvironment* env = instr->environment();
5852 // There is no LLazyBailout instruction for stack-checks. We have to
5853 // prepare for lazy deoptimization explicitly here.
5854 if (instr->hydrogen()->is_function_entry()) {
5855 // Perform stack overflow check.
5857 __ LoadRoot(at, Heap::kStackLimitRootIndex);
5858 __ Branch(&done, hs, sp, Operand(at));
5859 DCHECK(instr->context()->IsRegister());
5860 DCHECK(ToRegister(instr->context()).is(cp));
5861 CallCode(isolate()->builtins()->StackCheck(),
5862 RelocInfo::CODE_TARGET,
5866 DCHECK(instr->hydrogen()->is_backwards_branch());
5867 // Perform stack overflow check if this goto needs it before jumping.
5868 DeferredStackCheck* deferred_stack_check =
5869 new(zone()) DeferredStackCheck(this, instr);
5870 __ LoadRoot(at, Heap::kStackLimitRootIndex);
5871 __ Branch(deferred_stack_check->entry(), lo, sp, Operand(at));
5872 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
5873 __ bind(instr->done_label());
5874 deferred_stack_check->SetExit(instr->done_label());
5875 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5876 // Don't record a deoptimization index for the safepoint here.
5877 // This will be done explicitly when emitting call and the safepoint in
5878 // the deferred code.
5883 void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
5884 // This is a pseudo-instruction that ensures that the environment here is
5885 // properly registered for deoptimization and records the assembler's PC
5887 LEnvironment* environment = instr->environment();
5889 // If the environment were already registered, we would have no way of
5890 // backpatching it with the spill slot operands.
5891 DCHECK(!environment->HasBeenRegistered());
5892 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
5894 GenerateOsrPrologue();
5898 void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
5899 Register result = ToRegister(instr->result());
5900 Register object = ToRegister(instr->object());
5901 __ And(at, object, kSmiTagMask);
5902 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
5904 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
5905 __ GetObjectType(object, a1, a1);
5906 DeoptimizeIf(le, instr, Deoptimizer::kNotAJavaScriptObject, a1,
5907 Operand(LAST_JS_PROXY_TYPE));
5909 Label use_cache, call_runtime;
5910 DCHECK(object.is(a0));
5911 Register null_value = t1;
5912 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
5913 __ CheckEnumCache(null_value, &call_runtime);
5915 __ lw(result, FieldMemOperand(object, HeapObject::kMapOffset));
5916 __ Branch(&use_cache);
5918 // Get the set of properties to enumerate.
5919 __ bind(&call_runtime);
5921 CallRuntime(Runtime::kGetPropertyNamesFast, 1, instr);
5923 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
5924 DCHECK(result.is(v0));
5925 __ LoadRoot(at, Heap::kMetaMapRootIndex);
5926 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap, a1, Operand(at));
5927 __ bind(&use_cache);
5931 void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5932 Register map = ToRegister(instr->map());
5933 Register result = ToRegister(instr->result());
5934 Label load_cache, done;
5935 __ EnumLength(result, map);
5936 __ Branch(&load_cache, ne, result, Operand(Smi::FromInt(0)));
5937 __ li(result, Operand(isolate()->factory()->empty_fixed_array()));
5940 __ bind(&load_cache);
5941 __ LoadInstanceDescriptors(map, result);
5943 FieldMemOperand(result, DescriptorArray::kEnumCacheOffset));
5945 FieldMemOperand(result, FixedArray::SizeFor(instr->idx())));
5946 DeoptimizeIf(eq, instr, Deoptimizer::kNoCache, result, Operand(zero_reg));
5952 void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5953 Register object = ToRegister(instr->value());
5954 Register map = ToRegister(instr->map());
5955 __ lw(scratch0(), FieldMemOperand(object, HeapObject::kMapOffset));
5956 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap, map, Operand(scratch0()));
5960 void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5964 PushSafepointRegistersScope scope(this);
5965 __ Push(object, index);
5966 __ mov(cp, zero_reg);
5967 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
5968 RecordSafepointWithRegisters(
5969 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
5970 __ StoreToSafepointRegisterSlot(v0, result);
5974 void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
5975 class DeferredLoadMutableDouble final : public LDeferredCode {
5977 DeferredLoadMutableDouble(LCodeGen* codegen,
5978 LLoadFieldByIndex* instr,
5982 : LDeferredCode(codegen),
5988 void Generate() override {
5989 codegen()->DoDeferredLoadMutableDouble(instr_, result_, object_, index_);
5991 LInstruction* instr() override { return instr_; }
5994 LLoadFieldByIndex* instr_;
6000 Register object = ToRegister(instr->object());
6001 Register index = ToRegister(instr->index());
6002 Register result = ToRegister(instr->result());
6003 Register scratch = scratch0();
6005 DeferredLoadMutableDouble* deferred;
6006 deferred = new(zone()) DeferredLoadMutableDouble(
6007 this, instr, result, object, index);
6009 Label out_of_object, done;
6011 __ And(scratch, index, Operand(Smi::FromInt(1)));
6012 __ Branch(deferred->entry(), ne, scratch, Operand(zero_reg));
6013 __ sra(index, index, 1);
6015 __ Branch(USE_DELAY_SLOT, &out_of_object, lt, index, Operand(zero_reg));
6016 __ sll(scratch, index, kPointerSizeLog2 - kSmiTagSize); // In delay slot.
6018 STATIC_ASSERT(kPointerSizeLog2 > kSmiTagSize);
6019 __ Addu(scratch, object, scratch);
6020 __ lw(result, FieldMemOperand(scratch, JSObject::kHeaderSize));
6024 __ bind(&out_of_object);
6025 __ lw(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
6026 // Index is equal to negated out of object property index plus 1.
6027 __ Subu(scratch, result, scratch);
6028 __ lw(result, FieldMemOperand(scratch,
6029 FixedArray::kHeaderSize - kPointerSize));
6030 __ bind(deferred->exit());
6035 void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
6036 Register context = ToRegister(instr->context());
6037 __ sw(context, MemOperand(fp, StandardFrameConstants::kContextOffset));
6041 void LCodeGen::DoAllocateBlockContext(LAllocateBlockContext* instr) {
6042 Handle<ScopeInfo> scope_info = instr->scope_info();
6043 __ li(at, scope_info);
6044 __ Push(at, ToRegister(instr->function()));
6045 CallRuntime(Runtime::kPushBlockContext, 2, instr);
6046 RecordSafepoint(Safepoint::kNoLazyDeopt);
6052 } // namespace internal