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
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.
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.
30 #include "lithium-allocator-inl.h"
31 #include "mips/lithium-mips.h"
32 #include "mips/lithium-codegen-mips.h"
37 #define DEFINE_COMPILE(type) \
38 void L##type::CompileToNative(LCodeGen* generator) { \
39 generator->Do##type(this); \
41 LITHIUM_CONCRETE_INSTRUCTION_LIST(DEFINE_COMPILE)
44 LOsrEntry::LOsrEntry() {
45 for (int i = 0; i < Register::kNumAllocatableRegisters; ++i) {
46 register_spills_[i] = NULL;
48 for (int i = 0; i < DoubleRegister::kNumAllocatableRegisters; ++i) {
49 double_register_spills_[i] = NULL;
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;
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());
76 for (TempIterator it(this); !it.Done(); it.Advance()) {
77 LUnallocated* operand = LUnallocated::cast(it.Current());
78 ASSERT(operand->HasFixedPolicy() ||!operand->HasRegisterPolicy());
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;
92 void LInstruction::PrintTo(StringStream* stream) {
93 stream->Add("%s ", this->Mnemonic());
95 PrintOutputOperandTo(stream);
99 if (HasEnvironment()) {
101 environment()->PrintTo(stream);
104 if (HasPointerMap()) {
106 pointer_map()->PrintTo(stream);
111 void LInstruction::PrintDataTo(StringStream* stream) {
113 for (int i = 0; i < InputCount(); i++) {
114 if (i > 0) stream->Add(" ");
115 InputAt(i)->PrintTo(stream);
120 void LInstruction::PrintOutputOperandTo(StringStream* stream) {
121 if (HasResult()) result()->PrintTo(stream);
125 void LLabel::PrintDataTo(StringStream* stream) {
126 LGap::PrintDataTo(stream);
127 LLabel* rep = replacement();
129 stream->Add(" Dead block replaced with B%d", rep->block_id());
134 bool LGap::IsRedundant() const {
135 for (int i = 0; i < 4; i++) {
136 if (parallel_moves_[i] != NULL && !parallel_moves_[i]->IsRedundant()) {
145 void LGap::PrintDataTo(StringStream* stream) {
146 for (int i = 0; i < 4; i++) {
148 if (parallel_moves_[i] != NULL) {
149 parallel_moves_[i]->PrintDataTo(stream);
156 const char* LArithmeticD::Mnemonic() const {
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";
170 const char* LArithmeticT::Mnemonic() const {
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";
190 void LGoto::PrintDataTo(StringStream* stream) {
191 stream->Add("B%d", block_id());
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);
201 void LCmpIDAndBranch::PrintDataTo(StringStream* stream) {
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());
210 void LIsNilAndBranch::PrintDataTo(StringStream* stream) {
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());
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());
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());
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());
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());
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());
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());
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());
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(),
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());
288 void LCallConstantFunction::PrintDataTo(StringStream* stream) {
289 stream->Add("#%d / ", arity());
293 void LUnaryMathOperation::PrintDataTo(StringStream* stream) {
294 stream->Add("/%s ", hydrogen()->OpName());
295 InputAt(0)->PrintTo(stream);
299 void LLoadContextSlot::PrintDataTo(StringStream* stream) {
300 InputAt(0)->PrintTo(stream);
301 stream->Add("[%d]", slot_index());
305 void LStoreContextSlot::PrintDataTo(StringStream* stream) {
306 InputAt(0)->PrintTo(stream);
307 stream->Add("[%d] <- ", slot_index());
308 InputAt(1)->PrintTo(stream);
312 void LInvokeFunction::PrintDataTo(StringStream* stream) {
314 InputAt(0)->PrintTo(stream);
315 stream->Add(" #%d / ", arity());
319 void LCallKeyed::PrintDataTo(StringStream* stream) {
320 stream->Add("[a2] #%d / ", arity());
324 void LCallNamed::PrintDataTo(StringStream* stream) {
325 SmartArrayPointer<char> name_string = name()->ToCString();
326 stream->Add("%s #%d / ", *name_string, arity());
330 void LCallGlobal::PrintDataTo(StringStream* stream) {
331 SmartArrayPointer<char> name_string = name()->ToCString();
332 stream->Add("%s #%d / ", *name_string, arity());
336 void LCallKnownGlobal::PrintDataTo(StringStream* stream) {
337 stream->Add("#%d / ", arity());
341 void LCallNew::PrintDataTo(StringStream* stream) {
343 InputAt(0)->PrintTo(stream);
344 stream->Add(" #%d / ", arity());
348 void LAccessArgumentsAt::PrintDataTo(StringStream* stream) {
349 arguments()->PrintTo(stream);
351 stream->Add(" length ");
352 length()->PrintTo(stream);
354 stream->Add(" index ");
355 index()->PrintTo(stream);
359 void LStoreNamedField::PrintDataTo(StringStream* stream) {
360 object()->PrintTo(stream);
362 stream->Add(*String::cast(*name())->ToCString());
364 value()->PrintTo(stream);
368 void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
369 object()->PrintTo(stream);
371 stream->Add(*String::cast(*name())->ToCString());
373 value()->PrintTo(stream);
377 void LStoreKeyedFastElement::PrintDataTo(StringStream* stream) {
378 object()->PrintTo(stream);
380 key()->PrintTo(stream);
381 stream->Add("] <- ");
382 value()->PrintTo(stream);
386 void LStoreKeyedFastDoubleElement::PrintDataTo(StringStream* stream) {
387 elements()->PrintTo(stream);
389 key()->PrintTo(stream);
390 stream->Add("] <- ");
391 value()->PrintTo(stream);
395 void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
396 object()->PrintTo(stream);
398 key()->PrintTo(stream);
399 stream->Add("] <- ");
400 value()->PrintTo(stream);
404 void LTransitionElementsKind::PrintDataTo(StringStream* stream) {
405 object()->PrintTo(stream);
406 stream->Add(" %p -> %p", *original_map(), *transitioned_map());
410 LChunk::LChunk(CompilationInfo* info, HGraph* graph)
411 : spill_slot_count_(0),
416 inlined_closures_(1) {
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_++;
427 LOperand* LChunk::GetNextSpillSlot(bool is_double) {
428 int index = GetNextSpillIndex(is_double);
430 return LDoubleStackSlot::Create(index);
432 return LStackSlot::Create(index);
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);
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);
455 LGap* gap = LGap::cast(cur);
456 if (!gap->IsRedundant()) {
457 can_eliminate = false;
460 can_eliminate = false;
465 label->set_replacement(GetLabel(goto_instr->block_id()));
473 void LChunk::AddInstruction(LInstruction* instr, HBasicBlock* block) {
474 LInstructionGap* gap = new(graph_->zone()) LInstructionGap(block);
476 if (instr->IsControl()) {
477 instructions_.Add(gap);
478 index = instructions_.length();
479 instructions_.Add(instr);
481 index = instructions_.length();
482 instructions_.Add(instr);
483 instructions_.Add(gap);
485 if (instr->HasPointerMap()) {
486 pointer_maps_.Add(instr->pointer_map());
487 instr->pointer_map()->set_lithium_position(index);
492 LConstantOperand* LChunk::DefineConstantOperand(HConstant* constant) {
493 return LConstantOperand::Create(constant->id());
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
502 int result = index - info()->scope()->num_parameters() - 1;
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) *
515 LGap* LChunk::GetGapAt(int index) const {
516 return LGap::cast(instructions_[index]);
520 bool LChunk::IsGapAt(int index) const {
521 return instructions_[index]->IsGap();
525 int LChunk::NearestGapPos(int index) const {
526 while (!IsGapAt(index)) index--;
531 void LChunk::AddGapMove(int index, LOperand* from, LOperand* to) {
532 GetGapAt(index)->GetOrCreateParallelMove(LGap::START)->AddMove(from, to);
536 Handle<Object> LChunk::LookupLiteral(LConstantOperand* operand) const {
537 return HConstant::cast(graph_->LookupValue(operand->index()))->handle();
541 Representation LChunk::LookupLiteralRepresentation(
542 LConstantOperand* operand) const {
543 return graph_->LookupValue(operand->index())->representation();
547 LChunk* LChunkBuilder::Build() {
549 chunk_ = new(zone()) LChunk(info(), graph());
550 HPhase phase("L_Building chunk", chunk_);
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;
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);
570 va_start(arguments, format);
571 OS::VPrint(format, arguments);
579 LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
580 return new(zone()) LUnallocated(LUnallocated::FIXED_REGISTER,
581 Register::ToAllocationIndex(reg));
585 LUnallocated* LChunkBuilder::ToUnallocated(DoubleRegister reg) {
586 return new(zone()) LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
587 DoubleRegister::ToAllocationIndex(reg));
591 LOperand* LChunkBuilder::UseFixed(HValue* value, Register fixed_register) {
592 return Use(value, ToUnallocated(fixed_register));
596 LOperand* LChunkBuilder::UseFixedDouble(HValue* value, DoubleRegister reg) {
597 return Use(value, ToUnallocated(reg));
601 LOperand* LChunkBuilder::UseRegister(HValue* value) {
602 return Use(value, new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
606 LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
608 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
609 LUnallocated::USED_AT_START));
613 LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
614 return Use(value, new(zone()) LUnallocated(LUnallocated::WRITABLE_REGISTER));
618 LOperand* LChunkBuilder::Use(HValue* value) {
619 return Use(value, new(zone()) LUnallocated(LUnallocated::NONE));
623 LOperand* LChunkBuilder::UseAtStart(HValue* value) {
624 return Use(value, new(zone()) LUnallocated(LUnallocated::NONE,
625 LUnallocated::USED_AT_START));
629 LOperand* LChunkBuilder::UseOrConstant(HValue* value) {
630 return value->IsConstant()
631 ? chunk_->DefineConstantOperand(HConstant::cast(value))
636 LOperand* LChunkBuilder::UseOrConstantAtStart(HValue* value) {
637 return value->IsConstant()
638 ? chunk_->DefineConstantOperand(HConstant::cast(value))
643 LOperand* LChunkBuilder::UseRegisterOrConstant(HValue* value) {
644 return value->IsConstant()
645 ? chunk_->DefineConstantOperand(HConstant::cast(value))
646 : UseRegister(value);
650 LOperand* LChunkBuilder::UseRegisterOrConstantAtStart(HValue* value) {
651 return value->IsConstant()
652 ? chunk_->DefineConstantOperand(HConstant::cast(value))
653 : UseRegisterAtStart(value);
657 LOperand* LChunkBuilder::UseAny(HValue* value) {
658 return value->IsConstant()
659 ? chunk_->DefineConstantOperand(HConstant::cast(value))
660 : Use(value, new(zone()) LUnallocated(LUnallocated::ANY));
664 LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
665 if (value->EmitAtUses()) {
666 HInstruction* instr = HInstruction::cast(value);
667 VisitInstruction(instr);
669 operand->set_virtual_register(value->id());
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);
683 template<int I, int T>
684 LInstruction* LChunkBuilder::DefineAsRegister(
685 LTemplateInstruction<1, I, T>* instr) {
687 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
691 template<int I, int T>
692 LInstruction* LChunkBuilder::DefineAsSpilled(
693 LTemplateInstruction<1, I, T>* instr, int index) {
695 new(zone()) LUnallocated(LUnallocated::FIXED_SLOT, index));
699 template<int I, int T>
700 LInstruction* LChunkBuilder::DefineSameAsFirst(
701 LTemplateInstruction<1, I, T>* instr) {
703 new(zone()) LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
707 template<int I, int T>
708 LInstruction* LChunkBuilder::DefineFixed(
709 LTemplateInstruction<1, I, T>* instr, Register reg) {
710 return Define(instr, ToUnallocated(reg));
714 template<int I, int T>
715 LInstruction* LChunkBuilder::DefineFixedDouble(
716 LTemplateInstruction<1, I, T>* instr, DoubleRegister reg) {
717 return Define(instr, ToUnallocated(reg));
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));
730 LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
731 HInstruction* hinstr,
732 CanDeoptimize can_deoptimize) {
737 instr = AssignPointerMap(instr);
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();
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);
763 LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
764 ASSERT(!instr->HasPointerMap());
765 instr->set_pointer_map(new(zone()) LPointerMap(position_));
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.");
779 LOperand* LChunkBuilder::FixedTemp(Register reg) {
780 LUnallocated* operand = ToUnallocated(reg);
781 ASSERT(operand->HasFixedPolicy());
786 LOperand* LChunkBuilder::FixedTemp(DoubleRegister reg) {
787 LUnallocated* operand = ToUnallocated(reg);
788 ASSERT(operand->HasFixedPolicy());
793 LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
794 return new(zone()) LLabel(instr->block());
798 LInstruction* LChunkBuilder::DoSoftDeoptimize(HSoftDeoptimize* instr) {
799 return AssignEnvironment(new(zone()) LDeoptimize);
803 LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
804 return AssignEnvironment(new(zone()) LDeoptimize);
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());
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);
820 ASSERT(instr->representation().IsInteger32());
821 ASSERT(instr->left()->representation().IsInteger32());
822 ASSERT(instr->right()->representation().IsInteger32());
823 LOperand* left = UseRegisterAtStart(instr->left());
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;
833 right = UseRegisterAtStart(right_value);
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;
841 for (HUseIterator it(instr->uses()); !it.Done(); it.Advance()) {
842 if (!it.value()->CheckFlag(HValue::kTruncatingToInt32)) {
849 LInstruction* result =
850 DefineAsRegister(new(zone()) LShiftI(op, left, right, does_deopt));
851 return does_deopt ? AssignEnvironment(result) : result;
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);
868 LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
869 HArithmeticBinaryOperation* instr) {
870 ASSERT(op == Token::ADD ||
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);
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());
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);
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();
910 block->UpdateEnvironment(last_environment);
911 ASSERT(pred->argument_count() >= 0);
912 argument_count_ = pred->argument_count();
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);
922 for (int i = 0; i < block->deleted_phis()->length(); ++i) {
923 last_environment->SetValueAt(block->deleted_phis()->at(i),
924 graph_->GetConstantUndefined());
926 block->UpdateEnvironment(last_environment);
927 // Pick up the outgoing argument count of one of the predecessors.
928 argument_count_ = pred->argument_count();
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);
937 current = current->next();
939 int end = chunk_->instructions()->length() - 1;
941 block->set_first_instruction_index(start);
942 block->set_last_instruction_index(end);
944 block->set_argument_count(argument_count_);
946 current_block_ = NULL;
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);
957 if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
958 instr = AssignPointerMap(instr);
960 if (FLAG_stress_environments && !instr->HasEnvironment()) {
961 instr = AssignEnvironment(instr);
963 instr->set_hydrogen_value(current);
964 chunk_->AddInstruction(instr, current_block_);
966 current_instruction_ = old_current;
970 LEnvironment* LChunkBuilder::CreateEnvironment(
971 HEnvironment* hydrogen_env,
972 int* argument_index_accumulator) {
973 if (hydrogen_env == NULL) return NULL;
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(),
985 hydrogen_env->parameter_count(),
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;
993 HValue* value = hydrogen_env->values()->at(i);
995 if (value->IsArgumentsObject()) {
997 } else if (value->IsPushArgument()) {
998 op = new(zone()) LArgument(argument_index++);
1002 result->AddValue(op, value->representation());
1005 if (hydrogen_env->frame_type() == JS_FUNCTION) {
1006 *argument_index_accumulator = argument_index;
1013 LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
1014 return new(zone()) LGoto(instr->FirstSuccessor()->block_id());
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());
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);
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);
1047 LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* length) {
1048 return DefineAsRegister(
1049 new(zone()) LArgumentsLength(UseRegister(length->value())));
1053 LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
1054 return DefineAsRegister(new(zone()) LArgumentsElements);
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);
1066 LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
1067 HInstanceOfKnownGlobal* instr) {
1068 LInstanceOfKnownGlobal* result =
1069 new(zone()) LInstanceOfKnownGlobal(UseFixed(instr->left(), a0),
1071 return MarkAsCall(DefineFixed(result, v0), instr);
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));
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,
1092 return MarkAsCall(DefineFixed(result, v0), instr, CAN_DEOPTIMIZE_EAGERLY);
1096 LInstruction* LChunkBuilder::DoPushArgument(HPushArgument* instr) {
1098 LOperand* argument = Use(instr->argument());
1099 return new(zone()) LPushArgument(argument);
1103 LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1104 return instr->HasNoUses()
1106 : DefineAsRegister(new(zone()) LThisFunction);
1110 LInstruction* LChunkBuilder::DoContext(HContext* instr) {
1111 return instr->HasNoUses() ? NULL : DefineAsRegister(new(zone()) LContext);
1115 LInstruction* LChunkBuilder::DoOuterContext(HOuterContext* instr) {
1116 LOperand* context = UseRegisterAtStart(instr->value());
1117 return DefineAsRegister(new(zone()) LOuterContext(context));
1121 LInstruction* LChunkBuilder::DoDeclareGlobals(HDeclareGlobals* instr) {
1122 return MarkAsCall(new(zone()) LDeclareGlobals, instr);
1126 LInstruction* LChunkBuilder::DoGlobalObject(HGlobalObject* instr) {
1127 LOperand* context = UseRegisterAtStart(instr->value());
1128 return DefineAsRegister(new(zone()) LGlobalObject(context, instr->qml_global()));
1132 LInstruction* LChunkBuilder::DoGlobalReceiver(HGlobalReceiver* instr) {
1133 LOperand* global_object = UseRegisterAtStart(instr->value());
1134 return DefineAsRegister(new(zone()) LGlobalReceiver(global_object));
1138 LInstruction* LChunkBuilder::DoCallConstantFunction(
1139 HCallConstantFunction* instr) {
1140 argument_count_ -= instr->argument_count();
1141 return MarkAsCall(DefineFixed(new(zone()) LCallConstantFunction, v0), instr);
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);
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);
1167 LOperand* input = UseRegisterAtStart(instr->value());
1168 LOperand* temp = (op == kMathFloor) ? TempRegister() : NULL;
1169 LUnaryMathOperation* result = new(zone()) LUnaryMathOperation(input, temp);
1172 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1174 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1176 return DefineAsRegister(result);
1178 return AssignEnvironment(DefineAsRegister(result));
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);
1195 LInstruction* LChunkBuilder::DoCallNamed(HCallNamed* instr) {
1196 argument_count_ -= instr->argument_count();
1197 return MarkAsCall(DefineFixed(new(zone()) LCallNamed, v0), instr);
1201 LInstruction* LChunkBuilder::DoCallGlobal(HCallGlobal* instr) {
1202 argument_count_ -= instr->argument_count();
1203 return MarkAsCall(DefineFixed(new(zone()) LCallGlobal(instr->qml_global()), v0), instr);
1207 LInstruction* LChunkBuilder::DoCallKnownGlobal(HCallKnownGlobal* instr) {
1208 argument_count_ -= instr->argument_count();
1209 return MarkAsCall(DefineFixed(new(zone()) LCallKnownGlobal, v0), instr);
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);
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),
1229 LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1230 argument_count_ -= instr->argument_count();
1231 return MarkAsCall(DefineFixed(new(zone()) LCallRuntime, v0), instr);
1235 LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1236 return DoShift(Token::SHR, instr);
1240 LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1241 return DoShift(Token::SAR, instr);
1245 LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1246 return DoShift(Token::SHL, instr);
1250 LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
1251 if (instr->representation().IsInteger32()) {
1252 ASSERT(instr->left()->representation().IsInteger32());
1253 ASSERT(instr->right()->representation().IsInteger32());
1255 LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1256 LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1257 return DefineAsRegister(new(zone()) LBitI(left, right));
1259 ASSERT(instr->representation().IsTagged());
1260 ASSERT(instr->left()->representation().IsTagged());
1261 ASSERT(instr->right()->representation().IsTagged());
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);
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));
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)));
1294 return DoArithmeticT(Token::DIV, instr);
1299 LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
1305 LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1306 if (instr->representation().IsInteger32()) {
1307 ASSERT(instr->left()->representation().IsInteger32());
1308 ASSERT(instr->right()->representation().IsInteger32());
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()));
1316 LOperand* dividend = UseRegister(instr->left());
1317 LOperand* divisor = UseRegister(instr->right());
1318 mod = new(zone()) LModI(dividend,
1325 if (instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1326 instr->CheckFlag(HValue::kCanBeDivByZero)) {
1327 return AssignEnvironment(DefineAsRegister(mod));
1329 return DefineAsRegister(mod);
1331 } else if (instr->representation().IsTagged()) {
1332 return DoArithmeticT(Token::MOD, instr);
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);
1346 LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1347 if (instr->representation().IsInteger32()) {
1348 ASSERT(instr->left()->representation().IsInteger32());
1349 ASSERT(instr->right()->representation().IsInteger32());
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();
1359 left = UseRegisterAtStart(instr->LeastConstantOperand());
1361 LMulI* mul = new(zone()) LMulI(left, right, temp);
1362 if (instr->CheckFlag(HValue::kCanOverflow) ||
1363 instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1364 AssignEnvironment(mul);
1366 return DefineAsRegister(mul);
1368 } else if (instr->representation().IsDouble()) {
1369 return DoArithmeticD(Token::MUL, instr);
1372 return DoArithmeticT(Token::MUL, instr);
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);
1389 } else if (instr->representation().IsDouble()) {
1390 return DoArithmeticD(Token::SUB, instr);
1392 return DoArithmeticT(Token::SUB, instr);
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);
1409 } else if (instr->representation().IsDouble()) {
1410 return DoArithmeticD(Token::ADD, instr);
1412 ASSERT(instr->representation().IsTagged());
1413 return DoArithmeticT(Token::ADD, instr);
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),
1431 CAN_DEOPTIMIZE_EAGERLY);
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);
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);
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);
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);
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);
1482 LInstruction* LChunkBuilder::DoCompareConstantEqAndBranch(
1483 HCompareConstantEqAndBranch* instr) {
1484 return new(zone()) LCmpConstantEqAndBranch(
1485 UseRegisterAtStart(instr->value()));
1489 LInstruction* LChunkBuilder::DoIsNilAndBranch(HIsNilAndBranch* instr) {
1490 ASSERT(instr->value()->representation().IsTagged());
1491 return new(zone()) LIsNilAndBranch(UseRegisterAtStart(instr->value()));
1495 LInstruction* LChunkBuilder::DoIsObjectAndBranch(HIsObjectAndBranch* instr) {
1496 ASSERT(instr->value()->representation().IsTagged());
1497 LOperand* temp = TempRegister();
1498 return new(zone()) LIsObjectAndBranch(UseRegisterAtStart(instr->value()),
1503 LInstruction* LChunkBuilder::DoIsStringAndBranch(HIsStringAndBranch* instr) {
1504 ASSERT(instr->value()->representation().IsTagged());
1505 LOperand* temp = TempRegister();
1506 return new(zone()) LIsStringAndBranch(UseRegisterAtStart(instr->value()),
1511 LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
1512 ASSERT(instr->value()->representation().IsTagged());
1513 return new(zone()) LIsSmiAndBranch(Use(instr->value()));
1517 LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
1518 HIsUndetectableAndBranch* instr) {
1519 ASSERT(instr->value()->representation().IsTagged());
1520 return new(zone()) LIsUndetectableAndBranch(
1521 UseRegisterAtStart(instr->value()), TempRegister());
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);
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);
1545 LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
1546 HGetCachedArrayIndex* instr) {
1547 ASSERT(instr->value()->representation().IsTagged());
1548 LOperand* value = UseRegisterAtStart(instr->value());
1550 return DefineAsRegister(new(zone()) LGetCachedArrayIndex(value));
1554 LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
1555 HHasCachedArrayIndexAndBranch* instr) {
1556 ASSERT(instr->value()->representation().IsTagged());
1557 return new(zone()) LHasCachedArrayIndexAndBranch(
1558 UseRegisterAtStart(instr->value()));
1562 LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
1563 HClassOfTestAndBranch* instr) {
1564 ASSERT(instr->value()->representation().IsTagged());
1565 return new(zone()) LClassOfTestAndBranch(UseRegister(instr->value()),
1570 LInstruction* LChunkBuilder::DoJSArrayLength(HJSArrayLength* instr) {
1571 LOperand* array = UseRegisterAtStart(instr->value());
1572 return DefineAsRegister(new(zone()) LJSArrayLength(array));
1576 LInstruction* LChunkBuilder::DoFixedArrayBaseLength(
1577 HFixedArrayBaseLength* instr) {
1578 LOperand* array = UseRegisterAtStart(instr->value());
1579 return DefineAsRegister(new(zone()) LFixedArrayBaseLength(array));
1583 LInstruction* LChunkBuilder::DoElementsKind(HElementsKind* instr) {
1584 LOperand* object = UseRegisterAtStart(instr->value());
1585 return DefineAsRegister(new(zone()) LElementsKind(object));
1589 LInstruction* LChunkBuilder::DoValueOf(HValueOf* instr) {
1590 LOperand* object = UseRegister(instr->value());
1591 LValueOf* result = new(zone()) LValueOf(object, TempRegister());
1592 return DefineAsRegister(result);
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);
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));
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.
1617 LInstruction* LChunkBuilder::DoThrow(HThrow* instr) {
1618 LOperand* value = UseFixed(instr->value(), a0);
1619 return MarkAsCall(new(zone()) LThrow(value), instr);
1623 LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
1628 LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
1629 // All HForceRepresentation instructions should be eliminated in the
1630 // representation change phase of Hydrogen.
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));
1645 ASSERT(to.IsInteger32());
1646 LOperand* value = UseRegisterAtStart(instr->value());
1647 bool needs_check = !instr->value()->type().IsSmi();
1648 LInstruction* res = NULL;
1650 res = DefineAsRegister(new(zone()) LSmiUntag(value, needs_check));
1652 LOperand* temp1 = TempRegister();
1653 LOperand* temp2 = instr->CanTruncateToInt32() ? TempRegister()
1655 LOperand* temp3 = instr->CanTruncateToInt32() ? FixedTemp(f22)
1657 res = DefineSameAsFirst(new(zone()) LTaggedToI(value,
1661 res = AssignEnvironment(res);
1665 } else if (from.IsDouble()) {
1666 if (to.IsTagged()) {
1667 LOperand* value = UseRegister(instr->value());
1668 LOperand* temp1 = TempRegister();
1669 LOperand* temp2 = TempRegister();
1671 // Make sure that the temp and result_temp registers are
1673 LUnallocated* result_temp = TempRegister();
1674 LNumberTagD* result = new(zone()) LNumberTagD(value, temp1, temp2);
1675 Define(result, result_temp);
1676 return AssignPointerMap(result);
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));
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));
1692 LNumberTagI* result = new(zone()) LNumberTagI(value);
1693 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1696 ASSERT(to.IsDouble());
1697 LOperand* value = Use(instr->value());
1698 return DefineAsRegister(new(zone()) LInteger32ToDouble(value));
1706 LInstruction* LChunkBuilder::DoCheckNonSmi(HCheckNonSmi* instr) {
1707 LOperand* value = UseRegisterAtStart(instr->value());
1708 return AssignEnvironment(new(zone()) LCheckNonSmi(value));
1712 LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
1713 LOperand* value = UseRegisterAtStart(instr->value());
1714 LInstruction* result = new(zone()) LCheckInstanceType(value);
1715 return AssignEnvironment(result);
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);
1727 LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
1728 LOperand* value = UseRegisterAtStart(instr->value());
1729 return AssignEnvironment(new(zone()) LCheckSmi(value));
1733 LInstruction* LChunkBuilder::DoCheckFunction(HCheckFunction* instr) {
1734 LOperand* value = UseRegisterAtStart(instr->value());
1735 return AssignEnvironment(new(zone()) LCheckFunction(value));
1739 LInstruction* LChunkBuilder::DoCheckMaps(HCheckMaps* instr) {
1740 LOperand* value = UseRegisterAtStart(instr->value());
1741 LInstruction* result = new(zone()) LCheckMaps(value);
1742 return AssignEnvironment(result);
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));
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));
1765 LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
1766 return new(zone()) LReturn(UseFixed(instr->value(), v0));
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);
1785 LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
1786 LLoadGlobalCell* result = new(zone()) LLoadGlobalCell;
1787 return instr->RequiresHoleCheck()
1788 ? AssignEnvironment(DefineAsRegister(result))
1789 : DefineAsRegister(result);
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);
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
1804 return instr->RequiresHoleCheck()
1805 ? AssignEnvironment(new(zone()) LStoreGlobalCell(value, TempRegister()))
1806 : new(zone()) LStoreGlobalCell(value, NULL);
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);
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;
1827 LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
1830 if (instr->NeedsWriteBarrier()) {
1831 context = UseTempRegister(instr->context());
1832 value = UseTempRegister(instr->value());
1834 context = UseRegister(instr->context());
1835 value = UseRegister(instr->value());
1837 LInstruction* result = new(zone()) LStoreContextSlot(context, value);
1838 return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
1842 LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
1843 return DefineAsRegister(
1844 new(zone()) LLoadNamedField(UseRegisterAtStart(instr->object())));
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);
1857 LOperand* obj = UseRegisterAtStart(instr->object());
1858 LLoadNamedFieldPolymorphic* result =
1859 new(zone()) LLoadNamedFieldPolymorphic(obj);
1860 return AssignEnvironment(DefineAsRegister(result));
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);
1872 LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
1873 HLoadFunctionPrototype* instr) {
1874 return AssignEnvironment(DefineAsRegister(
1875 new(zone()) LLoadFunctionPrototype(UseRegister(instr->function()))));
1879 LInstruction* LChunkBuilder::DoLoadElements(HLoadElements* instr) {
1880 LOperand* input = UseRegisterAtStart(instr->value());
1881 return DefineAsRegister(new(zone()) LLoadElements(input));
1885 LInstruction* LChunkBuilder::DoLoadExternalArrayPointer(
1886 HLoadExternalArrayPointer* instr) {
1887 LOperand* input = UseRegisterAtStart(instr->value());
1888 return DefineAsRegister(new(zone()) LLoadExternalArrayPointer(input));
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);
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));
1916 LInstruction* LChunkBuilder::DoLoadKeyedSpecializedArrayElement(
1917 HLoadKeyedSpecializedArrayElement* instr) {
1918 ElementsKind elements_kind = instr->elements_kind();
1919 Representation representation(instr->representation());
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;
1940 LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
1941 LOperand* object = UseFixed(instr->object(), a1);
1942 LOperand* key = UseFixed(instr->key(), a0);
1944 LInstruction* result =
1945 DefineFixed(new(zone()) LLoadKeyedGeneric(object, key), v0);
1946 return MarkAsCall(result, instr);
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());
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);
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());
1974 LOperand* elements = UseRegisterAtStart(instr->elements());
1975 LOperand* val = UseTempRegister(instr->value());
1976 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1978 return new(zone()) LStoreKeyedFastDoubleElement(elements, key, val);
1982 LInstruction* LChunkBuilder::DoStoreKeyedSpecializedArrayElement(
1983 HStoreKeyedSpecializedArrayElement* instr) {
1984 Representation representation(instr->value()->representation());
1985 ElementsKind elements_kind = instr->elements_kind();
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());
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());
2005 return new(zone()) LStoreKeyedSpecializedArrayElement(external_pointer,
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);
2016 ASSERT(instr->object()->representation().IsTagged());
2017 ASSERT(instr->key()->representation().IsTagged());
2018 ASSERT(instr->value()->representation().IsTagged());
2020 return MarkAsCall(new(zone()) LStoreKeyedGeneric(obj, key, val), instr);
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);
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,
2041 return MarkAsCall(DefineFixed(result, v0), instr);
2046 LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
2047 bool needs_write_barrier = instr->NeedsWriteBarrier();
2049 LOperand* obj = needs_write_barrier
2050 ? UseTempRegister(instr->object())
2051 : UseRegisterAtStart(instr->object());
2053 LOperand* val = needs_write_barrier
2054 ? UseTempRegister(instr->value())
2055 : UseRegister(instr->value());
2057 return new(zone()) LStoreNamedField(obj, val);
2061 LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
2062 LOperand* obj = UseFixed(instr->object(), a1);
2063 LOperand* val = UseFixed(instr->value(), a0);
2065 LInstruction* result = new(zone()) LStoreNamedGeneric(obj, val);
2066 return MarkAsCall(result, instr);
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),
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)));
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));
2093 LInstruction* LChunkBuilder::DoStringLength(HStringLength* instr) {
2094 LOperand* string = UseRegisterAtStart(instr->value());
2095 return DefineAsRegister(new(zone()) LStringLength(string));
2099 LInstruction* LChunkBuilder::DoAllocateObject(HAllocateObject* instr) {
2100 LAllocateObject* result = new(zone()) LAllocateObject(
2101 TempRegister(), TempRegister());
2102 return AssignPointerMap(DefineAsRegister(result));
2106 LInstruction* LChunkBuilder::DoFastLiteral(HFastLiteral* instr) {
2107 return MarkAsCall(DefineFixed(new(zone()) LFastLiteral, v0), instr);
2111 LInstruction* LChunkBuilder::DoArrayLiteral(HArrayLiteral* instr) {
2112 return MarkAsCall(DefineFixed(new(zone()) LArrayLiteral, v0), instr);
2116 LInstruction* LChunkBuilder::DoObjectLiteral(HObjectLiteral* instr) {
2117 return MarkAsCall(DefineFixed(new(zone()) LObjectLiteral, v0), instr);
2121 LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2122 return MarkAsCall(DefineFixed(new(zone()) LRegExpLiteral, v0), instr);
2126 LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2127 return MarkAsCall(DefineFixed(new(zone()) LFunctionLiteral, v0), instr);
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);
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);
2146 LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2147 int spill_index = chunk()->GetParameterStackSlot(instr->index());
2148 return DefineAsSpilled(new(zone()) LParameter, spill_index);
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");
2158 return DefineAsSpilled(new(zone()) LUnknownOSRValue, spill_index);
2162 LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2163 argument_count_ -= instr->argument_count();
2164 return MarkAsCall(DefineFixed(new(zone()) LCallStub, v0), instr);
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.
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));
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);
2194 LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2195 LTypeof* result = new(zone()) LTypeof(UseFixed(instr->value(), a0));
2196 return MarkAsCall(DefineFixed(result, v0), instr);
2200 LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2201 return new(zone()) LTypeofIsAndBranch(UseTempRegister(instr->value()));
2205 LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
2206 HIsConstructCallAndBranch* instr) {
2207 return new(zone()) LIsConstructCallAndBranch(TempRegister());
2211 LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2212 HEnvironment* env = current_block_->last_environment();
2213 ASSERT(env != NULL);
2215 env->set_ast_id(instr->ast_id());
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);
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;
2245 LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2246 if (instr->is_function_entry()) {
2247 return MarkAsCall(new(zone()) LStackCheck, instr);
2249 ASSERT(instr->is_backwards_branch());
2250 return AssignEnvironment(AssignPointerMap(new(zone()) LStackCheck));
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(),
2263 instr->is_construct());
2264 if (instr->arguments_var() != NULL) {
2265 inner->Bind(instr->arguments_var(), graph()->GetArgumentsObject());
2267 current_block_->UpdateEnvironment(inner);
2268 chunk_->AddInlinedClosure(instr->closure());
2273 LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2274 LInstruction* pop = NULL;
2276 HEnvironment* env = current_block_->last_environment();
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;
2284 HEnvironment* outer = current_block_->last_environment()->
2285 DiscardInlined(false);
2286 current_block_->UpdateEnvironment(outer);
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);
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);
2307 LInstruction* LChunkBuilder::DoForInCacheArray(HForInCacheArray* instr) {
2308 LOperand* map = UseRegister(instr->map());
2309 return AssignEnvironment(DefineAsRegister(
2310 new(zone()) LForInCacheArray(map)));
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));
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));
2328 } } // namespace v8::internal