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