blob: cdc68c865275d7897fc8ce25040eb7c9303663ea [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.7
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028#include "src/v8.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010029
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030#include "src/base/bits.h"
31#include "src/code-factory.h"
32#include "src/code-stubs.h"
33#include "src/hydrogen-osr.h"
34#include "src/ic/ic.h"
35#include "src/ic/stub-cache.h"
36#include "src/mips/lithium-codegen-mips.h"
37#include "src/mips/lithium-gap-resolver-mips.h"
38
Ben Murdoch3ef787d2012-04-12 10:51:47 +010039
40namespace v8 {
41namespace internal {
42
43
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044class SafepointGenerator FINAL : public CallWrapper {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010045 public:
46 SafepointGenerator(LCodeGen* codegen,
47 LPointerMap* pointers,
48 Safepoint::DeoptMode mode)
49 : codegen_(codegen),
50 pointers_(pointers),
51 deopt_mode_(mode) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052 virtual ~SafepointGenerator() {}
Ben Murdoch3ef787d2012-04-12 10:51:47 +010053
Emily Bernierd0a1eb72015-03-24 16:35:39 -040054 void BeforeCall(int call_size) const OVERRIDE {}
Ben Murdoch3ef787d2012-04-12 10:51:47 +010055
Emily Bernierd0a1eb72015-03-24 16:35:39 -040056 void AfterCall() const OVERRIDE {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010057 codegen_->RecordSafepoint(pointers_, deopt_mode_);
58 }
59
60 private:
61 LCodeGen* codegen_;
62 LPointerMap* pointers_;
63 Safepoint::DeoptMode deopt_mode_;
64};
65
66
67#define __ masm()->
68
69bool LCodeGen::GenerateCode() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070 LPhase phase("Z_Code generation", chunk());
71 DCHECK(is_unused());
Ben Murdoch3ef787d2012-04-12 10:51:47 +010072 status_ = GENERATING;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010073
74 // Open a frame scope to indicate that there is a frame on the stack. The
75 // NONE indicates that the scope shouldn't actually generate code to set up
76 // the frame (that is done in GeneratePrologue).
77 FrameScope frame_scope(masm_, StackFrame::NONE);
78
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 return GeneratePrologue() && GenerateBody() && GenerateDeferredCode() &&
80 GenerateJumpTable() && GenerateSafepointTable();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010081}
82
83
84void LCodeGen::FinishCode(Handle<Code> code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085 DCHECK(is_done());
Ben Murdoch3ef787d2012-04-12 10:51:47 +010086 code->set_stack_slots(GetStackSlotCount());
87 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088 if (code->is_optimized_code()) RegisterWeakObjectsInOptimizedCode(code);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010089 PopulateDeoptimizationData(code);
90}
91
92
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093void LCodeGen::SaveCallerDoubles() {
94 DCHECK(info()->saves_caller_doubles());
95 DCHECK(NeedsEagerFrame());
96 Comment(";;; Save clobbered callee double registers");
97 int count = 0;
98 BitVector* doubles = chunk()->allocated_double_registers();
99 BitVector::Iterator save_iterator(doubles);
100 while (!save_iterator.Done()) {
101 __ sdc1(DoubleRegister::FromAllocationIndex(save_iterator.Current()),
102 MemOperand(sp, count * kDoubleSize));
103 save_iterator.Advance();
104 count++;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100105 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100106}
107
108
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109void LCodeGen::RestoreCallerDoubles() {
110 DCHECK(info()->saves_caller_doubles());
111 DCHECK(NeedsEagerFrame());
112 Comment(";;; Restore clobbered callee double registers");
113 BitVector* doubles = chunk()->allocated_double_registers();
114 BitVector::Iterator save_iterator(doubles);
115 int count = 0;
116 while (!save_iterator.Done()) {
117 __ ldc1(DoubleRegister::FromAllocationIndex(save_iterator.Current()),
118 MemOperand(sp, count * kDoubleSize));
119 save_iterator.Advance();
120 count++;
121 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100122}
123
124
125bool LCodeGen::GeneratePrologue() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000126 DCHECK(is_generating());
127
128 if (info()->IsOptimizing()) {
129 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100130
131#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000132 if (strlen(FLAG_stop_at) > 0 &&
133 info_->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
134 __ stop("stop_at");
135 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100136#endif
137
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138 // a1: Callee's JS function.
139 // cp: Callee's context.
140 // fp: Caller's frame pointer.
141 // lr: Caller's pc.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100142
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000143 // Sloppy mode functions and builtins need to replace the receiver with the
144 // global proxy when called as functions (without an explicit receiver
145 // object).
146 if (info_->this_has_uses() &&
147 info_->strict_mode() == SLOPPY &&
148 !info_->is_native()) {
149 Label ok;
150 int receiver_offset = info_->scope()->num_parameters() * kPointerSize;
151 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
152 __ lw(a2, MemOperand(sp, receiver_offset));
153 __ Branch(&ok, ne, a2, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100154
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155 __ lw(a2, GlobalObjectOperand());
156 __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalProxyOffset));
157
158 __ sw(a2, MemOperand(sp, receiver_offset));
159
160 __ bind(&ok);
161 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100162 }
163
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000164 info()->set_prologue_offset(masm_->pc_offset());
165 if (NeedsEagerFrame()) {
166 if (info()->IsStub()) {
167 __ StubPrologue();
168 } else {
169 __ Prologue(info()->IsCodePreAgingActive());
170 }
171 frame_is_built_ = true;
172 info_->AddNoFrameRange(0, masm_->pc_offset());
173 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100174
175 // Reserve space for the stack slots needed by the code.
176 int slots = GetStackSlotCount();
177 if (slots > 0) {
178 if (FLAG_debug_code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179 __ Subu(sp, sp, Operand(slots * kPointerSize));
180 __ Push(a0, a1);
181 __ Addu(a0, sp, Operand(slots * kPointerSize));
182 __ li(a1, Operand(kSlotsZapValue));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100183 Label loop;
184 __ bind(&loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000185 __ Subu(a0, a0, Operand(kPointerSize));
186 __ sw(a1, MemOperand(a0, 2 * kPointerSize));
187 __ Branch(&loop, ne, a0, Operand(sp));
188 __ Pop(a0, a1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100189 } else {
190 __ Subu(sp, sp, Operand(slots * kPointerSize));
191 }
192 }
193
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 if (info()->saves_caller_doubles()) {
195 SaveCallerDoubles();
196 }
197
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100198 // Possibly allocate a local context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000199 int heap_slots = info()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100200 if (heap_slots > 0) {
201 Comment(";;; Allocate local context");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000202 bool need_write_barrier = true;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100203 // Argument to NewContext is the function, which is in a1.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100204 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000205 FastNewContextStub stub(isolate(), heap_slots);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100206 __ CallStub(&stub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000207 // Result of FastNewContextStub is always in new space.
208 need_write_barrier = false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100209 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 __ push(a1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100211 __ CallRuntime(Runtime::kNewFunctionContext, 1);
212 }
213 RecordSafepoint(Safepoint::kNoLazyDeopt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214 // Context is returned in both v0. It replaces the context passed to us.
215 // It's saved in the stack and kept live in cp.
216 __ mov(cp, v0);
217 __ sw(v0, MemOperand(fp, StandardFrameConstants::kContextOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100218 // Copy any necessary parameters into the context.
219 int num_parameters = scope()->num_parameters();
220 for (int i = 0; i < num_parameters; i++) {
221 Variable* var = scope()->parameter(i);
222 if (var->IsContextSlot()) {
223 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
224 (num_parameters - 1 - i) * kPointerSize;
225 // Load parameter from stack.
226 __ lw(a0, MemOperand(fp, parameter_offset));
227 // Store it in the context.
228 MemOperand target = ContextOperand(cp, var->index());
229 __ sw(a0, target);
230 // Update the write barrier. This clobbers a3 and a0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000231 if (need_write_barrier) {
232 __ RecordWriteContextSlot(
233 cp, target.offset(), a0, a3, GetRAState(), kSaveFPRegs);
234 } else if (FLAG_debug_code) {
235 Label done;
236 __ JumpIfInNewSpace(cp, a0, &done);
237 __ Abort(kExpectedNewSpaceObject);
238 __ bind(&done);
239 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100240 }
241 }
242 Comment(";;; End allocate local context");
243 }
244
245 // Trace the call.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000246 if (FLAG_trace && info()->IsOptimizing()) {
247 // We have not executed any compiled code yet, so cp still holds the
248 // incoming context.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100249 __ CallRuntime(Runtime::kTraceEnter, 0);
250 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100251 return !is_aborted();
252}
253
254
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255void LCodeGen::GenerateOsrPrologue() {
256 // Generate the OSR entry prologue at the first unknown OSR value, or if there
257 // are none, at the OSR entrypoint instruction.
258 if (osr_pc_offset_ >= 0) return;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100259
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000260 osr_pc_offset_ = masm()->pc_offset();
261
262 // Adjust the frame size, subsuming the unoptimized frame into the
263 // optimized frame.
264 int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
265 DCHECK(slots >= 0);
266 __ Subu(sp, sp, Operand(slots * kPointerSize));
267}
268
269
270void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
271 if (instr->IsCall()) {
272 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100273 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000274 if (!instr->IsLazyBailout() && !instr->IsGap()) {
275 safepoints_.BumpLastLazySafepointIndex();
276 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100277}
278
279
280bool LCodeGen::GenerateDeferredCode() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000281 DCHECK(is_generating());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100282 if (deferred_.length() > 0) {
283 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
284 LDeferredCode* code = deferred_[i];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000285
286 HValue* value =
287 instructions_->at(code->instruction_index())->hydrogen_value();
288 RecordAndWritePosition(
289 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
290
291 Comment(";;; <@%d,#%d> "
292 "-------------------- Deferred %s --------------------",
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100293 code->instruction_index(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000294 code->instr()->hydrogen_value()->id(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100295 code->instr()->Mnemonic());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000296 __ bind(code->entry());
297 if (NeedsDeferredFrame()) {
298 Comment(";;; Build frame");
299 DCHECK(!frame_is_built_);
300 DCHECK(info()->IsStub());
301 frame_is_built_ = true;
302 __ MultiPush(cp.bit() | fp.bit() | ra.bit());
303 __ li(scratch0(), Operand(Smi::FromInt(StackFrame::STUB)));
304 __ push(scratch0());
305 __ Addu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
306 Comment(";;; Deferred code");
307 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100308 code->Generate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000309 if (NeedsDeferredFrame()) {
310 Comment(";;; Destroy frame");
311 DCHECK(frame_is_built_);
312 __ pop(at);
313 __ MultiPop(cp.bit() | fp.bit() | ra.bit());
314 frame_is_built_ = false;
315 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100316 __ jmp(code->exit());
317 }
318 }
319 // Deferred code is the last part of the instruction sequence. Mark
320 // the generated code as done unless we bailed out.
321 if (!is_aborted()) status_ = DONE;
322 return !is_aborted();
323}
324
325
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000326bool LCodeGen::GenerateJumpTable() {
327 if (jump_table_.length() > 0) {
328 Label needs_frame, call_deopt_entry;
329
330 Comment(";;; -------------------- Jump table --------------------");
331 Address base = jump_table_[0].address;
332
333 Register entry_offset = t9;
334
335 int length = jump_table_.length();
336 for (int i = 0; i < length; i++) {
337 Deoptimizer::JumpTableEntry* table_entry = &jump_table_[i];
338 __ bind(&table_entry->label);
339
340 DCHECK(table_entry->bailout_type == jump_table_[0].bailout_type);
341 Address entry = table_entry->address;
342 DeoptComment(table_entry->reason);
343
344 // Second-level deopt table entries are contiguous and small, so instead
345 // of loading the full, absolute address of each one, load an immediate
346 // offset which will be added to the base address later.
347 __ li(entry_offset, Operand(entry - base));
348
349 if (table_entry->needs_frame) {
350 DCHECK(!info()->saves_caller_doubles());
351 if (needs_frame.is_bound()) {
352 __ Branch(&needs_frame);
353 } else {
354 __ bind(&needs_frame);
355 Comment(";;; call deopt with frame");
356 __ MultiPush(cp.bit() | fp.bit() | ra.bit());
357 // This variant of deopt can only be used with stubs. Since we don't
358 // have a function pointer to install in the stack frame that we're
359 // building, install a special marker there instead.
360 DCHECK(info()->IsStub());
361 __ li(at, Operand(Smi::FromInt(StackFrame::STUB)));
362 __ push(at);
363 __ Addu(fp, sp,
364 Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
365 __ bind(&call_deopt_entry);
366 // Add the base address to the offset previously loaded in
367 // entry_offset.
368 __ Addu(entry_offset, entry_offset,
369 Operand(ExternalReference::ForDeoptEntry(base)));
370 __ Call(entry_offset);
371 }
372 } else {
373 // The last entry can fall through into `call_deopt_entry`, avoiding a
374 // branch.
375 bool need_branch = ((i + 1) != length) || call_deopt_entry.is_bound();
376
377 if (need_branch) __ Branch(&call_deopt_entry);
378 }
379 }
380
381 if (!call_deopt_entry.is_bound()) {
382 Comment(";;; call deopt");
383 __ bind(&call_deopt_entry);
384
385 if (info()->saves_caller_doubles()) {
386 DCHECK(info()->IsStub());
387 RestoreCallerDoubles();
388 }
389
390 // Add the base address to the offset previously loaded in entry_offset.
391 __ Addu(entry_offset, entry_offset,
392 Operand(ExternalReference::ForDeoptEntry(base)));
393 __ Call(entry_offset);
394 }
395 }
396 __ RecordComment("]");
397
398 // The deoptimization jump table is the last part of the instruction
399 // sequence. Mark the generated code as done unless we bailed out.
400 if (!is_aborted()) status_ = DONE;
401 return !is_aborted();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100402}
403
404
405bool LCodeGen::GenerateSafepointTable() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000406 DCHECK(is_done());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100407 safepoints_.Emit(masm(), GetStackSlotCount());
408 return !is_aborted();
409}
410
411
412Register LCodeGen::ToRegister(int index) const {
413 return Register::FromAllocationIndex(index);
414}
415
416
417DoubleRegister LCodeGen::ToDoubleRegister(int index) const {
418 return DoubleRegister::FromAllocationIndex(index);
419}
420
421
422Register LCodeGen::ToRegister(LOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000423 DCHECK(op->IsRegister());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100424 return ToRegister(op->index());
425}
426
427
428Register LCodeGen::EmitLoadRegister(LOperand* op, Register scratch) {
429 if (op->IsRegister()) {
430 return ToRegister(op->index());
431 } else if (op->IsConstantOperand()) {
432 LConstantOperand* const_op = LConstantOperand::cast(op);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000433 HConstant* constant = chunk_->LookupConstant(const_op);
434 Handle<Object> literal = constant->handle(isolate());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100435 Representation r = chunk_->LookupLiteralRepresentation(const_op);
436 if (r.IsInteger32()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000437 DCHECK(literal->IsNumber());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100438 __ li(scratch, Operand(static_cast<int32_t>(literal->Number())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000439 } else if (r.IsSmi()) {
440 DCHECK(constant->HasSmiValue());
441 __ li(scratch, Operand(Smi::FromInt(constant->Integer32Value())));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100442 } else if (r.IsDouble()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000443 Abort(kEmitLoadRegisterUnsupportedDoubleImmediate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100444 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000445 DCHECK(r.IsSmiOrTagged());
446 __ li(scratch, literal);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100447 }
448 return scratch;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000449 } else if (op->IsStackSlot()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100450 __ lw(scratch, ToMemOperand(op));
451 return scratch;
452 }
453 UNREACHABLE();
454 return scratch;
455}
456
457
458DoubleRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000459 DCHECK(op->IsDoubleRegister());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100460 return ToDoubleRegister(op->index());
461}
462
463
464DoubleRegister LCodeGen::EmitLoadDoubleRegister(LOperand* op,
465 FloatRegister flt_scratch,
466 DoubleRegister dbl_scratch) {
467 if (op->IsDoubleRegister()) {
468 return ToDoubleRegister(op->index());
469 } else if (op->IsConstantOperand()) {
470 LConstantOperand* const_op = LConstantOperand::cast(op);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000471 HConstant* constant = chunk_->LookupConstant(const_op);
472 Handle<Object> literal = constant->handle(isolate());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100473 Representation r = chunk_->LookupLiteralRepresentation(const_op);
474 if (r.IsInteger32()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000475 DCHECK(literal->IsNumber());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100476 __ li(at, Operand(static_cast<int32_t>(literal->Number())));
477 __ mtc1(at, flt_scratch);
478 __ cvt_d_w(dbl_scratch, flt_scratch);
479 return dbl_scratch;
480 } else if (r.IsDouble()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000481 Abort(kUnsupportedDoubleImmediate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100482 } else if (r.IsTagged()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000483 Abort(kUnsupportedTaggedImmediate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100484 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000485 } else if (op->IsStackSlot()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100486 MemOperand mem_op = ToMemOperand(op);
487 __ ldc1(dbl_scratch, mem_op);
488 return dbl_scratch;
489 }
490 UNREACHABLE();
491 return dbl_scratch;
492}
493
494
495Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000496 HConstant* constant = chunk_->LookupConstant(op);
497 DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
498 return constant->handle(isolate());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100499}
500
501
502bool LCodeGen::IsInteger32(LConstantOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000503 return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100504}
505
506
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000507bool LCodeGen::IsSmi(LConstantOperand* op) const {
508 return chunk_->LookupLiteralRepresentation(op).IsSmi();
509}
510
511
512int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
513 return ToRepresentation(op, Representation::Integer32());
514}
515
516
517int32_t LCodeGen::ToRepresentation(LConstantOperand* op,
518 const Representation& r) const {
519 HConstant* constant = chunk_->LookupConstant(op);
520 int32_t value = constant->Integer32Value();
521 if (r.IsInteger32()) return value;
522 DCHECK(r.IsSmiOrTagged());
523 return reinterpret_cast<int32_t>(Smi::FromInt(value));
524}
525
526
527Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
528 HConstant* constant = chunk_->LookupConstant(op);
529 return Smi::FromInt(constant->Integer32Value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100530}
531
532
533double LCodeGen::ToDouble(LConstantOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000534 HConstant* constant = chunk_->LookupConstant(op);
535 DCHECK(constant->HasDoubleValue());
536 return constant->DoubleValue();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100537}
538
539
540Operand LCodeGen::ToOperand(LOperand* op) {
541 if (op->IsConstantOperand()) {
542 LConstantOperand* const_op = LConstantOperand::cast(op);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000543 HConstant* constant = chunk()->LookupConstant(const_op);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100544 Representation r = chunk_->LookupLiteralRepresentation(const_op);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000545 if (r.IsSmi()) {
546 DCHECK(constant->HasSmiValue());
547 return Operand(Smi::FromInt(constant->Integer32Value()));
548 } else if (r.IsInteger32()) {
549 DCHECK(constant->HasInteger32Value());
550 return Operand(constant->Integer32Value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100551 } else if (r.IsDouble()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000552 Abort(kToOperandUnsupportedDoubleImmediate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100553 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000554 DCHECK(r.IsTagged());
555 return Operand(constant->handle(isolate()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100556 } else if (op->IsRegister()) {
557 return Operand(ToRegister(op));
558 } else if (op->IsDoubleRegister()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000559 Abort(kToOperandIsDoubleRegisterUnimplemented);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100560 return Operand(0);
561 }
562 // Stack slots not implemented, use ToMemOperand instead.
563 UNREACHABLE();
564 return Operand(0);
565}
566
567
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000568static int ArgumentsOffsetWithoutFrame(int index) {
569 DCHECK(index < 0);
570 return -(index + 1) * kPointerSize;
571}
572
573
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100574MemOperand LCodeGen::ToMemOperand(LOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000575 DCHECK(!op->IsRegister());
576 DCHECK(!op->IsDoubleRegister());
577 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
578 if (NeedsEagerFrame()) {
579 return MemOperand(fp, StackSlotOffset(op->index()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100580 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000581 // Retrieve parameter without eager stack-frame relative to the
582 // stack-pointer.
583 return MemOperand(sp, ArgumentsOffsetWithoutFrame(op->index()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100584 }
585}
586
587
588MemOperand LCodeGen::ToHighMemOperand(LOperand* op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000589 DCHECK(op->IsDoubleStackSlot());
590 if (NeedsEagerFrame()) {
591 return MemOperand(fp, StackSlotOffset(op->index()) + kPointerSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100592 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000593 // Retrieve parameter without eager stack-frame relative to the
594 // stack-pointer.
595 return MemOperand(
596 sp, ArgumentsOffsetWithoutFrame(op->index()) + kPointerSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100597 }
598}
599
600
601void LCodeGen::WriteTranslation(LEnvironment* environment,
602 Translation* translation) {
603 if (environment == NULL) return;
604
605 // The translation includes one command per value in the environment.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000606 int translation_size = environment->translation_size();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100607 // The output frame height does not include the parameters.
608 int height = translation_size - environment->parameter_count();
609
610 WriteTranslation(environment->outer(), translation);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000611 bool has_closure_id = !info()->closure().is_null() &&
612 !info()->closure().is_identical_to(environment->closure());
613 int closure_id = has_closure_id
614 ? DefineDeoptimizationLiteral(environment->closure())
615 : Translation::kSelfLiteralId;
616
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100617 switch (environment->frame_type()) {
618 case JS_FUNCTION:
619 translation->BeginJSFrame(environment->ast_id(), closure_id, height);
620 break;
621 case JS_CONSTRUCT:
622 translation->BeginConstructStubFrame(closure_id, translation_size);
623 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000624 case JS_GETTER:
625 DCHECK(translation_size == 1);
626 DCHECK(height == 0);
627 translation->BeginGetterStubFrame(closure_id);
628 break;
629 case JS_SETTER:
630 DCHECK(translation_size == 2);
631 DCHECK(height == 0);
632 translation->BeginSetterStubFrame(closure_id);
633 break;
634 case STUB:
635 translation->BeginCompiledStubFrame();
636 break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100637 case ARGUMENTS_ADAPTOR:
638 translation->BeginArgumentsAdaptorFrame(closure_id, translation_size);
639 break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100640 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000641
642 int object_index = 0;
643 int dematerialized_index = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100644 for (int i = 0; i < translation_size; ++i) {
645 LOperand* value = environment->values()->at(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000646 AddToTranslation(environment,
647 translation,
648 value,
649 environment->HasTaggedValueAt(i),
650 environment->HasUint32ValueAt(i),
651 &object_index,
652 &dematerialized_index);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100653 }
654}
655
656
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000657void LCodeGen::AddToTranslation(LEnvironment* environment,
658 Translation* translation,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100659 LOperand* op,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000660 bool is_tagged,
661 bool is_uint32,
662 int* object_index_pointer,
663 int* dematerialized_index_pointer) {
664 if (op == LEnvironment::materialization_marker()) {
665 int object_index = (*object_index_pointer)++;
666 if (environment->ObjectIsDuplicateAt(object_index)) {
667 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
668 translation->DuplicateObject(dupe_of);
669 return;
670 }
671 int object_length = environment->ObjectLengthAt(object_index);
672 if (environment->ObjectIsArgumentsAt(object_index)) {
673 translation->BeginArgumentsObject(object_length);
674 } else {
675 translation->BeginCapturedObject(object_length);
676 }
677 int dematerialized_index = *dematerialized_index_pointer;
678 int env_offset = environment->translation_size() + dematerialized_index;
679 *dematerialized_index_pointer += object_length;
680 for (int i = 0; i < object_length; ++i) {
681 LOperand* value = environment->values()->at(env_offset + i);
682 AddToTranslation(environment,
683 translation,
684 value,
685 environment->HasTaggedValueAt(env_offset + i),
686 environment->HasUint32ValueAt(env_offset + i),
687 object_index_pointer,
688 dematerialized_index_pointer);
689 }
690 return;
691 }
692
693 if (op->IsStackSlot()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100694 if (is_tagged) {
695 translation->StoreStackSlot(op->index());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000696 } else if (is_uint32) {
697 translation->StoreUint32StackSlot(op->index());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100698 } else {
699 translation->StoreInt32StackSlot(op->index());
700 }
701 } else if (op->IsDoubleStackSlot()) {
702 translation->StoreDoubleStackSlot(op->index());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100703 } else if (op->IsRegister()) {
704 Register reg = ToRegister(op);
705 if (is_tagged) {
706 translation->StoreRegister(reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000707 } else if (is_uint32) {
708 translation->StoreUint32Register(reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100709 } else {
710 translation->StoreInt32Register(reg);
711 }
712 } else if (op->IsDoubleRegister()) {
713 DoubleRegister reg = ToDoubleRegister(op);
714 translation->StoreDoubleRegister(reg);
715 } else if (op->IsConstantOperand()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000716 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
717 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100718 translation->StoreLiteral(src_index);
719 } else {
720 UNREACHABLE();
721 }
722}
723
724
725void LCodeGen::CallCode(Handle<Code> code,
726 RelocInfo::Mode mode,
727 LInstruction* instr) {
728 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT);
729}
730
731
732void LCodeGen::CallCodeGeneric(Handle<Code> code,
733 RelocInfo::Mode mode,
734 LInstruction* instr,
735 SafepointMode safepoint_mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000736 DCHECK(instr != NULL);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100737 __ Call(code, mode);
738 RecordSafepointWithLazyDeopt(instr, safepoint_mode);
739}
740
741
742void LCodeGen::CallRuntime(const Runtime::Function* function,
743 int num_arguments,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000744 LInstruction* instr,
745 SaveFPRegsMode save_doubles) {
746 DCHECK(instr != NULL);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100747
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000748 __ CallRuntime(function, num_arguments, save_doubles);
749
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100750 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
751}
752
753
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000754void LCodeGen::LoadContextFromDeferred(LOperand* context) {
755 if (context->IsRegister()) {
756 __ Move(cp, ToRegister(context));
757 } else if (context->IsStackSlot()) {
758 __ lw(cp, ToMemOperand(context));
759 } else if (context->IsConstantOperand()) {
760 HConstant* constant =
761 chunk_->LookupConstant(LConstantOperand::cast(context));
762 __ li(cp, Handle<Object>::cast(constant->handle(isolate())));
763 } else {
764 UNREACHABLE();
765 }
766}
767
768
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100769void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
770 int argc,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000771 LInstruction* instr,
772 LOperand* context) {
773 LoadContextFromDeferred(context);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100774 __ CallRuntimeSaveDoubles(id);
775 RecordSafepointWithRegisters(
776 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
777}
778
779
780void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
781 Safepoint::DeoptMode mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000782 environment->set_has_been_used();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100783 if (!environment->HasBeenRegistered()) {
784 // Physical stack frame layout:
785 // -x ............. -4 0 ..................................... y
786 // [incoming arguments] [spill slots] [pushed outgoing arguments]
787
788 // Layout of the environment:
789 // 0 ..................................................... size-1
790 // [parameters] [locals] [expression stack including arguments]
791
792 // Layout of the translation:
793 // 0 ........................................................ size - 1 + 4
794 // [expression stack including arguments] [locals] [4 words] [parameters]
795 // |>------------ translation_size ------------<|
796
797 int frame_count = 0;
798 int jsframe_count = 0;
799 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
800 ++frame_count;
801 if (e->frame_type() == JS_FUNCTION) {
802 ++jsframe_count;
803 }
804 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000805 Translation translation(&translations_, frame_count, jsframe_count, zone());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100806 WriteTranslation(environment, &translation);
807 int deoptimization_index = deoptimizations_.length();
808 int pc_offset = masm()->pc_offset();
809 environment->Register(deoptimization_index,
810 translation.index(),
811 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000812 deoptimizations_.Add(environment, zone());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100813 }
814}
815
816
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000817void LCodeGen::DeoptimizeIf(Condition condition, LInstruction* instr,
818 Deoptimizer::BailoutType bailout_type,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400819 const char* detail, Register src1,
820 const Operand& src2) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000821 LEnvironment* environment = instr->environment();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100822 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000823 DCHECK(environment->HasBeenRegistered());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100824 int id = environment->deoptimization_index();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000825 DCHECK(info()->IsOptimizing() || info()->IsStub());
826 Address entry =
827 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100828 if (entry == NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000829 Abort(kBailoutWasNotPrepared);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100830 return;
831 }
832
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000833 if (FLAG_deopt_every_n_times != 0 && !info()->IsStub()) {
834 Register scratch = scratch0();
835 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
836 Label no_deopt;
837 __ Push(a1, scratch);
838 __ li(scratch, Operand(count));
839 __ lw(a1, MemOperand(scratch));
840 __ Subu(a1, a1, Operand(1));
841 __ Branch(&no_deopt, ne, a1, Operand(zero_reg));
842 __ li(a1, Operand(FLAG_deopt_every_n_times));
843 __ sw(a1, MemOperand(scratch));
844 __ Pop(a1, scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100845
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000846 __ Call(entry, RelocInfo::RUNTIME_ENTRY);
847 __ bind(&no_deopt);
848 __ sw(a1, MemOperand(scratch));
849 __ Pop(a1, scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100850 }
851
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000852 if (info()->ShouldTrapOnDeopt()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100853 Label skip;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000854 if (condition != al) {
855 __ Branch(&skip, NegateCondition(condition), src1, src2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100856 }
857 __ stop("trap_on_deopt");
858 __ bind(&skip);
859 }
860
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000861 Deoptimizer::Reason reason(instr->hydrogen_value()->position().raw(),
862 instr->Mnemonic(), detail);
863 DCHECK(info()->IsStub() || frame_is_built_);
864 // Go through jump table if we need to handle condition, build frame, or
865 // restore caller doubles.
866 if (condition == al && frame_is_built_ &&
867 !info()->saves_caller_doubles()) {
868 DeoptComment(reason);
869 __ Call(entry, RelocInfo::RUNTIME_ENTRY, condition, src1, src2);
870 } else {
871 Deoptimizer::JumpTableEntry table_entry(entry, reason, bailout_type,
872 !frame_is_built_);
873 // We often have several deopts to the same entry, reuse the last
874 // jump entry if this is the case.
875 if (jump_table_.is_empty() ||
876 !table_entry.IsEquivalentTo(jump_table_.last())) {
877 jump_table_.Add(table_entry, zone());
878 }
879 __ Branch(&jump_table_.last().label, condition, src1, src2);
880 }
881}
882
883
884void LCodeGen::DeoptimizeIf(Condition condition, LInstruction* instr,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400885 const char* detail, Register src1,
886 const Operand& src2) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000887 Deoptimizer::BailoutType bailout_type = info()->IsStub()
888 ? Deoptimizer::LAZY
889 : Deoptimizer::EAGER;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400890 DeoptimizeIf(condition, instr, bailout_type, detail, src1, src2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100891}
892
893
894void LCodeGen::PopulateDeoptimizationData(Handle<Code> code) {
895 int length = deoptimizations_.length();
896 if (length == 0) return;
897 Handle<DeoptimizationInputData> data =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000898 DeoptimizationInputData::New(isolate(), length, TENURED);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100899
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000900 Handle<ByteArray> translations =
901 translations_.CreateByteArray(isolate()->factory());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100902 data->SetTranslationByteArray(*translations);
903 data->SetInlinedFunctionCount(Smi::FromInt(inlined_function_count_));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000904 data->SetOptimizationId(Smi::FromInt(info_->optimization_id()));
905 if (info_->IsOptimizing()) {
906 // Reference to shared function info does not change between phases.
907 AllowDeferredHandleDereference allow_handle_dereference;
908 data->SetSharedFunctionInfo(*info_->shared_info());
909 } else {
910 data->SetSharedFunctionInfo(Smi::FromInt(0));
911 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100912
913 Handle<FixedArray> literals =
914 factory()->NewFixedArray(deoptimization_literals_.length(), TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000915 { AllowDeferredHandleDereference copy_handles;
916 for (int i = 0; i < deoptimization_literals_.length(); i++) {
917 literals->set(i, *deoptimization_literals_[i]);
918 }
919 data->SetLiteralArray(*literals);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100920 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100921
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000922 data->SetOsrAstId(Smi::FromInt(info_->osr_ast_id().ToInt()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100923 data->SetOsrPcOffset(Smi::FromInt(osr_pc_offset_));
924
925 // Populate the deoptimization entries.
926 for (int i = 0; i < length; i++) {
927 LEnvironment* env = deoptimizations_[i];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000928 data->SetAstId(i, env->ast_id());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100929 data->SetTranslationIndex(i, Smi::FromInt(env->translation_index()));
930 data->SetArgumentsStackHeight(i,
931 Smi::FromInt(env->arguments_stack_height()));
932 data->SetPc(i, Smi::FromInt(env->pc_offset()));
933 }
934 code->set_deoptimization_data(*data);
935}
936
937
938int LCodeGen::DefineDeoptimizationLiteral(Handle<Object> literal) {
939 int result = deoptimization_literals_.length();
940 for (int i = 0; i < deoptimization_literals_.length(); ++i) {
941 if (deoptimization_literals_[i].is_identical_to(literal)) return i;
942 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000943 deoptimization_literals_.Add(literal, zone());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100944 return result;
945}
946
947
948void LCodeGen::PopulateDeoptimizationLiteralsWithInlinedFunctions() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000949 DCHECK(deoptimization_literals_.length() == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100950
951 const ZoneList<Handle<JSFunction> >* inlined_closures =
952 chunk()->inlined_closures();
953
954 for (int i = 0, length = inlined_closures->length();
955 i < length;
956 i++) {
957 DefineDeoptimizationLiteral(inlined_closures->at(i));
958 }
959
960 inlined_function_count_ = deoptimization_literals_.length();
961}
962
963
964void LCodeGen::RecordSafepointWithLazyDeopt(
965 LInstruction* instr, SafepointMode safepoint_mode) {
966 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
967 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
968 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000969 DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100970 RecordSafepointWithRegisters(
971 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
972 }
973}
974
975
976void LCodeGen::RecordSafepoint(
977 LPointerMap* pointers,
978 Safepoint::Kind kind,
979 int arguments,
980 Safepoint::DeoptMode deopt_mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000981 DCHECK(expected_safepoint_kind_ == kind);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100982
983 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
984 Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
985 kind, arguments, deopt_mode);
986 for (int i = 0; i < operands->length(); i++) {
987 LOperand* pointer = operands->at(i);
988 if (pointer->IsStackSlot()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000989 safepoint.DefinePointerSlot(pointer->index(), zone());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100990 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000991 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100992 }
993 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100994}
995
996
997void LCodeGen::RecordSafepoint(LPointerMap* pointers,
998 Safepoint::DeoptMode deopt_mode) {
999 RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
1000}
1001
1002
1003void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001004 LPointerMap empty_pointers(zone());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001005 RecordSafepoint(&empty_pointers, deopt_mode);
1006}
1007
1008
1009void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
1010 int arguments,
1011 Safepoint::DeoptMode deopt_mode) {
1012 RecordSafepoint(
1013 pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
1014}
1015
1016
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001017void LCodeGen::RecordAndWritePosition(int position) {
1018 if (position == RelocInfo::kNoPosition) return;
1019 masm()->positions_recorder()->RecordPosition(position);
1020 masm()->positions_recorder()->WriteRecordedPositions();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001021}
1022
1023
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001024static const char* LabelType(LLabel* label) {
1025 if (label->is_loop_header()) return " (loop header)";
1026 if (label->is_osr_entry()) return " (OSR entry)";
1027 return "";
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001028}
1029
1030
1031void LCodeGen::DoLabel(LLabel* label) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001032 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
1033 current_instruction_,
1034 label->hydrogen_value()->id(),
1035 label->block_id(),
1036 LabelType(label));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001037 __ bind(label->label());
1038 current_block_ = label->block_id();
1039 DoGap(label);
1040}
1041
1042
1043void LCodeGen::DoParallelMove(LParallelMove* move) {
1044 resolver_.Resolve(move);
1045}
1046
1047
1048void LCodeGen::DoGap(LGap* gap) {
1049 for (int i = LGap::FIRST_INNER_POSITION;
1050 i <= LGap::LAST_INNER_POSITION;
1051 i++) {
1052 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
1053 LParallelMove* move = gap->GetParallelMove(inner_pos);
1054 if (move != NULL) DoParallelMove(move);
1055 }
1056}
1057
1058
1059void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
1060 DoGap(instr);
1061}
1062
1063
1064void LCodeGen::DoParameter(LParameter* instr) {
1065 // Nothing to do.
1066}
1067
1068
1069void LCodeGen::DoCallStub(LCallStub* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001070 DCHECK(ToRegister(instr->context()).is(cp));
1071 DCHECK(ToRegister(instr->result()).is(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001072 switch (instr->hydrogen()->major_key()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001073 case CodeStub::RegExpExec: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001074 RegExpExecStub stub(isolate());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001075 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1076 break;
1077 }
1078 case CodeStub::SubString: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001079 SubStringStub stub(isolate());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001080 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1081 break;
1082 }
1083 case CodeStub::StringCompare: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001084 StringCompareStub stub(isolate());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001085 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
1086 break;
1087 }
1088 default:
1089 UNREACHABLE();
1090 }
1091}
1092
1093
1094void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001095 GenerateOsrPrologue();
1096}
1097
1098
1099void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
1100 Register dividend = ToRegister(instr->dividend());
1101 int32_t divisor = instr->divisor();
1102 DCHECK(dividend.is(ToRegister(instr->result())));
1103
1104 // Theoretically, a variation of the branch-free code for integer division by
1105 // a power of 2 (calculating the remainder via an additional multiplication
1106 // (which gets simplified to an 'and') and subtraction) should be faster, and
1107 // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
1108 // indicate that positive dividends are heavily favored, so the branching
1109 // version performs better.
1110 HMod* hmod = instr->hydrogen();
1111 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1112 Label dividend_is_not_negative, done;
1113
1114 if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
1115 __ Branch(&dividend_is_not_negative, ge, dividend, Operand(zero_reg));
1116 // Note: The code below even works when right contains kMinInt.
1117 __ subu(dividend, zero_reg, dividend);
1118 __ And(dividend, dividend, Operand(mask));
1119 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001120 DeoptimizeIf(eq, instr, "minus zero", dividend, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001121 }
1122 __ Branch(USE_DELAY_SLOT, &done);
1123 __ subu(dividend, zero_reg, dividend);
1124 }
1125
1126 __ bind(&dividend_is_not_negative);
1127 __ And(dividend, dividend, Operand(mask));
1128 __ bind(&done);
1129}
1130
1131
1132void LCodeGen::DoModByConstI(LModByConstI* instr) {
1133 Register dividend = ToRegister(instr->dividend());
1134 int32_t divisor = instr->divisor();
1135 Register result = ToRegister(instr->result());
1136 DCHECK(!dividend.is(result));
1137
1138 if (divisor == 0) {
1139 DeoptimizeIf(al, instr);
1140 return;
1141 }
1142
1143 __ TruncatingDiv(result, dividend, Abs(divisor));
1144 __ Mul(result, result, Operand(Abs(divisor)));
1145 __ Subu(result, dividend, Operand(result));
1146
1147 // Check for negative zero.
1148 HMod* hmod = instr->hydrogen();
1149 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1150 Label remainder_not_zero;
1151 __ Branch(&remainder_not_zero, ne, result, Operand(zero_reg));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001152 DeoptimizeIf(lt, instr, "minus zero", dividend, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001153 __ bind(&remainder_not_zero);
1154 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001155}
1156
1157
1158void LCodeGen::DoModI(LModI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001159 HMod* hmod = instr->hydrogen();
1160 const Register left_reg = ToRegister(instr->left());
1161 const Register right_reg = ToRegister(instr->right());
1162 const Register result_reg = ToRegister(instr->result());
1163
1164 // div runs in the background while we check for special cases.
1165 __ Mod(result_reg, left_reg, right_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001166
1167 Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001168 // Check for x % 0, we have to deopt in this case because we can't return a
1169 // NaN.
1170 if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001171 DeoptimizeIf(eq, instr, "division by zero", right_reg, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001172 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001173
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001174 // Check for kMinInt % -1, div will return kMinInt, which is not what we
1175 // want. We have to deopt if we care about -0, because we can't return that.
1176 if (hmod->CheckFlag(HValue::kCanOverflow)) {
1177 Label no_overflow_possible;
1178 __ Branch(&no_overflow_possible, ne, left_reg, Operand(kMinInt));
1179 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001180 DeoptimizeIf(eq, instr, "minus zero", right_reg, Operand(-1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001181 } else {
1182 __ Branch(&no_overflow_possible, ne, right_reg, Operand(-1));
1183 __ Branch(USE_DELAY_SLOT, &done);
1184 __ mov(result_reg, zero_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001185 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001186 __ bind(&no_overflow_possible);
1187 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001188
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001189 // If we care about -0, test if the dividend is <0 and the result is 0.
1190 __ Branch(&done, ge, left_reg, Operand(zero_reg));
1191 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001192 DeoptimizeIf(eq, instr, "minus zero", result_reg, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001193 }
1194 __ bind(&done);
1195}
1196
1197
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001198void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
1199 Register dividend = ToRegister(instr->dividend());
1200 int32_t divisor = instr->divisor();
1201 Register result = ToRegister(instr->result());
1202 DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
1203 DCHECK(!result.is(dividend));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001204
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001205 // Check for (0 / -x) that will produce negative zero.
1206 HDiv* hdiv = instr->hydrogen();
1207 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001208 DeoptimizeIf(eq, instr, "minus zero", dividend, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001209 }
1210 // Check for (kMinInt / -1).
1211 if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001212 DeoptimizeIf(eq, instr, "overflow", dividend, Operand(kMinInt));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001213 }
1214 // Deoptimize if remainder will not be 0.
1215 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1216 divisor != 1 && divisor != -1) {
1217 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1218 __ And(at, dividend, Operand(mask));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001219 DeoptimizeIf(ne, instr, "lost precision", at, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001220 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001221
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001222 if (divisor == -1) { // Nice shortcut, not needed for correctness.
1223 __ Subu(result, zero_reg, dividend);
1224 return;
1225 }
1226 uint16_t shift = WhichPowerOf2Abs(divisor);
1227 if (shift == 0) {
1228 __ Move(result, dividend);
1229 } else if (shift == 1) {
1230 __ srl(result, dividend, 31);
1231 __ Addu(result, dividend, Operand(result));
1232 } else {
1233 __ sra(result, dividend, 31);
1234 __ srl(result, result, 32 - shift);
1235 __ Addu(result, dividend, Operand(result));
1236 }
1237 if (shift > 0) __ sra(result, result, shift);
1238 if (divisor < 0) __ Subu(result, zero_reg, result);
1239}
1240
1241
1242void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
1243 Register dividend = ToRegister(instr->dividend());
1244 int32_t divisor = instr->divisor();
1245 Register result = ToRegister(instr->result());
1246 DCHECK(!dividend.is(result));
1247
1248 if (divisor == 0) {
1249 DeoptimizeIf(al, instr);
1250 return;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001251 }
1252
1253 // Check for (0 / -x) that will produce negative zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001254 HDiv* hdiv = instr->hydrogen();
1255 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001256 DeoptimizeIf(eq, instr, "minus zero", dividend, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001257 }
1258
1259 __ TruncatingDiv(result, dividend, Abs(divisor));
1260 if (divisor < 0) __ Subu(result, zero_reg, result);
1261
1262 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1263 __ Mul(scratch0(), result, Operand(divisor));
1264 __ Subu(scratch0(), scratch0(), dividend);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001265 DeoptimizeIf(ne, instr, "lost precision", scratch0(), Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001266 }
1267}
1268
1269
1270// TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
1271void LCodeGen::DoDivI(LDivI* instr) {
1272 HBinaryOperation* hdiv = instr->hydrogen();
1273 Register dividend = ToRegister(instr->dividend());
1274 Register divisor = ToRegister(instr->divisor());
1275 const Register result = ToRegister(instr->result());
1276 Register remainder = ToRegister(instr->temp());
1277
1278 // On MIPS div is asynchronous - it will run in the background while we
1279 // check for special cases.
1280 __ Div(remainder, result, dividend, divisor);
1281
1282 // Check for x / 0.
1283 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001284 DeoptimizeIf(eq, instr, "division by zero", divisor, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001285 }
1286
1287 // Check for (0 / -x) that will produce negative zero.
1288 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001289 Label left_not_zero;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001290 __ Branch(&left_not_zero, ne, dividend, Operand(zero_reg));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001291 DeoptimizeIf(lt, instr, "minus zero", divisor, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001292 __ bind(&left_not_zero);
1293 }
1294
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001295 // Check for (kMinInt / -1).
1296 if (hdiv->CheckFlag(HValue::kCanOverflow) &&
1297 !hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001298 Label left_not_min_int;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001299 __ Branch(&left_not_min_int, ne, dividend, Operand(kMinInt));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001300 DeoptimizeIf(eq, instr, "overflow", divisor, Operand(-1));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001301 __ bind(&left_not_min_int);
1302 }
1303
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001304 if (!hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001305 DeoptimizeIf(ne, instr, "lost precision", remainder, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001306 }
1307}
1308
1309
1310void LCodeGen::DoMultiplyAddD(LMultiplyAddD* instr) {
1311 DoubleRegister addend = ToDoubleRegister(instr->addend());
1312 DoubleRegister multiplier = ToDoubleRegister(instr->multiplier());
1313 DoubleRegister multiplicand = ToDoubleRegister(instr->multiplicand());
1314
1315 // This is computed in-place.
1316 DCHECK(addend.is(ToDoubleRegister(instr->result())));
1317
1318 __ madd_d(addend, addend, multiplier, multiplicand);
1319}
1320
1321
1322void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
1323 Register dividend = ToRegister(instr->dividend());
1324 Register result = ToRegister(instr->result());
1325 int32_t divisor = instr->divisor();
1326 Register scratch = result.is(dividend) ? scratch0() : dividend;
1327 DCHECK(!result.is(dividend) || !scratch.is(dividend));
1328
1329 // If the divisor is 1, return the dividend.
1330 if (divisor == 1) {
1331 __ Move(result, dividend);
1332 return;
1333 }
1334
1335 // If the divisor is positive, things are easy: There can be no deopts and we
1336 // can simply do an arithmetic right shift.
1337 uint16_t shift = WhichPowerOf2Abs(divisor);
1338 if (divisor > 1) {
1339 __ sra(result, dividend, shift);
1340 return;
1341 }
1342
1343 // If the divisor is negative, we have to negate and handle edge cases.
1344
1345 // dividend can be the same register as result so save the value of it
1346 // for checking overflow.
1347 __ Move(scratch, dividend);
1348
1349 __ Subu(result, zero_reg, dividend);
1350 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001351 DeoptimizeIf(eq, instr, "minus zero", result, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001352 }
1353
1354 // Dividing by -1 is basically negation, unless we overflow.
1355 __ Xor(scratch, scratch, result);
1356 if (divisor == -1) {
1357 if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001358 DeoptimizeIf(ge, instr, "overflow", scratch, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001359 }
1360 return;
1361 }
1362
1363 // If the negation could not overflow, simply shifting is OK.
1364 if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1365 __ sra(result, result, shift);
1366 return;
1367 }
1368
1369 Label no_overflow, done;
1370 __ Branch(&no_overflow, lt, scratch, Operand(zero_reg));
1371 __ li(result, Operand(kMinInt / divisor));
1372 __ Branch(&done);
1373 __ bind(&no_overflow);
1374 __ sra(result, result, shift);
1375 __ bind(&done);
1376}
1377
1378
1379void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
1380 Register dividend = ToRegister(instr->dividend());
1381 int32_t divisor = instr->divisor();
1382 Register result = ToRegister(instr->result());
1383 DCHECK(!dividend.is(result));
1384
1385 if (divisor == 0) {
1386 DeoptimizeIf(al, instr);
1387 return;
1388 }
1389
1390 // Check for (0 / -x) that will produce negative zero.
1391 HMathFloorOfDiv* hdiv = instr->hydrogen();
1392 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001393 DeoptimizeIf(eq, instr, "minus zero", dividend, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001394 }
1395
1396 // Easy case: We need no dynamic check for the dividend and the flooring
1397 // division is the same as the truncating division.
1398 if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
1399 (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
1400 __ TruncatingDiv(result, dividend, Abs(divisor));
1401 if (divisor < 0) __ Subu(result, zero_reg, result);
1402 return;
1403 }
1404
1405 // In the general case we may need to adjust before and after the truncating
1406 // division to get a flooring division.
1407 Register temp = ToRegister(instr->temp());
1408 DCHECK(!temp.is(dividend) && !temp.is(result));
1409 Label needs_adjustment, done;
1410 __ Branch(&needs_adjustment, divisor > 0 ? lt : gt,
1411 dividend, Operand(zero_reg));
1412 __ TruncatingDiv(result, dividend, Abs(divisor));
1413 if (divisor < 0) __ Subu(result, zero_reg, result);
1414 __ jmp(&done);
1415 __ bind(&needs_adjustment);
1416 __ Addu(temp, dividend, Operand(divisor > 0 ? 1 : -1));
1417 __ TruncatingDiv(result, temp, Abs(divisor));
1418 if (divisor < 0) __ Subu(result, zero_reg, result);
1419 __ Subu(result, result, Operand(1));
1420 __ bind(&done);
1421}
1422
1423
1424// TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
1425void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
1426 HBinaryOperation* hdiv = instr->hydrogen();
1427 Register dividend = ToRegister(instr->dividend());
1428 Register divisor = ToRegister(instr->divisor());
1429 const Register result = ToRegister(instr->result());
1430 Register remainder = scratch0();
1431 // On MIPS div is asynchronous - it will run in the background while we
1432 // check for special cases.
1433 __ Div(remainder, result, dividend, divisor);
1434
1435 // Check for x / 0.
1436 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001437 DeoptimizeIf(eq, instr, "division by zero", divisor, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001438 }
1439
1440 // Check for (0 / -x) that will produce negative zero.
1441 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1442 Label left_not_zero;
1443 __ Branch(&left_not_zero, ne, dividend, Operand(zero_reg));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001444 DeoptimizeIf(lt, instr, "minus zero", divisor, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001445 __ bind(&left_not_zero);
1446 }
1447
1448 // Check for (kMinInt / -1).
1449 if (hdiv->CheckFlag(HValue::kCanOverflow) &&
1450 !hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1451 Label left_not_min_int;
1452 __ Branch(&left_not_min_int, ne, dividend, Operand(kMinInt));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001453 DeoptimizeIf(eq, instr, "overflow", divisor, Operand(-1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001454 __ bind(&left_not_min_int);
1455 }
1456
1457 // We performed a truncating division. Correct the result if necessary.
1458 Label done;
1459 __ Branch(&done, eq, remainder, Operand(zero_reg), USE_DELAY_SLOT);
1460 __ Xor(remainder, remainder, Operand(divisor));
1461 __ Branch(&done, ge, remainder, Operand(zero_reg));
1462 __ Subu(result, result, Operand(1));
1463 __ bind(&done);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001464}
1465
1466
1467void LCodeGen::DoMulI(LMulI* instr) {
1468 Register scratch = scratch0();
1469 Register result = ToRegister(instr->result());
1470 // Note that result may alias left.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001471 Register left = ToRegister(instr->left());
1472 LOperand* right_op = instr->right();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001473
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001474 bool bailout_on_minus_zero =
1475 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001476 bool overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001477
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001478 if (right_op->IsConstantOperand()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001479 int32_t constant = ToInteger32(LConstantOperand::cast(right_op));
1480
1481 if (bailout_on_minus_zero && (constant < 0)) {
1482 // The case of a null constant will be handled separately.
1483 // If constant is negative and left is null, the result should be -0.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001484 DeoptimizeIf(eq, instr, "minus zero", left, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001485 }
1486
1487 switch (constant) {
1488 case -1:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001489 if (overflow) {
1490 __ SubuAndCheckForOverflow(result, zero_reg, left, scratch);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001491 DeoptimizeIf(lt, instr, "overflow", scratch, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001492 } else {
1493 __ Subu(result, zero_reg, left);
1494 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001495 break;
1496 case 0:
1497 if (bailout_on_minus_zero) {
1498 // If left is strictly negative and the constant is null, the
1499 // result is -0. Deoptimize if required, otherwise return 0.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001500 DeoptimizeIf(lt, instr, "minus zero", left, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001501 }
1502 __ mov(result, zero_reg);
1503 break;
1504 case 1:
1505 // Nothing to do.
1506 __ Move(result, left);
1507 break;
1508 default:
1509 // Multiplying by powers of two and powers of two plus or minus
1510 // one can be done faster with shifted operands.
1511 // For other constants we emit standard code.
1512 int32_t mask = constant >> 31;
1513 uint32_t constant_abs = (constant + mask) ^ mask;
1514
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001515 if (base::bits::IsPowerOfTwo32(constant_abs)) {
1516 int32_t shift = WhichPowerOf2(constant_abs);
1517 __ sll(result, left, shift);
1518 // Correct the sign of the result if the constant is negative.
1519 if (constant < 0) __ Subu(result, zero_reg, result);
1520 } else if (base::bits::IsPowerOfTwo32(constant_abs - 1)) {
1521 int32_t shift = WhichPowerOf2(constant_abs - 1);
1522 __ sll(scratch, left, shift);
1523 __ Addu(result, scratch, left);
1524 // Correct the sign of the result if the constant is negative.
1525 if (constant < 0) __ Subu(result, zero_reg, result);
1526 } else if (base::bits::IsPowerOfTwo32(constant_abs + 1)) {
1527 int32_t shift = WhichPowerOf2(constant_abs + 1);
1528 __ sll(scratch, left, shift);
1529 __ Subu(result, scratch, left);
1530 // Correct the sign of the result if the constant is negative.
1531 if (constant < 0) __ Subu(result, zero_reg, result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001532 } else {
1533 // Generate standard code.
1534 __ li(at, constant);
1535 __ Mul(result, left, at);
1536 }
1537 }
1538
1539 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001540 DCHECK(right_op->IsRegister());
1541 Register right = ToRegister(right_op);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001542
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001543 if (overflow) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001544 // hi:lo = left * right.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001545 if (instr->hydrogen()->representation().IsSmi()) {
1546 __ SmiUntag(result, left);
1547 __ Mul(scratch, result, result, right);
1548 } else {
1549 __ Mul(scratch, result, left, right);
1550 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001551 __ sra(at, result, 31);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001552 DeoptimizeIf(ne, instr, "overflow", scratch, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001553 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001554 if (instr->hydrogen()->representation().IsSmi()) {
1555 __ SmiUntag(result, left);
1556 __ Mul(result, result, right);
1557 } else {
1558 __ Mul(result, left, right);
1559 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001560 }
1561
1562 if (bailout_on_minus_zero) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001563 Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001564 __ Xor(at, left, right);
1565 __ Branch(&done, ge, at, Operand(zero_reg));
1566 // Bail out if the result is minus zero.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001567 DeoptimizeIf(eq, instr, "minus zero", result, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001568 __ bind(&done);
1569 }
1570 }
1571}
1572
1573
1574void LCodeGen::DoBitI(LBitI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001575 LOperand* left_op = instr->left();
1576 LOperand* right_op = instr->right();
1577 DCHECK(left_op->IsRegister());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001578 Register left = ToRegister(left_op);
1579 Register result = ToRegister(instr->result());
1580 Operand right(no_reg);
1581
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001582 if (right_op->IsStackSlot()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001583 right = Operand(EmitLoadRegister(right_op, at));
1584 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001585 DCHECK(right_op->IsRegister() || right_op->IsConstantOperand());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001586 right = ToOperand(right_op);
1587 }
1588
1589 switch (instr->op()) {
1590 case Token::BIT_AND:
1591 __ And(result, left, right);
1592 break;
1593 case Token::BIT_OR:
1594 __ Or(result, left, right);
1595 break;
1596 case Token::BIT_XOR:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001597 if (right_op->IsConstantOperand() && right.immediate() == int32_t(~0)) {
1598 __ Nor(result, zero_reg, left);
1599 } else {
1600 __ Xor(result, left, right);
1601 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001602 break;
1603 default:
1604 UNREACHABLE();
1605 break;
1606 }
1607}
1608
1609
1610void LCodeGen::DoShiftI(LShiftI* instr) {
1611 // Both 'left' and 'right' are "used at start" (see LCodeGen::DoShift), so
1612 // result may alias either of them.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001613 LOperand* right_op = instr->right();
1614 Register left = ToRegister(instr->left());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001615 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001616 Register scratch = scratch0();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001617
1618 if (right_op->IsRegister()) {
1619 // No need to mask the right operand on MIPS, it is built into the variable
1620 // shift instructions.
1621 switch (instr->op()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001622 case Token::ROR:
1623 __ Ror(result, left, Operand(ToRegister(right_op)));
1624 break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001625 case Token::SAR:
1626 __ srav(result, left, ToRegister(right_op));
1627 break;
1628 case Token::SHR:
1629 __ srlv(result, left, ToRegister(right_op));
1630 if (instr->can_deopt()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001631 DeoptimizeIf(lt, instr, "negative value", result, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001632 }
1633 break;
1634 case Token::SHL:
1635 __ sllv(result, left, ToRegister(right_op));
1636 break;
1637 default:
1638 UNREACHABLE();
1639 break;
1640 }
1641 } else {
1642 // Mask the right_op operand.
1643 int value = ToInteger32(LConstantOperand::cast(right_op));
1644 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1645 switch (instr->op()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001646 case Token::ROR:
1647 if (shift_count != 0) {
1648 __ Ror(result, left, Operand(shift_count));
1649 } else {
1650 __ Move(result, left);
1651 }
1652 break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001653 case Token::SAR:
1654 if (shift_count != 0) {
1655 __ sra(result, left, shift_count);
1656 } else {
1657 __ Move(result, left);
1658 }
1659 break;
1660 case Token::SHR:
1661 if (shift_count != 0) {
1662 __ srl(result, left, shift_count);
1663 } else {
1664 if (instr->can_deopt()) {
1665 __ And(at, left, Operand(0x80000000));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001666 DeoptimizeIf(ne, instr, "negative value", at, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001667 }
1668 __ Move(result, left);
1669 }
1670 break;
1671 case Token::SHL:
1672 if (shift_count != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001673 if (instr->hydrogen_value()->representation().IsSmi() &&
1674 instr->can_deopt()) {
1675 if (shift_count != 1) {
1676 __ sll(result, left, shift_count - 1);
1677 __ SmiTagCheckOverflow(result, result, scratch);
1678 } else {
1679 __ SmiTagCheckOverflow(result, left, scratch);
1680 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001681 DeoptimizeIf(lt, instr, "overflow", scratch, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001682 } else {
1683 __ sll(result, left, shift_count);
1684 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001685 } else {
1686 __ Move(result, left);
1687 }
1688 break;
1689 default:
1690 UNREACHABLE();
1691 break;
1692 }
1693 }
1694}
1695
1696
1697void LCodeGen::DoSubI(LSubI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001698 LOperand* left = instr->left();
1699 LOperand* right = instr->right();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001700 LOperand* result = instr->result();
1701 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1702
1703 if (!can_overflow) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001704 if (right->IsStackSlot()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001705 Register right_reg = EmitLoadRegister(right, at);
1706 __ Subu(ToRegister(result), ToRegister(left), Operand(right_reg));
1707 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001708 DCHECK(right->IsRegister() || right->IsConstantOperand());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001709 __ Subu(ToRegister(result), ToRegister(left), ToOperand(right));
1710 }
1711 } else { // can_overflow.
1712 Register overflow = scratch0();
1713 Register scratch = scratch1();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001714 if (right->IsStackSlot() || right->IsConstantOperand()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001715 Register right_reg = EmitLoadRegister(right, scratch);
1716 __ SubuAndCheckForOverflow(ToRegister(result),
1717 ToRegister(left),
1718 right_reg,
1719 overflow); // Reg at also used as scratch.
1720 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001721 DCHECK(right->IsRegister());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001722 // Due to overflow check macros not supporting constant operands,
1723 // handling the IsConstantOperand case was moved to prev if clause.
1724 __ SubuAndCheckForOverflow(ToRegister(result),
1725 ToRegister(left),
1726 ToRegister(right),
1727 overflow); // Reg at also used as scratch.
1728 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001729 DeoptimizeIf(lt, instr, "overflow", overflow, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001730 }
1731}
1732
1733
1734void LCodeGen::DoConstantI(LConstantI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001735 __ li(ToRegister(instr->result()), Operand(instr->value()));
1736}
1737
1738
1739void LCodeGen::DoConstantS(LConstantS* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001740 __ li(ToRegister(instr->result()), Operand(instr->value()));
1741}
1742
1743
1744void LCodeGen::DoConstantD(LConstantD* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001745 DCHECK(instr->result()->IsDoubleRegister());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001746 DoubleRegister result = ToDoubleRegister(instr->result());
1747 double v = instr->value();
1748 __ Move(result, v);
1749}
1750
1751
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001752void LCodeGen::DoConstantE(LConstantE* instr) {
1753 __ li(ToRegister(instr->result()), Operand(instr->value()));
1754}
1755
1756
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001757void LCodeGen::DoConstantT(LConstantT* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001758 Handle<Object> object = instr->value(isolate());
1759 AllowDeferredHandleDereference smi_check;
1760 __ li(ToRegister(instr->result()), object);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001761}
1762
1763
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001764void LCodeGen::DoMapEnumLength(LMapEnumLength* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001765 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001766 Register map = ToRegister(instr->value());
1767 __ EnumLength(result, map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001768}
1769
1770
1771void LCodeGen::DoDateField(LDateField* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001772 Register object = ToRegister(instr->date());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001773 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001774 Register scratch = ToRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001775 Smi* index = instr->index();
1776 Label runtime, done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001777 DCHECK(object.is(a0));
1778 DCHECK(result.is(v0));
1779 DCHECK(!scratch.is(scratch0()));
1780 DCHECK(!scratch.is(object));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001781
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001782 __ SmiTst(object, at);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001783 DeoptimizeIf(eq, instr, "Smi", at, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001784 __ GetObjectType(object, scratch, scratch);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001785 DeoptimizeIf(ne, instr, "not a date object", scratch, Operand(JS_DATE_TYPE));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001786
1787 if (index->value() == 0) {
1788 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset));
1789 } else {
1790 if (index->value() < JSDate::kFirstUncachedField) {
1791 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
1792 __ li(scratch, Operand(stamp));
1793 __ lw(scratch, MemOperand(scratch));
1794 __ lw(scratch0(), FieldMemOperand(object, JSDate::kCacheStampOffset));
1795 __ Branch(&runtime, ne, scratch, Operand(scratch0()));
1796 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset +
1797 kPointerSize * index->value()));
1798 __ jmp(&done);
1799 }
1800 __ bind(&runtime);
1801 __ PrepareCallCFunction(2, scratch);
1802 __ li(a1, Operand(index));
1803 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
1804 __ bind(&done);
1805 }
1806}
1807
1808
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001809MemOperand LCodeGen::BuildSeqStringOperand(Register string,
1810 LOperand* index,
1811 String::Encoding encoding) {
1812 if (index->IsConstantOperand()) {
1813 int offset = ToInteger32(LConstantOperand::cast(index));
1814 if (encoding == String::TWO_BYTE_ENCODING) {
1815 offset *= kUC16Size;
1816 }
1817 STATIC_ASSERT(kCharSize == 1);
1818 return FieldMemOperand(string, SeqString::kHeaderSize + offset);
1819 }
1820 Register scratch = scratch0();
1821 DCHECK(!scratch.is(string));
1822 DCHECK(!scratch.is(ToRegister(index)));
1823 if (encoding == String::ONE_BYTE_ENCODING) {
1824 __ Addu(scratch, string, ToRegister(index));
1825 } else {
1826 STATIC_ASSERT(kUC16Size == 2);
1827 __ sll(scratch, ToRegister(index), 1);
1828 __ Addu(scratch, string, scratch);
1829 }
1830 return FieldMemOperand(scratch, SeqString::kHeaderSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001831}
1832
1833
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001834void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
1835 String::Encoding encoding = instr->hydrogen()->encoding();
1836 Register string = ToRegister(instr->string());
1837 Register result = ToRegister(instr->result());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001838
1839 if (FLAG_debug_code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001840 Register scratch = scratch0();
1841 __ lw(scratch, FieldMemOperand(string, HeapObject::kMapOffset));
1842 __ lbu(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1843
1844 __ And(scratch, scratch,
1845 Operand(kStringRepresentationMask | kStringEncodingMask));
1846 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1847 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1848 __ Subu(at, scratch, Operand(encoding == String::ONE_BYTE_ENCODING
1849 ? one_byte_seq_type : two_byte_seq_type));
1850 __ Check(eq, kUnexpectedStringType, at, Operand(zero_reg));
1851 }
1852
1853 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1854 if (encoding == String::ONE_BYTE_ENCODING) {
1855 __ lbu(result, operand);
1856 } else {
1857 __ lhu(result, operand);
1858 }
1859}
1860
1861
1862void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
1863 String::Encoding encoding = instr->hydrogen()->encoding();
1864 Register string = ToRegister(instr->string());
1865 Register value = ToRegister(instr->value());
1866
1867 if (FLAG_debug_code) {
1868 Register scratch = scratch0();
1869 Register index = ToRegister(instr->index());
1870 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1871 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1872 int encoding_mask =
1873 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
1874 ? one_byte_seq_type : two_byte_seq_type;
1875 __ EmitSeqStringSetCharCheck(string, index, value, scratch, encoding_mask);
1876 }
1877
1878 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1879 if (encoding == String::ONE_BYTE_ENCODING) {
1880 __ sb(value, operand);
1881 } else {
1882 __ sh(value, operand);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001883 }
1884}
1885
1886
1887void LCodeGen::DoAddI(LAddI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001888 LOperand* left = instr->left();
1889 LOperand* right = instr->right();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001890 LOperand* result = instr->result();
1891 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1892
1893 if (!can_overflow) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001894 if (right->IsStackSlot()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001895 Register right_reg = EmitLoadRegister(right, at);
1896 __ Addu(ToRegister(result), ToRegister(left), Operand(right_reg));
1897 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001898 DCHECK(right->IsRegister() || right->IsConstantOperand());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001899 __ Addu(ToRegister(result), ToRegister(left), ToOperand(right));
1900 }
1901 } else { // can_overflow.
1902 Register overflow = scratch0();
1903 Register scratch = scratch1();
1904 if (right->IsStackSlot() ||
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001905 right->IsConstantOperand()) {
1906 Register right_reg = EmitLoadRegister(right, scratch);
1907 __ AdduAndCheckForOverflow(ToRegister(result),
1908 ToRegister(left),
1909 right_reg,
1910 overflow); // Reg at also used as scratch.
1911 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001912 DCHECK(right->IsRegister());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001913 // Due to overflow check macros not supporting constant operands,
1914 // handling the IsConstantOperand case was moved to prev if clause.
1915 __ AdduAndCheckForOverflow(ToRegister(result),
1916 ToRegister(left),
1917 ToRegister(right),
1918 overflow); // Reg at also used as scratch.
1919 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001920 DeoptimizeIf(lt, instr, "overflow", overflow, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001921 }
1922}
1923
1924
1925void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
1926 LOperand* left = instr->left();
1927 LOperand* right = instr->right();
1928 HMathMinMax::Operation operation = instr->hydrogen()->operation();
1929 Condition condition = (operation == HMathMinMax::kMathMin) ? le : ge;
1930 if (instr->hydrogen()->representation().IsSmiOrInteger32()) {
1931 Register left_reg = ToRegister(left);
1932 Register right_reg = EmitLoadRegister(right, scratch0());
1933 Register result_reg = ToRegister(instr->result());
1934 Label return_right, done;
1935 Register scratch = scratch1();
1936 __ Slt(scratch, left_reg, Operand(right_reg));
1937 if (condition == ge) {
1938 __ Movz(result_reg, left_reg, scratch);
1939 __ Movn(result_reg, right_reg, scratch);
1940 } else {
1941 DCHECK(condition == le);
1942 __ Movn(result_reg, left_reg, scratch);
1943 __ Movz(result_reg, right_reg, scratch);
1944 }
1945 } else {
1946 DCHECK(instr->hydrogen()->representation().IsDouble());
1947 FPURegister left_reg = ToDoubleRegister(left);
1948 FPURegister right_reg = ToDoubleRegister(right);
1949 FPURegister result_reg = ToDoubleRegister(instr->result());
1950 Label check_nan_left, check_zero, return_left, return_right, done;
1951 __ BranchF(&check_zero, &check_nan_left, eq, left_reg, right_reg);
1952 __ BranchF(&return_left, NULL, condition, left_reg, right_reg);
1953 __ Branch(&return_right);
1954
1955 __ bind(&check_zero);
1956 // left == right != 0.
1957 __ BranchF(&return_left, NULL, ne, left_reg, kDoubleRegZero);
1958 // At this point, both left and right are either 0 or -0.
1959 if (operation == HMathMinMax::kMathMin) {
1960 __ neg_d(left_reg, left_reg);
1961 __ sub_d(result_reg, left_reg, right_reg);
1962 __ neg_d(result_reg, result_reg);
1963 } else {
1964 __ add_d(result_reg, left_reg, right_reg);
1965 }
1966 __ Branch(&done);
1967
1968 __ bind(&check_nan_left);
1969 // left == NaN.
1970 __ BranchF(NULL, &return_left, eq, left_reg, left_reg);
1971 __ bind(&return_right);
1972 if (!right_reg.is(result_reg)) {
1973 __ mov_d(result_reg, right_reg);
1974 }
1975 __ Branch(&done);
1976
1977 __ bind(&return_left);
1978 if (!left_reg.is(result_reg)) {
1979 __ mov_d(result_reg, left_reg);
1980 }
1981 __ bind(&done);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001982 }
1983}
1984
1985
1986void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001987 DoubleRegister left = ToDoubleRegister(instr->left());
1988 DoubleRegister right = ToDoubleRegister(instr->right());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001989 DoubleRegister result = ToDoubleRegister(instr->result());
1990 switch (instr->op()) {
1991 case Token::ADD:
1992 __ add_d(result, left, right);
1993 break;
1994 case Token::SUB:
1995 __ sub_d(result, left, right);
1996 break;
1997 case Token::MUL:
1998 __ mul_d(result, left, right);
1999 break;
2000 case Token::DIV:
2001 __ div_d(result, left, right);
2002 break;
2003 case Token::MOD: {
2004 // Save a0-a3 on the stack.
2005 RegList saved_regs = a0.bit() | a1.bit() | a2.bit() | a3.bit();
2006 __ MultiPush(saved_regs);
2007
2008 __ PrepareCallCFunction(0, 2, scratch0());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002009 __ MovToFloatParameters(left, right);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002010 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002011 ExternalReference::mod_two_doubles_operation(isolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002012 0, 2);
2013 // Move the result in the double result register.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002014 __ MovFromFloatResult(result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002015
2016 // Restore saved register.
2017 __ MultiPop(saved_regs);
2018 break;
2019 }
2020 default:
2021 UNREACHABLE();
2022 break;
2023 }
2024}
2025
2026
2027void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002028 DCHECK(ToRegister(instr->context()).is(cp));
2029 DCHECK(ToRegister(instr->left()).is(a1));
2030 DCHECK(ToRegister(instr->right()).is(a0));
2031 DCHECK(ToRegister(instr->result()).is(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002032
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002033 Handle<Code> code =
2034 CodeFactory::BinaryOpIC(isolate(), instr->op(), NO_OVERWRITE).code();
2035 CallCode(code, RelocInfo::CODE_TARGET, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002036 // Other arch use a nop here, to signal that there is no inlined
2037 // patchable code. Mips does not need the nop, since our marker
2038 // instruction (andi zero_reg) will never be used in normal code.
2039}
2040
2041
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002042template<class InstrType>
2043void LCodeGen::EmitBranch(InstrType instr,
2044 Condition condition,
2045 Register src1,
2046 const Operand& src2) {
2047 int left_block = instr->TrueDestination(chunk_);
2048 int right_block = instr->FalseDestination(chunk_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002049
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002050 int next_block = GetNextEmittedBlock();
2051 if (right_block == left_block || condition == al) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002052 EmitGoto(left_block);
2053 } else if (left_block == next_block) {
2054 __ Branch(chunk_->GetAssemblyLabel(right_block),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002055 NegateCondition(condition), src1, src2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002056 } else if (right_block == next_block) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002057 __ Branch(chunk_->GetAssemblyLabel(left_block), condition, src1, src2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002058 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002059 __ Branch(chunk_->GetAssemblyLabel(left_block), condition, src1, src2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002060 __ Branch(chunk_->GetAssemblyLabel(right_block));
2061 }
2062}
2063
2064
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002065template<class InstrType>
2066void LCodeGen::EmitBranchF(InstrType instr,
2067 Condition condition,
2068 FPURegister src1,
2069 FPURegister src2) {
2070 int right_block = instr->FalseDestination(chunk_);
2071 int left_block = instr->TrueDestination(chunk_);
2072
2073 int next_block = GetNextEmittedBlock();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002074 if (right_block == left_block) {
2075 EmitGoto(left_block);
2076 } else if (left_block == next_block) {
2077 __ BranchF(chunk_->GetAssemblyLabel(right_block), NULL,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002078 NegateCondition(condition), src1, src2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002079 } else if (right_block == next_block) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002080 __ BranchF(chunk_->GetAssemblyLabel(left_block), NULL,
2081 condition, src1, src2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002082 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002083 __ BranchF(chunk_->GetAssemblyLabel(left_block), NULL,
2084 condition, src1, src2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002085 __ Branch(chunk_->GetAssemblyLabel(right_block));
2086 }
2087}
2088
2089
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002090template<class InstrType>
2091void LCodeGen::EmitFalseBranch(InstrType instr,
2092 Condition condition,
2093 Register src1,
2094 const Operand& src2) {
2095 int false_block = instr->FalseDestination(chunk_);
2096 __ Branch(chunk_->GetAssemblyLabel(false_block), condition, src1, src2);
2097}
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002098
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002099
2100template<class InstrType>
2101void LCodeGen::EmitFalseBranchF(InstrType instr,
2102 Condition condition,
2103 FPURegister src1,
2104 FPURegister src2) {
2105 int false_block = instr->FalseDestination(chunk_);
2106 __ BranchF(chunk_->GetAssemblyLabel(false_block), NULL,
2107 condition, src1, src2);
2108}
2109
2110
2111void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
2112 __ stop("LDebugBreak");
2113}
2114
2115
2116void LCodeGen::DoBranch(LBranch* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002117 Representation r = instr->hydrogen()->value()->representation();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002118 if (r.IsInteger32() || r.IsSmi()) {
2119 DCHECK(!info()->IsStub());
2120 Register reg = ToRegister(instr->value());
2121 EmitBranch(instr, ne, reg, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002122 } else if (r.IsDouble()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002123 DCHECK(!info()->IsStub());
2124 DoubleRegister reg = ToDoubleRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002125 // Test the double value. Zero and NaN are false.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002126 EmitBranchF(instr, nue, reg, kDoubleRegZero);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002127 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002128 DCHECK(r.IsTagged());
2129 Register reg = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002130 HType type = instr->hydrogen()->value()->type();
2131 if (type.IsBoolean()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002132 DCHECK(!info()->IsStub());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002133 __ LoadRoot(at, Heap::kTrueValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002134 EmitBranch(instr, eq, reg, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002135 } else if (type.IsSmi()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002136 DCHECK(!info()->IsStub());
2137 EmitBranch(instr, ne, reg, Operand(zero_reg));
2138 } else if (type.IsJSArray()) {
2139 DCHECK(!info()->IsStub());
2140 EmitBranch(instr, al, zero_reg, Operand(zero_reg));
2141 } else if (type.IsHeapNumber()) {
2142 DCHECK(!info()->IsStub());
2143 DoubleRegister dbl_scratch = double_scratch0();
2144 __ ldc1(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
2145 // Test the double value. Zero and NaN are false.
2146 EmitBranchF(instr, nue, dbl_scratch, kDoubleRegZero);
2147 } else if (type.IsString()) {
2148 DCHECK(!info()->IsStub());
2149 __ lw(at, FieldMemOperand(reg, String::kLengthOffset));
2150 EmitBranch(instr, ne, at, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002151 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002152 ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
2153 // Avoid deopts in the case where we've never executed this path before.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002154 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002155
2156 if (expected.Contains(ToBooleanStub::UNDEFINED)) {
2157 // undefined -> false.
2158 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002159 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002160 }
2161 if (expected.Contains(ToBooleanStub::BOOLEAN)) {
2162 // Boolean -> its value.
2163 __ LoadRoot(at, Heap::kTrueValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002164 __ Branch(instr->TrueLabel(chunk_), eq, reg, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002165 __ LoadRoot(at, Heap::kFalseValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002166 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002167 }
2168 if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
2169 // 'null' -> false.
2170 __ LoadRoot(at, Heap::kNullValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002171 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002172 }
2173
2174 if (expected.Contains(ToBooleanStub::SMI)) {
2175 // Smis: 0 -> false, all other -> true.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002176 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(zero_reg));
2177 __ JumpIfSmi(reg, instr->TrueLabel(chunk_));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002178 } else if (expected.NeedsMap()) {
2179 // If we need a map later and have a Smi -> deopt.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002180 __ SmiTst(reg, at);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002181 DeoptimizeIf(eq, instr, "Smi", at, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002182 }
2183
2184 const Register map = scratch0();
2185 if (expected.NeedsMap()) {
2186 __ lw(map, FieldMemOperand(reg, HeapObject::kMapOffset));
2187 if (expected.CanBeUndetectable()) {
2188 // Undetectable -> false.
2189 __ lbu(at, FieldMemOperand(map, Map::kBitFieldOffset));
2190 __ And(at, at, Operand(1 << Map::kIsUndetectable));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002191 __ Branch(instr->FalseLabel(chunk_), ne, at, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002192 }
2193 }
2194
2195 if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
2196 // spec object -> true.
2197 __ lbu(at, FieldMemOperand(map, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002198 __ Branch(instr->TrueLabel(chunk_),
2199 ge, at, Operand(FIRST_SPEC_OBJECT_TYPE));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002200 }
2201
2202 if (expected.Contains(ToBooleanStub::STRING)) {
2203 // String value -> false iff empty.
2204 Label not_string;
2205 __ lbu(at, FieldMemOperand(map, Map::kInstanceTypeOffset));
2206 __ Branch(&not_string, ge , at, Operand(FIRST_NONSTRING_TYPE));
2207 __ lw(at, FieldMemOperand(reg, String::kLengthOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002208 __ Branch(instr->TrueLabel(chunk_), ne, at, Operand(zero_reg));
2209 __ Branch(instr->FalseLabel(chunk_));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002210 __ bind(&not_string);
2211 }
2212
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002213 if (expected.Contains(ToBooleanStub::SYMBOL)) {
2214 // Symbol value -> true.
2215 const Register scratch = scratch1();
2216 __ lbu(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
2217 __ Branch(instr->TrueLabel(chunk_), eq, scratch, Operand(SYMBOL_TYPE));
2218 }
2219
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002220 if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
2221 // heap number -> false iff +0, -0, or NaN.
2222 DoubleRegister dbl_scratch = double_scratch0();
2223 Label not_heap_number;
2224 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
2225 __ Branch(&not_heap_number, ne, map, Operand(at));
2226 __ ldc1(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002227 __ BranchF(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2228 ne, dbl_scratch, kDoubleRegZero);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002229 // Falls through if dbl_scratch == 0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002230 __ Branch(instr->FalseLabel(chunk_));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002231 __ bind(&not_heap_number);
2232 }
2233
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002234 if (!expected.IsGeneric()) {
2235 // We've seen something for the first time -> deopt.
2236 // This can only happen if we are not generic already.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002237 DeoptimizeIf(al, instr, "unexpected object", zero_reg,
2238 Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002239 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002240 }
2241 }
2242}
2243
2244
2245void LCodeGen::EmitGoto(int block) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002246 if (!IsNextEmittedBlock(block)) {
2247 __ jmp(chunk_->GetAssemblyLabel(LookupDestination(block)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002248 }
2249}
2250
2251
2252void LCodeGen::DoGoto(LGoto* instr) {
2253 EmitGoto(instr->block_id());
2254}
2255
2256
2257Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
2258 Condition cond = kNoCondition;
2259 switch (op) {
2260 case Token::EQ:
2261 case Token::EQ_STRICT:
2262 cond = eq;
2263 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002264 case Token::NE:
2265 case Token::NE_STRICT:
2266 cond = ne;
2267 break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002268 case Token::LT:
2269 cond = is_unsigned ? lo : lt;
2270 break;
2271 case Token::GT:
2272 cond = is_unsigned ? hi : gt;
2273 break;
2274 case Token::LTE:
2275 cond = is_unsigned ? ls : le;
2276 break;
2277 case Token::GTE:
2278 cond = is_unsigned ? hs : ge;
2279 break;
2280 case Token::IN:
2281 case Token::INSTANCEOF:
2282 default:
2283 UNREACHABLE();
2284 }
2285 return cond;
2286}
2287
2288
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002289void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2290 LOperand* left = instr->left();
2291 LOperand* right = instr->right();
2292 bool is_unsigned =
2293 instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2294 instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2295 Condition cond = TokenToCondition(instr->op(), is_unsigned);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002296
2297 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2298 // We can statically evaluate the comparison.
2299 double left_val = ToDouble(LConstantOperand::cast(left));
2300 double right_val = ToDouble(LConstantOperand::cast(right));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002301 int next_block = EvalComparison(instr->op(), left_val, right_val) ?
2302 instr->TrueDestination(chunk_) : instr->FalseDestination(chunk_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002303 EmitGoto(next_block);
2304 } else {
2305 if (instr->is_double()) {
2306 // Compare left and right as doubles and load the
2307 // resulting flags into the normal status register.
2308 FPURegister left_reg = ToDoubleRegister(left);
2309 FPURegister right_reg = ToDoubleRegister(right);
2310
2311 // If a NaN is involved, i.e. the result is unordered,
2312 // jump to false block label.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002313 __ BranchF(NULL, instr->FalseLabel(chunk_), eq,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002314 left_reg, right_reg);
2315
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002316 EmitBranchF(instr, cond, left_reg, right_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002317 } else {
2318 Register cmp_left;
2319 Operand cmp_right = Operand(0);
2320
2321 if (right->IsConstantOperand()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002322 int32_t value = ToInteger32(LConstantOperand::cast(right));
2323 if (instr->hydrogen_value()->representation().IsSmi()) {
2324 cmp_left = ToRegister(left);
2325 cmp_right = Operand(Smi::FromInt(value));
2326 } else {
2327 cmp_left = ToRegister(left);
2328 cmp_right = Operand(value);
2329 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002330 } else if (left->IsConstantOperand()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002331 int32_t value = ToInteger32(LConstantOperand::cast(left));
2332 if (instr->hydrogen_value()->representation().IsSmi()) {
2333 cmp_left = ToRegister(right);
2334 cmp_right = Operand(Smi::FromInt(value));
2335 } else {
2336 cmp_left = ToRegister(right);
2337 cmp_right = Operand(value);
2338 }
2339 // We commuted the operands, so commute the condition.
2340 cond = CommuteCondition(cond);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002341 } else {
2342 cmp_left = ToRegister(left);
2343 cmp_right = Operand(ToRegister(right));
2344 }
2345
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002346 EmitBranch(instr, cond, cmp_left, cmp_right);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002347 }
2348 }
2349}
2350
2351
2352void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002353 Register left = ToRegister(instr->left());
2354 Register right = ToRegister(instr->right());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002355
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002356 EmitBranch(instr, eq, left, Operand(right));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002357}
2358
2359
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002360void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) {
2361 if (instr->hydrogen()->representation().IsTagged()) {
2362 Register input_reg = ToRegister(instr->object());
2363 __ li(at, Operand(factory()->the_hole_value()));
2364 EmitBranch(instr, eq, input_reg, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002365 return;
2366 }
2367
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002368 DoubleRegister input_reg = ToDoubleRegister(instr->object());
2369 EmitFalseBranchF(instr, eq, input_reg, input_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002370
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002371 Register scratch = scratch0();
2372 __ FmoveHigh(scratch, input_reg);
2373 EmitBranch(instr, eq, scratch, Operand(kHoleNanUpper32));
2374}
2375
2376
2377void LCodeGen::DoCompareMinusZeroAndBranch(LCompareMinusZeroAndBranch* instr) {
2378 Representation rep = instr->hydrogen()->value()->representation();
2379 DCHECK(!rep.IsInteger32());
2380 Register scratch = ToRegister(instr->temp());
2381
2382 if (rep.IsDouble()) {
2383 DoubleRegister value = ToDoubleRegister(instr->value());
2384 EmitFalseBranchF(instr, ne, value, kDoubleRegZero);
2385 __ FmoveHigh(scratch, value);
2386 __ li(at, 0x80000000);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002387 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002388 Register value = ToRegister(instr->value());
2389 __ CheckMap(value,
2390 scratch,
2391 Heap::kHeapNumberMapRootIndex,
2392 instr->FalseLabel(chunk()),
2393 DO_SMI_CHECK);
2394 __ lw(scratch, FieldMemOperand(value, HeapNumber::kExponentOffset));
2395 EmitFalseBranch(instr, ne, scratch, Operand(0x80000000));
2396 __ lw(scratch, FieldMemOperand(value, HeapNumber::kMantissaOffset));
2397 __ mov(at, zero_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002398 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002399 EmitBranch(instr, eq, scratch, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002400}
2401
2402
2403Condition LCodeGen::EmitIsObject(Register input,
2404 Register temp1,
2405 Register temp2,
2406 Label* is_not_object,
2407 Label* is_object) {
2408 __ JumpIfSmi(input, is_not_object);
2409
2410 __ LoadRoot(temp2, Heap::kNullValueRootIndex);
2411 __ Branch(is_object, eq, input, Operand(temp2));
2412
2413 // Load map.
2414 __ lw(temp1, FieldMemOperand(input, HeapObject::kMapOffset));
2415 // Undetectable objects behave like undefined.
2416 __ lbu(temp2, FieldMemOperand(temp1, Map::kBitFieldOffset));
2417 __ And(temp2, temp2, Operand(1 << Map::kIsUndetectable));
2418 __ Branch(is_not_object, ne, temp2, Operand(zero_reg));
2419
2420 // Load instance type and check that it is in object type range.
2421 __ lbu(temp2, FieldMemOperand(temp1, Map::kInstanceTypeOffset));
2422 __ Branch(is_not_object,
2423 lt, temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2424
2425 return le;
2426}
2427
2428
2429void LCodeGen::DoIsObjectAndBranch(LIsObjectAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002430 Register reg = ToRegister(instr->value());
2431 Register temp1 = ToRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002432 Register temp2 = scratch0();
2433
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002434 Condition true_cond =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002435 EmitIsObject(reg, temp1, temp2,
2436 instr->FalseLabel(chunk_), instr->TrueLabel(chunk_));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002437
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002438 EmitBranch(instr, true_cond, temp2,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002439 Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
2440}
2441
2442
2443Condition LCodeGen::EmitIsString(Register input,
2444 Register temp1,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002445 Label* is_not_string,
2446 SmiCheck check_needed = INLINE_SMI_CHECK) {
2447 if (check_needed == INLINE_SMI_CHECK) {
2448 __ JumpIfSmi(input, is_not_string);
2449 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002450 __ GetObjectType(input, temp1, temp1);
2451
2452 return lt;
2453}
2454
2455
2456void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002457 Register reg = ToRegister(instr->value());
2458 Register temp1 = ToRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002459
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002460 SmiCheck check_needed =
2461 instr->hydrogen()->value()->type().IsHeapObject()
2462 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002463 Condition true_cond =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002464 EmitIsString(reg, temp1, instr->FalseLabel(chunk_), check_needed);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002465
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002466 EmitBranch(instr, true_cond, temp1,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002467 Operand(FIRST_NONSTRING_TYPE));
2468}
2469
2470
2471void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002472 Register input_reg = EmitLoadRegister(instr->value(), at);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002473 __ And(at, input_reg, kSmiTagMask);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002474 EmitBranch(instr, eq, at, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002475}
2476
2477
2478void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002479 Register input = ToRegister(instr->value());
2480 Register temp = ToRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002481
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002482 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2483 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2484 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002485 __ lw(temp, FieldMemOperand(input, HeapObject::kMapOffset));
2486 __ lbu(temp, FieldMemOperand(temp, Map::kBitFieldOffset));
2487 __ And(at, temp, Operand(1 << Map::kIsUndetectable));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002488 EmitBranch(instr, ne, at, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002489}
2490
2491
2492static Condition ComputeCompareCondition(Token::Value op) {
2493 switch (op) {
2494 case Token::EQ_STRICT:
2495 case Token::EQ:
2496 return eq;
2497 case Token::LT:
2498 return lt;
2499 case Token::GT:
2500 return gt;
2501 case Token::LTE:
2502 return le;
2503 case Token::GTE:
2504 return ge;
2505 default:
2506 UNREACHABLE();
2507 return kNoCondition;
2508 }
2509}
2510
2511
2512void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002513 DCHECK(ToRegister(instr->context()).is(cp));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002514 Token::Value op = instr->op();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002515
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002516 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002517 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2518
2519 Condition condition = ComputeCompareCondition(op);
2520
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002521 EmitBranch(instr, condition, v0, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002522}
2523
2524
2525static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
2526 InstanceType from = instr->from();
2527 InstanceType to = instr->to();
2528 if (from == FIRST_TYPE) return to;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002529 DCHECK(from == to || to == LAST_TYPE);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002530 return from;
2531}
2532
2533
2534static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
2535 InstanceType from = instr->from();
2536 InstanceType to = instr->to();
2537 if (from == to) return eq;
2538 if (to == LAST_TYPE) return hs;
2539 if (from == FIRST_TYPE) return ls;
2540 UNREACHABLE();
2541 return eq;
2542}
2543
2544
2545void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
2546 Register scratch = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002547 Register input = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002548
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002549 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2550 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2551 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002552
2553 __ GetObjectType(input, scratch, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002554 EmitBranch(instr,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002555 BranchCondition(instr->hydrogen()),
2556 scratch,
2557 Operand(TestType(instr->hydrogen())));
2558}
2559
2560
2561void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002562 Register input = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002563 Register result = ToRegister(instr->result());
2564
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002565 __ AssertString(input);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002566
2567 __ lw(result, FieldMemOperand(input, String::kHashFieldOffset));
2568 __ IndexFromHash(result, result);
2569}
2570
2571
2572void LCodeGen::DoHasCachedArrayIndexAndBranch(
2573 LHasCachedArrayIndexAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002574 Register input = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002575 Register scratch = scratch0();
2576
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002577 __ lw(scratch,
2578 FieldMemOperand(input, String::kHashFieldOffset));
2579 __ And(at, scratch, Operand(String::kContainsCachedArrayIndexMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002580 EmitBranch(instr, eq, at, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002581}
2582
2583
2584// Branches to a label or falls through with the answer in flags. Trashes
2585// the temp registers, but not the input.
2586void LCodeGen::EmitClassOfTest(Label* is_true,
2587 Label* is_false,
2588 Handle<String>class_name,
2589 Register input,
2590 Register temp,
2591 Register temp2) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002592 DCHECK(!input.is(temp));
2593 DCHECK(!input.is(temp2));
2594 DCHECK(!temp.is(temp2));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002595
2596 __ JumpIfSmi(input, is_false);
2597
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002598 if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002599 // Assuming the following assertions, we can use the same compares to test
2600 // for both being a function type and being in the object type range.
2601 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
2602 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2603 FIRST_SPEC_OBJECT_TYPE + 1);
2604 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
2605 LAST_SPEC_OBJECT_TYPE - 1);
2606 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
2607
2608 __ GetObjectType(input, temp, temp2);
2609 __ Branch(is_false, lt, temp2, Operand(FIRST_SPEC_OBJECT_TYPE));
2610 __ Branch(is_true, eq, temp2, Operand(FIRST_SPEC_OBJECT_TYPE));
2611 __ Branch(is_true, eq, temp2, Operand(LAST_SPEC_OBJECT_TYPE));
2612 } else {
2613 // Faster code path to avoid two compares: subtract lower bound from the
2614 // actual type and do a signed compare with the width of the type range.
2615 __ GetObjectType(input, temp, temp2);
2616 __ Subu(temp2, temp2, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2617 __ Branch(is_false, gt, temp2, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE -
2618 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
2619 }
2620
2621 // Now we are in the FIRST-LAST_NONCALLABLE_SPEC_OBJECT_TYPE range.
2622 // Check if the constructor in the map is a function.
2623 __ lw(temp, FieldMemOperand(temp, Map::kConstructorOffset));
2624
2625 // Objects with a non-function constructor have class 'Object'.
2626 __ GetObjectType(temp, temp2, temp2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002627 if (String::Equals(class_name, isolate()->factory()->Object_string())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002628 __ Branch(is_true, ne, temp2, Operand(JS_FUNCTION_TYPE));
2629 } else {
2630 __ Branch(is_false, ne, temp2, Operand(JS_FUNCTION_TYPE));
2631 }
2632
2633 // temp now contains the constructor function. Grab the
2634 // instance class name from there.
2635 __ lw(temp, FieldMemOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2636 __ lw(temp, FieldMemOperand(temp,
2637 SharedFunctionInfo::kInstanceClassNameOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002638 // The class name we are testing against is internalized since it's a literal.
2639 // The name in the constructor is internalized because of the way the context
2640 // is booted. This routine isn't expected to work for random API-created
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002641 // classes and it doesn't have to because you can't access it with natives
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002642 // syntax. Since both sides are internalized it is sufficient to use an
2643 // identity comparison.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002644
2645 // End with the address of this class_name instance in temp register.
2646 // On MIPS, the caller must do the comparison with Handle<String>class_name.
2647}
2648
2649
2650void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002651 Register input = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002652 Register temp = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002653 Register temp2 = ToRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002654 Handle<String> class_name = instr->hydrogen()->class_name();
2655
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002656 EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2657 class_name, input, temp, temp2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002658
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002659 EmitBranch(instr, eq, temp, Operand(class_name));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002660}
2661
2662
2663void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002664 Register reg = ToRegister(instr->value());
2665 Register temp = ToRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002666
2667 __ lw(temp, FieldMemOperand(reg, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002668 EmitBranch(instr, eq, temp, Operand(instr->map()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002669}
2670
2671
2672void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002673 DCHECK(ToRegister(instr->context()).is(cp));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002674 Label true_label, done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002675 DCHECK(ToRegister(instr->left()).is(a0)); // Object is in a0.
2676 DCHECK(ToRegister(instr->right()).is(a1)); // Function is in a1.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002677 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002678 DCHECK(result.is(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002679
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002680 InstanceofStub stub(isolate(), InstanceofStub::kArgsInRegisters);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002681 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2682
2683 __ Branch(&true_label, eq, result, Operand(zero_reg));
2684 __ li(result, Operand(factory()->false_value()));
2685 __ Branch(&done);
2686 __ bind(&true_label);
2687 __ li(result, Operand(factory()->true_value()));
2688 __ bind(&done);
2689}
2690
2691
2692void LCodeGen::DoInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002693 class DeferredInstanceOfKnownGlobal FINAL : public LDeferredCode {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002694 public:
2695 DeferredInstanceOfKnownGlobal(LCodeGen* codegen,
2696 LInstanceOfKnownGlobal* instr)
2697 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002698 void Generate() OVERRIDE {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002699 codegen()->DoDeferredInstanceOfKnownGlobal(instr_, &map_check_);
2700 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002701 LInstruction* instr() OVERRIDE { return instr_; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002702 Label* map_check() { return &map_check_; }
2703
2704 private:
2705 LInstanceOfKnownGlobal* instr_;
2706 Label map_check_;
2707 };
2708
2709 DeferredInstanceOfKnownGlobal* deferred;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002710 deferred = new(zone()) DeferredInstanceOfKnownGlobal(this, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002711
2712 Label done, false_result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002713 Register object = ToRegister(instr->value());
2714 Register temp = ToRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002715 Register result = ToRegister(instr->result());
2716
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002717 DCHECK(object.is(a0));
2718 DCHECK(result.is(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002719
2720 // A Smi is not instance of anything.
2721 __ JumpIfSmi(object, &false_result);
2722
2723 // This is the inlined call site instanceof cache. The two occurences of the
2724 // hole value will be patched to the last map/result pair generated by the
2725 // instanceof stub.
2726 Label cache_miss;
2727 Register map = temp;
2728 __ lw(map, FieldMemOperand(object, HeapObject::kMapOffset));
2729
2730 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
2731 __ bind(deferred->map_check()); // Label for calculating code patching.
2732 // We use Factory::the_hole_value() on purpose instead of loading from the
2733 // root array to force relocation to be able to later patch with
2734 // the cached map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002735 Handle<Cell> cell = factory()->NewCell(factory()->the_hole_value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002736 __ li(at, Operand(Handle<Object>(cell)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002737 __ lw(at, FieldMemOperand(at, PropertyCell::kValueOffset));
2738 __ BranchShort(&cache_miss, ne, map, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002739 // We use Factory::the_hole_value() on purpose instead of loading from the
2740 // root array to force relocation to be able to later patch
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002741 // with true or false. The distance from map check has to be constant.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002742 __ li(result, Operand(factory()->the_hole_value()), CONSTANT_SIZE);
2743 __ Branch(&done);
2744
2745 // The inlined call site cache did not match. Check null and string before
2746 // calling the deferred code.
2747 __ bind(&cache_miss);
2748 // Null is not instance of anything.
2749 __ LoadRoot(temp, Heap::kNullValueRootIndex);
2750 __ Branch(&false_result, eq, object, Operand(temp));
2751
2752 // String values is not instance of anything.
2753 Condition cc = __ IsObjectStringType(object, temp, temp);
2754 __ Branch(&false_result, cc, temp, Operand(zero_reg));
2755
2756 // Go to the deferred code.
2757 __ Branch(deferred->entry());
2758
2759 __ bind(&false_result);
2760 __ LoadRoot(result, Heap::kFalseValueRootIndex);
2761
2762 // Here result has either true or false. Deferred code also produces true or
2763 // false object.
2764 __ bind(deferred->exit());
2765 __ bind(&done);
2766}
2767
2768
2769void LCodeGen::DoDeferredInstanceOfKnownGlobal(LInstanceOfKnownGlobal* instr,
2770 Label* map_check) {
2771 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002772 DCHECK(result.is(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002773
2774 InstanceofStub::Flags flags = InstanceofStub::kNoFlags;
2775 flags = static_cast<InstanceofStub::Flags>(
2776 flags | InstanceofStub::kArgsInRegisters);
2777 flags = static_cast<InstanceofStub::Flags>(
2778 flags | InstanceofStub::kCallSiteInlineCheck);
2779 flags = static_cast<InstanceofStub::Flags>(
2780 flags | InstanceofStub::kReturnTrueFalseObject);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002781 InstanceofStub stub(isolate(), flags);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002782
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002783 PushSafepointRegistersScope scope(this);
2784 LoadContextFromDeferred(instr->context());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002785
2786 // Get the temp register reserved by the instruction. This needs to be t0 as
2787 // its slot of the pushing of safepoint registers is used to communicate the
2788 // offset to the location of the map check.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002789 Register temp = ToRegister(instr->temp());
2790 DCHECK(temp.is(t0));
2791 __ li(InstanceofStub::right(), instr->function());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002792 static const int kAdditionalDelta = 7;
2793 int delta = masm_->InstructionsGeneratedSince(map_check) + kAdditionalDelta;
2794 Label before_push_delta;
2795 __ bind(&before_push_delta);
2796 {
2797 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
2798 __ li(temp, Operand(delta * kPointerSize), CONSTANT_SIZE);
2799 __ StoreToSafepointRegisterSlot(temp, temp);
2800 }
2801 CallCodeGeneric(stub.GetCode(),
2802 RelocInfo::CODE_TARGET,
2803 instr,
2804 RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002805 LEnvironment* env = instr->GetDeferredLazyDeoptimizationEnvironment();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002806 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
2807 // Put the result value into the result register slot and
2808 // restore all registers.
2809 __ StoreToSafepointRegisterSlot(result, result);
2810}
2811
2812
2813void LCodeGen::DoCmpT(LCmpT* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002814 DCHECK(ToRegister(instr->context()).is(cp));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002815 Token::Value op = instr->op();
2816
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002817 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002818 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2819 // On MIPS there is no need for a "no inlined smi code" marker (nop).
2820
2821 Condition condition = ComputeCompareCondition(op);
2822 // A minor optimization that relies on LoadRoot always emitting one
2823 // instruction.
2824 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002825 Label done, check;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002826 __ Branch(USE_DELAY_SLOT, &done, condition, v0, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002827 __ bind(&check);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002828 __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002829 DCHECK_EQ(1, masm()->InstructionsGeneratedSince(&check));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002830 __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002831 __ bind(&done);
2832}
2833
2834
2835void LCodeGen::DoReturn(LReturn* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002836 if (FLAG_trace && info()->IsOptimizing()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002837 // Push the return value on the stack as the parameter.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002838 // Runtime::TraceExit returns its parameter in v0. We're leaving the code
2839 // managed by the register allocator and tearing down the frame, it's
2840 // safe to write to the context register.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002841 __ push(v0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002842 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002843 __ CallRuntime(Runtime::kTraceExit, 1);
2844 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002845 if (info()->saves_caller_doubles()) {
2846 RestoreCallerDoubles();
2847 }
2848 int no_frame_start = -1;
2849 if (NeedsEagerFrame()) {
2850 __ mov(sp, fp);
2851 no_frame_start = masm_->pc_offset();
2852 __ Pop(ra, fp);
2853 }
2854 if (instr->has_constant_parameter_count()) {
2855 int parameter_count = ToInteger32(instr->constant_parameter_count());
2856 int32_t sp_delta = (parameter_count + 1) * kPointerSize;
2857 if (sp_delta != 0) {
2858 __ Addu(sp, sp, Operand(sp_delta));
2859 }
2860 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002861 DCHECK(info()->IsStub()); // Functions would need to drop one more value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002862 Register reg = ToRegister(instr->parameter_count());
2863 // The argument count parameter is a smi
2864 __ SmiUntag(reg);
2865 __ sll(at, reg, kPointerSizeLog2);
2866 __ Addu(sp, sp, at);
2867 }
2868
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002869 __ Jump(ra);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002870
2871 if (no_frame_start != -1) {
2872 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
2873 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002874}
2875
2876
2877void LCodeGen::DoLoadGlobalCell(LLoadGlobalCell* instr) {
2878 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002879 __ li(at, Operand(Handle<Object>(instr->hydrogen()->cell().handle())));
2880 __ lw(result, FieldMemOperand(at, Cell::kValueOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002881 if (instr->hydrogen()->RequiresHoleCheck()) {
2882 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002883 DeoptimizeIf(eq, instr, "hole", result, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002884 }
2885}
2886
2887
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002888template <class T>
2889void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
2890 DCHECK(FLAG_vector_ics);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002891 Register vector_register = ToRegister(instr->temp_vector());
2892 Register slot_register = VectorLoadICDescriptor::SlotRegister();
2893 DCHECK(vector_register.is(VectorLoadICDescriptor::VectorRegister()));
2894 DCHECK(slot_register.is(a0));
2895
2896 AllowDeferredHandleDereference vector_structure_check;
2897 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2898 __ li(vector_register, vector);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002899 // No need to allocate this register.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002900 FeedbackVectorICSlot slot = instr->hydrogen()->slot();
2901 int index = vector->GetIndex(slot);
2902 __ li(slot_register, Operand(Smi::FromInt(index)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002903}
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002904
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002905
2906void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
2907 DCHECK(ToRegister(instr->context()).is(cp));
2908 DCHECK(ToRegister(instr->global_object())
2909 .is(LoadDescriptor::ReceiverRegister()));
2910 DCHECK(ToRegister(instr->result()).is(v0));
2911
2912 __ li(LoadDescriptor::NameRegister(), Operand(instr->name()));
2913 if (FLAG_vector_ics) {
2914 EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
2915 }
2916 ContextualMode mode = instr->for_typeof() ? NOT_CONTEXTUAL : CONTEXTUAL;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002917 Handle<Code> ic = CodeFactory::LoadICInOptimizedCode(isolate(), mode).code();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002918 CallCode(ic, RelocInfo::CODE_TARGET, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002919}
2920
2921
2922void LCodeGen::DoStoreGlobalCell(LStoreGlobalCell* instr) {
2923 Register value = ToRegister(instr->value());
2924 Register cell = scratch0();
2925
2926 // Load the cell.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002927 __ li(cell, Operand(instr->hydrogen()->cell().handle()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002928
2929 // If the cell we are storing to contains the hole it could have
2930 // been deleted from the property dictionary. In that case, we need
2931 // to update the property details in the property dictionary to mark
2932 // it as no longer deleted.
2933 if (instr->hydrogen()->RequiresHoleCheck()) {
2934 // We use a temp to check the payload.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002935 Register payload = ToRegister(instr->temp());
2936 __ lw(payload, FieldMemOperand(cell, Cell::kValueOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002937 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002938 DeoptimizeIf(eq, instr, "hole", payload, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002939 }
2940
2941 // Store the value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002942 __ sw(value, FieldMemOperand(cell, Cell::kValueOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002943 // Cells are always rescanned, so no write barrier here.
2944}
2945
2946
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002947
2948void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
2949 Register context = ToRegister(instr->context());
2950 Register result = ToRegister(instr->result());
2951
2952 __ lw(result, ContextOperand(context, instr->slot_index()));
2953 if (instr->hydrogen()->RequiresHoleCheck()) {
2954 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2955
2956 if (instr->hydrogen()->DeoptimizesOnHole()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002957 DeoptimizeIf(eq, instr, "hole", result, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002958 } else {
2959 Label is_not_hole;
2960 __ Branch(&is_not_hole, ne, result, Operand(at));
2961 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2962 __ bind(&is_not_hole);
2963 }
2964 }
2965}
2966
2967
2968void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
2969 Register context = ToRegister(instr->context());
2970 Register value = ToRegister(instr->value());
2971 Register scratch = scratch0();
2972 MemOperand target = ContextOperand(context, instr->slot_index());
2973
2974 Label skip_assignment;
2975
2976 if (instr->hydrogen()->RequiresHoleCheck()) {
2977 __ lw(scratch, target);
2978 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2979
2980 if (instr->hydrogen()->DeoptimizesOnHole()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002981 DeoptimizeIf(eq, instr, "hole", scratch, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002982 } else {
2983 __ Branch(&skip_assignment, ne, scratch, Operand(at));
2984 }
2985 }
2986
2987 __ sw(value, target);
2988 if (instr->hydrogen()->NeedsWriteBarrier()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002989 SmiCheck check_needed =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002990 instr->hydrogen()->value()->type().IsHeapObject()
2991 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002992 __ RecordWriteContextSlot(context,
2993 target.offset(),
2994 value,
2995 scratch0(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002996 GetRAState(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002997 kSaveFPRegs,
2998 EMIT_REMEMBERED_SET,
2999 check_needed);
3000 }
3001
3002 __ bind(&skip_assignment);
3003}
3004
3005
3006void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003007 HObjectAccess access = instr->hydrogen()->access();
3008 int offset = access.offset();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003009 Register object = ToRegister(instr->object());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003010
3011 if (access.IsExternalMemory()) {
3012 Register result = ToRegister(instr->result());
3013 MemOperand operand = MemOperand(object, offset);
3014 __ Load(result, operand, access.representation());
3015 return;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003016 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003017
3018 if (instr->hydrogen()->representation().IsDouble()) {
3019 DoubleRegister result = ToDoubleRegister(instr->result());
3020 __ ldc1(result, FieldMemOperand(object, offset));
3021 return;
3022 }
3023
3024 Register result = ToRegister(instr->result());
3025 if (!access.IsInobject()) {
3026 __ lw(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
3027 object = result;
3028 }
3029 MemOperand operand = FieldMemOperand(object, offset);
3030 __ Load(result, operand, access.representation());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003031}
3032
3033
3034void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003035 DCHECK(ToRegister(instr->context()).is(cp));
3036 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3037 DCHECK(ToRegister(instr->result()).is(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003038
3039 // Name is always in a2.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003040 __ li(LoadDescriptor::NameRegister(), Operand(instr->name()));
3041 if (FLAG_vector_ics) {
3042 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
3043 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003044 Handle<Code> ic =
3045 CodeFactory::LoadICInOptimizedCode(isolate(), NOT_CONTEXTUAL).code();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003046 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3047}
3048
3049
3050void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
3051 Register scratch = scratch0();
3052 Register function = ToRegister(instr->function());
3053 Register result = ToRegister(instr->result());
3054
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003055 // Get the prototype or initial map from the function.
3056 __ lw(result,
3057 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
3058
3059 // Check that the function has a prototype or an initial map.
3060 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003061 DeoptimizeIf(eq, instr, "hole", result, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003062
3063 // If the function does not have an initial map, we're done.
3064 Label done;
3065 __ GetObjectType(result, scratch, scratch);
3066 __ Branch(&done, ne, scratch, Operand(MAP_TYPE));
3067
3068 // Get the prototype from the initial map.
3069 __ lw(result, FieldMemOperand(result, Map::kPrototypeOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003070
3071 // All done.
3072 __ bind(&done);
3073}
3074
3075
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003076void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003077 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003078 __ LoadRoot(result, instr->index());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003079}
3080
3081
3082void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
3083 Register arguments = ToRegister(instr->arguments());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003084 Register result = ToRegister(instr->result());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003085 // There are two words between the frame pointer and the last argument.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003086 // Subtracting from length accounts for one of them add one more.
3087 if (instr->length()->IsConstantOperand()) {
3088 int const_length = ToInteger32(LConstantOperand::cast(instr->length()));
3089 if (instr->index()->IsConstantOperand()) {
3090 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3091 int index = (const_length - const_index) + 1;
3092 __ lw(result, MemOperand(arguments, index * kPointerSize));
3093 } else {
3094 Register index = ToRegister(instr->index());
3095 __ li(at, Operand(const_length + 1));
3096 __ Subu(result, at, index);
3097 __ sll(at, result, kPointerSizeLog2);
3098 __ Addu(at, arguments, at);
3099 __ lw(result, MemOperand(at));
3100 }
3101 } else if (instr->index()->IsConstantOperand()) {
3102 Register length = ToRegister(instr->length());
3103 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
3104 int loc = const_index - 1;
3105 if (loc != 0) {
3106 __ Subu(result, length, Operand(loc));
3107 __ sll(at, result, kPointerSizeLog2);
3108 __ Addu(at, arguments, at);
3109 __ lw(result, MemOperand(at));
3110 } else {
3111 __ sll(at, length, kPointerSizeLog2);
3112 __ Addu(at, arguments, at);
3113 __ lw(result, MemOperand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003114 }
3115 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003116 Register length = ToRegister(instr->length());
3117 Register index = ToRegister(instr->index());
3118 __ Subu(result, length, index);
3119 __ Addu(result, result, 1);
3120 __ sll(at, result, kPointerSizeLog2);
3121 __ Addu(at, arguments, at);
3122 __ lw(result, MemOperand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003123 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003124}
3125
3126
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003127void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
3128 Register external_pointer = ToRegister(instr->elements());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003129 Register key = no_reg;
3130 ElementsKind elements_kind = instr->elements_kind();
3131 bool key_is_constant = instr->key()->IsConstantOperand();
3132 int constant_key = 0;
3133 if (key_is_constant) {
3134 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3135 if (constant_key & 0xF0000000) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003136 Abort(kArrayIndexConstantValueTooBig);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003137 }
3138 } else {
3139 key = ToRegister(instr->key());
3140 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003141 int element_size_shift = ElementsKindToShiftSize(elements_kind);
3142 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3143 ? (element_size_shift - kSmiTagSize) : element_size_shift;
3144 int base_offset = instr->base_offset();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003145
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003146 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3147 elements_kind == FLOAT32_ELEMENTS ||
3148 elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
3149 elements_kind == FLOAT64_ELEMENTS) {
3150 int base_offset = instr->base_offset();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003151 FPURegister result = ToDoubleRegister(instr->result());
3152 if (key_is_constant) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003153 __ Addu(scratch0(), external_pointer, constant_key << element_size_shift);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003154 } else {
3155 __ sll(scratch0(), key, shift_size);
3156 __ Addu(scratch0(), scratch0(), external_pointer);
3157 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003158 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
3159 elements_kind == FLOAT32_ELEMENTS) {
3160 __ lwc1(result, MemOperand(scratch0(), base_offset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003161 __ cvt_d_s(result, result);
3162 } else { // i.e. elements_kind == EXTERNAL_DOUBLE_ELEMENTS
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003163 __ ldc1(result, MemOperand(scratch0(), base_offset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003164 }
3165 } else {
3166 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003167 MemOperand mem_operand = PrepareKeyedOperand(
3168 key, external_pointer, key_is_constant, constant_key,
3169 element_size_shift, shift_size, base_offset);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003170 switch (elements_kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003171 case EXTERNAL_INT8_ELEMENTS:
3172 case INT8_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003173 __ lb(result, mem_operand);
3174 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003175 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
3176 case EXTERNAL_UINT8_ELEMENTS:
3177 case UINT8_ELEMENTS:
3178 case UINT8_CLAMPED_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003179 __ lbu(result, mem_operand);
3180 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003181 case EXTERNAL_INT16_ELEMENTS:
3182 case INT16_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003183 __ lh(result, mem_operand);
3184 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003185 case EXTERNAL_UINT16_ELEMENTS:
3186 case UINT16_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003187 __ lhu(result, mem_operand);
3188 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003189 case EXTERNAL_INT32_ELEMENTS:
3190 case INT32_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003191 __ lw(result, mem_operand);
3192 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003193 case EXTERNAL_UINT32_ELEMENTS:
3194 case UINT32_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003195 __ lw(result, mem_operand);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003196 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003197 DeoptimizeIf(Ugreater_equal, instr, "negative value", result,
3198 Operand(0x80000000));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003199 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003200 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003201 case FLOAT32_ELEMENTS:
3202 case FLOAT64_ELEMENTS:
3203 case EXTERNAL_FLOAT32_ELEMENTS:
3204 case EXTERNAL_FLOAT64_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003205 case FAST_DOUBLE_ELEMENTS:
3206 case FAST_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003207 case FAST_SMI_ELEMENTS:
3208 case FAST_HOLEY_DOUBLE_ELEMENTS:
3209 case FAST_HOLEY_ELEMENTS:
3210 case FAST_HOLEY_SMI_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003211 case DICTIONARY_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003212 case SLOPPY_ARGUMENTS_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003213 UNREACHABLE();
3214 break;
3215 }
3216 }
3217}
3218
3219
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003220void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
3221 Register elements = ToRegister(instr->elements());
3222 bool key_is_constant = instr->key()->IsConstantOperand();
3223 Register key = no_reg;
3224 DoubleRegister result = ToDoubleRegister(instr->result());
3225 Register scratch = scratch0();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003226
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003227 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
3228
3229 int base_offset = instr->base_offset();
3230 if (key_is_constant) {
3231 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3232 if (constant_key & 0xF0000000) {
3233 Abort(kArrayIndexConstantValueTooBig);
3234 }
3235 base_offset += constant_key * kDoubleSize;
3236 }
3237 __ Addu(scratch, elements, Operand(base_offset));
3238
3239 if (!key_is_constant) {
3240 key = ToRegister(instr->key());
3241 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3242 ? (element_size_shift - kSmiTagSize) : element_size_shift;
3243 __ sll(at, key, shift_size);
3244 __ Addu(scratch, scratch, at);
3245 }
3246
3247 __ ldc1(result, MemOperand(scratch));
3248
3249 if (instr->hydrogen()->RequiresHoleCheck()) {
3250 __ lw(scratch, MemOperand(scratch, kHoleNanUpper32Offset));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003251 DeoptimizeIf(eq, instr, "hole", scratch, Operand(kHoleNanUpper32));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003252 }
3253}
3254
3255
3256void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
3257 Register elements = ToRegister(instr->elements());
3258 Register result = ToRegister(instr->result());
3259 Register scratch = scratch0();
3260 Register store_base = scratch;
3261 int offset = instr->base_offset();
3262
3263 if (instr->key()->IsConstantOperand()) {
3264 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
3265 offset += ToInteger32(const_operand) * kPointerSize;
3266 store_base = elements;
3267 } else {
3268 Register key = ToRegister(instr->key());
3269 // Even though the HLoadKeyed instruction forces the input
3270 // representation for the key to be an integer, the input gets replaced
3271 // during bound check elimination with the index argument to the bounds
3272 // check, which can be tagged, so that case must be handled here, too.
3273 if (instr->hydrogen()->key()->representation().IsSmi()) {
3274 __ sll(scratch, key, kPointerSizeLog2 - kSmiTagSize);
3275 __ addu(scratch, elements, scratch);
3276 } else {
3277 __ sll(scratch, key, kPointerSizeLog2);
3278 __ addu(scratch, elements, scratch);
3279 }
3280 }
3281 __ lw(result, MemOperand(store_base, offset));
3282
3283 // Check for the hole value.
3284 if (instr->hydrogen()->RequiresHoleCheck()) {
3285 if (IsFastSmiElementsKind(instr->hydrogen()->elements_kind())) {
3286 __ SmiTst(result, scratch);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003287 DeoptimizeIf(ne, instr, "not a Smi", scratch, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003288 } else {
3289 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003290 DeoptimizeIf(eq, instr, "hole", result, Operand(scratch));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003291 }
3292 }
3293}
3294
3295
3296void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
3297 if (instr->is_typed_elements()) {
3298 DoLoadKeyedExternalArray(instr);
3299 } else if (instr->hydrogen()->representation().IsDouble()) {
3300 DoLoadKeyedFixedDoubleArray(instr);
3301 } else {
3302 DoLoadKeyedFixedArray(instr);
3303 }
3304}
3305
3306
3307MemOperand LCodeGen::PrepareKeyedOperand(Register key,
3308 Register base,
3309 bool key_is_constant,
3310 int constant_key,
3311 int element_size,
3312 int shift_size,
3313 int base_offset) {
3314 if (key_is_constant) {
3315 return MemOperand(base, (constant_key << element_size) + base_offset);
3316 }
3317
3318 if (base_offset == 0) {
3319 if (shift_size >= 0) {
3320 __ sll(scratch0(), key, shift_size);
3321 __ Addu(scratch0(), base, scratch0());
3322 return MemOperand(scratch0());
3323 } else {
3324 DCHECK_EQ(-1, shift_size);
3325 __ srl(scratch0(), key, 1);
3326 __ Addu(scratch0(), base, scratch0());
3327 return MemOperand(scratch0());
3328 }
3329 }
3330
3331 if (shift_size >= 0) {
3332 __ sll(scratch0(), key, shift_size);
3333 __ Addu(scratch0(), base, scratch0());
3334 return MemOperand(scratch0(), base_offset);
3335 } else {
3336 DCHECK_EQ(-1, shift_size);
3337 __ sra(scratch0(), key, 1);
3338 __ Addu(scratch0(), base, scratch0());
3339 return MemOperand(scratch0(), base_offset);
3340 }
3341}
3342
3343
3344void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3345 DCHECK(ToRegister(instr->context()).is(cp));
3346 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3347 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
3348
3349 if (FLAG_vector_ics) {
3350 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
3351 }
3352
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003353 Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(isolate()).code();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003354 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3355}
3356
3357
3358void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
3359 Register scratch = scratch0();
3360 Register temp = scratch1();
3361 Register result = ToRegister(instr->result());
3362
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003363 if (instr->hydrogen()->from_inlined()) {
3364 __ Subu(result, sp, 2 * kPointerSize);
3365 } else {
3366 // Check if the calling frame is an arguments adaptor frame.
3367 Label done, adapted;
3368 __ lw(scratch, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3369 __ lw(result, MemOperand(scratch, StandardFrameConstants::kContextOffset));
3370 __ Xor(temp, result, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003371
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003372 // Result is the frame pointer for the frame if not adapted and for the real
3373 // frame below the adaptor frame if adapted.
3374 __ Movn(result, fp, temp); // Move only if temp is not equal to zero (ne).
3375 __ Movz(result, scratch, temp); // Move only if temp is equal to zero (eq).
3376 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003377}
3378
3379
3380void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003381 Register elem = ToRegister(instr->elements());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003382 Register result = ToRegister(instr->result());
3383
3384 Label done;
3385
3386 // If no arguments adaptor frame the number of arguments is fixed.
3387 __ Addu(result, zero_reg, Operand(scope()->num_parameters()));
3388 __ Branch(&done, eq, fp, Operand(elem));
3389
3390 // Arguments adaptor frame present. Get argument length from there.
3391 __ lw(result, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3392 __ lw(result,
3393 MemOperand(result, ArgumentsAdaptorFrameConstants::kLengthOffset));
3394 __ SmiUntag(result);
3395
3396 // Argument length is in result register.
3397 __ bind(&done);
3398}
3399
3400
3401void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
3402 Register receiver = ToRegister(instr->receiver());
3403 Register function = ToRegister(instr->function());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003404 Register result = ToRegister(instr->result());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003405 Register scratch = scratch0();
3406
3407 // If the receiver is null or undefined, we have to pass the global
3408 // object as a receiver to normal functions. Values have to be
3409 // passed unchanged to builtins and strict-mode functions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003410 Label global_object, result_in_receiver;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003411
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003412 if (!instr->hydrogen()->known_function()) {
3413 // Do not transform the receiver to object for strict mode
3414 // functions.
3415 __ lw(scratch,
3416 FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
3417 __ lw(scratch,
3418 FieldMemOperand(scratch, SharedFunctionInfo::kCompilerHintsOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003419
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003420 // Do not transform the receiver to object for builtins.
3421 int32_t strict_mode_function_mask =
3422 1 << (SharedFunctionInfo::kStrictModeFunction + kSmiTagSize);
3423 int32_t native_mask = 1 << (SharedFunctionInfo::kNative + kSmiTagSize);
3424 __ And(scratch, scratch, Operand(strict_mode_function_mask | native_mask));
3425 __ Branch(&result_in_receiver, ne, scratch, Operand(zero_reg));
3426 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003427
3428 // Normal function. Replace undefined or null with global receiver.
3429 __ LoadRoot(scratch, Heap::kNullValueRootIndex);
3430 __ Branch(&global_object, eq, receiver, Operand(scratch));
3431 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
3432 __ Branch(&global_object, eq, receiver, Operand(scratch));
3433
3434 // Deoptimize if the receiver is not a JS object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003435 __ SmiTst(receiver, scratch);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003436 DeoptimizeIf(eq, instr, "Smi", scratch, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003437
3438 __ GetObjectType(receiver, scratch, scratch);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003439 DeoptimizeIf(lt, instr, "not a JavaScript object", scratch,
3440 Operand(FIRST_SPEC_OBJECT_TYPE));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003441
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003442 __ Branch(&result_in_receiver);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003443 __ bind(&global_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003444 __ lw(result, FieldMemOperand(function, JSFunction::kContextOffset));
3445 __ lw(result,
3446 ContextOperand(result, Context::GLOBAL_OBJECT_INDEX));
3447 __ lw(result,
3448 FieldMemOperand(result, GlobalObject::kGlobalProxyOffset));
3449
3450 if (result.is(receiver)) {
3451 __ bind(&result_in_receiver);
3452 } else {
3453 Label result_ok;
3454 __ Branch(&result_ok);
3455 __ bind(&result_in_receiver);
3456 __ mov(result, receiver);
3457 __ bind(&result_ok);
3458 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003459}
3460
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003461
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003462void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3463 Register receiver = ToRegister(instr->receiver());
3464 Register function = ToRegister(instr->function());
3465 Register length = ToRegister(instr->length());
3466 Register elements = ToRegister(instr->elements());
3467 Register scratch = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003468 DCHECK(receiver.is(a0)); // Used for parameter count.
3469 DCHECK(function.is(a1)); // Required by InvokeFunction.
3470 DCHECK(ToRegister(instr->result()).is(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003471
3472 // Copy the arguments to this function possibly from the
3473 // adaptor frame below it.
3474 const uint32_t kArgumentsLimit = 1 * KB;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003475 DeoptimizeIf(hi, instr, "too many arguments", length,
3476 Operand(kArgumentsLimit));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003477
3478 // Push the receiver and use the register to keep the original
3479 // number of arguments.
3480 __ push(receiver);
3481 __ Move(receiver, length);
3482 // The arguments are at a one pointer size offset from elements.
3483 __ Addu(elements, elements, Operand(1 * kPointerSize));
3484
3485 // Loop through the arguments pushing them onto the execution
3486 // stack.
3487 Label invoke, loop;
3488 // length is a small non-negative integer, due to the test above.
3489 __ Branch(USE_DELAY_SLOT, &invoke, eq, length, Operand(zero_reg));
3490 __ sll(scratch, length, 2);
3491 __ bind(&loop);
3492 __ Addu(scratch, elements, scratch);
3493 __ lw(scratch, MemOperand(scratch));
3494 __ push(scratch);
3495 __ Subu(length, length, Operand(1));
3496 __ Branch(USE_DELAY_SLOT, &loop, ne, length, Operand(zero_reg));
3497 __ sll(scratch, length, 2);
3498
3499 __ bind(&invoke);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003500 DCHECK(instr->HasPointerMap());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003501 LPointerMap* pointers = instr->pointer_map();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003502 SafepointGenerator safepoint_generator(
3503 this, pointers, Safepoint::kLazyDeopt);
3504 // The number of arguments is stored in receiver which is a0, as expected
3505 // by InvokeFunction.
3506 ParameterCount actual(receiver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003507 __ InvokeFunction(function, actual, CALL_FUNCTION, safepoint_generator);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003508}
3509
3510
3511void LCodeGen::DoPushArgument(LPushArgument* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003512 LOperand* argument = instr->value();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003513 if (argument->IsDoubleRegister() || argument->IsDoubleStackSlot()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003514 Abort(kDoPushArgumentNotImplementedForDoubleType);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003515 } else {
3516 Register argument_reg = EmitLoadRegister(argument, at);
3517 __ push(argument_reg);
3518 }
3519}
3520
3521
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003522void LCodeGen::DoDrop(LDrop* instr) {
3523 __ Drop(instr->count());
3524}
3525
3526
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003527void LCodeGen::DoThisFunction(LThisFunction* instr) {
3528 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003529 __ lw(result, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003530}
3531
3532
3533void LCodeGen::DoContext(LContext* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003534 // If there is a non-return use, the context must be moved to a register.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003535 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003536 if (info()->IsOptimizing()) {
3537 __ lw(result, MemOperand(fp, StandardFrameConstants::kContextOffset));
3538 } else {
3539 // If there is no frame, the context must be in cp.
3540 DCHECK(result.is(cp));
3541 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003542}
3543
3544
3545void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003546 DCHECK(ToRegister(instr->context()).is(cp));
3547 __ li(scratch0(), instr->hydrogen()->pairs());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003548 __ li(scratch1(), Operand(Smi::FromInt(instr->hydrogen()->flags())));
3549 // The context is the first argument.
3550 __ Push(cp, scratch0(), scratch1());
3551 CallRuntime(Runtime::kDeclareGlobals, 3, instr);
3552}
3553
3554
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003555void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003556 int formal_parameter_count,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003557 int arity,
3558 LInstruction* instr,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003559 A1State a1_state) {
3560 bool dont_adapt_arguments =
3561 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3562 bool can_invoke_directly =
3563 dont_adapt_arguments || formal_parameter_count == arity;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003564
3565 LPointerMap* pointers = instr->pointer_map();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003566
3567 if (can_invoke_directly) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003568 if (a1_state == A1_UNINITIALIZED) {
3569 __ li(a1, function);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003570 }
3571
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003572 // Change context.
3573 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
3574
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003575 // Set r0 to arguments count if adaption is not needed. Assumes that r0
3576 // is available to write to at this point.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003577 if (dont_adapt_arguments) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003578 __ li(a0, Operand(arity));
3579 }
3580
3581 // Invoke function.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003582 __ lw(at, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
3583 __ Call(at);
3584
3585 // Set up deoptimization.
3586 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3587 } else {
3588 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3589 ParameterCount count(arity);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003590 ParameterCount expected(formal_parameter_count);
3591 __ InvokeFunction(function, expected, count, CALL_FUNCTION, generator);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003592 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003593}
3594
3595
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003596void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3597 DCHECK(instr->context() != NULL);
3598 DCHECK(ToRegister(instr->context()).is(cp));
3599 Register input = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003600 Register result = ToRegister(instr->result());
3601 Register scratch = scratch0();
3602
3603 // Deoptimize if not a heap number.
3604 __ lw(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
3605 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003606 DeoptimizeIf(ne, instr, "not a heap number", scratch, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003607
3608 Label done;
3609 Register exponent = scratch0();
3610 scratch = no_reg;
3611 __ lw(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3612 // Check the sign of the argument. If the argument is positive, just
3613 // return it.
3614 __ Move(result, input);
3615 __ And(at, exponent, Operand(HeapNumber::kSignMask));
3616 __ Branch(&done, eq, at, Operand(zero_reg));
3617
3618 // Input is negative. Reverse its sign.
3619 // Preserve the value of all registers.
3620 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003621 PushSafepointRegistersScope scope(this);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003622
3623 // Registers were saved at the safepoint, so we can use
3624 // many scratch registers.
3625 Register tmp1 = input.is(a1) ? a0 : a1;
3626 Register tmp2 = input.is(a2) ? a0 : a2;
3627 Register tmp3 = input.is(a3) ? a0 : a3;
3628 Register tmp4 = input.is(t0) ? a0 : t0;
3629
3630 // exponent: floating point exponent value.
3631
3632 Label allocated, slow;
3633 __ LoadRoot(tmp4, Heap::kHeapNumberMapRootIndex);
3634 __ AllocateHeapNumber(tmp1, tmp2, tmp3, tmp4, &slow);
3635 __ Branch(&allocated);
3636
3637 // Slow case: Call the runtime system to do the number allocation.
3638 __ bind(&slow);
3639
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003640 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr,
3641 instr->context());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003642 // Set the pointer to the new heap number in tmp.
3643 if (!tmp1.is(v0))
3644 __ mov(tmp1, v0);
3645 // Restore input_reg after call to runtime.
3646 __ LoadFromSafepointRegisterSlot(input, input);
3647 __ lw(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3648
3649 __ bind(&allocated);
3650 // exponent: floating point exponent value.
3651 // tmp1: allocated heap number.
3652 __ And(exponent, exponent, Operand(~HeapNumber::kSignMask));
3653 __ sw(exponent, FieldMemOperand(tmp1, HeapNumber::kExponentOffset));
3654 __ lw(tmp2, FieldMemOperand(input, HeapNumber::kMantissaOffset));
3655 __ sw(tmp2, FieldMemOperand(tmp1, HeapNumber::kMantissaOffset));
3656
3657 __ StoreToSafepointRegisterSlot(tmp1, result);
3658 }
3659
3660 __ bind(&done);
3661}
3662
3663
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003664void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3665 Register input = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003666 Register result = ToRegister(instr->result());
3667 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
3668 Label done;
3669 __ Branch(USE_DELAY_SLOT, &done, ge, input, Operand(zero_reg));
3670 __ mov(result, input);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003671 __ subu(result, zero_reg, input);
3672 // Overflow if result is still negative, i.e. 0x80000000.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003673 DeoptimizeIf(lt, instr, "overflow", result, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003674 __ bind(&done);
3675}
3676
3677
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003678void LCodeGen::DoMathAbs(LMathAbs* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003679 // Class for deferred case.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003680 class DeferredMathAbsTaggedHeapNumber FINAL : public LDeferredCode {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003681 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003682 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003683 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003684 void Generate() OVERRIDE {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003685 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3686 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003687 LInstruction* instr() OVERRIDE { return instr_; }
3688
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003689 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003690 LMathAbs* instr_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003691 };
3692
3693 Representation r = instr->hydrogen()->value()->representation();
3694 if (r.IsDouble()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003695 FPURegister input = ToDoubleRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003696 FPURegister result = ToDoubleRegister(instr->result());
3697 __ abs_d(result, input);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003698 } else if (r.IsSmiOrInteger32()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003699 EmitIntegerMathAbs(instr);
3700 } else {
3701 // Representation is tagged.
3702 DeferredMathAbsTaggedHeapNumber* deferred =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003703 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3704 Register input = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003705 // Smi check.
3706 __ JumpIfNotSmi(input, deferred->entry());
3707 // If smi, handle it directly.
3708 EmitIntegerMathAbs(instr);
3709 __ bind(deferred->exit());
3710 }
3711}
3712
3713
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003714void LCodeGen::DoMathFloor(LMathFloor* instr) {
3715 DoubleRegister input = ToDoubleRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003716 Register result = ToRegister(instr->result());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003717 Register scratch1 = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003718 Register except_flag = ToRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003719
3720 __ EmitFPUTruncate(kRoundToMinusInf,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003721 result,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003722 input,
3723 scratch1,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003724 double_scratch0(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003725 except_flag);
3726
3727 // Deopt if the operation did not succeed.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003728 DeoptimizeIf(ne, instr, "lost precision or NaN", except_flag,
3729 Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003730
3731 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3732 // Test for -0.
3733 Label done;
3734 __ Branch(&done, ne, result, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003735 __ Mfhc1(scratch1, input);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003736 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003737 DeoptimizeIf(ne, instr, "minus zero", scratch1, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003738 __ bind(&done);
3739 }
3740}
3741
3742
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003743void LCodeGen::DoMathRound(LMathRound* instr) {
3744 DoubleRegister input = ToDoubleRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003745 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003746 DoubleRegister double_scratch1 = ToDoubleRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003747 Register scratch = scratch0();
3748 Label done, check_sign_on_zero;
3749
3750 // Extract exponent bits.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003751 __ Mfhc1(result, input);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003752 __ Ext(scratch,
3753 result,
3754 HeapNumber::kExponentShift,
3755 HeapNumber::kExponentBits);
3756
3757 // If the number is in ]-0.5, +0.5[, the result is +/- 0.
3758 Label skip1;
3759 __ Branch(&skip1, gt, scratch, Operand(HeapNumber::kExponentBias - 2));
3760 __ mov(result, zero_reg);
3761 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3762 __ Branch(&check_sign_on_zero);
3763 } else {
3764 __ Branch(&done);
3765 }
3766 __ bind(&skip1);
3767
3768 // The following conversion will not work with numbers
3769 // outside of ]-2^32, 2^32[.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003770 DeoptimizeIf(ge, instr, "overflow", scratch,
3771 Operand(HeapNumber::kExponentBias + 32));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003772
3773 // Save the original sign for later comparison.
3774 __ And(scratch, result, Operand(HeapNumber::kSignMask));
3775
3776 __ Move(double_scratch0(), 0.5);
3777 __ add_d(double_scratch0(), input, double_scratch0());
3778
3779 // Check sign of the result: if the sign changed, the input
3780 // value was in ]0.5, 0[ and the result should be -0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003781 __ Mfhc1(result, double_scratch0());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003782 __ Xor(result, result, Operand(scratch));
3783 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3784 // ARM uses 'mi' here, which is 'lt'
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003785 DeoptimizeIf(lt, instr, "minus zero", result, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003786 } else {
3787 Label skip2;
3788 // ARM uses 'mi' here, which is 'lt'
3789 // Negating it results in 'ge'
3790 __ Branch(&skip2, ge, result, Operand(zero_reg));
3791 __ mov(result, zero_reg);
3792 __ Branch(&done);
3793 __ bind(&skip2);
3794 }
3795
3796 Register except_flag = scratch;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003797 __ EmitFPUTruncate(kRoundToMinusInf,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003798 result,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003799 double_scratch0(),
3800 at,
3801 double_scratch1,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003802 except_flag);
3803
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003804 DeoptimizeIf(ne, instr, "lost precision or NaN", except_flag,
3805 Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003806
3807 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3808 // Test for -0.
3809 __ Branch(&done, ne, result, Operand(zero_reg));
3810 __ bind(&check_sign_on_zero);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003811 __ Mfhc1(scratch, input);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003812 __ And(scratch, scratch, Operand(HeapNumber::kSignMask));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003813 DeoptimizeIf(ne, instr, "minus zero", scratch, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003814 }
3815 __ bind(&done);
3816}
3817
3818
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003819void LCodeGen::DoMathFround(LMathFround* instr) {
3820 DoubleRegister input = ToDoubleRegister(instr->value());
3821 DoubleRegister result = ToDoubleRegister(instr->result());
3822 __ cvt_s_d(result.low(), input);
3823 __ cvt_d_s(result, result.low());
3824}
3825
3826
3827void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3828 DoubleRegister input = ToDoubleRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003829 DoubleRegister result = ToDoubleRegister(instr->result());
3830 __ sqrt_d(result, input);
3831}
3832
3833
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003834void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3835 DoubleRegister input = ToDoubleRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003836 DoubleRegister result = ToDoubleRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003837 DoubleRegister temp = ToDoubleRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003838
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003839 DCHECK(!input.is(result));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003840
3841 // Note that according to ECMA-262 15.8.2.13:
3842 // Math.pow(-Infinity, 0.5) == Infinity
3843 // Math.sqrt(-Infinity) == NaN
3844 Label done;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003845 __ Move(temp, static_cast<double>(-V8_INFINITY));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003846 __ BranchF(USE_DELAY_SLOT, &done, NULL, eq, temp, input);
3847 // Set up Infinity in the delay slot.
3848 // result is overwritten if the branch is not taken.
3849 __ neg_d(result, temp);
3850
3851 // Add +0 to convert -0 to +0.
3852 __ add_d(result, input, kDoubleRegZero);
3853 __ sqrt_d(result, result);
3854 __ bind(&done);
3855}
3856
3857
3858void LCodeGen::DoPower(LPower* instr) {
3859 Representation exponent_type = instr->hydrogen()->right()->representation();
3860 // Having marked this as a call, we can use any registers.
3861 // Just make sure that the input/output registers are the expected ones.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003862 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3863 DCHECK(!instr->right()->IsDoubleRegister() ||
3864 ToDoubleRegister(instr->right()).is(f4));
3865 DCHECK(!instr->right()->IsRegister() ||
3866 ToRegister(instr->right()).is(tagged_exponent));
3867 DCHECK(ToDoubleRegister(instr->left()).is(f2));
3868 DCHECK(ToDoubleRegister(instr->result()).is(f0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003869
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003870 if (exponent_type.IsSmi()) {
3871 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3872 __ CallStub(&stub);
3873 } else if (exponent_type.IsTagged()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003874 Label no_deopt;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003875 __ JumpIfSmi(tagged_exponent, &no_deopt);
3876 DCHECK(!t3.is(tagged_exponent));
3877 __ lw(t3, FieldMemOperand(tagged_exponent, HeapObject::kMapOffset));
3878 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003879 DeoptimizeIf(ne, instr, "not a heap number", t3, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003880 __ bind(&no_deopt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003881 MathPowStub stub(isolate(), MathPowStub::TAGGED);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003882 __ CallStub(&stub);
3883 } else if (exponent_type.IsInteger32()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003884 MathPowStub stub(isolate(), MathPowStub::INTEGER);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003885 __ CallStub(&stub);
3886 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003887 DCHECK(exponent_type.IsDouble());
3888 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003889 __ CallStub(&stub);
3890 }
3891}
3892
3893
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003894void LCodeGen::DoMathExp(LMathExp* instr) {
3895 DoubleRegister input = ToDoubleRegister(instr->value());
3896 DoubleRegister result = ToDoubleRegister(instr->result());
3897 DoubleRegister double_scratch1 = ToDoubleRegister(instr->double_temp());
3898 DoubleRegister double_scratch2 = double_scratch0();
3899 Register temp1 = ToRegister(instr->temp1());
3900 Register temp2 = ToRegister(instr->temp2());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003901
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003902 MathExpGenerator::EmitMathExp(
3903 masm(), input, result, double_scratch1, double_scratch2,
3904 temp1, temp2, scratch0());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003905}
3906
3907
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003908void LCodeGen::DoMathLog(LMathLog* instr) {
3909 __ PrepareCallCFunction(0, 1, scratch0());
3910 __ MovToFloatParameter(ToDoubleRegister(instr->value()));
3911 __ CallCFunction(ExternalReference::math_log_double_function(isolate()),
3912 0, 1);
3913 __ MovFromFloatResult(ToDoubleRegister(instr->result()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003914}
3915
3916
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003917void LCodeGen::DoMathClz32(LMathClz32* instr) {
3918 Register input = ToRegister(instr->value());
3919 Register result = ToRegister(instr->result());
3920 __ Clz(result, input);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003921}
3922
3923
3924void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003925 DCHECK(ToRegister(instr->context()).is(cp));
3926 DCHECK(ToRegister(instr->function()).is(a1));
3927 DCHECK(instr->HasPointerMap());
3928
3929 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
3930 if (known_function.is_null()) {
3931 LPointerMap* pointers = instr->pointer_map();
3932 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3933 ParameterCount count(instr->arity());
3934 __ InvokeFunction(a1, count, CALL_FUNCTION, generator);
3935 } else {
3936 CallKnownFunction(known_function,
3937 instr->hydrogen()->formal_parameter_count(),
3938 instr->arity(),
3939 instr,
3940 A1_CONTAINS_TARGET);
3941 }
3942}
3943
3944
3945void LCodeGen::DoTailCallThroughMegamorphicCache(
3946 LTailCallThroughMegamorphicCache* instr) {
3947 Register receiver = ToRegister(instr->receiver());
3948 Register name = ToRegister(instr->name());
3949 DCHECK(receiver.is(LoadDescriptor::ReceiverRegister()));
3950 DCHECK(name.is(LoadDescriptor::NameRegister()));
3951 DCHECK(receiver.is(a1));
3952 DCHECK(name.is(a2));
3953
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003954 Register scratch = t0;
3955 Register extra = t1;
3956 Register extra2 = t2;
3957 Register extra3 = t5;
3958#ifdef DEBUG
3959 Register slot = FLAG_vector_ics ? ToRegister(instr->slot()) : no_reg;
3960 Register vector = FLAG_vector_ics ? ToRegister(instr->vector()) : no_reg;
3961 DCHECK(!FLAG_vector_ics ||
3962 !AreAliased(slot, vector, scratch, extra, extra2, extra3));
3963#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003964
3965 // Important for the tail-call.
3966 bool must_teardown_frame = NeedsEagerFrame();
3967
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003968 if (!instr->hydrogen()->is_just_miss()) {
3969 DCHECK(!instr->hydrogen()->is_keyed_load());
3970
3971 // The probe will tail call to a handler if found.
3972 isolate()->stub_cache()->GenerateProbe(
3973 masm(), Code::LOAD_IC, instr->hydrogen()->flags(), must_teardown_frame,
3974 receiver, name, scratch, extra, extra2, extra3);
3975 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003976
3977 // Tail call to miss if we ended up here.
3978 if (must_teardown_frame) __ LeaveFrame(StackFrame::INTERNAL);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003979 if (instr->hydrogen()->is_keyed_load()) {
3980 KeyedLoadIC::GenerateMiss(masm());
3981 } else {
3982 LoadIC::GenerateMiss(masm());
3983 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003984}
3985
3986
3987void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
3988 DCHECK(ToRegister(instr->result()).is(v0));
3989
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003990 if (instr->hydrogen()->IsTailCall()) {
3991 if (NeedsEagerFrame()) __ LeaveFrame(StackFrame::INTERNAL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003992
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003993 if (instr->target()->IsConstantOperand()) {
3994 LConstantOperand* target = LConstantOperand::cast(instr->target());
3995 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3996 __ Jump(code, RelocInfo::CODE_TARGET);
3997 } else {
3998 DCHECK(instr->target()->IsRegister());
3999 Register target = ToRegister(instr->target());
4000 __ Addu(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
4001 __ Jump(target);
4002 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004003 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004004 LPointerMap* pointers = instr->pointer_map();
4005 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
4006
4007 if (instr->target()->IsConstantOperand()) {
4008 LConstantOperand* target = LConstantOperand::cast(instr->target());
4009 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
4010 generator.BeforeCall(__ CallSize(code, RelocInfo::CODE_TARGET));
4011 __ Call(code, RelocInfo::CODE_TARGET);
4012 } else {
4013 DCHECK(instr->target()->IsRegister());
4014 Register target = ToRegister(instr->target());
4015 generator.BeforeCall(__ CallSize(target));
4016 __ Addu(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
4017 __ Call(target);
4018 }
4019 generator.AfterCall();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004020 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004021}
4022
4023
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004024void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
4025 DCHECK(ToRegister(instr->function()).is(a1));
4026 DCHECK(ToRegister(instr->result()).is(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004027
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004028 if (instr->hydrogen()->pass_argument_count()) {
4029 __ li(a0, Operand(instr->arity()));
4030 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004031
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004032 // Change context.
4033 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004034
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004035 // Load the code entry address
4036 __ lw(at, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
4037 __ Call(at);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004038
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004039 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004040}
4041
4042
4043void LCodeGen::DoCallFunction(LCallFunction* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004044 DCHECK(ToRegister(instr->context()).is(cp));
4045 DCHECK(ToRegister(instr->function()).is(a1));
4046 DCHECK(ToRegister(instr->result()).is(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004047
4048 int arity = instr->arity();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004049 CallFunctionStub stub(isolate(), arity, instr->hydrogen()->function_flags());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004050 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004051}
4052
4053
4054void LCodeGen::DoCallNew(LCallNew* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004055 DCHECK(ToRegister(instr->context()).is(cp));
4056 DCHECK(ToRegister(instr->constructor()).is(a1));
4057 DCHECK(ToRegister(instr->result()).is(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004058
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004059 __ li(a0, Operand(instr->arity()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004060 // No cell in a2 for construct type feedback in optimized code
4061 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4062 CallConstructStub stub(isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004063 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4064}
4065
4066
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004067void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
4068 DCHECK(ToRegister(instr->context()).is(cp));
4069 DCHECK(ToRegister(instr->constructor()).is(a1));
4070 DCHECK(ToRegister(instr->result()).is(v0));
4071
4072 __ li(a0, Operand(instr->arity()));
4073 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4074 ElementsKind kind = instr->hydrogen()->elements_kind();
4075 AllocationSiteOverrideMode override_mode =
4076 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
4077 ? DISABLE_ALLOCATION_SITES
4078 : DONT_OVERRIDE;
4079
4080 if (instr->arity() == 0) {
4081 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
4082 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4083 } else if (instr->arity() == 1) {
4084 Label done;
4085 if (IsFastPackedElementsKind(kind)) {
4086 Label packed_case;
4087 // We might need a change here,
4088 // look at the first argument.
4089 __ lw(t1, MemOperand(sp, 0));
4090 __ Branch(&packed_case, eq, t1, Operand(zero_reg));
4091
4092 ElementsKind holey_kind = GetHoleyElementsKind(kind);
4093 ArraySingleArgumentConstructorStub stub(isolate(),
4094 holey_kind,
4095 override_mode);
4096 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4097 __ jmp(&done);
4098 __ bind(&packed_case);
4099 }
4100
4101 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
4102 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4103 __ bind(&done);
4104 } else {
4105 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
4106 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
4107 }
4108}
4109
4110
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004111void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
4112 CallRuntime(instr->function(), instr->arity(), instr);
4113}
4114
4115
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004116void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
4117 Register function = ToRegister(instr->function());
4118 Register code_object = ToRegister(instr->code_object());
4119 __ Addu(code_object, code_object,
4120 Operand(Code::kHeaderSize - kHeapObjectTag));
4121 __ sw(code_object,
4122 FieldMemOperand(function, JSFunction::kCodeEntryOffset));
4123}
4124
4125
4126void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
4127 Register result = ToRegister(instr->result());
4128 Register base = ToRegister(instr->base_object());
4129 if (instr->offset()->IsConstantOperand()) {
4130 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
4131 __ Addu(result, base, Operand(ToInteger32(offset)));
4132 } else {
4133 Register offset = ToRegister(instr->offset());
4134 __ Addu(result, base, offset);
4135 }
4136}
4137
4138
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004139void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004140 Representation representation = instr->representation();
4141
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004142 Register object = ToRegister(instr->object());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004143 Register scratch = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004144 HObjectAccess access = instr->hydrogen()->access();
4145 int offset = access.offset();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004146
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004147 if (access.IsExternalMemory()) {
4148 Register value = ToRegister(instr->value());
4149 MemOperand operand = MemOperand(object, offset);
4150 __ Store(value, operand, representation);
4151 return;
4152 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004153
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004154 __ AssertNotSmi(object);
4155
4156 DCHECK(!representation.IsSmi() ||
4157 !instr->value()->IsConstantOperand() ||
4158 IsSmi(LConstantOperand::cast(instr->value())));
4159 if (representation.IsDouble()) {
4160 DCHECK(access.IsInobject());
4161 DCHECK(!instr->hydrogen()->has_transition());
4162 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4163 DoubleRegister value = ToDoubleRegister(instr->value());
4164 __ sdc1(value, FieldMemOperand(object, offset));
4165 return;
4166 }
4167
4168 if (instr->hydrogen()->has_transition()) {
4169 Handle<Map> transition = instr->hydrogen()->transition_map();
4170 AddDeprecationDependency(transition);
4171 __ li(scratch, Operand(transition));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004172 __ sw(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004173 if (instr->hydrogen()->NeedsWriteBarrierForMap()) {
4174 Register temp = ToRegister(instr->temp());
4175 // Update the write barrier for the map field.
4176 __ RecordWriteForMap(object,
4177 scratch,
4178 temp,
4179 GetRAState(),
4180 kSaveFPRegs);
4181 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004182 }
4183
4184 // Do the store.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004185 Register value = ToRegister(instr->value());
4186 if (access.IsInobject()) {
4187 MemOperand operand = FieldMemOperand(object, offset);
4188 __ Store(value, operand, representation);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004189 if (instr->hydrogen()->NeedsWriteBarrier()) {
4190 // Update the write barrier for the object for in-object properties.
4191 __ RecordWriteField(object,
4192 offset,
4193 value,
4194 scratch,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004195 GetRAState(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004196 kSaveFPRegs,
4197 EMIT_REMEMBERED_SET,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004198 instr->hydrogen()->SmiCheckForWriteBarrier(),
4199 instr->hydrogen()->PointersToHereCheckForValue());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004200 }
4201 } else {
4202 __ lw(scratch, FieldMemOperand(object, JSObject::kPropertiesOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004203 MemOperand operand = FieldMemOperand(scratch, offset);
4204 __ Store(value, operand, representation);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004205 if (instr->hydrogen()->NeedsWriteBarrier()) {
4206 // Update the write barrier for the properties array.
4207 // object is used as a scratch register.
4208 __ RecordWriteField(scratch,
4209 offset,
4210 value,
4211 object,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004212 GetRAState(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004213 kSaveFPRegs,
4214 EMIT_REMEMBERED_SET,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004215 instr->hydrogen()->SmiCheckForWriteBarrier(),
4216 instr->hydrogen()->PointersToHereCheckForValue());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004217 }
4218 }
4219}
4220
4221
4222void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004223 DCHECK(ToRegister(instr->context()).is(cp));
4224 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4225 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004226
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004227 __ li(StoreDescriptor::NameRegister(), Operand(instr->name()));
4228 Handle<Code> ic = StoreIC::initialize_stub(isolate(), instr->strict_mode());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004229 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4230}
4231
4232
4233void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004234 Condition cc = instr->hydrogen()->allow_equality() ? hi : hs;
4235 Operand operand(0);
4236 Register reg;
4237 if (instr->index()->IsConstantOperand()) {
4238 operand = ToOperand(instr->index());
4239 reg = ToRegister(instr->length());
4240 cc = CommuteCondition(cc);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004241 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004242 reg = ToRegister(instr->index());
4243 operand = ToOperand(instr->length());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004244 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004245 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
4246 Label done;
4247 __ Branch(&done, NegateCondition(cc), reg, operand);
4248 __ stop("eliminated bounds check failed");
4249 __ bind(&done);
4250 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004251 DeoptimizeIf(cc, instr, "out of bounds", reg, operand);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004252 }
4253}
4254
4255
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004256void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
4257 Register external_pointer = ToRegister(instr->elements());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004258 Register key = no_reg;
4259 ElementsKind elements_kind = instr->elements_kind();
4260 bool key_is_constant = instr->key()->IsConstantOperand();
4261 int constant_key = 0;
4262 if (key_is_constant) {
4263 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4264 if (constant_key & 0xF0000000) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004265 Abort(kArrayIndexConstantValueTooBig);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004266 }
4267 } else {
4268 key = ToRegister(instr->key());
4269 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004270 int element_size_shift = ElementsKindToShiftSize(elements_kind);
4271 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4272 ? (element_size_shift - kSmiTagSize) : element_size_shift;
4273 int base_offset = instr->base_offset();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004274
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004275 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4276 elements_kind == FLOAT32_ELEMENTS ||
4277 elements_kind == EXTERNAL_FLOAT64_ELEMENTS ||
4278 elements_kind == FLOAT64_ELEMENTS) {
4279 Register address = scratch0();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004280 FPURegister value(ToDoubleRegister(instr->value()));
4281 if (key_is_constant) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004282 if (constant_key != 0) {
4283 __ Addu(address, external_pointer,
4284 Operand(constant_key << element_size_shift));
4285 } else {
4286 address = external_pointer;
4287 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004288 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004289 __ sll(address, key, shift_size);
4290 __ Addu(address, external_pointer, address);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004291 }
4292
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004293 if (elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
4294 elements_kind == FLOAT32_ELEMENTS) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004295 __ cvt_s_d(double_scratch0(), value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004296 __ swc1(double_scratch0(), MemOperand(address, base_offset));
4297 } else { // Storing doubles, not floats.
4298 __ sdc1(value, MemOperand(address, base_offset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004299 }
4300 } else {
4301 Register value(ToRegister(instr->value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004302 MemOperand mem_operand = PrepareKeyedOperand(
4303 key, external_pointer, key_is_constant, constant_key,
4304 element_size_shift, shift_size,
4305 base_offset);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004306 switch (elements_kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004307 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
4308 case EXTERNAL_INT8_ELEMENTS:
4309 case EXTERNAL_UINT8_ELEMENTS:
4310 case UINT8_ELEMENTS:
4311 case UINT8_CLAMPED_ELEMENTS:
4312 case INT8_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004313 __ sb(value, mem_operand);
4314 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004315 case EXTERNAL_INT16_ELEMENTS:
4316 case EXTERNAL_UINT16_ELEMENTS:
4317 case INT16_ELEMENTS:
4318 case UINT16_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004319 __ sh(value, mem_operand);
4320 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004321 case EXTERNAL_INT32_ELEMENTS:
4322 case EXTERNAL_UINT32_ELEMENTS:
4323 case INT32_ELEMENTS:
4324 case UINT32_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004325 __ sw(value, mem_operand);
4326 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004327 case FLOAT32_ELEMENTS:
4328 case FLOAT64_ELEMENTS:
4329 case EXTERNAL_FLOAT32_ELEMENTS:
4330 case EXTERNAL_FLOAT64_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004331 case FAST_DOUBLE_ELEMENTS:
4332 case FAST_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004333 case FAST_SMI_ELEMENTS:
4334 case FAST_HOLEY_DOUBLE_ELEMENTS:
4335 case FAST_HOLEY_ELEMENTS:
4336 case FAST_HOLEY_SMI_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004337 case DICTIONARY_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004338 case SLOPPY_ARGUMENTS_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004339 UNREACHABLE();
4340 break;
4341 }
4342 }
4343}
4344
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004345
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004346void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
4347 DoubleRegister value = ToDoubleRegister(instr->value());
4348 Register elements = ToRegister(instr->elements());
4349 Register scratch = scratch0();
4350 DoubleRegister double_scratch = double_scratch0();
4351 bool key_is_constant = instr->key()->IsConstantOperand();
4352 int base_offset = instr->base_offset();
4353 Label not_nan, done;
4354
4355 // Calculate the effective address of the slot in the array to store the
4356 // double value.
4357 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
4358 if (key_is_constant) {
4359 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4360 if (constant_key & 0xF0000000) {
4361 Abort(kArrayIndexConstantValueTooBig);
4362 }
4363 __ Addu(scratch, elements,
4364 Operand((constant_key << element_size_shift) + base_offset));
4365 } else {
4366 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
4367 ? (element_size_shift - kSmiTagSize) : element_size_shift;
4368 __ Addu(scratch, elements, Operand(base_offset));
4369 __ sll(at, ToRegister(instr->key()), shift_size);
4370 __ Addu(scratch, scratch, at);
4371 }
4372
4373 if (instr->NeedsCanonicalization()) {
4374 Label is_nan;
4375 // Check for NaN. All NaNs must be canonicalized.
4376 __ BranchF(NULL, &is_nan, eq, value, value);
4377 __ Branch(&not_nan);
4378
4379 // Only load canonical NaN if the comparison above set the overflow.
4380 __ bind(&is_nan);
4381 __ LoadRoot(at, Heap::kNanValueRootIndex);
4382 __ ldc1(double_scratch, FieldMemOperand(at, HeapNumber::kValueOffset));
4383 __ sdc1(double_scratch, MemOperand(scratch, 0));
4384 __ Branch(&done);
4385 }
4386
4387 __ bind(&not_nan);
4388 __ sdc1(value, MemOperand(scratch, 0));
4389 __ bind(&done);
4390}
4391
4392
4393void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4394 Register value = ToRegister(instr->value());
4395 Register elements = ToRegister(instr->elements());
4396 Register key = instr->key()->IsRegister() ? ToRegister(instr->key())
4397 : no_reg;
4398 Register scratch = scratch0();
4399 Register store_base = scratch;
4400 int offset = instr->base_offset();
4401
4402 // Do the store.
4403 if (instr->key()->IsConstantOperand()) {
4404 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
4405 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
4406 offset += ToInteger32(const_operand) * kPointerSize;
4407 store_base = elements;
4408 } else {
4409 // Even though the HLoadKeyed instruction forces the input
4410 // representation for the key to be an integer, the input gets replaced
4411 // during bound check elimination with the index argument to the bounds
4412 // check, which can be tagged, so that case must be handled here, too.
4413 if (instr->hydrogen()->key()->representation().IsSmi()) {
4414 __ sll(scratch, key, kPointerSizeLog2 - kSmiTagSize);
4415 __ addu(scratch, elements, scratch);
4416 } else {
4417 __ sll(scratch, key, kPointerSizeLog2);
4418 __ addu(scratch, elements, scratch);
4419 }
4420 }
4421 __ sw(value, MemOperand(store_base, offset));
4422
4423 if (instr->hydrogen()->NeedsWriteBarrier()) {
4424 SmiCheck check_needed =
4425 instr->hydrogen()->value()->type().IsHeapObject()
4426 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4427 // Compute address of modified element and store it into key register.
4428 __ Addu(key, store_base, Operand(offset));
4429 __ RecordWrite(elements,
4430 key,
4431 value,
4432 GetRAState(),
4433 kSaveFPRegs,
4434 EMIT_REMEMBERED_SET,
4435 check_needed,
4436 instr->hydrogen()->PointersToHereCheckForValue());
4437 }
4438}
4439
4440
4441void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4442 // By cases: external, fast double
4443 if (instr->is_typed_elements()) {
4444 DoStoreKeyedExternalArray(instr);
4445 } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4446 DoStoreKeyedFixedDoubleArray(instr);
4447 } else {
4448 DoStoreKeyedFixedArray(instr);
4449 }
4450}
4451
4452
4453void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4454 DCHECK(ToRegister(instr->context()).is(cp));
4455 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4456 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4457 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4458
4459 Handle<Code> ic =
4460 CodeFactory::KeyedStoreIC(isolate(), instr->strict_mode()).code();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004461 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4462}
4463
4464
4465void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4466 Register object_reg = ToRegister(instr->object());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004467 Register scratch = scratch0();
4468
4469 Handle<Map> from_map = instr->original_map();
4470 Handle<Map> to_map = instr->transitioned_map();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004471 ElementsKind from_kind = instr->from_kind();
4472 ElementsKind to_kind = instr->to_kind();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004473
4474 Label not_applicable;
4475 __ lw(scratch, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4476 __ Branch(&not_applicable, ne, scratch, Operand(from_map));
4477
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004478 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
4479 Register new_map_reg = ToRegister(instr->new_map_temp());
4480 __ li(new_map_reg, Operand(to_map));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004481 __ sw(new_map_reg, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4482 // Write barrier.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004483 __ RecordWriteForMap(object_reg,
4484 new_map_reg,
4485 scratch,
4486 GetRAState(),
4487 kDontSaveFPRegs);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004488 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004489 DCHECK(object_reg.is(a0));
4490 DCHECK(ToRegister(instr->context()).is(cp));
4491 PushSafepointRegistersScope scope(this);
4492 __ li(a1, Operand(to_map));
4493 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4494 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4495 __ CallStub(&stub);
4496 RecordSafepointWithRegisters(
4497 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004498 }
4499 __ bind(&not_applicable);
4500}
4501
4502
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004503void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4504 Register object = ToRegister(instr->object());
4505 Register temp = ToRegister(instr->temp());
4506 Label no_memento_found;
4507 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found,
4508 ne, &no_memento_found);
4509 DeoptimizeIf(al, instr);
4510 __ bind(&no_memento_found);
4511}
4512
4513
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004514void LCodeGen::DoStringAdd(LStringAdd* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004515 DCHECK(ToRegister(instr->context()).is(cp));
4516 DCHECK(ToRegister(instr->left()).is(a1));
4517 DCHECK(ToRegister(instr->right()).is(a0));
4518 StringAddStub stub(isolate(),
4519 instr->hydrogen()->flags(),
4520 instr->hydrogen()->pretenure_flag());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004521 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4522}
4523
4524
4525void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004526 class DeferredStringCharCodeAt FINAL : public LDeferredCode {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004527 public:
4528 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
4529 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004530 void Generate() OVERRIDE { codegen()->DoDeferredStringCharCodeAt(instr_); }
4531 LInstruction* instr() OVERRIDE { return instr_; }
4532
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004533 private:
4534 LStringCharCodeAt* instr_;
4535 };
4536
4537 DeferredStringCharCodeAt* deferred =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004538 new(zone()) DeferredStringCharCodeAt(this, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004539 StringCharLoadGenerator::Generate(masm(),
4540 ToRegister(instr->string()),
4541 ToRegister(instr->index()),
4542 ToRegister(instr->result()),
4543 deferred->entry());
4544 __ bind(deferred->exit());
4545}
4546
4547
4548void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4549 Register string = ToRegister(instr->string());
4550 Register result = ToRegister(instr->result());
4551 Register scratch = scratch0();
4552
4553 // TODO(3095996): Get rid of this. For now, we need to make the
4554 // result register contain a valid pointer because it is already
4555 // contained in the register pointer map.
4556 __ mov(result, zero_reg);
4557
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004558 PushSafepointRegistersScope scope(this);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004559 __ push(string);
4560 // Push the index as a smi. This is safe because of the checks in
4561 // DoStringCharCodeAt above.
4562 if (instr->index()->IsConstantOperand()) {
4563 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
4564 __ Addu(scratch, zero_reg, Operand(Smi::FromInt(const_index)));
4565 __ push(scratch);
4566 } else {
4567 Register index = ToRegister(instr->index());
4568 __ SmiTag(index);
4569 __ push(index);
4570 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004571 CallRuntimeFromDeferred(Runtime::kStringCharCodeAtRT, 2, instr,
4572 instr->context());
4573 __ AssertSmi(v0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004574 __ SmiUntag(v0);
4575 __ StoreToSafepointRegisterSlot(v0, result);
4576}
4577
4578
4579void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004580 class DeferredStringCharFromCode FINAL : public LDeferredCode {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004581 public:
4582 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
4583 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004584 void Generate() OVERRIDE {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004585 codegen()->DoDeferredStringCharFromCode(instr_);
4586 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004587 LInstruction* instr() OVERRIDE { return instr_; }
4588
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004589 private:
4590 LStringCharFromCode* instr_;
4591 };
4592
4593 DeferredStringCharFromCode* deferred =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004594 new(zone()) DeferredStringCharFromCode(this, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004595
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004596 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004597 Register char_code = ToRegister(instr->char_code());
4598 Register result = ToRegister(instr->result());
4599 Register scratch = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004600 DCHECK(!char_code.is(result));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004601
4602 __ Branch(deferred->entry(), hi,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004603 char_code, Operand(String::kMaxOneByteCharCode));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004604 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
4605 __ sll(scratch, char_code, kPointerSizeLog2);
4606 __ Addu(result, result, scratch);
4607 __ lw(result, FieldMemOperand(result, FixedArray::kHeaderSize));
4608 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
4609 __ Branch(deferred->entry(), eq, result, Operand(scratch));
4610 __ bind(deferred->exit());
4611}
4612
4613
4614void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4615 Register char_code = ToRegister(instr->char_code());
4616 Register result = ToRegister(instr->result());
4617
4618 // TODO(3095996): Get rid of this. For now, we need to make the
4619 // result register contain a valid pointer because it is already
4620 // contained in the register pointer map.
4621 __ mov(result, zero_reg);
4622
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004623 PushSafepointRegistersScope scope(this);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004624 __ SmiTag(char_code);
4625 __ push(char_code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004626 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr, instr->context());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004627 __ StoreToSafepointRegisterSlot(v0, result);
4628}
4629
4630
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004631void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004632 LOperand* input = instr->value();
4633 DCHECK(input->IsRegister() || input->IsStackSlot());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004634 LOperand* output = instr->result();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004635 DCHECK(output->IsDoubleRegister());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004636 FPURegister single_scratch = double_scratch0().low();
4637 if (input->IsStackSlot()) {
4638 Register scratch = scratch0();
4639 __ lw(scratch, ToMemOperand(input));
4640 __ mtc1(scratch, single_scratch);
4641 } else {
4642 __ mtc1(ToRegister(input), single_scratch);
4643 }
4644 __ cvt_d_w(ToDoubleRegister(output), single_scratch);
4645}
4646
4647
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004648void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4649 LOperand* input = instr->value();
4650 LOperand* output = instr->result();
4651
4652 FPURegister dbl_scratch = double_scratch0();
4653 __ mtc1(ToRegister(input), dbl_scratch);
4654 __ Cvt_d_uw(ToDoubleRegister(output), dbl_scratch, f22);
4655}
4656
4657
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004658void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004659 class DeferredNumberTagI FINAL : public LDeferredCode {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004660 public:
4661 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
4662 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004663 void Generate() OVERRIDE {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004664 codegen()->DoDeferredNumberTagIU(instr_,
4665 instr_->value(),
4666 instr_->temp1(),
4667 instr_->temp2(),
4668 SIGNED_INT32);
4669 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004670 LInstruction* instr() OVERRIDE { return instr_; }
4671
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004672 private:
4673 LNumberTagI* instr_;
4674 };
4675
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004676 Register src = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004677 Register dst = ToRegister(instr->result());
4678 Register overflow = scratch0();
4679
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004680 DeferredNumberTagI* deferred = new(zone()) DeferredNumberTagI(this, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004681 __ SmiTagCheckOverflow(dst, src, overflow);
4682 __ BranchOnOverflow(deferred->entry(), overflow);
4683 __ bind(deferred->exit());
4684}
4685
4686
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004687void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4688 class DeferredNumberTagU FINAL : public LDeferredCode {
4689 public:
4690 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4691 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004692 void Generate() OVERRIDE {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004693 codegen()->DoDeferredNumberTagIU(instr_,
4694 instr_->value(),
4695 instr_->temp1(),
4696 instr_->temp2(),
4697 UNSIGNED_INT32);
4698 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004699 LInstruction* instr() OVERRIDE { return instr_; }
4700
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004701 private:
4702 LNumberTagU* instr_;
4703 };
4704
4705 Register input = ToRegister(instr->value());
4706 Register result = ToRegister(instr->result());
4707
4708 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4709 __ Branch(deferred->entry(), hi, input, Operand(Smi::kMaxValue));
4710 __ SmiTag(result, input);
4711 __ bind(deferred->exit());
4712}
4713
4714
4715void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4716 LOperand* value,
4717 LOperand* temp1,
4718 LOperand* temp2,
4719 IntegerSignedness signedness) {
4720 Label done, slow;
4721 Register src = ToRegister(value);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004722 Register dst = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004723 Register tmp1 = scratch0();
4724 Register tmp2 = ToRegister(temp1);
4725 Register tmp3 = ToRegister(temp2);
4726 DoubleRegister dbl_scratch = double_scratch0();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004727
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004728 if (signedness == SIGNED_INT32) {
4729 // There was overflow, so bits 30 and 31 of the original integer
4730 // disagree. Try to allocate a heap number in new space and store
4731 // the value in there. If that fails, call the runtime system.
4732 if (dst.is(src)) {
4733 __ SmiUntag(src, dst);
4734 __ Xor(src, src, Operand(0x80000000));
4735 }
4736 __ mtc1(src, dbl_scratch);
4737 __ cvt_d_w(dbl_scratch, dbl_scratch);
4738 } else {
4739 __ mtc1(src, dbl_scratch);
4740 __ Cvt_d_uw(dbl_scratch, dbl_scratch, f22);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004741 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004742
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004743 if (FLAG_inline_new) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004744 __ LoadRoot(tmp3, Heap::kHeapNumberMapRootIndex);
4745 __ AllocateHeapNumber(dst, tmp1, tmp2, tmp3, &slow, DONT_TAG_RESULT);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004746 __ Branch(&done);
4747 }
4748
4749 // Slow case: Call the runtime system to do the number allocation.
4750 __ bind(&slow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004751 {
4752 // TODO(3095996): Put a valid pointer value in the stack slot where the
4753 // result register is stored, as this register is in the pointer map, but
4754 // contains an integer value.
4755 __ mov(dst, zero_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004756
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004757 // Preserve the value of all registers.
4758 PushSafepointRegistersScope scope(this);
4759
4760 // NumberTagI and NumberTagD use the context from the frame, rather than
4761 // the environment's HContext or HInlinedContext value.
4762 // They only call Runtime::kAllocateHeapNumber.
4763 // The corresponding HChange instructions are added in a phase that does
4764 // not have easy access to the local context.
4765 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4766 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4767 RecordSafepointWithRegisters(
4768 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4769 __ Subu(v0, v0, kHeapObjectTag);
4770 __ StoreToSafepointRegisterSlot(v0, dst);
4771 }
4772
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004773
4774 // Done. Put the value in dbl_scratch into the value of the allocated heap
4775 // number.
4776 __ bind(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004777 __ sdc1(dbl_scratch, MemOperand(dst, HeapNumber::kValueOffset));
4778 __ Addu(dst, dst, kHeapObjectTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004779}
4780
4781
4782void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004783 class DeferredNumberTagD FINAL : public LDeferredCode {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004784 public:
4785 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4786 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004787 void Generate() OVERRIDE { codegen()->DoDeferredNumberTagD(instr_); }
4788 LInstruction* instr() OVERRIDE { return instr_; }
4789
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004790 private:
4791 LNumberTagD* instr_;
4792 };
4793
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004794 DoubleRegister input_reg = ToDoubleRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004795 Register scratch = scratch0();
4796 Register reg = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004797 Register temp1 = ToRegister(instr->temp());
4798 Register temp2 = ToRegister(instr->temp2());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004799
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004800 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004801 if (FLAG_inline_new) {
4802 __ LoadRoot(scratch, Heap::kHeapNumberMapRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004803 // We want the untagged address first for performance
4804 __ AllocateHeapNumber(reg, temp1, temp2, scratch, deferred->entry(),
4805 DONT_TAG_RESULT);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004806 } else {
4807 __ Branch(deferred->entry());
4808 }
4809 __ bind(deferred->exit());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004810 __ sdc1(input_reg, MemOperand(reg, HeapNumber::kValueOffset));
4811 // Now that we have finished with the object's real address tag it
4812 __ Addu(reg, reg, kHeapObjectTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004813}
4814
4815
4816void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4817 // TODO(3095996): Get rid of this. For now, we need to make the
4818 // result register contain a valid pointer because it is already
4819 // contained in the register pointer map.
4820 Register reg = ToRegister(instr->result());
4821 __ mov(reg, zero_reg);
4822
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004823 PushSafepointRegistersScope scope(this);
4824 // NumberTagI and NumberTagD use the context from the frame, rather than
4825 // the environment's HContext or HInlinedContext value.
4826 // They only call Runtime::kAllocateHeapNumber.
4827 // The corresponding HChange instructions are added in a phase that does
4828 // not have easy access to the local context.
4829 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4830 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4831 RecordSafepointWithRegisters(
4832 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4833 __ Subu(v0, v0, kHeapObjectTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004834 __ StoreToSafepointRegisterSlot(v0, reg);
4835}
4836
4837
4838void LCodeGen::DoSmiTag(LSmiTag* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004839 HChange* hchange = instr->hydrogen();
4840 Register input = ToRegister(instr->value());
4841 Register output = ToRegister(instr->result());
4842 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4843 hchange->value()->CheckFlag(HValue::kUint32)) {
4844 __ And(at, input, Operand(0xc0000000));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004845 DeoptimizeIf(ne, instr, "overflow", at, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004846 }
4847 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4848 !hchange->value()->CheckFlag(HValue::kUint32)) {
4849 __ SmiTagCheckOverflow(output, input, at);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004850 DeoptimizeIf(lt, instr, "overflow", at, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004851 } else {
4852 __ SmiTag(output, input);
4853 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004854}
4855
4856
4857void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4858 Register scratch = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004859 Register input = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004860 Register result = ToRegister(instr->result());
4861 if (instr->needs_check()) {
4862 STATIC_ASSERT(kHeapObjectTag == 1);
4863 // If the input is a HeapObject, value of scratch won't be zero.
4864 __ And(scratch, input, Operand(kHeapObjectTag));
4865 __ SmiUntag(result, input);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004866 DeoptimizeIf(ne, instr, "not a Smi", scratch, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004867 } else {
4868 __ SmiUntag(result, input);
4869 }
4870}
4871
4872
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004873void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004874 DoubleRegister result_reg,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004875 NumberUntagDMode mode) {
4876 bool can_convert_undefined_to_nan =
4877 instr->hydrogen()->can_convert_undefined_to_nan();
4878 bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
4879
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004880 Register scratch = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004881 Label convert, load_smi, done;
4882 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4883 // Smi check.
4884 __ UntagAndJumpIfSmi(scratch, input_reg, &load_smi);
4885 // Heap number map check.
4886 __ lw(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
4887 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4888 if (can_convert_undefined_to_nan) {
4889 __ Branch(&convert, ne, scratch, Operand(at));
4890 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004891 DeoptimizeIf(ne, instr, "not a heap number", scratch, Operand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004892 }
4893 // Load heap number.
4894 __ ldc1(result_reg, FieldMemOperand(input_reg, HeapNumber::kValueOffset));
4895 if (deoptimize_on_minus_zero) {
4896 __ mfc1(at, result_reg.low());
4897 __ Branch(&done, ne, at, Operand(zero_reg));
4898 __ Mfhc1(scratch, result_reg);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004899 DeoptimizeIf(eq, instr, "minus zero", scratch,
4900 Operand(HeapNumber::kSignMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004901 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004902 __ Branch(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004903 if (can_convert_undefined_to_nan) {
4904 __ bind(&convert);
4905 // Convert undefined (and hole) to NaN.
4906 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004907 DeoptimizeIf(ne, instr, "not a heap number/undefined", input_reg,
4908 Operand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004909 __ LoadRoot(scratch, Heap::kNanValueRootIndex);
4910 __ ldc1(result_reg, FieldMemOperand(scratch, HeapNumber::kValueOffset));
4911 __ Branch(&done);
4912 }
4913 } else {
4914 __ SmiUntag(scratch, input_reg);
4915 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004916 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004917 // Smi to double register conversion
4918 __ bind(&load_smi);
4919 // scratch: untagged value of input_reg
4920 __ mtc1(scratch, result_reg);
4921 __ cvt_d_w(result_reg, result_reg);
4922 __ bind(&done);
4923}
4924
4925
4926void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004927 Register input_reg = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004928 Register scratch1 = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004929 Register scratch2 = ToRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004930 DoubleRegister double_scratch = double_scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004931 DoubleRegister double_scratch2 = ToDoubleRegister(instr->temp2());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004932
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004933 DCHECK(!scratch1.is(input_reg) && !scratch1.is(scratch2));
4934 DCHECK(!scratch2.is(input_reg) && !scratch2.is(scratch1));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004935
4936 Label done;
4937
4938 // The input is a tagged HeapObject.
4939 // Heap number map check.
4940 __ lw(scratch1, FieldMemOperand(input_reg, HeapObject::kMapOffset));
4941 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4942 // This 'at' value and scratch1 map value are used for tests in both clauses
4943 // of the if.
4944
4945 if (instr->truncating()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004946 // Performs a truncating conversion of a floating point number as used by
4947 // the JS bitwise operations.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004948 Label no_heap_number, check_bools, check_false;
4949 // Check HeapNumber map.
4950 __ Branch(USE_DELAY_SLOT, &no_heap_number, ne, scratch1, Operand(at));
4951 __ mov(scratch2, input_reg); // In delay slot.
4952 __ TruncateHeapNumberToI(input_reg, scratch2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004953 __ Branch(&done);
4954
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004955 // Check for Oddballs. Undefined/False is converted to zero and True to one
4956 // for truncating conversions.
4957 __ bind(&no_heap_number);
4958 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4959 __ Branch(&check_bools, ne, input_reg, Operand(at));
4960 DCHECK(ToRegister(instr->result()).is(input_reg));
4961 __ Branch(USE_DELAY_SLOT, &done);
4962 __ mov(input_reg, zero_reg); // In delay slot.
4963
4964 __ bind(&check_bools);
4965 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4966 __ Branch(&check_false, ne, scratch2, Operand(at));
4967 __ Branch(USE_DELAY_SLOT, &done);
4968 __ li(input_reg, Operand(1)); // In delay slot.
4969
4970 __ bind(&check_false);
4971 __ LoadRoot(at, Heap::kFalseValueRootIndex);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004972 DeoptimizeIf(ne, instr, "not a heap number/undefined/true/false", scratch2,
4973 Operand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004974 __ Branch(USE_DELAY_SLOT, &done);
4975 __ mov(input_reg, zero_reg); // In delay slot.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004976 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004977 DeoptimizeIf(ne, instr, "not a heap number", scratch1, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004978
4979 // Load the double value.
4980 __ ldc1(double_scratch,
4981 FieldMemOperand(input_reg, HeapNumber::kValueOffset));
4982
4983 Register except_flag = scratch2;
4984 __ EmitFPUTruncate(kRoundToZero,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004985 input_reg,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004986 double_scratch,
4987 scratch1,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004988 double_scratch2,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004989 except_flag,
4990 kCheckForInexactConversion);
4991
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004992 DeoptimizeIf(ne, instr, "lost precision or NaN", except_flag,
4993 Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004994
4995 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
4996 __ Branch(&done, ne, input_reg, Operand(zero_reg));
4997
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004998 __ Mfhc1(scratch1, double_scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004999 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005000 DeoptimizeIf(ne, instr, "minus zero", scratch1, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005001 }
5002 }
5003 __ bind(&done);
5004}
5005
5006
5007void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005008 class DeferredTaggedToI FINAL : public LDeferredCode {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005009 public:
5010 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
5011 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005012 void Generate() OVERRIDE { codegen()->DoDeferredTaggedToI(instr_); }
5013 LInstruction* instr() OVERRIDE { return instr_; }
5014
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005015 private:
5016 LTaggedToI* instr_;
5017 };
5018
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005019 LOperand* input = instr->value();
5020 DCHECK(input->IsRegister());
5021 DCHECK(input->Equals(instr->result()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005022
5023 Register input_reg = ToRegister(input);
5024
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005025 if (instr->hydrogen()->value()->representation().IsSmi()) {
5026 __ SmiUntag(input_reg);
5027 } else {
5028 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005029
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005030 // Let the deferred code handle the HeapObject case.
5031 __ JumpIfNotSmi(input_reg, deferred->entry());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005032
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005033 // Smi to int32 conversion.
5034 __ SmiUntag(input_reg);
5035 __ bind(deferred->exit());
5036 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005037}
5038
5039
5040void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005041 LOperand* input = instr->value();
5042 DCHECK(input->IsRegister());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005043 LOperand* result = instr->result();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005044 DCHECK(result->IsDoubleRegister());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005045
5046 Register input_reg = ToRegister(input);
5047 DoubleRegister result_reg = ToDoubleRegister(result);
5048
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005049 HValue* value = instr->hydrogen()->value();
5050 NumberUntagDMode mode = value->representation().IsSmi()
5051 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
5052
5053 EmitNumberUntagD(instr, input_reg, result_reg, mode);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005054}
5055
5056
5057void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
5058 Register result_reg = ToRegister(instr->result());
5059 Register scratch1 = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005060 DoubleRegister double_input = ToDoubleRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005061
5062 if (instr->truncating()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005063 __ TruncateDoubleToI(result_reg, double_input);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005064 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005065 Register except_flag = LCodeGen::scratch1();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005066
5067 __ EmitFPUTruncate(kRoundToMinusInf,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005068 result_reg,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005069 double_input,
5070 scratch1,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005071 double_scratch0(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005072 except_flag,
5073 kCheckForInexactConversion);
5074
5075 // Deopt if the operation did not succeed (except_flag != 0).
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005076 DeoptimizeIf(ne, instr, "lost precision or NaN", except_flag,
5077 Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005078
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005079 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5080 Label done;
5081 __ Branch(&done, ne, result_reg, Operand(zero_reg));
5082 __ Mfhc1(scratch1, double_input);
5083 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005084 DeoptimizeIf(ne, instr, "minus zero", scratch1, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005085 __ bind(&done);
5086 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005087 }
5088}
5089
5090
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005091void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
5092 Register result_reg = ToRegister(instr->result());
5093 Register scratch1 = LCodeGen::scratch0();
5094 DoubleRegister double_input = ToDoubleRegister(instr->value());
5095
5096 if (instr->truncating()) {
5097 __ TruncateDoubleToI(result_reg, double_input);
5098 } else {
5099 Register except_flag = LCodeGen::scratch1();
5100
5101 __ EmitFPUTruncate(kRoundToMinusInf,
5102 result_reg,
5103 double_input,
5104 scratch1,
5105 double_scratch0(),
5106 except_flag,
5107 kCheckForInexactConversion);
5108
5109 // Deopt if the operation did not succeed (except_flag != 0).
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005110 DeoptimizeIf(ne, instr, "lost precision or NaN", except_flag,
5111 Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005112
5113 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5114 Label done;
5115 __ Branch(&done, ne, result_reg, Operand(zero_reg));
5116 __ Mfhc1(scratch1, double_input);
5117 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005118 DeoptimizeIf(ne, instr, "minus zero", scratch1, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005119 __ bind(&done);
5120 }
5121 }
5122 __ SmiTagCheckOverflow(result_reg, result_reg, scratch1);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005123 DeoptimizeIf(lt, instr, "overflow", scratch1, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005124}
5125
5126
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005127void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005128 LOperand* input = instr->value();
5129 __ SmiTst(ToRegister(input), at);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005130 DeoptimizeIf(ne, instr, "not a Smi", at, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005131}
5132
5133
5134void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005135 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
5136 LOperand* input = instr->value();
5137 __ SmiTst(ToRegister(input), at);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005138 DeoptimizeIf(eq, instr, "Smi", at, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005139 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005140}
5141
5142
5143void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005144 Register input = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005145 Register scratch = scratch0();
5146
5147 __ GetObjectType(input, scratch, scratch);
5148
5149 if (instr->hydrogen()->is_interval_check()) {
5150 InstanceType first;
5151 InstanceType last;
5152 instr->hydrogen()->GetCheckInterval(&first, &last);
5153
5154 // If there is only one type in the interval check for equality.
5155 if (first == last) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005156 DeoptimizeIf(ne, instr, "wrong instance type", scratch, Operand(first));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005157 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005158 DeoptimizeIf(lo, instr, "wrong instance type", scratch, Operand(first));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005159 // Omit check for the last type.
5160 if (last != LAST_TYPE) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005161 DeoptimizeIf(hi, instr, "wrong instance type", scratch, Operand(last));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005162 }
5163 }
5164 } else {
5165 uint8_t mask;
5166 uint8_t tag;
5167 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
5168
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005169 if (base::bits::IsPowerOfTwo32(mask)) {
5170 DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005171 __ And(at, scratch, mask);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005172 DeoptimizeIf(tag == 0 ? ne : eq, instr, "wrong instance type", at,
5173 Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005174 } else {
5175 __ And(scratch, scratch, Operand(mask));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005176 DeoptimizeIf(ne, instr, "wrong instance type", scratch, Operand(tag));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005177 }
5178 }
5179}
5180
5181
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005182void LCodeGen::DoCheckValue(LCheckValue* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005183 Register reg = ToRegister(instr->value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005184 Handle<HeapObject> object = instr->hydrogen()->object().handle();
5185 AllowDeferredHandleDereference smi_check;
5186 if (isolate()->heap()->InNewSpace(*object)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005187 Register reg = ToRegister(instr->value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005188 Handle<Cell> cell = isolate()->factory()->NewCell(object);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005189 __ li(at, Operand(Handle<Object>(cell)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005190 __ lw(at, FieldMemOperand(at, Cell::kValueOffset));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005191 DeoptimizeIf(ne, instr, "value mismatch", reg, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005192 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005193 DeoptimizeIf(ne, instr, "value mismatch", reg, Operand(object));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005194 }
5195}
5196
5197
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005198void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
5199 {
5200 PushSafepointRegistersScope scope(this);
5201 __ push(object);
5202 __ mov(cp, zero_reg);
5203 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
5204 RecordSafepointWithRegisters(
5205 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
5206 __ StoreToSafepointRegisterSlot(v0, scratch0());
5207 }
5208 __ SmiTst(scratch0(), at);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005209 DeoptimizeIf(eq, instr, "instance migration failed", at, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005210}
5211
5212
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005213void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
5214 class DeferredCheckMaps FINAL : public LDeferredCode {
5215 public:
5216 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
5217 : LDeferredCode(codegen), instr_(instr), object_(object) {
5218 SetExit(check_maps());
5219 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005220 void Generate() OVERRIDE {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005221 codegen()->DoDeferredInstanceMigration(instr_, object_);
5222 }
5223 Label* check_maps() { return &check_maps_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005224 LInstruction* instr() OVERRIDE { return instr_; }
5225
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005226 private:
5227 LCheckMaps* instr_;
5228 Label check_maps_;
5229 Register object_;
5230 };
5231
5232 if (instr->hydrogen()->IsStabilityCheck()) {
5233 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5234 for (int i = 0; i < maps->size(); ++i) {
5235 AddStabilityDependency(maps->at(i).handle());
5236 }
5237 return;
5238 }
5239
5240 Register map_reg = scratch0();
5241 LOperand* input = instr->value();
5242 DCHECK(input->IsRegister());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005243 Register reg = ToRegister(input);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005244 __ lw(map_reg, FieldMemOperand(reg, HeapObject::kMapOffset));
5245
5246 DeferredCheckMaps* deferred = NULL;
5247 if (instr->hydrogen()->HasMigrationTarget()) {
5248 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5249 __ bind(deferred->check_maps());
5250 }
5251
5252 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5253 Label success;
5254 for (int i = 0; i < maps->size() - 1; i++) {
5255 Handle<Map> map = maps->at(i).handle();
5256 __ CompareMapAndBranch(map_reg, map, &success, eq, &success);
5257 }
5258 Handle<Map> map = maps->at(maps->size() - 1).handle();
5259 // Do the CompareMap() directly within the Branch() and DeoptimizeIf().
5260 if (instr->hydrogen()->HasMigrationTarget()) {
5261 __ Branch(deferred->entry(), ne, map_reg, Operand(map));
5262 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005263 DeoptimizeIf(ne, instr, "wrong map", map_reg, Operand(map));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005264 }
5265
5266 __ bind(&success);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005267}
5268
5269
5270void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
5271 DoubleRegister value_reg = ToDoubleRegister(instr->unclamped());
5272 Register result_reg = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005273 DoubleRegister temp_reg = ToDoubleRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005274 __ ClampDoubleToUint8(result_reg, value_reg, temp_reg);
5275}
5276
5277
5278void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
5279 Register unclamped_reg = ToRegister(instr->unclamped());
5280 Register result_reg = ToRegister(instr->result());
5281 __ ClampUint8(result_reg, unclamped_reg);
5282}
5283
5284
5285void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
5286 Register scratch = scratch0();
5287 Register input_reg = ToRegister(instr->unclamped());
5288 Register result_reg = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005289 DoubleRegister temp_reg = ToDoubleRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005290 Label is_smi, done, heap_number;
5291
5292 // Both smi and heap number cases are handled.
5293 __ UntagAndJumpIfSmi(scratch, input_reg, &is_smi);
5294
5295 // Check for heap number
5296 __ lw(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
5297 __ Branch(&heap_number, eq, scratch, Operand(factory()->heap_number_map()));
5298
5299 // Check for undefined. Undefined is converted to zero for clamping
5300 // conversions.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005301 DeoptimizeIf(ne, instr, "not a heap number/undefined", input_reg,
5302 Operand(factory()->undefined_value()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005303 __ mov(result_reg, zero_reg);
5304 __ jmp(&done);
5305
5306 // Heap number
5307 __ bind(&heap_number);
5308 __ ldc1(double_scratch0(), FieldMemOperand(input_reg,
5309 HeapNumber::kValueOffset));
5310 __ ClampDoubleToUint8(result_reg, double_scratch0(), temp_reg);
5311 __ jmp(&done);
5312
5313 __ bind(&is_smi);
5314 __ ClampUint8(result_reg, scratch);
5315
5316 __ bind(&done);
5317}
5318
5319
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005320void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5321 DoubleRegister value_reg = ToDoubleRegister(instr->value());
5322 Register result_reg = ToRegister(instr->result());
5323 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5324 __ FmoveHigh(result_reg, value_reg);
5325 } else {
5326 __ FmoveLow(result_reg, value_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005327 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005328}
5329
5330
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005331void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5332 Register hi_reg = ToRegister(instr->hi());
5333 Register lo_reg = ToRegister(instr->lo());
5334 DoubleRegister result_reg = ToDoubleRegister(instr->result());
5335 __ Move(result_reg, lo_reg, hi_reg);
5336}
5337
5338
5339void LCodeGen::DoAllocate(LAllocate* instr) {
5340 class DeferredAllocate FINAL : public LDeferredCode {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005341 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005342 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005343 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005344 void Generate() OVERRIDE { codegen()->DoDeferredAllocate(instr_); }
5345 LInstruction* instr() OVERRIDE { return instr_; }
5346
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005347 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005348 LAllocate* instr_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005349 };
5350
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005351 DeferredAllocate* deferred =
5352 new(zone()) DeferredAllocate(this, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005353
5354 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005355 Register scratch = ToRegister(instr->temp1());
5356 Register scratch2 = ToRegister(instr->temp2());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005357
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005358 // Allocate memory for the object.
5359 AllocationFlags flags = TAG_OBJECT;
5360 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5361 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5362 }
5363 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5364 DCHECK(!instr->hydrogen()->IsOldDataSpaceAllocation());
5365 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5366 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_POINTER_SPACE);
5367 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5368 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5369 flags = static_cast<AllocationFlags>(flags | PRETENURE_OLD_DATA_SPACE);
5370 }
5371 if (instr->size()->IsConstantOperand()) {
5372 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5373 if (size <= Page::kMaxRegularHeapObjectSize) {
5374 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5375 } else {
5376 __ jmp(deferred->entry());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005377 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005378 } else {
5379 Register size = ToRegister(instr->size());
5380 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005381 }
5382
5383 __ bind(deferred->exit());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005384
5385 if (instr->hydrogen()->MustPrefillWithFiller()) {
5386 STATIC_ASSERT(kHeapObjectTag == 1);
5387 if (instr->size()->IsConstantOperand()) {
5388 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5389 __ li(scratch, Operand(size - kHeapObjectTag));
5390 } else {
5391 __ Subu(scratch, ToRegister(instr->size()), Operand(kHeapObjectTag));
5392 }
5393 __ li(scratch2, Operand(isolate()->factory()->one_pointer_filler_map()));
5394 Label loop;
5395 __ bind(&loop);
5396 __ Subu(scratch, scratch, Operand(kPointerSize));
5397 __ Addu(at, result, Operand(scratch));
5398 __ sw(scratch2, MemOperand(at));
5399 __ Branch(&loop, ge, scratch, Operand(zero_reg));
5400 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005401}
5402
5403
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005404void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005405 Register result = ToRegister(instr->result());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005406
5407 // TODO(3095996): Get rid of this. For now, we need to make the
5408 // result register contain a valid pointer because it is already
5409 // contained in the register pointer map.
5410 __ mov(result, zero_reg);
5411
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005412 PushSafepointRegistersScope scope(this);
5413 if (instr->size()->IsRegister()) {
5414 Register size = ToRegister(instr->size());
5415 DCHECK(!size.is(result));
5416 __ SmiTag(size);
5417 __ push(size);
5418 } else {
5419 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5420 if (size >= 0 && size <= Smi::kMaxValue) {
5421 __ Push(Smi::FromInt(size));
5422 } else {
5423 // We should never get here at runtime => abort
5424 __ stop("invalid allocation size");
5425 return;
5426 }
5427 }
5428
5429 int flags = AllocateDoubleAlignFlag::encode(
5430 instr->hydrogen()->MustAllocateDoubleAligned());
5431 if (instr->hydrogen()->IsOldPointerSpaceAllocation()) {
5432 DCHECK(!instr->hydrogen()->IsOldDataSpaceAllocation());
5433 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5434 flags = AllocateTargetSpace::update(flags, OLD_POINTER_SPACE);
5435 } else if (instr->hydrogen()->IsOldDataSpaceAllocation()) {
5436 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5437 flags = AllocateTargetSpace::update(flags, OLD_DATA_SPACE);
5438 } else {
5439 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5440 }
5441 __ Push(Smi::FromInt(flags));
5442
5443 CallRuntimeFromDeferred(
5444 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005445 __ StoreToSafepointRegisterSlot(v0, result);
5446}
5447
5448
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005449void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005450 DCHECK(ToRegister(instr->value()).is(a0));
5451 DCHECK(ToRegister(instr->result()).is(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005452 __ push(a0);
5453 CallRuntime(Runtime::kToFastProperties, 1, instr);
5454}
5455
5456
5457void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005458 DCHECK(ToRegister(instr->context()).is(cp));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005459 Label materialized;
5460 // Registers will be used as follows:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005461 // t3 = literals array.
5462 // a1 = regexp literal.
5463 // a0 = regexp literal clone.
5464 // a2 and t0-t2 are used as temporaries.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005465 int literal_offset =
5466 FixedArray::OffsetOfElementAt(instr->hydrogen()->literal_index());
5467 __ li(t3, instr->hydrogen()->literals());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005468 __ lw(a1, FieldMemOperand(t3, literal_offset));
5469 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5470 __ Branch(&materialized, ne, a1, Operand(at));
5471
5472 // Create regexp literal using runtime function
5473 // Result will be in v0.
5474 __ li(t2, Operand(Smi::FromInt(instr->hydrogen()->literal_index())));
5475 __ li(t1, Operand(instr->hydrogen()->pattern()));
5476 __ li(t0, Operand(instr->hydrogen()->flags()));
5477 __ Push(t3, t2, t1, t0);
5478 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
5479 __ mov(a1, v0);
5480
5481 __ bind(&materialized);
5482 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
5483 Label allocated, runtime_allocate;
5484
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005485 __ Allocate(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005486 __ jmp(&allocated);
5487
5488 __ bind(&runtime_allocate);
5489 __ li(a0, Operand(Smi::FromInt(size)));
5490 __ Push(a1, a0);
5491 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
5492 __ pop(a1);
5493
5494 __ bind(&allocated);
5495 // Copy the content into the newly allocated memory.
5496 // (Unroll copy loop once for better throughput).
5497 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
5498 __ lw(a3, FieldMemOperand(a1, i));
5499 __ lw(a2, FieldMemOperand(a1, i + kPointerSize));
5500 __ sw(a3, FieldMemOperand(v0, i));
5501 __ sw(a2, FieldMemOperand(v0, i + kPointerSize));
5502 }
5503 if ((size % (2 * kPointerSize)) != 0) {
5504 __ lw(a3, FieldMemOperand(a1, size - kPointerSize));
5505 __ sw(a3, FieldMemOperand(v0, size - kPointerSize));
5506 }
5507}
5508
5509
5510void LCodeGen::DoFunctionLiteral(LFunctionLiteral* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005511 DCHECK(ToRegister(instr->context()).is(cp));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005512 // Use the fast case closure allocation code that allocates in new
5513 // space for nested functions that don't need literals cloning.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005514 bool pretenure = instr->hydrogen()->pretenure();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005515 if (!pretenure && instr->hydrogen()->has_no_literals()) {
5516 FastNewClosureStub stub(isolate(), instr->hydrogen()->strict_mode(),
5517 instr->hydrogen()->kind());
5518 __ li(a2, Operand(instr->hydrogen()->shared_info()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005519 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5520 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005521 __ li(a2, Operand(instr->hydrogen()->shared_info()));
5522 __ li(a1, Operand(pretenure ? factory()->true_value()
5523 : factory()->false_value()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005524 __ Push(cp, a2, a1);
5525 CallRuntime(Runtime::kNewClosure, 3, instr);
5526 }
5527}
5528
5529
5530void LCodeGen::DoTypeof(LTypeof* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005531 DCHECK(ToRegister(instr->result()).is(v0));
5532 Register input = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005533 __ push(input);
5534 CallRuntime(Runtime::kTypeof, 1, instr);
5535}
5536
5537
5538void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005539 Register input = ToRegister(instr->value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005540
5541 Register cmp1 = no_reg;
5542 Operand cmp2 = Operand(no_reg);
5543
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005544 Condition final_branch_condition = EmitTypeofIs(instr->TrueLabel(chunk_),
5545 instr->FalseLabel(chunk_),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005546 input,
5547 instr->type_literal(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005548 &cmp1,
5549 &cmp2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005550
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005551 DCHECK(cmp1.is_valid());
5552 DCHECK(!cmp2.is_reg() || cmp2.rm().is_valid());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005553
5554 if (final_branch_condition != kNoCondition) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005555 EmitBranch(instr, final_branch_condition, cmp1, cmp2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005556 }
5557}
5558
5559
5560Condition LCodeGen::EmitTypeofIs(Label* true_label,
5561 Label* false_label,
5562 Register input,
5563 Handle<String> type_name,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005564 Register* cmp1,
5565 Operand* cmp2) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005566 // This function utilizes the delay slot heavily. This is used to load
5567 // values that are always usable without depending on the type of the input
5568 // register.
5569 Condition final_branch_condition = kNoCondition;
5570 Register scratch = scratch0();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005571 Factory* factory = isolate()->factory();
5572 if (String::Equals(type_name, factory->number_string())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005573 __ JumpIfSmi(input, true_label);
5574 __ lw(input, FieldMemOperand(input, HeapObject::kMapOffset));
5575 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005576 *cmp1 = input;
5577 *cmp2 = Operand(at);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005578 final_branch_condition = eq;
5579
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005580 } else if (String::Equals(type_name, factory->string_string())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005581 __ JumpIfSmi(input, false_label);
5582 __ GetObjectType(input, input, scratch);
5583 __ Branch(USE_DELAY_SLOT, false_label,
5584 ge, scratch, Operand(FIRST_NONSTRING_TYPE));
5585 // input is an object so we can load the BitFieldOffset even if we take the
5586 // other branch.
5587 __ lbu(at, FieldMemOperand(input, Map::kBitFieldOffset));
5588 __ And(at, at, 1 << Map::kIsUndetectable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005589 *cmp1 = at;
5590 *cmp2 = Operand(zero_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005591 final_branch_condition = eq;
5592
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005593 } else if (String::Equals(type_name, factory->symbol_string())) {
5594 __ JumpIfSmi(input, false_label);
5595 __ GetObjectType(input, input, scratch);
5596 *cmp1 = scratch;
5597 *cmp2 = Operand(SYMBOL_TYPE);
5598 final_branch_condition = eq;
5599
5600 } else if (String::Equals(type_name, factory->boolean_string())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005601 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5602 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5603 __ LoadRoot(at, Heap::kFalseValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005604 *cmp1 = at;
5605 *cmp2 = Operand(input);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005606 final_branch_condition = eq;
5607
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005608 } else if (String::Equals(type_name, factory->undefined_string())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005609 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5610 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5611 // The first instruction of JumpIfSmi is an And - it is safe in the delay
5612 // slot.
5613 __ JumpIfSmi(input, false_label);
5614 // Check for undetectable objects => true.
5615 __ lw(input, FieldMemOperand(input, HeapObject::kMapOffset));
5616 __ lbu(at, FieldMemOperand(input, Map::kBitFieldOffset));
5617 __ And(at, at, 1 << Map::kIsUndetectable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005618 *cmp1 = at;
5619 *cmp2 = Operand(zero_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005620 final_branch_condition = ne;
5621
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005622 } else if (String::Equals(type_name, factory->function_string())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005623 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5624 __ JumpIfSmi(input, false_label);
5625 __ GetObjectType(input, scratch, input);
5626 __ Branch(true_label, eq, input, Operand(JS_FUNCTION_TYPE));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005627 *cmp1 = input;
5628 *cmp2 = Operand(JS_FUNCTION_PROXY_TYPE);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005629 final_branch_condition = eq;
5630
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005631 } else if (String::Equals(type_name, factory->object_string())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005632 __ JumpIfSmi(input, false_label);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005633 __ LoadRoot(at, Heap::kNullValueRootIndex);
5634 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5635 Register map = input;
5636 __ GetObjectType(input, map, scratch);
5637 __ Branch(false_label,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005638 lt, scratch, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005639 __ Branch(USE_DELAY_SLOT, false_label,
5640 gt, scratch, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005641 // map is still valid, so the BitField can be loaded in delay slot.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005642 // Check for undetectable objects => false.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005643 __ lbu(at, FieldMemOperand(map, Map::kBitFieldOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005644 __ And(at, at, 1 << Map::kIsUndetectable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005645 *cmp1 = at;
5646 *cmp2 = Operand(zero_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005647 final_branch_condition = eq;
5648
5649 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005650 *cmp1 = at;
5651 *cmp2 = Operand(zero_reg); // Set to valid regs, to avoid caller assertion.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005652 __ Branch(false_label);
5653 }
5654
5655 return final_branch_condition;
5656}
5657
5658
5659void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005660 Register temp1 = ToRegister(instr->temp());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005661
5662 EmitIsConstructCall(temp1, scratch0());
5663
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005664 EmitBranch(instr, eq, temp1,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005665 Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
5666}
5667
5668
5669void LCodeGen::EmitIsConstructCall(Register temp1, Register temp2) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005670 DCHECK(!temp1.is(temp2));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005671 // Get the frame pointer for the calling frame.
5672 __ lw(temp1, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5673
5674 // Skip the arguments adaptor frame if it exists.
5675 Label check_frame_marker;
5676 __ lw(temp2, MemOperand(temp1, StandardFrameConstants::kContextOffset));
5677 __ Branch(&check_frame_marker, ne, temp2,
5678 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5679 __ lw(temp1, MemOperand(temp1, StandardFrameConstants::kCallerFPOffset));
5680
5681 // Check the marker in the calling frame.
5682 __ bind(&check_frame_marker);
5683 __ lw(temp1, MemOperand(temp1, StandardFrameConstants::kMarkerOffset));
5684}
5685
5686
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005687void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5688 if (!info()->IsStub()) {
5689 // Ensure that we have enough space after the previous lazy-bailout
5690 // instruction for patching the code here.
5691 int current_pc = masm()->pc_offset();
5692 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5693 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5694 DCHECK_EQ(0, padding_size % Assembler::kInstrSize);
5695 while (padding_size > 0) {
5696 __ nop();
5697 padding_size -= Assembler::kInstrSize;
5698 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005699 }
5700 }
5701 last_lazy_deopt_pc_ = masm()->pc_offset();
5702}
5703
5704
5705void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005706 last_lazy_deopt_pc_ = masm()->pc_offset();
5707 DCHECK(instr->HasEnvironment());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005708 LEnvironment* env = instr->environment();
5709 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5710 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5711}
5712
5713
5714void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005715 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5716 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5717 // needed return address), even though the implementation of LAZY and EAGER is
5718 // now identical. When LAZY is eventually completely folded into EAGER, remove
5719 // the special case below.
5720 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5721 type = Deoptimizer::LAZY;
5722 }
5723
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005724 DeoptimizeIf(al, instr, type, instr->hydrogen()->reason(), zero_reg,
5725 Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005726}
5727
5728
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005729void LCodeGen::DoDummy(LDummy* instr) {
5730 // Nothing to see here, move on!
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005731}
5732
5733
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005734void LCodeGen::DoDummyUse(LDummyUse* instr) {
5735 // Nothing to see here, move on!
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005736}
5737
5738
5739void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005740 PushSafepointRegistersScope scope(this);
5741 LoadContextFromDeferred(instr->context());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005742 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5743 RecordSafepointWithLazyDeopt(
5744 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005745 DCHECK(instr->HasEnvironment());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005746 LEnvironment* env = instr->environment();
5747 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5748}
5749
5750
5751void LCodeGen::DoStackCheck(LStackCheck* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005752 class DeferredStackCheck FINAL : public LDeferredCode {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005753 public:
5754 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5755 : LDeferredCode(codegen), instr_(instr) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005756 void Generate() OVERRIDE { codegen()->DoDeferredStackCheck(instr_); }
5757 LInstruction* instr() OVERRIDE { return instr_; }
5758
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005759 private:
5760 LStackCheck* instr_;
5761 };
5762
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005763 DCHECK(instr->HasEnvironment());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005764 LEnvironment* env = instr->environment();
5765 // There is no LLazyBailout instruction for stack-checks. We have to
5766 // prepare for lazy deoptimization explicitly here.
5767 if (instr->hydrogen()->is_function_entry()) {
5768 // Perform stack overflow check.
5769 Label done;
5770 __ LoadRoot(at, Heap::kStackLimitRootIndex);
5771 __ Branch(&done, hs, sp, Operand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005772 DCHECK(instr->context()->IsRegister());
5773 DCHECK(ToRegister(instr->context()).is(cp));
5774 CallCode(isolate()->builtins()->StackCheck(),
5775 RelocInfo::CODE_TARGET,
5776 instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005777 __ bind(&done);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005778 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005779 DCHECK(instr->hydrogen()->is_backwards_branch());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005780 // Perform stack overflow check if this goto needs it before jumping.
5781 DeferredStackCheck* deferred_stack_check =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005782 new(zone()) DeferredStackCheck(this, instr);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005783 __ LoadRoot(at, Heap::kStackLimitRootIndex);
5784 __ Branch(deferred_stack_check->entry(), lo, sp, Operand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005785 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005786 __ bind(instr->done_label());
5787 deferred_stack_check->SetExit(instr->done_label());
5788 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5789 // Don't record a deoptimization index for the safepoint here.
5790 // This will be done explicitly when emitting call and the safepoint in
5791 // the deferred code.
5792 }
5793}
5794
5795
5796void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
5797 // This is a pseudo-instruction that ensures that the environment here is
5798 // properly registered for deoptimization and records the assembler's PC
5799 // offset.
5800 LEnvironment* environment = instr->environment();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005801
5802 // If the environment were already registered, we would have no way of
5803 // backpatching it with the spill slot operands.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005804 DCHECK(!environment->HasBeenRegistered());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005805 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005806
5807 GenerateOsrPrologue();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005808}
5809
5810
5811void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
5812 Register result = ToRegister(instr->result());
5813 Register object = ToRegister(instr->object());
5814 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005815 DeoptimizeIf(eq, instr, "undefined", object, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005816
5817 Register null_value = t1;
5818 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005819 DeoptimizeIf(eq, instr, "null", object, Operand(null_value));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005820
5821 __ And(at, object, kSmiTagMask);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005822 DeoptimizeIf(eq, instr, "Smi", at, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005823
5824 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
5825 __ GetObjectType(object, a1, a1);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005826 DeoptimizeIf(le, instr, "not a JavaScript object", a1,
5827 Operand(LAST_JS_PROXY_TYPE));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005828
5829 Label use_cache, call_runtime;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005830 DCHECK(object.is(a0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005831 __ CheckEnumCache(null_value, &call_runtime);
5832
5833 __ lw(result, FieldMemOperand(object, HeapObject::kMapOffset));
5834 __ Branch(&use_cache);
5835
5836 // Get the set of properties to enumerate.
5837 __ bind(&call_runtime);
5838 __ push(object);
5839 CallRuntime(Runtime::kGetPropertyNamesFast, 1, instr);
5840
5841 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005842 DCHECK(result.is(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005843 __ LoadRoot(at, Heap::kMetaMapRootIndex);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005844 DeoptimizeIf(ne, instr, "wrong map", a1, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005845 __ bind(&use_cache);
5846}
5847
5848
5849void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5850 Register map = ToRegister(instr->map());
5851 Register result = ToRegister(instr->result());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005852 Label load_cache, done;
5853 __ EnumLength(result, map);
5854 __ Branch(&load_cache, ne, result, Operand(Smi::FromInt(0)));
5855 __ li(result, Operand(isolate()->factory()->empty_fixed_array()));
5856 __ jmp(&done);
5857
5858 __ bind(&load_cache);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005859 __ LoadInstanceDescriptors(map, result);
5860 __ lw(result,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005861 FieldMemOperand(result, DescriptorArray::kEnumCacheOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005862 __ lw(result,
5863 FieldMemOperand(result, FixedArray::SizeFor(instr->idx())));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005864 DeoptimizeIf(eq, instr, "no cache", result, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005865
5866 __ bind(&done);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005867}
5868
5869
5870void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5871 Register object = ToRegister(instr->value());
5872 Register map = ToRegister(instr->map());
5873 __ lw(scratch0(), FieldMemOperand(object, HeapObject::kMapOffset));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005874 DeoptimizeIf(ne, instr, "wrong map", map, Operand(scratch0()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005875}
5876
5877
5878void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5879 Register result,
5880 Register object,
5881 Register index) {
5882 PushSafepointRegistersScope scope(this);
5883 __ Push(object, index);
5884 __ mov(cp, zero_reg);
5885 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
5886 RecordSafepointWithRegisters(
5887 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
5888 __ StoreToSafepointRegisterSlot(v0, result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005889}
5890
5891
5892void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005893 class DeferredLoadMutableDouble FINAL : public LDeferredCode {
5894 public:
5895 DeferredLoadMutableDouble(LCodeGen* codegen,
5896 LLoadFieldByIndex* instr,
5897 Register result,
5898 Register object,
5899 Register index)
5900 : LDeferredCode(codegen),
5901 instr_(instr),
5902 result_(result),
5903 object_(object),
5904 index_(index) {
5905 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005906 void Generate() OVERRIDE {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005907 codegen()->DoDeferredLoadMutableDouble(instr_, result_, object_, index_);
5908 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005909 LInstruction* instr() OVERRIDE { return instr_; }
5910
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005911 private:
5912 LLoadFieldByIndex* instr_;
5913 Register result_;
5914 Register object_;
5915 Register index_;
5916 };
5917
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005918 Register object = ToRegister(instr->object());
5919 Register index = ToRegister(instr->index());
5920 Register result = ToRegister(instr->result());
5921 Register scratch = scratch0();
5922
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005923 DeferredLoadMutableDouble* deferred;
5924 deferred = new(zone()) DeferredLoadMutableDouble(
5925 this, instr, result, object, index);
5926
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005927 Label out_of_object, done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005928
5929 __ And(scratch, index, Operand(Smi::FromInt(1)));
5930 __ Branch(deferred->entry(), ne, scratch, Operand(zero_reg));
5931 __ sra(index, index, 1);
5932
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005933 __ Branch(USE_DELAY_SLOT, &out_of_object, lt, index, Operand(zero_reg));
5934 __ sll(scratch, index, kPointerSizeLog2 - kSmiTagSize); // In delay slot.
5935
5936 STATIC_ASSERT(kPointerSizeLog2 > kSmiTagSize);
5937 __ Addu(scratch, object, scratch);
5938 __ lw(result, FieldMemOperand(scratch, JSObject::kHeaderSize));
5939
5940 __ Branch(&done);
5941
5942 __ bind(&out_of_object);
5943 __ lw(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
5944 // Index is equal to negated out of object property index plus 1.
5945 __ Subu(scratch, result, scratch);
5946 __ lw(result, FieldMemOperand(scratch,
5947 FixedArray::kHeaderSize - kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005948 __ bind(deferred->exit());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005949 __ bind(&done);
5950}
5951
5952
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005953void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
5954 Register context = ToRegister(instr->context());
5955 __ sw(context, MemOperand(fp, StandardFrameConstants::kContextOffset));
5956}
5957
5958
5959void LCodeGen::DoAllocateBlockContext(LAllocateBlockContext* instr) {
5960 Handle<ScopeInfo> scope_info = instr->scope_info();
5961 __ li(at, scope_info);
5962 __ Push(at, ToRegister(instr->function()));
5963 CallRuntime(Runtime::kPushBlockContext, 2, instr);
5964 RecordSafepoint(Safepoint::kNoLazyDeopt);
5965}
5966
5967
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005968#undef __
5969
5970} } // namespace v8::internal