lithium-codegen-x64.cc revision 109988c7ccb6f3fd1a58574fa3dfb88beaef6632
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.
4
5#if V8_TARGET_ARCH_X64
6
7#include "src/crankshaft/x64/lithium-codegen-x64.h"
8
9#include "src/base/bits.h"
10#include "src/code-factory.h"
11#include "src/code-stubs.h"
12#include "src/crankshaft/hydrogen-osr.h"
13#include "src/ic/ic.h"
14#include "src/ic/stub-cache.h"
15#include "src/profiler/cpu-profiler.h"
16
17namespace v8 {
18namespace internal {
19
20
21// When invoking builtins, we need to record the safepoint in the middle of
22// the invoke instruction sequence generated by the macro assembler.
23class SafepointGenerator final : public CallWrapper {
24 public:
25  SafepointGenerator(LCodeGen* codegen,
26                     LPointerMap* pointers,
27                     Safepoint::DeoptMode mode)
28      : codegen_(codegen),
29        pointers_(pointers),
30        deopt_mode_(mode) { }
31  virtual ~SafepointGenerator() {}
32
33  void BeforeCall(int call_size) const override {}
34
35  void AfterCall() const override {
36    codegen_->RecordSafepoint(pointers_, deopt_mode_);
37  }
38
39 private:
40  LCodeGen* codegen_;
41  LPointerMap* pointers_;
42  Safepoint::DeoptMode deopt_mode_;
43};
44
45
46#define __ masm()->
47
48bool LCodeGen::GenerateCode() {
49  LPhase phase("Z_Code generation", chunk());
50  DCHECK(is_unused());
51  status_ = GENERATING;
52
53  // Open a frame scope to indicate that there is a frame on the stack.  The
54  // MANUAL indicates that the scope shouldn't actually generate code to set up
55  // the frame (that is done in GeneratePrologue).
56  FrameScope frame_scope(masm_, StackFrame::MANUAL);
57
58  return GeneratePrologue() &&
59      GenerateBody() &&
60      GenerateDeferredCode() &&
61      GenerateJumpTable() &&
62      GenerateSafepointTable();
63}
64
65
66void LCodeGen::FinishCode(Handle<Code> code) {
67  DCHECK(is_done());
68  code->set_stack_slots(GetTotalFrameSlotCount());
69  code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
70  PopulateDeoptimizationData(code);
71}
72
73
74#ifdef _MSC_VER
75void LCodeGen::MakeSureStackPagesMapped(int offset) {
76  const int kPageSize = 4 * KB;
77  for (offset -= kPageSize; offset > 0; offset -= kPageSize) {
78    __ movp(Operand(rsp, offset), rax);
79  }
80}
81#endif
82
83
84void LCodeGen::SaveCallerDoubles() {
85  DCHECK(info()->saves_caller_doubles());
86  DCHECK(NeedsEagerFrame());
87  Comment(";;; Save clobbered callee double registers");
88  int count = 0;
89  BitVector* doubles = chunk()->allocated_double_registers();
90  BitVector::Iterator save_iterator(doubles);
91  while (!save_iterator.Done()) {
92    __ Movsd(MemOperand(rsp, count * kDoubleSize),
93             XMMRegister::from_code(save_iterator.Current()));
94    save_iterator.Advance();
95    count++;
96  }
97}
98
99
100void LCodeGen::RestoreCallerDoubles() {
101  DCHECK(info()->saves_caller_doubles());
102  DCHECK(NeedsEagerFrame());
103  Comment(";;; Restore clobbered callee double registers");
104  BitVector* doubles = chunk()->allocated_double_registers();
105  BitVector::Iterator save_iterator(doubles);
106  int count = 0;
107  while (!save_iterator.Done()) {
108    __ Movsd(XMMRegister::from_code(save_iterator.Current()),
109             MemOperand(rsp, count * kDoubleSize));
110    save_iterator.Advance();
111    count++;
112  }
113}
114
115
116bool LCodeGen::GeneratePrologue() {
117  DCHECK(is_generating());
118
119  if (info()->IsOptimizing()) {
120    ProfileEntryHookStub::MaybeCallEntryHook(masm_);
121  }
122
123  info()->set_prologue_offset(masm_->pc_offset());
124  if (NeedsEagerFrame()) {
125    DCHECK(!frame_is_built_);
126    frame_is_built_ = true;
127    if (info()->IsStub()) {
128      __ StubPrologue();
129    } else {
130      __ Prologue(info()->GeneratePreagedPrologue());
131    }
132  }
133
134  // Reserve space for the stack slots needed by the code.
135  int slots = GetStackSlotCount();
136  if (slots > 0) {
137    if (FLAG_debug_code) {
138      __ subp(rsp, Immediate(slots * kPointerSize));
139#ifdef _MSC_VER
140      MakeSureStackPagesMapped(slots * kPointerSize);
141#endif
142      __ Push(rax);
143      __ Set(rax, slots);
144      __ Set(kScratchRegister, kSlotsZapValue);
145      Label loop;
146      __ bind(&loop);
147      __ movp(MemOperand(rsp, rax, times_pointer_size, 0),
148              kScratchRegister);
149      __ decl(rax);
150      __ j(not_zero, &loop);
151      __ Pop(rax);
152    } else {
153      __ subp(rsp, Immediate(slots * kPointerSize));
154#ifdef _MSC_VER
155      MakeSureStackPagesMapped(slots * kPointerSize);
156#endif
157    }
158
159    if (info()->saves_caller_doubles()) {
160      SaveCallerDoubles();
161    }
162  }
163  return !is_aborted();
164}
165
166
167void LCodeGen::DoPrologue(LPrologue* instr) {
168  Comment(";;; Prologue begin");
169
170  // Possibly allocate a local context.
171  if (info_->num_heap_slots() > 0) {
172    Comment(";;; Allocate local context");
173    bool need_write_barrier = true;
174    // Argument to NewContext is the function, which is still in rdi.
175    int slots = info_->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
176    Safepoint::DeoptMode deopt_mode = Safepoint::kNoLazyDeopt;
177    if (info()->scope()->is_script_scope()) {
178      __ Push(rdi);
179      __ Push(info()->scope()->GetScopeInfo(info()->isolate()));
180      __ CallRuntime(Runtime::kNewScriptContext);
181      deopt_mode = Safepoint::kLazyDeopt;
182    } else if (slots <= FastNewContextStub::kMaximumSlots) {
183      FastNewContextStub stub(isolate(), slots);
184      __ CallStub(&stub);
185      // Result of FastNewContextStub is always in new space.
186      need_write_barrier = false;
187    } else {
188      __ Push(rdi);
189      __ CallRuntime(Runtime::kNewFunctionContext);
190    }
191    RecordSafepoint(deopt_mode);
192
193    // Context is returned in rax.  It replaces the context passed to us.
194    // It's saved in the stack and kept live in rsi.
195    __ movp(rsi, rax);
196    __ movp(Operand(rbp, StandardFrameConstants::kContextOffset), rax);
197
198    // Copy any necessary parameters into the context.
199    int num_parameters = scope()->num_parameters();
200    int first_parameter = scope()->has_this_declaration() ? -1 : 0;
201    for (int i = first_parameter; i < num_parameters; i++) {
202      Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
203      if (var->IsContextSlot()) {
204        int parameter_offset = StandardFrameConstants::kCallerSPOffset +
205            (num_parameters - 1 - i) * kPointerSize;
206        // Load parameter from stack.
207        __ movp(rax, Operand(rbp, parameter_offset));
208        // Store it in the context.
209        int context_offset = Context::SlotOffset(var->index());
210        __ movp(Operand(rsi, context_offset), rax);
211        // Update the write barrier. This clobbers rax and rbx.
212        if (need_write_barrier) {
213          __ RecordWriteContextSlot(rsi, context_offset, rax, rbx, kSaveFPRegs);
214        } else if (FLAG_debug_code) {
215          Label done;
216          __ JumpIfInNewSpace(rsi, rax, &done, Label::kNear);
217          __ Abort(kExpectedNewSpaceObject);
218          __ bind(&done);
219        }
220      }
221    }
222    Comment(";;; End allocate local context");
223  }
224
225  Comment(";;; Prologue end");
226}
227
228
229void LCodeGen::GenerateOsrPrologue() {
230  // Generate the OSR entry prologue at the first unknown OSR value, or if there
231  // are none, at the OSR entrypoint instruction.
232  if (osr_pc_offset_ >= 0) return;
233
234  osr_pc_offset_ = masm()->pc_offset();
235
236  // Adjust the frame size, subsuming the unoptimized frame into the
237  // optimized frame.
238  int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
239  DCHECK(slots >= 0);
240  __ subp(rsp, Immediate(slots * kPointerSize));
241}
242
243
244void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
245  if (instr->IsCall()) {
246    EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
247  }
248  if (!instr->IsLazyBailout() && !instr->IsGap()) {
249    safepoints_.BumpLastLazySafepointIndex();
250  }
251}
252
253
254void LCodeGen::GenerateBodyInstructionPost(LInstruction* instr) {
255  if (FLAG_debug_code && FLAG_enable_slow_asserts && instr->HasResult() &&
256      instr->hydrogen_value()->representation().IsInteger32() &&
257      instr->result()->IsRegister()) {
258    __ AssertZeroExtended(ToRegister(instr->result()));
259  }
260
261  if (instr->HasResult() && instr->MustSignExtendResult(chunk())) {
262    // We sign extend the dehoisted key at the definition point when the pointer
263    // size is 64-bit. For x32 port, we sign extend the dehoisted key at the use
264    // points and MustSignExtendResult is always false. We can't use
265    // STATIC_ASSERT here as the pointer size is 32-bit for x32.
266    DCHECK(kPointerSize == kInt64Size);
267    if (instr->result()->IsRegister()) {
268      Register result_reg = ToRegister(instr->result());
269      __ movsxlq(result_reg, result_reg);
270    } else {
271      // Sign extend the 32bit result in the stack slots.
272      DCHECK(instr->result()->IsStackSlot());
273      Operand src = ToOperand(instr->result());
274      __ movsxlq(kScratchRegister, src);
275      __ movq(src, kScratchRegister);
276    }
277  }
278}
279
280
281bool LCodeGen::GenerateJumpTable() {
282  if (jump_table_.length() == 0) return !is_aborted();
283
284  Label needs_frame;
285  Comment(";;; -------------------- Jump table --------------------");
286  for (int i = 0; i < jump_table_.length(); i++) {
287    Deoptimizer::JumpTableEntry* table_entry = &jump_table_[i];
288    __ bind(&table_entry->label);
289    Address entry = table_entry->address;
290    DeoptComment(table_entry->deopt_info);
291    if (table_entry->needs_frame) {
292      DCHECK(!info()->saves_caller_doubles());
293      __ Move(kScratchRegister, ExternalReference::ForDeoptEntry(entry));
294      __ call(&needs_frame);
295    } else {
296      if (info()->saves_caller_doubles()) {
297        DCHECK(info()->IsStub());
298        RestoreCallerDoubles();
299      }
300      __ call(entry, RelocInfo::RUNTIME_ENTRY);
301    }
302    info()->LogDeoptCallPosition(masm()->pc_offset(),
303                                 table_entry->deopt_info.inlining_id);
304  }
305
306  if (needs_frame.is_linked()) {
307    __ bind(&needs_frame);
308    /* stack layout
309       4: return address  <-- rsp
310       3: garbage
311       2: garbage
312       1: garbage
313       0: garbage
314    */
315    // Reserve space for context and stub marker.
316    __ subp(rsp, Immediate(2 * kPointerSize));
317    __ Push(MemOperand(rsp, 2 * kPointerSize));  // Copy return address.
318    __ Push(kScratchRegister);  // Save entry address for ret(0)
319
320    /* stack layout
321       4: return address
322       3: garbage
323       2: garbage
324       1: return address
325       0: entry address  <-- rsp
326    */
327
328    // Remember context pointer.
329    __ movp(kScratchRegister,
330            MemOperand(rbp, StandardFrameConstants::kContextOffset));
331    // Save context pointer into the stack frame.
332    __ movp(MemOperand(rsp, 3 * kPointerSize), kScratchRegister);
333
334    // Create a stack frame.
335    __ movp(MemOperand(rsp, 4 * kPointerSize), rbp);
336    __ leap(rbp, MemOperand(rsp, 4 * kPointerSize));
337
338    // This variant of deopt can only be used with stubs. Since we don't
339    // have a function pointer to install in the stack frame that we're
340    // building, install a special marker there instead.
341    DCHECK(info()->IsStub());
342    __ Move(MemOperand(rsp, 2 * kPointerSize), Smi::FromInt(StackFrame::STUB));
343
344    /* stack layout
345       4: old rbp
346       3: context pointer
347       2: stub marker
348       1: return address
349       0: entry address  <-- rsp
350    */
351    __ ret(0);
352  }
353
354  return !is_aborted();
355}
356
357
358bool LCodeGen::GenerateDeferredCode() {
359  DCHECK(is_generating());
360  if (deferred_.length() > 0) {
361    for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
362      LDeferredCode* code = deferred_[i];
363
364      HValue* value =
365          instructions_->at(code->instruction_index())->hydrogen_value();
366      RecordAndWritePosition(
367          chunk()->graph()->SourcePositionToScriptPosition(value->position()));
368
369      Comment(";;; <@%d,#%d> "
370              "-------------------- Deferred %s --------------------",
371              code->instruction_index(),
372              code->instr()->hydrogen_value()->id(),
373              code->instr()->Mnemonic());
374      __ bind(code->entry());
375      if (NeedsDeferredFrame()) {
376        Comment(";;; Build frame");
377        DCHECK(!frame_is_built_);
378        DCHECK(info()->IsStub());
379        frame_is_built_ = true;
380        // Build the frame in such a way that esi isn't trashed.
381        __ pushq(rbp);  // Caller's frame pointer.
382        __ Push(Operand(rbp, StandardFrameConstants::kContextOffset));
383        __ Push(Smi::FromInt(StackFrame::STUB));
384        __ leap(rbp, Operand(rsp, 2 * kPointerSize));
385        Comment(";;; Deferred code");
386      }
387      code->Generate();
388      if (NeedsDeferredFrame()) {
389        __ bind(code->done());
390        Comment(";;; Destroy frame");
391        DCHECK(frame_is_built_);
392        frame_is_built_ = false;
393        __ movp(rsp, rbp);
394        __ popq(rbp);
395      }
396      __ jmp(code->exit());
397    }
398  }
399
400  // Deferred code is the last part of the instruction sequence. Mark
401  // the generated code as done unless we bailed out.
402  if (!is_aborted()) status_ = DONE;
403  return !is_aborted();
404}
405
406
407bool LCodeGen::GenerateSafepointTable() {
408  DCHECK(is_done());
409  safepoints_.Emit(masm(), GetTotalFrameSlotCount());
410  return !is_aborted();
411}
412
413
414Register LCodeGen::ToRegister(int index) const {
415  return Register::from_code(index);
416}
417
418
419XMMRegister LCodeGen::ToDoubleRegister(int index) const {
420  return XMMRegister::from_code(index);
421}
422
423
424Register LCodeGen::ToRegister(LOperand* op) const {
425  DCHECK(op->IsRegister());
426  return ToRegister(op->index());
427}
428
429
430XMMRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
431  DCHECK(op->IsDoubleRegister());
432  return ToDoubleRegister(op->index());
433}
434
435
436bool LCodeGen::IsInteger32Constant(LConstantOperand* op) const {
437  return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
438}
439
440
441bool LCodeGen::IsExternalConstant(LConstantOperand* op) const {
442  return chunk_->LookupLiteralRepresentation(op).IsExternal();
443}
444
445
446bool LCodeGen::IsDehoistedKeyConstant(LConstantOperand* op) const {
447  return op->IsConstantOperand() &&
448      chunk_->IsDehoistedKey(chunk_->LookupConstant(op));
449}
450
451
452bool LCodeGen::IsSmiConstant(LConstantOperand* op) const {
453  return chunk_->LookupLiteralRepresentation(op).IsSmi();
454}
455
456
457int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
458  return ToRepresentation(op, Representation::Integer32());
459}
460
461
462int32_t LCodeGen::ToRepresentation(LConstantOperand* op,
463                                   const Representation& r) const {
464  HConstant* constant = chunk_->LookupConstant(op);
465  int32_t value = constant->Integer32Value();
466  if (r.IsInteger32()) return value;
467  DCHECK(SmiValuesAre31Bits() && r.IsSmiOrTagged());
468  return static_cast<int32_t>(reinterpret_cast<intptr_t>(Smi::FromInt(value)));
469}
470
471
472Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
473  HConstant* constant = chunk_->LookupConstant(op);
474  return Smi::FromInt(constant->Integer32Value());
475}
476
477
478double LCodeGen::ToDouble(LConstantOperand* op) const {
479  HConstant* constant = chunk_->LookupConstant(op);
480  DCHECK(constant->HasDoubleValue());
481  return constant->DoubleValue();
482}
483
484
485ExternalReference LCodeGen::ToExternalReference(LConstantOperand* op) const {
486  HConstant* constant = chunk_->LookupConstant(op);
487  DCHECK(constant->HasExternalReferenceValue());
488  return constant->ExternalReferenceValue();
489}
490
491
492Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
493  HConstant* constant = chunk_->LookupConstant(op);
494  DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
495  return constant->handle(isolate());
496}
497
498
499static int ArgumentsOffsetWithoutFrame(int index) {
500  DCHECK(index < 0);
501  return -(index + 1) * kPointerSize + kPCOnStackSize;
502}
503
504
505Operand LCodeGen::ToOperand(LOperand* op) const {
506  // Does not handle registers. In X64 assembler, plain registers are not
507  // representable as an Operand.
508  DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
509  if (NeedsEagerFrame()) {
510    return Operand(rbp, FrameSlotToFPOffset(op->index()));
511  } else {
512    // Retrieve parameter without eager stack-frame relative to the
513    // stack-pointer.
514    return Operand(rsp, ArgumentsOffsetWithoutFrame(op->index()));
515  }
516}
517
518
519void LCodeGen::WriteTranslation(LEnvironment* environment,
520                                Translation* translation) {
521  if (environment == NULL) return;
522
523  // The translation includes one command per value in the environment.
524  int translation_size = environment->translation_size();
525
526  WriteTranslation(environment->outer(), translation);
527  WriteTranslationFrame(environment, translation);
528
529  int object_index = 0;
530  int dematerialized_index = 0;
531  for (int i = 0; i < translation_size; ++i) {
532    LOperand* value = environment->values()->at(i);
533    AddToTranslation(
534        environment, translation, value, environment->HasTaggedValueAt(i),
535        environment->HasUint32ValueAt(i), &object_index, &dematerialized_index);
536  }
537}
538
539
540void LCodeGen::AddToTranslation(LEnvironment* environment,
541                                Translation* translation,
542                                LOperand* op,
543                                bool is_tagged,
544                                bool is_uint32,
545                                int* object_index_pointer,
546                                int* dematerialized_index_pointer) {
547  if (op == LEnvironment::materialization_marker()) {
548    int object_index = (*object_index_pointer)++;
549    if (environment->ObjectIsDuplicateAt(object_index)) {
550      int dupe_of = environment->ObjectDuplicateOfAt(object_index);
551      translation->DuplicateObject(dupe_of);
552      return;
553    }
554    int object_length = environment->ObjectLengthAt(object_index);
555    if (environment->ObjectIsArgumentsAt(object_index)) {
556      translation->BeginArgumentsObject(object_length);
557    } else {
558      translation->BeginCapturedObject(object_length);
559    }
560    int dematerialized_index = *dematerialized_index_pointer;
561    int env_offset = environment->translation_size() + dematerialized_index;
562    *dematerialized_index_pointer += object_length;
563    for (int i = 0; i < object_length; ++i) {
564      LOperand* value = environment->values()->at(env_offset + i);
565      AddToTranslation(environment,
566                       translation,
567                       value,
568                       environment->HasTaggedValueAt(env_offset + i),
569                       environment->HasUint32ValueAt(env_offset + i),
570                       object_index_pointer,
571                       dematerialized_index_pointer);
572    }
573    return;
574  }
575
576  if (op->IsStackSlot()) {
577    int index = op->index();
578    if (is_tagged) {
579      translation->StoreStackSlot(index);
580    } else if (is_uint32) {
581      translation->StoreUint32StackSlot(index);
582    } else {
583      translation->StoreInt32StackSlot(index);
584    }
585  } else if (op->IsDoubleStackSlot()) {
586    int index = op->index();
587    translation->StoreDoubleStackSlot(index);
588  } else if (op->IsRegister()) {
589    Register reg = ToRegister(op);
590    if (is_tagged) {
591      translation->StoreRegister(reg);
592    } else if (is_uint32) {
593      translation->StoreUint32Register(reg);
594    } else {
595      translation->StoreInt32Register(reg);
596    }
597  } else if (op->IsDoubleRegister()) {
598    XMMRegister reg = ToDoubleRegister(op);
599    translation->StoreDoubleRegister(reg);
600  } else if (op->IsConstantOperand()) {
601    HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
602    int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
603    translation->StoreLiteral(src_index);
604  } else {
605    UNREACHABLE();
606  }
607}
608
609
610void LCodeGen::CallCodeGeneric(Handle<Code> code,
611                               RelocInfo::Mode mode,
612                               LInstruction* instr,
613                               SafepointMode safepoint_mode,
614                               int argc) {
615  DCHECK(instr != NULL);
616  __ call(code, mode);
617  RecordSafepointWithLazyDeopt(instr, safepoint_mode, argc);
618
619  // Signal that we don't inline smi code before these stubs in the
620  // optimizing code generator.
621  if (code->kind() == Code::BINARY_OP_IC ||
622      code->kind() == Code::COMPARE_IC) {
623    __ nop();
624  }
625}
626
627
628void LCodeGen::CallCode(Handle<Code> code,
629                        RelocInfo::Mode mode,
630                        LInstruction* instr) {
631  CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT, 0);
632}
633
634
635void LCodeGen::CallRuntime(const Runtime::Function* function,
636                           int num_arguments,
637                           LInstruction* instr,
638                           SaveFPRegsMode save_doubles) {
639  DCHECK(instr != NULL);
640  DCHECK(instr->HasPointerMap());
641
642  __ CallRuntime(function, num_arguments, save_doubles);
643
644  RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT, 0);
645}
646
647
648void LCodeGen::LoadContextFromDeferred(LOperand* context) {
649  if (context->IsRegister()) {
650    if (!ToRegister(context).is(rsi)) {
651      __ movp(rsi, ToRegister(context));
652    }
653  } else if (context->IsStackSlot()) {
654    __ movp(rsi, ToOperand(context));
655  } else if (context->IsConstantOperand()) {
656    HConstant* constant =
657        chunk_->LookupConstant(LConstantOperand::cast(context));
658    __ Move(rsi, Handle<Object>::cast(constant->handle(isolate())));
659  } else {
660    UNREACHABLE();
661  }
662}
663
664
665
666void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
667                                       int argc,
668                                       LInstruction* instr,
669                                       LOperand* context) {
670  LoadContextFromDeferred(context);
671
672  __ CallRuntimeSaveDoubles(id);
673  RecordSafepointWithRegisters(
674      instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
675}
676
677
678void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
679                                                    Safepoint::DeoptMode mode) {
680  environment->set_has_been_used();
681  if (!environment->HasBeenRegistered()) {
682    // Physical stack frame layout:
683    // -x ............. -4  0 ..................................... y
684    // [incoming arguments] [spill slots] [pushed outgoing arguments]
685
686    // Layout of the environment:
687    // 0 ..................................................... size-1
688    // [parameters] [locals] [expression stack including arguments]
689
690    // Layout of the translation:
691    // 0 ........................................................ size - 1 + 4
692    // [expression stack including arguments] [locals] [4 words] [parameters]
693    // |>------------  translation_size ------------<|
694
695    int frame_count = 0;
696    int jsframe_count = 0;
697    for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
698      ++frame_count;
699      if (e->frame_type() == JS_FUNCTION) {
700        ++jsframe_count;
701      }
702    }
703    Translation translation(&translations_, frame_count, jsframe_count, zone());
704    WriteTranslation(environment, &translation);
705    int deoptimization_index = deoptimizations_.length();
706    int pc_offset = masm()->pc_offset();
707    environment->Register(deoptimization_index,
708                          translation.index(),
709                          (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
710    deoptimizations_.Add(environment, environment->zone());
711  }
712}
713
714
715void LCodeGen::DeoptimizeIf(Condition cc, LInstruction* instr,
716                            Deoptimizer::DeoptReason deopt_reason,
717                            Deoptimizer::BailoutType bailout_type) {
718  LEnvironment* environment = instr->environment();
719  RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
720  DCHECK(environment->HasBeenRegistered());
721  int id = environment->deoptimization_index();
722  Address entry =
723      Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
724  if (entry == NULL) {
725    Abort(kBailoutWasNotPrepared);
726    return;
727  }
728
729  if (DeoptEveryNTimes()) {
730    ExternalReference count = ExternalReference::stress_deopt_count(isolate());
731    Label no_deopt;
732    __ pushfq();
733    __ pushq(rax);
734    Operand count_operand = masm()->ExternalOperand(count, kScratchRegister);
735    __ movl(rax, count_operand);
736    __ subl(rax, Immediate(1));
737    __ j(not_zero, &no_deopt, Label::kNear);
738    if (FLAG_trap_on_deopt) __ int3();
739    __ movl(rax, Immediate(FLAG_deopt_every_n_times));
740    __ movl(count_operand, rax);
741    __ popq(rax);
742    __ popfq();
743    DCHECK(frame_is_built_);
744    __ call(entry, RelocInfo::RUNTIME_ENTRY);
745    __ bind(&no_deopt);
746    __ movl(count_operand, rax);
747    __ popq(rax);
748    __ popfq();
749  }
750
751  if (info()->ShouldTrapOnDeopt()) {
752    Label done;
753    if (cc != no_condition) {
754      __ j(NegateCondition(cc), &done, Label::kNear);
755    }
756    __ int3();
757    __ bind(&done);
758  }
759
760  Deoptimizer::DeoptInfo deopt_info = MakeDeoptInfo(instr, deopt_reason);
761
762  DCHECK(info()->IsStub() || frame_is_built_);
763  // Go through jump table if we need to handle condition, build frame, or
764  // restore caller doubles.
765  if (cc == no_condition && frame_is_built_ &&
766      !info()->saves_caller_doubles()) {
767    DeoptComment(deopt_info);
768    __ call(entry, RelocInfo::RUNTIME_ENTRY);
769    info()->LogDeoptCallPosition(masm()->pc_offset(), deopt_info.inlining_id);
770  } else {
771    Deoptimizer::JumpTableEntry table_entry(entry, deopt_info, bailout_type,
772                                            !frame_is_built_);
773    // We often have several deopts to the same entry, reuse the last
774    // jump entry if this is the case.
775    if (FLAG_trace_deopt || isolate()->cpu_profiler()->is_profiling() ||
776        jump_table_.is_empty() ||
777        !table_entry.IsEquivalentTo(jump_table_.last())) {
778      jump_table_.Add(table_entry, zone());
779    }
780    if (cc == no_condition) {
781      __ jmp(&jump_table_.last().label);
782    } else {
783      __ j(cc, &jump_table_.last().label);
784    }
785  }
786}
787
788
789void LCodeGen::DeoptimizeIf(Condition cc, LInstruction* instr,
790                            Deoptimizer::DeoptReason deopt_reason) {
791  Deoptimizer::BailoutType bailout_type = info()->IsStub()
792      ? Deoptimizer::LAZY
793      : Deoptimizer::EAGER;
794  DeoptimizeIf(cc, instr, deopt_reason, bailout_type);
795}
796
797
798void LCodeGen::RecordSafepointWithLazyDeopt(
799    LInstruction* instr, SafepointMode safepoint_mode, int argc) {
800  if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
801    RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
802  } else {
803    DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS);
804    RecordSafepointWithRegisters(
805        instr->pointer_map(), argc, Safepoint::kLazyDeopt);
806  }
807}
808
809
810void LCodeGen::RecordSafepoint(
811    LPointerMap* pointers,
812    Safepoint::Kind kind,
813    int arguments,
814    Safepoint::DeoptMode deopt_mode) {
815  DCHECK(kind == expected_safepoint_kind_);
816
817  const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
818
819  Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
820      kind, arguments, deopt_mode);
821  for (int i = 0; i < operands->length(); i++) {
822    LOperand* pointer = operands->at(i);
823    if (pointer->IsStackSlot()) {
824      safepoint.DefinePointerSlot(pointer->index(), zone());
825    } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
826      safepoint.DefinePointerRegister(ToRegister(pointer), zone());
827    }
828  }
829}
830
831
832void LCodeGen::RecordSafepoint(LPointerMap* pointers,
833                               Safepoint::DeoptMode deopt_mode) {
834  RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
835}
836
837
838void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
839  LPointerMap empty_pointers(zone());
840  RecordSafepoint(&empty_pointers, deopt_mode);
841}
842
843
844void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
845                                            int arguments,
846                                            Safepoint::DeoptMode deopt_mode) {
847  RecordSafepoint(pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
848}
849
850
851void LCodeGen::RecordAndWritePosition(int position) {
852  if (position == RelocInfo::kNoPosition) return;
853  masm()->positions_recorder()->RecordPosition(position);
854  masm()->positions_recorder()->WriteRecordedPositions();
855}
856
857
858static const char* LabelType(LLabel* label) {
859  if (label->is_loop_header()) return " (loop header)";
860  if (label->is_osr_entry()) return " (OSR entry)";
861  return "";
862}
863
864
865void LCodeGen::DoLabel(LLabel* label) {
866  Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
867          current_instruction_,
868          label->hydrogen_value()->id(),
869          label->block_id(),
870          LabelType(label));
871  __ bind(label->label());
872  current_block_ = label->block_id();
873  DoGap(label);
874}
875
876
877void LCodeGen::DoParallelMove(LParallelMove* move) {
878  resolver_.Resolve(move);
879}
880
881
882void LCodeGen::DoGap(LGap* gap) {
883  for (int i = LGap::FIRST_INNER_POSITION;
884       i <= LGap::LAST_INNER_POSITION;
885       i++) {
886    LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
887    LParallelMove* move = gap->GetParallelMove(inner_pos);
888    if (move != NULL) DoParallelMove(move);
889  }
890}
891
892
893void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
894  DoGap(instr);
895}
896
897
898void LCodeGen::DoParameter(LParameter* instr) {
899  // Nothing to do.
900}
901
902
903void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
904  GenerateOsrPrologue();
905}
906
907
908void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
909  Register dividend = ToRegister(instr->dividend());
910  int32_t divisor = instr->divisor();
911  DCHECK(dividend.is(ToRegister(instr->result())));
912
913  // Theoretically, a variation of the branch-free code for integer division by
914  // a power of 2 (calculating the remainder via an additional multiplication
915  // (which gets simplified to an 'and') and subtraction) should be faster, and
916  // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
917  // indicate that positive dividends are heavily favored, so the branching
918  // version performs better.
919  HMod* hmod = instr->hydrogen();
920  int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
921  Label dividend_is_not_negative, done;
922  if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
923    __ testl(dividend, dividend);
924    __ j(not_sign, &dividend_is_not_negative, Label::kNear);
925    // Note that this is correct even for kMinInt operands.
926    __ negl(dividend);
927    __ andl(dividend, Immediate(mask));
928    __ negl(dividend);
929    if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
930      DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
931    }
932    __ jmp(&done, Label::kNear);
933  }
934
935  __ bind(&dividend_is_not_negative);
936  __ andl(dividend, Immediate(mask));
937  __ bind(&done);
938}
939
940
941void LCodeGen::DoModByConstI(LModByConstI* instr) {
942  Register dividend = ToRegister(instr->dividend());
943  int32_t divisor = instr->divisor();
944  DCHECK(ToRegister(instr->result()).is(rax));
945
946  if (divisor == 0) {
947    DeoptimizeIf(no_condition, instr, Deoptimizer::kDivisionByZero);
948    return;
949  }
950
951  __ TruncatingDiv(dividend, Abs(divisor));
952  __ imull(rdx, rdx, Immediate(Abs(divisor)));
953  __ movl(rax, dividend);
954  __ subl(rax, rdx);
955
956  // Check for negative zero.
957  HMod* hmod = instr->hydrogen();
958  if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
959    Label remainder_not_zero;
960    __ j(not_zero, &remainder_not_zero, Label::kNear);
961    __ cmpl(dividend, Immediate(0));
962    DeoptimizeIf(less, instr, Deoptimizer::kMinusZero);
963    __ bind(&remainder_not_zero);
964  }
965}
966
967
968void LCodeGen::DoModI(LModI* instr) {
969  HMod* hmod = instr->hydrogen();
970
971  Register left_reg = ToRegister(instr->left());
972  DCHECK(left_reg.is(rax));
973  Register right_reg = ToRegister(instr->right());
974  DCHECK(!right_reg.is(rax));
975  DCHECK(!right_reg.is(rdx));
976  Register result_reg = ToRegister(instr->result());
977  DCHECK(result_reg.is(rdx));
978
979  Label done;
980  // Check for x % 0, idiv would signal a divide error. We have to
981  // deopt in this case because we can't return a NaN.
982  if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
983    __ testl(right_reg, right_reg);
984    DeoptimizeIf(zero, instr, Deoptimizer::kDivisionByZero);
985  }
986
987  // Check for kMinInt % -1, idiv would signal a divide error. We
988  // have to deopt if we care about -0, because we can't return that.
989  if (hmod->CheckFlag(HValue::kCanOverflow)) {
990    Label no_overflow_possible;
991    __ cmpl(left_reg, Immediate(kMinInt));
992    __ j(not_zero, &no_overflow_possible, Label::kNear);
993    __ cmpl(right_reg, Immediate(-1));
994    if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
995      DeoptimizeIf(equal, instr, Deoptimizer::kMinusZero);
996    } else {
997      __ j(not_equal, &no_overflow_possible, Label::kNear);
998      __ Set(result_reg, 0);
999      __ jmp(&done, Label::kNear);
1000    }
1001    __ bind(&no_overflow_possible);
1002  }
1003
1004  // Sign extend dividend in eax into edx:eax, since we are using only the low
1005  // 32 bits of the values.
1006  __ cdq();
1007
1008  // If we care about -0, test if the dividend is <0 and the result is 0.
1009  if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1010    Label positive_left;
1011    __ testl(left_reg, left_reg);
1012    __ j(not_sign, &positive_left, Label::kNear);
1013    __ idivl(right_reg);
1014    __ testl(result_reg, result_reg);
1015    DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1016    __ jmp(&done, Label::kNear);
1017    __ bind(&positive_left);
1018  }
1019  __ idivl(right_reg);
1020  __ bind(&done);
1021}
1022
1023
1024void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
1025  Register dividend = ToRegister(instr->dividend());
1026  int32_t divisor = instr->divisor();
1027  DCHECK(dividend.is(ToRegister(instr->result())));
1028
1029  // If the divisor is positive, things are easy: There can be no deopts and we
1030  // can simply do an arithmetic right shift.
1031  if (divisor == 1) return;
1032  int32_t shift = WhichPowerOf2Abs(divisor);
1033  if (divisor > 1) {
1034    __ sarl(dividend, Immediate(shift));
1035    return;
1036  }
1037
1038  // If the divisor is negative, we have to negate and handle edge cases.
1039  __ negl(dividend);
1040  if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1041    DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1042  }
1043
1044  // Dividing by -1 is basically negation, unless we overflow.
1045  if (divisor == -1) {
1046    if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1047      DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1048    }
1049    return;
1050  }
1051
1052  // If the negation could not overflow, simply shifting is OK.
1053  if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1054    __ sarl(dividend, Immediate(shift));
1055    return;
1056  }
1057
1058  Label not_kmin_int, done;
1059  __ j(no_overflow, &not_kmin_int, Label::kNear);
1060  __ movl(dividend, Immediate(kMinInt / divisor));
1061  __ jmp(&done, Label::kNear);
1062  __ bind(&not_kmin_int);
1063  __ sarl(dividend, Immediate(shift));
1064  __ bind(&done);
1065}
1066
1067
1068void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
1069  Register dividend = ToRegister(instr->dividend());
1070  int32_t divisor = instr->divisor();
1071  DCHECK(ToRegister(instr->result()).is(rdx));
1072
1073  if (divisor == 0) {
1074    DeoptimizeIf(no_condition, instr, Deoptimizer::kDivisionByZero);
1075    return;
1076  }
1077
1078  // Check for (0 / -x) that will produce negative zero.
1079  HMathFloorOfDiv* hdiv = instr->hydrogen();
1080  if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1081    __ testl(dividend, dividend);
1082    DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1083  }
1084
1085  // Easy case: We need no dynamic check for the dividend and the flooring
1086  // division is the same as the truncating division.
1087  if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
1088      (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
1089    __ TruncatingDiv(dividend, Abs(divisor));
1090    if (divisor < 0) __ negl(rdx);
1091    return;
1092  }
1093
1094  // In the general case we may need to adjust before and after the truncating
1095  // division to get a flooring division.
1096  Register temp = ToRegister(instr->temp3());
1097  DCHECK(!temp.is(dividend) && !temp.is(rax) && !temp.is(rdx));
1098  Label needs_adjustment, done;
1099  __ cmpl(dividend, Immediate(0));
1100  __ j(divisor > 0 ? less : greater, &needs_adjustment, Label::kNear);
1101  __ TruncatingDiv(dividend, Abs(divisor));
1102  if (divisor < 0) __ negl(rdx);
1103  __ jmp(&done, Label::kNear);
1104  __ bind(&needs_adjustment);
1105  __ leal(temp, Operand(dividend, divisor > 0 ? 1 : -1));
1106  __ TruncatingDiv(temp, Abs(divisor));
1107  if (divisor < 0) __ negl(rdx);
1108  __ decl(rdx);
1109  __ bind(&done);
1110}
1111
1112
1113// TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
1114void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
1115  HBinaryOperation* hdiv = instr->hydrogen();
1116  Register dividend = ToRegister(instr->dividend());
1117  Register divisor = ToRegister(instr->divisor());
1118  Register remainder = ToRegister(instr->temp());
1119  Register result = ToRegister(instr->result());
1120  DCHECK(dividend.is(rax));
1121  DCHECK(remainder.is(rdx));
1122  DCHECK(result.is(rax));
1123  DCHECK(!divisor.is(rax));
1124  DCHECK(!divisor.is(rdx));
1125
1126  // Check for x / 0.
1127  if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1128    __ testl(divisor, divisor);
1129    DeoptimizeIf(zero, instr, Deoptimizer::kDivisionByZero);
1130  }
1131
1132  // Check for (0 / -x) that will produce negative zero.
1133  if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1134    Label dividend_not_zero;
1135    __ testl(dividend, dividend);
1136    __ j(not_zero, &dividend_not_zero, Label::kNear);
1137    __ testl(divisor, divisor);
1138    DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero);
1139    __ bind(&dividend_not_zero);
1140  }
1141
1142  // Check for (kMinInt / -1).
1143  if (hdiv->CheckFlag(HValue::kCanOverflow)) {
1144    Label dividend_not_min_int;
1145    __ cmpl(dividend, Immediate(kMinInt));
1146    __ j(not_zero, &dividend_not_min_int, Label::kNear);
1147    __ cmpl(divisor, Immediate(-1));
1148    DeoptimizeIf(zero, instr, Deoptimizer::kOverflow);
1149    __ bind(&dividend_not_min_int);
1150  }
1151
1152  // Sign extend to rdx (= remainder).
1153  __ cdq();
1154  __ idivl(divisor);
1155
1156  Label done;
1157  __ testl(remainder, remainder);
1158  __ j(zero, &done, Label::kNear);
1159  __ xorl(remainder, divisor);
1160  __ sarl(remainder, Immediate(31));
1161  __ addl(result, remainder);
1162  __ bind(&done);
1163}
1164
1165
1166void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
1167  Register dividend = ToRegister(instr->dividend());
1168  int32_t divisor = instr->divisor();
1169  Register result = ToRegister(instr->result());
1170  DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
1171  DCHECK(!result.is(dividend));
1172
1173  // Check for (0 / -x) that will produce negative zero.
1174  HDiv* hdiv = instr->hydrogen();
1175  if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1176    __ testl(dividend, dividend);
1177    DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1178  }
1179  // Check for (kMinInt / -1).
1180  if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
1181    __ cmpl(dividend, Immediate(kMinInt));
1182    DeoptimizeIf(zero, instr, Deoptimizer::kOverflow);
1183  }
1184  // Deoptimize if remainder will not be 0.
1185  if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1186      divisor != 1 && divisor != -1) {
1187    int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1188    __ testl(dividend, Immediate(mask));
1189    DeoptimizeIf(not_zero, instr, Deoptimizer::kLostPrecision);
1190  }
1191  __ Move(result, dividend);
1192  int32_t shift = WhichPowerOf2Abs(divisor);
1193  if (shift > 0) {
1194    // The arithmetic shift is always OK, the 'if' is an optimization only.
1195    if (shift > 1) __ sarl(result, Immediate(31));
1196    __ shrl(result, Immediate(32 - shift));
1197    __ addl(result, dividend);
1198    __ sarl(result, Immediate(shift));
1199  }
1200  if (divisor < 0) __ negl(result);
1201}
1202
1203
1204void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
1205  Register dividend = ToRegister(instr->dividend());
1206  int32_t divisor = instr->divisor();
1207  DCHECK(ToRegister(instr->result()).is(rdx));
1208
1209  if (divisor == 0) {
1210    DeoptimizeIf(no_condition, instr, Deoptimizer::kDivisionByZero);
1211    return;
1212  }
1213
1214  // Check for (0 / -x) that will produce negative zero.
1215  HDiv* hdiv = instr->hydrogen();
1216  if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1217    __ testl(dividend, dividend);
1218    DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1219  }
1220
1221  __ TruncatingDiv(dividend, Abs(divisor));
1222  if (divisor < 0) __ negl(rdx);
1223
1224  if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1225    __ movl(rax, rdx);
1226    __ imull(rax, rax, Immediate(divisor));
1227    __ subl(rax, dividend);
1228    DeoptimizeIf(not_equal, instr, Deoptimizer::kLostPrecision);
1229  }
1230}
1231
1232
1233// TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
1234void LCodeGen::DoDivI(LDivI* instr) {
1235  HBinaryOperation* hdiv = instr->hydrogen();
1236  Register dividend = ToRegister(instr->dividend());
1237  Register divisor = ToRegister(instr->divisor());
1238  Register remainder = ToRegister(instr->temp());
1239  DCHECK(dividend.is(rax));
1240  DCHECK(remainder.is(rdx));
1241  DCHECK(ToRegister(instr->result()).is(rax));
1242  DCHECK(!divisor.is(rax));
1243  DCHECK(!divisor.is(rdx));
1244
1245  // Check for x / 0.
1246  if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1247    __ testl(divisor, divisor);
1248    DeoptimizeIf(zero, instr, Deoptimizer::kDivisionByZero);
1249  }
1250
1251  // Check for (0 / -x) that will produce negative zero.
1252  if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1253    Label dividend_not_zero;
1254    __ testl(dividend, dividend);
1255    __ j(not_zero, &dividend_not_zero, Label::kNear);
1256    __ testl(divisor, divisor);
1257    DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero);
1258    __ bind(&dividend_not_zero);
1259  }
1260
1261  // Check for (kMinInt / -1).
1262  if (hdiv->CheckFlag(HValue::kCanOverflow)) {
1263    Label dividend_not_min_int;
1264    __ cmpl(dividend, Immediate(kMinInt));
1265    __ j(not_zero, &dividend_not_min_int, Label::kNear);
1266    __ cmpl(divisor, Immediate(-1));
1267    DeoptimizeIf(zero, instr, Deoptimizer::kOverflow);
1268    __ bind(&dividend_not_min_int);
1269  }
1270
1271  // Sign extend to rdx (= remainder).
1272  __ cdq();
1273  __ idivl(divisor);
1274
1275  if (!hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1276    // Deoptimize if remainder is not 0.
1277    __ testl(remainder, remainder);
1278    DeoptimizeIf(not_zero, instr, Deoptimizer::kLostPrecision);
1279  }
1280}
1281
1282
1283void LCodeGen::DoMulI(LMulI* instr) {
1284  Register left = ToRegister(instr->left());
1285  LOperand* right = instr->right();
1286
1287  if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1288    if (instr->hydrogen_value()->representation().IsSmi()) {
1289      __ movp(kScratchRegister, left);
1290    } else {
1291      __ movl(kScratchRegister, left);
1292    }
1293  }
1294
1295  bool can_overflow =
1296      instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1297  if (right->IsConstantOperand()) {
1298    int32_t right_value = ToInteger32(LConstantOperand::cast(right));
1299    if (right_value == -1) {
1300      __ negl(left);
1301    } else if (right_value == 0) {
1302      __ xorl(left, left);
1303    } else if (right_value == 2) {
1304      __ addl(left, left);
1305    } else if (!can_overflow) {
1306      // If the multiplication is known to not overflow, we
1307      // can use operations that don't set the overflow flag
1308      // correctly.
1309      switch (right_value) {
1310        case 1:
1311          // Do nothing.
1312          break;
1313        case 3:
1314          __ leal(left, Operand(left, left, times_2, 0));
1315          break;
1316        case 4:
1317          __ shll(left, Immediate(2));
1318          break;
1319        case 5:
1320          __ leal(left, Operand(left, left, times_4, 0));
1321          break;
1322        case 8:
1323          __ shll(left, Immediate(3));
1324          break;
1325        case 9:
1326          __ leal(left, Operand(left, left, times_8, 0));
1327          break;
1328        case 16:
1329          __ shll(left, Immediate(4));
1330          break;
1331        default:
1332          __ imull(left, left, Immediate(right_value));
1333          break;
1334      }
1335    } else {
1336      __ imull(left, left, Immediate(right_value));
1337    }
1338  } else if (right->IsStackSlot()) {
1339    if (instr->hydrogen_value()->representation().IsSmi()) {
1340      __ SmiToInteger64(left, left);
1341      __ imulp(left, ToOperand(right));
1342    } else {
1343      __ imull(left, ToOperand(right));
1344    }
1345  } else {
1346    if (instr->hydrogen_value()->representation().IsSmi()) {
1347      __ SmiToInteger64(left, left);
1348      __ imulp(left, ToRegister(right));
1349    } else {
1350      __ imull(left, ToRegister(right));
1351    }
1352  }
1353
1354  if (can_overflow) {
1355    DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1356  }
1357
1358  if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1359    // Bail out if the result is supposed to be negative zero.
1360    Label done;
1361    if (instr->hydrogen_value()->representation().IsSmi()) {
1362      __ testp(left, left);
1363    } else {
1364      __ testl(left, left);
1365    }
1366    __ j(not_zero, &done, Label::kNear);
1367    if (right->IsConstantOperand()) {
1368      // Constant can't be represented as 32-bit Smi due to immediate size
1369      // limit.
1370      DCHECK(SmiValuesAre32Bits()
1371          ? !instr->hydrogen_value()->representation().IsSmi()
1372          : SmiValuesAre31Bits());
1373      if (ToInteger32(LConstantOperand::cast(right)) < 0) {
1374        DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero);
1375      } else if (ToInteger32(LConstantOperand::cast(right)) == 0) {
1376        __ cmpl(kScratchRegister, Immediate(0));
1377        DeoptimizeIf(less, instr, Deoptimizer::kMinusZero);
1378      }
1379    } else if (right->IsStackSlot()) {
1380      if (instr->hydrogen_value()->representation().IsSmi()) {
1381        __ orp(kScratchRegister, ToOperand(right));
1382      } else {
1383        __ orl(kScratchRegister, ToOperand(right));
1384      }
1385      DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero);
1386    } else {
1387      // Test the non-zero operand for negative sign.
1388      if (instr->hydrogen_value()->representation().IsSmi()) {
1389        __ orp(kScratchRegister, ToRegister(right));
1390      } else {
1391        __ orl(kScratchRegister, ToRegister(right));
1392      }
1393      DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero);
1394    }
1395    __ bind(&done);
1396  }
1397}
1398
1399
1400void LCodeGen::DoBitI(LBitI* instr) {
1401  LOperand* left = instr->left();
1402  LOperand* right = instr->right();
1403  DCHECK(left->Equals(instr->result()));
1404  DCHECK(left->IsRegister());
1405
1406  if (right->IsConstantOperand()) {
1407    int32_t right_operand =
1408        ToRepresentation(LConstantOperand::cast(right),
1409                         instr->hydrogen()->right()->representation());
1410    switch (instr->op()) {
1411      case Token::BIT_AND:
1412        __ andl(ToRegister(left), Immediate(right_operand));
1413        break;
1414      case Token::BIT_OR:
1415        __ orl(ToRegister(left), Immediate(right_operand));
1416        break;
1417      case Token::BIT_XOR:
1418        if (right_operand == int32_t(~0)) {
1419          __ notl(ToRegister(left));
1420        } else {
1421          __ xorl(ToRegister(left), Immediate(right_operand));
1422        }
1423        break;
1424      default:
1425        UNREACHABLE();
1426        break;
1427    }
1428  } else if (right->IsStackSlot()) {
1429    switch (instr->op()) {
1430      case Token::BIT_AND:
1431        if (instr->IsInteger32()) {
1432          __ andl(ToRegister(left), ToOperand(right));
1433        } else {
1434          __ andp(ToRegister(left), ToOperand(right));
1435        }
1436        break;
1437      case Token::BIT_OR:
1438        if (instr->IsInteger32()) {
1439          __ orl(ToRegister(left), ToOperand(right));
1440        } else {
1441          __ orp(ToRegister(left), ToOperand(right));
1442        }
1443        break;
1444      case Token::BIT_XOR:
1445        if (instr->IsInteger32()) {
1446          __ xorl(ToRegister(left), ToOperand(right));
1447        } else {
1448          __ xorp(ToRegister(left), ToOperand(right));
1449        }
1450        break;
1451      default:
1452        UNREACHABLE();
1453        break;
1454    }
1455  } else {
1456    DCHECK(right->IsRegister());
1457    switch (instr->op()) {
1458      case Token::BIT_AND:
1459        if (instr->IsInteger32()) {
1460          __ andl(ToRegister(left), ToRegister(right));
1461        } else {
1462          __ andp(ToRegister(left), ToRegister(right));
1463        }
1464        break;
1465      case Token::BIT_OR:
1466        if (instr->IsInteger32()) {
1467          __ orl(ToRegister(left), ToRegister(right));
1468        } else {
1469          __ orp(ToRegister(left), ToRegister(right));
1470        }
1471        break;
1472      case Token::BIT_XOR:
1473        if (instr->IsInteger32()) {
1474          __ xorl(ToRegister(left), ToRegister(right));
1475        } else {
1476          __ xorp(ToRegister(left), ToRegister(right));
1477        }
1478        break;
1479      default:
1480        UNREACHABLE();
1481        break;
1482    }
1483  }
1484}
1485
1486
1487void LCodeGen::DoShiftI(LShiftI* instr) {
1488  LOperand* left = instr->left();
1489  LOperand* right = instr->right();
1490  DCHECK(left->Equals(instr->result()));
1491  DCHECK(left->IsRegister());
1492  if (right->IsRegister()) {
1493    DCHECK(ToRegister(right).is(rcx));
1494
1495    switch (instr->op()) {
1496      case Token::ROR:
1497        __ rorl_cl(ToRegister(left));
1498        break;
1499      case Token::SAR:
1500        __ sarl_cl(ToRegister(left));
1501        break;
1502      case Token::SHR:
1503        __ shrl_cl(ToRegister(left));
1504        if (instr->can_deopt()) {
1505          __ testl(ToRegister(left), ToRegister(left));
1506          DeoptimizeIf(negative, instr, Deoptimizer::kNegativeValue);
1507        }
1508        break;
1509      case Token::SHL:
1510        __ shll_cl(ToRegister(left));
1511        break;
1512      default:
1513        UNREACHABLE();
1514        break;
1515    }
1516  } else {
1517    int32_t value = ToInteger32(LConstantOperand::cast(right));
1518    uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1519    switch (instr->op()) {
1520      case Token::ROR:
1521        if (shift_count != 0) {
1522          __ rorl(ToRegister(left), Immediate(shift_count));
1523        }
1524        break;
1525      case Token::SAR:
1526        if (shift_count != 0) {
1527          __ sarl(ToRegister(left), Immediate(shift_count));
1528        }
1529        break;
1530      case Token::SHR:
1531        if (shift_count != 0) {
1532          __ shrl(ToRegister(left), Immediate(shift_count));
1533        } else if (instr->can_deopt()) {
1534          __ testl(ToRegister(left), ToRegister(left));
1535          DeoptimizeIf(negative, instr, Deoptimizer::kNegativeValue);
1536        }
1537        break;
1538      case Token::SHL:
1539        if (shift_count != 0) {
1540          if (instr->hydrogen_value()->representation().IsSmi()) {
1541            if (SmiValuesAre32Bits()) {
1542              __ shlp(ToRegister(left), Immediate(shift_count));
1543            } else {
1544              DCHECK(SmiValuesAre31Bits());
1545              if (instr->can_deopt()) {
1546                if (shift_count != 1) {
1547                  __ shll(ToRegister(left), Immediate(shift_count - 1));
1548                }
1549                __ Integer32ToSmi(ToRegister(left), ToRegister(left));
1550                DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1551              } else {
1552                __ shll(ToRegister(left), Immediate(shift_count));
1553              }
1554            }
1555          } else {
1556            __ shll(ToRegister(left), Immediate(shift_count));
1557          }
1558        }
1559        break;
1560      default:
1561        UNREACHABLE();
1562        break;
1563    }
1564  }
1565}
1566
1567
1568void LCodeGen::DoSubI(LSubI* instr) {
1569  LOperand* left = instr->left();
1570  LOperand* right = instr->right();
1571  DCHECK(left->Equals(instr->result()));
1572
1573  if (right->IsConstantOperand()) {
1574    int32_t right_operand =
1575        ToRepresentation(LConstantOperand::cast(right),
1576                         instr->hydrogen()->right()->representation());
1577    __ subl(ToRegister(left), Immediate(right_operand));
1578  } else if (right->IsRegister()) {
1579    if (instr->hydrogen_value()->representation().IsSmi()) {
1580      __ subp(ToRegister(left), ToRegister(right));
1581    } else {
1582      __ subl(ToRegister(left), ToRegister(right));
1583    }
1584  } else {
1585    if (instr->hydrogen_value()->representation().IsSmi()) {
1586      __ subp(ToRegister(left), ToOperand(right));
1587    } else {
1588      __ subl(ToRegister(left), ToOperand(right));
1589    }
1590  }
1591
1592  if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1593    DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1594  }
1595}
1596
1597
1598void LCodeGen::DoConstantI(LConstantI* instr) {
1599  Register dst = ToRegister(instr->result());
1600  if (instr->value() == 0) {
1601    __ xorl(dst, dst);
1602  } else {
1603    __ movl(dst, Immediate(instr->value()));
1604  }
1605}
1606
1607
1608void LCodeGen::DoConstantS(LConstantS* instr) {
1609  __ Move(ToRegister(instr->result()), instr->value());
1610}
1611
1612
1613void LCodeGen::DoConstantD(LConstantD* instr) {
1614  __ Move(ToDoubleRegister(instr->result()), instr->bits());
1615}
1616
1617
1618void LCodeGen::DoConstantE(LConstantE* instr) {
1619  __ LoadAddress(ToRegister(instr->result()), instr->value());
1620}
1621
1622
1623void LCodeGen::DoConstantT(LConstantT* instr) {
1624  Handle<Object> object = instr->value(isolate());
1625  AllowDeferredHandleDereference smi_check;
1626  __ Move(ToRegister(instr->result()), object);
1627}
1628
1629
1630Operand LCodeGen::BuildSeqStringOperand(Register string,
1631                                        LOperand* index,
1632                                        String::Encoding encoding) {
1633  if (index->IsConstantOperand()) {
1634    int offset = ToInteger32(LConstantOperand::cast(index));
1635    if (encoding == String::TWO_BYTE_ENCODING) {
1636      offset *= kUC16Size;
1637    }
1638    STATIC_ASSERT(kCharSize == 1);
1639    return FieldOperand(string, SeqString::kHeaderSize + offset);
1640  }
1641  return FieldOperand(
1642      string, ToRegister(index),
1643      encoding == String::ONE_BYTE_ENCODING ? times_1 : times_2,
1644      SeqString::kHeaderSize);
1645}
1646
1647
1648void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
1649  String::Encoding encoding = instr->hydrogen()->encoding();
1650  Register result = ToRegister(instr->result());
1651  Register string = ToRegister(instr->string());
1652
1653  if (FLAG_debug_code) {
1654    __ Push(string);
1655    __ movp(string, FieldOperand(string, HeapObject::kMapOffset));
1656    __ movzxbp(string, FieldOperand(string, Map::kInstanceTypeOffset));
1657
1658    __ andb(string, Immediate(kStringRepresentationMask | kStringEncodingMask));
1659    static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1660    static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1661    __ cmpp(string, Immediate(encoding == String::ONE_BYTE_ENCODING
1662                              ? one_byte_seq_type : two_byte_seq_type));
1663    __ Check(equal, kUnexpectedStringType);
1664    __ Pop(string);
1665  }
1666
1667  Operand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1668  if (encoding == String::ONE_BYTE_ENCODING) {
1669    __ movzxbl(result, operand);
1670  } else {
1671    __ movzxwl(result, operand);
1672  }
1673}
1674
1675
1676void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
1677  String::Encoding encoding = instr->hydrogen()->encoding();
1678  Register string = ToRegister(instr->string());
1679
1680  if (FLAG_debug_code) {
1681    Register value = ToRegister(instr->value());
1682    Register index = ToRegister(instr->index());
1683    static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1684    static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1685    int encoding_mask =
1686        instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
1687        ? one_byte_seq_type : two_byte_seq_type;
1688    __ EmitSeqStringSetCharCheck(string, index, value, encoding_mask);
1689  }
1690
1691  Operand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1692  if (instr->value()->IsConstantOperand()) {
1693    int value = ToInteger32(LConstantOperand::cast(instr->value()));
1694    DCHECK_LE(0, value);
1695    if (encoding == String::ONE_BYTE_ENCODING) {
1696      DCHECK_LE(value, String::kMaxOneByteCharCode);
1697      __ movb(operand, Immediate(value));
1698    } else {
1699      DCHECK_LE(value, String::kMaxUtf16CodeUnit);
1700      __ movw(operand, Immediate(value));
1701    }
1702  } else {
1703    Register value = ToRegister(instr->value());
1704    if (encoding == String::ONE_BYTE_ENCODING) {
1705      __ movb(operand, value);
1706    } else {
1707      __ movw(operand, value);
1708    }
1709  }
1710}
1711
1712
1713void LCodeGen::DoAddI(LAddI* instr) {
1714  LOperand* left = instr->left();
1715  LOperand* right = instr->right();
1716
1717  Representation target_rep = instr->hydrogen()->representation();
1718  bool is_p = target_rep.IsSmi() || target_rep.IsExternal();
1719
1720  if (LAddI::UseLea(instr->hydrogen()) && !left->Equals(instr->result())) {
1721    if (right->IsConstantOperand()) {
1722      // No support for smi-immediates for 32-bit SMI.
1723      DCHECK(SmiValuesAre32Bits() ? !target_rep.IsSmi() : SmiValuesAre31Bits());
1724      int32_t offset =
1725          ToRepresentation(LConstantOperand::cast(right),
1726                           instr->hydrogen()->right()->representation());
1727      if (is_p) {
1728        __ leap(ToRegister(instr->result()),
1729                MemOperand(ToRegister(left), offset));
1730      } else {
1731        __ leal(ToRegister(instr->result()),
1732                MemOperand(ToRegister(left), offset));
1733      }
1734    } else {
1735      Operand address(ToRegister(left), ToRegister(right), times_1, 0);
1736      if (is_p) {
1737        __ leap(ToRegister(instr->result()), address);
1738      } else {
1739        __ leal(ToRegister(instr->result()), address);
1740      }
1741    }
1742  } else {
1743    if (right->IsConstantOperand()) {
1744      // No support for smi-immediates for 32-bit SMI.
1745      DCHECK(SmiValuesAre32Bits() ? !target_rep.IsSmi() : SmiValuesAre31Bits());
1746      int32_t right_operand =
1747          ToRepresentation(LConstantOperand::cast(right),
1748                           instr->hydrogen()->right()->representation());
1749      if (is_p) {
1750        __ addp(ToRegister(left), Immediate(right_operand));
1751      } else {
1752        __ addl(ToRegister(left), Immediate(right_operand));
1753      }
1754    } else if (right->IsRegister()) {
1755      if (is_p) {
1756        __ addp(ToRegister(left), ToRegister(right));
1757      } else {
1758        __ addl(ToRegister(left), ToRegister(right));
1759      }
1760    } else {
1761      if (is_p) {
1762        __ addp(ToRegister(left), ToOperand(right));
1763      } else {
1764        __ addl(ToRegister(left), ToOperand(right));
1765      }
1766    }
1767    if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1768      DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1769    }
1770  }
1771}
1772
1773
1774void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
1775  LOperand* left = instr->left();
1776  LOperand* right = instr->right();
1777  DCHECK(left->Equals(instr->result()));
1778  HMathMinMax::Operation operation = instr->hydrogen()->operation();
1779  if (instr->hydrogen()->representation().IsSmiOrInteger32()) {
1780    Label return_left;
1781    Condition condition = (operation == HMathMinMax::kMathMin)
1782        ? less_equal
1783        : greater_equal;
1784    Register left_reg = ToRegister(left);
1785    if (right->IsConstantOperand()) {
1786      Immediate right_imm = Immediate(
1787          ToRepresentation(LConstantOperand::cast(right),
1788                           instr->hydrogen()->right()->representation()));
1789      DCHECK(SmiValuesAre32Bits()
1790          ? !instr->hydrogen()->representation().IsSmi()
1791          : SmiValuesAre31Bits());
1792      __ cmpl(left_reg, right_imm);
1793      __ j(condition, &return_left, Label::kNear);
1794      __ movp(left_reg, right_imm);
1795    } else if (right->IsRegister()) {
1796      Register right_reg = ToRegister(right);
1797      if (instr->hydrogen_value()->representation().IsSmi()) {
1798        __ cmpp(left_reg, right_reg);
1799      } else {
1800        __ cmpl(left_reg, right_reg);
1801      }
1802      __ j(condition, &return_left, Label::kNear);
1803      __ movp(left_reg, right_reg);
1804    } else {
1805      Operand right_op = ToOperand(right);
1806      if (instr->hydrogen_value()->representation().IsSmi()) {
1807        __ cmpp(left_reg, right_op);
1808      } else {
1809        __ cmpl(left_reg, right_op);
1810      }
1811      __ j(condition, &return_left, Label::kNear);
1812      __ movp(left_reg, right_op);
1813    }
1814    __ bind(&return_left);
1815  } else {
1816    DCHECK(instr->hydrogen()->representation().IsDouble());
1817    Label not_nan, distinct, return_left, return_right;
1818    Condition condition = (operation == HMathMinMax::kMathMin) ? below : above;
1819    XMMRegister left_reg = ToDoubleRegister(left);
1820    XMMRegister right_reg = ToDoubleRegister(right);
1821    __ Ucomisd(left_reg, right_reg);
1822    __ j(parity_odd, &not_nan, Label::kNear);  // Both are not NaN.
1823
1824    // One of the numbers is NaN. Find which one and return it.
1825    __ Ucomisd(left_reg, left_reg);
1826    __ j(parity_even, &return_left, Label::kNear);  // left is NaN.
1827    __ jmp(&return_right, Label::kNear);            // right is NaN.
1828
1829    __ bind(&not_nan);
1830    __ j(not_equal, &distinct, Label::kNear);  // left != right.
1831
1832    // left == right
1833    XMMRegister xmm_scratch = double_scratch0();
1834    __ Xorpd(xmm_scratch, xmm_scratch);
1835    __ Ucomisd(left_reg, xmm_scratch);
1836    __ j(not_equal, &return_left, Label::kNear);  // left == right != 0.
1837
1838    // At this point, both left and right are either +0 or -0.
1839    if (operation == HMathMinMax::kMathMin) {
1840      __ Orpd(left_reg, right_reg);
1841    } else {
1842      __ Andpd(left_reg, right_reg);
1843    }
1844    __ jmp(&return_left, Label::kNear);
1845
1846    __ bind(&distinct);
1847    __ j(condition, &return_left, Label::kNear);
1848
1849    __ bind(&return_right);
1850    __ Movapd(left_reg, right_reg);
1851
1852    __ bind(&return_left);
1853  }
1854}
1855
1856
1857void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
1858  XMMRegister left = ToDoubleRegister(instr->left());
1859  XMMRegister right = ToDoubleRegister(instr->right());
1860  XMMRegister result = ToDoubleRegister(instr->result());
1861  switch (instr->op()) {
1862    case Token::ADD:
1863      if (CpuFeatures::IsSupported(AVX)) {
1864        CpuFeatureScope scope(masm(), AVX);
1865        __ vaddsd(result, left, right);
1866      } else {
1867        DCHECK(result.is(left));
1868        __ addsd(left, right);
1869      }
1870      break;
1871    case Token::SUB:
1872      if (CpuFeatures::IsSupported(AVX)) {
1873        CpuFeatureScope scope(masm(), AVX);
1874        __ vsubsd(result, left, right);
1875      } else {
1876        DCHECK(result.is(left));
1877        __ subsd(left, right);
1878      }
1879       break;
1880    case Token::MUL:
1881      if (CpuFeatures::IsSupported(AVX)) {
1882        CpuFeatureScope scope(masm(), AVX);
1883        __ vmulsd(result, left, right);
1884      } else {
1885        DCHECK(result.is(left));
1886        __ mulsd(left, right);
1887      }
1888      break;
1889    case Token::DIV:
1890      if (CpuFeatures::IsSupported(AVX)) {
1891        CpuFeatureScope scope(masm(), AVX);
1892        __ vdivsd(result, left, right);
1893      } else {
1894        DCHECK(result.is(left));
1895        __ divsd(left, right);
1896      }
1897      // Don't delete this mov. It may improve performance on some CPUs,
1898      // when there is a (v)mulsd depending on the result
1899      __ Movapd(result, result);
1900      break;
1901    case Token::MOD: {
1902      XMMRegister xmm_scratch = double_scratch0();
1903      __ PrepareCallCFunction(2);
1904      __ Movapd(xmm_scratch, left);
1905      DCHECK(right.is(xmm1));
1906      __ CallCFunction(
1907          ExternalReference::mod_two_doubles_operation(isolate()), 2);
1908      __ Movapd(result, xmm_scratch);
1909      break;
1910    }
1911    default:
1912      UNREACHABLE();
1913      break;
1914  }
1915}
1916
1917
1918void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
1919  DCHECK(ToRegister(instr->context()).is(rsi));
1920  DCHECK(ToRegister(instr->left()).is(rdx));
1921  DCHECK(ToRegister(instr->right()).is(rax));
1922  DCHECK(ToRegister(instr->result()).is(rax));
1923
1924  Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), instr->op()).code();
1925  CallCode(code, RelocInfo::CODE_TARGET, instr);
1926}
1927
1928
1929template<class InstrType>
1930void LCodeGen::EmitBranch(InstrType instr, Condition cc) {
1931  int left_block = instr->TrueDestination(chunk_);
1932  int right_block = instr->FalseDestination(chunk_);
1933
1934  int next_block = GetNextEmittedBlock();
1935
1936  if (right_block == left_block || cc == no_condition) {
1937    EmitGoto(left_block);
1938  } else if (left_block == next_block) {
1939    __ j(NegateCondition(cc), chunk_->GetAssemblyLabel(right_block));
1940  } else if (right_block == next_block) {
1941    __ j(cc, chunk_->GetAssemblyLabel(left_block));
1942  } else {
1943    __ j(cc, chunk_->GetAssemblyLabel(left_block));
1944    if (cc != always) {
1945      __ jmp(chunk_->GetAssemblyLabel(right_block));
1946    }
1947  }
1948}
1949
1950
1951template <class InstrType>
1952void LCodeGen::EmitTrueBranch(InstrType instr, Condition cc) {
1953  int true_block = instr->TrueDestination(chunk_);
1954  __ j(cc, chunk_->GetAssemblyLabel(true_block));
1955}
1956
1957
1958template <class InstrType>
1959void LCodeGen::EmitFalseBranch(InstrType instr, Condition cc) {
1960  int false_block = instr->FalseDestination(chunk_);
1961  __ j(cc, chunk_->GetAssemblyLabel(false_block));
1962}
1963
1964
1965void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
1966  __ int3();
1967}
1968
1969
1970void LCodeGen::DoBranch(LBranch* instr) {
1971  Representation r = instr->hydrogen()->value()->representation();
1972  if (r.IsInteger32()) {
1973    DCHECK(!info()->IsStub());
1974    Register reg = ToRegister(instr->value());
1975    __ testl(reg, reg);
1976    EmitBranch(instr, not_zero);
1977  } else if (r.IsSmi()) {
1978    DCHECK(!info()->IsStub());
1979    Register reg = ToRegister(instr->value());
1980    __ testp(reg, reg);
1981    EmitBranch(instr, not_zero);
1982  } else if (r.IsDouble()) {
1983    DCHECK(!info()->IsStub());
1984    XMMRegister reg = ToDoubleRegister(instr->value());
1985    XMMRegister xmm_scratch = double_scratch0();
1986    __ Xorpd(xmm_scratch, xmm_scratch);
1987    __ Ucomisd(reg, xmm_scratch);
1988    EmitBranch(instr, not_equal);
1989  } else {
1990    DCHECK(r.IsTagged());
1991    Register reg = ToRegister(instr->value());
1992    HType type = instr->hydrogen()->value()->type();
1993    if (type.IsBoolean()) {
1994      DCHECK(!info()->IsStub());
1995      __ CompareRoot(reg, Heap::kTrueValueRootIndex);
1996      EmitBranch(instr, equal);
1997    } else if (type.IsSmi()) {
1998      DCHECK(!info()->IsStub());
1999      __ SmiCompare(reg, Smi::FromInt(0));
2000      EmitBranch(instr, not_equal);
2001    } else if (type.IsJSArray()) {
2002      DCHECK(!info()->IsStub());
2003      EmitBranch(instr, no_condition);
2004    } else if (type.IsHeapNumber()) {
2005      DCHECK(!info()->IsStub());
2006      XMMRegister xmm_scratch = double_scratch0();
2007      __ Xorpd(xmm_scratch, xmm_scratch);
2008      __ Ucomisd(xmm_scratch, FieldOperand(reg, HeapNumber::kValueOffset));
2009      EmitBranch(instr, not_equal);
2010    } else if (type.IsString()) {
2011      DCHECK(!info()->IsStub());
2012      __ cmpp(FieldOperand(reg, String::kLengthOffset), Immediate(0));
2013      EmitBranch(instr, not_equal);
2014    } else {
2015      ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
2016      // Avoid deopts in the case where we've never executed this path before.
2017      if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
2018
2019      if (expected.Contains(ToBooleanStub::UNDEFINED)) {
2020        // undefined -> false.
2021        __ CompareRoot(reg, Heap::kUndefinedValueRootIndex);
2022        __ j(equal, instr->FalseLabel(chunk_));
2023      }
2024      if (expected.Contains(ToBooleanStub::BOOLEAN)) {
2025        // true -> true.
2026        __ CompareRoot(reg, Heap::kTrueValueRootIndex);
2027        __ j(equal, instr->TrueLabel(chunk_));
2028        // false -> false.
2029        __ CompareRoot(reg, Heap::kFalseValueRootIndex);
2030        __ j(equal, instr->FalseLabel(chunk_));
2031      }
2032      if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
2033        // 'null' -> false.
2034        __ CompareRoot(reg, Heap::kNullValueRootIndex);
2035        __ j(equal, instr->FalseLabel(chunk_));
2036      }
2037
2038      if (expected.Contains(ToBooleanStub::SMI)) {
2039        // Smis: 0 -> false, all other -> true.
2040        __ Cmp(reg, Smi::FromInt(0));
2041        __ j(equal, instr->FalseLabel(chunk_));
2042        __ JumpIfSmi(reg, instr->TrueLabel(chunk_));
2043      } else if (expected.NeedsMap()) {
2044        // If we need a map later and have a Smi -> deopt.
2045        __ testb(reg, Immediate(kSmiTagMask));
2046        DeoptimizeIf(zero, instr, Deoptimizer::kSmi);
2047      }
2048
2049      const Register map = kScratchRegister;
2050      if (expected.NeedsMap()) {
2051        __ movp(map, FieldOperand(reg, HeapObject::kMapOffset));
2052
2053        if (expected.CanBeUndetectable()) {
2054          // Undetectable -> false.
2055          __ testb(FieldOperand(map, Map::kBitFieldOffset),
2056                   Immediate(1 << Map::kIsUndetectable));
2057          __ j(not_zero, instr->FalseLabel(chunk_));
2058        }
2059      }
2060
2061      if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
2062        // spec object -> true.
2063        __ CmpInstanceType(map, FIRST_JS_RECEIVER_TYPE);
2064        __ j(above_equal, instr->TrueLabel(chunk_));
2065      }
2066
2067      if (expected.Contains(ToBooleanStub::STRING)) {
2068        // String value -> false iff empty.
2069        Label not_string;
2070        __ CmpInstanceType(map, FIRST_NONSTRING_TYPE);
2071        __ j(above_equal, &not_string, Label::kNear);
2072        __ cmpp(FieldOperand(reg, String::kLengthOffset), Immediate(0));
2073        __ j(not_zero, instr->TrueLabel(chunk_));
2074        __ jmp(instr->FalseLabel(chunk_));
2075        __ bind(&not_string);
2076      }
2077
2078      if (expected.Contains(ToBooleanStub::SYMBOL)) {
2079        // Symbol value -> true.
2080        __ CmpInstanceType(map, SYMBOL_TYPE);
2081        __ j(equal, instr->TrueLabel(chunk_));
2082      }
2083
2084      if (expected.Contains(ToBooleanStub::SIMD_VALUE)) {
2085        // SIMD value -> true.
2086        __ CmpInstanceType(map, SIMD128_VALUE_TYPE);
2087        __ j(equal, instr->TrueLabel(chunk_));
2088      }
2089
2090      if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
2091        // heap number -> false iff +0, -0, or NaN.
2092        Label not_heap_number;
2093        __ CompareRoot(map, Heap::kHeapNumberMapRootIndex);
2094        __ j(not_equal, &not_heap_number, Label::kNear);
2095        XMMRegister xmm_scratch = double_scratch0();
2096        __ Xorpd(xmm_scratch, xmm_scratch);
2097        __ Ucomisd(xmm_scratch, FieldOperand(reg, HeapNumber::kValueOffset));
2098        __ j(zero, instr->FalseLabel(chunk_));
2099        __ jmp(instr->TrueLabel(chunk_));
2100        __ bind(&not_heap_number);
2101      }
2102
2103      if (!expected.IsGeneric()) {
2104        // We've seen something for the first time -> deopt.
2105        // This can only happen if we are not generic already.
2106        DeoptimizeIf(no_condition, instr, Deoptimizer::kUnexpectedObject);
2107      }
2108    }
2109  }
2110}
2111
2112
2113void LCodeGen::EmitGoto(int block) {
2114  if (!IsNextEmittedBlock(block)) {
2115    __ jmp(chunk_->GetAssemblyLabel(chunk_->LookupDestination(block)));
2116  }
2117}
2118
2119
2120void LCodeGen::DoGoto(LGoto* instr) {
2121  EmitGoto(instr->block_id());
2122}
2123
2124
2125inline Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
2126  Condition cond = no_condition;
2127  switch (op) {
2128    case Token::EQ:
2129    case Token::EQ_STRICT:
2130      cond = equal;
2131      break;
2132    case Token::NE:
2133    case Token::NE_STRICT:
2134      cond = not_equal;
2135      break;
2136    case Token::LT:
2137      cond = is_unsigned ? below : less;
2138      break;
2139    case Token::GT:
2140      cond = is_unsigned ? above : greater;
2141      break;
2142    case Token::LTE:
2143      cond = is_unsigned ? below_equal : less_equal;
2144      break;
2145    case Token::GTE:
2146      cond = is_unsigned ? above_equal : greater_equal;
2147      break;
2148    case Token::IN:
2149    case Token::INSTANCEOF:
2150    default:
2151      UNREACHABLE();
2152  }
2153  return cond;
2154}
2155
2156
2157void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2158  LOperand* left = instr->left();
2159  LOperand* right = instr->right();
2160  bool is_unsigned =
2161      instr->is_double() ||
2162      instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2163      instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2164  Condition cc = TokenToCondition(instr->op(), is_unsigned);
2165
2166  if (left->IsConstantOperand() && right->IsConstantOperand()) {
2167    // We can statically evaluate the comparison.
2168    double left_val = ToDouble(LConstantOperand::cast(left));
2169    double right_val = ToDouble(LConstantOperand::cast(right));
2170    int next_block = Token::EvalComparison(instr->op(), left_val, right_val)
2171                         ? instr->TrueDestination(chunk_)
2172                         : instr->FalseDestination(chunk_);
2173    EmitGoto(next_block);
2174  } else {
2175    if (instr->is_double()) {
2176      // Don't base result on EFLAGS when a NaN is involved. Instead
2177      // jump to the false block.
2178      __ Ucomisd(ToDoubleRegister(left), ToDoubleRegister(right));
2179      __ j(parity_even, instr->FalseLabel(chunk_));
2180    } else {
2181      int32_t value;
2182      if (right->IsConstantOperand()) {
2183        value = ToInteger32(LConstantOperand::cast(right));
2184        if (instr->hydrogen_value()->representation().IsSmi()) {
2185          __ Cmp(ToRegister(left), Smi::FromInt(value));
2186        } else {
2187          __ cmpl(ToRegister(left), Immediate(value));
2188        }
2189      } else if (left->IsConstantOperand()) {
2190        value = ToInteger32(LConstantOperand::cast(left));
2191        if (instr->hydrogen_value()->representation().IsSmi()) {
2192          if (right->IsRegister()) {
2193            __ Cmp(ToRegister(right), Smi::FromInt(value));
2194          } else {
2195            __ Cmp(ToOperand(right), Smi::FromInt(value));
2196          }
2197        } else if (right->IsRegister()) {
2198          __ cmpl(ToRegister(right), Immediate(value));
2199        } else {
2200          __ cmpl(ToOperand(right), Immediate(value));
2201        }
2202        // We commuted the operands, so commute the condition.
2203        cc = CommuteCondition(cc);
2204      } else if (instr->hydrogen_value()->representation().IsSmi()) {
2205        if (right->IsRegister()) {
2206          __ cmpp(ToRegister(left), ToRegister(right));
2207        } else {
2208          __ cmpp(ToRegister(left), ToOperand(right));
2209        }
2210      } else {
2211        if (right->IsRegister()) {
2212          __ cmpl(ToRegister(left), ToRegister(right));
2213        } else {
2214          __ cmpl(ToRegister(left), ToOperand(right));
2215        }
2216      }
2217    }
2218    EmitBranch(instr, cc);
2219  }
2220}
2221
2222
2223void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
2224  Register left = ToRegister(instr->left());
2225
2226  if (instr->right()->IsConstantOperand()) {
2227    Handle<Object> right = ToHandle(LConstantOperand::cast(instr->right()));
2228    __ Cmp(left, right);
2229  } else {
2230    Register right = ToRegister(instr->right());
2231    __ cmpp(left, right);
2232  }
2233  EmitBranch(instr, equal);
2234}
2235
2236
2237void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) {
2238  if (instr->hydrogen()->representation().IsTagged()) {
2239    Register input_reg = ToRegister(instr->object());
2240    __ Cmp(input_reg, factory()->the_hole_value());
2241    EmitBranch(instr, equal);
2242    return;
2243  }
2244
2245  XMMRegister input_reg = ToDoubleRegister(instr->object());
2246  __ Ucomisd(input_reg, input_reg);
2247  EmitFalseBranch(instr, parity_odd);
2248
2249  __ subp(rsp, Immediate(kDoubleSize));
2250  __ Movsd(MemOperand(rsp, 0), input_reg);
2251  __ addp(rsp, Immediate(kDoubleSize));
2252
2253  int offset = sizeof(kHoleNanUpper32);
2254  __ cmpl(MemOperand(rsp, -offset), Immediate(kHoleNanUpper32));
2255  EmitBranch(instr, equal);
2256}
2257
2258
2259Condition LCodeGen::EmitIsString(Register input,
2260                                 Register temp1,
2261                                 Label* is_not_string,
2262                                 SmiCheck check_needed = INLINE_SMI_CHECK) {
2263  if (check_needed == INLINE_SMI_CHECK) {
2264    __ JumpIfSmi(input, is_not_string);
2265  }
2266
2267  Condition cond =  masm_->IsObjectStringType(input, temp1, temp1);
2268
2269  return cond;
2270}
2271
2272
2273void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
2274  Register reg = ToRegister(instr->value());
2275  Register temp = ToRegister(instr->temp());
2276
2277  SmiCheck check_needed =
2278      instr->hydrogen()->value()->type().IsHeapObject()
2279          ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2280
2281  Condition true_cond = EmitIsString(
2282      reg, temp, instr->FalseLabel(chunk_), check_needed);
2283
2284  EmitBranch(instr, true_cond);
2285}
2286
2287
2288void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
2289  Condition is_smi;
2290  if (instr->value()->IsRegister()) {
2291    Register input = ToRegister(instr->value());
2292    is_smi = masm()->CheckSmi(input);
2293  } else {
2294    Operand input = ToOperand(instr->value());
2295    is_smi = masm()->CheckSmi(input);
2296  }
2297  EmitBranch(instr, is_smi);
2298}
2299
2300
2301void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
2302  Register input = ToRegister(instr->value());
2303  Register temp = ToRegister(instr->temp());
2304
2305  if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2306    __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2307  }
2308  __ movp(temp, FieldOperand(input, HeapObject::kMapOffset));
2309  __ testb(FieldOperand(temp, Map::kBitFieldOffset),
2310           Immediate(1 << Map::kIsUndetectable));
2311  EmitBranch(instr, not_zero);
2312}
2313
2314
2315void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
2316  DCHECK(ToRegister(instr->context()).is(rsi));
2317  DCHECK(ToRegister(instr->left()).is(rdx));
2318  DCHECK(ToRegister(instr->right()).is(rax));
2319
2320  Handle<Code> code = CodeFactory::StringCompare(isolate()).code();
2321  CallCode(code, RelocInfo::CODE_TARGET, instr);
2322  __ testp(rax, rax);
2323
2324  EmitBranch(instr, TokenToCondition(instr->op(), false));
2325}
2326
2327
2328static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
2329  InstanceType from = instr->from();
2330  InstanceType to = instr->to();
2331  if (from == FIRST_TYPE) return to;
2332  DCHECK(from == to || to == LAST_TYPE);
2333  return from;
2334}
2335
2336
2337static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
2338  InstanceType from = instr->from();
2339  InstanceType to = instr->to();
2340  if (from == to) return equal;
2341  if (to == LAST_TYPE) return above_equal;
2342  if (from == FIRST_TYPE) return below_equal;
2343  UNREACHABLE();
2344  return equal;
2345}
2346
2347
2348void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
2349  Register input = ToRegister(instr->value());
2350
2351  if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2352    __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2353  }
2354
2355  __ CmpObjectType(input, TestType(instr->hydrogen()), kScratchRegister);
2356  EmitBranch(instr, BranchCondition(instr->hydrogen()));
2357}
2358
2359
2360void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
2361  Register input = ToRegister(instr->value());
2362  Register result = ToRegister(instr->result());
2363
2364  __ AssertString(input);
2365
2366  __ movl(result, FieldOperand(input, String::kHashFieldOffset));
2367  DCHECK(String::kHashShift >= kSmiTagSize);
2368  __ IndexFromHash(result, result);
2369}
2370
2371
2372void LCodeGen::DoHasCachedArrayIndexAndBranch(
2373    LHasCachedArrayIndexAndBranch* instr) {
2374  Register input = ToRegister(instr->value());
2375
2376  __ testl(FieldOperand(input, String::kHashFieldOffset),
2377           Immediate(String::kContainsCachedArrayIndexMask));
2378  EmitBranch(instr, equal);
2379}
2380
2381
2382// Branches to a label or falls through with the answer in the z flag.
2383// Trashes the temp register.
2384void LCodeGen::EmitClassOfTest(Label* is_true,
2385                               Label* is_false,
2386                               Handle<String> class_name,
2387                               Register input,
2388                               Register temp,
2389                               Register temp2) {
2390  DCHECK(!input.is(temp));
2391  DCHECK(!input.is(temp2));
2392  DCHECK(!temp.is(temp2));
2393
2394  __ JumpIfSmi(input, is_false);
2395
2396  __ CmpObjectType(input, JS_FUNCTION_TYPE, temp);
2397  if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
2398    __ j(equal, is_true);
2399  } else {
2400    __ j(equal, is_false);
2401  }
2402
2403  // Check if the constructor in the map is a function.
2404  __ GetMapConstructor(temp, temp, kScratchRegister);
2405
2406  // Objects with a non-function constructor have class 'Object'.
2407  __ CmpInstanceType(kScratchRegister, JS_FUNCTION_TYPE);
2408  if (String::Equals(class_name, isolate()->factory()->Object_string())) {
2409    __ j(not_equal, is_true);
2410  } else {
2411    __ j(not_equal, is_false);
2412  }
2413
2414  // temp now contains the constructor function. Grab the
2415  // instance class name from there.
2416  __ movp(temp, FieldOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2417  __ movp(temp, FieldOperand(temp,
2418                             SharedFunctionInfo::kInstanceClassNameOffset));
2419  // The class name we are testing against is internalized since it's a literal.
2420  // The name in the constructor is internalized because of the way the context
2421  // is booted.  This routine isn't expected to work for random API-created
2422  // classes and it doesn't have to because you can't access it with natives
2423  // syntax.  Since both sides are internalized it is sufficient to use an
2424  // identity comparison.
2425  DCHECK(class_name->IsInternalizedString());
2426  __ Cmp(temp, class_name);
2427  // End with the answer in the z flag.
2428}
2429
2430
2431void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
2432  Register input = ToRegister(instr->value());
2433  Register temp = ToRegister(instr->temp());
2434  Register temp2 = ToRegister(instr->temp2());
2435  Handle<String> class_name = instr->hydrogen()->class_name();
2436
2437  EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2438      class_name, input, temp, temp2);
2439
2440  EmitBranch(instr, equal);
2441}
2442
2443
2444void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
2445  Register reg = ToRegister(instr->value());
2446
2447  __ Cmp(FieldOperand(reg, HeapObject::kMapOffset), instr->map());
2448  EmitBranch(instr, equal);
2449}
2450
2451
2452void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
2453  DCHECK(ToRegister(instr->context()).is(rsi));
2454  DCHECK(ToRegister(instr->left()).is(InstanceOfDescriptor::LeftRegister()));
2455  DCHECK(ToRegister(instr->right()).is(InstanceOfDescriptor::RightRegister()));
2456  DCHECK(ToRegister(instr->result()).is(rax));
2457  InstanceOfStub stub(isolate());
2458  CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2459}
2460
2461
2462void LCodeGen::DoHasInPrototypeChainAndBranch(
2463    LHasInPrototypeChainAndBranch* instr) {
2464  Register const object = ToRegister(instr->object());
2465  Register const object_map = kScratchRegister;
2466  Register const object_prototype = object_map;
2467  Register const prototype = ToRegister(instr->prototype());
2468
2469  // The {object} must be a spec object.  It's sufficient to know that {object}
2470  // is not a smi, since all other non-spec objects have {null} prototypes and
2471  // will be ruled out below.
2472  if (instr->hydrogen()->ObjectNeedsSmiCheck()) {
2473    Condition is_smi = __ CheckSmi(object);
2474    EmitFalseBranch(instr, is_smi);
2475  }
2476
2477  // Loop through the {object}s prototype chain looking for the {prototype}.
2478  __ movp(object_map, FieldOperand(object, HeapObject::kMapOffset));
2479  Label loop;
2480  __ bind(&loop);
2481
2482
2483  // Deoptimize if the object needs to be access checked.
2484  __ testb(FieldOperand(object_map, Map::kBitFieldOffset),
2485           Immediate(1 << Map::kIsAccessCheckNeeded));
2486  DeoptimizeIf(not_zero, instr, Deoptimizer::kAccessCheck);
2487  // Deoptimize for proxies.
2488  __ CmpInstanceType(object_map, JS_PROXY_TYPE);
2489  DeoptimizeIf(equal, instr, Deoptimizer::kProxy);
2490
2491  __ movp(object_prototype, FieldOperand(object_map, Map::kPrototypeOffset));
2492  __ cmpp(object_prototype, prototype);
2493  EmitTrueBranch(instr, equal);
2494  __ CompareRoot(object_prototype, Heap::kNullValueRootIndex);
2495  EmitFalseBranch(instr, equal);
2496  __ movp(object_map, FieldOperand(object_prototype, HeapObject::kMapOffset));
2497  __ jmp(&loop);
2498}
2499
2500
2501void LCodeGen::DoCmpT(LCmpT* instr) {
2502  DCHECK(ToRegister(instr->context()).is(rsi));
2503  Token::Value op = instr->op();
2504
2505  Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
2506  CallCode(ic, RelocInfo::CODE_TARGET, instr);
2507
2508  Condition condition = TokenToCondition(op, false);
2509  Label true_value, done;
2510  __ testp(rax, rax);
2511  __ j(condition, &true_value, Label::kNear);
2512  __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex);
2513  __ jmp(&done, Label::kNear);
2514  __ bind(&true_value);
2515  __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex);
2516  __ bind(&done);
2517}
2518
2519
2520void LCodeGen::DoReturn(LReturn* instr) {
2521  if (FLAG_trace && info()->IsOptimizing()) {
2522    // Preserve the return value on the stack and rely on the runtime call
2523    // to return the value in the same register.  We're leaving the code
2524    // managed by the register allocator and tearing down the frame, it's
2525    // safe to write to the context register.
2526    __ Push(rax);
2527    __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
2528    __ CallRuntime(Runtime::kTraceExit);
2529  }
2530  if (info()->saves_caller_doubles()) {
2531    RestoreCallerDoubles();
2532  }
2533  if (NeedsEagerFrame()) {
2534    __ movp(rsp, rbp);
2535    __ popq(rbp);
2536  }
2537  if (instr->has_constant_parameter_count()) {
2538    __ Ret((ToInteger32(instr->constant_parameter_count()) + 1) * kPointerSize,
2539           rcx);
2540  } else {
2541    DCHECK(info()->IsStub());  // Functions would need to drop one more value.
2542    Register reg = ToRegister(instr->parameter_count());
2543    // The argument count parameter is a smi
2544    __ SmiToInteger32(reg, reg);
2545    Register return_addr_reg = reg.is(rcx) ? rbx : rcx;
2546    __ PopReturnAddressTo(return_addr_reg);
2547    __ shlp(reg, Immediate(kPointerSizeLog2));
2548    __ addp(rsp, reg);
2549    __ jmp(return_addr_reg);
2550  }
2551}
2552
2553
2554template <class T>
2555void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
2556  Register vector_register = ToRegister(instr->temp_vector());
2557  Register slot_register = LoadWithVectorDescriptor::SlotRegister();
2558  DCHECK(vector_register.is(LoadWithVectorDescriptor::VectorRegister()));
2559  DCHECK(slot_register.is(rax));
2560
2561  AllowDeferredHandleDereference vector_structure_check;
2562  Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2563  __ Move(vector_register, vector);
2564  // No need to allocate this register.
2565  FeedbackVectorSlot slot = instr->hydrogen()->slot();
2566  int index = vector->GetIndex(slot);
2567  __ Move(slot_register, Smi::FromInt(index));
2568}
2569
2570
2571template <class T>
2572void LCodeGen::EmitVectorStoreICRegisters(T* instr) {
2573  Register vector_register = ToRegister(instr->temp_vector());
2574  Register slot_register = ToRegister(instr->temp_slot());
2575
2576  AllowDeferredHandleDereference vector_structure_check;
2577  Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2578  __ Move(vector_register, vector);
2579  FeedbackVectorSlot slot = instr->hydrogen()->slot();
2580  int index = vector->GetIndex(slot);
2581  __ Move(slot_register, Smi::FromInt(index));
2582}
2583
2584
2585void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
2586  DCHECK(ToRegister(instr->context()).is(rsi));
2587  DCHECK(ToRegister(instr->global_object())
2588             .is(LoadDescriptor::ReceiverRegister()));
2589  DCHECK(ToRegister(instr->result()).is(rax));
2590
2591  __ Move(LoadDescriptor::NameRegister(), instr->name());
2592  EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
2593  Handle<Code> ic = CodeFactory::LoadICInOptimizedCode(
2594                        isolate(), instr->typeof_mode(), PREMONOMORPHIC)
2595                        .code();
2596  CallCode(ic, RelocInfo::CODE_TARGET, instr);
2597}
2598
2599
2600void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
2601  Register context = ToRegister(instr->context());
2602  Register result = ToRegister(instr->result());
2603  __ movp(result, ContextOperand(context, instr->slot_index()));
2604  if (instr->hydrogen()->RequiresHoleCheck()) {
2605    __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
2606    if (instr->hydrogen()->DeoptimizesOnHole()) {
2607      DeoptimizeIf(equal, instr, Deoptimizer::kHole);
2608    } else {
2609      Label is_not_hole;
2610      __ j(not_equal, &is_not_hole, Label::kNear);
2611      __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2612      __ bind(&is_not_hole);
2613    }
2614  }
2615}
2616
2617
2618void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
2619  Register context = ToRegister(instr->context());
2620  Register value = ToRegister(instr->value());
2621
2622  Operand target = ContextOperand(context, instr->slot_index());
2623
2624  Label skip_assignment;
2625  if (instr->hydrogen()->RequiresHoleCheck()) {
2626    __ CompareRoot(target, Heap::kTheHoleValueRootIndex);
2627    if (instr->hydrogen()->DeoptimizesOnHole()) {
2628      DeoptimizeIf(equal, instr, Deoptimizer::kHole);
2629    } else {
2630      __ j(not_equal, &skip_assignment);
2631    }
2632  }
2633  __ movp(target, value);
2634
2635  if (instr->hydrogen()->NeedsWriteBarrier()) {
2636    SmiCheck check_needed =
2637      instr->hydrogen()->value()->type().IsHeapObject()
2638          ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2639    int offset = Context::SlotOffset(instr->slot_index());
2640    Register scratch = ToRegister(instr->temp());
2641    __ RecordWriteContextSlot(context,
2642                              offset,
2643                              value,
2644                              scratch,
2645                              kSaveFPRegs,
2646                              EMIT_REMEMBERED_SET,
2647                              check_needed);
2648  }
2649
2650  __ bind(&skip_assignment);
2651}
2652
2653
2654void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
2655  HObjectAccess access = instr->hydrogen()->access();
2656  int offset = access.offset();
2657
2658  if (access.IsExternalMemory()) {
2659    Register result = ToRegister(instr->result());
2660    if (instr->object()->IsConstantOperand()) {
2661      DCHECK(result.is(rax));
2662      __ load_rax(ToExternalReference(LConstantOperand::cast(instr->object())));
2663    } else {
2664      Register object = ToRegister(instr->object());
2665      __ Load(result, MemOperand(object, offset), access.representation());
2666    }
2667    return;
2668  }
2669
2670  Register object = ToRegister(instr->object());
2671  if (instr->hydrogen()->representation().IsDouble()) {
2672    DCHECK(access.IsInobject());
2673    XMMRegister result = ToDoubleRegister(instr->result());
2674    __ Movsd(result, FieldOperand(object, offset));
2675    return;
2676  }
2677
2678  Register result = ToRegister(instr->result());
2679  if (!access.IsInobject()) {
2680    __ movp(result, FieldOperand(object, JSObject::kPropertiesOffset));
2681    object = result;
2682  }
2683
2684  Representation representation = access.representation();
2685  if (representation.IsSmi() && SmiValuesAre32Bits() &&
2686      instr->hydrogen()->representation().IsInteger32()) {
2687    if (FLAG_debug_code) {
2688      Register scratch = kScratchRegister;
2689      __ Load(scratch, FieldOperand(object, offset), representation);
2690      __ AssertSmi(scratch);
2691    }
2692
2693    // Read int value directly from upper half of the smi.
2694    STATIC_ASSERT(kSmiTag == 0);
2695    DCHECK(kSmiTagSize + kSmiShiftSize == 32);
2696    offset += kPointerSize / 2;
2697    representation = Representation::Integer32();
2698  }
2699  __ Load(result, FieldOperand(object, offset), representation);
2700}
2701
2702
2703void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
2704  DCHECK(ToRegister(instr->context()).is(rsi));
2705  DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
2706  DCHECK(ToRegister(instr->result()).is(rax));
2707
2708  __ Move(LoadDescriptor::NameRegister(), instr->name());
2709  EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
2710  Handle<Code> ic = CodeFactory::LoadICInOptimizedCode(
2711                        isolate(), NOT_INSIDE_TYPEOF,
2712                        instr->hydrogen()->initialization_state())
2713                        .code();
2714  CallCode(ic, RelocInfo::CODE_TARGET, instr);
2715}
2716
2717
2718void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
2719  Register function = ToRegister(instr->function());
2720  Register result = ToRegister(instr->result());
2721
2722  // Get the prototype or initial map from the function.
2723  __ movp(result,
2724         FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2725
2726  // Check that the function has a prototype or an initial map.
2727  __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
2728  DeoptimizeIf(equal, instr, Deoptimizer::kHole);
2729
2730  // If the function does not have an initial map, we're done.
2731  Label done;
2732  __ CmpObjectType(result, MAP_TYPE, kScratchRegister);
2733  __ j(not_equal, &done, Label::kNear);
2734
2735  // Get the prototype from the initial map.
2736  __ movp(result, FieldOperand(result, Map::kPrototypeOffset));
2737
2738  // All done.
2739  __ bind(&done);
2740}
2741
2742
2743void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
2744  Register result = ToRegister(instr->result());
2745  __ LoadRoot(result, instr->index());
2746}
2747
2748
2749void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
2750  Register arguments = ToRegister(instr->arguments());
2751  Register result = ToRegister(instr->result());
2752
2753  if (instr->length()->IsConstantOperand() &&
2754      instr->index()->IsConstantOperand()) {
2755    int32_t const_index = ToInteger32(LConstantOperand::cast(instr->index()));
2756    int32_t const_length = ToInteger32(LConstantOperand::cast(instr->length()));
2757    if (const_index >= 0 && const_index < const_length) {
2758      StackArgumentsAccessor args(arguments, const_length,
2759                                  ARGUMENTS_DONT_CONTAIN_RECEIVER);
2760      __ movp(result, args.GetArgumentOperand(const_index));
2761    } else if (FLAG_debug_code) {
2762      __ int3();
2763    }
2764  } else {
2765    Register length = ToRegister(instr->length());
2766    // There are two words between the frame pointer and the last argument.
2767    // Subtracting from length accounts for one of them add one more.
2768    if (instr->index()->IsRegister()) {
2769      __ subl(length, ToRegister(instr->index()));
2770    } else {
2771      __ subl(length, ToOperand(instr->index()));
2772    }
2773    StackArgumentsAccessor args(arguments, length,
2774                                ARGUMENTS_DONT_CONTAIN_RECEIVER);
2775    __ movp(result, args.GetArgumentOperand(0));
2776  }
2777}
2778
2779
2780void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
2781  ElementsKind elements_kind = instr->elements_kind();
2782  LOperand* key = instr->key();
2783  if (kPointerSize == kInt32Size && !key->IsConstantOperand()) {
2784    Register key_reg = ToRegister(key);
2785    Representation key_representation =
2786        instr->hydrogen()->key()->representation();
2787    if (ExternalArrayOpRequiresTemp(key_representation, elements_kind)) {
2788      __ SmiToInteger64(key_reg, key_reg);
2789    } else if (instr->hydrogen()->IsDehoisted()) {
2790      // Sign extend key because it could be a 32 bit negative value
2791      // and the dehoisted address computation happens in 64 bits
2792      __ movsxlq(key_reg, key_reg);
2793    }
2794  }
2795  Operand operand(BuildFastArrayOperand(
2796      instr->elements(),
2797      key,
2798      instr->hydrogen()->key()->representation(),
2799      elements_kind,
2800      instr->base_offset()));
2801
2802  if (elements_kind == FLOAT32_ELEMENTS) {
2803    XMMRegister result(ToDoubleRegister(instr->result()));
2804    __ Cvtss2sd(result, operand);
2805  } else if (elements_kind == FLOAT64_ELEMENTS) {
2806    __ Movsd(ToDoubleRegister(instr->result()), operand);
2807  } else {
2808    Register result(ToRegister(instr->result()));
2809    switch (elements_kind) {
2810      case INT8_ELEMENTS:
2811        __ movsxbl(result, operand);
2812        break;
2813      case UINT8_ELEMENTS:
2814      case UINT8_CLAMPED_ELEMENTS:
2815        __ movzxbl(result, operand);
2816        break;
2817      case INT16_ELEMENTS:
2818        __ movsxwl(result, operand);
2819        break;
2820      case UINT16_ELEMENTS:
2821        __ movzxwl(result, operand);
2822        break;
2823      case INT32_ELEMENTS:
2824        __ movl(result, operand);
2825        break;
2826      case UINT32_ELEMENTS:
2827        __ movl(result, operand);
2828        if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
2829          __ testl(result, result);
2830          DeoptimizeIf(negative, instr, Deoptimizer::kNegativeValue);
2831        }
2832        break;
2833      case FLOAT32_ELEMENTS:
2834      case FLOAT64_ELEMENTS:
2835      case FAST_ELEMENTS:
2836      case FAST_SMI_ELEMENTS:
2837      case FAST_DOUBLE_ELEMENTS:
2838      case FAST_HOLEY_ELEMENTS:
2839      case FAST_HOLEY_SMI_ELEMENTS:
2840      case FAST_HOLEY_DOUBLE_ELEMENTS:
2841      case DICTIONARY_ELEMENTS:
2842      case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
2843      case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
2844      case FAST_STRING_WRAPPER_ELEMENTS:
2845      case SLOW_STRING_WRAPPER_ELEMENTS:
2846      case NO_ELEMENTS:
2847        UNREACHABLE();
2848        break;
2849    }
2850  }
2851}
2852
2853
2854void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
2855  XMMRegister result(ToDoubleRegister(instr->result()));
2856  LOperand* key = instr->key();
2857  if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
2858      instr->hydrogen()->IsDehoisted()) {
2859    // Sign extend key because it could be a 32 bit negative value
2860    // and the dehoisted address computation happens in 64 bits
2861    __ movsxlq(ToRegister(key), ToRegister(key));
2862  }
2863  if (instr->hydrogen()->RequiresHoleCheck()) {
2864    Operand hole_check_operand = BuildFastArrayOperand(
2865        instr->elements(),
2866        key,
2867        instr->hydrogen()->key()->representation(),
2868        FAST_DOUBLE_ELEMENTS,
2869        instr->base_offset() + sizeof(kHoleNanLower32));
2870    __ cmpl(hole_check_operand, Immediate(kHoleNanUpper32));
2871    DeoptimizeIf(equal, instr, Deoptimizer::kHole);
2872  }
2873
2874  Operand double_load_operand = BuildFastArrayOperand(
2875      instr->elements(),
2876      key,
2877      instr->hydrogen()->key()->representation(),
2878      FAST_DOUBLE_ELEMENTS,
2879      instr->base_offset());
2880  __ Movsd(result, double_load_operand);
2881}
2882
2883
2884void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
2885  HLoadKeyed* hinstr = instr->hydrogen();
2886  Register result = ToRegister(instr->result());
2887  LOperand* key = instr->key();
2888  bool requires_hole_check = hinstr->RequiresHoleCheck();
2889  Representation representation = hinstr->representation();
2890  int offset = instr->base_offset();
2891
2892  if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
2893      instr->hydrogen()->IsDehoisted()) {
2894    // Sign extend key because it could be a 32 bit negative value
2895    // and the dehoisted address computation happens in 64 bits
2896    __ movsxlq(ToRegister(key), ToRegister(key));
2897  }
2898  if (representation.IsInteger32() && SmiValuesAre32Bits() &&
2899      hinstr->elements_kind() == FAST_SMI_ELEMENTS) {
2900    DCHECK(!requires_hole_check);
2901    if (FLAG_debug_code) {
2902      Register scratch = kScratchRegister;
2903      __ Load(scratch,
2904              BuildFastArrayOperand(instr->elements(),
2905                                    key,
2906                                    instr->hydrogen()->key()->representation(),
2907                                    FAST_ELEMENTS,
2908                                    offset),
2909              Representation::Smi());
2910      __ AssertSmi(scratch);
2911    }
2912    // Read int value directly from upper half of the smi.
2913    STATIC_ASSERT(kSmiTag == 0);
2914    DCHECK(kSmiTagSize + kSmiShiftSize == 32);
2915    offset += kPointerSize / 2;
2916  }
2917
2918  __ Load(result,
2919          BuildFastArrayOperand(instr->elements(), key,
2920                                instr->hydrogen()->key()->representation(),
2921                                FAST_ELEMENTS, offset),
2922          representation);
2923
2924  // Check for the hole value.
2925  if (requires_hole_check) {
2926    if (IsFastSmiElementsKind(hinstr->elements_kind())) {
2927      Condition smi = __ CheckSmi(result);
2928      DeoptimizeIf(NegateCondition(smi), instr, Deoptimizer::kNotASmi);
2929    } else {
2930      __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
2931      DeoptimizeIf(equal, instr, Deoptimizer::kHole);
2932    }
2933  } else if (hinstr->hole_mode() == CONVERT_HOLE_TO_UNDEFINED) {
2934    DCHECK(hinstr->elements_kind() == FAST_HOLEY_ELEMENTS);
2935    Label done;
2936    __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
2937    __ j(not_equal, &done);
2938    if (info()->IsStub()) {
2939      // A stub can safely convert the hole to undefined only if the array
2940      // protector cell contains (Smi) Isolate::kArrayProtectorValid. Otherwise
2941      // it needs to bail out.
2942      __ LoadRoot(result, Heap::kArrayProtectorRootIndex);
2943      __ Cmp(FieldOperand(result, Cell::kValueOffset),
2944             Smi::FromInt(Isolate::kArrayProtectorValid));
2945      DeoptimizeIf(not_equal, instr, Deoptimizer::kHole);
2946    }
2947    __ Move(result, isolate()->factory()->undefined_value());
2948    __ bind(&done);
2949  }
2950}
2951
2952
2953void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
2954  if (instr->is_fixed_typed_array()) {
2955    DoLoadKeyedExternalArray(instr);
2956  } else if (instr->hydrogen()->representation().IsDouble()) {
2957    DoLoadKeyedFixedDoubleArray(instr);
2958  } else {
2959    DoLoadKeyedFixedArray(instr);
2960  }
2961}
2962
2963
2964Operand LCodeGen::BuildFastArrayOperand(
2965    LOperand* elements_pointer,
2966    LOperand* key,
2967    Representation key_representation,
2968    ElementsKind elements_kind,
2969    uint32_t offset) {
2970  Register elements_pointer_reg = ToRegister(elements_pointer);
2971  int shift_size = ElementsKindToShiftSize(elements_kind);
2972  if (key->IsConstantOperand()) {
2973    int32_t constant_value = ToInteger32(LConstantOperand::cast(key));
2974    if (constant_value & 0xF0000000) {
2975      Abort(kArrayIndexConstantValueTooBig);
2976    }
2977    return Operand(elements_pointer_reg,
2978                   (constant_value << shift_size) + offset);
2979  } else {
2980    // Guaranteed by ArrayInstructionInterface::KeyedAccessIndexRequirement().
2981    DCHECK(key_representation.IsInteger32());
2982
2983    ScaleFactor scale_factor = static_cast<ScaleFactor>(shift_size);
2984    return Operand(elements_pointer_reg,
2985                   ToRegister(key),
2986                   scale_factor,
2987                   offset);
2988  }
2989}
2990
2991
2992void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
2993  DCHECK(ToRegister(instr->context()).is(rsi));
2994  DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
2995  DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
2996
2997  if (instr->hydrogen()->HasVectorAndSlot()) {
2998    EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
2999  }
3000
3001  Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(
3002                        isolate(), instr->hydrogen()->initialization_state())
3003                        .code();
3004  CallCode(ic, RelocInfo::CODE_TARGET, instr);
3005}
3006
3007
3008void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
3009  Register result = ToRegister(instr->result());
3010
3011  if (instr->hydrogen()->from_inlined()) {
3012    __ leap(result, Operand(rsp, -kFPOnStackSize + -kPCOnStackSize));
3013  } else {
3014    // Check for arguments adapter frame.
3015    Label done, adapted;
3016    __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
3017    __ Cmp(Operand(result, StandardFrameConstants::kContextOffset),
3018           Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
3019    __ j(equal, &adapted, Label::kNear);
3020
3021    // No arguments adaptor frame.
3022    __ movp(result, rbp);
3023    __ jmp(&done, Label::kNear);
3024
3025    // Arguments adaptor frame present.
3026    __ bind(&adapted);
3027    __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
3028
3029    // Result is the frame pointer for the frame if not adapted and for the real
3030    // frame below the adaptor frame if adapted.
3031    __ bind(&done);
3032  }
3033}
3034
3035
3036void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
3037  Register result = ToRegister(instr->result());
3038
3039  Label done;
3040
3041  // If no arguments adaptor frame the number of arguments is fixed.
3042  if (instr->elements()->IsRegister()) {
3043    __ cmpp(rbp, ToRegister(instr->elements()));
3044  } else {
3045    __ cmpp(rbp, ToOperand(instr->elements()));
3046  }
3047  __ movl(result, Immediate(scope()->num_parameters()));
3048  __ j(equal, &done, Label::kNear);
3049
3050  // Arguments adaptor frame present. Get argument length from there.
3051  __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
3052  __ SmiToInteger32(result,
3053                    Operand(result,
3054                            ArgumentsAdaptorFrameConstants::kLengthOffset));
3055
3056  // Argument length is in result register.
3057  __ bind(&done);
3058}
3059
3060
3061void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
3062  Register receiver = ToRegister(instr->receiver());
3063  Register function = ToRegister(instr->function());
3064
3065  // If the receiver is null or undefined, we have to pass the global
3066  // object as a receiver to normal functions. Values have to be
3067  // passed unchanged to builtins and strict-mode functions.
3068  Label global_object, receiver_ok;
3069  Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
3070
3071  if (!instr->hydrogen()->known_function()) {
3072    // Do not transform the receiver to object for strict mode
3073    // functions.
3074    __ movp(kScratchRegister,
3075            FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
3076    __ testb(FieldOperand(kScratchRegister,
3077                          SharedFunctionInfo::kStrictModeByteOffset),
3078             Immediate(1 << SharedFunctionInfo::kStrictModeBitWithinByte));
3079    __ j(not_equal, &receiver_ok, dist);
3080
3081    // Do not transform the receiver to object for builtins.
3082    __ testb(FieldOperand(kScratchRegister,
3083                          SharedFunctionInfo::kNativeByteOffset),
3084             Immediate(1 << SharedFunctionInfo::kNativeBitWithinByte));
3085    __ j(not_equal, &receiver_ok, dist);
3086  }
3087
3088  // Normal function. Replace undefined or null with global receiver.
3089  __ CompareRoot(receiver, Heap::kNullValueRootIndex);
3090  __ j(equal, &global_object, Label::kNear);
3091  __ CompareRoot(receiver, Heap::kUndefinedValueRootIndex);
3092  __ j(equal, &global_object, Label::kNear);
3093
3094  // The receiver should be a JS object.
3095  Condition is_smi = __ CheckSmi(receiver);
3096  DeoptimizeIf(is_smi, instr, Deoptimizer::kSmi);
3097  __ CmpObjectType(receiver, FIRST_JS_RECEIVER_TYPE, kScratchRegister);
3098  DeoptimizeIf(below, instr, Deoptimizer::kNotAJavaScriptObject);
3099
3100  __ jmp(&receiver_ok, Label::kNear);
3101  __ bind(&global_object);
3102  __ movp(receiver, FieldOperand(function, JSFunction::kContextOffset));
3103  __ movp(receiver, ContextOperand(receiver, Context::NATIVE_CONTEXT_INDEX));
3104  __ movp(receiver, ContextOperand(receiver, Context::GLOBAL_PROXY_INDEX));
3105
3106  __ bind(&receiver_ok);
3107}
3108
3109
3110void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3111  Register receiver = ToRegister(instr->receiver());
3112  Register function = ToRegister(instr->function());
3113  Register length = ToRegister(instr->length());
3114  Register elements = ToRegister(instr->elements());
3115  DCHECK(receiver.is(rax));  // Used for parameter count.
3116  DCHECK(function.is(rdi));  // Required by InvokeFunction.
3117  DCHECK(ToRegister(instr->result()).is(rax));
3118
3119  // Copy the arguments to this function possibly from the
3120  // adaptor frame below it.
3121  const uint32_t kArgumentsLimit = 1 * KB;
3122  __ cmpp(length, Immediate(kArgumentsLimit));
3123  DeoptimizeIf(above, instr, Deoptimizer::kTooManyArguments);
3124
3125  __ Push(receiver);
3126  __ movp(receiver, length);
3127
3128  // Loop through the arguments pushing them onto the execution
3129  // stack.
3130  Label invoke, loop;
3131  // length is a small non-negative integer, due to the test above.
3132  __ testl(length, length);
3133  __ j(zero, &invoke, Label::kNear);
3134  __ bind(&loop);
3135  StackArgumentsAccessor args(elements, length,
3136                              ARGUMENTS_DONT_CONTAIN_RECEIVER);
3137  __ Push(args.GetArgumentOperand(0));
3138  __ decl(length);
3139  __ j(not_zero, &loop);
3140
3141  // Invoke the function.
3142  __ bind(&invoke);
3143  DCHECK(instr->HasPointerMap());
3144  LPointerMap* pointers = instr->pointer_map();
3145  SafepointGenerator safepoint_generator(
3146      this, pointers, Safepoint::kLazyDeopt);
3147  ParameterCount actual(rax);
3148  __ InvokeFunction(function, no_reg, actual, CALL_FUNCTION,
3149                    safepoint_generator);
3150}
3151
3152
3153void LCodeGen::DoPushArgument(LPushArgument* instr) {
3154  LOperand* argument = instr->value();
3155  EmitPushTaggedOperand(argument);
3156}
3157
3158
3159void LCodeGen::DoDrop(LDrop* instr) {
3160  __ Drop(instr->count());
3161}
3162
3163
3164void LCodeGen::DoThisFunction(LThisFunction* instr) {
3165  Register result = ToRegister(instr->result());
3166  __ movp(result, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
3167}
3168
3169
3170void LCodeGen::DoContext(LContext* instr) {
3171  Register result = ToRegister(instr->result());
3172  if (info()->IsOptimizing()) {
3173    __ movp(result, Operand(rbp, StandardFrameConstants::kContextOffset));
3174  } else {
3175    // If there is no frame, the context must be in rsi.
3176    DCHECK(result.is(rsi));
3177  }
3178}
3179
3180
3181void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
3182  DCHECK(ToRegister(instr->context()).is(rsi));
3183  __ Push(instr->hydrogen()->pairs());
3184  __ Push(Smi::FromInt(instr->hydrogen()->flags()));
3185  CallRuntime(Runtime::kDeclareGlobals, instr);
3186}
3187
3188
3189void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
3190                                 int formal_parameter_count, int arity,
3191                                 LInstruction* instr) {
3192  bool dont_adapt_arguments =
3193      formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3194  bool can_invoke_directly =
3195      dont_adapt_arguments || formal_parameter_count == arity;
3196
3197  Register function_reg = rdi;
3198  LPointerMap* pointers = instr->pointer_map();
3199
3200  if (can_invoke_directly) {
3201    // Change context.
3202    __ movp(rsi, FieldOperand(function_reg, JSFunction::kContextOffset));
3203
3204    // Always initialize new target and number of actual arguments.
3205    __ LoadRoot(rdx, Heap::kUndefinedValueRootIndex);
3206    __ Set(rax, arity);
3207
3208    // Invoke function.
3209    if (function.is_identical_to(info()->closure())) {
3210      __ CallSelf();
3211    } else {
3212      __ Call(FieldOperand(function_reg, JSFunction::kCodeEntryOffset));
3213    }
3214
3215    // Set up deoptimization.
3216    RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT, 0);
3217  } else {
3218    // We need to adapt arguments.
3219    SafepointGenerator generator(
3220        this, pointers, Safepoint::kLazyDeopt);
3221    ParameterCount count(arity);
3222    ParameterCount expected(formal_parameter_count);
3223    __ InvokeFunction(function_reg, no_reg, expected, count, CALL_FUNCTION,
3224                      generator);
3225  }
3226}
3227
3228
3229void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
3230  DCHECK(ToRegister(instr->result()).is(rax));
3231
3232  if (instr->hydrogen()->IsTailCall()) {
3233    if (NeedsEagerFrame()) __ leave();
3234
3235    if (instr->target()->IsConstantOperand()) {
3236      LConstantOperand* target = LConstantOperand::cast(instr->target());
3237      Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3238      __ jmp(code, RelocInfo::CODE_TARGET);
3239    } else {
3240      DCHECK(instr->target()->IsRegister());
3241      Register target = ToRegister(instr->target());
3242      __ addp(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
3243      __ jmp(target);
3244    }
3245  } else {
3246    LPointerMap* pointers = instr->pointer_map();
3247    SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3248
3249    if (instr->target()->IsConstantOperand()) {
3250      LConstantOperand* target = LConstantOperand::cast(instr->target());
3251      Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3252      generator.BeforeCall(__ CallSize(code));
3253      __ call(code, RelocInfo::CODE_TARGET);
3254    } else {
3255      DCHECK(instr->target()->IsRegister());
3256      Register target = ToRegister(instr->target());
3257      generator.BeforeCall(__ CallSize(target));
3258      __ addp(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
3259      __ call(target);
3260    }
3261    generator.AfterCall();
3262  }
3263}
3264
3265
3266void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
3267  DCHECK(ToRegister(instr->function()).is(rdi));
3268  DCHECK(ToRegister(instr->result()).is(rax));
3269
3270  // Change context.
3271  __ movp(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
3272
3273  // Always initialize new target and number of actual arguments.
3274  __ LoadRoot(rdx, Heap::kUndefinedValueRootIndex);
3275  __ Set(rax, instr->arity());
3276
3277  LPointerMap* pointers = instr->pointer_map();
3278  SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3279
3280  bool is_self_call = false;
3281  if (instr->hydrogen()->function()->IsConstant()) {
3282    Handle<JSFunction> jsfun = Handle<JSFunction>::null();
3283    HConstant* fun_const = HConstant::cast(instr->hydrogen()->function());
3284    jsfun = Handle<JSFunction>::cast(fun_const->handle(isolate()));
3285    is_self_call = jsfun.is_identical_to(info()->closure());
3286  }
3287
3288  if (is_self_call) {
3289    __ CallSelf();
3290  } else {
3291    Operand target = FieldOperand(rdi, JSFunction::kCodeEntryOffset);
3292    generator.BeforeCall(__ CallSize(target));
3293    __ Call(target);
3294  }
3295  generator.AfterCall();
3296}
3297
3298
3299void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3300  Register input_reg = ToRegister(instr->value());
3301  __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
3302                 Heap::kHeapNumberMapRootIndex);
3303  DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
3304
3305  Label slow, allocated, done;
3306  Register tmp = input_reg.is(rax) ? rcx : rax;
3307  Register tmp2 = tmp.is(rcx) ? rdx : input_reg.is(rcx) ? rdx : rcx;
3308
3309  // Preserve the value of all registers.
3310  PushSafepointRegistersScope scope(this);
3311
3312  __ movl(tmp, FieldOperand(input_reg, HeapNumber::kExponentOffset));
3313  // Check the sign of the argument. If the argument is positive, just
3314  // return it. We do not need to patch the stack since |input| and
3315  // |result| are the same register and |input| will be restored
3316  // unchanged by popping safepoint registers.
3317  __ testl(tmp, Immediate(HeapNumber::kSignMask));
3318  __ j(zero, &done);
3319
3320  __ AllocateHeapNumber(tmp, tmp2, &slow);
3321  __ jmp(&allocated, Label::kNear);
3322
3323  // Slow case: Call the runtime system to do the number allocation.
3324  __ bind(&slow);
3325  CallRuntimeFromDeferred(
3326      Runtime::kAllocateHeapNumber, 0, instr, instr->context());
3327  // Set the pointer to the new heap number in tmp.
3328  if (!tmp.is(rax)) __ movp(tmp, rax);
3329  // Restore input_reg after call to runtime.
3330  __ LoadFromSafepointRegisterSlot(input_reg, input_reg);
3331
3332  __ bind(&allocated);
3333  __ movq(tmp2, FieldOperand(input_reg, HeapNumber::kValueOffset));
3334  __ shlq(tmp2, Immediate(1));
3335  __ shrq(tmp2, Immediate(1));
3336  __ movq(FieldOperand(tmp, HeapNumber::kValueOffset), tmp2);
3337  __ StoreToSafepointRegisterSlot(input_reg, tmp);
3338
3339  __ bind(&done);
3340}
3341
3342
3343void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3344  Register input_reg = ToRegister(instr->value());
3345  __ testl(input_reg, input_reg);
3346  Label is_positive;
3347  __ j(not_sign, &is_positive, Label::kNear);
3348  __ negl(input_reg);  // Sets flags.
3349  DeoptimizeIf(negative, instr, Deoptimizer::kOverflow);
3350  __ bind(&is_positive);
3351}
3352
3353
3354void LCodeGen::EmitSmiMathAbs(LMathAbs* instr) {
3355  Register input_reg = ToRegister(instr->value());
3356  __ testp(input_reg, input_reg);
3357  Label is_positive;
3358  __ j(not_sign, &is_positive, Label::kNear);
3359  __ negp(input_reg);  // Sets flags.
3360  DeoptimizeIf(negative, instr, Deoptimizer::kOverflow);
3361  __ bind(&is_positive);
3362}
3363
3364
3365void LCodeGen::DoMathAbs(LMathAbs* instr) {
3366  // Class for deferred case.
3367  class DeferredMathAbsTaggedHeapNumber final : public LDeferredCode {
3368   public:
3369    DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr)
3370        : LDeferredCode(codegen), instr_(instr) { }
3371    void Generate() override {
3372      codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3373    }
3374    LInstruction* instr() override { return instr_; }
3375
3376   private:
3377    LMathAbs* instr_;
3378  };
3379
3380  DCHECK(instr->value()->Equals(instr->result()));
3381  Representation r = instr->hydrogen()->value()->representation();
3382
3383  if (r.IsDouble()) {
3384    XMMRegister scratch = double_scratch0();
3385    XMMRegister input_reg = ToDoubleRegister(instr->value());
3386    __ Xorpd(scratch, scratch);
3387    __ Subsd(scratch, input_reg);
3388    __ Andpd(input_reg, scratch);
3389  } else if (r.IsInteger32()) {
3390    EmitIntegerMathAbs(instr);
3391  } else if (r.IsSmi()) {
3392    EmitSmiMathAbs(instr);
3393  } else {  // Tagged case.
3394    DeferredMathAbsTaggedHeapNumber* deferred =
3395        new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3396    Register input_reg = ToRegister(instr->value());
3397    // Smi check.
3398    __ JumpIfNotSmi(input_reg, deferred->entry());
3399    EmitSmiMathAbs(instr);
3400    __ bind(deferred->exit());
3401  }
3402}
3403
3404
3405void LCodeGen::DoMathFloor(LMathFloor* instr) {
3406  XMMRegister xmm_scratch = double_scratch0();
3407  Register output_reg = ToRegister(instr->result());
3408  XMMRegister input_reg = ToDoubleRegister(instr->value());
3409
3410  if (CpuFeatures::IsSupported(SSE4_1)) {
3411    CpuFeatureScope scope(masm(), SSE4_1);
3412    if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3413      // Deoptimize if minus zero.
3414      __ Movq(output_reg, input_reg);
3415      __ subq(output_reg, Immediate(1));
3416      DeoptimizeIf(overflow, instr, Deoptimizer::kMinusZero);
3417    }
3418    __ Roundsd(xmm_scratch, input_reg, kRoundDown);
3419    __ Cvttsd2si(output_reg, xmm_scratch);
3420    __ cmpl(output_reg, Immediate(0x1));
3421    DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3422  } else {
3423    Label negative_sign, done;
3424    // Deoptimize on unordered.
3425    __ Xorpd(xmm_scratch, xmm_scratch);  // Zero the register.
3426    __ Ucomisd(input_reg, xmm_scratch);
3427    DeoptimizeIf(parity_even, instr, Deoptimizer::kNaN);
3428    __ j(below, &negative_sign, Label::kNear);
3429
3430    if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3431      // Check for negative zero.
3432      Label positive_sign;
3433      __ j(above, &positive_sign, Label::kNear);
3434      __ Movmskpd(output_reg, input_reg);
3435      __ testl(output_reg, Immediate(1));
3436      DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero);
3437      __ Set(output_reg, 0);
3438      __ jmp(&done);
3439      __ bind(&positive_sign);
3440    }
3441
3442    // Use truncating instruction (OK because input is positive).
3443    __ Cvttsd2si(output_reg, input_reg);
3444    // Overflow is signalled with minint.
3445    __ cmpl(output_reg, Immediate(0x1));
3446    DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3447    __ jmp(&done, Label::kNear);
3448
3449    // Non-zero negative reaches here.
3450    __ bind(&negative_sign);
3451    // Truncate, then compare and compensate.
3452    __ Cvttsd2si(output_reg, input_reg);
3453    __ Cvtlsi2sd(xmm_scratch, output_reg);
3454    __ Ucomisd(input_reg, xmm_scratch);
3455    __ j(equal, &done, Label::kNear);
3456    __ subl(output_reg, Immediate(1));
3457    DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3458
3459    __ bind(&done);
3460  }
3461}
3462
3463
3464void LCodeGen::DoMathRound(LMathRound* instr) {
3465  const XMMRegister xmm_scratch = double_scratch0();
3466  Register output_reg = ToRegister(instr->result());
3467  XMMRegister input_reg = ToDoubleRegister(instr->value());
3468  XMMRegister input_temp = ToDoubleRegister(instr->temp());
3469  static int64_t one_half = V8_INT64_C(0x3FE0000000000000);  // 0.5
3470  static int64_t minus_one_half = V8_INT64_C(0xBFE0000000000000);  // -0.5
3471
3472  Label done, round_to_zero, below_one_half;
3473  Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
3474  __ movq(kScratchRegister, one_half);
3475  __ Movq(xmm_scratch, kScratchRegister);
3476  __ Ucomisd(xmm_scratch, input_reg);
3477  __ j(above, &below_one_half, Label::kNear);
3478
3479  // CVTTSD2SI rounds towards zero, since 0.5 <= x, we use floor(0.5 + x).
3480  __ Addsd(xmm_scratch, input_reg);
3481  __ Cvttsd2si(output_reg, xmm_scratch);
3482  // Overflow is signalled with minint.
3483  __ cmpl(output_reg, Immediate(0x1));
3484  DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3485  __ jmp(&done, dist);
3486
3487  __ bind(&below_one_half);
3488  __ movq(kScratchRegister, minus_one_half);
3489  __ Movq(xmm_scratch, kScratchRegister);
3490  __ Ucomisd(xmm_scratch, input_reg);
3491  __ j(below_equal, &round_to_zero, Label::kNear);
3492
3493  // CVTTSD2SI rounds towards zero, we use ceil(x - (-0.5)) and then
3494  // compare and compensate.
3495  __ Movapd(input_temp, input_reg);  // Do not alter input_reg.
3496  __ Subsd(input_temp, xmm_scratch);
3497  __ Cvttsd2si(output_reg, input_temp);
3498  // Catch minint due to overflow, and to prevent overflow when compensating.
3499  __ cmpl(output_reg, Immediate(0x1));
3500  DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3501
3502  __ Cvtlsi2sd(xmm_scratch, output_reg);
3503  __ Ucomisd(xmm_scratch, input_temp);
3504  __ j(equal, &done, dist);
3505  __ subl(output_reg, Immediate(1));
3506  // No overflow because we already ruled out minint.
3507  __ jmp(&done, dist);
3508
3509  __ bind(&round_to_zero);
3510  // We return 0 for the input range [+0, 0.5[, or [-0.5, 0.5[ if
3511  // we can ignore the difference between a result of -0 and +0.
3512  if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3513    __ Movq(output_reg, input_reg);
3514    __ testq(output_reg, output_reg);
3515    DeoptimizeIf(negative, instr, Deoptimizer::kMinusZero);
3516  }
3517  __ Set(output_reg, 0);
3518  __ bind(&done);
3519}
3520
3521
3522void LCodeGen::DoMathFround(LMathFround* instr) {
3523  XMMRegister input_reg = ToDoubleRegister(instr->value());
3524  XMMRegister output_reg = ToDoubleRegister(instr->result());
3525  __ Cvtsd2ss(output_reg, input_reg);
3526  __ Cvtss2sd(output_reg, output_reg);
3527}
3528
3529
3530void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3531  XMMRegister output = ToDoubleRegister(instr->result());
3532  if (instr->value()->IsDoubleRegister()) {
3533    XMMRegister input = ToDoubleRegister(instr->value());
3534    __ Sqrtsd(output, input);
3535  } else {
3536    Operand input = ToOperand(instr->value());
3537    __ Sqrtsd(output, input);
3538  }
3539}
3540
3541
3542void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3543  XMMRegister xmm_scratch = double_scratch0();
3544  XMMRegister input_reg = ToDoubleRegister(instr->value());
3545  DCHECK(ToDoubleRegister(instr->result()).is(input_reg));
3546
3547  // Note that according to ECMA-262 15.8.2.13:
3548  // Math.pow(-Infinity, 0.5) == Infinity
3549  // Math.sqrt(-Infinity) == NaN
3550  Label done, sqrt;
3551  // Check base for -Infinity.  According to IEEE-754, double-precision
3552  // -Infinity has the highest 12 bits set and the lowest 52 bits cleared.
3553  __ movq(kScratchRegister, V8_INT64_C(0xFFF0000000000000));
3554  __ Movq(xmm_scratch, kScratchRegister);
3555  __ Ucomisd(xmm_scratch, input_reg);
3556  // Comparing -Infinity with NaN results in "unordered", which sets the
3557  // zero flag as if both were equal.  However, it also sets the carry flag.
3558  __ j(not_equal, &sqrt, Label::kNear);
3559  __ j(carry, &sqrt, Label::kNear);
3560  // If input is -Infinity, return Infinity.
3561  __ Xorpd(input_reg, input_reg);
3562  __ Subsd(input_reg, xmm_scratch);
3563  __ jmp(&done, Label::kNear);
3564
3565  // Square root.
3566  __ bind(&sqrt);
3567  __ Xorpd(xmm_scratch, xmm_scratch);
3568  __ Addsd(input_reg, xmm_scratch);  // Convert -0 to +0.
3569  __ Sqrtsd(input_reg, input_reg);
3570  __ bind(&done);
3571}
3572
3573
3574void LCodeGen::DoPower(LPower* instr) {
3575  Representation exponent_type = instr->hydrogen()->right()->representation();
3576  // Having marked this as a call, we can use any registers.
3577  // Just make sure that the input/output registers are the expected ones.
3578
3579  Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3580  DCHECK(!instr->right()->IsRegister() ||
3581         ToRegister(instr->right()).is(tagged_exponent));
3582  DCHECK(!instr->right()->IsDoubleRegister() ||
3583         ToDoubleRegister(instr->right()).is(xmm1));
3584  DCHECK(ToDoubleRegister(instr->left()).is(xmm2));
3585  DCHECK(ToDoubleRegister(instr->result()).is(xmm3));
3586
3587  if (exponent_type.IsSmi()) {
3588    MathPowStub stub(isolate(), MathPowStub::TAGGED);
3589    __ CallStub(&stub);
3590  } else if (exponent_type.IsTagged()) {
3591    Label no_deopt;
3592    __ JumpIfSmi(tagged_exponent, &no_deopt, Label::kNear);
3593    __ CmpObjectType(tagged_exponent, HEAP_NUMBER_TYPE, rcx);
3594    DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
3595    __ bind(&no_deopt);
3596    MathPowStub stub(isolate(), MathPowStub::TAGGED);
3597    __ CallStub(&stub);
3598  } else if (exponent_type.IsInteger32()) {
3599    MathPowStub stub(isolate(), MathPowStub::INTEGER);
3600    __ CallStub(&stub);
3601  } else {
3602    DCHECK(exponent_type.IsDouble());
3603    MathPowStub stub(isolate(), MathPowStub::DOUBLE);
3604    __ CallStub(&stub);
3605  }
3606}
3607
3608
3609void LCodeGen::DoMathExp(LMathExp* instr) {
3610  XMMRegister input = ToDoubleRegister(instr->value());
3611  XMMRegister result = ToDoubleRegister(instr->result());
3612  XMMRegister temp0 = double_scratch0();
3613  Register temp1 = ToRegister(instr->temp1());
3614  Register temp2 = ToRegister(instr->temp2());
3615
3616  MathExpGenerator::EmitMathExp(masm(), input, result, temp0, temp1, temp2);
3617}
3618
3619
3620void LCodeGen::DoMathLog(LMathLog* instr) {
3621  DCHECK(instr->value()->Equals(instr->result()));
3622  XMMRegister input_reg = ToDoubleRegister(instr->value());
3623  XMMRegister xmm_scratch = double_scratch0();
3624  Label positive, done, zero;
3625  __ Xorpd(xmm_scratch, xmm_scratch);
3626  __ Ucomisd(input_reg, xmm_scratch);
3627  __ j(above, &positive, Label::kNear);
3628  __ j(not_carry, &zero, Label::kNear);
3629  __ Pcmpeqd(input_reg, input_reg);
3630  __ jmp(&done, Label::kNear);
3631  __ bind(&zero);
3632  ExternalReference ninf =
3633      ExternalReference::address_of_negative_infinity();
3634  Operand ninf_operand = masm()->ExternalOperand(ninf);
3635  __ Movsd(input_reg, ninf_operand);
3636  __ jmp(&done, Label::kNear);
3637  __ bind(&positive);
3638  __ fldln2();
3639  __ subp(rsp, Immediate(kDoubleSize));
3640  __ Movsd(Operand(rsp, 0), input_reg);
3641  __ fld_d(Operand(rsp, 0));
3642  __ fyl2x();
3643  __ fstp_d(Operand(rsp, 0));
3644  __ Movsd(input_reg, Operand(rsp, 0));
3645  __ addp(rsp, Immediate(kDoubleSize));
3646  __ bind(&done);
3647}
3648
3649
3650void LCodeGen::DoMathClz32(LMathClz32* instr) {
3651  Register input = ToRegister(instr->value());
3652  Register result = ToRegister(instr->result());
3653
3654  __ Lzcntl(result, input);
3655}
3656
3657
3658void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
3659  DCHECK(ToRegister(instr->context()).is(rsi));
3660  DCHECK(ToRegister(instr->function()).is(rdi));
3661  DCHECK(instr->HasPointerMap());
3662
3663  Handle<JSFunction> known_function = instr->hydrogen()->known_function();
3664  if (known_function.is_null()) {
3665    LPointerMap* pointers = instr->pointer_map();
3666    SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3667    ParameterCount count(instr->arity());
3668    __ InvokeFunction(rdi, no_reg, count, CALL_FUNCTION, generator);
3669  } else {
3670    CallKnownFunction(known_function,
3671                      instr->hydrogen()->formal_parameter_count(),
3672                      instr->arity(), instr);
3673  }
3674}
3675
3676
3677void LCodeGen::DoCallFunction(LCallFunction* instr) {
3678  HCallFunction* hinstr = instr->hydrogen();
3679  DCHECK(ToRegister(instr->context()).is(rsi));
3680  DCHECK(ToRegister(instr->function()).is(rdi));
3681  DCHECK(ToRegister(instr->result()).is(rax));
3682
3683  int arity = instr->arity();
3684
3685  ConvertReceiverMode mode = hinstr->convert_mode();
3686  if (hinstr->HasVectorAndSlot()) {
3687    Register slot_register = ToRegister(instr->temp_slot());
3688    Register vector_register = ToRegister(instr->temp_vector());
3689    DCHECK(slot_register.is(rdx));
3690    DCHECK(vector_register.is(rbx));
3691
3692    AllowDeferredHandleDereference vector_structure_check;
3693    Handle<TypeFeedbackVector> vector = hinstr->feedback_vector();
3694    int index = vector->GetIndex(hinstr->slot());
3695
3696    __ Move(vector_register, vector);
3697    __ Move(slot_register, Smi::FromInt(index));
3698
3699    Handle<Code> ic =
3700        CodeFactory::CallICInOptimizedCode(isolate(), arity, mode).code();
3701    CallCode(ic, RelocInfo::CODE_TARGET, instr);
3702  } else {
3703    __ Set(rax, arity);
3704    CallCode(isolate()->builtins()->Call(mode), RelocInfo::CODE_TARGET, instr);
3705  }
3706}
3707
3708
3709void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
3710  DCHECK(ToRegister(instr->context()).is(rsi));
3711  DCHECK(ToRegister(instr->constructor()).is(rdi));
3712  DCHECK(ToRegister(instr->result()).is(rax));
3713
3714  __ Set(rax, instr->arity());
3715  if (instr->arity() == 1) {
3716    // We only need the allocation site for the case we have a length argument.
3717    // The case may bail out to the runtime, which will determine the correct
3718    // elements kind with the site.
3719    __ Move(rbx, instr->hydrogen()->site());
3720  } else {
3721    __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
3722  }
3723
3724  ElementsKind kind = instr->hydrogen()->elements_kind();
3725  AllocationSiteOverrideMode override_mode =
3726      (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
3727          ? DISABLE_ALLOCATION_SITES
3728          : DONT_OVERRIDE;
3729
3730  if (instr->arity() == 0) {
3731    ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
3732    CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3733  } else if (instr->arity() == 1) {
3734    Label done;
3735    if (IsFastPackedElementsKind(kind)) {
3736      Label packed_case;
3737      // We might need a change here
3738      // look at the first argument
3739      __ movp(rcx, Operand(rsp, 0));
3740      __ testp(rcx, rcx);
3741      __ j(zero, &packed_case, Label::kNear);
3742
3743      ElementsKind holey_kind = GetHoleyElementsKind(kind);
3744      ArraySingleArgumentConstructorStub stub(isolate(),
3745                                              holey_kind,
3746                                              override_mode);
3747      CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3748      __ jmp(&done, Label::kNear);
3749      __ bind(&packed_case);
3750    }
3751
3752    ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
3753    CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3754    __ bind(&done);
3755  } else {
3756    ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
3757    CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3758  }
3759}
3760
3761
3762void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
3763  DCHECK(ToRegister(instr->context()).is(rsi));
3764  CallRuntime(instr->function(), instr->arity(), instr, instr->save_doubles());
3765}
3766
3767
3768void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
3769  Register function = ToRegister(instr->function());
3770  Register code_object = ToRegister(instr->code_object());
3771  __ leap(code_object, FieldOperand(code_object, Code::kHeaderSize));
3772  __ movp(FieldOperand(function, JSFunction::kCodeEntryOffset), code_object);
3773}
3774
3775
3776void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
3777  Register result = ToRegister(instr->result());
3778  Register base = ToRegister(instr->base_object());
3779  if (instr->offset()->IsConstantOperand()) {
3780    LConstantOperand* offset = LConstantOperand::cast(instr->offset());
3781    __ leap(result, Operand(base, ToInteger32(offset)));
3782  } else {
3783    Register offset = ToRegister(instr->offset());
3784    __ leap(result, Operand(base, offset, times_1, 0));
3785  }
3786}
3787
3788
3789void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
3790  HStoreNamedField* hinstr = instr->hydrogen();
3791  Representation representation = instr->representation();
3792
3793  HObjectAccess access = hinstr->access();
3794  int offset = access.offset();
3795
3796  if (access.IsExternalMemory()) {
3797    DCHECK(!hinstr->NeedsWriteBarrier());
3798    Register value = ToRegister(instr->value());
3799    if (instr->object()->IsConstantOperand()) {
3800      DCHECK(value.is(rax));
3801      LConstantOperand* object = LConstantOperand::cast(instr->object());
3802      __ store_rax(ToExternalReference(object));
3803    } else {
3804      Register object = ToRegister(instr->object());
3805      __ Store(MemOperand(object, offset), value, representation);
3806    }
3807    return;
3808  }
3809
3810  Register object = ToRegister(instr->object());
3811  __ AssertNotSmi(object);
3812
3813  DCHECK(!representation.IsSmi() ||
3814         !instr->value()->IsConstantOperand() ||
3815         IsInteger32Constant(LConstantOperand::cast(instr->value())));
3816  if (!FLAG_unbox_double_fields && representation.IsDouble()) {
3817    DCHECK(access.IsInobject());
3818    DCHECK(!hinstr->has_transition());
3819    DCHECK(!hinstr->NeedsWriteBarrier());
3820    XMMRegister value = ToDoubleRegister(instr->value());
3821    __ Movsd(FieldOperand(object, offset), value);
3822    return;
3823  }
3824
3825  if (hinstr->has_transition()) {
3826    Handle<Map> transition = hinstr->transition_map();
3827    AddDeprecationDependency(transition);
3828    if (!hinstr->NeedsWriteBarrierForMap()) {
3829      __ Move(FieldOperand(object, HeapObject::kMapOffset), transition);
3830    } else {
3831      Register temp = ToRegister(instr->temp());
3832      __ Move(kScratchRegister, transition);
3833      __ movp(FieldOperand(object, HeapObject::kMapOffset), kScratchRegister);
3834      // Update the write barrier for the map field.
3835      __ RecordWriteForMap(object,
3836                           kScratchRegister,
3837                           temp,
3838                           kSaveFPRegs);
3839    }
3840  }
3841
3842  // Do the store.
3843  Register write_register = object;
3844  if (!access.IsInobject()) {
3845    write_register = ToRegister(instr->temp());
3846    __ movp(write_register, FieldOperand(object, JSObject::kPropertiesOffset));
3847  }
3848
3849  if (representation.IsSmi() && SmiValuesAre32Bits() &&
3850      hinstr->value()->representation().IsInteger32()) {
3851    DCHECK(hinstr->store_mode() == STORE_TO_INITIALIZED_ENTRY);
3852    if (FLAG_debug_code) {
3853      Register scratch = kScratchRegister;
3854      __ Load(scratch, FieldOperand(write_register, offset), representation);
3855      __ AssertSmi(scratch);
3856    }
3857    // Store int value directly to upper half of the smi.
3858    STATIC_ASSERT(kSmiTag == 0);
3859    DCHECK(kSmiTagSize + kSmiShiftSize == 32);
3860    offset += kPointerSize / 2;
3861    representation = Representation::Integer32();
3862  }
3863
3864  Operand operand = FieldOperand(write_register, offset);
3865
3866  if (FLAG_unbox_double_fields && representation.IsDouble()) {
3867    DCHECK(access.IsInobject());
3868    XMMRegister value = ToDoubleRegister(instr->value());
3869    __ Movsd(operand, value);
3870
3871  } else if (instr->value()->IsRegister()) {
3872    Register value = ToRegister(instr->value());
3873    __ Store(operand, value, representation);
3874  } else {
3875    LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
3876    if (IsInteger32Constant(operand_value)) {
3877      DCHECK(!hinstr->NeedsWriteBarrier());
3878      int32_t value = ToInteger32(operand_value);
3879      if (representation.IsSmi()) {
3880        __ Move(operand, Smi::FromInt(value));
3881
3882      } else {
3883        __ movl(operand, Immediate(value));
3884      }
3885
3886    } else if (IsExternalConstant(operand_value)) {
3887      DCHECK(!hinstr->NeedsWriteBarrier());
3888      ExternalReference ptr = ToExternalReference(operand_value);
3889      __ Move(kScratchRegister, ptr);
3890      __ movp(operand, kScratchRegister);
3891    } else {
3892      Handle<Object> handle_value = ToHandle(operand_value);
3893      DCHECK(!hinstr->NeedsWriteBarrier());
3894      __ Move(operand, handle_value);
3895    }
3896  }
3897
3898  if (hinstr->NeedsWriteBarrier()) {
3899    Register value = ToRegister(instr->value());
3900    Register temp = access.IsInobject() ? ToRegister(instr->temp()) : object;
3901    // Update the write barrier for the object for in-object properties.
3902    __ RecordWriteField(write_register,
3903                        offset,
3904                        value,
3905                        temp,
3906                        kSaveFPRegs,
3907                        EMIT_REMEMBERED_SET,
3908                        hinstr->SmiCheckForWriteBarrier(),
3909                        hinstr->PointersToHereCheckForValue());
3910  }
3911}
3912
3913
3914void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
3915  DCHECK(ToRegister(instr->context()).is(rsi));
3916  DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
3917  DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
3918
3919  if (instr->hydrogen()->HasVectorAndSlot()) {
3920    EmitVectorStoreICRegisters<LStoreNamedGeneric>(instr);
3921  }
3922
3923  __ Move(StoreDescriptor::NameRegister(), instr->hydrogen()->name());
3924  Handle<Code> ic = CodeFactory::StoreICInOptimizedCode(
3925                        isolate(), instr->language_mode(),
3926                        instr->hydrogen()->initialization_state()).code();
3927  CallCode(ic, RelocInfo::CODE_TARGET, instr);
3928}
3929
3930
3931void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
3932  Representation representation = instr->hydrogen()->length()->representation();
3933  DCHECK(representation.Equals(instr->hydrogen()->index()->representation()));
3934  DCHECK(representation.IsSmiOrInteger32());
3935
3936  Condition cc = instr->hydrogen()->allow_equality() ? below : below_equal;
3937  if (instr->length()->IsConstantOperand()) {
3938    int32_t length = ToInteger32(LConstantOperand::cast(instr->length()));
3939    Register index = ToRegister(instr->index());
3940    if (representation.IsSmi()) {
3941      __ Cmp(index, Smi::FromInt(length));
3942    } else {
3943      __ cmpl(index, Immediate(length));
3944    }
3945    cc = CommuteCondition(cc);
3946  } else if (instr->index()->IsConstantOperand()) {
3947    int32_t index = ToInteger32(LConstantOperand::cast(instr->index()));
3948    if (instr->length()->IsRegister()) {
3949      Register length = ToRegister(instr->length());
3950      if (representation.IsSmi()) {
3951        __ Cmp(length, Smi::FromInt(index));
3952      } else {
3953        __ cmpl(length, Immediate(index));
3954      }
3955    } else {
3956      Operand length = ToOperand(instr->length());
3957      if (representation.IsSmi()) {
3958        __ Cmp(length, Smi::FromInt(index));
3959      } else {
3960        __ cmpl(length, Immediate(index));
3961      }
3962    }
3963  } else {
3964    Register index = ToRegister(instr->index());
3965    if (instr->length()->IsRegister()) {
3966      Register length = ToRegister(instr->length());
3967      if (representation.IsSmi()) {
3968        __ cmpp(length, index);
3969      } else {
3970        __ cmpl(length, index);
3971      }
3972    } else {
3973      Operand length = ToOperand(instr->length());
3974      if (representation.IsSmi()) {
3975        __ cmpp(length, index);
3976      } else {
3977        __ cmpl(length, index);
3978      }
3979    }
3980  }
3981  if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
3982    Label done;
3983    __ j(NegateCondition(cc), &done, Label::kNear);
3984    __ int3();
3985    __ bind(&done);
3986  } else {
3987    DeoptimizeIf(cc, instr, Deoptimizer::kOutOfBounds);
3988  }
3989}
3990
3991
3992void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
3993  ElementsKind elements_kind = instr->elements_kind();
3994  LOperand* key = instr->key();
3995  if (kPointerSize == kInt32Size && !key->IsConstantOperand()) {
3996    Register key_reg = ToRegister(key);
3997    Representation key_representation =
3998        instr->hydrogen()->key()->representation();
3999    if (ExternalArrayOpRequiresTemp(key_representation, elements_kind)) {
4000      __ SmiToInteger64(key_reg, key_reg);
4001    } else if (instr->hydrogen()->IsDehoisted()) {
4002      // Sign extend key because it could be a 32 bit negative value
4003      // and the dehoisted address computation happens in 64 bits
4004      __ movsxlq(key_reg, key_reg);
4005    }
4006  }
4007  Operand operand(BuildFastArrayOperand(
4008      instr->elements(),
4009      key,
4010      instr->hydrogen()->key()->representation(),
4011      elements_kind,
4012      instr->base_offset()));
4013
4014  if (elements_kind == FLOAT32_ELEMENTS) {
4015    XMMRegister value(ToDoubleRegister(instr->value()));
4016    __ Cvtsd2ss(value, value);
4017    __ Movss(operand, value);
4018  } else if (elements_kind == FLOAT64_ELEMENTS) {
4019    __ Movsd(operand, ToDoubleRegister(instr->value()));
4020  } else {
4021    Register value(ToRegister(instr->value()));
4022    switch (elements_kind) {
4023      case INT8_ELEMENTS:
4024      case UINT8_ELEMENTS:
4025      case UINT8_CLAMPED_ELEMENTS:
4026        __ movb(operand, value);
4027        break;
4028      case INT16_ELEMENTS:
4029      case UINT16_ELEMENTS:
4030        __ movw(operand, value);
4031        break;
4032      case INT32_ELEMENTS:
4033      case UINT32_ELEMENTS:
4034        __ movl(operand, value);
4035        break;
4036      case FLOAT32_ELEMENTS:
4037      case FLOAT64_ELEMENTS:
4038      case FAST_ELEMENTS:
4039      case FAST_SMI_ELEMENTS:
4040      case FAST_DOUBLE_ELEMENTS:
4041      case FAST_HOLEY_ELEMENTS:
4042      case FAST_HOLEY_SMI_ELEMENTS:
4043      case FAST_HOLEY_DOUBLE_ELEMENTS:
4044      case DICTIONARY_ELEMENTS:
4045      case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
4046      case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
4047      case FAST_STRING_WRAPPER_ELEMENTS:
4048      case SLOW_STRING_WRAPPER_ELEMENTS:
4049      case NO_ELEMENTS:
4050        UNREACHABLE();
4051        break;
4052    }
4053  }
4054}
4055
4056
4057void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
4058  XMMRegister value = ToDoubleRegister(instr->value());
4059  LOperand* key = instr->key();
4060  if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
4061      instr->hydrogen()->IsDehoisted()) {
4062    // Sign extend key because it could be a 32 bit negative value
4063    // and the dehoisted address computation happens in 64 bits
4064    __ movsxlq(ToRegister(key), ToRegister(key));
4065  }
4066  if (instr->NeedsCanonicalization()) {
4067    XMMRegister xmm_scratch = double_scratch0();
4068    // Turn potential sNaN value into qNaN.
4069    __ Xorpd(xmm_scratch, xmm_scratch);
4070    __ Subsd(value, xmm_scratch);
4071  }
4072
4073  Operand double_store_operand = BuildFastArrayOperand(
4074      instr->elements(),
4075      key,
4076      instr->hydrogen()->key()->representation(),
4077      FAST_DOUBLE_ELEMENTS,
4078      instr->base_offset());
4079
4080  __ Movsd(double_store_operand, value);
4081}
4082
4083
4084void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4085  HStoreKeyed* hinstr = instr->hydrogen();
4086  LOperand* key = instr->key();
4087  int offset = instr->base_offset();
4088  Representation representation = hinstr->value()->representation();
4089
4090  if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
4091      instr->hydrogen()->IsDehoisted()) {
4092    // Sign extend key because it could be a 32 bit negative value
4093    // and the dehoisted address computation happens in 64 bits
4094    __ movsxlq(ToRegister(key), ToRegister(key));
4095  }
4096  if (representation.IsInteger32() && SmiValuesAre32Bits()) {
4097    DCHECK(hinstr->store_mode() == STORE_TO_INITIALIZED_ENTRY);
4098    DCHECK(hinstr->elements_kind() == FAST_SMI_ELEMENTS);
4099    if (FLAG_debug_code) {
4100      Register scratch = kScratchRegister;
4101      __ Load(scratch,
4102              BuildFastArrayOperand(instr->elements(),
4103                                    key,
4104                                    instr->hydrogen()->key()->representation(),
4105                                    FAST_ELEMENTS,
4106                                    offset),
4107              Representation::Smi());
4108      __ AssertSmi(scratch);
4109    }
4110    // Store int value directly to upper half of the smi.
4111    STATIC_ASSERT(kSmiTag == 0);
4112    DCHECK(kSmiTagSize + kSmiShiftSize == 32);
4113    offset += kPointerSize / 2;
4114  }
4115
4116  Operand operand =
4117      BuildFastArrayOperand(instr->elements(),
4118                            key,
4119                            instr->hydrogen()->key()->representation(),
4120                            FAST_ELEMENTS,
4121                            offset);
4122  if (instr->value()->IsRegister()) {
4123    __ Store(operand, ToRegister(instr->value()), representation);
4124  } else {
4125    LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
4126    if (IsInteger32Constant(operand_value)) {
4127      int32_t value = ToInteger32(operand_value);
4128      if (representation.IsSmi()) {
4129        __ Move(operand, Smi::FromInt(value));
4130
4131      } else {
4132        __ movl(operand, Immediate(value));
4133      }
4134    } else {
4135      Handle<Object> handle_value = ToHandle(operand_value);
4136      __ Move(operand, handle_value);
4137    }
4138  }
4139
4140  if (hinstr->NeedsWriteBarrier()) {
4141    Register elements = ToRegister(instr->elements());
4142    DCHECK(instr->value()->IsRegister());
4143    Register value = ToRegister(instr->value());
4144    DCHECK(!key->IsConstantOperand());
4145    SmiCheck check_needed = hinstr->value()->type().IsHeapObject()
4146            ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4147    // Compute address of modified element and store it into key register.
4148    Register key_reg(ToRegister(key));
4149    __ leap(key_reg, operand);
4150    __ RecordWrite(elements,
4151                   key_reg,
4152                   value,
4153                   kSaveFPRegs,
4154                   EMIT_REMEMBERED_SET,
4155                   check_needed,
4156                   hinstr->PointersToHereCheckForValue());
4157  }
4158}
4159
4160
4161void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4162  if (instr->is_fixed_typed_array()) {
4163    DoStoreKeyedExternalArray(instr);
4164  } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4165    DoStoreKeyedFixedDoubleArray(instr);
4166  } else {
4167    DoStoreKeyedFixedArray(instr);
4168  }
4169}
4170
4171
4172void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4173  DCHECK(ToRegister(instr->context()).is(rsi));
4174  DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4175  DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4176  DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4177
4178  if (instr->hydrogen()->HasVectorAndSlot()) {
4179    EmitVectorStoreICRegisters<LStoreKeyedGeneric>(instr);
4180  }
4181
4182  Handle<Code> ic = CodeFactory::KeyedStoreICInOptimizedCode(
4183                        isolate(), instr->language_mode(),
4184                        instr->hydrogen()->initialization_state()).code();
4185  CallCode(ic, RelocInfo::CODE_TARGET, instr);
4186}
4187
4188
4189void LCodeGen::DoMaybeGrowElements(LMaybeGrowElements* instr) {
4190  class DeferredMaybeGrowElements final : public LDeferredCode {
4191   public:
4192    DeferredMaybeGrowElements(LCodeGen* codegen, LMaybeGrowElements* instr)
4193        : LDeferredCode(codegen), instr_(instr) {}
4194    void Generate() override { codegen()->DoDeferredMaybeGrowElements(instr_); }
4195    LInstruction* instr() override { return instr_; }
4196
4197   private:
4198    LMaybeGrowElements* instr_;
4199  };
4200
4201  Register result = rax;
4202  DeferredMaybeGrowElements* deferred =
4203      new (zone()) DeferredMaybeGrowElements(this, instr);
4204  LOperand* key = instr->key();
4205  LOperand* current_capacity = instr->current_capacity();
4206
4207  DCHECK(instr->hydrogen()->key()->representation().IsInteger32());
4208  DCHECK(instr->hydrogen()->current_capacity()->representation().IsInteger32());
4209  DCHECK(key->IsConstantOperand() || key->IsRegister());
4210  DCHECK(current_capacity->IsConstantOperand() ||
4211         current_capacity->IsRegister());
4212
4213  if (key->IsConstantOperand() && current_capacity->IsConstantOperand()) {
4214    int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4215    int32_t constant_capacity =
4216        ToInteger32(LConstantOperand::cast(current_capacity));
4217    if (constant_key >= constant_capacity) {
4218      // Deferred case.
4219      __ jmp(deferred->entry());
4220    }
4221  } else if (key->IsConstantOperand()) {
4222    int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4223    __ cmpl(ToRegister(current_capacity), Immediate(constant_key));
4224    __ j(less_equal, deferred->entry());
4225  } else if (current_capacity->IsConstantOperand()) {
4226    int32_t constant_capacity =
4227        ToInteger32(LConstantOperand::cast(current_capacity));
4228    __ cmpl(ToRegister(key), Immediate(constant_capacity));
4229    __ j(greater_equal, deferred->entry());
4230  } else {
4231    __ cmpl(ToRegister(key), ToRegister(current_capacity));
4232    __ j(greater_equal, deferred->entry());
4233  }
4234
4235  if (instr->elements()->IsRegister()) {
4236    __ movp(result, ToRegister(instr->elements()));
4237  } else {
4238    __ movp(result, ToOperand(instr->elements()));
4239  }
4240
4241  __ bind(deferred->exit());
4242}
4243
4244
4245void LCodeGen::DoDeferredMaybeGrowElements(LMaybeGrowElements* instr) {
4246  // TODO(3095996): Get rid of this. For now, we need to make the
4247  // result register contain a valid pointer because it is already
4248  // contained in the register pointer map.
4249  Register result = rax;
4250  __ Move(result, Smi::FromInt(0));
4251
4252  // We have to call a stub.
4253  {
4254    PushSafepointRegistersScope scope(this);
4255    if (instr->object()->IsConstantOperand()) {
4256      LConstantOperand* constant_object =
4257          LConstantOperand::cast(instr->object());
4258      if (IsSmiConstant(constant_object)) {
4259        Smi* immediate = ToSmi(constant_object);
4260        __ Move(result, immediate);
4261      } else {
4262        Handle<Object> handle_value = ToHandle(constant_object);
4263        __ Move(result, handle_value);
4264      }
4265    } else if (instr->object()->IsRegister()) {
4266      __ Move(result, ToRegister(instr->object()));
4267    } else {
4268      __ movp(result, ToOperand(instr->object()));
4269    }
4270
4271    LOperand* key = instr->key();
4272    if (key->IsConstantOperand()) {
4273      __ Move(rbx, ToSmi(LConstantOperand::cast(key)));
4274    } else {
4275      __ Move(rbx, ToRegister(key));
4276      __ Integer32ToSmi(rbx, rbx);
4277    }
4278
4279    GrowArrayElementsStub stub(isolate(), instr->hydrogen()->is_js_array(),
4280                               instr->hydrogen()->kind());
4281    __ CallStub(&stub);
4282    RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0);
4283    __ StoreToSafepointRegisterSlot(result, result);
4284  }
4285
4286  // Deopt on smi, which means the elements array changed to dictionary mode.
4287  Condition is_smi = __ CheckSmi(result);
4288  DeoptimizeIf(is_smi, instr, Deoptimizer::kSmi);
4289}
4290
4291
4292void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4293  Register object_reg = ToRegister(instr->object());
4294
4295  Handle<Map> from_map = instr->original_map();
4296  Handle<Map> to_map = instr->transitioned_map();
4297  ElementsKind from_kind = instr->from_kind();
4298  ElementsKind to_kind = instr->to_kind();
4299
4300  Label not_applicable;
4301  __ Cmp(FieldOperand(object_reg, HeapObject::kMapOffset), from_map);
4302  __ j(not_equal, &not_applicable);
4303  if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
4304    Register new_map_reg = ToRegister(instr->new_map_temp());
4305    __ Move(new_map_reg, to_map, RelocInfo::EMBEDDED_OBJECT);
4306    __ movp(FieldOperand(object_reg, HeapObject::kMapOffset), new_map_reg);
4307    // Write barrier.
4308    __ RecordWriteForMap(object_reg, new_map_reg, ToRegister(instr->temp()),
4309                         kDontSaveFPRegs);
4310  } else {
4311    DCHECK(object_reg.is(rax));
4312    DCHECK(ToRegister(instr->context()).is(rsi));
4313    PushSafepointRegistersScope scope(this);
4314    __ Move(rbx, to_map);
4315    bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4316    TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4317    __ CallStub(&stub);
4318    RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0);
4319  }
4320  __ bind(&not_applicable);
4321}
4322
4323
4324void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4325  Register object = ToRegister(instr->object());
4326  Register temp = ToRegister(instr->temp());
4327  Label no_memento_found;
4328  __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found);
4329  DeoptimizeIf(equal, instr, Deoptimizer::kMementoFound);
4330  __ bind(&no_memento_found);
4331}
4332
4333
4334void LCodeGen::DoStringAdd(LStringAdd* instr) {
4335  DCHECK(ToRegister(instr->context()).is(rsi));
4336  DCHECK(ToRegister(instr->left()).is(rdx));
4337  DCHECK(ToRegister(instr->right()).is(rax));
4338  StringAddStub stub(isolate(),
4339                     instr->hydrogen()->flags(),
4340                     instr->hydrogen()->pretenure_flag());
4341  CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4342}
4343
4344
4345void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
4346  class DeferredStringCharCodeAt final : public LDeferredCode {
4347   public:
4348    DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
4349        : LDeferredCode(codegen), instr_(instr) { }
4350    void Generate() override { codegen()->DoDeferredStringCharCodeAt(instr_); }
4351    LInstruction* instr() override { return instr_; }
4352
4353   private:
4354    LStringCharCodeAt* instr_;
4355  };
4356
4357  DeferredStringCharCodeAt* deferred =
4358      new(zone()) DeferredStringCharCodeAt(this, instr);
4359
4360  StringCharLoadGenerator::Generate(masm(),
4361                                    ToRegister(instr->string()),
4362                                    ToRegister(instr->index()),
4363                                    ToRegister(instr->result()),
4364                                    deferred->entry());
4365  __ bind(deferred->exit());
4366}
4367
4368
4369void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4370  Register string = ToRegister(instr->string());
4371  Register result = ToRegister(instr->result());
4372
4373  // TODO(3095996): Get rid of this. For now, we need to make the
4374  // result register contain a valid pointer because it is already
4375  // contained in the register pointer map.
4376  __ Set(result, 0);
4377
4378  PushSafepointRegistersScope scope(this);
4379  __ Push(string);
4380  // Push the index as a smi. This is safe because of the checks in
4381  // DoStringCharCodeAt above.
4382  STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue);
4383  if (instr->index()->IsConstantOperand()) {
4384    int32_t const_index = ToInteger32(LConstantOperand::cast(instr->index()));
4385    __ Push(Smi::FromInt(const_index));
4386  } else {
4387    Register index = ToRegister(instr->index());
4388    __ Integer32ToSmi(index, index);
4389    __ Push(index);
4390  }
4391  CallRuntimeFromDeferred(
4392      Runtime::kStringCharCodeAtRT, 2, instr, instr->context());
4393  __ AssertSmi(rax);
4394  __ SmiToInteger32(rax, rax);
4395  __ StoreToSafepointRegisterSlot(result, rax);
4396}
4397
4398
4399void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
4400  class DeferredStringCharFromCode final : public LDeferredCode {
4401   public:
4402    DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
4403        : LDeferredCode(codegen), instr_(instr) { }
4404    void Generate() override {
4405      codegen()->DoDeferredStringCharFromCode(instr_);
4406    }
4407    LInstruction* instr() override { return instr_; }
4408
4409   private:
4410    LStringCharFromCode* instr_;
4411  };
4412
4413  DeferredStringCharFromCode* deferred =
4414      new(zone()) DeferredStringCharFromCode(this, instr);
4415
4416  DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
4417  Register char_code = ToRegister(instr->char_code());
4418  Register result = ToRegister(instr->result());
4419  DCHECK(!char_code.is(result));
4420
4421  __ cmpl(char_code, Immediate(String::kMaxOneByteCharCode));
4422  __ j(above, deferred->entry());
4423  __ movsxlq(char_code, char_code);
4424  __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
4425  __ movp(result, FieldOperand(result,
4426                               char_code, times_pointer_size,
4427                               FixedArray::kHeaderSize));
4428  __ CompareRoot(result, Heap::kUndefinedValueRootIndex);
4429  __ j(equal, deferred->entry());
4430  __ bind(deferred->exit());
4431}
4432
4433
4434void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4435  Register char_code = ToRegister(instr->char_code());
4436  Register result = ToRegister(instr->result());
4437
4438  // TODO(3095996): Get rid of this. For now, we need to make the
4439  // result register contain a valid pointer because it is already
4440  // contained in the register pointer map.
4441  __ Set(result, 0);
4442
4443  PushSafepointRegistersScope scope(this);
4444  __ Integer32ToSmi(char_code, char_code);
4445  __ Push(char_code);
4446  CallRuntimeFromDeferred(Runtime::kStringCharFromCode, 1, instr,
4447                          instr->context());
4448  __ StoreToSafepointRegisterSlot(result, rax);
4449}
4450
4451
4452void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
4453  LOperand* input = instr->value();
4454  DCHECK(input->IsRegister() || input->IsStackSlot());
4455  LOperand* output = instr->result();
4456  DCHECK(output->IsDoubleRegister());
4457  if (input->IsRegister()) {
4458    __ Cvtlsi2sd(ToDoubleRegister(output), ToRegister(input));
4459  } else {
4460    __ Cvtlsi2sd(ToDoubleRegister(output), ToOperand(input));
4461  }
4462}
4463
4464
4465void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4466  LOperand* input = instr->value();
4467  LOperand* output = instr->result();
4468
4469  __ LoadUint32(ToDoubleRegister(output), ToRegister(input));
4470}
4471
4472
4473void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
4474  class DeferredNumberTagI final : public LDeferredCode {
4475   public:
4476    DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
4477        : LDeferredCode(codegen), instr_(instr) { }
4478    void Generate() override {
4479      codegen()->DoDeferredNumberTagIU(instr_, instr_->value(), instr_->temp1(),
4480                                       instr_->temp2(), SIGNED_INT32);
4481    }
4482    LInstruction* instr() override { return instr_; }
4483
4484   private:
4485    LNumberTagI* instr_;
4486  };
4487
4488  LOperand* input = instr->value();
4489  DCHECK(input->IsRegister() && input->Equals(instr->result()));
4490  Register reg = ToRegister(input);
4491
4492  if (SmiValuesAre32Bits()) {
4493    __ Integer32ToSmi(reg, reg);
4494  } else {
4495    DCHECK(SmiValuesAre31Bits());
4496    DeferredNumberTagI* deferred = new(zone()) DeferredNumberTagI(this, instr);
4497    __ Integer32ToSmi(reg, reg);
4498    __ j(overflow, deferred->entry());
4499    __ bind(deferred->exit());
4500  }
4501}
4502
4503
4504void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4505  class DeferredNumberTagU final : public LDeferredCode {
4506   public:
4507    DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4508        : LDeferredCode(codegen), instr_(instr) { }
4509    void Generate() override {
4510      codegen()->DoDeferredNumberTagIU(instr_, instr_->value(), instr_->temp1(),
4511                                       instr_->temp2(), UNSIGNED_INT32);
4512    }
4513    LInstruction* instr() override { return instr_; }
4514
4515   private:
4516    LNumberTagU* instr_;
4517  };
4518
4519  LOperand* input = instr->value();
4520  DCHECK(input->IsRegister() && input->Equals(instr->result()));
4521  Register reg = ToRegister(input);
4522
4523  DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4524  __ cmpl(reg, Immediate(Smi::kMaxValue));
4525  __ j(above, deferred->entry());
4526  __ Integer32ToSmi(reg, reg);
4527  __ bind(deferred->exit());
4528}
4529
4530
4531void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4532                                     LOperand* value,
4533                                     LOperand* temp1,
4534                                     LOperand* temp2,
4535                                     IntegerSignedness signedness) {
4536  Label done, slow;
4537  Register reg = ToRegister(value);
4538  Register tmp = ToRegister(temp1);
4539  XMMRegister temp_xmm = ToDoubleRegister(temp2);
4540
4541  // Load value into temp_xmm which will be preserved across potential call to
4542  // runtime (MacroAssembler::EnterExitFrameEpilogue preserves only allocatable
4543  // XMM registers on x64).
4544  if (signedness == SIGNED_INT32) {
4545    DCHECK(SmiValuesAre31Bits());
4546    // There was overflow, so bits 30 and 31 of the original integer
4547    // disagree. Try to allocate a heap number in new space and store
4548    // the value in there. If that fails, call the runtime system.
4549    __ SmiToInteger32(reg, reg);
4550    __ xorl(reg, Immediate(0x80000000));
4551    __ Cvtlsi2sd(temp_xmm, reg);
4552  } else {
4553    DCHECK(signedness == UNSIGNED_INT32);
4554    __ LoadUint32(temp_xmm, reg);
4555  }
4556
4557  if (FLAG_inline_new) {
4558    __ AllocateHeapNumber(reg, tmp, &slow);
4559    __ jmp(&done, kPointerSize == kInt64Size ? Label::kNear : Label::kFar);
4560  }
4561
4562  // Slow case: Call the runtime system to do the number allocation.
4563  __ bind(&slow);
4564  {
4565    // Put a valid pointer value in the stack slot where the result
4566    // register is stored, as this register is in the pointer map, but contains
4567    // an integer value.
4568    __ Set(reg, 0);
4569
4570    // Preserve the value of all registers.
4571    PushSafepointRegistersScope scope(this);
4572
4573    // NumberTagIU uses the context from the frame, rather than
4574    // the environment's HContext or HInlinedContext value.
4575    // They only call Runtime::kAllocateHeapNumber.
4576    // The corresponding HChange instructions are added in a phase that does
4577    // not have easy access to the local context.
4578    __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
4579    __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4580    RecordSafepointWithRegisters(
4581        instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4582    __ StoreToSafepointRegisterSlot(reg, rax);
4583  }
4584
4585  // Done. Put the value in temp_xmm into the value of the allocated heap
4586  // number.
4587  __ bind(&done);
4588  __ Movsd(FieldOperand(reg, HeapNumber::kValueOffset), temp_xmm);
4589}
4590
4591
4592void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
4593  class DeferredNumberTagD final : public LDeferredCode {
4594   public:
4595    DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4596        : LDeferredCode(codegen), instr_(instr) { }
4597    void Generate() override { codegen()->DoDeferredNumberTagD(instr_); }
4598    LInstruction* instr() override { return instr_; }
4599
4600   private:
4601    LNumberTagD* instr_;
4602  };
4603
4604  XMMRegister input_reg = ToDoubleRegister(instr->value());
4605  Register reg = ToRegister(instr->result());
4606  Register tmp = ToRegister(instr->temp());
4607
4608  DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
4609  if (FLAG_inline_new) {
4610    __ AllocateHeapNumber(reg, tmp, deferred->entry());
4611  } else {
4612    __ jmp(deferred->entry());
4613  }
4614  __ bind(deferred->exit());
4615  __ Movsd(FieldOperand(reg, HeapNumber::kValueOffset), input_reg);
4616}
4617
4618
4619void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4620  // TODO(3095996): Get rid of this. For now, we need to make the
4621  // result register contain a valid pointer because it is already
4622  // contained in the register pointer map.
4623  Register reg = ToRegister(instr->result());
4624  __ Move(reg, Smi::FromInt(0));
4625
4626  {
4627    PushSafepointRegistersScope scope(this);
4628    // NumberTagD uses the context from the frame, rather than
4629    // the environment's HContext or HInlinedContext value.
4630    // They only call Runtime::kAllocateHeapNumber.
4631    // The corresponding HChange instructions are added in a phase that does
4632    // not have easy access to the local context.
4633    __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
4634    __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4635    RecordSafepointWithRegisters(
4636        instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4637    __ movp(kScratchRegister, rax);
4638  }
4639  __ movp(reg, kScratchRegister);
4640}
4641
4642
4643void LCodeGen::DoSmiTag(LSmiTag* instr) {
4644  HChange* hchange = instr->hydrogen();
4645  Register input = ToRegister(instr->value());
4646  Register output = ToRegister(instr->result());
4647  if (hchange->CheckFlag(HValue::kCanOverflow) &&
4648      hchange->value()->CheckFlag(HValue::kUint32)) {
4649    Condition is_smi = __ CheckUInteger32ValidSmiValue(input);
4650    DeoptimizeIf(NegateCondition(is_smi), instr, Deoptimizer::kOverflow);
4651  }
4652  __ Integer32ToSmi(output, input);
4653  if (hchange->CheckFlag(HValue::kCanOverflow) &&
4654      !hchange->value()->CheckFlag(HValue::kUint32)) {
4655    DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
4656  }
4657}
4658
4659
4660void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4661  DCHECK(instr->value()->Equals(instr->result()));
4662  Register input = ToRegister(instr->value());
4663  if (instr->needs_check()) {
4664    Condition is_smi = __ CheckSmi(input);
4665    DeoptimizeIf(NegateCondition(is_smi), instr, Deoptimizer::kNotASmi);
4666  } else {
4667    __ AssertSmi(input);
4668  }
4669  __ SmiToInteger32(input, input);
4670}
4671
4672
4673void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
4674                                XMMRegister result_reg, NumberUntagDMode mode) {
4675  bool can_convert_undefined_to_nan =
4676      instr->hydrogen()->can_convert_undefined_to_nan();
4677  bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
4678
4679  Label convert, load_smi, done;
4680
4681  if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4682    // Smi check.
4683    __ JumpIfSmi(input_reg, &load_smi, Label::kNear);
4684
4685    // Heap number map check.
4686    __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
4687                   Heap::kHeapNumberMapRootIndex);
4688
4689    // On x64 it is safe to load at heap number offset before evaluating the map
4690    // check, since all heap objects are at least two words long.
4691    __ Movsd(result_reg, FieldOperand(input_reg, HeapNumber::kValueOffset));
4692
4693    if (can_convert_undefined_to_nan) {
4694      __ j(not_equal, &convert, Label::kNear);
4695    } else {
4696      DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
4697    }
4698
4699    if (deoptimize_on_minus_zero) {
4700      XMMRegister xmm_scratch = double_scratch0();
4701      __ Xorpd(xmm_scratch, xmm_scratch);
4702      __ Ucomisd(xmm_scratch, result_reg);
4703      __ j(not_equal, &done, Label::kNear);
4704      __ Movmskpd(kScratchRegister, result_reg);
4705      __ testl(kScratchRegister, Immediate(1));
4706      DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero);
4707    }
4708    __ jmp(&done, Label::kNear);
4709
4710    if (can_convert_undefined_to_nan) {
4711      __ bind(&convert);
4712
4713      // Convert undefined (and hole) to NaN. Compute NaN as 0/0.
4714      __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex);
4715      DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumberUndefined);
4716
4717      __ Pcmpeqd(result_reg, result_reg);
4718      __ jmp(&done, Label::kNear);
4719    }
4720  } else {
4721    DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
4722  }
4723
4724  // Smi to XMM conversion
4725  __ bind(&load_smi);
4726  __ SmiToInteger32(kScratchRegister, input_reg);
4727  __ Cvtlsi2sd(result_reg, kScratchRegister);
4728  __ bind(&done);
4729}
4730
4731
4732void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr, Label* done) {
4733  Register input_reg = ToRegister(instr->value());
4734
4735  if (instr->truncating()) {
4736    Label no_heap_number, check_bools, check_false;
4737
4738    // Heap number map check.
4739    __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
4740                   Heap::kHeapNumberMapRootIndex);
4741    __ j(not_equal, &no_heap_number, Label::kNear);
4742    __ TruncateHeapNumberToI(input_reg, input_reg);
4743    __ jmp(done);
4744
4745    __ bind(&no_heap_number);
4746    // Check for Oddballs. Undefined/False is converted to zero and True to one
4747    // for truncating conversions.
4748    __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex);
4749    __ j(not_equal, &check_bools, Label::kNear);
4750    __ Set(input_reg, 0);
4751    __ jmp(done);
4752
4753    __ bind(&check_bools);
4754    __ CompareRoot(input_reg, Heap::kTrueValueRootIndex);
4755    __ j(not_equal, &check_false, Label::kNear);
4756    __ Set(input_reg, 1);
4757    __ jmp(done);
4758
4759    __ bind(&check_false);
4760    __ CompareRoot(input_reg, Heap::kFalseValueRootIndex);
4761    DeoptimizeIf(not_equal, instr,
4762                 Deoptimizer::kNotAHeapNumberUndefinedBoolean);
4763    __ Set(input_reg, 0);
4764  } else {
4765    XMMRegister scratch = ToDoubleRegister(instr->temp());
4766    DCHECK(!scratch.is(xmm0));
4767    __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
4768                   Heap::kHeapNumberMapRootIndex);
4769    DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
4770    __ Movsd(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset));
4771    __ Cvttsd2si(input_reg, xmm0);
4772    __ Cvtlsi2sd(scratch, input_reg);
4773    __ Ucomisd(xmm0, scratch);
4774    DeoptimizeIf(not_equal, instr, Deoptimizer::kLostPrecision);
4775    DeoptimizeIf(parity_even, instr, Deoptimizer::kNaN);
4776    if (instr->hydrogen()->GetMinusZeroMode() == FAIL_ON_MINUS_ZERO) {
4777      __ testl(input_reg, input_reg);
4778      __ j(not_zero, done);
4779      __ Movmskpd(input_reg, xmm0);
4780      __ andl(input_reg, Immediate(1));
4781      DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero);
4782    }
4783  }
4784}
4785
4786
4787void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
4788  class DeferredTaggedToI final : public LDeferredCode {
4789   public:
4790    DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
4791        : LDeferredCode(codegen), instr_(instr) { }
4792    void Generate() override { codegen()->DoDeferredTaggedToI(instr_, done()); }
4793    LInstruction* instr() override { return instr_; }
4794
4795   private:
4796    LTaggedToI* instr_;
4797  };
4798
4799  LOperand* input = instr->value();
4800  DCHECK(input->IsRegister());
4801  DCHECK(input->Equals(instr->result()));
4802  Register input_reg = ToRegister(input);
4803
4804  if (instr->hydrogen()->value()->representation().IsSmi()) {
4805    __ SmiToInteger32(input_reg, input_reg);
4806  } else {
4807    DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
4808    __ JumpIfNotSmi(input_reg, deferred->entry());
4809    __ SmiToInteger32(input_reg, input_reg);
4810    __ bind(deferred->exit());
4811  }
4812}
4813
4814
4815void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
4816  LOperand* input = instr->value();
4817  DCHECK(input->IsRegister());
4818  LOperand* result = instr->result();
4819  DCHECK(result->IsDoubleRegister());
4820
4821  Register input_reg = ToRegister(input);
4822  XMMRegister result_reg = ToDoubleRegister(result);
4823
4824  HValue* value = instr->hydrogen()->value();
4825  NumberUntagDMode mode = value->representation().IsSmi()
4826      ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
4827
4828  EmitNumberUntagD(instr, input_reg, result_reg, mode);
4829}
4830
4831
4832void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
4833  LOperand* input = instr->value();
4834  DCHECK(input->IsDoubleRegister());
4835  LOperand* result = instr->result();
4836  DCHECK(result->IsRegister());
4837
4838  XMMRegister input_reg = ToDoubleRegister(input);
4839  Register result_reg = ToRegister(result);
4840
4841  if (instr->truncating()) {
4842    __ TruncateDoubleToI(result_reg, input_reg);
4843  } else {
4844    Label lost_precision, is_nan, minus_zero, done;
4845    XMMRegister xmm_scratch = double_scratch0();
4846    Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
4847    __ DoubleToI(result_reg, input_reg, xmm_scratch,
4848                 instr->hydrogen()->GetMinusZeroMode(), &lost_precision,
4849                 &is_nan, &minus_zero, dist);
4850    __ jmp(&done, dist);
4851    __ bind(&lost_precision);
4852    DeoptimizeIf(no_condition, instr, Deoptimizer::kLostPrecision);
4853    __ bind(&is_nan);
4854    DeoptimizeIf(no_condition, instr, Deoptimizer::kNaN);
4855    __ bind(&minus_zero);
4856    DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero);
4857    __ bind(&done);
4858  }
4859}
4860
4861
4862void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
4863  LOperand* input = instr->value();
4864  DCHECK(input->IsDoubleRegister());
4865  LOperand* result = instr->result();
4866  DCHECK(result->IsRegister());
4867
4868  XMMRegister input_reg = ToDoubleRegister(input);
4869  Register result_reg = ToRegister(result);
4870
4871  Label lost_precision, is_nan, minus_zero, done;
4872  XMMRegister xmm_scratch = double_scratch0();
4873  Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
4874  __ DoubleToI(result_reg, input_reg, xmm_scratch,
4875               instr->hydrogen()->GetMinusZeroMode(), &lost_precision, &is_nan,
4876               &minus_zero, dist);
4877  __ jmp(&done, dist);
4878  __ bind(&lost_precision);
4879  DeoptimizeIf(no_condition, instr, Deoptimizer::kLostPrecision);
4880  __ bind(&is_nan);
4881  DeoptimizeIf(no_condition, instr, Deoptimizer::kNaN);
4882  __ bind(&minus_zero);
4883  DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero);
4884  __ bind(&done);
4885  __ Integer32ToSmi(result_reg, result_reg);
4886  DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
4887}
4888
4889
4890void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
4891  LOperand* input = instr->value();
4892  Condition cc = masm()->CheckSmi(ToRegister(input));
4893  DeoptimizeIf(NegateCondition(cc), instr, Deoptimizer::kNotASmi);
4894}
4895
4896
4897void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
4898  if (!instr->hydrogen()->value()->type().IsHeapObject()) {
4899    LOperand* input = instr->value();
4900    Condition cc = masm()->CheckSmi(ToRegister(input));
4901    DeoptimizeIf(cc, instr, Deoptimizer::kSmi);
4902  }
4903}
4904
4905
4906void LCodeGen::DoCheckArrayBufferNotNeutered(
4907    LCheckArrayBufferNotNeutered* instr) {
4908  Register view = ToRegister(instr->view());
4909
4910  __ movp(kScratchRegister,
4911          FieldOperand(view, JSArrayBufferView::kBufferOffset));
4912  __ testb(FieldOperand(kScratchRegister, JSArrayBuffer::kBitFieldOffset),
4913           Immediate(1 << JSArrayBuffer::WasNeutered::kShift));
4914  DeoptimizeIf(not_zero, instr, Deoptimizer::kOutOfBounds);
4915}
4916
4917
4918void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
4919  Register input = ToRegister(instr->value());
4920
4921  __ movp(kScratchRegister, FieldOperand(input, HeapObject::kMapOffset));
4922
4923  if (instr->hydrogen()->is_interval_check()) {
4924    InstanceType first;
4925    InstanceType last;
4926    instr->hydrogen()->GetCheckInterval(&first, &last);
4927
4928    __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
4929            Immediate(static_cast<int8_t>(first)));
4930
4931    // If there is only one type in the interval check for equality.
4932    if (first == last) {
4933      DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongInstanceType);
4934    } else {
4935      DeoptimizeIf(below, instr, Deoptimizer::kWrongInstanceType);
4936      // Omit check for the last type.
4937      if (last != LAST_TYPE) {
4938        __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
4939                Immediate(static_cast<int8_t>(last)));
4940        DeoptimizeIf(above, instr, Deoptimizer::kWrongInstanceType);
4941      }
4942    }
4943  } else {
4944    uint8_t mask;
4945    uint8_t tag;
4946    instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
4947
4948    if (base::bits::IsPowerOfTwo32(mask)) {
4949      DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
4950      __ testb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
4951               Immediate(mask));
4952      DeoptimizeIf(tag == 0 ? not_zero : zero, instr,
4953                   Deoptimizer::kWrongInstanceType);
4954    } else {
4955      __ movzxbl(kScratchRegister,
4956                 FieldOperand(kScratchRegister, Map::kInstanceTypeOffset));
4957      __ andb(kScratchRegister, Immediate(mask));
4958      __ cmpb(kScratchRegister, Immediate(tag));
4959      DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongInstanceType);
4960    }
4961  }
4962}
4963
4964
4965void LCodeGen::DoCheckValue(LCheckValue* instr) {
4966  Register reg = ToRegister(instr->value());
4967  __ Cmp(reg, instr->hydrogen()->object().handle());
4968  DeoptimizeIf(not_equal, instr, Deoptimizer::kValueMismatch);
4969}
4970
4971
4972void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
4973  {
4974    PushSafepointRegistersScope scope(this);
4975    __ Push(object);
4976    __ Set(rsi, 0);
4977    __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
4978    RecordSafepointWithRegisters(
4979        instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
4980
4981    __ testp(rax, Immediate(kSmiTagMask));
4982  }
4983  DeoptimizeIf(zero, instr, Deoptimizer::kInstanceMigrationFailed);
4984}
4985
4986
4987void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
4988  class DeferredCheckMaps final : public LDeferredCode {
4989   public:
4990    DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
4991        : LDeferredCode(codegen), instr_(instr), object_(object) {
4992      SetExit(check_maps());
4993    }
4994    void Generate() override {
4995      codegen()->DoDeferredInstanceMigration(instr_, object_);
4996    }
4997    Label* check_maps() { return &check_maps_; }
4998    LInstruction* instr() override { return instr_; }
4999
5000   private:
5001    LCheckMaps* instr_;
5002    Label check_maps_;
5003    Register object_;
5004  };
5005
5006  if (instr->hydrogen()->IsStabilityCheck()) {
5007    const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5008    for (int i = 0; i < maps->size(); ++i) {
5009      AddStabilityDependency(maps->at(i).handle());
5010    }
5011    return;
5012  }
5013
5014  LOperand* input = instr->value();
5015  DCHECK(input->IsRegister());
5016  Register reg = ToRegister(input);
5017
5018  DeferredCheckMaps* deferred = NULL;
5019  if (instr->hydrogen()->HasMigrationTarget()) {
5020    deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5021    __ bind(deferred->check_maps());
5022  }
5023
5024  const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5025  Label success;
5026  for (int i = 0; i < maps->size() - 1; i++) {
5027    Handle<Map> map = maps->at(i).handle();
5028    __ CompareMap(reg, map);
5029    __ j(equal, &success, Label::kNear);
5030  }
5031
5032  Handle<Map> map = maps->at(maps->size() - 1).handle();
5033  __ CompareMap(reg, map);
5034  if (instr->hydrogen()->HasMigrationTarget()) {
5035    __ j(not_equal, deferred->entry());
5036  } else {
5037    DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongMap);
5038  }
5039
5040  __ bind(&success);
5041}
5042
5043
5044void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
5045  XMMRegister value_reg = ToDoubleRegister(instr->unclamped());
5046  XMMRegister xmm_scratch = double_scratch0();
5047  Register result_reg = ToRegister(instr->result());
5048  __ ClampDoubleToUint8(value_reg, xmm_scratch, result_reg);
5049}
5050
5051
5052void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
5053  DCHECK(instr->unclamped()->Equals(instr->result()));
5054  Register value_reg = ToRegister(instr->result());
5055  __ ClampUint8(value_reg);
5056}
5057
5058
5059void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
5060  DCHECK(instr->unclamped()->Equals(instr->result()));
5061  Register input_reg = ToRegister(instr->unclamped());
5062  XMMRegister temp_xmm_reg = ToDoubleRegister(instr->temp_xmm());
5063  XMMRegister xmm_scratch = double_scratch0();
5064  Label is_smi, done, heap_number;
5065  Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
5066  __ JumpIfSmi(input_reg, &is_smi, dist);
5067
5068  // Check for heap number
5069  __ Cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
5070         factory()->heap_number_map());
5071  __ j(equal, &heap_number, Label::kNear);
5072
5073  // Check for undefined. Undefined is converted to zero for clamping
5074  // conversions.
5075  __ Cmp(input_reg, factory()->undefined_value());
5076  DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumberUndefined);
5077  __ xorl(input_reg, input_reg);
5078  __ jmp(&done, Label::kNear);
5079
5080  // Heap number
5081  __ bind(&heap_number);
5082  __ Movsd(xmm_scratch, FieldOperand(input_reg, HeapNumber::kValueOffset));
5083  __ ClampDoubleToUint8(xmm_scratch, temp_xmm_reg, input_reg);
5084  __ jmp(&done, Label::kNear);
5085
5086  // smi
5087  __ bind(&is_smi);
5088  __ SmiToInteger32(input_reg, input_reg);
5089  __ ClampUint8(input_reg);
5090
5091  __ bind(&done);
5092}
5093
5094
5095void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5096  XMMRegister value_reg = ToDoubleRegister(instr->value());
5097  Register result_reg = ToRegister(instr->result());
5098  if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5099    __ Movq(result_reg, value_reg);
5100    __ shrq(result_reg, Immediate(32));
5101  } else {
5102    __ Movd(result_reg, value_reg);
5103  }
5104}
5105
5106
5107void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5108  Register hi_reg = ToRegister(instr->hi());
5109  Register lo_reg = ToRegister(instr->lo());
5110  XMMRegister result_reg = ToDoubleRegister(instr->result());
5111  __ movl(kScratchRegister, hi_reg);
5112  __ shlq(kScratchRegister, Immediate(32));
5113  __ orq(kScratchRegister, lo_reg);
5114  __ Movq(result_reg, kScratchRegister);
5115}
5116
5117
5118void LCodeGen::DoAllocate(LAllocate* instr) {
5119  class DeferredAllocate final : public LDeferredCode {
5120   public:
5121    DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
5122        : LDeferredCode(codegen), instr_(instr) { }
5123    void Generate() override { codegen()->DoDeferredAllocate(instr_); }
5124    LInstruction* instr() override { return instr_; }
5125
5126   private:
5127    LAllocate* instr_;
5128  };
5129
5130  DeferredAllocate* deferred =
5131      new(zone()) DeferredAllocate(this, instr);
5132
5133  Register result = ToRegister(instr->result());
5134  Register temp = ToRegister(instr->temp());
5135
5136  // Allocate memory for the object.
5137  AllocationFlags flags = TAG_OBJECT;
5138  if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5139    flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5140  }
5141  if (instr->hydrogen()->IsOldSpaceAllocation()) {
5142    DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5143    flags = static_cast<AllocationFlags>(flags | PRETENURE);
5144  }
5145
5146  if (instr->size()->IsConstantOperand()) {
5147    int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5148    CHECK(size <= Page::kMaxRegularHeapObjectSize);
5149    __ Allocate(size, result, temp, no_reg, deferred->entry(), flags);
5150  } else {
5151    Register size = ToRegister(instr->size());
5152    __ Allocate(size, result, temp, no_reg, deferred->entry(), flags);
5153  }
5154
5155  __ bind(deferred->exit());
5156
5157  if (instr->hydrogen()->MustPrefillWithFiller()) {
5158    if (instr->size()->IsConstantOperand()) {
5159      int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5160      __ movl(temp, Immediate((size / kPointerSize) - 1));
5161    } else {
5162      temp = ToRegister(instr->size());
5163      __ sarp(temp, Immediate(kPointerSizeLog2));
5164      __ decl(temp);
5165    }
5166    Label loop;
5167    __ bind(&loop);
5168    __ Move(FieldOperand(result, temp, times_pointer_size, 0),
5169        isolate()->factory()->one_pointer_filler_map());
5170    __ decl(temp);
5171    __ j(not_zero, &loop);
5172  }
5173}
5174
5175
5176void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
5177  Register result = ToRegister(instr->result());
5178
5179  // TODO(3095996): Get rid of this. For now, we need to make the
5180  // result register contain a valid pointer because it is already
5181  // contained in the register pointer map.
5182  __ Move(result, Smi::FromInt(0));
5183
5184  PushSafepointRegistersScope scope(this);
5185  if (instr->size()->IsRegister()) {
5186    Register size = ToRegister(instr->size());
5187    DCHECK(!size.is(result));
5188    __ Integer32ToSmi(size, size);
5189    __ Push(size);
5190  } else {
5191    int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5192    __ Push(Smi::FromInt(size));
5193  }
5194
5195  int flags = 0;
5196  if (instr->hydrogen()->IsOldSpaceAllocation()) {
5197    DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5198    flags = AllocateTargetSpace::update(flags, OLD_SPACE);
5199  } else {
5200    flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5201  }
5202  __ Push(Smi::FromInt(flags));
5203
5204  CallRuntimeFromDeferred(
5205      Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
5206  __ StoreToSafepointRegisterSlot(result, rax);
5207}
5208
5209
5210void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
5211  DCHECK(ToRegister(instr->value()).is(rax));
5212  __ Push(rax);
5213  CallRuntime(Runtime::kToFastProperties, 1, instr);
5214}
5215
5216
5217void LCodeGen::DoTypeof(LTypeof* instr) {
5218  DCHECK(ToRegister(instr->context()).is(rsi));
5219  DCHECK(ToRegister(instr->value()).is(rbx));
5220  Label end, do_call;
5221  Register value_register = ToRegister(instr->value());
5222  __ JumpIfNotSmi(value_register, &do_call);
5223  __ Move(rax, isolate()->factory()->number_string());
5224  __ jmp(&end);
5225  __ bind(&do_call);
5226  TypeofStub stub(isolate());
5227  CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5228  __ bind(&end);
5229}
5230
5231
5232void LCodeGen::EmitPushTaggedOperand(LOperand* operand) {
5233  DCHECK(!operand->IsDoubleRegister());
5234  if (operand->IsConstantOperand()) {
5235    __ Push(ToHandle(LConstantOperand::cast(operand)));
5236  } else if (operand->IsRegister()) {
5237    __ Push(ToRegister(operand));
5238  } else {
5239    __ Push(ToOperand(operand));
5240  }
5241}
5242
5243
5244void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5245  Register input = ToRegister(instr->value());
5246  Condition final_branch_condition = EmitTypeofIs(instr, input);
5247  if (final_branch_condition != no_condition) {
5248    EmitBranch(instr, final_branch_condition);
5249  }
5250}
5251
5252
5253Condition LCodeGen::EmitTypeofIs(LTypeofIsAndBranch* instr, Register input) {
5254  Label* true_label = instr->TrueLabel(chunk_);
5255  Label* false_label = instr->FalseLabel(chunk_);
5256  Handle<String> type_name = instr->type_literal();
5257  int left_block = instr->TrueDestination(chunk_);
5258  int right_block = instr->FalseDestination(chunk_);
5259  int next_block = GetNextEmittedBlock();
5260
5261  Label::Distance true_distance = left_block == next_block ? Label::kNear
5262                                                           : Label::kFar;
5263  Label::Distance false_distance = right_block == next_block ? Label::kNear
5264                                                             : Label::kFar;
5265  Condition final_branch_condition = no_condition;
5266  Factory* factory = isolate()->factory();
5267  if (String::Equals(type_name, factory->number_string())) {
5268    __ JumpIfSmi(input, true_label, true_distance);
5269    __ CompareRoot(FieldOperand(input, HeapObject::kMapOffset),
5270                   Heap::kHeapNumberMapRootIndex);
5271
5272    final_branch_condition = equal;
5273
5274  } else if (String::Equals(type_name, factory->string_string())) {
5275    __ JumpIfSmi(input, false_label, false_distance);
5276    __ CmpObjectType(input, FIRST_NONSTRING_TYPE, input);
5277    final_branch_condition = below;
5278
5279  } else if (String::Equals(type_name, factory->symbol_string())) {
5280    __ JumpIfSmi(input, false_label, false_distance);
5281    __ CmpObjectType(input, SYMBOL_TYPE, input);
5282    final_branch_condition = equal;
5283
5284  } else if (String::Equals(type_name, factory->boolean_string())) {
5285    __ CompareRoot(input, Heap::kTrueValueRootIndex);
5286    __ j(equal, true_label, true_distance);
5287    __ CompareRoot(input, Heap::kFalseValueRootIndex);
5288    final_branch_condition = equal;
5289
5290  } else if (String::Equals(type_name, factory->undefined_string())) {
5291    __ CompareRoot(input, Heap::kNullValueRootIndex);
5292    __ j(equal, false_label, false_distance);
5293    __ JumpIfSmi(input, false_label, false_distance);
5294    // Check for undetectable objects => true.
5295    __ movp(input, FieldOperand(input, HeapObject::kMapOffset));
5296    __ testb(FieldOperand(input, Map::kBitFieldOffset),
5297             Immediate(1 << Map::kIsUndetectable));
5298    final_branch_condition = not_zero;
5299
5300  } else if (String::Equals(type_name, factory->function_string())) {
5301    __ JumpIfSmi(input, false_label, false_distance);
5302    // Check for callable and not undetectable objects => true.
5303    __ movp(input, FieldOperand(input, HeapObject::kMapOffset));
5304    __ movzxbl(input, FieldOperand(input, Map::kBitFieldOffset));
5305    __ andb(input,
5306            Immediate((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable)));
5307    __ cmpb(input, Immediate(1 << Map::kIsCallable));
5308    final_branch_condition = equal;
5309
5310  } else if (String::Equals(type_name, factory->object_string())) {
5311    __ JumpIfSmi(input, false_label, false_distance);
5312    __ CompareRoot(input, Heap::kNullValueRootIndex);
5313    __ j(equal, true_label, true_distance);
5314    STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
5315    __ CmpObjectType(input, FIRST_JS_RECEIVER_TYPE, input);
5316    __ j(below, false_label, false_distance);
5317    // Check for callable or undetectable objects => false.
5318    __ testb(FieldOperand(input, Map::kBitFieldOffset),
5319             Immediate((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable)));
5320    final_branch_condition = zero;
5321
5322// clang-format off
5323#define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type)       \
5324  } else if (String::Equals(type_name, factory->type##_string())) { \
5325    __ JumpIfSmi(input, false_label, false_distance);               \
5326    __ CompareRoot(FieldOperand(input, HeapObject::kMapOffset),     \
5327                   Heap::k##Type##MapRootIndex);                    \
5328    final_branch_condition = equal;
5329  SIMD128_TYPES(SIMD128_TYPE)
5330#undef SIMD128_TYPE
5331    // clang-format on
5332
5333  } else {
5334    __ jmp(false_label, false_distance);
5335  }
5336
5337  return final_branch_condition;
5338}
5339
5340
5341void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5342  if (info()->ShouldEnsureSpaceForLazyDeopt()) {
5343    // Ensure that we have enough space after the previous lazy-bailout
5344    // instruction for patching the code here.
5345    int current_pc = masm()->pc_offset();
5346    if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5347      int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5348      __ Nop(padding_size);
5349    }
5350  }
5351  last_lazy_deopt_pc_ = masm()->pc_offset();
5352}
5353
5354
5355void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
5356  last_lazy_deopt_pc_ = masm()->pc_offset();
5357  DCHECK(instr->HasEnvironment());
5358  LEnvironment* env = instr->environment();
5359  RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5360  safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5361}
5362
5363
5364void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
5365  Deoptimizer::BailoutType type = instr->hydrogen()->type();
5366  // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5367  // needed return address), even though the implementation of LAZY and EAGER is
5368  // now identical. When LAZY is eventually completely folded into EAGER, remove
5369  // the special case below.
5370  if (info()->IsStub() && type == Deoptimizer::EAGER) {
5371    type = Deoptimizer::LAZY;
5372  }
5373  DeoptimizeIf(no_condition, instr, instr->hydrogen()->reason(), type);
5374}
5375
5376
5377void LCodeGen::DoDummy(LDummy* instr) {
5378  // Nothing to see here, move on!
5379}
5380
5381
5382void LCodeGen::DoDummyUse(LDummyUse* instr) {
5383  // Nothing to see here, move on!
5384}
5385
5386
5387void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
5388  PushSafepointRegistersScope scope(this);
5389  __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
5390  __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5391  RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0);
5392  DCHECK(instr->HasEnvironment());
5393  LEnvironment* env = instr->environment();
5394  safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5395}
5396
5397
5398void LCodeGen::DoStackCheck(LStackCheck* instr) {
5399  class DeferredStackCheck final : public LDeferredCode {
5400   public:
5401    DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5402        : LDeferredCode(codegen), instr_(instr) { }
5403    void Generate() override { codegen()->DoDeferredStackCheck(instr_); }
5404    LInstruction* instr() override { return instr_; }
5405
5406   private:
5407    LStackCheck* instr_;
5408  };
5409
5410  DCHECK(instr->HasEnvironment());
5411  LEnvironment* env = instr->environment();
5412  // There is no LLazyBailout instruction for stack-checks. We have to
5413  // prepare for lazy deoptimization explicitly here.
5414  if (instr->hydrogen()->is_function_entry()) {
5415    // Perform stack overflow check.
5416    Label done;
5417    __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
5418    __ j(above_equal, &done, Label::kNear);
5419
5420    DCHECK(instr->context()->IsRegister());
5421    DCHECK(ToRegister(instr->context()).is(rsi));
5422    CallCode(isolate()->builtins()->StackCheck(),
5423             RelocInfo::CODE_TARGET,
5424             instr);
5425    __ bind(&done);
5426  } else {
5427    DCHECK(instr->hydrogen()->is_backwards_branch());
5428    // Perform stack overflow check if this goto needs it before jumping.
5429    DeferredStackCheck* deferred_stack_check =
5430        new(zone()) DeferredStackCheck(this, instr);
5431    __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
5432    __ j(below, deferred_stack_check->entry());
5433    EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
5434    __ bind(instr->done_label());
5435    deferred_stack_check->SetExit(instr->done_label());
5436    RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5437    // Don't record a deoptimization index for the safepoint here.
5438    // This will be done explicitly when emitting call and the safepoint in
5439    // the deferred code.
5440  }
5441}
5442
5443
5444void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
5445  // This is a pseudo-instruction that ensures that the environment here is
5446  // properly registered for deoptimization and records the assembler's PC
5447  // offset.
5448  LEnvironment* environment = instr->environment();
5449
5450  // If the environment were already registered, we would have no way of
5451  // backpatching it with the spill slot operands.
5452  DCHECK(!environment->HasBeenRegistered());
5453  RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
5454
5455  GenerateOsrPrologue();
5456}
5457
5458
5459void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
5460  DCHECK(ToRegister(instr->context()).is(rsi));
5461
5462  Label use_cache, call_runtime;
5463  __ CheckEnumCache(&call_runtime);
5464
5465  __ movp(rax, FieldOperand(rax, HeapObject::kMapOffset));
5466  __ jmp(&use_cache, Label::kNear);
5467
5468  // Get the set of properties to enumerate.
5469  __ bind(&call_runtime);
5470  __ Push(rax);
5471  CallRuntime(Runtime::kForInEnumerate, instr);
5472  __ bind(&use_cache);
5473}
5474
5475
5476void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5477  Register map = ToRegister(instr->map());
5478  Register result = ToRegister(instr->result());
5479  Label load_cache, done;
5480  __ EnumLength(result, map);
5481  __ Cmp(result, Smi::FromInt(0));
5482  __ j(not_equal, &load_cache, Label::kNear);
5483  __ LoadRoot(result, Heap::kEmptyFixedArrayRootIndex);
5484  __ jmp(&done, Label::kNear);
5485  __ bind(&load_cache);
5486  __ LoadInstanceDescriptors(map, result);
5487  __ movp(result,
5488          FieldOperand(result, DescriptorArray::kEnumCacheOffset));
5489  __ movp(result,
5490          FieldOperand(result, FixedArray::SizeFor(instr->idx())));
5491  __ bind(&done);
5492  Condition cc = masm()->CheckSmi(result);
5493  DeoptimizeIf(cc, instr, Deoptimizer::kNoCache);
5494}
5495
5496
5497void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5498  Register object = ToRegister(instr->value());
5499  __ cmpp(ToRegister(instr->map()),
5500          FieldOperand(object, HeapObject::kMapOffset));
5501  DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongMap);
5502}
5503
5504
5505void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5506                                           Register object,
5507                                           Register index) {
5508  PushSafepointRegistersScope scope(this);
5509  __ Push(object);
5510  __ Push(index);
5511  __ xorp(rsi, rsi);
5512  __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
5513  RecordSafepointWithRegisters(
5514      instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
5515  __ StoreToSafepointRegisterSlot(object, rax);
5516}
5517
5518
5519void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
5520  class DeferredLoadMutableDouble final : public LDeferredCode {
5521   public:
5522    DeferredLoadMutableDouble(LCodeGen* codegen,
5523                              LLoadFieldByIndex* instr,
5524                              Register object,
5525                              Register index)
5526        : LDeferredCode(codegen),
5527          instr_(instr),
5528          object_(object),
5529          index_(index) {
5530    }
5531    void Generate() override {
5532      codegen()->DoDeferredLoadMutableDouble(instr_, object_, index_);
5533    }
5534    LInstruction* instr() override { return instr_; }
5535
5536   private:
5537    LLoadFieldByIndex* instr_;
5538    Register object_;
5539    Register index_;
5540  };
5541
5542  Register object = ToRegister(instr->object());
5543  Register index = ToRegister(instr->index());
5544
5545  DeferredLoadMutableDouble* deferred;
5546  deferred = new(zone()) DeferredLoadMutableDouble(this, instr, object, index);
5547
5548  Label out_of_object, done;
5549  __ Move(kScratchRegister, Smi::FromInt(1));
5550  __ testp(index, kScratchRegister);
5551  __ j(not_zero, deferred->entry());
5552
5553  __ sarp(index, Immediate(1));
5554
5555  __ SmiToInteger32(index, index);
5556  __ cmpl(index, Immediate(0));
5557  __ j(less, &out_of_object, Label::kNear);
5558  __ movp(object, FieldOperand(object,
5559                               index,
5560                               times_pointer_size,
5561                               JSObject::kHeaderSize));
5562  __ jmp(&done, Label::kNear);
5563
5564  __ bind(&out_of_object);
5565  __ movp(object, FieldOperand(object, JSObject::kPropertiesOffset));
5566  __ negl(index);
5567  // Index is now equal to out of object property index plus 1.
5568  __ movp(object, FieldOperand(object,
5569                               index,
5570                               times_pointer_size,
5571                               FixedArray::kHeaderSize - kPointerSize));
5572  __ bind(deferred->exit());
5573  __ bind(&done);
5574}
5575
5576
5577void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
5578  Register context = ToRegister(instr->context());
5579  __ movp(Operand(rbp, StandardFrameConstants::kContextOffset), context);
5580}
5581
5582
5583#undef __
5584
5585}  // namespace internal
5586}  // namespace v8
5587
5588#endif  // V8_TARGET_ARCH_X64
5589