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