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