blob: 8febb573b179cc25ae8a5507cf0ab97b9326eb62 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2012 the V8 project authors. All rights reserved.7
2// 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
28#include "src/crankshaft/mips/lithium-codegen-mips.h"
29
30#include "src/base/bits.h"
31#include "src/code-factory.h"
32#include "src/code-stubs.h"
33#include "src/crankshaft/hydrogen-osr.h"
34#include "src/crankshaft/mips/lithium-gap-resolver-mips.h"
35#include "src/ic/ic.h"
36#include "src/ic/stub-cache.h"
37#include "src/profiler/cpu-profiler.h"
38
39
40namespace v8 {
41namespace internal {
42
43
44class SafepointGenerator final : public CallWrapper {
45 public:
46 SafepointGenerator(LCodeGen* codegen,
47 LPointerMap* pointers,
48 Safepoint::DeoptMode mode)
49 : codegen_(codegen),
50 pointers_(pointers),
51 deopt_mode_(mode) { }
52 virtual ~SafepointGenerator() {}
53
54 void BeforeCall(int call_size) const override {}
55
56 void AfterCall() const override {
57 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() {
70 LPhase phase("Z_Code generation", chunk());
71 DCHECK(is_unused());
72 status_ = GENERATING;
73
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
79 return GeneratePrologue() && GenerateBody() && GenerateDeferredCode() &&
80 GenerateJumpTable() && GenerateSafepointTable();
81}
82
83
84void LCodeGen::FinishCode(Handle<Code> code) {
85 DCHECK(is_done());
Ben Murdoch097c5b22016-05-18 11:27:45 +010086 code->set_stack_slots(GetTotalFrameSlotCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000087 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
88 PopulateDeoptimizationData(code);
89}
90
91
92void LCodeGen::SaveCallerDoubles() {
93 DCHECK(info()->saves_caller_doubles());
94 DCHECK(NeedsEagerFrame());
95 Comment(";;; Save clobbered callee double registers");
96 int count = 0;
97 BitVector* doubles = chunk()->allocated_double_registers();
98 BitVector::Iterator save_iterator(doubles);
99 while (!save_iterator.Done()) {
100 __ sdc1(DoubleRegister::from_code(save_iterator.Current()),
101 MemOperand(sp, count * kDoubleSize));
102 save_iterator.Advance();
103 count++;
104 }
105}
106
107
108void LCodeGen::RestoreCallerDoubles() {
109 DCHECK(info()->saves_caller_doubles());
110 DCHECK(NeedsEagerFrame());
111 Comment(";;; Restore clobbered callee double registers");
112 BitVector* doubles = chunk()->allocated_double_registers();
113 BitVector::Iterator save_iterator(doubles);
114 int count = 0;
115 while (!save_iterator.Done()) {
116 __ ldc1(DoubleRegister::from_code(save_iterator.Current()),
117 MemOperand(sp, count * kDoubleSize));
118 save_iterator.Advance();
119 count++;
120 }
121}
122
123
124bool LCodeGen::GeneratePrologue() {
125 DCHECK(is_generating());
126
127 if (info()->IsOptimizing()) {
128 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
129
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000130 // a1: Callee's JS function.
131 // cp: Callee's context.
132 // fp: Caller's frame pointer.
133 // lr: Caller's pc.
134 }
135
136 info()->set_prologue_offset(masm_->pc_offset());
137 if (NeedsEagerFrame()) {
138 if (info()->IsStub()) {
139 __ StubPrologue();
140 } else {
141 __ Prologue(info()->GeneratePreagedPrologue());
142 }
143 frame_is_built_ = true;
144 }
145
146 // Reserve space for the stack slots needed by the code.
147 int slots = GetStackSlotCount();
148 if (slots > 0) {
149 if (FLAG_debug_code) {
150 __ Subu(sp, sp, Operand(slots * kPointerSize));
151 __ Push(a0, a1);
152 __ Addu(a0, sp, Operand(slots * kPointerSize));
153 __ li(a1, Operand(kSlotsZapValue));
154 Label loop;
155 __ bind(&loop);
156 __ Subu(a0, a0, Operand(kPointerSize));
157 __ sw(a1, MemOperand(a0, 2 * kPointerSize));
158 __ Branch(&loop, ne, a0, Operand(sp));
159 __ Pop(a0, a1);
160 } else {
161 __ Subu(sp, sp, Operand(slots * kPointerSize));
162 }
163 }
164
165 if (info()->saves_caller_doubles()) {
166 SaveCallerDoubles();
167 }
168 return !is_aborted();
169}
170
171
172void LCodeGen::DoPrologue(LPrologue* instr) {
173 Comment(";;; Prologue begin");
174
175 // Possibly allocate a local context.
176 if (info()->scope()->num_heap_slots() > 0) {
177 Comment(";;; Allocate local context");
178 bool need_write_barrier = true;
179 // Argument to NewContext is the function, which is in a1.
180 int slots = info()->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
181 Safepoint::DeoptMode deopt_mode = Safepoint::kNoLazyDeopt;
182 if (info()->scope()->is_script_scope()) {
183 __ push(a1);
184 __ Push(info()->scope()->GetScopeInfo(info()->isolate()));
185 __ CallRuntime(Runtime::kNewScriptContext);
186 deopt_mode = Safepoint::kLazyDeopt;
187 } else if (slots <= FastNewContextStub::kMaximumSlots) {
188 FastNewContextStub stub(isolate(), slots);
189 __ CallStub(&stub);
190 // Result of FastNewContextStub is always in new space.
191 need_write_barrier = false;
192 } else {
193 __ push(a1);
194 __ CallRuntime(Runtime::kNewFunctionContext);
195 }
196 RecordSafepoint(deopt_mode);
197
198 // Context is returned in both v0. It replaces the context passed to us.
199 // It's saved in the stack and kept live in cp.
200 __ mov(cp, v0);
201 __ sw(v0, MemOperand(fp, StandardFrameConstants::kContextOffset));
202 // Copy any necessary parameters into the context.
203 int num_parameters = scope()->num_parameters();
204 int first_parameter = scope()->has_this_declaration() ? -1 : 0;
205 for (int i = first_parameter; i < num_parameters; i++) {
206 Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
207 if (var->IsContextSlot()) {
208 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
209 (num_parameters - 1 - i) * kPointerSize;
210 // Load parameter from stack.
211 __ lw(a0, MemOperand(fp, parameter_offset));
212 // Store it in the context.
213 MemOperand target = ContextMemOperand(cp, var->index());
214 __ sw(a0, target);
215 // Update the write barrier. This clobbers a3 and a0.
216 if (need_write_barrier) {
217 __ RecordWriteContextSlot(
218 cp, target.offset(), a0, a3, GetRAState(), kSaveFPRegs);
219 } else if (FLAG_debug_code) {
220 Label done;
221 __ JumpIfInNewSpace(cp, a0, &done);
222 __ Abort(kExpectedNewSpaceObject);
223 __ bind(&done);
224 }
225 }
226 }
227 Comment(";;; End allocate local context");
228 }
229
230 Comment(";;; Prologue end");
231}
232
233
234void LCodeGen::GenerateOsrPrologue() {
235 // Generate the OSR entry prologue at the first unknown OSR value, or if there
236 // are none, at the OSR entrypoint instruction.
237 if (osr_pc_offset_ >= 0) return;
238
239 osr_pc_offset_ = masm()->pc_offset();
240
241 // Adjust the frame size, subsuming the unoptimized frame into the
242 // optimized frame.
243 int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
244 DCHECK(slots >= 0);
245 __ Subu(sp, sp, Operand(slots * kPointerSize));
246}
247
248
249void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
250 if (instr->IsCall()) {
251 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
252 }
253 if (!instr->IsLazyBailout() && !instr->IsGap()) {
254 safepoints_.BumpLastLazySafepointIndex();
255 }
256}
257
258
259bool LCodeGen::GenerateDeferredCode() {
260 DCHECK(is_generating());
261 if (deferred_.length() > 0) {
262 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
263 LDeferredCode* code = deferred_[i];
264
265 HValue* value =
266 instructions_->at(code->instruction_index())->hydrogen_value();
267 RecordAndWritePosition(
268 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
269
270 Comment(";;; <@%d,#%d> "
271 "-------------------- Deferred %s --------------------",
272 code->instruction_index(),
273 code->instr()->hydrogen_value()->id(),
274 code->instr()->Mnemonic());
275 __ bind(code->entry());
276 if (NeedsDeferredFrame()) {
277 Comment(";;; Build frame");
278 DCHECK(!frame_is_built_);
279 DCHECK(info()->IsStub());
280 frame_is_built_ = true;
281 __ MultiPush(cp.bit() | fp.bit() | ra.bit());
282 __ li(scratch0(), Operand(Smi::FromInt(StackFrame::STUB)));
283 __ push(scratch0());
284 __ Addu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
285 Comment(";;; Deferred code");
286 }
287 code->Generate();
288 if (NeedsDeferredFrame()) {
289 Comment(";;; Destroy frame");
290 DCHECK(frame_is_built_);
291 __ pop(at);
292 __ MultiPop(cp.bit() | fp.bit() | ra.bit());
293 frame_is_built_ = false;
294 }
295 __ jmp(code->exit());
296 }
297 }
298 // Deferred code is the last part of the instruction sequence. Mark
299 // the generated code as done unless we bailed out.
300 if (!is_aborted()) status_ = DONE;
301 return !is_aborted();
302}
303
304
305bool LCodeGen::GenerateJumpTable() {
306 if (jump_table_.length() > 0) {
307 Label needs_frame, call_deopt_entry;
308
309 Comment(";;; -------------------- Jump table --------------------");
310 Address base = jump_table_[0].address;
311
312 Register entry_offset = t9;
313
314 int length = jump_table_.length();
315 for (int i = 0; i < length; i++) {
316 Deoptimizer::JumpTableEntry* table_entry = &jump_table_[i];
317 __ bind(&table_entry->label);
318
319 DCHECK(table_entry->bailout_type == jump_table_[0].bailout_type);
320 Address entry = table_entry->address;
321 DeoptComment(table_entry->deopt_info);
322
323 // Second-level deopt table entries are contiguous and small, so instead
324 // of loading the full, absolute address of each one, load an immediate
325 // offset which will be added to the base address later.
326 __ li(entry_offset, Operand(entry - base));
327
328 if (table_entry->needs_frame) {
329 DCHECK(!info()->saves_caller_doubles());
330 Comment(";;; call deopt with frame");
331 __ MultiPush(cp.bit() | fp.bit() | ra.bit());
332 __ Call(&needs_frame);
333 } else {
334 __ Call(&call_deopt_entry);
335 }
336 info()->LogDeoptCallPosition(masm()->pc_offset(),
337 table_entry->deopt_info.inlining_id);
338 }
339
340 if (needs_frame.is_linked()) {
341 __ bind(&needs_frame);
342 // This variant of deopt can only be used with stubs. Since we don't
343 // have a function pointer to install in the stack frame that we're
344 // building, install a special marker there instead.
345 DCHECK(info()->IsStub());
346 __ li(at, Operand(Smi::FromInt(StackFrame::STUB)));
347 __ push(at);
348 __ Addu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
349 }
350
351 Comment(";;; call deopt");
352 __ bind(&call_deopt_entry);
353
354 if (info()->saves_caller_doubles()) {
355 DCHECK(info()->IsStub());
356 RestoreCallerDoubles();
357 }
358
359 // Add the base address to the offset previously loaded in entry_offset.
360 __ Addu(entry_offset, entry_offset,
361 Operand(ExternalReference::ForDeoptEntry(base)));
362 __ Jump(entry_offset);
363 }
364 __ RecordComment("]");
365
366 // The deoptimization jump table is the last part of the instruction
367 // sequence. Mark the generated code as done unless we bailed out.
368 if (!is_aborted()) status_ = DONE;
369 return !is_aborted();
370}
371
372
373bool LCodeGen::GenerateSafepointTable() {
374 DCHECK(is_done());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100375 safepoints_.Emit(masm(), GetTotalFrameSlotCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000376 return !is_aborted();
377}
378
379
380Register LCodeGen::ToRegister(int index) const {
381 return Register::from_code(index);
382}
383
384
385DoubleRegister LCodeGen::ToDoubleRegister(int index) const {
386 return DoubleRegister::from_code(index);
387}
388
389
390Register LCodeGen::ToRegister(LOperand* op) const {
391 DCHECK(op->IsRegister());
392 return ToRegister(op->index());
393}
394
395
396Register LCodeGen::EmitLoadRegister(LOperand* op, Register scratch) {
397 if (op->IsRegister()) {
398 return ToRegister(op->index());
399 } else if (op->IsConstantOperand()) {
400 LConstantOperand* const_op = LConstantOperand::cast(op);
401 HConstant* constant = chunk_->LookupConstant(const_op);
402 Handle<Object> literal = constant->handle(isolate());
403 Representation r = chunk_->LookupLiteralRepresentation(const_op);
404 if (r.IsInteger32()) {
405 AllowDeferredHandleDereference get_number;
406 DCHECK(literal->IsNumber());
407 __ li(scratch, Operand(static_cast<int32_t>(literal->Number())));
408 } else if (r.IsSmi()) {
409 DCHECK(constant->HasSmiValue());
410 __ li(scratch, Operand(Smi::FromInt(constant->Integer32Value())));
411 } else if (r.IsDouble()) {
412 Abort(kEmitLoadRegisterUnsupportedDoubleImmediate);
413 } else {
414 DCHECK(r.IsSmiOrTagged());
415 __ li(scratch, literal);
416 }
417 return scratch;
418 } else if (op->IsStackSlot()) {
419 __ lw(scratch, ToMemOperand(op));
420 return scratch;
421 }
422 UNREACHABLE();
423 return scratch;
424}
425
426
427DoubleRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
428 DCHECK(op->IsDoubleRegister());
429 return ToDoubleRegister(op->index());
430}
431
432
433DoubleRegister LCodeGen::EmitLoadDoubleRegister(LOperand* op,
434 FloatRegister flt_scratch,
435 DoubleRegister dbl_scratch) {
436 if (op->IsDoubleRegister()) {
437 return ToDoubleRegister(op->index());
438 } else if (op->IsConstantOperand()) {
439 LConstantOperand* const_op = LConstantOperand::cast(op);
440 HConstant* constant = chunk_->LookupConstant(const_op);
441 Handle<Object> literal = constant->handle(isolate());
442 Representation r = chunk_->LookupLiteralRepresentation(const_op);
443 if (r.IsInteger32()) {
444 DCHECK(literal->IsNumber());
445 __ li(at, Operand(static_cast<int32_t>(literal->Number())));
446 __ mtc1(at, flt_scratch);
447 __ cvt_d_w(dbl_scratch, flt_scratch);
448 return dbl_scratch;
449 } else if (r.IsDouble()) {
450 Abort(kUnsupportedDoubleImmediate);
451 } else if (r.IsTagged()) {
452 Abort(kUnsupportedTaggedImmediate);
453 }
454 } else if (op->IsStackSlot()) {
455 MemOperand mem_op = ToMemOperand(op);
456 __ ldc1(dbl_scratch, mem_op);
457 return dbl_scratch;
458 }
459 UNREACHABLE();
460 return dbl_scratch;
461}
462
463
464Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
465 HConstant* constant = chunk_->LookupConstant(op);
466 DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
467 return constant->handle(isolate());
468}
469
470
471bool LCodeGen::IsInteger32(LConstantOperand* op) const {
472 return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
473}
474
475
476bool LCodeGen::IsSmi(LConstantOperand* op) const {
477 return chunk_->LookupLiteralRepresentation(op).IsSmi();
478}
479
480
481int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
482 return ToRepresentation(op, Representation::Integer32());
483}
484
485
486int32_t LCodeGen::ToRepresentation(LConstantOperand* op,
487 const Representation& r) const {
488 HConstant* constant = chunk_->LookupConstant(op);
489 int32_t value = constant->Integer32Value();
490 if (r.IsInteger32()) return value;
491 DCHECK(r.IsSmiOrTagged());
492 return reinterpret_cast<int32_t>(Smi::FromInt(value));
493}
494
495
496Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
497 HConstant* constant = chunk_->LookupConstant(op);
498 return Smi::FromInt(constant->Integer32Value());
499}
500
501
502double LCodeGen::ToDouble(LConstantOperand* op) const {
503 HConstant* constant = chunk_->LookupConstant(op);
504 DCHECK(constant->HasDoubleValue());
505 return constant->DoubleValue();
506}
507
508
509Operand LCodeGen::ToOperand(LOperand* op) {
510 if (op->IsConstantOperand()) {
511 LConstantOperand* const_op = LConstantOperand::cast(op);
512 HConstant* constant = chunk()->LookupConstant(const_op);
513 Representation r = chunk_->LookupLiteralRepresentation(const_op);
514 if (r.IsSmi()) {
515 DCHECK(constant->HasSmiValue());
516 return Operand(Smi::FromInt(constant->Integer32Value()));
517 } else if (r.IsInteger32()) {
518 DCHECK(constant->HasInteger32Value());
519 return Operand(constant->Integer32Value());
520 } else if (r.IsDouble()) {
521 Abort(kToOperandUnsupportedDoubleImmediate);
522 }
523 DCHECK(r.IsTagged());
524 return Operand(constant->handle(isolate()));
525 } else if (op->IsRegister()) {
526 return Operand(ToRegister(op));
527 } else if (op->IsDoubleRegister()) {
528 Abort(kToOperandIsDoubleRegisterUnimplemented);
529 return Operand(0);
530 }
531 // Stack slots not implemented, use ToMemOperand instead.
532 UNREACHABLE();
533 return Operand(0);
534}
535
536
537static int ArgumentsOffsetWithoutFrame(int index) {
538 DCHECK(index < 0);
539 return -(index + 1) * kPointerSize;
540}
541
542
543MemOperand LCodeGen::ToMemOperand(LOperand* op) const {
544 DCHECK(!op->IsRegister());
545 DCHECK(!op->IsDoubleRegister());
546 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
547 if (NeedsEagerFrame()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100548 return MemOperand(fp, FrameSlotToFPOffset(op->index()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000549 } else {
550 // Retrieve parameter without eager stack-frame relative to the
551 // stack-pointer.
552 return MemOperand(sp, ArgumentsOffsetWithoutFrame(op->index()));
553 }
554}
555
556
557MemOperand LCodeGen::ToHighMemOperand(LOperand* op) const {
558 DCHECK(op->IsDoubleStackSlot());
559 if (NeedsEagerFrame()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100560 return MemOperand(fp, FrameSlotToFPOffset(op->index()) + kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000561 } else {
562 // Retrieve parameter without eager stack-frame relative to the
563 // stack-pointer.
564 return MemOperand(
565 sp, ArgumentsOffsetWithoutFrame(op->index()) + kPointerSize);
566 }
567}
568
569
570void LCodeGen::WriteTranslation(LEnvironment* environment,
571 Translation* translation) {
572 if (environment == NULL) return;
573
574 // The translation includes one command per value in the environment.
575 int translation_size = environment->translation_size();
576
577 WriteTranslation(environment->outer(), translation);
578 WriteTranslationFrame(environment, translation);
579
580 int object_index = 0;
581 int dematerialized_index = 0;
582 for (int i = 0; i < translation_size; ++i) {
583 LOperand* value = environment->values()->at(i);
584 AddToTranslation(
585 environment, translation, value, environment->HasTaggedValueAt(i),
586 environment->HasUint32ValueAt(i), &object_index, &dematerialized_index);
587 }
588}
589
590
591void LCodeGen::AddToTranslation(LEnvironment* environment,
592 Translation* translation,
593 LOperand* op,
594 bool is_tagged,
595 bool is_uint32,
596 int* object_index_pointer,
597 int* dematerialized_index_pointer) {
598 if (op == LEnvironment::materialization_marker()) {
599 int object_index = (*object_index_pointer)++;
600 if (environment->ObjectIsDuplicateAt(object_index)) {
601 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
602 translation->DuplicateObject(dupe_of);
603 return;
604 }
605 int object_length = environment->ObjectLengthAt(object_index);
606 if (environment->ObjectIsArgumentsAt(object_index)) {
607 translation->BeginArgumentsObject(object_length);
608 } else {
609 translation->BeginCapturedObject(object_length);
610 }
611 int dematerialized_index = *dematerialized_index_pointer;
612 int env_offset = environment->translation_size() + dematerialized_index;
613 *dematerialized_index_pointer += object_length;
614 for (int i = 0; i < object_length; ++i) {
615 LOperand* value = environment->values()->at(env_offset + i);
616 AddToTranslation(environment,
617 translation,
618 value,
619 environment->HasTaggedValueAt(env_offset + i),
620 environment->HasUint32ValueAt(env_offset + i),
621 object_index_pointer,
622 dematerialized_index_pointer);
623 }
624 return;
625 }
626
627 if (op->IsStackSlot()) {
628 int index = op->index();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000629 if (is_tagged) {
630 translation->StoreStackSlot(index);
631 } else if (is_uint32) {
632 translation->StoreUint32StackSlot(index);
633 } else {
634 translation->StoreInt32StackSlot(index);
635 }
636 } else if (op->IsDoubleStackSlot()) {
637 int index = op->index();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000638 translation->StoreDoubleStackSlot(index);
639 } else if (op->IsRegister()) {
640 Register reg = ToRegister(op);
641 if (is_tagged) {
642 translation->StoreRegister(reg);
643 } else if (is_uint32) {
644 translation->StoreUint32Register(reg);
645 } else {
646 translation->StoreInt32Register(reg);
647 }
648 } else if (op->IsDoubleRegister()) {
649 DoubleRegister reg = ToDoubleRegister(op);
650 translation->StoreDoubleRegister(reg);
651 } else if (op->IsConstantOperand()) {
652 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
653 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
654 translation->StoreLiteral(src_index);
655 } else {
656 UNREACHABLE();
657 }
658}
659
660
661void LCodeGen::CallCode(Handle<Code> code,
662 RelocInfo::Mode mode,
663 LInstruction* instr) {
664 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT);
665}
666
667
668void LCodeGen::CallCodeGeneric(Handle<Code> code,
669 RelocInfo::Mode mode,
670 LInstruction* instr,
671 SafepointMode safepoint_mode) {
672 DCHECK(instr != NULL);
673 __ Call(code, mode);
674 RecordSafepointWithLazyDeopt(instr, safepoint_mode);
675}
676
677
678void LCodeGen::CallRuntime(const Runtime::Function* function,
679 int num_arguments,
680 LInstruction* instr,
681 SaveFPRegsMode save_doubles) {
682 DCHECK(instr != NULL);
683
684 __ CallRuntime(function, num_arguments, save_doubles);
685
686 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
687}
688
689
690void LCodeGen::LoadContextFromDeferred(LOperand* context) {
691 if (context->IsRegister()) {
692 __ Move(cp, ToRegister(context));
693 } else if (context->IsStackSlot()) {
694 __ lw(cp, ToMemOperand(context));
695 } else if (context->IsConstantOperand()) {
696 HConstant* constant =
697 chunk_->LookupConstant(LConstantOperand::cast(context));
698 __ li(cp, Handle<Object>::cast(constant->handle(isolate())));
699 } else {
700 UNREACHABLE();
701 }
702}
703
704
705void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
706 int argc,
707 LInstruction* instr,
708 LOperand* context) {
709 LoadContextFromDeferred(context);
710 __ CallRuntimeSaveDoubles(id);
711 RecordSafepointWithRegisters(
712 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
713}
714
715
716void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
717 Safepoint::DeoptMode mode) {
718 environment->set_has_been_used();
719 if (!environment->HasBeenRegistered()) {
720 // Physical stack frame layout:
721 // -x ............. -4 0 ..................................... y
722 // [incoming arguments] [spill slots] [pushed outgoing arguments]
723
724 // Layout of the environment:
725 // 0 ..................................................... size-1
726 // [parameters] [locals] [expression stack including arguments]
727
728 // Layout of the translation:
729 // 0 ........................................................ size - 1 + 4
730 // [expression stack including arguments] [locals] [4 words] [parameters]
731 // |>------------ translation_size ------------<|
732
733 int frame_count = 0;
734 int jsframe_count = 0;
735 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
736 ++frame_count;
737 if (e->frame_type() == JS_FUNCTION) {
738 ++jsframe_count;
739 }
740 }
741 Translation translation(&translations_, frame_count, jsframe_count, zone());
742 WriteTranslation(environment, &translation);
743 int deoptimization_index = deoptimizations_.length();
744 int pc_offset = masm()->pc_offset();
745 environment->Register(deoptimization_index,
746 translation.index(),
747 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
748 deoptimizations_.Add(environment, zone());
749 }
750}
751
752
753void LCodeGen::DeoptimizeIf(Condition condition, LInstruction* instr,
754 Deoptimizer::DeoptReason deopt_reason,
755 Deoptimizer::BailoutType bailout_type,
756 Register src1, const Operand& src2) {
757 LEnvironment* environment = instr->environment();
758 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
759 DCHECK(environment->HasBeenRegistered());
760 int id = environment->deoptimization_index();
761 Address entry =
762 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
763 if (entry == NULL) {
764 Abort(kBailoutWasNotPrepared);
765 return;
766 }
767
768 if (FLAG_deopt_every_n_times != 0 && !info()->IsStub()) {
769 Register scratch = scratch0();
770 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
771 Label no_deopt;
772 __ Push(a1, scratch);
773 __ li(scratch, Operand(count));
774 __ lw(a1, MemOperand(scratch));
775 __ Subu(a1, a1, Operand(1));
776 __ Branch(&no_deopt, ne, a1, Operand(zero_reg));
777 __ li(a1, Operand(FLAG_deopt_every_n_times));
778 __ sw(a1, MemOperand(scratch));
779 __ Pop(a1, scratch);
780
781 __ Call(entry, RelocInfo::RUNTIME_ENTRY);
782 __ bind(&no_deopt);
783 __ sw(a1, MemOperand(scratch));
784 __ Pop(a1, scratch);
785 }
786
787 if (info()->ShouldTrapOnDeopt()) {
788 Label skip;
789 if (condition != al) {
790 __ Branch(&skip, NegateCondition(condition), src1, src2);
791 }
792 __ stop("trap_on_deopt");
793 __ bind(&skip);
794 }
795
796 Deoptimizer::DeoptInfo deopt_info = MakeDeoptInfo(instr, deopt_reason);
797
798 DCHECK(info()->IsStub() || frame_is_built_);
799 // Go through jump table if we need to handle condition, build frame, or
800 // restore caller doubles.
801 if (condition == al && frame_is_built_ &&
802 !info()->saves_caller_doubles()) {
803 DeoptComment(deopt_info);
804 __ Call(entry, RelocInfo::RUNTIME_ENTRY, condition, src1, src2);
805 info()->LogDeoptCallPosition(masm()->pc_offset(), deopt_info.inlining_id);
806 } else {
807 Deoptimizer::JumpTableEntry table_entry(entry, deopt_info, bailout_type,
808 !frame_is_built_);
809 // We often have several deopts to the same entry, reuse the last
810 // jump entry if this is the case.
811 if (FLAG_trace_deopt || isolate()->cpu_profiler()->is_profiling() ||
812 jump_table_.is_empty() ||
813 !table_entry.IsEquivalentTo(jump_table_.last())) {
814 jump_table_.Add(table_entry, zone());
815 }
816 __ Branch(&jump_table_.last().label, condition, src1, src2);
817 }
818}
819
820
821void LCodeGen::DeoptimizeIf(Condition condition, LInstruction* instr,
822 Deoptimizer::DeoptReason deopt_reason,
823 Register src1, const Operand& src2) {
824 Deoptimizer::BailoutType bailout_type = info()->IsStub()
825 ? Deoptimizer::LAZY
826 : Deoptimizer::EAGER;
827 DeoptimizeIf(condition, instr, deopt_reason, bailout_type, src1, src2);
828}
829
830
831void LCodeGen::RecordSafepointWithLazyDeopt(
832 LInstruction* instr, SafepointMode safepoint_mode) {
833 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
834 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
835 } else {
836 DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
837 RecordSafepointWithRegisters(
838 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
839 }
840}
841
842
843void LCodeGen::RecordSafepoint(
844 LPointerMap* pointers,
845 Safepoint::Kind kind,
846 int arguments,
847 Safepoint::DeoptMode deopt_mode) {
848 DCHECK(expected_safepoint_kind_ == kind);
849
850 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
851 Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
852 kind, arguments, deopt_mode);
853 for (int i = 0; i < operands->length(); i++) {
854 LOperand* pointer = operands->at(i);
855 if (pointer->IsStackSlot()) {
856 safepoint.DefinePointerSlot(pointer->index(), zone());
857 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
858 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
859 }
860 }
861}
862
863
864void LCodeGen::RecordSafepoint(LPointerMap* pointers,
865 Safepoint::DeoptMode deopt_mode) {
866 RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
867}
868
869
870void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
871 LPointerMap empty_pointers(zone());
872 RecordSafepoint(&empty_pointers, deopt_mode);
873}
874
875
876void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
877 int arguments,
878 Safepoint::DeoptMode deopt_mode) {
879 RecordSafepoint(
880 pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
881}
882
883
884void LCodeGen::RecordAndWritePosition(int position) {
885 if (position == RelocInfo::kNoPosition) return;
886 masm()->positions_recorder()->RecordPosition(position);
887 masm()->positions_recorder()->WriteRecordedPositions();
888}
889
890
891static const char* LabelType(LLabel* label) {
892 if (label->is_loop_header()) return " (loop header)";
893 if (label->is_osr_entry()) return " (OSR entry)";
894 return "";
895}
896
897
898void LCodeGen::DoLabel(LLabel* label) {
899 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
900 current_instruction_,
901 label->hydrogen_value()->id(),
902 label->block_id(),
903 LabelType(label));
904 __ bind(label->label());
905 current_block_ = label->block_id();
906 DoGap(label);
907}
908
909
910void LCodeGen::DoParallelMove(LParallelMove* move) {
911 resolver_.Resolve(move);
912}
913
914
915void LCodeGen::DoGap(LGap* gap) {
916 for (int i = LGap::FIRST_INNER_POSITION;
917 i <= LGap::LAST_INNER_POSITION;
918 i++) {
919 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
920 LParallelMove* move = gap->GetParallelMove(inner_pos);
921 if (move != NULL) DoParallelMove(move);
922 }
923}
924
925
926void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
927 DoGap(instr);
928}
929
930
931void LCodeGen::DoParameter(LParameter* instr) {
932 // Nothing to do.
933}
934
935
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000936void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
937 GenerateOsrPrologue();
938}
939
940
941void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
942 Register dividend = ToRegister(instr->dividend());
943 int32_t divisor = instr->divisor();
944 DCHECK(dividend.is(ToRegister(instr->result())));
945
946 // Theoretically, a variation of the branch-free code for integer division by
947 // a power of 2 (calculating the remainder via an additional multiplication
948 // (which gets simplified to an 'and') and subtraction) should be faster, and
949 // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
950 // indicate that positive dividends are heavily favored, so the branching
951 // version performs better.
952 HMod* hmod = instr->hydrogen();
953 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
954 Label dividend_is_not_negative, done;
955
956 if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
957 __ Branch(&dividend_is_not_negative, ge, dividend, Operand(zero_reg));
958 // Note: The code below even works when right contains kMinInt.
959 __ subu(dividend, zero_reg, dividend);
960 __ And(dividend, dividend, Operand(mask));
961 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
962 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, dividend,
963 Operand(zero_reg));
964 }
965 __ Branch(USE_DELAY_SLOT, &done);
966 __ subu(dividend, zero_reg, dividend);
967 }
968
969 __ bind(&dividend_is_not_negative);
970 __ And(dividend, dividend, Operand(mask));
971 __ bind(&done);
972}
973
974
975void LCodeGen::DoModByConstI(LModByConstI* instr) {
976 Register dividend = ToRegister(instr->dividend());
977 int32_t divisor = instr->divisor();
978 Register result = ToRegister(instr->result());
979 DCHECK(!dividend.is(result));
980
981 if (divisor == 0) {
982 DeoptimizeIf(al, instr);
983 return;
984 }
985
986 __ TruncatingDiv(result, dividend, Abs(divisor));
987 __ Mul(result, result, Operand(Abs(divisor)));
988 __ Subu(result, dividend, Operand(result));
989
990 // Check for negative zero.
991 HMod* hmod = instr->hydrogen();
992 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
993 Label remainder_not_zero;
994 __ Branch(&remainder_not_zero, ne, result, Operand(zero_reg));
995 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, dividend,
996 Operand(zero_reg));
997 __ bind(&remainder_not_zero);
998 }
999}
1000
1001
1002void LCodeGen::DoModI(LModI* instr) {
1003 HMod* hmod = instr->hydrogen();
1004 const Register left_reg = ToRegister(instr->left());
1005 const Register right_reg = ToRegister(instr->right());
1006 const Register result_reg = ToRegister(instr->result());
1007
1008 // div runs in the background while we check for special cases.
1009 __ Mod(result_reg, left_reg, right_reg);
1010
1011 Label done;
1012 // Check for x % 0, we have to deopt in this case because we can't return a
1013 // NaN.
1014 if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
1015 DeoptimizeIf(eq, instr, Deoptimizer::kDivisionByZero, right_reg,
1016 Operand(zero_reg));
1017 }
1018
1019 // Check for kMinInt % -1, div will return kMinInt, which is not what we
1020 // want. We have to deopt if we care about -0, because we can't return that.
1021 if (hmod->CheckFlag(HValue::kCanOverflow)) {
1022 Label no_overflow_possible;
1023 __ Branch(&no_overflow_possible, ne, left_reg, Operand(kMinInt));
1024 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1025 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, right_reg, Operand(-1));
1026 } else {
1027 __ Branch(&no_overflow_possible, ne, right_reg, Operand(-1));
1028 __ Branch(USE_DELAY_SLOT, &done);
1029 __ mov(result_reg, zero_reg);
1030 }
1031 __ bind(&no_overflow_possible);
1032 }
1033
1034 // If we care about -0, test if the dividend is <0 and the result is 0.
1035 __ Branch(&done, ge, left_reg, Operand(zero_reg));
1036 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
1037 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, result_reg,
1038 Operand(zero_reg));
1039 }
1040 __ bind(&done);
1041}
1042
1043
1044void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
1045 Register dividend = ToRegister(instr->dividend());
1046 int32_t divisor = instr->divisor();
1047 Register result = ToRegister(instr->result());
1048 DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
1049 DCHECK(!result.is(dividend));
1050
1051 // Check for (0 / -x) that will produce negative zero.
1052 HDiv* hdiv = instr->hydrogen();
1053 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1054 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, dividend,
1055 Operand(zero_reg));
1056 }
1057 // Check for (kMinInt / -1).
1058 if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
1059 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow, dividend, Operand(kMinInt));
1060 }
1061 // Deoptimize if remainder will not be 0.
1062 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1063 divisor != 1 && divisor != -1) {
1064 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1065 __ And(at, dividend, Operand(mask));
1066 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision, at, Operand(zero_reg));
1067 }
1068
1069 if (divisor == -1) { // Nice shortcut, not needed for correctness.
1070 __ Subu(result, zero_reg, dividend);
1071 return;
1072 }
1073 uint16_t shift = WhichPowerOf2Abs(divisor);
1074 if (shift == 0) {
1075 __ Move(result, dividend);
1076 } else if (shift == 1) {
1077 __ srl(result, dividend, 31);
1078 __ Addu(result, dividend, Operand(result));
1079 } else {
1080 __ sra(result, dividend, 31);
1081 __ srl(result, result, 32 - shift);
1082 __ Addu(result, dividend, Operand(result));
1083 }
1084 if (shift > 0) __ sra(result, result, shift);
1085 if (divisor < 0) __ Subu(result, zero_reg, result);
1086}
1087
1088
1089void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
1090 Register dividend = ToRegister(instr->dividend());
1091 int32_t divisor = instr->divisor();
1092 Register result = ToRegister(instr->result());
1093 DCHECK(!dividend.is(result));
1094
1095 if (divisor == 0) {
1096 DeoptimizeIf(al, instr);
1097 return;
1098 }
1099
1100 // Check for (0 / -x) that will produce negative zero.
1101 HDiv* hdiv = instr->hydrogen();
1102 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1103 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, dividend,
1104 Operand(zero_reg));
1105 }
1106
1107 __ TruncatingDiv(result, dividend, Abs(divisor));
1108 if (divisor < 0) __ Subu(result, zero_reg, result);
1109
1110 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1111 __ Mul(scratch0(), result, Operand(divisor));
1112 __ Subu(scratch0(), scratch0(), dividend);
1113 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision, scratch0(),
1114 Operand(zero_reg));
1115 }
1116}
1117
1118
1119// TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
1120void LCodeGen::DoDivI(LDivI* instr) {
1121 HBinaryOperation* hdiv = instr->hydrogen();
1122 Register dividend = ToRegister(instr->dividend());
1123 Register divisor = ToRegister(instr->divisor());
1124 const Register result = ToRegister(instr->result());
1125 Register remainder = ToRegister(instr->temp());
1126
1127 // On MIPS div is asynchronous - it will run in the background while we
1128 // check for special cases.
1129 __ Div(remainder, result, dividend, divisor);
1130
1131 // Check for x / 0.
1132 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1133 DeoptimizeIf(eq, instr, Deoptimizer::kDivisionByZero, divisor,
1134 Operand(zero_reg));
1135 }
1136
1137 // Check for (0 / -x) that will produce negative zero.
1138 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1139 Label left_not_zero;
1140 __ Branch(&left_not_zero, ne, dividend, Operand(zero_reg));
1141 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, divisor,
1142 Operand(zero_reg));
1143 __ bind(&left_not_zero);
1144 }
1145
1146 // Check for (kMinInt / -1).
1147 if (hdiv->CheckFlag(HValue::kCanOverflow) &&
1148 !hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1149 Label left_not_min_int;
1150 __ Branch(&left_not_min_int, ne, dividend, Operand(kMinInt));
1151 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow, divisor, Operand(-1));
1152 __ bind(&left_not_min_int);
1153 }
1154
1155 if (!hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1156 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision, remainder,
1157 Operand(zero_reg));
1158 }
1159}
1160
1161
1162void LCodeGen::DoMultiplyAddD(LMultiplyAddD* instr) {
1163 DoubleRegister addend = ToDoubleRegister(instr->addend());
1164 DoubleRegister multiplier = ToDoubleRegister(instr->multiplier());
1165 DoubleRegister multiplicand = ToDoubleRegister(instr->multiplicand());
1166
1167 // This is computed in-place.
1168 DCHECK(addend.is(ToDoubleRegister(instr->result())));
1169
1170 __ madd_d(addend, addend, multiplier, multiplicand);
1171}
1172
1173
1174void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
1175 Register dividend = ToRegister(instr->dividend());
1176 Register result = ToRegister(instr->result());
1177 int32_t divisor = instr->divisor();
1178 Register scratch = result.is(dividend) ? scratch0() : dividend;
1179 DCHECK(!result.is(dividend) || !scratch.is(dividend));
1180
1181 // If the divisor is 1, return the dividend.
1182 if (divisor == 1) {
1183 __ Move(result, dividend);
1184 return;
1185 }
1186
1187 // If the divisor is positive, things are easy: There can be no deopts and we
1188 // can simply do an arithmetic right shift.
1189 uint16_t shift = WhichPowerOf2Abs(divisor);
1190 if (divisor > 1) {
1191 __ sra(result, dividend, shift);
1192 return;
1193 }
1194
1195 // If the divisor is negative, we have to negate and handle edge cases.
1196
1197 // dividend can be the same register as result so save the value of it
1198 // for checking overflow.
1199 __ Move(scratch, dividend);
1200
1201 __ Subu(result, zero_reg, dividend);
1202 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1203 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, result, Operand(zero_reg));
1204 }
1205
1206 // Dividing by -1 is basically negation, unless we overflow.
1207 __ Xor(scratch, scratch, result);
1208 if (divisor == -1) {
1209 if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1210 DeoptimizeIf(ge, instr, Deoptimizer::kOverflow, scratch,
1211 Operand(zero_reg));
1212 }
1213 return;
1214 }
1215
1216 // If the negation could not overflow, simply shifting is OK.
1217 if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1218 __ sra(result, result, shift);
1219 return;
1220 }
1221
1222 Label no_overflow, done;
1223 __ Branch(&no_overflow, lt, scratch, Operand(zero_reg));
1224 __ li(result, Operand(kMinInt / divisor));
1225 __ Branch(&done);
1226 __ bind(&no_overflow);
1227 __ sra(result, result, shift);
1228 __ bind(&done);
1229}
1230
1231
1232void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
1233 Register dividend = ToRegister(instr->dividend());
1234 int32_t divisor = instr->divisor();
1235 Register result = ToRegister(instr->result());
1236 DCHECK(!dividend.is(result));
1237
1238 if (divisor == 0) {
1239 DeoptimizeIf(al, instr);
1240 return;
1241 }
1242
1243 // Check for (0 / -x) that will produce negative zero.
1244 HMathFloorOfDiv* hdiv = instr->hydrogen();
1245 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1246 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, dividend,
1247 Operand(zero_reg));
1248 }
1249
1250 // Easy case: We need no dynamic check for the dividend and the flooring
1251 // division is the same as the truncating division.
1252 if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
1253 (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
1254 __ TruncatingDiv(result, dividend, Abs(divisor));
1255 if (divisor < 0) __ Subu(result, zero_reg, result);
1256 return;
1257 }
1258
1259 // In the general case we may need to adjust before and after the truncating
1260 // division to get a flooring division.
1261 Register temp = ToRegister(instr->temp());
1262 DCHECK(!temp.is(dividend) && !temp.is(result));
1263 Label needs_adjustment, done;
1264 __ Branch(&needs_adjustment, divisor > 0 ? lt : gt,
1265 dividend, Operand(zero_reg));
1266 __ TruncatingDiv(result, dividend, Abs(divisor));
1267 if (divisor < 0) __ Subu(result, zero_reg, result);
1268 __ jmp(&done);
1269 __ bind(&needs_adjustment);
1270 __ Addu(temp, dividend, Operand(divisor > 0 ? 1 : -1));
1271 __ TruncatingDiv(result, temp, Abs(divisor));
1272 if (divisor < 0) __ Subu(result, zero_reg, result);
1273 __ Subu(result, result, Operand(1));
1274 __ bind(&done);
1275}
1276
1277
1278// TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
1279void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
1280 HBinaryOperation* hdiv = instr->hydrogen();
1281 Register dividend = ToRegister(instr->dividend());
1282 Register divisor = ToRegister(instr->divisor());
1283 const Register result = ToRegister(instr->result());
1284 Register remainder = scratch0();
1285 // On MIPS div is asynchronous - it will run in the background while we
1286 // check for special cases.
1287 __ Div(remainder, result, dividend, divisor);
1288
1289 // Check for x / 0.
1290 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1291 DeoptimizeIf(eq, instr, Deoptimizer::kDivisionByZero, divisor,
1292 Operand(zero_reg));
1293 }
1294
1295 // Check for (0 / -x) that will produce negative zero.
1296 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1297 Label left_not_zero;
1298 __ Branch(&left_not_zero, ne, dividend, Operand(zero_reg));
1299 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, divisor,
1300 Operand(zero_reg));
1301 __ bind(&left_not_zero);
1302 }
1303
1304 // Check for (kMinInt / -1).
1305 if (hdiv->CheckFlag(HValue::kCanOverflow) &&
1306 !hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1307 Label left_not_min_int;
1308 __ Branch(&left_not_min_int, ne, dividend, Operand(kMinInt));
1309 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow, divisor, Operand(-1));
1310 __ bind(&left_not_min_int);
1311 }
1312
1313 // We performed a truncating division. Correct the result if necessary.
1314 Label done;
1315 __ Branch(&done, eq, remainder, Operand(zero_reg), USE_DELAY_SLOT);
1316 __ Xor(remainder, remainder, Operand(divisor));
1317 __ Branch(&done, ge, remainder, Operand(zero_reg));
1318 __ Subu(result, result, Operand(1));
1319 __ bind(&done);
1320}
1321
1322
1323void LCodeGen::DoMulI(LMulI* instr) {
1324 Register scratch = scratch0();
1325 Register result = ToRegister(instr->result());
1326 // Note that result may alias left.
1327 Register left = ToRegister(instr->left());
1328 LOperand* right_op = instr->right();
1329
1330 bool bailout_on_minus_zero =
1331 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
1332 bool overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1333
1334 if (right_op->IsConstantOperand()) {
1335 int32_t constant = ToInteger32(LConstantOperand::cast(right_op));
1336
1337 if (bailout_on_minus_zero && (constant < 0)) {
1338 // The case of a null constant will be handled separately.
1339 // If constant is negative and left is null, the result should be -0.
1340 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, left, Operand(zero_reg));
1341 }
1342
1343 switch (constant) {
1344 case -1:
1345 if (overflow) {
1346 Label no_overflow;
1347 __ SubBranchNoOvf(result, zero_reg, Operand(left), &no_overflow);
1348 DeoptimizeIf(al, instr);
1349 __ bind(&no_overflow);
1350 } else {
1351 __ Subu(result, zero_reg, left);
1352 }
1353 break;
1354 case 0:
1355 if (bailout_on_minus_zero) {
1356 // If left is strictly negative and the constant is null, the
1357 // result is -0. Deoptimize if required, otherwise return 0.
1358 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, left,
1359 Operand(zero_reg));
1360 }
1361 __ mov(result, zero_reg);
1362 break;
1363 case 1:
1364 // Nothing to do.
1365 __ Move(result, left);
1366 break;
1367 default:
1368 // Multiplying by powers of two and powers of two plus or minus
1369 // one can be done faster with shifted operands.
1370 // For other constants we emit standard code.
1371 int32_t mask = constant >> 31;
1372 uint32_t constant_abs = (constant + mask) ^ mask;
1373
1374 if (base::bits::IsPowerOfTwo32(constant_abs)) {
1375 int32_t shift = WhichPowerOf2(constant_abs);
1376 __ sll(result, left, shift);
1377 // Correct the sign of the result if the constant is negative.
1378 if (constant < 0) __ Subu(result, zero_reg, result);
1379 } else if (base::bits::IsPowerOfTwo32(constant_abs - 1)) {
1380 int32_t shift = WhichPowerOf2(constant_abs - 1);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001381 __ Lsa(result, left, left, shift);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001382 // Correct the sign of the result if the constant is negative.
1383 if (constant < 0) __ Subu(result, zero_reg, result);
1384 } else if (base::bits::IsPowerOfTwo32(constant_abs + 1)) {
1385 int32_t shift = WhichPowerOf2(constant_abs + 1);
1386 __ sll(scratch, left, shift);
1387 __ Subu(result, scratch, left);
1388 // Correct the sign of the result if the constant is negative.
1389 if (constant < 0) __ Subu(result, zero_reg, result);
1390 } else {
1391 // Generate standard code.
1392 __ li(at, constant);
1393 __ Mul(result, left, at);
1394 }
1395 }
1396
1397 } else {
1398 DCHECK(right_op->IsRegister());
1399 Register right = ToRegister(right_op);
1400
1401 if (overflow) {
1402 // hi:lo = left * right.
1403 if (instr->hydrogen()->representation().IsSmi()) {
1404 __ SmiUntag(result, left);
1405 __ Mul(scratch, result, result, right);
1406 } else {
1407 __ Mul(scratch, result, left, right);
1408 }
1409 __ sra(at, result, 31);
1410 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow, scratch, Operand(at));
1411 } else {
1412 if (instr->hydrogen()->representation().IsSmi()) {
1413 __ SmiUntag(result, left);
1414 __ Mul(result, result, right);
1415 } else {
1416 __ Mul(result, left, right);
1417 }
1418 }
1419
1420 if (bailout_on_minus_zero) {
1421 Label done;
1422 __ Xor(at, left, right);
1423 __ Branch(&done, ge, at, Operand(zero_reg));
1424 // Bail out if the result is minus zero.
1425 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, result,
1426 Operand(zero_reg));
1427 __ bind(&done);
1428 }
1429 }
1430}
1431
1432
1433void LCodeGen::DoBitI(LBitI* instr) {
1434 LOperand* left_op = instr->left();
1435 LOperand* right_op = instr->right();
1436 DCHECK(left_op->IsRegister());
1437 Register left = ToRegister(left_op);
1438 Register result = ToRegister(instr->result());
1439 Operand right(no_reg);
1440
1441 if (right_op->IsStackSlot()) {
1442 right = Operand(EmitLoadRegister(right_op, at));
1443 } else {
1444 DCHECK(right_op->IsRegister() || right_op->IsConstantOperand());
1445 right = ToOperand(right_op);
1446 }
1447
1448 switch (instr->op()) {
1449 case Token::BIT_AND:
1450 __ And(result, left, right);
1451 break;
1452 case Token::BIT_OR:
1453 __ Or(result, left, right);
1454 break;
1455 case Token::BIT_XOR:
1456 if (right_op->IsConstantOperand() && right.immediate() == int32_t(~0)) {
1457 __ Nor(result, zero_reg, left);
1458 } else {
1459 __ Xor(result, left, right);
1460 }
1461 break;
1462 default:
1463 UNREACHABLE();
1464 break;
1465 }
1466}
1467
1468
1469void LCodeGen::DoShiftI(LShiftI* instr) {
1470 // Both 'left' and 'right' are "used at start" (see LCodeGen::DoShift), so
1471 // result may alias either of them.
1472 LOperand* right_op = instr->right();
1473 Register left = ToRegister(instr->left());
1474 Register result = ToRegister(instr->result());
1475 Register scratch = scratch0();
1476
1477 if (right_op->IsRegister()) {
1478 // No need to mask the right operand on MIPS, it is built into the variable
1479 // shift instructions.
1480 switch (instr->op()) {
1481 case Token::ROR:
1482 __ Ror(result, left, Operand(ToRegister(right_op)));
1483 break;
1484 case Token::SAR:
1485 __ srav(result, left, ToRegister(right_op));
1486 break;
1487 case Token::SHR:
1488 __ srlv(result, left, ToRegister(right_op));
1489 if (instr->can_deopt()) {
1490 DeoptimizeIf(lt, instr, Deoptimizer::kNegativeValue, result,
1491 Operand(zero_reg));
1492 }
1493 break;
1494 case Token::SHL:
1495 __ sllv(result, left, ToRegister(right_op));
1496 break;
1497 default:
1498 UNREACHABLE();
1499 break;
1500 }
1501 } else {
1502 // Mask the right_op operand.
1503 int value = ToInteger32(LConstantOperand::cast(right_op));
1504 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1505 switch (instr->op()) {
1506 case Token::ROR:
1507 if (shift_count != 0) {
1508 __ Ror(result, left, Operand(shift_count));
1509 } else {
1510 __ Move(result, left);
1511 }
1512 break;
1513 case Token::SAR:
1514 if (shift_count != 0) {
1515 __ sra(result, left, shift_count);
1516 } else {
1517 __ Move(result, left);
1518 }
1519 break;
1520 case Token::SHR:
1521 if (shift_count != 0) {
1522 __ srl(result, left, shift_count);
1523 } else {
1524 if (instr->can_deopt()) {
1525 __ And(at, left, Operand(0x80000000));
1526 DeoptimizeIf(ne, instr, Deoptimizer::kNegativeValue, at,
1527 Operand(zero_reg));
1528 }
1529 __ Move(result, left);
1530 }
1531 break;
1532 case Token::SHL:
1533 if (shift_count != 0) {
1534 if (instr->hydrogen_value()->representation().IsSmi() &&
1535 instr->can_deopt()) {
1536 if (shift_count != 1) {
1537 __ sll(result, left, shift_count - 1);
1538 __ SmiTagCheckOverflow(result, result, scratch);
1539 } else {
1540 __ SmiTagCheckOverflow(result, left, scratch);
1541 }
1542 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, scratch,
1543 Operand(zero_reg));
1544 } else {
1545 __ sll(result, left, shift_count);
1546 }
1547 } else {
1548 __ Move(result, left);
1549 }
1550 break;
1551 default:
1552 UNREACHABLE();
1553 break;
1554 }
1555 }
1556}
1557
1558
1559void LCodeGen::DoSubI(LSubI* instr) {
1560 LOperand* left = instr->left();
1561 LOperand* right = instr->right();
1562 LOperand* result = instr->result();
1563 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1564
1565 if (!can_overflow) {
1566 if (right->IsStackSlot()) {
1567 Register right_reg = EmitLoadRegister(right, at);
1568 __ Subu(ToRegister(result), ToRegister(left), Operand(right_reg));
1569 } else {
1570 DCHECK(right->IsRegister() || right->IsConstantOperand());
1571 __ Subu(ToRegister(result), ToRegister(left), ToOperand(right));
1572 }
1573 } else { // can_overflow.
1574 Register scratch = scratch0();
1575 Label no_overflow_label;
1576 if (right->IsStackSlot()) {
1577 Register right_reg = EmitLoadRegister(right, scratch);
1578 __ SubBranchNoOvf(ToRegister(result), ToRegister(left),
1579 Operand(right_reg), &no_overflow_label);
1580 } else {
1581 DCHECK(right->IsRegister() || right->IsConstantOperand());
1582 __ SubBranchNoOvf(ToRegister(result), ToRegister(left), ToOperand(right),
1583 &no_overflow_label, scratch);
1584 }
1585 DeoptimizeIf(al, instr);
1586 __ bind(&no_overflow_label);
1587 }
1588}
1589
1590
1591void LCodeGen::DoConstantI(LConstantI* instr) {
1592 __ li(ToRegister(instr->result()), Operand(instr->value()));
1593}
1594
1595
1596void LCodeGen::DoConstantS(LConstantS* instr) {
1597 __ li(ToRegister(instr->result()), Operand(instr->value()));
1598}
1599
1600
1601void LCodeGen::DoConstantD(LConstantD* instr) {
1602 DCHECK(instr->result()->IsDoubleRegister());
1603 DoubleRegister result = ToDoubleRegister(instr->result());
1604 double v = instr->value();
1605 __ Move(result, v);
1606}
1607
1608
1609void LCodeGen::DoConstantE(LConstantE* instr) {
1610 __ li(ToRegister(instr->result()), Operand(instr->value()));
1611}
1612
1613
1614void LCodeGen::DoConstantT(LConstantT* instr) {
1615 Handle<Object> object = instr->value(isolate());
1616 AllowDeferredHandleDereference smi_check;
1617 __ li(ToRegister(instr->result()), object);
1618}
1619
1620
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001621MemOperand LCodeGen::BuildSeqStringOperand(Register string,
1622 LOperand* index,
1623 String::Encoding encoding) {
1624 if (index->IsConstantOperand()) {
1625 int offset = ToInteger32(LConstantOperand::cast(index));
1626 if (encoding == String::TWO_BYTE_ENCODING) {
1627 offset *= kUC16Size;
1628 }
1629 STATIC_ASSERT(kCharSize == 1);
1630 return FieldMemOperand(string, SeqString::kHeaderSize + offset);
1631 }
1632 Register scratch = scratch0();
1633 DCHECK(!scratch.is(string));
1634 DCHECK(!scratch.is(ToRegister(index)));
1635 if (encoding == String::ONE_BYTE_ENCODING) {
1636 __ Addu(scratch, string, ToRegister(index));
1637 } else {
1638 STATIC_ASSERT(kUC16Size == 2);
1639 __ sll(scratch, ToRegister(index), 1);
1640 __ Addu(scratch, string, scratch);
1641 }
1642 return FieldMemOperand(scratch, SeqString::kHeaderSize);
1643}
1644
1645
1646void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
1647 String::Encoding encoding = instr->hydrogen()->encoding();
1648 Register string = ToRegister(instr->string());
1649 Register result = ToRegister(instr->result());
1650
1651 if (FLAG_debug_code) {
1652 Register scratch = scratch0();
1653 __ lw(scratch, FieldMemOperand(string, HeapObject::kMapOffset));
1654 __ lbu(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1655
1656 __ And(scratch, scratch,
1657 Operand(kStringRepresentationMask | kStringEncodingMask));
1658 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1659 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1660 __ Subu(at, scratch, Operand(encoding == String::ONE_BYTE_ENCODING
1661 ? one_byte_seq_type : two_byte_seq_type));
1662 __ Check(eq, kUnexpectedStringType, at, Operand(zero_reg));
1663 }
1664
1665 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1666 if (encoding == String::ONE_BYTE_ENCODING) {
1667 __ lbu(result, operand);
1668 } else {
1669 __ lhu(result, operand);
1670 }
1671}
1672
1673
1674void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
1675 String::Encoding encoding = instr->hydrogen()->encoding();
1676 Register string = ToRegister(instr->string());
1677 Register value = ToRegister(instr->value());
1678
1679 if (FLAG_debug_code) {
1680 Register scratch = scratch0();
1681 Register index = ToRegister(instr->index());
1682 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1683 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1684 int encoding_mask =
1685 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
1686 ? one_byte_seq_type : two_byte_seq_type;
1687 __ EmitSeqStringSetCharCheck(string, index, value, scratch, encoding_mask);
1688 }
1689
1690 MemOperand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1691 if (encoding == String::ONE_BYTE_ENCODING) {
1692 __ sb(value, operand);
1693 } else {
1694 __ sh(value, operand);
1695 }
1696}
1697
1698
1699void LCodeGen::DoAddI(LAddI* instr) {
1700 LOperand* left = instr->left();
1701 LOperand* right = instr->right();
1702 LOperand* result = instr->result();
1703 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1704
1705 if (!can_overflow) {
1706 if (right->IsStackSlot()) {
1707 Register right_reg = EmitLoadRegister(right, at);
1708 __ Addu(ToRegister(result), ToRegister(left), Operand(right_reg));
1709 } else {
1710 DCHECK(right->IsRegister() || right->IsConstantOperand());
1711 __ Addu(ToRegister(result), ToRegister(left), ToOperand(right));
1712 }
1713 } else { // can_overflow.
1714 Register scratch = scratch1();
1715 Label no_overflow_label;
1716 if (right->IsStackSlot()) {
1717 Register right_reg = EmitLoadRegister(right, scratch);
1718 __ AddBranchNoOvf(ToRegister(result), ToRegister(left),
1719 Operand(right_reg), &no_overflow_label);
1720 } else {
1721 DCHECK(right->IsRegister() || right->IsConstantOperand());
1722 __ AddBranchNoOvf(ToRegister(result), ToRegister(left), ToOperand(right),
1723 &no_overflow_label, scratch);
1724 }
1725 DeoptimizeIf(al, instr);
1726 __ bind(&no_overflow_label);
1727 }
1728}
1729
1730
1731void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
1732 LOperand* left = instr->left();
1733 LOperand* right = instr->right();
1734 HMathMinMax::Operation operation = instr->hydrogen()->operation();
1735 Condition condition = (operation == HMathMinMax::kMathMin) ? le : ge;
1736 if (instr->hydrogen()->representation().IsSmiOrInteger32()) {
1737 Register left_reg = ToRegister(left);
1738 Register right_reg = EmitLoadRegister(right, scratch0());
1739 Register result_reg = ToRegister(instr->result());
1740 Label return_right, done;
1741 Register scratch = scratch1();
1742 __ Slt(scratch, left_reg, Operand(right_reg));
1743 if (condition == ge) {
1744 __ Movz(result_reg, left_reg, scratch);
1745 __ Movn(result_reg, right_reg, scratch);
1746 } else {
1747 DCHECK(condition == le);
1748 __ Movn(result_reg, left_reg, scratch);
1749 __ Movz(result_reg, right_reg, scratch);
1750 }
1751 } else {
1752 DCHECK(instr->hydrogen()->representation().IsDouble());
1753 FPURegister left_reg = ToDoubleRegister(left);
1754 FPURegister right_reg = ToDoubleRegister(right);
1755 FPURegister result_reg = ToDoubleRegister(instr->result());
1756 Label check_nan_left, check_zero, return_left, return_right, done;
1757 __ BranchF(&check_zero, &check_nan_left, eq, left_reg, right_reg);
1758 __ BranchF(&return_left, NULL, condition, left_reg, right_reg);
1759 __ Branch(&return_right);
1760
1761 __ bind(&check_zero);
1762 // left == right != 0.
1763 __ BranchF(&return_left, NULL, ne, left_reg, kDoubleRegZero);
1764 // At this point, both left and right are either 0 or -0.
1765 if (operation == HMathMinMax::kMathMin) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001766 // The algorithm is: -((-L) + (-R)), which in case of L and R being
1767 // different registers is most efficiently expressed as -((-L) - R).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001768 __ neg_d(left_reg, left_reg);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001769 if (left_reg.is(right_reg)) {
1770 __ add_d(result_reg, left_reg, right_reg);
1771 } else {
1772 __ sub_d(result_reg, left_reg, right_reg);
1773 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001774 __ neg_d(result_reg, result_reg);
1775 } else {
1776 __ add_d(result_reg, left_reg, right_reg);
1777 }
1778 __ Branch(&done);
1779
1780 __ bind(&check_nan_left);
1781 // left == NaN.
1782 __ BranchF(NULL, &return_left, eq, left_reg, left_reg);
1783 __ bind(&return_right);
1784 if (!right_reg.is(result_reg)) {
1785 __ mov_d(result_reg, right_reg);
1786 }
1787 __ Branch(&done);
1788
1789 __ bind(&return_left);
1790 if (!left_reg.is(result_reg)) {
1791 __ mov_d(result_reg, left_reg);
1792 }
1793 __ bind(&done);
1794 }
1795}
1796
1797
1798void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
1799 DoubleRegister left = ToDoubleRegister(instr->left());
1800 DoubleRegister right = ToDoubleRegister(instr->right());
1801 DoubleRegister result = ToDoubleRegister(instr->result());
1802 switch (instr->op()) {
1803 case Token::ADD:
1804 __ add_d(result, left, right);
1805 break;
1806 case Token::SUB:
1807 __ sub_d(result, left, right);
1808 break;
1809 case Token::MUL:
1810 __ mul_d(result, left, right);
1811 break;
1812 case Token::DIV:
1813 __ div_d(result, left, right);
1814 break;
1815 case Token::MOD: {
1816 // Save a0-a3 on the stack.
1817 RegList saved_regs = a0.bit() | a1.bit() | a2.bit() | a3.bit();
1818 __ MultiPush(saved_regs);
1819
1820 __ PrepareCallCFunction(0, 2, scratch0());
1821 __ MovToFloatParameters(left, right);
1822 __ CallCFunction(
1823 ExternalReference::mod_two_doubles_operation(isolate()),
1824 0, 2);
1825 // Move the result in the double result register.
1826 __ MovFromFloatResult(result);
1827
1828 // Restore saved register.
1829 __ MultiPop(saved_regs);
1830 break;
1831 }
1832 default:
1833 UNREACHABLE();
1834 break;
1835 }
1836}
1837
1838
1839void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
1840 DCHECK(ToRegister(instr->context()).is(cp));
1841 DCHECK(ToRegister(instr->left()).is(a1));
1842 DCHECK(ToRegister(instr->right()).is(a0));
1843 DCHECK(ToRegister(instr->result()).is(v0));
1844
Ben Murdoch097c5b22016-05-18 11:27:45 +01001845 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), instr->op()).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001846 CallCode(code, RelocInfo::CODE_TARGET, instr);
1847 // Other arch use a nop here, to signal that there is no inlined
1848 // patchable code. Mips does not need the nop, since our marker
1849 // instruction (andi zero_reg) will never be used in normal code.
1850}
1851
1852
1853template<class InstrType>
1854void LCodeGen::EmitBranch(InstrType instr,
1855 Condition condition,
1856 Register src1,
1857 const Operand& src2) {
1858 int left_block = instr->TrueDestination(chunk_);
1859 int right_block = instr->FalseDestination(chunk_);
1860
1861 int next_block = GetNextEmittedBlock();
1862 if (right_block == left_block || condition == al) {
1863 EmitGoto(left_block);
1864 } else if (left_block == next_block) {
1865 __ Branch(chunk_->GetAssemblyLabel(right_block),
1866 NegateCondition(condition), src1, src2);
1867 } else if (right_block == next_block) {
1868 __ Branch(chunk_->GetAssemblyLabel(left_block), condition, src1, src2);
1869 } else {
1870 __ Branch(chunk_->GetAssemblyLabel(left_block), condition, src1, src2);
1871 __ Branch(chunk_->GetAssemblyLabel(right_block));
1872 }
1873}
1874
1875
1876template<class InstrType>
1877void LCodeGen::EmitBranchF(InstrType instr,
1878 Condition condition,
1879 FPURegister src1,
1880 FPURegister src2) {
1881 int right_block = instr->FalseDestination(chunk_);
1882 int left_block = instr->TrueDestination(chunk_);
1883
1884 int next_block = GetNextEmittedBlock();
1885 if (right_block == left_block) {
1886 EmitGoto(left_block);
1887 } else if (left_block == next_block) {
1888 __ BranchF(chunk_->GetAssemblyLabel(right_block), NULL,
1889 NegateFpuCondition(condition), src1, src2);
1890 } else if (right_block == next_block) {
1891 __ BranchF(chunk_->GetAssemblyLabel(left_block), NULL,
1892 condition, src1, src2);
1893 } else {
1894 __ BranchF(chunk_->GetAssemblyLabel(left_block), NULL,
1895 condition, src1, src2);
1896 __ Branch(chunk_->GetAssemblyLabel(right_block));
1897 }
1898}
1899
1900
1901template <class InstrType>
1902void LCodeGen::EmitTrueBranch(InstrType instr, Condition condition,
1903 Register src1, const Operand& src2) {
1904 int true_block = instr->TrueDestination(chunk_);
1905 __ Branch(chunk_->GetAssemblyLabel(true_block), condition, src1, src2);
1906}
1907
1908
1909template <class InstrType>
1910void LCodeGen::EmitFalseBranch(InstrType instr, Condition condition,
1911 Register src1, const Operand& src2) {
1912 int false_block = instr->FalseDestination(chunk_);
1913 __ Branch(chunk_->GetAssemblyLabel(false_block), condition, src1, src2);
1914}
1915
1916
1917template<class InstrType>
1918void LCodeGen::EmitFalseBranchF(InstrType instr,
1919 Condition condition,
1920 FPURegister src1,
1921 FPURegister src2) {
1922 int false_block = instr->FalseDestination(chunk_);
1923 __ BranchF(chunk_->GetAssemblyLabel(false_block), NULL,
1924 condition, src1, src2);
1925}
1926
1927
1928void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
1929 __ stop("LDebugBreak");
1930}
1931
1932
1933void LCodeGen::DoBranch(LBranch* instr) {
1934 Representation r = instr->hydrogen()->value()->representation();
1935 if (r.IsInteger32() || r.IsSmi()) {
1936 DCHECK(!info()->IsStub());
1937 Register reg = ToRegister(instr->value());
1938 EmitBranch(instr, ne, reg, Operand(zero_reg));
1939 } else if (r.IsDouble()) {
1940 DCHECK(!info()->IsStub());
1941 DoubleRegister reg = ToDoubleRegister(instr->value());
1942 // Test the double value. Zero and NaN are false.
1943 EmitBranchF(instr, ogl, reg, kDoubleRegZero);
1944 } else {
1945 DCHECK(r.IsTagged());
1946 Register reg = ToRegister(instr->value());
1947 HType type = instr->hydrogen()->value()->type();
1948 if (type.IsBoolean()) {
1949 DCHECK(!info()->IsStub());
1950 __ LoadRoot(at, Heap::kTrueValueRootIndex);
1951 EmitBranch(instr, eq, reg, Operand(at));
1952 } else if (type.IsSmi()) {
1953 DCHECK(!info()->IsStub());
1954 EmitBranch(instr, ne, reg, Operand(zero_reg));
1955 } else if (type.IsJSArray()) {
1956 DCHECK(!info()->IsStub());
1957 EmitBranch(instr, al, zero_reg, Operand(zero_reg));
1958 } else if (type.IsHeapNumber()) {
1959 DCHECK(!info()->IsStub());
1960 DoubleRegister dbl_scratch = double_scratch0();
1961 __ ldc1(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
1962 // Test the double value. Zero and NaN are false.
1963 EmitBranchF(instr, ogl, dbl_scratch, kDoubleRegZero);
1964 } else if (type.IsString()) {
1965 DCHECK(!info()->IsStub());
1966 __ lw(at, FieldMemOperand(reg, String::kLengthOffset));
1967 EmitBranch(instr, ne, at, Operand(zero_reg));
1968 } else {
1969 ToBooleanStub::Types expected = instr->hydrogen()->expected_input_types();
1970 // Avoid deopts in the case where we've never executed this path before.
1971 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
1972
1973 if (expected.Contains(ToBooleanStub::UNDEFINED)) {
1974 // undefined -> false.
1975 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1976 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
1977 }
1978 if (expected.Contains(ToBooleanStub::BOOLEAN)) {
1979 // Boolean -> its value.
1980 __ LoadRoot(at, Heap::kTrueValueRootIndex);
1981 __ Branch(instr->TrueLabel(chunk_), eq, reg, Operand(at));
1982 __ LoadRoot(at, Heap::kFalseValueRootIndex);
1983 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
1984 }
1985 if (expected.Contains(ToBooleanStub::NULL_TYPE)) {
1986 // 'null' -> false.
1987 __ LoadRoot(at, Heap::kNullValueRootIndex);
1988 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(at));
1989 }
1990
1991 if (expected.Contains(ToBooleanStub::SMI)) {
1992 // Smis: 0 -> false, all other -> true.
1993 __ Branch(instr->FalseLabel(chunk_), eq, reg, Operand(zero_reg));
1994 __ JumpIfSmi(reg, instr->TrueLabel(chunk_));
1995 } else if (expected.NeedsMap()) {
1996 // If we need a map later and have a Smi -> deopt.
1997 __ SmiTst(reg, at);
1998 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
1999 }
2000
2001 const Register map = scratch0();
2002 if (expected.NeedsMap()) {
2003 __ lw(map, FieldMemOperand(reg, HeapObject::kMapOffset));
2004 if (expected.CanBeUndetectable()) {
2005 // Undetectable -> false.
2006 __ lbu(at, FieldMemOperand(map, Map::kBitFieldOffset));
2007 __ And(at, at, Operand(1 << Map::kIsUndetectable));
2008 __ Branch(instr->FalseLabel(chunk_), ne, at, Operand(zero_reg));
2009 }
2010 }
2011
2012 if (expected.Contains(ToBooleanStub::SPEC_OBJECT)) {
2013 // spec object -> true.
2014 __ lbu(at, FieldMemOperand(map, Map::kInstanceTypeOffset));
2015 __ Branch(instr->TrueLabel(chunk_),
2016 ge, at, Operand(FIRST_JS_RECEIVER_TYPE));
2017 }
2018
2019 if (expected.Contains(ToBooleanStub::STRING)) {
2020 // String value -> false iff empty.
2021 Label not_string;
2022 __ lbu(at, FieldMemOperand(map, Map::kInstanceTypeOffset));
2023 __ Branch(&not_string, ge , at, Operand(FIRST_NONSTRING_TYPE));
2024 __ lw(at, FieldMemOperand(reg, String::kLengthOffset));
2025 __ Branch(instr->TrueLabel(chunk_), ne, at, Operand(zero_reg));
2026 __ Branch(instr->FalseLabel(chunk_));
2027 __ bind(&not_string);
2028 }
2029
2030 if (expected.Contains(ToBooleanStub::SYMBOL)) {
2031 // Symbol value -> true.
2032 const Register scratch = scratch1();
2033 __ lbu(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
2034 __ Branch(instr->TrueLabel(chunk_), eq, scratch, Operand(SYMBOL_TYPE));
2035 }
2036
2037 if (expected.Contains(ToBooleanStub::SIMD_VALUE)) {
2038 // SIMD value -> true.
2039 const Register scratch = scratch1();
2040 __ lbu(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
2041 __ Branch(instr->TrueLabel(chunk_), eq, scratch,
2042 Operand(SIMD128_VALUE_TYPE));
2043 }
2044
2045 if (expected.Contains(ToBooleanStub::HEAP_NUMBER)) {
2046 // heap number -> false iff +0, -0, or NaN.
2047 DoubleRegister dbl_scratch = double_scratch0();
2048 Label not_heap_number;
2049 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
2050 __ Branch(&not_heap_number, ne, map, Operand(at));
2051 __ ldc1(dbl_scratch, FieldMemOperand(reg, HeapNumber::kValueOffset));
2052 __ BranchF(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2053 ne, dbl_scratch, kDoubleRegZero);
2054 // Falls through if dbl_scratch == 0.
2055 __ Branch(instr->FalseLabel(chunk_));
2056 __ bind(&not_heap_number);
2057 }
2058
2059 if (!expected.IsGeneric()) {
2060 // We've seen something for the first time -> deopt.
2061 // This can only happen if we are not generic already.
2062 DeoptimizeIf(al, instr, Deoptimizer::kUnexpectedObject, zero_reg,
2063 Operand(zero_reg));
2064 }
2065 }
2066 }
2067}
2068
2069
2070void LCodeGen::EmitGoto(int block) {
2071 if (!IsNextEmittedBlock(block)) {
2072 __ jmp(chunk_->GetAssemblyLabel(LookupDestination(block)));
2073 }
2074}
2075
2076
2077void LCodeGen::DoGoto(LGoto* instr) {
2078 EmitGoto(instr->block_id());
2079}
2080
2081
2082Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
2083 Condition cond = kNoCondition;
2084 switch (op) {
2085 case Token::EQ:
2086 case Token::EQ_STRICT:
2087 cond = eq;
2088 break;
2089 case Token::NE:
2090 case Token::NE_STRICT:
2091 cond = ne;
2092 break;
2093 case Token::LT:
2094 cond = is_unsigned ? lo : lt;
2095 break;
2096 case Token::GT:
2097 cond = is_unsigned ? hi : gt;
2098 break;
2099 case Token::LTE:
2100 cond = is_unsigned ? ls : le;
2101 break;
2102 case Token::GTE:
2103 cond = is_unsigned ? hs : ge;
2104 break;
2105 case Token::IN:
2106 case Token::INSTANCEOF:
2107 default:
2108 UNREACHABLE();
2109 }
2110 return cond;
2111}
2112
2113
2114void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2115 LOperand* left = instr->left();
2116 LOperand* right = instr->right();
2117 bool is_unsigned =
2118 instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2119 instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2120 Condition cond = TokenToCondition(instr->op(), is_unsigned);
2121
2122 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2123 // We can statically evaluate the comparison.
2124 double left_val = ToDouble(LConstantOperand::cast(left));
2125 double right_val = ToDouble(LConstantOperand::cast(right));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002126 int next_block = Token::EvalComparison(instr->op(), left_val, right_val)
2127 ? instr->TrueDestination(chunk_)
2128 : instr->FalseDestination(chunk_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002129 EmitGoto(next_block);
2130 } else {
2131 if (instr->is_double()) {
2132 // Compare left and right as doubles and load the
2133 // resulting flags into the normal status register.
2134 FPURegister left_reg = ToDoubleRegister(left);
2135 FPURegister right_reg = ToDoubleRegister(right);
2136
2137 // If a NaN is involved, i.e. the result is unordered,
2138 // jump to false block label.
2139 __ BranchF(NULL, instr->FalseLabel(chunk_), eq,
2140 left_reg, right_reg);
2141
2142 EmitBranchF(instr, cond, left_reg, right_reg);
2143 } else {
2144 Register cmp_left;
2145 Operand cmp_right = Operand(0);
2146
2147 if (right->IsConstantOperand()) {
2148 int32_t value = ToInteger32(LConstantOperand::cast(right));
2149 if (instr->hydrogen_value()->representation().IsSmi()) {
2150 cmp_left = ToRegister(left);
2151 cmp_right = Operand(Smi::FromInt(value));
2152 } else {
2153 cmp_left = ToRegister(left);
2154 cmp_right = Operand(value);
2155 }
2156 } else if (left->IsConstantOperand()) {
2157 int32_t value = ToInteger32(LConstantOperand::cast(left));
2158 if (instr->hydrogen_value()->representation().IsSmi()) {
2159 cmp_left = ToRegister(right);
2160 cmp_right = Operand(Smi::FromInt(value));
2161 } else {
2162 cmp_left = ToRegister(right);
2163 cmp_right = Operand(value);
2164 }
2165 // We commuted the operands, so commute the condition.
2166 cond = CommuteCondition(cond);
2167 } else {
2168 cmp_left = ToRegister(left);
2169 cmp_right = Operand(ToRegister(right));
2170 }
2171
2172 EmitBranch(instr, cond, cmp_left, cmp_right);
2173 }
2174 }
2175}
2176
2177
2178void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
2179 Register left = ToRegister(instr->left());
2180 Register right = ToRegister(instr->right());
2181
2182 EmitBranch(instr, eq, left, Operand(right));
2183}
2184
2185
2186void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) {
2187 if (instr->hydrogen()->representation().IsTagged()) {
2188 Register input_reg = ToRegister(instr->object());
2189 __ li(at, Operand(factory()->the_hole_value()));
2190 EmitBranch(instr, eq, input_reg, Operand(at));
2191 return;
2192 }
2193
2194 DoubleRegister input_reg = ToDoubleRegister(instr->object());
2195 EmitFalseBranchF(instr, eq, input_reg, input_reg);
2196
2197 Register scratch = scratch0();
2198 __ FmoveHigh(scratch, input_reg);
2199 EmitBranch(instr, eq, scratch, Operand(kHoleNanUpper32));
2200}
2201
2202
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002203Condition LCodeGen::EmitIsString(Register input,
2204 Register temp1,
2205 Label* is_not_string,
2206 SmiCheck check_needed = INLINE_SMI_CHECK) {
2207 if (check_needed == INLINE_SMI_CHECK) {
2208 __ JumpIfSmi(input, is_not_string);
2209 }
2210 __ GetObjectType(input, temp1, temp1);
2211
2212 return lt;
2213}
2214
2215
2216void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
2217 Register reg = ToRegister(instr->value());
2218 Register temp1 = ToRegister(instr->temp());
2219
2220 SmiCheck check_needed =
2221 instr->hydrogen()->value()->type().IsHeapObject()
2222 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2223 Condition true_cond =
2224 EmitIsString(reg, temp1, instr->FalseLabel(chunk_), check_needed);
2225
2226 EmitBranch(instr, true_cond, temp1,
2227 Operand(FIRST_NONSTRING_TYPE));
2228}
2229
2230
2231void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
2232 Register input_reg = EmitLoadRegister(instr->value(), at);
2233 __ And(at, input_reg, kSmiTagMask);
2234 EmitBranch(instr, eq, at, Operand(zero_reg));
2235}
2236
2237
2238void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
2239 Register input = ToRegister(instr->value());
2240 Register temp = ToRegister(instr->temp());
2241
2242 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2243 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2244 }
2245 __ lw(temp, FieldMemOperand(input, HeapObject::kMapOffset));
2246 __ lbu(temp, FieldMemOperand(temp, Map::kBitFieldOffset));
2247 __ And(at, temp, Operand(1 << Map::kIsUndetectable));
2248 EmitBranch(instr, ne, at, Operand(zero_reg));
2249}
2250
2251
2252static Condition ComputeCompareCondition(Token::Value op) {
2253 switch (op) {
2254 case Token::EQ_STRICT:
2255 case Token::EQ:
2256 return eq;
2257 case Token::LT:
2258 return lt;
2259 case Token::GT:
2260 return gt;
2261 case Token::LTE:
2262 return le;
2263 case Token::GTE:
2264 return ge;
2265 default:
2266 UNREACHABLE();
2267 return kNoCondition;
2268 }
2269}
2270
2271
2272void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
2273 DCHECK(ToRegister(instr->context()).is(cp));
2274 DCHECK(ToRegister(instr->left()).is(a1));
2275 DCHECK(ToRegister(instr->right()).is(a0));
2276
2277 Handle<Code> code = CodeFactory::StringCompare(isolate()).code();
2278 CallCode(code, RelocInfo::CODE_TARGET, instr);
2279
2280 EmitBranch(instr, ComputeCompareCondition(instr->op()), v0,
2281 Operand(zero_reg));
2282}
2283
2284
2285static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
2286 InstanceType from = instr->from();
2287 InstanceType to = instr->to();
2288 if (from == FIRST_TYPE) return to;
2289 DCHECK(from == to || to == LAST_TYPE);
2290 return from;
2291}
2292
2293
2294static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
2295 InstanceType from = instr->from();
2296 InstanceType to = instr->to();
2297 if (from == to) return eq;
2298 if (to == LAST_TYPE) return hs;
2299 if (from == FIRST_TYPE) return ls;
2300 UNREACHABLE();
2301 return eq;
2302}
2303
2304
2305void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
2306 Register scratch = scratch0();
2307 Register input = ToRegister(instr->value());
2308
2309 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2310 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2311 }
2312
2313 __ GetObjectType(input, scratch, scratch);
2314 EmitBranch(instr,
2315 BranchCondition(instr->hydrogen()),
2316 scratch,
2317 Operand(TestType(instr->hydrogen())));
2318}
2319
2320
2321void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
2322 Register input = ToRegister(instr->value());
2323 Register result = ToRegister(instr->result());
2324
2325 __ AssertString(input);
2326
2327 __ lw(result, FieldMemOperand(input, String::kHashFieldOffset));
2328 __ IndexFromHash(result, result);
2329}
2330
2331
2332void LCodeGen::DoHasCachedArrayIndexAndBranch(
2333 LHasCachedArrayIndexAndBranch* instr) {
2334 Register input = ToRegister(instr->value());
2335 Register scratch = scratch0();
2336
2337 __ lw(scratch,
2338 FieldMemOperand(input, String::kHashFieldOffset));
2339 __ And(at, scratch, Operand(String::kContainsCachedArrayIndexMask));
2340 EmitBranch(instr, eq, at, Operand(zero_reg));
2341}
2342
2343
2344// Branches to a label or falls through with the answer in flags. Trashes
2345// the temp registers, but not the input.
2346void LCodeGen::EmitClassOfTest(Label* is_true,
2347 Label* is_false,
2348 Handle<String>class_name,
2349 Register input,
2350 Register temp,
2351 Register temp2) {
2352 DCHECK(!input.is(temp));
2353 DCHECK(!input.is(temp2));
2354 DCHECK(!temp.is(temp2));
2355
2356 __ JumpIfSmi(input, is_false);
2357 __ GetObjectType(input, temp, temp2);
2358 if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
2359 __ Branch(is_true, eq, temp2, Operand(JS_FUNCTION_TYPE));
2360 } else {
2361 __ Branch(is_false, eq, temp2, Operand(JS_FUNCTION_TYPE));
2362 }
2363
2364 // Check if the constructor in the map is a function.
2365 Register instance_type = scratch1();
2366 DCHECK(!instance_type.is(temp));
2367 __ GetMapConstructor(temp, temp, temp2, instance_type);
2368
2369 // Objects with a non-function constructor have class 'Object'.
2370 if (String::Equals(class_name, isolate()->factory()->Object_string())) {
2371 __ Branch(is_true, ne, instance_type, Operand(JS_FUNCTION_TYPE));
2372 } else {
2373 __ Branch(is_false, ne, instance_type, Operand(JS_FUNCTION_TYPE));
2374 }
2375
2376 // temp now contains the constructor function. Grab the
2377 // instance class name from there.
2378 __ lw(temp, FieldMemOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2379 __ lw(temp, FieldMemOperand(temp,
2380 SharedFunctionInfo::kInstanceClassNameOffset));
2381 // The class name we are testing against is internalized since it's a literal.
2382 // The name in the constructor is internalized because of the way the context
2383 // is booted. This routine isn't expected to work for random API-created
2384 // classes and it doesn't have to because you can't access it with natives
2385 // syntax. Since both sides are internalized it is sufficient to use an
2386 // identity comparison.
2387
2388 // End with the address of this class_name instance in temp register.
2389 // On MIPS, the caller must do the comparison with Handle<String>class_name.
2390}
2391
2392
2393void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
2394 Register input = ToRegister(instr->value());
2395 Register temp = scratch0();
2396 Register temp2 = ToRegister(instr->temp());
2397 Handle<String> class_name = instr->hydrogen()->class_name();
2398
2399 EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2400 class_name, input, temp, temp2);
2401
2402 EmitBranch(instr, eq, temp, Operand(class_name));
2403}
2404
2405
2406void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
2407 Register reg = ToRegister(instr->value());
2408 Register temp = ToRegister(instr->temp());
2409
2410 __ lw(temp, FieldMemOperand(reg, HeapObject::kMapOffset));
2411 EmitBranch(instr, eq, temp, Operand(instr->map()));
2412}
2413
2414
2415void LCodeGen::DoInstanceOf(LInstanceOf* instr) {
2416 DCHECK(ToRegister(instr->context()).is(cp));
2417 DCHECK(ToRegister(instr->left()).is(InstanceOfDescriptor::LeftRegister()));
2418 DCHECK(ToRegister(instr->right()).is(InstanceOfDescriptor::RightRegister()));
2419 DCHECK(ToRegister(instr->result()).is(v0));
2420 InstanceOfStub stub(isolate());
2421 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
2422}
2423
2424
2425void LCodeGen::DoHasInPrototypeChainAndBranch(
2426 LHasInPrototypeChainAndBranch* instr) {
2427 Register const object = ToRegister(instr->object());
2428 Register const object_map = scratch0();
2429 Register const object_instance_type = scratch1();
2430 Register const object_prototype = object_map;
2431 Register const prototype = ToRegister(instr->prototype());
2432
2433 // The {object} must be a spec object. It's sufficient to know that {object}
2434 // is not a smi, since all other non-spec objects have {null} prototypes and
2435 // will be ruled out below.
2436 if (instr->hydrogen()->ObjectNeedsSmiCheck()) {
2437 __ SmiTst(object, at);
2438 EmitFalseBranch(instr, eq, at, Operand(zero_reg));
2439 }
2440
2441 // Loop through the {object}s prototype chain looking for the {prototype}.
2442 __ lw(object_map, FieldMemOperand(object, HeapObject::kMapOffset));
2443 Label loop;
2444 __ bind(&loop);
2445
2446 // Deoptimize if the object needs to be access checked.
2447 __ lbu(object_instance_type,
2448 FieldMemOperand(object_map, Map::kBitFieldOffset));
2449 __ And(object_instance_type, object_instance_type,
2450 Operand(1 << Map::kIsAccessCheckNeeded));
2451 DeoptimizeIf(ne, instr, Deoptimizer::kAccessCheck, object_instance_type,
2452 Operand(zero_reg));
2453 // Deoptimize for proxies.
2454 __ lbu(object_instance_type,
2455 FieldMemOperand(object_map, Map::kInstanceTypeOffset));
2456 DeoptimizeIf(eq, instr, Deoptimizer::kProxy, object_instance_type,
2457 Operand(JS_PROXY_TYPE));
2458
2459 __ lw(object_prototype, FieldMemOperand(object_map, Map::kPrototypeOffset));
2460 EmitTrueBranch(instr, eq, object_prototype, Operand(prototype));
2461 __ LoadRoot(at, Heap::kNullValueRootIndex);
2462 EmitFalseBranch(instr, eq, object_prototype, Operand(at));
2463 __ Branch(USE_DELAY_SLOT, &loop);
2464 __ lw(object_map, FieldMemOperand(object_prototype, HeapObject::kMapOffset));
2465}
2466
2467
2468void LCodeGen::DoCmpT(LCmpT* instr) {
2469 DCHECK(ToRegister(instr->context()).is(cp));
2470 Token::Value op = instr->op();
2471
Ben Murdoch097c5b22016-05-18 11:27:45 +01002472 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002473 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2474 // On MIPS there is no need for a "no inlined smi code" marker (nop).
2475
2476 Condition condition = ComputeCompareCondition(op);
2477 // A minor optimization that relies on LoadRoot always emitting one
2478 // instruction.
2479 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm());
2480 Label done, check;
2481 __ Branch(USE_DELAY_SLOT, &done, condition, v0, Operand(zero_reg));
2482 __ bind(&check);
2483 __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex);
2484 DCHECK_EQ(1, masm()->InstructionsGeneratedSince(&check));
2485 __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex);
2486 __ bind(&done);
2487}
2488
2489
2490void LCodeGen::DoReturn(LReturn* instr) {
2491 if (FLAG_trace && info()->IsOptimizing()) {
2492 // Push the return value on the stack as the parameter.
2493 // Runtime::TraceExit returns its parameter in v0. We're leaving the code
2494 // managed by the register allocator and tearing down the frame, it's
2495 // safe to write to the context register.
2496 __ push(v0);
2497 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2498 __ CallRuntime(Runtime::kTraceExit);
2499 }
2500 if (info()->saves_caller_doubles()) {
2501 RestoreCallerDoubles();
2502 }
2503 if (NeedsEagerFrame()) {
2504 __ mov(sp, fp);
2505 __ Pop(ra, fp);
2506 }
2507 if (instr->has_constant_parameter_count()) {
2508 int parameter_count = ToInteger32(instr->constant_parameter_count());
2509 int32_t sp_delta = (parameter_count + 1) * kPointerSize;
2510 if (sp_delta != 0) {
2511 __ Addu(sp, sp, Operand(sp_delta));
2512 }
2513 } else {
2514 DCHECK(info()->IsStub()); // Functions would need to drop one more value.
2515 Register reg = ToRegister(instr->parameter_count());
2516 // The argument count parameter is a smi
2517 __ SmiUntag(reg);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002518 __ Lsa(sp, sp, reg, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002519 }
2520
2521 __ Jump(ra);
2522}
2523
2524
2525template <class T>
2526void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
2527 Register vector_register = ToRegister(instr->temp_vector());
2528 Register slot_register = LoadWithVectorDescriptor::SlotRegister();
2529 DCHECK(vector_register.is(LoadWithVectorDescriptor::VectorRegister()));
2530 DCHECK(slot_register.is(a0));
2531
2532 AllowDeferredHandleDereference vector_structure_check;
2533 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2534 __ li(vector_register, vector);
2535 // No need to allocate this register.
2536 FeedbackVectorSlot slot = instr->hydrogen()->slot();
2537 int index = vector->GetIndex(slot);
2538 __ li(slot_register, Operand(Smi::FromInt(index)));
2539}
2540
2541
2542template <class T>
2543void LCodeGen::EmitVectorStoreICRegisters(T* instr) {
2544 Register vector_register = ToRegister(instr->temp_vector());
2545 Register slot_register = ToRegister(instr->temp_slot());
2546
2547 AllowDeferredHandleDereference vector_structure_check;
2548 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2549 __ li(vector_register, vector);
2550 FeedbackVectorSlot slot = instr->hydrogen()->slot();
2551 int index = vector->GetIndex(slot);
2552 __ li(slot_register, Operand(Smi::FromInt(index)));
2553}
2554
2555
2556void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
2557 DCHECK(ToRegister(instr->context()).is(cp));
2558 DCHECK(ToRegister(instr->global_object())
2559 .is(LoadDescriptor::ReceiverRegister()));
2560 DCHECK(ToRegister(instr->result()).is(v0));
2561
2562 __ li(LoadDescriptor::NameRegister(), Operand(instr->name()));
2563 EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002564 Handle<Code> ic = CodeFactory::LoadICInOptimizedCode(
2565 isolate(), instr->typeof_mode(), PREMONOMORPHIC)
2566 .code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002567 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2568}
2569
2570
2571void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
2572 Register context = ToRegister(instr->context());
2573 Register result = ToRegister(instr->result());
2574
2575 __ lw(result, ContextMemOperand(context, instr->slot_index()));
2576 if (instr->hydrogen()->RequiresHoleCheck()) {
2577 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2578
2579 if (instr->hydrogen()->DeoptimizesOnHole()) {
2580 DeoptimizeIf(eq, instr, Deoptimizer::kHole, result, Operand(at));
2581 } else {
2582 Label is_not_hole;
2583 __ Branch(&is_not_hole, ne, result, Operand(at));
2584 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2585 __ bind(&is_not_hole);
2586 }
2587 }
2588}
2589
2590
2591void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
2592 Register context = ToRegister(instr->context());
2593 Register value = ToRegister(instr->value());
2594 Register scratch = scratch0();
2595 MemOperand target = ContextMemOperand(context, instr->slot_index());
2596
2597 Label skip_assignment;
2598
2599 if (instr->hydrogen()->RequiresHoleCheck()) {
2600 __ lw(scratch, target);
2601 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2602
2603 if (instr->hydrogen()->DeoptimizesOnHole()) {
2604 DeoptimizeIf(eq, instr, Deoptimizer::kHole, scratch, Operand(at));
2605 } else {
2606 __ Branch(&skip_assignment, ne, scratch, Operand(at));
2607 }
2608 }
2609
2610 __ sw(value, target);
2611 if (instr->hydrogen()->NeedsWriteBarrier()) {
2612 SmiCheck check_needed =
2613 instr->hydrogen()->value()->type().IsHeapObject()
2614 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2615 __ RecordWriteContextSlot(context,
2616 target.offset(),
2617 value,
2618 scratch0(),
2619 GetRAState(),
2620 kSaveFPRegs,
2621 EMIT_REMEMBERED_SET,
2622 check_needed);
2623 }
2624
2625 __ bind(&skip_assignment);
2626}
2627
2628
2629void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
2630 HObjectAccess access = instr->hydrogen()->access();
2631 int offset = access.offset();
2632 Register object = ToRegister(instr->object());
2633
2634 if (access.IsExternalMemory()) {
2635 Register result = ToRegister(instr->result());
2636 MemOperand operand = MemOperand(object, offset);
2637 __ Load(result, operand, access.representation());
2638 return;
2639 }
2640
2641 if (instr->hydrogen()->representation().IsDouble()) {
2642 DoubleRegister result = ToDoubleRegister(instr->result());
2643 __ ldc1(result, FieldMemOperand(object, offset));
2644 return;
2645 }
2646
2647 Register result = ToRegister(instr->result());
2648 if (!access.IsInobject()) {
2649 __ lw(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
2650 object = result;
2651 }
2652 MemOperand operand = FieldMemOperand(object, offset);
2653 __ Load(result, operand, access.representation());
2654}
2655
2656
2657void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
2658 DCHECK(ToRegister(instr->context()).is(cp));
2659 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
2660 DCHECK(ToRegister(instr->result()).is(v0));
2661
2662 // Name is always in a2.
2663 __ li(LoadDescriptor::NameRegister(), Operand(instr->name()));
2664 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002665 Handle<Code> ic = CodeFactory::LoadICInOptimizedCode(
2666 isolate(), NOT_INSIDE_TYPEOF,
2667 instr->hydrogen()->initialization_state())
2668 .code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002669 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2670}
2671
2672
2673void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
2674 Register scratch = scratch0();
2675 Register function = ToRegister(instr->function());
2676 Register result = ToRegister(instr->result());
2677
2678 // Get the prototype or initial map from the function.
2679 __ lw(result,
2680 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2681
2682 // Check that the function has a prototype or an initial map.
2683 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2684 DeoptimizeIf(eq, instr, Deoptimizer::kHole, result, Operand(at));
2685
2686 // If the function does not have an initial map, we're done.
2687 Label done;
2688 __ GetObjectType(result, scratch, scratch);
2689 __ Branch(&done, ne, scratch, Operand(MAP_TYPE));
2690
2691 // Get the prototype from the initial map.
2692 __ lw(result, FieldMemOperand(result, Map::kPrototypeOffset));
2693
2694 // All done.
2695 __ bind(&done);
2696}
2697
2698
2699void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
2700 Register result = ToRegister(instr->result());
2701 __ LoadRoot(result, instr->index());
2702}
2703
2704
2705void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
2706 Register arguments = ToRegister(instr->arguments());
2707 Register result = ToRegister(instr->result());
2708 // There are two words between the frame pointer and the last argument.
2709 // Subtracting from length accounts for one of them add one more.
2710 if (instr->length()->IsConstantOperand()) {
2711 int const_length = ToInteger32(LConstantOperand::cast(instr->length()));
2712 if (instr->index()->IsConstantOperand()) {
2713 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
2714 int index = (const_length - const_index) + 1;
2715 __ lw(result, MemOperand(arguments, index * kPointerSize));
2716 } else {
2717 Register index = ToRegister(instr->index());
2718 __ li(at, Operand(const_length + 1));
2719 __ Subu(result, at, index);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002720 __ Lsa(at, arguments, result, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002721 __ lw(result, MemOperand(at));
2722 }
2723 } else if (instr->index()->IsConstantOperand()) {
2724 Register length = ToRegister(instr->length());
2725 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
2726 int loc = const_index - 1;
2727 if (loc != 0) {
2728 __ Subu(result, length, Operand(loc));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002729 __ Lsa(at, arguments, result, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002730 __ lw(result, MemOperand(at));
2731 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002732 __ Lsa(at, arguments, length, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002733 __ lw(result, MemOperand(at));
2734 }
2735 } else {
2736 Register length = ToRegister(instr->length());
2737 Register index = ToRegister(instr->index());
2738 __ Subu(result, length, index);
2739 __ Addu(result, result, 1);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002740 __ Lsa(at, arguments, result, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002741 __ lw(result, MemOperand(at));
2742 }
2743}
2744
2745
2746void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
2747 Register external_pointer = ToRegister(instr->elements());
2748 Register key = no_reg;
2749 ElementsKind elements_kind = instr->elements_kind();
2750 bool key_is_constant = instr->key()->IsConstantOperand();
2751 int constant_key = 0;
2752 if (key_is_constant) {
2753 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
2754 if (constant_key & 0xF0000000) {
2755 Abort(kArrayIndexConstantValueTooBig);
2756 }
2757 } else {
2758 key = ToRegister(instr->key());
2759 }
2760 int element_size_shift = ElementsKindToShiftSize(elements_kind);
2761 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
2762 ? (element_size_shift - kSmiTagSize) : element_size_shift;
2763 int base_offset = instr->base_offset();
2764
2765 if (elements_kind == FLOAT32_ELEMENTS || elements_kind == FLOAT64_ELEMENTS) {
2766 FPURegister result = ToDoubleRegister(instr->result());
2767 if (key_is_constant) {
2768 __ Addu(scratch0(), external_pointer, constant_key << element_size_shift);
2769 } else {
2770 __ sll(scratch0(), key, shift_size);
2771 __ Addu(scratch0(), scratch0(), external_pointer);
2772 }
2773 if (elements_kind == FLOAT32_ELEMENTS) {
2774 __ lwc1(result, MemOperand(scratch0(), base_offset));
2775 __ cvt_d_s(result, result);
2776 } else { // i.e. elements_kind == EXTERNAL_DOUBLE_ELEMENTS
2777 __ ldc1(result, MemOperand(scratch0(), base_offset));
2778 }
2779 } else {
2780 Register result = ToRegister(instr->result());
2781 MemOperand mem_operand = PrepareKeyedOperand(
2782 key, external_pointer, key_is_constant, constant_key,
2783 element_size_shift, shift_size, base_offset);
2784 switch (elements_kind) {
2785 case INT8_ELEMENTS:
2786 __ lb(result, mem_operand);
2787 break;
2788 case UINT8_ELEMENTS:
2789 case UINT8_CLAMPED_ELEMENTS:
2790 __ lbu(result, mem_operand);
2791 break;
2792 case INT16_ELEMENTS:
2793 __ lh(result, mem_operand);
2794 break;
2795 case UINT16_ELEMENTS:
2796 __ lhu(result, mem_operand);
2797 break;
2798 case INT32_ELEMENTS:
2799 __ lw(result, mem_operand);
2800 break;
2801 case UINT32_ELEMENTS:
2802 __ lw(result, mem_operand);
2803 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
2804 DeoptimizeIf(Ugreater_equal, instr, Deoptimizer::kNegativeValue,
2805 result, Operand(0x80000000));
2806 }
2807 break;
2808 case FLOAT32_ELEMENTS:
2809 case FLOAT64_ELEMENTS:
2810 case FAST_DOUBLE_ELEMENTS:
2811 case FAST_ELEMENTS:
2812 case FAST_SMI_ELEMENTS:
2813 case FAST_HOLEY_DOUBLE_ELEMENTS:
2814 case FAST_HOLEY_ELEMENTS:
2815 case FAST_HOLEY_SMI_ELEMENTS:
2816 case DICTIONARY_ELEMENTS:
2817 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
2818 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002819 case FAST_STRING_WRAPPER_ELEMENTS:
2820 case SLOW_STRING_WRAPPER_ELEMENTS:
2821 case NO_ELEMENTS:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002822 UNREACHABLE();
2823 break;
2824 }
2825 }
2826}
2827
2828
2829void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
2830 Register elements = ToRegister(instr->elements());
2831 bool key_is_constant = instr->key()->IsConstantOperand();
2832 Register key = no_reg;
2833 DoubleRegister result = ToDoubleRegister(instr->result());
2834 Register scratch = scratch0();
2835
2836 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
2837
2838 int base_offset = instr->base_offset();
2839 if (key_is_constant) {
2840 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
2841 if (constant_key & 0xF0000000) {
2842 Abort(kArrayIndexConstantValueTooBig);
2843 }
2844 base_offset += constant_key * kDoubleSize;
2845 }
2846 __ Addu(scratch, elements, Operand(base_offset));
2847
2848 if (!key_is_constant) {
2849 key = ToRegister(instr->key());
2850 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
2851 ? (element_size_shift - kSmiTagSize) : element_size_shift;
Ben Murdoch097c5b22016-05-18 11:27:45 +01002852 __ Lsa(scratch, scratch, key, shift_size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002853 }
2854
2855 __ ldc1(result, MemOperand(scratch));
2856
2857 if (instr->hydrogen()->RequiresHoleCheck()) {
2858 __ lw(scratch, MemOperand(scratch, kHoleNanUpper32Offset));
2859 DeoptimizeIf(eq, instr, Deoptimizer::kHole, scratch,
2860 Operand(kHoleNanUpper32));
2861 }
2862}
2863
2864
2865void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
2866 Register elements = ToRegister(instr->elements());
2867 Register result = ToRegister(instr->result());
2868 Register scratch = scratch0();
2869 Register store_base = scratch;
2870 int offset = instr->base_offset();
2871
2872 if (instr->key()->IsConstantOperand()) {
2873 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
2874 offset += ToInteger32(const_operand) * kPointerSize;
2875 store_base = elements;
2876 } else {
2877 Register key = ToRegister(instr->key());
2878 // Even though the HLoadKeyed instruction forces the input
2879 // representation for the key to be an integer, the input gets replaced
2880 // during bound check elimination with the index argument to the bounds
2881 // check, which can be tagged, so that case must be handled here, too.
2882 if (instr->hydrogen()->key()->representation().IsSmi()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002883 __ Lsa(scratch, elements, key, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002884 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002885 __ Lsa(scratch, elements, key, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002886 }
2887 }
2888 __ lw(result, MemOperand(store_base, offset));
2889
2890 // Check for the hole value.
2891 if (instr->hydrogen()->RequiresHoleCheck()) {
2892 if (IsFastSmiElementsKind(instr->hydrogen()->elements_kind())) {
2893 __ SmiTst(result, scratch);
2894 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi, scratch,
2895 Operand(zero_reg));
2896 } else {
2897 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
2898 DeoptimizeIf(eq, instr, Deoptimizer::kHole, result, Operand(scratch));
2899 }
2900 } else if (instr->hydrogen()->hole_mode() == CONVERT_HOLE_TO_UNDEFINED) {
2901 DCHECK(instr->hydrogen()->elements_kind() == FAST_HOLEY_ELEMENTS);
2902 Label done;
2903 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
2904 __ Branch(&done, ne, result, Operand(scratch));
2905 if (info()->IsStub()) {
2906 // A stub can safely convert the hole to undefined only if the array
2907 // protector cell contains (Smi) Isolate::kArrayProtectorValid. Otherwise
2908 // it needs to bail out.
2909 __ LoadRoot(result, Heap::kArrayProtectorRootIndex);
2910 __ lw(result, FieldMemOperand(result, Cell::kValueOffset));
2911 DeoptimizeIf(ne, instr, Deoptimizer::kHole, result,
2912 Operand(Smi::FromInt(Isolate::kArrayProtectorValid)));
2913 }
2914 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2915 __ bind(&done);
2916 }
2917}
2918
2919
2920void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
2921 if (instr->is_fixed_typed_array()) {
2922 DoLoadKeyedExternalArray(instr);
2923 } else if (instr->hydrogen()->representation().IsDouble()) {
2924 DoLoadKeyedFixedDoubleArray(instr);
2925 } else {
2926 DoLoadKeyedFixedArray(instr);
2927 }
2928}
2929
2930
2931MemOperand LCodeGen::PrepareKeyedOperand(Register key,
2932 Register base,
2933 bool key_is_constant,
2934 int constant_key,
2935 int element_size,
2936 int shift_size,
2937 int base_offset) {
2938 if (key_is_constant) {
2939 return MemOperand(base, (constant_key << element_size) + base_offset);
2940 }
2941
2942 if (base_offset == 0) {
2943 if (shift_size >= 0) {
2944 __ sll(scratch0(), key, shift_size);
2945 __ Addu(scratch0(), base, scratch0());
2946 return MemOperand(scratch0());
2947 } else {
2948 DCHECK_EQ(-1, shift_size);
2949 __ srl(scratch0(), key, 1);
2950 __ Addu(scratch0(), base, scratch0());
2951 return MemOperand(scratch0());
2952 }
2953 }
2954
2955 if (shift_size >= 0) {
2956 __ sll(scratch0(), key, shift_size);
2957 __ Addu(scratch0(), base, scratch0());
2958 return MemOperand(scratch0(), base_offset);
2959 } else {
2960 DCHECK_EQ(-1, shift_size);
2961 __ sra(scratch0(), key, 1);
2962 __ Addu(scratch0(), base, scratch0());
2963 return MemOperand(scratch0(), base_offset);
2964 }
2965}
2966
2967
2968void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
2969 DCHECK(ToRegister(instr->context()).is(cp));
2970 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
2971 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
2972
2973 if (instr->hydrogen()->HasVectorAndSlot()) {
2974 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
2975 }
2976
2977 Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(
Ben Murdoch097c5b22016-05-18 11:27:45 +01002978 isolate(), instr->hydrogen()->initialization_state())
2979 .code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002980 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2981}
2982
2983
2984void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
2985 Register scratch = scratch0();
2986 Register temp = scratch1();
2987 Register result = ToRegister(instr->result());
2988
2989 if (instr->hydrogen()->from_inlined()) {
2990 __ Subu(result, sp, 2 * kPointerSize);
2991 } else {
2992 // Check if the calling frame is an arguments adaptor frame.
2993 Label done, adapted;
2994 __ lw(scratch, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2995 __ lw(result, MemOperand(scratch, StandardFrameConstants::kContextOffset));
2996 __ Xor(temp, result, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2997
2998 // Result is the frame pointer for the frame if not adapted and for the real
2999 // frame below the adaptor frame if adapted.
3000 __ Movn(result, fp, temp); // Move only if temp is not equal to zero (ne).
3001 __ Movz(result, scratch, temp); // Move only if temp is equal to zero (eq).
3002 }
3003}
3004
3005
3006void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
3007 Register elem = ToRegister(instr->elements());
3008 Register result = ToRegister(instr->result());
3009
3010 Label done;
3011
3012 // If no arguments adaptor frame the number of arguments is fixed.
3013 __ Addu(result, zero_reg, Operand(scope()->num_parameters()));
3014 __ Branch(&done, eq, fp, Operand(elem));
3015
3016 // Arguments adaptor frame present. Get argument length from there.
3017 __ lw(result, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3018 __ lw(result,
3019 MemOperand(result, ArgumentsAdaptorFrameConstants::kLengthOffset));
3020 __ SmiUntag(result);
3021
3022 // Argument length is in result register.
3023 __ bind(&done);
3024}
3025
3026
3027void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
3028 Register receiver = ToRegister(instr->receiver());
3029 Register function = ToRegister(instr->function());
3030 Register result = ToRegister(instr->result());
3031 Register scratch = scratch0();
3032
3033 // If the receiver is null or undefined, we have to pass the global
3034 // object as a receiver to normal functions. Values have to be
3035 // passed unchanged to builtins and strict-mode functions.
3036 Label global_object, result_in_receiver;
3037
3038 if (!instr->hydrogen()->known_function()) {
3039 // Do not transform the receiver to object for strict mode
3040 // functions.
3041 __ lw(scratch,
3042 FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
3043 __ lw(scratch,
3044 FieldMemOperand(scratch, SharedFunctionInfo::kCompilerHintsOffset));
3045
3046 // Do not transform the receiver to object for builtins.
3047 int32_t strict_mode_function_mask =
3048 1 << (SharedFunctionInfo::kStrictModeFunction + kSmiTagSize);
3049 int32_t native_mask = 1 << (SharedFunctionInfo::kNative + kSmiTagSize);
3050 __ And(scratch, scratch, Operand(strict_mode_function_mask | native_mask));
3051 __ Branch(&result_in_receiver, ne, scratch, Operand(zero_reg));
3052 }
3053
3054 // Normal function. Replace undefined or null with global receiver.
3055 __ LoadRoot(scratch, Heap::kNullValueRootIndex);
3056 __ Branch(&global_object, eq, receiver, Operand(scratch));
3057 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
3058 __ Branch(&global_object, eq, receiver, Operand(scratch));
3059
3060 // Deoptimize if the receiver is not a JS object.
3061 __ SmiTst(receiver, scratch);
3062 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, scratch, Operand(zero_reg));
3063
3064 __ GetObjectType(receiver, scratch, scratch);
3065 DeoptimizeIf(lt, instr, Deoptimizer::kNotAJavaScriptObject, scratch,
3066 Operand(FIRST_JS_RECEIVER_TYPE));
3067
3068 __ Branch(&result_in_receiver);
3069 __ bind(&global_object);
3070 __ lw(result, FieldMemOperand(function, JSFunction::kContextOffset));
3071 __ lw(result, ContextMemOperand(result, Context::NATIVE_CONTEXT_INDEX));
3072 __ lw(result, ContextMemOperand(result, Context::GLOBAL_PROXY_INDEX));
3073
3074 if (result.is(receiver)) {
3075 __ bind(&result_in_receiver);
3076 } else {
3077 Label result_ok;
3078 __ Branch(&result_ok);
3079 __ bind(&result_in_receiver);
3080 __ mov(result, receiver);
3081 __ bind(&result_ok);
3082 }
3083}
3084
3085
3086void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3087 Register receiver = ToRegister(instr->receiver());
3088 Register function = ToRegister(instr->function());
3089 Register length = ToRegister(instr->length());
3090 Register elements = ToRegister(instr->elements());
3091 Register scratch = scratch0();
3092 DCHECK(receiver.is(a0)); // Used for parameter count.
3093 DCHECK(function.is(a1)); // Required by InvokeFunction.
3094 DCHECK(ToRegister(instr->result()).is(v0));
3095
3096 // Copy the arguments to this function possibly from the
3097 // adaptor frame below it.
3098 const uint32_t kArgumentsLimit = 1 * KB;
3099 DeoptimizeIf(hi, instr, Deoptimizer::kTooManyArguments, length,
3100 Operand(kArgumentsLimit));
3101
3102 // Push the receiver and use the register to keep the original
3103 // number of arguments.
3104 __ push(receiver);
3105 __ Move(receiver, length);
3106 // The arguments are at a one pointer size offset from elements.
3107 __ Addu(elements, elements, Operand(1 * kPointerSize));
3108
3109 // Loop through the arguments pushing them onto the execution
3110 // stack.
3111 Label invoke, loop;
3112 // length is a small non-negative integer, due to the test above.
3113 __ Branch(USE_DELAY_SLOT, &invoke, eq, length, Operand(zero_reg));
3114 __ sll(scratch, length, 2);
3115 __ bind(&loop);
3116 __ Addu(scratch, elements, scratch);
3117 __ lw(scratch, MemOperand(scratch));
3118 __ push(scratch);
3119 __ Subu(length, length, Operand(1));
3120 __ Branch(USE_DELAY_SLOT, &loop, ne, length, Operand(zero_reg));
3121 __ sll(scratch, length, 2);
3122
3123 __ bind(&invoke);
3124 DCHECK(instr->HasPointerMap());
3125 LPointerMap* pointers = instr->pointer_map();
3126 SafepointGenerator safepoint_generator(
3127 this, pointers, Safepoint::kLazyDeopt);
3128 // The number of arguments is stored in receiver which is a0, as expected
3129 // by InvokeFunction.
3130 ParameterCount actual(receiver);
3131 __ InvokeFunction(function, no_reg, actual, CALL_FUNCTION,
3132 safepoint_generator);
3133}
3134
3135
3136void LCodeGen::DoPushArgument(LPushArgument* instr) {
3137 LOperand* argument = instr->value();
3138 if (argument->IsDoubleRegister() || argument->IsDoubleStackSlot()) {
3139 Abort(kDoPushArgumentNotImplementedForDoubleType);
3140 } else {
3141 Register argument_reg = EmitLoadRegister(argument, at);
3142 __ push(argument_reg);
3143 }
3144}
3145
3146
3147void LCodeGen::DoDrop(LDrop* instr) {
3148 __ Drop(instr->count());
3149}
3150
3151
3152void LCodeGen::DoThisFunction(LThisFunction* instr) {
3153 Register result = ToRegister(instr->result());
3154 __ lw(result, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3155}
3156
3157
3158void LCodeGen::DoContext(LContext* instr) {
3159 // If there is a non-return use, the context must be moved to a register.
3160 Register result = ToRegister(instr->result());
3161 if (info()->IsOptimizing()) {
3162 __ lw(result, MemOperand(fp, StandardFrameConstants::kContextOffset));
3163 } else {
3164 // If there is no frame, the context must be in cp.
3165 DCHECK(result.is(cp));
3166 }
3167}
3168
3169
3170void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
3171 DCHECK(ToRegister(instr->context()).is(cp));
3172 __ li(scratch0(), instr->hydrogen()->pairs());
3173 __ li(scratch1(), Operand(Smi::FromInt(instr->hydrogen()->flags())));
3174 __ Push(scratch0(), scratch1());
3175 CallRuntime(Runtime::kDeclareGlobals, instr);
3176}
3177
3178
3179void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
3180 int formal_parameter_count, int arity,
3181 LInstruction* instr) {
3182 bool dont_adapt_arguments =
3183 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3184 bool can_invoke_directly =
3185 dont_adapt_arguments || formal_parameter_count == arity;
3186
3187 Register function_reg = a1;
3188 LPointerMap* pointers = instr->pointer_map();
3189
3190 if (can_invoke_directly) {
3191 // Change context.
3192 __ lw(cp, FieldMemOperand(function_reg, JSFunction::kContextOffset));
3193
3194 // Always initialize new target and number of actual arguments.
3195 __ LoadRoot(a3, Heap::kUndefinedValueRootIndex);
3196 __ li(a0, Operand(arity));
3197
3198 // Invoke function.
3199 __ lw(at, FieldMemOperand(function_reg, JSFunction::kCodeEntryOffset));
3200 __ Call(at);
3201
3202 // Set up deoptimization.
3203 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3204 } else {
3205 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3206 ParameterCount count(arity);
3207 ParameterCount expected(formal_parameter_count);
3208 __ InvokeFunction(function_reg, expected, count, CALL_FUNCTION, generator);
3209 }
3210}
3211
3212
3213void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3214 DCHECK(instr->context() != NULL);
3215 DCHECK(ToRegister(instr->context()).is(cp));
3216 Register input = ToRegister(instr->value());
3217 Register result = ToRegister(instr->result());
3218 Register scratch = scratch0();
3219
3220 // Deoptimize if not a heap number.
3221 __ lw(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
3222 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
3223 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, scratch, Operand(at));
3224
3225 Label done;
3226 Register exponent = scratch0();
3227 scratch = no_reg;
3228 __ lw(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3229 // Check the sign of the argument. If the argument is positive, just
3230 // return it.
3231 __ Move(result, input);
3232 __ And(at, exponent, Operand(HeapNumber::kSignMask));
3233 __ Branch(&done, eq, at, Operand(zero_reg));
3234
3235 // Input is negative. Reverse its sign.
3236 // Preserve the value of all registers.
3237 {
3238 PushSafepointRegistersScope scope(this);
3239
3240 // Registers were saved at the safepoint, so we can use
3241 // many scratch registers.
3242 Register tmp1 = input.is(a1) ? a0 : a1;
3243 Register tmp2 = input.is(a2) ? a0 : a2;
3244 Register tmp3 = input.is(a3) ? a0 : a3;
3245 Register tmp4 = input.is(t0) ? a0 : t0;
3246
3247 // exponent: floating point exponent value.
3248
3249 Label allocated, slow;
3250 __ LoadRoot(tmp4, Heap::kHeapNumberMapRootIndex);
3251 __ AllocateHeapNumber(tmp1, tmp2, tmp3, tmp4, &slow);
3252 __ Branch(&allocated);
3253
3254 // Slow case: Call the runtime system to do the number allocation.
3255 __ bind(&slow);
3256
3257 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr,
3258 instr->context());
3259 // Set the pointer to the new heap number in tmp.
3260 if (!tmp1.is(v0))
3261 __ mov(tmp1, v0);
3262 // Restore input_reg after call to runtime.
3263 __ LoadFromSafepointRegisterSlot(input, input);
3264 __ lw(exponent, FieldMemOperand(input, HeapNumber::kExponentOffset));
3265
3266 __ bind(&allocated);
3267 // exponent: floating point exponent value.
3268 // tmp1: allocated heap number.
3269 __ And(exponent, exponent, Operand(~HeapNumber::kSignMask));
3270 __ sw(exponent, FieldMemOperand(tmp1, HeapNumber::kExponentOffset));
3271 __ lw(tmp2, FieldMemOperand(input, HeapNumber::kMantissaOffset));
3272 __ sw(tmp2, FieldMemOperand(tmp1, HeapNumber::kMantissaOffset));
3273
3274 __ StoreToSafepointRegisterSlot(tmp1, result);
3275 }
3276
3277 __ bind(&done);
3278}
3279
3280
3281void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3282 Register input = ToRegister(instr->value());
3283 Register result = ToRegister(instr->result());
3284 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
3285 Label done;
3286 __ Branch(USE_DELAY_SLOT, &done, ge, input, Operand(zero_reg));
3287 __ mov(result, input);
3288 __ subu(result, zero_reg, input);
3289 // Overflow if result is still negative, i.e. 0x80000000.
3290 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, result, Operand(zero_reg));
3291 __ bind(&done);
3292}
3293
3294
3295void LCodeGen::DoMathAbs(LMathAbs* instr) {
3296 // Class for deferred case.
3297 class DeferredMathAbsTaggedHeapNumber final : public LDeferredCode {
3298 public:
3299 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr)
3300 : LDeferredCode(codegen), instr_(instr) { }
3301 void Generate() override {
3302 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3303 }
3304 LInstruction* instr() override { return instr_; }
3305
3306 private:
3307 LMathAbs* instr_;
3308 };
3309
3310 Representation r = instr->hydrogen()->value()->representation();
3311 if (r.IsDouble()) {
3312 FPURegister input = ToDoubleRegister(instr->value());
3313 FPURegister result = ToDoubleRegister(instr->result());
3314 __ abs_d(result, input);
3315 } else if (r.IsSmiOrInteger32()) {
3316 EmitIntegerMathAbs(instr);
3317 } else {
3318 // Representation is tagged.
3319 DeferredMathAbsTaggedHeapNumber* deferred =
3320 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3321 Register input = ToRegister(instr->value());
3322 // Smi check.
3323 __ JumpIfNotSmi(input, deferred->entry());
3324 // If smi, handle it directly.
3325 EmitIntegerMathAbs(instr);
3326 __ bind(deferred->exit());
3327 }
3328}
3329
3330
3331void LCodeGen::DoMathFloor(LMathFloor* instr) {
3332 DoubleRegister input = ToDoubleRegister(instr->value());
3333 Register result = ToRegister(instr->result());
3334 Register scratch1 = scratch0();
3335 Register except_flag = ToRegister(instr->temp());
3336
3337 __ EmitFPUTruncate(kRoundToMinusInf,
3338 result,
3339 input,
3340 scratch1,
3341 double_scratch0(),
3342 except_flag);
3343
3344 // Deopt if the operation did not succeed.
3345 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
3346 Operand(zero_reg));
3347
3348 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3349 // Test for -0.
3350 Label done;
3351 __ Branch(&done, ne, result, Operand(zero_reg));
3352 __ Mfhc1(scratch1, input);
3353 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
3354 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
3355 Operand(zero_reg));
3356 __ bind(&done);
3357 }
3358}
3359
3360
3361void LCodeGen::DoMathRound(LMathRound* instr) {
3362 DoubleRegister input = ToDoubleRegister(instr->value());
3363 Register result = ToRegister(instr->result());
3364 DoubleRegister double_scratch1 = ToDoubleRegister(instr->temp());
3365 Register scratch = scratch0();
3366 Label done, check_sign_on_zero;
3367
3368 // Extract exponent bits.
3369 __ Mfhc1(result, input);
3370 __ Ext(scratch,
3371 result,
3372 HeapNumber::kExponentShift,
3373 HeapNumber::kExponentBits);
3374
3375 // If the number is in ]-0.5, +0.5[, the result is +/- 0.
3376 Label skip1;
3377 __ Branch(&skip1, gt, scratch, Operand(HeapNumber::kExponentBias - 2));
3378 __ mov(result, zero_reg);
3379 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3380 __ Branch(&check_sign_on_zero);
3381 } else {
3382 __ Branch(&done);
3383 }
3384 __ bind(&skip1);
3385
3386 // The following conversion will not work with numbers
3387 // outside of ]-2^32, 2^32[.
3388 DeoptimizeIf(ge, instr, Deoptimizer::kOverflow, scratch,
3389 Operand(HeapNumber::kExponentBias + 32));
3390
3391 // Save the original sign for later comparison.
3392 __ And(scratch, result, Operand(HeapNumber::kSignMask));
3393
3394 __ Move(double_scratch0(), 0.5);
3395 __ add_d(double_scratch0(), input, double_scratch0());
3396
3397 // Check sign of the result: if the sign changed, the input
3398 // value was in ]0.5, 0[ and the result should be -0.
3399 __ Mfhc1(result, double_scratch0());
3400 __ Xor(result, result, Operand(scratch));
3401 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3402 // ARM uses 'mi' here, which is 'lt'
3403 DeoptimizeIf(lt, instr, Deoptimizer::kMinusZero, result, Operand(zero_reg));
3404 } else {
3405 Label skip2;
3406 // ARM uses 'mi' here, which is 'lt'
3407 // Negating it results in 'ge'
3408 __ Branch(&skip2, ge, result, Operand(zero_reg));
3409 __ mov(result, zero_reg);
3410 __ Branch(&done);
3411 __ bind(&skip2);
3412 }
3413
3414 Register except_flag = scratch;
3415 __ EmitFPUTruncate(kRoundToMinusInf,
3416 result,
3417 double_scratch0(),
3418 at,
3419 double_scratch1,
3420 except_flag);
3421
3422 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
3423 Operand(zero_reg));
3424
3425 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3426 // Test for -0.
3427 __ Branch(&done, ne, result, Operand(zero_reg));
3428 __ bind(&check_sign_on_zero);
3429 __ Mfhc1(scratch, input);
3430 __ And(scratch, scratch, Operand(HeapNumber::kSignMask));
3431 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch,
3432 Operand(zero_reg));
3433 }
3434 __ bind(&done);
3435}
3436
3437
3438void LCodeGen::DoMathFround(LMathFround* instr) {
3439 DoubleRegister input = ToDoubleRegister(instr->value());
3440 DoubleRegister result = ToDoubleRegister(instr->result());
3441 __ cvt_s_d(result.low(), input);
3442 __ cvt_d_s(result, result.low());
3443}
3444
3445
3446void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3447 DoubleRegister input = ToDoubleRegister(instr->value());
3448 DoubleRegister result = ToDoubleRegister(instr->result());
3449 __ sqrt_d(result, input);
3450}
3451
3452
3453void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3454 DoubleRegister input = ToDoubleRegister(instr->value());
3455 DoubleRegister result = ToDoubleRegister(instr->result());
3456 DoubleRegister temp = ToDoubleRegister(instr->temp());
3457
3458 DCHECK(!input.is(result));
3459
3460 // Note that according to ECMA-262 15.8.2.13:
3461 // Math.pow(-Infinity, 0.5) == Infinity
3462 // Math.sqrt(-Infinity) == NaN
3463 Label done;
3464 __ Move(temp, static_cast<double>(-V8_INFINITY));
3465 __ BranchF(USE_DELAY_SLOT, &done, NULL, eq, temp, input);
3466 // Set up Infinity in the delay slot.
3467 // result is overwritten if the branch is not taken.
3468 __ neg_d(result, temp);
3469
3470 // Add +0 to convert -0 to +0.
3471 __ add_d(result, input, kDoubleRegZero);
3472 __ sqrt_d(result, result);
3473 __ bind(&done);
3474}
3475
3476
3477void LCodeGen::DoPower(LPower* instr) {
3478 Representation exponent_type = instr->hydrogen()->right()->representation();
3479 // Having marked this as a call, we can use any registers.
3480 // Just make sure that the input/output registers are the expected ones.
3481 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3482 DCHECK(!instr->right()->IsDoubleRegister() ||
3483 ToDoubleRegister(instr->right()).is(f4));
3484 DCHECK(!instr->right()->IsRegister() ||
3485 ToRegister(instr->right()).is(tagged_exponent));
3486 DCHECK(ToDoubleRegister(instr->left()).is(f2));
3487 DCHECK(ToDoubleRegister(instr->result()).is(f0));
3488
3489 if (exponent_type.IsSmi()) {
3490 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3491 __ CallStub(&stub);
3492 } else if (exponent_type.IsTagged()) {
3493 Label no_deopt;
3494 __ JumpIfSmi(tagged_exponent, &no_deopt);
3495 DCHECK(!t3.is(tagged_exponent));
3496 __ lw(t3, FieldMemOperand(tagged_exponent, HeapObject::kMapOffset));
3497 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
3498 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, t3, Operand(at));
3499 __ bind(&no_deopt);
3500 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3501 __ CallStub(&stub);
3502 } else if (exponent_type.IsInteger32()) {
3503 MathPowStub stub(isolate(), MathPowStub::INTEGER);
3504 __ CallStub(&stub);
3505 } else {
3506 DCHECK(exponent_type.IsDouble());
3507 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
3508 __ CallStub(&stub);
3509 }
3510}
3511
3512
3513void LCodeGen::DoMathExp(LMathExp* instr) {
3514 DoubleRegister input = ToDoubleRegister(instr->value());
3515 DoubleRegister result = ToDoubleRegister(instr->result());
3516 DoubleRegister double_scratch1 = ToDoubleRegister(instr->double_temp());
3517 DoubleRegister double_scratch2 = double_scratch0();
3518 Register temp1 = ToRegister(instr->temp1());
3519 Register temp2 = ToRegister(instr->temp2());
3520
3521 MathExpGenerator::EmitMathExp(
3522 masm(), input, result, double_scratch1, double_scratch2,
3523 temp1, temp2, scratch0());
3524}
3525
3526
3527void LCodeGen::DoMathLog(LMathLog* instr) {
3528 __ PrepareCallCFunction(0, 1, scratch0());
3529 __ MovToFloatParameter(ToDoubleRegister(instr->value()));
3530 __ CallCFunction(ExternalReference::math_log_double_function(isolate()),
3531 0, 1);
3532 __ MovFromFloatResult(ToDoubleRegister(instr->result()));
3533}
3534
3535
3536void LCodeGen::DoMathClz32(LMathClz32* instr) {
3537 Register input = ToRegister(instr->value());
3538 Register result = ToRegister(instr->result());
3539 __ Clz(result, input);
3540}
3541
3542
3543void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
3544 DCHECK(ToRegister(instr->context()).is(cp));
3545 DCHECK(ToRegister(instr->function()).is(a1));
3546 DCHECK(instr->HasPointerMap());
3547
3548 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
3549 if (known_function.is_null()) {
3550 LPointerMap* pointers = instr->pointer_map();
3551 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3552 ParameterCount count(instr->arity());
3553 __ InvokeFunction(a1, no_reg, count, CALL_FUNCTION, generator);
3554 } else {
3555 CallKnownFunction(known_function,
3556 instr->hydrogen()->formal_parameter_count(),
3557 instr->arity(), instr);
3558 }
3559}
3560
3561
3562void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
3563 DCHECK(ToRegister(instr->result()).is(v0));
3564
3565 if (instr->hydrogen()->IsTailCall()) {
3566 if (NeedsEagerFrame()) __ LeaveFrame(StackFrame::INTERNAL);
3567
3568 if (instr->target()->IsConstantOperand()) {
3569 LConstantOperand* target = LConstantOperand::cast(instr->target());
3570 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3571 __ Jump(code, RelocInfo::CODE_TARGET);
3572 } else {
3573 DCHECK(instr->target()->IsRegister());
3574 Register target = ToRegister(instr->target());
3575 __ Addu(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
3576 __ Jump(target);
3577 }
3578 } else {
3579 LPointerMap* pointers = instr->pointer_map();
3580 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3581
3582 if (instr->target()->IsConstantOperand()) {
3583 LConstantOperand* target = LConstantOperand::cast(instr->target());
3584 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3585 generator.BeforeCall(__ CallSize(code, RelocInfo::CODE_TARGET));
3586 __ Call(code, RelocInfo::CODE_TARGET);
3587 } else {
3588 DCHECK(instr->target()->IsRegister());
3589 Register target = ToRegister(instr->target());
3590 generator.BeforeCall(__ CallSize(target));
3591 __ Addu(target, target, Operand(Code::kHeaderSize - kHeapObjectTag));
3592 __ Call(target);
3593 }
3594 generator.AfterCall();
3595 }
3596}
3597
3598
3599void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
3600 DCHECK(ToRegister(instr->function()).is(a1));
3601 DCHECK(ToRegister(instr->result()).is(v0));
3602
3603 // Change context.
3604 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
3605
3606 // Always initialize new target and number of actual arguments.
3607 __ LoadRoot(a3, Heap::kUndefinedValueRootIndex);
3608 __ li(a0, Operand(instr->arity()));
3609
3610 // Load the code entry address
3611 __ lw(at, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
3612 __ Call(at);
3613
3614 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
3615}
3616
3617
3618void LCodeGen::DoCallFunction(LCallFunction* instr) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003619 HCallFunction* hinstr = instr->hydrogen();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003620 DCHECK(ToRegister(instr->context()).is(cp));
3621 DCHECK(ToRegister(instr->function()).is(a1));
3622 DCHECK(ToRegister(instr->result()).is(v0));
3623
3624 int arity = instr->arity();
Ben Murdoch097c5b22016-05-18 11:27:45 +01003625 ConvertReceiverMode mode = hinstr->convert_mode();
3626 if (hinstr->HasVectorAndSlot()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003627 Register slot_register = ToRegister(instr->temp_slot());
3628 Register vector_register = ToRegister(instr->temp_vector());
3629 DCHECK(slot_register.is(a3));
3630 DCHECK(vector_register.is(a2));
3631
3632 AllowDeferredHandleDereference vector_structure_check;
Ben Murdoch097c5b22016-05-18 11:27:45 +01003633 Handle<TypeFeedbackVector> vector = hinstr->feedback_vector();
3634 int index = vector->GetIndex(hinstr->slot());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003635
3636 __ li(vector_register, vector);
3637 __ li(slot_register, Operand(Smi::FromInt(index)));
3638
3639 Handle<Code> ic =
3640 CodeFactory::CallICInOptimizedCode(isolate(), arity, mode).code();
3641 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3642 } else {
3643 __ li(a0, Operand(arity));
3644 CallCode(isolate()->builtins()->Call(mode), RelocInfo::CODE_TARGET, instr);
3645 }
3646}
3647
3648
3649void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
3650 DCHECK(ToRegister(instr->context()).is(cp));
3651 DCHECK(ToRegister(instr->constructor()).is(a1));
3652 DCHECK(ToRegister(instr->result()).is(v0));
3653
3654 __ li(a0, Operand(instr->arity()));
3655 if (instr->arity() == 1) {
3656 // We only need the allocation site for the case we have a length argument.
3657 // The case may bail out to the runtime, which will determine the correct
3658 // elements kind with the site.
3659 __ li(a2, instr->hydrogen()->site());
3660 } else {
3661 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
3662 }
3663 ElementsKind kind = instr->hydrogen()->elements_kind();
3664 AllocationSiteOverrideMode override_mode =
3665 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
3666 ? DISABLE_ALLOCATION_SITES
3667 : DONT_OVERRIDE;
3668
3669 if (instr->arity() == 0) {
3670 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
3671 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3672 } else if (instr->arity() == 1) {
3673 Label done;
3674 if (IsFastPackedElementsKind(kind)) {
3675 Label packed_case;
3676 // We might need a change here,
3677 // look at the first argument.
3678 __ lw(t1, MemOperand(sp, 0));
3679 __ Branch(&packed_case, eq, t1, Operand(zero_reg));
3680
3681 ElementsKind holey_kind = GetHoleyElementsKind(kind);
3682 ArraySingleArgumentConstructorStub stub(isolate(),
3683 holey_kind,
3684 override_mode);
3685 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3686 __ jmp(&done);
3687 __ bind(&packed_case);
3688 }
3689
3690 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
3691 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3692 __ bind(&done);
3693 } else {
3694 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
3695 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3696 }
3697}
3698
3699
3700void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
3701 CallRuntime(instr->function(), instr->arity(), instr);
3702}
3703
3704
3705void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
3706 Register function = ToRegister(instr->function());
3707 Register code_object = ToRegister(instr->code_object());
3708 __ Addu(code_object, code_object,
3709 Operand(Code::kHeaderSize - kHeapObjectTag));
3710 __ sw(code_object,
3711 FieldMemOperand(function, JSFunction::kCodeEntryOffset));
3712}
3713
3714
3715void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
3716 Register result = ToRegister(instr->result());
3717 Register base = ToRegister(instr->base_object());
3718 if (instr->offset()->IsConstantOperand()) {
3719 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
3720 __ Addu(result, base, Operand(ToInteger32(offset)));
3721 } else {
3722 Register offset = ToRegister(instr->offset());
3723 __ Addu(result, base, offset);
3724 }
3725}
3726
3727
3728void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
3729 Representation representation = instr->representation();
3730
3731 Register object = ToRegister(instr->object());
3732 Register scratch = scratch0();
3733 HObjectAccess access = instr->hydrogen()->access();
3734 int offset = access.offset();
3735
3736 if (access.IsExternalMemory()) {
3737 Register value = ToRegister(instr->value());
3738 MemOperand operand = MemOperand(object, offset);
3739 __ Store(value, operand, representation);
3740 return;
3741 }
3742
3743 __ AssertNotSmi(object);
3744
3745 DCHECK(!representation.IsSmi() ||
3746 !instr->value()->IsConstantOperand() ||
3747 IsSmi(LConstantOperand::cast(instr->value())));
3748 if (representation.IsDouble()) {
3749 DCHECK(access.IsInobject());
3750 DCHECK(!instr->hydrogen()->has_transition());
3751 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
3752 DoubleRegister value = ToDoubleRegister(instr->value());
3753 __ sdc1(value, FieldMemOperand(object, offset));
3754 return;
3755 }
3756
3757 if (instr->hydrogen()->has_transition()) {
3758 Handle<Map> transition = instr->hydrogen()->transition_map();
3759 AddDeprecationDependency(transition);
3760 __ li(scratch, Operand(transition));
3761 __ sw(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
3762 if (instr->hydrogen()->NeedsWriteBarrierForMap()) {
3763 Register temp = ToRegister(instr->temp());
3764 // Update the write barrier for the map field.
3765 __ RecordWriteForMap(object,
3766 scratch,
3767 temp,
3768 GetRAState(),
3769 kSaveFPRegs);
3770 }
3771 }
3772
3773 // Do the store.
3774 Register value = ToRegister(instr->value());
3775 if (access.IsInobject()) {
3776 MemOperand operand = FieldMemOperand(object, offset);
3777 __ Store(value, operand, representation);
3778 if (instr->hydrogen()->NeedsWriteBarrier()) {
3779 // Update the write barrier for the object for in-object properties.
3780 __ RecordWriteField(object,
3781 offset,
3782 value,
3783 scratch,
3784 GetRAState(),
3785 kSaveFPRegs,
3786 EMIT_REMEMBERED_SET,
3787 instr->hydrogen()->SmiCheckForWriteBarrier(),
3788 instr->hydrogen()->PointersToHereCheckForValue());
3789 }
3790 } else {
3791 __ lw(scratch, FieldMemOperand(object, JSObject::kPropertiesOffset));
3792 MemOperand operand = FieldMemOperand(scratch, offset);
3793 __ Store(value, operand, representation);
3794 if (instr->hydrogen()->NeedsWriteBarrier()) {
3795 // Update the write barrier for the properties array.
3796 // object is used as a scratch register.
3797 __ RecordWriteField(scratch,
3798 offset,
3799 value,
3800 object,
3801 GetRAState(),
3802 kSaveFPRegs,
3803 EMIT_REMEMBERED_SET,
3804 instr->hydrogen()->SmiCheckForWriteBarrier(),
3805 instr->hydrogen()->PointersToHereCheckForValue());
3806 }
3807 }
3808}
3809
3810
3811void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
3812 DCHECK(ToRegister(instr->context()).is(cp));
3813 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
3814 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
3815
3816 if (instr->hydrogen()->HasVectorAndSlot()) {
3817 EmitVectorStoreICRegisters<LStoreNamedGeneric>(instr);
3818 }
3819
3820 __ li(StoreDescriptor::NameRegister(), Operand(instr->name()));
3821 Handle<Code> ic = CodeFactory::StoreICInOptimizedCode(
3822 isolate(), instr->language_mode(),
3823 instr->hydrogen()->initialization_state()).code();
3824 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3825}
3826
3827
3828void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
3829 Condition cc = instr->hydrogen()->allow_equality() ? hi : hs;
3830 Operand operand(0);
3831 Register reg;
3832 if (instr->index()->IsConstantOperand()) {
3833 operand = ToOperand(instr->index());
3834 reg = ToRegister(instr->length());
3835 cc = CommuteCondition(cc);
3836 } else {
3837 reg = ToRegister(instr->index());
3838 operand = ToOperand(instr->length());
3839 }
3840 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
3841 Label done;
3842 __ Branch(&done, NegateCondition(cc), reg, operand);
3843 __ stop("eliminated bounds check failed");
3844 __ bind(&done);
3845 } else {
3846 DeoptimizeIf(cc, instr, Deoptimizer::kOutOfBounds, reg, operand);
3847 }
3848}
3849
3850
3851void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
3852 Register external_pointer = ToRegister(instr->elements());
3853 Register key = no_reg;
3854 ElementsKind elements_kind = instr->elements_kind();
3855 bool key_is_constant = instr->key()->IsConstantOperand();
3856 int constant_key = 0;
3857 if (key_is_constant) {
3858 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3859 if (constant_key & 0xF0000000) {
3860 Abort(kArrayIndexConstantValueTooBig);
3861 }
3862 } else {
3863 key = ToRegister(instr->key());
3864 }
3865 int element_size_shift = ElementsKindToShiftSize(elements_kind);
3866 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3867 ? (element_size_shift - kSmiTagSize) : element_size_shift;
3868 int base_offset = instr->base_offset();
3869
3870 if (elements_kind == FLOAT32_ELEMENTS || elements_kind == FLOAT64_ELEMENTS) {
3871 Register address = scratch0();
3872 FPURegister value(ToDoubleRegister(instr->value()));
3873 if (key_is_constant) {
3874 if (constant_key != 0) {
3875 __ Addu(address, external_pointer,
3876 Operand(constant_key << element_size_shift));
3877 } else {
3878 address = external_pointer;
3879 }
3880 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003881 __ Lsa(address, external_pointer, key, shift_size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003882 }
3883
3884 if (elements_kind == FLOAT32_ELEMENTS) {
3885 __ cvt_s_d(double_scratch0(), value);
3886 __ swc1(double_scratch0(), MemOperand(address, base_offset));
3887 } else { // Storing doubles, not floats.
3888 __ sdc1(value, MemOperand(address, base_offset));
3889 }
3890 } else {
3891 Register value(ToRegister(instr->value()));
3892 MemOperand mem_operand = PrepareKeyedOperand(
3893 key, external_pointer, key_is_constant, constant_key,
3894 element_size_shift, shift_size,
3895 base_offset);
3896 switch (elements_kind) {
3897 case UINT8_ELEMENTS:
3898 case UINT8_CLAMPED_ELEMENTS:
3899 case INT8_ELEMENTS:
3900 __ sb(value, mem_operand);
3901 break;
3902 case INT16_ELEMENTS:
3903 case UINT16_ELEMENTS:
3904 __ sh(value, mem_operand);
3905 break;
3906 case INT32_ELEMENTS:
3907 case UINT32_ELEMENTS:
3908 __ sw(value, mem_operand);
3909 break;
3910 case FLOAT32_ELEMENTS:
3911 case FLOAT64_ELEMENTS:
3912 case FAST_DOUBLE_ELEMENTS:
3913 case FAST_ELEMENTS:
3914 case FAST_SMI_ELEMENTS:
3915 case FAST_HOLEY_DOUBLE_ELEMENTS:
3916 case FAST_HOLEY_ELEMENTS:
3917 case FAST_HOLEY_SMI_ELEMENTS:
3918 case DICTIONARY_ELEMENTS:
3919 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
3920 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
Ben Murdoch097c5b22016-05-18 11:27:45 +01003921 case FAST_STRING_WRAPPER_ELEMENTS:
3922 case SLOW_STRING_WRAPPER_ELEMENTS:
3923 case NO_ELEMENTS:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003924 UNREACHABLE();
3925 break;
3926 }
3927 }
3928}
3929
3930
3931void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
3932 DoubleRegister value = ToDoubleRegister(instr->value());
3933 Register elements = ToRegister(instr->elements());
3934 Register scratch = scratch0();
3935 Register scratch_1 = scratch1();
3936 DoubleRegister double_scratch = double_scratch0();
3937 bool key_is_constant = instr->key()->IsConstantOperand();
3938 int base_offset = instr->base_offset();
3939 Label not_nan, done;
3940
3941 // Calculate the effective address of the slot in the array to store the
3942 // double value.
3943 int element_size_shift = ElementsKindToShiftSize(FAST_DOUBLE_ELEMENTS);
3944 if (key_is_constant) {
3945 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3946 if (constant_key & 0xF0000000) {
3947 Abort(kArrayIndexConstantValueTooBig);
3948 }
3949 __ Addu(scratch, elements,
3950 Operand((constant_key << element_size_shift) + base_offset));
3951 } else {
3952 int shift_size = (instr->hydrogen()->key()->representation().IsSmi())
3953 ? (element_size_shift - kSmiTagSize) : element_size_shift;
3954 __ Addu(scratch, elements, Operand(base_offset));
3955 __ sll(at, ToRegister(instr->key()), shift_size);
3956 __ Addu(scratch, scratch, at);
3957 }
3958
3959 if (instr->NeedsCanonicalization()) {
3960 Label is_nan;
3961 // Check for NaN. All NaNs must be canonicalized.
3962 __ BranchF(NULL, &is_nan, eq, value, value);
3963 __ Branch(&not_nan);
3964
3965 // Only load canonical NaN if the comparison above set the overflow.
3966 __ bind(&is_nan);
3967 __ LoadRoot(scratch_1, Heap::kNanValueRootIndex);
3968 __ ldc1(double_scratch,
3969 FieldMemOperand(scratch_1, HeapNumber::kValueOffset));
3970 __ sdc1(double_scratch, MemOperand(scratch, 0));
3971 __ Branch(&done);
3972 }
3973
3974 __ bind(&not_nan);
3975 __ sdc1(value, MemOperand(scratch, 0));
3976 __ bind(&done);
3977}
3978
3979
3980void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
3981 Register value = ToRegister(instr->value());
3982 Register elements = ToRegister(instr->elements());
3983 Register key = instr->key()->IsRegister() ? ToRegister(instr->key())
3984 : no_reg;
3985 Register scratch = scratch0();
3986 Register store_base = scratch;
3987 int offset = instr->base_offset();
3988
3989 // Do the store.
3990 if (instr->key()->IsConstantOperand()) {
3991 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
3992 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
3993 offset += ToInteger32(const_operand) * kPointerSize;
3994 store_base = elements;
3995 } else {
3996 // Even though the HLoadKeyed instruction forces the input
3997 // representation for the key to be an integer, the input gets replaced
3998 // during bound check elimination with the index argument to the bounds
3999 // check, which can be tagged, so that case must be handled here, too.
4000 if (instr->hydrogen()->key()->representation().IsSmi()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004001 __ Lsa(scratch, elements, key, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004002 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004003 __ Lsa(scratch, elements, key, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004004 }
4005 }
4006 __ sw(value, MemOperand(store_base, offset));
4007
4008 if (instr->hydrogen()->NeedsWriteBarrier()) {
4009 SmiCheck check_needed =
4010 instr->hydrogen()->value()->type().IsHeapObject()
4011 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4012 // Compute address of modified element and store it into key register.
4013 __ Addu(key, store_base, Operand(offset));
4014 __ RecordWrite(elements,
4015 key,
4016 value,
4017 GetRAState(),
4018 kSaveFPRegs,
4019 EMIT_REMEMBERED_SET,
4020 check_needed,
4021 instr->hydrogen()->PointersToHereCheckForValue());
4022 }
4023}
4024
4025
4026void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4027 // By cases: external, fast double
4028 if (instr->is_fixed_typed_array()) {
4029 DoStoreKeyedExternalArray(instr);
4030 } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4031 DoStoreKeyedFixedDoubleArray(instr);
4032 } else {
4033 DoStoreKeyedFixedArray(instr);
4034 }
4035}
4036
4037
4038void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4039 DCHECK(ToRegister(instr->context()).is(cp));
4040 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4041 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4042 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4043
4044 if (instr->hydrogen()->HasVectorAndSlot()) {
4045 EmitVectorStoreICRegisters<LStoreKeyedGeneric>(instr);
4046 }
4047
4048 Handle<Code> ic = CodeFactory::KeyedStoreICInOptimizedCode(
4049 isolate(), instr->language_mode(),
4050 instr->hydrogen()->initialization_state()).code();
4051 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4052}
4053
4054
4055void LCodeGen::DoMaybeGrowElements(LMaybeGrowElements* instr) {
4056 class DeferredMaybeGrowElements final : public LDeferredCode {
4057 public:
4058 DeferredMaybeGrowElements(LCodeGen* codegen, LMaybeGrowElements* instr)
4059 : LDeferredCode(codegen), instr_(instr) {}
4060 void Generate() override { codegen()->DoDeferredMaybeGrowElements(instr_); }
4061 LInstruction* instr() override { return instr_; }
4062
4063 private:
4064 LMaybeGrowElements* instr_;
4065 };
4066
4067 Register result = v0;
4068 DeferredMaybeGrowElements* deferred =
4069 new (zone()) DeferredMaybeGrowElements(this, instr);
4070 LOperand* key = instr->key();
4071 LOperand* current_capacity = instr->current_capacity();
4072
4073 DCHECK(instr->hydrogen()->key()->representation().IsInteger32());
4074 DCHECK(instr->hydrogen()->current_capacity()->representation().IsInteger32());
4075 DCHECK(key->IsConstantOperand() || key->IsRegister());
4076 DCHECK(current_capacity->IsConstantOperand() ||
4077 current_capacity->IsRegister());
4078
4079 if (key->IsConstantOperand() && current_capacity->IsConstantOperand()) {
4080 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4081 int32_t constant_capacity =
4082 ToInteger32(LConstantOperand::cast(current_capacity));
4083 if (constant_key >= constant_capacity) {
4084 // Deferred case.
4085 __ jmp(deferred->entry());
4086 }
4087 } else if (key->IsConstantOperand()) {
4088 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4089 __ Branch(deferred->entry(), le, ToRegister(current_capacity),
4090 Operand(constant_key));
4091 } else if (current_capacity->IsConstantOperand()) {
4092 int32_t constant_capacity =
4093 ToInteger32(LConstantOperand::cast(current_capacity));
4094 __ Branch(deferred->entry(), ge, ToRegister(key),
4095 Operand(constant_capacity));
4096 } else {
4097 __ Branch(deferred->entry(), ge, ToRegister(key),
4098 Operand(ToRegister(current_capacity)));
4099 }
4100
4101 if (instr->elements()->IsRegister()) {
4102 __ mov(result, ToRegister(instr->elements()));
4103 } else {
4104 __ lw(result, ToMemOperand(instr->elements()));
4105 }
4106
4107 __ bind(deferred->exit());
4108}
4109
4110
4111void LCodeGen::DoDeferredMaybeGrowElements(LMaybeGrowElements* instr) {
4112 // TODO(3095996): Get rid of this. For now, we need to make the
4113 // result register contain a valid pointer because it is already
4114 // contained in the register pointer map.
4115 Register result = v0;
4116 __ mov(result, zero_reg);
4117
4118 // We have to call a stub.
4119 {
4120 PushSafepointRegistersScope scope(this);
4121 if (instr->object()->IsRegister()) {
4122 __ mov(result, ToRegister(instr->object()));
4123 } else {
4124 __ lw(result, ToMemOperand(instr->object()));
4125 }
4126
4127 LOperand* key = instr->key();
4128 if (key->IsConstantOperand()) {
4129 __ li(a3, Operand(ToSmi(LConstantOperand::cast(key))));
4130 } else {
4131 __ mov(a3, ToRegister(key));
4132 __ SmiTag(a3);
4133 }
4134
4135 GrowArrayElementsStub stub(isolate(), instr->hydrogen()->is_js_array(),
4136 instr->hydrogen()->kind());
4137 __ mov(a0, result);
4138 __ CallStub(&stub);
4139 RecordSafepointWithLazyDeopt(
4140 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
4141 __ StoreToSafepointRegisterSlot(result, result);
4142 }
4143
4144 // Deopt on smi, which means the elements array changed to dictionary mode.
4145 __ SmiTst(result, at);
4146 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
4147}
4148
4149
4150void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4151 Register object_reg = ToRegister(instr->object());
4152 Register scratch = scratch0();
4153
4154 Handle<Map> from_map = instr->original_map();
4155 Handle<Map> to_map = instr->transitioned_map();
4156 ElementsKind from_kind = instr->from_kind();
4157 ElementsKind to_kind = instr->to_kind();
4158
4159 Label not_applicable;
4160 __ lw(scratch, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4161 __ Branch(&not_applicable, ne, scratch, Operand(from_map));
4162
4163 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
4164 Register new_map_reg = ToRegister(instr->new_map_temp());
4165 __ li(new_map_reg, Operand(to_map));
4166 __ sw(new_map_reg, FieldMemOperand(object_reg, HeapObject::kMapOffset));
4167 // Write barrier.
4168 __ RecordWriteForMap(object_reg,
4169 new_map_reg,
4170 scratch,
4171 GetRAState(),
4172 kDontSaveFPRegs);
4173 } else {
4174 DCHECK(object_reg.is(a0));
4175 DCHECK(ToRegister(instr->context()).is(cp));
4176 PushSafepointRegistersScope scope(this);
4177 __ li(a1, Operand(to_map));
4178 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4179 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4180 __ CallStub(&stub);
4181 RecordSafepointWithRegisters(
4182 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
4183 }
4184 __ bind(&not_applicable);
4185}
4186
4187
4188void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4189 Register object = ToRegister(instr->object());
4190 Register temp = ToRegister(instr->temp());
4191 Label no_memento_found;
4192 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found,
4193 ne, &no_memento_found);
4194 DeoptimizeIf(al, instr);
4195 __ bind(&no_memento_found);
4196}
4197
4198
4199void LCodeGen::DoStringAdd(LStringAdd* instr) {
4200 DCHECK(ToRegister(instr->context()).is(cp));
4201 DCHECK(ToRegister(instr->left()).is(a1));
4202 DCHECK(ToRegister(instr->right()).is(a0));
4203 StringAddStub stub(isolate(),
4204 instr->hydrogen()->flags(),
4205 instr->hydrogen()->pretenure_flag());
4206 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4207}
4208
4209
4210void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
4211 class DeferredStringCharCodeAt final : public LDeferredCode {
4212 public:
4213 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
4214 : LDeferredCode(codegen), instr_(instr) { }
4215 void Generate() override { codegen()->DoDeferredStringCharCodeAt(instr_); }
4216 LInstruction* instr() override { return instr_; }
4217
4218 private:
4219 LStringCharCodeAt* instr_;
4220 };
4221
4222 DeferredStringCharCodeAt* deferred =
4223 new(zone()) DeferredStringCharCodeAt(this, instr);
4224 StringCharLoadGenerator::Generate(masm(),
4225 ToRegister(instr->string()),
4226 ToRegister(instr->index()),
4227 ToRegister(instr->result()),
4228 deferred->entry());
4229 __ bind(deferred->exit());
4230}
4231
4232
4233void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4234 Register string = ToRegister(instr->string());
4235 Register result = ToRegister(instr->result());
4236 Register scratch = scratch0();
4237
4238 // TODO(3095996): Get rid of this. For now, we need to make the
4239 // result register contain a valid pointer because it is already
4240 // contained in the register pointer map.
4241 __ mov(result, zero_reg);
4242
4243 PushSafepointRegistersScope scope(this);
4244 __ push(string);
4245 // Push the index as a smi. This is safe because of the checks in
4246 // DoStringCharCodeAt above.
4247 if (instr->index()->IsConstantOperand()) {
4248 int const_index = ToInteger32(LConstantOperand::cast(instr->index()));
4249 __ Addu(scratch, zero_reg, Operand(Smi::FromInt(const_index)));
4250 __ push(scratch);
4251 } else {
4252 Register index = ToRegister(instr->index());
4253 __ SmiTag(index);
4254 __ push(index);
4255 }
4256 CallRuntimeFromDeferred(Runtime::kStringCharCodeAtRT, 2, instr,
4257 instr->context());
4258 __ AssertSmi(v0);
4259 __ SmiUntag(v0);
4260 __ StoreToSafepointRegisterSlot(v0, result);
4261}
4262
4263
4264void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
4265 class DeferredStringCharFromCode final : public LDeferredCode {
4266 public:
4267 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
4268 : LDeferredCode(codegen), instr_(instr) { }
4269 void Generate() override {
4270 codegen()->DoDeferredStringCharFromCode(instr_);
4271 }
4272 LInstruction* instr() override { return instr_; }
4273
4274 private:
4275 LStringCharFromCode* instr_;
4276 };
4277
4278 DeferredStringCharFromCode* deferred =
4279 new(zone()) DeferredStringCharFromCode(this, instr);
4280
4281 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
4282 Register char_code = ToRegister(instr->char_code());
4283 Register result = ToRegister(instr->result());
4284 Register scratch = scratch0();
4285 DCHECK(!char_code.is(result));
4286
4287 __ Branch(deferred->entry(), hi,
4288 char_code, Operand(String::kMaxOneByteCharCode));
4289 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004290 __ Lsa(result, result, char_code, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004291 __ lw(result, FieldMemOperand(result, FixedArray::kHeaderSize));
4292 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
4293 __ Branch(deferred->entry(), eq, result, Operand(scratch));
4294 __ bind(deferred->exit());
4295}
4296
4297
4298void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4299 Register char_code = ToRegister(instr->char_code());
4300 Register result = ToRegister(instr->result());
4301
4302 // TODO(3095996): Get rid of this. For now, we need to make the
4303 // result register contain a valid pointer because it is already
4304 // contained in the register pointer map.
4305 __ mov(result, zero_reg);
4306
4307 PushSafepointRegistersScope scope(this);
4308 __ SmiTag(char_code);
4309 __ push(char_code);
4310 CallRuntimeFromDeferred(Runtime::kStringCharFromCode, 1, instr,
4311 instr->context());
4312 __ StoreToSafepointRegisterSlot(v0, result);
4313}
4314
4315
4316void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
4317 LOperand* input = instr->value();
4318 DCHECK(input->IsRegister() || input->IsStackSlot());
4319 LOperand* output = instr->result();
4320 DCHECK(output->IsDoubleRegister());
4321 FPURegister single_scratch = double_scratch0().low();
4322 if (input->IsStackSlot()) {
4323 Register scratch = scratch0();
4324 __ lw(scratch, ToMemOperand(input));
4325 __ mtc1(scratch, single_scratch);
4326 } else {
4327 __ mtc1(ToRegister(input), single_scratch);
4328 }
4329 __ cvt_d_w(ToDoubleRegister(output), single_scratch);
4330}
4331
4332
4333void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4334 LOperand* input = instr->value();
4335 LOperand* output = instr->result();
4336
4337 __ Cvt_d_uw(ToDoubleRegister(output), ToRegister(input), f22);
4338}
4339
4340
4341void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
4342 class DeferredNumberTagI final : public LDeferredCode {
4343 public:
4344 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
4345 : LDeferredCode(codegen), instr_(instr) { }
4346 void Generate() override {
4347 codegen()->DoDeferredNumberTagIU(instr_,
4348 instr_->value(),
4349 instr_->temp1(),
4350 instr_->temp2(),
4351 SIGNED_INT32);
4352 }
4353 LInstruction* instr() override { return instr_; }
4354
4355 private:
4356 LNumberTagI* instr_;
4357 };
4358
4359 Register src = ToRegister(instr->value());
4360 Register dst = ToRegister(instr->result());
4361 Register overflow = scratch0();
4362
4363 DeferredNumberTagI* deferred = new(zone()) DeferredNumberTagI(this, instr);
4364 __ SmiTagCheckOverflow(dst, src, overflow);
4365 __ BranchOnOverflow(deferred->entry(), overflow);
4366 __ bind(deferred->exit());
4367}
4368
4369
4370void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4371 class DeferredNumberTagU final : public LDeferredCode {
4372 public:
4373 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4374 : LDeferredCode(codegen), instr_(instr) { }
4375 void Generate() override {
4376 codegen()->DoDeferredNumberTagIU(instr_,
4377 instr_->value(),
4378 instr_->temp1(),
4379 instr_->temp2(),
4380 UNSIGNED_INT32);
4381 }
4382 LInstruction* instr() override { return instr_; }
4383
4384 private:
4385 LNumberTagU* instr_;
4386 };
4387
4388 Register input = ToRegister(instr->value());
4389 Register result = ToRegister(instr->result());
4390
4391 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4392 __ Branch(deferred->entry(), hi, input, Operand(Smi::kMaxValue));
4393 __ SmiTag(result, input);
4394 __ bind(deferred->exit());
4395}
4396
4397
4398void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4399 LOperand* value,
4400 LOperand* temp1,
4401 LOperand* temp2,
4402 IntegerSignedness signedness) {
4403 Label done, slow;
4404 Register src = ToRegister(value);
4405 Register dst = ToRegister(instr->result());
4406 Register tmp1 = scratch0();
4407 Register tmp2 = ToRegister(temp1);
4408 Register tmp3 = ToRegister(temp2);
4409 DoubleRegister dbl_scratch = double_scratch0();
4410
4411 if (signedness == SIGNED_INT32) {
4412 // There was overflow, so bits 30 and 31 of the original integer
4413 // disagree. Try to allocate a heap number in new space and store
4414 // the value in there. If that fails, call the runtime system.
4415 if (dst.is(src)) {
4416 __ SmiUntag(src, dst);
4417 __ Xor(src, src, Operand(0x80000000));
4418 }
4419 __ mtc1(src, dbl_scratch);
4420 __ cvt_d_w(dbl_scratch, dbl_scratch);
4421 } else {
4422 __ Cvt_d_uw(dbl_scratch, src, f22);
4423 }
4424
4425 if (FLAG_inline_new) {
4426 __ LoadRoot(tmp3, Heap::kHeapNumberMapRootIndex);
4427 __ AllocateHeapNumber(dst, tmp1, tmp2, tmp3, &slow, DONT_TAG_RESULT);
4428 __ Branch(&done);
4429 }
4430
4431 // Slow case: Call the runtime system to do the number allocation.
4432 __ bind(&slow);
4433 {
4434 // TODO(3095996): Put a valid pointer value in the stack slot where the
4435 // result register is stored, as this register is in the pointer map, but
4436 // contains an integer value.
4437 __ mov(dst, zero_reg);
4438
4439 // Preserve the value of all registers.
4440 PushSafepointRegistersScope scope(this);
4441
4442 // NumberTagI and NumberTagD use the context from the frame, rather than
4443 // the environment's HContext or HInlinedContext value.
4444 // They only call Runtime::kAllocateHeapNumber.
4445 // The corresponding HChange instructions are added in a phase that does
4446 // not have easy access to the local context.
4447 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4448 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4449 RecordSafepointWithRegisters(
4450 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4451 __ Subu(v0, v0, kHeapObjectTag);
4452 __ StoreToSafepointRegisterSlot(v0, dst);
4453 }
4454
4455
4456 // Done. Put the value in dbl_scratch into the value of the allocated heap
4457 // number.
4458 __ bind(&done);
4459 __ sdc1(dbl_scratch, MemOperand(dst, HeapNumber::kValueOffset));
4460 __ Addu(dst, dst, kHeapObjectTag);
4461}
4462
4463
4464void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
4465 class DeferredNumberTagD final : public LDeferredCode {
4466 public:
4467 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4468 : LDeferredCode(codegen), instr_(instr) { }
4469 void Generate() override { codegen()->DoDeferredNumberTagD(instr_); }
4470 LInstruction* instr() override { return instr_; }
4471
4472 private:
4473 LNumberTagD* instr_;
4474 };
4475
4476 DoubleRegister input_reg = ToDoubleRegister(instr->value());
4477 Register scratch = scratch0();
4478 Register reg = ToRegister(instr->result());
4479 Register temp1 = ToRegister(instr->temp());
4480 Register temp2 = ToRegister(instr->temp2());
4481
4482 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
4483 if (FLAG_inline_new) {
4484 __ LoadRoot(scratch, Heap::kHeapNumberMapRootIndex);
4485 // We want the untagged address first for performance
4486 __ AllocateHeapNumber(reg, temp1, temp2, scratch, deferred->entry(),
4487 DONT_TAG_RESULT);
4488 } else {
4489 __ Branch(deferred->entry());
4490 }
4491 __ bind(deferred->exit());
4492 __ sdc1(input_reg, MemOperand(reg, HeapNumber::kValueOffset));
4493 // Now that we have finished with the object's real address tag it
4494 __ Addu(reg, reg, kHeapObjectTag);
4495}
4496
4497
4498void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4499 // TODO(3095996): Get rid of this. For now, we need to make the
4500 // result register contain a valid pointer because it is already
4501 // contained in the register pointer map.
4502 Register reg = ToRegister(instr->result());
4503 __ mov(reg, zero_reg);
4504
4505 PushSafepointRegistersScope scope(this);
4506 // NumberTagI and NumberTagD use the context from the frame, rather than
4507 // the environment's HContext or HInlinedContext value.
4508 // They only call Runtime::kAllocateHeapNumber.
4509 // The corresponding HChange instructions are added in a phase that does
4510 // not have easy access to the local context.
4511 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4512 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4513 RecordSafepointWithRegisters(
4514 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4515 __ Subu(v0, v0, kHeapObjectTag);
4516 __ StoreToSafepointRegisterSlot(v0, reg);
4517}
4518
4519
4520void LCodeGen::DoSmiTag(LSmiTag* instr) {
4521 HChange* hchange = instr->hydrogen();
4522 Register input = ToRegister(instr->value());
4523 Register output = ToRegister(instr->result());
4524 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4525 hchange->value()->CheckFlag(HValue::kUint32)) {
4526 __ And(at, input, Operand(0xc0000000));
4527 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow, at, Operand(zero_reg));
4528 }
4529 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4530 !hchange->value()->CheckFlag(HValue::kUint32)) {
4531 __ SmiTagCheckOverflow(output, input, at);
4532 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, at, Operand(zero_reg));
4533 } else {
4534 __ SmiTag(output, input);
4535 }
4536}
4537
4538
4539void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4540 Register scratch = scratch0();
4541 Register input = ToRegister(instr->value());
4542 Register result = ToRegister(instr->result());
4543 if (instr->needs_check()) {
4544 STATIC_ASSERT(kHeapObjectTag == 1);
4545 // If the input is a HeapObject, value of scratch won't be zero.
4546 __ And(scratch, input, Operand(kHeapObjectTag));
4547 __ SmiUntag(result, input);
4548 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi, scratch, Operand(zero_reg));
4549 } else {
4550 __ SmiUntag(result, input);
4551 }
4552}
4553
4554
4555void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
4556 DoubleRegister result_reg,
4557 NumberUntagDMode mode) {
4558 bool can_convert_undefined_to_nan =
4559 instr->hydrogen()->can_convert_undefined_to_nan();
4560 bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
4561
4562 Register scratch = scratch0();
4563 Label convert, load_smi, done;
4564 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4565 // Smi check.
4566 __ UntagAndJumpIfSmi(scratch, input_reg, &load_smi);
4567 // Heap number map check.
4568 __ lw(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
4569 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4570 if (can_convert_undefined_to_nan) {
4571 __ Branch(&convert, ne, scratch, Operand(at));
4572 } else {
4573 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, scratch,
4574 Operand(at));
4575 }
4576 // Load heap number.
4577 __ ldc1(result_reg, FieldMemOperand(input_reg, HeapNumber::kValueOffset));
4578 if (deoptimize_on_minus_zero) {
4579 __ mfc1(at, result_reg.low());
4580 __ Branch(&done, ne, at, Operand(zero_reg));
4581 __ Mfhc1(scratch, result_reg);
4582 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero, scratch,
4583 Operand(HeapNumber::kSignMask));
4584 }
4585 __ Branch(&done);
4586 if (can_convert_undefined_to_nan) {
4587 __ bind(&convert);
4588 // Convert undefined (and hole) to NaN.
4589 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4590 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefined, input_reg,
4591 Operand(at));
4592 __ LoadRoot(scratch, Heap::kNanValueRootIndex);
4593 __ ldc1(result_reg, FieldMemOperand(scratch, HeapNumber::kValueOffset));
4594 __ Branch(&done);
4595 }
4596 } else {
4597 __ SmiUntag(scratch, input_reg);
4598 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
4599 }
4600 // Smi to double register conversion
4601 __ bind(&load_smi);
4602 // scratch: untagged value of input_reg
4603 __ mtc1(scratch, result_reg);
4604 __ cvt_d_w(result_reg, result_reg);
4605 __ bind(&done);
4606}
4607
4608
4609void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr) {
4610 Register input_reg = ToRegister(instr->value());
4611 Register scratch1 = scratch0();
4612 Register scratch2 = ToRegister(instr->temp());
4613 DoubleRegister double_scratch = double_scratch0();
4614 DoubleRegister double_scratch2 = ToDoubleRegister(instr->temp2());
4615
4616 DCHECK(!scratch1.is(input_reg) && !scratch1.is(scratch2));
4617 DCHECK(!scratch2.is(input_reg) && !scratch2.is(scratch1));
4618
4619 Label done;
4620
4621 // The input is a tagged HeapObject.
4622 // Heap number map check.
4623 __ lw(scratch1, FieldMemOperand(input_reg, HeapObject::kMapOffset));
4624 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4625 // This 'at' value and scratch1 map value are used for tests in both clauses
4626 // of the if.
4627
4628 if (instr->truncating()) {
4629 // Performs a truncating conversion of a floating point number as used by
4630 // the JS bitwise operations.
4631 Label no_heap_number, check_bools, check_false;
4632 // Check HeapNumber map.
4633 __ Branch(USE_DELAY_SLOT, &no_heap_number, ne, scratch1, Operand(at));
4634 __ mov(scratch2, input_reg); // In delay slot.
4635 __ TruncateHeapNumberToI(input_reg, scratch2);
4636 __ Branch(&done);
4637
4638 // Check for Oddballs. Undefined/False is converted to zero and True to one
4639 // for truncating conversions.
4640 __ bind(&no_heap_number);
4641 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4642 __ Branch(&check_bools, ne, input_reg, Operand(at));
4643 DCHECK(ToRegister(instr->result()).is(input_reg));
4644 __ Branch(USE_DELAY_SLOT, &done);
4645 __ mov(input_reg, zero_reg); // In delay slot.
4646
4647 __ bind(&check_bools);
4648 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4649 __ Branch(&check_false, ne, scratch2, Operand(at));
4650 __ Branch(USE_DELAY_SLOT, &done);
4651 __ li(input_reg, Operand(1)); // In delay slot.
4652
4653 __ bind(&check_false);
4654 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4655 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefinedBoolean,
4656 scratch2, Operand(at));
4657 __ Branch(USE_DELAY_SLOT, &done);
4658 __ mov(input_reg, zero_reg); // In delay slot.
4659 } else {
4660 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber, scratch1,
4661 Operand(at));
4662
4663 // Load the double value.
4664 __ ldc1(double_scratch,
4665 FieldMemOperand(input_reg, HeapNumber::kValueOffset));
4666
4667 Register except_flag = scratch2;
4668 __ EmitFPUTruncate(kRoundToZero,
4669 input_reg,
4670 double_scratch,
4671 scratch1,
4672 double_scratch2,
4673 except_flag,
4674 kCheckForInexactConversion);
4675
4676 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
4677 Operand(zero_reg));
4678
4679 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
4680 __ Branch(&done, ne, input_reg, Operand(zero_reg));
4681
4682 __ Mfhc1(scratch1, double_scratch);
4683 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
4684 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
4685 Operand(zero_reg));
4686 }
4687 }
4688 __ bind(&done);
4689}
4690
4691
4692void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
4693 class DeferredTaggedToI final : public LDeferredCode {
4694 public:
4695 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
4696 : LDeferredCode(codegen), instr_(instr) { }
4697 void Generate() override { codegen()->DoDeferredTaggedToI(instr_); }
4698 LInstruction* instr() override { return instr_; }
4699
4700 private:
4701 LTaggedToI* instr_;
4702 };
4703
4704 LOperand* input = instr->value();
4705 DCHECK(input->IsRegister());
4706 DCHECK(input->Equals(instr->result()));
4707
4708 Register input_reg = ToRegister(input);
4709
4710 if (instr->hydrogen()->value()->representation().IsSmi()) {
4711 __ SmiUntag(input_reg);
4712 } else {
4713 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
4714
4715 // Let the deferred code handle the HeapObject case.
4716 __ JumpIfNotSmi(input_reg, deferred->entry());
4717
4718 // Smi to int32 conversion.
4719 __ SmiUntag(input_reg);
4720 __ bind(deferred->exit());
4721 }
4722}
4723
4724
4725void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
4726 LOperand* input = instr->value();
4727 DCHECK(input->IsRegister());
4728 LOperand* result = instr->result();
4729 DCHECK(result->IsDoubleRegister());
4730
4731 Register input_reg = ToRegister(input);
4732 DoubleRegister result_reg = ToDoubleRegister(result);
4733
4734 HValue* value = instr->hydrogen()->value();
4735 NumberUntagDMode mode = value->representation().IsSmi()
4736 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
4737
4738 EmitNumberUntagD(instr, input_reg, result_reg, mode);
4739}
4740
4741
4742void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
4743 Register result_reg = ToRegister(instr->result());
4744 Register scratch1 = scratch0();
4745 DoubleRegister double_input = ToDoubleRegister(instr->value());
4746
4747 if (instr->truncating()) {
4748 __ TruncateDoubleToI(result_reg, double_input);
4749 } else {
4750 Register except_flag = LCodeGen::scratch1();
4751
4752 __ EmitFPUTruncate(kRoundToMinusInf,
4753 result_reg,
4754 double_input,
4755 scratch1,
4756 double_scratch0(),
4757 except_flag,
4758 kCheckForInexactConversion);
4759
4760 // Deopt if the operation did not succeed (except_flag != 0).
4761 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
4762 Operand(zero_reg));
4763
4764 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
4765 Label done;
4766 __ Branch(&done, ne, result_reg, Operand(zero_reg));
4767 __ Mfhc1(scratch1, double_input);
4768 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
4769 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
4770 Operand(zero_reg));
4771 __ bind(&done);
4772 }
4773 }
4774}
4775
4776
4777void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
4778 Register result_reg = ToRegister(instr->result());
4779 Register scratch1 = LCodeGen::scratch0();
4780 DoubleRegister double_input = ToDoubleRegister(instr->value());
4781
4782 if (instr->truncating()) {
4783 __ TruncateDoubleToI(result_reg, double_input);
4784 } else {
4785 Register except_flag = LCodeGen::scratch1();
4786
4787 __ EmitFPUTruncate(kRoundToMinusInf,
4788 result_reg,
4789 double_input,
4790 scratch1,
4791 double_scratch0(),
4792 except_flag,
4793 kCheckForInexactConversion);
4794
4795 // Deopt if the operation did not succeed (except_flag != 0).
4796 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN, except_flag,
4797 Operand(zero_reg));
4798
4799 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
4800 Label done;
4801 __ Branch(&done, ne, result_reg, Operand(zero_reg));
4802 __ Mfhc1(scratch1, double_input);
4803 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
4804 DeoptimizeIf(ne, instr, Deoptimizer::kMinusZero, scratch1,
4805 Operand(zero_reg));
4806 __ bind(&done);
4807 }
4808 }
4809 __ SmiTagCheckOverflow(result_reg, result_reg, scratch1);
4810 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow, scratch1, Operand(zero_reg));
4811}
4812
4813
4814void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
4815 LOperand* input = instr->value();
4816 __ SmiTst(ToRegister(input), at);
4817 DeoptimizeIf(ne, instr, Deoptimizer::kNotASmi, at, Operand(zero_reg));
4818}
4819
4820
4821void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
4822 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
4823 LOperand* input = instr->value();
4824 __ SmiTst(ToRegister(input), at);
4825 DeoptimizeIf(eq, instr, Deoptimizer::kSmi, at, Operand(zero_reg));
4826 }
4827}
4828
4829
4830void LCodeGen::DoCheckArrayBufferNotNeutered(
4831 LCheckArrayBufferNotNeutered* instr) {
4832 Register view = ToRegister(instr->view());
4833 Register scratch = scratch0();
4834
4835 __ lw(scratch, FieldMemOperand(view, JSArrayBufferView::kBufferOffset));
4836 __ lw(scratch, FieldMemOperand(scratch, JSArrayBuffer::kBitFieldOffset));
4837 __ And(at, scratch, 1 << JSArrayBuffer::WasNeutered::kShift);
4838 DeoptimizeIf(ne, instr, Deoptimizer::kOutOfBounds, at, Operand(zero_reg));
4839}
4840
4841
4842void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
4843 Register input = ToRegister(instr->value());
4844 Register scratch = scratch0();
4845
4846 __ GetObjectType(input, scratch, scratch);
4847
4848 if (instr->hydrogen()->is_interval_check()) {
4849 InstanceType first;
4850 InstanceType last;
4851 instr->hydrogen()->GetCheckInterval(&first, &last);
4852
4853 // If there is only one type in the interval check for equality.
4854 if (first == last) {
4855 DeoptimizeIf(ne, instr, Deoptimizer::kWrongInstanceType, scratch,
4856 Operand(first));
4857 } else {
4858 DeoptimizeIf(lo, instr, Deoptimizer::kWrongInstanceType, scratch,
4859 Operand(first));
4860 // Omit check for the last type.
4861 if (last != LAST_TYPE) {
4862 DeoptimizeIf(hi, instr, Deoptimizer::kWrongInstanceType, scratch,
4863 Operand(last));
4864 }
4865 }
4866 } else {
4867 uint8_t mask;
4868 uint8_t tag;
4869 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
4870
4871 if (base::bits::IsPowerOfTwo32(mask)) {
4872 DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
4873 __ And(at, scratch, mask);
4874 DeoptimizeIf(tag == 0 ? ne : eq, instr, Deoptimizer::kWrongInstanceType,
4875 at, Operand(zero_reg));
4876 } else {
4877 __ And(scratch, scratch, Operand(mask));
4878 DeoptimizeIf(ne, instr, Deoptimizer::kWrongInstanceType, scratch,
4879 Operand(tag));
4880 }
4881 }
4882}
4883
4884
4885void LCodeGen::DoCheckValue(LCheckValue* instr) {
4886 Register reg = ToRegister(instr->value());
4887 Handle<HeapObject> object = instr->hydrogen()->object().handle();
4888 AllowDeferredHandleDereference smi_check;
4889 if (isolate()->heap()->InNewSpace(*object)) {
4890 Register reg = ToRegister(instr->value());
4891 Handle<Cell> cell = isolate()->factory()->NewCell(object);
4892 __ li(at, Operand(cell));
4893 __ lw(at, FieldMemOperand(at, Cell::kValueOffset));
4894 DeoptimizeIf(ne, instr, Deoptimizer::kValueMismatch, reg, Operand(at));
4895 } else {
4896 DeoptimizeIf(ne, instr, Deoptimizer::kValueMismatch, reg, Operand(object));
4897 }
4898}
4899
4900
4901void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
4902 {
4903 PushSafepointRegistersScope scope(this);
4904 __ push(object);
4905 __ mov(cp, zero_reg);
4906 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
4907 RecordSafepointWithRegisters(
4908 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
4909 __ StoreToSafepointRegisterSlot(v0, scratch0());
4910 }
4911 __ SmiTst(scratch0(), at);
4912 DeoptimizeIf(eq, instr, Deoptimizer::kInstanceMigrationFailed, at,
4913 Operand(zero_reg));
4914}
4915
4916
4917void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
4918 class DeferredCheckMaps final : public LDeferredCode {
4919 public:
4920 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
4921 : LDeferredCode(codegen), instr_(instr), object_(object) {
4922 SetExit(check_maps());
4923 }
4924 void Generate() override {
4925 codegen()->DoDeferredInstanceMigration(instr_, object_);
4926 }
4927 Label* check_maps() { return &check_maps_; }
4928 LInstruction* instr() override { return instr_; }
4929
4930 private:
4931 LCheckMaps* instr_;
4932 Label check_maps_;
4933 Register object_;
4934 };
4935
4936 if (instr->hydrogen()->IsStabilityCheck()) {
4937 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
4938 for (int i = 0; i < maps->size(); ++i) {
4939 AddStabilityDependency(maps->at(i).handle());
4940 }
4941 return;
4942 }
4943
4944 Register map_reg = scratch0();
4945 LOperand* input = instr->value();
4946 DCHECK(input->IsRegister());
4947 Register reg = ToRegister(input);
4948 __ lw(map_reg, FieldMemOperand(reg, HeapObject::kMapOffset));
4949
4950 DeferredCheckMaps* deferred = NULL;
4951 if (instr->hydrogen()->HasMigrationTarget()) {
4952 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
4953 __ bind(deferred->check_maps());
4954 }
4955
4956 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
4957 Label success;
4958 for (int i = 0; i < maps->size() - 1; i++) {
4959 Handle<Map> map = maps->at(i).handle();
4960 __ CompareMapAndBranch(map_reg, map, &success, eq, &success);
4961 }
4962 Handle<Map> map = maps->at(maps->size() - 1).handle();
4963 // Do the CompareMap() directly within the Branch() and DeoptimizeIf().
4964 if (instr->hydrogen()->HasMigrationTarget()) {
4965 __ Branch(deferred->entry(), ne, map_reg, Operand(map));
4966 } else {
4967 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap, map_reg, Operand(map));
4968 }
4969
4970 __ bind(&success);
4971}
4972
4973
4974void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
4975 DoubleRegister value_reg = ToDoubleRegister(instr->unclamped());
4976 Register result_reg = ToRegister(instr->result());
4977 DoubleRegister temp_reg = ToDoubleRegister(instr->temp());
4978 __ ClampDoubleToUint8(result_reg, value_reg, temp_reg);
4979}
4980
4981
4982void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
4983 Register unclamped_reg = ToRegister(instr->unclamped());
4984 Register result_reg = ToRegister(instr->result());
4985 __ ClampUint8(result_reg, unclamped_reg);
4986}
4987
4988
4989void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
4990 Register scratch = scratch0();
4991 Register input_reg = ToRegister(instr->unclamped());
4992 Register result_reg = ToRegister(instr->result());
4993 DoubleRegister temp_reg = ToDoubleRegister(instr->temp());
4994 Label is_smi, done, heap_number;
4995
4996 // Both smi and heap number cases are handled.
4997 __ UntagAndJumpIfSmi(scratch, input_reg, &is_smi);
4998
4999 // Check for heap number
5000 __ lw(scratch, FieldMemOperand(input_reg, HeapObject::kMapOffset));
5001 __ Branch(&heap_number, eq, scratch, Operand(factory()->heap_number_map()));
5002
5003 // Check for undefined. Undefined is converted to zero for clamping
5004 // conversions.
5005 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumberUndefined, input_reg,
5006 Operand(factory()->undefined_value()));
5007 __ mov(result_reg, zero_reg);
5008 __ jmp(&done);
5009
5010 // Heap number
5011 __ bind(&heap_number);
5012 __ ldc1(double_scratch0(), FieldMemOperand(input_reg,
5013 HeapNumber::kValueOffset));
5014 __ ClampDoubleToUint8(result_reg, double_scratch0(), temp_reg);
5015 __ jmp(&done);
5016
5017 __ bind(&is_smi);
5018 __ ClampUint8(result_reg, scratch);
5019
5020 __ bind(&done);
5021}
5022
5023
5024void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5025 DoubleRegister value_reg = ToDoubleRegister(instr->value());
5026 Register result_reg = ToRegister(instr->result());
5027 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5028 __ FmoveHigh(result_reg, value_reg);
5029 } else {
5030 __ FmoveLow(result_reg, value_reg);
5031 }
5032}
5033
5034
5035void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5036 Register hi_reg = ToRegister(instr->hi());
5037 Register lo_reg = ToRegister(instr->lo());
5038 DoubleRegister result_reg = ToDoubleRegister(instr->result());
5039 __ Move(result_reg, lo_reg, hi_reg);
5040}
5041
5042
5043void LCodeGen::DoAllocate(LAllocate* instr) {
5044 class DeferredAllocate final : public LDeferredCode {
5045 public:
5046 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
5047 : LDeferredCode(codegen), instr_(instr) { }
5048 void Generate() override { codegen()->DoDeferredAllocate(instr_); }
5049 LInstruction* instr() override { return instr_; }
5050
5051 private:
5052 LAllocate* instr_;
5053 };
5054
5055 DeferredAllocate* deferred =
5056 new(zone()) DeferredAllocate(this, instr);
5057
5058 Register result = ToRegister(instr->result());
5059 Register scratch = ToRegister(instr->temp1());
5060 Register scratch2 = ToRegister(instr->temp2());
5061
5062 // Allocate memory for the object.
5063 AllocationFlags flags = TAG_OBJECT;
5064 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5065 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5066 }
5067 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5068 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5069 flags = static_cast<AllocationFlags>(flags | PRETENURE);
5070 }
5071 if (instr->size()->IsConstantOperand()) {
5072 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5073 CHECK(size <= Page::kMaxRegularHeapObjectSize);
5074 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5075 } else {
5076 Register size = ToRegister(instr->size());
5077 __ Allocate(size, result, scratch, scratch2, deferred->entry(), flags);
5078 }
5079
5080 __ bind(deferred->exit());
5081
5082 if (instr->hydrogen()->MustPrefillWithFiller()) {
5083 STATIC_ASSERT(kHeapObjectTag == 1);
5084 if (instr->size()->IsConstantOperand()) {
5085 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5086 __ li(scratch, Operand(size - kHeapObjectTag));
5087 } else {
5088 __ Subu(scratch, ToRegister(instr->size()), Operand(kHeapObjectTag));
5089 }
5090 __ li(scratch2, Operand(isolate()->factory()->one_pointer_filler_map()));
5091 Label loop;
5092 __ bind(&loop);
5093 __ Subu(scratch, scratch, Operand(kPointerSize));
5094 __ Addu(at, result, Operand(scratch));
5095 __ sw(scratch2, MemOperand(at));
5096 __ Branch(&loop, ge, scratch, Operand(zero_reg));
5097 }
5098}
5099
5100
5101void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
5102 Register result = ToRegister(instr->result());
5103
5104 // TODO(3095996): Get rid of this. For now, we need to make the
5105 // result register contain a valid pointer because it is already
5106 // contained in the register pointer map.
5107 __ mov(result, zero_reg);
5108
5109 PushSafepointRegistersScope scope(this);
5110 if (instr->size()->IsRegister()) {
5111 Register size = ToRegister(instr->size());
5112 DCHECK(!size.is(result));
5113 __ SmiTag(size);
5114 __ push(size);
5115 } else {
5116 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5117 if (size >= 0 && size <= Smi::kMaxValue) {
5118 __ Push(Smi::FromInt(size));
5119 } else {
5120 // We should never get here at runtime => abort
5121 __ stop("invalid allocation size");
5122 return;
5123 }
5124 }
5125
5126 int flags = AllocateDoubleAlignFlag::encode(
5127 instr->hydrogen()->MustAllocateDoubleAligned());
5128 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5129 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5130 flags = AllocateTargetSpace::update(flags, OLD_SPACE);
5131 } else {
5132 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5133 }
5134 __ Push(Smi::FromInt(flags));
5135
5136 CallRuntimeFromDeferred(
5137 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
5138 __ StoreToSafepointRegisterSlot(v0, result);
5139}
5140
5141
5142void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
5143 DCHECK(ToRegister(instr->value()).is(a0));
5144 DCHECK(ToRegister(instr->result()).is(v0));
5145 __ push(a0);
5146 CallRuntime(Runtime::kToFastProperties, 1, instr);
5147}
5148
5149
5150void LCodeGen::DoTypeof(LTypeof* instr) {
5151 DCHECK(ToRegister(instr->value()).is(a3));
5152 DCHECK(ToRegister(instr->result()).is(v0));
5153 Label end, do_call;
5154 Register value_register = ToRegister(instr->value());
5155 __ JumpIfNotSmi(value_register, &do_call);
5156 __ li(v0, Operand(isolate()->factory()->number_string()));
5157 __ jmp(&end);
5158 __ bind(&do_call);
5159 TypeofStub stub(isolate());
5160 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5161 __ bind(&end);
5162}
5163
5164
5165void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5166 Register input = ToRegister(instr->value());
5167
5168 Register cmp1 = no_reg;
5169 Operand cmp2 = Operand(no_reg);
5170
5171 Condition final_branch_condition = EmitTypeofIs(instr->TrueLabel(chunk_),
5172 instr->FalseLabel(chunk_),
5173 input,
5174 instr->type_literal(),
5175 &cmp1,
5176 &cmp2);
5177
5178 DCHECK(cmp1.is_valid());
5179 DCHECK(!cmp2.is_reg() || cmp2.rm().is_valid());
5180
5181 if (final_branch_condition != kNoCondition) {
5182 EmitBranch(instr, final_branch_condition, cmp1, cmp2);
5183 }
5184}
5185
5186
5187Condition LCodeGen::EmitTypeofIs(Label* true_label,
5188 Label* false_label,
5189 Register input,
5190 Handle<String> type_name,
5191 Register* cmp1,
5192 Operand* cmp2) {
5193 // This function utilizes the delay slot heavily. This is used to load
5194 // values that are always usable without depending on the type of the input
5195 // register.
5196 Condition final_branch_condition = kNoCondition;
5197 Register scratch = scratch0();
5198 Factory* factory = isolate()->factory();
5199 if (String::Equals(type_name, factory->number_string())) {
5200 __ JumpIfSmi(input, true_label);
5201 __ lw(input, FieldMemOperand(input, HeapObject::kMapOffset));
5202 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5203 *cmp1 = input;
5204 *cmp2 = Operand(at);
5205 final_branch_condition = eq;
5206
5207 } else if (String::Equals(type_name, factory->string_string())) {
5208 __ JumpIfSmi(input, false_label);
5209 __ GetObjectType(input, input, scratch);
5210 *cmp1 = scratch;
5211 *cmp2 = Operand(FIRST_NONSTRING_TYPE);
5212 final_branch_condition = lt;
5213
5214 } else if (String::Equals(type_name, factory->symbol_string())) {
5215 __ JumpIfSmi(input, false_label);
5216 __ GetObjectType(input, input, scratch);
5217 *cmp1 = scratch;
5218 *cmp2 = Operand(SYMBOL_TYPE);
5219 final_branch_condition = eq;
5220
5221 } else if (String::Equals(type_name, factory->boolean_string())) {
5222 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5223 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5224 __ LoadRoot(at, Heap::kFalseValueRootIndex);
5225 *cmp1 = at;
5226 *cmp2 = Operand(input);
5227 final_branch_condition = eq;
5228
5229 } else if (String::Equals(type_name, factory->undefined_string())) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01005230 __ LoadRoot(at, Heap::kNullValueRootIndex);
5231 __ Branch(USE_DELAY_SLOT, false_label, eq, at, Operand(input));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005232 // The first instruction of JumpIfSmi is an And - it is safe in the delay
5233 // slot.
5234 __ JumpIfSmi(input, false_label);
5235 // Check for undetectable objects => true.
5236 __ lw(input, FieldMemOperand(input, HeapObject::kMapOffset));
5237 __ lbu(at, FieldMemOperand(input, Map::kBitFieldOffset));
5238 __ And(at, at, 1 << Map::kIsUndetectable);
5239 *cmp1 = at;
5240 *cmp2 = Operand(zero_reg);
5241 final_branch_condition = ne;
5242
5243 } else if (String::Equals(type_name, factory->function_string())) {
5244 __ JumpIfSmi(input, false_label);
5245 __ lw(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
5246 __ lbu(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5247 __ And(scratch, scratch,
5248 Operand((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable)));
5249 *cmp1 = scratch;
5250 *cmp2 = Operand(1 << Map::kIsCallable);
5251 final_branch_condition = eq;
5252
5253 } else if (String::Equals(type_name, factory->object_string())) {
5254 __ JumpIfSmi(input, false_label);
5255 __ LoadRoot(at, Heap::kNullValueRootIndex);
5256 __ Branch(USE_DELAY_SLOT, true_label, eq, at, Operand(input));
5257 STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
5258 __ GetObjectType(input, scratch, scratch1());
5259 __ Branch(false_label, lt, scratch1(), Operand(FIRST_JS_RECEIVER_TYPE));
5260 // Check for callable or undetectable objects => false.
5261 __ lbu(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5262 __ And(at, scratch,
5263 Operand((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable)));
5264 *cmp1 = at;
5265 *cmp2 = Operand(zero_reg);
5266 final_branch_condition = eq;
5267
5268// clang-format off
5269#define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
5270 } else if (String::Equals(type_name, factory->type##_string())) { \
5271 __ JumpIfSmi(input, false_label); \
5272 __ lw(input, FieldMemOperand(input, HeapObject::kMapOffset)); \
5273 __ LoadRoot(at, Heap::k##Type##MapRootIndex); \
5274 *cmp1 = input; \
5275 *cmp2 = Operand(at); \
5276 final_branch_condition = eq;
5277 SIMD128_TYPES(SIMD128_TYPE)
5278#undef SIMD128_TYPE
5279 // clang-format on
5280
5281 } else {
5282 *cmp1 = at;
5283 *cmp2 = Operand(zero_reg); // Set to valid regs, to avoid caller assertion.
5284 __ Branch(false_label);
5285 }
5286
5287 return final_branch_condition;
5288}
5289
5290
5291void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5292 if (info()->ShouldEnsureSpaceForLazyDeopt()) {
5293 // Ensure that we have enough space after the previous lazy-bailout
5294 // instruction for patching the code here.
5295 int current_pc = masm()->pc_offset();
5296 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5297 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5298 DCHECK_EQ(0, padding_size % Assembler::kInstrSize);
5299 while (padding_size > 0) {
5300 __ nop();
5301 padding_size -= Assembler::kInstrSize;
5302 }
5303 }
5304 }
5305 last_lazy_deopt_pc_ = masm()->pc_offset();
5306}
5307
5308
5309void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
5310 last_lazy_deopt_pc_ = masm()->pc_offset();
5311 DCHECK(instr->HasEnvironment());
5312 LEnvironment* env = instr->environment();
5313 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5314 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5315}
5316
5317
5318void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
5319 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5320 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5321 // needed return address), even though the implementation of LAZY and EAGER is
5322 // now identical. When LAZY is eventually completely folded into EAGER, remove
5323 // the special case below.
5324 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5325 type = Deoptimizer::LAZY;
5326 }
5327
5328 DeoptimizeIf(al, instr, instr->hydrogen()->reason(), type, zero_reg,
5329 Operand(zero_reg));
5330}
5331
5332
5333void LCodeGen::DoDummy(LDummy* instr) {
5334 // Nothing to see here, move on!
5335}
5336
5337
5338void LCodeGen::DoDummyUse(LDummyUse* instr) {
5339 // Nothing to see here, move on!
5340}
5341
5342
5343void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
5344 PushSafepointRegistersScope scope(this);
5345 LoadContextFromDeferred(instr->context());
5346 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5347 RecordSafepointWithLazyDeopt(
5348 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
5349 DCHECK(instr->HasEnvironment());
5350 LEnvironment* env = instr->environment();
5351 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5352}
5353
5354
5355void LCodeGen::DoStackCheck(LStackCheck* instr) {
5356 class DeferredStackCheck final : public LDeferredCode {
5357 public:
5358 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5359 : LDeferredCode(codegen), instr_(instr) { }
5360 void Generate() override { codegen()->DoDeferredStackCheck(instr_); }
5361 LInstruction* instr() override { return instr_; }
5362
5363 private:
5364 LStackCheck* instr_;
5365 };
5366
5367 DCHECK(instr->HasEnvironment());
5368 LEnvironment* env = instr->environment();
5369 // There is no LLazyBailout instruction for stack-checks. We have to
5370 // prepare for lazy deoptimization explicitly here.
5371 if (instr->hydrogen()->is_function_entry()) {
5372 // Perform stack overflow check.
5373 Label done;
5374 __ LoadRoot(at, Heap::kStackLimitRootIndex);
5375 __ Branch(&done, hs, sp, Operand(at));
5376 DCHECK(instr->context()->IsRegister());
5377 DCHECK(ToRegister(instr->context()).is(cp));
5378 CallCode(isolate()->builtins()->StackCheck(),
5379 RelocInfo::CODE_TARGET,
5380 instr);
5381 __ bind(&done);
5382 } else {
5383 DCHECK(instr->hydrogen()->is_backwards_branch());
5384 // Perform stack overflow check if this goto needs it before jumping.
5385 DeferredStackCheck* deferred_stack_check =
5386 new(zone()) DeferredStackCheck(this, instr);
5387 __ LoadRoot(at, Heap::kStackLimitRootIndex);
5388 __ Branch(deferred_stack_check->entry(), lo, sp, Operand(at));
5389 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
5390 __ bind(instr->done_label());
5391 deferred_stack_check->SetExit(instr->done_label());
5392 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5393 // Don't record a deoptimization index for the safepoint here.
5394 // This will be done explicitly when emitting call and the safepoint in
5395 // the deferred code.
5396 }
5397}
5398
5399
5400void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
5401 // This is a pseudo-instruction that ensures that the environment here is
5402 // properly registered for deoptimization and records the assembler's PC
5403 // offset.
5404 LEnvironment* environment = instr->environment();
5405
5406 // If the environment were already registered, we would have no way of
5407 // backpatching it with the spill slot operands.
5408 DCHECK(!environment->HasBeenRegistered());
5409 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
5410
5411 GenerateOsrPrologue();
5412}
5413
5414
5415void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
5416 Register result = ToRegister(instr->result());
5417 Register object = ToRegister(instr->object());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005418
5419 Label use_cache, call_runtime;
5420 DCHECK(object.is(a0));
Ben Murdoch097c5b22016-05-18 11:27:45 +01005421 __ CheckEnumCache(&call_runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005422
5423 __ lw(result, FieldMemOperand(object, HeapObject::kMapOffset));
5424 __ Branch(&use_cache);
5425
5426 // Get the set of properties to enumerate.
5427 __ bind(&call_runtime);
5428 __ push(object);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005429 CallRuntime(Runtime::kForInEnumerate, instr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005430 __ bind(&use_cache);
5431}
5432
5433
5434void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5435 Register map = ToRegister(instr->map());
5436 Register result = ToRegister(instr->result());
5437 Label load_cache, done;
5438 __ EnumLength(result, map);
5439 __ Branch(&load_cache, ne, result, Operand(Smi::FromInt(0)));
5440 __ li(result, Operand(isolate()->factory()->empty_fixed_array()));
5441 __ jmp(&done);
5442
5443 __ bind(&load_cache);
5444 __ LoadInstanceDescriptors(map, result);
5445 __ lw(result,
5446 FieldMemOperand(result, DescriptorArray::kEnumCacheOffset));
5447 __ lw(result,
5448 FieldMemOperand(result, FixedArray::SizeFor(instr->idx())));
5449 DeoptimizeIf(eq, instr, Deoptimizer::kNoCache, result, Operand(zero_reg));
5450
5451 __ bind(&done);
5452}
5453
5454
5455void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5456 Register object = ToRegister(instr->value());
5457 Register map = ToRegister(instr->map());
5458 __ lw(scratch0(), FieldMemOperand(object, HeapObject::kMapOffset));
5459 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap, map, Operand(scratch0()));
5460}
5461
5462
5463void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5464 Register result,
5465 Register object,
5466 Register index) {
5467 PushSafepointRegistersScope scope(this);
5468 __ Push(object, index);
5469 __ mov(cp, zero_reg);
5470 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
5471 RecordSafepointWithRegisters(
5472 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
5473 __ StoreToSafepointRegisterSlot(v0, result);
5474}
5475
5476
5477void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
5478 class DeferredLoadMutableDouble final : public LDeferredCode {
5479 public:
5480 DeferredLoadMutableDouble(LCodeGen* codegen,
5481 LLoadFieldByIndex* instr,
5482 Register result,
5483 Register object,
5484 Register index)
5485 : LDeferredCode(codegen),
5486 instr_(instr),
5487 result_(result),
5488 object_(object),
5489 index_(index) {
5490 }
5491 void Generate() override {
5492 codegen()->DoDeferredLoadMutableDouble(instr_, result_, object_, index_);
5493 }
5494 LInstruction* instr() override { return instr_; }
5495
5496 private:
5497 LLoadFieldByIndex* instr_;
5498 Register result_;
5499 Register object_;
5500 Register index_;
5501 };
5502
5503 Register object = ToRegister(instr->object());
5504 Register index = ToRegister(instr->index());
5505 Register result = ToRegister(instr->result());
5506 Register scratch = scratch0();
5507
5508 DeferredLoadMutableDouble* deferred;
5509 deferred = new(zone()) DeferredLoadMutableDouble(
5510 this, instr, result, object, index);
5511
5512 Label out_of_object, done;
5513
5514 __ And(scratch, index, Operand(Smi::FromInt(1)));
5515 __ Branch(deferred->entry(), ne, scratch, Operand(zero_reg));
5516 __ sra(index, index, 1);
5517
5518 __ Branch(USE_DELAY_SLOT, &out_of_object, lt, index, Operand(zero_reg));
5519 __ sll(scratch, index, kPointerSizeLog2 - kSmiTagSize); // In delay slot.
5520
5521 STATIC_ASSERT(kPointerSizeLog2 > kSmiTagSize);
5522 __ Addu(scratch, object, scratch);
5523 __ lw(result, FieldMemOperand(scratch, JSObject::kHeaderSize));
5524
5525 __ Branch(&done);
5526
5527 __ bind(&out_of_object);
5528 __ lw(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
5529 // Index is equal to negated out of object property index plus 1.
5530 __ Subu(scratch, result, scratch);
5531 __ lw(result, FieldMemOperand(scratch,
5532 FixedArray::kHeaderSize - kPointerSize));
5533 __ bind(deferred->exit());
5534 __ bind(&done);
5535}
5536
5537
5538void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
5539 Register context = ToRegister(instr->context());
5540 __ sw(context, MemOperand(fp, StandardFrameConstants::kContextOffset));
5541}
5542
5543
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005544#undef __
5545
5546} // namespace internal
5547} // namespace v8