b1cd8d8a2dc6918e61a80d6fd31c6943852b441d
[profile/ivi/qtjsbackend.git] / src / 3rdparty / v8 / src / arm / lithium-arm.cc
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "v8.h"
29
30 #include "lithium-allocator-inl.h"
31 #include "arm/lithium-arm.h"
32 #include "arm/lithium-codegen-arm.h"
33
34 namespace v8 {
35 namespace internal {
36
37 #define DEFINE_COMPILE(type)                            \
38   void L##type::CompileToNative(LCodeGen* generator) {  \
39     generator->Do##type(this);                          \
40   }
41 LITHIUM_CONCRETE_INSTRUCTION_LIST(DEFINE_COMPILE)
42 #undef DEFINE_COMPILE
43
44 LOsrEntry::LOsrEntry() {
45   for (int i = 0; i < Register::kNumAllocatableRegisters; ++i) {
46     register_spills_[i] = NULL;
47   }
48   for (int i = 0; i < DoubleRegister::kNumAllocatableRegisters; ++i) {
49     double_register_spills_[i] = NULL;
50   }
51 }
52
53
54 void LOsrEntry::MarkSpilledRegister(int allocation_index,
55                                     LOperand* spill_operand) {
56   ASSERT(spill_operand->IsStackSlot());
57   ASSERT(register_spills_[allocation_index] == NULL);
58   register_spills_[allocation_index] = spill_operand;
59 }
60
61
62 #ifdef DEBUG
63 void LInstruction::VerifyCall() {
64   // Call instructions can use only fixed registers as temporaries and
65   // outputs because all registers are blocked by the calling convention.
66   // Inputs operands must use a fixed register or use-at-start policy or
67   // a non-register policy.
68   ASSERT(Output() == NULL ||
69          LUnallocated::cast(Output())->HasFixedPolicy() ||
70          !LUnallocated::cast(Output())->HasRegisterPolicy());
71   for (UseIterator it(this); !it.Done(); it.Advance()) {
72     LUnallocated* operand = LUnallocated::cast(it.Current());
73     ASSERT(operand->HasFixedPolicy() ||
74            operand->IsUsedAtStart());
75   }
76   for (TempIterator it(this); !it.Done(); it.Advance()) {
77     LUnallocated* operand = LUnallocated::cast(it.Current());
78     ASSERT(operand->HasFixedPolicy() ||!operand->HasRegisterPolicy());
79   }
80 }
81 #endif
82
83
84 void LOsrEntry::MarkSpilledDoubleRegister(int allocation_index,
85                                           LOperand* spill_operand) {
86   ASSERT(spill_operand->IsDoubleStackSlot());
87   ASSERT(double_register_spills_[allocation_index] == NULL);
88   double_register_spills_[allocation_index] = spill_operand;
89 }
90
91
92 void LInstruction::PrintTo(StringStream* stream) {
93   stream->Add("%s ", this->Mnemonic());
94
95   PrintOutputOperandTo(stream);
96
97   PrintDataTo(stream);
98
99   if (HasEnvironment()) {
100     stream->Add(" ");
101     environment()->PrintTo(stream);
102   }
103
104   if (HasPointerMap()) {
105     stream->Add(" ");
106     pointer_map()->PrintTo(stream);
107   }
108 }
109
110
111 template<int R, int I, int T>
112 void LTemplateInstruction<R, I, T>::PrintDataTo(StringStream* stream) {
113   stream->Add("= ");
114   for (int i = 0; i < inputs_.length(); i++) {
115     if (i > 0) stream->Add(" ");
116     inputs_[i]->PrintTo(stream);
117   }
118 }
119
120
121 template<int R, int I, int T>
122 void LTemplateInstruction<R, I, T>::PrintOutputOperandTo(StringStream* stream) {
123   for (int i = 0; i < results_.length(); i++) {
124     if (i > 0) stream->Add(" ");
125     results_[i]->PrintTo(stream);
126   }
127 }
128
129
130 void LLabel::PrintDataTo(StringStream* stream) {
131   LGap::PrintDataTo(stream);
132   LLabel* rep = replacement();
133   if (rep != NULL) {
134     stream->Add(" Dead block replaced with B%d", rep->block_id());
135   }
136 }
137
138
139 bool LGap::IsRedundant() const {
140   for (int i = 0; i < 4; i++) {
141     if (parallel_moves_[i] != NULL && !parallel_moves_[i]->IsRedundant()) {
142       return false;
143     }
144   }
145
146   return true;
147 }
148
149
150 void LGap::PrintDataTo(StringStream* stream) {
151   for (int i = 0; i < 4; i++) {
152     stream->Add("(");
153     if (parallel_moves_[i] != NULL) {
154       parallel_moves_[i]->PrintDataTo(stream);
155     }
156     stream->Add(") ");
157   }
158 }
159
160
161 const char* LArithmeticD::Mnemonic() const {
162   switch (op()) {
163     case Token::ADD: return "add-d";
164     case Token::SUB: return "sub-d";
165     case Token::MUL: return "mul-d";
166     case Token::DIV: return "div-d";
167     case Token::MOD: return "mod-d";
168     default:
169       UNREACHABLE();
170       return NULL;
171   }
172 }
173
174
175 const char* LArithmeticT::Mnemonic() const {
176   switch (op()) {
177     case Token::ADD: return "add-t";
178     case Token::SUB: return "sub-t";
179     case Token::MUL: return "mul-t";
180     case Token::MOD: return "mod-t";
181     case Token::DIV: return "div-t";
182     case Token::BIT_AND: return "bit-and-t";
183     case Token::BIT_OR: return "bit-or-t";
184     case Token::BIT_XOR: return "bit-xor-t";
185     case Token::SHL: return "shl-t";
186     case Token::SAR: return "sar-t";
187     case Token::SHR: return "shr-t";
188     default:
189       UNREACHABLE();
190       return NULL;
191   }
192 }
193
194
195 void LGoto::PrintDataTo(StringStream* stream) {
196   stream->Add("B%d", block_id());
197 }
198
199
200 void LBranch::PrintDataTo(StringStream* stream) {
201   stream->Add("B%d | B%d on ", true_block_id(), false_block_id());
202   InputAt(0)->PrintTo(stream);
203 }
204
205
206 void LCmpIDAndBranch::PrintDataTo(StringStream* stream) {
207   stream->Add("if ");
208   InputAt(0)->PrintTo(stream);
209   stream->Add(" %s ", Token::String(op()));
210   InputAt(1)->PrintTo(stream);
211   stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
212 }
213
214
215 void LIsNilAndBranch::PrintDataTo(StringStream* stream) {
216   stream->Add("if ");
217   InputAt(0)->PrintTo(stream);
218   stream->Add(kind() == kStrictEquality ? " === " : " == ");
219   stream->Add(nil() == kNullValue ? "null" : "undefined");
220   stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
221 }
222
223
224 void LIsObjectAndBranch::PrintDataTo(StringStream* stream) {
225   stream->Add("if is_object(");
226   InputAt(0)->PrintTo(stream);
227   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
228 }
229
230
231 void LIsSmiAndBranch::PrintDataTo(StringStream* stream) {
232   stream->Add("if is_smi(");
233   InputAt(0)->PrintTo(stream);
234   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
235 }
236
237
238 void LIsUndetectableAndBranch::PrintDataTo(StringStream* stream) {
239   stream->Add("if is_undetectable(");
240   InputAt(0)->PrintTo(stream);
241   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
242 }
243
244
245 void LHasInstanceTypeAndBranch::PrintDataTo(StringStream* stream) {
246   stream->Add("if has_instance_type(");
247   InputAt(0)->PrintTo(stream);
248   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
249 }
250
251
252 void LHasCachedArrayIndexAndBranch::PrintDataTo(StringStream* stream) {
253   stream->Add("if has_cached_array_index(");
254   InputAt(0)->PrintTo(stream);
255   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
256 }
257
258
259 void LClassOfTestAndBranch::PrintDataTo(StringStream* stream) {
260   stream->Add("if class_of_test(");
261   InputAt(0)->PrintTo(stream);
262   stream->Add(", \"%o\") then B%d else B%d",
263               *hydrogen()->class_name(),
264               true_block_id(),
265               false_block_id());
266 }
267
268
269 void LTypeofIsAndBranch::PrintDataTo(StringStream* stream) {
270   stream->Add("if typeof ");
271   InputAt(0)->PrintTo(stream);
272   stream->Add(" == \"%s\" then B%d else B%d",
273               *hydrogen()->type_literal()->ToCString(),
274               true_block_id(), false_block_id());
275 }
276
277
278 void LCallConstantFunction::PrintDataTo(StringStream* stream) {
279   stream->Add("#%d / ", arity());
280 }
281
282
283 void LUnaryMathOperation::PrintDataTo(StringStream* stream) {
284   stream->Add("/%s ", hydrogen()->OpName());
285   InputAt(0)->PrintTo(stream);
286 }
287
288
289 void LLoadContextSlot::PrintDataTo(StringStream* stream) {
290   InputAt(0)->PrintTo(stream);
291   stream->Add("[%d]", slot_index());
292 }
293
294
295 void LStoreContextSlot::PrintDataTo(StringStream* stream) {
296   InputAt(0)->PrintTo(stream);
297   stream->Add("[%d] <- ", slot_index());
298   InputAt(1)->PrintTo(stream);
299 }
300
301
302 void LInvokeFunction::PrintDataTo(StringStream* stream) {
303   stream->Add("= ");
304   InputAt(0)->PrintTo(stream);
305   stream->Add(" #%d / ", arity());
306 }
307
308
309 void LCallKeyed::PrintDataTo(StringStream* stream) {
310   stream->Add("[r2] #%d / ", arity());
311 }
312
313
314 void LCallNamed::PrintDataTo(StringStream* stream) {
315   SmartArrayPointer<char> name_string = name()->ToCString();
316   stream->Add("%s #%d / ", *name_string, arity());
317 }
318
319
320 void LCallGlobal::PrintDataTo(StringStream* stream) {
321   SmartArrayPointer<char> name_string = name()->ToCString();
322   stream->Add("%s #%d / ", *name_string, arity());
323 }
324
325
326 void LCallKnownGlobal::PrintDataTo(StringStream* stream) {
327   stream->Add("#%d / ", arity());
328 }
329
330
331 void LCallNew::PrintDataTo(StringStream* stream) {
332   stream->Add("= ");
333   InputAt(0)->PrintTo(stream);
334   stream->Add(" #%d / ", arity());
335 }
336
337
338 void LAccessArgumentsAt::PrintDataTo(StringStream* stream) {
339   arguments()->PrintTo(stream);
340
341   stream->Add(" length ");
342   length()->PrintTo(stream);
343
344   stream->Add(" index ");
345   index()->PrintTo(stream);
346 }
347
348
349 void LStoreNamedField::PrintDataTo(StringStream* stream) {
350   object()->PrintTo(stream);
351   stream->Add(".");
352   stream->Add(*String::cast(*name())->ToCString());
353   stream->Add(" <- ");
354   value()->PrintTo(stream);
355 }
356
357
358 void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
359   object()->PrintTo(stream);
360   stream->Add(".");
361   stream->Add(*String::cast(*name())->ToCString());
362   stream->Add(" <- ");
363   value()->PrintTo(stream);
364 }
365
366
367 void LStoreKeyedFastElement::PrintDataTo(StringStream* stream) {
368   object()->PrintTo(stream);
369   stream->Add("[");
370   key()->PrintTo(stream);
371   stream->Add("] <- ");
372   value()->PrintTo(stream);
373 }
374
375
376 void LStoreKeyedFastDoubleElement::PrintDataTo(StringStream* stream) {
377   elements()->PrintTo(stream);
378   stream->Add("[");
379   key()->PrintTo(stream);
380   stream->Add("] <- ");
381   value()->PrintTo(stream);
382 }
383
384
385 void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
386   object()->PrintTo(stream);
387   stream->Add("[");
388   key()->PrintTo(stream);
389   stream->Add("] <- ");
390   value()->PrintTo(stream);
391 }
392
393
394 void LTransitionElementsKind::PrintDataTo(StringStream* stream) {
395   object()->PrintTo(stream);
396   stream->Add(" %p -> %p", *original_map(), *transitioned_map());
397 }
398
399
400 LChunk::LChunk(CompilationInfo* info, HGraph* graph)
401     : spill_slot_count_(0),
402       info_(info),
403       graph_(graph),
404       instructions_(32),
405       pointer_maps_(8),
406       inlined_closures_(1) {
407 }
408
409
410 int LChunk::GetNextSpillIndex(bool is_double) {
411   // Skip a slot if for a double-width slot.
412   if (is_double) spill_slot_count_++;
413   return spill_slot_count_++;
414 }
415
416
417 LOperand* LChunk::GetNextSpillSlot(bool is_double)  {
418   int index = GetNextSpillIndex(is_double);
419   if (is_double) {
420     return LDoubleStackSlot::Create(index);
421   } else {
422     return LStackSlot::Create(index);
423   }
424 }
425
426
427 void LChunk::MarkEmptyBlocks() {
428   HPhase phase("Mark empty blocks", this);
429   for (int i = 0; i < graph()->blocks()->length(); ++i) {
430     HBasicBlock* block = graph()->blocks()->at(i);
431     int first = block->first_instruction_index();
432     int last = block->last_instruction_index();
433     LInstruction* first_instr = instructions()->at(first);
434     LInstruction* last_instr = instructions()->at(last);
435
436     LLabel* label = LLabel::cast(first_instr);
437     if (last_instr->IsGoto()) {
438       LGoto* goto_instr = LGoto::cast(last_instr);
439       if (label->IsRedundant() &&
440           !label->is_loop_header()) {
441         bool can_eliminate = true;
442         for (int i = first + 1; i < last && can_eliminate; ++i) {
443           LInstruction* cur = instructions()->at(i);
444           if (cur->IsGap()) {
445             LGap* gap = LGap::cast(cur);
446             if (!gap->IsRedundant()) {
447               can_eliminate = false;
448             }
449           } else {
450             can_eliminate = false;
451           }
452         }
453
454         if (can_eliminate) {
455           label->set_replacement(GetLabel(goto_instr->block_id()));
456         }
457       }
458     }
459   }
460 }
461
462
463 void LChunk::AddInstruction(LInstruction* instr, HBasicBlock* block) {
464   LInstructionGap* gap = new LInstructionGap(block);
465   int index = -1;
466   if (instr->IsControl()) {
467     instructions_.Add(gap);
468     index = instructions_.length();
469     instructions_.Add(instr);
470   } else {
471     index = instructions_.length();
472     instructions_.Add(instr);
473     instructions_.Add(gap);
474   }
475   if (instr->HasPointerMap()) {
476     pointer_maps_.Add(instr->pointer_map());
477     instr->pointer_map()->set_lithium_position(index);
478   }
479 }
480
481
482 LConstantOperand* LChunk::DefineConstantOperand(HConstant* constant) {
483   return LConstantOperand::Create(constant->id());
484 }
485
486
487 int LChunk::GetParameterStackSlot(int index) const {
488   // The receiver is at index 0, the first parameter at index 1, so we
489   // shift all parameter indexes down by the number of parameters, and
490   // make sure they end up negative so they are distinguishable from
491   // spill slots.
492   int result = index - info()->scope()->num_parameters() - 1;
493   ASSERT(result < 0);
494   return result;
495 }
496
497 // A parameter relative to ebp in the arguments stub.
498 int LChunk::ParameterAt(int index) {
499   ASSERT(-1 <= index);  // -1 is the receiver.
500   return (1 + info()->scope()->num_parameters() - index) *
501       kPointerSize;
502 }
503
504
505 LGap* LChunk::GetGapAt(int index) const {
506   return LGap::cast(instructions_[index]);
507 }
508
509
510 bool LChunk::IsGapAt(int index) const {
511   return instructions_[index]->IsGap();
512 }
513
514
515 int LChunk::NearestGapPos(int index) const {
516   while (!IsGapAt(index)) index--;
517   return index;
518 }
519
520
521 void LChunk::AddGapMove(int index, LOperand* from, LOperand* to) {
522   GetGapAt(index)->GetOrCreateParallelMove(LGap::START)->AddMove(from, to);
523 }
524
525
526 Handle<Object> LChunk::LookupLiteral(LConstantOperand* operand) const {
527   return HConstant::cast(graph_->LookupValue(operand->index()))->handle();
528 }
529
530
531 Representation LChunk::LookupLiteralRepresentation(
532     LConstantOperand* operand) const {
533   return graph_->LookupValue(operand->index())->representation();
534 }
535
536
537 LChunk* LChunkBuilder::Build() {
538   ASSERT(is_unused());
539   chunk_ = new LChunk(info(), graph());
540   HPhase phase("Building chunk", chunk_);
541   status_ = BUILDING;
542   const ZoneList<HBasicBlock*>* blocks = graph()->blocks();
543   for (int i = 0; i < blocks->length(); i++) {
544     HBasicBlock* next = NULL;
545     if (i < blocks->length() - 1) next = blocks->at(i + 1);
546     DoBasicBlock(blocks->at(i), next);
547     if (is_aborted()) return NULL;
548   }
549   status_ = DONE;
550   return chunk_;
551 }
552
553
554 void LChunkBuilder::Abort(const char* format, ...) {
555   if (FLAG_trace_bailout) {
556     SmartArrayPointer<char> name(
557         info()->shared_info()->DebugName()->ToCString());
558     PrintF("Aborting LChunk building in @\"%s\": ", *name);
559     va_list arguments;
560     va_start(arguments, format);
561     OS::VPrint(format, arguments);
562     va_end(arguments);
563     PrintF("\n");
564   }
565   status_ = ABORTED;
566 }
567
568
569 LRegister* LChunkBuilder::ToOperand(Register reg) {
570   return LRegister::Create(Register::ToAllocationIndex(reg));
571 }
572
573
574 LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
575   return new LUnallocated(LUnallocated::FIXED_REGISTER,
576                           Register::ToAllocationIndex(reg));
577 }
578
579
580 LUnallocated* LChunkBuilder::ToUnallocated(DoubleRegister reg) {
581   return new LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
582                           DoubleRegister::ToAllocationIndex(reg));
583 }
584
585
586 LOperand* LChunkBuilder::UseFixed(HValue* value, Register fixed_register) {
587   return Use(value, ToUnallocated(fixed_register));
588 }
589
590
591 LOperand* LChunkBuilder::UseFixedDouble(HValue* value, DoubleRegister reg) {
592   return Use(value, ToUnallocated(reg));
593 }
594
595
596 LOperand* LChunkBuilder::UseRegister(HValue* value) {
597   return Use(value, new LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
598 }
599
600
601 LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
602   return Use(value,
603              new LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
604                               LUnallocated::USED_AT_START));
605 }
606
607
608 LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
609   return Use(value, new LUnallocated(LUnallocated::WRITABLE_REGISTER));
610 }
611
612
613 LOperand* LChunkBuilder::Use(HValue* value) {
614   return Use(value, new LUnallocated(LUnallocated::NONE));
615 }
616
617
618 LOperand* LChunkBuilder::UseAtStart(HValue* value) {
619   return Use(value, new LUnallocated(LUnallocated::NONE,
620                                      LUnallocated::USED_AT_START));
621 }
622
623
624 LOperand* LChunkBuilder::UseOrConstant(HValue* value) {
625   return value->IsConstant()
626       ? chunk_->DefineConstantOperand(HConstant::cast(value))
627       : Use(value);
628 }
629
630
631 LOperand* LChunkBuilder::UseOrConstantAtStart(HValue* value) {
632   return value->IsConstant()
633       ? chunk_->DefineConstantOperand(HConstant::cast(value))
634       : UseAtStart(value);
635 }
636
637
638 LOperand* LChunkBuilder::UseRegisterOrConstant(HValue* value) {
639   return value->IsConstant()
640       ? chunk_->DefineConstantOperand(HConstant::cast(value))
641       : UseRegister(value);
642 }
643
644
645 LOperand* LChunkBuilder::UseRegisterOrConstantAtStart(HValue* value) {
646   return value->IsConstant()
647       ? chunk_->DefineConstantOperand(HConstant::cast(value))
648       : UseRegisterAtStart(value);
649 }
650
651
652 LOperand* LChunkBuilder::UseAny(HValue* value) {
653   return value->IsConstant()
654       ? chunk_->DefineConstantOperand(HConstant::cast(value))
655       :  Use(value, new LUnallocated(LUnallocated::ANY));
656 }
657
658
659 LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
660   if (value->EmitAtUses()) {
661     HInstruction* instr = HInstruction::cast(value);
662     VisitInstruction(instr);
663   }
664   allocator_->RecordUse(value, operand);
665   return operand;
666 }
667
668
669 template<int I, int T>
670 LInstruction* LChunkBuilder::Define(LTemplateInstruction<1, I, T>* instr,
671                                     LUnallocated* result) {
672   allocator_->RecordDefinition(current_instruction_, result);
673   instr->set_result(result);
674   return instr;
675 }
676
677
678 template<int I, int T>
679 LInstruction* LChunkBuilder::Define(LTemplateInstruction<1, I, T>* instr) {
680   return Define(instr, new LUnallocated(LUnallocated::NONE));
681 }
682
683
684 template<int I, int T>
685 LInstruction* LChunkBuilder::DefineAsRegister(
686     LTemplateInstruction<1, I, T>* instr) {
687   return Define(instr, new LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
688 }
689
690
691 template<int I, int T>
692 LInstruction* LChunkBuilder::DefineAsSpilled(
693     LTemplateInstruction<1, I, T>* instr, int index) {
694   return Define(instr, new LUnallocated(LUnallocated::FIXED_SLOT, index));
695 }
696
697
698 template<int I, int T>
699 LInstruction* LChunkBuilder::DefineSameAsFirst(
700     LTemplateInstruction<1, I, T>* instr) {
701   return Define(instr, new LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
702 }
703
704
705 template<int I, int T>
706 LInstruction* LChunkBuilder::DefineFixed(
707     LTemplateInstruction<1, I, T>* instr, Register reg) {
708   return Define(instr, ToUnallocated(reg));
709 }
710
711
712 template<int I, int T>
713 LInstruction* LChunkBuilder::DefineFixedDouble(
714     LTemplateInstruction<1, I, T>* instr, DoubleRegister reg) {
715   return Define(instr, ToUnallocated(reg));
716 }
717
718
719 LInstruction* LChunkBuilder::AssignEnvironment(LInstruction* instr) {
720   HEnvironment* hydrogen_env = current_block_->last_environment();
721   int argument_index_accumulator = 0;
722   instr->set_environment(CreateEnvironment(hydrogen_env,
723                                            &argument_index_accumulator));
724   return instr;
725 }
726
727
728 LInstruction* LChunkBuilder::SetInstructionPendingDeoptimizationEnvironment(
729     LInstruction* instr, int ast_id) {
730   ASSERT(instruction_pending_deoptimization_environment_ == NULL);
731   ASSERT(pending_deoptimization_ast_id_ == AstNode::kNoNumber);
732   instruction_pending_deoptimization_environment_ = instr;
733   pending_deoptimization_ast_id_ = ast_id;
734   return instr;
735 }
736
737
738 void LChunkBuilder::ClearInstructionPendingDeoptimizationEnvironment() {
739   instruction_pending_deoptimization_environment_ = NULL;
740   pending_deoptimization_ast_id_ = AstNode::kNoNumber;
741 }
742
743
744 LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
745                                         HInstruction* hinstr,
746                                         CanDeoptimize can_deoptimize) {
747 #ifdef DEBUG
748   instr->VerifyCall();
749 #endif
750   instr->MarkAsCall();
751   instr = AssignPointerMap(instr);
752
753   if (hinstr->HasObservableSideEffects()) {
754     ASSERT(hinstr->next()->IsSimulate());
755     HSimulate* sim = HSimulate::cast(hinstr->next());
756     instr = SetInstructionPendingDeoptimizationEnvironment(
757         instr, sim->ast_id());
758   }
759
760   // If instruction does not have side-effects lazy deoptimization
761   // after the call will try to deoptimize to the point before the call.
762   // Thus we still need to attach environment to this call even if
763   // call sequence can not deoptimize eagerly.
764   bool needs_environment =
765       (can_deoptimize == CAN_DEOPTIMIZE_EAGERLY) ||
766       !hinstr->HasObservableSideEffects();
767   if (needs_environment && !instr->HasEnvironment()) {
768     instr = AssignEnvironment(instr);
769   }
770
771   return instr;
772 }
773
774
775 LInstruction* LChunkBuilder::MarkAsSaveDoubles(LInstruction* instr) {
776   instr->MarkAsSaveDoubles();
777   return instr;
778 }
779
780
781 LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
782   ASSERT(!instr->HasPointerMap());
783   instr->set_pointer_map(new LPointerMap(position_));
784   return instr;
785 }
786
787
788 LUnallocated* LChunkBuilder::TempRegister() {
789   LUnallocated* operand = new LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
790   allocator_->RecordTemporary(operand);
791   return operand;
792 }
793
794
795 LOperand* LChunkBuilder::FixedTemp(Register reg) {
796   LUnallocated* operand = ToUnallocated(reg);
797   allocator_->RecordTemporary(operand);
798   return operand;
799 }
800
801
802 LOperand* LChunkBuilder::FixedTemp(DoubleRegister reg) {
803   LUnallocated* operand = ToUnallocated(reg);
804   allocator_->RecordTemporary(operand);
805   return operand;
806 }
807
808
809 LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
810   return new LLabel(instr->block());
811 }
812
813
814 LInstruction* LChunkBuilder::DoSoftDeoptimize(HSoftDeoptimize* instr) {
815   return AssignEnvironment(new LDeoptimize);
816 }
817
818
819 LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
820   return AssignEnvironment(new LDeoptimize);
821 }
822
823
824 LInstruction* LChunkBuilder::DoShift(Token::Value op,
825                                      HBitwiseBinaryOperation* instr) {
826   if (instr->representation().IsTagged()) {
827     ASSERT(instr->left()->representation().IsTagged());
828     ASSERT(instr->right()->representation().IsTagged());
829
830     LOperand* left = UseFixed(instr->left(), r1);
831     LOperand* right = UseFixed(instr->right(), r0);
832     LArithmeticT* result = new LArithmeticT(op, left, right);
833     return MarkAsCall(DefineFixed(result, r0), instr);
834   }
835
836   ASSERT(instr->representation().IsInteger32());
837   ASSERT(instr->left()->representation().IsInteger32());
838   ASSERT(instr->right()->representation().IsInteger32());
839   LOperand* left = UseRegisterAtStart(instr->left());
840
841   HValue* right_value = instr->right();
842   LOperand* right = NULL;
843   int constant_value = 0;
844   if (right_value->IsConstant()) {
845     HConstant* constant = HConstant::cast(right_value);
846     right = chunk_->DefineConstantOperand(constant);
847     constant_value = constant->Integer32Value() & 0x1f;
848   } else {
849     right = UseRegisterAtStart(right_value);
850   }
851
852   // Shift operations can only deoptimize if we do a logical shift
853   // by 0 and the result cannot be truncated to int32.
854   bool may_deopt = (op == Token::SHR && constant_value == 0);
855   bool does_deopt = false;
856   if (may_deopt) {
857     for (HUseIterator it(instr->uses()); !it.Done(); it.Advance()) {
858       if (!it.value()->CheckFlag(HValue::kTruncatingToInt32)) {
859         does_deopt = true;
860         break;
861       }
862     }
863   }
864
865   LInstruction* result =
866       DefineAsRegister(new LShiftI(op, left, right, does_deopt));
867   return does_deopt ? AssignEnvironment(result) : result;
868 }
869
870
871 LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
872                                            HArithmeticBinaryOperation* instr) {
873   ASSERT(instr->representation().IsDouble());
874   ASSERT(instr->left()->representation().IsDouble());
875   ASSERT(instr->right()->representation().IsDouble());
876   ASSERT(op != Token::MOD);
877   LOperand* left = UseRegisterAtStart(instr->left());
878   LOperand* right = UseRegisterAtStart(instr->right());
879   LArithmeticD* result = new LArithmeticD(op, left, right);
880   return DefineAsRegister(result);
881 }
882
883
884 LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
885                                            HArithmeticBinaryOperation* instr) {
886   ASSERT(op == Token::ADD ||
887          op == Token::DIV ||
888          op == Token::MOD ||
889          op == Token::MUL ||
890          op == Token::SUB);
891   HValue* left = instr->left();
892   HValue* right = instr->right();
893   ASSERT(left->representation().IsTagged());
894   ASSERT(right->representation().IsTagged());
895   LOperand* left_operand = UseFixed(left, r1);
896   LOperand* right_operand = UseFixed(right, r0);
897   LArithmeticT* result = new LArithmeticT(op, left_operand, right_operand);
898   return MarkAsCall(DefineFixed(result, r0), instr);
899 }
900
901
902 void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
903   ASSERT(is_building());
904   current_block_ = block;
905   next_block_ = next_block;
906   if (block->IsStartBlock()) {
907     block->UpdateEnvironment(graph_->start_environment());
908     argument_count_ = 0;
909   } else if (block->predecessors()->length() == 1) {
910     // We have a single predecessor => copy environment and outgoing
911     // argument count from the predecessor.
912     ASSERT(block->phis()->length() == 0);
913     HBasicBlock* pred = block->predecessors()->at(0);
914     HEnvironment* last_environment = pred->last_environment();
915     ASSERT(last_environment != NULL);
916     // Only copy the environment, if it is later used again.
917     if (pred->end()->SecondSuccessor() == NULL) {
918       ASSERT(pred->end()->FirstSuccessor() == block);
919     } else {
920       if (pred->end()->FirstSuccessor()->block_id() > block->block_id() ||
921           pred->end()->SecondSuccessor()->block_id() > block->block_id()) {
922         last_environment = last_environment->Copy();
923       }
924     }
925     block->UpdateEnvironment(last_environment);
926     ASSERT(pred->argument_count() >= 0);
927     argument_count_ = pred->argument_count();
928   } else {
929     // We are at a state join => process phis.
930     HBasicBlock* pred = block->predecessors()->at(0);
931     // No need to copy the environment, it cannot be used later.
932     HEnvironment* last_environment = pred->last_environment();
933     for (int i = 0; i < block->phis()->length(); ++i) {
934       HPhi* phi = block->phis()->at(i);
935       last_environment->SetValueAt(phi->merged_index(), phi);
936     }
937     for (int i = 0; i < block->deleted_phis()->length(); ++i) {
938       last_environment->SetValueAt(block->deleted_phis()->at(i),
939                                    graph_->GetConstantUndefined());
940     }
941     block->UpdateEnvironment(last_environment);
942     // Pick up the outgoing argument count of one of the predecessors.
943     argument_count_ = pred->argument_count();
944   }
945   HInstruction* current = block->first();
946   int start = chunk_->instructions()->length();
947   while (current != NULL && !is_aborted()) {
948     // Code for constants in registers is generated lazily.
949     if (!current->EmitAtUses()) {
950       VisitInstruction(current);
951     }
952     current = current->next();
953   }
954   int end = chunk_->instructions()->length() - 1;
955   if (end >= start) {
956     block->set_first_instruction_index(start);
957     block->set_last_instruction_index(end);
958   }
959   block->set_argument_count(argument_count_);
960   next_block_ = NULL;
961   current_block_ = NULL;
962 }
963
964
965 void LChunkBuilder::VisitInstruction(HInstruction* current) {
966   HInstruction* old_current = current_instruction_;
967   current_instruction_ = current;
968   if (current->has_position()) position_ = current->position();
969   LInstruction* instr = current->CompileToLithium(this);
970
971   if (instr != NULL) {
972     if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
973       instr = AssignPointerMap(instr);
974     }
975     if (FLAG_stress_environments && !instr->HasEnvironment()) {
976       instr = AssignEnvironment(instr);
977     }
978     instr->set_hydrogen_value(current);
979     chunk_->AddInstruction(instr, current_block_);
980   }
981   current_instruction_ = old_current;
982 }
983
984
985 LEnvironment* LChunkBuilder::CreateEnvironment(
986     HEnvironment* hydrogen_env,
987     int* argument_index_accumulator) {
988   if (hydrogen_env == NULL) return NULL;
989
990   LEnvironment* outer =
991       CreateEnvironment(hydrogen_env->outer(), argument_index_accumulator);
992   int ast_id = hydrogen_env->ast_id();
993   ASSERT(ast_id != AstNode::kNoNumber);
994   int value_count = hydrogen_env->length();
995   LEnvironment* result = new LEnvironment(hydrogen_env->closure(),
996                                           ast_id,
997                                           hydrogen_env->parameter_count(),
998                                           argument_count_,
999                                           value_count,
1000                                           outer);
1001   for (int i = 0; i < value_count; ++i) {
1002     if (hydrogen_env->is_special_index(i)) continue;
1003
1004     HValue* value = hydrogen_env->values()->at(i);
1005     LOperand* op = NULL;
1006     if (value->IsArgumentsObject()) {
1007       op = NULL;
1008     } else if (value->IsPushArgument()) {
1009       op = new LArgument((*argument_index_accumulator)++);
1010     } else {
1011       op = UseAny(value);
1012     }
1013     result->AddValue(op, value->representation());
1014   }
1015
1016   return result;
1017 }
1018
1019
1020 LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
1021   return new LGoto(instr->FirstSuccessor()->block_id());
1022 }
1023
1024
1025 LInstruction* LChunkBuilder::DoBranch(HBranch* instr) {
1026   HValue* v = instr->value();
1027   if (v->EmitAtUses()) {
1028     HBasicBlock* successor = HConstant::cast(v)->ToBoolean()
1029         ? instr->FirstSuccessor()
1030         : instr->SecondSuccessor();
1031     return new LGoto(successor->block_id());
1032   }
1033   return AssignEnvironment(new LBranch(UseRegister(v)));
1034 }
1035
1036
1037
1038 LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
1039   ASSERT(instr->value()->representation().IsTagged());
1040   LOperand* value = UseRegisterAtStart(instr->value());
1041   LOperand* temp = TempRegister();
1042   return new LCmpMapAndBranch(value, temp);
1043 }
1044
1045
1046 LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* length) {
1047   return DefineAsRegister(new LArgumentsLength(UseRegister(length->value())));
1048 }
1049
1050
1051 LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
1052   return DefineAsRegister(new LArgumentsElements);
1053 }
1054
1055
1056 LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
1057   LInstanceOf* result =
1058       new LInstanceOf(UseFixed(instr->left(), r0),
1059                       UseFixed(instr->right(), r1));
1060   return MarkAsCall(DefineFixed(result, r0), instr);
1061 }
1062
1063
1064 LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
1065     HInstanceOfKnownGlobal* instr) {
1066   LInstanceOfKnownGlobal* result =
1067       new LInstanceOfKnownGlobal(UseFixed(instr->left(), r0), FixedTemp(r4));
1068   return MarkAsCall(DefineFixed(result, r0), instr);
1069 }
1070
1071
1072 LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
1073   LOperand* function = UseFixed(instr->function(), r1);
1074   LOperand* receiver = UseFixed(instr->receiver(), r0);
1075   LOperand* length = UseFixed(instr->length(), r2);
1076   LOperand* elements = UseFixed(instr->elements(), r3);
1077   LApplyArguments* result = new LApplyArguments(function,
1078                                                 receiver,
1079                                                 length,
1080                                                 elements);
1081   return MarkAsCall(DefineFixed(result, r0), instr, CAN_DEOPTIMIZE_EAGERLY);
1082 }
1083
1084
1085 LInstruction* LChunkBuilder::DoPushArgument(HPushArgument* instr) {
1086   ++argument_count_;
1087   LOperand* argument = Use(instr->argument());
1088   return new LPushArgument(argument);
1089 }
1090
1091
1092 LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1093   return instr->HasNoUses() ? NULL : DefineAsRegister(new LThisFunction);
1094 }
1095
1096
1097 LInstruction* LChunkBuilder::DoContext(HContext* instr) {
1098   return instr->HasNoUses() ? NULL : DefineAsRegister(new LContext);
1099 }
1100
1101
1102 LInstruction* LChunkBuilder::DoOuterContext(HOuterContext* instr) {
1103   LOperand* context = UseRegisterAtStart(instr->value());
1104   return DefineAsRegister(new LOuterContext(context));
1105 }
1106
1107
1108 LInstruction* LChunkBuilder::DoGlobalObject(HGlobalObject* instr) {
1109   LOperand* context = UseRegisterAtStart(instr->value());
1110   return DefineAsRegister(new LGlobalObject(context, instr->qml_global()));
1111 }
1112
1113
1114 LInstruction* LChunkBuilder::DoGlobalReceiver(HGlobalReceiver* instr) {
1115   LOperand* global_object = UseRegisterAtStart(instr->value());
1116   return DefineAsRegister(new LGlobalReceiver(global_object));
1117 }
1118
1119
1120 LInstruction* LChunkBuilder::DoCallConstantFunction(
1121     HCallConstantFunction* instr) {
1122   argument_count_ -= instr->argument_count();
1123   return MarkAsCall(DefineFixed(new LCallConstantFunction, r0), instr);
1124 }
1125
1126
1127 LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1128   LOperand* function = UseFixed(instr->function(), r1);
1129   argument_count_ -= instr->argument_count();
1130   LInvokeFunction* result = new LInvokeFunction(function);
1131   return MarkAsCall(DefineFixed(result, r0), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1132 }
1133
1134
1135 LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
1136   BuiltinFunctionId op = instr->op();
1137   if (op == kMathLog || op == kMathSin || op == kMathCos) {
1138     LOperand* input = UseFixedDouble(instr->value(), d2);
1139     LUnaryMathOperation* result = new LUnaryMathOperation(input, NULL);
1140     return MarkAsCall(DefineFixedDouble(result, d2), instr);
1141   } else {
1142     LOperand* input = UseRegisterAtStart(instr->value());
1143     LOperand* temp = (op == kMathFloor) ? TempRegister() : NULL;
1144     LUnaryMathOperation* result = new LUnaryMathOperation(input, temp);
1145     switch (op) {
1146       case kMathAbs:
1147         return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1148       case kMathFloor:
1149         return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1150       case kMathSqrt:
1151         return DefineAsRegister(result);
1152       case kMathRound:
1153         return AssignEnvironment(DefineAsRegister(result));
1154       case kMathPowHalf:
1155         return DefineAsRegister(result);
1156       default:
1157         UNREACHABLE();
1158         return NULL;
1159     }
1160   }
1161 }
1162
1163
1164 LInstruction* LChunkBuilder::DoCallKeyed(HCallKeyed* instr) {
1165   ASSERT(instr->key()->representation().IsTagged());
1166   argument_count_ -= instr->argument_count();
1167   LOperand* key = UseFixed(instr->key(), r2);
1168   return MarkAsCall(DefineFixed(new LCallKeyed(key), r0), instr);
1169 }
1170
1171
1172 LInstruction* LChunkBuilder::DoCallNamed(HCallNamed* instr) {
1173   argument_count_ -= instr->argument_count();
1174   return MarkAsCall(DefineFixed(new LCallNamed, r0), instr);
1175 }
1176
1177
1178 LInstruction* LChunkBuilder::DoCallGlobal(HCallGlobal* instr) {
1179   argument_count_ -= instr->argument_count();
1180   return MarkAsCall(DefineFixed(new LCallGlobal(instr->qml_global()), r0), instr);
1181 }
1182
1183
1184 LInstruction* LChunkBuilder::DoCallKnownGlobal(HCallKnownGlobal* instr) {
1185   argument_count_ -= instr->argument_count();
1186   return MarkAsCall(DefineFixed(new LCallKnownGlobal, r0), instr);
1187 }
1188
1189
1190 LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
1191   LOperand* constructor = UseFixed(instr->constructor(), r1);
1192   argument_count_ -= instr->argument_count();
1193   LCallNew* result = new LCallNew(constructor);
1194   return MarkAsCall(DefineFixed(result, r0), instr);
1195 }
1196
1197
1198 LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
1199   argument_count_ -= instr->argument_count();
1200   return MarkAsCall(DefineFixed(new LCallFunction, r0), instr);
1201 }
1202
1203
1204 LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1205   argument_count_ -= instr->argument_count();
1206   return MarkAsCall(DefineFixed(new LCallRuntime, r0), instr);
1207 }
1208
1209
1210 LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1211   return DoShift(Token::SHR, instr);
1212 }
1213
1214
1215 LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1216   return DoShift(Token::SAR, instr);
1217 }
1218
1219
1220 LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1221   return DoShift(Token::SHL, instr);
1222 }
1223
1224
1225 LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
1226   if (instr->representation().IsInteger32()) {
1227     ASSERT(instr->left()->representation().IsInteger32());
1228     ASSERT(instr->right()->representation().IsInteger32());
1229
1230     LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1231     LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1232     return DefineAsRegister(new LBitI(left, right));
1233   } else {
1234     ASSERT(instr->representation().IsTagged());
1235     ASSERT(instr->left()->representation().IsTagged());
1236     ASSERT(instr->right()->representation().IsTagged());
1237
1238     LOperand* left = UseFixed(instr->left(), r1);
1239     LOperand* right = UseFixed(instr->right(), r0);
1240     LArithmeticT* result = new LArithmeticT(instr->op(), left, right);
1241     return MarkAsCall(DefineFixed(result, r0), instr);
1242   }
1243 }
1244
1245
1246 LInstruction* LChunkBuilder::DoBitNot(HBitNot* instr) {
1247   ASSERT(instr->value()->representation().IsInteger32());
1248   ASSERT(instr->representation().IsInteger32());
1249   return DefineAsRegister(new LBitNotI(UseRegisterAtStart(instr->value())));
1250 }
1251
1252
1253 LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
1254   if (instr->representation().IsDouble()) {
1255     return DoArithmeticD(Token::DIV, instr);
1256   } else if (instr->representation().IsInteger32()) {
1257     // TODO(1042) The fixed register allocation
1258     // is needed because we call TypeRecordingBinaryOpStub from
1259     // the generated code, which requires registers r0
1260     // and r1 to be used. We should remove that
1261     // when we provide a native implementation.
1262     LOperand* dividend = UseFixed(instr->left(), r0);
1263     LOperand* divisor = UseFixed(instr->right(), r1);
1264     return AssignEnvironment(AssignPointerMap(
1265              DefineFixed(new LDivI(dividend, divisor), r0)));
1266   } else {
1267     return DoArithmeticT(Token::DIV, instr);
1268   }
1269 }
1270
1271
1272 LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1273   if (instr->representation().IsInteger32()) {
1274     ASSERT(instr->left()->representation().IsInteger32());
1275     ASSERT(instr->right()->representation().IsInteger32());
1276
1277     LModI* mod;
1278     if (instr->HasPowerOf2Divisor()) {
1279       ASSERT(!instr->CheckFlag(HValue::kCanBeDivByZero));
1280       LOperand* value = UseRegisterAtStart(instr->left());
1281       mod = new LModI(value, UseOrConstant(instr->right()));
1282     } else {
1283       LOperand* dividend = UseRegister(instr->left());
1284       LOperand* divisor = UseRegister(instr->right());
1285       mod = new LModI(dividend,
1286                       divisor,
1287                       TempRegister(),
1288                       FixedTemp(d10),
1289                       FixedTemp(d11));
1290     }
1291
1292     if (instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1293         instr->CheckFlag(HValue::kCanBeDivByZero)) {
1294       return AssignEnvironment(DefineAsRegister(mod));
1295     } else {
1296       return DefineAsRegister(mod);
1297     }
1298   } else if (instr->representation().IsTagged()) {
1299     return DoArithmeticT(Token::MOD, instr);
1300   } else {
1301     ASSERT(instr->representation().IsDouble());
1302     // We call a C function for double modulo. It can't trigger a GC.
1303     // We need to use fixed result register for the call.
1304     // TODO(fschneider): Allow any register as input registers.
1305     LOperand* left = UseFixedDouble(instr->left(), d1);
1306     LOperand* right = UseFixedDouble(instr->right(), d2);
1307     LArithmeticD* result = new LArithmeticD(Token::MOD, left, right);
1308     return MarkAsCall(DefineFixedDouble(result, d1), instr);
1309   }
1310 }
1311
1312
1313 LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1314   if (instr->representation().IsInteger32()) {
1315     ASSERT(instr->left()->representation().IsInteger32());
1316     ASSERT(instr->right()->representation().IsInteger32());
1317     LOperand* left;
1318     LOperand* right = UseOrConstant(instr->MostConstantOperand());
1319     LOperand* temp = NULL;
1320     if (instr->CheckFlag(HValue::kBailoutOnMinusZero) &&
1321         (instr->CheckFlag(HValue::kCanOverflow) ||
1322         !right->IsConstantOperand())) {
1323       left = UseRegister(instr->LeastConstantOperand());
1324       temp = TempRegister();
1325     } else {
1326       left = UseRegisterAtStart(instr->LeastConstantOperand());
1327     }
1328     return AssignEnvironment(DefineAsRegister(new LMulI(left, right, temp)));
1329
1330   } else if (instr->representation().IsDouble()) {
1331     return DoArithmeticD(Token::MUL, instr);
1332
1333   } else {
1334     return DoArithmeticT(Token::MUL, instr);
1335   }
1336 }
1337
1338
1339 LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1340   if (instr->representation().IsInteger32()) {
1341     ASSERT(instr->left()->representation().IsInteger32());
1342     ASSERT(instr->right()->representation().IsInteger32());
1343     LOperand* left = UseRegisterAtStart(instr->left());
1344     LOperand* right = UseOrConstantAtStart(instr->right());
1345     LSubI* sub = new LSubI(left, right);
1346     LInstruction* result = DefineAsRegister(sub);
1347     if (instr->CheckFlag(HValue::kCanOverflow)) {
1348       result = AssignEnvironment(result);
1349     }
1350     return result;
1351   } else if (instr->representation().IsDouble()) {
1352     return DoArithmeticD(Token::SUB, instr);
1353   } else {
1354     return DoArithmeticT(Token::SUB, instr);
1355   }
1356 }
1357
1358
1359 LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1360   if (instr->representation().IsInteger32()) {
1361     ASSERT(instr->left()->representation().IsInteger32());
1362     ASSERT(instr->right()->representation().IsInteger32());
1363     LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1364     LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1365     LAddI* add = new LAddI(left, right);
1366     LInstruction* result = DefineAsRegister(add);
1367     if (instr->CheckFlag(HValue::kCanOverflow)) {
1368       result = AssignEnvironment(result);
1369     }
1370     return result;
1371   } else if (instr->representation().IsDouble()) {
1372     return DoArithmeticD(Token::ADD, instr);
1373   } else {
1374     ASSERT(instr->representation().IsTagged());
1375     return DoArithmeticT(Token::ADD, instr);
1376   }
1377 }
1378
1379
1380 LInstruction* LChunkBuilder::DoPower(HPower* instr) {
1381   ASSERT(instr->representation().IsDouble());
1382   // We call a C function for double power. It can't trigger a GC.
1383   // We need to use fixed result register for the call.
1384   Representation exponent_type = instr->right()->representation();
1385   ASSERT(instr->left()->representation().IsDouble());
1386   LOperand* left = UseFixedDouble(instr->left(), d1);
1387   LOperand* right = exponent_type.IsDouble() ?
1388       UseFixedDouble(instr->right(), d2) :
1389       UseFixed(instr->right(), r0);
1390   LPower* result = new LPower(left, right);
1391   return MarkAsCall(DefineFixedDouble(result, d3),
1392                     instr,
1393                     CAN_DEOPTIMIZE_EAGERLY);
1394 }
1395
1396
1397 LInstruction* LChunkBuilder::DoCompareGeneric(HCompareGeneric* instr) {
1398   ASSERT(instr->left()->representation().IsTagged());
1399   ASSERT(instr->right()->representation().IsTagged());
1400   LOperand* left = UseFixed(instr->left(), r1);
1401   LOperand* right = UseFixed(instr->right(), r0);
1402   LCmpT* result = new LCmpT(left, right);
1403   return MarkAsCall(DefineFixed(result, r0), instr);
1404 }
1405
1406
1407 LInstruction* LChunkBuilder::DoCompareIDAndBranch(
1408     HCompareIDAndBranch* instr) {
1409   Representation r = instr->GetInputRepresentation();
1410   if (r.IsInteger32()) {
1411     ASSERT(instr->left()->representation().IsInteger32());
1412     ASSERT(instr->right()->representation().IsInteger32());
1413     LOperand* left = UseRegisterOrConstantAtStart(instr->left());
1414     LOperand* right = UseRegisterOrConstantAtStart(instr->right());
1415     return new LCmpIDAndBranch(left, right);
1416   } else {
1417     ASSERT(r.IsDouble());
1418     ASSERT(instr->left()->representation().IsDouble());
1419     ASSERT(instr->right()->representation().IsDouble());
1420     LOperand* left = UseRegisterAtStart(instr->left());
1421     LOperand* right = UseRegisterAtStart(instr->right());
1422     return new LCmpIDAndBranch(left, right);
1423   }
1424 }
1425
1426
1427 LInstruction* LChunkBuilder::DoCompareObjectEqAndBranch(
1428     HCompareObjectEqAndBranch* instr) {
1429   LOperand* left = UseRegisterAtStart(instr->left());
1430   LOperand* right = UseRegisterAtStart(instr->right());
1431   return new LCmpObjectEqAndBranch(left, right);
1432 }
1433
1434
1435 LInstruction* LChunkBuilder::DoCompareConstantEqAndBranch(
1436     HCompareConstantEqAndBranch* instr) {
1437   return new LCmpConstantEqAndBranch(UseRegisterAtStart(instr->value()));
1438 }
1439
1440
1441 LInstruction* LChunkBuilder::DoIsNilAndBranch(HIsNilAndBranch* instr) {
1442   ASSERT(instr->value()->representation().IsTagged());
1443   return new LIsNilAndBranch(UseRegisterAtStart(instr->value()));
1444 }
1445
1446
1447 LInstruction* LChunkBuilder::DoIsObjectAndBranch(HIsObjectAndBranch* instr) {
1448   ASSERT(instr->value()->representation().IsTagged());
1449   LOperand* temp = TempRegister();
1450   return new LIsObjectAndBranch(UseRegisterAtStart(instr->value()), temp);
1451 }
1452
1453
1454 LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
1455   ASSERT(instr->value()->representation().IsTagged());
1456   return new LIsSmiAndBranch(Use(instr->value()));
1457 }
1458
1459
1460 LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
1461     HIsUndetectableAndBranch* instr) {
1462   ASSERT(instr->value()->representation().IsTagged());
1463   return new LIsUndetectableAndBranch(UseRegisterAtStart(instr->value()),
1464                                       TempRegister());
1465 }
1466
1467
1468 LInstruction* LChunkBuilder::DoHasInstanceTypeAndBranch(
1469     HHasInstanceTypeAndBranch* instr) {
1470   ASSERT(instr->value()->representation().IsTagged());
1471   return new LHasInstanceTypeAndBranch(UseRegisterAtStart(instr->value()));
1472 }
1473
1474
1475 LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
1476     HGetCachedArrayIndex* instr)  {
1477   ASSERT(instr->value()->representation().IsTagged());
1478   LOperand* value = UseRegisterAtStart(instr->value());
1479
1480   return DefineAsRegister(new LGetCachedArrayIndex(value));
1481 }
1482
1483
1484 LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
1485     HHasCachedArrayIndexAndBranch* instr) {
1486   ASSERT(instr->value()->representation().IsTagged());
1487   return new LHasCachedArrayIndexAndBranch(
1488       UseRegisterAtStart(instr->value()));
1489 }
1490
1491
1492 LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
1493     HClassOfTestAndBranch* instr) {
1494   ASSERT(instr->value()->representation().IsTagged());
1495   return new LClassOfTestAndBranch(UseTempRegister(instr->value()),
1496                                    TempRegister());
1497 }
1498
1499
1500 LInstruction* LChunkBuilder::DoJSArrayLength(HJSArrayLength* instr) {
1501   LOperand* array = UseRegisterAtStart(instr->value());
1502   return DefineAsRegister(new LJSArrayLength(array));
1503 }
1504
1505
1506 LInstruction* LChunkBuilder::DoFixedArrayBaseLength(
1507     HFixedArrayBaseLength* instr) {
1508   LOperand* array = UseRegisterAtStart(instr->value());
1509   return DefineAsRegister(new LFixedArrayBaseLength(array));
1510 }
1511
1512
1513 LInstruction* LChunkBuilder::DoElementsKind(HElementsKind* instr) {
1514   LOperand* object = UseRegisterAtStart(instr->value());
1515   return DefineAsRegister(new LElementsKind(object));
1516 }
1517
1518
1519 LInstruction* LChunkBuilder::DoValueOf(HValueOf* instr) {
1520   LOperand* object = UseRegister(instr->value());
1521   LValueOf* result = new LValueOf(object, TempRegister());
1522   return AssignEnvironment(DefineAsRegister(result));
1523 }
1524
1525
1526 LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
1527   return AssignEnvironment(new LBoundsCheck(UseRegisterAtStart(instr->index()),
1528                                             UseRegister(instr->length())));
1529 }
1530
1531
1532 LInstruction* LChunkBuilder::DoAbnormalExit(HAbnormalExit* instr) {
1533   // The control instruction marking the end of a block that completed
1534   // abruptly (e.g., threw an exception).  There is nothing specific to do.
1535   return NULL;
1536 }
1537
1538
1539 LInstruction* LChunkBuilder::DoThrow(HThrow* instr) {
1540   LOperand* value = UseFixed(instr->value(), r0);
1541   return MarkAsCall(new LThrow(value), instr);
1542 }
1543
1544
1545 LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
1546   return NULL;
1547 }
1548
1549
1550 LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
1551   // All HForceRepresentation instructions should be eliminated in the
1552   // representation change phase of Hydrogen.
1553   UNREACHABLE();
1554   return NULL;
1555 }
1556
1557
1558 LInstruction* LChunkBuilder::DoChange(HChange* instr) {
1559   Representation from = instr->from();
1560   Representation to = instr->to();
1561   if (from.IsTagged()) {
1562     if (to.IsDouble()) {
1563       LOperand* value = UseRegister(instr->value());
1564       LNumberUntagD* res = new LNumberUntagD(value);
1565       return AssignEnvironment(DefineAsRegister(res));
1566     } else {
1567       ASSERT(to.IsInteger32());
1568       LOperand* value = UseRegister(instr->value());
1569       bool needs_check = !instr->value()->type().IsSmi();
1570       LInstruction* res = NULL;
1571       if (!needs_check) {
1572         res = DefineSameAsFirst(new LSmiUntag(value, needs_check));
1573       } else {
1574         LOperand* temp1 = TempRegister();
1575         LOperand* temp2 = instr->CanTruncateToInt32() ? TempRegister()
1576                                                       : NULL;
1577         LOperand* temp3 = instr->CanTruncateToInt32() ? FixedTemp(d11)
1578                                                       : NULL;
1579         res = DefineSameAsFirst(new LTaggedToI(value, temp1, temp2, temp3));
1580         res = AssignEnvironment(res);
1581       }
1582       return res;
1583     }
1584   } else if (from.IsDouble()) {
1585     if (to.IsTagged()) {
1586       LOperand* value = UseRegister(instr->value());
1587       LOperand* temp1 = TempRegister();
1588       LOperand* temp2 = TempRegister();
1589
1590       // Make sure that the temp and result_temp registers are
1591       // different.
1592       LUnallocated* result_temp = TempRegister();
1593       LNumberTagD* result = new LNumberTagD(value, temp1, temp2);
1594       Define(result, result_temp);
1595       return AssignPointerMap(result);
1596     } else {
1597       ASSERT(to.IsInteger32());
1598       LOperand* value = UseRegister(instr->value());
1599       LDoubleToI* res =
1600         new LDoubleToI(value,
1601                        TempRegister(),
1602                        instr->CanTruncateToInt32() ? TempRegister() : NULL);
1603       return AssignEnvironment(DefineAsRegister(res));
1604     }
1605   } else if (from.IsInteger32()) {
1606     if (to.IsTagged()) {
1607       HValue* val = instr->value();
1608       LOperand* value = UseRegister(val);
1609       if (val->HasRange() && val->range()->IsInSmiRange()) {
1610         return DefineSameAsFirst(new LSmiTag(value));
1611       } else {
1612         LNumberTagI* result = new LNumberTagI(value);
1613         return AssignEnvironment(AssignPointerMap(DefineSameAsFirst(result)));
1614       }
1615     } else {
1616       ASSERT(to.IsDouble());
1617       LOperand* value = Use(instr->value());
1618       return DefineAsRegister(new LInteger32ToDouble(value));
1619     }
1620   }
1621   UNREACHABLE();
1622   return NULL;
1623 }
1624
1625
1626 LInstruction* LChunkBuilder::DoCheckNonSmi(HCheckNonSmi* instr) {
1627   LOperand* value = UseRegisterAtStart(instr->value());
1628   return AssignEnvironment(new LCheckNonSmi(value));
1629 }
1630
1631
1632 LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
1633   LOperand* value = UseRegisterAtStart(instr->value());
1634   LInstruction* result = new LCheckInstanceType(value);
1635   return AssignEnvironment(result);
1636 }
1637
1638
1639 LInstruction* LChunkBuilder::DoCheckPrototypeMaps(HCheckPrototypeMaps* instr) {
1640   LOperand* temp1 = TempRegister();
1641   LOperand* temp2 = TempRegister();
1642   LInstruction* result = new LCheckPrototypeMaps(temp1, temp2);
1643   return AssignEnvironment(result);
1644 }
1645
1646
1647 LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
1648   LOperand* value = UseRegisterAtStart(instr->value());
1649   return AssignEnvironment(new LCheckSmi(value));
1650 }
1651
1652
1653 LInstruction* LChunkBuilder::DoCheckFunction(HCheckFunction* instr) {
1654   LOperand* value = UseRegisterAtStart(instr->value());
1655   return AssignEnvironment(new LCheckFunction(value));
1656 }
1657
1658
1659 LInstruction* LChunkBuilder::DoCheckMap(HCheckMap* instr) {
1660   LOperand* value = UseRegisterAtStart(instr->value());
1661   LInstruction* result = new LCheckMap(value);
1662   return AssignEnvironment(result);
1663 }
1664
1665
1666 LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
1667   HValue* value = instr->value();
1668   Representation input_rep = value->representation();
1669   LOperand* reg = UseRegister(value);
1670   if (input_rep.IsDouble()) {
1671     return DefineAsRegister(new LClampDToUint8(reg, FixedTemp(d11)));
1672   } else if (input_rep.IsInteger32()) {
1673     return DefineAsRegister(new LClampIToUint8(reg));
1674   } else {
1675     ASSERT(input_rep.IsTagged());
1676     // Register allocator doesn't (yet) support allocation of double
1677     // temps. Reserve d1 explicitly.
1678     LClampTToUint8* result = new LClampTToUint8(reg, FixedTemp(d11));
1679     return AssignEnvironment(DefineAsRegister(result));
1680   }
1681 }
1682
1683
1684 LInstruction* LChunkBuilder::DoToInt32(HToInt32* instr) {
1685   HValue* value = instr->value();
1686   Representation input_rep = value->representation();
1687   LOperand* reg = UseRegister(value);
1688   if (input_rep.IsDouble()) {
1689     LOperand* temp1 = TempRegister();
1690     LOperand* temp2 = TempRegister();
1691     LDoubleToI* res = new LDoubleToI(reg, temp1, temp2);
1692     return AssignEnvironment(DefineAsRegister(res));
1693   } else if (input_rep.IsInteger32()) {
1694     // Canonicalization should already have removed the hydrogen instruction in
1695     // this case, since it is a noop.
1696     UNREACHABLE();
1697     return NULL;
1698   } else {
1699     ASSERT(input_rep.IsTagged());
1700     LOperand* temp1 = TempRegister();
1701     LOperand* temp2 = TempRegister();
1702     LOperand* temp3 = FixedTemp(d11);
1703     LTaggedToI* res = new LTaggedToI(reg, temp1, temp2, temp3);
1704     return AssignEnvironment(DefineSameAsFirst(res));
1705   }
1706 }
1707
1708
1709 LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
1710   return new LReturn(UseFixed(instr->value(), r0));
1711 }
1712
1713
1714 LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
1715   Representation r = instr->representation();
1716   if (r.IsInteger32()) {
1717     return DefineAsRegister(new LConstantI);
1718   } else if (r.IsDouble()) {
1719     return DefineAsRegister(new LConstantD);
1720   } else if (r.IsTagged()) {
1721     return DefineAsRegister(new LConstantT);
1722   } else {
1723     UNREACHABLE();
1724     return NULL;
1725   }
1726 }
1727
1728
1729 LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
1730   LLoadGlobalCell* result = new LLoadGlobalCell;
1731   return instr->RequiresHoleCheck()
1732       ? AssignEnvironment(DefineAsRegister(result))
1733       : DefineAsRegister(result);
1734 }
1735
1736
1737 LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
1738   LOperand* global_object = UseFixed(instr->global_object(), r0);
1739   LLoadGlobalGeneric* result = new LLoadGlobalGeneric(global_object);
1740   return MarkAsCall(DefineFixed(result, r0), instr);
1741 }
1742
1743
1744 LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
1745   LOperand* temp = TempRegister();
1746   LOperand* value = UseTempRegister(instr->value());
1747   LInstruction* result = new LStoreGlobalCell(value, temp);
1748   if (instr->RequiresHoleCheck()) result = AssignEnvironment(result);
1749   return result;
1750 }
1751
1752
1753 LInstruction* LChunkBuilder::DoStoreGlobalGeneric(HStoreGlobalGeneric* instr) {
1754   LOperand* global_object = UseFixed(instr->global_object(), r1);
1755   LOperand* value = UseFixed(instr->value(), r0);
1756   LStoreGlobalGeneric* result =
1757       new LStoreGlobalGeneric(global_object, value);
1758   return MarkAsCall(result, instr);
1759 }
1760
1761
1762 LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
1763   LOperand* context = UseRegisterAtStart(instr->value());
1764   return DefineAsRegister(new LLoadContextSlot(context));
1765 }
1766
1767
1768 LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
1769   LOperand* context;
1770   LOperand* value;
1771   if (instr->NeedsWriteBarrier()) {
1772     context = UseTempRegister(instr->context());
1773     value = UseTempRegister(instr->value());
1774   } else {
1775     context = UseRegister(instr->context());
1776     value = UseRegister(instr->value());
1777   }
1778   return new LStoreContextSlot(context, value);
1779 }
1780
1781
1782 LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
1783   return DefineAsRegister(
1784       new LLoadNamedField(UseRegisterAtStart(instr->object())));
1785 }
1786
1787
1788 LInstruction* LChunkBuilder::DoLoadNamedFieldPolymorphic(
1789     HLoadNamedFieldPolymorphic* instr) {
1790   ASSERT(instr->representation().IsTagged());
1791   if (instr->need_generic()) {
1792     LOperand* obj = UseFixed(instr->object(), r0);
1793     LLoadNamedFieldPolymorphic* result = new LLoadNamedFieldPolymorphic(obj);
1794     return MarkAsCall(DefineFixed(result, r0), instr);
1795   } else {
1796     LOperand* obj = UseRegisterAtStart(instr->object());
1797     LLoadNamedFieldPolymorphic* result = new LLoadNamedFieldPolymorphic(obj);
1798     return AssignEnvironment(DefineAsRegister(result));
1799   }
1800 }
1801
1802
1803 LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
1804   LOperand* object = UseFixed(instr->object(), r0);
1805   LInstruction* result = DefineFixed(new LLoadNamedGeneric(object), r0);
1806   return MarkAsCall(result, instr);
1807 }
1808
1809
1810 LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
1811     HLoadFunctionPrototype* instr) {
1812   return AssignEnvironment(DefineAsRegister(
1813       new LLoadFunctionPrototype(UseRegister(instr->function()))));
1814 }
1815
1816
1817 LInstruction* LChunkBuilder::DoLoadElements(HLoadElements* instr) {
1818   LOperand* input = UseRegisterAtStart(instr->value());
1819   return DefineAsRegister(new LLoadElements(input));
1820 }
1821
1822
1823 LInstruction* LChunkBuilder::DoLoadExternalArrayPointer(
1824     HLoadExternalArrayPointer* instr) {
1825   LOperand* input = UseRegisterAtStart(instr->value());
1826   return DefineAsRegister(new LLoadExternalArrayPointer(input));
1827 }
1828
1829
1830 LInstruction* LChunkBuilder::DoLoadKeyedFastElement(
1831     HLoadKeyedFastElement* instr) {
1832   ASSERT(instr->representation().IsTagged());
1833   ASSERT(instr->key()->representation().IsInteger32());
1834   LOperand* obj = UseRegisterAtStart(instr->object());
1835   LOperand* key = UseRegisterAtStart(instr->key());
1836   LLoadKeyedFastElement* result = new LLoadKeyedFastElement(obj, key);
1837   return AssignEnvironment(DefineAsRegister(result));
1838 }
1839
1840
1841 LInstruction* LChunkBuilder::DoLoadKeyedFastDoubleElement(
1842     HLoadKeyedFastDoubleElement* instr) {
1843   ASSERT(instr->representation().IsDouble());
1844   ASSERT(instr->key()->representation().IsInteger32());
1845   LOperand* elements = UseTempRegister(instr->elements());
1846   LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1847   LLoadKeyedFastDoubleElement* result =
1848       new LLoadKeyedFastDoubleElement(elements, key);
1849   return AssignEnvironment(DefineAsRegister(result));
1850 }
1851
1852
1853 LInstruction* LChunkBuilder::DoLoadKeyedSpecializedArrayElement(
1854     HLoadKeyedSpecializedArrayElement* instr) {
1855   ElementsKind elements_kind = instr->elements_kind();
1856   Representation representation(instr->representation());
1857   ASSERT(
1858       (representation.IsInteger32() &&
1859        (elements_kind != EXTERNAL_FLOAT_ELEMENTS) &&
1860        (elements_kind != EXTERNAL_DOUBLE_ELEMENTS)) ||
1861       (representation.IsDouble() &&
1862        ((elements_kind == EXTERNAL_FLOAT_ELEMENTS) ||
1863        (elements_kind == EXTERNAL_DOUBLE_ELEMENTS))));
1864   ASSERT(instr->key()->representation().IsInteger32());
1865   LOperand* external_pointer = UseRegister(instr->external_pointer());
1866   LOperand* key = UseRegisterOrConstant(instr->key());
1867   LLoadKeyedSpecializedArrayElement* result =
1868       new LLoadKeyedSpecializedArrayElement(external_pointer, key);
1869   LInstruction* load_instr = DefineAsRegister(result);
1870   // An unsigned int array load might overflow and cause a deopt, make sure it
1871   // has an environment.
1872   return (elements_kind == EXTERNAL_UNSIGNED_INT_ELEMENTS) ?
1873       AssignEnvironment(load_instr) : load_instr;
1874 }
1875
1876
1877 LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
1878   LOperand* object = UseFixed(instr->object(), r1);
1879   LOperand* key = UseFixed(instr->key(), r0);
1880
1881   LInstruction* result =
1882       DefineFixed(new LLoadKeyedGeneric(object, key), r0);
1883   return MarkAsCall(result, instr);
1884 }
1885
1886
1887 LInstruction* LChunkBuilder::DoStoreKeyedFastElement(
1888     HStoreKeyedFastElement* instr) {
1889   bool needs_write_barrier = instr->NeedsWriteBarrier();
1890   ASSERT(instr->value()->representation().IsTagged());
1891   ASSERT(instr->object()->representation().IsTagged());
1892   ASSERT(instr->key()->representation().IsInteger32());
1893
1894   LOperand* obj = UseTempRegister(instr->object());
1895   LOperand* val = needs_write_barrier
1896       ? UseTempRegister(instr->value())
1897       : UseRegisterAtStart(instr->value());
1898   LOperand* key = needs_write_barrier
1899       ? UseTempRegister(instr->key())
1900       : UseRegisterOrConstantAtStart(instr->key());
1901
1902   return AssignEnvironment(new LStoreKeyedFastElement(obj, key, val));
1903 }
1904
1905
1906 LInstruction* LChunkBuilder::DoStoreKeyedFastDoubleElement(
1907     HStoreKeyedFastDoubleElement* instr) {
1908   ASSERT(instr->value()->representation().IsDouble());
1909   ASSERT(instr->elements()->representation().IsTagged());
1910   ASSERT(instr->key()->representation().IsInteger32());
1911
1912   LOperand* elements = UseRegisterAtStart(instr->elements());
1913   LOperand* val = UseTempRegister(instr->value());
1914   LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1915
1916   return new LStoreKeyedFastDoubleElement(elements, key, val);
1917 }
1918
1919
1920 LInstruction* LChunkBuilder::DoStoreKeyedSpecializedArrayElement(
1921     HStoreKeyedSpecializedArrayElement* instr) {
1922   Representation representation(instr->value()->representation());
1923   ElementsKind elements_kind = instr->elements_kind();
1924   ASSERT(
1925       (representation.IsInteger32() &&
1926        (elements_kind != EXTERNAL_FLOAT_ELEMENTS) &&
1927        (elements_kind != EXTERNAL_DOUBLE_ELEMENTS)) ||
1928       (representation.IsDouble() &&
1929        ((elements_kind == EXTERNAL_FLOAT_ELEMENTS) ||
1930        (elements_kind == EXTERNAL_DOUBLE_ELEMENTS))));
1931   ASSERT(instr->external_pointer()->representation().IsExternal());
1932   ASSERT(instr->key()->representation().IsInteger32());
1933
1934   LOperand* external_pointer = UseRegister(instr->external_pointer());
1935   bool val_is_temp_register =
1936       elements_kind == EXTERNAL_PIXEL_ELEMENTS ||
1937       elements_kind == EXTERNAL_FLOAT_ELEMENTS;
1938   LOperand* val = val_is_temp_register
1939       ? UseTempRegister(instr->value())
1940       : UseRegister(instr->value());
1941   LOperand* key = UseRegisterOrConstant(instr->key());
1942
1943   return new LStoreKeyedSpecializedArrayElement(external_pointer,
1944                                                 key,
1945                                                 val);
1946 }
1947
1948
1949 LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
1950   LOperand* obj = UseFixed(instr->object(), r2);
1951   LOperand* key = UseFixed(instr->key(), r1);
1952   LOperand* val = UseFixed(instr->value(), r0);
1953
1954   ASSERT(instr->object()->representation().IsTagged());
1955   ASSERT(instr->key()->representation().IsTagged());
1956   ASSERT(instr->value()->representation().IsTagged());
1957
1958   return MarkAsCall(new LStoreKeyedGeneric(obj, key, val), instr);
1959 }
1960
1961
1962 LInstruction* LChunkBuilder::DoTransitionElementsKind(
1963     HTransitionElementsKind* instr) {
1964   if (instr->original_map()->elements_kind() == FAST_SMI_ONLY_ELEMENTS &&
1965       instr->transitioned_map()->elements_kind() == FAST_ELEMENTS) {
1966     LOperand* object = UseRegister(instr->object());
1967     LOperand* new_map_reg = TempRegister();
1968     LTransitionElementsKind* result =
1969         new LTransitionElementsKind(object, new_map_reg, NULL);
1970     return DefineSameAsFirst(result);
1971   } else {
1972     LOperand* object = UseFixed(instr->object(), r0);
1973     LOperand* fixed_object_reg = FixedTemp(r2);
1974     LOperand* new_map_reg = FixedTemp(r3);
1975     LTransitionElementsKind* result =
1976         new LTransitionElementsKind(object, new_map_reg, fixed_object_reg);
1977     return MarkAsCall(DefineFixed(result, r0), instr);
1978   }
1979 }
1980
1981
1982 LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
1983   bool needs_write_barrier = instr->NeedsWriteBarrier();
1984
1985   LOperand* obj = needs_write_barrier
1986       ? UseTempRegister(instr->object())
1987       : UseRegisterAtStart(instr->object());
1988
1989   LOperand* val = needs_write_barrier
1990       ? UseTempRegister(instr->value())
1991       : UseRegister(instr->value());
1992
1993   return new LStoreNamedField(obj, val);
1994 }
1995
1996
1997 LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
1998   LOperand* obj = UseFixed(instr->object(), r1);
1999   LOperand* val = UseFixed(instr->value(), r0);
2000
2001   LInstruction* result = new LStoreNamedGeneric(obj, val);
2002   return MarkAsCall(result, instr);
2003 }
2004
2005
2006 LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
2007   LOperand* left = UseRegisterAtStart(instr->left());
2008   LOperand* right = UseRegisterAtStart(instr->right());
2009   return MarkAsCall(DefineFixed(new LStringAdd(left, right), r0), instr);
2010 }
2011
2012
2013 LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
2014   LOperand* string = UseTempRegister(instr->string());
2015   LOperand* index = UseTempRegister(instr->index());
2016   LStringCharCodeAt* result = new LStringCharCodeAt(string, index);
2017   return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
2018 }
2019
2020
2021 LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2022   LOperand* char_code = UseRegister(instr->value());
2023   LStringCharFromCode* result = new LStringCharFromCode(char_code);
2024   return AssignPointerMap(DefineAsRegister(result));
2025 }
2026
2027
2028 LInstruction* LChunkBuilder::DoStringLength(HStringLength* instr) {
2029   LOperand* string = UseRegisterAtStart(instr->value());
2030   return DefineAsRegister(new LStringLength(string));
2031 }
2032
2033
2034 LInstruction* LChunkBuilder::DoArrayLiteral(HArrayLiteral* instr) {
2035   return MarkAsCall(DefineFixed(new LArrayLiteral, r0), instr);
2036 }
2037
2038
2039 LInstruction* LChunkBuilder::DoObjectLiteral(HObjectLiteral* instr) {
2040   return MarkAsCall(DefineFixed(new LObjectLiteral, r0), instr);
2041 }
2042
2043
2044 LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2045   return MarkAsCall(DefineFixed(new LRegExpLiteral, r0), instr);
2046 }
2047
2048
2049 LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2050   return MarkAsCall(DefineFixed(new LFunctionLiteral, r0), instr);
2051 }
2052
2053
2054 LInstruction* LChunkBuilder::DoDeleteProperty(HDeleteProperty* instr) {
2055   LOperand* object = UseFixed(instr->object(), r0);
2056   LOperand* key = UseFixed(instr->key(), r1);
2057   LDeleteProperty* result = new LDeleteProperty(object, key);
2058   return MarkAsCall(DefineFixed(result, r0), instr);
2059 }
2060
2061
2062 LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2063   allocator_->MarkAsOsrEntry();
2064   current_block_->last_environment()->set_ast_id(instr->ast_id());
2065   return AssignEnvironment(new LOsrEntry);
2066 }
2067
2068
2069 LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2070   int spill_index = chunk()->GetParameterStackSlot(instr->index());
2071   return DefineAsSpilled(new LParameter, spill_index);
2072 }
2073
2074
2075 LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2076   int spill_index = chunk()->GetNextSpillIndex(false);  // Not double-width.
2077   if (spill_index > LUnallocated::kMaxFixedIndex) {
2078     Abort("Too many spill slots needed for OSR");
2079     spill_index = 0;
2080   }
2081   return DefineAsSpilled(new LUnknownOSRValue, spill_index);
2082 }
2083
2084
2085 LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2086   argument_count_ -= instr->argument_count();
2087   return MarkAsCall(DefineFixed(new LCallStub, r0), instr);
2088 }
2089
2090
2091 LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
2092   // There are no real uses of the arguments object.
2093   // arguments.length and element access are supported directly on
2094   // stack arguments, and any real arguments object use causes a bailout.
2095   // So this value is never used.
2096   return NULL;
2097 }
2098
2099
2100 LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2101   LOperand* arguments = UseRegister(instr->arguments());
2102   LOperand* length = UseTempRegister(instr->length());
2103   LOperand* index = UseRegister(instr->index());
2104   LAccessArgumentsAt* result = new LAccessArgumentsAt(arguments, length, index);
2105   return AssignEnvironment(DefineAsRegister(result));
2106 }
2107
2108
2109 LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2110   LOperand* object = UseFixed(instr->value(), r0);
2111   LToFastProperties* result = new LToFastProperties(object);
2112   return MarkAsCall(DefineFixed(result, r0), instr);
2113 }
2114
2115
2116 LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2117   LTypeof* result = new LTypeof(UseFixed(instr->value(), r0));
2118   return MarkAsCall(DefineFixed(result, r0), instr);
2119 }
2120
2121
2122 LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2123   return new LTypeofIsAndBranch(UseTempRegister(instr->value()));
2124 }
2125
2126
2127 LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
2128     HIsConstructCallAndBranch* instr) {
2129   return new LIsConstructCallAndBranch(TempRegister());
2130 }
2131
2132
2133 LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2134   HEnvironment* env = current_block_->last_environment();
2135   ASSERT(env != NULL);
2136
2137   env->set_ast_id(instr->ast_id());
2138
2139   env->Drop(instr->pop_count());
2140   for (int i = 0; i < instr->values()->length(); ++i) {
2141     HValue* value = instr->values()->at(i);
2142     if (instr->HasAssignedIndexAt(i)) {
2143       env->Bind(instr->GetAssignedIndexAt(i), value);
2144     } else {
2145       env->Push(value);
2146     }
2147   }
2148
2149   // If there is an instruction pending deoptimization environment create a
2150   // lazy bailout instruction to capture the environment.
2151   if (pending_deoptimization_ast_id_ == instr->ast_id()) {
2152     LInstruction* result = new LLazyBailout;
2153     result = AssignEnvironment(result);
2154     instruction_pending_deoptimization_environment_->
2155         set_deoptimization_environment(result->environment());
2156     ClearInstructionPendingDeoptimizationEnvironment();
2157     return result;
2158   }
2159
2160   return NULL;
2161 }
2162
2163
2164 LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2165   if (instr->is_function_entry()) {
2166     return MarkAsCall(new LStackCheck, instr);
2167   } else {
2168     ASSERT(instr->is_backwards_branch());
2169     return AssignEnvironment(AssignPointerMap(new LStackCheck));
2170   }
2171 }
2172
2173
2174 LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2175   HEnvironment* outer = current_block_->last_environment();
2176   HConstant* undefined = graph()->GetConstantUndefined();
2177   HEnvironment* inner = outer->CopyForInlining(instr->closure(),
2178                                                instr->function(),
2179                                                undefined,
2180                                                instr->call_kind());
2181   current_block_->UpdateEnvironment(inner);
2182   chunk_->AddInlinedClosure(instr->closure());
2183   return NULL;
2184 }
2185
2186
2187 LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2188   HEnvironment* outer = current_block_->last_environment()->outer();
2189   current_block_->UpdateEnvironment(outer);
2190   return NULL;
2191 }
2192
2193
2194 LInstruction* LChunkBuilder::DoIn(HIn* instr) {
2195   LOperand* key = UseRegisterAtStart(instr->key());
2196   LOperand* object = UseRegisterAtStart(instr->object());
2197   LIn* result = new LIn(key, object);
2198   return MarkAsCall(DefineFixed(result, r0), instr);
2199 }
2200
2201
2202 } }  // namespace v8::internal