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 #if defined(V8_TARGET_ARCH_X64)
32 #include "lithium-allocator-inl.h"
33 #include "x64/lithium-x64.h"
34 #include "x64/lithium-codegen-x64.h"
39 #define DEFINE_COMPILE(type) \
40 void L##type::CompileToNative(LCodeGen* generator) { \
41 generator->Do##type(this); \
43 LITHIUM_CONCRETE_INSTRUCTION_LIST(DEFINE_COMPILE)
46 LOsrEntry::LOsrEntry() {
47 for (int i = 0; i < Register::kNumAllocatableRegisters; ++i) {
48 register_spills_[i] = NULL;
50 for (int i = 0; i < DoubleRegister::kNumAllocatableRegisters; ++i) {
51 double_register_spills_[i] = NULL;
56 void LOsrEntry::MarkSpilledRegister(int allocation_index,
57 LOperand* spill_operand) {
58 ASSERT(spill_operand->IsStackSlot());
59 ASSERT(register_spills_[allocation_index] == NULL);
60 register_spills_[allocation_index] = spill_operand;
64 void LOsrEntry::MarkSpilledDoubleRegister(int allocation_index,
65 LOperand* spill_operand) {
66 ASSERT(spill_operand->IsDoubleStackSlot());
67 ASSERT(double_register_spills_[allocation_index] == NULL);
68 double_register_spills_[allocation_index] = spill_operand;
73 void LInstruction::VerifyCall() {
74 // Call instructions can use only fixed registers as temporaries and
75 // outputs because all registers are blocked by the calling convention.
76 // Inputs operands must use a fixed register or use-at-start policy or
77 // a non-register policy.
78 ASSERT(Output() == NULL ||
79 LUnallocated::cast(Output())->HasFixedPolicy() ||
80 !LUnallocated::cast(Output())->HasRegisterPolicy());
81 for (UseIterator it(this); !it.Done(); it.Advance()) {
82 LUnallocated* operand = LUnallocated::cast(it.Current());
83 ASSERT(operand->HasFixedPolicy() ||
84 operand->IsUsedAtStart());
86 for (TempIterator it(this); !it.Done(); it.Advance()) {
87 LUnallocated* operand = LUnallocated::cast(it.Current());
88 ASSERT(operand->HasFixedPolicy() ||!operand->HasRegisterPolicy());
94 void LInstruction::PrintTo(StringStream* stream) {
95 stream->Add("%s ", this->Mnemonic());
97 PrintOutputOperandTo(stream);
101 if (HasEnvironment()) {
103 environment()->PrintTo(stream);
106 if (HasPointerMap()) {
108 pointer_map()->PrintTo(stream);
113 void LInstruction::PrintDataTo(StringStream* stream) {
115 for (int i = 0; i < InputCount(); i++) {
116 if (i > 0) stream->Add(" ");
117 InputAt(i)->PrintTo(stream);
122 void LInstruction::PrintOutputOperandTo(StringStream* stream) {
123 if (HasResult()) result()->PrintTo(stream);
127 void LLabel::PrintDataTo(StringStream* stream) {
128 LGap::PrintDataTo(stream);
129 LLabel* rep = replacement();
131 stream->Add(" Dead block replaced with B%d", rep->block_id());
136 bool LGap::IsRedundant() const {
137 for (int i = 0; i < 4; i++) {
138 if (parallel_moves_[i] != NULL && !parallel_moves_[i]->IsRedundant()) {
147 void LGap::PrintDataTo(StringStream* stream) {
148 for (int i = 0; i < 4; i++) {
150 if (parallel_moves_[i] != NULL) {
151 parallel_moves_[i]->PrintDataTo(stream);
158 const char* LArithmeticD::Mnemonic() const {
160 case Token::ADD: return "add-d";
161 case Token::SUB: return "sub-d";
162 case Token::MUL: return "mul-d";
163 case Token::DIV: return "div-d";
164 case Token::MOD: return "mod-d";
172 const char* LArithmeticT::Mnemonic() const {
174 case Token::ADD: return "add-t";
175 case Token::SUB: return "sub-t";
176 case Token::MUL: return "mul-t";
177 case Token::MOD: return "mod-t";
178 case Token::DIV: return "div-t";
179 case Token::BIT_AND: return "bit-and-t";
180 case Token::BIT_OR: return "bit-or-t";
181 case Token::BIT_XOR: return "bit-xor-t";
182 case Token::SHL: return "sal-t";
183 case Token::SAR: return "sar-t";
184 case Token::SHR: return "shr-t";
192 void LGoto::PrintDataTo(StringStream* stream) {
193 stream->Add("B%d", block_id());
197 void LBranch::PrintDataTo(StringStream* stream) {
198 stream->Add("B%d | B%d on ", true_block_id(), false_block_id());
199 InputAt(0)->PrintTo(stream);
203 void LCmpIDAndBranch::PrintDataTo(StringStream* stream) {
205 InputAt(0)->PrintTo(stream);
206 stream->Add(" %s ", Token::String(op()));
207 InputAt(1)->PrintTo(stream);
208 stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
212 void LIsNilAndBranch::PrintDataTo(StringStream* stream) {
214 InputAt(0)->PrintTo(stream);
215 stream->Add(kind() == kStrictEquality ? " === " : " == ");
216 stream->Add(nil() == kNullValue ? "null" : "undefined");
217 stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
221 void LIsObjectAndBranch::PrintDataTo(StringStream* stream) {
222 stream->Add("if is_object(");
223 InputAt(0)->PrintTo(stream);
224 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
228 void LIsStringAndBranch::PrintDataTo(StringStream* stream) {
229 stream->Add("if is_string(");
230 InputAt(0)->PrintTo(stream);
231 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
235 void LIsSmiAndBranch::PrintDataTo(StringStream* stream) {
236 stream->Add("if is_smi(");
237 InputAt(0)->PrintTo(stream);
238 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
242 void LIsUndetectableAndBranch::PrintDataTo(StringStream* stream) {
243 stream->Add("if is_undetectable(");
244 InputAt(0)->PrintTo(stream);
245 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
249 void LStringCompareAndBranch::PrintDataTo(StringStream* stream) {
250 stream->Add("if string_compare(");
251 InputAt(0)->PrintTo(stream);
252 InputAt(1)->PrintTo(stream);
253 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
257 void LHasInstanceTypeAndBranch::PrintDataTo(StringStream* stream) {
258 stream->Add("if has_instance_type(");
259 InputAt(0)->PrintTo(stream);
260 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
264 void LHasCachedArrayIndexAndBranch::PrintDataTo(StringStream* stream) {
265 stream->Add("if has_cached_array_index(");
266 InputAt(0)->PrintTo(stream);
267 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
271 void LClassOfTestAndBranch::PrintDataTo(StringStream* stream) {
272 stream->Add("if class_of_test(");
273 InputAt(0)->PrintTo(stream);
274 stream->Add(", \"%o\") then B%d else B%d",
275 *hydrogen()->class_name(),
281 void LTypeofIsAndBranch::PrintDataTo(StringStream* stream) {
282 stream->Add("if typeof ");
283 InputAt(0)->PrintTo(stream);
284 stream->Add(" == \"%s\" then B%d else B%d",
285 *hydrogen()->type_literal()->ToCString(),
286 true_block_id(), false_block_id());
290 void LCallConstantFunction::PrintDataTo(StringStream* stream) {
291 stream->Add("#%d / ", arity());
295 void LUnaryMathOperation::PrintDataTo(StringStream* stream) {
296 stream->Add("/%s ", hydrogen()->OpName());
297 InputAt(0)->PrintTo(stream);
301 void LLoadContextSlot::PrintDataTo(StringStream* stream) {
302 InputAt(0)->PrintTo(stream);
303 stream->Add("[%d]", slot_index());
307 void LStoreContextSlot::PrintDataTo(StringStream* stream) {
308 InputAt(0)->PrintTo(stream);
309 stream->Add("[%d] <- ", slot_index());
310 InputAt(1)->PrintTo(stream);
314 void LInvokeFunction::PrintDataTo(StringStream* stream) {
316 InputAt(0)->PrintTo(stream);
317 stream->Add(" #%d / ", arity());
321 void LCallKeyed::PrintDataTo(StringStream* stream) {
322 stream->Add("[rcx] #%d / ", arity());
326 void LCallNamed::PrintDataTo(StringStream* stream) {
327 SmartArrayPointer<char> name_string = name()->ToCString();
328 stream->Add("%s #%d / ", *name_string, arity());
332 void LCallGlobal::PrintDataTo(StringStream* stream) {
333 SmartArrayPointer<char> name_string = name()->ToCString();
334 stream->Add("%s #%d / ", *name_string, arity());
338 void LCallKnownGlobal::PrintDataTo(StringStream* stream) {
339 stream->Add("#%d / ", arity());
343 void LCallNew::PrintDataTo(StringStream* stream) {
345 InputAt(0)->PrintTo(stream);
346 stream->Add(" #%d / ", arity());
350 void LAccessArgumentsAt::PrintDataTo(StringStream* stream) {
351 arguments()->PrintTo(stream);
353 stream->Add(" length ");
354 length()->PrintTo(stream);
356 stream->Add(" index ");
357 index()->PrintTo(stream);
361 int LChunk::GetNextSpillIndex(bool is_double) {
362 return spill_slot_count_++;
366 LOperand* LChunk::GetNextSpillSlot(bool is_double) {
367 // All stack slots are Double stack slots on x64.
368 // Alternatively, at some point, start using half-size
369 // stack slots for int32 values.
370 int index = GetNextSpillIndex(is_double);
372 return LDoubleStackSlot::Create(index);
374 return LStackSlot::Create(index);
379 void LChunk::MarkEmptyBlocks() {
380 HPhase phase("L_Mark empty blocks", this);
381 for (int i = 0; i < graph()->blocks()->length(); ++i) {
382 HBasicBlock* block = graph()->blocks()->at(i);
383 int first = block->first_instruction_index();
384 int last = block->last_instruction_index();
385 LInstruction* first_instr = instructions()->at(first);
386 LInstruction* last_instr = instructions()->at(last);
388 LLabel* label = LLabel::cast(first_instr);
389 if (last_instr->IsGoto()) {
390 LGoto* goto_instr = LGoto::cast(last_instr);
391 if (label->IsRedundant() &&
392 !label->is_loop_header()) {
393 bool can_eliminate = true;
394 for (int i = first + 1; i < last && can_eliminate; ++i) {
395 LInstruction* cur = instructions()->at(i);
397 LGap* gap = LGap::cast(cur);
398 if (!gap->IsRedundant()) {
399 can_eliminate = false;
402 can_eliminate = false;
407 label->set_replacement(GetLabel(goto_instr->block_id()));
415 void LStoreNamedField::PrintDataTo(StringStream* stream) {
416 object()->PrintTo(stream);
418 stream->Add(*String::cast(*name())->ToCString());
420 value()->PrintTo(stream);
424 void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
425 object()->PrintTo(stream);
427 stream->Add(*String::cast(*name())->ToCString());
429 value()->PrintTo(stream);
433 void LStoreKeyedFastElement::PrintDataTo(StringStream* stream) {
434 object()->PrintTo(stream);
436 key()->PrintTo(stream);
437 stream->Add("] <- ");
438 value()->PrintTo(stream);
442 void LStoreKeyedFastDoubleElement::PrintDataTo(StringStream* stream) {
443 elements()->PrintTo(stream);
445 key()->PrintTo(stream);
446 stream->Add("] <- ");
447 value()->PrintTo(stream);
451 void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
452 object()->PrintTo(stream);
454 key()->PrintTo(stream);
455 stream->Add("] <- ");
456 value()->PrintTo(stream);
460 void LTransitionElementsKind::PrintDataTo(StringStream* stream) {
461 object()->PrintTo(stream);
462 stream->Add(" %p -> %p", *original_map(), *transitioned_map());
466 void LChunk::AddInstruction(LInstruction* instr, HBasicBlock* block) {
467 LInstructionGap* gap = new(graph_->zone()) LInstructionGap(block);
469 if (instr->IsControl()) {
470 instructions_.Add(gap);
471 index = instructions_.length();
472 instructions_.Add(instr);
474 index = instructions_.length();
475 instructions_.Add(instr);
476 instructions_.Add(gap);
478 if (instr->HasPointerMap()) {
479 pointer_maps_.Add(instr->pointer_map());
480 instr->pointer_map()->set_lithium_position(index);
485 LConstantOperand* LChunk::DefineConstantOperand(HConstant* constant) {
486 return LConstantOperand::Create(constant->id());
490 int LChunk::GetParameterStackSlot(int index) const {
491 // The receiver is at index 0, the first parameter at index 1, so we
492 // shift all parameter indexes down by the number of parameters, and
493 // make sure they end up negative so they are distinguishable from
495 int result = index - info()->scope()->num_parameters() - 1;
500 // A parameter relative to ebp in the arguments stub.
501 int LChunk::ParameterAt(int index) {
502 ASSERT(-1 <= index); // -1 is the receiver.
503 return (1 + info()->scope()->num_parameters() - index) *
508 LGap* LChunk::GetGapAt(int index) const {
509 return LGap::cast(instructions_[index]);
513 bool LChunk::IsGapAt(int index) const {
514 return instructions_[index]->IsGap();
518 int LChunk::NearestGapPos(int index) const {
519 while (!IsGapAt(index)) index--;
524 void LChunk::AddGapMove(int index, LOperand* from, LOperand* to) {
525 GetGapAt(index)->GetOrCreateParallelMove(LGap::START)->AddMove(from, to);
529 Handle<Object> LChunk::LookupLiteral(LConstantOperand* operand) const {
530 return HConstant::cast(graph_->LookupValue(operand->index()))->handle();
534 Representation LChunk::LookupLiteralRepresentation(
535 LConstantOperand* operand) const {
536 return graph_->LookupValue(operand->index())->representation();
540 LChunk* LChunkBuilder::Build() {
542 chunk_ = new(zone()) LChunk(info(), graph());
543 HPhase phase("L_Building chunk", chunk_);
545 const ZoneList<HBasicBlock*>* blocks = graph()->blocks();
546 for (int i = 0; i < blocks->length(); i++) {
547 HBasicBlock* next = NULL;
548 if (i < blocks->length() - 1) next = blocks->at(i + 1);
549 DoBasicBlock(blocks->at(i), next);
550 if (is_aborted()) return NULL;
557 void LChunkBuilder::Abort(const char* format, ...) {
558 if (FLAG_trace_bailout) {
559 SmartArrayPointer<char> name(
560 info()->shared_info()->DebugName()->ToCString());
561 PrintF("Aborting LChunk building in @\"%s\": ", *name);
563 va_start(arguments, format);
564 OS::VPrint(format, arguments);
572 LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
573 return new(zone()) LUnallocated(LUnallocated::FIXED_REGISTER,
574 Register::ToAllocationIndex(reg));
578 LUnallocated* LChunkBuilder::ToUnallocated(XMMRegister reg) {
579 return new(zone()) LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
580 XMMRegister::ToAllocationIndex(reg));
584 LOperand* LChunkBuilder::UseFixed(HValue* value, Register fixed_register) {
585 return Use(value, ToUnallocated(fixed_register));
589 LOperand* LChunkBuilder::UseFixedDouble(HValue* value, XMMRegister reg) {
590 return Use(value, ToUnallocated(reg));
594 LOperand* LChunkBuilder::UseRegister(HValue* value) {
595 return Use(value, new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
599 LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
601 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
602 LUnallocated::USED_AT_START));
606 LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
607 return Use(value, new(zone()) LUnallocated(LUnallocated::WRITABLE_REGISTER));
611 LOperand* LChunkBuilder::Use(HValue* value) {
612 return Use(value, new(zone()) LUnallocated(LUnallocated::NONE));
616 LOperand* LChunkBuilder::UseAtStart(HValue* value) {
617 return Use(value, new(zone()) LUnallocated(LUnallocated::NONE,
618 LUnallocated::USED_AT_START));
622 LOperand* LChunkBuilder::UseOrConstant(HValue* value) {
623 return value->IsConstant()
624 ? chunk_->DefineConstantOperand(HConstant::cast(value))
629 LOperand* LChunkBuilder::UseOrConstantAtStart(HValue* value) {
630 return value->IsConstant()
631 ? chunk_->DefineConstantOperand(HConstant::cast(value))
636 LOperand* LChunkBuilder::UseRegisterOrConstant(HValue* value) {
637 return value->IsConstant()
638 ? chunk_->DefineConstantOperand(HConstant::cast(value))
639 : UseRegister(value);
643 LOperand* LChunkBuilder::UseRegisterOrConstantAtStart(HValue* value) {
644 return value->IsConstant()
645 ? chunk_->DefineConstantOperand(HConstant::cast(value))
646 : UseRegisterAtStart(value);
650 LOperand* LChunkBuilder::UseAny(HValue* value) {
651 return value->IsConstant()
652 ? chunk_->DefineConstantOperand(HConstant::cast(value))
653 : Use(value, new(zone()) LUnallocated(LUnallocated::ANY));
657 LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
658 if (value->EmitAtUses()) {
659 HInstruction* instr = HInstruction::cast(value);
660 VisitInstruction(instr);
662 operand->set_virtual_register(value->id());
667 template<int I, int T>
668 LInstruction* LChunkBuilder::Define(LTemplateInstruction<1, I, T>* instr,
669 LUnallocated* result) {
670 result->set_virtual_register(current_instruction_->id());
671 instr->set_result(result);
676 template<int I, int T>
677 LInstruction* LChunkBuilder::DefineAsRegister(
678 LTemplateInstruction<1, I, T>* instr) {
680 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
684 template<int I, int T>
685 LInstruction* LChunkBuilder::DefineAsSpilled(
686 LTemplateInstruction<1, I, T>* instr,
689 new(zone()) LUnallocated(LUnallocated::FIXED_SLOT, index));
693 template<int I, int T>
694 LInstruction* LChunkBuilder::DefineSameAsFirst(
695 LTemplateInstruction<1, I, T>* instr) {
697 new(zone()) LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
701 template<int I, int T>
702 LInstruction* LChunkBuilder::DefineFixed(LTemplateInstruction<1, I, T>* instr,
704 return Define(instr, ToUnallocated(reg));
708 template<int I, int T>
709 LInstruction* LChunkBuilder::DefineFixedDouble(
710 LTemplateInstruction<1, I, T>* instr,
712 return Define(instr, ToUnallocated(reg));
716 LInstruction* LChunkBuilder::AssignEnvironment(LInstruction* instr) {
717 HEnvironment* hydrogen_env = current_block_->last_environment();
718 int argument_index_accumulator = 0;
719 instr->set_environment(CreateEnvironment(hydrogen_env,
720 &argument_index_accumulator));
725 LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
726 HInstruction* hinstr,
727 CanDeoptimize can_deoptimize) {
732 instr = AssignPointerMap(instr);
734 if (hinstr->HasObservableSideEffects()) {
735 ASSERT(hinstr->next()->IsSimulate());
736 HSimulate* sim = HSimulate::cast(hinstr->next());
737 ASSERT(instruction_pending_deoptimization_environment_ == NULL);
738 ASSERT(pending_deoptimization_ast_id_ == AstNode::kNoNumber);
739 instruction_pending_deoptimization_environment_ = instr;
740 pending_deoptimization_ast_id_ = sim->ast_id();
743 // If instruction does not have side-effects lazy deoptimization
744 // after the call will try to deoptimize to the point before the call.
745 // Thus we still need to attach environment to this call even if
746 // call sequence can not deoptimize eagerly.
747 bool needs_environment =
748 (can_deoptimize == CAN_DEOPTIMIZE_EAGERLY) ||
749 !hinstr->HasObservableSideEffects();
750 if (needs_environment && !instr->HasEnvironment()) {
751 instr = AssignEnvironment(instr);
758 LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
759 ASSERT(!instr->HasPointerMap());
760 instr->set_pointer_map(new(zone()) LPointerMap(position_));
765 LUnallocated* LChunkBuilder::TempRegister() {
766 LUnallocated* operand =
767 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
768 operand->set_virtual_register(allocator_->GetVirtualRegister());
769 if (!allocator_->AllocationOk()) Abort("Not enough virtual registers.");
774 LOperand* LChunkBuilder::FixedTemp(Register reg) {
775 LUnallocated* operand = ToUnallocated(reg);
776 ASSERT(operand->HasFixedPolicy());
781 LOperand* LChunkBuilder::FixedTemp(XMMRegister reg) {
782 LUnallocated* operand = ToUnallocated(reg);
783 ASSERT(operand->HasFixedPolicy());
788 LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
789 return new(zone()) LLabel(instr->block());
793 LInstruction* LChunkBuilder::DoSoftDeoptimize(HSoftDeoptimize* instr) {
794 return AssignEnvironment(new(zone()) LDeoptimize);
798 LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
799 return AssignEnvironment(new(zone()) LDeoptimize);
803 LInstruction* LChunkBuilder::DoShift(Token::Value op,
804 HBitwiseBinaryOperation* instr) {
805 if (instr->representation().IsTagged()) {
806 ASSERT(instr->left()->representation().IsTagged());
807 ASSERT(instr->right()->representation().IsTagged());
809 LOperand* left = UseFixed(instr->left(), rdx);
810 LOperand* right = UseFixed(instr->right(), rax);
811 LArithmeticT* result = new(zone()) LArithmeticT(op, left, right);
812 return MarkAsCall(DefineFixed(result, rax), instr);
815 ASSERT(instr->representation().IsInteger32());
816 ASSERT(instr->left()->representation().IsInteger32());
817 ASSERT(instr->right()->representation().IsInteger32());
818 LOperand* left = UseRegisterAtStart(instr->left());
820 HValue* right_value = instr->right();
821 LOperand* right = NULL;
822 int constant_value = 0;
823 if (right_value->IsConstant()) {
824 HConstant* constant = HConstant::cast(right_value);
825 right = chunk_->DefineConstantOperand(constant);
826 constant_value = constant->Integer32Value() & 0x1f;
828 right = UseFixed(right_value, rcx);
831 // Shift operations can only deoptimize if we do a logical shift by 0 and
832 // the result cannot be truncated to int32.
833 bool may_deopt = (op == Token::SHR && constant_value == 0);
834 bool does_deopt = false;
836 for (HUseIterator it(instr->uses()); !it.Done(); it.Advance()) {
837 if (!it.value()->CheckFlag(HValue::kTruncatingToInt32)) {
844 LInstruction* result =
845 DefineSameAsFirst(new(zone()) LShiftI(op, left, right, does_deopt));
846 return does_deopt ? AssignEnvironment(result) : result;
850 LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
851 HArithmeticBinaryOperation* instr) {
852 ASSERT(instr->representation().IsDouble());
853 ASSERT(instr->left()->representation().IsDouble());
854 ASSERT(instr->right()->representation().IsDouble());
855 ASSERT(op != Token::MOD);
856 LOperand* left = UseRegisterAtStart(instr->left());
857 LOperand* right = UseRegisterAtStart(instr->right());
858 LArithmeticD* result = new(zone()) LArithmeticD(op, left, right);
859 return DefineSameAsFirst(result);
863 LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
864 HArithmeticBinaryOperation* instr) {
865 ASSERT(op == Token::ADD ||
870 HValue* left = instr->left();
871 HValue* right = instr->right();
872 ASSERT(left->representation().IsTagged());
873 ASSERT(right->representation().IsTagged());
874 LOperand* left_operand = UseFixed(left, rdx);
875 LOperand* right_operand = UseFixed(right, rax);
876 LArithmeticT* result =
877 new(zone()) LArithmeticT(op, left_operand, right_operand);
878 return MarkAsCall(DefineFixed(result, rax), instr);
882 void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
883 ASSERT(is_building());
884 current_block_ = block;
885 next_block_ = next_block;
886 if (block->IsStartBlock()) {
887 block->UpdateEnvironment(graph_->start_environment());
889 } else if (block->predecessors()->length() == 1) {
890 // We have a single predecessor => copy environment and outgoing
891 // argument count from the predecessor.
892 ASSERT(block->phis()->length() == 0);
893 HBasicBlock* pred = block->predecessors()->at(0);
894 HEnvironment* last_environment = pred->last_environment();
895 ASSERT(last_environment != NULL);
896 // Only copy the environment, if it is later used again.
897 if (pred->end()->SecondSuccessor() == NULL) {
898 ASSERT(pred->end()->FirstSuccessor() == block);
900 if (pred->end()->FirstSuccessor()->block_id() > block->block_id() ||
901 pred->end()->SecondSuccessor()->block_id() > block->block_id()) {
902 last_environment = last_environment->Copy();
905 block->UpdateEnvironment(last_environment);
906 ASSERT(pred->argument_count() >= 0);
907 argument_count_ = pred->argument_count();
909 // We are at a state join => process phis.
910 HBasicBlock* pred = block->predecessors()->at(0);
911 // No need to copy the environment, it cannot be used later.
912 HEnvironment* last_environment = pred->last_environment();
913 for (int i = 0; i < block->phis()->length(); ++i) {
914 HPhi* phi = block->phis()->at(i);
915 last_environment->SetValueAt(phi->merged_index(), phi);
917 for (int i = 0; i < block->deleted_phis()->length(); ++i) {
918 last_environment->SetValueAt(block->deleted_phis()->at(i),
919 graph_->GetConstantUndefined());
921 block->UpdateEnvironment(last_environment);
922 // Pick up the outgoing argument count of one of the predecessors.
923 argument_count_ = pred->argument_count();
925 HInstruction* current = block->first();
926 int start = chunk_->instructions()->length();
927 while (current != NULL && !is_aborted()) {
928 // Code for constants in registers is generated lazily.
929 if (!current->EmitAtUses()) {
930 VisitInstruction(current);
932 current = current->next();
934 int end = chunk_->instructions()->length() - 1;
936 block->set_first_instruction_index(start);
937 block->set_last_instruction_index(end);
939 block->set_argument_count(argument_count_);
941 current_block_ = NULL;
945 void LChunkBuilder::VisitInstruction(HInstruction* current) {
946 HInstruction* old_current = current_instruction_;
947 current_instruction_ = current;
948 if (current->has_position()) position_ = current->position();
949 LInstruction* instr = current->CompileToLithium(this);
952 if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
953 instr = AssignPointerMap(instr);
955 if (FLAG_stress_environments && !instr->HasEnvironment()) {
956 instr = AssignEnvironment(instr);
958 instr->set_hydrogen_value(current);
959 chunk_->AddInstruction(instr, current_block_);
961 current_instruction_ = old_current;
965 LEnvironment* LChunkBuilder::CreateEnvironment(
966 HEnvironment* hydrogen_env,
967 int* argument_index_accumulator) {
968 if (hydrogen_env == NULL) return NULL;
970 LEnvironment* outer =
971 CreateEnvironment(hydrogen_env->outer(), argument_index_accumulator);
972 int ast_id = hydrogen_env->ast_id();
973 ASSERT(ast_id != AstNode::kNoNumber ||
974 hydrogen_env->frame_type() != JS_FUNCTION);
975 int value_count = hydrogen_env->length();
976 LEnvironment* result = new(zone()) LEnvironment(
977 hydrogen_env->closure(),
978 hydrogen_env->frame_type(),
980 hydrogen_env->parameter_count(),
984 int argument_index = *argument_index_accumulator;
985 for (int i = 0; i < value_count; ++i) {
986 if (hydrogen_env->is_special_index(i)) continue;
988 HValue* value = hydrogen_env->values()->at(i);
990 if (value->IsArgumentsObject()) {
992 } else if (value->IsPushArgument()) {
993 op = new(zone()) LArgument(argument_index++);
997 result->AddValue(op, value->representation());
1000 if (hydrogen_env->frame_type() == JS_FUNCTION) {
1001 *argument_index_accumulator = argument_index;
1008 LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
1009 return new(zone()) LGoto(instr->FirstSuccessor()->block_id());
1013 LInstruction* LChunkBuilder::DoBranch(HBranch* instr) {
1014 HValue* value = instr->value();
1015 if (value->EmitAtUses()) {
1016 ASSERT(value->IsConstant());
1017 ASSERT(!value->representation().IsDouble());
1018 HBasicBlock* successor = HConstant::cast(value)->ToBoolean()
1019 ? instr->FirstSuccessor()
1020 : instr->SecondSuccessor();
1021 return new(zone()) LGoto(successor->block_id());
1024 LBranch* result = new(zone()) LBranch(UseRegister(value));
1025 // Tagged values that are not known smis or booleans require a
1026 // deoptimization environment.
1027 Representation rep = value->representation();
1028 HType type = value->type();
1029 if (rep.IsTagged() && !type.IsSmi() && !type.IsBoolean()) {
1030 return AssignEnvironment(result);
1036 LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
1037 ASSERT(instr->value()->representation().IsTagged());
1038 LOperand* value = UseRegisterAtStart(instr->value());
1039 return new(zone()) LCmpMapAndBranch(value);
1043 LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* length) {
1044 return DefineAsRegister(new(zone()) LArgumentsLength(Use(length->value())));
1048 LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
1049 return DefineAsRegister(new(zone()) LArgumentsElements);
1053 LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
1054 LOperand* left = UseFixed(instr->left(), rax);
1055 LOperand* right = UseFixed(instr->right(), rdx);
1056 LInstanceOf* result = new(zone()) LInstanceOf(left, right);
1057 return MarkAsCall(DefineFixed(result, rax), instr);
1061 LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
1062 HInstanceOfKnownGlobal* instr) {
1063 LInstanceOfKnownGlobal* result =
1064 new(zone()) LInstanceOfKnownGlobal(UseFixed(instr->left(), rax),
1066 return MarkAsCall(DefineFixed(result, rax), instr);
1070 LInstruction* LChunkBuilder::DoWrapReceiver(HWrapReceiver* instr) {
1071 LOperand* receiver = UseRegister(instr->receiver());
1072 LOperand* function = UseRegisterAtStart(instr->function());
1073 LWrapReceiver* result = new(zone()) LWrapReceiver(receiver, function);
1074 return AssignEnvironment(DefineSameAsFirst(result));
1078 LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
1079 LOperand* function = UseFixed(instr->function(), rdi);
1080 LOperand* receiver = UseFixed(instr->receiver(), rax);
1081 LOperand* length = UseFixed(instr->length(), rbx);
1082 LOperand* elements = UseFixed(instr->elements(), rcx);
1083 LApplyArguments* result = new(zone()) LApplyArguments(function,
1087 return MarkAsCall(DefineFixed(result, rax), instr, CAN_DEOPTIMIZE_EAGERLY);
1091 LInstruction* LChunkBuilder::DoPushArgument(HPushArgument* instr) {
1093 LOperand* argument = UseOrConstant(instr->argument());
1094 return new(zone()) LPushArgument(argument);
1098 LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1099 return instr->HasNoUses()
1101 : DefineAsRegister(new(zone()) LThisFunction);
1105 LInstruction* LChunkBuilder::DoContext(HContext* instr) {
1106 return instr->HasNoUses() ? NULL : DefineAsRegister(new(zone()) LContext);
1110 LInstruction* LChunkBuilder::DoOuterContext(HOuterContext* instr) {
1111 LOperand* context = UseRegisterAtStart(instr->value());
1112 return DefineAsRegister(new(zone()) LOuterContext(context));
1116 LInstruction* LChunkBuilder::DoDeclareGlobals(HDeclareGlobals* instr) {
1117 return MarkAsCall(new(zone()) LDeclareGlobals, instr);
1121 LInstruction* LChunkBuilder::DoGlobalObject(HGlobalObject* instr) {
1122 return DefineAsRegister(new(zone()) LGlobalObject);
1126 LInstruction* LChunkBuilder::DoGlobalReceiver(HGlobalReceiver* instr) {
1127 LOperand* global_object = UseRegisterAtStart(instr->value());
1128 return DefineAsRegister(new(zone()) LGlobalReceiver(global_object));
1132 LInstruction* LChunkBuilder::DoCallConstantFunction(
1133 HCallConstantFunction* instr) {
1134 argument_count_ -= instr->argument_count();
1135 return MarkAsCall(DefineFixed(new(zone()) LCallConstantFunction, rax), instr);
1139 LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1140 LOperand* function = UseFixed(instr->function(), rdi);
1141 argument_count_ -= instr->argument_count();
1142 LInvokeFunction* result = new(zone()) LInvokeFunction(function);
1143 return MarkAsCall(DefineFixed(result, rax), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1147 LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
1148 BuiltinFunctionId op = instr->op();
1149 if (op == kMathLog || op == kMathSin || op == kMathCos || op == kMathTan) {
1150 LOperand* input = UseFixedDouble(instr->value(), xmm1);
1151 LUnaryMathOperation* result = new(zone()) LUnaryMathOperation(input);
1152 return MarkAsCall(DefineFixedDouble(result, xmm1), instr);
1154 LOperand* input = UseRegisterAtStart(instr->value());
1155 LUnaryMathOperation* result = new(zone()) LUnaryMathOperation(input);
1158 return AssignEnvironment(AssignPointerMap(DefineSameAsFirst(result)));
1160 return AssignEnvironment(DefineAsRegister(result));
1162 return AssignEnvironment(DefineAsRegister(result));
1164 return DefineSameAsFirst(result);
1166 return DefineSameAsFirst(result);
1175 LInstruction* LChunkBuilder::DoCallKeyed(HCallKeyed* instr) {
1176 ASSERT(instr->key()->representation().IsTagged());
1177 LOperand* key = UseFixed(instr->key(), rcx);
1178 argument_count_ -= instr->argument_count();
1179 LCallKeyed* result = new(zone()) LCallKeyed(key);
1180 return MarkAsCall(DefineFixed(result, rax), instr);
1184 LInstruction* LChunkBuilder::DoCallNamed(HCallNamed* instr) {
1185 argument_count_ -= instr->argument_count();
1186 return MarkAsCall(DefineFixed(new(zone()) LCallNamed, rax), instr);
1190 LInstruction* LChunkBuilder::DoCallGlobal(HCallGlobal* instr) {
1191 argument_count_ -= instr->argument_count();
1192 return MarkAsCall(DefineFixed(new(zone()) LCallGlobal, rax), instr);
1196 LInstruction* LChunkBuilder::DoCallKnownGlobal(HCallKnownGlobal* instr) {
1197 argument_count_ -= instr->argument_count();
1198 return MarkAsCall(DefineFixed(new(zone()) LCallKnownGlobal, rax), instr);
1202 LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
1203 LOperand* constructor = UseFixed(instr->constructor(), rdi);
1204 argument_count_ -= instr->argument_count();
1205 LCallNew* result = new(zone()) LCallNew(constructor);
1206 return MarkAsCall(DefineFixed(result, rax), instr);
1210 LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
1211 LOperand* function = UseFixed(instr->function(), rdi);
1212 argument_count_ -= instr->argument_count();
1213 LCallFunction* result = new(zone()) LCallFunction(function);
1214 return MarkAsCall(DefineFixed(result, rax), instr);
1218 LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1219 argument_count_ -= instr->argument_count();
1220 return MarkAsCall(DefineFixed(new(zone()) LCallRuntime, rax), instr);
1224 LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1225 return DoShift(Token::SHR, instr);
1229 LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1230 return DoShift(Token::SAR, instr);
1234 LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1235 return DoShift(Token::SHL, instr);
1239 LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
1240 if (instr->representation().IsInteger32()) {
1241 ASSERT(instr->left()->representation().IsInteger32());
1242 ASSERT(instr->right()->representation().IsInteger32());
1244 LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1245 LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1246 return DefineSameAsFirst(new(zone()) LBitI(left, right));
1248 ASSERT(instr->representation().IsTagged());
1249 ASSERT(instr->left()->representation().IsTagged());
1250 ASSERT(instr->right()->representation().IsTagged());
1252 LOperand* left = UseFixed(instr->left(), rdx);
1253 LOperand* right = UseFixed(instr->right(), rax);
1254 LArithmeticT* result = new(zone()) LArithmeticT(instr->op(), left, right);
1255 return MarkAsCall(DefineFixed(result, rax), instr);
1260 LInstruction* LChunkBuilder::DoBitNot(HBitNot* instr) {
1261 ASSERT(instr->value()->representation().IsInteger32());
1262 ASSERT(instr->representation().IsInteger32());
1263 if (instr->HasNoUses()) return NULL;
1264 LOperand* input = UseRegisterAtStart(instr->value());
1265 LBitNotI* result = new(zone()) LBitNotI(input);
1266 return DefineSameAsFirst(result);
1270 LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
1271 if (instr->representation().IsDouble()) {
1272 return DoArithmeticD(Token::DIV, instr);
1273 } else if (instr->representation().IsInteger32()) {
1274 // The temporary operand is necessary to ensure that right is not allocated
1276 LOperand* temp = FixedTemp(rdx);
1277 LOperand* dividend = UseFixed(instr->left(), rax);
1278 LOperand* divisor = UseRegister(instr->right());
1279 LDivI* result = new(zone()) LDivI(dividend, divisor, temp);
1280 return AssignEnvironment(DefineFixed(result, rax));
1282 ASSERT(instr->representation().IsTagged());
1283 return DoArithmeticT(Token::DIV, instr);
1288 LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
1294 LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1295 if (instr->representation().IsInteger32()) {
1296 ASSERT(instr->left()->representation().IsInteger32());
1297 ASSERT(instr->right()->representation().IsInteger32());
1299 LInstruction* result;
1300 if (instr->HasPowerOf2Divisor()) {
1301 ASSERT(!instr->CheckFlag(HValue::kCanBeDivByZero));
1302 LOperand* value = UseRegisterAtStart(instr->left());
1304 new(zone()) LModI(value, UseOrConstant(instr->right()), NULL);
1305 result = DefineSameAsFirst(mod);
1307 // The temporary operand is necessary to ensure that right is not
1308 // allocated into edx.
1309 LOperand* temp = FixedTemp(rdx);
1310 LOperand* value = UseFixed(instr->left(), rax);
1311 LOperand* divisor = UseRegister(instr->right());
1312 LModI* mod = new(zone()) LModI(value, divisor, temp);
1313 result = DefineFixed(mod, rdx);
1316 return (instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1317 instr->CheckFlag(HValue::kCanBeDivByZero))
1318 ? AssignEnvironment(result)
1320 } else if (instr->representation().IsTagged()) {
1321 return DoArithmeticT(Token::MOD, instr);
1323 ASSERT(instr->representation().IsDouble());
1324 // We call a C function for double modulo. It can't trigger a GC.
1325 // We need to use fixed result register for the call.
1326 // TODO(fschneider): Allow any register as input registers.
1327 LOperand* left = UseFixedDouble(instr->left(), xmm2);
1328 LOperand* right = UseFixedDouble(instr->right(), xmm1);
1329 LArithmeticD* result = new(zone()) LArithmeticD(Token::MOD, left, right);
1330 return MarkAsCall(DefineFixedDouble(result, xmm1), instr);
1335 LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1336 if (instr->representation().IsInteger32()) {
1337 ASSERT(instr->left()->representation().IsInteger32());
1338 ASSERT(instr->right()->representation().IsInteger32());
1339 LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1340 LOperand* right = UseOrConstant(instr->MostConstantOperand());
1341 LMulI* mul = new(zone()) LMulI(left, right);
1342 if (instr->CheckFlag(HValue::kCanOverflow) ||
1343 instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1344 AssignEnvironment(mul);
1346 return DefineSameAsFirst(mul);
1347 } else if (instr->representation().IsDouble()) {
1348 return DoArithmeticD(Token::MUL, instr);
1350 ASSERT(instr->representation().IsTagged());
1351 return DoArithmeticT(Token::MUL, instr);
1356 LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1357 if (instr->representation().IsInteger32()) {
1358 ASSERT(instr->left()->representation().IsInteger32());
1359 ASSERT(instr->right()->representation().IsInteger32());
1360 LOperand* left = UseRegisterAtStart(instr->left());
1361 LOperand* right = UseOrConstantAtStart(instr->right());
1362 LSubI* sub = new(zone()) LSubI(left, right);
1363 LInstruction* result = DefineSameAsFirst(sub);
1364 if (instr->CheckFlag(HValue::kCanOverflow)) {
1365 result = AssignEnvironment(result);
1368 } else if (instr->representation().IsDouble()) {
1369 return DoArithmeticD(Token::SUB, instr);
1371 ASSERT(instr->representation().IsTagged());
1372 return DoArithmeticT(Token::SUB, instr);
1377 LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1378 if (instr->representation().IsInteger32()) {
1379 ASSERT(instr->left()->representation().IsInteger32());
1380 ASSERT(instr->right()->representation().IsInteger32());
1381 LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1382 LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1383 LAddI* add = new(zone()) LAddI(left, right);
1384 LInstruction* result = DefineSameAsFirst(add);
1385 if (instr->CheckFlag(HValue::kCanOverflow)) {
1386 result = AssignEnvironment(result);
1389 } else if (instr->representation().IsDouble()) {
1390 return DoArithmeticD(Token::ADD, instr);
1392 ASSERT(instr->representation().IsTagged());
1393 return DoArithmeticT(Token::ADD, instr);
1399 LInstruction* LChunkBuilder::DoPower(HPower* instr) {
1400 ASSERT(instr->representation().IsDouble());
1401 // We call a C function for double power. It can't trigger a GC.
1402 // We need to use fixed result register for the call.
1403 Representation exponent_type = instr->right()->representation();
1404 ASSERT(instr->left()->representation().IsDouble());
1405 LOperand* left = UseFixedDouble(instr->left(), xmm2);
1406 LOperand* right = exponent_type.IsDouble() ?
1407 UseFixedDouble(instr->right(), xmm1) :
1409 UseFixed(instr->right(), rdx);
1411 UseFixed(instr->right(), rdi);
1413 LPower* result = new(zone()) LPower(left, right);
1414 return MarkAsCall(DefineFixedDouble(result, xmm3), instr,
1415 CAN_DEOPTIMIZE_EAGERLY);
1419 LInstruction* LChunkBuilder::DoRandom(HRandom* instr) {
1420 ASSERT(instr->representation().IsDouble());
1421 ASSERT(instr->global_object()->representation().IsTagged());
1423 LOperand* global_object = UseFixed(instr->global_object(), rcx);
1425 LOperand* global_object = UseFixed(instr->global_object(), rdi);
1427 LRandom* result = new(zone()) LRandom(global_object);
1428 return MarkAsCall(DefineFixedDouble(result, xmm1), instr);
1432 LInstruction* LChunkBuilder::DoCompareGeneric(HCompareGeneric* instr) {
1433 ASSERT(instr->left()->representation().IsTagged());
1434 ASSERT(instr->right()->representation().IsTagged());
1435 LOperand* left = UseFixed(instr->left(), rdx);
1436 LOperand* right = UseFixed(instr->right(), rax);
1437 LCmpT* result = new(zone()) LCmpT(left, right);
1438 return MarkAsCall(DefineFixed(result, rax), instr);
1442 LInstruction* LChunkBuilder::DoCompareIDAndBranch(
1443 HCompareIDAndBranch* instr) {
1444 Representation r = instr->GetInputRepresentation();
1445 if (r.IsInteger32()) {
1446 ASSERT(instr->left()->representation().IsInteger32());
1447 ASSERT(instr->right()->representation().IsInteger32());
1448 LOperand* left = UseRegisterOrConstantAtStart(instr->left());
1449 LOperand* right = UseOrConstantAtStart(instr->right());
1450 return new(zone()) LCmpIDAndBranch(left, right);
1452 ASSERT(r.IsDouble());
1453 ASSERT(instr->left()->representation().IsDouble());
1454 ASSERT(instr->right()->representation().IsDouble());
1457 if (instr->left()->IsConstant() && instr->right()->IsConstant()) {
1458 left = UseRegisterOrConstantAtStart(instr->left());
1459 right = UseRegisterOrConstantAtStart(instr->right());
1461 left = UseRegisterAtStart(instr->left());
1462 right = UseRegisterAtStart(instr->right());
1464 return new(zone()) LCmpIDAndBranch(left, right);
1469 LInstruction* LChunkBuilder::DoCompareObjectEqAndBranch(
1470 HCompareObjectEqAndBranch* instr) {
1471 LOperand* left = UseRegisterAtStart(instr->left());
1472 LOperand* right = UseRegisterAtStart(instr->right());
1473 return new(zone()) LCmpObjectEqAndBranch(left, right);
1477 LInstruction* LChunkBuilder::DoCompareConstantEqAndBranch(
1478 HCompareConstantEqAndBranch* instr) {
1479 LOperand* value = UseRegisterAtStart(instr->value());
1480 return new(zone()) LCmpConstantEqAndBranch(value);
1484 LInstruction* LChunkBuilder::DoIsNilAndBranch(HIsNilAndBranch* instr) {
1485 ASSERT(instr->value()->representation().IsTagged());
1486 LOperand* temp = instr->kind() == kStrictEquality ? NULL : TempRegister();
1487 return new(zone()) LIsNilAndBranch(UseRegisterAtStart(instr->value()), temp);
1491 LInstruction* LChunkBuilder::DoIsObjectAndBranch(HIsObjectAndBranch* instr) {
1492 ASSERT(instr->value()->representation().IsTagged());
1493 return new(zone()) LIsObjectAndBranch(UseRegisterAtStart(instr->value()));
1497 LInstruction* LChunkBuilder::DoIsStringAndBranch(HIsStringAndBranch* instr) {
1498 ASSERT(instr->value()->representation().IsTagged());
1499 LOperand* value = UseRegisterAtStart(instr->value());
1500 LOperand* temp = TempRegister();
1501 return new(zone()) LIsStringAndBranch(value, temp);
1505 LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
1506 ASSERT(instr->value()->representation().IsTagged());
1507 return new(zone()) LIsSmiAndBranch(Use(instr->value()));
1511 LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
1512 HIsUndetectableAndBranch* instr) {
1513 ASSERT(instr->value()->representation().IsTagged());
1514 LOperand* value = UseRegisterAtStart(instr->value());
1515 LOperand* temp = TempRegister();
1516 return new(zone()) LIsUndetectableAndBranch(value, temp);
1520 LInstruction* LChunkBuilder::DoStringCompareAndBranch(
1521 HStringCompareAndBranch* instr) {
1523 ASSERT(instr->left()->representation().IsTagged());
1524 ASSERT(instr->right()->representation().IsTagged());
1525 LOperand* left = UseFixed(instr->left(), rdx);
1526 LOperand* right = UseFixed(instr->right(), rax);
1527 LStringCompareAndBranch* result =
1528 new(zone()) LStringCompareAndBranch(left, right);
1530 return MarkAsCall(result, instr);
1534 LInstruction* LChunkBuilder::DoHasInstanceTypeAndBranch(
1535 HHasInstanceTypeAndBranch* instr) {
1536 ASSERT(instr->value()->representation().IsTagged());
1537 LOperand* value = UseRegisterAtStart(instr->value());
1538 return new(zone()) LHasInstanceTypeAndBranch(value);
1542 LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
1543 HGetCachedArrayIndex* instr) {
1544 ASSERT(instr->value()->representation().IsTagged());
1545 LOperand* value = UseRegisterAtStart(instr->value());
1547 return DefineAsRegister(new(zone()) LGetCachedArrayIndex(value));
1551 LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
1552 HHasCachedArrayIndexAndBranch* instr) {
1553 ASSERT(instr->value()->representation().IsTagged());
1554 LOperand* value = UseRegisterAtStart(instr->value());
1555 return new(zone()) LHasCachedArrayIndexAndBranch(value);
1559 LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
1560 HClassOfTestAndBranch* instr) {
1561 LOperand* value = UseRegister(instr->value());
1562 return new(zone()) LClassOfTestAndBranch(value,
1568 LInstruction* LChunkBuilder::DoJSArrayLength(HJSArrayLength* instr) {
1569 LOperand* array = UseRegisterAtStart(instr->value());
1570 return DefineAsRegister(new(zone()) LJSArrayLength(array));
1574 LInstruction* LChunkBuilder::DoFixedArrayBaseLength(
1575 HFixedArrayBaseLength* instr) {
1576 LOperand* array = UseRegisterAtStart(instr->value());
1577 return DefineAsRegister(new(zone()) LFixedArrayBaseLength(array));
1581 LInstruction* LChunkBuilder::DoElementsKind(HElementsKind* instr) {
1582 LOperand* object = UseRegisterAtStart(instr->value());
1583 return DefineAsRegister(new(zone()) LElementsKind(object));
1587 LInstruction* LChunkBuilder::DoValueOf(HValueOf* instr) {
1588 LOperand* object = UseRegister(instr->value());
1589 LValueOf* result = new(zone()) LValueOf(object);
1590 return DefineSameAsFirst(result);
1594 LInstruction* LChunkBuilder::DoDateField(HDateField* instr) {
1595 LOperand* object = UseFixed(instr->value(), rax);
1596 LDateField* result = new LDateField(object, instr->index());
1597 return MarkAsCall(DefineFixed(result, rax), instr);
1601 LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
1602 LOperand* value = UseRegisterOrConstantAtStart(instr->index());
1603 LOperand* length = Use(instr->length());
1604 return AssignEnvironment(new(zone()) LBoundsCheck(value, length));
1608 LInstruction* LChunkBuilder::DoAbnormalExit(HAbnormalExit* instr) {
1609 // The control instruction marking the end of a block that completed
1610 // abruptly (e.g., threw an exception). There is nothing specific to do.
1615 LInstruction* LChunkBuilder::DoThrow(HThrow* instr) {
1616 LOperand* value = UseFixed(instr->value(), rax);
1617 return MarkAsCall(new(zone()) LThrow(value), instr);
1621 LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
1626 LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
1627 // All HForceRepresentation instructions should be eliminated in the
1628 // representation change phase of Hydrogen.
1634 LInstruction* LChunkBuilder::DoChange(HChange* instr) {
1635 Representation from = instr->from();
1636 Representation to = instr->to();
1637 if (from.IsTagged()) {
1638 if (to.IsDouble()) {
1639 LOperand* value = UseRegister(instr->value());
1640 LNumberUntagD* res = new(zone()) LNumberUntagD(value);
1641 return AssignEnvironment(DefineAsRegister(res));
1643 ASSERT(to.IsInteger32());
1644 LOperand* value = UseRegister(instr->value());
1645 bool needs_check = !instr->value()->type().IsSmi();
1647 bool truncating = instr->CanTruncateToInt32();
1648 LOperand* xmm_temp = truncating ? NULL : FixedTemp(xmm1);
1649 LTaggedToI* res = new(zone()) LTaggedToI(value, xmm_temp);
1650 return AssignEnvironment(DefineSameAsFirst(res));
1652 return DefineSameAsFirst(new(zone()) LSmiUntag(value, needs_check));
1655 } else if (from.IsDouble()) {
1656 if (to.IsTagged()) {
1657 LOperand* value = UseRegister(instr->value());
1658 LOperand* temp = TempRegister();
1660 // Make sure that temp and result_temp are different registers.
1661 LUnallocated* result_temp = TempRegister();
1662 LNumberTagD* result = new(zone()) LNumberTagD(value, temp);
1663 return AssignPointerMap(Define(result, result_temp));
1665 ASSERT(to.IsInteger32());
1666 LOperand* value = UseRegister(instr->value());
1667 return AssignEnvironment(DefineAsRegister(new(zone()) LDoubleToI(value)));
1669 } else if (from.IsInteger32()) {
1670 if (to.IsTagged()) {
1671 HValue* val = instr->value();
1672 LOperand* value = UseRegister(val);
1673 if (val->HasRange() && val->range()->IsInSmiRange()) {
1674 return DefineSameAsFirst(new(zone()) LSmiTag(value));
1676 LNumberTagI* result = new(zone()) LNumberTagI(value);
1677 return AssignEnvironment(AssignPointerMap(DefineSameAsFirst(result)));
1680 ASSERT(to.IsDouble());
1681 LOperand* value = Use(instr->value());
1682 return DefineAsRegister(new(zone()) LInteger32ToDouble(value));
1690 LInstruction* LChunkBuilder::DoCheckNonSmi(HCheckNonSmi* instr) {
1691 LOperand* value = UseRegisterAtStart(instr->value());
1692 return AssignEnvironment(new(zone()) LCheckNonSmi(value));
1696 LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
1697 LOperand* value = UseRegisterAtStart(instr->value());
1698 LCheckInstanceType* result = new(zone()) LCheckInstanceType(value);
1699 return AssignEnvironment(result);
1703 LInstruction* LChunkBuilder::DoCheckPrototypeMaps(HCheckPrototypeMaps* instr) {
1704 LOperand* temp = TempRegister();
1705 LCheckPrototypeMaps* result = new(zone()) LCheckPrototypeMaps(temp);
1706 return AssignEnvironment(result);
1710 LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
1711 LOperand* value = UseRegisterAtStart(instr->value());
1712 return AssignEnvironment(new(zone()) LCheckSmi(value));
1716 LInstruction* LChunkBuilder::DoCheckFunction(HCheckFunction* instr) {
1717 LOperand* value = UseRegisterAtStart(instr->value());
1718 return AssignEnvironment(new(zone()) LCheckFunction(value));
1722 LInstruction* LChunkBuilder::DoCheckMaps(HCheckMaps* instr) {
1723 LOperand* value = UseRegisterAtStart(instr->value());
1724 LCheckMaps* result = new(zone()) LCheckMaps(value);
1725 return AssignEnvironment(result);
1729 LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
1730 HValue* value = instr->value();
1731 Representation input_rep = value->representation();
1732 LOperand* reg = UseRegister(value);
1733 if (input_rep.IsDouble()) {
1734 return DefineAsRegister(new(zone()) LClampDToUint8(reg,
1736 } else if (input_rep.IsInteger32()) {
1737 return DefineSameAsFirst(new(zone()) LClampIToUint8(reg));
1739 ASSERT(input_rep.IsTagged());
1740 // Register allocator doesn't (yet) support allocation of double
1741 // temps. Reserve xmm1 explicitly.
1742 LClampTToUint8* result = new(zone()) LClampTToUint8(reg,
1745 return AssignEnvironment(DefineSameAsFirst(result));
1750 LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
1751 return new(zone()) LReturn(UseFixed(instr->value(), rax));
1755 LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
1756 Representation r = instr->representation();
1757 if (r.IsInteger32()) {
1758 return DefineAsRegister(new(zone()) LConstantI);
1759 } else if (r.IsDouble()) {
1760 LOperand* temp = TempRegister();
1761 return DefineAsRegister(new(zone()) LConstantD(temp));
1762 } else if (r.IsTagged()) {
1763 return DefineAsRegister(new(zone()) LConstantT);
1771 LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
1772 LLoadGlobalCell* result = new(zone()) LLoadGlobalCell;
1773 return instr->RequiresHoleCheck()
1774 ? AssignEnvironment(DefineAsRegister(result))
1775 : DefineAsRegister(result);
1779 LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
1780 LOperand* global_object = UseFixed(instr->global_object(), rax);
1781 LLoadGlobalGeneric* result = new(zone()) LLoadGlobalGeneric(global_object);
1782 return MarkAsCall(DefineFixed(result, rax), instr);
1786 LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
1787 LOperand* value = UseRegister(instr->value());
1788 // Use a temp to avoid reloading the cell value address in the case where
1789 // we perform a hole check.
1790 return instr->RequiresHoleCheck()
1791 ? AssignEnvironment(new(zone()) LStoreGlobalCell(value, TempRegister()))
1792 : new(zone()) LStoreGlobalCell(value, NULL);
1796 LInstruction* LChunkBuilder::DoStoreGlobalGeneric(HStoreGlobalGeneric* instr) {
1797 LOperand* global_object = UseFixed(instr->global_object(), rdx);
1798 LOperand* value = UseFixed(instr->value(), rax);
1799 LStoreGlobalGeneric* result = new(zone()) LStoreGlobalGeneric(global_object,
1801 return MarkAsCall(result, instr);
1805 LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
1806 LOperand* context = UseRegisterAtStart(instr->value());
1807 LInstruction* result =
1808 DefineAsRegister(new(zone()) LLoadContextSlot(context));
1809 return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
1813 LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
1817 if (instr->NeedsWriteBarrier()) {
1818 context = UseTempRegister(instr->context());
1819 value = UseTempRegister(instr->value());
1820 temp = TempRegister();
1822 context = UseRegister(instr->context());
1823 value = UseRegister(instr->value());
1826 LInstruction* result = new(zone()) LStoreContextSlot(context, value, temp);
1827 return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
1831 LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
1832 ASSERT(instr->representation().IsTagged());
1833 LOperand* obj = UseRegisterAtStart(instr->object());
1834 return DefineAsRegister(new(zone()) LLoadNamedField(obj));
1838 LInstruction* LChunkBuilder::DoLoadNamedFieldPolymorphic(
1839 HLoadNamedFieldPolymorphic* instr) {
1840 ASSERT(instr->representation().IsTagged());
1841 if (instr->need_generic()) {
1842 LOperand* obj = UseFixed(instr->object(), rax);
1843 LLoadNamedFieldPolymorphic* result =
1844 new(zone()) LLoadNamedFieldPolymorphic(obj);
1845 return MarkAsCall(DefineFixed(result, rax), instr);
1847 LOperand* obj = UseRegisterAtStart(instr->object());
1848 LLoadNamedFieldPolymorphic* result =
1849 new(zone()) LLoadNamedFieldPolymorphic(obj);
1850 return AssignEnvironment(DefineAsRegister(result));
1855 LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
1856 LOperand* object = UseFixed(instr->object(), rax);
1857 LLoadNamedGeneric* result = new(zone()) LLoadNamedGeneric(object);
1858 return MarkAsCall(DefineFixed(result, rax), instr);
1862 LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
1863 HLoadFunctionPrototype* instr) {
1864 return AssignEnvironment(DefineAsRegister(
1865 new(zone()) LLoadFunctionPrototype(UseRegister(instr->function()))));
1869 LInstruction* LChunkBuilder::DoLoadElements(HLoadElements* instr) {
1870 LOperand* input = UseRegisterAtStart(instr->value());
1871 return DefineAsRegister(new(zone()) LLoadElements(input));
1875 LInstruction* LChunkBuilder::DoLoadExternalArrayPointer(
1876 HLoadExternalArrayPointer* instr) {
1877 LOperand* input = UseRegisterAtStart(instr->value());
1878 return DefineAsRegister(new(zone()) LLoadExternalArrayPointer(input));
1882 LInstruction* LChunkBuilder::DoLoadKeyedFastElement(
1883 HLoadKeyedFastElement* instr) {
1884 ASSERT(instr->representation().IsTagged());
1885 ASSERT(instr->key()->representation().IsInteger32());
1886 LOperand* obj = UseRegisterAtStart(instr->object());
1887 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1888 LLoadKeyedFastElement* result = new(zone()) LLoadKeyedFastElement(obj, key);
1889 if (instr->RequiresHoleCheck()) AssignEnvironment(result);
1890 return DefineAsRegister(result);
1894 LInstruction* LChunkBuilder::DoLoadKeyedFastDoubleElement(
1895 HLoadKeyedFastDoubleElement* instr) {
1896 ASSERT(instr->representation().IsDouble());
1897 ASSERT(instr->key()->representation().IsInteger32());
1898 LOperand* elements = UseRegisterAtStart(instr->elements());
1899 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1900 LLoadKeyedFastDoubleElement* result =
1901 new(zone()) LLoadKeyedFastDoubleElement(elements, key);
1902 return AssignEnvironment(DefineAsRegister(result));
1906 LInstruction* LChunkBuilder::DoLoadKeyedSpecializedArrayElement(
1907 HLoadKeyedSpecializedArrayElement* instr) {
1908 ElementsKind elements_kind = instr->elements_kind();
1910 (instr->representation().IsInteger32() &&
1911 (elements_kind != EXTERNAL_FLOAT_ELEMENTS) &&
1912 (elements_kind != EXTERNAL_DOUBLE_ELEMENTS)) ||
1913 (instr->representation().IsDouble() &&
1914 ((elements_kind == EXTERNAL_FLOAT_ELEMENTS) ||
1915 (elements_kind == EXTERNAL_DOUBLE_ELEMENTS))));
1916 ASSERT(instr->key()->representation().IsInteger32());
1917 LOperand* external_pointer = UseRegister(instr->external_pointer());
1918 LOperand* key = UseRegisterOrConstant(instr->key());
1919 LLoadKeyedSpecializedArrayElement* result =
1920 new(zone()) LLoadKeyedSpecializedArrayElement(external_pointer, key);
1921 LInstruction* load_instr = DefineAsRegister(result);
1922 // An unsigned int array load might overflow and cause a deopt, make sure it
1923 // has an environment.
1924 return (elements_kind == EXTERNAL_UNSIGNED_INT_ELEMENTS) ?
1925 AssignEnvironment(load_instr) : load_instr;
1929 LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
1930 LOperand* object = UseFixed(instr->object(), rdx);
1931 LOperand* key = UseFixed(instr->key(), rax);
1933 LLoadKeyedGeneric* result = new(zone()) LLoadKeyedGeneric(object, key);
1934 return MarkAsCall(DefineFixed(result, rax), instr);
1938 LInstruction* LChunkBuilder::DoStoreKeyedFastElement(
1939 HStoreKeyedFastElement* instr) {
1940 bool needs_write_barrier = instr->NeedsWriteBarrier();
1941 ASSERT(instr->value()->representation().IsTagged());
1942 ASSERT(instr->object()->representation().IsTagged());
1943 ASSERT(instr->key()->representation().IsInteger32());
1945 LOperand* obj = UseTempRegister(instr->object());
1946 LOperand* val = needs_write_barrier
1947 ? UseTempRegister(instr->value())
1948 : UseRegisterAtStart(instr->value());
1949 LOperand* key = needs_write_barrier
1950 ? UseTempRegister(instr->key())
1951 : UseRegisterOrConstantAtStart(instr->key());
1952 return new(zone()) LStoreKeyedFastElement(obj, key, val);
1956 LInstruction* LChunkBuilder::DoStoreKeyedFastDoubleElement(
1957 HStoreKeyedFastDoubleElement* instr) {
1958 ASSERT(instr->value()->representation().IsDouble());
1959 ASSERT(instr->elements()->representation().IsTagged());
1960 ASSERT(instr->key()->representation().IsInteger32());
1962 LOperand* elements = UseRegisterAtStart(instr->elements());
1963 LOperand* val = UseTempRegister(instr->value());
1964 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1966 return new(zone()) LStoreKeyedFastDoubleElement(elements, key, val);
1970 LInstruction* LChunkBuilder::DoStoreKeyedSpecializedArrayElement(
1971 HStoreKeyedSpecializedArrayElement* instr) {
1972 ElementsKind elements_kind = instr->elements_kind();
1974 (instr->value()->representation().IsInteger32() &&
1975 (elements_kind != EXTERNAL_FLOAT_ELEMENTS) &&
1976 (elements_kind != EXTERNAL_DOUBLE_ELEMENTS)) ||
1977 (instr->value()->representation().IsDouble() &&
1978 ((elements_kind == EXTERNAL_FLOAT_ELEMENTS) ||
1979 (elements_kind == EXTERNAL_DOUBLE_ELEMENTS))));
1980 ASSERT(instr->external_pointer()->representation().IsExternal());
1981 ASSERT(instr->key()->representation().IsInteger32());
1983 LOperand* external_pointer = UseRegister(instr->external_pointer());
1984 bool val_is_temp_register =
1985 elements_kind == EXTERNAL_PIXEL_ELEMENTS ||
1986 elements_kind == EXTERNAL_FLOAT_ELEMENTS;
1987 LOperand* val = val_is_temp_register
1988 ? UseTempRegister(instr->value())
1989 : UseRegister(instr->value());
1990 LOperand* key = UseRegisterOrConstant(instr->key());
1992 return new(zone()) LStoreKeyedSpecializedArrayElement(external_pointer,
1998 LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
1999 LOperand* object = UseFixed(instr->object(), rdx);
2000 LOperand* key = UseFixed(instr->key(), rcx);
2001 LOperand* value = UseFixed(instr->value(), rax);
2003 ASSERT(instr->object()->representation().IsTagged());
2004 ASSERT(instr->key()->representation().IsTagged());
2005 ASSERT(instr->value()->representation().IsTagged());
2007 LStoreKeyedGeneric* result =
2008 new(zone()) LStoreKeyedGeneric(object, key, value);
2009 return MarkAsCall(result, instr);
2013 LInstruction* LChunkBuilder::DoTransitionElementsKind(
2014 HTransitionElementsKind* instr) {
2015 if (instr->original_map()->elements_kind() == FAST_SMI_ONLY_ELEMENTS &&
2016 instr->transitioned_map()->elements_kind() == FAST_ELEMENTS) {
2017 LOperand* object = UseRegister(instr->object());
2018 LOperand* new_map_reg = TempRegister();
2019 LOperand* temp_reg = TempRegister();
2020 LTransitionElementsKind* result =
2021 new(zone()) LTransitionElementsKind(object, new_map_reg, temp_reg);
2022 return DefineSameAsFirst(result);
2024 LOperand* object = UseFixed(instr->object(), rax);
2025 LOperand* fixed_object_reg = FixedTemp(rdx);
2026 LOperand* new_map_reg = FixedTemp(rbx);
2027 LTransitionElementsKind* result =
2028 new(zone()) LTransitionElementsKind(object,
2031 return MarkAsCall(DefineFixed(result, rax), instr);
2036 LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
2037 bool needs_write_barrier = instr->NeedsWriteBarrier();
2039 LOperand* obj = needs_write_barrier
2040 ? UseTempRegister(instr->object())
2041 : UseRegisterAtStart(instr->object());
2043 LOperand* val = needs_write_barrier
2044 ? UseTempRegister(instr->value())
2045 : UseRegister(instr->value());
2047 // We only need a scratch register if we have a write barrier or we
2048 // have a store into the properties array (not in-object-property).
2049 LOperand* temp = (!instr->is_in_object() || needs_write_barrier)
2050 ? TempRegister() : NULL;
2052 return new(zone()) LStoreNamedField(obj, val, temp);
2056 LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
2057 LOperand* object = UseFixed(instr->object(), rdx);
2058 LOperand* value = UseFixed(instr->value(), rax);
2060 LStoreNamedGeneric* result = new(zone()) LStoreNamedGeneric(object, value);
2061 return MarkAsCall(result, instr);
2065 LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
2066 LOperand* left = UseOrConstantAtStart(instr->left());
2067 LOperand* right = UseOrConstantAtStart(instr->right());
2068 return MarkAsCall(DefineFixed(new(zone()) LStringAdd(left, right), rax),
2073 LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
2074 LOperand* string = UseTempRegister(instr->string());
2075 LOperand* index = UseTempRegister(instr->index());
2076 LStringCharCodeAt* result = new(zone()) LStringCharCodeAt(string, index);
2077 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
2081 LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2082 LOperand* char_code = UseRegister(instr->value());
2083 LStringCharFromCode* result = new(zone()) LStringCharFromCode(char_code);
2084 return AssignPointerMap(DefineAsRegister(result));
2088 LInstruction* LChunkBuilder::DoStringLength(HStringLength* instr) {
2089 LOperand* string = UseRegisterAtStart(instr->value());
2090 return DefineAsRegister(new(zone()) LStringLength(string));
2094 LInstruction* LChunkBuilder::DoAllocateObject(HAllocateObject* instr) {
2095 LAllocateObject* result = new LAllocateObject(TempRegister());
2096 return AssignPointerMap(DefineAsRegister(result));
2100 LInstruction* LChunkBuilder::DoFastLiteral(HFastLiteral* instr) {
2101 return MarkAsCall(DefineFixed(new(zone()) LFastLiteral, rax), instr);
2105 LInstruction* LChunkBuilder::DoArrayLiteral(HArrayLiteral* instr) {
2106 return MarkAsCall(DefineFixed(new(zone()) LArrayLiteral, rax), instr);
2110 LInstruction* LChunkBuilder::DoObjectLiteral(HObjectLiteral* instr) {
2111 return MarkAsCall(DefineFixed(new(zone()) LObjectLiteral, rax), instr);
2115 LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2116 return MarkAsCall(DefineFixed(new(zone()) LRegExpLiteral, rax), instr);
2120 LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2121 return MarkAsCall(DefineFixed(new(zone()) LFunctionLiteral, rax), instr);
2125 LInstruction* LChunkBuilder::DoDeleteProperty(HDeleteProperty* instr) {
2126 LOperand* object = UseAtStart(instr->object());
2127 LOperand* key = UseOrConstantAtStart(instr->key());
2128 LDeleteProperty* result = new(zone()) LDeleteProperty(object, key);
2129 return MarkAsCall(DefineFixed(result, rax), instr);
2133 LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2134 allocator_->MarkAsOsrEntry();
2135 current_block_->last_environment()->set_ast_id(instr->ast_id());
2136 return AssignEnvironment(new(zone()) LOsrEntry);
2140 LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2141 int spill_index = chunk()->GetParameterStackSlot(instr->index());
2142 return DefineAsSpilled(new(zone()) LParameter, spill_index);
2146 LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2147 int spill_index = chunk()->GetNextSpillIndex(false); // Not double-width.
2148 if (spill_index > LUnallocated::kMaxFixedIndex) {
2149 Abort("Too many spill slots needed for OSR");
2152 return DefineAsSpilled(new(zone()) LUnknownOSRValue, spill_index);
2156 LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2157 argument_count_ -= instr->argument_count();
2158 return MarkAsCall(DefineFixed(new(zone()) LCallStub, rax), instr);
2162 LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
2163 // There are no real uses of the arguments object.
2164 // arguments.length and element access are supported directly on
2165 // stack arguments, and any real arguments object use causes a bailout.
2166 // So this value is never used.
2171 LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2172 LOperand* arguments = UseRegister(instr->arguments());
2173 LOperand* length = UseTempRegister(instr->length());
2174 LOperand* index = Use(instr->index());
2175 LAccessArgumentsAt* result =
2176 new(zone()) LAccessArgumentsAt(arguments, length, index);
2177 return AssignEnvironment(DefineAsRegister(result));
2181 LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2182 LOperand* object = UseFixed(instr->value(), rax);
2183 LToFastProperties* result = new(zone()) LToFastProperties(object);
2184 return MarkAsCall(DefineFixed(result, rax), instr);
2188 LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2189 LTypeof* result = new(zone()) LTypeof(UseAtStart(instr->value()));
2190 return MarkAsCall(DefineFixed(result, rax), instr);
2194 LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2195 return new(zone()) LTypeofIsAndBranch(UseTempRegister(instr->value()));
2199 LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
2200 HIsConstructCallAndBranch* instr) {
2201 return new(zone()) LIsConstructCallAndBranch(TempRegister());
2205 LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2206 HEnvironment* env = current_block_->last_environment();
2207 ASSERT(env != NULL);
2209 env->set_ast_id(instr->ast_id());
2211 env->Drop(instr->pop_count());
2212 for (int i = 0; i < instr->values()->length(); ++i) {
2213 HValue* value = instr->values()->at(i);
2214 if (instr->HasAssignedIndexAt(i)) {
2215 env->Bind(instr->GetAssignedIndexAt(i), value);
2221 // If there is an instruction pending deoptimization environment create a
2222 // lazy bailout instruction to capture the environment.
2223 if (pending_deoptimization_ast_id_ == instr->ast_id()) {
2224 LLazyBailout* lazy_bailout = new(zone()) LLazyBailout;
2225 LInstruction* result = AssignEnvironment(lazy_bailout);
2226 // Store the lazy deopt environment with the instruction if needed. Right
2227 // now it is only used for LInstanceOfKnownGlobal.
2228 instruction_pending_deoptimization_environment_->
2229 SetDeferredLazyDeoptimizationEnvironment(result->environment());
2230 instruction_pending_deoptimization_environment_ = NULL;
2231 pending_deoptimization_ast_id_ = AstNode::kNoNumber;
2239 LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2240 if (instr->is_function_entry()) {
2241 return MarkAsCall(new(zone()) LStackCheck, instr);
2243 ASSERT(instr->is_backwards_branch());
2244 return AssignEnvironment(AssignPointerMap(new(zone()) LStackCheck));
2249 LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2250 HEnvironment* outer = current_block_->last_environment();
2251 HConstant* undefined = graph()->GetConstantUndefined();
2252 HEnvironment* inner = outer->CopyForInlining(instr->closure(),
2253 instr->arguments_count(),
2257 instr->is_construct());
2258 if (instr->arguments_var() != NULL) {
2259 inner->Bind(instr->arguments_var(), graph()->GetArgumentsObject());
2261 current_block_->UpdateEnvironment(inner);
2262 chunk_->AddInlinedClosure(instr->closure());
2267 LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2268 LInstruction* pop = NULL;
2270 HEnvironment* env = current_block_->last_environment();
2272 if (instr->arguments_pushed()) {
2273 int argument_count = env->arguments_environment()->parameter_count();
2274 pop = new(zone()) LDrop(argument_count);
2275 argument_count_ -= argument_count;
2278 HEnvironment* outer = current_block_->last_environment()->
2279 DiscardInlined(false);
2280 current_block_->UpdateEnvironment(outer);
2286 LInstruction* LChunkBuilder::DoIn(HIn* instr) {
2287 LOperand* key = UseOrConstantAtStart(instr->key());
2288 LOperand* object = UseOrConstantAtStart(instr->object());
2289 LIn* result = new(zone()) LIn(key, object);
2290 return MarkAsCall(DefineFixed(result, rax), instr);
2294 LInstruction* LChunkBuilder::DoForInPrepareMap(HForInPrepareMap* instr) {
2295 LOperand* object = UseFixed(instr->enumerable(), rax);
2296 LForInPrepareMap* result = new(zone()) LForInPrepareMap(object);
2297 return MarkAsCall(DefineFixed(result, rax), instr, CAN_DEOPTIMIZE_EAGERLY);
2301 LInstruction* LChunkBuilder::DoForInCacheArray(HForInCacheArray* instr) {
2302 LOperand* map = UseRegister(instr->map());
2303 return AssignEnvironment(DefineAsRegister(
2304 new(zone()) LForInCacheArray(map)));
2308 LInstruction* LChunkBuilder::DoCheckMapValue(HCheckMapValue* instr) {
2309 LOperand* value = UseRegisterAtStart(instr->value());
2310 LOperand* map = UseRegisterAtStart(instr->map());
2311 return AssignEnvironment(new(zone()) LCheckMapValue(value, map));
2315 LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) {
2316 LOperand* object = UseRegister(instr->object());
2317 LOperand* index = UseTempRegister(instr->index());
2318 return DefineSameAsFirst(new(zone()) LLoadFieldByIndex(object, index));
2322 } } // namespace v8::internal
2324 #endif // V8_TARGET_ARCH_X64