blob: 3316c960781f48bdcbe49979cf198d1c9f055827 [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 }
674 allocator_->RecordUse(value, operand);
675 return operand;
676}
677
678
679template<int I, int T>
680LInstruction* LChunkBuilder::Define(LTemplateInstruction<1, I, T>* instr,
681 LUnallocated* result) {
682 allocator_->RecordDefinition(current_instruction_, result);
683 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);
794 allocator_->RecordTemporary(operand);
795 return operand;
796}
797
798
799LOperand* LChunkBuilder::FixedTemp(Register reg) {
800 LUnallocated* operand = ToUnallocated(reg);
801 allocator_->RecordTemporary(operand);
802 return operand;
803}
804
805
806LOperand* LChunkBuilder::FixedTemp(DoubleRegister reg) {
807 LUnallocated* operand = ToUnallocated(reg);
808 allocator_->RecordTemporary(operand);
809 return operand;
810}
811
812
813LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
814 return new LLabel(instr->block());
815}
816
817
818LInstruction* LChunkBuilder::DoSoftDeoptimize(HSoftDeoptimize* instr) {
819 return AssignEnvironment(new LDeoptimize);
820}
821
822
823LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
824 return AssignEnvironment(new LDeoptimize);
825}
826
827
828LInstruction* LChunkBuilder::DoShift(Token::Value op,
829 HBitwiseBinaryOperation* instr) {
830 if (instr->representation().IsTagged()) {
831 ASSERT(instr->left()->representation().IsTagged());
832 ASSERT(instr->right()->representation().IsTagged());
833
834 LOperand* left = UseFixed(instr->left(), a1);
835 LOperand* right = UseFixed(instr->right(), a0);
836 LArithmeticT* result = new LArithmeticT(op, left, right);
837 return MarkAsCall(DefineFixed(result, v0), instr);
838 }
839
840 ASSERT(instr->representation().IsInteger32());
841 ASSERT(instr->left()->representation().IsInteger32());
842 ASSERT(instr->right()->representation().IsInteger32());
843 LOperand* left = UseRegisterAtStart(instr->left());
844
845 HValue* right_value = instr->right();
846 LOperand* right = NULL;
847 int constant_value = 0;
848 if (right_value->IsConstant()) {
849 HConstant* constant = HConstant::cast(right_value);
850 right = chunk_->DefineConstantOperand(constant);
851 constant_value = constant->Integer32Value() & 0x1f;
852 } else {
853 right = UseRegisterAtStart(right_value);
854 }
855
856 // Shift operations can only deoptimize if we do a logical shift
857 // by 0 and the result cannot be truncated to int32.
858 bool may_deopt = (op == Token::SHR && constant_value == 0);
859 bool does_deopt = false;
860 if (may_deopt) {
861 for (HUseIterator it(instr->uses()); !it.Done(); it.Advance()) {
862 if (!it.value()->CheckFlag(HValue::kTruncatingToInt32)) {
863 does_deopt = true;
864 break;
865 }
866 }
867 }
868
869 LInstruction* result =
870 DefineAsRegister(new LShiftI(op, left, right, does_deopt));
871 return does_deopt ? AssignEnvironment(result) : result;
872}
873
874
875LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
876 HArithmeticBinaryOperation* instr) {
877 ASSERT(instr->representation().IsDouble());
878 ASSERT(instr->left()->representation().IsDouble());
879 ASSERT(instr->right()->representation().IsDouble());
880 ASSERT(op != Token::MOD);
881 LOperand* left = UseRegisterAtStart(instr->left());
882 LOperand* right = UseRegisterAtStart(instr->right());
883 LArithmeticD* result = new LArithmeticD(op, left, right);
884 return DefineAsRegister(result);
885}
886
887
888LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
889 HArithmeticBinaryOperation* instr) {
890 ASSERT(op == Token::ADD ||
891 op == Token::DIV ||
892 op == Token::MOD ||
893 op == Token::MUL ||
894 op == Token::SUB);
895 HValue* left = instr->left();
896 HValue* right = instr->right();
897 ASSERT(left->representation().IsTagged());
898 ASSERT(right->representation().IsTagged());
899 LOperand* left_operand = UseFixed(left, a1);
900 LOperand* right_operand = UseFixed(right, a0);
901 LArithmeticT* result = new LArithmeticT(op, left_operand, right_operand);
902 return MarkAsCall(DefineFixed(result, v0), instr);
903}
904
905
906void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
907 ASSERT(is_building());
908 current_block_ = block;
909 next_block_ = next_block;
910 if (block->IsStartBlock()) {
911 block->UpdateEnvironment(graph_->start_environment());
912 argument_count_ = 0;
913 } else if (block->predecessors()->length() == 1) {
914 // We have a single predecessor => copy environment and outgoing
915 // argument count from the predecessor.
916 ASSERT(block->phis()->length() == 0);
917 HBasicBlock* pred = block->predecessors()->at(0);
918 HEnvironment* last_environment = pred->last_environment();
919 ASSERT(last_environment != NULL);
920 // Only copy the environment, if it is later used again.
921 if (pred->end()->SecondSuccessor() == NULL) {
922 ASSERT(pred->end()->FirstSuccessor() == block);
923 } else {
924 if (pred->end()->FirstSuccessor()->block_id() > block->block_id() ||
925 pred->end()->SecondSuccessor()->block_id() > block->block_id()) {
926 last_environment = last_environment->Copy();
927 }
928 }
929 block->UpdateEnvironment(last_environment);
930 ASSERT(pred->argument_count() >= 0);
931 argument_count_ = pred->argument_count();
932 } else {
933 // We are at a state join => process phis.
934 HBasicBlock* pred = block->predecessors()->at(0);
935 // No need to copy the environment, it cannot be used later.
936 HEnvironment* last_environment = pred->last_environment();
937 for (int i = 0; i < block->phis()->length(); ++i) {
938 HPhi* phi = block->phis()->at(i);
939 last_environment->SetValueAt(phi->merged_index(), phi);
940 }
941 for (int i = 0; i < block->deleted_phis()->length(); ++i) {
942 last_environment->SetValueAt(block->deleted_phis()->at(i),
943 graph_->GetConstantUndefined());
944 }
945 block->UpdateEnvironment(last_environment);
946 // Pick up the outgoing argument count of one of the predecessors.
947 argument_count_ = pred->argument_count();
948 }
949 HInstruction* current = block->first();
950 int start = chunk_->instructions()->length();
951 while (current != NULL && !is_aborted()) {
952 // Code for constants in registers is generated lazily.
953 if (!current->EmitAtUses()) {
954 VisitInstruction(current);
955 }
956 current = current->next();
957 }
958 int end = chunk_->instructions()->length() - 1;
959 if (end >= start) {
960 block->set_first_instruction_index(start);
961 block->set_last_instruction_index(end);
962 }
963 block->set_argument_count(argument_count_);
964 next_block_ = NULL;
965 current_block_ = NULL;
966}
967
968
969void LChunkBuilder::VisitInstruction(HInstruction* current) {
970 HInstruction* old_current = current_instruction_;
971 current_instruction_ = current;
972 if (current->has_position()) position_ = current->position();
973 LInstruction* instr = current->CompileToLithium(this);
974
975 if (instr != NULL) {
976 if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
977 instr = AssignPointerMap(instr);
978 }
979 if (FLAG_stress_environments && !instr->HasEnvironment()) {
980 instr = AssignEnvironment(instr);
981 }
982 instr->set_hydrogen_value(current);
983 chunk_->AddInstruction(instr, current_block_);
984 }
985 current_instruction_ = old_current;
986}
987
988
989LEnvironment* LChunkBuilder::CreateEnvironment(
990 HEnvironment* hydrogen_env,
991 int* argument_index_accumulator) {
992 if (hydrogen_env == NULL) return NULL;
993
994 LEnvironment* outer =
995 CreateEnvironment(hydrogen_env->outer(), argument_index_accumulator);
996 int ast_id = hydrogen_env->ast_id();
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000997 ASSERT(ast_id != AstNode::kNoNumber || hydrogen_env->is_arguments_adaptor());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000998 int value_count = hydrogen_env->length();
999 LEnvironment* result = new LEnvironment(hydrogen_env->closure(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001000 hydrogen_env->is_arguments_adaptor(),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001001 ast_id,
1002 hydrogen_env->parameter_count(),
1003 argument_count_,
1004 value_count,
1005 outer);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001006 int argument_index = *argument_index_accumulator;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001007 for (int i = 0; i < value_count; ++i) {
1008 if (hydrogen_env->is_special_index(i)) continue;
1009
1010 HValue* value = hydrogen_env->values()->at(i);
1011 LOperand* op = NULL;
1012 if (value->IsArgumentsObject()) {
1013 op = NULL;
1014 } else if (value->IsPushArgument()) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001015 op = new LArgument(argument_index++);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001016 } else {
1017 op = UseAny(value);
1018 }
1019 result->AddValue(op, value->representation());
1020 }
1021
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001022 if (!hydrogen_env->is_arguments_adaptor()) {
1023 *argument_index_accumulator = argument_index;
1024 }
1025
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001026 return result;
1027}
1028
1029
1030LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
1031 return new LGoto(instr->FirstSuccessor()->block_id());
1032}
1033
1034
1035LInstruction* LChunkBuilder::DoBranch(HBranch* instr) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001036 HValue* value = instr->value();
1037 if (value->EmitAtUses()) {
1038 HBasicBlock* successor = HConstant::cast(value)->ToBoolean()
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001039 ? instr->FirstSuccessor()
1040 : instr->SecondSuccessor();
1041 return new LGoto(successor->block_id());
1042 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001043
1044 LBranch* result = new LBranch(UseRegister(value));
1045 // Tagged values that are not known smis or booleans require a
1046 // deoptimization environment.
1047 Representation rep = value->representation();
1048 HType type = value->type();
1049 if (rep.IsTagged() && !type.IsSmi() && !type.IsBoolean()) {
1050 return AssignEnvironment(result);
1051 }
1052 return result;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001053}
1054
1055
1056LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
1057 ASSERT(instr->value()->representation().IsTagged());
1058 LOperand* value = UseRegisterAtStart(instr->value());
1059 LOperand* temp = TempRegister();
1060 return new LCmpMapAndBranch(value, temp);
1061}
1062
1063
1064LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* length) {
1065 return DefineAsRegister(new LArgumentsLength(UseRegister(length->value())));
1066}
1067
1068
1069LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
1070 return DefineAsRegister(new LArgumentsElements);
1071}
1072
1073
1074LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
1075 LInstanceOf* result =
1076 new LInstanceOf(UseFixed(instr->left(), a0),
1077 UseFixed(instr->right(), a1));
1078 return MarkAsCall(DefineFixed(result, v0), instr);
1079}
1080
1081
1082LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
1083 HInstanceOfKnownGlobal* instr) {
1084 LInstanceOfKnownGlobal* result =
1085 new LInstanceOfKnownGlobal(UseFixed(instr->left(), a0), FixedTemp(t0));
1086 return MarkAsCall(DefineFixed(result, v0), instr);
1087}
1088
1089
1090LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
1091 LOperand* function = UseFixed(instr->function(), a1);
1092 LOperand* receiver = UseFixed(instr->receiver(), a0);
1093 LOperand* length = UseFixed(instr->length(), a2);
1094 LOperand* elements = UseFixed(instr->elements(), a3);
1095 LApplyArguments* result = new LApplyArguments(function,
1096 receiver,
1097 length,
1098 elements);
1099 return MarkAsCall(DefineFixed(result, v0), instr, CAN_DEOPTIMIZE_EAGERLY);
1100}
1101
1102
1103LInstruction* LChunkBuilder::DoPushArgument(HPushArgument* instr) {
1104 ++argument_count_;
1105 LOperand* argument = Use(instr->argument());
1106 return new LPushArgument(argument);
1107}
1108
1109
1110LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1111 return instr->HasNoUses() ? NULL : DefineAsRegister(new LThisFunction);
1112}
1113
1114
1115LInstruction* LChunkBuilder::DoContext(HContext* instr) {
1116 return instr->HasNoUses() ? NULL : DefineAsRegister(new LContext);
1117}
1118
1119
1120LInstruction* LChunkBuilder::DoOuterContext(HOuterContext* instr) {
1121 LOperand* context = UseRegisterAtStart(instr->value());
1122 return DefineAsRegister(new LOuterContext(context));
1123}
1124
1125
1126LInstruction* LChunkBuilder::DoGlobalObject(HGlobalObject* instr) {
1127 LOperand* context = UseRegisterAtStart(instr->value());
1128 return DefineAsRegister(new LGlobalObject(context));
1129}
1130
1131
1132LInstruction* LChunkBuilder::DoGlobalReceiver(HGlobalReceiver* instr) {
1133 LOperand* global_object = UseRegisterAtStart(instr->value());
1134 return DefineAsRegister(new LGlobalReceiver(global_object));
1135}
1136
1137
1138LInstruction* LChunkBuilder::DoCallConstantFunction(
1139 HCallConstantFunction* instr) {
1140 argument_count_ -= instr->argument_count();
1141 return MarkAsCall(DefineFixed(new LCallConstantFunction, v0), instr);
1142}
1143
1144
1145LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1146 LOperand* function = UseFixed(instr->function(), a1);
1147 argument_count_ -= instr->argument_count();
1148 LInvokeFunction* result = new LInvokeFunction(function);
1149 return MarkAsCall(DefineFixed(result, v0), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1150}
1151
1152
1153LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
1154 BuiltinFunctionId op = instr->op();
1155 if (op == kMathLog || op == kMathSin || op == kMathCos) {
1156 LOperand* input = UseFixedDouble(instr->value(), f4);
1157 LUnaryMathOperation* result = new LUnaryMathOperation(input, NULL);
1158 return MarkAsCall(DefineFixedDouble(result, f4), instr);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001159 } else if (op == kMathPowHalf) {
1160 // Input cannot be the same as the result.
1161 // See lithium-codegen-mips.cc::DoMathPowHalf.
1162 LOperand* input = UseFixedDouble(instr->value(), f8);
1163 LOperand* temp = FixedTemp(f6);
1164 LUnaryMathOperation* result = new LUnaryMathOperation(input, temp);
1165 return DefineFixedDouble(result, f4);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001166 } else {
1167 LOperand* input = UseRegisterAtStart(instr->value());
1168 LOperand* temp = (op == kMathFloor) ? TempRegister() : NULL;
1169 LUnaryMathOperation* result = new LUnaryMathOperation(input, temp);
1170 switch (op) {
1171 case kMathAbs:
1172 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1173 case kMathFloor:
1174 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1175 case kMathSqrt:
1176 return DefineAsRegister(result);
1177 case kMathRound:
1178 return AssignEnvironment(DefineAsRegister(result));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001179 default:
1180 UNREACHABLE();
1181 return NULL;
1182 }
1183 }
1184}
1185
1186
1187LInstruction* LChunkBuilder::DoCallKeyed(HCallKeyed* instr) {
1188 ASSERT(instr->key()->representation().IsTagged());
1189 argument_count_ -= instr->argument_count();
1190 LOperand* key = UseFixed(instr->key(), a2);
1191 return MarkAsCall(DefineFixed(new LCallKeyed(key), v0), instr);
1192}
1193
1194
1195LInstruction* LChunkBuilder::DoCallNamed(HCallNamed* instr) {
1196 argument_count_ -= instr->argument_count();
1197 return MarkAsCall(DefineFixed(new LCallNamed, v0), instr);
1198}
1199
1200
1201LInstruction* LChunkBuilder::DoCallGlobal(HCallGlobal* instr) {
1202 argument_count_ -= instr->argument_count();
1203 return MarkAsCall(DefineFixed(new LCallGlobal, v0), instr);
1204}
1205
1206
1207LInstruction* LChunkBuilder::DoCallKnownGlobal(HCallKnownGlobal* instr) {
1208 argument_count_ -= instr->argument_count();
1209 return MarkAsCall(DefineFixed(new LCallKnownGlobal, v0), instr);
1210}
1211
1212
1213LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
1214 LOperand* constructor = UseFixed(instr->constructor(), a1);
1215 argument_count_ -= instr->argument_count();
1216 LCallNew* result = new LCallNew(constructor);
1217 return MarkAsCall(DefineFixed(result, v0), instr);
1218}
1219
1220
1221LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001222 LOperand* function = UseFixed(instr->function(), a1);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001223 argument_count_ -= instr->argument_count();
danno@chromium.orgc612e022011-11-10 11:38:15 +00001224 return MarkAsCall(DefineFixed(new LCallFunction(function), v0), instr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001225}
1226
1227
1228LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1229 argument_count_ -= instr->argument_count();
1230 return MarkAsCall(DefineFixed(new LCallRuntime, v0), instr);
1231}
1232
1233
1234LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1235 return DoShift(Token::SHR, instr);
1236}
1237
1238
1239LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1240 return DoShift(Token::SAR, instr);
1241}
1242
1243
1244LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1245 return DoShift(Token::SHL, instr);
1246}
1247
1248
1249LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
1250 if (instr->representation().IsInteger32()) {
1251 ASSERT(instr->left()->representation().IsInteger32());
1252 ASSERT(instr->right()->representation().IsInteger32());
1253
1254 LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1255 LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1256 return DefineAsRegister(new LBitI(left, right));
1257 } else {
1258 ASSERT(instr->representation().IsTagged());
1259 ASSERT(instr->left()->representation().IsTagged());
1260 ASSERT(instr->right()->representation().IsTagged());
1261
1262 LOperand* left = UseFixed(instr->left(), a1);
1263 LOperand* right = UseFixed(instr->right(), a0);
1264 LArithmeticT* result = new LArithmeticT(instr->op(), left, right);
1265 return MarkAsCall(DefineFixed(result, v0), instr);
1266 }
1267}
1268
1269
1270LInstruction* LChunkBuilder::DoBitNot(HBitNot* instr) {
1271 ASSERT(instr->value()->representation().IsInteger32());
1272 ASSERT(instr->representation().IsInteger32());
1273 return DefineAsRegister(new LBitNotI(UseRegisterAtStart(instr->value())));
1274}
1275
1276
1277LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
1278 if (instr->representation().IsDouble()) {
1279 return DoArithmeticD(Token::DIV, instr);
1280 } else if (instr->representation().IsInteger32()) {
1281 // TODO(1042) The fixed register allocation
1282 // is needed because we call TypeRecordingBinaryOpStub from
1283 // the generated code, which requires registers a0
1284 // and a1 to be used. We should remove that
1285 // when we provide a native implementation.
1286 LOperand* dividend = UseFixed(instr->left(), a0);
1287 LOperand* divisor = UseFixed(instr->right(), a1);
1288 return AssignEnvironment(AssignPointerMap(
1289 DefineFixed(new LDivI(dividend, divisor), v0)));
1290 } else {
1291 return DoArithmeticT(Token::DIV, instr);
1292 }
1293}
1294
1295
1296LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1297 if (instr->representation().IsInteger32()) {
1298 ASSERT(instr->left()->representation().IsInteger32());
1299 ASSERT(instr->right()->representation().IsInteger32());
1300
1301 LModI* mod;
1302 if (instr->HasPowerOf2Divisor()) {
1303 ASSERT(!instr->CheckFlag(HValue::kCanBeDivByZero));
1304 LOperand* value = UseRegisterAtStart(instr->left());
1305 mod = new LModI(value, UseOrConstant(instr->right()));
1306 } else {
1307 LOperand* dividend = UseRegister(instr->left());
1308 LOperand* divisor = UseRegister(instr->right());
1309 mod = new LModI(dividend,
1310 divisor,
1311 TempRegister(),
1312 FixedTemp(f20),
1313 FixedTemp(f22));
1314 }
1315
1316 if (instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1317 instr->CheckFlag(HValue::kCanBeDivByZero)) {
1318 return AssignEnvironment(DefineAsRegister(mod));
1319 } else {
1320 return DefineAsRegister(mod);
1321 }
1322 } else if (instr->representation().IsTagged()) {
1323 return DoArithmeticT(Token::MOD, instr);
1324 } else {
1325 ASSERT(instr->representation().IsDouble());
1326 // We call a C function for double modulo. It can't trigger a GC.
1327 // We need to use fixed result register for the call.
1328 // TODO(fschneider): Allow any register as input registers.
1329 LOperand* left = UseFixedDouble(instr->left(), f2);
1330 LOperand* right = UseFixedDouble(instr->right(), f4);
1331 LArithmeticD* result = new LArithmeticD(Token::MOD, left, right);
1332 return MarkAsCall(DefineFixedDouble(result, f2), instr);
1333 }
1334}
1335
1336
1337LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1338 if (instr->representation().IsInteger32()) {
1339 ASSERT(instr->left()->representation().IsInteger32());
1340 ASSERT(instr->right()->representation().IsInteger32());
1341 LOperand* left;
1342 LOperand* right = UseOrConstant(instr->MostConstantOperand());
1343 LOperand* temp = NULL;
1344 if (instr->CheckFlag(HValue::kBailoutOnMinusZero) &&
1345 (instr->CheckFlag(HValue::kCanOverflow) ||
1346 !right->IsConstantOperand())) {
1347 left = UseRegister(instr->LeastConstantOperand());
1348 temp = TempRegister();
1349 } else {
1350 left = UseRegisterAtStart(instr->LeastConstantOperand());
1351 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001352 LMulI* mul = new LMulI(left, right, temp);
1353 if (instr->CheckFlag(HValue::kCanOverflow) ||
1354 instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1355 AssignEnvironment(mul);
1356 }
1357 return DefineAsRegister(mul);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001358
1359 } else if (instr->representation().IsDouble()) {
1360 return DoArithmeticD(Token::MUL, instr);
1361
1362 } else {
1363 return DoArithmeticT(Token::MUL, instr);
1364 }
1365}
1366
1367
1368LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1369 if (instr->representation().IsInteger32()) {
1370 ASSERT(instr->left()->representation().IsInteger32());
1371 ASSERT(instr->right()->representation().IsInteger32());
1372 LOperand* left = UseRegisterAtStart(instr->left());
1373 LOperand* right = UseOrConstantAtStart(instr->right());
1374 LSubI* sub = new LSubI(left, right);
1375 LInstruction* result = DefineAsRegister(sub);
1376 if (instr->CheckFlag(HValue::kCanOverflow)) {
1377 result = AssignEnvironment(result);
1378 }
1379 return result;
1380 } else if (instr->representation().IsDouble()) {
1381 return DoArithmeticD(Token::SUB, instr);
1382 } else {
1383 return DoArithmeticT(Token::SUB, instr);
1384 }
1385}
1386
1387
1388LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1389 if (instr->representation().IsInteger32()) {
1390 ASSERT(instr->left()->representation().IsInteger32());
1391 ASSERT(instr->right()->representation().IsInteger32());
1392 LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1393 LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1394 LAddI* add = new LAddI(left, right);
1395 LInstruction* result = DefineAsRegister(add);
1396 if (instr->CheckFlag(HValue::kCanOverflow)) {
1397 result = AssignEnvironment(result);
1398 }
1399 return result;
1400 } else if (instr->representation().IsDouble()) {
1401 return DoArithmeticD(Token::ADD, instr);
1402 } else {
1403 ASSERT(instr->representation().IsTagged());
1404 return DoArithmeticT(Token::ADD, instr);
1405 }
1406}
1407
1408
1409LInstruction* LChunkBuilder::DoPower(HPower* instr) {
1410 ASSERT(instr->representation().IsDouble());
1411 // We call a C function for double power. It can't trigger a GC.
1412 // We need to use fixed result register for the call.
1413 Representation exponent_type = instr->right()->representation();
1414 ASSERT(instr->left()->representation().IsDouble());
1415 LOperand* left = UseFixedDouble(instr->left(), f2);
1416 LOperand* right = exponent_type.IsDouble() ?
1417 UseFixedDouble(instr->right(), f4) :
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001418 UseFixed(instr->right(), a2);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001419 LPower* result = new LPower(left, right);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001420 return MarkAsCall(DefineFixedDouble(result, f0),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001421 instr,
1422 CAN_DEOPTIMIZE_EAGERLY);
1423}
1424
1425
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001426LInstruction* LChunkBuilder::DoRandom(HRandom* instr) {
1427 ASSERT(instr->representation().IsDouble());
1428 ASSERT(instr->global_object()->representation().IsTagged());
1429 LOperand* global_object = UseFixed(instr->global_object(), a0);
1430 LRandom* result = new LRandom(global_object);
1431 return MarkAsCall(DefineFixedDouble(result, f0), instr);
1432}
1433
1434
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001435LInstruction* LChunkBuilder::DoCompareGeneric(HCompareGeneric* instr) {
1436 Representation r = instr->GetInputRepresentation();
1437 ASSERT(instr->left()->representation().IsTagged());
1438 ASSERT(instr->right()->representation().IsTagged());
1439 LOperand* left = UseFixed(instr->left(), a1);
1440 LOperand* right = UseFixed(instr->right(), a0);
1441 LCmpT* result = new LCmpT(left, right);
1442 return MarkAsCall(DefineFixed(result, v0), instr);
1443}
1444
1445
1446LInstruction* LChunkBuilder::DoCompareIDAndBranch(
1447 HCompareIDAndBranch* instr) {
1448 Representation r = instr->GetInputRepresentation();
1449 if (r.IsInteger32()) {
1450 ASSERT(instr->left()->representation().IsInteger32());
1451 ASSERT(instr->right()->representation().IsInteger32());
1452 LOperand* left = UseRegisterOrConstantAtStart(instr->left());
1453 LOperand* right = UseRegisterOrConstantAtStart(instr->right());
1454 return new LCmpIDAndBranch(left, right);
1455 } else {
1456 ASSERT(r.IsDouble());
1457 ASSERT(instr->left()->representation().IsDouble());
1458 ASSERT(instr->right()->representation().IsDouble());
1459 LOperand* left = UseRegisterAtStart(instr->left());
1460 LOperand* right = UseRegisterAtStart(instr->right());
1461 return new LCmpIDAndBranch(left, right);
1462 }
1463}
1464
1465
1466LInstruction* LChunkBuilder::DoCompareObjectEqAndBranch(
1467 HCompareObjectEqAndBranch* instr) {
1468 LOperand* left = UseRegisterAtStart(instr->left());
1469 LOperand* right = UseRegisterAtStart(instr->right());
1470 return new LCmpObjectEqAndBranch(left, right);
1471}
1472
1473
1474LInstruction* LChunkBuilder::DoCompareConstantEqAndBranch(
1475 HCompareConstantEqAndBranch* instr) {
1476 return new LCmpConstantEqAndBranch(UseRegisterAtStart(instr->value()));
1477}
1478
1479
1480LInstruction* LChunkBuilder::DoIsNilAndBranch(HIsNilAndBranch* instr) {
1481 ASSERT(instr->value()->representation().IsTagged());
1482 return new LIsNilAndBranch(UseRegisterAtStart(instr->value()));
1483}
1484
1485
1486LInstruction* LChunkBuilder::DoIsObjectAndBranch(HIsObjectAndBranch* instr) {
1487 ASSERT(instr->value()->representation().IsTagged());
1488 LOperand* temp = TempRegister();
1489 return new LIsObjectAndBranch(UseRegisterAtStart(instr->value()), temp);
1490}
1491
1492
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001493LInstruction* LChunkBuilder::DoIsStringAndBranch(HIsStringAndBranch* instr) {
1494 ASSERT(instr->value()->representation().IsTagged());
1495 LOperand* temp = TempRegister();
1496 return new LIsStringAndBranch(UseRegisterAtStart(instr->value()), temp);
1497}
1498
1499
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001500LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
1501 ASSERT(instr->value()->representation().IsTagged());
1502 return new LIsSmiAndBranch(Use(instr->value()));
1503}
1504
1505
1506LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
1507 HIsUndetectableAndBranch* instr) {
1508 ASSERT(instr->value()->representation().IsTagged());
1509 return new LIsUndetectableAndBranch(UseRegisterAtStart(instr->value()),
1510 TempRegister());
1511}
1512
1513
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00001514LInstruction* LChunkBuilder::DoStringCompareAndBranch(
1515 HStringCompareAndBranch* instr) {
1516 ASSERT(instr->left()->representation().IsTagged());
1517 ASSERT(instr->right()->representation().IsTagged());
1518 LOperand* left = UseFixed(instr->left(), a1);
1519 LOperand* right = UseFixed(instr->right(), a0);
1520 LStringCompareAndBranch* result = new LStringCompareAndBranch(left, right);
1521 return MarkAsCall(result, instr);
1522}
1523
1524
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001525LInstruction* LChunkBuilder::DoHasInstanceTypeAndBranch(
1526 HHasInstanceTypeAndBranch* instr) {
1527 ASSERT(instr->value()->representation().IsTagged());
1528 return new LHasInstanceTypeAndBranch(UseRegisterAtStart(instr->value()));
1529}
1530
1531
1532LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
1533 HGetCachedArrayIndex* instr) {
1534 ASSERT(instr->value()->representation().IsTagged());
1535 LOperand* value = UseRegisterAtStart(instr->value());
1536
1537 return DefineAsRegister(new LGetCachedArrayIndex(value));
1538}
1539
1540
1541LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
1542 HHasCachedArrayIndexAndBranch* instr) {
1543 ASSERT(instr->value()->representation().IsTagged());
1544 return new LHasCachedArrayIndexAndBranch(
1545 UseRegisterAtStart(instr->value()));
1546}
1547
1548
1549LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
1550 HClassOfTestAndBranch* instr) {
1551 ASSERT(instr->value()->representation().IsTagged());
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001552 return new LClassOfTestAndBranch(UseRegister(instr->value()),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001553 TempRegister());
1554}
1555
1556
1557LInstruction* LChunkBuilder::DoJSArrayLength(HJSArrayLength* instr) {
1558 LOperand* array = UseRegisterAtStart(instr->value());
1559 return DefineAsRegister(new LJSArrayLength(array));
1560}
1561
1562
1563LInstruction* LChunkBuilder::DoFixedArrayBaseLength(
1564 HFixedArrayBaseLength* instr) {
1565 LOperand* array = UseRegisterAtStart(instr->value());
1566 return DefineAsRegister(new LFixedArrayBaseLength(array));
1567}
1568
1569
1570LInstruction* LChunkBuilder::DoElementsKind(HElementsKind* instr) {
1571 LOperand* object = UseRegisterAtStart(instr->value());
1572 return DefineAsRegister(new LElementsKind(object));
1573}
1574
1575
1576LInstruction* LChunkBuilder::DoValueOf(HValueOf* instr) {
1577 LOperand* object = UseRegister(instr->value());
1578 LValueOf* result = new LValueOf(object, TempRegister());
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001579 return DefineAsRegister(result);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001580}
1581
1582
1583LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
1584 return AssignEnvironment(new LBoundsCheck(UseRegisterAtStart(instr->index()),
1585 UseRegister(instr->length())));
1586}
1587
1588
1589LInstruction* LChunkBuilder::DoAbnormalExit(HAbnormalExit* instr) {
1590 // The control instruction marking the end of a block that completed
1591 // abruptly (e.g., threw an exception). There is nothing specific to do.
1592 return NULL;
1593}
1594
1595
1596LInstruction* LChunkBuilder::DoThrow(HThrow* instr) {
1597 LOperand* value = UseFixed(instr->value(), a0);
1598 return MarkAsCall(new LThrow(value), instr);
1599}
1600
1601
1602LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
1603 return NULL;
1604}
1605
1606
1607LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
1608 // All HForceRepresentation instructions should be eliminated in the
1609 // representation change phase of Hydrogen.
1610 UNREACHABLE();
1611 return NULL;
1612}
1613
1614
1615LInstruction* LChunkBuilder::DoChange(HChange* instr) {
1616 Representation from = instr->from();
1617 Representation to = instr->to();
1618 if (from.IsTagged()) {
1619 if (to.IsDouble()) {
1620 LOperand* value = UseRegister(instr->value());
1621 LNumberUntagD* res = new LNumberUntagD(value);
1622 return AssignEnvironment(DefineAsRegister(res));
1623 } else {
1624 ASSERT(to.IsInteger32());
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001625 LOperand* value = UseRegisterAtStart(instr->value());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001626 bool needs_check = !instr->value()->type().IsSmi();
1627 LInstruction* res = NULL;
1628 if (!needs_check) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001629 res = DefineAsRegister(new LSmiUntag(value, needs_check));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001630 } else {
1631 LOperand* temp1 = TempRegister();
1632 LOperand* temp2 = instr->CanTruncateToInt32() ? TempRegister()
1633 : NULL;
1634 LOperand* temp3 = instr->CanTruncateToInt32() ? FixedTemp(f22)
1635 : NULL;
1636 res = DefineSameAsFirst(new LTaggedToI(value, temp1, temp2, temp3));
1637 res = AssignEnvironment(res);
1638 }
1639 return res;
1640 }
1641 } else if (from.IsDouble()) {
1642 if (to.IsTagged()) {
1643 LOperand* value = UseRegister(instr->value());
1644 LOperand* temp1 = TempRegister();
1645 LOperand* temp2 = TempRegister();
1646
1647 // Make sure that the temp and result_temp registers are
1648 // different.
1649 LUnallocated* result_temp = TempRegister();
1650 LNumberTagD* result = new LNumberTagD(value, temp1, temp2);
1651 Define(result, result_temp);
1652 return AssignPointerMap(result);
1653 } else {
1654 ASSERT(to.IsInteger32());
1655 LOperand* value = UseRegister(instr->value());
1656 LDoubleToI* res =
1657 new LDoubleToI(value,
1658 TempRegister(),
1659 instr->CanTruncateToInt32() ? TempRegister() : NULL);
1660 return AssignEnvironment(DefineAsRegister(res));
1661 }
1662 } else if (from.IsInteger32()) {
1663 if (to.IsTagged()) {
1664 HValue* val = instr->value();
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001665 LOperand* value = UseRegisterAtStart(val);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001666 if (val->HasRange() && val->range()->IsInSmiRange()) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001667 return DefineAsRegister(new LSmiTag(value));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001668 } else {
1669 LNumberTagI* result = new LNumberTagI(value);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001670 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001671 }
1672 } else {
1673 ASSERT(to.IsDouble());
1674 LOperand* value = Use(instr->value());
1675 return DefineAsRegister(new LInteger32ToDouble(value));
1676 }
1677 }
1678 UNREACHABLE();
1679 return NULL;
1680}
1681
1682
1683LInstruction* LChunkBuilder::DoCheckNonSmi(HCheckNonSmi* instr) {
1684 LOperand* value = UseRegisterAtStart(instr->value());
1685 return AssignEnvironment(new LCheckNonSmi(value));
1686}
1687
1688
1689LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
1690 LOperand* value = UseRegisterAtStart(instr->value());
1691 LInstruction* result = new LCheckInstanceType(value);
1692 return AssignEnvironment(result);
1693}
1694
1695
1696LInstruction* LChunkBuilder::DoCheckPrototypeMaps(HCheckPrototypeMaps* instr) {
1697 LOperand* temp1 = TempRegister();
1698 LOperand* temp2 = TempRegister();
1699 LInstruction* result = new LCheckPrototypeMaps(temp1, temp2);
1700 return AssignEnvironment(result);
1701}
1702
1703
1704LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
1705 LOperand* value = UseRegisterAtStart(instr->value());
1706 return AssignEnvironment(new LCheckSmi(value));
1707}
1708
1709
1710LInstruction* LChunkBuilder::DoCheckFunction(HCheckFunction* instr) {
1711 LOperand* value = UseRegisterAtStart(instr->value());
1712 return AssignEnvironment(new LCheckFunction(value));
1713}
1714
1715
1716LInstruction* LChunkBuilder::DoCheckMap(HCheckMap* instr) {
1717 LOperand* value = UseRegisterAtStart(instr->value());
1718 LInstruction* result = new LCheckMap(value);
1719 return AssignEnvironment(result);
1720}
1721
1722
1723LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
1724 HValue* value = instr->value();
1725 Representation input_rep = value->representation();
1726 LOperand* reg = UseRegister(value);
1727 if (input_rep.IsDouble()) {
1728 // Revisit this decision, here and 8 lines below.
1729 return DefineAsRegister(new LClampDToUint8(reg, FixedTemp(f22)));
1730 } else if (input_rep.IsInteger32()) {
1731 return DefineAsRegister(new LClampIToUint8(reg));
1732 } else {
1733 ASSERT(input_rep.IsTagged());
1734 // Register allocator doesn't (yet) support allocation of double
1735 // temps. Reserve f22 explicitly.
1736 LClampTToUint8* result = new LClampTToUint8(reg, FixedTemp(f22));
1737 return AssignEnvironment(DefineAsRegister(result));
1738 }
1739}
1740
1741
1742LInstruction* LChunkBuilder::DoToInt32(HToInt32* instr) {
1743 HValue* value = instr->value();
1744 Representation input_rep = value->representation();
1745 LOperand* reg = UseRegister(value);
1746 if (input_rep.IsDouble()) {
1747 LOperand* temp1 = TempRegister();
1748 LOperand* temp2 = TempRegister();
1749 LDoubleToI* res = new LDoubleToI(reg, temp1, temp2);
1750 return AssignEnvironment(DefineAsRegister(res));
1751 } else if (input_rep.IsInteger32()) {
1752 // Canonicalization should already have removed the hydrogen instruction in
1753 // this case, since it is a noop.
1754 UNREACHABLE();
1755 return NULL;
1756 } else {
1757 ASSERT(input_rep.IsTagged());
1758 LOperand* temp1 = TempRegister();
1759 LOperand* temp2 = TempRegister();
1760 LOperand* temp3 = FixedTemp(f22);
1761 LTaggedToI* res = new LTaggedToI(reg, temp1, temp2, temp3);
1762 return AssignEnvironment(DefineSameAsFirst(res));
1763 }
1764}
1765
1766
1767LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
1768 return new LReturn(UseFixed(instr->value(), v0));
1769}
1770
1771
1772LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
1773 Representation r = instr->representation();
1774 if (r.IsInteger32()) {
1775 return DefineAsRegister(new LConstantI);
1776 } else if (r.IsDouble()) {
1777 return DefineAsRegister(new LConstantD);
1778 } else if (r.IsTagged()) {
1779 return DefineAsRegister(new LConstantT);
1780 } else {
1781 UNREACHABLE();
1782 return NULL;
1783 }
1784}
1785
1786
1787LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
1788 LLoadGlobalCell* result = new LLoadGlobalCell;
1789 return instr->RequiresHoleCheck()
1790 ? AssignEnvironment(DefineAsRegister(result))
1791 : DefineAsRegister(result);
1792}
1793
1794
1795LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
1796 LOperand* global_object = UseFixed(instr->global_object(), a0);
1797 LLoadGlobalGeneric* result = new LLoadGlobalGeneric(global_object);
1798 return MarkAsCall(DefineFixed(result, v0), instr);
1799}
1800
1801
1802LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
danno@chromium.orge78f9fc2011-12-21 08:29:34 +00001803 LOperand* value = UseRegister(instr->value());
1804 // Use a temp to check the value in the cell in the case where we perform
1805 // a hole check.
1806 return instr->RequiresHoleCheck()
1807 ? AssignEnvironment(new LStoreGlobalCell(value, TempRegister()))
1808 : new LStoreGlobalCell(value, NULL);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001809}
1810
1811
1812LInstruction* LChunkBuilder::DoStoreGlobalGeneric(HStoreGlobalGeneric* instr) {
1813 LOperand* global_object = UseFixed(instr->global_object(), a1);
1814 LOperand* value = UseFixed(instr->value(), a0);
1815 LStoreGlobalGeneric* result =
1816 new LStoreGlobalGeneric(global_object, value);
1817 return MarkAsCall(result, instr);
1818}
1819
1820
1821LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
1822 LOperand* context = UseRegisterAtStart(instr->value());
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001823 LInstruction* result = DefineAsRegister(new LLoadContextSlot(context));
1824 return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001825}
1826
1827
1828LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
1829 LOperand* context;
1830 LOperand* value;
1831 if (instr->NeedsWriteBarrier()) {
1832 context = UseTempRegister(instr->context());
1833 value = UseTempRegister(instr->value());
1834 } else {
1835 context = UseRegister(instr->context());
1836 value = UseRegister(instr->value());
1837 }
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001838 LInstruction* result = new LStoreContextSlot(context, value);
1839 return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001840}
1841
1842
1843LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
1844 return DefineAsRegister(
1845 new LLoadNamedField(UseRegisterAtStart(instr->object())));
1846}
1847
1848
1849LInstruction* LChunkBuilder::DoLoadNamedFieldPolymorphic(
1850 HLoadNamedFieldPolymorphic* instr) {
1851 ASSERT(instr->representation().IsTagged());
1852 if (instr->need_generic()) {
1853 LOperand* obj = UseFixed(instr->object(), a0);
1854 LLoadNamedFieldPolymorphic* result = new LLoadNamedFieldPolymorphic(obj);
1855 return MarkAsCall(DefineFixed(result, v0), instr);
1856 } else {
1857 LOperand* obj = UseRegisterAtStart(instr->object());
1858 LLoadNamedFieldPolymorphic* result = new LLoadNamedFieldPolymorphic(obj);
1859 return AssignEnvironment(DefineAsRegister(result));
1860 }
1861}
1862
1863
1864LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
1865 LOperand* object = UseFixed(instr->object(), a0);
1866 LInstruction* result = DefineFixed(new LLoadNamedGeneric(object), v0);
1867 return MarkAsCall(result, instr);
1868}
1869
1870
1871LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
1872 HLoadFunctionPrototype* instr) {
1873 return AssignEnvironment(DefineAsRegister(
1874 new LLoadFunctionPrototype(UseRegister(instr->function()))));
1875}
1876
1877
1878LInstruction* LChunkBuilder::DoLoadElements(HLoadElements* instr) {
1879 LOperand* input = UseRegisterAtStart(instr->value());
1880 return DefineAsRegister(new LLoadElements(input));
1881}
1882
1883
1884LInstruction* LChunkBuilder::DoLoadExternalArrayPointer(
1885 HLoadExternalArrayPointer* instr) {
1886 LOperand* input = UseRegisterAtStart(instr->value());
1887 return DefineAsRegister(new LLoadExternalArrayPointer(input));
1888}
1889
1890
1891LInstruction* LChunkBuilder::DoLoadKeyedFastElement(
1892 HLoadKeyedFastElement* instr) {
1893 ASSERT(instr->representation().IsTagged());
1894 ASSERT(instr->key()->representation().IsInteger32());
1895 LOperand* obj = UseRegisterAtStart(instr->object());
1896 LOperand* key = UseRegisterAtStart(instr->key());
1897 LLoadKeyedFastElement* result = new LLoadKeyedFastElement(obj, key);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001898 if (instr->RequiresHoleCheck()) AssignEnvironment(result);
1899 return DefineAsRegister(result);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001900}
1901
1902
1903LInstruction* LChunkBuilder::DoLoadKeyedFastDoubleElement(
1904 HLoadKeyedFastDoubleElement* instr) {
1905 ASSERT(instr->representation().IsDouble());
1906 ASSERT(instr->key()->representation().IsInteger32());
1907 LOperand* elements = UseTempRegister(instr->elements());
1908 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1909 LLoadKeyedFastDoubleElement* result =
1910 new LLoadKeyedFastDoubleElement(elements, key);
1911 return AssignEnvironment(DefineAsRegister(result));
1912}
1913
1914
1915LInstruction* LChunkBuilder::DoLoadKeyedSpecializedArrayElement(
1916 HLoadKeyedSpecializedArrayElement* instr) {
1917 ElementsKind elements_kind = instr->elements_kind();
1918 Representation representation(instr->representation());
1919 ASSERT(
1920 (representation.IsInteger32() &&
1921 (elements_kind != EXTERNAL_FLOAT_ELEMENTS) &&
1922 (elements_kind != EXTERNAL_DOUBLE_ELEMENTS)) ||
1923 (representation.IsDouble() &&
1924 ((elements_kind == EXTERNAL_FLOAT_ELEMENTS) ||
1925 (elements_kind == EXTERNAL_DOUBLE_ELEMENTS))));
1926 ASSERT(instr->key()->representation().IsInteger32());
1927 LOperand* external_pointer = UseRegister(instr->external_pointer());
1928 LOperand* key = UseRegisterOrConstant(instr->key());
1929 LLoadKeyedSpecializedArrayElement* result =
1930 new LLoadKeyedSpecializedArrayElement(external_pointer, key);
1931 LInstruction* load_instr = DefineAsRegister(result);
1932 // An unsigned int array load might overflow and cause a deopt, make sure it
1933 // has an environment.
1934 return (elements_kind == EXTERNAL_UNSIGNED_INT_ELEMENTS) ?
1935 AssignEnvironment(load_instr) : load_instr;
1936}
1937
1938
1939LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
1940 LOperand* object = UseFixed(instr->object(), a1);
1941 LOperand* key = UseFixed(instr->key(), a0);
1942
1943 LInstruction* result =
1944 DefineFixed(new LLoadKeyedGeneric(object, key), v0);
1945 return MarkAsCall(result, instr);
1946}
1947
1948
1949LInstruction* LChunkBuilder::DoStoreKeyedFastElement(
1950 HStoreKeyedFastElement* instr) {
1951 bool needs_write_barrier = instr->NeedsWriteBarrier();
1952 ASSERT(instr->value()->representation().IsTagged());
1953 ASSERT(instr->object()->representation().IsTagged());
1954 ASSERT(instr->key()->representation().IsInteger32());
1955
1956 LOperand* obj = UseTempRegister(instr->object());
1957 LOperand* val = needs_write_barrier
1958 ? UseTempRegister(instr->value())
1959 : UseRegisterAtStart(instr->value());
1960 LOperand* key = needs_write_barrier
1961 ? UseTempRegister(instr->key())
1962 : UseRegisterOrConstantAtStart(instr->key());
danno@chromium.orgbf0c8202011-12-27 10:09:42 +00001963 return new LStoreKeyedFastElement(obj, key, val);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001964}
1965
1966
1967LInstruction* LChunkBuilder::DoStoreKeyedFastDoubleElement(
1968 HStoreKeyedFastDoubleElement* instr) {
1969 ASSERT(instr->value()->representation().IsDouble());
1970 ASSERT(instr->elements()->representation().IsTagged());
1971 ASSERT(instr->key()->representation().IsInteger32());
1972
1973 LOperand* elements = UseRegisterAtStart(instr->elements());
1974 LOperand* val = UseTempRegister(instr->value());
1975 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1976
1977 return new LStoreKeyedFastDoubleElement(elements, key, val);
1978}
1979
1980
1981LInstruction* LChunkBuilder::DoStoreKeyedSpecializedArrayElement(
1982 HStoreKeyedSpecializedArrayElement* instr) {
1983 Representation representation(instr->value()->representation());
1984 ElementsKind elements_kind = instr->elements_kind();
1985 ASSERT(
1986 (representation.IsInteger32() &&
1987 (elements_kind != EXTERNAL_FLOAT_ELEMENTS) &&
1988 (elements_kind != EXTERNAL_DOUBLE_ELEMENTS)) ||
1989 (representation.IsDouble() &&
1990 ((elements_kind == EXTERNAL_FLOAT_ELEMENTS) ||
1991 (elements_kind == EXTERNAL_DOUBLE_ELEMENTS))));
1992 ASSERT(instr->external_pointer()->representation().IsExternal());
1993 ASSERT(instr->key()->representation().IsInteger32());
1994
1995 LOperand* external_pointer = UseRegister(instr->external_pointer());
1996 bool val_is_temp_register =
1997 elements_kind == EXTERNAL_PIXEL_ELEMENTS ||
1998 elements_kind == EXTERNAL_FLOAT_ELEMENTS;
1999 LOperand* val = val_is_temp_register
2000 ? UseTempRegister(instr->value())
2001 : UseRegister(instr->value());
2002 LOperand* key = UseRegisterOrConstant(instr->key());
2003
2004 return new LStoreKeyedSpecializedArrayElement(external_pointer,
2005 key,
2006 val);
2007}
2008
2009
2010LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
2011 LOperand* obj = UseFixed(instr->object(), a2);
2012 LOperand* key = UseFixed(instr->key(), a1);
2013 LOperand* val = UseFixed(instr->value(), a0);
2014
2015 ASSERT(instr->object()->representation().IsTagged());
2016 ASSERT(instr->key()->representation().IsTagged());
2017 ASSERT(instr->value()->representation().IsTagged());
2018
2019 return MarkAsCall(new LStoreKeyedGeneric(obj, key, val), instr);
2020}
2021
2022
2023LInstruction* LChunkBuilder::DoTransitionElementsKind(
2024 HTransitionElementsKind* instr) {
2025 if (instr->original_map()->elements_kind() == FAST_SMI_ONLY_ELEMENTS &&
2026 instr->transitioned_map()->elements_kind() == FAST_ELEMENTS) {
2027 LOperand* object = UseRegister(instr->object());
2028 LOperand* new_map_reg = TempRegister();
2029 LTransitionElementsKind* result =
2030 new LTransitionElementsKind(object, new_map_reg, NULL);
2031 return DefineSameAsFirst(result);
2032 } else {
2033 LOperand* object = UseFixed(instr->object(), a0);
2034 LOperand* fixed_object_reg = FixedTemp(a2);
2035 LOperand* new_map_reg = FixedTemp(a3);
2036 LTransitionElementsKind* result =
2037 new LTransitionElementsKind(object, new_map_reg, fixed_object_reg);
2038 return MarkAsCall(DefineFixed(result, v0), instr);
2039 }
2040}
2041
2042
2043LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
2044 bool needs_write_barrier = instr->NeedsWriteBarrier();
2045
2046 LOperand* obj = needs_write_barrier
2047 ? UseTempRegister(instr->object())
2048 : UseRegisterAtStart(instr->object());
2049
2050 LOperand* val = needs_write_barrier
2051 ? UseTempRegister(instr->value())
2052 : UseRegister(instr->value());
2053
2054 return new LStoreNamedField(obj, val);
2055}
2056
2057
2058LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
2059 LOperand* obj = UseFixed(instr->object(), a1);
2060 LOperand* val = UseFixed(instr->value(), a0);
2061
2062 LInstruction* result = new LStoreNamedGeneric(obj, val);
2063 return MarkAsCall(result, instr);
2064}
2065
2066
2067LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
2068 LOperand* left = UseRegisterAtStart(instr->left());
2069 LOperand* right = UseRegisterAtStart(instr->right());
2070 return MarkAsCall(DefineFixed(new LStringAdd(left, right), v0), instr);
2071}
2072
2073
2074LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
2075 LOperand* string = UseTempRegister(instr->string());
2076 LOperand* index = UseTempRegister(instr->index());
2077 LStringCharCodeAt* result = new LStringCharCodeAt(string, index);
2078 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
2079}
2080
2081
2082LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2083 LOperand* char_code = UseRegister(instr->value());
2084 LStringCharFromCode* result = new LStringCharFromCode(char_code);
2085 return AssignPointerMap(DefineAsRegister(result));
2086}
2087
2088
2089LInstruction* LChunkBuilder::DoStringLength(HStringLength* instr) {
2090 LOperand* string = UseRegisterAtStart(instr->value());
2091 return DefineAsRegister(new LStringLength(string));
2092}
2093
2094
2095LInstruction* LChunkBuilder::DoArrayLiteral(HArrayLiteral* instr) {
2096 return MarkAsCall(DefineFixed(new LArrayLiteral, v0), instr);
2097}
2098
2099
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002100LInstruction* LChunkBuilder::DoObjectLiteralFast(HObjectLiteralFast* instr) {
2101 return MarkAsCall(DefineFixed(new LObjectLiteralFast, v0), instr);
2102}
2103
2104
2105LInstruction* LChunkBuilder::DoObjectLiteralGeneric(
2106 HObjectLiteralGeneric* instr) {
2107 return MarkAsCall(DefineFixed(new LObjectLiteralGeneric, v0), instr);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002108}
2109
2110
2111LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2112 return MarkAsCall(DefineFixed(new LRegExpLiteral, v0), instr);
2113}
2114
2115
2116LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2117 return MarkAsCall(DefineFixed(new LFunctionLiteral, v0), instr);
2118}
2119
2120
2121LInstruction* LChunkBuilder::DoDeleteProperty(HDeleteProperty* instr) {
2122 LOperand* object = UseFixed(instr->object(), a0);
2123 LOperand* key = UseFixed(instr->key(), a1);
2124 LDeleteProperty* result = new LDeleteProperty(object, key);
2125 return MarkAsCall(DefineFixed(result, v0), instr);
2126}
2127
2128
2129LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2130 allocator_->MarkAsOsrEntry();
2131 current_block_->last_environment()->set_ast_id(instr->ast_id());
2132 return AssignEnvironment(new LOsrEntry);
2133}
2134
2135
2136LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2137 int spill_index = chunk()->GetParameterStackSlot(instr->index());
2138 return DefineAsSpilled(new LParameter, spill_index);
2139}
2140
2141
2142LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2143 int spill_index = chunk()->GetNextSpillIndex(false); // Not double-width.
2144 if (spill_index > LUnallocated::kMaxFixedIndex) {
2145 Abort("Too many spill slots needed for OSR");
2146 spill_index = 0;
2147 }
2148 return DefineAsSpilled(new LUnknownOSRValue, spill_index);
2149}
2150
2151
2152LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2153 argument_count_ -= instr->argument_count();
2154 return MarkAsCall(DefineFixed(new LCallStub, v0), instr);
2155}
2156
2157
2158LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
2159 // There are no real uses of the arguments object.
2160 // arguments.length and element access are supported directly on
2161 // stack arguments, and any real arguments object use causes a bailout.
2162 // So this value is never used.
2163 return NULL;
2164}
2165
2166
2167LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2168 LOperand* arguments = UseRegister(instr->arguments());
2169 LOperand* length = UseTempRegister(instr->length());
2170 LOperand* index = UseRegister(instr->index());
2171 LAccessArgumentsAt* result = new LAccessArgumentsAt(arguments, length, index);
2172 return AssignEnvironment(DefineAsRegister(result));
2173}
2174
2175
2176LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2177 LOperand* object = UseFixed(instr->value(), a0);
2178 LToFastProperties* result = new LToFastProperties(object);
2179 return MarkAsCall(DefineFixed(result, v0), instr);
2180}
2181
2182
2183LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2184 LTypeof* result = new LTypeof(UseFixed(instr->value(), a0));
2185 return MarkAsCall(DefineFixed(result, v0), instr);
2186}
2187
2188
2189LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2190 return new LTypeofIsAndBranch(UseTempRegister(instr->value()));
2191}
2192
2193
2194LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
2195 HIsConstructCallAndBranch* instr) {
2196 return new LIsConstructCallAndBranch(TempRegister());
2197}
2198
2199
2200LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2201 HEnvironment* env = current_block_->last_environment();
2202 ASSERT(env != NULL);
2203
2204 env->set_ast_id(instr->ast_id());
2205
2206 env->Drop(instr->pop_count());
2207 for (int i = 0; i < instr->values()->length(); ++i) {
2208 HValue* value = instr->values()->at(i);
2209 if (instr->HasAssignedIndexAt(i)) {
2210 env->Bind(instr->GetAssignedIndexAt(i), value);
2211 } else {
2212 env->Push(value);
2213 }
2214 }
2215
2216 // If there is an instruction pending deoptimization environment create a
2217 // lazy bailout instruction to capture the environment.
2218 if (pending_deoptimization_ast_id_ == instr->ast_id()) {
2219 LInstruction* result = new LLazyBailout;
2220 result = AssignEnvironment(result);
2221 instruction_pending_deoptimization_environment_->
2222 set_deoptimization_environment(result->environment());
2223 ClearInstructionPendingDeoptimizationEnvironment();
2224 return result;
2225 }
2226
2227 return NULL;
2228}
2229
2230
2231LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2232 if (instr->is_function_entry()) {
2233 return MarkAsCall(new LStackCheck, instr);
2234 } else {
2235 ASSERT(instr->is_backwards_branch());
2236 return AssignEnvironment(AssignPointerMap(new LStackCheck));
2237 }
2238}
2239
2240
2241LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2242 HEnvironment* outer = current_block_->last_environment();
2243 HConstant* undefined = graph()->GetConstantUndefined();
2244 HEnvironment* inner = outer->CopyForInlining(instr->closure(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002245 instr->arguments_count(),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002246 instr->function(),
2247 undefined,
2248 instr->call_kind());
2249 current_block_->UpdateEnvironment(inner);
2250 chunk_->AddInlinedClosure(instr->closure());
2251 return NULL;
2252}
2253
2254
2255LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002256 HEnvironment* outer = current_block_->last_environment()->
2257 DiscardInlined(false);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002258 current_block_->UpdateEnvironment(outer);
2259 return NULL;
2260}
2261
2262
2263LInstruction* LChunkBuilder::DoIn(HIn* instr) {
2264 LOperand* key = UseRegisterAtStart(instr->key());
2265 LOperand* object = UseRegisterAtStart(instr->object());
2266 LIn* result = new LIn(key, object);
2267 return MarkAsCall(DefineFixed(result, v0), instr);
2268}
2269
2270
2271} } // namespace v8::internal