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