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