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/hydrogen-osr.h"
34 #include "src/ic/ic.h"
35 #include "src/ic/stub-cache.h"
36 #include "src/mips/lithium-codegen-mips.h"
37 #include "src/mips/lithium-gap-resolver-mips.h"
44 class SafepointGenerator FINAL : public CallWrapper {
46 SafepointGenerator(LCodeGen* codegen,
47 LPointerMap* pointers,
48 Safepoint::DeoptMode mode)
52 virtual ~SafepointGenerator() {}
54 virtual void BeforeCall(int call_size) const OVERRIDE {}
56 virtual void AfterCall() const OVERRIDE {
57 codegen_->RecordSafepoint(pointers_, deopt_mode_);
62 LPointerMap* pointers_;
63 Safepoint::DeoptMode deopt_mode_;
69 bool LCodeGen::GenerateCode() {
70 LPhase phase("Z_Code generation", chunk());
74 // Open a frame scope to indicate that there is a frame on the stack. The
75 // NONE indicates that the scope shouldn't actually generate code to set up
76 // the frame (that is done in GeneratePrologue).
77 FrameScope frame_scope(masm_, StackFrame::NONE);
79 return GeneratePrologue() && GenerateBody() && GenerateDeferredCode() &&
80 GenerateJumpTable() && GenerateSafepointTable();
84 void LCodeGen::FinishCode(Handle<Code> code) {
86 code->set_stack_slots(GetStackSlotCount());
87 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
88 if (code->is_optimized_code()) RegisterWeakObjectsInOptimizedCode(code);
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 (info_->this_has_uses() &&
147 info_->strict_mode() == SLOPPY &&
148 !info_->is_native()) {
150 int receiver_offset = info_->scope()->num_parameters() * kPointerSize;
151 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
152 __ lw(a2, MemOperand(sp, receiver_offset));
153 __ Branch(&ok, ne, a2, Operand(at));
155 __ lw(a2, GlobalObjectOperand());
156 __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalProxyOffset));
158 __ sw(a2, MemOperand(sp, receiver_offset));
164 info()->set_prologue_offset(masm_->pc_offset());
165 if (NeedsEagerFrame()) {
166 if (info()->IsStub()) {
169 __ Prologue(info()->IsCodePreAgingActive());
171 frame_is_built_ = true;
172 info_->AddNoFrameRange(0, masm_->pc_offset());
175 // Reserve space for the stack slots needed by the code.
176 int slots = GetStackSlotCount();
178 if (FLAG_debug_code) {
179 __ Subu(sp, sp, Operand(slots * kPointerSize));
181 __ Addu(a0, sp, Operand(slots * kPointerSize));
182 __ li(a1, Operand(kSlotsZapValue));
185 __ Subu(a0, a0, Operand(kPointerSize));
186 __ sw(a1, MemOperand(a0, 2 * kPointerSize));
187 __ Branch(&loop, ne, a0, Operand(sp));
190 __ Subu(sp, sp, Operand(slots * kPointerSize));
194 if (info()->saves_caller_doubles()) {
198 // Possibly allocate a local context.
199 int heap_slots = info()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
200 if (heap_slots > 0) {
201 Comment(";;; Allocate local context");
202 bool need_write_barrier = true;
203 // Argument to NewContext is the function, which is in a1.
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 for (int i = 0; i < num_parameters; i++) {
221 Variable* var = scope()->parameter(i);
222 if (var->IsContextSlot()) {
223 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
224 (num_parameters - 1 - i) * kPointerSize;
225 // Load parameter from stack.
226 __ lw(a0, MemOperand(fp, parameter_offset));
227 // Store it in the context.
228 MemOperand target = ContextOperand(cp, var->index());
230 // Update the write barrier. This clobbers a3 and a0.
231 if (need_write_barrier) {
232 __ RecordWriteContextSlot(
233 cp, target.offset(), a0, a3, GetRAState(), kSaveFPRegs);
234 } else if (FLAG_debug_code) {
236 __ JumpIfInNewSpace(cp, a0, &done);
237 __ Abort(kExpectedNewSpaceObject);
242 Comment(";;; End allocate local context");
246 if (FLAG_trace && info()->IsOptimizing()) {
247 // We have not executed any compiled code yet, so cp still holds the
249 __ CallRuntime(Runtime::kTraceEnter, 0);
251 return !is_aborted();
255 void LCodeGen::GenerateOsrPrologue() {
256 // Generate the OSR entry prologue at the first unknown OSR value, or if there
257 // are none, at the OSR entrypoint instruction.
258 if (osr_pc_offset_ >= 0) return;
260 osr_pc_offset_ = masm()->pc_offset();
262 // Adjust the frame size, subsuming the unoptimized frame into the
264 int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
266 __ Subu(sp, sp, Operand(slots * kPointerSize));
270 void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
271 if (instr->IsCall()) {
272 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
274 if (!instr->IsLazyBailout() && !instr->IsGap()) {
275 safepoints_.BumpLastLazySafepointIndex();
280 bool LCodeGen::GenerateDeferredCode() {
281 DCHECK(is_generating());
282 if (deferred_.length() > 0) {
283 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
284 LDeferredCode* code = deferred_[i];
287 instructions_->at(code->instruction_index())->hydrogen_value();
288 RecordAndWritePosition(
289 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
291 Comment(";;; <@%d,#%d> "
292 "-------------------- Deferred %s --------------------",
293 code->instruction_index(),
294 code->instr()->hydrogen_value()->id(),
295 code->instr()->Mnemonic());
296 __ bind(code->entry());
297 if (NeedsDeferredFrame()) {
298 Comment(";;; Build frame");
299 DCHECK(!frame_is_built_);
300 DCHECK(info()->IsStub());
301 frame_is_built_ = true;
302 __ MultiPush(cp.bit() | fp.bit() | ra.bit());
303 __ li(scratch0(), Operand(Smi::FromInt(StackFrame::STUB)));
305 __ Addu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
306 Comment(";;; Deferred code");
309 if (NeedsDeferredFrame()) {
310 Comment(";;; Destroy frame");
311 DCHECK(frame_is_built_);
313 __ MultiPop(cp.bit() | fp.bit() | ra.bit());
314 frame_is_built_ = false;
316 __ jmp(code->exit());
319 // Deferred code is the last part of the instruction sequence. Mark
320 // the generated code as done unless we bailed out.
321 if (!is_aborted()) status_ = DONE;
322 return !is_aborted();
326 bool LCodeGen::GenerateJumpTable() {
327 if (jump_table_.length() > 0) {
328 Label needs_frame, call_deopt_entry;
330 Comment(";;; -------------------- Jump table --------------------");
331 Address base = jump_table_[0].address;
333 Register entry_offset = t9;
335 int length = jump_table_.length();
336 for (int i = 0; i < length; i++) {
337 Deoptimizer::JumpTableEntry* table_entry = &jump_table_[i];
338 __ bind(&table_entry->label);
340 DCHECK(table_entry->bailout_type == jump_table_[0].bailout_type);
341 Address entry = table_entry->address;
342 DeoptComment(table_entry->reason);
344 // Second-level deopt table entries are contiguous and small, so instead
345 // of loading the full, absolute address of each one, load an immediate
346 // offset which will be added to the base address later.
347 __ li(entry_offset, Operand(entry - base));
349 if (table_entry->needs_frame) {
350 DCHECK(!info()->saves_caller_doubles());
351 if (needs_frame.is_bound()) {
352 __ Branch(&needs_frame);
354 __ bind(&needs_frame);
355 Comment(";;; call deopt with frame");
356 __ MultiPush(cp.bit() | fp.bit() | ra.bit());
357 // This variant of deopt can only be used with stubs. Since we don't
358 // have a function pointer to install in the stack frame that we're
359 // building, install a special marker there instead.
360 DCHECK(info()->IsStub());
361 __ li(at, Operand(Smi::FromInt(StackFrame::STUB)));
364 Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
365 __ bind(&call_deopt_entry);
366 // Add the base address to the offset previously loaded in
368 __ Addu(entry_offset, entry_offset,
369 Operand(ExternalReference::ForDeoptEntry(base)));
370 __ Call(entry_offset);
373 // The last entry can fall through into `call_deopt_entry`, avoiding a
375 bool need_branch = ((i + 1) != length) || call_deopt_entry.is_bound();
377 if (need_branch) __ Branch(&call_deopt_entry);
381 if (!call_deopt_entry.is_bound()) {
382 Comment(";;; call deopt");
383 __ bind(&call_deopt_entry);
385 if (info()->saves_caller_doubles()) {
386 DCHECK(info()->IsStub());
387 RestoreCallerDoubles();
390 // Add the base address to the offset previously loaded in entry_offset.
391 __ Addu(entry_offset, entry_offset,
392 Operand(ExternalReference::ForDeoptEntry(base)));
393 __ Call(entry_offset);
396 __ RecordComment("]");
398 // The deoptimization jump table is the last part of the instruction
399 // sequence. Mark the generated code as done unless we bailed out.
400 if (!is_aborted()) status_ = DONE;
401 return !is_aborted();
405 bool LCodeGen::GenerateSafepointTable() {
407 safepoints_.Emit(masm(), GetStackSlotCount());
408 return !is_aborted();
412 Register LCodeGen::ToRegister(int index) const {
413 return Register::FromAllocationIndex(index);
417 DoubleRegister LCodeGen::ToDoubleRegister(int index) const {
418 return DoubleRegister::FromAllocationIndex(index);
422 Register LCodeGen::ToRegister(LOperand* op) const {
423 DCHECK(op->IsRegister());
424 return ToRegister(op->index());
428 Register LCodeGen::EmitLoadRegister(LOperand* op, Register scratch) {
429 if (op->IsRegister()) {
430 return ToRegister(op->index());
431 } else if (op->IsConstantOperand()) {
432 LConstantOperand* const_op = LConstantOperand::cast(op);
433 HConstant* constant = chunk_->LookupConstant(const_op);
434 Handle<Object> literal = constant->handle(isolate());
435 Representation r = chunk_->LookupLiteralRepresentation(const_op);
436 if (r.IsInteger32()) {
437 DCHECK(literal->IsNumber());
438 __ li(scratch, Operand(static_cast<int32_t>(literal->Number())));
439 } else if (r.IsSmi()) {
440 DCHECK(constant->HasSmiValue());
441 __ li(scratch, Operand(Smi::FromInt(constant->Integer32Value())));
442 } else if (r.IsDouble()) {
443 Abort(kEmitLoadRegisterUnsupportedDoubleImmediate);
445 DCHECK(r.IsSmiOrTagged());
446 __ li(scratch, literal);
449 } else if (op->IsStackSlot()) {
450 __ lw(scratch, ToMemOperand(op));
458 DoubleRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
459 DCHECK(op->IsDoubleRegister());
460 return ToDoubleRegister(op->index());
464 DoubleRegister LCodeGen::EmitLoadDoubleRegister(LOperand* op,
465 FloatRegister flt_scratch,
466 DoubleRegister dbl_scratch) {
467 if (op->IsDoubleRegister()) {
468 return ToDoubleRegister(op->index());
469 } else if (op->IsConstantOperand()) {
470 LConstantOperand* const_op = LConstantOperand::cast(op);
471 HConstant* constant = chunk_->LookupConstant(const_op);
472 Handle<Object> literal = constant->handle(isolate());
473 Representation r = chunk_->LookupLiteralRepresentation(const_op);
474 if (r.IsInteger32()) {
475 DCHECK(literal->IsNumber());
476 __ li(at, Operand(static_cast<int32_t>(literal->Number())));
477 __ mtc1(at, flt_scratch);
478 __ cvt_d_w(dbl_scratch, flt_scratch);
480 } else if (r.IsDouble()) {
481 Abort(kUnsupportedDoubleImmediate);
482 } else if (r.IsTagged()) {
483 Abort(kUnsupportedTaggedImmediate);
485 } else if (op->IsStackSlot()) {
486 MemOperand mem_op = ToMemOperand(op);
487 __ ldc1(dbl_scratch, mem_op);
495 Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
496 HConstant* constant = chunk_->LookupConstant(op);
497 DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
498 return constant->handle(isolate());
502 bool LCodeGen::IsInteger32(LConstantOperand* op) const {
503 return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
507 bool LCodeGen::IsSmi(LConstantOperand* op) const {
508 return chunk_->LookupLiteralRepresentation(op).IsSmi();
512 int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
513 return ToRepresentation(op, Representation::Integer32());
517 int32_t LCodeGen::ToRepresentation(LConstantOperand* op,
518 const Representation& r) const {
519 HConstant* constant = chunk_->LookupConstant(op);
520 int32_t value = constant->Integer32Value();
521 if (r.IsInteger32()) return value;
522 DCHECK(r.IsSmiOrTagged());
523 return reinterpret_cast<int32_t>(Smi::FromInt(value));
527 Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
528 HConstant* constant = chunk_->LookupConstant(op);
529 return Smi::FromInt(constant->Integer32Value());
533 double LCodeGen::ToDouble(LConstantOperand* op) const {
534 HConstant* constant = chunk_->LookupConstant(op);
535 DCHECK(constant->HasDoubleValue());
536 return constant->DoubleValue();
540 Operand LCodeGen::ToOperand(LOperand* op) {
541 if (op->IsConstantOperand()) {
542 LConstantOperand* const_op = LConstantOperand::cast(op);
543 HConstant* constant = chunk()->LookupConstant(const_op);
544 Representation r = chunk_->LookupLiteralRepresentation(const_op);
546 DCHECK(constant->HasSmiValue());
547 return Operand(Smi::FromInt(constant->Integer32Value()));
548 } else if (r.IsInteger32()) {
549 DCHECK(constant->HasInteger32Value());
550 return Operand(constant->Integer32Value());
551 } else if (r.IsDouble()) {
552 Abort(kToOperandUnsupportedDoubleImmediate);
554 DCHECK(r.IsTagged());
555 return Operand(constant->handle(isolate()));
556 } else if (op->IsRegister()) {
557 return Operand(ToRegister(op));
558 } else if (op->IsDoubleRegister()) {
559 Abort(kToOperandIsDoubleRegisterUnimplemented);
562 // Stack slots not implemented, use ToMemOperand instead.
568 static int ArgumentsOffsetWithoutFrame(int index) {
570 return -(index + 1) * kPointerSize;
574 MemOperand LCodeGen::ToMemOperand(LOperand* op) const {
575 DCHECK(!op->IsRegister());
576 DCHECK(!op->IsDoubleRegister());
577 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
578 if (NeedsEagerFrame()) {
579 return MemOperand(fp, StackSlotOffset(op->index()));
581 // Retrieve parameter without eager stack-frame relative to the
583 return MemOperand(sp, ArgumentsOffsetWithoutFrame(op->index()));
588 MemOperand LCodeGen::ToHighMemOperand(LOperand* op) const {
589 DCHECK(op->IsDoubleStackSlot());
590 if (NeedsEagerFrame()) {
591 return MemOperand(fp, StackSlotOffset(op->index()) + kPointerSize);
593 // Retrieve parameter without eager stack-frame relative to the
596 sp, ArgumentsOffsetWithoutFrame(op->index()) + kPointerSize);
601 void LCodeGen::WriteTranslation(LEnvironment* environment,
602 Translation* translation) {
603 if (environment == NULL) return;
605 // The translation includes one command per value in the environment.
606 int translation_size = environment->translation_size();
607 // The output frame height does not include the parameters.
608 int height = translation_size - environment->parameter_count();
610 WriteTranslation(environment->outer(), translation);
611 bool has_closure_id = !info()->closure().is_null() &&
612 !info()->closure().is_identical_to(environment->closure());
613 int closure_id = has_closure_id
614 ? DefineDeoptimizationLiteral(environment->closure())
615 : Translation::kSelfLiteralId;
617 switch (environment->frame_type()) {
619 translation->BeginJSFrame(environment->ast_id(), closure_id, height);
622 translation->BeginConstructStubFrame(closure_id, translation_size);
625 DCHECK(translation_size == 1);
627 translation->BeginGetterStubFrame(closure_id);
630 DCHECK(translation_size == 2);
632 translation->BeginSetterStubFrame(closure_id);
635 translation->BeginCompiledStubFrame();
637 case ARGUMENTS_ADAPTOR:
638 translation->BeginArgumentsAdaptorFrame(closure_id, translation_size);
642 int object_index = 0;
643 int dematerialized_index = 0;
644 for (int i = 0; i < translation_size; ++i) {
645 LOperand* value = environment->values()->at(i);
646 AddToTranslation(environment,
649 environment->HasTaggedValueAt(i),
650 environment->HasUint32ValueAt(i),
652 &dematerialized_index);
657 void LCodeGen::AddToTranslation(LEnvironment* environment,
658 Translation* translation,
662 int* object_index_pointer,
663 int* dematerialized_index_pointer) {
664 if (op == LEnvironment::materialization_marker()) {
665 int object_index = (*object_index_pointer)++;
666 if (environment->ObjectIsDuplicateAt(object_index)) {
667 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
668 translation->DuplicateObject(dupe_of);
671 int object_length = environment->ObjectLengthAt(object_index);
672 if (environment->ObjectIsArgumentsAt(object_index)) {
673 translation->BeginArgumentsObject(object_length);
675 translation->BeginCapturedObject(object_length);
677 int dematerialized_index = *dematerialized_index_pointer;
678 int env_offset = environment->translation_size() + dematerialized_index;
679 *dematerialized_index_pointer += object_length;
680 for (int i = 0; i < object_length; ++i) {
681 LOperand* value = environment->values()->at(env_offset + i);
682 AddToTranslation(environment,
685 environment->HasTaggedValueAt(env_offset + i),
686 environment->HasUint32ValueAt(env_offset + i),
687 object_index_pointer,
688 dematerialized_index_pointer);
693 if (op->IsStackSlot()) {
695 translation->StoreStackSlot(op->index());
696 } else if (is_uint32) {
697 translation->StoreUint32StackSlot(op->index());
699 translation->StoreInt32StackSlot(op->index());
701 } else if (op->IsDoubleStackSlot()) {
702 translation->StoreDoubleStackSlot(op->index());
703 } else if (op->IsRegister()) {
704 Register reg = ToRegister(op);
706 translation->StoreRegister(reg);
707 } else if (is_uint32) {
708 translation->StoreUint32Register(reg);
710 translation->StoreInt32Register(reg);
712 } else if (op->IsDoubleRegister()) {
713 DoubleRegister reg = ToDoubleRegister(op);
714 translation->StoreDoubleRegister(reg);
715 } else if (op->IsConstantOperand()) {
716 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
717 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
718 translation->StoreLiteral(src_index);
725 void LCodeGen::CallCode(Handle<Code> code,
726 RelocInfo::Mode mode,
727 LInstruction* instr) {
728 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT);
732 void LCodeGen::CallCodeGeneric(Handle<Code> code,
733 RelocInfo::Mode mode,
735 SafepointMode safepoint_mode) {
736 DCHECK(instr != NULL);
738 RecordSafepointWithLazyDeopt(instr, safepoint_mode);
742 void LCodeGen::CallRuntime(const Runtime::Function* function,
745 SaveFPRegsMode save_doubles) {
746 DCHECK(instr != NULL);
748 __ CallRuntime(function, num_arguments, save_doubles);
750 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
754 void LCodeGen::LoadContextFromDeferred(LOperand* context) {
755 if (context->IsRegister()) {
756 __ Move(cp, ToRegister(context));
757 } else if (context->IsStackSlot()) {
758 __ lw(cp, ToMemOperand(context));
759 } else if (context->IsConstantOperand()) {
760 HConstant* constant =
761 chunk_->LookupConstant(LConstantOperand::cast(context));
762 __ li(cp, Handle<Object>::cast(constant->handle(isolate())));
769 void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
773 LoadContextFromDeferred(context);
774 __ CallRuntimeSaveDoubles(id);
775 RecordSafepointWithRegisters(
776 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
780 void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
781 Safepoint::DeoptMode mode) {
782 environment->set_has_been_used();
783 if (!environment->HasBeenRegistered()) {
784 // Physical stack frame layout:
785 // -x ............. -4 0 ..................................... y
786 // [incoming arguments] [spill slots] [pushed outgoing arguments]
788 // Layout of the environment:
789 // 0 ..................................................... size-1
790 // [parameters] [locals] [expression stack including arguments]
792 // Layout of the translation:
793 // 0 ........................................................ size - 1 + 4
794 // [expression stack including arguments] [locals] [4 words] [parameters]
795 // |>------------ translation_size ------------<|
798 int jsframe_count = 0;
799 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
801 if (e->frame_type() == JS_FUNCTION) {
805 Translation translation(&translations_, frame_count, jsframe_count, zone());
806 WriteTranslation(environment, &translation);
807 int deoptimization_index = deoptimizations_.length();
808 int pc_offset = masm()->pc_offset();
809 environment->Register(deoptimization_index,
811 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
812 deoptimizations_.Add(environment, zone());
817 void LCodeGen::DeoptimizeIf(Condition condition, LInstruction* instr,
818 Deoptimizer::BailoutType bailout_type,
819 const char* detail, Register src1,
820 const Operand& src2) {
821 LEnvironment* environment = instr->environment();
822 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
823 DCHECK(environment->HasBeenRegistered());
824 int id = environment->deoptimization_index();
825 DCHECK(info()->IsOptimizing() || info()->IsStub());
827 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
829 Abort(kBailoutWasNotPrepared);
833 if (FLAG_deopt_every_n_times != 0 && !info()->IsStub()) {
834 Register scratch = scratch0();
835 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
837 __ Push(a1, scratch);
838 __ li(scratch, Operand(count));
839 __ lw(a1, MemOperand(scratch));
840 __ Subu(a1, a1, Operand(1));
841 __ Branch(&no_deopt, ne, a1, Operand(zero_reg));
842 __ li(a1, Operand(FLAG_deopt_every_n_times));
843 __ sw(a1, MemOperand(scratch));
846 __ Call(entry, RelocInfo::RUNTIME_ENTRY);
848 __ sw(a1, MemOperand(scratch));
852 if (info()->ShouldTrapOnDeopt()) {
854 if (condition != al) {
855 __ Branch(&skip, NegateCondition(condition), src1, src2);
857 __ stop("trap_on_deopt");
861 Deoptimizer::Reason reason(instr->hydrogen_value()->position().raw(),
862 instr->Mnemonic(), detail);
863 DCHECK(info()->IsStub() || frame_is_built_);
864 // Go through jump table if we need to handle condition, build frame, or
865 // restore caller doubles.
866 if (condition == al && frame_is_built_ &&
867 !info()->saves_caller_doubles()) {
868 DeoptComment(reason);
869 __ Call(entry, RelocInfo::RUNTIME_ENTRY, condition, src1, src2);
871 Deoptimizer::JumpTableEntry table_entry(entry, reason, bailout_type,
873 // We often have several deopts to the same entry, reuse the last
874 // jump entry if this is the case.
875 if (jump_table_.is_empty() ||
876 !table_entry.IsEquivalentTo(jump_table_.last())) {
877 jump_table_.Add(table_entry, zone());
879 __ Branch(&jump_table_.last().label, condition, src1, src2);
884 void LCodeGen::DeoptimizeIf(Condition condition, LInstruction* instr,
885 const char* detail, Register src1,
886 const Operand& src2) {
887 Deoptimizer::BailoutType bailout_type = info()->IsStub()
889 : Deoptimizer::EAGER;
890 DeoptimizeIf(condition, instr, bailout_type, detail, src1, src2);
894 void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
895 int length = deoptimizations_.length();
896 if (length == 0) return;
897 Handle<DeoptimizationInputData> data =
898 DeoptimizationInputData::New(isolate(), length, TENURED);
900 Handle<ByteArray> translations =
901 translations_.CreateByteArray(isolate()->factory());
902 data->SetTranslationByteArray(*translations);
903 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
904 data->SetOptimizationId(Smi::FromInt(info_->optimization_id()));
905 if (info_->IsOptimizing()) {
906 // Reference to shared function info does not change between phases.
907 AllowDeferredHandleDereference allow_handle_dereference;
908 data->SetSharedFunctionInfo(*info_->shared_info());
910 data->SetSharedFunctionInfo(Smi::FromInt(0));
913 Handle<FixedArray> literals =
914 factory()->NewFixedArray(deoptimization_literals_.length(), TENURED);
915 { AllowDeferredHandleDereference copy_handles;
916 for (int i = 0; i < deoptimization_literals_.length(); i++) {
917 literals->set(i, *deoptimization_literals_[i]);
919 data->SetLiteralArray(*literals);
922 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id().ToInt()));
923 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
925 // Populate the deoptimization entries.
926 for (int i = 0; i < length; i++) {
927 LEnvironment* env = deoptimizations_[i];
928 data->SetAstId(i, env->ast_id());
929 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
930 data->SetArgumentsStackHeight(i,
931 Smi::FromInt(env->arguments_stack_height()));
932 data->SetPc(i, Smi::FromInt(env->pc_offset()));
934 code->set_deoptimization_data(*data);
938 int LCodeGen::DefineDeoptimizationLiteral(Handle<Object> literal) {
939 int result = deoptimization_literals_.length();
940 for (int i = 0; i < deoptimization_literals_.length(); ++i) {
941 if (deoptimization_literals_[i].is_identical_to(literal)) return i;
943 deoptimization_literals_.Add(literal, zone());
948 void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
949 DCHECK(deoptimization_literals_.length() == 0);
951 const ZoneList<Handle<JSFunction> >* inlined_closures =
952 chunk()->inlined_closures();
954 for (int i = 0, length = inlined_closures->length();
957 DefineDeoptimizationLiteral(inlined_closures->at(i));
960 inlined_function_count_ = deoptimization_literals_.length();
964 void LCodeGen::RecordSafepointWithLazyDeopt(
965 LInstruction* instr, SafepointMode safepoint_mode) {
966 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
967 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
969 DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
970 RecordSafepointWithRegisters(
971 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
976 void LCodeGen::RecordSafepoint(
977 LPointerMap* pointers,
978 Safepoint::Kind kind,
980 Safepoint::DeoptMode deopt_mode) {
981 DCHECK(expected_safepoint_kind_ == kind);
983 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
984 Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
985 kind, arguments, deopt_mode);
986 for (int i = 0; i < operands->length(); i++) {
987 LOperand* pointer = operands->at(i);
988 if (pointer->IsStackSlot()) {
989 safepoint.DefinePointerSlot(pointer->index(), zone());
990 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
991 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
997 void LCodeGen::RecordSafepoint(LPointerMap* pointers,
998 Safepoint::DeoptMode deopt_mode) {
999 RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
1003 void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
1004 LPointerMap empty_pointers(zone());
1005 RecordSafepoint(&empty_pointers, deopt_mode);
1009 void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
1011 Safepoint::DeoptMode deopt_mode) {
1013 pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
1017 void LCodeGen::RecordAndWritePosition(int position) {
1018 if (position == RelocInfo::kNoPosition) return;
1019 masm()->positions_recorder()->RecordPosition(position);
1020 masm()->positions_recorder()->WriteRecordedPositions();
1024 static const char* LabelType(LLabel* label) {
1025 if (label->is_loop_header()) return " (loop header)";
1026 if (label->is_osr_entry()) return " (OSR entry)";
1031 void LCodeGen::DoLabel(LLabel* label) {
1032 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
1033 current_instruction_,
1034 label->hydrogen_value()->id(),
1037 __ bind(label->label());
1038 current_block_ = label->block_id();
1043 void LCodeGen::DoParallelMove(LParallelMove* move) {
1044 resolver_.Resolve(move);
1048 void LCodeGen::DoGap(LGap* gap) {
1049 for (int i = LGap::FIRST_INNER_POSITION;
1050 i <= LGap::LAST_INNER_POSITION;
1052 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
1053 LParallelMove* move = gap->GetParallelMove(inner_pos);
1054 if (move != NULL) DoParallelMove(move);
1059 void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
1064 void LCodeGen::DoParameter(LParameter* instr) {
1069 void LCodeGen::DoCallStub(LCallStub* instr) {
1070 DCHECK(ToRegister(instr->context()).is(cp));
1071 DCHECK(ToRegister(instr->result()).is(v0));
1072 switch (instr->hydrogen()->major_key()) {
1073 case CodeStub::RegExpExec: {
1074 RegExpExecStub stub(isolate());
1075 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1078 case CodeStub::SubString: {
1079 SubStringStub stub(isolate());
1080 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1083 case CodeStub::StringCompare: {
1084 StringCompareStub stub(isolate());
1085 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1094 void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
1095 GenerateOsrPrologue();
1099 void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
1100 Register dividend = ToRegister(instr->dividend());
1101 int32_t divisor = instr->divisor();
1102 DCHECK(dividend.is(ToRegister(instr->result())));
1104 // Theoretically, a variation of the branch-free code for integer division by
1105 // a power of 2 (calculating the remainder via an additional multiplication
1106 // (which gets simplified to an 'and') and subtraction) should be faster, and
1107 // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
1108 // indicate that positive dividends are heavily favored, so the branching
1109 // version performs better.
1110 HMod* hmod = instr->hydrogen();
1111 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1112 Label dividend_is_not_negative, done;
1114 if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
1115 __ Branch(÷nd_is_not_negative, ge, dividend, Operand(zero_reg));
1116 // Note: The code below even works when right contains kMinInt.
1117 __ subu(dividend, zero_reg, dividend);
1118 __ And(dividend, dividend, Operand(mask));
1119 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1120 DeoptimizeIf(eq, instr, "minus zero", dividend, Operand(zero_reg));
1122 __ Branch(USE_DELAY_SLOT, &done);
1123 __ subu(dividend, zero_reg, dividend);
1126 __ bind(÷nd_is_not_negative);
1127 __ And(dividend, dividend, Operand(mask));
1132 void LCodeGen::DoModByConstI(LModByConstI* instr) {
1133 Register dividend = ToRegister(instr->dividend());
1134 int32_t divisor = instr->divisor();
1135 Register result = ToRegister(instr->result());
1136 DCHECK(!dividend.is(result));
1139 DeoptimizeIf(al, instr);
1143 __ TruncatingDiv(result, dividend, Abs(divisor));
1144 __ Mul(result, result, Operand(Abs(divisor)));
1145 __ Subu(result, dividend, Operand(result));
1147 // Check for negative zero.
1148 HMod* hmod = instr->hydrogen();
1149 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1150 Label remainder_not_zero;
1151 __ Branch(&remainder_not_zero, ne, result, Operand(zero_reg));
1152 DeoptimizeIf(lt, instr, "minus zero", dividend, Operand(zero_reg));
1153 __ bind(&remainder_not_zero);
1158 void LCodeGen::DoModI(LModI* instr) {
1159 HMod* hmod = instr->hydrogen();
1160 const Register left_reg = ToRegister(instr->left());
1161 const Register right_reg = ToRegister(instr->right());
1162 const Register result_reg = ToRegister(instr->result());
1164 // div runs in the background while we check for special cases.
1165 __ Mod(result_reg, left_reg, right_reg);
1168 // Check for x % 0, we have to deopt in this case because we can't return a
1170 if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
1171 DeoptimizeIf(eq, instr, "division by zero", right_reg, Operand(zero_reg));
1174 // Check for kMinInt % -1, div will return kMinInt, which is not what we
1175 // want. We have to deopt if we care about -0, because we can't return that.
1176 if (hmod->CheckFlag(HValue::kCanOverflow)) {
1177 Label no_overflow_possible;
1178 __ Branch(&no_overflow_possible, ne, left_reg, Operand(kMinInt));
1179 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1180 DeoptimizeIf(eq, instr, "minus zero", right_reg, Operand(-1));
1182 __ Branch(&no_overflow_possible, ne, right_reg, Operand(-1));
1183 __ Branch(USE_DELAY_SLOT, &done);
1184 __ mov(result_reg, zero_reg);
1186 __ bind(&no_overflow_possible);
1189 // If we care about -0, test if the dividend is <0 and the result is 0.
1190 __ Branch(&done, ge, left_reg, Operand(zero_reg));
1191 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1192 DeoptimizeIf(eq, instr, "minus zero", result_reg, Operand(zero_reg));
1198 void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
1199 Register dividend = ToRegister(instr->dividend());
1200 int32_t divisor = instr->divisor();
1201 Register result = ToRegister(instr->result());
1202 DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
1203 DCHECK(!result.is(dividend));
1205 // Check for (0 / -x) that will produce negative zero.
1206 HDiv* hdiv = instr->hydrogen();
1207 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1208 DeoptimizeIf(eq, instr, "minus zero", dividend, Operand(zero_reg));
1210 // Check for (kMinInt / -1).
1211 if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
1212 DeoptimizeIf(eq, instr, "overflow", dividend, Operand(kMinInt));
1214 // Deoptimize if remainder will not be 0.
1215 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1216 divisor != 1 && divisor != -1) {
1217 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1218 __ And(at, dividend, Operand(mask));
1219 DeoptimizeIf(ne, instr, "lost precision", at, Operand(zero_reg));
1222 if (divisor == -1) { // Nice shortcut, not needed for correctness.
1223 __ Subu(result, zero_reg, dividend);
1226 uint16_t shift = WhichPowerOf2Abs(divisor);
1228 __ Move(result, dividend);
1229 } else if (shift == 1) {
1230 __ srl(result, dividend, 31);
1231 __ Addu(result, dividend, Operand(result));
1233 __ sra(result, dividend, 31);
1234 __ srl(result, result, 32 - shift);
1235 __ Addu(result, dividend, Operand(result));
1237 if (shift > 0) __ sra(result, result, shift);
1238 if (divisor < 0) __ Subu(result, zero_reg, result);
1242 void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
1243 Register dividend = ToRegister(instr->dividend());
1244 int32_t divisor = instr->divisor();
1245 Register result = ToRegister(instr->result());
1246 DCHECK(!dividend.is(result));
1249 DeoptimizeIf(al, instr);
1253 // Check for (0 / -x) that will produce negative zero.
1254 HDiv* hdiv = instr->hydrogen();
1255 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1256 DeoptimizeIf(eq, instr, "minus zero", dividend, Operand(zero_reg));
1259 __ TruncatingDiv(result, dividend, Abs(divisor));
1260 if (divisor < 0) __ Subu(result, zero_reg, result);
1262 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1263 __ Mul(scratch0(), result, Operand(divisor));
1264 __ Subu(scratch0(), scratch0(), dividend);
1265 DeoptimizeIf(ne, instr, "lost precision", scratch0(), Operand(zero_reg));
1270 // TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
1271 void LCodeGen::DoDivI(LDivI* instr) {
1272 HBinaryOperation* hdiv = instr->hydrogen();
1273 Register dividend = ToRegister(instr->dividend());
1274 Register divisor = ToRegister(instr->divisor());
1275 const Register result = ToRegister(instr->result());
1276 Register remainder = ToRegister(instr->temp());
1278 // On MIPS div is asynchronous - it will run in the background while we
1279 // check for special cases.
1280 __ Div(remainder, result, dividend, divisor);
1283 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1284 DeoptimizeIf(eq, instr, "division by zero", divisor, Operand(zero_reg));
1287 // Check for (0 / -x) that will produce negative zero.
1288 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1289 Label left_not_zero;
1290 __ Branch(&left_not_zero, ne, dividend, Operand(zero_reg));
1291 DeoptimizeIf(lt, instr, "minus zero", divisor, Operand(zero_reg));
1292 __ bind(&left_not_zero);
1295 // Check for (kMinInt / -1).
1296 if (hdiv->CheckFlag(HValue::kCanOverflow) &&
1297 !hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1298 Label left_not_min_int;
1299 __ Branch(&left_not_min_int, ne, dividend, Operand(kMinInt));
1300 DeoptimizeIf(eq, instr, "overflow", divisor, Operand(-1));
1301 __ bind(&left_not_min_int);
1304 if (!hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1305 DeoptimizeIf(ne, instr, "lost precision", remainder, Operand(zero_reg));
1310 void LCodeGen::DoMultiplyAddD(LMultiplyAddD* instr) {
1311 DoubleRegister addend = ToDoubleRegister(instr->addend());
1312 DoubleRegister multiplier = ToDoubleRegister(instr->multiplier());
1313 DoubleRegister multiplicand = ToDoubleRegister(instr->multiplicand());
1315 // This is computed in-place.
1316 DCHECK(addend.is(ToDoubleRegister(instr->result())));
1318 __ madd_d(addend, addend, multiplier, multiplicand);
1322 void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
1323 Register dividend = ToRegister(instr->dividend());
1324 Register result = ToRegister(instr->result());
1325 int32_t divisor = instr->divisor();
1326 Register scratch = result.is(dividend) ? scratch0() : dividend;
1327 DCHECK(!result.is(dividend) || !scratch.is(dividend));
1329 // If the divisor is 1, return the dividend.
1331 __ Move(result, dividend);
1335 // If the divisor is positive, things are easy: There can be no deopts and we
1336 // can simply do an arithmetic right shift.
1337 uint16_t shift = WhichPowerOf2Abs(divisor);
1339 __ sra(result, dividend, shift);
1343 // If the divisor is negative, we have to negate and handle edge cases.
1345 // dividend can be the same register as result so save the value of it
1346 // for checking overflow.
1347 __ Move(scratch, dividend);
1349 __ Subu(result, zero_reg, dividend);
1350 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1351 DeoptimizeIf(eq, instr, "minus zero", result, Operand(zero_reg));
1354 // Dividing by -1 is basically negation, unless we overflow.
1355 __ Xor(scratch, scratch, result);
1356 if (divisor == -1) {
1357 if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1358 DeoptimizeIf(ge, instr, "overflow", scratch, Operand(zero_reg));
1363 // If the negation could not overflow, simply shifting is OK.
1364 if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1365 __ sra(result, result, shift);
1369 Label no_overflow, done;
1370 __ Branch(&no_overflow, lt, scratch, Operand(zero_reg));
1371 __ li(result, Operand(kMinInt / divisor));
1373 __ bind(&no_overflow);
1374 __ sra(result, result, shift);
1379 void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
1380 Register dividend = ToRegister(instr->dividend());
1381 int32_t divisor = instr->divisor();
1382 Register result = ToRegister(instr->result());
1383 DCHECK(!dividend.is(result));
1386 DeoptimizeIf(al, instr);
1390 // Check for (0 / -x) that will produce negative zero.
1391 HMathFloorOfDiv* hdiv = instr->hydrogen();
1392 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1393 DeoptimizeIf(eq, instr, "minus zero", dividend, Operand(zero_reg));
1396 // Easy case: We need no dynamic check for the dividend and the flooring
1397 // division is the same as the truncating division.
1398 if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
1399 (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
1400 __ TruncatingDiv(result, dividend, Abs(divisor));
1401 if (divisor < 0) __ Subu(result, zero_reg, result);
1405 // In the general case we may need to adjust before and after the truncating
1406 // division to get a flooring division.
1407 Register temp = ToRegister(instr->temp());
1408 DCHECK(!temp.is(dividend) && !temp.is(result));
1409 Label needs_adjustment, done;
1410 __ Branch(&needs_adjustment, divisor > 0 ? lt : gt,
1411 dividend, Operand(zero_reg));
1412 __ TruncatingDiv(result, dividend, Abs(divisor));
1413 if (divisor < 0) __ Subu(result, zero_reg, result);
1415 __ bind(&needs_adjustment);
1416 __ Addu(temp, dividend, Operand(divisor > 0 ? 1 : -1));
1417 __ TruncatingDiv(result, temp, Abs(divisor));
1418 if (divisor < 0) __ Subu(result, zero_reg, result);
1419 __ Subu(result, result, Operand(1));
1424 // TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
1425 void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
1426 HBinaryOperation* hdiv = instr->hydrogen();
1427 Register dividend = ToRegister(instr->dividend());
1428 Register divisor = ToRegister(instr->divisor());
1429 const Register result = ToRegister(instr->result());
1430 Register remainder = scratch0();
1431 // On MIPS div is asynchronous - it will run in the background while we
1432 // check for special cases.
1433 __ Div(remainder, result, dividend, divisor);
1436 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1437 DeoptimizeIf(eq, instr, "division by zero", divisor, Operand(zero_reg));
1440 // Check for (0 / -x) that will produce negative zero.
1441 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1442 Label left_not_zero;
1443 __ Branch(&left_not_zero, ne, dividend, Operand(zero_reg));
1444 DeoptimizeIf(lt, instr, "minus zero", divisor, Operand(zero_reg));
1445 __ bind(&left_not_zero);
1448 // Check for (kMinInt / -1).
1449 if (hdiv->CheckFlag(HValue::kCanOverflow) &&
1450 !hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1451 Label left_not_min_int;
1452 __ Branch(&left_not_min_int, ne, dividend, Operand(kMinInt));
1453 DeoptimizeIf(eq, instr, "overflow", divisor, Operand(-1));
1454 __ bind(&left_not_min_int);
1457 // We performed a truncating division. Correct the result if necessary.
1459 __ Branch(&done, eq, remainder, Operand(zero_reg), USE_DELAY_SLOT);
1460 __ Xor(remainder, remainder, Operand(divisor));
1461 __ Branch(&done, ge, remainder, Operand(zero_reg));
1462 __ Subu(result, result, Operand(1));
1467 void LCodeGen::DoMulI(LMulI* instr) {
1468 Register scratch = scratch0();
1469 Register result = ToRegister(instr->result());
1470 // Note that result may alias left.
1471 Register left = ToRegister(instr->left());
1472 LOperand* right_op = instr->right();
1474 bool bailout_on_minus_zero =
1475 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
1476 bool overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1478 if (right_op->IsConstantOperand()) {
1479 int32_t constant = ToInteger32(LConstantOperand::cast(right_op));
1481 if (bailout_on_minus_zero && (constant < 0)) {
1482 // The case of a null constant will be handled separately.
1483 // If constant is negative and left is null, the result should be -0.
1484 DeoptimizeIf(eq, instr, "minus zero", left, Operand(zero_reg));
1490 __ SubuAndCheckForOverflow(result, zero_reg, left, scratch);
1491 DeoptimizeIf(lt, instr, "overflow", scratch, Operand(zero_reg));
1493 __ Subu(result, zero_reg, left);
1497 if (bailout_on_minus_zero) {
1498 // If left is strictly negative and the constant is null, the
1499 // result is -0. Deoptimize if required, otherwise return 0.
1500 DeoptimizeIf(lt, instr, "minus zero", left, Operand(zero_reg));
1502 __ mov(result, zero_reg);
1506 __ Move(result, left);
1509 // Multiplying by powers of two and powers of two plus or minus
1510 // one can be done faster with shifted operands.
1511 // For other constants we emit standard code.
1512 int32_t mask = constant >> 31;
1513 uint32_t constant_abs = (constant + mask) ^ mask;
1515 if (base::bits::IsPowerOfTwo32(constant_abs)) {
1516 int32_t shift = WhichPowerOf2(constant_abs);
1517 __ sll(result, left, shift);
1518 // Correct the sign of the result if the constant is negative.
1519 if (constant < 0) __ Subu(result, zero_reg, result);
1520 } else if (base::bits::IsPowerOfTwo32(constant_abs - 1)) {
1521 int32_t shift = WhichPowerOf2(constant_abs - 1);
1522 __ sll(scratch, left, shift);
1523 __ Addu(result, scratch, left);
1524 // Correct the sign of the result if the constant is negative.
1525 if (constant < 0) __ Subu(result, zero_reg, result);
1526 } else if (base::bits::IsPowerOfTwo32(constant_abs + 1)) {
1527 int32_t shift = WhichPowerOf2(constant_abs + 1);
1528 __ sll(scratch, left, shift);
1529 __ Subu(result, scratch, left);
1530 // Correct the sign of the result if the constant is negative.
1531 if (constant < 0) __ Subu(result, zero_reg, result);
1533 // Generate standard code.
1534 __ li(at, constant);
1535 __ Mul(result, left, at);
1540 DCHECK(right_op->IsRegister());
1541 Register right = ToRegister(right_op);
1544 // hi:lo = left * right.
1545 if (instr->hydrogen()->representation().IsSmi()) {
1546 __ SmiUntag(result, left);
1547 __ Mul(scratch, result, result, right);
1549 __ Mul(scratch, result, left, right);
1551 __ sra(at, result, 31);
1552 DeoptimizeIf(ne, instr, "overflow", scratch, Operand(at));
1554 if (instr->hydrogen()->representation().IsSmi()) {
1555 __ SmiUntag(result, left);
1556 __ Mul(result, result, right);
1558 __ Mul(result, left, right);
1562 if (bailout_on_minus_zero) {
1564 __ Xor(at, left, right);
1565 __ Branch(&done, ge, at, Operand(zero_reg));
1566 // Bail out if the result is minus zero.
1567 DeoptimizeIf(eq, instr, "minus zero", result, Operand(zero_reg));
1574 void LCodeGen::DoBitI(LBitI* instr) {
1575 LOperand* left_op = instr->left();
1576 LOperand* right_op = instr->right();
1577 DCHECK(left_op->IsRegister());
1578 Register left = ToRegister(left_op);
1579 Register result = ToRegister(instr->result());
1580 Operand right(no_reg);
1582 if (right_op->IsStackSlot()) {
1583 right = Operand(EmitLoadRegister(right_op, at));
1585 DCHECK(right_op->IsRegister() || right_op->IsConstantOperand());
1586 right = ToOperand(right_op);
1589 switch (instr->op()) {
1590 case Token::BIT_AND:
1591 __ And(result, left, right);
1594 __ Or(result, left, right);
1596 case Token::BIT_XOR:
1597 if (right_op->IsConstantOperand() && right.immediate() == int32_t(~0)) {
1598 __ Nor(result, zero_reg, left);
1600 __ Xor(result, left, right);
1610 void LCodeGen::DoShiftI(LShiftI* instr) {
1611 // Both 'left' and 'right' are "used at start" (see LCodeGen::DoShift), so
1612 // result may alias either of them.
1613 LOperand* right_op = instr->right();
1614 Register left = ToRegister(instr->left());
1615 Register result = ToRegister(instr->result());
1616 Register scratch = scratch0();
1618 if (right_op->IsRegister()) {
1619 // No need to mask the right operand on MIPS, it is built into the variable
1620 // shift instructions.
1621 switch (instr->op()) {
1623 __ Ror(result, left, Operand(ToRegister(right_op)));
1626 __ srav(result, left, ToRegister(right_op));
1629 __ srlv(result, left, ToRegister(right_op));
1630 if (instr->can_deopt()) {
1631 DeoptimizeIf(lt, instr, "negative value", result, Operand(zero_reg));
1635 __ sllv(result, left, ToRegister(right_op));
1642 // Mask the right_op operand.
1643 int value = ToInteger32(LConstantOperand::cast(right_op));
1644 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1645 switch (instr->op()) {
1647 if (shift_count != 0) {
1648 __ Ror(result, left, Operand(shift_count));
1650 __ Move(result, left);
1654 if (shift_count != 0) {
1655 __ sra(result, left, shift_count);
1657 __ Move(result, left);
1661 if (shift_count != 0) {
1662 __ srl(result, left, shift_count);
1664 if (instr->can_deopt()) {
1665 __ And(at, left, Operand(0x80000000));
1666 DeoptimizeIf(ne, instr, "negative value", at, Operand(zero_reg));
1668 __ Move(result, left);
1672 if (shift_count != 0) {
1673 if (instr->hydrogen_value()->representation().IsSmi() &&
1674 instr->can_deopt()) {
1675 if (shift_count != 1) {
1676 __ sll(result, left, shift_count - 1);
1677 __ SmiTagCheckOverflow(result, result, scratch);
1679 __ SmiTagCheckOverflow(result, left, scratch);
1681 DeoptimizeIf(lt, instr, "overflow", scratch, Operand(zero_reg));
1683 __ sll(result, left, shift_count);
1686 __ Move(result, left);
1697 void LCodeGen::DoSubI(LSubI* instr) {
1698 LOperand* left = instr->left();
1699 LOperand* right = instr->right();
1700 LOperand* result = instr->result();
1701 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1703 if (!can_overflow) {
1704 if (right->IsStackSlot()) {
1705 Register right_reg = EmitLoadRegister(right, at);
1706 __ Subu(ToRegister(result), ToRegister(left), Operand(right_reg));
1708 DCHECK(right->IsRegister() || right->IsConstantOperand());
1709 __ Subu(ToRegister(result), ToRegister(left), ToOperand(right));
1711 } else { // can_overflow.
1712 Register overflow = scratch0();
1713 Register scratch = scratch1();
1714 if (right->IsStackSlot() || right->IsConstantOperand()) {
1715 Register right_reg = EmitLoadRegister(right, scratch);
1716 __ SubuAndCheckForOverflow(ToRegister(result),
1719 overflow); // Reg at also used as scratch.
1721 DCHECK(right->IsRegister());
1722 // Due to overflow check macros not supporting constant operands,
1723 // handling the IsConstantOperand case was moved to prev if clause.
1724 __ SubuAndCheckForOverflow(ToRegister(result),
1727 overflow); // Reg at also used as scratch.
1729 DeoptimizeIf(lt, instr, "overflow", overflow, Operand(zero_reg));
1734 void LCodeGen::DoConstantI(LConstantI* instr) {
1735 __ li(ToRegister(instr->result()), Operand(instr->value()));
1739 void LCodeGen::DoConstantS(LConstantS* instr) {
1740 __ li(ToRegister(instr->result()), Operand(instr->value()));
1744 void LCodeGen::DoConstantD(LConstantD* instr) {
1745 DCHECK(instr->result()->IsDoubleRegister());
1746 DoubleRegister result = ToDoubleRegister(instr->result());
1747 double v = instr->value();
1752 void LCodeGen::DoConstantE(LConstantE* instr) {
1753 __ li(ToRegister(instr->result()), Operand(instr->value()));
1757 void LCodeGen::DoConstantT(LConstantT* instr) {
1758 Handle<Object> object = instr->value(isolate());
1759 AllowDeferredHandleDereference smi_check;
1760 __ li(ToRegister(instr->result()), object);
1764 void LCodeGen::DoMapEnumLength(LMapEnumLength* instr) {
1765 Register result = ToRegister(instr->result());
1766 Register map = ToRegister(instr->value());
1767 __ EnumLength(result, map);
1771 void LCodeGen::DoDateField(LDateField* instr) {
1772 Register object = ToRegister(instr->date());
1773 Register result = ToRegister(instr->result());
1774 Register scratch = ToRegister(instr->temp());
1775 Smi* index = instr->index();
1776 Label runtime, done;
1777 DCHECK(object.is(a0));
1778 DCHECK(result.is(v0));
1779 DCHECK(!scratch.is(scratch0()));
1780 DCHECK(!scratch.is(object));
1782 __ SmiTst(object, at);
1783 DeoptimizeIf(eq, instr, "Smi", at, Operand(zero_reg));
1784 __ GetObjectType(object, scratch, scratch);
1785 DeoptimizeIf(ne, instr, "not a date object", scratch, Operand(JS_DATE_TYPE));
1787 if (index->value() == 0) {
1788 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset));
1790 if (index->value() < JSDate::kFirstUncachedField) {
1791 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
1792 __ li(scratch, Operand(stamp));
1793 __ lw(scratch, MemOperand(scratch));
1794 __ lw(scratch0(), FieldMemOperand(object, JSDate::kCacheStampOffset));
1795 __ Branch(&runtime, ne, scratch, Operand(scratch0()));
1796 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset +
1797 kPointerSize * index->value()));
1801 __ PrepareCallCFunction(2, scratch);
1802 __ li(a1, Operand(index));
1803 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
1809 MemOperand LCodeGen::BuildSeqStringOperand(Register string,
1811 String::Encoding encoding) {
1812 if (index->IsConstantOperand()) {
1813 int offset = ToInteger32(LConstantOperand::cast(index));
1814 if (encoding == String::TWO_BYTE_ENCODING) {
1815 offset *= kUC16Size;
1817 STATIC_ASSERT(kCharSize == 1);
1818 return FieldMemOperand(string, SeqString::kHeaderSize + offset);
1820 Register scratch = scratch0();
1821 DCHECK(!scratch.is(string));
1822 DCHECK(!scratch.is(ToRegister(index)));
1823 if (encoding == String::ONE_BYTE_ENCODING) {
1824 __ Addu(scratch, string, ToRegister(index));
1826 STATIC_ASSERT(kUC16Size == 2);
1827 __ sll(scratch, ToRegister(index), 1);
1828 __ Addu(scratch, string, scratch);
1830 return FieldMemOperand(scratch, SeqString::kHeaderSize);
1834 void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
1835 String::Encoding encoding = instr->hydrogen()->encoding();
1836 Register string = ToRegister(instr->string());
1837 Register result = ToRegister(instr->result());
1839 if (FLAG_debug_code) {
1840 Register scratch = scratch0();
1841 __ lw(scratch, FieldMemOperand(string, HeapObject::kMapOffset));
1842 __ lbu(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1844 __ And(scratch, scratch,
1845 Operand(kStringRepresentationMask | kStringEncodingMask));
1846 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1847 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1848 __ Subu(at, scratch, Operand(encoding == String::ONE_BYTE_ENCODING
1849 ? one_byte_seq_type : two_byte_seq_type));
1850 __ Check(eq, kUnexpectedStringType, at, Operand(zero_reg));
1853 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1854 if (encoding == String::ONE_BYTE_ENCODING) {
1855 __ lbu(result, operand);
1857 __ lhu(result, operand);
1862 void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
1863 String::Encoding encoding = instr->hydrogen()->encoding();
1864 Register string = ToRegister(instr->string());
1865 Register value = ToRegister(instr->value());
1867 if (FLAG_debug_code) {
1868 Register scratch = scratch0();
1869 Register index = ToRegister(instr->index());
1870 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1871 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1873 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
1874 ? one_byte_seq_type : two_byte_seq_type;
1875 __ EmitSeqStringSetCharCheck(string, index, value, scratch, encoding_mask);
1878 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1879 if (encoding == String::ONE_BYTE_ENCODING) {
1880 __ sb(value, operand);
1882 __ sh(value, operand);
1887 void LCodeGen::DoAddI(LAddI* instr) {
1888 LOperand* left = instr->left();
1889 LOperand* right = instr->right();
1890 LOperand* result = instr->result();
1891 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1893 if (!can_overflow) {
1894 if (right->IsStackSlot()) {
1895 Register right_reg = EmitLoadRegister(right, at);
1896 __ Addu(ToRegister(result), ToRegister(left), Operand(right_reg));
1898 DCHECK(right->IsRegister() || right->IsConstantOperand());
1899 __ Addu(ToRegister(result), ToRegister(left), ToOperand(right));
1901 } else { // can_overflow.
1902 Register overflow = scratch0();
1903 Register scratch = scratch1();
1904 if (right->IsStackSlot() ||
1905 right->IsConstantOperand()) {
1906 Register right_reg = EmitLoadRegister(right, scratch);
1907 __ AdduAndCheckForOverflow(ToRegister(result),
1910 overflow); // Reg at also used as scratch.
1912 DCHECK(right->IsRegister());
1913 // Due to overflow check macros not supporting constant operands,
1914 // handling the IsConstantOperand case was moved to prev if clause.
1915 __ AdduAndCheckForOverflow(ToRegister(result),
1918 overflow); // Reg at also used as scratch.
1920 DeoptimizeIf(lt, instr, "overflow", overflow, Operand(zero_reg));
1925 void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
1926 LOperand* left = instr->left();
1927 LOperand* right = instr->right();
1928 HMathMinMax::Operation operation = instr->hydrogen()->operation();
1929 Condition condition = (operation == HMathMinMax::kMathMin) ? le : ge;
1930 if (instr->hydrogen()->representation().IsSmiOrInteger32()) {
1931 Register left_reg = ToRegister(left);
1932 Register right_reg = EmitLoadRegister(right, scratch0());
1933 Register result_reg = ToRegister(instr->result());
1934 Label return_right, done;
1935 Register scratch = scratch1();
1936 __ Slt(scratch, left_reg, Operand(right_reg));
1937 if (condition == ge) {
1938 __ Movz(result_reg, left_reg, scratch);
1939 __ Movn(result_reg, right_reg, scratch);
1941 DCHECK(condition == le);
1942 __ Movn(result_reg, left_reg, scratch);
1943 __ Movz(result_reg, right_reg, scratch);
1946 DCHECK(instr->hydrogen()->representation().IsDouble());
1947 FPURegister left_reg = ToDoubleRegister(left);
1948 FPURegister right_reg = ToDoubleRegister(right);
1949 FPURegister result_reg = ToDoubleRegister(instr->result());
1950 Label check_nan_left, check_zero, return_left, return_right, done;
1951 __ BranchF(&check_zero, &check_nan_left, eq, left_reg, right_reg);
1952 __ BranchF(&return_left, NULL, condition, left_reg, right_reg);
1953 __ Branch(&return_right);
1955 __ bind(&check_zero);
1956 // left == right != 0.
1957 __ BranchF(&return_left, NULL, ne, left_reg, kDoubleRegZero);
1958 // At this point, both left and right are either 0 or -0.
1959 if (operation == HMathMinMax::kMathMin) {
1960 __ neg_d(left_reg, left_reg);
1961 __ sub_d(result_reg, left_reg, right_reg);
1962 __ neg_d(result_reg, result_reg);
1964 __ add_d(result_reg, left_reg, right_reg);
1968 __ bind(&check_nan_left);
1970 __ BranchF(NULL, &return_left, eq, left_reg, left_reg);
1971 __ bind(&return_right);
1972 if (!right_reg.is(result_reg)) {
1973 __ mov_d(result_reg, right_reg);
1977 __ bind(&return_left);
1978 if (!left_reg.is(result_reg)) {
1979 __ mov_d(result_reg, left_reg);
1986 void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
1987 DoubleRegister left = ToDoubleRegister(instr->left());
1988 DoubleRegister right = ToDoubleRegister(instr->right());
1989 DoubleRegister result = ToDoubleRegister(instr->result());
1990 switch (instr->op()) {
1992 __ add_d(result, left, right);
1995 __ sub_d(result, left, right);
1998 __ mul_d(result, left, right);
2001 __ div_d(result, left, right);
2004 // Save a0-a3 on the stack.
2005 RegList saved_regs = a0.bit() | a1.bit() | a2.bit() | a3.bit();
2006 __ MultiPush(saved_regs);
2008 __ PrepareCallCFunction(0, 2, scratch0());
2009 __ MovToFloatParameters(left, right);
2011 ExternalReference::mod_two_doubles_operation(isolate()),
2013 // Move the result in the double result register.
2014 __ MovFromFloatResult(result);
2016 // Restore saved register.
2017 __ MultiPop(saved_regs);
2027 void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
2028 DCHECK(ToRegister(instr->context()).is(cp));
2029 DCHECK(ToRegister(instr->left()).is(a1));
2030 DCHECK(ToRegister(instr->right()).is(a0));
2031 DCHECK(ToRegister(instr->result()).is(v0));
2034 CodeFactory::BinaryOpIC(isolate(), instr->op(), NO_OVERWRITE).code();
2035 CallCode(code, RelocInfo::CODE_TARGET, instr);
2036 // Other arch use a nop here, to signal that there is no inlined
2037 // patchable code. Mips does not need the nop, since our marker
2038 // instruction (andi zero_reg) will never be used in normal code.
2042 template<class InstrType>
2043 void LCodeGen::EmitBranch(InstrType instr,
2044 Condition condition,
2046 const Operand& src2) {
2047 int left_block = instr->TrueDestination(chunk_);
2048 int right_block = instr->FalseDestination(chunk_);
2050 int next_block = GetNextEmittedBlock();
2051 if (right_block == left_block || condition == al) {
2052 EmitGoto(left_block);
2053 } else if (left_block == next_block) {
2054 __ Branch(chunk_->GetAssemblyLabel(right_block),
2055 NegateCondition(condition), src1, src2);
2056 } else if (right_block == next_block) {
2057 __ Branch(chunk_->GetAssemblyLabel(left_block), condition, src1, src2);
2059 __ Branch(chunk_->GetAssemblyLabel(left_block), condition, src1, src2);
2060 __ Branch(chunk_->GetAssemblyLabel(right_block));
2065 template<class InstrType>
2066 void LCodeGen::EmitBranchF(InstrType instr,
2067 Condition condition,
2070 int right_block = instr->FalseDestination(chunk_);
2071 int left_block = instr->TrueDestination(chunk_);
2073 int next_block = GetNextEmittedBlock();
2074 if (right_block == left_block) {
2075 EmitGoto(left_block);
2076 } else if (left_block == next_block) {
2077 __ BranchF(chunk_->GetAssemblyLabel(right_block), NULL,
2078 NegateCondition(condition), src1, src2);
2079 } else if (right_block == next_block) {
2080 __ BranchF(chunk_->GetAssemblyLabel(left_block), NULL,
2081 condition, src1, src2);
2083 __ BranchF(chunk_->GetAssemblyLabel(left_block), NULL,
2084 condition, src1, src2);
2085 __ Branch(chunk_->GetAssemblyLabel(right_block));
2090 template<class InstrType>
2091 void LCodeGen::EmitFalseBranch(InstrType instr,
2092 Condition condition,
2094 const Operand& src2) {
2095 int false_block = instr->FalseDestination(chunk_);
2096 __ Branch(chunk_->GetAssemblyLabel(false_block), condition, src1, src2);
2100 template<class InstrType>
2101 void LCodeGen::EmitFalseBranchF(InstrType instr,
2102 Condition condition,
2105 int false_block = instr->FalseDestination(chunk_);
2106 __ BranchF(chunk_->GetAssemblyLabel(false_block), NULL,
2107 condition, src1, src2);
2111 void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
2112 __ stop("LDebugBreak");
2116 void LCodeGen::DoBranch(LBranch* instr) {
2117 Representation r = instr->hydrogen()->value()->representation();
2118 if (r.IsInteger32() || r.IsSmi()) {
2119 DCHECK(!info()->IsStub());
2120 Register reg = ToRegister(instr->value());
2121 EmitBranch(instr, ne, reg, Operand(zero_reg));
2122 } else if (r.IsDouble()) {
2123 DCHECK(!info()->IsStub());
2124 DoubleRegister reg = ToDoubleRegister(instr->value());
2125 // Test the double value. Zero and NaN are false.
2126 EmitBranchF(instr, nue, reg, kDoubleRegZero);
2128 DCHECK(r.IsTagged());
2129 Register reg = ToRegister(instr->value());
2130 HType type = instr->hydrogen()->value()->type();
2131 if (type.IsBoolean()) {
2132 DCHECK(!info()->IsStub());
2133 __ LoadRoot(at, Heap::kTrueValueRootIndex);
2134 EmitBranch(instr, eq, reg, Operand(at));
2135 } else if (type.IsSmi()) {
2136 DCHECK(!info()->IsStub());
2137 EmitBranch(instr, ne, reg, Operand(zero_reg));
2138 } else if (type.IsJSArray()) {
2139 DCHECK(!info()->IsStub());
2140 EmitBranch(instr, al, zero_reg, Operand(zero_reg));
2141 } else if (type.IsHeapNumber()) {
2142 DCHECK(!info()->IsStub());
2143 DoubleRegister dbl_scratch = double_scratch0();
2144 __ ldc1(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
2145 // Test the double value. Zero and NaN are false.
2146 EmitBranchF(instr, nue, dbl_scratch, kDoubleRegZero);
2147 } else if (type.IsString()) {
2148 DCHECK(!info()->IsStub());
2149 __ lw(at, FieldMemOperand(reg, String::kLengthOffset));
2150 EmitBranch(instr, ne, at, Operand(zero_reg));
2152 ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
2153 // Avoid deopts in the case where we've never executed this path before.
2154 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
2156 if (expected.Contains(ToBooleanStub::UNDEFINED)) {
2157 // undefined -> false.
2158 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
2159 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
2161 if (expected.Contains(ToBooleanStub::BOOLEAN)) {
2162 // Boolean -> its value.
2163 __ LoadRoot(at, Heap::kTrueValueRootIndex);
2164 __ Branch(instr->TrueLabel(chunk_), eq, reg, Operand(at));
2165 __ LoadRoot(at, Heap::kFalseValueRootIndex);
2166 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
2168 if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
2170 __ LoadRoot(at, Heap::kNullValueRootIndex);
2171 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
2174 if (expected.Contains(ToBooleanStub::SMI)) {
2175 // Smis: 0 -> false, all other -> true.
2176 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(zero_reg));
2177 __ JumpIfSmi(reg, instr->TrueLabel(chunk_));
2178 } else if (expected.NeedsMap()) {
2179 // If we need a map later and have a Smi -> deopt.
2181 DeoptimizeIf(eq, instr, "Smi", at, Operand(zero_reg));
2184 const Register map = scratch0();
2185 if (expected.NeedsMap()) {
2186 __ lw(map, FieldMemOperand(reg, HeapObject::kMapOffset));
2187 if (expected.CanBeUndetectable()) {
2188 // Undetectable -> false.
2189 __ lbu(at, FieldMemOperand(map, Map::kBitFieldOffset));
2190 __ And(at, at, Operand(1 << Map::kIsUndetectable));
2191 __ Branch(instr->FalseLabel(chunk_), ne, at, Operand(zero_reg));
2195 if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
2196 // spec object -> true.
2197 __ lbu(at, FieldMemOperand(map, Map::kInstanceTypeOffset));
2198 __ Branch(instr->TrueLabel(chunk_),
2199 ge, at, Operand(FIRST_SPEC_OBJECT_TYPE));
2202 if (expected.Contains(ToBooleanStub::STRING)) {
2203 // String value -> false iff empty.
2205 __ lbu(at, FieldMemOperand(map, Map::kInstanceTypeOffset));
2206 __ Branch(¬_string, ge , at, Operand(FIRST_NONSTRING_TYPE));
2207 __ lw(at, FieldMemOperand(reg, String::kLengthOffset));
2208 __ Branch(instr->TrueLabel(chunk_), ne, at, Operand(zero_reg));
2209 __ Branch(instr->FalseLabel(chunk_));
2210 __ bind(¬_string);
2213 if (expected.Contains(ToBooleanStub::SYMBOL)) {
2214 // Symbol value -> true.
2215 const Register scratch = scratch1();
2216 __ lbu(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
2217 __ Branch(instr->TrueLabel(chunk_), eq, scratch, Operand(SYMBOL_TYPE));
2220 if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
2221 // heap number -> false iff +0, -0, or NaN.
2222 DoubleRegister dbl_scratch = double_scratch0();
2223 Label not_heap_number;
2224 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
2225 __ Branch(¬_heap_number, ne, map, Operand(at));
2226 __ ldc1(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
2227 __ BranchF(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2228 ne, dbl_scratch, kDoubleRegZero);
2229 // Falls through if dbl_scratch == 0.
2230 __ Branch(instr->FalseLabel(chunk_));
2231 __ bind(¬_heap_number);
2234 if (!expected.IsGeneric()) {
2235 // We've seen something for the first time -> deopt.
2236 // This can only happen if we are not generic already.
2237 DeoptimizeIf(al, instr, "unexpected object", zero_reg,
2245 void LCodeGen::EmitGoto(int block) {
2246 if (!IsNextEmittedBlock(block)) {
2247 __ jmp(chunk_->GetAssemblyLabel(LookupDestination(block)));
2252 void LCodeGen::DoGoto(LGoto* instr) {
2253 EmitGoto(instr->block_id());
2257 Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
2258 Condition cond = kNoCondition;
2261 case Token::EQ_STRICT:
2265 case Token::NE_STRICT:
2269 cond = is_unsigned ? lo : lt;
2272 cond = is_unsigned ? hi : gt;
2275 cond = is_unsigned ? ls : le;
2278 cond = is_unsigned ? hs : ge;
2281 case Token::INSTANCEOF:
2289 void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2290 LOperand* left = instr->left();
2291 LOperand* right = instr->right();
2293 instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2294 instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2295 Condition cond = TokenToCondition(instr->op(), is_unsigned);
2297 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2298 // We can statically evaluate the comparison.
2299 double left_val = ToDouble(LConstantOperand::cast(left));
2300 double right_val = ToDouble(LConstantOperand::cast(right));
2301 int next_block = EvalComparison(instr->op(), left_val, right_val) ?
2302 instr->TrueDestination(chunk_) : instr->FalseDestination(chunk_);
2303 EmitGoto(next_block);
2305 if (instr->is_double()) {
2306 // Compare left and right as doubles and load the
2307 // resulting flags into the normal status register.
2308 FPURegister left_reg = ToDoubleRegister(left);
2309 FPURegister right_reg = ToDoubleRegister(right);
2311 // If a NaN is involved, i.e. the result is unordered,
2312 // jump to false block label.
2313 __ BranchF(NULL, instr->FalseLabel(chunk_), eq,
2314 left_reg, right_reg);
2316 EmitBranchF(instr, cond, left_reg, right_reg);
2319 Operand cmp_right = Operand(0);
2321 if (right->IsConstantOperand()) {
2322 int32_t value = ToInteger32(LConstantOperand::cast(right));
2323 if (instr->hydrogen_value()->representation().IsSmi()) {
2324 cmp_left = ToRegister(left);
2325 cmp_right = Operand(Smi::FromInt(value));
2327 cmp_left = ToRegister(left);
2328 cmp_right = Operand(value);
2330 } else if (left->IsConstantOperand()) {
2331 int32_t value = ToInteger32(LConstantOperand::cast(left));
2332 if (instr->hydrogen_value()->representation().IsSmi()) {
2333 cmp_left = ToRegister(right);
2334 cmp_right = Operand(Smi::FromInt(value));
2336 cmp_left = ToRegister(right);
2337 cmp_right = Operand(value);
2339 // We commuted the operands, so commute the condition.
2340 cond = CommuteCondition(cond);
2342 cmp_left = ToRegister(left);
2343 cmp_right = Operand(ToRegister(right));
2346 EmitBranch(instr, cond, cmp_left, cmp_right);
2352 void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
2353 Register left = ToRegister(instr->left());
2354 Register right = ToRegister(instr->right());
2356 EmitBranch(instr, eq, left, Operand(right));
2360 void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) {
2361 if (instr->hydrogen()->representation().IsTagged()) {
2362 Register input_reg = ToRegister(instr->object());
2363 __ li(at, Operand(factory()->the_hole_value()));
2364 EmitBranch(instr, eq, input_reg, Operand(at));
2368 DoubleRegister input_reg = ToDoubleRegister(instr->object());
2369 EmitFalseBranchF(instr, eq, input_reg, input_reg);
2371 Register scratch = scratch0();
2372 __ FmoveHigh(scratch, input_reg);
2373 EmitBranch(instr, eq, scratch, Operand(kHoleNanUpper32));
2377 void LCodeGen::DoCompareMinusZeroAndBranch(LCompareMinusZeroAndBranch* instr) {
2378 Representation rep = instr->hydrogen()->value()->representation();
2379 DCHECK(!rep.IsInteger32());
2380 Register scratch = ToRegister(instr->temp());
2382 if (rep.IsDouble()) {
2383 DoubleRegister value = ToDoubleRegister(instr->value());
2384 EmitFalseBranchF(instr, ne, value, kDoubleRegZero);
2385 __ FmoveHigh(scratch, value);
2386 __ li(at, 0x80000000);
2388 Register value = ToRegister(instr->value());
2391 Heap::kHeapNumberMapRootIndex,
2392 instr->FalseLabel(chunk()),
2394 __ lw(scratch, FieldMemOperand(value, HeapNumber::kExponentOffset));
2395 EmitFalseBranch(instr, ne, scratch, Operand(0x80000000));
2396 __ lw(scratch, FieldMemOperand(value, HeapNumber::kMantissaOffset));
2397 __ mov(at, zero_reg);
2399 EmitBranch(instr, eq, scratch, Operand(at));
2403 Condition LCodeGen::EmitIsObject(Register input,
2406 Label* is_not_object,
2408 __ JumpIfSmi(input, is_not_object);
2410 __ LoadRoot(temp2, Heap::kNullValueRootIndex);
2411 __ Branch(is_object, eq, input, Operand(temp2));
2414 __ lw(temp1, FieldMemOperand(input, HeapObject::kMapOffset));
2415 // Undetectable objects behave like undefined.
2416 __ lbu(temp2, FieldMemOperand(temp1, Map::kBitFieldOffset));
2417 __ And(temp2, temp2, Operand(1 << Map::kIsUndetectable));
2418 __ Branch(is_not_object, ne, temp2, Operand(zero_reg));
2420 // Load instance type and check that it is in object type range.
2421 __ lbu(temp2, FieldMemOperand(temp1, Map::kInstanceTypeOffset));
2422 __ Branch(is_not_object,
2423 lt, temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2429 void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) {
2430 Register reg = ToRegister(instr->value());
2431 Register temp1 = ToRegister(instr->temp());
2432 Register temp2 = scratch0();
2434 Condition true_cond =
2435 EmitIsObject(reg, temp1, temp2,
2436 instr->FalseLabel(chunk_), instr->TrueLabel(chunk_));
2438 EmitBranch(instr, true_cond, temp2,
2439 Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
2443 Condition LCodeGen::EmitIsString(Register input,
2445 Label* is_not_string,
2446 SmiCheck check_needed = INLINE_SMI_CHECK) {
2447 if (check_needed == INLINE_SMI_CHECK) {
2448 __ JumpIfSmi(input, is_not_string);
2450 __ GetObjectType(input, temp1, temp1);
2456 void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
2457 Register reg = ToRegister(instr->value());
2458 Register temp1 = ToRegister(instr->temp());
2460 SmiCheck check_needed =
2461 instr->hydrogen()->value()->type().IsHeapObject()
2462 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2463 Condition true_cond =
2464 EmitIsString(reg, temp1, instr->FalseLabel(chunk_), check_needed);
2466 EmitBranch(instr, true_cond, temp1,
2467 Operand(FIRST_NONSTRING_TYPE));
2471 void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
2472 Register input_reg = EmitLoadRegister(instr->value(), at);
2473 __ And(at, input_reg, kSmiTagMask);
2474 EmitBranch(instr, eq, at, Operand(zero_reg));
2478 void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
2479 Register input = ToRegister(instr->value());
2480 Register temp = ToRegister(instr->temp());
2482 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2483 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2485 __ lw(temp, FieldMemOperand(input, HeapObject::kMapOffset));
2486 __ lbu(temp, FieldMemOperand(temp, Map::kBitFieldOffset));
2487 __ And(at, temp, Operand(1 << Map::kIsUndetectable));
2488 EmitBranch(instr, ne, at, Operand(zero_reg));
2492 static Condition ComputeCompareCondition(Token::Value op) {
2494 case Token::EQ_STRICT:
2507 return kNoCondition;
2512 void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
2513 DCHECK(ToRegister(instr->context()).is(cp));
2514 Token::Value op = instr->op();
2516 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
2517 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2519 Condition condition = ComputeCompareCondition(op);
2521 EmitBranch(instr, condition, v0, Operand(zero_reg));
2525 static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
2526 InstanceType from = instr->from();
2527 InstanceType to = instr->to();
2528 if (from == FIRST_TYPE) return to;
2529 DCHECK(from == to || to == LAST_TYPE);
2534 static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
2535 InstanceType from = instr->from();
2536 InstanceType to = instr->to();
2537 if (from == to) return eq;
2538 if (to == LAST_TYPE) return hs;
2539 if (from == FIRST_TYPE) return ls;
2545 void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
2546 Register scratch = scratch0();
2547 Register input = ToRegister(instr->value());
2549 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2550 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2553 __ GetObjectType(input, scratch, scratch);
2555 BranchCondition(instr->hydrogen()),
2557 Operand(TestType(instr->hydrogen())));
2561 void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
2562 Register input = ToRegister(instr->value());
2563 Register result = ToRegister(instr->result());
2565 __ AssertString(input);
2567 __ lw(result, FieldMemOperand(input, String::kHashFieldOffset));
2568 __ IndexFromHash(result, result);
2572 void LCodeGen::DoHasCachedArrayIndexAndBranch(
2573 LHasCachedArrayIndexAndBranch* instr) {
2574 Register input = ToRegister(instr->value());
2575 Register scratch = scratch0();
2578 FieldMemOperand(input, String::kHashFieldOffset));
2579 __ And(at, scratch, Operand(String::kContainsCachedArrayIndexMask));
2580 EmitBranch(instr, eq, at, Operand(zero_reg));
2584 // Branches to a label or falls through with the answer in flags. Trashes
2585 // the temp registers, but not the input.
2586 void LCodeGen::EmitClassOfTest(Label* is_true,
2588 Handle<String>class_name,
2592 DCHECK(!input.is(temp));
2593 DCHECK(!input.is(temp2));
2594 DCHECK(!temp.is(temp2));
2596 __ JumpIfSmi(input, is_false);
2598 if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
2599 // Assuming the following assertions, we can use the same compares to test
2600 // for both being a function type and being in the object type range.
2601 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
2602 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2603 FIRST_SPEC_OBJECT_TYPE + 1);
2604 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2605 LAST_SPEC_OBJECT_TYPE - 1);
2606 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
2608 __ GetObjectType(input, temp, temp2);
2609 __ Branch(is_false, lt, temp2, Operand(FIRST_SPEC_OBJECT_TYPE));
2610 __ Branch(is_true, eq, temp2, Operand(FIRST_SPEC_OBJECT_TYPE));
2611 __ Branch(is_true, eq, temp2, Operand(LAST_SPEC_OBJECT_TYPE));
2613 // Faster code path to avoid two compares: subtract lower bound from the
2614 // actual type and do a signed compare with the width of the type range.
2615 __ GetObjectType(input, temp, temp2);
2616 __ Subu(temp2, temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2617 __ Branch(is_false, gt, temp2, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE -
2618 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2621 // Now we are in the FIRST-LAST_NONCALLABLE_SPEC_OBJECT_TYPE range.
2622 // Check if the constructor in the map is a function.
2623 __ lw(temp, FieldMemOperand(temp, Map::kConstructorOffset));
2625 // Objects with a non-function constructor have class 'Object'.
2626 __ GetObjectType(temp, temp2, temp2);
2627 if (String::Equals(class_name, isolate()->factory()->Object_string())) {
2628 __ Branch(is_true, ne, temp2, Operand(JS_FUNCTION_TYPE));
2630 __ Branch(is_false, ne, temp2, Operand(JS_FUNCTION_TYPE));
2633 // temp now contains the constructor function. Grab the
2634 // instance class name from there.
2635 __ lw(temp, FieldMemOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2636 __ lw(temp, FieldMemOperand(temp,
2637 SharedFunctionInfo::kInstanceClassNameOffset));
2638 // The class name we are testing against is internalized since it's a literal.
2639 // The name in the constructor is internalized because of the way the context
2640 // is booted. This routine isn't expected to work for random API-created
2641 // classes and it doesn't have to because you can't access it with natives
2642 // syntax. Since both sides are internalized it is sufficient to use an
2643 // identity comparison.
2645 // End with the address of this class_name instance in temp register.
2646 // On MIPS, the caller must do the comparison with Handle<String>class_name.
2650 void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
2651 Register input = ToRegister(instr->value());
2652 Register temp = scratch0();
2653 Register temp2 = ToRegister(instr->temp());
2654 Handle<String> class_name = instr->hydrogen()->class_name();
2656 EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2657 class_name, input, temp, temp2);
2659 EmitBranch(instr, eq, temp, Operand(class_name));
2663 void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
2664 Register reg = ToRegister(instr->value());
2665 Register temp = ToRegister(instr->temp());
2667 __ lw(temp, FieldMemOperand(reg, HeapObject::kMapOffset));
2668 EmitBranch(instr, eq, temp, Operand(instr->map()));
2672 void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
2673 DCHECK(ToRegister(instr->context()).is(cp));
2674 Label true_label, done;
2675 DCHECK(ToRegister(instr->left()).is(a0)); // Object is in a0.
2676 DCHECK(ToRegister(instr->right()).is(a1)); // Function is in a1.
2677 Register result = ToRegister(instr->result());
2678 DCHECK(result.is(v0));
2680 InstanceofStub stub(isolate(), InstanceofStub::kArgsInRegisters);
2681 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2683 __ Branch(&true_label, eq, result, Operand(zero_reg));
2684 __ li(result, Operand(factory()->false_value()));
2686 __ bind(&true_label);
2687 __ li(result, Operand(factory()->true_value()));
2692 void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) {
2693 class DeferredInstanceOfKnownGlobal FINAL : public LDeferredCode {
2695 DeferredInstanceOfKnownGlobal(LCodeGen* codegen,
2696 LInstanceOfKnownGlobal* instr)
2697 : LDeferredCode(codegen), instr_(instr) { }
2698 virtual void Generate() OVERRIDE {
2699 codegen()->DoDeferredInstanceOfKnownGlobal(instr_, &map_check_);
2701 virtual LInstruction* instr() OVERRIDE { return instr_; }
2702 Label* map_check() { return &map_check_; }
2705 LInstanceOfKnownGlobal* instr_;
2709 DeferredInstanceOfKnownGlobal* deferred;
2710 deferred = new(zone()) DeferredInstanceOfKnownGlobal(this, instr);
2712 Label done, false_result;
2713 Register object = ToRegister(instr->value());
2714 Register temp = ToRegister(instr->temp());
2715 Register result = ToRegister(instr->result());
2717 DCHECK(object.is(a0));
2718 DCHECK(result.is(v0));
2720 // A Smi is not instance of anything.
2721 __ JumpIfSmi(object, &false_result);
2723 // This is the inlined call site instanceof cache. The two occurences of the
2724 // hole value will be patched to the last map/result pair generated by the
2727 Register map = temp;
2728 __ lw(map, FieldMemOperand(object, HeapObject::kMapOffset));
2730 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
2731 __ bind(deferred->map_check()); // Label for calculating code patching.
2732 // We use Factory::the_hole_value() on purpose instead of loading from the
2733 // root array to force relocation to be able to later patch with
2735 Handle<Cell> cell = factory()->NewCell(factory()->the_hole_value());
2736 __ li(at, Operand(Handle<Object>(cell)));
2737 __ lw(at, FieldMemOperand(at, PropertyCell::kValueOffset));
2738 __ BranchShort(&cache_miss, ne, map, Operand(at));
2739 // We use Factory::the_hole_value() on purpose instead of loading from the
2740 // root array to force relocation to be able to later patch
2741 // with true or false. The distance from map check has to be constant.
2742 __ li(result, Operand(factory()->the_hole_value()), CONSTANT_SIZE);
2745 // The inlined call site cache did not match. Check null and string before
2746 // calling the deferred code.
2747 __ bind(&cache_miss);
2748 // Null is not instance of anything.
2749 __ LoadRoot(temp, Heap::kNullValueRootIndex);
2750 __ Branch(&false_result, eq, object, Operand(temp));
2752 // String values is not instance of anything.
2753 Condition cc = __ IsObjectStringType(object, temp, temp);
2754 __ Branch(&false_result, cc, temp, Operand(zero_reg));
2756 // Go to the deferred code.
2757 __ Branch(deferred->entry());
2759 __ bind(&false_result);
2760 __ LoadRoot(result, Heap::kFalseValueRootIndex);
2762 // Here result has either true or false. Deferred code also produces true or
2764 __ bind(deferred->exit());
2769 void LCodeGen::DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
2771 Register result = ToRegister(instr->result());
2772 DCHECK(result.is(v0));
2774 InstanceofStub::Flags flags = InstanceofStub::kNoFlags;
2775 flags = static_cast<InstanceofStub::Flags>(
2776 flags | InstanceofStub::kArgsInRegisters);
2777 flags = static_cast<InstanceofStub::Flags>(
2778 flags | InstanceofStub::kCallSiteInlineCheck);
2779 flags = static_cast<InstanceofStub::Flags>(
2780 flags | InstanceofStub::kReturnTrueFalseObject);
2781 InstanceofStub stub(isolate(), flags);
2783 PushSafepointRegistersScope scope(this);
2784 LoadContextFromDeferred(instr->context());
2786 // Get the temp register reserved by the instruction. This needs to be t0 as
2787 // its slot of the pushing of safepoint registers is used to communicate the
2788 // offset to the location of the map check.
2789 Register temp = ToRegister(instr->temp());
2790 DCHECK(temp.is(t0));
2791 __ li(InstanceofStub::right(), instr->function());
2792 static const int kAdditionalDelta = 7;
2793 int delta = masm_->InstructionsGeneratedSince(map_check) + kAdditionalDelta;
2794 Label before_push_delta;
2795 __ bind(&before_push_delta);
2797 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
2798 __ li(temp, Operand(delta * kPointerSize), CONSTANT_SIZE);
2799 __ StoreToSafepointRegisterSlot(temp, temp);
2801 CallCodeGeneric(stub.GetCode(),
2802 RelocInfo::CODE_TARGET,
2804 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
2805 LEnvironment* env = instr->GetDeferredLazyDeoptimizationEnvironment();
2806 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
2807 // Put the result value into the result register slot and
2808 // restore all registers.
2809 __ StoreToSafepointRegisterSlot(result, result);
2813 void LCodeGen::DoCmpT(LCmpT* instr) {
2814 DCHECK(ToRegister(instr->context()).is(cp));
2815 Token::Value op = instr->op();
2817 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
2818 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2819 // On MIPS there is no need for a "no inlined smi code" marker (nop).
2821 Condition condition = ComputeCompareCondition(op);
2822 // A minor optimization that relies on LoadRoot always emitting one
2824 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
2826 __ Branch(USE_DELAY_SLOT, &done, condition, v0, Operand(zero_reg));
2828 __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex);
2829 DCHECK_EQ(1, masm()->InstructionsGeneratedSince(&check));
2830 __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex);
2835 void LCodeGen::DoReturn(LReturn* instr) {
2836 if (FLAG_trace && info()->IsOptimizing()) {
2837 // Push the return value on the stack as the parameter.
2838 // Runtime::TraceExit returns its parameter in v0. We're leaving the code
2839 // managed by the register allocator and tearing down the frame, it's
2840 // safe to write to the context register.
2842 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2843 __ CallRuntime(Runtime::kTraceExit, 1);
2845 if (info()->saves_caller_doubles()) {
2846 RestoreCallerDoubles();
2848 int no_frame_start = -1;
2849 if (NeedsEagerFrame()) {
2851 no_frame_start = masm_->pc_offset();
2854 if (instr->has_constant_parameter_count()) {
2855 int parameter_count = ToInteger32(instr->constant_parameter_count());
2856 int32_t sp_delta = (parameter_count + 1) * kPointerSize;
2857 if (sp_delta != 0) {
2858 __ Addu(sp, sp, Operand(sp_delta));
2861 Register reg = ToRegister(instr->parameter_count());
2862 // The argument count parameter is a smi
2864 __ sll(at, reg, kPointerSizeLog2);
2865 __ Addu(sp, sp, at);
2870 if (no_frame_start != -1) {
2871 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
2876 void LCodeGen::DoLoadGlobalCell(LLoadGlobalCell* instr) {
2877 Register result = ToRegister(instr->result());
2878 __ li(at, Operand(Handle<Object>(instr->hydrogen()->cell().handle())));
2879 __ lw(result, FieldMemOperand(at, Cell::kValueOffset));
2880 if (instr->hydrogen()->RequiresHoleCheck()) {
2881 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2882 DeoptimizeIf(eq, instr, "hole", result, Operand(at));
2888 void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
2889 DCHECK(FLAG_vector_ics);
2890 Register vector_register = ToRegister(instr->temp_vector());
2891 DCHECK(vector_register.is(VectorLoadICDescriptor::VectorRegister()));
2892 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2893 __ li(vector_register, vector);
2894 // No need to allocate this register.
2895 DCHECK(VectorLoadICDescriptor::SlotRegister().is(a0));
2896 int index = vector->GetIndex(instr->hydrogen()->slot());
2897 __ li(VectorLoadICDescriptor::SlotRegister(), Operand(Smi::FromInt(index)));
2901 void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
2902 DCHECK(ToRegister(instr->context()).is(cp));
2903 DCHECK(ToRegister(instr->global_object())
2904 .is(LoadDescriptor::ReceiverRegister()));
2905 DCHECK(ToRegister(instr->result()).is(v0));
2907 __ li(LoadDescriptor::NameRegister(), Operand(instr->name()));
2908 if (FLAG_vector_ics) {
2909 EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
2911 ContextualMode mode = instr->for_typeof() ? NOT_CONTEXTUAL : CONTEXTUAL;
2912 Handle<Code> ic = CodeFactory::LoadICInOptimizedCode(isolate(), mode).code();
2913 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2917 void LCodeGen::DoStoreGlobalCell(LStoreGlobalCell* instr) {
2918 Register value = ToRegister(instr->value());
2919 Register cell = scratch0();
2922 __ li(cell, Operand(instr->hydrogen()->cell().handle()));
2924 // If the cell we are storing to contains the hole it could have
2925 // been deleted from the property dictionary. In that case, we need
2926 // to update the property details in the property dictionary to mark
2927 // it as no longer deleted.
2928 if (instr->hydrogen()->RequiresHoleCheck()) {
2929 // We use a temp to check the payload.
2930 Register payload = ToRegister(instr->temp());
2931 __ lw(payload, FieldMemOperand(cell, Cell::kValueOffset));
2932 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2933 DeoptimizeIf(eq, instr, "hole", payload, Operand(at));
2937 __ sw(value, FieldMemOperand(cell, Cell::kValueOffset));
2938 // Cells are always rescanned, so no write barrier here.
2943 void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
2944 Register context = ToRegister(instr->context());
2945 Register result = ToRegister(instr->result());
2947 __ lw(result, ContextOperand(context, instr->slot_index()));
2948 if (instr->hydrogen()->RequiresHoleCheck()) {
2949 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2951 if (instr->hydrogen()->DeoptimizesOnHole()) {
2952 DeoptimizeIf(eq, instr, "hole", result, Operand(at));
2955 __ Branch(&is_not_hole, ne, result, Operand(at));
2956 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2957 __ bind(&is_not_hole);
2963 void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
2964 Register context = ToRegister(instr->context());
2965 Register value = ToRegister(instr->value());
2966 Register scratch = scratch0();
2967 MemOperand target = ContextOperand(context, instr->slot_index());
2969 Label skip_assignment;
2971 if (instr->hydrogen()->RequiresHoleCheck()) {
2972 __ lw(scratch, target);
2973 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2975 if (instr->hydrogen()->DeoptimizesOnHole()) {
2976 DeoptimizeIf(eq, instr, "hole", scratch, Operand(at));
2978 __ Branch(&skip_assignment, ne, scratch, Operand(at));
2982 __ sw(value, target);
2983 if (instr->hydrogen()->NeedsWriteBarrier()) {
2984 SmiCheck check_needed =
2985 instr->hydrogen()->value()->type().IsHeapObject()
2986 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2987 __ RecordWriteContextSlot(context,
2993 EMIT_REMEMBERED_SET,
2997 __ bind(&skip_assignment);
3001 void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
3002 HObjectAccess access = instr->hydrogen()->access();
3003 int offset = access.offset();
3004 Register object = ToRegister(instr->object());
3006 if (access.IsExternalMemory()) {
3007 Register result = ToRegister(instr->result());
3008 MemOperand operand = MemOperand(object, offset);
3009 __ Load(result, operand, access.representation());
3013 if (instr->hydrogen()->representation().IsDouble()) {
3014 DoubleRegister result = ToDoubleRegister(instr->result());
3015 __ ldc1(result, FieldMemOperand(object, offset));
3019 Register result = ToRegister(instr->result());
3020 if (!access.IsInobject()) {
3021 __ lw(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
3024 MemOperand operand = FieldMemOperand(object, offset);
3025 __ Load(result, operand, access.representation());
3029 void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
3030 DCHECK(ToRegister(instr->context()).is(cp));
3031 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3032 DCHECK(ToRegister(instr->result()).is(v0));
3034 // Name is always in a2.
3035 __ li(LoadDescriptor::NameRegister(), Operand(instr->name()));
3036 if (FLAG_vector_ics) {
3037 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
3040 CodeFactory::LoadICInOptimizedCode(isolate(), NOT_CONTEXTUAL).code();
3041 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3045 void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
3046 Register scratch = scratch0();
3047 Register function = ToRegister(instr->function());
3048 Register result = ToRegister(instr->result());
3050 // Get the prototype or initial map from the function.
3052 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
3054 // Check that the function has a prototype or an initial map.
3055 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
3056 DeoptimizeIf(eq, instr, "hole", result, Operand(at));
3058 // If the function does not have an initial map, we're done.
3060 __ GetObjectType(result, scratch, scratch);
3061 __ Branch(&done, ne, scratch, Operand(MAP_TYPE));
3063 // Get the prototype from the initial map.
3064 __ lw(result, FieldMemOperand(result, Map::kPrototypeOffset));
3071 void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
3072 Register result = ToRegister(instr->result());
3073 __ LoadRoot(result, instr->index());
3077 void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
3078 Register arguments = ToRegister(instr->arguments());
3079 Register result = ToRegister(instr->result());
3080 // There are two words between the frame pointer and the last argument.
3081 // Subtracting from length accounts for one of them add one more.
3082 if (instr->length()->IsConstantOperand()) {
3083 int const_length = ToInteger32(LConstantOperand::cast(instr->length()));
3084 if (instr->index()->IsConstantOperand()) {
3085 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3086 int index = (const_length - const_index) + 1;
3087 __ lw(result, MemOperand(arguments, index * kPointerSize));
3089 Register index = ToRegister(instr->index());
3090 __ li(at, Operand(const_length + 1));
3091 __ Subu(result, at, index);
3092 __ sll(at, result, kPointerSizeLog2);
3093 __ Addu(at, arguments, at);
3094 __ lw(result, MemOperand(at));
3096 } else if (instr->index()->IsConstantOperand()) {
3097 Register length = ToRegister(instr->length());
3098 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3099 int loc = const_index - 1;
3101 __ Subu(result, length, Operand(loc));
3102 __ sll(at, result, kPointerSizeLog2);
3103 __ Addu(at, arguments, at);
3104 __ lw(result, MemOperand(at));
3106 __ sll(at, length, kPointerSizeLog2);
3107 __ Addu(at, arguments, at);
3108 __ lw(result, MemOperand(at));
3111 Register length = ToRegister(instr->length());
3112 Register index = ToRegister(instr->index());
3113 __ Subu(result, length, index);
3114 __ Addu(result, result, 1);
3115 __ sll(at, result, kPointerSizeLog2);
3116 __ Addu(at, arguments, at);
3117 __ lw(result, MemOperand(at));
3122 void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
3123 Register external_pointer = ToRegister(instr->elements());
3124 Register key = no_reg;
3125 ElementsKind elements_kind = instr->elements_kind();
3126 bool key_is_constant = instr->key()->IsConstantOperand();
3127 int constant_key = 0;
3128 if (key_is_constant) {
3129 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3130 if (constant_key & 0xF0000000) {
3131 Abort(kArrayIndexConstantValueTooBig);
3134 key = ToRegister(instr->key());
3136 int element_size_shift = ElementsKindToShiftSize(elements_kind);
3137 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3138 ? (element_size_shift - kSmiTagSize) : element_size_shift;
3139 int base_offset = instr->base_offset();
3141 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3142 elements_kind == FLOAT32_ELEMENTS ||
3143 elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
3144 elements_kind == FLOAT64_ELEMENTS) {
3145 int base_offset = instr->base_offset();
3146 FPURegister result = ToDoubleRegister(instr->result());
3147 if (key_is_constant) {
3148 __ Addu(scratch0(), external_pointer, constant_key << element_size_shift);
3150 __ sll(scratch0(), key, shift_size);
3151 __ Addu(scratch0(), scratch0(), external_pointer);
3153 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3154 elements_kind == FLOAT32_ELEMENTS) {
3155 __ lwc1(result, MemOperand(scratch0(), base_offset));
3156 __ cvt_d_s(result, result);
3157 } else { // i.e. elements_kind == EXTERNAL_DOUBLE_ELEMENTS
3158 __ ldc1(result, MemOperand(scratch0(), base_offset));
3161 Register result = ToRegister(instr->result());
3162 MemOperand mem_operand = PrepareKeyedOperand(
3163 key, external_pointer, key_is_constant, constant_key,
3164 element_size_shift, shift_size, base_offset);
3165 switch (elements_kind) {
3166 case EXTERNAL_INT8_ELEMENTS:
3168 __ lb(result, mem_operand);
3170 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
3171 case EXTERNAL_UINT8_ELEMENTS:
3172 case UINT8_ELEMENTS:
3173 case UINT8_CLAMPED_ELEMENTS:
3174 __ lbu(result, mem_operand);
3176 case EXTERNAL_INT16_ELEMENTS:
3177 case INT16_ELEMENTS:
3178 __ lh(result, mem_operand);
3180 case EXTERNAL_UINT16_ELEMENTS:
3181 case UINT16_ELEMENTS:
3182 __ lhu(result, mem_operand);
3184 case EXTERNAL_INT32_ELEMENTS:
3185 case INT32_ELEMENTS:
3186 __ lw(result, mem_operand);
3188 case EXTERNAL_UINT32_ELEMENTS:
3189 case UINT32_ELEMENTS:
3190 __ lw(result, mem_operand);
3191 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
3192 DeoptimizeIf(Ugreater_equal, instr, "negative value", result,
3193 Operand(0x80000000));
3196 case INT32x4_ELEMENTS:
3197 case FLOAT32_ELEMENTS:
3198 case FLOAT32x4_ELEMENTS:
3199 case FLOAT64_ELEMENTS:
3200 case FLOAT64x2_ELEMENTS:
3201 case EXTERNAL_INT32x4_ELEMENTS:
3202 case EXTERNAL_FLOAT32_ELEMENTS:
3203 case EXTERNAL_FLOAT32x4_ELEMENTS:
3204 case EXTERNAL_FLOAT64_ELEMENTS:
3205 case EXTERNAL_FLOAT64x2_ELEMENTS:
3206 case FAST_DOUBLE_ELEMENTS:
3208 case FAST_SMI_ELEMENTS:
3209 case FAST_HOLEY_DOUBLE_ELEMENTS:
3210 case FAST_HOLEY_ELEMENTS:
3211 case FAST_HOLEY_SMI_ELEMENTS:
3212 case DICTIONARY_ELEMENTS:
3213 case SLOPPY_ARGUMENTS_ELEMENTS:
3221 void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
3222 Register elements = ToRegister(instr->elements());
3223 bool key_is_constant = instr->key()->IsConstantOperand();
3224 Register key = no_reg;
3225 DoubleRegister result = ToDoubleRegister(instr->result());
3226 Register scratch = scratch0();
3228 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
3230 int base_offset = instr->base_offset();
3231 if (key_is_constant) {
3232 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3233 if (constant_key & 0xF0000000) {
3234 Abort(kArrayIndexConstantValueTooBig);
3236 base_offset += constant_key * kDoubleSize;
3238 __ Addu(scratch, elements, Operand(base_offset));
3240 if (!key_is_constant) {
3241 key = ToRegister(instr->key());
3242 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3243 ? (element_size_shift - kSmiTagSize) : element_size_shift;
3244 __ sll(at, key, shift_size);
3245 __ Addu(scratch, scratch, at);
3248 __ ldc1(result, MemOperand(scratch));
3250 if (instr->hydrogen()->RequiresHoleCheck()) {
3251 __ lw(scratch, MemOperand(scratch, kHoleNanUpper32Offset));
3252 DeoptimizeIf(eq, instr, "hole", scratch, Operand(kHoleNanUpper32));
3257 void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
3258 Register elements = ToRegister(instr->elements());
3259 Register result = ToRegister(instr->result());
3260 Register scratch = scratch0();
3261 Register store_base = scratch;
3262 int offset = instr->base_offset();
3264 if (instr->key()->IsConstantOperand()) {
3265 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
3266 offset += ToInteger32(const_operand) * kPointerSize;
3267 store_base = elements;
3269 Register key = ToRegister(instr->key());
3270 // Even though the HLoadKeyed instruction forces the input
3271 // representation for the key to be an integer, the input gets replaced
3272 // during bound check elimination with the index argument to the bounds
3273 // check, which can be tagged, so that case must be handled here, too.
3274 if (instr->hydrogen()->key()->representation().IsSmi()) {
3275 __ sll(scratch, key, kPointerSizeLog2 - kSmiTagSize);
3276 __ addu(scratch, elements, scratch);
3278 __ sll(scratch, key, kPointerSizeLog2);
3279 __ addu(scratch, elements, scratch);
3282 __ lw(result, MemOperand(store_base, offset));
3284 // Check for the hole value.
3285 if (instr->hydrogen()->RequiresHoleCheck()) {
3286 if (IsFastSmiElementsKind(instr->hydrogen()->elements_kind())) {
3287 __ SmiTst(result, scratch);
3288 DeoptimizeIf(ne, instr, "not a Smi", scratch, Operand(zero_reg));
3290 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
3291 DeoptimizeIf(eq, instr, "hole", result, Operand(scratch));
3297 void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
3298 if (instr->is_typed_elements()) {
3299 DoLoadKeyedExternalArray(instr);
3300 } else if (instr->hydrogen()->representation().IsDouble()) {
3301 DoLoadKeyedFixedDoubleArray(instr);
3303 DoLoadKeyedFixedArray(instr);
3308 MemOperand LCodeGen::PrepareKeyedOperand(Register key,
3310 bool key_is_constant,
3315 if (key_is_constant) {
3316 return MemOperand(base, (constant_key << element_size) + base_offset);
3319 if (base_offset == 0) {
3320 if (shift_size >= 0) {
3321 __ sll(scratch0(), key, shift_size);
3322 __ Addu(scratch0(), base, scratch0());
3323 return MemOperand(scratch0());
3325 DCHECK_EQ(-1, shift_size);
3326 __ srl(scratch0(), key, 1);
3327 __ Addu(scratch0(), base, scratch0());
3328 return MemOperand(scratch0());
3332 if (shift_size >= 0) {
3333 __ sll(scratch0(), key, shift_size);
3334 __ Addu(scratch0(), base, scratch0());
3335 return MemOperand(scratch0(), base_offset);
3337 DCHECK_EQ(-1, shift_size);
3338 __ sra(scratch0(), key, 1);
3339 __ Addu(scratch0(), base, scratch0());
3340 return MemOperand(scratch0(), base_offset);
3345 void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3346 DCHECK(ToRegister(instr->context()).is(cp));
3347 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3348 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
3350 if (FLAG_vector_ics) {
3351 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
3354 Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(isolate()).code();
3355 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3359 void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
3360 Register scratch = scratch0();
3361 Register temp = scratch1();
3362 Register result = ToRegister(instr->result());
3364 if (instr->hydrogen()->from_inlined()) {
3365 __ Subu(result, sp, 2 * kPointerSize);
3367 // Check if the calling frame is an arguments adaptor frame.
3368 Label done, adapted;
3369 __ lw(scratch, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3370 __ lw(result, MemOperand(scratch, StandardFrameConstants::kContextOffset));
3371 __ Xor(temp, result, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3373 // Result is the frame pointer for the frame if not adapted and for the real
3374 // frame below the adaptor frame if adapted.
3375 __ Movn(result, fp, temp); // Move only if temp is not equal to zero (ne).
3376 __ Movz(result, scratch, temp); // Move only if temp is equal to zero (eq).
3381 void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
3382 Register elem = ToRegister(instr->elements());
3383 Register result = ToRegister(instr->result());
3387 // If no arguments adaptor frame the number of arguments is fixed.
3388 __ Addu(result, zero_reg, Operand(scope()->num_parameters()));
3389 __ Branch(&done, eq, fp, Operand(elem));
3391 // Arguments adaptor frame present. Get argument length from there.
3392 __ lw(result, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3394 MemOperand(result, ArgumentsAdaptorFrameConstants::kLengthOffset));
3395 __ SmiUntag(result);
3397 // Argument length is in result register.
3402 void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
3403 Register receiver = ToRegister(instr->receiver());
3404 Register function = ToRegister(instr->function());
3405 Register result = ToRegister(instr->result());
3406 Register scratch = scratch0();
3408 // If the receiver is null or undefined, we have to pass the global
3409 // object as a receiver to normal functions. Values have to be
3410 // passed unchanged to builtins and strict-mode functions.
3411 Label global_object, result_in_receiver;
3413 if (!instr->hydrogen()->known_function()) {
3414 // Do not transform the receiver to object for strict mode
3417 FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
3419 FieldMemOperand(scratch, SharedFunctionInfo::kCompilerHintsOffset));
3421 // Do not transform the receiver to object for builtins.
3422 int32_t strict_mode_function_mask =
3423 1 << (SharedFunctionInfo::kStrictModeFunction + kSmiTagSize);
3424 int32_t native_mask = 1 << (SharedFunctionInfo::kNative + kSmiTagSize);
3425 __ And(scratch, scratch, Operand(strict_mode_function_mask | native_mask));
3426 __ Branch(&result_in_receiver, ne, scratch, Operand(zero_reg));
3429 // Normal function. Replace undefined or null with global receiver.
3430 __ LoadRoot(scratch, Heap::kNullValueRootIndex);
3431 __ Branch(&global_object, eq, receiver, Operand(scratch));
3432 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
3433 __ Branch(&global_object, eq, receiver, Operand(scratch));
3435 // Deoptimize if the receiver is not a JS object.
3436 __ SmiTst(receiver, scratch);
3437 DeoptimizeIf(eq, instr, "Smi", scratch, Operand(zero_reg));
3439 __ GetObjectType(receiver, scratch, scratch);
3440 DeoptimizeIf(lt, instr, "not a JavaScript object", scratch,
3441 Operand(FIRST_SPEC_OBJECT_TYPE));
3443 __ Branch(&result_in_receiver);
3444 __ bind(&global_object);
3445 __ lw(result, FieldMemOperand(function, JSFunction::kContextOffset));
3447 ContextOperand(result, Context::GLOBAL_OBJECT_INDEX));
3449 FieldMemOperand(result, GlobalObject::kGlobalProxyOffset));
3451 if (result.is(receiver)) {
3452 __ bind(&result_in_receiver);
3455 __ Branch(&result_ok);
3456 __ bind(&result_in_receiver);
3457 __ mov(result, receiver);
3458 __ bind(&result_ok);
3463 void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3464 Register receiver = ToRegister(instr->receiver());
3465 Register function = ToRegister(instr->function());
3466 Register length = ToRegister(instr->length());
3467 Register elements = ToRegister(instr->elements());
3468 Register scratch = scratch0();
3469 DCHECK(receiver.is(a0)); // Used for parameter count.
3470 DCHECK(function.is(a1)); // Required by InvokeFunction.
3471 DCHECK(ToRegister(instr->result()).is(v0));
3473 // Copy the arguments to this function possibly from the
3474 // adaptor frame below it.
3475 const uint32_t kArgumentsLimit = 1 * KB;
3476 DeoptimizeIf(hi, instr, "too many arguments", length,
3477 Operand(kArgumentsLimit));
3479 // Push the receiver and use the register to keep the original
3480 // number of arguments.
3482 __ Move(receiver, length);
3483 // The arguments are at a one pointer size offset from elements.
3484 __ Addu(elements, elements, Operand(1 * kPointerSize));
3486 // Loop through the arguments pushing them onto the execution
3489 // length is a small non-negative integer, due to the test above.
3490 __ Branch(USE_DELAY_SLOT, &invoke, eq, length, Operand(zero_reg));
3491 __ sll(scratch, length, 2);
3493 __ Addu(scratch, elements, scratch);
3494 __ lw(scratch, MemOperand(scratch));
3496 __ Subu(length, length, Operand(1));
3497 __ Branch(USE_DELAY_SLOT, &loop, ne, length, Operand(zero_reg));
3498 __ sll(scratch, length, 2);
3501 DCHECK(instr->HasPointerMap());
3502 LPointerMap* pointers = instr->pointer_map();
3503 SafepointGenerator safepoint_generator(
3504 this, pointers, Safepoint::kLazyDeopt);
3505 // The number of arguments is stored in receiver which is a0, as expected
3506 // by InvokeFunction.
3507 ParameterCount actual(receiver);
3508 __ InvokeFunction(function, actual, CALL_FUNCTION, safepoint_generator);
3512 void LCodeGen::DoPushArgument(LPushArgument* instr) {
3513 LOperand* argument = instr->value();
3514 if (argument->IsDoubleRegister() || argument->IsDoubleStackSlot()) {
3515 Abort(kDoPushArgumentNotImplementedForDoubleType);
3517 Register argument_reg = EmitLoadRegister(argument, at);
3518 __ push(argument_reg);
3523 void LCodeGen::DoDrop(LDrop* instr) {
3524 __ Drop(instr->count());
3528 void LCodeGen::DoThisFunction(LThisFunction* instr) {
3529 Register result = ToRegister(instr->result());
3530 __ lw(result, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3534 void LCodeGen::DoContext(LContext* instr) {
3535 // If there is a non-return use, the context must be moved to a register.
3536 Register result = ToRegister(instr->result());
3537 if (info()->IsOptimizing()) {
3538 __ lw(result, MemOperand(fp, StandardFrameConstants::kContextOffset));
3540 // If there is no frame, the context must be in cp.
3541 DCHECK(result.is(cp));
3546 void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
3547 DCHECK(ToRegister(instr->context()).is(cp));
3548 __ li(scratch0(), instr->hydrogen()->pairs());
3549 __ li(scratch1(), Operand(Smi::FromInt(instr->hydrogen()->flags())));
3550 // The context is the first argument.
3551 __ Push(cp, scratch0(), scratch1());
3552 CallRuntime(Runtime::kDeclareGlobals, 3, instr);
3556 void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
3557 int formal_parameter_count,
3559 LInstruction* instr,
3561 bool dont_adapt_arguments =
3562 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3563 bool can_invoke_directly =
3564 dont_adapt_arguments || formal_parameter_count == arity;
3566 LPointerMap* pointers = instr->pointer_map();
3568 if (can_invoke_directly) {
3569 if (a1_state == A1_UNINITIALIZED) {
3570 __ li(a1, function);
3574 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
3576 // Set r0 to arguments count if adaption is not needed. Assumes that r0
3577 // is available to write to at this point.
3578 if (dont_adapt_arguments) {
3579 __ li(a0, Operand(arity));
3583 __ lw(at, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
3586 // Set up deoptimization.
3587 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3589 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3590 ParameterCount count(arity);
3591 ParameterCount expected(formal_parameter_count);
3592 __ InvokeFunction(function, expected, count, CALL_FUNCTION, generator);
3597 void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3598 DCHECK(instr->context() != NULL);
3599 DCHECK(ToRegister(instr->context()).is(cp));
3600 Register input = ToRegister(instr->value());
3601 Register result = ToRegister(instr->result());
3602 Register scratch = scratch0();
3604 // Deoptimize if not a heap number.
3605 __ lw(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
3606 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
3607 DeoptimizeIf(ne, instr, "not a heap number", scratch, Operand(at));
3610 Register exponent = scratch0();
3612 __ lw(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3613 // Check the sign of the argument. If the argument is positive, just
3615 __ Move(result, input);
3616 __ And(at, exponent, Operand(HeapNumber::kSignMask));
3617 __ Branch(&done, eq, at, Operand(zero_reg));
3619 // Input is negative. Reverse its sign.
3620 // Preserve the value of all registers.
3622 PushSafepointRegistersScope scope(this);
3624 // Registers were saved at the safepoint, so we can use
3625 // many scratch registers.
3626 Register tmp1 = input.is(a1) ? a0 : a1;
3627 Register tmp2 = input.is(a2) ? a0 : a2;
3628 Register tmp3 = input.is(a3) ? a0 : a3;
3629 Register tmp4 = input.is(t0) ? a0 : t0;
3631 // exponent: floating point exponent value.
3633 Label allocated, slow;
3634 __ LoadRoot(tmp4, Heap::kHeapNumberMapRootIndex);
3635 __ AllocateHeapNumber(tmp1, tmp2, tmp3, tmp4, &slow);
3636 __ Branch(&allocated);
3638 // Slow case: Call the runtime system to do the number allocation.
3641 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr,
3643 // Set the pointer to the new heap number in tmp.
3646 // Restore input_reg after call to runtime.
3647 __ LoadFromSafepointRegisterSlot(input, input);
3648 __ lw(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3650 __ bind(&allocated);
3651 // exponent: floating point exponent value.
3652 // tmp1: allocated heap number.
3653 __ And(exponent, exponent, Operand(~HeapNumber::kSignMask));
3654 __ sw(exponent, FieldMemOperand(tmp1, HeapNumber::kExponentOffset));
3655 __ lw(tmp2, FieldMemOperand(input, HeapNumber::kMantissaOffset));
3656 __ sw(tmp2, FieldMemOperand(tmp1, HeapNumber::kMantissaOffset));
3658 __ StoreToSafepointRegisterSlot(tmp1, result);
3665 void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3666 Register input = ToRegister(instr->value());
3667 Register result = ToRegister(instr->result());
3668 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
3670 __ Branch(USE_DELAY_SLOT, &done, ge, input, Operand(zero_reg));
3671 __ mov(result, input);
3672 __ subu(result, zero_reg, input);
3673 // Overflow if result is still negative, i.e. 0x80000000.
3674 DeoptimizeIf(lt, instr, "overflow", result, Operand(zero_reg));
3679 void LCodeGen::DoMathAbs(LMathAbs* instr) {
3680 // Class for deferred case.
3681 class DeferredMathAbsTaggedHeapNumber FINAL : public LDeferredCode {
3683 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr)
3684 : LDeferredCode(codegen), instr_(instr) { }
3685 virtual void Generate() OVERRIDE {
3686 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3688 virtual LInstruction* instr() OVERRIDE { return instr_; }
3693 Representation r = instr->hydrogen()->value()->representation();
3695 FPURegister input = ToDoubleRegister(instr->value());
3696 FPURegister result = ToDoubleRegister(instr->result());
3697 __ abs_d(result, input);
3698 } else if (r.IsSmiOrInteger32()) {
3699 EmitIntegerMathAbs(instr);
3701 // Representation is tagged.
3702 DeferredMathAbsTaggedHeapNumber* deferred =
3703 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3704 Register input = ToRegister(instr->value());
3706 __ JumpIfNotSmi(input, deferred->entry());
3707 // If smi, handle it directly.
3708 EmitIntegerMathAbs(instr);
3709 __ bind(deferred->exit());
3714 void LCodeGen::DoMathFloor(LMathFloor* instr) {
3715 DoubleRegister input = ToDoubleRegister(instr->value());
3716 Register result = ToRegister(instr->result());
3717 Register scratch1 = scratch0();
3718 Register except_flag = ToRegister(instr->temp());
3720 __ EmitFPUTruncate(kRoundToMinusInf,
3727 // Deopt if the operation did not succeed.
3728 DeoptimizeIf(ne, instr, "lost precision or NaN", except_flag,
3731 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3734 __ Branch(&done, ne, result, Operand(zero_reg));
3735 __ Mfhc1(scratch1, input);
3736 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
3737 DeoptimizeIf(ne, instr, "minus zero", scratch1, Operand(zero_reg));
3743 void LCodeGen::DoMathRound(LMathRound* instr) {
3744 DoubleRegister input = ToDoubleRegister(instr->value());
3745 Register result = ToRegister(instr->result());
3746 DoubleRegister double_scratch1 = ToDoubleRegister(instr->temp());
3747 Register scratch = scratch0();
3748 Label done, check_sign_on_zero;
3750 // Extract exponent bits.
3751 __ Mfhc1(result, input);
3754 HeapNumber::kExponentShift,
3755 HeapNumber::kExponentBits);
3757 // If the number is in ]-0.5, +0.5[, the result is +/- 0.
3759 __ Branch(&skip1, gt, scratch, Operand(HeapNumber::kExponentBias - 2));
3760 __ mov(result, zero_reg);
3761 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3762 __ Branch(&check_sign_on_zero);
3768 // The following conversion will not work with numbers
3769 // outside of ]-2^32, 2^32[.
3770 DeoptimizeIf(ge, instr, "overflow", scratch,
3771 Operand(HeapNumber::kExponentBias + 32));
3773 // Save the original sign for later comparison.
3774 __ And(scratch, result, Operand(HeapNumber::kSignMask));
3776 __ Move(double_scratch0(), 0.5);
3777 __ add_d(double_scratch0(), input, double_scratch0());
3779 // Check sign of the result: if the sign changed, the input
3780 // value was in ]0.5, 0[ and the result should be -0.
3781 __ Mfhc1(result, double_scratch0());
3782 __ Xor(result, result, Operand(scratch));
3783 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3784 // ARM uses 'mi' here, which is 'lt'
3785 DeoptimizeIf(lt, instr, "minus zero", result, Operand(zero_reg));
3788 // ARM uses 'mi' here, which is 'lt'
3789 // Negating it results in 'ge'
3790 __ Branch(&skip2, ge, result, Operand(zero_reg));
3791 __ mov(result, zero_reg);
3796 Register except_flag = scratch;
3797 __ EmitFPUTruncate(kRoundToMinusInf,
3804 DeoptimizeIf(ne, instr, "lost precision or NaN", except_flag,
3807 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3809 __ Branch(&done, ne, result, Operand(zero_reg));
3810 __ bind(&check_sign_on_zero);
3811 __ Mfhc1(scratch, input);
3812 __ And(scratch, scratch, Operand(HeapNumber::kSignMask));
3813 DeoptimizeIf(ne, instr, "minus zero", scratch, Operand(zero_reg));
3819 void LCodeGen::DoMathFround(LMathFround* instr) {
3820 DoubleRegister input = ToDoubleRegister(instr->value());
3821 DoubleRegister result = ToDoubleRegister(instr->result());
3822 __ cvt_s_d(result.low(), input);
3823 __ cvt_d_s(result, result.low());
3827 void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3828 DoubleRegister input = ToDoubleRegister(instr->value());
3829 DoubleRegister result = ToDoubleRegister(instr->result());
3830 __ sqrt_d(result, input);
3834 void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3835 DoubleRegister input = ToDoubleRegister(instr->value());
3836 DoubleRegister result = ToDoubleRegister(instr->result());
3837 DoubleRegister temp = ToDoubleRegister(instr->temp());
3839 DCHECK(!input.is(result));
3841 // Note that according to ECMA-262 15.8.2.13:
3842 // Math.pow(-Infinity, 0.5) == Infinity
3843 // Math.sqrt(-Infinity) == NaN
3845 __ Move(temp, -V8_INFINITY);
3846 __ BranchF(USE_DELAY_SLOT, &done, NULL, eq, temp, input);
3847 // Set up Infinity in the delay slot.
3848 // result is overwritten if the branch is not taken.
3849 __ neg_d(result, temp);
3851 // Add +0 to convert -0 to +0.
3852 __ add_d(result, input, kDoubleRegZero);
3853 __ sqrt_d(result, result);
3858 void LCodeGen::DoPower(LPower* instr) {
3859 Representation exponent_type = instr->hydrogen()->right()->representation();
3860 // Having marked this as a call, we can use any registers.
3861 // Just make sure that the input/output registers are the expected ones.
3862 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3863 DCHECK(!instr->right()->IsDoubleRegister() ||
3864 ToDoubleRegister(instr->right()).is(f4));
3865 DCHECK(!instr->right()->IsRegister() ||
3866 ToRegister(instr->right()).is(tagged_exponent));
3867 DCHECK(ToDoubleRegister(instr->left()).is(f2));
3868 DCHECK(ToDoubleRegister(instr->result()).is(f0));
3870 if (exponent_type.IsSmi()) {
3871 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3873 } else if (exponent_type.IsTagged()) {
3875 __ JumpIfSmi(tagged_exponent, &no_deopt);
3876 DCHECK(!t3.is(tagged_exponent));
3877 __ lw(t3, FieldMemOperand(tagged_exponent, HeapObject::kMapOffset));
3878 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
3879 DeoptimizeIf(ne, instr, "not a heap number", t3, Operand(at));
3881 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3883 } else if (exponent_type.IsInteger32()) {
3884 MathPowStub stub(isolate(), MathPowStub::INTEGER);
3887 DCHECK(exponent_type.IsDouble());
3888 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
3894 void LCodeGen::DoMathExp(LMathExp* instr) {
3895 DoubleRegister input = ToDoubleRegister(instr->value());
3896 DoubleRegister result = ToDoubleRegister(instr->result());
3897 DoubleRegister double_scratch1 = ToDoubleRegister(instr->double_temp());
3898 DoubleRegister double_scratch2 = double_scratch0();
3899 Register temp1 = ToRegister(instr->temp1());
3900 Register temp2 = ToRegister(instr->temp2());
3902 MathExpGenerator::EmitMathExp(
3903 masm(), input, result, double_scratch1, double_scratch2,
3904 temp1, temp2, scratch0());
3908 void LCodeGen::DoMathLog(LMathLog* instr) {
3909 __ PrepareCallCFunction(0, 1, scratch0());
3910 __ MovToFloatParameter(ToDoubleRegister(instr->value()));
3911 __ CallCFunction(ExternalReference::math_log_double_function(isolate()),
3913 __ MovFromFloatResult(ToDoubleRegister(instr->result()));
3917 void LCodeGen::DoMathClz32(LMathClz32* instr) {
3918 Register input = ToRegister(instr->value());
3919 Register result = ToRegister(instr->result());
3920 __ Clz(result, input);
3924 void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
3925 DCHECK(ToRegister(instr->context()).is(cp));
3926 DCHECK(ToRegister(instr->function()).is(a1));
3927 DCHECK(instr->HasPointerMap());
3929 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
3930 if (known_function.is_null()) {
3931 LPointerMap* pointers = instr->pointer_map();
3932 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3933 ParameterCount count(instr->arity());
3934 __ InvokeFunction(a1, count, CALL_FUNCTION, generator);
3936 CallKnownFunction(known_function,
3937 instr->hydrogen()->formal_parameter_count(),
3940 A1_CONTAINS_TARGET);
3945 void LCodeGen::DoTailCallThroughMegamorphicCache(
3946 LTailCallThroughMegamorphicCache* instr) {
3947 Register receiver = ToRegister(instr->receiver());
3948 Register name = ToRegister(instr->name());
3949 DCHECK(receiver.is(LoadDescriptor::ReceiverRegister()));
3950 DCHECK(name.is(LoadDescriptor::NameRegister()));
3951 DCHECK(receiver.is(a1));
3952 DCHECK(name.is(a2));
3954 Register scratch = a3;
3955 Register extra = t0;
3956 Register extra2 = t1;
3957 Register extra3 = t2;
3959 // Important for the tail-call.
3960 bool must_teardown_frame = NeedsEagerFrame();
3962 // The probe will tail call to a handler if found.
3963 isolate()->stub_cache()->GenerateProbe(masm(), instr->hydrogen()->flags(),
3964 must_teardown_frame, receiver, name,
3965 scratch, extra, extra2, extra3);
3967 // Tail call to miss if we ended up here.
3968 if (must_teardown_frame) __ LeaveFrame(StackFrame::INTERNAL);
3969 LoadIC::GenerateMiss(masm());
3973 void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
3974 DCHECK(ToRegister(instr->result()).is(v0));
3976 LPointerMap* pointers = instr->pointer_map();
3977 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3979 if (instr->target()->IsConstantOperand()) {
3980 LConstantOperand* target = LConstantOperand::cast(instr->target());
3981 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3982 generator.BeforeCall(__ CallSize(code, RelocInfo::CODE_TARGET));
3983 __ Call(code, RelocInfo::CODE_TARGET);
3985 DCHECK(instr->target()->IsRegister());
3986 Register target = ToRegister(instr->target());
3987 generator.BeforeCall(__ CallSize(target));
3988 __ Addu(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
3991 generator.AfterCall();
3995 void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
3996 DCHECK(ToRegister(instr->function()).is(a1));
3997 DCHECK(ToRegister(instr->result()).is(v0));
3999 if (instr->hydrogen()->pass_argument_count()) {
4000 __ li(a0, Operand(instr->arity()));
4004 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
4006 // Load the code entry address
4007 __ lw(at, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
4010 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
4014 void LCodeGen::DoCallFunction(LCallFunction* instr) {
4015 DCHECK(ToRegister(instr->context()).is(cp));
4016 DCHECK(ToRegister(instr->function()).is(a1));
4017 DCHECK(ToRegister(instr->result()).is(v0));
4019 int arity = instr->arity();
4020 CallFunctionStub stub(isolate(), arity, instr->hydrogen()->function_flags());
4021 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4025 void LCodeGen::DoCallNew(LCallNew* instr) {
4026 DCHECK(ToRegister(instr->context()).is(cp));
4027 DCHECK(ToRegister(instr->constructor()).is(a1));
4028 DCHECK(ToRegister(instr->result()).is(v0));
4030 __ li(a0, Operand(instr->arity()));
4031 // No cell in a2 for construct type feedback in optimized code
4032 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4033 CallConstructStub stub(isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
4034 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4038 void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
4039 DCHECK(ToRegister(instr->context()).is(cp));
4040 DCHECK(ToRegister(instr->constructor()).is(a1));
4041 DCHECK(ToRegister(instr->result()).is(v0));
4043 __ li(a0, Operand(instr->arity()));
4044 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4045 ElementsKind kind = instr->hydrogen()->elements_kind();
4046 AllocationSiteOverrideMode override_mode =
4047 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
4048 ? DISABLE_ALLOCATION_SITES
4051 if (instr->arity() == 0) {
4052 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
4053 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4054 } else if (instr->arity() == 1) {
4056 if (IsFastPackedElementsKind(kind)) {
4058 // We might need a change here,
4059 // look at the first argument.
4060 __ lw(t1, MemOperand(sp, 0));
4061 __ Branch(&packed_case, eq, t1, Operand(zero_reg));
4063 ElementsKind holey_kind = GetHoleyElementsKind(kind);
4064 ArraySingleArgumentConstructorStub stub(isolate(),
4067 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4069 __ bind(&packed_case);
4072 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
4073 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4076 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
4077 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4082 void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
4083 CallRuntime(instr->function(), instr->arity(), instr);
4087 void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
4088 Register function = ToRegister(instr->function());
4089 Register code_object = ToRegister(instr->code_object());
4090 __ Addu(code_object, code_object,
4091 Operand(Code::kHeaderSize - kHeapObjectTag));
4093 FieldMemOperand(function, JSFunction::kCodeEntryOffset));
4097 void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
4098 Register result = ToRegister(instr->result());
4099 Register base = ToRegister(instr->base_object());
4100 if (instr->offset()->IsConstantOperand()) {
4101 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
4102 __ Addu(result, base, Operand(ToInteger32(offset)));
4104 Register offset = ToRegister(instr->offset());
4105 __ Addu(result, base, offset);
4110 void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
4111 Representation representation = instr->representation();
4113 Register object = ToRegister(instr->object());
4114 Register scratch = scratch0();
4115 HObjectAccess access = instr->hydrogen()->access();
4116 int offset = access.offset();
4118 if (access.IsExternalMemory()) {
4119 Register value = ToRegister(instr->value());
4120 MemOperand operand = MemOperand(object, offset);
4121 __ Store(value, operand, representation);
4125 __ AssertNotSmi(object);
4127 DCHECK(!representation.IsSmi() ||
4128 !instr->value()->IsConstantOperand() ||
4129 IsSmi(LConstantOperand::cast(instr->value())));
4130 if (representation.IsDouble()) {
4131 DCHECK(access.IsInobject());
4132 DCHECK(!instr->hydrogen()->has_transition());
4133 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4134 DoubleRegister value = ToDoubleRegister(instr->value());
4135 __ sdc1(value, FieldMemOperand(object, offset));
4139 if (instr->hydrogen()->has_transition()) {
4140 Handle<Map> transition = instr->hydrogen()->transition_map();
4141 AddDeprecationDependency(transition);
4142 __ li(scratch, Operand(transition));
4143 __ sw(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
4144 if (instr->hydrogen()->NeedsWriteBarrierForMap()) {
4145 Register temp = ToRegister(instr->temp());
4146 // Update the write barrier for the map field.
4147 __ RecordWriteForMap(object,
4156 Register value = ToRegister(instr->value());
4157 if (access.IsInobject()) {
4158 MemOperand operand = FieldMemOperand(object, offset);
4159 __ Store(value, operand, representation);
4160 if (instr->hydrogen()->NeedsWriteBarrier()) {
4161 // Update the write barrier for the object for in-object properties.
4162 __ RecordWriteField(object,
4168 EMIT_REMEMBERED_SET,
4169 instr->hydrogen()->SmiCheckForWriteBarrier(),
4170 instr->hydrogen()->PointersToHereCheckForValue());
4173 __ lw(scratch, FieldMemOperand(object, JSObject::kPropertiesOffset));
4174 MemOperand operand = FieldMemOperand(scratch, offset);
4175 __ Store(value, operand, representation);
4176 if (instr->hydrogen()->NeedsWriteBarrier()) {
4177 // Update the write barrier for the properties array.
4178 // object is used as a scratch register.
4179 __ RecordWriteField(scratch,
4185 EMIT_REMEMBERED_SET,
4186 instr->hydrogen()->SmiCheckForWriteBarrier(),
4187 instr->hydrogen()->PointersToHereCheckForValue());
4193 void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
4194 DCHECK(ToRegister(instr->context()).is(cp));
4195 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4196 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4198 __ li(StoreDescriptor::NameRegister(), Operand(instr->name()));
4199 Handle<Code> ic = StoreIC::initialize_stub(isolate(), instr->strict_mode());
4200 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4204 void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
4205 Condition cc = instr->hydrogen()->allow_equality() ? hi : hs;
4208 if (instr->index()->IsConstantOperand()) {
4209 operand = ToOperand(instr->index());
4210 reg = ToRegister(instr->length());
4211 cc = CommuteCondition(cc);
4213 reg = ToRegister(instr->index());
4214 operand = ToOperand(instr->length());
4216 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
4218 __ Branch(&done, NegateCondition(cc), reg, operand);
4219 __ stop("eliminated bounds check failed");
4222 DeoptimizeIf(cc, instr, "out of bounds", reg, operand);
4227 void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
4228 Register external_pointer = ToRegister(instr->elements());
4229 Register key = no_reg;
4230 ElementsKind elements_kind = instr->elements_kind();
4231 bool key_is_constant = instr->key()->IsConstantOperand();
4232 int constant_key = 0;
4233 if (key_is_constant) {
4234 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4235 if (constant_key & 0xF0000000) {
4236 Abort(kArrayIndexConstantValueTooBig);
4239 key = ToRegister(instr->key());
4241 int element_size_shift = ElementsKindToShiftSize(elements_kind);
4242 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4243 ? (element_size_shift - kSmiTagSize) : element_size_shift;
4244 int base_offset = instr->base_offset();
4246 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4247 elements_kind == FLOAT32_ELEMENTS ||
4248 elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
4249 elements_kind == FLOAT64_ELEMENTS) {
4250 Register address = scratch0();
4251 FPURegister value(ToDoubleRegister(instr->value()));
4252 if (key_is_constant) {
4253 if (constant_key != 0) {
4254 __ Addu(address, external_pointer,
4255 Operand(constant_key << element_size_shift));
4257 address = external_pointer;
4260 __ sll(address, key, shift_size);
4261 __ Addu(address, external_pointer, address);
4264 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4265 elements_kind == FLOAT32_ELEMENTS) {
4266 __ cvt_s_d(double_scratch0(), value);
4267 __ swc1(double_scratch0(), MemOperand(address, base_offset));
4268 } else { // Storing doubles, not floats.
4269 __ sdc1(value, MemOperand(address, base_offset));
4272 Register value(ToRegister(instr->value()));
4273 MemOperand mem_operand = PrepareKeyedOperand(
4274 key, external_pointer, key_is_constant, constant_key,
4275 element_size_shift, shift_size,
4277 switch (elements_kind) {
4278 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
4279 case EXTERNAL_INT8_ELEMENTS:
4280 case EXTERNAL_UINT8_ELEMENTS:
4281 case UINT8_ELEMENTS:
4282 case UINT8_CLAMPED_ELEMENTS:
4284 __ sb(value, mem_operand);
4286 case EXTERNAL_INT16_ELEMENTS:
4287 case EXTERNAL_UINT16_ELEMENTS:
4288 case INT16_ELEMENTS:
4289 case UINT16_ELEMENTS:
4290 __ sh(value, mem_operand);
4292 case EXTERNAL_INT32_ELEMENTS:
4293 case EXTERNAL_UINT32_ELEMENTS:
4294 case INT32_ELEMENTS:
4295 case UINT32_ELEMENTS:
4296 __ sw(value, mem_operand);
4298 case INT32x4_ELEMENTS:
4299 case FLOAT32_ELEMENTS:
4300 case FLOAT32x4_ELEMENTS:
4301 case FLOAT64_ELEMENTS:
4302 case FLOAT64x2_ELEMENTS:
4303 case EXTERNAL_INT32x4_ELEMENTS:
4304 case EXTERNAL_FLOAT32_ELEMENTS:
4305 case EXTERNAL_FLOAT32x4_ELEMENTS:
4306 case EXTERNAL_FLOAT64_ELEMENTS:
4307 case EXTERNAL_FLOAT64x2_ELEMENTS:
4308 case FAST_DOUBLE_ELEMENTS:
4310 case FAST_SMI_ELEMENTS:
4311 case FAST_HOLEY_DOUBLE_ELEMENTS:
4312 case FAST_HOLEY_ELEMENTS:
4313 case FAST_HOLEY_SMI_ELEMENTS:
4314 case DICTIONARY_ELEMENTS:
4315 case SLOPPY_ARGUMENTS_ELEMENTS:
4323 void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
4324 DoubleRegister value = ToDoubleRegister(instr->value());
4325 Register elements = ToRegister(instr->elements());
4326 Register scratch = scratch0();
4327 DoubleRegister double_scratch = double_scratch0();
4328 bool key_is_constant = instr->key()->IsConstantOperand();
4329 int base_offset = instr->base_offset();
4330 Label not_nan, done;
4332 // Calculate the effective address of the slot in the array to store the
4334 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
4335 if (key_is_constant) {
4336 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4337 if (constant_key & 0xF0000000) {
4338 Abort(kArrayIndexConstantValueTooBig);
4340 __ Addu(scratch, elements,
4341 Operand((constant_key << element_size_shift) + base_offset));
4343 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4344 ? (element_size_shift - kSmiTagSize) : element_size_shift;
4345 __ Addu(scratch, elements, Operand(base_offset));
4346 __ sll(at, ToRegister(instr->key()), shift_size);
4347 __ Addu(scratch, scratch, at);
4350 if (instr->NeedsCanonicalization()) {
4352 // Check for NaN. All NaNs must be canonicalized.
4353 __ BranchF(NULL, &is_nan, eq, value, value);
4354 __ Branch(¬_nan);
4356 // Only load canonical NaN if the comparison above set the overflow.
4358 __ LoadRoot(at, Heap::kNanValueRootIndex);
4359 __ ldc1(double_scratch, FieldMemOperand(at, HeapNumber::kValueOffset));
4360 __ sdc1(double_scratch, MemOperand(scratch, 0));
4365 __ sdc1(value, MemOperand(scratch, 0));
4370 void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4371 Register value = ToRegister(instr->value());
4372 Register elements = ToRegister(instr->elements());
4373 Register key = instr->key()->IsRegister() ? ToRegister(instr->key())
4375 Register scratch = scratch0();
4376 Register store_base = scratch;
4377 int offset = instr->base_offset();
4380 if (instr->key()->IsConstantOperand()) {
4381 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4382 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
4383 offset += ToInteger32(const_operand) * kPointerSize;
4384 store_base = elements;
4386 // Even though the HLoadKeyed instruction forces the input
4387 // representation for the key to be an integer, the input gets replaced
4388 // during bound check elimination with the index argument to the bounds
4389 // check, which can be tagged, so that case must be handled here, too.
4390 if (instr->hydrogen()->key()->representation().IsSmi()) {
4391 __ sll(scratch, key, kPointerSizeLog2 - kSmiTagSize);
4392 __ addu(scratch, elements, scratch);
4394 __ sll(scratch, key, kPointerSizeLog2);
4395 __ addu(scratch, elements, scratch);
4398 __ sw(value, MemOperand(store_base, offset));
4400 if (instr->hydrogen()->NeedsWriteBarrier()) {
4401 SmiCheck check_needed =
4402 instr->hydrogen()->value()->type().IsHeapObject()
4403 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4404 // Compute address of modified element and store it into key register.
4405 __ Addu(key, store_base, Operand(offset));
4406 __ RecordWrite(elements,
4411 EMIT_REMEMBERED_SET,
4413 instr->hydrogen()->PointersToHereCheckForValue());
4418 void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4419 // By cases: external, fast double
4420 if (instr->is_typed_elements()) {
4421 DoStoreKeyedExternalArray(instr);
4422 } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4423 DoStoreKeyedFixedDoubleArray(instr);
4425 DoStoreKeyedFixedArray(instr);
4430 void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4431 DCHECK(ToRegister(instr->context()).is(cp));
4432 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4433 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4434 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4437 CodeFactory::KeyedStoreIC(isolate(), instr->strict_mode()).code();
4438 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4442 void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4443 Register object_reg = ToRegister(instr->object());
4444 Register scratch = scratch0();
4446 Handle<Map> from_map = instr->original_map();
4447 Handle<Map> to_map = instr->transitioned_map();
4448 ElementsKind from_kind = instr->from_kind();
4449 ElementsKind to_kind = instr->to_kind();
4451 Label not_applicable;
4452 __ lw(scratch, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4453 __ Branch(¬_applicable, ne, scratch, Operand(from_map));
4455 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
4456 Register new_map_reg = ToRegister(instr->new_map_temp());
4457 __ li(new_map_reg, Operand(to_map));
4458 __ sw(new_map_reg, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4460 __ RecordWriteForMap(object_reg,
4466 DCHECK(object_reg.is(a0));
4467 DCHECK(ToRegister(instr->context()).is(cp));
4468 PushSafepointRegistersScope scope(this);
4469 __ li(a1, Operand(to_map));
4470 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4471 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4473 RecordSafepointWithRegisters(
4474 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
4476 __ bind(¬_applicable);
4480 void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4481 Register object = ToRegister(instr->object());
4482 Register temp = ToRegister(instr->temp());
4483 Label no_memento_found;
4484 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found,
4485 ne, &no_memento_found);
4486 DeoptimizeIf(al, instr);
4487 __ bind(&no_memento_found);
4491 void LCodeGen::DoStringAdd(LStringAdd* instr) {
4492 DCHECK(ToRegister(instr->context()).is(cp));
4493 DCHECK(ToRegister(instr->left()).is(a1));
4494 DCHECK(ToRegister(instr->right()).is(a0));
4495 StringAddStub stub(isolate(),
4496 instr->hydrogen()->flags(),
4497 instr->hydrogen()->pretenure_flag());
4498 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4502 void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
4503 class DeferredStringCharCodeAt FINAL : public LDeferredCode {
4505 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
4506 : LDeferredCode(codegen), instr_(instr) { }
4507 virtual void Generate() OVERRIDE {
4508 codegen()->DoDeferredStringCharCodeAt(instr_);
4510 virtual LInstruction* instr() OVERRIDE { return instr_; }
4512 LStringCharCodeAt* instr_;
4515 DeferredStringCharCodeAt* deferred =
4516 new(zone()) DeferredStringCharCodeAt(this, instr);
4517 StringCharLoadGenerator::Generate(masm(),
4518 ToRegister(instr->string()),
4519 ToRegister(instr->index()),
4520 ToRegister(instr->result()),
4522 __ bind(deferred->exit());
4526 void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4527 Register string = ToRegister(instr->string());
4528 Register result = ToRegister(instr->result());
4529 Register scratch = scratch0();
4531 // TODO(3095996): Get rid of this. For now, we need to make the
4532 // result register contain a valid pointer because it is already
4533 // contained in the register pointer map.
4534 __ mov(result, zero_reg);
4536 PushSafepointRegistersScope scope(this);
4538 // Push the index as a smi. This is safe because of the checks in
4539 // DoStringCharCodeAt above.
4540 if (instr->index()->IsConstantOperand()) {
4541 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
4542 __ Addu(scratch, zero_reg, Operand(Smi::FromInt(const_index)));
4545 Register index = ToRegister(instr->index());
4549 CallRuntimeFromDeferred(Runtime::kStringCharCodeAtRT, 2, instr,
4553 __ StoreToSafepointRegisterSlot(v0, result);
4557 void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
4558 class DeferredStringCharFromCode FINAL : public LDeferredCode {
4560 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
4561 : LDeferredCode(codegen), instr_(instr) { }
4562 virtual void Generate() OVERRIDE {
4563 codegen()->DoDeferredStringCharFromCode(instr_);
4565 virtual LInstruction* instr() OVERRIDE { return instr_; }
4567 LStringCharFromCode* instr_;
4570 DeferredStringCharFromCode* deferred =
4571 new(zone()) DeferredStringCharFromCode(this, instr);
4573 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
4574 Register char_code = ToRegister(instr->char_code());
4575 Register result = ToRegister(instr->result());
4576 Register scratch = scratch0();
4577 DCHECK(!char_code.is(result));
4579 __ Branch(deferred->entry(), hi,
4580 char_code, Operand(String::kMaxOneByteCharCode));
4581 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
4582 __ sll(scratch, char_code, kPointerSizeLog2);
4583 __ Addu(result, result, scratch);
4584 __ lw(result, FieldMemOperand(result, FixedArray::kHeaderSize));
4585 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
4586 __ Branch(deferred->entry(), eq, result, Operand(scratch));
4587 __ bind(deferred->exit());
4591 void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4592 Register char_code = ToRegister(instr->char_code());
4593 Register result = ToRegister(instr->result());
4595 // TODO(3095996): Get rid of this. For now, we need to make the
4596 // result register contain a valid pointer because it is already
4597 // contained in the register pointer map.
4598 __ mov(result, zero_reg);
4600 PushSafepointRegistersScope scope(this);
4601 __ SmiTag(char_code);
4603 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr, instr->context());
4604 __ StoreToSafepointRegisterSlot(v0, result);
4608 void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
4609 LOperand* input = instr->value();
4610 DCHECK(input->IsRegister() || input->IsStackSlot());
4611 LOperand* output = instr->result();
4612 DCHECK(output->IsDoubleRegister());
4613 FPURegister single_scratch = double_scratch0().low();
4614 if (input->IsStackSlot()) {
4615 Register scratch = scratch0();
4616 __ lw(scratch, ToMemOperand(input));
4617 __ mtc1(scratch, single_scratch);
4619 __ mtc1(ToRegister(input), single_scratch);
4621 __ cvt_d_w(ToDoubleRegister(output), single_scratch);
4625 void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4626 LOperand* input = instr->value();
4627 LOperand* output = instr->result();
4629 FPURegister dbl_scratch = double_scratch0();
4630 __ mtc1(ToRegister(input), dbl_scratch);
4631 __ Cvt_d_uw(ToDoubleRegister(output), dbl_scratch, f22);
4635 void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
4636 class DeferredNumberTagI FINAL : public LDeferredCode {
4638 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
4639 : LDeferredCode(codegen), instr_(instr) { }
4640 virtual void Generate() OVERRIDE {
4641 codegen()->DoDeferredNumberTagIU(instr_,
4647 virtual LInstruction* instr() OVERRIDE { return instr_; }
4649 LNumberTagI* instr_;
4652 Register src = ToRegister(instr->value());
4653 Register dst = ToRegister(instr->result());
4654 Register overflow = scratch0();
4656 DeferredNumberTagI* deferred = new(zone()) DeferredNumberTagI(this, instr);
4657 __ SmiTagCheckOverflow(dst, src, overflow);
4658 __ BranchOnOverflow(deferred->entry(), overflow);
4659 __ bind(deferred->exit());
4663 void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4664 class DeferredNumberTagU FINAL : public LDeferredCode {
4666 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4667 : LDeferredCode(codegen), instr_(instr) { }
4668 virtual void Generate() OVERRIDE {
4669 codegen()->DoDeferredNumberTagIU(instr_,
4675 virtual LInstruction* instr() OVERRIDE { return instr_; }
4677 LNumberTagU* instr_;
4680 Register input = ToRegister(instr->value());
4681 Register result = ToRegister(instr->result());
4683 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4684 __ Branch(deferred->entry(), hi, input, Operand(Smi::kMaxValue));
4685 __ SmiTag(result, input);
4686 __ bind(deferred->exit());
4690 void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4694 IntegerSignedness signedness) {
4696 Register src = ToRegister(value);
4697 Register dst = ToRegister(instr->result());
4698 Register tmp1 = scratch0();
4699 Register tmp2 = ToRegister(temp1);
4700 Register tmp3 = ToRegister(temp2);
4701 DoubleRegister dbl_scratch = double_scratch0();
4703 if (signedness == SIGNED_INT32) {
4704 // There was overflow, so bits 30 and 31 of the original integer
4705 // disagree. Try to allocate a heap number in new space and store
4706 // the value in there. If that fails, call the runtime system.
4708 __ SmiUntag(src, dst);
4709 __ Xor(src, src, Operand(0x80000000));
4711 __ mtc1(src, dbl_scratch);
4712 __ cvt_d_w(dbl_scratch, dbl_scratch);
4714 __ mtc1(src, dbl_scratch);
4715 __ Cvt_d_uw(dbl_scratch, dbl_scratch, f22);
4718 if (FLAG_inline_new) {
4719 __ LoadRoot(tmp3, Heap::kHeapNumberMapRootIndex);
4720 __ AllocateHeapNumber(dst, tmp1, tmp2, tmp3, &slow, DONT_TAG_RESULT);
4724 // Slow case: Call the runtime system to do the number allocation.
4727 // TODO(3095996): Put a valid pointer value in the stack slot where the
4728 // result register is stored, as this register is in the pointer map, but
4729 // contains an integer value.
4730 __ mov(dst, zero_reg);
4732 // Preserve the value of all registers.
4733 PushSafepointRegistersScope scope(this);
4735 // NumberTagI and NumberTagD use the context from the frame, rather than
4736 // the environment's HContext or HInlinedContext value.
4737 // They only call Runtime::kAllocateHeapNumber.
4738 // The corresponding HChange instructions are added in a phase that does
4739 // not have easy access to the local context.
4740 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4741 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4742 RecordSafepointWithRegisters(
4743 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4744 __ Subu(v0, v0, kHeapObjectTag);
4745 __ StoreToSafepointRegisterSlot(v0, dst);
4749 // Done. Put the value in dbl_scratch into the value of the allocated heap
4752 __ sdc1(dbl_scratch, MemOperand(dst, HeapNumber::kValueOffset));
4753 __ Addu(dst, dst, kHeapObjectTag);
4757 void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
4758 class DeferredNumberTagD FINAL : public LDeferredCode {
4760 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4761 : LDeferredCode(codegen), instr_(instr) { }
4762 virtual void Generate() OVERRIDE {
4763 codegen()->DoDeferredNumberTagD(instr_);
4765 virtual LInstruction* instr() OVERRIDE { return instr_; }
4767 LNumberTagD* instr_;
4770 DoubleRegister input_reg = ToDoubleRegister(instr->value());
4771 Register scratch = scratch0();
4772 Register reg = ToRegister(instr->result());
4773 Register temp1 = ToRegister(instr->temp());
4774 Register temp2 = ToRegister(instr->temp2());
4776 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
4777 if (FLAG_inline_new) {
4778 __ LoadRoot(scratch, Heap::kHeapNumberMapRootIndex);
4779 // We want the untagged address first for performance
4780 __ AllocateHeapNumber(reg, temp1, temp2, scratch, deferred->entry(),
4783 __ Branch(deferred->entry());
4785 __ bind(deferred->exit());
4786 __ sdc1(input_reg, MemOperand(reg, HeapNumber::kValueOffset));
4787 // Now that we have finished with the object's real address tag it
4788 __ Addu(reg, reg, kHeapObjectTag);
4792 void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4793 // TODO(3095996): Get rid of this. For now, we need to make the
4794 // result register contain a valid pointer because it is already
4795 // contained in the register pointer map.
4796 Register reg = ToRegister(instr->result());
4797 __ mov(reg, zero_reg);
4799 PushSafepointRegistersScope scope(this);
4800 // NumberTagI and NumberTagD use the context from the frame, rather than
4801 // the environment's HContext or HInlinedContext value.
4802 // They only call Runtime::kAllocateHeapNumber.
4803 // The corresponding HChange instructions are added in a phase that does
4804 // not have easy access to the local context.
4805 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4806 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4807 RecordSafepointWithRegisters(
4808 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4809 __ Subu(v0, v0, kHeapObjectTag);
4810 __ StoreToSafepointRegisterSlot(v0, reg);
4814 void LCodeGen::DoSmiTag(LSmiTag* instr) {
4815 HChange* hchange = instr->hydrogen();
4816 Register input = ToRegister(instr->value());
4817 Register output = ToRegister(instr->result());
4818 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4819 hchange->value()->CheckFlag(HValue::kUint32)) {
4820 __ And(at, input, Operand(0xc0000000));
4821 DeoptimizeIf(ne, instr, "overflow", at, Operand(zero_reg));
4823 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4824 !hchange->value()->CheckFlag(HValue::kUint32)) {
4825 __ SmiTagCheckOverflow(output, input, at);
4826 DeoptimizeIf(lt, instr, "overflow", at, Operand(zero_reg));
4828 __ SmiTag(output, input);
4833 void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4834 Register scratch = scratch0();
4835 Register input = ToRegister(instr->value());
4836 Register result = ToRegister(instr->result());
4837 if (instr->needs_check()) {
4838 STATIC_ASSERT(kHeapObjectTag == 1);
4839 // If the input is a HeapObject, value of scratch won't be zero.
4840 __ And(scratch, input, Operand(kHeapObjectTag));
4841 __ SmiUntag(result, input);
4842 DeoptimizeIf(ne, instr, "not a Smi", scratch, Operand(zero_reg));
4844 __ SmiUntag(result, input);
4849 void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
4850 DoubleRegister result_reg,
4851 NumberUntagDMode mode) {
4852 bool can_convert_undefined_to_nan =
4853 instr->hydrogen()->can_convert_undefined_to_nan();
4854 bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
4856 Register scratch = scratch0();
4857 Label convert, load_smi, done;
4858 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4860 __ UntagAndJumpIfSmi(scratch, input_reg, &load_smi);
4861 // Heap number map check.
4862 __ lw(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
4863 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4864 if (can_convert_undefined_to_nan) {
4865 __ Branch(&convert, ne, scratch, Operand(at));
4867 DeoptimizeIf(ne, instr, "not a heap number", scratch, Operand(at));
4869 // Load heap number.
4870 __ ldc1(result_reg, FieldMemOperand(input_reg, HeapNumber::kValueOffset));
4871 if (deoptimize_on_minus_zero) {
4872 __ mfc1(at, result_reg.low());
4873 __ Branch(&done, ne, at, Operand(zero_reg));
4874 __ Mfhc1(scratch, result_reg);
4875 DeoptimizeIf(eq, instr, "minus zero", scratch,
4876 Operand(HeapNumber::kSignMask));
4879 if (can_convert_undefined_to_nan) {
4881 // Convert undefined (and hole) to NaN.
4882 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4883 DeoptimizeIf(ne, instr, "not a heap number/undefined", input_reg,
4885 __ LoadRoot(scratch, Heap::kNanValueRootIndex);
4886 __ ldc1(result_reg, FieldMemOperand(scratch, HeapNumber::kValueOffset));
4890 __ SmiUntag(scratch, input_reg);
4891 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
4893 // Smi to double register conversion
4895 // scratch: untagged value of input_reg
4896 __ mtc1(scratch, result_reg);
4897 __ cvt_d_w(result_reg, result_reg);
4902 void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) {
4903 Register input_reg = ToRegister(instr->value());
4904 Register scratch1 = scratch0();
4905 Register scratch2 = ToRegister(instr->temp());
4906 DoubleRegister double_scratch = double_scratch0();
4907 DoubleRegister double_scratch2 = ToDoubleRegister(instr->temp2());
4909 DCHECK(!scratch1.is(input_reg) && !scratch1.is(scratch2));
4910 DCHECK(!scratch2.is(input_reg) && !scratch2.is(scratch1));
4914 // The input is a tagged HeapObject.
4915 // Heap number map check.
4916 __ lw(scratch1, FieldMemOperand(input_reg, HeapObject::kMapOffset));
4917 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4918 // This 'at' value and scratch1 map value are used for tests in both clauses
4921 if (instr->truncating()) {
4922 // Performs a truncating conversion of a floating point number as used by
4923 // the JS bitwise operations.
4924 Label no_heap_number, check_bools, check_false;
4925 // Check HeapNumber map.
4926 __ Branch(USE_DELAY_SLOT, &no_heap_number, ne, scratch1, Operand(at));
4927 __ mov(scratch2, input_reg); // In delay slot.
4928 __ TruncateHeapNumberToI(input_reg, scratch2);
4931 // Check for Oddballs. Undefined/False is converted to zero and True to one
4932 // for truncating conversions.
4933 __ bind(&no_heap_number);
4934 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4935 __ Branch(&check_bools, ne, input_reg, Operand(at));
4936 DCHECK(ToRegister(instr->result()).is(input_reg));
4937 __ Branch(USE_DELAY_SLOT, &done);
4938 __ mov(input_reg, zero_reg); // In delay slot.
4940 __ bind(&check_bools);
4941 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4942 __ Branch(&check_false, ne, scratch2, Operand(at));
4943 __ Branch(USE_DELAY_SLOT, &done);
4944 __ li(input_reg, Operand(1)); // In delay slot.
4946 __ bind(&check_false);
4947 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4948 DeoptimizeIf(ne, instr, "not a heap number/undefined/true/false", scratch2,
4950 __ Branch(USE_DELAY_SLOT, &done);
4951 __ mov(input_reg, zero_reg); // In delay slot.
4953 DeoptimizeIf(ne, instr, "not a heap number", scratch1, Operand(at));
4955 // Load the double value.
4956 __ ldc1(double_scratch,
4957 FieldMemOperand(input_reg, HeapNumber::kValueOffset));
4959 Register except_flag = scratch2;
4960 __ EmitFPUTruncate(kRoundToZero,
4966 kCheckForInexactConversion);
4968 DeoptimizeIf(ne, instr, "lost precision or NaN", except_flag,
4971 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
4972 __ Branch(&done, ne, input_reg, Operand(zero_reg));
4974 __ Mfhc1(scratch1, double_scratch);
4975 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
4976 DeoptimizeIf(ne, instr, "minus zero", scratch1, Operand(zero_reg));
4983 void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
4984 class DeferredTaggedToI FINAL : public LDeferredCode {
4986 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
4987 : LDeferredCode(codegen), instr_(instr) { }
4988 virtual void Generate() OVERRIDE {
4989 codegen()->DoDeferredTaggedToI(instr_);
4991 virtual LInstruction* instr() OVERRIDE { return instr_; }
4996 LOperand* input = instr->value();
4997 DCHECK(input->IsRegister());
4998 DCHECK(input->Equals(instr->result()));
5000 Register input_reg = ToRegister(input);
5002 if (instr->hydrogen()->value()->representation().IsSmi()) {
5003 __ SmiUntag(input_reg);
5005 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
5007 // Let the deferred code handle the HeapObject case.
5008 __ JumpIfNotSmi(input_reg, deferred->entry());
5010 // Smi to int32 conversion.
5011 __ SmiUntag(input_reg);
5012 __ bind(deferred->exit());
5017 void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
5018 LOperand* input = instr->value();
5019 DCHECK(input->IsRegister());
5020 LOperand* result = instr->result();
5021 DCHECK(result->IsDoubleRegister());
5023 Register input_reg = ToRegister(input);
5024 DoubleRegister result_reg = ToDoubleRegister(result);
5026 HValue* value = instr->hydrogen()->value();
5027 NumberUntagDMode mode = value->representation().IsSmi()
5028 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
5030 EmitNumberUntagD(instr, input_reg, result_reg, mode);
5034 void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
5035 Register result_reg = ToRegister(instr->result());
5036 Register scratch1 = scratch0();
5037 DoubleRegister double_input = ToDoubleRegister(instr->value());
5039 if (instr->truncating()) {
5040 __ TruncateDoubleToI(result_reg, double_input);
5042 Register except_flag = LCodeGen::scratch1();
5044 __ EmitFPUTruncate(kRoundToMinusInf,
5050 kCheckForInexactConversion);
5052 // Deopt if the operation did not succeed (except_flag != 0).
5053 DeoptimizeIf(ne, instr, "lost precision or NaN", except_flag,
5056 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5058 __ Branch(&done, ne, result_reg, Operand(zero_reg));
5059 __ Mfhc1(scratch1, double_input);
5060 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
5061 DeoptimizeIf(ne, instr, "minus zero", scratch1, Operand(zero_reg));
5068 void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
5069 Register result_reg = ToRegister(instr->result());
5070 Register scratch1 = LCodeGen::scratch0();
5071 DoubleRegister double_input = ToDoubleRegister(instr->value());
5073 if (instr->truncating()) {
5074 __ TruncateDoubleToI(result_reg, double_input);
5076 Register except_flag = LCodeGen::scratch1();
5078 __ EmitFPUTruncate(kRoundToMinusInf,
5084 kCheckForInexactConversion);
5086 // Deopt if the operation did not succeed (except_flag != 0).
5087 DeoptimizeIf(ne, instr, "lost precision or NaN", except_flag,
5090 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5092 __ Branch(&done, ne, result_reg, Operand(zero_reg));
5093 __ Mfhc1(scratch1, double_input);
5094 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
5095 DeoptimizeIf(ne, instr, "minus zero", scratch1, Operand(zero_reg));
5099 __ SmiTagCheckOverflow(result_reg, result_reg, scratch1);
5100 DeoptimizeIf(lt, instr, "overflow", scratch1, Operand(zero_reg));
5104 void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
5105 LOperand* input = instr->value();
5106 __ SmiTst(ToRegister(input), at);
5107 DeoptimizeIf(ne, instr, "not a Smi", at, Operand(zero_reg));
5111 void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
5112 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
5113 LOperand* input = instr->value();
5114 __ SmiTst(ToRegister(input), at);
5115 DeoptimizeIf(eq, instr, "Smi", at, Operand(zero_reg));
5120 void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
5121 Register input = ToRegister(instr->value());
5122 Register scratch = scratch0();
5124 __ GetObjectType(input, scratch, scratch);
5126 if (instr->hydrogen()->is_interval_check()) {
5129 instr->hydrogen()->GetCheckInterval(&first, &last);
5131 // If there is only one type in the interval check for equality.
5132 if (first == last) {
5133 DeoptimizeIf(ne, instr, "wrong instance type", scratch, Operand(first));
5135 DeoptimizeIf(lo, instr, "wrong instance type", scratch, Operand(first));
5136 // Omit check for the last type.
5137 if (last != LAST_TYPE) {
5138 DeoptimizeIf(hi, instr, "wrong instance type", scratch, Operand(last));
5144 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
5146 if (base::bits::IsPowerOfTwo32(mask)) {
5147 DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
5148 __ And(at, scratch, mask);
5149 DeoptimizeIf(tag == 0 ? ne : eq, instr, "wrong instance type", at,
5152 __ And(scratch, scratch, Operand(mask));
5153 DeoptimizeIf(ne, instr, "wrong instance type", scratch, Operand(tag));
5159 void LCodeGen::DoCheckValue(LCheckValue* instr) {
5160 Register reg = ToRegister(instr->value());
5161 Handle<HeapObject> object = instr->hydrogen()->object().handle();
5162 AllowDeferredHandleDereference smi_check;
5163 if (isolate()->heap()->InNewSpace(*object)) {
5164 Register reg = ToRegister(instr->value());
5165 Handle<Cell> cell = isolate()->factory()->NewCell(object);
5166 __ li(at, Operand(Handle<Object>(cell)));
5167 __ lw(at, FieldMemOperand(at, Cell::kValueOffset));
5168 DeoptimizeIf(ne, instr, "value mismatch", reg, Operand(at));
5170 DeoptimizeIf(ne, instr, "value mismatch", reg, Operand(object));
5175 void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
5177 PushSafepointRegistersScope scope(this);
5179 __ mov(cp, zero_reg);
5180 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
5181 RecordSafepointWithRegisters(
5182 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
5183 __ StoreToSafepointRegisterSlot(v0, scratch0());
5185 __ SmiTst(scratch0(), at);
5186 DeoptimizeIf(eq, instr, "instance migration failed", at, Operand(zero_reg));
5190 void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
5191 class DeferredCheckMaps FINAL : public LDeferredCode {
5193 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
5194 : LDeferredCode(codegen), instr_(instr), object_(object) {
5195 SetExit(check_maps());
5197 virtual void Generate() OVERRIDE {
5198 codegen()->DoDeferredInstanceMigration(instr_, object_);
5200 Label* check_maps() { return &check_maps_; }
5201 virtual LInstruction* instr() OVERRIDE { return instr_; }
5208 if (instr->hydrogen()->IsStabilityCheck()) {
5209 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5210 for (int i = 0; i < maps->size(); ++i) {
5211 AddStabilityDependency(maps->at(i).handle());
5216 Register map_reg = scratch0();
5217 LOperand* input = instr->value();
5218 DCHECK(input->IsRegister());
5219 Register reg = ToRegister(input);
5220 __ lw(map_reg, FieldMemOperand(reg, HeapObject::kMapOffset));
5222 DeferredCheckMaps* deferred = NULL;
5223 if (instr->hydrogen()->HasMigrationTarget()) {
5224 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5225 __ bind(deferred->check_maps());
5228 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5230 for (int i = 0; i < maps->size() - 1; i++) {
5231 Handle<Map> map = maps->at(i).handle();
5232 __ CompareMapAndBranch(map_reg, map, &success, eq, &success);
5234 Handle<Map> map = maps->at(maps->size() - 1).handle();
5235 // Do the CompareMap() directly within the Branch() and DeoptimizeIf().
5236 if (instr->hydrogen()->HasMigrationTarget()) {
5237 __ Branch(deferred->entry(), ne, map_reg, Operand(map));
5239 DeoptimizeIf(ne, instr, "wrong map", map_reg, Operand(map));
5246 void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
5247 DoubleRegister value_reg = ToDoubleRegister(instr->unclamped());
5248 Register result_reg = ToRegister(instr->result());
5249 DoubleRegister temp_reg = ToDoubleRegister(instr->temp());
5250 __ ClampDoubleToUint8(result_reg, value_reg, temp_reg);
5254 void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
5255 Register unclamped_reg = ToRegister(instr->unclamped());
5256 Register result_reg = ToRegister(instr->result());
5257 __ ClampUint8(result_reg, unclamped_reg);
5261 void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
5262 Register scratch = scratch0();
5263 Register input_reg = ToRegister(instr->unclamped());
5264 Register result_reg = ToRegister(instr->result());
5265 DoubleRegister temp_reg = ToDoubleRegister(instr->temp());
5266 Label is_smi, done, heap_number;
5268 // Both smi and heap number cases are handled.
5269 __ UntagAndJumpIfSmi(scratch, input_reg, &is_smi);
5271 // Check for heap number
5272 __ lw(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
5273 __ Branch(&heap_number, eq, scratch, Operand(factory()->heap_number_map()));
5275 // Check for undefined. Undefined is converted to zero for clamping
5277 DeoptimizeIf(ne, instr, "not a heap number/undefined", input_reg,
5278 Operand(factory()->undefined_value()));
5279 __ mov(result_reg, zero_reg);
5283 __ bind(&heap_number);
5284 __ ldc1(double_scratch0(), FieldMemOperand(input_reg,
5285 HeapNumber::kValueOffset));
5286 __ ClampDoubleToUint8(result_reg, double_scratch0(), temp_reg);
5290 __ ClampUint8(result_reg, scratch);
5296 void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5297 DoubleRegister value_reg = ToDoubleRegister(instr->value());
5298 Register result_reg = ToRegister(instr->result());
5299 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5300 __ FmoveHigh(result_reg, value_reg);
5302 __ FmoveLow(result_reg, value_reg);
5307 void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5308 Register hi_reg = ToRegister(instr->hi());
5309 Register lo_reg = ToRegister(instr->lo());
5310 DoubleRegister result_reg = ToDoubleRegister(instr->result());
5311 __ Move(result_reg, lo_reg, hi_reg);
5315 void LCodeGen::DoAllocate(LAllocate* instr) {
5316 class DeferredAllocate FINAL : public LDeferredCode {
5318 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
5319 : LDeferredCode(codegen), instr_(instr) { }
5320 virtual void Generate() OVERRIDE {
5321 codegen()->DoDeferredAllocate(instr_);
5323 virtual LInstruction* instr() OVERRIDE { return instr_; }
5328 DeferredAllocate* deferred =
5329 new(zone()) DeferredAllocate(this, instr);
5331 Register result = ToRegister(instr->result());
5332 Register scratch = ToRegister(instr->temp1());
5333 Register scratch2 = ToRegister(instr->temp2());
5335 // Allocate memory for the object.
5336 AllocationFlags flags = TAG_OBJECT;
5337 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5338 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5340 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5341 DCHECK(!instr->hydrogen()->IsOldDataSpaceAllocation());
5342 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5343 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_POINTER_SPACE);
5344 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5345 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5346 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_DATA_SPACE);
5348 if (instr->size()->IsConstantOperand()) {
5349 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5350 if (size <= Page::kMaxRegularHeapObjectSize) {
5351 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5353 __ jmp(deferred->entry());
5356 Register size = ToRegister(instr->size());
5357 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5360 __ bind(deferred->exit());
5362 if (instr->hydrogen()->MustPrefillWithFiller()) {
5363 STATIC_ASSERT(kHeapObjectTag == 1);
5364 if (instr->size()->IsConstantOperand()) {
5365 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5366 __ li(scratch, Operand(size - kHeapObjectTag));
5368 __ Subu(scratch, ToRegister(instr->size()), Operand(kHeapObjectTag));
5370 __ li(scratch2, Operand(isolate()->factory()->one_pointer_filler_map()));
5373 __ Subu(scratch, scratch, Operand(kPointerSize));
5374 __ Addu(at, result, Operand(scratch));
5375 __ sw(scratch2, MemOperand(at));
5376 __ Branch(&loop, ge, scratch, Operand(zero_reg));
5381 void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
5382 Register result = ToRegister(instr->result());
5384 // TODO(3095996): Get rid of this. For now, we need to make the
5385 // result register contain a valid pointer because it is already
5386 // contained in the register pointer map.
5387 __ mov(result, zero_reg);
5389 PushSafepointRegistersScope scope(this);
5390 if (instr->size()->IsRegister()) {
5391 Register size = ToRegister(instr->size());
5392 DCHECK(!size.is(result));
5396 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5397 if (size >= 0 && size <= Smi::kMaxValue) {
5398 __ Push(Smi::FromInt(size));
5400 // We should never get here at runtime => abort
5401 __ stop("invalid allocation size");
5406 int flags = AllocateDoubleAlignFlag::encode(
5407 instr->hydrogen()->MustAllocateDoubleAligned());
5408 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5409 DCHECK(!instr->hydrogen()->IsOldDataSpaceAllocation());
5410 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5411 flags = AllocateTargetSpace::update(flags, OLD_POINTER_SPACE);
5412 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5413 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5414 flags = AllocateTargetSpace::update(flags, OLD_DATA_SPACE);
5416 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5418 __ Push(Smi::FromInt(flags));
5420 CallRuntimeFromDeferred(
5421 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
5422 __ StoreToSafepointRegisterSlot(v0, result);
5426 void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
5427 DCHECK(ToRegister(instr->value()).is(a0));
5428 DCHECK(ToRegister(instr->result()).is(v0));
5430 CallRuntime(Runtime::kToFastProperties, 1, instr);
5434 void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
5435 DCHECK(ToRegister(instr->context()).is(cp));
5437 // Registers will be used as follows:
5438 // t3 = literals array.
5439 // a1 = regexp literal.
5440 // a0 = regexp literal clone.
5441 // a2 and t0-t2 are used as temporaries.
5442 int literal_offset =
5443 FixedArray::OffsetOfElementAt(instr->hydrogen()->literal_index());
5444 __ li(t3, instr->hydrogen()->literals());
5445 __ lw(a1, FieldMemOperand(t3, literal_offset));
5446 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5447 __ Branch(&materialized, ne, a1, Operand(at));
5449 // Create regexp literal using runtime function
5450 // Result will be in v0.
5451 __ li(t2, Operand(Smi::FromInt(instr->hydrogen()->literal_index())));
5452 __ li(t1, Operand(instr->hydrogen()->pattern()));
5453 __ li(t0, Operand(instr->hydrogen()->flags()));
5454 __ Push(t3, t2, t1, t0);
5455 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
5458 __ bind(&materialized);
5459 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
5460 Label allocated, runtime_allocate;
5462 __ Allocate(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
5465 __ bind(&runtime_allocate);
5466 __ li(a0, Operand(Smi::FromInt(size)));
5468 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
5471 __ bind(&allocated);
5472 // Copy the content into the newly allocated memory.
5473 // (Unroll copy loop once for better throughput).
5474 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
5475 __ lw(a3, FieldMemOperand(a1, i));
5476 __ lw(a2, FieldMemOperand(a1, i + kPointerSize));
5477 __ sw(a3, FieldMemOperand(v0, i));
5478 __ sw(a2, FieldMemOperand(v0, i + kPointerSize));
5480 if ((size % (2 * kPointerSize)) != 0) {
5481 __ lw(a3, FieldMemOperand(a1, size - kPointerSize));
5482 __ sw(a3, FieldMemOperand(v0, size - kPointerSize));
5487 void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
5488 DCHECK(ToRegister(instr->context()).is(cp));
5489 // Use the fast case closure allocation code that allocates in new
5490 // space for nested functions that don't need literals cloning.
5491 bool pretenure = instr->hydrogen()->pretenure();
5492 if (!pretenure && instr->hydrogen()->has_no_literals()) {
5493 FastNewClosureStub stub(isolate(), instr->hydrogen()->strict_mode(),
5494 instr->hydrogen()->kind());
5495 __ li(a2, Operand(instr->hydrogen()->shared_info()));
5496 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5498 __ li(a2, Operand(instr->hydrogen()->shared_info()));
5499 __ li(a1, Operand(pretenure ? factory()->true_value()
5500 : factory()->false_value()));
5501 __ Push(cp, a2, a1);
5502 CallRuntime(Runtime::kNewClosure, 3, instr);
5507 void LCodeGen::DoTypeof(LTypeof* instr) {
5508 DCHECK(ToRegister(instr->result()).is(v0));
5509 Register input = ToRegister(instr->value());
5511 CallRuntime(Runtime::kTypeof, 1, instr);
5515 void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5516 Register input = ToRegister(instr->value());
5518 Register cmp1 = no_reg;
5519 Operand cmp2 = Operand(no_reg);
5521 Condition final_branch_condition = EmitTypeofIs(instr->TrueLabel(chunk_),
5522 instr->FalseLabel(chunk_),
5524 instr->type_literal(),
5528 DCHECK(cmp1.is_valid());
5529 DCHECK(!cmp2.is_reg() || cmp2.rm().is_valid());
5531 if (final_branch_condition != kNoCondition) {
5532 EmitBranch(instr, final_branch_condition, cmp1, cmp2);
5537 Condition LCodeGen::EmitTypeofIs(Label* true_label,
5540 Handle<String> type_name,
5543 // This function utilizes the delay slot heavily. This is used to load
5544 // values that are always usable without depending on the type of the input
5546 Condition final_branch_condition = kNoCondition;
5547 Register scratch = scratch0();
5548 Factory* factory = isolate()->factory();
5549 if (String::Equals(type_name, factory->number_string())) {
5550 __ JumpIfSmi(input, true_label);
5551 __ lw(input, FieldMemOperand(input, HeapObject::kMapOffset));
5552 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5554 *cmp2 = Operand(at);
5555 final_branch_condition = eq;
5557 } else if (String::Equals(type_name, factory->string_string())) {
5558 __ JumpIfSmi(input, false_label);
5559 __ GetObjectType(input, input, scratch);
5560 __ Branch(USE_DELAY_SLOT, false_label,
5561 ge, scratch, Operand(FIRST_NONSTRING_TYPE));
5562 // input is an object so we can load the BitFieldOffset even if we take the
5564 __ lbu(at, FieldMemOperand(input, Map::kBitFieldOffset));
5565 __ And(at, at, 1 << Map::kIsUndetectable);
5567 *cmp2 = Operand(zero_reg);
5568 final_branch_condition = eq;
5570 } else if (String::Equals(type_name, factory->symbol_string())) {
5571 __ JumpIfSmi(input, false_label);
5572 __ GetObjectType(input, input, scratch);
5574 *cmp2 = Operand(SYMBOL_TYPE);
5575 final_branch_condition = eq;
5577 } else if (String::Equals(type_name, factory->boolean_string())) {
5578 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5579 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5580 __ LoadRoot(at, Heap::kFalseValueRootIndex);
5582 *cmp2 = Operand(input);
5583 final_branch_condition = eq;
5585 } else if (String::Equals(type_name, factory->undefined_string())) {
5586 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5587 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5588 // The first instruction of JumpIfSmi is an And - it is safe in the delay
5590 __ JumpIfSmi(input, false_label);
5591 // Check for undetectable objects => true.
5592 __ lw(input, FieldMemOperand(input, HeapObject::kMapOffset));
5593 __ lbu(at, FieldMemOperand(input, Map::kBitFieldOffset));
5594 __ And(at, at, 1 << Map::kIsUndetectable);
5596 *cmp2 = Operand(zero_reg);
5597 final_branch_condition = ne;
5599 } else if (String::Equals(type_name, factory->function_string())) {
5600 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5601 __ JumpIfSmi(input, false_label);
5602 __ GetObjectType(input, scratch, input);
5603 __ Branch(true_label, eq, input, Operand(JS_FUNCTION_TYPE));
5605 *cmp2 = Operand(JS_FUNCTION_PROXY_TYPE);
5606 final_branch_condition = eq;
5608 } else if (String::Equals(type_name, factory->object_string())) {
5609 __ JumpIfSmi(input, false_label);
5610 __ LoadRoot(at, Heap::kNullValueRootIndex);
5611 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5612 Register map = input;
5613 __ GetObjectType(input, map, scratch);
5614 __ Branch(false_label,
5615 lt, scratch, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
5616 __ Branch(USE_DELAY_SLOT, false_label,
5617 gt, scratch, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
5618 // map is still valid, so the BitField can be loaded in delay slot.
5619 // Check for undetectable objects => false.
5620 __ lbu(at, FieldMemOperand(map, Map::kBitFieldOffset));
5621 __ And(at, at, 1 << Map::kIsUndetectable);
5623 *cmp2 = Operand(zero_reg);
5624 final_branch_condition = eq;
5628 *cmp2 = Operand(zero_reg); // Set to valid regs, to avoid caller assertion.
5629 __ Branch(false_label);
5632 return final_branch_condition;
5636 void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
5637 Register temp1 = ToRegister(instr->temp());
5639 EmitIsConstructCall(temp1, scratch0());
5641 EmitBranch(instr, eq, temp1,
5642 Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
5646 void LCodeGen::EmitIsConstructCall(Register temp1, Register temp2) {
5647 DCHECK(!temp1.is(temp2));
5648 // Get the frame pointer for the calling frame.
5649 __ lw(temp1, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5651 // Skip the arguments adaptor frame if it exists.
5652 Label check_frame_marker;
5653 __ lw(temp2, MemOperand(temp1, StandardFrameConstants::kContextOffset));
5654 __ Branch(&check_frame_marker, ne, temp2,
5655 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5656 __ lw(temp1, MemOperand(temp1, StandardFrameConstants::kCallerFPOffset));
5658 // Check the marker in the calling frame.
5659 __ bind(&check_frame_marker);
5660 __ lw(temp1, MemOperand(temp1, StandardFrameConstants::kMarkerOffset));
5664 void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5665 if (!info()->IsStub()) {
5666 // Ensure that we have enough space after the previous lazy-bailout
5667 // instruction for patching the code here.
5668 int current_pc = masm()->pc_offset();
5669 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5670 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5671 DCHECK_EQ(0, padding_size % Assembler::kInstrSize);
5672 while (padding_size > 0) {
5674 padding_size -= Assembler::kInstrSize;
5678 last_lazy_deopt_pc_ = masm()->pc_offset();
5682 void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
5683 last_lazy_deopt_pc_ = masm()->pc_offset();
5684 DCHECK(instr->HasEnvironment());
5685 LEnvironment* env = instr->environment();
5686 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5687 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5691 void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
5692 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5693 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5694 // needed return address), even though the implementation of LAZY and EAGER is
5695 // now identical. When LAZY is eventually completely folded into EAGER, remove
5696 // the special case below.
5697 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5698 type = Deoptimizer::LAZY;
5701 DeoptimizeIf(al, instr, type, instr->hydrogen()->reason(), zero_reg,
5706 void LCodeGen::DoDummy(LDummy* instr) {
5707 // Nothing to see here, move on!
5711 void LCodeGen::DoDummyUse(LDummyUse* instr) {
5712 // Nothing to see here, move on!
5716 void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
5717 PushSafepointRegistersScope scope(this);
5718 LoadContextFromDeferred(instr->context());
5719 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5720 RecordSafepointWithLazyDeopt(
5721 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
5722 DCHECK(instr->HasEnvironment());
5723 LEnvironment* env = instr->environment();
5724 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5728 void LCodeGen::DoStackCheck(LStackCheck* instr) {
5729 class DeferredStackCheck FINAL : public LDeferredCode {
5731 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5732 : LDeferredCode(codegen), instr_(instr) { }
5733 virtual void Generate() OVERRIDE {
5734 codegen()->DoDeferredStackCheck(instr_);
5736 virtual LInstruction* instr() OVERRIDE { return instr_; }
5738 LStackCheck* instr_;
5741 DCHECK(instr->HasEnvironment());
5742 LEnvironment* env = instr->environment();
5743 // There is no LLazyBailout instruction for stack-checks. We have to
5744 // prepare for lazy deoptimization explicitly here.
5745 if (instr->hydrogen()->is_function_entry()) {
5746 // Perform stack overflow check.
5748 __ LoadRoot(at, Heap::kStackLimitRootIndex);
5749 __ Branch(&done, hs, sp, Operand(at));
5750 DCHECK(instr->context()->IsRegister());
5751 DCHECK(ToRegister(instr->context()).is(cp));
5752 CallCode(isolate()->builtins()->StackCheck(),
5753 RelocInfo::CODE_TARGET,
5757 DCHECK(instr->hydrogen()->is_backwards_branch());
5758 // Perform stack overflow check if this goto needs it before jumping.
5759 DeferredStackCheck* deferred_stack_check =
5760 new(zone()) DeferredStackCheck(this, instr);
5761 __ LoadRoot(at, Heap::kStackLimitRootIndex);
5762 __ Branch(deferred_stack_check->entry(), lo, sp, Operand(at));
5763 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
5764 __ bind(instr->done_label());
5765 deferred_stack_check->SetExit(instr->done_label());
5766 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5767 // Don't record a deoptimization index for the safepoint here.
5768 // This will be done explicitly when emitting call and the safepoint in
5769 // the deferred code.
5774 void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
5775 // This is a pseudo-instruction that ensures that the environment here is
5776 // properly registered for deoptimization and records the assembler's PC
5778 LEnvironment* environment = instr->environment();
5780 // If the environment were already registered, we would have no way of
5781 // backpatching it with the spill slot operands.
5782 DCHECK(!environment->HasBeenRegistered());
5783 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
5785 GenerateOsrPrologue();
5789 void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
5790 Register result = ToRegister(instr->result());
5791 Register object = ToRegister(instr->object());
5792 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5793 DeoptimizeIf(eq, instr, "undefined", object, Operand(at));
5795 Register null_value = t1;
5796 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
5797 DeoptimizeIf(eq, instr, "null", object, Operand(null_value));
5799 __ And(at, object, kSmiTagMask);
5800 DeoptimizeIf(eq, instr, "Smi", at, Operand(zero_reg));
5802 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
5803 __ GetObjectType(object, a1, a1);
5804 DeoptimizeIf(le, instr, "not a JavaScript object", a1,
5805 Operand(LAST_JS_PROXY_TYPE));
5807 Label use_cache, call_runtime;
5808 DCHECK(object.is(a0));
5809 __ CheckEnumCache(null_value, &call_runtime);
5811 __ lw(result, FieldMemOperand(object, HeapObject::kMapOffset));
5812 __ Branch(&use_cache);
5814 // Get the set of properties to enumerate.
5815 __ bind(&call_runtime);
5817 CallRuntime(Runtime::kGetPropertyNamesFast, 1, instr);
5819 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
5820 DCHECK(result.is(v0));
5821 __ LoadRoot(at, Heap::kMetaMapRootIndex);
5822 DeoptimizeIf(ne, instr, "wrong map", a1, Operand(at));
5823 __ bind(&use_cache);
5827 void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5828 Register map = ToRegister(instr->map());
5829 Register result = ToRegister(instr->result());
5830 Label load_cache, done;
5831 __ EnumLength(result, map);
5832 __ Branch(&load_cache, ne, result, Operand(Smi::FromInt(0)));
5833 __ li(result, Operand(isolate()->factory()->empty_fixed_array()));
5836 __ bind(&load_cache);
5837 __ LoadInstanceDescriptors(map, result);
5839 FieldMemOperand(result, DescriptorArray::kEnumCacheOffset));
5841 FieldMemOperand(result, FixedArray::SizeFor(instr->idx())));
5842 DeoptimizeIf(eq, instr, "no cache", result, Operand(zero_reg));
5848 void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5849 Register object = ToRegister(instr->value());
5850 Register map = ToRegister(instr->map());
5851 __ lw(scratch0(), FieldMemOperand(object, HeapObject::kMapOffset));
5852 DeoptimizeIf(ne, instr, "wrong map", map, Operand(scratch0()));
5856 void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5860 PushSafepointRegistersScope scope(this);
5861 __ Push(object, index);
5862 __ mov(cp, zero_reg);
5863 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
5864 RecordSafepointWithRegisters(
5865 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
5866 __ StoreToSafepointRegisterSlot(v0, result);
5870 void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
5871 class DeferredLoadMutableDouble FINAL : public LDeferredCode {
5873 DeferredLoadMutableDouble(LCodeGen* codegen,
5874 LLoadFieldByIndex* instr,
5878 : LDeferredCode(codegen),
5884 virtual void Generate() OVERRIDE {
5885 codegen()->DoDeferredLoadMutableDouble(instr_, result_, object_, index_);
5887 virtual LInstruction* instr() OVERRIDE { return instr_; }
5889 LLoadFieldByIndex* instr_;
5895 Register object = ToRegister(instr->object());
5896 Register index = ToRegister(instr->index());
5897 Register result = ToRegister(instr->result());
5898 Register scratch = scratch0();
5900 DeferredLoadMutableDouble* deferred;
5901 deferred = new(zone()) DeferredLoadMutableDouble(
5902 this, instr, result, object, index);
5904 Label out_of_object, done;
5906 __ And(scratch, index, Operand(Smi::FromInt(1)));
5907 __ Branch(deferred->entry(), ne, scratch, Operand(zero_reg));
5908 __ sra(index, index, 1);
5910 __ Branch(USE_DELAY_SLOT, &out_of_object, lt, index, Operand(zero_reg));
5911 __ sll(scratch, index, kPointerSizeLog2 - kSmiTagSize); // In delay slot.
5913 STATIC_ASSERT(kPointerSizeLog2 > kSmiTagSize);
5914 __ Addu(scratch, object, scratch);
5915 __ lw(result, FieldMemOperand(scratch, JSObject::kHeaderSize));
5919 __ bind(&out_of_object);
5920 __ lw(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
5921 // Index is equal to negated out of object property index plus 1.
5922 __ Subu(scratch, result, scratch);
5923 __ lw(result, FieldMemOperand(scratch,
5924 FixedArray::kHeaderSize - kPointerSize));
5925 __ bind(deferred->exit());
5930 void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
5931 Register context = ToRegister(instr->context());
5932 __ sw(context, MemOperand(fp, StandardFrameConstants::kContextOffset));
5936 void LCodeGen::DoAllocateBlockContext(LAllocateBlockContext* instr) {
5937 Handle<ScopeInfo> scope_info = instr->scope_info();
5938 __ li(at, scope_info);
5939 __ Push(at, ToRegister(instr->function()));
5940 CallRuntime(Runtime::kPushBlockContext, 2, instr);
5941 RecordSafepoint(Safepoint::kNoLazyDeopt);
5947 } } // namespace v8::internal