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