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