blob: 1f2b2f63cdbd64ebffbcaaa8b103c1e5707bf1d6 [file] [log] [blame]
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001// Copyright 2011 the V8 project authors. All rights reserved.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000028#include "v8.h"
29
ricow@chromium.org83aa5492011-02-07 12:42:56 +000030#include "lithium-allocator-inl.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000031#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
ricow@chromium.org83aa5492011-02-07 12:42:56 +000062#ifdef DEBUG
63void LInstruction::VerifyCall() {
danno@chromium.org160a7b02011-04-18 15:51:38 +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.
ricow@chromium.org83aa5492011-02-07 12:42:56 +000068 ASSERT(Output() == NULL ||
69 LUnallocated::cast(Output())->HasFixedPolicy() ||
70 !LUnallocated::cast(Output())->HasRegisterPolicy());
jkummerow@chromium.orge297f592011-06-08 10:05:15 +000071 for (UseIterator it(this); !it.Done(); it.Advance()) {
72 LUnallocated* operand = LUnallocated::cast(it.Current());
danno@chromium.org160a7b02011-04-18 15:51:38 +000073 ASSERT(operand->HasFixedPolicy() ||
74 operand->IsUsedAtStart());
ricow@chromium.org83aa5492011-02-07 12:42:56 +000075 }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +000076 for (TempIterator it(this); !it.Done(); it.Advance()) {
77 LUnallocated* operand = LUnallocated::cast(it.Current());
danno@chromium.org160a7b02011-04-18 15:51:38 +000078 ASSERT(operand->HasFixedPolicy() ||!operand->HasRegisterPolicy());
ricow@chromium.org83aa5492011-02-07 12:42:56 +000079 }
80}
81#endif
82
83
kasperl@chromium.orga5551262010-12-07 12:49:48 +000084void 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
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +000092void LInstruction::PrintTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +000093 stream->Add("%s ", this->Mnemonic());
ricow@chromium.org83aa5492011-02-07 12:42:56 +000094
95 PrintOutputOperandTo(stream);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +000096
kasperl@chromium.orga5551262010-12-07 12:49:48 +000097 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
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000111template<int R, int I, int T>
112void LTemplateInstruction<R, I, T>::PrintDataTo(StringStream* stream) {
113 stream->Add("= ");
114 inputs_.PrintOperandsTo(stream);
115}
116
117
118template<int R, int I, int T>
119void LTemplateInstruction<R, I, T>::PrintOutputOperandTo(StringStream* stream) {
120 results_.PrintOperandsTo(stream);
121}
122
123
124template<typename T, int N>
125void OperandContainer<T, N>::PrintOperandsTo(StringStream* stream) {
126 for (int i = 0; i < N; i++) {
127 if (i > 0) stream->Add(" ");
128 elems_[i]->PrintTo(stream);
129 }
130}
131
132
133void LLabel::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000134 LGap::PrintDataTo(stream);
135 LLabel* rep = replacement();
136 if (rep != NULL) {
137 stream->Add(" Dead block replaced with B%d", rep->block_id());
138 }
139}
140
141
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000142bool LGap::IsRedundant() const {
143 for (int i = 0; i < 4; i++) {
144 if (parallel_moves_[i] != NULL && !parallel_moves_[i]->IsRedundant()) {
145 return false;
146 }
147 }
148
149 return true;
150}
151
152
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000153void LGap::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000154 for (int i = 0; i < 4; i++) {
155 stream->Add("(");
156 if (parallel_moves_[i] != NULL) {
157 parallel_moves_[i]->PrintDataTo(stream);
158 }
159 stream->Add(") ");
160 }
161}
162
163
164const char* LArithmeticD::Mnemonic() const {
165 switch (op()) {
166 case Token::ADD: return "add-d";
167 case Token::SUB: return "sub-d";
168 case Token::MUL: return "mul-d";
169 case Token::DIV: return "div-d";
170 case Token::MOD: return "mod-d";
171 default:
172 UNREACHABLE();
173 return NULL;
174 }
175}
176
177
178const char* LArithmeticT::Mnemonic() const {
179 switch (op()) {
180 case Token::ADD: return "add-t";
181 case Token::SUB: return "sub-t";
182 case Token::MUL: return "mul-t";
183 case Token::MOD: return "mod-t";
184 case Token::DIV: return "div-t";
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000185 case Token::BIT_AND: return "bit-and-t";
186 case Token::BIT_OR: return "bit-or-t";
187 case Token::BIT_XOR: return "bit-xor-t";
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000188 case Token::SHL: return "shl-t";
189 case Token::SAR: return "sar-t";
190 case Token::SHR: return "shr-t";
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000191 default:
192 UNREACHABLE();
193 return NULL;
194 }
195}
196
197
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000198void LGoto::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000199 stream->Add("B%d", block_id());
200}
201
202
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000203void LBranch::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000204 stream->Add("B%d | B%d on ", true_block_id(), false_block_id());
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000205 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000206}
207
208
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000209void LCmpIDAndBranch::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000210 stream->Add("if ");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000211 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000212 stream->Add(" %s ", Token::String(op()));
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000213 InputAt(1)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000214 stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
215}
216
217
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000218void LIsNullAndBranch::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000219 stream->Add("if ");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000220 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000221 stream->Add(is_strict() ? " === null" : " == null");
222 stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
223}
224
225
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000226void LIsObjectAndBranch::PrintDataTo(StringStream* stream) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000227 stream->Add("if is_object(");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000228 InputAt(0)->PrintTo(stream);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000229 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
230}
231
232
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000233void LIsSmiAndBranch::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000234 stream->Add("if is_smi(");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000235 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000236 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
237}
238
239
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000240void LIsUndetectableAndBranch::PrintDataTo(StringStream* stream) {
241 stream->Add("if is_undetectable(");
242 InputAt(0)->PrintTo(stream);
243 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
244}
245
246
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000247void LHasInstanceTypeAndBranch::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000248 stream->Add("if has_instance_type(");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000249 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000250 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
251}
252
253
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000254void LHasCachedArrayIndexAndBranch::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000255 stream->Add("if has_cached_array_index(");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000256 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000257 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
258}
259
260
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000261void LClassOfTestAndBranch::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000262 stream->Add("if class_of_test(");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000263 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000264 stream->Add(", \"%o\") then B%d else B%d",
265 *hydrogen()->class_name(),
266 true_block_id(),
267 false_block_id());
268}
269
270
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000271void LTypeofIs::PrintDataTo(StringStream* stream) {
272 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000273 stream->Add(" == \"%s\"", *hydrogen()->type_literal()->ToCString());
274}
275
276
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000277void LTypeofIsAndBranch::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000278 stream->Add("if typeof ");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000279 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000280 stream->Add(" == \"%s\" then B%d else B%d",
281 *hydrogen()->type_literal()->ToCString(),
282 true_block_id(), false_block_id());
283}
284
285
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000286void LCallConstantFunction::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000287 stream->Add("#%d / ", arity());
288}
289
290
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000291void LUnaryMathOperation::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000292 stream->Add("/%s ", hydrogen()->OpName());
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000293 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000294}
295
296
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000297void LLoadContextSlot::PrintDataTo(StringStream* stream) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000298 InputAt(0)->PrintTo(stream);
299 stream->Add("[%d]", slot_index());
300}
301
302
303void LStoreContextSlot::PrintDataTo(StringStream* stream) {
304 InputAt(0)->PrintTo(stream);
305 stream->Add("[%d] <- ", slot_index());
306 InputAt(1)->PrintTo(stream);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000307}
308
309
danno@chromium.org160a7b02011-04-18 15:51:38 +0000310void LInvokeFunction::PrintDataTo(StringStream* stream) {
311 stream->Add("= ");
312 InputAt(0)->PrintTo(stream);
313 stream->Add(" #%d / ", arity());
314}
315
316
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000317void LCallKeyed::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000318 stream->Add("[r2] #%d / ", arity());
319}
320
321
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000322void LCallNamed::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000323 SmartPointer<char> name_string = name()->ToCString();
324 stream->Add("%s #%d / ", *name_string, arity());
325}
326
327
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000328void LCallGlobal::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000329 SmartPointer<char> name_string = name()->ToCString();
330 stream->Add("%s #%d / ", *name_string, arity());
331}
332
333
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000334void LCallKnownGlobal::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000335 stream->Add("#%d / ", arity());
336}
337
338
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000339void LCallNew::PrintDataTo(StringStream* stream) {
340 stream->Add("= ");
341 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000342 stream->Add(" #%d / ", arity());
343}
344
345
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000346void LClassOfTest::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000347 stream->Add("= class_of_test(");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000348 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000349 stream->Add(", \"%o\")", *hydrogen()->class_name());
350}
351
352
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000353void LAccessArgumentsAt::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000354 arguments()->PrintTo(stream);
355
356 stream->Add(" length ");
357 length()->PrintTo(stream);
358
359 stream->Add(" index ");
360 index()->PrintTo(stream);
361}
362
363
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000364void LStoreNamedField::PrintDataTo(StringStream* stream) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000365 object()->PrintTo(stream);
366 stream->Add(".");
367 stream->Add(*String::cast(*name())->ToCString());
368 stream->Add(" <- ");
369 value()->PrintTo(stream);
370}
371
372
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000373void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
374 object()->PrintTo(stream);
375 stream->Add(".");
376 stream->Add(*String::cast(*name())->ToCString());
377 stream->Add(" <- ");
378 value()->PrintTo(stream);
379}
380
381
382void LStoreKeyedFastElement::PrintDataTo(StringStream* stream) {
383 object()->PrintTo(stream);
384 stream->Add("[");
385 key()->PrintTo(stream);
386 stream->Add("] <- ");
387 value()->PrintTo(stream);
388}
389
390
391void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000392 object()->PrintTo(stream);
393 stream->Add("[");
394 key()->PrintTo(stream);
395 stream->Add("] <- ");
396 value()->PrintTo(stream);
397}
398
399
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000400LChunk::LChunk(CompilationInfo* info, HGraph* graph)
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000401 : spill_slot_count_(0),
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000402 info_(info),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000403 graph_(graph),
404 instructions_(32),
405 pointer_maps_(8),
406 inlined_closures_(1) {
407}
408
409
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000410int LChunk::GetNextSpillIndex(bool is_double) {
411 // Skip a slot if for a double-width slot.
412 if (is_double) spill_slot_count_++;
413 return spill_slot_count_++;
414}
415
416
417LOperand* LChunk::GetNextSpillSlot(bool is_double) {
418 int index = GetNextSpillIndex(is_double);
419 if (is_double) {
420 return LDoubleStackSlot::Create(index);
421 } else {
422 return LStackSlot::Create(index);
423 }
424}
425
426
427void LChunk::MarkEmptyBlocks() {
428 HPhase phase("Mark empty blocks", this);
429 for (int i = 0; i < graph()->blocks()->length(); ++i) {
430 HBasicBlock* block = graph()->blocks()->at(i);
431 int first = block->first_instruction_index();
432 int last = block->last_instruction_index();
433 LInstruction* first_instr = instructions()->at(first);
434 LInstruction* last_instr = instructions()->at(last);
435
436 LLabel* label = LLabel::cast(first_instr);
437 if (last_instr->IsGoto()) {
438 LGoto* goto_instr = LGoto::cast(last_instr);
439 if (!goto_instr->include_stack_check() &&
440 label->IsRedundant() &&
441 !label->is_loop_header()) {
442 bool can_eliminate = true;
443 for (int i = first + 1; i < last && can_eliminate; ++i) {
444 LInstruction* cur = instructions()->at(i);
445 if (cur->IsGap()) {
446 LGap* gap = LGap::cast(cur);
447 if (!gap->IsRedundant()) {
448 can_eliminate = false;
449 }
450 } else {
451 can_eliminate = false;
452 }
453 }
454
455 if (can_eliminate) {
456 label->set_replacement(GetLabel(goto_instr->block_id()));
457 }
458 }
459 }
460 }
461}
462
463
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000464void LChunk::AddInstruction(LInstruction* instr, HBasicBlock* block) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000465 LInstructionGap* gap = new LInstructionGap(block);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000466 int index = -1;
467 if (instr->IsControl()) {
468 instructions_.Add(gap);
469 index = instructions_.length();
470 instructions_.Add(instr);
471 } else {
472 index = instructions_.length();
473 instructions_.Add(instr);
474 instructions_.Add(gap);
475 }
476 if (instr->HasPointerMap()) {
477 pointer_maps_.Add(instr->pointer_map());
478 instr->pointer_map()->set_lithium_position(index);
479 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000480}
481
482
483LConstantOperand* LChunk::DefineConstantOperand(HConstant* constant) {
484 return LConstantOperand::Create(constant->id());
485}
486
487
488int LChunk::GetParameterStackSlot(int index) const {
489 // The receiver is at index 0, the first parameter at index 1, so we
490 // shift all parameter indexes down by the number of parameters, and
491 // make sure they end up negative so they are distinguishable from
492 // spill slots.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000493 int result = index - info()->scope()->num_parameters() - 1;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000494 ASSERT(result < 0);
495 return result;
496}
497
498// A parameter relative to ebp in the arguments stub.
499int LChunk::ParameterAt(int index) {
500 ASSERT(-1 <= index); // -1 is the receiver.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000501 return (1 + info()->scope()->num_parameters() - index) *
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000502 kPointerSize;
503}
504
505
506LGap* LChunk::GetGapAt(int index) const {
507 return LGap::cast(instructions_[index]);
508}
509
510
511bool LChunk::IsGapAt(int index) const {
512 return instructions_[index]->IsGap();
513}
514
515
516int LChunk::NearestGapPos(int index) const {
517 while (!IsGapAt(index)) index--;
518 return index;
519}
520
521
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000522void LChunk::AddGapMove(int index, LOperand* from, LOperand* to) {
523 GetGapAt(index)->GetOrCreateParallelMove(LGap::START)->AddMove(from, to);
524}
525
526
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000527Handle<Object> LChunk::LookupLiteral(LConstantOperand* operand) const {
528 return HConstant::cast(graph_->LookupValue(operand->index()))->handle();
529}
530
531
532Representation LChunk::LookupLiteralRepresentation(
533 LConstantOperand* operand) const {
534 return graph_->LookupValue(operand->index())->representation();
535}
536
537
538LChunk* LChunkBuilder::Build() {
539 ASSERT(is_unused());
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000540 chunk_ = new LChunk(info(), graph());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000541 HPhase phase("Building chunk", chunk_);
542 status_ = BUILDING;
543 const ZoneList<HBasicBlock*>* blocks = graph()->blocks();
544 for (int i = 0; i < blocks->length(); i++) {
545 HBasicBlock* next = NULL;
546 if (i < blocks->length() - 1) next = blocks->at(i + 1);
547 DoBasicBlock(blocks->at(i), next);
548 if (is_aborted()) return NULL;
549 }
550 status_ = DONE;
551 return chunk_;
552}
553
554
555void LChunkBuilder::Abort(const char* format, ...) {
556 if (FLAG_trace_bailout) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000557 SmartPointer<char> name(info()->shared_info()->DebugName()->ToCString());
558 PrintF("Aborting LChunk building in @\"%s\": ", *name);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000559 va_list arguments;
560 va_start(arguments, format);
561 OS::VPrint(format, arguments);
562 va_end(arguments);
563 PrintF("\n");
564 }
565 status_ = ABORTED;
566}
567
568
569LRegister* LChunkBuilder::ToOperand(Register reg) {
570 return LRegister::Create(Register::ToAllocationIndex(reg));
571}
572
573
574LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
575 return new LUnallocated(LUnallocated::FIXED_REGISTER,
576 Register::ToAllocationIndex(reg));
577}
578
579
580LUnallocated* LChunkBuilder::ToUnallocated(DoubleRegister reg) {
581 return new LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
582 DoubleRegister::ToAllocationIndex(reg));
583}
584
585
586LOperand* LChunkBuilder::UseFixed(HValue* value, Register fixed_register) {
587 return Use(value, ToUnallocated(fixed_register));
588}
589
590
591LOperand* LChunkBuilder::UseFixedDouble(HValue* value, DoubleRegister reg) {
592 return Use(value, ToUnallocated(reg));
593}
594
595
596LOperand* LChunkBuilder::UseRegister(HValue* value) {
597 return Use(value, new LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
598}
599
600
601LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
602 return Use(value,
603 new LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
604 LUnallocated::USED_AT_START));
605}
606
607
608LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
609 return Use(value, new LUnallocated(LUnallocated::WRITABLE_REGISTER));
610}
611
612
613LOperand* LChunkBuilder::Use(HValue* value) {
614 return Use(value, new LUnallocated(LUnallocated::NONE));
615}
616
617
618LOperand* LChunkBuilder::UseAtStart(HValue* value) {
619 return Use(value, new LUnallocated(LUnallocated::NONE,
620 LUnallocated::USED_AT_START));
621}
622
623
624LOperand* LChunkBuilder::UseOrConstant(HValue* value) {
625 return value->IsConstant()
626 ? chunk_->DefineConstantOperand(HConstant::cast(value))
627 : Use(value);
628}
629
630
631LOperand* LChunkBuilder::UseOrConstantAtStart(HValue* value) {
632 return value->IsConstant()
633 ? chunk_->DefineConstantOperand(HConstant::cast(value))
634 : UseAtStart(value);
635}
636
637
638LOperand* LChunkBuilder::UseRegisterOrConstant(HValue* value) {
639 return value->IsConstant()
640 ? chunk_->DefineConstantOperand(HConstant::cast(value))
641 : UseRegister(value);
642}
643
644
645LOperand* LChunkBuilder::UseRegisterOrConstantAtStart(HValue* value) {
646 return value->IsConstant()
647 ? chunk_->DefineConstantOperand(HConstant::cast(value))
648 : UseRegisterAtStart(value);
649}
650
651
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000652LOperand* LChunkBuilder::UseAny(HValue* value) {
653 return value->IsConstant()
654 ? chunk_->DefineConstantOperand(HConstant::cast(value))
655 : Use(value, new LUnallocated(LUnallocated::ANY));
656}
657
658
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000659LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
660 if (value->EmitAtUses()) {
661 HInstruction* instr = HInstruction::cast(value);
662 VisitInstruction(instr);
663 }
664 allocator_->RecordUse(value, operand);
665 return operand;
666}
667
668
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000669template<int I, int T>
670LInstruction* LChunkBuilder::Define(LTemplateInstruction<1, I, T>* instr,
671 LUnallocated* result) {
672 allocator_->RecordDefinition(current_instruction_, result);
673 instr->set_result(result);
674 return instr;
675}
676
677
678template<int I, int T>
679LInstruction* LChunkBuilder::Define(LTemplateInstruction<1, I, T>* instr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000680 return Define(instr, new LUnallocated(LUnallocated::NONE));
681}
682
683
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000684template<int I, int T>
685LInstruction* LChunkBuilder::DefineAsRegister(
686 LTemplateInstruction<1, I, T>* instr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000687 return Define(instr, new LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
688}
689
690
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000691template<int I, int T>
692LInstruction* LChunkBuilder::DefineAsSpilled(
693 LTemplateInstruction<1, I, T>* instr, int index) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000694 return Define(instr, new LUnallocated(LUnallocated::FIXED_SLOT, index));
695}
696
697
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000698template<int I, int T>
699LInstruction* LChunkBuilder::DefineSameAsFirst(
700 LTemplateInstruction<1, I, T>* instr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000701 return Define(instr, new LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
702}
703
704
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000705template<int I, int T>
706LInstruction* LChunkBuilder::DefineFixed(
707 LTemplateInstruction<1, I, T>* instr, Register reg) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000708 return Define(instr, ToUnallocated(reg));
709}
710
711
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000712template<int I, int T>
713LInstruction* LChunkBuilder::DefineFixedDouble(
714 LTemplateInstruction<1, I, T>* instr, DoubleRegister reg) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000715 return Define(instr, ToUnallocated(reg));
716}
717
718
719LInstruction* LChunkBuilder::AssignEnvironment(LInstruction* instr) {
720 HEnvironment* hydrogen_env = current_block_->last_environment();
721 instr->set_environment(CreateEnvironment(hydrogen_env));
722 return instr;
723}
724
725
fschneider@chromium.org1df6b472011-01-26 08:23:03 +0000726LInstruction* LChunkBuilder::SetInstructionPendingDeoptimizationEnvironment(
727 LInstruction* instr, int ast_id) {
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000728 ASSERT(instruction_pending_deoptimization_environment_ == NULL);
fschneider@chromium.org1df6b472011-01-26 08:23:03 +0000729 ASSERT(pending_deoptimization_ast_id_ == AstNode::kNoNumber);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000730 instruction_pending_deoptimization_environment_ = instr;
fschneider@chromium.org1df6b472011-01-26 08:23:03 +0000731 pending_deoptimization_ast_id_ = ast_id;
732 return instr;
733}
734
735
736void LChunkBuilder::ClearInstructionPendingDeoptimizationEnvironment() {
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000737 instruction_pending_deoptimization_environment_ = NULL;
fschneider@chromium.org1df6b472011-01-26 08:23:03 +0000738 pending_deoptimization_ast_id_ = AstNode::kNoNumber;
739}
740
741
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000742LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
743 HInstruction* hinstr,
744 CanDeoptimize can_deoptimize) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000745#ifdef DEBUG
746 instr->VerifyCall();
747#endif
748 instr->MarkAsCall();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000749 instr = AssignPointerMap(instr);
750
751 if (hinstr->HasSideEffects()) {
752 ASSERT(hinstr->next()->IsSimulate());
753 HSimulate* sim = HSimulate::cast(hinstr->next());
fschneider@chromium.org1df6b472011-01-26 08:23:03 +0000754 instr = SetInstructionPendingDeoptimizationEnvironment(
755 instr, sim->ast_id());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000756 }
757
758 // If instruction does not have side-effects lazy deoptimization
759 // after the call will try to deoptimize to the point before the call.
760 // Thus we still need to attach environment to this call even if
761 // call sequence can not deoptimize eagerly.
762 bool needs_environment =
763 (can_deoptimize == CAN_DEOPTIMIZE_EAGERLY) || !hinstr->HasSideEffects();
764 if (needs_environment && !instr->HasEnvironment()) {
765 instr = AssignEnvironment(instr);
766 }
767
768 return instr;
769}
770
771
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000772LInstruction* LChunkBuilder::MarkAsSaveDoubles(LInstruction* instr) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000773 instr->MarkAsSaveDoubles();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000774 return instr;
775}
776
777
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000778LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
779 ASSERT(!instr->HasPointerMap());
780 instr->set_pointer_map(new LPointerMap(position_));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000781 return instr;
782}
783
784
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000785LUnallocated* LChunkBuilder::TempRegister() {
786 LUnallocated* operand = new LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
787 allocator_->RecordTemporary(operand);
788 return operand;
789}
790
791
792LOperand* LChunkBuilder::FixedTemp(Register reg) {
793 LUnallocated* operand = ToUnallocated(reg);
794 allocator_->RecordTemporary(operand);
795 return operand;
796}
797
798
799LOperand* LChunkBuilder::FixedTemp(DoubleRegister reg) {
800 LUnallocated* operand = ToUnallocated(reg);
801 allocator_->RecordTemporary(operand);
802 return operand;
803}
804
805
806LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
807 return new LLabel(instr->block());
808}
809
810
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000811LInstruction* LChunkBuilder::DoSoftDeoptimize(HSoftDeoptimize* instr) {
812 return AssignEnvironment(new LDeoptimize);
813}
814
815
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000816LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
817 return AssignEnvironment(new LDeoptimize);
818}
819
820
821LInstruction* LChunkBuilder::DoBit(Token::Value op,
822 HBitwiseBinaryOperation* instr) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000823 if (instr->representation().IsInteger32()) {
824 ASSERT(instr->left()->representation().IsInteger32());
825 ASSERT(instr->right()->representation().IsInteger32());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000826
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000827 LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
828 LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
829 return DefineSameAsFirst(new LBitI(op, left, right));
830 } else {
831 ASSERT(instr->representation().IsTagged());
832 ASSERT(instr->left()->representation().IsTagged());
833 ASSERT(instr->right()->representation().IsTagged());
834
835 LOperand* left = UseFixed(instr->left(), r1);
836 LOperand* right = UseFixed(instr->right(), r0);
837 LArithmeticT* result = new LArithmeticT(op, left, right);
838 return MarkAsCall(DefineFixed(result, r0), instr);
839 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000840}
841
842
843LInstruction* LChunkBuilder::DoShift(Token::Value op,
844 HBitwiseBinaryOperation* instr) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000845 if (instr->representation().IsTagged()) {
846 ASSERT(instr->left()->representation().IsTagged());
847 ASSERT(instr->right()->representation().IsTagged());
848
849 LOperand* left = UseFixed(instr->left(), r1);
850 LOperand* right = UseFixed(instr->right(), r0);
851 LArithmeticT* result = new LArithmeticT(op, left, right);
852 return MarkAsCall(DefineFixed(result, r0), instr);
853 }
854
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000855 ASSERT(instr->representation().IsInteger32());
856 ASSERT(instr->OperandAt(0)->representation().IsInteger32());
857 ASSERT(instr->OperandAt(1)->representation().IsInteger32());
858 LOperand* left = UseRegisterAtStart(instr->OperandAt(0));
859
860 HValue* right_value = instr->OperandAt(1);
861 LOperand* right = NULL;
862 int constant_value = 0;
863 if (right_value->IsConstant()) {
864 HConstant* constant = HConstant::cast(right_value);
865 right = chunk_->DefineConstantOperand(constant);
866 constant_value = constant->Integer32Value() & 0x1f;
867 } else {
868 right = UseRegister(right_value);
869 }
870
871 // Shift operations can only deoptimize if we do a logical shift
872 // by 0 and the result cannot be truncated to int32.
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000873 bool may_deopt = (op == Token::SHR && constant_value == 0);
874 bool does_deopt = false;
875 if (may_deopt) {
876 for (HUseIterator it(instr->uses()); !it.Done(); it.Advance()) {
877 if (!it.value()->CheckFlag(HValue::kTruncatingToInt32)) {
878 does_deopt = true;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000879 break;
880 }
881 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000882 }
883
884 LInstruction* result =
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000885 DefineSameAsFirst(new LShiftI(op, left, right, does_deopt));
886 return does_deopt ? AssignEnvironment(result) : result;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000887}
888
889
890LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
891 HArithmeticBinaryOperation* instr) {
892 ASSERT(instr->representation().IsDouble());
893 ASSERT(instr->left()->representation().IsDouble());
894 ASSERT(instr->right()->representation().IsDouble());
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000895 ASSERT(op != Token::MOD);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000896 LOperand* left = UseRegisterAtStart(instr->left());
897 LOperand* right = UseRegisterAtStart(instr->right());
898 LArithmeticD* result = new LArithmeticD(op, left, right);
899 return DefineSameAsFirst(result);
900}
901
902
903LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
904 HArithmeticBinaryOperation* instr) {
905 ASSERT(op == Token::ADD ||
906 op == Token::DIV ||
907 op == Token::MOD ||
908 op == Token::MUL ||
909 op == Token::SUB);
910 HValue* left = instr->left();
911 HValue* right = instr->right();
912 ASSERT(left->representation().IsTagged());
913 ASSERT(right->representation().IsTagged());
914 LOperand* left_operand = UseFixed(left, r1);
915 LOperand* right_operand = UseFixed(right, r0);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000916 LArithmeticT* result = new LArithmeticT(op, left_operand, right_operand);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000917 return MarkAsCall(DefineFixed(result, r0), instr);
918}
919
ager@chromium.org378b34e2011-01-28 08:04:38 +0000920
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000921void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
922 ASSERT(is_building());
923 current_block_ = block;
924 next_block_ = next_block;
925 if (block->IsStartBlock()) {
926 block->UpdateEnvironment(graph_->start_environment());
927 argument_count_ = 0;
928 } else if (block->predecessors()->length() == 1) {
929 // We have a single predecessor => copy environment and outgoing
930 // argument count from the predecessor.
931 ASSERT(block->phis()->length() == 0);
932 HBasicBlock* pred = block->predecessors()->at(0);
933 HEnvironment* last_environment = pred->last_environment();
934 ASSERT(last_environment != NULL);
935 // Only copy the environment, if it is later used again.
936 if (pred->end()->SecondSuccessor() == NULL) {
937 ASSERT(pred->end()->FirstSuccessor() == block);
938 } else {
939 if (pred->end()->FirstSuccessor()->block_id() > block->block_id() ||
940 pred->end()->SecondSuccessor()->block_id() > block->block_id()) {
941 last_environment = last_environment->Copy();
942 }
943 }
944 block->UpdateEnvironment(last_environment);
945 ASSERT(pred->argument_count() >= 0);
946 argument_count_ = pred->argument_count();
947 } else {
948 // We are at a state join => process phis.
949 HBasicBlock* pred = block->predecessors()->at(0);
950 // No need to copy the environment, it cannot be used later.
951 HEnvironment* last_environment = pred->last_environment();
952 for (int i = 0; i < block->phis()->length(); ++i) {
953 HPhi* phi = block->phis()->at(i);
954 last_environment->SetValueAt(phi->merged_index(), phi);
955 }
956 for (int i = 0; i < block->deleted_phis()->length(); ++i) {
957 last_environment->SetValueAt(block->deleted_phis()->at(i),
958 graph_->GetConstantUndefined());
959 }
960 block->UpdateEnvironment(last_environment);
961 // Pick up the outgoing argument count of one of the predecessors.
962 argument_count_ = pred->argument_count();
963 }
964 HInstruction* current = block->first();
965 int start = chunk_->instructions()->length();
966 while (current != NULL && !is_aborted()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000967 // Code for constants in registers is generated lazily.
968 if (!current->EmitAtUses()) {
969 VisitInstruction(current);
970 }
971 current = current->next();
972 }
973 int end = chunk_->instructions()->length() - 1;
974 if (end >= start) {
975 block->set_first_instruction_index(start);
976 block->set_last_instruction_index(end);
977 }
978 block->set_argument_count(argument_count_);
979 next_block_ = NULL;
980 current_block_ = NULL;
981}
982
983
984void LChunkBuilder::VisitInstruction(HInstruction* current) {
985 HInstruction* old_current = current_instruction_;
986 current_instruction_ = current;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000987 if (current->has_position()) position_ = current->position();
988 LInstruction* instr = current->CompileToLithium(this);
989
990 if (instr != NULL) {
991 if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
992 instr = AssignPointerMap(instr);
993 }
994 if (FLAG_stress_environments && !instr->HasEnvironment()) {
995 instr = AssignEnvironment(instr);
996 }
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000997 if (current->IsTest() && !instr->IsGoto()) {
998 ASSERT(instr->IsControl());
999 HTest* test = HTest::cast(current);
1000 instr->set_hydrogen_value(test->value());
1001 HBasicBlock* first = test->FirstSuccessor();
1002 HBasicBlock* second = test->SecondSuccessor();
1003 ASSERT(first != NULL && second != NULL);
1004 instr->SetBranchTargets(first->block_id(), second->block_id());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001005 } else {
1006 instr->set_hydrogen_value(current);
1007 }
1008
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001009 chunk_->AddInstruction(instr, current_block_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001010 }
1011 current_instruction_ = old_current;
1012}
1013
1014
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001015LEnvironment* LChunkBuilder::CreateEnvironment(HEnvironment* hydrogen_env) {
1016 if (hydrogen_env == NULL) return NULL;
1017
1018 LEnvironment* outer = CreateEnvironment(hydrogen_env->outer());
1019 int ast_id = hydrogen_env->ast_id();
1020 ASSERT(ast_id != AstNode::kNoNumber);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001021 int value_count = hydrogen_env->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001022 LEnvironment* result = new LEnvironment(hydrogen_env->closure(),
1023 ast_id,
1024 hydrogen_env->parameter_count(),
1025 argument_count_,
1026 value_count,
1027 outer);
1028 int argument_index = 0;
1029 for (int i = 0; i < value_count; ++i) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001030 if (hydrogen_env->is_special_index(i)) continue;
1031
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001032 HValue* value = hydrogen_env->values()->at(i);
1033 LOperand* op = NULL;
1034 if (value->IsArgumentsObject()) {
1035 op = NULL;
1036 } else if (value->IsPushArgument()) {
1037 op = new LArgument(argument_index++);
1038 } else {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001039 op = UseAny(value);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001040 }
1041 result->AddValue(op, value->representation());
1042 }
1043
1044 return result;
1045}
1046
1047
1048LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
1049 LInstruction* result = new LGoto(instr->FirstSuccessor()->block_id(),
1050 instr->include_stack_check());
1051 if (instr->include_stack_check()) result = AssignPointerMap(result);
1052 return result;
1053}
1054
1055
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001056LInstruction* LChunkBuilder::DoTest(HTest* instr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001057 HValue* v = instr->value();
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001058 if (!v->EmitAtUses()) {
1059 return new LBranch(UseRegisterAtStart(v));
1060 } else if (v->IsClassOfTest()) {
1061 HClassOfTest* compare = HClassOfTest::cast(v);
1062 ASSERT(compare->value()->representation().IsTagged());
1063 return new LClassOfTestAndBranch(UseTempRegister(compare->value()),
1064 TempRegister());
1065 } else if (v->IsCompare()) {
1066 HCompare* compare = HCompare::cast(v);
1067 Token::Value op = compare->token();
1068 HValue* left = compare->left();
1069 HValue* right = compare->right();
1070 Representation r = compare->GetInputRepresentation();
1071 if (r.IsInteger32()) {
1072 ASSERT(left->representation().IsInteger32());
1073 ASSERT(right->representation().IsInteger32());
1074 return new LCmpIDAndBranch(UseRegisterAtStart(left),
1075 UseRegisterAtStart(right));
1076 } else if (r.IsDouble()) {
1077 ASSERT(left->representation().IsDouble());
1078 ASSERT(right->representation().IsDouble());
1079 return new LCmpIDAndBranch(UseRegisterAtStart(left),
1080 UseRegisterAtStart(right));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001081 } else {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001082 ASSERT(left->representation().IsTagged());
1083 ASSERT(right->representation().IsTagged());
1084 bool reversed = op == Token::GT || op == Token::LTE;
1085 LOperand* left_operand = UseFixed(left, reversed ? r0 : r1);
1086 LOperand* right_operand = UseFixed(right, reversed ? r1 : r0);
1087 LInstruction* result = new LCmpTAndBranch(left_operand, right_operand);
1088 return MarkAsCall(result, instr);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001089 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001090 } else if (v->IsIsSmi()) {
1091 HIsSmi* compare = HIsSmi::cast(v);
1092 ASSERT(compare->value()->representation().IsTagged());
1093 return new LIsSmiAndBranch(Use(compare->value()));
1094 } else if (v->IsIsUndetectable()) {
1095 HIsUndetectable* compare = HIsUndetectable::cast(v);
1096 ASSERT(compare->value()->representation().IsTagged());
1097 return new LIsUndetectableAndBranch(UseRegisterAtStart(compare->value()),
1098 TempRegister());
1099 } else if (v->IsHasInstanceType()) {
1100 HHasInstanceType* compare = HHasInstanceType::cast(v);
1101 ASSERT(compare->value()->representation().IsTagged());
1102 return new LHasInstanceTypeAndBranch(UseRegisterAtStart(compare->value()));
1103 } else if (v->IsHasCachedArrayIndex()) {
1104 HHasCachedArrayIndex* compare = HHasCachedArrayIndex::cast(v);
1105 ASSERT(compare->value()->representation().IsTagged());
1106 return new LHasCachedArrayIndexAndBranch(
1107 UseRegisterAtStart(compare->value()));
1108 } else if (v->IsIsNull()) {
1109 HIsNull* compare = HIsNull::cast(v);
1110 ASSERT(compare->value()->representation().IsTagged());
1111 return new LIsNullAndBranch(UseRegisterAtStart(compare->value()));
1112 } else if (v->IsIsObject()) {
1113 HIsObject* compare = HIsObject::cast(v);
1114 ASSERT(compare->value()->representation().IsTagged());
1115 LOperand* temp = TempRegister();
1116 return new LIsObjectAndBranch(UseRegisterAtStart(compare->value()), temp);
1117 } else if (v->IsCompareJSObjectEq()) {
1118 HCompareJSObjectEq* compare = HCompareJSObjectEq::cast(v);
1119 return new LCmpJSObjectEqAndBranch(UseRegisterAtStart(compare->left()),
1120 UseRegisterAtStart(compare->right()));
1121 } else if (v->IsCompareSymbolEq()) {
1122 HCompareSymbolEq* compare = HCompareSymbolEq::cast(v);
1123 return new LCmpSymbolEqAndBranch(UseRegisterAtStart(compare->left()),
1124 UseRegisterAtStart(compare->right()));
1125 } else if (v->IsInstanceOf()) {
1126 HInstanceOf* instance_of = HInstanceOf::cast(v);
1127 LInstruction* result =
1128 new LInstanceOfAndBranch(UseFixed(instance_of->left(), r0),
1129 UseFixed(instance_of->right(), r1));
1130 return MarkAsCall(result, instr);
1131 } else if (v->IsTypeofIs()) {
1132 HTypeofIs* typeof_is = HTypeofIs::cast(v);
1133 return new LTypeofIsAndBranch(UseTempRegister(typeof_is->value()));
1134 } else if (v->IsIsConstructCall()) {
1135 return new LIsConstructCallAndBranch(TempRegister());
1136 } else if (v->IsConstant()) {
1137 HBasicBlock* successor = HConstant::cast(v)->ToBoolean()
1138 ? instr->FirstSuccessor()
1139 : instr->SecondSuccessor();
1140 return new LGoto(successor->block_id());
1141 } else {
1142 Abort("Undefined compare before branch");
1143 return NULL;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001144 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001145}
1146
1147
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001148
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001149LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001150 ASSERT(instr->value()->representation().IsTagged());
1151 LOperand* value = UseRegisterAtStart(instr->value());
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001152 LOperand* temp = TempRegister();
1153 return new LCmpMapAndBranch(value, temp);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001154}
1155
1156
1157LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* length) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001158 return DefineAsRegister(new LArgumentsLength(UseRegister(length->value())));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001159}
1160
1161
1162LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
1163 return DefineAsRegister(new LArgumentsElements);
1164}
1165
1166
1167LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001168 LInstanceOf* result =
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00001169 new LInstanceOf(UseFixed(instr->left(), r0),
1170 UseFixed(instr->right(), r1));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001171 return MarkAsCall(DefineFixed(result, r0), instr);
1172}
1173
1174
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001175LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
1176 HInstanceOfKnownGlobal* instr) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001177 LInstanceOfKnownGlobal* result =
1178 new LInstanceOfKnownGlobal(UseFixed(instr->value(), r0), FixedTemp(r4));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001179 return MarkAsCall(DefineFixed(result, r0), instr);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001180}
1181
1182
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001183LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
1184 LOperand* function = UseFixed(instr->function(), r1);
1185 LOperand* receiver = UseFixed(instr->receiver(), r0);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001186 LOperand* length = UseFixed(instr->length(), r2);
1187 LOperand* elements = UseFixed(instr->elements(), r3);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001188 LApplyArguments* result = new LApplyArguments(function,
1189 receiver,
1190 length,
1191 elements);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001192 return MarkAsCall(DefineFixed(result, r0), instr, CAN_DEOPTIMIZE_EAGERLY);
1193}
1194
1195
1196LInstruction* LChunkBuilder::DoPushArgument(HPushArgument* instr) {
1197 ++argument_count_;
1198 LOperand* argument = Use(instr->argument());
1199 return new LPushArgument(argument);
1200}
1201
1202
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001203LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1204 return instr->HasNoUses() ? NULL : DefineAsRegister(new LThisFunction);
1205}
1206
1207
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001208LInstruction* LChunkBuilder::DoContext(HContext* instr) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001209 return instr->HasNoUses() ? NULL : DefineAsRegister(new LContext);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001210}
1211
1212
1213LInstruction* LChunkBuilder::DoOuterContext(HOuterContext* instr) {
1214 LOperand* context = UseRegisterAtStart(instr->value());
1215 return DefineAsRegister(new LOuterContext(context));
1216}
1217
1218
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001219LInstruction* LChunkBuilder::DoGlobalObject(HGlobalObject* instr) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001220 LOperand* context = UseRegisterAtStart(instr->value());
1221 return DefineAsRegister(new LGlobalObject(context));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001222}
1223
1224
1225LInstruction* LChunkBuilder::DoGlobalReceiver(HGlobalReceiver* instr) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001226 LOperand* global_object = UseRegisterAtStart(instr->value());
1227 return DefineAsRegister(new LGlobalReceiver(global_object));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001228}
1229
1230
1231LInstruction* LChunkBuilder::DoCallConstantFunction(
1232 HCallConstantFunction* instr) {
1233 argument_count_ -= instr->argument_count();
1234 return MarkAsCall(DefineFixed(new LCallConstantFunction, r0), instr);
1235}
1236
1237
danno@chromium.org160a7b02011-04-18 15:51:38 +00001238LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1239 LOperand* function = UseFixed(instr->function(), r1);
1240 argument_count_ -= instr->argument_count();
1241 LInvokeFunction* result = new LInvokeFunction(function);
1242 return MarkAsCall(DefineFixed(result, r0), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1243}
1244
1245
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001246LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001247 BuiltinFunctionId op = instr->op();
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001248 if (op == kMathLog || op == kMathSin || op == kMathCos) {
1249 LOperand* input = UseFixedDouble(instr->value(), d2);
1250 LUnaryMathOperation* result = new LUnaryMathOperation(input, NULL);
1251 return MarkAsCall(DefineFixedDouble(result, d2), instr);
1252 } else {
1253 LOperand* input = UseRegisterAtStart(instr->value());
1254 LOperand* temp = (op == kMathFloor) ? TempRegister() : NULL;
1255 LUnaryMathOperation* result = new LUnaryMathOperation(input, temp);
1256 switch (op) {
1257 case kMathAbs:
1258 return AssignEnvironment(AssignPointerMap(DefineSameAsFirst(result)));
1259 case kMathFloor:
1260 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1261 case kMathSqrt:
1262 return DefineSameAsFirst(result);
1263 case kMathRound:
1264 return AssignEnvironment(DefineAsRegister(result));
1265 case kMathPowHalf:
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001266 return DefineSameAsFirst(result);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001267 default:
1268 UNREACHABLE();
1269 return NULL;
1270 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001271 }
1272}
1273
1274
1275LInstruction* LChunkBuilder::DoCallKeyed(HCallKeyed* instr) {
1276 ASSERT(instr->key()->representation().IsTagged());
1277 argument_count_ -= instr->argument_count();
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001278 LOperand* key = UseFixed(instr->key(), r2);
1279 return MarkAsCall(DefineFixed(new LCallKeyed(key), r0), instr);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001280}
1281
1282
1283LInstruction* LChunkBuilder::DoCallNamed(HCallNamed* instr) {
1284 argument_count_ -= instr->argument_count();
1285 return MarkAsCall(DefineFixed(new LCallNamed, r0), instr);
1286}
1287
1288
1289LInstruction* LChunkBuilder::DoCallGlobal(HCallGlobal* instr) {
1290 argument_count_ -= instr->argument_count();
1291 return MarkAsCall(DefineFixed(new LCallGlobal, r0), instr);
1292}
1293
1294
1295LInstruction* LChunkBuilder::DoCallKnownGlobal(HCallKnownGlobal* instr) {
1296 argument_count_ -= instr->argument_count();
1297 return MarkAsCall(DefineFixed(new LCallKnownGlobal, r0), instr);
1298}
1299
1300
1301LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
1302 LOperand* constructor = UseFixed(instr->constructor(), r1);
1303 argument_count_ -= instr->argument_count();
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001304 LCallNew* result = new LCallNew(constructor);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001305 return MarkAsCall(DefineFixed(result, r0), instr);
1306}
1307
1308
1309LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
1310 argument_count_ -= instr->argument_count();
1311 return MarkAsCall(DefineFixed(new LCallFunction, r0), instr);
1312}
1313
1314
1315LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1316 argument_count_ -= instr->argument_count();
1317 return MarkAsCall(DefineFixed(new LCallRuntime, r0), instr);
1318}
1319
1320
1321LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1322 return DoShift(Token::SHR, instr);
1323}
1324
1325
1326LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1327 return DoShift(Token::SAR, instr);
1328}
1329
1330
1331LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1332 return DoShift(Token::SHL, instr);
1333}
1334
1335
1336LInstruction* LChunkBuilder::DoBitAnd(HBitAnd* instr) {
1337 return DoBit(Token::BIT_AND, instr);
1338}
1339
1340
1341LInstruction* LChunkBuilder::DoBitNot(HBitNot* instr) {
1342 ASSERT(instr->value()->representation().IsInteger32());
1343 ASSERT(instr->representation().IsInteger32());
1344 return DefineSameAsFirst(new LBitNotI(UseRegisterAtStart(instr->value())));
1345}
1346
1347
1348LInstruction* LChunkBuilder::DoBitOr(HBitOr* instr) {
1349 return DoBit(Token::BIT_OR, instr);
1350}
1351
1352
1353LInstruction* LChunkBuilder::DoBitXor(HBitXor* instr) {
1354 return DoBit(Token::BIT_XOR, instr);
1355}
1356
1357
1358LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
1359 if (instr->representation().IsDouble()) {
1360 return DoArithmeticD(Token::DIV, instr);
1361 } else if (instr->representation().IsInteger32()) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001362 // TODO(1042) The fixed register allocation
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001363 // is needed because we call TypeRecordingBinaryOpStub from
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001364 // the generated code, which requires registers r0
1365 // and r1 to be used. We should remove that
1366 // when we provide a native implementation.
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001367 LOperand* dividend = UseFixed(instr->left(), r0);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001368 LOperand* divisor = UseFixed(instr->right(), r1);
1369 return AssignEnvironment(AssignPointerMap(
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001370 DefineFixed(new LDivI(dividend, divisor), r0)));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001371 } else {
1372 return DoArithmeticT(Token::DIV, instr);
1373 }
1374}
1375
1376
1377LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1378 if (instr->representation().IsInteger32()) {
1379 ASSERT(instr->left()->representation().IsInteger32());
1380 ASSERT(instr->right()->representation().IsInteger32());
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001381
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001382 LModI* mod;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001383 if (instr->HasPowerOf2Divisor()) {
1384 ASSERT(!instr->CheckFlag(HValue::kCanBeDivByZero));
1385 LOperand* value = UseRegisterAtStart(instr->left());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001386 mod = new LModI(value, UseOrConstant(instr->right()));
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001387 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001388 LOperand* dividend = UseRegister(instr->left());
1389 LOperand* divisor = UseRegisterAtStart(instr->right());
1390 mod = new LModI(dividend,
1391 divisor,
1392 TempRegister(),
1393 FixedTemp(d1),
1394 FixedTemp(d2));
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001395 }
1396
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001397 return AssignEnvironment(DefineSameAsFirst(mod));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001398 } else if (instr->representation().IsTagged()) {
1399 return DoArithmeticT(Token::MOD, instr);
1400 } else {
1401 ASSERT(instr->representation().IsDouble());
1402 // We call a C function for double modulo. It can't trigger a GC.
1403 // We need to use fixed result register for the call.
1404 // TODO(fschneider): Allow any register as input registers.
1405 LOperand* left = UseFixedDouble(instr->left(), d1);
1406 LOperand* right = UseFixedDouble(instr->right(), d2);
1407 LArithmeticD* result = new LArithmeticD(Token::MOD, left, right);
1408 return MarkAsCall(DefineFixedDouble(result, d1), instr);
1409 }
1410}
1411
1412
1413LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1414 if (instr->representation().IsInteger32()) {
1415 ASSERT(instr->left()->representation().IsInteger32());
1416 ASSERT(instr->right()->representation().IsInteger32());
1417 LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1418 LOperand* right = UseOrConstant(instr->MostConstantOperand());
1419 LOperand* temp = NULL;
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001420 if (instr->CheckFlag(HValue::kBailoutOnMinusZero) &&
1421 (instr->CheckFlag(HValue::kCanOverflow) ||
1422 !right->IsConstantOperand())) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001423 temp = TempRegister();
1424 }
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001425 return AssignEnvironment(DefineSameAsFirst(new LMulI(left, right, temp)));
1426
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001427 } else if (instr->representation().IsDouble()) {
1428 return DoArithmeticD(Token::MUL, instr);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001429
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001430 } else {
1431 return DoArithmeticT(Token::MUL, instr);
1432 }
1433}
1434
1435
1436LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1437 if (instr->representation().IsInteger32()) {
1438 ASSERT(instr->left()->representation().IsInteger32());
1439 ASSERT(instr->right()->representation().IsInteger32());
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001440 LOperand* left = UseRegisterAtStart(instr->left());
1441 LOperand* right = UseOrConstantAtStart(instr->right());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001442 LSubI* sub = new LSubI(left, right);
1443 LInstruction* result = DefineSameAsFirst(sub);
1444 if (instr->CheckFlag(HValue::kCanOverflow)) {
1445 result = AssignEnvironment(result);
1446 }
1447 return result;
1448 } else if (instr->representation().IsDouble()) {
1449 return DoArithmeticD(Token::SUB, instr);
1450 } else {
1451 return DoArithmeticT(Token::SUB, instr);
1452 }
1453}
1454
1455
1456LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1457 if (instr->representation().IsInteger32()) {
1458 ASSERT(instr->left()->representation().IsInteger32());
1459 ASSERT(instr->right()->representation().IsInteger32());
1460 LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1461 LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1462 LAddI* add = new LAddI(left, right);
1463 LInstruction* result = DefineSameAsFirst(add);
1464 if (instr->CheckFlag(HValue::kCanOverflow)) {
1465 result = AssignEnvironment(result);
1466 }
1467 return result;
1468 } else if (instr->representation().IsDouble()) {
1469 return DoArithmeticD(Token::ADD, instr);
1470 } else {
1471 ASSERT(instr->representation().IsTagged());
1472 return DoArithmeticT(Token::ADD, instr);
1473 }
1474}
1475
1476
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001477LInstruction* LChunkBuilder::DoPower(HPower* instr) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001478 ASSERT(instr->representation().IsDouble());
1479 // We call a C function for double power. It can't trigger a GC.
1480 // We need to use fixed result register for the call.
1481 Representation exponent_type = instr->right()->representation();
1482 ASSERT(instr->left()->representation().IsDouble());
1483 LOperand* left = UseFixedDouble(instr->left(), d1);
1484 LOperand* right = exponent_type.IsDouble() ?
1485 UseFixedDouble(instr->right(), d2) :
1486 UseFixed(instr->right(), r0);
1487 LPower* result = new LPower(left, right);
1488 return MarkAsCall(DefineFixedDouble(result, d3),
1489 instr,
1490 CAN_DEOPTIMIZE_EAGERLY);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001491}
1492
1493
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001494LInstruction* LChunkBuilder::DoCompare(HCompare* instr) {
1495 Token::Value op = instr->token();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001496 Representation r = instr->GetInputRepresentation();
1497 if (r.IsInteger32()) {
1498 ASSERT(instr->left()->representation().IsInteger32());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001499 ASSERT(instr->right()->representation().IsInteger32());
1500 LOperand* left = UseRegisterAtStart(instr->left());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001501 LOperand* right = UseRegisterAtStart(instr->right());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001502 return DefineAsRegister(new LCmpID(left, right));
1503 } else if (r.IsDouble()) {
1504 ASSERT(instr->left()->representation().IsDouble());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001505 ASSERT(instr->right()->representation().IsDouble());
1506 LOperand* left = UseRegisterAtStart(instr->left());
1507 LOperand* right = UseRegisterAtStart(instr->right());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001508 return DefineAsRegister(new LCmpID(left, right));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001509 } else {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001510 ASSERT(instr->left()->representation().IsTagged());
1511 ASSERT(instr->right()->representation().IsTagged());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001512 bool reversed = (op == Token::GT || op == Token::LTE);
1513 LOperand* left = UseFixed(instr->left(), reversed ? r0 : r1);
1514 LOperand* right = UseFixed(instr->right(), reversed ? r1 : r0);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001515 LCmpT* result = new LCmpT(left, right);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001516 return MarkAsCall(DefineFixed(result, r0), instr);
1517 }
1518}
1519
1520
1521LInstruction* LChunkBuilder::DoCompareJSObjectEq(
1522 HCompareJSObjectEq* instr) {
1523 LOperand* left = UseRegisterAtStart(instr->left());
1524 LOperand* right = UseRegisterAtStart(instr->right());
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001525 LCmpJSObjectEq* result = new LCmpJSObjectEq(left, right);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001526 return DefineAsRegister(result);
1527}
1528
1529
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001530LInstruction* LChunkBuilder::DoCompareSymbolEq(
1531 HCompareSymbolEq* instr) {
1532 LOperand* left = UseRegisterAtStart(instr->left());
1533 LOperand* right = UseRegisterAtStart(instr->right());
1534 LCmpSymbolEq* result = new LCmpSymbolEq(left, right);
1535 return DefineAsRegister(result);
1536}
1537
1538
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001539LInstruction* LChunkBuilder::DoIsNull(HIsNull* instr) {
1540 ASSERT(instr->value()->representation().IsTagged());
1541 LOperand* value = UseRegisterAtStart(instr->value());
1542
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001543 return DefineAsRegister(new LIsNull(value));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001544}
1545
1546
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001547LInstruction* LChunkBuilder::DoIsObject(HIsObject* instr) {
1548 ASSERT(instr->value()->representation().IsTagged());
1549 LOperand* value = UseRegisterAtStart(instr->value());
1550
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001551 return DefineAsRegister(new LIsObject(value));
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001552}
1553
1554
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001555LInstruction* LChunkBuilder::DoIsSmi(HIsSmi* instr) {
1556 ASSERT(instr->value()->representation().IsTagged());
1557 LOperand* value = UseAtStart(instr->value());
1558
1559 return DefineAsRegister(new LIsSmi(value));
1560}
1561
1562
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001563LInstruction* LChunkBuilder::DoIsUndetectable(HIsUndetectable* instr) {
1564 ASSERT(instr->value()->representation().IsTagged());
1565 LOperand* value = UseRegisterAtStart(instr->value());
1566
1567 return DefineAsRegister(new LIsUndetectable(value));
1568}
1569
1570
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001571LInstruction* LChunkBuilder::DoHasInstanceType(HHasInstanceType* instr) {
1572 ASSERT(instr->value()->representation().IsTagged());
1573 LOperand* value = UseRegisterAtStart(instr->value());
1574
1575 return DefineAsRegister(new LHasInstanceType(value));
1576}
1577
1578
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001579LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
1580 HGetCachedArrayIndex* instr) {
1581 ASSERT(instr->value()->representation().IsTagged());
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001582 LOperand* value = UseRegisterAtStart(instr->value());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001583
1584 return DefineAsRegister(new LGetCachedArrayIndex(value));
1585}
1586
1587
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001588LInstruction* LChunkBuilder::DoHasCachedArrayIndex(
1589 HHasCachedArrayIndex* instr) {
1590 ASSERT(instr->value()->representation().IsTagged());
1591 LOperand* value = UseRegister(instr->value());
1592
1593 return DefineAsRegister(new LHasCachedArrayIndex(value));
1594}
1595
1596
1597LInstruction* LChunkBuilder::DoClassOfTest(HClassOfTest* instr) {
1598 ASSERT(instr->value()->representation().IsTagged());
1599 LOperand* value = UseTempRegister(instr->value());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001600 return DefineSameAsFirst(new LClassOfTest(value));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001601}
1602
1603
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00001604LInstruction* LChunkBuilder::DoJSArrayLength(HJSArrayLength* instr) {
1605 LOperand* array = UseRegisterAtStart(instr->value());
1606 return DefineAsRegister(new LJSArrayLength(array));
1607}
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001608
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001609
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001610LInstruction* LChunkBuilder::DoExternalArrayLength(
1611 HExternalArrayLength* instr) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001612 LOperand* array = UseRegisterAtStart(instr->value());
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001613 return DefineAsRegister(new LExternalArrayLength(array));
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001614}
1615
1616
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00001617LInstruction* LChunkBuilder::DoFixedArrayLength(HFixedArrayLength* instr) {
1618 LOperand* array = UseRegisterAtStart(instr->value());
1619 return DefineAsRegister(new LFixedArrayLength(array));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001620}
1621
1622
1623LInstruction* LChunkBuilder::DoValueOf(HValueOf* instr) {
1624 LOperand* object = UseRegister(instr->value());
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001625 LValueOf* result = new LValueOf(object, TempRegister());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001626 return AssignEnvironment(DefineSameAsFirst(result));
1627}
1628
1629
1630LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
1631 return AssignEnvironment(new LBoundsCheck(UseRegisterAtStart(instr->index()),
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001632 UseRegister(instr->length())));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001633}
1634
1635
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001636LInstruction* LChunkBuilder::DoAbnormalExit(HAbnormalExit* instr) {
1637 // The control instruction marking the end of a block that completed
1638 // abruptly (e.g., threw an exception). There is nothing specific to do.
1639 return NULL;
1640}
1641
1642
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001643LInstruction* LChunkBuilder::DoThrow(HThrow* instr) {
1644 LOperand* value = UseFixed(instr->value(), r0);
1645 return MarkAsCall(new LThrow(value), instr);
1646}
1647
1648
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001649LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
1650 return NULL;
1651}
1652
1653
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001654LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
1655 // All HForceRepresentation instructions should be eliminated in the
1656 // representation change phase of Hydrogen.
1657 UNREACHABLE();
1658 return NULL;
1659}
1660
1661
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001662LInstruction* LChunkBuilder::DoChange(HChange* instr) {
1663 Representation from = instr->from();
1664 Representation to = instr->to();
1665 if (from.IsTagged()) {
1666 if (to.IsDouble()) {
1667 LOperand* value = UseRegister(instr->value());
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001668 LNumberUntagD* res = new LNumberUntagD(value);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001669 return AssignEnvironment(DefineAsRegister(res));
1670 } else {
1671 ASSERT(to.IsInteger32());
1672 LOperand* value = UseRegister(instr->value());
1673 bool needs_check = !instr->value()->type().IsSmi();
1674 LInstruction* res = NULL;
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001675 if (!needs_check) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001676 res = DefineSameAsFirst(new LSmiUntag(value, needs_check));
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001677 } else {
1678 LOperand* temp1 = TempRegister();
1679 LOperand* temp2 = instr->CanTruncateToInt32() ? TempRegister()
1680 : NULL;
1681 LOperand* temp3 = instr->CanTruncateToInt32() ? FixedTemp(d3)
1682 : NULL;
1683 res = DefineSameAsFirst(new LTaggedToI(value, temp1, temp2, temp3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001684 res = AssignEnvironment(res);
1685 }
1686 return res;
1687 }
1688 } else if (from.IsDouble()) {
1689 if (to.IsTagged()) {
1690 LOperand* value = UseRegister(instr->value());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001691 LOperand* temp1 = TempRegister();
1692 LOperand* temp2 = TempRegister();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001693
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001694 // Make sure that the temp and result_temp registers are
1695 // different.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001696 LUnallocated* result_temp = TempRegister();
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001697 LNumberTagD* result = new LNumberTagD(value, temp1, temp2);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001698 Define(result, result_temp);
1699 return AssignPointerMap(result);
1700 } else {
1701 ASSERT(to.IsInteger32());
1702 LOperand* value = UseRegister(instr->value());
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001703 LDoubleToI* res =
1704 new LDoubleToI(value,
1705 TempRegister(),
1706 instr->CanTruncateToInt32() ? TempRegister() : NULL);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001707 return AssignEnvironment(DefineAsRegister(res));
1708 }
1709 } else if (from.IsInteger32()) {
1710 if (to.IsTagged()) {
1711 HValue* val = instr->value();
1712 LOperand* value = UseRegister(val);
1713 if (val->HasRange() && val->range()->IsInSmiRange()) {
1714 return DefineSameAsFirst(new LSmiTag(value));
1715 } else {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001716 LNumberTagI* result = new LNumberTagI(value);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001717 return AssignEnvironment(AssignPointerMap(DefineSameAsFirst(result)));
1718 }
1719 } else {
1720 ASSERT(to.IsDouble());
1721 LOperand* value = Use(instr->value());
1722 return DefineAsRegister(new LInteger32ToDouble(value));
1723 }
1724 }
1725 UNREACHABLE();
1726 return NULL;
1727}
1728
1729
1730LInstruction* LChunkBuilder::DoCheckNonSmi(HCheckNonSmi* instr) {
1731 LOperand* value = UseRegisterAtStart(instr->value());
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001732 return AssignEnvironment(new LCheckNonSmi(value));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001733}
1734
1735
1736LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
1737 LOperand* value = UseRegisterAtStart(instr->value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001738 LInstruction* result = new LCheckInstanceType(value);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001739 return AssignEnvironment(result);
1740}
1741
1742
1743LInstruction* LChunkBuilder::DoCheckPrototypeMaps(HCheckPrototypeMaps* instr) {
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001744 LOperand* temp1 = TempRegister();
1745 LOperand* temp2 = TempRegister();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001746 LInstruction* result = new LCheckPrototypeMaps(temp1, temp2);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001747 return AssignEnvironment(result);
1748}
1749
1750
1751LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
1752 LOperand* value = UseRegisterAtStart(instr->value());
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001753 return AssignEnvironment(new LCheckSmi(value));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001754}
1755
1756
1757LInstruction* LChunkBuilder::DoCheckFunction(HCheckFunction* instr) {
1758 LOperand* value = UseRegisterAtStart(instr->value());
1759 return AssignEnvironment(new LCheckFunction(value));
1760}
1761
1762
1763LInstruction* LChunkBuilder::DoCheckMap(HCheckMap* instr) {
1764 LOperand* value = UseRegisterAtStart(instr->value());
1765 LInstruction* result = new LCheckMap(value);
1766 return AssignEnvironment(result);
1767}
1768
1769
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001770LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
1771 HValue* value = instr->value();
1772 Representation input_rep = value->representation();
1773 LOperand* reg = UseRegister(value);
1774 if (input_rep.IsDouble()) {
1775 return DefineAsRegister(new LClampDToUint8(reg, FixedTemp(d1)));
1776 } else if (input_rep.IsInteger32()) {
1777 return DefineAsRegister(new LClampIToUint8(reg));
1778 } else {
1779 ASSERT(input_rep.IsTagged());
1780 // Register allocator doesn't (yet) support allocation of double
1781 // temps. Reserve d1 explicitly.
1782 LClampTToUint8* result = new LClampTToUint8(reg, FixedTemp(d1));
1783 return AssignEnvironment(DefineAsRegister(result));
1784 }
1785}
1786
1787
ricow@chromium.orgc54d3652011-05-30 09:20:16 +00001788LInstruction* LChunkBuilder::DoToInt32(HToInt32* instr) {
1789 HValue* value = instr->value();
1790 Representation input_rep = value->representation();
1791 LOperand* reg = UseRegister(value);
1792 if (input_rep.IsDouble()) {
1793 LOperand* temp1 = TempRegister();
1794 LOperand* temp2 = TempRegister();
1795 LDoubleToI* res = new LDoubleToI(reg, temp1, temp2);
1796 return AssignEnvironment(DefineAsRegister(res));
1797 } else if (input_rep.IsInteger32()) {
1798 // Canonicalization should already have removed the hydrogen instruction in
1799 // this case, since it is a noop.
1800 UNREACHABLE();
1801 return NULL;
1802 } else {
1803 ASSERT(input_rep.IsTagged());
1804 LOperand* temp1 = TempRegister();
1805 LOperand* temp2 = TempRegister();
1806 LOperand* temp3 = FixedTemp(d3);
1807 LTaggedToI* res = new LTaggedToI(reg, temp1, temp2, temp3);
1808 return AssignEnvironment(DefineSameAsFirst(res));
1809 }
1810}
1811
1812
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001813LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
1814 return new LReturn(UseFixed(instr->value(), r0));
1815}
1816
1817
1818LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
1819 Representation r = instr->representation();
1820 if (r.IsInteger32()) {
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001821 return DefineAsRegister(new LConstantI);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001822 } else if (r.IsDouble()) {
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001823 return DefineAsRegister(new LConstantD);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001824 } else if (r.IsTagged()) {
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001825 return DefineAsRegister(new LConstantT);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001826 } else {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001827 UNREACHABLE();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001828 return NULL;
1829 }
1830}
1831
1832
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001833LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
1834 LLoadGlobalCell* result = new LLoadGlobalCell;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001835 return instr->check_hole_value()
1836 ? AssignEnvironment(DefineAsRegister(result))
1837 : DefineAsRegister(result);
1838}
1839
1840
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001841LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
1842 LOperand* global_object = UseFixed(instr->global_object(), r0);
1843 LLoadGlobalGeneric* result = new LLoadGlobalGeneric(global_object);
1844 return MarkAsCall(DefineFixed(result, r0), instr);
1845}
1846
1847
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001848LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001849 if (instr->check_hole_value()) {
1850 LOperand* temp = TempRegister();
1851 LOperand* value = UseRegister(instr->value());
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001852 return AssignEnvironment(new LStoreGlobalCell(value, temp));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001853 } else {
1854 LOperand* value = UseRegisterAtStart(instr->value());
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001855 return new LStoreGlobalCell(value, NULL);
ager@chromium.org378b34e2011-01-28 08:04:38 +00001856 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001857}
1858
1859
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001860LInstruction* LChunkBuilder::DoStoreGlobalGeneric(HStoreGlobalGeneric* instr) {
1861 LOperand* global_object = UseFixed(instr->global_object(), r1);
1862 LOperand* value = UseFixed(instr->value(), r0);
1863 LStoreGlobalGeneric* result =
1864 new LStoreGlobalGeneric(global_object, value);
1865 return MarkAsCall(result, instr);
1866}
1867
1868
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001869LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001870 LOperand* context = UseRegisterAtStart(instr->value());
1871 return DefineAsRegister(new LLoadContextSlot(context));
1872}
1873
1874
1875LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001876 LOperand* context;
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001877 LOperand* value;
1878 if (instr->NeedsWriteBarrier()) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001879 context = UseTempRegister(instr->context());
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001880 value = UseTempRegister(instr->value());
1881 } else {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001882 context = UseRegister(instr->context());
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001883 value = UseRegister(instr->value());
1884 }
1885 return new LStoreContextSlot(context, value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001886}
1887
1888
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001889LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
1890 return DefineAsRegister(
1891 new LLoadNamedField(UseRegisterAtStart(instr->object())));
1892}
1893
1894
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001895LInstruction* LChunkBuilder::DoLoadNamedFieldPolymorphic(
1896 HLoadNamedFieldPolymorphic* instr) {
1897 ASSERT(instr->representation().IsTagged());
1898 if (instr->need_generic()) {
1899 LOperand* obj = UseFixed(instr->object(), r0);
1900 LLoadNamedFieldPolymorphic* result = new LLoadNamedFieldPolymorphic(obj);
1901 return MarkAsCall(DefineFixed(result, r0), instr);
1902 } else {
1903 LOperand* obj = UseRegisterAtStart(instr->object());
1904 LLoadNamedFieldPolymorphic* result = new LLoadNamedFieldPolymorphic(obj);
1905 return AssignEnvironment(DefineAsRegister(result));
1906 }
1907}
1908
1909
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001910LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
1911 LOperand* object = UseFixed(instr->object(), r0);
1912 LInstruction* result = DefineFixed(new LLoadNamedGeneric(object), r0);
1913 return MarkAsCall(result, instr);
1914}
1915
1916
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00001917LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
1918 HLoadFunctionPrototype* instr) {
1919 return AssignEnvironment(DefineAsRegister(
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001920 new LLoadFunctionPrototype(UseRegister(instr->function()))));
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00001921}
1922
1923
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001924LInstruction* LChunkBuilder::DoLoadElements(HLoadElements* instr) {
1925 LOperand* input = UseRegisterAtStart(instr->value());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001926 return DefineAsRegister(new LLoadElements(input));
1927}
1928
1929
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001930LInstruction* LChunkBuilder::DoLoadExternalArrayPointer(
1931 HLoadExternalArrayPointer* instr) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001932 LOperand* input = UseRegisterAtStart(instr->value());
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001933 return DefineAsRegister(new LLoadExternalArrayPointer(input));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001934}
1935
1936
1937LInstruction* LChunkBuilder::DoLoadKeyedFastElement(
1938 HLoadKeyedFastElement* instr) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001939 ASSERT(instr->representation().IsTagged());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001940 ASSERT(instr->key()->representation().IsInteger32());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001941 LOperand* obj = UseRegisterAtStart(instr->object());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001942 LOperand* key = UseRegisterAtStart(instr->key());
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001943 LLoadKeyedFastElement* result = new LLoadKeyedFastElement(obj, key);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001944 return AssignEnvironment(DefineSameAsFirst(result));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001945}
1946
1947
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001948LInstruction* LChunkBuilder::DoLoadKeyedSpecializedArrayElement(
1949 HLoadKeyedSpecializedArrayElement* instr) {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001950 ExternalArrayType array_type = instr->array_type();
1951 Representation representation(instr->representation());
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001952 ASSERT(
1953 (representation.IsInteger32() && (array_type != kExternalFloatArray &&
1954 array_type != kExternalDoubleArray)) ||
1955 (representation.IsDouble() && (array_type == kExternalFloatArray ||
1956 array_type == kExternalDoubleArray)));
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001957 ASSERT(instr->key()->representation().IsInteger32());
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001958 LOperand* external_pointer = UseRegister(instr->external_pointer());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001959 LOperand* key = UseRegisterOrConstant(instr->key());
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001960 LLoadKeyedSpecializedArrayElement* result =
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001961 new LLoadKeyedSpecializedArrayElement(external_pointer, key);
1962 LInstruction* load_instr = DefineAsRegister(result);
1963 // An unsigned int array load might overflow and cause a deopt, make sure it
1964 // has an environment.
1965 return (array_type == kExternalUnsignedIntArray) ?
1966 AssignEnvironment(load_instr) : load_instr;
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001967}
1968
1969
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001970LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
1971 LOperand* object = UseFixed(instr->object(), r1);
1972 LOperand* key = UseFixed(instr->key(), r0);
1973
1974 LInstruction* result =
1975 DefineFixed(new LLoadKeyedGeneric(object, key), r0);
1976 return MarkAsCall(result, instr);
1977}
1978
1979
1980LInstruction* LChunkBuilder::DoStoreKeyedFastElement(
1981 HStoreKeyedFastElement* instr) {
1982 bool needs_write_barrier = instr->NeedsWriteBarrier();
1983 ASSERT(instr->value()->representation().IsTagged());
1984 ASSERT(instr->object()->representation().IsTagged());
1985 ASSERT(instr->key()->representation().IsInteger32());
1986
1987 LOperand* obj = UseTempRegister(instr->object());
1988 LOperand* val = needs_write_barrier
1989 ? UseTempRegister(instr->value())
1990 : UseRegisterAtStart(instr->value());
1991 LOperand* key = needs_write_barrier
1992 ? UseTempRegister(instr->key())
1993 : UseRegisterOrConstantAtStart(instr->key());
1994
1995 return AssignEnvironment(new LStoreKeyedFastElement(obj, key, val));
1996}
1997
1998
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001999LInstruction* LChunkBuilder::DoStoreKeyedSpecializedArrayElement(
2000 HStoreKeyedSpecializedArrayElement* instr) {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00002001 Representation representation(instr->value()->representation());
2002 ExternalArrayType array_type = instr->array_type();
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00002003 ASSERT(
2004 (representation.IsInteger32() && (array_type != kExternalFloatArray &&
2005 array_type != kExternalDoubleArray)) ||
2006 (representation.IsDouble() && (array_type == kExternalFloatArray ||
2007 array_type == kExternalDoubleArray)));
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00002008 ASSERT(instr->external_pointer()->representation().IsExternal());
2009 ASSERT(instr->key()->representation().IsInteger32());
2010
2011 LOperand* external_pointer = UseRegister(instr->external_pointer());
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00002012 bool val_is_temp_register = array_type == kExternalPixelArray ||
2013 array_type == kExternalFloatArray;
2014 LOperand* val = val_is_temp_register
2015 ? UseTempRegister(instr->value())
2016 : UseRegister(instr->value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002017 LOperand* key = UseRegisterOrConstant(instr->key());
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00002018
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002019 return new LStoreKeyedSpecializedArrayElement(external_pointer,
2020 key,
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00002021 val);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002022}
2023
2024
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002025LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
2026 LOperand* obj = UseFixed(instr->object(), r2);
2027 LOperand* key = UseFixed(instr->key(), r1);
2028 LOperand* val = UseFixed(instr->value(), r0);
2029
2030 ASSERT(instr->object()->representation().IsTagged());
2031 ASSERT(instr->key()->representation().IsTagged());
2032 ASSERT(instr->value()->representation().IsTagged());
2033
2034 return MarkAsCall(new LStoreKeyedGeneric(obj, key, val), instr);
2035}
2036
2037
2038LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00002039 bool needs_write_barrier = instr->NeedsWriteBarrier();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002040
2041 LOperand* obj = needs_write_barrier
2042 ? UseTempRegister(instr->object())
2043 : UseRegisterAtStart(instr->object());
2044
2045 LOperand* val = needs_write_barrier
2046 ? UseTempRegister(instr->value())
2047 : UseRegister(instr->value());
2048
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002049 return new LStoreNamedField(obj, val);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002050}
2051
2052
2053LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
2054 LOperand* obj = UseFixed(instr->object(), r1);
2055 LOperand* val = UseFixed(instr->value(), r0);
2056
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002057 LInstruction* result = new LStoreNamedGeneric(obj, val);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002058 return MarkAsCall(result, instr);
2059}
2060
2061
danno@chromium.org160a7b02011-04-18 15:51:38 +00002062LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
2063 LOperand* left = UseRegisterAtStart(instr->left());
2064 LOperand* right = UseRegisterAtStart(instr->right());
2065 return MarkAsCall(DefineFixed(new LStringAdd(left, right), r0), instr);
2066}
2067
2068
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00002069LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
2070 LOperand* string = UseRegister(instr->string());
2071 LOperand* index = UseRegisterOrConstant(instr->index());
2072 LStringCharCodeAt* result = new LStringCharCodeAt(string, index);
2073 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
2074}
2075
2076
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002077LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2078 LOperand* char_code = UseRegister(instr->value());
2079 LStringCharFromCode* result = new LStringCharFromCode(char_code);
2080 return AssignPointerMap(DefineAsRegister(result));
2081}
2082
2083
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00002084LInstruction* LChunkBuilder::DoStringLength(HStringLength* instr) {
2085 LOperand* string = UseRegisterAtStart(instr->value());
2086 return DefineAsRegister(new LStringLength(string));
2087}
2088
2089
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002090LInstruction* LChunkBuilder::DoArrayLiteral(HArrayLiteral* instr) {
2091 return MarkAsCall(DefineFixed(new LArrayLiteral, r0), instr);
2092}
2093
2094
2095LInstruction* LChunkBuilder::DoObjectLiteral(HObjectLiteral* instr) {
2096 return MarkAsCall(DefineFixed(new LObjectLiteral, r0), instr);
2097}
2098
2099
2100LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2101 return MarkAsCall(DefineFixed(new LRegExpLiteral, r0), instr);
2102}
2103
2104
2105LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2106 return MarkAsCall(DefineFixed(new LFunctionLiteral, r0), instr);
2107}
2108
2109
2110LInstruction* LChunkBuilder::DoDeleteProperty(HDeleteProperty* instr) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002111 LOperand* object = UseFixed(instr->object(), r0);
2112 LOperand* key = UseFixed(instr->key(), r1);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00002113 LDeleteProperty* result = new LDeleteProperty(object, key);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002114 return MarkAsCall(DefineFixed(result, r0), instr);
2115}
2116
2117
2118LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2119 allocator_->MarkAsOsrEntry();
2120 current_block_->last_environment()->set_ast_id(instr->ast_id());
2121 return AssignEnvironment(new LOsrEntry);
2122}
2123
2124
2125LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2126 int spill_index = chunk()->GetParameterStackSlot(instr->index());
2127 return DefineAsSpilled(new LParameter, spill_index);
2128}
2129
2130
2131LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2132 int spill_index = chunk()->GetNextSpillIndex(false); // Not double-width.
2133 return DefineAsSpilled(new LUnknownOSRValue, spill_index);
2134}
2135
2136
2137LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2138 argument_count_ -= instr->argument_count();
2139 return MarkAsCall(DefineFixed(new LCallStub, r0), instr);
2140}
2141
2142
2143LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002144 // There are no real uses of the arguments object.
2145 // arguments.length and element access are supported directly on
2146 // stack arguments, and any real arguments object use causes a bailout.
2147 // So this value is never used.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002148 return NULL;
2149}
2150
2151
2152LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2153 LOperand* arguments = UseRegister(instr->arguments());
2154 LOperand* length = UseTempRegister(instr->length());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00002155 LOperand* index = UseRegister(instr->index());
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00002156 LAccessArgumentsAt* result = new LAccessArgumentsAt(arguments, length, index);
2157 return AssignEnvironment(DefineAsRegister(result));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002158}
2159
2160
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002161LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2162 LOperand* object = UseFixed(instr->value(), r0);
2163 LToFastProperties* result = new LToFastProperties(object);
2164 return MarkAsCall(DefineFixed(result, r0), instr);
2165}
2166
2167
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002168LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002169 LTypeof* result = new LTypeof(UseFixed(instr->value(), r0));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002170 return MarkAsCall(DefineFixed(result, r0), instr);
2171}
2172
2173
2174LInstruction* LChunkBuilder::DoTypeofIs(HTypeofIs* instr) {
2175 return DefineSameAsFirst(new LTypeofIs(UseRegister(instr->value())));
2176}
2177
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00002178
2179LInstruction* LChunkBuilder::DoIsConstructCall(HIsConstructCall* instr) {
2180 return DefineAsRegister(new LIsConstructCall());
2181}
2182
2183
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002184LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2185 HEnvironment* env = current_block_->last_environment();
2186 ASSERT(env != NULL);
2187
2188 env->set_ast_id(instr->ast_id());
2189
2190 env->Drop(instr->pop_count());
2191 for (int i = 0; i < instr->values()->length(); ++i) {
2192 HValue* value = instr->values()->at(i);
2193 if (instr->HasAssignedIndexAt(i)) {
2194 env->Bind(instr->GetAssignedIndexAt(i), value);
2195 } else {
2196 env->Push(value);
2197 }
2198 }
2199
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002200 // If there is an instruction pending deoptimization environment create a
2201 // lazy bailout instruction to capture the environment.
fschneider@chromium.org1df6b472011-01-26 08:23:03 +00002202 if (pending_deoptimization_ast_id_ == instr->ast_id()) {
2203 LInstruction* result = new LLazyBailout;
2204 result = AssignEnvironment(result);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002205 instruction_pending_deoptimization_environment_->
fschneider@chromium.org1df6b472011-01-26 08:23:03 +00002206 set_deoptimization_environment(result->environment());
2207 ClearInstructionPendingDeoptimizationEnvironment();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002208 return result;
2209 }
2210
2211 return NULL;
2212}
2213
2214
2215LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2216 return MarkAsCall(new LStackCheck, instr);
2217}
2218
2219
2220LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2221 HEnvironment* outer = current_block_->last_environment();
2222 HConstant* undefined = graph()->GetConstantUndefined();
2223 HEnvironment* inner = outer->CopyForInlining(instr->closure(),
2224 instr->function(),
lrn@chromium.org1c092762011-05-09 09:42:16 +00002225 HEnvironment::LITHIUM,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002226 undefined,
2227 instr->call_kind());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002228 current_block_->UpdateEnvironment(inner);
2229 chunk_->AddInlinedClosure(instr->closure());
2230 return NULL;
2231}
2232
2233
2234LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2235 HEnvironment* outer = current_block_->last_environment()->outer();
2236 current_block_->UpdateEnvironment(outer);
2237 return NULL;
2238}
2239
2240
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00002241LInstruction* LChunkBuilder::DoIn(HIn* instr) {
2242 LOperand* key = UseRegisterAtStart(instr->key());
2243 LOperand* object = UseRegisterAtStart(instr->object());
2244 LIn* result = new LIn(key, object);
2245 return MarkAsCall(DefineFixed(result, r0), instr);
2246}
2247
2248
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002249} } // namespace v8::internal