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