1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "src/arm/lithium-codegen-arm.h"
8 #include "src/arm/lithium-gap-resolver-arm.h"
9 #include "src/base/bits.h"
10 #include "src/code-factory.h"
11 #include "src/code-stubs.h"
12 #include "src/hydrogen-osr.h"
13 #include "src/ic/ic.h"
14 #include "src/ic/stub-cache.h"
20 class SafepointGenerator FINAL : public CallWrapper {
22 SafepointGenerator(LCodeGen* codegen,
23 LPointerMap* pointers,
24 Safepoint::DeoptMode mode)
28 virtual ~SafepointGenerator() {}
30 void BeforeCall(int call_size) const OVERRIDE {}
32 void AfterCall() const OVERRIDE {
33 codegen_->RecordSafepoint(pointers_, deopt_mode_);
38 LPointerMap* pointers_;
39 Safepoint::DeoptMode deopt_mode_;
45 bool LCodeGen::GenerateCode() {
46 LPhase phase("Z_Code generation", chunk());
50 // Open a frame scope to indicate that there is a frame on the stack. The
51 // NONE indicates that the scope shouldn't actually generate code to set up
52 // the frame (that is done in GeneratePrologue).
53 FrameScope frame_scope(masm_, StackFrame::NONE);
55 return GeneratePrologue() && GenerateBody() && GenerateDeferredCode() &&
56 GenerateJumpTable() && GenerateSafepointTable();
60 void LCodeGen::FinishCode(Handle<Code> code) {
62 code->set_stack_slots(GetStackSlotCount());
63 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
64 PopulateDeoptimizationData(code);
68 void LCodeGen::SaveCallerDoubles() {
69 DCHECK(info()->saves_caller_doubles());
70 DCHECK(NeedsEagerFrame());
71 Comment(";;; Save clobbered callee double registers");
73 BitVector* doubles = chunk()->allocated_double_registers();
74 BitVector::Iterator save_iterator(doubles);
75 while (!save_iterator.Done()) {
76 __ vstr(DwVfpRegister::FromAllocationIndex(save_iterator.Current()),
77 MemOperand(sp, count * kDoubleSize));
78 save_iterator.Advance();
84 void LCodeGen::RestoreCallerDoubles() {
85 DCHECK(info()->saves_caller_doubles());
86 DCHECK(NeedsEagerFrame());
87 Comment(";;; Restore clobbered callee double registers");
88 BitVector* doubles = chunk()->allocated_double_registers();
89 BitVector::Iterator save_iterator(doubles);
91 while (!save_iterator.Done()) {
92 __ vldr(DwVfpRegister::FromAllocationIndex(save_iterator.Current()),
93 MemOperand(sp, count * kDoubleSize));
94 save_iterator.Advance();
100 bool LCodeGen::GeneratePrologue() {
101 DCHECK(is_generating());
103 if (info()->IsOptimizing()) {
104 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
107 if (strlen(FLAG_stop_at) > 0 &&
108 info_->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
113 // r1: Callee's JS function.
114 // cp: Callee's context.
115 // pp: Callee's constant pool pointer (if FLAG_enable_ool_constant_pool)
116 // fp: Caller's frame pointer.
119 // Sloppy mode functions and builtins need to replace the receiver with the
120 // global proxy when called as functions (without an explicit receiver
122 if (info_->this_has_uses() && is_sloppy(info_->language_mode()) &&
123 !info_->is_native()) {
125 int receiver_offset = info_->scope()->num_parameters() * kPointerSize;
126 __ ldr(r2, MemOperand(sp, receiver_offset));
127 __ CompareRoot(r2, Heap::kUndefinedValueRootIndex);
130 __ ldr(r2, GlobalObjectOperand());
131 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalProxyOffset));
133 __ str(r2, MemOperand(sp, receiver_offset));
139 info()->set_prologue_offset(masm_->pc_offset());
140 if (NeedsEagerFrame()) {
141 if (info()->IsStub()) {
144 __ Prologue(info()->IsCodePreAgingActive());
146 frame_is_built_ = true;
147 info_->AddNoFrameRange(0, masm_->pc_offset());
150 // Reserve space for the stack slots needed by the code.
151 int slots = GetStackSlotCount();
153 if (FLAG_debug_code) {
154 __ sub(sp, sp, Operand(slots * kPointerSize));
157 __ add(r0, sp, Operand(slots * kPointerSize));
158 __ mov(r1, Operand(kSlotsZapValue));
161 __ sub(r0, r0, Operand(kPointerSize));
162 __ str(r1, MemOperand(r0, 2 * kPointerSize));
168 __ sub(sp, sp, Operand(slots * kPointerSize));
172 if (info()->saves_caller_doubles()) {
176 // Possibly allocate a local context.
177 int heap_slots = info()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
178 if (heap_slots > 0) {
179 Comment(";;; Allocate local context");
180 bool need_write_barrier = true;
181 // Argument to NewContext is the function, which is in r1.
182 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
183 FastNewContextStub stub(isolate(), heap_slots);
185 // Result of FastNewContextStub is always in new space.
186 need_write_barrier = false;
189 __ CallRuntime(Runtime::kNewFunctionContext, 1);
191 RecordSafepoint(Safepoint::kNoLazyDeopt);
192 // Context is returned in both r0 and cp. It replaces the context
193 // passed to us. It's saved in the stack and kept live in cp.
195 __ str(r0, MemOperand(fp, StandardFrameConstants::kContextOffset));
196 // Copy any necessary parameters into the context.
197 int num_parameters = scope()->num_parameters();
198 for (int i = 0; i < num_parameters; i++) {
199 Variable* var = scope()->parameter(i);
200 if (var->IsContextSlot()) {
201 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
202 (num_parameters - 1 - i) * kPointerSize;
203 // Load parameter from stack.
204 __ ldr(r0, MemOperand(fp, parameter_offset));
205 // Store it in the context.
206 MemOperand target = ContextOperand(cp, var->index());
208 // Update the write barrier. This clobbers r3 and r0.
209 if (need_write_barrier) {
210 __ RecordWriteContextSlot(
215 GetLinkRegisterState(),
217 } else if (FLAG_debug_code) {
219 __ JumpIfInNewSpace(cp, r0, &done);
220 __ Abort(kExpectedNewSpaceObject);
225 Comment(";;; End allocate local context");
229 if (FLAG_trace && info()->IsOptimizing()) {
230 // We have not executed any compiled code yet, so cp still holds the
232 __ CallRuntime(Runtime::kTraceEnter, 0);
234 return !is_aborted();
238 void LCodeGen::GenerateOsrPrologue() {
239 // Generate the OSR entry prologue at the first unknown OSR value, or if there
240 // are none, at the OSR entrypoint instruction.
241 if (osr_pc_offset_ >= 0) return;
243 osr_pc_offset_ = masm()->pc_offset();
245 // Adjust the frame size, subsuming the unoptimized frame into the
247 int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
249 __ sub(sp, sp, Operand(slots * kPointerSize));
253 void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
254 if (instr->IsCall()) {
255 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
257 if (!instr->IsLazyBailout() && !instr->IsGap()) {
258 safepoints_.BumpLastLazySafepointIndex();
263 bool LCodeGen::GenerateDeferredCode() {
264 DCHECK(is_generating());
265 if (deferred_.length() > 0) {
266 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
267 LDeferredCode* code = deferred_[i];
270 instructions_->at(code->instruction_index())->hydrogen_value();
271 RecordAndWritePosition(
272 chunk()->graph()->SourcePositionToScriptPosition(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 DCHECK(!frame_is_built_);
283 DCHECK(info()->IsStub());
284 frame_is_built_ = true;
286 __ mov(scratch0(), Operand(Smi::FromInt(StackFrame::STUB)));
288 __ add(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
289 Comment(";;; Deferred code");
292 if (NeedsDeferredFrame()) {
293 Comment(";;; Destroy frame");
294 DCHECK(frame_is_built_);
297 frame_is_built_ = false;
299 __ jmp(code->exit());
303 // Force constant pool emission at the end of the deferred code to make
304 // sure that no constant pools are emitted after.
305 masm()->CheckConstPool(true, false);
307 return !is_aborted();
311 bool LCodeGen::GenerateJumpTable() {
312 // Check that the jump table is accessible from everywhere in the function
313 // code, i.e. that offsets to the table can be encoded in the 24bit signed
314 // immediate of a branch instruction.
315 // To simplify we consider the code size from the first instruction to the
316 // end of the jump table. We also don't consider the pc load delta.
317 // Each entry in the jump table generates one instruction and inlines one
318 // 32bit data after it.
319 if (!is_int24((masm()->pc_offset() / Assembler::kInstrSize) +
320 jump_table_.length() * 7)) {
321 Abort(kGeneratedCodeIsTooLarge);
324 if (jump_table_.length() > 0) {
325 Label needs_frame, call_deopt_entry;
327 Comment(";;; -------------------- Jump table --------------------");
328 Address base = jump_table_[0].address;
330 Register entry_offset = scratch0();
332 int length = jump_table_.length();
333 for (int i = 0; i < length; i++) {
334 Deoptimizer::JumpTableEntry* table_entry = &jump_table_[i];
335 __ bind(&table_entry->label);
337 DCHECK_EQ(jump_table_[0].bailout_type, table_entry->bailout_type);
338 Address entry = table_entry->address;
339 DeoptComment(table_entry->deopt_info);
341 // Second-level deopt table entries are contiguous and small, so instead
342 // of loading the full, absolute address of each one, load an immediate
343 // offset which will be added to the base address later.
344 __ mov(entry_offset, Operand(entry - base));
346 if (table_entry->needs_frame) {
347 DCHECK(!info()->saves_caller_doubles());
348 if (needs_frame.is_bound()) {
351 __ bind(&needs_frame);
352 Comment(";;; call deopt with frame");
354 // This variant of deopt can only be used with stubs. Since we don't
355 // have a function pointer to install in the stack frame that we're
356 // building, install a special marker there instead.
357 DCHECK(info()->IsStub());
358 __ mov(ip, Operand(Smi::FromInt(StackFrame::STUB)));
361 Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
362 __ bind(&call_deopt_entry);
363 // Add the base address to the offset previously loaded in
365 __ add(entry_offset, entry_offset,
366 Operand(ExternalReference::ForDeoptEntry(base)));
367 __ blx(entry_offset);
370 masm()->CheckConstPool(false, false);
372 // The last entry can fall through into `call_deopt_entry`, avoiding a
374 bool need_branch = ((i + 1) != length) || call_deopt_entry.is_bound();
376 if (need_branch) __ b(&call_deopt_entry);
378 masm()->CheckConstPool(false, !need_branch);
382 if (!call_deopt_entry.is_bound()) {
383 Comment(";;; call deopt");
384 __ bind(&call_deopt_entry);
386 if (info()->saves_caller_doubles()) {
387 DCHECK(info()->IsStub());
388 RestoreCallerDoubles();
391 // Add the base address to the offset previously loaded in entry_offset.
392 __ add(entry_offset, entry_offset,
393 Operand(ExternalReference::ForDeoptEntry(base)));
394 __ blx(entry_offset);
398 // Force constant pool emission at the end of the deopt jump table to make
399 // sure that no constant pools are emitted after.
400 masm()->CheckConstPool(true, false);
402 // The deoptimization jump table is the last part of the instruction
403 // sequence. Mark the generated code as done unless we bailed out.
404 if (!is_aborted()) status_ = DONE;
405 return !is_aborted();
409 bool LCodeGen::GenerateSafepointTable() {
411 safepoints_.Emit(masm(), GetStackSlotCount());
412 return !is_aborted();
416 Register LCodeGen::ToRegister(int index) const {
417 return Register::FromAllocationIndex(index);
421 DwVfpRegister LCodeGen::ToDoubleRegister(int index) const {
422 return DwVfpRegister::FromAllocationIndex(index);
426 Register LCodeGen::ToRegister(LOperand* op) const {
427 DCHECK(op->IsRegister());
428 return ToRegister(op->index());
432 Register LCodeGen::EmitLoadRegister(LOperand* op, Register scratch) {
433 if (op->IsRegister()) {
434 return ToRegister(op->index());
435 } else if (op->IsConstantOperand()) {
436 LConstantOperand* const_op = LConstantOperand::cast(op);
437 HConstant* constant = chunk_->LookupConstant(const_op);
438 Handle<Object> literal = constant->handle(isolate());
439 Representation r = chunk_->LookupLiteralRepresentation(const_op);
440 if (r.IsInteger32()) {
441 DCHECK(literal->IsNumber());
442 __ mov(scratch, Operand(static_cast<int32_t>(literal->Number())));
443 } else if (r.IsDouble()) {
444 Abort(kEmitLoadRegisterUnsupportedDoubleImmediate);
446 DCHECK(r.IsSmiOrTagged());
447 __ Move(scratch, literal);
450 } else if (op->IsStackSlot()) {
451 __ ldr(scratch, ToMemOperand(op));
459 DwVfpRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
460 DCHECK(op->IsDoubleRegister());
461 return ToDoubleRegister(op->index());
465 DwVfpRegister LCodeGen::EmitLoadDoubleRegister(LOperand* op,
466 SwVfpRegister flt_scratch,
467 DwVfpRegister dbl_scratch) {
468 if (op->IsDoubleRegister()) {
469 return ToDoubleRegister(op->index());
470 } else if (op->IsConstantOperand()) {
471 LConstantOperand* const_op = LConstantOperand::cast(op);
472 HConstant* constant = chunk_->LookupConstant(const_op);
473 Handle<Object> literal = constant->handle(isolate());
474 Representation r = chunk_->LookupLiteralRepresentation(const_op);
475 if (r.IsInteger32()) {
476 DCHECK(literal->IsNumber());
477 __ mov(ip, Operand(static_cast<int32_t>(literal->Number())));
478 __ vmov(flt_scratch, ip);
479 __ vcvt_f64_s32(dbl_scratch, flt_scratch);
481 } else if (r.IsDouble()) {
482 Abort(kUnsupportedDoubleImmediate);
483 } else if (r.IsTagged()) {
484 Abort(kUnsupportedTaggedImmediate);
486 } else if (op->IsStackSlot()) {
487 // TODO(regis): Why is vldr not taking a MemOperand?
488 // __ vldr(dbl_scratch, ToMemOperand(op));
489 MemOperand mem_op = ToMemOperand(op);
490 __ vldr(dbl_scratch, mem_op.rn(), mem_op.offset());
498 Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
499 HConstant* constant = chunk_->LookupConstant(op);
500 DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
501 return constant->handle(isolate());
505 bool LCodeGen::IsInteger32(LConstantOperand* op) const {
506 return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
510 bool LCodeGen::IsSmi(LConstantOperand* op) const {
511 return chunk_->LookupLiteralRepresentation(op).IsSmi();
515 int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
516 return ToRepresentation(op, Representation::Integer32());
520 int32_t LCodeGen::ToRepresentation(LConstantOperand* op,
521 const Representation& r) const {
522 HConstant* constant = chunk_->LookupConstant(op);
523 int32_t value = constant->Integer32Value();
524 if (r.IsInteger32()) return value;
525 DCHECK(r.IsSmiOrTagged());
526 return reinterpret_cast<int32_t>(Smi::FromInt(value));
530 Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
531 HConstant* constant = chunk_->LookupConstant(op);
532 return Smi::FromInt(constant->Integer32Value());
536 double LCodeGen::ToDouble(LConstantOperand* op) const {
537 HConstant* constant = chunk_->LookupConstant(op);
538 DCHECK(constant->HasDoubleValue());
539 return constant->DoubleValue();
543 Operand LCodeGen::ToOperand(LOperand* op) {
544 if (op->IsConstantOperand()) {
545 LConstantOperand* const_op = LConstantOperand::cast(op);
546 HConstant* constant = chunk()->LookupConstant(const_op);
547 Representation r = chunk_->LookupLiteralRepresentation(const_op);
549 DCHECK(constant->HasSmiValue());
550 return Operand(Smi::FromInt(constant->Integer32Value()));
551 } else if (r.IsInteger32()) {
552 DCHECK(constant->HasInteger32Value());
553 return Operand(constant->Integer32Value());
554 } else if (r.IsDouble()) {
555 Abort(kToOperandUnsupportedDoubleImmediate);
557 DCHECK(r.IsTagged());
558 return Operand(constant->handle(isolate()));
559 } else if (op->IsRegister()) {
560 return Operand(ToRegister(op));
561 } else if (op->IsDoubleRegister()) {
562 Abort(kToOperandIsDoubleRegisterUnimplemented);
563 return Operand::Zero();
565 // Stack slots not implemented, use ToMemOperand instead.
567 return Operand::Zero();
571 static int ArgumentsOffsetWithoutFrame(int index) {
573 return -(index + 1) * kPointerSize;
577 MemOperand LCodeGen::ToMemOperand(LOperand* op) const {
578 DCHECK(!op->IsRegister());
579 DCHECK(!op->IsDoubleRegister());
580 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
581 if (NeedsEagerFrame()) {
582 return MemOperand(fp, StackSlotOffset(op->index()));
584 // Retrieve parameter without eager stack-frame relative to the
586 return MemOperand(sp, ArgumentsOffsetWithoutFrame(op->index()));
591 MemOperand LCodeGen::ToHighMemOperand(LOperand* op) const {
592 DCHECK(op->IsDoubleStackSlot());
593 if (NeedsEagerFrame()) {
594 return MemOperand(fp, StackSlotOffset(op->index()) + kPointerSize);
596 // Retrieve parameter without eager stack-frame relative to the
599 sp, ArgumentsOffsetWithoutFrame(op->index()) + kPointerSize);
604 void LCodeGen::WriteTranslation(LEnvironment* environment,
605 Translation* translation) {
606 if (environment == NULL) return;
608 // The translation includes one command per value in the environment.
609 int translation_size = environment->translation_size();
610 // The output frame height does not include the parameters.
611 int height = translation_size - environment->parameter_count();
613 WriteTranslation(environment->outer(), translation);
614 bool has_closure_id = !info()->closure().is_null() &&
615 !info()->closure().is_identical_to(environment->closure());
616 int closure_id = has_closure_id
617 ? DefineDeoptimizationLiteral(environment->closure())
618 : Translation::kSelfLiteralId;
620 switch (environment->frame_type()) {
622 translation->BeginJSFrame(environment->ast_id(), closure_id, height);
625 translation->BeginConstructStubFrame(closure_id, translation_size);
628 DCHECK(translation_size == 1);
630 translation->BeginGetterStubFrame(closure_id);
633 DCHECK(translation_size == 2);
635 translation->BeginSetterStubFrame(closure_id);
638 translation->BeginCompiledStubFrame();
640 case ARGUMENTS_ADAPTOR:
641 translation->BeginArgumentsAdaptorFrame(closure_id, translation_size);
645 int object_index = 0;
646 int dematerialized_index = 0;
647 for (int i = 0; i < translation_size; ++i) {
648 LOperand* value = environment->values()->at(i);
649 AddToTranslation(environment,
652 environment->HasTaggedValueAt(i),
653 environment->HasUint32ValueAt(i),
655 &dematerialized_index);
660 void LCodeGen::AddToTranslation(LEnvironment* environment,
661 Translation* translation,
665 int* object_index_pointer,
666 int* dematerialized_index_pointer) {
667 if (op == LEnvironment::materialization_marker()) {
668 int object_index = (*object_index_pointer)++;
669 if (environment->ObjectIsDuplicateAt(object_index)) {
670 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
671 translation->DuplicateObject(dupe_of);
674 int object_length = environment->ObjectLengthAt(object_index);
675 if (environment->ObjectIsArgumentsAt(object_index)) {
676 translation->BeginArgumentsObject(object_length);
678 translation->BeginCapturedObject(object_length);
680 int dematerialized_index = *dematerialized_index_pointer;
681 int env_offset = environment->translation_size() + dematerialized_index;
682 *dematerialized_index_pointer += object_length;
683 for (int i = 0; i < object_length; ++i) {
684 LOperand* value = environment->values()->at(env_offset + i);
685 AddToTranslation(environment,
688 environment->HasTaggedValueAt(env_offset + i),
689 environment->HasUint32ValueAt(env_offset + i),
690 object_index_pointer,
691 dematerialized_index_pointer);
696 if (op->IsStackSlot()) {
698 translation->StoreStackSlot(op->index());
699 } else if (is_uint32) {
700 translation->StoreUint32StackSlot(op->index());
702 translation->StoreInt32StackSlot(op->index());
704 } else if (op->IsDoubleStackSlot()) {
705 translation->StoreDoubleStackSlot(op->index());
706 } else if (op->IsRegister()) {
707 Register reg = ToRegister(op);
709 translation->StoreRegister(reg);
710 } else if (is_uint32) {
711 translation->StoreUint32Register(reg);
713 translation->StoreInt32Register(reg);
715 } else if (op->IsDoubleRegister()) {
716 DoubleRegister reg = ToDoubleRegister(op);
717 translation->StoreDoubleRegister(reg);
718 } else if (op->IsConstantOperand()) {
719 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
720 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
721 translation->StoreLiteral(src_index);
728 int LCodeGen::CallCodeSize(Handle<Code> code, RelocInfo::Mode mode) {
729 int size = masm()->CallSize(code, mode);
730 if (code->kind() == Code::BINARY_OP_IC ||
731 code->kind() == Code::COMPARE_IC) {
732 size += Assembler::kInstrSize; // extra nop() added in CallCodeGeneric.
738 void LCodeGen::CallCode(Handle<Code> code,
739 RelocInfo::Mode mode,
741 TargetAddressStorageMode storage_mode) {
742 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT, storage_mode);
746 void LCodeGen::CallCodeGeneric(Handle<Code> code,
747 RelocInfo::Mode mode,
749 SafepointMode safepoint_mode,
750 TargetAddressStorageMode storage_mode) {
751 DCHECK(instr != NULL);
752 // Block literal pool emission to ensure nop indicating no inlined smi code
753 // is in the correct position.
754 Assembler::BlockConstPoolScope block_const_pool(masm());
755 __ Call(code, mode, TypeFeedbackId::None(), al, storage_mode);
756 RecordSafepointWithLazyDeopt(instr, safepoint_mode);
758 // Signal that we don't inline smi code before these stubs in the
759 // optimizing code generator.
760 if (code->kind() == Code::BINARY_OP_IC ||
761 code->kind() == Code::COMPARE_IC) {
767 void LCodeGen::CallRuntime(const Runtime::Function* function,
770 SaveFPRegsMode save_doubles) {
771 DCHECK(instr != NULL);
773 __ CallRuntime(function, num_arguments, save_doubles);
775 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
779 void LCodeGen::LoadContextFromDeferred(LOperand* context) {
780 if (context->IsRegister()) {
781 __ Move(cp, ToRegister(context));
782 } else if (context->IsStackSlot()) {
783 __ ldr(cp, ToMemOperand(context));
784 } else if (context->IsConstantOperand()) {
785 HConstant* constant =
786 chunk_->LookupConstant(LConstantOperand::cast(context));
787 __ Move(cp, Handle<Object>::cast(constant->handle(isolate())));
794 void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
798 LoadContextFromDeferred(context);
799 __ CallRuntimeSaveDoubles(id);
800 RecordSafepointWithRegisters(
801 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
805 void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
806 Safepoint::DeoptMode mode) {
807 environment->set_has_been_used();
808 if (!environment->HasBeenRegistered()) {
809 // Physical stack frame layout:
810 // -x ............. -4 0 ..................................... y
811 // [incoming arguments] [spill slots] [pushed outgoing arguments]
813 // Layout of the environment:
814 // 0 ..................................................... size-1
815 // [parameters] [locals] [expression stack including arguments]
817 // Layout of the translation:
818 // 0 ........................................................ size - 1 + 4
819 // [expression stack including arguments] [locals] [4 words] [parameters]
820 // |>------------ translation_size ------------<|
823 int jsframe_count = 0;
824 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
826 if (e->frame_type() == JS_FUNCTION) {
830 Translation translation(&translations_, frame_count, jsframe_count, zone());
831 WriteTranslation(environment, &translation);
832 int deoptimization_index = deoptimizations_.length();
833 int pc_offset = masm()->pc_offset();
834 environment->Register(deoptimization_index,
836 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
837 deoptimizations_.Add(environment, zone());
842 void LCodeGen::DeoptimizeIf(Condition condition, LInstruction* instr,
843 Deoptimizer::DeoptReason deopt_reason,
844 Deoptimizer::BailoutType bailout_type) {
845 LEnvironment* environment = instr->environment();
846 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
847 DCHECK(environment->HasBeenRegistered());
848 int id = environment->deoptimization_index();
849 DCHECK(info()->IsOptimizing() || info()->IsStub());
851 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
853 Abort(kBailoutWasNotPrepared);
857 if (FLAG_deopt_every_n_times != 0 && !info()->IsStub()) {
858 Register scratch = scratch0();
859 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
861 // Store the condition on the stack if necessary
862 if (condition != al) {
863 __ mov(scratch, Operand::Zero(), LeaveCC, NegateCondition(condition));
864 __ mov(scratch, Operand(1), LeaveCC, condition);
869 __ mov(scratch, Operand(count));
870 __ ldr(r1, MemOperand(scratch));
871 __ sub(r1, r1, Operand(1), SetCC);
872 __ mov(r1, Operand(FLAG_deopt_every_n_times), LeaveCC, eq);
873 __ str(r1, MemOperand(scratch));
876 if (condition != al) {
877 // Clean up the stack before the deoptimizer call
881 __ Call(entry, RelocInfo::RUNTIME_ENTRY, eq);
883 // 'Restore' the condition in a slightly hacky way. (It would be better
884 // to use 'msr' and 'mrs' instructions here, but they are not supported by
885 // our ARM simulator).
886 if (condition != al) {
888 __ cmp(scratch, Operand::Zero());
892 if (info()->ShouldTrapOnDeopt()) {
893 __ stop("trap_on_deopt", condition);
896 Deoptimizer::DeoptInfo deopt_info(instr->hydrogen_value()->position().raw(),
897 instr->Mnemonic(), deopt_reason);
898 DCHECK(info()->IsStub() || frame_is_built_);
899 // Go through jump table if we need to handle condition, build frame, or
900 // restore caller doubles.
901 if (condition == al && frame_is_built_ &&
902 !info()->saves_caller_doubles()) {
903 DeoptComment(deopt_info);
904 __ Call(entry, RelocInfo::RUNTIME_ENTRY);
906 Deoptimizer::JumpTableEntry table_entry(entry, deopt_info, bailout_type,
908 // We often have several deopts to the same entry, reuse the last
909 // jump entry if this is the case.
910 if (FLAG_trace_deopt || isolate()->cpu_profiler()->is_profiling() ||
911 jump_table_.is_empty() ||
912 !table_entry.IsEquivalentTo(jump_table_.last())) {
913 jump_table_.Add(table_entry, zone());
915 __ b(condition, &jump_table_.last().label);
920 void LCodeGen::DeoptimizeIf(Condition condition, LInstruction* instr,
921 Deoptimizer::DeoptReason deopt_reason) {
922 Deoptimizer::BailoutType bailout_type = info()->IsStub()
924 : Deoptimizer::EAGER;
925 DeoptimizeIf(condition, instr, deopt_reason, bailout_type);
929 void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
930 int length = deoptimizations_.length();
931 if (length == 0) return;
932 Handle<DeoptimizationInputData> data =
933 DeoptimizationInputData::New(isolate(), length, TENURED);
935 Handle<ByteArray> translations =
936 translations_.CreateByteArray(isolate()->factory());
937 data->SetTranslationByteArray(*translations);
938 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
939 data->SetOptimizationId(Smi::FromInt(info_->optimization_id()));
940 if (info_->IsOptimizing()) {
941 // Reference to shared function info does not change between phases.
942 AllowDeferredHandleDereference allow_handle_dereference;
943 data->SetSharedFunctionInfo(*info_->shared_info());
945 data->SetSharedFunctionInfo(Smi::FromInt(0));
947 data->SetWeakCellCache(Smi::FromInt(0));
949 Handle<FixedArray> literals =
950 factory()->NewFixedArray(deoptimization_literals_.length(), TENURED);
951 { AllowDeferredHandleDereference copy_handles;
952 for (int i = 0; i < deoptimization_literals_.length(); i++) {
953 literals->set(i, *deoptimization_literals_[i]);
955 data->SetLiteralArray(*literals);
958 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id().ToInt()));
959 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
961 // Populate the deoptimization entries.
962 for (int i = 0; i < length; i++) {
963 LEnvironment* env = deoptimizations_[i];
964 data->SetAstId(i, env->ast_id());
965 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
966 data->SetArgumentsStackHeight(i,
967 Smi::FromInt(env->arguments_stack_height()));
968 data->SetPc(i, Smi::FromInt(env->pc_offset()));
970 code->set_deoptimization_data(*data);
974 int LCodeGen::DefineDeoptimizationLiteral(Handle<Object> literal) {
975 int result = deoptimization_literals_.length();
976 for (int i = 0; i < deoptimization_literals_.length(); ++i) {
977 if (deoptimization_literals_[i].is_identical_to(literal)) return i;
979 deoptimization_literals_.Add(literal, zone());
984 void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
985 DCHECK(deoptimization_literals_.length() == 0);
987 const ZoneList<Handle<JSFunction> >* inlined_closures =
988 chunk()->inlined_closures();
990 for (int i = 0, length = inlined_closures->length();
993 DefineDeoptimizationLiteral(inlined_closures->at(i));
996 inlined_function_count_ = deoptimization_literals_.length();
1000 void LCodeGen::RecordSafepointWithLazyDeopt(
1001 LInstruction* instr, SafepointMode safepoint_mode) {
1002 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
1003 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
1005 DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
1006 RecordSafepointWithRegisters(
1007 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
1012 void LCodeGen::RecordSafepoint(
1013 LPointerMap* pointers,
1014 Safepoint::Kind kind,
1016 Safepoint::DeoptMode deopt_mode) {
1017 DCHECK(expected_safepoint_kind_ == kind);
1019 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
1020 Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
1021 kind, arguments, deopt_mode);
1022 for (int i = 0; i < operands->length(); i++) {
1023 LOperand* pointer = operands->at(i);
1024 if (pointer->IsStackSlot()) {
1025 safepoint.DefinePointerSlot(pointer->index(), zone());
1026 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
1027 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
1030 if (FLAG_enable_ool_constant_pool && (kind & Safepoint::kWithRegisters)) {
1031 // Register pp always contains a pointer to the constant pool.
1032 safepoint.DefinePointerRegister(pp, zone());
1037 void LCodeGen::RecordSafepoint(LPointerMap* pointers,
1038 Safepoint::DeoptMode deopt_mode) {
1039 RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
1043 void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
1044 LPointerMap empty_pointers(zone());
1045 RecordSafepoint(&empty_pointers, deopt_mode);
1049 void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
1051 Safepoint::DeoptMode deopt_mode) {
1053 pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
1057 void LCodeGen::RecordAndWritePosition(int position) {
1058 if (position == RelocInfo::kNoPosition) return;
1059 masm()->positions_recorder()->RecordPosition(position);
1060 masm()->positions_recorder()->WriteRecordedPositions();
1064 static const char* LabelType(LLabel* label) {
1065 if (label->is_loop_header()) return " (loop header)";
1066 if (label->is_osr_entry()) return " (OSR entry)";
1071 void LCodeGen::DoLabel(LLabel* label) {
1072 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
1073 current_instruction_,
1074 label->hydrogen_value()->id(),
1077 __ bind(label->label());
1078 current_block_ = label->block_id();
1083 void LCodeGen::DoParallelMove(LParallelMove* move) {
1084 resolver_.Resolve(move);
1088 void LCodeGen::DoGap(LGap* gap) {
1089 for (int i = LGap::FIRST_INNER_POSITION;
1090 i <= LGap::LAST_INNER_POSITION;
1092 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
1093 LParallelMove* move = gap->GetParallelMove(inner_pos);
1094 if (move != NULL) DoParallelMove(move);
1099 void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
1104 void LCodeGen::DoParameter(LParameter* instr) {
1109 void LCodeGen::DoCallStub(LCallStub* instr) {
1110 DCHECK(ToRegister(instr->context()).is(cp));
1111 DCHECK(ToRegister(instr->result()).is(r0));
1112 switch (instr->hydrogen()->major_key()) {
1113 case CodeStub::RegExpExec: {
1114 RegExpExecStub stub(isolate());
1115 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1118 case CodeStub::SubString: {
1119 SubStringStub stub(isolate());
1120 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1123 case CodeStub::StringCompare: {
1124 StringCompareStub stub(isolate());
1125 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1134 void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
1135 GenerateOsrPrologue();
1139 void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
1140 Register dividend = ToRegister(instr->dividend());
1141 int32_t divisor = instr->divisor();
1142 DCHECK(dividend.is(ToRegister(instr->result())));
1144 // Theoretically, a variation of the branch-free code for integer division by
1145 // a power of 2 (calculating the remainder via an additional multiplication
1146 // (which gets simplified to an 'and') and subtraction) should be faster, and
1147 // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
1148 // indicate that positive dividends are heavily favored, so the branching
1149 // version performs better.
1150 HMod* hmod = instr->hydrogen();
1151 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1152 Label dividend_is_not_negative, done;
1153 if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
1154 __ cmp(dividend, Operand::Zero());
1155 __ b(pl, ÷nd_is_not_negative);
1156 // Note that this is correct even for kMinInt operands.
1157 __ rsb(dividend, dividend, Operand::Zero());
1158 __ and_(dividend, dividend, Operand(mask));
1159 __ rsb(dividend, dividend, Operand::Zero(), SetCC);
1160 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1161 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
1166 __ bind(÷nd_is_not_negative);
1167 __ and_(dividend, dividend, Operand(mask));
1172 void LCodeGen::DoModByConstI(LModByConstI* instr) {
1173 Register dividend = ToRegister(instr->dividend());
1174 int32_t divisor = instr->divisor();
1175 Register result = ToRegister(instr->result());
1176 DCHECK(!dividend.is(result));
1179 DeoptimizeIf(al, instr, Deoptimizer::kDivisionByZero);
1183 __ TruncatingDiv(result, dividend, Abs(divisor));
1184 __ mov(ip, Operand(Abs(divisor)));
1185 __ smull(result, ip, result, ip);
1186 __ sub(result, dividend, result, SetCC);
1188 // Check for negative zero.
1189 HMod* hmod = instr->hydrogen();
1190 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1191 Label remainder_not_zero;
1192 __ b(ne, &remainder_not_zero);
1193 __ cmp(dividend, Operand::Zero());
1194 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero);
1195 __ bind(&remainder_not_zero);
1200 void LCodeGen::DoModI(LModI* instr) {
1201 HMod* hmod = instr->hydrogen();
1202 if (CpuFeatures::IsSupported(SUDIV)) {
1203 CpuFeatureScope scope(masm(), SUDIV);
1205 Register left_reg = ToRegister(instr->left());
1206 Register right_reg = ToRegister(instr->right());
1207 Register result_reg = ToRegister(instr->result());
1210 // Check for x % 0, sdiv might signal an exception. We have to deopt in this
1211 // case because we can't return a NaN.
1212 if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
1213 __ cmp(right_reg, Operand::Zero());
1214 DeoptimizeIf(eq, instr, Deoptimizer::kDivisionByZero);
1217 // Check for kMinInt % -1, sdiv will return kMinInt, which is not what we
1218 // want. We have to deopt if we care about -0, because we can't return that.
1219 if (hmod->CheckFlag(HValue::kCanOverflow)) {
1220 Label no_overflow_possible;
1221 __ cmp(left_reg, Operand(kMinInt));
1222 __ b(ne, &no_overflow_possible);
1223 __ cmp(right_reg, Operand(-1));
1224 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1225 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
1227 __ b(ne, &no_overflow_possible);
1228 __ mov(result_reg, Operand::Zero());
1231 __ bind(&no_overflow_possible);
1234 // For 'r3 = r1 % r2' we can have the following ARM code:
1236 // mls r3, r3, r2, r1
1238 __ sdiv(result_reg, left_reg, right_reg);
1239 __ Mls(result_reg, result_reg, right_reg, left_reg);
1241 // If we care about -0, test if the dividend is <0 and the result is 0.
1242 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1243 __ cmp(result_reg, Operand::Zero());
1245 __ cmp(left_reg, Operand::Zero());
1246 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero);
1251 // General case, without any SDIV support.
1252 Register left_reg = ToRegister(instr->left());
1253 Register right_reg = ToRegister(instr->right());
1254 Register result_reg = ToRegister(instr->result());
1255 Register scratch = scratch0();
1256 DCHECK(!scratch.is(left_reg));
1257 DCHECK(!scratch.is(right_reg));
1258 DCHECK(!scratch.is(result_reg));
1259 DwVfpRegister dividend = ToDoubleRegister(instr->temp());
1260 DwVfpRegister divisor = ToDoubleRegister(instr->temp2());
1261 DCHECK(!divisor.is(dividend));
1262 LowDwVfpRegister quotient = double_scratch0();
1263 DCHECK(!quotient.is(dividend));
1264 DCHECK(!quotient.is(divisor));
1267 // Check for x % 0, we have to deopt in this case because we can't return a
1269 if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
1270 __ cmp(right_reg, Operand::Zero());
1271 DeoptimizeIf(eq, instr, Deoptimizer::kDivisionByZero);
1274 __ Move(result_reg, left_reg);
1275 // Load the arguments in VFP registers. The divisor value is preloaded
1276 // before. Be careful that 'right_reg' is only live on entry.
1277 // TODO(svenpanne) The last comments seems to be wrong nowadays.
1278 __ vmov(double_scratch0().low(), left_reg);
1279 __ vcvt_f64_s32(dividend, double_scratch0().low());
1280 __ vmov(double_scratch0().low(), right_reg);
1281 __ vcvt_f64_s32(divisor, double_scratch0().low());
1283 // We do not care about the sign of the divisor. Note that we still handle
1284 // the kMinInt % -1 case correctly, though.
1285 __ vabs(divisor, divisor);
1286 // Compute the quotient and round it to a 32bit integer.
1287 __ vdiv(quotient, dividend, divisor);
1288 __ vcvt_s32_f64(quotient.low(), quotient);
1289 __ vcvt_f64_s32(quotient, quotient.low());
1291 // Compute the remainder in result.
1292 __ vmul(double_scratch0(), divisor, quotient);
1293 __ vcvt_s32_f64(double_scratch0().low(), double_scratch0());
1294 __ vmov(scratch, double_scratch0().low());
1295 __ sub(result_reg, left_reg, scratch, SetCC);
1297 // If we care about -0, test if the dividend is <0 and the result is 0.
1298 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1300 __ cmp(left_reg, Operand::Zero());
1301 DeoptimizeIf(mi, instr, Deoptimizer::kMinusZero);
1308 void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
1309 Register dividend = ToRegister(instr->dividend());
1310 int32_t divisor = instr->divisor();
1311 Register result = ToRegister(instr->result());
1312 DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
1313 DCHECK(!result.is(dividend));
1315 // Check for (0 / -x) that will produce negative zero.
1316 HDiv* hdiv = instr->hydrogen();
1317 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1318 __ cmp(dividend, Operand::Zero());
1319 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
1321 // Check for (kMinInt / -1).
1322 if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
1323 __ cmp(dividend, Operand(kMinInt));
1324 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow);
1326 // Deoptimize if remainder will not be 0.
1327 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1328 divisor != 1 && divisor != -1) {
1329 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1330 __ tst(dividend, Operand(mask));
1331 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision);
1334 if (divisor == -1) { // Nice shortcut, not needed for correctness.
1335 __ rsb(result, dividend, Operand(0));
1338 int32_t shift = WhichPowerOf2Abs(divisor);
1340 __ mov(result, dividend);
1341 } else if (shift == 1) {
1342 __ add(result, dividend, Operand(dividend, LSR, 31));
1344 __ mov(result, Operand(dividend, ASR, 31));
1345 __ add(result, dividend, Operand(result, LSR, 32 - shift));
1347 if (shift > 0) __ mov(result, Operand(result, ASR, shift));
1348 if (divisor < 0) __ rsb(result, result, Operand(0));
1352 void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
1353 Register dividend = ToRegister(instr->dividend());
1354 int32_t divisor = instr->divisor();
1355 Register result = ToRegister(instr->result());
1356 DCHECK(!dividend.is(result));
1359 DeoptimizeIf(al, instr, Deoptimizer::kDivisionByZero);
1363 // Check for (0 / -x) that will produce negative zero.
1364 HDiv* hdiv = instr->hydrogen();
1365 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1366 __ cmp(dividend, Operand::Zero());
1367 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
1370 __ TruncatingDiv(result, dividend, Abs(divisor));
1371 if (divisor < 0) __ rsb(result, result, Operand::Zero());
1373 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1374 __ mov(ip, Operand(divisor));
1375 __ smull(scratch0(), ip, result, ip);
1376 __ sub(scratch0(), scratch0(), dividend, SetCC);
1377 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision);
1382 // TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
1383 void LCodeGen::DoDivI(LDivI* instr) {
1384 HBinaryOperation* hdiv = instr->hydrogen();
1385 Register dividend = ToRegister(instr->dividend());
1386 Register divisor = ToRegister(instr->divisor());
1387 Register result = ToRegister(instr->result());
1390 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1391 __ cmp(divisor, Operand::Zero());
1392 DeoptimizeIf(eq, instr, Deoptimizer::kDivisionByZero);
1395 // Check for (0 / -x) that will produce negative zero.
1396 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1398 if (!instr->hydrogen_value()->CheckFlag(HValue::kCanBeDivByZero)) {
1399 // Do the test only if it hadn't be done above.
1400 __ cmp(divisor, Operand::Zero());
1402 __ b(pl, &positive);
1403 __ cmp(dividend, Operand::Zero());
1404 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
1408 // Check for (kMinInt / -1).
1409 if (hdiv->CheckFlag(HValue::kCanOverflow) &&
1410 (!CpuFeatures::IsSupported(SUDIV) ||
1411 !hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32))) {
1412 // We don't need to check for overflow when truncating with sdiv
1413 // support because, on ARM, sdiv kMinInt, -1 -> kMinInt.
1414 __ cmp(dividend, Operand(kMinInt));
1415 __ cmp(divisor, Operand(-1), eq);
1416 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow);
1419 if (CpuFeatures::IsSupported(SUDIV)) {
1420 CpuFeatureScope scope(masm(), SUDIV);
1421 __ sdiv(result, dividend, divisor);
1423 DoubleRegister vleft = ToDoubleRegister(instr->temp());
1424 DoubleRegister vright = double_scratch0();
1425 __ vmov(double_scratch0().low(), dividend);
1426 __ vcvt_f64_s32(vleft, double_scratch0().low());
1427 __ vmov(double_scratch0().low(), divisor);
1428 __ vcvt_f64_s32(vright, double_scratch0().low());
1429 __ vdiv(vleft, vleft, vright); // vleft now contains the result.
1430 __ vcvt_s32_f64(double_scratch0().low(), vleft);
1431 __ vmov(result, double_scratch0().low());
1434 if (!hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1435 // Compute remainder and deopt if it's not zero.
1436 Register remainder = scratch0();
1437 __ Mls(remainder, result, divisor, dividend);
1438 __ cmp(remainder, Operand::Zero());
1439 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision);
1444 void LCodeGen::DoMultiplyAddD(LMultiplyAddD* instr) {
1445 DwVfpRegister addend = ToDoubleRegister(instr->addend());
1446 DwVfpRegister multiplier = ToDoubleRegister(instr->multiplier());
1447 DwVfpRegister multiplicand = ToDoubleRegister(instr->multiplicand());
1449 // This is computed in-place.
1450 DCHECK(addend.is(ToDoubleRegister(instr->result())));
1452 __ vmla(addend, multiplier, multiplicand);
1456 void LCodeGen::DoMultiplySubD(LMultiplySubD* instr) {
1457 DwVfpRegister minuend = ToDoubleRegister(instr->minuend());
1458 DwVfpRegister multiplier = ToDoubleRegister(instr->multiplier());
1459 DwVfpRegister multiplicand = ToDoubleRegister(instr->multiplicand());
1461 // This is computed in-place.
1462 DCHECK(minuend.is(ToDoubleRegister(instr->result())));
1464 __ vmls(minuend, multiplier, multiplicand);
1468 void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
1469 Register dividend = ToRegister(instr->dividend());
1470 Register result = ToRegister(instr->result());
1471 int32_t divisor = instr->divisor();
1473 // If the divisor is 1, return the dividend.
1475 __ Move(result, dividend);
1479 // If the divisor is positive, things are easy: There can be no deopts and we
1480 // can simply do an arithmetic right shift.
1481 int32_t shift = WhichPowerOf2Abs(divisor);
1483 __ mov(result, Operand(dividend, ASR, shift));
1487 // If the divisor is negative, we have to negate and handle edge cases.
1488 __ rsb(result, dividend, Operand::Zero(), SetCC);
1489 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1490 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
1493 // Dividing by -1 is basically negation, unless we overflow.
1494 if (divisor == -1) {
1495 if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1496 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
1501 // If the negation could not overflow, simply shifting is OK.
1502 if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1503 __ mov(result, Operand(result, ASR, shift));
1507 __ mov(result, Operand(kMinInt / divisor), LeaveCC, vs);
1508 __ mov(result, Operand(result, ASR, shift), LeaveCC, vc);
1512 void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
1513 Register dividend = ToRegister(instr->dividend());
1514 int32_t divisor = instr->divisor();
1515 Register result = ToRegister(instr->result());
1516 DCHECK(!dividend.is(result));
1519 DeoptimizeIf(al, instr, Deoptimizer::kDivisionByZero);
1523 // Check for (0 / -x) that will produce negative zero.
1524 HMathFloorOfDiv* hdiv = instr->hydrogen();
1525 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1526 __ cmp(dividend, Operand::Zero());
1527 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
1530 // Easy case: We need no dynamic check for the dividend and the flooring
1531 // division is the same as the truncating division.
1532 if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
1533 (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
1534 __ TruncatingDiv(result, dividend, Abs(divisor));
1535 if (divisor < 0) __ rsb(result, result, Operand::Zero());
1539 // In the general case we may need to adjust before and after the truncating
1540 // division to get a flooring division.
1541 Register temp = ToRegister(instr->temp());
1542 DCHECK(!temp.is(dividend) && !temp.is(result));
1543 Label needs_adjustment, done;
1544 __ cmp(dividend, Operand::Zero());
1545 __ b(divisor > 0 ? lt : gt, &needs_adjustment);
1546 __ TruncatingDiv(result, dividend, Abs(divisor));
1547 if (divisor < 0) __ rsb(result, result, Operand::Zero());
1549 __ bind(&needs_adjustment);
1550 __ add(temp, dividend, Operand(divisor > 0 ? 1 : -1));
1551 __ TruncatingDiv(result, temp, Abs(divisor));
1552 if (divisor < 0) __ rsb(result, result, Operand::Zero());
1553 __ sub(result, result, Operand(1));
1558 // TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
1559 void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
1560 HBinaryOperation* hdiv = instr->hydrogen();
1561 Register left = ToRegister(instr->dividend());
1562 Register right = ToRegister(instr->divisor());
1563 Register result = ToRegister(instr->result());
1566 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1567 __ cmp(right, Operand::Zero());
1568 DeoptimizeIf(eq, instr, Deoptimizer::kDivisionByZero);
1571 // Check for (0 / -x) that will produce negative zero.
1572 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1574 if (!instr->hydrogen_value()->CheckFlag(HValue::kCanBeDivByZero)) {
1575 // Do the test only if it hadn't be done above.
1576 __ cmp(right, Operand::Zero());
1578 __ b(pl, &positive);
1579 __ cmp(left, Operand::Zero());
1580 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
1584 // Check for (kMinInt / -1).
1585 if (hdiv->CheckFlag(HValue::kCanOverflow) &&
1586 (!CpuFeatures::IsSupported(SUDIV) ||
1587 !hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32))) {
1588 // We don't need to check for overflow when truncating with sdiv
1589 // support because, on ARM, sdiv kMinInt, -1 -> kMinInt.
1590 __ cmp(left, Operand(kMinInt));
1591 __ cmp(right, Operand(-1), eq);
1592 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow);
1595 if (CpuFeatures::IsSupported(SUDIV)) {
1596 CpuFeatureScope scope(masm(), SUDIV);
1597 __ sdiv(result, left, right);
1599 DoubleRegister vleft = ToDoubleRegister(instr->temp());
1600 DoubleRegister vright = double_scratch0();
1601 __ vmov(double_scratch0().low(), left);
1602 __ vcvt_f64_s32(vleft, double_scratch0().low());
1603 __ vmov(double_scratch0().low(), right);
1604 __ vcvt_f64_s32(vright, double_scratch0().low());
1605 __ vdiv(vleft, vleft, vright); // vleft now contains the result.
1606 __ vcvt_s32_f64(double_scratch0().low(), vleft);
1607 __ vmov(result, double_scratch0().low());
1611 Register remainder = scratch0();
1612 __ Mls(remainder, result, right, left);
1613 __ cmp(remainder, Operand::Zero());
1615 __ eor(remainder, remainder, Operand(right));
1616 __ add(result, result, Operand(remainder, ASR, 31));
1621 void LCodeGen::DoMulI(LMulI* instr) {
1622 Register result = ToRegister(instr->result());
1623 // Note that result may alias left.
1624 Register left = ToRegister(instr->left());
1625 LOperand* right_op = instr->right();
1627 bool bailout_on_minus_zero =
1628 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
1629 bool overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1631 if (right_op->IsConstantOperand()) {
1632 int32_t constant = ToInteger32(LConstantOperand::cast(right_op));
1634 if (bailout_on_minus_zero && (constant < 0)) {
1635 // The case of a null constant will be handled separately.
1636 // If constant is negative and left is null, the result should be -0.
1637 __ cmp(left, Operand::Zero());
1638 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
1644 __ rsb(result, left, Operand::Zero(), SetCC);
1645 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
1647 __ rsb(result, left, Operand::Zero());
1651 if (bailout_on_minus_zero) {
1652 // If left is strictly negative and the constant is null, the
1653 // result is -0. Deoptimize if required, otherwise return 0.
1654 __ cmp(left, Operand::Zero());
1655 DeoptimizeIf(mi, instr, Deoptimizer::kMinusZero);
1657 __ mov(result, Operand::Zero());
1660 __ Move(result, left);
1663 // Multiplying by powers of two and powers of two plus or minus
1664 // one can be done faster with shifted operands.
1665 // For other constants we emit standard code.
1666 int32_t mask = constant >> 31;
1667 uint32_t constant_abs = (constant + mask) ^ mask;
1669 if (base::bits::IsPowerOfTwo32(constant_abs)) {
1670 int32_t shift = WhichPowerOf2(constant_abs);
1671 __ mov(result, Operand(left, LSL, shift));
1672 // Correct the sign of the result is the constant is negative.
1673 if (constant < 0) __ rsb(result, result, Operand::Zero());
1674 } else if (base::bits::IsPowerOfTwo32(constant_abs - 1)) {
1675 int32_t shift = WhichPowerOf2(constant_abs - 1);
1676 __ add(result, left, Operand(left, LSL, shift));
1677 // Correct the sign of the result is the constant is negative.
1678 if (constant < 0) __ rsb(result, result, Operand::Zero());
1679 } else if (base::bits::IsPowerOfTwo32(constant_abs + 1)) {
1680 int32_t shift = WhichPowerOf2(constant_abs + 1);
1681 __ rsb(result, left, Operand(left, LSL, shift));
1682 // Correct the sign of the result is the constant is negative.
1683 if (constant < 0) __ rsb(result, result, Operand::Zero());
1685 // Generate standard code.
1686 __ mov(ip, Operand(constant));
1687 __ mul(result, left, ip);
1692 DCHECK(right_op->IsRegister());
1693 Register right = ToRegister(right_op);
1696 Register scratch = scratch0();
1697 // scratch:result = left * right.
1698 if (instr->hydrogen()->representation().IsSmi()) {
1699 __ SmiUntag(result, left);
1700 __ smull(result, scratch, result, right);
1702 __ smull(result, scratch, left, right);
1704 __ cmp(scratch, Operand(result, ASR, 31));
1705 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow);
1707 if (instr->hydrogen()->representation().IsSmi()) {
1708 __ SmiUntag(result, left);
1709 __ mul(result, result, right);
1711 __ mul(result, left, right);
1715 if (bailout_on_minus_zero) {
1717 __ teq(left, Operand(right));
1719 // Bail out if the result is minus zero.
1720 __ cmp(result, Operand::Zero());
1721 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
1728 void LCodeGen::DoBitI(LBitI* instr) {
1729 LOperand* left_op = instr->left();
1730 LOperand* right_op = instr->right();
1731 DCHECK(left_op->IsRegister());
1732 Register left = ToRegister(left_op);
1733 Register result = ToRegister(instr->result());
1734 Operand right(no_reg);
1736 if (right_op->IsStackSlot()) {
1737 right = Operand(EmitLoadRegister(right_op, ip));
1739 DCHECK(right_op->IsRegister() || right_op->IsConstantOperand());
1740 right = ToOperand(right_op);
1743 switch (instr->op()) {
1744 case Token::BIT_AND:
1745 __ and_(result, left, right);
1748 __ orr(result, left, right);
1750 case Token::BIT_XOR:
1751 if (right_op->IsConstantOperand() && right.immediate() == int32_t(~0)) {
1752 __ mvn(result, Operand(left));
1754 __ eor(result, left, right);
1764 void LCodeGen::DoShiftI(LShiftI* instr) {
1765 // Both 'left' and 'right' are "used at start" (see LCodeGen::DoShift), so
1766 // result may alias either of them.
1767 LOperand* right_op = instr->right();
1768 Register left = ToRegister(instr->left());
1769 Register result = ToRegister(instr->result());
1770 Register scratch = scratch0();
1771 if (right_op->IsRegister()) {
1772 // Mask the right_op operand.
1773 __ and_(scratch, ToRegister(right_op), Operand(0x1F));
1774 switch (instr->op()) {
1776 __ mov(result, Operand(left, ROR, scratch));
1779 __ mov(result, Operand(left, ASR, scratch));
1782 if (instr->can_deopt()) {
1783 __ mov(result, Operand(left, LSR, scratch), SetCC);
1784 DeoptimizeIf(mi, instr, Deoptimizer::kNegativeValue);
1786 __ mov(result, Operand(left, LSR, scratch));
1790 __ mov(result, Operand(left, LSL, scratch));
1797 // Mask the right_op operand.
1798 int value = ToInteger32(LConstantOperand::cast(right_op));
1799 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1800 switch (instr->op()) {
1802 if (shift_count != 0) {
1803 __ mov(result, Operand(left, ROR, shift_count));
1805 __ Move(result, left);
1809 if (shift_count != 0) {
1810 __ mov(result, Operand(left, ASR, shift_count));
1812 __ Move(result, left);
1816 if (shift_count != 0) {
1817 __ mov(result, Operand(left, LSR, shift_count));
1819 if (instr->can_deopt()) {
1820 __ tst(left, Operand(0x80000000));
1821 DeoptimizeIf(ne, instr, Deoptimizer::kNegativeValue);
1823 __ Move(result, left);
1827 if (shift_count != 0) {
1828 if (instr->hydrogen_value()->representation().IsSmi() &&
1829 instr->can_deopt()) {
1830 if (shift_count != 1) {
1831 __ mov(result, Operand(left, LSL, shift_count - 1));
1832 __ SmiTag(result, result, SetCC);
1834 __ SmiTag(result, left, SetCC);
1836 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
1838 __ mov(result, Operand(left, LSL, shift_count));
1841 __ Move(result, left);
1852 void LCodeGen::DoSubI(LSubI* instr) {
1853 LOperand* left = instr->left();
1854 LOperand* right = instr->right();
1855 LOperand* result = instr->result();
1856 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1857 SBit set_cond = can_overflow ? SetCC : LeaveCC;
1859 if (right->IsStackSlot()) {
1860 Register right_reg = EmitLoadRegister(right, ip);
1861 __ sub(ToRegister(result), ToRegister(left), Operand(right_reg), set_cond);
1863 DCHECK(right->IsRegister() || right->IsConstantOperand());
1864 __ sub(ToRegister(result), ToRegister(left), ToOperand(right), set_cond);
1868 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
1873 void LCodeGen::DoRSubI(LRSubI* instr) {
1874 LOperand* left = instr->left();
1875 LOperand* right = instr->right();
1876 LOperand* result = instr->result();
1877 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1878 SBit set_cond = can_overflow ? SetCC : LeaveCC;
1880 if (right->IsStackSlot()) {
1881 Register right_reg = EmitLoadRegister(right, ip);
1882 __ rsb(ToRegister(result), ToRegister(left), Operand(right_reg), set_cond);
1884 DCHECK(right->IsRegister() || right->IsConstantOperand());
1885 __ rsb(ToRegister(result), ToRegister(left), ToOperand(right), set_cond);
1889 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
1894 void LCodeGen::DoConstantI(LConstantI* instr) {
1895 __ mov(ToRegister(instr->result()), Operand(instr->value()));
1899 void LCodeGen::DoConstantS(LConstantS* instr) {
1900 __ mov(ToRegister(instr->result()), Operand(instr->value()));
1904 void LCodeGen::DoConstantD(LConstantD* instr) {
1905 DCHECK(instr->result()->IsDoubleRegister());
1906 DwVfpRegister result = ToDoubleRegister(instr->result());
1907 #if V8_HOST_ARCH_IA32
1908 // Need some crappy work-around for x87 sNaN -> qNaN breakage in simulator
1910 uint64_t bits = instr->bits();
1911 if ((bits & V8_UINT64_C(0x7FF8000000000000)) ==
1912 V8_UINT64_C(0x7FF0000000000000)) {
1913 uint32_t lo = static_cast<uint32_t>(bits);
1914 uint32_t hi = static_cast<uint32_t>(bits >> 32);
1915 __ mov(ip, Operand(lo));
1916 __ mov(scratch0(), Operand(hi));
1917 __ vmov(result, ip, scratch0());
1921 double v = instr->value();
1922 __ Vmov(result, v, scratch0());
1926 void LCodeGen::DoConstantE(LConstantE* instr) {
1927 __ mov(ToRegister(instr->result()), Operand(instr->value()));
1931 void LCodeGen::DoConstantT(LConstantT* instr) {
1932 Handle<Object> object = instr->value(isolate());
1933 AllowDeferredHandleDereference smi_check;
1934 __ Move(ToRegister(instr->result()), object);
1938 void LCodeGen::DoMapEnumLength(LMapEnumLength* instr) {
1939 Register result = ToRegister(instr->result());
1940 Register map = ToRegister(instr->value());
1941 __ EnumLength(result, map);
1945 void LCodeGen::DoDateField(LDateField* instr) {
1946 Register object = ToRegister(instr->date());
1947 Register result = ToRegister(instr->result());
1948 Register scratch = ToRegister(instr->temp());
1949 Smi* index = instr->index();
1950 Label runtime, done;
1951 DCHECK(object.is(result));
1952 DCHECK(object.is(r0));
1953 DCHECK(!scratch.is(scratch0()));
1954 DCHECK(!scratch.is(object));
1957 DeoptimizeIf(eq, instr, Deoptimizer::kSmi);
1958 __ CompareObjectType(object, scratch, scratch, JS_DATE_TYPE);
1959 DeoptimizeIf(ne, instr, Deoptimizer::kNotADateObject);
1961 if (index->value() == 0) {
1962 __ ldr(result, FieldMemOperand(object, JSDate::kValueOffset));
1964 if (index->value() < JSDate::kFirstUncachedField) {
1965 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
1966 __ mov(scratch, Operand(stamp));
1967 __ ldr(scratch, MemOperand(scratch));
1968 __ ldr(scratch0(), FieldMemOperand(object, JSDate::kCacheStampOffset));
1969 __ cmp(scratch, scratch0());
1971 __ ldr(result, FieldMemOperand(object, JSDate::kValueOffset +
1972 kPointerSize * index->value()));
1976 __ PrepareCallCFunction(2, scratch);
1977 __ mov(r1, Operand(index));
1978 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
1984 MemOperand LCodeGen::BuildSeqStringOperand(Register string,
1986 String::Encoding encoding) {
1987 if (index->IsConstantOperand()) {
1988 int offset = ToInteger32(LConstantOperand::cast(index));
1989 if (encoding == String::TWO_BYTE_ENCODING) {
1990 offset *= kUC16Size;
1992 STATIC_ASSERT(kCharSize == 1);
1993 return FieldMemOperand(string, SeqString::kHeaderSize + offset);
1995 Register scratch = scratch0();
1996 DCHECK(!scratch.is(string));
1997 DCHECK(!scratch.is(ToRegister(index)));
1998 if (encoding == String::ONE_BYTE_ENCODING) {
1999 __ add(scratch, string, Operand(ToRegister(index)));
2001 STATIC_ASSERT(kUC16Size == 2);
2002 __ add(scratch, string, Operand(ToRegister(index), LSL, 1));
2004 return FieldMemOperand(scratch, SeqString::kHeaderSize);
2008 void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
2009 String::Encoding encoding = instr->hydrogen()->encoding();
2010 Register string = ToRegister(instr->string());
2011 Register result = ToRegister(instr->result());
2013 if (FLAG_debug_code) {
2014 Register scratch = scratch0();
2015 __ ldr(scratch, FieldMemOperand(string, HeapObject::kMapOffset));
2016 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
2018 __ and_(scratch, scratch,
2019 Operand(kStringRepresentationMask | kStringEncodingMask));
2020 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
2021 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
2022 __ cmp(scratch, Operand(encoding == String::ONE_BYTE_ENCODING
2023 ? one_byte_seq_type : two_byte_seq_type));
2024 __ Check(eq, kUnexpectedStringType);
2027 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
2028 if (encoding == String::ONE_BYTE_ENCODING) {
2029 __ ldrb(result, operand);
2031 __ ldrh(result, operand);
2036 void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
2037 String::Encoding encoding = instr->hydrogen()->encoding();
2038 Register string = ToRegister(instr->string());
2039 Register value = ToRegister(instr->value());
2041 if (FLAG_debug_code) {
2042 Register index = ToRegister(instr->index());
2043 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
2044 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
2046 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
2047 ? one_byte_seq_type : two_byte_seq_type;
2048 __ EmitSeqStringSetCharCheck(string, index, value, encoding_mask);
2051 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
2052 if (encoding == String::ONE_BYTE_ENCODING) {
2053 __ strb(value, operand);
2055 __ strh(value, operand);
2060 void LCodeGen::DoAddI(LAddI* instr) {
2061 LOperand* left = instr->left();
2062 LOperand* right = instr->right();
2063 LOperand* result = instr->result();
2064 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
2065 SBit set_cond = can_overflow ? SetCC : LeaveCC;
2067 if (right->IsStackSlot()) {
2068 Register right_reg = EmitLoadRegister(right, ip);
2069 __ add(ToRegister(result), ToRegister(left), Operand(right_reg), set_cond);
2071 DCHECK(right->IsRegister() || right->IsConstantOperand());
2072 __ add(ToRegister(result), ToRegister(left), ToOperand(right), set_cond);
2076 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
2081 void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
2082 LOperand* left = instr->left();
2083 LOperand* right = instr->right();
2084 HMathMinMax::Operation operation = instr->hydrogen()->operation();
2085 if (instr->hydrogen()->representation().IsSmiOrInteger32()) {
2086 Condition condition = (operation == HMathMinMax::kMathMin) ? le : ge;
2087 Register left_reg = ToRegister(left);
2088 Operand right_op = (right->IsRegister() || right->IsConstantOperand())
2090 : Operand(EmitLoadRegister(right, ip));
2091 Register result_reg = ToRegister(instr->result());
2092 __ cmp(left_reg, right_op);
2093 __ Move(result_reg, left_reg, condition);
2094 __ mov(result_reg, right_op, LeaveCC, NegateCondition(condition));
2096 DCHECK(instr->hydrogen()->representation().IsDouble());
2097 DwVfpRegister left_reg = ToDoubleRegister(left);
2098 DwVfpRegister right_reg = ToDoubleRegister(right);
2099 DwVfpRegister result_reg = ToDoubleRegister(instr->result());
2100 Label result_is_nan, return_left, return_right, check_zero, done;
2101 __ VFPCompareAndSetFlags(left_reg, right_reg);
2102 if (operation == HMathMinMax::kMathMin) {
2103 __ b(mi, &return_left);
2104 __ b(gt, &return_right);
2106 __ b(mi, &return_right);
2107 __ b(gt, &return_left);
2109 __ b(vs, &result_is_nan);
2110 // Left equals right => check for -0.
2111 __ VFPCompareAndSetFlags(left_reg, 0.0);
2112 if (left_reg.is(result_reg) || right_reg.is(result_reg)) {
2113 __ b(ne, &done); // left == right != 0.
2115 __ b(ne, &return_left); // left == right != 0.
2117 // At this point, both left and right are either 0 or -0.
2118 if (operation == HMathMinMax::kMathMin) {
2119 // We could use a single 'vorr' instruction here if we had NEON support.
2120 __ vneg(left_reg, left_reg);
2121 __ vsub(result_reg, left_reg, right_reg);
2122 __ vneg(result_reg, result_reg);
2124 // Since we operate on +0 and/or -0, vadd and vand have the same effect;
2125 // the decision for vadd is easy because vand is a NEON instruction.
2126 __ vadd(result_reg, left_reg, right_reg);
2130 __ bind(&result_is_nan);
2131 __ vadd(result_reg, left_reg, right_reg);
2134 __ bind(&return_right);
2135 __ Move(result_reg, right_reg);
2136 if (!left_reg.is(result_reg)) {
2140 __ bind(&return_left);
2141 __ Move(result_reg, left_reg);
2148 void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
2149 DwVfpRegister left = ToDoubleRegister(instr->left());
2150 DwVfpRegister right = ToDoubleRegister(instr->right());
2151 DwVfpRegister result = ToDoubleRegister(instr->result());
2152 switch (instr->op()) {
2154 __ vadd(result, left, right);
2157 __ vsub(result, left, right);
2160 __ vmul(result, left, right);
2163 __ vdiv(result, left, right);
2166 __ PrepareCallCFunction(0, 2, scratch0());
2167 __ MovToFloatParameters(left, right);
2169 ExternalReference::mod_two_doubles_operation(isolate()),
2171 // Move the result in the double result register.
2172 __ MovFromFloatResult(result);
2182 void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
2183 DCHECK(ToRegister(instr->context()).is(cp));
2184 DCHECK(ToRegister(instr->left()).is(r1));
2185 DCHECK(ToRegister(instr->right()).is(r0));
2186 DCHECK(ToRegister(instr->result()).is(r0));
2188 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), instr->op()).code();
2189 // Block literal pool emission to ensure nop indicating no inlined smi code
2190 // is in the correct position.
2191 Assembler::BlockConstPoolScope block_const_pool(masm());
2192 CallCode(code, RelocInfo::CODE_TARGET, instr);
2196 template<class InstrType>
2197 void LCodeGen::EmitBranch(InstrType instr, Condition condition) {
2198 int left_block = instr->TrueDestination(chunk_);
2199 int right_block = instr->FalseDestination(chunk_);
2201 int next_block = GetNextEmittedBlock();
2203 if (right_block == left_block || condition == al) {
2204 EmitGoto(left_block);
2205 } else if (left_block == next_block) {
2206 __ b(NegateCondition(condition), chunk_->GetAssemblyLabel(right_block));
2207 } else if (right_block == next_block) {
2208 __ b(condition, chunk_->GetAssemblyLabel(left_block));
2210 __ b(condition, chunk_->GetAssemblyLabel(left_block));
2211 __ b(chunk_->GetAssemblyLabel(right_block));
2216 template<class InstrType>
2217 void LCodeGen::EmitFalseBranch(InstrType instr, Condition condition) {
2218 int false_block = instr->FalseDestination(chunk_);
2219 __ b(condition, chunk_->GetAssemblyLabel(false_block));
2223 void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
2228 void LCodeGen::DoBranch(LBranch* instr) {
2229 Representation r = instr->hydrogen()->value()->representation();
2230 if (r.IsInteger32() || r.IsSmi()) {
2231 DCHECK(!info()->IsStub());
2232 Register reg = ToRegister(instr->value());
2233 __ cmp(reg, Operand::Zero());
2234 EmitBranch(instr, ne);
2235 } else if (r.IsDouble()) {
2236 DCHECK(!info()->IsStub());
2237 DwVfpRegister reg = ToDoubleRegister(instr->value());
2238 // Test the double value. Zero and NaN are false.
2239 __ VFPCompareAndSetFlags(reg, 0.0);
2240 __ cmp(r0, r0, vs); // If NaN, set the Z flag. (NaN -> false)
2241 EmitBranch(instr, ne);
2243 DCHECK(r.IsTagged());
2244 Register reg = ToRegister(instr->value());
2245 HType type = instr->hydrogen()->value()->type();
2246 if (type.IsBoolean()) {
2247 DCHECK(!info()->IsStub());
2248 __ CompareRoot(reg, Heap::kTrueValueRootIndex);
2249 EmitBranch(instr, eq);
2250 } else if (type.IsSmi()) {
2251 DCHECK(!info()->IsStub());
2252 __ cmp(reg, Operand::Zero());
2253 EmitBranch(instr, ne);
2254 } else if (type.IsJSArray()) {
2255 DCHECK(!info()->IsStub());
2256 EmitBranch(instr, al);
2257 } else if (type.IsHeapNumber()) {
2258 DCHECK(!info()->IsStub());
2259 DwVfpRegister dbl_scratch = double_scratch0();
2260 __ vldr(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
2261 // Test the double value. Zero and NaN are false.
2262 __ VFPCompareAndSetFlags(dbl_scratch, 0.0);
2263 __ cmp(r0, r0, vs); // If NaN, set the Z flag. (NaN)
2264 EmitBranch(instr, ne);
2265 } else if (type.IsString()) {
2266 DCHECK(!info()->IsStub());
2267 __ ldr(ip, FieldMemOperand(reg, String::kLengthOffset));
2268 __ cmp(ip, Operand::Zero());
2269 EmitBranch(instr, ne);
2271 ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
2272 // Avoid deopts in the case where we've never executed this path before.
2273 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
2275 if (expected.Contains(ToBooleanStub::UNDEFINED)) {
2276 // undefined -> false.
2277 __ CompareRoot(reg, Heap::kUndefinedValueRootIndex);
2278 __ b(eq, instr->FalseLabel(chunk_));
2280 if (expected.Contains(ToBooleanStub::BOOLEAN)) {
2281 // Boolean -> its value.
2282 __ CompareRoot(reg, Heap::kTrueValueRootIndex);
2283 __ b(eq, instr->TrueLabel(chunk_));
2284 __ CompareRoot(reg, Heap::kFalseValueRootIndex);
2285 __ b(eq, instr->FalseLabel(chunk_));
2287 if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
2289 __ CompareRoot(reg, Heap::kNullValueRootIndex);
2290 __ b(eq, instr->FalseLabel(chunk_));
2293 if (expected.Contains(ToBooleanStub::SMI)) {
2294 // Smis: 0 -> false, all other -> true.
2295 __ cmp(reg, Operand::Zero());
2296 __ b(eq, instr->FalseLabel(chunk_));
2297 __ JumpIfSmi(reg, instr->TrueLabel(chunk_));
2298 } else if (expected.NeedsMap()) {
2299 // If we need a map later and have a Smi -> deopt.
2301 DeoptimizeIf(eq, instr, Deoptimizer::kSmi);
2304 const Register map = scratch0();
2305 if (expected.NeedsMap()) {
2306 __ ldr(map, FieldMemOperand(reg, HeapObject::kMapOffset));
2308 if (expected.CanBeUndetectable()) {
2309 // Undetectable -> false.
2310 __ ldrb(ip, FieldMemOperand(map, Map::kBitFieldOffset));
2311 __ tst(ip, Operand(1 << Map::kIsUndetectable));
2312 __ b(ne, instr->FalseLabel(chunk_));
2316 if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
2317 // spec object -> true.
2318 __ CompareInstanceType(map, ip, FIRST_SPEC_OBJECT_TYPE);
2319 __ b(ge, instr->TrueLabel(chunk_));
2322 if (expected.Contains(ToBooleanStub::STRING)) {
2323 // String value -> false iff empty.
2325 __ CompareInstanceType(map, ip, FIRST_NONSTRING_TYPE);
2326 __ b(ge, ¬_string);
2327 __ ldr(ip, FieldMemOperand(reg, String::kLengthOffset));
2328 __ cmp(ip, Operand::Zero());
2329 __ b(ne, instr->TrueLabel(chunk_));
2330 __ b(instr->FalseLabel(chunk_));
2331 __ bind(¬_string);
2334 if (expected.Contains(ToBooleanStub::SYMBOL)) {
2335 // Symbol value -> true.
2336 __ CompareInstanceType(map, ip, SYMBOL_TYPE);
2337 __ b(eq, instr->TrueLabel(chunk_));
2340 if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
2341 // heap number -> false iff +0, -0, or NaN.
2342 DwVfpRegister dbl_scratch = double_scratch0();
2343 Label not_heap_number;
2344 __ CompareRoot(map, Heap::kHeapNumberMapRootIndex);
2345 __ b(ne, ¬_heap_number);
2346 __ vldr(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
2347 __ VFPCompareAndSetFlags(dbl_scratch, 0.0);
2348 __ cmp(r0, r0, vs); // NaN -> false.
2349 __ b(eq, instr->FalseLabel(chunk_)); // +0, -0 -> false.
2350 __ b(instr->TrueLabel(chunk_));
2351 __ bind(¬_heap_number);
2354 if (!expected.IsGeneric()) {
2355 // We've seen something for the first time -> deopt.
2356 // This can only happen if we are not generic already.
2357 DeoptimizeIf(al, instr, Deoptimizer::kUnexpectedObject);
2364 void LCodeGen::EmitGoto(int block) {
2365 if (!IsNextEmittedBlock(block)) {
2366 __ jmp(chunk_->GetAssemblyLabel(LookupDestination(block)));
2371 void LCodeGen::DoGoto(LGoto* instr) {
2372 EmitGoto(instr->block_id());
2376 Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
2377 Condition cond = kNoCondition;
2380 case Token::EQ_STRICT:
2384 case Token::NE_STRICT:
2388 cond = is_unsigned ? lo : lt;
2391 cond = is_unsigned ? hi : gt;
2394 cond = is_unsigned ? ls : le;
2397 cond = is_unsigned ? hs : ge;
2400 case Token::INSTANCEOF:
2408 void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2409 LOperand* left = instr->left();
2410 LOperand* right = instr->right();
2412 instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2413 instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2414 Condition cond = TokenToCondition(instr->op(), is_unsigned);
2416 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2417 // We can statically evaluate the comparison.
2418 double left_val = ToDouble(LConstantOperand::cast(left));
2419 double right_val = ToDouble(LConstantOperand::cast(right));
2420 int next_block = EvalComparison(instr->op(), left_val, right_val) ?
2421 instr->TrueDestination(chunk_) : instr->FalseDestination(chunk_);
2422 EmitGoto(next_block);
2424 if (instr->is_double()) {
2425 // Compare left and right operands as doubles and load the
2426 // resulting flags into the normal status register.
2427 __ VFPCompareAndSetFlags(ToDoubleRegister(left), ToDoubleRegister(right));
2428 // If a NaN is involved, i.e. the result is unordered (V set),
2429 // jump to false block label.
2430 __ b(vs, instr->FalseLabel(chunk_));
2432 if (right->IsConstantOperand()) {
2433 int32_t value = ToInteger32(LConstantOperand::cast(right));
2434 if (instr->hydrogen_value()->representation().IsSmi()) {
2435 __ cmp(ToRegister(left), Operand(Smi::FromInt(value)));
2437 __ cmp(ToRegister(left), Operand(value));
2439 } else if (left->IsConstantOperand()) {
2440 int32_t value = ToInteger32(LConstantOperand::cast(left));
2441 if (instr->hydrogen_value()->representation().IsSmi()) {
2442 __ cmp(ToRegister(right), Operand(Smi::FromInt(value)));
2444 __ cmp(ToRegister(right), Operand(value));
2446 // We commuted the operands, so commute the condition.
2447 cond = CommuteCondition(cond);
2449 __ cmp(ToRegister(left), ToRegister(right));
2452 EmitBranch(instr, cond);
2457 void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
2458 Register left = ToRegister(instr->left());
2459 Register right = ToRegister(instr->right());
2461 __ cmp(left, Operand(right));
2462 EmitBranch(instr, eq);
2466 void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) {
2467 if (instr->hydrogen()->representation().IsTagged()) {
2468 Register input_reg = ToRegister(instr->object());
2469 __ mov(ip, Operand(factory()->the_hole_value()));
2470 __ cmp(input_reg, ip);
2471 EmitBranch(instr, eq);
2475 DwVfpRegister input_reg = ToDoubleRegister(instr->object());
2476 __ VFPCompareAndSetFlags(input_reg, input_reg);
2477 EmitFalseBranch(instr, vc);
2479 Register scratch = scratch0();
2480 __ VmovHigh(scratch, input_reg);
2481 __ cmp(scratch, Operand(kHoleNanUpper32));
2482 EmitBranch(instr, eq);
2486 void LCodeGen::DoCompareMinusZeroAndBranch(LCompareMinusZeroAndBranch* instr) {
2487 Representation rep = instr->hydrogen()->value()->representation();
2488 DCHECK(!rep.IsInteger32());
2489 Register scratch = ToRegister(instr->temp());
2491 if (rep.IsDouble()) {
2492 DwVfpRegister value = ToDoubleRegister(instr->value());
2493 __ VFPCompareAndSetFlags(value, 0.0);
2494 EmitFalseBranch(instr, ne);
2495 __ VmovHigh(scratch, value);
2496 __ cmp(scratch, Operand(0x80000000));
2498 Register value = ToRegister(instr->value());
2501 Heap::kHeapNumberMapRootIndex,
2502 instr->FalseLabel(chunk()),
2504 __ ldr(scratch, FieldMemOperand(value, HeapNumber::kExponentOffset));
2505 __ ldr(ip, FieldMemOperand(value, HeapNumber::kMantissaOffset));
2506 __ cmp(scratch, Operand(0x80000000));
2507 __ cmp(ip, Operand(0x00000000), eq);
2509 EmitBranch(instr, eq);
2513 Condition LCodeGen::EmitIsObject(Register input,
2515 Label* is_not_object,
2517 Register temp2 = scratch0();
2518 __ JumpIfSmi(input, is_not_object);
2520 __ LoadRoot(temp2, Heap::kNullValueRootIndex);
2521 __ cmp(input, temp2);
2522 __ b(eq, is_object);
2525 __ ldr(temp1, FieldMemOperand(input, HeapObject::kMapOffset));
2526 // Undetectable objects behave like undefined.
2527 __ ldrb(temp2, FieldMemOperand(temp1, Map::kBitFieldOffset));
2528 __ tst(temp2, Operand(1 << Map::kIsUndetectable));
2529 __ b(ne, is_not_object);
2531 // Load instance type and check that it is in object type range.
2532 __ ldrb(temp2, FieldMemOperand(temp1, Map::kInstanceTypeOffset));
2533 __ cmp(temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2534 __ b(lt, is_not_object);
2535 __ cmp(temp2, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
2540 void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) {
2541 Register reg = ToRegister(instr->value());
2542 Register temp1 = ToRegister(instr->temp());
2544 Condition true_cond =
2545 EmitIsObject(reg, temp1,
2546 instr->FalseLabel(chunk_), instr->TrueLabel(chunk_));
2548 EmitBranch(instr, true_cond);
2552 Condition LCodeGen::EmitIsString(Register input,
2554 Label* is_not_string,
2555 SmiCheck check_needed = INLINE_SMI_CHECK) {
2556 if (check_needed == INLINE_SMI_CHECK) {
2557 __ JumpIfSmi(input, is_not_string);
2559 __ CompareObjectType(input, temp1, temp1, FIRST_NONSTRING_TYPE);
2565 void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
2566 Register reg = ToRegister(instr->value());
2567 Register temp1 = ToRegister(instr->temp());
2569 SmiCheck check_needed =
2570 instr->hydrogen()->value()->type().IsHeapObject()
2571 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2572 Condition true_cond =
2573 EmitIsString(reg, temp1, instr->FalseLabel(chunk_), check_needed);
2575 EmitBranch(instr, true_cond);
2579 void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
2580 Register input_reg = EmitLoadRegister(instr->value(), ip);
2581 __ SmiTst(input_reg);
2582 EmitBranch(instr, eq);
2586 void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
2587 Register input = ToRegister(instr->value());
2588 Register temp = ToRegister(instr->temp());
2590 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2591 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2593 __ ldr(temp, FieldMemOperand(input, HeapObject::kMapOffset));
2594 __ ldrb(temp, FieldMemOperand(temp, Map::kBitFieldOffset));
2595 __ tst(temp, Operand(1 << Map::kIsUndetectable));
2596 EmitBranch(instr, ne);
2600 static Condition ComputeCompareCondition(Token::Value op) {
2602 case Token::EQ_STRICT:
2615 return kNoCondition;
2620 void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
2621 DCHECK(ToRegister(instr->context()).is(cp));
2622 Token::Value op = instr->op();
2624 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
2625 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2626 // This instruction also signals no smi code inlined.
2627 __ cmp(r0, Operand::Zero());
2629 Condition condition = ComputeCompareCondition(op);
2631 EmitBranch(instr, condition);
2635 static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
2636 InstanceType from = instr->from();
2637 InstanceType to = instr->to();
2638 if (from == FIRST_TYPE) return to;
2639 DCHECK(from == to || to == LAST_TYPE);
2644 static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
2645 InstanceType from = instr->from();
2646 InstanceType to = instr->to();
2647 if (from == to) return eq;
2648 if (to == LAST_TYPE) return hs;
2649 if (from == FIRST_TYPE) return ls;
2655 void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
2656 Register scratch = scratch0();
2657 Register input = ToRegister(instr->value());
2659 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2660 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2663 __ CompareObjectType(input, scratch, scratch, TestType(instr->hydrogen()));
2664 EmitBranch(instr, BranchCondition(instr->hydrogen()));
2668 void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
2669 Register input = ToRegister(instr->value());
2670 Register result = ToRegister(instr->result());
2672 __ AssertString(input);
2674 __ ldr(result, FieldMemOperand(input, String::kHashFieldOffset));
2675 __ IndexFromHash(result, result);
2679 void LCodeGen::DoHasCachedArrayIndexAndBranch(
2680 LHasCachedArrayIndexAndBranch* instr) {
2681 Register input = ToRegister(instr->value());
2682 Register scratch = scratch0();
2685 FieldMemOperand(input, String::kHashFieldOffset));
2686 __ tst(scratch, Operand(String::kContainsCachedArrayIndexMask));
2687 EmitBranch(instr, eq);
2691 // Branches to a label or falls through with the answer in flags. Trashes
2692 // the temp registers, but not the input.
2693 void LCodeGen::EmitClassOfTest(Label* is_true,
2695 Handle<String>class_name,
2699 DCHECK(!input.is(temp));
2700 DCHECK(!input.is(temp2));
2701 DCHECK(!temp.is(temp2));
2703 __ JumpIfSmi(input, is_false);
2705 if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
2706 // Assuming the following assertions, we can use the same compares to test
2707 // for both being a function type and being in the object type range.
2708 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
2709 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2710 FIRST_SPEC_OBJECT_TYPE + 1);
2711 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2712 LAST_SPEC_OBJECT_TYPE - 1);
2713 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
2714 __ CompareObjectType(input, temp, temp2, FIRST_SPEC_OBJECT_TYPE);
2717 __ cmp(temp2, Operand(LAST_SPEC_OBJECT_TYPE));
2720 // Faster code path to avoid two compares: subtract lower bound from the
2721 // actual type and do a signed compare with the width of the type range.
2722 __ ldr(temp, FieldMemOperand(input, HeapObject::kMapOffset));
2723 __ ldrb(temp2, FieldMemOperand(temp, Map::kInstanceTypeOffset));
2724 __ sub(temp2, temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2725 __ cmp(temp2, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE -
2726 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2730 // Now we are in the FIRST-LAST_NONCALLABLE_SPEC_OBJECT_TYPE range.
2731 // Check if the constructor in the map is a function.
2732 __ ldr(temp, FieldMemOperand(temp, Map::kConstructorOffset));
2734 // Objects with a non-function constructor have class 'Object'.
2735 __ CompareObjectType(temp, temp2, temp2, JS_FUNCTION_TYPE);
2736 if (class_name->IsOneByteEqualTo(STATIC_CHAR_VECTOR("Object"))) {
2742 // temp now contains the constructor function. Grab the
2743 // instance class name from there.
2744 __ ldr(temp, FieldMemOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2745 __ ldr(temp, FieldMemOperand(temp,
2746 SharedFunctionInfo::kInstanceClassNameOffset));
2747 // The class name we are testing against is internalized since it's a literal.
2748 // The name in the constructor is internalized because of the way the context
2749 // is booted. This routine isn't expected to work for random API-created
2750 // classes and it doesn't have to because you can't access it with natives
2751 // syntax. Since both sides are internalized it is sufficient to use an
2752 // identity comparison.
2753 __ cmp(temp, Operand(class_name));
2754 // End with the answer in flags.
2758 void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
2759 Register input = ToRegister(instr->value());
2760 Register temp = scratch0();
2761 Register temp2 = ToRegister(instr->temp());
2762 Handle<String> class_name = instr->hydrogen()->class_name();
2764 EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2765 class_name, input, temp, temp2);
2767 EmitBranch(instr, eq);
2771 void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
2772 Register reg = ToRegister(instr->value());
2773 Register temp = ToRegister(instr->temp());
2775 __ ldr(temp, FieldMemOperand(reg, HeapObject::kMapOffset));
2776 __ cmp(temp, Operand(instr->map()));
2777 EmitBranch(instr, eq);
2781 void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
2782 DCHECK(ToRegister(instr->context()).is(cp));
2783 DCHECK(ToRegister(instr->left()).is(r0)); // Object is in r0.
2784 DCHECK(ToRegister(instr->right()).is(r1)); // Function is in r1.
2786 InstanceofStub stub(isolate(), InstanceofStub::kArgsInRegisters);
2787 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2789 __ cmp(r0, Operand::Zero());
2790 __ mov(r0, Operand(factory()->false_value()), LeaveCC, ne);
2791 __ mov(r0, Operand(factory()->true_value()), LeaveCC, eq);
2795 void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) {
2796 class DeferredInstanceOfKnownGlobal FINAL : public LDeferredCode {
2798 DeferredInstanceOfKnownGlobal(LCodeGen* codegen,
2799 LInstanceOfKnownGlobal* instr)
2800 : LDeferredCode(codegen), instr_(instr) { }
2801 void Generate() OVERRIDE {
2802 codegen()->DoDeferredInstanceOfKnownGlobal(instr_, &map_check_,
2805 LInstruction* instr() OVERRIDE { return instr_; }
2806 Label* map_check() { return &map_check_; }
2807 Label* load_bool() { return &load_bool_; }
2810 LInstanceOfKnownGlobal* instr_;
2815 DeferredInstanceOfKnownGlobal* deferred;
2816 deferred = new(zone()) DeferredInstanceOfKnownGlobal(this, instr);
2818 Label done, false_result;
2819 Register object = ToRegister(instr->value());
2820 Register temp = ToRegister(instr->temp());
2821 Register result = ToRegister(instr->result());
2823 // A Smi is not instance of anything.
2824 __ JumpIfSmi(object, &false_result);
2826 // This is the inlined call site instanceof cache. The two occurences of the
2827 // hole value will be patched to the last map/result pair generated by the
2830 Register map = temp;
2831 __ ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
2833 // Block constant pool emission to ensure the positions of instructions are
2834 // as expected by the patcher. See InstanceofStub::Generate().
2835 Assembler::BlockConstPoolScope block_const_pool(masm());
2836 __ bind(deferred->map_check()); // Label for calculating code patching.
2837 // We use Factory::the_hole_value() on purpose instead of loading from the
2838 // root array to force relocation to be able to later patch with
2840 Handle<Cell> cell = factory()->NewCell(factory()->the_hole_value());
2841 __ mov(ip, Operand(Handle<Object>(cell)));
2842 __ ldr(ip, FieldMemOperand(ip, PropertyCell::kValueOffset));
2843 __ cmp(map, Operand(ip));
2844 __ b(ne, &cache_miss);
2845 __ bind(deferred->load_bool()); // Label for calculating code patching.
2846 // We use Factory::the_hole_value() on purpose instead of loading from the
2847 // root array to force relocation to be able to later patch
2848 // with true or false.
2849 __ mov(result, Operand(factory()->the_hole_value()));
2853 // The inlined call site cache did not match. Check null and string before
2854 // calling the deferred code.
2855 __ bind(&cache_miss);
2856 // Null is not instance of anything.
2857 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2858 __ cmp(object, Operand(ip));
2859 __ b(eq, &false_result);
2861 // String values is not instance of anything.
2862 Condition is_string = masm_->IsObjectStringType(object, temp);
2863 __ b(is_string, &false_result);
2865 // Go to the deferred code.
2866 __ b(deferred->entry());
2868 __ bind(&false_result);
2869 __ LoadRoot(result, Heap::kFalseValueRootIndex);
2871 // Here result has either true or false. Deferred code also produces true or
2873 __ bind(deferred->exit());
2878 void LCodeGen::DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
2881 InstanceofStub::Flags flags = InstanceofStub::kNoFlags;
2882 flags = static_cast<InstanceofStub::Flags>(
2883 flags | InstanceofStub::kArgsInRegisters);
2884 flags = static_cast<InstanceofStub::Flags>(
2885 flags | InstanceofStub::kCallSiteInlineCheck);
2886 flags = static_cast<InstanceofStub::Flags>(
2887 flags | InstanceofStub::kReturnTrueFalseObject);
2888 InstanceofStub stub(isolate(), flags);
2890 PushSafepointRegistersScope scope(this);
2891 LoadContextFromDeferred(instr->context());
2893 __ Move(InstanceofStub::right(), instr->function());
2895 int call_size = CallCodeSize(stub.GetCode(), RelocInfo::CODE_TARGET);
2896 int additional_delta = (call_size / Assembler::kInstrSize) + 4;
2897 // Make sure that code size is predicable, since we use specific constants
2898 // offsets in the code to find embedded values..
2899 PredictableCodeSizeScope predictable(
2900 masm_, (additional_delta + 1) * Assembler::kInstrSize);
2901 // Make sure we don't emit any additional entries in the constant pool before
2902 // the call to ensure that the CallCodeSize() calculated the correct number of
2903 // instructions for the constant pool load.
2905 ConstantPoolUnavailableScope constant_pool_unavailable(masm_);
2906 int map_check_delta =
2907 masm_->InstructionsGeneratedSince(map_check) + additional_delta;
2908 int bool_load_delta =
2909 masm_->InstructionsGeneratedSince(bool_load) + additional_delta;
2910 Label before_push_delta;
2911 __ bind(&before_push_delta);
2912 __ BlockConstPoolFor(additional_delta);
2913 // r5 is used to communicate the offset to the location of the map check.
2914 __ mov(r5, Operand(map_check_delta * kPointerSize));
2915 // r6 is used to communicate the offset to the location of the bool load.
2916 __ mov(r6, Operand(bool_load_delta * kPointerSize));
2917 // The mov above can generate one or two instructions. The delta was
2918 // computed for two instructions, so we need to pad here in case of one
2920 while (masm_->InstructionsGeneratedSince(&before_push_delta) != 4) {
2924 CallCodeGeneric(stub.GetCode(),
2925 RelocInfo::CODE_TARGET,
2927 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
2928 LEnvironment* env = instr->GetDeferredLazyDeoptimizationEnvironment();
2929 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
2930 // Put the result value (r0) into the result register slot and
2931 // restore all registers.
2932 __ StoreToSafepointRegisterSlot(r0, ToRegister(instr->result()));
2936 void LCodeGen::DoCmpT(LCmpT* instr) {
2937 DCHECK(ToRegister(instr->context()).is(cp));
2938 Token::Value op = instr->op();
2940 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
2941 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2942 // This instruction also signals no smi code inlined.
2943 __ cmp(r0, Operand::Zero());
2945 Condition condition = ComputeCompareCondition(op);
2946 __ LoadRoot(ToRegister(instr->result()),
2947 Heap::kTrueValueRootIndex,
2949 __ LoadRoot(ToRegister(instr->result()),
2950 Heap::kFalseValueRootIndex,
2951 NegateCondition(condition));
2955 void LCodeGen::DoReturn(LReturn* instr) {
2956 if (FLAG_trace && info()->IsOptimizing()) {
2957 // Push the return value on the stack as the parameter.
2958 // Runtime::TraceExit returns its parameter in r0. We're leaving the code
2959 // managed by the register allocator and tearing down the frame, it's
2960 // safe to write to the context register.
2962 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2963 __ CallRuntime(Runtime::kTraceExit, 1);
2965 if (info()->saves_caller_doubles()) {
2966 RestoreCallerDoubles();
2968 int no_frame_start = -1;
2969 if (NeedsEagerFrame()) {
2970 no_frame_start = masm_->LeaveFrame(StackFrame::JAVA_SCRIPT);
2972 { ConstantPoolUnavailableScope constant_pool_unavailable(masm());
2973 if (instr->has_constant_parameter_count()) {
2974 int parameter_count = ToInteger32(instr->constant_parameter_count());
2975 int32_t sp_delta = (parameter_count + 1) * kPointerSize;
2976 if (sp_delta != 0) {
2977 __ add(sp, sp, Operand(sp_delta));
2980 DCHECK(info()->IsStub()); // Functions would need to drop one more value.
2981 Register reg = ToRegister(instr->parameter_count());
2982 // The argument count parameter is a smi
2984 __ add(sp, sp, Operand(reg, LSL, kPointerSizeLog2));
2989 if (no_frame_start != -1) {
2990 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
2996 void LCodeGen::DoLoadGlobalCell(LLoadGlobalCell* instr) {
2997 Register result = ToRegister(instr->result());
2998 __ mov(ip, Operand(Handle<Object>(instr->hydrogen()->cell().handle())));
2999 __ ldr(result, FieldMemOperand(ip, Cell::kValueOffset));
3000 if (instr->hydrogen()->RequiresHoleCheck()) {
3001 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3003 DeoptimizeIf(eq, instr, Deoptimizer::kHole);
3009 void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
3010 DCHECK(FLAG_vector_ics);
3011 Register vector_register = ToRegister(instr->temp_vector());
3012 Register slot_register = VectorLoadICDescriptor::SlotRegister();
3013 DCHECK(vector_register.is(VectorLoadICDescriptor::VectorRegister()));
3014 DCHECK(slot_register.is(r0));
3016 AllowDeferredHandleDereference vector_structure_check;
3017 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
3018 __ Move(vector_register, vector);
3019 // No need to allocate this register.
3020 FeedbackVectorICSlot slot = instr->hydrogen()->slot();
3021 int index = vector->GetIndex(slot);
3022 __ mov(slot_register, Operand(Smi::FromInt(index)));
3026 void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
3027 DCHECK(ToRegister(instr->context()).is(cp));
3028 DCHECK(ToRegister(instr->global_object())
3029 .is(LoadDescriptor::ReceiverRegister()));
3030 DCHECK(ToRegister(instr->result()).is(r0));
3032 __ mov(LoadDescriptor::NameRegister(), Operand(instr->name()));
3033 if (FLAG_vector_ics) {
3034 EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
3036 ContextualMode mode = instr->for_typeof() ? NOT_CONTEXTUAL : CONTEXTUAL;
3037 Handle<Code> ic = CodeFactory::LoadICInOptimizedCode(isolate(), mode).code();
3038 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3042 void LCodeGen::DoStoreGlobalCell(LStoreGlobalCell* instr) {
3043 Register value = ToRegister(instr->value());
3044 Register cell = scratch0();
3047 __ mov(cell, Operand(instr->hydrogen()->cell().handle()));
3049 // If the cell we are storing to contains the hole it could have
3050 // been deleted from the property dictionary. In that case, we need
3051 // to update the property details in the property dictionary to mark
3052 // it as no longer deleted.
3053 if (instr->hydrogen()->RequiresHoleCheck()) {
3054 // We use a temp to check the payload (CompareRoot might clobber ip).
3055 Register payload = ToRegister(instr->temp());
3056 __ ldr(payload, FieldMemOperand(cell, Cell::kValueOffset));
3057 __ CompareRoot(payload, Heap::kTheHoleValueRootIndex);
3058 DeoptimizeIf(eq, instr, Deoptimizer::kHole);
3062 __ str(value, FieldMemOperand(cell, Cell::kValueOffset));
3063 // Cells are always rescanned, so no write barrier here.
3067 void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
3068 Register context = ToRegister(instr->context());
3069 Register result = ToRegister(instr->result());
3070 __ ldr(result, ContextOperand(context, instr->slot_index()));
3071 if (instr->hydrogen()->RequiresHoleCheck()) {
3072 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3074 if (instr->hydrogen()->DeoptimizesOnHole()) {
3075 DeoptimizeIf(eq, instr, Deoptimizer::kHole);
3077 __ mov(result, Operand(factory()->undefined_value()), LeaveCC, eq);
3083 void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
3084 Register context = ToRegister(instr->context());
3085 Register value = ToRegister(instr->value());
3086 Register scratch = scratch0();
3087 MemOperand target = ContextOperand(context, instr->slot_index());
3089 Label skip_assignment;
3091 if (instr->hydrogen()->RequiresHoleCheck()) {
3092 __ ldr(scratch, target);
3093 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3094 __ cmp(scratch, ip);
3095 if (instr->hydrogen()->DeoptimizesOnHole()) {
3096 DeoptimizeIf(eq, instr, Deoptimizer::kHole);
3098 __ b(ne, &skip_assignment);
3102 __ str(value, target);
3103 if (instr->hydrogen()->NeedsWriteBarrier()) {
3104 SmiCheck check_needed =
3105 instr->hydrogen()->value()->type().IsHeapObject()
3106 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
3107 __ RecordWriteContextSlot(context,
3111 GetLinkRegisterState(),
3113 EMIT_REMEMBERED_SET,
3117 __ bind(&skip_assignment);
3121 void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
3122 HObjectAccess access = instr->hydrogen()->access();
3123 int offset = access.offset();
3124 Register object = ToRegister(instr->object());
3126 if (access.IsExternalMemory()) {
3127 Register result = ToRegister(instr->result());
3128 MemOperand operand = MemOperand(object, offset);
3129 __ Load(result, operand, access.representation());
3133 if (instr->hydrogen()->representation().IsDouble()) {
3134 DwVfpRegister result = ToDoubleRegister(instr->result());
3135 __ vldr(result, FieldMemOperand(object, offset));
3139 Register result = ToRegister(instr->result());
3140 if (!access.IsInobject()) {
3141 __ ldr(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
3144 MemOperand operand = FieldMemOperand(object, offset);
3145 __ Load(result, operand, access.representation());
3149 void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
3150 DCHECK(ToRegister(instr->context()).is(cp));
3151 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3152 DCHECK(ToRegister(instr->result()).is(r0));
3154 // Name is always in r2.
3155 __ mov(LoadDescriptor::NameRegister(), Operand(instr->name()));
3156 if (FLAG_vector_ics) {
3157 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
3160 CodeFactory::LoadICInOptimizedCode(isolate(), NOT_CONTEXTUAL).code();
3161 CallCode(ic, RelocInfo::CODE_TARGET, instr, NEVER_INLINE_TARGET_ADDRESS);
3165 void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
3166 Register scratch = scratch0();
3167 Register function = ToRegister(instr->function());
3168 Register result = ToRegister(instr->result());
3170 // Get the prototype or initial map from the function.
3172 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
3174 // Check that the function has a prototype or an initial map.
3175 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3177 DeoptimizeIf(eq, instr, Deoptimizer::kHole);
3179 // If the function does not have an initial map, we're done.
3181 __ CompareObjectType(result, scratch, scratch, MAP_TYPE);
3184 // Get the prototype from the initial map.
3185 __ ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
3192 void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
3193 Register result = ToRegister(instr->result());
3194 __ LoadRoot(result, instr->index());
3198 void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
3199 Register arguments = ToRegister(instr->arguments());
3200 Register result = ToRegister(instr->result());
3201 // There are two words between the frame pointer and the last argument.
3202 // Subtracting from length accounts for one of them add one more.
3203 if (instr->length()->IsConstantOperand()) {
3204 int const_length = ToInteger32(LConstantOperand::cast(instr->length()));
3205 if (instr->index()->IsConstantOperand()) {
3206 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3207 int index = (const_length - const_index) + 1;
3208 __ ldr(result, MemOperand(arguments, index * kPointerSize));
3210 Register index = ToRegister(instr->index());
3211 __ rsb(result, index, Operand(const_length + 1));
3212 __ ldr(result, MemOperand(arguments, result, LSL, kPointerSizeLog2));
3214 } else if (instr->index()->IsConstantOperand()) {
3215 Register length = ToRegister(instr->length());
3216 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3217 int loc = const_index - 1;
3219 __ sub(result, length, Operand(loc));
3220 __ ldr(result, MemOperand(arguments, result, LSL, kPointerSizeLog2));
3222 __ ldr(result, MemOperand(arguments, length, LSL, kPointerSizeLog2));
3225 Register length = ToRegister(instr->length());
3226 Register index = ToRegister(instr->index());
3227 __ sub(result, length, index);
3228 __ add(result, result, Operand(1));
3229 __ ldr(result, MemOperand(arguments, result, LSL, kPointerSizeLog2));
3234 void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
3235 Register external_pointer = ToRegister(instr->elements());
3236 Register key = no_reg;
3237 ElementsKind elements_kind = instr->elements_kind();
3238 bool key_is_constant = instr->key()->IsConstantOperand();
3239 int constant_key = 0;
3240 if (key_is_constant) {
3241 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3242 if (constant_key & 0xF0000000) {
3243 Abort(kArrayIndexConstantValueTooBig);
3246 key = ToRegister(instr->key());
3248 int element_size_shift = ElementsKindToShiftSize(elements_kind);
3249 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3250 ? (element_size_shift - kSmiTagSize) : element_size_shift;
3251 int base_offset = instr->base_offset();
3253 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3254 elements_kind == FLOAT32_ELEMENTS ||
3255 elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
3256 elements_kind == FLOAT64_ELEMENTS) {
3257 int base_offset = instr->base_offset();
3258 DwVfpRegister result = ToDoubleRegister(instr->result());
3259 Operand operand = key_is_constant
3260 ? Operand(constant_key << element_size_shift)
3261 : Operand(key, LSL, shift_size);
3262 __ add(scratch0(), external_pointer, operand);
3263 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3264 elements_kind == FLOAT32_ELEMENTS) {
3265 __ vldr(double_scratch0().low(), scratch0(), base_offset);
3266 __ vcvt_f64_f32(result, double_scratch0().low());
3267 } else { // i.e. elements_kind == EXTERNAL_DOUBLE_ELEMENTS
3268 __ vldr(result, scratch0(), base_offset);
3271 Register result = ToRegister(instr->result());
3272 MemOperand mem_operand = PrepareKeyedOperand(
3273 key, external_pointer, key_is_constant, constant_key,
3274 element_size_shift, shift_size, base_offset);
3275 switch (elements_kind) {
3276 case EXTERNAL_INT8_ELEMENTS:
3278 __ ldrsb(result, mem_operand);
3280 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
3281 case EXTERNAL_UINT8_ELEMENTS:
3282 case UINT8_ELEMENTS:
3283 case UINT8_CLAMPED_ELEMENTS:
3284 __ ldrb(result, mem_operand);
3286 case EXTERNAL_INT16_ELEMENTS:
3287 case INT16_ELEMENTS:
3288 __ ldrsh(result, mem_operand);
3290 case EXTERNAL_UINT16_ELEMENTS:
3291 case UINT16_ELEMENTS:
3292 __ ldrh(result, mem_operand);
3294 case EXTERNAL_INT32_ELEMENTS:
3295 case INT32_ELEMENTS:
3296 __ ldr(result, mem_operand);
3298 case EXTERNAL_UINT32_ELEMENTS:
3299 case UINT32_ELEMENTS:
3300 __ ldr(result, mem_operand);
3301 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
3302 __ cmp(result, Operand(0x80000000));
3303 DeoptimizeIf(cs, instr, Deoptimizer::kNegativeValue);
3306 case FLOAT32_ELEMENTS:
3307 case FLOAT64_ELEMENTS:
3308 case EXTERNAL_FLOAT32_ELEMENTS:
3309 case EXTERNAL_FLOAT64_ELEMENTS:
3310 case FAST_HOLEY_DOUBLE_ELEMENTS:
3311 case FAST_HOLEY_ELEMENTS:
3312 case FAST_HOLEY_SMI_ELEMENTS:
3313 case FAST_DOUBLE_ELEMENTS:
3315 case FAST_SMI_ELEMENTS:
3316 case DICTIONARY_ELEMENTS:
3317 case SLOPPY_ARGUMENTS_ELEMENTS:
3325 void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
3326 Register elements = ToRegister(instr->elements());
3327 bool key_is_constant = instr->key()->IsConstantOperand();
3328 Register key = no_reg;
3329 DwVfpRegister result = ToDoubleRegister(instr->result());
3330 Register scratch = scratch0();
3332 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
3334 int base_offset = instr->base_offset();
3335 if (key_is_constant) {
3336 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3337 if (constant_key & 0xF0000000) {
3338 Abort(kArrayIndexConstantValueTooBig);
3340 base_offset += constant_key * kDoubleSize;
3342 __ add(scratch, elements, Operand(base_offset));
3344 if (!key_is_constant) {
3345 key = ToRegister(instr->key());
3346 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3347 ? (element_size_shift - kSmiTagSize) : element_size_shift;
3348 __ add(scratch, scratch, Operand(key, LSL, shift_size));
3351 __ vldr(result, scratch, 0);
3353 if (instr->hydrogen()->RequiresHoleCheck()) {
3354 __ ldr(scratch, MemOperand(scratch, sizeof(kHoleNanLower32)));
3355 __ cmp(scratch, Operand(kHoleNanUpper32));
3356 DeoptimizeIf(eq, instr, Deoptimizer::kHole);
3361 void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
3362 Register elements = ToRegister(instr->elements());
3363 Register result = ToRegister(instr->result());
3364 Register scratch = scratch0();
3365 Register store_base = scratch;
3366 int offset = instr->base_offset();
3368 if (instr->key()->IsConstantOperand()) {
3369 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
3370 offset += ToInteger32(const_operand) * kPointerSize;
3371 store_base = elements;
3373 Register key = ToRegister(instr->key());
3374 // Even though the HLoadKeyed instruction forces the input
3375 // representation for the key to be an integer, the input gets replaced
3376 // during bound check elimination with the index argument to the bounds
3377 // check, which can be tagged, so that case must be handled here, too.
3378 if (instr->hydrogen()->key()->representation().IsSmi()) {
3379 __ add(scratch, elements, Operand::PointerOffsetFromSmiKey(key));
3381 __ add(scratch, elements, Operand(key, LSL, kPointerSizeLog2));
3384 __ ldr(result, MemOperand(store_base, offset));
3386 // Check for the hole value.
3387 if (instr->hydrogen()->RequiresHoleCheck()) {
3388 if (IsFastSmiElementsKind(instr->hydrogen()->elements_kind())) {
3390 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi);
3392 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
3393 __ cmp(result, scratch);
3394 DeoptimizeIf(eq, instr, Deoptimizer::kHole);
3400 void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
3401 if (instr->is_typed_elements()) {
3402 DoLoadKeyedExternalArray(instr);
3403 } else if (instr->hydrogen()->representation().IsDouble()) {
3404 DoLoadKeyedFixedDoubleArray(instr);
3406 DoLoadKeyedFixedArray(instr);
3411 MemOperand LCodeGen::PrepareKeyedOperand(Register key,
3413 bool key_is_constant,
3418 if (key_is_constant) {
3419 return MemOperand(base, (constant_key << element_size) + base_offset);
3422 if (base_offset == 0) {
3423 if (shift_size >= 0) {
3424 return MemOperand(base, key, LSL, shift_size);
3426 DCHECK_EQ(-1, shift_size);
3427 return MemOperand(base, key, LSR, 1);
3431 if (shift_size >= 0) {
3432 __ add(scratch0(), base, Operand(key, LSL, shift_size));
3433 return MemOperand(scratch0(), base_offset);
3435 DCHECK_EQ(-1, shift_size);
3436 __ add(scratch0(), base, Operand(key, ASR, 1));
3437 return MemOperand(scratch0(), base_offset);
3442 void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3443 DCHECK(ToRegister(instr->context()).is(cp));
3444 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3445 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
3447 if (FLAG_vector_ics) {
3448 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
3451 Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(isolate()).code();
3452 CallCode(ic, RelocInfo::CODE_TARGET, instr, NEVER_INLINE_TARGET_ADDRESS);
3456 void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
3457 Register scratch = scratch0();
3458 Register result = ToRegister(instr->result());
3460 if (instr->hydrogen()->from_inlined()) {
3461 __ sub(result, sp, Operand(2 * kPointerSize));
3463 // Check if the calling frame is an arguments adaptor frame.
3464 Label done, adapted;
3465 __ ldr(scratch, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3466 __ ldr(result, MemOperand(scratch, StandardFrameConstants::kContextOffset));
3467 __ cmp(result, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3469 // Result is the frame pointer for the frame if not adapted and for the real
3470 // frame below the adaptor frame if adapted.
3471 __ mov(result, fp, LeaveCC, ne);
3472 __ mov(result, scratch, LeaveCC, eq);
3477 void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
3478 Register elem = ToRegister(instr->elements());
3479 Register result = ToRegister(instr->result());
3483 // If no arguments adaptor frame the number of arguments is fixed.
3485 __ mov(result, Operand(scope()->num_parameters()));
3488 // Arguments adaptor frame present. Get argument length from there.
3489 __ ldr(result, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3491 MemOperand(result, ArgumentsAdaptorFrameConstants::kLengthOffset));
3492 __ SmiUntag(result);
3494 // Argument length is in result register.
3499 void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
3500 Register receiver = ToRegister(instr->receiver());
3501 Register function = ToRegister(instr->function());
3502 Register result = ToRegister(instr->result());
3503 Register scratch = scratch0();
3505 // If the receiver is null or undefined, we have to pass the global
3506 // object as a receiver to normal functions. Values have to be
3507 // passed unchanged to builtins and strict-mode functions.
3508 Label global_object, result_in_receiver;
3510 if (!instr->hydrogen()->known_function()) {
3511 // Do not transform the receiver to object for strict mode
3514 FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
3516 FieldMemOperand(scratch, SharedFunctionInfo::kCompilerHintsOffset));
3517 int mask = 1 << (SharedFunctionInfo::kStrictModeFunction + kSmiTagSize);
3518 __ tst(scratch, Operand(mask));
3519 __ b(ne, &result_in_receiver);
3521 // Do not transform the receiver to object for builtins.
3522 __ tst(scratch, Operand(1 << (SharedFunctionInfo::kNative + kSmiTagSize)));
3523 __ b(ne, &result_in_receiver);
3526 // Normal function. Replace undefined or null with global receiver.
3527 __ LoadRoot(scratch, Heap::kNullValueRootIndex);
3528 __ cmp(receiver, scratch);
3529 __ b(eq, &global_object);
3530 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
3531 __ cmp(receiver, scratch);
3532 __ b(eq, &global_object);
3534 // Deoptimize if the receiver is not a JS object.
3535 __ SmiTst(receiver);
3536 DeoptimizeIf(eq, instr, Deoptimizer::kSmi);
3537 __ CompareObjectType(receiver, scratch, scratch, FIRST_SPEC_OBJECT_TYPE);
3538 DeoptimizeIf(lt, instr, Deoptimizer::kNotAJavaScriptObject);
3540 __ b(&result_in_receiver);
3541 __ bind(&global_object);
3542 __ ldr(result, FieldMemOperand(function, JSFunction::kContextOffset));
3544 ContextOperand(result, Context::GLOBAL_OBJECT_INDEX));
3545 __ ldr(result, FieldMemOperand(result, GlobalObject::kGlobalProxyOffset));
3547 if (result.is(receiver)) {
3548 __ bind(&result_in_receiver);
3552 __ bind(&result_in_receiver);
3553 __ mov(result, receiver);
3554 __ bind(&result_ok);
3559 void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3560 Register receiver = ToRegister(instr->receiver());
3561 Register function = ToRegister(instr->function());
3562 Register length = ToRegister(instr->length());
3563 Register elements = ToRegister(instr->elements());
3564 Register scratch = scratch0();
3565 DCHECK(receiver.is(r0)); // Used for parameter count.
3566 DCHECK(function.is(r1)); // Required by InvokeFunction.
3567 DCHECK(ToRegister(instr->result()).is(r0));
3569 // Copy the arguments to this function possibly from the
3570 // adaptor frame below it.
3571 const uint32_t kArgumentsLimit = 1 * KB;
3572 __ cmp(length, Operand(kArgumentsLimit));
3573 DeoptimizeIf(hi, instr, Deoptimizer::kTooManyArguments);
3575 // Push the receiver and use the register to keep the original
3576 // number of arguments.
3578 __ mov(receiver, length);
3579 // The arguments are at a one pointer size offset from elements.
3580 __ add(elements, elements, Operand(1 * kPointerSize));
3582 // Loop through the arguments pushing them onto the execution
3585 // length is a small non-negative integer, due to the test above.
3586 __ cmp(length, Operand::Zero());
3589 __ ldr(scratch, MemOperand(elements, length, LSL, 2));
3591 __ sub(length, length, Operand(1), SetCC);
3595 DCHECK(instr->HasPointerMap());
3596 LPointerMap* pointers = instr->pointer_map();
3597 SafepointGenerator safepoint_generator(
3598 this, pointers, Safepoint::kLazyDeopt);
3599 // The number of arguments is stored in receiver which is r0, as expected
3600 // by InvokeFunction.
3601 ParameterCount actual(receiver);
3602 __ InvokeFunction(function, actual, CALL_FUNCTION, safepoint_generator);
3606 void LCodeGen::DoPushArgument(LPushArgument* instr) {
3607 LOperand* argument = instr->value();
3608 if (argument->IsDoubleRegister() || argument->IsDoubleStackSlot()) {
3609 Abort(kDoPushArgumentNotImplementedForDoubleType);
3611 Register argument_reg = EmitLoadRegister(argument, ip);
3612 __ push(argument_reg);
3617 void LCodeGen::DoDrop(LDrop* instr) {
3618 __ Drop(instr->count());
3622 void LCodeGen::DoThisFunction(LThisFunction* instr) {
3623 Register result = ToRegister(instr->result());
3624 __ ldr(result, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3628 void LCodeGen::DoContext(LContext* instr) {
3629 // If there is a non-return use, the context must be moved to a register.
3630 Register result = ToRegister(instr->result());
3631 if (info()->IsOptimizing()) {
3632 __ ldr(result, MemOperand(fp, StandardFrameConstants::kContextOffset));
3634 // If there is no frame, the context must be in cp.
3635 DCHECK(result.is(cp));
3640 void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
3641 DCHECK(ToRegister(instr->context()).is(cp));
3642 __ push(cp); // The context is the first argument.
3643 __ Move(scratch0(), instr->hydrogen()->pairs());
3644 __ push(scratch0());
3645 __ mov(scratch0(), Operand(Smi::FromInt(instr->hydrogen()->flags())));
3646 __ push(scratch0());
3647 CallRuntime(Runtime::kDeclareGlobals, 3, instr);
3651 void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
3652 int formal_parameter_count, int arity,
3653 LInstruction* instr) {
3654 bool dont_adapt_arguments =
3655 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3656 bool can_invoke_directly =
3657 dont_adapt_arguments || formal_parameter_count == arity;
3659 Register function_reg = r1;
3661 LPointerMap* pointers = instr->pointer_map();
3663 if (can_invoke_directly) {
3665 __ ldr(cp, FieldMemOperand(function_reg, JSFunction::kContextOffset));
3667 // Set r0 to arguments count if adaption is not needed. Assumes that r0
3668 // is available to write to at this point.
3669 if (dont_adapt_arguments) {
3670 __ mov(r0, Operand(arity));
3674 __ ldr(ip, FieldMemOperand(function_reg, JSFunction::kCodeEntryOffset));
3677 // Set up deoptimization.
3678 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3680 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3681 ParameterCount count(arity);
3682 ParameterCount expected(formal_parameter_count);
3683 __ InvokeFunction(function_reg, expected, count, CALL_FUNCTION, generator);
3688 void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3689 DCHECK(instr->context() != NULL);
3690 DCHECK(ToRegister(instr->context()).is(cp));
3691 Register input = ToRegister(instr->value());
3692 Register result = ToRegister(instr->result());
3693 Register scratch = scratch0();
3695 // Deoptimize if not a heap number.
3696 __ ldr(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
3697 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
3698 __ cmp(scratch, Operand(ip));
3699 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber);
3702 Register exponent = scratch0();
3704 __ ldr(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3705 // Check the sign of the argument. If the argument is positive, just
3707 __ tst(exponent, Operand(HeapNumber::kSignMask));
3708 // Move the input to the result if necessary.
3709 __ Move(result, input);
3712 // Input is negative. Reverse its sign.
3713 // Preserve the value of all registers.
3715 PushSafepointRegistersScope scope(this);
3717 // Registers were saved at the safepoint, so we can use
3718 // many scratch registers.
3719 Register tmp1 = input.is(r1) ? r0 : r1;
3720 Register tmp2 = input.is(r2) ? r0 : r2;
3721 Register tmp3 = input.is(r3) ? r0 : r3;
3722 Register tmp4 = input.is(r4) ? r0 : r4;
3724 // exponent: floating point exponent value.
3726 Label allocated, slow;
3727 __ LoadRoot(tmp4, Heap::kHeapNumberMapRootIndex);
3728 __ AllocateHeapNumber(tmp1, tmp2, tmp3, tmp4, &slow);
3731 // Slow case: Call the runtime system to do the number allocation.
3734 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr,
3736 // Set the pointer to the new heap number in tmp.
3737 if (!tmp1.is(r0)) __ mov(tmp1, Operand(r0));
3738 // Restore input_reg after call to runtime.
3739 __ LoadFromSafepointRegisterSlot(input, input);
3740 __ ldr(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3742 __ bind(&allocated);
3743 // exponent: floating point exponent value.
3744 // tmp1: allocated heap number.
3745 __ bic(exponent, exponent, Operand(HeapNumber::kSignMask));
3746 __ str(exponent, FieldMemOperand(tmp1, HeapNumber::kExponentOffset));
3747 __ ldr(tmp2, FieldMemOperand(input, HeapNumber::kMantissaOffset));
3748 __ str(tmp2, FieldMemOperand(tmp1, HeapNumber::kMantissaOffset));
3750 __ StoreToSafepointRegisterSlot(tmp1, result);
3757 void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3758 Register input = ToRegister(instr->value());
3759 Register result = ToRegister(instr->result());
3760 __ cmp(input, Operand::Zero());
3761 __ Move(result, input, pl);
3762 // We can make rsb conditional because the previous cmp instruction
3763 // will clear the V (overflow) flag and rsb won't set this flag
3764 // if input is positive.
3765 __ rsb(result, input, Operand::Zero(), SetCC, mi);
3766 // Deoptimize on overflow.
3767 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
3771 void LCodeGen::DoMathAbs(LMathAbs* instr) {
3772 // Class for deferred case.
3773 class DeferredMathAbsTaggedHeapNumber FINAL : public LDeferredCode {
3775 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr)
3776 : LDeferredCode(codegen), instr_(instr) { }
3777 void Generate() OVERRIDE {
3778 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3780 LInstruction* instr() OVERRIDE { return instr_; }
3786 Representation r = instr->hydrogen()->value()->representation();
3788 DwVfpRegister input = ToDoubleRegister(instr->value());
3789 DwVfpRegister result = ToDoubleRegister(instr->result());
3790 __ vabs(result, input);
3791 } else if (r.IsSmiOrInteger32()) {
3792 EmitIntegerMathAbs(instr);
3794 // Representation is tagged.
3795 DeferredMathAbsTaggedHeapNumber* deferred =
3796 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3797 Register input = ToRegister(instr->value());
3799 __ JumpIfNotSmi(input, deferred->entry());
3800 // If smi, handle it directly.
3801 EmitIntegerMathAbs(instr);
3802 __ bind(deferred->exit());
3807 void LCodeGen::DoMathFloor(LMathFloor* instr) {
3808 DwVfpRegister input = ToDoubleRegister(instr->value());
3809 Register result = ToRegister(instr->result());
3810 Register input_high = scratch0();
3813 __ TryInt32Floor(result, input, input_high, double_scratch0(), &done, &exact);
3814 DeoptimizeIf(al, instr, Deoptimizer::kLostPrecisionOrNaN);
3817 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3819 __ cmp(result, Operand::Zero());
3821 __ cmp(input_high, Operand::Zero());
3822 DeoptimizeIf(mi, instr, Deoptimizer::kMinusZero);
3828 void LCodeGen::DoMathRound(LMathRound* instr) {
3829 DwVfpRegister input = ToDoubleRegister(instr->value());
3830 Register result = ToRegister(instr->result());
3831 DwVfpRegister double_scratch1 = ToDoubleRegister(instr->temp());
3832 DwVfpRegister input_plus_dot_five = double_scratch1;
3833 Register input_high = scratch0();
3834 DwVfpRegister dot_five = double_scratch0();
3835 Label convert, done;
3837 __ Vmov(dot_five, 0.5, scratch0());
3838 __ vabs(double_scratch1, input);
3839 __ VFPCompareAndSetFlags(double_scratch1, dot_five);
3840 // If input is in [-0.5, -0], the result is -0.
3841 // If input is in [+0, +0.5[, the result is +0.
3842 // If the input is +0.5, the result is 1.
3843 __ b(hi, &convert); // Out of [-0.5, +0.5].
3844 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3845 __ VmovHigh(input_high, input);
3846 __ cmp(input_high, Operand::Zero());
3848 DeoptimizeIf(mi, instr, Deoptimizer::kMinusZero);
3850 __ VFPCompareAndSetFlags(input, dot_five);
3851 __ mov(result, Operand(1), LeaveCC, eq); // +0.5.
3852 // Remaining cases: [+0, +0.5[ or [-0.5, +0.5[, depending on
3853 // flag kBailoutOnMinusZero.
3854 __ mov(result, Operand::Zero(), LeaveCC, ne);
3858 __ vadd(input_plus_dot_five, input, dot_five);
3859 // Reuse dot_five (double_scratch0) as we no longer need this value.
3860 __ TryInt32Floor(result, input_plus_dot_five, input_high, double_scratch0(),
3862 DeoptimizeIf(al, instr, Deoptimizer::kLostPrecisionOrNaN);
3867 void LCodeGen::DoMathFround(LMathFround* instr) {
3868 DwVfpRegister input_reg = ToDoubleRegister(instr->value());
3869 DwVfpRegister output_reg = ToDoubleRegister(instr->result());
3870 LowDwVfpRegister scratch = double_scratch0();
3871 __ vcvt_f32_f64(scratch.low(), input_reg);
3872 __ vcvt_f64_f32(output_reg, scratch.low());
3876 void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3877 DwVfpRegister input = ToDoubleRegister(instr->value());
3878 DwVfpRegister result = ToDoubleRegister(instr->result());
3879 __ vsqrt(result, input);
3883 void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3884 DwVfpRegister input = ToDoubleRegister(instr->value());
3885 DwVfpRegister result = ToDoubleRegister(instr->result());
3886 DwVfpRegister temp = double_scratch0();
3888 // Note that according to ECMA-262 15.8.2.13:
3889 // Math.pow(-Infinity, 0.5) == Infinity
3890 // Math.sqrt(-Infinity) == NaN
3892 __ vmov(temp, -V8_INFINITY, scratch0());
3893 __ VFPCompareAndSetFlags(input, temp);
3894 __ vneg(result, temp, eq);
3897 // Add +0 to convert -0 to +0.
3898 __ vadd(result, input, kDoubleRegZero);
3899 __ vsqrt(result, result);
3904 void LCodeGen::DoPower(LPower* instr) {
3905 Representation exponent_type = instr->hydrogen()->right()->representation();
3906 // Having marked this as a call, we can use any registers.
3907 // Just make sure that the input/output registers are the expected ones.
3908 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3909 DCHECK(!instr->right()->IsDoubleRegister() ||
3910 ToDoubleRegister(instr->right()).is(d1));
3911 DCHECK(!instr->right()->IsRegister() ||
3912 ToRegister(instr->right()).is(tagged_exponent));
3913 DCHECK(ToDoubleRegister(instr->left()).is(d0));
3914 DCHECK(ToDoubleRegister(instr->result()).is(d2));
3916 if (exponent_type.IsSmi()) {
3917 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3919 } else if (exponent_type.IsTagged()) {
3921 __ JumpIfSmi(tagged_exponent, &no_deopt);
3922 DCHECK(!r6.is(tagged_exponent));
3923 __ ldr(r6, FieldMemOperand(tagged_exponent, HeapObject::kMapOffset));
3924 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
3925 __ cmp(r6, Operand(ip));
3926 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber);
3928 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3930 } else if (exponent_type.IsInteger32()) {
3931 MathPowStub stub(isolate(), MathPowStub::INTEGER);
3934 DCHECK(exponent_type.IsDouble());
3935 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
3941 void LCodeGen::DoMathExp(LMathExp* instr) {
3942 DwVfpRegister input = ToDoubleRegister(instr->value());
3943 DwVfpRegister result = ToDoubleRegister(instr->result());
3944 DwVfpRegister double_scratch1 = ToDoubleRegister(instr->double_temp());
3945 DwVfpRegister double_scratch2 = double_scratch0();
3946 Register temp1 = ToRegister(instr->temp1());
3947 Register temp2 = ToRegister(instr->temp2());
3949 MathExpGenerator::EmitMathExp(
3950 masm(), input, result, double_scratch1, double_scratch2,
3951 temp1, temp2, scratch0());
3955 void LCodeGen::DoMathLog(LMathLog* instr) {
3956 __ PrepareCallCFunction(0, 1, scratch0());
3957 __ MovToFloatParameter(ToDoubleRegister(instr->value()));
3958 __ CallCFunction(ExternalReference::math_log_double_function(isolate()),
3960 __ MovFromFloatResult(ToDoubleRegister(instr->result()));
3964 void LCodeGen::DoMathClz32(LMathClz32* instr) {
3965 Register input = ToRegister(instr->value());
3966 Register result = ToRegister(instr->result());
3967 __ clz(result, input);
3971 void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
3972 DCHECK(ToRegister(instr->context()).is(cp));
3973 DCHECK(ToRegister(instr->function()).is(r1));
3974 DCHECK(instr->HasPointerMap());
3976 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
3977 if (known_function.is_null()) {
3978 LPointerMap* pointers = instr->pointer_map();
3979 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3980 ParameterCount count(instr->arity());
3981 __ InvokeFunction(r1, count, CALL_FUNCTION, generator);
3983 CallKnownFunction(known_function,
3984 instr->hydrogen()->formal_parameter_count(),
3985 instr->arity(), instr);
3990 void LCodeGen::DoTailCallThroughMegamorphicCache(
3991 LTailCallThroughMegamorphicCache* instr) {
3992 Register receiver = ToRegister(instr->receiver());
3993 Register name = ToRegister(instr->name());
3994 DCHECK(receiver.is(LoadDescriptor::ReceiverRegister()));
3995 DCHECK(name.is(LoadDescriptor::NameRegister()));
3996 DCHECK(receiver.is(r1));
3997 DCHECK(name.is(r2));
3998 Register scratch = r4;
3999 Register extra = r5;
4000 Register extra2 = r6;
4001 Register extra3 = r9;
4004 Register slot = FLAG_vector_ics ? ToRegister(instr->slot()) : no_reg;
4005 Register vector = FLAG_vector_ics ? ToRegister(instr->vector()) : no_reg;
4006 DCHECK(!FLAG_vector_ics ||
4007 !AreAliased(slot, vector, scratch, extra, extra2, extra3));
4010 // Important for the tail-call.
4011 bool must_teardown_frame = NeedsEagerFrame();
4013 if (!instr->hydrogen()->is_just_miss()) {
4014 DCHECK(!instr->hydrogen()->is_keyed_load());
4016 // The probe will tail call to a handler if found.
4017 isolate()->stub_cache()->GenerateProbe(
4018 masm(), Code::LOAD_IC, instr->hydrogen()->flags(), must_teardown_frame,
4019 receiver, name, scratch, extra, extra2, extra3);
4022 // Tail call to miss if we ended up here.
4023 if (must_teardown_frame) __ LeaveFrame(StackFrame::INTERNAL);
4024 if (instr->hydrogen()->is_keyed_load()) {
4025 KeyedLoadIC::GenerateMiss(masm());
4027 LoadIC::GenerateMiss(masm());
4032 void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
4033 DCHECK(ToRegister(instr->result()).is(r0));
4035 if (instr->hydrogen()->IsTailCall()) {
4036 if (NeedsEagerFrame()) __ LeaveFrame(StackFrame::INTERNAL);
4038 if (instr->target()->IsConstantOperand()) {
4039 LConstantOperand* target = LConstantOperand::cast(instr->target());
4040 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
4041 __ Jump(code, RelocInfo::CODE_TARGET);
4043 DCHECK(instr->target()->IsRegister());
4044 Register target = ToRegister(instr->target());
4045 // Make sure we don't emit any additional entries in the constant pool
4046 // before the call to ensure that the CallCodeSize() calculated the
4048 // number of instructions for the constant pool load.
4050 ConstantPoolUnavailableScope constant_pool_unavailable(masm_);
4051 __ add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
4056 LPointerMap* pointers = instr->pointer_map();
4057 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
4059 if (instr->target()->IsConstantOperand()) {
4060 LConstantOperand* target = LConstantOperand::cast(instr->target());
4061 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
4062 generator.BeforeCall(__ CallSize(code, RelocInfo::CODE_TARGET));
4063 PlatformInterfaceDescriptor* call_descriptor =
4064 instr->descriptor().platform_specific_descriptor();
4065 __ Call(code, RelocInfo::CODE_TARGET, TypeFeedbackId::None(), al,
4066 call_descriptor->storage_mode());
4068 DCHECK(instr->target()->IsRegister());
4069 Register target = ToRegister(instr->target());
4070 generator.BeforeCall(__ CallSize(target));
4071 // Make sure we don't emit any additional entries in the constant pool
4072 // before the call to ensure that the CallCodeSize() calculated the
4074 // number of instructions for the constant pool load.
4076 ConstantPoolUnavailableScope constant_pool_unavailable(masm_);
4077 __ add(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
4081 generator.AfterCall();
4086 void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
4087 DCHECK(ToRegister(instr->function()).is(r1));
4088 DCHECK(ToRegister(instr->result()).is(r0));
4090 if (instr->hydrogen()->pass_argument_count()) {
4091 __ mov(r0, Operand(instr->arity()));
4095 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
4097 // Load the code entry address
4098 __ ldr(ip, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
4101 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
4105 void LCodeGen::DoCallFunction(LCallFunction* instr) {
4106 DCHECK(ToRegister(instr->context()).is(cp));
4107 DCHECK(ToRegister(instr->function()).is(r1));
4108 DCHECK(ToRegister(instr->result()).is(r0));
4110 int arity = instr->arity();
4111 CallFunctionFlags flags = instr->hydrogen()->function_flags();
4112 if (instr->hydrogen()->HasVectorAndSlot()) {
4113 Register slot_register = ToRegister(instr->temp_slot());
4114 Register vector_register = ToRegister(instr->temp_vector());
4115 DCHECK(slot_register.is(r3));
4116 DCHECK(vector_register.is(r2));
4118 AllowDeferredHandleDereference vector_structure_check;
4119 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
4120 int index = vector->GetIndex(instr->hydrogen()->slot());
4122 __ Move(vector_register, vector);
4123 __ mov(slot_register, Operand(Smi::FromInt(index)));
4125 CallICState::CallType call_type =
4126 (flags & CALL_AS_METHOD) ? CallICState::METHOD : CallICState::FUNCTION;
4129 CodeFactory::CallICInOptimizedCode(isolate(), arity, call_type).code();
4130 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4132 CallFunctionStub stub(isolate(), arity, flags);
4133 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4138 void LCodeGen::DoCallNew(LCallNew* instr) {
4139 DCHECK(ToRegister(instr->context()).is(cp));
4140 DCHECK(ToRegister(instr->constructor()).is(r1));
4141 DCHECK(ToRegister(instr->result()).is(r0));
4143 __ mov(r0, Operand(instr->arity()));
4144 // No cell in r2 for construct type feedback in optimized code
4145 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
4146 CallConstructStub stub(isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
4147 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4151 void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
4152 DCHECK(ToRegister(instr->context()).is(cp));
4153 DCHECK(ToRegister(instr->constructor()).is(r1));
4154 DCHECK(ToRegister(instr->result()).is(r0));
4156 __ mov(r0, Operand(instr->arity()));
4157 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
4158 ElementsKind kind = instr->hydrogen()->elements_kind();
4159 AllocationSiteOverrideMode override_mode =
4160 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
4161 ? DISABLE_ALLOCATION_SITES
4164 if (instr->arity() == 0) {
4165 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
4166 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4167 } else if (instr->arity() == 1) {
4169 if (IsFastPackedElementsKind(kind)) {
4171 // We might need a change here
4172 // look at the first argument
4173 __ ldr(r5, MemOperand(sp, 0));
4174 __ cmp(r5, Operand::Zero());
4175 __ b(eq, &packed_case);
4177 ElementsKind holey_kind = GetHoleyElementsKind(kind);
4178 ArraySingleArgumentConstructorStub stub(isolate(),
4181 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4183 __ bind(&packed_case);
4186 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
4187 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4190 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
4191 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4196 void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
4197 CallRuntime(instr->function(), instr->arity(), instr);
4201 void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
4202 Register function = ToRegister(instr->function());
4203 Register code_object = ToRegister(instr->code_object());
4204 __ add(code_object, code_object, Operand(Code::kHeaderSize - kHeapObjectTag));
4206 FieldMemOperand(function, JSFunction::kCodeEntryOffset));
4210 void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
4211 Register result = ToRegister(instr->result());
4212 Register base = ToRegister(instr->base_object());
4213 if (instr->offset()->IsConstantOperand()) {
4214 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
4215 __ add(result, base, Operand(ToInteger32(offset)));
4217 Register offset = ToRegister(instr->offset());
4218 __ add(result, base, offset);
4223 void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
4224 Representation representation = instr->representation();
4226 Register object = ToRegister(instr->object());
4227 Register scratch = scratch0();
4228 HObjectAccess access = instr->hydrogen()->access();
4229 int offset = access.offset();
4231 if (access.IsExternalMemory()) {
4232 Register value = ToRegister(instr->value());
4233 MemOperand operand = MemOperand(object, offset);
4234 __ Store(value, operand, representation);
4238 __ AssertNotSmi(object);
4240 DCHECK(!representation.IsSmi() ||
4241 !instr->value()->IsConstantOperand() ||
4242 IsSmi(LConstantOperand::cast(instr->value())));
4243 if (representation.IsDouble()) {
4244 DCHECK(access.IsInobject());
4245 DCHECK(!instr->hydrogen()->has_transition());
4246 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4247 DwVfpRegister value = ToDoubleRegister(instr->value());
4248 __ vstr(value, FieldMemOperand(object, offset));
4252 if (instr->hydrogen()->has_transition()) {
4253 Handle<Map> transition = instr->hydrogen()->transition_map();
4254 AddDeprecationDependency(transition);
4255 __ mov(scratch, Operand(transition));
4256 __ str(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
4257 if (instr->hydrogen()->NeedsWriteBarrierForMap()) {
4258 Register temp = ToRegister(instr->temp());
4259 // Update the write barrier for the map field.
4260 __ RecordWriteForMap(object,
4263 GetLinkRegisterState(),
4269 Register value = ToRegister(instr->value());
4270 if (access.IsInobject()) {
4271 MemOperand operand = FieldMemOperand(object, offset);
4272 __ Store(value, operand, representation);
4273 if (instr->hydrogen()->NeedsWriteBarrier()) {
4274 // Update the write barrier for the object for in-object properties.
4275 __ RecordWriteField(object,
4279 GetLinkRegisterState(),
4281 EMIT_REMEMBERED_SET,
4282 instr->hydrogen()->SmiCheckForWriteBarrier(),
4283 instr->hydrogen()->PointersToHereCheckForValue());
4286 __ ldr(scratch, FieldMemOperand(object, JSObject::kPropertiesOffset));
4287 MemOperand operand = FieldMemOperand(scratch, offset);
4288 __ Store(value, operand, representation);
4289 if (instr->hydrogen()->NeedsWriteBarrier()) {
4290 // Update the write barrier for the properties array.
4291 // object is used as a scratch register.
4292 __ RecordWriteField(scratch,
4296 GetLinkRegisterState(),
4298 EMIT_REMEMBERED_SET,
4299 instr->hydrogen()->SmiCheckForWriteBarrier(),
4300 instr->hydrogen()->PointersToHereCheckForValue());
4306 void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
4307 DCHECK(ToRegister(instr->context()).is(cp));
4308 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4309 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4311 __ mov(StoreDescriptor::NameRegister(), Operand(instr->name()));
4312 Handle<Code> ic = StoreIC::initialize_stub(isolate(), instr->language_mode());
4313 CallCode(ic, RelocInfo::CODE_TARGET, instr, NEVER_INLINE_TARGET_ADDRESS);
4317 void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
4318 Condition cc = instr->hydrogen()->allow_equality() ? hi : hs;
4319 if (instr->index()->IsConstantOperand()) {
4320 Operand index = ToOperand(instr->index());
4321 Register length = ToRegister(instr->length());
4322 __ cmp(length, index);
4323 cc = CommuteCondition(cc);
4325 Register index = ToRegister(instr->index());
4326 Operand length = ToOperand(instr->length());
4327 __ cmp(index, length);
4329 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
4331 __ b(NegateCondition(cc), &done);
4332 __ stop("eliminated bounds check failed");
4335 DeoptimizeIf(cc, instr, Deoptimizer::kOutOfBounds);
4340 void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
4341 Register external_pointer = ToRegister(instr->elements());
4342 Register key = no_reg;
4343 ElementsKind elements_kind = instr->elements_kind();
4344 bool key_is_constant = instr->key()->IsConstantOperand();
4345 int constant_key = 0;
4346 if (key_is_constant) {
4347 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4348 if (constant_key & 0xF0000000) {
4349 Abort(kArrayIndexConstantValueTooBig);
4352 key = ToRegister(instr->key());
4354 int element_size_shift = ElementsKindToShiftSize(elements_kind);
4355 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4356 ? (element_size_shift - kSmiTagSize) : element_size_shift;
4357 int base_offset = instr->base_offset();
4359 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4360 elements_kind == FLOAT32_ELEMENTS ||
4361 elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
4362 elements_kind == FLOAT64_ELEMENTS) {
4363 Register address = scratch0();
4364 DwVfpRegister value(ToDoubleRegister(instr->value()));
4365 if (key_is_constant) {
4366 if (constant_key != 0) {
4367 __ add(address, external_pointer,
4368 Operand(constant_key << element_size_shift));
4370 address = external_pointer;
4373 __ add(address, external_pointer, Operand(key, LSL, shift_size));
4375 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4376 elements_kind == FLOAT32_ELEMENTS) {
4377 __ vcvt_f32_f64(double_scratch0().low(), value);
4378 __ vstr(double_scratch0().low(), address, base_offset);
4379 } else { // Storing doubles, not floats.
4380 __ vstr(value, address, base_offset);
4383 Register value(ToRegister(instr->value()));
4384 MemOperand mem_operand = PrepareKeyedOperand(
4385 key, external_pointer, key_is_constant, constant_key,
4386 element_size_shift, shift_size,
4388 switch (elements_kind) {
4389 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
4390 case EXTERNAL_INT8_ELEMENTS:
4391 case EXTERNAL_UINT8_ELEMENTS:
4392 case UINT8_ELEMENTS:
4393 case UINT8_CLAMPED_ELEMENTS:
4395 __ strb(value, mem_operand);
4397 case EXTERNAL_INT16_ELEMENTS:
4398 case EXTERNAL_UINT16_ELEMENTS:
4399 case INT16_ELEMENTS:
4400 case UINT16_ELEMENTS:
4401 __ strh(value, mem_operand);
4403 case EXTERNAL_INT32_ELEMENTS:
4404 case EXTERNAL_UINT32_ELEMENTS:
4405 case INT32_ELEMENTS:
4406 case UINT32_ELEMENTS:
4407 __ str(value, mem_operand);
4409 case FLOAT32_ELEMENTS:
4410 case FLOAT64_ELEMENTS:
4411 case EXTERNAL_FLOAT32_ELEMENTS:
4412 case EXTERNAL_FLOAT64_ELEMENTS:
4413 case FAST_DOUBLE_ELEMENTS:
4415 case FAST_SMI_ELEMENTS:
4416 case FAST_HOLEY_DOUBLE_ELEMENTS:
4417 case FAST_HOLEY_ELEMENTS:
4418 case FAST_HOLEY_SMI_ELEMENTS:
4419 case DICTIONARY_ELEMENTS:
4420 case SLOPPY_ARGUMENTS_ELEMENTS:
4428 void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
4429 DwVfpRegister value = ToDoubleRegister(instr->value());
4430 Register elements = ToRegister(instr->elements());
4431 Register scratch = scratch0();
4432 DwVfpRegister double_scratch = double_scratch0();
4433 bool key_is_constant = instr->key()->IsConstantOperand();
4434 int base_offset = instr->base_offset();
4436 // Calculate the effective address of the slot in the array to store the
4438 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
4439 if (key_is_constant) {
4440 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4441 if (constant_key & 0xF0000000) {
4442 Abort(kArrayIndexConstantValueTooBig);
4444 __ add(scratch, elements,
4445 Operand((constant_key << element_size_shift) + base_offset));
4447 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4448 ? (element_size_shift - kSmiTagSize) : element_size_shift;
4449 __ add(scratch, elements, Operand(base_offset));
4450 __ add(scratch, scratch,
4451 Operand(ToRegister(instr->key()), LSL, shift_size));
4454 if (instr->NeedsCanonicalization()) {
4455 // Force a canonical NaN.
4456 if (masm()->emit_debug_code()) {
4458 __ tst(ip, Operand(kVFPDefaultNaNModeControlBit));
4459 __ Assert(ne, kDefaultNaNModeNotSet);
4461 __ VFPCanonicalizeNaN(double_scratch, value);
4462 __ vstr(double_scratch, scratch, 0);
4464 __ vstr(value, scratch, 0);
4469 void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4470 Register value = ToRegister(instr->value());
4471 Register elements = ToRegister(instr->elements());
4472 Register key = instr->key()->IsRegister() ? ToRegister(instr->key())
4474 Register scratch = scratch0();
4475 Register store_base = scratch;
4476 int offset = instr->base_offset();
4479 if (instr->key()->IsConstantOperand()) {
4480 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4481 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
4482 offset += ToInteger32(const_operand) * kPointerSize;
4483 store_base = elements;
4485 // Even though the HLoadKeyed instruction forces the input
4486 // representation for the key to be an integer, the input gets replaced
4487 // during bound check elimination with the index argument to the bounds
4488 // check, which can be tagged, so that case must be handled here, too.
4489 if (instr->hydrogen()->key()->representation().IsSmi()) {
4490 __ add(scratch, elements, Operand::PointerOffsetFromSmiKey(key));
4492 __ add(scratch, elements, Operand(key, LSL, kPointerSizeLog2));
4495 __ str(value, MemOperand(store_base, offset));
4497 if (instr->hydrogen()->NeedsWriteBarrier()) {
4498 SmiCheck check_needed =
4499 instr->hydrogen()->value()->type().IsHeapObject()
4500 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4501 // Compute address of modified element and store it into key register.
4502 __ add(key, store_base, Operand(offset));
4503 __ RecordWrite(elements,
4506 GetLinkRegisterState(),
4508 EMIT_REMEMBERED_SET,
4510 instr->hydrogen()->PointersToHereCheckForValue());
4515 void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4516 // By cases: external, fast double
4517 if (instr->is_typed_elements()) {
4518 DoStoreKeyedExternalArray(instr);
4519 } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4520 DoStoreKeyedFixedDoubleArray(instr);
4522 DoStoreKeyedFixedArray(instr);
4527 void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4528 DCHECK(ToRegister(instr->context()).is(cp));
4529 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4530 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4531 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4534 CodeFactory::KeyedStoreIC(isolate(), instr->language_mode()).code();
4535 CallCode(ic, RelocInfo::CODE_TARGET, instr, NEVER_INLINE_TARGET_ADDRESS);
4539 void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4540 Register object_reg = ToRegister(instr->object());
4541 Register scratch = scratch0();
4543 Handle<Map> from_map = instr->original_map();
4544 Handle<Map> to_map = instr->transitioned_map();
4545 ElementsKind from_kind = instr->from_kind();
4546 ElementsKind to_kind = instr->to_kind();
4548 Label not_applicable;
4549 __ ldr(scratch, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4550 __ cmp(scratch, Operand(from_map));
4551 __ b(ne, ¬_applicable);
4553 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
4554 Register new_map_reg = ToRegister(instr->new_map_temp());
4555 __ mov(new_map_reg, Operand(to_map));
4556 __ str(new_map_reg, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4558 __ RecordWriteForMap(object_reg,
4561 GetLinkRegisterState(),
4564 DCHECK(ToRegister(instr->context()).is(cp));
4565 DCHECK(object_reg.is(r0));
4566 PushSafepointRegistersScope scope(this);
4567 __ Move(r1, to_map);
4568 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4569 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4571 RecordSafepointWithRegisters(
4572 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
4574 __ bind(¬_applicable);
4578 void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4579 Register object = ToRegister(instr->object());
4580 Register temp = ToRegister(instr->temp());
4581 Label no_memento_found;
4582 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found);
4583 DeoptimizeIf(eq, instr, Deoptimizer::kMementoFound);
4584 __ bind(&no_memento_found);
4588 void LCodeGen::DoStringAdd(LStringAdd* instr) {
4589 DCHECK(ToRegister(instr->context()).is(cp));
4590 DCHECK(ToRegister(instr->left()).is(r1));
4591 DCHECK(ToRegister(instr->right()).is(r0));
4592 StringAddStub stub(isolate(),
4593 instr->hydrogen()->flags(),
4594 instr->hydrogen()->pretenure_flag());
4595 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4599 void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
4600 class DeferredStringCharCodeAt FINAL : public LDeferredCode {
4602 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
4603 : LDeferredCode(codegen), instr_(instr) { }
4604 void Generate() OVERRIDE { codegen()->DoDeferredStringCharCodeAt(instr_); }
4605 LInstruction* instr() OVERRIDE { return instr_; }
4608 LStringCharCodeAt* instr_;
4611 DeferredStringCharCodeAt* deferred =
4612 new(zone()) DeferredStringCharCodeAt(this, instr);
4614 StringCharLoadGenerator::Generate(masm(),
4615 ToRegister(instr->string()),
4616 ToRegister(instr->index()),
4617 ToRegister(instr->result()),
4619 __ bind(deferred->exit());
4623 void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4624 Register string = ToRegister(instr->string());
4625 Register result = ToRegister(instr->result());
4626 Register scratch = scratch0();
4628 // TODO(3095996): Get rid of this. For now, we need to make the
4629 // result register contain a valid pointer because it is already
4630 // contained in the register pointer map.
4631 __ mov(result, Operand::Zero());
4633 PushSafepointRegistersScope scope(this);
4635 // Push the index as a smi. This is safe because of the checks in
4636 // DoStringCharCodeAt above.
4637 if (instr->index()->IsConstantOperand()) {
4638 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
4639 __ mov(scratch, Operand(Smi::FromInt(const_index)));
4642 Register index = ToRegister(instr->index());
4646 CallRuntimeFromDeferred(Runtime::kStringCharCodeAtRT, 2, instr,
4650 __ StoreToSafepointRegisterSlot(r0, result);
4654 void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
4655 class DeferredStringCharFromCode FINAL : public LDeferredCode {
4657 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
4658 : LDeferredCode(codegen), instr_(instr) { }
4659 void Generate() OVERRIDE {
4660 codegen()->DoDeferredStringCharFromCode(instr_);
4662 LInstruction* instr() OVERRIDE { return instr_; }
4665 LStringCharFromCode* instr_;
4668 DeferredStringCharFromCode* deferred =
4669 new(zone()) DeferredStringCharFromCode(this, instr);
4671 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
4672 Register char_code = ToRegister(instr->char_code());
4673 Register result = ToRegister(instr->result());
4674 DCHECK(!char_code.is(result));
4676 __ cmp(char_code, Operand(String::kMaxOneByteCharCode));
4677 __ b(hi, deferred->entry());
4678 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
4679 __ add(result, result, Operand(char_code, LSL, kPointerSizeLog2));
4680 __ ldr(result, FieldMemOperand(result, FixedArray::kHeaderSize));
4681 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4683 __ b(eq, deferred->entry());
4684 __ bind(deferred->exit());
4688 void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4689 Register char_code = ToRegister(instr->char_code());
4690 Register result = ToRegister(instr->result());
4692 // TODO(3095996): Get rid of this. For now, we need to make the
4693 // result register contain a valid pointer because it is already
4694 // contained in the register pointer map.
4695 __ mov(result, Operand::Zero());
4697 PushSafepointRegistersScope scope(this);
4698 __ SmiTag(char_code);
4700 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr, instr->context());
4701 __ StoreToSafepointRegisterSlot(r0, result);
4705 void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
4706 LOperand* input = instr->value();
4707 DCHECK(input->IsRegister() || input->IsStackSlot());
4708 LOperand* output = instr->result();
4709 DCHECK(output->IsDoubleRegister());
4710 SwVfpRegister single_scratch = double_scratch0().low();
4711 if (input->IsStackSlot()) {
4712 Register scratch = scratch0();
4713 __ ldr(scratch, ToMemOperand(input));
4714 __ vmov(single_scratch, scratch);
4716 __ vmov(single_scratch, ToRegister(input));
4718 __ vcvt_f64_s32(ToDoubleRegister(output), single_scratch);
4722 void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4723 LOperand* input = instr->value();
4724 LOperand* output = instr->result();
4726 SwVfpRegister flt_scratch = double_scratch0().low();
4727 __ vmov(flt_scratch, ToRegister(input));
4728 __ vcvt_f64_u32(ToDoubleRegister(output), flt_scratch);
4732 void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
4733 class DeferredNumberTagI FINAL : public LDeferredCode {
4735 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
4736 : LDeferredCode(codegen), instr_(instr) { }
4737 void Generate() OVERRIDE {
4738 codegen()->DoDeferredNumberTagIU(instr_,
4744 LInstruction* instr() OVERRIDE { return instr_; }
4747 LNumberTagI* instr_;
4750 Register src = ToRegister(instr->value());
4751 Register dst = ToRegister(instr->result());
4753 DeferredNumberTagI* deferred = new(zone()) DeferredNumberTagI(this, instr);
4754 __ SmiTag(dst, src, SetCC);
4755 __ b(vs, deferred->entry());
4756 __ bind(deferred->exit());
4760 void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4761 class DeferredNumberTagU FINAL : public LDeferredCode {
4763 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4764 : LDeferredCode(codegen), instr_(instr) { }
4765 void Generate() OVERRIDE {
4766 codegen()->DoDeferredNumberTagIU(instr_,
4772 LInstruction* instr() OVERRIDE { return instr_; }
4775 LNumberTagU* instr_;
4778 Register input = ToRegister(instr->value());
4779 Register result = ToRegister(instr->result());
4781 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4782 __ cmp(input, Operand(Smi::kMaxValue));
4783 __ b(hi, deferred->entry());
4784 __ SmiTag(result, input);
4785 __ bind(deferred->exit());
4789 void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4793 IntegerSignedness signedness) {
4795 Register src = ToRegister(value);
4796 Register dst = ToRegister(instr->result());
4797 Register tmp1 = scratch0();
4798 Register tmp2 = ToRegister(temp1);
4799 Register tmp3 = ToRegister(temp2);
4800 LowDwVfpRegister dbl_scratch = double_scratch0();
4802 if (signedness == SIGNED_INT32) {
4803 // There was overflow, so bits 30 and 31 of the original integer
4804 // disagree. Try to allocate a heap number in new space and store
4805 // the value in there. If that fails, call the runtime system.
4807 __ SmiUntag(src, dst);
4808 __ eor(src, src, Operand(0x80000000));
4810 __ vmov(dbl_scratch.low(), src);
4811 __ vcvt_f64_s32(dbl_scratch, dbl_scratch.low());
4813 __ vmov(dbl_scratch.low(), src);
4814 __ vcvt_f64_u32(dbl_scratch, dbl_scratch.low());
4817 if (FLAG_inline_new) {
4818 __ LoadRoot(tmp3, Heap::kHeapNumberMapRootIndex);
4819 __ AllocateHeapNumber(dst, tmp1, tmp2, tmp3, &slow, DONT_TAG_RESULT);
4823 // Slow case: Call the runtime system to do the number allocation.
4826 // TODO(3095996): Put a valid pointer value in the stack slot where the
4827 // result register is stored, as this register is in the pointer map, but
4828 // contains an integer value.
4829 __ mov(dst, Operand::Zero());
4831 // Preserve the value of all registers.
4832 PushSafepointRegistersScope scope(this);
4834 // NumberTagI and NumberTagD use the context from the frame, rather than
4835 // the environment's HContext or HInlinedContext value.
4836 // They only call Runtime::kAllocateHeapNumber.
4837 // The corresponding HChange instructions are added in a phase that does
4838 // not have easy access to the local context.
4839 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4840 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4841 RecordSafepointWithRegisters(
4842 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4843 __ sub(r0, r0, Operand(kHeapObjectTag));
4844 __ StoreToSafepointRegisterSlot(r0, dst);
4847 // Done. Put the value in dbl_scratch into the value of the allocated heap
4850 __ vstr(dbl_scratch, dst, HeapNumber::kValueOffset);
4851 __ add(dst, dst, Operand(kHeapObjectTag));
4855 void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
4856 class DeferredNumberTagD FINAL : public LDeferredCode {
4858 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4859 : LDeferredCode(codegen), instr_(instr) { }
4860 void Generate() OVERRIDE { codegen()->DoDeferredNumberTagD(instr_); }
4861 LInstruction* instr() OVERRIDE { return instr_; }
4864 LNumberTagD* instr_;
4867 DwVfpRegister input_reg = ToDoubleRegister(instr->value());
4868 Register scratch = scratch0();
4869 Register reg = ToRegister(instr->result());
4870 Register temp1 = ToRegister(instr->temp());
4871 Register temp2 = ToRegister(instr->temp2());
4873 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
4874 if (FLAG_inline_new) {
4875 __ LoadRoot(scratch, Heap::kHeapNumberMapRootIndex);
4876 // We want the untagged address first for performance
4877 __ AllocateHeapNumber(reg, temp1, temp2, scratch, deferred->entry(),
4880 __ jmp(deferred->entry());
4882 __ bind(deferred->exit());
4883 __ vstr(input_reg, reg, HeapNumber::kValueOffset);
4884 // Now that we have finished with the object's real address tag it
4885 __ add(reg, reg, Operand(kHeapObjectTag));
4889 void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4890 // TODO(3095996): Get rid of this. For now, we need to make the
4891 // result register contain a valid pointer because it is already
4892 // contained in the register pointer map.
4893 Register reg = ToRegister(instr->result());
4894 __ mov(reg, Operand::Zero());
4896 PushSafepointRegistersScope scope(this);
4897 // NumberTagI and NumberTagD use the context from the frame, rather than
4898 // the environment's HContext or HInlinedContext value.
4899 // They only call Runtime::kAllocateHeapNumber.
4900 // The corresponding HChange instructions are added in a phase that does
4901 // not have easy access to the local context.
4902 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4903 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4904 RecordSafepointWithRegisters(
4905 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4906 __ sub(r0, r0, Operand(kHeapObjectTag));
4907 __ StoreToSafepointRegisterSlot(r0, reg);
4911 void LCodeGen::DoSmiTag(LSmiTag* instr) {
4912 HChange* hchange = instr->hydrogen();
4913 Register input = ToRegister(instr->value());
4914 Register output = ToRegister(instr->result());
4915 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4916 hchange->value()->CheckFlag(HValue::kUint32)) {
4917 __ tst(input, Operand(0xc0000000));
4918 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow);
4920 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4921 !hchange->value()->CheckFlag(HValue::kUint32)) {
4922 __ SmiTag(output, input, SetCC);
4923 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
4925 __ SmiTag(output, input);
4930 void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4931 Register input = ToRegister(instr->value());
4932 Register result = ToRegister(instr->result());
4933 if (instr->needs_check()) {
4934 STATIC_ASSERT(kHeapObjectTag == 1);
4935 // If the input is a HeapObject, SmiUntag will set the carry flag.
4936 __ SmiUntag(result, input, SetCC);
4937 DeoptimizeIf(cs, instr, Deoptimizer::kNotASmi);
4939 __ SmiUntag(result, input);
4944 void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
4945 DwVfpRegister result_reg,
4946 NumberUntagDMode mode) {
4947 bool can_convert_undefined_to_nan =
4948 instr->hydrogen()->can_convert_undefined_to_nan();
4949 bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
4951 Register scratch = scratch0();
4952 SwVfpRegister flt_scratch = double_scratch0().low();
4953 DCHECK(!result_reg.is(double_scratch0()));
4954 Label convert, load_smi, done;
4955 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4957 __ UntagAndJumpIfSmi(scratch, input_reg, &load_smi);
4958 // Heap number map check.
4959 __ ldr(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
4960 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
4961 __ cmp(scratch, Operand(ip));
4962 if (can_convert_undefined_to_nan) {
4965 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber);
4968 __ vldr(result_reg, input_reg, HeapNumber::kValueOffset - kHeapObjectTag);
4969 if (deoptimize_on_minus_zero) {
4970 __ VmovLow(scratch, result_reg);
4971 __ cmp(scratch, Operand::Zero());
4973 __ VmovHigh(scratch, result_reg);
4974 __ cmp(scratch, Operand(HeapNumber::kSignMask));
4975 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
4978 if (can_convert_undefined_to_nan) {
4980 // Convert undefined (and hole) to NaN.
4981 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4982 __ cmp(input_reg, Operand(ip));
4983 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefined);
4984 __ LoadRoot(scratch, Heap::kNanValueRootIndex);
4985 __ vldr(result_reg, scratch, HeapNumber::kValueOffset - kHeapObjectTag);
4989 __ SmiUntag(scratch, input_reg);
4990 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
4992 // Smi to double register conversion
4994 // scratch: untagged value of input_reg
4995 __ vmov(flt_scratch, scratch);
4996 __ vcvt_f64_s32(result_reg, flt_scratch);
5001 void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) {
5002 Register input_reg = ToRegister(instr->value());
5003 Register scratch1 = scratch0();
5004 Register scratch2 = ToRegister(instr->temp());
5005 LowDwVfpRegister double_scratch = double_scratch0();
5006 DwVfpRegister double_scratch2 = ToDoubleRegister(instr->temp2());
5008 DCHECK(!scratch1.is(input_reg) && !scratch1.is(scratch2));
5009 DCHECK(!scratch2.is(input_reg) && !scratch2.is(scratch1));
5013 // The input was optimistically untagged; revert it.
5014 // The carry flag is set when we reach this deferred code as we just executed
5015 // SmiUntag(heap_object, SetCC)
5016 STATIC_ASSERT(kHeapObjectTag == 1);
5017 __ adc(scratch2, input_reg, Operand(input_reg));
5019 // Heap number map check.
5020 __ ldr(scratch1, FieldMemOperand(scratch2, HeapObject::kMapOffset));
5021 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
5022 __ cmp(scratch1, Operand(ip));
5024 if (instr->truncating()) {
5025 // Performs a truncating conversion of a floating point number as used by
5026 // the JS bitwise operations.
5027 Label no_heap_number, check_bools, check_false;
5028 __ b(ne, &no_heap_number);
5029 __ TruncateHeapNumberToI(input_reg, scratch2);
5032 // Check for Oddballs. Undefined/False is converted to zero and True to one
5033 // for truncating conversions.
5034 __ bind(&no_heap_number);
5035 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
5036 __ cmp(scratch2, Operand(ip));
5037 __ b(ne, &check_bools);
5038 __ mov(input_reg, Operand::Zero());
5041 __ bind(&check_bools);
5042 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
5043 __ cmp(scratch2, Operand(ip));
5044 __ b(ne, &check_false);
5045 __ mov(input_reg, Operand(1));
5048 __ bind(&check_false);
5049 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
5050 __ cmp(scratch2, Operand(ip));
5051 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefinedBoolean);
5052 __ mov(input_reg, Operand::Zero());
5054 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber);
5056 __ sub(ip, scratch2, Operand(kHeapObjectTag));
5057 __ vldr(double_scratch2, ip, HeapNumber::kValueOffset);
5058 __ TryDoubleToInt32Exact(input_reg, double_scratch2, double_scratch);
5059 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN);
5061 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5062 __ cmp(input_reg, Operand::Zero());
5064 __ VmovHigh(scratch1, double_scratch2);
5065 __ tst(scratch1, Operand(HeapNumber::kSignMask));
5066 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero);
5073 void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
5074 class DeferredTaggedToI FINAL : public LDeferredCode {
5076 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
5077 : LDeferredCode(codegen), instr_(instr) { }
5078 void Generate() OVERRIDE { codegen()->DoDeferredTaggedToI(instr_); }
5079 LInstruction* instr() OVERRIDE { return instr_; }
5085 LOperand* input = instr->value();
5086 DCHECK(input->IsRegister());
5087 DCHECK(input->Equals(instr->result()));
5089 Register input_reg = ToRegister(input);
5091 if (instr->hydrogen()->value()->representation().IsSmi()) {
5092 __ SmiUntag(input_reg);
5094 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
5096 // Optimistically untag the input.
5097 // If the input is a HeapObject, SmiUntag will set the carry flag.
5098 __ SmiUntag(input_reg, SetCC);
5099 // Branch to deferred code if the input was tagged.
5100 // The deferred code will take care of restoring the tag.
5101 __ b(cs, deferred->entry());
5102 __ bind(deferred->exit());
5107 void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
5108 LOperand* input = instr->value();
5109 DCHECK(input->IsRegister());
5110 LOperand* result = instr->result();
5111 DCHECK(result->IsDoubleRegister());
5113 Register input_reg = ToRegister(input);
5114 DwVfpRegister result_reg = ToDoubleRegister(result);
5116 HValue* value = instr->hydrogen()->value();
5117 NumberUntagDMode mode = value->representation().IsSmi()
5118 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
5120 EmitNumberUntagD(instr, input_reg, result_reg, mode);
5124 void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
5125 Register result_reg = ToRegister(instr->result());
5126 Register scratch1 = scratch0();
5127 DwVfpRegister double_input = ToDoubleRegister(instr->value());
5128 LowDwVfpRegister double_scratch = double_scratch0();
5130 if (instr->truncating()) {
5131 __ TruncateDoubleToI(result_reg, double_input);
5133 __ TryDoubleToInt32Exact(result_reg, double_input, double_scratch);
5134 // Deoptimize if the input wasn't a int32 (inside a double).
5135 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN);
5136 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5138 __ cmp(result_reg, Operand::Zero());
5140 __ VmovHigh(scratch1, double_input);
5141 __ tst(scratch1, Operand(HeapNumber::kSignMask));
5142 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero);
5149 void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
5150 Register result_reg = ToRegister(instr->result());
5151 Register scratch1 = scratch0();
5152 DwVfpRegister double_input = ToDoubleRegister(instr->value());
5153 LowDwVfpRegister double_scratch = double_scratch0();
5155 if (instr->truncating()) {
5156 __ TruncateDoubleToI(result_reg, double_input);
5158 __ TryDoubleToInt32Exact(result_reg, double_input, double_scratch);
5159 // Deoptimize if the input wasn't a int32 (inside a double).
5160 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN);
5161 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5163 __ cmp(result_reg, Operand::Zero());
5165 __ VmovHigh(scratch1, double_input);
5166 __ tst(scratch1, Operand(HeapNumber::kSignMask));
5167 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero);
5171 __ SmiTag(result_reg, SetCC);
5172 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
5176 void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
5177 LOperand* input = instr->value();
5178 __ SmiTst(ToRegister(input));
5179 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi);
5183 void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
5184 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
5185 LOperand* input = instr->value();
5186 __ SmiTst(ToRegister(input));
5187 DeoptimizeIf(eq, instr, Deoptimizer::kSmi);
5192 void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
5193 Register input = ToRegister(instr->value());
5194 Register scratch = scratch0();
5196 __ ldr(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
5197 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
5199 if (instr->hydrogen()->is_interval_check()) {
5202 instr->hydrogen()->GetCheckInterval(&first, &last);
5204 __ cmp(scratch, Operand(first));
5206 // If there is only one type in the interval check for equality.
5207 if (first == last) {
5208 DeoptimizeIf(ne, instr, Deoptimizer::kWrongInstanceType);
5210 DeoptimizeIf(lo, instr, Deoptimizer::kWrongInstanceType);
5211 // Omit check for the last type.
5212 if (last != LAST_TYPE) {
5213 __ cmp(scratch, Operand(last));
5214 DeoptimizeIf(hi, instr, Deoptimizer::kWrongInstanceType);
5220 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
5222 if (base::bits::IsPowerOfTwo32(mask)) {
5223 DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
5224 __ tst(scratch, Operand(mask));
5225 DeoptimizeIf(tag == 0 ? ne : eq, instr, Deoptimizer::kWrongInstanceType);
5227 __ and_(scratch, scratch, Operand(mask));
5228 __ cmp(scratch, Operand(tag));
5229 DeoptimizeIf(ne, instr, Deoptimizer::kWrongInstanceType);
5235 void LCodeGen::DoCheckValue(LCheckValue* instr) {
5236 Register reg = ToRegister(instr->value());
5237 Handle<HeapObject> object = instr->hydrogen()->object().handle();
5238 AllowDeferredHandleDereference smi_check;
5239 if (isolate()->heap()->InNewSpace(*object)) {
5240 Register reg = ToRegister(instr->value());
5241 Handle<Cell> cell = isolate()->factory()->NewCell(object);
5242 __ mov(ip, Operand(Handle<Object>(cell)));
5243 __ ldr(ip, FieldMemOperand(ip, Cell::kValueOffset));
5246 __ cmp(reg, Operand(object));
5248 DeoptimizeIf(ne, instr, Deoptimizer::kValueMismatch);
5252 void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
5254 PushSafepointRegistersScope scope(this);
5256 __ mov(cp, Operand::Zero());
5257 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
5258 RecordSafepointWithRegisters(
5259 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
5260 __ StoreToSafepointRegisterSlot(r0, scratch0());
5262 __ tst(scratch0(), Operand(kSmiTagMask));
5263 DeoptimizeIf(eq, instr, Deoptimizer::kInstanceMigrationFailed);
5267 void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
5268 class DeferredCheckMaps FINAL : public LDeferredCode {
5270 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
5271 : LDeferredCode(codegen), instr_(instr), object_(object) {
5272 SetExit(check_maps());
5274 void Generate() OVERRIDE {
5275 codegen()->DoDeferredInstanceMigration(instr_, object_);
5277 Label* check_maps() { return &check_maps_; }
5278 LInstruction* instr() OVERRIDE { return instr_; }
5286 if (instr->hydrogen()->IsStabilityCheck()) {
5287 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5288 for (int i = 0; i < maps->size(); ++i) {
5289 AddStabilityDependency(maps->at(i).handle());
5294 Register map_reg = scratch0();
5296 LOperand* input = instr->value();
5297 DCHECK(input->IsRegister());
5298 Register reg = ToRegister(input);
5300 __ ldr(map_reg, FieldMemOperand(reg, HeapObject::kMapOffset));
5302 DeferredCheckMaps* deferred = NULL;
5303 if (instr->hydrogen()->HasMigrationTarget()) {
5304 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5305 __ bind(deferred->check_maps());
5308 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5310 for (int i = 0; i < maps->size() - 1; i++) {
5311 Handle<Map> map = maps->at(i).handle();
5312 __ CompareMap(map_reg, map, &success);
5316 Handle<Map> map = maps->at(maps->size() - 1).handle();
5317 __ CompareMap(map_reg, map, &success);
5318 if (instr->hydrogen()->HasMigrationTarget()) {
5319 __ b(ne, deferred->entry());
5321 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap);
5328 void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
5329 DwVfpRegister value_reg = ToDoubleRegister(instr->unclamped());
5330 Register result_reg = ToRegister(instr->result());
5331 __ ClampDoubleToUint8(result_reg, value_reg, double_scratch0());
5335 void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
5336 Register unclamped_reg = ToRegister(instr->unclamped());
5337 Register result_reg = ToRegister(instr->result());
5338 __ ClampUint8(result_reg, unclamped_reg);
5342 void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
5343 Register scratch = scratch0();
5344 Register input_reg = ToRegister(instr->unclamped());
5345 Register result_reg = ToRegister(instr->result());
5346 DwVfpRegister temp_reg = ToDoubleRegister(instr->temp());
5347 Label is_smi, done, heap_number;
5349 // Both smi and heap number cases are handled.
5350 __ UntagAndJumpIfSmi(result_reg, input_reg, &is_smi);
5352 // Check for heap number
5353 __ ldr(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
5354 __ cmp(scratch, Operand(factory()->heap_number_map()));
5355 __ b(eq, &heap_number);
5357 // Check for undefined. Undefined is converted to zero for clamping
5359 __ cmp(input_reg, Operand(factory()->undefined_value()));
5360 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefined);
5361 __ mov(result_reg, Operand::Zero());
5365 __ bind(&heap_number);
5366 __ vldr(temp_reg, FieldMemOperand(input_reg, HeapNumber::kValueOffset));
5367 __ ClampDoubleToUint8(result_reg, temp_reg, double_scratch0());
5372 __ ClampUint8(result_reg, result_reg);
5378 void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5379 DwVfpRegister value_reg = ToDoubleRegister(instr->value());
5380 Register result_reg = ToRegister(instr->result());
5381 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5382 __ VmovHigh(result_reg, value_reg);
5384 __ VmovLow(result_reg, value_reg);
5389 void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5390 Register hi_reg = ToRegister(instr->hi());
5391 Register lo_reg = ToRegister(instr->lo());
5392 DwVfpRegister result_reg = ToDoubleRegister(instr->result());
5393 __ VmovHigh(result_reg, hi_reg);
5394 __ VmovLow(result_reg, lo_reg);
5398 void LCodeGen::DoAllocate(LAllocate* instr) {
5399 class DeferredAllocate FINAL : public LDeferredCode {
5401 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
5402 : LDeferredCode(codegen), instr_(instr) { }
5403 void Generate() OVERRIDE { codegen()->DoDeferredAllocate(instr_); }
5404 LInstruction* instr() OVERRIDE { return instr_; }
5410 DeferredAllocate* deferred =
5411 new(zone()) DeferredAllocate(this, instr);
5413 Register result = ToRegister(instr->result());
5414 Register scratch = ToRegister(instr->temp1());
5415 Register scratch2 = ToRegister(instr->temp2());
5417 // Allocate memory for the object.
5418 AllocationFlags flags = TAG_OBJECT;
5419 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5420 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5422 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5423 DCHECK(!instr->hydrogen()->IsOldDataSpaceAllocation());
5424 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5425 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_POINTER_SPACE);
5426 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5427 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5428 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_DATA_SPACE);
5431 if (instr->size()->IsConstantOperand()) {
5432 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5433 if (size <= Page::kMaxRegularHeapObjectSize) {
5434 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5436 __ jmp(deferred->entry());
5439 Register size = ToRegister(instr->size());
5440 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5443 __ bind(deferred->exit());
5445 if (instr->hydrogen()->MustPrefillWithFiller()) {
5446 STATIC_ASSERT(kHeapObjectTag == 1);
5447 if (instr->size()->IsConstantOperand()) {
5448 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5449 __ mov(scratch, Operand(size - kHeapObjectTag));
5451 __ sub(scratch, ToRegister(instr->size()), Operand(kHeapObjectTag));
5453 __ mov(scratch2, Operand(isolate()->factory()->one_pointer_filler_map()));
5456 __ sub(scratch, scratch, Operand(kPointerSize), SetCC);
5457 __ str(scratch2, MemOperand(result, scratch));
5463 void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
5464 Register result = ToRegister(instr->result());
5466 // TODO(3095996): Get rid of this. For now, we need to make the
5467 // result register contain a valid pointer because it is already
5468 // contained in the register pointer map.
5469 __ mov(result, Operand(Smi::FromInt(0)));
5471 PushSafepointRegistersScope scope(this);
5472 if (instr->size()->IsRegister()) {
5473 Register size = ToRegister(instr->size());
5474 DCHECK(!size.is(result));
5478 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5479 if (size >= 0 && size <= Smi::kMaxValue) {
5480 __ Push(Smi::FromInt(size));
5482 // We should never get here at runtime => abort
5483 __ stop("invalid allocation size");
5488 int flags = AllocateDoubleAlignFlag::encode(
5489 instr->hydrogen()->MustAllocateDoubleAligned());
5490 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5491 DCHECK(!instr->hydrogen()->IsOldDataSpaceAllocation());
5492 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5493 flags = AllocateTargetSpace::update(flags, OLD_POINTER_SPACE);
5494 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5495 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5496 flags = AllocateTargetSpace::update(flags, OLD_DATA_SPACE);
5498 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5500 __ Push(Smi::FromInt(flags));
5502 CallRuntimeFromDeferred(
5503 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
5504 __ StoreToSafepointRegisterSlot(r0, result);
5508 void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
5509 DCHECK(ToRegister(instr->value()).is(r0));
5511 CallRuntime(Runtime::kToFastProperties, 1, instr);
5515 void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
5516 DCHECK(ToRegister(instr->context()).is(cp));
5518 // Registers will be used as follows:
5519 // r6 = literals array.
5520 // r1 = regexp literal.
5521 // r0 = regexp literal clone.
5522 // r2-5 are used as temporaries.
5523 int literal_offset =
5524 FixedArray::OffsetOfElementAt(instr->hydrogen()->literal_index());
5525 __ Move(r6, instr->hydrogen()->literals());
5526 __ ldr(r1, FieldMemOperand(r6, literal_offset));
5527 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
5529 __ b(ne, &materialized);
5531 // Create regexp literal using runtime function
5532 // Result will be in r0.
5533 __ mov(r5, Operand(Smi::FromInt(instr->hydrogen()->literal_index())));
5534 __ mov(r4, Operand(instr->hydrogen()->pattern()));
5535 __ mov(r3, Operand(instr->hydrogen()->flags()));
5536 __ Push(r6, r5, r4, r3);
5537 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
5540 __ bind(&materialized);
5541 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
5542 Label allocated, runtime_allocate;
5544 __ Allocate(size, r0, r2, r3, &runtime_allocate, TAG_OBJECT);
5547 __ bind(&runtime_allocate);
5548 __ mov(r0, Operand(Smi::FromInt(size)));
5550 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
5553 __ bind(&allocated);
5554 // Copy the content into the newly allocated memory.
5555 __ CopyFields(r0, r1, double_scratch0(), size / kPointerSize);
5559 void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
5560 DCHECK(ToRegister(instr->context()).is(cp));
5561 // Use the fast case closure allocation code that allocates in new
5562 // space for nested functions that don't need literals cloning.
5563 bool pretenure = instr->hydrogen()->pretenure();
5564 if (!pretenure && instr->hydrogen()->has_no_literals()) {
5565 FastNewClosureStub stub(isolate(), instr->hydrogen()->language_mode(),
5566 instr->hydrogen()->kind());
5567 __ mov(r2, Operand(instr->hydrogen()->shared_info()));
5568 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5570 __ mov(r2, Operand(instr->hydrogen()->shared_info()));
5571 __ mov(r1, Operand(pretenure ? factory()->true_value()
5572 : factory()->false_value()));
5573 __ Push(cp, r2, r1);
5574 CallRuntime(Runtime::kNewClosure, 3, instr);
5579 void LCodeGen::DoTypeof(LTypeof* instr) {
5580 Register input = ToRegister(instr->value());
5582 CallRuntime(Runtime::kTypeof, 1, instr);
5586 void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5587 Register input = ToRegister(instr->value());
5589 Condition final_branch_condition = EmitTypeofIs(instr->TrueLabel(chunk_),
5590 instr->FalseLabel(chunk_),
5592 instr->type_literal());
5593 if (final_branch_condition != kNoCondition) {
5594 EmitBranch(instr, final_branch_condition);
5599 Condition LCodeGen::EmitTypeofIs(Label* true_label,
5602 Handle<String> type_name) {
5603 Condition final_branch_condition = kNoCondition;
5604 Register scratch = scratch0();
5605 Factory* factory = isolate()->factory();
5606 if (String::Equals(type_name, factory->number_string())) {
5607 __ JumpIfSmi(input, true_label);
5608 __ ldr(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
5609 __ CompareRoot(scratch, Heap::kHeapNumberMapRootIndex);
5610 final_branch_condition = eq;
5612 } else if (String::Equals(type_name, factory->string_string())) {
5613 __ JumpIfSmi(input, false_label);
5614 __ CompareObjectType(input, scratch, no_reg, FIRST_NONSTRING_TYPE);
5615 __ b(ge, false_label);
5616 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5617 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
5618 final_branch_condition = eq;
5620 } else if (String::Equals(type_name, factory->symbol_string())) {
5621 __ JumpIfSmi(input, false_label);
5622 __ CompareObjectType(input, scratch, no_reg, SYMBOL_TYPE);
5623 final_branch_condition = eq;
5625 } else if (String::Equals(type_name, factory->boolean_string())) {
5626 __ CompareRoot(input, Heap::kTrueValueRootIndex);
5627 __ b(eq, true_label);
5628 __ CompareRoot(input, Heap::kFalseValueRootIndex);
5629 final_branch_condition = eq;
5631 } else if (String::Equals(type_name, factory->undefined_string())) {
5632 __ CompareRoot(input, Heap::kUndefinedValueRootIndex);
5633 __ b(eq, true_label);
5634 __ JumpIfSmi(input, false_label);
5635 // Check for undetectable objects => true.
5636 __ ldr(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
5637 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5638 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
5639 final_branch_condition = ne;
5641 } else if (String::Equals(type_name, factory->function_string())) {
5642 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5643 Register type_reg = scratch;
5644 __ JumpIfSmi(input, false_label);
5645 __ CompareObjectType(input, scratch, type_reg, JS_FUNCTION_TYPE);
5646 __ b(eq, true_label);
5647 __ cmp(type_reg, Operand(JS_FUNCTION_PROXY_TYPE));
5648 final_branch_condition = eq;
5650 } else if (String::Equals(type_name, factory->object_string())) {
5651 Register map = scratch;
5652 __ JumpIfSmi(input, false_label);
5653 __ CompareRoot(input, Heap::kNullValueRootIndex);
5654 __ b(eq, true_label);
5655 __ CheckObjectTypeRange(input,
5657 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE,
5658 LAST_NONCALLABLE_SPEC_OBJECT_TYPE,
5660 // Check for undetectable objects => false.
5661 __ ldrb(scratch, FieldMemOperand(map, Map::kBitFieldOffset));
5662 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
5663 final_branch_condition = eq;
5669 return final_branch_condition;
5673 void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
5674 Register temp1 = ToRegister(instr->temp());
5676 EmitIsConstructCall(temp1, scratch0());
5677 EmitBranch(instr, eq);
5681 void LCodeGen::EmitIsConstructCall(Register temp1, Register temp2) {
5682 DCHECK(!temp1.is(temp2));
5683 // Get the frame pointer for the calling frame.
5684 __ ldr(temp1, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5686 // Skip the arguments adaptor frame if it exists.
5687 __ ldr(temp2, MemOperand(temp1, StandardFrameConstants::kContextOffset));
5688 __ cmp(temp2, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5689 __ ldr(temp1, MemOperand(temp1, StandardFrameConstants::kCallerFPOffset), eq);
5691 // Check the marker in the calling frame.
5692 __ ldr(temp1, MemOperand(temp1, StandardFrameConstants::kMarkerOffset));
5693 __ cmp(temp1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
5697 void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5698 if (!info()->IsStub()) {
5699 // Ensure that we have enough space after the previous lazy-bailout
5700 // instruction for patching the code here.
5701 int current_pc = masm()->pc_offset();
5702 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5703 // Block literal pool emission for duration of padding.
5704 Assembler::BlockConstPoolScope block_const_pool(masm());
5705 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5706 DCHECK_EQ(0, padding_size % Assembler::kInstrSize);
5707 while (padding_size > 0) {
5709 padding_size -= Assembler::kInstrSize;
5713 last_lazy_deopt_pc_ = masm()->pc_offset();
5717 void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
5718 last_lazy_deopt_pc_ = masm()->pc_offset();
5719 DCHECK(instr->HasEnvironment());
5720 LEnvironment* env = instr->environment();
5721 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5722 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5726 void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
5727 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5728 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5729 // needed return address), even though the implementation of LAZY and EAGER is
5730 // now identical. When LAZY is eventually completely folded into EAGER, remove
5731 // the special case below.
5732 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5733 type = Deoptimizer::LAZY;
5736 DeoptimizeIf(al, instr, instr->hydrogen()->reason(), type);
5740 void LCodeGen::DoDummy(LDummy* instr) {
5741 // Nothing to see here, move on!
5745 void LCodeGen::DoDummyUse(LDummyUse* instr) {
5746 // Nothing to see here, move on!
5750 void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
5751 PushSafepointRegistersScope scope(this);
5752 LoadContextFromDeferred(instr->context());
5753 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5754 RecordSafepointWithLazyDeopt(
5755 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
5756 DCHECK(instr->HasEnvironment());
5757 LEnvironment* env = instr->environment();
5758 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5762 void LCodeGen::DoStackCheck(LStackCheck* instr) {
5763 class DeferredStackCheck FINAL : public LDeferredCode {
5765 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5766 : LDeferredCode(codegen), instr_(instr) { }
5767 void Generate() OVERRIDE { codegen()->DoDeferredStackCheck(instr_); }
5768 LInstruction* instr() OVERRIDE { return instr_; }
5771 LStackCheck* instr_;
5774 DCHECK(instr->HasEnvironment());
5775 LEnvironment* env = instr->environment();
5776 // There is no LLazyBailout instruction for stack-checks. We have to
5777 // prepare for lazy deoptimization explicitly here.
5778 if (instr->hydrogen()->is_function_entry()) {
5779 // Perform stack overflow check.
5781 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
5782 __ cmp(sp, Operand(ip));
5784 Handle<Code> stack_check = isolate()->builtins()->StackCheck();
5785 PredictableCodeSizeScope predictable(masm(),
5786 CallCodeSize(stack_check, RelocInfo::CODE_TARGET));
5787 DCHECK(instr->context()->IsRegister());
5788 DCHECK(ToRegister(instr->context()).is(cp));
5789 CallCode(stack_check, RelocInfo::CODE_TARGET, instr);
5792 DCHECK(instr->hydrogen()->is_backwards_branch());
5793 // Perform stack overflow check if this goto needs it before jumping.
5794 DeferredStackCheck* deferred_stack_check =
5795 new(zone()) DeferredStackCheck(this, instr);
5796 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
5797 __ cmp(sp, Operand(ip));
5798 __ b(lo, deferred_stack_check->entry());
5799 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
5800 __ bind(instr->done_label());
5801 deferred_stack_check->SetExit(instr->done_label());
5802 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5803 // Don't record a deoptimization index for the safepoint here.
5804 // This will be done explicitly when emitting call and the safepoint in
5805 // the deferred code.
5810 void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
5811 // This is a pseudo-instruction that ensures that the environment here is
5812 // properly registered for deoptimization and records the assembler's PC
5814 LEnvironment* environment = instr->environment();
5816 // If the environment were already registered, we would have no way of
5817 // backpatching it with the spill slot operands.
5818 DCHECK(!environment->HasBeenRegistered());
5819 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
5821 GenerateOsrPrologue();
5825 void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
5826 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
5828 DeoptimizeIf(eq, instr, Deoptimizer::kUndefined);
5830 Register null_value = r5;
5831 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
5832 __ cmp(r0, null_value);
5833 DeoptimizeIf(eq, instr, Deoptimizer::kNull);
5836 DeoptimizeIf(eq, instr, Deoptimizer::kSmi);
5838 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
5839 __ CompareObjectType(r0, r1, r1, LAST_JS_PROXY_TYPE);
5840 DeoptimizeIf(le, instr, Deoptimizer::kWrongInstanceType);
5842 Label use_cache, call_runtime;
5843 __ CheckEnumCache(null_value, &call_runtime);
5845 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
5848 // Get the set of properties to enumerate.
5849 __ bind(&call_runtime);
5851 CallRuntime(Runtime::kGetPropertyNamesFast, 1, instr);
5853 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
5854 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
5856 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap);
5857 __ bind(&use_cache);
5861 void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5862 Register map = ToRegister(instr->map());
5863 Register result = ToRegister(instr->result());
5864 Label load_cache, done;
5865 __ EnumLength(result, map);
5866 __ cmp(result, Operand(Smi::FromInt(0)));
5867 __ b(ne, &load_cache);
5868 __ mov(result, Operand(isolate()->factory()->empty_fixed_array()));
5871 __ bind(&load_cache);
5872 __ LoadInstanceDescriptors(map, result);
5874 FieldMemOperand(result, DescriptorArray::kEnumCacheOffset));
5876 FieldMemOperand(result, FixedArray::SizeFor(instr->idx())));
5877 __ cmp(result, Operand::Zero());
5878 DeoptimizeIf(eq, instr, Deoptimizer::kNoCache);
5884 void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5885 Register object = ToRegister(instr->value());
5886 Register map = ToRegister(instr->map());
5887 __ ldr(scratch0(), FieldMemOperand(object, HeapObject::kMapOffset));
5888 __ cmp(map, scratch0());
5889 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap);
5893 void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5897 PushSafepointRegistersScope scope(this);
5900 __ mov(cp, Operand::Zero());
5901 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
5902 RecordSafepointWithRegisters(
5903 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
5904 __ StoreToSafepointRegisterSlot(r0, result);
5908 void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
5909 class DeferredLoadMutableDouble FINAL : public LDeferredCode {
5911 DeferredLoadMutableDouble(LCodeGen* codegen,
5912 LLoadFieldByIndex* instr,
5916 : LDeferredCode(codegen),
5922 void Generate() OVERRIDE {
5923 codegen()->DoDeferredLoadMutableDouble(instr_, result_, object_, index_);
5925 LInstruction* instr() OVERRIDE { return instr_; }
5928 LLoadFieldByIndex* instr_;
5934 Register object = ToRegister(instr->object());
5935 Register index = ToRegister(instr->index());
5936 Register result = ToRegister(instr->result());
5937 Register scratch = scratch0();
5939 DeferredLoadMutableDouble* deferred;
5940 deferred = new(zone()) DeferredLoadMutableDouble(
5941 this, instr, result, object, index);
5943 Label out_of_object, done;
5945 __ tst(index, Operand(Smi::FromInt(1)));
5946 __ b(ne, deferred->entry());
5947 __ mov(index, Operand(index, ASR, 1));
5949 __ cmp(index, Operand::Zero());
5950 __ b(lt, &out_of_object);
5952 __ add(scratch, object, Operand::PointerOffsetFromSmiKey(index));
5953 __ ldr(result, FieldMemOperand(scratch, JSObject::kHeaderSize));
5957 __ bind(&out_of_object);
5958 __ ldr(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
5959 // Index is equal to negated out of object property index plus 1.
5960 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize < kPointerSizeLog2);
5961 __ sub(scratch, result, Operand::PointerOffsetFromSmiKey(index));
5962 __ ldr(result, FieldMemOperand(scratch,
5963 FixedArray::kHeaderSize - kPointerSize));
5964 __ bind(deferred->exit());
5969 void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
5970 Register context = ToRegister(instr->context());
5971 __ str(context, MemOperand(fp, StandardFrameConstants::kContextOffset));
5975 void LCodeGen::DoAllocateBlockContext(LAllocateBlockContext* instr) {
5976 Handle<ScopeInfo> scope_info = instr->scope_info();
5977 __ Push(scope_info);
5978 __ push(ToRegister(instr->function()));
5979 CallRuntime(Runtime::kPushBlockContext, 2, instr);
5980 RecordSafepoint(Safepoint::kNoLazyDeopt);
5986 } } // namespace v8::internal