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