blob: be08cff84314b224488ea5ffaf76cc31b8dfe144 [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("= ");
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000114 for (int i = 0; i < inputs_.length(); i++) {
115 if (i > 0) stream->Add(" ");
116 inputs_[i]->PrintTo(stream);
117 }
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000118}
119
120
121template<int R, int I, int T>
122void LTemplateInstruction<R, I, T>::PrintOutputOperandTo(StringStream* stream) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000123 for (int i = 0; i < results_.length(); i++) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000124 if (i > 0) stream->Add(" ");
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000125 results_[i]->PrintTo(stream);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000126 }
127}
128
129
130void LLabel::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000131 LGap::PrintDataTo(stream);
132 LLabel* rep = replacement();
133 if (rep != NULL) {
134 stream->Add(" Dead block replaced with B%d", rep->block_id());
135 }
136}
137
138
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000139bool LGap::IsRedundant() const {
140 for (int i = 0; i < 4; i++) {
141 if (parallel_moves_[i] != NULL && !parallel_moves_[i]->IsRedundant()) {
142 return false;
143 }
144 }
145
146 return true;
147}
148
149
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000150void LGap::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000151 for (int i = 0; i < 4; i++) {
152 stream->Add("(");
153 if (parallel_moves_[i] != NULL) {
154 parallel_moves_[i]->PrintDataTo(stream);
155 }
156 stream->Add(") ");
157 }
158}
159
160
161const char* LArithmeticD::Mnemonic() const {
162 switch (op()) {
163 case Token::ADD: return "add-d";
164 case Token::SUB: return "sub-d";
165 case Token::MUL: return "mul-d";
166 case Token::DIV: return "div-d";
167 case Token::MOD: return "mod-d";
168 default:
169 UNREACHABLE();
170 return NULL;
171 }
172}
173
174
175const char* LArithmeticT::Mnemonic() const {
176 switch (op()) {
177 case Token::ADD: return "add-t";
178 case Token::SUB: return "sub-t";
179 case Token::MUL: return "mul-t";
180 case Token::MOD: return "mod-t";
181 case Token::DIV: return "div-t";
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000182 case Token::BIT_AND: return "bit-and-t";
183 case Token::BIT_OR: return "bit-or-t";
184 case Token::BIT_XOR: return "bit-xor-t";
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000185 case Token::SHL: return "shl-t";
186 case Token::SAR: return "sar-t";
187 case Token::SHR: return "shr-t";
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000188 default:
189 UNREACHABLE();
190 return NULL;
191 }
192}
193
194
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000195void LGoto::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000196 stream->Add("B%d", block_id());
197}
198
199
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000200void LBranch::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000201 stream->Add("B%d | B%d on ", true_block_id(), false_block_id());
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000202 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000203}
204
205
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000206void LCmpIDAndBranch::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000207 stream->Add("if ");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000208 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000209 stream->Add(" %s ", Token::String(op()));
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000210 InputAt(1)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000211 stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
212}
213
214
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000215void LIsNullAndBranch::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000216 stream->Add("if ");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000217 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000218 stream->Add(is_strict() ? " === null" : " == null");
219 stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
220}
221
222
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000223void LIsObjectAndBranch::PrintDataTo(StringStream* stream) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000224 stream->Add("if is_object(");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000225 InputAt(0)->PrintTo(stream);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000226 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
227}
228
229
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000230void LIsSmiAndBranch::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000231 stream->Add("if is_smi(");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000232 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000233 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
234}
235
236
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000237void LIsUndetectableAndBranch::PrintDataTo(StringStream* stream) {
238 stream->Add("if is_undetectable(");
239 InputAt(0)->PrintTo(stream);
240 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
241}
242
243
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000244void LHasInstanceTypeAndBranch::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000245 stream->Add("if has_instance_type(");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000246 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000247 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
248}
249
250
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000251void LHasCachedArrayIndexAndBranch::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000252 stream->Add("if has_cached_array_index(");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000253 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000254 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
255}
256
257
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000258void LClassOfTestAndBranch::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000259 stream->Add("if class_of_test(");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000260 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000261 stream->Add(", \"%o\") then B%d else B%d",
262 *hydrogen()->class_name(),
263 true_block_id(),
264 false_block_id());
265}
266
267
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000268void LTypeofIs::PrintDataTo(StringStream* stream) {
269 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000270 stream->Add(" == \"%s\"", *hydrogen()->type_literal()->ToCString());
271}
272
273
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000274void LTypeofIsAndBranch::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000275 stream->Add("if typeof ");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000276 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000277 stream->Add(" == \"%s\" then B%d else B%d",
278 *hydrogen()->type_literal()->ToCString(),
279 true_block_id(), false_block_id());
280}
281
282
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000283void LCallConstantFunction::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000284 stream->Add("#%d / ", arity());
285}
286
287
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000288void LUnaryMathOperation::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000289 stream->Add("/%s ", hydrogen()->OpName());
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000290 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000291}
292
293
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000294void LLoadContextSlot::PrintDataTo(StringStream* stream) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000295 InputAt(0)->PrintTo(stream);
296 stream->Add("[%d]", slot_index());
297}
298
299
300void LStoreContextSlot::PrintDataTo(StringStream* stream) {
301 InputAt(0)->PrintTo(stream);
302 stream->Add("[%d] <- ", slot_index());
303 InputAt(1)->PrintTo(stream);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000304}
305
306
danno@chromium.org160a7b02011-04-18 15:51:38 +0000307void LInvokeFunction::PrintDataTo(StringStream* stream) {
308 stream->Add("= ");
309 InputAt(0)->PrintTo(stream);
310 stream->Add(" #%d / ", arity());
311}
312
313
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000314void LCallKeyed::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000315 stream->Add("[r2] #%d / ", arity());
316}
317
318
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000319void LCallNamed::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000320 SmartPointer<char> name_string = name()->ToCString();
321 stream->Add("%s #%d / ", *name_string, arity());
322}
323
324
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000325void LCallGlobal::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000326 SmartPointer<char> name_string = name()->ToCString();
327 stream->Add("%s #%d / ", *name_string, arity());
328}
329
330
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000331void LCallKnownGlobal::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000332 stream->Add("#%d / ", arity());
333}
334
335
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000336void LCallNew::PrintDataTo(StringStream* stream) {
337 stream->Add("= ");
338 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000339 stream->Add(" #%d / ", arity());
340}
341
342
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000343void LClassOfTest::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000344 stream->Add("= class_of_test(");
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000345 InputAt(0)->PrintTo(stream);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000346 stream->Add(", \"%o\")", *hydrogen()->class_name());
347}
348
349
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000350void LAccessArgumentsAt::PrintDataTo(StringStream* stream) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000351 arguments()->PrintTo(stream);
352
353 stream->Add(" length ");
354 length()->PrintTo(stream);
355
356 stream->Add(" index ");
357 index()->PrintTo(stream);
358}
359
360
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000361void LStoreNamedField::PrintDataTo(StringStream* stream) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000362 object()->PrintTo(stream);
363 stream->Add(".");
364 stream->Add(*String::cast(*name())->ToCString());
365 stream->Add(" <- ");
366 value()->PrintTo(stream);
367}
368
369
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000370void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
371 object()->PrintTo(stream);
372 stream->Add(".");
373 stream->Add(*String::cast(*name())->ToCString());
374 stream->Add(" <- ");
375 value()->PrintTo(stream);
376}
377
378
379void LStoreKeyedFastElement::PrintDataTo(StringStream* stream) {
380 object()->PrintTo(stream);
381 stream->Add("[");
382 key()->PrintTo(stream);
383 stream->Add("] <- ");
384 value()->PrintTo(stream);
385}
386
387
388void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000389 object()->PrintTo(stream);
390 stream->Add("[");
391 key()->PrintTo(stream);
392 stream->Add("] <- ");
393 value()->PrintTo(stream);
394}
395
396
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000397LChunk::LChunk(CompilationInfo* info, HGraph* graph)
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000398 : spill_slot_count_(0),
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000399 info_(info),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000400 graph_(graph),
401 instructions_(32),
402 pointer_maps_(8),
403 inlined_closures_(1) {
404}
405
406
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000407int LChunk::GetNextSpillIndex(bool is_double) {
408 // Skip a slot if for a double-width slot.
409 if (is_double) spill_slot_count_++;
410 return spill_slot_count_++;
411}
412
413
414LOperand* LChunk::GetNextSpillSlot(bool is_double) {
415 int index = GetNextSpillIndex(is_double);
416 if (is_double) {
417 return LDoubleStackSlot::Create(index);
418 } else {
419 return LStackSlot::Create(index);
420 }
421}
422
423
424void LChunk::MarkEmptyBlocks() {
425 HPhase phase("Mark empty blocks", this);
426 for (int i = 0; i < graph()->blocks()->length(); ++i) {
427 HBasicBlock* block = graph()->blocks()->at(i);
428 int first = block->first_instruction_index();
429 int last = block->last_instruction_index();
430 LInstruction* first_instr = instructions()->at(first);
431 LInstruction* last_instr = instructions()->at(last);
432
433 LLabel* label = LLabel::cast(first_instr);
434 if (last_instr->IsGoto()) {
435 LGoto* goto_instr = LGoto::cast(last_instr);
436 if (!goto_instr->include_stack_check() &&
437 label->IsRedundant() &&
438 !label->is_loop_header()) {
439 bool can_eliminate = true;
440 for (int i = first + 1; i < last && can_eliminate; ++i) {
441 LInstruction* cur = instructions()->at(i);
442 if (cur->IsGap()) {
443 LGap* gap = LGap::cast(cur);
444 if (!gap->IsRedundant()) {
445 can_eliminate = false;
446 }
447 } else {
448 can_eliminate = false;
449 }
450 }
451
452 if (can_eliminate) {
453 label->set_replacement(GetLabel(goto_instr->block_id()));
454 }
455 }
456 }
457 }
458}
459
460
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000461void LChunk::AddInstruction(LInstruction* instr, HBasicBlock* block) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000462 LInstructionGap* gap = new LInstructionGap(block);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000463 int index = -1;
464 if (instr->IsControl()) {
465 instructions_.Add(gap);
466 index = instructions_.length();
467 instructions_.Add(instr);
468 } else {
469 index = instructions_.length();
470 instructions_.Add(instr);
471 instructions_.Add(gap);
472 }
473 if (instr->HasPointerMap()) {
474 pointer_maps_.Add(instr->pointer_map());
475 instr->pointer_map()->set_lithium_position(index);
476 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000477}
478
479
480LConstantOperand* LChunk::DefineConstantOperand(HConstant* constant) {
481 return LConstantOperand::Create(constant->id());
482}
483
484
485int LChunk::GetParameterStackSlot(int index) const {
486 // The receiver is at index 0, the first parameter at index 1, so we
487 // shift all parameter indexes down by the number of parameters, and
488 // make sure they end up negative so they are distinguishable from
489 // spill slots.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000490 int result = index - info()->scope()->num_parameters() - 1;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000491 ASSERT(result < 0);
492 return result;
493}
494
495// A parameter relative to ebp in the arguments stub.
496int LChunk::ParameterAt(int index) {
497 ASSERT(-1 <= index); // -1 is the receiver.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000498 return (1 + info()->scope()->num_parameters() - index) *
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000499 kPointerSize;
500}
501
502
503LGap* LChunk::GetGapAt(int index) const {
504 return LGap::cast(instructions_[index]);
505}
506
507
508bool LChunk::IsGapAt(int index) const {
509 return instructions_[index]->IsGap();
510}
511
512
513int LChunk::NearestGapPos(int index) const {
514 while (!IsGapAt(index)) index--;
515 return index;
516}
517
518
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000519void LChunk::AddGapMove(int index, LOperand* from, LOperand* to) {
520 GetGapAt(index)->GetOrCreateParallelMove(LGap::START)->AddMove(from, to);
521}
522
523
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000524Handle<Object> LChunk::LookupLiteral(LConstantOperand* operand) const {
525 return HConstant::cast(graph_->LookupValue(operand->index()))->handle();
526}
527
528
529Representation LChunk::LookupLiteralRepresentation(
530 LConstantOperand* operand) const {
531 return graph_->LookupValue(operand->index())->representation();
532}
533
534
535LChunk* LChunkBuilder::Build() {
536 ASSERT(is_unused());
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000537 chunk_ = new LChunk(info(), graph());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000538 HPhase phase("Building chunk", chunk_);
539 status_ = BUILDING;
540 const ZoneList<HBasicBlock*>* blocks = graph()->blocks();
541 for (int i = 0; i < blocks->length(); i++) {
542 HBasicBlock* next = NULL;
543 if (i < blocks->length() - 1) next = blocks->at(i + 1);
544 DoBasicBlock(blocks->at(i), next);
545 if (is_aborted()) return NULL;
546 }
547 status_ = DONE;
548 return chunk_;
549}
550
551
552void LChunkBuilder::Abort(const char* format, ...) {
553 if (FLAG_trace_bailout) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000554 SmartPointer<char> name(info()->shared_info()->DebugName()->ToCString());
555 PrintF("Aborting LChunk building in @\"%s\": ", *name);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000556 va_list arguments;
557 va_start(arguments, format);
558 OS::VPrint(format, arguments);
559 va_end(arguments);
560 PrintF("\n");
561 }
562 status_ = ABORTED;
563}
564
565
566LRegister* LChunkBuilder::ToOperand(Register reg) {
567 return LRegister::Create(Register::ToAllocationIndex(reg));
568}
569
570
571LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
572 return new LUnallocated(LUnallocated::FIXED_REGISTER,
573 Register::ToAllocationIndex(reg));
574}
575
576
577LUnallocated* LChunkBuilder::ToUnallocated(DoubleRegister reg) {
578 return new LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
579 DoubleRegister::ToAllocationIndex(reg));
580}
581
582
583LOperand* LChunkBuilder::UseFixed(HValue* value, Register fixed_register) {
584 return Use(value, ToUnallocated(fixed_register));
585}
586
587
588LOperand* LChunkBuilder::UseFixedDouble(HValue* value, DoubleRegister reg) {
589 return Use(value, ToUnallocated(reg));
590}
591
592
593LOperand* LChunkBuilder::UseRegister(HValue* value) {
594 return Use(value, new LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
595}
596
597
598LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
599 return Use(value,
600 new LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
601 LUnallocated::USED_AT_START));
602}
603
604
605LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
606 return Use(value, new LUnallocated(LUnallocated::WRITABLE_REGISTER));
607}
608
609
610LOperand* LChunkBuilder::Use(HValue* value) {
611 return Use(value, new LUnallocated(LUnallocated::NONE));
612}
613
614
615LOperand* LChunkBuilder::UseAtStart(HValue* value) {
616 return Use(value, new LUnallocated(LUnallocated::NONE,
617 LUnallocated::USED_AT_START));
618}
619
620
621LOperand* LChunkBuilder::UseOrConstant(HValue* value) {
622 return value->IsConstant()
623 ? chunk_->DefineConstantOperand(HConstant::cast(value))
624 : Use(value);
625}
626
627
628LOperand* LChunkBuilder::UseOrConstantAtStart(HValue* value) {
629 return value->IsConstant()
630 ? chunk_->DefineConstantOperand(HConstant::cast(value))
631 : UseAtStart(value);
632}
633
634
635LOperand* LChunkBuilder::UseRegisterOrConstant(HValue* value) {
636 return value->IsConstant()
637 ? chunk_->DefineConstantOperand(HConstant::cast(value))
638 : UseRegister(value);
639}
640
641
642LOperand* LChunkBuilder::UseRegisterOrConstantAtStart(HValue* value) {
643 return value->IsConstant()
644 ? chunk_->DefineConstantOperand(HConstant::cast(value))
645 : UseRegisterAtStart(value);
646}
647
648
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000649LOperand* LChunkBuilder::UseAny(HValue* value) {
650 return value->IsConstant()
651 ? chunk_->DefineConstantOperand(HConstant::cast(value))
652 : Use(value, new LUnallocated(LUnallocated::ANY));
653}
654
655
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000656LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
657 if (value->EmitAtUses()) {
658 HInstruction* instr = HInstruction::cast(value);
659 VisitInstruction(instr);
660 }
661 allocator_->RecordUse(value, operand);
662 return operand;
663}
664
665
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000666template<int I, int T>
667LInstruction* LChunkBuilder::Define(LTemplateInstruction<1, I, T>* instr,
668 LUnallocated* result) {
669 allocator_->RecordDefinition(current_instruction_, result);
670 instr->set_result(result);
671 return instr;
672}
673
674
675template<int I, int T>
676LInstruction* LChunkBuilder::Define(LTemplateInstruction<1, I, T>* instr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000677 return Define(instr, new LUnallocated(LUnallocated::NONE));
678}
679
680
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000681template<int I, int T>
682LInstruction* LChunkBuilder::DefineAsRegister(
683 LTemplateInstruction<1, I, T>* instr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000684 return Define(instr, new LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
685}
686
687
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000688template<int I, int T>
689LInstruction* LChunkBuilder::DefineAsSpilled(
690 LTemplateInstruction<1, I, T>* instr, int index) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000691 return Define(instr, new LUnallocated(LUnallocated::FIXED_SLOT, index));
692}
693
694
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000695template<int I, int T>
696LInstruction* LChunkBuilder::DefineSameAsFirst(
697 LTemplateInstruction<1, I, T>* instr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000698 return Define(instr, new LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
699}
700
701
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000702template<int I, int T>
703LInstruction* LChunkBuilder::DefineFixed(
704 LTemplateInstruction<1, I, T>* instr, Register reg) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000705 return Define(instr, ToUnallocated(reg));
706}
707
708
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000709template<int I, int T>
710LInstruction* LChunkBuilder::DefineFixedDouble(
711 LTemplateInstruction<1, I, T>* instr, DoubleRegister reg) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000712 return Define(instr, ToUnallocated(reg));
713}
714
715
716LInstruction* LChunkBuilder::AssignEnvironment(LInstruction* instr) {
717 HEnvironment* hydrogen_env = current_block_->last_environment();
718 instr->set_environment(CreateEnvironment(hydrogen_env));
719 return instr;
720}
721
722
fschneider@chromium.org1df6b472011-01-26 08:23:03 +0000723LInstruction* LChunkBuilder::SetInstructionPendingDeoptimizationEnvironment(
724 LInstruction* instr, int ast_id) {
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000725 ASSERT(instruction_pending_deoptimization_environment_ == NULL);
fschneider@chromium.org1df6b472011-01-26 08:23:03 +0000726 ASSERT(pending_deoptimization_ast_id_ == AstNode::kNoNumber);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000727 instruction_pending_deoptimization_environment_ = instr;
fschneider@chromium.org1df6b472011-01-26 08:23:03 +0000728 pending_deoptimization_ast_id_ = ast_id;
729 return instr;
730}
731
732
733void LChunkBuilder::ClearInstructionPendingDeoptimizationEnvironment() {
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000734 instruction_pending_deoptimization_environment_ = NULL;
fschneider@chromium.org1df6b472011-01-26 08:23:03 +0000735 pending_deoptimization_ast_id_ = AstNode::kNoNumber;
736}
737
738
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000739LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
740 HInstruction* hinstr,
741 CanDeoptimize can_deoptimize) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000742#ifdef DEBUG
743 instr->VerifyCall();
744#endif
745 instr->MarkAsCall();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000746 instr = AssignPointerMap(instr);
747
748 if (hinstr->HasSideEffects()) {
749 ASSERT(hinstr->next()->IsSimulate());
750 HSimulate* sim = HSimulate::cast(hinstr->next());
fschneider@chromium.org1df6b472011-01-26 08:23:03 +0000751 instr = SetInstructionPendingDeoptimizationEnvironment(
752 instr, sim->ast_id());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000753 }
754
755 // If instruction does not have side-effects lazy deoptimization
756 // after the call will try to deoptimize to the point before the call.
757 // Thus we still need to attach environment to this call even if
758 // call sequence can not deoptimize eagerly.
759 bool needs_environment =
760 (can_deoptimize == CAN_DEOPTIMIZE_EAGERLY) || !hinstr->HasSideEffects();
761 if (needs_environment && !instr->HasEnvironment()) {
762 instr = AssignEnvironment(instr);
763 }
764
765 return instr;
766}
767
768
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000769LInstruction* LChunkBuilder::MarkAsSaveDoubles(LInstruction* instr) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000770 instr->MarkAsSaveDoubles();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000771 return instr;
772}
773
774
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000775LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
776 ASSERT(!instr->HasPointerMap());
777 instr->set_pointer_map(new LPointerMap(position_));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000778 return instr;
779}
780
781
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000782LUnallocated* LChunkBuilder::TempRegister() {
783 LUnallocated* operand = new LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
784 allocator_->RecordTemporary(operand);
785 return operand;
786}
787
788
789LOperand* LChunkBuilder::FixedTemp(Register reg) {
790 LUnallocated* operand = ToUnallocated(reg);
791 allocator_->RecordTemporary(operand);
792 return operand;
793}
794
795
796LOperand* LChunkBuilder::FixedTemp(DoubleRegister reg) {
797 LUnallocated* operand = ToUnallocated(reg);
798 allocator_->RecordTemporary(operand);
799 return operand;
800}
801
802
803LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
804 return new LLabel(instr->block());
805}
806
807
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000808LInstruction* LChunkBuilder::DoSoftDeoptimize(HSoftDeoptimize* instr) {
809 return AssignEnvironment(new LDeoptimize);
810}
811
812
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000813LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
814 return AssignEnvironment(new LDeoptimize);
815}
816
817
818LInstruction* LChunkBuilder::DoBit(Token::Value op,
819 HBitwiseBinaryOperation* instr) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000820 if (instr->representation().IsInteger32()) {
821 ASSERT(instr->left()->representation().IsInteger32());
822 ASSERT(instr->right()->representation().IsInteger32());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000823
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000824 LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
825 LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
826 return DefineSameAsFirst(new LBitI(op, left, right));
827 } else {
828 ASSERT(instr->representation().IsTagged());
829 ASSERT(instr->left()->representation().IsTagged());
830 ASSERT(instr->right()->representation().IsTagged());
831
832 LOperand* left = UseFixed(instr->left(), r1);
833 LOperand* right = UseFixed(instr->right(), r0);
834 LArithmeticT* result = new LArithmeticT(op, left, right);
835 return MarkAsCall(DefineFixed(result, r0), instr);
836 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000837}
838
839
840LInstruction* LChunkBuilder::DoShift(Token::Value op,
841 HBitwiseBinaryOperation* instr) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000842 if (instr->representation().IsTagged()) {
843 ASSERT(instr->left()->representation().IsTagged());
844 ASSERT(instr->right()->representation().IsTagged());
845
846 LOperand* left = UseFixed(instr->left(), r1);
847 LOperand* right = UseFixed(instr->right(), r0);
848 LArithmeticT* result = new LArithmeticT(op, left, right);
849 return MarkAsCall(DefineFixed(result, r0), instr);
850 }
851
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000852 ASSERT(instr->representation().IsInteger32());
853 ASSERT(instr->OperandAt(0)->representation().IsInteger32());
854 ASSERT(instr->OperandAt(1)->representation().IsInteger32());
855 LOperand* left = UseRegisterAtStart(instr->OperandAt(0));
856
857 HValue* right_value = instr->OperandAt(1);
858 LOperand* right = NULL;
859 int constant_value = 0;
860 if (right_value->IsConstant()) {
861 HConstant* constant = HConstant::cast(right_value);
862 right = chunk_->DefineConstantOperand(constant);
863 constant_value = constant->Integer32Value() & 0x1f;
864 } else {
865 right = UseRegister(right_value);
866 }
867
868 // Shift operations can only deoptimize if we do a logical shift
869 // by 0 and the result cannot be truncated to int32.
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000870 bool may_deopt = (op == Token::SHR && constant_value == 0);
871 bool does_deopt = false;
872 if (may_deopt) {
873 for (HUseIterator it(instr->uses()); !it.Done(); it.Advance()) {
874 if (!it.value()->CheckFlag(HValue::kTruncatingToInt32)) {
875 does_deopt = true;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000876 break;
877 }
878 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000879 }
880
881 LInstruction* result =
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000882 DefineSameAsFirst(new LShiftI(op, left, right, does_deopt));
883 return does_deopt ? AssignEnvironment(result) : result;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000884}
885
886
887LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
888 HArithmeticBinaryOperation* instr) {
889 ASSERT(instr->representation().IsDouble());
890 ASSERT(instr->left()->representation().IsDouble());
891 ASSERT(instr->right()->representation().IsDouble());
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000892 ASSERT(op != Token::MOD);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000893 LOperand* left = UseRegisterAtStart(instr->left());
894 LOperand* right = UseRegisterAtStart(instr->right());
895 LArithmeticD* result = new LArithmeticD(op, left, right);
896 return DefineSameAsFirst(result);
897}
898
899
900LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
901 HArithmeticBinaryOperation* instr) {
902 ASSERT(op == Token::ADD ||
903 op == Token::DIV ||
904 op == Token::MOD ||
905 op == Token::MUL ||
906 op == Token::SUB);
907 HValue* left = instr->left();
908 HValue* right = instr->right();
909 ASSERT(left->representation().IsTagged());
910 ASSERT(right->representation().IsTagged());
911 LOperand* left_operand = UseFixed(left, r1);
912 LOperand* right_operand = UseFixed(right, r0);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000913 LArithmeticT* result = new LArithmeticT(op, left_operand, right_operand);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000914 return MarkAsCall(DefineFixed(result, r0), instr);
915}
916
ager@chromium.org378b34e2011-01-28 08:04:38 +0000917
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000918void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
919 ASSERT(is_building());
920 current_block_ = block;
921 next_block_ = next_block;
922 if (block->IsStartBlock()) {
923 block->UpdateEnvironment(graph_->start_environment());
924 argument_count_ = 0;
925 } else if (block->predecessors()->length() == 1) {
926 // We have a single predecessor => copy environment and outgoing
927 // argument count from the predecessor.
928 ASSERT(block->phis()->length() == 0);
929 HBasicBlock* pred = block->predecessors()->at(0);
930 HEnvironment* last_environment = pred->last_environment();
931 ASSERT(last_environment != NULL);
932 // Only copy the environment, if it is later used again.
933 if (pred->end()->SecondSuccessor() == NULL) {
934 ASSERT(pred->end()->FirstSuccessor() == block);
935 } else {
936 if (pred->end()->FirstSuccessor()->block_id() > block->block_id() ||
937 pred->end()->SecondSuccessor()->block_id() > block->block_id()) {
938 last_environment = last_environment->Copy();
939 }
940 }
941 block->UpdateEnvironment(last_environment);
942 ASSERT(pred->argument_count() >= 0);
943 argument_count_ = pred->argument_count();
944 } else {
945 // We are at a state join => process phis.
946 HBasicBlock* pred = block->predecessors()->at(0);
947 // No need to copy the environment, it cannot be used later.
948 HEnvironment* last_environment = pred->last_environment();
949 for (int i = 0; i < block->phis()->length(); ++i) {
950 HPhi* phi = block->phis()->at(i);
951 last_environment->SetValueAt(phi->merged_index(), phi);
952 }
953 for (int i = 0; i < block->deleted_phis()->length(); ++i) {
954 last_environment->SetValueAt(block->deleted_phis()->at(i),
955 graph_->GetConstantUndefined());
956 }
957 block->UpdateEnvironment(last_environment);
958 // Pick up the outgoing argument count of one of the predecessors.
959 argument_count_ = pred->argument_count();
960 }
961 HInstruction* current = block->first();
962 int start = chunk_->instructions()->length();
963 while (current != NULL && !is_aborted()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000964 // Code for constants in registers is generated lazily.
965 if (!current->EmitAtUses()) {
966 VisitInstruction(current);
967 }
968 current = current->next();
969 }
970 int end = chunk_->instructions()->length() - 1;
971 if (end >= start) {
972 block->set_first_instruction_index(start);
973 block->set_last_instruction_index(end);
974 }
975 block->set_argument_count(argument_count_);
976 next_block_ = NULL;
977 current_block_ = NULL;
978}
979
980
981void LChunkBuilder::VisitInstruction(HInstruction* current) {
982 HInstruction* old_current = current_instruction_;
983 current_instruction_ = current;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000984 if (current->has_position()) position_ = current->position();
985 LInstruction* instr = current->CompileToLithium(this);
986
987 if (instr != NULL) {
988 if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
989 instr = AssignPointerMap(instr);
990 }
991 if (FLAG_stress_environments && !instr->HasEnvironment()) {
992 instr = AssignEnvironment(instr);
993 }
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000994 if (current->IsTest() && !instr->IsGoto()) {
995 ASSERT(instr->IsControl());
996 HTest* test = HTest::cast(current);
997 instr->set_hydrogen_value(test->value());
998 HBasicBlock* first = test->FirstSuccessor();
999 HBasicBlock* second = test->SecondSuccessor();
1000 ASSERT(first != NULL && second != NULL);
1001 instr->SetBranchTargets(first->block_id(), second->block_id());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001002 } else {
1003 instr->set_hydrogen_value(current);
1004 }
1005
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001006 chunk_->AddInstruction(instr, current_block_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001007 }
1008 current_instruction_ = old_current;
1009}
1010
1011
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001012LEnvironment* LChunkBuilder::CreateEnvironment(HEnvironment* hydrogen_env) {
1013 if (hydrogen_env == NULL) return NULL;
1014
1015 LEnvironment* outer = CreateEnvironment(hydrogen_env->outer());
1016 int ast_id = hydrogen_env->ast_id();
1017 ASSERT(ast_id != AstNode::kNoNumber);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001018 int value_count = hydrogen_env->length();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001019 LEnvironment* result = new LEnvironment(hydrogen_env->closure(),
1020 ast_id,
1021 hydrogen_env->parameter_count(),
1022 argument_count_,
1023 value_count,
1024 outer);
1025 int argument_index = 0;
1026 for (int i = 0; i < value_count; ++i) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001027 if (hydrogen_env->is_special_index(i)) continue;
1028
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001029 HValue* value = hydrogen_env->values()->at(i);
1030 LOperand* op = NULL;
1031 if (value->IsArgumentsObject()) {
1032 op = NULL;
1033 } else if (value->IsPushArgument()) {
1034 op = new LArgument(argument_index++);
1035 } else {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001036 op = UseAny(value);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001037 }
1038 result->AddValue(op, value->representation());
1039 }
1040
1041 return result;
1042}
1043
1044
1045LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
1046 LInstruction* result = new LGoto(instr->FirstSuccessor()->block_id(),
1047 instr->include_stack_check());
1048 if (instr->include_stack_check()) result = AssignPointerMap(result);
1049 return result;
1050}
1051
1052
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001053LInstruction* LChunkBuilder::DoTest(HTest* instr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001054 HValue* v = instr->value();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001055 if (!v->EmitAtUses()) return new LBranch(UseRegisterAtStart(v));
1056 ASSERT(!v->HasSideEffects());
1057 if (v->IsClassOfTest()) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001058 HClassOfTest* compare = HClassOfTest::cast(v);
1059 ASSERT(compare->value()->representation().IsTagged());
1060 return new LClassOfTestAndBranch(UseTempRegister(compare->value()),
1061 TempRegister());
1062 } else if (v->IsCompare()) {
1063 HCompare* compare = HCompare::cast(v);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001064 HValue* left = compare->left();
1065 HValue* right = compare->right();
1066 Representation r = compare->GetInputRepresentation();
1067 if (r.IsInteger32()) {
1068 ASSERT(left->representation().IsInteger32());
1069 ASSERT(right->representation().IsInteger32());
1070 return new LCmpIDAndBranch(UseRegisterAtStart(left),
1071 UseRegisterAtStart(right));
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001072 } else {
1073 ASSERT(r.IsDouble());
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001074 ASSERT(left->representation().IsDouble());
1075 ASSERT(right->representation().IsDouble());
1076 return new LCmpIDAndBranch(UseRegisterAtStart(left),
1077 UseRegisterAtStart(right));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001078 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001079 } else if (v->IsIsSmi()) {
1080 HIsSmi* compare = HIsSmi::cast(v);
1081 ASSERT(compare->value()->representation().IsTagged());
1082 return new LIsSmiAndBranch(Use(compare->value()));
1083 } else if (v->IsIsUndetectable()) {
1084 HIsUndetectable* compare = HIsUndetectable::cast(v);
1085 ASSERT(compare->value()->representation().IsTagged());
1086 return new LIsUndetectableAndBranch(UseRegisterAtStart(compare->value()),
1087 TempRegister());
1088 } else if (v->IsHasInstanceType()) {
1089 HHasInstanceType* compare = HHasInstanceType::cast(v);
1090 ASSERT(compare->value()->representation().IsTagged());
1091 return new LHasInstanceTypeAndBranch(UseRegisterAtStart(compare->value()));
1092 } else if (v->IsHasCachedArrayIndex()) {
1093 HHasCachedArrayIndex* compare = HHasCachedArrayIndex::cast(v);
1094 ASSERT(compare->value()->representation().IsTagged());
1095 return new LHasCachedArrayIndexAndBranch(
1096 UseRegisterAtStart(compare->value()));
1097 } else if (v->IsIsNull()) {
1098 HIsNull* compare = HIsNull::cast(v);
1099 ASSERT(compare->value()->representation().IsTagged());
1100 return new LIsNullAndBranch(UseRegisterAtStart(compare->value()));
1101 } else if (v->IsIsObject()) {
1102 HIsObject* compare = HIsObject::cast(v);
1103 ASSERT(compare->value()->representation().IsTagged());
1104 LOperand* temp = TempRegister();
1105 return new LIsObjectAndBranch(UseRegisterAtStart(compare->value()), temp);
1106 } else if (v->IsCompareJSObjectEq()) {
1107 HCompareJSObjectEq* compare = HCompareJSObjectEq::cast(v);
1108 return new LCmpJSObjectEqAndBranch(UseRegisterAtStart(compare->left()),
1109 UseRegisterAtStart(compare->right()));
1110 } else if (v->IsCompareSymbolEq()) {
1111 HCompareSymbolEq* compare = HCompareSymbolEq::cast(v);
1112 return new LCmpSymbolEqAndBranch(UseRegisterAtStart(compare->left()),
1113 UseRegisterAtStart(compare->right()));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001114 } else if (v->IsTypeofIs()) {
1115 HTypeofIs* typeof_is = HTypeofIs::cast(v);
1116 return new LTypeofIsAndBranch(UseTempRegister(typeof_is->value()));
1117 } else if (v->IsIsConstructCall()) {
1118 return new LIsConstructCallAndBranch(TempRegister());
1119 } else if (v->IsConstant()) {
1120 HBasicBlock* successor = HConstant::cast(v)->ToBoolean()
1121 ? instr->FirstSuccessor()
1122 : instr->SecondSuccessor();
1123 return new LGoto(successor->block_id());
1124 } else {
1125 Abort("Undefined compare before branch");
1126 return NULL;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001127 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001128}
1129
1130
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001131
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001132LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001133 ASSERT(instr->value()->representation().IsTagged());
1134 LOperand* value = UseRegisterAtStart(instr->value());
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001135 LOperand* temp = TempRegister();
1136 return new LCmpMapAndBranch(value, temp);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001137}
1138
1139
1140LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* length) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001141 return DefineAsRegister(new LArgumentsLength(UseRegister(length->value())));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001142}
1143
1144
1145LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
1146 return DefineAsRegister(new LArgumentsElements);
1147}
1148
1149
1150LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001151 LInstanceOf* result =
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00001152 new LInstanceOf(UseFixed(instr->left(), r0),
1153 UseFixed(instr->right(), r1));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001154 return MarkAsCall(DefineFixed(result, r0), instr);
1155}
1156
1157
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001158LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
1159 HInstanceOfKnownGlobal* instr) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001160 LInstanceOfKnownGlobal* result =
1161 new LInstanceOfKnownGlobal(UseFixed(instr->value(), r0), FixedTemp(r4));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001162 return MarkAsCall(DefineFixed(result, r0), instr);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001163}
1164
1165
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001166LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
1167 LOperand* function = UseFixed(instr->function(), r1);
1168 LOperand* receiver = UseFixed(instr->receiver(), r0);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001169 LOperand* length = UseFixed(instr->length(), r2);
1170 LOperand* elements = UseFixed(instr->elements(), r3);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001171 LApplyArguments* result = new LApplyArguments(function,
1172 receiver,
1173 length,
1174 elements);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001175 return MarkAsCall(DefineFixed(result, r0), instr, CAN_DEOPTIMIZE_EAGERLY);
1176}
1177
1178
1179LInstruction* LChunkBuilder::DoPushArgument(HPushArgument* instr) {
1180 ++argument_count_;
1181 LOperand* argument = Use(instr->argument());
1182 return new LPushArgument(argument);
1183}
1184
1185
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001186LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1187 return instr->HasNoUses() ? NULL : DefineAsRegister(new LThisFunction);
1188}
1189
1190
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001191LInstruction* LChunkBuilder::DoContext(HContext* instr) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001192 return instr->HasNoUses() ? NULL : DefineAsRegister(new LContext);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001193}
1194
1195
1196LInstruction* LChunkBuilder::DoOuterContext(HOuterContext* instr) {
1197 LOperand* context = UseRegisterAtStart(instr->value());
1198 return DefineAsRegister(new LOuterContext(context));
1199}
1200
1201
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001202LInstruction* LChunkBuilder::DoGlobalObject(HGlobalObject* instr) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001203 LOperand* context = UseRegisterAtStart(instr->value());
1204 return DefineAsRegister(new LGlobalObject(context));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001205}
1206
1207
1208LInstruction* LChunkBuilder::DoGlobalReceiver(HGlobalReceiver* instr) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001209 LOperand* global_object = UseRegisterAtStart(instr->value());
1210 return DefineAsRegister(new LGlobalReceiver(global_object));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001211}
1212
1213
1214LInstruction* LChunkBuilder::DoCallConstantFunction(
1215 HCallConstantFunction* instr) {
1216 argument_count_ -= instr->argument_count();
1217 return MarkAsCall(DefineFixed(new LCallConstantFunction, r0), instr);
1218}
1219
1220
danno@chromium.org160a7b02011-04-18 15:51:38 +00001221LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1222 LOperand* function = UseFixed(instr->function(), r1);
1223 argument_count_ -= instr->argument_count();
1224 LInvokeFunction* result = new LInvokeFunction(function);
1225 return MarkAsCall(DefineFixed(result, r0), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1226}
1227
1228
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001229LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001230 BuiltinFunctionId op = instr->op();
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001231 if (op == kMathLog || op == kMathSin || op == kMathCos) {
1232 LOperand* input = UseFixedDouble(instr->value(), d2);
1233 LUnaryMathOperation* result = new LUnaryMathOperation(input, NULL);
1234 return MarkAsCall(DefineFixedDouble(result, d2), instr);
1235 } else {
1236 LOperand* input = UseRegisterAtStart(instr->value());
1237 LOperand* temp = (op == kMathFloor) ? TempRegister() : NULL;
1238 LUnaryMathOperation* result = new LUnaryMathOperation(input, temp);
1239 switch (op) {
1240 case kMathAbs:
1241 return AssignEnvironment(AssignPointerMap(DefineSameAsFirst(result)));
1242 case kMathFloor:
1243 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1244 case kMathSqrt:
1245 return DefineSameAsFirst(result);
1246 case kMathRound:
1247 return AssignEnvironment(DefineAsRegister(result));
1248 case kMathPowHalf:
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001249 return DefineSameAsFirst(result);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001250 default:
1251 UNREACHABLE();
1252 return NULL;
1253 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001254 }
1255}
1256
1257
1258LInstruction* LChunkBuilder::DoCallKeyed(HCallKeyed* instr) {
1259 ASSERT(instr->key()->representation().IsTagged());
1260 argument_count_ -= instr->argument_count();
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001261 LOperand* key = UseFixed(instr->key(), r2);
1262 return MarkAsCall(DefineFixed(new LCallKeyed(key), r0), instr);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001263}
1264
1265
1266LInstruction* LChunkBuilder::DoCallNamed(HCallNamed* instr) {
1267 argument_count_ -= instr->argument_count();
1268 return MarkAsCall(DefineFixed(new LCallNamed, r0), instr);
1269}
1270
1271
1272LInstruction* LChunkBuilder::DoCallGlobal(HCallGlobal* instr) {
1273 argument_count_ -= instr->argument_count();
1274 return MarkAsCall(DefineFixed(new LCallGlobal, r0), instr);
1275}
1276
1277
1278LInstruction* LChunkBuilder::DoCallKnownGlobal(HCallKnownGlobal* instr) {
1279 argument_count_ -= instr->argument_count();
1280 return MarkAsCall(DefineFixed(new LCallKnownGlobal, r0), instr);
1281}
1282
1283
1284LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
1285 LOperand* constructor = UseFixed(instr->constructor(), r1);
1286 argument_count_ -= instr->argument_count();
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001287 LCallNew* result = new LCallNew(constructor);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001288 return MarkAsCall(DefineFixed(result, r0), instr);
1289}
1290
1291
1292LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
1293 argument_count_ -= instr->argument_count();
1294 return MarkAsCall(DefineFixed(new LCallFunction, r0), instr);
1295}
1296
1297
1298LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1299 argument_count_ -= instr->argument_count();
1300 return MarkAsCall(DefineFixed(new LCallRuntime, r0), instr);
1301}
1302
1303
1304LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1305 return DoShift(Token::SHR, instr);
1306}
1307
1308
1309LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1310 return DoShift(Token::SAR, instr);
1311}
1312
1313
1314LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1315 return DoShift(Token::SHL, instr);
1316}
1317
1318
1319LInstruction* LChunkBuilder::DoBitAnd(HBitAnd* instr) {
1320 return DoBit(Token::BIT_AND, instr);
1321}
1322
1323
1324LInstruction* LChunkBuilder::DoBitNot(HBitNot* instr) {
1325 ASSERT(instr->value()->representation().IsInteger32());
1326 ASSERT(instr->representation().IsInteger32());
1327 return DefineSameAsFirst(new LBitNotI(UseRegisterAtStart(instr->value())));
1328}
1329
1330
1331LInstruction* LChunkBuilder::DoBitOr(HBitOr* instr) {
1332 return DoBit(Token::BIT_OR, instr);
1333}
1334
1335
1336LInstruction* LChunkBuilder::DoBitXor(HBitXor* instr) {
1337 return DoBit(Token::BIT_XOR, instr);
1338}
1339
1340
1341LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
1342 if (instr->representation().IsDouble()) {
1343 return DoArithmeticD(Token::DIV, instr);
1344 } else if (instr->representation().IsInteger32()) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001345 // TODO(1042) The fixed register allocation
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001346 // is needed because we call TypeRecordingBinaryOpStub from
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001347 // the generated code, which requires registers r0
1348 // and r1 to be used. We should remove that
1349 // when we provide a native implementation.
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001350 LOperand* dividend = UseFixed(instr->left(), r0);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001351 LOperand* divisor = UseFixed(instr->right(), r1);
1352 return AssignEnvironment(AssignPointerMap(
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001353 DefineFixed(new LDivI(dividend, divisor), r0)));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001354 } else {
1355 return DoArithmeticT(Token::DIV, instr);
1356 }
1357}
1358
1359
1360LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1361 if (instr->representation().IsInteger32()) {
1362 ASSERT(instr->left()->representation().IsInteger32());
1363 ASSERT(instr->right()->representation().IsInteger32());
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001364
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001365 LModI* mod;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001366 if (instr->HasPowerOf2Divisor()) {
1367 ASSERT(!instr->CheckFlag(HValue::kCanBeDivByZero));
1368 LOperand* value = UseRegisterAtStart(instr->left());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001369 mod = new LModI(value, UseOrConstant(instr->right()));
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001370 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001371 LOperand* dividend = UseRegister(instr->left());
1372 LOperand* divisor = UseRegisterAtStart(instr->right());
1373 mod = new LModI(dividend,
1374 divisor,
1375 TempRegister(),
1376 FixedTemp(d1),
1377 FixedTemp(d2));
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001378 }
1379
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001380 return AssignEnvironment(DefineSameAsFirst(mod));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001381 } else if (instr->representation().IsTagged()) {
1382 return DoArithmeticT(Token::MOD, instr);
1383 } else {
1384 ASSERT(instr->representation().IsDouble());
1385 // We call a C function for double modulo. It can't trigger a GC.
1386 // We need to use fixed result register for the call.
1387 // TODO(fschneider): Allow any register as input registers.
1388 LOperand* left = UseFixedDouble(instr->left(), d1);
1389 LOperand* right = UseFixedDouble(instr->right(), d2);
1390 LArithmeticD* result = new LArithmeticD(Token::MOD, left, right);
1391 return MarkAsCall(DefineFixedDouble(result, d1), instr);
1392 }
1393}
1394
1395
1396LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1397 if (instr->representation().IsInteger32()) {
1398 ASSERT(instr->left()->representation().IsInteger32());
1399 ASSERT(instr->right()->representation().IsInteger32());
1400 LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1401 LOperand* right = UseOrConstant(instr->MostConstantOperand());
1402 LOperand* temp = NULL;
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001403 if (instr->CheckFlag(HValue::kBailoutOnMinusZero) &&
1404 (instr->CheckFlag(HValue::kCanOverflow) ||
1405 !right->IsConstantOperand())) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001406 temp = TempRegister();
1407 }
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001408 return AssignEnvironment(DefineSameAsFirst(new LMulI(left, right, temp)));
1409
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001410 } else if (instr->representation().IsDouble()) {
1411 return DoArithmeticD(Token::MUL, instr);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001412
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001413 } else {
1414 return DoArithmeticT(Token::MUL, instr);
1415 }
1416}
1417
1418
1419LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1420 if (instr->representation().IsInteger32()) {
1421 ASSERT(instr->left()->representation().IsInteger32());
1422 ASSERT(instr->right()->representation().IsInteger32());
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001423 LOperand* left = UseRegisterAtStart(instr->left());
1424 LOperand* right = UseOrConstantAtStart(instr->right());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001425 LSubI* sub = new LSubI(left, right);
1426 LInstruction* result = DefineSameAsFirst(sub);
1427 if (instr->CheckFlag(HValue::kCanOverflow)) {
1428 result = AssignEnvironment(result);
1429 }
1430 return result;
1431 } else if (instr->representation().IsDouble()) {
1432 return DoArithmeticD(Token::SUB, instr);
1433 } else {
1434 return DoArithmeticT(Token::SUB, instr);
1435 }
1436}
1437
1438
1439LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1440 if (instr->representation().IsInteger32()) {
1441 ASSERT(instr->left()->representation().IsInteger32());
1442 ASSERT(instr->right()->representation().IsInteger32());
1443 LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1444 LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1445 LAddI* add = new LAddI(left, right);
1446 LInstruction* result = DefineSameAsFirst(add);
1447 if (instr->CheckFlag(HValue::kCanOverflow)) {
1448 result = AssignEnvironment(result);
1449 }
1450 return result;
1451 } else if (instr->representation().IsDouble()) {
1452 return DoArithmeticD(Token::ADD, instr);
1453 } else {
1454 ASSERT(instr->representation().IsTagged());
1455 return DoArithmeticT(Token::ADD, instr);
1456 }
1457}
1458
1459
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001460LInstruction* LChunkBuilder::DoPower(HPower* instr) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001461 ASSERT(instr->representation().IsDouble());
1462 // We call a C function for double power. It can't trigger a GC.
1463 // We need to use fixed result register for the call.
1464 Representation exponent_type = instr->right()->representation();
1465 ASSERT(instr->left()->representation().IsDouble());
1466 LOperand* left = UseFixedDouble(instr->left(), d1);
1467 LOperand* right = exponent_type.IsDouble() ?
1468 UseFixedDouble(instr->right(), d2) :
1469 UseFixed(instr->right(), r0);
1470 LPower* result = new LPower(left, right);
1471 return MarkAsCall(DefineFixedDouble(result, d3),
1472 instr,
1473 CAN_DEOPTIMIZE_EAGERLY);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001474}
1475
1476
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001477LInstruction* LChunkBuilder::DoCompare(HCompare* instr) {
1478 Token::Value op = instr->token();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001479 Representation r = instr->GetInputRepresentation();
1480 if (r.IsInteger32()) {
1481 ASSERT(instr->left()->representation().IsInteger32());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001482 ASSERT(instr->right()->representation().IsInteger32());
1483 LOperand* left = UseRegisterAtStart(instr->left());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001484 LOperand* right = UseRegisterAtStart(instr->right());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001485 return DefineAsRegister(new LCmpID(left, right));
1486 } else if (r.IsDouble()) {
1487 ASSERT(instr->left()->representation().IsDouble());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001488 ASSERT(instr->right()->representation().IsDouble());
1489 LOperand* left = UseRegisterAtStart(instr->left());
1490 LOperand* right = UseRegisterAtStart(instr->right());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001491 return DefineAsRegister(new LCmpID(left, right));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001492 } else {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001493 ASSERT(instr->left()->representation().IsTagged());
1494 ASSERT(instr->right()->representation().IsTagged());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001495 bool reversed = (op == Token::GT || op == Token::LTE);
1496 LOperand* left = UseFixed(instr->left(), reversed ? r0 : r1);
1497 LOperand* right = UseFixed(instr->right(), reversed ? r1 : r0);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001498 LCmpT* result = new LCmpT(left, right);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001499 return MarkAsCall(DefineFixed(result, r0), instr);
1500 }
1501}
1502
1503
1504LInstruction* LChunkBuilder::DoCompareJSObjectEq(
1505 HCompareJSObjectEq* instr) {
1506 LOperand* left = UseRegisterAtStart(instr->left());
1507 LOperand* right = UseRegisterAtStart(instr->right());
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001508 LCmpJSObjectEq* result = new LCmpJSObjectEq(left, right);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001509 return DefineAsRegister(result);
1510}
1511
1512
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001513LInstruction* LChunkBuilder::DoCompareSymbolEq(
1514 HCompareSymbolEq* instr) {
1515 LOperand* left = UseRegisterAtStart(instr->left());
1516 LOperand* right = UseRegisterAtStart(instr->right());
1517 LCmpSymbolEq* result = new LCmpSymbolEq(left, right);
1518 return DefineAsRegister(result);
1519}
1520
1521
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001522LInstruction* LChunkBuilder::DoIsNull(HIsNull* instr) {
1523 ASSERT(instr->value()->representation().IsTagged());
1524 LOperand* value = UseRegisterAtStart(instr->value());
1525
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001526 return DefineAsRegister(new LIsNull(value));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001527}
1528
1529
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001530LInstruction* LChunkBuilder::DoIsObject(HIsObject* instr) {
1531 ASSERT(instr->value()->representation().IsTagged());
1532 LOperand* value = UseRegisterAtStart(instr->value());
1533
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001534 return DefineAsRegister(new LIsObject(value));
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001535}
1536
1537
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001538LInstruction* LChunkBuilder::DoIsSmi(HIsSmi* instr) {
1539 ASSERT(instr->value()->representation().IsTagged());
1540 LOperand* value = UseAtStart(instr->value());
1541
1542 return DefineAsRegister(new LIsSmi(value));
1543}
1544
1545
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001546LInstruction* LChunkBuilder::DoIsUndetectable(HIsUndetectable* instr) {
1547 ASSERT(instr->value()->representation().IsTagged());
1548 LOperand* value = UseRegisterAtStart(instr->value());
1549
1550 return DefineAsRegister(new LIsUndetectable(value));
1551}
1552
1553
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001554LInstruction* LChunkBuilder::DoHasInstanceType(HHasInstanceType* instr) {
1555 ASSERT(instr->value()->representation().IsTagged());
1556 LOperand* value = UseRegisterAtStart(instr->value());
1557
1558 return DefineAsRegister(new LHasInstanceType(value));
1559}
1560
1561
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001562LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
1563 HGetCachedArrayIndex* instr) {
1564 ASSERT(instr->value()->representation().IsTagged());
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001565 LOperand* value = UseRegisterAtStart(instr->value());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001566
1567 return DefineAsRegister(new LGetCachedArrayIndex(value));
1568}
1569
1570
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001571LInstruction* LChunkBuilder::DoHasCachedArrayIndex(
1572 HHasCachedArrayIndex* instr) {
1573 ASSERT(instr->value()->representation().IsTagged());
1574 LOperand* value = UseRegister(instr->value());
1575
1576 return DefineAsRegister(new LHasCachedArrayIndex(value));
1577}
1578
1579
1580LInstruction* LChunkBuilder::DoClassOfTest(HClassOfTest* instr) {
1581 ASSERT(instr->value()->representation().IsTagged());
1582 LOperand* value = UseTempRegister(instr->value());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001583 return DefineSameAsFirst(new LClassOfTest(value));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001584}
1585
1586
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00001587LInstruction* LChunkBuilder::DoJSArrayLength(HJSArrayLength* instr) {
1588 LOperand* array = UseRegisterAtStart(instr->value());
1589 return DefineAsRegister(new LJSArrayLength(array));
1590}
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001591
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001592
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001593LInstruction* LChunkBuilder::DoExternalArrayLength(
1594 HExternalArrayLength* instr) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001595 LOperand* array = UseRegisterAtStart(instr->value());
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001596 return DefineAsRegister(new LExternalArrayLength(array));
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001597}
1598
1599
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00001600LInstruction* LChunkBuilder::DoFixedArrayLength(HFixedArrayLength* instr) {
1601 LOperand* array = UseRegisterAtStart(instr->value());
1602 return DefineAsRegister(new LFixedArrayLength(array));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001603}
1604
1605
1606LInstruction* LChunkBuilder::DoValueOf(HValueOf* instr) {
1607 LOperand* object = UseRegister(instr->value());
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001608 LValueOf* result = new LValueOf(object, TempRegister());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001609 return AssignEnvironment(DefineSameAsFirst(result));
1610}
1611
1612
1613LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
1614 return AssignEnvironment(new LBoundsCheck(UseRegisterAtStart(instr->index()),
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001615 UseRegister(instr->length())));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001616}
1617
1618
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001619LInstruction* LChunkBuilder::DoAbnormalExit(HAbnormalExit* instr) {
1620 // The control instruction marking the end of a block that completed
1621 // abruptly (e.g., threw an exception). There is nothing specific to do.
1622 return NULL;
1623}
1624
1625
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001626LInstruction* LChunkBuilder::DoThrow(HThrow* instr) {
1627 LOperand* value = UseFixed(instr->value(), r0);
1628 return MarkAsCall(new LThrow(value), instr);
1629}
1630
1631
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001632LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
1633 return NULL;
1634}
1635
1636
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001637LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
1638 // All HForceRepresentation instructions should be eliminated in the
1639 // representation change phase of Hydrogen.
1640 UNREACHABLE();
1641 return NULL;
1642}
1643
1644
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001645LInstruction* LChunkBuilder::DoChange(HChange* instr) {
1646 Representation from = instr->from();
1647 Representation to = instr->to();
1648 if (from.IsTagged()) {
1649 if (to.IsDouble()) {
1650 LOperand* value = UseRegister(instr->value());
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001651 LNumberUntagD* res = new LNumberUntagD(value);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001652 return AssignEnvironment(DefineAsRegister(res));
1653 } else {
1654 ASSERT(to.IsInteger32());
1655 LOperand* value = UseRegister(instr->value());
1656 bool needs_check = !instr->value()->type().IsSmi();
1657 LInstruction* res = NULL;
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001658 if (!needs_check) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001659 res = DefineSameAsFirst(new LSmiUntag(value, needs_check));
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001660 } else {
1661 LOperand* temp1 = TempRegister();
1662 LOperand* temp2 = instr->CanTruncateToInt32() ? TempRegister()
1663 : NULL;
1664 LOperand* temp3 = instr->CanTruncateToInt32() ? FixedTemp(d3)
1665 : NULL;
1666 res = DefineSameAsFirst(new LTaggedToI(value, temp1, temp2, temp3));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001667 res = AssignEnvironment(res);
1668 }
1669 return res;
1670 }
1671 } else if (from.IsDouble()) {
1672 if (to.IsTagged()) {
1673 LOperand* value = UseRegister(instr->value());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001674 LOperand* temp1 = TempRegister();
1675 LOperand* temp2 = TempRegister();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001676
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001677 // Make sure that the temp and result_temp registers are
1678 // different.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001679 LUnallocated* result_temp = TempRegister();
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001680 LNumberTagD* result = new LNumberTagD(value, temp1, temp2);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001681 Define(result, result_temp);
1682 return AssignPointerMap(result);
1683 } else {
1684 ASSERT(to.IsInteger32());
1685 LOperand* value = UseRegister(instr->value());
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001686 LDoubleToI* res =
1687 new LDoubleToI(value,
1688 TempRegister(),
1689 instr->CanTruncateToInt32() ? TempRegister() : NULL);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001690 return AssignEnvironment(DefineAsRegister(res));
1691 }
1692 } else if (from.IsInteger32()) {
1693 if (to.IsTagged()) {
1694 HValue* val = instr->value();
1695 LOperand* value = UseRegister(val);
1696 if (val->HasRange() && val->range()->IsInSmiRange()) {
1697 return DefineSameAsFirst(new LSmiTag(value));
1698 } else {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001699 LNumberTagI* result = new LNumberTagI(value);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001700 return AssignEnvironment(AssignPointerMap(DefineSameAsFirst(result)));
1701 }
1702 } else {
1703 ASSERT(to.IsDouble());
1704 LOperand* value = Use(instr->value());
1705 return DefineAsRegister(new LInteger32ToDouble(value));
1706 }
1707 }
1708 UNREACHABLE();
1709 return NULL;
1710}
1711
1712
1713LInstruction* LChunkBuilder::DoCheckNonSmi(HCheckNonSmi* instr) {
1714 LOperand* value = UseRegisterAtStart(instr->value());
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001715 return AssignEnvironment(new LCheckNonSmi(value));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001716}
1717
1718
1719LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
1720 LOperand* value = UseRegisterAtStart(instr->value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001721 LInstruction* result = new LCheckInstanceType(value);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001722 return AssignEnvironment(result);
1723}
1724
1725
1726LInstruction* LChunkBuilder::DoCheckPrototypeMaps(HCheckPrototypeMaps* instr) {
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001727 LOperand* temp1 = TempRegister();
1728 LOperand* temp2 = TempRegister();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001729 LInstruction* result = new LCheckPrototypeMaps(temp1, temp2);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001730 return AssignEnvironment(result);
1731}
1732
1733
1734LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
1735 LOperand* value = UseRegisterAtStart(instr->value());
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001736 return AssignEnvironment(new LCheckSmi(value));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001737}
1738
1739
1740LInstruction* LChunkBuilder::DoCheckFunction(HCheckFunction* instr) {
1741 LOperand* value = UseRegisterAtStart(instr->value());
1742 return AssignEnvironment(new LCheckFunction(value));
1743}
1744
1745
1746LInstruction* LChunkBuilder::DoCheckMap(HCheckMap* instr) {
1747 LOperand* value = UseRegisterAtStart(instr->value());
1748 LInstruction* result = new LCheckMap(value);
1749 return AssignEnvironment(result);
1750}
1751
1752
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001753LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
1754 HValue* value = instr->value();
1755 Representation input_rep = value->representation();
1756 LOperand* reg = UseRegister(value);
1757 if (input_rep.IsDouble()) {
1758 return DefineAsRegister(new LClampDToUint8(reg, FixedTemp(d1)));
1759 } else if (input_rep.IsInteger32()) {
1760 return DefineAsRegister(new LClampIToUint8(reg));
1761 } else {
1762 ASSERT(input_rep.IsTagged());
1763 // Register allocator doesn't (yet) support allocation of double
1764 // temps. Reserve d1 explicitly.
1765 LClampTToUint8* result = new LClampTToUint8(reg, FixedTemp(d1));
1766 return AssignEnvironment(DefineAsRegister(result));
1767 }
1768}
1769
1770
ricow@chromium.orgc54d3652011-05-30 09:20:16 +00001771LInstruction* LChunkBuilder::DoToInt32(HToInt32* instr) {
1772 HValue* value = instr->value();
1773 Representation input_rep = value->representation();
1774 LOperand* reg = UseRegister(value);
1775 if (input_rep.IsDouble()) {
1776 LOperand* temp1 = TempRegister();
1777 LOperand* temp2 = TempRegister();
1778 LDoubleToI* res = new LDoubleToI(reg, temp1, temp2);
1779 return AssignEnvironment(DefineAsRegister(res));
1780 } else if (input_rep.IsInteger32()) {
1781 // Canonicalization should already have removed the hydrogen instruction in
1782 // this case, since it is a noop.
1783 UNREACHABLE();
1784 return NULL;
1785 } else {
1786 ASSERT(input_rep.IsTagged());
1787 LOperand* temp1 = TempRegister();
1788 LOperand* temp2 = TempRegister();
1789 LOperand* temp3 = FixedTemp(d3);
1790 LTaggedToI* res = new LTaggedToI(reg, temp1, temp2, temp3);
1791 return AssignEnvironment(DefineSameAsFirst(res));
1792 }
1793}
1794
1795
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001796LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
1797 return new LReturn(UseFixed(instr->value(), r0));
1798}
1799
1800
1801LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
1802 Representation r = instr->representation();
1803 if (r.IsInteger32()) {
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001804 return DefineAsRegister(new LConstantI);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001805 } else if (r.IsDouble()) {
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001806 return DefineAsRegister(new LConstantD);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001807 } else if (r.IsTagged()) {
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001808 return DefineAsRegister(new LConstantT);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001809 } else {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001810 UNREACHABLE();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001811 return NULL;
1812 }
1813}
1814
1815
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001816LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
1817 LLoadGlobalCell* result = new LLoadGlobalCell;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001818 return instr->check_hole_value()
1819 ? AssignEnvironment(DefineAsRegister(result))
1820 : DefineAsRegister(result);
1821}
1822
1823
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001824LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
1825 LOperand* global_object = UseFixed(instr->global_object(), r0);
1826 LLoadGlobalGeneric* result = new LLoadGlobalGeneric(global_object);
1827 return MarkAsCall(DefineFixed(result, r0), instr);
1828}
1829
1830
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001831LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
ager@chromium.org378b34e2011-01-28 08:04:38 +00001832 if (instr->check_hole_value()) {
1833 LOperand* temp = TempRegister();
1834 LOperand* value = UseRegister(instr->value());
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001835 return AssignEnvironment(new LStoreGlobalCell(value, temp));
ager@chromium.org378b34e2011-01-28 08:04:38 +00001836 } else {
1837 LOperand* value = UseRegisterAtStart(instr->value());
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001838 return new LStoreGlobalCell(value, NULL);
ager@chromium.org378b34e2011-01-28 08:04:38 +00001839 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001840}
1841
1842
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001843LInstruction* LChunkBuilder::DoStoreGlobalGeneric(HStoreGlobalGeneric* instr) {
1844 LOperand* global_object = UseFixed(instr->global_object(), r1);
1845 LOperand* value = UseFixed(instr->value(), r0);
1846 LStoreGlobalGeneric* result =
1847 new LStoreGlobalGeneric(global_object, value);
1848 return MarkAsCall(result, instr);
1849}
1850
1851
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001852LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001853 LOperand* context = UseRegisterAtStart(instr->value());
1854 return DefineAsRegister(new LLoadContextSlot(context));
1855}
1856
1857
1858LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001859 LOperand* context;
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001860 LOperand* value;
1861 if (instr->NeedsWriteBarrier()) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001862 context = UseTempRegister(instr->context());
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001863 value = UseTempRegister(instr->value());
1864 } else {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001865 context = UseRegister(instr->context());
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001866 value = UseRegister(instr->value());
1867 }
1868 return new LStoreContextSlot(context, value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001869}
1870
1871
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001872LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
1873 return DefineAsRegister(
1874 new LLoadNamedField(UseRegisterAtStart(instr->object())));
1875}
1876
1877
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001878LInstruction* LChunkBuilder::DoLoadNamedFieldPolymorphic(
1879 HLoadNamedFieldPolymorphic* instr) {
1880 ASSERT(instr->representation().IsTagged());
1881 if (instr->need_generic()) {
1882 LOperand* obj = UseFixed(instr->object(), r0);
1883 LLoadNamedFieldPolymorphic* result = new LLoadNamedFieldPolymorphic(obj);
1884 return MarkAsCall(DefineFixed(result, r0), instr);
1885 } else {
1886 LOperand* obj = UseRegisterAtStart(instr->object());
1887 LLoadNamedFieldPolymorphic* result = new LLoadNamedFieldPolymorphic(obj);
1888 return AssignEnvironment(DefineAsRegister(result));
1889 }
1890}
1891
1892
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001893LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
1894 LOperand* object = UseFixed(instr->object(), r0);
1895 LInstruction* result = DefineFixed(new LLoadNamedGeneric(object), r0);
1896 return MarkAsCall(result, instr);
1897}
1898
1899
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00001900LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
1901 HLoadFunctionPrototype* instr) {
1902 return AssignEnvironment(DefineAsRegister(
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001903 new LLoadFunctionPrototype(UseRegister(instr->function()))));
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00001904}
1905
1906
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001907LInstruction* LChunkBuilder::DoLoadElements(HLoadElements* instr) {
1908 LOperand* input = UseRegisterAtStart(instr->value());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001909 return DefineAsRegister(new LLoadElements(input));
1910}
1911
1912
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001913LInstruction* LChunkBuilder::DoLoadExternalArrayPointer(
1914 HLoadExternalArrayPointer* instr) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001915 LOperand* input = UseRegisterAtStart(instr->value());
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001916 return DefineAsRegister(new LLoadExternalArrayPointer(input));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001917}
1918
1919
1920LInstruction* LChunkBuilder::DoLoadKeyedFastElement(
1921 HLoadKeyedFastElement* instr) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001922 ASSERT(instr->representation().IsTagged());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001923 ASSERT(instr->key()->representation().IsInteger32());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001924 LOperand* obj = UseRegisterAtStart(instr->object());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001925 LOperand* key = UseRegisterAtStart(instr->key());
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001926 LLoadKeyedFastElement* result = new LLoadKeyedFastElement(obj, key);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001927 return AssignEnvironment(DefineSameAsFirst(result));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001928}
1929
1930
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001931LInstruction* LChunkBuilder::DoLoadKeyedSpecializedArrayElement(
1932 HLoadKeyedSpecializedArrayElement* instr) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001933 JSObject::ElementsKind elements_kind = instr->elements_kind();
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001934 Representation representation(instr->representation());
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001935 ASSERT(
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001936 (representation.IsInteger32() &&
1937 (elements_kind != JSObject::EXTERNAL_FLOAT_ELEMENTS) &&
1938 (elements_kind != JSObject::EXTERNAL_DOUBLE_ELEMENTS)) ||
1939 (representation.IsDouble() &&
1940 ((elements_kind == JSObject::EXTERNAL_FLOAT_ELEMENTS) ||
1941 (elements_kind == JSObject::EXTERNAL_DOUBLE_ELEMENTS))));
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001942 ASSERT(instr->key()->representation().IsInteger32());
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001943 LOperand* external_pointer = UseRegister(instr->external_pointer());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001944 LOperand* key = UseRegisterOrConstant(instr->key());
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001945 LLoadKeyedSpecializedArrayElement* result =
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001946 new LLoadKeyedSpecializedArrayElement(external_pointer, key);
1947 LInstruction* load_instr = DefineAsRegister(result);
1948 // An unsigned int array load might overflow and cause a deopt, make sure it
1949 // has an environment.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001950 return (elements_kind == JSObject::EXTERNAL_UNSIGNED_INT_ELEMENTS) ?
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001951 AssignEnvironment(load_instr) : load_instr;
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001952}
1953
1954
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001955LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
1956 LOperand* object = UseFixed(instr->object(), r1);
1957 LOperand* key = UseFixed(instr->key(), r0);
1958
1959 LInstruction* result =
1960 DefineFixed(new LLoadKeyedGeneric(object, key), r0);
1961 return MarkAsCall(result, instr);
1962}
1963
1964
1965LInstruction* LChunkBuilder::DoStoreKeyedFastElement(
1966 HStoreKeyedFastElement* instr) {
1967 bool needs_write_barrier = instr->NeedsWriteBarrier();
1968 ASSERT(instr->value()->representation().IsTagged());
1969 ASSERT(instr->object()->representation().IsTagged());
1970 ASSERT(instr->key()->representation().IsInteger32());
1971
1972 LOperand* obj = UseTempRegister(instr->object());
1973 LOperand* val = needs_write_barrier
1974 ? UseTempRegister(instr->value())
1975 : UseRegisterAtStart(instr->value());
1976 LOperand* key = needs_write_barrier
1977 ? UseTempRegister(instr->key())
1978 : UseRegisterOrConstantAtStart(instr->key());
1979
1980 return AssignEnvironment(new LStoreKeyedFastElement(obj, key, val));
1981}
1982
1983
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001984LInstruction* LChunkBuilder::DoStoreKeyedSpecializedArrayElement(
1985 HStoreKeyedSpecializedArrayElement* instr) {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001986 Representation representation(instr->value()->representation());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001987 JSObject::ElementsKind elements_kind = instr->elements_kind();
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001988 ASSERT(
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001989 (representation.IsInteger32() &&
1990 (elements_kind != JSObject::EXTERNAL_FLOAT_ELEMENTS) &&
1991 (elements_kind != JSObject::EXTERNAL_DOUBLE_ELEMENTS)) ||
1992 (representation.IsDouble() &&
1993 ((elements_kind == JSObject::EXTERNAL_FLOAT_ELEMENTS) ||
1994 (elements_kind == JSObject::EXTERNAL_DOUBLE_ELEMENTS))));
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001995 ASSERT(instr->external_pointer()->representation().IsExternal());
1996 ASSERT(instr->key()->representation().IsInteger32());
1997
1998 LOperand* external_pointer = UseRegister(instr->external_pointer());
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001999 bool val_is_temp_register =
2000 elements_kind == JSObject::EXTERNAL_PIXEL_ELEMENTS ||
2001 elements_kind == JSObject::EXTERNAL_FLOAT_ELEMENTS;
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00002002 LOperand* val = val_is_temp_register
2003 ? UseTempRegister(instr->value())
2004 : UseRegister(instr->value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002005 LOperand* key = UseRegisterOrConstant(instr->key());
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00002006
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002007 return new LStoreKeyedSpecializedArrayElement(external_pointer,
2008 key,
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00002009 val);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002010}
2011
2012
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002013LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
2014 LOperand* obj = UseFixed(instr->object(), r2);
2015 LOperand* key = UseFixed(instr->key(), r1);
2016 LOperand* val = UseFixed(instr->value(), r0);
2017
2018 ASSERT(instr->object()->representation().IsTagged());
2019 ASSERT(instr->key()->representation().IsTagged());
2020 ASSERT(instr->value()->representation().IsTagged());
2021
2022 return MarkAsCall(new LStoreKeyedGeneric(obj, key, val), instr);
2023}
2024
2025
2026LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00002027 bool needs_write_barrier = instr->NeedsWriteBarrier();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002028
2029 LOperand* obj = needs_write_barrier
2030 ? UseTempRegister(instr->object())
2031 : UseRegisterAtStart(instr->object());
2032
2033 LOperand* val = needs_write_barrier
2034 ? UseTempRegister(instr->value())
2035 : UseRegister(instr->value());
2036
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002037 return new LStoreNamedField(obj, val);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002038}
2039
2040
2041LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
2042 LOperand* obj = UseFixed(instr->object(), r1);
2043 LOperand* val = UseFixed(instr->value(), r0);
2044
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002045 LInstruction* result = new LStoreNamedGeneric(obj, val);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002046 return MarkAsCall(result, instr);
2047}
2048
2049
danno@chromium.org160a7b02011-04-18 15:51:38 +00002050LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
2051 LOperand* left = UseRegisterAtStart(instr->left());
2052 LOperand* right = UseRegisterAtStart(instr->right());
2053 return MarkAsCall(DefineFixed(new LStringAdd(left, right), r0), instr);
2054}
2055
2056
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00002057LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
2058 LOperand* string = UseRegister(instr->string());
2059 LOperand* index = UseRegisterOrConstant(instr->index());
2060 LStringCharCodeAt* result = new LStringCharCodeAt(string, index);
2061 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
2062}
2063
2064
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002065LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2066 LOperand* char_code = UseRegister(instr->value());
2067 LStringCharFromCode* result = new LStringCharFromCode(char_code);
2068 return AssignPointerMap(DefineAsRegister(result));
2069}
2070
2071
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00002072LInstruction* LChunkBuilder::DoStringLength(HStringLength* instr) {
2073 LOperand* string = UseRegisterAtStart(instr->value());
2074 return DefineAsRegister(new LStringLength(string));
2075}
2076
2077
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002078LInstruction* LChunkBuilder::DoArrayLiteral(HArrayLiteral* instr) {
2079 return MarkAsCall(DefineFixed(new LArrayLiteral, r0), instr);
2080}
2081
2082
2083LInstruction* LChunkBuilder::DoObjectLiteral(HObjectLiteral* instr) {
2084 return MarkAsCall(DefineFixed(new LObjectLiteral, r0), instr);
2085}
2086
2087
2088LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2089 return MarkAsCall(DefineFixed(new LRegExpLiteral, r0), instr);
2090}
2091
2092
2093LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2094 return MarkAsCall(DefineFixed(new LFunctionLiteral, r0), instr);
2095}
2096
2097
2098LInstruction* LChunkBuilder::DoDeleteProperty(HDeleteProperty* instr) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002099 LOperand* object = UseFixed(instr->object(), r0);
2100 LOperand* key = UseFixed(instr->key(), r1);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00002101 LDeleteProperty* result = new LDeleteProperty(object, key);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002102 return MarkAsCall(DefineFixed(result, r0), instr);
2103}
2104
2105
2106LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2107 allocator_->MarkAsOsrEntry();
2108 current_block_->last_environment()->set_ast_id(instr->ast_id());
2109 return AssignEnvironment(new LOsrEntry);
2110}
2111
2112
2113LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2114 int spill_index = chunk()->GetParameterStackSlot(instr->index());
2115 return DefineAsSpilled(new LParameter, spill_index);
2116}
2117
2118
2119LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2120 int spill_index = chunk()->GetNextSpillIndex(false); // Not double-width.
2121 return DefineAsSpilled(new LUnknownOSRValue, spill_index);
2122}
2123
2124
2125LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2126 argument_count_ -= instr->argument_count();
2127 return MarkAsCall(DefineFixed(new LCallStub, r0), instr);
2128}
2129
2130
2131LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002132 // There are no real uses of the arguments object.
2133 // arguments.length and element access are supported directly on
2134 // stack arguments, and any real arguments object use causes a bailout.
2135 // So this value is never used.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002136 return NULL;
2137}
2138
2139
2140LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2141 LOperand* arguments = UseRegister(instr->arguments());
2142 LOperand* length = UseTempRegister(instr->length());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00002143 LOperand* index = UseRegister(instr->index());
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00002144 LAccessArgumentsAt* result = new LAccessArgumentsAt(arguments, length, index);
2145 return AssignEnvironment(DefineAsRegister(result));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002146}
2147
2148
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002149LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2150 LOperand* object = UseFixed(instr->value(), r0);
2151 LToFastProperties* result = new LToFastProperties(object);
2152 return MarkAsCall(DefineFixed(result, r0), instr);
2153}
2154
2155
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002156LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002157 LTypeof* result = new LTypeof(UseFixed(instr->value(), r0));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002158 return MarkAsCall(DefineFixed(result, r0), instr);
2159}
2160
2161
2162LInstruction* LChunkBuilder::DoTypeofIs(HTypeofIs* instr) {
2163 return DefineSameAsFirst(new LTypeofIs(UseRegister(instr->value())));
2164}
2165
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00002166
2167LInstruction* LChunkBuilder::DoIsConstructCall(HIsConstructCall* instr) {
2168 return DefineAsRegister(new LIsConstructCall());
2169}
2170
2171
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002172LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2173 HEnvironment* env = current_block_->last_environment();
2174 ASSERT(env != NULL);
2175
2176 env->set_ast_id(instr->ast_id());
2177
2178 env->Drop(instr->pop_count());
2179 for (int i = 0; i < instr->values()->length(); ++i) {
2180 HValue* value = instr->values()->at(i);
2181 if (instr->HasAssignedIndexAt(i)) {
2182 env->Bind(instr->GetAssignedIndexAt(i), value);
2183 } else {
2184 env->Push(value);
2185 }
2186 }
2187
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002188 // If there is an instruction pending deoptimization environment create a
2189 // lazy bailout instruction to capture the environment.
fschneider@chromium.org1df6b472011-01-26 08:23:03 +00002190 if (pending_deoptimization_ast_id_ == instr->ast_id()) {
2191 LInstruction* result = new LLazyBailout;
2192 result = AssignEnvironment(result);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002193 instruction_pending_deoptimization_environment_->
fschneider@chromium.org1df6b472011-01-26 08:23:03 +00002194 set_deoptimization_environment(result->environment());
2195 ClearInstructionPendingDeoptimizationEnvironment();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002196 return result;
2197 }
2198
2199 return NULL;
2200}
2201
2202
2203LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2204 return MarkAsCall(new LStackCheck, instr);
2205}
2206
2207
2208LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2209 HEnvironment* outer = current_block_->last_environment();
2210 HConstant* undefined = graph()->GetConstantUndefined();
2211 HEnvironment* inner = outer->CopyForInlining(instr->closure(),
2212 instr->function(),
lrn@chromium.org1c092762011-05-09 09:42:16 +00002213 HEnvironment::LITHIUM,
danno@chromium.org40cb8782011-05-25 07:58:50 +00002214 undefined,
2215 instr->call_kind());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002216 current_block_->UpdateEnvironment(inner);
2217 chunk_->AddInlinedClosure(instr->closure());
2218 return NULL;
2219}
2220
2221
2222LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2223 HEnvironment* outer = current_block_->last_environment()->outer();
2224 current_block_->UpdateEnvironment(outer);
2225 return NULL;
2226}
2227
2228
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00002229LInstruction* LChunkBuilder::DoIn(HIn* instr) {
2230 LOperand* key = UseRegisterAtStart(instr->key());
2231 LOperand* object = UseRegisterAtStart(instr->object());
2232 LIn* result = new LIn(key, object);
2233 return MarkAsCall(DefineFixed(result, r0), instr);
2234}
2235
2236
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002237} } // namespace v8::internal