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