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