blob: 42470c53a058a84a23f57a9ad5379d4c981e4a28 [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <sstream>
6
7#include "src/v8.h"
8
9#include "src/hydrogen-osr.h"
10#include "src/lithium-inl.h"
11#include "src/ppc/lithium-codegen-ppc.h"
12
13namespace v8 {
14namespace internal {
15
16#define DEFINE_COMPILE(type) \
17 void L##type::CompileToNative(LCodeGen* generator) { \
18 generator->Do##type(this); \
19 }
20LITHIUM_CONCRETE_INSTRUCTION_LIST(DEFINE_COMPILE)
21#undef DEFINE_COMPILE
22
23#ifdef DEBUG
24void LInstruction::VerifyCall() {
25 // Call instructions can use only fixed registers as temporaries and
26 // outputs because all registers are blocked by the calling convention.
27 // Inputs operands must use a fixed register or use-at-start policy or
28 // a non-register policy.
29 DCHECK(Output() == NULL || LUnallocated::cast(Output())->HasFixedPolicy() ||
30 !LUnallocated::cast(Output())->HasRegisterPolicy());
31 for (UseIterator it(this); !it.Done(); it.Advance()) {
32 LUnallocated* operand = LUnallocated::cast(it.Current());
33 DCHECK(operand->HasFixedPolicy() || operand->IsUsedAtStart());
34 }
35 for (TempIterator it(this); !it.Done(); it.Advance()) {
36 LUnallocated* operand = LUnallocated::cast(it.Current());
37 DCHECK(operand->HasFixedPolicy() || !operand->HasRegisterPolicy());
38 }
39}
40#endif
41
42
43void LInstruction::PrintTo(StringStream* stream) {
44 stream->Add("%s ", this->Mnemonic());
45
46 PrintOutputOperandTo(stream);
47
48 PrintDataTo(stream);
49
50 if (HasEnvironment()) {
51 stream->Add(" ");
52 environment()->PrintTo(stream);
53 }
54
55 if (HasPointerMap()) {
56 stream->Add(" ");
57 pointer_map()->PrintTo(stream);
58 }
59}
60
61
62void LInstruction::PrintDataTo(StringStream* stream) {
63 stream->Add("= ");
64 for (int i = 0; i < InputCount(); i++) {
65 if (i > 0) stream->Add(" ");
66 if (InputAt(i) == NULL) {
67 stream->Add("NULL");
68 } else {
69 InputAt(i)->PrintTo(stream);
70 }
71 }
72}
73
74
75void LInstruction::PrintOutputOperandTo(StringStream* stream) {
76 if (HasResult()) result()->PrintTo(stream);
77}
78
79
80void LLabel::PrintDataTo(StringStream* stream) {
81 LGap::PrintDataTo(stream);
82 LLabel* rep = replacement();
83 if (rep != NULL) {
84 stream->Add(" Dead block replaced with B%d", rep->block_id());
85 }
86}
87
88
89bool LGap::IsRedundant() const {
90 for (int i = 0; i < 4; i++) {
91 if (parallel_moves_[i] != NULL && !parallel_moves_[i]->IsRedundant()) {
92 return false;
93 }
94 }
95
96 return true;
97}
98
99
100void LGap::PrintDataTo(StringStream* stream) {
101 for (int i = 0; i < 4; i++) {
102 stream->Add("(");
103 if (parallel_moves_[i] != NULL) {
104 parallel_moves_[i]->PrintDataTo(stream);
105 }
106 stream->Add(") ");
107 }
108}
109
110
111const char* LArithmeticD::Mnemonic() const {
112 switch (op()) {
113 case Token::ADD:
114 return "add-d";
115 case Token::SUB:
116 return "sub-d";
117 case Token::MUL:
118 return "mul-d";
119 case Token::DIV:
120 return "div-d";
121 case Token::MOD:
122 return "mod-d";
123 default:
124 UNREACHABLE();
125 return NULL;
126 }
127}
128
129
130const char* LArithmeticT::Mnemonic() const {
131 switch (op()) {
132 case Token::ADD:
133 return "add-t";
134 case Token::SUB:
135 return "sub-t";
136 case Token::MUL:
137 return "mul-t";
138 case Token::MOD:
139 return "mod-t";
140 case Token::DIV:
141 return "div-t";
142 case Token::BIT_AND:
143 return "bit-and-t";
144 case Token::BIT_OR:
145 return "bit-or-t";
146 case Token::BIT_XOR:
147 return "bit-xor-t";
148 case Token::ROR:
149 return "ror-t";
150 case Token::SHL:
151 return "shl-t";
152 case Token::SAR:
153 return "sar-t";
154 case Token::SHR:
155 return "shr-t";
156 default:
157 UNREACHABLE();
158 return NULL;
159 }
160}
161
162
163bool LGoto::HasInterestingComment(LCodeGen* gen) const {
164 return !gen->IsNextEmittedBlock(block_id());
165}
166
167
168void LGoto::PrintDataTo(StringStream* stream) {
169 stream->Add("B%d", block_id());
170}
171
172
173void LBranch::PrintDataTo(StringStream* stream) {
174 stream->Add("B%d | B%d on ", true_block_id(), false_block_id());
175 value()->PrintTo(stream);
176}
177
178
179void LCompareNumericAndBranch::PrintDataTo(StringStream* stream) {
180 stream->Add("if ");
181 left()->PrintTo(stream);
182 stream->Add(" %s ", Token::String(op()));
183 right()->PrintTo(stream);
184 stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
185}
186
187
188void LIsObjectAndBranch::PrintDataTo(StringStream* stream) {
189 stream->Add("if is_object(");
190 value()->PrintTo(stream);
191 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
192}
193
194
195void LIsStringAndBranch::PrintDataTo(StringStream* stream) {
196 stream->Add("if is_string(");
197 value()->PrintTo(stream);
198 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
199}
200
201
202void LIsSmiAndBranch::PrintDataTo(StringStream* stream) {
203 stream->Add("if is_smi(");
204 value()->PrintTo(stream);
205 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
206}
207
208
209void LIsUndetectableAndBranch::PrintDataTo(StringStream* stream) {
210 stream->Add("if is_undetectable(");
211 value()->PrintTo(stream);
212 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
213}
214
215
216void LStringCompareAndBranch::PrintDataTo(StringStream* stream) {
217 stream->Add("if string_compare(");
218 left()->PrintTo(stream);
219 right()->PrintTo(stream);
220 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
221}
222
223
224void LHasInstanceTypeAndBranch::PrintDataTo(StringStream* stream) {
225 stream->Add("if has_instance_type(");
226 value()->PrintTo(stream);
227 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
228}
229
230
231void LHasCachedArrayIndexAndBranch::PrintDataTo(StringStream* stream) {
232 stream->Add("if has_cached_array_index(");
233 value()->PrintTo(stream);
234 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
235}
236
237
238void LClassOfTestAndBranch::PrintDataTo(StringStream* stream) {
239 stream->Add("if class_of_test(");
240 value()->PrintTo(stream);
241 stream->Add(", \"%o\") then B%d else B%d", *hydrogen()->class_name(),
242 true_block_id(), false_block_id());
243}
244
245
246void LTypeofIsAndBranch::PrintDataTo(StringStream* stream) {
247 stream->Add("if typeof ");
248 value()->PrintTo(stream);
249 stream->Add(" == \"%s\" then B%d else B%d",
250 hydrogen()->type_literal()->ToCString().get(), true_block_id(),
251 false_block_id());
252}
253
254
255void LStoreCodeEntry::PrintDataTo(StringStream* stream) {
256 stream->Add(" = ");
257 function()->PrintTo(stream);
258 stream->Add(".code_entry = ");
259 code_object()->PrintTo(stream);
260}
261
262
263void LInnerAllocatedObject::PrintDataTo(StringStream* stream) {
264 stream->Add(" = ");
265 base_object()->PrintTo(stream);
266 stream->Add(" + ");
267 offset()->PrintTo(stream);
268}
269
270
271void LCallJSFunction::PrintDataTo(StringStream* stream) {
272 stream->Add("= ");
273 function()->PrintTo(stream);
274 stream->Add("#%d / ", arity());
275}
276
277
278void LCallWithDescriptor::PrintDataTo(StringStream* stream) {
279 for (int i = 0; i < InputCount(); i++) {
280 InputAt(i)->PrintTo(stream);
281 stream->Add(" ");
282 }
283 stream->Add("#%d / ", arity());
284}
285
286
287void LLoadContextSlot::PrintDataTo(StringStream* stream) {
288 context()->PrintTo(stream);
289 stream->Add("[%d]", slot_index());
290}
291
292
293void LStoreContextSlot::PrintDataTo(StringStream* stream) {
294 context()->PrintTo(stream);
295 stream->Add("[%d] <- ", slot_index());
296 value()->PrintTo(stream);
297}
298
299
300void LInvokeFunction::PrintDataTo(StringStream* stream) {
301 stream->Add("= ");
302 function()->PrintTo(stream);
303 stream->Add(" #%d / ", arity());
304}
305
306
307void LCallNew::PrintDataTo(StringStream* stream) {
308 stream->Add("= ");
309 constructor()->PrintTo(stream);
310 stream->Add(" #%d / ", arity());
311}
312
313
314void LCallNewArray::PrintDataTo(StringStream* stream) {
315 stream->Add("= ");
316 constructor()->PrintTo(stream);
317 stream->Add(" #%d / ", arity());
318 ElementsKind kind = hydrogen()->elements_kind();
319 stream->Add(" (%s) ", ElementsKindToString(kind));
320}
321
322
323void LAccessArgumentsAt::PrintDataTo(StringStream* stream) {
324 arguments()->PrintTo(stream);
325 stream->Add(" length ");
326 length()->PrintTo(stream);
327 stream->Add(" index ");
328 index()->PrintTo(stream);
329}
330
331
332void LStoreNamedField::PrintDataTo(StringStream* stream) {
333 object()->PrintTo(stream);
334 std::ostringstream os;
335 os << hydrogen()->access() << " <- ";
336 stream->Add(os.str().c_str());
337 value()->PrintTo(stream);
338}
339
340
341void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
342 object()->PrintTo(stream);
343 stream->Add(".");
344 stream->Add(String::cast(*name())->ToCString().get());
345 stream->Add(" <- ");
346 value()->PrintTo(stream);
347}
348
349
350void LLoadKeyed::PrintDataTo(StringStream* stream) {
351 elements()->PrintTo(stream);
352 stream->Add("[");
353 key()->PrintTo(stream);
354 if (hydrogen()->IsDehoisted()) {
355 stream->Add(" + %d]", base_offset());
356 } else {
357 stream->Add("]");
358 }
359}
360
361
362void LStoreKeyed::PrintDataTo(StringStream* stream) {
363 elements()->PrintTo(stream);
364 stream->Add("[");
365 key()->PrintTo(stream);
366 if (hydrogen()->IsDehoisted()) {
367 stream->Add(" + %d] <-", base_offset());
368 } else {
369 stream->Add("] <- ");
370 }
371
372 if (value() == NULL) {
373 DCHECK(hydrogen()->IsConstantHoleStore() &&
374 hydrogen()->value()->representation().IsDouble());
375 stream->Add("<the hole(nan)>");
376 } else {
377 value()->PrintTo(stream);
378 }
379}
380
381
382void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
383 object()->PrintTo(stream);
384 stream->Add("[");
385 key()->PrintTo(stream);
386 stream->Add("] <- ");
387 value()->PrintTo(stream);
388}
389
390
391void LTransitionElementsKind::PrintDataTo(StringStream* stream) {
392 object()->PrintTo(stream);
393 stream->Add(" %p -> %p", *original_map(), *transitioned_map());
394}
395
396
397int LPlatformChunk::GetNextSpillIndex(RegisterKind kind) {
398 // Skip a slot if for a double-width slot.
399 if (kind == DOUBLE_REGISTERS) spill_slot_count_++;
400 return spill_slot_count_++;
401}
402
403
404LOperand* LPlatformChunk::GetNextSpillSlot(RegisterKind kind) {
405 int index = GetNextSpillIndex(kind);
406 if (kind == DOUBLE_REGISTERS) {
407 return LDoubleStackSlot::Create(index, zone());
408 } else {
409 DCHECK(kind == GENERAL_REGISTERS);
410 return LStackSlot::Create(index, zone());
411 }
412}
413
414
415LPlatformChunk* LChunkBuilder::Build() {
416 DCHECK(is_unused());
417 chunk_ = new (zone()) LPlatformChunk(info(), graph());
418 LPhase phase("L_Building chunk", chunk_);
419 status_ = BUILDING;
420
421 // If compiling for OSR, reserve space for the unoptimized frame,
422 // which will be subsumed into this frame.
423 if (graph()->has_osr()) {
424 for (int i = graph()->osr()->UnoptimizedFrameSlots(); i > 0; i--) {
425 chunk_->GetNextSpillIndex(GENERAL_REGISTERS);
426 }
427 }
428
429 const ZoneList<HBasicBlock*>* blocks = graph()->blocks();
430 for (int i = 0; i < blocks->length(); i++) {
431 HBasicBlock* next = NULL;
432 if (i < blocks->length() - 1) next = blocks->at(i + 1);
433 DoBasicBlock(blocks->at(i), next);
434 if (is_aborted()) return NULL;
435 }
436 status_ = DONE;
437 return chunk_;
438}
439
440
441LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
442 return new (zone()) LUnallocated(LUnallocated::FIXED_REGISTER,
443 Register::ToAllocationIndex(reg));
444}
445
446
447LUnallocated* LChunkBuilder::ToUnallocated(DoubleRegister reg) {
448 return new (zone()) LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
449 DoubleRegister::ToAllocationIndex(reg));
450}
451
452
453LOperand* LChunkBuilder::UseFixed(HValue* value, Register fixed_register) {
454 return Use(value, ToUnallocated(fixed_register));
455}
456
457
458LOperand* LChunkBuilder::UseFixedDouble(HValue* value, DoubleRegister reg) {
459 return Use(value, ToUnallocated(reg));
460}
461
462
463LOperand* LChunkBuilder::UseRegister(HValue* value) {
464 return Use(value,
465 new (zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
466}
467
468
469LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
470 return Use(value, new (zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
471 LUnallocated::USED_AT_START));
472}
473
474
475LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
476 return Use(value, new (zone()) LUnallocated(LUnallocated::WRITABLE_REGISTER));
477}
478
479
480LOperand* LChunkBuilder::Use(HValue* value) {
481 return Use(value, new (zone()) LUnallocated(LUnallocated::NONE));
482}
483
484
485LOperand* LChunkBuilder::UseAtStart(HValue* value) {
486 return Use(value, new (zone())
487 LUnallocated(LUnallocated::NONE, LUnallocated::USED_AT_START));
488}
489
490
491LOperand* LChunkBuilder::UseOrConstant(HValue* value) {
492 return value->IsConstant()
493 ? chunk_->DefineConstantOperand(HConstant::cast(value))
494 : Use(value);
495}
496
497
498LOperand* LChunkBuilder::UseOrConstantAtStart(HValue* value) {
499 return value->IsConstant()
500 ? chunk_->DefineConstantOperand(HConstant::cast(value))
501 : UseAtStart(value);
502}
503
504
505LOperand* LChunkBuilder::UseRegisterOrConstant(HValue* value) {
506 return value->IsConstant()
507 ? chunk_->DefineConstantOperand(HConstant::cast(value))
508 : UseRegister(value);
509}
510
511
512LOperand* LChunkBuilder::UseRegisterOrConstantAtStart(HValue* value) {
513 return value->IsConstant()
514 ? chunk_->DefineConstantOperand(HConstant::cast(value))
515 : UseRegisterAtStart(value);
516}
517
518
519LOperand* LChunkBuilder::UseConstant(HValue* value) {
520 return chunk_->DefineConstantOperand(HConstant::cast(value));
521}
522
523
524LOperand* LChunkBuilder::UseAny(HValue* value) {
525 return value->IsConstant()
526 ? chunk_->DefineConstantOperand(HConstant::cast(value))
527 : Use(value, new (zone()) LUnallocated(LUnallocated::ANY));
528}
529
530
531LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
532 if (value->EmitAtUses()) {
533 HInstruction* instr = HInstruction::cast(value);
534 VisitInstruction(instr);
535 }
536 operand->set_virtual_register(value->id());
537 return operand;
538}
539
540
541LInstruction* LChunkBuilder::Define(LTemplateResultInstruction<1>* instr,
542 LUnallocated* result) {
543 result->set_virtual_register(current_instruction_->id());
544 instr->set_result(result);
545 return instr;
546}
547
548
549LInstruction* LChunkBuilder::DefineAsRegister(
550 LTemplateResultInstruction<1>* instr) {
551 return Define(instr,
552 new (zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
553}
554
555
556LInstruction* LChunkBuilder::DefineAsSpilled(
557 LTemplateResultInstruction<1>* instr, int index) {
558 return Define(instr,
559 new (zone()) LUnallocated(LUnallocated::FIXED_SLOT, index));
560}
561
562
563LInstruction* LChunkBuilder::DefineSameAsFirst(
564 LTemplateResultInstruction<1>* instr) {
565 return Define(instr,
566 new (zone()) LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
567}
568
569
570LInstruction* LChunkBuilder::DefineFixed(LTemplateResultInstruction<1>* instr,
571 Register reg) {
572 return Define(instr, ToUnallocated(reg));
573}
574
575
576LInstruction* LChunkBuilder::DefineFixedDouble(
577 LTemplateResultInstruction<1>* instr, DoubleRegister reg) {
578 return Define(instr, ToUnallocated(reg));
579}
580
581
582LInstruction* LChunkBuilder::AssignEnvironment(LInstruction* instr) {
583 HEnvironment* hydrogen_env = current_block_->last_environment();
584 int argument_index_accumulator = 0;
585 ZoneList<HValue*> objects_to_materialize(0, zone());
586 instr->set_environment(CreateEnvironment(
587 hydrogen_env, &argument_index_accumulator, &objects_to_materialize));
588 return instr;
589}
590
591
592LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
593 HInstruction* hinstr,
594 CanDeoptimize can_deoptimize) {
595 info()->MarkAsNonDeferredCalling();
596#ifdef DEBUG
597 instr->VerifyCall();
598#endif
599 instr->MarkAsCall();
600 instr = AssignPointerMap(instr);
601
602 // If instruction does not have side-effects lazy deoptimization
603 // after the call will try to deoptimize to the point before the call.
604 // Thus we still need to attach environment to this call even if
605 // call sequence can not deoptimize eagerly.
606 bool needs_environment = (can_deoptimize == CAN_DEOPTIMIZE_EAGERLY) ||
607 !hinstr->HasObservableSideEffects();
608 if (needs_environment && !instr->HasEnvironment()) {
609 instr = AssignEnvironment(instr);
610 // We can't really figure out if the environment is needed or not.
611 instr->environment()->set_has_been_used();
612 }
613
614 return instr;
615}
616
617
618LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
619 DCHECK(!instr->HasPointerMap());
620 instr->set_pointer_map(new (zone()) LPointerMap(zone()));
621 return instr;
622}
623
624
625LUnallocated* LChunkBuilder::TempRegister() {
626 LUnallocated* operand =
627 new (zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
628 int vreg = allocator_->GetVirtualRegister();
629 if (!allocator_->AllocationOk()) {
630 Abort(kOutOfVirtualRegistersWhileTryingToAllocateTempRegister);
631 vreg = 0;
632 }
633 operand->set_virtual_register(vreg);
634 return operand;
635}
636
637
638LUnallocated* LChunkBuilder::TempDoubleRegister() {
639 LUnallocated* operand =
640 new (zone()) LUnallocated(LUnallocated::MUST_HAVE_DOUBLE_REGISTER);
641 int vreg = allocator_->GetVirtualRegister();
642 if (!allocator_->AllocationOk()) {
643 Abort(kOutOfVirtualRegistersWhileTryingToAllocateTempRegister);
644 vreg = 0;
645 }
646 operand->set_virtual_register(vreg);
647 return operand;
648}
649
650
651LOperand* LChunkBuilder::FixedTemp(Register reg) {
652 LUnallocated* operand = ToUnallocated(reg);
653 DCHECK(operand->HasFixedPolicy());
654 return operand;
655}
656
657
658LOperand* LChunkBuilder::FixedTemp(DoubleRegister reg) {
659 LUnallocated* operand = ToUnallocated(reg);
660 DCHECK(operand->HasFixedPolicy());
661 return operand;
662}
663
664
665LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
666 return new (zone()) LLabel(instr->block());
667}
668
669
670LInstruction* LChunkBuilder::DoDummyUse(HDummyUse* instr) {
671 return DefineAsRegister(new (zone()) LDummyUse(UseAny(instr->value())));
672}
673
674
675LInstruction* LChunkBuilder::DoEnvironmentMarker(HEnvironmentMarker* instr) {
676 UNREACHABLE();
677 return NULL;
678}
679
680
681LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
682 return AssignEnvironment(new (zone()) LDeoptimize);
683}
684
685
686LInstruction* LChunkBuilder::DoShift(Token::Value op,
687 HBitwiseBinaryOperation* instr) {
688 if (instr->representation().IsSmiOrInteger32()) {
689 DCHECK(instr->left()->representation().Equals(instr->representation()));
690 DCHECK(instr->right()->representation().Equals(instr->representation()));
691 LOperand* left = UseRegisterAtStart(instr->left());
692
693 HValue* right_value = instr->right();
694 LOperand* right = NULL;
695 int constant_value = 0;
696 bool does_deopt = false;
697 if (right_value->IsConstant()) {
698 HConstant* constant = HConstant::cast(right_value);
699 right = chunk_->DefineConstantOperand(constant);
700 constant_value = constant->Integer32Value() & 0x1f;
701 // Left shifts can deoptimize if we shift by > 0 and the result cannot be
702 // truncated to smi.
703 if (instr->representation().IsSmi() && constant_value > 0) {
704 does_deopt = !instr->CheckUsesForFlag(HValue::kTruncatingToSmi);
705 }
706 } else {
707 right = UseRegisterAtStart(right_value);
708 }
709
710 // Shift operations can only deoptimize if we do a logical shift
711 // by 0 and the result cannot be truncated to int32.
712 if (op == Token::SHR && constant_value == 0) {
713 does_deopt = !instr->CheckFlag(HInstruction::kUint32);
714 }
715
716 LInstruction* result =
717 DefineAsRegister(new (zone()) LShiftI(op, left, right, does_deopt));
718 return does_deopt ? AssignEnvironment(result) : result;
719 } else {
720 return DoArithmeticT(op, instr);
721 }
722}
723
724
725LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
726 HArithmeticBinaryOperation* instr) {
727 DCHECK(instr->representation().IsDouble());
728 DCHECK(instr->left()->representation().IsDouble());
729 DCHECK(instr->right()->representation().IsDouble());
730 if (op == Token::MOD) {
731 LOperand* left = UseFixedDouble(instr->left(), d1);
732 LOperand* right = UseFixedDouble(instr->right(), d2);
733 LArithmeticD* result = new (zone()) LArithmeticD(op, left, right);
734 // We call a C function for double modulo. It can't trigger a GC. We need
735 // to use fixed result register for the call.
736 // TODO(fschneider): Allow any register as input registers.
737 return MarkAsCall(DefineFixedDouble(result, d1), instr);
738 } else {
739 LOperand* left = UseRegisterAtStart(instr->left());
740 LOperand* right = UseRegisterAtStart(instr->right());
741 LArithmeticD* result = new (zone()) LArithmeticD(op, left, right);
742 return DefineAsRegister(result);
743 }
744}
745
746
747LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
748 HBinaryOperation* instr) {
749 HValue* left = instr->left();
750 HValue* right = instr->right();
751 DCHECK(left->representation().IsTagged());
752 DCHECK(right->representation().IsTagged());
753 LOperand* context = UseFixed(instr->context(), cp);
754 LOperand* left_operand = UseFixed(left, r4);
755 LOperand* right_operand = UseFixed(right, r3);
756 LArithmeticT* result =
757 new (zone()) LArithmeticT(op, context, left_operand, right_operand);
758 return MarkAsCall(DefineFixed(result, r3), instr);
759}
760
761
762void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
763 DCHECK(is_building());
764 current_block_ = block;
765 next_block_ = next_block;
766 if (block->IsStartBlock()) {
767 block->UpdateEnvironment(graph_->start_environment());
768 argument_count_ = 0;
769 } else if (block->predecessors()->length() == 1) {
770 // We have a single predecessor => copy environment and outgoing
771 // argument count from the predecessor.
772 DCHECK(block->phis()->length() == 0);
773 HBasicBlock* pred = block->predecessors()->at(0);
774 HEnvironment* last_environment = pred->last_environment();
775 DCHECK(last_environment != NULL);
776 // Only copy the environment, if it is later used again.
777 if (pred->end()->SecondSuccessor() == NULL) {
778 DCHECK(pred->end()->FirstSuccessor() == block);
779 } else {
780 if (pred->end()->FirstSuccessor()->block_id() > block->block_id() ||
781 pred->end()->SecondSuccessor()->block_id() > block->block_id()) {
782 last_environment = last_environment->Copy();
783 }
784 }
785 block->UpdateEnvironment(last_environment);
786 DCHECK(pred->argument_count() >= 0);
787 argument_count_ = pred->argument_count();
788 } else {
789 // We are at a state join => process phis.
790 HBasicBlock* pred = block->predecessors()->at(0);
791 // No need to copy the environment, it cannot be used later.
792 HEnvironment* last_environment = pred->last_environment();
793 for (int i = 0; i < block->phis()->length(); ++i) {
794 HPhi* phi = block->phis()->at(i);
795 if (phi->HasMergedIndex()) {
796 last_environment->SetValueAt(phi->merged_index(), phi);
797 }
798 }
799 for (int i = 0; i < block->deleted_phis()->length(); ++i) {
800 if (block->deleted_phis()->at(i) < last_environment->length()) {
801 last_environment->SetValueAt(block->deleted_phis()->at(i),
802 graph_->GetConstantUndefined());
803 }
804 }
805 block->UpdateEnvironment(last_environment);
806 // Pick up the outgoing argument count of one of the predecessors.
807 argument_count_ = pred->argument_count();
808 }
809 HInstruction* current = block->first();
810 int start = chunk_->instructions()->length();
811 while (current != NULL && !is_aborted()) {
812 // Code for constants in registers is generated lazily.
813 if (!current->EmitAtUses()) {
814 VisitInstruction(current);
815 }
816 current = current->next();
817 }
818 int end = chunk_->instructions()->length() - 1;
819 if (end >= start) {
820 block->set_first_instruction_index(start);
821 block->set_last_instruction_index(end);
822 }
823 block->set_argument_count(argument_count_);
824 next_block_ = NULL;
825 current_block_ = NULL;
826}
827
828
829void LChunkBuilder::VisitInstruction(HInstruction* current) {
830 HInstruction* old_current = current_instruction_;
831 current_instruction_ = current;
832
833 LInstruction* instr = NULL;
834 if (current->CanReplaceWithDummyUses()) {
835 if (current->OperandCount() == 0) {
836 instr = DefineAsRegister(new (zone()) LDummy());
837 } else {
838 DCHECK(!current->OperandAt(0)->IsControlInstruction());
839 instr = DefineAsRegister(new (zone())
840 LDummyUse(UseAny(current->OperandAt(0))));
841 }
842 for (int i = 1; i < current->OperandCount(); ++i) {
843 if (current->OperandAt(i)->IsControlInstruction()) continue;
844 LInstruction* dummy =
845 new (zone()) LDummyUse(UseAny(current->OperandAt(i)));
846 dummy->set_hydrogen_value(current);
847 chunk_->AddInstruction(dummy, current_block_);
848 }
849 } else {
850 HBasicBlock* successor;
851 if (current->IsControlInstruction() &&
852 HControlInstruction::cast(current)->KnownSuccessorBlock(&successor) &&
853 successor != NULL) {
854 instr = new (zone()) LGoto(successor);
855 } else {
856 instr = current->CompileToLithium(this);
857 }
858 }
859
860 argument_count_ += current->argument_delta();
861 DCHECK(argument_count_ >= 0);
862
863 if (instr != NULL) {
864 AddInstruction(instr, current);
865 }
866
867 current_instruction_ = old_current;
868}
869
870
871void LChunkBuilder::AddInstruction(LInstruction* instr,
872 HInstruction* hydrogen_val) {
873 // Associate the hydrogen instruction first, since we may need it for
874 // the ClobbersRegisters() or ClobbersDoubleRegisters() calls below.
875 instr->set_hydrogen_value(hydrogen_val);
876
877#if DEBUG
878 // Make sure that the lithium instruction has either no fixed register
879 // constraints in temps or the result OR no uses that are only used at
880 // start. If this invariant doesn't hold, the register allocator can decide
881 // to insert a split of a range immediately before the instruction due to an
882 // already allocated register needing to be used for the instruction's fixed
883 // register constraint. In this case, The register allocator won't see an
884 // interference between the split child and the use-at-start (it would if
885 // the it was just a plain use), so it is free to move the split child into
886 // the same register that is used for the use-at-start.
887 // See https://code.google.com/p/chromium/issues/detail?id=201590
888 if (!(instr->ClobbersRegisters() &&
889 instr->ClobbersDoubleRegisters(isolate()))) {
890 int fixed = 0;
891 int used_at_start = 0;
892 for (UseIterator it(instr); !it.Done(); it.Advance()) {
893 LUnallocated* operand = LUnallocated::cast(it.Current());
894 if (operand->IsUsedAtStart()) ++used_at_start;
895 }
896 if (instr->Output() != NULL) {
897 if (LUnallocated::cast(instr->Output())->HasFixedPolicy()) ++fixed;
898 }
899 for (TempIterator it(instr); !it.Done(); it.Advance()) {
900 LUnallocated* operand = LUnallocated::cast(it.Current());
901 if (operand->HasFixedPolicy()) ++fixed;
902 }
903 DCHECK(fixed == 0 || used_at_start == 0);
904 }
905#endif
906
907 if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
908 instr = AssignPointerMap(instr);
909 }
910 if (FLAG_stress_environments && !instr->HasEnvironment()) {
911 instr = AssignEnvironment(instr);
912 }
913 chunk_->AddInstruction(instr, current_block_);
914
915 if (instr->IsCall()) {
916 HValue* hydrogen_value_for_lazy_bailout = hydrogen_val;
917 LInstruction* instruction_needing_environment = NULL;
918 if (hydrogen_val->HasObservableSideEffects()) {
919 HSimulate* sim = HSimulate::cast(hydrogen_val->next());
920 instruction_needing_environment = instr;
921 sim->ReplayEnvironment(current_block_->last_environment());
922 hydrogen_value_for_lazy_bailout = sim;
923 }
924 LInstruction* bailout = AssignEnvironment(new (zone()) LLazyBailout());
925 bailout->set_hydrogen_value(hydrogen_value_for_lazy_bailout);
926 chunk_->AddInstruction(bailout, current_block_);
927 if (instruction_needing_environment != NULL) {
928 // Store the lazy deopt environment with the instruction if needed.
929 // Right now it is only used for LInstanceOfKnownGlobal.
930 instruction_needing_environment->SetDeferredLazyDeoptimizationEnvironment(
931 bailout->environment());
932 }
933 }
934}
935
936
937LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
938 return new (zone()) LGoto(instr->FirstSuccessor());
939}
940
941
942LInstruction* LChunkBuilder::DoBranch(HBranch* instr) {
943 HValue* value = instr->value();
944 Representation r = value->representation();
945 HType type = value->type();
946 ToBooleanStub::Types expected = instr->expected_input_types();
947 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
948
949 bool easy_case = !r.IsTagged() || type.IsBoolean() || type.IsSmi() ||
950 type.IsJSArray() || type.IsHeapNumber() || type.IsString();
951 LInstruction* branch = new (zone()) LBranch(UseRegister(value));
952 if (!easy_case &&
953 ((!expected.Contains(ToBooleanStub::SMI) && expected.NeedsMap()) ||
954 !expected.IsGeneric())) {
955 branch = AssignEnvironment(branch);
956 }
957 return branch;
958}
959
960
961LInstruction* LChunkBuilder::DoDebugBreak(HDebugBreak* instr) {
962 return new (zone()) LDebugBreak();
963}
964
965
966LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
967 DCHECK(instr->value()->representation().IsTagged());
968 LOperand* value = UseRegisterAtStart(instr->value());
969 LOperand* temp = TempRegister();
970 return new (zone()) LCmpMapAndBranch(value, temp);
971}
972
973
974LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* instr) {
975 info()->MarkAsRequiresFrame();
976 LOperand* value = UseRegister(instr->value());
977 return DefineAsRegister(new (zone()) LArgumentsLength(value));
978}
979
980
981LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
982 info()->MarkAsRequiresFrame();
983 return DefineAsRegister(new (zone()) LArgumentsElements);
984}
985
986
987LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
988 LOperand* context = UseFixed(instr->context(), cp);
989 LInstanceOf* result = new (zone()) LInstanceOf(
990 context, UseFixed(instr->left(), r3), UseFixed(instr->right(), r4));
991 return MarkAsCall(DefineFixed(result, r3), instr);
992}
993
994
995LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
996 HInstanceOfKnownGlobal* instr) {
997 LInstanceOfKnownGlobal* result = new (zone())
998 LInstanceOfKnownGlobal(UseFixed(instr->context(), cp),
999 UseFixed(instr->left(), r3), FixedTemp(r7));
1000 return MarkAsCall(DefineFixed(result, r3), instr);
1001}
1002
1003
1004LInstruction* LChunkBuilder::DoWrapReceiver(HWrapReceiver* instr) {
1005 LOperand* receiver = UseRegisterAtStart(instr->receiver());
1006 LOperand* function = UseRegisterAtStart(instr->function());
1007 LWrapReceiver* result = new (zone()) LWrapReceiver(receiver, function);
1008 return AssignEnvironment(DefineAsRegister(result));
1009}
1010
1011
1012LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
1013 LOperand* function = UseFixed(instr->function(), r4);
1014 LOperand* receiver = UseFixed(instr->receiver(), r3);
1015 LOperand* length = UseFixed(instr->length(), r5);
1016 LOperand* elements = UseFixed(instr->elements(), r6);
1017 LApplyArguments* result =
1018 new (zone()) LApplyArguments(function, receiver, length, elements);
1019 return MarkAsCall(DefineFixed(result, r3), instr, CAN_DEOPTIMIZE_EAGERLY);
1020}
1021
1022
1023LInstruction* LChunkBuilder::DoPushArguments(HPushArguments* instr) {
1024 int argc = instr->OperandCount();
1025 for (int i = 0; i < argc; ++i) {
1026 LOperand* argument = Use(instr->argument(i));
1027 AddInstruction(new (zone()) LPushArgument(argument), instr);
1028 }
1029 return NULL;
1030}
1031
1032
1033LInstruction* LChunkBuilder::DoStoreCodeEntry(
1034 HStoreCodeEntry* store_code_entry) {
1035 LOperand* function = UseRegister(store_code_entry->function());
1036 LOperand* code_object = UseTempRegister(store_code_entry->code_object());
1037 return new (zone()) LStoreCodeEntry(function, code_object);
1038}
1039
1040
1041LInstruction* LChunkBuilder::DoInnerAllocatedObject(
1042 HInnerAllocatedObject* instr) {
1043 LOperand* base_object = UseRegisterAtStart(instr->base_object());
1044 LOperand* offset = UseRegisterOrConstantAtStart(instr->offset());
1045 return DefineAsRegister(new (zone())
1046 LInnerAllocatedObject(base_object, offset));
1047}
1048
1049
1050LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1051 return instr->HasNoUses() ? NULL
1052 : DefineAsRegister(new (zone()) LThisFunction);
1053}
1054
1055
1056LInstruction* LChunkBuilder::DoContext(HContext* instr) {
1057 if (instr->HasNoUses()) return NULL;
1058
1059 if (info()->IsStub()) {
1060 return DefineFixed(new (zone()) LContext, cp);
1061 }
1062
1063 return DefineAsRegister(new (zone()) LContext);
1064}
1065
1066
1067LInstruction* LChunkBuilder::DoDeclareGlobals(HDeclareGlobals* instr) {
1068 LOperand* context = UseFixed(instr->context(), cp);
1069 return MarkAsCall(new (zone()) LDeclareGlobals(context), instr);
1070}
1071
1072
1073LInstruction* LChunkBuilder::DoCallJSFunction(HCallJSFunction* instr) {
1074 LOperand* function = UseFixed(instr->function(), r4);
1075
1076 LCallJSFunction* result = new (zone()) LCallJSFunction(function);
1077
1078 return MarkAsCall(DefineFixed(result, r3), instr);
1079}
1080
1081
1082LInstruction* LChunkBuilder::DoCallWithDescriptor(HCallWithDescriptor* instr) {
1083 CallInterfaceDescriptor descriptor = instr->descriptor();
1084
1085 LOperand* target = UseRegisterOrConstantAtStart(instr->target());
1086 ZoneList<LOperand*> ops(instr->OperandCount(), zone());
1087 ops.Add(target, zone());
1088 for (int i = 1; i < instr->OperandCount(); i++) {
1089 LOperand* op =
1090 UseFixed(instr->OperandAt(i), descriptor.GetParameterRegister(i - 1));
1091 ops.Add(op, zone());
1092 }
1093
1094 LCallWithDescriptor* result =
1095 new (zone()) LCallWithDescriptor(descriptor, ops, zone());
1096 return MarkAsCall(DefineFixed(result, r3), instr);
1097}
1098
1099
1100LInstruction* LChunkBuilder::DoTailCallThroughMegamorphicCache(
1101 HTailCallThroughMegamorphicCache* instr) {
1102 LOperand* context = UseFixed(instr->context(), cp);
1103 LOperand* receiver_register =
1104 UseFixed(instr->receiver(), LoadDescriptor::ReceiverRegister());
1105 LOperand* name_register =
1106 UseFixed(instr->name(), LoadDescriptor::NameRegister());
1107 // Not marked as call. It can't deoptimize, and it never returns.
1108 return new (zone()) LTailCallThroughMegamorphicCache(
1109 context, receiver_register, name_register);
1110}
1111
1112
1113LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1114 LOperand* context = UseFixed(instr->context(), cp);
1115 LOperand* function = UseFixed(instr->function(), r4);
1116 LInvokeFunction* result = new (zone()) LInvokeFunction(context, function);
1117 return MarkAsCall(DefineFixed(result, r3), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1118}
1119
1120
1121LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
1122 switch (instr->op()) {
1123 case kMathFloor:
1124 return DoMathFloor(instr);
1125 case kMathRound:
1126 return DoMathRound(instr);
1127 case kMathFround:
1128 return DoMathFround(instr);
1129 case kMathAbs:
1130 return DoMathAbs(instr);
1131 case kMathLog:
1132 return DoMathLog(instr);
1133 case kMathExp:
1134 return DoMathExp(instr);
1135 case kMathSqrt:
1136 return DoMathSqrt(instr);
1137 case kMathPowHalf:
1138 return DoMathPowHalf(instr);
1139 case kMathClz32:
1140 return DoMathClz32(instr);
1141 default:
1142 UNREACHABLE();
1143 return NULL;
1144 }
1145}
1146
1147
1148LInstruction* LChunkBuilder::DoMathFloor(HUnaryMathOperation* instr) {
1149 LOperand* input = UseRegister(instr->value());
1150 LMathFloor* result = new (zone()) LMathFloor(input);
1151 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1152}
1153
1154
1155LInstruction* LChunkBuilder::DoMathRound(HUnaryMathOperation* instr) {
1156 LOperand* input = UseRegister(instr->value());
1157 LOperand* temp = TempDoubleRegister();
1158 LMathRound* result = new (zone()) LMathRound(input, temp);
1159 return AssignEnvironment(DefineAsRegister(result));
1160}
1161
1162
1163LInstruction* LChunkBuilder::DoMathFround(HUnaryMathOperation* instr) {
1164 LOperand* input = UseRegister(instr->value());
1165 LMathFround* result = new (zone()) LMathFround(input);
1166 return DefineAsRegister(result);
1167}
1168
1169
1170LInstruction* LChunkBuilder::DoMathAbs(HUnaryMathOperation* instr) {
1171 Representation r = instr->value()->representation();
1172 LOperand* context = (r.IsDouble() || r.IsSmiOrInteger32())
1173 ? NULL
1174 : UseFixed(instr->context(), cp);
1175 LOperand* input = UseRegister(instr->value());
1176 LInstruction* result =
1177 DefineAsRegister(new (zone()) LMathAbs(context, input));
1178 if (!r.IsDouble() && !r.IsSmiOrInteger32()) result = AssignPointerMap(result);
1179 if (!r.IsDouble()) result = AssignEnvironment(result);
1180 return result;
1181}
1182
1183
1184LInstruction* LChunkBuilder::DoMathLog(HUnaryMathOperation* instr) {
1185 DCHECK(instr->representation().IsDouble());
1186 DCHECK(instr->value()->representation().IsDouble());
1187 LOperand* input = UseFixedDouble(instr->value(), d1);
1188 return MarkAsCall(DefineFixedDouble(new (zone()) LMathLog(input), d1), instr);
1189}
1190
1191
1192LInstruction* LChunkBuilder::DoMathClz32(HUnaryMathOperation* instr) {
1193 LOperand* input = UseRegisterAtStart(instr->value());
1194 LMathClz32* result = new (zone()) LMathClz32(input);
1195 return DefineAsRegister(result);
1196}
1197
1198
1199LInstruction* LChunkBuilder::DoMathExp(HUnaryMathOperation* instr) {
1200 DCHECK(instr->representation().IsDouble());
1201 DCHECK(instr->value()->representation().IsDouble());
1202 LOperand* input = UseRegister(instr->value());
1203 LOperand* temp1 = TempRegister();
1204 LOperand* temp2 = TempRegister();
1205 LOperand* double_temp = TempDoubleRegister();
1206 LMathExp* result = new (zone()) LMathExp(input, double_temp, temp1, temp2);
1207 return DefineAsRegister(result);
1208}
1209
1210
1211LInstruction* LChunkBuilder::DoMathSqrt(HUnaryMathOperation* instr) {
1212 LOperand* input = UseRegisterAtStart(instr->value());
1213 LMathSqrt* result = new (zone()) LMathSqrt(input);
1214 return DefineAsRegister(result);
1215}
1216
1217
1218LInstruction* LChunkBuilder::DoMathPowHalf(HUnaryMathOperation* instr) {
1219 LOperand* input = UseRegisterAtStart(instr->value());
1220 LMathPowHalf* result = new (zone()) LMathPowHalf(input);
1221 return DefineAsRegister(result);
1222}
1223
1224
1225LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
1226 LOperand* context = UseFixed(instr->context(), cp);
1227 LOperand* constructor = UseFixed(instr->constructor(), r4);
1228 LCallNew* result = new (zone()) LCallNew(context, constructor);
1229 return MarkAsCall(DefineFixed(result, r3), instr);
1230}
1231
1232
1233LInstruction* LChunkBuilder::DoCallNewArray(HCallNewArray* instr) {
1234 LOperand* context = UseFixed(instr->context(), cp);
1235 LOperand* constructor = UseFixed(instr->constructor(), r4);
1236 LCallNewArray* result = new (zone()) LCallNewArray(context, constructor);
1237 return MarkAsCall(DefineFixed(result, r3), instr);
1238}
1239
1240
1241LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
1242 LOperand* context = UseFixed(instr->context(), cp);
1243 LOperand* function = UseFixed(instr->function(), r4);
1244 LCallFunction* call = new (zone()) LCallFunction(context, function);
1245 return MarkAsCall(DefineFixed(call, r3), instr);
1246}
1247
1248
1249LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1250 LOperand* context = UseFixed(instr->context(), cp);
1251 return MarkAsCall(DefineFixed(new (zone()) LCallRuntime(context), r3), instr);
1252}
1253
1254
1255LInstruction* LChunkBuilder::DoRor(HRor* instr) {
1256 return DoShift(Token::ROR, instr);
1257}
1258
1259
1260LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1261 return DoShift(Token::SHR, instr);
1262}
1263
1264
1265LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1266 return DoShift(Token::SAR, instr);
1267}
1268
1269
1270LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1271 return DoShift(Token::SHL, instr);
1272}
1273
1274
1275LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
1276 if (instr->representation().IsSmiOrInteger32()) {
1277 DCHECK(instr->left()->representation().Equals(instr->representation()));
1278 DCHECK(instr->right()->representation().Equals(instr->representation()));
1279 DCHECK(instr->CheckFlag(HValue::kTruncatingToInt32));
1280
1281 LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
1282 LOperand* right = UseOrConstantAtStart(instr->BetterRightOperand());
1283 return DefineAsRegister(new (zone()) LBitI(left, right));
1284 } else {
1285 return DoArithmeticT(instr->op(), instr);
1286 }
1287}
1288
1289
1290LInstruction* LChunkBuilder::DoDivByPowerOf2I(HDiv* instr) {
1291 DCHECK(instr->representation().IsSmiOrInteger32());
1292 DCHECK(instr->left()->representation().Equals(instr->representation()));
1293 DCHECK(instr->right()->representation().Equals(instr->representation()));
1294 LOperand* dividend = UseRegister(instr->left());
1295 int32_t divisor = instr->right()->GetInteger32Constant();
1296 LInstruction* result =
1297 DefineAsRegister(new (zone()) LDivByPowerOf2I(dividend, divisor));
1298 if ((instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1299 (instr->CheckFlag(HValue::kCanOverflow) && divisor == -1) ||
1300 (!instr->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1301 divisor != 1 && divisor != -1)) {
1302 result = AssignEnvironment(result);
1303 }
1304 return result;
1305}
1306
1307
1308LInstruction* LChunkBuilder::DoDivByConstI(HDiv* instr) {
1309 DCHECK(instr->representation().IsInteger32());
1310 DCHECK(instr->left()->representation().Equals(instr->representation()));
1311 DCHECK(instr->right()->representation().Equals(instr->representation()));
1312 LOperand* dividend = UseRegister(instr->left());
1313 int32_t divisor = instr->right()->GetInteger32Constant();
1314 LInstruction* result =
1315 DefineAsRegister(new (zone()) LDivByConstI(dividend, divisor));
1316 if (divisor == 0 ||
1317 (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1318 !instr->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1319 result = AssignEnvironment(result);
1320 }
1321 return result;
1322}
1323
1324
1325LInstruction* LChunkBuilder::DoDivI(HDiv* instr) {
1326 DCHECK(instr->representation().IsSmiOrInteger32());
1327 DCHECK(instr->left()->representation().Equals(instr->representation()));
1328 DCHECK(instr->right()->representation().Equals(instr->representation()));
1329 LOperand* dividend = UseRegister(instr->left());
1330 LOperand* divisor = UseRegister(instr->right());
1331 LInstruction* result =
1332 DefineAsRegister(new (zone()) LDivI(dividend, divisor));
1333 if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1334 instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1335 (instr->CheckFlag(HValue::kCanOverflow) &&
1336 !instr->CheckFlag(HValue::kAllUsesTruncatingToInt32)) ||
1337 (!instr->IsMathFloorOfDiv() &&
1338 !instr->CheckFlag(HValue::kAllUsesTruncatingToInt32))) {
1339 result = AssignEnvironment(result);
1340 }
1341 return result;
1342}
1343
1344
1345LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
1346 if (instr->representation().IsSmiOrInteger32()) {
1347 if (instr->RightIsPowerOf2()) {
1348 return DoDivByPowerOf2I(instr);
1349 } else if (instr->right()->IsConstant()) {
1350 return DoDivByConstI(instr);
1351 } else {
1352 return DoDivI(instr);
1353 }
1354 } else if (instr->representation().IsDouble()) {
1355 return DoArithmeticD(Token::DIV, instr);
1356 } else {
1357 return DoArithmeticT(Token::DIV, instr);
1358 }
1359}
1360
1361
1362LInstruction* LChunkBuilder::DoFlooringDivByPowerOf2I(HMathFloorOfDiv* instr) {
1363 LOperand* dividend = UseRegisterAtStart(instr->left());
1364 int32_t divisor = instr->right()->GetInteger32Constant();
1365 LInstruction* result =
1366 DefineAsRegister(new (zone()) LFlooringDivByPowerOf2I(dividend, divisor));
1367 if ((instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1368 (instr->CheckFlag(HValue::kLeftCanBeMinInt) && divisor == -1)) {
1369 result = AssignEnvironment(result);
1370 }
1371 return result;
1372}
1373
1374
1375LInstruction* LChunkBuilder::DoFlooringDivByConstI(HMathFloorOfDiv* instr) {
1376 DCHECK(instr->representation().IsInteger32());
1377 DCHECK(instr->left()->representation().Equals(instr->representation()));
1378 DCHECK(instr->right()->representation().Equals(instr->representation()));
1379 LOperand* dividend = UseRegister(instr->left());
1380 int32_t divisor = instr->right()->GetInteger32Constant();
1381 LOperand* temp =
1382 ((divisor > 0 && !instr->CheckFlag(HValue::kLeftCanBeNegative)) ||
1383 (divisor < 0 && !instr->CheckFlag(HValue::kLeftCanBePositive)))
1384 ? NULL
1385 : TempRegister();
1386 LInstruction* result = DefineAsRegister(
1387 new (zone()) LFlooringDivByConstI(dividend, divisor, temp));
1388 if (divisor == 0 ||
1389 (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0)) {
1390 result = AssignEnvironment(result);
1391 }
1392 return result;
1393}
1394
1395
1396LInstruction* LChunkBuilder::DoFlooringDivI(HMathFloorOfDiv* instr) {
1397 DCHECK(instr->representation().IsSmiOrInteger32());
1398 DCHECK(instr->left()->representation().Equals(instr->representation()));
1399 DCHECK(instr->right()->representation().Equals(instr->representation()));
1400 LOperand* dividend = UseRegister(instr->left());
1401 LOperand* divisor = UseRegister(instr->right());
1402 LFlooringDivI* div = new (zone()) LFlooringDivI(dividend, divisor);
1403 return AssignEnvironment(DefineAsRegister(div));
1404}
1405
1406
1407LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
1408 if (instr->RightIsPowerOf2()) {
1409 return DoFlooringDivByPowerOf2I(instr);
1410 } else if (instr->right()->IsConstant()) {
1411 return DoFlooringDivByConstI(instr);
1412 } else {
1413 return DoFlooringDivI(instr);
1414 }
1415}
1416
1417
1418LInstruction* LChunkBuilder::DoModByPowerOf2I(HMod* instr) {
1419 DCHECK(instr->representation().IsSmiOrInteger32());
1420 DCHECK(instr->left()->representation().Equals(instr->representation()));
1421 DCHECK(instr->right()->representation().Equals(instr->representation()));
1422 LOperand* dividend = UseRegisterAtStart(instr->left());
1423 int32_t divisor = instr->right()->GetInteger32Constant();
1424 LInstruction* result =
1425 DefineSameAsFirst(new (zone()) LModByPowerOf2I(dividend, divisor));
1426 if (instr->CheckFlag(HValue::kLeftCanBeNegative) &&
1427 instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1428 result = AssignEnvironment(result);
1429 }
1430 return result;
1431}
1432
1433
1434LInstruction* LChunkBuilder::DoModByConstI(HMod* instr) {
1435 DCHECK(instr->representation().IsSmiOrInteger32());
1436 DCHECK(instr->left()->representation().Equals(instr->representation()));
1437 DCHECK(instr->right()->representation().Equals(instr->representation()));
1438 LOperand* dividend = UseRegister(instr->left());
1439 int32_t divisor = instr->right()->GetInteger32Constant();
1440 LInstruction* result =
1441 DefineAsRegister(new (zone()) LModByConstI(dividend, divisor));
1442 if (divisor == 0 || instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1443 result = AssignEnvironment(result);
1444 }
1445 return result;
1446}
1447
1448
1449LInstruction* LChunkBuilder::DoModI(HMod* instr) {
1450 DCHECK(instr->representation().IsSmiOrInteger32());
1451 DCHECK(instr->left()->representation().Equals(instr->representation()));
1452 DCHECK(instr->right()->representation().Equals(instr->representation()));
1453 LOperand* dividend = UseRegister(instr->left());
1454 LOperand* divisor = UseRegister(instr->right());
1455 LInstruction* result =
1456 DefineAsRegister(new (zone()) LModI(dividend, divisor));
1457 if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1458 instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1459 result = AssignEnvironment(result);
1460 }
1461 return result;
1462}
1463
1464
1465LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1466 if (instr->representation().IsSmiOrInteger32()) {
1467 if (instr->RightIsPowerOf2()) {
1468 return DoModByPowerOf2I(instr);
1469 } else if (instr->right()->IsConstant()) {
1470 return DoModByConstI(instr);
1471 } else {
1472 return DoModI(instr);
1473 }
1474 } else if (instr->representation().IsDouble()) {
1475 return DoArithmeticD(Token::MOD, instr);
1476 } else {
1477 return DoArithmeticT(Token::MOD, instr);
1478 }
1479}
1480
1481
1482LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1483 if (instr->representation().IsSmiOrInteger32()) {
1484 DCHECK(instr->left()->representation().Equals(instr->representation()));
1485 DCHECK(instr->right()->representation().Equals(instr->representation()));
1486 HValue* left = instr->BetterLeftOperand();
1487 HValue* right = instr->BetterRightOperand();
1488 LOperand* left_op;
1489 LOperand* right_op;
1490 bool can_overflow = instr->CheckFlag(HValue::kCanOverflow);
1491 bool bailout_on_minus_zero = instr->CheckFlag(HValue::kBailoutOnMinusZero);
1492
1493 if (right->IsConstant()) {
1494 HConstant* constant = HConstant::cast(right);
1495 int32_t constant_value = constant->Integer32Value();
1496 // Constants -1, 0 and 1 can be optimized if the result can overflow.
1497 // For other constants, it can be optimized only without overflow.
1498 if (!can_overflow || ((constant_value >= -1) && (constant_value <= 1))) {
1499 left_op = UseRegisterAtStart(left);
1500 right_op = UseConstant(right);
1501 } else {
1502 if (bailout_on_minus_zero) {
1503 left_op = UseRegister(left);
1504 } else {
1505 left_op = UseRegisterAtStart(left);
1506 }
1507 right_op = UseRegister(right);
1508 }
1509 } else {
1510 if (bailout_on_minus_zero) {
1511 left_op = UseRegister(left);
1512 } else {
1513 left_op = UseRegisterAtStart(left);
1514 }
1515 right_op = UseRegister(right);
1516 }
1517 LMulI* mul = new (zone()) LMulI(left_op, right_op);
1518 if (can_overflow || bailout_on_minus_zero) {
1519 AssignEnvironment(mul);
1520 }
1521 return DefineAsRegister(mul);
1522
1523 } else if (instr->representation().IsDouble()) {
1524 if (instr->HasOneUse() &&
1525 (instr->uses().value()->IsAdd() || instr->uses().value()->IsSub())) {
1526 HBinaryOperation* use = HBinaryOperation::cast(instr->uses().value());
1527
1528 if (use->IsAdd() && instr == use->left()) {
1529 // This mul is the lhs of an add. The add and mul will be folded into a
1530 // multiply-add in DoAdd.
1531 return NULL;
1532 }
1533 if (instr == use->right() && use->IsAdd() &&
1534 !(use->left()->IsMul() && use->left()->HasOneUse())) {
1535 // This mul is the rhs of an add, where the lhs is not another mul.
1536 // The add and mul will be folded into a multiply-add in DoAdd.
1537 return NULL;
1538 }
1539 if (instr == use->left() && use->IsSub()) {
1540 // This mul is the lhs of a sub. The mul and sub will be folded into a
1541 // multiply-sub in DoSub.
1542 return NULL;
1543 }
1544 }
1545
1546 return DoArithmeticD(Token::MUL, instr);
1547 } else {
1548 return DoArithmeticT(Token::MUL, instr);
1549 }
1550}
1551
1552
1553LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1554 if (instr->representation().IsSmiOrInteger32()) {
1555 DCHECK(instr->left()->representation().Equals(instr->representation()));
1556 DCHECK(instr->right()->representation().Equals(instr->representation()));
1557
1558 if (instr->left()->IsConstant() &&
1559 !instr->CheckFlag(HValue::kCanOverflow)) {
1560 // If lhs is constant, do reverse subtraction instead.
1561 return DoRSub(instr);
1562 }
1563
1564 LOperand* left = UseRegisterAtStart(instr->left());
1565 LOperand* right = UseOrConstantAtStart(instr->right());
1566 LSubI* sub = new (zone()) LSubI(left, right);
1567 LInstruction* result = DefineAsRegister(sub);
1568 if (instr->CheckFlag(HValue::kCanOverflow)) {
1569 result = AssignEnvironment(result);
1570 }
1571 return result;
1572 } else if (instr->representation().IsDouble()) {
1573 if (instr->left()->IsMul() && instr->left()->HasOneUse()) {
1574 return DoMultiplySub(instr->right(), HMul::cast(instr->left()));
1575 }
1576
1577 return DoArithmeticD(Token::SUB, instr);
1578 } else {
1579 return DoArithmeticT(Token::SUB, instr);
1580 }
1581}
1582
1583
1584LInstruction* LChunkBuilder::DoRSub(HSub* instr) {
1585 DCHECK(instr->representation().IsSmiOrInteger32());
1586 DCHECK(instr->left()->representation().Equals(instr->representation()));
1587 DCHECK(instr->right()->representation().Equals(instr->representation()));
1588 DCHECK(!instr->CheckFlag(HValue::kCanOverflow));
1589
1590 // Note: The lhs of the subtraction becomes the rhs of the
1591 // reverse-subtraction.
1592 LOperand* left = UseRegisterAtStart(instr->right());
1593 LOperand* right = UseOrConstantAtStart(instr->left());
1594 LRSubI* rsb = new (zone()) LRSubI(left, right);
1595 LInstruction* result = DefineAsRegister(rsb);
1596 return result;
1597}
1598
1599
1600LInstruction* LChunkBuilder::DoMultiplyAdd(HMul* mul, HValue* addend) {
1601 LOperand* multiplier_op = UseRegisterAtStart(mul->left());
1602 LOperand* multiplicand_op = UseRegisterAtStart(mul->right());
1603 LOperand* addend_op = UseRegisterAtStart(addend);
1604 return DefineSameAsFirst(
1605 new (zone()) LMultiplyAddD(addend_op, multiplier_op, multiplicand_op));
1606}
1607
1608
1609LInstruction* LChunkBuilder::DoMultiplySub(HValue* minuend, HMul* mul) {
1610 LOperand* minuend_op = UseRegisterAtStart(minuend);
1611 LOperand* multiplier_op = UseRegisterAtStart(mul->left());
1612 LOperand* multiplicand_op = UseRegisterAtStart(mul->right());
1613
1614 return DefineSameAsFirst(
1615 new (zone()) LMultiplySubD(minuend_op, multiplier_op, multiplicand_op));
1616}
1617
1618
1619LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1620 if (instr->representation().IsSmiOrInteger32()) {
1621 DCHECK(instr->left()->representation().Equals(instr->representation()));
1622 DCHECK(instr->right()->representation().Equals(instr->representation()));
1623 LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
1624 LOperand* right = UseOrConstantAtStart(instr->BetterRightOperand());
1625 LAddI* add = new (zone()) LAddI(left, right);
1626 LInstruction* result = DefineAsRegister(add);
1627 if (instr->CheckFlag(HValue::kCanOverflow)) {
1628 result = AssignEnvironment(result);
1629 }
1630 return result;
1631 } else if (instr->representation().IsExternal()) {
1632 DCHECK(instr->left()->representation().IsExternal());
1633 DCHECK(instr->right()->representation().IsInteger32());
1634 DCHECK(!instr->CheckFlag(HValue::kCanOverflow));
1635 LOperand* left = UseRegisterAtStart(instr->left());
1636 LOperand* right = UseOrConstantAtStart(instr->right());
1637 LAddI* add = new (zone()) LAddI(left, right);
1638 LInstruction* result = DefineAsRegister(add);
1639 return result;
1640 } else if (instr->representation().IsDouble()) {
1641 if (instr->left()->IsMul() && instr->left()->HasOneUse()) {
1642 return DoMultiplyAdd(HMul::cast(instr->left()), instr->right());
1643 }
1644
1645 if (instr->right()->IsMul() && instr->right()->HasOneUse()) {
1646 DCHECK(!instr->left()->IsMul() || !instr->left()->HasOneUse());
1647 return DoMultiplyAdd(HMul::cast(instr->right()), instr->left());
1648 }
1649
1650 return DoArithmeticD(Token::ADD, instr);
1651 } else {
1652 return DoArithmeticT(Token::ADD, instr);
1653 }
1654}
1655
1656
1657LInstruction* LChunkBuilder::DoMathMinMax(HMathMinMax* instr) {
1658 LOperand* left = NULL;
1659 LOperand* right = NULL;
1660 if (instr->representation().IsSmiOrInteger32()) {
1661 DCHECK(instr->left()->representation().Equals(instr->representation()));
1662 DCHECK(instr->right()->representation().Equals(instr->representation()));
1663 left = UseRegisterAtStart(instr->BetterLeftOperand());
1664 right = UseOrConstantAtStart(instr->BetterRightOperand());
1665 } else {
1666 DCHECK(instr->representation().IsDouble());
1667 DCHECK(instr->left()->representation().IsDouble());
1668 DCHECK(instr->right()->representation().IsDouble());
1669 left = UseRegisterAtStart(instr->left());
1670 right = UseRegisterAtStart(instr->right());
1671 }
1672 return DefineAsRegister(new (zone()) LMathMinMax(left, right));
1673}
1674
1675
1676LInstruction* LChunkBuilder::DoPower(HPower* instr) {
1677 DCHECK(instr->representation().IsDouble());
1678 // We call a C function for double power. It can't trigger a GC.
1679 // We need to use fixed result register for the call.
1680 Representation exponent_type = instr->right()->representation();
1681 DCHECK(instr->left()->representation().IsDouble());
1682 LOperand* left = UseFixedDouble(instr->left(), d1);
1683 LOperand* right =
1684 exponent_type.IsDouble()
1685 ? UseFixedDouble(instr->right(), d2)
1686 : UseFixed(instr->right(), MathPowTaggedDescriptor::exponent());
1687 LPower* result = new (zone()) LPower(left, right);
1688 return MarkAsCall(DefineFixedDouble(result, d3), instr,
1689 CAN_DEOPTIMIZE_EAGERLY);
1690}
1691
1692
1693LInstruction* LChunkBuilder::DoCompareGeneric(HCompareGeneric* instr) {
1694 DCHECK(instr->left()->representation().IsTagged());
1695 DCHECK(instr->right()->representation().IsTagged());
1696 LOperand* context = UseFixed(instr->context(), cp);
1697 LOperand* left = UseFixed(instr->left(), r4);
1698 LOperand* right = UseFixed(instr->right(), r3);
1699 LCmpT* result = new (zone()) LCmpT(context, left, right);
1700 return MarkAsCall(DefineFixed(result, r3), instr);
1701}
1702
1703
1704LInstruction* LChunkBuilder::DoCompareNumericAndBranch(
1705 HCompareNumericAndBranch* instr) {
1706 Representation r = instr->representation();
1707 if (r.IsSmiOrInteger32()) {
1708 DCHECK(instr->left()->representation().Equals(r));
1709 DCHECK(instr->right()->representation().Equals(r));
1710 LOperand* left = UseRegisterOrConstantAtStart(instr->left());
1711 LOperand* right = UseRegisterOrConstantAtStart(instr->right());
1712 return new (zone()) LCompareNumericAndBranch(left, right);
1713 } else {
1714 DCHECK(r.IsDouble());
1715 DCHECK(instr->left()->representation().IsDouble());
1716 DCHECK(instr->right()->representation().IsDouble());
1717 LOperand* left = UseRegisterAtStart(instr->left());
1718 LOperand* right = UseRegisterAtStart(instr->right());
1719 return new (zone()) LCompareNumericAndBranch(left, right);
1720 }
1721}
1722
1723
1724LInstruction* LChunkBuilder::DoCompareObjectEqAndBranch(
1725 HCompareObjectEqAndBranch* instr) {
1726 LOperand* left = UseRegisterAtStart(instr->left());
1727 LOperand* right = UseRegisterAtStart(instr->right());
1728 return new (zone()) LCmpObjectEqAndBranch(left, right);
1729}
1730
1731
1732LInstruction* LChunkBuilder::DoCompareHoleAndBranch(
1733 HCompareHoleAndBranch* instr) {
1734 LOperand* value = UseRegisterAtStart(instr->value());
1735 return new (zone()) LCmpHoleAndBranch(value);
1736}
1737
1738
1739LInstruction* LChunkBuilder::DoCompareMinusZeroAndBranch(
1740 HCompareMinusZeroAndBranch* instr) {
1741 LOperand* value = UseRegister(instr->value());
1742 LOperand* scratch = TempRegister();
1743 return new (zone()) LCompareMinusZeroAndBranch(value, scratch);
1744}
1745
1746
1747LInstruction* LChunkBuilder::DoIsObjectAndBranch(HIsObjectAndBranch* instr) {
1748 DCHECK(instr->value()->representation().IsTagged());
1749 LOperand* value = UseRegisterAtStart(instr->value());
1750 LOperand* temp = TempRegister();
1751 return new (zone()) LIsObjectAndBranch(value, temp);
1752}
1753
1754
1755LInstruction* LChunkBuilder::DoIsStringAndBranch(HIsStringAndBranch* instr) {
1756 DCHECK(instr->value()->representation().IsTagged());
1757 LOperand* value = UseRegisterAtStart(instr->value());
1758 LOperand* temp = TempRegister();
1759 return new (zone()) LIsStringAndBranch(value, temp);
1760}
1761
1762
1763LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
1764 DCHECK(instr->value()->representation().IsTagged());
1765 return new (zone()) LIsSmiAndBranch(Use(instr->value()));
1766}
1767
1768
1769LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
1770 HIsUndetectableAndBranch* instr) {
1771 DCHECK(instr->value()->representation().IsTagged());
1772 LOperand* value = UseRegisterAtStart(instr->value());
1773 return new (zone()) LIsUndetectableAndBranch(value, TempRegister());
1774}
1775
1776
1777LInstruction* LChunkBuilder::DoStringCompareAndBranch(
1778 HStringCompareAndBranch* instr) {
1779 DCHECK(instr->left()->representation().IsTagged());
1780 DCHECK(instr->right()->representation().IsTagged());
1781 LOperand* context = UseFixed(instr->context(), cp);
1782 LOperand* left = UseFixed(instr->left(), r4);
1783 LOperand* right = UseFixed(instr->right(), r3);
1784 LStringCompareAndBranch* result =
1785 new (zone()) LStringCompareAndBranch(context, left, right);
1786 return MarkAsCall(result, instr);
1787}
1788
1789
1790LInstruction* LChunkBuilder::DoHasInstanceTypeAndBranch(
1791 HHasInstanceTypeAndBranch* instr) {
1792 DCHECK(instr->value()->representation().IsTagged());
1793 LOperand* value = UseRegisterAtStart(instr->value());
1794 return new (zone()) LHasInstanceTypeAndBranch(value);
1795}
1796
1797
1798LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
1799 HGetCachedArrayIndex* instr) {
1800 DCHECK(instr->value()->representation().IsTagged());
1801 LOperand* value = UseRegisterAtStart(instr->value());
1802
1803 return DefineAsRegister(new (zone()) LGetCachedArrayIndex(value));
1804}
1805
1806
1807LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
1808 HHasCachedArrayIndexAndBranch* instr) {
1809 DCHECK(instr->value()->representation().IsTagged());
1810 return new (zone())
1811 LHasCachedArrayIndexAndBranch(UseRegisterAtStart(instr->value()));
1812}
1813
1814
1815LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
1816 HClassOfTestAndBranch* instr) {
1817 DCHECK(instr->value()->representation().IsTagged());
1818 LOperand* value = UseRegister(instr->value());
1819 return new (zone()) LClassOfTestAndBranch(value, TempRegister());
1820}
1821
1822
1823LInstruction* LChunkBuilder::DoMapEnumLength(HMapEnumLength* instr) {
1824 LOperand* map = UseRegisterAtStart(instr->value());
1825 return DefineAsRegister(new (zone()) LMapEnumLength(map));
1826}
1827
1828
1829LInstruction* LChunkBuilder::DoDateField(HDateField* instr) {
1830 LOperand* object = UseFixed(instr->value(), r3);
1831 LDateField* result =
1832 new (zone()) LDateField(object, FixedTemp(r4), instr->index());
1833 return MarkAsCall(DefineFixed(result, r3), instr, CAN_DEOPTIMIZE_EAGERLY);
1834}
1835
1836
1837LInstruction* LChunkBuilder::DoSeqStringGetChar(HSeqStringGetChar* instr) {
1838 LOperand* string = UseRegisterAtStart(instr->string());
1839 LOperand* index = UseRegisterOrConstantAtStart(instr->index());
1840 return DefineAsRegister(new (zone()) LSeqStringGetChar(string, index));
1841}
1842
1843
1844LInstruction* LChunkBuilder::DoSeqStringSetChar(HSeqStringSetChar* instr) {
1845 LOperand* string = UseRegisterAtStart(instr->string());
1846 LOperand* index = FLAG_debug_code
1847 ? UseRegisterAtStart(instr->index())
1848 : UseRegisterOrConstantAtStart(instr->index());
1849 LOperand* value = UseRegisterAtStart(instr->value());
1850 LOperand* context = FLAG_debug_code ? UseFixed(instr->context(), cp) : NULL;
1851 return new (zone()) LSeqStringSetChar(context, string, index, value);
1852}
1853
1854
1855LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
1856 if (!FLAG_debug_code && instr->skip_check()) return NULL;
1857 LOperand* index = UseRegisterOrConstantAtStart(instr->index());
1858 LOperand* length = !index->IsConstantOperand()
1859 ? UseRegisterOrConstantAtStart(instr->length())
1860 : UseRegisterAtStart(instr->length());
1861 LInstruction* result = new (zone()) LBoundsCheck(index, length);
1862 if (!FLAG_debug_code || !instr->skip_check()) {
1863 result = AssignEnvironment(result);
1864 }
1865 return result;
1866}
1867
1868
1869LInstruction* LChunkBuilder::DoBoundsCheckBaseIndexInformation(
1870 HBoundsCheckBaseIndexInformation* instr) {
1871 UNREACHABLE();
1872 return NULL;
1873}
1874
1875
1876LInstruction* LChunkBuilder::DoAbnormalExit(HAbnormalExit* instr) {
1877 // The control instruction marking the end of a block that completed
1878 // abruptly (e.g., threw an exception). There is nothing specific to do.
1879 return NULL;
1880}
1881
1882
1883LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) { return NULL; }
1884
1885
1886LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
1887 // All HForceRepresentation instructions should be eliminated in the
1888 // representation change phase of Hydrogen.
1889 UNREACHABLE();
1890 return NULL;
1891}
1892
1893
1894LInstruction* LChunkBuilder::DoChange(HChange* instr) {
1895 Representation from = instr->from();
1896 Representation to = instr->to();
1897 HValue* val = instr->value();
1898 if (from.IsSmi()) {
1899 if (to.IsTagged()) {
1900 LOperand* value = UseRegister(val);
1901 return DefineSameAsFirst(new (zone()) LDummyUse(value));
1902 }
1903 from = Representation::Tagged();
1904 }
1905 if (from.IsTagged()) {
1906 if (to.IsDouble()) {
1907 LOperand* value = UseRegister(val);
1908 LInstruction* result =
1909 DefineAsRegister(new (zone()) LNumberUntagD(value));
1910 if (!val->representation().IsSmi()) result = AssignEnvironment(result);
1911 return result;
1912 } else if (to.IsSmi()) {
1913 LOperand* value = UseRegister(val);
1914 if (val->type().IsSmi()) {
1915 return DefineSameAsFirst(new (zone()) LDummyUse(value));
1916 }
1917 return AssignEnvironment(
1918 DefineSameAsFirst(new (zone()) LCheckSmi(value)));
1919 } else {
1920 DCHECK(to.IsInteger32());
1921 if (val->type().IsSmi() || val->representation().IsSmi()) {
1922 LOperand* value = UseRegisterAtStart(val);
1923 return DefineAsRegister(new (zone()) LSmiUntag(value, false));
1924 } else {
1925 LOperand* value = UseRegister(val);
1926 LOperand* temp1 = TempRegister();
1927 LOperand* temp2 = TempDoubleRegister();
1928 LInstruction* result =
1929 DefineSameAsFirst(new (zone()) LTaggedToI(value, temp1, temp2));
1930 if (!val->representation().IsSmi()) result = AssignEnvironment(result);
1931 return result;
1932 }
1933 }
1934 } else if (from.IsDouble()) {
1935 if (to.IsTagged()) {
1936 info()->MarkAsDeferredCalling();
1937 LOperand* value = UseRegister(val);
1938 LOperand* temp1 = TempRegister();
1939 LOperand* temp2 = TempRegister();
1940 LUnallocated* result_temp = TempRegister();
1941 LNumberTagD* result = new (zone()) LNumberTagD(value, temp1, temp2);
1942 return AssignPointerMap(Define(result, result_temp));
1943 } else if (to.IsSmi()) {
1944 LOperand* value = UseRegister(val);
1945 return AssignEnvironment(
1946 DefineAsRegister(new (zone()) LDoubleToSmi(value)));
1947 } else {
1948 DCHECK(to.IsInteger32());
1949 LOperand* value = UseRegister(val);
1950 LInstruction* result = DefineAsRegister(new (zone()) LDoubleToI(value));
1951 if (!instr->CanTruncateToInt32()) result = AssignEnvironment(result);
1952 return result;
1953 }
1954 } else if (from.IsInteger32()) {
1955 info()->MarkAsDeferredCalling();
1956 if (to.IsTagged()) {
1957 if (!instr->CheckFlag(HValue::kCanOverflow)) {
1958 LOperand* value = UseRegisterAtStart(val);
1959 return DefineAsRegister(new (zone()) LSmiTag(value));
1960 } else if (val->CheckFlag(HInstruction::kUint32)) {
1961 LOperand* value = UseRegisterAtStart(val);
1962 LOperand* temp1 = TempRegister();
1963 LOperand* temp2 = TempRegister();
1964 LNumberTagU* result = new (zone()) LNumberTagU(value, temp1, temp2);
1965 return AssignPointerMap(DefineAsRegister(result));
1966 } else {
1967 LOperand* value = UseRegisterAtStart(val);
1968 LOperand* temp1 = TempRegister();
1969 LOperand* temp2 = TempRegister();
1970 LNumberTagI* result = new (zone()) LNumberTagI(value, temp1, temp2);
1971 return AssignPointerMap(DefineAsRegister(result));
1972 }
1973 } else if (to.IsSmi()) {
1974 LOperand* value = UseRegister(val);
1975 LInstruction* result = DefineAsRegister(new (zone()) LSmiTag(value));
1976 if (instr->CheckFlag(HValue::kCanOverflow)) {
1977 result = AssignEnvironment(result);
1978 }
1979 return result;
1980 } else {
1981 DCHECK(to.IsDouble());
1982 if (val->CheckFlag(HInstruction::kUint32)) {
1983 return DefineAsRegister(new (zone()) LUint32ToDouble(UseRegister(val)));
1984 } else {
1985 return DefineAsRegister(new (zone()) LInteger32ToDouble(Use(val)));
1986 }
1987 }
1988 }
1989 UNREACHABLE();
1990 return NULL;
1991}
1992
1993
1994LInstruction* LChunkBuilder::DoCheckHeapObject(HCheckHeapObject* instr) {
1995 LOperand* value = UseRegisterAtStart(instr->value());
1996 LInstruction* result = new (zone()) LCheckNonSmi(value);
1997 if (!instr->value()->type().IsHeapObject()) {
1998 result = AssignEnvironment(result);
1999 }
2000 return result;
2001}
2002
2003
2004LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
2005 LOperand* value = UseRegisterAtStart(instr->value());
2006 return AssignEnvironment(new (zone()) LCheckSmi(value));
2007}
2008
2009
2010LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
2011 LOperand* value = UseRegisterAtStart(instr->value());
2012 LInstruction* result = new (zone()) LCheckInstanceType(value);
2013 return AssignEnvironment(result);
2014}
2015
2016
2017LInstruction* LChunkBuilder::DoCheckValue(HCheckValue* instr) {
2018 LOperand* value = UseRegisterAtStart(instr->value());
2019 return AssignEnvironment(new (zone()) LCheckValue(value));
2020}
2021
2022
2023LInstruction* LChunkBuilder::DoCheckMaps(HCheckMaps* instr) {
2024 if (instr->IsStabilityCheck()) return new (zone()) LCheckMaps;
2025 LOperand* value = UseRegisterAtStart(instr->value());
2026 LInstruction* result = AssignEnvironment(new (zone()) LCheckMaps(value));
2027 if (instr->HasMigrationTarget()) {
2028 info()->MarkAsDeferredCalling();
2029 result = AssignPointerMap(result);
2030 }
2031 return result;
2032}
2033
2034
2035LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
2036 HValue* value = instr->value();
2037 Representation input_rep = value->representation();
2038 LOperand* reg = UseRegister(value);
2039 if (input_rep.IsDouble()) {
2040 return DefineAsRegister(new (zone()) LClampDToUint8(reg));
2041 } else if (input_rep.IsInteger32()) {
2042 return DefineAsRegister(new (zone()) LClampIToUint8(reg));
2043 } else {
2044 DCHECK(input_rep.IsSmiOrTagged());
2045 LClampTToUint8* result =
2046 new (zone()) LClampTToUint8(reg, TempDoubleRegister());
2047 return AssignEnvironment(DefineAsRegister(result));
2048 }
2049}
2050
2051
2052LInstruction* LChunkBuilder::DoDoubleBits(HDoubleBits* instr) {
2053 HValue* value = instr->value();
2054 DCHECK(value->representation().IsDouble());
2055 return DefineAsRegister(new (zone()) LDoubleBits(UseRegister(value)));
2056}
2057
2058
2059LInstruction* LChunkBuilder::DoConstructDouble(HConstructDouble* instr) {
2060 LOperand* lo = UseRegister(instr->lo());
2061 LOperand* hi = UseRegister(instr->hi());
2062 return DefineAsRegister(new (zone()) LConstructDouble(hi, lo));
2063}
2064
2065
2066LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
2067 LOperand* context = info()->IsStub() ? UseFixed(instr->context(), cp) : NULL;
2068 LOperand* parameter_count = UseRegisterOrConstant(instr->parameter_count());
2069 return new (zone())
2070 LReturn(UseFixed(instr->value(), r3), context, parameter_count);
2071}
2072
2073
2074LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
2075 Representation r = instr->representation();
2076 if (r.IsSmi()) {
2077 return DefineAsRegister(new (zone()) LConstantS);
2078 } else if (r.IsInteger32()) {
2079 return DefineAsRegister(new (zone()) LConstantI);
2080 } else if (r.IsDouble()) {
2081 return DefineAsRegister(new (zone()) LConstantD);
2082 } else if (r.IsExternal()) {
2083 return DefineAsRegister(new (zone()) LConstantE);
2084 } else if (r.IsTagged()) {
2085 return DefineAsRegister(new (zone()) LConstantT);
2086 } else {
2087 UNREACHABLE();
2088 return NULL;
2089 }
2090}
2091
2092
2093LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
2094 LLoadGlobalCell* result = new (zone()) LLoadGlobalCell;
2095 return instr->RequiresHoleCheck()
2096 ? AssignEnvironment(DefineAsRegister(result))
2097 : DefineAsRegister(result);
2098}
2099
2100
2101LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
2102 LOperand* context = UseFixed(instr->context(), cp);
2103 LOperand* global_object =
2104 UseFixed(instr->global_object(), LoadDescriptor::ReceiverRegister());
2105 LOperand* vector = NULL;
2106 if (FLAG_vector_ics) {
2107 vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2108 }
2109 LLoadGlobalGeneric* result =
2110 new (zone()) LLoadGlobalGeneric(context, global_object, vector);
2111 return MarkAsCall(DefineFixed(result, r3), instr);
2112}
2113
2114
2115LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
2116 LOperand* value = UseRegister(instr->value());
2117 // Use a temp to check the value in the cell in the case where we perform
2118 // a hole check.
2119 return instr->RequiresHoleCheck()
2120 ? AssignEnvironment(new (zone())
2121 LStoreGlobalCell(value, TempRegister()))
2122 : new (zone()) LStoreGlobalCell(value, NULL);
2123}
2124
2125
2126LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
2127 LOperand* context = UseRegisterAtStart(instr->value());
2128 LInstruction* result =
2129 DefineAsRegister(new (zone()) LLoadContextSlot(context));
2130 if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
2131 result = AssignEnvironment(result);
2132 }
2133 return result;
2134}
2135
2136
2137LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
2138 LOperand* context;
2139 LOperand* value;
2140 if (instr->NeedsWriteBarrier()) {
2141 context = UseTempRegister(instr->context());
2142 value = UseTempRegister(instr->value());
2143 } else {
2144 context = UseRegister(instr->context());
2145 value = UseRegister(instr->value());
2146 }
2147 LInstruction* result = new (zone()) LStoreContextSlot(context, value);
2148 if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
2149 result = AssignEnvironment(result);
2150 }
2151 return result;
2152}
2153
2154
2155LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
2156 LOperand* obj = UseRegisterAtStart(instr->object());
2157 return DefineAsRegister(new (zone()) LLoadNamedField(obj));
2158}
2159
2160
2161LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
2162 LOperand* context = UseFixed(instr->context(), cp);
2163 LOperand* object =
2164 UseFixed(instr->object(), LoadDescriptor::ReceiverRegister());
2165 LOperand* vector = NULL;
2166 if (FLAG_vector_ics) {
2167 vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2168 }
2169
2170 LInstruction* result =
2171 DefineFixed(new (zone()) LLoadNamedGeneric(context, object, vector), r3);
2172 return MarkAsCall(result, instr);
2173}
2174
2175
2176LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
2177 HLoadFunctionPrototype* instr) {
2178 return AssignEnvironment(DefineAsRegister(
2179 new (zone()) LLoadFunctionPrototype(UseRegister(instr->function()))));
2180}
2181
2182
2183LInstruction* LChunkBuilder::DoLoadRoot(HLoadRoot* instr) {
2184 return DefineAsRegister(new (zone()) LLoadRoot);
2185}
2186
2187
2188LInstruction* LChunkBuilder::DoLoadKeyed(HLoadKeyed* instr) {
2189 DCHECK(instr->key()->representation().IsSmiOrInteger32());
2190 ElementsKind elements_kind = instr->elements_kind();
2191 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
2192 LInstruction* result = NULL;
2193
2194 if (!instr->is_typed_elements()) {
2195 LOperand* obj = NULL;
2196 if (instr->representation().IsDouble()) {
2197 obj = UseRegister(instr->elements());
2198 } else {
2199 obj = UseRegisterAtStart(instr->elements());
2200 }
2201 result = DefineAsRegister(new (zone()) LLoadKeyed(obj, key));
2202 } else {
2203 DCHECK((instr->representation().IsInteger32() &&
2204 !IsDoubleOrFloatElementsKind(elements_kind)) ||
2205 (instr->representation().IsDouble() &&
2206 IsDoubleOrFloatElementsKind(elements_kind)));
2207 LOperand* backing_store = UseRegister(instr->elements());
2208 result = DefineAsRegister(new (zone()) LLoadKeyed(backing_store, key));
2209 }
2210
2211 if ((instr->is_external() || instr->is_fixed_typed_array())
2212 ?
2213 // see LCodeGen::DoLoadKeyedExternalArray
2214 ((elements_kind == EXTERNAL_UINT32_ELEMENTS ||
2215 elements_kind == UINT32_ELEMENTS) &&
2216 !instr->CheckFlag(HInstruction::kUint32))
2217 :
2218 // see LCodeGen::DoLoadKeyedFixedDoubleArray and
2219 // LCodeGen::DoLoadKeyedFixedArray
2220 instr->RequiresHoleCheck()) {
2221 result = AssignEnvironment(result);
2222 }
2223 return result;
2224}
2225
2226
2227LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
2228 LOperand* context = UseFixed(instr->context(), cp);
2229 LOperand* object =
2230 UseFixed(instr->object(), LoadDescriptor::ReceiverRegister());
2231 LOperand* key = UseFixed(instr->key(), LoadDescriptor::NameRegister());
2232 LOperand* vector = NULL;
2233 if (FLAG_vector_ics) {
2234 vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2235 }
2236
2237 LInstruction* result = DefineFixed(
2238 new (zone()) LLoadKeyedGeneric(context, object, key, vector), r3);
2239 return MarkAsCall(result, instr);
2240}
2241
2242
2243LInstruction* LChunkBuilder::DoStoreKeyed(HStoreKeyed* instr) {
2244 if (!instr->is_typed_elements()) {
2245 DCHECK(instr->elements()->representation().IsTagged());
2246 bool needs_write_barrier = instr->NeedsWriteBarrier();
2247 LOperand* object = NULL;
2248 LOperand* key = NULL;
2249 LOperand* val = NULL;
2250
2251 if (instr->value()->representation().IsDouble()) {
2252 object = UseRegisterAtStart(instr->elements());
2253 val = UseRegister(instr->value());
2254 key = UseRegisterOrConstantAtStart(instr->key());
2255 } else {
2256 if (needs_write_barrier) {
2257 object = UseTempRegister(instr->elements());
2258 val = UseTempRegister(instr->value());
2259 key = UseTempRegister(instr->key());
2260 } else {
2261 object = UseRegisterAtStart(instr->elements());
2262 val = UseRegisterAtStart(instr->value());
2263 key = UseRegisterOrConstantAtStart(instr->key());
2264 }
2265 }
2266
2267 return new (zone()) LStoreKeyed(object, key, val);
2268 }
2269
2270 DCHECK((instr->value()->representation().IsInteger32() &&
2271 !IsDoubleOrFloatElementsKind(instr->elements_kind())) ||
2272 (instr->value()->representation().IsDouble() &&
2273 IsDoubleOrFloatElementsKind(instr->elements_kind())));
2274 DCHECK((instr->is_fixed_typed_array() &&
2275 instr->elements()->representation().IsTagged()) ||
2276 (instr->is_external() &&
2277 instr->elements()->representation().IsExternal()));
2278 LOperand* val = UseRegister(instr->value());
2279 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
2280 LOperand* backing_store = UseRegister(instr->elements());
2281 return new (zone()) LStoreKeyed(backing_store, key, val);
2282}
2283
2284
2285LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
2286 LOperand* context = UseFixed(instr->context(), cp);
2287 LOperand* obj =
2288 UseFixed(instr->object(), StoreDescriptor::ReceiverRegister());
2289 LOperand* key = UseFixed(instr->key(), StoreDescriptor::NameRegister());
2290 LOperand* val = UseFixed(instr->value(), StoreDescriptor::ValueRegister());
2291
2292 DCHECK(instr->object()->representation().IsTagged());
2293 DCHECK(instr->key()->representation().IsTagged());
2294 DCHECK(instr->value()->representation().IsTagged());
2295
2296 return MarkAsCall(new (zone()) LStoreKeyedGeneric(context, obj, key, val),
2297 instr);
2298}
2299
2300
2301LInstruction* LChunkBuilder::DoTransitionElementsKind(
2302 HTransitionElementsKind* instr) {
2303 if (IsSimpleMapChangeTransition(instr->from_kind(), instr->to_kind())) {
2304 LOperand* object = UseRegister(instr->object());
2305 LOperand* new_map_reg = TempRegister();
2306 LTransitionElementsKind* result =
2307 new (zone()) LTransitionElementsKind(object, NULL, new_map_reg);
2308 return result;
2309 } else {
2310 LOperand* object = UseFixed(instr->object(), r3);
2311 LOperand* context = UseFixed(instr->context(), cp);
2312 LTransitionElementsKind* result =
2313 new (zone()) LTransitionElementsKind(object, context, NULL);
2314 return MarkAsCall(result, instr);
2315 }
2316}
2317
2318
2319LInstruction* LChunkBuilder::DoTrapAllocationMemento(
2320 HTrapAllocationMemento* instr) {
2321 LOperand* object = UseRegister(instr->object());
2322 LOperand* temp = TempRegister();
2323 LTrapAllocationMemento* result =
2324 new (zone()) LTrapAllocationMemento(object, temp);
2325 return AssignEnvironment(result);
2326}
2327
2328
2329LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
2330 bool is_in_object = instr->access().IsInobject();
2331 bool needs_write_barrier = instr->NeedsWriteBarrier();
2332 bool needs_write_barrier_for_map =
2333 instr->has_transition() && instr->NeedsWriteBarrierForMap();
2334
2335 LOperand* obj;
2336 if (needs_write_barrier) {
2337 obj = is_in_object ? UseRegister(instr->object())
2338 : UseTempRegister(instr->object());
2339 } else {
2340 obj = needs_write_barrier_for_map ? UseRegister(instr->object())
2341 : UseRegisterAtStart(instr->object());
2342 }
2343
2344 LOperand* val;
2345 if (needs_write_barrier) {
2346 val = UseTempRegister(instr->value());
2347 } else if (instr->field_representation().IsDouble()) {
2348 val = UseRegisterAtStart(instr->value());
2349 } else {
2350 val = UseRegister(instr->value());
2351 }
2352
2353 // We need a temporary register for write barrier of the map field.
2354 LOperand* temp = needs_write_barrier_for_map ? TempRegister() : NULL;
2355
2356 return new (zone()) LStoreNamedField(obj, val, temp);
2357}
2358
2359
2360LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
2361 LOperand* context = UseFixed(instr->context(), cp);
2362 LOperand* obj =
2363 UseFixed(instr->object(), StoreDescriptor::ReceiverRegister());
2364 LOperand* val = UseFixed(instr->value(), StoreDescriptor::ValueRegister());
2365
2366 LInstruction* result = new (zone()) LStoreNamedGeneric(context, obj, val);
2367 return MarkAsCall(result, instr);
2368}
2369
2370
2371LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
2372 LOperand* context = UseFixed(instr->context(), cp);
2373 LOperand* left = UseFixed(instr->left(), r4);
2374 LOperand* right = UseFixed(instr->right(), r3);
2375 return MarkAsCall(
2376 DefineFixed(new (zone()) LStringAdd(context, left, right), r3), instr);
2377}
2378
2379
2380LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
2381 LOperand* string = UseTempRegister(instr->string());
2382 LOperand* index = UseTempRegister(instr->index());
2383 LOperand* context = UseAny(instr->context());
2384 LStringCharCodeAt* result =
2385 new (zone()) LStringCharCodeAt(context, string, index);
2386 return AssignPointerMap(DefineAsRegister(result));
2387}
2388
2389
2390LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2391 LOperand* char_code = UseRegister(instr->value());
2392 LOperand* context = UseAny(instr->context());
2393 LStringCharFromCode* result =
2394 new (zone()) LStringCharFromCode(context, char_code);
2395 return AssignPointerMap(DefineAsRegister(result));
2396}
2397
2398
2399LInstruction* LChunkBuilder::DoAllocate(HAllocate* instr) {
2400 info()->MarkAsDeferredCalling();
2401 LOperand* context = UseAny(instr->context());
2402 LOperand* size = UseRegisterOrConstant(instr->size());
2403 LOperand* temp1 = TempRegister();
2404 LOperand* temp2 = TempRegister();
2405 LAllocate* result = new (zone()) LAllocate(context, size, temp1, temp2);
2406 return AssignPointerMap(DefineAsRegister(result));
2407}
2408
2409
2410LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2411 LOperand* context = UseFixed(instr->context(), cp);
2412 return MarkAsCall(DefineFixed(new (zone()) LRegExpLiteral(context), r3),
2413 instr);
2414}
2415
2416
2417LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2418 LOperand* context = UseFixed(instr->context(), cp);
2419 return MarkAsCall(DefineFixed(new (zone()) LFunctionLiteral(context), r3),
2420 instr);
2421}
2422
2423
2424LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2425 DCHECK(argument_count_ == 0);
2426 allocator_->MarkAsOsrEntry();
2427 current_block_->last_environment()->set_ast_id(instr->ast_id());
2428 return AssignEnvironment(new (zone()) LOsrEntry);
2429}
2430
2431
2432LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2433 LParameter* result = new (zone()) LParameter;
2434 if (instr->kind() == HParameter::STACK_PARAMETER) {
2435 int spill_index = chunk()->GetParameterStackSlot(instr->index());
2436 return DefineAsSpilled(result, spill_index);
2437 } else {
2438 DCHECK(info()->IsStub());
2439 CallInterfaceDescriptor descriptor =
2440 info()->code_stub()->GetCallInterfaceDescriptor();
2441 int index = static_cast<int>(instr->index());
2442 Register reg = descriptor.GetEnvironmentParameterRegister(index);
2443 return DefineFixed(result, reg);
2444 }
2445}
2446
2447
2448LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2449 // Use an index that corresponds to the location in the unoptimized frame,
2450 // which the optimized frame will subsume.
2451 int env_index = instr->index();
2452 int spill_index = 0;
2453 if (instr->environment()->is_parameter_index(env_index)) {
2454 spill_index = chunk()->GetParameterStackSlot(env_index);
2455 } else {
2456 spill_index = env_index - instr->environment()->first_local_index();
2457 if (spill_index > LUnallocated::kMaxFixedSlotIndex) {
2458 Retry(kTooManySpillSlotsNeededForOSR);
2459 spill_index = 0;
2460 }
2461 }
2462 return DefineAsSpilled(new (zone()) LUnknownOSRValue, spill_index);
2463}
2464
2465
2466LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2467 LOperand* context = UseFixed(instr->context(), cp);
2468 return MarkAsCall(DefineFixed(new (zone()) LCallStub(context), r3), instr);
2469}
2470
2471
2472LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
2473 // There are no real uses of the arguments object.
2474 // arguments.length and element access are supported directly on
2475 // stack arguments, and any real arguments object use causes a bailout.
2476 // So this value is never used.
2477 return NULL;
2478}
2479
2480
2481LInstruction* LChunkBuilder::DoCapturedObject(HCapturedObject* instr) {
2482 instr->ReplayEnvironment(current_block_->last_environment());
2483
2484 // There are no real uses of a captured object.
2485 return NULL;
2486}
2487
2488
2489LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2490 info()->MarkAsRequiresFrame();
2491 LOperand* args = UseRegister(instr->arguments());
2492 LOperand* length = UseRegisterOrConstantAtStart(instr->length());
2493 LOperand* index = UseRegisterOrConstantAtStart(instr->index());
2494 return DefineAsRegister(new (zone()) LAccessArgumentsAt(args, length, index));
2495}
2496
2497
2498LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2499 LOperand* object = UseFixed(instr->value(), r3);
2500 LToFastProperties* result = new (zone()) LToFastProperties(object);
2501 return MarkAsCall(DefineFixed(result, r3), instr);
2502}
2503
2504
2505LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2506 LOperand* context = UseFixed(instr->context(), cp);
2507 LTypeof* result = new (zone()) LTypeof(context, UseFixed(instr->value(), r3));
2508 return MarkAsCall(DefineFixed(result, r3), instr);
2509}
2510
2511
2512LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2513 return new (zone()) LTypeofIsAndBranch(UseRegister(instr->value()));
2514}
2515
2516
2517LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
2518 HIsConstructCallAndBranch* instr) {
2519 return new (zone()) LIsConstructCallAndBranch(TempRegister());
2520}
2521
2522
2523LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2524 instr->ReplayEnvironment(current_block_->last_environment());
2525 return NULL;
2526}
2527
2528
2529LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2530 if (instr->is_function_entry()) {
2531 LOperand* context = UseFixed(instr->context(), cp);
2532 return MarkAsCall(new (zone()) LStackCheck(context), instr);
2533 } else {
2534 DCHECK(instr->is_backwards_branch());
2535 LOperand* context = UseAny(instr->context());
2536 return AssignEnvironment(
2537 AssignPointerMap(new (zone()) LStackCheck(context)));
2538 }
2539}
2540
2541
2542LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2543 HEnvironment* outer = current_block_->last_environment();
2544 outer->set_ast_id(instr->ReturnId());
2545 HConstant* undefined = graph()->GetConstantUndefined();
2546 HEnvironment* inner = outer->CopyForInlining(
2547 instr->closure(), instr->arguments_count(), instr->function(), undefined,
2548 instr->inlining_kind());
2549 // Only replay binding of arguments object if it wasn't removed from graph.
2550 if (instr->arguments_var() != NULL && instr->arguments_object()->IsLinked()) {
2551 inner->Bind(instr->arguments_var(), instr->arguments_object());
2552 }
2553 inner->BindContext(instr->closure_context());
2554 inner->set_entry(instr);
2555 current_block_->UpdateEnvironment(inner);
2556 chunk_->AddInlinedClosure(instr->closure());
2557 return NULL;
2558}
2559
2560
2561LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2562 LInstruction* pop = NULL;
2563
2564 HEnvironment* env = current_block_->last_environment();
2565
2566 if (env->entry()->arguments_pushed()) {
2567 int argument_count = env->arguments_environment()->parameter_count();
2568 pop = new (zone()) LDrop(argument_count);
2569 DCHECK(instr->argument_delta() == -argument_count);
2570 }
2571
2572 HEnvironment* outer =
2573 current_block_->last_environment()->DiscardInlined(false);
2574 current_block_->UpdateEnvironment(outer);
2575
2576 return pop;
2577}
2578
2579
2580LInstruction* LChunkBuilder::DoForInPrepareMap(HForInPrepareMap* instr) {
2581 LOperand* context = UseFixed(instr->context(), cp);
2582 LOperand* object = UseFixed(instr->enumerable(), r3);
2583 LForInPrepareMap* result = new (zone()) LForInPrepareMap(context, object);
2584 return MarkAsCall(DefineFixed(result, r3), instr, CAN_DEOPTIMIZE_EAGERLY);
2585}
2586
2587
2588LInstruction* LChunkBuilder::DoForInCacheArray(HForInCacheArray* instr) {
2589 LOperand* map = UseRegister(instr->map());
2590 return AssignEnvironment(
2591 DefineAsRegister(new (zone()) LForInCacheArray(map)));
2592}
2593
2594
2595LInstruction* LChunkBuilder::DoCheckMapValue(HCheckMapValue* instr) {
2596 LOperand* value = UseRegisterAtStart(instr->value());
2597 LOperand* map = UseRegisterAtStart(instr->map());
2598 return AssignEnvironment(new (zone()) LCheckMapValue(value, map));
2599}
2600
2601
2602LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) {
2603 LOperand* object = UseRegister(instr->object());
2604 LOperand* index = UseTempRegister(instr->index());
2605 LLoadFieldByIndex* load = new (zone()) LLoadFieldByIndex(object, index);
2606 LInstruction* result = DefineSameAsFirst(load);
2607 return AssignPointerMap(result);
2608}
2609
2610
2611LInstruction* LChunkBuilder::DoStoreFrameContext(HStoreFrameContext* instr) {
2612 LOperand* context = UseRegisterAtStart(instr->context());
2613 return new (zone()) LStoreFrameContext(context);
2614}
2615
2616
2617LInstruction* LChunkBuilder::DoAllocateBlockContext(
2618 HAllocateBlockContext* instr) {
2619 LOperand* context = UseFixed(instr->context(), cp);
2620 LOperand* function = UseRegisterAtStart(instr->function());
2621 LAllocateBlockContext* result =
2622 new (zone()) LAllocateBlockContext(context, function);
2623 return MarkAsCall(DefineFixed(result, cp), instr);
2624}
2625}
2626} // namespace v8::internal