blob: ebc527700e4a4ae56b3118e2becbaf6beb9e50b3 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2013 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/crankshaft/arm64/lithium-codegen-arm64.h"
6
7#include "src/arm64/frames-arm64.h"
8#include "src/base/bits.h"
9#include "src/code-factory.h"
10#include "src/code-stubs.h"
11#include "src/crankshaft/arm64/lithium-gap-resolver-arm64.h"
12#include "src/crankshaft/hydrogen-osr.h"
13#include "src/ic/ic.h"
14#include "src/ic/stub-cache.h"
15#include "src/profiler/cpu-profiler.h"
16
17namespace v8 {
18namespace internal {
19
20
21class SafepointGenerator final : public CallWrapper {
22 public:
23 SafepointGenerator(LCodeGen* codegen,
24 LPointerMap* pointers,
25 Safepoint::DeoptMode mode)
26 : codegen_(codegen),
27 pointers_(pointers),
28 deopt_mode_(mode) { }
29 virtual ~SafepointGenerator() { }
30
31 virtual void BeforeCall(int call_size) const { }
32
33 virtual void AfterCall() const {
34 codegen_->RecordSafepoint(pointers_, deopt_mode_);
35 }
36
37 private:
38 LCodeGen* codegen_;
39 LPointerMap* pointers_;
40 Safepoint::DeoptMode deopt_mode_;
41};
42
43
44#define __ masm()->
45
46// Emit code to branch if the given condition holds.
47// The code generated here doesn't modify the flags and they must have
48// been set by some prior instructions.
49//
50// The EmitInverted function simply inverts the condition.
51class BranchOnCondition : public BranchGenerator {
52 public:
53 BranchOnCondition(LCodeGen* codegen, Condition cond)
54 : BranchGenerator(codegen),
55 cond_(cond) { }
56
57 virtual void Emit(Label* label) const {
58 __ B(cond_, label);
59 }
60
61 virtual void EmitInverted(Label* label) const {
62 if (cond_ != al) {
63 __ B(NegateCondition(cond_), label);
64 }
65 }
66
67 private:
68 Condition cond_;
69};
70
71
72// Emit code to compare lhs and rhs and branch if the condition holds.
73// This uses MacroAssembler's CompareAndBranch function so it will handle
74// converting the comparison to Cbz/Cbnz if the right-hand side is 0.
75//
76// EmitInverted still compares the two operands but inverts the condition.
77class CompareAndBranch : public BranchGenerator {
78 public:
79 CompareAndBranch(LCodeGen* codegen,
80 Condition cond,
81 const Register& lhs,
82 const Operand& rhs)
83 : BranchGenerator(codegen),
84 cond_(cond),
85 lhs_(lhs),
86 rhs_(rhs) { }
87
88 virtual void Emit(Label* label) const {
89 __ CompareAndBranch(lhs_, rhs_, cond_, label);
90 }
91
92 virtual void EmitInverted(Label* label) const {
93 __ CompareAndBranch(lhs_, rhs_, NegateCondition(cond_), label);
94 }
95
96 private:
97 Condition cond_;
98 const Register& lhs_;
99 const Operand& rhs_;
100};
101
102
103// Test the input with the given mask and branch if the condition holds.
104// If the condition is 'eq' or 'ne' this will use MacroAssembler's
105// TestAndBranchIfAllClear and TestAndBranchIfAnySet so it will handle the
106// conversion to Tbz/Tbnz when possible.
107class TestAndBranch : public BranchGenerator {
108 public:
109 TestAndBranch(LCodeGen* codegen,
110 Condition cond,
111 const Register& value,
112 uint64_t mask)
113 : BranchGenerator(codegen),
114 cond_(cond),
115 value_(value),
116 mask_(mask) { }
117
118 virtual void Emit(Label* label) const {
119 switch (cond_) {
120 case eq:
121 __ TestAndBranchIfAllClear(value_, mask_, label);
122 break;
123 case ne:
124 __ TestAndBranchIfAnySet(value_, mask_, label);
125 break;
126 default:
127 __ Tst(value_, mask_);
128 __ B(cond_, label);
129 }
130 }
131
132 virtual void EmitInverted(Label* label) const {
133 // The inverse of "all clear" is "any set" and vice versa.
134 switch (cond_) {
135 case eq:
136 __ TestAndBranchIfAnySet(value_, mask_, label);
137 break;
138 case ne:
139 __ TestAndBranchIfAllClear(value_, mask_, label);
140 break;
141 default:
142 __ Tst(value_, mask_);
143 __ B(NegateCondition(cond_), label);
144 }
145 }
146
147 private:
148 Condition cond_;
149 const Register& value_;
150 uint64_t mask_;
151};
152
153
154// Test the input and branch if it is non-zero and not a NaN.
155class BranchIfNonZeroNumber : public BranchGenerator {
156 public:
157 BranchIfNonZeroNumber(LCodeGen* codegen, const FPRegister& value,
158 const FPRegister& scratch)
159 : BranchGenerator(codegen), value_(value), scratch_(scratch) { }
160
161 virtual void Emit(Label* label) const {
162 __ Fabs(scratch_, value_);
163 // Compare with 0.0. Because scratch_ is positive, the result can be one of
164 // nZCv (equal), nzCv (greater) or nzCV (unordered).
165 __ Fcmp(scratch_, 0.0);
166 __ B(gt, label);
167 }
168
169 virtual void EmitInverted(Label* label) const {
170 __ Fabs(scratch_, value_);
171 __ Fcmp(scratch_, 0.0);
172 __ B(le, label);
173 }
174
175 private:
176 const FPRegister& value_;
177 const FPRegister& scratch_;
178};
179
180
181// Test the input and branch if it is a heap number.
182class BranchIfHeapNumber : public BranchGenerator {
183 public:
184 BranchIfHeapNumber(LCodeGen* codegen, const Register& value)
185 : BranchGenerator(codegen), value_(value) { }
186
187 virtual void Emit(Label* label) const {
188 __ JumpIfHeapNumber(value_, label);
189 }
190
191 virtual void EmitInverted(Label* label) const {
192 __ JumpIfNotHeapNumber(value_, label);
193 }
194
195 private:
196 const Register& value_;
197};
198
199
200// Test the input and branch if it is the specified root value.
201class BranchIfRoot : public BranchGenerator {
202 public:
203 BranchIfRoot(LCodeGen* codegen, const Register& value,
204 Heap::RootListIndex index)
205 : BranchGenerator(codegen), value_(value), index_(index) { }
206
207 virtual void Emit(Label* label) const {
208 __ JumpIfRoot(value_, index_, label);
209 }
210
211 virtual void EmitInverted(Label* label) const {
212 __ JumpIfNotRoot(value_, index_, label);
213 }
214
215 private:
216 const Register& value_;
217 const Heap::RootListIndex index_;
218};
219
220
221void LCodeGen::WriteTranslation(LEnvironment* environment,
222 Translation* translation) {
223 if (environment == NULL) return;
224
225 // The translation includes one command per value in the environment.
226 int translation_size = environment->translation_size();
227
228 WriteTranslation(environment->outer(), translation);
229 WriteTranslationFrame(environment, translation);
230
231 int object_index = 0;
232 int dematerialized_index = 0;
233 for (int i = 0; i < translation_size; ++i) {
234 LOperand* value = environment->values()->at(i);
235 AddToTranslation(
236 environment, translation, value, environment->HasTaggedValueAt(i),
237 environment->HasUint32ValueAt(i), &object_index, &dematerialized_index);
238 }
239}
240
241
242void LCodeGen::AddToTranslation(LEnvironment* environment,
243 Translation* translation,
244 LOperand* op,
245 bool is_tagged,
246 bool is_uint32,
247 int* object_index_pointer,
248 int* dematerialized_index_pointer) {
249 if (op == LEnvironment::materialization_marker()) {
250 int object_index = (*object_index_pointer)++;
251 if (environment->ObjectIsDuplicateAt(object_index)) {
252 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
253 translation->DuplicateObject(dupe_of);
254 return;
255 }
256 int object_length = environment->ObjectLengthAt(object_index);
257 if (environment->ObjectIsArgumentsAt(object_index)) {
258 translation->BeginArgumentsObject(object_length);
259 } else {
260 translation->BeginCapturedObject(object_length);
261 }
262 int dematerialized_index = *dematerialized_index_pointer;
263 int env_offset = environment->translation_size() + dematerialized_index;
264 *dematerialized_index_pointer += object_length;
265 for (int i = 0; i < object_length; ++i) {
266 LOperand* value = environment->values()->at(env_offset + i);
267 AddToTranslation(environment,
268 translation,
269 value,
270 environment->HasTaggedValueAt(env_offset + i),
271 environment->HasUint32ValueAt(env_offset + i),
272 object_index_pointer,
273 dematerialized_index_pointer);
274 }
275 return;
276 }
277
278 if (op->IsStackSlot()) {
279 int index = op->index();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000280 if (is_tagged) {
281 translation->StoreStackSlot(index);
282 } else if (is_uint32) {
283 translation->StoreUint32StackSlot(index);
284 } else {
285 translation->StoreInt32StackSlot(index);
286 }
287 } else if (op->IsDoubleStackSlot()) {
288 int index = op->index();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000289 translation->StoreDoubleStackSlot(index);
290 } else if (op->IsRegister()) {
291 Register reg = ToRegister(op);
292 if (is_tagged) {
293 translation->StoreRegister(reg);
294 } else if (is_uint32) {
295 translation->StoreUint32Register(reg);
296 } else {
297 translation->StoreInt32Register(reg);
298 }
299 } else if (op->IsDoubleRegister()) {
300 DoubleRegister reg = ToDoubleRegister(op);
301 translation->StoreDoubleRegister(reg);
302 } else if (op->IsConstantOperand()) {
303 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
304 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
305 translation->StoreLiteral(src_index);
306 } else {
307 UNREACHABLE();
308 }
309}
310
311
312void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
313 Safepoint::DeoptMode mode) {
314 environment->set_has_been_used();
315 if (!environment->HasBeenRegistered()) {
316 int frame_count = 0;
317 int jsframe_count = 0;
318 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
319 ++frame_count;
320 if (e->frame_type() == JS_FUNCTION) {
321 ++jsframe_count;
322 }
323 }
324 Translation translation(&translations_, frame_count, jsframe_count, zone());
325 WriteTranslation(environment, &translation);
326 int deoptimization_index = deoptimizations_.length();
327 int pc_offset = masm()->pc_offset();
328 environment->Register(deoptimization_index,
329 translation.index(),
330 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
331 deoptimizations_.Add(environment, zone());
332 }
333}
334
335
336void LCodeGen::CallCode(Handle<Code> code,
337 RelocInfo::Mode mode,
338 LInstruction* instr) {
339 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT);
340}
341
342
343void LCodeGen::CallCodeGeneric(Handle<Code> code,
344 RelocInfo::Mode mode,
345 LInstruction* instr,
346 SafepointMode safepoint_mode) {
347 DCHECK(instr != NULL);
348
349 Assembler::BlockPoolsScope scope(masm_);
350 __ Call(code, mode);
351 RecordSafepointWithLazyDeopt(instr, safepoint_mode);
352
353 if ((code->kind() == Code::BINARY_OP_IC) ||
354 (code->kind() == Code::COMPARE_IC)) {
355 // Signal that we don't inline smi code before these stubs in the
356 // optimizing code generator.
357 InlineSmiCheckInfo::EmitNotInlined(masm());
358 }
359}
360
361
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000362void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
363 DCHECK(instr->IsMarkedAsCall());
364 DCHECK(ToRegister(instr->context()).is(cp));
365 DCHECK(ToRegister(instr->constructor()).is(x1));
366
367 __ Mov(x0, Operand(instr->arity()));
368 if (instr->arity() == 1) {
369 // We only need the allocation site for the case we have a length argument.
370 // The case may bail out to the runtime, which will determine the correct
371 // elements kind with the site.
372 __ Mov(x2, instr->hydrogen()->site());
373 } else {
374 __ LoadRoot(x2, Heap::kUndefinedValueRootIndex);
375 }
376
377
378 ElementsKind kind = instr->hydrogen()->elements_kind();
379 AllocationSiteOverrideMode override_mode =
380 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
381 ? DISABLE_ALLOCATION_SITES
382 : DONT_OVERRIDE;
383
384 if (instr->arity() == 0) {
385 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
386 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
387 } else if (instr->arity() == 1) {
388 Label done;
389 if (IsFastPackedElementsKind(kind)) {
390 Label packed_case;
391
392 // We might need to create a holey array; look at the first argument.
393 __ Peek(x10, 0);
394 __ Cbz(x10, &packed_case);
395
396 ElementsKind holey_kind = GetHoleyElementsKind(kind);
397 ArraySingleArgumentConstructorStub stub(isolate(),
398 holey_kind,
399 override_mode);
400 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
401 __ B(&done);
402 __ Bind(&packed_case);
403 }
404
405 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
406 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
407 __ Bind(&done);
408 } else {
409 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
410 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
411 }
412 RecordPushedArgumentsDelta(instr->hydrogen()->argument_delta());
413
414 DCHECK(ToRegister(instr->result()).is(x0));
415}
416
417
418void LCodeGen::CallRuntime(const Runtime::Function* function,
419 int num_arguments,
420 LInstruction* instr,
421 SaveFPRegsMode save_doubles) {
422 DCHECK(instr != NULL);
423
424 __ CallRuntime(function, num_arguments, save_doubles);
425
426 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
427}
428
429
430void LCodeGen::LoadContextFromDeferred(LOperand* context) {
431 if (context->IsRegister()) {
432 __ Mov(cp, ToRegister(context));
433 } else if (context->IsStackSlot()) {
434 __ Ldr(cp, ToMemOperand(context, kMustUseFramePointer));
435 } else if (context->IsConstantOperand()) {
436 HConstant* constant =
437 chunk_->LookupConstant(LConstantOperand::cast(context));
438 __ LoadHeapObject(cp,
439 Handle<HeapObject>::cast(constant->handle(isolate())));
440 } else {
441 UNREACHABLE();
442 }
443}
444
445
446void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
447 int argc,
448 LInstruction* instr,
449 LOperand* context) {
450 LoadContextFromDeferred(context);
451 __ CallRuntimeSaveDoubles(id);
452 RecordSafepointWithRegisters(
453 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
454}
455
456
457void LCodeGen::RecordAndWritePosition(int position) {
458 if (position == RelocInfo::kNoPosition) return;
459 masm()->positions_recorder()->RecordPosition(position);
460 masm()->positions_recorder()->WriteRecordedPositions();
461}
462
463
464void LCodeGen::RecordSafepointWithLazyDeopt(LInstruction* instr,
465 SafepointMode safepoint_mode) {
466 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
467 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
468 } else {
469 DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
470 RecordSafepointWithRegisters(
471 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
472 }
473}
474
475
476void LCodeGen::RecordSafepoint(LPointerMap* pointers,
477 Safepoint::Kind kind,
478 int arguments,
479 Safepoint::DeoptMode deopt_mode) {
480 DCHECK(expected_safepoint_kind_ == kind);
481
482 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
483 Safepoint safepoint = safepoints_.DefineSafepoint(
484 masm(), kind, arguments, deopt_mode);
485
486 for (int i = 0; i < operands->length(); i++) {
487 LOperand* pointer = operands->at(i);
488 if (pointer->IsStackSlot()) {
489 safepoint.DefinePointerSlot(pointer->index(), zone());
490 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
491 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
492 }
493 }
494}
495
496void LCodeGen::RecordSafepoint(LPointerMap* pointers,
497 Safepoint::DeoptMode deopt_mode) {
498 RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
499}
500
501
502void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
503 LPointerMap empty_pointers(zone());
504 RecordSafepoint(&empty_pointers, deopt_mode);
505}
506
507
508void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
509 int arguments,
510 Safepoint::DeoptMode deopt_mode) {
511 RecordSafepoint(pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
512}
513
514
515bool LCodeGen::GenerateCode() {
516 LPhase phase("Z_Code generation", chunk());
517 DCHECK(is_unused());
518 status_ = GENERATING;
519
520 // Open a frame scope to indicate that there is a frame on the stack. The
521 // NONE indicates that the scope shouldn't actually generate code to set up
522 // the frame (that is done in GeneratePrologue).
523 FrameScope frame_scope(masm_, StackFrame::NONE);
524
525 return GeneratePrologue() && GenerateBody() && GenerateDeferredCode() &&
526 GenerateJumpTable() && GenerateSafepointTable();
527}
528
529
530void LCodeGen::SaveCallerDoubles() {
531 DCHECK(info()->saves_caller_doubles());
532 DCHECK(NeedsEagerFrame());
533 Comment(";;; Save clobbered callee double registers");
534 BitVector* doubles = chunk()->allocated_double_registers();
535 BitVector::Iterator iterator(doubles);
536 int count = 0;
537 while (!iterator.Done()) {
538 // TODO(all): Is this supposed to save just the callee-saved doubles? It
539 // looks like it's saving all of them.
540 FPRegister value = FPRegister::from_code(iterator.Current());
541 __ Poke(value, count * kDoubleSize);
542 iterator.Advance();
543 count++;
544 }
545}
546
547
548void LCodeGen::RestoreCallerDoubles() {
549 DCHECK(info()->saves_caller_doubles());
550 DCHECK(NeedsEagerFrame());
551 Comment(";;; Restore clobbered callee double registers");
552 BitVector* doubles = chunk()->allocated_double_registers();
553 BitVector::Iterator iterator(doubles);
554 int count = 0;
555 while (!iterator.Done()) {
556 // TODO(all): Is this supposed to restore just the callee-saved doubles? It
557 // looks like it's restoring all of them.
558 FPRegister value = FPRegister::from_code(iterator.Current());
559 __ Peek(value, count * kDoubleSize);
560 iterator.Advance();
561 count++;
562 }
563}
564
565
566bool LCodeGen::GeneratePrologue() {
567 DCHECK(is_generating());
568
569 if (info()->IsOptimizing()) {
570 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000571 }
572
573 DCHECK(__ StackPointer().Is(jssp));
574 info()->set_prologue_offset(masm_->pc_offset());
575 if (NeedsEagerFrame()) {
576 if (info()->IsStub()) {
Ben Murdochda12d292016-06-02 14:46:10 +0100577 __ StubPrologue(
578 StackFrame::STUB,
579 GetStackSlotCount() + TypedFrameConstants::kFixedSlotCount);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000580 } else {
581 __ Prologue(info()->GeneratePreagedPrologue());
Ben Murdochda12d292016-06-02 14:46:10 +0100582 // Reserve space for the stack slots needed by the code.
583 int slots = GetStackSlotCount();
584 if (slots > 0) {
585 __ Claim(slots, kPointerSize);
586 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000587 }
588 frame_is_built_ = true;
589 }
590
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000591 if (info()->saves_caller_doubles()) {
592 SaveCallerDoubles();
593 }
594 return !is_aborted();
595}
596
597
598void LCodeGen::DoPrologue(LPrologue* instr) {
599 Comment(";;; Prologue begin");
600
601 // Allocate a local context if needed.
Ben Murdochc5610432016-08-08 18:44:38 +0100602 if (info()->scope()->num_heap_slots() > 0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000603 Comment(";;; Allocate local context");
604 bool need_write_barrier = true;
605 // Argument to NewContext is the function, which is in x1.
606 int slots = info()->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
607 Safepoint::DeoptMode deopt_mode = Safepoint::kNoLazyDeopt;
608 if (info()->scope()->is_script_scope()) {
609 __ Mov(x10, Operand(info()->scope()->GetScopeInfo(info()->isolate())));
610 __ Push(x1, x10);
611 __ CallRuntime(Runtime::kNewScriptContext);
612 deopt_mode = Safepoint::kLazyDeopt;
613 } else if (slots <= FastNewContextStub::kMaximumSlots) {
614 FastNewContextStub stub(isolate(), slots);
615 __ CallStub(&stub);
616 // Result of FastNewContextStub is always in new space.
617 need_write_barrier = false;
618 } else {
619 __ Push(x1);
620 __ CallRuntime(Runtime::kNewFunctionContext);
621 }
622 RecordSafepoint(deopt_mode);
623 // Context is returned in x0. It replaces the context passed to us. It's
624 // saved in the stack and kept live in cp.
625 __ Mov(cp, x0);
626 __ Str(x0, MemOperand(fp, StandardFrameConstants::kContextOffset));
627 // Copy any necessary parameters into the context.
628 int num_parameters = scope()->num_parameters();
629 int first_parameter = scope()->has_this_declaration() ? -1 : 0;
630 for (int i = first_parameter; i < num_parameters; i++) {
631 Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
632 if (var->IsContextSlot()) {
633 Register value = x0;
634 Register scratch = x3;
635
636 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
637 (num_parameters - 1 - i) * kPointerSize;
638 // Load parameter from stack.
639 __ Ldr(value, MemOperand(fp, parameter_offset));
640 // Store it in the context.
641 MemOperand target = ContextMemOperand(cp, var->index());
642 __ Str(value, target);
643 // Update the write barrier. This clobbers value and scratch.
644 if (need_write_barrier) {
645 __ RecordWriteContextSlot(cp, static_cast<int>(target.offset()),
646 value, scratch, GetLinkRegisterState(),
647 kSaveFPRegs);
648 } else if (FLAG_debug_code) {
649 Label done;
650 __ JumpIfInNewSpace(cp, &done);
651 __ Abort(kExpectedNewSpaceObject);
652 __ bind(&done);
653 }
654 }
655 }
656 Comment(";;; End allocate local context");
657 }
658
659 Comment(";;; Prologue end");
660}
661
662
663void LCodeGen::GenerateOsrPrologue() {
664 // Generate the OSR entry prologue at the first unknown OSR value, or if there
665 // are none, at the OSR entrypoint instruction.
666 if (osr_pc_offset_ >= 0) return;
667
668 osr_pc_offset_ = masm()->pc_offset();
669
670 // Adjust the frame size, subsuming the unoptimized frame into the
671 // optimized frame.
672 int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
673 DCHECK(slots >= 0);
674 __ Claim(slots);
675}
676
677
678void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
679 if (instr->IsCall()) {
680 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
681 }
682 if (!instr->IsLazyBailout() && !instr->IsGap()) {
683 safepoints_.BumpLastLazySafepointIndex();
684 }
685}
686
687
688bool LCodeGen::GenerateDeferredCode() {
689 DCHECK(is_generating());
690 if (deferred_.length() > 0) {
691 for (int i = 0; !is_aborted() && (i < deferred_.length()); i++) {
692 LDeferredCode* code = deferred_[i];
693
694 HValue* value =
695 instructions_->at(code->instruction_index())->hydrogen_value();
696 RecordAndWritePosition(
697 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
698
699 Comment(";;; <@%d,#%d> "
700 "-------------------- Deferred %s --------------------",
701 code->instruction_index(),
702 code->instr()->hydrogen_value()->id(),
703 code->instr()->Mnemonic());
704
705 __ Bind(code->entry());
706
707 if (NeedsDeferredFrame()) {
708 Comment(";;; Build frame");
709 DCHECK(!frame_is_built_);
710 DCHECK(info()->IsStub());
711 frame_is_built_ = true;
Ben Murdochda12d292016-06-02 14:46:10 +0100712 __ Push(lr, fp);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000713 __ Mov(fp, Smi::FromInt(StackFrame::STUB));
714 __ Push(fp);
715 __ Add(fp, __ StackPointer(),
Ben Murdochda12d292016-06-02 14:46:10 +0100716 TypedFrameConstants::kFixedFrameSizeFromFp);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000717 Comment(";;; Deferred code");
718 }
719
720 code->Generate();
721
722 if (NeedsDeferredFrame()) {
723 Comment(";;; Destroy frame");
724 DCHECK(frame_is_built_);
Ben Murdochda12d292016-06-02 14:46:10 +0100725 __ Pop(xzr, fp, lr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000726 frame_is_built_ = false;
727 }
728
729 __ B(code->exit());
730 }
731 }
732
733 // Force constant pool emission at the end of the deferred code to make
734 // sure that no constant pools are emitted after deferred code because
735 // deferred code generation is the last step which generates code. The two
736 // following steps will only output data used by crakshaft.
737 masm()->CheckConstPool(true, false);
738
739 return !is_aborted();
740}
741
742
743bool LCodeGen::GenerateJumpTable() {
744 Label needs_frame, call_deopt_entry;
745
746 if (jump_table_.length() > 0) {
747 Comment(";;; -------------------- Jump table --------------------");
748 Address base = jump_table_[0]->address;
749
750 UseScratchRegisterScope temps(masm());
751 Register entry_offset = temps.AcquireX();
752
753 int length = jump_table_.length();
754 for (int i = 0; i < length; i++) {
755 Deoptimizer::JumpTableEntry* table_entry = jump_table_[i];
756 __ Bind(&table_entry->label);
757
758 Address entry = table_entry->address;
759 DeoptComment(table_entry->deopt_info);
760
761 // Second-level deopt table entries are contiguous and small, so instead
762 // of loading the full, absolute address of each one, load the base
763 // address and add an immediate offset.
764 __ Mov(entry_offset, entry - base);
765
766 if (table_entry->needs_frame) {
767 DCHECK(!info()->saves_caller_doubles());
768 Comment(";;; call deopt with frame");
769 // Save lr before Bl, fp will be adjusted in the needs_frame code.
770 __ Push(lr, fp);
771 // Reuse the existing needs_frame code.
772 __ Bl(&needs_frame);
773 } else {
774 // There is nothing special to do, so just continue to the second-level
775 // table.
776 __ Bl(&call_deopt_entry);
777 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000778
779 masm()->CheckConstPool(false, false);
780 }
781
782 if (needs_frame.is_linked()) {
783 // This variant of deopt can only be used with stubs. Since we don't
784 // have a function pointer to install in the stack frame that we're
785 // building, install a special marker there instead.
786 DCHECK(info()->IsStub());
787
788 Comment(";;; needs_frame common code");
789 UseScratchRegisterScope temps(masm());
790 Register stub_marker = temps.AcquireX();
791 __ Bind(&needs_frame);
792 __ Mov(stub_marker, Smi::FromInt(StackFrame::STUB));
793 __ Push(cp, stub_marker);
794 __ Add(fp, __ StackPointer(), 2 * kPointerSize);
795 }
796
797 // Generate common code for calling the second-level deopt table.
798 __ Bind(&call_deopt_entry);
799
800 if (info()->saves_caller_doubles()) {
801 DCHECK(info()->IsStub());
802 RestoreCallerDoubles();
803 }
804
805 Register deopt_entry = temps.AcquireX();
806 __ Mov(deopt_entry, Operand(reinterpret_cast<uint64_t>(base),
807 RelocInfo::RUNTIME_ENTRY));
808 __ Add(deopt_entry, deopt_entry, entry_offset);
809 __ Br(deopt_entry);
810 }
811
812 // Force constant pool emission at the end of the deopt jump table to make
813 // sure that no constant pools are emitted after.
814 masm()->CheckConstPool(true, false);
815
816 // The deoptimization jump table is the last part of the instruction
817 // sequence. Mark the generated code as done unless we bailed out.
818 if (!is_aborted()) status_ = DONE;
819 return !is_aborted();
820}
821
822
823bool LCodeGen::GenerateSafepointTable() {
824 DCHECK(is_done());
825 // We do not know how much data will be emitted for the safepoint table, so
826 // force emission of the veneer pool.
827 masm()->CheckVeneerPool(true, true);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100828 safepoints_.Emit(masm(), GetTotalFrameSlotCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000829 return !is_aborted();
830}
831
832
833void LCodeGen::FinishCode(Handle<Code> code) {
834 DCHECK(is_done());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100835 code->set_stack_slots(GetTotalFrameSlotCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000836 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
837 PopulateDeoptimizationData(code);
838}
839
840
841void LCodeGen::DeoptimizeBranch(
842 LInstruction* instr, Deoptimizer::DeoptReason deopt_reason,
843 BranchType branch_type, Register reg, int bit,
844 Deoptimizer::BailoutType* override_bailout_type) {
845 LEnvironment* environment = instr->environment();
846 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
847 Deoptimizer::BailoutType bailout_type =
848 info()->IsStub() ? Deoptimizer::LAZY : Deoptimizer::EAGER;
849
850 if (override_bailout_type != NULL) {
851 bailout_type = *override_bailout_type;
852 }
853
854 DCHECK(environment->HasBeenRegistered());
855 int id = environment->deoptimization_index();
856 Address entry =
857 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
858
859 if (entry == NULL) {
860 Abort(kBailoutWasNotPrepared);
861 }
862
863 if (FLAG_deopt_every_n_times != 0 && !info()->IsStub()) {
864 Label not_zero;
865 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
866
867 __ Push(x0, x1, x2);
868 __ Mrs(x2, NZCV);
869 __ Mov(x0, count);
870 __ Ldr(w1, MemOperand(x0));
871 __ Subs(x1, x1, 1);
872 __ B(gt, &not_zero);
873 __ Mov(w1, FLAG_deopt_every_n_times);
874 __ Str(w1, MemOperand(x0));
875 __ Pop(x2, x1, x0);
876 DCHECK(frame_is_built_);
877 __ Call(entry, RelocInfo::RUNTIME_ENTRY);
878 __ Unreachable();
879
880 __ Bind(&not_zero);
881 __ Str(w1, MemOperand(x0));
882 __ Msr(NZCV, x2);
883 __ Pop(x2, x1, x0);
884 }
885
886 if (info()->ShouldTrapOnDeopt()) {
887 Label dont_trap;
888 __ B(&dont_trap, InvertBranchType(branch_type), reg, bit);
889 __ Debug("trap_on_deopt", __LINE__, BREAK);
890 __ Bind(&dont_trap);
891 }
892
Ben Murdochc5610432016-08-08 18:44:38 +0100893 Deoptimizer::DeoptInfo deopt_info = MakeDeoptInfo(instr, deopt_reason, id);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000894
895 DCHECK(info()->IsStub() || frame_is_built_);
896 // Go through jump table if we need to build frame, or restore caller doubles.
897 if (branch_type == always &&
898 frame_is_built_ && !info()->saves_caller_doubles()) {
899 DeoptComment(deopt_info);
900 __ Call(entry, RelocInfo::RUNTIME_ENTRY);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000901 } else {
902 Deoptimizer::JumpTableEntry* table_entry =
903 new (zone()) Deoptimizer::JumpTableEntry(
904 entry, deopt_info, bailout_type, !frame_is_built_);
905 // We often have several deopts to the same entry, reuse the last
906 // jump entry if this is the case.
907 if (FLAG_trace_deopt || isolate()->cpu_profiler()->is_profiling() ||
908 jump_table_.is_empty() ||
909 !table_entry->IsEquivalentTo(*jump_table_.last())) {
910 jump_table_.Add(table_entry, zone());
911 }
912 __ B(&jump_table_.last()->label, branch_type, reg, bit);
913 }
914}
915
916
917void LCodeGen::Deoptimize(LInstruction* instr,
918 Deoptimizer::DeoptReason deopt_reason,
919 Deoptimizer::BailoutType* override_bailout_type) {
920 DeoptimizeBranch(instr, deopt_reason, always, NoReg, -1,
921 override_bailout_type);
922}
923
924
925void LCodeGen::DeoptimizeIf(Condition cond, LInstruction* instr,
926 Deoptimizer::DeoptReason deopt_reason) {
927 DeoptimizeBranch(instr, deopt_reason, static_cast<BranchType>(cond));
928}
929
930
931void LCodeGen::DeoptimizeIfZero(Register rt, LInstruction* instr,
932 Deoptimizer::DeoptReason deopt_reason) {
933 DeoptimizeBranch(instr, deopt_reason, reg_zero, rt);
934}
935
936
937void LCodeGen::DeoptimizeIfNotZero(Register rt, LInstruction* instr,
938 Deoptimizer::DeoptReason deopt_reason) {
939 DeoptimizeBranch(instr, deopt_reason, reg_not_zero, rt);
940}
941
942
943void LCodeGen::DeoptimizeIfNegative(Register rt, LInstruction* instr,
944 Deoptimizer::DeoptReason deopt_reason) {
945 int sign_bit = rt.Is64Bits() ? kXSignBit : kWSignBit;
946 DeoptimizeIfBitSet(rt, sign_bit, instr, deopt_reason);
947}
948
949
950void LCodeGen::DeoptimizeIfSmi(Register rt, LInstruction* instr,
951 Deoptimizer::DeoptReason deopt_reason) {
952 DeoptimizeIfBitClear(rt, MaskToBit(kSmiTagMask), instr, deopt_reason);
953}
954
955
956void LCodeGen::DeoptimizeIfNotSmi(Register rt, LInstruction* instr,
957 Deoptimizer::DeoptReason deopt_reason) {
958 DeoptimizeIfBitSet(rt, MaskToBit(kSmiTagMask), instr, deopt_reason);
959}
960
961
962void LCodeGen::DeoptimizeIfRoot(Register rt, Heap::RootListIndex index,
963 LInstruction* instr,
964 Deoptimizer::DeoptReason deopt_reason) {
965 __ CompareRoot(rt, index);
966 DeoptimizeIf(eq, instr, deopt_reason);
967}
968
969
970void LCodeGen::DeoptimizeIfNotRoot(Register rt, Heap::RootListIndex index,
971 LInstruction* instr,
972 Deoptimizer::DeoptReason deopt_reason) {
973 __ CompareRoot(rt, index);
974 DeoptimizeIf(ne, instr, deopt_reason);
975}
976
977
978void LCodeGen::DeoptimizeIfMinusZero(DoubleRegister input, LInstruction* instr,
979 Deoptimizer::DeoptReason deopt_reason) {
980 __ TestForMinusZero(input);
981 DeoptimizeIf(vs, instr, deopt_reason);
982}
983
984
985void LCodeGen::DeoptimizeIfNotHeapNumber(Register object, LInstruction* instr) {
986 __ CompareObjectMap(object, Heap::kHeapNumberMapRootIndex);
987 DeoptimizeIf(ne, instr, Deoptimizer::kNotAHeapNumber);
988}
989
990
991void LCodeGen::DeoptimizeIfBitSet(Register rt, int bit, LInstruction* instr,
992 Deoptimizer::DeoptReason deopt_reason) {
993 DeoptimizeBranch(instr, deopt_reason, reg_bit_set, rt, bit);
994}
995
996
997void LCodeGen::DeoptimizeIfBitClear(Register rt, int bit, LInstruction* instr,
998 Deoptimizer::DeoptReason deopt_reason) {
999 DeoptimizeBranch(instr, deopt_reason, reg_bit_clear, rt, bit);
1000}
1001
1002
1003void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
1004 if (info()->ShouldEnsureSpaceForLazyDeopt()) {
1005 // Ensure that we have enough space after the previous lazy-bailout
1006 // instruction for patching the code here.
1007 intptr_t current_pc = masm()->pc_offset();
1008
1009 if (current_pc < (last_lazy_deopt_pc_ + space_needed)) {
1010 ptrdiff_t padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
1011 DCHECK((padding_size % kInstructionSize) == 0);
1012 InstructionAccurateScope instruction_accurate(
1013 masm(), padding_size / kInstructionSize);
1014
1015 while (padding_size > 0) {
1016 __ nop();
1017 padding_size -= kInstructionSize;
1018 }
1019 }
1020 }
1021 last_lazy_deopt_pc_ = masm()->pc_offset();
1022}
1023
1024
1025Register LCodeGen::ToRegister(LOperand* op) const {
1026 // TODO(all): support zero register results, as ToRegister32.
1027 DCHECK((op != NULL) && op->IsRegister());
1028 return Register::from_code(op->index());
1029}
1030
1031
1032Register LCodeGen::ToRegister32(LOperand* op) const {
1033 DCHECK(op != NULL);
1034 if (op->IsConstantOperand()) {
1035 // If this is a constant operand, the result must be the zero register.
1036 DCHECK(ToInteger32(LConstantOperand::cast(op)) == 0);
1037 return wzr;
1038 } else {
1039 return ToRegister(op).W();
1040 }
1041}
1042
1043
1044Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
1045 HConstant* constant = chunk_->LookupConstant(op);
1046 return Smi::FromInt(constant->Integer32Value());
1047}
1048
1049
1050DoubleRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
1051 DCHECK((op != NULL) && op->IsDoubleRegister());
1052 return DoubleRegister::from_code(op->index());
1053}
1054
1055
1056Operand LCodeGen::ToOperand(LOperand* op) {
1057 DCHECK(op != NULL);
1058 if (op->IsConstantOperand()) {
1059 LConstantOperand* const_op = LConstantOperand::cast(op);
1060 HConstant* constant = chunk()->LookupConstant(const_op);
1061 Representation r = chunk_->LookupLiteralRepresentation(const_op);
1062 if (r.IsSmi()) {
1063 DCHECK(constant->HasSmiValue());
1064 return Operand(Smi::FromInt(constant->Integer32Value()));
1065 } else if (r.IsInteger32()) {
1066 DCHECK(constant->HasInteger32Value());
1067 return Operand(constant->Integer32Value());
1068 } else if (r.IsDouble()) {
1069 Abort(kToOperandUnsupportedDoubleImmediate);
1070 }
1071 DCHECK(r.IsTagged());
1072 return Operand(constant->handle(isolate()));
1073 } else if (op->IsRegister()) {
1074 return Operand(ToRegister(op));
1075 } else if (op->IsDoubleRegister()) {
1076 Abort(kToOperandIsDoubleRegisterUnimplemented);
1077 return Operand(0);
1078 }
1079 // Stack slots not implemented, use ToMemOperand instead.
1080 UNREACHABLE();
1081 return Operand(0);
1082}
1083
1084
1085Operand LCodeGen::ToOperand32(LOperand* op) {
1086 DCHECK(op != NULL);
1087 if (op->IsRegister()) {
1088 return Operand(ToRegister32(op));
1089 } else if (op->IsConstantOperand()) {
1090 LConstantOperand* const_op = LConstantOperand::cast(op);
1091 HConstant* constant = chunk()->LookupConstant(const_op);
1092 Representation r = chunk_->LookupLiteralRepresentation(const_op);
1093 if (r.IsInteger32()) {
1094 return Operand(constant->Integer32Value());
1095 } else {
1096 // Other constants not implemented.
1097 Abort(kToOperand32UnsupportedImmediate);
1098 }
1099 }
1100 // Other cases are not implemented.
1101 UNREACHABLE();
1102 return Operand(0);
1103}
1104
1105
1106static int64_t ArgumentsOffsetWithoutFrame(int index) {
1107 DCHECK(index < 0);
1108 return -(index + 1) * kPointerSize;
1109}
1110
1111
1112MemOperand LCodeGen::ToMemOperand(LOperand* op, StackMode stack_mode) const {
1113 DCHECK(op != NULL);
1114 DCHECK(!op->IsRegister());
1115 DCHECK(!op->IsDoubleRegister());
1116 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
1117 if (NeedsEagerFrame()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001118 int fp_offset = FrameSlotToFPOffset(op->index());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001119 // Loads and stores have a bigger reach in positive offset than negative.
1120 // We try to access using jssp (positive offset) first, then fall back to
1121 // fp (negative offset) if that fails.
1122 //
1123 // We can reference a stack slot from jssp only if we know how much we've
1124 // put on the stack. We don't know this in the following cases:
1125 // - stack_mode != kCanUseStackPointer: this is the case when deferred
1126 // code has saved the registers.
1127 // - saves_caller_doubles(): some double registers have been pushed, jssp
1128 // references the end of the double registers and not the end of the stack
1129 // slots.
1130 // In both of the cases above, we _could_ add the tracking information
1131 // required so that we can use jssp here, but in practice it isn't worth it.
1132 if ((stack_mode == kCanUseStackPointer) &&
1133 !info()->saves_caller_doubles()) {
1134 int jssp_offset_to_fp =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001135 (pushed_arguments_ + GetTotalFrameSlotCount()) * kPointerSize -
1136 StandardFrameConstants::kFixedFrameSizeAboveFp;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001137 int jssp_offset = fp_offset + jssp_offset_to_fp;
1138 if (masm()->IsImmLSScaled(jssp_offset, LSDoubleWord)) {
1139 return MemOperand(masm()->StackPointer(), jssp_offset);
1140 }
1141 }
1142 return MemOperand(fp, fp_offset);
1143 } else {
1144 // Retrieve parameter without eager stack-frame relative to the
1145 // stack-pointer.
1146 return MemOperand(masm()->StackPointer(),
1147 ArgumentsOffsetWithoutFrame(op->index()));
1148 }
1149}
1150
1151
1152Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
1153 HConstant* constant = chunk_->LookupConstant(op);
1154 DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
1155 return constant->handle(isolate());
1156}
1157
1158
1159template <class LI>
1160Operand LCodeGen::ToShiftedRightOperand32(LOperand* right, LI* shift_info) {
1161 if (shift_info->shift() == NO_SHIFT) {
1162 return ToOperand32(right);
1163 } else {
1164 return Operand(
1165 ToRegister32(right),
1166 shift_info->shift(),
1167 JSShiftAmountFromLConstant(shift_info->shift_amount()));
1168 }
1169}
1170
1171
1172bool LCodeGen::IsSmi(LConstantOperand* op) const {
1173 return chunk_->LookupLiteralRepresentation(op).IsSmi();
1174}
1175
1176
1177bool LCodeGen::IsInteger32Constant(LConstantOperand* op) const {
1178 return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
1179}
1180
1181
1182int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
1183 HConstant* constant = chunk_->LookupConstant(op);
1184 return constant->Integer32Value();
1185}
1186
1187
1188double LCodeGen::ToDouble(LConstantOperand* op) const {
1189 HConstant* constant = chunk_->LookupConstant(op);
1190 DCHECK(constant->HasDoubleValue());
1191 return constant->DoubleValue();
1192}
1193
1194
1195Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
1196 Condition cond = nv;
1197 switch (op) {
1198 case Token::EQ:
1199 case Token::EQ_STRICT:
1200 cond = eq;
1201 break;
1202 case Token::NE:
1203 case Token::NE_STRICT:
1204 cond = ne;
1205 break;
1206 case Token::LT:
1207 cond = is_unsigned ? lo : lt;
1208 break;
1209 case Token::GT:
1210 cond = is_unsigned ? hi : gt;
1211 break;
1212 case Token::LTE:
1213 cond = is_unsigned ? ls : le;
1214 break;
1215 case Token::GTE:
1216 cond = is_unsigned ? hs : ge;
1217 break;
1218 case Token::IN:
1219 case Token::INSTANCEOF:
1220 default:
1221 UNREACHABLE();
1222 }
1223 return cond;
1224}
1225
1226
1227template<class InstrType>
1228void LCodeGen::EmitBranchGeneric(InstrType instr,
1229 const BranchGenerator& branch) {
1230 int left_block = instr->TrueDestination(chunk_);
1231 int right_block = instr->FalseDestination(chunk_);
1232
1233 int next_block = GetNextEmittedBlock();
1234
1235 if (right_block == left_block) {
1236 EmitGoto(left_block);
1237 } else if (left_block == next_block) {
1238 branch.EmitInverted(chunk_->GetAssemblyLabel(right_block));
1239 } else {
1240 branch.Emit(chunk_->GetAssemblyLabel(left_block));
1241 if (right_block != next_block) {
1242 __ B(chunk_->GetAssemblyLabel(right_block));
1243 }
1244 }
1245}
1246
1247
1248template<class InstrType>
1249void LCodeGen::EmitBranch(InstrType instr, Condition condition) {
1250 DCHECK((condition != al) && (condition != nv));
1251 BranchOnCondition branch(this, condition);
1252 EmitBranchGeneric(instr, branch);
1253}
1254
1255
1256template<class InstrType>
1257void LCodeGen::EmitCompareAndBranch(InstrType instr,
1258 Condition condition,
1259 const Register& lhs,
1260 const Operand& rhs) {
1261 DCHECK((condition != al) && (condition != nv));
1262 CompareAndBranch branch(this, condition, lhs, rhs);
1263 EmitBranchGeneric(instr, branch);
1264}
1265
1266
1267template<class InstrType>
1268void LCodeGen::EmitTestAndBranch(InstrType instr,
1269 Condition condition,
1270 const Register& value,
1271 uint64_t mask) {
1272 DCHECK((condition != al) && (condition != nv));
1273 TestAndBranch branch(this, condition, value, mask);
1274 EmitBranchGeneric(instr, branch);
1275}
1276
1277
1278template<class InstrType>
1279void LCodeGen::EmitBranchIfNonZeroNumber(InstrType instr,
1280 const FPRegister& value,
1281 const FPRegister& scratch) {
1282 BranchIfNonZeroNumber branch(this, value, scratch);
1283 EmitBranchGeneric(instr, branch);
1284}
1285
1286
1287template<class InstrType>
1288void LCodeGen::EmitBranchIfHeapNumber(InstrType instr,
1289 const Register& value) {
1290 BranchIfHeapNumber branch(this, value);
1291 EmitBranchGeneric(instr, branch);
1292}
1293
1294
1295template<class InstrType>
1296void LCodeGen::EmitBranchIfRoot(InstrType instr,
1297 const Register& value,
1298 Heap::RootListIndex index) {
1299 BranchIfRoot branch(this, value, index);
1300 EmitBranchGeneric(instr, branch);
1301}
1302
1303
1304void LCodeGen::DoGap(LGap* gap) {
1305 for (int i = LGap::FIRST_INNER_POSITION;
1306 i <= LGap::LAST_INNER_POSITION;
1307 i++) {
1308 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
1309 LParallelMove* move = gap->GetParallelMove(inner_pos);
1310 if (move != NULL) {
1311 resolver_.Resolve(move);
1312 }
1313 }
1314}
1315
1316
1317void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
1318 Register arguments = ToRegister(instr->arguments());
1319 Register result = ToRegister(instr->result());
1320
1321 // The pointer to the arguments array come from DoArgumentsElements.
1322 // It does not point directly to the arguments and there is an offest of
1323 // two words that we must take into account when accessing an argument.
1324 // Subtracting the index from length accounts for one, so we add one more.
1325
1326 if (instr->length()->IsConstantOperand() &&
1327 instr->index()->IsConstantOperand()) {
1328 int index = ToInteger32(LConstantOperand::cast(instr->index()));
1329 int length = ToInteger32(LConstantOperand::cast(instr->length()));
1330 int offset = ((length - index) + 1) * kPointerSize;
1331 __ Ldr(result, MemOperand(arguments, offset));
1332 } else if (instr->index()->IsConstantOperand()) {
1333 Register length = ToRegister32(instr->length());
1334 int index = ToInteger32(LConstantOperand::cast(instr->index()));
1335 int loc = index - 1;
1336 if (loc != 0) {
1337 __ Sub(result.W(), length, loc);
1338 __ Ldr(result, MemOperand(arguments, result, UXTW, kPointerSizeLog2));
1339 } else {
1340 __ Ldr(result, MemOperand(arguments, length, UXTW, kPointerSizeLog2));
1341 }
1342 } else {
1343 Register length = ToRegister32(instr->length());
1344 Operand index = ToOperand32(instr->index());
1345 __ Sub(result.W(), length, index);
1346 __ Add(result.W(), result.W(), 1);
1347 __ Ldr(result, MemOperand(arguments, result, UXTW, kPointerSizeLog2));
1348 }
1349}
1350
1351
1352void LCodeGen::DoAddE(LAddE* instr) {
1353 Register result = ToRegister(instr->result());
1354 Register left = ToRegister(instr->left());
1355 Operand right = Operand(x0); // Dummy initialization.
1356 if (instr->hydrogen()->external_add_type() == AddOfExternalAndTagged) {
1357 right = Operand(ToRegister(instr->right()));
1358 } else if (instr->right()->IsConstantOperand()) {
1359 right = ToInteger32(LConstantOperand::cast(instr->right()));
1360 } else {
1361 right = Operand(ToRegister32(instr->right()), SXTW);
1362 }
1363
1364 DCHECK(!instr->hydrogen()->CheckFlag(HValue::kCanOverflow));
1365 __ Add(result, left, right);
1366}
1367
1368
1369void LCodeGen::DoAddI(LAddI* instr) {
1370 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1371 Register result = ToRegister32(instr->result());
1372 Register left = ToRegister32(instr->left());
1373 Operand right = ToShiftedRightOperand32(instr->right(), instr);
1374
1375 if (can_overflow) {
1376 __ Adds(result, left, right);
1377 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
1378 } else {
1379 __ Add(result, left, right);
1380 }
1381}
1382
1383
1384void LCodeGen::DoAddS(LAddS* instr) {
1385 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1386 Register result = ToRegister(instr->result());
1387 Register left = ToRegister(instr->left());
1388 Operand right = ToOperand(instr->right());
1389 if (can_overflow) {
1390 __ Adds(result, left, right);
1391 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
1392 } else {
1393 __ Add(result, left, right);
1394 }
1395}
1396
1397
1398void LCodeGen::DoAllocate(LAllocate* instr) {
1399 class DeferredAllocate: public LDeferredCode {
1400 public:
1401 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
1402 : LDeferredCode(codegen), instr_(instr) { }
1403 virtual void Generate() { codegen()->DoDeferredAllocate(instr_); }
1404 virtual LInstruction* instr() { return instr_; }
1405 private:
1406 LAllocate* instr_;
1407 };
1408
1409 DeferredAllocate* deferred = new(zone()) DeferredAllocate(this, instr);
1410
1411 Register result = ToRegister(instr->result());
1412 Register temp1 = ToRegister(instr->temp1());
1413 Register temp2 = ToRegister(instr->temp2());
1414
1415 // Allocate memory for the object.
Ben Murdochc5610432016-08-08 18:44:38 +01001416 AllocationFlags flags = NO_ALLOCATION_FLAGS;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001417 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
1418 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
1419 }
1420
1421 if (instr->hydrogen()->IsOldSpaceAllocation()) {
1422 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
1423 flags = static_cast<AllocationFlags>(flags | PRETENURE);
1424 }
1425
Ben Murdochc5610432016-08-08 18:44:38 +01001426 if (instr->hydrogen()->IsAllocationFoldingDominator()) {
1427 flags = static_cast<AllocationFlags>(flags | ALLOCATION_FOLDING_DOMINATOR);
1428 }
1429 DCHECK(!instr->hydrogen()->IsAllocationFolded());
1430
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001431 if (instr->size()->IsConstantOperand()) {
1432 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
1433 CHECK(size <= Page::kMaxRegularHeapObjectSize);
1434 __ Allocate(size, result, temp1, temp2, deferred->entry(), flags);
1435 } else {
1436 Register size = ToRegister32(instr->size());
1437 __ Sxtw(size.X(), size);
1438 __ Allocate(size.X(), result, temp1, temp2, deferred->entry(), flags);
1439 }
1440
1441 __ Bind(deferred->exit());
1442
1443 if (instr->hydrogen()->MustPrefillWithFiller()) {
1444 Register start = temp1;
1445 Register end = temp2;
1446 Register filler = ToRegister(instr->temp3());
1447
1448 __ Sub(start, result, kHeapObjectTag);
1449
1450 if (instr->size()->IsConstantOperand()) {
1451 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
1452 __ Add(end, start, size);
1453 } else {
1454 __ Add(end, start, ToRegister(instr->size()));
1455 }
1456 __ LoadRoot(filler, Heap::kOnePointerFillerMapRootIndex);
1457 __ InitializeFieldsWithFiller(start, end, filler);
1458 } else {
1459 DCHECK(instr->temp3() == NULL);
1460 }
1461}
1462
1463
1464void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
1465 // TODO(3095996): Get rid of this. For now, we need to make the
1466 // result register contain a valid pointer because it is already
1467 // contained in the register pointer map.
1468 __ Mov(ToRegister(instr->result()), Smi::FromInt(0));
1469
1470 PushSafepointRegistersScope scope(this);
1471 // We're in a SafepointRegistersScope so we can use any scratch registers.
1472 Register size = x0;
1473 if (instr->size()->IsConstantOperand()) {
1474 __ Mov(size, ToSmi(LConstantOperand::cast(instr->size())));
1475 } else {
1476 __ SmiTag(size, ToRegister32(instr->size()).X());
1477 }
1478 int flags = AllocateDoubleAlignFlag::encode(
1479 instr->hydrogen()->MustAllocateDoubleAligned());
1480 if (instr->hydrogen()->IsOldSpaceAllocation()) {
1481 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
1482 flags = AllocateTargetSpace::update(flags, OLD_SPACE);
1483 } else {
1484 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
1485 }
1486 __ Mov(x10, Smi::FromInt(flags));
1487 __ Push(size, x10);
1488
1489 CallRuntimeFromDeferred(
1490 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
1491 __ StoreToSafepointRegisterSlot(x0, ToRegister(instr->result()));
Ben Murdochc5610432016-08-08 18:44:38 +01001492
1493 if (instr->hydrogen()->IsAllocationFoldingDominator()) {
1494 AllocationFlags allocation_flags = NO_ALLOCATION_FLAGS;
1495 if (instr->hydrogen()->IsOldSpaceAllocation()) {
1496 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
1497 allocation_flags = static_cast<AllocationFlags>(flags | PRETENURE);
1498 }
1499 // If the allocation folding dominator allocate triggered a GC, allocation
1500 // happend in the runtime. We have to reset the top pointer to virtually
1501 // undo the allocation.
1502 ExternalReference allocation_top =
1503 AllocationUtils::GetAllocationTopReference(isolate(), allocation_flags);
1504 Register top_address = x10;
1505 __ Sub(x0, x0, Operand(kHeapObjectTag));
1506 __ Mov(top_address, Operand(allocation_top));
1507 __ Str(x0, MemOperand(top_address));
1508 __ Add(x0, x0, Operand(kHeapObjectTag));
1509 }
1510}
1511
1512void LCodeGen::DoFastAllocate(LFastAllocate* instr) {
1513 DCHECK(instr->hydrogen()->IsAllocationFolded());
1514 DCHECK(!instr->hydrogen()->IsAllocationFoldingDominator());
1515 Register result = ToRegister(instr->result());
1516 Register scratch1 = ToRegister(instr->temp1());
1517 Register scratch2 = ToRegister(instr->temp2());
1518
1519 AllocationFlags flags = ALLOCATION_FOLDED;
1520 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
1521 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
1522 }
1523 if (instr->hydrogen()->IsOldSpaceAllocation()) {
1524 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
1525 flags = static_cast<AllocationFlags>(flags | PRETENURE);
1526 }
1527 if (instr->size()->IsConstantOperand()) {
1528 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
1529 CHECK(size <= Page::kMaxRegularHeapObjectSize);
1530 __ FastAllocate(size, result, scratch1, scratch2, flags);
1531 } else {
1532 Register size = ToRegister(instr->size());
1533 __ FastAllocate(size, result, scratch1, scratch2, flags);
1534 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001535}
1536
1537
1538void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
1539 Register receiver = ToRegister(instr->receiver());
1540 Register function = ToRegister(instr->function());
1541 Register length = ToRegister32(instr->length());
1542
1543 Register elements = ToRegister(instr->elements());
1544 Register scratch = x5;
1545 DCHECK(receiver.Is(x0)); // Used for parameter count.
1546 DCHECK(function.Is(x1)); // Required by InvokeFunction.
1547 DCHECK(ToRegister(instr->result()).Is(x0));
1548 DCHECK(instr->IsMarkedAsCall());
1549
1550 // Copy the arguments to this function possibly from the
1551 // adaptor frame below it.
1552 const uint32_t kArgumentsLimit = 1 * KB;
1553 __ Cmp(length, kArgumentsLimit);
1554 DeoptimizeIf(hi, instr, Deoptimizer::kTooManyArguments);
1555
1556 // Push the receiver and use the register to keep the original
1557 // number of arguments.
1558 __ Push(receiver);
1559 Register argc = receiver;
1560 receiver = NoReg;
1561 __ Sxtw(argc, length);
1562 // The arguments are at a one pointer size offset from elements.
1563 __ Add(elements, elements, 1 * kPointerSize);
1564
1565 // Loop through the arguments pushing them onto the execution
1566 // stack.
1567 Label invoke, loop;
1568 // length is a small non-negative integer, due to the test above.
1569 __ Cbz(length, &invoke);
1570 __ Bind(&loop);
1571 __ Ldr(scratch, MemOperand(elements, length, SXTW, kPointerSizeLog2));
1572 __ Push(scratch);
1573 __ Subs(length, length, 1);
1574 __ B(ne, &loop);
1575
1576 __ Bind(&invoke);
Ben Murdochda12d292016-06-02 14:46:10 +01001577
1578 InvokeFlag flag = CALL_FUNCTION;
1579 if (instr->hydrogen()->tail_call_mode() == TailCallMode::kAllow) {
1580 DCHECK(!info()->saves_caller_doubles());
1581 // TODO(ishell): drop current frame before pushing arguments to the stack.
1582 flag = JUMP_FUNCTION;
1583 ParameterCount actual(x0);
1584 // It is safe to use x3, x4 and x5 as scratch registers here given that
1585 // 1) we are not going to return to caller function anyway,
1586 // 2) x3 (new.target) will be initialized below.
1587 PrepareForTailCall(actual, x3, x4, x5);
1588 }
1589
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001590 DCHECK(instr->HasPointerMap());
1591 LPointerMap* pointers = instr->pointer_map();
1592 SafepointGenerator safepoint_generator(this, pointers, Safepoint::kLazyDeopt);
1593 // The number of arguments is stored in argc (receiver) which is x0, as
1594 // expected by InvokeFunction.
1595 ParameterCount actual(argc);
Ben Murdochda12d292016-06-02 14:46:10 +01001596 __ InvokeFunction(function, no_reg, actual, flag, safepoint_generator);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001597}
1598
1599
1600void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
1601 Register result = ToRegister(instr->result());
1602
1603 if (instr->hydrogen()->from_inlined()) {
1604 // When we are inside an inlined function, the arguments are the last things
1605 // that have been pushed on the stack. Therefore the arguments array can be
1606 // accessed directly from jssp.
1607 // However in the normal case, it is accessed via fp but there are two words
1608 // on the stack between fp and the arguments (the saved lr and fp) and the
1609 // LAccessArgumentsAt implementation take that into account.
1610 // In the inlined case we need to subtract the size of 2 words to jssp to
1611 // get a pointer which will work well with LAccessArgumentsAt.
1612 DCHECK(masm()->StackPointer().Is(jssp));
1613 __ Sub(result, jssp, 2 * kPointerSize);
Ben Murdochda12d292016-06-02 14:46:10 +01001614 } else if (instr->hydrogen()->arguments_adaptor()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001615 DCHECK(instr->temp() != NULL);
1616 Register previous_fp = ToRegister(instr->temp());
1617
1618 __ Ldr(previous_fp,
1619 MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01001620 __ Ldr(result, MemOperand(previous_fp,
1621 CommonFrameConstants::kContextOrFrameTypeOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001622 __ Cmp(result, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
1623 __ Csel(result, fp, previous_fp, ne);
Ben Murdochda12d292016-06-02 14:46:10 +01001624 } else {
1625 __ Mov(result, fp);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001626 }
1627}
1628
1629
1630void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
1631 Register elements = ToRegister(instr->elements());
1632 Register result = ToRegister32(instr->result());
1633 Label done;
1634
1635 // If no arguments adaptor frame the number of arguments is fixed.
1636 __ Cmp(fp, elements);
1637 __ Mov(result, scope()->num_parameters());
1638 __ B(eq, &done);
1639
1640 // Arguments adaptor frame present. Get argument length from there.
1641 __ Ldr(result.X(), MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1642 __ Ldr(result,
1643 UntagSmiMemOperand(result.X(),
1644 ArgumentsAdaptorFrameConstants::kLengthOffset));
1645
1646 // Argument length is in result register.
1647 __ Bind(&done);
1648}
1649
1650
1651void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
1652 DoubleRegister left = ToDoubleRegister(instr->left());
1653 DoubleRegister right = ToDoubleRegister(instr->right());
1654 DoubleRegister result = ToDoubleRegister(instr->result());
1655
1656 switch (instr->op()) {
1657 case Token::ADD: __ Fadd(result, left, right); break;
1658 case Token::SUB: __ Fsub(result, left, right); break;
1659 case Token::MUL: __ Fmul(result, left, right); break;
1660 case Token::DIV: __ Fdiv(result, left, right); break;
1661 case Token::MOD: {
1662 // The ECMA-262 remainder operator is the remainder from a truncating
1663 // (round-towards-zero) division. Note that this differs from IEEE-754.
1664 //
1665 // TODO(jbramley): See if it's possible to do this inline, rather than by
1666 // calling a helper function. With frintz (to produce the intermediate
1667 // quotient) and fmsub (to calculate the remainder without loss of
1668 // precision), it should be possible. However, we would need support for
1669 // fdiv in round-towards-zero mode, and the ARM64 simulator doesn't
1670 // support that yet.
1671 DCHECK(left.Is(d0));
1672 DCHECK(right.Is(d1));
1673 __ CallCFunction(
1674 ExternalReference::mod_two_doubles_operation(isolate()),
1675 0, 2);
1676 DCHECK(result.Is(d0));
1677 break;
1678 }
1679 default:
1680 UNREACHABLE();
1681 break;
1682 }
1683}
1684
1685
1686void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
1687 DCHECK(ToRegister(instr->context()).is(cp));
1688 DCHECK(ToRegister(instr->left()).is(x1));
1689 DCHECK(ToRegister(instr->right()).is(x0));
1690 DCHECK(ToRegister(instr->result()).is(x0));
1691
Ben Murdoch097c5b22016-05-18 11:27:45 +01001692 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), instr->op()).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001693 CallCode(code, RelocInfo::CODE_TARGET, instr);
1694}
1695
1696
1697void LCodeGen::DoBitI(LBitI* instr) {
1698 Register result = ToRegister32(instr->result());
1699 Register left = ToRegister32(instr->left());
1700 Operand right = ToShiftedRightOperand32(instr->right(), instr);
1701
1702 switch (instr->op()) {
1703 case Token::BIT_AND: __ And(result, left, right); break;
1704 case Token::BIT_OR: __ Orr(result, left, right); break;
1705 case Token::BIT_XOR: __ Eor(result, left, right); break;
1706 default:
1707 UNREACHABLE();
1708 break;
1709 }
1710}
1711
1712
1713void LCodeGen::DoBitS(LBitS* instr) {
1714 Register result = ToRegister(instr->result());
1715 Register left = ToRegister(instr->left());
1716 Operand right = ToOperand(instr->right());
1717
1718 switch (instr->op()) {
1719 case Token::BIT_AND: __ And(result, left, right); break;
1720 case Token::BIT_OR: __ Orr(result, left, right); break;
1721 case Token::BIT_XOR: __ Eor(result, left, right); break;
1722 default:
1723 UNREACHABLE();
1724 break;
1725 }
1726}
1727
1728
1729void LCodeGen::DoBoundsCheck(LBoundsCheck *instr) {
1730 Condition cond = instr->hydrogen()->allow_equality() ? hi : hs;
1731 DCHECK(instr->hydrogen()->index()->representation().IsInteger32());
1732 DCHECK(instr->hydrogen()->length()->representation().IsInteger32());
1733 if (instr->index()->IsConstantOperand()) {
1734 Operand index = ToOperand32(instr->index());
1735 Register length = ToRegister32(instr->length());
1736 __ Cmp(length, index);
1737 cond = CommuteCondition(cond);
1738 } else {
1739 Register index = ToRegister32(instr->index());
1740 Operand length = ToOperand32(instr->length());
1741 __ Cmp(index, length);
1742 }
1743 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
1744 __ Assert(NegateCondition(cond), kEliminatedBoundsCheckFailed);
1745 } else {
1746 DeoptimizeIf(cond, instr, Deoptimizer::kOutOfBounds);
1747 }
1748}
1749
1750
1751void LCodeGen::DoBranch(LBranch* instr) {
1752 Representation r = instr->hydrogen()->value()->representation();
1753 Label* true_label = instr->TrueLabel(chunk_);
1754 Label* false_label = instr->FalseLabel(chunk_);
1755
1756 if (r.IsInteger32()) {
1757 DCHECK(!info()->IsStub());
1758 EmitCompareAndBranch(instr, ne, ToRegister32(instr->value()), 0);
1759 } else if (r.IsSmi()) {
1760 DCHECK(!info()->IsStub());
1761 STATIC_ASSERT(kSmiTag == 0);
1762 EmitCompareAndBranch(instr, ne, ToRegister(instr->value()), 0);
1763 } else if (r.IsDouble()) {
1764 DoubleRegister value = ToDoubleRegister(instr->value());
1765 // Test the double value. Zero and NaN are false.
1766 EmitBranchIfNonZeroNumber(instr, value, double_scratch());
1767 } else {
1768 DCHECK(r.IsTagged());
1769 Register value = ToRegister(instr->value());
1770 HType type = instr->hydrogen()->value()->type();
1771
1772 if (type.IsBoolean()) {
1773 DCHECK(!info()->IsStub());
1774 __ CompareRoot(value, Heap::kTrueValueRootIndex);
1775 EmitBranch(instr, eq);
1776 } else if (type.IsSmi()) {
1777 DCHECK(!info()->IsStub());
1778 EmitCompareAndBranch(instr, ne, value, Smi::FromInt(0));
1779 } else if (type.IsJSArray()) {
1780 DCHECK(!info()->IsStub());
1781 EmitGoto(instr->TrueDestination(chunk()));
1782 } else if (type.IsHeapNumber()) {
1783 DCHECK(!info()->IsStub());
1784 __ Ldr(double_scratch(), FieldMemOperand(value,
1785 HeapNumber::kValueOffset));
1786 // Test the double value. Zero and NaN are false.
1787 EmitBranchIfNonZeroNumber(instr, double_scratch(), double_scratch());
1788 } else if (type.IsString()) {
1789 DCHECK(!info()->IsStub());
1790 Register temp = ToRegister(instr->temp1());
1791 __ Ldr(temp, FieldMemOperand(value, String::kLengthOffset));
1792 EmitCompareAndBranch(instr, ne, temp, 0);
1793 } else {
Ben Murdochda12d292016-06-02 14:46:10 +01001794 ToBooleanICStub::Types expected =
1795 instr->hydrogen()->expected_input_types();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001796 // Avoid deopts in the case where we've never executed this path before.
Ben Murdochda12d292016-06-02 14:46:10 +01001797 if (expected.IsEmpty()) expected = ToBooleanICStub::Types::Generic();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001798
Ben Murdochda12d292016-06-02 14:46:10 +01001799 if (expected.Contains(ToBooleanICStub::UNDEFINED)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001800 // undefined -> false.
1801 __ JumpIfRoot(
1802 value, Heap::kUndefinedValueRootIndex, false_label);
1803 }
1804
Ben Murdochda12d292016-06-02 14:46:10 +01001805 if (expected.Contains(ToBooleanICStub::BOOLEAN)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001806 // Boolean -> its value.
1807 __ JumpIfRoot(
1808 value, Heap::kTrueValueRootIndex, true_label);
1809 __ JumpIfRoot(
1810 value, Heap::kFalseValueRootIndex, false_label);
1811 }
1812
Ben Murdochda12d292016-06-02 14:46:10 +01001813 if (expected.Contains(ToBooleanICStub::NULL_TYPE)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001814 // 'null' -> false.
1815 __ JumpIfRoot(
1816 value, Heap::kNullValueRootIndex, false_label);
1817 }
1818
Ben Murdochda12d292016-06-02 14:46:10 +01001819 if (expected.Contains(ToBooleanICStub::SMI)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001820 // Smis: 0 -> false, all other -> true.
1821 DCHECK(Smi::FromInt(0) == 0);
1822 __ Cbz(value, false_label);
1823 __ JumpIfSmi(value, true_label);
1824 } else if (expected.NeedsMap()) {
1825 // If we need a map later and have a smi, deopt.
1826 DeoptimizeIfSmi(value, instr, Deoptimizer::kSmi);
1827 }
1828
1829 Register map = NoReg;
1830 Register scratch = NoReg;
1831
1832 if (expected.NeedsMap()) {
1833 DCHECK((instr->temp1() != NULL) && (instr->temp2() != NULL));
1834 map = ToRegister(instr->temp1());
1835 scratch = ToRegister(instr->temp2());
1836
1837 __ Ldr(map, FieldMemOperand(value, HeapObject::kMapOffset));
1838
1839 if (expected.CanBeUndetectable()) {
1840 // Undetectable -> false.
1841 __ Ldrb(scratch, FieldMemOperand(map, Map::kBitFieldOffset));
1842 __ TestAndBranchIfAnySet(
1843 scratch, 1 << Map::kIsUndetectable, false_label);
1844 }
1845 }
1846
Ben Murdochda12d292016-06-02 14:46:10 +01001847 if (expected.Contains(ToBooleanICStub::SPEC_OBJECT)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001848 // spec object -> true.
1849 __ CompareInstanceType(map, scratch, FIRST_JS_RECEIVER_TYPE);
1850 __ B(ge, true_label);
1851 }
1852
Ben Murdochda12d292016-06-02 14:46:10 +01001853 if (expected.Contains(ToBooleanICStub::STRING)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001854 // String value -> false iff empty.
1855 Label not_string;
1856 __ CompareInstanceType(map, scratch, FIRST_NONSTRING_TYPE);
1857 __ B(ge, &not_string);
1858 __ Ldr(scratch, FieldMemOperand(value, String::kLengthOffset));
1859 __ Cbz(scratch, false_label);
1860 __ B(true_label);
1861 __ Bind(&not_string);
1862 }
1863
Ben Murdochda12d292016-06-02 14:46:10 +01001864 if (expected.Contains(ToBooleanICStub::SYMBOL)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001865 // Symbol value -> true.
1866 __ CompareInstanceType(map, scratch, SYMBOL_TYPE);
1867 __ B(eq, true_label);
1868 }
1869
Ben Murdochda12d292016-06-02 14:46:10 +01001870 if (expected.Contains(ToBooleanICStub::SIMD_VALUE)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001871 // SIMD value -> true.
1872 __ CompareInstanceType(map, scratch, SIMD128_VALUE_TYPE);
1873 __ B(eq, true_label);
1874 }
1875
Ben Murdochda12d292016-06-02 14:46:10 +01001876 if (expected.Contains(ToBooleanICStub::HEAP_NUMBER)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001877 Label not_heap_number;
1878 __ JumpIfNotRoot(map, Heap::kHeapNumberMapRootIndex, &not_heap_number);
1879
1880 __ Ldr(double_scratch(),
1881 FieldMemOperand(value, HeapNumber::kValueOffset));
1882 __ Fcmp(double_scratch(), 0.0);
1883 // If we got a NaN (overflow bit is set), jump to the false branch.
1884 __ B(vs, false_label);
1885 __ B(eq, false_label);
1886 __ B(true_label);
1887 __ Bind(&not_heap_number);
1888 }
1889
1890 if (!expected.IsGeneric()) {
1891 // We've seen something for the first time -> deopt.
1892 // This can only happen if we are not generic already.
1893 Deoptimize(instr, Deoptimizer::kUnexpectedObject);
1894 }
1895 }
1896 }
1897}
1898
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001899void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
1900 int formal_parameter_count, int arity,
Ben Murdochda12d292016-06-02 14:46:10 +01001901 bool is_tail_call, LInstruction* instr) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001902 bool dont_adapt_arguments =
1903 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1904 bool can_invoke_directly =
1905 dont_adapt_arguments || formal_parameter_count == arity;
1906
1907 // The function interface relies on the following register assignments.
1908 Register function_reg = x1;
1909 Register arity_reg = x0;
1910
1911 LPointerMap* pointers = instr->pointer_map();
1912
1913 if (FLAG_debug_code) {
1914 Label is_not_smi;
1915 // Try to confirm that function_reg (x1) is a tagged pointer.
1916 __ JumpIfNotSmi(function_reg, &is_not_smi);
1917 __ Abort(kExpectedFunctionObject);
1918 __ Bind(&is_not_smi);
1919 }
1920
1921 if (can_invoke_directly) {
1922 // Change context.
1923 __ Ldr(cp, FieldMemOperand(function_reg, JSFunction::kContextOffset));
1924
1925 // Always initialize new target and number of actual arguments.
1926 __ LoadRoot(x3, Heap::kUndefinedValueRootIndex);
1927 __ Mov(arity_reg, arity);
1928
Ben Murdochda12d292016-06-02 14:46:10 +01001929 bool is_self_call = function.is_identical_to(info()->closure());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001930
Ben Murdochda12d292016-06-02 14:46:10 +01001931 // Invoke function.
1932 if (is_self_call) {
1933 Handle<Code> self(reinterpret_cast<Code**>(__ CodeObject().location()));
1934 if (is_tail_call) {
1935 __ Jump(self, RelocInfo::CODE_TARGET);
1936 } else {
1937 __ Call(self, RelocInfo::CODE_TARGET);
1938 }
1939 } else {
1940 __ Ldr(x10, FieldMemOperand(function_reg, JSFunction::kCodeEntryOffset));
1941 if (is_tail_call) {
1942 __ Jump(x10);
1943 } else {
1944 __ Call(x10);
1945 }
1946 }
1947
1948 if (!is_tail_call) {
1949 // Set up deoptimization.
1950 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT);
1951 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001952 } else {
1953 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
Ben Murdochda12d292016-06-02 14:46:10 +01001954 ParameterCount actual(arity);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001955 ParameterCount expected(formal_parameter_count);
Ben Murdochda12d292016-06-02 14:46:10 +01001956 InvokeFlag flag = is_tail_call ? JUMP_FUNCTION : CALL_FUNCTION;
1957 __ InvokeFunction(function_reg, expected, actual, flag, generator);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001958 }
1959}
1960
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001961void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
1962 DCHECK(instr->IsMarkedAsCall());
1963 DCHECK(ToRegister(instr->result()).Is(x0));
1964
1965 if (instr->hydrogen()->IsTailCall()) {
1966 if (NeedsEagerFrame()) __ LeaveFrame(StackFrame::INTERNAL);
1967
1968 if (instr->target()->IsConstantOperand()) {
1969 LConstantOperand* target = LConstantOperand::cast(instr->target());
1970 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
1971 // TODO(all): on ARM we use a call descriptor to specify a storage mode
1972 // but on ARM64 we only have one storage mode so it isn't necessary. Check
1973 // this understanding is correct.
1974 __ Jump(code, RelocInfo::CODE_TARGET);
1975 } else {
1976 DCHECK(instr->target()->IsRegister());
1977 Register target = ToRegister(instr->target());
1978 __ Add(target, target, Code::kHeaderSize - kHeapObjectTag);
1979 __ Br(target);
1980 }
1981 } else {
1982 LPointerMap* pointers = instr->pointer_map();
1983 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
1984
1985 if (instr->target()->IsConstantOperand()) {
1986 LConstantOperand* target = LConstantOperand::cast(instr->target());
1987 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
1988 generator.BeforeCall(__ CallSize(code, RelocInfo::CODE_TARGET));
1989 // TODO(all): on ARM we use a call descriptor to specify a storage mode
1990 // but on ARM64 we only have one storage mode so it isn't necessary. Check
1991 // this understanding is correct.
1992 __ Call(code, RelocInfo::CODE_TARGET, TypeFeedbackId::None());
1993 } else {
1994 DCHECK(instr->target()->IsRegister());
1995 Register target = ToRegister(instr->target());
1996 generator.BeforeCall(__ CallSize(target));
1997 __ Add(target, target, Code::kHeaderSize - kHeapObjectTag);
1998 __ Call(target);
1999 }
2000 generator.AfterCall();
2001 }
2002
2003 RecordPushedArgumentsDelta(instr->hydrogen()->argument_delta());
2004}
2005
2006
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002007void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
2008 CallRuntime(instr->function(), instr->arity(), instr);
2009 RecordPushedArgumentsDelta(instr->hydrogen()->argument_delta());
2010}
2011
2012
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002013void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
2014 GenerateOsrPrologue();
2015}
2016
2017
2018void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
2019 Register temp = ToRegister(instr->temp());
2020 {
2021 PushSafepointRegistersScope scope(this);
2022 __ Push(object);
2023 __ Mov(cp, 0);
2024 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
2025 RecordSafepointWithRegisters(
2026 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
2027 __ StoreToSafepointRegisterSlot(x0, temp);
2028 }
2029 DeoptimizeIfSmi(temp, instr, Deoptimizer::kInstanceMigrationFailed);
2030}
2031
2032
2033void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
2034 class DeferredCheckMaps: public LDeferredCode {
2035 public:
2036 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
2037 : LDeferredCode(codegen), instr_(instr), object_(object) {
2038 SetExit(check_maps());
2039 }
2040 virtual void Generate() {
2041 codegen()->DoDeferredInstanceMigration(instr_, object_);
2042 }
2043 Label* check_maps() { return &check_maps_; }
2044 virtual LInstruction* instr() { return instr_; }
2045 private:
2046 LCheckMaps* instr_;
2047 Label check_maps_;
2048 Register object_;
2049 };
2050
2051 if (instr->hydrogen()->IsStabilityCheck()) {
2052 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
2053 for (int i = 0; i < maps->size(); ++i) {
2054 AddStabilityDependency(maps->at(i).handle());
2055 }
2056 return;
2057 }
2058
2059 Register object = ToRegister(instr->value());
2060 Register map_reg = ToRegister(instr->temp());
2061
2062 __ Ldr(map_reg, FieldMemOperand(object, HeapObject::kMapOffset));
2063
2064 DeferredCheckMaps* deferred = NULL;
2065 if (instr->hydrogen()->HasMigrationTarget()) {
2066 deferred = new(zone()) DeferredCheckMaps(this, instr, object);
2067 __ Bind(deferred->check_maps());
2068 }
2069
2070 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
2071 Label success;
2072 for (int i = 0; i < maps->size() - 1; i++) {
2073 Handle<Map> map = maps->at(i).handle();
2074 __ CompareMap(map_reg, map);
2075 __ B(eq, &success);
2076 }
2077 Handle<Map> map = maps->at(maps->size() - 1).handle();
2078 __ CompareMap(map_reg, map);
2079
2080 // We didn't match a map.
2081 if (instr->hydrogen()->HasMigrationTarget()) {
2082 __ B(ne, deferred->entry());
2083 } else {
2084 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap);
2085 }
2086
2087 __ Bind(&success);
2088}
2089
2090
2091void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
2092 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2093 DeoptimizeIfSmi(ToRegister(instr->value()), instr, Deoptimizer::kSmi);
2094 }
2095}
2096
2097
2098void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
2099 Register value = ToRegister(instr->value());
2100 DCHECK(!instr->result() || ToRegister(instr->result()).Is(value));
2101 DeoptimizeIfNotSmi(value, instr, Deoptimizer::kNotASmi);
2102}
2103
2104
2105void LCodeGen::DoCheckArrayBufferNotNeutered(
2106 LCheckArrayBufferNotNeutered* instr) {
2107 UseScratchRegisterScope temps(masm());
2108 Register view = ToRegister(instr->view());
2109 Register scratch = temps.AcquireX();
2110
2111 __ Ldr(scratch, FieldMemOperand(view, JSArrayBufferView::kBufferOffset));
2112 __ Ldr(scratch, FieldMemOperand(scratch, JSArrayBuffer::kBitFieldOffset));
2113 __ Tst(scratch, Operand(1 << JSArrayBuffer::WasNeutered::kShift));
2114 DeoptimizeIf(ne, instr, Deoptimizer::kOutOfBounds);
2115}
2116
2117
2118void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
2119 Register input = ToRegister(instr->value());
2120 Register scratch = ToRegister(instr->temp());
2121
2122 __ Ldr(scratch, FieldMemOperand(input, HeapObject::kMapOffset));
2123 __ Ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
2124
2125 if (instr->hydrogen()->is_interval_check()) {
2126 InstanceType first, last;
2127 instr->hydrogen()->GetCheckInterval(&first, &last);
2128
2129 __ Cmp(scratch, first);
2130 if (first == last) {
2131 // If there is only one type in the interval check for equality.
2132 DeoptimizeIf(ne, instr, Deoptimizer::kWrongInstanceType);
2133 } else if (last == LAST_TYPE) {
2134 // We don't need to compare with the higher bound of the interval.
2135 DeoptimizeIf(lo, instr, Deoptimizer::kWrongInstanceType);
2136 } else {
2137 // If we are below the lower bound, set the C flag and clear the Z flag
2138 // to force a deopt.
2139 __ Ccmp(scratch, last, CFlag, hs);
2140 DeoptimizeIf(hi, instr, Deoptimizer::kWrongInstanceType);
2141 }
2142 } else {
2143 uint8_t mask;
2144 uint8_t tag;
2145 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
2146
2147 if (base::bits::IsPowerOfTwo32(mask)) {
2148 DCHECK((tag == 0) || (tag == mask));
2149 if (tag == 0) {
2150 DeoptimizeIfBitSet(scratch, MaskToBit(mask), instr,
2151 Deoptimizer::kWrongInstanceType);
2152 } else {
2153 DeoptimizeIfBitClear(scratch, MaskToBit(mask), instr,
2154 Deoptimizer::kWrongInstanceType);
2155 }
2156 } else {
2157 if (tag == 0) {
2158 __ Tst(scratch, mask);
2159 } else {
2160 __ And(scratch, scratch, mask);
2161 __ Cmp(scratch, tag);
2162 }
2163 DeoptimizeIf(ne, instr, Deoptimizer::kWrongInstanceType);
2164 }
2165 }
2166}
2167
2168
2169void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
2170 DoubleRegister input = ToDoubleRegister(instr->unclamped());
2171 Register result = ToRegister32(instr->result());
2172 __ ClampDoubleToUint8(result, input, double_scratch());
2173}
2174
2175
2176void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
2177 Register input = ToRegister32(instr->unclamped());
2178 Register result = ToRegister32(instr->result());
2179 __ ClampInt32ToUint8(result, input);
2180}
2181
2182
2183void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
2184 Register input = ToRegister(instr->unclamped());
2185 Register result = ToRegister32(instr->result());
2186 Label done;
2187
2188 // Both smi and heap number cases are handled.
2189 Label is_not_smi;
2190 __ JumpIfNotSmi(input, &is_not_smi);
2191 __ SmiUntag(result.X(), input);
2192 __ ClampInt32ToUint8(result);
2193 __ B(&done);
2194
2195 __ Bind(&is_not_smi);
2196
2197 // Check for heap number.
2198 Label is_heap_number;
2199 __ JumpIfHeapNumber(input, &is_heap_number);
2200
2201 // Check for undefined. Undefined is coverted to zero for clamping conversion.
2202 DeoptimizeIfNotRoot(input, Heap::kUndefinedValueRootIndex, instr,
2203 Deoptimizer::kNotAHeapNumberUndefined);
2204 __ Mov(result, 0);
2205 __ B(&done);
2206
2207 // Heap number case.
2208 __ Bind(&is_heap_number);
2209 DoubleRegister dbl_scratch = double_scratch();
2210 DoubleRegister dbl_scratch2 = ToDoubleRegister(instr->temp1());
2211 __ Ldr(dbl_scratch, FieldMemOperand(input, HeapNumber::kValueOffset));
2212 __ ClampDoubleToUint8(result, dbl_scratch, dbl_scratch2);
2213
2214 __ Bind(&done);
2215}
2216
2217
2218void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
2219 DoubleRegister value_reg = ToDoubleRegister(instr->value());
2220 Register result_reg = ToRegister(instr->result());
2221 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
2222 __ Fmov(result_reg, value_reg);
2223 __ Lsr(result_reg, result_reg, 32);
2224 } else {
2225 __ Fmov(result_reg.W(), value_reg.S());
2226 }
2227}
2228
2229
2230void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
2231 Register hi_reg = ToRegister(instr->hi());
2232 Register lo_reg = ToRegister(instr->lo());
2233 DoubleRegister result_reg = ToDoubleRegister(instr->result());
2234
2235 // Insert the least significant 32 bits of hi_reg into the most significant
2236 // 32 bits of lo_reg, and move to a floating point register.
2237 __ Bfi(lo_reg, hi_reg, 32, 32);
2238 __ Fmov(result_reg, lo_reg);
2239}
2240
2241
2242void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
2243 Handle<String> class_name = instr->hydrogen()->class_name();
2244 Label* true_label = instr->TrueLabel(chunk_);
2245 Label* false_label = instr->FalseLabel(chunk_);
2246 Register input = ToRegister(instr->value());
2247 Register scratch1 = ToRegister(instr->temp1());
2248 Register scratch2 = ToRegister(instr->temp2());
2249
2250 __ JumpIfSmi(input, false_label);
2251
2252 Register map = scratch2;
Ben Murdochda12d292016-06-02 14:46:10 +01002253 __ CompareObjectType(input, map, scratch1, FIRST_FUNCTION_TYPE);
2254 STATIC_ASSERT(LAST_FUNCTION_TYPE == LAST_TYPE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002255 if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
Ben Murdochda12d292016-06-02 14:46:10 +01002256 __ B(hs, true_label);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002257 } else {
Ben Murdochda12d292016-06-02 14:46:10 +01002258 __ B(hs, false_label);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002259 }
2260
2261 // Check if the constructor in the map is a function.
2262 {
2263 UseScratchRegisterScope temps(masm());
2264 Register instance_type = temps.AcquireX();
2265 __ GetMapConstructor(scratch1, map, scratch2, instance_type);
2266 __ Cmp(instance_type, JS_FUNCTION_TYPE);
2267 }
2268 // Objects with a non-function constructor have class 'Object'.
2269 if (String::Equals(class_name, isolate()->factory()->Object_string())) {
2270 __ B(ne, true_label);
2271 } else {
2272 __ B(ne, false_label);
2273 }
2274
2275 // The constructor function is in scratch1. Get its instance class name.
2276 __ Ldr(scratch1,
2277 FieldMemOperand(scratch1, JSFunction::kSharedFunctionInfoOffset));
2278 __ Ldr(scratch1,
2279 FieldMemOperand(scratch1,
2280 SharedFunctionInfo::kInstanceClassNameOffset));
2281
2282 // The class name we are testing against is internalized since it's a literal.
2283 // The name in the constructor is internalized because of the way the context
2284 // is booted. This routine isn't expected to work for random API-created
2285 // classes and it doesn't have to because you can't access it with natives
2286 // syntax. Since both sides are internalized it is sufficient to use an
2287 // identity comparison.
2288 EmitCompareAndBranch(instr, eq, scratch1, Operand(class_name));
2289}
2290
2291
2292void LCodeGen::DoCmpHoleAndBranchD(LCmpHoleAndBranchD* instr) {
2293 DCHECK(instr->hydrogen()->representation().IsDouble());
2294 FPRegister object = ToDoubleRegister(instr->object());
2295 Register temp = ToRegister(instr->temp());
2296
2297 // If we don't have a NaN, we don't have the hole, so branch now to avoid the
2298 // (relatively expensive) hole-NaN check.
2299 __ Fcmp(object, object);
2300 __ B(vc, instr->FalseLabel(chunk_));
2301
2302 // We have a NaN, but is it the hole?
2303 __ Fmov(temp, object);
2304 EmitCompareAndBranch(instr, eq, temp, kHoleNanInt64);
2305}
2306
2307
2308void LCodeGen::DoCmpHoleAndBranchT(LCmpHoleAndBranchT* instr) {
2309 DCHECK(instr->hydrogen()->representation().IsTagged());
2310 Register object = ToRegister(instr->object());
2311
2312 EmitBranchIfRoot(instr, object, Heap::kTheHoleValueRootIndex);
2313}
2314
2315
2316void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
2317 Register value = ToRegister(instr->value());
2318 Register map = ToRegister(instr->temp());
2319
2320 __ Ldr(map, FieldMemOperand(value, HeapObject::kMapOffset));
2321 EmitCompareAndBranch(instr, eq, map, Operand(instr->map()));
2322}
2323
2324
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002325void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2326 LOperand* left = instr->left();
2327 LOperand* right = instr->right();
2328 bool is_unsigned =
2329 instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2330 instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2331 Condition cond = TokenToCondition(instr->op(), is_unsigned);
2332
2333 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2334 // We can statically evaluate the comparison.
2335 double left_val = ToDouble(LConstantOperand::cast(left));
2336 double right_val = ToDouble(LConstantOperand::cast(right));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002337 int next_block = Token::EvalComparison(instr->op(), left_val, right_val)
2338 ? instr->TrueDestination(chunk_)
2339 : instr->FalseDestination(chunk_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002340 EmitGoto(next_block);
2341 } else {
2342 if (instr->is_double()) {
2343 __ Fcmp(ToDoubleRegister(left), ToDoubleRegister(right));
2344
2345 // If a NaN is involved, i.e. the result is unordered (V set),
2346 // jump to false block label.
2347 __ B(vs, instr->FalseLabel(chunk_));
2348 EmitBranch(instr, cond);
2349 } else {
2350 if (instr->hydrogen_value()->representation().IsInteger32()) {
2351 if (right->IsConstantOperand()) {
2352 EmitCompareAndBranch(instr, cond, ToRegister32(left),
2353 ToOperand32(right));
2354 } else {
2355 // Commute the operands and the condition.
2356 EmitCompareAndBranch(instr, CommuteCondition(cond),
2357 ToRegister32(right), ToOperand32(left));
2358 }
2359 } else {
2360 DCHECK(instr->hydrogen_value()->representation().IsSmi());
2361 if (right->IsConstantOperand()) {
2362 int32_t value = ToInteger32(LConstantOperand::cast(right));
2363 EmitCompareAndBranch(instr,
2364 cond,
2365 ToRegister(left),
2366 Operand(Smi::FromInt(value)));
2367 } else if (left->IsConstantOperand()) {
2368 // Commute the operands and the condition.
2369 int32_t value = ToInteger32(LConstantOperand::cast(left));
2370 EmitCompareAndBranch(instr,
2371 CommuteCondition(cond),
2372 ToRegister(right),
2373 Operand(Smi::FromInt(value)));
2374 } else {
2375 EmitCompareAndBranch(instr,
2376 cond,
2377 ToRegister(left),
2378 ToRegister(right));
2379 }
2380 }
2381 }
2382 }
2383}
2384
2385
2386void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
2387 Register left = ToRegister(instr->left());
2388 Register right = ToRegister(instr->right());
2389 EmitCompareAndBranch(instr, eq, left, right);
2390}
2391
2392
2393void LCodeGen::DoCmpT(LCmpT* instr) {
2394 DCHECK(ToRegister(instr->context()).is(cp));
2395 Token::Value op = instr->op();
2396 Condition cond = TokenToCondition(op, false);
2397
2398 DCHECK(ToRegister(instr->left()).Is(x1));
2399 DCHECK(ToRegister(instr->right()).Is(x0));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002400 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002401 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2402 // Signal that we don't inline smi code before this stub.
2403 InlineSmiCheckInfo::EmitNotInlined(masm());
2404
2405 // Return true or false depending on CompareIC result.
2406 // This instruction is marked as call. We can clobber any register.
2407 DCHECK(instr->IsMarkedAsCall());
2408 __ LoadTrueFalseRoots(x1, x2);
2409 __ Cmp(x0, 0);
2410 __ Csel(ToRegister(instr->result()), x1, x2, cond);
2411}
2412
2413
2414void LCodeGen::DoConstantD(LConstantD* instr) {
2415 DCHECK(instr->result()->IsDoubleRegister());
2416 DoubleRegister result = ToDoubleRegister(instr->result());
2417 if (instr->value() == 0) {
2418 if (copysign(1.0, instr->value()) == 1.0) {
2419 __ Fmov(result, fp_zero);
2420 } else {
2421 __ Fneg(result, fp_zero);
2422 }
2423 } else {
2424 __ Fmov(result, instr->value());
2425 }
2426}
2427
2428
2429void LCodeGen::DoConstantE(LConstantE* instr) {
2430 __ Mov(ToRegister(instr->result()), Operand(instr->value()));
2431}
2432
2433
2434void LCodeGen::DoConstantI(LConstantI* instr) {
2435 DCHECK(is_int32(instr->value()));
2436 // Cast the value here to ensure that the value isn't sign extended by the
2437 // implicit Operand constructor.
2438 __ Mov(ToRegister32(instr->result()), static_cast<uint32_t>(instr->value()));
2439}
2440
2441
2442void LCodeGen::DoConstantS(LConstantS* instr) {
2443 __ Mov(ToRegister(instr->result()), Operand(instr->value()));
2444}
2445
2446
2447void LCodeGen::DoConstantT(LConstantT* instr) {
2448 Handle<Object> object = instr->value(isolate());
2449 AllowDeferredHandleDereference smi_check;
2450 __ LoadObject(ToRegister(instr->result()), object);
2451}
2452
2453
2454void LCodeGen::DoContext(LContext* instr) {
2455 // If there is a non-return use, the context must be moved to a register.
2456 Register result = ToRegister(instr->result());
2457 if (info()->IsOptimizing()) {
2458 __ Ldr(result, MemOperand(fp, StandardFrameConstants::kContextOffset));
2459 } else {
2460 // If there is no frame, the context must be in cp.
2461 DCHECK(result.is(cp));
2462 }
2463}
2464
2465
2466void LCodeGen::DoCheckValue(LCheckValue* instr) {
2467 Register reg = ToRegister(instr->value());
2468 Handle<HeapObject> object = instr->hydrogen()->object().handle();
2469 AllowDeferredHandleDereference smi_check;
2470 if (isolate()->heap()->InNewSpace(*object)) {
2471 UseScratchRegisterScope temps(masm());
2472 Register temp = temps.AcquireX();
2473 Handle<Cell> cell = isolate()->factory()->NewCell(object);
2474 __ Mov(temp, Operand(cell));
2475 __ Ldr(temp, FieldMemOperand(temp, Cell::kValueOffset));
2476 __ Cmp(reg, temp);
2477 } else {
2478 __ Cmp(reg, Operand(object));
2479 }
2480 DeoptimizeIf(ne, instr, Deoptimizer::kValueMismatch);
2481}
2482
2483
2484void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
2485 last_lazy_deopt_pc_ = masm()->pc_offset();
2486 DCHECK(instr->HasEnvironment());
2487 LEnvironment* env = instr->environment();
2488 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
2489 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
2490}
2491
2492
2493void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
2494 Deoptimizer::BailoutType type = instr->hydrogen()->type();
2495 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
2496 // needed return address), even though the implementation of LAZY and EAGER is
2497 // now identical. When LAZY is eventually completely folded into EAGER, remove
2498 // the special case below.
2499 if (info()->IsStub() && (type == Deoptimizer::EAGER)) {
2500 type = Deoptimizer::LAZY;
2501 }
2502
2503 Deoptimize(instr, instr->hydrogen()->reason(), &type);
2504}
2505
2506
2507void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
2508 Register dividend = ToRegister32(instr->dividend());
2509 int32_t divisor = instr->divisor();
2510 Register result = ToRegister32(instr->result());
2511 DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
2512 DCHECK(!result.is(dividend));
2513
2514 // Check for (0 / -x) that will produce negative zero.
2515 HDiv* hdiv = instr->hydrogen();
2516 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
2517 DeoptimizeIfZero(dividend, instr, Deoptimizer::kDivisionByZero);
2518 }
2519 // Check for (kMinInt / -1).
2520 if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
2521 // Test dividend for kMinInt by subtracting one (cmp) and checking for
2522 // overflow.
2523 __ Cmp(dividend, 1);
2524 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
2525 }
2526 // Deoptimize if remainder will not be 0.
2527 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
2528 divisor != 1 && divisor != -1) {
2529 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
2530 __ Tst(dividend, mask);
2531 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecision);
2532 }
2533
2534 if (divisor == -1) { // Nice shortcut, not needed for correctness.
2535 __ Neg(result, dividend);
2536 return;
2537 }
2538 int32_t shift = WhichPowerOf2Abs(divisor);
2539 if (shift == 0) {
2540 __ Mov(result, dividend);
2541 } else if (shift == 1) {
2542 __ Add(result, dividend, Operand(dividend, LSR, 31));
2543 } else {
2544 __ Mov(result, Operand(dividend, ASR, 31));
2545 __ Add(result, dividend, Operand(result, LSR, 32 - shift));
2546 }
2547 if (shift > 0) __ Mov(result, Operand(result, ASR, shift));
2548 if (divisor < 0) __ Neg(result, result);
2549}
2550
2551
2552void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
2553 Register dividend = ToRegister32(instr->dividend());
2554 int32_t divisor = instr->divisor();
2555 Register result = ToRegister32(instr->result());
2556 DCHECK(!AreAliased(dividend, result));
2557
2558 if (divisor == 0) {
2559 Deoptimize(instr, Deoptimizer::kDivisionByZero);
2560 return;
2561 }
2562
2563 // Check for (0 / -x) that will produce negative zero.
2564 HDiv* hdiv = instr->hydrogen();
2565 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
2566 DeoptimizeIfZero(dividend, instr, Deoptimizer::kMinusZero);
2567 }
2568
2569 __ TruncatingDiv(result, dividend, Abs(divisor));
2570 if (divisor < 0) __ Neg(result, result);
2571
2572 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
2573 Register temp = ToRegister32(instr->temp());
2574 DCHECK(!AreAliased(dividend, result, temp));
2575 __ Sxtw(dividend.X(), dividend);
2576 __ Mov(temp, divisor);
2577 __ Smsubl(temp.X(), result, temp, dividend.X());
2578 DeoptimizeIfNotZero(temp, instr, Deoptimizer::kLostPrecision);
2579 }
2580}
2581
2582
2583// TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
2584void LCodeGen::DoDivI(LDivI* instr) {
2585 HBinaryOperation* hdiv = instr->hydrogen();
2586 Register dividend = ToRegister32(instr->dividend());
2587 Register divisor = ToRegister32(instr->divisor());
2588 Register result = ToRegister32(instr->result());
2589
2590 // Issue the division first, and then check for any deopt cases whilst the
2591 // result is computed.
2592 __ Sdiv(result, dividend, divisor);
2593
2594 if (hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
2595 DCHECK(!instr->temp());
2596 return;
2597 }
2598
2599 // Check for x / 0.
2600 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
2601 DeoptimizeIfZero(divisor, instr, Deoptimizer::kDivisionByZero);
2602 }
2603
2604 // Check for (0 / -x) as that will produce negative zero.
2605 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
2606 __ Cmp(divisor, 0);
2607
2608 // If the divisor < 0 (mi), compare the dividend, and deopt if it is
2609 // zero, ie. zero dividend with negative divisor deopts.
2610 // If the divisor >= 0 (pl, the opposite of mi) set the flags to
2611 // condition ne, so we don't deopt, ie. positive divisor doesn't deopt.
2612 __ Ccmp(dividend, 0, NoFlag, mi);
2613 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
2614 }
2615
2616 // Check for (kMinInt / -1).
2617 if (hdiv->CheckFlag(HValue::kCanOverflow)) {
2618 // Test dividend for kMinInt by subtracting one (cmp) and checking for
2619 // overflow.
2620 __ Cmp(dividend, 1);
2621 // If overflow is set, ie. dividend = kMinInt, compare the divisor with
2622 // -1. If overflow is clear, set the flags for condition ne, as the
2623 // dividend isn't -1, and thus we shouldn't deopt.
2624 __ Ccmp(divisor, -1, NoFlag, vs);
2625 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow);
2626 }
2627
2628 // Compute remainder and deopt if it's not zero.
2629 Register remainder = ToRegister32(instr->temp());
2630 __ Msub(remainder, result, divisor, dividend);
2631 DeoptimizeIfNotZero(remainder, instr, Deoptimizer::kLostPrecision);
2632}
2633
2634
2635void LCodeGen::DoDoubleToIntOrSmi(LDoubleToIntOrSmi* instr) {
2636 DoubleRegister input = ToDoubleRegister(instr->value());
2637 Register result = ToRegister32(instr->result());
2638
2639 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
2640 DeoptimizeIfMinusZero(input, instr, Deoptimizer::kMinusZero);
2641 }
2642
2643 __ TryRepresentDoubleAsInt32(result, input, double_scratch());
2644 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN);
2645
2646 if (instr->tag_result()) {
2647 __ SmiTag(result.X());
2648 }
2649}
2650
2651
2652void LCodeGen::DoDrop(LDrop* instr) {
2653 __ Drop(instr->count());
2654
2655 RecordPushedArgumentsDelta(instr->hydrogen_value()->argument_delta());
2656}
2657
2658
2659void LCodeGen::DoDummy(LDummy* instr) {
2660 // Nothing to see here, move on!
2661}
2662
2663
2664void LCodeGen::DoDummyUse(LDummyUse* instr) {
2665 // Nothing to see here, move on!
2666}
2667
2668
2669void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
2670 Register map = ToRegister(instr->map());
2671 Register result = ToRegister(instr->result());
2672 Label load_cache, done;
2673
2674 __ EnumLengthUntagged(result, map);
2675 __ Cbnz(result, &load_cache);
2676
2677 __ Mov(result, Operand(isolate()->factory()->empty_fixed_array()));
2678 __ B(&done);
2679
2680 __ Bind(&load_cache);
2681 __ LoadInstanceDescriptors(map, result);
2682 __ Ldr(result, FieldMemOperand(result, DescriptorArray::kEnumCacheOffset));
2683 __ Ldr(result, FieldMemOperand(result, FixedArray::SizeFor(instr->idx())));
2684 DeoptimizeIfZero(result, instr, Deoptimizer::kNoCache);
2685
2686 __ Bind(&done);
2687}
2688
2689
2690void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
2691 Register object = ToRegister(instr->object());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002692
2693 DCHECK(instr->IsMarkedAsCall());
2694 DCHECK(object.Is(x0));
2695
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002696 Label use_cache, call_runtime;
Ben Murdoch097c5b22016-05-18 11:27:45 +01002697 __ CheckEnumCache(object, x5, x1, x2, x3, x4, &call_runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002698
2699 __ Ldr(object, FieldMemOperand(object, HeapObject::kMapOffset));
2700 __ B(&use_cache);
2701
2702 // Get the set of properties to enumerate.
2703 __ Bind(&call_runtime);
2704 __ Push(object);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002705 CallRuntime(Runtime::kForInEnumerate, instr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002706 __ Bind(&use_cache);
2707}
2708
2709
2710void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
2711 Register input = ToRegister(instr->value());
2712 Register result = ToRegister(instr->result());
2713
2714 __ AssertString(input);
2715
2716 // Assert that we can use a W register load to get the hash.
2717 DCHECK((String::kHashShift + String::kArrayIndexValueBits) < kWRegSizeInBits);
2718 __ Ldr(result.W(), FieldMemOperand(input, String::kHashFieldOffset));
2719 __ IndexFromHash(result, result);
2720}
2721
2722
2723void LCodeGen::EmitGoto(int block) {
2724 // Do not emit jump if we are emitting a goto to the next block.
2725 if (!IsNextEmittedBlock(block)) {
2726 __ B(chunk_->GetAssemblyLabel(LookupDestination(block)));
2727 }
2728}
2729
2730
2731void LCodeGen::DoGoto(LGoto* instr) {
2732 EmitGoto(instr->block_id());
2733}
2734
2735
2736void LCodeGen::DoHasCachedArrayIndexAndBranch(
2737 LHasCachedArrayIndexAndBranch* instr) {
2738 Register input = ToRegister(instr->value());
2739 Register temp = ToRegister32(instr->temp());
2740
2741 // Assert that the cache status bits fit in a W register.
2742 DCHECK(is_uint32(String::kContainsCachedArrayIndexMask));
2743 __ Ldr(temp, FieldMemOperand(input, String::kHashFieldOffset));
2744 __ Tst(temp, String::kContainsCachedArrayIndexMask);
2745 EmitBranch(instr, eq);
2746}
2747
2748
2749// HHasInstanceTypeAndBranch instruction is built with an interval of type
2750// to test but is only used in very restricted ways. The only possible kinds
2751// of intervals are:
2752// - [ FIRST_TYPE, instr->to() ]
2753// - [ instr->form(), LAST_TYPE ]
2754// - instr->from() == instr->to()
2755//
2756// These kinds of intervals can be check with only one compare instruction
2757// providing the correct value and test condition are used.
2758//
2759// TestType() will return the value to use in the compare instruction and
2760// BranchCondition() will return the condition to use depending on the kind
2761// of interval actually specified in the instruction.
2762static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
2763 InstanceType from = instr->from();
2764 InstanceType to = instr->to();
2765 if (from == FIRST_TYPE) return to;
2766 DCHECK((from == to) || (to == LAST_TYPE));
2767 return from;
2768}
2769
2770
2771// See comment above TestType function for what this function does.
2772static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
2773 InstanceType from = instr->from();
2774 InstanceType to = instr->to();
2775 if (from == to) return eq;
2776 if (to == LAST_TYPE) return hs;
2777 if (from == FIRST_TYPE) return ls;
2778 UNREACHABLE();
2779 return eq;
2780}
2781
2782
2783void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
2784 Register input = ToRegister(instr->value());
2785 Register scratch = ToRegister(instr->temp());
2786
2787 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2788 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2789 }
2790 __ CompareObjectType(input, scratch, scratch, TestType(instr->hydrogen()));
2791 EmitBranch(instr, BranchCondition(instr->hydrogen()));
2792}
2793
2794
2795void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
2796 Register result = ToRegister(instr->result());
2797 Register base = ToRegister(instr->base_object());
2798 if (instr->offset()->IsConstantOperand()) {
2799 __ Add(result, base, ToOperand32(instr->offset()));
2800 } else {
2801 __ Add(result, base, Operand(ToRegister32(instr->offset()), SXTW));
2802 }
2803}
2804
2805
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002806void LCodeGen::DoHasInPrototypeChainAndBranch(
2807 LHasInPrototypeChainAndBranch* instr) {
2808 Register const object = ToRegister(instr->object());
2809 Register const object_map = ToRegister(instr->scratch1());
2810 Register const object_instance_type = ToRegister(instr->scratch2());
2811 Register const object_prototype = object_map;
2812 Register const prototype = ToRegister(instr->prototype());
2813
2814 // The {object} must be a spec object. It's sufficient to know that {object}
2815 // is not a smi, since all other non-spec objects have {null} prototypes and
2816 // will be ruled out below.
2817 if (instr->hydrogen()->ObjectNeedsSmiCheck()) {
2818 __ JumpIfSmi(object, instr->FalseLabel(chunk_));
2819 }
2820
2821 // Loop through the {object}s prototype chain looking for the {prototype}.
2822 __ Ldr(object_map, FieldMemOperand(object, HeapObject::kMapOffset));
2823 Label loop;
2824 __ Bind(&loop);
2825
2826 // Deoptimize if the object needs to be access checked.
2827 __ Ldrb(object_instance_type,
2828 FieldMemOperand(object_map, Map::kBitFieldOffset));
2829 __ Tst(object_instance_type, Operand(1 << Map::kIsAccessCheckNeeded));
2830 DeoptimizeIf(ne, instr, Deoptimizer::kAccessCheck);
2831 // Deoptimize for proxies.
2832 __ CompareInstanceType(object_map, object_instance_type, JS_PROXY_TYPE);
2833 DeoptimizeIf(eq, instr, Deoptimizer::kProxy);
2834
2835 __ Ldr(object_prototype, FieldMemOperand(object_map, Map::kPrototypeOffset));
2836 __ Cmp(object_prototype, prototype);
2837 __ B(eq, instr->TrueLabel(chunk_));
2838 __ CompareRoot(object_prototype, Heap::kNullValueRootIndex);
2839 __ B(eq, instr->FalseLabel(chunk_));
2840 __ Ldr(object_map, FieldMemOperand(object_prototype, HeapObject::kMapOffset));
2841 __ B(&loop);
2842}
2843
2844
2845void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
2846 DoGap(instr);
2847}
2848
2849
2850void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
2851 Register value = ToRegister32(instr->value());
2852 DoubleRegister result = ToDoubleRegister(instr->result());
2853 __ Scvtf(result, value);
2854}
2855
Ben Murdochda12d292016-06-02 14:46:10 +01002856void LCodeGen::PrepareForTailCall(const ParameterCount& actual,
2857 Register scratch1, Register scratch2,
2858 Register scratch3) {
2859#if DEBUG
2860 if (actual.is_reg()) {
2861 DCHECK(!AreAliased(actual.reg(), scratch1, scratch2, scratch3));
2862 } else {
2863 DCHECK(!AreAliased(scratch1, scratch2, scratch3));
2864 }
2865#endif
2866 if (FLAG_code_comments) {
2867 if (actual.is_reg()) {
2868 Comment(";;; PrepareForTailCall, actual: %s {", actual.reg().ToString());
2869 } else {
2870 Comment(";;; PrepareForTailCall, actual: %d {", actual.immediate());
2871 }
2872 }
2873
2874 // Check if next frame is an arguments adaptor frame.
2875 Register caller_args_count_reg = scratch1;
2876 Label no_arguments_adaptor, formal_parameter_count_loaded;
2877 __ Ldr(scratch2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2878 __ Ldr(scratch3,
2879 MemOperand(scratch2, StandardFrameConstants::kContextOffset));
2880 __ Cmp(scratch3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2881 __ B(ne, &no_arguments_adaptor);
2882
2883 // Drop current frame and load arguments count from arguments adaptor frame.
2884 __ mov(fp, scratch2);
2885 __ Ldr(caller_args_count_reg,
2886 MemOperand(fp, ArgumentsAdaptorFrameConstants::kLengthOffset));
2887 __ SmiUntag(caller_args_count_reg);
2888 __ B(&formal_parameter_count_loaded);
2889
2890 __ bind(&no_arguments_adaptor);
2891 // Load caller's formal parameter count
2892 __ Mov(caller_args_count_reg,
2893 Immediate(info()->literal()->parameter_count()));
2894
2895 __ bind(&formal_parameter_count_loaded);
2896 __ PrepareForTailCall(actual, caller_args_count_reg, scratch2, scratch3);
2897
2898 Comment(";;; }");
2899}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002900
2901void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
Ben Murdochda12d292016-06-02 14:46:10 +01002902 HInvokeFunction* hinstr = instr->hydrogen();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002903 DCHECK(ToRegister(instr->context()).is(cp));
2904 // The function is required to be in x1.
2905 DCHECK(ToRegister(instr->function()).is(x1));
2906 DCHECK(instr->HasPointerMap());
2907
Ben Murdochda12d292016-06-02 14:46:10 +01002908 bool is_tail_call = hinstr->tail_call_mode() == TailCallMode::kAllow;
2909
2910 if (is_tail_call) {
2911 DCHECK(!info()->saves_caller_doubles());
2912 ParameterCount actual(instr->arity());
2913 // It is safe to use x3, x4 and x5 as scratch registers here given that
2914 // 1) we are not going to return to caller function anyway,
2915 // 2) x3 (new.target) will be initialized below.
2916 PrepareForTailCall(actual, x3, x4, x5);
2917 }
2918
2919 Handle<JSFunction> known_function = hinstr->known_function();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002920 if (known_function.is_null()) {
2921 LPointerMap* pointers = instr->pointer_map();
2922 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
Ben Murdochda12d292016-06-02 14:46:10 +01002923 ParameterCount actual(instr->arity());
2924 InvokeFlag flag = is_tail_call ? JUMP_FUNCTION : CALL_FUNCTION;
2925 __ InvokeFunction(x1, no_reg, actual, flag, generator);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002926 } else {
Ben Murdochda12d292016-06-02 14:46:10 +01002927 CallKnownFunction(known_function, hinstr->formal_parameter_count(),
2928 instr->arity(), is_tail_call, instr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002929 }
2930 RecordPushedArgumentsDelta(instr->hydrogen()->argument_delta());
2931}
2932
2933
2934Condition LCodeGen::EmitIsString(Register input,
2935 Register temp1,
2936 Label* is_not_string,
2937 SmiCheck check_needed = INLINE_SMI_CHECK) {
2938 if (check_needed == INLINE_SMI_CHECK) {
2939 __ JumpIfSmi(input, is_not_string);
2940 }
2941 __ CompareObjectType(input, temp1, temp1, FIRST_NONSTRING_TYPE);
2942
2943 return lt;
2944}
2945
2946
2947void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
2948 Register val = ToRegister(instr->value());
2949 Register scratch = ToRegister(instr->temp());
2950
2951 SmiCheck check_needed =
2952 instr->hydrogen()->value()->type().IsHeapObject()
2953 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2954 Condition true_cond =
2955 EmitIsString(val, scratch, instr->FalseLabel(chunk_), check_needed);
2956
2957 EmitBranch(instr, true_cond);
2958}
2959
2960
2961void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
2962 Register value = ToRegister(instr->value());
2963 STATIC_ASSERT(kSmiTag == 0);
2964 EmitTestAndBranch(instr, eq, value, kSmiTagMask);
2965}
2966
2967
2968void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
2969 Register input = ToRegister(instr->value());
2970 Register temp = ToRegister(instr->temp());
2971
2972 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2973 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2974 }
2975 __ Ldr(temp, FieldMemOperand(input, HeapObject::kMapOffset));
2976 __ Ldrb(temp, FieldMemOperand(temp, Map::kBitFieldOffset));
2977
2978 EmitTestAndBranch(instr, ne, temp, 1 << Map::kIsUndetectable);
2979}
2980
2981
2982static const char* LabelType(LLabel* label) {
2983 if (label->is_loop_header()) return " (loop header)";
2984 if (label->is_osr_entry()) return " (OSR entry)";
2985 return "";
2986}
2987
2988
2989void LCodeGen::DoLabel(LLabel* label) {
2990 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
2991 current_instruction_,
2992 label->hydrogen_value()->id(),
2993 label->block_id(),
2994 LabelType(label));
2995
2996 // Inherit pushed_arguments_ from the predecessor's argument count.
2997 if (label->block()->HasPredecessor()) {
2998 pushed_arguments_ = label->block()->predecessors()->at(0)->argument_count();
2999#ifdef DEBUG
3000 for (auto p : *label->block()->predecessors()) {
3001 DCHECK_EQ(p->argument_count(), pushed_arguments_);
3002 }
3003#endif
3004 }
3005
3006 __ Bind(label->label());
3007 current_block_ = label->block_id();
3008 DoGap(label);
3009}
3010
3011
3012void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
3013 Register context = ToRegister(instr->context());
3014 Register result = ToRegister(instr->result());
3015 __ Ldr(result, ContextMemOperand(context, instr->slot_index()));
3016 if (instr->hydrogen()->RequiresHoleCheck()) {
3017 if (instr->hydrogen()->DeoptimizesOnHole()) {
3018 DeoptimizeIfRoot(result, Heap::kTheHoleValueRootIndex, instr,
3019 Deoptimizer::kHole);
3020 } else {
3021 Label not_the_hole;
3022 __ JumpIfNotRoot(result, Heap::kTheHoleValueRootIndex, &not_the_hole);
3023 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3024 __ Bind(&not_the_hole);
3025 }
3026 }
3027}
3028
3029
3030void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
3031 Register function = ToRegister(instr->function());
3032 Register result = ToRegister(instr->result());
3033 Register temp = ToRegister(instr->temp());
3034
3035 // Get the prototype or initial map from the function.
3036 __ Ldr(result, FieldMemOperand(function,
3037 JSFunction::kPrototypeOrInitialMapOffset));
3038
3039 // Check that the function has a prototype or an initial map.
3040 DeoptimizeIfRoot(result, Heap::kTheHoleValueRootIndex, instr,
3041 Deoptimizer::kHole);
3042
3043 // If the function does not have an initial map, we're done.
3044 Label done;
3045 __ CompareObjectType(result, temp, temp, MAP_TYPE);
3046 __ B(ne, &done);
3047
3048 // Get the prototype from the initial map.
3049 __ Ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
3050
3051 // All done.
3052 __ Bind(&done);
3053}
3054
3055
3056template <class T>
3057void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
3058 Register vector_register = ToRegister(instr->temp_vector());
3059 Register slot_register = LoadWithVectorDescriptor::SlotRegister();
3060 DCHECK(vector_register.is(LoadWithVectorDescriptor::VectorRegister()));
3061 DCHECK(slot_register.is(x0));
3062
3063 AllowDeferredHandleDereference vector_structure_check;
3064 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
3065 __ Mov(vector_register, vector);
3066 // No need to allocate this register.
3067 FeedbackVectorSlot slot = instr->hydrogen()->slot();
3068 int index = vector->GetIndex(slot);
3069 __ Mov(slot_register, Smi::FromInt(index));
3070}
3071
3072
3073template <class T>
3074void LCodeGen::EmitVectorStoreICRegisters(T* instr) {
3075 Register vector_register = ToRegister(instr->temp_vector());
3076 Register slot_register = ToRegister(instr->temp_slot());
3077
3078 AllowDeferredHandleDereference vector_structure_check;
3079 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
3080 __ Mov(vector_register, vector);
3081 FeedbackVectorSlot slot = instr->hydrogen()->slot();
3082 int index = vector->GetIndex(slot);
3083 __ Mov(slot_register, Smi::FromInt(index));
3084}
3085
3086
3087void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
3088 DCHECK(ToRegister(instr->context()).is(cp));
3089 DCHECK(ToRegister(instr->global_object())
3090 .is(LoadDescriptor::ReceiverRegister()));
3091 DCHECK(ToRegister(instr->result()).Is(x0));
3092 __ Mov(LoadDescriptor::NameRegister(), Operand(instr->name()));
3093 EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003094 Handle<Code> ic = CodeFactory::LoadICInOptimizedCode(
3095 isolate(), instr->typeof_mode(), PREMONOMORPHIC)
3096 .code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003097 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3098}
3099
3100
3101MemOperand LCodeGen::PrepareKeyedExternalArrayOperand(
3102 Register key,
3103 Register base,
3104 Register scratch,
3105 bool key_is_smi,
3106 bool key_is_constant,
3107 int constant_key,
3108 ElementsKind elements_kind,
3109 int base_offset) {
3110 int element_size_shift = ElementsKindToShiftSize(elements_kind);
3111
3112 if (key_is_constant) {
3113 int key_offset = constant_key << element_size_shift;
3114 return MemOperand(base, key_offset + base_offset);
3115 }
3116
3117 if (key_is_smi) {
3118 __ Add(scratch, base, Operand::UntagSmiAndScale(key, element_size_shift));
3119 return MemOperand(scratch, base_offset);
3120 }
3121
3122 if (base_offset == 0) {
3123 return MemOperand(base, key, SXTW, element_size_shift);
3124 }
3125
3126 DCHECK(!AreAliased(scratch, key));
3127 __ Add(scratch, base, base_offset);
3128 return MemOperand(scratch, key, SXTW, element_size_shift);
3129}
3130
3131
3132void LCodeGen::DoLoadKeyedExternal(LLoadKeyedExternal* instr) {
3133 Register ext_ptr = ToRegister(instr->elements());
3134 Register scratch;
3135 ElementsKind elements_kind = instr->elements_kind();
3136
3137 bool key_is_smi = instr->hydrogen()->key()->representation().IsSmi();
3138 bool key_is_constant = instr->key()->IsConstantOperand();
3139 Register key = no_reg;
3140 int constant_key = 0;
3141 if (key_is_constant) {
3142 DCHECK(instr->temp() == NULL);
3143 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3144 if (constant_key & 0xf0000000) {
3145 Abort(kArrayIndexConstantValueTooBig);
3146 }
3147 } else {
3148 scratch = ToRegister(instr->temp());
3149 key = ToRegister(instr->key());
3150 }
3151
3152 MemOperand mem_op =
3153 PrepareKeyedExternalArrayOperand(key, ext_ptr, scratch, key_is_smi,
3154 key_is_constant, constant_key,
3155 elements_kind,
3156 instr->base_offset());
3157
3158 if (elements_kind == FLOAT32_ELEMENTS) {
3159 DoubleRegister result = ToDoubleRegister(instr->result());
3160 __ Ldr(result.S(), mem_op);
3161 __ Fcvt(result, result.S());
3162 } else if (elements_kind == FLOAT64_ELEMENTS) {
3163 DoubleRegister result = ToDoubleRegister(instr->result());
3164 __ Ldr(result, mem_op);
3165 } else {
3166 Register result = ToRegister(instr->result());
3167
3168 switch (elements_kind) {
3169 case INT8_ELEMENTS:
3170 __ Ldrsb(result, mem_op);
3171 break;
3172 case UINT8_ELEMENTS:
3173 case UINT8_CLAMPED_ELEMENTS:
3174 __ Ldrb(result, mem_op);
3175 break;
3176 case INT16_ELEMENTS:
3177 __ Ldrsh(result, mem_op);
3178 break;
3179 case UINT16_ELEMENTS:
3180 __ Ldrh(result, mem_op);
3181 break;
3182 case INT32_ELEMENTS:
3183 __ Ldrsw(result, mem_op);
3184 break;
3185 case UINT32_ELEMENTS:
3186 __ Ldr(result.W(), mem_op);
3187 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
3188 // Deopt if value > 0x80000000.
3189 __ Tst(result, 0xFFFFFFFF80000000);
3190 DeoptimizeIf(ne, instr, Deoptimizer::kNegativeValue);
3191 }
3192 break;
3193 case FLOAT32_ELEMENTS:
3194 case FLOAT64_ELEMENTS:
3195 case FAST_HOLEY_DOUBLE_ELEMENTS:
3196 case FAST_HOLEY_ELEMENTS:
3197 case FAST_HOLEY_SMI_ELEMENTS:
3198 case FAST_DOUBLE_ELEMENTS:
3199 case FAST_ELEMENTS:
3200 case FAST_SMI_ELEMENTS:
3201 case DICTIONARY_ELEMENTS:
3202 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
3203 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
Ben Murdoch097c5b22016-05-18 11:27:45 +01003204 case FAST_STRING_WRAPPER_ELEMENTS:
3205 case SLOW_STRING_WRAPPER_ELEMENTS:
3206 case NO_ELEMENTS:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003207 UNREACHABLE();
3208 break;
3209 }
3210 }
3211}
3212
3213
3214MemOperand LCodeGen::PrepareKeyedArrayOperand(Register base,
3215 Register elements,
3216 Register key,
3217 bool key_is_tagged,
3218 ElementsKind elements_kind,
3219 Representation representation,
3220 int base_offset) {
3221 STATIC_ASSERT(static_cast<unsigned>(kSmiValueSize) == kWRegSizeInBits);
3222 STATIC_ASSERT(kSmiTag == 0);
3223 int element_size_shift = ElementsKindToShiftSize(elements_kind);
3224
3225 // Even though the HLoad/StoreKeyed instructions force the input
3226 // representation for the key to be an integer, the input gets replaced during
3227 // bounds check elimination with the index argument to the bounds check, which
3228 // can be tagged, so that case must be handled here, too.
3229 if (key_is_tagged) {
3230 __ Add(base, elements, Operand::UntagSmiAndScale(key, element_size_shift));
3231 if (representation.IsInteger32()) {
3232 DCHECK(elements_kind == FAST_SMI_ELEMENTS);
3233 // Read or write only the smi payload in the case of fast smi arrays.
3234 return UntagSmiMemOperand(base, base_offset);
3235 } else {
3236 return MemOperand(base, base_offset);
3237 }
3238 } else {
3239 // Sign extend key because it could be a 32-bit negative value or contain
3240 // garbage in the top 32-bits. The address computation happens in 64-bit.
3241 DCHECK((element_size_shift >= 0) && (element_size_shift <= 4));
3242 if (representation.IsInteger32()) {
3243 DCHECK(elements_kind == FAST_SMI_ELEMENTS);
3244 // Read or write only the smi payload in the case of fast smi arrays.
3245 __ Add(base, elements, Operand(key, SXTW, element_size_shift));
3246 return UntagSmiMemOperand(base, base_offset);
3247 } else {
3248 __ Add(base, elements, base_offset);
3249 return MemOperand(base, key, SXTW, element_size_shift);
3250 }
3251 }
3252}
3253
3254
3255void LCodeGen::DoLoadKeyedFixedDouble(LLoadKeyedFixedDouble* instr) {
3256 Register elements = ToRegister(instr->elements());
3257 DoubleRegister result = ToDoubleRegister(instr->result());
3258 MemOperand mem_op;
3259
3260 if (instr->key()->IsConstantOperand()) {
3261 DCHECK(instr->hydrogen()->RequiresHoleCheck() ||
3262 (instr->temp() == NULL));
3263
3264 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
3265 if (constant_key & 0xf0000000) {
3266 Abort(kArrayIndexConstantValueTooBig);
3267 }
3268 int offset = instr->base_offset() + constant_key * kDoubleSize;
3269 mem_op = MemOperand(elements, offset);
3270 } else {
3271 Register load_base = ToRegister(instr->temp());
3272 Register key = ToRegister(instr->key());
3273 bool key_is_tagged = instr->hydrogen()->key()->representation().IsSmi();
3274 mem_op = PrepareKeyedArrayOperand(load_base, elements, key, key_is_tagged,
3275 instr->hydrogen()->elements_kind(),
3276 instr->hydrogen()->representation(),
3277 instr->base_offset());
3278 }
3279
3280 __ Ldr(result, mem_op);
3281
3282 if (instr->hydrogen()->RequiresHoleCheck()) {
3283 Register scratch = ToRegister(instr->temp());
3284 __ Fmov(scratch, result);
3285 __ Eor(scratch, scratch, kHoleNanInt64);
3286 DeoptimizeIfZero(scratch, instr, Deoptimizer::kHole);
3287 }
3288}
3289
3290
3291void LCodeGen::DoLoadKeyedFixed(LLoadKeyedFixed* instr) {
3292 Register elements = ToRegister(instr->elements());
3293 Register result = ToRegister(instr->result());
3294 MemOperand mem_op;
3295
3296 Representation representation = instr->hydrogen()->representation();
3297 if (instr->key()->IsConstantOperand()) {
3298 DCHECK(instr->temp() == NULL);
3299 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
3300 int offset = instr->base_offset() +
3301 ToInteger32(const_operand) * kPointerSize;
3302 if (representation.IsInteger32()) {
3303 DCHECK(instr->hydrogen()->elements_kind() == FAST_SMI_ELEMENTS);
3304 STATIC_ASSERT(static_cast<unsigned>(kSmiValueSize) == kWRegSizeInBits);
3305 STATIC_ASSERT(kSmiTag == 0);
3306 mem_op = UntagSmiMemOperand(elements, offset);
3307 } else {
3308 mem_op = MemOperand(elements, offset);
3309 }
3310 } else {
3311 Register load_base = ToRegister(instr->temp());
3312 Register key = ToRegister(instr->key());
3313 bool key_is_tagged = instr->hydrogen()->key()->representation().IsSmi();
3314
3315 mem_op = PrepareKeyedArrayOperand(load_base, elements, key, key_is_tagged,
3316 instr->hydrogen()->elements_kind(),
3317 representation, instr->base_offset());
3318 }
3319
3320 __ Load(result, mem_op, representation);
3321
3322 if (instr->hydrogen()->RequiresHoleCheck()) {
3323 if (IsFastSmiElementsKind(instr->hydrogen()->elements_kind())) {
3324 DeoptimizeIfNotSmi(result, instr, Deoptimizer::kNotASmi);
3325 } else {
3326 DeoptimizeIfRoot(result, Heap::kTheHoleValueRootIndex, instr,
3327 Deoptimizer::kHole);
3328 }
3329 } else if (instr->hydrogen()->hole_mode() == CONVERT_HOLE_TO_UNDEFINED) {
3330 DCHECK(instr->hydrogen()->elements_kind() == FAST_HOLEY_ELEMENTS);
3331 Label done;
3332 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
3333 __ B(ne, &done);
3334 if (info()->IsStub()) {
3335 // A stub can safely convert the hole to undefined only if the array
3336 // protector cell contains (Smi) Isolate::kArrayProtectorValid. Otherwise
3337 // it needs to bail out.
3338 __ LoadRoot(result, Heap::kArrayProtectorRootIndex);
3339 __ Ldr(result, FieldMemOperand(result, Cell::kValueOffset));
3340 __ Cmp(result, Operand(Smi::FromInt(Isolate::kArrayProtectorValid)));
3341 DeoptimizeIf(ne, instr, Deoptimizer::kHole);
3342 }
3343 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3344 __ Bind(&done);
3345 }
3346}
3347
3348
3349void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3350 DCHECK(ToRegister(instr->context()).is(cp));
3351 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3352 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
3353
3354 if (instr->hydrogen()->HasVectorAndSlot()) {
3355 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
3356 }
3357
3358 Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(
Ben Murdoch097c5b22016-05-18 11:27:45 +01003359 isolate(), instr->hydrogen()->initialization_state())
3360 .code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003361 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3362
3363 DCHECK(ToRegister(instr->result()).Is(x0));
3364}
3365
3366
3367void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
3368 HObjectAccess access = instr->hydrogen()->access();
3369 int offset = access.offset();
3370 Register object = ToRegister(instr->object());
3371
3372 if (access.IsExternalMemory()) {
3373 Register result = ToRegister(instr->result());
3374 __ Load(result, MemOperand(object, offset), access.representation());
3375 return;
3376 }
3377
3378 if (instr->hydrogen()->representation().IsDouble()) {
3379 DCHECK(access.IsInobject());
3380 FPRegister result = ToDoubleRegister(instr->result());
3381 __ Ldr(result, FieldMemOperand(object, offset));
3382 return;
3383 }
3384
3385 Register result = ToRegister(instr->result());
3386 Register source;
3387 if (access.IsInobject()) {
3388 source = object;
3389 } else {
3390 // Load the properties array, using result as a scratch register.
3391 __ Ldr(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
3392 source = result;
3393 }
3394
3395 if (access.representation().IsSmi() &&
3396 instr->hydrogen()->representation().IsInteger32()) {
3397 // Read int value directly from upper half of the smi.
3398 STATIC_ASSERT(static_cast<unsigned>(kSmiValueSize) == kWRegSizeInBits);
3399 STATIC_ASSERT(kSmiTag == 0);
3400 __ Load(result, UntagSmiFieldMemOperand(source, offset),
3401 Representation::Integer32());
3402 } else {
3403 __ Load(result, FieldMemOperand(source, offset), access.representation());
3404 }
3405}
3406
3407
3408void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
3409 DCHECK(ToRegister(instr->context()).is(cp));
3410 // LoadIC expects name and receiver in registers.
3411 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3412 __ Mov(LoadDescriptor::NameRegister(), Operand(instr->name()));
3413 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003414 Handle<Code> ic = CodeFactory::LoadICInOptimizedCode(
3415 isolate(), NOT_INSIDE_TYPEOF,
3416 instr->hydrogen()->initialization_state())
3417 .code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003418 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3419
3420 DCHECK(ToRegister(instr->result()).is(x0));
3421}
3422
3423
3424void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
3425 Register result = ToRegister(instr->result());
3426 __ LoadRoot(result, instr->index());
3427}
3428
3429
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003430void LCodeGen::DoMathAbs(LMathAbs* instr) {
3431 Representation r = instr->hydrogen()->value()->representation();
3432 if (r.IsDouble()) {
3433 DoubleRegister input = ToDoubleRegister(instr->value());
3434 DoubleRegister result = ToDoubleRegister(instr->result());
3435 __ Fabs(result, input);
3436 } else if (r.IsSmi() || r.IsInteger32()) {
3437 Register input = r.IsSmi() ? ToRegister(instr->value())
3438 : ToRegister32(instr->value());
3439 Register result = r.IsSmi() ? ToRegister(instr->result())
3440 : ToRegister32(instr->result());
3441 __ Abs(result, input);
3442 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
3443 }
3444}
3445
3446
3447void LCodeGen::DoDeferredMathAbsTagged(LMathAbsTagged* instr,
3448 Label* exit,
3449 Label* allocation_entry) {
3450 // Handle the tricky cases of MathAbsTagged:
3451 // - HeapNumber inputs.
3452 // - Negative inputs produce a positive result, so a new HeapNumber is
3453 // allocated to hold it.
3454 // - Positive inputs are returned as-is, since there is no need to allocate
3455 // a new HeapNumber for the result.
3456 // - The (smi) input -0x80000000, produces +0x80000000, which does not fit
3457 // a smi. In this case, the inline code sets the result and jumps directly
3458 // to the allocation_entry label.
3459 DCHECK(instr->context() != NULL);
3460 DCHECK(ToRegister(instr->context()).is(cp));
3461 Register input = ToRegister(instr->value());
3462 Register temp1 = ToRegister(instr->temp1());
3463 Register temp2 = ToRegister(instr->temp2());
3464 Register result_bits = ToRegister(instr->temp3());
3465 Register result = ToRegister(instr->result());
3466
3467 Label runtime_allocation;
3468
3469 // Deoptimize if the input is not a HeapNumber.
3470 DeoptimizeIfNotHeapNumber(input, instr);
3471
3472 // If the argument is positive, we can return it as-is, without any need to
3473 // allocate a new HeapNumber for the result. We have to do this in integer
3474 // registers (rather than with fabs) because we need to be able to distinguish
3475 // the two zeroes.
3476 __ Ldr(result_bits, FieldMemOperand(input, HeapNumber::kValueOffset));
3477 __ Mov(result, input);
3478 __ Tbz(result_bits, kXSignBit, exit);
3479
3480 // Calculate abs(input) by clearing the sign bit.
3481 __ Bic(result_bits, result_bits, kXSignMask);
3482
3483 // Allocate a new HeapNumber to hold the result.
3484 // result_bits The bit representation of the (double) result.
3485 __ Bind(allocation_entry);
3486 __ AllocateHeapNumber(result, &runtime_allocation, temp1, temp2);
3487 // The inline (non-deferred) code will store result_bits into result.
3488 __ B(exit);
3489
3490 __ Bind(&runtime_allocation);
3491 if (FLAG_debug_code) {
3492 // Because result is in the pointer map, we need to make sure it has a valid
3493 // tagged value before we call the runtime. We speculatively set it to the
3494 // input (for abs(+x)) or to a smi (for abs(-SMI_MIN)), so it should already
3495 // be valid.
3496 Label result_ok;
3497 Register input = ToRegister(instr->value());
3498 __ JumpIfSmi(result, &result_ok);
3499 __ Cmp(input, result);
3500 __ Assert(eq, kUnexpectedValue);
3501 __ Bind(&result_ok);
3502 }
3503
3504 { PushSafepointRegistersScope scope(this);
3505 CallRuntimeFromDeferred(Runtime::kAllocateHeapNumber, 0, instr,
3506 instr->context());
3507 __ StoreToSafepointRegisterSlot(x0, result);
3508 }
3509 // The inline (non-deferred) code will store result_bits into result.
3510}
3511
3512
3513void LCodeGen::DoMathAbsTagged(LMathAbsTagged* instr) {
3514 // Class for deferred case.
3515 class DeferredMathAbsTagged: public LDeferredCode {
3516 public:
3517 DeferredMathAbsTagged(LCodeGen* codegen, LMathAbsTagged* instr)
3518 : LDeferredCode(codegen), instr_(instr) { }
3519 virtual void Generate() {
3520 codegen()->DoDeferredMathAbsTagged(instr_, exit(),
3521 allocation_entry());
3522 }
3523 virtual LInstruction* instr() { return instr_; }
3524 Label* allocation_entry() { return &allocation; }
3525 private:
3526 LMathAbsTagged* instr_;
3527 Label allocation;
3528 };
3529
3530 // TODO(jbramley): The early-exit mechanism would skip the new frame handling
3531 // in GenerateDeferredCode. Tidy this up.
3532 DCHECK(!NeedsDeferredFrame());
3533
3534 DeferredMathAbsTagged* deferred =
3535 new(zone()) DeferredMathAbsTagged(this, instr);
3536
3537 DCHECK(instr->hydrogen()->value()->representation().IsTagged() ||
3538 instr->hydrogen()->value()->representation().IsSmi());
3539 Register input = ToRegister(instr->value());
3540 Register result_bits = ToRegister(instr->temp3());
3541 Register result = ToRegister(instr->result());
3542 Label done;
3543
3544 // Handle smis inline.
3545 // We can treat smis as 64-bit integers, since the (low-order) tag bits will
3546 // never get set by the negation. This is therefore the same as the Integer32
3547 // case in DoMathAbs, except that it operates on 64-bit values.
3548 STATIC_ASSERT((kSmiValueSize == 32) && (kSmiShift == 32) && (kSmiTag == 0));
3549
3550 __ JumpIfNotSmi(input, deferred->entry());
3551
3552 __ Abs(result, input, NULL, &done);
3553
3554 // The result is the magnitude (abs) of the smallest value a smi can
3555 // represent, encoded as a double.
3556 __ Mov(result_bits, double_to_rawbits(0x80000000));
3557 __ B(deferred->allocation_entry());
3558
3559 __ Bind(deferred->exit());
3560 __ Str(result_bits, FieldMemOperand(result, HeapNumber::kValueOffset));
3561
3562 __ Bind(&done);
3563}
3564
3565
3566void LCodeGen::DoMathExp(LMathExp* instr) {
3567 DoubleRegister input = ToDoubleRegister(instr->value());
3568 DoubleRegister result = ToDoubleRegister(instr->result());
3569 DoubleRegister double_temp1 = ToDoubleRegister(instr->double_temp1());
3570 DoubleRegister double_temp2 = double_scratch();
3571 Register temp1 = ToRegister(instr->temp1());
3572 Register temp2 = ToRegister(instr->temp2());
3573 Register temp3 = ToRegister(instr->temp3());
3574
3575 MathExpGenerator::EmitMathExp(masm(), input, result,
3576 double_temp1, double_temp2,
3577 temp1, temp2, temp3);
3578}
3579
3580
3581void LCodeGen::DoMathFloorD(LMathFloorD* instr) {
3582 DoubleRegister input = ToDoubleRegister(instr->value());
3583 DoubleRegister result = ToDoubleRegister(instr->result());
3584
3585 __ Frintm(result, input);
3586}
3587
3588
3589void LCodeGen::DoMathFloorI(LMathFloorI* instr) {
3590 DoubleRegister input = ToDoubleRegister(instr->value());
3591 Register result = ToRegister(instr->result());
3592
3593 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3594 DeoptimizeIfMinusZero(input, instr, Deoptimizer::kMinusZero);
3595 }
3596
3597 __ Fcvtms(result, input);
3598
3599 // Check that the result fits into a 32-bit integer.
3600 // - The result did not overflow.
3601 __ Cmp(result, Operand(result, SXTW));
3602 // - The input was not NaN.
3603 __ Fccmp(input, input, NoFlag, eq);
3604 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN);
3605}
3606
3607
3608void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
3609 Register dividend = ToRegister32(instr->dividend());
3610 Register result = ToRegister32(instr->result());
3611 int32_t divisor = instr->divisor();
3612
3613 // If the divisor is 1, return the dividend.
3614 if (divisor == 1) {
3615 __ Mov(result, dividend, kDiscardForSameWReg);
3616 return;
3617 }
3618
3619 // If the divisor is positive, things are easy: There can be no deopts and we
3620 // can simply do an arithmetic right shift.
3621 int32_t shift = WhichPowerOf2Abs(divisor);
3622 if (divisor > 1) {
3623 __ Mov(result, Operand(dividend, ASR, shift));
3624 return;
3625 }
3626
3627 // If the divisor is negative, we have to negate and handle edge cases.
3628 __ Negs(result, dividend);
3629 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3630 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
3631 }
3632
3633 // Dividing by -1 is basically negation, unless we overflow.
3634 if (divisor == -1) {
3635 if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
3636 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
3637 }
3638 return;
3639 }
3640
3641 // If the negation could not overflow, simply shifting is OK.
3642 if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
3643 __ Mov(result, Operand(dividend, ASR, shift));
3644 return;
3645 }
3646
3647 __ Asr(result, result, shift);
3648 __ Csel(result, result, kMinInt / divisor, vc);
3649}
3650
3651
3652void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
3653 Register dividend = ToRegister32(instr->dividend());
3654 int32_t divisor = instr->divisor();
3655 Register result = ToRegister32(instr->result());
3656 DCHECK(!AreAliased(dividend, result));
3657
3658 if (divisor == 0) {
3659 Deoptimize(instr, Deoptimizer::kDivisionByZero);
3660 return;
3661 }
3662
3663 // Check for (0 / -x) that will produce negative zero.
3664 HMathFloorOfDiv* hdiv = instr->hydrogen();
3665 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
3666 DeoptimizeIfZero(dividend, instr, Deoptimizer::kMinusZero);
3667 }
3668
3669 // Easy case: We need no dynamic check for the dividend and the flooring
3670 // division is the same as the truncating division.
3671 if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
3672 (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
3673 __ TruncatingDiv(result, dividend, Abs(divisor));
3674 if (divisor < 0) __ Neg(result, result);
3675 return;
3676 }
3677
3678 // In the general case we may need to adjust before and after the truncating
3679 // division to get a flooring division.
3680 Register temp = ToRegister32(instr->temp());
3681 DCHECK(!AreAliased(temp, dividend, result));
3682 Label needs_adjustment, done;
3683 __ Cmp(dividend, 0);
3684 __ B(divisor > 0 ? lt : gt, &needs_adjustment);
3685 __ TruncatingDiv(result, dividend, Abs(divisor));
3686 if (divisor < 0) __ Neg(result, result);
3687 __ B(&done);
3688 __ Bind(&needs_adjustment);
3689 __ Add(temp, dividend, Operand(divisor > 0 ? 1 : -1));
3690 __ TruncatingDiv(result, temp, Abs(divisor));
3691 if (divisor < 0) __ Neg(result, result);
3692 __ Sub(result, result, Operand(1));
3693 __ Bind(&done);
3694}
3695
3696
3697// TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
3698void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
3699 Register dividend = ToRegister32(instr->dividend());
3700 Register divisor = ToRegister32(instr->divisor());
3701 Register remainder = ToRegister32(instr->temp());
3702 Register result = ToRegister32(instr->result());
3703
3704 // This can't cause an exception on ARM, so we can speculatively
3705 // execute it already now.
3706 __ Sdiv(result, dividend, divisor);
3707
3708 // Check for x / 0.
3709 DeoptimizeIfZero(divisor, instr, Deoptimizer::kDivisionByZero);
3710
3711 // Check for (kMinInt / -1).
3712 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
3713 // The V flag will be set iff dividend == kMinInt.
3714 __ Cmp(dividend, 1);
3715 __ Ccmp(divisor, -1, NoFlag, vs);
3716 DeoptimizeIf(eq, instr, Deoptimizer::kOverflow);
3717 }
3718
3719 // Check for (0 / -x) that will produce negative zero.
3720 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3721 __ Cmp(divisor, 0);
3722 __ Ccmp(dividend, 0, ZFlag, mi);
3723 // "divisor" can't be null because the code would have already been
3724 // deoptimized. The Z flag is set only if (divisor < 0) and (dividend == 0).
3725 // In this case we need to deoptimize to produce a -0.
3726 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
3727 }
3728
3729 Label done;
3730 // If both operands have the same sign then we are done.
3731 __ Eor(remainder, dividend, divisor);
3732 __ Tbz(remainder, kWSignBit, &done);
3733
3734 // Check if the result needs to be corrected.
3735 __ Msub(remainder, result, divisor, dividend);
3736 __ Cbz(remainder, &done);
3737 __ Sub(result, result, 1);
3738
3739 __ Bind(&done);
3740}
3741
3742
3743void LCodeGen::DoMathLog(LMathLog* instr) {
3744 DCHECK(instr->IsMarkedAsCall());
3745 DCHECK(ToDoubleRegister(instr->value()).is(d0));
3746 __ CallCFunction(ExternalReference::math_log_double_function(isolate()),
3747 0, 1);
3748 DCHECK(ToDoubleRegister(instr->result()).Is(d0));
3749}
3750
3751
3752void LCodeGen::DoMathClz32(LMathClz32* instr) {
3753 Register input = ToRegister32(instr->value());
3754 Register result = ToRegister32(instr->result());
3755 __ Clz(result, input);
3756}
3757
3758
3759void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3760 DoubleRegister input = ToDoubleRegister(instr->value());
3761 DoubleRegister result = ToDoubleRegister(instr->result());
3762 Label done;
3763
3764 // Math.pow(x, 0.5) differs from fsqrt(x) in the following cases:
3765 // Math.pow(-Infinity, 0.5) == +Infinity
3766 // Math.pow(-0.0, 0.5) == +0.0
3767
3768 // Catch -infinity inputs first.
3769 // TODO(jbramley): A constant infinity register would be helpful here.
3770 __ Fmov(double_scratch(), kFP64NegativeInfinity);
3771 __ Fcmp(double_scratch(), input);
3772 __ Fabs(result, input);
3773 __ B(&done, eq);
3774
3775 // Add +0.0 to convert -0.0 to +0.0.
3776 __ Fadd(double_scratch(), input, fp_zero);
3777 __ Fsqrt(result, double_scratch());
3778
3779 __ Bind(&done);
3780}
3781
3782
3783void LCodeGen::DoPower(LPower* instr) {
3784 Representation exponent_type = instr->hydrogen()->right()->representation();
3785 // Having marked this as a call, we can use any registers.
3786 // Just make sure that the input/output registers are the expected ones.
3787 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3788 Register integer_exponent = MathPowIntegerDescriptor::exponent();
3789 DCHECK(!instr->right()->IsDoubleRegister() ||
3790 ToDoubleRegister(instr->right()).is(d1));
3791 DCHECK(exponent_type.IsInteger32() || !instr->right()->IsRegister() ||
3792 ToRegister(instr->right()).is(tagged_exponent));
3793 DCHECK(!exponent_type.IsInteger32() ||
3794 ToRegister(instr->right()).is(integer_exponent));
3795 DCHECK(ToDoubleRegister(instr->left()).is(d0));
3796 DCHECK(ToDoubleRegister(instr->result()).is(d0));
3797
3798 if (exponent_type.IsSmi()) {
3799 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3800 __ CallStub(&stub);
3801 } else if (exponent_type.IsTagged()) {
3802 Label no_deopt;
3803 __ JumpIfSmi(tagged_exponent, &no_deopt);
3804 DeoptimizeIfNotHeapNumber(tagged_exponent, instr);
3805 __ Bind(&no_deopt);
3806 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3807 __ CallStub(&stub);
3808 } else if (exponent_type.IsInteger32()) {
3809 // Ensure integer exponent has no garbage in top 32-bits, as MathPowStub
3810 // supports large integer exponents.
3811 __ Sxtw(integer_exponent, integer_exponent);
3812 MathPowStub stub(isolate(), MathPowStub::INTEGER);
3813 __ CallStub(&stub);
3814 } else {
3815 DCHECK(exponent_type.IsDouble());
3816 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
3817 __ CallStub(&stub);
3818 }
3819}
3820
3821
3822void LCodeGen::DoMathRoundD(LMathRoundD* instr) {
3823 DoubleRegister input = ToDoubleRegister(instr->value());
3824 DoubleRegister result = ToDoubleRegister(instr->result());
3825 DoubleRegister scratch_d = double_scratch();
3826
3827 DCHECK(!AreAliased(input, result, scratch_d));
3828
3829 Label done;
3830
3831 __ Frinta(result, input);
3832 __ Fcmp(input, 0.0);
3833 __ Fccmp(result, input, ZFlag, lt);
3834 // The result is correct if the input was in [-0, +infinity], or was a
3835 // negative integral value.
3836 __ B(eq, &done);
3837
3838 // Here the input is negative, non integral, with an exponent lower than 52.
3839 // We do not have to worry about the 0.49999999999999994 (0x3fdfffffffffffff)
3840 // case. So we can safely add 0.5.
3841 __ Fmov(scratch_d, 0.5);
3842 __ Fadd(result, input, scratch_d);
3843 __ Frintm(result, result);
3844 // The range [-0.5, -0.0[ yielded +0.0. Force the sign to negative.
3845 __ Fabs(result, result);
3846 __ Fneg(result, result);
3847
3848 __ Bind(&done);
3849}
3850
3851
3852void LCodeGen::DoMathRoundI(LMathRoundI* instr) {
3853 DoubleRegister input = ToDoubleRegister(instr->value());
3854 DoubleRegister temp = ToDoubleRegister(instr->temp1());
3855 DoubleRegister dot_five = double_scratch();
3856 Register result = ToRegister(instr->result());
3857 Label done;
3858
3859 // Math.round() rounds to the nearest integer, with ties going towards
3860 // +infinity. This does not match any IEEE-754 rounding mode.
3861 // - Infinities and NaNs are propagated unchanged, but cause deopts because
3862 // they can't be represented as integers.
3863 // - The sign of the result is the same as the sign of the input. This means
3864 // that -0.0 rounds to itself, and values -0.5 <= input < 0 also produce a
3865 // result of -0.0.
3866
3867 // Add 0.5 and round towards -infinity.
3868 __ Fmov(dot_five, 0.5);
3869 __ Fadd(temp, input, dot_five);
3870 __ Fcvtms(result, temp);
3871
3872 // The result is correct if:
3873 // result is not 0, as the input could be NaN or [-0.5, -0.0].
3874 // result is not 1, as 0.499...94 will wrongly map to 1.
3875 // result fits in 32 bits.
3876 __ Cmp(result, Operand(result.W(), SXTW));
3877 __ Ccmp(result, 1, ZFlag, eq);
3878 __ B(hi, &done);
3879
3880 // At this point, we have to handle possible inputs of NaN or numbers in the
3881 // range [-0.5, 1.5[, or numbers larger than 32 bits.
3882
3883 // Deoptimize if the result > 1, as it must be larger than 32 bits.
3884 __ Cmp(result, 1);
3885 DeoptimizeIf(hi, instr, Deoptimizer::kOverflow);
3886
3887 // Deoptimize for negative inputs, which at this point are only numbers in
3888 // the range [-0.5, -0.0]
3889 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3890 __ Fmov(result, input);
3891 DeoptimizeIfNegative(result, instr, Deoptimizer::kMinusZero);
3892 }
3893
3894 // Deoptimize if the input was NaN.
3895 __ Fcmp(input, dot_five);
3896 DeoptimizeIf(vs, instr, Deoptimizer::kNaN);
3897
3898 // Now, the only unhandled inputs are in the range [0.0, 1.5[ (or [-0.5, 1.5[
3899 // if we didn't generate a -0.0 bailout). If input >= 0.5 then return 1,
3900 // else 0; we avoid dealing with 0.499...94 directly.
3901 __ Cset(result, ge);
3902 __ Bind(&done);
3903}
3904
3905
3906void LCodeGen::DoMathFround(LMathFround* instr) {
3907 DoubleRegister input = ToDoubleRegister(instr->value());
3908 DoubleRegister result = ToDoubleRegister(instr->result());
3909 __ Fcvt(result.S(), input);
3910 __ Fcvt(result, result.S());
3911}
3912
3913
3914void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3915 DoubleRegister input = ToDoubleRegister(instr->value());
3916 DoubleRegister result = ToDoubleRegister(instr->result());
3917 __ Fsqrt(result, input);
3918}
3919
3920
3921void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
3922 HMathMinMax::Operation op = instr->hydrogen()->operation();
3923 if (instr->hydrogen()->representation().IsInteger32()) {
3924 Register result = ToRegister32(instr->result());
3925 Register left = ToRegister32(instr->left());
3926 Operand right = ToOperand32(instr->right());
3927
3928 __ Cmp(left, right);
3929 __ Csel(result, left, right, (op == HMathMinMax::kMathMax) ? ge : le);
3930 } else if (instr->hydrogen()->representation().IsSmi()) {
3931 Register result = ToRegister(instr->result());
3932 Register left = ToRegister(instr->left());
3933 Operand right = ToOperand(instr->right());
3934
3935 __ Cmp(left, right);
3936 __ Csel(result, left, right, (op == HMathMinMax::kMathMax) ? ge : le);
3937 } else {
3938 DCHECK(instr->hydrogen()->representation().IsDouble());
3939 DoubleRegister result = ToDoubleRegister(instr->result());
3940 DoubleRegister left = ToDoubleRegister(instr->left());
3941 DoubleRegister right = ToDoubleRegister(instr->right());
3942
3943 if (op == HMathMinMax::kMathMax) {
3944 __ Fmax(result, left, right);
3945 } else {
3946 DCHECK(op == HMathMinMax::kMathMin);
3947 __ Fmin(result, left, right);
3948 }
3949 }
3950}
3951
3952
3953void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
3954 Register dividend = ToRegister32(instr->dividend());
3955 int32_t divisor = instr->divisor();
3956 DCHECK(dividend.is(ToRegister32(instr->result())));
3957
3958 // Theoretically, a variation of the branch-free code for integer division by
3959 // a power of 2 (calculating the remainder via an additional multiplication
3960 // (which gets simplified to an 'and') and subtraction) should be faster, and
3961 // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
3962 // indicate that positive dividends are heavily favored, so the branching
3963 // version performs better.
3964 HMod* hmod = instr->hydrogen();
3965 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
3966 Label dividend_is_not_negative, done;
3967 if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
3968 __ Tbz(dividend, kWSignBit, &dividend_is_not_negative);
3969 // Note that this is correct even for kMinInt operands.
3970 __ Neg(dividend, dividend);
3971 __ And(dividend, dividend, mask);
3972 __ Negs(dividend, dividend);
3973 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
3974 DeoptimizeIf(eq, instr, Deoptimizer::kMinusZero);
3975 }
3976 __ B(&done);
3977 }
3978
3979 __ bind(&dividend_is_not_negative);
3980 __ And(dividend, dividend, mask);
3981 __ bind(&done);
3982}
3983
3984
3985void LCodeGen::DoModByConstI(LModByConstI* instr) {
3986 Register dividend = ToRegister32(instr->dividend());
3987 int32_t divisor = instr->divisor();
3988 Register result = ToRegister32(instr->result());
3989 Register temp = ToRegister32(instr->temp());
3990 DCHECK(!AreAliased(dividend, result, temp));
3991
3992 if (divisor == 0) {
3993 Deoptimize(instr, Deoptimizer::kDivisionByZero);
3994 return;
3995 }
3996
3997 __ TruncatingDiv(result, dividend, Abs(divisor));
3998 __ Sxtw(dividend.X(), dividend);
3999 __ Mov(temp, Abs(divisor));
4000 __ Smsubl(result.X(), result, temp, dividend.X());
4001
4002 // Check for negative zero.
4003 HMod* hmod = instr->hydrogen();
4004 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
4005 Label remainder_not_zero;
4006 __ Cbnz(result, &remainder_not_zero);
4007 DeoptimizeIfNegative(dividend, instr, Deoptimizer::kMinusZero);
4008 __ bind(&remainder_not_zero);
4009 }
4010}
4011
4012
4013void LCodeGen::DoModI(LModI* instr) {
4014 Register dividend = ToRegister32(instr->left());
4015 Register divisor = ToRegister32(instr->right());
4016 Register result = ToRegister32(instr->result());
4017
4018 Label done;
4019 // modulo = dividend - quotient * divisor
4020 __ Sdiv(result, dividend, divisor);
4021 if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) {
4022 DeoptimizeIfZero(divisor, instr, Deoptimizer::kDivisionByZero);
4023 }
4024 __ Msub(result, result, divisor, dividend);
4025 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
4026 __ Cbnz(result, &done);
4027 DeoptimizeIfNegative(dividend, instr, Deoptimizer::kMinusZero);
4028 }
4029 __ Bind(&done);
4030}
4031
4032
4033void LCodeGen::DoMulConstIS(LMulConstIS* instr) {
4034 DCHECK(instr->hydrogen()->representation().IsSmiOrInteger32());
4035 bool is_smi = instr->hydrogen()->representation().IsSmi();
4036 Register result =
4037 is_smi ? ToRegister(instr->result()) : ToRegister32(instr->result());
4038 Register left =
4039 is_smi ? ToRegister(instr->left()) : ToRegister32(instr->left());
4040 int32_t right = ToInteger32(instr->right());
4041 DCHECK((right > -kMaxInt) && (right < kMaxInt));
4042
4043 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
4044 bool bailout_on_minus_zero =
4045 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
4046
4047 if (bailout_on_minus_zero) {
4048 if (right < 0) {
4049 // The result is -0 if right is negative and left is zero.
4050 DeoptimizeIfZero(left, instr, Deoptimizer::kMinusZero);
4051 } else if (right == 0) {
4052 // The result is -0 if the right is zero and the left is negative.
4053 DeoptimizeIfNegative(left, instr, Deoptimizer::kMinusZero);
4054 }
4055 }
4056
4057 switch (right) {
4058 // Cases which can detect overflow.
4059 case -1:
4060 if (can_overflow) {
4061 // Only 0x80000000 can overflow here.
4062 __ Negs(result, left);
4063 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
4064 } else {
4065 __ Neg(result, left);
4066 }
4067 break;
4068 case 0:
4069 // This case can never overflow.
4070 __ Mov(result, 0);
4071 break;
4072 case 1:
4073 // This case can never overflow.
4074 __ Mov(result, left, kDiscardForSameWReg);
4075 break;
4076 case 2:
4077 if (can_overflow) {
4078 __ Adds(result, left, left);
4079 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
4080 } else {
4081 __ Add(result, left, left);
4082 }
4083 break;
4084
4085 default:
4086 // Multiplication by constant powers of two (and some related values)
4087 // can be done efficiently with shifted operands.
4088 int32_t right_abs = Abs(right);
4089
4090 if (base::bits::IsPowerOfTwo32(right_abs)) {
4091 int right_log2 = WhichPowerOf2(right_abs);
4092
4093 if (can_overflow) {
4094 Register scratch = result;
4095 DCHECK(!AreAliased(scratch, left));
4096 __ Cls(scratch, left);
4097 __ Cmp(scratch, right_log2);
4098 DeoptimizeIf(lt, instr, Deoptimizer::kOverflow);
4099 }
4100
4101 if (right >= 0) {
4102 // result = left << log2(right)
4103 __ Lsl(result, left, right_log2);
4104 } else {
4105 // result = -left << log2(-right)
4106 if (can_overflow) {
4107 __ Negs(result, Operand(left, LSL, right_log2));
4108 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
4109 } else {
4110 __ Neg(result, Operand(left, LSL, right_log2));
4111 }
4112 }
4113 return;
4114 }
4115
4116
4117 // For the following cases, we could perform a conservative overflow check
4118 // with CLS as above. However the few cycles saved are likely not worth
4119 // the risk of deoptimizing more often than required.
4120 DCHECK(!can_overflow);
4121
4122 if (right >= 0) {
4123 if (base::bits::IsPowerOfTwo32(right - 1)) {
4124 // result = left + left << log2(right - 1)
4125 __ Add(result, left, Operand(left, LSL, WhichPowerOf2(right - 1)));
4126 } else if (base::bits::IsPowerOfTwo32(right + 1)) {
4127 // result = -left + left << log2(right + 1)
4128 __ Sub(result, left, Operand(left, LSL, WhichPowerOf2(right + 1)));
4129 __ Neg(result, result);
4130 } else {
4131 UNREACHABLE();
4132 }
4133 } else {
4134 if (base::bits::IsPowerOfTwo32(-right + 1)) {
4135 // result = left - left << log2(-right + 1)
4136 __ Sub(result, left, Operand(left, LSL, WhichPowerOf2(-right + 1)));
4137 } else if (base::bits::IsPowerOfTwo32(-right - 1)) {
4138 // result = -left - left << log2(-right - 1)
4139 __ Add(result, left, Operand(left, LSL, WhichPowerOf2(-right - 1)));
4140 __ Neg(result, result);
4141 } else {
4142 UNREACHABLE();
4143 }
4144 }
4145 }
4146}
4147
4148
4149void LCodeGen::DoMulI(LMulI* instr) {
4150 Register result = ToRegister32(instr->result());
4151 Register left = ToRegister32(instr->left());
4152 Register right = ToRegister32(instr->right());
4153
4154 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
4155 bool bailout_on_minus_zero =
4156 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
4157
4158 if (bailout_on_minus_zero && !left.Is(right)) {
4159 // If one operand is zero and the other is negative, the result is -0.
4160 // - Set Z (eq) if either left or right, or both, are 0.
4161 __ Cmp(left, 0);
4162 __ Ccmp(right, 0, ZFlag, ne);
4163 // - If so (eq), set N (mi) if left + right is negative.
4164 // - Otherwise, clear N.
4165 __ Ccmn(left, right, NoFlag, eq);
4166 DeoptimizeIf(mi, instr, Deoptimizer::kMinusZero);
4167 }
4168
4169 if (can_overflow) {
4170 __ Smull(result.X(), left, right);
4171 __ Cmp(result.X(), Operand(result, SXTW));
4172 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow);
4173 } else {
4174 __ Mul(result, left, right);
4175 }
4176}
4177
4178
4179void LCodeGen::DoMulS(LMulS* instr) {
4180 Register result = ToRegister(instr->result());
4181 Register left = ToRegister(instr->left());
4182 Register right = ToRegister(instr->right());
4183
4184 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
4185 bool bailout_on_minus_zero =
4186 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
4187
4188 if (bailout_on_minus_zero && !left.Is(right)) {
4189 // If one operand is zero and the other is negative, the result is -0.
4190 // - Set Z (eq) if either left or right, or both, are 0.
4191 __ Cmp(left, 0);
4192 __ Ccmp(right, 0, ZFlag, ne);
4193 // - If so (eq), set N (mi) if left + right is negative.
4194 // - Otherwise, clear N.
4195 __ Ccmn(left, right, NoFlag, eq);
4196 DeoptimizeIf(mi, instr, Deoptimizer::kMinusZero);
4197 }
4198
4199 STATIC_ASSERT((kSmiShift == 32) && (kSmiTag == 0));
4200 if (can_overflow) {
4201 __ Smulh(result, left, right);
4202 __ Cmp(result, Operand(result.W(), SXTW));
4203 __ SmiTag(result);
4204 DeoptimizeIf(ne, instr, Deoptimizer::kOverflow);
4205 } else {
4206 if (AreAliased(result, left, right)) {
4207 // All three registers are the same: half untag the input and then
4208 // multiply, giving a tagged result.
4209 STATIC_ASSERT((kSmiShift % 2) == 0);
4210 __ Asr(result, left, kSmiShift / 2);
4211 __ Mul(result, result, result);
4212 } else if (result.Is(left) && !left.Is(right)) {
4213 // Registers result and left alias, right is distinct: untag left into
4214 // result, and then multiply by right, giving a tagged result.
4215 __ SmiUntag(result, left);
4216 __ Mul(result, result, right);
4217 } else {
4218 DCHECK(!left.Is(result));
4219 // Registers result and right alias, left is distinct, or all registers
4220 // are distinct: untag right into result, and then multiply by left,
4221 // giving a tagged result.
4222 __ SmiUntag(result, right);
4223 __ Mul(result, left, result);
4224 }
4225 }
4226}
4227
4228
4229void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4230 // TODO(3095996): Get rid of this. For now, we need to make the
4231 // result register contain a valid pointer because it is already
4232 // contained in the register pointer map.
4233 Register result = ToRegister(instr->result());
4234 __ Mov(result, 0);
4235
4236 PushSafepointRegistersScope scope(this);
4237 // NumberTagU and NumberTagD use the context from the frame, rather than
4238 // the environment's HContext or HInlinedContext value.
4239 // They only call Runtime::kAllocateHeapNumber.
4240 // The corresponding HChange instructions are added in a phase that does
4241 // not have easy access to the local context.
4242 __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4243 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4244 RecordSafepointWithRegisters(
4245 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4246 __ StoreToSafepointRegisterSlot(x0, result);
4247}
4248
4249
4250void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
4251 class DeferredNumberTagD: public LDeferredCode {
4252 public:
4253 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4254 : LDeferredCode(codegen), instr_(instr) { }
4255 virtual void Generate() { codegen()->DoDeferredNumberTagD(instr_); }
4256 virtual LInstruction* instr() { return instr_; }
4257 private:
4258 LNumberTagD* instr_;
4259 };
4260
4261 DoubleRegister input = ToDoubleRegister(instr->value());
4262 Register result = ToRegister(instr->result());
4263 Register temp1 = ToRegister(instr->temp1());
4264 Register temp2 = ToRegister(instr->temp2());
4265
4266 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
4267 if (FLAG_inline_new) {
4268 __ AllocateHeapNumber(result, deferred->entry(), temp1, temp2);
4269 } else {
4270 __ B(deferred->entry());
4271 }
4272
4273 __ Bind(deferred->exit());
4274 __ Str(input, FieldMemOperand(result, HeapNumber::kValueOffset));
4275}
4276
4277
4278void LCodeGen::DoDeferredNumberTagU(LInstruction* instr,
4279 LOperand* value,
4280 LOperand* temp1,
4281 LOperand* temp2) {
4282 Label slow, convert_and_store;
4283 Register src = ToRegister32(value);
4284 Register dst = ToRegister(instr->result());
4285 Register scratch1 = ToRegister(temp1);
4286
4287 if (FLAG_inline_new) {
4288 Register scratch2 = ToRegister(temp2);
4289 __ AllocateHeapNumber(dst, &slow, scratch1, scratch2);
4290 __ B(&convert_and_store);
4291 }
4292
4293 // Slow case: call the runtime system to do the number allocation.
4294 __ Bind(&slow);
4295 // TODO(3095996): Put a valid pointer value in the stack slot where the result
4296 // register is stored, as this register is in the pointer map, but contains an
4297 // integer value.
4298 __ Mov(dst, 0);
4299 {
4300 // Preserve the value of all registers.
4301 PushSafepointRegistersScope scope(this);
4302
4303 // NumberTagU and NumberTagD use the context from the frame, rather than
4304 // the environment's HContext or HInlinedContext value.
4305 // They only call Runtime::kAllocateHeapNumber.
4306 // The corresponding HChange instructions are added in a phase that does
4307 // not have easy access to the local context.
4308 __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4309 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4310 RecordSafepointWithRegisters(
4311 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4312 __ StoreToSafepointRegisterSlot(x0, dst);
4313 }
4314
4315 // Convert number to floating point and store in the newly allocated heap
4316 // number.
4317 __ Bind(&convert_and_store);
4318 DoubleRegister dbl_scratch = double_scratch();
4319 __ Ucvtf(dbl_scratch, src);
4320 __ Str(dbl_scratch, FieldMemOperand(dst, HeapNumber::kValueOffset));
4321}
4322
4323
4324void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4325 class DeferredNumberTagU: public LDeferredCode {
4326 public:
4327 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4328 : LDeferredCode(codegen), instr_(instr) { }
4329 virtual void Generate() {
4330 codegen()->DoDeferredNumberTagU(instr_,
4331 instr_->value(),
4332 instr_->temp1(),
4333 instr_->temp2());
4334 }
4335 virtual LInstruction* instr() { return instr_; }
4336 private:
4337 LNumberTagU* instr_;
4338 };
4339
4340 Register value = ToRegister32(instr->value());
4341 Register result = ToRegister(instr->result());
4342
4343 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4344 __ Cmp(value, Smi::kMaxValue);
4345 __ B(hi, deferred->entry());
4346 __ SmiTag(result, value.X());
4347 __ Bind(deferred->exit());
4348}
4349
4350
4351void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
4352 Register input = ToRegister(instr->value());
4353 Register scratch = ToRegister(instr->temp());
4354 DoubleRegister result = ToDoubleRegister(instr->result());
4355 bool can_convert_undefined_to_nan =
4356 instr->hydrogen()->can_convert_undefined_to_nan();
4357
4358 Label done, load_smi;
4359
4360 // Work out what untag mode we're working with.
4361 HValue* value = instr->hydrogen()->value();
4362 NumberUntagDMode mode = value->representation().IsSmi()
4363 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
4364
4365 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4366 __ JumpIfSmi(input, &load_smi);
4367
4368 Label convert_undefined;
4369
4370 // Heap number map check.
4371 if (can_convert_undefined_to_nan) {
4372 __ JumpIfNotHeapNumber(input, &convert_undefined);
4373 } else {
4374 DeoptimizeIfNotHeapNumber(input, instr);
4375 }
4376
4377 // Load heap number.
4378 __ Ldr(result, FieldMemOperand(input, HeapNumber::kValueOffset));
4379 if (instr->hydrogen()->deoptimize_on_minus_zero()) {
4380 DeoptimizeIfMinusZero(result, instr, Deoptimizer::kMinusZero);
4381 }
4382 __ B(&done);
4383
4384 if (can_convert_undefined_to_nan) {
4385 __ Bind(&convert_undefined);
4386 DeoptimizeIfNotRoot(input, Heap::kUndefinedValueRootIndex, instr,
4387 Deoptimizer::kNotAHeapNumberUndefined);
4388
4389 __ LoadRoot(scratch, Heap::kNanValueRootIndex);
4390 __ Ldr(result, FieldMemOperand(scratch, HeapNumber::kValueOffset));
4391 __ B(&done);
4392 }
4393
4394 } else {
4395 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
4396 // Fall through to load_smi.
4397 }
4398
4399 // Smi to double register conversion.
4400 __ Bind(&load_smi);
4401 __ SmiUntagToDouble(result, input);
4402
4403 __ Bind(&done);
4404}
4405
4406
4407void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
4408 // This is a pseudo-instruction that ensures that the environment here is
4409 // properly registered for deoptimization and records the assembler's PC
4410 // offset.
4411 LEnvironment* environment = instr->environment();
4412
4413 // If the environment were already registered, we would have no way of
4414 // backpatching it with the spill slot operands.
4415 DCHECK(!environment->HasBeenRegistered());
4416 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
4417
4418 GenerateOsrPrologue();
4419}
4420
4421
4422void LCodeGen::DoParameter(LParameter* instr) {
4423 // Nothing to do.
4424}
4425
4426
4427void LCodeGen::DoPreparePushArguments(LPreparePushArguments* instr) {
4428 __ PushPreamble(instr->argc(), kPointerSize);
4429}
4430
4431
4432void LCodeGen::DoPushArguments(LPushArguments* instr) {
4433 MacroAssembler::PushPopQueue args(masm());
4434
4435 for (int i = 0; i < instr->ArgumentCount(); ++i) {
4436 LOperand* arg = instr->argument(i);
4437 if (arg->IsDoubleRegister() || arg->IsDoubleStackSlot()) {
4438 Abort(kDoPushArgumentNotImplementedForDoubleType);
4439 return;
4440 }
4441 args.Queue(ToRegister(arg));
4442 }
4443
4444 // The preamble was done by LPreparePushArguments.
4445 args.PushQueued(MacroAssembler::PushPopQueue::SKIP_PREAMBLE);
4446
4447 RecordPushedArgumentsDelta(instr->ArgumentCount());
4448}
4449
4450
4451void LCodeGen::DoReturn(LReturn* instr) {
4452 if (FLAG_trace && info()->IsOptimizing()) {
4453 // Push the return value on the stack as the parameter.
4454 // Runtime::TraceExit returns its parameter in x0. We're leaving the code
4455 // managed by the register allocator and tearing down the frame, it's
4456 // safe to write to the context register.
4457 __ Push(x0);
4458 __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4459 __ CallRuntime(Runtime::kTraceExit);
4460 }
4461
4462 if (info()->saves_caller_doubles()) {
4463 RestoreCallerDoubles();
4464 }
4465
4466 if (NeedsEagerFrame()) {
4467 Register stack_pointer = masm()->StackPointer();
4468 __ Mov(stack_pointer, fp);
4469 __ Pop(fp, lr);
4470 }
4471
4472 if (instr->has_constant_parameter_count()) {
4473 int parameter_count = ToInteger32(instr->constant_parameter_count());
4474 __ Drop(parameter_count + 1);
4475 } else {
4476 DCHECK(info()->IsStub()); // Functions would need to drop one more value.
4477 Register parameter_count = ToRegister(instr->parameter_count());
4478 __ DropBySMI(parameter_count);
4479 }
4480 __ Ret();
4481}
4482
4483
4484MemOperand LCodeGen::BuildSeqStringOperand(Register string,
4485 Register temp,
4486 LOperand* index,
4487 String::Encoding encoding) {
4488 if (index->IsConstantOperand()) {
4489 int offset = ToInteger32(LConstantOperand::cast(index));
4490 if (encoding == String::TWO_BYTE_ENCODING) {
4491 offset *= kUC16Size;
4492 }
4493 STATIC_ASSERT(kCharSize == 1);
4494 return FieldMemOperand(string, SeqString::kHeaderSize + offset);
4495 }
4496
4497 __ Add(temp, string, SeqString::kHeaderSize - kHeapObjectTag);
4498 if (encoding == String::ONE_BYTE_ENCODING) {
4499 return MemOperand(temp, ToRegister32(index), SXTW);
4500 } else {
4501 STATIC_ASSERT(kUC16Size == 2);
4502 return MemOperand(temp, ToRegister32(index), SXTW, 1);
4503 }
4504}
4505
4506
4507void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
4508 String::Encoding encoding = instr->hydrogen()->encoding();
4509 Register string = ToRegister(instr->string());
4510 Register result = ToRegister(instr->result());
4511 Register temp = ToRegister(instr->temp());
4512
4513 if (FLAG_debug_code) {
4514 // Even though this lithium instruction comes with a temp register, we
4515 // can't use it here because we want to use "AtStart" constraints on the
4516 // inputs and the debug code here needs a scratch register.
4517 UseScratchRegisterScope temps(masm());
4518 Register dbg_temp = temps.AcquireX();
4519
4520 __ Ldr(dbg_temp, FieldMemOperand(string, HeapObject::kMapOffset));
4521 __ Ldrb(dbg_temp, FieldMemOperand(dbg_temp, Map::kInstanceTypeOffset));
4522
4523 __ And(dbg_temp, dbg_temp,
4524 Operand(kStringRepresentationMask | kStringEncodingMask));
4525 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
4526 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
4527 __ Cmp(dbg_temp, Operand(encoding == String::ONE_BYTE_ENCODING
4528 ? one_byte_seq_type : two_byte_seq_type));
4529 __ Check(eq, kUnexpectedStringType);
4530 }
4531
4532 MemOperand operand =
4533 BuildSeqStringOperand(string, temp, instr->index(), encoding);
4534 if (encoding == String::ONE_BYTE_ENCODING) {
4535 __ Ldrb(result, operand);
4536 } else {
4537 __ Ldrh(result, operand);
4538 }
4539}
4540
4541
4542void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
4543 String::Encoding encoding = instr->hydrogen()->encoding();
4544 Register string = ToRegister(instr->string());
4545 Register value = ToRegister(instr->value());
4546 Register temp = ToRegister(instr->temp());
4547
4548 if (FLAG_debug_code) {
4549 DCHECK(ToRegister(instr->context()).is(cp));
4550 Register index = ToRegister(instr->index());
4551 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
4552 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
4553 int encoding_mask =
4554 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
4555 ? one_byte_seq_type : two_byte_seq_type;
4556 __ EmitSeqStringSetCharCheck(string, index, kIndexIsInteger32, temp,
4557 encoding_mask);
4558 }
4559 MemOperand operand =
4560 BuildSeqStringOperand(string, temp, instr->index(), encoding);
4561 if (encoding == String::ONE_BYTE_ENCODING) {
4562 __ Strb(value, operand);
4563 } else {
4564 __ Strh(value, operand);
4565 }
4566}
4567
4568
4569void LCodeGen::DoSmiTag(LSmiTag* instr) {
4570 HChange* hchange = instr->hydrogen();
4571 Register input = ToRegister(instr->value());
4572 Register output = ToRegister(instr->result());
4573 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4574 hchange->value()->CheckFlag(HValue::kUint32)) {
4575 DeoptimizeIfNegative(input.W(), instr, Deoptimizer::kOverflow);
4576 }
4577 __ SmiTag(output, input);
4578}
4579
4580
4581void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4582 Register input = ToRegister(instr->value());
4583 Register result = ToRegister(instr->result());
4584 Label done, untag;
4585
4586 if (instr->needs_check()) {
4587 DeoptimizeIfNotSmi(input, instr, Deoptimizer::kNotASmi);
4588 }
4589
4590 __ Bind(&untag);
4591 __ SmiUntag(result, input);
4592 __ Bind(&done);
4593}
4594
4595
4596void LCodeGen::DoShiftI(LShiftI* instr) {
4597 LOperand* right_op = instr->right();
4598 Register left = ToRegister32(instr->left());
4599 Register result = ToRegister32(instr->result());
4600
4601 if (right_op->IsRegister()) {
4602 Register right = ToRegister32(instr->right());
4603 switch (instr->op()) {
4604 case Token::ROR: __ Ror(result, left, right); break;
4605 case Token::SAR: __ Asr(result, left, right); break;
4606 case Token::SHL: __ Lsl(result, left, right); break;
4607 case Token::SHR:
4608 __ Lsr(result, left, right);
4609 if (instr->can_deopt()) {
4610 // If `left >>> right` >= 0x80000000, the result is not representable
4611 // in a signed 32-bit smi.
4612 DeoptimizeIfNegative(result, instr, Deoptimizer::kNegativeValue);
4613 }
4614 break;
4615 default: UNREACHABLE();
4616 }
4617 } else {
4618 DCHECK(right_op->IsConstantOperand());
4619 int shift_count = JSShiftAmountFromLConstant(right_op);
4620 if (shift_count == 0) {
4621 if ((instr->op() == Token::SHR) && instr->can_deopt()) {
4622 DeoptimizeIfNegative(left, instr, Deoptimizer::kNegativeValue);
4623 }
4624 __ Mov(result, left, kDiscardForSameWReg);
4625 } else {
4626 switch (instr->op()) {
4627 case Token::ROR: __ Ror(result, left, shift_count); break;
4628 case Token::SAR: __ Asr(result, left, shift_count); break;
4629 case Token::SHL: __ Lsl(result, left, shift_count); break;
4630 case Token::SHR: __ Lsr(result, left, shift_count); break;
4631 default: UNREACHABLE();
4632 }
4633 }
4634 }
4635}
4636
4637
4638void LCodeGen::DoShiftS(LShiftS* instr) {
4639 LOperand* right_op = instr->right();
4640 Register left = ToRegister(instr->left());
4641 Register result = ToRegister(instr->result());
4642
4643 if (right_op->IsRegister()) {
4644 Register right = ToRegister(instr->right());
4645
4646 // JavaScript shifts only look at the bottom 5 bits of the 'right' operand.
4647 // Since we're handling smis in X registers, we have to extract these bits
4648 // explicitly.
4649 __ Ubfx(result, right, kSmiShift, 5);
4650
4651 switch (instr->op()) {
4652 case Token::ROR: {
4653 // This is the only case that needs a scratch register. To keep things
4654 // simple for the other cases, borrow a MacroAssembler scratch register.
4655 UseScratchRegisterScope temps(masm());
4656 Register temp = temps.AcquireW();
4657 __ SmiUntag(temp, left);
4658 __ Ror(result.W(), temp.W(), result.W());
4659 __ SmiTag(result);
4660 break;
4661 }
4662 case Token::SAR:
4663 __ Asr(result, left, result);
4664 __ Bic(result, result, kSmiShiftMask);
4665 break;
4666 case Token::SHL:
4667 __ Lsl(result, left, result);
4668 break;
4669 case Token::SHR:
4670 __ Lsr(result, left, result);
4671 __ Bic(result, result, kSmiShiftMask);
4672 if (instr->can_deopt()) {
4673 // If `left >>> right` >= 0x80000000, the result is not representable
4674 // in a signed 32-bit smi.
4675 DeoptimizeIfNegative(result, instr, Deoptimizer::kNegativeValue);
4676 }
4677 break;
4678 default: UNREACHABLE();
4679 }
4680 } else {
4681 DCHECK(right_op->IsConstantOperand());
4682 int shift_count = JSShiftAmountFromLConstant(right_op);
4683 if (shift_count == 0) {
4684 if ((instr->op() == Token::SHR) && instr->can_deopt()) {
4685 DeoptimizeIfNegative(left, instr, Deoptimizer::kNegativeValue);
4686 }
4687 __ Mov(result, left);
4688 } else {
4689 switch (instr->op()) {
4690 case Token::ROR:
4691 __ SmiUntag(result, left);
4692 __ Ror(result.W(), result.W(), shift_count);
4693 __ SmiTag(result);
4694 break;
4695 case Token::SAR:
4696 __ Asr(result, left, shift_count);
4697 __ Bic(result, result, kSmiShiftMask);
4698 break;
4699 case Token::SHL:
4700 __ Lsl(result, left, shift_count);
4701 break;
4702 case Token::SHR:
4703 __ Lsr(result, left, shift_count);
4704 __ Bic(result, result, kSmiShiftMask);
4705 break;
4706 default: UNREACHABLE();
4707 }
4708 }
4709 }
4710}
4711
4712
4713void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
4714 __ Debug("LDebugBreak", 0, BREAK);
4715}
4716
4717
4718void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
4719 DCHECK(ToRegister(instr->context()).is(cp));
4720 Register scratch1 = x5;
4721 Register scratch2 = x6;
4722 DCHECK(instr->IsMarkedAsCall());
4723
4724 // TODO(all): if Mov could handle object in new space then it could be used
4725 // here.
4726 __ LoadHeapObject(scratch1, instr->hydrogen()->pairs());
4727 __ Mov(scratch2, Smi::FromInt(instr->hydrogen()->flags()));
4728 __ Push(scratch1, scratch2);
4729 CallRuntime(Runtime::kDeclareGlobals, instr);
4730}
4731
4732
4733void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
4734 PushSafepointRegistersScope scope(this);
4735 LoadContextFromDeferred(instr->context());
4736 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
4737 RecordSafepointWithLazyDeopt(
4738 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
4739 DCHECK(instr->HasEnvironment());
4740 LEnvironment* env = instr->environment();
4741 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
4742}
4743
4744
4745void LCodeGen::DoStackCheck(LStackCheck* instr) {
4746 class DeferredStackCheck: public LDeferredCode {
4747 public:
4748 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
4749 : LDeferredCode(codegen), instr_(instr) { }
4750 virtual void Generate() { codegen()->DoDeferredStackCheck(instr_); }
4751 virtual LInstruction* instr() { return instr_; }
4752 private:
4753 LStackCheck* instr_;
4754 };
4755
4756 DCHECK(instr->HasEnvironment());
4757 LEnvironment* env = instr->environment();
4758 // There is no LLazyBailout instruction for stack-checks. We have to
4759 // prepare for lazy deoptimization explicitly here.
4760 if (instr->hydrogen()->is_function_entry()) {
4761 // Perform stack overflow check.
4762 Label done;
4763 __ CompareRoot(masm()->StackPointer(), Heap::kStackLimitRootIndex);
4764 __ B(hs, &done);
4765
4766 PredictableCodeSizeScope predictable(masm_,
4767 Assembler::kCallSizeWithRelocation);
4768 DCHECK(instr->context()->IsRegister());
4769 DCHECK(ToRegister(instr->context()).is(cp));
4770 CallCode(isolate()->builtins()->StackCheck(),
4771 RelocInfo::CODE_TARGET,
4772 instr);
4773 __ Bind(&done);
4774 } else {
4775 DCHECK(instr->hydrogen()->is_backwards_branch());
4776 // Perform stack overflow check if this goto needs it before jumping.
4777 DeferredStackCheck* deferred_stack_check =
4778 new(zone()) DeferredStackCheck(this, instr);
4779 __ CompareRoot(masm()->StackPointer(), Heap::kStackLimitRootIndex);
4780 __ B(lo, deferred_stack_check->entry());
4781
4782 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
4783 __ Bind(instr->done_label());
4784 deferred_stack_check->SetExit(instr->done_label());
4785 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
4786 // Don't record a deoptimization index for the safepoint here.
4787 // This will be done explicitly when emitting call and the safepoint in
4788 // the deferred code.
4789 }
4790}
4791
4792
4793void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
4794 Register function = ToRegister(instr->function());
4795 Register code_object = ToRegister(instr->code_object());
4796 Register temp = ToRegister(instr->temp());
4797 __ Add(temp, code_object, Code::kHeaderSize - kHeapObjectTag);
4798 __ Str(temp, FieldMemOperand(function, JSFunction::kCodeEntryOffset));
4799}
4800
4801
4802void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
4803 Register context = ToRegister(instr->context());
4804 Register value = ToRegister(instr->value());
4805 Register scratch = ToRegister(instr->temp());
4806 MemOperand target = ContextMemOperand(context, instr->slot_index());
4807
4808 Label skip_assignment;
4809
4810 if (instr->hydrogen()->RequiresHoleCheck()) {
4811 __ Ldr(scratch, target);
4812 if (instr->hydrogen()->DeoptimizesOnHole()) {
4813 DeoptimizeIfRoot(scratch, Heap::kTheHoleValueRootIndex, instr,
4814 Deoptimizer::kHole);
4815 } else {
4816 __ JumpIfNotRoot(scratch, Heap::kTheHoleValueRootIndex, &skip_assignment);
4817 }
4818 }
4819
4820 __ Str(value, target);
4821 if (instr->hydrogen()->NeedsWriteBarrier()) {
4822 SmiCheck check_needed =
4823 instr->hydrogen()->value()->type().IsHeapObject()
4824 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4825 __ RecordWriteContextSlot(context, static_cast<int>(target.offset()), value,
4826 scratch, GetLinkRegisterState(), kSaveFPRegs,
4827 EMIT_REMEMBERED_SET, check_needed);
4828 }
4829 __ Bind(&skip_assignment);
4830}
4831
4832
4833void LCodeGen::DoStoreKeyedExternal(LStoreKeyedExternal* instr) {
4834 Register ext_ptr = ToRegister(instr->elements());
4835 Register key = no_reg;
4836 Register scratch;
4837 ElementsKind elements_kind = instr->elements_kind();
4838
4839 bool key_is_smi = instr->hydrogen()->key()->representation().IsSmi();
4840 bool key_is_constant = instr->key()->IsConstantOperand();
4841 int constant_key = 0;
4842 if (key_is_constant) {
4843 DCHECK(instr->temp() == NULL);
4844 constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4845 if (constant_key & 0xf0000000) {
4846 Abort(kArrayIndexConstantValueTooBig);
4847 }
4848 } else {
4849 key = ToRegister(instr->key());
4850 scratch = ToRegister(instr->temp());
4851 }
4852
4853 MemOperand dst =
4854 PrepareKeyedExternalArrayOperand(key, ext_ptr, scratch, key_is_smi,
4855 key_is_constant, constant_key,
4856 elements_kind,
4857 instr->base_offset());
4858
4859 if (elements_kind == FLOAT32_ELEMENTS) {
4860 DoubleRegister value = ToDoubleRegister(instr->value());
4861 DoubleRegister dbl_scratch = double_scratch();
4862 __ Fcvt(dbl_scratch.S(), value);
4863 __ Str(dbl_scratch.S(), dst);
4864 } else if (elements_kind == FLOAT64_ELEMENTS) {
4865 DoubleRegister value = ToDoubleRegister(instr->value());
4866 __ Str(value, dst);
4867 } else {
4868 Register value = ToRegister(instr->value());
4869
4870 switch (elements_kind) {
4871 case UINT8_ELEMENTS:
4872 case UINT8_CLAMPED_ELEMENTS:
4873 case INT8_ELEMENTS:
4874 __ Strb(value, dst);
4875 break;
4876 case INT16_ELEMENTS:
4877 case UINT16_ELEMENTS:
4878 __ Strh(value, dst);
4879 break;
4880 case INT32_ELEMENTS:
4881 case UINT32_ELEMENTS:
4882 __ Str(value.W(), dst);
4883 break;
4884 case FLOAT32_ELEMENTS:
4885 case FLOAT64_ELEMENTS:
4886 case FAST_DOUBLE_ELEMENTS:
4887 case FAST_ELEMENTS:
4888 case FAST_SMI_ELEMENTS:
4889 case FAST_HOLEY_DOUBLE_ELEMENTS:
4890 case FAST_HOLEY_ELEMENTS:
4891 case FAST_HOLEY_SMI_ELEMENTS:
4892 case DICTIONARY_ELEMENTS:
4893 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
4894 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
Ben Murdoch097c5b22016-05-18 11:27:45 +01004895 case FAST_STRING_WRAPPER_ELEMENTS:
4896 case SLOW_STRING_WRAPPER_ELEMENTS:
4897 case NO_ELEMENTS:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004898 UNREACHABLE();
4899 break;
4900 }
4901 }
4902}
4903
4904
4905void LCodeGen::DoStoreKeyedFixedDouble(LStoreKeyedFixedDouble* instr) {
4906 Register elements = ToRegister(instr->elements());
4907 DoubleRegister value = ToDoubleRegister(instr->value());
4908 MemOperand mem_op;
4909
4910 if (instr->key()->IsConstantOperand()) {
4911 int constant_key = ToInteger32(LConstantOperand::cast(instr->key()));
4912 if (constant_key & 0xf0000000) {
4913 Abort(kArrayIndexConstantValueTooBig);
4914 }
4915 int offset = instr->base_offset() + constant_key * kDoubleSize;
4916 mem_op = MemOperand(elements, offset);
4917 } else {
4918 Register store_base = ToRegister(instr->temp());
4919 Register key = ToRegister(instr->key());
4920 bool key_is_tagged = instr->hydrogen()->key()->representation().IsSmi();
4921 mem_op = PrepareKeyedArrayOperand(store_base, elements, key, key_is_tagged,
4922 instr->hydrogen()->elements_kind(),
4923 instr->hydrogen()->representation(),
4924 instr->base_offset());
4925 }
4926
4927 if (instr->NeedsCanonicalization()) {
4928 __ CanonicalizeNaN(double_scratch(), value);
4929 __ Str(double_scratch(), mem_op);
4930 } else {
4931 __ Str(value, mem_op);
4932 }
4933}
4934
4935
4936void LCodeGen::DoStoreKeyedFixed(LStoreKeyedFixed* instr) {
4937 Register value = ToRegister(instr->value());
4938 Register elements = ToRegister(instr->elements());
4939 Register scratch = no_reg;
4940 Register store_base = no_reg;
4941 Register key = no_reg;
4942 MemOperand mem_op;
4943
4944 if (!instr->key()->IsConstantOperand() ||
4945 instr->hydrogen()->NeedsWriteBarrier()) {
4946 scratch = ToRegister(instr->temp());
4947 }
4948
4949 Representation representation = instr->hydrogen()->value()->representation();
4950 if (instr->key()->IsConstantOperand()) {
4951 LConstantOperand* const_operand = LConstantOperand::cast(instr->key());
4952 int offset = instr->base_offset() +
4953 ToInteger32(const_operand) * kPointerSize;
4954 store_base = elements;
4955 if (representation.IsInteger32()) {
4956 DCHECK(instr->hydrogen()->store_mode() == STORE_TO_INITIALIZED_ENTRY);
4957 DCHECK(instr->hydrogen()->elements_kind() == FAST_SMI_ELEMENTS);
4958 STATIC_ASSERT(static_cast<unsigned>(kSmiValueSize) == kWRegSizeInBits);
4959 STATIC_ASSERT(kSmiTag == 0);
4960 mem_op = UntagSmiMemOperand(store_base, offset);
4961 } else {
4962 mem_op = MemOperand(store_base, offset);
4963 }
4964 } else {
4965 store_base = scratch;
4966 key = ToRegister(instr->key());
4967 bool key_is_tagged = instr->hydrogen()->key()->representation().IsSmi();
4968
4969 mem_op = PrepareKeyedArrayOperand(store_base, elements, key, key_is_tagged,
4970 instr->hydrogen()->elements_kind(),
4971 representation, instr->base_offset());
4972 }
4973
4974 __ Store(value, mem_op, representation);
4975
4976 if (instr->hydrogen()->NeedsWriteBarrier()) {
4977 DCHECK(representation.IsTagged());
4978 // This assignment may cause element_addr to alias store_base.
4979 Register element_addr = scratch;
4980 SmiCheck check_needed =
4981 instr->hydrogen()->value()->type().IsHeapObject()
4982 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4983 // Compute address of modified element and store it into key register.
4984 __ Add(element_addr, mem_op.base(), mem_op.OffsetAsOperand());
4985 __ RecordWrite(elements, element_addr, value, GetLinkRegisterState(),
4986 kSaveFPRegs, EMIT_REMEMBERED_SET, check_needed,
4987 instr->hydrogen()->PointersToHereCheckForValue());
4988 }
4989}
4990
4991
4992void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4993 DCHECK(ToRegister(instr->context()).is(cp));
4994 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4995 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4996 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4997
4998 if (instr->hydrogen()->HasVectorAndSlot()) {
4999 EmitVectorStoreICRegisters<LStoreKeyedGeneric>(instr);
5000 }
5001
5002 Handle<Code> ic = CodeFactory::KeyedStoreICInOptimizedCode(
5003 isolate(), instr->language_mode(),
5004 instr->hydrogen()->initialization_state()).code();
5005 CallCode(ic, RelocInfo::CODE_TARGET, instr);
5006}
5007
5008
5009void LCodeGen::DoMaybeGrowElements(LMaybeGrowElements* instr) {
5010 class DeferredMaybeGrowElements final : public LDeferredCode {
5011 public:
5012 DeferredMaybeGrowElements(LCodeGen* codegen, LMaybeGrowElements* instr)
5013 : LDeferredCode(codegen), instr_(instr) {}
5014 void Generate() override { codegen()->DoDeferredMaybeGrowElements(instr_); }
5015 LInstruction* instr() override { return instr_; }
5016
5017 private:
5018 LMaybeGrowElements* instr_;
5019 };
5020
5021 Register result = x0;
5022 DeferredMaybeGrowElements* deferred =
5023 new (zone()) DeferredMaybeGrowElements(this, instr);
5024 LOperand* key = instr->key();
5025 LOperand* current_capacity = instr->current_capacity();
5026
5027 DCHECK(instr->hydrogen()->key()->representation().IsInteger32());
5028 DCHECK(instr->hydrogen()->current_capacity()->representation().IsInteger32());
5029 DCHECK(key->IsConstantOperand() || key->IsRegister());
5030 DCHECK(current_capacity->IsConstantOperand() ||
5031 current_capacity->IsRegister());
5032
5033 if (key->IsConstantOperand() && current_capacity->IsConstantOperand()) {
5034 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
5035 int32_t constant_capacity =
5036 ToInteger32(LConstantOperand::cast(current_capacity));
5037 if (constant_key >= constant_capacity) {
5038 // Deferred case.
5039 __ B(deferred->entry());
5040 }
5041 } else if (key->IsConstantOperand()) {
5042 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
5043 __ Cmp(ToRegister(current_capacity), Operand(constant_key));
5044 __ B(le, deferred->entry());
5045 } else if (current_capacity->IsConstantOperand()) {
5046 int32_t constant_capacity =
5047 ToInteger32(LConstantOperand::cast(current_capacity));
5048 __ Cmp(ToRegister(key), Operand(constant_capacity));
5049 __ B(ge, deferred->entry());
5050 } else {
5051 __ Cmp(ToRegister(key), ToRegister(current_capacity));
5052 __ B(ge, deferred->entry());
5053 }
5054
5055 __ Mov(result, ToRegister(instr->elements()));
5056
5057 __ Bind(deferred->exit());
5058}
5059
5060
5061void LCodeGen::DoDeferredMaybeGrowElements(LMaybeGrowElements* instr) {
5062 // TODO(3095996): Get rid of this. For now, we need to make the
5063 // result register contain a valid pointer because it is already
5064 // contained in the register pointer map.
5065 Register result = x0;
5066 __ Mov(result, 0);
5067
5068 // We have to call a stub.
5069 {
5070 PushSafepointRegistersScope scope(this);
5071 __ Move(result, ToRegister(instr->object()));
5072
5073 LOperand* key = instr->key();
5074 if (key->IsConstantOperand()) {
5075 __ Mov(x3, Operand(ToSmi(LConstantOperand::cast(key))));
5076 } else {
5077 __ Mov(x3, ToRegister(key));
5078 __ SmiTag(x3);
5079 }
5080
5081 GrowArrayElementsStub stub(isolate(), instr->hydrogen()->is_js_array(),
5082 instr->hydrogen()->kind());
5083 __ CallStub(&stub);
5084 RecordSafepointWithLazyDeopt(
5085 instr, RECORD_SAFEPOINT_WITH_REGISTERS_AND_NO_ARGUMENTS);
5086 __ StoreToSafepointRegisterSlot(result, result);
5087 }
5088
5089 // Deopt on smi, which means the elements array changed to dictionary mode.
5090 DeoptimizeIfSmi(result, instr, Deoptimizer::kSmi);
5091}
5092
5093
5094void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
5095 Representation representation = instr->representation();
5096
5097 Register object = ToRegister(instr->object());
5098 HObjectAccess access = instr->hydrogen()->access();
5099 int offset = access.offset();
5100
5101 if (access.IsExternalMemory()) {
5102 DCHECK(!instr->hydrogen()->has_transition());
5103 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
5104 Register value = ToRegister(instr->value());
5105 __ Store(value, MemOperand(object, offset), representation);
5106 return;
5107 }
5108
5109 __ AssertNotSmi(object);
5110
5111 if (!FLAG_unbox_double_fields && representation.IsDouble()) {
5112 DCHECK(access.IsInobject());
5113 DCHECK(!instr->hydrogen()->has_transition());
5114 DCHECK(!instr->hydrogen()->NeedsWriteBarrier());
5115 FPRegister value = ToDoubleRegister(instr->value());
5116 __ Str(value, FieldMemOperand(object, offset));
5117 return;
5118 }
5119
5120 DCHECK(!representation.IsSmi() ||
5121 !instr->value()->IsConstantOperand() ||
5122 IsInteger32Constant(LConstantOperand::cast(instr->value())));
5123
5124 if (instr->hydrogen()->has_transition()) {
5125 Handle<Map> transition = instr->hydrogen()->transition_map();
5126 AddDeprecationDependency(transition);
5127 // Store the new map value.
5128 Register new_map_value = ToRegister(instr->temp0());
5129 __ Mov(new_map_value, Operand(transition));
5130 __ Str(new_map_value, FieldMemOperand(object, HeapObject::kMapOffset));
5131 if (instr->hydrogen()->NeedsWriteBarrierForMap()) {
5132 // Update the write barrier for the map field.
5133 __ RecordWriteForMap(object,
5134 new_map_value,
5135 ToRegister(instr->temp1()),
5136 GetLinkRegisterState(),
5137 kSaveFPRegs);
5138 }
5139 }
5140
5141 // Do the store.
5142 Register destination;
5143 if (access.IsInobject()) {
5144 destination = object;
5145 } else {
5146 Register temp0 = ToRegister(instr->temp0());
5147 __ Ldr(temp0, FieldMemOperand(object, JSObject::kPropertiesOffset));
5148 destination = temp0;
5149 }
5150
5151 if (FLAG_unbox_double_fields && representation.IsDouble()) {
5152 DCHECK(access.IsInobject());
5153 FPRegister value = ToDoubleRegister(instr->value());
5154 __ Str(value, FieldMemOperand(object, offset));
5155 } else if (representation.IsSmi() &&
5156 instr->hydrogen()->value()->representation().IsInteger32()) {
5157 DCHECK(instr->hydrogen()->store_mode() == STORE_TO_INITIALIZED_ENTRY);
5158#ifdef DEBUG
5159 Register temp0 = ToRegister(instr->temp0());
5160 __ Ldr(temp0, FieldMemOperand(destination, offset));
5161 __ AssertSmi(temp0);
5162 // If destination aliased temp0, restore it to the address calculated
5163 // earlier.
5164 if (destination.Is(temp0)) {
5165 DCHECK(!access.IsInobject());
5166 __ Ldr(destination, FieldMemOperand(object, JSObject::kPropertiesOffset));
5167 }
5168#endif
5169 STATIC_ASSERT(static_cast<unsigned>(kSmiValueSize) == kWRegSizeInBits);
5170 STATIC_ASSERT(kSmiTag == 0);
5171 Register value = ToRegister(instr->value());
5172 __ Store(value, UntagSmiFieldMemOperand(destination, offset),
5173 Representation::Integer32());
5174 } else {
5175 Register value = ToRegister(instr->value());
5176 __ Store(value, FieldMemOperand(destination, offset), representation);
5177 }
5178 if (instr->hydrogen()->NeedsWriteBarrier()) {
5179 Register value = ToRegister(instr->value());
5180 __ RecordWriteField(destination,
5181 offset,
5182 value, // Clobbered.
5183 ToRegister(instr->temp1()), // Clobbered.
5184 GetLinkRegisterState(),
5185 kSaveFPRegs,
5186 EMIT_REMEMBERED_SET,
5187 instr->hydrogen()->SmiCheckForWriteBarrier(),
5188 instr->hydrogen()->PointersToHereCheckForValue());
5189 }
5190}
5191
5192
5193void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
5194 DCHECK(ToRegister(instr->context()).is(cp));
5195 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
5196 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
5197
5198 if (instr->hydrogen()->HasVectorAndSlot()) {
5199 EmitVectorStoreICRegisters<LStoreNamedGeneric>(instr);
5200 }
5201
5202 __ Mov(StoreDescriptor::NameRegister(), Operand(instr->name()));
5203 Handle<Code> ic = CodeFactory::StoreICInOptimizedCode(
5204 isolate(), instr->language_mode(),
5205 instr->hydrogen()->initialization_state()).code();
5206 CallCode(ic, RelocInfo::CODE_TARGET, instr);
5207}
5208
5209
5210void LCodeGen::DoStringAdd(LStringAdd* instr) {
5211 DCHECK(ToRegister(instr->context()).is(cp));
5212 DCHECK(ToRegister(instr->left()).Is(x1));
5213 DCHECK(ToRegister(instr->right()).Is(x0));
5214 StringAddStub stub(isolate(),
5215 instr->hydrogen()->flags(),
5216 instr->hydrogen()->pretenure_flag());
5217 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5218}
5219
5220
5221void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
5222 class DeferredStringCharCodeAt: public LDeferredCode {
5223 public:
5224 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
5225 : LDeferredCode(codegen), instr_(instr) { }
5226 virtual void Generate() { codegen()->DoDeferredStringCharCodeAt(instr_); }
5227 virtual LInstruction* instr() { return instr_; }
5228 private:
5229 LStringCharCodeAt* instr_;
5230 };
5231
5232 DeferredStringCharCodeAt* deferred =
5233 new(zone()) DeferredStringCharCodeAt(this, instr);
5234
5235 StringCharLoadGenerator::Generate(masm(),
5236 ToRegister(instr->string()),
5237 ToRegister32(instr->index()),
5238 ToRegister(instr->result()),
5239 deferred->entry());
5240 __ Bind(deferred->exit());
5241}
5242
5243
5244void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
5245 Register string = ToRegister(instr->string());
5246 Register result = ToRegister(instr->result());
5247
5248 // TODO(3095996): Get rid of this. For now, we need to make the
5249 // result register contain a valid pointer because it is already
5250 // contained in the register pointer map.
5251 __ Mov(result, 0);
5252
5253 PushSafepointRegistersScope scope(this);
5254 __ Push(string);
5255 // Push the index as a smi. This is safe because of the checks in
5256 // DoStringCharCodeAt above.
5257 Register index = ToRegister(instr->index());
5258 __ SmiTagAndPush(index);
5259
5260 CallRuntimeFromDeferred(Runtime::kStringCharCodeAtRT, 2, instr,
5261 instr->context());
5262 __ AssertSmi(x0);
5263 __ SmiUntag(x0);
5264 __ StoreToSafepointRegisterSlot(x0, result);
5265}
5266
5267
5268void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
5269 class DeferredStringCharFromCode: public LDeferredCode {
5270 public:
5271 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
5272 : LDeferredCode(codegen), instr_(instr) { }
5273 virtual void Generate() { codegen()->DoDeferredStringCharFromCode(instr_); }
5274 virtual LInstruction* instr() { return instr_; }
5275 private:
5276 LStringCharFromCode* instr_;
5277 };
5278
5279 DeferredStringCharFromCode* deferred =
5280 new(zone()) DeferredStringCharFromCode(this, instr);
5281
5282 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
5283 Register char_code = ToRegister32(instr->char_code());
5284 Register result = ToRegister(instr->result());
5285
5286 __ Cmp(char_code, String::kMaxOneByteCharCode);
5287 __ B(hi, deferred->entry());
5288 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
5289 __ Add(result, result, FixedArray::kHeaderSize - kHeapObjectTag);
5290 __ Ldr(result, MemOperand(result, char_code, SXTW, kPointerSizeLog2));
5291 __ CompareRoot(result, Heap::kUndefinedValueRootIndex);
5292 __ B(eq, deferred->entry());
5293 __ Bind(deferred->exit());
5294}
5295
5296
5297void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
5298 Register char_code = ToRegister(instr->char_code());
5299 Register result = ToRegister(instr->result());
5300
5301 // TODO(3095996): Get rid of this. For now, we need to make the
5302 // result register contain a valid pointer because it is already
5303 // contained in the register pointer map.
5304 __ Mov(result, 0);
5305
5306 PushSafepointRegistersScope scope(this);
5307 __ SmiTagAndPush(char_code);
5308 CallRuntimeFromDeferred(Runtime::kStringCharFromCode, 1, instr,
5309 instr->context());
5310 __ StoreToSafepointRegisterSlot(x0, result);
5311}
5312
5313
5314void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
5315 DCHECK(ToRegister(instr->context()).is(cp));
5316 DCHECK(ToRegister(instr->left()).is(x1));
5317 DCHECK(ToRegister(instr->right()).is(x0));
5318
Ben Murdochda12d292016-06-02 14:46:10 +01005319 Handle<Code> code = CodeFactory::StringCompare(isolate(), instr->op()).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005320 CallCode(code, RelocInfo::CODE_TARGET, instr);
Ben Murdochda12d292016-06-02 14:46:10 +01005321 __ CompareRoot(x0, Heap::kTrueValueRootIndex);
5322 EmitBranch(instr, eq);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005323}
5324
5325
5326void LCodeGen::DoSubI(LSubI* instr) {
5327 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
5328 Register result = ToRegister32(instr->result());
5329 Register left = ToRegister32(instr->left());
5330 Operand right = ToShiftedRightOperand32(instr->right(), instr);
5331
5332 if (can_overflow) {
5333 __ Subs(result, left, right);
5334 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
5335 } else {
5336 __ Sub(result, left, right);
5337 }
5338}
5339
5340
5341void LCodeGen::DoSubS(LSubS* instr) {
5342 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
5343 Register result = ToRegister(instr->result());
5344 Register left = ToRegister(instr->left());
5345 Operand right = ToOperand(instr->right());
5346 if (can_overflow) {
5347 __ Subs(result, left, right);
5348 DeoptimizeIf(vs, instr, Deoptimizer::kOverflow);
5349 } else {
5350 __ Sub(result, left, right);
5351 }
5352}
5353
5354
5355void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr,
5356 LOperand* value,
5357 LOperand* temp1,
5358 LOperand* temp2) {
5359 Register input = ToRegister(value);
5360 Register scratch1 = ToRegister(temp1);
5361 DoubleRegister dbl_scratch1 = double_scratch();
5362
5363 Label done;
5364
5365 if (instr->truncating()) {
5366 Register output = ToRegister(instr->result());
5367 Label check_bools;
5368
5369 // If it's not a heap number, jump to undefined check.
5370 __ JumpIfNotHeapNumber(input, &check_bools);
5371
5372 // A heap number: load value and convert to int32 using truncating function.
5373 __ TruncateHeapNumberToI(output, input);
5374 __ B(&done);
5375
5376 __ Bind(&check_bools);
5377
5378 Register true_root = output;
5379 Register false_root = scratch1;
5380 __ LoadTrueFalseRoots(true_root, false_root);
5381 __ Cmp(input, true_root);
5382 __ Cset(output, eq);
5383 __ Ccmp(input, false_root, ZFlag, ne);
5384 __ B(eq, &done);
5385
5386 // Output contains zero, undefined is converted to zero for truncating
5387 // conversions.
5388 DeoptimizeIfNotRoot(input, Heap::kUndefinedValueRootIndex, instr,
5389 Deoptimizer::kNotAHeapNumberUndefinedBoolean);
5390 } else {
5391 Register output = ToRegister32(instr->result());
5392 DoubleRegister dbl_scratch2 = ToDoubleRegister(temp2);
5393
5394 DeoptimizeIfNotHeapNumber(input, instr);
5395
5396 // A heap number: load value and convert to int32 using non-truncating
5397 // function. If the result is out of range, branch to deoptimize.
5398 __ Ldr(dbl_scratch1, FieldMemOperand(input, HeapNumber::kValueOffset));
5399 __ TryRepresentDoubleAsInt32(output, dbl_scratch1, dbl_scratch2);
5400 DeoptimizeIf(ne, instr, Deoptimizer::kLostPrecisionOrNaN);
5401
5402 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
5403 __ Cmp(output, 0);
5404 __ B(ne, &done);
5405 __ Fmov(scratch1, dbl_scratch1);
5406 DeoptimizeIfNegative(scratch1, instr, Deoptimizer::kMinusZero);
5407 }
5408 }
5409 __ Bind(&done);
5410}
5411
5412
5413void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
5414 class DeferredTaggedToI: public LDeferredCode {
5415 public:
5416 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
5417 : LDeferredCode(codegen), instr_(instr) { }
5418 virtual void Generate() {
5419 codegen()->DoDeferredTaggedToI(instr_, instr_->value(), instr_->temp1(),
5420 instr_->temp2());
5421 }
5422
5423 virtual LInstruction* instr() { return instr_; }
5424 private:
5425 LTaggedToI* instr_;
5426 };
5427
5428 Register input = ToRegister(instr->value());
5429 Register output = ToRegister(instr->result());
5430
5431 if (instr->hydrogen()->value()->representation().IsSmi()) {
5432 __ SmiUntag(output, input);
5433 } else {
5434 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
5435
5436 __ JumpIfNotSmi(input, deferred->entry());
5437 __ SmiUntag(output, input);
5438 __ Bind(deferred->exit());
5439 }
5440}
5441
5442
5443void LCodeGen::DoThisFunction(LThisFunction* instr) {
5444 Register result = ToRegister(instr->result());
5445 __ Ldr(result, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5446}
5447
5448
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005449void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
5450 Register object = ToRegister(instr->object());
5451
5452 Handle<Map> from_map = instr->original_map();
5453 Handle<Map> to_map = instr->transitioned_map();
5454 ElementsKind from_kind = instr->from_kind();
5455 ElementsKind to_kind = instr->to_kind();
5456
5457 Label not_applicable;
5458
5459 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
5460 Register temp1 = ToRegister(instr->temp1());
5461 Register new_map = ToRegister(instr->temp2());
5462 __ CheckMap(object, temp1, from_map, &not_applicable, DONT_DO_SMI_CHECK);
5463 __ Mov(new_map, Operand(to_map));
5464 __ Str(new_map, FieldMemOperand(object, HeapObject::kMapOffset));
5465 // Write barrier.
5466 __ RecordWriteForMap(object, new_map, temp1, GetLinkRegisterState(),
5467 kDontSaveFPRegs);
5468 } else {
5469 {
5470 UseScratchRegisterScope temps(masm());
5471 // Use the temp register only in a restricted scope - the codegen checks
5472 // that we do not use any register across a call.
5473 __ CheckMap(object, temps.AcquireX(), from_map, &not_applicable,
5474 DONT_DO_SMI_CHECK);
5475 }
5476 DCHECK(object.is(x0));
5477 DCHECK(ToRegister(instr->context()).is(cp));
5478 PushSafepointRegistersScope scope(this);
5479 __ Mov(x1, Operand(to_map));
5480 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
5481 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
5482 __ CallStub(&stub);
5483 RecordSafepointWithRegisters(
5484 instr->pointer_map(), 0, Safepoint::kLazyDeopt);
5485 }
5486 __ Bind(&not_applicable);
5487}
5488
5489
5490void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
5491 Register object = ToRegister(instr->object());
5492 Register temp1 = ToRegister(instr->temp1());
5493 Register temp2 = ToRegister(instr->temp2());
5494
5495 Label no_memento_found;
5496 __ TestJSArrayForAllocationMemento(object, temp1, temp2, &no_memento_found);
5497 DeoptimizeIf(eq, instr, Deoptimizer::kMementoFound);
5498 __ Bind(&no_memento_found);
5499}
5500
5501
5502void LCodeGen::DoTruncateDoubleToIntOrSmi(LTruncateDoubleToIntOrSmi* instr) {
5503 DoubleRegister input = ToDoubleRegister(instr->value());
5504 Register result = ToRegister(instr->result());
5505 __ TruncateDoubleToI(result, input);
5506 if (instr->tag_result()) {
5507 __ SmiTag(result, result);
5508 }
5509}
5510
5511
5512void LCodeGen::DoTypeof(LTypeof* instr) {
5513 DCHECK(ToRegister(instr->value()).is(x3));
5514 DCHECK(ToRegister(instr->result()).is(x0));
5515 Label end, do_call;
5516 Register value_register = ToRegister(instr->value());
5517 __ JumpIfNotSmi(value_register, &do_call);
5518 __ Mov(x0, Immediate(isolate()->factory()->number_string()));
5519 __ B(&end);
5520 __ Bind(&do_call);
5521 TypeofStub stub(isolate());
5522 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5523 __ Bind(&end);
5524}
5525
5526
5527void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5528 Handle<String> type_name = instr->type_literal();
5529 Label* true_label = instr->TrueLabel(chunk_);
5530 Label* false_label = instr->FalseLabel(chunk_);
5531 Register value = ToRegister(instr->value());
5532
5533 Factory* factory = isolate()->factory();
5534 if (String::Equals(type_name, factory->number_string())) {
5535 __ JumpIfSmi(value, true_label);
5536
5537 int true_block = instr->TrueDestination(chunk_);
5538 int false_block = instr->FalseDestination(chunk_);
5539 int next_block = GetNextEmittedBlock();
5540
5541 if (true_block == false_block) {
5542 EmitGoto(true_block);
5543 } else if (true_block == next_block) {
5544 __ JumpIfNotHeapNumber(value, chunk_->GetAssemblyLabel(false_block));
5545 } else {
5546 __ JumpIfHeapNumber(value, chunk_->GetAssemblyLabel(true_block));
5547 if (false_block != next_block) {
5548 __ B(chunk_->GetAssemblyLabel(false_block));
5549 }
5550 }
5551
5552 } else if (String::Equals(type_name, factory->string_string())) {
5553 DCHECK((instr->temp1() != NULL) && (instr->temp2() != NULL));
5554 Register map = ToRegister(instr->temp1());
5555 Register scratch = ToRegister(instr->temp2());
5556
5557 __ JumpIfSmi(value, false_label);
5558 __ CompareObjectType(value, map, scratch, FIRST_NONSTRING_TYPE);
5559 EmitBranch(instr, lt);
5560
5561 } else if (String::Equals(type_name, factory->symbol_string())) {
5562 DCHECK((instr->temp1() != NULL) && (instr->temp2() != NULL));
5563 Register map = ToRegister(instr->temp1());
5564 Register scratch = ToRegister(instr->temp2());
5565
5566 __ JumpIfSmi(value, false_label);
5567 __ CompareObjectType(value, map, scratch, SYMBOL_TYPE);
5568 EmitBranch(instr, eq);
5569
5570 } else if (String::Equals(type_name, factory->boolean_string())) {
5571 __ JumpIfRoot(value, Heap::kTrueValueRootIndex, true_label);
5572 __ CompareRoot(value, Heap::kFalseValueRootIndex);
5573 EmitBranch(instr, eq);
5574
5575 } else if (String::Equals(type_name, factory->undefined_string())) {
5576 DCHECK(instr->temp1() != NULL);
5577 Register scratch = ToRegister(instr->temp1());
5578
Ben Murdoch097c5b22016-05-18 11:27:45 +01005579 __ JumpIfRoot(value, Heap::kNullValueRootIndex, false_label);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005580 __ JumpIfSmi(value, false_label);
5581 // Check for undetectable objects and jump to the true branch in this case.
5582 __ Ldr(scratch, FieldMemOperand(value, HeapObject::kMapOffset));
5583 __ Ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5584 EmitTestAndBranch(instr, ne, scratch, 1 << Map::kIsUndetectable);
5585
5586 } else if (String::Equals(type_name, factory->function_string())) {
5587 DCHECK(instr->temp1() != NULL);
5588 Register scratch = ToRegister(instr->temp1());
5589
5590 __ JumpIfSmi(value, false_label);
5591 __ Ldr(scratch, FieldMemOperand(value, HeapObject::kMapOffset));
5592 __ Ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5593 __ And(scratch, scratch,
5594 (1 << Map::kIsCallable) | (1 << Map::kIsUndetectable));
5595 EmitCompareAndBranch(instr, eq, scratch, 1 << Map::kIsCallable);
5596
5597 } else if (String::Equals(type_name, factory->object_string())) {
5598 DCHECK((instr->temp1() != NULL) && (instr->temp2() != NULL));
5599 Register map = ToRegister(instr->temp1());
5600 Register scratch = ToRegister(instr->temp2());
5601
5602 __ JumpIfSmi(value, false_label);
5603 __ JumpIfRoot(value, Heap::kNullValueRootIndex, true_label);
5604 STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
5605 __ JumpIfObjectType(value, map, scratch, FIRST_JS_RECEIVER_TYPE,
5606 false_label, lt);
5607 // Check for callable or undetectable objects => false.
5608 __ Ldrb(scratch, FieldMemOperand(map, Map::kBitFieldOffset));
5609 EmitTestAndBranch(instr, eq, scratch,
5610 (1 << Map::kIsCallable) | (1 << Map::kIsUndetectable));
5611
5612// clang-format off
5613#define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
5614 } else if (String::Equals(type_name, factory->type##_string())) { \
5615 DCHECK((instr->temp1() != NULL) && (instr->temp2() != NULL)); \
5616 Register map = ToRegister(instr->temp1()); \
5617 \
5618 __ JumpIfSmi(value, false_label); \
5619 __ Ldr(map, FieldMemOperand(value, HeapObject::kMapOffset)); \
5620 __ CompareRoot(map, Heap::k##Type##MapRootIndex); \
5621 EmitBranch(instr, eq);
5622 SIMD128_TYPES(SIMD128_TYPE)
5623#undef SIMD128_TYPE
5624 // clang-format on
5625
5626 } else {
5627 __ B(false_label);
5628 }
5629}
5630
5631
5632void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
5633 __ Ucvtf(ToDoubleRegister(instr->result()), ToRegister32(instr->value()));
5634}
5635
5636
5637void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5638 Register object = ToRegister(instr->value());
5639 Register map = ToRegister(instr->map());
5640 Register temp = ToRegister(instr->temp());
5641 __ Ldr(temp, FieldMemOperand(object, HeapObject::kMapOffset));
5642 __ Cmp(map, temp);
5643 DeoptimizeIf(ne, instr, Deoptimizer::kWrongMap);
5644}
5645
5646
5647void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
5648 Register receiver = ToRegister(instr->receiver());
5649 Register function = ToRegister(instr->function());
5650 Register result = ToRegister(instr->result());
5651
5652 // If the receiver is null or undefined, we have to pass the global object as
5653 // a receiver to normal functions. Values have to be passed unchanged to
5654 // builtins and strict-mode functions.
5655 Label global_object, done, copy_receiver;
5656
5657 if (!instr->hydrogen()->known_function()) {
5658 __ Ldr(result, FieldMemOperand(function,
5659 JSFunction::kSharedFunctionInfoOffset));
5660
5661 // CompilerHints is an int32 field. See objects.h.
5662 __ Ldr(result.W(),
5663 FieldMemOperand(result, SharedFunctionInfo::kCompilerHintsOffset));
5664
5665 // Do not transform the receiver to object for strict mode functions.
5666 __ Tbnz(result, SharedFunctionInfo::kStrictModeFunction, &copy_receiver);
5667
5668 // Do not transform the receiver to object for builtins.
5669 __ Tbnz(result, SharedFunctionInfo::kNative, &copy_receiver);
5670 }
5671
5672 // Normal function. Replace undefined or null with global receiver.
5673 __ JumpIfRoot(receiver, Heap::kNullValueRootIndex, &global_object);
5674 __ JumpIfRoot(receiver, Heap::kUndefinedValueRootIndex, &global_object);
5675
5676 // Deoptimize if the receiver is not a JS object.
5677 DeoptimizeIfSmi(receiver, instr, Deoptimizer::kSmi);
5678 __ CompareObjectType(receiver, result, result, FIRST_JS_RECEIVER_TYPE);
5679 __ B(ge, &copy_receiver);
5680 Deoptimize(instr, Deoptimizer::kNotAJavaScriptObject);
5681
5682 __ Bind(&global_object);
5683 __ Ldr(result, FieldMemOperand(function, JSFunction::kContextOffset));
5684 __ Ldr(result, ContextMemOperand(result, Context::NATIVE_CONTEXT_INDEX));
5685 __ Ldr(result, ContextMemOperand(result, Context::GLOBAL_PROXY_INDEX));
5686 __ B(&done);
5687
5688 __ Bind(&copy_receiver);
5689 __ Mov(result, receiver);
5690 __ Bind(&done);
5691}
5692
5693
5694void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5695 Register result,
5696 Register object,
5697 Register index) {
5698 PushSafepointRegistersScope scope(this);
5699 __ Push(object);
5700 __ Push(index);
5701 __ Mov(cp, 0);
5702 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
5703 RecordSafepointWithRegisters(
5704 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
5705 __ StoreToSafepointRegisterSlot(x0, result);
5706}
5707
5708
5709void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
5710 class DeferredLoadMutableDouble final : public LDeferredCode {
5711 public:
5712 DeferredLoadMutableDouble(LCodeGen* codegen,
5713 LLoadFieldByIndex* instr,
5714 Register result,
5715 Register object,
5716 Register index)
5717 : LDeferredCode(codegen),
5718 instr_(instr),
5719 result_(result),
5720 object_(object),
5721 index_(index) {
5722 }
5723 void Generate() override {
5724 codegen()->DoDeferredLoadMutableDouble(instr_, result_, object_, index_);
5725 }
5726 LInstruction* instr() override { return instr_; }
5727
5728 private:
5729 LLoadFieldByIndex* instr_;
5730 Register result_;
5731 Register object_;
5732 Register index_;
5733 };
5734 Register object = ToRegister(instr->object());
5735 Register index = ToRegister(instr->index());
5736 Register result = ToRegister(instr->result());
5737
5738 __ AssertSmi(index);
5739
5740 DeferredLoadMutableDouble* deferred;
5741 deferred = new(zone()) DeferredLoadMutableDouble(
5742 this, instr, result, object, index);
5743
5744 Label out_of_object, done;
5745
5746 __ TestAndBranchIfAnySet(
5747 index, reinterpret_cast<uint64_t>(Smi::FromInt(1)), deferred->entry());
5748 __ Mov(index, Operand(index, ASR, 1));
5749
5750 __ Cmp(index, Smi::FromInt(0));
5751 __ B(lt, &out_of_object);
5752
5753 STATIC_ASSERT(kPointerSizeLog2 > kSmiTagSize);
5754 __ Add(result, object, Operand::UntagSmiAndScale(index, kPointerSizeLog2));
5755 __ Ldr(result, FieldMemOperand(result, JSObject::kHeaderSize));
5756
5757 __ B(&done);
5758
5759 __ Bind(&out_of_object);
5760 __ Ldr(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
5761 // Index is equal to negated out of object property index plus 1.
5762 __ Sub(result, result, Operand::UntagSmiAndScale(index, kPointerSizeLog2));
5763 __ Ldr(result, FieldMemOperand(result,
5764 FixedArray::kHeaderSize - kPointerSize));
5765 __ Bind(deferred->exit());
5766 __ Bind(&done);
5767}
5768
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005769} // namespace internal
5770} // namespace v8