deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / arm64 / full-codegen-arm64.cc
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/v8.h"
6
7 #if V8_TARGET_ARCH_ARM64
8
9 #include "src/code-factory.h"
10 #include "src/code-stubs.h"
11 #include "src/codegen.h"
12 #include "src/compiler.h"
13 #include "src/debug.h"
14 #include "src/full-codegen.h"
15 #include "src/ic/ic.h"
16 #include "src/isolate-inl.h"
17 #include "src/parser.h"
18 #include "src/scopes.h"
19
20 #include "src/arm64/code-stubs-arm64.h"
21 #include "src/arm64/macro-assembler-arm64.h"
22
23 namespace v8 {
24 namespace internal {
25
26 #define __ ACCESS_MASM(masm_)
27
28 class JumpPatchSite BASE_EMBEDDED {
29  public:
30   explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm), reg_(NoReg) {
31 #ifdef DEBUG
32     info_emitted_ = false;
33 #endif
34   }
35
36   ~JumpPatchSite() {
37     if (patch_site_.is_bound()) {
38       DCHECK(info_emitted_);
39     } else {
40       DCHECK(reg_.IsNone());
41     }
42   }
43
44   void EmitJumpIfNotSmi(Register reg, Label* target) {
45     // This code will be patched by PatchInlinedSmiCode, in ic-arm64.cc.
46     InstructionAccurateScope scope(masm_, 1);
47     DCHECK(!info_emitted_);
48     DCHECK(reg.Is64Bits());
49     DCHECK(!reg.Is(csp));
50     reg_ = reg;
51     __ bind(&patch_site_);
52     __ tbz(xzr, 0, target);   // Always taken before patched.
53   }
54
55   void EmitJumpIfSmi(Register reg, Label* target) {
56     // This code will be patched by PatchInlinedSmiCode, in ic-arm64.cc.
57     InstructionAccurateScope scope(masm_, 1);
58     DCHECK(!info_emitted_);
59     DCHECK(reg.Is64Bits());
60     DCHECK(!reg.Is(csp));
61     reg_ = reg;
62     __ bind(&patch_site_);
63     __ tbnz(xzr, 0, target);  // Never taken before patched.
64   }
65
66   void EmitJumpIfEitherNotSmi(Register reg1, Register reg2, Label* target) {
67     UseScratchRegisterScope temps(masm_);
68     Register temp = temps.AcquireX();
69     __ Orr(temp, reg1, reg2);
70     EmitJumpIfNotSmi(temp, target);
71   }
72
73   void EmitPatchInfo() {
74     Assembler::BlockPoolsScope scope(masm_);
75     InlineSmiCheckInfo::Emit(masm_, reg_, &patch_site_);
76 #ifdef DEBUG
77     info_emitted_ = true;
78 #endif
79   }
80
81  private:
82   MacroAssembler* masm_;
83   Label patch_site_;
84   Register reg_;
85 #ifdef DEBUG
86   bool info_emitted_;
87 #endif
88 };
89
90
91 // Generate code for a JS function. On entry to the function the receiver
92 // and arguments have been pushed on the stack left to right. The actual
93 // argument count matches the formal parameter count expected by the
94 // function.
95 //
96 // The live registers are:
97 //   - x1: the JS function object being called (i.e. ourselves).
98 //   - cp: our context.
99 //   - fp: our caller's frame pointer.
100 //   - jssp: stack pointer.
101 //   - lr: return address.
102 //
103 // The function builds a JS frame. See JavaScriptFrameConstants in
104 // frames-arm.h for its layout.
105 void FullCodeGenerator::Generate() {
106   CompilationInfo* info = info_;
107   handler_table_ =
108       Handle<HandlerTable>::cast(isolate()->factory()->NewFixedArray(
109           HandlerTable::LengthForRange(function()->handler_count()), TENURED));
110
111   profiling_counter_ = isolate()->factory()->NewCell(
112       Handle<Smi>(Smi::FromInt(FLAG_interrupt_budget), isolate()));
113   SetFunctionPosition(function());
114   Comment cmnt(masm_, "[ Function compiled by full code generator");
115
116   ProfileEntryHookStub::MaybeCallEntryHook(masm_);
117
118 #ifdef DEBUG
119   if (strlen(FLAG_stop_at) > 0 &&
120       info->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
121     __ Debug("stop-at", __LINE__, BREAK);
122   }
123 #endif
124
125   // Sloppy mode functions and builtins need to replace the receiver with the
126   // global proxy when called as functions (without an explicit receiver
127   // object).
128   if (is_sloppy(info->language_mode()) && !info->is_native()) {
129     Label ok;
130     int receiver_offset = info->scope()->num_parameters() * kXRegSize;
131     __ Peek(x10, receiver_offset);
132     __ JumpIfNotRoot(x10, Heap::kUndefinedValueRootIndex, &ok);
133
134     __ Ldr(x10, GlobalObjectMemOperand());
135     __ Ldr(x10, FieldMemOperand(x10, GlobalObject::kGlobalProxyOffset));
136     __ Poke(x10, receiver_offset);
137
138     __ Bind(&ok);
139   }
140
141
142   // Open a frame scope to indicate that there is a frame on the stack.
143   // The MANUAL indicates that the scope shouldn't actually generate code
144   // to set up the frame because we do it manually below.
145   FrameScope frame_scope(masm_, StackFrame::MANUAL);
146
147   // This call emits the following sequence in a way that can be patched for
148   // code ageing support:
149   //  Push(lr, fp, cp, x1);
150   //  Add(fp, jssp, 2 * kPointerSize);
151   info->set_prologue_offset(masm_->pc_offset());
152   __ Prologue(info->IsCodePreAgingActive());
153   info->AddNoFrameRange(0, masm_->pc_offset());
154
155   // Reserve space on the stack for locals.
156   { Comment cmnt(masm_, "[ Allocate locals");
157     int locals_count = info->scope()->num_stack_slots();
158     // Generators allocate locals, if any, in context slots.
159     DCHECK(!IsGeneratorFunction(info->function()->kind()) || locals_count == 0);
160
161     if (locals_count > 0) {
162       if (locals_count >= 128) {
163         Label ok;
164         DCHECK(jssp.Is(__ StackPointer()));
165         __ Sub(x10, jssp, locals_count * kPointerSize);
166         __ CompareRoot(x10, Heap::kRealStackLimitRootIndex);
167         __ B(hs, &ok);
168         __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
169         __ Bind(&ok);
170       }
171       __ LoadRoot(x10, Heap::kUndefinedValueRootIndex);
172       if (FLAG_optimize_for_size) {
173         __ PushMultipleTimes(x10 , locals_count);
174       } else {
175         const int kMaxPushes = 32;
176         if (locals_count >= kMaxPushes) {
177           int loop_iterations = locals_count / kMaxPushes;
178           __ Mov(x3, loop_iterations);
179           Label loop_header;
180           __ Bind(&loop_header);
181           // Do pushes.
182           __ PushMultipleTimes(x10 , kMaxPushes);
183           __ Subs(x3, x3, 1);
184           __ B(ne, &loop_header);
185         }
186         int remaining = locals_count % kMaxPushes;
187         // Emit the remaining pushes.
188         __ PushMultipleTimes(x10 , remaining);
189       }
190     }
191   }
192
193   bool function_in_register_x1 = true;
194
195   int heap_slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
196   if (heap_slots > 0) {
197     // Argument to NewContext is the function, which is still in x1.
198     Comment cmnt(masm_, "[ Allocate context");
199     bool need_write_barrier = true;
200     if (info->scope()->is_script_scope()) {
201       __ Mov(x10, Operand(info->scope()->GetScopeInfo(info->isolate())));
202       __ Push(x1, x10);
203       __ CallRuntime(Runtime::kNewScriptContext, 2);
204     } else if (heap_slots <= FastNewContextStub::kMaximumSlots) {
205       FastNewContextStub stub(isolate(), heap_slots);
206       __ CallStub(&stub);
207       // Result of FastNewContextStub is always in new space.
208       need_write_barrier = false;
209     } else {
210       __ Push(x1);
211       __ CallRuntime(Runtime::kNewFunctionContext, 1);
212     }
213     function_in_register_x1 = false;
214     // Context is returned in x0.  It replaces the context passed to us.
215     // It's saved in the stack and kept live in cp.
216     __ Mov(cp, x0);
217     __ Str(x0, MemOperand(fp, StandardFrameConstants::kContextOffset));
218     // Copy any necessary parameters into the context.
219     int num_parameters = info->scope()->num_parameters();
220     for (int i = 0; i < num_parameters; i++) {
221       Variable* var = scope()->parameter(i);
222       if (var->IsContextSlot()) {
223         int parameter_offset = StandardFrameConstants::kCallerSPOffset +
224             (num_parameters - 1 - i) * kPointerSize;
225         // Load parameter from stack.
226         __ Ldr(x10, MemOperand(fp, parameter_offset));
227         // Store it in the context.
228         MemOperand target = ContextMemOperand(cp, var->index());
229         __ Str(x10, target);
230
231         // Update the write barrier.
232         if (need_write_barrier) {
233           __ RecordWriteContextSlot(
234               cp, target.offset(), x10, x11, kLRHasBeenSaved, kDontSaveFPRegs);
235         } else if (FLAG_debug_code) {
236           Label done;
237           __ JumpIfInNewSpace(cp, &done);
238           __ Abort(kExpectedNewSpaceObject);
239           __ bind(&done);
240         }
241       }
242     }
243   }
244
245   ArgumentsAccessStub::HasNewTarget has_new_target =
246       IsSubclassConstructor(info->function()->kind())
247           ? ArgumentsAccessStub::HAS_NEW_TARGET
248           : ArgumentsAccessStub::NO_NEW_TARGET;
249
250   // Possibly allocate RestParameters
251   int rest_index;
252   Variable* rest_param = scope()->rest_parameter(&rest_index);
253   if (rest_param) {
254     Comment cmnt(masm_, "[ Allocate rest parameter array");
255
256     int num_parameters = info->scope()->num_parameters();
257     int offset = num_parameters * kPointerSize;
258     if (has_new_target == ArgumentsAccessStub::HAS_NEW_TARGET) {
259       --num_parameters;
260       ++rest_index;
261     }
262
263     __ Add(x3, fp, StandardFrameConstants::kCallerSPOffset + offset);
264     __ Mov(x2, Smi::FromInt(num_parameters));
265     __ Mov(x1, Smi::FromInt(rest_index));
266     __ Push(x3, x2, x1);
267
268     RestParamAccessStub stub(isolate());
269     __ CallStub(&stub);
270
271     SetVar(rest_param, x0, x1, x2);
272   }
273
274   Variable* arguments = scope()->arguments();
275   if (arguments != NULL) {
276     // Function uses arguments object.
277     Comment cmnt(masm_, "[ Allocate arguments object");
278     if (!function_in_register_x1) {
279       // Load this again, if it's used by the local context below.
280       __ Ldr(x3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
281     } else {
282       __ Mov(x3, x1);
283     }
284     // Receiver is just before the parameters on the caller's stack.
285     int num_parameters = info->scope()->num_parameters();
286     int offset = num_parameters * kPointerSize;
287     __ Add(x2, fp, StandardFrameConstants::kCallerSPOffset + offset);
288     __ Mov(x1, Smi::FromInt(num_parameters));
289     __ Push(x3, x2, x1);
290
291     // Arguments to ArgumentsAccessStub:
292     //   function, receiver address, parameter count.
293     // The stub will rewrite receiver and parameter count if the previous
294     // stack frame was an arguments adapter frame.
295     ArgumentsAccessStub::Type type;
296     if (is_strict(language_mode()) || !is_simple_parameter_list()) {
297       type = ArgumentsAccessStub::NEW_STRICT;
298     } else if (function()->has_duplicate_parameters()) {
299       type = ArgumentsAccessStub::NEW_SLOPPY_SLOW;
300     } else {
301       type = ArgumentsAccessStub::NEW_SLOPPY_FAST;
302     }
303     ArgumentsAccessStub stub(isolate(), type, has_new_target);
304     __ CallStub(&stub);
305
306     SetVar(arguments, x0, x1, x2);
307   }
308
309   if (FLAG_trace) {
310     __ CallRuntime(Runtime::kTraceEnter, 0);
311   }
312
313
314   // Visit the declarations and body unless there is an illegal
315   // redeclaration.
316   if (scope()->HasIllegalRedeclaration()) {
317     Comment cmnt(masm_, "[ Declarations");
318     scope()->VisitIllegalRedeclaration(this);
319
320   } else {
321     PrepareForBailoutForId(BailoutId::FunctionEntry(), NO_REGISTERS);
322     { Comment cmnt(masm_, "[ Declarations");
323       if (scope()->is_function_scope() && scope()->function() != NULL) {
324         VariableDeclaration* function = scope()->function();
325         DCHECK(function->proxy()->var()->mode() == CONST ||
326                function->proxy()->var()->mode() == CONST_LEGACY);
327         DCHECK(function->proxy()->var()->location() != Variable::UNALLOCATED);
328         VisitVariableDeclaration(function);
329       }
330       VisitDeclarations(scope()->declarations());
331     }
332
333     {
334       Comment cmnt(masm_, "[ Stack check");
335       PrepareForBailoutForId(BailoutId::Declarations(), NO_REGISTERS);
336       Label ok;
337       DCHECK(jssp.Is(__ StackPointer()));
338       __ CompareRoot(jssp, Heap::kStackLimitRootIndex);
339       __ B(hs, &ok);
340       PredictableCodeSizeScope predictable(masm_,
341                                            Assembler::kCallSizeWithRelocation);
342       __ Call(isolate()->builtins()->StackCheck(), RelocInfo::CODE_TARGET);
343       __ Bind(&ok);
344     }
345
346     {
347       Comment cmnt(masm_, "[ Body");
348       DCHECK(loop_depth() == 0);
349       VisitStatements(function()->body());
350       DCHECK(loop_depth() == 0);
351     }
352   }
353
354   // Always emit a 'return undefined' in case control fell off the end of
355   // the body.
356   { Comment cmnt(masm_, "[ return <undefined>;");
357     __ LoadRoot(x0, Heap::kUndefinedValueRootIndex);
358   }
359   EmitReturnSequence();
360
361   // Force emission of the pools, so they don't get emitted in the middle
362   // of the back edge table.
363   masm()->CheckVeneerPool(true, false);
364   masm()->CheckConstPool(true, false);
365 }
366
367
368 void FullCodeGenerator::ClearAccumulator() {
369   __ Mov(x0, Smi::FromInt(0));
370 }
371
372
373 void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
374   __ Mov(x2, Operand(profiling_counter_));
375   __ Ldr(x3, FieldMemOperand(x2, Cell::kValueOffset));
376   __ Subs(x3, x3, Smi::FromInt(delta));
377   __ Str(x3, FieldMemOperand(x2, Cell::kValueOffset));
378 }
379
380
381 void FullCodeGenerator::EmitProfilingCounterReset() {
382   int reset_value = FLAG_interrupt_budget;
383   if (info_->is_debug()) {
384     // Detect debug break requests as soon as possible.
385     reset_value = FLAG_interrupt_budget >> 4;
386   }
387   __ Mov(x2, Operand(profiling_counter_));
388   __ Mov(x3, Smi::FromInt(reset_value));
389   __ Str(x3, FieldMemOperand(x2, Cell::kValueOffset));
390 }
391
392
393 void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
394                                                 Label* back_edge_target) {
395   DCHECK(jssp.Is(__ StackPointer()));
396   Comment cmnt(masm_, "[ Back edge bookkeeping");
397   // Block literal pools whilst emitting back edge code.
398   Assembler::BlockPoolsScope block_const_pool(masm_);
399   Label ok;
400
401   DCHECK(back_edge_target->is_bound());
402   // We want to do a round rather than a floor of distance/kCodeSizeMultiplier
403   // to reduce the absolute error due to the integer division. To do that,
404   // we add kCodeSizeMultiplier/2 to the distance (equivalent to adding 0.5 to
405   // the result).
406   int distance =
407     masm_->SizeOfCodeGeneratedSince(back_edge_target) + kCodeSizeMultiplier / 2;
408   int weight = Min(kMaxBackEdgeWeight,
409                    Max(1, distance / kCodeSizeMultiplier));
410   EmitProfilingCounterDecrement(weight);
411   __ B(pl, &ok);
412   __ Call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
413
414   // Record a mapping of this PC offset to the OSR id.  This is used to find
415   // the AST id from the unoptimized code in order to use it as a key into
416   // the deoptimization input data found in the optimized code.
417   RecordBackEdge(stmt->OsrEntryId());
418
419   EmitProfilingCounterReset();
420
421   __ Bind(&ok);
422   PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
423   // Record a mapping of the OSR id to this PC.  This is used if the OSR
424   // entry becomes the target of a bailout.  We don't expect it to be, but
425   // we want it to work if it is.
426   PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
427 }
428
429
430 void FullCodeGenerator::EmitReturnSequence() {
431   Comment cmnt(masm_, "[ Return sequence");
432
433   if (return_label_.is_bound()) {
434     __ B(&return_label_);
435
436   } else {
437     __ Bind(&return_label_);
438     if (FLAG_trace) {
439       // Push the return value on the stack as the parameter.
440       // Runtime::TraceExit returns its parameter in x0.
441       __ Push(result_register());
442       __ CallRuntime(Runtime::kTraceExit, 1);
443       DCHECK(x0.Is(result_register()));
444     }
445     // Pretend that the exit is a backwards jump to the entry.
446     int weight = 1;
447     if (info_->ShouldSelfOptimize()) {
448       weight = FLAG_interrupt_budget / FLAG_self_opt_count;
449     } else {
450       int distance = masm_->pc_offset() + kCodeSizeMultiplier / 2;
451       weight = Min(kMaxBackEdgeWeight,
452                    Max(1, distance / kCodeSizeMultiplier));
453     }
454     EmitProfilingCounterDecrement(weight);
455     Label ok;
456     __ B(pl, &ok);
457     __ Push(x0);
458     __ Call(isolate()->builtins()->InterruptCheck(),
459             RelocInfo::CODE_TARGET);
460     __ Pop(x0);
461     EmitProfilingCounterReset();
462     __ Bind(&ok);
463
464     // Make sure that the constant pool is not emitted inside of the return
465     // sequence. This sequence can get patched when the debugger is used. See
466     // debug-arm64.cc:BreakLocation::SetDebugBreakAtReturn().
467     {
468       InstructionAccurateScope scope(masm_,
469                                      Assembler::kJSReturnSequenceInstructions);
470       CodeGenerator::RecordPositions(masm_, function()->end_position() - 1);
471       __ RecordJSReturn();
472       // This code is generated using Assembler methods rather than Macro
473       // Assembler methods because it will be patched later on, and so the size
474       // of the generated code must be consistent.
475       const Register& current_sp = __ StackPointer();
476       // Nothing ensures 16 bytes alignment here.
477       DCHECK(!current_sp.Is(csp));
478       __ mov(current_sp, fp);
479       int no_frame_start = masm_->pc_offset();
480       __ ldp(fp, lr, MemOperand(current_sp, 2 * kXRegSize, PostIndex));
481       // Drop the arguments and receiver and return.
482       // TODO(all): This implementation is overkill as it supports 2**31+1
483       // arguments, consider how to improve it without creating a security
484       // hole.
485       __ ldr_pcrel(ip0, (3 * kInstructionSize) >> kLoadLiteralScaleLog2);
486       __ add(current_sp, current_sp, ip0);
487       __ ret();
488       int32_t arg_count = info_->scope()->num_parameters() + 1;
489       if (IsSubclassConstructor(info_->function()->kind())) {
490         arg_count++;
491       }
492       __ dc64(kXRegSize * arg_count);
493       info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
494     }
495   }
496 }
497
498
499 void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
500   DCHECK(var->IsStackAllocated() || var->IsContextSlot());
501 }
502
503
504 void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
505   DCHECK(var->IsStackAllocated() || var->IsContextSlot());
506   codegen()->GetVar(result_register(), var);
507 }
508
509
510 void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
511   DCHECK(var->IsStackAllocated() || var->IsContextSlot());
512   codegen()->GetVar(result_register(), var);
513   __ Push(result_register());
514 }
515
516
517 void FullCodeGenerator::TestContext::Plug(Variable* var) const {
518   DCHECK(var->IsStackAllocated() || var->IsContextSlot());
519   // For simplicity we always test the accumulator register.
520   codegen()->GetVar(result_register(), var);
521   codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
522   codegen()->DoTest(this);
523 }
524
525
526 void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
527   // Root values have no side effects.
528 }
529
530
531 void FullCodeGenerator::AccumulatorValueContext::Plug(
532     Heap::RootListIndex index) const {
533   __ LoadRoot(result_register(), index);
534 }
535
536
537 void FullCodeGenerator::StackValueContext::Plug(
538     Heap::RootListIndex index) const {
539   __ LoadRoot(result_register(), index);
540   __ Push(result_register());
541 }
542
543
544 void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
545   codegen()->PrepareForBailoutBeforeSplit(condition(), true, true_label_,
546                                           false_label_);
547   if (index == Heap::kUndefinedValueRootIndex ||
548       index == Heap::kNullValueRootIndex ||
549       index == Heap::kFalseValueRootIndex) {
550     if (false_label_ != fall_through_) __ B(false_label_);
551   } else if (index == Heap::kTrueValueRootIndex) {
552     if (true_label_ != fall_through_) __ B(true_label_);
553   } else {
554     __ LoadRoot(result_register(), index);
555     codegen()->DoTest(this);
556   }
557 }
558
559
560 void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
561 }
562
563
564 void FullCodeGenerator::AccumulatorValueContext::Plug(
565     Handle<Object> lit) const {
566   __ Mov(result_register(), Operand(lit));
567 }
568
569
570 void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
571   // Immediates cannot be pushed directly.
572   __ Mov(result_register(), Operand(lit));
573   __ Push(result_register());
574 }
575
576
577 void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
578   codegen()->PrepareForBailoutBeforeSplit(condition(),
579                                           true,
580                                           true_label_,
581                                           false_label_);
582   DCHECK(!lit->IsUndetectableObject());  // There are no undetectable literals.
583   if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
584     if (false_label_ != fall_through_) __ B(false_label_);
585   } else if (lit->IsTrue() || lit->IsJSObject()) {
586     if (true_label_ != fall_through_) __ B(true_label_);
587   } else if (lit->IsString()) {
588     if (String::cast(*lit)->length() == 0) {
589       if (false_label_ != fall_through_) __ B(false_label_);
590     } else {
591       if (true_label_ != fall_through_) __ B(true_label_);
592     }
593   } else if (lit->IsSmi()) {
594     if (Smi::cast(*lit)->value() == 0) {
595       if (false_label_ != fall_through_) __ B(false_label_);
596     } else {
597       if (true_label_ != fall_through_) __ B(true_label_);
598     }
599   } else {
600     // For simplicity we always test the accumulator register.
601     __ Mov(result_register(), Operand(lit));
602     codegen()->DoTest(this);
603   }
604 }
605
606
607 void FullCodeGenerator::EffectContext::DropAndPlug(int count,
608                                                    Register reg) const {
609   DCHECK(count > 0);
610   __ Drop(count);
611 }
612
613
614 void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
615     int count,
616     Register reg) const {
617   DCHECK(count > 0);
618   __ Drop(count);
619   __ Move(result_register(), reg);
620 }
621
622
623 void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
624                                                        Register reg) const {
625   DCHECK(count > 0);
626   if (count > 1) __ Drop(count - 1);
627   __ Poke(reg, 0);
628 }
629
630
631 void FullCodeGenerator::TestContext::DropAndPlug(int count,
632                                                  Register reg) const {
633   DCHECK(count > 0);
634   // For simplicity we always test the accumulator register.
635   __ Drop(count);
636   __ Mov(result_register(), reg);
637   codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
638   codegen()->DoTest(this);
639 }
640
641
642 void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
643                                             Label* materialize_false) const {
644   DCHECK(materialize_true == materialize_false);
645   __ Bind(materialize_true);
646 }
647
648
649 void FullCodeGenerator::AccumulatorValueContext::Plug(
650     Label* materialize_true,
651     Label* materialize_false) const {
652   Label done;
653   __ Bind(materialize_true);
654   __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
655   __ B(&done);
656   __ Bind(materialize_false);
657   __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
658   __ Bind(&done);
659 }
660
661
662 void FullCodeGenerator::StackValueContext::Plug(
663     Label* materialize_true,
664     Label* materialize_false) const {
665   Label done;
666   __ Bind(materialize_true);
667   __ LoadRoot(x10, Heap::kTrueValueRootIndex);
668   __ B(&done);
669   __ Bind(materialize_false);
670   __ LoadRoot(x10, Heap::kFalseValueRootIndex);
671   __ Bind(&done);
672   __ Push(x10);
673 }
674
675
676 void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
677                                           Label* materialize_false) const {
678   DCHECK(materialize_true == true_label_);
679   DCHECK(materialize_false == false_label_);
680 }
681
682
683 void FullCodeGenerator::EffectContext::Plug(bool flag) const {
684 }
685
686
687 void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
688   Heap::RootListIndex value_root_index =
689       flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
690   __ LoadRoot(result_register(), value_root_index);
691 }
692
693
694 void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
695   Heap::RootListIndex value_root_index =
696       flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
697   __ LoadRoot(x10, value_root_index);
698   __ Push(x10);
699 }
700
701
702 void FullCodeGenerator::TestContext::Plug(bool flag) const {
703   codegen()->PrepareForBailoutBeforeSplit(condition(),
704                                           true,
705                                           true_label_,
706                                           false_label_);
707   if (flag) {
708     if (true_label_ != fall_through_) {
709       __ B(true_label_);
710     }
711   } else {
712     if (false_label_ != fall_through_) {
713       __ B(false_label_);
714     }
715   }
716 }
717
718
719 void FullCodeGenerator::DoTest(Expression* condition,
720                                Label* if_true,
721                                Label* if_false,
722                                Label* fall_through) {
723   Handle<Code> ic = ToBooleanStub::GetUninitialized(isolate());
724   CallIC(ic, condition->test_id());
725   __ CompareAndSplit(result_register(), 0, ne, if_true, if_false, fall_through);
726 }
727
728
729 // If (cond), branch to if_true.
730 // If (!cond), branch to if_false.
731 // fall_through is used as an optimization in cases where only one branch
732 // instruction is necessary.
733 void FullCodeGenerator::Split(Condition cond,
734                               Label* if_true,
735                               Label* if_false,
736                               Label* fall_through) {
737   if (if_false == fall_through) {
738     __ B(cond, if_true);
739   } else if (if_true == fall_through) {
740     DCHECK(if_false != fall_through);
741     __ B(NegateCondition(cond), if_false);
742   } else {
743     __ B(cond, if_true);
744     __ B(if_false);
745   }
746 }
747
748
749 MemOperand FullCodeGenerator::StackOperand(Variable* var) {
750   // Offset is negative because higher indexes are at lower addresses.
751   int offset = -var->index() * kXRegSize;
752   // Adjust by a (parameter or local) base offset.
753   if (var->IsParameter()) {
754     offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
755   } else {
756     offset += JavaScriptFrameConstants::kLocal0Offset;
757   }
758   return MemOperand(fp, offset);
759 }
760
761
762 MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
763   DCHECK(var->IsContextSlot() || var->IsStackAllocated());
764   if (var->IsContextSlot()) {
765     int context_chain_length = scope()->ContextChainLength(var->scope());
766     __ LoadContext(scratch, context_chain_length);
767     return ContextMemOperand(scratch, var->index());
768   } else {
769     return StackOperand(var);
770   }
771 }
772
773
774 void FullCodeGenerator::GetVar(Register dest, Variable* var) {
775   // Use destination as scratch.
776   MemOperand location = VarOperand(var, dest);
777   __ Ldr(dest, location);
778 }
779
780
781 void FullCodeGenerator::SetVar(Variable* var,
782                                Register src,
783                                Register scratch0,
784                                Register scratch1) {
785   DCHECK(var->IsContextSlot() || var->IsStackAllocated());
786   DCHECK(!AreAliased(src, scratch0, scratch1));
787   MemOperand location = VarOperand(var, scratch0);
788   __ Str(src, location);
789
790   // Emit the write barrier code if the location is in the heap.
791   if (var->IsContextSlot()) {
792     // scratch0 contains the correct context.
793     __ RecordWriteContextSlot(scratch0,
794                               location.offset(),
795                               src,
796                               scratch1,
797                               kLRHasBeenSaved,
798                               kDontSaveFPRegs);
799   }
800 }
801
802
803 void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
804                                                      bool should_normalize,
805                                                      Label* if_true,
806                                                      Label* if_false) {
807   // Only prepare for bailouts before splits if we're in a test
808   // context. Otherwise, we let the Visit function deal with the
809   // preparation to avoid preparing with the same AST id twice.
810   if (!context()->IsTest() || !info_->IsOptimizable()) return;
811
812   // TODO(all): Investigate to see if there is something to work on here.
813   Label skip;
814   if (should_normalize) {
815     __ B(&skip);
816   }
817   PrepareForBailout(expr, TOS_REG);
818   if (should_normalize) {
819     __ CompareRoot(x0, Heap::kTrueValueRootIndex);
820     Split(eq, if_true, if_false, NULL);
821     __ Bind(&skip);
822   }
823 }
824
825
826 void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
827   // The variable in the declaration always resides in the current function
828   // context.
829   DCHECK_EQ(0, scope()->ContextChainLength(variable->scope()));
830   if (generate_debug_code_) {
831     // Check that we're not inside a with or catch context.
832     __ Ldr(x1, FieldMemOperand(cp, HeapObject::kMapOffset));
833     __ CompareRoot(x1, Heap::kWithContextMapRootIndex);
834     __ Check(ne, kDeclarationInWithContext);
835     __ CompareRoot(x1, Heap::kCatchContextMapRootIndex);
836     __ Check(ne, kDeclarationInCatchContext);
837   }
838 }
839
840
841 void FullCodeGenerator::VisitVariableDeclaration(
842     VariableDeclaration* declaration) {
843   // If it was not possible to allocate the variable at compile time, we
844   // need to "declare" it at runtime to make sure it actually exists in the
845   // local context.
846   VariableProxy* proxy = declaration->proxy();
847   VariableMode mode = declaration->mode();
848   Variable* variable = proxy->var();
849   bool hole_init = mode == LET || mode == CONST || mode == CONST_LEGACY;
850
851   switch (variable->location()) {
852     case Variable::UNALLOCATED:
853       globals_->Add(variable->name(), zone());
854       globals_->Add(variable->binding_needs_init()
855                         ? isolate()->factory()->the_hole_value()
856                         : isolate()->factory()->undefined_value(),
857                     zone());
858       break;
859
860     case Variable::PARAMETER:
861     case Variable::LOCAL:
862       if (hole_init) {
863         Comment cmnt(masm_, "[ VariableDeclaration");
864         __ LoadRoot(x10, Heap::kTheHoleValueRootIndex);
865         __ Str(x10, StackOperand(variable));
866       }
867       break;
868
869     case Variable::CONTEXT:
870       if (hole_init) {
871         Comment cmnt(masm_, "[ VariableDeclaration");
872         EmitDebugCheckDeclarationContext(variable);
873         __ LoadRoot(x10, Heap::kTheHoleValueRootIndex);
874         __ Str(x10, ContextMemOperand(cp, variable->index()));
875         // No write barrier since the_hole_value is in old space.
876         PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
877       }
878       break;
879
880     case Variable::LOOKUP: {
881       Comment cmnt(masm_, "[ VariableDeclaration");
882       __ Mov(x2, Operand(variable->name()));
883       // Declaration nodes are always introduced in one of four modes.
884       DCHECK(IsDeclaredVariableMode(mode));
885       PropertyAttributes attr = IsImmutableVariableMode(mode) ? READ_ONLY
886                                                               : NONE;
887       __ Mov(x1, Smi::FromInt(attr));
888       // Push initial value, if any.
889       // Note: For variables we must not push an initial value (such as
890       // 'undefined') because we may have a (legal) redeclaration and we
891       // must not destroy the current value.
892       if (hole_init) {
893         __ LoadRoot(x0, Heap::kTheHoleValueRootIndex);
894         __ Push(cp, x2, x1, x0);
895       } else {
896         // Pushing 0 (xzr) indicates no initial value.
897         __ Push(cp, x2, x1, xzr);
898       }
899       __ CallRuntime(Runtime::kDeclareLookupSlot, 4);
900       break;
901     }
902   }
903 }
904
905
906 void FullCodeGenerator::VisitFunctionDeclaration(
907     FunctionDeclaration* declaration) {
908   VariableProxy* proxy = declaration->proxy();
909   Variable* variable = proxy->var();
910   switch (variable->location()) {
911     case Variable::UNALLOCATED: {
912       globals_->Add(variable->name(), zone());
913       Handle<SharedFunctionInfo> function =
914           Compiler::BuildFunctionInfo(declaration->fun(), script(), info_);
915       // Check for stack overflow exception.
916       if (function.is_null()) return SetStackOverflow();
917       globals_->Add(function, zone());
918       break;
919     }
920
921     case Variable::PARAMETER:
922     case Variable::LOCAL: {
923       Comment cmnt(masm_, "[ Function Declaration");
924       VisitForAccumulatorValue(declaration->fun());
925       __ Str(result_register(), StackOperand(variable));
926       break;
927     }
928
929     case Variable::CONTEXT: {
930       Comment cmnt(masm_, "[ Function Declaration");
931       EmitDebugCheckDeclarationContext(variable);
932       VisitForAccumulatorValue(declaration->fun());
933       __ Str(result_register(), ContextMemOperand(cp, variable->index()));
934       int offset = Context::SlotOffset(variable->index());
935       // We know that we have written a function, which is not a smi.
936       __ RecordWriteContextSlot(cp,
937                                 offset,
938                                 result_register(),
939                                 x2,
940                                 kLRHasBeenSaved,
941                                 kDontSaveFPRegs,
942                                 EMIT_REMEMBERED_SET,
943                                 OMIT_SMI_CHECK);
944       PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
945       break;
946     }
947
948     case Variable::LOOKUP: {
949       Comment cmnt(masm_, "[ Function Declaration");
950       __ Mov(x2, Operand(variable->name()));
951       __ Mov(x1, Smi::FromInt(NONE));
952       __ Push(cp, x2, x1);
953       // Push initial value for function declaration.
954       VisitForStackValue(declaration->fun());
955       __ CallRuntime(Runtime::kDeclareLookupSlot, 4);
956       break;
957     }
958   }
959 }
960
961
962 void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* declaration) {
963   Variable* variable = declaration->proxy()->var();
964   ModuleDescriptor* descriptor = declaration->module()->descriptor();
965   DCHECK(variable->location() == Variable::CONTEXT);
966   DCHECK(descriptor->IsFrozen());
967
968   Comment cmnt(masm_, "[ ModuleDeclaration");
969   EmitDebugCheckDeclarationContext(variable);
970
971   // Load instance object.
972   __ LoadContext(x1, scope_->ContextChainLength(scope_->ScriptScope()));
973   __ Ldr(x1, ContextMemOperand(x1, descriptor->Index()));
974   __ Ldr(x1, ContextMemOperand(x1, Context::EXTENSION_INDEX));
975
976   // Assign it.
977   __ Str(x1, ContextMemOperand(cp, variable->index()));
978   // We know that we have written a module, which is not a smi.
979   __ RecordWriteContextSlot(cp,
980                             Context::SlotOffset(variable->index()),
981                             x1,
982                             x3,
983                             kLRHasBeenSaved,
984                             kDontSaveFPRegs,
985                             EMIT_REMEMBERED_SET,
986                             OMIT_SMI_CHECK);
987   PrepareForBailoutForId(declaration->proxy()->id(), NO_REGISTERS);
988
989   // Traverse info body.
990   Visit(declaration->module());
991 }
992
993
994 void FullCodeGenerator::VisitImportDeclaration(ImportDeclaration* declaration) {
995   VariableProxy* proxy = declaration->proxy();
996   Variable* variable = proxy->var();
997   switch (variable->location()) {
998     case Variable::UNALLOCATED:
999       // TODO(rossberg)
1000       break;
1001
1002     case Variable::CONTEXT: {
1003       Comment cmnt(masm_, "[ ImportDeclaration");
1004       EmitDebugCheckDeclarationContext(variable);
1005       // TODO(rossberg)
1006       break;
1007     }
1008
1009     case Variable::PARAMETER:
1010     case Variable::LOCAL:
1011     case Variable::LOOKUP:
1012       UNREACHABLE();
1013   }
1014 }
1015
1016
1017 void FullCodeGenerator::VisitExportDeclaration(ExportDeclaration* declaration) {
1018   // TODO(rossberg)
1019 }
1020
1021
1022 void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
1023   // Call the runtime to declare the globals.
1024   __ Mov(x11, Operand(pairs));
1025   Register flags = xzr;
1026   if (Smi::FromInt(DeclareGlobalsFlags())) {
1027     flags = x10;
1028   __ Mov(flags, Smi::FromInt(DeclareGlobalsFlags()));
1029   }
1030   __ Push(cp, x11, flags);
1031   __ CallRuntime(Runtime::kDeclareGlobals, 3);
1032   // Return value is ignored.
1033 }
1034
1035
1036 void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
1037   // Call the runtime to declare the modules.
1038   __ Push(descriptions);
1039   __ CallRuntime(Runtime::kDeclareModules, 1);
1040   // Return value is ignored.
1041 }
1042
1043
1044 void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
1045   ASM_LOCATION("FullCodeGenerator::VisitSwitchStatement");
1046   Comment cmnt(masm_, "[ SwitchStatement");
1047   Breakable nested_statement(this, stmt);
1048   SetStatementPosition(stmt);
1049
1050   // Keep the switch value on the stack until a case matches.
1051   VisitForStackValue(stmt->tag());
1052   PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
1053
1054   ZoneList<CaseClause*>* clauses = stmt->cases();
1055   CaseClause* default_clause = NULL;  // Can occur anywhere in the list.
1056
1057   Label next_test;  // Recycled for each test.
1058   // Compile all the tests with branches to their bodies.
1059   for (int i = 0; i < clauses->length(); i++) {
1060     CaseClause* clause = clauses->at(i);
1061     clause->body_target()->Unuse();
1062
1063     // The default is not a test, but remember it as final fall through.
1064     if (clause->is_default()) {
1065       default_clause = clause;
1066       continue;
1067     }
1068
1069     Comment cmnt(masm_, "[ Case comparison");
1070     __ Bind(&next_test);
1071     next_test.Unuse();
1072
1073     // Compile the label expression.
1074     VisitForAccumulatorValue(clause->label());
1075
1076     // Perform the comparison as if via '==='.
1077     __ Peek(x1, 0);   // Switch value.
1078
1079     JumpPatchSite patch_site(masm_);
1080     if (ShouldInlineSmiCase(Token::EQ_STRICT)) {
1081       Label slow_case;
1082       patch_site.EmitJumpIfEitherNotSmi(x0, x1, &slow_case);
1083       __ Cmp(x1, x0);
1084       __ B(ne, &next_test);
1085       __ Drop(1);  // Switch value is no longer needed.
1086       __ B(clause->body_target());
1087       __ Bind(&slow_case);
1088     }
1089
1090     // Record position before stub call for type feedback.
1091     SetSourcePosition(clause->position());
1092     Handle<Code> ic =
1093         CodeFactory::CompareIC(isolate(), Token::EQ_STRICT).code();
1094     CallIC(ic, clause->CompareId());
1095     patch_site.EmitPatchInfo();
1096
1097     Label skip;
1098     __ B(&skip);
1099     PrepareForBailout(clause, TOS_REG);
1100     __ JumpIfNotRoot(x0, Heap::kTrueValueRootIndex, &next_test);
1101     __ Drop(1);
1102     __ B(clause->body_target());
1103     __ Bind(&skip);
1104
1105     __ Cbnz(x0, &next_test);
1106     __ Drop(1);  // Switch value is no longer needed.
1107     __ B(clause->body_target());
1108   }
1109
1110   // Discard the test value and jump to the default if present, otherwise to
1111   // the end of the statement.
1112   __ Bind(&next_test);
1113   __ Drop(1);  // Switch value is no longer needed.
1114   if (default_clause == NULL) {
1115     __ B(nested_statement.break_label());
1116   } else {
1117     __ B(default_clause->body_target());
1118   }
1119
1120   // Compile all the case bodies.
1121   for (int i = 0; i < clauses->length(); i++) {
1122     Comment cmnt(masm_, "[ Case body");
1123     CaseClause* clause = clauses->at(i);
1124     __ Bind(clause->body_target());
1125     PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
1126     VisitStatements(clause->statements());
1127   }
1128
1129   __ Bind(nested_statement.break_label());
1130   PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1131 }
1132
1133
1134 void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
1135   ASM_LOCATION("FullCodeGenerator::VisitForInStatement");
1136   Comment cmnt(masm_, "[ ForInStatement");
1137   FeedbackVectorSlot slot = stmt->ForInFeedbackSlot();
1138   // TODO(all): This visitor probably needs better comments and a revisit.
1139   SetStatementPosition(stmt);
1140
1141   Label loop, exit;
1142   ForIn loop_statement(this, stmt);
1143   increment_loop_depth();
1144
1145   // Get the object to enumerate over. If the object is null or undefined, skip
1146   // over the loop.  See ECMA-262 version 5, section 12.6.4.
1147   SetExpressionPosition(stmt->enumerable());
1148   VisitForAccumulatorValue(stmt->enumerable());
1149   __ JumpIfRoot(x0, Heap::kUndefinedValueRootIndex, &exit);
1150   Register null_value = x15;
1151   __ LoadRoot(null_value, Heap::kNullValueRootIndex);
1152   __ Cmp(x0, null_value);
1153   __ B(eq, &exit);
1154
1155   PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1156
1157   // Convert the object to a JS object.
1158   Label convert, done_convert;
1159   __ JumpIfSmi(x0, &convert);
1160   __ JumpIfObjectType(x0, x10, x11, FIRST_SPEC_OBJECT_TYPE, &done_convert, ge);
1161   __ Bind(&convert);
1162   __ Push(x0);
1163   __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1164   __ Bind(&done_convert);
1165   PrepareForBailoutForId(stmt->ToObjectId(), TOS_REG);
1166   __ Push(x0);
1167
1168   // Check for proxies.
1169   Label call_runtime;
1170   STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1171   __ JumpIfObjectType(x0, x10, x11, LAST_JS_PROXY_TYPE, &call_runtime, le);
1172
1173   // Check cache validity in generated code. This is a fast case for
1174   // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1175   // guarantee cache validity, call the runtime system to check cache
1176   // validity or get the property names in a fixed array.
1177   __ CheckEnumCache(x0, null_value, x10, x11, x12, x13, &call_runtime);
1178
1179   // The enum cache is valid.  Load the map of the object being
1180   // iterated over and use the cache for the iteration.
1181   Label use_cache;
1182   __ Ldr(x0, FieldMemOperand(x0, HeapObject::kMapOffset));
1183   __ B(&use_cache);
1184
1185   // Get the set of properties to enumerate.
1186   __ Bind(&call_runtime);
1187   __ Push(x0);  // Duplicate the enumerable object on the stack.
1188   __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1189   PrepareForBailoutForId(stmt->EnumId(), TOS_REG);
1190
1191   // If we got a map from the runtime call, we can do a fast
1192   // modification check. Otherwise, we got a fixed array, and we have
1193   // to do a slow check.
1194   Label fixed_array, no_descriptors;
1195   __ Ldr(x2, FieldMemOperand(x0, HeapObject::kMapOffset));
1196   __ JumpIfNotRoot(x2, Heap::kMetaMapRootIndex, &fixed_array);
1197
1198   // We got a map in register x0. Get the enumeration cache from it.
1199   __ Bind(&use_cache);
1200
1201   __ EnumLengthUntagged(x1, x0);
1202   __ Cbz(x1, &no_descriptors);
1203
1204   __ LoadInstanceDescriptors(x0, x2);
1205   __ Ldr(x2, FieldMemOperand(x2, DescriptorArray::kEnumCacheOffset));
1206   __ Ldr(x2,
1207          FieldMemOperand(x2, DescriptorArray::kEnumCacheBridgeCacheOffset));
1208
1209   // Set up the four remaining stack slots.
1210   __ SmiTag(x1);
1211   // Map, enumeration cache, enum cache length, zero (both last as smis).
1212   __ Push(x0, x2, x1, xzr);
1213   __ B(&loop);
1214
1215   __ Bind(&no_descriptors);
1216   __ Drop(1);
1217   __ B(&exit);
1218
1219   // We got a fixed array in register x0. Iterate through that.
1220   __ Bind(&fixed_array);
1221
1222   __ LoadObject(x1, FeedbackVector());
1223   __ Mov(x10, Operand(TypeFeedbackVector::MegamorphicSentinel(isolate())));
1224   int vector_index = FeedbackVector()->GetIndex(slot);
1225   __ Str(x10, FieldMemOperand(x1, FixedArray::OffsetOfElementAt(vector_index)));
1226
1227   __ Mov(x1, Smi::FromInt(1));  // Smi indicates slow check.
1228   __ Peek(x10, 0);  // Get enumerated object.
1229   STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1230   // TODO(all): similar check was done already. Can we avoid it here?
1231   __ CompareObjectType(x10, x11, x12, LAST_JS_PROXY_TYPE);
1232   DCHECK(Smi::FromInt(0) == 0);
1233   __ CzeroX(x1, le);  // Zero indicates proxy.
1234   __ Ldr(x2, FieldMemOperand(x0, FixedArray::kLengthOffset));
1235   // Smi and array, fixed array length (as smi) and initial index.
1236   __ Push(x1, x0, x2, xzr);
1237
1238   // Generate code for doing the condition check.
1239   PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
1240   __ Bind(&loop);
1241   SetExpressionPosition(stmt->each());
1242
1243   // Load the current count to x0, load the length to x1.
1244   __ PeekPair(x0, x1, 0);
1245   __ Cmp(x0, x1);  // Compare to the array length.
1246   __ B(hs, loop_statement.break_label());
1247
1248   // Get the current entry of the array into register r3.
1249   __ Peek(x10, 2 * kXRegSize);
1250   __ Add(x10, x10, Operand::UntagSmiAndScale(x0, kPointerSizeLog2));
1251   __ Ldr(x3, MemOperand(x10, FixedArray::kHeaderSize - kHeapObjectTag));
1252
1253   // Get the expected map from the stack or a smi in the
1254   // permanent slow case into register x10.
1255   __ Peek(x2, 3 * kXRegSize);
1256
1257   // Check if the expected map still matches that of the enumerable.
1258   // If not, we may have to filter the key.
1259   Label update_each;
1260   __ Peek(x1, 4 * kXRegSize);
1261   __ Ldr(x11, FieldMemOperand(x1, HeapObject::kMapOffset));
1262   __ Cmp(x11, x2);
1263   __ B(eq, &update_each);
1264
1265   // For proxies, no filtering is done.
1266   // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1267   STATIC_ASSERT(kSmiTag == 0);
1268   __ Cbz(x2, &update_each);
1269
1270   // Convert the entry to a string or (smi) 0 if it isn't a property
1271   // any more. If the property has been removed while iterating, we
1272   // just skip it.
1273   __ Push(x1, x3);
1274   __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_FUNCTION);
1275   __ Mov(x3, x0);
1276   __ Cbz(x0, loop_statement.continue_label());
1277
1278   // Update the 'each' property or variable from the possibly filtered
1279   // entry in register x3.
1280   __ Bind(&update_each);
1281   __ Mov(result_register(), x3);
1282   // Perform the assignment as if via '='.
1283   { EffectContext context(this);
1284     EmitAssignment(stmt->each());
1285     PrepareForBailoutForId(stmt->AssignmentId(), NO_REGISTERS);
1286   }
1287
1288   // Generate code for the body of the loop.
1289   Visit(stmt->body());
1290
1291   // Generate code for going to the next element by incrementing
1292   // the index (smi) stored on top of the stack.
1293   __ Bind(loop_statement.continue_label());
1294   // TODO(all): We could use a callee saved register to avoid popping.
1295   __ Pop(x0);
1296   __ Add(x0, x0, Smi::FromInt(1));
1297   __ Push(x0);
1298
1299   EmitBackEdgeBookkeeping(stmt, &loop);
1300   __ B(&loop);
1301
1302   // Remove the pointers stored on the stack.
1303   __ Bind(loop_statement.break_label());
1304   __ Drop(5);
1305
1306   // Exit and decrement the loop depth.
1307   PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1308   __ Bind(&exit);
1309   decrement_loop_depth();
1310 }
1311
1312
1313 void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1314                                        bool pretenure) {
1315   // Use the fast case closure allocation code that allocates in new space for
1316   // nested functions that don't need literals cloning. If we're running with
1317   // the --always-opt or the --prepare-always-opt flag, we need to use the
1318   // runtime function so that the new function we are creating here gets a
1319   // chance to have its code optimized and doesn't just get a copy of the
1320   // existing unoptimized code.
1321   if (!FLAG_always_opt &&
1322       !FLAG_prepare_always_opt &&
1323       !pretenure &&
1324       scope()->is_function_scope() &&
1325       info->num_literals() == 0) {
1326     FastNewClosureStub stub(isolate(), info->language_mode(), info->kind());
1327     __ Mov(x2, Operand(info));
1328     __ CallStub(&stub);
1329   } else {
1330     __ Mov(x11, Operand(info));
1331     __ LoadRoot(x10, pretenure ? Heap::kTrueValueRootIndex
1332                                : Heap::kFalseValueRootIndex);
1333     __ Push(cp, x11, x10);
1334     __ CallRuntime(Runtime::kNewClosure, 3);
1335   }
1336   context()->Plug(x0);
1337 }
1338
1339
1340 void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
1341   Comment cmnt(masm_, "[ VariableProxy");
1342   EmitVariableLoad(expr);
1343 }
1344
1345
1346 void FullCodeGenerator::EmitLoadHomeObject(SuperReference* expr) {
1347   Comment cnmt(masm_, "[ SuperReference ");
1348
1349   __ ldr(LoadDescriptor::ReceiverRegister(),
1350          MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1351
1352   Handle<Symbol> home_object_symbol(isolate()->heap()->home_object_symbol());
1353   __ Mov(LoadDescriptor::NameRegister(), Operand(home_object_symbol));
1354
1355   if (FLAG_vector_ics) {
1356     __ Mov(VectorLoadICDescriptor::SlotRegister(),
1357            SmiFromSlot(expr->HomeObjectFeedbackSlot()));
1358     CallLoadIC(NOT_CONTEXTUAL);
1359   } else {
1360     CallLoadIC(NOT_CONTEXTUAL, expr->HomeObjectFeedbackId());
1361   }
1362
1363   __ Mov(x10, Operand(isolate()->factory()->undefined_value()));
1364   __ cmp(x0, x10);
1365   Label done;
1366   __ b(&done, ne);
1367   __ CallRuntime(Runtime::kThrowNonMethodError, 0);
1368   __ bind(&done);
1369 }
1370
1371
1372 void FullCodeGenerator::EmitSetHomeObjectIfNeeded(Expression* initializer,
1373                                                   int offset) {
1374   if (NeedsHomeObject(initializer)) {
1375     __ Peek(StoreDescriptor::ReceiverRegister(), 0);
1376     __ Mov(StoreDescriptor::NameRegister(),
1377            Operand(isolate()->factory()->home_object_symbol()));
1378     __ Peek(StoreDescriptor::ValueRegister(), offset * kPointerSize);
1379     CallStoreIC();
1380   }
1381 }
1382
1383
1384 void FullCodeGenerator::EmitLoadGlobalCheckExtensions(VariableProxy* proxy,
1385                                                       TypeofState typeof_state,
1386                                                       Label* slow) {
1387   Register current = cp;
1388   Register next = x10;
1389   Register temp = x11;
1390
1391   Scope* s = scope();
1392   while (s != NULL) {
1393     if (s->num_heap_slots() > 0) {
1394       if (s->calls_sloppy_eval()) {
1395         // Check that extension is NULL.
1396         __ Ldr(temp, ContextMemOperand(current, Context::EXTENSION_INDEX));
1397         __ Cbnz(temp, slow);
1398       }
1399       // Load next context in chain.
1400       __ Ldr(next, ContextMemOperand(current, Context::PREVIOUS_INDEX));
1401       // Walk the rest of the chain without clobbering cp.
1402       current = next;
1403     }
1404     // If no outer scope calls eval, we do not need to check more
1405     // context extensions.
1406     if (!s->outer_scope_calls_sloppy_eval() || s->is_eval_scope()) break;
1407     s = s->outer_scope();
1408   }
1409
1410   if (s->is_eval_scope()) {
1411     Label loop, fast;
1412     __ Mov(next, current);
1413
1414     __ Bind(&loop);
1415     // Terminate at native context.
1416     __ Ldr(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1417     __ JumpIfRoot(temp, Heap::kNativeContextMapRootIndex, &fast);
1418     // Check that extension is NULL.
1419     __ Ldr(temp, ContextMemOperand(next, Context::EXTENSION_INDEX));
1420     __ Cbnz(temp, slow);
1421     // Load next context in chain.
1422     __ Ldr(next, ContextMemOperand(next, Context::PREVIOUS_INDEX));
1423     __ B(&loop);
1424     __ Bind(&fast);
1425   }
1426
1427   __ Ldr(LoadDescriptor::ReceiverRegister(), GlobalObjectMemOperand());
1428   __ Mov(LoadDescriptor::NameRegister(), Operand(proxy->var()->name()));
1429   if (FLAG_vector_ics) {
1430     __ Mov(VectorLoadICDescriptor::SlotRegister(),
1431            SmiFromSlot(proxy->VariableFeedbackSlot()));
1432   }
1433
1434   ContextualMode mode = (typeof_state == INSIDE_TYPEOF) ? NOT_CONTEXTUAL
1435                                                         : CONTEXTUAL;
1436   CallLoadIC(mode);
1437 }
1438
1439
1440 MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1441                                                                 Label* slow) {
1442   DCHECK(var->IsContextSlot());
1443   Register context = cp;
1444   Register next = x10;
1445   Register temp = x11;
1446
1447   for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
1448     if (s->num_heap_slots() > 0) {
1449       if (s->calls_sloppy_eval()) {
1450         // Check that extension is NULL.
1451         __ Ldr(temp, ContextMemOperand(context, Context::EXTENSION_INDEX));
1452         __ Cbnz(temp, slow);
1453       }
1454       __ Ldr(next, ContextMemOperand(context, Context::PREVIOUS_INDEX));
1455       // Walk the rest of the chain without clobbering cp.
1456       context = next;
1457     }
1458   }
1459   // Check that last extension is NULL.
1460   __ Ldr(temp, ContextMemOperand(context, Context::EXTENSION_INDEX));
1461   __ Cbnz(temp, slow);
1462
1463   // This function is used only for loads, not stores, so it's safe to
1464   // return an cp-based operand (the write barrier cannot be allowed to
1465   // destroy the cp register).
1466   return ContextMemOperand(context, var->index());
1467 }
1468
1469
1470 void FullCodeGenerator::EmitDynamicLookupFastCase(VariableProxy* proxy,
1471                                                   TypeofState typeof_state,
1472                                                   Label* slow,
1473                                                   Label* done) {
1474   // Generate fast-case code for variables that might be shadowed by
1475   // eval-introduced variables.  Eval is used a lot without
1476   // introducing variables.  In those cases, we do not want to
1477   // perform a runtime call for all variables in the scope
1478   // containing the eval.
1479   Variable* var = proxy->var();
1480   if (var->mode() == DYNAMIC_GLOBAL) {
1481     EmitLoadGlobalCheckExtensions(proxy, typeof_state, slow);
1482     __ B(done);
1483   } else if (var->mode() == DYNAMIC_LOCAL) {
1484     Variable* local = var->local_if_not_shadowed();
1485     __ Ldr(x0, ContextSlotOperandCheckExtensions(local, slow));
1486     if (local->mode() == LET || local->mode() == CONST ||
1487         local->mode() == CONST_LEGACY) {
1488       __ JumpIfNotRoot(x0, Heap::kTheHoleValueRootIndex, done);
1489       if (local->mode() == CONST_LEGACY) {
1490         __ LoadRoot(x0, Heap::kUndefinedValueRootIndex);
1491       } else {  // LET || CONST
1492         __ Mov(x0, Operand(var->name()));
1493         __ Push(x0);
1494         __ CallRuntime(Runtime::kThrowReferenceError, 1);
1495       }
1496     }
1497     __ B(done);
1498   }
1499 }
1500
1501
1502 void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy) {
1503   // Record position before possible IC call.
1504   SetSourcePosition(proxy->position());
1505   Variable* var = proxy->var();
1506
1507   // Three cases: global variables, lookup variables, and all other types of
1508   // variables.
1509   switch (var->location()) {
1510     case Variable::UNALLOCATED: {
1511       Comment cmnt(masm_, "Global variable");
1512       __ Ldr(LoadDescriptor::ReceiverRegister(), GlobalObjectMemOperand());
1513       __ Mov(LoadDescriptor::NameRegister(), Operand(var->name()));
1514       if (FLAG_vector_ics) {
1515         __ Mov(VectorLoadICDescriptor::SlotRegister(),
1516                SmiFromSlot(proxy->VariableFeedbackSlot()));
1517       }
1518       CallGlobalLoadIC(var->name());
1519       context()->Plug(x0);
1520       break;
1521     }
1522
1523     case Variable::PARAMETER:
1524     case Variable::LOCAL:
1525     case Variable::CONTEXT: {
1526       Comment cmnt(masm_, var->IsContextSlot()
1527                               ? "Context variable"
1528                               : "Stack variable");
1529       if (var->binding_needs_init()) {
1530         // var->scope() may be NULL when the proxy is located in eval code and
1531         // refers to a potential outside binding. Currently those bindings are
1532         // always looked up dynamically, i.e. in that case
1533         //     var->location() == LOOKUP.
1534         // always holds.
1535         DCHECK(var->scope() != NULL);
1536
1537         // Check if the binding really needs an initialization check. The check
1538         // can be skipped in the following situation: we have a LET or CONST
1539         // binding in harmony mode, both the Variable and the VariableProxy have
1540         // the same declaration scope (i.e. they are both in global code, in the
1541         // same function or in the same eval code) and the VariableProxy is in
1542         // the source physically located after the initializer of the variable.
1543         //
1544         // We cannot skip any initialization checks for CONST in non-harmony
1545         // mode because const variables may be declared but never initialized:
1546         //   if (false) { const x; }; var y = x;
1547         //
1548         // The condition on the declaration scopes is a conservative check for
1549         // nested functions that access a binding and are called before the
1550         // binding is initialized:
1551         //   function() { f(); let x = 1; function f() { x = 2; } }
1552         //
1553         bool skip_init_check;
1554         if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1555           skip_init_check = false;
1556         } else if (var->is_this()) {
1557           CHECK(info_->function() != nullptr &&
1558                 (info_->function()->kind() & kSubclassConstructor) != 0);
1559           // TODO(dslomov): implement 'this' hole check elimination.
1560           skip_init_check = false;
1561         } else {
1562           // Check that we always have valid source position.
1563           DCHECK(var->initializer_position() != RelocInfo::kNoPosition);
1564           DCHECK(proxy->position() != RelocInfo::kNoPosition);
1565           skip_init_check = var->mode() != CONST_LEGACY &&
1566               var->initializer_position() < proxy->position();
1567         }
1568
1569         if (!skip_init_check) {
1570           // Let and const need a read barrier.
1571           GetVar(x0, var);
1572           Label done;
1573           __ JumpIfNotRoot(x0, Heap::kTheHoleValueRootIndex, &done);
1574           if (var->mode() == LET || var->mode() == CONST) {
1575             // Throw a reference error when using an uninitialized let/const
1576             // binding in harmony mode.
1577             __ Mov(x0, Operand(var->name()));
1578             __ Push(x0);
1579             __ CallRuntime(Runtime::kThrowReferenceError, 1);
1580             __ Bind(&done);
1581           } else {
1582             // Uninitalized const bindings outside of harmony mode are unholed.
1583             DCHECK(var->mode() == CONST_LEGACY);
1584             __ LoadRoot(x0, Heap::kUndefinedValueRootIndex);
1585             __ Bind(&done);
1586           }
1587           context()->Plug(x0);
1588           break;
1589         }
1590       }
1591       context()->Plug(var);
1592       break;
1593     }
1594
1595     case Variable::LOOKUP: {
1596       Label done, slow;
1597       // Generate code for loading from variables potentially shadowed by
1598       // eval-introduced variables.
1599       EmitDynamicLookupFastCase(proxy, NOT_INSIDE_TYPEOF, &slow, &done);
1600       __ Bind(&slow);
1601       Comment cmnt(masm_, "Lookup variable");
1602       __ Mov(x1, Operand(var->name()));
1603       __ Push(cp, x1);  // Context and name.
1604       __ CallRuntime(Runtime::kLoadLookupSlot, 2);
1605       __ Bind(&done);
1606       context()->Plug(x0);
1607       break;
1608     }
1609   }
1610 }
1611
1612
1613 void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1614   Comment cmnt(masm_, "[ RegExpLiteral");
1615   Label materialized;
1616   // Registers will be used as follows:
1617   // x5 = materialized value (RegExp literal)
1618   // x4 = JS function, literals array
1619   // x3 = literal index
1620   // x2 = RegExp pattern
1621   // x1 = RegExp flags
1622   // x0 = RegExp literal clone
1623   __ Ldr(x10, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1624   __ Ldr(x4, FieldMemOperand(x10, JSFunction::kLiteralsOffset));
1625   int literal_offset =
1626       FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1627   __ Ldr(x5, FieldMemOperand(x4, literal_offset));
1628   __ JumpIfNotRoot(x5, Heap::kUndefinedValueRootIndex, &materialized);
1629
1630   // Create regexp literal using runtime function.
1631   // Result will be in x0.
1632   __ Mov(x3, Smi::FromInt(expr->literal_index()));
1633   __ Mov(x2, Operand(expr->pattern()));
1634   __ Mov(x1, Operand(expr->flags()));
1635   __ Push(x4, x3, x2, x1);
1636   __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1637   __ Mov(x5, x0);
1638
1639   __ Bind(&materialized);
1640   int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1641   Label allocated, runtime_allocate;
1642   __ Allocate(size, x0, x2, x3, &runtime_allocate, TAG_OBJECT);
1643   __ B(&allocated);
1644
1645   __ Bind(&runtime_allocate);
1646   __ Mov(x10, Smi::FromInt(size));
1647   __ Push(x5, x10);
1648   __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1649   __ Pop(x5);
1650
1651   __ Bind(&allocated);
1652   // After this, registers are used as follows:
1653   // x0: Newly allocated regexp.
1654   // x5: Materialized regexp.
1655   // x10, x11, x12: temps.
1656   __ CopyFields(x0, x5, CPURegList(x10, x11, x12), size / kPointerSize);
1657   context()->Plug(x0);
1658 }
1659
1660
1661 void FullCodeGenerator::EmitAccessor(Expression* expression) {
1662   if (expression == NULL) {
1663     __ LoadRoot(x10, Heap::kNullValueRootIndex);
1664     __ Push(x10);
1665   } else {
1666     VisitForStackValue(expression);
1667   }
1668 }
1669
1670
1671 void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1672   Comment cmnt(masm_, "[ ObjectLiteral");
1673
1674   expr->BuildConstantProperties(isolate());
1675   Handle<FixedArray> constant_properties = expr->constant_properties();
1676   __ Ldr(x3, MemOperand(fp,  JavaScriptFrameConstants::kFunctionOffset));
1677   __ Ldr(x3, FieldMemOperand(x3, JSFunction::kLiteralsOffset));
1678   __ Mov(x2, Smi::FromInt(expr->literal_index()));
1679   __ Mov(x1, Operand(constant_properties));
1680   int flags = expr->ComputeFlags();
1681   __ Mov(x0, Smi::FromInt(flags));
1682   if (MustCreateObjectLiteralWithRuntime(expr)) {
1683     __ Push(x3, x2, x1, x0);
1684     __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1685   } else {
1686     FastCloneShallowObjectStub stub(isolate(), expr->properties_count());
1687     __ CallStub(&stub);
1688   }
1689   PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1690
1691   // If result_saved is true the result is on top of the stack.  If
1692   // result_saved is false the result is in x0.
1693   bool result_saved = false;
1694
1695   // Mark all computed expressions that are bound to a key that
1696   // is shadowed by a later occurrence of the same key. For the
1697   // marked expressions, no store code is emitted.
1698   expr->CalculateEmitStore(zone());
1699
1700   AccessorTable accessor_table(zone());
1701   int property_index = 0;
1702   for (; property_index < expr->properties()->length(); property_index++) {
1703     ObjectLiteral::Property* property = expr->properties()->at(property_index);
1704     if (property->is_computed_name()) break;
1705     if (property->IsCompileTimeValue()) continue;
1706
1707     Literal* key = property->key()->AsLiteral();
1708     Expression* value = property->value();
1709     if (!result_saved) {
1710       __ Push(x0);  // Save result on stack
1711       result_saved = true;
1712     }
1713     switch (property->kind()) {
1714       case ObjectLiteral::Property::CONSTANT:
1715         UNREACHABLE();
1716       case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1717         DCHECK(!CompileTimeValue::IsCompileTimeValue(property->value()));
1718         // Fall through.
1719       case ObjectLiteral::Property::COMPUTED:
1720         // It is safe to use [[Put]] here because the boilerplate already
1721         // contains computed properties with an uninitialized value.
1722         if (key->value()->IsInternalizedString()) {
1723           if (property->emit_store()) {
1724             VisitForAccumulatorValue(value);
1725             DCHECK(StoreDescriptor::ValueRegister().is(x0));
1726             __ Mov(StoreDescriptor::NameRegister(), Operand(key->value()));
1727             __ Peek(StoreDescriptor::ReceiverRegister(), 0);
1728             CallStoreIC(key->LiteralFeedbackId());
1729             PrepareForBailoutForId(key->id(), NO_REGISTERS);
1730
1731             if (NeedsHomeObject(value)) {
1732               __ Mov(StoreDescriptor::ReceiverRegister(), x0);
1733               __ Mov(StoreDescriptor::NameRegister(),
1734                      Operand(isolate()->factory()->home_object_symbol()));
1735               __ Peek(StoreDescriptor::ValueRegister(), 0);
1736               CallStoreIC();
1737             }
1738           } else {
1739             VisitForEffect(value);
1740           }
1741           break;
1742         }
1743         if (property->emit_store()) {
1744           // Duplicate receiver on stack.
1745           __ Peek(x0, 0);
1746           __ Push(x0);
1747           VisitForStackValue(key);
1748           VisitForStackValue(value);
1749           EmitSetHomeObjectIfNeeded(value, 2);
1750           __ Mov(x0, Smi::FromInt(SLOPPY));  // Language mode
1751           __ Push(x0);
1752           __ CallRuntime(Runtime::kSetProperty, 4);
1753         } else {
1754           VisitForEffect(key);
1755           VisitForEffect(value);
1756         }
1757         break;
1758       case ObjectLiteral::Property::PROTOTYPE:
1759         DCHECK(property->emit_store());
1760         // Duplicate receiver on stack.
1761         __ Peek(x0, 0);
1762         __ Push(x0);
1763         VisitForStackValue(value);
1764         __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1765         break;
1766       case ObjectLiteral::Property::GETTER:
1767         if (property->emit_store()) {
1768           accessor_table.lookup(key)->second->getter = value;
1769         }
1770         break;
1771       case ObjectLiteral::Property::SETTER:
1772         if (property->emit_store()) {
1773           accessor_table.lookup(key)->second->setter = value;
1774         }
1775         break;
1776     }
1777   }
1778
1779   // Emit code to define accessors, using only a single call to the runtime for
1780   // each pair of corresponding getters and setters.
1781   for (AccessorTable::Iterator it = accessor_table.begin();
1782        it != accessor_table.end();
1783        ++it) {
1784       __ Peek(x10, 0);  // Duplicate receiver.
1785       __ Push(x10);
1786       VisitForStackValue(it->first);
1787       EmitAccessor(it->second->getter);
1788       EmitSetHomeObjectIfNeeded(it->second->getter, 2);
1789       EmitAccessor(it->second->setter);
1790       EmitSetHomeObjectIfNeeded(it->second->setter, 3);
1791       __ Mov(x10, Smi::FromInt(NONE));
1792       __ Push(x10);
1793       __ CallRuntime(Runtime::kDefineAccessorPropertyUnchecked, 5);
1794   }
1795
1796   // Object literals have two parts. The "static" part on the left contains no
1797   // computed property names, and so we can compute its map ahead of time; see
1798   // runtime.cc::CreateObjectLiteralBoilerplate. The second "dynamic" part
1799   // starts with the first computed property name, and continues with all
1800   // properties to its right.  All the code from above initializes the static
1801   // component of the object literal, and arranges for the map of the result to
1802   // reflect the static order in which the keys appear. For the dynamic
1803   // properties, we compile them into a series of "SetOwnProperty" runtime
1804   // calls. This will preserve insertion order.
1805   for (; property_index < expr->properties()->length(); property_index++) {
1806     ObjectLiteral::Property* property = expr->properties()->at(property_index);
1807
1808     Expression* value = property->value();
1809     if (!result_saved) {
1810       __ Push(x0);  // Save result on stack
1811       result_saved = true;
1812     }
1813
1814     __ Peek(x10, 0);  // Duplicate receiver.
1815     __ Push(x10);
1816
1817     if (property->kind() == ObjectLiteral::Property::PROTOTYPE) {
1818       DCHECK(!property->is_computed_name());
1819       VisitForStackValue(value);
1820       DCHECK(property->emit_store());
1821       __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1822     } else {
1823       EmitPropertyKey(property, expr->GetIdForProperty(property_index));
1824       VisitForStackValue(value);
1825       EmitSetHomeObjectIfNeeded(value, 2);
1826
1827       switch (property->kind()) {
1828         case ObjectLiteral::Property::CONSTANT:
1829         case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1830         case ObjectLiteral::Property::COMPUTED:
1831           if (property->emit_store()) {
1832             __ Mov(x0, Smi::FromInt(NONE));
1833             __ Push(x0);
1834             __ CallRuntime(Runtime::kDefineDataPropertyUnchecked, 4);
1835           } else {
1836             __ Drop(3);
1837           }
1838           break;
1839
1840         case ObjectLiteral::Property::PROTOTYPE:
1841           UNREACHABLE();
1842           break;
1843
1844         case ObjectLiteral::Property::GETTER:
1845           __ Mov(x0, Smi::FromInt(NONE));
1846           __ Push(x0);
1847           __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
1848           break;
1849
1850         case ObjectLiteral::Property::SETTER:
1851           __ Mov(x0, Smi::FromInt(NONE));
1852           __ Push(x0);
1853           __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
1854           break;
1855       }
1856     }
1857   }
1858
1859   if (expr->has_function()) {
1860     DCHECK(result_saved);
1861     __ Peek(x0, 0);
1862     __ Push(x0);
1863     __ CallRuntime(Runtime::kToFastProperties, 1);
1864   }
1865
1866   if (result_saved) {
1867     context()->PlugTOS();
1868   } else {
1869     context()->Plug(x0);
1870   }
1871 }
1872
1873
1874 void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1875   Comment cmnt(masm_, "[ ArrayLiteral");
1876
1877   expr->BuildConstantElements(isolate());
1878   Handle<FixedArray> constant_elements = expr->constant_elements();
1879   bool has_fast_elements =
1880       IsFastObjectElementsKind(expr->constant_elements_kind());
1881
1882   AllocationSiteMode allocation_site_mode = TRACK_ALLOCATION_SITE;
1883   if (has_fast_elements && !FLAG_allocation_site_pretenuring) {
1884     // If the only customer of allocation sites is transitioning, then
1885     // we can turn it off if we don't have anywhere else to transition to.
1886     allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
1887   }
1888
1889   __ Ldr(x3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1890   __ Ldr(x3, FieldMemOperand(x3, JSFunction::kLiteralsOffset));
1891   __ Mov(x2, Smi::FromInt(expr->literal_index()));
1892   __ Mov(x1, Operand(constant_elements));
1893   if (MustCreateArrayLiteralWithRuntime(expr)) {
1894     __ Mov(x0, Smi::FromInt(expr->ComputeFlags()));
1895     __ Push(x3, x2, x1, x0);
1896     __ CallRuntime(Runtime::kCreateArrayLiteral, 4);
1897   } else {
1898     FastCloneShallowArrayStub stub(isolate(), allocation_site_mode);
1899     __ CallStub(&stub);
1900   }
1901   PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1902
1903   bool result_saved = false;  // Is the result saved to the stack?
1904   ZoneList<Expression*>* subexprs = expr->values();
1905   int length = subexprs->length();
1906
1907   // Emit code to evaluate all the non-constant subexpressions and to store
1908   // them into the newly cloned array.
1909   for (int i = 0; i < length; i++) {
1910     Expression* subexpr = subexprs->at(i);
1911     // If the subexpression is a literal or a simple materialized literal it
1912     // is already set in the cloned array.
1913     if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
1914
1915     if (!result_saved) {
1916       __ Mov(x1, Smi::FromInt(expr->literal_index()));
1917       __ Push(x0, x1);
1918       result_saved = true;
1919     }
1920     VisitForAccumulatorValue(subexpr);
1921
1922     if (has_fast_elements) {
1923       int offset = FixedArray::kHeaderSize + (i * kPointerSize);
1924       __ Peek(x6, kPointerSize);  // Copy of array literal.
1925       __ Ldr(x1, FieldMemOperand(x6, JSObject::kElementsOffset));
1926       __ Str(result_register(), FieldMemOperand(x1, offset));
1927       // Update the write barrier for the array store.
1928       __ RecordWriteField(x1, offset, result_register(), x10,
1929                           kLRHasBeenSaved, kDontSaveFPRegs,
1930                           EMIT_REMEMBERED_SET, INLINE_SMI_CHECK);
1931     } else {
1932       __ Mov(x3, Smi::FromInt(i));
1933       StoreArrayLiteralElementStub stub(isolate());
1934       __ CallStub(&stub);
1935     }
1936
1937     PrepareForBailoutForId(expr->GetIdForElement(i), NO_REGISTERS);
1938   }
1939
1940   if (result_saved) {
1941     __ Drop(1);   // literal index
1942     context()->PlugTOS();
1943   } else {
1944     context()->Plug(x0);
1945   }
1946 }
1947
1948
1949 void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1950   DCHECK(expr->target()->IsValidReferenceExpression());
1951
1952   Comment cmnt(masm_, "[ Assignment");
1953
1954   Property* property = expr->target()->AsProperty();
1955   LhsKind assign_type = GetAssignType(property);
1956
1957   // Evaluate LHS expression.
1958   switch (assign_type) {
1959     case VARIABLE:
1960       // Nothing to do here.
1961       break;
1962     case NAMED_PROPERTY:
1963       if (expr->is_compound()) {
1964         // We need the receiver both on the stack and in the register.
1965         VisitForStackValue(property->obj());
1966         __ Peek(LoadDescriptor::ReceiverRegister(), 0);
1967       } else {
1968         VisitForStackValue(property->obj());
1969       }
1970       break;
1971     case NAMED_SUPER_PROPERTY:
1972       VisitForStackValue(property->obj()->AsSuperReference()->this_var());
1973       EmitLoadHomeObject(property->obj()->AsSuperReference());
1974       __ Push(result_register());
1975       if (expr->is_compound()) {
1976         const Register scratch = x10;
1977         __ Peek(scratch, kPointerSize);
1978         __ Push(scratch, result_register());
1979       }
1980       break;
1981     case KEYED_SUPER_PROPERTY:
1982       VisitForStackValue(property->obj()->AsSuperReference()->this_var());
1983       EmitLoadHomeObject(property->obj()->AsSuperReference());
1984       __ Push(result_register());
1985       VisitForAccumulatorValue(property->key());
1986       __ Push(result_register());
1987       if (expr->is_compound()) {
1988         const Register scratch1 = x10;
1989         const Register scratch2 = x11;
1990         __ Peek(scratch1, 2 * kPointerSize);
1991         __ Peek(scratch2, kPointerSize);
1992         __ Push(scratch1, scratch2, result_register());
1993       }
1994       break;
1995     case KEYED_PROPERTY:
1996       if (expr->is_compound()) {
1997         VisitForStackValue(property->obj());
1998         VisitForStackValue(property->key());
1999         __ Peek(LoadDescriptor::ReceiverRegister(), 1 * kPointerSize);
2000         __ Peek(LoadDescriptor::NameRegister(), 0);
2001       } else {
2002         VisitForStackValue(property->obj());
2003         VisitForStackValue(property->key());
2004       }
2005       break;
2006   }
2007
2008   // For compound assignments we need another deoptimization point after the
2009   // variable/property load.
2010   if (expr->is_compound()) {
2011     { AccumulatorValueContext context(this);
2012       switch (assign_type) {
2013         case VARIABLE:
2014           EmitVariableLoad(expr->target()->AsVariableProxy());
2015           PrepareForBailout(expr->target(), TOS_REG);
2016           break;
2017         case NAMED_PROPERTY:
2018           EmitNamedPropertyLoad(property);
2019           PrepareForBailoutForId(property->LoadId(), TOS_REG);
2020           break;
2021         case NAMED_SUPER_PROPERTY:
2022           EmitNamedSuperPropertyLoad(property);
2023           PrepareForBailoutForId(property->LoadId(), TOS_REG);
2024           break;
2025         case KEYED_SUPER_PROPERTY:
2026           EmitKeyedSuperPropertyLoad(property);
2027           PrepareForBailoutForId(property->LoadId(), TOS_REG);
2028           break;
2029         case KEYED_PROPERTY:
2030           EmitKeyedPropertyLoad(property);
2031           PrepareForBailoutForId(property->LoadId(), TOS_REG);
2032           break;
2033       }
2034     }
2035
2036     Token::Value op = expr->binary_op();
2037     __ Push(x0);  // Left operand goes on the stack.
2038     VisitForAccumulatorValue(expr->value());
2039
2040     SetSourcePosition(expr->position() + 1);
2041     AccumulatorValueContext context(this);
2042     if (ShouldInlineSmiCase(op)) {
2043       EmitInlineSmiBinaryOp(expr->binary_operation(),
2044                             op,
2045                             expr->target(),
2046                             expr->value());
2047     } else {
2048       EmitBinaryOp(expr->binary_operation(), op);
2049     }
2050
2051     // Deoptimization point in case the binary operation may have side effects.
2052     PrepareForBailout(expr->binary_operation(), TOS_REG);
2053   } else {
2054     VisitForAccumulatorValue(expr->value());
2055   }
2056
2057   // Record source position before possible IC call.
2058   SetSourcePosition(expr->position());
2059
2060   // Store the value.
2061   switch (assign_type) {
2062     case VARIABLE:
2063       EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
2064                              expr->op());
2065       PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2066       context()->Plug(x0);
2067       break;
2068     case NAMED_PROPERTY:
2069       EmitNamedPropertyAssignment(expr);
2070       break;
2071     case NAMED_SUPER_PROPERTY:
2072       EmitNamedSuperPropertyStore(property);
2073       context()->Plug(x0);
2074       break;
2075     case KEYED_SUPER_PROPERTY:
2076       EmitKeyedSuperPropertyStore(property);
2077       context()->Plug(x0);
2078       break;
2079     case KEYED_PROPERTY:
2080       EmitKeyedPropertyAssignment(expr);
2081       break;
2082   }
2083 }
2084
2085
2086 void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
2087   SetSourcePosition(prop->position());
2088   Literal* key = prop->key()->AsLiteral();
2089   DCHECK(!prop->IsSuperAccess());
2090
2091   __ Mov(LoadDescriptor::NameRegister(), Operand(key->value()));
2092   if (FLAG_vector_ics) {
2093     __ Mov(VectorLoadICDescriptor::SlotRegister(),
2094            SmiFromSlot(prop->PropertyFeedbackSlot()));
2095     CallLoadIC(NOT_CONTEXTUAL);
2096   } else {
2097     CallLoadIC(NOT_CONTEXTUAL, prop->PropertyFeedbackId());
2098   }
2099 }
2100
2101
2102 void FullCodeGenerator::EmitNamedSuperPropertyLoad(Property* prop) {
2103   // Stack: receiver, home_object.
2104   SetSourcePosition(prop->position());
2105   Literal* key = prop->key()->AsLiteral();
2106   DCHECK(!key->value()->IsSmi());
2107   DCHECK(prop->IsSuperAccess());
2108
2109   __ Push(key->value());
2110   __ CallRuntime(Runtime::kLoadFromSuper, 3);
2111 }
2112
2113
2114 void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
2115   SetSourcePosition(prop->position());
2116   // Call keyed load IC. It has arguments key and receiver in x0 and x1.
2117   Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate()).code();
2118   if (FLAG_vector_ics) {
2119     __ Mov(VectorLoadICDescriptor::SlotRegister(),
2120            SmiFromSlot(prop->PropertyFeedbackSlot()));
2121     CallIC(ic);
2122   } else {
2123     CallIC(ic, prop->PropertyFeedbackId());
2124   }
2125 }
2126
2127
2128 void FullCodeGenerator::EmitKeyedSuperPropertyLoad(Property* prop) {
2129   // Stack: receiver, home_object, key.
2130   SetSourcePosition(prop->position());
2131
2132   __ CallRuntime(Runtime::kLoadKeyedFromSuper, 3);
2133 }
2134
2135
2136 void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
2137                                               Token::Value op,
2138                                               Expression* left_expr,
2139                                               Expression* right_expr) {
2140   Label done, both_smis, stub_call;
2141
2142   // Get the arguments.
2143   Register left = x1;
2144   Register right = x0;
2145   Register result = x0;
2146   __ Pop(left);
2147
2148   // Perform combined smi check on both operands.
2149   __ Orr(x10, left, right);
2150   JumpPatchSite patch_site(masm_);
2151   patch_site.EmitJumpIfSmi(x10, &both_smis);
2152
2153   __ Bind(&stub_call);
2154
2155   Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), op).code();
2156   {
2157     Assembler::BlockPoolsScope scope(masm_);
2158     CallIC(code, expr->BinaryOperationFeedbackId());
2159     patch_site.EmitPatchInfo();
2160   }
2161   __ B(&done);
2162
2163   __ Bind(&both_smis);
2164   // Smi case. This code works in the same way as the smi-smi case in the type
2165   // recording binary operation stub, see
2166   // BinaryOpStub::GenerateSmiSmiOperation for comments.
2167   // TODO(all): That doesn't exist any more. Where are the comments?
2168   //
2169   // The set of operations that needs to be supported here is controlled by
2170   // FullCodeGenerator::ShouldInlineSmiCase().
2171   switch (op) {
2172     case Token::SAR:
2173       __ Ubfx(right, right, kSmiShift, 5);
2174       __ Asr(result, left, right);
2175       __ Bic(result, result, kSmiShiftMask);
2176       break;
2177     case Token::SHL:
2178       __ Ubfx(right, right, kSmiShift, 5);
2179       __ Lsl(result, left, right);
2180       break;
2181     case Token::SHR:
2182       // If `left >>> right` >= 0x80000000, the result is not representable in a
2183       // signed 32-bit smi.
2184       __ Ubfx(right, right, kSmiShift, 5);
2185       __ Lsr(x10, left, right);
2186       __ Tbnz(x10, kXSignBit, &stub_call);
2187       __ Bic(result, x10, kSmiShiftMask);
2188       break;
2189     case Token::ADD:
2190       __ Adds(x10, left, right);
2191       __ B(vs, &stub_call);
2192       __ Mov(result, x10);
2193       break;
2194     case Token::SUB:
2195       __ Subs(x10, left, right);
2196       __ B(vs, &stub_call);
2197       __ Mov(result, x10);
2198       break;
2199     case Token::MUL: {
2200       Label not_minus_zero, done;
2201       STATIC_ASSERT(static_cast<unsigned>(kSmiShift) == (kXRegSizeInBits / 2));
2202       STATIC_ASSERT(kSmiTag == 0);
2203       __ Smulh(x10, left, right);
2204       __ Cbnz(x10, &not_minus_zero);
2205       __ Eor(x11, left, right);
2206       __ Tbnz(x11, kXSignBit, &stub_call);
2207       __ Mov(result, x10);
2208       __ B(&done);
2209       __ Bind(&not_minus_zero);
2210       __ Cls(x11, x10);
2211       __ Cmp(x11, kXRegSizeInBits - kSmiShift);
2212       __ B(lt, &stub_call);
2213       __ SmiTag(result, x10);
2214       __ Bind(&done);
2215       break;
2216     }
2217     case Token::BIT_OR:
2218       __ Orr(result, left, right);
2219       break;
2220     case Token::BIT_AND:
2221       __ And(result, left, right);
2222       break;
2223     case Token::BIT_XOR:
2224       __ Eor(result, left, right);
2225       break;
2226     default:
2227       UNREACHABLE();
2228   }
2229
2230   __ Bind(&done);
2231   context()->Plug(x0);
2232 }
2233
2234
2235 void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr, Token::Value op) {
2236   __ Pop(x1);
2237   Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), op).code();
2238   JumpPatchSite patch_site(masm_);    // Unbound, signals no inlined smi code.
2239   {
2240     Assembler::BlockPoolsScope scope(masm_);
2241     CallIC(code, expr->BinaryOperationFeedbackId());
2242     patch_site.EmitPatchInfo();
2243   }
2244   context()->Plug(x0);
2245 }
2246
2247
2248 void FullCodeGenerator::EmitClassDefineProperties(ClassLiteral* lit) {
2249   // Constructor is in x0.
2250   DCHECK(lit != NULL);
2251   __ push(x0);
2252
2253   // No access check is needed here since the constructor is created by the
2254   // class literal.
2255   Register scratch = x1;
2256   __ Ldr(scratch,
2257          FieldMemOperand(x0, JSFunction::kPrototypeOrInitialMapOffset));
2258   __ Push(scratch);
2259
2260   for (int i = 0; i < lit->properties()->length(); i++) {
2261     ObjectLiteral::Property* property = lit->properties()->at(i);
2262     Expression* value = property->value();
2263
2264     if (property->is_static()) {
2265       __ Peek(scratch, kPointerSize);  // constructor
2266     } else {
2267       __ Peek(scratch, 0);  // prototype
2268     }
2269     __ Push(scratch);
2270     EmitPropertyKey(property, lit->GetIdForProperty(i));
2271
2272     // The static prototype property is read only. We handle the non computed
2273     // property name case in the parser. Since this is the only case where we
2274     // need to check for an own read only property we special case this so we do
2275     // not need to do this for every property.
2276     if (property->is_static() && property->is_computed_name()) {
2277       __ CallRuntime(Runtime::kThrowIfStaticPrototype, 1);
2278       __ Push(x0);
2279     }
2280
2281     VisitForStackValue(value);
2282     EmitSetHomeObjectIfNeeded(value, 2);
2283
2284     switch (property->kind()) {
2285       case ObjectLiteral::Property::CONSTANT:
2286       case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2287       case ObjectLiteral::Property::PROTOTYPE:
2288         UNREACHABLE();
2289       case ObjectLiteral::Property::COMPUTED:
2290         __ CallRuntime(Runtime::kDefineClassMethod, 3);
2291         break;
2292
2293       case ObjectLiteral::Property::GETTER:
2294         __ Mov(x0, Smi::FromInt(DONT_ENUM));
2295         __ Push(x0);
2296         __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
2297         break;
2298
2299       case ObjectLiteral::Property::SETTER:
2300         __ Mov(x0, Smi::FromInt(DONT_ENUM));
2301         __ Push(x0);
2302         __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
2303         break;
2304
2305       default:
2306         UNREACHABLE();
2307     }
2308   }
2309
2310   // prototype
2311   __ CallRuntime(Runtime::kToFastProperties, 1);
2312
2313   // constructor
2314   __ CallRuntime(Runtime::kToFastProperties, 1);
2315 }
2316
2317
2318 void FullCodeGenerator::EmitAssignment(Expression* expr) {
2319   DCHECK(expr->IsValidReferenceExpression());
2320
2321   Property* prop = expr->AsProperty();
2322   LhsKind assign_type = GetAssignType(prop);
2323
2324   switch (assign_type) {
2325     case VARIABLE: {
2326       Variable* var = expr->AsVariableProxy()->var();
2327       EffectContext context(this);
2328       EmitVariableAssignment(var, Token::ASSIGN);
2329       break;
2330     }
2331     case NAMED_PROPERTY: {
2332       __ Push(x0);  // Preserve value.
2333       VisitForAccumulatorValue(prop->obj());
2334       // TODO(all): We could introduce a VisitForRegValue(reg, expr) to avoid
2335       // this copy.
2336       __ Mov(StoreDescriptor::ReceiverRegister(), x0);
2337       __ Pop(StoreDescriptor::ValueRegister());  // Restore value.
2338       __ Mov(StoreDescriptor::NameRegister(),
2339              Operand(prop->key()->AsLiteral()->value()));
2340       CallStoreIC();
2341       break;
2342     }
2343     case NAMED_SUPER_PROPERTY: {
2344       __ Push(x0);
2345       VisitForStackValue(prop->obj()->AsSuperReference()->this_var());
2346       EmitLoadHomeObject(prop->obj()->AsSuperReference());
2347       // stack: value, this; x0: home_object
2348       Register scratch = x10;
2349       Register scratch2 = x11;
2350       __ mov(scratch, result_register());  // home_object
2351       __ Peek(x0, kPointerSize);           // value
2352       __ Peek(scratch2, 0);                // this
2353       __ Poke(scratch2, kPointerSize);     // this
2354       __ Poke(scratch, 0);                 // home_object
2355       // stack: this, home_object; x0: value
2356       EmitNamedSuperPropertyStore(prop);
2357       break;
2358     }
2359     case KEYED_SUPER_PROPERTY: {
2360       __ Push(x0);
2361       VisitForStackValue(prop->obj()->AsSuperReference()->this_var());
2362       EmitLoadHomeObject(prop->obj()->AsSuperReference());
2363       __ Push(result_register());
2364       VisitForAccumulatorValue(prop->key());
2365       Register scratch = x10;
2366       Register scratch2 = x11;
2367       __ Peek(scratch2, 2 * kPointerSize);  // value
2368       // stack: value, this, home_object; x0: key, x11: value
2369       __ Peek(scratch, kPointerSize);  // this
2370       __ Poke(scratch, 2 * kPointerSize);
2371       __ Peek(scratch, 0);  // home_object
2372       __ Poke(scratch, kPointerSize);
2373       __ Poke(x0, 0);
2374       __ Move(x0, scratch2);
2375       // stack: this, home_object, key; x0: value.
2376       EmitKeyedSuperPropertyStore(prop);
2377       break;
2378     }
2379     case KEYED_PROPERTY: {
2380       __ Push(x0);  // Preserve value.
2381       VisitForStackValue(prop->obj());
2382       VisitForAccumulatorValue(prop->key());
2383       __ Mov(StoreDescriptor::NameRegister(), x0);
2384       __ Pop(StoreDescriptor::ReceiverRegister(),
2385              StoreDescriptor::ValueRegister());
2386       Handle<Code> ic =
2387           CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2388       CallIC(ic);
2389       break;
2390     }
2391   }
2392   context()->Plug(x0);
2393 }
2394
2395
2396 void FullCodeGenerator::EmitStoreToStackLocalOrContextSlot(
2397     Variable* var, MemOperand location) {
2398   __ Str(result_register(), location);
2399   if (var->IsContextSlot()) {
2400     // RecordWrite may destroy all its register arguments.
2401     __ Mov(x10, result_register());
2402     int offset = Context::SlotOffset(var->index());
2403     __ RecordWriteContextSlot(
2404         x1, offset, x10, x11, kLRHasBeenSaved, kDontSaveFPRegs);
2405   }
2406 }
2407
2408
2409 void FullCodeGenerator::EmitVariableAssignment(Variable* var,
2410                                                Token::Value op) {
2411   ASM_LOCATION("FullCodeGenerator::EmitVariableAssignment");
2412   if (var->IsUnallocated()) {
2413     // Global var, const, or let.
2414     __ Mov(StoreDescriptor::NameRegister(), Operand(var->name()));
2415     __ Ldr(StoreDescriptor::ReceiverRegister(), GlobalObjectMemOperand());
2416     CallStoreIC();
2417
2418   } else if (var->mode() == LET && op != Token::INIT_LET) {
2419     // Non-initializing assignment to let variable needs a write barrier.
2420     DCHECK(!var->IsLookupSlot());
2421     DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2422     Label assign;
2423     MemOperand location = VarOperand(var, x1);
2424     __ Ldr(x10, location);
2425     __ JumpIfNotRoot(x10, Heap::kTheHoleValueRootIndex, &assign);
2426     __ Mov(x10, Operand(var->name()));
2427     __ Push(x10);
2428     __ CallRuntime(Runtime::kThrowReferenceError, 1);
2429     // Perform the assignment.
2430     __ Bind(&assign);
2431     EmitStoreToStackLocalOrContextSlot(var, location);
2432
2433   } else if (var->mode() == CONST && op != Token::INIT_CONST) {
2434     // Assignment to const variable needs a write barrier.
2435     DCHECK(!var->IsLookupSlot());
2436     DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2437     Label const_error;
2438     MemOperand location = VarOperand(var, x1);
2439     __ Ldr(x10, location);
2440     __ JumpIfNotRoot(x10, Heap::kTheHoleValueRootIndex, &const_error);
2441     __ Mov(x10, Operand(var->name()));
2442     __ Push(x10);
2443     __ CallRuntime(Runtime::kThrowReferenceError, 1);
2444     __ Bind(&const_error);
2445     __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2446
2447   } else if (!var->is_const_mode() || op == Token::INIT_CONST) {
2448     if (var->IsLookupSlot()) {
2449       // Assignment to var.
2450       __ Mov(x11, Operand(var->name()));
2451       __ Mov(x10, Smi::FromInt(language_mode()));
2452       // jssp[0]  : mode.
2453       // jssp[8]  : name.
2454       // jssp[16] : context.
2455       // jssp[24] : value.
2456       __ Push(x0, cp, x11, x10);
2457       __ CallRuntime(Runtime::kStoreLookupSlot, 4);
2458     } else {
2459       // Assignment to var or initializing assignment to let/const in harmony
2460       // mode.
2461       DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2462       MemOperand location = VarOperand(var, x1);
2463       if (FLAG_debug_code && op == Token::INIT_LET) {
2464         __ Ldr(x10, location);
2465         __ CompareRoot(x10, Heap::kTheHoleValueRootIndex);
2466         __ Check(eq, kLetBindingReInitialization);
2467       }
2468       EmitStoreToStackLocalOrContextSlot(var, location);
2469     }
2470
2471   } else if (op == Token::INIT_CONST_LEGACY) {
2472     // Const initializers need a write barrier.
2473     DCHECK(var->mode() == CONST_LEGACY);
2474     DCHECK(!var->IsParameter());  // No const parameters.
2475     if (var->IsLookupSlot()) {
2476       __ Mov(x1, Operand(var->name()));
2477       __ Push(x0, cp, x1);
2478       __ CallRuntime(Runtime::kInitializeLegacyConstLookupSlot, 3);
2479     } else {
2480       DCHECK(var->IsStackLocal() || var->IsContextSlot());
2481       Label skip;
2482       MemOperand location = VarOperand(var, x1);
2483       __ Ldr(x10, location);
2484       __ JumpIfNotRoot(x10, Heap::kTheHoleValueRootIndex, &skip);
2485       EmitStoreToStackLocalOrContextSlot(var, location);
2486       __ Bind(&skip);
2487     }
2488
2489   } else {
2490     DCHECK(var->mode() == CONST_LEGACY && op != Token::INIT_CONST_LEGACY);
2491     if (is_strict(language_mode())) {
2492       __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2493     }
2494     // Silently ignore store in sloppy mode.
2495   }
2496 }
2497
2498
2499 void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2500   ASM_LOCATION("FullCodeGenerator::EmitNamedPropertyAssignment");
2501   // Assignment to a property, using a named store IC.
2502   Property* prop = expr->target()->AsProperty();
2503   DCHECK(prop != NULL);
2504   DCHECK(prop->key()->IsLiteral());
2505
2506   // Record source code position before IC call.
2507   SetSourcePosition(expr->position());
2508   __ Mov(StoreDescriptor::NameRegister(),
2509          Operand(prop->key()->AsLiteral()->value()));
2510   __ Pop(StoreDescriptor::ReceiverRegister());
2511   CallStoreIC(expr->AssignmentFeedbackId());
2512
2513   PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2514   context()->Plug(x0);
2515 }
2516
2517
2518 void FullCodeGenerator::EmitNamedSuperPropertyStore(Property* prop) {
2519   // Assignment to named property of super.
2520   // x0 : value
2521   // stack : receiver ('this'), home_object
2522   DCHECK(prop != NULL);
2523   Literal* key = prop->key()->AsLiteral();
2524   DCHECK(key != NULL);
2525
2526   __ Push(key->value());
2527   __ Push(x0);
2528   __ CallRuntime((is_strict(language_mode()) ? Runtime::kStoreToSuper_Strict
2529                                              : Runtime::kStoreToSuper_Sloppy),
2530                  4);
2531 }
2532
2533
2534 void FullCodeGenerator::EmitKeyedSuperPropertyStore(Property* prop) {
2535   // Assignment to named property of super.
2536   // x0 : value
2537   // stack : receiver ('this'), home_object, key
2538   DCHECK(prop != NULL);
2539
2540   __ Push(x0);
2541   __ CallRuntime(
2542       (is_strict(language_mode()) ? Runtime::kStoreKeyedToSuper_Strict
2543                                   : Runtime::kStoreKeyedToSuper_Sloppy),
2544       4);
2545 }
2546
2547
2548 void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2549   ASM_LOCATION("FullCodeGenerator::EmitKeyedPropertyAssignment");
2550   // Assignment to a property, using a keyed store IC.
2551
2552   // Record source code position before IC call.
2553   SetSourcePosition(expr->position());
2554   // TODO(all): Could we pass this in registers rather than on the stack?
2555   __ Pop(StoreDescriptor::NameRegister(), StoreDescriptor::ReceiverRegister());
2556   DCHECK(StoreDescriptor::ValueRegister().is(x0));
2557
2558   Handle<Code> ic =
2559       CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2560   CallIC(ic, expr->AssignmentFeedbackId());
2561
2562   PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2563   context()->Plug(x0);
2564 }
2565
2566
2567 void FullCodeGenerator::VisitProperty(Property* expr) {
2568   Comment cmnt(masm_, "[ Property");
2569   Expression* key = expr->key();
2570
2571   if (key->IsPropertyName()) {
2572     if (!expr->IsSuperAccess()) {
2573       VisitForAccumulatorValue(expr->obj());
2574       __ Move(LoadDescriptor::ReceiverRegister(), x0);
2575       EmitNamedPropertyLoad(expr);
2576     } else {
2577       VisitForStackValue(expr->obj()->AsSuperReference()->this_var());
2578       EmitLoadHomeObject(expr->obj()->AsSuperReference());
2579       __ Push(result_register());
2580       EmitNamedSuperPropertyLoad(expr);
2581     }
2582   } else {
2583     if (!expr->IsSuperAccess()) {
2584       VisitForStackValue(expr->obj());
2585       VisitForAccumulatorValue(expr->key());
2586       __ Move(LoadDescriptor::NameRegister(), x0);
2587       __ Pop(LoadDescriptor::ReceiverRegister());
2588       EmitKeyedPropertyLoad(expr);
2589     } else {
2590       VisitForStackValue(expr->obj()->AsSuperReference()->this_var());
2591       EmitLoadHomeObject(expr->obj()->AsSuperReference());
2592       __ Push(result_register());
2593       VisitForStackValue(expr->key());
2594       EmitKeyedSuperPropertyLoad(expr);
2595     }
2596   }
2597   PrepareForBailoutForId(expr->LoadId(), TOS_REG);
2598   context()->Plug(x0);
2599 }
2600
2601
2602 void FullCodeGenerator::CallIC(Handle<Code> code,
2603                                TypeFeedbackId ast_id) {
2604   ic_total_count_++;
2605   // All calls must have a predictable size in full-codegen code to ensure that
2606   // the debugger can patch them correctly.
2607   __ Call(code, RelocInfo::CODE_TARGET, ast_id);
2608 }
2609
2610
2611 // Code common for calls using the IC.
2612 void FullCodeGenerator::EmitCallWithLoadIC(Call* expr) {
2613   Expression* callee = expr->expression();
2614
2615   CallICState::CallType call_type =
2616       callee->IsVariableProxy() ? CallICState::FUNCTION : CallICState::METHOD;
2617
2618   // Get the target function.
2619   if (call_type == CallICState::FUNCTION) {
2620     { StackValueContext context(this);
2621       EmitVariableLoad(callee->AsVariableProxy());
2622       PrepareForBailout(callee, NO_REGISTERS);
2623     }
2624     // Push undefined as receiver. This is patched in the method prologue if it
2625     // is a sloppy mode method.
2626     {
2627       UseScratchRegisterScope temps(masm_);
2628       Register temp = temps.AcquireX();
2629       __ LoadRoot(temp, Heap::kUndefinedValueRootIndex);
2630       __ Push(temp);
2631     }
2632   } else {
2633     // Load the function from the receiver.
2634     DCHECK(callee->IsProperty());
2635     DCHECK(!callee->AsProperty()->IsSuperAccess());
2636     __ Peek(LoadDescriptor::ReceiverRegister(), 0);
2637     EmitNamedPropertyLoad(callee->AsProperty());
2638     PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2639     // Push the target function under the receiver.
2640     __ Pop(x10);
2641     __ Push(x0, x10);
2642   }
2643
2644   EmitCall(expr, call_type);
2645 }
2646
2647
2648 void FullCodeGenerator::EmitSuperCallWithLoadIC(Call* expr) {
2649   Expression* callee = expr->expression();
2650   DCHECK(callee->IsProperty());
2651   Property* prop = callee->AsProperty();
2652   DCHECK(prop->IsSuperAccess());
2653
2654   SetSourcePosition(prop->position());
2655   Literal* key = prop->key()->AsLiteral();
2656   DCHECK(!key->value()->IsSmi());
2657
2658   // Load the function from the receiver.
2659   const Register scratch = x10;
2660   SuperReference* super_ref = callee->AsProperty()->obj()->AsSuperReference();
2661   EmitLoadHomeObject(super_ref);
2662   __ Push(x0);
2663   VisitForAccumulatorValue(super_ref->this_var());
2664   __ Push(x0);
2665   __ Peek(scratch, kPointerSize);
2666   __ Push(x0, scratch);
2667   __ Push(key->value());
2668
2669   // Stack here:
2670   //  - home_object
2671   //  - this (receiver)
2672   //  - this (receiver) <-- LoadFromSuper will pop here and below.
2673   //  - home_object
2674   //  - key
2675   __ CallRuntime(Runtime::kLoadFromSuper, 3);
2676
2677   // Replace home_object with target function.
2678   __ Poke(x0, kPointerSize);
2679
2680   // Stack here:
2681   // - target function
2682   // - this (receiver)
2683   EmitCall(expr, CallICState::METHOD);
2684 }
2685
2686
2687 // Code common for calls using the IC.
2688 void FullCodeGenerator::EmitKeyedCallWithLoadIC(Call* expr,
2689                                                 Expression* key) {
2690   // Load the key.
2691   VisitForAccumulatorValue(key);
2692
2693   Expression* callee = expr->expression();
2694
2695   // Load the function from the receiver.
2696   DCHECK(callee->IsProperty());
2697   __ Peek(LoadDescriptor::ReceiverRegister(), 0);
2698   __ Move(LoadDescriptor::NameRegister(), x0);
2699   EmitKeyedPropertyLoad(callee->AsProperty());
2700   PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2701
2702   // Push the target function under the receiver.
2703   __ Pop(x10);
2704   __ Push(x0, x10);
2705
2706   EmitCall(expr, CallICState::METHOD);
2707 }
2708
2709
2710 void FullCodeGenerator::EmitKeyedSuperCallWithLoadIC(Call* expr) {
2711   Expression* callee = expr->expression();
2712   DCHECK(callee->IsProperty());
2713   Property* prop = callee->AsProperty();
2714   DCHECK(prop->IsSuperAccess());
2715
2716   SetSourcePosition(prop->position());
2717
2718   // Load the function from the receiver.
2719   const Register scratch = x10;
2720   SuperReference* super_ref = callee->AsProperty()->obj()->AsSuperReference();
2721   EmitLoadHomeObject(super_ref);
2722   __ Push(x0);
2723   VisitForAccumulatorValue(super_ref->this_var());
2724   __ Push(x0);
2725   __ Peek(scratch, kPointerSize);
2726   __ Push(x0, scratch);
2727   VisitForStackValue(prop->key());
2728
2729   // Stack here:
2730   //  - home_object
2731   //  - this (receiver)
2732   //  - this (receiver) <-- LoadKeyedFromSuper will pop here and below.
2733   //  - home_object
2734   //  - key
2735   __ CallRuntime(Runtime::kLoadKeyedFromSuper, 3);
2736
2737   // Replace home_object with target function.
2738   __ Poke(x0, kPointerSize);
2739
2740   // Stack here:
2741   // - target function
2742   // - this (receiver)
2743   EmitCall(expr, CallICState::METHOD);
2744 }
2745
2746
2747 void FullCodeGenerator::EmitCall(Call* expr, CallICState::CallType call_type) {
2748   // Load the arguments.
2749   ZoneList<Expression*>* args = expr->arguments();
2750   int arg_count = args->length();
2751   { PreservePositionScope scope(masm()->positions_recorder());
2752     for (int i = 0; i < arg_count; i++) {
2753       VisitForStackValue(args->at(i));
2754     }
2755   }
2756   // Record source position of the IC call.
2757   SetSourcePosition(expr->position());
2758
2759   Handle<Code> ic = CodeFactory::CallIC(isolate(), arg_count, call_type).code();
2760   __ Mov(x3, SmiFromSlot(expr->CallFeedbackICSlot()));
2761   __ Peek(x1, (arg_count + 1) * kXRegSize);
2762   // Don't assign a type feedback id to the IC, since type feedback is provided
2763   // by the vector above.
2764   CallIC(ic);
2765
2766   RecordJSReturnSite(expr);
2767   // Restore context register.
2768   __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2769   context()->DropAndPlug(1, x0);
2770 }
2771
2772
2773 void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
2774   ASM_LOCATION("FullCodeGenerator::EmitResolvePossiblyDirectEval");
2775   // Prepare to push a copy of the first argument or undefined if it doesn't
2776   // exist.
2777   if (arg_count > 0) {
2778     __ Peek(x9, arg_count * kXRegSize);
2779   } else {
2780     __ LoadRoot(x9, Heap::kUndefinedValueRootIndex);
2781   }
2782
2783   __ Ldr(x10, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
2784   // Prepare to push the receiver of the enclosing function.
2785   int receiver_offset = 2 + info_->scope()->num_parameters();
2786   __ Ldr(x11, MemOperand(fp, receiver_offset * kPointerSize));
2787
2788   // Prepare to push the language mode.
2789   __ Mov(x12, Smi::FromInt(language_mode()));
2790   // Prepare to push the start position of the scope the calls resides in.
2791   __ Mov(x13, Smi::FromInt(scope()->start_position()));
2792
2793   // Push.
2794   __ Push(x9, x10, x11, x12, x13);
2795
2796   // Do the runtime call.
2797   __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 6);
2798 }
2799
2800
2801 void FullCodeGenerator::EmitLoadSuperConstructor() {
2802   __ ldr(x0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
2803   __ Push(x0);
2804   __ CallRuntime(Runtime::kGetPrototype, 1);
2805 }
2806
2807
2808 void FullCodeGenerator::VisitCall(Call* expr) {
2809 #ifdef DEBUG
2810   // We want to verify that RecordJSReturnSite gets called on all paths
2811   // through this function.  Avoid early returns.
2812   expr->return_is_recorded_ = false;
2813 #endif
2814
2815   Comment cmnt(masm_, "[ Call");
2816   Expression* callee = expr->expression();
2817   Call::CallType call_type = expr->GetCallType(isolate());
2818
2819   if (call_type == Call::POSSIBLY_EVAL_CALL) {
2820     // In a call to eval, we first call RuntimeHidden_ResolvePossiblyDirectEval
2821     // to resolve the function we need to call and the receiver of the
2822     // call.  Then we call the resolved function using the given
2823     // arguments.
2824     ZoneList<Expression*>* args = expr->arguments();
2825     int arg_count = args->length();
2826
2827     {
2828       PreservePositionScope pos_scope(masm()->positions_recorder());
2829       VisitForStackValue(callee);
2830       __ LoadRoot(x10, Heap::kUndefinedValueRootIndex);
2831       __ Push(x10);  // Reserved receiver slot.
2832
2833       // Push the arguments.
2834       for (int i = 0; i < arg_count; i++) {
2835         VisitForStackValue(args->at(i));
2836       }
2837
2838       // Push a copy of the function (found below the arguments) and
2839       // resolve eval.
2840       __ Peek(x10, (arg_count + 1) * kPointerSize);
2841       __ Push(x10);
2842       EmitResolvePossiblyDirectEval(arg_count);
2843
2844       // The runtime call returns a pair of values in x0 (function) and
2845       // x1 (receiver). Touch up the stack with the right values.
2846       __ PokePair(x1, x0, arg_count * kPointerSize);
2847
2848       PrepareForBailoutForId(expr->EvalOrLookupId(), NO_REGISTERS);
2849     }
2850
2851     // Record source position for debugger.
2852     SetSourcePosition(expr->position());
2853
2854     // Call the evaluated function.
2855     CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
2856     __ Peek(x1, (arg_count + 1) * kXRegSize);
2857     __ CallStub(&stub);
2858     RecordJSReturnSite(expr);
2859     // Restore context register.
2860     __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2861     context()->DropAndPlug(1, x0);
2862
2863   } else if (call_type == Call::GLOBAL_CALL) {
2864     EmitCallWithLoadIC(expr);
2865
2866   } else if (call_type == Call::LOOKUP_SLOT_CALL) {
2867     // Call to a lookup slot (dynamically introduced variable).
2868     VariableProxy* proxy = callee->AsVariableProxy();
2869     Label slow, done;
2870
2871     { PreservePositionScope scope(masm()->positions_recorder());
2872       // Generate code for loading from variables potentially shadowed
2873       // by eval-introduced variables.
2874       EmitDynamicLookupFastCase(proxy, NOT_INSIDE_TYPEOF, &slow, &done);
2875     }
2876
2877     __ Bind(&slow);
2878     // Call the runtime to find the function to call (returned in x0)
2879     // and the object holding it (returned in x1).
2880     __ Mov(x10, Operand(proxy->name()));
2881     __ Push(context_register(), x10);
2882     __ CallRuntime(Runtime::kLoadLookupSlot, 2);
2883     __ Push(x0, x1);  // Receiver, function.
2884     PrepareForBailoutForId(expr->EvalOrLookupId(), NO_REGISTERS);
2885
2886     // If fast case code has been generated, emit code to push the
2887     // function and receiver and have the slow path jump around this
2888     // code.
2889     if (done.is_linked()) {
2890       Label call;
2891       __ B(&call);
2892       __ Bind(&done);
2893       // Push function.
2894       // The receiver is implicitly the global receiver. Indicate this
2895       // by passing the undefined to the call function stub.
2896       __ LoadRoot(x1, Heap::kUndefinedValueRootIndex);
2897       __ Push(x0, x1);
2898       __ Bind(&call);
2899     }
2900
2901     // The receiver is either the global receiver or an object found
2902     // by LoadContextSlot.
2903     EmitCall(expr);
2904   } else if (call_type == Call::PROPERTY_CALL) {
2905     Property* property = callee->AsProperty();
2906     bool is_named_call = property->key()->IsPropertyName();
2907     if (property->IsSuperAccess()) {
2908       if (is_named_call) {
2909         EmitSuperCallWithLoadIC(expr);
2910       } else {
2911         EmitKeyedSuperCallWithLoadIC(expr);
2912       }
2913     } else {
2914       {
2915         PreservePositionScope scope(masm()->positions_recorder());
2916         VisitForStackValue(property->obj());
2917       }
2918       if (is_named_call) {
2919         EmitCallWithLoadIC(expr);
2920       } else {
2921         EmitKeyedCallWithLoadIC(expr, property->key());
2922       }
2923     }
2924   } else if (call_type == Call::SUPER_CALL) {
2925     EmitSuperConstructorCall(expr);
2926   } else {
2927     DCHECK(call_type == Call::OTHER_CALL);
2928     // Call to an arbitrary expression not handled specially above.
2929     { PreservePositionScope scope(masm()->positions_recorder());
2930       VisitForStackValue(callee);
2931     }
2932     __ LoadRoot(x1, Heap::kUndefinedValueRootIndex);
2933     __ Push(x1);
2934     // Emit function call.
2935     EmitCall(expr);
2936   }
2937
2938 #ifdef DEBUG
2939   // RecordJSReturnSite should have been called.
2940   DCHECK(expr->return_is_recorded_);
2941 #endif
2942 }
2943
2944
2945 void FullCodeGenerator::VisitCallNew(CallNew* expr) {
2946   Comment cmnt(masm_, "[ CallNew");
2947   // According to ECMA-262, section 11.2.2, page 44, the function
2948   // expression in new calls must be evaluated before the
2949   // arguments.
2950
2951   // Push constructor on the stack.  If it's not a function it's used as
2952   // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2953   // ignored.
2954   DCHECK(!expr->expression()->IsSuperReference());
2955   VisitForStackValue(expr->expression());
2956
2957   // Push the arguments ("left-to-right") on the stack.
2958   ZoneList<Expression*>* args = expr->arguments();
2959   int arg_count = args->length();
2960   for (int i = 0; i < arg_count; i++) {
2961     VisitForStackValue(args->at(i));
2962   }
2963
2964   // Call the construct call builtin that handles allocation and
2965   // constructor invocation.
2966   SetSourcePosition(expr->position());
2967
2968   // Load function and argument count into x1 and x0.
2969   __ Mov(x0, arg_count);
2970   __ Peek(x1, arg_count * kXRegSize);
2971
2972   // Record call targets in unoptimized code.
2973   if (FLAG_pretenuring_call_new) {
2974     EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
2975     DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
2976            expr->CallNewFeedbackSlot().ToInt() + 1);
2977   }
2978
2979   __ LoadObject(x2, FeedbackVector());
2980   __ Mov(x3, SmiFromSlot(expr->CallNewFeedbackSlot()));
2981
2982   CallConstructStub stub(isolate(), RECORD_CONSTRUCTOR_TARGET);
2983   __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
2984   PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
2985   context()->Plug(x0);
2986 }
2987
2988
2989 void FullCodeGenerator::EmitSuperConstructorCall(Call* expr) {
2990   Variable* new_target_var = scope()->DeclarationScope()->new_target_var();
2991   GetVar(result_register(), new_target_var);
2992   __ Push(result_register());
2993
2994   EmitLoadSuperConstructor();
2995   __ push(result_register());
2996
2997   // Push the arguments ("left-to-right") on the stack.
2998   ZoneList<Expression*>* args = expr->arguments();
2999   int arg_count = args->length();
3000   for (int i = 0; i < arg_count; i++) {
3001     VisitForStackValue(args->at(i));
3002   }
3003
3004   // Call the construct call builtin that handles allocation and
3005   // constructor invocation.
3006   SetSourcePosition(expr->position());
3007
3008   // Load function and argument count into x1 and x0.
3009   __ Mov(x0, arg_count);
3010   __ Peek(x1, arg_count * kXRegSize);
3011
3012   // Record call targets in unoptimized code.
3013   if (FLAG_pretenuring_call_new) {
3014     UNREACHABLE();
3015     /* TODO(dslomov): support pretenuring.
3016     EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3017     DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3018            expr->CallNewFeedbackSlot().ToInt() + 1);
3019     */
3020   }
3021
3022   __ LoadObject(x2, FeedbackVector());
3023   __ Mov(x3, SmiFromSlot(expr->CallFeedbackSlot()));
3024
3025   CallConstructStub stub(isolate(), SUPER_CALL_RECORD_TARGET);
3026   __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3027
3028   __ Drop(1);
3029
3030   RecordJSReturnSite(expr);
3031
3032   SuperReference* super_ref = expr->expression()->AsSuperReference();
3033   Variable* this_var = super_ref->this_var()->var();
3034   GetVar(x1, this_var);
3035   Label uninitialized_this;
3036   __ JumpIfRoot(x1, Heap::kTheHoleValueRootIndex, &uninitialized_this);
3037   __ Mov(x0, Operand(this_var->name()));
3038   __ Push(x0);
3039   __ CallRuntime(Runtime::kThrowReferenceError, 1);
3040   __ bind(&uninitialized_this);
3041
3042   EmitVariableAssignment(this_var, Token::INIT_CONST);
3043   context()->Plug(x0);
3044 }
3045
3046
3047 void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
3048   ZoneList<Expression*>* args = expr->arguments();
3049   DCHECK(args->length() == 1);
3050
3051   VisitForAccumulatorValue(args->at(0));
3052
3053   Label materialize_true, materialize_false;
3054   Label* if_true = NULL;
3055   Label* if_false = NULL;
3056   Label* fall_through = NULL;
3057   context()->PrepareTest(&materialize_true, &materialize_false,
3058                          &if_true, &if_false, &fall_through);
3059
3060   PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3061   __ TestAndSplit(x0, kSmiTagMask, if_true, if_false, fall_through);
3062
3063   context()->Plug(if_true, if_false);
3064 }
3065
3066
3067 void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
3068   ZoneList<Expression*>* args = expr->arguments();
3069   DCHECK(args->length() == 1);
3070
3071   VisitForAccumulatorValue(args->at(0));
3072
3073   Label materialize_true, materialize_false;
3074   Label* if_true = NULL;
3075   Label* if_false = NULL;
3076   Label* fall_through = NULL;
3077   context()->PrepareTest(&materialize_true, &materialize_false,
3078                          &if_true, &if_false, &fall_through);
3079
3080   uint64_t sign_mask = V8_UINT64_C(1) << (kSmiShift + kSmiValueSize - 1);
3081
3082   PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3083   __ TestAndSplit(x0, kSmiTagMask | sign_mask, if_true, if_false, fall_through);
3084
3085   context()->Plug(if_true, if_false);
3086 }
3087
3088
3089 void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
3090   ZoneList<Expression*>* args = expr->arguments();
3091   DCHECK(args->length() == 1);
3092
3093   VisitForAccumulatorValue(args->at(0));
3094
3095   Label materialize_true, materialize_false;
3096   Label* if_true = NULL;
3097   Label* if_false = NULL;
3098   Label* fall_through = NULL;
3099   context()->PrepareTest(&materialize_true, &materialize_false,
3100                          &if_true, &if_false, &fall_through);
3101
3102   __ JumpIfSmi(x0, if_false);
3103   __ JumpIfRoot(x0, Heap::kNullValueRootIndex, if_true);
3104   __ Ldr(x10, FieldMemOperand(x0, HeapObject::kMapOffset));
3105   // Undetectable objects behave like undefined when tested with typeof.
3106   __ Ldrb(x11, FieldMemOperand(x10, Map::kBitFieldOffset));
3107   __ Tbnz(x11, Map::kIsUndetectable, if_false);
3108   __ Ldrb(x12, FieldMemOperand(x10, Map::kInstanceTypeOffset));
3109   __ Cmp(x12, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
3110   __ B(lt, if_false);
3111   __ Cmp(x12, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
3112   PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3113   Split(le, if_true, if_false, fall_through);
3114
3115   context()->Plug(if_true, if_false);
3116 }
3117
3118
3119 void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
3120   ZoneList<Expression*>* args = expr->arguments();
3121   DCHECK(args->length() == 1);
3122
3123   VisitForAccumulatorValue(args->at(0));
3124
3125   Label materialize_true, materialize_false;
3126   Label* if_true = NULL;
3127   Label* if_false = NULL;
3128   Label* fall_through = NULL;
3129   context()->PrepareTest(&materialize_true, &materialize_false,
3130                          &if_true, &if_false, &fall_through);
3131
3132   __ JumpIfSmi(x0, if_false);
3133   __ CompareObjectType(x0, x10, x11, FIRST_SPEC_OBJECT_TYPE);
3134   PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3135   Split(ge, if_true, if_false, fall_through);
3136
3137   context()->Plug(if_true, if_false);
3138 }
3139
3140
3141 void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
3142   ASM_LOCATION("FullCodeGenerator::EmitIsUndetectableObject");
3143   ZoneList<Expression*>* args = expr->arguments();
3144   DCHECK(args->length() == 1);
3145
3146   VisitForAccumulatorValue(args->at(0));
3147
3148   Label materialize_true, materialize_false;
3149   Label* if_true = NULL;
3150   Label* if_false = NULL;
3151   Label* fall_through = NULL;
3152   context()->PrepareTest(&materialize_true, &materialize_false,
3153                          &if_true, &if_false, &fall_through);
3154
3155   __ JumpIfSmi(x0, if_false);
3156   __ Ldr(x10, FieldMemOperand(x0, HeapObject::kMapOffset));
3157   __ Ldrb(x11, FieldMemOperand(x10, Map::kBitFieldOffset));
3158   __ Tst(x11, 1 << Map::kIsUndetectable);
3159   PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3160   Split(ne, if_true, if_false, fall_through);
3161
3162   context()->Plug(if_true, if_false);
3163 }
3164
3165
3166 void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
3167     CallRuntime* expr) {
3168   ZoneList<Expression*>* args = expr->arguments();
3169   DCHECK(args->length() == 1);
3170   VisitForAccumulatorValue(args->at(0));
3171
3172   Label materialize_true, materialize_false, skip_lookup;
3173   Label* if_true = NULL;
3174   Label* if_false = NULL;
3175   Label* fall_through = NULL;
3176   context()->PrepareTest(&materialize_true, &materialize_false,
3177                          &if_true, &if_false, &fall_through);
3178
3179   Register object = x0;
3180   __ AssertNotSmi(object);
3181
3182   Register map = x10;
3183   Register bitfield2 = x11;
3184   __ Ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
3185   __ Ldrb(bitfield2, FieldMemOperand(map, Map::kBitField2Offset));
3186   __ Tbnz(bitfield2, Map::kStringWrapperSafeForDefaultValueOf, &skip_lookup);
3187
3188   // Check for fast case object. Generate false result for slow case object.
3189   Register props = x12;
3190   Register props_map = x12;
3191   Register hash_table_map = x13;
3192   __ Ldr(props, FieldMemOperand(object, JSObject::kPropertiesOffset));
3193   __ Ldr(props_map, FieldMemOperand(props, HeapObject::kMapOffset));
3194   __ LoadRoot(hash_table_map, Heap::kHashTableMapRootIndex);
3195   __ Cmp(props_map, hash_table_map);
3196   __ B(eq, if_false);
3197
3198   // Look for valueOf name in the descriptor array, and indicate false if found.
3199   // Since we omit an enumeration index check, if it is added via a transition
3200   // that shares its descriptor array, this is a false positive.
3201   Label loop, done;
3202
3203   // Skip loop if no descriptors are valid.
3204   Register descriptors = x12;
3205   Register descriptors_length = x13;
3206   __ NumberOfOwnDescriptors(descriptors_length, map);
3207   __ Cbz(descriptors_length, &done);
3208
3209   __ LoadInstanceDescriptors(map, descriptors);
3210
3211   // Calculate the end of the descriptor array.
3212   Register descriptors_end = x14;
3213   __ Mov(x15, DescriptorArray::kDescriptorSize);
3214   __ Mul(descriptors_length, descriptors_length, x15);
3215   // Calculate location of the first key name.
3216   __ Add(descriptors, descriptors,
3217          DescriptorArray::kFirstOffset - kHeapObjectTag);
3218   // Calculate the end of the descriptor array.
3219   __ Add(descriptors_end, descriptors,
3220          Operand(descriptors_length, LSL, kPointerSizeLog2));
3221
3222   // Loop through all the keys in the descriptor array. If one of these is the
3223   // string "valueOf" the result is false.
3224   Register valueof_string = x1;
3225   int descriptor_size = DescriptorArray::kDescriptorSize * kPointerSize;
3226   __ Mov(valueof_string, Operand(isolate()->factory()->value_of_string()));
3227   __ Bind(&loop);
3228   __ Ldr(x15, MemOperand(descriptors, descriptor_size, PostIndex));
3229   __ Cmp(x15, valueof_string);
3230   __ B(eq, if_false);
3231   __ Cmp(descriptors, descriptors_end);
3232   __ B(ne, &loop);
3233
3234   __ Bind(&done);
3235
3236   // Set the bit in the map to indicate that there is no local valueOf field.
3237   __ Ldrb(x2, FieldMemOperand(map, Map::kBitField2Offset));
3238   __ Orr(x2, x2, 1 << Map::kStringWrapperSafeForDefaultValueOf);
3239   __ Strb(x2, FieldMemOperand(map, Map::kBitField2Offset));
3240
3241   __ Bind(&skip_lookup);
3242
3243   // If a valueOf property is not found on the object check that its prototype
3244   // is the unmodified String prototype. If not result is false.
3245   Register prototype = x1;
3246   Register global_idx = x2;
3247   Register native_context = x2;
3248   Register string_proto = x3;
3249   Register proto_map = x4;
3250   __ Ldr(prototype, FieldMemOperand(map, Map::kPrototypeOffset));
3251   __ JumpIfSmi(prototype, if_false);
3252   __ Ldr(proto_map, FieldMemOperand(prototype, HeapObject::kMapOffset));
3253   __ Ldr(global_idx, GlobalObjectMemOperand());
3254   __ Ldr(native_context,
3255          FieldMemOperand(global_idx, GlobalObject::kNativeContextOffset));
3256   __ Ldr(string_proto,
3257          ContextMemOperand(native_context,
3258                            Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
3259   __ Cmp(proto_map, string_proto);
3260
3261   PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3262   Split(eq, if_true, if_false, fall_through);
3263
3264   context()->Plug(if_true, if_false);
3265 }
3266
3267
3268 void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
3269   ZoneList<Expression*>* args = expr->arguments();
3270   DCHECK(args->length() == 1);
3271
3272   VisitForAccumulatorValue(args->at(0));
3273
3274   Label materialize_true, materialize_false;
3275   Label* if_true = NULL;
3276   Label* if_false = NULL;
3277   Label* fall_through = NULL;
3278   context()->PrepareTest(&materialize_true, &materialize_false,
3279                          &if_true, &if_false, &fall_through);
3280
3281   __ JumpIfSmi(x0, if_false);
3282   __ CompareObjectType(x0, x10, x11, JS_FUNCTION_TYPE);
3283   PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3284   Split(eq, if_true, if_false, fall_through);
3285
3286   context()->Plug(if_true, if_false);
3287 }
3288
3289
3290 void FullCodeGenerator::EmitIsMinusZero(CallRuntime* expr) {
3291   ZoneList<Expression*>* args = expr->arguments();
3292   DCHECK(args->length() == 1);
3293
3294   VisitForAccumulatorValue(args->at(0));
3295
3296   Label materialize_true, materialize_false;
3297   Label* if_true = NULL;
3298   Label* if_false = NULL;
3299   Label* fall_through = NULL;
3300   context()->PrepareTest(&materialize_true, &materialize_false,
3301                          &if_true, &if_false, &fall_through);
3302
3303   // Only a HeapNumber can be -0.0, so return false if we have something else.
3304   __ JumpIfNotHeapNumber(x0, if_false, DO_SMI_CHECK);
3305
3306   // Test the bit pattern.
3307   __ Ldr(x10, FieldMemOperand(x0, HeapNumber::kValueOffset));
3308   __ Cmp(x10, 1);   // Set V on 0x8000000000000000.
3309
3310   PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3311   Split(vs, if_true, if_false, fall_through);
3312
3313   context()->Plug(if_true, if_false);
3314 }
3315
3316
3317 void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
3318   ZoneList<Expression*>* args = expr->arguments();
3319   DCHECK(args->length() == 1);
3320
3321   VisitForAccumulatorValue(args->at(0));
3322
3323   Label materialize_true, materialize_false;
3324   Label* if_true = NULL;
3325   Label* if_false = NULL;
3326   Label* fall_through = NULL;
3327   context()->PrepareTest(&materialize_true, &materialize_false,
3328                          &if_true, &if_false, &fall_through);
3329
3330   __ JumpIfSmi(x0, if_false);
3331   __ CompareObjectType(x0, x10, x11, JS_ARRAY_TYPE);
3332   PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3333   Split(eq, if_true, if_false, fall_through);
3334
3335   context()->Plug(if_true, if_false);
3336 }
3337
3338
3339 void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
3340   ZoneList<Expression*>* args = expr->arguments();
3341   DCHECK(args->length() == 1);
3342
3343   VisitForAccumulatorValue(args->at(0));
3344
3345   Label materialize_true, materialize_false;
3346   Label* if_true = NULL;
3347   Label* if_false = NULL;
3348   Label* fall_through = NULL;
3349   context()->PrepareTest(&materialize_true, &materialize_false,
3350                          &if_true, &if_false, &fall_through);
3351
3352   __ JumpIfSmi(x0, if_false);
3353   __ CompareObjectType(x0, x10, x11, JS_REGEXP_TYPE);
3354   PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3355   Split(eq, if_true, if_false, fall_through);
3356
3357   context()->Plug(if_true, if_false);
3358 }
3359
3360
3361 void FullCodeGenerator::EmitIsJSProxy(CallRuntime* expr) {
3362   ZoneList<Expression*>* args = expr->arguments();
3363   DCHECK(args->length() == 1);
3364
3365   VisitForAccumulatorValue(args->at(0));
3366
3367   Label materialize_true, materialize_false;
3368   Label* if_true = NULL;
3369   Label* if_false = NULL;
3370   Label* fall_through = NULL;
3371   context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3372                          &if_false, &fall_through);
3373
3374   __ JumpIfSmi(x0, if_false);
3375   Register map = x10;
3376   Register type_reg = x11;
3377   __ Ldr(map, FieldMemOperand(x0, HeapObject::kMapOffset));
3378   __ Ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
3379   __ Sub(type_reg, type_reg, Operand(FIRST_JS_PROXY_TYPE));
3380   __ Cmp(type_reg, Operand(LAST_JS_PROXY_TYPE - FIRST_JS_PROXY_TYPE));
3381   PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3382   Split(ls, if_true, if_false, fall_through);
3383
3384   context()->Plug(if_true, if_false);
3385 }
3386
3387
3388 void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
3389   DCHECK(expr->arguments()->length() == 0);
3390
3391   Label materialize_true, materialize_false;
3392   Label* if_true = NULL;
3393   Label* if_false = NULL;
3394   Label* fall_through = NULL;
3395   context()->PrepareTest(&materialize_true, &materialize_false,
3396                          &if_true, &if_false, &fall_through);
3397
3398   // Get the frame pointer for the calling frame.
3399   __ Ldr(x2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3400
3401   // Skip the arguments adaptor frame if it exists.
3402   Label check_frame_marker;
3403   __ Ldr(x1, MemOperand(x2, StandardFrameConstants::kContextOffset));
3404   __ Cmp(x1, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
3405   __ B(ne, &check_frame_marker);
3406   __ Ldr(x2, MemOperand(x2, StandardFrameConstants::kCallerFPOffset));
3407
3408   // Check the marker in the calling frame.
3409   __ Bind(&check_frame_marker);
3410   __ Ldr(x1, MemOperand(x2, StandardFrameConstants::kMarkerOffset));
3411   __ Cmp(x1, Smi::FromInt(StackFrame::CONSTRUCT));
3412   PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3413   Split(eq, if_true, if_false, fall_through);
3414
3415   context()->Plug(if_true, if_false);
3416 }
3417
3418
3419 void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
3420   ZoneList<Expression*>* args = expr->arguments();
3421   DCHECK(args->length() == 2);
3422
3423   // Load the two objects into registers and perform the comparison.
3424   VisitForStackValue(args->at(0));
3425   VisitForAccumulatorValue(args->at(1));
3426
3427   Label materialize_true, materialize_false;
3428   Label* if_true = NULL;
3429   Label* if_false = NULL;
3430   Label* fall_through = NULL;
3431   context()->PrepareTest(&materialize_true, &materialize_false,
3432                          &if_true, &if_false, &fall_through);
3433
3434   __ Pop(x1);
3435   __ Cmp(x0, x1);
3436   PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3437   Split(eq, if_true, if_false, fall_through);
3438
3439   context()->Plug(if_true, if_false);
3440 }
3441
3442
3443 void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
3444   ZoneList<Expression*>* args = expr->arguments();
3445   DCHECK(args->length() == 1);
3446
3447   // ArgumentsAccessStub expects the key in x1.
3448   VisitForAccumulatorValue(args->at(0));
3449   __ Mov(x1, x0);
3450   __ Mov(x0, Smi::FromInt(info_->scope()->num_parameters()));
3451   ArgumentsAccessStub stub(isolate(), ArgumentsAccessStub::READ_ELEMENT);
3452   __ CallStub(&stub);
3453   context()->Plug(x0);
3454 }
3455
3456
3457 void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
3458   DCHECK(expr->arguments()->length() == 0);
3459   Label exit;
3460   // Get the number of formal parameters.
3461   __ Mov(x0, Smi::FromInt(info_->scope()->num_parameters()));
3462
3463   // Check if the calling frame is an arguments adaptor frame.
3464   __ Ldr(x12, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3465   __ Ldr(x13, MemOperand(x12, StandardFrameConstants::kContextOffset));
3466   __ Cmp(x13, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
3467   __ B(ne, &exit);
3468
3469   // Arguments adaptor case: Read the arguments length from the
3470   // adaptor frame.
3471   __ Ldr(x0, MemOperand(x12, ArgumentsAdaptorFrameConstants::kLengthOffset));
3472
3473   __ Bind(&exit);
3474   context()->Plug(x0);
3475 }
3476
3477
3478 void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
3479   ASM_LOCATION("FullCodeGenerator::EmitClassOf");
3480   ZoneList<Expression*>* args = expr->arguments();
3481   DCHECK(args->length() == 1);
3482   Label done, null, function, non_function_constructor;
3483
3484   VisitForAccumulatorValue(args->at(0));
3485
3486   // If the object is a smi, we return null.
3487   __ JumpIfSmi(x0, &null);
3488
3489   // Check that the object is a JS object but take special care of JS
3490   // functions to make sure they have 'Function' as their class.
3491   // Assume that there are only two callable types, and one of them is at
3492   // either end of the type range for JS object types. Saves extra comparisons.
3493   STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
3494   __ CompareObjectType(x0, x10, x11, FIRST_SPEC_OBJECT_TYPE);
3495   // x10: object's map.
3496   // x11: object's type.
3497   __ B(lt, &null);
3498   STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3499                 FIRST_SPEC_OBJECT_TYPE + 1);
3500   __ B(eq, &function);
3501
3502   __ Cmp(x11, LAST_SPEC_OBJECT_TYPE);
3503   STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3504                 LAST_SPEC_OBJECT_TYPE - 1);
3505   __ B(eq, &function);
3506   // Assume that there is no larger type.
3507   STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
3508
3509   // Check if the constructor in the map is a JS function.
3510   Register instance_type = x14;
3511   __ GetMapConstructor(x12, x10, x13, instance_type);
3512   __ Cmp(instance_type, JS_FUNCTION_TYPE);
3513   __ B(ne, &non_function_constructor);
3514
3515   // x12 now contains the constructor function. Grab the
3516   // instance class name from there.
3517   __ Ldr(x13, FieldMemOperand(x12, JSFunction::kSharedFunctionInfoOffset));
3518   __ Ldr(x0,
3519          FieldMemOperand(x13, SharedFunctionInfo::kInstanceClassNameOffset));
3520   __ B(&done);
3521
3522   // Functions have class 'Function'.
3523   __ Bind(&function);
3524   __ LoadRoot(x0, Heap::kFunction_stringRootIndex);
3525   __ B(&done);
3526
3527   // Objects with a non-function constructor have class 'Object'.
3528   __ Bind(&non_function_constructor);
3529   __ LoadRoot(x0, Heap::kObject_stringRootIndex);
3530   __ B(&done);
3531
3532   // Non-JS objects have class null.
3533   __ Bind(&null);
3534   __ LoadRoot(x0, Heap::kNullValueRootIndex);
3535
3536   // All done.
3537   __ Bind(&done);
3538
3539   context()->Plug(x0);
3540 }
3541
3542
3543 void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
3544   // Load the arguments on the stack and call the stub.
3545   SubStringStub stub(isolate());
3546   ZoneList<Expression*>* args = expr->arguments();
3547   DCHECK(args->length() == 3);
3548   VisitForStackValue(args->at(0));
3549   VisitForStackValue(args->at(1));
3550   VisitForStackValue(args->at(2));
3551   __ CallStub(&stub);
3552   context()->Plug(x0);
3553 }
3554
3555
3556 void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
3557   // Load the arguments on the stack and call the stub.
3558   RegExpExecStub stub(isolate());
3559   ZoneList<Expression*>* args = expr->arguments();
3560   DCHECK(args->length() == 4);
3561   VisitForStackValue(args->at(0));
3562   VisitForStackValue(args->at(1));
3563   VisitForStackValue(args->at(2));
3564   VisitForStackValue(args->at(3));
3565   __ CallStub(&stub);
3566   context()->Plug(x0);
3567 }
3568
3569
3570 void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3571   ASM_LOCATION("FullCodeGenerator::EmitValueOf");
3572   ZoneList<Expression*>* args = expr->arguments();
3573   DCHECK(args->length() == 1);
3574   VisitForAccumulatorValue(args->at(0));  // Load the object.
3575
3576   Label done;
3577   // If the object is a smi return the object.
3578   __ JumpIfSmi(x0, &done);
3579   // If the object is not a value type, return the object.
3580   __ JumpIfNotObjectType(x0, x10, x11, JS_VALUE_TYPE, &done);
3581   __ Ldr(x0, FieldMemOperand(x0, JSValue::kValueOffset));
3582
3583   __ Bind(&done);
3584   context()->Plug(x0);
3585 }
3586
3587
3588 void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3589   ZoneList<Expression*>* args = expr->arguments();
3590   DCHECK(args->length() == 2);
3591   DCHECK_NOT_NULL(args->at(1)->AsLiteral());
3592   Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->value()));
3593
3594   VisitForAccumulatorValue(args->at(0));  // Load the object.
3595
3596   Label runtime, done, not_date_object;
3597   Register object = x0;
3598   Register result = x0;
3599   Register stamp_addr = x10;
3600   Register stamp_cache = x11;
3601
3602   __ JumpIfSmi(object, &not_date_object);
3603   __ JumpIfNotObjectType(object, x10, x10, JS_DATE_TYPE, &not_date_object);
3604
3605   if (index->value() == 0) {
3606     __ Ldr(result, FieldMemOperand(object, JSDate::kValueOffset));
3607     __ B(&done);
3608   } else {
3609     if (index->value() < JSDate::kFirstUncachedField) {
3610       ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3611       __ Mov(x10, stamp);
3612       __ Ldr(stamp_addr, MemOperand(x10));
3613       __ Ldr(stamp_cache, FieldMemOperand(object, JSDate::kCacheStampOffset));
3614       __ Cmp(stamp_addr, stamp_cache);
3615       __ B(ne, &runtime);
3616       __ Ldr(result, FieldMemOperand(object, JSDate::kValueOffset +
3617                                              kPointerSize * index->value()));
3618       __ B(&done);
3619     }
3620
3621     __ Bind(&runtime);
3622     __ Mov(x1, index);
3623     __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
3624     __ B(&done);
3625   }
3626
3627   __ Bind(&not_date_object);
3628   __ CallRuntime(Runtime::kThrowNotDateError, 0);
3629   __ Bind(&done);
3630   context()->Plug(x0);
3631 }
3632
3633
3634 void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
3635   ZoneList<Expression*>* args = expr->arguments();
3636   DCHECK_EQ(3, args->length());
3637
3638   Register string = x0;
3639   Register index = x1;
3640   Register value = x2;
3641   Register scratch = x10;
3642
3643   VisitForStackValue(args->at(0));        // index
3644   VisitForStackValue(args->at(1));        // value
3645   VisitForAccumulatorValue(args->at(2));  // string
3646   __ Pop(value, index);
3647
3648   if (FLAG_debug_code) {
3649     __ AssertSmi(value, kNonSmiValue);
3650     __ AssertSmi(index, kNonSmiIndex);
3651     static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
3652     __ EmitSeqStringSetCharCheck(string, index, kIndexIsSmi, scratch,
3653                                  one_byte_seq_type);
3654   }
3655
3656   __ Add(scratch, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
3657   __ SmiUntag(value);
3658   __ SmiUntag(index);
3659   __ Strb(value, MemOperand(scratch, index));
3660   context()->Plug(string);
3661 }
3662
3663
3664 void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
3665   ZoneList<Expression*>* args = expr->arguments();
3666   DCHECK_EQ(3, args->length());
3667
3668   Register string = x0;
3669   Register index = x1;
3670   Register value = x2;
3671   Register scratch = x10;
3672
3673   VisitForStackValue(args->at(0));        // index
3674   VisitForStackValue(args->at(1));        // value
3675   VisitForAccumulatorValue(args->at(2));  // string
3676   __ Pop(value, index);
3677
3678   if (FLAG_debug_code) {
3679     __ AssertSmi(value, kNonSmiValue);
3680     __ AssertSmi(index, kNonSmiIndex);
3681     static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
3682     __ EmitSeqStringSetCharCheck(string, index, kIndexIsSmi, scratch,
3683                                  two_byte_seq_type);
3684   }
3685
3686   __ Add(scratch, string, SeqTwoByteString::kHeaderSize - kHeapObjectTag);
3687   __ SmiUntag(value);
3688   __ SmiUntag(index);
3689   __ Strh(value, MemOperand(scratch, index, LSL, 1));
3690   context()->Plug(string);
3691 }
3692
3693
3694 void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
3695   // Load the arguments on the stack and call the MathPow stub.
3696   ZoneList<Expression*>* args = expr->arguments();
3697   DCHECK(args->length() == 2);
3698   VisitForStackValue(args->at(0));
3699   VisitForStackValue(args->at(1));
3700   MathPowStub stub(isolate(), MathPowStub::ON_STACK);
3701   __ CallStub(&stub);
3702   context()->Plug(x0);
3703 }
3704
3705
3706 void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3707   ZoneList<Expression*>* args = expr->arguments();
3708   DCHECK(args->length() == 2);
3709   VisitForStackValue(args->at(0));  // Load the object.
3710   VisitForAccumulatorValue(args->at(1));  // Load the value.
3711   __ Pop(x1);
3712   // x0 = value.
3713   // x1 = object.
3714
3715   Label done;
3716   // If the object is a smi, return the value.
3717   __ JumpIfSmi(x1, &done);
3718
3719   // If the object is not a value type, return the value.
3720   __ JumpIfNotObjectType(x1, x10, x11, JS_VALUE_TYPE, &done);
3721
3722   // Store the value.
3723   __ Str(x0, FieldMemOperand(x1, JSValue::kValueOffset));
3724   // Update the write barrier. Save the value as it will be
3725   // overwritten by the write barrier code and is needed afterward.
3726   __ Mov(x10, x0);
3727   __ RecordWriteField(
3728       x1, JSValue::kValueOffset, x10, x11, kLRHasBeenSaved, kDontSaveFPRegs);
3729
3730   __ Bind(&done);
3731   context()->Plug(x0);
3732 }
3733
3734
3735 void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3736   ZoneList<Expression*>* args = expr->arguments();
3737   DCHECK_EQ(args->length(), 1);
3738
3739   // Load the argument into x0 and call the stub.
3740   VisitForAccumulatorValue(args->at(0));
3741
3742   NumberToStringStub stub(isolate());
3743   __ CallStub(&stub);
3744   context()->Plug(x0);
3745 }
3746
3747
3748 void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3749   ZoneList<Expression*>* args = expr->arguments();
3750   DCHECK(args->length() == 1);
3751
3752   VisitForAccumulatorValue(args->at(0));
3753
3754   Label done;
3755   Register code = x0;
3756   Register result = x1;
3757
3758   StringCharFromCodeGenerator generator(code, result);
3759   generator.GenerateFast(masm_);
3760   __ B(&done);
3761
3762   NopRuntimeCallHelper call_helper;
3763   generator.GenerateSlow(masm_, call_helper);
3764
3765   __ Bind(&done);
3766   context()->Plug(result);
3767 }
3768
3769
3770 void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3771   ZoneList<Expression*>* args = expr->arguments();
3772   DCHECK(args->length() == 2);
3773
3774   VisitForStackValue(args->at(0));
3775   VisitForAccumulatorValue(args->at(1));
3776
3777   Register object = x1;
3778   Register index = x0;
3779   Register result = x3;
3780
3781   __ Pop(object);
3782
3783   Label need_conversion;
3784   Label index_out_of_range;
3785   Label done;
3786   StringCharCodeAtGenerator generator(object,
3787                                       index,
3788                                       result,
3789                                       &need_conversion,
3790                                       &need_conversion,
3791                                       &index_out_of_range,
3792                                       STRING_INDEX_IS_NUMBER);
3793   generator.GenerateFast(masm_);
3794   __ B(&done);
3795
3796   __ Bind(&index_out_of_range);
3797   // When the index is out of range, the spec requires us to return NaN.
3798   __ LoadRoot(result, Heap::kNanValueRootIndex);
3799   __ B(&done);
3800
3801   __ Bind(&need_conversion);
3802   // Load the undefined value into the result register, which will
3803   // trigger conversion.
3804   __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3805   __ B(&done);
3806
3807   NopRuntimeCallHelper call_helper;
3808   generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
3809
3810   __ Bind(&done);
3811   context()->Plug(result);
3812 }
3813
3814
3815 void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3816   ZoneList<Expression*>* args = expr->arguments();
3817   DCHECK(args->length() == 2);
3818
3819   VisitForStackValue(args->at(0));
3820   VisitForAccumulatorValue(args->at(1));
3821
3822   Register object = x1;
3823   Register index = x0;
3824   Register result = x0;
3825
3826   __ Pop(object);
3827
3828   Label need_conversion;
3829   Label index_out_of_range;
3830   Label done;
3831   StringCharAtGenerator generator(object,
3832                                   index,
3833                                   x3,
3834                                   result,
3835                                   &need_conversion,
3836                                   &need_conversion,
3837                                   &index_out_of_range,
3838                                   STRING_INDEX_IS_NUMBER);
3839   generator.GenerateFast(masm_);
3840   __ B(&done);
3841
3842   __ Bind(&index_out_of_range);
3843   // When the index is out of range, the spec requires us to return
3844   // the empty string.
3845   __ LoadRoot(result, Heap::kempty_stringRootIndex);
3846   __ B(&done);
3847
3848   __ Bind(&need_conversion);
3849   // Move smi zero into the result register, which will trigger conversion.
3850   __ Mov(result, Smi::FromInt(0));
3851   __ B(&done);
3852
3853   NopRuntimeCallHelper call_helper;
3854   generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
3855
3856   __ Bind(&done);
3857   context()->Plug(result);
3858 }
3859
3860
3861 void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
3862   ASM_LOCATION("FullCodeGenerator::EmitStringAdd");
3863   ZoneList<Expression*>* args = expr->arguments();
3864   DCHECK_EQ(2, args->length());
3865
3866   VisitForStackValue(args->at(0));
3867   VisitForAccumulatorValue(args->at(1));
3868
3869   __ Pop(x1);
3870   StringAddStub stub(isolate(), STRING_ADD_CHECK_BOTH, NOT_TENURED);
3871   __ CallStub(&stub);
3872
3873   context()->Plug(x0);
3874 }
3875
3876
3877 void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
3878   ZoneList<Expression*>* args = expr->arguments();
3879   DCHECK_EQ(2, args->length());
3880   VisitForStackValue(args->at(0));
3881   VisitForStackValue(args->at(1));
3882
3883   StringCompareStub stub(isolate());
3884   __ CallStub(&stub);
3885   context()->Plug(x0);
3886 }
3887
3888
3889 void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
3890   ASM_LOCATION("FullCodeGenerator::EmitCallFunction");
3891   ZoneList<Expression*>* args = expr->arguments();
3892   DCHECK(args->length() >= 2);
3893
3894   int arg_count = args->length() - 2;  // 2 ~ receiver and function.
3895   for (int i = 0; i < arg_count + 1; i++) {
3896     VisitForStackValue(args->at(i));
3897   }
3898   VisitForAccumulatorValue(args->last());  // Function.
3899
3900   Label runtime, done;
3901   // Check for non-function argument (including proxy).
3902   __ JumpIfSmi(x0, &runtime);
3903   __ JumpIfNotObjectType(x0, x1, x1, JS_FUNCTION_TYPE, &runtime);
3904
3905   // InvokeFunction requires the function in x1. Move it in there.
3906   __ Mov(x1, x0);
3907   ParameterCount count(arg_count);
3908   __ InvokeFunction(x1, count, CALL_FUNCTION, NullCallWrapper());
3909   __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3910   __ B(&done);
3911
3912   __ Bind(&runtime);
3913   __ Push(x0);
3914   __ CallRuntime(Runtime::kCall, args->length());
3915   __ Bind(&done);
3916
3917   context()->Plug(x0);
3918 }
3919
3920
3921 void FullCodeGenerator::EmitDefaultConstructorCallSuper(CallRuntime* expr) {
3922   Variable* new_target_var = scope()->DeclarationScope()->new_target_var();
3923   GetVar(result_register(), new_target_var);
3924   __ Push(result_register());
3925
3926   EmitLoadSuperConstructor();
3927   __ Push(result_register());
3928
3929   // Check if the calling frame is an arguments adaptor frame.
3930   Label adaptor_frame, args_set_up, runtime;
3931   __ Ldr(x11, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3932   __ Ldr(x12, MemOperand(x11, StandardFrameConstants::kContextOffset));
3933   __ Cmp(x12, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
3934   __ B(eq, &adaptor_frame);
3935   // default constructor has no arguments, so no adaptor frame means no args.
3936   __ Mov(x0, Operand(0));
3937   __ B(&args_set_up);
3938
3939   // Copy arguments from adaptor frame.
3940   {
3941     __ bind(&adaptor_frame);
3942     __ Ldr(x1, MemOperand(x11, ArgumentsAdaptorFrameConstants::kLengthOffset));
3943     __ SmiUntag(x1, x1);
3944
3945     // Subtract 1 from arguments count, for new.target.
3946     __ Sub(x1, x1, Operand(1));
3947     __ Mov(x0, x1);
3948
3949     // Get arguments pointer in x11.
3950     __ Add(x11, x11, Operand(x1, LSL, kPointerSizeLog2));
3951     __ Add(x11, x11, StandardFrameConstants::kCallerSPOffset);
3952     Label loop;
3953     __ bind(&loop);
3954     // Pre-decrement x11 with kPointerSize on each iteration.
3955     // Pre-decrement in order to skip receiver.
3956     __ Ldr(x10, MemOperand(x11, -kPointerSize, PreIndex));
3957     __ Push(x10);
3958     __ Sub(x1, x1, Operand(1));
3959     __ Cbnz(x1, &loop);
3960   }
3961
3962   __ bind(&args_set_up);
3963   __ Peek(x1, Operand(x0, LSL, kPointerSizeLog2));
3964   __ LoadRoot(x2, Heap::kUndefinedValueRootIndex);
3965
3966   CallConstructStub stub(isolate(), SUPER_CONSTRUCTOR_CALL);
3967   __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3968
3969   __ Drop(1);
3970
3971   context()->Plug(result_register());
3972 }
3973
3974
3975 void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
3976   RegExpConstructResultStub stub(isolate());
3977   ZoneList<Expression*>* args = expr->arguments();
3978   DCHECK(args->length() == 3);
3979   VisitForStackValue(args->at(0));
3980   VisitForStackValue(args->at(1));
3981   VisitForAccumulatorValue(args->at(2));
3982   __ Pop(x1, x2);
3983   __ CallStub(&stub);
3984   context()->Plug(x0);
3985 }
3986
3987
3988 void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
3989   ZoneList<Expression*>* args = expr->arguments();
3990   DCHECK_EQ(2, args->length());
3991   DCHECK_NOT_NULL(args->at(0)->AsLiteral());
3992   int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->value()))->value();
3993
3994   Handle<FixedArray> jsfunction_result_caches(
3995       isolate()->native_context()->jsfunction_result_caches());
3996   if (jsfunction_result_caches->length() <= cache_id) {
3997     __ Abort(kAttemptToUseUndefinedCache);
3998     __ LoadRoot(x0, Heap::kUndefinedValueRootIndex);
3999     context()->Plug(x0);
4000     return;
4001   }
4002
4003   VisitForAccumulatorValue(args->at(1));
4004
4005   Register key = x0;
4006   Register cache = x1;
4007   __ Ldr(cache, GlobalObjectMemOperand());
4008   __ Ldr(cache, FieldMemOperand(cache, GlobalObject::kNativeContextOffset));
4009   __ Ldr(cache, ContextMemOperand(cache,
4010                                   Context::JSFUNCTION_RESULT_CACHES_INDEX));
4011   __ Ldr(cache,
4012          FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
4013
4014   Label done;
4015   __ Ldrsw(x2, UntagSmiFieldMemOperand(cache,
4016                                        JSFunctionResultCache::kFingerOffset));
4017   __ Add(x3, cache, FixedArray::kHeaderSize - kHeapObjectTag);
4018   __ Add(x3, x3, Operand(x2, LSL, kPointerSizeLog2));
4019
4020   // Load the key and data from the cache.
4021   __ Ldp(x2, x3, MemOperand(x3));
4022
4023   __ Cmp(key, x2);
4024   __ CmovX(x0, x3, eq);
4025   __ B(eq, &done);
4026
4027   // Call runtime to perform the lookup.
4028   __ Push(cache, key);
4029   __ CallRuntime(Runtime::kGetFromCacheRT, 2);
4030
4031   __ Bind(&done);
4032   context()->Plug(x0);
4033 }
4034
4035
4036 void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
4037   ZoneList<Expression*>* args = expr->arguments();
4038   VisitForAccumulatorValue(args->at(0));
4039
4040   Label materialize_true, materialize_false;
4041   Label* if_true = NULL;
4042   Label* if_false = NULL;
4043   Label* fall_through = NULL;
4044   context()->PrepareTest(&materialize_true, &materialize_false,
4045                          &if_true, &if_false, &fall_through);
4046
4047   __ Ldr(x10, FieldMemOperand(x0, String::kHashFieldOffset));
4048   __ Tst(x10, String::kContainsCachedArrayIndexMask);
4049   PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4050   Split(eq, if_true, if_false, fall_through);
4051
4052   context()->Plug(if_true, if_false);
4053 }
4054
4055
4056 void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
4057   ZoneList<Expression*>* args = expr->arguments();
4058   DCHECK(args->length() == 1);
4059   VisitForAccumulatorValue(args->at(0));
4060
4061   __ AssertString(x0);
4062
4063   __ Ldr(x10, FieldMemOperand(x0, String::kHashFieldOffset));
4064   __ IndexFromHash(x10, x0);
4065
4066   context()->Plug(x0);
4067 }
4068
4069
4070 void FullCodeGenerator::EmitFastOneByteArrayJoin(CallRuntime* expr) {
4071   ASM_LOCATION("FullCodeGenerator::EmitFastOneByteArrayJoin");
4072
4073   ZoneList<Expression*>* args = expr->arguments();
4074   DCHECK(args->length() == 2);
4075   VisitForStackValue(args->at(1));
4076   VisitForAccumulatorValue(args->at(0));
4077
4078   Register array = x0;
4079   Register result = x0;
4080   Register elements = x1;
4081   Register element = x2;
4082   Register separator = x3;
4083   Register array_length = x4;
4084   Register result_pos = x5;
4085   Register map = x6;
4086   Register string_length = x10;
4087   Register elements_end = x11;
4088   Register string = x12;
4089   Register scratch1 = x13;
4090   Register scratch2 = x14;
4091   Register scratch3 = x7;
4092   Register separator_length = x15;
4093
4094   Label bailout, done, one_char_separator, long_separator,
4095       non_trivial_array, not_size_one_array, loop,
4096       empty_separator_loop, one_char_separator_loop,
4097       one_char_separator_loop_entry, long_separator_loop;
4098
4099   // The separator operand is on the stack.
4100   __ Pop(separator);
4101
4102   // Check that the array is a JSArray.
4103   __ JumpIfSmi(array, &bailout);
4104   __ JumpIfNotObjectType(array, map, scratch1, JS_ARRAY_TYPE, &bailout);
4105
4106   // Check that the array has fast elements.
4107   __ CheckFastElements(map, scratch1, &bailout);
4108
4109   // If the array has length zero, return the empty string.
4110   // Load and untag the length of the array.
4111   // It is an unsigned value, so we can skip sign extension.
4112   // We assume little endianness.
4113   __ Ldrsw(array_length,
4114            UntagSmiFieldMemOperand(array, JSArray::kLengthOffset));
4115   __ Cbnz(array_length, &non_trivial_array);
4116   __ LoadRoot(result, Heap::kempty_stringRootIndex);
4117   __ B(&done);
4118
4119   __ Bind(&non_trivial_array);
4120   // Get the FixedArray containing array's elements.
4121   __ Ldr(elements, FieldMemOperand(array, JSArray::kElementsOffset));
4122
4123   // Check that all array elements are sequential one-byte strings, and
4124   // accumulate the sum of their lengths.
4125   __ Mov(string_length, 0);
4126   __ Add(element, elements, FixedArray::kHeaderSize - kHeapObjectTag);
4127   __ Add(elements_end, element, Operand(array_length, LSL, kPointerSizeLog2));
4128   // Loop condition: while (element < elements_end).
4129   // Live values in registers:
4130   //   elements: Fixed array of strings.
4131   //   array_length: Length of the fixed array of strings (not smi)
4132   //   separator: Separator string
4133   //   string_length: Accumulated sum of string lengths (not smi).
4134   //   element: Current array element.
4135   //   elements_end: Array end.
4136   if (FLAG_debug_code) {
4137     __ Cmp(array_length, 0);
4138     __ Assert(gt, kNoEmptyArraysHereInEmitFastOneByteArrayJoin);
4139   }
4140   __ Bind(&loop);
4141   __ Ldr(string, MemOperand(element, kPointerSize, PostIndex));
4142   __ JumpIfSmi(string, &bailout);
4143   __ Ldr(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
4144   __ Ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
4145   __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
4146   __ Ldrsw(scratch1,
4147            UntagSmiFieldMemOperand(string, SeqOneByteString::kLengthOffset));
4148   __ Adds(string_length, string_length, scratch1);
4149   __ B(vs, &bailout);
4150   __ Cmp(element, elements_end);
4151   __ B(lt, &loop);
4152
4153   // If array_length is 1, return elements[0], a string.
4154   __ Cmp(array_length, 1);
4155   __ B(ne, &not_size_one_array);
4156   __ Ldr(result, FieldMemOperand(elements, FixedArray::kHeaderSize));
4157   __ B(&done);
4158
4159   __ Bind(&not_size_one_array);
4160
4161   // Live values in registers:
4162   //   separator: Separator string
4163   //   array_length: Length of the array (not smi).
4164   //   string_length: Sum of string lengths (not smi).
4165   //   elements: FixedArray of strings.
4166
4167   // Check that the separator is a flat one-byte string.
4168   __ JumpIfSmi(separator, &bailout);
4169   __ Ldr(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
4170   __ Ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
4171   __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
4172
4173   // Add (separator length times array_length) - separator length to the
4174   // string_length to get the length of the result string.
4175   // Load the separator length as untagged.
4176   // We assume little endianness, and that the length is positive.
4177   __ Ldrsw(separator_length,
4178            UntagSmiFieldMemOperand(separator,
4179                                    SeqOneByteString::kLengthOffset));
4180   __ Sub(string_length, string_length, separator_length);
4181   __ Umaddl(string_length, array_length.W(), separator_length.W(),
4182             string_length);
4183
4184   // Get first element in the array.
4185   __ Add(element, elements, FixedArray::kHeaderSize - kHeapObjectTag);
4186   // Live values in registers:
4187   //   element: First array element
4188   //   separator: Separator string
4189   //   string_length: Length of result string (not smi)
4190   //   array_length: Length of the array (not smi).
4191   __ AllocateOneByteString(result, string_length, scratch1, scratch2, scratch3,
4192                            &bailout);
4193
4194   // Prepare for looping. Set up elements_end to end of the array. Set
4195   // result_pos to the position of the result where to write the first
4196   // character.
4197   // TODO(all): useless unless AllocateOneByteString trashes the register.
4198   __ Add(elements_end, element, Operand(array_length, LSL, kPointerSizeLog2));
4199   __ Add(result_pos, result, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4200
4201   // Check the length of the separator.
4202   __ Cmp(separator_length, 1);
4203   __ B(eq, &one_char_separator);
4204   __ B(gt, &long_separator);
4205
4206   // Empty separator case
4207   __ Bind(&empty_separator_loop);
4208   // Live values in registers:
4209   //   result_pos: the position to which we are currently copying characters.
4210   //   element: Current array element.
4211   //   elements_end: Array end.
4212
4213   // Copy next array element to the result.
4214   __ Ldr(string, MemOperand(element, kPointerSize, PostIndex));
4215   __ Ldrsw(string_length,
4216            UntagSmiFieldMemOperand(string, String::kLengthOffset));
4217   __ Add(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4218   __ CopyBytes(result_pos, string, string_length, scratch1);
4219   __ Cmp(element, elements_end);
4220   __ B(lt, &empty_separator_loop);  // End while (element < elements_end).
4221   __ B(&done);
4222
4223   // One-character separator case
4224   __ Bind(&one_char_separator);
4225   // Replace separator with its one-byte character value.
4226   __ Ldrb(separator, FieldMemOperand(separator, SeqOneByteString::kHeaderSize));
4227   // Jump into the loop after the code that copies the separator, so the first
4228   // element is not preceded by a separator
4229   __ B(&one_char_separator_loop_entry);
4230
4231   __ Bind(&one_char_separator_loop);
4232   // Live values in registers:
4233   //   result_pos: the position to which we are currently copying characters.
4234   //   element: Current array element.
4235   //   elements_end: Array end.
4236   //   separator: Single separator one-byte char (in lower byte).
4237
4238   // Copy the separator character to the result.
4239   __ Strb(separator, MemOperand(result_pos, 1, PostIndex));
4240
4241   // Copy next array element to the result.
4242   __ Bind(&one_char_separator_loop_entry);
4243   __ Ldr(string, MemOperand(element, kPointerSize, PostIndex));
4244   __ Ldrsw(string_length,
4245            UntagSmiFieldMemOperand(string, String::kLengthOffset));
4246   __ Add(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4247   __ CopyBytes(result_pos, string, string_length, scratch1);
4248   __ Cmp(element, elements_end);
4249   __ B(lt, &one_char_separator_loop);  // End while (element < elements_end).
4250   __ B(&done);
4251
4252   // Long separator case (separator is more than one character). Entry is at the
4253   // label long_separator below.
4254   __ Bind(&long_separator_loop);
4255   // Live values in registers:
4256   //   result_pos: the position to which we are currently copying characters.
4257   //   element: Current array element.
4258   //   elements_end: Array end.
4259   //   separator: Separator string.
4260
4261   // Copy the separator to the result.
4262   // TODO(all): hoist next two instructions.
4263   __ Ldrsw(string_length,
4264            UntagSmiFieldMemOperand(separator, String::kLengthOffset));
4265   __ Add(string, separator, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4266   __ CopyBytes(result_pos, string, string_length, scratch1);
4267
4268   __ Bind(&long_separator);
4269   __ Ldr(string, MemOperand(element, kPointerSize, PostIndex));
4270   __ Ldrsw(string_length,
4271            UntagSmiFieldMemOperand(string, String::kLengthOffset));
4272   __ Add(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4273   __ CopyBytes(result_pos, string, string_length, scratch1);
4274   __ Cmp(element, elements_end);
4275   __ B(lt, &long_separator_loop);  // End while (element < elements_end).
4276   __ B(&done);
4277
4278   __ Bind(&bailout);
4279   // Returning undefined will force slower code to handle it.
4280   __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
4281   __ Bind(&done);
4282   context()->Plug(result);
4283 }
4284
4285
4286 void FullCodeGenerator::EmitDebugIsActive(CallRuntime* expr) {
4287   DCHECK(expr->arguments()->length() == 0);
4288   ExternalReference debug_is_active =
4289       ExternalReference::debug_is_active_address(isolate());
4290   __ Mov(x10, debug_is_active);
4291   __ Ldrb(x0, MemOperand(x10));
4292   __ SmiTag(x0);
4293   context()->Plug(x0);
4294 }
4295
4296
4297 void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
4298   ZoneList<Expression*>* args = expr->arguments();
4299   int arg_count = args->length();
4300
4301   if (expr->is_jsruntime()) {
4302     Comment cmnt(masm_, "[ CallRunTime");
4303     // Push the builtins object as the receiver.
4304     __ Ldr(x10, GlobalObjectMemOperand());
4305     __ Ldr(LoadDescriptor::ReceiverRegister(),
4306            FieldMemOperand(x10, GlobalObject::kBuiltinsOffset));
4307     __ Push(LoadDescriptor::ReceiverRegister());
4308
4309     // Load the function from the receiver.
4310     Handle<String> name = expr->name();
4311     __ Mov(LoadDescriptor::NameRegister(), Operand(name));
4312     if (FLAG_vector_ics) {
4313       __ Mov(VectorLoadICDescriptor::SlotRegister(),
4314              SmiFromSlot(expr->CallRuntimeFeedbackSlot()));
4315       CallLoadIC(NOT_CONTEXTUAL);
4316     } else {
4317       CallLoadIC(NOT_CONTEXTUAL, expr->CallRuntimeFeedbackId());
4318     }
4319
4320     // Push the target function under the receiver.
4321     __ Pop(x10);
4322     __ Push(x0, x10);
4323
4324     for (int i = 0; i < arg_count; i++) {
4325       VisitForStackValue(args->at(i));
4326     }
4327
4328     // Record source position of the IC call.
4329     SetSourcePosition(expr->position());
4330     CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
4331     __ Peek(x1, (arg_count + 1) * kPointerSize);
4332     __ CallStub(&stub);
4333
4334     // Restore context register.
4335     __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4336
4337     context()->DropAndPlug(1, x0);
4338
4339   } else {
4340     const Runtime::Function* function = expr->function();
4341     switch (function->function_id) {
4342 #define CALL_INTRINSIC_GENERATOR(Name)     \
4343   case Runtime::kInline##Name: {           \
4344     Comment cmnt(masm_, "[ Inline" #Name); \
4345     return Emit##Name(expr);               \
4346   }
4347       FOR_EACH_FULL_CODE_INTRINSIC(CALL_INTRINSIC_GENERATOR)
4348 #undef CALL_INTRINSIC_GENERATOR
4349       default: {
4350         Comment cmnt(masm_, "[ CallRuntime for unhandled intrinsic");
4351         // Push the arguments ("left-to-right").
4352         for (int i = 0; i < arg_count; i++) {
4353           VisitForStackValue(args->at(i));
4354         }
4355
4356         // Call the C runtime function.
4357         __ CallRuntime(expr->function(), arg_count);
4358         context()->Plug(x0);
4359       }
4360     }
4361   }
4362 }
4363
4364
4365 void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
4366   switch (expr->op()) {
4367     case Token::DELETE: {
4368       Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
4369       Property* property = expr->expression()->AsProperty();
4370       VariableProxy* proxy = expr->expression()->AsVariableProxy();
4371
4372       if (property != NULL) {
4373         VisitForStackValue(property->obj());
4374         VisitForStackValue(property->key());
4375         __ Mov(x10, Smi::FromInt(language_mode()));
4376         __ Push(x10);
4377         __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4378         context()->Plug(x0);
4379       } else if (proxy != NULL) {
4380         Variable* var = proxy->var();
4381         // Delete of an unqualified identifier is disallowed in strict mode
4382         // but "delete this" is allowed.
4383         DCHECK(is_sloppy(language_mode()) || var->is_this());
4384         if (var->IsUnallocated()) {
4385           __ Ldr(x12, GlobalObjectMemOperand());
4386           __ Mov(x11, Operand(var->name()));
4387           __ Mov(x10, Smi::FromInt(SLOPPY));
4388           __ Push(x12, x11, x10);
4389           __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4390           context()->Plug(x0);
4391         } else if (var->IsStackAllocated() || var->IsContextSlot()) {
4392           // Result of deleting non-global, non-dynamic variables is false.
4393           // The subexpression does not have side effects.
4394           context()->Plug(var->is_this());
4395         } else {
4396           // Non-global variable.  Call the runtime to try to delete from the
4397           // context where the variable was introduced.
4398           __ Mov(x2, Operand(var->name()));
4399           __ Push(context_register(), x2);
4400           __ CallRuntime(Runtime::kDeleteLookupSlot, 2);
4401           context()->Plug(x0);
4402         }
4403       } else {
4404         // Result of deleting non-property, non-variable reference is true.
4405         // The subexpression may have side effects.
4406         VisitForEffect(expr->expression());
4407         context()->Plug(true);
4408       }
4409       break;
4410       break;
4411     }
4412     case Token::VOID: {
4413       Comment cmnt(masm_, "[ UnaryOperation (VOID)");
4414       VisitForEffect(expr->expression());
4415       context()->Plug(Heap::kUndefinedValueRootIndex);
4416       break;
4417     }
4418     case Token::NOT: {
4419       Comment cmnt(masm_, "[ UnaryOperation (NOT)");
4420       if (context()->IsEffect()) {
4421         // Unary NOT has no side effects so it's only necessary to visit the
4422         // subexpression.  Match the optimizing compiler by not branching.
4423         VisitForEffect(expr->expression());
4424       } else if (context()->IsTest()) {
4425         const TestContext* test = TestContext::cast(context());
4426         // The labels are swapped for the recursive call.
4427         VisitForControl(expr->expression(),
4428                         test->false_label(),
4429                         test->true_label(),
4430                         test->fall_through());
4431         context()->Plug(test->true_label(), test->false_label());
4432       } else {
4433         DCHECK(context()->IsAccumulatorValue() || context()->IsStackValue());
4434         // TODO(jbramley): This could be much more efficient using (for
4435         // example) the CSEL instruction.
4436         Label materialize_true, materialize_false, done;
4437         VisitForControl(expr->expression(),
4438                         &materialize_false,
4439                         &materialize_true,
4440                         &materialize_true);
4441
4442         __ Bind(&materialize_true);
4443         PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
4444         __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
4445         __ B(&done);
4446
4447         __ Bind(&materialize_false);
4448         PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
4449         __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
4450         __ B(&done);
4451
4452         __ Bind(&done);
4453         if (context()->IsStackValue()) {
4454           __ Push(result_register());
4455         }
4456       }
4457       break;
4458     }
4459     case Token::TYPEOF: {
4460       Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
4461       {
4462         StackValueContext context(this);
4463         VisitForTypeofValue(expr->expression());
4464       }
4465       __ CallRuntime(Runtime::kTypeof, 1);
4466       context()->Plug(x0);
4467       break;
4468     }
4469     default:
4470       UNREACHABLE();
4471   }
4472 }
4473
4474
4475 void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
4476   DCHECK(expr->expression()->IsValidReferenceExpression());
4477
4478   Comment cmnt(masm_, "[ CountOperation");
4479   SetSourcePosition(expr->position());
4480
4481   Property* prop = expr->expression()->AsProperty();
4482   LhsKind assign_type = GetAssignType(prop);
4483
4484   // Evaluate expression and get value.
4485   if (assign_type == VARIABLE) {
4486     DCHECK(expr->expression()->AsVariableProxy()->var() != NULL);
4487     AccumulatorValueContext context(this);
4488     EmitVariableLoad(expr->expression()->AsVariableProxy());
4489   } else {
4490     // Reserve space for result of postfix operation.
4491     if (expr->is_postfix() && !context()->IsEffect()) {
4492       __ Push(xzr);
4493     }
4494     switch (assign_type) {
4495       case NAMED_PROPERTY: {
4496         // Put the object both on the stack and in the register.
4497         VisitForStackValue(prop->obj());
4498         __ Peek(LoadDescriptor::ReceiverRegister(), 0);
4499         EmitNamedPropertyLoad(prop);
4500         break;
4501       }
4502
4503       case NAMED_SUPER_PROPERTY: {
4504         VisitForStackValue(prop->obj()->AsSuperReference()->this_var());
4505         EmitLoadHomeObject(prop->obj()->AsSuperReference());
4506         __ Push(result_register());
4507         const Register scratch = x10;
4508         __ Peek(scratch, kPointerSize);
4509         __ Push(scratch, result_register());
4510         EmitNamedSuperPropertyLoad(prop);
4511         break;
4512       }
4513
4514       case KEYED_SUPER_PROPERTY: {
4515         VisitForStackValue(prop->obj()->AsSuperReference()->this_var());
4516         EmitLoadHomeObject(prop->obj()->AsSuperReference());
4517         __ Push(result_register());
4518         VisitForAccumulatorValue(prop->key());
4519         __ Push(result_register());
4520         const Register scratch1 = x10;
4521         const Register scratch2 = x11;
4522         __ Peek(scratch1, 2 * kPointerSize);
4523         __ Peek(scratch2, kPointerSize);
4524         __ Push(scratch1, scratch2, result_register());
4525         EmitKeyedSuperPropertyLoad(prop);
4526         break;
4527       }
4528
4529       case KEYED_PROPERTY: {
4530         VisitForStackValue(prop->obj());
4531         VisitForStackValue(prop->key());
4532         __ Peek(LoadDescriptor::ReceiverRegister(), 1 * kPointerSize);
4533         __ Peek(LoadDescriptor::NameRegister(), 0);
4534         EmitKeyedPropertyLoad(prop);
4535         break;
4536       }
4537
4538       case VARIABLE:
4539         UNREACHABLE();
4540     }
4541   }
4542
4543   // We need a second deoptimization point after loading the value
4544   // in case evaluating the property load my have a side effect.
4545   if (assign_type == VARIABLE) {
4546     PrepareForBailout(expr->expression(), TOS_REG);
4547   } else {
4548     PrepareForBailoutForId(prop->LoadId(), TOS_REG);
4549   }
4550
4551   // Inline smi case if we are in a loop.
4552   Label stub_call, done;
4553   JumpPatchSite patch_site(masm_);
4554
4555   int count_value = expr->op() == Token::INC ? 1 : -1;
4556   if (ShouldInlineSmiCase(expr->op())) {
4557     Label slow;
4558     patch_site.EmitJumpIfNotSmi(x0, &slow);
4559
4560     // Save result for postfix expressions.
4561     if (expr->is_postfix()) {
4562       if (!context()->IsEffect()) {
4563         // Save the result on the stack. If we have a named or keyed property we
4564         // store the result under the receiver that is currently on top of the
4565         // stack.
4566         switch (assign_type) {
4567           case VARIABLE:
4568             __ Push(x0);
4569             break;
4570           case NAMED_PROPERTY:
4571             __ Poke(x0, kPointerSize);
4572             break;
4573           case NAMED_SUPER_PROPERTY:
4574             __ Poke(x0, kPointerSize * 2);
4575             break;
4576           case KEYED_PROPERTY:
4577             __ Poke(x0, kPointerSize * 2);
4578             break;
4579           case KEYED_SUPER_PROPERTY:
4580             __ Poke(x0, kPointerSize * 3);
4581             break;
4582         }
4583       }
4584     }
4585
4586     __ Adds(x0, x0, Smi::FromInt(count_value));
4587     __ B(vc, &done);
4588     // Call stub. Undo operation first.
4589     __ Sub(x0, x0, Smi::FromInt(count_value));
4590     __ B(&stub_call);
4591     __ Bind(&slow);
4592   }
4593   ToNumberStub convert_stub(isolate());
4594   __ CallStub(&convert_stub);
4595   PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
4596
4597   // Save result for postfix expressions.
4598   if (expr->is_postfix()) {
4599     if (!context()->IsEffect()) {
4600       // Save the result on the stack. If we have a named or keyed property
4601       // we store the result under the receiver that is currently on top
4602       // of the stack.
4603       switch (assign_type) {
4604         case VARIABLE:
4605           __ Push(x0);
4606           break;
4607         case NAMED_PROPERTY:
4608           __ Poke(x0, kXRegSize);
4609           break;
4610         case NAMED_SUPER_PROPERTY:
4611           __ Poke(x0, 2 * kXRegSize);
4612           break;
4613         case KEYED_PROPERTY:
4614           __ Poke(x0, 2 * kXRegSize);
4615           break;
4616         case KEYED_SUPER_PROPERTY:
4617           __ Poke(x0, 3 * kXRegSize);
4618           break;
4619       }
4620     }
4621   }
4622
4623   __ Bind(&stub_call);
4624   __ Mov(x1, x0);
4625   __ Mov(x0, Smi::FromInt(count_value));
4626
4627   // Record position before stub call.
4628   SetSourcePosition(expr->position());
4629
4630   {
4631     Assembler::BlockPoolsScope scope(masm_);
4632     Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), Token::ADD).code();
4633     CallIC(code, expr->CountBinOpFeedbackId());
4634     patch_site.EmitPatchInfo();
4635   }
4636   __ Bind(&done);
4637
4638   // Store the value returned in x0.
4639   switch (assign_type) {
4640     case VARIABLE:
4641       if (expr->is_postfix()) {
4642         { EffectContext context(this);
4643           EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4644                                  Token::ASSIGN);
4645           PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4646           context.Plug(x0);
4647         }
4648         // For all contexts except EffectConstant We have the result on
4649         // top of the stack.
4650         if (!context()->IsEffect()) {
4651           context()->PlugTOS();
4652         }
4653       } else {
4654         EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4655                                Token::ASSIGN);
4656         PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4657         context()->Plug(x0);
4658       }
4659       break;
4660     case NAMED_PROPERTY: {
4661       __ Mov(StoreDescriptor::NameRegister(),
4662              Operand(prop->key()->AsLiteral()->value()));
4663       __ Pop(StoreDescriptor::ReceiverRegister());
4664       CallStoreIC(expr->CountStoreFeedbackId());
4665       PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4666       if (expr->is_postfix()) {
4667         if (!context()->IsEffect()) {
4668           context()->PlugTOS();
4669         }
4670       } else {
4671         context()->Plug(x0);
4672       }
4673       break;
4674     }
4675     case NAMED_SUPER_PROPERTY: {
4676       EmitNamedSuperPropertyStore(prop);
4677       if (expr->is_postfix()) {
4678         if (!context()->IsEffect()) {
4679           context()->PlugTOS();
4680         }
4681       } else {
4682         context()->Plug(x0);
4683       }
4684       break;
4685     }
4686     case KEYED_SUPER_PROPERTY: {
4687       EmitKeyedSuperPropertyStore(prop);
4688       if (expr->is_postfix()) {
4689         if (!context()->IsEffect()) {
4690           context()->PlugTOS();
4691         }
4692       } else {
4693         context()->Plug(x0);
4694       }
4695       break;
4696     }
4697     case KEYED_PROPERTY: {
4698       __ Pop(StoreDescriptor::NameRegister());
4699       __ Pop(StoreDescriptor::ReceiverRegister());
4700       Handle<Code> ic =
4701           CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
4702       CallIC(ic, expr->CountStoreFeedbackId());
4703       PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4704       if (expr->is_postfix()) {
4705         if (!context()->IsEffect()) {
4706           context()->PlugTOS();
4707         }
4708       } else {
4709         context()->Plug(x0);
4710       }
4711       break;
4712     }
4713   }
4714 }
4715
4716
4717 void FullCodeGenerator::VisitForTypeofValue(Expression* expr) {
4718   DCHECK(!context()->IsEffect());
4719   DCHECK(!context()->IsTest());
4720   VariableProxy* proxy = expr->AsVariableProxy();
4721   if (proxy != NULL && proxy->var()->IsUnallocated()) {
4722     Comment cmnt(masm_, "Global variable");
4723     __ Ldr(LoadDescriptor::ReceiverRegister(), GlobalObjectMemOperand());
4724     __ Mov(LoadDescriptor::NameRegister(), Operand(proxy->name()));
4725     if (FLAG_vector_ics) {
4726       __ Mov(VectorLoadICDescriptor::SlotRegister(),
4727              SmiFromSlot(proxy->VariableFeedbackSlot()));
4728     }
4729     // Use a regular load, not a contextual load, to avoid a reference
4730     // error.
4731     CallLoadIC(NOT_CONTEXTUAL);
4732     PrepareForBailout(expr, TOS_REG);
4733     context()->Plug(x0);
4734   } else if (proxy != NULL && proxy->var()->IsLookupSlot()) {
4735     Label done, slow;
4736
4737     // Generate code for loading from variables potentially shadowed
4738     // by eval-introduced variables.
4739     EmitDynamicLookupFastCase(proxy, INSIDE_TYPEOF, &slow, &done);
4740
4741     __ Bind(&slow);
4742     __ Mov(x0, Operand(proxy->name()));
4743     __ Push(cp, x0);
4744     __ CallRuntime(Runtime::kLoadLookupSlotNoReferenceError, 2);
4745     PrepareForBailout(expr, TOS_REG);
4746     __ Bind(&done);
4747
4748     context()->Plug(x0);
4749   } else {
4750     // This expression cannot throw a reference error at the top level.
4751     VisitInDuplicateContext(expr);
4752   }
4753 }
4754
4755
4756 void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
4757                                                  Expression* sub_expr,
4758                                                  Handle<String> check) {
4759   ASM_LOCATION("FullCodeGenerator::EmitLiteralCompareTypeof");
4760   Comment cmnt(masm_, "[ EmitLiteralCompareTypeof");
4761   Label materialize_true, materialize_false;
4762   Label* if_true = NULL;
4763   Label* if_false = NULL;
4764   Label* fall_through = NULL;
4765   context()->PrepareTest(&materialize_true, &materialize_false,
4766                          &if_true, &if_false, &fall_through);
4767
4768   { AccumulatorValueContext context(this);
4769     VisitForTypeofValue(sub_expr);
4770   }
4771   PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4772
4773   Factory* factory = isolate()->factory();
4774   if (String::Equals(check, factory->number_string())) {
4775     ASM_LOCATION("FullCodeGenerator::EmitLiteralCompareTypeof number_string");
4776     __ JumpIfSmi(x0, if_true);
4777     __ Ldr(x0, FieldMemOperand(x0, HeapObject::kMapOffset));
4778     __ CompareRoot(x0, Heap::kHeapNumberMapRootIndex);
4779     Split(eq, if_true, if_false, fall_through);
4780   } else if (String::Equals(check, factory->string_string())) {
4781     ASM_LOCATION("FullCodeGenerator::EmitLiteralCompareTypeof string_string");
4782     __ JumpIfSmi(x0, if_false);
4783     // Check for undetectable objects => false.
4784     __ JumpIfObjectType(x0, x0, x1, FIRST_NONSTRING_TYPE, if_false, ge);
4785     __ Ldrb(x1, FieldMemOperand(x0, Map::kBitFieldOffset));
4786     __ TestAndSplit(x1, 1 << Map::kIsUndetectable, if_true, if_false,
4787                     fall_through);
4788   } else if (String::Equals(check, factory->symbol_string())) {
4789     ASM_LOCATION("FullCodeGenerator::EmitLiteralCompareTypeof symbol_string");
4790     __ JumpIfSmi(x0, if_false);
4791     __ CompareObjectType(x0, x0, x1, SYMBOL_TYPE);
4792     Split(eq, if_true, if_false, fall_through);
4793   } else if (String::Equals(check, factory->boolean_string())) {
4794     ASM_LOCATION("FullCodeGenerator::EmitLiteralCompareTypeof boolean_string");
4795     __ JumpIfRoot(x0, Heap::kTrueValueRootIndex, if_true);
4796     __ CompareRoot(x0, Heap::kFalseValueRootIndex);
4797     Split(eq, if_true, if_false, fall_through);
4798   } else if (String::Equals(check, factory->undefined_string())) {
4799     ASM_LOCATION(
4800         "FullCodeGenerator::EmitLiteralCompareTypeof undefined_string");
4801     __ JumpIfRoot(x0, Heap::kUndefinedValueRootIndex, if_true);
4802     __ JumpIfSmi(x0, if_false);
4803     // Check for undetectable objects => true.
4804     __ Ldr(x0, FieldMemOperand(x0, HeapObject::kMapOffset));
4805     __ Ldrb(x1, FieldMemOperand(x0, Map::kBitFieldOffset));
4806     __ TestAndSplit(x1, 1 << Map::kIsUndetectable, if_false, if_true,
4807                     fall_through);
4808   } else if (String::Equals(check, factory->function_string())) {
4809     ASM_LOCATION("FullCodeGenerator::EmitLiteralCompareTypeof function_string");
4810     __ JumpIfSmi(x0, if_false);
4811     STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
4812     __ JumpIfObjectType(x0, x10, x11, JS_FUNCTION_TYPE, if_true);
4813     __ CompareAndSplit(x11, JS_FUNCTION_PROXY_TYPE, eq, if_true, if_false,
4814                        fall_through);
4815
4816   } else if (String::Equals(check, factory->object_string())) {
4817     ASM_LOCATION("FullCodeGenerator::EmitLiteralCompareTypeof object_string");
4818     __ JumpIfSmi(x0, if_false);
4819     __ JumpIfRoot(x0, Heap::kNullValueRootIndex, if_true);
4820     // Check for JS objects => true.
4821     Register map = x10;
4822     __ JumpIfObjectType(x0, map, x11, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE,
4823                         if_false, lt);
4824     __ CompareInstanceType(map, x11, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
4825     __ B(gt, if_false);
4826     // Check for undetectable objects => false.
4827     __ Ldrb(x10, FieldMemOperand(map, Map::kBitFieldOffset));
4828
4829     __ TestAndSplit(x10, 1 << Map::kIsUndetectable, if_true, if_false,
4830                     fall_through);
4831
4832   } else {
4833     ASM_LOCATION("FullCodeGenerator::EmitLiteralCompareTypeof other");
4834     if (if_false != fall_through) __ B(if_false);
4835   }
4836   context()->Plug(if_true, if_false);
4837 }
4838
4839
4840 void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
4841   Comment cmnt(masm_, "[ CompareOperation");
4842   SetSourcePosition(expr->position());
4843
4844   // Try to generate an optimized comparison with a literal value.
4845   // TODO(jbramley): This only checks common values like NaN or undefined.
4846   // Should it also handle ARM64 immediate operands?
4847   if (TryLiteralCompare(expr)) {
4848     return;
4849   }
4850
4851   // Assign labels according to context()->PrepareTest.
4852   Label materialize_true;
4853   Label materialize_false;
4854   Label* if_true = NULL;
4855   Label* if_false = NULL;
4856   Label* fall_through = NULL;
4857   context()->PrepareTest(&materialize_true, &materialize_false,
4858                          &if_true, &if_false, &fall_through);
4859
4860   Token::Value op = expr->op();
4861   VisitForStackValue(expr->left());
4862   switch (op) {
4863     case Token::IN:
4864       VisitForStackValue(expr->right());
4865       __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
4866       PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
4867       __ CompareRoot(x0, Heap::kTrueValueRootIndex);
4868       Split(eq, if_true, if_false, fall_through);
4869       break;
4870
4871     case Token::INSTANCEOF: {
4872       VisitForStackValue(expr->right());
4873       InstanceofStub stub(isolate(), InstanceofStub::kNoFlags);
4874       __ CallStub(&stub);
4875       PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4876       // The stub returns 0 for true.
4877       __ CompareAndSplit(x0, 0, eq, if_true, if_false, fall_through);
4878       break;
4879     }
4880
4881     default: {
4882       VisitForAccumulatorValue(expr->right());
4883       Condition cond = CompareIC::ComputeCondition(op);
4884
4885       // Pop the stack value.
4886       __ Pop(x1);
4887
4888       JumpPatchSite patch_site(masm_);
4889       if (ShouldInlineSmiCase(op)) {
4890         Label slow_case;
4891         patch_site.EmitJumpIfEitherNotSmi(x0, x1, &slow_case);
4892         __ Cmp(x1, x0);
4893         Split(cond, if_true, if_false, NULL);
4894         __ Bind(&slow_case);
4895       }
4896
4897       // Record position and call the compare IC.
4898       SetSourcePosition(expr->position());
4899       Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
4900       CallIC(ic, expr->CompareOperationFeedbackId());
4901       patch_site.EmitPatchInfo();
4902       PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4903       __ CompareAndSplit(x0, 0, cond, if_true, if_false, fall_through);
4904     }
4905   }
4906
4907   // Convert the result of the comparison into one expected for this
4908   // expression's context.
4909   context()->Plug(if_true, if_false);
4910 }
4911
4912
4913 void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4914                                               Expression* sub_expr,
4915                                               NilValue nil) {
4916   ASM_LOCATION("FullCodeGenerator::EmitLiteralCompareNil");
4917   Label materialize_true, materialize_false;
4918   Label* if_true = NULL;
4919   Label* if_false = NULL;
4920   Label* fall_through = NULL;
4921   context()->PrepareTest(&materialize_true, &materialize_false,
4922                          &if_true, &if_false, &fall_through);
4923
4924   VisitForAccumulatorValue(sub_expr);
4925   PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4926
4927   if (expr->op() == Token::EQ_STRICT) {
4928     Heap::RootListIndex nil_value = nil == kNullValue ?
4929         Heap::kNullValueRootIndex :
4930         Heap::kUndefinedValueRootIndex;
4931     __ CompareRoot(x0, nil_value);
4932     Split(eq, if_true, if_false, fall_through);
4933   } else {
4934     Handle<Code> ic = CompareNilICStub::GetUninitialized(isolate(), nil);
4935     CallIC(ic, expr->CompareOperationFeedbackId());
4936     __ CompareAndSplit(x0, 0, ne, if_true, if_false, fall_through);
4937   }
4938
4939   context()->Plug(if_true, if_false);
4940 }
4941
4942
4943 void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
4944   __ Ldr(x0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4945   context()->Plug(x0);
4946 }
4947
4948
4949 void FullCodeGenerator::VisitYield(Yield* expr) {
4950   Comment cmnt(masm_, "[ Yield");
4951   // Evaluate yielded value first; the initial iterator definition depends on
4952   // this. It stays on the stack while we update the iterator.
4953   VisitForStackValue(expr->expression());
4954
4955   // TODO(jbramley): Tidy this up once the merge is done, using named registers
4956   // and suchlike. The implementation changes a little by bleeding_edge so I
4957   // don't want to spend too much time on it now.
4958
4959   switch (expr->yield_kind()) {
4960     case Yield::kSuspend:
4961       // Pop value from top-of-stack slot; box result into result register.
4962       EmitCreateIteratorResult(false);
4963       __ Push(result_register());
4964       // Fall through.
4965     case Yield::kInitial: {
4966       Label suspend, continuation, post_runtime, resume;
4967
4968       __ B(&suspend);
4969
4970       // TODO(jbramley): This label is bound here because the following code
4971       // looks at its pos(). Is it possible to do something more efficient here,
4972       // perhaps using Adr?
4973       __ Bind(&continuation);
4974       __ B(&resume);
4975
4976       __ Bind(&suspend);
4977       VisitForAccumulatorValue(expr->generator_object());
4978       DCHECK((continuation.pos() > 0) && Smi::IsValid(continuation.pos()));
4979       __ Mov(x1, Smi::FromInt(continuation.pos()));
4980       __ Str(x1, FieldMemOperand(x0, JSGeneratorObject::kContinuationOffset));
4981       __ Str(cp, FieldMemOperand(x0, JSGeneratorObject::kContextOffset));
4982       __ Mov(x1, cp);
4983       __ RecordWriteField(x0, JSGeneratorObject::kContextOffset, x1, x2,
4984                           kLRHasBeenSaved, kDontSaveFPRegs);
4985       __ Add(x1, fp, StandardFrameConstants::kExpressionsOffset);
4986       __ Cmp(__ StackPointer(), x1);
4987       __ B(eq, &post_runtime);
4988       __ Push(x0);  // generator object
4989       __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
4990       __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4991       __ Bind(&post_runtime);
4992       __ Pop(result_register());
4993       EmitReturnSequence();
4994
4995       __ Bind(&resume);
4996       context()->Plug(result_register());
4997       break;
4998     }
4999
5000     case Yield::kFinal: {
5001       VisitForAccumulatorValue(expr->generator_object());
5002       __ Mov(x1, Smi::FromInt(JSGeneratorObject::kGeneratorClosed));
5003       __ Str(x1, FieldMemOperand(result_register(),
5004                                  JSGeneratorObject::kContinuationOffset));
5005       // Pop value from top-of-stack slot, box result into result register.
5006       EmitCreateIteratorResult(true);
5007       EmitUnwindBeforeReturn();
5008       EmitReturnSequence();
5009       break;
5010     }
5011
5012     case Yield::kDelegating: {
5013       VisitForStackValue(expr->generator_object());
5014
5015       // Initial stack layout is as follows:
5016       // [sp + 1 * kPointerSize] iter
5017       // [sp + 0 * kPointerSize] g
5018
5019       Label l_catch, l_try, l_suspend, l_continuation, l_resume;
5020       Label l_next, l_call, l_loop;
5021       Register load_receiver = LoadDescriptor::ReceiverRegister();
5022       Register load_name = LoadDescriptor::NameRegister();
5023
5024       // Initial send value is undefined.
5025       __ LoadRoot(x0, Heap::kUndefinedValueRootIndex);
5026       __ B(&l_next);
5027
5028       // catch (e) { receiver = iter; f = 'throw'; arg = e; goto l_call; }
5029       __ Bind(&l_catch);
5030       __ LoadRoot(load_name, Heap::kthrow_stringRootIndex);  // "throw"
5031       __ Peek(x3, 1 * kPointerSize);                         // iter
5032       __ Push(load_name, x3, x0);                       // "throw", iter, except
5033       __ B(&l_call);
5034
5035       // try { received = %yield result }
5036       // Shuffle the received result above a try handler and yield it without
5037       // re-boxing.
5038       __ Bind(&l_try);
5039       __ Pop(x0);                                        // result
5040       EnterTryBlock(expr->index(), &l_catch);
5041       const int try_block_size = TryCatch::kElementCount * kPointerSize;
5042       __ Push(x0);                                       // result
5043       __ B(&l_suspend);
5044
5045       // TODO(jbramley): This label is bound here because the following code
5046       // looks at its pos(). Is it possible to do something more efficient here,
5047       // perhaps using Adr?
5048       __ Bind(&l_continuation);
5049       __ B(&l_resume);
5050
5051       __ Bind(&l_suspend);
5052       const int generator_object_depth = kPointerSize + try_block_size;
5053       __ Peek(x0, generator_object_depth);
5054       __ Push(x0);                                       // g
5055       __ Push(Smi::FromInt(expr->index()));              // handler-index
5056       DCHECK((l_continuation.pos() > 0) && Smi::IsValid(l_continuation.pos()));
5057       __ Mov(x1, Smi::FromInt(l_continuation.pos()));
5058       __ Str(x1, FieldMemOperand(x0, JSGeneratorObject::kContinuationOffset));
5059       __ Str(cp, FieldMemOperand(x0, JSGeneratorObject::kContextOffset));
5060       __ Mov(x1, cp);
5061       __ RecordWriteField(x0, JSGeneratorObject::kContextOffset, x1, x2,
5062                           kLRHasBeenSaved, kDontSaveFPRegs);
5063       __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 2);
5064       __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
5065       __ Pop(x0);                                        // result
5066       EmitReturnSequence();
5067       __ Bind(&l_resume);                                // received in x0
5068       ExitTryBlock(expr->index());
5069
5070       // receiver = iter; f = 'next'; arg = received;
5071       __ Bind(&l_next);
5072
5073       __ LoadRoot(load_name, Heap::knext_stringRootIndex);  // "next"
5074       __ Peek(x3, 1 * kPointerSize);                        // iter
5075       __ Push(load_name, x3, x0);                      // "next", iter, received
5076
5077       // result = receiver[f](arg);
5078       __ Bind(&l_call);
5079       __ Peek(load_receiver, 1 * kPointerSize);
5080       __ Peek(load_name, 2 * kPointerSize);
5081       if (FLAG_vector_ics) {
5082         __ Mov(VectorLoadICDescriptor::SlotRegister(),
5083                SmiFromSlot(expr->KeyedLoadFeedbackSlot()));
5084       }
5085       Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate()).code();
5086       CallIC(ic, TypeFeedbackId::None());
5087       __ Mov(x1, x0);
5088       __ Poke(x1, 2 * kPointerSize);
5089       CallFunctionStub stub(isolate(), 1, CALL_AS_METHOD);
5090       __ CallStub(&stub);
5091
5092       __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
5093       __ Drop(1);  // The function is still on the stack; drop it.
5094
5095       // if (!result.done) goto l_try;
5096       __ Bind(&l_loop);
5097       __ Move(load_receiver, x0);
5098
5099       __ Push(load_receiver);                               // save result
5100       __ LoadRoot(load_name, Heap::kdone_stringRootIndex);  // "done"
5101       if (FLAG_vector_ics) {
5102         __ Mov(VectorLoadICDescriptor::SlotRegister(),
5103                SmiFromSlot(expr->DoneFeedbackSlot()));
5104       }
5105       CallLoadIC(NOT_CONTEXTUAL);                           // x0=result.done
5106       // The ToBooleanStub argument (result.done) is in x0.
5107       Handle<Code> bool_ic = ToBooleanStub::GetUninitialized(isolate());
5108       CallIC(bool_ic);
5109       __ Cbz(x0, &l_try);
5110
5111       // result.value
5112       __ Pop(load_receiver);                                 // result
5113       __ LoadRoot(load_name, Heap::kvalue_stringRootIndex);  // "value"
5114       if (FLAG_vector_ics) {
5115         __ Mov(VectorLoadICDescriptor::SlotRegister(),
5116                SmiFromSlot(expr->ValueFeedbackSlot()));
5117       }
5118       CallLoadIC(NOT_CONTEXTUAL);                            // x0=result.value
5119       context()->DropAndPlug(2, x0);                         // drop iter and g
5120       break;
5121     }
5122   }
5123 }
5124
5125
5126 void FullCodeGenerator::EmitGeneratorResume(Expression *generator,
5127     Expression *value,
5128     JSGeneratorObject::ResumeMode resume_mode) {
5129   ASM_LOCATION("FullCodeGenerator::EmitGeneratorResume");
5130   Register generator_object = x1;
5131   Register the_hole = x2;
5132   Register operand_stack_size = w3;
5133   Register function = x4;
5134
5135   // The value stays in x0, and is ultimately read by the resumed generator, as
5136   // if CallRuntime(Runtime::kSuspendJSGeneratorObject) returned it. Or it
5137   // is read to throw the value when the resumed generator is already closed. x1
5138   // will hold the generator object until the activation has been resumed.
5139   VisitForStackValue(generator);
5140   VisitForAccumulatorValue(value);
5141   __ Pop(generator_object);
5142
5143   // Load suspended function and context.
5144   __ Ldr(cp, FieldMemOperand(generator_object,
5145                              JSGeneratorObject::kContextOffset));
5146   __ Ldr(function, FieldMemOperand(generator_object,
5147                                    JSGeneratorObject::kFunctionOffset));
5148
5149   // Load receiver and store as the first argument.
5150   __ Ldr(x10, FieldMemOperand(generator_object,
5151                               JSGeneratorObject::kReceiverOffset));
5152   __ Push(x10);
5153
5154   // Push holes for the rest of the arguments to the generator function.
5155   __ Ldr(x10, FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
5156
5157   // The number of arguments is stored as an int32_t, and -1 is a marker
5158   // (SharedFunctionInfo::kDontAdaptArgumentsSentinel), so we need sign
5159   // extension to correctly handle it. However, in this case, we operate on
5160   // 32-bit W registers, so extension isn't required.
5161   __ Ldr(w10, FieldMemOperand(x10,
5162                               SharedFunctionInfo::kFormalParameterCountOffset));
5163   __ LoadRoot(the_hole, Heap::kTheHoleValueRootIndex);
5164   __ PushMultipleTimes(the_hole, w10);
5165
5166   // Enter a new JavaScript frame, and initialize its slots as they were when
5167   // the generator was suspended.
5168   Label resume_frame, done;
5169   __ Bl(&resume_frame);
5170   __ B(&done);
5171
5172   __ Bind(&resume_frame);
5173   __ Push(lr,           // Return address.
5174           fp,           // Caller's frame pointer.
5175           cp,           // Callee's context.
5176           function);    // Callee's JS Function.
5177   __ Add(fp, __ StackPointer(), kPointerSize * 2);
5178
5179   // Load and untag the operand stack size.
5180   __ Ldr(x10, FieldMemOperand(generator_object,
5181                               JSGeneratorObject::kOperandStackOffset));
5182   __ Ldr(operand_stack_size,
5183          UntagSmiFieldMemOperand(x10, FixedArray::kLengthOffset));
5184
5185   // If we are sending a value and there is no operand stack, we can jump back
5186   // in directly.
5187   if (resume_mode == JSGeneratorObject::NEXT) {
5188     Label slow_resume;
5189     __ Cbnz(operand_stack_size, &slow_resume);
5190     __ Ldr(x10, FieldMemOperand(function, JSFunction::kCodeEntryOffset));
5191     __ Ldrsw(x11,
5192              UntagSmiFieldMemOperand(generator_object,
5193                                      JSGeneratorObject::kContinuationOffset));
5194     __ Add(x10, x10, x11);
5195     __ Mov(x12, Smi::FromInt(JSGeneratorObject::kGeneratorExecuting));
5196     __ Str(x12, FieldMemOperand(generator_object,
5197                                 JSGeneratorObject::kContinuationOffset));
5198     __ Br(x10);
5199
5200     __ Bind(&slow_resume);
5201   }
5202
5203   // Otherwise, we push holes for the operand stack and call the runtime to fix
5204   // up the stack and the handlers.
5205   __ PushMultipleTimes(the_hole, operand_stack_size);
5206
5207   __ Mov(x10, Smi::FromInt(resume_mode));
5208   __ Push(generator_object, result_register(), x10);
5209   __ CallRuntime(Runtime::kResumeJSGeneratorObject, 3);
5210   // Not reached: the runtime call returns elsewhere.
5211   __ Unreachable();
5212
5213   __ Bind(&done);
5214   context()->Plug(result_register());
5215 }
5216
5217
5218 void FullCodeGenerator::EmitCreateIteratorResult(bool done) {
5219   Label gc_required;
5220   Label allocated;
5221
5222   const int instance_size = 5 * kPointerSize;
5223   DCHECK_EQ(isolate()->native_context()->iterator_result_map()->instance_size(),
5224             instance_size);
5225
5226   // Allocate and populate an object with this form: { value: VAL, done: DONE }
5227
5228   Register result = x0;
5229   __ Allocate(instance_size, result, x10, x11, &gc_required, TAG_OBJECT);
5230   __ B(&allocated);
5231
5232   __ Bind(&gc_required);
5233   __ Push(Smi::FromInt(instance_size));
5234   __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
5235   __ Ldr(context_register(),
5236          MemOperand(fp, StandardFrameConstants::kContextOffset));
5237
5238   __ Bind(&allocated);
5239   Register map_reg = x1;
5240   Register result_value = x2;
5241   Register boolean_done = x3;
5242   Register empty_fixed_array = x4;
5243   Register untagged_result = x5;
5244   __ Ldr(map_reg, GlobalObjectMemOperand());
5245   __ Ldr(map_reg, FieldMemOperand(map_reg, GlobalObject::kNativeContextOffset));
5246   __ Ldr(map_reg,
5247          ContextMemOperand(map_reg, Context::ITERATOR_RESULT_MAP_INDEX));
5248   __ Pop(result_value);
5249   __ Mov(boolean_done, Operand(isolate()->factory()->ToBoolean(done)));
5250   __ Mov(empty_fixed_array, Operand(isolate()->factory()->empty_fixed_array()));
5251   STATIC_ASSERT(JSObject::kPropertiesOffset + kPointerSize ==
5252                 JSObject::kElementsOffset);
5253   STATIC_ASSERT(JSGeneratorObject::kResultValuePropertyOffset + kPointerSize ==
5254                 JSGeneratorObject::kResultDonePropertyOffset);
5255   __ ObjectUntag(untagged_result, result);
5256   __ Str(map_reg, MemOperand(untagged_result, HeapObject::kMapOffset));
5257   __ Stp(empty_fixed_array, empty_fixed_array,
5258          MemOperand(untagged_result, JSObject::kPropertiesOffset));
5259   __ Stp(result_value, boolean_done,
5260          MemOperand(untagged_result,
5261                     JSGeneratorObject::kResultValuePropertyOffset));
5262
5263   // Only the value field needs a write barrier, as the other values are in the
5264   // root set.
5265   __ RecordWriteField(result, JSGeneratorObject::kResultValuePropertyOffset,
5266                       x10, x11, kLRHasBeenSaved, kDontSaveFPRegs);
5267 }
5268
5269
5270 // TODO(all): I don't like this method.
5271 // It seems to me that in too many places x0 is used in place of this.
5272 // Also, this function is not suitable for all places where x0 should be
5273 // abstracted (eg. when used as an argument). But some places assume that the
5274 // first argument register is x0, and use this function instead.
5275 // Considering that most of the register allocation is hard-coded in the
5276 // FullCodeGen, that it is unlikely we will need to change it extensively, and
5277 // that abstracting the allocation through functions would not yield any
5278 // performance benefit, I think the existence of this function is debatable.
5279 Register FullCodeGenerator::result_register() {
5280   return x0;
5281 }
5282
5283
5284 Register FullCodeGenerator::context_register() {
5285   return cp;
5286 }
5287
5288
5289 void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
5290   DCHECK(POINTER_SIZE_ALIGN(frame_offset) == frame_offset);
5291   __ Str(value, MemOperand(fp, frame_offset));
5292 }
5293
5294
5295 void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
5296   __ Ldr(dst, ContextMemOperand(cp, context_index));
5297 }
5298
5299
5300 void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
5301   Scope* declaration_scope = scope()->DeclarationScope();
5302   if (declaration_scope->is_script_scope() ||
5303       declaration_scope->is_module_scope()) {
5304     // Contexts nested in the native context have a canonical empty function
5305     // as their closure, not the anonymous closure containing the global
5306     // code.  Pass a smi sentinel and let the runtime look up the empty
5307     // function.
5308     DCHECK(kSmiTag == 0);
5309     __ Push(xzr);
5310   } else if (declaration_scope->is_eval_scope()) {
5311     // Contexts created by a call to eval have the same closure as the
5312     // context calling eval, not the anonymous closure containing the eval
5313     // code.  Fetch it from the context.
5314     __ Ldr(x10, ContextMemOperand(cp, Context::CLOSURE_INDEX));
5315     __ Push(x10);
5316   } else {
5317     DCHECK(declaration_scope->is_function_scope());
5318     __ Ldr(x10, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5319     __ Push(x10);
5320   }
5321 }
5322
5323
5324 void FullCodeGenerator::EnterFinallyBlock() {
5325   ASM_LOCATION("FullCodeGenerator::EnterFinallyBlock");
5326   DCHECK(!result_register().is(x10));
5327   // Preserve the result register while executing finally block.
5328   // Also cook the return address in lr to the stack (smi encoded Code* delta).
5329   __ Sub(x10, lr, Operand(masm_->CodeObject()));
5330   __ SmiTag(x10);
5331   __ Push(result_register(), x10);
5332
5333   // Store pending message while executing finally block.
5334   ExternalReference pending_message_obj =
5335       ExternalReference::address_of_pending_message_obj(isolate());
5336   __ Mov(x10, pending_message_obj);
5337   __ Ldr(x10, MemOperand(x10));
5338   __ Push(x10);
5339 }
5340
5341
5342 void FullCodeGenerator::ExitFinallyBlock() {
5343   ASM_LOCATION("FullCodeGenerator::ExitFinallyBlock");
5344   DCHECK(!result_register().is(x10));
5345
5346   // Restore pending message from stack.
5347   __ Pop(x10);
5348   ExternalReference pending_message_obj =
5349       ExternalReference::address_of_pending_message_obj(isolate());
5350   __ Mov(x13, pending_message_obj);
5351   __ Str(x10, MemOperand(x13));
5352
5353   // Restore result register and cooked return address from the stack.
5354   __ Pop(x10, result_register());
5355
5356   // Uncook the return address (see EnterFinallyBlock).
5357   __ SmiUntag(x10);
5358   __ Add(x11, x10, Operand(masm_->CodeObject()));
5359   __ Br(x11);
5360 }
5361
5362
5363 #undef __
5364
5365
5366 void BackEdgeTable::PatchAt(Code* unoptimized_code,
5367                             Address pc,
5368                             BackEdgeState target_state,
5369                             Code* replacement_code) {
5370   // Turn the jump into a nop.
5371   Address branch_address = pc - 3 * kInstructionSize;
5372   PatchingAssembler patcher(branch_address, 1);
5373
5374   DCHECK(Instruction::Cast(branch_address)
5375              ->IsNop(Assembler::INTERRUPT_CODE_NOP) ||
5376          (Instruction::Cast(branch_address)->IsCondBranchImm() &&
5377           Instruction::Cast(branch_address)->ImmPCOffset() ==
5378               6 * kInstructionSize));
5379
5380   switch (target_state) {
5381     case INTERRUPT:
5382       //  <decrement profiling counter>
5383       //  .. .. .. ..       b.pl ok
5384       //  .. .. .. ..       ldr x16, pc+<interrupt stub address>
5385       //  .. .. .. ..       blr x16
5386       //  ... more instructions.
5387       //  ok-label
5388       // Jump offset is 6 instructions.
5389       patcher.b(6, pl);
5390       break;
5391     case ON_STACK_REPLACEMENT:
5392     case OSR_AFTER_STACK_CHECK:
5393       //  <decrement profiling counter>
5394       //  .. .. .. ..       mov x0, x0 (NOP)
5395       //  .. .. .. ..       ldr x16, pc+<on-stack replacement address>
5396       //  .. .. .. ..       blr x16
5397       patcher.nop(Assembler::INTERRUPT_CODE_NOP);
5398       break;
5399   }
5400
5401   // Replace the call address.
5402   Instruction* load = Instruction::Cast(pc)->preceding(2);
5403   Address interrupt_address_pointer =
5404       reinterpret_cast<Address>(load) + load->ImmPCOffset();
5405   DCHECK((Memory::uint64_at(interrupt_address_pointer) ==
5406           reinterpret_cast<uint64_t>(unoptimized_code->GetIsolate()
5407                                          ->builtins()
5408                                          ->OnStackReplacement()
5409                                          ->entry())) ||
5410          (Memory::uint64_at(interrupt_address_pointer) ==
5411           reinterpret_cast<uint64_t>(unoptimized_code->GetIsolate()
5412                                          ->builtins()
5413                                          ->InterruptCheck()
5414                                          ->entry())) ||
5415          (Memory::uint64_at(interrupt_address_pointer) ==
5416           reinterpret_cast<uint64_t>(unoptimized_code->GetIsolate()
5417                                          ->builtins()
5418                                          ->OsrAfterStackCheck()
5419                                          ->entry())) ||
5420          (Memory::uint64_at(interrupt_address_pointer) ==
5421           reinterpret_cast<uint64_t>(unoptimized_code->GetIsolate()
5422                                          ->builtins()
5423                                          ->OnStackReplacement()
5424                                          ->entry())));
5425   Memory::uint64_at(interrupt_address_pointer) =
5426       reinterpret_cast<uint64_t>(replacement_code->entry());
5427
5428   unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
5429       unoptimized_code, reinterpret_cast<Address>(load), replacement_code);
5430 }
5431
5432
5433 BackEdgeTable::BackEdgeState BackEdgeTable::GetBackEdgeState(
5434     Isolate* isolate,
5435     Code* unoptimized_code,
5436     Address pc) {
5437   // TODO(jbramley): There should be some extra assertions here (as in the ARM
5438   // back-end), but this function is gone in bleeding_edge so it might not
5439   // matter anyway.
5440   Instruction* jump_or_nop = Instruction::Cast(pc)->preceding(3);
5441
5442   if (jump_or_nop->IsNop(Assembler::INTERRUPT_CODE_NOP)) {
5443     Instruction* load = Instruction::Cast(pc)->preceding(2);
5444     uint64_t entry = Memory::uint64_at(reinterpret_cast<Address>(load) +
5445                                        load->ImmPCOffset());
5446     if (entry == reinterpret_cast<uint64_t>(
5447         isolate->builtins()->OnStackReplacement()->entry())) {
5448       return ON_STACK_REPLACEMENT;
5449     } else if (entry == reinterpret_cast<uint64_t>(
5450         isolate->builtins()->OsrAfterStackCheck()->entry())) {
5451       return OSR_AFTER_STACK_CHECK;
5452     } else {
5453       UNREACHABLE();
5454     }
5455   }
5456
5457   return INTERRUPT;
5458 }
5459
5460
5461 } }  // namespace v8::internal
5462
5463 #endif  // V8_TARGET_ARCH_ARM64