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