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 "mips/lithium-codegen-mips.h"
31 #include "mips/lithium-gap-resolver-mips.h"
32 #include "code-stubs.h"
33 #include "stub-cache.h"
34 #include "hydrogen-osr.h"
40 class SafepointGenerator V8_FINAL : public CallWrapper {
42 SafepointGenerator(LCodeGen* codegen,
43 LPointerMap* pointers,
44 Safepoint::DeoptMode mode)
48 virtual ~SafepointGenerator() {}
50 virtual void BeforeCall(int call_size) const V8_OVERRIDE {}
52 virtual void AfterCall() const V8_OVERRIDE {
53 codegen_->RecordSafepoint(pointers_, deopt_mode_);
58 LPointerMap* pointers_;
59 Safepoint::DeoptMode deopt_mode_;
65 bool LCodeGen::GenerateCode() {
66 LPhase phase("Z_Code generation", chunk());
70 // Open a frame scope to indicate that there is a frame on the stack. The
71 // NONE indicates that the scope shouldn't actually generate code to set up
72 // the frame (that is done in GeneratePrologue).
73 FrameScope frame_scope(masm_, StackFrame::NONE);
75 return GeneratePrologue() &&
77 GenerateDeferredCode() &&
78 GenerateDeoptJumpTable() &&
79 GenerateSafepointTable();
83 void LCodeGen::FinishCode(Handle<Code> code) {
85 code->set_stack_slots(GetStackSlotCount());
86 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
87 RegisterDependentCodeForEmbeddedMaps(code);
88 PopulateDeoptimizationData(code);
89 info()->CommitDependencies(code);
93 void LChunkBuilder::Abort(BailoutReason reason) {
94 info()->set_bailout_reason(reason);
99 void LCodeGen::SaveCallerDoubles() {
100 ASSERT(info()->saves_caller_doubles());
101 ASSERT(NeedsEagerFrame());
102 Comment(";;; Save clobbered callee double registers");
104 BitVector* doubles = chunk()->allocated_double_registers();
105 BitVector::Iterator save_iterator(doubles);
106 while (!save_iterator.Done()) {
107 __ sdc1(DoubleRegister::FromAllocationIndex(save_iterator.Current()),
108 MemOperand(sp, count * kDoubleSize));
109 save_iterator.Advance();
115 void LCodeGen::RestoreCallerDoubles() {
116 ASSERT(info()->saves_caller_doubles());
117 ASSERT(NeedsEagerFrame());
118 Comment(";;; Restore clobbered callee double registers");
119 BitVector* doubles = chunk()->allocated_double_registers();
120 BitVector::Iterator save_iterator(doubles);
122 while (!save_iterator.Done()) {
123 __ ldc1(DoubleRegister::FromAllocationIndex(save_iterator.Current()),
124 MemOperand(sp, count * kDoubleSize));
125 save_iterator.Advance();
131 bool LCodeGen::GeneratePrologue() {
132 ASSERT(is_generating());
134 if (info()->IsOptimizing()) {
135 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
138 if (strlen(FLAG_stop_at) > 0 &&
139 info_->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
144 // a1: Callee's JS function.
145 // cp: Callee's context.
146 // fp: Caller's frame pointer.
149 // Classic mode functions and builtins need to replace the receiver with the
150 // global proxy when called as functions (without an explicit receiver
152 if (info_->this_has_uses() &&
153 info_->is_classic_mode() &&
154 !info_->is_native()) {
156 int receiver_offset = info_->scope()->num_parameters() * kPointerSize;
157 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
158 __ lw(a2, MemOperand(sp, receiver_offset));
159 __ Branch(&ok, ne, a2, Operand(at));
161 __ lw(a2, GlobalObjectOperand());
162 __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalReceiverOffset));
164 __ sw(a2, MemOperand(sp, receiver_offset));
170 info()->set_prologue_offset(masm_->pc_offset());
171 if (NeedsEagerFrame()) {
172 __ Prologue(info()->IsStub() ? BUILD_STUB_FRAME : BUILD_FUNCTION_FRAME);
173 frame_is_built_ = true;
174 info_->AddNoFrameRange(0, masm_->pc_offset());
177 // Reserve space for the stack slots needed by the code.
178 int slots = GetStackSlotCount();
180 if (FLAG_debug_code) {
181 __ Subu(sp, sp, Operand(slots * kPointerSize));
184 __ Addu(a0, sp, Operand(slots * kPointerSize));
185 __ li(a1, Operand(kSlotsZapValue));
188 __ Subu(a0, a0, Operand(kPointerSize));
189 __ sw(a1, MemOperand(a0, 2 * kPointerSize));
190 __ Branch(&loop, ne, a0, Operand(sp));
194 __ Subu(sp, sp, Operand(slots * kPointerSize));
198 if (info()->saves_caller_doubles()) {
202 // Possibly allocate a local context.
203 int heap_slots = info()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
204 if (heap_slots > 0) {
205 Comment(";;; Allocate local context");
206 // Argument to NewContext is the function, which is in a1.
207 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
208 FastNewContextStub stub(heap_slots);
212 __ CallRuntime(Runtime::kNewFunctionContext, 1);
214 RecordSafepoint(Safepoint::kNoLazyDeopt);
215 // Context is returned in both v0. It replaces the context passed to us.
216 // It's saved in the stack and kept live in cp.
218 __ sw(v0, MemOperand(fp, StandardFrameConstants::kContextOffset));
219 // Copy any necessary parameters into the context.
220 int num_parameters = scope()->num_parameters();
221 for (int i = 0; i < num_parameters; i++) {
222 Variable* var = scope()->parameter(i);
223 if (var->IsContextSlot()) {
224 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
225 (num_parameters - 1 - i) * kPointerSize;
226 // Load parameter from stack.
227 __ lw(a0, MemOperand(fp, parameter_offset));
228 // Store it in the context.
229 MemOperand target = ContextOperand(cp, var->index());
231 // Update the write barrier. This clobbers a3 and a0.
232 __ RecordWriteContextSlot(
233 cp, target.offset(), a0, a3, GetRAState(), kSaveFPRegs);
236 Comment(";;; End allocate local context");
240 if (FLAG_trace && info()->IsOptimizing()) {
241 // We have not executed any compiled code yet, so cp still holds the
243 __ CallRuntime(Runtime::kTraceEnter, 0);
245 return !is_aborted();
249 void LCodeGen::GenerateOsrPrologue() {
250 // Generate the OSR entry prologue at the first unknown OSR value, or if there
251 // are none, at the OSR entrypoint instruction.
252 if (osr_pc_offset_ >= 0) return;
254 osr_pc_offset_ = masm()->pc_offset();
256 // Adjust the frame size, subsuming the unoptimized frame into the
258 int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
260 __ Subu(sp, sp, Operand(slots * kPointerSize));
264 bool LCodeGen::GenerateDeferredCode() {
265 ASSERT(is_generating());
266 if (deferred_.length() > 0) {
267 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
268 LDeferredCode* code = deferred_[i];
271 instructions_->at(code->instruction_index())->hydrogen_value();
272 RecordAndWritePosition(value->position());
274 Comment(";;; <@%d,#%d> "
275 "-------------------- Deferred %s --------------------",
276 code->instruction_index(),
277 code->instr()->hydrogen_value()->id(),
278 code->instr()->Mnemonic());
279 __ bind(code->entry());
280 if (NeedsDeferredFrame()) {
281 Comment(";;; Build frame");
282 ASSERT(!frame_is_built_);
283 ASSERT(info()->IsStub());
284 frame_is_built_ = true;
285 __ MultiPush(cp.bit() | fp.bit() | ra.bit());
286 __ li(scratch0(), Operand(Smi::FromInt(StackFrame::STUB)));
288 __ Addu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
289 Comment(";;; Deferred code");
292 if (NeedsDeferredFrame()) {
293 Comment(";;; Destroy frame");
294 ASSERT(frame_is_built_);
296 __ MultiPop(cp.bit() | fp.bit() | ra.bit());
297 frame_is_built_ = false;
299 __ jmp(code->exit());
302 // Deferred code is the last part of the instruction sequence. Mark
303 // the generated code as done unless we bailed out.
304 if (!is_aborted()) status_ = DONE;
305 return !is_aborted();
309 bool LCodeGen::GenerateDeoptJumpTable() {
310 if (deopt_jump_table_.length() > 0) {
311 Comment(";;; -------------------- Jump table --------------------");
313 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
315 __ bind(&table_start);
317 for (int i = 0; i < deopt_jump_table_.length(); i++) {
318 __ bind(&deopt_jump_table_[i].label);
319 Address entry = deopt_jump_table_[i].address;
320 Deoptimizer::BailoutType type = deopt_jump_table_[i].bailout_type;
321 int id = Deoptimizer::GetDeoptimizationId(isolate(), entry, type);
322 if (id == Deoptimizer::kNotDeoptimizationEntry) {
323 Comment(";;; jump table entry %d.", i);
325 Comment(";;; jump table entry %d: deoptimization bailout %d.", i, id);
327 __ li(t9, Operand(ExternalReference::ForDeoptEntry(entry)));
328 if (deopt_jump_table_[i].needs_frame) {
329 ASSERT(!info()->saves_caller_doubles());
330 if (needs_frame.is_bound()) {
331 __ Branch(&needs_frame);
333 __ bind(&needs_frame);
334 __ MultiPush(cp.bit() | fp.bit() | ra.bit());
335 // This variant of deopt can only be used with stubs. Since we don't
336 // have a function pointer to install in the stack frame that we're
337 // building, install a special marker there instead.
338 ASSERT(info()->IsStub());
339 __ li(scratch0(), Operand(Smi::FromInt(StackFrame::STUB)));
341 __ Addu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
345 if (info()->saves_caller_doubles()) {
346 ASSERT(info()->IsStub());
347 RestoreCallerDoubles();
352 __ RecordComment("]");
354 // The deoptimization jump table is the last part of the instruction
355 // sequence. Mark the generated code as done unless we bailed out.
356 if (!is_aborted()) status_ = DONE;
357 return !is_aborted();
361 bool LCodeGen::GenerateSafepointTable() {
363 safepoints_.Emit(masm(), GetStackSlotCount());
364 return !is_aborted();
368 Register LCodeGen::ToRegister(int index) const {
369 return Register::FromAllocationIndex(index);
373 DoubleRegister LCodeGen::ToDoubleRegister(int index) const {
374 return DoubleRegister::FromAllocationIndex(index);
378 Register LCodeGen::ToRegister(LOperand* op) const {
379 ASSERT(op->IsRegister());
380 return ToRegister(op->index());
384 Register LCodeGen::EmitLoadRegister(LOperand* op, Register scratch) {
385 if (op->IsRegister()) {
386 return ToRegister(op->index());
387 } else if (op->IsConstantOperand()) {
388 LConstantOperand* const_op = LConstantOperand::cast(op);
389 HConstant* constant = chunk_->LookupConstant(const_op);
390 Handle<Object> literal = constant->handle(isolate());
391 Representation r = chunk_->LookupLiteralRepresentation(const_op);
392 if (r.IsInteger32()) {
393 ASSERT(literal->IsNumber());
394 __ li(scratch, Operand(static_cast<int32_t>(literal->Number())));
395 } else if (r.IsSmi()) {
396 ASSERT(constant->HasSmiValue());
397 __ li(scratch, Operand(Smi::FromInt(constant->Integer32Value())));
398 } else if (r.IsDouble()) {
399 Abort(kEmitLoadRegisterUnsupportedDoubleImmediate);
401 ASSERT(r.IsSmiOrTagged());
402 __ li(scratch, literal);
405 } else if (op->IsStackSlot() || op->IsArgument()) {
406 __ lw(scratch, ToMemOperand(op));
414 DoubleRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
415 ASSERT(op->IsDoubleRegister());
416 return ToDoubleRegister(op->index());
420 DoubleRegister LCodeGen::EmitLoadDoubleRegister(LOperand* op,
421 FloatRegister flt_scratch,
422 DoubleRegister dbl_scratch) {
423 if (op->IsDoubleRegister()) {
424 return ToDoubleRegister(op->index());
425 } else if (op->IsConstantOperand()) {
426 LConstantOperand* const_op = LConstantOperand::cast(op);
427 HConstant* constant = chunk_->LookupConstant(const_op);
428 Handle<Object> literal = constant->handle(isolate());
429 Representation r = chunk_->LookupLiteralRepresentation(const_op);
430 if (r.IsInteger32()) {
431 ASSERT(literal->IsNumber());
432 __ li(at, Operand(static_cast<int32_t>(literal->Number())));
433 __ mtc1(at, flt_scratch);
434 __ cvt_d_w(dbl_scratch, flt_scratch);
436 } else if (r.IsDouble()) {
437 Abort(kUnsupportedDoubleImmediate);
438 } else if (r.IsTagged()) {
439 Abort(kUnsupportedTaggedImmediate);
441 } else if (op->IsStackSlot() || op->IsArgument()) {
442 MemOperand mem_op = ToMemOperand(op);
443 __ ldc1(dbl_scratch, mem_op);
451 Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
452 HConstant* constant = chunk_->LookupConstant(op);
453 ASSERT(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
454 return constant->handle(isolate());
458 bool LCodeGen::IsInteger32(LConstantOperand* op) const {
459 return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
463 bool LCodeGen::IsSmi(LConstantOperand* op) const {
464 return chunk_->LookupLiteralRepresentation(op).IsSmi();
468 int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
469 return ToRepresentation(op, Representation::Integer32());
473 int32_t LCodeGen::ToRepresentation(LConstantOperand* op,
474 const Representation& r) const {
475 HConstant* constant = chunk_->LookupConstant(op);
476 int32_t value = constant->Integer32Value();
477 if (r.IsInteger32()) return value;
478 ASSERT(r.IsSmiOrTagged());
479 return reinterpret_cast<int32_t>(Smi::FromInt(value));
483 Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
484 HConstant* constant = chunk_->LookupConstant(op);
485 return Smi::FromInt(constant->Integer32Value());
489 double LCodeGen::ToDouble(LConstantOperand* op) const {
490 HConstant* constant = chunk_->LookupConstant(op);
491 ASSERT(constant->HasDoubleValue());
492 return constant->DoubleValue();
496 Operand LCodeGen::ToOperand(LOperand* op) {
497 if (op->IsConstantOperand()) {
498 LConstantOperand* const_op = LConstantOperand::cast(op);
499 HConstant* constant = chunk()->LookupConstant(const_op);
500 Representation r = chunk_->LookupLiteralRepresentation(const_op);
502 ASSERT(constant->HasSmiValue());
503 return Operand(Smi::FromInt(constant->Integer32Value()));
504 } else if (r.IsInteger32()) {
505 ASSERT(constant->HasInteger32Value());
506 return Operand(constant->Integer32Value());
507 } else if (r.IsDouble()) {
508 Abort(kToOperandUnsupportedDoubleImmediate);
510 ASSERT(r.IsTagged());
511 return Operand(constant->handle(isolate()));
512 } else if (op->IsRegister()) {
513 return Operand(ToRegister(op));
514 } else if (op->IsDoubleRegister()) {
515 Abort(kToOperandIsDoubleRegisterUnimplemented);
518 // Stack slots not implemented, use ToMemOperand instead.
524 static int ArgumentsOffsetWithoutFrame(int index) {
526 return -(index + 1) * kPointerSize;
530 MemOperand LCodeGen::ToMemOperand(LOperand* op) const {
531 ASSERT(!op->IsRegister());
532 ASSERT(!op->IsDoubleRegister());
533 ASSERT(op->IsStackSlot() || op->IsDoubleStackSlot());
534 if (NeedsEagerFrame()) {
535 return MemOperand(fp, StackSlotOffset(op->index()));
537 // Retrieve parameter without eager stack-frame relative to the
539 return MemOperand(sp, ArgumentsOffsetWithoutFrame(op->index()));
544 MemOperand LCodeGen::ToHighMemOperand(LOperand* op) const {
545 ASSERT(op->IsDoubleStackSlot());
546 if (NeedsEagerFrame()) {
547 return MemOperand(fp, StackSlotOffset(op->index()) + kPointerSize);
549 // Retrieve parameter without eager stack-frame relative to the
552 sp, ArgumentsOffsetWithoutFrame(op->index()) + kPointerSize);
557 void LCodeGen::WriteTranslation(LEnvironment* environment,
558 Translation* translation) {
559 if (environment == NULL) return;
561 // The translation includes one command per value in the environment.
562 int translation_size = environment->translation_size();
563 // The output frame height does not include the parameters.
564 int height = translation_size - environment->parameter_count();
566 WriteTranslation(environment->outer(), translation);
567 bool has_closure_id = !info()->closure().is_null() &&
568 !info()->closure().is_identical_to(environment->closure());
569 int closure_id = has_closure_id
570 ? DefineDeoptimizationLiteral(environment->closure())
571 : Translation::kSelfLiteralId;
573 switch (environment->frame_type()) {
575 translation->BeginJSFrame(environment->ast_id(), closure_id, height);
578 translation->BeginConstructStubFrame(closure_id, translation_size);
581 ASSERT(translation_size == 1);
583 translation->BeginGetterStubFrame(closure_id);
586 ASSERT(translation_size == 2);
588 translation->BeginSetterStubFrame(closure_id);
591 translation->BeginCompiledStubFrame();
593 case ARGUMENTS_ADAPTOR:
594 translation->BeginArgumentsAdaptorFrame(closure_id, translation_size);
598 int object_index = 0;
599 int dematerialized_index = 0;
600 for (int i = 0; i < translation_size; ++i) {
601 LOperand* value = environment->values()->at(i);
602 AddToTranslation(environment,
605 environment->HasTaggedValueAt(i),
606 environment->HasUint32ValueAt(i),
608 &dematerialized_index);
613 void LCodeGen::AddToTranslation(LEnvironment* environment,
614 Translation* translation,
618 int* object_index_pointer,
619 int* dematerialized_index_pointer) {
620 if (op == LEnvironment::materialization_marker()) {
621 int object_index = (*object_index_pointer)++;
622 if (environment->ObjectIsDuplicateAt(object_index)) {
623 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
624 translation->DuplicateObject(dupe_of);
627 int object_length = environment->ObjectLengthAt(object_index);
628 if (environment->ObjectIsArgumentsAt(object_index)) {
629 translation->BeginArgumentsObject(object_length);
631 translation->BeginCapturedObject(object_length);
633 int dematerialized_index = *dematerialized_index_pointer;
634 int env_offset = environment->translation_size() + dematerialized_index;
635 *dematerialized_index_pointer += object_length;
636 for (int i = 0; i < object_length; ++i) {
637 LOperand* value = environment->values()->at(env_offset + i);
638 AddToTranslation(environment,
641 environment->HasTaggedValueAt(env_offset + i),
642 environment->HasUint32ValueAt(env_offset + i),
643 object_index_pointer,
644 dematerialized_index_pointer);
649 if (op->IsStackSlot()) {
651 translation->StoreStackSlot(op->index());
652 } else if (is_uint32) {
653 translation->StoreUint32StackSlot(op->index());
655 translation->StoreInt32StackSlot(op->index());
657 } else if (op->IsDoubleStackSlot()) {
658 translation->StoreDoubleStackSlot(op->index());
659 } else if (op->IsArgument()) {
661 int src_index = GetStackSlotCount() + op->index();
662 translation->StoreStackSlot(src_index);
663 } else if (op->IsRegister()) {
664 Register reg = ToRegister(op);
666 translation->StoreRegister(reg);
667 } else if (is_uint32) {
668 translation->StoreUint32Register(reg);
670 translation->StoreInt32Register(reg);
672 } else if (op->IsDoubleRegister()) {
673 DoubleRegister reg = ToDoubleRegister(op);
674 translation->StoreDoubleRegister(reg);
675 } else if (op->IsConstantOperand()) {
676 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
677 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
678 translation->StoreLiteral(src_index);
685 void LCodeGen::CallCode(Handle<Code> code,
686 RelocInfo::Mode mode,
687 LInstruction* instr) {
688 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT);
692 void LCodeGen::CallCodeGeneric(Handle<Code> code,
693 RelocInfo::Mode mode,
695 SafepointMode safepoint_mode) {
696 ASSERT(instr != NULL);
698 RecordSafepointWithLazyDeopt(instr, safepoint_mode);
702 void LCodeGen::CallRuntime(const Runtime::Function* function,
705 SaveFPRegsMode save_doubles) {
706 ASSERT(instr != NULL);
708 __ CallRuntime(function, num_arguments, save_doubles);
710 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
714 void LCodeGen::LoadContextFromDeferred(LOperand* context) {
715 if (context->IsRegister()) {
716 __ Move(cp, ToRegister(context));
717 } else if (context->IsStackSlot()) {
718 __ lw(cp, ToMemOperand(context));
719 } else if (context->IsConstantOperand()) {
720 HConstant* constant =
721 chunk_->LookupConstant(LConstantOperand::cast(context));
722 __ li(cp, Handle<Object>::cast(constant->handle(isolate())));
729 void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
733 LoadContextFromDeferred(context);
734 __ CallRuntimeSaveDoubles(id);
735 RecordSafepointWithRegisters(
736 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
740 void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
741 Safepoint::DeoptMode mode) {
742 if (!environment->HasBeenRegistered()) {
743 // Physical stack frame layout:
744 // -x ............. -4 0 ..................................... y
745 // [incoming arguments] [spill slots] [pushed outgoing arguments]
747 // Layout of the environment:
748 // 0 ..................................................... size-1
749 // [parameters] [locals] [expression stack including arguments]
751 // Layout of the translation:
752 // 0 ........................................................ size - 1 + 4
753 // [expression stack including arguments] [locals] [4 words] [parameters]
754 // |>------------ translation_size ------------<|
757 int jsframe_count = 0;
758 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
760 if (e->frame_type() == JS_FUNCTION) {
764 Translation translation(&translations_, frame_count, jsframe_count, zone());
765 WriteTranslation(environment, &translation);
766 int deoptimization_index = deoptimizations_.length();
767 int pc_offset = masm()->pc_offset();
768 environment->Register(deoptimization_index,
770 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
771 deoptimizations_.Add(environment, zone());
776 void LCodeGen::DeoptimizeIf(Condition condition,
777 LEnvironment* environment,
778 Deoptimizer::BailoutType bailout_type,
780 const Operand& src2) {
781 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
782 ASSERT(environment->HasBeenRegistered());
783 int id = environment->deoptimization_index();
784 ASSERT(info()->IsOptimizing() || info()->IsStub());
786 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
788 Abort(kBailoutWasNotPrepared);
792 if (FLAG_deopt_every_n_times != 0 && !info()->IsStub()) {
793 Register scratch = scratch0();
794 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
796 __ Push(a1, scratch);
797 __ li(scratch, Operand(count));
798 __ lw(a1, MemOperand(scratch));
799 __ Subu(a1, a1, Operand(1));
800 __ Branch(&no_deopt, ne, a1, Operand(zero_reg));
801 __ li(a1, Operand(FLAG_deopt_every_n_times));
802 __ sw(a1, MemOperand(scratch));
805 __ Call(entry, RelocInfo::RUNTIME_ENTRY);
807 __ sw(a1, MemOperand(scratch));
811 if (info()->ShouldTrapOnDeopt()) {
813 if (condition != al) {
814 __ Branch(&skip, NegateCondition(condition), src1, src2);
816 __ stop("trap_on_deopt");
820 ASSERT(info()->IsStub() || frame_is_built_);
821 // Go through jump table if we need to handle condition, build frame, or
822 // restore caller doubles.
823 if (condition == al && frame_is_built_ &&
824 !info()->saves_caller_doubles()) {
825 __ Call(entry, RelocInfo::RUNTIME_ENTRY, condition, src1, src2);
827 // We often have several deopts to the same entry, reuse the last
828 // jump entry if this is the case.
829 if (deopt_jump_table_.is_empty() ||
830 (deopt_jump_table_.last().address != entry) ||
831 (deopt_jump_table_.last().bailout_type != bailout_type) ||
832 (deopt_jump_table_.last().needs_frame != !frame_is_built_)) {
833 Deoptimizer::JumpTableEntry table_entry(entry,
836 deopt_jump_table_.Add(table_entry, zone());
838 __ Branch(&deopt_jump_table_.last().label, condition, src1, src2);
843 void LCodeGen::DeoptimizeIf(Condition condition,
844 LEnvironment* environment,
846 const Operand& src2) {
847 Deoptimizer::BailoutType bailout_type = info()->IsStub()
849 : Deoptimizer::EAGER;
850 DeoptimizeIf(condition, environment, bailout_type, src1, src2);
854 void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
855 int length = deoptimizations_.length();
856 if (length == 0) return;
857 Handle<DeoptimizationInputData> data =
858 factory()->NewDeoptimizationInputData(length, TENURED);
860 Handle<ByteArray> translations =
861 translations_.CreateByteArray(isolate()->factory());
862 data->SetTranslationByteArray(*translations);
863 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
865 Handle<FixedArray> literals =
866 factory()->NewFixedArray(deoptimization_literals_.length(), TENURED);
867 { AllowDeferredHandleDereference copy_handles;
868 for (int i = 0; i < deoptimization_literals_.length(); i++) {
869 literals->set(i, *deoptimization_literals_[i]);
871 data->SetLiteralArray(*literals);
874 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id().ToInt()));
875 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
877 // Populate the deoptimization entries.
878 for (int i = 0; i < length; i++) {
879 LEnvironment* env = deoptimizations_[i];
880 data->SetAstId(i, env->ast_id());
881 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
882 data->SetArgumentsStackHeight(i,
883 Smi::FromInt(env->arguments_stack_height()));
884 data->SetPc(i, Smi::FromInt(env->pc_offset()));
886 code->set_deoptimization_data(*data);
890 int LCodeGen::DefineDeoptimizationLiteral(Handle<Object> literal) {
891 int result = deoptimization_literals_.length();
892 for (int i = 0; i < deoptimization_literals_.length(); ++i) {
893 if (deoptimization_literals_[i].is_identical_to(literal)) return i;
895 deoptimization_literals_.Add(literal, zone());
900 void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
901 ASSERT(deoptimization_literals_.length() == 0);
903 const ZoneList<Handle<JSFunction> >* inlined_closures =
904 chunk()->inlined_closures();
906 for (int i = 0, length = inlined_closures->length();
909 DefineDeoptimizationLiteral(inlined_closures->at(i));
912 inlined_function_count_ = deoptimization_literals_.length();
916 void LCodeGen::RecordSafepointWithLazyDeopt(
917 LInstruction* instr, SafepointMode safepoint_mode) {
918 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
919 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
921 ASSERT(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
922 RecordSafepointWithRegisters(
923 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
928 void LCodeGen::RecordSafepoint(
929 LPointerMap* pointers,
930 Safepoint::Kind kind,
932 Safepoint::DeoptMode deopt_mode) {
933 ASSERT(expected_safepoint_kind_ == kind);
935 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
936 Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
937 kind, arguments, deopt_mode);
938 for (int i = 0; i < operands->length(); i++) {
939 LOperand* pointer = operands->at(i);
940 if (pointer->IsStackSlot()) {
941 safepoint.DefinePointerSlot(pointer->index(), zone());
942 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
943 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
949 void LCodeGen::RecordSafepoint(LPointerMap* pointers,
950 Safepoint::DeoptMode deopt_mode) {
951 RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
955 void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
956 LPointerMap empty_pointers(zone());
957 RecordSafepoint(&empty_pointers, deopt_mode);
961 void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
963 Safepoint::DeoptMode deopt_mode) {
965 pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
969 void LCodeGen::RecordSafepointWithRegistersAndDoubles(
970 LPointerMap* pointers,
972 Safepoint::DeoptMode deopt_mode) {
974 pointers, Safepoint::kWithRegistersAndDoubles, arguments, deopt_mode);
978 void LCodeGen::RecordAndWritePosition(int position) {
979 if (position == RelocInfo::kNoPosition) return;
980 masm()->positions_recorder()->RecordPosition(position);
981 masm()->positions_recorder()->WriteRecordedPositions();
985 static const char* LabelType(LLabel* label) {
986 if (label->is_loop_header()) return " (loop header)";
987 if (label->is_osr_entry()) return " (OSR entry)";
992 void LCodeGen::DoLabel(LLabel* label) {
993 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
994 current_instruction_,
995 label->hydrogen_value()->id(),
998 __ bind(label->label());
999 current_block_ = label->block_id();
1004 void LCodeGen::DoParallelMove(LParallelMove* move) {
1005 resolver_.Resolve(move);
1009 void LCodeGen::DoGap(LGap* gap) {
1010 for (int i = LGap::FIRST_INNER_POSITION;
1011 i <= LGap::LAST_INNER_POSITION;
1013 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
1014 LParallelMove* move = gap->GetParallelMove(inner_pos);
1015 if (move != NULL) DoParallelMove(move);
1020 void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
1025 void LCodeGen::DoParameter(LParameter* instr) {
1030 void LCodeGen::DoCallStub(LCallStub* instr) {
1031 ASSERT(ToRegister(instr->context()).is(cp));
1032 ASSERT(ToRegister(instr->result()).is(v0));
1033 switch (instr->hydrogen()->major_key()) {
1034 case CodeStub::RegExpConstructResult: {
1035 RegExpConstructResultStub stub;
1036 CallCode(stub.GetCode(isolate()), RelocInfo::CODE_TARGET, instr);
1039 case CodeStub::RegExpExec: {
1040 RegExpExecStub stub;
1041 CallCode(stub.GetCode(isolate()), RelocInfo::CODE_TARGET, instr);
1044 case CodeStub::SubString: {
1046 CallCode(stub.GetCode(isolate()), RelocInfo::CODE_TARGET, instr);
1049 case CodeStub::StringCompare: {
1050 StringCompareStub stub;
1051 CallCode(stub.GetCode(isolate()), RelocInfo::CODE_TARGET, instr);
1060 void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
1061 GenerateOsrPrologue();
1065 void LCodeGen::DoModI(LModI* instr) {
1066 HMod* hmod = instr->hydrogen();
1067 HValue* left = hmod->left();
1068 HValue* right = hmod->right();
1069 if (hmod->RightIsPowerOf2()) {
1070 const Register left_reg = ToRegister(instr->left());
1071 const Register result_reg = ToRegister(instr->result());
1073 // Note: The code below even works when right contains kMinInt.
1074 int32_t divisor = Abs(right->GetInteger32Constant());
1076 Label left_is_not_negative, done;
1077 if (left->CanBeNegative()) {
1078 __ Branch(left_reg.is(result_reg) ? PROTECT : USE_DELAY_SLOT,
1079 &left_is_not_negative, ge, left_reg, Operand(zero_reg));
1080 __ subu(result_reg, zero_reg, left_reg);
1081 __ And(result_reg, result_reg, divisor - 1);
1082 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1083 DeoptimizeIf(eq, instr->environment(), result_reg, Operand(zero_reg));
1085 __ Branch(USE_DELAY_SLOT, &done);
1086 __ subu(result_reg, zero_reg, result_reg);
1089 __ bind(&left_is_not_negative);
1090 __ And(result_reg, left_reg, divisor - 1);
1093 const Register scratch = scratch0();
1094 const Register left_reg = ToRegister(instr->left());
1095 const Register result_reg = ToRegister(instr->result());
1097 // div runs in the background while we check for special cases.
1098 Register right_reg = EmitLoadRegister(instr->right(), scratch);
1099 __ div(left_reg, right_reg);
1102 // Check for x % 0, we have to deopt in this case because we can't return a
1104 if (right->CanBeZero()) {
1105 DeoptimizeIf(eq, instr->environment(), right_reg, Operand(zero_reg));
1108 // Check for kMinInt % -1, we have to deopt if we care about -0, because we
1109 // can't return that.
1110 if (left->RangeCanInclude(kMinInt) && right->RangeCanInclude(-1)) {
1111 Label left_not_min_int;
1112 __ Branch(&left_not_min_int, ne, left_reg, Operand(kMinInt));
1113 // TODO(svenpanne) Don't deopt when we don't care about -0.
1114 DeoptimizeIf(eq, instr->environment(), right_reg, Operand(-1));
1115 __ bind(&left_not_min_int);
1118 // TODO(svenpanne) Only emit the test/deopt if we have to.
1119 __ Branch(USE_DELAY_SLOT, &done, ge, left_reg, Operand(zero_reg));
1120 __ mfhi(result_reg);
1122 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1123 DeoptimizeIf(eq, instr->environment(), result_reg, Operand(zero_reg));
1130 void LCodeGen::EmitSignedIntegerDivisionByConstant(
1136 LEnvironment* environment) {
1137 ASSERT(!AreAliased(dividend, scratch, at, no_reg));
1139 uint32_t divisor_abs = abs(divisor);
1141 int32_t power_of_2_factor =
1142 CompilerIntrinsics::CountTrailingZeros(divisor_abs);
1144 switch (divisor_abs) {
1146 DeoptimizeIf(al, environment);
1151 __ Move(result, dividend);
1153 __ SubuAndCheckForOverflow(result, zero_reg, dividend, scratch);
1154 DeoptimizeIf(lt, environment, scratch, Operand(zero_reg));
1156 // Compute the remainder.
1157 __ Move(remainder, zero_reg);
1161 if (IsPowerOf2(divisor_abs)) {
1162 // Branch and condition free code for integer division by a power
1164 int32_t power = WhichPowerOf2(divisor_abs);
1166 __ sra(scratch, dividend, power - 1);
1168 __ srl(scratch, scratch, 32 - power);
1169 __ Addu(scratch, dividend, Operand(scratch));
1170 __ sra(result, scratch, power);
1171 // Negate if necessary.
1172 // We don't need to check for overflow because the case '-1' is
1173 // handled separately.
1175 ASSERT(divisor != -1);
1176 __ Subu(result, zero_reg, Operand(result));
1178 // Compute the remainder.
1180 __ sll(scratch, result, power);
1181 __ Subu(remainder, dividend, Operand(scratch));
1183 __ sll(scratch, result, power);
1184 __ Addu(remainder, dividend, Operand(scratch));
1187 } else if (LChunkBuilder::HasMagicNumberForDivisor(divisor)) {
1188 // Use magic numbers for a few specific divisors.
1189 // Details and proofs can be found in:
1190 // - Hacker's Delight, Henry S. Warren, Jr.
1191 // - The PowerPC Compiler Writer's Guide
1192 // and probably many others.
1195 // <divisor with magic numbers> * <power of 2>
1197 // <divisor with magic numbers> * <other divisor with magic numbers>
1198 DivMagicNumbers magic_numbers =
1199 DivMagicNumberFor(divisor_abs >> power_of_2_factor);
1200 // Branch and condition free code for integer division by a power
1202 const int32_t M = magic_numbers.M;
1203 const int32_t s = magic_numbers.s + power_of_2_factor;
1205 __ li(scratch, Operand(M));
1206 __ mult(dividend, scratch);
1209 __ Addu(scratch, scratch, Operand(dividend));
1212 __ sra(scratch, scratch, s);
1213 __ mov(scratch, scratch);
1215 __ srl(at, dividend, 31);
1216 __ Addu(result, scratch, Operand(at));
1217 if (divisor < 0) __ Subu(result, zero_reg, Operand(result));
1218 // Compute the remainder.
1219 __ li(scratch, Operand(divisor));
1220 __ Mul(scratch, result, Operand(scratch));
1221 __ Subu(remainder, dividend, Operand(scratch));
1223 __ li(scratch, Operand(divisor));
1224 __ div(dividend, scratch);
1232 void LCodeGen::DoDivI(LDivI* instr) {
1233 const Register left = ToRegister(instr->left());
1234 const Register right = ToRegister(instr->right());
1235 const Register result = ToRegister(instr->result());
1237 // On MIPS div is asynchronous - it will run in the background while we
1238 // check for special cases.
1239 __ div(left, right);
1242 if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) {
1243 DeoptimizeIf(eq, instr->environment(), right, Operand(zero_reg));
1246 // Check for (0 / -x) that will produce negative zero.
1247 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1248 Label left_not_zero;
1249 __ Branch(&left_not_zero, ne, left, Operand(zero_reg));
1250 DeoptimizeIf(lt, instr->environment(), right, Operand(zero_reg));
1251 __ bind(&left_not_zero);
1254 // Check for (kMinInt / -1).
1255 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1256 Label left_not_min_int;
1257 __ Branch(&left_not_min_int, ne, left, Operand(kMinInt));
1258 DeoptimizeIf(eq, instr->environment(), right, Operand(-1));
1259 __ bind(&left_not_min_int);
1262 if (!instr->hydrogen()->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1264 DeoptimizeIf(ne, instr->environment(), result, Operand(zero_reg));
1270 void LCodeGen::DoMultiplyAddD(LMultiplyAddD* instr) {
1271 DoubleRegister addend = ToDoubleRegister(instr->addend());
1272 DoubleRegister multiplier = ToDoubleRegister(instr->multiplier());
1273 DoubleRegister multiplicand = ToDoubleRegister(instr->multiplicand());
1275 // This is computed in-place.
1276 ASSERT(addend.is(ToDoubleRegister(instr->result())));
1278 __ madd_d(addend, addend, multiplier, multiplicand);
1282 void LCodeGen::DoMathFloorOfDiv(LMathFloorOfDiv* instr) {
1283 const Register result = ToRegister(instr->result());
1284 const Register left = ToRegister(instr->left());
1285 const Register remainder = ToRegister(instr->temp());
1286 const Register scratch = scratch0();
1288 if (instr->right()->IsConstantOperand()) {
1290 int32_t divisor = ToInteger32(LConstantOperand::cast(instr->right()));
1292 DeoptimizeIf(eq, instr->environment(), left, Operand(zero_reg));
1294 EmitSignedIntegerDivisionByConstant(result,
1299 instr->environment());
1300 // We performed a truncating division. Correct the result if necessary.
1301 __ Branch(&done, eq, remainder, Operand(zero_reg), USE_DELAY_SLOT);
1302 __ Xor(scratch , remainder, Operand(divisor));
1303 __ Branch(&done, ge, scratch, Operand(zero_reg));
1304 __ Subu(result, result, Operand(1));
1308 const Register right = ToRegister(instr->right());
1310 // On MIPS div is asynchronous - it will run in the background while we
1311 // check for special cases.
1312 __ div(left, right);
1315 DeoptimizeIf(eq, instr->environment(), right, Operand(zero_reg));
1317 // Check for (0 / -x) that will produce negative zero.
1318 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1319 Label left_not_zero;
1320 __ Branch(&left_not_zero, ne, left, Operand(zero_reg));
1321 DeoptimizeIf(lt, instr->environment(), right, Operand(zero_reg));
1322 __ bind(&left_not_zero);
1325 // Check for (kMinInt / -1).
1326 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1327 Label left_not_min_int;
1328 __ Branch(&left_not_min_int, ne, left, Operand(kMinInt));
1329 DeoptimizeIf(eq, instr->environment(), right, Operand(-1));
1330 __ bind(&left_not_min_int);
1336 // We performed a truncating division. Correct the result if necessary.
1337 __ Branch(&done, eq, remainder, Operand(zero_reg), USE_DELAY_SLOT);
1338 __ Xor(scratch , remainder, Operand(right));
1339 __ Branch(&done, ge, scratch, Operand(zero_reg));
1340 __ Subu(result, result, Operand(1));
1346 void LCodeGen::DoMulI(LMulI* instr) {
1347 Register scratch = scratch0();
1348 Register result = ToRegister(instr->result());
1349 // Note that result may alias left.
1350 Register left = ToRegister(instr->left());
1351 LOperand* right_op = instr->right();
1353 bool bailout_on_minus_zero =
1354 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
1355 bool overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1357 if (right_op->IsConstantOperand()) {
1358 int32_t constant = ToInteger32(LConstantOperand::cast(right_op));
1360 if (bailout_on_minus_zero && (constant < 0)) {
1361 // The case of a null constant will be handled separately.
1362 // If constant is negative and left is null, the result should be -0.
1363 DeoptimizeIf(eq, instr->environment(), left, Operand(zero_reg));
1369 __ SubuAndCheckForOverflow(result, zero_reg, left, scratch);
1370 DeoptimizeIf(lt, instr->environment(), scratch, Operand(zero_reg));
1372 __ Subu(result, zero_reg, left);
1376 if (bailout_on_minus_zero) {
1377 // If left is strictly negative and the constant is null, the
1378 // result is -0. Deoptimize if required, otherwise return 0.
1379 DeoptimizeIf(lt, instr->environment(), left, Operand(zero_reg));
1381 __ mov(result, zero_reg);
1385 __ Move(result, left);
1388 // Multiplying by powers of two and powers of two plus or minus
1389 // one can be done faster with shifted operands.
1390 // For other constants we emit standard code.
1391 int32_t mask = constant >> 31;
1392 uint32_t constant_abs = (constant + mask) ^ mask;
1394 if (IsPowerOf2(constant_abs)) {
1395 int32_t shift = WhichPowerOf2(constant_abs);
1396 __ sll(result, left, shift);
1397 // Correct the sign of the result if the constant is negative.
1398 if (constant < 0) __ Subu(result, zero_reg, result);
1399 } else if (IsPowerOf2(constant_abs - 1)) {
1400 int32_t shift = WhichPowerOf2(constant_abs - 1);
1401 __ sll(scratch, left, shift);
1402 __ Addu(result, scratch, left);
1403 // Correct the sign of the result if the constant is negative.
1404 if (constant < 0) __ Subu(result, zero_reg, result);
1405 } else if (IsPowerOf2(constant_abs + 1)) {
1406 int32_t shift = WhichPowerOf2(constant_abs + 1);
1407 __ sll(scratch, left, shift);
1408 __ Subu(result, scratch, left);
1409 // Correct the sign of the result if the constant is negative.
1410 if (constant < 0) __ Subu(result, zero_reg, result);
1412 // Generate standard code.
1413 __ li(at, constant);
1414 __ Mul(result, left, at);
1419 ASSERT(right_op->IsRegister());
1420 Register right = ToRegister(right_op);
1423 // hi:lo = left * right.
1424 if (instr->hydrogen()->representation().IsSmi()) {
1425 __ SmiUntag(result, left);
1426 __ mult(result, right);
1430 __ mult(left, right);
1434 __ sra(at, result, 31);
1435 DeoptimizeIf(ne, instr->environment(), scratch, Operand(at));
1437 if (instr->hydrogen()->representation().IsSmi()) {
1438 __ SmiUntag(result, left);
1439 __ Mul(result, result, right);
1441 __ Mul(result, left, right);
1445 if (bailout_on_minus_zero) {
1447 __ Xor(at, left, right);
1448 __ Branch(&done, ge, at, Operand(zero_reg));
1449 // Bail out if the result is minus zero.
1451 instr->environment(),
1460 void LCodeGen::DoBitI(LBitI* instr) {
1461 LOperand* left_op = instr->left();
1462 LOperand* right_op = instr->right();
1463 ASSERT(left_op->IsRegister());
1464 Register left = ToRegister(left_op);
1465 Register result = ToRegister(instr->result());
1466 Operand right(no_reg);
1468 if (right_op->IsStackSlot() || right_op->IsArgument()) {
1469 right = Operand(EmitLoadRegister(right_op, at));
1471 ASSERT(right_op->IsRegister() || right_op->IsConstantOperand());
1472 right = ToOperand(right_op);
1475 switch (instr->op()) {
1476 case Token::BIT_AND:
1477 __ And(result, left, right);
1480 __ Or(result, left, right);
1482 case Token::BIT_XOR:
1483 if (right_op->IsConstantOperand() && right.immediate() == int32_t(~0)) {
1484 __ Nor(result, zero_reg, left);
1486 __ Xor(result, left, right);
1496 void LCodeGen::DoShiftI(LShiftI* instr) {
1497 // Both 'left' and 'right' are "used at start" (see LCodeGen::DoShift), so
1498 // result may alias either of them.
1499 LOperand* right_op = instr->right();
1500 Register left = ToRegister(instr->left());
1501 Register result = ToRegister(instr->result());
1502 Register scratch = scratch0();
1504 if (right_op->IsRegister()) {
1505 // No need to mask the right operand on MIPS, it is built into the variable
1506 // shift instructions.
1507 switch (instr->op()) {
1509 __ Ror(result, left, Operand(ToRegister(right_op)));
1512 __ srav(result, left, ToRegister(right_op));
1515 __ srlv(result, left, ToRegister(right_op));
1516 if (instr->can_deopt()) {
1517 DeoptimizeIf(lt, instr->environment(), result, Operand(zero_reg));
1521 __ sllv(result, left, ToRegister(right_op));
1528 // Mask the right_op operand.
1529 int value = ToInteger32(LConstantOperand::cast(right_op));
1530 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1531 switch (instr->op()) {
1533 if (shift_count != 0) {
1534 __ Ror(result, left, Operand(shift_count));
1536 __ Move(result, left);
1540 if (shift_count != 0) {
1541 __ sra(result, left, shift_count);
1543 __ Move(result, left);
1547 if (shift_count != 0) {
1548 __ srl(result, left, shift_count);
1550 if (instr->can_deopt()) {
1551 __ And(at, left, Operand(0x80000000));
1552 DeoptimizeIf(ne, instr->environment(), at, Operand(zero_reg));
1554 __ Move(result, left);
1558 if (shift_count != 0) {
1559 if (instr->hydrogen_value()->representation().IsSmi() &&
1560 instr->can_deopt()) {
1561 if (shift_count != 1) {
1562 __ sll(result, left, shift_count - 1);
1563 __ SmiTagCheckOverflow(result, result, scratch);
1565 __ SmiTagCheckOverflow(result, left, scratch);
1567 DeoptimizeIf(lt, instr->environment(), scratch, Operand(zero_reg));
1569 __ sll(result, left, shift_count);
1572 __ Move(result, left);
1583 void LCodeGen::DoSubI(LSubI* instr) {
1584 LOperand* left = instr->left();
1585 LOperand* right = instr->right();
1586 LOperand* result = instr->result();
1587 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1589 if (!can_overflow) {
1590 if (right->IsStackSlot() || right->IsArgument()) {
1591 Register right_reg = EmitLoadRegister(right, at);
1592 __ Subu(ToRegister(result), ToRegister(left), Operand(right_reg));
1594 ASSERT(right->IsRegister() || right->IsConstantOperand());
1595 __ Subu(ToRegister(result), ToRegister(left), ToOperand(right));
1597 } else { // can_overflow.
1598 Register overflow = scratch0();
1599 Register scratch = scratch1();
1600 if (right->IsStackSlot() ||
1601 right->IsArgument() ||
1602 right->IsConstantOperand()) {
1603 Register right_reg = EmitLoadRegister(right, scratch);
1604 __ SubuAndCheckForOverflow(ToRegister(result),
1607 overflow); // Reg at also used as scratch.
1609 ASSERT(right->IsRegister());
1610 // Due to overflow check macros not supporting constant operands,
1611 // handling the IsConstantOperand case was moved to prev if clause.
1612 __ SubuAndCheckForOverflow(ToRegister(result),
1615 overflow); // Reg at also used as scratch.
1617 DeoptimizeIf(lt, instr->environment(), overflow, Operand(zero_reg));
1622 void LCodeGen::DoConstantI(LConstantI* instr) {
1623 __ li(ToRegister(instr->result()), Operand(instr->value()));
1627 void LCodeGen::DoConstantS(LConstantS* instr) {
1628 __ li(ToRegister(instr->result()), Operand(instr->value()));
1632 void LCodeGen::DoConstantD(LConstantD* instr) {
1633 ASSERT(instr->result()->IsDoubleRegister());
1634 DoubleRegister result = ToDoubleRegister(instr->result());
1635 double v = instr->value();
1640 void LCodeGen::DoConstantE(LConstantE* instr) {
1641 __ li(ToRegister(instr->result()), Operand(instr->value()));
1645 void LCodeGen::DoConstantT(LConstantT* instr) {
1646 Handle<Object> value = instr->value(isolate());
1647 AllowDeferredHandleDereference smi_check;
1648 __ li(ToRegister(instr->result()), value);
1652 void LCodeGen::DoMapEnumLength(LMapEnumLength* instr) {
1653 Register result = ToRegister(instr->result());
1654 Register map = ToRegister(instr->value());
1655 __ EnumLength(result, map);
1659 void LCodeGen::DoElementsKind(LElementsKind* instr) {
1660 Register result = ToRegister(instr->result());
1661 Register input = ToRegister(instr->value());
1663 // Load map into |result|.
1664 __ lw(result, FieldMemOperand(input, HeapObject::kMapOffset));
1665 // Load the map's "bit field 2" into |result|. We only need the first byte,
1666 // but the following bit field extraction takes care of that anyway.
1667 __ lbu(result, FieldMemOperand(result, Map::kBitField2Offset));
1668 // Retrieve elements_kind from bit field 2.
1669 __ Ext(result, result, Map::kElementsKindShift, Map::kElementsKindBitCount);
1673 void LCodeGen::DoValueOf(LValueOf* instr) {
1674 Register input = ToRegister(instr->value());
1675 Register result = ToRegister(instr->result());
1676 Register map = ToRegister(instr->temp());
1679 if (!instr->hydrogen()->value()->IsHeapObject()) {
1680 // If the object is a smi return the object.
1681 __ Move(result, input);
1682 __ JumpIfSmi(input, &done);
1685 // If the object is not a value type, return the object.
1686 __ GetObjectType(input, map, map);
1687 __ Branch(&done, ne, map, Operand(JS_VALUE_TYPE));
1688 __ lw(result, FieldMemOperand(input, JSValue::kValueOffset));
1694 void LCodeGen::DoDateField(LDateField* instr) {
1695 Register object = ToRegister(instr->date());
1696 Register result = ToRegister(instr->result());
1697 Register scratch = ToRegister(instr->temp());
1698 Smi* index = instr->index();
1699 Label runtime, done;
1700 ASSERT(object.is(a0));
1701 ASSERT(result.is(v0));
1702 ASSERT(!scratch.is(scratch0()));
1703 ASSERT(!scratch.is(object));
1705 __ SmiTst(object, at);
1706 DeoptimizeIf(eq, instr->environment(), at, Operand(zero_reg));
1707 __ GetObjectType(object, scratch, scratch);
1708 DeoptimizeIf(ne, instr->environment(), scratch, Operand(JS_DATE_TYPE));
1710 if (index->value() == 0) {
1711 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset));
1713 if (index->value() < JSDate::kFirstUncachedField) {
1714 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
1715 __ li(scratch, Operand(stamp));
1716 __ lw(scratch, MemOperand(scratch));
1717 __ lw(scratch0(), FieldMemOperand(object, JSDate::kCacheStampOffset));
1718 __ Branch(&runtime, ne, scratch, Operand(scratch0()));
1719 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset +
1720 kPointerSize * index->value()));
1724 __ PrepareCallCFunction(2, scratch);
1725 __ li(a1, Operand(index));
1726 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
1732 MemOperand LCodeGen::BuildSeqStringOperand(Register string,
1734 String::Encoding encoding) {
1735 if (index->IsConstantOperand()) {
1736 int offset = ToInteger32(LConstantOperand::cast(index));
1737 if (encoding == String::TWO_BYTE_ENCODING) {
1738 offset *= kUC16Size;
1740 STATIC_ASSERT(kCharSize == 1);
1741 return FieldMemOperand(string, SeqString::kHeaderSize + offset);
1743 Register scratch = scratch0();
1744 ASSERT(!scratch.is(string));
1745 ASSERT(!scratch.is(ToRegister(index)));
1746 if (encoding == String::ONE_BYTE_ENCODING) {
1747 __ Addu(scratch, string, ToRegister(index));
1749 STATIC_ASSERT(kUC16Size == 2);
1750 __ sll(scratch, ToRegister(index), 1);
1751 __ Addu(scratch, string, scratch);
1753 return FieldMemOperand(scratch, SeqString::kHeaderSize);
1757 void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
1758 String::Encoding encoding = instr->hydrogen()->encoding();
1759 Register string = ToRegister(instr->string());
1760 Register result = ToRegister(instr->result());
1762 if (FLAG_debug_code) {
1763 Register scratch = scratch0();
1764 __ lw(scratch, FieldMemOperand(string, HeapObject::kMapOffset));
1765 __ lbu(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1767 __ And(scratch, scratch,
1768 Operand(kStringRepresentationMask | kStringEncodingMask));
1769 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1770 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1771 __ Subu(at, scratch, Operand(encoding == String::ONE_BYTE_ENCODING
1772 ? one_byte_seq_type : two_byte_seq_type));
1773 __ Check(eq, kUnexpectedStringType, at, Operand(zero_reg));
1776 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1777 if (encoding == String::ONE_BYTE_ENCODING) {
1778 __ lbu(result, operand);
1780 __ lhu(result, operand);
1785 void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
1786 String::Encoding encoding = instr->hydrogen()->encoding();
1787 Register string = ToRegister(instr->string());
1788 Register value = ToRegister(instr->value());
1790 if (FLAG_debug_code) {
1791 Register scratch = scratch0();
1792 Register index = ToRegister(instr->index());
1793 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1794 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1796 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
1797 ? one_byte_seq_type : two_byte_seq_type;
1798 __ EmitSeqStringSetCharCheck(string, index, value, scratch, encoding_mask);
1801 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1802 if (encoding == String::ONE_BYTE_ENCODING) {
1803 __ sb(value, operand);
1805 __ sh(value, operand);
1810 void LCodeGen::DoThrow(LThrow* instr) {
1811 __ push(ToRegister(instr->value()));
1812 ASSERT(ToRegister(instr->context()).is(cp));
1813 CallRuntime(Runtime::kThrow, 1, instr);
1815 if (FLAG_debug_code) {
1816 __ stop("Unreachable code.");
1821 void LCodeGen::DoAddI(LAddI* instr) {
1822 LOperand* left = instr->left();
1823 LOperand* right = instr->right();
1824 LOperand* result = instr->result();
1825 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1827 if (!can_overflow) {
1828 if (right->IsStackSlot() || right->IsArgument()) {
1829 Register right_reg = EmitLoadRegister(right, at);
1830 __ Addu(ToRegister(result), ToRegister(left), Operand(right_reg));
1832 ASSERT(right->IsRegister() || right->IsConstantOperand());
1833 __ Addu(ToRegister(result), ToRegister(left), ToOperand(right));
1835 } else { // can_overflow.
1836 Register overflow = scratch0();
1837 Register scratch = scratch1();
1838 if (right->IsStackSlot() ||
1839 right->IsArgument() ||
1840 right->IsConstantOperand()) {
1841 Register right_reg = EmitLoadRegister(right, scratch);
1842 __ AdduAndCheckForOverflow(ToRegister(result),
1845 overflow); // Reg at also used as scratch.
1847 ASSERT(right->IsRegister());
1848 // Due to overflow check macros not supporting constant operands,
1849 // handling the IsConstantOperand case was moved to prev if clause.
1850 __ AdduAndCheckForOverflow(ToRegister(result),
1853 overflow); // Reg at also used as scratch.
1855 DeoptimizeIf(lt, instr->environment(), overflow, Operand(zero_reg));
1860 void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
1861 LOperand* left = instr->left();
1862 LOperand* right = instr->right();
1863 HMathMinMax::Operation operation = instr->hydrogen()->operation();
1864 Condition condition = (operation == HMathMinMax::kMathMin) ? le : ge;
1865 if (instr->hydrogen()->representation().IsSmiOrInteger32()) {
1866 Register left_reg = ToRegister(left);
1867 Operand right_op = (right->IsRegister() || right->IsConstantOperand())
1869 : Operand(EmitLoadRegister(right, at));
1870 Register result_reg = ToRegister(instr->result());
1871 Label return_right, done;
1872 if (!result_reg.is(left_reg)) {
1873 __ Branch(&return_right, NegateCondition(condition), left_reg, right_op);
1874 __ mov(result_reg, left_reg);
1877 __ Branch(&done, condition, left_reg, right_op);
1878 __ bind(&return_right);
1879 __ Addu(result_reg, zero_reg, right_op);
1882 ASSERT(instr->hydrogen()->representation().IsDouble());
1883 FPURegister left_reg = ToDoubleRegister(left);
1884 FPURegister right_reg = ToDoubleRegister(right);
1885 FPURegister result_reg = ToDoubleRegister(instr->result());
1886 Label check_nan_left, check_zero, return_left, return_right, done;
1887 __ BranchF(&check_zero, &check_nan_left, eq, left_reg, right_reg);
1888 __ BranchF(&return_left, NULL, condition, left_reg, right_reg);
1889 __ Branch(&return_right);
1891 __ bind(&check_zero);
1892 // left == right != 0.
1893 __ BranchF(&return_left, NULL, ne, left_reg, kDoubleRegZero);
1894 // At this point, both left and right are either 0 or -0.
1895 if (operation == HMathMinMax::kMathMin) {
1896 __ neg_d(left_reg, left_reg);
1897 __ sub_d(result_reg, left_reg, right_reg);
1898 __ neg_d(result_reg, result_reg);
1900 __ add_d(result_reg, left_reg, right_reg);
1904 __ bind(&check_nan_left);
1906 __ BranchF(NULL, &return_left, eq, left_reg, left_reg);
1907 __ bind(&return_right);
1908 if (!right_reg.is(result_reg)) {
1909 __ mov_d(result_reg, right_reg);
1913 __ bind(&return_left);
1914 if (!left_reg.is(result_reg)) {
1915 __ mov_d(result_reg, left_reg);
1922 void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
1923 DoubleRegister left = ToDoubleRegister(instr->left());
1924 DoubleRegister right = ToDoubleRegister(instr->right());
1925 DoubleRegister result = ToDoubleRegister(instr->result());
1926 switch (instr->op()) {
1928 __ add_d(result, left, right);
1931 __ sub_d(result, left, right);
1934 __ mul_d(result, left, right);
1937 __ div_d(result, left, right);
1940 // Save a0-a3 on the stack.
1941 RegList saved_regs = a0.bit() | a1.bit() | a2.bit() | a3.bit();
1942 __ MultiPush(saved_regs);
1944 __ PrepareCallCFunction(0, 2, scratch0());
1945 __ MovToFloatParameters(left, right);
1947 ExternalReference::mod_two_doubles_operation(isolate()),
1949 // Move the result in the double result register.
1950 __ MovFromFloatResult(result);
1952 // Restore saved register.
1953 __ MultiPop(saved_regs);
1963 void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
1964 ASSERT(ToRegister(instr->context()).is(cp));
1965 ASSERT(ToRegister(instr->left()).is(a1));
1966 ASSERT(ToRegister(instr->right()).is(a0));
1967 ASSERT(ToRegister(instr->result()).is(v0));
1969 BinaryOpICStub stub(instr->op(), NO_OVERWRITE);
1970 CallCode(stub.GetCode(isolate()), RelocInfo::CODE_TARGET, instr);
1971 // Other arch use a nop here, to signal that there is no inlined
1972 // patchable code. Mips does not need the nop, since our marker
1973 // instruction (andi zero_reg) will never be used in normal code.
1977 template<class InstrType>
1978 void LCodeGen::EmitBranch(InstrType instr,
1979 Condition condition,
1981 const Operand& src2) {
1982 int left_block = instr->TrueDestination(chunk_);
1983 int right_block = instr->FalseDestination(chunk_);
1985 int next_block = GetNextEmittedBlock();
1986 if (right_block == left_block || condition == al) {
1987 EmitGoto(left_block);
1988 } else if (left_block == next_block) {
1989 __ Branch(chunk_->GetAssemblyLabel(right_block),
1990 NegateCondition(condition), src1, src2);
1991 } else if (right_block == next_block) {
1992 __ Branch(chunk_->GetAssemblyLabel(left_block), condition, src1, src2);
1994 __ Branch(chunk_->GetAssemblyLabel(left_block), condition, src1, src2);
1995 __ Branch(chunk_->GetAssemblyLabel(right_block));
2000 template<class InstrType>
2001 void LCodeGen::EmitBranchF(InstrType instr,
2002 Condition condition,
2005 int right_block = instr->FalseDestination(chunk_);
2006 int left_block = instr->TrueDestination(chunk_);
2008 int next_block = GetNextEmittedBlock();
2009 if (right_block == left_block) {
2010 EmitGoto(left_block);
2011 } else if (left_block == next_block) {
2012 __ BranchF(chunk_->GetAssemblyLabel(right_block), NULL,
2013 NegateCondition(condition), src1, src2);
2014 } else if (right_block == next_block) {
2015 __ BranchF(chunk_->GetAssemblyLabel(left_block), NULL,
2016 condition, src1, src2);
2018 __ BranchF(chunk_->GetAssemblyLabel(left_block), NULL,
2019 condition, src1, src2);
2020 __ Branch(chunk_->GetAssemblyLabel(right_block));
2025 template<class InstrType>
2026 void LCodeGen::EmitFalseBranch(InstrType instr,
2027 Condition condition,
2029 const Operand& src2) {
2030 int false_block = instr->FalseDestination(chunk_);
2031 __ Branch(chunk_->GetAssemblyLabel(false_block), condition, src1, src2);
2035 template<class InstrType>
2036 void LCodeGen::EmitFalseBranchF(InstrType instr,
2037 Condition condition,
2040 int false_block = instr->FalseDestination(chunk_);
2041 __ BranchF(chunk_->GetAssemblyLabel(false_block), NULL,
2042 condition, src1, src2);
2046 void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
2047 __ stop("LDebugBreak");
2051 void LCodeGen::DoBranch(LBranch* instr) {
2052 Representation r = instr->hydrogen()->value()->representation();
2053 if (r.IsInteger32() || r.IsSmi()) {
2054 ASSERT(!info()->IsStub());
2055 Register reg = ToRegister(instr->value());
2056 EmitBranch(instr, ne, reg, Operand(zero_reg));
2057 } else if (r.IsDouble()) {
2058 ASSERT(!info()->IsStub());
2059 DoubleRegister reg = ToDoubleRegister(instr->value());
2060 // Test the double value. Zero and NaN are false.
2061 EmitBranchF(instr, nue, reg, kDoubleRegZero);
2063 ASSERT(r.IsTagged());
2064 Register reg = ToRegister(instr->value());
2065 HType type = instr->hydrogen()->value()->type();
2066 if (type.IsBoolean()) {
2067 ASSERT(!info()->IsStub());
2068 __ LoadRoot(at, Heap::kTrueValueRootIndex);
2069 EmitBranch(instr, eq, reg, Operand(at));
2070 } else if (type.IsSmi()) {
2071 ASSERT(!info()->IsStub());
2072 EmitBranch(instr, ne, reg, Operand(zero_reg));
2073 } else if (type.IsJSArray()) {
2074 ASSERT(!info()->IsStub());
2075 EmitBranch(instr, al, zero_reg, Operand(zero_reg));
2076 } else if (type.IsHeapNumber()) {
2077 ASSERT(!info()->IsStub());
2078 DoubleRegister dbl_scratch = double_scratch0();
2079 __ ldc1(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
2080 // Test the double value. Zero and NaN are false.
2081 EmitBranchF(instr, nue, dbl_scratch, kDoubleRegZero);
2082 } else if (type.IsString()) {
2083 ASSERT(!info()->IsStub());
2084 __ lw(at, FieldMemOperand(reg, String::kLengthOffset));
2085 EmitBranch(instr, ne, at, Operand(zero_reg));
2087 ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
2088 // Avoid deopts in the case where we've never executed this path before.
2089 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
2091 if (expected.Contains(ToBooleanStub::UNDEFINED)) {
2092 // undefined -> false.
2093 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
2094 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
2096 if (expected.Contains(ToBooleanStub::BOOLEAN)) {
2097 // Boolean -> its value.
2098 __ LoadRoot(at, Heap::kTrueValueRootIndex);
2099 __ Branch(instr->TrueLabel(chunk_), eq, reg, Operand(at));
2100 __ LoadRoot(at, Heap::kFalseValueRootIndex);
2101 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
2103 if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
2105 __ LoadRoot(at, Heap::kNullValueRootIndex);
2106 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
2109 if (expected.Contains(ToBooleanStub::SMI)) {
2110 // Smis: 0 -> false, all other -> true.
2111 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(zero_reg));
2112 __ JumpIfSmi(reg, instr->TrueLabel(chunk_));
2113 } else if (expected.NeedsMap()) {
2114 // If we need a map later and have a Smi -> deopt.
2116 DeoptimizeIf(eq, instr->environment(), at, Operand(zero_reg));
2119 const Register map = scratch0();
2120 if (expected.NeedsMap()) {
2121 __ lw(map, FieldMemOperand(reg, HeapObject::kMapOffset));
2122 if (expected.CanBeUndetectable()) {
2123 // Undetectable -> false.
2124 __ lbu(at, FieldMemOperand(map, Map::kBitFieldOffset));
2125 __ And(at, at, Operand(1 << Map::kIsUndetectable));
2126 __ Branch(instr->FalseLabel(chunk_), ne, at, Operand(zero_reg));
2130 if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
2131 // spec object -> true.
2132 __ lbu(at, FieldMemOperand(map, Map::kInstanceTypeOffset));
2133 __ Branch(instr->TrueLabel(chunk_),
2134 ge, at, Operand(FIRST_SPEC_OBJECT_TYPE));
2137 if (expected.Contains(ToBooleanStub::STRING)) {
2138 // String value -> false iff empty.
2140 __ lbu(at, FieldMemOperand(map, Map::kInstanceTypeOffset));
2141 __ Branch(¬_string, ge , at, Operand(FIRST_NONSTRING_TYPE));
2142 __ lw(at, FieldMemOperand(reg, String::kLengthOffset));
2143 __ Branch(instr->TrueLabel(chunk_), ne, at, Operand(zero_reg));
2144 __ Branch(instr->FalseLabel(chunk_));
2145 __ bind(¬_string);
2148 if (expected.Contains(ToBooleanStub::SYMBOL)) {
2149 // Symbol value -> true.
2150 const Register scratch = scratch1();
2151 __ lbu(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
2152 __ Branch(instr->TrueLabel(chunk_), eq, scratch, Operand(SYMBOL_TYPE));
2155 if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
2156 // heap number -> false iff +0, -0, or NaN.
2157 DoubleRegister dbl_scratch = double_scratch0();
2158 Label not_heap_number;
2159 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
2160 __ Branch(¬_heap_number, ne, map, Operand(at));
2161 __ ldc1(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
2162 __ BranchF(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2163 ne, dbl_scratch, kDoubleRegZero);
2164 // Falls through if dbl_scratch == 0.
2165 __ Branch(instr->FalseLabel(chunk_));
2166 __ bind(¬_heap_number);
2169 if (!expected.IsGeneric()) {
2170 // We've seen something for the first time -> deopt.
2171 // This can only happen if we are not generic already.
2172 DeoptimizeIf(al, instr->environment(), zero_reg, Operand(zero_reg));
2179 void LCodeGen::EmitGoto(int block) {
2180 if (!IsNextEmittedBlock(block)) {
2181 __ jmp(chunk_->GetAssemblyLabel(LookupDestination(block)));
2186 void LCodeGen::DoGoto(LGoto* instr) {
2187 EmitGoto(instr->block_id());
2191 Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
2192 Condition cond = kNoCondition;
2195 case Token::EQ_STRICT:
2199 case Token::NE_STRICT:
2203 cond = is_unsigned ? lo : lt;
2206 cond = is_unsigned ? hi : gt;
2209 cond = is_unsigned ? ls : le;
2212 cond = is_unsigned ? hs : ge;
2215 case Token::INSTANCEOF:
2223 void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2224 LOperand* left = instr->left();
2225 LOperand* right = instr->right();
2226 Condition cond = TokenToCondition(instr->op(), false);
2228 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2229 // We can statically evaluate the comparison.
2230 double left_val = ToDouble(LConstantOperand::cast(left));
2231 double right_val = ToDouble(LConstantOperand::cast(right));
2232 int next_block = EvalComparison(instr->op(), left_val, right_val) ?
2233 instr->TrueDestination(chunk_) : instr->FalseDestination(chunk_);
2234 EmitGoto(next_block);
2236 if (instr->is_double()) {
2237 // Compare left and right as doubles and load the
2238 // resulting flags into the normal status register.
2239 FPURegister left_reg = ToDoubleRegister(left);
2240 FPURegister right_reg = ToDoubleRegister(right);
2242 // If a NaN is involved, i.e. the result is unordered,
2243 // jump to false block label.
2244 __ BranchF(NULL, instr->FalseLabel(chunk_), eq,
2245 left_reg, right_reg);
2247 EmitBranchF(instr, cond, left_reg, right_reg);
2250 Operand cmp_right = Operand(0);
2252 if (right->IsConstantOperand()) {
2253 int32_t value = ToInteger32(LConstantOperand::cast(right));
2254 if (instr->hydrogen_value()->representation().IsSmi()) {
2255 cmp_left = ToRegister(left);
2256 cmp_right = Operand(Smi::FromInt(value));
2258 cmp_left = ToRegister(left);
2259 cmp_right = Operand(value);
2261 } else if (left->IsConstantOperand()) {
2262 int32_t value = ToInteger32(LConstantOperand::cast(left));
2263 if (instr->hydrogen_value()->representation().IsSmi()) {
2264 cmp_left = ToRegister(right);
2265 cmp_right = Operand(Smi::FromInt(value));
2267 cmp_left = ToRegister(right);
2268 cmp_right = Operand(value);
2270 // We transposed the operands. Reverse the condition.
2271 cond = ReverseCondition(cond);
2273 cmp_left = ToRegister(left);
2274 cmp_right = Operand(ToRegister(right));
2277 EmitBranch(instr, cond, cmp_left, cmp_right);
2283 void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
2284 Register left = ToRegister(instr->left());
2285 Register right = ToRegister(instr->right());
2287 EmitBranch(instr, eq, left, Operand(right));
2291 void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) {
2292 if (instr->hydrogen()->representation().IsTagged()) {
2293 Register input_reg = ToRegister(instr->object());
2294 __ li(at, Operand(factory()->the_hole_value()));
2295 EmitBranch(instr, eq, input_reg, Operand(at));
2299 DoubleRegister input_reg = ToDoubleRegister(instr->object());
2300 EmitFalseBranchF(instr, eq, input_reg, input_reg);
2302 Register scratch = scratch0();
2303 __ FmoveHigh(scratch, input_reg);
2304 EmitBranch(instr, eq, scratch, Operand(kHoleNanUpper32));
2308 void LCodeGen::DoCompareMinusZeroAndBranch(LCompareMinusZeroAndBranch* instr) {
2309 Representation rep = instr->hydrogen()->value()->representation();
2310 ASSERT(!rep.IsInteger32());
2311 Register scratch = ToRegister(instr->temp());
2313 if (rep.IsDouble()) {
2314 DoubleRegister value = ToDoubleRegister(instr->value());
2315 EmitFalseBranchF(instr, ne, value, kDoubleRegZero);
2316 __ FmoveHigh(scratch, value);
2317 __ li(at, 0x80000000);
2319 Register value = ToRegister(instr->value());
2322 Heap::kHeapNumberMapRootIndex,
2323 instr->FalseLabel(chunk()),
2325 __ lw(scratch, FieldMemOperand(value, HeapNumber::kExponentOffset));
2326 EmitFalseBranch(instr, ne, scratch, Operand(0x80000000));
2327 __ lw(scratch, FieldMemOperand(value, HeapNumber::kMantissaOffset));
2328 __ mov(at, zero_reg);
2330 EmitBranch(instr, eq, scratch, Operand(at));
2334 Condition LCodeGen::EmitIsObject(Register input,
2337 Label* is_not_object,
2339 __ JumpIfSmi(input, is_not_object);
2341 __ LoadRoot(temp2, Heap::kNullValueRootIndex);
2342 __ Branch(is_object, eq, input, Operand(temp2));
2345 __ lw(temp1, FieldMemOperand(input, HeapObject::kMapOffset));
2346 // Undetectable objects behave like undefined.
2347 __ lbu(temp2, FieldMemOperand(temp1, Map::kBitFieldOffset));
2348 __ And(temp2, temp2, Operand(1 << Map::kIsUndetectable));
2349 __ Branch(is_not_object, ne, temp2, Operand(zero_reg));
2351 // Load instance type and check that it is in object type range.
2352 __ lbu(temp2, FieldMemOperand(temp1, Map::kInstanceTypeOffset));
2353 __ Branch(is_not_object,
2354 lt, temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2360 void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) {
2361 Register reg = ToRegister(instr->value());
2362 Register temp1 = ToRegister(instr->temp());
2363 Register temp2 = scratch0();
2365 Condition true_cond =
2366 EmitIsObject(reg, temp1, temp2,
2367 instr->FalseLabel(chunk_), instr->TrueLabel(chunk_));
2369 EmitBranch(instr, true_cond, temp2,
2370 Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
2374 Condition LCodeGen::EmitIsString(Register input,
2376 Label* is_not_string,
2377 SmiCheck check_needed = INLINE_SMI_CHECK) {
2378 if (check_needed == INLINE_SMI_CHECK) {
2379 __ JumpIfSmi(input, is_not_string);
2381 __ GetObjectType(input, temp1, temp1);
2387 void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
2388 Register reg = ToRegister(instr->value());
2389 Register temp1 = ToRegister(instr->temp());
2391 SmiCheck check_needed =
2392 instr->hydrogen()->value()->IsHeapObject()
2393 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2394 Condition true_cond =
2395 EmitIsString(reg, temp1, instr->FalseLabel(chunk_), check_needed);
2397 EmitBranch(instr, true_cond, temp1,
2398 Operand(FIRST_NONSTRING_TYPE));
2402 void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
2403 Register input_reg = EmitLoadRegister(instr->value(), at);
2404 __ And(at, input_reg, kSmiTagMask);
2405 EmitBranch(instr, eq, at, Operand(zero_reg));
2409 void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
2410 Register input = ToRegister(instr->value());
2411 Register temp = ToRegister(instr->temp());
2413 if (!instr->hydrogen()->value()->IsHeapObject()) {
2414 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2416 __ lw(temp, FieldMemOperand(input, HeapObject::kMapOffset));
2417 __ lbu(temp, FieldMemOperand(temp, Map::kBitFieldOffset));
2418 __ And(at, temp, Operand(1 << Map::kIsUndetectable));
2419 EmitBranch(instr, ne, at, Operand(zero_reg));
2423 static Condition ComputeCompareCondition(Token::Value op) {
2425 case Token::EQ_STRICT:
2438 return kNoCondition;
2443 void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
2444 ASSERT(ToRegister(instr->context()).is(cp));
2445 Token::Value op = instr->op();
2447 Handle<Code> ic = CompareIC::GetUninitialized(isolate(), op);
2448 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2450 Condition condition = ComputeCompareCondition(op);
2452 EmitBranch(instr, condition, v0, Operand(zero_reg));
2456 static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
2457 InstanceType from = instr->from();
2458 InstanceType to = instr->to();
2459 if (from == FIRST_TYPE) return to;
2460 ASSERT(from == to || to == LAST_TYPE);
2465 static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
2466 InstanceType from = instr->from();
2467 InstanceType to = instr->to();
2468 if (from == to) return eq;
2469 if (to == LAST_TYPE) return hs;
2470 if (from == FIRST_TYPE) return ls;
2476 void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
2477 Register scratch = scratch0();
2478 Register input = ToRegister(instr->value());
2480 if (!instr->hydrogen()->value()->IsHeapObject()) {
2481 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2484 __ GetObjectType(input, scratch, scratch);
2486 BranchCondition(instr->hydrogen()),
2488 Operand(TestType(instr->hydrogen())));
2492 void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
2493 Register input = ToRegister(instr->value());
2494 Register result = ToRegister(instr->result());
2496 __ AssertString(input);
2498 __ lw(result, FieldMemOperand(input, String::kHashFieldOffset));
2499 __ IndexFromHash(result, result);
2503 void LCodeGen::DoHasCachedArrayIndexAndBranch(
2504 LHasCachedArrayIndexAndBranch* instr) {
2505 Register input = ToRegister(instr->value());
2506 Register scratch = scratch0();
2509 FieldMemOperand(input, String::kHashFieldOffset));
2510 __ And(at, scratch, Operand(String::kContainsCachedArrayIndexMask));
2511 EmitBranch(instr, eq, at, Operand(zero_reg));
2515 // Branches to a label or falls through with the answer in flags. Trashes
2516 // the temp registers, but not the input.
2517 void LCodeGen::EmitClassOfTest(Label* is_true,
2519 Handle<String>class_name,
2523 ASSERT(!input.is(temp));
2524 ASSERT(!input.is(temp2));
2525 ASSERT(!temp.is(temp2));
2527 __ JumpIfSmi(input, is_false);
2529 if (class_name->IsOneByteEqualTo(STATIC_ASCII_VECTOR("Function"))) {
2530 // Assuming the following assertions, we can use the same compares to test
2531 // for both being a function type and being in the object type range.
2532 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
2533 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2534 FIRST_SPEC_OBJECT_TYPE + 1);
2535 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2536 LAST_SPEC_OBJECT_TYPE - 1);
2537 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
2539 __ GetObjectType(input, temp, temp2);
2540 __ Branch(is_false, lt, temp2, Operand(FIRST_SPEC_OBJECT_TYPE));
2541 __ Branch(is_true, eq, temp2, Operand(FIRST_SPEC_OBJECT_TYPE));
2542 __ Branch(is_true, eq, temp2, Operand(LAST_SPEC_OBJECT_TYPE));
2544 // Faster code path to avoid two compares: subtract lower bound from the
2545 // actual type and do a signed compare with the width of the type range.
2546 __ GetObjectType(input, temp, temp2);
2547 __ Subu(temp2, temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2548 __ Branch(is_false, gt, temp2, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE -
2549 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2552 // Now we are in the FIRST-LAST_NONCALLABLE_SPEC_OBJECT_TYPE range.
2553 // Check if the constructor in the map is a function.
2554 __ lw(temp, FieldMemOperand(temp, Map::kConstructorOffset));
2556 // Objects with a non-function constructor have class 'Object'.
2557 __ GetObjectType(temp, temp2, temp2);
2558 if (class_name->IsOneByteEqualTo(STATIC_ASCII_VECTOR("Object"))) {
2559 __ Branch(is_true, ne, temp2, Operand(JS_FUNCTION_TYPE));
2561 __ Branch(is_false, ne, temp2, Operand(JS_FUNCTION_TYPE));
2564 // temp now contains the constructor function. Grab the
2565 // instance class name from there.
2566 __ lw(temp, FieldMemOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2567 __ lw(temp, FieldMemOperand(temp,
2568 SharedFunctionInfo::kInstanceClassNameOffset));
2569 // The class name we are testing against is internalized since it's a literal.
2570 // The name in the constructor is internalized because of the way the context
2571 // is booted. This routine isn't expected to work for random API-created
2572 // classes and it doesn't have to because you can't access it with natives
2573 // syntax. Since both sides are internalized it is sufficient to use an
2574 // identity comparison.
2576 // End with the address of this class_name instance in temp register.
2577 // On MIPS, the caller must do the comparison with Handle<String>class_name.
2581 void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
2582 Register input = ToRegister(instr->value());
2583 Register temp = scratch0();
2584 Register temp2 = ToRegister(instr->temp());
2585 Handle<String> class_name = instr->hydrogen()->class_name();
2587 EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2588 class_name, input, temp, temp2);
2590 EmitBranch(instr, eq, temp, Operand(class_name));
2594 void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
2595 Register reg = ToRegister(instr->value());
2596 Register temp = ToRegister(instr->temp());
2598 __ lw(temp, FieldMemOperand(reg, HeapObject::kMapOffset));
2599 EmitBranch(instr, eq, temp, Operand(instr->map()));
2603 void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
2604 ASSERT(ToRegister(instr->context()).is(cp));
2605 Label true_label, done;
2606 ASSERT(ToRegister(instr->left()).is(a0)); // Object is in a0.
2607 ASSERT(ToRegister(instr->right()).is(a1)); // Function is in a1.
2608 Register result = ToRegister(instr->result());
2609 ASSERT(result.is(v0));
2611 InstanceofStub stub(InstanceofStub::kArgsInRegisters);
2612 CallCode(stub.GetCode(isolate()), RelocInfo::CODE_TARGET, instr);
2614 __ Branch(&true_label, eq, result, Operand(zero_reg));
2615 __ li(result, Operand(factory()->false_value()));
2617 __ bind(&true_label);
2618 __ li(result, Operand(factory()->true_value()));
2623 void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) {
2624 class DeferredInstanceOfKnownGlobal V8_FINAL : public LDeferredCode {
2626 DeferredInstanceOfKnownGlobal(LCodeGen* codegen,
2627 LInstanceOfKnownGlobal* instr)
2628 : LDeferredCode(codegen), instr_(instr) { }
2629 virtual void Generate() V8_OVERRIDE {
2630 codegen()->DoDeferredInstanceOfKnownGlobal(instr_, &map_check_);
2632 virtual LInstruction* instr() V8_OVERRIDE { return instr_; }
2633 Label* map_check() { return &map_check_; }
2636 LInstanceOfKnownGlobal* instr_;
2640 DeferredInstanceOfKnownGlobal* deferred;
2641 deferred = new(zone()) DeferredInstanceOfKnownGlobal(this, instr);
2643 Label done, false_result;
2644 Register object = ToRegister(instr->value());
2645 Register temp = ToRegister(instr->temp());
2646 Register result = ToRegister(instr->result());
2648 ASSERT(object.is(a0));
2649 ASSERT(result.is(v0));
2651 // A Smi is not instance of anything.
2652 __ JumpIfSmi(object, &false_result);
2654 // This is the inlined call site instanceof cache. The two occurences of the
2655 // hole value will be patched to the last map/result pair generated by the
2658 Register map = temp;
2659 __ lw(map, FieldMemOperand(object, HeapObject::kMapOffset));
2661 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
2662 __ bind(deferred->map_check()); // Label for calculating code patching.
2663 // We use Factory::the_hole_value() on purpose instead of loading from the
2664 // root array to force relocation to be able to later patch with
2666 Handle<Cell> cell = factory()->NewCell(factory()->the_hole_value());
2667 __ li(at, Operand(Handle<Object>(cell)));
2668 __ lw(at, FieldMemOperand(at, PropertyCell::kValueOffset));
2669 __ Branch(&cache_miss, ne, map, Operand(at));
2670 // We use Factory::the_hole_value() on purpose instead of loading from the
2671 // root array to force relocation to be able to later patch
2672 // with true or false.
2673 __ li(result, Operand(factory()->the_hole_value()), CONSTANT_SIZE);
2676 // The inlined call site cache did not match. Check null and string before
2677 // calling the deferred code.
2678 __ bind(&cache_miss);
2679 // Null is not instance of anything.
2680 __ LoadRoot(temp, Heap::kNullValueRootIndex);
2681 __ Branch(&false_result, eq, object, Operand(temp));
2683 // String values is not instance of anything.
2684 Condition cc = __ IsObjectStringType(object, temp, temp);
2685 __ Branch(&false_result, cc, temp, Operand(zero_reg));
2687 // Go to the deferred code.
2688 __ Branch(deferred->entry());
2690 __ bind(&false_result);
2691 __ LoadRoot(result, Heap::kFalseValueRootIndex);
2693 // Here result has either true or false. Deferred code also produces true or
2695 __ bind(deferred->exit());
2700 void LCodeGen::DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
2702 Register result = ToRegister(instr->result());
2703 ASSERT(result.is(v0));
2705 InstanceofStub::Flags flags = InstanceofStub::kNoFlags;
2706 flags = static_cast<InstanceofStub::Flags>(
2707 flags | InstanceofStub::kArgsInRegisters);
2708 flags = static_cast<InstanceofStub::Flags>(
2709 flags | InstanceofStub::kCallSiteInlineCheck);
2710 flags = static_cast<InstanceofStub::Flags>(
2711 flags | InstanceofStub::kReturnTrueFalseObject);
2712 InstanceofStub stub(flags);
2714 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
2715 LoadContextFromDeferred(instr->context());
2717 // Get the temp register reserved by the instruction. This needs to be t0 as
2718 // its slot of the pushing of safepoint registers is used to communicate the
2719 // offset to the location of the map check.
2720 Register temp = ToRegister(instr->temp());
2721 ASSERT(temp.is(t0));
2722 __ li(InstanceofStub::right(), instr->function());
2723 static const int kAdditionalDelta = 7;
2724 int delta = masm_->InstructionsGeneratedSince(map_check) + kAdditionalDelta;
2725 Label before_push_delta;
2726 __ bind(&before_push_delta);
2728 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
2729 __ li(temp, Operand(delta * kPointerSize), CONSTANT_SIZE);
2730 __ StoreToSafepointRegisterSlot(temp, temp);
2732 CallCodeGeneric(stub.GetCode(isolate()),
2733 RelocInfo::CODE_TARGET,
2735 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
2736 LEnvironment* env = instr->GetDeferredLazyDeoptimizationEnvironment();
2737 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
2738 // Put the result value into the result register slot and
2739 // restore all registers.
2740 __ StoreToSafepointRegisterSlot(result, result);
2744 void LCodeGen::DoCmpT(LCmpT* instr) {
2745 ASSERT(ToRegister(instr->context()).is(cp));
2746 Token::Value op = instr->op();
2748 Handle<Code> ic = CompareIC::GetUninitialized(isolate(), op);
2749 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2750 // On MIPS there is no need for a "no inlined smi code" marker (nop).
2752 Condition condition = ComputeCompareCondition(op);
2753 // A minor optimization that relies on LoadRoot always emitting one
2755 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
2757 __ Branch(USE_DELAY_SLOT, &done, condition, v0, Operand(zero_reg));
2759 __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex);
2760 ASSERT_EQ(1, masm()->InstructionsGeneratedSince(&check));
2761 __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex);
2766 void LCodeGen::DoReturn(LReturn* instr) {
2767 if (FLAG_trace && info()->IsOptimizing()) {
2768 // Push the return value on the stack as the parameter.
2769 // Runtime::TraceExit returns its parameter in v0. We're leaving the code
2770 // managed by the register allocator and tearing down the frame, it's
2771 // safe to write to the context register.
2773 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2774 __ CallRuntime(Runtime::kTraceExit, 1);
2776 if (info()->saves_caller_doubles()) {
2777 RestoreCallerDoubles();
2779 int no_frame_start = -1;
2780 if (NeedsEagerFrame()) {
2782 no_frame_start = masm_->pc_offset();
2785 if (instr->has_constant_parameter_count()) {
2786 int parameter_count = ToInteger32(instr->constant_parameter_count());
2787 int32_t sp_delta = (parameter_count + 1) * kPointerSize;
2788 if (sp_delta != 0) {
2789 __ Addu(sp, sp, Operand(sp_delta));
2792 Register reg = ToRegister(instr->parameter_count());
2793 // The argument count parameter is a smi
2795 __ sll(at, reg, kPointerSizeLog2);
2796 __ Addu(sp, sp, at);
2801 if (no_frame_start != -1) {
2802 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
2807 void LCodeGen::DoLoadGlobalCell(LLoadGlobalCell* instr) {
2808 Register result = ToRegister(instr->result());
2809 __ li(at, Operand(Handle<Object>(instr->hydrogen()->cell().handle())));
2810 __ lw(result, FieldMemOperand(at, Cell::kValueOffset));
2811 if (instr->hydrogen()->RequiresHoleCheck()) {
2812 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2813 DeoptimizeIf(eq, instr->environment(), result, Operand(at));
2818 void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
2819 ASSERT(ToRegister(instr->context()).is(cp));
2820 ASSERT(ToRegister(instr->global_object()).is(a0));
2821 ASSERT(ToRegister(instr->result()).is(v0));
2823 __ li(a2, Operand(instr->name()));
2824 ContextualMode mode = instr->for_typeof() ? NOT_CONTEXTUAL : CONTEXTUAL;
2825 Handle<Code> ic = LoadIC::initialize_stub(isolate(), mode);
2826 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2830 void LCodeGen::DoStoreGlobalCell(LStoreGlobalCell* instr) {
2831 Register value = ToRegister(instr->value());
2832 Register cell = scratch0();
2835 __ li(cell, Operand(instr->hydrogen()->cell().handle()));
2837 // If the cell we are storing to contains the hole it could have
2838 // been deleted from the property dictionary. In that case, we need
2839 // to update the property details in the property dictionary to mark
2840 // it as no longer deleted.
2841 if (instr->hydrogen()->RequiresHoleCheck()) {
2842 // We use a temp to check the payload.
2843 Register payload = ToRegister(instr->temp());
2844 __ lw(payload, FieldMemOperand(cell, Cell::kValueOffset));
2845 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2846 DeoptimizeIf(eq, instr->environment(), payload, Operand(at));
2850 __ sw(value, FieldMemOperand(cell, Cell::kValueOffset));
2851 // Cells are always rescanned, so no write barrier here.
2856 void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
2857 Register context = ToRegister(instr->context());
2858 Register result = ToRegister(instr->result());
2860 __ lw(result, ContextOperand(context, instr->slot_index()));
2861 if (instr->hydrogen()->RequiresHoleCheck()) {
2862 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2864 if (instr->hydrogen()->DeoptimizesOnHole()) {
2865 DeoptimizeIf(eq, instr->environment(), result, Operand(at));
2868 __ Branch(&is_not_hole, ne, result, Operand(at));
2869 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2870 __ bind(&is_not_hole);
2876 void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
2877 Register context = ToRegister(instr->context());
2878 Register value = ToRegister(instr->value());
2879 Register scratch = scratch0();
2880 MemOperand target = ContextOperand(context, instr->slot_index());
2882 Label skip_assignment;
2884 if (instr->hydrogen()->RequiresHoleCheck()) {
2885 __ lw(scratch, target);
2886 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2888 if (instr->hydrogen()->DeoptimizesOnHole()) {
2889 DeoptimizeIf(eq, instr->environment(), scratch, Operand(at));
2891 __ Branch(&skip_assignment, ne, scratch, Operand(at));
2895 __ sw(value, target);
2896 if (instr->hydrogen()->NeedsWriteBarrier()) {
2897 SmiCheck check_needed =
2898 instr->hydrogen()->value()->IsHeapObject()
2899 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2900 __ RecordWriteContextSlot(context,
2906 EMIT_REMEMBERED_SET,
2910 __ bind(&skip_assignment);
2914 void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
2915 HObjectAccess access = instr->hydrogen()->access();
2916 int offset = access.offset();
2917 Register object = ToRegister(instr->object());
2919 if (access.IsExternalMemory()) {
2920 Register result = ToRegister(instr->result());
2921 MemOperand operand = MemOperand(object, offset);
2922 __ Load(result, operand, access.representation());
2926 if (instr->hydrogen()->representation().IsDouble()) {
2927 DoubleRegister result = ToDoubleRegister(instr->result());
2928 __ ldc1(result, FieldMemOperand(object, offset));
2932 Register result = ToRegister(instr->result());
2933 if (!access.IsInobject()) {
2934 __ lw(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
2937 MemOperand operand = FieldMemOperand(object, offset);
2938 __ Load(result, operand, access.representation());
2942 void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
2943 ASSERT(ToRegister(instr->context()).is(cp));
2944 ASSERT(ToRegister(instr->object()).is(a0));
2945 ASSERT(ToRegister(instr->result()).is(v0));
2947 // Name is always in a2.
2948 __ li(a2, Operand(instr->name()));
2949 Handle<Code> ic = LoadIC::initialize_stub(isolate(), NOT_CONTEXTUAL);
2950 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2954 void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
2955 Register scratch = scratch0();
2956 Register function = ToRegister(instr->function());
2957 Register result = ToRegister(instr->result());
2959 // Check that the function really is a function. Load map into the
2961 __ GetObjectType(function, result, scratch);
2962 DeoptimizeIf(ne, instr->environment(), scratch, Operand(JS_FUNCTION_TYPE));
2964 // Make sure that the function has an instance prototype.
2966 __ lbu(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
2967 __ And(scratch, scratch, Operand(1 << Map::kHasNonInstancePrototype));
2968 __ Branch(&non_instance, ne, scratch, Operand(zero_reg));
2970 // Get the prototype or initial map from the function.
2972 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2974 // Check that the function has a prototype or an initial map.
2975 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2976 DeoptimizeIf(eq, instr->environment(), result, Operand(at));
2978 // If the function does not have an initial map, we're done.
2980 __ GetObjectType(result, scratch, scratch);
2981 __ Branch(&done, ne, scratch, Operand(MAP_TYPE));
2983 // Get the prototype from the initial map.
2984 __ lw(result, FieldMemOperand(result, Map::kPrototypeOffset));
2987 // Non-instance prototype: Fetch prototype from constructor field
2989 __ bind(&non_instance);
2990 __ lw(result, FieldMemOperand(result, Map::kConstructorOffset));
2997 void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
2998 Register result = ToRegister(instr->result());
2999 __ LoadRoot(result, instr->index());
3003 void LCodeGen::DoLoadExternalArrayPointer(
3004 LLoadExternalArrayPointer* instr) {
3005 Register to_reg = ToRegister(instr->result());
3006 Register from_reg = ToRegister(instr->object());
3007 __ lw(to_reg, FieldMemOperand(from_reg,
3008 ExternalArray::kExternalPointerOffset));
3012 void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
3013 Register arguments = ToRegister(instr->arguments());
3014 Register result = ToRegister(instr->result());
3015 // There are two words between the frame pointer and the last argument.
3016 // Subtracting from length accounts for one of them add one more.
3017 if (instr->length()->IsConstantOperand()) {
3018 int const_length = ToInteger32(LConstantOperand::cast(instr->length()));
3019 if (instr->index()->IsConstantOperand()) {
3020 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3021 int index = (const_length - const_index) + 1;
3022 __ lw(result, MemOperand(arguments, index * kPointerSize));
3024 Register index = ToRegister(instr->index());
3025 __ li(at, Operand(const_length + 1));
3026 __ Subu(result, at, index);
3027 __ sll(at, result, kPointerSizeLog2);
3028 __ Addu(at, arguments, at);
3029 __ lw(result, MemOperand(at));
3031 } else if (instr->index()->IsConstantOperand()) {
3032 Register length = ToRegister(instr->length());
3033 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3034 int loc = const_index - 1;
3036 __ Subu(result, length, Operand(loc));
3037 __ sll(at, result, kPointerSizeLog2);
3038 __ Addu(at, arguments, at);
3039 __ lw(result, MemOperand(at));
3041 __ sll(at, length, kPointerSizeLog2);
3042 __ Addu(at, arguments, at);
3043 __ lw(result, MemOperand(at));
3046 Register length = ToRegister(instr->length());
3047 Register index = ToRegister(instr->index());
3048 __ Subu(result, length, index);
3049 __ Addu(result, result, 1);
3050 __ sll(at, result, kPointerSizeLog2);
3051 __ Addu(at, arguments, at);
3052 __ lw(result, MemOperand(at));
3057 void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
3058 Register external_pointer = ToRegister(instr->elements());
3059 Register key = no_reg;
3060 ElementsKind elements_kind = instr->elements_kind();
3061 bool key_is_constant = instr->key()->IsConstantOperand();
3062 int constant_key = 0;
3063 if (key_is_constant) {
3064 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3065 if (constant_key & 0xF0000000) {
3066 Abort(kArrayIndexConstantValueTooBig);
3069 key = ToRegister(instr->key());
3071 int element_size_shift = ElementsKindToShiftSize(elements_kind);
3072 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3073 ? (element_size_shift - kSmiTagSize) : element_size_shift;
3074 int additional_offset = IsFixedTypedArrayElementsKind(elements_kind)
3075 ? FixedTypedArrayBase::kDataOffset - kHeapObjectTag
3078 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3079 elements_kind == FLOAT32_ELEMENTS ||
3080 elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
3081 elements_kind == FLOAT64_ELEMENTS) {
3083 (instr->additional_index() << element_size_shift) + additional_offset;
3084 FPURegister result = ToDoubleRegister(instr->result());
3085 if (key_is_constant) {
3086 __ Addu(scratch0(), external_pointer, constant_key << element_size_shift);
3088 __ sll(scratch0(), key, shift_size);
3089 __ Addu(scratch0(), scratch0(), external_pointer);
3091 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3092 elements_kind == FLOAT32_ELEMENTS) {
3093 __ lwc1(result, MemOperand(scratch0(), base_offset));
3094 __ cvt_d_s(result, result);
3095 } else { // loading doubles, not floats.
3096 __ ldc1(result, MemOperand(scratch0(), base_offset));
3099 Register result = ToRegister(instr->result());
3100 MemOperand mem_operand = PrepareKeyedOperand(
3101 key, external_pointer, key_is_constant, constant_key,
3102 element_size_shift, shift_size,
3103 instr->additional_index(), additional_offset);
3104 switch (elements_kind) {
3105 case EXTERNAL_INT8_ELEMENTS:
3107 __ lb(result, mem_operand);
3109 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
3110 case EXTERNAL_UINT8_ELEMENTS:
3111 case UINT8_ELEMENTS:
3112 case UINT8_CLAMPED_ELEMENTS:
3113 __ lbu(result, mem_operand);
3115 case EXTERNAL_INT16_ELEMENTS:
3116 case INT16_ELEMENTS:
3117 __ lh(result, mem_operand);
3119 case EXTERNAL_UINT16_ELEMENTS:
3120 case UINT16_ELEMENTS:
3121 __ lhu(result, mem_operand);
3123 case EXTERNAL_INT32_ELEMENTS:
3124 case INT32_ELEMENTS:
3125 __ lw(result, mem_operand);
3127 case EXTERNAL_UINT32_ELEMENTS:
3128 case UINT32_ELEMENTS:
3129 __ lw(result, mem_operand);
3130 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
3131 DeoptimizeIf(Ugreater_equal, instr->environment(),
3132 result, Operand(0x80000000));
3135 case FLOAT32_ELEMENTS:
3136 case FLOAT64_ELEMENTS:
3137 case EXTERNAL_FLOAT32_ELEMENTS:
3138 case EXTERNAL_FLOAT64_ELEMENTS:
3139 case FAST_DOUBLE_ELEMENTS:
3141 case FAST_SMI_ELEMENTS:
3142 case FAST_HOLEY_DOUBLE_ELEMENTS:
3143 case FAST_HOLEY_ELEMENTS:
3144 case FAST_HOLEY_SMI_ELEMENTS:
3145 case DICTIONARY_ELEMENTS:
3146 case NON_STRICT_ARGUMENTS_ELEMENTS:
3154 void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
3155 Register elements = ToRegister(instr->elements());
3156 bool key_is_constant = instr->key()->IsConstantOperand();
3157 Register key = no_reg;
3158 DoubleRegister result = ToDoubleRegister(instr->result());
3159 Register scratch = scratch0();
3161 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
3164 FixedDoubleArray::kHeaderSize - kHeapObjectTag +
3165 (instr->additional_index() << element_size_shift);
3166 if (key_is_constant) {
3167 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3168 if (constant_key & 0xF0000000) {
3169 Abort(kArrayIndexConstantValueTooBig);
3171 base_offset += constant_key << element_size_shift;
3173 __ Addu(scratch, elements, Operand(base_offset));
3175 if (!key_is_constant) {
3176 key = ToRegister(instr->key());
3177 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3178 ? (element_size_shift - kSmiTagSize) : element_size_shift;
3179 __ sll(at, key, shift_size);
3180 __ Addu(scratch, scratch, at);
3183 __ ldc1(result, MemOperand(scratch));
3185 if (instr->hydrogen()->RequiresHoleCheck()) {
3186 __ lw(scratch, MemOperand(scratch, sizeof(kHoleNanLower32)));
3187 DeoptimizeIf(eq, instr->environment(), scratch, Operand(kHoleNanUpper32));
3192 void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
3193 Register elements = ToRegister(instr->elements());
3194 Register result = ToRegister(instr->result());
3195 Register scratch = scratch0();
3196 Register store_base = scratch;
3199 if (instr->key()->IsConstantOperand()) {
3200 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
3201 offset = FixedArray::OffsetOfElementAt(ToInteger32(const_operand) +
3202 instr->additional_index());
3203 store_base = elements;
3205 Register key = ToRegister(instr->key());
3206 // Even though the HLoadKeyed instruction forces the input
3207 // representation for the key to be an integer, the input gets replaced
3208 // during bound check elimination with the index argument to the bounds
3209 // check, which can be tagged, so that case must be handled here, too.
3210 if (instr->hydrogen()->key()->representation().IsSmi()) {
3211 __ sll(scratch, key, kPointerSizeLog2 - kSmiTagSize);
3212 __ addu(scratch, elements, scratch);
3214 __ sll(scratch, key, kPointerSizeLog2);
3215 __ addu(scratch, elements, scratch);
3217 offset = FixedArray::OffsetOfElementAt(instr->additional_index());
3219 __ lw(result, FieldMemOperand(store_base, offset));
3221 // Check for the hole value.
3222 if (instr->hydrogen()->RequiresHoleCheck()) {
3223 if (IsFastSmiElementsKind(instr->hydrogen()->elements_kind())) {
3224 __ SmiTst(result, scratch);
3225 DeoptimizeIf(ne, instr->environment(), scratch, Operand(zero_reg));
3227 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
3228 DeoptimizeIf(eq, instr->environment(), result, Operand(scratch));
3234 void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
3235 if (instr->is_typed_elements()) {
3236 DoLoadKeyedExternalArray(instr);
3237 } else if (instr->hydrogen()->representation().IsDouble()) {
3238 DoLoadKeyedFixedDoubleArray(instr);
3240 DoLoadKeyedFixedArray(instr);
3245 MemOperand LCodeGen::PrepareKeyedOperand(Register key,
3247 bool key_is_constant,
3251 int additional_index,
3252 int additional_offset) {
3253 int base_offset = (additional_index << element_size) + additional_offset;
3254 if (key_is_constant) {
3255 return MemOperand(base,
3256 base_offset + (constant_key << element_size));
3259 if (additional_offset != 0) {
3260 if (shift_size >= 0) {
3261 __ sll(scratch0(), key, shift_size);
3262 __ Addu(scratch0(), scratch0(), Operand(base_offset));
3264 ASSERT_EQ(-1, shift_size);
3265 __ srl(scratch0(), key, 1);
3266 __ Addu(scratch0(), scratch0(), Operand(base_offset));
3268 __ Addu(scratch0(), base, scratch0());
3269 return MemOperand(scratch0());
3272 if (additional_index != 0) {
3273 additional_index *= 1 << (element_size - shift_size);
3274 __ Addu(scratch0(), key, Operand(additional_index));
3277 if (additional_index == 0) {
3278 if (shift_size >= 0) {
3279 __ sll(scratch0(), key, shift_size);
3280 __ Addu(scratch0(), base, scratch0());
3281 return MemOperand(scratch0());
3283 ASSERT_EQ(-1, shift_size);
3284 __ srl(scratch0(), key, 1);
3285 __ Addu(scratch0(), base, scratch0());
3286 return MemOperand(scratch0());
3290 if (shift_size >= 0) {
3291 __ sll(scratch0(), scratch0(), shift_size);
3292 __ Addu(scratch0(), base, scratch0());
3293 return MemOperand(scratch0());
3295 ASSERT_EQ(-1, shift_size);
3296 __ srl(scratch0(), scratch0(), 1);
3297 __ Addu(scratch0(), base, scratch0());
3298 return MemOperand(scratch0());
3303 void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3304 ASSERT(ToRegister(instr->context()).is(cp));
3305 ASSERT(ToRegister(instr->object()).is(a1));
3306 ASSERT(ToRegister(instr->key()).is(a0));
3308 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Initialize();
3309 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3313 void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
3314 Register scratch = scratch0();
3315 Register temp = scratch1();
3316 Register result = ToRegister(instr->result());
3318 if (instr->hydrogen()->from_inlined()) {
3319 __ Subu(result, sp, 2 * kPointerSize);
3321 // Check if the calling frame is an arguments adaptor frame.
3322 Label done, adapted;
3323 __ lw(scratch, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3324 __ lw(result, MemOperand(scratch, StandardFrameConstants::kContextOffset));
3325 __ Xor(temp, result, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3327 // Result is the frame pointer for the frame if not adapted and for the real
3328 // frame below the adaptor frame if adapted.
3329 __ Movn(result, fp, temp); // Move only if temp is not equal to zero (ne).
3330 __ Movz(result, scratch, temp); // Move only if temp is equal to zero (eq).
3335 void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
3336 Register elem = ToRegister(instr->elements());
3337 Register result = ToRegister(instr->result());
3341 // If no arguments adaptor frame the number of arguments is fixed.
3342 __ Addu(result, zero_reg, Operand(scope()->num_parameters()));
3343 __ Branch(&done, eq, fp, Operand(elem));
3345 // Arguments adaptor frame present. Get argument length from there.
3346 __ lw(result, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3348 MemOperand(result, ArgumentsAdaptorFrameConstants::kLengthOffset));
3349 __ SmiUntag(result);
3351 // Argument length is in result register.
3356 void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
3357 Register receiver = ToRegister(instr->receiver());
3358 Register function = ToRegister(instr->function());
3359 Register result = ToRegister(instr->result());
3360 Register scratch = scratch0();
3362 // If the receiver is null or undefined, we have to pass the global
3363 // object as a receiver to normal functions. Values have to be
3364 // passed unchanged to builtins and strict-mode functions.
3365 Label global_object, result_in_receiver;
3367 // Do not transform the receiver to object for strict mode
3370 FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
3372 FieldMemOperand(scratch, SharedFunctionInfo::kCompilerHintsOffset));
3374 // Do not transform the receiver to object for builtins.
3375 int32_t strict_mode_function_mask =
3376 1 << (SharedFunctionInfo::kStrictModeFunction + kSmiTagSize);
3377 int32_t native_mask = 1 << (SharedFunctionInfo::kNative + kSmiTagSize);
3378 __ And(scratch, scratch, Operand(strict_mode_function_mask | native_mask));
3379 __ Branch(&result_in_receiver, ne, scratch, Operand(zero_reg));
3381 // Normal function. Replace undefined or null with global receiver.
3382 __ LoadRoot(scratch, Heap::kNullValueRootIndex);
3383 __ Branch(&global_object, eq, receiver, Operand(scratch));
3384 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
3385 __ Branch(&global_object, eq, receiver, Operand(scratch));
3387 // Deoptimize if the receiver is not a JS object.
3388 __ SmiTst(receiver, scratch);
3389 DeoptimizeIf(eq, instr->environment(), scratch, Operand(zero_reg));
3391 __ GetObjectType(receiver, scratch, scratch);
3392 DeoptimizeIf(lt, instr->environment(),
3393 scratch, Operand(FIRST_SPEC_OBJECT_TYPE));
3394 __ Branch(&result_in_receiver);
3396 __ bind(&global_object);
3397 __ lw(receiver, FieldMemOperand(function, JSFunction::kContextOffset));
3399 ContextOperand(receiver, Context::GLOBAL_OBJECT_INDEX));
3401 FieldMemOperand(receiver, GlobalObject::kGlobalReceiverOffset));
3403 if (result.is(receiver)) {
3404 __ bind(&result_in_receiver);
3407 __ Branch(&result_ok);
3408 __ bind(&result_in_receiver);
3409 __ mov(result, receiver);
3410 __ bind(&result_ok);
3415 void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3416 Register receiver = ToRegister(instr->receiver());
3417 Register function = ToRegister(instr->function());
3418 Register length = ToRegister(instr->length());
3419 Register elements = ToRegister(instr->elements());
3420 Register scratch = scratch0();
3421 ASSERT(receiver.is(a0)); // Used for parameter count.
3422 ASSERT(function.is(a1)); // Required by InvokeFunction.
3423 ASSERT(ToRegister(instr->result()).is(v0));
3425 // Copy the arguments to this function possibly from the
3426 // adaptor frame below it.
3427 const uint32_t kArgumentsLimit = 1 * KB;
3428 DeoptimizeIf(hi, instr->environment(), length, Operand(kArgumentsLimit));
3430 // Push the receiver and use the register to keep the original
3431 // number of arguments.
3433 __ Move(receiver, length);
3434 // The arguments are at a one pointer size offset from elements.
3435 __ Addu(elements, elements, Operand(1 * kPointerSize));
3437 // Loop through the arguments pushing them onto the execution
3440 // length is a small non-negative integer, due to the test above.
3441 __ Branch(USE_DELAY_SLOT, &invoke, eq, length, Operand(zero_reg));
3442 __ sll(scratch, length, 2);
3444 __ Addu(scratch, elements, scratch);
3445 __ lw(scratch, MemOperand(scratch));
3447 __ Subu(length, length, Operand(1));
3448 __ Branch(USE_DELAY_SLOT, &loop, ne, length, Operand(zero_reg));
3449 __ sll(scratch, length, 2);
3452 ASSERT(instr->HasPointerMap());
3453 LPointerMap* pointers = instr->pointer_map();
3454 SafepointGenerator safepoint_generator(
3455 this, pointers, Safepoint::kLazyDeopt);
3456 // The number of arguments is stored in receiver which is a0, as expected
3457 // by InvokeFunction.
3458 ParameterCount actual(receiver);
3459 __ InvokeFunction(function, actual, CALL_FUNCTION, safepoint_generator);
3463 void LCodeGen::DoPushArgument(LPushArgument* instr) {
3464 LOperand* argument = instr->value();
3465 if (argument->IsDoubleRegister() || argument->IsDoubleStackSlot()) {
3466 Abort(kDoPushArgumentNotImplementedForDoubleType);
3468 Register argument_reg = EmitLoadRegister(argument, at);
3469 __ push(argument_reg);
3474 void LCodeGen::DoDrop(LDrop* instr) {
3475 __ Drop(instr->count());
3479 void LCodeGen::DoThisFunction(LThisFunction* instr) {
3480 Register result = ToRegister(instr->result());
3481 __ lw(result, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3485 void LCodeGen::DoContext(LContext* instr) {
3486 // If there is a non-return use, the context must be moved to a register.
3487 Register result = ToRegister(instr->result());
3488 if (info()->IsOptimizing()) {
3489 __ lw(result, MemOperand(fp, StandardFrameConstants::kContextOffset));
3491 // If there is no frame, the context must be in cp.
3492 ASSERT(result.is(cp));
3497 void LCodeGen::DoOuterContext(LOuterContext* instr) {
3498 Register context = ToRegister(instr->context());
3499 Register result = ToRegister(instr->result());
3501 MemOperand(context, Context::SlotOffset(Context::PREVIOUS_INDEX)));
3505 void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
3506 ASSERT(ToRegister(instr->context()).is(cp));
3507 __ li(scratch0(), instr->hydrogen()->pairs());
3508 __ li(scratch1(), Operand(Smi::FromInt(instr->hydrogen()->flags())));
3509 // The context is the first argument.
3510 __ Push(cp, scratch0(), scratch1());
3511 CallRuntime(Runtime::kDeclareGlobals, 3, instr);
3515 void LCodeGen::DoGlobalObject(LGlobalObject* instr) {
3516 Register context = ToRegister(instr->context());
3517 Register result = ToRegister(instr->result());
3518 __ lw(result, ContextOperand(context, Context::GLOBAL_OBJECT_INDEX));
3522 void LCodeGen::DoGlobalReceiver(LGlobalReceiver* instr) {
3523 Register global = ToRegister(instr->global_object());
3524 Register result = ToRegister(instr->result());
3525 __ lw(result, FieldMemOperand(global, GlobalObject::kGlobalReceiverOffset));
3529 void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
3530 int formal_parameter_count,
3532 LInstruction* instr,
3534 bool dont_adapt_arguments =
3535 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3536 bool can_invoke_directly =
3537 dont_adapt_arguments || formal_parameter_count == arity;
3539 LPointerMap* pointers = instr->pointer_map();
3541 if (can_invoke_directly) {
3542 if (a1_state == A1_UNINITIALIZED) {
3543 __ li(a1, function);
3547 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
3549 // Set r0 to arguments count if adaption is not needed. Assumes that r0
3550 // is available to write to at this point.
3551 if (dont_adapt_arguments) {
3552 __ li(a0, Operand(arity));
3556 __ lw(at, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
3559 // Set up deoptimization.
3560 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3562 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3563 ParameterCount count(arity);
3564 ParameterCount expected(formal_parameter_count);
3565 __ InvokeFunction(function, expected, count, CALL_FUNCTION, generator);
3570 void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3571 ASSERT(instr->context() != NULL);
3572 ASSERT(ToRegister(instr->context()).is(cp));
3573 Register input = ToRegister(instr->value());
3574 Register result = ToRegister(instr->result());
3575 Register scratch = scratch0();
3577 // Deoptimize if not a heap number.
3578 __ lw(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
3579 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
3580 DeoptimizeIf(ne, instr->environment(), scratch, Operand(at));
3583 Register exponent = scratch0();
3585 __ lw(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3586 // Check the sign of the argument. If the argument is positive, just
3588 __ Move(result, input);
3589 __ And(at, exponent, Operand(HeapNumber::kSignMask));
3590 __ Branch(&done, eq, at, Operand(zero_reg));
3592 // Input is negative. Reverse its sign.
3593 // Preserve the value of all registers.
3595 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
3597 // Registers were saved at the safepoint, so we can use
3598 // many scratch registers.
3599 Register tmp1 = input.is(a1) ? a0 : a1;
3600 Register tmp2 = input.is(a2) ? a0 : a2;
3601 Register tmp3 = input.is(a3) ? a0 : a3;
3602 Register tmp4 = input.is(t0) ? a0 : t0;
3604 // exponent: floating point exponent value.
3606 Label allocated, slow;
3607 __ LoadRoot(tmp4, Heap::kHeapNumberMapRootIndex);
3608 __ AllocateHeapNumber(tmp1, tmp2, tmp3, tmp4, &slow);
3609 __ Branch(&allocated);
3611 // Slow case: Call the runtime system to do the number allocation.
3614 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr,
3616 // Set the pointer to the new heap number in tmp.
3619 // Restore input_reg after call to runtime.
3620 __ LoadFromSafepointRegisterSlot(input, input);
3621 __ lw(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3623 __ bind(&allocated);
3624 // exponent: floating point exponent value.
3625 // tmp1: allocated heap number.
3626 __ And(exponent, exponent, Operand(~HeapNumber::kSignMask));
3627 __ sw(exponent, FieldMemOperand(tmp1, HeapNumber::kExponentOffset));
3628 __ lw(tmp2, FieldMemOperand(input, HeapNumber::kMantissaOffset));
3629 __ sw(tmp2, FieldMemOperand(tmp1, HeapNumber::kMantissaOffset));
3631 __ StoreToSafepointRegisterSlot(tmp1, result);
3638 void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3639 Register input = ToRegister(instr->value());
3640 Register result = ToRegister(instr->result());
3641 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
3643 __ Branch(USE_DELAY_SLOT, &done, ge, input, Operand(zero_reg));
3644 __ mov(result, input);
3645 __ subu(result, zero_reg, input);
3646 // Overflow if result is still negative, i.e. 0x80000000.
3647 DeoptimizeIf(lt, instr->environment(), result, Operand(zero_reg));
3652 void LCodeGen::DoMathAbs(LMathAbs* instr) {
3653 // Class for deferred case.
3654 class DeferredMathAbsTaggedHeapNumber V8_FINAL : public LDeferredCode {
3656 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr)
3657 : LDeferredCode(codegen), instr_(instr) { }
3658 virtual void Generate() V8_OVERRIDE {
3659 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3661 virtual LInstruction* instr() V8_OVERRIDE { return instr_; }
3666 Representation r = instr->hydrogen()->value()->representation();
3668 FPURegister input = ToDoubleRegister(instr->value());
3669 FPURegister result = ToDoubleRegister(instr->result());
3670 __ abs_d(result, input);
3671 } else if (r.IsSmiOrInteger32()) {
3672 EmitIntegerMathAbs(instr);
3674 // Representation is tagged.
3675 DeferredMathAbsTaggedHeapNumber* deferred =
3676 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3677 Register input = ToRegister(instr->value());
3679 __ JumpIfNotSmi(input, deferred->entry());
3680 // If smi, handle it directly.
3681 EmitIntegerMathAbs(instr);
3682 __ bind(deferred->exit());
3687 void LCodeGen::DoMathFloor(LMathFloor* instr) {
3688 DoubleRegister input = ToDoubleRegister(instr->value());
3689 Register result = ToRegister(instr->result());
3690 Register scratch1 = scratch0();
3691 Register except_flag = ToRegister(instr->temp());
3693 __ EmitFPUTruncate(kRoundToMinusInf,
3700 // Deopt if the operation did not succeed.
3701 DeoptimizeIf(ne, instr->environment(), except_flag, Operand(zero_reg));
3703 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3706 __ Branch(&done, ne, result, Operand(zero_reg));
3707 __ mfc1(scratch1, input.high());
3708 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
3709 DeoptimizeIf(ne, instr->environment(), scratch1, Operand(zero_reg));
3715 void LCodeGen::DoMathRound(LMathRound* instr) {
3716 DoubleRegister input = ToDoubleRegister(instr->value());
3717 Register result = ToRegister(instr->result());
3718 DoubleRegister double_scratch1 = ToDoubleRegister(instr->temp());
3719 Register scratch = scratch0();
3720 Label done, check_sign_on_zero;
3722 // Extract exponent bits.
3723 __ mfc1(result, input.high());
3726 HeapNumber::kExponentShift,
3727 HeapNumber::kExponentBits);
3729 // If the number is in ]-0.5, +0.5[, the result is +/- 0.
3731 __ Branch(&skip1, gt, scratch, Operand(HeapNumber::kExponentBias - 2));
3732 __ mov(result, zero_reg);
3733 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3734 __ Branch(&check_sign_on_zero);
3740 // The following conversion will not work with numbers
3741 // outside of ]-2^32, 2^32[.
3742 DeoptimizeIf(ge, instr->environment(), scratch,
3743 Operand(HeapNumber::kExponentBias + 32));
3745 // Save the original sign for later comparison.
3746 __ And(scratch, result, Operand(HeapNumber::kSignMask));
3748 __ Move(double_scratch0(), 0.5);
3749 __ add_d(double_scratch0(), input, double_scratch0());
3751 // Check sign of the result: if the sign changed, the input
3752 // value was in ]0.5, 0[ and the result should be -0.
3753 __ mfc1(result, double_scratch0().high());
3754 __ Xor(result, result, Operand(scratch));
3755 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3756 // ARM uses 'mi' here, which is 'lt'
3757 DeoptimizeIf(lt, instr->environment(), result,
3761 // ARM uses 'mi' here, which is 'lt'
3762 // Negating it results in 'ge'
3763 __ Branch(&skip2, ge, result, Operand(zero_reg));
3764 __ mov(result, zero_reg);
3769 Register except_flag = scratch;
3770 __ EmitFPUTruncate(kRoundToMinusInf,
3777 DeoptimizeIf(ne, instr->environment(), except_flag, Operand(zero_reg));
3779 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3781 __ Branch(&done, ne, result, Operand(zero_reg));
3782 __ bind(&check_sign_on_zero);
3783 __ mfc1(scratch, input.high());
3784 __ And(scratch, scratch, Operand(HeapNumber::kSignMask));
3785 DeoptimizeIf(ne, instr->environment(), scratch, Operand(zero_reg));
3791 void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3792 DoubleRegister input = ToDoubleRegister(instr->value());
3793 DoubleRegister result = ToDoubleRegister(instr->result());
3794 __ sqrt_d(result, input);
3798 void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3799 DoubleRegister input = ToDoubleRegister(instr->value());
3800 DoubleRegister result = ToDoubleRegister(instr->result());
3801 DoubleRegister temp = ToDoubleRegister(instr->temp());
3803 ASSERT(!input.is(result));
3805 // Note that according to ECMA-262 15.8.2.13:
3806 // Math.pow(-Infinity, 0.5) == Infinity
3807 // Math.sqrt(-Infinity) == NaN
3809 __ Move(temp, -V8_INFINITY);
3810 __ BranchF(USE_DELAY_SLOT, &done, NULL, eq, temp, input);
3811 // Set up Infinity in the delay slot.
3812 // result is overwritten if the branch is not taken.
3813 __ neg_d(result, temp);
3815 // Add +0 to convert -0 to +0.
3816 __ add_d(result, input, kDoubleRegZero);
3817 __ sqrt_d(result, result);
3822 void LCodeGen::DoPower(LPower* instr) {
3823 Representation exponent_type = instr->hydrogen()->right()->representation();
3824 // Having marked this as a call, we can use any registers.
3825 // Just make sure that the input/output registers are the expected ones.
3826 ASSERT(!instr->right()->IsDoubleRegister() ||
3827 ToDoubleRegister(instr->right()).is(f4));
3828 ASSERT(!instr->right()->IsRegister() ||
3829 ToRegister(instr->right()).is(a2));
3830 ASSERT(ToDoubleRegister(instr->left()).is(f2));
3831 ASSERT(ToDoubleRegister(instr->result()).is(f0));
3833 if (exponent_type.IsSmi()) {
3834 MathPowStub stub(MathPowStub::TAGGED);
3836 } else if (exponent_type.IsTagged()) {
3838 __ JumpIfSmi(a2, &no_deopt);
3839 __ lw(t3, FieldMemOperand(a2, HeapObject::kMapOffset));
3840 DeoptimizeIf(ne, instr->environment(), t3, Operand(at));
3842 MathPowStub stub(MathPowStub::TAGGED);
3844 } else if (exponent_type.IsInteger32()) {
3845 MathPowStub stub(MathPowStub::INTEGER);
3848 ASSERT(exponent_type.IsDouble());
3849 MathPowStub stub(MathPowStub::DOUBLE);
3855 void LCodeGen::DoMathExp(LMathExp* instr) {
3856 DoubleRegister input = ToDoubleRegister(instr->value());
3857 DoubleRegister result = ToDoubleRegister(instr->result());
3858 DoubleRegister double_scratch1 = ToDoubleRegister(instr->double_temp());
3859 DoubleRegister double_scratch2 = double_scratch0();
3860 Register temp1 = ToRegister(instr->temp1());
3861 Register temp2 = ToRegister(instr->temp2());
3863 MathExpGenerator::EmitMathExp(
3864 masm(), input, result, double_scratch1, double_scratch2,
3865 temp1, temp2, scratch0());
3869 void LCodeGen::DoMathLog(LMathLog* instr) {
3870 __ PrepareCallCFunction(0, 1, scratch0());
3871 __ MovToFloatParameter(ToDoubleRegister(instr->value()));
3872 __ CallCFunction(ExternalReference::math_log_double_function(isolate()),
3874 __ MovFromFloatResult(ToDoubleRegister(instr->result()));
3878 void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
3879 ASSERT(ToRegister(instr->context()).is(cp));
3880 ASSERT(ToRegister(instr->function()).is(a1));
3881 ASSERT(instr->HasPointerMap());
3883 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
3884 if (known_function.is_null()) {
3885 LPointerMap* pointers = instr->pointer_map();
3886 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3887 ParameterCount count(instr->arity());
3888 __ InvokeFunction(a1, count, CALL_FUNCTION, generator);
3890 CallKnownFunction(known_function,
3891 instr->hydrogen()->formal_parameter_count(),
3894 A1_CONTAINS_TARGET);
3899 void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
3900 ASSERT(ToRegister(instr->result()).is(v0));
3902 LPointerMap* pointers = instr->pointer_map();
3903 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3905 if (instr->target()->IsConstantOperand()) {
3906 LConstantOperand* target = LConstantOperand::cast(instr->target());
3907 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3908 generator.BeforeCall(__ CallSize(code, RelocInfo::CODE_TARGET));
3909 __ Call(code, RelocInfo::CODE_TARGET);
3911 ASSERT(instr->target()->IsRegister());
3912 Register target = ToRegister(instr->target());
3913 generator.BeforeCall(__ CallSize(target));
3914 __ Addu(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
3917 generator.AfterCall();
3921 void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
3922 ASSERT(ToRegister(instr->function()).is(a1));
3923 ASSERT(ToRegister(instr->result()).is(v0));
3925 if (instr->hydrogen()->pass_argument_count()) {
3926 __ li(a0, Operand(instr->arity()));
3930 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
3932 // Load the code entry address
3933 __ lw(at, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
3936 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3940 void LCodeGen::DoCallFunction(LCallFunction* instr) {
3941 ASSERT(ToRegister(instr->context()).is(cp));
3942 ASSERT(ToRegister(instr->function()).is(a1));
3943 ASSERT(ToRegister(instr->result()).is(v0));
3945 int arity = instr->arity();
3946 CallFunctionStub stub(arity, NO_CALL_FUNCTION_FLAGS);
3947 if (instr->hydrogen()->IsTailCall()) {
3948 if (NeedsEagerFrame()) __ mov(sp, fp);
3949 __ Jump(stub.GetCode(isolate()), RelocInfo::CODE_TARGET);
3951 CallCode(stub.GetCode(isolate()), RelocInfo::CODE_TARGET, instr);
3956 void LCodeGen::DoCallNew(LCallNew* instr) {
3957 ASSERT(ToRegister(instr->context()).is(cp));
3958 ASSERT(ToRegister(instr->constructor()).is(a1));
3959 ASSERT(ToRegister(instr->result()).is(v0));
3961 __ li(a0, Operand(instr->arity()));
3962 // No cell in a2 for construct type feedback in optimized code
3963 Handle<Object> undefined_value(isolate()->factory()->undefined_value());
3964 __ li(a2, Operand(undefined_value));
3965 CallConstructStub stub(NO_CALL_FUNCTION_FLAGS);
3966 CallCode(stub.GetCode(isolate()), RelocInfo::CONSTRUCT_CALL, instr);
3970 void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
3971 ASSERT(ToRegister(instr->context()).is(cp));
3972 ASSERT(ToRegister(instr->constructor()).is(a1));
3973 ASSERT(ToRegister(instr->result()).is(v0));
3975 __ li(a0, Operand(instr->arity()));
3976 __ li(a2, Operand(factory()->undefined_value()));
3977 ElementsKind kind = instr->hydrogen()->elements_kind();
3978 AllocationSiteOverrideMode override_mode =
3979 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
3980 ? DISABLE_ALLOCATION_SITES
3983 if (instr->arity() == 0) {
3984 ArrayNoArgumentConstructorStub stub(kind, override_mode);
3985 CallCode(stub.GetCode(isolate()), RelocInfo::CONSTRUCT_CALL, instr);
3986 } else if (instr->arity() == 1) {
3988 if (IsFastPackedElementsKind(kind)) {
3990 // We might need a change here,
3991 // look at the first argument.
3992 __ lw(t1, MemOperand(sp, 0));
3993 __ Branch(&packed_case, eq, t1, Operand(zero_reg));
3995 ElementsKind holey_kind = GetHoleyElementsKind(kind);
3996 ArraySingleArgumentConstructorStub stub(holey_kind, override_mode);
3997 CallCode(stub.GetCode(isolate()), RelocInfo::CONSTRUCT_CALL, instr);
3999 __ bind(&packed_case);
4002 ArraySingleArgumentConstructorStub stub(kind, override_mode);
4003 CallCode(stub.GetCode(isolate()), RelocInfo::CONSTRUCT_CALL, instr);
4006 ArrayNArgumentsConstructorStub stub(kind, override_mode);
4007 CallCode(stub.GetCode(isolate()), RelocInfo::CONSTRUCT_CALL, instr);
4012 void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
4013 CallRuntime(instr->function(), instr->arity(), instr);
4017 void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
4018 Register function = ToRegister(instr->function());
4019 Register code_object = ToRegister(instr->code_object());
4020 __ Addu(code_object, code_object,
4021 Operand(Code::kHeaderSize - kHeapObjectTag));
4023 FieldMemOperand(function, JSFunction::kCodeEntryOffset));
4027 void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
4028 Register result = ToRegister(instr->result());
4029 Register base = ToRegister(instr->base_object());
4030 if (instr->offset()->IsConstantOperand()) {
4031 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
4032 __ Addu(result, base, Operand(ToInteger32(offset)));
4034 Register offset = ToRegister(instr->offset());
4035 __ Addu(result, base, offset);
4040 void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
4041 Representation representation = instr->representation();
4043 Register object = ToRegister(instr->object());
4044 Register scratch = scratch0();
4045 HObjectAccess access = instr->hydrogen()->access();
4046 int offset = access.offset();
4048 if (access.IsExternalMemory()) {
4049 Register value = ToRegister(instr->value());
4050 MemOperand operand = MemOperand(object, offset);
4051 __ Store(value, operand, representation);
4055 Handle<Map> transition = instr->transition();
4057 if (FLAG_track_heap_object_fields && representation.IsHeapObject()) {
4058 Register value = ToRegister(instr->value());
4059 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
4060 __ SmiTst(value, scratch);
4061 DeoptimizeIf(eq, instr->environment(), scratch, Operand(zero_reg));
4063 } else if (FLAG_track_double_fields && representation.IsDouble()) {
4064 ASSERT(transition.is_null());
4065 ASSERT(access.IsInobject());
4066 ASSERT(!instr->hydrogen()->NeedsWriteBarrier());
4067 DoubleRegister value = ToDoubleRegister(instr->value());
4068 __ sdc1(value, FieldMemOperand(object, offset));
4072 if (!transition.is_null()) {
4073 __ li(scratch, Operand(transition));
4074 __ sw(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
4075 if (instr->hydrogen()->NeedsWriteBarrierForMap()) {
4076 Register temp = ToRegister(instr->temp());
4077 // Update the write barrier for the map field.
4078 __ RecordWriteField(object,
4079 HeapObject::kMapOffset,
4084 OMIT_REMEMBERED_SET,
4090 Register value = ToRegister(instr->value());
4091 ASSERT(!object.is(value));
4092 SmiCheck check_needed =
4093 instr->hydrogen()->value()->IsHeapObject()
4094 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4095 if (access.IsInobject()) {
4096 MemOperand operand = FieldMemOperand(object, offset);
4097 __ Store(value, operand, representation);
4098 if (instr->hydrogen()->NeedsWriteBarrier()) {
4099 // Update the write barrier for the object for in-object properties.
4100 __ RecordWriteField(object,
4106 EMIT_REMEMBERED_SET,
4110 __ lw(scratch, FieldMemOperand(object, JSObject::kPropertiesOffset));
4111 MemOperand operand = FieldMemOperand(scratch, offset);
4112 __ Store(value, operand, representation);
4113 if (instr->hydrogen()->NeedsWriteBarrier()) {
4114 // Update the write barrier for the properties array.
4115 // object is used as a scratch register.
4116 __ RecordWriteField(scratch,
4122 EMIT_REMEMBERED_SET,
4129 void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
4130 ASSERT(ToRegister(instr->context()).is(cp));
4131 ASSERT(ToRegister(instr->object()).is(a1));
4132 ASSERT(ToRegister(instr->value()).is(a0));
4134 // Name is always in a2.
4135 __ li(a2, Operand(instr->name()));
4136 Handle<Code> ic = StoreIC::initialize_stub(isolate(),
4137 instr->strict_mode_flag());
4138 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4142 void LCodeGen::ApplyCheckIf(Condition condition,
4143 LBoundsCheck* check,
4145 const Operand& src2) {
4146 if (FLAG_debug_code && check->hydrogen()->skip_check()) {
4148 __ Branch(&done, NegateCondition(condition), src1, src2);
4149 __ stop("eliminated bounds check failed");
4152 DeoptimizeIf(condition, check->environment(), src1, src2);
4157 void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
4158 if (instr->hydrogen()->skip_check()) return;
4160 Condition condition = instr->hydrogen()->allow_equality() ? hi : hs;
4161 if (instr->index()->IsConstantOperand()) {
4162 int constant_index =
4163 ToInteger32(LConstantOperand::cast(instr->index()));
4164 if (instr->hydrogen()->length()->representation().IsSmi()) {
4165 __ li(at, Operand(Smi::FromInt(constant_index)));
4167 __ li(at, Operand(constant_index));
4169 ApplyCheckIf(condition,
4172 Operand(ToRegister(instr->length())));
4174 ApplyCheckIf(condition,
4176 ToRegister(instr->index()),
4177 Operand(ToRegister(instr->length())));
4182 void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
4183 Register external_pointer = ToRegister(instr->elements());
4184 Register key = no_reg;
4185 ElementsKind elements_kind = instr->elements_kind();
4186 bool key_is_constant = instr->key()->IsConstantOperand();
4187 int constant_key = 0;
4188 if (key_is_constant) {
4189 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4190 if (constant_key & 0xF0000000) {
4191 Abort(kArrayIndexConstantValueTooBig);
4194 key = ToRegister(instr->key());
4196 int element_size_shift = ElementsKindToShiftSize(elements_kind);
4197 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4198 ? (element_size_shift - kSmiTagSize) : element_size_shift;
4199 int additional_offset = IsFixedTypedArrayElementsKind(elements_kind)
4200 ? FixedTypedArrayBase::kDataOffset - kHeapObjectTag
4203 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4204 elements_kind == FLOAT32_ELEMENTS ||
4205 elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
4206 elements_kind == FLOAT64_ELEMENTS) {
4208 (instr->additional_index() << element_size_shift) + additional_offset;
4209 Register address = scratch0();
4210 FPURegister value(ToDoubleRegister(instr->value()));
4211 if (key_is_constant) {
4212 if (constant_key != 0) {
4213 __ Addu(address, external_pointer,
4214 Operand(constant_key << element_size_shift));
4216 address = external_pointer;
4219 __ sll(address, key, shift_size);
4220 __ Addu(address, external_pointer, address);
4223 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4224 elements_kind == FLOAT32_ELEMENTS) {
4225 __ cvt_s_d(double_scratch0(), value);
4226 __ swc1(double_scratch0(), MemOperand(address, base_offset));
4227 } else { // Storing doubles, not floats.
4228 __ sdc1(value, MemOperand(address, base_offset));
4231 Register value(ToRegister(instr->value()));
4232 MemOperand mem_operand = PrepareKeyedOperand(
4233 key, external_pointer, key_is_constant, constant_key,
4234 element_size_shift, shift_size,
4235 instr->additional_index(), additional_offset);
4236 switch (elements_kind) {
4237 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
4238 case EXTERNAL_INT8_ELEMENTS:
4239 case EXTERNAL_UINT8_ELEMENTS:
4240 case UINT8_ELEMENTS:
4241 case UINT8_CLAMPED_ELEMENTS:
4243 __ sb(value, mem_operand);
4245 case EXTERNAL_INT16_ELEMENTS:
4246 case EXTERNAL_UINT16_ELEMENTS:
4247 case INT16_ELEMENTS:
4248 case UINT16_ELEMENTS:
4249 __ sh(value, mem_operand);
4251 case EXTERNAL_INT32_ELEMENTS:
4252 case EXTERNAL_UINT32_ELEMENTS:
4253 case INT32_ELEMENTS:
4254 case UINT32_ELEMENTS:
4255 __ sw(value, mem_operand);
4257 case FLOAT32_ELEMENTS:
4258 case FLOAT64_ELEMENTS:
4259 case EXTERNAL_FLOAT32_ELEMENTS:
4260 case EXTERNAL_FLOAT64_ELEMENTS:
4261 case FAST_DOUBLE_ELEMENTS:
4263 case FAST_SMI_ELEMENTS:
4264 case FAST_HOLEY_DOUBLE_ELEMENTS:
4265 case FAST_HOLEY_ELEMENTS:
4266 case FAST_HOLEY_SMI_ELEMENTS:
4267 case DICTIONARY_ELEMENTS:
4268 case NON_STRICT_ARGUMENTS_ELEMENTS:
4276 void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
4277 DoubleRegister value = ToDoubleRegister(instr->value());
4278 Register elements = ToRegister(instr->elements());
4279 Register scratch = scratch0();
4280 DoubleRegister double_scratch = double_scratch0();
4281 bool key_is_constant = instr->key()->IsConstantOperand();
4282 Label not_nan, done;
4284 // Calculate the effective address of the slot in the array to store the
4286 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
4287 if (key_is_constant) {
4288 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4289 if (constant_key & 0xF0000000) {
4290 Abort(kArrayIndexConstantValueTooBig);
4292 __ Addu(scratch, elements,
4293 Operand((constant_key << element_size_shift) +
4294 FixedDoubleArray::kHeaderSize - kHeapObjectTag));
4296 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4297 ? (element_size_shift - kSmiTagSize) : element_size_shift;
4298 __ Addu(scratch, elements,
4299 Operand(FixedDoubleArray::kHeaderSize - kHeapObjectTag));
4300 __ sll(at, ToRegister(instr->key()), shift_size);
4301 __ Addu(scratch, scratch, at);
4304 if (instr->NeedsCanonicalization()) {
4306 // Check for NaN. All NaNs must be canonicalized.
4307 __ BranchF(NULL, &is_nan, eq, value, value);
4308 __ Branch(¬_nan);
4310 // Only load canonical NaN if the comparison above set the overflow.
4312 __ Move(double_scratch,
4313 FixedDoubleArray::canonical_not_the_hole_nan_as_double());
4314 __ sdc1(double_scratch, MemOperand(scratch, instr->additional_index() <<
4315 element_size_shift));
4320 __ sdc1(value, MemOperand(scratch, instr->additional_index() <<
4321 element_size_shift));
4326 void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4327 Register value = ToRegister(instr->value());
4328 Register elements = ToRegister(instr->elements());
4329 Register key = instr->key()->IsRegister() ? ToRegister(instr->key())
4331 Register scratch = scratch0();
4332 Register store_base = scratch;
4336 if (instr->key()->IsConstantOperand()) {
4337 ASSERT(!instr->hydrogen()->NeedsWriteBarrier());
4338 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
4339 offset = FixedArray::OffsetOfElementAt(ToInteger32(const_operand) +
4340 instr->additional_index());
4341 store_base = elements;
4343 // Even though the HLoadKeyed instruction forces the input
4344 // representation for the key to be an integer, the input gets replaced
4345 // during bound check elimination with the index argument to the bounds
4346 // check, which can be tagged, so that case must be handled here, too.
4347 if (instr->hydrogen()->key()->representation().IsSmi()) {
4348 __ sll(scratch, key, kPointerSizeLog2 - kSmiTagSize);
4349 __ addu(scratch, elements, scratch);
4351 __ sll(scratch, key, kPointerSizeLog2);
4352 __ addu(scratch, elements, scratch);
4354 offset = FixedArray::OffsetOfElementAt(instr->additional_index());
4356 __ sw(value, FieldMemOperand(store_base, offset));
4358 if (instr->hydrogen()->NeedsWriteBarrier()) {
4359 SmiCheck check_needed =
4360 instr->hydrogen()->value()->IsHeapObject()
4361 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4362 // Compute address of modified element and store it into key register.
4363 __ Addu(key, store_base, Operand(offset - kHeapObjectTag));
4364 __ RecordWrite(elements,
4369 EMIT_REMEMBERED_SET,
4375 void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4376 // By cases: external, fast double
4377 if (instr->is_typed_elements()) {
4378 DoStoreKeyedExternalArray(instr);
4379 } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4380 DoStoreKeyedFixedDoubleArray(instr);
4382 DoStoreKeyedFixedArray(instr);
4387 void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4388 ASSERT(ToRegister(instr->context()).is(cp));
4389 ASSERT(ToRegister(instr->object()).is(a2));
4390 ASSERT(ToRegister(instr->key()).is(a1));
4391 ASSERT(ToRegister(instr->value()).is(a0));
4393 Handle<Code> ic = (instr->strict_mode_flag() == kStrictMode)
4394 ? isolate()->builtins()->KeyedStoreIC_Initialize_Strict()
4395 : isolate()->builtins()->KeyedStoreIC_Initialize();
4396 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4400 void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4401 Register object_reg = ToRegister(instr->object());
4402 Register scratch = scratch0();
4404 Handle<Map> from_map = instr->original_map();
4405 Handle<Map> to_map = instr->transitioned_map();
4406 ElementsKind from_kind = instr->from_kind();
4407 ElementsKind to_kind = instr->to_kind();
4409 Label not_applicable;
4410 __ lw(scratch, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4411 __ Branch(¬_applicable, ne, scratch, Operand(from_map));
4413 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
4414 Register new_map_reg = ToRegister(instr->new_map_temp());
4415 __ li(new_map_reg, Operand(to_map));
4416 __ sw(new_map_reg, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4418 __ RecordWriteField(object_reg, HeapObject::kMapOffset, new_map_reg,
4419 scratch, GetRAState(), kDontSaveFPRegs);
4421 ASSERT(ToRegister(instr->context()).is(cp));
4422 PushSafepointRegistersScope scope(
4423 this, Safepoint::kWithRegistersAndDoubles);
4424 __ mov(a0, object_reg);
4425 __ li(a1, Operand(to_map));
4426 TransitionElementsKindStub stub(from_kind, to_kind);
4428 RecordSafepointWithRegistersAndDoubles(
4429 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4431 __ bind(¬_applicable);
4435 void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4436 Register object = ToRegister(instr->object());
4437 Register temp = ToRegister(instr->temp());
4438 Label no_memento_found;
4439 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found,
4440 ne, &no_memento_found);
4441 DeoptimizeIf(al, instr->environment());
4442 __ bind(&no_memento_found);
4446 void LCodeGen::DoStringAdd(LStringAdd* instr) {
4447 ASSERT(ToRegister(instr->context()).is(cp));
4448 ASSERT(ToRegister(instr->left()).is(a1));
4449 ASSERT(ToRegister(instr->right()).is(a0));
4450 StringAddStub stub(instr->hydrogen()->flags(),
4451 isolate()->heap()->GetPretenureMode());
4452 CallCode(stub.GetCode(isolate()), RelocInfo::CODE_TARGET, instr);
4456 void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
4457 class DeferredStringCharCodeAt V8_FINAL : public LDeferredCode {
4459 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
4460 : LDeferredCode(codegen), instr_(instr) { }
4461 virtual void Generate() V8_OVERRIDE {
4462 codegen()->DoDeferredStringCharCodeAt(instr_);
4464 virtual LInstruction* instr() V8_OVERRIDE { return instr_; }
4466 LStringCharCodeAt* instr_;
4469 DeferredStringCharCodeAt* deferred =
4470 new(zone()) DeferredStringCharCodeAt(this, instr);
4471 StringCharLoadGenerator::Generate(masm(),
4472 ToRegister(instr->string()),
4473 ToRegister(instr->index()),
4474 ToRegister(instr->result()),
4476 __ bind(deferred->exit());
4480 void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4481 Register string = ToRegister(instr->string());
4482 Register result = ToRegister(instr->result());
4483 Register scratch = scratch0();
4485 // TODO(3095996): Get rid of this. For now, we need to make the
4486 // result register contain a valid pointer because it is already
4487 // contained in the register pointer map.
4488 __ mov(result, zero_reg);
4490 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
4492 // Push the index as a smi. This is safe because of the checks in
4493 // DoStringCharCodeAt above.
4494 if (instr->index()->IsConstantOperand()) {
4495 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
4496 __ Addu(scratch, zero_reg, Operand(Smi::FromInt(const_index)));
4499 Register index = ToRegister(instr->index());
4503 CallRuntimeFromDeferred(Runtime::kStringCharCodeAt, 2, instr,
4507 __ StoreToSafepointRegisterSlot(v0, result);
4511 void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
4512 class DeferredStringCharFromCode V8_FINAL : public LDeferredCode {
4514 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
4515 : LDeferredCode(codegen), instr_(instr) { }
4516 virtual void Generate() V8_OVERRIDE {
4517 codegen()->DoDeferredStringCharFromCode(instr_);
4519 virtual LInstruction* instr() V8_OVERRIDE { return instr_; }
4521 LStringCharFromCode* instr_;
4524 DeferredStringCharFromCode* deferred =
4525 new(zone()) DeferredStringCharFromCode(this, instr);
4527 ASSERT(instr->hydrogen()->value()->representation().IsInteger32());
4528 Register char_code = ToRegister(instr->char_code());
4529 Register result = ToRegister(instr->result());
4530 Register scratch = scratch0();
4531 ASSERT(!char_code.is(result));
4533 __ Branch(deferred->entry(), hi,
4534 char_code, Operand(String::kMaxOneByteCharCode));
4535 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
4536 __ sll(scratch, char_code, kPointerSizeLog2);
4537 __ Addu(result, result, scratch);
4538 __ lw(result, FieldMemOperand(result, FixedArray::kHeaderSize));
4539 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
4540 __ Branch(deferred->entry(), eq, result, Operand(scratch));
4541 __ bind(deferred->exit());
4545 void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4546 Register char_code = ToRegister(instr->char_code());
4547 Register result = ToRegister(instr->result());
4549 // TODO(3095996): Get rid of this. For now, we need to make the
4550 // result register contain a valid pointer because it is already
4551 // contained in the register pointer map.
4552 __ mov(result, zero_reg);
4554 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
4555 __ SmiTag(char_code);
4557 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr, instr->context());
4558 __ StoreToSafepointRegisterSlot(v0, result);
4562 void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
4563 LOperand* input = instr->value();
4564 ASSERT(input->IsRegister() || input->IsStackSlot());
4565 LOperand* output = instr->result();
4566 ASSERT(output->IsDoubleRegister());
4567 FPURegister single_scratch = double_scratch0().low();
4568 if (input->IsStackSlot()) {
4569 Register scratch = scratch0();
4570 __ lw(scratch, ToMemOperand(input));
4571 __ mtc1(scratch, single_scratch);
4573 __ mtc1(ToRegister(input), single_scratch);
4575 __ cvt_d_w(ToDoubleRegister(output), single_scratch);
4579 void LCodeGen::DoInteger32ToSmi(LInteger32ToSmi* instr) {
4580 LOperand* input = instr->value();
4581 LOperand* output = instr->result();
4582 Register scratch = scratch0();
4584 ASSERT(output->IsRegister());
4585 if (!instr->hydrogen()->value()->HasRange() ||
4586 !instr->hydrogen()->value()->range()->IsInSmiRange()) {
4587 __ SmiTagCheckOverflow(ToRegister(output), ToRegister(input), scratch);
4588 DeoptimizeIf(lt, instr->environment(), scratch, Operand(zero_reg));
4590 __ SmiTag(ToRegister(output), ToRegister(input));
4595 void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4596 LOperand* input = instr->value();
4597 LOperand* output = instr->result();
4599 FPURegister dbl_scratch = double_scratch0();
4600 __ mtc1(ToRegister(input), dbl_scratch);
4601 __ Cvt_d_uw(ToDoubleRegister(output), dbl_scratch, f22);
4605 void LCodeGen::DoUint32ToSmi(LUint32ToSmi* instr) {
4606 LOperand* input = instr->value();
4607 LOperand* output = instr->result();
4608 if (!instr->hydrogen()->value()->HasRange() ||
4609 !instr->hydrogen()->value()->range()->IsInSmiRange()) {
4610 Register scratch = scratch0();
4611 __ And(scratch, ToRegister(input), Operand(0xc0000000));
4612 DeoptimizeIf(ne, instr->environment(), scratch, Operand(zero_reg));
4614 __ SmiTag(ToRegister(output), ToRegister(input));
4618 void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
4619 class DeferredNumberTagI V8_FINAL : public LDeferredCode {
4621 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
4622 : LDeferredCode(codegen), instr_(instr) { }
4623 virtual void Generate() V8_OVERRIDE {
4624 codegen()->DoDeferredNumberTagI(instr_,
4628 virtual LInstruction* instr() V8_OVERRIDE { return instr_; }
4630 LNumberTagI* instr_;
4633 Register src = ToRegister(instr->value());
4634 Register dst = ToRegister(instr->result());
4635 Register overflow = scratch0();
4637 DeferredNumberTagI* deferred = new(zone()) DeferredNumberTagI(this, instr);
4638 __ SmiTagCheckOverflow(dst, src, overflow);
4639 __ BranchOnOverflow(deferred->entry(), overflow);
4640 __ bind(deferred->exit());
4644 void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4645 class DeferredNumberTagU V8_FINAL : public LDeferredCode {
4647 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4648 : LDeferredCode(codegen), instr_(instr) { }
4649 virtual void Generate() V8_OVERRIDE {
4650 codegen()->DoDeferredNumberTagI(instr_,
4654 virtual LInstruction* instr() V8_OVERRIDE { return instr_; }
4656 LNumberTagU* instr_;
4659 Register input = ToRegister(instr->value());
4660 Register result = ToRegister(instr->result());
4662 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4663 __ Branch(deferred->entry(), hi, input, Operand(Smi::kMaxValue));
4664 __ SmiTag(result, input);
4665 __ bind(deferred->exit());
4669 void LCodeGen::DoDeferredNumberTagI(LInstruction* instr,
4671 IntegerSignedness signedness) {
4673 Register src = ToRegister(value);
4674 Register dst = ToRegister(instr->result());
4675 DoubleRegister dbl_scratch = double_scratch0();
4677 // Preserve the value of all registers.
4678 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
4681 if (signedness == SIGNED_INT32) {
4682 // There was overflow, so bits 30 and 31 of the original integer
4683 // disagree. Try to allocate a heap number in new space and store
4684 // the value in there. If that fails, call the runtime system.
4686 __ SmiUntag(src, dst);
4687 __ Xor(src, src, Operand(0x80000000));
4689 __ mtc1(src, dbl_scratch);
4690 __ cvt_d_w(dbl_scratch, dbl_scratch);
4692 __ mtc1(src, dbl_scratch);
4693 __ Cvt_d_uw(dbl_scratch, dbl_scratch, f22);
4696 if (FLAG_inline_new) {
4697 __ LoadRoot(scratch0(), Heap::kHeapNumberMapRootIndex);
4698 __ AllocateHeapNumber(t1, a3, t0, scratch0(), &slow, DONT_TAG_RESULT);
4703 // Slow case: Call the runtime system to do the number allocation.
4706 // TODO(3095996): Put a valid pointer value in the stack slot where the result
4707 // register is stored, as this register is in the pointer map, but contains an
4709 __ StoreToSafepointRegisterSlot(zero_reg, dst);
4710 // NumberTagI and NumberTagD use the context from the frame, rather than
4711 // the environment's HContext or HInlinedContext value.
4712 // They only call Runtime::kAllocateHeapNumber.
4713 // The corresponding HChange instructions are added in a phase that does
4714 // not have easy access to the local context.
4715 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4716 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4717 RecordSafepointWithRegisters(
4718 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4720 __ Subu(dst, dst, kHeapObjectTag);
4722 // Done. Put the value in dbl_scratch into the value of the allocated heap
4725 __ sdc1(dbl_scratch, MemOperand(dst, HeapNumber::kValueOffset));
4726 __ Addu(dst, dst, kHeapObjectTag);
4727 __ StoreToSafepointRegisterSlot(dst, dst);
4731 void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
4732 class DeferredNumberTagD V8_FINAL : public LDeferredCode {
4734 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4735 : LDeferredCode(codegen), instr_(instr) { }
4736 virtual void Generate() V8_OVERRIDE {
4737 codegen()->DoDeferredNumberTagD(instr_);
4739 virtual LInstruction* instr() V8_OVERRIDE { return instr_; }
4741 LNumberTagD* instr_;
4744 DoubleRegister input_reg = ToDoubleRegister(instr->value());
4745 Register scratch = scratch0();
4746 Register reg = ToRegister(instr->result());
4747 Register temp1 = ToRegister(instr->temp());
4748 Register temp2 = ToRegister(instr->temp2());
4750 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
4751 if (FLAG_inline_new) {
4752 __ LoadRoot(scratch, Heap::kHeapNumberMapRootIndex);
4753 // We want the untagged address first for performance
4754 __ AllocateHeapNumber(reg, temp1, temp2, scratch, deferred->entry(),
4757 __ Branch(deferred->entry());
4759 __ bind(deferred->exit());
4760 __ sdc1(input_reg, MemOperand(reg, HeapNumber::kValueOffset));
4761 // Now that we have finished with the object's real address tag it
4762 __ Addu(reg, reg, kHeapObjectTag);
4766 void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4767 // TODO(3095996): Get rid of this. For now, we need to make the
4768 // result register contain a valid pointer because it is already
4769 // contained in the register pointer map.
4770 Register reg = ToRegister(instr->result());
4771 __ mov(reg, zero_reg);
4773 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
4774 // NumberTagI and NumberTagD use the context from the frame, rather than
4775 // the environment's HContext or HInlinedContext value.
4776 // They only call Runtime::kAllocateHeapNumber.
4777 // The corresponding HChange instructions are added in a phase that does
4778 // not have easy access to the local context.
4779 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4780 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4781 RecordSafepointWithRegisters(
4782 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4783 __ Subu(v0, v0, kHeapObjectTag);
4784 __ StoreToSafepointRegisterSlot(v0, reg);
4788 void LCodeGen::DoSmiTag(LSmiTag* instr) {
4789 ASSERT(!instr->hydrogen_value()->CheckFlag(HValue::kCanOverflow));
4790 __ SmiTag(ToRegister(instr->result()), ToRegister(instr->value()));
4794 void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4795 Register scratch = scratch0();
4796 Register input = ToRegister(instr->value());
4797 Register result = ToRegister(instr->result());
4798 if (instr->needs_check()) {
4799 STATIC_ASSERT(kHeapObjectTag == 1);
4800 // If the input is a HeapObject, value of scratch won't be zero.
4801 __ And(scratch, input, Operand(kHeapObjectTag));
4802 __ SmiUntag(result, input);
4803 DeoptimizeIf(ne, instr->environment(), scratch, Operand(zero_reg));
4805 __ SmiUntag(result, input);
4810 void LCodeGen::EmitNumberUntagD(Register input_reg,
4811 DoubleRegister result_reg,
4812 bool can_convert_undefined_to_nan,
4813 bool deoptimize_on_minus_zero,
4815 NumberUntagDMode mode) {
4816 Register scratch = scratch0();
4817 Label convert, load_smi, done;
4818 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4820 __ UntagAndJumpIfSmi(scratch, input_reg, &load_smi);
4821 // Heap number map check.
4822 __ lw(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
4823 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4824 if (can_convert_undefined_to_nan) {
4825 __ Branch(&convert, ne, scratch, Operand(at));
4827 DeoptimizeIf(ne, env, scratch, Operand(at));
4829 // Load heap number.
4830 __ ldc1(result_reg, FieldMemOperand(input_reg, HeapNumber::kValueOffset));
4831 if (deoptimize_on_minus_zero) {
4832 __ mfc1(at, result_reg.low());
4833 __ Branch(&done, ne, at, Operand(zero_reg));
4834 __ mfc1(scratch, result_reg.high());
4835 DeoptimizeIf(eq, env, scratch, Operand(HeapNumber::kSignMask));
4838 if (can_convert_undefined_to_nan) {
4840 // Convert undefined (and hole) to NaN.
4841 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4842 DeoptimizeIf(ne, env, input_reg, Operand(at));
4843 __ LoadRoot(scratch, Heap::kNanValueRootIndex);
4844 __ ldc1(result_reg, FieldMemOperand(scratch, HeapNumber::kValueOffset));
4848 __ SmiUntag(scratch, input_reg);
4849 ASSERT(mode == NUMBER_CANDIDATE_IS_SMI);
4851 // Smi to double register conversion
4853 // scratch: untagged value of input_reg
4854 __ mtc1(scratch, result_reg);
4855 __ cvt_d_w(result_reg, result_reg);
4860 void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) {
4861 Register input_reg = ToRegister(instr->value());
4862 Register scratch1 = scratch0();
4863 Register scratch2 = ToRegister(instr->temp());
4864 DoubleRegister double_scratch = double_scratch0();
4865 DoubleRegister double_scratch2 = ToDoubleRegister(instr->temp2());
4867 ASSERT(!scratch1.is(input_reg) && !scratch1.is(scratch2));
4868 ASSERT(!scratch2.is(input_reg) && !scratch2.is(scratch1));
4872 // The input is a tagged HeapObject.
4873 // Heap number map check.
4874 __ lw(scratch1, FieldMemOperand(input_reg, HeapObject::kMapOffset));
4875 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4876 // This 'at' value and scratch1 map value are used for tests in both clauses
4879 if (instr->truncating()) {
4880 // Performs a truncating conversion of a floating point number as used by
4881 // the JS bitwise operations.
4882 Label no_heap_number, check_bools, check_false;
4883 __ Branch(&no_heap_number, ne, scratch1, Operand(at)); // HeapNumber map?
4884 __ mov(scratch2, input_reg);
4885 __ TruncateHeapNumberToI(input_reg, scratch2);
4888 // Check for Oddballs. Undefined/False is converted to zero and True to one
4889 // for truncating conversions.
4890 __ bind(&no_heap_number);
4891 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4892 __ Branch(&check_bools, ne, input_reg, Operand(at));
4893 ASSERT(ToRegister(instr->result()).is(input_reg));
4894 __ Branch(USE_DELAY_SLOT, &done);
4895 __ mov(input_reg, zero_reg); // In delay slot.
4897 __ bind(&check_bools);
4898 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4899 __ Branch(&check_false, ne, scratch2, Operand(at));
4900 __ Branch(USE_DELAY_SLOT, &done);
4901 __ li(input_reg, Operand(1)); // In delay slot.
4903 __ bind(&check_false);
4904 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4905 DeoptimizeIf(ne, instr->environment(), scratch2, Operand(at));
4906 __ Branch(USE_DELAY_SLOT, &done);
4907 __ mov(input_reg, zero_reg); // In delay slot.
4909 // Deoptimize if we don't have a heap number.
4910 DeoptimizeIf(ne, instr->environment(), scratch1, Operand(at));
4912 // Load the double value.
4913 __ ldc1(double_scratch,
4914 FieldMemOperand(input_reg, HeapNumber::kValueOffset));
4916 Register except_flag = scratch2;
4917 __ EmitFPUTruncate(kRoundToZero,
4923 kCheckForInexactConversion);
4925 // Deopt if the operation did not succeed.
4926 DeoptimizeIf(ne, instr->environment(), except_flag, Operand(zero_reg));
4928 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
4929 __ Branch(&done, ne, input_reg, Operand(zero_reg));
4931 __ mfc1(scratch1, double_scratch.high());
4932 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
4933 DeoptimizeIf(ne, instr->environment(), scratch1, Operand(zero_reg));
4940 void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
4941 class DeferredTaggedToI V8_FINAL : public LDeferredCode {
4943 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
4944 : LDeferredCode(codegen), instr_(instr) { }
4945 virtual void Generate() V8_OVERRIDE {
4946 codegen()->DoDeferredTaggedToI(instr_);
4948 virtual LInstruction* instr() V8_OVERRIDE { return instr_; }
4953 LOperand* input = instr->value();
4954 ASSERT(input->IsRegister());
4955 ASSERT(input->Equals(instr->result()));
4957 Register input_reg = ToRegister(input);
4959 if (instr->hydrogen()->value()->representation().IsSmi()) {
4960 __ SmiUntag(input_reg);
4962 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
4964 // Let the deferred code handle the HeapObject case.
4965 __ JumpIfNotSmi(input_reg, deferred->entry());
4967 // Smi to int32 conversion.
4968 __ SmiUntag(input_reg);
4969 __ bind(deferred->exit());
4974 void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
4975 LOperand* input = instr->value();
4976 ASSERT(input->IsRegister());
4977 LOperand* result = instr->result();
4978 ASSERT(result->IsDoubleRegister());
4980 Register input_reg = ToRegister(input);
4981 DoubleRegister result_reg = ToDoubleRegister(result);
4983 HValue* value = instr->hydrogen()->value();
4984 NumberUntagDMode mode = value->representation().IsSmi()
4985 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
4987 EmitNumberUntagD(input_reg, result_reg,
4988 instr->hydrogen()->can_convert_undefined_to_nan(),
4989 instr->hydrogen()->deoptimize_on_minus_zero(),
4990 instr->environment(),
4995 void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
4996 Register result_reg = ToRegister(instr->result());
4997 Register scratch1 = scratch0();
4998 DoubleRegister double_input = ToDoubleRegister(instr->value());
5000 if (instr->truncating()) {
5001 __ TruncateDoubleToI(result_reg, double_input);
5003 Register except_flag = LCodeGen::scratch1();
5005 __ EmitFPUTruncate(kRoundToMinusInf,
5011 kCheckForInexactConversion);
5013 // Deopt if the operation did not succeed (except_flag != 0).
5014 DeoptimizeIf(ne, instr->environment(), except_flag, Operand(zero_reg));
5016 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5018 __ Branch(&done, ne, result_reg, Operand(zero_reg));
5019 __ mfc1(scratch1, double_input.high());
5020 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
5021 DeoptimizeIf(ne, instr->environment(), scratch1, Operand(zero_reg));
5028 void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
5029 Register result_reg = ToRegister(instr->result());
5030 Register scratch1 = LCodeGen::scratch0();
5031 DoubleRegister double_input = ToDoubleRegister(instr->value());
5033 if (instr->truncating()) {
5034 __ TruncateDoubleToI(result_reg, double_input);
5036 Register except_flag = LCodeGen::scratch1();
5038 __ EmitFPUTruncate(kRoundToMinusInf,
5044 kCheckForInexactConversion);
5046 // Deopt if the operation did not succeed (except_flag != 0).
5047 DeoptimizeIf(ne, instr->environment(), except_flag, Operand(zero_reg));
5049 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5051 __ Branch(&done, ne, result_reg, Operand(zero_reg));
5052 __ mfc1(scratch1, double_input.high());
5053 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
5054 DeoptimizeIf(ne, instr->environment(), scratch1, Operand(zero_reg));
5058 __ SmiTagCheckOverflow(result_reg, result_reg, scratch1);
5059 DeoptimizeIf(lt, instr->environment(), scratch1, Operand(zero_reg));
5063 void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
5064 LOperand* input = instr->value();
5065 __ SmiTst(ToRegister(input), at);
5066 DeoptimizeIf(ne, instr->environment(), at, Operand(zero_reg));
5070 void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
5071 if (!instr->hydrogen()->value()->IsHeapObject()) {
5072 LOperand* input = instr->value();
5073 __ SmiTst(ToRegister(input), at);
5074 DeoptimizeIf(eq, instr->environment(), at, Operand(zero_reg));
5079 void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
5080 Register input = ToRegister(instr->value());
5081 Register scratch = scratch0();
5083 __ GetObjectType(input, scratch, scratch);
5085 if (instr->hydrogen()->is_interval_check()) {
5088 instr->hydrogen()->GetCheckInterval(&first, &last);
5090 // If there is only one type in the interval check for equality.
5091 if (first == last) {
5092 DeoptimizeIf(ne, instr->environment(), scratch, Operand(first));
5094 DeoptimizeIf(lo, instr->environment(), scratch, Operand(first));
5095 // Omit check for the last type.
5096 if (last != LAST_TYPE) {
5097 DeoptimizeIf(hi, instr->environment(), scratch, Operand(last));
5103 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
5105 if (IsPowerOf2(mask)) {
5106 ASSERT(tag == 0 || IsPowerOf2(tag));
5107 __ And(at, scratch, mask);
5108 DeoptimizeIf(tag == 0 ? ne : eq, instr->environment(),
5109 at, Operand(zero_reg));
5111 __ And(scratch, scratch, Operand(mask));
5112 DeoptimizeIf(ne, instr->environment(), scratch, Operand(tag));
5118 void LCodeGen::DoCheckValue(LCheckValue* instr) {
5119 Register reg = ToRegister(instr->value());
5120 Handle<HeapObject> object = instr->hydrogen()->object().handle();
5121 AllowDeferredHandleDereference smi_check;
5122 if (isolate()->heap()->InNewSpace(*object)) {
5123 Register reg = ToRegister(instr->value());
5124 Handle<Cell> cell = isolate()->factory()->NewCell(object);
5125 __ li(at, Operand(Handle<Object>(cell)));
5126 __ lw(at, FieldMemOperand(at, Cell::kValueOffset));
5127 DeoptimizeIf(ne, instr->environment(), reg,
5130 DeoptimizeIf(ne, instr->environment(), reg,
5136 void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
5138 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
5140 __ mov(cp, zero_reg);
5141 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
5142 RecordSafepointWithRegisters(
5143 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
5144 __ StoreToSafepointRegisterSlot(v0, scratch0());
5146 __ SmiTst(scratch0(), at);
5147 DeoptimizeIf(eq, instr->environment(), at, Operand(zero_reg));
5151 void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
5152 class DeferredCheckMaps V8_FINAL : public LDeferredCode {
5154 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
5155 : LDeferredCode(codegen), instr_(instr), object_(object) {
5156 SetExit(check_maps());
5158 virtual void Generate() V8_OVERRIDE {
5159 codegen()->DoDeferredInstanceMigration(instr_, object_);
5161 Label* check_maps() { return &check_maps_; }
5162 virtual LInstruction* instr() V8_OVERRIDE { return instr_; }
5169 if (instr->hydrogen()->CanOmitMapChecks()) return;
5170 Register map_reg = scratch0();
5171 LOperand* input = instr->value();
5172 ASSERT(input->IsRegister());
5173 Register reg = ToRegister(input);
5174 __ lw(map_reg, FieldMemOperand(reg, HeapObject::kMapOffset));
5176 DeferredCheckMaps* deferred = NULL;
5177 if (instr->hydrogen()->has_migration_target()) {
5178 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5179 __ bind(deferred->check_maps());
5182 UniqueSet<Map> map_set = instr->hydrogen()->map_set();
5184 for (int i = 0; i < map_set.size() - 1; i++) {
5185 Handle<Map> map = map_set.at(i).handle();
5186 __ CompareMapAndBranch(map_reg, map, &success, eq, &success);
5188 Handle<Map> map = map_set.at(map_set.size() - 1).handle();
5189 // Do the CompareMap() directly within the Branch() and DeoptimizeIf().
5190 if (instr->hydrogen()->has_migration_target()) {
5191 __ Branch(deferred->entry(), ne, map_reg, Operand(map));
5193 DeoptimizeIf(ne, instr->environment(), map_reg, Operand(map));
5200 void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
5201 DoubleRegister value_reg = ToDoubleRegister(instr->unclamped());
5202 Register result_reg = ToRegister(instr->result());
5203 DoubleRegister temp_reg = ToDoubleRegister(instr->temp());
5204 __ ClampDoubleToUint8(result_reg, value_reg, temp_reg);
5208 void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
5209 Register unclamped_reg = ToRegister(instr->unclamped());
5210 Register result_reg = ToRegister(instr->result());
5211 __ ClampUint8(result_reg, unclamped_reg);
5215 void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
5216 Register scratch = scratch0();
5217 Register input_reg = ToRegister(instr->unclamped());
5218 Register result_reg = ToRegister(instr->result());
5219 DoubleRegister temp_reg = ToDoubleRegister(instr->temp());
5220 Label is_smi, done, heap_number;
5222 // Both smi and heap number cases are handled.
5223 __ UntagAndJumpIfSmi(scratch, input_reg, &is_smi);
5225 // Check for heap number
5226 __ lw(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
5227 __ Branch(&heap_number, eq, scratch, Operand(factory()->heap_number_map()));
5229 // Check for undefined. Undefined is converted to zero for clamping
5231 DeoptimizeIf(ne, instr->environment(), input_reg,
5232 Operand(factory()->undefined_value()));
5233 __ mov(result_reg, zero_reg);
5237 __ bind(&heap_number);
5238 __ ldc1(double_scratch0(), FieldMemOperand(input_reg,
5239 HeapNumber::kValueOffset));
5240 __ ClampDoubleToUint8(result_reg, double_scratch0(), temp_reg);
5244 __ ClampUint8(result_reg, scratch);
5250 void LCodeGen::DoAllocate(LAllocate* instr) {
5251 class DeferredAllocate V8_FINAL : public LDeferredCode {
5253 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
5254 : LDeferredCode(codegen), instr_(instr) { }
5255 virtual void Generate() V8_OVERRIDE {
5256 codegen()->DoDeferredAllocate(instr_);
5258 virtual LInstruction* instr() V8_OVERRIDE { return instr_; }
5263 DeferredAllocate* deferred =
5264 new(zone()) DeferredAllocate(this, instr);
5266 Register result = ToRegister(instr->result());
5267 Register scratch = ToRegister(instr->temp1());
5268 Register scratch2 = ToRegister(instr->temp2());
5270 // Allocate memory for the object.
5271 AllocationFlags flags = TAG_OBJECT;
5272 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5273 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5275 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5276 ASSERT(!instr->hydrogen()->IsOldDataSpaceAllocation());
5277 ASSERT(!instr->hydrogen()->IsNewSpaceAllocation());
5278 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_POINTER_SPACE);
5279 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5280 ASSERT(!instr->hydrogen()->IsNewSpaceAllocation());
5281 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_DATA_SPACE);
5283 if (instr->size()->IsConstantOperand()) {
5284 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5285 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5287 Register size = ToRegister(instr->size());
5296 __ bind(deferred->exit());
5298 if (instr->hydrogen()->MustPrefillWithFiller()) {
5299 if (instr->size()->IsConstantOperand()) {
5300 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5301 __ li(scratch, Operand(size));
5303 scratch = ToRegister(instr->size());
5305 __ Subu(scratch, scratch, Operand(kPointerSize));
5306 __ Subu(result, result, Operand(kHeapObjectTag));
5309 __ li(scratch2, Operand(isolate()->factory()->one_pointer_filler_map()));
5310 __ Addu(at, result, Operand(scratch));
5311 __ sw(scratch2, MemOperand(at));
5312 __ Subu(scratch, scratch, Operand(kPointerSize));
5313 __ Branch(&loop, ge, scratch, Operand(zero_reg));
5314 __ Addu(result, result, Operand(kHeapObjectTag));
5319 void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
5320 Register result = ToRegister(instr->result());
5322 // TODO(3095996): Get rid of this. For now, we need to make the
5323 // result register contain a valid pointer because it is already
5324 // contained in the register pointer map.
5325 __ mov(result, zero_reg);
5327 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
5328 if (instr->size()->IsRegister()) {
5329 Register size = ToRegister(instr->size());
5330 ASSERT(!size.is(result));
5334 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5335 __ Push(Smi::FromInt(size));
5338 int flags = AllocateDoubleAlignFlag::encode(
5339 instr->hydrogen()->MustAllocateDoubleAligned());
5340 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5341 ASSERT(!instr->hydrogen()->IsOldDataSpaceAllocation());
5342 ASSERT(!instr->hydrogen()->IsNewSpaceAllocation());
5343 flags = AllocateTargetSpace::update(flags, OLD_POINTER_SPACE);
5344 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5345 ASSERT(!instr->hydrogen()->IsNewSpaceAllocation());
5346 flags = AllocateTargetSpace::update(flags, OLD_DATA_SPACE);
5348 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5350 __ Push(Smi::FromInt(flags));
5352 CallRuntimeFromDeferred(
5353 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
5354 __ StoreToSafepointRegisterSlot(v0, result);
5358 void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
5359 ASSERT(ToRegister(instr->value()).is(a0));
5360 ASSERT(ToRegister(instr->result()).is(v0));
5362 CallRuntime(Runtime::kToFastProperties, 1, instr);
5366 void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
5367 ASSERT(ToRegister(instr->context()).is(cp));
5369 // Registers will be used as follows:
5370 // t3 = literals array.
5371 // a1 = regexp literal.
5372 // a0 = regexp literal clone.
5373 // a2 and t0-t2 are used as temporaries.
5374 int literal_offset =
5375 FixedArray::OffsetOfElementAt(instr->hydrogen()->literal_index());
5376 __ li(t3, instr->hydrogen()->literals());
5377 __ lw(a1, FieldMemOperand(t3, literal_offset));
5378 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5379 __ Branch(&materialized, ne, a1, Operand(at));
5381 // Create regexp literal using runtime function
5382 // Result will be in v0.
5383 __ li(t2, Operand(Smi::FromInt(instr->hydrogen()->literal_index())));
5384 __ li(t1, Operand(instr->hydrogen()->pattern()));
5385 __ li(t0, Operand(instr->hydrogen()->flags()));
5386 __ Push(t3, t2, t1, t0);
5387 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
5390 __ bind(&materialized);
5391 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
5392 Label allocated, runtime_allocate;
5394 __ Allocate(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
5397 __ bind(&runtime_allocate);
5398 __ li(a0, Operand(Smi::FromInt(size)));
5400 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
5403 __ bind(&allocated);
5404 // Copy the content into the newly allocated memory.
5405 // (Unroll copy loop once for better throughput).
5406 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
5407 __ lw(a3, FieldMemOperand(a1, i));
5408 __ lw(a2, FieldMemOperand(a1, i + kPointerSize));
5409 __ sw(a3, FieldMemOperand(v0, i));
5410 __ sw(a2, FieldMemOperand(v0, i + kPointerSize));
5412 if ((size % (2 * kPointerSize)) != 0) {
5413 __ lw(a3, FieldMemOperand(a1, size - kPointerSize));
5414 __ sw(a3, FieldMemOperand(v0, size - kPointerSize));
5419 void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
5420 ASSERT(ToRegister(instr->context()).is(cp));
5421 // Use the fast case closure allocation code that allocates in new
5422 // space for nested functions that don't need literals cloning.
5423 bool pretenure = instr->hydrogen()->pretenure();
5424 if (!pretenure && instr->hydrogen()->has_no_literals()) {
5425 FastNewClosureStub stub(instr->hydrogen()->language_mode(),
5426 instr->hydrogen()->is_generator());
5427 __ li(a2, Operand(instr->hydrogen()->shared_info()));
5428 CallCode(stub.GetCode(isolate()), RelocInfo::CODE_TARGET, instr);
5430 __ li(a2, Operand(instr->hydrogen()->shared_info()));
5431 __ li(a1, Operand(pretenure ? factory()->true_value()
5432 : factory()->false_value()));
5433 __ Push(cp, a2, a1);
5434 CallRuntime(Runtime::kNewClosure, 3, instr);
5439 void LCodeGen::DoTypeof(LTypeof* instr) {
5440 ASSERT(ToRegister(instr->result()).is(v0));
5441 Register input = ToRegister(instr->value());
5443 CallRuntime(Runtime::kTypeof, 1, instr);
5447 void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5448 Register input = ToRegister(instr->value());
5450 Register cmp1 = no_reg;
5451 Operand cmp2 = Operand(no_reg);
5453 Condition final_branch_condition = EmitTypeofIs(instr->TrueLabel(chunk_),
5454 instr->FalseLabel(chunk_),
5456 instr->type_literal(),
5460 ASSERT(cmp1.is_valid());
5461 ASSERT(!cmp2.is_reg() || cmp2.rm().is_valid());
5463 if (final_branch_condition != kNoCondition) {
5464 EmitBranch(instr, final_branch_condition, cmp1, cmp2);
5469 Condition LCodeGen::EmitTypeofIs(Label* true_label,
5472 Handle<String> type_name,
5475 // This function utilizes the delay slot heavily. This is used to load
5476 // values that are always usable without depending on the type of the input
5478 Condition final_branch_condition = kNoCondition;
5479 Register scratch = scratch0();
5480 if (type_name->Equals(heap()->number_string())) {
5481 __ JumpIfSmi(input, true_label);
5482 __ lw(input, FieldMemOperand(input, HeapObject::kMapOffset));
5483 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5486 final_branch_condition = eq;
5488 } else if (type_name->Equals(heap()->string_string())) {
5489 __ JumpIfSmi(input, false_label);
5490 __ GetObjectType(input, input, scratch);
5491 __ Branch(USE_DELAY_SLOT, false_label,
5492 ge, scratch, Operand(FIRST_NONSTRING_TYPE));
5493 // input is an object so we can load the BitFieldOffset even if we take the
5495 __ lbu(at, FieldMemOperand(input, Map::kBitFieldOffset));
5496 __ And(at, at, 1 << Map::kIsUndetectable);
5498 cmp2 = Operand(zero_reg);
5499 final_branch_condition = eq;
5501 } else if (type_name->Equals(heap()->symbol_string())) {
5502 __ JumpIfSmi(input, false_label);
5503 __ GetObjectType(input, input, scratch);
5505 cmp2 = Operand(SYMBOL_TYPE);
5506 final_branch_condition = eq;
5508 } else if (type_name->Equals(heap()->boolean_string())) {
5509 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5510 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5511 __ LoadRoot(at, Heap::kFalseValueRootIndex);
5513 cmp2 = Operand(input);
5514 final_branch_condition = eq;
5516 } else if (FLAG_harmony_typeof && type_name->Equals(heap()->null_string())) {
5517 __ LoadRoot(at, Heap::kNullValueRootIndex);
5519 cmp2 = Operand(input);
5520 final_branch_condition = eq;
5522 } else if (type_name->Equals(heap()->undefined_string())) {
5523 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5524 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5525 // The first instruction of JumpIfSmi is an And - it is safe in the delay
5527 __ JumpIfSmi(input, false_label);
5528 // Check for undetectable objects => true.
5529 __ lw(input, FieldMemOperand(input, HeapObject::kMapOffset));
5530 __ lbu(at, FieldMemOperand(input, Map::kBitFieldOffset));
5531 __ And(at, at, 1 << Map::kIsUndetectable);
5533 cmp2 = Operand(zero_reg);
5534 final_branch_condition = ne;
5536 } else if (type_name->Equals(heap()->function_string())) {
5537 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5538 __ JumpIfSmi(input, false_label);
5539 __ GetObjectType(input, scratch, input);
5540 __ Branch(true_label, eq, input, Operand(JS_FUNCTION_TYPE));
5542 cmp2 = Operand(JS_FUNCTION_PROXY_TYPE);
5543 final_branch_condition = eq;
5545 } else if (type_name->Equals(heap()->object_string())) {
5546 __ JumpIfSmi(input, false_label);
5547 if (!FLAG_harmony_typeof) {
5548 __ LoadRoot(at, Heap::kNullValueRootIndex);
5549 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5551 Register map = input;
5552 __ GetObjectType(input, map, scratch);
5553 __ Branch(false_label,
5554 lt, scratch, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
5555 __ Branch(USE_DELAY_SLOT, false_label,
5556 gt, scratch, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
5557 // map is still valid, so the BitField can be loaded in delay slot.
5558 // Check for undetectable objects => false.
5559 __ lbu(at, FieldMemOperand(map, Map::kBitFieldOffset));
5560 __ And(at, at, 1 << Map::kIsUndetectable);
5562 cmp2 = Operand(zero_reg);
5563 final_branch_condition = eq;
5567 cmp2 = Operand(zero_reg); // Set to valid regs, to avoid caller assertion.
5568 __ Branch(false_label);
5571 return final_branch_condition;
5575 void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
5576 Register temp1 = ToRegister(instr->temp());
5578 EmitIsConstructCall(temp1, scratch0());
5580 EmitBranch(instr, eq, temp1,
5581 Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
5585 void LCodeGen::EmitIsConstructCall(Register temp1, Register temp2) {
5586 ASSERT(!temp1.is(temp2));
5587 // Get the frame pointer for the calling frame.
5588 __ lw(temp1, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5590 // Skip the arguments adaptor frame if it exists.
5591 Label check_frame_marker;
5592 __ lw(temp2, MemOperand(temp1, StandardFrameConstants::kContextOffset));
5593 __ Branch(&check_frame_marker, ne, temp2,
5594 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5595 __ lw(temp1, MemOperand(temp1, StandardFrameConstants::kCallerFPOffset));
5597 // Check the marker in the calling frame.
5598 __ bind(&check_frame_marker);
5599 __ lw(temp1, MemOperand(temp1, StandardFrameConstants::kMarkerOffset));
5603 void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5604 if (!info()->IsStub()) {
5605 // Ensure that we have enough space after the previous lazy-bailout
5606 // instruction for patching the code here.
5607 int current_pc = masm()->pc_offset();
5608 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5609 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5610 ASSERT_EQ(0, padding_size % Assembler::kInstrSize);
5611 while (padding_size > 0) {
5613 padding_size -= Assembler::kInstrSize;
5617 last_lazy_deopt_pc_ = masm()->pc_offset();
5621 void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
5622 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
5623 ASSERT(instr->HasEnvironment());
5624 LEnvironment* env = instr->environment();
5625 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5626 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5630 void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
5631 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5632 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5633 // needed return address), even though the implementation of LAZY and EAGER is
5634 // now identical. When LAZY is eventually completely folded into EAGER, remove
5635 // the special case below.
5636 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5637 type = Deoptimizer::LAZY;
5640 Comment(";;; deoptimize: %s", instr->hydrogen()->reason());
5641 DeoptimizeIf(al, instr->environment(), type, zero_reg, Operand(zero_reg));
5645 void LCodeGen::DoDummy(LDummy* instr) {
5646 // Nothing to see here, move on!
5650 void LCodeGen::DoDummyUse(LDummyUse* instr) {
5651 // Nothing to see here, move on!
5655 void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
5656 PushSafepointRegistersScope scope(this, Safepoint::kWithRegisters);
5657 LoadContextFromDeferred(instr->context());
5658 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5659 RecordSafepointWithLazyDeopt(
5660 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
5661 ASSERT(instr->HasEnvironment());
5662 LEnvironment* env = instr->environment();
5663 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5667 void LCodeGen::DoStackCheck(LStackCheck* instr) {
5668 class DeferredStackCheck V8_FINAL : public LDeferredCode {
5670 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5671 : LDeferredCode(codegen), instr_(instr) { }
5672 virtual void Generate() V8_OVERRIDE {
5673 codegen()->DoDeferredStackCheck(instr_);
5675 virtual LInstruction* instr() V8_OVERRIDE { return instr_; }
5677 LStackCheck* instr_;
5680 ASSERT(instr->HasEnvironment());
5681 LEnvironment* env = instr->environment();
5682 // There is no LLazyBailout instruction for stack-checks. We have to
5683 // prepare for lazy deoptimization explicitly here.
5684 if (instr->hydrogen()->is_function_entry()) {
5685 // Perform stack overflow check.
5687 __ LoadRoot(at, Heap::kStackLimitRootIndex);
5688 __ Branch(&done, hs, sp, Operand(at));
5689 ASSERT(instr->context()->IsRegister());
5690 ASSERT(ToRegister(instr->context()).is(cp));
5691 CallCode(isolate()->builtins()->StackCheck(),
5692 RelocInfo::CODE_TARGET,
5694 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
5696 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5697 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5699 ASSERT(instr->hydrogen()->is_backwards_branch());
5700 // Perform stack overflow check if this goto needs it before jumping.
5701 DeferredStackCheck* deferred_stack_check =
5702 new(zone()) DeferredStackCheck(this, instr);
5703 __ LoadRoot(at, Heap::kStackLimitRootIndex);
5704 __ Branch(deferred_stack_check->entry(), lo, sp, Operand(at));
5705 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
5706 __ bind(instr->done_label());
5707 deferred_stack_check->SetExit(instr->done_label());
5708 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5709 // Don't record a deoptimization index for the safepoint here.
5710 // This will be done explicitly when emitting call and the safepoint in
5711 // the deferred code.
5716 void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
5717 // This is a pseudo-instruction that ensures that the environment here is
5718 // properly registered for deoptimization and records the assembler's PC
5720 LEnvironment* environment = instr->environment();
5722 // If the environment were already registered, we would have no way of
5723 // backpatching it with the spill slot operands.
5724 ASSERT(!environment->HasBeenRegistered());
5725 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
5727 GenerateOsrPrologue();
5731 void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
5732 Register result = ToRegister(instr->result());
5733 Register object = ToRegister(instr->object());
5734 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5735 DeoptimizeIf(eq, instr->environment(), object, Operand(at));
5737 Register null_value = t1;
5738 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
5739 DeoptimizeIf(eq, instr->environment(), object, Operand(null_value));
5741 __ And(at, object, kSmiTagMask);
5742 DeoptimizeIf(eq, instr->environment(), at, Operand(zero_reg));
5744 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
5745 __ GetObjectType(object, a1, a1);
5746 DeoptimizeIf(le, instr->environment(), a1, Operand(LAST_JS_PROXY_TYPE));
5748 Label use_cache, call_runtime;
5749 ASSERT(object.is(a0));
5750 __ CheckEnumCache(null_value, &call_runtime);
5752 __ lw(result, FieldMemOperand(object, HeapObject::kMapOffset));
5753 __ Branch(&use_cache);
5755 // Get the set of properties to enumerate.
5756 __ bind(&call_runtime);
5758 CallRuntime(Runtime::kGetPropertyNamesFast, 1, instr);
5760 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
5761 ASSERT(result.is(v0));
5762 __ LoadRoot(at, Heap::kMetaMapRootIndex);
5763 DeoptimizeIf(ne, instr->environment(), a1, Operand(at));
5764 __ bind(&use_cache);
5768 void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5769 Register map = ToRegister(instr->map());
5770 Register result = ToRegister(instr->result());
5771 Label load_cache, done;
5772 __ EnumLength(result, map);
5773 __ Branch(&load_cache, ne, result, Operand(Smi::FromInt(0)));
5774 __ li(result, Operand(isolate()->factory()->empty_fixed_array()));
5777 __ bind(&load_cache);
5778 __ LoadInstanceDescriptors(map, result);
5780 FieldMemOperand(result, DescriptorArray::kEnumCacheOffset));
5782 FieldMemOperand(result, FixedArray::SizeFor(instr->idx())));
5783 DeoptimizeIf(eq, instr->environment(), result, Operand(zero_reg));
5789 void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5790 Register object = ToRegister(instr->value());
5791 Register map = ToRegister(instr->map());
5792 __ lw(scratch0(), FieldMemOperand(object, HeapObject::kMapOffset));
5793 DeoptimizeIf(ne, instr->environment(), map, Operand(scratch0()));
5797 void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
5798 Register object = ToRegister(instr->object());
5799 Register index = ToRegister(instr->index());
5800 Register result = ToRegister(instr->result());
5801 Register scratch = scratch0();
5803 Label out_of_object, done;
5804 __ Branch(USE_DELAY_SLOT, &out_of_object, lt, index, Operand(zero_reg));
5805 __ sll(scratch, index, kPointerSizeLog2 - kSmiTagSize); // In delay slot.
5807 STATIC_ASSERT(kPointerSizeLog2 > kSmiTagSize);
5808 __ Addu(scratch, object, scratch);
5809 __ lw(result, FieldMemOperand(scratch, JSObject::kHeaderSize));
5813 __ bind(&out_of_object);
5814 __ lw(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
5815 // Index is equal to negated out of object property index plus 1.
5816 __ Subu(scratch, result, scratch);
5817 __ lw(result, FieldMemOperand(scratch,
5818 FixedArray::kHeaderSize - kPointerSize));
5825 } } // namespace v8::internal