blob: c534abcb65defa69a6c03fa497cc566155a65cc7 [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
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001127LInstruction* LChunkBuilder::DoDeclareGlobals(HDeclareGlobals* instr) {
1128 return MarkAsCall(new LDeclareGlobals, instr);
1129}
1130
1131
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001132LInstruction* LChunkBuilder::DoGlobalObject(HGlobalObject* instr) {
1133 LOperand* context = UseRegisterAtStart(instr->value());
1134 return DefineAsRegister(new LGlobalObject(context));
1135}
1136
1137
1138LInstruction* LChunkBuilder::DoGlobalReceiver(HGlobalReceiver* instr) {
1139 LOperand* global_object = UseRegisterAtStart(instr->value());
1140 return DefineAsRegister(new LGlobalReceiver(global_object));
1141}
1142
1143
1144LInstruction* LChunkBuilder::DoCallConstantFunction(
1145 HCallConstantFunction* instr) {
1146 argument_count_ -= instr->argument_count();
1147 return MarkAsCall(DefineFixed(new LCallConstantFunction, v0), instr);
1148}
1149
1150
1151LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1152 LOperand* function = UseFixed(instr->function(), a1);
1153 argument_count_ -= instr->argument_count();
1154 LInvokeFunction* result = new LInvokeFunction(function);
1155 return MarkAsCall(DefineFixed(result, v0), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1156}
1157
1158
1159LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
1160 BuiltinFunctionId op = instr->op();
1161 if (op == kMathLog || op == kMathSin || op == kMathCos) {
1162 LOperand* input = UseFixedDouble(instr->value(), f4);
1163 LUnaryMathOperation* result = new LUnaryMathOperation(input, NULL);
1164 return MarkAsCall(DefineFixedDouble(result, f4), instr);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001165 } else if (op == kMathPowHalf) {
1166 // Input cannot be the same as the result.
1167 // See lithium-codegen-mips.cc::DoMathPowHalf.
1168 LOperand* input = UseFixedDouble(instr->value(), f8);
1169 LOperand* temp = FixedTemp(f6);
1170 LUnaryMathOperation* result = new LUnaryMathOperation(input, temp);
1171 return DefineFixedDouble(result, f4);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001172 } else {
1173 LOperand* input = UseRegisterAtStart(instr->value());
1174 LOperand* temp = (op == kMathFloor) ? TempRegister() : NULL;
1175 LUnaryMathOperation* result = new LUnaryMathOperation(input, temp);
1176 switch (op) {
1177 case kMathAbs:
1178 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1179 case kMathFloor:
1180 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1181 case kMathSqrt:
1182 return DefineAsRegister(result);
1183 case kMathRound:
1184 return AssignEnvironment(DefineAsRegister(result));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001185 default:
1186 UNREACHABLE();
1187 return NULL;
1188 }
1189 }
1190}
1191
1192
1193LInstruction* LChunkBuilder::DoCallKeyed(HCallKeyed* instr) {
1194 ASSERT(instr->key()->representation().IsTagged());
1195 argument_count_ -= instr->argument_count();
1196 LOperand* key = UseFixed(instr->key(), a2);
1197 return MarkAsCall(DefineFixed(new LCallKeyed(key), v0), instr);
1198}
1199
1200
1201LInstruction* LChunkBuilder::DoCallNamed(HCallNamed* instr) {
1202 argument_count_ -= instr->argument_count();
1203 return MarkAsCall(DefineFixed(new LCallNamed, v0), instr);
1204}
1205
1206
1207LInstruction* LChunkBuilder::DoCallGlobal(HCallGlobal* instr) {
1208 argument_count_ -= instr->argument_count();
1209 return MarkAsCall(DefineFixed(new LCallGlobal, v0), instr);
1210}
1211
1212
1213LInstruction* LChunkBuilder::DoCallKnownGlobal(HCallKnownGlobal* instr) {
1214 argument_count_ -= instr->argument_count();
1215 return MarkAsCall(DefineFixed(new LCallKnownGlobal, v0), instr);
1216}
1217
1218
1219LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
1220 LOperand* constructor = UseFixed(instr->constructor(), a1);
1221 argument_count_ -= instr->argument_count();
1222 LCallNew* result = new LCallNew(constructor);
1223 return MarkAsCall(DefineFixed(result, v0), instr);
1224}
1225
1226
1227LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001228 LOperand* function = UseFixed(instr->function(), a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001229 argument_count_ -= instr->argument_count();
danno@chromium.orgc612e022011-11-10 11:38:15 +00001230 return MarkAsCall(DefineFixed(new LCallFunction(function), v0), instr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001231}
1232
1233
1234LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1235 argument_count_ -= instr->argument_count();
1236 return MarkAsCall(DefineFixed(new LCallRuntime, v0), instr);
1237}
1238
1239
1240LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1241 return DoShift(Token::SHR, instr);
1242}
1243
1244
1245LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1246 return DoShift(Token::SAR, instr);
1247}
1248
1249
1250LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1251 return DoShift(Token::SHL, instr);
1252}
1253
1254
1255LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
1256 if (instr->representation().IsInteger32()) {
1257 ASSERT(instr->left()->representation().IsInteger32());
1258 ASSERT(instr->right()->representation().IsInteger32());
1259
1260 LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1261 LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1262 return DefineAsRegister(new LBitI(left, right));
1263 } else {
1264 ASSERT(instr->representation().IsTagged());
1265 ASSERT(instr->left()->representation().IsTagged());
1266 ASSERT(instr->right()->representation().IsTagged());
1267
1268 LOperand* left = UseFixed(instr->left(), a1);
1269 LOperand* right = UseFixed(instr->right(), a0);
1270 LArithmeticT* result = new LArithmeticT(instr->op(), left, right);
1271 return MarkAsCall(DefineFixed(result, v0), instr);
1272 }
1273}
1274
1275
1276LInstruction* LChunkBuilder::DoBitNot(HBitNot* instr) {
1277 ASSERT(instr->value()->representation().IsInteger32());
1278 ASSERT(instr->representation().IsInteger32());
1279 return DefineAsRegister(new LBitNotI(UseRegisterAtStart(instr->value())));
1280}
1281
1282
1283LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
1284 if (instr->representation().IsDouble()) {
1285 return DoArithmeticD(Token::DIV, instr);
1286 } else if (instr->representation().IsInteger32()) {
1287 // TODO(1042) The fixed register allocation
1288 // is needed because we call TypeRecordingBinaryOpStub from
1289 // the generated code, which requires registers a0
1290 // and a1 to be used. We should remove that
1291 // when we provide a native implementation.
1292 LOperand* dividend = UseFixed(instr->left(), a0);
1293 LOperand* divisor = UseFixed(instr->right(), a1);
1294 return AssignEnvironment(AssignPointerMap(
1295 DefineFixed(new LDivI(dividend, divisor), v0)));
1296 } else {
1297 return DoArithmeticT(Token::DIV, instr);
1298 }
1299}
1300
1301
1302LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1303 if (instr->representation().IsInteger32()) {
1304 ASSERT(instr->left()->representation().IsInteger32());
1305 ASSERT(instr->right()->representation().IsInteger32());
1306
1307 LModI* mod;
1308 if (instr->HasPowerOf2Divisor()) {
1309 ASSERT(!instr->CheckFlag(HValue::kCanBeDivByZero));
1310 LOperand* value = UseRegisterAtStart(instr->left());
1311 mod = new LModI(value, UseOrConstant(instr->right()));
1312 } else {
1313 LOperand* dividend = UseRegister(instr->left());
1314 LOperand* divisor = UseRegister(instr->right());
1315 mod = new LModI(dividend,
1316 divisor,
1317 TempRegister(),
1318 FixedTemp(f20),
1319 FixedTemp(f22));
1320 }
1321
1322 if (instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1323 instr->CheckFlag(HValue::kCanBeDivByZero)) {
1324 return AssignEnvironment(DefineAsRegister(mod));
1325 } else {
1326 return DefineAsRegister(mod);
1327 }
1328 } else if (instr->representation().IsTagged()) {
1329 return DoArithmeticT(Token::MOD, instr);
1330 } else {
1331 ASSERT(instr->representation().IsDouble());
1332 // We call a C function for double modulo. It can't trigger a GC.
1333 // We need to use fixed result register for the call.
1334 // TODO(fschneider): Allow any register as input registers.
1335 LOperand* left = UseFixedDouble(instr->left(), f2);
1336 LOperand* right = UseFixedDouble(instr->right(), f4);
1337 LArithmeticD* result = new LArithmeticD(Token::MOD, left, right);
1338 return MarkAsCall(DefineFixedDouble(result, f2), instr);
1339 }
1340}
1341
1342
1343LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1344 if (instr->representation().IsInteger32()) {
1345 ASSERT(instr->left()->representation().IsInteger32());
1346 ASSERT(instr->right()->representation().IsInteger32());
1347 LOperand* left;
1348 LOperand* right = UseOrConstant(instr->MostConstantOperand());
1349 LOperand* temp = NULL;
1350 if (instr->CheckFlag(HValue::kBailoutOnMinusZero) &&
1351 (instr->CheckFlag(HValue::kCanOverflow) ||
1352 !right->IsConstantOperand())) {
1353 left = UseRegister(instr->LeastConstantOperand());
1354 temp = TempRegister();
1355 } else {
1356 left = UseRegisterAtStart(instr->LeastConstantOperand());
1357 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001358 LMulI* mul = new LMulI(left, right, temp);
1359 if (instr->CheckFlag(HValue::kCanOverflow) ||
1360 instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1361 AssignEnvironment(mul);
1362 }
1363 return DefineAsRegister(mul);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001364
1365 } else if (instr->representation().IsDouble()) {
1366 return DoArithmeticD(Token::MUL, instr);
1367
1368 } else {
1369 return DoArithmeticT(Token::MUL, instr);
1370 }
1371}
1372
1373
1374LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1375 if (instr->representation().IsInteger32()) {
1376 ASSERT(instr->left()->representation().IsInteger32());
1377 ASSERT(instr->right()->representation().IsInteger32());
1378 LOperand* left = UseRegisterAtStart(instr->left());
1379 LOperand* right = UseOrConstantAtStart(instr->right());
1380 LSubI* sub = new LSubI(left, right);
1381 LInstruction* result = DefineAsRegister(sub);
1382 if (instr->CheckFlag(HValue::kCanOverflow)) {
1383 result = AssignEnvironment(result);
1384 }
1385 return result;
1386 } else if (instr->representation().IsDouble()) {
1387 return DoArithmeticD(Token::SUB, instr);
1388 } else {
1389 return DoArithmeticT(Token::SUB, instr);
1390 }
1391}
1392
1393
1394LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1395 if (instr->representation().IsInteger32()) {
1396 ASSERT(instr->left()->representation().IsInteger32());
1397 ASSERT(instr->right()->representation().IsInteger32());
1398 LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1399 LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1400 LAddI* add = new LAddI(left, right);
1401 LInstruction* result = DefineAsRegister(add);
1402 if (instr->CheckFlag(HValue::kCanOverflow)) {
1403 result = AssignEnvironment(result);
1404 }
1405 return result;
1406 } else if (instr->representation().IsDouble()) {
1407 return DoArithmeticD(Token::ADD, instr);
1408 } else {
1409 ASSERT(instr->representation().IsTagged());
1410 return DoArithmeticT(Token::ADD, instr);
1411 }
1412}
1413
1414
1415LInstruction* LChunkBuilder::DoPower(HPower* instr) {
1416 ASSERT(instr->representation().IsDouble());
1417 // We call a C function for double power. It can't trigger a GC.
1418 // We need to use fixed result register for the call.
1419 Representation exponent_type = instr->right()->representation();
1420 ASSERT(instr->left()->representation().IsDouble());
1421 LOperand* left = UseFixedDouble(instr->left(), f2);
1422 LOperand* right = exponent_type.IsDouble() ?
1423 UseFixedDouble(instr->right(), f4) :
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001424 UseFixed(instr->right(), a2);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001425 LPower* result = new LPower(left, right);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001426 return MarkAsCall(DefineFixedDouble(result, f0),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001427 instr,
1428 CAN_DEOPTIMIZE_EAGERLY);
1429}
1430
1431
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001432LInstruction* LChunkBuilder::DoRandom(HRandom* instr) {
1433 ASSERT(instr->representation().IsDouble());
1434 ASSERT(instr->global_object()->representation().IsTagged());
1435 LOperand* global_object = UseFixed(instr->global_object(), a0);
1436 LRandom* result = new LRandom(global_object);
1437 return MarkAsCall(DefineFixedDouble(result, f0), instr);
1438}
1439
1440
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001441LInstruction* LChunkBuilder::DoCompareGeneric(HCompareGeneric* instr) {
1442 Representation r = instr->GetInputRepresentation();
1443 ASSERT(instr->left()->representation().IsTagged());
1444 ASSERT(instr->right()->representation().IsTagged());
1445 LOperand* left = UseFixed(instr->left(), a1);
1446 LOperand* right = UseFixed(instr->right(), a0);
1447 LCmpT* result = new LCmpT(left, right);
1448 return MarkAsCall(DefineFixed(result, v0), instr);
1449}
1450
1451
1452LInstruction* LChunkBuilder::DoCompareIDAndBranch(
1453 HCompareIDAndBranch* instr) {
1454 Representation r = instr->GetInputRepresentation();
1455 if (r.IsInteger32()) {
1456 ASSERT(instr->left()->representation().IsInteger32());
1457 ASSERT(instr->right()->representation().IsInteger32());
1458 LOperand* left = UseRegisterOrConstantAtStart(instr->left());
1459 LOperand* right = UseRegisterOrConstantAtStart(instr->right());
1460 return new LCmpIDAndBranch(left, right);
1461 } else {
1462 ASSERT(r.IsDouble());
1463 ASSERT(instr->left()->representation().IsDouble());
1464 ASSERT(instr->right()->representation().IsDouble());
1465 LOperand* left = UseRegisterAtStart(instr->left());
1466 LOperand* right = UseRegisterAtStart(instr->right());
1467 return new LCmpIDAndBranch(left, right);
1468 }
1469}
1470
1471
1472LInstruction* LChunkBuilder::DoCompareObjectEqAndBranch(
1473 HCompareObjectEqAndBranch* instr) {
1474 LOperand* left = UseRegisterAtStart(instr->left());
1475 LOperand* right = UseRegisterAtStart(instr->right());
1476 return new LCmpObjectEqAndBranch(left, right);
1477}
1478
1479
1480LInstruction* LChunkBuilder::DoCompareConstantEqAndBranch(
1481 HCompareConstantEqAndBranch* instr) {
1482 return new LCmpConstantEqAndBranch(UseRegisterAtStart(instr->value()));
1483}
1484
1485
1486LInstruction* LChunkBuilder::DoIsNilAndBranch(HIsNilAndBranch* instr) {
1487 ASSERT(instr->value()->representation().IsTagged());
1488 return new LIsNilAndBranch(UseRegisterAtStart(instr->value()));
1489}
1490
1491
1492LInstruction* LChunkBuilder::DoIsObjectAndBranch(HIsObjectAndBranch* instr) {
1493 ASSERT(instr->value()->representation().IsTagged());
1494 LOperand* temp = TempRegister();
1495 return new LIsObjectAndBranch(UseRegisterAtStart(instr->value()), temp);
1496}
1497
1498
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001499LInstruction* LChunkBuilder::DoIsStringAndBranch(HIsStringAndBranch* instr) {
1500 ASSERT(instr->value()->representation().IsTagged());
1501 LOperand* temp = TempRegister();
1502 return new LIsStringAndBranch(UseRegisterAtStart(instr->value()), temp);
1503}
1504
1505
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001506LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
1507 ASSERT(instr->value()->representation().IsTagged());
1508 return new LIsSmiAndBranch(Use(instr->value()));
1509}
1510
1511
1512LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
1513 HIsUndetectableAndBranch* instr) {
1514 ASSERT(instr->value()->representation().IsTagged());
1515 return new LIsUndetectableAndBranch(UseRegisterAtStart(instr->value()),
1516 TempRegister());
1517}
1518
1519
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001520LInstruction* LChunkBuilder::DoStringCompareAndBranch(
1521 HStringCompareAndBranch* instr) {
1522 ASSERT(instr->left()->representation().IsTagged());
1523 ASSERT(instr->right()->representation().IsTagged());
1524 LOperand* left = UseFixed(instr->left(), a1);
1525 LOperand* right = UseFixed(instr->right(), a0);
1526 LStringCompareAndBranch* result = new LStringCompareAndBranch(left, right);
1527 return MarkAsCall(result, instr);
1528}
1529
1530
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001531LInstruction* LChunkBuilder::DoHasInstanceTypeAndBranch(
1532 HHasInstanceTypeAndBranch* instr) {
1533 ASSERT(instr->value()->representation().IsTagged());
1534 return new LHasInstanceTypeAndBranch(UseRegisterAtStart(instr->value()));
1535}
1536
1537
1538LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
1539 HGetCachedArrayIndex* instr) {
1540 ASSERT(instr->value()->representation().IsTagged());
1541 LOperand* value = UseRegisterAtStart(instr->value());
1542
1543 return DefineAsRegister(new LGetCachedArrayIndex(value));
1544}
1545
1546
1547LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
1548 HHasCachedArrayIndexAndBranch* instr) {
1549 ASSERT(instr->value()->representation().IsTagged());
1550 return new LHasCachedArrayIndexAndBranch(
1551 UseRegisterAtStart(instr->value()));
1552}
1553
1554
1555LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
1556 HClassOfTestAndBranch* instr) {
1557 ASSERT(instr->value()->representation().IsTagged());
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001558 return new LClassOfTestAndBranch(UseRegister(instr->value()),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001559 TempRegister());
1560}
1561
1562
1563LInstruction* LChunkBuilder::DoJSArrayLength(HJSArrayLength* instr) {
1564 LOperand* array = UseRegisterAtStart(instr->value());
1565 return DefineAsRegister(new LJSArrayLength(array));
1566}
1567
1568
1569LInstruction* LChunkBuilder::DoFixedArrayBaseLength(
1570 HFixedArrayBaseLength* instr) {
1571 LOperand* array = UseRegisterAtStart(instr->value());
1572 return DefineAsRegister(new LFixedArrayBaseLength(array));
1573}
1574
1575
1576LInstruction* LChunkBuilder::DoElementsKind(HElementsKind* instr) {
1577 LOperand* object = UseRegisterAtStart(instr->value());
1578 return DefineAsRegister(new LElementsKind(object));
1579}
1580
1581
1582LInstruction* LChunkBuilder::DoValueOf(HValueOf* instr) {
1583 LOperand* object = UseRegister(instr->value());
1584 LValueOf* result = new LValueOf(object, TempRegister());
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001585 return DefineAsRegister(result);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001586}
1587
1588
1589LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
1590 return AssignEnvironment(new LBoundsCheck(UseRegisterAtStart(instr->index()),
1591 UseRegister(instr->length())));
1592}
1593
1594
1595LInstruction* LChunkBuilder::DoAbnormalExit(HAbnormalExit* instr) {
1596 // The control instruction marking the end of a block that completed
1597 // abruptly (e.g., threw an exception). There is nothing specific to do.
1598 return NULL;
1599}
1600
1601
1602LInstruction* LChunkBuilder::DoThrow(HThrow* instr) {
1603 LOperand* value = UseFixed(instr->value(), a0);
1604 return MarkAsCall(new LThrow(value), instr);
1605}
1606
1607
1608LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
1609 return NULL;
1610}
1611
1612
1613LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
1614 // All HForceRepresentation instructions should be eliminated in the
1615 // representation change phase of Hydrogen.
1616 UNREACHABLE();
1617 return NULL;
1618}
1619
1620
1621LInstruction* LChunkBuilder::DoChange(HChange* instr) {
1622 Representation from = instr->from();
1623 Representation to = instr->to();
1624 if (from.IsTagged()) {
1625 if (to.IsDouble()) {
1626 LOperand* value = UseRegister(instr->value());
1627 LNumberUntagD* res = new LNumberUntagD(value);
1628 return AssignEnvironment(DefineAsRegister(res));
1629 } else {
1630 ASSERT(to.IsInteger32());
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001631 LOperand* value = UseRegisterAtStart(instr->value());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001632 bool needs_check = !instr->value()->type().IsSmi();
1633 LInstruction* res = NULL;
1634 if (!needs_check) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001635 res = DefineAsRegister(new LSmiUntag(value, needs_check));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001636 } else {
1637 LOperand* temp1 = TempRegister();
1638 LOperand* temp2 = instr->CanTruncateToInt32() ? TempRegister()
1639 : NULL;
1640 LOperand* temp3 = instr->CanTruncateToInt32() ? FixedTemp(f22)
1641 : NULL;
1642 res = DefineSameAsFirst(new LTaggedToI(value, temp1, temp2, temp3));
1643 res = AssignEnvironment(res);
1644 }
1645 return res;
1646 }
1647 } else if (from.IsDouble()) {
1648 if (to.IsTagged()) {
1649 LOperand* value = UseRegister(instr->value());
1650 LOperand* temp1 = TempRegister();
1651 LOperand* temp2 = TempRegister();
1652
1653 // Make sure that the temp and result_temp registers are
1654 // different.
1655 LUnallocated* result_temp = TempRegister();
1656 LNumberTagD* result = new LNumberTagD(value, temp1, temp2);
1657 Define(result, result_temp);
1658 return AssignPointerMap(result);
1659 } else {
1660 ASSERT(to.IsInteger32());
1661 LOperand* value = UseRegister(instr->value());
1662 LDoubleToI* res =
1663 new LDoubleToI(value,
1664 TempRegister(),
1665 instr->CanTruncateToInt32() ? TempRegister() : NULL);
1666 return AssignEnvironment(DefineAsRegister(res));
1667 }
1668 } else if (from.IsInteger32()) {
1669 if (to.IsTagged()) {
1670 HValue* val = instr->value();
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001671 LOperand* value = UseRegisterAtStart(val);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001672 if (val->HasRange() && val->range()->IsInSmiRange()) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001673 return DefineAsRegister(new LSmiTag(value));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001674 } else {
1675 LNumberTagI* result = new LNumberTagI(value);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001676 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001677 }
1678 } else {
1679 ASSERT(to.IsDouble());
1680 LOperand* value = Use(instr->value());
1681 return DefineAsRegister(new LInteger32ToDouble(value));
1682 }
1683 }
1684 UNREACHABLE();
1685 return NULL;
1686}
1687
1688
1689LInstruction* LChunkBuilder::DoCheckNonSmi(HCheckNonSmi* instr) {
1690 LOperand* value = UseRegisterAtStart(instr->value());
1691 return AssignEnvironment(new LCheckNonSmi(value));
1692}
1693
1694
1695LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
1696 LOperand* value = UseRegisterAtStart(instr->value());
1697 LInstruction* result = new LCheckInstanceType(value);
1698 return AssignEnvironment(result);
1699}
1700
1701
1702LInstruction* LChunkBuilder::DoCheckPrototypeMaps(HCheckPrototypeMaps* instr) {
1703 LOperand* temp1 = TempRegister();
1704 LOperand* temp2 = TempRegister();
1705 LInstruction* result = new LCheckPrototypeMaps(temp1, temp2);
1706 return AssignEnvironment(result);
1707}
1708
1709
1710LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
1711 LOperand* value = UseRegisterAtStart(instr->value());
1712 return AssignEnvironment(new LCheckSmi(value));
1713}
1714
1715
1716LInstruction* LChunkBuilder::DoCheckFunction(HCheckFunction* instr) {
1717 LOperand* value = UseRegisterAtStart(instr->value());
1718 return AssignEnvironment(new LCheckFunction(value));
1719}
1720
1721
1722LInstruction* LChunkBuilder::DoCheckMap(HCheckMap* instr) {
1723 LOperand* value = UseRegisterAtStart(instr->value());
1724 LInstruction* result = new LCheckMap(value);
1725 return AssignEnvironment(result);
1726}
1727
1728
1729LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
1730 HValue* value = instr->value();
1731 Representation input_rep = value->representation();
1732 LOperand* reg = UseRegister(value);
1733 if (input_rep.IsDouble()) {
1734 // Revisit this decision, here and 8 lines below.
1735 return DefineAsRegister(new LClampDToUint8(reg, FixedTemp(f22)));
1736 } else if (input_rep.IsInteger32()) {
1737 return DefineAsRegister(new LClampIToUint8(reg));
1738 } else {
1739 ASSERT(input_rep.IsTagged());
1740 // Register allocator doesn't (yet) support allocation of double
1741 // temps. Reserve f22 explicitly.
1742 LClampTToUint8* result = new LClampTToUint8(reg, FixedTemp(f22));
1743 return AssignEnvironment(DefineAsRegister(result));
1744 }
1745}
1746
1747
1748LInstruction* LChunkBuilder::DoToInt32(HToInt32* instr) {
1749 HValue* value = instr->value();
1750 Representation input_rep = value->representation();
1751 LOperand* reg = UseRegister(value);
1752 if (input_rep.IsDouble()) {
1753 LOperand* temp1 = TempRegister();
1754 LOperand* temp2 = TempRegister();
1755 LDoubleToI* res = new LDoubleToI(reg, temp1, temp2);
1756 return AssignEnvironment(DefineAsRegister(res));
1757 } else if (input_rep.IsInteger32()) {
1758 // Canonicalization should already have removed the hydrogen instruction in
1759 // this case, since it is a noop.
1760 UNREACHABLE();
1761 return NULL;
1762 } else {
1763 ASSERT(input_rep.IsTagged());
1764 LOperand* temp1 = TempRegister();
1765 LOperand* temp2 = TempRegister();
1766 LOperand* temp3 = FixedTemp(f22);
1767 LTaggedToI* res = new LTaggedToI(reg, temp1, temp2, temp3);
1768 return AssignEnvironment(DefineSameAsFirst(res));
1769 }
1770}
1771
1772
1773LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
1774 return new LReturn(UseFixed(instr->value(), v0));
1775}
1776
1777
1778LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
1779 Representation r = instr->representation();
1780 if (r.IsInteger32()) {
1781 return DefineAsRegister(new LConstantI);
1782 } else if (r.IsDouble()) {
1783 return DefineAsRegister(new LConstantD);
1784 } else if (r.IsTagged()) {
1785 return DefineAsRegister(new LConstantT);
1786 } else {
1787 UNREACHABLE();
1788 return NULL;
1789 }
1790}
1791
1792
1793LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
1794 LLoadGlobalCell* result = new LLoadGlobalCell;
1795 return instr->RequiresHoleCheck()
1796 ? AssignEnvironment(DefineAsRegister(result))
1797 : DefineAsRegister(result);
1798}
1799
1800
1801LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
1802 LOperand* global_object = UseFixed(instr->global_object(), a0);
1803 LLoadGlobalGeneric* result = new LLoadGlobalGeneric(global_object);
1804 return MarkAsCall(DefineFixed(result, v0), instr);
1805}
1806
1807
1808LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
danno@chromium.orge78f9fc2011-12-21 08:29:34 +00001809 LOperand* value = UseRegister(instr->value());
1810 // Use a temp to check the value in the cell in the case where we perform
1811 // a hole check.
1812 return instr->RequiresHoleCheck()
1813 ? AssignEnvironment(new LStoreGlobalCell(value, TempRegister()))
1814 : new LStoreGlobalCell(value, NULL);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001815}
1816
1817
1818LInstruction* LChunkBuilder::DoStoreGlobalGeneric(HStoreGlobalGeneric* instr) {
1819 LOperand* global_object = UseFixed(instr->global_object(), a1);
1820 LOperand* value = UseFixed(instr->value(), a0);
1821 LStoreGlobalGeneric* result =
1822 new LStoreGlobalGeneric(global_object, value);
1823 return MarkAsCall(result, instr);
1824}
1825
1826
1827LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
1828 LOperand* context = UseRegisterAtStart(instr->value());
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001829 LInstruction* result = DefineAsRegister(new LLoadContextSlot(context));
1830 return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001831}
1832
1833
1834LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
1835 LOperand* context;
1836 LOperand* value;
1837 if (instr->NeedsWriteBarrier()) {
1838 context = UseTempRegister(instr->context());
1839 value = UseTempRegister(instr->value());
1840 } else {
1841 context = UseRegister(instr->context());
1842 value = UseRegister(instr->value());
1843 }
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001844 LInstruction* result = new LStoreContextSlot(context, value);
1845 return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001846}
1847
1848
1849LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
1850 return DefineAsRegister(
1851 new LLoadNamedField(UseRegisterAtStart(instr->object())));
1852}
1853
1854
1855LInstruction* LChunkBuilder::DoLoadNamedFieldPolymorphic(
1856 HLoadNamedFieldPolymorphic* instr) {
1857 ASSERT(instr->representation().IsTagged());
1858 if (instr->need_generic()) {
1859 LOperand* obj = UseFixed(instr->object(), a0);
1860 LLoadNamedFieldPolymorphic* result = new LLoadNamedFieldPolymorphic(obj);
1861 return MarkAsCall(DefineFixed(result, v0), instr);
1862 } else {
1863 LOperand* obj = UseRegisterAtStart(instr->object());
1864 LLoadNamedFieldPolymorphic* result = new LLoadNamedFieldPolymorphic(obj);
1865 return AssignEnvironment(DefineAsRegister(result));
1866 }
1867}
1868
1869
1870LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
1871 LOperand* object = UseFixed(instr->object(), a0);
1872 LInstruction* result = DefineFixed(new LLoadNamedGeneric(object), v0);
1873 return MarkAsCall(result, instr);
1874}
1875
1876
1877LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
1878 HLoadFunctionPrototype* instr) {
1879 return AssignEnvironment(DefineAsRegister(
1880 new LLoadFunctionPrototype(UseRegister(instr->function()))));
1881}
1882
1883
1884LInstruction* LChunkBuilder::DoLoadElements(HLoadElements* instr) {
1885 LOperand* input = UseRegisterAtStart(instr->value());
1886 return DefineAsRegister(new LLoadElements(input));
1887}
1888
1889
1890LInstruction* LChunkBuilder::DoLoadExternalArrayPointer(
1891 HLoadExternalArrayPointer* instr) {
1892 LOperand* input = UseRegisterAtStart(instr->value());
1893 return DefineAsRegister(new LLoadExternalArrayPointer(input));
1894}
1895
1896
1897LInstruction* LChunkBuilder::DoLoadKeyedFastElement(
1898 HLoadKeyedFastElement* instr) {
1899 ASSERT(instr->representation().IsTagged());
1900 ASSERT(instr->key()->representation().IsInteger32());
1901 LOperand* obj = UseRegisterAtStart(instr->object());
1902 LOperand* key = UseRegisterAtStart(instr->key());
1903 LLoadKeyedFastElement* result = new LLoadKeyedFastElement(obj, key);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001904 if (instr->RequiresHoleCheck()) AssignEnvironment(result);
1905 return DefineAsRegister(result);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001906}
1907
1908
1909LInstruction* LChunkBuilder::DoLoadKeyedFastDoubleElement(
1910 HLoadKeyedFastDoubleElement* instr) {
1911 ASSERT(instr->representation().IsDouble());
1912 ASSERT(instr->key()->representation().IsInteger32());
1913 LOperand* elements = UseTempRegister(instr->elements());
1914 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1915 LLoadKeyedFastDoubleElement* result =
1916 new LLoadKeyedFastDoubleElement(elements, key);
1917 return AssignEnvironment(DefineAsRegister(result));
1918}
1919
1920
1921LInstruction* LChunkBuilder::DoLoadKeyedSpecializedArrayElement(
1922 HLoadKeyedSpecializedArrayElement* instr) {
1923 ElementsKind elements_kind = instr->elements_kind();
1924 Representation representation(instr->representation());
1925 ASSERT(
1926 (representation.IsInteger32() &&
1927 (elements_kind != EXTERNAL_FLOAT_ELEMENTS) &&
1928 (elements_kind != EXTERNAL_DOUBLE_ELEMENTS)) ||
1929 (representation.IsDouble() &&
1930 ((elements_kind == EXTERNAL_FLOAT_ELEMENTS) ||
1931 (elements_kind == EXTERNAL_DOUBLE_ELEMENTS))));
1932 ASSERT(instr->key()->representation().IsInteger32());
1933 LOperand* external_pointer = UseRegister(instr->external_pointer());
1934 LOperand* key = UseRegisterOrConstant(instr->key());
1935 LLoadKeyedSpecializedArrayElement* result =
1936 new LLoadKeyedSpecializedArrayElement(external_pointer, key);
1937 LInstruction* load_instr = DefineAsRegister(result);
1938 // An unsigned int array load might overflow and cause a deopt, make sure it
1939 // has an environment.
1940 return (elements_kind == EXTERNAL_UNSIGNED_INT_ELEMENTS) ?
1941 AssignEnvironment(load_instr) : load_instr;
1942}
1943
1944
1945LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
1946 LOperand* object = UseFixed(instr->object(), a1);
1947 LOperand* key = UseFixed(instr->key(), a0);
1948
1949 LInstruction* result =
1950 DefineFixed(new LLoadKeyedGeneric(object, key), v0);
1951 return MarkAsCall(result, instr);
1952}
1953
1954
1955LInstruction* LChunkBuilder::DoStoreKeyedFastElement(
1956 HStoreKeyedFastElement* instr) {
1957 bool needs_write_barrier = instr->NeedsWriteBarrier();
1958 ASSERT(instr->value()->representation().IsTagged());
1959 ASSERT(instr->object()->representation().IsTagged());
1960 ASSERT(instr->key()->representation().IsInteger32());
1961
1962 LOperand* obj = UseTempRegister(instr->object());
1963 LOperand* val = needs_write_barrier
1964 ? UseTempRegister(instr->value())
1965 : UseRegisterAtStart(instr->value());
1966 LOperand* key = needs_write_barrier
1967 ? UseTempRegister(instr->key())
1968 : UseRegisterOrConstantAtStart(instr->key());
danno@chromium.orgbf0c8202011-12-27 10:09:42 +00001969 return new LStoreKeyedFastElement(obj, key, val);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001970}
1971
1972
1973LInstruction* LChunkBuilder::DoStoreKeyedFastDoubleElement(
1974 HStoreKeyedFastDoubleElement* instr) {
1975 ASSERT(instr->value()->representation().IsDouble());
1976 ASSERT(instr->elements()->representation().IsTagged());
1977 ASSERT(instr->key()->representation().IsInteger32());
1978
1979 LOperand* elements = UseRegisterAtStart(instr->elements());
1980 LOperand* val = UseTempRegister(instr->value());
1981 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1982
1983 return new LStoreKeyedFastDoubleElement(elements, key, val);
1984}
1985
1986
1987LInstruction* LChunkBuilder::DoStoreKeyedSpecializedArrayElement(
1988 HStoreKeyedSpecializedArrayElement* instr) {
1989 Representation representation(instr->value()->representation());
1990 ElementsKind elements_kind = instr->elements_kind();
1991 ASSERT(
1992 (representation.IsInteger32() &&
1993 (elements_kind != EXTERNAL_FLOAT_ELEMENTS) &&
1994 (elements_kind != EXTERNAL_DOUBLE_ELEMENTS)) ||
1995 (representation.IsDouble() &&
1996 ((elements_kind == EXTERNAL_FLOAT_ELEMENTS) ||
1997 (elements_kind == EXTERNAL_DOUBLE_ELEMENTS))));
1998 ASSERT(instr->external_pointer()->representation().IsExternal());
1999 ASSERT(instr->key()->representation().IsInteger32());
2000
2001 LOperand* external_pointer = UseRegister(instr->external_pointer());
2002 bool val_is_temp_register =
2003 elements_kind == EXTERNAL_PIXEL_ELEMENTS ||
2004 elements_kind == EXTERNAL_FLOAT_ELEMENTS;
2005 LOperand* val = val_is_temp_register
2006 ? UseTempRegister(instr->value())
2007 : UseRegister(instr->value());
2008 LOperand* key = UseRegisterOrConstant(instr->key());
2009
2010 return new LStoreKeyedSpecializedArrayElement(external_pointer,
2011 key,
2012 val);
2013}
2014
2015
2016LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
2017 LOperand* obj = UseFixed(instr->object(), a2);
2018 LOperand* key = UseFixed(instr->key(), a1);
2019 LOperand* val = UseFixed(instr->value(), a0);
2020
2021 ASSERT(instr->object()->representation().IsTagged());
2022 ASSERT(instr->key()->representation().IsTagged());
2023 ASSERT(instr->value()->representation().IsTagged());
2024
2025 return MarkAsCall(new LStoreKeyedGeneric(obj, key, val), instr);
2026}
2027
2028
2029LInstruction* LChunkBuilder::DoTransitionElementsKind(
2030 HTransitionElementsKind* instr) {
2031 if (instr->original_map()->elements_kind() == FAST_SMI_ONLY_ELEMENTS &&
2032 instr->transitioned_map()->elements_kind() == FAST_ELEMENTS) {
2033 LOperand* object = UseRegister(instr->object());
2034 LOperand* new_map_reg = TempRegister();
2035 LTransitionElementsKind* result =
2036 new LTransitionElementsKind(object, new_map_reg, NULL);
2037 return DefineSameAsFirst(result);
2038 } else {
2039 LOperand* object = UseFixed(instr->object(), a0);
2040 LOperand* fixed_object_reg = FixedTemp(a2);
2041 LOperand* new_map_reg = FixedTemp(a3);
2042 LTransitionElementsKind* result =
2043 new LTransitionElementsKind(object, new_map_reg, fixed_object_reg);
2044 return MarkAsCall(DefineFixed(result, v0), instr);
2045 }
2046}
2047
2048
2049LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
2050 bool needs_write_barrier = instr->NeedsWriteBarrier();
2051
2052 LOperand* obj = needs_write_barrier
2053 ? UseTempRegister(instr->object())
2054 : UseRegisterAtStart(instr->object());
2055
2056 LOperand* val = needs_write_barrier
2057 ? UseTempRegister(instr->value())
2058 : UseRegister(instr->value());
2059
2060 return new LStoreNamedField(obj, val);
2061}
2062
2063
2064LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
2065 LOperand* obj = UseFixed(instr->object(), a1);
2066 LOperand* val = UseFixed(instr->value(), a0);
2067
2068 LInstruction* result = new LStoreNamedGeneric(obj, val);
2069 return MarkAsCall(result, instr);
2070}
2071
2072
2073LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
2074 LOperand* left = UseRegisterAtStart(instr->left());
2075 LOperand* right = UseRegisterAtStart(instr->right());
2076 return MarkAsCall(DefineFixed(new LStringAdd(left, right), v0), instr);
2077}
2078
2079
2080LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
2081 LOperand* string = UseTempRegister(instr->string());
2082 LOperand* index = UseTempRegister(instr->index());
2083 LStringCharCodeAt* result = new LStringCharCodeAt(string, index);
2084 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
2085}
2086
2087
2088LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2089 LOperand* char_code = UseRegister(instr->value());
2090 LStringCharFromCode* result = new LStringCharFromCode(char_code);
2091 return AssignPointerMap(DefineAsRegister(result));
2092}
2093
2094
2095LInstruction* LChunkBuilder::DoStringLength(HStringLength* instr) {
2096 LOperand* string = UseRegisterAtStart(instr->value());
2097 return DefineAsRegister(new LStringLength(string));
2098}
2099
2100
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002101LInstruction* LChunkBuilder::DoFastLiteral(HFastLiteral* instr) {
2102 return MarkAsCall(DefineFixed(new LFastLiteral, v0), instr);
2103}
2104
2105
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002106LInstruction* LChunkBuilder::DoArrayLiteral(HArrayLiteral* instr) {
2107 return MarkAsCall(DefineFixed(new LArrayLiteral, v0), instr);
2108}
2109
2110
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002111LInstruction* LChunkBuilder::DoObjectLiteral(HObjectLiteral* instr) {
2112 return MarkAsCall(DefineFixed(new LObjectLiteral, v0), instr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002113}
2114
2115
2116LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2117 return MarkAsCall(DefineFixed(new LRegExpLiteral, v0), instr);
2118}
2119
2120
2121LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2122 return MarkAsCall(DefineFixed(new LFunctionLiteral, v0), instr);
2123}
2124
2125
2126LInstruction* LChunkBuilder::DoDeleteProperty(HDeleteProperty* instr) {
2127 LOperand* object = UseFixed(instr->object(), a0);
2128 LOperand* key = UseFixed(instr->key(), a1);
2129 LDeleteProperty* result = new LDeleteProperty(object, key);
2130 return MarkAsCall(DefineFixed(result, v0), instr);
2131}
2132
2133
2134LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2135 allocator_->MarkAsOsrEntry();
2136 current_block_->last_environment()->set_ast_id(instr->ast_id());
2137 return AssignEnvironment(new LOsrEntry);
2138}
2139
2140
2141LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2142 int spill_index = chunk()->GetParameterStackSlot(instr->index());
2143 return DefineAsSpilled(new LParameter, spill_index);
2144}
2145
2146
2147LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2148 int spill_index = chunk()->GetNextSpillIndex(false); // Not double-width.
2149 if (spill_index > LUnallocated::kMaxFixedIndex) {
2150 Abort("Too many spill slots needed for OSR");
2151 spill_index = 0;
2152 }
2153 return DefineAsSpilled(new LUnknownOSRValue, spill_index);
2154}
2155
2156
2157LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2158 argument_count_ -= instr->argument_count();
2159 return MarkAsCall(DefineFixed(new LCallStub, v0), instr);
2160}
2161
2162
2163LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
2164 // There are no real uses of the arguments object.
2165 // arguments.length and element access are supported directly on
2166 // stack arguments, and any real arguments object use causes a bailout.
2167 // So this value is never used.
2168 return NULL;
2169}
2170
2171
2172LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2173 LOperand* arguments = UseRegister(instr->arguments());
2174 LOperand* length = UseTempRegister(instr->length());
2175 LOperand* index = UseRegister(instr->index());
2176 LAccessArgumentsAt* result = new LAccessArgumentsAt(arguments, length, index);
2177 return AssignEnvironment(DefineAsRegister(result));
2178}
2179
2180
2181LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2182 LOperand* object = UseFixed(instr->value(), a0);
2183 LToFastProperties* result = new LToFastProperties(object);
2184 return MarkAsCall(DefineFixed(result, v0), instr);
2185}
2186
2187
2188LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2189 LTypeof* result = new LTypeof(UseFixed(instr->value(), a0));
2190 return MarkAsCall(DefineFixed(result, v0), instr);
2191}
2192
2193
2194LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2195 return new LTypeofIsAndBranch(UseTempRegister(instr->value()));
2196}
2197
2198
2199LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
2200 HIsConstructCallAndBranch* instr) {
2201 return new LIsConstructCallAndBranch(TempRegister());
2202}
2203
2204
2205LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2206 HEnvironment* env = current_block_->last_environment();
2207 ASSERT(env != NULL);
2208
2209 env->set_ast_id(instr->ast_id());
2210
2211 env->Drop(instr->pop_count());
2212 for (int i = 0; i < instr->values()->length(); ++i) {
2213 HValue* value = instr->values()->at(i);
2214 if (instr->HasAssignedIndexAt(i)) {
2215 env->Bind(instr->GetAssignedIndexAt(i), value);
2216 } else {
2217 env->Push(value);
2218 }
2219 }
2220
2221 // If there is an instruction pending deoptimization environment create a
2222 // lazy bailout instruction to capture the environment.
2223 if (pending_deoptimization_ast_id_ == instr->ast_id()) {
2224 LInstruction* result = new LLazyBailout;
2225 result = AssignEnvironment(result);
2226 instruction_pending_deoptimization_environment_->
2227 set_deoptimization_environment(result->environment());
2228 ClearInstructionPendingDeoptimizationEnvironment();
2229 return result;
2230 }
2231
2232 return NULL;
2233}
2234
2235
2236LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2237 if (instr->is_function_entry()) {
2238 return MarkAsCall(new LStackCheck, instr);
2239 } else {
2240 ASSERT(instr->is_backwards_branch());
2241 return AssignEnvironment(AssignPointerMap(new LStackCheck));
2242 }
2243}
2244
2245
2246LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2247 HEnvironment* outer = current_block_->last_environment();
2248 HConstant* undefined = graph()->GetConstantUndefined();
2249 HEnvironment* inner = outer->CopyForInlining(instr->closure(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002250 instr->arguments_count(),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002251 instr->function(),
2252 undefined,
2253 instr->call_kind());
2254 current_block_->UpdateEnvironment(inner);
2255 chunk_->AddInlinedClosure(instr->closure());
2256 return NULL;
2257}
2258
2259
2260LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002261 HEnvironment* outer = current_block_->last_environment()->
2262 DiscardInlined(false);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002263 current_block_->UpdateEnvironment(outer);
2264 return NULL;
2265}
2266
2267
2268LInstruction* LChunkBuilder::DoIn(HIn* instr) {
2269 LOperand* key = UseRegisterAtStart(instr->key());
2270 LOperand* object = UseRegisterAtStart(instr->object());
2271 LIn* result = new LIn(key, object);
2272 return MarkAsCall(DefineFixed(result, v0), instr);
2273}
2274
2275
2276} } // namespace v8::internal