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

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

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

Powered by Google App Engine
This is Rietveld 408576698