blob: 3be2fc41d0788a2a26e2a24a6e4d519f631b5035 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005#include <sstream>
6
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/v8.h"
Ben Murdochb8e0da22011-05-16 14:20:40 +01008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#if V8_TARGET_ARCH_IA32
Ben Murdochb8e0da22011-05-16 14:20:40 +010010
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/hydrogen-osr.h"
12#include "src/ia32/lithium-codegen-ia32.h"
13#include "src/lithium-inl.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010014
15namespace v8 {
16namespace internal {
17
18#define DEFINE_COMPILE(type) \
19 void L##type::CompileToNative(LCodeGen* generator) { \
20 generator->Do##type(this); \
21 }
22LITHIUM_CONCRETE_INSTRUCTION_LIST(DEFINE_COMPILE)
23#undef DEFINE_COMPILE
24
Ben Murdochb0fe1622011-05-05 13:52:32 +010025
Steve Block1e0659c2011-05-24 12:43:12 +010026#ifdef DEBUG
27void LInstruction::VerifyCall() {
Ben Murdoch257744e2011-11-30 15:57:28 +000028 // Call instructions can use only fixed registers as temporaries and
29 // outputs because all registers are blocked by the calling convention.
30 // Inputs operands must use a fixed register or use-at-start policy or
31 // a non-register policy.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032 DCHECK(Output() == NULL ||
Steve Block1e0659c2011-05-24 12:43:12 +010033 LUnallocated::cast(Output())->HasFixedPolicy() ||
34 !LUnallocated::cast(Output())->HasRegisterPolicy());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000035 for (UseIterator it(this); !it.Done(); it.Advance()) {
36 LUnallocated* operand = LUnallocated::cast(it.Current());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037 DCHECK(operand->HasFixedPolicy() ||
Ben Murdoch257744e2011-11-30 15:57:28 +000038 operand->IsUsedAtStart());
Steve Block1e0659c2011-05-24 12:43:12 +010039 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000040 for (TempIterator it(this); !it.Done(); it.Advance()) {
41 LUnallocated* operand = LUnallocated::cast(it.Current());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042 DCHECK(operand->HasFixedPolicy() ||!operand->HasRegisterPolicy());
Steve Block1e0659c2011-05-24 12:43:12 +010043 }
44}
45#endif
46
47
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048bool LInstruction::HasDoubleRegisterResult() {
49 return HasResult() && result()->IsDoubleRegister();
50}
51
52
53bool LInstruction::HasDoubleRegisterInput() {
54 for (int i = 0; i < InputCount(); i++) {
55 LOperand* op = InputAt(i);
56 if (op != NULL && op->IsDoubleRegister()) {
57 return true;
58 }
59 }
60 return false;
61}
62
63
Ben Murdochb8e0da22011-05-16 14:20:40 +010064void LInstruction::PrintTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +010065 stream->Add("%s ", this->Mnemonic());
Steve Block1e0659c2011-05-24 12:43:12 +010066
67 PrintOutputOperandTo(stream);
Ben Murdochb8e0da22011-05-16 14:20:40 +010068
Ben Murdochb0fe1622011-05-05 13:52:32 +010069 PrintDataTo(stream);
70
71 if (HasEnvironment()) {
72 stream->Add(" ");
73 environment()->PrintTo(stream);
74 }
75
76 if (HasPointerMap()) {
77 stream->Add(" ");
78 pointer_map()->PrintTo(stream);
79 }
80}
81
82
Ben Murdoch3ef787d2012-04-12 10:51:47 +010083void LInstruction::PrintDataTo(StringStream* stream) {
Ben Murdochb8e0da22011-05-16 14:20:40 +010084 stream->Add("= ");
Ben Murdoch3ef787d2012-04-12 10:51:47 +010085 for (int i = 0; i < InputCount(); i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000086 if (i > 0) stream->Add(" ");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000087 if (InputAt(i) == NULL) {
88 stream->Add("NULL");
89 } else {
90 InputAt(i)->PrintTo(stream);
91 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000092 }
Ben Murdochb8e0da22011-05-16 14:20:40 +010093}
94
95
Ben Murdoch3ef787d2012-04-12 10:51:47 +010096void LInstruction::PrintOutputOperandTo(StringStream* stream) {
97 if (HasResult()) result()->PrintTo(stream);
Ben Murdochb8e0da22011-05-16 14:20:40 +010098}
99
100
101void LLabel::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100102 LGap::PrintDataTo(stream);
103 LLabel* rep = replacement();
104 if (rep != NULL) {
105 stream->Add(" Dead block replaced with B%d", rep->block_id());
106 }
107}
108
109
Ben Murdochb0fe1622011-05-05 13:52:32 +0100110bool LGap::IsRedundant() const {
111 for (int i = 0; i < 4; i++) {
112 if (parallel_moves_[i] != NULL && !parallel_moves_[i]->IsRedundant()) {
113 return false;
114 }
115 }
116
117 return true;
118}
119
120
Ben Murdochb8e0da22011-05-16 14:20:40 +0100121void LGap::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100122 for (int i = 0; i < 4; i++) {
123 stream->Add("(");
124 if (parallel_moves_[i] != NULL) {
125 parallel_moves_[i]->PrintDataTo(stream);
126 }
127 stream->Add(") ");
128 }
129}
130
131
132const char* LArithmeticD::Mnemonic() const {
133 switch (op()) {
134 case Token::ADD: return "add-d";
135 case Token::SUB: return "sub-d";
136 case Token::MUL: return "mul-d";
137 case Token::DIV: return "div-d";
138 case Token::MOD: return "mod-d";
139 default:
140 UNREACHABLE();
141 return NULL;
142 }
143}
144
145
146const char* LArithmeticT::Mnemonic() const {
147 switch (op()) {
148 case Token::ADD: return "add-t";
149 case Token::SUB: return "sub-t";
150 case Token::MUL: return "mul-t";
151 case Token::MOD: return "mod-t";
152 case Token::DIV: return "div-t";
Steve Block1e0659c2011-05-24 12:43:12 +0100153 case Token::BIT_AND: return "bit-and-t";
154 case Token::BIT_OR: return "bit-or-t";
155 case Token::BIT_XOR: return "bit-xor-t";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156 case Token::ROR: return "ror-t";
Steve Block1e0659c2011-05-24 12:43:12 +0100157 case Token::SHL: return "sal-t";
158 case Token::SAR: return "sar-t";
159 case Token::SHR: return "shr-t";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100160 default:
161 UNREACHABLE();
162 return NULL;
163 }
164}
165
166
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000167bool LGoto::HasInterestingComment(LCodeGen* gen) const {
168 return !gen->IsNextEmittedBlock(block_id());
169}
170
171
Ben Murdochb8e0da22011-05-16 14:20:40 +0100172void LGoto::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100173 stream->Add("B%d", block_id());
174}
175
176
Ben Murdochb8e0da22011-05-16 14:20:40 +0100177void LBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100178 stream->Add("B%d | B%d on ", true_block_id(), false_block_id());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179 value()->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100180}
181
182
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000183void LCompareNumericAndBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100184 stream->Add("if ");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000185 left()->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100186 stream->Add(" %s ", Token::String(op()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000187 right()->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100188 stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
189}
190
191
Ben Murdochb8e0da22011-05-16 14:20:40 +0100192void LIsObjectAndBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100193 stream->Add("if is_object(");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 value()->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100195 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
196}
197
198
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100199void LIsStringAndBranch::PrintDataTo(StringStream* stream) {
200 stream->Add("if is_string(");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000201 value()->PrintTo(stream);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100202 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
203}
204
205
Ben Murdochb8e0da22011-05-16 14:20:40 +0100206void LIsSmiAndBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100207 stream->Add("if is_smi(");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000208 value()->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100209 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
210}
211
212
Ben Murdoch257744e2011-11-30 15:57:28 +0000213void LIsUndetectableAndBranch::PrintDataTo(StringStream* stream) {
214 stream->Add("if is_undetectable(");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000215 value()->PrintTo(stream);
Ben Murdoch257744e2011-11-30 15:57:28 +0000216 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
217}
218
219
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100220void LStringCompareAndBranch::PrintDataTo(StringStream* stream) {
221 stream->Add("if string_compare(");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000222 left()->PrintTo(stream);
223 right()->PrintTo(stream);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100224 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
225}
226
227
Ben Murdochb8e0da22011-05-16 14:20:40 +0100228void LHasInstanceTypeAndBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100229 stream->Add("if has_instance_type(");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 value()->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100231 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
232}
233
234
Ben Murdochb8e0da22011-05-16 14:20:40 +0100235void LHasCachedArrayIndexAndBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100236 stream->Add("if has_cached_array_index(");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000237 value()->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100238 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
239}
240
241
Ben Murdochb8e0da22011-05-16 14:20:40 +0100242void LClassOfTestAndBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100243 stream->Add("if class_of_test(");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000244 value()->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100245 stream->Add(", \"%o\") then B%d else B%d",
246 *hydrogen()->class_name(),
247 true_block_id(),
248 false_block_id());
249}
250
251
Ben Murdochb8e0da22011-05-16 14:20:40 +0100252void LTypeofIsAndBranch::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100253 stream->Add("if typeof ");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000254 value()->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100255 stream->Add(" == \"%s\" then B%d else B%d",
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000256 hydrogen()->type_literal()->ToCString().get(),
Ben Murdochb0fe1622011-05-05 13:52:32 +0100257 true_block_id(), false_block_id());
258}
259
260
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000261void LStoreCodeEntry::PrintDataTo(StringStream* stream) {
262 stream->Add(" = ");
263 function()->PrintTo(stream);
264 stream->Add(".code_entry = ");
265 code_object()->PrintTo(stream);
266}
267
268
269void LInnerAllocatedObject::PrintDataTo(StringStream* stream) {
270 stream->Add(" = ");
271 base_object()->PrintTo(stream);
272 stream->Add(" + ");
273 offset()->PrintTo(stream);
274}
275
276
277void LCallJSFunction::PrintDataTo(StringStream* stream) {
278 stream->Add("= ");
279 function()->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100280 stream->Add("#%d / ", arity());
281}
282
283
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000284void LCallWithDescriptor::PrintDataTo(StringStream* stream) {
285 for (int i = 0; i < InputCount(); i++) {
286 InputAt(i)->PrintTo(stream);
287 stream->Add(" ");
288 }
289 stream->Add("#%d / ", arity());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100290}
291
292
Ben Murdochb8e0da22011-05-16 14:20:40 +0100293void LLoadContextSlot::PrintDataTo(StringStream* stream) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000294 context()->PrintTo(stream);
Steve Block1e0659c2011-05-24 12:43:12 +0100295 stream->Add("[%d]", slot_index());
296}
297
298
299void LStoreContextSlot::PrintDataTo(StringStream* stream) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000300 context()->PrintTo(stream);
Steve Block1e0659c2011-05-24 12:43:12 +0100301 stream->Add("[%d] <- ", slot_index());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302 value()->PrintTo(stream);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100303}
304
305
Ben Murdoch257744e2011-11-30 15:57:28 +0000306void LInvokeFunction::PrintDataTo(StringStream* stream) {
307 stream->Add("= ");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000308 context()->PrintTo(stream);
Ben Murdoch257744e2011-11-30 15:57:28 +0000309 stream->Add(" ");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000310 function()->PrintTo(stream);
Ben Murdoch257744e2011-11-30 15:57:28 +0000311 stream->Add(" #%d / ", arity());
312}
313
314
Ben Murdochb8e0da22011-05-16 14:20:40 +0100315void LCallNew::PrintDataTo(StringStream* stream) {
316 stream->Add("= ");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317 context()->PrintTo(stream);
318 stream->Add(" ");
319 constructor()->PrintTo(stream);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100320 stream->Add(" #%d / ", arity());
321}
322
323
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000324void LCallNewArray::PrintDataTo(StringStream* stream) {
325 stream->Add("= ");
326 context()->PrintTo(stream);
327 stream->Add(" ");
328 constructor()->PrintTo(stream);
329 stream->Add(" #%d / ", arity());
330 ElementsKind kind = hydrogen()->elements_kind();
331 stream->Add(" (%s) ", ElementsKindToString(kind));
332}
333
334
Ben Murdochb8e0da22011-05-16 14:20:40 +0100335void LAccessArgumentsAt::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100336 arguments()->PrintTo(stream);
337
338 stream->Add(" length ");
339 length()->PrintTo(stream);
340
341 stream->Add(" index ");
342 index()->PrintTo(stream);
343}
344
345
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000346int LPlatformChunk::GetNextSpillIndex(RegisterKind kind) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100347 // Skip a slot if for a double-width slot.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000348 if (kind == DOUBLE_REGISTERS) {
349 spill_slot_count_++;
350 spill_slot_count_ |= 1;
351 num_double_slots_++;
352 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100353 return spill_slot_count_++;
354}
355
356
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000357LOperand* LPlatformChunk::GetNextSpillSlot(RegisterKind kind) {
358 int index = GetNextSpillIndex(kind);
359 if (kind == DOUBLE_REGISTERS) {
360 return LDoubleStackSlot::Create(index, zone());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100361 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000362 DCHECK(kind == GENERAL_REGISTERS);
363 return LStackSlot::Create(index, zone());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100364 }
365}
366
367
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100368void LStoreNamedField::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100369 object()->PrintTo(stream);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400370 std::ostringstream os;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000371 os << hydrogen()->access() << " <- ";
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400372 stream->Add(os.str().c_str());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100373 value()->PrintTo(stream);
374}
375
376
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100377void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
378 object()->PrintTo(stream);
379 stream->Add(".");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000380 stream->Add(String::cast(*name())->ToCString().get());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100381 stream->Add(" <- ");
382 value()->PrintTo(stream);
383}
384
385
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000386void LLoadKeyed::PrintDataTo(StringStream* stream) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000387 elements()->PrintTo(stream);
388 stream->Add("[");
389 key()->PrintTo(stream);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000390 if (hydrogen()->IsDehoisted()) {
391 stream->Add(" + %d]", base_offset());
392 } else {
393 stream->Add("]");
394 }
395}
396
397
398void LStoreKeyed::PrintDataTo(StringStream* stream) {
399 elements()->PrintTo(stream);
400 stream->Add("[");
401 key()->PrintTo(stream);
402 if (hydrogen()->IsDehoisted()) {
403 stream->Add(" + %d] <-", base_offset());
404 } else {
405 stream->Add("] <- ");
406 }
407
408 if (value() == NULL) {
409 DCHECK(hydrogen()->IsConstantHoleStore() &&
410 hydrogen()->value()->representation().IsDouble());
411 stream->Add("<the hole(nan)>");
412 } else {
413 value()->PrintTo(stream);
414 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000415}
416
417
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100418void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100419 object()->PrintTo(stream);
420 stream->Add("[");
421 key()->PrintTo(stream);
422 stream->Add("] <- ");
423 value()->PrintTo(stream);
424}
425
426
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100427void LTransitionElementsKind::PrintDataTo(StringStream* stream) {
428 object()->PrintTo(stream);
429 stream->Add(" %p -> %p", *original_map(), *transitioned_map());
430}
431
432
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000433LPlatformChunk* LChunkBuilder::Build() {
434 DCHECK(is_unused());
435 chunk_ = new(zone()) LPlatformChunk(info(), graph());
436 LPhase phase("L_Building chunk", chunk_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100437 status_ = BUILDING;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000438
439 // Reserve the first spill slot for the state of dynamic alignment.
440 if (info()->IsOptimizing()) {
441 int alignment_state_index = chunk_->GetNextSpillIndex(GENERAL_REGISTERS);
442 DCHECK_EQ(alignment_state_index, 0);
443 USE(alignment_state_index);
444 }
445
446 // If compiling for OSR, reserve space for the unoptimized frame,
447 // which will be subsumed into this frame.
448 if (graph()->has_osr()) {
449 for (int i = graph()->osr()->UnoptimizedFrameSlots(); i > 0; i--) {
450 chunk_->GetNextSpillIndex(GENERAL_REGISTERS);
451 }
452 }
453
Ben Murdochb0fe1622011-05-05 13:52:32 +0100454 const ZoneList<HBasicBlock*>* blocks = graph()->blocks();
455 for (int i = 0; i < blocks->length(); i++) {
456 HBasicBlock* next = NULL;
457 if (i < blocks->length() - 1) next = blocks->at(i + 1);
458 DoBasicBlock(blocks->at(i), next);
459 if (is_aborted()) return NULL;
460 }
461 status_ = DONE;
462 return chunk_;
463}
464
465
Ben Murdochb0fe1622011-05-05 13:52:32 +0100466LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100467 return new(zone()) LUnallocated(LUnallocated::FIXED_REGISTER,
468 Register::ToAllocationIndex(reg));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100469}
470
471
472LUnallocated* LChunkBuilder::ToUnallocated(XMMRegister reg) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100473 return new(zone()) LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
474 XMMRegister::ToAllocationIndex(reg));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100475}
476
477
478LOperand* LChunkBuilder::UseFixed(HValue* value, Register fixed_register) {
479 return Use(value, ToUnallocated(fixed_register));
480}
481
482
483LOperand* LChunkBuilder::UseFixedDouble(HValue* value, XMMRegister reg) {
484 return Use(value, ToUnallocated(reg));
485}
486
487
488LOperand* LChunkBuilder::UseRegister(HValue* value) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100489 return Use(value, new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100490}
491
492
493LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
494 return Use(value,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100495 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
496 LUnallocated::USED_AT_START));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100497}
498
499
500LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100501 return Use(value, new(zone()) LUnallocated(LUnallocated::WRITABLE_REGISTER));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100502}
503
504
505LOperand* LChunkBuilder::Use(HValue* value) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100506 return Use(value, new(zone()) LUnallocated(LUnallocated::NONE));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100507}
508
509
510LOperand* LChunkBuilder::UseAtStart(HValue* value) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100511 return Use(value, new(zone()) LUnallocated(LUnallocated::NONE,
512 LUnallocated::USED_AT_START));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100513}
514
515
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000516static inline bool CanBeImmediateConstant(HValue* value) {
517 return value->IsConstant() && HConstant::cast(value)->NotInNewSpace();
518}
519
520
Ben Murdochb0fe1622011-05-05 13:52:32 +0100521LOperand* LChunkBuilder::UseOrConstant(HValue* value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000522 return CanBeImmediateConstant(value)
Ben Murdochb0fe1622011-05-05 13:52:32 +0100523 ? chunk_->DefineConstantOperand(HConstant::cast(value))
524 : Use(value);
525}
526
527
528LOperand* LChunkBuilder::UseOrConstantAtStart(HValue* value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000529 return CanBeImmediateConstant(value)
Ben Murdochb0fe1622011-05-05 13:52:32 +0100530 ? chunk_->DefineConstantOperand(HConstant::cast(value))
531 : UseAtStart(value);
532}
533
534
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000535LOperand* LChunkBuilder::UseFixedOrConstant(HValue* value,
536 Register fixed_register) {
537 return CanBeImmediateConstant(value)
538 ? chunk_->DefineConstantOperand(HConstant::cast(value))
539 : UseFixed(value, fixed_register);
540}
541
542
Ben Murdochb0fe1622011-05-05 13:52:32 +0100543LOperand* LChunkBuilder::UseRegisterOrConstant(HValue* value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000544 return CanBeImmediateConstant(value)
Ben Murdochb0fe1622011-05-05 13:52:32 +0100545 ? chunk_->DefineConstantOperand(HConstant::cast(value))
546 : UseRegister(value);
547}
548
549
550LOperand* LChunkBuilder::UseRegisterOrConstantAtStart(HValue* value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000551 return CanBeImmediateConstant(value)
Ben Murdochb0fe1622011-05-05 13:52:32 +0100552 ? chunk_->DefineConstantOperand(HConstant::cast(value))
553 : UseRegisterAtStart(value);
554}
555
556
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557LOperand* LChunkBuilder::UseConstant(HValue* value) {
558 return chunk_->DefineConstantOperand(HConstant::cast(value));
559}
560
561
Ben Murdochb8e0da22011-05-16 14:20:40 +0100562LOperand* LChunkBuilder::UseAny(HValue* value) {
563 return value->IsConstant()
564 ? chunk_->DefineConstantOperand(HConstant::cast(value))
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100565 : Use(value, new(zone()) LUnallocated(LUnallocated::ANY));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100566}
567
568
Ben Murdochb0fe1622011-05-05 13:52:32 +0100569LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
570 if (value->EmitAtUses()) {
571 HInstruction* instr = HInstruction::cast(value);
572 VisitInstruction(instr);
573 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100574 operand->set_virtual_register(value->id());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100575 return operand;
576}
577
578
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000579LInstruction* LChunkBuilder::Define(LTemplateResultInstruction<1>* instr,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100580 LUnallocated* result) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100581 result->set_virtual_register(current_instruction_->id());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100582 instr->set_result(result);
583 return instr;
584}
585
586
Ben Murdochb8e0da22011-05-16 14:20:40 +0100587LInstruction* LChunkBuilder::DefineAsRegister(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000588 LTemplateResultInstruction<1>* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100589 return Define(instr,
590 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100591}
592
593
Ben Murdochb8e0da22011-05-16 14:20:40 +0100594LInstruction* LChunkBuilder::DefineAsSpilled(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000595 LTemplateResultInstruction<1>* instr,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100596 int index) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100597 return Define(instr,
598 new(zone()) LUnallocated(LUnallocated::FIXED_SLOT, index));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100599}
600
601
Ben Murdochb8e0da22011-05-16 14:20:40 +0100602LInstruction* LChunkBuilder::DefineSameAsFirst(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000603 LTemplateResultInstruction<1>* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100604 return Define(instr,
605 new(zone()) LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100606}
607
608
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000609LInstruction* LChunkBuilder::DefineFixed(LTemplateResultInstruction<1>* instr,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100610 Register reg) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100611 return Define(instr, ToUnallocated(reg));
612}
613
614
Ben Murdochb8e0da22011-05-16 14:20:40 +0100615LInstruction* LChunkBuilder::DefineFixedDouble(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000616 LTemplateResultInstruction<1>* instr,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100617 XMMRegister reg) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100618 return Define(instr, ToUnallocated(reg));
619}
620
621
622LInstruction* LChunkBuilder::AssignEnvironment(LInstruction* instr) {
623 HEnvironment* hydrogen_env = current_block_->last_environment();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100624 int argument_index_accumulator = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000625 ZoneList<HValue*> objects_to_materialize(0, zone());
626 instr->set_environment(CreateEnvironment(
627 hydrogen_env, &argument_index_accumulator, &objects_to_materialize));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100628 return instr;
629}
630
631
Ben Murdochb0fe1622011-05-05 13:52:32 +0100632LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
633 HInstruction* hinstr,
634 CanDeoptimize can_deoptimize) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000635 info()->MarkAsNonDeferredCalling();
636
Steve Block1e0659c2011-05-24 12:43:12 +0100637#ifdef DEBUG
638 instr->VerifyCall();
639#endif
640 instr->MarkAsCall();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100641 instr = AssignPointerMap(instr);
642
Ben Murdochb0fe1622011-05-05 13:52:32 +0100643 // If instruction does not have side-effects lazy deoptimization
644 // after the call will try to deoptimize to the point before the call.
645 // Thus we still need to attach environment to this call even if
646 // call sequence can not deoptimize eagerly.
647 bool needs_environment =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100648 (can_deoptimize == CAN_DEOPTIMIZE_EAGERLY) ||
649 !hinstr->HasObservableSideEffects();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100650 if (needs_environment && !instr->HasEnvironment()) {
651 instr = AssignEnvironment(instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000652 // We can't really figure out if the environment is needed or not.
653 instr->environment()->set_has_been_used();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100654 }
655
656 return instr;
657}
658
659
660LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000661 DCHECK(!instr->HasPointerMap());
662 instr->set_pointer_map(new(zone()) LPointerMap(zone()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100663 return instr;
664}
665
666
Ben Murdochb0fe1622011-05-05 13:52:32 +0100667LUnallocated* LChunkBuilder::TempRegister() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100668 LUnallocated* operand =
669 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000670 int vreg = allocator_->GetVirtualRegister();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100671 if (!allocator_->AllocationOk()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000672 Abort(kOutOfVirtualRegistersWhileTryingToAllocateTempRegister);
673 vreg = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100674 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000675 operand->set_virtual_register(vreg);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100676 return operand;
677}
678
679
680LOperand* LChunkBuilder::FixedTemp(Register reg) {
681 LUnallocated* operand = ToUnallocated(reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000682 DCHECK(operand->HasFixedPolicy());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100683 return operand;
684}
685
686
687LOperand* LChunkBuilder::FixedTemp(XMMRegister reg) {
688 LUnallocated* operand = ToUnallocated(reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000689 DCHECK(operand->HasFixedPolicy());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100690 return operand;
691}
692
693
694LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100695 return new(zone()) LLabel(instr->block());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100696}
697
698
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000699LInstruction* LChunkBuilder::DoDummyUse(HDummyUse* instr) {
700 return DefineAsRegister(new(zone()) LDummyUse(UseAny(instr->value())));
701}
702
703
704LInstruction* LChunkBuilder::DoEnvironmentMarker(HEnvironmentMarker* instr) {
705 UNREACHABLE();
706 return NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000707}
708
709
Ben Murdochb0fe1622011-05-05 13:52:32 +0100710LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100711 return AssignEnvironment(new(zone()) LDeoptimize);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100712}
713
714
715LInstruction* LChunkBuilder::DoShift(Token::Value op,
716 HBitwiseBinaryOperation* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000717 if (instr->representation().IsSmiOrInteger32()) {
718 DCHECK(instr->left()->representation().Equals(instr->representation()));
719 DCHECK(instr->right()->representation().Equals(instr->representation()));
720 LOperand* left = UseRegisterAtStart(instr->left());
Steve Block1e0659c2011-05-24 12:43:12 +0100721
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000722 HValue* right_value = instr->right();
723 LOperand* right = NULL;
724 int constant_value = 0;
725 bool does_deopt = false;
726 if (right_value->IsConstant()) {
727 HConstant* constant = HConstant::cast(right_value);
728 right = chunk_->DefineConstantOperand(constant);
729 constant_value = constant->Integer32Value() & 0x1f;
730 // Left shifts can deoptimize if we shift by > 0 and the result cannot be
731 // truncated to smi.
732 if (instr->representation().IsSmi() && constant_value > 0) {
733 does_deopt = !instr->CheckUsesForFlag(HValue::kTruncatingToSmi);
734 }
735 } else {
736 right = UseFixed(right_value, ecx);
737 }
Steve Block1e0659c2011-05-24 12:43:12 +0100738
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000739 // Shift operations can only deoptimize if we do a logical shift by 0 and
740 // the result cannot be truncated to int32.
741 if (op == Token::SHR && constant_value == 0) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400742 does_deopt = !instr->CheckFlag(HInstruction::kUint32);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100743 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100744
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000745 LInstruction* result =
746 DefineSameAsFirst(new(zone()) LShiftI(op, left, right, does_deopt));
747 return does_deopt ? AssignEnvironment(result) : result;
748 } else {
749 return DoArithmeticT(op, instr);
750 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100751}
752
753
754LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
755 HArithmeticBinaryOperation* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000756 DCHECK(instr->representation().IsDouble());
757 DCHECK(instr->left()->representation().IsDouble());
758 DCHECK(instr->right()->representation().IsDouble());
759 if (op == Token::MOD) {
760 LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
761 LOperand* right = UseRegisterAtStart(instr->BetterRightOperand());
762 LArithmeticD* result = new(zone()) LArithmeticD(op, left, right);
763 return MarkAsCall(DefineSameAsFirst(result), instr);
764 } else {
765 LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
766 LOperand* right = UseRegisterAtStart(instr->BetterRightOperand());
767 LArithmeticD* result = new(zone()) LArithmeticD(op, left, right);
768 return DefineSameAsFirst(result);
769 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100770}
771
772
773LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000774 HBinaryOperation* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100775 HValue* left = instr->left();
776 HValue* right = instr->right();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000777 DCHECK(left->representation().IsTagged());
778 DCHECK(right->representation().IsTagged());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000779 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100780 LOperand* left_operand = UseFixed(left, edx);
781 LOperand* right_operand = UseFixed(right, eax);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000782 LArithmeticT* result =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100783 new(zone()) LArithmeticT(op, context, left_operand, right_operand);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100784 return MarkAsCall(DefineFixed(result, eax), instr);
785}
786
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000787
Ben Murdochb0fe1622011-05-05 13:52:32 +0100788void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000789 DCHECK(is_building());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100790 current_block_ = block;
791 next_block_ = next_block;
792 if (block->IsStartBlock()) {
793 block->UpdateEnvironment(graph_->start_environment());
794 argument_count_ = 0;
795 } else if (block->predecessors()->length() == 1) {
796 // We have a single predecessor => copy environment and outgoing
797 // argument count from the predecessor.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000798 DCHECK(block->phis()->length() == 0);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100799 HBasicBlock* pred = block->predecessors()->at(0);
800 HEnvironment* last_environment = pred->last_environment();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000801 DCHECK(last_environment != NULL);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100802 // Only copy the environment, if it is later used again.
803 if (pred->end()->SecondSuccessor() == NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000804 DCHECK(pred->end()->FirstSuccessor() == block);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100805 } else {
806 if (pred->end()->FirstSuccessor()->block_id() > block->block_id() ||
807 pred->end()->SecondSuccessor()->block_id() > block->block_id()) {
808 last_environment = last_environment->Copy();
809 }
810 }
811 block->UpdateEnvironment(last_environment);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000812 DCHECK(pred->argument_count() >= 0);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100813 argument_count_ = pred->argument_count();
814 } else {
815 // We are at a state join => process phis.
816 HBasicBlock* pred = block->predecessors()->at(0);
817 // No need to copy the environment, it cannot be used later.
818 HEnvironment* last_environment = pred->last_environment();
819 for (int i = 0; i < block->phis()->length(); ++i) {
820 HPhi* phi = block->phis()->at(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000821 if (phi->HasMergedIndex()) {
822 last_environment->SetValueAt(phi->merged_index(), phi);
823 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100824 }
825 for (int i = 0; i < block->deleted_phis()->length(); ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000826 if (block->deleted_phis()->at(i) < last_environment->length()) {
827 last_environment->SetValueAt(block->deleted_phis()->at(i),
828 graph_->GetConstantUndefined());
829 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100830 }
831 block->UpdateEnvironment(last_environment);
832 // Pick up the outgoing argument count of one of the predecessors.
833 argument_count_ = pred->argument_count();
834 }
835 HInstruction* current = block->first();
836 int start = chunk_->instructions()->length();
837 while (current != NULL && !is_aborted()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100838 // Code for constants in registers is generated lazily.
839 if (!current->EmitAtUses()) {
840 VisitInstruction(current);
841 }
842 current = current->next();
843 }
844 int end = chunk_->instructions()->length() - 1;
845 if (end >= start) {
846 block->set_first_instruction_index(start);
847 block->set_last_instruction_index(end);
848 }
849 block->set_argument_count(argument_count_);
850 next_block_ = NULL;
851 current_block_ = NULL;
852}
853
854
855void LChunkBuilder::VisitInstruction(HInstruction* current) {
856 HInstruction* old_current = current_instruction_;
857 current_instruction_ = current;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000858
859 LInstruction* instr = NULL;
860 if (current->CanReplaceWithDummyUses()) {
861 if (current->OperandCount() == 0) {
862 instr = DefineAsRegister(new(zone()) LDummy());
863 } else {
864 DCHECK(!current->OperandAt(0)->IsControlInstruction());
865 instr = DefineAsRegister(new(zone())
866 LDummyUse(UseAny(current->OperandAt(0))));
867 }
868 for (int i = 1; i < current->OperandCount(); ++i) {
869 if (current->OperandAt(i)->IsControlInstruction()) continue;
870 LInstruction* dummy =
871 new(zone()) LDummyUse(UseAny(current->OperandAt(i)));
872 dummy->set_hydrogen_value(current);
873 chunk_->AddInstruction(dummy, current_block_);
874 }
875 } else {
876 HBasicBlock* successor;
877 if (current->IsControlInstruction() &&
878 HControlInstruction::cast(current)->KnownSuccessorBlock(&successor) &&
879 successor != NULL) {
880 instr = new(zone()) LGoto(successor);
881 } else {
882 instr = current->CompileToLithium(this);
883 }
884 }
885
886 argument_count_ += current->argument_delta();
887 DCHECK(argument_count_ >= 0);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100888
889 if (instr != NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000890 AddInstruction(instr, current);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100891 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000892
Ben Murdochb0fe1622011-05-05 13:52:32 +0100893 current_instruction_ = old_current;
894}
895
896
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000897void LChunkBuilder::AddInstruction(LInstruction* instr,
898 HInstruction* hydrogen_val) {
899 // Associate the hydrogen instruction first, since we may need it for
900 // the ClobbersRegisters() or ClobbersDoubleRegisters() calls below.
901 instr->set_hydrogen_value(hydrogen_val);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100902
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000903#if DEBUG
904 // Make sure that the lithium instruction has either no fixed register
905 // constraints in temps or the result OR no uses that are only used at
906 // start. If this invariant doesn't hold, the register allocator can decide
907 // to insert a split of a range immediately before the instruction due to an
908 // already allocated register needing to be used for the instruction's fixed
909 // register constraint. In this case, The register allocator won't see an
910 // interference between the split child and the use-at-start (it would if
911 // the it was just a plain use), so it is free to move the split child into
912 // the same register that is used for the use-at-start.
913 // See https://code.google.com/p/chromium/issues/detail?id=201590
914 if (!(instr->ClobbersRegisters() &&
915 instr->ClobbersDoubleRegisters(isolate()))) {
916 int fixed = 0;
917 int used_at_start = 0;
918 for (UseIterator it(instr); !it.Done(); it.Advance()) {
919 LUnallocated* operand = LUnallocated::cast(it.Current());
920 if (operand->IsUsedAtStart()) ++used_at_start;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100921 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000922 if (instr->Output() != NULL) {
923 if (LUnallocated::cast(instr->Output())->HasFixedPolicy()) ++fixed;
924 }
925 for (TempIterator it(instr); !it.Done(); it.Advance()) {
926 LUnallocated* operand = LUnallocated::cast(it.Current());
927 if (operand->HasFixedPolicy()) ++fixed;
928 }
929 DCHECK(fixed == 0 || used_at_start == 0);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100930 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000931#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +0100932
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000933 if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
934 instr = AssignPointerMap(instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100935 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000936 if (FLAG_stress_environments && !instr->HasEnvironment()) {
937 instr = AssignEnvironment(instr);
938 }
939 chunk_->AddInstruction(instr, current_block_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100940
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000941 if (instr->IsCall()) {
942 HValue* hydrogen_value_for_lazy_bailout = hydrogen_val;
943 LInstruction* instruction_needing_environment = NULL;
944 if (hydrogen_val->HasObservableSideEffects()) {
945 HSimulate* sim = HSimulate::cast(hydrogen_val->next());
946 instruction_needing_environment = instr;
947 sim->ReplayEnvironment(current_block_->last_environment());
948 hydrogen_value_for_lazy_bailout = sim;
949 }
950 LInstruction* bailout = AssignEnvironment(new(zone()) LLazyBailout());
951 bailout->set_hydrogen_value(hydrogen_value_for_lazy_bailout);
952 chunk_->AddInstruction(bailout, current_block_);
953 if (instruction_needing_environment != NULL) {
954 // Store the lazy deopt environment with the instruction if needed.
955 // Right now it is only used for LInstanceOfKnownGlobal.
956 instruction_needing_environment->
957 SetDeferredLazyDeoptimizationEnvironment(bailout->environment());
958 }
959 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100960}
961
962
963LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000964 return new(zone()) LGoto(instr->FirstSuccessor());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100965}
966
967
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000968LInstruction* LChunkBuilder::DoBranch(HBranch* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100969 HValue* value = instr->value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000970 Representation r = value->representation();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100971 HType type = value->type();
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000972 ToBooleanStub::Types expected = instr->expected_input_types();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000973 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
974
975 bool easy_case = !r.IsTagged() || type.IsBoolean() || type.IsSmi() ||
976 type.IsJSArray() || type.IsHeapNumber() || type.IsString();
977 LOperand* temp = !easy_case && expected.NeedsMap() ? TempRegister() : NULL;
978 LInstruction* branch = new(zone()) LBranch(UseRegister(value), temp);
979 if (!easy_case &&
980 ((!expected.Contains(ToBooleanStub::SMI) && expected.NeedsMap()) ||
981 !expected.IsGeneric())) {
982 branch = AssignEnvironment(branch);
983 }
984 return branch;
985}
986
987
988LInstruction* LChunkBuilder::DoDebugBreak(HDebugBreak* instr) {
989 return new(zone()) LDebugBreak();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100990}
991
992
Steve Block1e0659c2011-05-24 12:43:12 +0100993LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000994 DCHECK(instr->value()->representation().IsTagged());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100995 LOperand* value = UseRegisterAtStart(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100996 return new(zone()) LCmpMapAndBranch(value);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100997}
998
999
1000LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* length) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001001 info()->MarkAsRequiresFrame();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001002 return DefineAsRegister(new(zone()) LArgumentsLength(Use(length->value())));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001003}
1004
1005
1006LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001007 info()->MarkAsRequiresFrame();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001008 return DefineAsRegister(new(zone()) LArgumentsElements);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001009}
1010
1011
1012LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001013 LOperand* left = UseFixed(instr->left(), InstanceofStub::left());
1014 LOperand* right = UseFixed(instr->right(), InstanceofStub::right());
1015 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001016 LInstanceOf* result = new(zone()) LInstanceOf(context, left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001017 return MarkAsCall(DefineFixed(result, eax), instr);
1018}
1019
1020
Ben Murdoch086aeea2011-05-13 15:57:08 +01001021LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
1022 HInstanceOfKnownGlobal* instr) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01001023 LInstanceOfKnownGlobal* result =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001024 new(zone()) LInstanceOfKnownGlobal(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001025 UseFixed(instr->context(), esi),
1026 UseFixed(instr->left(), InstanceofStub::left()),
Ben Murdoch086aeea2011-05-13 15:57:08 +01001027 FixedTemp(edi));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001028 return MarkAsCall(DefineFixed(result, eax), instr);
Ben Murdoch086aeea2011-05-13 15:57:08 +01001029}
1030
1031
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001032LInstruction* LChunkBuilder::DoWrapReceiver(HWrapReceiver* instr) {
1033 LOperand* receiver = UseRegister(instr->receiver());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001034 LOperand* function = UseRegister(instr->function());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001035 LOperand* temp = TempRegister();
1036 LWrapReceiver* result =
1037 new(zone()) LWrapReceiver(receiver, function, temp);
1038 return AssignEnvironment(DefineSameAsFirst(result));
1039}
1040
1041
Ben Murdochb0fe1622011-05-05 13:52:32 +01001042LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
1043 LOperand* function = UseFixed(instr->function(), edi);
1044 LOperand* receiver = UseFixed(instr->receiver(), eax);
Steve Block1e0659c2011-05-24 12:43:12 +01001045 LOperand* length = UseFixed(instr->length(), ebx);
1046 LOperand* elements = UseFixed(instr->elements(), ecx);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001047 LApplyArguments* result = new(zone()) LApplyArguments(function,
1048 receiver,
1049 length,
1050 elements);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001051 return MarkAsCall(DefineFixed(result, eax), instr, CAN_DEOPTIMIZE_EAGERLY);
1052}
1053
1054
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001055LInstruction* LChunkBuilder::DoPushArguments(HPushArguments* instr) {
1056 int argc = instr->OperandCount();
1057 for (int i = 0; i < argc; ++i) {
1058 LOperand* argument = UseAny(instr->argument(i));
1059 AddInstruction(new(zone()) LPushArgument(argument), instr);
1060 }
1061 return NULL;
1062}
1063
1064
1065LInstruction* LChunkBuilder::DoStoreCodeEntry(
1066 HStoreCodeEntry* store_code_entry) {
1067 LOperand* function = UseRegister(store_code_entry->function());
1068 LOperand* code_object = UseTempRegister(store_code_entry->code_object());
1069 return new(zone()) LStoreCodeEntry(function, code_object);
1070}
1071
1072
1073LInstruction* LChunkBuilder::DoInnerAllocatedObject(
1074 HInnerAllocatedObject* instr) {
1075 LOperand* base_object = UseRegisterAtStart(instr->base_object());
1076 LOperand* offset = UseRegisterOrConstantAtStart(instr->offset());
1077 return DefineAsRegister(
1078 new(zone()) LInnerAllocatedObject(base_object, offset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001079}
1080
1081
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001082LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001083 return instr->HasNoUses()
1084 ? NULL
1085 : DefineAsRegister(new(zone()) LThisFunction);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001086}
1087
1088
Steve Block1e0659c2011-05-24 12:43:12 +01001089LInstruction* LChunkBuilder::DoContext(HContext* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001090 if (instr->HasNoUses()) return NULL;
Steve Block1e0659c2011-05-24 12:43:12 +01001091
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001092 if (info()->IsStub()) {
1093 return DefineFixed(new(zone()) LContext, esi);
1094 }
Steve Block1e0659c2011-05-24 12:43:12 +01001095
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001096 return DefineAsRegister(new(zone()) LContext);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001097}
1098
1099
1100LInstruction* LChunkBuilder::DoDeclareGlobals(HDeclareGlobals* instr) {
1101 LOperand* context = UseFixed(instr->context(), esi);
1102 return MarkAsCall(new(zone()) LDeclareGlobals(context), instr);
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001103}
1104
1105
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001106LInstruction* LChunkBuilder::DoCallJSFunction(
1107 HCallJSFunction* instr) {
1108 LOperand* function = UseFixed(instr->function(), edi);
1109
1110 LCallJSFunction* result = new(zone()) LCallJSFunction(function);
1111
1112 return MarkAsCall(DefineFixed(result, eax), instr, CANNOT_DEOPTIMIZE_EAGERLY);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001113}
1114
1115
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001116LInstruction* LChunkBuilder::DoCallWithDescriptor(
1117 HCallWithDescriptor* instr) {
1118 CallInterfaceDescriptor descriptor = instr->descriptor();
1119 LOperand* target = UseRegisterOrConstantAtStart(instr->target());
1120 ZoneList<LOperand*> ops(instr->OperandCount(), zone());
1121 ops.Add(target, zone());
1122 for (int i = 1; i < instr->OperandCount(); i++) {
1123 LOperand* op =
1124 UseFixed(instr->OperandAt(i), descriptor.GetParameterRegister(i - 1));
1125 ops.Add(op, zone());
1126 }
1127
1128 LCallWithDescriptor* result = new(zone()) LCallWithDescriptor(
1129 descriptor, ops, zone());
1130 return MarkAsCall(DefineFixed(result, eax), instr, CANNOT_DEOPTIMIZE_EAGERLY);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001131}
1132
1133
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001134LInstruction* LChunkBuilder::DoTailCallThroughMegamorphicCache(
1135 HTailCallThroughMegamorphicCache* instr) {
1136 LOperand* context = UseFixed(instr->context(), esi);
1137 LOperand* receiver_register =
1138 UseFixed(instr->receiver(), LoadDescriptor::ReceiverRegister());
1139 LOperand* name_register =
1140 UseFixed(instr->name(), LoadDescriptor::NameRegister());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001141 LOperand* slot = NULL;
1142 LOperand* vector = NULL;
1143 if (FLAG_vector_ics) {
1144 slot = UseFixed(instr->slot(), VectorLoadICDescriptor::SlotRegister());
1145 vector =
1146 UseFixed(instr->vector(), VectorLoadICDescriptor::VectorRegister());
1147 }
1148
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001149 // Not marked as call. It can't deoptimize, and it never returns.
1150 return new (zone()) LTailCallThroughMegamorphicCache(
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001151 context, receiver_register, name_register, slot, vector);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001152}
1153
1154
Ben Murdoch257744e2011-11-30 15:57:28 +00001155LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1156 LOperand* context = UseFixed(instr->context(), esi);
1157 LOperand* function = UseFixed(instr->function(), edi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001158 LInvokeFunction* result = new(zone()) LInvokeFunction(context, function);
Ben Murdoch257744e2011-11-30 15:57:28 +00001159 return MarkAsCall(DefineFixed(result, eax), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1160}
1161
1162
Ben Murdochb0fe1622011-05-05 13:52:32 +01001163LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001164 switch (instr->op()) {
1165 case kMathFloor:
1166 return DoMathFloor(instr);
1167 case kMathRound:
1168 return DoMathRound(instr);
1169 case kMathFround:
1170 return DoMathFround(instr);
1171 case kMathAbs:
1172 return DoMathAbs(instr);
1173 case kMathLog:
1174 return DoMathLog(instr);
1175 case kMathExp:
1176 return DoMathExp(instr);
1177 case kMathSqrt:
1178 return DoMathSqrt(instr);
1179 case kMathPowHalf:
1180 return DoMathPowHalf(instr);
1181 case kMathClz32:
1182 return DoMathClz32(instr);
1183 default:
1184 UNREACHABLE();
1185 return NULL;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001186 }
1187}
1188
1189
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001190LInstruction* LChunkBuilder::DoMathFloor(HUnaryMathOperation* instr) {
1191 LOperand* input = UseRegisterAtStart(instr->value());
1192 LMathFloor* result = new(zone()) LMathFloor(input);
1193 return AssignEnvironment(DefineAsRegister(result));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001194}
1195
1196
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001197LInstruction* LChunkBuilder::DoMathRound(HUnaryMathOperation* instr) {
1198 LOperand* input = UseRegister(instr->value());
1199 LOperand* temp = FixedTemp(xmm4);
1200 LMathRound* result = new(zone()) LMathRound(input, temp);
1201 return AssignEnvironment(DefineAsRegister(result));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001202}
1203
1204
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001205LInstruction* LChunkBuilder::DoMathFround(HUnaryMathOperation* instr) {
1206 LOperand* input = UseRegister(instr->value());
1207 LMathFround* result = new (zone()) LMathFround(input);
1208 return DefineAsRegister(result);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001209}
1210
1211
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001212LInstruction* LChunkBuilder::DoMathAbs(HUnaryMathOperation* instr) {
1213 LOperand* context = UseAny(instr->context()); // Deferred use.
1214 LOperand* input = UseRegisterAtStart(instr->value());
1215 LInstruction* result =
1216 DefineSameAsFirst(new(zone()) LMathAbs(context, input));
1217 Representation r = instr->value()->representation();
1218 if (!r.IsDouble() && !r.IsSmiOrInteger32()) result = AssignPointerMap(result);
1219 if (!r.IsDouble()) result = AssignEnvironment(result);
1220 return result;
1221}
1222
1223
1224LInstruction* LChunkBuilder::DoMathLog(HUnaryMathOperation* instr) {
1225 DCHECK(instr->representation().IsDouble());
1226 DCHECK(instr->value()->representation().IsDouble());
1227 LOperand* input = UseRegisterAtStart(instr->value());
1228 return MarkAsCall(DefineSameAsFirst(new(zone()) LMathLog(input)), instr);
1229}
1230
1231
1232LInstruction* LChunkBuilder::DoMathClz32(HUnaryMathOperation* instr) {
1233 LOperand* input = UseRegisterAtStart(instr->value());
1234 LMathClz32* result = new(zone()) LMathClz32(input);
1235 return DefineAsRegister(result);
1236}
1237
1238
1239LInstruction* LChunkBuilder::DoMathExp(HUnaryMathOperation* instr) {
1240 DCHECK(instr->representation().IsDouble());
1241 DCHECK(instr->value()->representation().IsDouble());
1242 LOperand* value = UseTempRegister(instr->value());
1243 LOperand* temp1 = TempRegister();
1244 LOperand* temp2 = TempRegister();
1245 LMathExp* result = new(zone()) LMathExp(value, temp1, temp2);
1246 return DefineAsRegister(result);
1247}
1248
1249
1250LInstruction* LChunkBuilder::DoMathSqrt(HUnaryMathOperation* instr) {
1251 LOperand* input = UseAtStart(instr->value());
1252 return DefineAsRegister(new(zone()) LMathSqrt(input));
1253}
1254
1255
1256LInstruction* LChunkBuilder::DoMathPowHalf(HUnaryMathOperation* instr) {
1257 LOperand* input = UseRegisterAtStart(instr->value());
1258 LOperand* temp = TempRegister();
1259 LMathPowHalf* result = new(zone()) LMathPowHalf(input, temp);
1260 return DefineSameAsFirst(result);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001261}
1262
1263
1264LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001265 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001266 LOperand* constructor = UseFixed(instr->constructor(), edi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001267 LCallNew* result = new(zone()) LCallNew(context, constructor);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001268 return MarkAsCall(DefineFixed(result, eax), instr);
1269}
1270
1271
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001272LInstruction* LChunkBuilder::DoCallNewArray(HCallNewArray* instr) {
1273 LOperand* context = UseFixed(instr->context(), esi);
1274 LOperand* constructor = UseFixed(instr->constructor(), edi);
1275 LCallNewArray* result = new(zone()) LCallNewArray(context, constructor);
1276 return MarkAsCall(DefineFixed(result, eax), instr);
1277}
1278
1279
Ben Murdochb0fe1622011-05-05 13:52:32 +01001280LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01001281 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001282 LOperand* function = UseFixed(instr->function(), edi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001283 LCallFunction* call = new(zone()) LCallFunction(context, function);
1284 return MarkAsCall(DefineFixed(call, eax), instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001285}
1286
1287
1288LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001289 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001290 return MarkAsCall(DefineFixed(new(zone()) LCallRuntime(context), eax), instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001291}
1292
1293
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001294LInstruction* LChunkBuilder::DoRor(HRor* instr) {
1295 return DoShift(Token::ROR, instr);
1296}
1297
1298
Ben Murdochb0fe1622011-05-05 13:52:32 +01001299LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1300 return DoShift(Token::SHR, instr);
1301}
1302
1303
1304LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1305 return DoShift(Token::SAR, instr);
1306}
1307
1308
1309LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1310 return DoShift(Token::SHL, instr);
1311}
1312
1313
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001314LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001315 if (instr->representation().IsSmiOrInteger32()) {
1316 DCHECK(instr->left()->representation().Equals(instr->representation()));
1317 DCHECK(instr->right()->representation().Equals(instr->representation()));
1318 DCHECK(instr->CheckFlag(HValue::kTruncatingToInt32));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001319
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001320 LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
1321 LOperand* right = UseOrConstantAtStart(instr->BetterRightOperand());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001322 return DefineSameAsFirst(new(zone()) LBitI(left, right));
1323 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001324 return DoArithmeticT(instr->op(), instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001325 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001326}
1327
1328
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001329LInstruction* LChunkBuilder::DoDivByPowerOf2I(HDiv* instr) {
1330 DCHECK(instr->representation().IsSmiOrInteger32());
1331 DCHECK(instr->left()->representation().Equals(instr->representation()));
1332 DCHECK(instr->right()->representation().Equals(instr->representation()));
1333 LOperand* dividend = UseRegister(instr->left());
1334 int32_t divisor = instr->right()->GetInteger32Constant();
1335 LInstruction* result = DefineAsRegister(new(zone()) LDivByPowerOf2I(
1336 dividend, divisor));
1337 if ((instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1338 (instr->CheckFlag(HValue::kCanOverflow) && divisor == -1) ||
1339 (!instr->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1340 divisor != 1 && divisor != -1)) {
1341 result = AssignEnvironment(result);
1342 }
1343 return result;
1344}
1345
1346
1347LInstruction* LChunkBuilder::DoDivByConstI(HDiv* instr) {
1348 DCHECK(instr->representation().IsInteger32());
1349 DCHECK(instr->left()->representation().Equals(instr->representation()));
1350 DCHECK(instr->right()->representation().Equals(instr->representation()));
1351 LOperand* dividend = UseRegister(instr->left());
1352 int32_t divisor = instr->right()->GetInteger32Constant();
1353 LOperand* temp1 = FixedTemp(eax);
1354 LOperand* temp2 = FixedTemp(edx);
1355 LInstruction* result = DefineFixed(new(zone()) LDivByConstI(
1356 dividend, divisor, temp1, temp2), edx);
1357 if (divisor == 0 ||
1358 (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1359 !instr->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1360 result = AssignEnvironment(result);
1361 }
1362 return result;
1363}
1364
1365
1366LInstruction* LChunkBuilder::DoDivI(HDiv* instr) {
1367 DCHECK(instr->representation().IsSmiOrInteger32());
1368 DCHECK(instr->left()->representation().Equals(instr->representation()));
1369 DCHECK(instr->right()->representation().Equals(instr->representation()));
1370 LOperand* dividend = UseFixed(instr->left(), eax);
1371 LOperand* divisor = UseRegister(instr->right());
1372 LOperand* temp = FixedTemp(edx);
1373 LInstruction* result = DefineFixed(new(zone()) LDivI(
1374 dividend, divisor, temp), eax);
1375 if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1376 instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1377 instr->CheckFlag(HValue::kCanOverflow) ||
1378 !instr->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1379 result = AssignEnvironment(result);
1380 }
1381 return result;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001382}
1383
1384
Ben Murdochb0fe1622011-05-05 13:52:32 +01001385LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001386 if (instr->representation().IsSmiOrInteger32()) {
1387 if (instr->RightIsPowerOf2()) {
1388 return DoDivByPowerOf2I(instr);
1389 } else if (instr->right()->IsConstant()) {
1390 return DoDivByConstI(instr);
1391 } else {
1392 return DoDivI(instr);
1393 }
1394 } else if (instr->representation().IsDouble()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001395 return DoArithmeticD(Token::DIV, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001396 } else {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001397 return DoArithmeticT(Token::DIV, instr);
1398 }
1399}
1400
1401
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001402LInstruction* LChunkBuilder::DoFlooringDivByPowerOf2I(HMathFloorOfDiv* instr) {
1403 LOperand* dividend = UseRegisterAtStart(instr->left());
1404 int32_t divisor = instr->right()->GetInteger32Constant();
1405 LInstruction* result = DefineSameAsFirst(new(zone()) LFlooringDivByPowerOf2I(
1406 dividend, divisor));
1407 if ((instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1408 (instr->CheckFlag(HValue::kLeftCanBeMinInt) && divisor == -1)) {
1409 result = AssignEnvironment(result);
1410 }
1411 return result;
1412}
Steve Block44f0eee2011-05-26 01:26:41 +01001413
Steve Block44f0eee2011-05-26 01:26:41 +01001414
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001415LInstruction* LChunkBuilder::DoFlooringDivByConstI(HMathFloorOfDiv* instr) {
1416 DCHECK(instr->representation().IsInteger32());
1417 DCHECK(instr->left()->representation().Equals(instr->representation()));
1418 DCHECK(instr->right()->representation().Equals(instr->representation()));
1419 LOperand* dividend = UseRegister(instr->left());
1420 int32_t divisor = instr->right()->GetInteger32Constant();
1421 LOperand* temp1 = FixedTemp(eax);
1422 LOperand* temp2 = FixedTemp(edx);
1423 LOperand* temp3 =
1424 ((divisor > 0 && !instr->CheckFlag(HValue::kLeftCanBeNegative)) ||
1425 (divisor < 0 && !instr->CheckFlag(HValue::kLeftCanBePositive))) ?
1426 NULL : TempRegister();
1427 LInstruction* result =
1428 DefineFixed(new(zone()) LFlooringDivByConstI(dividend,
1429 divisor,
1430 temp1,
1431 temp2,
1432 temp3),
1433 edx);
1434 if (divisor == 0 ||
1435 (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0)) {
1436 result = AssignEnvironment(result);
1437 }
1438 return result;
1439}
1440
1441
1442LInstruction* LChunkBuilder::DoFlooringDivI(HMathFloorOfDiv* instr) {
1443 DCHECK(instr->representation().IsSmiOrInteger32());
1444 DCHECK(instr->left()->representation().Equals(instr->representation()));
1445 DCHECK(instr->right()->representation().Equals(instr->representation()));
1446 LOperand* dividend = UseFixed(instr->left(), eax);
1447 LOperand* divisor = UseRegister(instr->right());
1448 LOperand* temp = FixedTemp(edx);
1449 LInstruction* result = DefineFixed(new(zone()) LFlooringDivI(
1450 dividend, divisor, temp), eax);
1451 if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1452 instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1453 instr->CheckFlag(HValue::kCanOverflow)) {
1454 result = AssignEnvironment(result);
1455 }
1456 return result;
1457}
1458
1459
1460LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
1461 if (instr->RightIsPowerOf2()) {
1462 return DoFlooringDivByPowerOf2I(instr);
1463 } else if (instr->right()->IsConstant()) {
1464 return DoFlooringDivByConstI(instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001465 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001466 return DoFlooringDivI(instr);
1467 }
1468}
1469
1470
1471LInstruction* LChunkBuilder::DoModByPowerOf2I(HMod* instr) {
1472 DCHECK(instr->representation().IsSmiOrInteger32());
1473 DCHECK(instr->left()->representation().Equals(instr->representation()));
1474 DCHECK(instr->right()->representation().Equals(instr->representation()));
1475 LOperand* dividend = UseRegisterAtStart(instr->left());
1476 int32_t divisor = instr->right()->GetInteger32Constant();
1477 LInstruction* result = DefineSameAsFirst(new(zone()) LModByPowerOf2I(
1478 dividend, divisor));
1479 if (instr->CheckFlag(HValue::kLeftCanBeNegative) &&
1480 instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1481 result = AssignEnvironment(result);
1482 }
1483 return result;
1484}
1485
1486
1487LInstruction* LChunkBuilder::DoModByConstI(HMod* instr) {
1488 DCHECK(instr->representation().IsSmiOrInteger32());
1489 DCHECK(instr->left()->representation().Equals(instr->representation()));
1490 DCHECK(instr->right()->representation().Equals(instr->representation()));
1491 LOperand* dividend = UseRegister(instr->left());
1492 int32_t divisor = instr->right()->GetInteger32Constant();
1493 LOperand* temp1 = FixedTemp(eax);
1494 LOperand* temp2 = FixedTemp(edx);
1495 LInstruction* result = DefineFixed(new(zone()) LModByConstI(
1496 dividend, divisor, temp1, temp2), eax);
1497 if (divisor == 0 || instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1498 result = AssignEnvironment(result);
1499 }
1500 return result;
1501}
1502
1503
1504LInstruction* LChunkBuilder::DoModI(HMod* instr) {
1505 DCHECK(instr->representation().IsSmiOrInteger32());
1506 DCHECK(instr->left()->representation().Equals(instr->representation()));
1507 DCHECK(instr->right()->representation().Equals(instr->representation()));
1508 LOperand* dividend = UseFixed(instr->left(), eax);
1509 LOperand* divisor = UseRegister(instr->right());
1510 LOperand* temp = FixedTemp(edx);
1511 LInstruction* result = DefineFixed(new(zone()) LModI(
1512 dividend, divisor, temp), edx);
1513 if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1514 instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1515 result = AssignEnvironment(result);
1516 }
1517 return result;
1518}
1519
1520
1521LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1522 if (instr->representation().IsSmiOrInteger32()) {
1523 if (instr->RightIsPowerOf2()) {
1524 return DoModByPowerOf2I(instr);
1525 } else if (instr->right()->IsConstant()) {
1526 return DoModByConstI(instr);
1527 } else {
1528 return DoModI(instr);
1529 }
1530 } else if (instr->representation().IsDouble()) {
1531 return DoArithmeticD(Token::MOD, instr);
1532 } else {
1533 return DoArithmeticT(Token::MOD, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001534 }
1535}
1536
1537
1538LInstruction* LChunkBuilder::DoMul(HMul* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001539 if (instr->representation().IsSmiOrInteger32()) {
1540 DCHECK(instr->left()->representation().Equals(instr->representation()));
1541 DCHECK(instr->right()->representation().Equals(instr->representation()));
1542 LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
1543 LOperand* right = UseOrConstant(instr->BetterRightOperand());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001544 LOperand* temp = NULL;
1545 if (instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1546 temp = TempRegister();
1547 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001548 LMulI* mul = new(zone()) LMulI(left, right, temp);
1549 if (instr->CheckFlag(HValue::kCanOverflow) ||
1550 instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1551 AssignEnvironment(mul);
1552 }
1553 return DefineSameAsFirst(mul);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001554 } else if (instr->representation().IsDouble()) {
1555 return DoArithmeticD(Token::MUL, instr);
1556 } else {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001557 return DoArithmeticT(Token::MUL, instr);
1558 }
1559}
1560
1561
1562LInstruction* LChunkBuilder::DoSub(HSub* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001563 if (instr->representation().IsSmiOrInteger32()) {
1564 DCHECK(instr->left()->representation().Equals(instr->representation()));
1565 DCHECK(instr->right()->representation().Equals(instr->representation()));
Steve Block1e0659c2011-05-24 12:43:12 +01001566 LOperand* left = UseRegisterAtStart(instr->left());
1567 LOperand* right = UseOrConstantAtStart(instr->right());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001568 LSubI* sub = new(zone()) LSubI(left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001569 LInstruction* result = DefineSameAsFirst(sub);
1570 if (instr->CheckFlag(HValue::kCanOverflow)) {
1571 result = AssignEnvironment(result);
1572 }
1573 return result;
1574 } else if (instr->representation().IsDouble()) {
1575 return DoArithmeticD(Token::SUB, instr);
1576 } else {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001577 return DoArithmeticT(Token::SUB, instr);
1578 }
1579}
1580
1581
1582LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001583 if (instr->representation().IsSmiOrInteger32()) {
1584 DCHECK(instr->left()->representation().Equals(instr->representation()));
1585 DCHECK(instr->right()->representation().Equals(instr->representation()));
1586 // Check to see if it would be advantageous to use an lea instruction rather
1587 // than an add. This is the case when no overflow check is needed and there
1588 // are multiple uses of the add's inputs, so using a 3-register add will
1589 // preserve all input values for later uses.
1590 bool use_lea = LAddI::UseLea(instr);
1591 LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
1592 HValue* right_candidate = instr->BetterRightOperand();
1593 LOperand* right = use_lea
1594 ? UseRegisterOrConstantAtStart(right_candidate)
1595 : UseOrConstantAtStart(right_candidate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001596 LAddI* add = new(zone()) LAddI(left, right);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001597 bool can_overflow = instr->CheckFlag(HValue::kCanOverflow);
1598 LInstruction* result = use_lea
1599 ? DefineAsRegister(add)
1600 : DefineSameAsFirst(add);
1601 if (can_overflow) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001602 result = AssignEnvironment(result);
1603 }
1604 return result;
1605 } else if (instr->representation().IsDouble()) {
1606 return DoArithmeticD(Token::ADD, instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001607 } else if (instr->representation().IsExternal()) {
1608 DCHECK(instr->left()->representation().IsExternal());
1609 DCHECK(instr->right()->representation().IsInteger32());
1610 DCHECK(!instr->CheckFlag(HValue::kCanOverflow));
1611 bool use_lea = LAddI::UseLea(instr);
1612 LOperand* left = UseRegisterAtStart(instr->left());
1613 HValue* right_candidate = instr->right();
1614 LOperand* right = use_lea
1615 ? UseRegisterOrConstantAtStart(right_candidate)
1616 : UseOrConstantAtStart(right_candidate);
1617 LAddI* add = new(zone()) LAddI(left, right);
1618 LInstruction* result = use_lea
1619 ? DefineAsRegister(add)
1620 : DefineSameAsFirst(add);
1621 return result;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001622 } else {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001623 return DoArithmeticT(Token::ADD, instr);
1624 }
1625}
1626
1627
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001628LInstruction* LChunkBuilder::DoMathMinMax(HMathMinMax* instr) {
1629 LOperand* left = NULL;
1630 LOperand* right = NULL;
1631 if (instr->representation().IsSmiOrInteger32()) {
1632 DCHECK(instr->left()->representation().Equals(instr->representation()));
1633 DCHECK(instr->right()->representation().Equals(instr->representation()));
1634 left = UseRegisterAtStart(instr->BetterLeftOperand());
1635 right = UseOrConstantAtStart(instr->BetterRightOperand());
1636 } else {
1637 DCHECK(instr->representation().IsDouble());
1638 DCHECK(instr->left()->representation().IsDouble());
1639 DCHECK(instr->right()->representation().IsDouble());
1640 left = UseRegisterAtStart(instr->left());
1641 right = UseRegisterAtStart(instr->right());
1642 }
1643 LMathMinMax* minmax = new(zone()) LMathMinMax(left, right);
1644 return DefineSameAsFirst(minmax);
1645}
1646
1647
Ben Murdochb0fe1622011-05-05 13:52:32 +01001648LInstruction* LChunkBuilder::DoPower(HPower* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001649 DCHECK(instr->representation().IsDouble());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001650 // We call a C function for double power. It can't trigger a GC.
1651 // We need to use fixed result register for the call.
1652 Representation exponent_type = instr->right()->representation();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001653 DCHECK(instr->left()->representation().IsDouble());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001654 LOperand* left = UseFixedDouble(instr->left(), xmm2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001655 LOperand* right =
1656 exponent_type.IsDouble()
1657 ? UseFixedDouble(instr->right(), xmm1)
1658 : UseFixed(instr->right(), MathPowTaggedDescriptor::exponent());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001659 LPower* result = new(zone()) LPower(left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001660 return MarkAsCall(DefineFixedDouble(result, xmm3), instr,
1661 CAN_DEOPTIMIZE_EAGERLY);
1662}
1663
1664
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001665LInstruction* LChunkBuilder::DoCompareGeneric(HCompareGeneric* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001666 DCHECK(instr->left()->representation().IsSmiOrTagged());
1667 DCHECK(instr->right()->representation().IsSmiOrTagged());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001668 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001669 LOperand* left = UseFixed(instr->left(), edx);
1670 LOperand* right = UseFixed(instr->right(), eax);
1671 LCmpT* result = new(zone()) LCmpT(context, left, right);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001672 return MarkAsCall(DefineFixed(result, eax), instr);
1673}
1674
1675
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001676LInstruction* LChunkBuilder::DoCompareNumericAndBranch(
1677 HCompareNumericAndBranch* instr) {
1678 Representation r = instr->representation();
1679 if (r.IsSmiOrInteger32()) {
1680 DCHECK(instr->left()->representation().Equals(r));
1681 DCHECK(instr->right()->representation().Equals(r));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001682 LOperand* left = UseRegisterOrConstantAtStart(instr->left());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001683 LOperand* right = UseOrConstantAtStart(instr->right());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001684 return new(zone()) LCompareNumericAndBranch(left, right);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001685 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001686 DCHECK(r.IsDouble());
1687 DCHECK(instr->left()->representation().IsDouble());
1688 DCHECK(instr->right()->representation().IsDouble());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001689 LOperand* left;
1690 LOperand* right;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001691 if (CanBeImmediateConstant(instr->left()) &&
1692 CanBeImmediateConstant(instr->right())) {
1693 // The code generator requires either both inputs to be constant
1694 // operands, or neither.
1695 left = UseConstant(instr->left());
1696 right = UseConstant(instr->right());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001697 } else {
1698 left = UseRegisterAtStart(instr->left());
1699 right = UseRegisterAtStart(instr->right());
1700 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001701 return new(zone()) LCompareNumericAndBranch(left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001702 }
1703}
1704
1705
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001706LInstruction* LChunkBuilder::DoCompareObjectEqAndBranch(
1707 HCompareObjectEqAndBranch* instr) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001708 LOperand* left = UseRegisterAtStart(instr->left());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001709 LOperand* right = UseOrConstantAtStart(instr->right());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001710 return new(zone()) LCmpObjectEqAndBranch(left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001711}
1712
1713
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001714LInstruction* LChunkBuilder::DoCompareHoleAndBranch(
1715 HCompareHoleAndBranch* instr) {
1716 LOperand* value = UseRegisterAtStart(instr->value());
1717 return new(zone()) LCmpHoleAndBranch(value);
Ben Murdoch257744e2011-11-30 15:57:28 +00001718}
1719
1720
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001721LInstruction* LChunkBuilder::DoCompareMinusZeroAndBranch(
1722 HCompareMinusZeroAndBranch* instr) {
1723 LOperand* value = UseRegister(instr->value());
1724 LOperand* scratch = TempRegister();
1725 return new(zone()) LCompareMinusZeroAndBranch(value, scratch);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001726}
1727
1728
1729LInstruction* LChunkBuilder::DoIsObjectAndBranch(HIsObjectAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001730 DCHECK(instr->value()->representation().IsSmiOrTagged());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001731 LOperand* temp = TempRegister();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001732 return new(zone()) LIsObjectAndBranch(UseRegister(instr->value()), temp);
1733}
1734
1735
1736LInstruction* LChunkBuilder::DoIsStringAndBranch(HIsStringAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001737 DCHECK(instr->value()->representation().IsTagged());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001738 LOperand* temp = TempRegister();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001739 return new(zone()) LIsStringAndBranch(UseRegister(instr->value()), temp);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001740}
1741
1742
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001743LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001744 DCHECK(instr->value()->representation().IsTagged());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001745 return new(zone()) LIsSmiAndBranch(Use(instr->value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001746}
1747
1748
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001749LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
1750 HIsUndetectableAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001751 DCHECK(instr->value()->representation().IsTagged());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001752 return new(zone()) LIsUndetectableAndBranch(
1753 UseRegisterAtStart(instr->value()), TempRegister());
1754}
1755
1756
1757LInstruction* LChunkBuilder::DoStringCompareAndBranch(
1758 HStringCompareAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001759 DCHECK(instr->left()->representation().IsTagged());
1760 DCHECK(instr->right()->representation().IsTagged());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001761 LOperand* context = UseFixed(instr->context(), esi);
1762 LOperand* left = UseFixed(instr->left(), edx);
1763 LOperand* right = UseFixed(instr->right(), eax);
1764
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001765 LStringCompareAndBranch* result = new(zone())
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001766 LStringCompareAndBranch(context, left, right);
1767
1768 return MarkAsCall(result, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001769}
1770
1771
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001772LInstruction* LChunkBuilder::DoHasInstanceTypeAndBranch(
1773 HHasInstanceTypeAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001774 DCHECK(instr->value()->representation().IsTagged());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001775 return new(zone()) LHasInstanceTypeAndBranch(
1776 UseRegisterAtStart(instr->value()),
1777 TempRegister());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001778}
1779
1780
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001781LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
1782 HGetCachedArrayIndex* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001783 DCHECK(instr->value()->representation().IsTagged());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001784 LOperand* value = UseRegisterAtStart(instr->value());
1785
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001786 return DefineAsRegister(new(zone()) LGetCachedArrayIndex(value));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001787}
1788
1789
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001790LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
1791 HHasCachedArrayIndexAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001792 DCHECK(instr->value()->representation().IsTagged());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001793 return new(zone()) LHasCachedArrayIndexAndBranch(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001794 UseRegisterAtStart(instr->value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001795}
1796
1797
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001798LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
1799 HClassOfTestAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001800 DCHECK(instr->value()->representation().IsTagged());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001801 return new(zone()) LClassOfTestAndBranch(UseRegister(instr->value()),
1802 TempRegister(),
1803 TempRegister());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001804}
1805
1806
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001807LInstruction* LChunkBuilder::DoMapEnumLength(HMapEnumLength* instr) {
1808 LOperand* map = UseRegisterAtStart(instr->value());
1809 return DefineAsRegister(new(zone()) LMapEnumLength(map));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001810}
1811
1812
1813LInstruction* LChunkBuilder::DoDateField(HDateField* instr) {
1814 LOperand* date = UseFixed(instr->value(), eax);
1815 LDateField* result =
1816 new(zone()) LDateField(date, FixedTemp(ecx), instr->index());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001817 return MarkAsCall(DefineFixed(result, eax), instr, CAN_DEOPTIMIZE_EAGERLY);
1818}
1819
1820
1821LInstruction* LChunkBuilder::DoSeqStringGetChar(HSeqStringGetChar* instr) {
1822 LOperand* string = UseRegisterAtStart(instr->string());
1823 LOperand* index = UseRegisterOrConstantAtStart(instr->index());
1824 return DefineAsRegister(new(zone()) LSeqStringGetChar(string, index));
1825}
1826
1827
1828LOperand* LChunkBuilder::GetSeqStringSetCharOperand(HSeqStringSetChar* instr) {
1829 if (instr->encoding() == String::ONE_BYTE_ENCODING) {
1830 if (FLAG_debug_code) {
1831 return UseFixed(instr->value(), eax);
1832 } else {
1833 return UseFixedOrConstant(instr->value(), eax);
1834 }
1835 } else {
1836 if (FLAG_debug_code) {
1837 return UseRegisterAtStart(instr->value());
1838 } else {
1839 return UseRegisterOrConstantAtStart(instr->value());
1840 }
1841 }
1842}
1843
1844
1845LInstruction* LChunkBuilder::DoSeqStringSetChar(HSeqStringSetChar* instr) {
1846 LOperand* string = UseRegisterAtStart(instr->string());
1847 LOperand* index = FLAG_debug_code
1848 ? UseRegisterAtStart(instr->index())
1849 : UseRegisterOrConstantAtStart(instr->index());
1850 LOperand* value = GetSeqStringSetCharOperand(instr);
1851 LOperand* context = FLAG_debug_code ? UseFixed(instr->context(), esi) : NULL;
1852 LInstruction* result = new(zone()) LSeqStringSetChar(context, string,
1853 index, value);
1854 if (FLAG_debug_code) {
1855 result = MarkAsCall(result, instr);
1856 }
1857 return result;
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001858}
1859
1860
Ben Murdochb0fe1622011-05-05 13:52:32 +01001861LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001862 if (!FLAG_debug_code && instr->skip_check()) return NULL;
1863 LOperand* index = UseRegisterOrConstantAtStart(instr->index());
1864 LOperand* length = !index->IsConstantOperand()
1865 ? UseOrConstantAtStart(instr->length())
1866 : UseAtStart(instr->length());
1867 LInstruction* result = new(zone()) LBoundsCheck(index, length);
1868 if (!FLAG_debug_code || !instr->skip_check()) {
1869 result = AssignEnvironment(result);
1870 }
1871 return result;
1872}
1873
1874
1875LInstruction* LChunkBuilder::DoBoundsCheckBaseIndexInformation(
1876 HBoundsCheckBaseIndexInformation* instr) {
1877 UNREACHABLE();
1878 return NULL;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001879}
1880
1881
Steve Block1e0659c2011-05-24 12:43:12 +01001882LInstruction* LChunkBuilder::DoAbnormalExit(HAbnormalExit* instr) {
1883 // The control instruction marking the end of a block that completed
1884 // abruptly (e.g., threw an exception). There is nothing specific to do.
1885 return NULL;
1886}
1887
1888
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001889LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
1890 return NULL;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001891}
1892
1893
Ben Murdoch257744e2011-11-30 15:57:28 +00001894LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
1895 // All HForceRepresentation instructions should be eliminated in the
1896 // representation change phase of Hydrogen.
1897 UNREACHABLE();
1898 return NULL;
1899}
1900
1901
Ben Murdochb0fe1622011-05-05 13:52:32 +01001902LInstruction* LChunkBuilder::DoChange(HChange* instr) {
1903 Representation from = instr->from();
1904 Representation to = instr->to();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001905 HValue* val = instr->value();
1906 if (from.IsSmi()) {
1907 if (to.IsTagged()) {
1908 LOperand* value = UseRegister(val);
1909 return DefineSameAsFirst(new(zone()) LDummyUse(value));
1910 }
1911 from = Representation::Tagged();
1912 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001913 if (from.IsTagged()) {
1914 if (to.IsDouble()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001915 LOperand* value = UseRegister(val);
1916 LOperand* temp = TempRegister();
1917 LInstruction* result =
1918 DefineAsRegister(new(zone()) LNumberUntagD(value, temp));
1919 if (!val->representation().IsSmi()) result = AssignEnvironment(result);
1920 return result;
1921 } else if (to.IsSmi()) {
1922 LOperand* value = UseRegister(val);
1923 if (val->type().IsSmi()) {
1924 return DefineSameAsFirst(new(zone()) LDummyUse(value));
1925 }
1926 return AssignEnvironment(DefineSameAsFirst(new(zone()) LCheckSmi(value)));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001927 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001928 DCHECK(to.IsInteger32());
1929 if (val->type().IsSmi() || val->representation().IsSmi()) {
1930 LOperand* value = UseRegister(val);
1931 return DefineSameAsFirst(new(zone()) LSmiUntag(value, false));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001932 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001933 LOperand* value = UseRegister(val);
1934 bool truncating = instr->CanTruncateToInt32();
1935 LOperand* xmm_temp = !truncating ? FixedTemp(xmm1) : NULL;
1936 LInstruction* result =
1937 DefineSameAsFirst(new(zone()) LTaggedToI(value, xmm_temp));
1938 if (!val->representation().IsSmi()) result = AssignEnvironment(result);
1939 return result;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001940 }
1941 }
1942 } else if (from.IsDouble()) {
1943 if (to.IsTagged()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001944 info()->MarkAsDeferredCalling();
1945 LOperand* value = UseRegisterAtStart(val);
1946 LOperand* temp = FLAG_inline_new ? TempRegister() : NULL;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001947 LUnallocated* result_temp = TempRegister();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001948 LNumberTagD* result = new(zone()) LNumberTagD(value, temp);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001949 return AssignPointerMap(Define(result, result_temp));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001950 } else if (to.IsSmi()) {
1951 LOperand* value = UseRegister(val);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001952 return AssignEnvironment(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001953 DefineAsRegister(new(zone()) LDoubleToSmi(value)));
1954 } else {
1955 DCHECK(to.IsInteger32());
1956 bool truncating = instr->CanTruncateToInt32();
1957 bool needs_temp = !truncating;
1958 LOperand* value = needs_temp ? UseTempRegister(val) : UseRegister(val);
1959 LOperand* temp = needs_temp ? TempRegister() : NULL;
1960 LInstruction* result =
1961 DefineAsRegister(new(zone()) LDoubleToI(value, temp));
1962 if (!truncating) result = AssignEnvironment(result);
1963 return result;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001964 }
1965 } else if (from.IsInteger32()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001966 info()->MarkAsDeferredCalling();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001967 if (to.IsTagged()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001968 LOperand* value = UseRegister(val);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001969 if (!instr->CheckFlag(HValue::kCanOverflow)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001970 return DefineSameAsFirst(new(zone()) LSmiTag(value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001971 } else if (val->CheckFlag(HInstruction::kUint32)) {
1972 LOperand* temp = TempRegister();
1973 LNumberTagU* result = new(zone()) LNumberTagU(value, temp);
1974 return AssignPointerMap(DefineSameAsFirst(result));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001975 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001976 LOperand* temp = TempRegister();
1977 LNumberTagI* result = new(zone()) LNumberTagI(value, temp);
1978 return AssignPointerMap(DefineSameAsFirst(result));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001979 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001980 } else if (to.IsSmi()) {
1981 LOperand* value = UseRegister(val);
1982 LInstruction* result = DefineSameAsFirst(new(zone()) LSmiTag(value));
1983 if (instr->CheckFlag(HValue::kCanOverflow)) {
1984 result = AssignEnvironment(result);
1985 }
1986 return result;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001987 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001988 DCHECK(to.IsDouble());
1989 if (val->CheckFlag(HInstruction::kUint32)) {
1990 return DefineAsRegister(new(zone()) LUint32ToDouble(UseRegister(val)));
1991 } else {
1992 return DefineAsRegister(new(zone()) LInteger32ToDouble(Use(val)));
1993 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001994 }
1995 }
1996 UNREACHABLE();
1997 return NULL;
1998}
1999
2000
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002001LInstruction* LChunkBuilder::DoCheckHeapObject(HCheckHeapObject* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002002 LOperand* value = UseAtStart(instr->value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002003 LInstruction* result = new(zone()) LCheckNonSmi(value);
2004 if (!instr->value()->type().IsHeapObject()) {
2005 result = AssignEnvironment(result);
2006 }
2007 return result;
2008}
2009
2010
2011LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
2012 LOperand* value = UseRegisterAtStart(instr->value());
2013 return AssignEnvironment(new(zone()) LCheckSmi(value));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002014}
2015
2016
2017LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
2018 LOperand* value = UseRegisterAtStart(instr->value());
2019 LOperand* temp = TempRegister();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002020 LCheckInstanceType* result = new(zone()) LCheckInstanceType(value, temp);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002021 return AssignEnvironment(result);
2022}
2023
2024
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002025LInstruction* LChunkBuilder::DoCheckValue(HCheckValue* instr) {
2026 // If the object is in new space, we'll emit a global cell compare and so
2027 // want the value in a register. If the object gets promoted before we
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002028 // emit code, we will still get the register but will do an immediate
2029 // compare instead of the cell compare. This is safe.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002030 LOperand* value = instr->object_in_new_space()
2031 ? UseRegisterAtStart(instr->value()) : UseAtStart(instr->value());
2032 return AssignEnvironment(new(zone()) LCheckValue(value));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002033}
2034
2035
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002036LInstruction* LChunkBuilder::DoCheckMaps(HCheckMaps* instr) {
2037 if (instr->IsStabilityCheck()) return new(zone()) LCheckMaps;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002038 LOperand* value = UseRegisterAtStart(instr->value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002039 LInstruction* result = AssignEnvironment(new(zone()) LCheckMaps(value));
2040 if (instr->HasMigrationTarget()) {
2041 info()->MarkAsDeferredCalling();
2042 result = AssignPointerMap(result);
2043 }
2044 return result;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002045}
2046
2047
Ben Murdoch257744e2011-11-30 15:57:28 +00002048LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
2049 HValue* value = instr->value();
2050 Representation input_rep = value->representation();
2051 if (input_rep.IsDouble()) {
2052 LOperand* reg = UseRegister(value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002053 return DefineFixed(new(zone()) LClampDToUint8(reg), eax);
Ben Murdoch257744e2011-11-30 15:57:28 +00002054 } else if (input_rep.IsInteger32()) {
2055 LOperand* reg = UseFixed(value, eax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002056 return DefineFixed(new(zone()) LClampIToUint8(reg), eax);
Ben Murdoch257744e2011-11-30 15:57:28 +00002057 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002058 DCHECK(input_rep.IsSmiOrTagged());
Ben Murdoch257744e2011-11-30 15:57:28 +00002059 LOperand* reg = UseFixed(value, eax);
2060 // Register allocator doesn't (yet) support allocation of double
2061 // temps. Reserve xmm1 explicitly.
2062 LOperand* temp = FixedTemp(xmm1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002063 LClampTToUint8* result = new(zone()) LClampTToUint8(reg, temp);
Ben Murdoch257744e2011-11-30 15:57:28 +00002064 return AssignEnvironment(DefineFixed(result, eax));
2065 }
2066}
2067
2068
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002069LInstruction* LChunkBuilder::DoDoubleBits(HDoubleBits* instr) {
2070 HValue* value = instr->value();
2071 DCHECK(value->representation().IsDouble());
2072 return DefineAsRegister(new(zone()) LDoubleBits(UseRegister(value)));
2073}
2074
2075
2076LInstruction* LChunkBuilder::DoConstructDouble(HConstructDouble* instr) {
2077 LOperand* lo = UseRegister(instr->lo());
2078 LOperand* hi = UseRegister(instr->hi());
2079 return DefineAsRegister(new(zone()) LConstructDouble(hi, lo));
2080}
2081
2082
Ben Murdochb0fe1622011-05-05 13:52:32 +01002083LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002084 LOperand* context = info()->IsStub() ? UseFixed(instr->context(), esi) : NULL;
2085 LOperand* parameter_count = UseRegisterOrConstant(instr->parameter_count());
2086 return new(zone()) LReturn(
2087 UseFixed(instr->value(), eax), context, parameter_count);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002088}
2089
2090
2091LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
2092 Representation r = instr->representation();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002093 if (r.IsSmi()) {
2094 return DefineAsRegister(new(zone()) LConstantS);
2095 } else if (r.IsInteger32()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002096 return DefineAsRegister(new(zone()) LConstantI);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002097 } else if (r.IsDouble()) {
2098 double value = instr->DoubleValue();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002099 bool value_is_zero = bit_cast<uint64_t, double>(value) == 0;
2100 LOperand* temp = value_is_zero ? NULL : TempRegister();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002101 return DefineAsRegister(new(zone()) LConstantD(temp));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002102 } else if (r.IsExternal()) {
2103 return DefineAsRegister(new(zone()) LConstantE);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002104 } else if (r.IsTagged()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002105 return DefineAsRegister(new(zone()) LConstantT);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002106 } else {
Ben Murdochb8e0da22011-05-16 14:20:40 +01002107 UNREACHABLE();
Ben Murdochb0fe1622011-05-05 13:52:32 +01002108 return NULL;
2109 }
2110}
2111
2112
Ben Murdoch8b112d22011-06-08 16:22:53 +01002113LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002114 LLoadGlobalCell* result = new(zone()) LLoadGlobalCell;
2115 return instr->RequiresHoleCheck()
Ben Murdochb0fe1622011-05-05 13:52:32 +01002116 ? AssignEnvironment(DefineAsRegister(result))
2117 : DefineAsRegister(result);
2118}
2119
2120
Ben Murdoch8b112d22011-06-08 16:22:53 +01002121LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
2122 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002123 LOperand* global_object =
2124 UseFixed(instr->global_object(), LoadDescriptor::ReceiverRegister());
2125 LOperand* vector = NULL;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002126 if (instr->HasVectorAndSlot()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002127 vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2128 }
2129
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002130 LLoadGlobalGeneric* result =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002131 new(zone()) LLoadGlobalGeneric(context, global_object, vector);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002132 return MarkAsCall(DefineFixed(result, eax), instr);
2133}
2134
2135
2136LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
2137 LStoreGlobalCell* result =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002138 new(zone()) LStoreGlobalCell(UseRegister(instr->value()));
2139 return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002140}
2141
2142
Ben Murdochb8e0da22011-05-16 14:20:40 +01002143LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002144 LOperand* context = UseRegisterAtStart(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002145 LInstruction* result =
2146 DefineAsRegister(new(zone()) LLoadContextSlot(context));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002147 if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
2148 result = AssignEnvironment(result);
2149 }
2150 return result;
Steve Block1e0659c2011-05-24 12:43:12 +01002151}
2152
2153
2154LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002155 LOperand* value;
2156 LOperand* temp;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002157 LOperand* context = UseRegister(instr->context());
Steve Block1e0659c2011-05-24 12:43:12 +01002158 if (instr->NeedsWriteBarrier()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002159 value = UseTempRegister(instr->value());
2160 temp = TempRegister();
2161 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01002162 value = UseRegister(instr->value());
2163 temp = NULL;
2164 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002165 LInstruction* result = new(zone()) LStoreContextSlot(context, value, temp);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002166 if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
2167 result = AssignEnvironment(result);
2168 }
2169 return result;
Ben Murdochb8e0da22011-05-16 14:20:40 +01002170}
2171
2172
Ben Murdochb0fe1622011-05-05 13:52:32 +01002173LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002174 LOperand* obj = (instr->access().IsExternalMemory() &&
2175 instr->access().offset() == 0)
2176 ? UseRegisterOrConstantAtStart(instr->object())
2177 : UseRegisterAtStart(instr->object());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002178 return DefineAsRegister(new(zone()) LLoadNamedField(obj));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002179}
2180
2181
2182LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002183 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002184 LOperand* object =
2185 UseFixed(instr->object(), LoadDescriptor::ReceiverRegister());
2186 LOperand* vector = NULL;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002187 if (instr->HasVectorAndSlot()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002188 vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2189 }
2190 LLoadNamedGeneric* result = new(zone()) LLoadNamedGeneric(
2191 context, object, vector);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002192 return MarkAsCall(DefineFixed(result, eax), instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002193}
2194
2195
Steve Block9fac8402011-05-12 15:51:54 +01002196LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
2197 HLoadFunctionPrototype* instr) {
2198 return AssignEnvironment(DefineAsRegister(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002199 new(zone()) LLoadFunctionPrototype(UseRegister(instr->function()),
2200 TempRegister())));
Steve Block9fac8402011-05-12 15:51:54 +01002201}
2202
2203
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002204LInstruction* LChunkBuilder::DoLoadRoot(HLoadRoot* instr) {
2205 return DefineAsRegister(new(zone()) LLoadRoot);
Steve Block1e0659c2011-05-24 12:43:12 +01002206}
2207
2208
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002209LInstruction* LChunkBuilder::DoLoadKeyed(HLoadKeyed* instr) {
2210 DCHECK(instr->key()->representation().IsSmiOrInteger32());
Ben Murdoch589d6972011-11-30 16:04:58 +00002211 ElementsKind elements_kind = instr->elements_kind();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002212 bool clobbers_key = ExternalArrayOpRequiresTemp(
2213 instr->key()->representation(), elements_kind);
2214 LOperand* key = clobbers_key
2215 ? UseTempRegister(instr->key())
2216 : UseRegisterOrConstantAtStart(instr->key());
2217 LInstruction* result = NULL;
2218
2219 if (!instr->is_typed_elements()) {
2220 LOperand* obj = UseRegisterAtStart(instr->elements());
2221 result = DefineAsRegister(new(zone()) LLoadKeyed(obj, key));
2222 } else {
2223 DCHECK(
2224 (instr->representation().IsInteger32() &&
2225 !(IsDoubleOrFloatElementsKind(instr->elements_kind()))) ||
2226 (instr->representation().IsDouble() &&
2227 (IsDoubleOrFloatElementsKind(instr->elements_kind()))));
2228 LOperand* backing_store = UseRegister(instr->elements());
2229 result = DefineAsRegister(new(zone()) LLoadKeyed(backing_store, key));
2230 }
2231
2232 if ((instr->is_external() || instr->is_fixed_typed_array()) ?
2233 // see LCodeGen::DoLoadKeyedExternalArray
2234 ((instr->elements_kind() == EXTERNAL_UINT32_ELEMENTS ||
2235 instr->elements_kind() == UINT32_ELEMENTS) &&
2236 !instr->CheckFlag(HInstruction::kUint32)) :
2237 // see LCodeGen::DoLoadKeyedFixedDoubleArray and
2238 // LCodeGen::DoLoadKeyedFixedArray
2239 instr->RequiresHoleCheck()) {
2240 result = AssignEnvironment(result);
2241 }
2242 return result;
Steve Block1e0659c2011-05-24 12:43:12 +01002243}
2244
2245
Ben Murdochb0fe1622011-05-05 13:52:32 +01002246LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002247 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002248 LOperand* object =
2249 UseFixed(instr->object(), LoadDescriptor::ReceiverRegister());
2250 LOperand* key = UseFixed(instr->key(), LoadDescriptor::NameRegister());
2251 LOperand* vector = NULL;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002252 if (instr->HasVectorAndSlot()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002253 vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2254 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002255 LLoadKeyedGeneric* result =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002256 new(zone()) LLoadKeyedGeneric(context, object, key, vector);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002257 return MarkAsCall(DefineFixed(result, eax), instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002258}
2259
2260
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002261LOperand* LChunkBuilder::GetStoreKeyedValueOperand(HStoreKeyed* instr) {
Ben Murdoch589d6972011-11-30 16:04:58 +00002262 ElementsKind elements_kind = instr->elements_kind();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002263
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002264 // Determine if we need a byte register in this case for the value.
2265 bool val_is_fixed_register =
2266 elements_kind == EXTERNAL_INT8_ELEMENTS ||
2267 elements_kind == EXTERNAL_UINT8_ELEMENTS ||
2268 elements_kind == EXTERNAL_UINT8_CLAMPED_ELEMENTS ||
2269 elements_kind == UINT8_ELEMENTS ||
2270 elements_kind == INT8_ELEMENTS ||
2271 elements_kind == UINT8_CLAMPED_ELEMENTS;
2272 if (val_is_fixed_register) {
2273 return UseFixed(instr->value(), eax);
Steve Block44f0eee2011-05-26 01:26:41 +01002274 }
2275
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002276 return UseRegister(instr->value());
2277}
2278
2279
2280LInstruction* LChunkBuilder::DoStoreKeyed(HStoreKeyed* instr) {
2281 if (!instr->is_typed_elements()) {
2282 DCHECK(instr->elements()->representation().IsTagged());
2283 DCHECK(instr->key()->representation().IsInteger32() ||
2284 instr->key()->representation().IsSmi());
2285
2286 if (instr->value()->representation().IsDouble()) {
2287 LOperand* object = UseRegisterAtStart(instr->elements());
2288 LOperand* val = NULL;
2289 val = UseRegisterAtStart(instr->value());
2290 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
2291 return new(zone()) LStoreKeyed(object, key, val);
2292 } else {
2293 DCHECK(instr->value()->representation().IsSmiOrTagged());
2294 bool needs_write_barrier = instr->NeedsWriteBarrier();
2295
2296 LOperand* obj = UseRegister(instr->elements());
2297 LOperand* val;
2298 LOperand* key;
2299 if (needs_write_barrier) {
2300 val = UseTempRegister(instr->value());
2301 key = UseTempRegister(instr->key());
2302 } else {
2303 val = UseRegisterOrConstantAtStart(instr->value());
2304 key = UseRegisterOrConstantAtStart(instr->key());
2305 }
2306 return new(zone()) LStoreKeyed(obj, key, val);
2307 }
2308 }
2309
2310 ElementsKind elements_kind = instr->elements_kind();
2311 DCHECK(
2312 (instr->value()->representation().IsInteger32() &&
2313 !IsDoubleOrFloatElementsKind(elements_kind)) ||
2314 (instr->value()->representation().IsDouble() &&
2315 IsDoubleOrFloatElementsKind(elements_kind)));
2316 DCHECK((instr->is_fixed_typed_array() &&
2317 instr->elements()->representation().IsTagged()) ||
2318 (instr->is_external() &&
2319 instr->elements()->representation().IsExternal()));
2320
2321 LOperand* backing_store = UseRegister(instr->elements());
2322 LOperand* val = GetStoreKeyedValueOperand(instr);
2323 bool clobbers_key = ExternalArrayOpRequiresTemp(
2324 instr->key()->representation(), elements_kind);
2325 LOperand* key = clobbers_key
2326 ? UseTempRegister(instr->key())
2327 : UseRegisterOrConstantAtStart(instr->key());
2328 return new(zone()) LStoreKeyed(backing_store, key, val);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002329}
2330
2331
Ben Murdochb0fe1622011-05-05 13:52:32 +01002332LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002333 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002334 LOperand* object =
2335 UseFixed(instr->object(), StoreDescriptor::ReceiverRegister());
2336 LOperand* key = UseFixed(instr->key(), StoreDescriptor::NameRegister());
2337 LOperand* value = UseFixed(instr->value(), StoreDescriptor::ValueRegister());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002338
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002339 DCHECK(instr->object()->representation().IsTagged());
2340 DCHECK(instr->key()->representation().IsTagged());
2341 DCHECK(instr->value()->representation().IsTagged());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002342
Steve Block1e0659c2011-05-24 12:43:12 +01002343 LStoreKeyedGeneric* result =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002344 new(zone()) LStoreKeyedGeneric(context, object, key, value);
Steve Block1e0659c2011-05-24 12:43:12 +01002345 return MarkAsCall(result, instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002346}
2347
2348
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002349LInstruction* LChunkBuilder::DoTransitionElementsKind(
2350 HTransitionElementsKind* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002351 if (IsSimpleMapChangeTransition(instr->from_kind(), instr->to_kind())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002352 LOperand* object = UseRegister(instr->object());
2353 LOperand* new_map_reg = TempRegister();
2354 LOperand* temp_reg = TempRegister();
2355 LTransitionElementsKind* result =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002356 new(zone()) LTransitionElementsKind(object, NULL,
2357 new_map_reg, temp_reg);
2358 return result;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002359 } else {
2360 LOperand* object = UseFixed(instr->object(), eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002361 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002362 LTransitionElementsKind* result =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002363 new(zone()) LTransitionElementsKind(object, context, NULL, NULL);
2364 return MarkAsCall(result, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002365 }
2366}
2367
2368
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002369LInstruction* LChunkBuilder::DoTrapAllocationMemento(
2370 HTrapAllocationMemento* instr) {
2371 LOperand* object = UseRegister(instr->object());
2372 LOperand* temp = TempRegister();
2373 LTrapAllocationMemento* result =
2374 new(zone()) LTrapAllocationMemento(object, temp);
2375 return AssignEnvironment(result);
2376}
2377
2378
Ben Murdochb0fe1622011-05-05 13:52:32 +01002379LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002380 bool is_in_object = instr->access().IsInobject();
2381 bool is_external_location = instr->access().IsExternalMemory() &&
2382 instr->access().offset() == 0;
Steve Block9fac8402011-05-12 15:51:54 +01002383 bool needs_write_barrier = instr->NeedsWriteBarrier();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002384 bool needs_write_barrier_for_map = instr->has_transition() &&
2385 instr->NeedsWriteBarrierForMap();
Ben Murdochb0fe1622011-05-05 13:52:32 +01002386
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002387 LOperand* obj;
2388 if (needs_write_barrier) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002389 obj = is_in_object
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002390 ? UseRegister(instr->object())
2391 : UseTempRegister(instr->object());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002392 } else if (is_external_location) {
2393 DCHECK(!is_in_object);
2394 DCHECK(!needs_write_barrier);
2395 DCHECK(!needs_write_barrier_for_map);
2396 obj = UseRegisterOrConstant(instr->object());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002397 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002398 obj = needs_write_barrier_for_map
2399 ? UseRegister(instr->object())
2400 : UseRegisterAtStart(instr->object());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002401 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002402
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002403 bool can_be_constant = instr->value()->IsConstant() &&
2404 HConstant::cast(instr->value())->NotInNewSpace() &&
2405 !instr->field_representation().IsDouble();
2406
2407 LOperand* val;
2408 if (instr->field_representation().IsInteger8() ||
2409 instr->field_representation().IsUInteger8()) {
2410 // mov_b requires a byte register (i.e. any of eax, ebx, ecx, edx).
2411 // Just force the value to be in eax and we're safe here.
2412 val = UseFixed(instr->value(), eax);
2413 } else if (needs_write_barrier) {
2414 val = UseTempRegister(instr->value());
2415 } else if (can_be_constant) {
2416 val = UseRegisterOrConstant(instr->value());
2417 } else if (instr->field_representation().IsDouble()) {
2418 val = UseRegisterAtStart(instr->value());
2419 } else {
2420 val = UseRegister(instr->value());
2421 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002422
2423 // We only need a scratch register if we have a write barrier or we
2424 // have a store into the properties array (not in-object-property).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002425 LOperand* temp = (!is_in_object || needs_write_barrier ||
2426 needs_write_barrier_for_map) ? TempRegister() : NULL;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002427
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002428 // We need a temporary register for write barrier of the map field.
2429 LOperand* temp_map = needs_write_barrier_for_map ? TempRegister() : NULL;
2430
2431 return new(zone()) LStoreNamedField(obj, val, temp, temp_map);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002432}
2433
2434
2435LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002436 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002437 LOperand* object =
2438 UseFixed(instr->object(), StoreDescriptor::ReceiverRegister());
2439 LOperand* value = UseFixed(instr->value(), StoreDescriptor::ValueRegister());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002440
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002441 LStoreNamedGeneric* result =
2442 new(zone()) LStoreNamedGeneric(context, object, value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002443 return MarkAsCall(result, instr);
2444}
2445
2446
Ben Murdoch257744e2011-11-30 15:57:28 +00002447LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002448 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002449 LOperand* left = UseFixed(instr->left(), edx);
2450 LOperand* right = UseFixed(instr->right(), eax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002451 LStringAdd* string_add = new(zone()) LStringAdd(context, left, right);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002452 return MarkAsCall(DefineFixed(string_add, eax), instr);
Ben Murdoch257744e2011-11-30 15:57:28 +00002453}
2454
2455
Steve Block1e0659c2011-05-24 12:43:12 +01002456LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002457 LOperand* string = UseTempRegister(instr->string());
2458 LOperand* index = UseTempRegister(instr->index());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002459 LOperand* context = UseAny(instr->context());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002460 LStringCharCodeAt* result =
2461 new(zone()) LStringCharCodeAt(context, string, index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002462 return AssignPointerMap(DefineAsRegister(result));
Steve Block1e0659c2011-05-24 12:43:12 +01002463}
2464
2465
Steve Block44f0eee2011-05-26 01:26:41 +01002466LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2467 LOperand* char_code = UseRegister(instr->value());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002468 LOperand* context = UseAny(instr->context());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002469 LStringCharFromCode* result =
2470 new(zone()) LStringCharFromCode(context, char_code);
Steve Block44f0eee2011-05-26 01:26:41 +01002471 return AssignPointerMap(DefineAsRegister(result));
2472}
2473
2474
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002475LInstruction* LChunkBuilder::DoAllocate(HAllocate* instr) {
2476 info()->MarkAsDeferredCalling();
2477 LOperand* context = UseAny(instr->context());
2478 LOperand* size = instr->size()->IsConstant()
2479 ? UseConstant(instr->size())
2480 : UseTempRegister(instr->size());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002481 LOperand* temp = TempRegister();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002482 LAllocate* result = new(zone()) LAllocate(context, size, temp);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002483 return AssignPointerMap(DefineAsRegister(result));
2484}
2485
2486
Ben Murdochb0fe1622011-05-05 13:52:32 +01002487LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002488 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002489 return MarkAsCall(
2490 DefineFixed(new(zone()) LRegExpLiteral(context), eax), instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002491}
2492
2493
2494LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002495 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002496 return MarkAsCall(
2497 DefineFixed(new(zone()) LFunctionLiteral(context), eax), instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002498}
2499
2500
Ben Murdochb0fe1622011-05-05 13:52:32 +01002501LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002502 DCHECK(argument_count_ == 0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002503 allocator_->MarkAsOsrEntry();
2504 current_block_->last_environment()->set_ast_id(instr->ast_id());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002505 return AssignEnvironment(new(zone()) LOsrEntry);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002506}
2507
2508
2509LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002510 LParameter* result = new(zone()) LParameter;
2511 if (instr->kind() == HParameter::STACK_PARAMETER) {
2512 int spill_index = chunk()->GetParameterStackSlot(instr->index());
2513 return DefineAsSpilled(result, spill_index);
2514 } else {
2515 DCHECK(info()->IsStub());
2516 CallInterfaceDescriptor descriptor =
2517 info()->code_stub()->GetCallInterfaceDescriptor();
2518 int index = static_cast<int>(instr->index());
2519 Register reg = descriptor.GetEnvironmentParameterRegister(index);
2520 return DefineFixed(result, reg);
2521 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002522}
2523
2524
2525LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002526 // Use an index that corresponds to the location in the unoptimized frame,
2527 // which the optimized frame will subsume.
2528 int env_index = instr->index();
2529 int spill_index = 0;
2530 if (instr->environment()->is_parameter_index(env_index)) {
2531 spill_index = chunk()->GetParameterStackSlot(env_index);
2532 } else {
2533 spill_index = env_index - instr->environment()->first_local_index();
2534 if (spill_index > LUnallocated::kMaxFixedSlotIndex) {
2535 Retry(kNotEnoughSpillSlotsForOsr);
2536 spill_index = 0;
2537 }
2538 if (spill_index == 0) {
2539 // The dynamic frame alignment state overwrites the first local.
2540 // The first local is saved at the end of the unoptimized frame.
2541 spill_index = graph()->osr()->UnoptimizedFrameSlots();
2542 }
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01002543 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002544 return DefineAsSpilled(new(zone()) LUnknownOSRValue, spill_index);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002545}
2546
2547
2548LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
Steve Block1e0659c2011-05-24 12:43:12 +01002549 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002550 LCallStub* result = new(zone()) LCallStub(context);
Steve Block1e0659c2011-05-24 12:43:12 +01002551 return MarkAsCall(DefineFixed(result, eax), instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002552}
2553
2554
2555LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002556 // There are no real uses of the arguments object.
2557 // arguments.length and element access are supported directly on
2558 // stack arguments, and any real arguments object use causes a bailout.
2559 // So this value is never used.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002560 return NULL;
2561}
2562
2563
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002564LInstruction* LChunkBuilder::DoCapturedObject(HCapturedObject* instr) {
2565 instr->ReplayEnvironment(current_block_->last_environment());
2566
2567 // There are no real uses of a captured object.
2568 return NULL;
2569}
2570
2571
Ben Murdochb0fe1622011-05-05 13:52:32 +01002572LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002573 info()->MarkAsRequiresFrame();
2574 LOperand* args = UseRegister(instr->arguments());
2575 LOperand* length;
2576 LOperand* index;
2577 if (instr->length()->IsConstant() && instr->index()->IsConstant()) {
2578 length = UseRegisterOrConstant(instr->length());
2579 index = UseOrConstant(instr->index());
2580 } else {
2581 length = UseTempRegister(instr->length());
2582 index = Use(instr->index());
2583 }
2584 return DefineAsRegister(new(zone()) LAccessArgumentsAt(args, length, index));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002585}
2586
2587
Steve Block44f0eee2011-05-26 01:26:41 +01002588LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2589 LOperand* object = UseFixed(instr->value(), eax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002590 LToFastProperties* result = new(zone()) LToFastProperties(object);
Steve Block44f0eee2011-05-26 01:26:41 +01002591 return MarkAsCall(DefineFixed(result, eax), instr);
2592}
2593
2594
Ben Murdochb0fe1622011-05-05 13:52:32 +01002595LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002596 LOperand* context = UseFixed(instr->context(), esi);
2597 LOperand* value = UseAtStart(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002598 LTypeof* result = new(zone()) LTypeof(context, value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002599 return MarkAsCall(DefineFixed(result, eax), instr);
2600}
2601
2602
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002603LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002604 return new(zone()) LTypeofIsAndBranch(UseTempRegister(instr->value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002605}
2606
Steve Block1e0659c2011-05-24 12:43:12 +01002607
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002608LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
2609 HIsConstructCallAndBranch* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002610 return new(zone()) LIsConstructCallAndBranch(TempRegister());
Steve Block1e0659c2011-05-24 12:43:12 +01002611}
2612
2613
Ben Murdochb0fe1622011-05-05 13:52:32 +01002614LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002615 instr->ReplayEnvironment(current_block_->last_environment());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002616 return NULL;
2617}
2618
2619
2620LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002621 info()->MarkAsDeferredCalling();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002622 if (instr->is_function_entry()) {
2623 LOperand* context = UseFixed(instr->context(), esi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002624 return MarkAsCall(new(zone()) LStackCheck(context), instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002625 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002626 DCHECK(instr->is_backwards_branch());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002627 LOperand* context = UseAny(instr->context());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002628 return AssignEnvironment(
2629 AssignPointerMap(new(zone()) LStackCheck(context)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002630 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002631}
2632
2633
2634LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2635 HEnvironment* outer = current_block_->last_environment();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002636 outer->set_ast_id(instr->ReturnId());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002637 HConstant* undefined = graph()->GetConstantUndefined();
2638 HEnvironment* inner = outer->CopyForInlining(instr->closure(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002639 instr->arguments_count(),
Ben Murdochb0fe1622011-05-05 13:52:32 +01002640 instr->function(),
Ben Murdoch257744e2011-11-30 15:57:28 +00002641 undefined,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002642 instr->inlining_kind());
2643 // Only replay binding of arguments object if it wasn't removed from graph.
2644 if (instr->arguments_var() != NULL && instr->arguments_object()->IsLinked()) {
2645 inner->Bind(instr->arguments_var(), instr->arguments_object());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002646 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002647 inner->BindContext(instr->closure_context());
2648 inner->set_entry(instr);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002649 current_block_->UpdateEnvironment(inner);
2650 chunk_->AddInlinedClosure(instr->closure());
2651 return NULL;
2652}
2653
2654
2655LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002656 LInstruction* pop = NULL;
2657
2658 HEnvironment* env = current_block_->last_environment();
2659
2660 if (env->entry()->arguments_pushed()) {
2661 int argument_count = env->arguments_environment()->parameter_count();
2662 pop = new(zone()) LDrop(argument_count);
2663 DCHECK(instr->argument_delta() == -argument_count);
2664 }
2665
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002666 HEnvironment* outer = current_block_->last_environment()->
2667 DiscardInlined(false);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002668 current_block_->UpdateEnvironment(outer);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002669 return pop;
Ben Murdoch257744e2011-11-30 15:57:28 +00002670}
2671
2672
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002673LInstruction* LChunkBuilder::DoForInPrepareMap(HForInPrepareMap* instr) {
2674 LOperand* context = UseFixed(instr->context(), esi);
2675 LOperand* object = UseFixed(instr->enumerable(), eax);
2676 LForInPrepareMap* result = new(zone()) LForInPrepareMap(context, object);
2677 return MarkAsCall(DefineFixed(result, eax), instr, CAN_DEOPTIMIZE_EAGERLY);
2678}
2679
2680
2681LInstruction* LChunkBuilder::DoForInCacheArray(HForInCacheArray* instr) {
2682 LOperand* map = UseRegister(instr->map());
2683 return AssignEnvironment(DefineAsRegister(
2684 new(zone()) LForInCacheArray(map)));
2685}
2686
2687
2688LInstruction* LChunkBuilder::DoCheckMapValue(HCheckMapValue* instr) {
2689 LOperand* value = UseRegisterAtStart(instr->value());
2690 LOperand* map = UseRegisterAtStart(instr->map());
2691 return AssignEnvironment(new(zone()) LCheckMapValue(value, map));
2692}
2693
2694
2695LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) {
2696 LOperand* object = UseRegister(instr->object());
2697 LOperand* index = UseTempRegister(instr->index());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002698 LLoadFieldByIndex* load = new(zone()) LLoadFieldByIndex(object, index);
2699 LInstruction* result = DefineSameAsFirst(load);
2700 return AssignPointerMap(result);
2701}
2702
2703
2704LInstruction* LChunkBuilder::DoStoreFrameContext(HStoreFrameContext* instr) {
2705 LOperand* context = UseRegisterAtStart(instr->context());
2706 return new(zone()) LStoreFrameContext(context);
2707}
2708
2709
2710LInstruction* LChunkBuilder::DoAllocateBlockContext(
2711 HAllocateBlockContext* instr) {
2712 LOperand* context = UseFixed(instr->context(), esi);
2713 LOperand* function = UseRegisterAtStart(instr->function());
2714 LAllocateBlockContext* result =
2715 new(zone()) LAllocateBlockContext(context, function);
2716 return MarkAsCall(DefineFixed(result, esi), instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002717}
2718
2719
Ben Murdochb0fe1622011-05-05 13:52:32 +01002720} } // namespace v8::internal
Ben Murdochb8e0da22011-05-16 14:20:40 +01002721
2722#endif // V8_TARGET_ARCH_IA32