Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / v8 / src / x87 / lithium-x87.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 <sstream>
6
7 #include "src/v8.h"
8
9 #if V8_TARGET_ARCH_X87
10
11 #include "src/hydrogen-osr.h"
12 #include "src/lithium-inl.h"
13 #include "src/x87/lithium-codegen-x87.h"
14
15 namespace v8 {
16 namespace internal {
17
18 #define DEFINE_COMPILE(type)                            \
19   void L##type::CompileToNative(LCodeGen* generator) {  \
20     generator->Do##type(this);                          \
21   }
22 LITHIUM_CONCRETE_INSTRUCTION_LIST(DEFINE_COMPILE)
23 #undef DEFINE_COMPILE
24
25
26 #ifdef DEBUG
27 void LInstruction::VerifyCall() {
28   // Call instructions can use only fixed registers as temporaries and
29   // outputs because all registers are blocked by the calling convention.
30   // Inputs operands must use a fixed register or use-at-start policy or
31   // a non-register policy.
32   DCHECK(Output() == NULL ||
33          LUnallocated::cast(Output())->HasFixedPolicy() ||
34          !LUnallocated::cast(Output())->HasRegisterPolicy());
35   for (UseIterator it(this); !it.Done(); it.Advance()) {
36     LUnallocated* operand = LUnallocated::cast(it.Current());
37     DCHECK(operand->HasFixedPolicy() ||
38            operand->IsUsedAtStart());
39   }
40   for (TempIterator it(this); !it.Done(); it.Advance()) {
41     LUnallocated* operand = LUnallocated::cast(it.Current());
42     DCHECK(operand->HasFixedPolicy() ||!operand->HasRegisterPolicy());
43   }
44 }
45 #endif
46
47
48 bool LInstruction::HasDoubleRegisterResult() {
49   return HasResult() && result()->IsDoubleRegister();
50 }
51
52
53 bool LInstruction::HasDoubleRegisterInput() {
54   for (int i = 0; i < InputCount(); i++) {
55     LOperand* op = InputAt(i);
56     if (op != NULL && op->IsDoubleRegister()) {
57       return true;
58     }
59   }
60   return false;
61 }
62
63
64 bool LInstruction::IsDoubleInput(X87Register reg, LCodeGen* cgen) {
65   for (int i = 0; i < InputCount(); i++) {
66     LOperand* op = InputAt(i);
67     if (op != NULL && op->IsDoubleRegister()) {
68       if (cgen->ToX87Register(op).is(reg)) return true;
69     }
70   }
71   return false;
72 }
73
74
75 void LInstruction::PrintTo(StringStream* stream) {
76   stream->Add("%s ", this->Mnemonic());
77
78   PrintOutputOperandTo(stream);
79
80   PrintDataTo(stream);
81
82   if (HasEnvironment()) {
83     stream->Add(" ");
84     environment()->PrintTo(stream);
85   }
86
87   if (HasPointerMap()) {
88     stream->Add(" ");
89     pointer_map()->PrintTo(stream);
90   }
91 }
92
93
94 void LInstruction::PrintDataTo(StringStream* stream) {
95   stream->Add("= ");
96   for (int i = 0; i < InputCount(); i++) {
97     if (i > 0) stream->Add(" ");
98     if (InputAt(i) == NULL) {
99       stream->Add("NULL");
100     } else {
101       InputAt(i)->PrintTo(stream);
102     }
103   }
104 }
105
106
107 void LInstruction::PrintOutputOperandTo(StringStream* stream) {
108   if (HasResult()) result()->PrintTo(stream);
109 }
110
111
112 void LLabel::PrintDataTo(StringStream* stream) {
113   LGap::PrintDataTo(stream);
114   LLabel* rep = replacement();
115   if (rep != NULL) {
116     stream->Add(" Dead block replaced with B%d", rep->block_id());
117   }
118 }
119
120
121 bool LGap::IsRedundant() const {
122   for (int i = 0; i < 4; i++) {
123     if (parallel_moves_[i] != NULL && !parallel_moves_[i]->IsRedundant()) {
124       return false;
125     }
126   }
127
128   return true;
129 }
130
131
132 void LGap::PrintDataTo(StringStream* stream) {
133   for (int i = 0; i < 4; i++) {
134     stream->Add("(");
135     if (parallel_moves_[i] != NULL) {
136       parallel_moves_[i]->PrintDataTo(stream);
137     }
138     stream->Add(") ");
139   }
140 }
141
142
143 const char* LArithmeticD::Mnemonic() const {
144   switch (op()) {
145     case Token::ADD: return "add-d";
146     case Token::SUB: return "sub-d";
147     case Token::MUL: return "mul-d";
148     case Token::DIV: return "div-d";
149     case Token::MOD: return "mod-d";
150     default:
151       UNREACHABLE();
152       return NULL;
153   }
154 }
155
156
157 const char* LArithmeticT::Mnemonic() const {
158   switch (op()) {
159     case Token::ADD: return "add-t";
160     case Token::SUB: return "sub-t";
161     case Token::MUL: return "mul-t";
162     case Token::MOD: return "mod-t";
163     case Token::DIV: return "div-t";
164     case Token::BIT_AND: return "bit-and-t";
165     case Token::BIT_OR: return "bit-or-t";
166     case Token::BIT_XOR: return "bit-xor-t";
167     case Token::ROR: return "ror-t";
168     case Token::SHL: return "sal-t";
169     case Token::SAR: return "sar-t";
170     case Token::SHR: return "shr-t";
171     default:
172       UNREACHABLE();
173       return NULL;
174   }
175 }
176
177
178 bool LGoto::HasInterestingComment(LCodeGen* gen) const {
179   return !gen->IsNextEmittedBlock(block_id());
180 }
181
182
183 void LGoto::PrintDataTo(StringStream* stream) {
184   stream->Add("B%d", block_id());
185 }
186
187
188 void LBranch::PrintDataTo(StringStream* stream) {
189   stream->Add("B%d | B%d on ", true_block_id(), false_block_id());
190   value()->PrintTo(stream);
191 }
192
193
194 void LCompareNumericAndBranch::PrintDataTo(StringStream* stream) {
195   stream->Add("if ");
196   left()->PrintTo(stream);
197   stream->Add(" %s ", Token::String(op()));
198   right()->PrintTo(stream);
199   stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
200 }
201
202
203 void LIsObjectAndBranch::PrintDataTo(StringStream* stream) {
204   stream->Add("if is_object(");
205   value()->PrintTo(stream);
206   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
207 }
208
209
210 void LIsStringAndBranch::PrintDataTo(StringStream* stream) {
211   stream->Add("if is_string(");
212   value()->PrintTo(stream);
213   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
214 }
215
216
217 void LIsSmiAndBranch::PrintDataTo(StringStream* stream) {
218   stream->Add("if is_smi(");
219   value()->PrintTo(stream);
220   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
221 }
222
223
224 void LIsUndetectableAndBranch::PrintDataTo(StringStream* stream) {
225   stream->Add("if is_undetectable(");
226   value()->PrintTo(stream);
227   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
228 }
229
230
231 void LStringCompareAndBranch::PrintDataTo(StringStream* stream) {
232   stream->Add("if string_compare(");
233   left()->PrintTo(stream);
234   right()->PrintTo(stream);
235   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
236 }
237
238
239 void LHasInstanceTypeAndBranch::PrintDataTo(StringStream* stream) {
240   stream->Add("if has_instance_type(");
241   value()->PrintTo(stream);
242   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
243 }
244
245
246 void LHasCachedArrayIndexAndBranch::PrintDataTo(StringStream* stream) {
247   stream->Add("if has_cached_array_index(");
248   value()->PrintTo(stream);
249   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
250 }
251
252
253 void LClassOfTestAndBranch::PrintDataTo(StringStream* stream) {
254   stream->Add("if class_of_test(");
255   value()->PrintTo(stream);
256   stream->Add(", \"%o\") then B%d else B%d",
257               *hydrogen()->class_name(),
258               true_block_id(),
259               false_block_id());
260 }
261
262
263 void LTypeofIsAndBranch::PrintDataTo(StringStream* stream) {
264   stream->Add("if typeof ");
265   value()->PrintTo(stream);
266   stream->Add(" == \"%s\" then B%d else B%d",
267               hydrogen()->type_literal()->ToCString().get(),
268               true_block_id(), false_block_id());
269 }
270
271
272 void LStoreCodeEntry::PrintDataTo(StringStream* stream) {
273   stream->Add(" = ");
274   function()->PrintTo(stream);
275   stream->Add(".code_entry = ");
276   code_object()->PrintTo(stream);
277 }
278
279
280 void LInnerAllocatedObject::PrintDataTo(StringStream* stream) {
281   stream->Add(" = ");
282   base_object()->PrintTo(stream);
283   stream->Add(" + ");
284   offset()->PrintTo(stream);
285 }
286
287
288 void LCallJSFunction::PrintDataTo(StringStream* stream) {
289   stream->Add("= ");
290   function()->PrintTo(stream);
291   stream->Add("#%d / ", arity());
292 }
293
294
295 void LCallWithDescriptor::PrintDataTo(StringStream* stream) {
296   for (int i = 0; i < InputCount(); i++) {
297     InputAt(i)->PrintTo(stream);
298     stream->Add(" ");
299   }
300   stream->Add("#%d / ", arity());
301 }
302
303
304 void LLoadContextSlot::PrintDataTo(StringStream* stream) {
305   context()->PrintTo(stream);
306   stream->Add("[%d]", slot_index());
307 }
308
309
310 void LStoreContextSlot::PrintDataTo(StringStream* stream) {
311   context()->PrintTo(stream);
312   stream->Add("[%d] <- ", slot_index());
313   value()->PrintTo(stream);
314 }
315
316
317 void LInvokeFunction::PrintDataTo(StringStream* stream) {
318   stream->Add("= ");
319   context()->PrintTo(stream);
320   stream->Add(" ");
321   function()->PrintTo(stream);
322   stream->Add(" #%d / ", arity());
323 }
324
325
326 void LCallNew::PrintDataTo(StringStream* stream) {
327   stream->Add("= ");
328   context()->PrintTo(stream);
329   stream->Add(" ");
330   constructor()->PrintTo(stream);
331   stream->Add(" #%d / ", arity());
332 }
333
334
335 void LCallNewArray::PrintDataTo(StringStream* stream) {
336   stream->Add("= ");
337   context()->PrintTo(stream);
338   stream->Add(" ");
339   constructor()->PrintTo(stream);
340   stream->Add(" #%d / ", arity());
341   ElementsKind kind = hydrogen()->elements_kind();
342   stream->Add(" (%s) ", ElementsKindToString(kind));
343 }
344
345
346 void LAccessArgumentsAt::PrintDataTo(StringStream* stream) {
347   arguments()->PrintTo(stream);
348
349   stream->Add(" length ");
350   length()->PrintTo(stream);
351
352   stream->Add(" index ");
353   index()->PrintTo(stream);
354 }
355
356
357 int LPlatformChunk::GetNextSpillIndex(RegisterKind kind) {
358   // Skip a slot if for a double-width slot.
359   if (kind == DOUBLE_REGISTERS) {
360     spill_slot_count_++;
361     spill_slot_count_ |= 1;
362     num_double_slots_++;
363   }
364   return spill_slot_count_++;
365 }
366
367
368 LOperand* LPlatformChunk::GetNextSpillSlot(RegisterKind kind) {
369   int index = GetNextSpillIndex(kind);
370   if (kind == DOUBLE_REGISTERS) {
371     return LDoubleStackSlot::Create(index, zone());
372   } else {
373     DCHECK(kind == GENERAL_REGISTERS);
374     return LStackSlot::Create(index, zone());
375   }
376 }
377
378
379 void LStoreNamedField::PrintDataTo(StringStream* stream) {
380   object()->PrintTo(stream);
381   std::ostringstream os;
382   os << hydrogen()->access() << " <- ";
383   stream->Add(os.str().c_str());
384   value()->PrintTo(stream);
385 }
386
387
388 void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
389   object()->PrintTo(stream);
390   stream->Add(".");
391   stream->Add(String::cast(*name())->ToCString().get());
392   stream->Add(" <- ");
393   value()->PrintTo(stream);
394 }
395
396
397 void LLoadKeyed::PrintDataTo(StringStream* stream) {
398   elements()->PrintTo(stream);
399   stream->Add("[");
400   key()->PrintTo(stream);
401   if (hydrogen()->IsDehoisted()) {
402     stream->Add(" + %d]", base_offset());
403   } else {
404     stream->Add("]");
405   }
406 }
407
408
409 void LStoreKeyed::PrintDataTo(StringStream* stream) {
410   elements()->PrintTo(stream);
411   stream->Add("[");
412   key()->PrintTo(stream);
413   if (hydrogen()->IsDehoisted()) {
414     stream->Add(" + %d] <-", base_offset());
415   } else {
416     stream->Add("] <- ");
417   }
418
419   if (value() == NULL) {
420     DCHECK(hydrogen()->IsConstantHoleStore() &&
421            hydrogen()->value()->representation().IsDouble());
422     stream->Add("<the hole(nan)>");
423   } else {
424     value()->PrintTo(stream);
425   }
426 }
427
428
429 void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
430   object()->PrintTo(stream);
431   stream->Add("[");
432   key()->PrintTo(stream);
433   stream->Add("] <- ");
434   value()->PrintTo(stream);
435 }
436
437
438 void LTransitionElementsKind::PrintDataTo(StringStream* stream) {
439   object()->PrintTo(stream);
440   stream->Add(" %p -> %p", *original_map(), *transitioned_map());
441 }
442
443
444 LPlatformChunk* LChunkBuilder::Build() {
445   DCHECK(is_unused());
446   chunk_ = new(zone()) LPlatformChunk(info(), graph());
447   LPhase phase("L_Building chunk", chunk_);
448   status_ = BUILDING;
449
450   // Reserve the first spill slot for the state of dynamic alignment.
451   if (info()->IsOptimizing()) {
452     int alignment_state_index = chunk_->GetNextSpillIndex(GENERAL_REGISTERS);
453     DCHECK_EQ(alignment_state_index, 0);
454     USE(alignment_state_index);
455   }
456
457   // If compiling for OSR, reserve space for the unoptimized frame,
458   // which will be subsumed into this frame.
459   if (graph()->has_osr()) {
460     for (int i = graph()->osr()->UnoptimizedFrameSlots(); i > 0; i--) {
461       chunk_->GetNextSpillIndex(GENERAL_REGISTERS);
462     }
463   }
464
465   const ZoneList<HBasicBlock*>* blocks = graph()->blocks();
466   for (int i = 0; i < blocks->length(); i++) {
467     HBasicBlock* next = NULL;
468     if (i < blocks->length() - 1) next = blocks->at(i + 1);
469     DoBasicBlock(blocks->at(i), next);
470     if (is_aborted()) return NULL;
471   }
472   status_ = DONE;
473   return chunk_;
474 }
475
476
477 LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
478   return new(zone()) LUnallocated(LUnallocated::FIXED_REGISTER,
479                                   Register::ToAllocationIndex(reg));
480 }
481
482
483 LUnallocated* LChunkBuilder::ToUnallocated(X87Register reg) {
484   return new (zone()) LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
485                                    X87Register::ToAllocationIndex(reg));
486 }
487
488
489 LOperand* LChunkBuilder::UseFixed(HValue* value, Register fixed_register) {
490   return Use(value, ToUnallocated(fixed_register));
491 }
492
493
494 LOperand* LChunkBuilder::UseRegister(HValue* value) {
495   return Use(value, new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
496 }
497
498
499 LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
500   return Use(value,
501              new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
502                                       LUnallocated::USED_AT_START));
503 }
504
505
506 LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
507   return Use(value, new(zone()) LUnallocated(LUnallocated::WRITABLE_REGISTER));
508 }
509
510
511 LOperand* LChunkBuilder::Use(HValue* value) {
512   return Use(value, new(zone()) LUnallocated(LUnallocated::NONE));
513 }
514
515
516 LOperand* LChunkBuilder::UseAtStart(HValue* value) {
517   return Use(value, new(zone()) LUnallocated(LUnallocated::NONE,
518                                              LUnallocated::USED_AT_START));
519 }
520
521
522 static inline bool CanBeImmediateConstant(HValue* value) {
523   return value->IsConstant() && HConstant::cast(value)->NotInNewSpace();
524 }
525
526
527 LOperand* LChunkBuilder::UseOrConstant(HValue* value) {
528   return CanBeImmediateConstant(value)
529       ? chunk_->DefineConstantOperand(HConstant::cast(value))
530       : Use(value);
531 }
532
533
534 LOperand* LChunkBuilder::UseOrConstantAtStart(HValue* value) {
535   return CanBeImmediateConstant(value)
536       ? chunk_->DefineConstantOperand(HConstant::cast(value))
537       : UseAtStart(value);
538 }
539
540
541 LOperand* LChunkBuilder::UseFixedOrConstant(HValue* value,
542                                             Register fixed_register) {
543   return CanBeImmediateConstant(value)
544       ? chunk_->DefineConstantOperand(HConstant::cast(value))
545       : UseFixed(value, fixed_register);
546 }
547
548
549 LOperand* LChunkBuilder::UseRegisterOrConstant(HValue* value) {
550   return CanBeImmediateConstant(value)
551       ? chunk_->DefineConstantOperand(HConstant::cast(value))
552       : UseRegister(value);
553 }
554
555
556 LOperand* LChunkBuilder::UseRegisterOrConstantAtStart(HValue* value) {
557   return CanBeImmediateConstant(value)
558       ? chunk_->DefineConstantOperand(HConstant::cast(value))
559       : UseRegisterAtStart(value);
560 }
561
562
563 LOperand* LChunkBuilder::UseConstant(HValue* value) {
564   return chunk_->DefineConstantOperand(HConstant::cast(value));
565 }
566
567
568 LOperand* LChunkBuilder::UseAny(HValue* value) {
569   return value->IsConstant()
570       ? chunk_->DefineConstantOperand(HConstant::cast(value))
571       :  Use(value, new(zone()) LUnallocated(LUnallocated::ANY));
572 }
573
574
575 LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
576   if (value->EmitAtUses()) {
577     HInstruction* instr = HInstruction::cast(value);
578     VisitInstruction(instr);
579   }
580   operand->set_virtual_register(value->id());
581   return operand;
582 }
583
584
585 LInstruction* LChunkBuilder::Define(LTemplateResultInstruction<1>* instr,
586                                     LUnallocated* result) {
587   result->set_virtual_register(current_instruction_->id());
588   instr->set_result(result);
589   return instr;
590 }
591
592
593 LInstruction* LChunkBuilder::DefineAsRegister(
594     LTemplateResultInstruction<1>* instr) {
595   return Define(instr,
596                 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
597 }
598
599
600 LInstruction* LChunkBuilder::DefineAsSpilled(
601     LTemplateResultInstruction<1>* instr,
602     int index) {
603   return Define(instr,
604                 new(zone()) LUnallocated(LUnallocated::FIXED_SLOT, index));
605 }
606
607
608 LInstruction* LChunkBuilder::DefineSameAsFirst(
609     LTemplateResultInstruction<1>* instr) {
610   return Define(instr,
611                 new(zone()) LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
612 }
613
614
615 LInstruction* LChunkBuilder::DefineFixed(LTemplateResultInstruction<1>* instr,
616                                          Register reg) {
617   return Define(instr, ToUnallocated(reg));
618 }
619
620
621 LInstruction* LChunkBuilder::DefineFixed(LTemplateResultInstruction<1>* instr,
622                                          X87Register reg) {
623   return Define(instr, ToUnallocated(reg));
624 }
625
626
627 LInstruction* LChunkBuilder::AssignEnvironment(LInstruction* instr) {
628   HEnvironment* hydrogen_env = current_block_->last_environment();
629   int argument_index_accumulator = 0;
630   ZoneList<HValue*> objects_to_materialize(0, zone());
631   instr->set_environment(CreateEnvironment(hydrogen_env,
632                                            &argument_index_accumulator,
633                                            &objects_to_materialize));
634   return instr;
635 }
636
637
638 LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
639                                         HInstruction* hinstr,
640                                         CanDeoptimize can_deoptimize) {
641   info()->MarkAsNonDeferredCalling();
642
643 #ifdef DEBUG
644   instr->VerifyCall();
645 #endif
646   instr->MarkAsCall();
647   instr = AssignPointerMap(instr);
648
649   // If instruction does not have side-effects lazy deoptimization
650   // after the call will try to deoptimize to the point before the call.
651   // Thus we still need to attach environment to this call even if
652   // call sequence can not deoptimize eagerly.
653   bool needs_environment =
654       (can_deoptimize == CAN_DEOPTIMIZE_EAGERLY) ||
655       !hinstr->HasObservableSideEffects();
656   if (needs_environment && !instr->HasEnvironment()) {
657     instr = AssignEnvironment(instr);
658     // We can't really figure out if the environment is needed or not.
659     instr->environment()->set_has_been_used();
660   }
661
662   return instr;
663 }
664
665
666 LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
667   DCHECK(!instr->HasPointerMap());
668   instr->set_pointer_map(new(zone()) LPointerMap(zone()));
669   return instr;
670 }
671
672
673 LUnallocated* LChunkBuilder::TempRegister() {
674   LUnallocated* operand =
675       new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
676   int vreg = allocator_->GetVirtualRegister();
677   if (!allocator_->AllocationOk()) {
678     Abort(kOutOfVirtualRegistersWhileTryingToAllocateTempRegister);
679     vreg = 0;
680   }
681   operand->set_virtual_register(vreg);
682   return operand;
683 }
684
685
686 LOperand* LChunkBuilder::FixedTemp(Register reg) {
687   LUnallocated* operand = ToUnallocated(reg);
688   DCHECK(operand->HasFixedPolicy());
689   return operand;
690 }
691
692
693 LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
694   return new(zone()) LLabel(instr->block());
695 }
696
697
698 LInstruction* LChunkBuilder::DoDummyUse(HDummyUse* instr) {
699   return DefineAsRegister(new(zone()) LDummyUse(UseAny(instr->value())));
700 }
701
702
703 LInstruction* LChunkBuilder::DoEnvironmentMarker(HEnvironmentMarker* instr) {
704   UNREACHABLE();
705   return NULL;
706 }
707
708
709 LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
710   return AssignEnvironment(new(zone()) LDeoptimize);
711 }
712
713
714 LInstruction* LChunkBuilder::DoShift(Token::Value op,
715                                      HBitwiseBinaryOperation* instr) {
716   if (instr->representation().IsSmiOrInteger32()) {
717     DCHECK(instr->left()->representation().Equals(instr->representation()));
718     DCHECK(instr->right()->representation().Equals(instr->representation()));
719     LOperand* left = UseRegisterAtStart(instr->left());
720
721     HValue* right_value = instr->right();
722     LOperand* right = NULL;
723     int constant_value = 0;
724     bool does_deopt = false;
725     if (right_value->IsConstant()) {
726       HConstant* constant = HConstant::cast(right_value);
727       right = chunk_->DefineConstantOperand(constant);
728       constant_value = constant->Integer32Value() & 0x1f;
729       // Left shifts can deoptimize if we shift by > 0 and the result cannot be
730       // truncated to smi.
731       if (instr->representation().IsSmi() && constant_value > 0) {
732         does_deopt = !instr->CheckUsesForFlag(HValue::kTruncatingToSmi);
733       }
734     } else {
735       right = UseFixed(right_value, ecx);
736     }
737
738     // Shift operations can only deoptimize if we do a logical shift by 0 and
739     // the result cannot be truncated to int32.
740     if (op == Token::SHR && constant_value == 0) {
741       does_deopt = !instr->CheckFlag(HInstruction::kUint32);
742     }
743
744     LInstruction* result =
745         DefineSameAsFirst(new(zone()) LShiftI(op, left, right, does_deopt));
746     return does_deopt ? AssignEnvironment(result) : result;
747   } else {
748     return DoArithmeticT(op, instr);
749   }
750 }
751
752
753 LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
754                                            HArithmeticBinaryOperation* instr) {
755   DCHECK(instr->representation().IsDouble());
756   DCHECK(instr->left()->representation().IsDouble());
757   DCHECK(instr->right()->representation().IsDouble());
758   if (op == Token::MOD) {
759     LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
760     LOperand* right = UseRegisterAtStart(instr->BetterRightOperand());
761     LArithmeticD* result = new(zone()) LArithmeticD(op, left, right);
762     return MarkAsCall(DefineSameAsFirst(result), instr);
763   } else {
764     LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
765     LOperand* right = UseRegisterAtStart(instr->BetterRightOperand());
766     LArithmeticD* result = new(zone()) LArithmeticD(op, left, right);
767     return DefineSameAsFirst(result);
768   }
769 }
770
771
772 LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
773                                            HBinaryOperation* instr) {
774   HValue* left = instr->left();
775   HValue* right = instr->right();
776   DCHECK(left->representation().IsTagged());
777   DCHECK(right->representation().IsTagged());
778   LOperand* context = UseFixed(instr->context(), esi);
779   LOperand* left_operand = UseFixed(left, edx);
780   LOperand* right_operand = UseFixed(right, eax);
781   LArithmeticT* result =
782       new(zone()) LArithmeticT(op, context, left_operand, right_operand);
783   return MarkAsCall(DefineFixed(result, eax), instr);
784 }
785
786
787 void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
788   DCHECK(is_building());
789   current_block_ = block;
790   next_block_ = next_block;
791   if (block->IsStartBlock()) {
792     block->UpdateEnvironment(graph_->start_environment());
793     argument_count_ = 0;
794   } else if (block->predecessors()->length() == 1) {
795     // We have a single predecessor => copy environment and outgoing
796     // argument count from the predecessor.
797     DCHECK(block->phis()->length() == 0);
798     HBasicBlock* pred = block->predecessors()->at(0);
799     HEnvironment* last_environment = pred->last_environment();
800     DCHECK(last_environment != NULL);
801     // Only copy the environment, if it is later used again.
802     if (pred->end()->SecondSuccessor() == NULL) {
803       DCHECK(pred->end()->FirstSuccessor() == block);
804     } else {
805       if (pred->end()->FirstSuccessor()->block_id() > block->block_id() ||
806           pred->end()->SecondSuccessor()->block_id() > block->block_id()) {
807         last_environment = last_environment->Copy();
808       }
809     }
810     block->UpdateEnvironment(last_environment);
811     DCHECK(pred->argument_count() >= 0);
812     argument_count_ = pred->argument_count();
813   } else {
814     // We are at a state join => process phis.
815     HBasicBlock* pred = block->predecessors()->at(0);
816     // No need to copy the environment, it cannot be used later.
817     HEnvironment* last_environment = pred->last_environment();
818     for (int i = 0; i < block->phis()->length(); ++i) {
819       HPhi* phi = block->phis()->at(i);
820       if (phi->HasMergedIndex()) {
821         last_environment->SetValueAt(phi->merged_index(), phi);
822       }
823     }
824     for (int i = 0; i < block->deleted_phis()->length(); ++i) {
825       if (block->deleted_phis()->at(i) < last_environment->length()) {
826         last_environment->SetValueAt(block->deleted_phis()->at(i),
827                                      graph_->GetConstantUndefined());
828       }
829     }
830     block->UpdateEnvironment(last_environment);
831     // Pick up the outgoing argument count of one of the predecessors.
832     argument_count_ = pred->argument_count();
833   }
834   HInstruction* current = block->first();
835   int start = chunk_->instructions()->length();
836   while (current != NULL && !is_aborted()) {
837     // Code for constants in registers is generated lazily.
838     if (!current->EmitAtUses()) {
839       VisitInstruction(current);
840     }
841     current = current->next();
842   }
843   int end = chunk_->instructions()->length() - 1;
844   if (end >= start) {
845     block->set_first_instruction_index(start);
846     block->set_last_instruction_index(end);
847   }
848   block->set_argument_count(argument_count_);
849   next_block_ = NULL;
850   current_block_ = NULL;
851 }
852
853
854 void LChunkBuilder::VisitInstruction(HInstruction* current) {
855   HInstruction* old_current = current_instruction_;
856   current_instruction_ = current;
857
858   LInstruction* instr = NULL;
859   if (current->CanReplaceWithDummyUses()) {
860     if (current->OperandCount() == 0) {
861       instr = DefineAsRegister(new(zone()) LDummy());
862     } else {
863       DCHECK(!current->OperandAt(0)->IsControlInstruction());
864       instr = DefineAsRegister(new(zone())
865           LDummyUse(UseAny(current->OperandAt(0))));
866     }
867     for (int i = 1; i < current->OperandCount(); ++i) {
868       if (current->OperandAt(i)->IsControlInstruction()) continue;
869       LInstruction* dummy =
870           new(zone()) LDummyUse(UseAny(current->OperandAt(i)));
871       dummy->set_hydrogen_value(current);
872       chunk_->AddInstruction(dummy, current_block_);
873     }
874   } else {
875     HBasicBlock* successor;
876     if (current->IsControlInstruction() &&
877         HControlInstruction::cast(current)->KnownSuccessorBlock(&successor) &&
878         successor != NULL) {
879       // Always insert a fpu register barrier here when branch is optimized to
880       // be a direct goto.
881       // TODO(weiliang): require a better solution.
882       if (!current->IsGoto()) {
883         LClobberDoubles* clobber = new (zone()) LClobberDoubles(isolate());
884         clobber->set_hydrogen_value(current);
885         chunk_->AddInstruction(clobber, current_block_);
886       }
887       instr = new(zone()) LGoto(successor);
888     } else {
889       instr = current->CompileToLithium(this);
890     }
891   }
892
893   argument_count_ += current->argument_delta();
894   DCHECK(argument_count_ >= 0);
895
896   if (instr != NULL) {
897     AddInstruction(instr, current);
898   }
899
900   current_instruction_ = old_current;
901 }
902
903
904 void LChunkBuilder::AddInstruction(LInstruction* instr,
905                                    HInstruction* hydrogen_val) {
906   // Associate the hydrogen instruction first, since we may need it for
907   // the ClobbersRegisters() or ClobbersDoubleRegisters() calls below.
908   instr->set_hydrogen_value(hydrogen_val);
909
910 #if DEBUG
911   // Make sure that the lithium instruction has either no fixed register
912   // constraints in temps or the result OR no uses that are only used at
913   // start. If this invariant doesn't hold, the register allocator can decide
914   // to insert a split of a range immediately before the instruction due to an
915   // already allocated register needing to be used for the instruction's fixed
916   // register constraint. In this case, The register allocator won't see an
917   // interference between the split child and the use-at-start (it would if
918   // the it was just a plain use), so it is free to move the split child into
919   // the same register that is used for the use-at-start.
920   // See https://code.google.com/p/chromium/issues/detail?id=201590
921   if (!(instr->ClobbersRegisters() &&
922         instr->ClobbersDoubleRegisters(isolate()))) {
923     int fixed = 0;
924     int used_at_start = 0;
925     for (UseIterator it(instr); !it.Done(); it.Advance()) {
926       LUnallocated* operand = LUnallocated::cast(it.Current());
927       if (operand->IsUsedAtStart()) ++used_at_start;
928     }
929     if (instr->Output() != NULL) {
930       if (LUnallocated::cast(instr->Output())->HasFixedPolicy()) ++fixed;
931     }
932     for (TempIterator it(instr); !it.Done(); it.Advance()) {
933       LUnallocated* operand = LUnallocated::cast(it.Current());
934       if (operand->HasFixedPolicy()) ++fixed;
935     }
936     DCHECK(fixed == 0 || used_at_start == 0);
937   }
938 #endif
939
940   if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
941     instr = AssignPointerMap(instr);
942   }
943   if (FLAG_stress_environments && !instr->HasEnvironment()) {
944     instr = AssignEnvironment(instr);
945   }
946   if (instr->IsGoto() &&
947       (LGoto::cast(instr)->jumps_to_join() || next_block_->is_osr_entry())) {
948     // TODO(olivf) Since phis of spilled values are joined as registers
949     // (not in the stack slot), we need to allow the goto gaps to keep one
950     // x87 register alive. To ensure all other values are still spilled, we
951     // insert a fpu register barrier right before.
952     LClobberDoubles* clobber = new(zone()) LClobberDoubles(isolate());
953     clobber->set_hydrogen_value(hydrogen_val);
954     chunk_->AddInstruction(clobber, current_block_);
955   }
956   chunk_->AddInstruction(instr, current_block_);
957
958   if (instr->IsCall()) {
959     HValue* hydrogen_value_for_lazy_bailout = hydrogen_val;
960     LInstruction* instruction_needing_environment = NULL;
961     if (hydrogen_val->HasObservableSideEffects()) {
962       HSimulate* sim = HSimulate::cast(hydrogen_val->next());
963       instruction_needing_environment = instr;
964       sim->ReplayEnvironment(current_block_->last_environment());
965       hydrogen_value_for_lazy_bailout = sim;
966     }
967     LInstruction* bailout = AssignEnvironment(new(zone()) LLazyBailout());
968     bailout->set_hydrogen_value(hydrogen_value_for_lazy_bailout);
969     chunk_->AddInstruction(bailout, current_block_);
970     if (instruction_needing_environment != NULL) {
971       // Store the lazy deopt environment with the instruction if needed.
972       // Right now it is only used for LInstanceOfKnownGlobal.
973       instruction_needing_environment->
974           SetDeferredLazyDeoptimizationEnvironment(bailout->environment());
975     }
976   }
977 }
978
979
980 LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
981   return new(zone()) LGoto(instr->FirstSuccessor());
982 }
983
984
985 LInstruction* LChunkBuilder::DoBranch(HBranch* instr) {
986   HValue* value = instr->value();
987   Representation r = value->representation();
988   HType type = value->type();
989   ToBooleanStub::Types expected = instr->expected_input_types();
990   if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
991
992   bool easy_case = !r.IsTagged() || type.IsBoolean() || type.IsSmi() ||
993       type.IsJSArray() || type.IsHeapNumber() || type.IsString();
994   LOperand* temp = !easy_case && expected.NeedsMap() ? TempRegister() : NULL;
995   LInstruction* branch =
996       temp != NULL ? new (zone()) LBranch(UseRegister(value), temp)
997                    : new (zone()) LBranch(UseRegisterAtStart(value), temp);
998   if (!easy_case &&
999       ((!expected.Contains(ToBooleanStub::SMI) && expected.NeedsMap()) ||
1000        !expected.IsGeneric())) {
1001     branch = AssignEnvironment(branch);
1002   }
1003   return branch;
1004 }
1005
1006
1007 LInstruction* LChunkBuilder::DoDebugBreak(HDebugBreak* instr) {
1008   return new(zone()) LDebugBreak();
1009 }
1010
1011
1012 LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
1013   DCHECK(instr->value()->representation().IsTagged());
1014   LOperand* value = UseRegisterAtStart(instr->value());
1015   return new(zone()) LCmpMapAndBranch(value);
1016 }
1017
1018
1019 LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* length) {
1020   info()->MarkAsRequiresFrame();
1021   return DefineAsRegister(new(zone()) LArgumentsLength(Use(length->value())));
1022 }
1023
1024
1025 LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
1026   info()->MarkAsRequiresFrame();
1027   return DefineAsRegister(new(zone()) LArgumentsElements);
1028 }
1029
1030
1031 LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
1032   LOperand* left = UseFixed(instr->left(), InstanceofStub::left());
1033   LOperand* right = UseFixed(instr->right(), InstanceofStub::right());
1034   LOperand* context = UseFixed(instr->context(), esi);
1035   LInstanceOf* result = new(zone()) LInstanceOf(context, left, right);
1036   return MarkAsCall(DefineFixed(result, eax), instr);
1037 }
1038
1039
1040 LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
1041     HInstanceOfKnownGlobal* instr) {
1042   LInstanceOfKnownGlobal* result =
1043       new(zone()) LInstanceOfKnownGlobal(
1044           UseFixed(instr->context(), esi),
1045           UseFixed(instr->left(), InstanceofStub::left()),
1046           FixedTemp(edi));
1047   return MarkAsCall(DefineFixed(result, eax), instr);
1048 }
1049
1050
1051 LInstruction* LChunkBuilder::DoWrapReceiver(HWrapReceiver* instr) {
1052   LOperand* receiver = UseRegister(instr->receiver());
1053   LOperand* function = UseRegister(instr->function());
1054   LOperand* temp = TempRegister();
1055   LWrapReceiver* result =
1056       new(zone()) LWrapReceiver(receiver, function, temp);
1057   return AssignEnvironment(DefineSameAsFirst(result));
1058 }
1059
1060
1061 LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
1062   LOperand* function = UseFixed(instr->function(), edi);
1063   LOperand* receiver = UseFixed(instr->receiver(), eax);
1064   LOperand* length = UseFixed(instr->length(), ebx);
1065   LOperand* elements = UseFixed(instr->elements(), ecx);
1066   LApplyArguments* result = new(zone()) LApplyArguments(function,
1067                                                         receiver,
1068                                                         length,
1069                                                         elements);
1070   return MarkAsCall(DefineFixed(result, eax), instr, CAN_DEOPTIMIZE_EAGERLY);
1071 }
1072
1073
1074 LInstruction* LChunkBuilder::DoPushArguments(HPushArguments* instr) {
1075   int argc = instr->OperandCount();
1076   for (int i = 0; i < argc; ++i) {
1077     LOperand* argument = UseAny(instr->argument(i));
1078     AddInstruction(new(zone()) LPushArgument(argument), instr);
1079   }
1080   return NULL;
1081 }
1082
1083
1084 LInstruction* LChunkBuilder::DoStoreCodeEntry(
1085     HStoreCodeEntry* store_code_entry) {
1086   LOperand* function = UseRegister(store_code_entry->function());
1087   LOperand* code_object = UseTempRegister(store_code_entry->code_object());
1088   return new(zone()) LStoreCodeEntry(function, code_object);
1089 }
1090
1091
1092 LInstruction* LChunkBuilder::DoInnerAllocatedObject(
1093     HInnerAllocatedObject* instr) {
1094   LOperand* base_object = UseRegisterAtStart(instr->base_object());
1095   LOperand* offset = UseRegisterOrConstantAtStart(instr->offset());
1096   return DefineAsRegister(
1097       new(zone()) LInnerAllocatedObject(base_object, offset));
1098 }
1099
1100
1101 LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1102   return instr->HasNoUses()
1103       ? NULL
1104       : DefineAsRegister(new(zone()) LThisFunction);
1105 }
1106
1107
1108 LInstruction* LChunkBuilder::DoContext(HContext* instr) {
1109   if (instr->HasNoUses()) return NULL;
1110
1111   if (info()->IsStub()) {
1112     return DefineFixed(new(zone()) LContext, esi);
1113   }
1114
1115   return DefineAsRegister(new(zone()) LContext);
1116 }
1117
1118
1119 LInstruction* LChunkBuilder::DoDeclareGlobals(HDeclareGlobals* instr) {
1120   LOperand* context = UseFixed(instr->context(), esi);
1121   return MarkAsCall(new(zone()) LDeclareGlobals(context), instr);
1122 }
1123
1124
1125 LInstruction* LChunkBuilder::DoCallJSFunction(
1126     HCallJSFunction* instr) {
1127   LOperand* function = UseFixed(instr->function(), edi);
1128
1129   LCallJSFunction* result = new(zone()) LCallJSFunction(function);
1130
1131   return MarkAsCall(DefineFixed(result, eax), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1132 }
1133
1134
1135 LInstruction* LChunkBuilder::DoCallWithDescriptor(
1136     HCallWithDescriptor* instr) {
1137   CallInterfaceDescriptor descriptor = instr->descriptor();
1138   LOperand* target = UseRegisterOrConstantAtStart(instr->target());
1139   ZoneList<LOperand*> ops(instr->OperandCount(), zone());
1140   ops.Add(target, zone());
1141   for (int i = 1; i < instr->OperandCount(); i++) {
1142     LOperand* op =
1143         UseFixed(instr->OperandAt(i), descriptor.GetParameterRegister(i - 1));
1144     ops.Add(op, zone());
1145   }
1146
1147   LCallWithDescriptor* result = new(zone()) LCallWithDescriptor(
1148       descriptor, ops, zone());
1149   return MarkAsCall(DefineFixed(result, eax), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1150 }
1151
1152
1153 LInstruction* LChunkBuilder::DoTailCallThroughMegamorphicCache(
1154     HTailCallThroughMegamorphicCache* instr) {
1155   LOperand* context = UseFixed(instr->context(), esi);
1156   LOperand* receiver_register =
1157       UseFixed(instr->receiver(), LoadDescriptor::ReceiverRegister());
1158   LOperand* name_register =
1159       UseFixed(instr->name(), LoadDescriptor::NameRegister());
1160   // Not marked as call. It can't deoptimize, and it never returns.
1161   return new (zone()) LTailCallThroughMegamorphicCache(
1162       context, receiver_register, name_register);
1163 }
1164
1165
1166 LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1167   LOperand* context = UseFixed(instr->context(), esi);
1168   LOperand* function = UseFixed(instr->function(), edi);
1169   LInvokeFunction* result = new(zone()) LInvokeFunction(context, function);
1170   return MarkAsCall(DefineFixed(result, eax), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1171 }
1172
1173
1174 LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
1175   switch (instr->op()) {
1176     case kMathFloor: return DoMathFloor(instr);
1177     case kMathRound: return DoMathRound(instr);
1178     case kMathFround: return DoMathFround(instr);
1179     case kMathAbs: return DoMathAbs(instr);
1180     case kMathLog: return DoMathLog(instr);
1181     case kMathExp: return DoMathExp(instr);
1182     case kMathSqrt: return DoMathSqrt(instr);
1183     case kMathPowHalf: return DoMathPowHalf(instr);
1184     case kMathClz32: return DoMathClz32(instr);
1185     default:
1186       UNREACHABLE();
1187       return NULL;
1188   }
1189 }
1190
1191
1192 LInstruction* LChunkBuilder::DoMathFloor(HUnaryMathOperation* instr) {
1193   LOperand* input = UseRegisterAtStart(instr->value());
1194   LMathFloor* result = new(zone()) LMathFloor(input);
1195   return AssignEnvironment(DefineAsRegister(result));
1196 }
1197
1198
1199 LInstruction* LChunkBuilder::DoMathRound(HUnaryMathOperation* instr) {
1200   LOperand* input = UseRegisterAtStart(instr->value());
1201   LInstruction* result = DefineAsRegister(new (zone()) LMathRound(input));
1202   return AssignEnvironment(result);
1203 }
1204
1205
1206 LInstruction* LChunkBuilder::DoMathFround(HUnaryMathOperation* instr) {
1207   LOperand* input = UseRegister(instr->value());
1208   LMathFround* result = new (zone()) LMathFround(input);
1209   return DefineSameAsFirst(result);
1210 }
1211
1212
1213 LInstruction* LChunkBuilder::DoMathAbs(HUnaryMathOperation* instr) {
1214   LOperand* context = UseAny(instr->context());  // Deferred use.
1215   LOperand* input = UseRegisterAtStart(instr->value());
1216   LInstruction* result =
1217       DefineSameAsFirst(new(zone()) LMathAbs(context, input));
1218   Representation r = instr->value()->representation();
1219   if (!r.IsDouble() && !r.IsSmiOrInteger32()) result = AssignPointerMap(result);
1220   if (!r.IsDouble()) result = AssignEnvironment(result);
1221   return result;
1222 }
1223
1224
1225 LInstruction* LChunkBuilder::DoMathLog(HUnaryMathOperation* instr) {
1226   DCHECK(instr->representation().IsDouble());
1227   DCHECK(instr->value()->representation().IsDouble());
1228   LOperand* input = UseRegisterAtStart(instr->value());
1229   return MarkAsCall(DefineSameAsFirst(new(zone()) LMathLog(input)), instr);
1230 }
1231
1232
1233 LInstruction* LChunkBuilder::DoMathClz32(HUnaryMathOperation* instr) {
1234   LOperand* input = UseRegisterAtStart(instr->value());
1235   LMathClz32* result = new(zone()) LMathClz32(input);
1236   return DefineAsRegister(result);
1237 }
1238
1239
1240 LInstruction* LChunkBuilder::DoMathExp(HUnaryMathOperation* instr) {
1241   DCHECK(instr->representation().IsDouble());
1242   DCHECK(instr->value()->representation().IsDouble());
1243   LOperand* value = UseRegisterAtStart(instr->value());
1244   LOperand* temp1 = FixedTemp(ecx);
1245   LOperand* temp2 = FixedTemp(edx);
1246   LMathExp* result = new(zone()) LMathExp(value, temp1, temp2);
1247   return MarkAsCall(DefineSameAsFirst(result), instr);
1248 }
1249
1250
1251 LInstruction* LChunkBuilder::DoMathSqrt(HUnaryMathOperation* instr) {
1252   LOperand* input = UseRegisterAtStart(instr->value());
1253   LOperand* temp1 = FixedTemp(ecx);
1254   LOperand* temp2 = FixedTemp(edx);
1255   LMathSqrt* result = new(zone()) LMathSqrt(input, temp1, temp2);
1256   return MarkAsCall(DefineSameAsFirst(result), instr);
1257 }
1258
1259
1260 LInstruction* LChunkBuilder::DoMathPowHalf(HUnaryMathOperation* instr) {
1261   LOperand* input = UseRegisterAtStart(instr->value());
1262   LMathPowHalf* result = new (zone()) LMathPowHalf(input);
1263   return DefineSameAsFirst(result);
1264 }
1265
1266
1267 LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
1268   LOperand* context = UseFixed(instr->context(), esi);
1269   LOperand* constructor = UseFixed(instr->constructor(), edi);
1270   LCallNew* result = new(zone()) LCallNew(context, constructor);
1271   return MarkAsCall(DefineFixed(result, eax), instr);
1272 }
1273
1274
1275 LInstruction* LChunkBuilder::DoCallNewArray(HCallNewArray* instr) {
1276   LOperand* context = UseFixed(instr->context(), esi);
1277   LOperand* constructor = UseFixed(instr->constructor(), edi);
1278   LCallNewArray* result = new(zone()) LCallNewArray(context, constructor);
1279   return MarkAsCall(DefineFixed(result, eax), instr);
1280 }
1281
1282
1283 LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
1284   LOperand* context = UseFixed(instr->context(), esi);
1285   LOperand* function = UseFixed(instr->function(), edi);
1286   LCallFunction* call = new(zone()) LCallFunction(context, function);
1287   return MarkAsCall(DefineFixed(call, eax), instr);
1288 }
1289
1290
1291 LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1292   LOperand* context = UseFixed(instr->context(), esi);
1293   return MarkAsCall(DefineFixed(new(zone()) LCallRuntime(context), eax), instr);
1294 }
1295
1296
1297 LInstruction* LChunkBuilder::DoRor(HRor* instr) {
1298   return DoShift(Token::ROR, instr);
1299 }
1300
1301
1302 LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1303   return DoShift(Token::SHR, instr);
1304 }
1305
1306
1307 LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1308   return DoShift(Token::SAR, instr);
1309 }
1310
1311
1312 LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1313   return DoShift(Token::SHL, instr);
1314 }
1315
1316
1317 LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
1318   if (instr->representation().IsSmiOrInteger32()) {
1319     DCHECK(instr->left()->representation().Equals(instr->representation()));
1320     DCHECK(instr->right()->representation().Equals(instr->representation()));
1321     DCHECK(instr->CheckFlag(HValue::kTruncatingToInt32));
1322
1323     LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
1324     LOperand* right = UseOrConstantAtStart(instr->BetterRightOperand());
1325     return DefineSameAsFirst(new(zone()) LBitI(left, right));
1326   } else {
1327     return DoArithmeticT(instr->op(), instr);
1328   }
1329 }
1330
1331
1332 LInstruction* LChunkBuilder::DoDivByPowerOf2I(HDiv* instr) {
1333   DCHECK(instr->representation().IsSmiOrInteger32());
1334   DCHECK(instr->left()->representation().Equals(instr->representation()));
1335   DCHECK(instr->right()->representation().Equals(instr->representation()));
1336   LOperand* dividend = UseRegister(instr->left());
1337   int32_t divisor = instr->right()->GetInteger32Constant();
1338   LInstruction* result = DefineAsRegister(new(zone()) LDivByPowerOf2I(
1339           dividend, divisor));
1340   if ((instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1341       (instr->CheckFlag(HValue::kCanOverflow) && divisor == -1) ||
1342       (!instr->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1343        divisor != 1 && divisor != -1)) {
1344     result = AssignEnvironment(result);
1345   }
1346   return result;
1347 }
1348
1349
1350 LInstruction* LChunkBuilder::DoDivByConstI(HDiv* instr) {
1351   DCHECK(instr->representation().IsInteger32());
1352   DCHECK(instr->left()->representation().Equals(instr->representation()));
1353   DCHECK(instr->right()->representation().Equals(instr->representation()));
1354   LOperand* dividend = UseRegister(instr->left());
1355   int32_t divisor = instr->right()->GetInteger32Constant();
1356   LOperand* temp1 = FixedTemp(eax);
1357   LOperand* temp2 = FixedTemp(edx);
1358   LInstruction* result = DefineFixed(new(zone()) LDivByConstI(
1359           dividend, divisor, temp1, temp2), edx);
1360   if (divisor == 0 ||
1361       (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1362       !instr->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1363     result = AssignEnvironment(result);
1364   }
1365   return result;
1366 }
1367
1368
1369 LInstruction* LChunkBuilder::DoDivI(HDiv* instr) {
1370   DCHECK(instr->representation().IsSmiOrInteger32());
1371   DCHECK(instr->left()->representation().Equals(instr->representation()));
1372   DCHECK(instr->right()->representation().Equals(instr->representation()));
1373   LOperand* dividend = UseFixed(instr->left(), eax);
1374   LOperand* divisor = UseRegister(instr->right());
1375   LOperand* temp = FixedTemp(edx);
1376   LInstruction* result = DefineFixed(new(zone()) LDivI(
1377           dividend, divisor, temp), eax);
1378   if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1379       instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1380       instr->CheckFlag(HValue::kCanOverflow) ||
1381       !instr->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1382     result = AssignEnvironment(result);
1383   }
1384   return result;
1385 }
1386
1387
1388 LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
1389   if (instr->representation().IsSmiOrInteger32()) {
1390     if (instr->RightIsPowerOf2()) {
1391       return DoDivByPowerOf2I(instr);
1392     } else if (instr->right()->IsConstant()) {
1393       return DoDivByConstI(instr);
1394     } else {
1395       return DoDivI(instr);
1396     }
1397   } else if (instr->representation().IsDouble()) {
1398     return DoArithmeticD(Token::DIV, instr);
1399   } else {
1400     return DoArithmeticT(Token::DIV, instr);
1401   }
1402 }
1403
1404
1405 LInstruction* LChunkBuilder::DoFlooringDivByPowerOf2I(HMathFloorOfDiv* instr) {
1406   LOperand* dividend = UseRegisterAtStart(instr->left());
1407   int32_t divisor = instr->right()->GetInteger32Constant();
1408   LInstruction* result = DefineSameAsFirst(new(zone()) LFlooringDivByPowerOf2I(
1409           dividend, divisor));
1410   if ((instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1411       (instr->CheckFlag(HValue::kLeftCanBeMinInt) && divisor == -1)) {
1412     result = AssignEnvironment(result);
1413   }
1414   return result;
1415 }
1416
1417
1418 LInstruction* LChunkBuilder::DoFlooringDivByConstI(HMathFloorOfDiv* instr) {
1419   DCHECK(instr->representation().IsInteger32());
1420   DCHECK(instr->left()->representation().Equals(instr->representation()));
1421   DCHECK(instr->right()->representation().Equals(instr->representation()));
1422   LOperand* dividend = UseRegister(instr->left());
1423   int32_t divisor = instr->right()->GetInteger32Constant();
1424   LOperand* temp1 = FixedTemp(eax);
1425   LOperand* temp2 = FixedTemp(edx);
1426   LOperand* temp3 =
1427       ((divisor > 0 && !instr->CheckFlag(HValue::kLeftCanBeNegative)) ||
1428        (divisor < 0 && !instr->CheckFlag(HValue::kLeftCanBePositive))) ?
1429       NULL : TempRegister();
1430   LInstruction* result =
1431       DefineFixed(new(zone()) LFlooringDivByConstI(dividend,
1432                                                    divisor,
1433                                                    temp1,
1434                                                    temp2,
1435                                                    temp3),
1436                   edx);
1437   if (divisor == 0 ||
1438       (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0)) {
1439     result = AssignEnvironment(result);
1440   }
1441   return result;
1442 }
1443
1444
1445 LInstruction* LChunkBuilder::DoFlooringDivI(HMathFloorOfDiv* instr) {
1446   DCHECK(instr->representation().IsSmiOrInteger32());
1447   DCHECK(instr->left()->representation().Equals(instr->representation()));
1448   DCHECK(instr->right()->representation().Equals(instr->representation()));
1449   LOperand* dividend = UseFixed(instr->left(), eax);
1450   LOperand* divisor = UseRegister(instr->right());
1451   LOperand* temp = FixedTemp(edx);
1452   LInstruction* result = DefineFixed(new(zone()) LFlooringDivI(
1453           dividend, divisor, temp), eax);
1454   if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1455       instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1456       instr->CheckFlag(HValue::kCanOverflow)) {
1457     result = AssignEnvironment(result);
1458   }
1459   return result;
1460 }
1461
1462
1463 LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
1464   if (instr->RightIsPowerOf2()) {
1465     return DoFlooringDivByPowerOf2I(instr);
1466   } else if (instr->right()->IsConstant()) {
1467     return DoFlooringDivByConstI(instr);
1468   } else {
1469     return DoFlooringDivI(instr);
1470   }
1471 }
1472
1473
1474 LInstruction* LChunkBuilder::DoModByPowerOf2I(HMod* instr) {
1475   DCHECK(instr->representation().IsSmiOrInteger32());
1476   DCHECK(instr->left()->representation().Equals(instr->representation()));
1477   DCHECK(instr->right()->representation().Equals(instr->representation()));
1478   LOperand* dividend = UseRegisterAtStart(instr->left());
1479   int32_t divisor = instr->right()->GetInteger32Constant();
1480   LInstruction* result = DefineSameAsFirst(new(zone()) LModByPowerOf2I(
1481           dividend, divisor));
1482   if (instr->CheckFlag(HValue::kLeftCanBeNegative) &&
1483       instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1484     result = AssignEnvironment(result);
1485   }
1486   return result;
1487 }
1488
1489
1490 LInstruction* LChunkBuilder::DoModByConstI(HMod* instr) {
1491   DCHECK(instr->representation().IsSmiOrInteger32());
1492   DCHECK(instr->left()->representation().Equals(instr->representation()));
1493   DCHECK(instr->right()->representation().Equals(instr->representation()));
1494   LOperand* dividend = UseRegister(instr->left());
1495   int32_t divisor = instr->right()->GetInteger32Constant();
1496   LOperand* temp1 = FixedTemp(eax);
1497   LOperand* temp2 = FixedTemp(edx);
1498   LInstruction* result = DefineFixed(new(zone()) LModByConstI(
1499           dividend, divisor, temp1, temp2), eax);
1500   if (divisor == 0 || instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1501     result = AssignEnvironment(result);
1502   }
1503   return result;
1504 }
1505
1506
1507 LInstruction* LChunkBuilder::DoModI(HMod* instr) {
1508   DCHECK(instr->representation().IsSmiOrInteger32());
1509   DCHECK(instr->left()->representation().Equals(instr->representation()));
1510   DCHECK(instr->right()->representation().Equals(instr->representation()));
1511   LOperand* dividend = UseFixed(instr->left(), eax);
1512   LOperand* divisor = UseRegister(instr->right());
1513   LOperand* temp = FixedTemp(edx);
1514   LInstruction* result = DefineFixed(new(zone()) LModI(
1515           dividend, divisor, temp), edx);
1516   if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1517       instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1518     result = AssignEnvironment(result);
1519   }
1520   return result;
1521 }
1522
1523
1524 LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1525   if (instr->representation().IsSmiOrInteger32()) {
1526     if (instr->RightIsPowerOf2()) {
1527       return DoModByPowerOf2I(instr);
1528     } else if (instr->right()->IsConstant()) {
1529       return DoModByConstI(instr);
1530     } else {
1531       return DoModI(instr);
1532     }
1533   } else if (instr->representation().IsDouble()) {
1534     return DoArithmeticD(Token::MOD, instr);
1535   } else {
1536     return DoArithmeticT(Token::MOD, instr);
1537   }
1538 }
1539
1540
1541 LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1542   if (instr->representation().IsSmiOrInteger32()) {
1543     DCHECK(instr->left()->representation().Equals(instr->representation()));
1544     DCHECK(instr->right()->representation().Equals(instr->representation()));
1545     LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
1546     LOperand* right = UseOrConstant(instr->BetterRightOperand());
1547     LOperand* temp = NULL;
1548     if (instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1549       temp = TempRegister();
1550     }
1551     LMulI* mul = new(zone()) LMulI(left, right, temp);
1552     if (instr->CheckFlag(HValue::kCanOverflow) ||
1553         instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1554       AssignEnvironment(mul);
1555     }
1556     return DefineSameAsFirst(mul);
1557   } else if (instr->representation().IsDouble()) {
1558     return DoArithmeticD(Token::MUL, instr);
1559   } else {
1560     return DoArithmeticT(Token::MUL, instr);
1561   }
1562 }
1563
1564
1565 LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1566   if (instr->representation().IsSmiOrInteger32()) {
1567     DCHECK(instr->left()->representation().Equals(instr->representation()));
1568     DCHECK(instr->right()->representation().Equals(instr->representation()));
1569     LOperand* left = UseRegisterAtStart(instr->left());
1570     LOperand* right = UseOrConstantAtStart(instr->right());
1571     LSubI* sub = new(zone()) LSubI(left, right);
1572     LInstruction* result = DefineSameAsFirst(sub);
1573     if (instr->CheckFlag(HValue::kCanOverflow)) {
1574       result = AssignEnvironment(result);
1575     }
1576     return result;
1577   } else if (instr->representation().IsDouble()) {
1578     return DoArithmeticD(Token::SUB, instr);
1579   } else {
1580     return DoArithmeticT(Token::SUB, instr);
1581   }
1582 }
1583
1584
1585 LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1586   if (instr->representation().IsSmiOrInteger32()) {
1587     DCHECK(instr->left()->representation().Equals(instr->representation()));
1588     DCHECK(instr->right()->representation().Equals(instr->representation()));
1589     // Check to see if it would be advantageous to use an lea instruction rather
1590     // than an add. This is the case when no overflow check is needed and there
1591     // are multiple uses of the add's inputs, so using a 3-register add will
1592     // preserve all input values for later uses.
1593     bool use_lea = LAddI::UseLea(instr);
1594     LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
1595     HValue* right_candidate = instr->BetterRightOperand();
1596     LOperand* right = use_lea
1597         ? UseRegisterOrConstantAtStart(right_candidate)
1598         : UseOrConstantAtStart(right_candidate);
1599     LAddI* add = new(zone()) LAddI(left, right);
1600     bool can_overflow = instr->CheckFlag(HValue::kCanOverflow);
1601     LInstruction* result = use_lea
1602         ? DefineAsRegister(add)
1603         : DefineSameAsFirst(add);
1604     if (can_overflow) {
1605       result = AssignEnvironment(result);
1606     }
1607     return result;
1608   } else if (instr->representation().IsDouble()) {
1609     return DoArithmeticD(Token::ADD, instr);
1610   } else if (instr->representation().IsExternal()) {
1611     DCHECK(instr->left()->representation().IsExternal());
1612     DCHECK(instr->right()->representation().IsInteger32());
1613     DCHECK(!instr->CheckFlag(HValue::kCanOverflow));
1614     bool use_lea = LAddI::UseLea(instr);
1615     LOperand* left = UseRegisterAtStart(instr->left());
1616     HValue* right_candidate = instr->right();
1617     LOperand* right = use_lea
1618         ? UseRegisterOrConstantAtStart(right_candidate)
1619         : UseOrConstantAtStart(right_candidate);
1620     LAddI* add = new(zone()) LAddI(left, right);
1621     LInstruction* result = use_lea
1622         ? DefineAsRegister(add)
1623         : DefineSameAsFirst(add);
1624     return result;
1625   } else {
1626     return DoArithmeticT(Token::ADD, instr);
1627   }
1628 }
1629
1630
1631 LInstruction* LChunkBuilder::DoMathMinMax(HMathMinMax* instr) {
1632   LOperand* left = NULL;
1633   LOperand* right = NULL;
1634   LOperand* scratch = TempRegister();
1635
1636   if (instr->representation().IsSmiOrInteger32()) {
1637     DCHECK(instr->left()->representation().Equals(instr->representation()));
1638     DCHECK(instr->right()->representation().Equals(instr->representation()));
1639     left = UseRegisterAtStart(instr->BetterLeftOperand());
1640     right = UseOrConstantAtStart(instr->BetterRightOperand());
1641   } else {
1642     DCHECK(instr->representation().IsDouble());
1643     DCHECK(instr->left()->representation().IsDouble());
1644     DCHECK(instr->right()->representation().IsDouble());
1645     left = UseRegisterAtStart(instr->left());
1646     right = UseRegisterAtStart(instr->right());
1647   }
1648   LMathMinMax* minmax = new (zone()) LMathMinMax(left, right, scratch);
1649   return DefineSameAsFirst(minmax);
1650 }
1651
1652
1653 LInstruction* LChunkBuilder::DoPower(HPower* instr) {
1654   // Unlike ia32, we don't have a MathPowStub and directly call c function.
1655   DCHECK(instr->representation().IsDouble());
1656   DCHECK(instr->left()->representation().IsDouble());
1657   LOperand* left = UseRegisterAtStart(instr->left());
1658   LOperand* right = UseRegisterAtStart(instr->right());
1659   LPower* result = new (zone()) LPower(left, right);
1660   return MarkAsCall(DefineSameAsFirst(result), instr);
1661 }
1662
1663
1664 LInstruction* LChunkBuilder::DoCompareGeneric(HCompareGeneric* instr) {
1665   DCHECK(instr->left()->representation().IsSmiOrTagged());
1666   DCHECK(instr->right()->representation().IsSmiOrTagged());
1667   LOperand* context = UseFixed(instr->context(), esi);
1668   LOperand* left = UseFixed(instr->left(), edx);
1669   LOperand* right = UseFixed(instr->right(), eax);
1670   LCmpT* result = new(zone()) LCmpT(context, left, right);
1671   return MarkAsCall(DefineFixed(result, eax), instr);
1672 }
1673
1674
1675 LInstruction* LChunkBuilder::DoCompareNumericAndBranch(
1676     HCompareNumericAndBranch* instr) {
1677   Representation r = instr->representation();
1678   if (r.IsSmiOrInteger32()) {
1679     DCHECK(instr->left()->representation().Equals(r));
1680     DCHECK(instr->right()->representation().Equals(r));
1681     LOperand* left = UseRegisterOrConstantAtStart(instr->left());
1682     LOperand* right = UseOrConstantAtStart(instr->right());
1683     return new(zone()) LCompareNumericAndBranch(left, right);
1684   } else {
1685     DCHECK(r.IsDouble());
1686     DCHECK(instr->left()->representation().IsDouble());
1687     DCHECK(instr->right()->representation().IsDouble());
1688     LOperand* left;
1689     LOperand* right;
1690     if (CanBeImmediateConstant(instr->left()) &&
1691         CanBeImmediateConstant(instr->right())) {
1692       // The code generator requires either both inputs to be constant
1693       // operands, or neither.
1694       left = UseConstant(instr->left());
1695       right = UseConstant(instr->right());
1696     } else {
1697       left = UseRegisterAtStart(instr->left());
1698       right = UseRegisterAtStart(instr->right());
1699     }
1700     return new(zone()) LCompareNumericAndBranch(left, right);
1701   }
1702 }
1703
1704
1705 LInstruction* LChunkBuilder::DoCompareObjectEqAndBranch(
1706     HCompareObjectEqAndBranch* instr) {
1707   LOperand* left = UseRegisterAtStart(instr->left());
1708   LOperand* right = UseOrConstantAtStart(instr->right());
1709   return new(zone()) LCmpObjectEqAndBranch(left, right);
1710 }
1711
1712
1713 LInstruction* LChunkBuilder::DoCompareHoleAndBranch(
1714     HCompareHoleAndBranch* instr) {
1715   LOperand* value = UseRegisterAtStart(instr->value());
1716   return new(zone()) LCmpHoleAndBranch(value);
1717 }
1718
1719
1720 LInstruction* LChunkBuilder::DoCompareMinusZeroAndBranch(
1721     HCompareMinusZeroAndBranch* instr) {
1722   LOperand* value = UseRegisterAtStart(instr->value());
1723   return new (zone()) LCompareMinusZeroAndBranch(value);
1724 }
1725
1726
1727 LInstruction* LChunkBuilder::DoIsObjectAndBranch(HIsObjectAndBranch* instr) {
1728   DCHECK(instr->value()->representation().IsSmiOrTagged());
1729   LOperand* temp = TempRegister();
1730   return new(zone()) LIsObjectAndBranch(UseRegister(instr->value()), temp);
1731 }
1732
1733
1734 LInstruction* LChunkBuilder::DoIsStringAndBranch(HIsStringAndBranch* instr) {
1735   DCHECK(instr->value()->representation().IsTagged());
1736   LOperand* temp = TempRegister();
1737   return new(zone()) LIsStringAndBranch(UseRegister(instr->value()), temp);
1738 }
1739
1740
1741 LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
1742   DCHECK(instr->value()->representation().IsTagged());
1743   return new(zone()) LIsSmiAndBranch(Use(instr->value()));
1744 }
1745
1746
1747 LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
1748     HIsUndetectableAndBranch* instr) {
1749   DCHECK(instr->value()->representation().IsTagged());
1750   return new(zone()) LIsUndetectableAndBranch(
1751       UseRegisterAtStart(instr->value()), TempRegister());
1752 }
1753
1754
1755 LInstruction* LChunkBuilder::DoStringCompareAndBranch(
1756     HStringCompareAndBranch* instr) {
1757   DCHECK(instr->left()->representation().IsTagged());
1758   DCHECK(instr->right()->representation().IsTagged());
1759   LOperand* context = UseFixed(instr->context(), esi);
1760   LOperand* left = UseFixed(instr->left(), edx);
1761   LOperand* right = UseFixed(instr->right(), eax);
1762
1763   LStringCompareAndBranch* result = new(zone())
1764       LStringCompareAndBranch(context, left, right);
1765
1766   return MarkAsCall(result, instr);
1767 }
1768
1769
1770 LInstruction* LChunkBuilder::DoHasInstanceTypeAndBranch(
1771     HHasInstanceTypeAndBranch* instr) {
1772   DCHECK(instr->value()->representation().IsTagged());
1773   return new(zone()) LHasInstanceTypeAndBranch(
1774       UseRegisterAtStart(instr->value()),
1775       TempRegister());
1776 }
1777
1778
1779 LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
1780     HGetCachedArrayIndex* instr)  {
1781   DCHECK(instr->value()->representation().IsTagged());
1782   LOperand* value = UseRegisterAtStart(instr->value());
1783
1784   return DefineAsRegister(new(zone()) LGetCachedArrayIndex(value));
1785 }
1786
1787
1788 LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
1789     HHasCachedArrayIndexAndBranch* instr) {
1790   DCHECK(instr->value()->representation().IsTagged());
1791   return new(zone()) LHasCachedArrayIndexAndBranch(
1792       UseRegisterAtStart(instr->value()));
1793 }
1794
1795
1796 LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
1797     HClassOfTestAndBranch* instr) {
1798   DCHECK(instr->value()->representation().IsTagged());
1799   return new(zone()) LClassOfTestAndBranch(UseRegister(instr->value()),
1800                                            TempRegister(),
1801                                            TempRegister());
1802 }
1803
1804
1805 LInstruction* LChunkBuilder::DoMapEnumLength(HMapEnumLength* instr) {
1806   LOperand* map = UseRegisterAtStart(instr->value());
1807   return DefineAsRegister(new(zone()) LMapEnumLength(map));
1808 }
1809
1810
1811 LInstruction* LChunkBuilder::DoDateField(HDateField* instr) {
1812   LOperand* date = UseFixed(instr->value(), eax);
1813   LDateField* result =
1814       new(zone()) LDateField(date, FixedTemp(ecx), instr->index());
1815   return MarkAsCall(DefineFixed(result, eax), instr, CAN_DEOPTIMIZE_EAGERLY);
1816 }
1817
1818
1819 LInstruction* LChunkBuilder::DoSeqStringGetChar(HSeqStringGetChar* instr) {
1820   LOperand* string = UseRegisterAtStart(instr->string());
1821   LOperand* index = UseRegisterOrConstantAtStart(instr->index());
1822   return DefineAsRegister(new(zone()) LSeqStringGetChar(string, index));
1823 }
1824
1825
1826 LOperand* LChunkBuilder::GetSeqStringSetCharOperand(HSeqStringSetChar* instr) {
1827   if (instr->encoding() == String::ONE_BYTE_ENCODING) {
1828     if (FLAG_debug_code) {
1829       return UseFixed(instr->value(), eax);
1830     } else {
1831       return UseFixedOrConstant(instr->value(), eax);
1832     }
1833   } else {
1834     if (FLAG_debug_code) {
1835       return UseRegisterAtStart(instr->value());
1836     } else {
1837       return UseRegisterOrConstantAtStart(instr->value());
1838     }
1839   }
1840 }
1841
1842
1843 LInstruction* LChunkBuilder::DoSeqStringSetChar(HSeqStringSetChar* instr) {
1844   LOperand* string = UseRegisterAtStart(instr->string());
1845   LOperand* index = FLAG_debug_code
1846       ? UseRegisterAtStart(instr->index())
1847       : UseRegisterOrConstantAtStart(instr->index());
1848   LOperand* value = GetSeqStringSetCharOperand(instr);
1849   LOperand* context = FLAG_debug_code ? UseFixed(instr->context(), esi) : NULL;
1850   LInstruction* result = new(zone()) LSeqStringSetChar(context, string,
1851                                                        index, value);
1852   if (FLAG_debug_code) {
1853     result = MarkAsCall(result, instr);
1854   }
1855   return result;
1856 }
1857
1858
1859 LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
1860   if (!FLAG_debug_code && instr->skip_check()) return NULL;
1861   LOperand* index = UseRegisterOrConstantAtStart(instr->index());
1862   LOperand* length = !index->IsConstantOperand()
1863       ? UseOrConstantAtStart(instr->length())
1864       : UseAtStart(instr->length());
1865   LInstruction* result = new(zone()) LBoundsCheck(index, length);
1866   if (!FLAG_debug_code || !instr->skip_check()) {
1867     result = AssignEnvironment(result);
1868   }
1869   return result;
1870 }
1871
1872
1873 LInstruction* LChunkBuilder::DoBoundsCheckBaseIndexInformation(
1874     HBoundsCheckBaseIndexInformation* instr) {
1875   UNREACHABLE();
1876   return NULL;
1877 }
1878
1879
1880 LInstruction* LChunkBuilder::DoAbnormalExit(HAbnormalExit* instr) {
1881   // The control instruction marking the end of a block that completed
1882   // abruptly (e.g., threw an exception).  There is nothing specific to do.
1883   return NULL;
1884 }
1885
1886
1887 LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
1888   return NULL;
1889 }
1890
1891
1892 LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
1893   // All HForceRepresentation instructions should be eliminated in the
1894   // representation change phase of Hydrogen.
1895   UNREACHABLE();
1896   return NULL;
1897 }
1898
1899
1900 LInstruction* LChunkBuilder::DoChange(HChange* instr) {
1901   Representation from = instr->from();
1902   Representation to = instr->to();
1903   HValue* val = instr->value();
1904   if (from.IsSmi()) {
1905     if (to.IsTagged()) {
1906       LOperand* value = UseRegister(val);
1907       return DefineSameAsFirst(new(zone()) LDummyUse(value));
1908     }
1909     from = Representation::Tagged();
1910   }
1911   if (from.IsTagged()) {
1912     if (to.IsDouble()) {
1913       LOperand* value = UseRegister(val);
1914       LOperand* temp = TempRegister();
1915       LInstruction* result =
1916           DefineAsRegister(new(zone()) LNumberUntagD(value, temp));
1917       if (!val->representation().IsSmi()) result = AssignEnvironment(result);
1918       return result;
1919     } else if (to.IsSmi()) {
1920       LOperand* value = UseRegister(val);
1921       if (val->type().IsSmi()) {
1922         return DefineSameAsFirst(new(zone()) LDummyUse(value));
1923       }
1924       return AssignEnvironment(DefineSameAsFirst(new(zone()) LCheckSmi(value)));
1925     } else {
1926       DCHECK(to.IsInteger32());
1927       if (val->type().IsSmi() || val->representation().IsSmi()) {
1928         LOperand* value = UseRegister(val);
1929         return DefineSameAsFirst(new(zone()) LSmiUntag(value, false));
1930       } else {
1931         LOperand* value = UseRegister(val);
1932         LInstruction* result = DefineSameAsFirst(new(zone()) LTaggedToI(value));
1933         if (!val->representation().IsSmi()) result = AssignEnvironment(result);
1934         return result;
1935       }
1936     }
1937   } else if (from.IsDouble()) {
1938     if (to.IsTagged()) {
1939       info()->MarkAsDeferredCalling();
1940       LOperand* value = UseRegisterAtStart(val);
1941       LOperand* temp = FLAG_inline_new ? TempRegister() : NULL;
1942       LUnallocated* result_temp = TempRegister();
1943       LNumberTagD* result = new(zone()) LNumberTagD(value, temp);
1944       return AssignPointerMap(Define(result, result_temp));
1945     } else if (to.IsSmi()) {
1946       LOperand* value = UseRegister(val);
1947       return AssignEnvironment(
1948           DefineAsRegister(new(zone()) LDoubleToSmi(value)));
1949     } else {
1950       DCHECK(to.IsInteger32());
1951       bool truncating = instr->CanTruncateToInt32();
1952       LOperand* value = UseRegister(val);
1953       LInstruction* result = DefineAsRegister(new(zone()) LDoubleToI(value));
1954       if (!truncating) result = AssignEnvironment(result);
1955       return result;
1956     }
1957   } else if (from.IsInteger32()) {
1958     info()->MarkAsDeferredCalling();
1959     if (to.IsTagged()) {
1960       if (!instr->CheckFlag(HValue::kCanOverflow)) {
1961         LOperand* value = UseRegister(val);
1962         return DefineSameAsFirst(new(zone()) LSmiTag(value));
1963       } else if (val->CheckFlag(HInstruction::kUint32)) {
1964         LOperand* value = UseRegister(val);
1965         LOperand* temp = TempRegister();
1966         LNumberTagU* result = new(zone()) LNumberTagU(value, temp);
1967         return AssignPointerMap(DefineSameAsFirst(result));
1968       } else {
1969         LOperand* value = UseRegister(val);
1970         LOperand* temp = TempRegister();
1971         LNumberTagI* result = new(zone()) LNumberTagI(value, temp);
1972         return AssignPointerMap(DefineSameAsFirst(result));
1973       }
1974     } else if (to.IsSmi()) {
1975       LOperand* value = UseRegister(val);
1976       LInstruction* result = DefineSameAsFirst(new(zone()) LSmiTag(value));
1977       if (instr->CheckFlag(HValue::kCanOverflow)) {
1978         result = AssignEnvironment(result);
1979       }
1980       return result;
1981     } else {
1982       DCHECK(to.IsDouble());
1983       if (val->CheckFlag(HInstruction::kUint32)) {
1984         return DefineAsRegister(new(zone()) LUint32ToDouble(UseRegister(val)));
1985       } else {
1986         return DefineAsRegister(new(zone()) LInteger32ToDouble(Use(val)));
1987       }
1988     }
1989   }
1990   UNREACHABLE();
1991   return NULL;
1992 }
1993
1994
1995 LInstruction* LChunkBuilder::DoCheckHeapObject(HCheckHeapObject* instr) {
1996   LOperand* value = UseAtStart(instr->value());
1997   LInstruction* result = new(zone()) LCheckNonSmi(value);
1998   if (!instr->value()->type().IsHeapObject()) {
1999     result = AssignEnvironment(result);
2000   }
2001   return result;
2002 }
2003
2004
2005 LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
2006   LOperand* value = UseRegisterAtStart(instr->value());
2007   return AssignEnvironment(new(zone()) LCheckSmi(value));
2008 }
2009
2010
2011 LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
2012   LOperand* value = UseRegisterAtStart(instr->value());
2013   LOperand* temp = TempRegister();
2014   LCheckInstanceType* result = new(zone()) LCheckInstanceType(value, temp);
2015   return AssignEnvironment(result);
2016 }
2017
2018
2019 LInstruction* LChunkBuilder::DoCheckValue(HCheckValue* instr) {
2020   // If the object is in new space, we'll emit a global cell compare and so
2021   // want the value in a register.  If the object gets promoted before we
2022   // emit code, we will still get the register but will do an immediate
2023   // compare instead of the cell compare.  This is safe.
2024   LOperand* value = instr->object_in_new_space()
2025       ? UseRegisterAtStart(instr->value()) : UseAtStart(instr->value());
2026   return AssignEnvironment(new(zone()) LCheckValue(value));
2027 }
2028
2029
2030 LInstruction* LChunkBuilder::DoCheckMaps(HCheckMaps* instr) {
2031   if (instr->IsStabilityCheck()) return new(zone()) LCheckMaps;
2032   LOperand* value = UseRegisterAtStart(instr->value());
2033   LInstruction* result = AssignEnvironment(new(zone()) LCheckMaps(value));
2034   if (instr->HasMigrationTarget()) {
2035     info()->MarkAsDeferredCalling();
2036     result = AssignPointerMap(result);
2037   }
2038   return result;
2039 }
2040
2041
2042 LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
2043   HValue* value = instr->value();
2044   Representation input_rep = value->representation();
2045   if (input_rep.IsDouble()) {
2046     LOperand* reg = UseRegister(value);
2047     return DefineFixed(new (zone()) LClampDToUint8(reg), eax);
2048   } else if (input_rep.IsInteger32()) {
2049     LOperand* reg = UseFixed(value, eax);
2050     return DefineFixed(new(zone()) LClampIToUint8(reg), eax);
2051   } else {
2052     DCHECK(input_rep.IsSmiOrTagged());
2053     LOperand* value = UseRegister(instr->value());
2054     LClampTToUint8NoSSE2* res =
2055         new(zone()) LClampTToUint8NoSSE2(value, TempRegister(),
2056                                          TempRegister(), TempRegister());
2057     return AssignEnvironment(DefineFixed(res, ecx));
2058   }
2059 }
2060
2061
2062 LInstruction* LChunkBuilder::DoDoubleBits(HDoubleBits* instr) {
2063   HValue* value = instr->value();
2064   DCHECK(value->representation().IsDouble());
2065   return DefineAsRegister(new(zone()) LDoubleBits(UseRegister(value)));
2066 }
2067
2068
2069 LInstruction* LChunkBuilder::DoConstructDouble(HConstructDouble* instr) {
2070   LOperand* lo = UseRegister(instr->lo());
2071   LOperand* hi = UseRegister(instr->hi());
2072   return DefineAsRegister(new(zone()) LConstructDouble(hi, lo));
2073 }
2074
2075
2076 LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
2077   LOperand* context = info()->IsStub() ? UseFixed(instr->context(), esi) : NULL;
2078   LOperand* parameter_count = UseRegisterOrConstant(instr->parameter_count());
2079   return new(zone()) LReturn(
2080       UseFixed(instr->value(), eax), context, parameter_count);
2081 }
2082
2083
2084 LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
2085   Representation r = instr->representation();
2086   if (r.IsSmi()) {
2087     return DefineAsRegister(new(zone()) LConstantS);
2088   } else if (r.IsInteger32()) {
2089     return DefineAsRegister(new(zone()) LConstantI);
2090   } else if (r.IsDouble()) {
2091     return DefineAsRegister(new (zone()) LConstantD);
2092   } else if (r.IsExternal()) {
2093     return DefineAsRegister(new(zone()) LConstantE);
2094   } else if (r.IsTagged()) {
2095     return DefineAsRegister(new(zone()) LConstantT);
2096   } else {
2097     UNREACHABLE();
2098     return NULL;
2099   }
2100 }
2101
2102
2103 LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
2104   LLoadGlobalCell* result = new(zone()) LLoadGlobalCell;
2105   return instr->RequiresHoleCheck()
2106       ? AssignEnvironment(DefineAsRegister(result))
2107       : DefineAsRegister(result);
2108 }
2109
2110
2111 LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
2112   LOperand* context = UseFixed(instr->context(), esi);
2113   LOperand* global_object =
2114       UseFixed(instr->global_object(), LoadDescriptor::ReceiverRegister());
2115   LOperand* vector = NULL;
2116   if (FLAG_vector_ics) {
2117     vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2118   }
2119
2120   LLoadGlobalGeneric* result =
2121       new(zone()) LLoadGlobalGeneric(context, global_object, vector);
2122   return MarkAsCall(DefineFixed(result, eax), instr);
2123 }
2124
2125
2126 LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
2127   LStoreGlobalCell* result =
2128       new(zone()) LStoreGlobalCell(UseRegister(instr->value()));
2129   return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
2130 }
2131
2132
2133 LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
2134   LOperand* context = UseRegisterAtStart(instr->value());
2135   LInstruction* result =
2136       DefineAsRegister(new(zone()) LLoadContextSlot(context));
2137   if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
2138     result = AssignEnvironment(result);
2139   }
2140   return result;
2141 }
2142
2143
2144 LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
2145   LOperand* value;
2146   LOperand* temp;
2147   LOperand* context = UseRegister(instr->context());
2148   if (instr->NeedsWriteBarrier()) {
2149     value = UseTempRegister(instr->value());
2150     temp = TempRegister();
2151   } else {
2152     value = UseRegister(instr->value());
2153     temp = NULL;
2154   }
2155   LInstruction* result = new(zone()) LStoreContextSlot(context, value, temp);
2156   if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
2157     result = AssignEnvironment(result);
2158   }
2159   return result;
2160 }
2161
2162
2163 LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
2164   LOperand* obj = (instr->access().IsExternalMemory() &&
2165                    instr->access().offset() == 0)
2166       ? UseRegisterOrConstantAtStart(instr->object())
2167       : UseRegisterAtStart(instr->object());
2168   return DefineAsRegister(new(zone()) LLoadNamedField(obj));
2169 }
2170
2171
2172 LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
2173   LOperand* context = UseFixed(instr->context(), esi);
2174   LOperand* object =
2175       UseFixed(instr->object(), LoadDescriptor::ReceiverRegister());
2176   LOperand* vector = NULL;
2177   if (FLAG_vector_ics) {
2178     vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2179   }
2180   LLoadNamedGeneric* result = new(zone()) LLoadNamedGeneric(
2181       context, object, vector);
2182   return MarkAsCall(DefineFixed(result, eax), instr);
2183 }
2184
2185
2186 LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
2187     HLoadFunctionPrototype* instr) {
2188   return AssignEnvironment(DefineAsRegister(
2189       new(zone()) LLoadFunctionPrototype(UseRegister(instr->function()),
2190                                          TempRegister())));
2191 }
2192
2193
2194 LInstruction* LChunkBuilder::DoLoadRoot(HLoadRoot* instr) {
2195   return DefineAsRegister(new(zone()) LLoadRoot);
2196 }
2197
2198
2199 LInstruction* LChunkBuilder::DoLoadKeyed(HLoadKeyed* instr) {
2200   DCHECK(instr->key()->representation().IsSmiOrInteger32());
2201   ElementsKind elements_kind = instr->elements_kind();
2202   bool clobbers_key = ExternalArrayOpRequiresTemp(
2203       instr->key()->representation(), elements_kind);
2204   LOperand* key = clobbers_key
2205       ? UseTempRegister(instr->key())
2206       : UseRegisterOrConstantAtStart(instr->key());
2207   LInstruction* result = NULL;
2208
2209   if (!instr->is_typed_elements()) {
2210     LOperand* obj = UseRegisterAtStart(instr->elements());
2211     result = DefineAsRegister(new(zone()) LLoadKeyed(obj, key));
2212   } else {
2213     DCHECK(
2214         (instr->representation().IsInteger32() &&
2215          !(IsDoubleOrFloatElementsKind(instr->elements_kind()))) ||
2216         (instr->representation().IsDouble() &&
2217          (IsDoubleOrFloatElementsKind(instr->elements_kind()))));
2218     LOperand* backing_store = UseRegister(instr->elements());
2219     result = DefineAsRegister(new(zone()) LLoadKeyed(backing_store, key));
2220   }
2221
2222   if ((instr->is_external() || instr->is_fixed_typed_array()) ?
2223       // see LCodeGen::DoLoadKeyedExternalArray
2224       ((instr->elements_kind() == EXTERNAL_UINT32_ELEMENTS ||
2225         instr->elements_kind() == UINT32_ELEMENTS) &&
2226        !instr->CheckFlag(HInstruction::kUint32)) :
2227       // see LCodeGen::DoLoadKeyedFixedDoubleArray and
2228       // LCodeGen::DoLoadKeyedFixedArray
2229       instr->RequiresHoleCheck()) {
2230     result = AssignEnvironment(result);
2231   }
2232   return result;
2233 }
2234
2235
2236 LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
2237   LOperand* context = UseFixed(instr->context(), esi);
2238   LOperand* object =
2239       UseFixed(instr->object(), LoadDescriptor::ReceiverRegister());
2240   LOperand* key = UseFixed(instr->key(), LoadDescriptor::NameRegister());
2241   LOperand* vector = NULL;
2242   if (FLAG_vector_ics) {
2243     vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2244   }
2245   LLoadKeyedGeneric* result =
2246       new(zone()) LLoadKeyedGeneric(context, object, key, vector);
2247   return MarkAsCall(DefineFixed(result, eax), instr);
2248 }
2249
2250
2251 LOperand* LChunkBuilder::GetStoreKeyedValueOperand(HStoreKeyed* instr) {
2252   ElementsKind elements_kind = instr->elements_kind();
2253
2254   // Determine if we need a byte register in this case for the value.
2255   bool val_is_fixed_register =
2256       elements_kind == EXTERNAL_INT8_ELEMENTS ||
2257       elements_kind == EXTERNAL_UINT8_ELEMENTS ||
2258       elements_kind == EXTERNAL_UINT8_CLAMPED_ELEMENTS ||
2259       elements_kind == UINT8_ELEMENTS ||
2260       elements_kind == INT8_ELEMENTS ||
2261       elements_kind == UINT8_CLAMPED_ELEMENTS;
2262   if (val_is_fixed_register) {
2263     return UseFixed(instr->value(), eax);
2264   }
2265
2266   if (IsDoubleOrFloatElementsKind(elements_kind)) {
2267     return UseRegisterAtStart(instr->value());
2268   }
2269
2270   return UseRegister(instr->value());
2271 }
2272
2273
2274 LInstruction* LChunkBuilder::DoStoreKeyed(HStoreKeyed* instr) {
2275   if (!instr->is_typed_elements()) {
2276     DCHECK(instr->elements()->representation().IsTagged());
2277     DCHECK(instr->key()->representation().IsInteger32() ||
2278            instr->key()->representation().IsSmi());
2279
2280     if (instr->value()->representation().IsDouble()) {
2281       LOperand* object = UseRegisterAtStart(instr->elements());
2282       // For storing double hole, no fp register required.
2283       LOperand* val = instr->IsConstantHoleStore()
2284                           ? NULL
2285                           : UseRegisterAtStart(instr->value());
2286       LOperand* key = UseRegisterOrConstantAtStart(instr->key());
2287       return new(zone()) LStoreKeyed(object, key, val);
2288     } else {
2289       DCHECK(instr->value()->representation().IsSmiOrTagged());
2290       bool needs_write_barrier = instr->NeedsWriteBarrier();
2291
2292       LOperand* obj = UseRegister(instr->elements());
2293       LOperand* val;
2294       LOperand* key;
2295       if (needs_write_barrier) {
2296         val = UseTempRegister(instr->value());
2297         key = UseTempRegister(instr->key());
2298       } else {
2299         val = UseRegisterOrConstantAtStart(instr->value());
2300         key = UseRegisterOrConstantAtStart(instr->key());
2301       }
2302       return new(zone()) LStoreKeyed(obj, key, val);
2303     }
2304   }
2305
2306   ElementsKind elements_kind = instr->elements_kind();
2307   DCHECK(
2308       (instr->value()->representation().IsInteger32() &&
2309        !IsDoubleOrFloatElementsKind(elements_kind)) ||
2310       (instr->value()->representation().IsDouble() &&
2311        IsDoubleOrFloatElementsKind(elements_kind)));
2312   DCHECK((instr->is_fixed_typed_array() &&
2313           instr->elements()->representation().IsTagged()) ||
2314          (instr->is_external() &&
2315           instr->elements()->representation().IsExternal()));
2316
2317   LOperand* backing_store = UseRegister(instr->elements());
2318   LOperand* val = GetStoreKeyedValueOperand(instr);
2319   bool clobbers_key = ExternalArrayOpRequiresTemp(
2320       instr->key()->representation(), elements_kind);
2321   LOperand* key = clobbers_key
2322       ? UseTempRegister(instr->key())
2323       : UseRegisterOrConstantAtStart(instr->key());
2324   return new(zone()) LStoreKeyed(backing_store, key, val);
2325 }
2326
2327
2328 LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
2329   LOperand* context = UseFixed(instr->context(), esi);
2330   LOperand* object =
2331       UseFixed(instr->object(), StoreDescriptor::ReceiverRegister());
2332   LOperand* key = UseFixed(instr->key(), StoreDescriptor::NameRegister());
2333   LOperand* value = UseFixed(instr->value(), StoreDescriptor::ValueRegister());
2334
2335   DCHECK(instr->object()->representation().IsTagged());
2336   DCHECK(instr->key()->representation().IsTagged());
2337   DCHECK(instr->value()->representation().IsTagged());
2338
2339   LStoreKeyedGeneric* result =
2340       new(zone()) LStoreKeyedGeneric(context, object, key, value);
2341   return MarkAsCall(result, instr);
2342 }
2343
2344
2345 LInstruction* LChunkBuilder::DoTransitionElementsKind(
2346     HTransitionElementsKind* instr) {
2347   if (IsSimpleMapChangeTransition(instr->from_kind(), instr->to_kind())) {
2348     LOperand* object = UseRegister(instr->object());
2349     LOperand* new_map_reg = TempRegister();
2350     LOperand* temp_reg = TempRegister();
2351     LTransitionElementsKind* result =
2352         new(zone()) LTransitionElementsKind(object, NULL,
2353                                             new_map_reg, temp_reg);
2354     return result;
2355   } else {
2356     LOperand* object = UseFixed(instr->object(), eax);
2357     LOperand* context = UseFixed(instr->context(), esi);
2358     LTransitionElementsKind* result =
2359         new(zone()) LTransitionElementsKind(object, context, NULL, NULL);
2360     return MarkAsCall(result, instr);
2361   }
2362 }
2363
2364
2365 LInstruction* LChunkBuilder::DoTrapAllocationMemento(
2366     HTrapAllocationMemento* instr) {
2367   LOperand* object = UseRegister(instr->object());
2368   LOperand* temp = TempRegister();
2369   LTrapAllocationMemento* result =
2370       new(zone()) LTrapAllocationMemento(object, temp);
2371   return AssignEnvironment(result);
2372 }
2373
2374
2375 LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
2376   bool is_in_object = instr->access().IsInobject();
2377   bool is_external_location = instr->access().IsExternalMemory() &&
2378       instr->access().offset() == 0;
2379   bool needs_write_barrier = instr->NeedsWriteBarrier();
2380   bool needs_write_barrier_for_map = instr->has_transition() &&
2381       instr->NeedsWriteBarrierForMap();
2382
2383   LOperand* obj;
2384   if (needs_write_barrier) {
2385     obj = is_in_object
2386         ? UseRegister(instr->object())
2387         : UseTempRegister(instr->object());
2388   } else if (is_external_location) {
2389     DCHECK(!is_in_object);
2390     DCHECK(!needs_write_barrier);
2391     DCHECK(!needs_write_barrier_for_map);
2392     obj = UseRegisterOrConstant(instr->object());
2393   } else {
2394     obj = needs_write_barrier_for_map
2395         ? UseRegister(instr->object())
2396         : UseRegisterAtStart(instr->object());
2397   }
2398
2399   bool can_be_constant = instr->value()->IsConstant() &&
2400       HConstant::cast(instr->value())->NotInNewSpace() &&
2401       !instr->field_representation().IsDouble();
2402
2403   LOperand* val;
2404   if (instr->field_representation().IsInteger8() ||
2405       instr->field_representation().IsUInteger8()) {
2406     // mov_b requires a byte register (i.e. any of eax, ebx, ecx, edx).
2407     // Just force the value to be in eax and we're safe here.
2408     val = UseFixed(instr->value(), eax);
2409   } else if (needs_write_barrier) {
2410     val = UseTempRegister(instr->value());
2411   } else if (can_be_constant) {
2412     val = UseRegisterOrConstant(instr->value());
2413   } else if (instr->field_representation().IsDouble()) {
2414     val = UseRegisterAtStart(instr->value());
2415   } else {
2416     val = UseRegister(instr->value());
2417   }
2418
2419   // We only need a scratch register if we have a write barrier or we
2420   // have a store into the properties array (not in-object-property).
2421   LOperand* temp = (!is_in_object || needs_write_barrier ||
2422                     needs_write_barrier_for_map) ? TempRegister() : NULL;
2423
2424   // We need a temporary register for write barrier of the map field.
2425   LOperand* temp_map = needs_write_barrier_for_map ? TempRegister() : NULL;
2426
2427   return new(zone()) LStoreNamedField(obj, val, temp, temp_map);
2428 }
2429
2430
2431 LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
2432   LOperand* context = UseFixed(instr->context(), esi);
2433   LOperand* object =
2434       UseFixed(instr->object(), StoreDescriptor::ReceiverRegister());
2435   LOperand* value = UseFixed(instr->value(), StoreDescriptor::ValueRegister());
2436
2437   LStoreNamedGeneric* result =
2438       new(zone()) LStoreNamedGeneric(context, object, value);
2439   return MarkAsCall(result, instr);
2440 }
2441
2442
2443 LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
2444   LOperand* context = UseFixed(instr->context(), esi);
2445   LOperand* left = UseFixed(instr->left(), edx);
2446   LOperand* right = UseFixed(instr->right(), eax);
2447   LStringAdd* string_add = new(zone()) LStringAdd(context, left, right);
2448   return MarkAsCall(DefineFixed(string_add, eax), instr);
2449 }
2450
2451
2452 LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
2453   LOperand* string = UseTempRegister(instr->string());
2454   LOperand* index = UseTempRegister(instr->index());
2455   LOperand* context = UseAny(instr->context());
2456   LStringCharCodeAt* result =
2457       new(zone()) LStringCharCodeAt(context, string, index);
2458   return AssignPointerMap(DefineAsRegister(result));
2459 }
2460
2461
2462 LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2463   LOperand* char_code = UseRegister(instr->value());
2464   LOperand* context = UseAny(instr->context());
2465   LStringCharFromCode* result =
2466       new(zone()) LStringCharFromCode(context, char_code);
2467   return AssignPointerMap(DefineAsRegister(result));
2468 }
2469
2470
2471 LInstruction* LChunkBuilder::DoAllocate(HAllocate* instr) {
2472   info()->MarkAsDeferredCalling();
2473   LOperand* context = UseAny(instr->context());
2474   LOperand* size = instr->size()->IsConstant()
2475       ? UseConstant(instr->size())
2476       : UseTempRegister(instr->size());
2477   LOperand* temp = TempRegister();
2478   LAllocate* result = new(zone()) LAllocate(context, size, temp);
2479   return AssignPointerMap(DefineAsRegister(result));
2480 }
2481
2482
2483 LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2484   LOperand* context = UseFixed(instr->context(), esi);
2485   return MarkAsCall(
2486       DefineFixed(new(zone()) LRegExpLiteral(context), eax), instr);
2487 }
2488
2489
2490 LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2491   LOperand* context = UseFixed(instr->context(), esi);
2492   return MarkAsCall(
2493       DefineFixed(new(zone()) LFunctionLiteral(context), eax), instr);
2494 }
2495
2496
2497 LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2498   DCHECK(argument_count_ == 0);
2499   allocator_->MarkAsOsrEntry();
2500   current_block_->last_environment()->set_ast_id(instr->ast_id());
2501   return AssignEnvironment(new(zone()) LOsrEntry);
2502 }
2503
2504
2505 LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2506   LParameter* result = new(zone()) LParameter;
2507   if (instr->kind() == HParameter::STACK_PARAMETER) {
2508     int spill_index = chunk()->GetParameterStackSlot(instr->index());
2509     return DefineAsSpilled(result, spill_index);
2510   } else {
2511     DCHECK(info()->IsStub());
2512     CallInterfaceDescriptor descriptor =
2513         info()->code_stub()->GetCallInterfaceDescriptor();
2514     int index = static_cast<int>(instr->index());
2515     Register reg = descriptor.GetEnvironmentParameterRegister(index);
2516     return DefineFixed(result, reg);
2517   }
2518 }
2519
2520
2521 LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2522   // Use an index that corresponds to the location in the unoptimized frame,
2523   // which the optimized frame will subsume.
2524   int env_index = instr->index();
2525   int spill_index = 0;
2526   if (instr->environment()->is_parameter_index(env_index)) {
2527     spill_index = chunk()->GetParameterStackSlot(env_index);
2528   } else {
2529     spill_index = env_index - instr->environment()->first_local_index();
2530     if (spill_index > LUnallocated::kMaxFixedSlotIndex) {
2531       Retry(kNotEnoughSpillSlotsForOsr);
2532       spill_index = 0;
2533     }
2534     if (spill_index == 0) {
2535       // The dynamic frame alignment state overwrites the first local.
2536       // The first local is saved at the end of the unoptimized frame.
2537       spill_index = graph()->osr()->UnoptimizedFrameSlots();
2538     }
2539   }
2540   return DefineAsSpilled(new(zone()) LUnknownOSRValue, spill_index);
2541 }
2542
2543
2544 LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2545   LOperand* context = UseFixed(instr->context(), esi);
2546   LCallStub* result = new(zone()) LCallStub(context);
2547   return MarkAsCall(DefineFixed(result, eax), instr);
2548 }
2549
2550
2551 LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
2552   // There are no real uses of the arguments object.
2553   // arguments.length and element access are supported directly on
2554   // stack arguments, and any real arguments object use causes a bailout.
2555   // So this value is never used.
2556   return NULL;
2557 }
2558
2559
2560 LInstruction* LChunkBuilder::DoCapturedObject(HCapturedObject* instr) {
2561   instr->ReplayEnvironment(current_block_->last_environment());
2562
2563   // There are no real uses of a captured object.
2564   return NULL;
2565 }
2566
2567
2568 LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2569   info()->MarkAsRequiresFrame();
2570   LOperand* args = UseRegister(instr->arguments());
2571   LOperand* length;
2572   LOperand* index;
2573   if (instr->length()->IsConstant() && instr->index()->IsConstant()) {
2574     length = UseRegisterOrConstant(instr->length());
2575     index = UseOrConstant(instr->index());
2576   } else {
2577     length = UseTempRegister(instr->length());
2578     index = Use(instr->index());
2579   }
2580   return DefineAsRegister(new(zone()) LAccessArgumentsAt(args, length, index));
2581 }
2582
2583
2584 LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2585   LOperand* object = UseFixed(instr->value(), eax);
2586   LToFastProperties* result = new(zone()) LToFastProperties(object);
2587   return MarkAsCall(DefineFixed(result, eax), instr);
2588 }
2589
2590
2591 LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2592   LOperand* context = UseFixed(instr->context(), esi);
2593   LOperand* value = UseAtStart(instr->value());
2594   LTypeof* result = new(zone()) LTypeof(context, value);
2595   return MarkAsCall(DefineFixed(result, eax), instr);
2596 }
2597
2598
2599 LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2600   return new(zone()) LTypeofIsAndBranch(UseTempRegister(instr->value()));
2601 }
2602
2603
2604 LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
2605     HIsConstructCallAndBranch* instr) {
2606   return new(zone()) LIsConstructCallAndBranch(TempRegister());
2607 }
2608
2609
2610 LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2611   instr->ReplayEnvironment(current_block_->last_environment());
2612   return NULL;
2613 }
2614
2615
2616 LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2617   info()->MarkAsDeferredCalling();
2618   if (instr->is_function_entry()) {
2619     LOperand* context = UseFixed(instr->context(), esi);
2620     return MarkAsCall(new(zone()) LStackCheck(context), instr);
2621   } else {
2622     DCHECK(instr->is_backwards_branch());
2623     LOperand* context = UseAny(instr->context());
2624     return AssignEnvironment(
2625         AssignPointerMap(new(zone()) LStackCheck(context)));
2626   }
2627 }
2628
2629
2630 LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2631   HEnvironment* outer = current_block_->last_environment();
2632   outer->set_ast_id(instr->ReturnId());
2633   HConstant* undefined = graph()->GetConstantUndefined();
2634   HEnvironment* inner = outer->CopyForInlining(instr->closure(),
2635                                                instr->arguments_count(),
2636                                                instr->function(),
2637                                                undefined,
2638                                                instr->inlining_kind());
2639   // Only replay binding of arguments object if it wasn't removed from graph.
2640   if (instr->arguments_var() != NULL && instr->arguments_object()->IsLinked()) {
2641     inner->Bind(instr->arguments_var(), instr->arguments_object());
2642   }
2643   inner->BindContext(instr->closure_context());
2644   inner->set_entry(instr);
2645   current_block_->UpdateEnvironment(inner);
2646   chunk_->AddInlinedClosure(instr->closure());
2647   return NULL;
2648 }
2649
2650
2651 LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2652   LInstruction* pop = NULL;
2653
2654   HEnvironment* env = current_block_->last_environment();
2655
2656   if (env->entry()->arguments_pushed()) {
2657     int argument_count = env->arguments_environment()->parameter_count();
2658     pop = new(zone()) LDrop(argument_count);
2659     DCHECK(instr->argument_delta() == -argument_count);
2660   }
2661
2662   HEnvironment* outer = current_block_->last_environment()->
2663       DiscardInlined(false);
2664   current_block_->UpdateEnvironment(outer);
2665   return pop;
2666 }
2667
2668
2669 LInstruction* LChunkBuilder::DoForInPrepareMap(HForInPrepareMap* instr) {
2670   LOperand* context = UseFixed(instr->context(), esi);
2671   LOperand* object = UseFixed(instr->enumerable(), eax);
2672   LForInPrepareMap* result = new(zone()) LForInPrepareMap(context, object);
2673   return MarkAsCall(DefineFixed(result, eax), instr, CAN_DEOPTIMIZE_EAGERLY);
2674 }
2675
2676
2677 LInstruction* LChunkBuilder::DoForInCacheArray(HForInCacheArray* instr) {
2678   LOperand* map = UseRegister(instr->map());
2679   return AssignEnvironment(DefineAsRegister(
2680       new(zone()) LForInCacheArray(map)));
2681 }
2682
2683
2684 LInstruction* LChunkBuilder::DoCheckMapValue(HCheckMapValue* instr) {
2685   LOperand* value = UseRegisterAtStart(instr->value());
2686   LOperand* map = UseRegisterAtStart(instr->map());
2687   return AssignEnvironment(new(zone()) LCheckMapValue(value, map));
2688 }
2689
2690
2691 LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) {
2692   LOperand* object = UseRegister(instr->object());
2693   LOperand* index = UseTempRegister(instr->index());
2694   LLoadFieldByIndex* load = new(zone()) LLoadFieldByIndex(object, index);
2695   LInstruction* result = DefineSameAsFirst(load);
2696   return AssignPointerMap(result);
2697 }
2698
2699
2700 LInstruction* LChunkBuilder::DoStoreFrameContext(HStoreFrameContext* instr) {
2701   LOperand* context = UseRegisterAtStart(instr->context());
2702   return new(zone()) LStoreFrameContext(context);
2703 }
2704
2705
2706 LInstruction* LChunkBuilder::DoAllocateBlockContext(
2707     HAllocateBlockContext* instr) {
2708   LOperand* context = UseFixed(instr->context(), esi);
2709   LOperand* function = UseRegisterAtStart(instr->function());
2710   LAllocateBlockContext* result =
2711       new(zone()) LAllocateBlockContext(context, function);
2712   return MarkAsCall(DefineFixed(result, esi), instr);
2713 }
2714
2715
2716 } }  // namespace v8::internal
2717
2718 #endif  // V8_TARGET_ARCH_X87