blob: 6292ff85bd81275d9866ec04d6a56d2830294bb5 [file] [log] [blame]
Ben Murdoch086aeea2011-05-13 15:57:08 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002// 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
Steve Block44f0eee2011-05-26 01:26:41 +010028#include "v8.h"
29
Steve Block1e0659c2011-05-24 12:43:12 +010030#include "lithium-allocator-inl.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010031#include "arm/lithium-arm.h"
32#include "arm/lithium-codegen-arm.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
Steve Block1e0659c2011-05-24 12:43:12 +010062#ifdef DEBUG
63void LInstruction::VerifyCall() {
Ben Murdoch257744e2011-11-30 15:57:28 +000064 // 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.
Steve Block1e0659c2011-05-24 12:43:12 +010068 ASSERT(Output() == NULL ||
69 LUnallocated::cast(Output())->HasFixedPolicy() ||
70 !LUnallocated::cast(Output())->HasRegisterPolicy());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000071 for (UseIterator it(this); !it.Done(); it.Advance()) {
72 LUnallocated* operand = LUnallocated::cast(it.Current());
Ben Murdoch257744e2011-11-30 15:57:28 +000073 ASSERT(operand->HasFixedPolicy() ||
74 operand->IsUsedAtStart());
Steve Block1e0659c2011-05-24 12:43:12 +010075 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000076 for (TempIterator it(this); !it.Done(); it.Advance()) {
77 LUnallocated* operand = LUnallocated::cast(it.Current());
Ben Murdoch257744e2011-11-30 15:57:28 +000078 ASSERT(operand->HasFixedPolicy() ||!operand->HasRegisterPolicy());
Steve Block1e0659c2011-05-24 12:43:12 +010079 }
80}
81#endif
82
83
Ben Murdochb0fe1622011-05-05 13:52:32 +010084void 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
Steve Block1e0659c2011-05-24 12:43:12 +010092void LInstruction::PrintTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +010093 stream->Add("%s ", this->Mnemonic());
Steve Block1e0659c2011-05-24 12:43:12 +010094
95 PrintOutputOperandTo(stream);
96
Ben Murdochb0fe1622011-05-05 13:52:32 +010097 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
Steve Block1e0659c2011-05-24 12:43:12 +0100111template<int R, int I, int T>
112void LTemplateInstruction<R, I, T>::PrintDataTo(StringStream* stream) {
113 stream->Add("= ");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000114 for (int i = 0; i < inputs_.length(); i++) {
115 if (i > 0) stream->Add(" ");
116 inputs_[i]->PrintTo(stream);
117 }
Steve Block1e0659c2011-05-24 12:43:12 +0100118}
119
120
121template<int R, int I, int T>
122void LTemplateInstruction<R, I, T>::PrintOutputOperandTo(StringStream* stream) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000123 for (int i = 0; i < results_.length(); i++) {
Steve Block1e0659c2011-05-24 12:43:12 +0100124 if (i > 0) stream->Add(" ");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000125 results_[i]->PrintTo(stream);
Steve Block1e0659c2011-05-24 12:43:12 +0100126 }
127}
128
129
130void LLabel::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100131 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
Ben Murdochb0fe1622011-05-05 13:52:32 +0100139bool 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
Ben Murdoch257744e2011-11-30 15:57:28 +0000150void LGap::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100151 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";
Steve Block1e0659c2011-05-24 12:43:12 +0100182 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 "shl-t";
186 case Token::SAR: return "sar-t";
187 case Token::SHR: return "shr-t";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100188 default:
189 UNREACHABLE();
190 return NULL;
191 }
192}
193
194
Steve Block1e0659c2011-05-24 12:43:12 +0100195void LGoto::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100196 stream->Add("B%d", block_id());
197}
198
199
Steve Block1e0659c2011-05-24 12:43:12 +0100200void LBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100201 stream->Add("B%d | B%d on ", true_block_id(), false_block_id());
Steve Block1e0659c2011-05-24 12:43:12 +0100202 InputAt(0)->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100203}
204
205
Steve Block1e0659c2011-05-24 12:43:12 +0100206void LCmpIDAndBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100207 stream->Add("if ");
Steve Block1e0659c2011-05-24 12:43:12 +0100208 InputAt(0)->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100209 stream->Add(" %s ", Token::String(op()));
Steve Block1e0659c2011-05-24 12:43:12 +0100210 InputAt(1)->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100211 stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
212}
213
214
Steve Block1e0659c2011-05-24 12:43:12 +0100215void LIsNullAndBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100216 stream->Add("if ");
Steve Block1e0659c2011-05-24 12:43:12 +0100217 InputAt(0)->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100218 stream->Add(is_strict() ? " === null" : " == null");
219 stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
220}
221
222
Steve Block1e0659c2011-05-24 12:43:12 +0100223void LIsObjectAndBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100224 stream->Add("if is_object(");
Steve Block1e0659c2011-05-24 12:43:12 +0100225 InputAt(0)->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100226 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
227}
228
229
Steve Block1e0659c2011-05-24 12:43:12 +0100230void LIsSmiAndBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100231 stream->Add("if is_smi(");
Steve Block1e0659c2011-05-24 12:43:12 +0100232 InputAt(0)->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100233 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
234}
235
236
Ben Murdoch257744e2011-11-30 15:57:28 +0000237void LIsUndetectableAndBranch::PrintDataTo(StringStream* stream) {
238 stream->Add("if is_undetectable(");
239 InputAt(0)->PrintTo(stream);
240 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
241}
242
243
Steve Block1e0659c2011-05-24 12:43:12 +0100244void LHasInstanceTypeAndBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100245 stream->Add("if has_instance_type(");
Steve Block1e0659c2011-05-24 12:43:12 +0100246 InputAt(0)->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100247 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
248}
249
250
Steve Block1e0659c2011-05-24 12:43:12 +0100251void LHasCachedArrayIndexAndBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100252 stream->Add("if has_cached_array_index(");
Steve Block1e0659c2011-05-24 12:43:12 +0100253 InputAt(0)->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100254 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
255}
256
257
Steve Block1e0659c2011-05-24 12:43:12 +0100258void LClassOfTestAndBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100259 stream->Add("if class_of_test(");
Steve Block1e0659c2011-05-24 12:43:12 +0100260 InputAt(0)->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100261 stream->Add(", \"%o\") then B%d else B%d",
262 *hydrogen()->class_name(),
263 true_block_id(),
264 false_block_id());
265}
266
267
Steve Block1e0659c2011-05-24 12:43:12 +0100268void LTypeofIsAndBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100269 stream->Add("if typeof ");
Steve Block1e0659c2011-05-24 12:43:12 +0100270 InputAt(0)->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100271 stream->Add(" == \"%s\" then B%d else B%d",
272 *hydrogen()->type_literal()->ToCString(),
273 true_block_id(), false_block_id());
274}
275
276
Steve Block1e0659c2011-05-24 12:43:12 +0100277void LCallConstantFunction::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100278 stream->Add("#%d / ", arity());
279}
280
281
Steve Block1e0659c2011-05-24 12:43:12 +0100282void LUnaryMathOperation::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100283 stream->Add("/%s ", hydrogen()->OpName());
Steve Block1e0659c2011-05-24 12:43:12 +0100284 InputAt(0)->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100285}
286
287
Ben Murdochb8e0da22011-05-16 14:20:40 +0100288void LLoadContextSlot::PrintDataTo(StringStream* stream) {
Steve Block1e0659c2011-05-24 12:43:12 +0100289 InputAt(0)->PrintTo(stream);
290 stream->Add("[%d]", slot_index());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100291}
292
293
Steve Block1e0659c2011-05-24 12:43:12 +0100294void LStoreContextSlot::PrintDataTo(StringStream* stream) {
295 InputAt(0)->PrintTo(stream);
296 stream->Add("[%d] <- ", slot_index());
297 InputAt(1)->PrintTo(stream);
298}
299
300
Ben Murdoch257744e2011-11-30 15:57:28 +0000301void LInvokeFunction::PrintDataTo(StringStream* stream) {
302 stream->Add("= ");
303 InputAt(0)->PrintTo(stream);
304 stream->Add(" #%d / ", arity());
305}
306
307
Steve Block1e0659c2011-05-24 12:43:12 +0100308void LCallKeyed::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100309 stream->Add("[r2] #%d / ", arity());
310}
311
312
Steve Block1e0659c2011-05-24 12:43:12 +0100313void LCallNamed::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100314 SmartPointer<char> name_string = name()->ToCString();
315 stream->Add("%s #%d / ", *name_string, arity());
316}
317
318
Steve Block1e0659c2011-05-24 12:43:12 +0100319void LCallGlobal::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100320 SmartPointer<char> name_string = name()->ToCString();
321 stream->Add("%s #%d / ", *name_string, arity());
322}
323
324
Steve Block1e0659c2011-05-24 12:43:12 +0100325void LCallKnownGlobal::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100326 stream->Add("#%d / ", arity());
327}
328
329
Steve Block1e0659c2011-05-24 12:43:12 +0100330void LCallNew::PrintDataTo(StringStream* stream) {
331 stream->Add("= ");
332 InputAt(0)->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100333 stream->Add(" #%d / ", arity());
334}
335
336
Steve Block1e0659c2011-05-24 12:43:12 +0100337void LAccessArgumentsAt::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100338 arguments()->PrintTo(stream);
339
340 stream->Add(" length ");
341 length()->PrintTo(stream);
342
343 stream->Add(" index ");
344 index()->PrintTo(stream);
345}
346
347
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100348void LStoreNamedField::PrintDataTo(StringStream* stream) {
Steve Block1e0659c2011-05-24 12:43:12 +0100349 object()->PrintTo(stream);
350 stream->Add(".");
351 stream->Add(*String::cast(*name())->ToCString());
352 stream->Add(" <- ");
353 value()->PrintTo(stream);
354}
355
356
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100357void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
358 object()->PrintTo(stream);
359 stream->Add(".");
360 stream->Add(*String::cast(*name())->ToCString());
361 stream->Add(" <- ");
362 value()->PrintTo(stream);
363}
364
365
366void LStoreKeyedFastElement::PrintDataTo(StringStream* stream) {
Steve Block1e0659c2011-05-24 12:43:12 +0100367 object()->PrintTo(stream);
368 stream->Add("[");
369 key()->PrintTo(stream);
370 stream->Add("] <- ");
371 value()->PrintTo(stream);
372}
373
374
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000375void LStoreKeyedFastDoubleElement::PrintDataTo(StringStream* stream) {
376 elements()->PrintTo(stream);
377 stream->Add("[");
378 key()->PrintTo(stream);
379 stream->Add("] <- ");
380 value()->PrintTo(stream);
381}
382
383
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100384void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
385 object()->PrintTo(stream);
386 stream->Add("[");
387 key()->PrintTo(stream);
388 stream->Add("] <- ");
389 value()->PrintTo(stream);
390}
391
392
393LChunk::LChunk(CompilationInfo* info, HGraph* graph)
Ben Murdochb0fe1622011-05-05 13:52:32 +0100394 : spill_slot_count_(0),
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100395 info_(info),
Ben Murdochb0fe1622011-05-05 13:52:32 +0100396 graph_(graph),
397 instructions_(32),
398 pointer_maps_(8),
399 inlined_closures_(1) {
400}
401
402
Ben Murdochb0fe1622011-05-05 13:52:32 +0100403int LChunk::GetNextSpillIndex(bool is_double) {
404 // Skip a slot if for a double-width slot.
405 if (is_double) spill_slot_count_++;
406 return spill_slot_count_++;
407}
408
409
410LOperand* LChunk::GetNextSpillSlot(bool is_double) {
411 int index = GetNextSpillIndex(is_double);
412 if (is_double) {
413 return LDoubleStackSlot::Create(index);
414 } else {
415 return LStackSlot::Create(index);
416 }
417}
418
419
420void LChunk::MarkEmptyBlocks() {
421 HPhase phase("Mark empty blocks", this);
422 for (int i = 0; i < graph()->blocks()->length(); ++i) {
423 HBasicBlock* block = graph()->blocks()->at(i);
424 int first = block->first_instruction_index();
425 int last = block->last_instruction_index();
426 LInstruction* first_instr = instructions()->at(first);
427 LInstruction* last_instr = instructions()->at(last);
428
429 LLabel* label = LLabel::cast(first_instr);
430 if (last_instr->IsGoto()) {
431 LGoto* goto_instr = LGoto::cast(last_instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000432 if (label->IsRedundant() &&
Ben Murdochb0fe1622011-05-05 13:52:32 +0100433 !label->is_loop_header()) {
434 bool can_eliminate = true;
435 for (int i = first + 1; i < last && can_eliminate; ++i) {
436 LInstruction* cur = instructions()->at(i);
437 if (cur->IsGap()) {
438 LGap* gap = LGap::cast(cur);
439 if (!gap->IsRedundant()) {
440 can_eliminate = false;
441 }
442 } else {
443 can_eliminate = false;
444 }
445 }
446
447 if (can_eliminate) {
448 label->set_replacement(GetLabel(goto_instr->block_id()));
449 }
450 }
451 }
452 }
453}
454
455
Steve Block1e0659c2011-05-24 12:43:12 +0100456void LChunk::AddInstruction(LInstruction* instr, HBasicBlock* block) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000457 LInstructionGap* gap = new LInstructionGap(block);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100458 int index = -1;
459 if (instr->IsControl()) {
460 instructions_.Add(gap);
461 index = instructions_.length();
462 instructions_.Add(instr);
463 } else {
464 index = instructions_.length();
465 instructions_.Add(instr);
466 instructions_.Add(gap);
467 }
468 if (instr->HasPointerMap()) {
469 pointer_maps_.Add(instr->pointer_map());
470 instr->pointer_map()->set_lithium_position(index);
471 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100472}
473
474
475LConstantOperand* LChunk::DefineConstantOperand(HConstant* constant) {
476 return LConstantOperand::Create(constant->id());
477}
478
479
480int LChunk::GetParameterStackSlot(int index) const {
481 // The receiver is at index 0, the first parameter at index 1, so we
482 // shift all parameter indexes down by the number of parameters, and
483 // make sure they end up negative so they are distinguishable from
484 // spill slots.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100485 int result = index - info()->scope()->num_parameters() - 1;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100486 ASSERT(result < 0);
487 return result;
488}
489
490// A parameter relative to ebp in the arguments stub.
491int LChunk::ParameterAt(int index) {
492 ASSERT(-1 <= index); // -1 is the receiver.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100493 return (1 + info()->scope()->num_parameters() - index) *
Ben Murdochb0fe1622011-05-05 13:52:32 +0100494 kPointerSize;
495}
496
497
498LGap* LChunk::GetGapAt(int index) const {
499 return LGap::cast(instructions_[index]);
500}
501
502
503bool LChunk::IsGapAt(int index) const {
504 return instructions_[index]->IsGap();
505}
506
507
508int LChunk::NearestGapPos(int index) const {
509 while (!IsGapAt(index)) index--;
510 return index;
511}
512
513
514void LChunk::AddGapMove(int index, LOperand* from, LOperand* to) {
515 GetGapAt(index)->GetOrCreateParallelMove(LGap::START)->AddMove(from, to);
516}
517
518
Ben Murdochb0fe1622011-05-05 13:52:32 +0100519Handle<Object> LChunk::LookupLiteral(LConstantOperand* operand) const {
520 return HConstant::cast(graph_->LookupValue(operand->index()))->handle();
521}
522
523
524Representation LChunk::LookupLiteralRepresentation(
525 LConstantOperand* operand) const {
526 return graph_->LookupValue(operand->index())->representation();
527}
528
529
530LChunk* LChunkBuilder::Build() {
531 ASSERT(is_unused());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100532 chunk_ = new LChunk(info(), graph());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100533 HPhase phase("Building chunk", chunk_);
534 status_ = BUILDING;
535 const ZoneList<HBasicBlock*>* blocks = graph()->blocks();
536 for (int i = 0; i < blocks->length(); i++) {
537 HBasicBlock* next = NULL;
538 if (i < blocks->length() - 1) next = blocks->at(i + 1);
539 DoBasicBlock(blocks->at(i), next);
540 if (is_aborted()) return NULL;
541 }
542 status_ = DONE;
543 return chunk_;
544}
545
546
547void LChunkBuilder::Abort(const char* format, ...) {
548 if (FLAG_trace_bailout) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100549 SmartPointer<char> name(info()->shared_info()->DebugName()->ToCString());
550 PrintF("Aborting LChunk building in @\"%s\": ", *name);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100551 va_list arguments;
552 va_start(arguments, format);
553 OS::VPrint(format, arguments);
554 va_end(arguments);
555 PrintF("\n");
556 }
557 status_ = ABORTED;
558}
559
560
561LRegister* LChunkBuilder::ToOperand(Register reg) {
562 return LRegister::Create(Register::ToAllocationIndex(reg));
563}
564
565
566LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
567 return new LUnallocated(LUnallocated::FIXED_REGISTER,
568 Register::ToAllocationIndex(reg));
569}
570
571
572LUnallocated* LChunkBuilder::ToUnallocated(DoubleRegister reg) {
573 return new LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
574 DoubleRegister::ToAllocationIndex(reg));
575}
576
577
578LOperand* LChunkBuilder::UseFixed(HValue* value, Register fixed_register) {
579 return Use(value, ToUnallocated(fixed_register));
580}
581
582
583LOperand* LChunkBuilder::UseFixedDouble(HValue* value, DoubleRegister reg) {
584 return Use(value, ToUnallocated(reg));
585}
586
587
588LOperand* LChunkBuilder::UseRegister(HValue* value) {
589 return Use(value, new LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
590}
591
592
593LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
594 return Use(value,
595 new LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
596 LUnallocated::USED_AT_START));
597}
598
599
600LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
601 return Use(value, new LUnallocated(LUnallocated::WRITABLE_REGISTER));
602}
603
604
605LOperand* LChunkBuilder::Use(HValue* value) {
606 return Use(value, new LUnallocated(LUnallocated::NONE));
607}
608
609
610LOperand* LChunkBuilder::UseAtStart(HValue* value) {
611 return Use(value, new LUnallocated(LUnallocated::NONE,
612 LUnallocated::USED_AT_START));
613}
614
615
616LOperand* LChunkBuilder::UseOrConstant(HValue* value) {
617 return value->IsConstant()
618 ? chunk_->DefineConstantOperand(HConstant::cast(value))
619 : Use(value);
620}
621
622
623LOperand* LChunkBuilder::UseOrConstantAtStart(HValue* value) {
624 return value->IsConstant()
625 ? chunk_->DefineConstantOperand(HConstant::cast(value))
626 : UseAtStart(value);
627}
628
629
630LOperand* LChunkBuilder::UseRegisterOrConstant(HValue* value) {
631 return value->IsConstant()
632 ? chunk_->DefineConstantOperand(HConstant::cast(value))
633 : UseRegister(value);
634}
635
636
637LOperand* LChunkBuilder::UseRegisterOrConstantAtStart(HValue* value) {
638 return value->IsConstant()
639 ? chunk_->DefineConstantOperand(HConstant::cast(value))
640 : UseRegisterAtStart(value);
641}
642
643
Ben Murdochb8e0da22011-05-16 14:20:40 +0100644LOperand* LChunkBuilder::UseAny(HValue* value) {
645 return value->IsConstant()
646 ? chunk_->DefineConstantOperand(HConstant::cast(value))
647 : Use(value, new LUnallocated(LUnallocated::ANY));
648}
649
650
Ben Murdochb0fe1622011-05-05 13:52:32 +0100651LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
652 if (value->EmitAtUses()) {
653 HInstruction* instr = HInstruction::cast(value);
654 VisitInstruction(instr);
655 }
656 allocator_->RecordUse(value, operand);
657 return operand;
658}
659
660
Steve Block1e0659c2011-05-24 12:43:12 +0100661template<int I, int T>
662LInstruction* LChunkBuilder::Define(LTemplateInstruction<1, I, T>* instr,
663 LUnallocated* result) {
664 allocator_->RecordDefinition(current_instruction_, result);
665 instr->set_result(result);
666 return instr;
667}
668
669
670template<int I, int T>
671LInstruction* LChunkBuilder::Define(LTemplateInstruction<1, I, T>* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100672 return Define(instr, new LUnallocated(LUnallocated::NONE));
673}
674
675
Steve Block1e0659c2011-05-24 12:43:12 +0100676template<int I, int T>
677LInstruction* LChunkBuilder::DefineAsRegister(
678 LTemplateInstruction<1, I, T>* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100679 return Define(instr, new LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
680}
681
682
Steve Block1e0659c2011-05-24 12:43:12 +0100683template<int I, int T>
684LInstruction* LChunkBuilder::DefineAsSpilled(
685 LTemplateInstruction<1, I, T>* instr, int index) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100686 return Define(instr, new LUnallocated(LUnallocated::FIXED_SLOT, index));
687}
688
689
Steve Block1e0659c2011-05-24 12:43:12 +0100690template<int I, int T>
691LInstruction* LChunkBuilder::DefineSameAsFirst(
692 LTemplateInstruction<1, I, T>* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100693 return Define(instr, new LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
694}
695
696
Steve Block1e0659c2011-05-24 12:43:12 +0100697template<int I, int T>
698LInstruction* LChunkBuilder::DefineFixed(
699 LTemplateInstruction<1, I, T>* instr, Register reg) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100700 return Define(instr, ToUnallocated(reg));
701}
702
703
Steve Block1e0659c2011-05-24 12:43:12 +0100704template<int I, int T>
705LInstruction* LChunkBuilder::DefineFixedDouble(
706 LTemplateInstruction<1, I, T>* instr, DoubleRegister reg) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100707 return Define(instr, ToUnallocated(reg));
708}
709
710
711LInstruction* LChunkBuilder::AssignEnvironment(LInstruction* instr) {
712 HEnvironment* hydrogen_env = current_block_->last_environment();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000713 int argument_index_accumulator = 0;
714 instr->set_environment(CreateEnvironment(hydrogen_env,
715 &argument_index_accumulator));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100716 return instr;
717}
718
719
720LInstruction* LChunkBuilder::SetInstructionPendingDeoptimizationEnvironment(
721 LInstruction* instr, int ast_id) {
Steve Block1e0659c2011-05-24 12:43:12 +0100722 ASSERT(instruction_pending_deoptimization_environment_ == NULL);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100723 ASSERT(pending_deoptimization_ast_id_ == AstNode::kNoNumber);
Steve Block1e0659c2011-05-24 12:43:12 +0100724 instruction_pending_deoptimization_environment_ = instr;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100725 pending_deoptimization_ast_id_ = ast_id;
726 return instr;
727}
728
729
730void LChunkBuilder::ClearInstructionPendingDeoptimizationEnvironment() {
Steve Block1e0659c2011-05-24 12:43:12 +0100731 instruction_pending_deoptimization_environment_ = NULL;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100732 pending_deoptimization_ast_id_ = AstNode::kNoNumber;
733}
734
735
736LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
737 HInstruction* hinstr,
738 CanDeoptimize can_deoptimize) {
Steve Block1e0659c2011-05-24 12:43:12 +0100739#ifdef DEBUG
740 instr->VerifyCall();
741#endif
742 instr->MarkAsCall();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100743 instr = AssignPointerMap(instr);
744
745 if (hinstr->HasSideEffects()) {
746 ASSERT(hinstr->next()->IsSimulate());
747 HSimulate* sim = HSimulate::cast(hinstr->next());
748 instr = SetInstructionPendingDeoptimizationEnvironment(
749 instr, sim->ast_id());
750 }
751
752 // If instruction does not have side-effects lazy deoptimization
753 // after the call will try to deoptimize to the point before the call.
754 // Thus we still need to attach environment to this call even if
755 // call sequence can not deoptimize eagerly.
756 bool needs_environment =
757 (can_deoptimize == CAN_DEOPTIMIZE_EAGERLY) || !hinstr->HasSideEffects();
758 if (needs_environment && !instr->HasEnvironment()) {
759 instr = AssignEnvironment(instr);
760 }
761
762 return instr;
763}
764
765
Steve Block1e0659c2011-05-24 12:43:12 +0100766LInstruction* LChunkBuilder::MarkAsSaveDoubles(LInstruction* instr) {
767 instr->MarkAsSaveDoubles();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100768 return instr;
769}
770
771
Steve Block1e0659c2011-05-24 12:43:12 +0100772LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
773 ASSERT(!instr->HasPointerMap());
774 instr->set_pointer_map(new LPointerMap(position_));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100775 return instr;
776}
777
778
Ben Murdochb0fe1622011-05-05 13:52:32 +0100779LUnallocated* LChunkBuilder::TempRegister() {
780 LUnallocated* operand = new LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
781 allocator_->RecordTemporary(operand);
782 return operand;
783}
784
785
786LOperand* LChunkBuilder::FixedTemp(Register reg) {
787 LUnallocated* operand = ToUnallocated(reg);
788 allocator_->RecordTemporary(operand);
789 return operand;
790}
791
792
793LOperand* LChunkBuilder::FixedTemp(DoubleRegister reg) {
794 LUnallocated* operand = ToUnallocated(reg);
795 allocator_->RecordTemporary(operand);
796 return operand;
797}
798
799
800LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
801 return new LLabel(instr->block());
802}
803
804
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000805LInstruction* LChunkBuilder::DoSoftDeoptimize(HSoftDeoptimize* instr) {
806 return AssignEnvironment(new LDeoptimize);
807}
808
809
Ben Murdochb0fe1622011-05-05 13:52:32 +0100810LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
811 return AssignEnvironment(new LDeoptimize);
812}
813
814
815LInstruction* LChunkBuilder::DoBit(Token::Value op,
816 HBitwiseBinaryOperation* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +0100817 if (instr->representation().IsInteger32()) {
818 ASSERT(instr->left()->representation().IsInteger32());
819 ASSERT(instr->right()->representation().IsInteger32());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100820
Steve Block1e0659c2011-05-24 12:43:12 +0100821 LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
822 LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000823 return DefineAsRegister(new LBitI(op, left, right));
Steve Block1e0659c2011-05-24 12:43:12 +0100824 } else {
825 ASSERT(instr->representation().IsTagged());
826 ASSERT(instr->left()->representation().IsTagged());
827 ASSERT(instr->right()->representation().IsTagged());
828
829 LOperand* left = UseFixed(instr->left(), r1);
830 LOperand* right = UseFixed(instr->right(), r0);
831 LArithmeticT* result = new LArithmeticT(op, left, right);
832 return MarkAsCall(DefineFixed(result, r0), instr);
833 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100834}
835
836
837LInstruction* LChunkBuilder::DoShift(Token::Value op,
838 HBitwiseBinaryOperation* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +0100839 if (instr->representation().IsTagged()) {
840 ASSERT(instr->left()->representation().IsTagged());
841 ASSERT(instr->right()->representation().IsTagged());
842
843 LOperand* left = UseFixed(instr->left(), r1);
844 LOperand* right = UseFixed(instr->right(), r0);
845 LArithmeticT* result = new LArithmeticT(op, left, right);
846 return MarkAsCall(DefineFixed(result, r0), instr);
847 }
848
Ben Murdochb0fe1622011-05-05 13:52:32 +0100849 ASSERT(instr->representation().IsInteger32());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000850 ASSERT(instr->left()->representation().IsInteger32());
851 ASSERT(instr->right()->representation().IsInteger32());
852 LOperand* left = UseRegisterAtStart(instr->left());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100853
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000854 HValue* right_value = instr->right();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100855 LOperand* right = NULL;
856 int constant_value = 0;
857 if (right_value->IsConstant()) {
858 HConstant* constant = HConstant::cast(right_value);
859 right = chunk_->DefineConstantOperand(constant);
860 constant_value = constant->Integer32Value() & 0x1f;
861 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000862 right = UseRegisterAtStart(right_value);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100863 }
864
865 // Shift operations can only deoptimize if we do a logical shift
866 // by 0 and the result cannot be truncated to int32.
Ben Murdoch257744e2011-11-30 15:57:28 +0000867 bool may_deopt = (op == Token::SHR && constant_value == 0);
868 bool does_deopt = false;
869 if (may_deopt) {
870 for (HUseIterator it(instr->uses()); !it.Done(); it.Advance()) {
871 if (!it.value()->CheckFlag(HValue::kTruncatingToInt32)) {
872 does_deopt = true;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100873 break;
874 }
875 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100876 }
877
878 LInstruction* result =
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000879 DefineAsRegister(new LShiftI(op, left, right, does_deopt));
Ben Murdoch257744e2011-11-30 15:57:28 +0000880 return does_deopt ? AssignEnvironment(result) : result;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100881}
882
883
884LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
885 HArithmeticBinaryOperation* instr) {
886 ASSERT(instr->representation().IsDouble());
887 ASSERT(instr->left()->representation().IsDouble());
888 ASSERT(instr->right()->representation().IsDouble());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100889 ASSERT(op != Token::MOD);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100890 LOperand* left = UseRegisterAtStart(instr->left());
891 LOperand* right = UseRegisterAtStart(instr->right());
892 LArithmeticD* result = new LArithmeticD(op, left, right);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000893 return DefineAsRegister(result);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100894}
895
896
897LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
898 HArithmeticBinaryOperation* instr) {
899 ASSERT(op == Token::ADD ||
900 op == Token::DIV ||
901 op == Token::MOD ||
902 op == Token::MUL ||
903 op == Token::SUB);
904 HValue* left = instr->left();
905 HValue* right = instr->right();
906 ASSERT(left->representation().IsTagged());
907 ASSERT(right->representation().IsTagged());
908 LOperand* left_operand = UseFixed(left, r1);
909 LOperand* right_operand = UseFixed(right, r0);
Steve Block1e0659c2011-05-24 12:43:12 +0100910 LArithmeticT* result = new LArithmeticT(op, left_operand, right_operand);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100911 return MarkAsCall(DefineFixed(result, r0), instr);
912}
913
Steve Block1e0659c2011-05-24 12:43:12 +0100914
Ben Murdochb0fe1622011-05-05 13:52:32 +0100915void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
916 ASSERT(is_building());
917 current_block_ = block;
918 next_block_ = next_block;
919 if (block->IsStartBlock()) {
920 block->UpdateEnvironment(graph_->start_environment());
921 argument_count_ = 0;
922 } else if (block->predecessors()->length() == 1) {
923 // We have a single predecessor => copy environment and outgoing
924 // argument count from the predecessor.
925 ASSERT(block->phis()->length() == 0);
926 HBasicBlock* pred = block->predecessors()->at(0);
927 HEnvironment* last_environment = pred->last_environment();
928 ASSERT(last_environment != NULL);
929 // Only copy the environment, if it is later used again.
930 if (pred->end()->SecondSuccessor() == NULL) {
931 ASSERT(pred->end()->FirstSuccessor() == block);
932 } else {
933 if (pred->end()->FirstSuccessor()->block_id() > block->block_id() ||
934 pred->end()->SecondSuccessor()->block_id() > block->block_id()) {
935 last_environment = last_environment->Copy();
936 }
937 }
938 block->UpdateEnvironment(last_environment);
939 ASSERT(pred->argument_count() >= 0);
940 argument_count_ = pred->argument_count();
941 } else {
942 // We are at a state join => process phis.
943 HBasicBlock* pred = block->predecessors()->at(0);
944 // No need to copy the environment, it cannot be used later.
945 HEnvironment* last_environment = pred->last_environment();
946 for (int i = 0; i < block->phis()->length(); ++i) {
947 HPhi* phi = block->phis()->at(i);
948 last_environment->SetValueAt(phi->merged_index(), phi);
949 }
950 for (int i = 0; i < block->deleted_phis()->length(); ++i) {
951 last_environment->SetValueAt(block->deleted_phis()->at(i),
952 graph_->GetConstantUndefined());
953 }
954 block->UpdateEnvironment(last_environment);
955 // Pick up the outgoing argument count of one of the predecessors.
956 argument_count_ = pred->argument_count();
957 }
958 HInstruction* current = block->first();
959 int start = chunk_->instructions()->length();
960 while (current != NULL && !is_aborted()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100961 // Code for constants in registers is generated lazily.
962 if (!current->EmitAtUses()) {
963 VisitInstruction(current);
964 }
965 current = current->next();
966 }
967 int end = chunk_->instructions()->length() - 1;
968 if (end >= start) {
969 block->set_first_instruction_index(start);
970 block->set_last_instruction_index(end);
971 }
972 block->set_argument_count(argument_count_);
973 next_block_ = NULL;
974 current_block_ = NULL;
975}
976
977
978void LChunkBuilder::VisitInstruction(HInstruction* current) {
979 HInstruction* old_current = current_instruction_;
980 current_instruction_ = current;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100981 if (current->has_position()) position_ = current->position();
982 LInstruction* instr = current->CompileToLithium(this);
983
984 if (instr != NULL) {
985 if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
986 instr = AssignPointerMap(instr);
987 }
988 if (FLAG_stress_environments && !instr->HasEnvironment()) {
989 instr = AssignEnvironment(instr);
990 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000991 instr->set_hydrogen_value(current);
Steve Block1e0659c2011-05-24 12:43:12 +0100992 chunk_->AddInstruction(instr, current_block_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100993 }
994 current_instruction_ = old_current;
995}
996
997
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000998LEnvironment* LChunkBuilder::CreateEnvironment(
999 HEnvironment* hydrogen_env,
1000 int* argument_index_accumulator) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001001 if (hydrogen_env == NULL) return NULL;
1002
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001003 LEnvironment* outer =
1004 CreateEnvironment(hydrogen_env->outer(), argument_index_accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001005 int ast_id = hydrogen_env->ast_id();
1006 ASSERT(ast_id != AstNode::kNoNumber);
Steve Block9fac8402011-05-12 15:51:54 +01001007 int value_count = hydrogen_env->length();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001008 LEnvironment* result = new LEnvironment(hydrogen_env->closure(),
1009 ast_id,
1010 hydrogen_env->parameter_count(),
1011 argument_count_,
1012 value_count,
1013 outer);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001014 for (int i = 0; i < value_count; ++i) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001015 if (hydrogen_env->is_special_index(i)) continue;
1016
Ben Murdochb0fe1622011-05-05 13:52:32 +01001017 HValue* value = hydrogen_env->values()->at(i);
1018 LOperand* op = NULL;
1019 if (value->IsArgumentsObject()) {
1020 op = NULL;
1021 } else if (value->IsPushArgument()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001022 op = new LArgument((*argument_index_accumulator)++);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001023 } else {
Ben Murdochb8e0da22011-05-16 14:20:40 +01001024 op = UseAny(value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001025 }
1026 result->AddValue(op, value->representation());
1027 }
1028
1029 return result;
1030}
1031
1032
1033LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001034 return new LGoto(instr->FirstSuccessor()->block_id());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001035}
1036
1037
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001038LInstruction* LChunkBuilder::DoBranch(HBranch* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001039 HValue* v = instr->value();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001040 if (v->EmitAtUses()) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001041 HBasicBlock* successor = HConstant::cast(v)->ToBoolean()
1042 ? instr->FirstSuccessor()
1043 : instr->SecondSuccessor();
1044 return new LGoto(successor->block_id());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001045 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001046 return AssignEnvironment(new LBranch(UseRegister(v)));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001047}
1048
1049
Ben Murdoch257744e2011-11-30 15:57:28 +00001050
Steve Block1e0659c2011-05-24 12:43:12 +01001051LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001052 ASSERT(instr->value()->representation().IsTagged());
1053 LOperand* value = UseRegisterAtStart(instr->value());
Steve Block9fac8402011-05-12 15:51:54 +01001054 LOperand* temp = TempRegister();
1055 return new LCmpMapAndBranch(value, temp);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001056}
1057
1058
1059LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* length) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01001060 return DefineAsRegister(new LArgumentsLength(UseRegister(length->value())));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001061}
1062
1063
1064LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
1065 return DefineAsRegister(new LArgumentsElements);
1066}
1067
1068
1069LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001070 LInstanceOf* result =
Steve Block9fac8402011-05-12 15:51:54 +01001071 new LInstanceOf(UseFixed(instr->left(), r0),
1072 UseFixed(instr->right(), r1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001073 return MarkAsCall(DefineFixed(result, r0), instr);
1074}
1075
1076
Ben Murdoch086aeea2011-05-13 15:57:08 +01001077LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
1078 HInstanceOfKnownGlobal* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001079 LInstanceOfKnownGlobal* result =
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001080 new LInstanceOfKnownGlobal(UseFixed(instr->left(), r0), FixedTemp(r4));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001081 return MarkAsCall(DefineFixed(result, r0), instr);
Ben Murdoch086aeea2011-05-13 15:57:08 +01001082}
1083
1084
Ben Murdochb0fe1622011-05-05 13:52:32 +01001085LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
1086 LOperand* function = UseFixed(instr->function(), r1);
1087 LOperand* receiver = UseFixed(instr->receiver(), r0);
Steve Block1e0659c2011-05-24 12:43:12 +01001088 LOperand* length = UseFixed(instr->length(), r2);
1089 LOperand* elements = UseFixed(instr->elements(), r3);
1090 LApplyArguments* result = new LApplyArguments(function,
1091 receiver,
1092 length,
1093 elements);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001094 return MarkAsCall(DefineFixed(result, r0), instr, CAN_DEOPTIMIZE_EAGERLY);
1095}
1096
1097
1098LInstruction* LChunkBuilder::DoPushArgument(HPushArgument* instr) {
1099 ++argument_count_;
1100 LOperand* argument = Use(instr->argument());
1101 return new LPushArgument(argument);
1102}
1103
1104
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001105LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1106 return instr->HasNoUses() ? NULL : DefineAsRegister(new LThisFunction);
1107}
1108
1109
Steve Block1e0659c2011-05-24 12:43:12 +01001110LInstruction* LChunkBuilder::DoContext(HContext* instr) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001111 return instr->HasNoUses() ? NULL : DefineAsRegister(new LContext);
Steve Block1e0659c2011-05-24 12:43:12 +01001112}
1113
1114
1115LInstruction* LChunkBuilder::DoOuterContext(HOuterContext* instr) {
1116 LOperand* context = UseRegisterAtStart(instr->value());
1117 return DefineAsRegister(new LOuterContext(context));
1118}
1119
1120
Ben Murdochb0fe1622011-05-05 13:52:32 +01001121LInstruction* LChunkBuilder::DoGlobalObject(HGlobalObject* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001122 LOperand* context = UseRegisterAtStart(instr->value());
1123 return DefineAsRegister(new LGlobalObject(context));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001124}
1125
1126
1127LInstruction* LChunkBuilder::DoGlobalReceiver(HGlobalReceiver* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001128 LOperand* global_object = UseRegisterAtStart(instr->value());
1129 return DefineAsRegister(new LGlobalReceiver(global_object));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001130}
1131
1132
1133LInstruction* LChunkBuilder::DoCallConstantFunction(
1134 HCallConstantFunction* instr) {
1135 argument_count_ -= instr->argument_count();
1136 return MarkAsCall(DefineFixed(new LCallConstantFunction, r0), instr);
1137}
1138
1139
Ben Murdoch257744e2011-11-30 15:57:28 +00001140LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1141 LOperand* function = UseFixed(instr->function(), r1);
1142 argument_count_ -= instr->argument_count();
1143 LInvokeFunction* result = new LInvokeFunction(function);
1144 return MarkAsCall(DefineFixed(result, r0), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1145}
1146
1147
Ben Murdochb0fe1622011-05-05 13:52:32 +01001148LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
1149 BuiltinFunctionId op = instr->op();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001150 if (op == kMathLog || op == kMathSin || op == kMathCos) {
1151 LOperand* input = UseFixedDouble(instr->value(), d2);
1152 LUnaryMathOperation* result = new LUnaryMathOperation(input, NULL);
1153 return MarkAsCall(DefineFixedDouble(result, d2), instr);
1154 } else {
1155 LOperand* input = UseRegisterAtStart(instr->value());
1156 LOperand* temp = (op == kMathFloor) ? TempRegister() : NULL;
1157 LUnaryMathOperation* result = new LUnaryMathOperation(input, temp);
1158 switch (op) {
1159 case kMathAbs:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001160 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001161 case kMathFloor:
1162 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1163 case kMathSqrt:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001164 return DefineAsRegister(result);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001165 case kMathRound:
1166 return AssignEnvironment(DefineAsRegister(result));
1167 case kMathPowHalf:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001168 return DefineAsRegister(result);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001169 default:
1170 UNREACHABLE();
1171 return NULL;
1172 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001173 }
1174}
1175
1176
1177LInstruction* LChunkBuilder::DoCallKeyed(HCallKeyed* instr) {
1178 ASSERT(instr->key()->representation().IsTagged());
1179 argument_count_ -= instr->argument_count();
Steve Block1e0659c2011-05-24 12:43:12 +01001180 LOperand* key = UseFixed(instr->key(), r2);
1181 return MarkAsCall(DefineFixed(new LCallKeyed(key), r0), instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001182}
1183
1184
1185LInstruction* LChunkBuilder::DoCallNamed(HCallNamed* instr) {
1186 argument_count_ -= instr->argument_count();
1187 return MarkAsCall(DefineFixed(new LCallNamed, r0), instr);
1188}
1189
1190
1191LInstruction* LChunkBuilder::DoCallGlobal(HCallGlobal* instr) {
1192 argument_count_ -= instr->argument_count();
1193 return MarkAsCall(DefineFixed(new LCallGlobal, r0), instr);
1194}
1195
1196
1197LInstruction* LChunkBuilder::DoCallKnownGlobal(HCallKnownGlobal* instr) {
1198 argument_count_ -= instr->argument_count();
1199 return MarkAsCall(DefineFixed(new LCallKnownGlobal, r0), instr);
1200}
1201
1202
1203LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
1204 LOperand* constructor = UseFixed(instr->constructor(), r1);
1205 argument_count_ -= instr->argument_count();
Steve Block1e0659c2011-05-24 12:43:12 +01001206 LCallNew* result = new LCallNew(constructor);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001207 return MarkAsCall(DefineFixed(result, r0), instr);
1208}
1209
1210
1211LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
1212 argument_count_ -= instr->argument_count();
1213 return MarkAsCall(DefineFixed(new LCallFunction, r0), instr);
1214}
1215
1216
1217LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1218 argument_count_ -= instr->argument_count();
1219 return MarkAsCall(DefineFixed(new LCallRuntime, r0), instr);
1220}
1221
1222
1223LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1224 return DoShift(Token::SHR, instr);
1225}
1226
1227
1228LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1229 return DoShift(Token::SAR, instr);
1230}
1231
1232
1233LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1234 return DoShift(Token::SHL, instr);
1235}
1236
1237
1238LInstruction* LChunkBuilder::DoBitAnd(HBitAnd* instr) {
1239 return DoBit(Token::BIT_AND, instr);
1240}
1241
1242
1243LInstruction* LChunkBuilder::DoBitNot(HBitNot* instr) {
1244 ASSERT(instr->value()->representation().IsInteger32());
1245 ASSERT(instr->representation().IsInteger32());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001246 return DefineAsRegister(new LBitNotI(UseRegisterAtStart(instr->value())));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001247}
1248
1249
1250LInstruction* LChunkBuilder::DoBitOr(HBitOr* instr) {
1251 return DoBit(Token::BIT_OR, instr);
1252}
1253
1254
1255LInstruction* LChunkBuilder::DoBitXor(HBitXor* instr) {
1256 return DoBit(Token::BIT_XOR, instr);
1257}
1258
1259
1260LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
1261 if (instr->representation().IsDouble()) {
1262 return DoArithmeticD(Token::DIV, instr);
1263 } else if (instr->representation().IsInteger32()) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01001264 // TODO(1042) The fixed register allocation
Ben Murdoch8b112d22011-06-08 16:22:53 +01001265 // is needed because we call TypeRecordingBinaryOpStub from
Ben Murdochb8e0da22011-05-16 14:20:40 +01001266 // the generated code, which requires registers r0
1267 // and r1 to be used. We should remove that
1268 // when we provide a native implementation.
Steve Block1e0659c2011-05-24 12:43:12 +01001269 LOperand* dividend = UseFixed(instr->left(), r0);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001270 LOperand* divisor = UseFixed(instr->right(), r1);
1271 return AssignEnvironment(AssignPointerMap(
Steve Block1e0659c2011-05-24 12:43:12 +01001272 DefineFixed(new LDivI(dividend, divisor), r0)));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001273 } else {
1274 return DoArithmeticT(Token::DIV, instr);
1275 }
1276}
1277
1278
1279LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1280 if (instr->representation().IsInteger32()) {
1281 ASSERT(instr->left()->representation().IsInteger32());
1282 ASSERT(instr->right()->representation().IsInteger32());
Steve Block44f0eee2011-05-26 01:26:41 +01001283
1284 LModI* mod;
1285 if (instr->HasPowerOf2Divisor()) {
1286 ASSERT(!instr->CheckFlag(HValue::kCanBeDivByZero));
1287 LOperand* value = UseRegisterAtStart(instr->left());
1288 mod = new LModI(value, UseOrConstant(instr->right()));
1289 } else {
1290 LOperand* dividend = UseRegister(instr->left());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001291 LOperand* divisor = UseRegister(instr->right());
Steve Block44f0eee2011-05-26 01:26:41 +01001292 mod = new LModI(dividend,
1293 divisor,
1294 TempRegister(),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001295 FixedTemp(d10),
1296 FixedTemp(d11));
Steve Block44f0eee2011-05-26 01:26:41 +01001297 }
1298
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001299 if (instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1300 instr->CheckFlag(HValue::kCanBeDivByZero)) {
1301 return AssignEnvironment(DefineAsRegister(mod));
1302 } else {
1303 return DefineAsRegister(mod);
1304 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001305 } else if (instr->representation().IsTagged()) {
1306 return DoArithmeticT(Token::MOD, instr);
1307 } else {
1308 ASSERT(instr->representation().IsDouble());
1309 // We call a C function for double modulo. It can't trigger a GC.
1310 // We need to use fixed result register for the call.
1311 // TODO(fschneider): Allow any register as input registers.
1312 LOperand* left = UseFixedDouble(instr->left(), d1);
1313 LOperand* right = UseFixedDouble(instr->right(), d2);
1314 LArithmeticD* result = new LArithmeticD(Token::MOD, left, right);
1315 return MarkAsCall(DefineFixedDouble(result, d1), instr);
1316 }
1317}
1318
1319
1320LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1321 if (instr->representation().IsInteger32()) {
1322 ASSERT(instr->left()->representation().IsInteger32());
1323 ASSERT(instr->right()->representation().IsInteger32());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001324 LOperand* left;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001325 LOperand* right = UseOrConstant(instr->MostConstantOperand());
1326 LOperand* temp = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001327 if (instr->CheckFlag(HValue::kBailoutOnMinusZero) &&
1328 (instr->CheckFlag(HValue::kCanOverflow) ||
1329 !right->IsConstantOperand())) {
1330 left = UseRegister(instr->LeastConstantOperand());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001331 temp = TempRegister();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001332 } else {
1333 left = UseRegisterAtStart(instr->LeastConstantOperand());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001334 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001335 return AssignEnvironment(DefineAsRegister(new LMulI(left, right, temp)));
1336
Ben Murdochb0fe1622011-05-05 13:52:32 +01001337 } else if (instr->representation().IsDouble()) {
1338 return DoArithmeticD(Token::MUL, instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001339
Ben Murdochb0fe1622011-05-05 13:52:32 +01001340 } else {
1341 return DoArithmeticT(Token::MUL, instr);
1342 }
1343}
1344
1345
1346LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1347 if (instr->representation().IsInteger32()) {
1348 ASSERT(instr->left()->representation().IsInteger32());
1349 ASSERT(instr->right()->representation().IsInteger32());
Steve Block1e0659c2011-05-24 12:43:12 +01001350 LOperand* left = UseRegisterAtStart(instr->left());
1351 LOperand* right = UseOrConstantAtStart(instr->right());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001352 LSubI* sub = new LSubI(left, right);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001353 LInstruction* result = DefineAsRegister(sub);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001354 if (instr->CheckFlag(HValue::kCanOverflow)) {
1355 result = AssignEnvironment(result);
1356 }
1357 return result;
1358 } else if (instr->representation().IsDouble()) {
1359 return DoArithmeticD(Token::SUB, instr);
1360 } else {
1361 return DoArithmeticT(Token::SUB, instr);
1362 }
1363}
1364
1365
1366LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1367 if (instr->representation().IsInteger32()) {
1368 ASSERT(instr->left()->representation().IsInteger32());
1369 ASSERT(instr->right()->representation().IsInteger32());
1370 LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1371 LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1372 LAddI* add = new LAddI(left, right);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001373 LInstruction* result = DefineAsRegister(add);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001374 if (instr->CheckFlag(HValue::kCanOverflow)) {
1375 result = AssignEnvironment(result);
1376 }
1377 return result;
1378 } else if (instr->representation().IsDouble()) {
1379 return DoArithmeticD(Token::ADD, instr);
1380 } else {
1381 ASSERT(instr->representation().IsTagged());
1382 return DoArithmeticT(Token::ADD, instr);
1383 }
1384}
1385
1386
1387LInstruction* LChunkBuilder::DoPower(HPower* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001388 ASSERT(instr->representation().IsDouble());
1389 // We call a C function for double power. It can't trigger a GC.
1390 // We need to use fixed result register for the call.
1391 Representation exponent_type = instr->right()->representation();
1392 ASSERT(instr->left()->representation().IsDouble());
1393 LOperand* left = UseFixedDouble(instr->left(), d1);
1394 LOperand* right = exponent_type.IsDouble() ?
1395 UseFixedDouble(instr->right(), d2) :
1396 UseFixed(instr->right(), r0);
1397 LPower* result = new LPower(left, right);
1398 return MarkAsCall(DefineFixedDouble(result, d3),
1399 instr,
1400 CAN_DEOPTIMIZE_EAGERLY);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001401}
1402
1403
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001404LInstruction* LChunkBuilder::DoCompareGeneric(HCompareGeneric* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001405 Token::Value op = instr->token();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001406 ASSERT(instr->left()->representation().IsTagged());
1407 ASSERT(instr->right()->representation().IsTagged());
1408 bool reversed = (op == Token::GT || op == Token::LTE);
1409 LOperand* left = UseFixed(instr->left(), reversed ? r0 : r1);
1410 LOperand* right = UseFixed(instr->right(), reversed ? r1 : r0);
1411 LCmpT* result = new LCmpT(left, right);
1412 return MarkAsCall(DefineFixed(result, r0), instr);
1413}
1414
1415
1416LInstruction* LChunkBuilder::DoCompareIDAndBranch(
1417 HCompareIDAndBranch* instr) {
1418 Representation r = instr->GetInputRepresentation();
Ben Murdochb8e0da22011-05-16 14:20:40 +01001419 if (r.IsInteger32()) {
1420 ASSERT(instr->left()->representation().IsInteger32());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001421 ASSERT(instr->right()->representation().IsInteger32());
1422 LOperand* left = UseRegisterAtStart(instr->left());
Steve Block1e0659c2011-05-24 12:43:12 +01001423 LOperand* right = UseRegisterAtStart(instr->right());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001424 return new LCmpIDAndBranch(left, right);
1425 } else {
1426 ASSERT(r.IsDouble());
Ben Murdochb8e0da22011-05-16 14:20:40 +01001427 ASSERT(instr->left()->representation().IsDouble());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001428 ASSERT(instr->right()->representation().IsDouble());
1429 LOperand* left = UseRegisterAtStart(instr->left());
1430 LOperand* right = UseRegisterAtStart(instr->right());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001431 return new LCmpIDAndBranch(left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001432 }
1433}
1434
1435
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001436LInstruction* LChunkBuilder::DoCompareObjectEqAndBranch(
1437 HCompareObjectEqAndBranch* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001438 LOperand* left = UseRegisterAtStart(instr->left());
1439 LOperand* right = UseRegisterAtStart(instr->right());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001440 return new LCmpObjectEqAndBranch(left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001441}
1442
1443
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001444LInstruction* LChunkBuilder::DoCompareConstantEqAndBranch(
1445 HCompareConstantEqAndBranch* instr) {
1446 return new LCmpConstantEqAndBranch(UseRegisterAtStart(instr->value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00001447}
1448
1449
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001450LInstruction* LChunkBuilder::DoIsNullAndBranch(HIsNullAndBranch* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001451 ASSERT(instr->value()->representation().IsTagged());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001452 return new LIsNullAndBranch(UseRegisterAtStart(instr->value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001453}
1454
1455
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001456LInstruction* LChunkBuilder::DoIsObjectAndBranch(HIsObjectAndBranch* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001457 ASSERT(instr->value()->representation().IsTagged());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001458 LOperand* temp = TempRegister();
1459 return new LIsObjectAndBranch(UseRegisterAtStart(instr->value()), temp);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001460}
1461
1462
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001463LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001464 ASSERT(instr->value()->representation().IsTagged());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001465 return new LIsSmiAndBranch(Use(instr->value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001466}
1467
1468
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001469LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
1470 HIsUndetectableAndBranch* instr) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001471 ASSERT(instr->value()->representation().IsTagged());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001472 return new LIsUndetectableAndBranch(UseRegisterAtStart(instr->value()),
1473 TempRegister());
Ben Murdoch257744e2011-11-30 15:57:28 +00001474}
1475
1476
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001477LInstruction* LChunkBuilder::DoHasInstanceTypeAndBranch(
1478 HHasInstanceTypeAndBranch* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001479 ASSERT(instr->value()->representation().IsTagged());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001480 return new LHasInstanceTypeAndBranch(UseRegisterAtStart(instr->value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001481}
1482
1483
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001484LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
1485 HGetCachedArrayIndex* instr) {
1486 ASSERT(instr->value()->representation().IsTagged());
1487 LOperand* value = UseRegisterAtStart(instr->value());
1488
1489 return DefineAsRegister(new LGetCachedArrayIndex(value));
1490}
1491
1492
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001493LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
1494 HHasCachedArrayIndexAndBranch* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001495 ASSERT(instr->value()->representation().IsTagged());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001496 return new LHasCachedArrayIndexAndBranch(
1497 UseRegisterAtStart(instr->value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001498}
1499
1500
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001501LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
1502 HClassOfTestAndBranch* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001503 ASSERT(instr->value()->representation().IsTagged());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001504 return new LClassOfTestAndBranch(UseTempRegister(instr->value()),
1505 TempRegister());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001506}
1507
1508
Steve Block9fac8402011-05-12 15:51:54 +01001509LInstruction* LChunkBuilder::DoJSArrayLength(HJSArrayLength* instr) {
1510 LOperand* array = UseRegisterAtStart(instr->value());
1511 return DefineAsRegister(new LJSArrayLength(array));
1512}
Ben Murdochb0fe1622011-05-05 13:52:32 +01001513
Ben Murdochb0fe1622011-05-05 13:52:32 +01001514
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001515LInstruction* LChunkBuilder::DoFixedArrayBaseLength(
1516 HFixedArrayBaseLength* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001517 LOperand* array = UseRegisterAtStart(instr->value());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001518 return DefineAsRegister(new LFixedArrayBaseLength(array));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001519}
1520
1521
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001522LInstruction* LChunkBuilder::DoElementsKind(HElementsKind* instr) {
1523 LOperand* object = UseRegisterAtStart(instr->value());
1524 return DefineAsRegister(new LElementsKind(object));
1525}
1526
1527
Ben Murdochb0fe1622011-05-05 13:52:32 +01001528LInstruction* LChunkBuilder::DoValueOf(HValueOf* instr) {
1529 LOperand* object = UseRegister(instr->value());
Steve Block1e0659c2011-05-24 12:43:12 +01001530 LValueOf* result = new LValueOf(object, TempRegister());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001531 return AssignEnvironment(DefineAsRegister(result));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001532}
1533
1534
1535LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
1536 return AssignEnvironment(new LBoundsCheck(UseRegisterAtStart(instr->index()),
Ben Murdoch086aeea2011-05-13 15:57:08 +01001537 UseRegister(instr->length())));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001538}
1539
1540
Steve Block1e0659c2011-05-24 12:43:12 +01001541LInstruction* LChunkBuilder::DoAbnormalExit(HAbnormalExit* instr) {
1542 // The control instruction marking the end of a block that completed
1543 // abruptly (e.g., threw an exception). There is nothing specific to do.
1544 return NULL;
1545}
1546
1547
Ben Murdochb0fe1622011-05-05 13:52:32 +01001548LInstruction* LChunkBuilder::DoThrow(HThrow* instr) {
1549 LOperand* value = UseFixed(instr->value(), r0);
1550 return MarkAsCall(new LThrow(value), instr);
1551}
1552
1553
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001554LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
1555 return NULL;
1556}
1557
1558
Ben Murdoch257744e2011-11-30 15:57:28 +00001559LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
1560 // All HForceRepresentation instructions should be eliminated in the
1561 // representation change phase of Hydrogen.
1562 UNREACHABLE();
1563 return NULL;
1564}
1565
1566
Ben Murdochb0fe1622011-05-05 13:52:32 +01001567LInstruction* LChunkBuilder::DoChange(HChange* instr) {
1568 Representation from = instr->from();
1569 Representation to = instr->to();
1570 if (from.IsTagged()) {
1571 if (to.IsDouble()) {
1572 LOperand* value = UseRegister(instr->value());
Steve Block1e0659c2011-05-24 12:43:12 +01001573 LNumberUntagD* res = new LNumberUntagD(value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001574 return AssignEnvironment(DefineAsRegister(res));
1575 } else {
1576 ASSERT(to.IsInteger32());
1577 LOperand* value = UseRegister(instr->value());
1578 bool needs_check = !instr->value()->type().IsSmi();
1579 LInstruction* res = NULL;
Steve Block44f0eee2011-05-26 01:26:41 +01001580 if (!needs_check) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001581 res = DefineSameAsFirst(new LSmiUntag(value, needs_check));
Steve Block44f0eee2011-05-26 01:26:41 +01001582 } else {
1583 LOperand* temp1 = TempRegister();
1584 LOperand* temp2 = instr->CanTruncateToInt32() ? TempRegister()
1585 : NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001586 LOperand* temp3 = instr->CanTruncateToInt32() ? FixedTemp(d11)
Steve Block44f0eee2011-05-26 01:26:41 +01001587 : NULL;
1588 res = DefineSameAsFirst(new LTaggedToI(value, temp1, temp2, temp3));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001589 res = AssignEnvironment(res);
1590 }
1591 return res;
1592 }
1593 } else if (from.IsDouble()) {
1594 if (to.IsTagged()) {
1595 LOperand* value = UseRegister(instr->value());
1596 LOperand* temp1 = TempRegister();
1597 LOperand* temp2 = TempRegister();
1598
1599 // Make sure that the temp and result_temp registers are
1600 // different.
1601 LUnallocated* result_temp = TempRegister();
Steve Block1e0659c2011-05-24 12:43:12 +01001602 LNumberTagD* result = new LNumberTagD(value, temp1, temp2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001603 Define(result, result_temp);
1604 return AssignPointerMap(result);
1605 } else {
1606 ASSERT(to.IsInteger32());
1607 LOperand* value = UseRegister(instr->value());
Steve Block44f0eee2011-05-26 01:26:41 +01001608 LDoubleToI* res =
1609 new LDoubleToI(value,
1610 TempRegister(),
1611 instr->CanTruncateToInt32() ? TempRegister() : NULL);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001612 return AssignEnvironment(DefineAsRegister(res));
1613 }
1614 } else if (from.IsInteger32()) {
1615 if (to.IsTagged()) {
1616 HValue* val = instr->value();
1617 LOperand* value = UseRegister(val);
1618 if (val->HasRange() && val->range()->IsInSmiRange()) {
1619 return DefineSameAsFirst(new LSmiTag(value));
1620 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01001621 LNumberTagI* result = new LNumberTagI(value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001622 return AssignEnvironment(AssignPointerMap(DefineSameAsFirst(result)));
1623 }
1624 } else {
1625 ASSERT(to.IsDouble());
1626 LOperand* value = Use(instr->value());
1627 return DefineAsRegister(new LInteger32ToDouble(value));
1628 }
1629 }
1630 UNREACHABLE();
1631 return NULL;
1632}
1633
1634
1635LInstruction* LChunkBuilder::DoCheckNonSmi(HCheckNonSmi* instr) {
1636 LOperand* value = UseRegisterAtStart(instr->value());
Steve Block44f0eee2011-05-26 01:26:41 +01001637 return AssignEnvironment(new LCheckNonSmi(value));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001638}
1639
1640
1641LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
1642 LOperand* value = UseRegisterAtStart(instr->value());
Ben Murdoch086aeea2011-05-13 15:57:08 +01001643 LInstruction* result = new LCheckInstanceType(value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001644 return AssignEnvironment(result);
1645}
1646
1647
1648LInstruction* LChunkBuilder::DoCheckPrototypeMaps(HCheckPrototypeMaps* instr) {
Steve Block9fac8402011-05-12 15:51:54 +01001649 LOperand* temp1 = TempRegister();
1650 LOperand* temp2 = TempRegister();
Ben Murdochb8e0da22011-05-16 14:20:40 +01001651 LInstruction* result = new LCheckPrototypeMaps(temp1, temp2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001652 return AssignEnvironment(result);
1653}
1654
1655
1656LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
1657 LOperand* value = UseRegisterAtStart(instr->value());
Steve Block44f0eee2011-05-26 01:26:41 +01001658 return AssignEnvironment(new LCheckSmi(value));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001659}
1660
1661
1662LInstruction* LChunkBuilder::DoCheckFunction(HCheckFunction* instr) {
1663 LOperand* value = UseRegisterAtStart(instr->value());
1664 return AssignEnvironment(new LCheckFunction(value));
1665}
1666
1667
1668LInstruction* LChunkBuilder::DoCheckMap(HCheckMap* instr) {
1669 LOperand* value = UseRegisterAtStart(instr->value());
1670 LInstruction* result = new LCheckMap(value);
1671 return AssignEnvironment(result);
1672}
1673
1674
Ben Murdoch257744e2011-11-30 15:57:28 +00001675LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
1676 HValue* value = instr->value();
1677 Representation input_rep = value->representation();
1678 LOperand* reg = UseRegister(value);
1679 if (input_rep.IsDouble()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001680 return DefineAsRegister(new LClampDToUint8(reg, FixedTemp(d11)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001681 } else if (input_rep.IsInteger32()) {
1682 return DefineAsRegister(new LClampIToUint8(reg));
1683 } else {
1684 ASSERT(input_rep.IsTagged());
1685 // Register allocator doesn't (yet) support allocation of double
1686 // temps. Reserve d1 explicitly.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001687 LClampTToUint8* result = new LClampTToUint8(reg, FixedTemp(d11));
Ben Murdoch257744e2011-11-30 15:57:28 +00001688 return AssignEnvironment(DefineAsRegister(result));
1689 }
1690}
1691
1692
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001693LInstruction* LChunkBuilder::DoToInt32(HToInt32* instr) {
1694 HValue* value = instr->value();
1695 Representation input_rep = value->representation();
1696 LOperand* reg = UseRegister(value);
1697 if (input_rep.IsDouble()) {
1698 LOperand* temp1 = TempRegister();
1699 LOperand* temp2 = TempRegister();
1700 LDoubleToI* res = new LDoubleToI(reg, temp1, temp2);
1701 return AssignEnvironment(DefineAsRegister(res));
1702 } else if (input_rep.IsInteger32()) {
1703 // Canonicalization should already have removed the hydrogen instruction in
1704 // this case, since it is a noop.
1705 UNREACHABLE();
1706 return NULL;
1707 } else {
1708 ASSERT(input_rep.IsTagged());
1709 LOperand* temp1 = TempRegister();
1710 LOperand* temp2 = TempRegister();
1711 LOperand* temp3 = FixedTemp(d11);
1712 LTaggedToI* res = new LTaggedToI(reg, temp1, temp2, temp3);
1713 return AssignEnvironment(DefineSameAsFirst(res));
1714 }
1715}
1716
1717
Ben Murdochb0fe1622011-05-05 13:52:32 +01001718LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
1719 return new LReturn(UseFixed(instr->value(), r0));
1720}
1721
1722
1723LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
1724 Representation r = instr->representation();
1725 if (r.IsInteger32()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001726 return DefineAsRegister(new LConstantI);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001727 } else if (r.IsDouble()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001728 return DefineAsRegister(new LConstantD);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001729 } else if (r.IsTagged()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001730 return DefineAsRegister(new LConstantT);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001731 } else {
Ben Murdochb8e0da22011-05-16 14:20:40 +01001732 UNREACHABLE();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001733 return NULL;
1734 }
1735}
1736
1737
Ben Murdoch8b112d22011-06-08 16:22:53 +01001738LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
1739 LLoadGlobalCell* result = new LLoadGlobalCell;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001740 return instr->check_hole_value()
1741 ? AssignEnvironment(DefineAsRegister(result))
1742 : DefineAsRegister(result);
1743}
1744
1745
Ben Murdoch8b112d22011-06-08 16:22:53 +01001746LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
1747 LOperand* global_object = UseFixed(instr->global_object(), r0);
1748 LLoadGlobalGeneric* result = new LLoadGlobalGeneric(global_object);
1749 return MarkAsCall(DefineFixed(result, r0), instr);
1750}
1751
1752
1753LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001754 if (instr->check_hole_value()) {
1755 LOperand* temp = TempRegister();
1756 LOperand* value = UseRegister(instr->value());
Ben Murdoch8b112d22011-06-08 16:22:53 +01001757 return AssignEnvironment(new LStoreGlobalCell(value, temp));
Steve Block1e0659c2011-05-24 12:43:12 +01001758 } else {
1759 LOperand* value = UseRegisterAtStart(instr->value());
Ben Murdoch8b112d22011-06-08 16:22:53 +01001760 return new LStoreGlobalCell(value, NULL);
Steve Block1e0659c2011-05-24 12:43:12 +01001761 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001762}
1763
1764
Ben Murdoch8b112d22011-06-08 16:22:53 +01001765LInstruction* LChunkBuilder::DoStoreGlobalGeneric(HStoreGlobalGeneric* instr) {
1766 LOperand* global_object = UseFixed(instr->global_object(), r1);
1767 LOperand* value = UseFixed(instr->value(), r0);
1768 LStoreGlobalGeneric* result =
1769 new LStoreGlobalGeneric(global_object, value);
1770 return MarkAsCall(result, instr);
1771}
1772
1773
Ben Murdochb8e0da22011-05-16 14:20:40 +01001774LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001775 LOperand* context = UseRegisterAtStart(instr->value());
1776 return DefineAsRegister(new LLoadContextSlot(context));
1777}
1778
1779
1780LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001781 LOperand* context;
Steve Block1e0659c2011-05-24 12:43:12 +01001782 LOperand* value;
1783 if (instr->NeedsWriteBarrier()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001784 context = UseTempRegister(instr->context());
Steve Block1e0659c2011-05-24 12:43:12 +01001785 value = UseTempRegister(instr->value());
1786 } else {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001787 context = UseRegister(instr->context());
Steve Block1e0659c2011-05-24 12:43:12 +01001788 value = UseRegister(instr->value());
1789 }
1790 return new LStoreContextSlot(context, value);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001791}
1792
1793
Ben Murdochb0fe1622011-05-05 13:52:32 +01001794LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
1795 return DefineAsRegister(
1796 new LLoadNamedField(UseRegisterAtStart(instr->object())));
1797}
1798
1799
Steve Block44f0eee2011-05-26 01:26:41 +01001800LInstruction* LChunkBuilder::DoLoadNamedFieldPolymorphic(
1801 HLoadNamedFieldPolymorphic* instr) {
1802 ASSERT(instr->representation().IsTagged());
1803 if (instr->need_generic()) {
1804 LOperand* obj = UseFixed(instr->object(), r0);
1805 LLoadNamedFieldPolymorphic* result = new LLoadNamedFieldPolymorphic(obj);
1806 return MarkAsCall(DefineFixed(result, r0), instr);
1807 } else {
1808 LOperand* obj = UseRegisterAtStart(instr->object());
1809 LLoadNamedFieldPolymorphic* result = new LLoadNamedFieldPolymorphic(obj);
1810 return AssignEnvironment(DefineAsRegister(result));
1811 }
1812}
1813
1814
Ben Murdochb0fe1622011-05-05 13:52:32 +01001815LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
1816 LOperand* object = UseFixed(instr->object(), r0);
1817 LInstruction* result = DefineFixed(new LLoadNamedGeneric(object), r0);
1818 return MarkAsCall(result, instr);
1819}
1820
1821
Steve Block9fac8402011-05-12 15:51:54 +01001822LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
1823 HLoadFunctionPrototype* instr) {
1824 return AssignEnvironment(DefineAsRegister(
1825 new LLoadFunctionPrototype(UseRegister(instr->function()))));
1826}
1827
1828
Ben Murdochb0fe1622011-05-05 13:52:32 +01001829LInstruction* LChunkBuilder::DoLoadElements(HLoadElements* instr) {
1830 LOperand* input = UseRegisterAtStart(instr->value());
Steve Block1e0659c2011-05-24 12:43:12 +01001831 return DefineAsRegister(new LLoadElements(input));
1832}
1833
1834
Steve Block44f0eee2011-05-26 01:26:41 +01001835LInstruction* LChunkBuilder::DoLoadExternalArrayPointer(
1836 HLoadExternalArrayPointer* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001837 LOperand* input = UseRegisterAtStart(instr->value());
Steve Block44f0eee2011-05-26 01:26:41 +01001838 return DefineAsRegister(new LLoadExternalArrayPointer(input));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001839}
1840
1841
1842LInstruction* LChunkBuilder::DoLoadKeyedFastElement(
1843 HLoadKeyedFastElement* instr) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01001844 ASSERT(instr->representation().IsTagged());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001845 ASSERT(instr->key()->representation().IsInteger32());
Ben Murdochb8e0da22011-05-16 14:20:40 +01001846 LOperand* obj = UseRegisterAtStart(instr->object());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001847 LOperand* key = UseRegisterAtStart(instr->key());
Steve Block1e0659c2011-05-24 12:43:12 +01001848 LLoadKeyedFastElement* result = new LLoadKeyedFastElement(obj, key);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001849 return AssignEnvironment(DefineAsRegister(result));
1850}
1851
1852
1853LInstruction* LChunkBuilder::DoLoadKeyedFastDoubleElement(
1854 HLoadKeyedFastDoubleElement* instr) {
1855 ASSERT(instr->representation().IsDouble());
1856 ASSERT(instr->key()->representation().IsInteger32());
1857 LOperand* elements = UseTempRegister(instr->elements());
1858 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1859 LLoadKeyedFastDoubleElement* result =
1860 new LLoadKeyedFastDoubleElement(elements, key);
1861 return AssignEnvironment(DefineAsRegister(result));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001862}
1863
1864
Steve Block44f0eee2011-05-26 01:26:41 +01001865LInstruction* LChunkBuilder::DoLoadKeyedSpecializedArrayElement(
1866 HLoadKeyedSpecializedArrayElement* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001867 JSObject::ElementsKind elements_kind = instr->elements_kind();
Ben Murdoch8b112d22011-06-08 16:22:53 +01001868 Representation representation(instr->representation());
Ben Murdoch257744e2011-11-30 15:57:28 +00001869 ASSERT(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001870 (representation.IsInteger32() &&
1871 (elements_kind != JSObject::EXTERNAL_FLOAT_ELEMENTS) &&
1872 (elements_kind != JSObject::EXTERNAL_DOUBLE_ELEMENTS)) ||
1873 (representation.IsDouble() &&
1874 ((elements_kind == JSObject::EXTERNAL_FLOAT_ELEMENTS) ||
1875 (elements_kind == JSObject::EXTERNAL_DOUBLE_ELEMENTS))));
Steve Block1e0659c2011-05-24 12:43:12 +01001876 ASSERT(instr->key()->representation().IsInteger32());
Ben Murdoch8b112d22011-06-08 16:22:53 +01001877 LOperand* external_pointer = UseRegister(instr->external_pointer());
Ben Murdoch257744e2011-11-30 15:57:28 +00001878 LOperand* key = UseRegisterOrConstant(instr->key());
Steve Block44f0eee2011-05-26 01:26:41 +01001879 LLoadKeyedSpecializedArrayElement* result =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001880 new LLoadKeyedSpecializedArrayElement(external_pointer, key);
1881 LInstruction* load_instr = DefineAsRegister(result);
1882 // An unsigned int array load might overflow and cause a deopt, make sure it
1883 // has an environment.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001884 return (elements_kind == JSObject::EXTERNAL_UNSIGNED_INT_ELEMENTS) ?
Ben Murdoch8b112d22011-06-08 16:22:53 +01001885 AssignEnvironment(load_instr) : load_instr;
Steve Block1e0659c2011-05-24 12:43:12 +01001886}
1887
1888
Ben Murdochb0fe1622011-05-05 13:52:32 +01001889LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
1890 LOperand* object = UseFixed(instr->object(), r1);
1891 LOperand* key = UseFixed(instr->key(), r0);
1892
1893 LInstruction* result =
1894 DefineFixed(new LLoadKeyedGeneric(object, key), r0);
1895 return MarkAsCall(result, instr);
1896}
1897
1898
1899LInstruction* LChunkBuilder::DoStoreKeyedFastElement(
1900 HStoreKeyedFastElement* instr) {
1901 bool needs_write_barrier = instr->NeedsWriteBarrier();
1902 ASSERT(instr->value()->representation().IsTagged());
1903 ASSERT(instr->object()->representation().IsTagged());
1904 ASSERT(instr->key()->representation().IsInteger32());
1905
1906 LOperand* obj = UseTempRegister(instr->object());
1907 LOperand* val = needs_write_barrier
1908 ? UseTempRegister(instr->value())
1909 : UseRegisterAtStart(instr->value());
1910 LOperand* key = needs_write_barrier
1911 ? UseTempRegister(instr->key())
1912 : UseRegisterOrConstantAtStart(instr->key());
1913
1914 return AssignEnvironment(new LStoreKeyedFastElement(obj, key, val));
1915}
1916
1917
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001918LInstruction* LChunkBuilder::DoStoreKeyedFastDoubleElement(
1919 HStoreKeyedFastDoubleElement* instr) {
1920 ASSERT(instr->value()->representation().IsDouble());
1921 ASSERT(instr->elements()->representation().IsTagged());
1922 ASSERT(instr->key()->representation().IsInteger32());
1923
1924 LOperand* elements = UseRegisterAtStart(instr->elements());
1925 LOperand* val = UseTempRegister(instr->value());
1926 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1927
1928 return new LStoreKeyedFastDoubleElement(elements, key, val);
1929}
1930
1931
Steve Block44f0eee2011-05-26 01:26:41 +01001932LInstruction* LChunkBuilder::DoStoreKeyedSpecializedArrayElement(
1933 HStoreKeyedSpecializedArrayElement* instr) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001934 Representation representation(instr->value()->representation());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001935 JSObject::ElementsKind elements_kind = instr->elements_kind();
Ben Murdoch257744e2011-11-30 15:57:28 +00001936 ASSERT(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001937 (representation.IsInteger32() &&
1938 (elements_kind != JSObject::EXTERNAL_FLOAT_ELEMENTS) &&
1939 (elements_kind != JSObject::EXTERNAL_DOUBLE_ELEMENTS)) ||
1940 (representation.IsDouble() &&
1941 ((elements_kind == JSObject::EXTERNAL_FLOAT_ELEMENTS) ||
1942 (elements_kind == JSObject::EXTERNAL_DOUBLE_ELEMENTS))));
Steve Block44f0eee2011-05-26 01:26:41 +01001943 ASSERT(instr->external_pointer()->representation().IsExternal());
1944 ASSERT(instr->key()->representation().IsInteger32());
1945
1946 LOperand* external_pointer = UseRegister(instr->external_pointer());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001947 bool val_is_temp_register =
1948 elements_kind == JSObject::EXTERNAL_PIXEL_ELEMENTS ||
1949 elements_kind == JSObject::EXTERNAL_FLOAT_ELEMENTS;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001950 LOperand* val = val_is_temp_register
1951 ? UseTempRegister(instr->value())
1952 : UseRegister(instr->value());
Ben Murdoch257744e2011-11-30 15:57:28 +00001953 LOperand* key = UseRegisterOrConstant(instr->key());
Steve Block44f0eee2011-05-26 01:26:41 +01001954
1955 return new LStoreKeyedSpecializedArrayElement(external_pointer,
1956 key,
Ben Murdoch8b112d22011-06-08 16:22:53 +01001957 val);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001958}
1959
1960
Ben Murdochb0fe1622011-05-05 13:52:32 +01001961LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
1962 LOperand* obj = UseFixed(instr->object(), r2);
1963 LOperand* key = UseFixed(instr->key(), r1);
1964 LOperand* val = UseFixed(instr->value(), r0);
1965
1966 ASSERT(instr->object()->representation().IsTagged());
1967 ASSERT(instr->key()->representation().IsTagged());
1968 ASSERT(instr->value()->representation().IsTagged());
1969
1970 return MarkAsCall(new LStoreKeyedGeneric(obj, key, val), instr);
1971}
1972
1973
1974LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01001975 bool needs_write_barrier = instr->NeedsWriteBarrier();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001976
1977 LOperand* obj = needs_write_barrier
1978 ? UseTempRegister(instr->object())
1979 : UseRegisterAtStart(instr->object());
1980
1981 LOperand* val = needs_write_barrier
1982 ? UseTempRegister(instr->value())
1983 : UseRegister(instr->value());
1984
Ben Murdochb8e0da22011-05-16 14:20:40 +01001985 return new LStoreNamedField(obj, val);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001986}
1987
1988
1989LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
1990 LOperand* obj = UseFixed(instr->object(), r1);
1991 LOperand* val = UseFixed(instr->value(), r0);
1992
Ben Murdochb8e0da22011-05-16 14:20:40 +01001993 LInstruction* result = new LStoreNamedGeneric(obj, val);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001994 return MarkAsCall(result, instr);
1995}
1996
1997
Ben Murdoch257744e2011-11-30 15:57:28 +00001998LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
1999 LOperand* left = UseRegisterAtStart(instr->left());
2000 LOperand* right = UseRegisterAtStart(instr->right());
2001 return MarkAsCall(DefineFixed(new LStringAdd(left, right), r0), instr);
2002}
2003
2004
Steve Block1e0659c2011-05-24 12:43:12 +01002005LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002006 LOperand* string = UseTempRegister(instr->string());
2007 LOperand* index = UseTempRegister(instr->index());
Steve Block1e0659c2011-05-24 12:43:12 +01002008 LStringCharCodeAt* result = new LStringCharCodeAt(string, index);
2009 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
2010}
2011
2012
Steve Block44f0eee2011-05-26 01:26:41 +01002013LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2014 LOperand* char_code = UseRegister(instr->value());
2015 LStringCharFromCode* result = new LStringCharFromCode(char_code);
2016 return AssignPointerMap(DefineAsRegister(result));
2017}
2018
2019
Steve Block1e0659c2011-05-24 12:43:12 +01002020LInstruction* LChunkBuilder::DoStringLength(HStringLength* instr) {
2021 LOperand* string = UseRegisterAtStart(instr->value());
2022 return DefineAsRegister(new LStringLength(string));
2023}
2024
2025
Ben Murdochb0fe1622011-05-05 13:52:32 +01002026LInstruction* LChunkBuilder::DoArrayLiteral(HArrayLiteral* instr) {
2027 return MarkAsCall(DefineFixed(new LArrayLiteral, r0), instr);
2028}
2029
2030
2031LInstruction* LChunkBuilder::DoObjectLiteral(HObjectLiteral* instr) {
2032 return MarkAsCall(DefineFixed(new LObjectLiteral, r0), instr);
2033}
2034
2035
2036LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2037 return MarkAsCall(DefineFixed(new LRegExpLiteral, r0), instr);
2038}
2039
2040
2041LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2042 return MarkAsCall(DefineFixed(new LFunctionLiteral, r0), instr);
2043}
2044
2045
2046LInstruction* LChunkBuilder::DoDeleteProperty(HDeleteProperty* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002047 LOperand* object = UseFixed(instr->object(), r0);
2048 LOperand* key = UseFixed(instr->key(), r1);
2049 LDeleteProperty* result = new LDeleteProperty(object, key);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002050 return MarkAsCall(DefineFixed(result, r0), instr);
2051}
2052
2053
2054LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2055 allocator_->MarkAsOsrEntry();
2056 current_block_->last_environment()->set_ast_id(instr->ast_id());
2057 return AssignEnvironment(new LOsrEntry);
2058}
2059
2060
2061LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2062 int spill_index = chunk()->GetParameterStackSlot(instr->index());
2063 return DefineAsSpilled(new LParameter, spill_index);
2064}
2065
2066
2067LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2068 int spill_index = chunk()->GetNextSpillIndex(false); // Not double-width.
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01002069 if (spill_index > LUnallocated::kMaxFixedIndex) {
2070 Abort("Too many spill slots needed for OSR");
2071 spill_index = 0;
2072 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002073 return DefineAsSpilled(new LUnknownOSRValue, spill_index);
2074}
2075
2076
2077LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2078 argument_count_ -= instr->argument_count();
2079 return MarkAsCall(DefineFixed(new LCallStub, r0), instr);
2080}
2081
2082
2083LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002084 // There are no real uses of the arguments object.
2085 // arguments.length and element access are supported directly on
2086 // stack arguments, and any real arguments object use causes a bailout.
2087 // So this value is never used.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002088 return NULL;
2089}
2090
2091
2092LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2093 LOperand* arguments = UseRegister(instr->arguments());
2094 LOperand* length = UseTempRegister(instr->length());
Ben Murdoch086aeea2011-05-13 15:57:08 +01002095 LOperand* index = UseRegister(instr->index());
Steve Block1e0659c2011-05-24 12:43:12 +01002096 LAccessArgumentsAt* result = new LAccessArgumentsAt(arguments, length, index);
2097 return AssignEnvironment(DefineAsRegister(result));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002098}
2099
2100
Steve Block44f0eee2011-05-26 01:26:41 +01002101LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2102 LOperand* object = UseFixed(instr->value(), r0);
2103 LToFastProperties* result = new LToFastProperties(object);
2104 return MarkAsCall(DefineFixed(result, r0), instr);
2105}
2106
2107
Ben Murdochb0fe1622011-05-05 13:52:32 +01002108LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002109 LTypeof* result = new LTypeof(UseFixed(instr->value(), r0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002110 return MarkAsCall(DefineFixed(result, r0), instr);
2111}
2112
2113
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002114LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2115 return new LTypeofIsAndBranch(UseTempRegister(instr->value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002116}
2117
Steve Block1e0659c2011-05-24 12:43:12 +01002118
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002119LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
2120 HIsConstructCallAndBranch* instr) {
2121 return new LIsConstructCallAndBranch(TempRegister());
Steve Block1e0659c2011-05-24 12:43:12 +01002122}
2123
2124
Ben Murdochb0fe1622011-05-05 13:52:32 +01002125LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2126 HEnvironment* env = current_block_->last_environment();
2127 ASSERT(env != NULL);
2128
2129 env->set_ast_id(instr->ast_id());
2130
2131 env->Drop(instr->pop_count());
2132 for (int i = 0; i < instr->values()->length(); ++i) {
2133 HValue* value = instr->values()->at(i);
2134 if (instr->HasAssignedIndexAt(i)) {
2135 env->Bind(instr->GetAssignedIndexAt(i), value);
2136 } else {
2137 env->Push(value);
2138 }
2139 }
2140
Ben Murdochb0fe1622011-05-05 13:52:32 +01002141 // If there is an instruction pending deoptimization environment create a
2142 // lazy bailout instruction to capture the environment.
2143 if (pending_deoptimization_ast_id_ == instr->ast_id()) {
2144 LInstruction* result = new LLazyBailout;
2145 result = AssignEnvironment(result);
Steve Block1e0659c2011-05-24 12:43:12 +01002146 instruction_pending_deoptimization_environment_->
Ben Murdochb0fe1622011-05-05 13:52:32 +01002147 set_deoptimization_environment(result->environment());
2148 ClearInstructionPendingDeoptimizationEnvironment();
2149 return result;
2150 }
2151
2152 return NULL;
2153}
2154
2155
2156LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002157 if (instr->is_function_entry()) {
2158 return MarkAsCall(new LStackCheck, instr);
2159 } else {
2160 ASSERT(instr->is_backwards_branch());
2161 return AssignEnvironment(AssignPointerMap(new LStackCheck));
2162 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002163}
2164
2165
2166LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2167 HEnvironment* outer = current_block_->last_environment();
2168 HConstant* undefined = graph()->GetConstantUndefined();
2169 HEnvironment* inner = outer->CopyForInlining(instr->closure(),
2170 instr->function(),
Ben Murdoch257744e2011-11-30 15:57:28 +00002171 undefined,
2172 instr->call_kind());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002173 current_block_->UpdateEnvironment(inner);
2174 chunk_->AddInlinedClosure(instr->closure());
2175 return NULL;
2176}
2177
2178
2179LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2180 HEnvironment* outer = current_block_->last_environment()->outer();
2181 current_block_->UpdateEnvironment(outer);
2182 return NULL;
2183}
2184
2185
Ben Murdoch257744e2011-11-30 15:57:28 +00002186LInstruction* LChunkBuilder::DoIn(HIn* instr) {
2187 LOperand* key = UseRegisterAtStart(instr->key());
2188 LOperand* object = UseRegisterAtStart(instr->object());
2189 LIn* result = new LIn(key, object);
2190 return MarkAsCall(DefineFixed(result, r0), instr);
2191}
2192
2193
Ben Murdochb0fe1622011-05-05 13:52:32 +01002194} } // namespace v8::internal