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