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