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