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