Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: src/arm/lithium-codegen-arm.cc

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

Powered by Google App Engine
This is Rietveld 408576698