Updated V8 from git://github.com/v8/v8.git to 06e55bc22bcb8ddb0a602e54e11971576f2d9d8a
[profile/ivi/qtjsbackend.git] / src / 3rdparty / v8 / src / mips / lithium-mips.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 "mips/lithium-mips.h"
32 #include "mips/lithium-codegen-mips.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 "sll-t";
186     case Token::SAR: return "sra-t";
187     case Token::SHR: return "srl-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("[a2] #%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(), a1);
831     LOperand* right = UseFixed(instr->right(), a0);
832     LArithmeticT* result = new LArithmeticT(op, left, right);
833     return MarkAsCall(DefineFixed(result, v0), 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, a1);
896   LOperand* right_operand = UseFixed(right, a0);
897   LArithmeticT* result = new LArithmeticT(op, left_operand, right_operand);
898   return MarkAsCall(DefineFixed(result, v0), 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 LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
1038   ASSERT(instr->value()->representation().IsTagged());
1039   LOperand* value = UseRegisterAtStart(instr->value());
1040   LOperand* temp = TempRegister();
1041   return new LCmpMapAndBranch(value, temp);
1042 }
1043
1044
1045 LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* length) {
1046   return DefineAsRegister(new LArgumentsLength(UseRegister(length->value())));
1047 }
1048
1049
1050 LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
1051   return DefineAsRegister(new LArgumentsElements);
1052 }
1053
1054
1055 LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
1056   LInstanceOf* result =
1057       new LInstanceOf(UseFixed(instr->left(), a0),
1058                       UseFixed(instr->right(), a1));
1059   return MarkAsCall(DefineFixed(result, v0), instr);
1060 }
1061
1062
1063 LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
1064     HInstanceOfKnownGlobal* instr) {
1065   LInstanceOfKnownGlobal* result =
1066       new LInstanceOfKnownGlobal(UseFixed(instr->left(), a0), FixedTemp(t0));
1067   return MarkAsCall(DefineFixed(result, v0), instr);
1068 }
1069
1070
1071 LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
1072   LOperand* function = UseFixed(instr->function(), a1);
1073   LOperand* receiver = UseFixed(instr->receiver(), a0);
1074   LOperand* length = UseFixed(instr->length(), a2);
1075   LOperand* elements = UseFixed(instr->elements(), a3);
1076   LApplyArguments* result = new LApplyArguments(function,
1077                                                 receiver,
1078                                                 length,
1079                                                 elements);
1080   return MarkAsCall(DefineFixed(result, v0), instr, CAN_DEOPTIMIZE_EAGERLY);
1081 }
1082
1083
1084 LInstruction* LChunkBuilder::DoPushArgument(HPushArgument* instr) {
1085   ++argument_count_;
1086   LOperand* argument = Use(instr->argument());
1087   return new LPushArgument(argument);
1088 }
1089
1090
1091 LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1092   return instr->HasNoUses() ? NULL : DefineAsRegister(new LThisFunction);
1093 }
1094
1095
1096 LInstruction* LChunkBuilder::DoContext(HContext* instr) {
1097   return instr->HasNoUses() ? NULL : DefineAsRegister(new LContext);
1098 }
1099
1100
1101 LInstruction* LChunkBuilder::DoOuterContext(HOuterContext* instr) {
1102   LOperand* context = UseRegisterAtStart(instr->value());
1103   return DefineAsRegister(new LOuterContext(context));
1104 }
1105
1106
1107 LInstruction* LChunkBuilder::DoGlobalObject(HGlobalObject* instr) {
1108   LOperand* context = UseRegisterAtStart(instr->value());
1109   return DefineAsRegister(new LGlobalObject(context));
1110 }
1111
1112
1113 LInstruction* LChunkBuilder::DoGlobalReceiver(HGlobalReceiver* instr) {
1114   LOperand* global_object = UseRegisterAtStart(instr->value());
1115   return DefineAsRegister(new LGlobalReceiver(global_object));
1116 }
1117
1118
1119 LInstruction* LChunkBuilder::DoCallConstantFunction(
1120     HCallConstantFunction* instr) {
1121   argument_count_ -= instr->argument_count();
1122   return MarkAsCall(DefineFixed(new LCallConstantFunction, v0), instr);
1123 }
1124
1125
1126 LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1127   LOperand* function = UseFixed(instr->function(), a1);
1128   argument_count_ -= instr->argument_count();
1129   LInvokeFunction* result = new LInvokeFunction(function);
1130   return MarkAsCall(DefineFixed(result, v0), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1131 }
1132
1133
1134 LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
1135   BuiltinFunctionId op = instr->op();
1136   if (op == kMathLog || op == kMathSin || op == kMathCos) {
1137     LOperand* input = UseFixedDouble(instr->value(), f4);
1138     LUnaryMathOperation* result = new LUnaryMathOperation(input, NULL);
1139     return MarkAsCall(DefineFixedDouble(result, f4), instr);
1140   } else {
1141     LOperand* input = UseRegisterAtStart(instr->value());
1142     LOperand* temp = (op == kMathFloor) ? TempRegister() : NULL;
1143     LUnaryMathOperation* result = new LUnaryMathOperation(input, temp);
1144     switch (op) {
1145       case kMathAbs:
1146         return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1147       case kMathFloor:
1148         return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1149       case kMathSqrt:
1150         return DefineAsRegister(result);
1151       case kMathRound:
1152         return AssignEnvironment(DefineAsRegister(result));
1153       case kMathPowHalf:
1154         return DefineAsRegister(result);
1155       default:
1156         UNREACHABLE();
1157         return NULL;
1158     }
1159   }
1160 }
1161
1162
1163 LInstruction* LChunkBuilder::DoCallKeyed(HCallKeyed* instr) {
1164   ASSERT(instr->key()->representation().IsTagged());
1165   argument_count_ -= instr->argument_count();
1166   LOperand* key = UseFixed(instr->key(), a2);
1167   return MarkAsCall(DefineFixed(new LCallKeyed(key), v0), instr);
1168 }
1169
1170
1171 LInstruction* LChunkBuilder::DoCallNamed(HCallNamed* instr) {
1172   argument_count_ -= instr->argument_count();
1173   return MarkAsCall(DefineFixed(new LCallNamed, v0), instr);
1174 }
1175
1176
1177 LInstruction* LChunkBuilder::DoCallGlobal(HCallGlobal* instr) {
1178   argument_count_ -= instr->argument_count();
1179   return MarkAsCall(DefineFixed(new LCallGlobal, v0), instr);
1180 }
1181
1182
1183 LInstruction* LChunkBuilder::DoCallKnownGlobal(HCallKnownGlobal* instr) {
1184   argument_count_ -= instr->argument_count();
1185   return MarkAsCall(DefineFixed(new LCallKnownGlobal, v0), instr);
1186 }
1187
1188
1189 LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
1190   LOperand* constructor = UseFixed(instr->constructor(), a1);
1191   argument_count_ -= instr->argument_count();
1192   LCallNew* result = new LCallNew(constructor);
1193   return MarkAsCall(DefineFixed(result, v0), instr);
1194 }
1195
1196
1197 LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
1198   argument_count_ -= instr->argument_count();
1199   return MarkAsCall(DefineFixed(new LCallFunction, v0), instr);
1200 }
1201
1202
1203 LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1204   argument_count_ -= instr->argument_count();
1205   return MarkAsCall(DefineFixed(new LCallRuntime, v0), instr);
1206 }
1207
1208
1209 LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1210   return DoShift(Token::SHR, instr);
1211 }
1212
1213
1214 LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1215   return DoShift(Token::SAR, instr);
1216 }
1217
1218
1219 LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1220   return DoShift(Token::SHL, instr);
1221 }
1222
1223
1224 LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
1225   if (instr->representation().IsInteger32()) {
1226     ASSERT(instr->left()->representation().IsInteger32());
1227     ASSERT(instr->right()->representation().IsInteger32());
1228
1229     LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1230     LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1231     return DefineAsRegister(new LBitI(left, right));
1232   } else {
1233     ASSERT(instr->representation().IsTagged());
1234     ASSERT(instr->left()->representation().IsTagged());
1235     ASSERT(instr->right()->representation().IsTagged());
1236
1237     LOperand* left = UseFixed(instr->left(), a1);
1238     LOperand* right = UseFixed(instr->right(), a0);
1239     LArithmeticT* result = new LArithmeticT(instr->op(), left, right);
1240     return MarkAsCall(DefineFixed(result, v0), instr);
1241   }
1242 }
1243
1244
1245 LInstruction* LChunkBuilder::DoBitNot(HBitNot* instr) {
1246   ASSERT(instr->value()->representation().IsInteger32());
1247   ASSERT(instr->representation().IsInteger32());
1248   return DefineAsRegister(new LBitNotI(UseRegisterAtStart(instr->value())));
1249 }
1250
1251
1252 LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
1253   if (instr->representation().IsDouble()) {
1254     return DoArithmeticD(Token::DIV, instr);
1255   } else if (instr->representation().IsInteger32()) {
1256     // TODO(1042) The fixed register allocation
1257     // is needed because we call TypeRecordingBinaryOpStub from
1258     // the generated code, which requires registers a0
1259     // and a1 to be used. We should remove that
1260     // when we provide a native implementation.
1261     LOperand* dividend = UseFixed(instr->left(), a0);
1262     LOperand* divisor = UseFixed(instr->right(), a1);
1263     return AssignEnvironment(AssignPointerMap(
1264              DefineFixed(new LDivI(dividend, divisor), v0)));
1265   } else {
1266     return DoArithmeticT(Token::DIV, instr);
1267   }
1268 }
1269
1270
1271 LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1272   if (instr->representation().IsInteger32()) {
1273     ASSERT(instr->left()->representation().IsInteger32());
1274     ASSERT(instr->right()->representation().IsInteger32());
1275
1276     LModI* mod;
1277     if (instr->HasPowerOf2Divisor()) {
1278       ASSERT(!instr->CheckFlag(HValue::kCanBeDivByZero));
1279       LOperand* value = UseRegisterAtStart(instr->left());
1280       mod = new LModI(value, UseOrConstant(instr->right()));
1281     } else {
1282       LOperand* dividend = UseRegister(instr->left());
1283       LOperand* divisor = UseRegister(instr->right());
1284       mod = new LModI(dividend,
1285                       divisor,
1286                       TempRegister(),
1287                       FixedTemp(f20),
1288                       FixedTemp(f22));
1289     }
1290
1291     if (instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1292         instr->CheckFlag(HValue::kCanBeDivByZero)) {
1293       return AssignEnvironment(DefineAsRegister(mod));
1294     } else {
1295       return DefineAsRegister(mod);
1296     }
1297   } else if (instr->representation().IsTagged()) {
1298     return DoArithmeticT(Token::MOD, instr);
1299   } else {
1300     ASSERT(instr->representation().IsDouble());
1301     // We call a C function for double modulo. It can't trigger a GC.
1302     // We need to use fixed result register for the call.
1303     // TODO(fschneider): Allow any register as input registers.
1304     LOperand* left = UseFixedDouble(instr->left(), f2);
1305     LOperand* right = UseFixedDouble(instr->right(), f4);
1306     LArithmeticD* result = new LArithmeticD(Token::MOD, left, right);
1307     return MarkAsCall(DefineFixedDouble(result, f2), instr);
1308   }
1309 }
1310
1311
1312 LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1313   if (instr->representation().IsInteger32()) {
1314     ASSERT(instr->left()->representation().IsInteger32());
1315     ASSERT(instr->right()->representation().IsInteger32());
1316     LOperand* left;
1317     LOperand* right = UseOrConstant(instr->MostConstantOperand());
1318     LOperand* temp = NULL;
1319     if (instr->CheckFlag(HValue::kBailoutOnMinusZero) &&
1320         (instr->CheckFlag(HValue::kCanOverflow) ||
1321         !right->IsConstantOperand())) {
1322       left = UseRegister(instr->LeastConstantOperand());
1323       temp = TempRegister();
1324     } else {
1325       left = UseRegisterAtStart(instr->LeastConstantOperand());
1326     }
1327     return AssignEnvironment(DefineAsRegister(new LMulI(left, right, temp)));
1328
1329   } else if (instr->representation().IsDouble()) {
1330     return DoArithmeticD(Token::MUL, instr);
1331
1332   } else {
1333     return DoArithmeticT(Token::MUL, instr);
1334   }
1335 }
1336
1337
1338 LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1339   if (instr->representation().IsInteger32()) {
1340     ASSERT(instr->left()->representation().IsInteger32());
1341     ASSERT(instr->right()->representation().IsInteger32());
1342     LOperand* left = UseRegisterAtStart(instr->left());
1343     LOperand* right = UseOrConstantAtStart(instr->right());
1344     LSubI* sub = new LSubI(left, right);
1345     LInstruction* result = DefineAsRegister(sub);
1346     if (instr->CheckFlag(HValue::kCanOverflow)) {
1347       result = AssignEnvironment(result);
1348     }
1349     return result;
1350   } else if (instr->representation().IsDouble()) {
1351     return DoArithmeticD(Token::SUB, instr);
1352   } else {
1353     return DoArithmeticT(Token::SUB, instr);
1354   }
1355 }
1356
1357
1358 LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1359   if (instr->representation().IsInteger32()) {
1360     ASSERT(instr->left()->representation().IsInteger32());
1361     ASSERT(instr->right()->representation().IsInteger32());
1362     LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1363     LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1364     LAddI* add = new LAddI(left, right);
1365     LInstruction* result = DefineAsRegister(add);
1366     if (instr->CheckFlag(HValue::kCanOverflow)) {
1367       result = AssignEnvironment(result);
1368     }
1369     return result;
1370   } else if (instr->representation().IsDouble()) {
1371     return DoArithmeticD(Token::ADD, instr);
1372   } else {
1373     ASSERT(instr->representation().IsTagged());
1374     return DoArithmeticT(Token::ADD, instr);
1375   }
1376 }
1377
1378
1379 LInstruction* LChunkBuilder::DoPower(HPower* instr) {
1380   ASSERT(instr->representation().IsDouble());
1381   // We call a C function for double power. It can't trigger a GC.
1382   // We need to use fixed result register for the call.
1383   Representation exponent_type = instr->right()->representation();
1384   ASSERT(instr->left()->representation().IsDouble());
1385   LOperand* left = UseFixedDouble(instr->left(), f2);
1386   LOperand* right = exponent_type.IsDouble() ?
1387       UseFixedDouble(instr->right(), f4) :
1388       UseFixed(instr->right(), a0);
1389   LPower* result = new LPower(left, right);
1390   return MarkAsCall(DefineFixedDouble(result, f6),
1391                     instr,
1392                     CAN_DEOPTIMIZE_EAGERLY);
1393 }
1394
1395
1396 LInstruction* LChunkBuilder::DoCompareGeneric(HCompareGeneric* instr) {
1397   Representation r = instr->GetInputRepresentation();
1398   ASSERT(instr->left()->representation().IsTagged());
1399   ASSERT(instr->right()->representation().IsTagged());
1400   LOperand* left = UseFixed(instr->left(), a1);
1401   LOperand* right = UseFixed(instr->right(), a0);
1402   LCmpT* result = new LCmpT(left, right);
1403   return MarkAsCall(DefineFixed(result, v0), 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(), a0);
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(f22)
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     // Revisit this decision, here and 8 lines below.
1672     return DefineAsRegister(new LClampDToUint8(reg, FixedTemp(f22)));
1673   } else if (input_rep.IsInteger32()) {
1674     return DefineAsRegister(new LClampIToUint8(reg));
1675   } else {
1676     ASSERT(input_rep.IsTagged());
1677     // Register allocator doesn't (yet) support allocation of double
1678     // temps. Reserve f22 explicitly.
1679     LClampTToUint8* result = new LClampTToUint8(reg, FixedTemp(f22));
1680     return AssignEnvironment(DefineAsRegister(result));
1681   }
1682 }
1683
1684
1685 LInstruction* LChunkBuilder::DoToInt32(HToInt32* instr) {
1686   HValue* value = instr->value();
1687   Representation input_rep = value->representation();
1688   LOperand* reg = UseRegister(value);
1689   if (input_rep.IsDouble()) {
1690     LOperand* temp1 = TempRegister();
1691     LOperand* temp2 = TempRegister();
1692     LDoubleToI* res = new LDoubleToI(reg, temp1, temp2);
1693     return AssignEnvironment(DefineAsRegister(res));
1694   } else if (input_rep.IsInteger32()) {
1695     // Canonicalization should already have removed the hydrogen instruction in
1696     // this case, since it is a noop.
1697     UNREACHABLE();
1698     return NULL;
1699   } else {
1700     ASSERT(input_rep.IsTagged());
1701     LOperand* temp1 = TempRegister();
1702     LOperand* temp2 = TempRegister();
1703     LOperand* temp3 = FixedTemp(f22);
1704     LTaggedToI* res = new LTaggedToI(reg, temp1, temp2, temp3);
1705     return AssignEnvironment(DefineSameAsFirst(res));
1706   }
1707 }
1708
1709
1710 LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
1711   return new LReturn(UseFixed(instr->value(), v0));
1712 }
1713
1714
1715 LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
1716   Representation r = instr->representation();
1717   if (r.IsInteger32()) {
1718     return DefineAsRegister(new LConstantI);
1719   } else if (r.IsDouble()) {
1720     return DefineAsRegister(new LConstantD);
1721   } else if (r.IsTagged()) {
1722     return DefineAsRegister(new LConstantT);
1723   } else {
1724     UNREACHABLE();
1725     return NULL;
1726   }
1727 }
1728
1729
1730 LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
1731   LLoadGlobalCell* result = new LLoadGlobalCell;
1732   return instr->RequiresHoleCheck()
1733       ? AssignEnvironment(DefineAsRegister(result))
1734       : DefineAsRegister(result);
1735 }
1736
1737
1738 LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
1739   LOperand* global_object = UseFixed(instr->global_object(), a0);
1740   LLoadGlobalGeneric* result = new LLoadGlobalGeneric(global_object);
1741   return MarkAsCall(DefineFixed(result, v0), instr);
1742 }
1743
1744
1745 LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
1746   LOperand* temp = TempRegister();
1747   LOperand* value = UseTempRegister(instr->value());
1748   LInstruction* result = new LStoreGlobalCell(value, temp);
1749   if (instr->RequiresHoleCheck()) result = AssignEnvironment(result);
1750   return result;
1751 }
1752
1753
1754 LInstruction* LChunkBuilder::DoStoreGlobalGeneric(HStoreGlobalGeneric* instr) {
1755   LOperand* global_object = UseFixed(instr->global_object(), a1);
1756   LOperand* value = UseFixed(instr->value(), a0);
1757   LStoreGlobalGeneric* result =
1758       new LStoreGlobalGeneric(global_object, value);
1759   return MarkAsCall(result, instr);
1760 }
1761
1762
1763 LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
1764   LOperand* context = UseRegisterAtStart(instr->value());
1765   return DefineAsRegister(new LLoadContextSlot(context));
1766 }
1767
1768
1769 LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
1770   LOperand* context;
1771   LOperand* value;
1772   if (instr->NeedsWriteBarrier()) {
1773     context = UseTempRegister(instr->context());
1774     value = UseTempRegister(instr->value());
1775   } else {
1776     context = UseRegister(instr->context());
1777     value = UseRegister(instr->value());
1778   }
1779   return new LStoreContextSlot(context, value);
1780 }
1781
1782
1783 LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
1784   return DefineAsRegister(
1785       new LLoadNamedField(UseRegisterAtStart(instr->object())));
1786 }
1787
1788
1789 LInstruction* LChunkBuilder::DoLoadNamedFieldPolymorphic(
1790     HLoadNamedFieldPolymorphic* instr) {
1791   ASSERT(instr->representation().IsTagged());
1792   if (instr->need_generic()) {
1793     LOperand* obj = UseFixed(instr->object(), a0);
1794     LLoadNamedFieldPolymorphic* result = new LLoadNamedFieldPolymorphic(obj);
1795     return MarkAsCall(DefineFixed(result, v0), instr);
1796   } else {
1797     LOperand* obj = UseRegisterAtStart(instr->object());
1798     LLoadNamedFieldPolymorphic* result = new LLoadNamedFieldPolymorphic(obj);
1799     return AssignEnvironment(DefineAsRegister(result));
1800   }
1801 }
1802
1803
1804 LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
1805   LOperand* object = UseFixed(instr->object(), a0);
1806   LInstruction* result = DefineFixed(new LLoadNamedGeneric(object), v0);
1807   return MarkAsCall(result, instr);
1808 }
1809
1810
1811 LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
1812     HLoadFunctionPrototype* instr) {
1813   return AssignEnvironment(DefineAsRegister(
1814       new LLoadFunctionPrototype(UseRegister(instr->function()))));
1815 }
1816
1817
1818 LInstruction* LChunkBuilder::DoLoadElements(HLoadElements* instr) {
1819   LOperand* input = UseRegisterAtStart(instr->value());
1820   return DefineAsRegister(new LLoadElements(input));
1821 }
1822
1823
1824 LInstruction* LChunkBuilder::DoLoadExternalArrayPointer(
1825     HLoadExternalArrayPointer* instr) {
1826   LOperand* input = UseRegisterAtStart(instr->value());
1827   return DefineAsRegister(new LLoadExternalArrayPointer(input));
1828 }
1829
1830
1831 LInstruction* LChunkBuilder::DoLoadKeyedFastElement(
1832     HLoadKeyedFastElement* instr) {
1833   ASSERT(instr->representation().IsTagged());
1834   ASSERT(instr->key()->representation().IsInteger32());
1835   LOperand* obj = UseRegisterAtStart(instr->object());
1836   LOperand* key = UseRegisterAtStart(instr->key());
1837   LLoadKeyedFastElement* result = new LLoadKeyedFastElement(obj, key);
1838   return AssignEnvironment(DefineAsRegister(result));
1839 }
1840
1841
1842 LInstruction* LChunkBuilder::DoLoadKeyedFastDoubleElement(
1843     HLoadKeyedFastDoubleElement* instr) {
1844   ASSERT(instr->representation().IsDouble());
1845   ASSERT(instr->key()->representation().IsInteger32());
1846   LOperand* elements = UseTempRegister(instr->elements());
1847   LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1848   LLoadKeyedFastDoubleElement* result =
1849       new LLoadKeyedFastDoubleElement(elements, key);
1850   return AssignEnvironment(DefineAsRegister(result));
1851 }
1852
1853
1854 LInstruction* LChunkBuilder::DoLoadKeyedSpecializedArrayElement(
1855     HLoadKeyedSpecializedArrayElement* instr) {
1856   ElementsKind elements_kind = instr->elements_kind();
1857   Representation representation(instr->representation());
1858   ASSERT(
1859       (representation.IsInteger32() &&
1860        (elements_kind != EXTERNAL_FLOAT_ELEMENTS) &&
1861        (elements_kind != EXTERNAL_DOUBLE_ELEMENTS)) ||
1862       (representation.IsDouble() &&
1863        ((elements_kind == EXTERNAL_FLOAT_ELEMENTS) ||
1864        (elements_kind == EXTERNAL_DOUBLE_ELEMENTS))));
1865   ASSERT(instr->key()->representation().IsInteger32());
1866   LOperand* external_pointer = UseRegister(instr->external_pointer());
1867   LOperand* key = UseRegisterOrConstant(instr->key());
1868   LLoadKeyedSpecializedArrayElement* result =
1869       new LLoadKeyedSpecializedArrayElement(external_pointer, key);
1870   LInstruction* load_instr = DefineAsRegister(result);
1871   // An unsigned int array load might overflow and cause a deopt, make sure it
1872   // has an environment.
1873   return (elements_kind == EXTERNAL_UNSIGNED_INT_ELEMENTS) ?
1874       AssignEnvironment(load_instr) : load_instr;
1875 }
1876
1877
1878 LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
1879   LOperand* object = UseFixed(instr->object(), a1);
1880   LOperand* key = UseFixed(instr->key(), a0);
1881
1882   LInstruction* result =
1883       DefineFixed(new LLoadKeyedGeneric(object, key), v0);
1884   return MarkAsCall(result, instr);
1885 }
1886
1887
1888 LInstruction* LChunkBuilder::DoStoreKeyedFastElement(
1889     HStoreKeyedFastElement* instr) {
1890   bool needs_write_barrier = instr->NeedsWriteBarrier();
1891   ASSERT(instr->value()->representation().IsTagged());
1892   ASSERT(instr->object()->representation().IsTagged());
1893   ASSERT(instr->key()->representation().IsInteger32());
1894
1895   LOperand* obj = UseTempRegister(instr->object());
1896   LOperand* val = needs_write_barrier
1897       ? UseTempRegister(instr->value())
1898       : UseRegisterAtStart(instr->value());
1899   LOperand* key = needs_write_barrier
1900       ? UseTempRegister(instr->key())
1901       : UseRegisterOrConstantAtStart(instr->key());
1902
1903   return AssignEnvironment(new LStoreKeyedFastElement(obj, key, val));
1904 }
1905
1906
1907 LInstruction* LChunkBuilder::DoStoreKeyedFastDoubleElement(
1908     HStoreKeyedFastDoubleElement* instr) {
1909   ASSERT(instr->value()->representation().IsDouble());
1910   ASSERT(instr->elements()->representation().IsTagged());
1911   ASSERT(instr->key()->representation().IsInteger32());
1912
1913   LOperand* elements = UseRegisterAtStart(instr->elements());
1914   LOperand* val = UseTempRegister(instr->value());
1915   LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1916
1917   return new LStoreKeyedFastDoubleElement(elements, key, val);
1918 }
1919
1920
1921 LInstruction* LChunkBuilder::DoStoreKeyedSpecializedArrayElement(
1922     HStoreKeyedSpecializedArrayElement* instr) {
1923   Representation representation(instr->value()->representation());
1924   ElementsKind elements_kind = instr->elements_kind();
1925   ASSERT(
1926       (representation.IsInteger32() &&
1927        (elements_kind != EXTERNAL_FLOAT_ELEMENTS) &&
1928        (elements_kind != EXTERNAL_DOUBLE_ELEMENTS)) ||
1929       (representation.IsDouble() &&
1930        ((elements_kind == EXTERNAL_FLOAT_ELEMENTS) ||
1931        (elements_kind == EXTERNAL_DOUBLE_ELEMENTS))));
1932   ASSERT(instr->external_pointer()->representation().IsExternal());
1933   ASSERT(instr->key()->representation().IsInteger32());
1934
1935   LOperand* external_pointer = UseRegister(instr->external_pointer());
1936   bool val_is_temp_register =
1937       elements_kind == EXTERNAL_PIXEL_ELEMENTS ||
1938       elements_kind == EXTERNAL_FLOAT_ELEMENTS;
1939   LOperand* val = val_is_temp_register
1940       ? UseTempRegister(instr->value())
1941       : UseRegister(instr->value());
1942   LOperand* key = UseRegisterOrConstant(instr->key());
1943
1944   return new LStoreKeyedSpecializedArrayElement(external_pointer,
1945                                                 key,
1946                                                 val);
1947 }
1948
1949
1950 LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
1951   LOperand* obj = UseFixed(instr->object(), a2);
1952   LOperand* key = UseFixed(instr->key(), a1);
1953   LOperand* val = UseFixed(instr->value(), a0);
1954
1955   ASSERT(instr->object()->representation().IsTagged());
1956   ASSERT(instr->key()->representation().IsTagged());
1957   ASSERT(instr->value()->representation().IsTagged());
1958
1959   return MarkAsCall(new LStoreKeyedGeneric(obj, key, val), instr);
1960 }
1961
1962
1963 LInstruction* LChunkBuilder::DoTransitionElementsKind(
1964     HTransitionElementsKind* instr) {
1965   if (instr->original_map()->elements_kind() == FAST_SMI_ONLY_ELEMENTS &&
1966       instr->transitioned_map()->elements_kind() == FAST_ELEMENTS) {
1967     LOperand* object = UseRegister(instr->object());
1968     LOperand* new_map_reg = TempRegister();
1969     LTransitionElementsKind* result =
1970         new LTransitionElementsKind(object, new_map_reg, NULL);
1971     return DefineSameAsFirst(result);
1972   } else {
1973     LOperand* object = UseFixed(instr->object(), a0);
1974     LOperand* fixed_object_reg = FixedTemp(a2);
1975     LOperand* new_map_reg = FixedTemp(a3);
1976     LTransitionElementsKind* result =
1977         new LTransitionElementsKind(object, new_map_reg, fixed_object_reg);
1978     return MarkAsCall(DefineFixed(result, v0), instr);
1979   }
1980 }
1981
1982
1983 LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
1984   bool needs_write_barrier = instr->NeedsWriteBarrier();
1985
1986   LOperand* obj = needs_write_barrier
1987       ? UseTempRegister(instr->object())
1988       : UseRegisterAtStart(instr->object());
1989
1990   LOperand* val = needs_write_barrier
1991       ? UseTempRegister(instr->value())
1992       : UseRegister(instr->value());
1993
1994   return new LStoreNamedField(obj, val);
1995 }
1996
1997
1998 LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
1999   LOperand* obj = UseFixed(instr->object(), a1);
2000   LOperand* val = UseFixed(instr->value(), a0);
2001
2002   LInstruction* result = new LStoreNamedGeneric(obj, val);
2003   return MarkAsCall(result, instr);
2004 }
2005
2006
2007 LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
2008   LOperand* left = UseRegisterAtStart(instr->left());
2009   LOperand* right = UseRegisterAtStart(instr->right());
2010   return MarkAsCall(DefineFixed(new LStringAdd(left, right), v0), instr);
2011 }
2012
2013
2014 LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
2015   LOperand* string = UseTempRegister(instr->string());
2016   LOperand* index = UseTempRegister(instr->index());
2017   LStringCharCodeAt* result = new LStringCharCodeAt(string, index);
2018   return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
2019 }
2020
2021
2022 LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2023   LOperand* char_code = UseRegister(instr->value());
2024   LStringCharFromCode* result = new LStringCharFromCode(char_code);
2025   return AssignPointerMap(DefineAsRegister(result));
2026 }
2027
2028
2029 LInstruction* LChunkBuilder::DoStringLength(HStringLength* instr) {
2030   LOperand* string = UseRegisterAtStart(instr->value());
2031   return DefineAsRegister(new LStringLength(string));
2032 }
2033
2034
2035 LInstruction* LChunkBuilder::DoArrayLiteral(HArrayLiteral* instr) {
2036   return MarkAsCall(DefineFixed(new LArrayLiteral, v0), instr);
2037 }
2038
2039
2040 LInstruction* LChunkBuilder::DoObjectLiteral(HObjectLiteral* instr) {
2041   return MarkAsCall(DefineFixed(new LObjectLiteral, v0), instr);
2042 }
2043
2044
2045 LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2046   return MarkAsCall(DefineFixed(new LRegExpLiteral, v0), instr);
2047 }
2048
2049
2050 LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2051   return MarkAsCall(DefineFixed(new LFunctionLiteral, v0), instr);
2052 }
2053
2054
2055 LInstruction* LChunkBuilder::DoDeleteProperty(HDeleteProperty* instr) {
2056   LOperand* object = UseFixed(instr->object(), a0);
2057   LOperand* key = UseFixed(instr->key(), a1);
2058   LDeleteProperty* result = new LDeleteProperty(object, key);
2059   return MarkAsCall(DefineFixed(result, v0), instr);
2060 }
2061
2062
2063 LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2064   allocator_->MarkAsOsrEntry();
2065   current_block_->last_environment()->set_ast_id(instr->ast_id());
2066   return AssignEnvironment(new LOsrEntry);
2067 }
2068
2069
2070 LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2071   int spill_index = chunk()->GetParameterStackSlot(instr->index());
2072   return DefineAsSpilled(new LParameter, spill_index);
2073 }
2074
2075
2076 LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2077   int spill_index = chunk()->GetNextSpillIndex(false);  // Not double-width.
2078   if (spill_index > LUnallocated::kMaxFixedIndex) {
2079     Abort("Too many spill slots needed for OSR");
2080     spill_index = 0;
2081   }
2082   return DefineAsSpilled(new LUnknownOSRValue, spill_index);
2083 }
2084
2085
2086 LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2087   argument_count_ -= instr->argument_count();
2088   return MarkAsCall(DefineFixed(new LCallStub, v0), instr);
2089 }
2090
2091
2092 LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
2093   // There are no real uses of the arguments object.
2094   // arguments.length and element access are supported directly on
2095   // stack arguments, and any real arguments object use causes a bailout.
2096   // So this value is never used.
2097   return NULL;
2098 }
2099
2100
2101 LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2102   LOperand* arguments = UseRegister(instr->arguments());
2103   LOperand* length = UseTempRegister(instr->length());
2104   LOperand* index = UseRegister(instr->index());
2105   LAccessArgumentsAt* result = new LAccessArgumentsAt(arguments, length, index);
2106   return AssignEnvironment(DefineAsRegister(result));
2107 }
2108
2109
2110 LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2111   LOperand* object = UseFixed(instr->value(), a0);
2112   LToFastProperties* result = new LToFastProperties(object);
2113   return MarkAsCall(DefineFixed(result, v0), instr);
2114 }
2115
2116
2117 LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2118   LTypeof* result = new LTypeof(UseFixed(instr->value(), a0));
2119   return MarkAsCall(DefineFixed(result, v0), instr);
2120 }
2121
2122
2123 LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2124   return new LTypeofIsAndBranch(UseTempRegister(instr->value()));
2125 }
2126
2127
2128 LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
2129     HIsConstructCallAndBranch* instr) {
2130   return new LIsConstructCallAndBranch(TempRegister());
2131 }
2132
2133
2134 LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2135   HEnvironment* env = current_block_->last_environment();
2136   ASSERT(env != NULL);
2137
2138   env->set_ast_id(instr->ast_id());
2139
2140   env->Drop(instr->pop_count());
2141   for (int i = 0; i < instr->values()->length(); ++i) {
2142     HValue* value = instr->values()->at(i);
2143     if (instr->HasAssignedIndexAt(i)) {
2144       env->Bind(instr->GetAssignedIndexAt(i), value);
2145     } else {
2146       env->Push(value);
2147     }
2148   }
2149
2150   // If there is an instruction pending deoptimization environment create a
2151   // lazy bailout instruction to capture the environment.
2152   if (pending_deoptimization_ast_id_ == instr->ast_id()) {
2153     LInstruction* result = new LLazyBailout;
2154     result = AssignEnvironment(result);
2155     instruction_pending_deoptimization_environment_->
2156         set_deoptimization_environment(result->environment());
2157     ClearInstructionPendingDeoptimizationEnvironment();
2158     return result;
2159   }
2160
2161   return NULL;
2162 }
2163
2164
2165 LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2166   if (instr->is_function_entry()) {
2167     return MarkAsCall(new LStackCheck, instr);
2168   } else {
2169     ASSERT(instr->is_backwards_branch());
2170     return AssignEnvironment(AssignPointerMap(new LStackCheck));
2171   }
2172 }
2173
2174
2175 LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2176   HEnvironment* outer = current_block_->last_environment();
2177   HConstant* undefined = graph()->GetConstantUndefined();
2178   HEnvironment* inner = outer->CopyForInlining(instr->closure(),
2179                                                instr->function(),
2180                                                undefined,
2181                                                instr->call_kind());
2182   current_block_->UpdateEnvironment(inner);
2183   chunk_->AddInlinedClosure(instr->closure());
2184   return NULL;
2185 }
2186
2187
2188 LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2189   HEnvironment* outer = current_block_->last_environment()->outer();
2190   current_block_->UpdateEnvironment(outer);
2191   return NULL;
2192 }
2193
2194
2195 LInstruction* LChunkBuilder::DoIn(HIn* instr) {
2196   LOperand* key = UseRegisterAtStart(instr->key());
2197   LOperand* object = UseRegisterAtStart(instr->object());
2198   LIn* result = new LIn(key, object);
2199   return MarkAsCall(DefineFixed(result, v0), instr);
2200 }
2201
2202
2203 } }  // namespace v8::internal