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.
7 #include "src/base/bits.h"
8 #include "src/code-factory.h"
9 #include "src/code-stubs.h"
10 #include "src/cpu-profiler.h"
11 #include "src/hydrogen-osr.h"
12 #include "src/ic/ic.h"
13 #include "src/ic/stub-cache.h"
14 #include "src/x64/lithium-codegen-x64.h"
20 // When invoking builtins, we need to record the safepoint in the middle of
21 // the invoke instruction sequence generated by the macro assembler.
22 class SafepointGenerator final : public CallWrapper {
24 SafepointGenerator(LCodeGen* codegen,
25 LPointerMap* pointers,
26 Safepoint::DeoptMode mode)
30 virtual ~SafepointGenerator() {}
32 void BeforeCall(int call_size) const override {}
34 void AfterCall() const override {
35 codegen_->RecordSafepoint(pointers_, deopt_mode_);
40 LPointerMap* pointers_;
41 Safepoint::DeoptMode deopt_mode_;
47 bool LCodeGen::GenerateCode() {
48 LPhase phase("Z_Code generation", chunk());
52 // Open a frame scope to indicate that there is a frame on the stack. The
53 // MANUAL indicates that the scope shouldn't actually generate code to set up
54 // the frame (that is done in GeneratePrologue).
55 FrameScope frame_scope(masm_, StackFrame::MANUAL);
57 return GeneratePrologue() &&
59 GenerateDeferredCode() &&
60 GenerateJumpTable() &&
61 GenerateSafepointTable();
65 void LCodeGen::FinishCode(Handle<Code> code) {
67 code->set_stack_slots(GetStackSlotCount());
68 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
69 PopulateDeoptimizationData(code);
74 void LCodeGen::MakeSureStackPagesMapped(int offset) {
75 const int kPageSize = 4 * KB;
76 for (offset -= kPageSize; offset > 0; offset -= kPageSize) {
77 __ movp(Operand(rsp, offset), rax);
83 void LCodeGen::SaveCallerDoubles() {
84 DCHECK(info()->saves_caller_doubles());
85 DCHECK(NeedsEagerFrame());
86 Comment(";;; Save clobbered callee double registers");
88 BitVector* doubles = chunk()->allocated_double_registers();
89 BitVector::Iterator save_iterator(doubles);
90 while (!save_iterator.Done()) {
91 __ movsd(MemOperand(rsp, count * kDoubleSize),
92 XMMRegister::FromAllocationIndex(save_iterator.Current()));
93 save_iterator.Advance();
99 void LCodeGen::RestoreCallerDoubles() {
100 DCHECK(info()->saves_caller_doubles());
101 DCHECK(NeedsEagerFrame());
102 Comment(";;; Restore clobbered callee double registers");
103 BitVector* doubles = chunk()->allocated_double_registers();
104 BitVector::Iterator save_iterator(doubles);
106 while (!save_iterator.Done()) {
107 __ movsd(XMMRegister::FromAllocationIndex(save_iterator.Current()),
108 MemOperand(rsp, count * kDoubleSize));
109 save_iterator.Advance();
115 bool LCodeGen::GeneratePrologue() {
116 DCHECK(is_generating());
118 if (info()->IsOptimizing()) {
119 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
122 if (strlen(FLAG_stop_at) > 0 &&
123 info_->literal()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
128 // Sloppy mode functions need to replace the receiver with the global proxy
129 // when called as functions (without an explicit receiver object).
130 if (is_sloppy(info()->language_mode()) && info()->MayUseThis() &&
131 !info()->is_native() && info()->scope()->has_this_declaration()) {
133 StackArgumentsAccessor args(rsp, scope()->num_parameters());
134 __ movp(rcx, args.GetReceiverOperand());
136 __ CompareRoot(rcx, Heap::kUndefinedValueRootIndex);
137 __ j(not_equal, &ok, Label::kNear);
139 __ movp(rcx, GlobalObjectOperand());
140 __ movp(rcx, FieldOperand(rcx, GlobalObject::kGlobalProxyOffset));
142 __ movp(args.GetReceiverOperand(), rcx);
148 info()->set_prologue_offset(masm_->pc_offset());
149 if (NeedsEagerFrame()) {
150 DCHECK(!frame_is_built_);
151 frame_is_built_ = true;
152 if (info()->IsStub()) {
155 __ Prologue(info()->IsCodePreAgingActive());
157 info()->AddNoFrameRange(0, masm_->pc_offset());
160 // Reserve space for the stack slots needed by the code.
161 int slots = GetStackSlotCount();
163 if (FLAG_debug_code) {
164 __ subp(rsp, Immediate(slots * kPointerSize));
166 MakeSureStackPagesMapped(slots * kPointerSize);
170 __ Set(kScratchRegister, kSlotsZapValue);
173 __ movp(MemOperand(rsp, rax, times_pointer_size, 0),
176 __ j(not_zero, &loop);
179 __ subp(rsp, Immediate(slots * kPointerSize));
181 MakeSureStackPagesMapped(slots * kPointerSize);
185 if (info()->saves_caller_doubles()) {
190 // Possibly allocate a local context.
191 int heap_slots = info_->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
192 if (heap_slots > 0) {
193 Comment(";;; Allocate local context");
194 bool need_write_barrier = true;
195 // Argument to NewContext is the function, which is still in rdi.
196 DCHECK(!info()->scope()->is_script_scope());
197 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
198 FastNewContextStub stub(isolate(), heap_slots);
200 // Result of FastNewContextStub is always in new space.
201 need_write_barrier = false;
204 __ CallRuntime(Runtime::kNewFunctionContext, 1);
206 RecordSafepoint(Safepoint::kNoLazyDeopt);
207 // Context is returned in rax. It replaces the context passed to us.
208 // It's saved in the stack and kept live in rsi.
210 __ movp(Operand(rbp, StandardFrameConstants::kContextOffset), rax);
212 // Copy any necessary parameters into the context.
213 int num_parameters = scope()->num_parameters();
214 int first_parameter = scope()->has_this_declaration() ? -1 : 0;
215 for (int i = first_parameter; i < num_parameters; i++) {
216 Variable* var = (i == -1) ? scope()->receiver() : 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::IsExternalConstant(LConstantOperand* op) const {
460 return chunk_->LookupLiteralRepresentation(op).IsExternal();
464 bool LCodeGen::IsDehoistedKeyConstant(LConstantOperand* op) const {
465 return op->IsConstantOperand() &&
466 chunk_->IsDehoistedKey(chunk_->LookupConstant(op));
470 bool LCodeGen::IsSmiConstant(LConstantOperand* op) const {
471 return chunk_->LookupLiteralRepresentation(op).IsSmi();
475 int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
476 return ToRepresentation(op, Representation::Integer32());
480 int32_t LCodeGen::ToRepresentation(LConstantOperand* op,
481 const Representation& r) const {
482 HConstant* constant = chunk_->LookupConstant(op);
483 int32_t value = constant->Integer32Value();
484 if (r.IsInteger32()) return value;
485 DCHECK(SmiValuesAre31Bits() && r.IsSmiOrTagged());
486 return static_cast<int32_t>(reinterpret_cast<intptr_t>(Smi::FromInt(value)));
490 Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
491 HConstant* constant = chunk_->LookupConstant(op);
492 return Smi::FromInt(constant->Integer32Value());
496 double LCodeGen::ToDouble(LConstantOperand* op) const {
497 HConstant* constant = chunk_->LookupConstant(op);
498 DCHECK(constant->HasDoubleValue());
499 return constant->DoubleValue();
503 ExternalReference LCodeGen::ToExternalReference(LConstantOperand* op) const {
504 HConstant* constant = chunk_->LookupConstant(op);
505 DCHECK(constant->HasExternalReferenceValue());
506 return constant->ExternalReferenceValue();
510 Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
511 HConstant* constant = chunk_->LookupConstant(op);
512 DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
513 return constant->handle(isolate());
517 static int ArgumentsOffsetWithoutFrame(int index) {
519 return -(index + 1) * kPointerSize + kPCOnStackSize;
523 Operand LCodeGen::ToOperand(LOperand* op) const {
524 // Does not handle registers. In X64 assembler, plain registers are not
525 // representable as an Operand.
526 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
527 if (NeedsEagerFrame()) {
528 return Operand(rbp, StackSlotOffset(op->index()));
530 // Retrieve parameter without eager stack-frame relative to the
532 return Operand(rsp, ArgumentsOffsetWithoutFrame(op->index()));
537 void LCodeGen::WriteTranslation(LEnvironment* environment,
538 Translation* translation) {
539 if (environment == NULL) return;
541 // The translation includes one command per value in the environment.
542 int translation_size = environment->translation_size();
544 WriteTranslation(environment->outer(), translation);
545 WriteTranslationFrame(environment, translation);
547 int object_index = 0;
548 int dematerialized_index = 0;
549 for (int i = 0; i < translation_size; ++i) {
550 LOperand* value = environment->values()->at(i);
552 environment, translation, value, environment->HasTaggedValueAt(i),
553 environment->HasUint32ValueAt(i), &object_index, &dematerialized_index);
558 void LCodeGen::AddToTranslation(LEnvironment* environment,
559 Translation* translation,
563 int* object_index_pointer,
564 int* dematerialized_index_pointer) {
565 if (op == LEnvironment::materialization_marker()) {
566 int object_index = (*object_index_pointer)++;
567 if (environment->ObjectIsDuplicateAt(object_index)) {
568 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
569 translation->DuplicateObject(dupe_of);
572 int object_length = environment->ObjectLengthAt(object_index);
573 if (environment->ObjectIsArgumentsAt(object_index)) {
574 translation->BeginArgumentsObject(object_length);
576 translation->BeginCapturedObject(object_length);
578 int dematerialized_index = *dematerialized_index_pointer;
579 int env_offset = environment->translation_size() + dematerialized_index;
580 *dematerialized_index_pointer += object_length;
581 for (int i = 0; i < object_length; ++i) {
582 LOperand* value = environment->values()->at(env_offset + i);
583 AddToTranslation(environment,
586 environment->HasTaggedValueAt(env_offset + i),
587 environment->HasUint32ValueAt(env_offset + i),
588 object_index_pointer,
589 dematerialized_index_pointer);
594 if (op->IsStackSlot()) {
595 int index = op->index();
597 index += StandardFrameConstants::kFixedFrameSize / kPointerSize;
600 translation->StoreStackSlot(index);
601 } else if (is_uint32) {
602 translation->StoreUint32StackSlot(index);
604 translation->StoreInt32StackSlot(index);
606 } else if (op->IsDoubleStackSlot()) {
607 int index = op->index();
609 index += StandardFrameConstants::kFixedFrameSize / kPointerSize;
611 translation->StoreDoubleStackSlot(index);
612 } else if (op->IsRegister()) {
613 Register reg = ToRegister(op);
615 translation->StoreRegister(reg);
616 } else if (is_uint32) {
617 translation->StoreUint32Register(reg);
619 translation->StoreInt32Register(reg);
621 } else if (op->IsDoubleRegister()) {
622 XMMRegister reg = ToDoubleRegister(op);
623 translation->StoreDoubleRegister(reg);
624 } else if (op->IsConstantOperand()) {
625 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
626 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
627 translation->StoreLiteral(src_index);
634 void LCodeGen::CallCodeGeneric(Handle<Code> code,
635 RelocInfo::Mode mode,
637 SafepointMode safepoint_mode,
639 DCHECK(instr != NULL);
641 RecordSafepointWithLazyDeopt(instr, safepoint_mode, argc);
643 // Signal that we don't inline smi code before these stubs in the
644 // optimizing code generator.
645 if (code->kind() == Code::BINARY_OP_IC ||
646 code->kind() == Code::COMPARE_IC) {
652 void LCodeGen::CallCode(Handle<Code> code,
653 RelocInfo::Mode mode,
654 LInstruction* instr) {
655 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT, 0);
659 void LCodeGen::CallRuntime(const Runtime::Function* function,
662 SaveFPRegsMode save_doubles) {
663 DCHECK(instr != NULL);
664 DCHECK(instr->HasPointerMap());
666 __ CallRuntime(function, num_arguments, save_doubles);
668 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT, 0);
672 void LCodeGen::LoadContextFromDeferred(LOperand* context) {
673 if (context->IsRegister()) {
674 if (!ToRegister(context).is(rsi)) {
675 __ movp(rsi, ToRegister(context));
677 } else if (context->IsStackSlot()) {
678 __ movp(rsi, ToOperand(context));
679 } else if (context->IsConstantOperand()) {
680 HConstant* constant =
681 chunk_->LookupConstant(LConstantOperand::cast(context));
682 __ Move(rsi, Handle<Object>::cast(constant->handle(isolate())));
690 void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
694 LoadContextFromDeferred(context);
696 __ CallRuntimeSaveDoubles(id);
697 RecordSafepointWithRegisters(
698 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
702 void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
703 Safepoint::DeoptMode mode) {
704 environment->set_has_been_used();
705 if (!environment->HasBeenRegistered()) {
706 // Physical stack frame layout:
707 // -x ............. -4 0 ..................................... y
708 // [incoming arguments] [spill slots] [pushed outgoing arguments]
710 // Layout of the environment:
711 // 0 ..................................................... size-1
712 // [parameters] [locals] [expression stack including arguments]
714 // Layout of the translation:
715 // 0 ........................................................ size - 1 + 4
716 // [expression stack including arguments] [locals] [4 words] [parameters]
717 // |>------------ translation_size ------------<|
720 int jsframe_count = 0;
721 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
723 if (e->frame_type() == JS_FUNCTION) {
727 Translation translation(&translations_, frame_count, jsframe_count, zone());
728 WriteTranslation(environment, &translation);
729 int deoptimization_index = deoptimizations_.length();
730 int pc_offset = masm()->pc_offset();
731 environment->Register(deoptimization_index,
733 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
734 deoptimizations_.Add(environment, environment->zone());
739 void LCodeGen::DeoptimizeIf(Condition cc, LInstruction* instr,
740 Deoptimizer::DeoptReason deopt_reason,
741 Deoptimizer::BailoutType bailout_type) {
742 LEnvironment* environment = instr->environment();
743 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
744 DCHECK(environment->HasBeenRegistered());
745 int id = environment->deoptimization_index();
746 DCHECK(info()->IsOptimizing() || info()->IsStub());
748 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
750 Abort(kBailoutWasNotPrepared);
754 if (DeoptEveryNTimes()) {
755 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
759 Operand count_operand = masm()->ExternalOperand(count, kScratchRegister);
760 __ movl(rax, count_operand);
761 __ subl(rax, Immediate(1));
762 __ j(not_zero, &no_deopt, Label::kNear);
763 if (FLAG_trap_on_deopt) __ int3();
764 __ movl(rax, Immediate(FLAG_deopt_every_n_times));
765 __ movl(count_operand, rax);
768 DCHECK(frame_is_built_);
769 __ call(entry, RelocInfo::RUNTIME_ENTRY);
771 __ movl(count_operand, rax);
776 if (info()->ShouldTrapOnDeopt()) {
778 if (cc != no_condition) {
779 __ j(NegateCondition(cc), &done, Label::kNear);
785 Deoptimizer::DeoptInfo deopt_info = MakeDeoptInfo(instr, deopt_reason);
787 DCHECK(info()->IsStub() || frame_is_built_);
788 // Go through jump table if we need to handle condition, build frame, or
789 // restore caller doubles.
790 if (cc == no_condition && frame_is_built_ &&
791 !info()->saves_caller_doubles()) {
792 DeoptComment(deopt_info);
793 __ call(entry, RelocInfo::RUNTIME_ENTRY);
794 info()->LogDeoptCallPosition(masm()->pc_offset(), deopt_info.inlining_id);
796 Deoptimizer::JumpTableEntry table_entry(entry, deopt_info, bailout_type,
798 // We often have several deopts to the same entry, reuse the last
799 // jump entry if this is the case.
800 if (FLAG_trace_deopt || isolate()->cpu_profiler()->is_profiling() ||
801 jump_table_.is_empty() ||
802 !table_entry.IsEquivalentTo(jump_table_.last())) {
803 jump_table_.Add(table_entry, zone());
805 if (cc == no_condition) {
806 __ jmp(&jump_table_.last().label);
808 __ j(cc, &jump_table_.last().label);
814 void LCodeGen::DeoptimizeIf(Condition cc, LInstruction* instr,
815 Deoptimizer::DeoptReason deopt_reason) {
816 Deoptimizer::BailoutType bailout_type = info()->IsStub()
818 : Deoptimizer::EAGER;
819 DeoptimizeIf(cc, instr, deopt_reason, bailout_type);
823 void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
824 int length = deoptimizations_.length();
825 if (length == 0) return;
826 Handle<DeoptimizationInputData> data =
827 DeoptimizationInputData::New(isolate(), length, TENURED);
829 Handle<ByteArray> translations =
830 translations_.CreateByteArray(isolate()->factory());
831 data->SetTranslationByteArray(*translations);
832 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
833 data->SetOptimizationId(Smi::FromInt(info_->optimization_id()));
834 if (info_->IsOptimizing()) {
835 // Reference to shared function info does not change between phases.
836 AllowDeferredHandleDereference allow_handle_dereference;
837 data->SetSharedFunctionInfo(*info_->shared_info());
839 data->SetSharedFunctionInfo(Smi::FromInt(0));
841 data->SetWeakCellCache(Smi::FromInt(0));
843 Handle<FixedArray> literals =
844 factory()->NewFixedArray(deoptimization_literals_.length(), TENURED);
845 { AllowDeferredHandleDereference copy_handles;
846 for (int i = 0; i < deoptimization_literals_.length(); i++) {
847 literals->set(i, *deoptimization_literals_[i]);
849 data->SetLiteralArray(*literals);
852 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id().ToInt()));
853 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
855 // Populate the deoptimization entries.
856 for (int i = 0; i < length; i++) {
857 LEnvironment* env = deoptimizations_[i];
858 data->SetAstId(i, env->ast_id());
859 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
860 data->SetArgumentsStackHeight(i,
861 Smi::FromInt(env->arguments_stack_height()));
862 data->SetPc(i, Smi::FromInt(env->pc_offset()));
864 code->set_deoptimization_data(*data);
868 void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
869 DCHECK_EQ(0, deoptimization_literals_.length());
870 for (auto function : chunk()->inlined_functions()) {
871 DefineDeoptimizationLiteral(function);
873 inlined_function_count_ = deoptimization_literals_.length();
877 void LCodeGen::RecordSafepointWithLazyDeopt(
878 LInstruction* instr, SafepointMode safepoint_mode, int argc) {
879 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
880 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
882 DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS);
883 RecordSafepointWithRegisters(
884 instr->pointer_map(), argc, Safepoint::kLazyDeopt);
889 void LCodeGen::RecordSafepoint(
890 LPointerMap* pointers,
891 Safepoint::Kind kind,
893 Safepoint::DeoptMode deopt_mode) {
894 DCHECK(kind == expected_safepoint_kind_);
896 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
898 Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
899 kind, arguments, deopt_mode);
900 for (int i = 0; i < operands->length(); i++) {
901 LOperand* pointer = operands->at(i);
902 if (pointer->IsStackSlot()) {
903 safepoint.DefinePointerSlot(pointer->index(), zone());
904 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
905 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
911 void LCodeGen::RecordSafepoint(LPointerMap* pointers,
912 Safepoint::DeoptMode deopt_mode) {
913 RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
917 void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
918 LPointerMap empty_pointers(zone());
919 RecordSafepoint(&empty_pointers, deopt_mode);
923 void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
925 Safepoint::DeoptMode deopt_mode) {
926 RecordSafepoint(pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
930 void LCodeGen::RecordAndWritePosition(int position) {
931 if (position == RelocInfo::kNoPosition) return;
932 masm()->positions_recorder()->RecordPosition(position);
933 masm()->positions_recorder()->WriteRecordedPositions();
937 static const char* LabelType(LLabel* label) {
938 if (label->is_loop_header()) return " (loop header)";
939 if (label->is_osr_entry()) return " (OSR entry)";
944 void LCodeGen::DoLabel(LLabel* label) {
945 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
946 current_instruction_,
947 label->hydrogen_value()->id(),
950 __ bind(label->label());
951 current_block_ = label->block_id();
956 void LCodeGen::DoParallelMove(LParallelMove* move) {
957 resolver_.Resolve(move);
961 void LCodeGen::DoGap(LGap* gap) {
962 for (int i = LGap::FIRST_INNER_POSITION;
963 i <= LGap::LAST_INNER_POSITION;
965 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
966 LParallelMove* move = gap->GetParallelMove(inner_pos);
967 if (move != NULL) DoParallelMove(move);
972 void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
977 void LCodeGen::DoParameter(LParameter* instr) {
982 void LCodeGen::DoCallStub(LCallStub* instr) {
983 DCHECK(ToRegister(instr->context()).is(rsi));
984 DCHECK(ToRegister(instr->result()).is(rax));
985 switch (instr->hydrogen()->major_key()) {
986 case CodeStub::RegExpExec: {
987 RegExpExecStub stub(isolate());
988 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
991 case CodeStub::SubString: {
992 SubStringStub stub(isolate());
993 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
996 case CodeStub::StringCompare: {
997 StringCompareStub stub(isolate());
998 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1007 void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
1008 GenerateOsrPrologue();
1012 void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
1013 Register dividend = ToRegister(instr->dividend());
1014 int32_t divisor = instr->divisor();
1015 DCHECK(dividend.is(ToRegister(instr->result())));
1017 // Theoretically, a variation of the branch-free code for integer division by
1018 // a power of 2 (calculating the remainder via an additional multiplication
1019 // (which gets simplified to an 'and') and subtraction) should be faster, and
1020 // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
1021 // indicate that positive dividends are heavily favored, so the branching
1022 // version performs better.
1023 HMod* hmod = instr->hydrogen();
1024 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1025 Label dividend_is_not_negative, done;
1026 if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
1027 __ testl(dividend, dividend);
1028 __ j(not_sign, ÷nd_is_not_negative, Label::kNear);
1029 // Note that this is correct even for kMinInt operands.
1031 __ andl(dividend, Immediate(mask));
1033 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1034 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1036 __ jmp(&done, Label::kNear);
1039 __ bind(÷nd_is_not_negative);
1040 __ andl(dividend, Immediate(mask));
1045 void LCodeGen::DoModByConstI(LModByConstI* instr) {
1046 Register dividend = ToRegister(instr->dividend());
1047 int32_t divisor = instr->divisor();
1048 DCHECK(ToRegister(instr->result()).is(rax));
1051 DeoptimizeIf(no_condition, instr, Deoptimizer::kDivisionByZero);
1055 __ TruncatingDiv(dividend, Abs(divisor));
1056 __ imull(rdx, rdx, Immediate(Abs(divisor)));
1057 __ movl(rax, dividend);
1060 // Check for negative zero.
1061 HMod* hmod = instr->hydrogen();
1062 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1063 Label remainder_not_zero;
1064 __ j(not_zero, &remainder_not_zero, Label::kNear);
1065 __ cmpl(dividend, Immediate(0));
1066 DeoptimizeIf(less, instr, Deoptimizer::kMinusZero);
1067 __ bind(&remainder_not_zero);
1072 void LCodeGen::DoModI(LModI* instr) {
1073 HMod* hmod = instr->hydrogen();
1075 Register left_reg = ToRegister(instr->left());
1076 DCHECK(left_reg.is(rax));
1077 Register right_reg = ToRegister(instr->right());
1078 DCHECK(!right_reg.is(rax));
1079 DCHECK(!right_reg.is(rdx));
1080 Register result_reg = ToRegister(instr->result());
1081 DCHECK(result_reg.is(rdx));
1084 // Check for x % 0, idiv would signal a divide error. We have to
1085 // deopt in this case because we can't return a NaN.
1086 if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
1087 __ testl(right_reg, right_reg);
1088 DeoptimizeIf(zero, instr, Deoptimizer::kDivisionByZero);
1091 // Check for kMinInt % -1, idiv would signal a divide error. We
1092 // have to deopt if we care about -0, because we can't return that.
1093 if (hmod->CheckFlag(HValue::kCanOverflow)) {
1094 Label no_overflow_possible;
1095 __ cmpl(left_reg, Immediate(kMinInt));
1096 __ j(not_zero, &no_overflow_possible, Label::kNear);
1097 __ cmpl(right_reg, Immediate(-1));
1098 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1099 DeoptimizeIf(equal, instr, Deoptimizer::kMinusZero);
1101 __ j(not_equal, &no_overflow_possible, Label::kNear);
1102 __ Set(result_reg, 0);
1103 __ jmp(&done, Label::kNear);
1105 __ bind(&no_overflow_possible);
1108 // Sign extend dividend in eax into edx:eax, since we are using only the low
1109 // 32 bits of the values.
1112 // If we care about -0, test if the dividend is <0 and the result is 0.
1113 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1114 Label positive_left;
1115 __ testl(left_reg, left_reg);
1116 __ j(not_sign, &positive_left, Label::kNear);
1117 __ idivl(right_reg);
1118 __ testl(result_reg, result_reg);
1119 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1120 __ jmp(&done, Label::kNear);
1121 __ bind(&positive_left);
1123 __ idivl(right_reg);
1128 void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
1129 Register dividend = ToRegister(instr->dividend());
1130 int32_t divisor = instr->divisor();
1131 DCHECK(dividend.is(ToRegister(instr->result())));
1133 // If the divisor is positive, things are easy: There can be no deopts and we
1134 // can simply do an arithmetic right shift.
1135 if (divisor == 1) return;
1136 int32_t shift = WhichPowerOf2Abs(divisor);
1138 __ sarl(dividend, Immediate(shift));
1142 // If the divisor is negative, we have to negate and handle edge cases.
1144 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1145 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1148 // Dividing by -1 is basically negation, unless we overflow.
1149 if (divisor == -1) {
1150 if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1151 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1156 // If the negation could not overflow, simply shifting is OK.
1157 if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1158 __ sarl(dividend, Immediate(shift));
1162 Label not_kmin_int, done;
1163 __ j(no_overflow, ¬_kmin_int, Label::kNear);
1164 __ movl(dividend, Immediate(kMinInt / divisor));
1165 __ jmp(&done, Label::kNear);
1166 __ bind(¬_kmin_int);
1167 __ sarl(dividend, Immediate(shift));
1172 void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
1173 Register dividend = ToRegister(instr->dividend());
1174 int32_t divisor = instr->divisor();
1175 DCHECK(ToRegister(instr->result()).is(rdx));
1178 DeoptimizeIf(no_condition, instr, Deoptimizer::kDivisionByZero);
1182 // Check for (0 / -x) that will produce negative zero.
1183 HMathFloorOfDiv* hdiv = instr->hydrogen();
1184 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1185 __ testl(dividend, dividend);
1186 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1189 // Easy case: We need no dynamic check for the dividend and the flooring
1190 // division is the same as the truncating division.
1191 if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
1192 (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
1193 __ TruncatingDiv(dividend, Abs(divisor));
1194 if (divisor < 0) __ negl(rdx);
1198 // In the general case we may need to adjust before and after the truncating
1199 // division to get a flooring division.
1200 Register temp = ToRegister(instr->temp3());
1201 DCHECK(!temp.is(dividend) && !temp.is(rax) && !temp.is(rdx));
1202 Label needs_adjustment, done;
1203 __ cmpl(dividend, Immediate(0));
1204 __ j(divisor > 0 ? less : greater, &needs_adjustment, Label::kNear);
1205 __ TruncatingDiv(dividend, Abs(divisor));
1206 if (divisor < 0) __ negl(rdx);
1207 __ jmp(&done, Label::kNear);
1208 __ bind(&needs_adjustment);
1209 __ leal(temp, Operand(dividend, divisor > 0 ? 1 : -1));
1210 __ TruncatingDiv(temp, Abs(divisor));
1211 if (divisor < 0) __ negl(rdx);
1217 // TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
1218 void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
1219 HBinaryOperation* hdiv = instr->hydrogen();
1220 Register dividend = ToRegister(instr->dividend());
1221 Register divisor = ToRegister(instr->divisor());
1222 Register remainder = ToRegister(instr->temp());
1223 Register result = ToRegister(instr->result());
1224 DCHECK(dividend.is(rax));
1225 DCHECK(remainder.is(rdx));
1226 DCHECK(result.is(rax));
1227 DCHECK(!divisor.is(rax));
1228 DCHECK(!divisor.is(rdx));
1231 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1232 __ testl(divisor, divisor);
1233 DeoptimizeIf(zero, instr, Deoptimizer::kDivisionByZero);
1236 // Check for (0 / -x) that will produce negative zero.
1237 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1238 Label dividend_not_zero;
1239 __ testl(dividend, dividend);
1240 __ j(not_zero, ÷nd_not_zero, Label::kNear);
1241 __ testl(divisor, divisor);
1242 DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero);
1243 __ bind(÷nd_not_zero);
1246 // Check for (kMinInt / -1).
1247 if (hdiv->CheckFlag(HValue::kCanOverflow)) {
1248 Label dividend_not_min_int;
1249 __ cmpl(dividend, Immediate(kMinInt));
1250 __ j(not_zero, ÷nd_not_min_int, Label::kNear);
1251 __ cmpl(divisor, Immediate(-1));
1252 DeoptimizeIf(zero, instr, Deoptimizer::kOverflow);
1253 __ bind(÷nd_not_min_int);
1256 // Sign extend to rdx (= remainder).
1261 __ testl(remainder, remainder);
1262 __ j(zero, &done, Label::kNear);
1263 __ xorl(remainder, divisor);
1264 __ sarl(remainder, Immediate(31));
1265 __ addl(result, remainder);
1270 void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
1271 Register dividend = ToRegister(instr->dividend());
1272 int32_t divisor = instr->divisor();
1273 Register result = ToRegister(instr->result());
1274 DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
1275 DCHECK(!result.is(dividend));
1277 // Check for (0 / -x) that will produce negative zero.
1278 HDiv* hdiv = instr->hydrogen();
1279 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1280 __ testl(dividend, dividend);
1281 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1283 // Check for (kMinInt / -1).
1284 if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
1285 __ cmpl(dividend, Immediate(kMinInt));
1286 DeoptimizeIf(zero, instr, Deoptimizer::kOverflow);
1288 // Deoptimize if remainder will not be 0.
1289 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1290 divisor != 1 && divisor != -1) {
1291 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1292 __ testl(dividend, Immediate(mask));
1293 DeoptimizeIf(not_zero, instr, Deoptimizer::kLostPrecision);
1295 __ Move(result, dividend);
1296 int32_t shift = WhichPowerOf2Abs(divisor);
1298 // The arithmetic shift is always OK, the 'if' is an optimization only.
1299 if (shift > 1) __ sarl(result, Immediate(31));
1300 __ shrl(result, Immediate(32 - shift));
1301 __ addl(result, dividend);
1302 __ sarl(result, Immediate(shift));
1304 if (divisor < 0) __ negl(result);
1308 void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
1309 Register dividend = ToRegister(instr->dividend());
1310 int32_t divisor = instr->divisor();
1311 DCHECK(ToRegister(instr->result()).is(rdx));
1314 DeoptimizeIf(no_condition, instr, Deoptimizer::kDivisionByZero);
1318 // Check for (0 / -x) that will produce negative zero.
1319 HDiv* hdiv = instr->hydrogen();
1320 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1321 __ testl(dividend, dividend);
1322 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1325 __ TruncatingDiv(dividend, Abs(divisor));
1326 if (divisor < 0) __ negl(rdx);
1328 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1330 __ imull(rax, rax, Immediate(divisor));
1331 __ subl(rax, dividend);
1332 DeoptimizeIf(not_equal, instr, Deoptimizer::kLostPrecision);
1337 // TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
1338 void LCodeGen::DoDivI(LDivI* instr) {
1339 HBinaryOperation* hdiv = instr->hydrogen();
1340 Register dividend = ToRegister(instr->dividend());
1341 Register divisor = ToRegister(instr->divisor());
1342 Register remainder = ToRegister(instr->temp());
1343 DCHECK(dividend.is(rax));
1344 DCHECK(remainder.is(rdx));
1345 DCHECK(ToRegister(instr->result()).is(rax));
1346 DCHECK(!divisor.is(rax));
1347 DCHECK(!divisor.is(rdx));
1350 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1351 __ testl(divisor, divisor);
1352 DeoptimizeIf(zero, instr, Deoptimizer::kDivisionByZero);
1355 // Check for (0 / -x) that will produce negative zero.
1356 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1357 Label dividend_not_zero;
1358 __ testl(dividend, dividend);
1359 __ j(not_zero, ÷nd_not_zero, Label::kNear);
1360 __ testl(divisor, divisor);
1361 DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero);
1362 __ bind(÷nd_not_zero);
1365 // Check for (kMinInt / -1).
1366 if (hdiv->CheckFlag(HValue::kCanOverflow)) {
1367 Label dividend_not_min_int;
1368 __ cmpl(dividend, Immediate(kMinInt));
1369 __ j(not_zero, ÷nd_not_min_int, Label::kNear);
1370 __ cmpl(divisor, Immediate(-1));
1371 DeoptimizeIf(zero, instr, Deoptimizer::kOverflow);
1372 __ bind(÷nd_not_min_int);
1375 // Sign extend to rdx (= remainder).
1379 if (!hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1380 // Deoptimize if remainder is not 0.
1381 __ testl(remainder, remainder);
1382 DeoptimizeIf(not_zero, instr, Deoptimizer::kLostPrecision);
1387 void LCodeGen::DoMulI(LMulI* instr) {
1388 Register left = ToRegister(instr->left());
1389 LOperand* right = instr->right();
1391 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1392 if (instr->hydrogen_value()->representation().IsSmi()) {
1393 __ movp(kScratchRegister, left);
1395 __ movl(kScratchRegister, left);
1400 instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1401 if (right->IsConstantOperand()) {
1402 int32_t right_value = ToInteger32(LConstantOperand::cast(right));
1403 if (right_value == -1) {
1405 } else if (right_value == 0) {
1406 __ xorl(left, left);
1407 } else if (right_value == 2) {
1408 __ addl(left, left);
1409 } else if (!can_overflow) {
1410 // If the multiplication is known to not overflow, we
1411 // can use operations that don't set the overflow flag
1413 switch (right_value) {
1418 __ leal(left, Operand(left, left, times_2, 0));
1421 __ shll(left, Immediate(2));
1424 __ leal(left, Operand(left, left, times_4, 0));
1427 __ shll(left, Immediate(3));
1430 __ leal(left, Operand(left, left, times_8, 0));
1433 __ shll(left, Immediate(4));
1436 __ imull(left, left, Immediate(right_value));
1440 __ imull(left, left, Immediate(right_value));
1442 } else if (right->IsStackSlot()) {
1443 if (instr->hydrogen_value()->representation().IsSmi()) {
1444 __ SmiToInteger64(left, left);
1445 __ imulp(left, ToOperand(right));
1447 __ imull(left, ToOperand(right));
1450 if (instr->hydrogen_value()->representation().IsSmi()) {
1451 __ SmiToInteger64(left, left);
1452 __ imulp(left, ToRegister(right));
1454 __ imull(left, ToRegister(right));
1459 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1462 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1463 // Bail out if the result is supposed to be negative zero.
1465 if (instr->hydrogen_value()->representation().IsSmi()) {
1466 __ testp(left, left);
1468 __ testl(left, left);
1470 __ j(not_zero, &done, Label::kNear);
1471 if (right->IsConstantOperand()) {
1472 // Constant can't be represented as 32-bit Smi due to immediate size
1474 DCHECK(SmiValuesAre32Bits()
1475 ? !instr->hydrogen_value()->representation().IsSmi()
1476 : SmiValuesAre31Bits());
1477 if (ToInteger32(LConstantOperand::cast(right)) < 0) {
1478 DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero);
1479 } else if (ToInteger32(LConstantOperand::cast(right)) == 0) {
1480 __ cmpl(kScratchRegister, Immediate(0));
1481 DeoptimizeIf(less, instr, Deoptimizer::kMinusZero);
1483 } else if (right->IsStackSlot()) {
1484 if (instr->hydrogen_value()->representation().IsSmi()) {
1485 __ orp(kScratchRegister, ToOperand(right));
1487 __ orl(kScratchRegister, ToOperand(right));
1489 DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero);
1491 // Test the non-zero operand for negative sign.
1492 if (instr->hydrogen_value()->representation().IsSmi()) {
1493 __ orp(kScratchRegister, ToRegister(right));
1495 __ orl(kScratchRegister, ToRegister(right));
1497 DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero);
1504 void LCodeGen::DoBitI(LBitI* instr) {
1505 LOperand* left = instr->left();
1506 LOperand* right = instr->right();
1507 DCHECK(left->Equals(instr->result()));
1508 DCHECK(left->IsRegister());
1510 if (right->IsConstantOperand()) {
1511 int32_t right_operand =
1512 ToRepresentation(LConstantOperand::cast(right),
1513 instr->hydrogen()->right()->representation());
1514 switch (instr->op()) {
1515 case Token::BIT_AND:
1516 __ andl(ToRegister(left), Immediate(right_operand));
1519 __ orl(ToRegister(left), Immediate(right_operand));
1521 case Token::BIT_XOR:
1522 if (right_operand == int32_t(~0)) {
1523 __ notl(ToRegister(left));
1525 __ xorl(ToRegister(left), Immediate(right_operand));
1532 } else if (right->IsStackSlot()) {
1533 switch (instr->op()) {
1534 case Token::BIT_AND:
1535 if (instr->IsInteger32()) {
1536 __ andl(ToRegister(left), ToOperand(right));
1538 __ andp(ToRegister(left), ToOperand(right));
1542 if (instr->IsInteger32()) {
1543 __ orl(ToRegister(left), ToOperand(right));
1545 __ orp(ToRegister(left), ToOperand(right));
1548 case Token::BIT_XOR:
1549 if (instr->IsInteger32()) {
1550 __ xorl(ToRegister(left), ToOperand(right));
1552 __ xorp(ToRegister(left), ToOperand(right));
1560 DCHECK(right->IsRegister());
1561 switch (instr->op()) {
1562 case Token::BIT_AND:
1563 if (instr->IsInteger32()) {
1564 __ andl(ToRegister(left), ToRegister(right));
1566 __ andp(ToRegister(left), ToRegister(right));
1570 if (instr->IsInteger32()) {
1571 __ orl(ToRegister(left), ToRegister(right));
1573 __ orp(ToRegister(left), ToRegister(right));
1576 case Token::BIT_XOR:
1577 if (instr->IsInteger32()) {
1578 __ xorl(ToRegister(left), ToRegister(right));
1580 __ xorp(ToRegister(left), ToRegister(right));
1591 void LCodeGen::DoShiftI(LShiftI* instr) {
1592 LOperand* left = instr->left();
1593 LOperand* right = instr->right();
1594 DCHECK(left->Equals(instr->result()));
1595 DCHECK(left->IsRegister());
1596 if (right->IsRegister()) {
1597 DCHECK(ToRegister(right).is(rcx));
1599 switch (instr->op()) {
1601 __ rorl_cl(ToRegister(left));
1604 __ sarl_cl(ToRegister(left));
1607 __ shrl_cl(ToRegister(left));
1608 if (instr->can_deopt()) {
1609 __ testl(ToRegister(left), ToRegister(left));
1610 DeoptimizeIf(negative, instr, Deoptimizer::kNegativeValue);
1614 __ shll_cl(ToRegister(left));
1621 int32_t value = ToInteger32(LConstantOperand::cast(right));
1622 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1623 switch (instr->op()) {
1625 if (shift_count != 0) {
1626 __ rorl(ToRegister(left), Immediate(shift_count));
1630 if (shift_count != 0) {
1631 __ sarl(ToRegister(left), Immediate(shift_count));
1635 if (shift_count != 0) {
1636 __ shrl(ToRegister(left), Immediate(shift_count));
1637 } else if (instr->can_deopt()) {
1638 __ testl(ToRegister(left), ToRegister(left));
1639 DeoptimizeIf(negative, instr, Deoptimizer::kNegativeValue);
1643 if (shift_count != 0) {
1644 if (instr->hydrogen_value()->representation().IsSmi()) {
1645 if (SmiValuesAre32Bits()) {
1646 __ shlp(ToRegister(left), Immediate(shift_count));
1648 DCHECK(SmiValuesAre31Bits());
1649 if (instr->can_deopt()) {
1650 if (shift_count != 1) {
1651 __ shll(ToRegister(left), Immediate(shift_count - 1));
1653 __ Integer32ToSmi(ToRegister(left), ToRegister(left));
1654 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1656 __ shll(ToRegister(left), Immediate(shift_count));
1660 __ shll(ToRegister(left), Immediate(shift_count));
1672 void LCodeGen::DoSubI(LSubI* instr) {
1673 LOperand* left = instr->left();
1674 LOperand* right = instr->right();
1675 DCHECK(left->Equals(instr->result()));
1677 if (right->IsConstantOperand()) {
1678 int32_t right_operand =
1679 ToRepresentation(LConstantOperand::cast(right),
1680 instr->hydrogen()->right()->representation());
1681 __ subl(ToRegister(left), Immediate(right_operand));
1682 } else if (right->IsRegister()) {
1683 if (instr->hydrogen_value()->representation().IsSmi()) {
1684 __ subp(ToRegister(left), ToRegister(right));
1686 __ subl(ToRegister(left), ToRegister(right));
1689 if (instr->hydrogen_value()->representation().IsSmi()) {
1690 __ subp(ToRegister(left), ToOperand(right));
1692 __ subl(ToRegister(left), ToOperand(right));
1696 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1697 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1702 void LCodeGen::DoConstantI(LConstantI* instr) {
1703 Register dst = ToRegister(instr->result());
1704 if (instr->value() == 0) {
1707 __ movl(dst, Immediate(instr->value()));
1712 void LCodeGen::DoConstantS(LConstantS* instr) {
1713 __ Move(ToRegister(instr->result()), instr->value());
1717 void LCodeGen::DoConstantD(LConstantD* instr) {
1718 __ Move(ToDoubleRegister(instr->result()), instr->bits());
1722 void LCodeGen::DoConstantE(LConstantE* instr) {
1723 __ LoadAddress(ToRegister(instr->result()), instr->value());
1727 void LCodeGen::DoConstantT(LConstantT* instr) {
1728 Handle<Object> object = instr->value(isolate());
1729 AllowDeferredHandleDereference smi_check;
1730 __ Move(ToRegister(instr->result()), object);
1734 void LCodeGen::DoMapEnumLength(LMapEnumLength* instr) {
1735 Register result = ToRegister(instr->result());
1736 Register map = ToRegister(instr->value());
1737 __ EnumLength(result, map);
1741 void LCodeGen::DoDateField(LDateField* instr) {
1742 Register object = ToRegister(instr->date());
1743 Register result = ToRegister(instr->result());
1744 Smi* index = instr->index();
1745 DCHECK(object.is(result));
1746 DCHECK(object.is(rax));
1748 if (FLAG_debug_code) {
1749 __ AssertNotSmi(object);
1750 __ CmpObjectType(object, JS_DATE_TYPE, kScratchRegister);
1751 __ Check(equal, kOperandIsNotADate);
1754 if (index->value() == 0) {
1755 __ movp(result, FieldOperand(object, JSDate::kValueOffset));
1757 Label runtime, done;
1758 if (index->value() < JSDate::kFirstUncachedField) {
1759 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
1760 Operand stamp_operand = __ ExternalOperand(stamp);
1761 __ movp(kScratchRegister, stamp_operand);
1762 __ cmpp(kScratchRegister, FieldOperand(object,
1763 JSDate::kCacheStampOffset));
1764 __ j(not_equal, &runtime, Label::kNear);
1765 __ movp(result, FieldOperand(object, JSDate::kValueOffset +
1766 kPointerSize * index->value()));
1767 __ jmp(&done, Label::kNear);
1770 __ PrepareCallCFunction(2);
1771 __ movp(arg_reg_1, object);
1772 __ Move(arg_reg_2, index, Assembler::RelocInfoNone());
1773 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
1779 Operand LCodeGen::BuildSeqStringOperand(Register string,
1781 String::Encoding encoding) {
1782 if (index->IsConstantOperand()) {
1783 int offset = ToInteger32(LConstantOperand::cast(index));
1784 if (encoding == String::TWO_BYTE_ENCODING) {
1785 offset *= kUC16Size;
1787 STATIC_ASSERT(kCharSize == 1);
1788 return FieldOperand(string, SeqString::kHeaderSize + offset);
1790 return FieldOperand(
1791 string, ToRegister(index),
1792 encoding == String::ONE_BYTE_ENCODING ? times_1 : times_2,
1793 SeqString::kHeaderSize);
1797 void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
1798 String::Encoding encoding = instr->hydrogen()->encoding();
1799 Register result = ToRegister(instr->result());
1800 Register string = ToRegister(instr->string());
1802 if (FLAG_debug_code) {
1804 __ movp(string, FieldOperand(string, HeapObject::kMapOffset));
1805 __ movzxbp(string, FieldOperand(string, Map::kInstanceTypeOffset));
1807 __ andb(string, Immediate(kStringRepresentationMask | kStringEncodingMask));
1808 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1809 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1810 __ cmpp(string, Immediate(encoding == String::ONE_BYTE_ENCODING
1811 ? one_byte_seq_type : two_byte_seq_type));
1812 __ Check(equal, kUnexpectedStringType);
1816 Operand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1817 if (encoding == String::ONE_BYTE_ENCODING) {
1818 __ movzxbl(result, operand);
1820 __ movzxwl(result, operand);
1825 void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
1826 String::Encoding encoding = instr->hydrogen()->encoding();
1827 Register string = ToRegister(instr->string());
1829 if (FLAG_debug_code) {
1830 Register value = ToRegister(instr->value());
1831 Register index = ToRegister(instr->index());
1832 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1833 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1835 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
1836 ? one_byte_seq_type : two_byte_seq_type;
1837 __ EmitSeqStringSetCharCheck(string, index, value, encoding_mask);
1840 Operand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1841 if (instr->value()->IsConstantOperand()) {
1842 int value = ToInteger32(LConstantOperand::cast(instr->value()));
1843 DCHECK_LE(0, value);
1844 if (encoding == String::ONE_BYTE_ENCODING) {
1845 DCHECK_LE(value, String::kMaxOneByteCharCode);
1846 __ movb(operand, Immediate(value));
1848 DCHECK_LE(value, String::kMaxUtf16CodeUnit);
1849 __ movw(operand, Immediate(value));
1852 Register value = ToRegister(instr->value());
1853 if (encoding == String::ONE_BYTE_ENCODING) {
1854 __ movb(operand, value);
1856 __ movw(operand, value);
1862 void LCodeGen::DoAddI(LAddI* instr) {
1863 LOperand* left = instr->left();
1864 LOperand* right = instr->right();
1866 Representation target_rep = instr->hydrogen()->representation();
1867 bool is_p = target_rep.IsSmi() || target_rep.IsExternal();
1869 if (LAddI::UseLea(instr->hydrogen()) && !left->Equals(instr->result())) {
1870 if (right->IsConstantOperand()) {
1871 // No support for smi-immediates for 32-bit SMI.
1872 DCHECK(SmiValuesAre32Bits() ? !target_rep.IsSmi() : SmiValuesAre31Bits());
1874 ToRepresentation(LConstantOperand::cast(right),
1875 instr->hydrogen()->right()->representation());
1877 __ leap(ToRegister(instr->result()),
1878 MemOperand(ToRegister(left), offset));
1880 __ leal(ToRegister(instr->result()),
1881 MemOperand(ToRegister(left), offset));
1884 Operand address(ToRegister(left), ToRegister(right), times_1, 0);
1886 __ leap(ToRegister(instr->result()), address);
1888 __ leal(ToRegister(instr->result()), address);
1892 if (right->IsConstantOperand()) {
1893 // No support for smi-immediates for 32-bit SMI.
1894 DCHECK(SmiValuesAre32Bits() ? !target_rep.IsSmi() : SmiValuesAre31Bits());
1895 int32_t right_operand =
1896 ToRepresentation(LConstantOperand::cast(right),
1897 instr->hydrogen()->right()->representation());
1899 __ addp(ToRegister(left), Immediate(right_operand));
1901 __ addl(ToRegister(left), Immediate(right_operand));
1903 } else if (right->IsRegister()) {
1905 __ addp(ToRegister(left), ToRegister(right));
1907 __ addl(ToRegister(left), ToRegister(right));
1911 __ addp(ToRegister(left), ToOperand(right));
1913 __ addl(ToRegister(left), ToOperand(right));
1916 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1917 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1923 void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
1924 LOperand* left = instr->left();
1925 LOperand* right = instr->right();
1926 DCHECK(left->Equals(instr->result()));
1927 HMathMinMax::Operation operation = instr->hydrogen()->operation();
1928 if (instr->hydrogen()->representation().IsSmiOrInteger32()) {
1930 Condition condition = (operation == HMathMinMax::kMathMin)
1933 Register left_reg = ToRegister(left);
1934 if (right->IsConstantOperand()) {
1935 Immediate right_imm = Immediate(
1936 ToRepresentation(LConstantOperand::cast(right),
1937 instr->hydrogen()->right()->representation()));
1938 DCHECK(SmiValuesAre32Bits()
1939 ? !instr->hydrogen()->representation().IsSmi()
1940 : SmiValuesAre31Bits());
1941 __ cmpl(left_reg, right_imm);
1942 __ j(condition, &return_left, Label::kNear);
1943 __ movp(left_reg, right_imm);
1944 } else if (right->IsRegister()) {
1945 Register right_reg = ToRegister(right);
1946 if (instr->hydrogen_value()->representation().IsSmi()) {
1947 __ cmpp(left_reg, right_reg);
1949 __ cmpl(left_reg, right_reg);
1951 __ j(condition, &return_left, Label::kNear);
1952 __ movp(left_reg, right_reg);
1954 Operand right_op = ToOperand(right);
1955 if (instr->hydrogen_value()->representation().IsSmi()) {
1956 __ cmpp(left_reg, right_op);
1958 __ cmpl(left_reg, right_op);
1960 __ j(condition, &return_left, Label::kNear);
1961 __ movp(left_reg, right_op);
1963 __ bind(&return_left);
1965 DCHECK(instr->hydrogen()->representation().IsDouble());
1966 Label check_nan_left, check_zero, return_left, return_right;
1967 Condition condition = (operation == HMathMinMax::kMathMin) ? below : above;
1968 XMMRegister left_reg = ToDoubleRegister(left);
1969 XMMRegister right_reg = ToDoubleRegister(right);
1970 __ ucomisd(left_reg, right_reg);
1971 __ j(parity_even, &check_nan_left, Label::kNear); // At least one NaN.
1972 __ j(equal, &check_zero, Label::kNear); // left == right.
1973 __ j(condition, &return_left, Label::kNear);
1974 __ jmp(&return_right, Label::kNear);
1976 __ bind(&check_zero);
1977 XMMRegister xmm_scratch = double_scratch0();
1978 __ xorps(xmm_scratch, xmm_scratch);
1979 __ ucomisd(left_reg, xmm_scratch);
1980 __ j(not_equal, &return_left, Label::kNear); // left == right != 0.
1981 // At this point, both left and right are either 0 or -0.
1982 if (operation == HMathMinMax::kMathMin) {
1983 __ orps(left_reg, right_reg);
1985 // Since we operate on +0 and/or -0, addsd and andsd have the same effect.
1986 __ addsd(left_reg, right_reg);
1988 __ jmp(&return_left, Label::kNear);
1990 __ bind(&check_nan_left);
1991 __ ucomisd(left_reg, left_reg); // NaN check.
1992 __ j(parity_even, &return_left, Label::kNear);
1993 __ bind(&return_right);
1994 __ movaps(left_reg, right_reg);
1996 __ bind(&return_left);
2001 void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
2002 XMMRegister left = ToDoubleRegister(instr->left());
2003 XMMRegister right = ToDoubleRegister(instr->right());
2004 XMMRegister result = ToDoubleRegister(instr->result());
2005 switch (instr->op()) {
2007 if (CpuFeatures::IsSupported(AVX)) {
2008 CpuFeatureScope scope(masm(), AVX);
2009 __ vaddsd(result, left, right);
2011 DCHECK(result.is(left));
2012 __ addsd(left, right);
2016 if (CpuFeatures::IsSupported(AVX)) {
2017 CpuFeatureScope scope(masm(), AVX);
2018 __ vsubsd(result, left, right);
2020 DCHECK(result.is(left));
2021 __ subsd(left, right);
2025 if (CpuFeatures::IsSupported(AVX)) {
2026 CpuFeatureScope scope(masm(), AVX);
2027 __ vmulsd(result, left, right);
2029 DCHECK(result.is(left));
2030 __ mulsd(left, right);
2034 if (CpuFeatures::IsSupported(AVX)) {
2035 CpuFeatureScope scope(masm(), AVX);
2036 __ vdivsd(result, left, right);
2038 DCHECK(result.is(left));
2039 __ divsd(left, right);
2041 // Don't delete this mov. It may improve performance on some CPUs,
2042 // when there is a (v)mulsd depending on the result
2043 __ movaps(result, result);
2046 XMMRegister xmm_scratch = double_scratch0();
2047 __ PrepareCallCFunction(2);
2048 __ movaps(xmm_scratch, left);
2049 DCHECK(right.is(xmm1));
2051 ExternalReference::mod_two_doubles_operation(isolate()), 2);
2052 __ movaps(result, xmm_scratch);
2062 void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
2063 DCHECK(ToRegister(instr->context()).is(rsi));
2064 DCHECK(ToRegister(instr->left()).is(rdx));
2065 DCHECK(ToRegister(instr->right()).is(rax));
2066 DCHECK(ToRegister(instr->result()).is(rax));
2069 CodeFactory::BinaryOpIC(isolate(), instr->op(), instr->strength()).code();
2070 CallCode(code, RelocInfo::CODE_TARGET, instr);
2074 template<class InstrType>
2075 void LCodeGen::EmitBranch(InstrType instr, Condition cc) {
2076 int left_block = instr->TrueDestination(chunk_);
2077 int right_block = instr->FalseDestination(chunk_);
2079 int next_block = GetNextEmittedBlock();
2081 if (right_block == left_block || cc == no_condition) {
2082 EmitGoto(left_block);
2083 } else if (left_block == next_block) {
2084 __ j(NegateCondition(cc), chunk_->GetAssemblyLabel(right_block));
2085 } else if (right_block == next_block) {
2086 __ j(cc, chunk_->GetAssemblyLabel(left_block));
2088 __ j(cc, chunk_->GetAssemblyLabel(left_block));
2090 __ jmp(chunk_->GetAssemblyLabel(right_block));
2096 template <class InstrType>
2097 void LCodeGen::EmitTrueBranch(InstrType instr, Condition cc) {
2098 int true_block = instr->TrueDestination(chunk_);
2099 __ j(cc, chunk_->GetAssemblyLabel(true_block));
2103 template <class InstrType>
2104 void LCodeGen::EmitFalseBranch(InstrType instr, Condition cc) {
2105 int false_block = instr->FalseDestination(chunk_);
2106 __ j(cc, chunk_->GetAssemblyLabel(false_block));
2110 void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
2115 void LCodeGen::DoBranch(LBranch* instr) {
2116 Representation r = instr->hydrogen()->value()->representation();
2117 if (r.IsInteger32()) {
2118 DCHECK(!info()->IsStub());
2119 Register reg = ToRegister(instr->value());
2121 EmitBranch(instr, not_zero);
2122 } else if (r.IsSmi()) {
2123 DCHECK(!info()->IsStub());
2124 Register reg = ToRegister(instr->value());
2126 EmitBranch(instr, not_zero);
2127 } else if (r.IsDouble()) {
2128 DCHECK(!info()->IsStub());
2129 XMMRegister reg = ToDoubleRegister(instr->value());
2130 XMMRegister xmm_scratch = double_scratch0();
2131 __ xorps(xmm_scratch, xmm_scratch);
2132 __ ucomisd(reg, xmm_scratch);
2133 EmitBranch(instr, not_equal);
2135 DCHECK(r.IsTagged());
2136 Register reg = ToRegister(instr->value());
2137 HType type = instr->hydrogen()->value()->type();
2138 if (type.IsBoolean()) {
2139 DCHECK(!info()->IsStub());
2140 __ CompareRoot(reg, Heap::kTrueValueRootIndex);
2141 EmitBranch(instr, equal);
2142 } else if (type.IsSmi()) {
2143 DCHECK(!info()->IsStub());
2144 __ SmiCompare(reg, Smi::FromInt(0));
2145 EmitBranch(instr, not_equal);
2146 } else if (type.IsJSArray()) {
2147 DCHECK(!info()->IsStub());
2148 EmitBranch(instr, no_condition);
2149 } else if (type.IsHeapNumber()) {
2150 DCHECK(!info()->IsStub());
2151 XMMRegister xmm_scratch = double_scratch0();
2152 __ xorps(xmm_scratch, xmm_scratch);
2153 __ ucomisd(xmm_scratch, FieldOperand(reg, HeapNumber::kValueOffset));
2154 EmitBranch(instr, not_equal);
2155 } else if (type.IsString()) {
2156 DCHECK(!info()->IsStub());
2157 __ cmpp(FieldOperand(reg, String::kLengthOffset), Immediate(0));
2158 EmitBranch(instr, not_equal);
2160 ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
2161 // Avoid deopts in the case where we've never executed this path before.
2162 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
2164 if (expected.Contains(ToBooleanStub::UNDEFINED)) {
2165 // undefined -> false.
2166 __ CompareRoot(reg, Heap::kUndefinedValueRootIndex);
2167 __ j(equal, instr->FalseLabel(chunk_));
2169 if (expected.Contains(ToBooleanStub::BOOLEAN)) {
2171 __ CompareRoot(reg, Heap::kTrueValueRootIndex);
2172 __ j(equal, instr->TrueLabel(chunk_));
2174 __ CompareRoot(reg, Heap::kFalseValueRootIndex);
2175 __ j(equal, instr->FalseLabel(chunk_));
2177 if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
2179 __ CompareRoot(reg, Heap::kNullValueRootIndex);
2180 __ j(equal, instr->FalseLabel(chunk_));
2183 if (expected.Contains(ToBooleanStub::SMI)) {
2184 // Smis: 0 -> false, all other -> true.
2185 __ Cmp(reg, Smi::FromInt(0));
2186 __ j(equal, instr->FalseLabel(chunk_));
2187 __ JumpIfSmi(reg, instr->TrueLabel(chunk_));
2188 } else if (expected.NeedsMap()) {
2189 // If we need a map later and have a Smi -> deopt.
2190 __ testb(reg, Immediate(kSmiTagMask));
2191 DeoptimizeIf(zero, instr, Deoptimizer::kSmi);
2194 const Register map = kScratchRegister;
2195 if (expected.NeedsMap()) {
2196 __ movp(map, FieldOperand(reg, HeapObject::kMapOffset));
2198 if (expected.CanBeUndetectable()) {
2199 // Undetectable -> false.
2200 __ testb(FieldOperand(map, Map::kBitFieldOffset),
2201 Immediate(1 << Map::kIsUndetectable));
2202 __ j(not_zero, instr->FalseLabel(chunk_));
2206 if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
2207 // spec object -> true.
2208 __ CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
2209 __ j(above_equal, instr->TrueLabel(chunk_));
2212 if (expected.Contains(ToBooleanStub::STRING)) {
2213 // String value -> false iff empty.
2215 __ CmpInstanceType(map, FIRST_NONSTRING_TYPE);
2216 __ j(above_equal, ¬_string, Label::kNear);
2217 __ cmpp(FieldOperand(reg, String::kLengthOffset), Immediate(0));
2218 __ j(not_zero, instr->TrueLabel(chunk_));
2219 __ jmp(instr->FalseLabel(chunk_));
2220 __ bind(¬_string);
2223 if (expected.Contains(ToBooleanStub::SYMBOL)) {
2224 // Symbol value -> true.
2225 __ CmpInstanceType(map, SYMBOL_TYPE);
2226 __ j(equal, instr->TrueLabel(chunk_));
2229 if (expected.Contains(ToBooleanStub::SIMD_VALUE)) {
2230 // SIMD value -> true.
2231 __ CmpInstanceType(map, SIMD128_VALUE_TYPE);
2232 __ j(equal, instr->TrueLabel(chunk_));
2235 if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
2236 // heap number -> false iff +0, -0, or NaN.
2237 Label not_heap_number;
2238 __ CompareRoot(map, Heap::kHeapNumberMapRootIndex);
2239 __ j(not_equal, ¬_heap_number, Label::kNear);
2240 XMMRegister xmm_scratch = double_scratch0();
2241 __ xorps(xmm_scratch, xmm_scratch);
2242 __ ucomisd(xmm_scratch, FieldOperand(reg, HeapNumber::kValueOffset));
2243 __ j(zero, instr->FalseLabel(chunk_));
2244 __ jmp(instr->TrueLabel(chunk_));
2245 __ bind(¬_heap_number);
2248 if (!expected.IsGeneric()) {
2249 // We've seen something for the first time -> deopt.
2250 // This can only happen if we are not generic already.
2251 DeoptimizeIf(no_condition, instr, Deoptimizer::kUnexpectedObject);
2258 void LCodeGen::EmitGoto(int block) {
2259 if (!IsNextEmittedBlock(block)) {
2260 __ jmp(chunk_->GetAssemblyLabel(chunk_->LookupDestination(block)));
2265 void LCodeGen::DoGoto(LGoto* instr) {
2266 EmitGoto(instr->block_id());
2270 inline Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
2271 Condition cond = no_condition;
2274 case Token::EQ_STRICT:
2278 case Token::NE_STRICT:
2282 cond = is_unsigned ? below : less;
2285 cond = is_unsigned ? above : greater;
2288 cond = is_unsigned ? below_equal : less_equal;
2291 cond = is_unsigned ? above_equal : greater_equal;
2294 case Token::INSTANCEOF:
2302 void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2303 LOperand* left = instr->left();
2304 LOperand* right = instr->right();
2306 instr->is_double() ||
2307 instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2308 instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2309 Condition cc = TokenToCondition(instr->op(), is_unsigned);
2311 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2312 // We can statically evaluate the comparison.
2313 double left_val = ToDouble(LConstantOperand::cast(left));
2314 double right_val = ToDouble(LConstantOperand::cast(right));
2315 int next_block = EvalComparison(instr->op(), left_val, right_val) ?
2316 instr->TrueDestination(chunk_) : instr->FalseDestination(chunk_);
2317 EmitGoto(next_block);
2319 if (instr->is_double()) {
2320 // Don't base result on EFLAGS when a NaN is involved. Instead
2321 // jump to the false block.
2322 __ ucomisd(ToDoubleRegister(left), ToDoubleRegister(right));
2323 __ j(parity_even, instr->FalseLabel(chunk_));
2326 if (right->IsConstantOperand()) {
2327 value = ToInteger32(LConstantOperand::cast(right));
2328 if (instr->hydrogen_value()->representation().IsSmi()) {
2329 __ Cmp(ToRegister(left), Smi::FromInt(value));
2331 __ cmpl(ToRegister(left), Immediate(value));
2333 } else if (left->IsConstantOperand()) {
2334 value = ToInteger32(LConstantOperand::cast(left));
2335 if (instr->hydrogen_value()->representation().IsSmi()) {
2336 if (right->IsRegister()) {
2337 __ Cmp(ToRegister(right), Smi::FromInt(value));
2339 __ Cmp(ToOperand(right), Smi::FromInt(value));
2341 } else if (right->IsRegister()) {
2342 __ cmpl(ToRegister(right), Immediate(value));
2344 __ cmpl(ToOperand(right), Immediate(value));
2346 // We commuted the operands, so commute the condition.
2347 cc = CommuteCondition(cc);
2348 } else if (instr->hydrogen_value()->representation().IsSmi()) {
2349 if (right->IsRegister()) {
2350 __ cmpp(ToRegister(left), ToRegister(right));
2352 __ cmpp(ToRegister(left), ToOperand(right));
2355 if (right->IsRegister()) {
2356 __ cmpl(ToRegister(left), ToRegister(right));
2358 __ cmpl(ToRegister(left), ToOperand(right));
2362 EmitBranch(instr, cc);
2367 void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
2368 Register left = ToRegister(instr->left());
2370 if (instr->right()->IsConstantOperand()) {
2371 Handle<Object> right = ToHandle(LConstantOperand::cast(instr->right()));
2372 __ Cmp(left, right);
2374 Register right = ToRegister(instr->right());
2375 __ cmpp(left, right);
2377 EmitBranch(instr, equal);
2381 void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) {
2382 if (instr->hydrogen()->representation().IsTagged()) {
2383 Register input_reg = ToRegister(instr->object());
2384 __ Cmp(input_reg, factory()->the_hole_value());
2385 EmitBranch(instr, equal);
2389 XMMRegister input_reg = ToDoubleRegister(instr->object());
2390 __ ucomisd(input_reg, input_reg);
2391 EmitFalseBranch(instr, parity_odd);
2393 __ subp(rsp, Immediate(kDoubleSize));
2394 __ movsd(MemOperand(rsp, 0), input_reg);
2395 __ addp(rsp, Immediate(kDoubleSize));
2397 int offset = sizeof(kHoleNanUpper32);
2398 __ cmpl(MemOperand(rsp, -offset), Immediate(kHoleNanUpper32));
2399 EmitBranch(instr, equal);
2403 void LCodeGen::DoCompareMinusZeroAndBranch(LCompareMinusZeroAndBranch* instr) {
2404 Representation rep = instr->hydrogen()->value()->representation();
2405 DCHECK(!rep.IsInteger32());
2407 if (rep.IsDouble()) {
2408 XMMRegister value = ToDoubleRegister(instr->value());
2409 XMMRegister xmm_scratch = double_scratch0();
2410 __ xorps(xmm_scratch, xmm_scratch);
2411 __ ucomisd(xmm_scratch, value);
2412 EmitFalseBranch(instr, not_equal);
2413 __ movmskpd(kScratchRegister, value);
2414 __ testl(kScratchRegister, Immediate(1));
2415 EmitBranch(instr, not_zero);
2417 Register value = ToRegister(instr->value());
2418 Handle<Map> map = masm()->isolate()->factory()->heap_number_map();
2419 __ CheckMap(value, map, instr->FalseLabel(chunk()), DO_SMI_CHECK);
2420 __ cmpl(FieldOperand(value, HeapNumber::kExponentOffset),
2422 EmitFalseBranch(instr, no_overflow);
2423 __ cmpl(FieldOperand(value, HeapNumber::kMantissaOffset),
2424 Immediate(0x00000000));
2425 EmitBranch(instr, equal);
2430 Condition LCodeGen::EmitIsObject(Register input,
2431 Label* is_not_object,
2433 DCHECK(!input.is(kScratchRegister));
2435 __ JumpIfSmi(input, is_not_object);
2437 __ CompareRoot(input, Heap::kNullValueRootIndex);
2438 __ j(equal, is_object);
2440 __ movp(kScratchRegister, FieldOperand(input, HeapObject::kMapOffset));
2441 // Undetectable objects behave like undefined.
2442 __ testb(FieldOperand(kScratchRegister, Map::kBitFieldOffset),
2443 Immediate(1 << Map::kIsUndetectable));
2444 __ j(not_zero, is_not_object);
2446 __ movzxbl(kScratchRegister,
2447 FieldOperand(kScratchRegister, Map::kInstanceTypeOffset));
2448 __ cmpb(kScratchRegister, Immediate(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2449 __ j(below, is_not_object);
2450 __ cmpb(kScratchRegister, Immediate(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
2455 void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) {
2456 Register reg = ToRegister(instr->value());
2458 Condition true_cond = EmitIsObject(
2459 reg, instr->FalseLabel(chunk_), instr->TrueLabel(chunk_));
2461 EmitBranch(instr, true_cond);
2465 Condition LCodeGen::EmitIsString(Register input,
2467 Label* is_not_string,
2468 SmiCheck check_needed = INLINE_SMI_CHECK) {
2469 if (check_needed == INLINE_SMI_CHECK) {
2470 __ JumpIfSmi(input, is_not_string);
2473 Condition cond = masm_->IsObjectStringType(input, temp1, temp1);
2479 void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
2480 Register reg = ToRegister(instr->value());
2481 Register temp = ToRegister(instr->temp());
2483 SmiCheck check_needed =
2484 instr->hydrogen()->value()->type().IsHeapObject()
2485 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2487 Condition true_cond = EmitIsString(
2488 reg, temp, instr->FalseLabel(chunk_), check_needed);
2490 EmitBranch(instr, true_cond);
2494 void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
2496 if (instr->value()->IsRegister()) {
2497 Register input = ToRegister(instr->value());
2498 is_smi = masm()->CheckSmi(input);
2500 Operand input = ToOperand(instr->value());
2501 is_smi = masm()->CheckSmi(input);
2503 EmitBranch(instr, is_smi);
2507 void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
2508 Register input = ToRegister(instr->value());
2509 Register temp = ToRegister(instr->temp());
2511 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2512 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2514 __ movp(temp, FieldOperand(input, HeapObject::kMapOffset));
2515 __ testb(FieldOperand(temp, Map::kBitFieldOffset),
2516 Immediate(1 << Map::kIsUndetectable));
2517 EmitBranch(instr, not_zero);
2521 void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
2522 DCHECK(ToRegister(instr->context()).is(rsi));
2523 Token::Value op = instr->op();
2526 CodeFactory::CompareIC(isolate(), op, Strength::WEAK).code();
2527 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2529 Condition condition = TokenToCondition(op, false);
2532 EmitBranch(instr, condition);
2536 static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
2537 InstanceType from = instr->from();
2538 InstanceType to = instr->to();
2539 if (from == FIRST_TYPE) return to;
2540 DCHECK(from == to || to == LAST_TYPE);
2545 static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
2546 InstanceType from = instr->from();
2547 InstanceType to = instr->to();
2548 if (from == to) return equal;
2549 if (to == LAST_TYPE) return above_equal;
2550 if (from == FIRST_TYPE) return below_equal;
2556 void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
2557 Register input = ToRegister(instr->value());
2559 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2560 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2563 __ CmpObjectType(input, TestType(instr->hydrogen()), kScratchRegister);
2564 EmitBranch(instr, BranchCondition(instr->hydrogen()));
2568 void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
2569 Register input = ToRegister(instr->value());
2570 Register result = ToRegister(instr->result());
2572 __ AssertString(input);
2574 __ movl(result, FieldOperand(input, String::kHashFieldOffset));
2575 DCHECK(String::kHashShift >= kSmiTagSize);
2576 __ IndexFromHash(result, result);
2580 void LCodeGen::DoHasCachedArrayIndexAndBranch(
2581 LHasCachedArrayIndexAndBranch* instr) {
2582 Register input = ToRegister(instr->value());
2584 __ testl(FieldOperand(input, String::kHashFieldOffset),
2585 Immediate(String::kContainsCachedArrayIndexMask));
2586 EmitBranch(instr, equal);
2590 // Branches to a label or falls through with the answer in the z flag.
2591 // Trashes the temp register.
2592 void LCodeGen::EmitClassOfTest(Label* is_true,
2594 Handle<String> class_name,
2598 DCHECK(!input.is(temp));
2599 DCHECK(!input.is(temp2));
2600 DCHECK(!temp.is(temp2));
2602 __ JumpIfSmi(input, is_false);
2604 if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
2605 // Assuming the following assertions, we can use the same compares to test
2606 // for both being a function type and being in the object type range.
2607 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
2608 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2609 FIRST_SPEC_OBJECT_TYPE + 1);
2610 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2611 LAST_SPEC_OBJECT_TYPE - 1);
2612 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
2613 __ CmpObjectType(input, FIRST_SPEC_OBJECT_TYPE, temp);
2614 __ j(below, is_false);
2615 __ j(equal, is_true);
2616 __ CmpInstanceType(temp, LAST_SPEC_OBJECT_TYPE);
2617 __ j(equal, is_true);
2619 // Faster code path to avoid two compares: subtract lower bound from the
2620 // actual type and do a signed compare with the width of the type range.
2621 __ movp(temp, FieldOperand(input, HeapObject::kMapOffset));
2622 __ movzxbl(temp2, FieldOperand(temp, Map::kInstanceTypeOffset));
2623 __ subp(temp2, Immediate(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2624 __ cmpp(temp2, Immediate(LAST_NONCALLABLE_SPEC_OBJECT_TYPE -
2625 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2626 __ j(above, is_false);
2629 // Now we are in the FIRST-LAST_NONCALLABLE_SPEC_OBJECT_TYPE range.
2630 // Check if the constructor in the map is a function.
2631 __ GetMapConstructor(temp, temp, kScratchRegister);
2633 // Objects with a non-function constructor have class 'Object'.
2634 __ CmpInstanceType(kScratchRegister, JS_FUNCTION_TYPE);
2635 if (String::Equals(class_name, isolate()->factory()->Object_string())) {
2636 __ j(not_equal, is_true);
2638 __ j(not_equal, is_false);
2641 // temp now contains the constructor function. Grab the
2642 // instance class name from there.
2643 __ movp(temp, FieldOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2644 __ movp(temp, FieldOperand(temp,
2645 SharedFunctionInfo::kInstanceClassNameOffset));
2646 // The class name we are testing against is internalized since it's a literal.
2647 // The name in the constructor is internalized because of the way the context
2648 // is booted. This routine isn't expected to work for random API-created
2649 // classes and it doesn't have to because you can't access it with natives
2650 // syntax. Since both sides are internalized it is sufficient to use an
2651 // identity comparison.
2652 DCHECK(class_name->IsInternalizedString());
2653 __ Cmp(temp, class_name);
2654 // End with the answer in the z flag.
2658 void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
2659 Register input = ToRegister(instr->value());
2660 Register temp = ToRegister(instr->temp());
2661 Register temp2 = ToRegister(instr->temp2());
2662 Handle<String> class_name = instr->hydrogen()->class_name();
2664 EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2665 class_name, input, temp, temp2);
2667 EmitBranch(instr, equal);
2671 void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
2672 Register reg = ToRegister(instr->value());
2674 __ Cmp(FieldOperand(reg, HeapObject::kMapOffset), instr->map());
2675 EmitBranch(instr, equal);
2679 void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
2680 DCHECK(ToRegister(instr->context()).is(rsi));
2681 DCHECK(ToRegister(instr->left()).is(InstanceOfDescriptor::LeftRegister()));
2682 DCHECK(ToRegister(instr->right()).is(InstanceOfDescriptor::RightRegister()));
2683 DCHECK(ToRegister(instr->result()).is(rax));
2684 InstanceOfStub stub(isolate());
2685 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2689 void LCodeGen::DoHasInPrototypeChainAndBranch(
2690 LHasInPrototypeChainAndBranch* instr) {
2691 Register const object = ToRegister(instr->object());
2692 Register const object_map = kScratchRegister;
2693 Register const object_prototype = object_map;
2694 Register const prototype = ToRegister(instr->prototype());
2696 // The {object} must be a spec object. It's sufficient to know that {object}
2697 // is not a smi, since all other non-spec objects have {null} prototypes and
2698 // will be ruled out below.
2699 if (instr->hydrogen()->ObjectNeedsSmiCheck()) {
2700 Condition is_smi = __ CheckSmi(object);
2701 EmitFalseBranch(instr, is_smi);
2704 // Loop through the {object}s prototype chain looking for the {prototype}.
2705 __ movp(object_map, FieldOperand(object, HeapObject::kMapOffset));
2708 __ movp(object_prototype, FieldOperand(object_map, Map::kPrototypeOffset));
2709 __ cmpp(object_prototype, prototype);
2710 EmitTrueBranch(instr, equal);
2711 __ CompareRoot(object_prototype, Heap::kNullValueRootIndex);
2712 EmitFalseBranch(instr, equal);
2713 __ movp(object_map, FieldOperand(object_prototype, HeapObject::kMapOffset));
2718 void LCodeGen::DoCmpT(LCmpT* instr) {
2719 DCHECK(ToRegister(instr->context()).is(rsi));
2720 Token::Value op = instr->op();
2723 CodeFactory::CompareIC(isolate(), op, instr->strength()).code();
2724 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2726 Condition condition = TokenToCondition(op, false);
2727 Label true_value, done;
2729 __ j(condition, &true_value, Label::kNear);
2730 __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex);
2731 __ jmp(&done, Label::kNear);
2732 __ bind(&true_value);
2733 __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex);
2738 void LCodeGen::DoReturn(LReturn* instr) {
2739 if (FLAG_trace && info()->IsOptimizing()) {
2740 // Preserve the return value on the stack and rely on the runtime call
2741 // to return the value in the same register. We're leaving the code
2742 // managed by the register allocator and tearing down the frame, it's
2743 // safe to write to the context register.
2745 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
2746 __ CallRuntime(Runtime::kTraceExit, 1);
2748 if (info()->saves_caller_doubles()) {
2749 RestoreCallerDoubles();
2751 int no_frame_start = -1;
2752 if (NeedsEagerFrame()) {
2755 no_frame_start = masm_->pc_offset();
2757 if (instr->has_constant_parameter_count()) {
2758 __ Ret((ToInteger32(instr->constant_parameter_count()) + 1) * kPointerSize,
2761 DCHECK(info()->IsStub()); // Functions would need to drop one more value.
2762 Register reg = ToRegister(instr->parameter_count());
2763 // The argument count parameter is a smi
2764 __ SmiToInteger32(reg, reg);
2765 Register return_addr_reg = reg.is(rcx) ? rbx : rcx;
2766 __ PopReturnAddressTo(return_addr_reg);
2767 __ shlp(reg, Immediate(kPointerSizeLog2));
2769 __ jmp(return_addr_reg);
2771 if (no_frame_start != -1) {
2772 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
2778 void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
2779 Register vector_register = ToRegister(instr->temp_vector());
2780 Register slot_register = LoadWithVectorDescriptor::SlotRegister();
2781 DCHECK(vector_register.is(LoadWithVectorDescriptor::VectorRegister()));
2782 DCHECK(slot_register.is(rax));
2784 AllowDeferredHandleDereference vector_structure_check;
2785 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2786 __ Move(vector_register, vector);
2787 // No need to allocate this register.
2788 FeedbackVectorICSlot slot = instr->hydrogen()->slot();
2789 int index = vector->GetIndex(slot);
2790 __ Move(slot_register, Smi::FromInt(index));
2795 void LCodeGen::EmitVectorStoreICRegisters(T* instr) {
2796 Register vector_register = ToRegister(instr->temp_vector());
2797 Register slot_register = ToRegister(instr->temp_slot());
2799 AllowDeferredHandleDereference vector_structure_check;
2800 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2801 __ Move(vector_register, vector);
2802 FeedbackVectorICSlot slot = instr->hydrogen()->slot();
2803 int index = vector->GetIndex(slot);
2804 __ Move(slot_register, Smi::FromInt(index));
2808 void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
2809 DCHECK(ToRegister(instr->context()).is(rsi));
2810 DCHECK(ToRegister(instr->global_object())
2811 .is(LoadDescriptor::ReceiverRegister()));
2812 DCHECK(ToRegister(instr->result()).is(rax));
2814 __ Move(LoadDescriptor::NameRegister(), instr->name());
2815 EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
2817 CodeFactory::LoadICInOptimizedCode(isolate(), instr->typeof_mode(),
2818 SLOPPY, PREMONOMORPHIC).code();
2819 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2823 void LCodeGen::DoLoadGlobalViaContext(LLoadGlobalViaContext* instr) {
2824 DCHECK(ToRegister(instr->context()).is(rsi));
2825 DCHECK(ToRegister(instr->result()).is(rax));
2826 int const slot = instr->slot_index();
2827 int const depth = instr->depth();
2828 if (depth <= LoadGlobalViaContextStub::kMaximumDepth) {
2829 __ Set(LoadGlobalViaContextDescriptor::SlotRegister(), slot);
2831 CodeFactory::LoadGlobalViaContext(isolate(), depth).code();
2832 CallCode(stub, RelocInfo::CODE_TARGET, instr);
2834 __ Push(Smi::FromInt(slot));
2835 __ CallRuntime(Runtime::kLoadGlobalViaContext, 1);
2840 void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
2841 Register context = ToRegister(instr->context());
2842 Register result = ToRegister(instr->result());
2843 __ movp(result, ContextOperand(context, instr->slot_index()));
2844 if (instr->hydrogen()->RequiresHoleCheck()) {
2845 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
2846 if (instr->hydrogen()->DeoptimizesOnHole()) {
2847 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
2850 __ j(not_equal, &is_not_hole, Label::kNear);
2851 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2852 __ bind(&is_not_hole);
2858 void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
2859 Register context = ToRegister(instr->context());
2860 Register value = ToRegister(instr->value());
2862 Operand target = ContextOperand(context, instr->slot_index());
2864 Label skip_assignment;
2865 if (instr->hydrogen()->RequiresHoleCheck()) {
2866 __ CompareRoot(target, Heap::kTheHoleValueRootIndex);
2867 if (instr->hydrogen()->DeoptimizesOnHole()) {
2868 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
2870 __ j(not_equal, &skip_assignment);
2873 __ movp(target, value);
2875 if (instr->hydrogen()->NeedsWriteBarrier()) {
2876 SmiCheck check_needed =
2877 instr->hydrogen()->value()->type().IsHeapObject()
2878 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2879 int offset = Context::SlotOffset(instr->slot_index());
2880 Register scratch = ToRegister(instr->temp());
2881 __ RecordWriteContextSlot(context,
2886 EMIT_REMEMBERED_SET,
2890 __ bind(&skip_assignment);
2894 void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
2895 HObjectAccess access = instr->hydrogen()->access();
2896 int offset = access.offset();
2898 if (access.IsExternalMemory()) {
2899 Register result = ToRegister(instr->result());
2900 if (instr->object()->IsConstantOperand()) {
2901 DCHECK(result.is(rax));
2902 __ load_rax(ToExternalReference(LConstantOperand::cast(instr->object())));
2904 Register object = ToRegister(instr->object());
2905 __ Load(result, MemOperand(object, offset), access.representation());
2910 Register object = ToRegister(instr->object());
2911 if (instr->hydrogen()->representation().IsDouble()) {
2912 DCHECK(access.IsInobject());
2913 XMMRegister result = ToDoubleRegister(instr->result());
2914 __ movsd(result, FieldOperand(object, offset));
2918 Register result = ToRegister(instr->result());
2919 if (!access.IsInobject()) {
2920 __ movp(result, FieldOperand(object, JSObject::kPropertiesOffset));
2924 Representation representation = access.representation();
2925 if (representation.IsSmi() && SmiValuesAre32Bits() &&
2926 instr->hydrogen()->representation().IsInteger32()) {
2927 if (FLAG_debug_code) {
2928 Register scratch = kScratchRegister;
2929 __ Load(scratch, FieldOperand(object, offset), representation);
2930 __ AssertSmi(scratch);
2933 // Read int value directly from upper half of the smi.
2934 STATIC_ASSERT(kSmiTag == 0);
2935 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
2936 offset += kPointerSize / 2;
2937 representation = Representation::Integer32();
2939 __ Load(result, FieldOperand(object, offset), representation);
2943 void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
2944 DCHECK(ToRegister(instr->context()).is(rsi));
2945 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
2946 DCHECK(ToRegister(instr->result()).is(rax));
2948 __ Move(LoadDescriptor::NameRegister(), instr->name());
2949 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
2951 CodeFactory::LoadICInOptimizedCode(
2952 isolate(), NOT_INSIDE_TYPEOF, instr->hydrogen()->language_mode(),
2953 instr->hydrogen()->initialization_state()).code();
2954 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2958 void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
2959 Register function = ToRegister(instr->function());
2960 Register result = ToRegister(instr->result());
2962 // Get the prototype or initial map from the function.
2964 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2966 // Check that the function has a prototype or an initial map.
2967 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
2968 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
2970 // If the function does not have an initial map, we're done.
2972 __ CmpObjectType(result, MAP_TYPE, kScratchRegister);
2973 __ j(not_equal, &done, Label::kNear);
2975 // Get the prototype from the initial map.
2976 __ movp(result, FieldOperand(result, Map::kPrototypeOffset));
2983 void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
2984 Register result = ToRegister(instr->result());
2985 __ LoadRoot(result, instr->index());
2989 void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
2990 Register arguments = ToRegister(instr->arguments());
2991 Register result = ToRegister(instr->result());
2993 if (instr->length()->IsConstantOperand() &&
2994 instr->index()->IsConstantOperand()) {
2995 int32_t const_index = ToInteger32(LConstantOperand::cast(instr->index()));
2996 int32_t const_length = ToInteger32(LConstantOperand::cast(instr->length()));
2997 if (const_index >= 0 && const_index < const_length) {
2998 StackArgumentsAccessor args(arguments, const_length,
2999 ARGUMENTS_DONT_CONTAIN_RECEIVER);
3000 __ movp(result, args.GetArgumentOperand(const_index));
3001 } else if (FLAG_debug_code) {
3005 Register length = ToRegister(instr->length());
3006 // There are two words between the frame pointer and the last argument.
3007 // Subtracting from length accounts for one of them add one more.
3008 if (instr->index()->IsRegister()) {
3009 __ subl(length, ToRegister(instr->index()));
3011 __ subl(length, ToOperand(instr->index()));
3013 StackArgumentsAccessor args(arguments, length,
3014 ARGUMENTS_DONT_CONTAIN_RECEIVER);
3015 __ movp(result, args.GetArgumentOperand(0));
3020 void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
3021 ElementsKind elements_kind = instr->elements_kind();
3022 LOperand* key = instr->key();
3023 if (kPointerSize == kInt32Size && !key->IsConstantOperand()) {
3024 Register key_reg = ToRegister(key);
3025 Representation key_representation =
3026 instr->hydrogen()->key()->representation();
3027 if (ExternalArrayOpRequiresTemp(key_representation, elements_kind)) {
3028 __ SmiToInteger64(key_reg, key_reg);
3029 } else if (instr->hydrogen()->IsDehoisted()) {
3030 // Sign extend key because it could be a 32 bit negative value
3031 // and the dehoisted address computation happens in 64 bits
3032 __ movsxlq(key_reg, key_reg);
3035 Operand operand(BuildFastArrayOperand(
3038 instr->hydrogen()->key()->representation(),
3040 instr->base_offset()));
3042 if (elements_kind == FLOAT32_ELEMENTS) {
3043 XMMRegister result(ToDoubleRegister(instr->result()));
3044 __ movss(result, operand);
3045 __ cvtss2sd(result, result);
3046 } else if (elements_kind == FLOAT64_ELEMENTS) {
3047 __ movsd(ToDoubleRegister(instr->result()), operand);
3049 Register result(ToRegister(instr->result()));
3050 switch (elements_kind) {
3052 __ movsxbl(result, operand);
3054 case UINT8_ELEMENTS:
3055 case UINT8_CLAMPED_ELEMENTS:
3056 __ movzxbl(result, operand);
3058 case INT16_ELEMENTS:
3059 __ movsxwl(result, operand);
3061 case UINT16_ELEMENTS:
3062 __ movzxwl(result, operand);
3064 case INT32_ELEMENTS:
3065 __ movl(result, operand);
3067 case UINT32_ELEMENTS:
3068 __ movl(result, operand);
3069 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
3070 __ testl(result, result);
3071 DeoptimizeIf(negative, instr, Deoptimizer::kNegativeValue);
3074 case FLOAT32_ELEMENTS:
3075 case FLOAT64_ELEMENTS:
3077 case FAST_SMI_ELEMENTS:
3078 case FAST_DOUBLE_ELEMENTS:
3079 case FAST_HOLEY_ELEMENTS:
3080 case FAST_HOLEY_SMI_ELEMENTS:
3081 case FAST_HOLEY_DOUBLE_ELEMENTS:
3082 case DICTIONARY_ELEMENTS:
3083 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
3084 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
3092 void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
3093 XMMRegister result(ToDoubleRegister(instr->result()));
3094 LOperand* key = instr->key();
3095 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
3096 instr->hydrogen()->IsDehoisted()) {
3097 // Sign extend key because it could be a 32 bit negative value
3098 // and the dehoisted address computation happens in 64 bits
3099 __ movsxlq(ToRegister(key), ToRegister(key));
3101 if (instr->hydrogen()->RequiresHoleCheck()) {
3102 Operand hole_check_operand = BuildFastArrayOperand(
3105 instr->hydrogen()->key()->representation(),
3106 FAST_DOUBLE_ELEMENTS,
3107 instr->base_offset() + sizeof(kHoleNanLower32));
3108 __ cmpl(hole_check_operand, Immediate(kHoleNanUpper32));
3109 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
3112 Operand double_load_operand = BuildFastArrayOperand(
3115 instr->hydrogen()->key()->representation(),
3116 FAST_DOUBLE_ELEMENTS,
3117 instr->base_offset());
3118 __ movsd(result, double_load_operand);
3122 void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
3123 HLoadKeyed* hinstr = instr->hydrogen();
3124 Register result = ToRegister(instr->result());
3125 LOperand* key = instr->key();
3126 bool requires_hole_check = hinstr->RequiresHoleCheck();
3127 Representation representation = hinstr->representation();
3128 int offset = instr->base_offset();
3130 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
3131 instr->hydrogen()->IsDehoisted()) {
3132 // Sign extend key because it could be a 32 bit negative value
3133 // and the dehoisted address computation happens in 64 bits
3134 __ movsxlq(ToRegister(key), ToRegister(key));
3136 if (representation.IsInteger32() && SmiValuesAre32Bits() &&
3137 hinstr->elements_kind() == FAST_SMI_ELEMENTS) {
3138 DCHECK(!requires_hole_check);
3139 if (FLAG_debug_code) {
3140 Register scratch = kScratchRegister;
3142 BuildFastArrayOperand(instr->elements(),
3144 instr->hydrogen()->key()->representation(),
3147 Representation::Smi());
3148 __ AssertSmi(scratch);
3150 // Read int value directly from upper half of the smi.
3151 STATIC_ASSERT(kSmiTag == 0);
3152 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
3153 offset += kPointerSize / 2;
3157 BuildFastArrayOperand(instr->elements(), key,
3158 instr->hydrogen()->key()->representation(),
3159 FAST_ELEMENTS, offset),
3162 // Check for the hole value.
3163 if (requires_hole_check) {
3164 if (IsFastSmiElementsKind(hinstr->elements_kind())) {
3165 Condition smi = __ CheckSmi(result);
3166 DeoptimizeIf(NegateCondition(smi), instr, Deoptimizer::kNotASmi);
3168 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
3169 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
3171 } else if (hinstr->hole_mode() == CONVERT_HOLE_TO_UNDEFINED) {
3172 DCHECK(hinstr->elements_kind() == FAST_HOLEY_ELEMENTS);
3174 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
3175 __ j(not_equal, &done);
3176 if (info()->IsStub()) {
3177 // A stub can safely convert the hole to undefined only if the array
3178 // protector cell contains (Smi) Isolate::kArrayProtectorValid. Otherwise
3179 // it needs to bail out.
3180 __ LoadRoot(result, Heap::kArrayProtectorRootIndex);
3181 __ Cmp(FieldOperand(result, Cell::kValueOffset),
3182 Smi::FromInt(Isolate::kArrayProtectorValid));
3183 DeoptimizeIf(not_equal, instr, Deoptimizer::kHole);
3185 __ Move(result, isolate()->factory()->undefined_value());
3191 void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
3192 if (instr->is_fixed_typed_array()) {
3193 DoLoadKeyedExternalArray(instr);
3194 } else if (instr->hydrogen()->representation().IsDouble()) {
3195 DoLoadKeyedFixedDoubleArray(instr);
3197 DoLoadKeyedFixedArray(instr);
3202 Operand LCodeGen::BuildFastArrayOperand(
3203 LOperand* elements_pointer,
3205 Representation key_representation,
3206 ElementsKind elements_kind,
3208 Register elements_pointer_reg = ToRegister(elements_pointer);
3209 int shift_size = ElementsKindToShiftSize(elements_kind);
3210 if (key->IsConstantOperand()) {
3211 int32_t constant_value = ToInteger32(LConstantOperand::cast(key));
3212 if (constant_value & 0xF0000000) {
3213 Abort(kArrayIndexConstantValueTooBig);
3215 return Operand(elements_pointer_reg,
3216 (constant_value << shift_size) + offset);
3218 // Guaranteed by ArrayInstructionInterface::KeyedAccessIndexRequirement().
3219 DCHECK(key_representation.IsInteger32());
3221 ScaleFactor scale_factor = static_cast<ScaleFactor>(shift_size);
3222 return Operand(elements_pointer_reg,
3230 void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3231 DCHECK(ToRegister(instr->context()).is(rsi));
3232 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3233 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
3235 if (instr->hydrogen()->HasVectorAndSlot()) {
3236 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
3239 Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(
3240 isolate(), instr->hydrogen()->language_mode(),
3241 instr->hydrogen()->initialization_state()).code();
3242 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3246 void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
3247 Register result = ToRegister(instr->result());
3249 if (instr->hydrogen()->from_inlined()) {
3250 __ leap(result, Operand(rsp, -kFPOnStackSize + -kPCOnStackSize));
3252 // Check for arguments adapter frame.
3253 Label done, adapted;
3254 __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
3255 __ Cmp(Operand(result, StandardFrameConstants::kContextOffset),
3256 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
3257 __ j(equal, &adapted, Label::kNear);
3259 // No arguments adaptor frame.
3260 __ movp(result, rbp);
3261 __ jmp(&done, Label::kNear);
3263 // Arguments adaptor frame present.
3265 __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
3267 // Result is the frame pointer for the frame if not adapted and for the real
3268 // frame below the adaptor frame if adapted.
3274 void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
3275 Register result = ToRegister(instr->result());
3279 // If no arguments adaptor frame the number of arguments is fixed.
3280 if (instr->elements()->IsRegister()) {
3281 __ cmpp(rbp, ToRegister(instr->elements()));
3283 __ cmpp(rbp, ToOperand(instr->elements()));
3285 __ movl(result, Immediate(scope()->num_parameters()));
3286 __ j(equal, &done, Label::kNear);
3288 // Arguments adaptor frame present. Get argument length from there.
3289 __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
3290 __ SmiToInteger32(result,
3292 ArgumentsAdaptorFrameConstants::kLengthOffset));
3294 // Argument length is in result register.
3299 void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
3300 Register receiver = ToRegister(instr->receiver());
3301 Register function = ToRegister(instr->function());
3303 // If the receiver is null or undefined, we have to pass the global
3304 // object as a receiver to normal functions. Values have to be
3305 // passed unchanged to builtins and strict-mode functions.
3306 Label global_object, receiver_ok;
3307 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
3309 if (!instr->hydrogen()->known_function()) {
3310 // Do not transform the receiver to object for strict mode
3312 __ movp(kScratchRegister,
3313 FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
3314 __ testb(FieldOperand(kScratchRegister,
3315 SharedFunctionInfo::kStrictModeByteOffset),
3316 Immediate(1 << SharedFunctionInfo::kStrictModeBitWithinByte));
3317 __ j(not_equal, &receiver_ok, dist);
3319 // Do not transform the receiver to object for builtins.
3320 __ testb(FieldOperand(kScratchRegister,
3321 SharedFunctionInfo::kNativeByteOffset),
3322 Immediate(1 << SharedFunctionInfo::kNativeBitWithinByte));
3323 __ j(not_equal, &receiver_ok, dist);
3326 // Normal function. Replace undefined or null with global receiver.
3327 __ CompareRoot(receiver, Heap::kNullValueRootIndex);
3328 __ j(equal, &global_object, Label::kNear);
3329 __ CompareRoot(receiver, Heap::kUndefinedValueRootIndex);
3330 __ j(equal, &global_object, Label::kNear);
3332 // The receiver should be a JS object.
3333 Condition is_smi = __ CheckSmi(receiver);
3334 DeoptimizeIf(is_smi, instr, Deoptimizer::kSmi);
3335 __ CmpObjectType(receiver, FIRST_SPEC_OBJECT_TYPE, kScratchRegister);
3336 DeoptimizeIf(below, instr, Deoptimizer::kNotAJavaScriptObject);
3338 __ jmp(&receiver_ok, Label::kNear);
3339 __ bind(&global_object);
3340 __ movp(receiver, FieldOperand(function, JSFunction::kContextOffset));
3343 Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
3344 __ movp(receiver, FieldOperand(receiver, GlobalObject::kGlobalProxyOffset));
3346 __ bind(&receiver_ok);
3350 void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3351 Register receiver = ToRegister(instr->receiver());
3352 Register function = ToRegister(instr->function());
3353 Register length = ToRegister(instr->length());
3354 Register elements = ToRegister(instr->elements());
3355 DCHECK(receiver.is(rax)); // Used for parameter count.
3356 DCHECK(function.is(rdi)); // Required by InvokeFunction.
3357 DCHECK(ToRegister(instr->result()).is(rax));
3359 // Copy the arguments to this function possibly from the
3360 // adaptor frame below it.
3361 const uint32_t kArgumentsLimit = 1 * KB;
3362 __ cmpp(length, Immediate(kArgumentsLimit));
3363 DeoptimizeIf(above, instr, Deoptimizer::kTooManyArguments);
3366 __ movp(receiver, length);
3368 // Loop through the arguments pushing them onto the execution
3371 // length is a small non-negative integer, due to the test above.
3372 __ testl(length, length);
3373 __ j(zero, &invoke, Label::kNear);
3375 StackArgumentsAccessor args(elements, length,
3376 ARGUMENTS_DONT_CONTAIN_RECEIVER);
3377 __ Push(args.GetArgumentOperand(0));
3379 __ j(not_zero, &loop);
3381 // Invoke the function.
3383 DCHECK(instr->HasPointerMap());
3384 LPointerMap* pointers = instr->pointer_map();
3385 SafepointGenerator safepoint_generator(
3386 this, pointers, Safepoint::kLazyDeopt);
3387 ParameterCount actual(rax);
3388 __ InvokeFunction(function, actual, CALL_FUNCTION, safepoint_generator);
3392 void LCodeGen::DoPushArgument(LPushArgument* instr) {
3393 LOperand* argument = instr->value();
3394 EmitPushTaggedOperand(argument);
3398 void LCodeGen::DoDrop(LDrop* instr) {
3399 __ Drop(instr->count());
3403 void LCodeGen::DoThisFunction(LThisFunction* instr) {
3404 Register result = ToRegister(instr->result());
3405 __ movp(result, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
3409 void LCodeGen::DoContext(LContext* instr) {
3410 Register result = ToRegister(instr->result());
3411 if (info()->IsOptimizing()) {
3412 __ movp(result, Operand(rbp, StandardFrameConstants::kContextOffset));
3414 // If there is no frame, the context must be in rsi.
3415 DCHECK(result.is(rsi));
3420 void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
3421 DCHECK(ToRegister(instr->context()).is(rsi));
3422 __ Push(instr->hydrogen()->pairs());
3423 __ Push(Smi::FromInt(instr->hydrogen()->flags()));
3424 CallRuntime(Runtime::kDeclareGlobals, 2, instr);
3428 void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
3429 int formal_parameter_count, int arity,
3430 LInstruction* instr) {
3431 bool dont_adapt_arguments =
3432 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3433 bool can_invoke_directly =
3434 dont_adapt_arguments || formal_parameter_count == arity;
3436 Register function_reg = rdi;
3437 LPointerMap* pointers = instr->pointer_map();
3439 if (can_invoke_directly) {
3441 __ movp(rsi, FieldOperand(function_reg, JSFunction::kContextOffset));
3443 // Set rax to arguments count if adaption is not needed. Assumes that rax
3444 // is available to write to at this point.
3445 if (dont_adapt_arguments) {
3450 if (function.is_identical_to(info()->closure())) {
3453 __ Call(FieldOperand(function_reg, JSFunction::kCodeEntryOffset));
3456 // Set up deoptimization.
3457 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT, 0);
3459 // We need to adapt arguments.
3460 SafepointGenerator generator(
3461 this, pointers, Safepoint::kLazyDeopt);
3462 ParameterCount count(arity);
3463 ParameterCount expected(formal_parameter_count);
3464 __ InvokeFunction(function_reg, expected, count, CALL_FUNCTION, generator);
3469 void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
3470 DCHECK(ToRegister(instr->result()).is(rax));
3472 if (instr->hydrogen()->IsTailCall()) {
3473 if (NeedsEagerFrame()) __ leave();
3475 if (instr->target()->IsConstantOperand()) {
3476 LConstantOperand* target = LConstantOperand::cast(instr->target());
3477 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3478 __ jmp(code, RelocInfo::CODE_TARGET);
3480 DCHECK(instr->target()->IsRegister());
3481 Register target = ToRegister(instr->target());
3482 __ addp(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
3486 LPointerMap* pointers = instr->pointer_map();
3487 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3489 if (instr->target()->IsConstantOperand()) {
3490 LConstantOperand* target = LConstantOperand::cast(instr->target());
3491 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3492 generator.BeforeCall(__ CallSize(code));
3493 __ call(code, RelocInfo::CODE_TARGET);
3495 DCHECK(instr->target()->IsRegister());
3496 Register target = ToRegister(instr->target());
3497 generator.BeforeCall(__ CallSize(target));
3498 __ addp(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
3501 generator.AfterCall();
3506 void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
3507 DCHECK(ToRegister(instr->function()).is(rdi));
3508 DCHECK(ToRegister(instr->result()).is(rax));
3510 if (instr->hydrogen()->pass_argument_count()) {
3511 __ Set(rax, instr->arity());
3515 __ movp(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
3517 LPointerMap* pointers = instr->pointer_map();
3518 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3520 bool is_self_call = false;
3521 if (instr->hydrogen()->function()->IsConstant()) {
3522 Handle<JSFunction> jsfun = Handle<JSFunction>::null();
3523 HConstant* fun_const = HConstant::cast(instr->hydrogen()->function());
3524 jsfun = Handle<JSFunction>::cast(fun_const->handle(isolate()));
3525 is_self_call = jsfun.is_identical_to(info()->closure());
3531 Operand target = FieldOperand(rdi, JSFunction::kCodeEntryOffset);
3532 generator.BeforeCall(__ CallSize(target));
3535 generator.AfterCall();
3539 void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3540 Register input_reg = ToRegister(instr->value());
3541 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
3542 Heap::kHeapNumberMapRootIndex);
3543 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
3545 Label slow, allocated, done;
3546 Register tmp = input_reg.is(rax) ? rcx : rax;
3547 Register tmp2 = tmp.is(rcx) ? rdx : input_reg.is(rcx) ? rdx : rcx;
3549 // Preserve the value of all registers.
3550 PushSafepointRegistersScope scope(this);
3552 __ movl(tmp, FieldOperand(input_reg, HeapNumber::kExponentOffset));
3553 // Check the sign of the argument. If the argument is positive, just
3554 // return it. We do not need to patch the stack since |input| and
3555 // |result| are the same register and |input| will be restored
3556 // unchanged by popping safepoint registers.
3557 __ testl(tmp, Immediate(HeapNumber::kSignMask));
3560 __ AllocateHeapNumber(tmp, tmp2, &slow);
3561 __ jmp(&allocated, Label::kNear);
3563 // Slow case: Call the runtime system to do the number allocation.
3565 CallRuntimeFromDeferred(
3566 Runtime::kAllocateHeapNumber, 0, instr, instr->context());
3567 // Set the pointer to the new heap number in tmp.
3568 if (!tmp.is(rax)) __ movp(tmp, rax);
3569 // Restore input_reg after call to runtime.
3570 __ LoadFromSafepointRegisterSlot(input_reg, input_reg);
3572 __ bind(&allocated);
3573 __ movq(tmp2, FieldOperand(input_reg, HeapNumber::kValueOffset));
3574 __ shlq(tmp2, Immediate(1));
3575 __ shrq(tmp2, Immediate(1));
3576 __ movq(FieldOperand(tmp, HeapNumber::kValueOffset), tmp2);
3577 __ StoreToSafepointRegisterSlot(input_reg, tmp);
3583 void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3584 Register input_reg = ToRegister(instr->value());
3585 __ testl(input_reg, input_reg);
3587 __ j(not_sign, &is_positive, Label::kNear);
3588 __ negl(input_reg); // Sets flags.
3589 DeoptimizeIf(negative, instr, Deoptimizer::kOverflow);
3590 __ bind(&is_positive);
3594 void LCodeGen::EmitSmiMathAbs(LMathAbs* instr) {
3595 Register input_reg = ToRegister(instr->value());
3596 __ testp(input_reg, input_reg);
3598 __ j(not_sign, &is_positive, Label::kNear);
3599 __ negp(input_reg); // Sets flags.
3600 DeoptimizeIf(negative, instr, Deoptimizer::kOverflow);
3601 __ bind(&is_positive);
3605 void LCodeGen::DoMathAbs(LMathAbs* instr) {
3606 // Class for deferred case.
3607 class DeferredMathAbsTaggedHeapNumber final : public LDeferredCode {
3609 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr)
3610 : LDeferredCode(codegen), instr_(instr) { }
3611 void Generate() override {
3612 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3614 LInstruction* instr() override { return instr_; }
3620 DCHECK(instr->value()->Equals(instr->result()));
3621 Representation r = instr->hydrogen()->value()->representation();
3624 XMMRegister scratch = double_scratch0();
3625 XMMRegister input_reg = ToDoubleRegister(instr->value());
3626 __ xorps(scratch, scratch);
3627 __ subsd(scratch, input_reg);
3628 __ andps(input_reg, scratch);
3629 } else if (r.IsInteger32()) {
3630 EmitIntegerMathAbs(instr);
3631 } else if (r.IsSmi()) {
3632 EmitSmiMathAbs(instr);
3633 } else { // Tagged case.
3634 DeferredMathAbsTaggedHeapNumber* deferred =
3635 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3636 Register input_reg = ToRegister(instr->value());
3638 __ JumpIfNotSmi(input_reg, deferred->entry());
3639 EmitSmiMathAbs(instr);
3640 __ bind(deferred->exit());
3645 void LCodeGen::DoMathFloor(LMathFloor* instr) {
3646 XMMRegister xmm_scratch = double_scratch0();
3647 Register output_reg = ToRegister(instr->result());
3648 XMMRegister input_reg = ToDoubleRegister(instr->value());
3650 if (CpuFeatures::IsSupported(SSE4_1)) {
3651 CpuFeatureScope scope(masm(), SSE4_1);
3652 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3653 // Deoptimize if minus zero.
3654 __ movq(output_reg, input_reg);
3655 __ subq(output_reg, Immediate(1));
3656 DeoptimizeIf(overflow, instr, Deoptimizer::kMinusZero);
3658 __ roundsd(xmm_scratch, input_reg, kRoundDown);
3659 __ cvttsd2si(output_reg, xmm_scratch);
3660 __ cmpl(output_reg, Immediate(0x1));
3661 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3663 Label negative_sign, done;
3664 // Deoptimize on unordered.
3665 __ xorps(xmm_scratch, xmm_scratch); // Zero the register.
3666 __ ucomisd(input_reg, xmm_scratch);
3667 DeoptimizeIf(parity_even, instr, Deoptimizer::kNaN);
3668 __ j(below, &negative_sign, Label::kNear);
3670 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3671 // Check for negative zero.
3672 Label positive_sign;
3673 __ j(above, &positive_sign, Label::kNear);
3674 __ movmskpd(output_reg, input_reg);
3675 __ testq(output_reg, Immediate(1));
3676 DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero);
3677 __ Set(output_reg, 0);
3679 __ bind(&positive_sign);
3682 // Use truncating instruction (OK because input is positive).
3683 __ cvttsd2si(output_reg, input_reg);
3684 // Overflow is signalled with minint.
3685 __ cmpl(output_reg, Immediate(0x1));
3686 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3687 __ jmp(&done, Label::kNear);
3689 // Non-zero negative reaches here.
3690 __ bind(&negative_sign);
3691 // Truncate, then compare and compensate.
3692 __ cvttsd2si(output_reg, input_reg);
3693 __ Cvtlsi2sd(xmm_scratch, output_reg);
3694 __ ucomisd(input_reg, xmm_scratch);
3695 __ j(equal, &done, Label::kNear);
3696 __ subl(output_reg, Immediate(1));
3697 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3704 void LCodeGen::DoMathRound(LMathRound* instr) {
3705 const XMMRegister xmm_scratch = double_scratch0();
3706 Register output_reg = ToRegister(instr->result());
3707 XMMRegister input_reg = ToDoubleRegister(instr->value());
3708 XMMRegister input_temp = ToDoubleRegister(instr->temp());
3709 static int64_t one_half = V8_INT64_C(0x3FE0000000000000); // 0.5
3710 static int64_t minus_one_half = V8_INT64_C(0xBFE0000000000000); // -0.5
3712 Label done, round_to_zero, below_one_half;
3713 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
3714 __ movq(kScratchRegister, one_half);
3715 __ movq(xmm_scratch, kScratchRegister);
3716 __ ucomisd(xmm_scratch, input_reg);
3717 __ j(above, &below_one_half, Label::kNear);
3719 // CVTTSD2SI rounds towards zero, since 0.5 <= x, we use floor(0.5 + x).
3720 __ addsd(xmm_scratch, input_reg);
3721 __ cvttsd2si(output_reg, xmm_scratch);
3722 // Overflow is signalled with minint.
3723 __ cmpl(output_reg, Immediate(0x1));
3724 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3725 __ jmp(&done, dist);
3727 __ bind(&below_one_half);
3728 __ movq(kScratchRegister, minus_one_half);
3729 __ movq(xmm_scratch, kScratchRegister);
3730 __ ucomisd(xmm_scratch, input_reg);
3731 __ j(below_equal, &round_to_zero, Label::kNear);
3733 // CVTTSD2SI rounds towards zero, we use ceil(x - (-0.5)) and then
3734 // compare and compensate.
3735 __ movq(input_temp, input_reg); // Do not alter input_reg.
3736 __ subsd(input_temp, xmm_scratch);
3737 __ cvttsd2si(output_reg, input_temp);
3738 // Catch minint due to overflow, and to prevent overflow when compensating.
3739 __ cmpl(output_reg, Immediate(0x1));
3740 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3742 __ Cvtlsi2sd(xmm_scratch, output_reg);
3743 __ ucomisd(xmm_scratch, input_temp);
3744 __ j(equal, &done, dist);
3745 __ subl(output_reg, Immediate(1));
3746 // No overflow because we already ruled out minint.
3747 __ jmp(&done, dist);
3749 __ bind(&round_to_zero);
3750 // We return 0 for the input range [+0, 0.5[, or [-0.5, 0.5[ if
3751 // we can ignore the difference between a result of -0 and +0.
3752 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3753 __ movq(output_reg, input_reg);
3754 __ testq(output_reg, output_reg);
3755 DeoptimizeIf(negative, instr, Deoptimizer::kMinusZero);
3757 __ Set(output_reg, 0);
3762 void LCodeGen::DoMathFround(LMathFround* instr) {
3763 XMMRegister input_reg = ToDoubleRegister(instr->value());
3764 XMMRegister output_reg = ToDoubleRegister(instr->result());
3765 __ cvtsd2ss(output_reg, input_reg);
3766 __ cvtss2sd(output_reg, output_reg);
3770 void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3771 XMMRegister output = ToDoubleRegister(instr->result());
3772 if (instr->value()->IsDoubleRegister()) {
3773 XMMRegister input = ToDoubleRegister(instr->value());
3774 __ sqrtsd(output, input);
3776 Operand input = ToOperand(instr->value());
3777 __ sqrtsd(output, input);
3782 void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3783 XMMRegister xmm_scratch = double_scratch0();
3784 XMMRegister input_reg = ToDoubleRegister(instr->value());
3785 DCHECK(ToDoubleRegister(instr->result()).is(input_reg));
3787 // Note that according to ECMA-262 15.8.2.13:
3788 // Math.pow(-Infinity, 0.5) == Infinity
3789 // Math.sqrt(-Infinity) == NaN
3791 // Check base for -Infinity. According to IEEE-754, double-precision
3792 // -Infinity has the highest 12 bits set and the lowest 52 bits cleared.
3793 __ movq(kScratchRegister, V8_INT64_C(0xFFF0000000000000));
3794 __ movq(xmm_scratch, kScratchRegister);
3795 __ ucomisd(xmm_scratch, input_reg);
3796 // Comparing -Infinity with NaN results in "unordered", which sets the
3797 // zero flag as if both were equal. However, it also sets the carry flag.
3798 __ j(not_equal, &sqrt, Label::kNear);
3799 __ j(carry, &sqrt, Label::kNear);
3800 // If input is -Infinity, return Infinity.
3801 __ xorps(input_reg, input_reg);
3802 __ subsd(input_reg, xmm_scratch);
3803 __ jmp(&done, Label::kNear);
3807 __ xorps(xmm_scratch, xmm_scratch);
3808 __ addsd(input_reg, xmm_scratch); // Convert -0 to +0.
3809 __ sqrtsd(input_reg, input_reg);
3814 void LCodeGen::DoPower(LPower* instr) {
3815 Representation exponent_type = instr->hydrogen()->right()->representation();
3816 // Having marked this as a call, we can use any registers.
3817 // Just make sure that the input/output registers are the expected ones.
3819 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3820 DCHECK(!instr->right()->IsRegister() ||
3821 ToRegister(instr->right()).is(tagged_exponent));
3822 DCHECK(!instr->right()->IsDoubleRegister() ||
3823 ToDoubleRegister(instr->right()).is(xmm1));
3824 DCHECK(ToDoubleRegister(instr->left()).is(xmm2));
3825 DCHECK(ToDoubleRegister(instr->result()).is(xmm3));
3827 if (exponent_type.IsSmi()) {
3828 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3830 } else if (exponent_type.IsTagged()) {
3832 __ JumpIfSmi(tagged_exponent, &no_deopt, Label::kNear);
3833 __ CmpObjectType(tagged_exponent, HEAP_NUMBER_TYPE, rcx);
3834 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
3836 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3838 } else if (exponent_type.IsInteger32()) {
3839 MathPowStub stub(isolate(), MathPowStub::INTEGER);
3842 DCHECK(exponent_type.IsDouble());
3843 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
3849 void LCodeGen::DoMathExp(LMathExp* instr) {
3850 XMMRegister input = ToDoubleRegister(instr->value());
3851 XMMRegister result = ToDoubleRegister(instr->result());
3852 XMMRegister temp0 = double_scratch0();
3853 Register temp1 = ToRegister(instr->temp1());
3854 Register temp2 = ToRegister(instr->temp2());
3856 MathExpGenerator::EmitMathExp(masm(), input, result, temp0, temp1, temp2);
3860 void LCodeGen::DoMathLog(LMathLog* instr) {
3861 DCHECK(instr->value()->Equals(instr->result()));
3862 XMMRegister input_reg = ToDoubleRegister(instr->value());
3863 XMMRegister xmm_scratch = double_scratch0();
3864 Label positive, done, zero;
3865 __ xorps(xmm_scratch, xmm_scratch);
3866 __ ucomisd(input_reg, xmm_scratch);
3867 __ j(above, &positive, Label::kNear);
3868 __ j(not_carry, &zero, Label::kNear);
3869 __ pcmpeqd(input_reg, input_reg);
3870 __ jmp(&done, Label::kNear);
3872 ExternalReference ninf =
3873 ExternalReference::address_of_negative_infinity();
3874 Operand ninf_operand = masm()->ExternalOperand(ninf);
3875 __ movsd(input_reg, ninf_operand);
3876 __ jmp(&done, Label::kNear);
3879 __ subp(rsp, Immediate(kDoubleSize));
3880 __ movsd(Operand(rsp, 0), input_reg);
3881 __ fld_d(Operand(rsp, 0));
3883 __ fstp_d(Operand(rsp, 0));
3884 __ movsd(input_reg, Operand(rsp, 0));
3885 __ addp(rsp, Immediate(kDoubleSize));
3890 void LCodeGen::DoMathClz32(LMathClz32* instr) {
3891 Register input = ToRegister(instr->value());
3892 Register result = ToRegister(instr->result());
3894 __ Lzcntl(result, input);
3898 void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
3899 DCHECK(ToRegister(instr->context()).is(rsi));
3900 DCHECK(ToRegister(instr->function()).is(rdi));
3901 DCHECK(instr->HasPointerMap());
3903 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
3904 if (known_function.is_null()) {
3905 LPointerMap* pointers = instr->pointer_map();
3906 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3907 ParameterCount count(instr->arity());
3908 __ InvokeFunction(rdi, count, CALL_FUNCTION, generator);
3910 CallKnownFunction(known_function,
3911 instr->hydrogen()->formal_parameter_count(),
3912 instr->arity(), instr);
3917 void LCodeGen::DoCallFunction(LCallFunction* instr) {
3918 DCHECK(ToRegister(instr->context()).is(rsi));
3919 DCHECK(ToRegister(instr->function()).is(rdi));
3920 DCHECK(ToRegister(instr->result()).is(rax));
3922 int arity = instr->arity();
3923 CallFunctionFlags flags = instr->hydrogen()->function_flags();
3924 if (instr->hydrogen()->HasVectorAndSlot()) {
3925 Register slot_register = ToRegister(instr->temp_slot());
3926 Register vector_register = ToRegister(instr->temp_vector());
3927 DCHECK(slot_register.is(rdx));
3928 DCHECK(vector_register.is(rbx));
3930 AllowDeferredHandleDereference vector_structure_check;
3931 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
3932 int index = vector->GetIndex(instr->hydrogen()->slot());
3934 __ Move(vector_register, vector);
3935 __ Move(slot_register, Smi::FromInt(index));
3937 CallICState::CallType call_type =
3938 (flags & CALL_AS_METHOD) ? CallICState::METHOD : CallICState::FUNCTION;
3941 CodeFactory::CallICInOptimizedCode(isolate(), arity, call_type).code();
3942 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3944 CallFunctionStub stub(isolate(), arity, flags);
3945 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3950 void LCodeGen::DoCallNew(LCallNew* instr) {
3951 DCHECK(ToRegister(instr->context()).is(rsi));
3952 DCHECK(ToRegister(instr->constructor()).is(rdi));
3953 DCHECK(ToRegister(instr->result()).is(rax));
3955 __ Set(rax, instr->arity());
3956 // No cell in ebx for construct type feedback in optimized code
3957 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
3958 CallConstructStub stub(isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
3959 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
3963 void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
3964 DCHECK(ToRegister(instr->context()).is(rsi));
3965 DCHECK(ToRegister(instr->constructor()).is(rdi));
3966 DCHECK(ToRegister(instr->result()).is(rax));
3968 __ Set(rax, instr->arity());
3969 if (instr->arity() == 1) {
3970 // We only need the allocation site for the case we have a length argument.
3971 // The case may bail out to the runtime, which will determine the correct
3972 // elements kind with the site.
3973 __ Move(rbx, instr->hydrogen()->site());
3975 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
3978 ElementsKind kind = instr->hydrogen()->elements_kind();
3979 AllocationSiteOverrideMode override_mode =
3980 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
3981 ? DISABLE_ALLOCATION_SITES
3984 if (instr->arity() == 0) {
3985 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
3986 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
3987 } else if (instr->arity() == 1) {
3989 if (IsFastPackedElementsKind(kind)) {
3991 // We might need a change here
3992 // look at the first argument
3993 __ movp(rcx, Operand(rsp, 0));
3995 __ j(zero, &packed_case, Label::kNear);
3997 ElementsKind holey_kind = GetHoleyElementsKind(kind);
3998 ArraySingleArgumentConstructorStub stub(isolate(),
4001 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4002 __ jmp(&done, Label::kNear);
4003 __ bind(&packed_case);
4006 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
4007 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4010 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
4011 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4016 void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
4017 DCHECK(ToRegister(instr->context()).is(rsi));
4018 CallRuntime(instr->function(), instr->arity(), instr, instr->save_doubles());
4022 void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
4023 Register function = ToRegister(instr->function());
4024 Register code_object = ToRegister(instr->code_object());
4025 __ leap(code_object, FieldOperand(code_object, Code::kHeaderSize));
4026 __ movp(FieldOperand(function, JSFunction::kCodeEntryOffset), code_object);
4030 void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
4031 Register result = ToRegister(instr->result());
4032 Register base = ToRegister(instr->base_object());
4033 if (instr->offset()->IsConstantOperand()) {
4034 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
4035 __ leap(result, Operand(base, ToInteger32(offset)));
4037 Register offset = ToRegister(instr->offset());
4038 __ leap(result, Operand(base, offset, times_1, 0));
4043 void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
4044 HStoreNamedField* hinstr = instr->hydrogen();
4045 Representation representation = instr->representation();
4047 HObjectAccess access = hinstr->access();
4048 int offset = access.offset();
4050 if (access.IsExternalMemory()) {
4051 DCHECK(!hinstr->NeedsWriteBarrier());
4052 Register value = ToRegister(instr->value());
4053 if (instr->object()->IsConstantOperand()) {
4054 DCHECK(value.is(rax));
4055 LConstantOperand* object = LConstantOperand::cast(instr->object());
4056 __ store_rax(ToExternalReference(object));
4058 Register object = ToRegister(instr->object());
4059 __ Store(MemOperand(object, offset), value, representation);
4064 Register object = ToRegister(instr->object());
4065 __ AssertNotSmi(object);
4067 DCHECK(!representation.IsSmi() ||
4068 !instr->value()->IsConstantOperand() ||
4069 IsInteger32Constant(LConstantOperand::cast(instr->value())));
4070 if (!FLAG_unbox_double_fields && representation.IsDouble()) {
4071 DCHECK(access.IsInobject());
4072 DCHECK(!hinstr->has_transition());
4073 DCHECK(!hinstr->NeedsWriteBarrier());
4074 XMMRegister value = ToDoubleRegister(instr->value());
4075 __ movsd(FieldOperand(object, offset), value);
4079 if (hinstr->has_transition()) {
4080 Handle<Map> transition = hinstr->transition_map();
4081 AddDeprecationDependency(transition);
4082 if (!hinstr->NeedsWriteBarrierForMap()) {
4083 __ Move(FieldOperand(object, HeapObject::kMapOffset), transition);
4085 Register temp = ToRegister(instr->temp());
4086 __ Move(kScratchRegister, transition);
4087 __ movp(FieldOperand(object, HeapObject::kMapOffset), kScratchRegister);
4088 // Update the write barrier for the map field.
4089 __ RecordWriteForMap(object,
4097 Register write_register = object;
4098 if (!access.IsInobject()) {
4099 write_register = ToRegister(instr->temp());
4100 __ movp(write_register, FieldOperand(object, JSObject::kPropertiesOffset));
4103 if (representation.IsSmi() && SmiValuesAre32Bits() &&
4104 hinstr->value()->representation().IsInteger32()) {
4105 DCHECK(hinstr->store_mode() == STORE_TO_INITIALIZED_ENTRY);
4106 if (FLAG_debug_code) {
4107 Register scratch = kScratchRegister;
4108 __ Load(scratch, FieldOperand(write_register, offset), representation);
4109 __ AssertSmi(scratch);
4111 // Store int value directly to upper half of the smi.
4112 STATIC_ASSERT(kSmiTag == 0);
4113 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
4114 offset += kPointerSize / 2;
4115 representation = Representation::Integer32();
4118 Operand operand = FieldOperand(write_register, offset);
4120 if (FLAG_unbox_double_fields && representation.IsDouble()) {
4121 DCHECK(access.IsInobject());
4122 XMMRegister value = ToDoubleRegister(instr->value());
4123 __ movsd(operand, value);
4125 } else if (instr->value()->IsRegister()) {
4126 Register value = ToRegister(instr->value());
4127 __ Store(operand, value, representation);
4129 LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
4130 if (IsInteger32Constant(operand_value)) {
4131 DCHECK(!hinstr->NeedsWriteBarrier());
4132 int32_t value = ToInteger32(operand_value);
4133 if (representation.IsSmi()) {
4134 __ Move(operand, Smi::FromInt(value));
4137 __ movl(operand, Immediate(value));
4140 } else if (IsExternalConstant(operand_value)) {
4141 DCHECK(!hinstr->NeedsWriteBarrier());
4142 ExternalReference ptr = ToExternalReference(operand_value);
4143 __ Move(kScratchRegister, ptr);
4144 __ movp(operand, kScratchRegister);
4146 Handle<Object> handle_value = ToHandle(operand_value);
4147 DCHECK(!hinstr->NeedsWriteBarrier());
4148 __ Move(operand, handle_value);
4152 if (hinstr->NeedsWriteBarrier()) {
4153 Register value = ToRegister(instr->value());
4154 Register temp = access.IsInobject() ? ToRegister(instr->temp()) : object;
4155 // Update the write barrier for the object for in-object properties.
4156 __ RecordWriteField(write_register,
4161 EMIT_REMEMBERED_SET,
4162 hinstr->SmiCheckForWriteBarrier(),
4163 hinstr->PointersToHereCheckForValue());
4168 void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
4169 DCHECK(ToRegister(instr->context()).is(rsi));
4170 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4171 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4173 if (instr->hydrogen()->HasVectorAndSlot()) {
4174 EmitVectorStoreICRegisters<LStoreNamedGeneric>(instr);
4177 __ Move(StoreDescriptor::NameRegister(), instr->hydrogen()->name());
4178 Handle<Code> ic = CodeFactory::StoreICInOptimizedCode(
4179 isolate(), instr->language_mode(),
4180 instr->hydrogen()->initialization_state()).code();
4181 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4185 void LCodeGen::DoStoreGlobalViaContext(LStoreGlobalViaContext* instr) {
4186 DCHECK(ToRegister(instr->context()).is(rsi));
4187 DCHECK(ToRegister(instr->value())
4188 .is(StoreGlobalViaContextDescriptor::ValueRegister()));
4189 int const slot = instr->slot_index();
4190 int const depth = instr->depth();
4191 if (depth <= StoreGlobalViaContextStub::kMaximumDepth) {
4192 __ Set(StoreGlobalViaContextDescriptor::SlotRegister(), slot);
4193 Handle<Code> stub = CodeFactory::StoreGlobalViaContext(
4194 isolate(), depth, instr->language_mode())
4196 CallCode(stub, RelocInfo::CODE_TARGET, instr);
4198 __ Push(Smi::FromInt(slot));
4199 __ Push(StoreGlobalViaContextDescriptor::ValueRegister());
4200 __ CallRuntime(is_strict(instr->language_mode())
4201 ? Runtime::kStoreGlobalViaContext_Strict
4202 : Runtime::kStoreGlobalViaContext_Sloppy,
4208 void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
4209 Representation representation = instr->hydrogen()->length()->representation();
4210 DCHECK(representation.Equals(instr->hydrogen()->index()->representation()));
4211 DCHECK(representation.IsSmiOrInteger32());
4213 Condition cc = instr->hydrogen()->allow_equality() ? below : below_equal;
4214 if (instr->length()->IsConstantOperand()) {
4215 int32_t length = ToInteger32(LConstantOperand::cast(instr->length()));
4216 Register index = ToRegister(instr->index());
4217 if (representation.IsSmi()) {
4218 __ Cmp(index, Smi::FromInt(length));
4220 __ cmpl(index, Immediate(length));
4222 cc = CommuteCondition(cc);
4223 } else if (instr->index()->IsConstantOperand()) {
4224 int32_t index = ToInteger32(LConstantOperand::cast(instr->index()));
4225 if (instr->length()->IsRegister()) {
4226 Register length = ToRegister(instr->length());
4227 if (representation.IsSmi()) {
4228 __ Cmp(length, Smi::FromInt(index));
4230 __ cmpl(length, Immediate(index));
4233 Operand length = ToOperand(instr->length());
4234 if (representation.IsSmi()) {
4235 __ Cmp(length, Smi::FromInt(index));
4237 __ cmpl(length, Immediate(index));
4241 Register index = ToRegister(instr->index());
4242 if (instr->length()->IsRegister()) {
4243 Register length = ToRegister(instr->length());
4244 if (representation.IsSmi()) {
4245 __ cmpp(length, index);
4247 __ cmpl(length, index);
4250 Operand length = ToOperand(instr->length());
4251 if (representation.IsSmi()) {
4252 __ cmpp(length, index);
4254 __ cmpl(length, index);
4258 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
4260 __ j(NegateCondition(cc), &done, Label::kNear);
4264 DeoptimizeIf(cc, instr, Deoptimizer::kOutOfBounds);
4269 void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
4270 ElementsKind elements_kind = instr->elements_kind();
4271 LOperand* key = instr->key();
4272 if (kPointerSize == kInt32Size && !key->IsConstantOperand()) {
4273 Register key_reg = ToRegister(key);
4274 Representation key_representation =
4275 instr->hydrogen()->key()->representation();
4276 if (ExternalArrayOpRequiresTemp(key_representation, elements_kind)) {
4277 __ SmiToInteger64(key_reg, key_reg);
4278 } else if (instr->hydrogen()->IsDehoisted()) {
4279 // Sign extend key because it could be a 32 bit negative value
4280 // and the dehoisted address computation happens in 64 bits
4281 __ movsxlq(key_reg, key_reg);
4284 Operand operand(BuildFastArrayOperand(
4287 instr->hydrogen()->key()->representation(),
4289 instr->base_offset()));
4291 if (elements_kind == FLOAT32_ELEMENTS) {
4292 XMMRegister value(ToDoubleRegister(instr->value()));
4293 __ cvtsd2ss(value, value);
4294 __ movss(operand, value);
4295 } else if (elements_kind == FLOAT64_ELEMENTS) {
4296 __ movsd(operand, ToDoubleRegister(instr->value()));
4298 Register value(ToRegister(instr->value()));
4299 switch (elements_kind) {
4301 case UINT8_ELEMENTS:
4302 case UINT8_CLAMPED_ELEMENTS:
4303 __ movb(operand, value);
4305 case INT16_ELEMENTS:
4306 case UINT16_ELEMENTS:
4307 __ movw(operand, value);
4309 case INT32_ELEMENTS:
4310 case UINT32_ELEMENTS:
4311 __ movl(operand, value);
4313 case FLOAT32_ELEMENTS:
4314 case FLOAT64_ELEMENTS:
4316 case FAST_SMI_ELEMENTS:
4317 case FAST_DOUBLE_ELEMENTS:
4318 case FAST_HOLEY_ELEMENTS:
4319 case FAST_HOLEY_SMI_ELEMENTS:
4320 case FAST_HOLEY_DOUBLE_ELEMENTS:
4321 case DICTIONARY_ELEMENTS:
4322 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
4323 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
4331 void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
4332 XMMRegister value = ToDoubleRegister(instr->value());
4333 LOperand* key = instr->key();
4334 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
4335 instr->hydrogen()->IsDehoisted()) {
4336 // Sign extend key because it could be a 32 bit negative value
4337 // and the dehoisted address computation happens in 64 bits
4338 __ movsxlq(ToRegister(key), ToRegister(key));
4340 if (instr->NeedsCanonicalization()) {
4341 XMMRegister xmm_scratch = double_scratch0();
4342 // Turn potential sNaN value into qNaN.
4343 __ xorps(xmm_scratch, xmm_scratch);
4344 __ subsd(value, xmm_scratch);
4347 Operand double_store_operand = BuildFastArrayOperand(
4350 instr->hydrogen()->key()->representation(),
4351 FAST_DOUBLE_ELEMENTS,
4352 instr->base_offset());
4354 __ movsd(double_store_operand, value);
4358 void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4359 HStoreKeyed* hinstr = instr->hydrogen();
4360 LOperand* key = instr->key();
4361 int offset = instr->base_offset();
4362 Representation representation = hinstr->value()->representation();
4364 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
4365 instr->hydrogen()->IsDehoisted()) {
4366 // Sign extend key because it could be a 32 bit negative value
4367 // and the dehoisted address computation happens in 64 bits
4368 __ movsxlq(ToRegister(key), ToRegister(key));
4370 if (representation.IsInteger32() && SmiValuesAre32Bits()) {
4371 DCHECK(hinstr->store_mode() == STORE_TO_INITIALIZED_ENTRY);
4372 DCHECK(hinstr->elements_kind() == FAST_SMI_ELEMENTS);
4373 if (FLAG_debug_code) {
4374 Register scratch = kScratchRegister;
4376 BuildFastArrayOperand(instr->elements(),
4378 instr->hydrogen()->key()->representation(),
4381 Representation::Smi());
4382 __ AssertSmi(scratch);
4384 // Store int value directly to upper half of the smi.
4385 STATIC_ASSERT(kSmiTag == 0);
4386 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
4387 offset += kPointerSize / 2;
4391 BuildFastArrayOperand(instr->elements(),
4393 instr->hydrogen()->key()->representation(),
4396 if (instr->value()->IsRegister()) {
4397 __ Store(operand, ToRegister(instr->value()), representation);
4399 LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
4400 if (IsInteger32Constant(operand_value)) {
4401 int32_t value = ToInteger32(operand_value);
4402 if (representation.IsSmi()) {
4403 __ Move(operand, Smi::FromInt(value));
4406 __ movl(operand, Immediate(value));
4409 Handle<Object> handle_value = ToHandle(operand_value);
4410 __ Move(operand, handle_value);
4414 if (hinstr->NeedsWriteBarrier()) {
4415 Register elements = ToRegister(instr->elements());
4416 DCHECK(instr->value()->IsRegister());
4417 Register value = ToRegister(instr->value());
4418 DCHECK(!key->IsConstantOperand());
4419 SmiCheck check_needed = hinstr->value()->type().IsHeapObject()
4420 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4421 // Compute address of modified element and store it into key register.
4422 Register key_reg(ToRegister(key));
4423 __ leap(key_reg, operand);
4424 __ RecordWrite(elements,
4428 EMIT_REMEMBERED_SET,
4430 hinstr->PointersToHereCheckForValue());
4435 void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4436 if (instr->is_fixed_typed_array()) {
4437 DoStoreKeyedExternalArray(instr);
4438 } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4439 DoStoreKeyedFixedDoubleArray(instr);
4441 DoStoreKeyedFixedArray(instr);
4446 void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4447 DCHECK(ToRegister(instr->context()).is(rsi));
4448 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4449 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4450 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4452 if (instr->hydrogen()->HasVectorAndSlot()) {
4453 EmitVectorStoreICRegisters<LStoreKeyedGeneric>(instr);
4456 Handle<Code> ic = CodeFactory::KeyedStoreICInOptimizedCode(
4457 isolate(), instr->language_mode(),
4458 instr->hydrogen()->initialization_state()).code();
4459 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4463 void LCodeGen::DoMaybeGrowElements(LMaybeGrowElements* instr) {
4464 class DeferredMaybeGrowElements final : public LDeferredCode {
4466 DeferredMaybeGrowElements(LCodeGen* codegen, LMaybeGrowElements* instr)
4467 : LDeferredCode(codegen), instr_(instr) {}
4468 void Generate() override { codegen()->DoDeferredMaybeGrowElements(instr_); }
4469 LInstruction* instr() override { return instr_; }
4472 LMaybeGrowElements* instr_;
4475 Register result = rax;
4476 DeferredMaybeGrowElements* deferred =
4477 new (zone()) DeferredMaybeGrowElements(this, instr);
4478 LOperand* key = instr->key();
4479 LOperand* current_capacity = instr->current_capacity();
4481 DCHECK(instr->hydrogen()->key()->representation().IsInteger32());
4482 DCHECK(instr->hydrogen()->current_capacity()->representation().IsInteger32());
4483 DCHECK(key->IsConstantOperand() || key->IsRegister());
4484 DCHECK(current_capacity->IsConstantOperand() ||
4485 current_capacity->IsRegister());
4487 if (key->IsConstantOperand() && current_capacity->IsConstantOperand()) {
4488 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4489 int32_t constant_capacity =
4490 ToInteger32(LConstantOperand::cast(current_capacity));
4491 if (constant_key >= constant_capacity) {
4493 __ jmp(deferred->entry());
4495 } else if (key->IsConstantOperand()) {
4496 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4497 __ cmpl(ToRegister(current_capacity), Immediate(constant_key));
4498 __ j(less_equal, deferred->entry());
4499 } else if (current_capacity->IsConstantOperand()) {
4500 int32_t constant_capacity =
4501 ToInteger32(LConstantOperand::cast(current_capacity));
4502 __ cmpl(ToRegister(key), Immediate(constant_capacity));
4503 __ j(greater_equal, deferred->entry());
4505 __ cmpl(ToRegister(key), ToRegister(current_capacity));
4506 __ j(greater_equal, deferred->entry());
4509 if (instr->elements()->IsRegister()) {
4510 __ movp(result, ToRegister(instr->elements()));
4512 __ movp(result, ToOperand(instr->elements()));
4515 __ bind(deferred->exit());
4519 void LCodeGen::DoDeferredMaybeGrowElements(LMaybeGrowElements* instr) {
4520 // TODO(3095996): Get rid of this. For now, we need to make the
4521 // result register contain a valid pointer because it is already
4522 // contained in the register pointer map.
4523 Register result = rax;
4524 __ Move(result, Smi::FromInt(0));
4526 // We have to call a stub.
4528 PushSafepointRegistersScope scope(this);
4529 if (instr->object()->IsConstantOperand()) {
4530 LConstantOperand* constant_object =
4531 LConstantOperand::cast(instr->object());
4532 if (IsSmiConstant(constant_object)) {
4533 Smi* immediate = ToSmi(constant_object);
4534 __ Move(result, immediate);
4536 Handle<Object> handle_value = ToHandle(constant_object);
4537 __ Move(result, handle_value);
4539 } else if (instr->object()->IsRegister()) {
4540 __ Move(result, ToRegister(instr->object()));
4542 __ movp(result, ToOperand(instr->object()));
4545 LOperand* key = instr->key();
4546 if (key->IsConstantOperand()) {
4547 __ Move(rbx, ToSmi(LConstantOperand::cast(key)));
4549 __ Move(rbx, ToRegister(key));
4550 __ Integer32ToSmi(rbx, rbx);
4553 GrowArrayElementsStub stub(isolate(), instr->hydrogen()->is_js_array(),
4554 instr->hydrogen()->kind());
4556 RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0);
4557 __ StoreToSafepointRegisterSlot(result, result);
4560 // Deopt on smi, which means the elements array changed to dictionary mode.
4561 Condition is_smi = __ CheckSmi(result);
4562 DeoptimizeIf(is_smi, instr, Deoptimizer::kSmi);
4566 void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4567 Register object_reg = ToRegister(instr->object());
4569 Handle<Map> from_map = instr->original_map();
4570 Handle<Map> to_map = instr->transitioned_map();
4571 ElementsKind from_kind = instr->from_kind();
4572 ElementsKind to_kind = instr->to_kind();
4574 Label not_applicable;
4575 __ Cmp(FieldOperand(object_reg, HeapObject::kMapOffset), from_map);
4576 __ j(not_equal, ¬_applicable);
4577 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
4578 Register new_map_reg = ToRegister(instr->new_map_temp());
4579 __ Move(new_map_reg, to_map, RelocInfo::EMBEDDED_OBJECT);
4580 __ movp(FieldOperand(object_reg, HeapObject::kMapOffset), new_map_reg);
4582 __ RecordWriteForMap(object_reg, new_map_reg, ToRegister(instr->temp()),
4585 DCHECK(object_reg.is(rax));
4586 DCHECK(ToRegister(instr->context()).is(rsi));
4587 PushSafepointRegistersScope scope(this);
4588 __ Move(rbx, to_map);
4589 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4590 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4592 RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0);
4594 __ bind(¬_applicable);
4598 void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4599 Register object = ToRegister(instr->object());
4600 Register temp = ToRegister(instr->temp());
4601 Label no_memento_found;
4602 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found);
4603 DeoptimizeIf(equal, instr, Deoptimizer::kMementoFound);
4604 __ bind(&no_memento_found);
4608 void LCodeGen::DoStringAdd(LStringAdd* instr) {
4609 DCHECK(ToRegister(instr->context()).is(rsi));
4610 DCHECK(ToRegister(instr->left()).is(rdx));
4611 DCHECK(ToRegister(instr->right()).is(rax));
4612 StringAddStub stub(isolate(),
4613 instr->hydrogen()->flags(),
4614 instr->hydrogen()->pretenure_flag());
4615 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4619 void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
4620 class DeferredStringCharCodeAt final : public LDeferredCode {
4622 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
4623 : LDeferredCode(codegen), instr_(instr) { }
4624 void Generate() override { codegen()->DoDeferredStringCharCodeAt(instr_); }
4625 LInstruction* instr() override { return instr_; }
4628 LStringCharCodeAt* instr_;
4631 DeferredStringCharCodeAt* deferred =
4632 new(zone()) DeferredStringCharCodeAt(this, instr);
4634 StringCharLoadGenerator::Generate(masm(),
4635 ToRegister(instr->string()),
4636 ToRegister(instr->index()),
4637 ToRegister(instr->result()),
4639 __ bind(deferred->exit());
4643 void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4644 Register string = ToRegister(instr->string());
4645 Register result = ToRegister(instr->result());
4647 // TODO(3095996): Get rid of this. For now, we need to make the
4648 // result register contain a valid pointer because it is already
4649 // contained in the register pointer map.
4652 PushSafepointRegistersScope scope(this);
4654 // Push the index as a smi. This is safe because of the checks in
4655 // DoStringCharCodeAt above.
4656 STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue);
4657 if (instr->index()->IsConstantOperand()) {
4658 int32_t const_index = ToInteger32(LConstantOperand::cast(instr->index()));
4659 __ Push(Smi::FromInt(const_index));
4661 Register index = ToRegister(instr->index());
4662 __ Integer32ToSmi(index, index);
4665 CallRuntimeFromDeferred(
4666 Runtime::kStringCharCodeAtRT, 2, instr, instr->context());
4668 __ SmiToInteger32(rax, rax);
4669 __ StoreToSafepointRegisterSlot(result, rax);
4673 void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
4674 class DeferredStringCharFromCode final : public LDeferredCode {
4676 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
4677 : LDeferredCode(codegen), instr_(instr) { }
4678 void Generate() override {
4679 codegen()->DoDeferredStringCharFromCode(instr_);
4681 LInstruction* instr() override { return instr_; }
4684 LStringCharFromCode* instr_;
4687 DeferredStringCharFromCode* deferred =
4688 new(zone()) DeferredStringCharFromCode(this, instr);
4690 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
4691 Register char_code = ToRegister(instr->char_code());
4692 Register result = ToRegister(instr->result());
4693 DCHECK(!char_code.is(result));
4695 __ cmpl(char_code, Immediate(String::kMaxOneByteCharCode));
4696 __ j(above, deferred->entry());
4697 __ movsxlq(char_code, char_code);
4698 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
4699 __ movp(result, FieldOperand(result,
4700 char_code, times_pointer_size,
4701 FixedArray::kHeaderSize));
4702 __ CompareRoot(result, Heap::kUndefinedValueRootIndex);
4703 __ j(equal, deferred->entry());
4704 __ bind(deferred->exit());
4708 void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4709 Register char_code = ToRegister(instr->char_code());
4710 Register result = ToRegister(instr->result());
4712 // TODO(3095996): Get rid of this. For now, we need to make the
4713 // result register contain a valid pointer because it is already
4714 // contained in the register pointer map.
4717 PushSafepointRegistersScope scope(this);
4718 __ Integer32ToSmi(char_code, char_code);
4720 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr, instr->context());
4721 __ StoreToSafepointRegisterSlot(result, rax);
4725 void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
4726 LOperand* input = instr->value();
4727 DCHECK(input->IsRegister() || input->IsStackSlot());
4728 LOperand* output = instr->result();
4729 DCHECK(output->IsDoubleRegister());
4730 if (input->IsRegister()) {
4731 __ Cvtlsi2sd(ToDoubleRegister(output), ToRegister(input));
4733 __ Cvtlsi2sd(ToDoubleRegister(output), ToOperand(input));
4738 void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4739 LOperand* input = instr->value();
4740 LOperand* output = instr->result();
4742 __ LoadUint32(ToDoubleRegister(output), ToRegister(input));
4746 void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
4747 class DeferredNumberTagI final : public LDeferredCode {
4749 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
4750 : LDeferredCode(codegen), instr_(instr) { }
4751 void Generate() override {
4752 codegen()->DoDeferredNumberTagIU(instr_, instr_->value(), instr_->temp1(),
4753 instr_->temp2(), SIGNED_INT32);
4755 LInstruction* instr() override { return instr_; }
4758 LNumberTagI* instr_;
4761 LOperand* input = instr->value();
4762 DCHECK(input->IsRegister() && input->Equals(instr->result()));
4763 Register reg = ToRegister(input);
4765 if (SmiValuesAre32Bits()) {
4766 __ Integer32ToSmi(reg, reg);
4768 DCHECK(SmiValuesAre31Bits());
4769 DeferredNumberTagI* deferred = new(zone()) DeferredNumberTagI(this, instr);
4770 __ Integer32ToSmi(reg, reg);
4771 __ j(overflow, deferred->entry());
4772 __ bind(deferred->exit());
4777 void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4778 class DeferredNumberTagU final : public LDeferredCode {
4780 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4781 : LDeferredCode(codegen), instr_(instr) { }
4782 void Generate() override {
4783 codegen()->DoDeferredNumberTagIU(instr_, instr_->value(), instr_->temp1(),
4784 instr_->temp2(), UNSIGNED_INT32);
4786 LInstruction* instr() override { return instr_; }
4789 LNumberTagU* instr_;
4792 LOperand* input = instr->value();
4793 DCHECK(input->IsRegister() && input->Equals(instr->result()));
4794 Register reg = ToRegister(input);
4796 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4797 __ cmpl(reg, Immediate(Smi::kMaxValue));
4798 __ j(above, deferred->entry());
4799 __ Integer32ToSmi(reg, reg);
4800 __ bind(deferred->exit());
4804 void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4808 IntegerSignedness signedness) {
4810 Register reg = ToRegister(value);
4811 Register tmp = ToRegister(temp1);
4812 XMMRegister temp_xmm = ToDoubleRegister(temp2);
4814 // Load value into temp_xmm which will be preserved across potential call to
4815 // runtime (MacroAssembler::EnterExitFrameEpilogue preserves only allocatable
4816 // XMM registers on x64).
4817 if (signedness == SIGNED_INT32) {
4818 DCHECK(SmiValuesAre31Bits());
4819 // There was overflow, so bits 30 and 31 of the original integer
4820 // disagree. Try to allocate a heap number in new space and store
4821 // the value in there. If that fails, call the runtime system.
4822 __ SmiToInteger32(reg, reg);
4823 __ xorl(reg, Immediate(0x80000000));
4824 __ cvtlsi2sd(temp_xmm, reg);
4826 DCHECK(signedness == UNSIGNED_INT32);
4827 __ LoadUint32(temp_xmm, reg);
4830 if (FLAG_inline_new) {
4831 __ AllocateHeapNumber(reg, tmp, &slow);
4832 __ jmp(&done, kPointerSize == kInt64Size ? Label::kNear : Label::kFar);
4835 // Slow case: Call the runtime system to do the number allocation.
4838 // Put a valid pointer value in the stack slot where the result
4839 // register is stored, as this register is in the pointer map, but contains
4840 // an integer value.
4843 // Preserve the value of all registers.
4844 PushSafepointRegistersScope scope(this);
4846 // NumberTagIU uses the context from the frame, rather than
4847 // the environment's HContext or HInlinedContext value.
4848 // They only call Runtime::kAllocateHeapNumber.
4849 // The corresponding HChange instructions are added in a phase that does
4850 // not have easy access to the local context.
4851 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
4852 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4853 RecordSafepointWithRegisters(
4854 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4855 __ StoreToSafepointRegisterSlot(reg, rax);
4858 // Done. Put the value in temp_xmm into the value of the allocated heap
4861 __ movsd(FieldOperand(reg, HeapNumber::kValueOffset), temp_xmm);
4865 void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
4866 class DeferredNumberTagD final : public LDeferredCode {
4868 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4869 : LDeferredCode(codegen), instr_(instr) { }
4870 void Generate() override { codegen()->DoDeferredNumberTagD(instr_); }
4871 LInstruction* instr() override { return instr_; }
4874 LNumberTagD* instr_;
4877 XMMRegister input_reg = ToDoubleRegister(instr->value());
4878 Register reg = ToRegister(instr->result());
4879 Register tmp = ToRegister(instr->temp());
4881 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
4882 if (FLAG_inline_new) {
4883 __ AllocateHeapNumber(reg, tmp, deferred->entry());
4885 __ jmp(deferred->entry());
4887 __ bind(deferred->exit());
4888 __ movsd(FieldOperand(reg, HeapNumber::kValueOffset), input_reg);
4892 void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4893 // TODO(3095996): Get rid of this. For now, we need to make the
4894 // result register contain a valid pointer because it is already
4895 // contained in the register pointer map.
4896 Register reg = ToRegister(instr->result());
4897 __ Move(reg, Smi::FromInt(0));
4900 PushSafepointRegistersScope scope(this);
4901 // NumberTagD uses the context from the frame, rather than
4902 // the environment's HContext or HInlinedContext value.
4903 // They only call Runtime::kAllocateHeapNumber.
4904 // The corresponding HChange instructions are added in a phase that does
4905 // not have easy access to the local context.
4906 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
4907 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4908 RecordSafepointWithRegisters(
4909 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4910 __ movp(kScratchRegister, rax);
4912 __ movp(reg, kScratchRegister);
4916 void LCodeGen::DoSmiTag(LSmiTag* instr) {
4917 HChange* hchange = instr->hydrogen();
4918 Register input = ToRegister(instr->value());
4919 Register output = ToRegister(instr->result());
4920 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4921 hchange->value()->CheckFlag(HValue::kUint32)) {
4922 Condition is_smi = __ CheckUInteger32ValidSmiValue(input);
4923 DeoptimizeIf(NegateCondition(is_smi), instr, Deoptimizer::kOverflow);
4925 __ Integer32ToSmi(output, input);
4926 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4927 !hchange->value()->CheckFlag(HValue::kUint32)) {
4928 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
4933 void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4934 DCHECK(instr->value()->Equals(instr->result()));
4935 Register input = ToRegister(instr->value());
4936 if (instr->needs_check()) {
4937 Condition is_smi = __ CheckSmi(input);
4938 DeoptimizeIf(NegateCondition(is_smi), instr, Deoptimizer::kNotASmi);
4940 __ AssertSmi(input);
4942 __ SmiToInteger32(input, input);
4946 void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
4947 XMMRegister result_reg, NumberUntagDMode mode) {
4948 bool can_convert_undefined_to_nan =
4949 instr->hydrogen()->can_convert_undefined_to_nan();
4950 bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
4952 Label convert, load_smi, done;
4954 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4956 __ JumpIfSmi(input_reg, &load_smi, Label::kNear);
4958 // Heap number map check.
4959 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
4960 Heap::kHeapNumberMapRootIndex);
4962 // On x64 it is safe to load at heap number offset before evaluating the map
4963 // check, since all heap objects are at least two words long.
4964 __ movsd(result_reg, FieldOperand(input_reg, HeapNumber::kValueOffset));
4966 if (can_convert_undefined_to_nan) {
4967 __ j(not_equal, &convert, Label::kNear);
4969 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
4972 if (deoptimize_on_minus_zero) {
4973 XMMRegister xmm_scratch = double_scratch0();
4974 __ xorps(xmm_scratch, xmm_scratch);
4975 __ ucomisd(xmm_scratch, result_reg);
4976 __ j(not_equal, &done, Label::kNear);
4977 __ movmskpd(kScratchRegister, result_reg);
4978 __ testq(kScratchRegister, Immediate(1));
4979 DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero);
4981 __ jmp(&done, Label::kNear);
4983 if (can_convert_undefined_to_nan) {
4986 // Convert undefined (and hole) to NaN. Compute NaN as 0/0.
4987 __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex);
4988 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumberUndefined);
4990 __ pcmpeqd(result_reg, result_reg);
4991 __ jmp(&done, Label::kNear);
4994 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
4997 // Smi to XMM conversion
4999 __ SmiToInteger32(kScratchRegister, input_reg);
5000 __ Cvtlsi2sd(result_reg, kScratchRegister);
5005 void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr, Label* done) {
5006 Register input_reg = ToRegister(instr->value());
5008 if (instr->truncating()) {
5009 Label no_heap_number, check_bools, check_false;
5011 // Heap number map check.
5012 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
5013 Heap::kHeapNumberMapRootIndex);
5014 __ j(not_equal, &no_heap_number, Label::kNear);
5015 __ TruncateHeapNumberToI(input_reg, input_reg);
5018 __ bind(&no_heap_number);
5019 // Check for Oddballs. Undefined/False is converted to zero and True to one
5020 // for truncating conversions.
5021 __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex);
5022 __ j(not_equal, &check_bools, Label::kNear);
5023 __ Set(input_reg, 0);
5026 __ bind(&check_bools);
5027 __ CompareRoot(input_reg, Heap::kTrueValueRootIndex);
5028 __ j(not_equal, &check_false, Label::kNear);
5029 __ Set(input_reg, 1);
5032 __ bind(&check_false);
5033 __ CompareRoot(input_reg, Heap::kFalseValueRootIndex);
5034 DeoptimizeIf(not_equal, instr,
5035 Deoptimizer::kNotAHeapNumberUndefinedBoolean);
5036 __ Set(input_reg, 0);
5038 XMMRegister scratch = ToDoubleRegister(instr->temp());
5039 DCHECK(!scratch.is(xmm0));
5040 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
5041 Heap::kHeapNumberMapRootIndex);
5042 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
5043 __ movsd(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset));
5044 __ cvttsd2si(input_reg, xmm0);
5045 __ Cvtlsi2sd(scratch, input_reg);
5046 __ ucomisd(xmm0, scratch);
5047 DeoptimizeIf(not_equal, instr, Deoptimizer::kLostPrecision);
5048 DeoptimizeIf(parity_even, instr, Deoptimizer::kNaN);
5049 if (instr->hydrogen()->GetMinusZeroMode() == FAIL_ON_MINUS_ZERO) {
5050 __ testl(input_reg, input_reg);
5051 __ j(not_zero, done);
5052 __ movmskpd(input_reg, xmm0);
5053 __ andl(input_reg, Immediate(1));
5054 DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero);
5060 void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
5061 class DeferredTaggedToI final : public LDeferredCode {
5063 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
5064 : LDeferredCode(codegen), instr_(instr) { }
5065 void Generate() override { codegen()->DoDeferredTaggedToI(instr_, done()); }
5066 LInstruction* instr() override { return instr_; }
5072 LOperand* input = instr->value();
5073 DCHECK(input->IsRegister());
5074 DCHECK(input->Equals(instr->result()));
5075 Register input_reg = ToRegister(input);
5077 if (instr->hydrogen()->value()->representation().IsSmi()) {
5078 __ SmiToInteger32(input_reg, input_reg);
5080 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
5081 __ JumpIfNotSmi(input_reg, deferred->entry());
5082 __ SmiToInteger32(input_reg, input_reg);
5083 __ bind(deferred->exit());
5088 void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
5089 LOperand* input = instr->value();
5090 DCHECK(input->IsRegister());
5091 LOperand* result = instr->result();
5092 DCHECK(result->IsDoubleRegister());
5094 Register input_reg = ToRegister(input);
5095 XMMRegister result_reg = ToDoubleRegister(result);
5097 HValue* value = instr->hydrogen()->value();
5098 NumberUntagDMode mode = value->representation().IsSmi()
5099 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
5101 EmitNumberUntagD(instr, input_reg, result_reg, mode);
5105 void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
5106 LOperand* input = instr->value();
5107 DCHECK(input->IsDoubleRegister());
5108 LOperand* result = instr->result();
5109 DCHECK(result->IsRegister());
5111 XMMRegister input_reg = ToDoubleRegister(input);
5112 Register result_reg = ToRegister(result);
5114 if (instr->truncating()) {
5115 __ TruncateDoubleToI(result_reg, input_reg);
5117 Label lost_precision, is_nan, minus_zero, done;
5118 XMMRegister xmm_scratch = double_scratch0();
5119 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
5120 __ DoubleToI(result_reg, input_reg, xmm_scratch,
5121 instr->hydrogen()->GetMinusZeroMode(), &lost_precision,
5122 &is_nan, &minus_zero, dist);
5123 __ jmp(&done, dist);
5124 __ bind(&lost_precision);
5125 DeoptimizeIf(no_condition, instr, Deoptimizer::kLostPrecision);
5127 DeoptimizeIf(no_condition, instr, Deoptimizer::kNaN);
5128 __ bind(&minus_zero);
5129 DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero);
5135 void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
5136 LOperand* input = instr->value();
5137 DCHECK(input->IsDoubleRegister());
5138 LOperand* result = instr->result();
5139 DCHECK(result->IsRegister());
5141 XMMRegister input_reg = ToDoubleRegister(input);
5142 Register result_reg = ToRegister(result);
5144 Label lost_precision, is_nan, minus_zero, done;
5145 XMMRegister xmm_scratch = double_scratch0();
5146 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
5147 __ DoubleToI(result_reg, input_reg, xmm_scratch,
5148 instr->hydrogen()->GetMinusZeroMode(), &lost_precision, &is_nan,
5150 __ jmp(&done, dist);
5151 __ bind(&lost_precision);
5152 DeoptimizeIf(no_condition, instr, Deoptimizer::kLostPrecision);
5154 DeoptimizeIf(no_condition, instr, Deoptimizer::kNaN);
5155 __ bind(&minus_zero);
5156 DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero);
5158 __ Integer32ToSmi(result_reg, result_reg);
5159 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
5163 void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
5164 LOperand* input = instr->value();
5165 Condition cc = masm()->CheckSmi(ToRegister(input));
5166 DeoptimizeIf(NegateCondition(cc), instr, Deoptimizer::kNotASmi);
5170 void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
5171 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
5172 LOperand* input = instr->value();
5173 Condition cc = masm()->CheckSmi(ToRegister(input));
5174 DeoptimizeIf(cc, instr, Deoptimizer::kSmi);
5179 void LCodeGen::DoCheckArrayBufferNotNeutered(
5180 LCheckArrayBufferNotNeutered* instr) {
5181 Register view = ToRegister(instr->view());
5183 __ movp(kScratchRegister,
5184 FieldOperand(view, JSArrayBufferView::kBufferOffset));
5185 __ testb(FieldOperand(kScratchRegister, JSArrayBuffer::kBitFieldOffset),
5186 Immediate(1 << JSArrayBuffer::WasNeutered::kShift));
5187 DeoptimizeIf(not_zero, instr, Deoptimizer::kOutOfBounds);
5191 void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
5192 Register input = ToRegister(instr->value());
5194 __ movp(kScratchRegister, FieldOperand(input, HeapObject::kMapOffset));
5196 if (instr->hydrogen()->is_interval_check()) {
5199 instr->hydrogen()->GetCheckInterval(&first, &last);
5201 __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
5202 Immediate(static_cast<int8_t>(first)));
5204 // If there is only one type in the interval check for equality.
5205 if (first == last) {
5206 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongInstanceType);
5208 DeoptimizeIf(below, instr, Deoptimizer::kWrongInstanceType);
5209 // Omit check for the last type.
5210 if (last != LAST_TYPE) {
5211 __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
5212 Immediate(static_cast<int8_t>(last)));
5213 DeoptimizeIf(above, instr, Deoptimizer::kWrongInstanceType);
5219 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
5221 if (base::bits::IsPowerOfTwo32(mask)) {
5222 DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
5223 __ testb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
5225 DeoptimizeIf(tag == 0 ? not_zero : zero, instr,
5226 Deoptimizer::kWrongInstanceType);
5228 __ movzxbl(kScratchRegister,
5229 FieldOperand(kScratchRegister, Map::kInstanceTypeOffset));
5230 __ andb(kScratchRegister, Immediate(mask));
5231 __ cmpb(kScratchRegister, Immediate(tag));
5232 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongInstanceType);
5238 void LCodeGen::DoCheckValue(LCheckValue* instr) {
5239 Register reg = ToRegister(instr->value());
5240 __ Cmp(reg, instr->hydrogen()->object().handle());
5241 DeoptimizeIf(not_equal, instr, Deoptimizer::kValueMismatch);
5245 void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
5247 PushSafepointRegistersScope scope(this);
5250 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
5251 RecordSafepointWithRegisters(
5252 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
5254 __ testp(rax, Immediate(kSmiTagMask));
5256 DeoptimizeIf(zero, instr, Deoptimizer::kInstanceMigrationFailed);
5260 void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
5261 class DeferredCheckMaps final : public LDeferredCode {
5263 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
5264 : LDeferredCode(codegen), instr_(instr), object_(object) {
5265 SetExit(check_maps());
5267 void Generate() override {
5268 codegen()->DoDeferredInstanceMigration(instr_, object_);
5270 Label* check_maps() { return &check_maps_; }
5271 LInstruction* instr() override { return instr_; }
5279 if (instr->hydrogen()->IsStabilityCheck()) {
5280 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5281 for (int i = 0; i < maps->size(); ++i) {
5282 AddStabilityDependency(maps->at(i).handle());
5287 LOperand* input = instr->value();
5288 DCHECK(input->IsRegister());
5289 Register reg = ToRegister(input);
5291 DeferredCheckMaps* deferred = NULL;
5292 if (instr->hydrogen()->HasMigrationTarget()) {
5293 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5294 __ bind(deferred->check_maps());
5297 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5299 for (int i = 0; i < maps->size() - 1; i++) {
5300 Handle<Map> map = maps->at(i).handle();
5301 __ CompareMap(reg, map);
5302 __ j(equal, &success, Label::kNear);
5305 Handle<Map> map = maps->at(maps->size() - 1).handle();
5306 __ CompareMap(reg, map);
5307 if (instr->hydrogen()->HasMigrationTarget()) {
5308 __ j(not_equal, deferred->entry());
5310 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongMap);
5317 void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
5318 XMMRegister value_reg = ToDoubleRegister(instr->unclamped());
5319 XMMRegister xmm_scratch = double_scratch0();
5320 Register result_reg = ToRegister(instr->result());
5321 __ ClampDoubleToUint8(value_reg, xmm_scratch, result_reg);
5325 void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
5326 DCHECK(instr->unclamped()->Equals(instr->result()));
5327 Register value_reg = ToRegister(instr->result());
5328 __ ClampUint8(value_reg);
5332 void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
5333 DCHECK(instr->unclamped()->Equals(instr->result()));
5334 Register input_reg = ToRegister(instr->unclamped());
5335 XMMRegister temp_xmm_reg = ToDoubleRegister(instr->temp_xmm());
5336 XMMRegister xmm_scratch = double_scratch0();
5337 Label is_smi, done, heap_number;
5338 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
5339 __ JumpIfSmi(input_reg, &is_smi, dist);
5341 // Check for heap number
5342 __ Cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
5343 factory()->heap_number_map());
5344 __ j(equal, &heap_number, Label::kNear);
5346 // Check for undefined. Undefined is converted to zero for clamping
5348 __ Cmp(input_reg, factory()->undefined_value());
5349 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumberUndefined);
5350 __ xorl(input_reg, input_reg);
5351 __ jmp(&done, Label::kNear);
5354 __ bind(&heap_number);
5355 __ movsd(xmm_scratch, FieldOperand(input_reg, HeapNumber::kValueOffset));
5356 __ ClampDoubleToUint8(xmm_scratch, temp_xmm_reg, input_reg);
5357 __ jmp(&done, Label::kNear);
5361 __ SmiToInteger32(input_reg, input_reg);
5362 __ ClampUint8(input_reg);
5368 void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5369 XMMRegister value_reg = ToDoubleRegister(instr->value());
5370 Register result_reg = ToRegister(instr->result());
5371 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5372 __ movq(result_reg, value_reg);
5373 __ shrq(result_reg, Immediate(32));
5375 __ movd(result_reg, value_reg);
5380 void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5381 Register hi_reg = ToRegister(instr->hi());
5382 Register lo_reg = ToRegister(instr->lo());
5383 XMMRegister result_reg = ToDoubleRegister(instr->result());
5384 XMMRegister xmm_scratch = double_scratch0();
5385 __ movd(result_reg, hi_reg);
5386 __ psllq(result_reg, 32);
5387 __ movd(xmm_scratch, lo_reg);
5388 __ orps(result_reg, xmm_scratch);
5392 void LCodeGen::DoAllocate(LAllocate* instr) {
5393 class DeferredAllocate final : public LDeferredCode {
5395 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
5396 : LDeferredCode(codegen), instr_(instr) { }
5397 void Generate() override { codegen()->DoDeferredAllocate(instr_); }
5398 LInstruction* instr() override { return instr_; }
5404 DeferredAllocate* deferred =
5405 new(zone()) DeferredAllocate(this, instr);
5407 Register result = ToRegister(instr->result());
5408 Register temp = ToRegister(instr->temp());
5410 // Allocate memory for the object.
5411 AllocationFlags flags = TAG_OBJECT;
5412 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5413 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5415 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5416 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5417 flags = static_cast<AllocationFlags>(flags | PRETENURE);
5420 if (instr->size()->IsConstantOperand()) {
5421 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5422 if (size <= Page::kMaxRegularHeapObjectSize) {
5423 __ Allocate(size, result, temp, no_reg, deferred->entry(), flags);
5425 __ jmp(deferred->entry());
5428 Register size = ToRegister(instr->size());
5429 __ Allocate(size, result, temp, no_reg, deferred->entry(), flags);
5432 __ bind(deferred->exit());
5434 if (instr->hydrogen()->MustPrefillWithFiller()) {
5435 if (instr->size()->IsConstantOperand()) {
5436 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5437 __ movl(temp, Immediate((size / kPointerSize) - 1));
5439 temp = ToRegister(instr->size());
5440 __ sarp(temp, Immediate(kPointerSizeLog2));
5445 __ Move(FieldOperand(result, temp, times_pointer_size, 0),
5446 isolate()->factory()->one_pointer_filler_map());
5448 __ j(not_zero, &loop);
5453 void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
5454 Register result = ToRegister(instr->result());
5456 // TODO(3095996): Get rid of this. For now, we need to make the
5457 // result register contain a valid pointer because it is already
5458 // contained in the register pointer map.
5459 __ Move(result, Smi::FromInt(0));
5461 PushSafepointRegistersScope scope(this);
5462 if (instr->size()->IsRegister()) {
5463 Register size = ToRegister(instr->size());
5464 DCHECK(!size.is(result));
5465 __ Integer32ToSmi(size, size);
5468 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5469 __ Push(Smi::FromInt(size));
5473 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5474 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5475 flags = AllocateTargetSpace::update(flags, OLD_SPACE);
5477 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5479 __ Push(Smi::FromInt(flags));
5481 CallRuntimeFromDeferred(
5482 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
5483 __ StoreToSafepointRegisterSlot(result, rax);
5487 void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
5488 DCHECK(ToRegister(instr->value()).is(rax));
5490 CallRuntime(Runtime::kToFastProperties, 1, instr);
5494 void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
5495 DCHECK(ToRegister(instr->context()).is(rsi));
5497 // Registers will be used as follows:
5498 // rcx = literals array.
5499 // rbx = regexp literal.
5500 // rax = regexp literal clone.
5501 int literal_offset =
5502 FixedArray::OffsetOfElementAt(instr->hydrogen()->literal_index());
5503 __ Move(rcx, instr->hydrogen()->literals());
5504 __ movp(rbx, FieldOperand(rcx, literal_offset));
5505 __ CompareRoot(rbx, Heap::kUndefinedValueRootIndex);
5506 __ j(not_equal, &materialized, Label::kNear);
5508 // Create regexp literal using runtime function
5509 // Result will be in rax.
5511 __ Push(Smi::FromInt(instr->hydrogen()->literal_index()));
5512 __ Push(instr->hydrogen()->pattern());
5513 __ Push(instr->hydrogen()->flags());
5514 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
5517 __ bind(&materialized);
5518 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
5519 Label allocated, runtime_allocate;
5520 __ Allocate(size, rax, rcx, rdx, &runtime_allocate, TAG_OBJECT);
5521 __ jmp(&allocated, Label::kNear);
5523 __ bind(&runtime_allocate);
5525 __ Push(Smi::FromInt(size));
5526 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
5529 __ bind(&allocated);
5530 // Copy the content into the newly allocated memory.
5531 // (Unroll copy loop once for better throughput).
5532 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
5533 __ movp(rdx, FieldOperand(rbx, i));
5534 __ movp(rcx, FieldOperand(rbx, i + kPointerSize));
5535 __ movp(FieldOperand(rax, i), rdx);
5536 __ movp(FieldOperand(rax, i + kPointerSize), rcx);
5538 if ((size % (2 * kPointerSize)) != 0) {
5539 __ movp(rdx, FieldOperand(rbx, size - kPointerSize));
5540 __ movp(FieldOperand(rax, size - kPointerSize), rdx);
5545 void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
5546 DCHECK(ToRegister(instr->context()).is(rsi));
5547 // Use the fast case closure allocation code that allocates in new
5548 // space for nested functions that don't need literals cloning.
5549 bool pretenure = instr->hydrogen()->pretenure();
5550 if (!pretenure && instr->hydrogen()->has_no_literals()) {
5551 FastNewClosureStub stub(isolate(), instr->hydrogen()->language_mode(),
5552 instr->hydrogen()->kind());
5553 __ Move(rbx, instr->hydrogen()->shared_info());
5554 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5557 __ Push(instr->hydrogen()->shared_info());
5558 __ PushRoot(pretenure ? Heap::kTrueValueRootIndex :
5559 Heap::kFalseValueRootIndex);
5560 CallRuntime(Runtime::kNewClosure, 3, instr);
5565 void LCodeGen::DoTypeof(LTypeof* instr) {
5566 DCHECK(ToRegister(instr->context()).is(rsi));
5567 DCHECK(ToRegister(instr->value()).is(rbx));
5569 Register value_register = ToRegister(instr->value());
5570 __ JumpIfNotSmi(value_register, &do_call);
5571 __ Move(rax, isolate()->factory()->number_string());
5574 TypeofStub stub(isolate());
5575 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5580 void LCodeGen::EmitPushTaggedOperand(LOperand* operand) {
5581 DCHECK(!operand->IsDoubleRegister());
5582 if (operand->IsConstantOperand()) {
5583 __ Push(ToHandle(LConstantOperand::cast(operand)));
5584 } else if (operand->IsRegister()) {
5585 __ Push(ToRegister(operand));
5587 __ Push(ToOperand(operand));
5592 void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5593 Register input = ToRegister(instr->value());
5594 Condition final_branch_condition = EmitTypeofIs(instr, input);
5595 if (final_branch_condition != no_condition) {
5596 EmitBranch(instr, final_branch_condition);
5601 Condition LCodeGen::EmitTypeofIs(LTypeofIsAndBranch* instr, Register input) {
5602 Label* true_label = instr->TrueLabel(chunk_);
5603 Label* false_label = instr->FalseLabel(chunk_);
5604 Handle<String> type_name = instr->type_literal();
5605 int left_block = instr->TrueDestination(chunk_);
5606 int right_block = instr->FalseDestination(chunk_);
5607 int next_block = GetNextEmittedBlock();
5609 Label::Distance true_distance = left_block == next_block ? Label::kNear
5611 Label::Distance false_distance = right_block == next_block ? Label::kNear
5613 Condition final_branch_condition = no_condition;
5614 Factory* factory = isolate()->factory();
5615 if (String::Equals(type_name, factory->number_string())) {
5616 __ JumpIfSmi(input, true_label, true_distance);
5617 __ CompareRoot(FieldOperand(input, HeapObject::kMapOffset),
5618 Heap::kHeapNumberMapRootIndex);
5620 final_branch_condition = equal;
5622 } else if (String::Equals(type_name, factory->string_string())) {
5623 __ JumpIfSmi(input, false_label, false_distance);
5624 __ CmpObjectType(input, FIRST_NONSTRING_TYPE, input);
5625 final_branch_condition = below;
5627 } else if (String::Equals(type_name, factory->symbol_string())) {
5628 __ JumpIfSmi(input, false_label, false_distance);
5629 __ CmpObjectType(input, SYMBOL_TYPE, input);
5630 final_branch_condition = equal;
5632 } else if (String::Equals(type_name, factory->boolean_string())) {
5633 __ CompareRoot(input, Heap::kTrueValueRootIndex);
5634 __ j(equal, true_label, true_distance);
5635 __ CompareRoot(input, Heap::kFalseValueRootIndex);
5636 final_branch_condition = equal;
5638 } else if (String::Equals(type_name, factory->undefined_string())) {
5639 __ CompareRoot(input, Heap::kUndefinedValueRootIndex);
5640 __ j(equal, true_label, true_distance);
5641 __ JumpIfSmi(input, false_label, false_distance);
5642 // Check for undetectable objects => true.
5643 __ movp(input, FieldOperand(input, HeapObject::kMapOffset));
5644 __ testb(FieldOperand(input, Map::kBitFieldOffset),
5645 Immediate(1 << Map::kIsUndetectable));
5646 final_branch_condition = not_zero;
5648 } else if (String::Equals(type_name, factory->function_string())) {
5649 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5650 __ JumpIfSmi(input, false_label, false_distance);
5651 __ CmpObjectType(input, JS_FUNCTION_TYPE, input);
5652 __ j(equal, true_label, true_distance);
5653 __ CmpInstanceType(input, JS_FUNCTION_PROXY_TYPE);
5654 final_branch_condition = equal;
5656 } else if (String::Equals(type_name, factory->object_string())) {
5657 __ JumpIfSmi(input, false_label, false_distance);
5658 __ CompareRoot(input, Heap::kNullValueRootIndex);
5659 __ j(equal, true_label, true_distance);
5660 __ CmpObjectType(input, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE, input);
5661 __ j(below, false_label, false_distance);
5662 __ CmpInstanceType(input, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
5663 __ j(above, false_label, false_distance);
5664 // Check for undetectable objects => false.
5665 __ testb(FieldOperand(input, Map::kBitFieldOffset),
5666 Immediate(1 << Map::kIsUndetectable));
5667 final_branch_condition = zero;
5670 #define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
5671 } else if (String::Equals(type_name, factory->type##_string())) { \
5672 __ JumpIfSmi(input, false_label, false_distance); \
5673 __ movp(input, FieldOperand(input, HeapObject::kMapOffset)); \
5674 __ CompareRoot(input, Heap::k##Type##MapRootIndex); \
5675 final_branch_condition = equal;
5676 SIMD128_TYPES(SIMD128_TYPE)
5681 __ jmp(false_label, false_distance);
5684 return final_branch_condition;
5688 void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
5689 Register temp = ToRegister(instr->temp());
5691 EmitIsConstructCall(temp);
5692 EmitBranch(instr, equal);
5696 void LCodeGen::EmitIsConstructCall(Register temp) {
5697 // Get the frame pointer for the calling frame.
5698 __ movp(temp, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
5700 // Skip the arguments adaptor frame if it exists.
5701 Label check_frame_marker;
5702 __ Cmp(Operand(temp, StandardFrameConstants::kContextOffset),
5703 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
5704 __ j(not_equal, &check_frame_marker, Label::kNear);
5705 __ movp(temp, Operand(temp, StandardFrameConstants::kCallerFPOffset));
5707 // Check the marker in the calling frame.
5708 __ bind(&check_frame_marker);
5709 __ Cmp(Operand(temp, StandardFrameConstants::kMarkerOffset),
5710 Smi::FromInt(StackFrame::CONSTRUCT));
5714 void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5715 if (!info()->IsStub()) {
5716 // Ensure that we have enough space after the previous lazy-bailout
5717 // instruction for patching the code here.
5718 int current_pc = masm()->pc_offset();
5719 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5720 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5721 __ Nop(padding_size);
5724 last_lazy_deopt_pc_ = masm()->pc_offset();
5728 void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
5729 last_lazy_deopt_pc_ = masm()->pc_offset();
5730 DCHECK(instr->HasEnvironment());
5731 LEnvironment* env = instr->environment();
5732 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5733 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5737 void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
5738 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5739 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5740 // needed return address), even though the implementation of LAZY and EAGER is
5741 // now identical. When LAZY is eventually completely folded into EAGER, remove
5742 // the special case below.
5743 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5744 type = Deoptimizer::LAZY;
5746 DeoptimizeIf(no_condition, instr, instr->hydrogen()->reason(), type);
5750 void LCodeGen::DoDummy(LDummy* instr) {
5751 // Nothing to see here, move on!
5755 void LCodeGen::DoDummyUse(LDummyUse* instr) {
5756 // Nothing to see here, move on!
5760 void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
5761 PushSafepointRegistersScope scope(this);
5762 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
5763 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5764 RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0);
5765 DCHECK(instr->HasEnvironment());
5766 LEnvironment* env = instr->environment();
5767 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5771 void LCodeGen::DoStackCheck(LStackCheck* instr) {
5772 class DeferredStackCheck final : public LDeferredCode {
5774 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5775 : LDeferredCode(codegen), instr_(instr) { }
5776 void Generate() override { codegen()->DoDeferredStackCheck(instr_); }
5777 LInstruction* instr() override { return instr_; }
5780 LStackCheck* instr_;
5783 DCHECK(instr->HasEnvironment());
5784 LEnvironment* env = instr->environment();
5785 // There is no LLazyBailout instruction for stack-checks. We have to
5786 // prepare for lazy deoptimization explicitly here.
5787 if (instr->hydrogen()->is_function_entry()) {
5788 // Perform stack overflow check.
5790 __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
5791 __ j(above_equal, &done, Label::kNear);
5793 DCHECK(instr->context()->IsRegister());
5794 DCHECK(ToRegister(instr->context()).is(rsi));
5795 CallCode(isolate()->builtins()->StackCheck(),
5796 RelocInfo::CODE_TARGET,
5800 DCHECK(instr->hydrogen()->is_backwards_branch());
5801 // Perform stack overflow check if this goto needs it before jumping.
5802 DeferredStackCheck* deferred_stack_check =
5803 new(zone()) DeferredStackCheck(this, instr);
5804 __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
5805 __ j(below, deferred_stack_check->entry());
5806 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
5807 __ bind(instr->done_label());
5808 deferred_stack_check->SetExit(instr->done_label());
5809 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5810 // Don't record a deoptimization index for the safepoint here.
5811 // This will be done explicitly when emitting call and the safepoint in
5812 // the deferred code.
5817 void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
5818 // This is a pseudo-instruction that ensures that the environment here is
5819 // properly registered for deoptimization and records the assembler's PC
5821 LEnvironment* environment = instr->environment();
5823 // If the environment were already registered, we would have no way of
5824 // backpatching it with the spill slot operands.
5825 DCHECK(!environment->HasBeenRegistered());
5826 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
5828 GenerateOsrPrologue();
5832 void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
5833 DCHECK(ToRegister(instr->context()).is(rsi));
5835 Condition cc = masm()->CheckSmi(rax);
5836 DeoptimizeIf(cc, instr, Deoptimizer::kSmi);
5838 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
5839 __ CmpObjectType(rax, LAST_JS_PROXY_TYPE, rcx);
5840 DeoptimizeIf(below_equal, instr, Deoptimizer::kWrongInstanceType);
5842 Label use_cache, call_runtime;
5843 Register null_value = rdi;
5844 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
5845 __ CheckEnumCache(null_value, &call_runtime);
5847 __ movp(rax, FieldOperand(rax, HeapObject::kMapOffset));
5848 __ jmp(&use_cache, Label::kNear);
5850 // Get the set of properties to enumerate.
5851 __ bind(&call_runtime);
5853 CallRuntime(Runtime::kGetPropertyNamesFast, 1, instr);
5855 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
5856 Heap::kMetaMapRootIndex);
5857 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongMap);
5858 __ bind(&use_cache);
5862 void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5863 Register map = ToRegister(instr->map());
5864 Register result = ToRegister(instr->result());
5865 Label load_cache, done;
5866 __ EnumLength(result, map);
5867 __ Cmp(result, Smi::FromInt(0));
5868 __ j(not_equal, &load_cache, Label::kNear);
5869 __ LoadRoot(result, Heap::kEmptyFixedArrayRootIndex);
5870 __ jmp(&done, Label::kNear);
5871 __ bind(&load_cache);
5872 __ LoadInstanceDescriptors(map, result);
5874 FieldOperand(result, DescriptorArray::kEnumCacheOffset));
5876 FieldOperand(result, FixedArray::SizeFor(instr->idx())));
5878 Condition cc = masm()->CheckSmi(result);
5879 DeoptimizeIf(cc, instr, Deoptimizer::kNoCache);
5883 void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5884 Register object = ToRegister(instr->value());
5885 __ cmpp(ToRegister(instr->map()),
5886 FieldOperand(object, HeapObject::kMapOffset));
5887 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongMap);
5891 void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5894 PushSafepointRegistersScope scope(this);
5898 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
5899 RecordSafepointWithRegisters(
5900 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
5901 __ StoreToSafepointRegisterSlot(object, rax);
5905 void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
5906 class DeferredLoadMutableDouble final : public LDeferredCode {
5908 DeferredLoadMutableDouble(LCodeGen* codegen,
5909 LLoadFieldByIndex* instr,
5912 : LDeferredCode(codegen),
5917 void Generate() override {
5918 codegen()->DoDeferredLoadMutableDouble(instr_, object_, index_);
5920 LInstruction* instr() override { return instr_; }
5923 LLoadFieldByIndex* instr_;
5928 Register object = ToRegister(instr->object());
5929 Register index = ToRegister(instr->index());
5931 DeferredLoadMutableDouble* deferred;
5932 deferred = new(zone()) DeferredLoadMutableDouble(this, instr, object, index);
5934 Label out_of_object, done;
5935 __ Move(kScratchRegister, Smi::FromInt(1));
5936 __ testp(index, kScratchRegister);
5937 __ j(not_zero, deferred->entry());
5939 __ sarp(index, Immediate(1));
5941 __ SmiToInteger32(index, index);
5942 __ cmpl(index, Immediate(0));
5943 __ j(less, &out_of_object, Label::kNear);
5944 __ movp(object, FieldOperand(object,
5947 JSObject::kHeaderSize));
5948 __ jmp(&done, Label::kNear);
5950 __ bind(&out_of_object);
5951 __ movp(object, FieldOperand(object, JSObject::kPropertiesOffset));
5953 // Index is now equal to out of object property index plus 1.
5954 __ movp(object, FieldOperand(object,
5957 FixedArray::kHeaderSize - kPointerSize));
5958 __ bind(deferred->exit());
5963 void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
5964 Register context = ToRegister(instr->context());
5965 __ movp(Operand(rbp, StandardFrameConstants::kContextOffset), context);
5969 void LCodeGen::DoAllocateBlockContext(LAllocateBlockContext* instr) {
5970 Handle<ScopeInfo> scope_info = instr->scope_info();
5971 __ Push(scope_info);
5972 __ Push(ToRegister(instr->function()));
5973 CallRuntime(Runtime::kPushBlockContext, 2, instr);
5974 RecordSafepoint(Safepoint::kNoLazyDeopt);
5980 } // namespace internal
5983 #endif // V8_TARGET_ARCH_X64