1 // Copyright 2013 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.
9 #include "src/base/bits.h"
10 #include "src/code-factory.h"
11 #include "src/code-stubs.h"
12 #include "src/cpu-profiler.h"
13 #include "src/hydrogen-osr.h"
14 #include "src/ic/ic.h"
15 #include "src/ic/stub-cache.h"
16 #include "src/x64/lithium-codegen-x64.h"
22 // When invoking builtins, we need to record the safepoint in the middle of
23 // the invoke instruction sequence generated by the macro assembler.
24 class SafepointGenerator FINAL : public CallWrapper {
26 SafepointGenerator(LCodeGen* codegen,
27 LPointerMap* pointers,
28 Safepoint::DeoptMode mode)
32 virtual ~SafepointGenerator() {}
34 void BeforeCall(int call_size) const OVERRIDE {}
36 void AfterCall() const OVERRIDE {
37 codegen_->RecordSafepoint(pointers_, deopt_mode_);
42 LPointerMap* pointers_;
43 Safepoint::DeoptMode deopt_mode_;
49 bool LCodeGen::GenerateCode() {
50 LPhase phase("Z_Code generation", chunk());
54 // Open a frame scope to indicate that there is a frame on the stack. The
55 // MANUAL indicates that the scope shouldn't actually generate code to set up
56 // the frame (that is done in GeneratePrologue).
57 FrameScope frame_scope(masm_, StackFrame::MANUAL);
59 return GeneratePrologue() &&
61 GenerateDeferredCode() &&
62 GenerateJumpTable() &&
63 GenerateSafepointTable();
67 void LCodeGen::FinishCode(Handle<Code> code) {
69 code->set_stack_slots(GetStackSlotCount());
70 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
71 PopulateDeoptimizationData(code);
76 void LCodeGen::MakeSureStackPagesMapped(int offset) {
77 const int kPageSize = 4 * KB;
78 for (offset -= kPageSize; offset > 0; offset -= kPageSize) {
79 __ movp(Operand(rsp, offset), rax);
85 void LCodeGen::SaveCallerDoubles() {
86 DCHECK(info()->saves_caller_doubles());
87 DCHECK(NeedsEagerFrame());
88 Comment(";;; Save clobbered callee double registers");
90 BitVector* doubles = chunk()->allocated_double_registers();
91 BitVector::Iterator save_iterator(doubles);
92 while (!save_iterator.Done()) {
93 __ movsd(MemOperand(rsp, count * kDoubleSize),
94 XMMRegister::FromAllocationIndex(save_iterator.Current()));
95 save_iterator.Advance();
101 void LCodeGen::RestoreCallerDoubles() {
102 DCHECK(info()->saves_caller_doubles());
103 DCHECK(NeedsEagerFrame());
104 Comment(";;; Restore clobbered callee double registers");
105 BitVector* doubles = chunk()->allocated_double_registers();
106 BitVector::Iterator save_iterator(doubles);
108 while (!save_iterator.Done()) {
109 __ movsd(XMMRegister::FromAllocationIndex(save_iterator.Current()),
110 MemOperand(rsp, count * kDoubleSize));
111 save_iterator.Advance();
117 bool LCodeGen::GeneratePrologue() {
118 DCHECK(is_generating());
120 if (info()->IsOptimizing()) {
121 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
124 if (strlen(FLAG_stop_at) > 0 &&
125 info_->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
130 // Sloppy mode functions need to replace the receiver with the global proxy
131 // when called as functions (without an explicit receiver object).
132 if (graph()->this_has_uses() && is_sloppy(info_->language_mode()) &&
133 !info_->is_native()) {
135 StackArgumentsAccessor args(rsp, scope()->num_parameters());
136 __ movp(rcx, args.GetReceiverOperand());
138 __ CompareRoot(rcx, Heap::kUndefinedValueRootIndex);
139 __ j(not_equal, &ok, Label::kNear);
141 __ movp(rcx, GlobalObjectOperand());
142 __ movp(rcx, FieldOperand(rcx, GlobalObject::kGlobalProxyOffset));
144 __ movp(args.GetReceiverOperand(), rcx);
150 info()->set_prologue_offset(masm_->pc_offset());
151 if (NeedsEagerFrame()) {
152 DCHECK(!frame_is_built_);
153 frame_is_built_ = true;
154 if (info()->IsStub()) {
157 __ Prologue(info()->IsCodePreAgingActive());
159 info()->AddNoFrameRange(0, masm_->pc_offset());
162 // Reserve space for the stack slots needed by the code.
163 int slots = GetStackSlotCount();
165 if (FLAG_debug_code) {
166 __ subp(rsp, Immediate(slots * kPointerSize));
168 MakeSureStackPagesMapped(slots * kPointerSize);
172 __ Set(kScratchRegister, kSlotsZapValue);
175 __ movp(MemOperand(rsp, rax, times_pointer_size, 0),
178 __ j(not_zero, &loop);
181 __ subp(rsp, Immediate(slots * kPointerSize));
183 MakeSureStackPagesMapped(slots * kPointerSize);
187 if (info()->saves_caller_doubles()) {
192 // Possibly allocate a local context.
193 int heap_slots = info_->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
194 if (heap_slots > 0) {
195 Comment(";;; Allocate local context");
196 bool need_write_barrier = true;
197 // Argument to NewContext is the function, which is still in rdi.
198 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
199 FastNewContextStub stub(isolate(), heap_slots);
201 // Result of FastNewContextStub is always in new space.
202 need_write_barrier = false;
205 __ CallRuntime(Runtime::kNewFunctionContext, 1);
207 RecordSafepoint(Safepoint::kNoLazyDeopt);
208 // Context is returned in rax. It replaces the context passed to us.
209 // It's saved in the stack and kept live in rsi.
211 __ movp(Operand(rbp, StandardFrameConstants::kContextOffset), rax);
213 // Copy any necessary parameters into the context.
214 int num_parameters = scope()->num_parameters();
215 for (int i = 0; i < num_parameters; i++) {
216 Variable* var = scope()->parameter(i);
217 if (var->IsContextSlot()) {
218 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
219 (num_parameters - 1 - i) * kPointerSize;
220 // Load parameter from stack.
221 __ movp(rax, Operand(rbp, parameter_offset));
222 // Store it in the context.
223 int context_offset = Context::SlotOffset(var->index());
224 __ movp(Operand(rsi, context_offset), rax);
225 // Update the write barrier. This clobbers rax and rbx.
226 if (need_write_barrier) {
227 __ RecordWriteContextSlot(rsi, context_offset, rax, rbx, kSaveFPRegs);
228 } else if (FLAG_debug_code) {
230 __ JumpIfInNewSpace(rsi, rax, &done, Label::kNear);
231 __ Abort(kExpectedNewSpaceObject);
236 Comment(";;; End allocate local context");
240 if (FLAG_trace && info()->IsOptimizing()) {
241 __ CallRuntime(Runtime::kTraceEnter, 0);
243 return !is_aborted();
247 void LCodeGen::GenerateOsrPrologue() {
248 // Generate the OSR entry prologue at the first unknown OSR value, or if there
249 // are none, at the OSR entrypoint instruction.
250 if (osr_pc_offset_ >= 0) return;
252 osr_pc_offset_ = masm()->pc_offset();
254 // Adjust the frame size, subsuming the unoptimized frame into the
256 int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
258 __ subp(rsp, Immediate(slots * kPointerSize));
262 void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
263 if (instr->IsCall()) {
264 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
266 if (!instr->IsLazyBailout() && !instr->IsGap()) {
267 safepoints_.BumpLastLazySafepointIndex();
272 void LCodeGen::GenerateBodyInstructionPost(LInstruction* instr) {
273 if (FLAG_debug_code && FLAG_enable_slow_asserts && instr->HasResult() &&
274 instr->hydrogen_value()->representation().IsInteger32() &&
275 instr->result()->IsRegister()) {
276 __ AssertZeroExtended(ToRegister(instr->result()));
279 if (instr->HasResult() && instr->MustSignExtendResult(chunk())) {
280 // We sign extend the dehoisted key at the definition point when the pointer
281 // size is 64-bit. For x32 port, we sign extend the dehoisted key at the use
282 // points and MustSignExtendResult is always false. We can't use
283 // STATIC_ASSERT here as the pointer size is 32-bit for x32.
284 DCHECK(kPointerSize == kInt64Size);
285 if (instr->result()->IsRegister()) {
286 Register result_reg = ToRegister(instr->result());
287 __ movsxlq(result_reg, result_reg);
289 // Sign extend the 32bit result in the stack slots.
290 DCHECK(instr->result()->IsStackSlot());
291 Operand src = ToOperand(instr->result());
292 __ movsxlq(kScratchRegister, src);
293 __ movq(src, kScratchRegister);
299 bool LCodeGen::GenerateJumpTable() {
300 if (jump_table_.length() == 0) return !is_aborted();
303 Comment(";;; -------------------- Jump table --------------------");
304 for (int i = 0; i < jump_table_.length(); i++) {
305 Deoptimizer::JumpTableEntry* table_entry = &jump_table_[i];
306 __ bind(&table_entry->label);
307 Address entry = table_entry->address;
308 DeoptComment(table_entry->deopt_info);
309 if (table_entry->needs_frame) {
310 DCHECK(!info()->saves_caller_doubles());
311 __ Move(kScratchRegister, ExternalReference::ForDeoptEntry(entry));
312 __ call(&needs_frame);
314 if (info()->saves_caller_doubles()) {
315 DCHECK(info()->IsStub());
316 RestoreCallerDoubles();
318 __ call(entry, RelocInfo::RUNTIME_ENTRY);
320 info()->LogDeoptCallPosition(masm()->pc_offset(),
321 table_entry->deopt_info.inlining_id);
324 if (needs_frame.is_linked()) {
325 __ bind(&needs_frame);
327 4: return address <-- rsp
333 // Reserve space for context and stub marker.
334 __ subp(rsp, Immediate(2 * kPointerSize));
335 __ Push(MemOperand(rsp, 2 * kPointerSize)); // Copy return address.
336 __ Push(kScratchRegister); // Save entry address for ret(0)
343 0: entry address <-- rsp
346 // Remember context pointer.
347 __ movp(kScratchRegister,
348 MemOperand(rbp, StandardFrameConstants::kContextOffset));
349 // Save context pointer into the stack frame.
350 __ movp(MemOperand(rsp, 3 * kPointerSize), kScratchRegister);
352 // Create a stack frame.
353 __ movp(MemOperand(rsp, 4 * kPointerSize), rbp);
354 __ leap(rbp, MemOperand(rsp, 4 * kPointerSize));
356 // This variant of deopt can only be used with stubs. Since we don't
357 // have a function pointer to install in the stack frame that we're
358 // building, install a special marker there instead.
359 DCHECK(info()->IsStub());
360 __ Move(MemOperand(rsp, 2 * kPointerSize), Smi::FromInt(StackFrame::STUB));
367 0: entry address <-- rsp
372 return !is_aborted();
376 bool LCodeGen::GenerateDeferredCode() {
377 DCHECK(is_generating());
378 if (deferred_.length() > 0) {
379 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
380 LDeferredCode* code = deferred_[i];
383 instructions_->at(code->instruction_index())->hydrogen_value();
384 RecordAndWritePosition(
385 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
387 Comment(";;; <@%d,#%d> "
388 "-------------------- Deferred %s --------------------",
389 code->instruction_index(),
390 code->instr()->hydrogen_value()->id(),
391 code->instr()->Mnemonic());
392 __ bind(code->entry());
393 if (NeedsDeferredFrame()) {
394 Comment(";;; Build frame");
395 DCHECK(!frame_is_built_);
396 DCHECK(info()->IsStub());
397 frame_is_built_ = true;
398 // Build the frame in such a way that esi isn't trashed.
399 __ pushq(rbp); // Caller's frame pointer.
400 __ Push(Operand(rbp, StandardFrameConstants::kContextOffset));
401 __ Push(Smi::FromInt(StackFrame::STUB));
402 __ leap(rbp, Operand(rsp, 2 * kPointerSize));
403 Comment(";;; Deferred code");
406 if (NeedsDeferredFrame()) {
407 __ bind(code->done());
408 Comment(";;; Destroy frame");
409 DCHECK(frame_is_built_);
410 frame_is_built_ = false;
414 __ jmp(code->exit());
418 // Deferred code is the last part of the instruction sequence. Mark
419 // the generated code as done unless we bailed out.
420 if (!is_aborted()) status_ = DONE;
421 return !is_aborted();
425 bool LCodeGen::GenerateSafepointTable() {
427 safepoints_.Emit(masm(), GetStackSlotCount());
428 return !is_aborted();
432 Register LCodeGen::ToRegister(int index) const {
433 return Register::FromAllocationIndex(index);
437 XMMRegister LCodeGen::ToDoubleRegister(int index) const {
438 return XMMRegister::FromAllocationIndex(index);
442 Register LCodeGen::ToRegister(LOperand* op) const {
443 DCHECK(op->IsRegister());
444 return ToRegister(op->index());
448 XMMRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
449 DCHECK(op->IsDoubleRegister());
450 return ToDoubleRegister(op->index());
454 bool LCodeGen::IsInteger32Constant(LConstantOperand* op) const {
455 return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
459 bool LCodeGen::IsDehoistedKeyConstant(LConstantOperand* op) const {
460 return op->IsConstantOperand() &&
461 chunk_->IsDehoistedKey(chunk_->LookupConstant(op));
465 bool LCodeGen::IsSmiConstant(LConstantOperand* op) const {
466 return chunk_->LookupLiteralRepresentation(op).IsSmi();
470 int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
471 return ToRepresentation(op, Representation::Integer32());
475 int32_t LCodeGen::ToRepresentation(LConstantOperand* op,
476 const Representation& r) const {
477 HConstant* constant = chunk_->LookupConstant(op);
478 int32_t value = constant->Integer32Value();
479 if (r.IsInteger32()) return value;
480 DCHECK(SmiValuesAre31Bits() && r.IsSmiOrTagged());
481 return static_cast<int32_t>(reinterpret_cast<intptr_t>(Smi::FromInt(value)));
485 Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
486 HConstant* constant = chunk_->LookupConstant(op);
487 return Smi::FromInt(constant->Integer32Value());
491 double LCodeGen::ToDouble(LConstantOperand* op) const {
492 HConstant* constant = chunk_->LookupConstant(op);
493 DCHECK(constant->HasDoubleValue());
494 return constant->DoubleValue();
498 ExternalReference LCodeGen::ToExternalReference(LConstantOperand* op) const {
499 HConstant* constant = chunk_->LookupConstant(op);
500 DCHECK(constant->HasExternalReferenceValue());
501 return constant->ExternalReferenceValue();
505 Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
506 HConstant* constant = chunk_->LookupConstant(op);
507 DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
508 return constant->handle(isolate());
512 static int ArgumentsOffsetWithoutFrame(int index) {
514 return -(index + 1) * kPointerSize + kPCOnStackSize;
518 Operand LCodeGen::ToOperand(LOperand* op) const {
519 // Does not handle registers. In X64 assembler, plain registers are not
520 // representable as an Operand.
521 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
522 if (NeedsEagerFrame()) {
523 return Operand(rbp, StackSlotOffset(op->index()));
525 // Retrieve parameter without eager stack-frame relative to the
527 return Operand(rsp, ArgumentsOffsetWithoutFrame(op->index()));
532 void LCodeGen::WriteTranslation(LEnvironment* environment,
533 Translation* translation) {
534 if (environment == NULL) return;
536 // The translation includes one command per value in the environment.
537 int translation_size = environment->translation_size();
538 // The output frame height does not include the parameters.
539 int height = translation_size - environment->parameter_count();
541 WriteTranslation(environment->outer(), translation);
542 bool has_closure_id = !info()->closure().is_null() &&
543 !info()->closure().is_identical_to(environment->closure());
544 int closure_id = has_closure_id
545 ? DefineDeoptimizationLiteral(environment->closure())
546 : Translation::kSelfLiteralId;
548 switch (environment->frame_type()) {
550 translation->BeginJSFrame(environment->ast_id(), closure_id, height);
553 translation->BeginConstructStubFrame(closure_id, translation_size);
556 DCHECK(translation_size == 1);
558 translation->BeginGetterStubFrame(closure_id);
561 DCHECK(translation_size == 2);
563 translation->BeginSetterStubFrame(closure_id);
565 case ARGUMENTS_ADAPTOR:
566 translation->BeginArgumentsAdaptorFrame(closure_id, translation_size);
569 translation->BeginCompiledStubFrame();
573 int object_index = 0;
574 int dematerialized_index = 0;
575 for (int i = 0; i < translation_size; ++i) {
576 LOperand* value = environment->values()->at(i);
577 AddToTranslation(environment,
580 environment->HasTaggedValueAt(i),
581 environment->HasUint32ValueAt(i),
583 &dematerialized_index);
588 void LCodeGen::AddToTranslation(LEnvironment* environment,
589 Translation* translation,
593 int* object_index_pointer,
594 int* dematerialized_index_pointer) {
595 if (op == LEnvironment::materialization_marker()) {
596 int object_index = (*object_index_pointer)++;
597 if (environment->ObjectIsDuplicateAt(object_index)) {
598 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
599 translation->DuplicateObject(dupe_of);
602 int object_length = environment->ObjectLengthAt(object_index);
603 if (environment->ObjectIsArgumentsAt(object_index)) {
604 translation->BeginArgumentsObject(object_length);
606 translation->BeginCapturedObject(object_length);
608 int dematerialized_index = *dematerialized_index_pointer;
609 int env_offset = environment->translation_size() + dematerialized_index;
610 *dematerialized_index_pointer += object_length;
611 for (int i = 0; i < object_length; ++i) {
612 LOperand* value = environment->values()->at(env_offset + i);
613 AddToTranslation(environment,
616 environment->HasTaggedValueAt(env_offset + i),
617 environment->HasUint32ValueAt(env_offset + i),
618 object_index_pointer,
619 dematerialized_index_pointer);
624 if (op->IsStackSlot()) {
626 translation->StoreStackSlot(op->index());
627 } else if (is_uint32) {
628 translation->StoreUint32StackSlot(op->index());
630 translation->StoreInt32StackSlot(op->index());
632 } else if (op->IsDoubleStackSlot()) {
633 translation->StoreDoubleStackSlot(op->index());
634 } else if (op->IsRegister()) {
635 Register reg = ToRegister(op);
637 translation->StoreRegister(reg);
638 } else if (is_uint32) {
639 translation->StoreUint32Register(reg);
641 translation->StoreInt32Register(reg);
643 } else if (op->IsDoubleRegister()) {
644 XMMRegister reg = ToDoubleRegister(op);
645 translation->StoreDoubleRegister(reg);
646 } else if (op->IsConstantOperand()) {
647 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
648 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
649 translation->StoreLiteral(src_index);
656 void LCodeGen::CallCodeGeneric(Handle<Code> code,
657 RelocInfo::Mode mode,
659 SafepointMode safepoint_mode,
661 DCHECK(instr != NULL);
663 RecordSafepointWithLazyDeopt(instr, safepoint_mode, argc);
665 // Signal that we don't inline smi code before these stubs in the
666 // optimizing code generator.
667 if (code->kind() == Code::BINARY_OP_IC ||
668 code->kind() == Code::COMPARE_IC) {
674 void LCodeGen::CallCode(Handle<Code> code,
675 RelocInfo::Mode mode,
676 LInstruction* instr) {
677 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT, 0);
681 void LCodeGen::CallRuntime(const Runtime::Function* function,
684 SaveFPRegsMode save_doubles) {
685 DCHECK(instr != NULL);
686 DCHECK(instr->HasPointerMap());
688 __ CallRuntime(function, num_arguments, save_doubles);
690 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT, 0);
694 void LCodeGen::LoadContextFromDeferred(LOperand* context) {
695 if (context->IsRegister()) {
696 if (!ToRegister(context).is(rsi)) {
697 __ movp(rsi, ToRegister(context));
699 } else if (context->IsStackSlot()) {
700 __ movp(rsi, ToOperand(context));
701 } else if (context->IsConstantOperand()) {
702 HConstant* constant =
703 chunk_->LookupConstant(LConstantOperand::cast(context));
704 __ Move(rsi, Handle<Object>::cast(constant->handle(isolate())));
712 void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
716 LoadContextFromDeferred(context);
718 __ CallRuntimeSaveDoubles(id);
719 RecordSafepointWithRegisters(
720 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
724 void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
725 Safepoint::DeoptMode mode) {
726 environment->set_has_been_used();
727 if (!environment->HasBeenRegistered()) {
728 // Physical stack frame layout:
729 // -x ............. -4 0 ..................................... y
730 // [incoming arguments] [spill slots] [pushed outgoing arguments]
732 // Layout of the environment:
733 // 0 ..................................................... size-1
734 // [parameters] [locals] [expression stack including arguments]
736 // Layout of the translation:
737 // 0 ........................................................ size - 1 + 4
738 // [expression stack including arguments] [locals] [4 words] [parameters]
739 // |>------------ translation_size ------------<|
742 int jsframe_count = 0;
743 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
745 if (e->frame_type() == JS_FUNCTION) {
749 Translation translation(&translations_, frame_count, jsframe_count, zone());
750 WriteTranslation(environment, &translation);
751 int deoptimization_index = deoptimizations_.length();
752 int pc_offset = masm()->pc_offset();
753 environment->Register(deoptimization_index,
755 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
756 deoptimizations_.Add(environment, environment->zone());
761 void LCodeGen::DeoptimizeIf(Condition cc, LInstruction* instr,
762 Deoptimizer::DeoptReason deopt_reason,
763 Deoptimizer::BailoutType bailout_type) {
764 LEnvironment* environment = instr->environment();
765 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
766 DCHECK(environment->HasBeenRegistered());
767 int id = environment->deoptimization_index();
768 DCHECK(info()->IsOptimizing() || info()->IsStub());
770 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
772 Abort(kBailoutWasNotPrepared);
776 if (DeoptEveryNTimes()) {
777 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
781 Operand count_operand = masm()->ExternalOperand(count, kScratchRegister);
782 __ movl(rax, count_operand);
783 __ subl(rax, Immediate(1));
784 __ j(not_zero, &no_deopt, Label::kNear);
785 if (FLAG_trap_on_deopt) __ int3();
786 __ movl(rax, Immediate(FLAG_deopt_every_n_times));
787 __ movl(count_operand, rax);
790 DCHECK(frame_is_built_);
791 __ call(entry, RelocInfo::RUNTIME_ENTRY);
793 __ movl(count_operand, rax);
798 if (info()->ShouldTrapOnDeopt()) {
800 if (cc != no_condition) {
801 __ j(NegateCondition(cc), &done, Label::kNear);
807 Deoptimizer::DeoptInfo deopt_info = MakeDeoptInfo(instr, deopt_reason);
809 DCHECK(info()->IsStub() || frame_is_built_);
810 // Go through jump table if we need to handle condition, build frame, or
811 // restore caller doubles.
812 if (cc == no_condition && frame_is_built_ &&
813 !info()->saves_caller_doubles()) {
814 DeoptComment(deopt_info);
815 __ call(entry, RelocInfo::RUNTIME_ENTRY);
816 info()->LogDeoptCallPosition(masm()->pc_offset(), deopt_info.inlining_id);
818 Deoptimizer::JumpTableEntry table_entry(entry, deopt_info, bailout_type,
820 // We often have several deopts to the same entry, reuse the last
821 // jump entry if this is the case.
822 if (FLAG_trace_deopt || isolate()->cpu_profiler()->is_profiling() ||
823 jump_table_.is_empty() ||
824 !table_entry.IsEquivalentTo(jump_table_.last())) {
825 jump_table_.Add(table_entry, zone());
827 if (cc == no_condition) {
828 __ jmp(&jump_table_.last().label);
830 __ j(cc, &jump_table_.last().label);
836 void LCodeGen::DeoptimizeIf(Condition cc, LInstruction* instr,
837 Deoptimizer::DeoptReason deopt_reason) {
838 Deoptimizer::BailoutType bailout_type = info()->IsStub()
840 : Deoptimizer::EAGER;
841 DeoptimizeIf(cc, instr, deopt_reason, bailout_type);
845 void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
846 int length = deoptimizations_.length();
847 if (length == 0) return;
848 Handle<DeoptimizationInputData> data =
849 DeoptimizationInputData::New(isolate(), length, TENURED);
851 Handle<ByteArray> translations =
852 translations_.CreateByteArray(isolate()->factory());
853 data->SetTranslationByteArray(*translations);
854 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
855 data->SetOptimizationId(Smi::FromInt(info_->optimization_id()));
856 if (info_->IsOptimizing()) {
857 // Reference to shared function info does not change between phases.
858 AllowDeferredHandleDereference allow_handle_dereference;
859 data->SetSharedFunctionInfo(*info_->shared_info());
861 data->SetSharedFunctionInfo(Smi::FromInt(0));
863 data->SetWeakCellCache(Smi::FromInt(0));
865 Handle<FixedArray> literals =
866 factory()->NewFixedArray(deoptimization_literals_.length(), TENURED);
867 { AllowDeferredHandleDereference copy_handles;
868 for (int i = 0; i < deoptimization_literals_.length(); i++) {
869 literals->set(i, *deoptimization_literals_[i]);
871 data->SetLiteralArray(*literals);
874 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id().ToInt()));
875 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
877 // Populate the deoptimization entries.
878 for (int i = 0; i < length; i++) {
879 LEnvironment* env = deoptimizations_[i];
880 data->SetAstId(i, env->ast_id());
881 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
882 data->SetArgumentsStackHeight(i,
883 Smi::FromInt(env->arguments_stack_height()));
884 data->SetPc(i, Smi::FromInt(env->pc_offset()));
886 code->set_deoptimization_data(*data);
890 int LCodeGen::DefineDeoptimizationLiteral(Handle<Object> literal) {
891 int result = deoptimization_literals_.length();
892 for (int i = 0; i < deoptimization_literals_.length(); ++i) {
893 if (deoptimization_literals_[i].is_identical_to(literal)) return i;
895 deoptimization_literals_.Add(literal, zone());
900 void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
901 DCHECK(deoptimization_literals_.length() == 0);
903 const ZoneList<Handle<JSFunction> >* inlined_closures =
904 chunk()->inlined_closures();
906 for (int i = 0, length = inlined_closures->length();
909 DefineDeoptimizationLiteral(inlined_closures->at(i));
912 inlined_function_count_ = deoptimization_literals_.length();
916 void LCodeGen::RecordSafepointWithLazyDeopt(
917 LInstruction* instr, SafepointMode safepoint_mode, int argc) {
918 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
919 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
921 DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS);
922 RecordSafepointWithRegisters(
923 instr->pointer_map(), argc, Safepoint::kLazyDeopt);
928 void LCodeGen::RecordSafepoint(
929 LPointerMap* pointers,
930 Safepoint::Kind kind,
932 Safepoint::DeoptMode deopt_mode) {
933 DCHECK(kind == expected_safepoint_kind_);
935 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
937 Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
938 kind, arguments, deopt_mode);
939 for (int i = 0; i < operands->length(); i++) {
940 LOperand* pointer = operands->at(i);
941 if (pointer->IsStackSlot()) {
942 safepoint.DefinePointerSlot(pointer->index(), zone());
943 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
944 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
950 void LCodeGen::RecordSafepoint(LPointerMap* pointers,
951 Safepoint::DeoptMode deopt_mode) {
952 RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
956 void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
957 LPointerMap empty_pointers(zone());
958 RecordSafepoint(&empty_pointers, deopt_mode);
962 void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
964 Safepoint::DeoptMode deopt_mode) {
965 RecordSafepoint(pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
969 void LCodeGen::RecordAndWritePosition(int position) {
970 if (position == RelocInfo::kNoPosition) return;
971 masm()->positions_recorder()->RecordPosition(position);
972 masm()->positions_recorder()->WriteRecordedPositions();
976 static const char* LabelType(LLabel* label) {
977 if (label->is_loop_header()) return " (loop header)";
978 if (label->is_osr_entry()) return " (OSR entry)";
983 void LCodeGen::DoLabel(LLabel* label) {
984 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
985 current_instruction_,
986 label->hydrogen_value()->id(),
989 __ bind(label->label());
990 current_block_ = label->block_id();
995 void LCodeGen::DoParallelMove(LParallelMove* move) {
996 resolver_.Resolve(move);
1000 void LCodeGen::DoGap(LGap* gap) {
1001 for (int i = LGap::FIRST_INNER_POSITION;
1002 i <= LGap::LAST_INNER_POSITION;
1004 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
1005 LParallelMove* move = gap->GetParallelMove(inner_pos);
1006 if (move != NULL) DoParallelMove(move);
1011 void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
1016 void LCodeGen::DoParameter(LParameter* instr) {
1021 void LCodeGen::DoCallStub(LCallStub* instr) {
1022 DCHECK(ToRegister(instr->context()).is(rsi));
1023 DCHECK(ToRegister(instr->result()).is(rax));
1024 switch (instr->hydrogen()->major_key()) {
1025 case CodeStub::RegExpExec: {
1026 RegExpExecStub stub(isolate());
1027 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1030 case CodeStub::SubString: {
1031 SubStringStub stub(isolate());
1032 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1035 case CodeStub::StringCompare: {
1036 StringCompareStub stub(isolate());
1037 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1046 void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
1047 GenerateOsrPrologue();
1051 void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
1052 Register dividend = ToRegister(instr->dividend());
1053 int32_t divisor = instr->divisor();
1054 DCHECK(dividend.is(ToRegister(instr->result())));
1056 // Theoretically, a variation of the branch-free code for integer division by
1057 // a power of 2 (calculating the remainder via an additional multiplication
1058 // (which gets simplified to an 'and') and subtraction) should be faster, and
1059 // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
1060 // indicate that positive dividends are heavily favored, so the branching
1061 // version performs better.
1062 HMod* hmod = instr->hydrogen();
1063 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1064 Label dividend_is_not_negative, done;
1065 if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
1066 __ testl(dividend, dividend);
1067 __ j(not_sign, ÷nd_is_not_negative, Label::kNear);
1068 // Note that this is correct even for kMinInt operands.
1070 __ andl(dividend, Immediate(mask));
1072 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1073 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1075 __ jmp(&done, Label::kNear);
1078 __ bind(÷nd_is_not_negative);
1079 __ andl(dividend, Immediate(mask));
1084 void LCodeGen::DoModByConstI(LModByConstI* instr) {
1085 Register dividend = ToRegister(instr->dividend());
1086 int32_t divisor = instr->divisor();
1087 DCHECK(ToRegister(instr->result()).is(rax));
1090 DeoptimizeIf(no_condition, instr, Deoptimizer::kDivisionByZero);
1094 __ TruncatingDiv(dividend, Abs(divisor));
1095 __ imull(rdx, rdx, Immediate(Abs(divisor)));
1096 __ movl(rax, dividend);
1099 // Check for negative zero.
1100 HMod* hmod = instr->hydrogen();
1101 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1102 Label remainder_not_zero;
1103 __ j(not_zero, &remainder_not_zero, Label::kNear);
1104 __ cmpl(dividend, Immediate(0));
1105 DeoptimizeIf(less, instr, Deoptimizer::kMinusZero);
1106 __ bind(&remainder_not_zero);
1111 void LCodeGen::DoModI(LModI* instr) {
1112 HMod* hmod = instr->hydrogen();
1114 Register left_reg = ToRegister(instr->left());
1115 DCHECK(left_reg.is(rax));
1116 Register right_reg = ToRegister(instr->right());
1117 DCHECK(!right_reg.is(rax));
1118 DCHECK(!right_reg.is(rdx));
1119 Register result_reg = ToRegister(instr->result());
1120 DCHECK(result_reg.is(rdx));
1123 // Check for x % 0, idiv would signal a divide error. We have to
1124 // deopt in this case because we can't return a NaN.
1125 if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
1126 __ testl(right_reg, right_reg);
1127 DeoptimizeIf(zero, instr, Deoptimizer::kDivisionByZero);
1130 // Check for kMinInt % -1, idiv would signal a divide error. We
1131 // have to deopt if we care about -0, because we can't return that.
1132 if (hmod->CheckFlag(HValue::kCanOverflow)) {
1133 Label no_overflow_possible;
1134 __ cmpl(left_reg, Immediate(kMinInt));
1135 __ j(not_zero, &no_overflow_possible, Label::kNear);
1136 __ cmpl(right_reg, Immediate(-1));
1137 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1138 DeoptimizeIf(equal, instr, Deoptimizer::kMinusZero);
1140 __ j(not_equal, &no_overflow_possible, Label::kNear);
1141 __ Set(result_reg, 0);
1142 __ jmp(&done, Label::kNear);
1144 __ bind(&no_overflow_possible);
1147 // Sign extend dividend in eax into edx:eax, since we are using only the low
1148 // 32 bits of the values.
1151 // If we care about -0, test if the dividend is <0 and the result is 0.
1152 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1153 Label positive_left;
1154 __ testl(left_reg, left_reg);
1155 __ j(not_sign, &positive_left, Label::kNear);
1156 __ idivl(right_reg);
1157 __ testl(result_reg, result_reg);
1158 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1159 __ jmp(&done, Label::kNear);
1160 __ bind(&positive_left);
1162 __ idivl(right_reg);
1167 void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
1168 Register dividend = ToRegister(instr->dividend());
1169 int32_t divisor = instr->divisor();
1170 DCHECK(dividend.is(ToRegister(instr->result())));
1172 // If the divisor is positive, things are easy: There can be no deopts and we
1173 // can simply do an arithmetic right shift.
1174 if (divisor == 1) return;
1175 int32_t shift = WhichPowerOf2Abs(divisor);
1177 __ sarl(dividend, Immediate(shift));
1181 // If the divisor is negative, we have to negate and handle edge cases.
1183 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1184 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1187 // Dividing by -1 is basically negation, unless we overflow.
1188 if (divisor == -1) {
1189 if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1190 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1195 // If the negation could not overflow, simply shifting is OK.
1196 if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1197 __ sarl(dividend, Immediate(shift));
1201 Label not_kmin_int, done;
1202 __ j(no_overflow, ¬_kmin_int, Label::kNear);
1203 __ movl(dividend, Immediate(kMinInt / divisor));
1204 __ jmp(&done, Label::kNear);
1205 __ bind(¬_kmin_int);
1206 __ sarl(dividend, Immediate(shift));
1211 void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
1212 Register dividend = ToRegister(instr->dividend());
1213 int32_t divisor = instr->divisor();
1214 DCHECK(ToRegister(instr->result()).is(rdx));
1217 DeoptimizeIf(no_condition, instr, Deoptimizer::kDivisionByZero);
1221 // Check for (0 / -x) that will produce negative zero.
1222 HMathFloorOfDiv* hdiv = instr->hydrogen();
1223 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1224 __ testl(dividend, dividend);
1225 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1228 // Easy case: We need no dynamic check for the dividend and the flooring
1229 // division is the same as the truncating division.
1230 if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
1231 (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
1232 __ TruncatingDiv(dividend, Abs(divisor));
1233 if (divisor < 0) __ negl(rdx);
1237 // In the general case we may need to adjust before and after the truncating
1238 // division to get a flooring division.
1239 Register temp = ToRegister(instr->temp3());
1240 DCHECK(!temp.is(dividend) && !temp.is(rax) && !temp.is(rdx));
1241 Label needs_adjustment, done;
1242 __ cmpl(dividend, Immediate(0));
1243 __ j(divisor > 0 ? less : greater, &needs_adjustment, Label::kNear);
1244 __ TruncatingDiv(dividend, Abs(divisor));
1245 if (divisor < 0) __ negl(rdx);
1246 __ jmp(&done, Label::kNear);
1247 __ bind(&needs_adjustment);
1248 __ leal(temp, Operand(dividend, divisor > 0 ? 1 : -1));
1249 __ TruncatingDiv(temp, Abs(divisor));
1250 if (divisor < 0) __ negl(rdx);
1256 // TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
1257 void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
1258 HBinaryOperation* hdiv = instr->hydrogen();
1259 Register dividend = ToRegister(instr->dividend());
1260 Register divisor = ToRegister(instr->divisor());
1261 Register remainder = ToRegister(instr->temp());
1262 Register result = ToRegister(instr->result());
1263 DCHECK(dividend.is(rax));
1264 DCHECK(remainder.is(rdx));
1265 DCHECK(result.is(rax));
1266 DCHECK(!divisor.is(rax));
1267 DCHECK(!divisor.is(rdx));
1270 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1271 __ testl(divisor, divisor);
1272 DeoptimizeIf(zero, instr, Deoptimizer::kDivisionByZero);
1275 // Check for (0 / -x) that will produce negative zero.
1276 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1277 Label dividend_not_zero;
1278 __ testl(dividend, dividend);
1279 __ j(not_zero, ÷nd_not_zero, Label::kNear);
1280 __ testl(divisor, divisor);
1281 DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero);
1282 __ bind(÷nd_not_zero);
1285 // Check for (kMinInt / -1).
1286 if (hdiv->CheckFlag(HValue::kCanOverflow)) {
1287 Label dividend_not_min_int;
1288 __ cmpl(dividend, Immediate(kMinInt));
1289 __ j(not_zero, ÷nd_not_min_int, Label::kNear);
1290 __ cmpl(divisor, Immediate(-1));
1291 DeoptimizeIf(zero, instr, Deoptimizer::kOverflow);
1292 __ bind(÷nd_not_min_int);
1295 // Sign extend to rdx (= remainder).
1300 __ testl(remainder, remainder);
1301 __ j(zero, &done, Label::kNear);
1302 __ xorl(remainder, divisor);
1303 __ sarl(remainder, Immediate(31));
1304 __ addl(result, remainder);
1309 void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
1310 Register dividend = ToRegister(instr->dividend());
1311 int32_t divisor = instr->divisor();
1312 Register result = ToRegister(instr->result());
1313 DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
1314 DCHECK(!result.is(dividend));
1316 // Check for (0 / -x) that will produce negative zero.
1317 HDiv* hdiv = instr->hydrogen();
1318 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1319 __ testl(dividend, dividend);
1320 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1322 // Check for (kMinInt / -1).
1323 if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
1324 __ cmpl(dividend, Immediate(kMinInt));
1325 DeoptimizeIf(zero, instr, Deoptimizer::kOverflow);
1327 // Deoptimize if remainder will not be 0.
1328 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1329 divisor != 1 && divisor != -1) {
1330 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1331 __ testl(dividend, Immediate(mask));
1332 DeoptimizeIf(not_zero, instr, Deoptimizer::kLostPrecision);
1334 __ Move(result, dividend);
1335 int32_t shift = WhichPowerOf2Abs(divisor);
1337 // The arithmetic shift is always OK, the 'if' is an optimization only.
1338 if (shift > 1) __ sarl(result, Immediate(31));
1339 __ shrl(result, Immediate(32 - shift));
1340 __ addl(result, dividend);
1341 __ sarl(result, Immediate(shift));
1343 if (divisor < 0) __ negl(result);
1347 void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
1348 Register dividend = ToRegister(instr->dividend());
1349 int32_t divisor = instr->divisor();
1350 DCHECK(ToRegister(instr->result()).is(rdx));
1353 DeoptimizeIf(no_condition, instr, Deoptimizer::kDivisionByZero);
1357 // Check for (0 / -x) that will produce negative zero.
1358 HDiv* hdiv = instr->hydrogen();
1359 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1360 __ testl(dividend, dividend);
1361 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1364 __ TruncatingDiv(dividend, Abs(divisor));
1365 if (divisor < 0) __ negl(rdx);
1367 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1369 __ imull(rax, rax, Immediate(divisor));
1370 __ subl(rax, dividend);
1371 DeoptimizeIf(not_equal, instr, Deoptimizer::kLostPrecision);
1376 // TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
1377 void LCodeGen::DoDivI(LDivI* instr) {
1378 HBinaryOperation* hdiv = instr->hydrogen();
1379 Register dividend = ToRegister(instr->dividend());
1380 Register divisor = ToRegister(instr->divisor());
1381 Register remainder = ToRegister(instr->temp());
1382 DCHECK(dividend.is(rax));
1383 DCHECK(remainder.is(rdx));
1384 DCHECK(ToRegister(instr->result()).is(rax));
1385 DCHECK(!divisor.is(rax));
1386 DCHECK(!divisor.is(rdx));
1389 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1390 __ testl(divisor, divisor);
1391 DeoptimizeIf(zero, instr, Deoptimizer::kDivisionByZero);
1394 // Check for (0 / -x) that will produce negative zero.
1395 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1396 Label dividend_not_zero;
1397 __ testl(dividend, dividend);
1398 __ j(not_zero, ÷nd_not_zero, Label::kNear);
1399 __ testl(divisor, divisor);
1400 DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero);
1401 __ bind(÷nd_not_zero);
1404 // Check for (kMinInt / -1).
1405 if (hdiv->CheckFlag(HValue::kCanOverflow)) {
1406 Label dividend_not_min_int;
1407 __ cmpl(dividend, Immediate(kMinInt));
1408 __ j(not_zero, ÷nd_not_min_int, Label::kNear);
1409 __ cmpl(divisor, Immediate(-1));
1410 DeoptimizeIf(zero, instr, Deoptimizer::kOverflow);
1411 __ bind(÷nd_not_min_int);
1414 // Sign extend to rdx (= remainder).
1418 if (!hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1419 // Deoptimize if remainder is not 0.
1420 __ testl(remainder, remainder);
1421 DeoptimizeIf(not_zero, instr, Deoptimizer::kLostPrecision);
1426 void LCodeGen::DoMulI(LMulI* instr) {
1427 Register left = ToRegister(instr->left());
1428 LOperand* right = instr->right();
1430 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1431 if (instr->hydrogen_value()->representation().IsSmi()) {
1432 __ movp(kScratchRegister, left);
1434 __ movl(kScratchRegister, left);
1439 instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1440 if (right->IsConstantOperand()) {
1441 int32_t right_value = ToInteger32(LConstantOperand::cast(right));
1442 if (right_value == -1) {
1444 } else if (right_value == 0) {
1445 __ xorl(left, left);
1446 } else if (right_value == 2) {
1447 __ addl(left, left);
1448 } else if (!can_overflow) {
1449 // If the multiplication is known to not overflow, we
1450 // can use operations that don't set the overflow flag
1452 switch (right_value) {
1457 __ leal(left, Operand(left, left, times_2, 0));
1460 __ shll(left, Immediate(2));
1463 __ leal(left, Operand(left, left, times_4, 0));
1466 __ shll(left, Immediate(3));
1469 __ leal(left, Operand(left, left, times_8, 0));
1472 __ shll(left, Immediate(4));
1475 __ imull(left, left, Immediate(right_value));
1479 __ imull(left, left, Immediate(right_value));
1481 } else if (right->IsStackSlot()) {
1482 if (instr->hydrogen_value()->representation().IsSmi()) {
1483 __ SmiToInteger64(left, left);
1484 __ imulp(left, ToOperand(right));
1486 __ imull(left, ToOperand(right));
1489 if (instr->hydrogen_value()->representation().IsSmi()) {
1490 __ SmiToInteger64(left, left);
1491 __ imulp(left, ToRegister(right));
1493 __ imull(left, ToRegister(right));
1498 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1501 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1502 // Bail out if the result is supposed to be negative zero.
1504 if (instr->hydrogen_value()->representation().IsSmi()) {
1505 __ testp(left, left);
1507 __ testl(left, left);
1509 __ j(not_zero, &done, Label::kNear);
1510 if (right->IsConstantOperand()) {
1511 // Constant can't be represented as 32-bit Smi due to immediate size
1513 DCHECK(SmiValuesAre32Bits()
1514 ? !instr->hydrogen_value()->representation().IsSmi()
1515 : SmiValuesAre31Bits());
1516 if (ToInteger32(LConstantOperand::cast(right)) < 0) {
1517 DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero);
1518 } else if (ToInteger32(LConstantOperand::cast(right)) == 0) {
1519 __ cmpl(kScratchRegister, Immediate(0));
1520 DeoptimizeIf(less, instr, Deoptimizer::kMinusZero);
1522 } else if (right->IsStackSlot()) {
1523 if (instr->hydrogen_value()->representation().IsSmi()) {
1524 __ orp(kScratchRegister, ToOperand(right));
1526 __ orl(kScratchRegister, ToOperand(right));
1528 DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero);
1530 // Test the non-zero operand for negative sign.
1531 if (instr->hydrogen_value()->representation().IsSmi()) {
1532 __ orp(kScratchRegister, ToRegister(right));
1534 __ orl(kScratchRegister, ToRegister(right));
1536 DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero);
1543 void LCodeGen::DoBitI(LBitI* instr) {
1544 LOperand* left = instr->left();
1545 LOperand* right = instr->right();
1546 DCHECK(left->Equals(instr->result()));
1547 DCHECK(left->IsRegister());
1549 if (right->IsConstantOperand()) {
1550 int32_t right_operand =
1551 ToRepresentation(LConstantOperand::cast(right),
1552 instr->hydrogen()->right()->representation());
1553 switch (instr->op()) {
1554 case Token::BIT_AND:
1555 __ andl(ToRegister(left), Immediate(right_operand));
1558 __ orl(ToRegister(left), Immediate(right_operand));
1560 case Token::BIT_XOR:
1561 if (right_operand == int32_t(~0)) {
1562 __ notl(ToRegister(left));
1564 __ xorl(ToRegister(left), Immediate(right_operand));
1571 } else if (right->IsStackSlot()) {
1572 switch (instr->op()) {
1573 case Token::BIT_AND:
1574 if (instr->IsInteger32()) {
1575 __ andl(ToRegister(left), ToOperand(right));
1577 __ andp(ToRegister(left), ToOperand(right));
1581 if (instr->IsInteger32()) {
1582 __ orl(ToRegister(left), ToOperand(right));
1584 __ orp(ToRegister(left), ToOperand(right));
1587 case Token::BIT_XOR:
1588 if (instr->IsInteger32()) {
1589 __ xorl(ToRegister(left), ToOperand(right));
1591 __ xorp(ToRegister(left), ToOperand(right));
1599 DCHECK(right->IsRegister());
1600 switch (instr->op()) {
1601 case Token::BIT_AND:
1602 if (instr->IsInteger32()) {
1603 __ andl(ToRegister(left), ToRegister(right));
1605 __ andp(ToRegister(left), ToRegister(right));
1609 if (instr->IsInteger32()) {
1610 __ orl(ToRegister(left), ToRegister(right));
1612 __ orp(ToRegister(left), ToRegister(right));
1615 case Token::BIT_XOR:
1616 if (instr->IsInteger32()) {
1617 __ xorl(ToRegister(left), ToRegister(right));
1619 __ xorp(ToRegister(left), ToRegister(right));
1630 void LCodeGen::DoShiftI(LShiftI* instr) {
1631 LOperand* left = instr->left();
1632 LOperand* right = instr->right();
1633 DCHECK(left->Equals(instr->result()));
1634 DCHECK(left->IsRegister());
1635 if (right->IsRegister()) {
1636 DCHECK(ToRegister(right).is(rcx));
1638 switch (instr->op()) {
1640 __ rorl_cl(ToRegister(left));
1643 __ sarl_cl(ToRegister(left));
1646 __ shrl_cl(ToRegister(left));
1647 if (instr->can_deopt()) {
1648 __ testl(ToRegister(left), ToRegister(left));
1649 DeoptimizeIf(negative, instr, Deoptimizer::kNegativeValue);
1653 __ shll_cl(ToRegister(left));
1660 int32_t value = ToInteger32(LConstantOperand::cast(right));
1661 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1662 switch (instr->op()) {
1664 if (shift_count != 0) {
1665 __ rorl(ToRegister(left), Immediate(shift_count));
1669 if (shift_count != 0) {
1670 __ sarl(ToRegister(left), Immediate(shift_count));
1674 if (shift_count != 0) {
1675 __ shrl(ToRegister(left), Immediate(shift_count));
1676 } else if (instr->can_deopt()) {
1677 __ testl(ToRegister(left), ToRegister(left));
1678 DeoptimizeIf(negative, instr, Deoptimizer::kNegativeValue);
1682 if (shift_count != 0) {
1683 if (instr->hydrogen_value()->representation().IsSmi()) {
1684 if (SmiValuesAre32Bits()) {
1685 __ shlp(ToRegister(left), Immediate(shift_count));
1687 DCHECK(SmiValuesAre31Bits());
1688 if (instr->can_deopt()) {
1689 if (shift_count != 1) {
1690 __ shll(ToRegister(left), Immediate(shift_count - 1));
1692 __ Integer32ToSmi(ToRegister(left), ToRegister(left));
1693 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1695 __ shll(ToRegister(left), Immediate(shift_count));
1699 __ shll(ToRegister(left), Immediate(shift_count));
1711 void LCodeGen::DoSubI(LSubI* instr) {
1712 LOperand* left = instr->left();
1713 LOperand* right = instr->right();
1714 DCHECK(left->Equals(instr->result()));
1716 if (right->IsConstantOperand()) {
1717 int32_t right_operand =
1718 ToRepresentation(LConstantOperand::cast(right),
1719 instr->hydrogen()->right()->representation());
1720 __ subl(ToRegister(left), Immediate(right_operand));
1721 } else if (right->IsRegister()) {
1722 if (instr->hydrogen_value()->representation().IsSmi()) {
1723 __ subp(ToRegister(left), ToRegister(right));
1725 __ subl(ToRegister(left), ToRegister(right));
1728 if (instr->hydrogen_value()->representation().IsSmi()) {
1729 __ subp(ToRegister(left), ToOperand(right));
1731 __ subl(ToRegister(left), ToOperand(right));
1735 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1736 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1741 void LCodeGen::DoConstantI(LConstantI* instr) {
1742 Register dst = ToRegister(instr->result());
1743 if (instr->value() == 0) {
1746 __ movl(dst, Immediate(instr->value()));
1751 void LCodeGen::DoConstantS(LConstantS* instr) {
1752 __ Move(ToRegister(instr->result()), instr->value());
1756 void LCodeGen::DoConstantD(LConstantD* instr) {
1757 __ Move(ToDoubleRegister(instr->result()), instr->bits());
1761 void LCodeGen::DoConstantE(LConstantE* instr) {
1762 __ LoadAddress(ToRegister(instr->result()), instr->value());
1766 void LCodeGen::DoConstantT(LConstantT* instr) {
1767 Handle<Object> object = instr->value(isolate());
1768 AllowDeferredHandleDereference smi_check;
1769 __ Move(ToRegister(instr->result()), object);
1773 void LCodeGen::DoMapEnumLength(LMapEnumLength* instr) {
1774 Register result = ToRegister(instr->result());
1775 Register map = ToRegister(instr->value());
1776 __ EnumLength(result, map);
1780 void LCodeGen::DoDateField(LDateField* instr) {
1781 Register object = ToRegister(instr->date());
1782 Register result = ToRegister(instr->result());
1783 Smi* index = instr->index();
1784 Label runtime, done, not_date_object;
1785 DCHECK(object.is(result));
1786 DCHECK(object.is(rax));
1788 Condition cc = masm()->CheckSmi(object);
1789 DeoptimizeIf(cc, instr, Deoptimizer::kSmi);
1790 __ CmpObjectType(object, JS_DATE_TYPE, kScratchRegister);
1791 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotADateObject);
1793 if (index->value() == 0) {
1794 __ movp(result, FieldOperand(object, JSDate::kValueOffset));
1796 if (index->value() < JSDate::kFirstUncachedField) {
1797 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
1798 Operand stamp_operand = __ ExternalOperand(stamp);
1799 __ movp(kScratchRegister, stamp_operand);
1800 __ cmpp(kScratchRegister, FieldOperand(object,
1801 JSDate::kCacheStampOffset));
1802 __ j(not_equal, &runtime, Label::kNear);
1803 __ movp(result, FieldOperand(object, JSDate::kValueOffset +
1804 kPointerSize * index->value()));
1805 __ jmp(&done, Label::kNear);
1808 __ PrepareCallCFunction(2);
1809 __ movp(arg_reg_1, object);
1810 __ Move(arg_reg_2, index, Assembler::RelocInfoNone());
1811 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
1817 Operand LCodeGen::BuildSeqStringOperand(Register string,
1819 String::Encoding encoding) {
1820 if (index->IsConstantOperand()) {
1821 int offset = ToInteger32(LConstantOperand::cast(index));
1822 if (encoding == String::TWO_BYTE_ENCODING) {
1823 offset *= kUC16Size;
1825 STATIC_ASSERT(kCharSize == 1);
1826 return FieldOperand(string, SeqString::kHeaderSize + offset);
1828 return FieldOperand(
1829 string, ToRegister(index),
1830 encoding == String::ONE_BYTE_ENCODING ? times_1 : times_2,
1831 SeqString::kHeaderSize);
1835 void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
1836 String::Encoding encoding = instr->hydrogen()->encoding();
1837 Register result = ToRegister(instr->result());
1838 Register string = ToRegister(instr->string());
1840 if (FLAG_debug_code) {
1842 __ movp(string, FieldOperand(string, HeapObject::kMapOffset));
1843 __ movzxbp(string, FieldOperand(string, Map::kInstanceTypeOffset));
1845 __ andb(string, Immediate(kStringRepresentationMask | kStringEncodingMask));
1846 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1847 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1848 __ cmpp(string, Immediate(encoding == String::ONE_BYTE_ENCODING
1849 ? one_byte_seq_type : two_byte_seq_type));
1850 __ Check(equal, kUnexpectedStringType);
1854 Operand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1855 if (encoding == String::ONE_BYTE_ENCODING) {
1856 __ movzxbl(result, operand);
1858 __ movzxwl(result, operand);
1863 void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
1864 String::Encoding encoding = instr->hydrogen()->encoding();
1865 Register string = ToRegister(instr->string());
1867 if (FLAG_debug_code) {
1868 Register value = ToRegister(instr->value());
1869 Register index = ToRegister(instr->index());
1870 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1871 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1873 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
1874 ? one_byte_seq_type : two_byte_seq_type;
1875 __ EmitSeqStringSetCharCheck(string, index, value, encoding_mask);
1878 Operand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1879 if (instr->value()->IsConstantOperand()) {
1880 int value = ToInteger32(LConstantOperand::cast(instr->value()));
1881 DCHECK_LE(0, value);
1882 if (encoding == String::ONE_BYTE_ENCODING) {
1883 DCHECK_LE(value, String::kMaxOneByteCharCode);
1884 __ movb(operand, Immediate(value));
1886 DCHECK_LE(value, String::kMaxUtf16CodeUnit);
1887 __ movw(operand, Immediate(value));
1890 Register value = ToRegister(instr->value());
1891 if (encoding == String::ONE_BYTE_ENCODING) {
1892 __ movb(operand, value);
1894 __ movw(operand, value);
1900 void LCodeGen::DoAddI(LAddI* instr) {
1901 LOperand* left = instr->left();
1902 LOperand* right = instr->right();
1904 Representation target_rep = instr->hydrogen()->representation();
1905 bool is_p = target_rep.IsSmi() || target_rep.IsExternal();
1907 if (LAddI::UseLea(instr->hydrogen()) && !left->Equals(instr->result())) {
1908 if (right->IsConstantOperand()) {
1909 // No support for smi-immediates for 32-bit SMI.
1910 DCHECK(SmiValuesAre32Bits() ? !target_rep.IsSmi() : SmiValuesAre31Bits());
1912 ToRepresentation(LConstantOperand::cast(right),
1913 instr->hydrogen()->right()->representation());
1915 __ leap(ToRegister(instr->result()),
1916 MemOperand(ToRegister(left), offset));
1918 __ leal(ToRegister(instr->result()),
1919 MemOperand(ToRegister(left), offset));
1922 Operand address(ToRegister(left), ToRegister(right), times_1, 0);
1924 __ leap(ToRegister(instr->result()), address);
1926 __ leal(ToRegister(instr->result()), address);
1930 if (right->IsConstantOperand()) {
1931 // No support for smi-immediates for 32-bit SMI.
1932 DCHECK(SmiValuesAre32Bits() ? !target_rep.IsSmi() : SmiValuesAre31Bits());
1933 int32_t right_operand =
1934 ToRepresentation(LConstantOperand::cast(right),
1935 instr->hydrogen()->right()->representation());
1937 __ addp(ToRegister(left), Immediate(right_operand));
1939 __ addl(ToRegister(left), Immediate(right_operand));
1941 } else if (right->IsRegister()) {
1943 __ addp(ToRegister(left), ToRegister(right));
1945 __ addl(ToRegister(left), ToRegister(right));
1949 __ addp(ToRegister(left), ToOperand(right));
1951 __ addl(ToRegister(left), ToOperand(right));
1954 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1955 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1961 void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
1962 LOperand* left = instr->left();
1963 LOperand* right = instr->right();
1964 DCHECK(left->Equals(instr->result()));
1965 HMathMinMax::Operation operation = instr->hydrogen()->operation();
1966 if (instr->hydrogen()->representation().IsSmiOrInteger32()) {
1968 Condition condition = (operation == HMathMinMax::kMathMin)
1971 Register left_reg = ToRegister(left);
1972 if (right->IsConstantOperand()) {
1973 Immediate right_imm = Immediate(
1974 ToRepresentation(LConstantOperand::cast(right),
1975 instr->hydrogen()->right()->representation()));
1976 DCHECK(SmiValuesAre32Bits()
1977 ? !instr->hydrogen()->representation().IsSmi()
1978 : SmiValuesAre31Bits());
1979 __ cmpl(left_reg, right_imm);
1980 __ j(condition, &return_left, Label::kNear);
1981 __ movp(left_reg, right_imm);
1982 } else if (right->IsRegister()) {
1983 Register right_reg = ToRegister(right);
1984 if (instr->hydrogen_value()->representation().IsSmi()) {
1985 __ cmpp(left_reg, right_reg);
1987 __ cmpl(left_reg, right_reg);
1989 __ j(condition, &return_left, Label::kNear);
1990 __ movp(left_reg, right_reg);
1992 Operand right_op = ToOperand(right);
1993 if (instr->hydrogen_value()->representation().IsSmi()) {
1994 __ cmpp(left_reg, right_op);
1996 __ cmpl(left_reg, right_op);
1998 __ j(condition, &return_left, Label::kNear);
1999 __ movp(left_reg, right_op);
2001 __ bind(&return_left);
2003 DCHECK(instr->hydrogen()->representation().IsDouble());
2004 Label check_nan_left, check_zero, return_left, return_right;
2005 Condition condition = (operation == HMathMinMax::kMathMin) ? below : above;
2006 XMMRegister left_reg = ToDoubleRegister(left);
2007 XMMRegister right_reg = ToDoubleRegister(right);
2008 __ ucomisd(left_reg, right_reg);
2009 __ j(parity_even, &check_nan_left, Label::kNear); // At least one NaN.
2010 __ j(equal, &check_zero, Label::kNear); // left == right.
2011 __ j(condition, &return_left, Label::kNear);
2012 __ jmp(&return_right, Label::kNear);
2014 __ bind(&check_zero);
2015 XMMRegister xmm_scratch = double_scratch0();
2016 __ xorps(xmm_scratch, xmm_scratch);
2017 __ ucomisd(left_reg, xmm_scratch);
2018 __ j(not_equal, &return_left, Label::kNear); // left == right != 0.
2019 // At this point, both left and right are either 0 or -0.
2020 if (operation == HMathMinMax::kMathMin) {
2021 __ orps(left_reg, right_reg);
2023 // Since we operate on +0 and/or -0, addsd and andsd have the same effect.
2024 __ addsd(left_reg, right_reg);
2026 __ jmp(&return_left, Label::kNear);
2028 __ bind(&check_nan_left);
2029 __ ucomisd(left_reg, left_reg); // NaN check.
2030 __ j(parity_even, &return_left, Label::kNear);
2031 __ bind(&return_right);
2032 __ movaps(left_reg, right_reg);
2034 __ bind(&return_left);
2039 void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
2040 XMMRegister left = ToDoubleRegister(instr->left());
2041 XMMRegister right = ToDoubleRegister(instr->right());
2042 XMMRegister result = ToDoubleRegister(instr->result());
2043 switch (instr->op()) {
2045 if (CpuFeatures::IsSupported(AVX)) {
2046 CpuFeatureScope scope(masm(), AVX);
2047 __ vaddsd(result, left, right);
2049 DCHECK(result.is(left));
2050 __ addsd(left, right);
2054 if (CpuFeatures::IsSupported(AVX)) {
2055 CpuFeatureScope scope(masm(), AVX);
2056 __ vsubsd(result, left, right);
2058 DCHECK(result.is(left));
2059 __ subsd(left, right);
2063 if (CpuFeatures::IsSupported(AVX)) {
2064 CpuFeatureScope scope(masm(), AVX);
2065 __ vmulsd(result, left, right);
2067 DCHECK(result.is(left));
2068 __ mulsd(left, right);
2072 if (CpuFeatures::IsSupported(AVX)) {
2073 CpuFeatureScope scope(masm(), AVX);
2074 __ vdivsd(result, left, right);
2076 DCHECK(result.is(left));
2077 __ divsd(left, right);
2078 // Don't delete this mov. It may improve performance on some CPUs,
2079 // when there is a mulsd depending on the result
2080 __ movaps(left, left);
2084 XMMRegister xmm_scratch = double_scratch0();
2085 __ PrepareCallCFunction(2);
2086 __ movaps(xmm_scratch, left);
2087 DCHECK(right.is(xmm1));
2089 ExternalReference::mod_two_doubles_operation(isolate()), 2);
2090 __ movaps(result, xmm_scratch);
2100 void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
2101 DCHECK(ToRegister(instr->context()).is(rsi));
2102 DCHECK(ToRegister(instr->left()).is(rdx));
2103 DCHECK(ToRegister(instr->right()).is(rax));
2104 DCHECK(ToRegister(instr->result()).is(rax));
2106 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), instr->op()).code();
2107 CallCode(code, RelocInfo::CODE_TARGET, instr);
2111 template<class InstrType>
2112 void LCodeGen::EmitBranch(InstrType instr, Condition cc) {
2113 int left_block = instr->TrueDestination(chunk_);
2114 int right_block = instr->FalseDestination(chunk_);
2116 int next_block = GetNextEmittedBlock();
2118 if (right_block == left_block || cc == no_condition) {
2119 EmitGoto(left_block);
2120 } else if (left_block == next_block) {
2121 __ j(NegateCondition(cc), chunk_->GetAssemblyLabel(right_block));
2122 } else if (right_block == next_block) {
2123 __ j(cc, chunk_->GetAssemblyLabel(left_block));
2125 __ j(cc, chunk_->GetAssemblyLabel(left_block));
2127 __ jmp(chunk_->GetAssemblyLabel(right_block));
2133 template<class InstrType>
2134 void LCodeGen::EmitFalseBranch(InstrType instr, Condition cc) {
2135 int false_block = instr->FalseDestination(chunk_);
2136 __ j(cc, chunk_->GetAssemblyLabel(false_block));
2140 void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
2145 void LCodeGen::DoBranch(LBranch* instr) {
2146 Representation r = instr->hydrogen()->value()->representation();
2147 if (r.IsInteger32()) {
2148 DCHECK(!info()->IsStub());
2149 Register reg = ToRegister(instr->value());
2151 EmitBranch(instr, not_zero);
2152 } else if (r.IsSmi()) {
2153 DCHECK(!info()->IsStub());
2154 Register reg = ToRegister(instr->value());
2156 EmitBranch(instr, not_zero);
2157 } else if (r.IsDouble()) {
2158 DCHECK(!info()->IsStub());
2159 XMMRegister reg = ToDoubleRegister(instr->value());
2160 XMMRegister xmm_scratch = double_scratch0();
2161 __ xorps(xmm_scratch, xmm_scratch);
2162 __ ucomisd(reg, xmm_scratch);
2163 EmitBranch(instr, not_equal);
2165 DCHECK(r.IsTagged());
2166 Register reg = ToRegister(instr->value());
2167 HType type = instr->hydrogen()->value()->type();
2168 if (type.IsBoolean()) {
2169 DCHECK(!info()->IsStub());
2170 __ CompareRoot(reg, Heap::kTrueValueRootIndex);
2171 EmitBranch(instr, equal);
2172 } else if (type.IsSmi()) {
2173 DCHECK(!info()->IsStub());
2174 __ SmiCompare(reg, Smi::FromInt(0));
2175 EmitBranch(instr, not_equal);
2176 } else if (type.IsJSArray()) {
2177 DCHECK(!info()->IsStub());
2178 EmitBranch(instr, no_condition);
2179 } else if (type.IsHeapNumber()) {
2180 DCHECK(!info()->IsStub());
2181 XMMRegister xmm_scratch = double_scratch0();
2182 __ xorps(xmm_scratch, xmm_scratch);
2183 __ ucomisd(xmm_scratch, FieldOperand(reg, HeapNumber::kValueOffset));
2184 EmitBranch(instr, not_equal);
2185 } else if (type.IsString()) {
2186 DCHECK(!info()->IsStub());
2187 __ cmpp(FieldOperand(reg, String::kLengthOffset), Immediate(0));
2188 EmitBranch(instr, not_equal);
2190 ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
2191 // Avoid deopts in the case where we've never executed this path before.
2192 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
2194 if (expected.Contains(ToBooleanStub::UNDEFINED)) {
2195 // undefined -> false.
2196 __ CompareRoot(reg, Heap::kUndefinedValueRootIndex);
2197 __ j(equal, instr->FalseLabel(chunk_));
2199 if (expected.Contains(ToBooleanStub::BOOLEAN)) {
2201 __ CompareRoot(reg, Heap::kTrueValueRootIndex);
2202 __ j(equal, instr->TrueLabel(chunk_));
2204 __ CompareRoot(reg, Heap::kFalseValueRootIndex);
2205 __ j(equal, instr->FalseLabel(chunk_));
2207 if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
2209 __ CompareRoot(reg, Heap::kNullValueRootIndex);
2210 __ j(equal, instr->FalseLabel(chunk_));
2213 if (expected.Contains(ToBooleanStub::SMI)) {
2214 // Smis: 0 -> false, all other -> true.
2215 __ Cmp(reg, Smi::FromInt(0));
2216 __ j(equal, instr->FalseLabel(chunk_));
2217 __ JumpIfSmi(reg, instr->TrueLabel(chunk_));
2218 } else if (expected.NeedsMap()) {
2219 // If we need a map later and have a Smi -> deopt.
2220 __ testb(reg, Immediate(kSmiTagMask));
2221 DeoptimizeIf(zero, instr, Deoptimizer::kSmi);
2224 const Register map = kScratchRegister;
2225 if (expected.NeedsMap()) {
2226 __ movp(map, FieldOperand(reg, HeapObject::kMapOffset));
2228 if (expected.CanBeUndetectable()) {
2229 // Undetectable -> false.
2230 __ testb(FieldOperand(map, Map::kBitFieldOffset),
2231 Immediate(1 << Map::kIsUndetectable));
2232 __ j(not_zero, instr->FalseLabel(chunk_));
2236 if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
2237 // spec object -> true.
2238 __ CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
2239 __ j(above_equal, instr->TrueLabel(chunk_));
2242 if (expected.Contains(ToBooleanStub::STRING)) {
2243 // String value -> false iff empty.
2245 __ CmpInstanceType(map, FIRST_NONSTRING_TYPE);
2246 __ j(above_equal, ¬_string, Label::kNear);
2247 __ cmpp(FieldOperand(reg, String::kLengthOffset), Immediate(0));
2248 __ j(not_zero, instr->TrueLabel(chunk_));
2249 __ jmp(instr->FalseLabel(chunk_));
2250 __ bind(¬_string);
2253 if (expected.Contains(ToBooleanStub::SYMBOL)) {
2254 // Symbol value -> true.
2255 __ CmpInstanceType(map, SYMBOL_TYPE);
2256 __ j(equal, instr->TrueLabel(chunk_));
2259 if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
2260 // heap number -> false iff +0, -0, or NaN.
2261 Label not_heap_number;
2262 __ CompareRoot(map, Heap::kHeapNumberMapRootIndex);
2263 __ j(not_equal, ¬_heap_number, Label::kNear);
2264 XMMRegister xmm_scratch = double_scratch0();
2265 __ xorps(xmm_scratch, xmm_scratch);
2266 __ ucomisd(xmm_scratch, FieldOperand(reg, HeapNumber::kValueOffset));
2267 __ j(zero, instr->FalseLabel(chunk_));
2268 __ jmp(instr->TrueLabel(chunk_));
2269 __ bind(¬_heap_number);
2272 if (!expected.IsGeneric()) {
2273 // We've seen something for the first time -> deopt.
2274 // This can only happen if we are not generic already.
2275 DeoptimizeIf(no_condition, instr, Deoptimizer::kUnexpectedObject);
2282 void LCodeGen::EmitGoto(int block) {
2283 if (!IsNextEmittedBlock(block)) {
2284 __ jmp(chunk_->GetAssemblyLabel(chunk_->LookupDestination(block)));
2289 void LCodeGen::DoGoto(LGoto* instr) {
2290 EmitGoto(instr->block_id());
2294 inline Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
2295 Condition cond = no_condition;
2298 case Token::EQ_STRICT:
2302 case Token::NE_STRICT:
2306 cond = is_unsigned ? below : less;
2309 cond = is_unsigned ? above : greater;
2312 cond = is_unsigned ? below_equal : less_equal;
2315 cond = is_unsigned ? above_equal : greater_equal;
2318 case Token::INSTANCEOF:
2326 void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2327 LOperand* left = instr->left();
2328 LOperand* right = instr->right();
2330 instr->is_double() ||
2331 instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2332 instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2333 Condition cc = TokenToCondition(instr->op(), is_unsigned);
2335 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2336 // We can statically evaluate the comparison.
2337 double left_val = ToDouble(LConstantOperand::cast(left));
2338 double right_val = ToDouble(LConstantOperand::cast(right));
2339 int next_block = EvalComparison(instr->op(), left_val, right_val) ?
2340 instr->TrueDestination(chunk_) : instr->FalseDestination(chunk_);
2341 EmitGoto(next_block);
2343 if (instr->is_double()) {
2344 // Don't base result on EFLAGS when a NaN is involved. Instead
2345 // jump to the false block.
2346 __ ucomisd(ToDoubleRegister(left), ToDoubleRegister(right));
2347 __ j(parity_even, instr->FalseLabel(chunk_));
2350 if (right->IsConstantOperand()) {
2351 value = ToInteger32(LConstantOperand::cast(right));
2352 if (instr->hydrogen_value()->representation().IsSmi()) {
2353 __ Cmp(ToRegister(left), Smi::FromInt(value));
2355 __ cmpl(ToRegister(left), Immediate(value));
2357 } else if (left->IsConstantOperand()) {
2358 value = ToInteger32(LConstantOperand::cast(left));
2359 if (instr->hydrogen_value()->representation().IsSmi()) {
2360 if (right->IsRegister()) {
2361 __ Cmp(ToRegister(right), Smi::FromInt(value));
2363 __ Cmp(ToOperand(right), Smi::FromInt(value));
2365 } else if (right->IsRegister()) {
2366 __ cmpl(ToRegister(right), Immediate(value));
2368 __ cmpl(ToOperand(right), Immediate(value));
2370 // We commuted the operands, so commute the condition.
2371 cc = CommuteCondition(cc);
2372 } else if (instr->hydrogen_value()->representation().IsSmi()) {
2373 if (right->IsRegister()) {
2374 __ cmpp(ToRegister(left), ToRegister(right));
2376 __ cmpp(ToRegister(left), ToOperand(right));
2379 if (right->IsRegister()) {
2380 __ cmpl(ToRegister(left), ToRegister(right));
2382 __ cmpl(ToRegister(left), ToOperand(right));
2386 EmitBranch(instr, cc);
2391 void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
2392 Register left = ToRegister(instr->left());
2394 if (instr->right()->IsConstantOperand()) {
2395 Handle<Object> right = ToHandle(LConstantOperand::cast(instr->right()));
2396 __ Cmp(left, right);
2398 Register right = ToRegister(instr->right());
2399 __ cmpp(left, right);
2401 EmitBranch(instr, equal);
2405 void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) {
2406 if (instr->hydrogen()->representation().IsTagged()) {
2407 Register input_reg = ToRegister(instr->object());
2408 __ Cmp(input_reg, factory()->the_hole_value());
2409 EmitBranch(instr, equal);
2413 XMMRegister input_reg = ToDoubleRegister(instr->object());
2414 __ ucomisd(input_reg, input_reg);
2415 EmitFalseBranch(instr, parity_odd);
2417 __ subp(rsp, Immediate(kDoubleSize));
2418 __ movsd(MemOperand(rsp, 0), input_reg);
2419 __ addp(rsp, Immediate(kDoubleSize));
2421 int offset = sizeof(kHoleNanUpper32);
2422 __ cmpl(MemOperand(rsp, -offset), Immediate(kHoleNanUpper32));
2423 EmitBranch(instr, equal);
2427 void LCodeGen::DoCompareMinusZeroAndBranch(LCompareMinusZeroAndBranch* instr) {
2428 Representation rep = instr->hydrogen()->value()->representation();
2429 DCHECK(!rep.IsInteger32());
2431 if (rep.IsDouble()) {
2432 XMMRegister value = ToDoubleRegister(instr->value());
2433 XMMRegister xmm_scratch = double_scratch0();
2434 __ xorps(xmm_scratch, xmm_scratch);
2435 __ ucomisd(xmm_scratch, value);
2436 EmitFalseBranch(instr, not_equal);
2437 __ movmskpd(kScratchRegister, value);
2438 __ testl(kScratchRegister, Immediate(1));
2439 EmitBranch(instr, not_zero);
2441 Register value = ToRegister(instr->value());
2442 Handle<Map> map = masm()->isolate()->factory()->heap_number_map();
2443 __ CheckMap(value, map, instr->FalseLabel(chunk()), DO_SMI_CHECK);
2444 __ cmpl(FieldOperand(value, HeapNumber::kExponentOffset),
2446 EmitFalseBranch(instr, no_overflow);
2447 __ cmpl(FieldOperand(value, HeapNumber::kMantissaOffset),
2448 Immediate(0x00000000));
2449 EmitBranch(instr, equal);
2454 Condition LCodeGen::EmitIsObject(Register input,
2455 Label* is_not_object,
2457 DCHECK(!input.is(kScratchRegister));
2459 __ JumpIfSmi(input, is_not_object);
2461 __ CompareRoot(input, Heap::kNullValueRootIndex);
2462 __ j(equal, is_object);
2464 __ movp(kScratchRegister, FieldOperand(input, HeapObject::kMapOffset));
2465 // Undetectable objects behave like undefined.
2466 __ testb(FieldOperand(kScratchRegister, Map::kBitFieldOffset),
2467 Immediate(1 << Map::kIsUndetectable));
2468 __ j(not_zero, is_not_object);
2470 __ movzxbl(kScratchRegister,
2471 FieldOperand(kScratchRegister, Map::kInstanceTypeOffset));
2472 __ cmpb(kScratchRegister, Immediate(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2473 __ j(below, is_not_object);
2474 __ cmpb(kScratchRegister, Immediate(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
2479 void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) {
2480 Register reg = ToRegister(instr->value());
2482 Condition true_cond = EmitIsObject(
2483 reg, instr->FalseLabel(chunk_), instr->TrueLabel(chunk_));
2485 EmitBranch(instr, true_cond);
2489 Condition LCodeGen::EmitIsString(Register input,
2491 Label* is_not_string,
2492 SmiCheck check_needed = INLINE_SMI_CHECK) {
2493 if (check_needed == INLINE_SMI_CHECK) {
2494 __ JumpIfSmi(input, is_not_string);
2497 Condition cond = masm_->IsObjectStringType(input, temp1, temp1);
2503 void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
2504 Register reg = ToRegister(instr->value());
2505 Register temp = ToRegister(instr->temp());
2507 SmiCheck check_needed =
2508 instr->hydrogen()->value()->type().IsHeapObject()
2509 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2511 Condition true_cond = EmitIsString(
2512 reg, temp, instr->FalseLabel(chunk_), check_needed);
2514 EmitBranch(instr, true_cond);
2518 void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
2520 if (instr->value()->IsRegister()) {
2521 Register input = ToRegister(instr->value());
2522 is_smi = masm()->CheckSmi(input);
2524 Operand input = ToOperand(instr->value());
2525 is_smi = masm()->CheckSmi(input);
2527 EmitBranch(instr, is_smi);
2531 void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
2532 Register input = ToRegister(instr->value());
2533 Register temp = ToRegister(instr->temp());
2535 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2536 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2538 __ movp(temp, FieldOperand(input, HeapObject::kMapOffset));
2539 __ testb(FieldOperand(temp, Map::kBitFieldOffset),
2540 Immediate(1 << Map::kIsUndetectable));
2541 EmitBranch(instr, not_zero);
2545 void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
2546 DCHECK(ToRegister(instr->context()).is(rsi));
2547 Token::Value op = instr->op();
2549 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
2550 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2552 Condition condition = TokenToCondition(op, false);
2555 EmitBranch(instr, condition);
2559 static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
2560 InstanceType from = instr->from();
2561 InstanceType to = instr->to();
2562 if (from == FIRST_TYPE) return to;
2563 DCHECK(from == to || to == LAST_TYPE);
2568 static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
2569 InstanceType from = instr->from();
2570 InstanceType to = instr->to();
2571 if (from == to) return equal;
2572 if (to == LAST_TYPE) return above_equal;
2573 if (from == FIRST_TYPE) return below_equal;
2579 void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
2580 Register input = ToRegister(instr->value());
2582 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2583 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2586 __ CmpObjectType(input, TestType(instr->hydrogen()), kScratchRegister);
2587 EmitBranch(instr, BranchCondition(instr->hydrogen()));
2591 void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
2592 Register input = ToRegister(instr->value());
2593 Register result = ToRegister(instr->result());
2595 __ AssertString(input);
2597 __ movl(result, FieldOperand(input, String::kHashFieldOffset));
2598 DCHECK(String::kHashShift >= kSmiTagSize);
2599 __ IndexFromHash(result, result);
2603 void LCodeGen::DoHasCachedArrayIndexAndBranch(
2604 LHasCachedArrayIndexAndBranch* instr) {
2605 Register input = ToRegister(instr->value());
2607 __ testl(FieldOperand(input, String::kHashFieldOffset),
2608 Immediate(String::kContainsCachedArrayIndexMask));
2609 EmitBranch(instr, equal);
2613 // Branches to a label or falls through with the answer in the z flag.
2614 // Trashes the temp register.
2615 void LCodeGen::EmitClassOfTest(Label* is_true,
2617 Handle<String> class_name,
2621 DCHECK(!input.is(temp));
2622 DCHECK(!input.is(temp2));
2623 DCHECK(!temp.is(temp2));
2625 __ JumpIfSmi(input, is_false);
2627 if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
2628 // Assuming the following assertions, we can use the same compares to test
2629 // for both being a function type and being in the object type range.
2630 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
2631 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2632 FIRST_SPEC_OBJECT_TYPE + 1);
2633 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2634 LAST_SPEC_OBJECT_TYPE - 1);
2635 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
2636 __ CmpObjectType(input, FIRST_SPEC_OBJECT_TYPE, temp);
2637 __ j(below, is_false);
2638 __ j(equal, is_true);
2639 __ CmpInstanceType(temp, LAST_SPEC_OBJECT_TYPE);
2640 __ j(equal, is_true);
2642 // Faster code path to avoid two compares: subtract lower bound from the
2643 // actual type and do a signed compare with the width of the type range.
2644 __ movp(temp, FieldOperand(input, HeapObject::kMapOffset));
2645 __ movzxbl(temp2, FieldOperand(temp, Map::kInstanceTypeOffset));
2646 __ subp(temp2, Immediate(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2647 __ cmpp(temp2, Immediate(LAST_NONCALLABLE_SPEC_OBJECT_TYPE -
2648 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2649 __ j(above, is_false);
2652 // Now we are in the FIRST-LAST_NONCALLABLE_SPEC_OBJECT_TYPE range.
2653 // Check if the constructor in the map is a function.
2654 __ GetMapConstructor(temp, temp, kScratchRegister);
2656 // Objects with a non-function constructor have class 'Object'.
2657 __ CmpInstanceType(kScratchRegister, JS_FUNCTION_TYPE);
2658 if (String::Equals(class_name, isolate()->factory()->Object_string())) {
2659 __ j(not_equal, is_true);
2661 __ j(not_equal, is_false);
2664 // temp now contains the constructor function. Grab the
2665 // instance class name from there.
2666 __ movp(temp, FieldOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2667 __ movp(temp, FieldOperand(temp,
2668 SharedFunctionInfo::kInstanceClassNameOffset));
2669 // The class name we are testing against is internalized since it's a literal.
2670 // The name in the constructor is internalized because of the way the context
2671 // is booted. This routine isn't expected to work for random API-created
2672 // classes and it doesn't have to because you can't access it with natives
2673 // syntax. Since both sides are internalized it is sufficient to use an
2674 // identity comparison.
2675 DCHECK(class_name->IsInternalizedString());
2676 __ Cmp(temp, class_name);
2677 // End with the answer in the z flag.
2681 void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
2682 Register input = ToRegister(instr->value());
2683 Register temp = ToRegister(instr->temp());
2684 Register temp2 = ToRegister(instr->temp2());
2685 Handle<String> class_name = instr->hydrogen()->class_name();
2687 EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2688 class_name, input, temp, temp2);
2690 EmitBranch(instr, equal);
2694 void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
2695 Register reg = ToRegister(instr->value());
2697 __ Cmp(FieldOperand(reg, HeapObject::kMapOffset), instr->map());
2698 EmitBranch(instr, equal);
2702 void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
2703 DCHECK(ToRegister(instr->context()).is(rsi));
2704 InstanceofStub stub(isolate(), InstanceofStub::kNoFlags);
2705 __ Push(ToRegister(instr->left()));
2706 __ Push(ToRegister(instr->right()));
2707 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2708 Label true_value, done;
2710 __ j(zero, &true_value, Label::kNear);
2711 __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex);
2712 __ jmp(&done, Label::kNear);
2713 __ bind(&true_value);
2714 __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex);
2719 void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) {
2720 class DeferredInstanceOfKnownGlobal FINAL : public LDeferredCode {
2722 DeferredInstanceOfKnownGlobal(LCodeGen* codegen,
2723 LInstanceOfKnownGlobal* instr)
2724 : LDeferredCode(codegen), instr_(instr) { }
2725 void Generate() OVERRIDE {
2726 codegen()->DoDeferredInstanceOfKnownGlobal(instr_, &map_check_);
2728 LInstruction* instr() OVERRIDE { return instr_; }
2729 Label* map_check() { return &map_check_; }
2731 LInstanceOfKnownGlobal* instr_;
2735 DCHECK(ToRegister(instr->context()).is(rsi));
2736 DeferredInstanceOfKnownGlobal* deferred;
2737 deferred = new(zone()) DeferredInstanceOfKnownGlobal(this, instr);
2739 Label done, false_result;
2740 Register object = ToRegister(instr->value());
2742 // A Smi is not an instance of anything.
2743 __ JumpIfSmi(object, &false_result, Label::kNear);
2745 // This is the inlined call site instanceof cache. The two occurences of the
2746 // hole value will be patched to the last map/result pair generated by the
2749 // Use a temp register to avoid memory operands with variable lengths.
2750 Register map = ToRegister(instr->temp());
2751 __ movp(map, FieldOperand(object, HeapObject::kMapOffset));
2752 __ bind(deferred->map_check()); // Label for calculating code patching.
2753 Handle<Cell> cache_cell = factory()->NewCell(factory()->the_hole_value());
2754 __ Move(kScratchRegister, cache_cell, RelocInfo::CELL);
2755 __ cmpp(map, Operand(kScratchRegister, 0));
2756 __ j(not_equal, &cache_miss, Label::kNear);
2757 // Patched to load either true or false.
2758 __ LoadRoot(ToRegister(instr->result()), Heap::kTheHoleValueRootIndex);
2760 // Check that the code size between patch label and patch sites is invariant.
2761 Label end_of_patched_code;
2762 __ bind(&end_of_patched_code);
2765 __ jmp(&done, Label::kNear);
2767 // The inlined call site cache did not match. Check for null and string
2768 // before calling the deferred code.
2769 __ bind(&cache_miss); // Null is not an instance of anything.
2770 __ CompareRoot(object, Heap::kNullValueRootIndex);
2771 __ j(equal, &false_result, Label::kNear);
2773 // String values are not instances of anything.
2774 __ JumpIfNotString(object, kScratchRegister, deferred->entry());
2776 __ bind(&false_result);
2777 __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex);
2779 __ bind(deferred->exit());
2784 void LCodeGen::DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
2787 PushSafepointRegistersScope scope(this);
2788 InstanceofStub::Flags flags = static_cast<InstanceofStub::Flags>(
2789 InstanceofStub::kNoFlags | InstanceofStub::kCallSiteInlineCheck);
2790 InstanceofStub stub(isolate(), flags);
2792 __ Push(ToRegister(instr->value()));
2793 __ Push(instr->function());
2795 static const int kAdditionalDelta = kPointerSize == kInt64Size ? 10 : 16;
2797 masm_->SizeOfCodeGeneratedSince(map_check) + kAdditionalDelta;
2799 __ PushImm32(delta);
2801 // We are pushing three values on the stack but recording a
2802 // safepoint with two arguments because stub is going to
2803 // remove the third argument from the stack before jumping
2804 // to instanceof builtin on the slow path.
2805 CallCodeGeneric(stub.GetCode(),
2806 RelocInfo::CODE_TARGET,
2808 RECORD_SAFEPOINT_WITH_REGISTERS,
2810 DCHECK(delta == masm_->SizeOfCodeGeneratedSince(map_check));
2811 LEnvironment* env = instr->GetDeferredLazyDeoptimizationEnvironment();
2812 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
2813 // Move result to a register that survives the end of the
2814 // PushSafepointRegisterScope.
2815 __ movp(kScratchRegister, rax);
2817 __ testp(kScratchRegister, kScratchRegister);
2820 __ j(not_zero, &load_false, Label::kNear);
2821 __ LoadRoot(rax, Heap::kTrueValueRootIndex);
2822 __ jmp(&done, Label::kNear);
2823 __ bind(&load_false);
2824 __ LoadRoot(rax, Heap::kFalseValueRootIndex);
2829 void LCodeGen::DoCmpT(LCmpT* instr) {
2830 DCHECK(ToRegister(instr->context()).is(rsi));
2831 Token::Value op = instr->op();
2833 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
2834 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2836 Condition condition = TokenToCondition(op, false);
2837 Label true_value, done;
2839 __ j(condition, &true_value, Label::kNear);
2840 __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex);
2841 __ jmp(&done, Label::kNear);
2842 __ bind(&true_value);
2843 __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex);
2848 void LCodeGen::DoReturn(LReturn* instr) {
2849 if (FLAG_trace && info()->IsOptimizing()) {
2850 // Preserve the return value on the stack and rely on the runtime call
2851 // to return the value in the same register. We're leaving the code
2852 // managed by the register allocator and tearing down the frame, it's
2853 // safe to write to the context register.
2855 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
2856 __ CallRuntime(Runtime::kTraceExit, 1);
2858 if (info()->saves_caller_doubles()) {
2859 RestoreCallerDoubles();
2861 int no_frame_start = -1;
2862 if (NeedsEagerFrame()) {
2865 no_frame_start = masm_->pc_offset();
2867 if (instr->has_constant_parameter_count()) {
2868 __ Ret((ToInteger32(instr->constant_parameter_count()) + 1) * kPointerSize,
2871 DCHECK(info()->IsStub()); // Functions would need to drop one more value.
2872 Register reg = ToRegister(instr->parameter_count());
2873 // The argument count parameter is a smi
2874 __ SmiToInteger32(reg, reg);
2875 Register return_addr_reg = reg.is(rcx) ? rbx : rcx;
2876 __ PopReturnAddressTo(return_addr_reg);
2877 __ shlp(reg, Immediate(kPointerSizeLog2));
2879 __ jmp(return_addr_reg);
2881 if (no_frame_start != -1) {
2882 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
2888 void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
2889 DCHECK(FLAG_vector_ics);
2890 Register vector_register = ToRegister(instr->temp_vector());
2891 Register slot_register = VectorLoadICDescriptor::SlotRegister();
2892 DCHECK(vector_register.is(VectorLoadICDescriptor::VectorRegister()));
2893 DCHECK(slot_register.is(rax));
2895 AllowDeferredHandleDereference vector_structure_check;
2896 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2897 __ Move(vector_register, vector);
2898 // No need to allocate this register.
2899 FeedbackVectorICSlot slot = instr->hydrogen()->slot();
2900 int index = vector->GetIndex(slot);
2901 __ Move(slot_register, Smi::FromInt(index));
2905 void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
2906 DCHECK(ToRegister(instr->context()).is(rsi));
2907 DCHECK(ToRegister(instr->global_object())
2908 .is(LoadDescriptor::ReceiverRegister()));
2909 DCHECK(ToRegister(instr->result()).is(rax));
2911 __ Move(LoadDescriptor::NameRegister(), instr->name());
2912 if (FLAG_vector_ics) {
2913 EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
2915 ContextualMode mode = instr->for_typeof() ? NOT_CONTEXTUAL : CONTEXTUAL;
2916 Handle<Code> ic = CodeFactory::LoadICInOptimizedCode(isolate(), mode,
2917 PREMONOMORPHIC).code();
2918 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2922 void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
2923 Register context = ToRegister(instr->context());
2924 Register result = ToRegister(instr->result());
2925 __ movp(result, ContextOperand(context, instr->slot_index()));
2926 if (instr->hydrogen()->RequiresHoleCheck()) {
2927 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
2928 if (instr->hydrogen()->DeoptimizesOnHole()) {
2929 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
2932 __ j(not_equal, &is_not_hole, Label::kNear);
2933 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2934 __ bind(&is_not_hole);
2940 void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
2941 Register context = ToRegister(instr->context());
2942 Register value = ToRegister(instr->value());
2944 Operand target = ContextOperand(context, instr->slot_index());
2946 Label skip_assignment;
2947 if (instr->hydrogen()->RequiresHoleCheck()) {
2948 __ CompareRoot(target, Heap::kTheHoleValueRootIndex);
2949 if (instr->hydrogen()->DeoptimizesOnHole()) {
2950 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
2952 __ j(not_equal, &skip_assignment);
2955 __ movp(target, value);
2957 if (instr->hydrogen()->NeedsWriteBarrier()) {
2958 SmiCheck check_needed =
2959 instr->hydrogen()->value()->type().IsHeapObject()
2960 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2961 int offset = Context::SlotOffset(instr->slot_index());
2962 Register scratch = ToRegister(instr->temp());
2963 __ RecordWriteContextSlot(context,
2968 EMIT_REMEMBERED_SET,
2972 __ bind(&skip_assignment);
2976 void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
2977 HObjectAccess access = instr->hydrogen()->access();
2978 int offset = access.offset();
2980 if (access.IsExternalMemory()) {
2981 Register result = ToRegister(instr->result());
2982 if (instr->object()->IsConstantOperand()) {
2983 DCHECK(result.is(rax));
2984 __ load_rax(ToExternalReference(LConstantOperand::cast(instr->object())));
2986 Register object = ToRegister(instr->object());
2987 __ Load(result, MemOperand(object, offset), access.representation());
2992 Register object = ToRegister(instr->object());
2993 if (instr->hydrogen()->representation().IsDouble()) {
2994 DCHECK(access.IsInobject());
2995 XMMRegister result = ToDoubleRegister(instr->result());
2996 __ movsd(result, FieldOperand(object, offset));
3000 Register result = ToRegister(instr->result());
3001 if (!access.IsInobject()) {
3002 __ movp(result, FieldOperand(object, JSObject::kPropertiesOffset));
3006 Representation representation = access.representation();
3007 if (representation.IsSmi() && SmiValuesAre32Bits() &&
3008 instr->hydrogen()->representation().IsInteger32()) {
3009 if (FLAG_debug_code) {
3010 Register scratch = kScratchRegister;
3011 __ Load(scratch, FieldOperand(object, offset), representation);
3012 __ AssertSmi(scratch);
3015 // Read int value directly from upper half of the smi.
3016 STATIC_ASSERT(kSmiTag == 0);
3017 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
3018 offset += kPointerSize / 2;
3019 representation = Representation::Integer32();
3021 __ Load(result, FieldOperand(object, offset), representation);
3025 void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
3026 DCHECK(ToRegister(instr->context()).is(rsi));
3027 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3028 DCHECK(ToRegister(instr->result()).is(rax));
3030 __ Move(LoadDescriptor::NameRegister(), instr->name());
3031 if (FLAG_vector_ics) {
3032 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
3034 Handle<Code> ic = CodeFactory::LoadICInOptimizedCode(
3035 isolate(), NOT_CONTEXTUAL,
3036 instr->hydrogen()->initialization_state()).code();
3037 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3041 void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
3042 Register function = ToRegister(instr->function());
3043 Register result = ToRegister(instr->result());
3045 // Get the prototype or initial map from the function.
3047 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
3049 // Check that the function has a prototype or an initial map.
3050 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
3051 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
3053 // If the function does not have an initial map, we're done.
3055 __ CmpObjectType(result, MAP_TYPE, kScratchRegister);
3056 __ j(not_equal, &done, Label::kNear);
3058 // Get the prototype from the initial map.
3059 __ movp(result, FieldOperand(result, Map::kPrototypeOffset));
3066 void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
3067 Register result = ToRegister(instr->result());
3068 __ LoadRoot(result, instr->index());
3072 void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
3073 Register arguments = ToRegister(instr->arguments());
3074 Register result = ToRegister(instr->result());
3076 if (instr->length()->IsConstantOperand() &&
3077 instr->index()->IsConstantOperand()) {
3078 int32_t const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3079 int32_t const_length = ToInteger32(LConstantOperand::cast(instr->length()));
3080 if (const_index >= 0 && const_index < const_length) {
3081 StackArgumentsAccessor args(arguments, const_length,
3082 ARGUMENTS_DONT_CONTAIN_RECEIVER);
3083 __ movp(result, args.GetArgumentOperand(const_index));
3084 } else if (FLAG_debug_code) {
3088 Register length = ToRegister(instr->length());
3089 // There are two words between the frame pointer and the last argument.
3090 // Subtracting from length accounts for one of them add one more.
3091 if (instr->index()->IsRegister()) {
3092 __ subl(length, ToRegister(instr->index()));
3094 __ subl(length, ToOperand(instr->index()));
3096 StackArgumentsAccessor args(arguments, length,
3097 ARGUMENTS_DONT_CONTAIN_RECEIVER);
3098 __ movp(result, args.GetArgumentOperand(0));
3103 void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
3104 ElementsKind elements_kind = instr->elements_kind();
3105 LOperand* key = instr->key();
3106 if (kPointerSize == kInt32Size && !key->IsConstantOperand()) {
3107 Register key_reg = ToRegister(key);
3108 Representation key_representation =
3109 instr->hydrogen()->key()->representation();
3110 if (ExternalArrayOpRequiresTemp(key_representation, elements_kind)) {
3111 __ SmiToInteger64(key_reg, key_reg);
3112 } else if (instr->hydrogen()->IsDehoisted()) {
3113 // Sign extend key because it could be a 32 bit negative value
3114 // and the dehoisted address computation happens in 64 bits
3115 __ movsxlq(key_reg, key_reg);
3118 Operand operand(BuildFastArrayOperand(
3121 instr->hydrogen()->key()->representation(),
3123 instr->base_offset()));
3125 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3126 elements_kind == FLOAT32_ELEMENTS) {
3127 XMMRegister result(ToDoubleRegister(instr->result()));
3128 __ movss(result, operand);
3129 __ cvtss2sd(result, result);
3130 } else if (elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
3131 elements_kind == FLOAT64_ELEMENTS) {
3132 __ movsd(ToDoubleRegister(instr->result()), operand);
3134 Register result(ToRegister(instr->result()));
3135 switch (elements_kind) {
3136 case EXTERNAL_INT8_ELEMENTS:
3138 __ movsxbl(result, operand);
3140 case EXTERNAL_UINT8_ELEMENTS:
3141 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
3142 case UINT8_ELEMENTS:
3143 case UINT8_CLAMPED_ELEMENTS:
3144 __ movzxbl(result, operand);
3146 case EXTERNAL_INT16_ELEMENTS:
3147 case INT16_ELEMENTS:
3148 __ movsxwl(result, operand);
3150 case EXTERNAL_UINT16_ELEMENTS:
3151 case UINT16_ELEMENTS:
3152 __ movzxwl(result, operand);
3154 case EXTERNAL_INT32_ELEMENTS:
3155 case INT32_ELEMENTS:
3156 __ movl(result, operand);
3158 case EXTERNAL_UINT32_ELEMENTS:
3159 case UINT32_ELEMENTS:
3160 __ movl(result, operand);
3161 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
3162 __ testl(result, result);
3163 DeoptimizeIf(negative, instr, Deoptimizer::kNegativeValue);
3166 case EXTERNAL_FLOAT32_ELEMENTS:
3167 case EXTERNAL_FLOAT64_ELEMENTS:
3168 case FLOAT32_ELEMENTS:
3169 case FLOAT64_ELEMENTS:
3171 case FAST_SMI_ELEMENTS:
3172 case FAST_DOUBLE_ELEMENTS:
3173 case FAST_HOLEY_ELEMENTS:
3174 case FAST_HOLEY_SMI_ELEMENTS:
3175 case FAST_HOLEY_DOUBLE_ELEMENTS:
3176 case DICTIONARY_ELEMENTS:
3177 case SLOPPY_ARGUMENTS_ELEMENTS:
3185 void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
3186 XMMRegister result(ToDoubleRegister(instr->result()));
3187 LOperand* key = instr->key();
3188 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
3189 instr->hydrogen()->IsDehoisted()) {
3190 // Sign extend key because it could be a 32 bit negative value
3191 // and the dehoisted address computation happens in 64 bits
3192 __ movsxlq(ToRegister(key), ToRegister(key));
3194 if (instr->hydrogen()->RequiresHoleCheck()) {
3195 Operand hole_check_operand = BuildFastArrayOperand(
3198 instr->hydrogen()->key()->representation(),
3199 FAST_DOUBLE_ELEMENTS,
3200 instr->base_offset() + sizeof(kHoleNanLower32));
3201 __ cmpl(hole_check_operand, Immediate(kHoleNanUpper32));
3202 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
3205 Operand double_load_operand = BuildFastArrayOperand(
3208 instr->hydrogen()->key()->representation(),
3209 FAST_DOUBLE_ELEMENTS,
3210 instr->base_offset());
3211 __ movsd(result, double_load_operand);
3215 void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
3216 HLoadKeyed* hinstr = instr->hydrogen();
3217 Register result = ToRegister(instr->result());
3218 LOperand* key = instr->key();
3219 bool requires_hole_check = hinstr->RequiresHoleCheck();
3220 Representation representation = hinstr->representation();
3221 int offset = instr->base_offset();
3223 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
3224 instr->hydrogen()->IsDehoisted()) {
3225 // Sign extend key because it could be a 32 bit negative value
3226 // and the dehoisted address computation happens in 64 bits
3227 __ movsxlq(ToRegister(key), ToRegister(key));
3229 if (representation.IsInteger32() && SmiValuesAre32Bits() &&
3230 hinstr->elements_kind() == FAST_SMI_ELEMENTS) {
3231 DCHECK(!requires_hole_check);
3232 if (FLAG_debug_code) {
3233 Register scratch = kScratchRegister;
3235 BuildFastArrayOperand(instr->elements(),
3237 instr->hydrogen()->key()->representation(),
3240 Representation::Smi());
3241 __ AssertSmi(scratch);
3243 // Read int value directly from upper half of the smi.
3244 STATIC_ASSERT(kSmiTag == 0);
3245 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
3246 offset += kPointerSize / 2;
3250 BuildFastArrayOperand(instr->elements(), key,
3251 instr->hydrogen()->key()->representation(),
3252 FAST_ELEMENTS, offset),
3255 // Check for the hole value.
3256 if (requires_hole_check) {
3257 if (IsFastSmiElementsKind(hinstr->elements_kind())) {
3258 Condition smi = __ CheckSmi(result);
3259 DeoptimizeIf(NegateCondition(smi), instr, Deoptimizer::kNotASmi);
3261 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
3262 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
3268 void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
3269 if (instr->is_typed_elements()) {
3270 DoLoadKeyedExternalArray(instr);
3271 } else if (instr->hydrogen()->representation().IsDouble()) {
3272 DoLoadKeyedFixedDoubleArray(instr);
3274 DoLoadKeyedFixedArray(instr);
3279 Operand LCodeGen::BuildFastArrayOperand(
3280 LOperand* elements_pointer,
3282 Representation key_representation,
3283 ElementsKind elements_kind,
3285 Register elements_pointer_reg = ToRegister(elements_pointer);
3286 int shift_size = ElementsKindToShiftSize(elements_kind);
3287 if (key->IsConstantOperand()) {
3288 int32_t constant_value = ToInteger32(LConstantOperand::cast(key));
3289 if (constant_value & 0xF0000000) {
3290 Abort(kArrayIndexConstantValueTooBig);
3292 return Operand(elements_pointer_reg,
3293 (constant_value << shift_size) + offset);
3295 // Guaranteed by ArrayInstructionInterface::KeyedAccessIndexRequirement().
3296 DCHECK(key_representation.IsInteger32());
3298 ScaleFactor scale_factor = static_cast<ScaleFactor>(shift_size);
3299 return Operand(elements_pointer_reg,
3307 void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3308 DCHECK(ToRegister(instr->context()).is(rsi));
3309 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3310 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
3312 if (FLAG_vector_ics) {
3313 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
3317 CodeFactory::KeyedLoadICInOptimizedCode(
3318 isolate(), instr->hydrogen()->initialization_state()).code();
3319 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3323 void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
3324 Register result = ToRegister(instr->result());
3326 if (instr->hydrogen()->from_inlined()) {
3327 __ leap(result, Operand(rsp, -kFPOnStackSize + -kPCOnStackSize));
3329 // Check for arguments adapter frame.
3330 Label done, adapted;
3331 __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
3332 __ Cmp(Operand(result, StandardFrameConstants::kContextOffset),
3333 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
3334 __ j(equal, &adapted, Label::kNear);
3336 // No arguments adaptor frame.
3337 __ movp(result, rbp);
3338 __ jmp(&done, Label::kNear);
3340 // Arguments adaptor frame present.
3342 __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
3344 // Result is the frame pointer for the frame if not adapted and for the real
3345 // frame below the adaptor frame if adapted.
3351 void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
3352 Register result = ToRegister(instr->result());
3356 // If no arguments adaptor frame the number of arguments is fixed.
3357 if (instr->elements()->IsRegister()) {
3358 __ cmpp(rbp, ToRegister(instr->elements()));
3360 __ cmpp(rbp, ToOperand(instr->elements()));
3362 __ movl(result, Immediate(scope()->num_parameters()));
3363 __ j(equal, &done, Label::kNear);
3365 // Arguments adaptor frame present. Get argument length from there.
3366 __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
3367 __ SmiToInteger32(result,
3369 ArgumentsAdaptorFrameConstants::kLengthOffset));
3371 // Argument length is in result register.
3376 void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
3377 Register receiver = ToRegister(instr->receiver());
3378 Register function = ToRegister(instr->function());
3380 // If the receiver is null or undefined, we have to pass the global
3381 // object as a receiver to normal functions. Values have to be
3382 // passed unchanged to builtins and strict-mode functions.
3383 Label global_object, receiver_ok;
3384 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
3386 if (!instr->hydrogen()->known_function()) {
3387 // Do not transform the receiver to object for strict mode
3389 __ movp(kScratchRegister,
3390 FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
3391 __ testb(FieldOperand(kScratchRegister,
3392 SharedFunctionInfo::kStrictModeByteOffset),
3393 Immediate(1 << SharedFunctionInfo::kStrictModeBitWithinByte));
3394 __ j(not_equal, &receiver_ok, dist);
3396 // Do not transform the receiver to object for builtins.
3397 __ testb(FieldOperand(kScratchRegister,
3398 SharedFunctionInfo::kNativeByteOffset),
3399 Immediate(1 << SharedFunctionInfo::kNativeBitWithinByte));
3400 __ j(not_equal, &receiver_ok, dist);
3403 // Normal function. Replace undefined or null with global receiver.
3404 __ CompareRoot(receiver, Heap::kNullValueRootIndex);
3405 __ j(equal, &global_object, Label::kNear);
3406 __ CompareRoot(receiver, Heap::kUndefinedValueRootIndex);
3407 __ j(equal, &global_object, Label::kNear);
3409 // The receiver should be a JS object.
3410 Condition is_smi = __ CheckSmi(receiver);
3411 DeoptimizeIf(is_smi, instr, Deoptimizer::kSmi);
3412 __ CmpObjectType(receiver, FIRST_SPEC_OBJECT_TYPE, kScratchRegister);
3413 DeoptimizeIf(below, instr, Deoptimizer::kNotAJavaScriptObject);
3415 __ jmp(&receiver_ok, Label::kNear);
3416 __ bind(&global_object);
3417 __ movp(receiver, FieldOperand(function, JSFunction::kContextOffset));
3420 Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
3421 __ movp(receiver, FieldOperand(receiver, GlobalObject::kGlobalProxyOffset));
3423 __ bind(&receiver_ok);
3427 void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3428 Register receiver = ToRegister(instr->receiver());
3429 Register function = ToRegister(instr->function());
3430 Register length = ToRegister(instr->length());
3431 Register elements = ToRegister(instr->elements());
3432 DCHECK(receiver.is(rax)); // Used for parameter count.
3433 DCHECK(function.is(rdi)); // Required by InvokeFunction.
3434 DCHECK(ToRegister(instr->result()).is(rax));
3436 // Copy the arguments to this function possibly from the
3437 // adaptor frame below it.
3438 const uint32_t kArgumentsLimit = 1 * KB;
3439 __ cmpp(length, Immediate(kArgumentsLimit));
3440 DeoptimizeIf(above, instr, Deoptimizer::kTooManyArguments);
3443 __ movp(receiver, length);
3445 // Loop through the arguments pushing them onto the execution
3448 // length is a small non-negative integer, due to the test above.
3449 __ testl(length, length);
3450 __ j(zero, &invoke, Label::kNear);
3452 StackArgumentsAccessor args(elements, length,
3453 ARGUMENTS_DONT_CONTAIN_RECEIVER);
3454 __ Push(args.GetArgumentOperand(0));
3456 __ j(not_zero, &loop);
3458 // Invoke the function.
3460 DCHECK(instr->HasPointerMap());
3461 LPointerMap* pointers = instr->pointer_map();
3462 SafepointGenerator safepoint_generator(
3463 this, pointers, Safepoint::kLazyDeopt);
3464 ParameterCount actual(rax);
3465 __ InvokeFunction(function, actual, CALL_FUNCTION, safepoint_generator);
3469 void LCodeGen::DoPushArgument(LPushArgument* instr) {
3470 LOperand* argument = instr->value();
3471 EmitPushTaggedOperand(argument);
3475 void LCodeGen::DoDrop(LDrop* instr) {
3476 __ Drop(instr->count());
3480 void LCodeGen::DoThisFunction(LThisFunction* instr) {
3481 Register result = ToRegister(instr->result());
3482 __ movp(result, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
3486 void LCodeGen::DoContext(LContext* instr) {
3487 Register result = ToRegister(instr->result());
3488 if (info()->IsOptimizing()) {
3489 __ movp(result, Operand(rbp, StandardFrameConstants::kContextOffset));
3491 // If there is no frame, the context must be in rsi.
3492 DCHECK(result.is(rsi));
3497 void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
3498 DCHECK(ToRegister(instr->context()).is(rsi));
3499 __ Push(rsi); // The context is the first argument.
3500 __ Push(instr->hydrogen()->pairs());
3501 __ Push(Smi::FromInt(instr->hydrogen()->flags()));
3502 CallRuntime(Runtime::kDeclareGlobals, 3, instr);
3506 void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
3507 int formal_parameter_count, int arity,
3508 LInstruction* instr) {
3509 bool dont_adapt_arguments =
3510 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3511 bool can_invoke_directly =
3512 dont_adapt_arguments || formal_parameter_count == arity;
3514 Register function_reg = rdi;
3515 LPointerMap* pointers = instr->pointer_map();
3517 if (can_invoke_directly) {
3519 __ movp(rsi, FieldOperand(function_reg, JSFunction::kContextOffset));
3521 // Set rax to arguments count if adaption is not needed. Assumes that rax
3522 // is available to write to at this point.
3523 if (dont_adapt_arguments) {
3528 if (function.is_identical_to(info()->closure())) {
3531 __ Call(FieldOperand(function_reg, JSFunction::kCodeEntryOffset));
3534 // Set up deoptimization.
3535 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT, 0);
3537 // We need to adapt arguments.
3538 SafepointGenerator generator(
3539 this, pointers, Safepoint::kLazyDeopt);
3540 ParameterCount count(arity);
3541 ParameterCount expected(formal_parameter_count);
3542 __ InvokeFunction(function_reg, expected, count, CALL_FUNCTION, generator);
3547 void LCodeGen::DoTailCallThroughMegamorphicCache(
3548 LTailCallThroughMegamorphicCache* instr) {
3549 Register receiver = ToRegister(instr->receiver());
3550 Register name = ToRegister(instr->name());
3551 DCHECK(receiver.is(LoadDescriptor::ReceiverRegister()));
3552 DCHECK(name.is(LoadDescriptor::NameRegister()));
3553 Register scratch = rdi;
3554 DCHECK(!scratch.is(receiver) && !scratch.is(name));
3555 DCHECK(!FLAG_vector_ics ||
3556 !AreAliased(ToRegister(instr->slot()), ToRegister(instr->vector()),
3559 // Important for the tail-call.
3560 bool must_teardown_frame = NeedsEagerFrame();
3562 if (!instr->hydrogen()->is_just_miss()) {
3563 // The probe will tail call to a handler if found.
3564 DCHECK(!instr->hydrogen()->is_keyed_load());
3565 isolate()->stub_cache()->GenerateProbe(
3566 masm(), Code::LOAD_IC, instr->hydrogen()->flags(), must_teardown_frame,
3567 receiver, name, scratch, no_reg);
3570 // Tail call to miss if we ended up here.
3571 if (must_teardown_frame) __ leave();
3572 if (instr->hydrogen()->is_keyed_load()) {
3573 KeyedLoadIC::GenerateMiss(masm());
3575 LoadIC::GenerateMiss(masm());
3580 void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
3581 DCHECK(ToRegister(instr->result()).is(rax));
3583 if (instr->hydrogen()->IsTailCall()) {
3584 if (NeedsEagerFrame()) __ leave();
3586 if (instr->target()->IsConstantOperand()) {
3587 LConstantOperand* target = LConstantOperand::cast(instr->target());
3588 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3589 __ jmp(code, RelocInfo::CODE_TARGET);
3591 DCHECK(instr->target()->IsRegister());
3592 Register target = ToRegister(instr->target());
3593 __ addp(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
3597 LPointerMap* pointers = instr->pointer_map();
3598 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3600 if (instr->target()->IsConstantOperand()) {
3601 LConstantOperand* target = LConstantOperand::cast(instr->target());
3602 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3603 generator.BeforeCall(__ CallSize(code));
3604 __ call(code, RelocInfo::CODE_TARGET);
3606 DCHECK(instr->target()->IsRegister());
3607 Register target = ToRegister(instr->target());
3608 generator.BeforeCall(__ CallSize(target));
3609 __ addp(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
3612 generator.AfterCall();
3617 void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
3618 DCHECK(ToRegister(instr->function()).is(rdi));
3619 DCHECK(ToRegister(instr->result()).is(rax));
3621 if (instr->hydrogen()->pass_argument_count()) {
3622 __ Set(rax, instr->arity());
3626 __ movp(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
3628 LPointerMap* pointers = instr->pointer_map();
3629 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3631 bool is_self_call = false;
3632 if (instr->hydrogen()->function()->IsConstant()) {
3633 Handle<JSFunction> jsfun = Handle<JSFunction>::null();
3634 HConstant* fun_const = HConstant::cast(instr->hydrogen()->function());
3635 jsfun = Handle<JSFunction>::cast(fun_const->handle(isolate()));
3636 is_self_call = jsfun.is_identical_to(info()->closure());
3642 Operand target = FieldOperand(rdi, JSFunction::kCodeEntryOffset);
3643 generator.BeforeCall(__ CallSize(target));
3646 generator.AfterCall();
3650 void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3651 Register input_reg = ToRegister(instr->value());
3652 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
3653 Heap::kHeapNumberMapRootIndex);
3654 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
3656 Label slow, allocated, done;
3657 Register tmp = input_reg.is(rax) ? rcx : rax;
3658 Register tmp2 = tmp.is(rcx) ? rdx : input_reg.is(rcx) ? rdx : rcx;
3660 // Preserve the value of all registers.
3661 PushSafepointRegistersScope scope(this);
3663 __ movl(tmp, FieldOperand(input_reg, HeapNumber::kExponentOffset));
3664 // Check the sign of the argument. If the argument is positive, just
3665 // return it. We do not need to patch the stack since |input| and
3666 // |result| are the same register and |input| will be restored
3667 // unchanged by popping safepoint registers.
3668 __ testl(tmp, Immediate(HeapNumber::kSignMask));
3671 __ AllocateHeapNumber(tmp, tmp2, &slow);
3672 __ jmp(&allocated, Label::kNear);
3674 // Slow case: Call the runtime system to do the number allocation.
3676 CallRuntimeFromDeferred(
3677 Runtime::kAllocateHeapNumber, 0, instr, instr->context());
3678 // Set the pointer to the new heap number in tmp.
3679 if (!tmp.is(rax)) __ movp(tmp, rax);
3680 // Restore input_reg after call to runtime.
3681 __ LoadFromSafepointRegisterSlot(input_reg, input_reg);
3683 __ bind(&allocated);
3684 __ movq(tmp2, FieldOperand(input_reg, HeapNumber::kValueOffset));
3685 __ shlq(tmp2, Immediate(1));
3686 __ shrq(tmp2, Immediate(1));
3687 __ movq(FieldOperand(tmp, HeapNumber::kValueOffset), tmp2);
3688 __ StoreToSafepointRegisterSlot(input_reg, tmp);
3694 void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3695 Register input_reg = ToRegister(instr->value());
3696 __ testl(input_reg, input_reg);
3698 __ j(not_sign, &is_positive, Label::kNear);
3699 __ negl(input_reg); // Sets flags.
3700 DeoptimizeIf(negative, instr, Deoptimizer::kOverflow);
3701 __ bind(&is_positive);
3705 void LCodeGen::EmitSmiMathAbs(LMathAbs* instr) {
3706 Register input_reg = ToRegister(instr->value());
3707 __ testp(input_reg, input_reg);
3709 __ j(not_sign, &is_positive, Label::kNear);
3710 __ negp(input_reg); // Sets flags.
3711 DeoptimizeIf(negative, instr, Deoptimizer::kOverflow);
3712 __ bind(&is_positive);
3716 void LCodeGen::DoMathAbs(LMathAbs* instr) {
3717 // Class for deferred case.
3718 class DeferredMathAbsTaggedHeapNumber FINAL : public LDeferredCode {
3720 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr)
3721 : LDeferredCode(codegen), instr_(instr) { }
3722 void Generate() OVERRIDE {
3723 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3725 LInstruction* instr() OVERRIDE { return instr_; }
3731 DCHECK(instr->value()->Equals(instr->result()));
3732 Representation r = instr->hydrogen()->value()->representation();
3735 XMMRegister scratch = double_scratch0();
3736 XMMRegister input_reg = ToDoubleRegister(instr->value());
3737 __ xorps(scratch, scratch);
3738 __ subsd(scratch, input_reg);
3739 __ andps(input_reg, scratch);
3740 } else if (r.IsInteger32()) {
3741 EmitIntegerMathAbs(instr);
3742 } else if (r.IsSmi()) {
3743 EmitSmiMathAbs(instr);
3744 } else { // Tagged case.
3745 DeferredMathAbsTaggedHeapNumber* deferred =
3746 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3747 Register input_reg = ToRegister(instr->value());
3749 __ JumpIfNotSmi(input_reg, deferred->entry());
3750 EmitSmiMathAbs(instr);
3751 __ bind(deferred->exit());
3756 void LCodeGen::DoMathFloor(LMathFloor* instr) {
3757 XMMRegister xmm_scratch = double_scratch0();
3758 Register output_reg = ToRegister(instr->result());
3759 XMMRegister input_reg = ToDoubleRegister(instr->value());
3761 if (CpuFeatures::IsSupported(SSE4_1)) {
3762 CpuFeatureScope scope(masm(), SSE4_1);
3763 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3764 // Deoptimize if minus zero.
3765 __ movq(output_reg, input_reg);
3766 __ subq(output_reg, Immediate(1));
3767 DeoptimizeIf(overflow, instr, Deoptimizer::kMinusZero);
3769 __ roundsd(xmm_scratch, input_reg, kRoundDown);
3770 __ cvttsd2si(output_reg, xmm_scratch);
3771 __ cmpl(output_reg, Immediate(0x1));
3772 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3774 Label negative_sign, done;
3775 // Deoptimize on unordered.
3776 __ xorps(xmm_scratch, xmm_scratch); // Zero the register.
3777 __ ucomisd(input_reg, xmm_scratch);
3778 DeoptimizeIf(parity_even, instr, Deoptimizer::kNaN);
3779 __ j(below, &negative_sign, Label::kNear);
3781 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3782 // Check for negative zero.
3783 Label positive_sign;
3784 __ j(above, &positive_sign, Label::kNear);
3785 __ movmskpd(output_reg, input_reg);
3786 __ testq(output_reg, Immediate(1));
3787 DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero);
3788 __ Set(output_reg, 0);
3790 __ bind(&positive_sign);
3793 // Use truncating instruction (OK because input is positive).
3794 __ cvttsd2si(output_reg, input_reg);
3795 // Overflow is signalled with minint.
3796 __ cmpl(output_reg, Immediate(0x1));
3797 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3798 __ jmp(&done, Label::kNear);
3800 // Non-zero negative reaches here.
3801 __ bind(&negative_sign);
3802 // Truncate, then compare and compensate.
3803 __ cvttsd2si(output_reg, input_reg);
3804 __ Cvtlsi2sd(xmm_scratch, output_reg);
3805 __ ucomisd(input_reg, xmm_scratch);
3806 __ j(equal, &done, Label::kNear);
3807 __ subl(output_reg, Immediate(1));
3808 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3815 void LCodeGen::DoMathRound(LMathRound* instr) {
3816 const XMMRegister xmm_scratch = double_scratch0();
3817 Register output_reg = ToRegister(instr->result());
3818 XMMRegister input_reg = ToDoubleRegister(instr->value());
3819 XMMRegister input_temp = ToDoubleRegister(instr->temp());
3820 static int64_t one_half = V8_INT64_C(0x3FE0000000000000); // 0.5
3821 static int64_t minus_one_half = V8_INT64_C(0xBFE0000000000000); // -0.5
3823 Label done, round_to_zero, below_one_half;
3824 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
3825 __ movq(kScratchRegister, one_half);
3826 __ movq(xmm_scratch, kScratchRegister);
3827 __ ucomisd(xmm_scratch, input_reg);
3828 __ j(above, &below_one_half, Label::kNear);
3830 // CVTTSD2SI rounds towards zero, since 0.5 <= x, we use floor(0.5 + x).
3831 __ addsd(xmm_scratch, input_reg);
3832 __ cvttsd2si(output_reg, xmm_scratch);
3833 // Overflow is signalled with minint.
3834 __ cmpl(output_reg, Immediate(0x1));
3835 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3836 __ jmp(&done, dist);
3838 __ bind(&below_one_half);
3839 __ movq(kScratchRegister, minus_one_half);
3840 __ movq(xmm_scratch, kScratchRegister);
3841 __ ucomisd(xmm_scratch, input_reg);
3842 __ j(below_equal, &round_to_zero, Label::kNear);
3844 // CVTTSD2SI rounds towards zero, we use ceil(x - (-0.5)) and then
3845 // compare and compensate.
3846 __ movq(input_temp, input_reg); // Do not alter input_reg.
3847 __ subsd(input_temp, xmm_scratch);
3848 __ cvttsd2si(output_reg, input_temp);
3849 // Catch minint due to overflow, and to prevent overflow when compensating.
3850 __ cmpl(output_reg, Immediate(0x1));
3851 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3853 __ Cvtlsi2sd(xmm_scratch, output_reg);
3854 __ ucomisd(xmm_scratch, input_temp);
3855 __ j(equal, &done, dist);
3856 __ subl(output_reg, Immediate(1));
3857 // No overflow because we already ruled out minint.
3858 __ jmp(&done, dist);
3860 __ bind(&round_to_zero);
3861 // We return 0 for the input range [+0, 0.5[, or [-0.5, 0.5[ if
3862 // we can ignore the difference between a result of -0 and +0.
3863 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3864 __ movq(output_reg, input_reg);
3865 __ testq(output_reg, output_reg);
3866 DeoptimizeIf(negative, instr, Deoptimizer::kMinusZero);
3868 __ Set(output_reg, 0);
3873 void LCodeGen::DoMathFround(LMathFround* instr) {
3874 XMMRegister input_reg = ToDoubleRegister(instr->value());
3875 XMMRegister output_reg = ToDoubleRegister(instr->result());
3876 __ cvtsd2ss(output_reg, input_reg);
3877 __ cvtss2sd(output_reg, output_reg);
3881 void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3882 XMMRegister output = ToDoubleRegister(instr->result());
3883 if (instr->value()->IsDoubleRegister()) {
3884 XMMRegister input = ToDoubleRegister(instr->value());
3885 __ sqrtsd(output, input);
3887 Operand input = ToOperand(instr->value());
3888 __ sqrtsd(output, input);
3893 void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3894 XMMRegister xmm_scratch = double_scratch0();
3895 XMMRegister input_reg = ToDoubleRegister(instr->value());
3896 DCHECK(ToDoubleRegister(instr->result()).is(input_reg));
3898 // Note that according to ECMA-262 15.8.2.13:
3899 // Math.pow(-Infinity, 0.5) == Infinity
3900 // Math.sqrt(-Infinity) == NaN
3902 // Check base for -Infinity. According to IEEE-754, double-precision
3903 // -Infinity has the highest 12 bits set and the lowest 52 bits cleared.
3904 __ movq(kScratchRegister, V8_INT64_C(0xFFF0000000000000));
3905 __ movq(xmm_scratch, kScratchRegister);
3906 __ ucomisd(xmm_scratch, input_reg);
3907 // Comparing -Infinity with NaN results in "unordered", which sets the
3908 // zero flag as if both were equal. However, it also sets the carry flag.
3909 __ j(not_equal, &sqrt, Label::kNear);
3910 __ j(carry, &sqrt, Label::kNear);
3911 // If input is -Infinity, return Infinity.
3912 __ xorps(input_reg, input_reg);
3913 __ subsd(input_reg, xmm_scratch);
3914 __ jmp(&done, Label::kNear);
3918 __ xorps(xmm_scratch, xmm_scratch);
3919 __ addsd(input_reg, xmm_scratch); // Convert -0 to +0.
3920 __ sqrtsd(input_reg, input_reg);
3925 void LCodeGen::DoPower(LPower* instr) {
3926 Representation exponent_type = instr->hydrogen()->right()->representation();
3927 // Having marked this as a call, we can use any registers.
3928 // Just make sure that the input/output registers are the expected ones.
3930 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3931 DCHECK(!instr->right()->IsRegister() ||
3932 ToRegister(instr->right()).is(tagged_exponent));
3933 DCHECK(!instr->right()->IsDoubleRegister() ||
3934 ToDoubleRegister(instr->right()).is(xmm1));
3935 DCHECK(ToDoubleRegister(instr->left()).is(xmm2));
3936 DCHECK(ToDoubleRegister(instr->result()).is(xmm3));
3938 if (exponent_type.IsSmi()) {
3939 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3941 } else if (exponent_type.IsTagged()) {
3943 __ JumpIfSmi(tagged_exponent, &no_deopt, Label::kNear);
3944 __ CmpObjectType(tagged_exponent, HEAP_NUMBER_TYPE, rcx);
3945 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
3947 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3949 } else if (exponent_type.IsInteger32()) {
3950 MathPowStub stub(isolate(), MathPowStub::INTEGER);
3953 DCHECK(exponent_type.IsDouble());
3954 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
3960 void LCodeGen::DoMathExp(LMathExp* instr) {
3961 XMMRegister input = ToDoubleRegister(instr->value());
3962 XMMRegister result = ToDoubleRegister(instr->result());
3963 XMMRegister temp0 = double_scratch0();
3964 Register temp1 = ToRegister(instr->temp1());
3965 Register temp2 = ToRegister(instr->temp2());
3967 MathExpGenerator::EmitMathExp(masm(), input, result, temp0, temp1, temp2);
3971 void LCodeGen::DoMathLog(LMathLog* instr) {
3972 DCHECK(instr->value()->Equals(instr->result()));
3973 XMMRegister input_reg = ToDoubleRegister(instr->value());
3974 XMMRegister xmm_scratch = double_scratch0();
3975 Label positive, done, zero;
3976 __ xorps(xmm_scratch, xmm_scratch);
3977 __ ucomisd(input_reg, xmm_scratch);
3978 __ j(above, &positive, Label::kNear);
3979 __ j(not_carry, &zero, Label::kNear);
3980 __ pcmpeqd(input_reg, input_reg);
3981 __ jmp(&done, Label::kNear);
3983 ExternalReference ninf =
3984 ExternalReference::address_of_negative_infinity();
3985 Operand ninf_operand = masm()->ExternalOperand(ninf);
3986 __ movsd(input_reg, ninf_operand);
3987 __ jmp(&done, Label::kNear);
3990 __ subp(rsp, Immediate(kDoubleSize));
3991 __ movsd(Operand(rsp, 0), input_reg);
3992 __ fld_d(Operand(rsp, 0));
3994 __ fstp_d(Operand(rsp, 0));
3995 __ movsd(input_reg, Operand(rsp, 0));
3996 __ addp(rsp, Immediate(kDoubleSize));
4001 void LCodeGen::DoMathClz32(LMathClz32* instr) {
4002 Register input = ToRegister(instr->value());
4003 Register result = ToRegister(instr->result());
4005 __ Lzcntl(result, input);
4009 void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
4010 DCHECK(ToRegister(instr->context()).is(rsi));
4011 DCHECK(ToRegister(instr->function()).is(rdi));
4012 DCHECK(instr->HasPointerMap());
4014 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
4015 if (known_function.is_null()) {
4016 LPointerMap* pointers = instr->pointer_map();
4017 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
4018 ParameterCount count(instr->arity());
4019 __ InvokeFunction(rdi, count, CALL_FUNCTION, generator);
4021 CallKnownFunction(known_function,
4022 instr->hydrogen()->formal_parameter_count(),
4023 instr->arity(), instr);
4028 void LCodeGen::DoCallFunction(LCallFunction* instr) {
4029 DCHECK(ToRegister(instr->context()).is(rsi));
4030 DCHECK(ToRegister(instr->function()).is(rdi));
4031 DCHECK(ToRegister(instr->result()).is(rax));
4033 int arity = instr->arity();
4034 CallFunctionFlags flags = instr->hydrogen()->function_flags();
4035 if (instr->hydrogen()->HasVectorAndSlot()) {
4036 Register slot_register = ToRegister(instr->temp_slot());
4037 Register vector_register = ToRegister(instr->temp_vector());
4038 DCHECK(slot_register.is(rdx));
4039 DCHECK(vector_register.is(rbx));
4041 AllowDeferredHandleDereference vector_structure_check;
4042 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
4043 int index = vector->GetIndex(instr->hydrogen()->slot());
4045 __ Move(vector_register, vector);
4046 __ Move(slot_register, Smi::FromInt(index));
4048 CallICState::CallType call_type =
4049 (flags & CALL_AS_METHOD) ? CallICState::METHOD : CallICState::FUNCTION;
4052 CodeFactory::CallICInOptimizedCode(isolate(), arity, call_type).code();
4053 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4055 CallFunctionStub stub(isolate(), arity, flags);
4056 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4061 void LCodeGen::DoCallNew(LCallNew* instr) {
4062 DCHECK(ToRegister(instr->context()).is(rsi));
4063 DCHECK(ToRegister(instr->constructor()).is(rdi));
4064 DCHECK(ToRegister(instr->result()).is(rax));
4066 __ Set(rax, instr->arity());
4067 // No cell in ebx for construct type feedback in optimized code
4068 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
4069 CallConstructStub stub(isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
4070 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4074 void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
4075 DCHECK(ToRegister(instr->context()).is(rsi));
4076 DCHECK(ToRegister(instr->constructor()).is(rdi));
4077 DCHECK(ToRegister(instr->result()).is(rax));
4079 __ Set(rax, instr->arity());
4080 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
4081 ElementsKind kind = instr->hydrogen()->elements_kind();
4082 AllocationSiteOverrideMode override_mode =
4083 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
4084 ? DISABLE_ALLOCATION_SITES
4087 if (instr->arity() == 0) {
4088 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
4089 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4090 } else if (instr->arity() == 1) {
4092 if (IsFastPackedElementsKind(kind)) {
4094 // We might need a change here
4095 // look at the first argument
4096 __ movp(rcx, Operand(rsp, 0));
4098 __ j(zero, &packed_case, Label::kNear);
4100 ElementsKind holey_kind = GetHoleyElementsKind(kind);
4101 ArraySingleArgumentConstructorStub stub(isolate(),
4104 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4105 __ jmp(&done, Label::kNear);
4106 __ bind(&packed_case);
4109 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
4110 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4113 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
4114 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4119 void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
4120 DCHECK(ToRegister(instr->context()).is(rsi));
4121 CallRuntime(instr->function(), instr->arity(), instr, instr->save_doubles());
4125 void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
4126 Register function = ToRegister(instr->function());
4127 Register code_object = ToRegister(instr->code_object());
4128 __ leap(code_object, FieldOperand(code_object, Code::kHeaderSize));
4129 __ movp(FieldOperand(function, JSFunction::kCodeEntryOffset), code_object);
4133 void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
4134 Register result = ToRegister(instr->result());
4135 Register base = ToRegister(instr->base_object());
4136 if (instr->offset()->IsConstantOperand()) {
4137 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
4138 __ leap(result, Operand(base, ToInteger32(offset)));
4140 Register offset = ToRegister(instr->offset());
4141 __ leap(result, Operand(base, offset, times_1, 0));
4146 void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
4147 HStoreNamedField* hinstr = instr->hydrogen();
4148 Representation representation = instr->representation();
4150 HObjectAccess access = hinstr->access();
4151 int offset = access.offset();
4153 if (access.IsExternalMemory()) {
4154 DCHECK(!hinstr->NeedsWriteBarrier());
4155 Register value = ToRegister(instr->value());
4156 if (instr->object()->IsConstantOperand()) {
4157 DCHECK(value.is(rax));
4158 LConstantOperand* object = LConstantOperand::cast(instr->object());
4159 __ store_rax(ToExternalReference(object));
4161 Register object = ToRegister(instr->object());
4162 __ Store(MemOperand(object, offset), value, representation);
4167 Register object = ToRegister(instr->object());
4168 __ AssertNotSmi(object);
4170 DCHECK(!representation.IsSmi() ||
4171 !instr->value()->IsConstantOperand() ||
4172 IsInteger32Constant(LConstantOperand::cast(instr->value())));
4173 if (!FLAG_unbox_double_fields && representation.IsDouble()) {
4174 DCHECK(access.IsInobject());
4175 DCHECK(!hinstr->has_transition());
4176 DCHECK(!hinstr->NeedsWriteBarrier());
4177 XMMRegister value = ToDoubleRegister(instr->value());
4178 __ movsd(FieldOperand(object, offset), value);
4182 if (hinstr->has_transition()) {
4183 Handle<Map> transition = hinstr->transition_map();
4184 AddDeprecationDependency(transition);
4185 if (!hinstr->NeedsWriteBarrierForMap()) {
4186 __ Move(FieldOperand(object, HeapObject::kMapOffset), transition);
4188 Register temp = ToRegister(instr->temp());
4189 __ Move(kScratchRegister, transition);
4190 __ movp(FieldOperand(object, HeapObject::kMapOffset), kScratchRegister);
4191 // Update the write barrier for the map field.
4192 __ RecordWriteForMap(object,
4200 Register write_register = object;
4201 if (!access.IsInobject()) {
4202 write_register = ToRegister(instr->temp());
4203 __ movp(write_register, FieldOperand(object, JSObject::kPropertiesOffset));
4206 if (representation.IsSmi() && SmiValuesAre32Bits() &&
4207 hinstr->value()->representation().IsInteger32()) {
4208 DCHECK(hinstr->store_mode() == STORE_TO_INITIALIZED_ENTRY);
4209 if (FLAG_debug_code) {
4210 Register scratch = kScratchRegister;
4211 __ Load(scratch, FieldOperand(write_register, offset), representation);
4212 __ AssertSmi(scratch);
4214 // Store int value directly to upper half of the smi.
4215 STATIC_ASSERT(kSmiTag == 0);
4216 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
4217 offset += kPointerSize / 2;
4218 representation = Representation::Integer32();
4221 Operand operand = FieldOperand(write_register, offset);
4223 if (FLAG_unbox_double_fields && representation.IsDouble()) {
4224 DCHECK(access.IsInobject());
4225 XMMRegister value = ToDoubleRegister(instr->value());
4226 __ movsd(operand, value);
4228 } else if (instr->value()->IsRegister()) {
4229 Register value = ToRegister(instr->value());
4230 __ Store(operand, value, representation);
4232 LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
4233 if (IsInteger32Constant(operand_value)) {
4234 DCHECK(!hinstr->NeedsWriteBarrier());
4235 int32_t value = ToInteger32(operand_value);
4236 if (representation.IsSmi()) {
4237 __ Move(operand, Smi::FromInt(value));
4240 __ movl(operand, Immediate(value));
4244 Handle<Object> handle_value = ToHandle(operand_value);
4245 DCHECK(!hinstr->NeedsWriteBarrier());
4246 __ Move(operand, handle_value);
4250 if (hinstr->NeedsWriteBarrier()) {
4251 Register value = ToRegister(instr->value());
4252 Register temp = access.IsInobject() ? ToRegister(instr->temp()) : object;
4253 // Update the write barrier for the object for in-object properties.
4254 __ RecordWriteField(write_register,
4259 EMIT_REMEMBERED_SET,
4260 hinstr->SmiCheckForWriteBarrier(),
4261 hinstr->PointersToHereCheckForValue());
4266 void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
4267 DCHECK(ToRegister(instr->context()).is(rsi));
4268 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4269 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4271 __ Move(StoreDescriptor::NameRegister(), instr->hydrogen()->name());
4273 StoreIC::initialize_stub(isolate(), instr->language_mode(),
4274 instr->hydrogen()->initialization_state());
4275 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4279 void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
4280 Representation representation = instr->hydrogen()->length()->representation();
4281 DCHECK(representation.Equals(instr->hydrogen()->index()->representation()));
4282 DCHECK(representation.IsSmiOrInteger32());
4284 Condition cc = instr->hydrogen()->allow_equality() ? below : below_equal;
4285 if (instr->length()->IsConstantOperand()) {
4286 int32_t length = ToInteger32(LConstantOperand::cast(instr->length()));
4287 Register index = ToRegister(instr->index());
4288 if (representation.IsSmi()) {
4289 __ Cmp(index, Smi::FromInt(length));
4291 __ cmpl(index, Immediate(length));
4293 cc = CommuteCondition(cc);
4294 } else if (instr->index()->IsConstantOperand()) {
4295 int32_t index = ToInteger32(LConstantOperand::cast(instr->index()));
4296 if (instr->length()->IsRegister()) {
4297 Register length = ToRegister(instr->length());
4298 if (representation.IsSmi()) {
4299 __ Cmp(length, Smi::FromInt(index));
4301 __ cmpl(length, Immediate(index));
4304 Operand length = ToOperand(instr->length());
4305 if (representation.IsSmi()) {
4306 __ Cmp(length, Smi::FromInt(index));
4308 __ cmpl(length, Immediate(index));
4312 Register index = ToRegister(instr->index());
4313 if (instr->length()->IsRegister()) {
4314 Register length = ToRegister(instr->length());
4315 if (representation.IsSmi()) {
4316 __ cmpp(length, index);
4318 __ cmpl(length, index);
4321 Operand length = ToOperand(instr->length());
4322 if (representation.IsSmi()) {
4323 __ cmpp(length, index);
4325 __ cmpl(length, index);
4329 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
4331 __ j(NegateCondition(cc), &done, Label::kNear);
4335 DeoptimizeIf(cc, instr, Deoptimizer::kOutOfBounds);
4340 void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
4341 ElementsKind elements_kind = instr->elements_kind();
4342 LOperand* key = instr->key();
4343 if (kPointerSize == kInt32Size && !key->IsConstantOperand()) {
4344 Register key_reg = ToRegister(key);
4345 Representation key_representation =
4346 instr->hydrogen()->key()->representation();
4347 if (ExternalArrayOpRequiresTemp(key_representation, elements_kind)) {
4348 __ SmiToInteger64(key_reg, key_reg);
4349 } else if (instr->hydrogen()->IsDehoisted()) {
4350 // Sign extend key because it could be a 32 bit negative value
4351 // and the dehoisted address computation happens in 64 bits
4352 __ movsxlq(key_reg, key_reg);
4355 Operand operand(BuildFastArrayOperand(
4358 instr->hydrogen()->key()->representation(),
4360 instr->base_offset()));
4362 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4363 elements_kind == FLOAT32_ELEMENTS) {
4364 XMMRegister value(ToDoubleRegister(instr->value()));
4365 __ cvtsd2ss(value, value);
4366 __ movss(operand, value);
4367 } else if (elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
4368 elements_kind == FLOAT64_ELEMENTS) {
4369 __ movsd(operand, ToDoubleRegister(instr->value()));
4371 Register value(ToRegister(instr->value()));
4372 switch (elements_kind) {
4373 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
4374 case EXTERNAL_INT8_ELEMENTS:
4375 case EXTERNAL_UINT8_ELEMENTS:
4377 case UINT8_ELEMENTS:
4378 case UINT8_CLAMPED_ELEMENTS:
4379 __ movb(operand, value);
4381 case EXTERNAL_INT16_ELEMENTS:
4382 case EXTERNAL_UINT16_ELEMENTS:
4383 case INT16_ELEMENTS:
4384 case UINT16_ELEMENTS:
4385 __ movw(operand, value);
4387 case EXTERNAL_INT32_ELEMENTS:
4388 case EXTERNAL_UINT32_ELEMENTS:
4389 case INT32_ELEMENTS:
4390 case UINT32_ELEMENTS:
4391 __ movl(operand, value);
4393 case EXTERNAL_FLOAT32_ELEMENTS:
4394 case EXTERNAL_FLOAT64_ELEMENTS:
4395 case FLOAT32_ELEMENTS:
4396 case FLOAT64_ELEMENTS:
4398 case FAST_SMI_ELEMENTS:
4399 case FAST_DOUBLE_ELEMENTS:
4400 case FAST_HOLEY_ELEMENTS:
4401 case FAST_HOLEY_SMI_ELEMENTS:
4402 case FAST_HOLEY_DOUBLE_ELEMENTS:
4403 case DICTIONARY_ELEMENTS:
4404 case SLOPPY_ARGUMENTS_ELEMENTS:
4412 void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
4413 XMMRegister value = ToDoubleRegister(instr->value());
4414 LOperand* key = instr->key();
4415 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
4416 instr->hydrogen()->IsDehoisted()) {
4417 // Sign extend key because it could be a 32 bit negative value
4418 // and the dehoisted address computation happens in 64 bits
4419 __ movsxlq(ToRegister(key), ToRegister(key));
4421 if (instr->NeedsCanonicalization()) {
4422 XMMRegister xmm_scratch = double_scratch0();
4423 // Turn potential sNaN value into qNaN.
4424 __ xorps(xmm_scratch, xmm_scratch);
4425 __ subsd(value, xmm_scratch);
4428 Operand double_store_operand = BuildFastArrayOperand(
4431 instr->hydrogen()->key()->representation(),
4432 FAST_DOUBLE_ELEMENTS,
4433 instr->base_offset());
4435 __ movsd(double_store_operand, value);
4439 void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4440 HStoreKeyed* hinstr = instr->hydrogen();
4441 LOperand* key = instr->key();
4442 int offset = instr->base_offset();
4443 Representation representation = hinstr->value()->representation();
4445 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
4446 instr->hydrogen()->IsDehoisted()) {
4447 // Sign extend key because it could be a 32 bit negative value
4448 // and the dehoisted address computation happens in 64 bits
4449 __ movsxlq(ToRegister(key), ToRegister(key));
4451 if (representation.IsInteger32() && SmiValuesAre32Bits()) {
4452 DCHECK(hinstr->store_mode() == STORE_TO_INITIALIZED_ENTRY);
4453 DCHECK(hinstr->elements_kind() == FAST_SMI_ELEMENTS);
4454 if (FLAG_debug_code) {
4455 Register scratch = kScratchRegister;
4457 BuildFastArrayOperand(instr->elements(),
4459 instr->hydrogen()->key()->representation(),
4462 Representation::Smi());
4463 __ AssertSmi(scratch);
4465 // Store int value directly to upper half of the smi.
4466 STATIC_ASSERT(kSmiTag == 0);
4467 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
4468 offset += kPointerSize / 2;
4472 BuildFastArrayOperand(instr->elements(),
4474 instr->hydrogen()->key()->representation(),
4477 if (instr->value()->IsRegister()) {
4478 __ Store(operand, ToRegister(instr->value()), representation);
4480 LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
4481 if (IsInteger32Constant(operand_value)) {
4482 int32_t value = ToInteger32(operand_value);
4483 if (representation.IsSmi()) {
4484 __ Move(operand, Smi::FromInt(value));
4487 __ movl(operand, Immediate(value));
4490 Handle<Object> handle_value = ToHandle(operand_value);
4491 __ Move(operand, handle_value);
4495 if (hinstr->NeedsWriteBarrier()) {
4496 Register elements = ToRegister(instr->elements());
4497 DCHECK(instr->value()->IsRegister());
4498 Register value = ToRegister(instr->value());
4499 DCHECK(!key->IsConstantOperand());
4500 SmiCheck check_needed = hinstr->value()->type().IsHeapObject()
4501 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4502 // Compute address of modified element and store it into key register.
4503 Register key_reg(ToRegister(key));
4504 __ leap(key_reg, operand);
4505 __ RecordWrite(elements,
4509 EMIT_REMEMBERED_SET,
4511 hinstr->PointersToHereCheckForValue());
4516 void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
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(rsi));
4529 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4530 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4531 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4533 Handle<Code> ic = CodeFactory::KeyedStoreICInOptimizedCode(
4534 isolate(), instr->language_mode(),
4535 instr->hydrogen()->initialization_state()).code();
4536 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4540 void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4541 Register object_reg = ToRegister(instr->object());
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 __ Cmp(FieldOperand(object_reg, HeapObject::kMapOffset), from_map);
4550 __ j(not_equal, ¬_applicable);
4551 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
4552 Register new_map_reg = ToRegister(instr->new_map_temp());
4553 __ Move(new_map_reg, to_map, RelocInfo::EMBEDDED_OBJECT);
4554 __ movp(FieldOperand(object_reg, HeapObject::kMapOffset), new_map_reg);
4556 __ RecordWriteForMap(object_reg, new_map_reg, ToRegister(instr->temp()),
4559 DCHECK(object_reg.is(rax));
4560 DCHECK(ToRegister(instr->context()).is(rsi));
4561 PushSafepointRegistersScope scope(this);
4562 __ Move(rbx, to_map);
4563 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4564 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4566 RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0);
4568 __ bind(¬_applicable);
4572 void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4573 Register object = ToRegister(instr->object());
4574 Register temp = ToRegister(instr->temp());
4575 Label no_memento_found;
4576 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found);
4577 DeoptimizeIf(equal, instr, Deoptimizer::kMementoFound);
4578 __ bind(&no_memento_found);
4582 void LCodeGen::DoStringAdd(LStringAdd* instr) {
4583 DCHECK(ToRegister(instr->context()).is(rsi));
4584 DCHECK(ToRegister(instr->left()).is(rdx));
4585 DCHECK(ToRegister(instr->right()).is(rax));
4586 StringAddStub stub(isolate(),
4587 instr->hydrogen()->flags(),
4588 instr->hydrogen()->pretenure_flag());
4589 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4593 void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
4594 class DeferredStringCharCodeAt FINAL : public LDeferredCode {
4596 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
4597 : LDeferredCode(codegen), instr_(instr) { }
4598 void Generate() OVERRIDE { codegen()->DoDeferredStringCharCodeAt(instr_); }
4599 LInstruction* instr() OVERRIDE { return instr_; }
4602 LStringCharCodeAt* instr_;
4605 DeferredStringCharCodeAt* deferred =
4606 new(zone()) DeferredStringCharCodeAt(this, instr);
4608 StringCharLoadGenerator::Generate(masm(),
4609 ToRegister(instr->string()),
4610 ToRegister(instr->index()),
4611 ToRegister(instr->result()),
4613 __ bind(deferred->exit());
4617 void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4618 Register string = ToRegister(instr->string());
4619 Register result = ToRegister(instr->result());
4621 // TODO(3095996): Get rid of this. For now, we need to make the
4622 // result register contain a valid pointer because it is already
4623 // contained in the register pointer map.
4626 PushSafepointRegistersScope scope(this);
4628 // Push the index as a smi. This is safe because of the checks in
4629 // DoStringCharCodeAt above.
4630 STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue);
4631 if (instr->index()->IsConstantOperand()) {
4632 int32_t const_index = ToInteger32(LConstantOperand::cast(instr->index()));
4633 __ Push(Smi::FromInt(const_index));
4635 Register index = ToRegister(instr->index());
4636 __ Integer32ToSmi(index, index);
4639 CallRuntimeFromDeferred(
4640 Runtime::kStringCharCodeAtRT, 2, instr, instr->context());
4642 __ SmiToInteger32(rax, rax);
4643 __ StoreToSafepointRegisterSlot(result, rax);
4647 void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
4648 class DeferredStringCharFromCode FINAL : public LDeferredCode {
4650 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
4651 : LDeferredCode(codegen), instr_(instr) { }
4652 void Generate() OVERRIDE {
4653 codegen()->DoDeferredStringCharFromCode(instr_);
4655 LInstruction* instr() OVERRIDE { return instr_; }
4658 LStringCharFromCode* instr_;
4661 DeferredStringCharFromCode* deferred =
4662 new(zone()) DeferredStringCharFromCode(this, instr);
4664 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
4665 Register char_code = ToRegister(instr->char_code());
4666 Register result = ToRegister(instr->result());
4667 DCHECK(!char_code.is(result));
4669 __ cmpl(char_code, Immediate(String::kMaxOneByteCharCode));
4670 __ j(above, deferred->entry());
4671 __ movsxlq(char_code, char_code);
4672 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
4673 __ movp(result, FieldOperand(result,
4674 char_code, times_pointer_size,
4675 FixedArray::kHeaderSize));
4676 __ CompareRoot(result, Heap::kUndefinedValueRootIndex);
4677 __ j(equal, deferred->entry());
4678 __ bind(deferred->exit());
4682 void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4683 Register char_code = ToRegister(instr->char_code());
4684 Register result = ToRegister(instr->result());
4686 // TODO(3095996): Get rid of this. For now, we need to make the
4687 // result register contain a valid pointer because it is already
4688 // contained in the register pointer map.
4691 PushSafepointRegistersScope scope(this);
4692 __ Integer32ToSmi(char_code, char_code);
4694 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr, instr->context());
4695 __ StoreToSafepointRegisterSlot(result, rax);
4699 void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
4700 LOperand* input = instr->value();
4701 DCHECK(input->IsRegister() || input->IsStackSlot());
4702 LOperand* output = instr->result();
4703 DCHECK(output->IsDoubleRegister());
4704 if (input->IsRegister()) {
4705 __ Cvtlsi2sd(ToDoubleRegister(output), ToRegister(input));
4707 __ Cvtlsi2sd(ToDoubleRegister(output), ToOperand(input));
4712 void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4713 LOperand* input = instr->value();
4714 LOperand* output = instr->result();
4716 __ LoadUint32(ToDoubleRegister(output), ToRegister(input));
4720 void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
4721 class DeferredNumberTagI FINAL : public LDeferredCode {
4723 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
4724 : LDeferredCode(codegen), instr_(instr) { }
4725 void Generate() OVERRIDE {
4726 codegen()->DoDeferredNumberTagIU(instr_, instr_->value(), instr_->temp1(),
4727 instr_->temp2(), SIGNED_INT32);
4729 LInstruction* instr() OVERRIDE { return instr_; }
4732 LNumberTagI* instr_;
4735 LOperand* input = instr->value();
4736 DCHECK(input->IsRegister() && input->Equals(instr->result()));
4737 Register reg = ToRegister(input);
4739 if (SmiValuesAre32Bits()) {
4740 __ Integer32ToSmi(reg, reg);
4742 DCHECK(SmiValuesAre31Bits());
4743 DeferredNumberTagI* deferred = new(zone()) DeferredNumberTagI(this, instr);
4744 __ Integer32ToSmi(reg, reg);
4745 __ j(overflow, deferred->entry());
4746 __ bind(deferred->exit());
4751 void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4752 class DeferredNumberTagU FINAL : public LDeferredCode {
4754 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4755 : LDeferredCode(codegen), instr_(instr) { }
4756 void Generate() OVERRIDE {
4757 codegen()->DoDeferredNumberTagIU(instr_, instr_->value(), instr_->temp1(),
4758 instr_->temp2(), UNSIGNED_INT32);
4760 LInstruction* instr() OVERRIDE { return instr_; }
4763 LNumberTagU* instr_;
4766 LOperand* input = instr->value();
4767 DCHECK(input->IsRegister() && input->Equals(instr->result()));
4768 Register reg = ToRegister(input);
4770 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4771 __ cmpl(reg, Immediate(Smi::kMaxValue));
4772 __ j(above, deferred->entry());
4773 __ Integer32ToSmi(reg, reg);
4774 __ bind(deferred->exit());
4778 void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4782 IntegerSignedness signedness) {
4784 Register reg = ToRegister(value);
4785 Register tmp = ToRegister(temp1);
4786 XMMRegister temp_xmm = ToDoubleRegister(temp2);
4788 // Load value into temp_xmm which will be preserved across potential call to
4789 // runtime (MacroAssembler::EnterExitFrameEpilogue preserves only allocatable
4790 // XMM registers on x64).
4791 if (signedness == SIGNED_INT32) {
4792 DCHECK(SmiValuesAre31Bits());
4793 // There was overflow, so bits 30 and 31 of the original integer
4794 // disagree. Try to allocate a heap number in new space and store
4795 // the value in there. If that fails, call the runtime system.
4796 __ SmiToInteger32(reg, reg);
4797 __ xorl(reg, Immediate(0x80000000));
4798 __ cvtlsi2sd(temp_xmm, reg);
4800 DCHECK(signedness == UNSIGNED_INT32);
4801 __ LoadUint32(temp_xmm, reg);
4804 if (FLAG_inline_new) {
4805 __ AllocateHeapNumber(reg, tmp, &slow);
4806 __ jmp(&done, kPointerSize == kInt64Size ? Label::kNear : Label::kFar);
4809 // Slow case: Call the runtime system to do the number allocation.
4812 // Put a valid pointer value in the stack slot where the result
4813 // register is stored, as this register is in the pointer map, but contains
4814 // an integer value.
4817 // Preserve the value of all registers.
4818 PushSafepointRegistersScope scope(this);
4820 // NumberTagIU uses the context from the frame, rather than
4821 // the environment's HContext or HInlinedContext value.
4822 // They only call Runtime::kAllocateHeapNumber.
4823 // The corresponding HChange instructions are added in a phase that does
4824 // not have easy access to the local context.
4825 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
4826 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4827 RecordSafepointWithRegisters(
4828 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4829 __ StoreToSafepointRegisterSlot(reg, rax);
4832 // Done. Put the value in temp_xmm into the value of the allocated heap
4835 __ movsd(FieldOperand(reg, HeapNumber::kValueOffset), temp_xmm);
4839 void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
4840 class DeferredNumberTagD FINAL : public LDeferredCode {
4842 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4843 : LDeferredCode(codegen), instr_(instr) { }
4844 void Generate() OVERRIDE { codegen()->DoDeferredNumberTagD(instr_); }
4845 LInstruction* instr() OVERRIDE { return instr_; }
4848 LNumberTagD* instr_;
4851 XMMRegister input_reg = ToDoubleRegister(instr->value());
4852 Register reg = ToRegister(instr->result());
4853 Register tmp = ToRegister(instr->temp());
4855 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
4856 if (FLAG_inline_new) {
4857 __ AllocateHeapNumber(reg, tmp, deferred->entry());
4859 __ jmp(deferred->entry());
4861 __ bind(deferred->exit());
4862 __ movsd(FieldOperand(reg, HeapNumber::kValueOffset), input_reg);
4866 void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4867 // TODO(3095996): Get rid of this. For now, we need to make the
4868 // result register contain a valid pointer because it is already
4869 // contained in the register pointer map.
4870 Register reg = ToRegister(instr->result());
4871 __ Move(reg, Smi::FromInt(0));
4874 PushSafepointRegistersScope scope(this);
4875 // NumberTagD uses the context from the frame, rather than
4876 // the environment's HContext or HInlinedContext value.
4877 // They only call Runtime::kAllocateHeapNumber.
4878 // The corresponding HChange instructions are added in a phase that does
4879 // not have easy access to the local context.
4880 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
4881 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4882 RecordSafepointWithRegisters(
4883 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4884 __ movp(kScratchRegister, rax);
4886 __ movp(reg, kScratchRegister);
4890 void LCodeGen::DoSmiTag(LSmiTag* instr) {
4891 HChange* hchange = instr->hydrogen();
4892 Register input = ToRegister(instr->value());
4893 Register output = ToRegister(instr->result());
4894 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4895 hchange->value()->CheckFlag(HValue::kUint32)) {
4896 Condition is_smi = __ CheckUInteger32ValidSmiValue(input);
4897 DeoptimizeIf(NegateCondition(is_smi), instr, Deoptimizer::kOverflow);
4899 __ Integer32ToSmi(output, input);
4900 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4901 !hchange->value()->CheckFlag(HValue::kUint32)) {
4902 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
4907 void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4908 DCHECK(instr->value()->Equals(instr->result()));
4909 Register input = ToRegister(instr->value());
4910 if (instr->needs_check()) {
4911 Condition is_smi = __ CheckSmi(input);
4912 DeoptimizeIf(NegateCondition(is_smi), instr, Deoptimizer::kNotASmi);
4914 __ AssertSmi(input);
4916 __ SmiToInteger32(input, input);
4920 void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
4921 XMMRegister result_reg, NumberUntagDMode mode) {
4922 bool can_convert_undefined_to_nan =
4923 instr->hydrogen()->can_convert_undefined_to_nan();
4924 bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
4926 Label convert, load_smi, done;
4928 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4930 __ JumpIfSmi(input_reg, &load_smi, Label::kNear);
4932 // Heap number map check.
4933 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
4934 Heap::kHeapNumberMapRootIndex);
4936 // On x64 it is safe to load at heap number offset before evaluating the map
4937 // check, since all heap objects are at least two words long.
4938 __ movsd(result_reg, FieldOperand(input_reg, HeapNumber::kValueOffset));
4940 if (can_convert_undefined_to_nan) {
4941 __ j(not_equal, &convert, Label::kNear);
4943 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
4946 if (deoptimize_on_minus_zero) {
4947 XMMRegister xmm_scratch = double_scratch0();
4948 __ xorps(xmm_scratch, xmm_scratch);
4949 __ ucomisd(xmm_scratch, result_reg);
4950 __ j(not_equal, &done, Label::kNear);
4951 __ movmskpd(kScratchRegister, result_reg);
4952 __ testq(kScratchRegister, Immediate(1));
4953 DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero);
4955 __ jmp(&done, Label::kNear);
4957 if (can_convert_undefined_to_nan) {
4960 // Convert undefined (and hole) to NaN. Compute NaN as 0/0.
4961 __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex);
4962 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumberUndefined);
4964 __ pcmpeqd(result_reg, result_reg);
4965 __ jmp(&done, Label::kNear);
4968 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
4971 // Smi to XMM conversion
4973 __ SmiToInteger32(kScratchRegister, input_reg);
4974 __ Cvtlsi2sd(result_reg, kScratchRegister);
4979 void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr, Label* done) {
4980 Register input_reg = ToRegister(instr->value());
4982 if (instr->truncating()) {
4983 Label no_heap_number, check_bools, check_false;
4985 // Heap number map check.
4986 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
4987 Heap::kHeapNumberMapRootIndex);
4988 __ j(not_equal, &no_heap_number, Label::kNear);
4989 __ TruncateHeapNumberToI(input_reg, input_reg);
4992 __ bind(&no_heap_number);
4993 // Check for Oddballs. Undefined/False is converted to zero and True to one
4994 // for truncating conversions.
4995 __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex);
4996 __ j(not_equal, &check_bools, Label::kNear);
4997 __ Set(input_reg, 0);
5000 __ bind(&check_bools);
5001 __ CompareRoot(input_reg, Heap::kTrueValueRootIndex);
5002 __ j(not_equal, &check_false, Label::kNear);
5003 __ Set(input_reg, 1);
5006 __ bind(&check_false);
5007 __ CompareRoot(input_reg, Heap::kFalseValueRootIndex);
5008 DeoptimizeIf(not_equal, instr,
5009 Deoptimizer::kNotAHeapNumberUndefinedBoolean);
5010 __ Set(input_reg, 0);
5012 XMMRegister scratch = ToDoubleRegister(instr->temp());
5013 DCHECK(!scratch.is(xmm0));
5014 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
5015 Heap::kHeapNumberMapRootIndex);
5016 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
5017 __ movsd(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset));
5018 __ cvttsd2si(input_reg, xmm0);
5019 __ Cvtlsi2sd(scratch, input_reg);
5020 __ ucomisd(xmm0, scratch);
5021 DeoptimizeIf(not_equal, instr, Deoptimizer::kLostPrecision);
5022 DeoptimizeIf(parity_even, instr, Deoptimizer::kNaN);
5023 if (instr->hydrogen()->GetMinusZeroMode() == FAIL_ON_MINUS_ZERO) {
5024 __ testl(input_reg, input_reg);
5025 __ j(not_zero, done);
5026 __ movmskpd(input_reg, xmm0);
5027 __ andl(input_reg, Immediate(1));
5028 DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero);
5034 void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
5035 class DeferredTaggedToI FINAL : public LDeferredCode {
5037 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
5038 : LDeferredCode(codegen), instr_(instr) { }
5039 void Generate() OVERRIDE { codegen()->DoDeferredTaggedToI(instr_, done()); }
5040 LInstruction* instr() OVERRIDE { return instr_; }
5046 LOperand* input = instr->value();
5047 DCHECK(input->IsRegister());
5048 DCHECK(input->Equals(instr->result()));
5049 Register input_reg = ToRegister(input);
5051 if (instr->hydrogen()->value()->representation().IsSmi()) {
5052 __ SmiToInteger32(input_reg, input_reg);
5054 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
5055 __ JumpIfNotSmi(input_reg, deferred->entry());
5056 __ SmiToInteger32(input_reg, input_reg);
5057 __ bind(deferred->exit());
5062 void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
5063 LOperand* input = instr->value();
5064 DCHECK(input->IsRegister());
5065 LOperand* result = instr->result();
5066 DCHECK(result->IsDoubleRegister());
5068 Register input_reg = ToRegister(input);
5069 XMMRegister result_reg = ToDoubleRegister(result);
5071 HValue* value = instr->hydrogen()->value();
5072 NumberUntagDMode mode = value->representation().IsSmi()
5073 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
5075 EmitNumberUntagD(instr, input_reg, result_reg, mode);
5079 void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
5080 LOperand* input = instr->value();
5081 DCHECK(input->IsDoubleRegister());
5082 LOperand* result = instr->result();
5083 DCHECK(result->IsRegister());
5085 XMMRegister input_reg = ToDoubleRegister(input);
5086 Register result_reg = ToRegister(result);
5088 if (instr->truncating()) {
5089 __ TruncateDoubleToI(result_reg, input_reg);
5091 Label lost_precision, is_nan, minus_zero, done;
5092 XMMRegister xmm_scratch = double_scratch0();
5093 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
5094 __ DoubleToI(result_reg, input_reg, xmm_scratch,
5095 instr->hydrogen()->GetMinusZeroMode(), &lost_precision,
5096 &is_nan, &minus_zero, dist);
5097 __ jmp(&done, dist);
5098 __ bind(&lost_precision);
5099 DeoptimizeIf(no_condition, instr, Deoptimizer::kLostPrecision);
5101 DeoptimizeIf(no_condition, instr, Deoptimizer::kNaN);
5102 __ bind(&minus_zero);
5103 DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero);
5109 void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
5110 LOperand* input = instr->value();
5111 DCHECK(input->IsDoubleRegister());
5112 LOperand* result = instr->result();
5113 DCHECK(result->IsRegister());
5115 XMMRegister input_reg = ToDoubleRegister(input);
5116 Register result_reg = ToRegister(result);
5118 Label lost_precision, is_nan, minus_zero, done;
5119 XMMRegister xmm_scratch = double_scratch0();
5120 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
5121 __ DoubleToI(result_reg, input_reg, xmm_scratch,
5122 instr->hydrogen()->GetMinusZeroMode(), &lost_precision, &is_nan,
5124 __ jmp(&done, dist);
5125 __ bind(&lost_precision);
5126 DeoptimizeIf(no_condition, instr, Deoptimizer::kLostPrecision);
5128 DeoptimizeIf(no_condition, instr, Deoptimizer::kNaN);
5129 __ bind(&minus_zero);
5130 DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero);
5132 __ Integer32ToSmi(result_reg, result_reg);
5133 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
5137 void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
5138 LOperand* input = instr->value();
5139 Condition cc = masm()->CheckSmi(ToRegister(input));
5140 DeoptimizeIf(NegateCondition(cc), instr, Deoptimizer::kNotASmi);
5144 void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
5145 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
5146 LOperand* input = instr->value();
5147 Condition cc = masm()->CheckSmi(ToRegister(input));
5148 DeoptimizeIf(cc, instr, Deoptimizer::kSmi);
5153 void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
5154 Register input = ToRegister(instr->value());
5156 __ movp(kScratchRegister, FieldOperand(input, HeapObject::kMapOffset));
5158 if (instr->hydrogen()->is_interval_check()) {
5161 instr->hydrogen()->GetCheckInterval(&first, &last);
5163 __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
5164 Immediate(static_cast<int8_t>(first)));
5166 // If there is only one type in the interval check for equality.
5167 if (first == last) {
5168 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongInstanceType);
5170 DeoptimizeIf(below, instr, Deoptimizer::kWrongInstanceType);
5171 // Omit check for the last type.
5172 if (last != LAST_TYPE) {
5173 __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
5174 Immediate(static_cast<int8_t>(last)));
5175 DeoptimizeIf(above, instr, Deoptimizer::kWrongInstanceType);
5181 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
5183 if (base::bits::IsPowerOfTwo32(mask)) {
5184 DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
5185 __ testb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
5187 DeoptimizeIf(tag == 0 ? not_zero : zero, instr,
5188 Deoptimizer::kWrongInstanceType);
5190 __ movzxbl(kScratchRegister,
5191 FieldOperand(kScratchRegister, Map::kInstanceTypeOffset));
5192 __ andb(kScratchRegister, Immediate(mask));
5193 __ cmpb(kScratchRegister, Immediate(tag));
5194 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongInstanceType);
5200 void LCodeGen::DoCheckValue(LCheckValue* instr) {
5201 Register reg = ToRegister(instr->value());
5202 __ Cmp(reg, instr->hydrogen()->object().handle());
5203 DeoptimizeIf(not_equal, instr, Deoptimizer::kValueMismatch);
5207 void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
5209 PushSafepointRegistersScope scope(this);
5212 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
5213 RecordSafepointWithRegisters(
5214 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
5216 __ testp(rax, Immediate(kSmiTagMask));
5218 DeoptimizeIf(zero, instr, Deoptimizer::kInstanceMigrationFailed);
5222 void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
5223 class DeferredCheckMaps FINAL : public LDeferredCode {
5225 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
5226 : LDeferredCode(codegen), instr_(instr), object_(object) {
5227 SetExit(check_maps());
5229 void Generate() OVERRIDE {
5230 codegen()->DoDeferredInstanceMigration(instr_, object_);
5232 Label* check_maps() { return &check_maps_; }
5233 LInstruction* instr() OVERRIDE { return instr_; }
5241 if (instr->hydrogen()->IsStabilityCheck()) {
5242 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5243 for (int i = 0; i < maps->size(); ++i) {
5244 AddStabilityDependency(maps->at(i).handle());
5249 LOperand* input = instr->value();
5250 DCHECK(input->IsRegister());
5251 Register reg = ToRegister(input);
5253 DeferredCheckMaps* deferred = NULL;
5254 if (instr->hydrogen()->HasMigrationTarget()) {
5255 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5256 __ bind(deferred->check_maps());
5259 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5261 for (int i = 0; i < maps->size() - 1; i++) {
5262 Handle<Map> map = maps->at(i).handle();
5263 __ CompareMap(reg, map);
5264 __ j(equal, &success, Label::kNear);
5267 Handle<Map> map = maps->at(maps->size() - 1).handle();
5268 __ CompareMap(reg, map);
5269 if (instr->hydrogen()->HasMigrationTarget()) {
5270 __ j(not_equal, deferred->entry());
5272 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongMap);
5279 void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
5280 XMMRegister value_reg = ToDoubleRegister(instr->unclamped());
5281 XMMRegister xmm_scratch = double_scratch0();
5282 Register result_reg = ToRegister(instr->result());
5283 __ ClampDoubleToUint8(value_reg, xmm_scratch, result_reg);
5287 void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
5288 DCHECK(instr->unclamped()->Equals(instr->result()));
5289 Register value_reg = ToRegister(instr->result());
5290 __ ClampUint8(value_reg);
5294 void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
5295 DCHECK(instr->unclamped()->Equals(instr->result()));
5296 Register input_reg = ToRegister(instr->unclamped());
5297 XMMRegister temp_xmm_reg = ToDoubleRegister(instr->temp_xmm());
5298 XMMRegister xmm_scratch = double_scratch0();
5299 Label is_smi, done, heap_number;
5300 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
5301 __ JumpIfSmi(input_reg, &is_smi, dist);
5303 // Check for heap number
5304 __ Cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
5305 factory()->heap_number_map());
5306 __ j(equal, &heap_number, Label::kNear);
5308 // Check for undefined. Undefined is converted to zero for clamping
5310 __ Cmp(input_reg, factory()->undefined_value());
5311 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumberUndefined);
5312 __ xorl(input_reg, input_reg);
5313 __ jmp(&done, Label::kNear);
5316 __ bind(&heap_number);
5317 __ movsd(xmm_scratch, FieldOperand(input_reg, HeapNumber::kValueOffset));
5318 __ ClampDoubleToUint8(xmm_scratch, temp_xmm_reg, input_reg);
5319 __ jmp(&done, Label::kNear);
5323 __ SmiToInteger32(input_reg, input_reg);
5324 __ ClampUint8(input_reg);
5330 void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5331 XMMRegister value_reg = ToDoubleRegister(instr->value());
5332 Register result_reg = ToRegister(instr->result());
5333 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5334 __ movq(result_reg, value_reg);
5335 __ shrq(result_reg, Immediate(32));
5337 __ movd(result_reg, value_reg);
5342 void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5343 Register hi_reg = ToRegister(instr->hi());
5344 Register lo_reg = ToRegister(instr->lo());
5345 XMMRegister result_reg = ToDoubleRegister(instr->result());
5346 XMMRegister xmm_scratch = double_scratch0();
5347 __ movd(result_reg, hi_reg);
5348 __ psllq(result_reg, 32);
5349 __ movd(xmm_scratch, lo_reg);
5350 __ orps(result_reg, xmm_scratch);
5354 void LCodeGen::DoAllocate(LAllocate* instr) {
5355 class DeferredAllocate FINAL : public LDeferredCode {
5357 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
5358 : LDeferredCode(codegen), instr_(instr) { }
5359 void Generate() OVERRIDE { codegen()->DoDeferredAllocate(instr_); }
5360 LInstruction* instr() OVERRIDE { return instr_; }
5366 DeferredAllocate* deferred =
5367 new(zone()) DeferredAllocate(this, instr);
5369 Register result = ToRegister(instr->result());
5370 Register temp = ToRegister(instr->temp());
5372 // Allocate memory for the object.
5373 AllocationFlags flags = TAG_OBJECT;
5374 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5375 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5377 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5378 DCHECK(!instr->hydrogen()->IsOldDataSpaceAllocation());
5379 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5380 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_POINTER_SPACE);
5381 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5382 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5383 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_DATA_SPACE);
5386 if (instr->size()->IsConstantOperand()) {
5387 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5388 if (size <= Page::kMaxRegularHeapObjectSize) {
5389 __ Allocate(size, result, temp, no_reg, deferred->entry(), flags);
5391 __ jmp(deferred->entry());
5394 Register size = ToRegister(instr->size());
5395 __ Allocate(size, result, temp, no_reg, deferred->entry(), flags);
5398 __ bind(deferred->exit());
5400 if (instr->hydrogen()->MustPrefillWithFiller()) {
5401 if (instr->size()->IsConstantOperand()) {
5402 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5403 __ movl(temp, Immediate((size / kPointerSize) - 1));
5405 temp = ToRegister(instr->size());
5406 __ sarp(temp, Immediate(kPointerSizeLog2));
5411 __ Move(FieldOperand(result, temp, times_pointer_size, 0),
5412 isolate()->factory()->one_pointer_filler_map());
5414 __ j(not_zero, &loop);
5419 void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
5420 Register result = ToRegister(instr->result());
5422 // TODO(3095996): Get rid of this. For now, we need to make the
5423 // result register contain a valid pointer because it is already
5424 // contained in the register pointer map.
5425 __ Move(result, Smi::FromInt(0));
5427 PushSafepointRegistersScope scope(this);
5428 if (instr->size()->IsRegister()) {
5429 Register size = ToRegister(instr->size());
5430 DCHECK(!size.is(result));
5431 __ Integer32ToSmi(size, size);
5434 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5435 __ Push(Smi::FromInt(size));
5439 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5440 DCHECK(!instr->hydrogen()->IsOldDataSpaceAllocation());
5441 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5442 flags = AllocateTargetSpace::update(flags, OLD_POINTER_SPACE);
5443 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5444 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5445 flags = AllocateTargetSpace::update(flags, OLD_DATA_SPACE);
5447 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5449 __ Push(Smi::FromInt(flags));
5451 CallRuntimeFromDeferred(
5452 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
5453 __ StoreToSafepointRegisterSlot(result, rax);
5457 void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
5458 DCHECK(ToRegister(instr->value()).is(rax));
5460 CallRuntime(Runtime::kToFastProperties, 1, instr);
5464 void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
5465 DCHECK(ToRegister(instr->context()).is(rsi));
5467 // Registers will be used as follows:
5468 // rcx = literals array.
5469 // rbx = regexp literal.
5470 // rax = regexp literal clone.
5471 int literal_offset =
5472 FixedArray::OffsetOfElementAt(instr->hydrogen()->literal_index());
5473 __ Move(rcx, instr->hydrogen()->literals());
5474 __ movp(rbx, FieldOperand(rcx, literal_offset));
5475 __ CompareRoot(rbx, Heap::kUndefinedValueRootIndex);
5476 __ j(not_equal, &materialized, Label::kNear);
5478 // Create regexp literal using runtime function
5479 // Result will be in rax.
5481 __ Push(Smi::FromInt(instr->hydrogen()->literal_index()));
5482 __ Push(instr->hydrogen()->pattern());
5483 __ Push(instr->hydrogen()->flags());
5484 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
5487 __ bind(&materialized);
5488 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
5489 Label allocated, runtime_allocate;
5490 __ Allocate(size, rax, rcx, rdx, &runtime_allocate, TAG_OBJECT);
5491 __ jmp(&allocated, Label::kNear);
5493 __ bind(&runtime_allocate);
5495 __ Push(Smi::FromInt(size));
5496 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
5499 __ bind(&allocated);
5500 // Copy the content into the newly allocated memory.
5501 // (Unroll copy loop once for better throughput).
5502 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
5503 __ movp(rdx, FieldOperand(rbx, i));
5504 __ movp(rcx, FieldOperand(rbx, i + kPointerSize));
5505 __ movp(FieldOperand(rax, i), rdx);
5506 __ movp(FieldOperand(rax, i + kPointerSize), rcx);
5508 if ((size % (2 * kPointerSize)) != 0) {
5509 __ movp(rdx, FieldOperand(rbx, size - kPointerSize));
5510 __ movp(FieldOperand(rax, size - kPointerSize), rdx);
5515 void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
5516 DCHECK(ToRegister(instr->context()).is(rsi));
5517 // Use the fast case closure allocation code that allocates in new
5518 // space for nested functions that don't need literals cloning.
5519 bool pretenure = instr->hydrogen()->pretenure();
5520 if (!pretenure && instr->hydrogen()->has_no_literals()) {
5521 FastNewClosureStub stub(isolate(), instr->hydrogen()->language_mode(),
5522 instr->hydrogen()->kind());
5523 __ Move(rbx, instr->hydrogen()->shared_info());
5524 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5527 __ Push(instr->hydrogen()->shared_info());
5528 __ PushRoot(pretenure ? Heap::kTrueValueRootIndex :
5529 Heap::kFalseValueRootIndex);
5530 CallRuntime(Runtime::kNewClosure, 3, instr);
5535 void LCodeGen::DoTypeof(LTypeof* instr) {
5536 DCHECK(ToRegister(instr->context()).is(rsi));
5537 LOperand* input = instr->value();
5538 EmitPushTaggedOperand(input);
5539 CallRuntime(Runtime::kTypeof, 1, instr);
5543 void LCodeGen::EmitPushTaggedOperand(LOperand* operand) {
5544 DCHECK(!operand->IsDoubleRegister());
5545 if (operand->IsConstantOperand()) {
5546 __ Push(ToHandle(LConstantOperand::cast(operand)));
5547 } else if (operand->IsRegister()) {
5548 __ Push(ToRegister(operand));
5550 __ Push(ToOperand(operand));
5555 void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5556 Register input = ToRegister(instr->value());
5557 Condition final_branch_condition = EmitTypeofIs(instr, input);
5558 if (final_branch_condition != no_condition) {
5559 EmitBranch(instr, final_branch_condition);
5564 Condition LCodeGen::EmitTypeofIs(LTypeofIsAndBranch* instr, Register input) {
5565 Label* true_label = instr->TrueLabel(chunk_);
5566 Label* false_label = instr->FalseLabel(chunk_);
5567 Handle<String> type_name = instr->type_literal();
5568 int left_block = instr->TrueDestination(chunk_);
5569 int right_block = instr->FalseDestination(chunk_);
5570 int next_block = GetNextEmittedBlock();
5572 Label::Distance true_distance = left_block == next_block ? Label::kNear
5574 Label::Distance false_distance = right_block == next_block ? Label::kNear
5576 Condition final_branch_condition = no_condition;
5577 Factory* factory = isolate()->factory();
5578 if (String::Equals(type_name, factory->number_string())) {
5579 __ JumpIfSmi(input, true_label, true_distance);
5580 __ CompareRoot(FieldOperand(input, HeapObject::kMapOffset),
5581 Heap::kHeapNumberMapRootIndex);
5583 final_branch_condition = equal;
5585 } else if (String::Equals(type_name, factory->string_string())) {
5586 __ JumpIfSmi(input, false_label, false_distance);
5587 __ CmpObjectType(input, FIRST_NONSTRING_TYPE, input);
5588 __ j(above_equal, false_label, false_distance);
5589 __ testb(FieldOperand(input, Map::kBitFieldOffset),
5590 Immediate(1 << Map::kIsUndetectable));
5591 final_branch_condition = zero;
5593 } else if (String::Equals(type_name, factory->symbol_string())) {
5594 __ JumpIfSmi(input, false_label, false_distance);
5595 __ CmpObjectType(input, SYMBOL_TYPE, input);
5596 final_branch_condition = equal;
5598 } else if (String::Equals(type_name, factory->boolean_string())) {
5599 __ CompareRoot(input, Heap::kTrueValueRootIndex);
5600 __ j(equal, true_label, true_distance);
5601 __ CompareRoot(input, Heap::kFalseValueRootIndex);
5602 final_branch_condition = equal;
5604 } else if (String::Equals(type_name, factory->undefined_string())) {
5605 __ CompareRoot(input, Heap::kUndefinedValueRootIndex);
5606 __ j(equal, true_label, true_distance);
5607 __ JumpIfSmi(input, false_label, false_distance);
5608 // Check for undetectable objects => true.
5609 __ movp(input, FieldOperand(input, HeapObject::kMapOffset));
5610 __ testb(FieldOperand(input, Map::kBitFieldOffset),
5611 Immediate(1 << Map::kIsUndetectable));
5612 final_branch_condition = not_zero;
5614 } else if (String::Equals(type_name, factory->function_string())) {
5615 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5616 __ JumpIfSmi(input, false_label, false_distance);
5617 __ CmpObjectType(input, JS_FUNCTION_TYPE, input);
5618 __ j(equal, true_label, true_distance);
5619 __ CmpInstanceType(input, JS_FUNCTION_PROXY_TYPE);
5620 final_branch_condition = equal;
5622 } else if (String::Equals(type_name, factory->object_string())) {
5623 __ JumpIfSmi(input, false_label, false_distance);
5624 __ CompareRoot(input, Heap::kNullValueRootIndex);
5625 __ j(equal, true_label, true_distance);
5626 __ CmpObjectType(input, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE, input);
5627 __ j(below, false_label, false_distance);
5628 __ CmpInstanceType(input, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
5629 __ j(above, false_label, false_distance);
5630 // Check for undetectable objects => false.
5631 __ testb(FieldOperand(input, Map::kBitFieldOffset),
5632 Immediate(1 << Map::kIsUndetectable));
5633 final_branch_condition = zero;
5636 __ jmp(false_label, false_distance);
5639 return final_branch_condition;
5643 void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
5644 Register temp = ToRegister(instr->temp());
5646 EmitIsConstructCall(temp);
5647 EmitBranch(instr, equal);
5651 void LCodeGen::EmitIsConstructCall(Register temp) {
5652 // Get the frame pointer for the calling frame.
5653 __ movp(temp, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
5655 // Skip the arguments adaptor frame if it exists.
5656 Label check_frame_marker;
5657 __ Cmp(Operand(temp, StandardFrameConstants::kContextOffset),
5658 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
5659 __ j(not_equal, &check_frame_marker, Label::kNear);
5660 __ movp(temp, Operand(temp, StandardFrameConstants::kCallerFPOffset));
5662 // Check the marker in the calling frame.
5663 __ bind(&check_frame_marker);
5664 __ Cmp(Operand(temp, StandardFrameConstants::kMarkerOffset),
5665 Smi::FromInt(StackFrame::CONSTRUCT));
5669 void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5670 if (!info()->IsStub()) {
5671 // Ensure that we have enough space after the previous lazy-bailout
5672 // instruction for patching the code here.
5673 int current_pc = masm()->pc_offset();
5674 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5675 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5676 __ Nop(padding_size);
5679 last_lazy_deopt_pc_ = masm()->pc_offset();
5683 void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
5684 last_lazy_deopt_pc_ = masm()->pc_offset();
5685 DCHECK(instr->HasEnvironment());
5686 LEnvironment* env = instr->environment();
5687 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5688 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5692 void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
5693 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5694 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5695 // needed return address), even though the implementation of LAZY and EAGER is
5696 // now identical. When LAZY is eventually completely folded into EAGER, remove
5697 // the special case below.
5698 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5699 type = Deoptimizer::LAZY;
5701 DeoptimizeIf(no_condition, instr, instr->hydrogen()->reason(), type);
5705 void LCodeGen::DoDummy(LDummy* instr) {
5706 // Nothing to see here, move on!
5710 void LCodeGen::DoDummyUse(LDummyUse* instr) {
5711 // Nothing to see here, move on!
5715 void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
5716 PushSafepointRegistersScope scope(this);
5717 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
5718 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5719 RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0);
5720 DCHECK(instr->HasEnvironment());
5721 LEnvironment* env = instr->environment();
5722 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5726 void LCodeGen::DoStackCheck(LStackCheck* instr) {
5727 class DeferredStackCheck FINAL : public LDeferredCode {
5729 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5730 : LDeferredCode(codegen), instr_(instr) { }
5731 void Generate() OVERRIDE { codegen()->DoDeferredStackCheck(instr_); }
5732 LInstruction* instr() OVERRIDE { return instr_; }
5735 LStackCheck* instr_;
5738 DCHECK(instr->HasEnvironment());
5739 LEnvironment* env = instr->environment();
5740 // There is no LLazyBailout instruction for stack-checks. We have to
5741 // prepare for lazy deoptimization explicitly here.
5742 if (instr->hydrogen()->is_function_entry()) {
5743 // Perform stack overflow check.
5745 __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
5746 __ j(above_equal, &done, Label::kNear);
5748 DCHECK(instr->context()->IsRegister());
5749 DCHECK(ToRegister(instr->context()).is(rsi));
5750 CallCode(isolate()->builtins()->StackCheck(),
5751 RelocInfo::CODE_TARGET,
5755 DCHECK(instr->hydrogen()->is_backwards_branch());
5756 // Perform stack overflow check if this goto needs it before jumping.
5757 DeferredStackCheck* deferred_stack_check =
5758 new(zone()) DeferredStackCheck(this, instr);
5759 __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
5760 __ j(below, deferred_stack_check->entry());
5761 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
5762 __ bind(instr->done_label());
5763 deferred_stack_check->SetExit(instr->done_label());
5764 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5765 // Don't record a deoptimization index for the safepoint here.
5766 // This will be done explicitly when emitting call and the safepoint in
5767 // the deferred code.
5772 void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
5773 // This is a pseudo-instruction that ensures that the environment here is
5774 // properly registered for deoptimization and records the assembler's PC
5776 LEnvironment* environment = instr->environment();
5778 // If the environment were already registered, we would have no way of
5779 // backpatching it with the spill slot operands.
5780 DCHECK(!environment->HasBeenRegistered());
5781 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
5783 GenerateOsrPrologue();
5787 void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
5788 DCHECK(ToRegister(instr->context()).is(rsi));
5789 __ CompareRoot(rax, Heap::kUndefinedValueRootIndex);
5790 DeoptimizeIf(equal, instr, Deoptimizer::kUndefined);
5792 Register null_value = rdi;
5793 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
5794 __ cmpp(rax, null_value);
5795 DeoptimizeIf(equal, instr, Deoptimizer::kNull);
5797 Condition cc = masm()->CheckSmi(rax);
5798 DeoptimizeIf(cc, instr, Deoptimizer::kSmi);
5800 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
5801 __ CmpObjectType(rax, LAST_JS_PROXY_TYPE, rcx);
5802 DeoptimizeIf(below_equal, instr, Deoptimizer::kWrongInstanceType);
5804 Label use_cache, call_runtime;
5805 __ CheckEnumCache(null_value, &call_runtime);
5807 __ movp(rax, FieldOperand(rax, HeapObject::kMapOffset));
5808 __ jmp(&use_cache, Label::kNear);
5810 // Get the set of properties to enumerate.
5811 __ bind(&call_runtime);
5813 CallRuntime(Runtime::kGetPropertyNamesFast, 1, instr);
5815 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
5816 Heap::kMetaMapRootIndex);
5817 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongMap);
5818 __ bind(&use_cache);
5822 void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5823 Register map = ToRegister(instr->map());
5824 Register result = ToRegister(instr->result());
5825 Label load_cache, done;
5826 __ EnumLength(result, map);
5827 __ Cmp(result, Smi::FromInt(0));
5828 __ j(not_equal, &load_cache, Label::kNear);
5829 __ LoadRoot(result, Heap::kEmptyFixedArrayRootIndex);
5830 __ jmp(&done, Label::kNear);
5831 __ bind(&load_cache);
5832 __ LoadInstanceDescriptors(map, result);
5834 FieldOperand(result, DescriptorArray::kEnumCacheOffset));
5836 FieldOperand(result, FixedArray::SizeFor(instr->idx())));
5838 Condition cc = masm()->CheckSmi(result);
5839 DeoptimizeIf(cc, instr, Deoptimizer::kNoCache);
5843 void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5844 Register object = ToRegister(instr->value());
5845 __ cmpp(ToRegister(instr->map()),
5846 FieldOperand(object, HeapObject::kMapOffset));
5847 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongMap);
5851 void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5854 PushSafepointRegistersScope scope(this);
5858 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
5859 RecordSafepointWithRegisters(
5860 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
5861 __ StoreToSafepointRegisterSlot(object, rax);
5865 void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
5866 class DeferredLoadMutableDouble FINAL : public LDeferredCode {
5868 DeferredLoadMutableDouble(LCodeGen* codegen,
5869 LLoadFieldByIndex* instr,
5872 : LDeferredCode(codegen),
5877 void Generate() OVERRIDE {
5878 codegen()->DoDeferredLoadMutableDouble(instr_, object_, index_);
5880 LInstruction* instr() OVERRIDE { return instr_; }
5883 LLoadFieldByIndex* instr_;
5888 Register object = ToRegister(instr->object());
5889 Register index = ToRegister(instr->index());
5891 DeferredLoadMutableDouble* deferred;
5892 deferred = new(zone()) DeferredLoadMutableDouble(this, instr, object, index);
5894 Label out_of_object, done;
5895 __ Move(kScratchRegister, Smi::FromInt(1));
5896 __ testp(index, kScratchRegister);
5897 __ j(not_zero, deferred->entry());
5899 __ sarp(index, Immediate(1));
5901 __ SmiToInteger32(index, index);
5902 __ cmpl(index, Immediate(0));
5903 __ j(less, &out_of_object, Label::kNear);
5904 __ movp(object, FieldOperand(object,
5907 JSObject::kHeaderSize));
5908 __ jmp(&done, Label::kNear);
5910 __ bind(&out_of_object);
5911 __ movp(object, FieldOperand(object, JSObject::kPropertiesOffset));
5913 // Index is now equal to out of object property index plus 1.
5914 __ movp(object, FieldOperand(object,
5917 FixedArray::kHeaderSize - kPointerSize));
5918 __ bind(deferred->exit());
5923 void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
5924 Register context = ToRegister(instr->context());
5925 __ movp(Operand(rbp, StandardFrameConstants::kContextOffset), context);
5929 void LCodeGen::DoAllocateBlockContext(LAllocateBlockContext* instr) {
5930 Handle<ScopeInfo> scope_info = instr->scope_info();
5931 __ Push(scope_info);
5932 __ Push(ToRegister(instr->function()));
5933 CallRuntime(Runtime::kPushBlockContext, 2, instr);
5934 RecordSafepoint(Safepoint::kNoLazyDeopt);
5940 } } // namespace v8::internal
5942 #endif // V8_TARGET_ARCH_X64