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