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.
9 #include "src/code-factory.h"
10 #include "src/code-stubs.h"
11 #include "src/codegen.h"
12 #include "src/compiler.h"
13 #include "src/debug.h"
14 #include "src/full-codegen/full-codegen.h"
15 #include "src/ic/ic.h"
16 #include "src/parser.h"
17 #include "src/scopes.h"
19 #include "src/ppc/code-stubs-ppc.h"
20 #include "src/ppc/macro-assembler-ppc.h"
25 #define __ ACCESS_MASM(masm_)
27 // A patch site is a location in the code which it is possible to patch. This
28 // class has a number of methods to emit the code which is patchable and the
29 // method EmitPatchInfo to record a marker back to the patchable code. This
30 // marker is a cmpi rx, #yyy instruction, and x * 0x0000ffff + yyy (raw 16 bit
31 // immediate value is used) is the delta from the pc to the first instruction of
32 // the patchable code.
33 // See PatchInlinedSmiCode in ic-ppc.cc for the code that patches it
34 class JumpPatchSite BASE_EMBEDDED {
36 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
38 info_emitted_ = false;
42 ~JumpPatchSite() { DCHECK(patch_site_.is_bound() == info_emitted_); }
44 // When initially emitting this ensure that a jump is always generated to skip
45 // the inlined smi code.
46 void EmitJumpIfNotSmi(Register reg, Label* target) {
47 DCHECK(!patch_site_.is_bound() && !info_emitted_);
48 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
49 __ bind(&patch_site_);
50 __ cmp(reg, reg, cr0);
51 __ beq(target, cr0); // Always taken before patched.
54 // When initially emitting this ensure that a jump is never generated to skip
55 // the inlined smi code.
56 void EmitJumpIfSmi(Register reg, Label* target) {
57 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
58 DCHECK(!patch_site_.is_bound() && !info_emitted_);
59 __ bind(&patch_site_);
60 __ cmp(reg, reg, cr0);
61 __ bne(target, cr0); // Never taken before patched.
64 void EmitPatchInfo() {
65 if (patch_site_.is_bound()) {
66 int delta_to_patch_site = masm_->InstructionsGeneratedSince(&patch_site_);
68 // I believe this is using reg as the high bits of of the offset
69 reg.set_code(delta_to_patch_site / kOff16Mask);
70 __ cmpi(reg, Operand(delta_to_patch_site % kOff16Mask));
75 __ nop(); // Signals no inlined code.
80 MacroAssembler* masm_;
88 // Generate code for a JS function. On entry to the function the receiver
89 // and arguments have been pushed on the stack left to right. The actual
90 // argument count matches the formal parameter count expected by the
93 // The live registers are:
94 // o r4: the JS function object being called (i.e., ourselves)
96 // o fp: our caller's frame pointer (aka r31)
97 // o sp: stack pointer
98 // o lr: return address
99 // o ip: our own function entry (required by the prologue)
101 // The function builds a JS frame. Please see JavaScriptFrameConstants in
102 // frames-ppc.h for its layout.
103 void FullCodeGenerator::Generate() {
104 CompilationInfo* info = info_;
105 profiling_counter_ = isolate()->factory()->NewCell(
106 Handle<Smi>(Smi::FromInt(FLAG_interrupt_budget), isolate()));
107 SetFunctionPosition(function());
108 Comment cmnt(masm_, "[ function compiled by full code generator");
110 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
113 if (strlen(FLAG_stop_at) > 0 &&
114 info->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
119 // Sloppy mode functions and builtins need to replace the receiver with the
120 // global proxy when called as functions (without an explicit receiver
122 if (is_sloppy(info->language_mode()) && !info->is_native() &&
123 info->MayUseThis() && info->scope()->has_this_declaration()) {
125 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
126 __ LoadP(r5, MemOperand(sp, receiver_offset), r0);
127 __ CompareRoot(r5, Heap::kUndefinedValueRootIndex);
130 __ LoadP(r5, GlobalObjectOperand());
131 __ LoadP(r5, FieldMemOperand(r5, GlobalObject::kGlobalProxyOffset));
133 __ StoreP(r5, MemOperand(sp, receiver_offset), r0);
138 // Open a frame scope to indicate that there is a frame on the stack. The
139 // MANUAL indicates that the scope shouldn't actually generate code to set up
140 // the frame (that is done below).
141 FrameScope frame_scope(masm_, StackFrame::MANUAL);
142 int prologue_offset = masm_->pc_offset();
144 if (prologue_offset) {
145 // Prologue logic requires it's starting address in ip and the
146 // corresponding offset from the function entry.
147 prologue_offset += Instruction::kInstrSize;
148 __ addi(ip, ip, Operand(prologue_offset));
150 info->set_prologue_offset(prologue_offset);
151 __ Prologue(info->IsCodePreAgingActive(), prologue_offset);
152 info->AddNoFrameRange(0, masm_->pc_offset());
155 Comment cmnt(masm_, "[ Allocate locals");
156 int locals_count = info->scope()->num_stack_slots();
157 // Generators allocate locals, if any, in context slots.
158 DCHECK(!IsGeneratorFunction(info->function()->kind()) || locals_count == 0);
159 if (locals_count > 0) {
160 if (locals_count >= 128) {
162 __ Add(ip, sp, -(locals_count * kPointerSize), r0);
163 __ LoadRoot(r5, Heap::kRealStackLimitRootIndex);
165 __ bc_short(ge, &ok);
166 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
169 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
170 int kMaxPushes = FLAG_optimize_for_size ? 4 : 32;
171 if (locals_count >= kMaxPushes) {
172 int loop_iterations = locals_count / kMaxPushes;
173 __ mov(r5, Operand(loop_iterations));
176 __ bind(&loop_header);
178 for (int i = 0; i < kMaxPushes; i++) {
181 // Continue loop if not done.
182 __ bdnz(&loop_header);
184 int remaining = locals_count % kMaxPushes;
185 // Emit the remaining pushes.
186 for (int i = 0; i < remaining; i++) {
192 bool function_in_register = true;
194 // Possibly allocate a local context.
195 if (info->scope()->num_heap_slots() > 0) {
196 // Argument to NewContext is the function, which is still in r4.
197 Comment cmnt(masm_, "[ Allocate context");
198 bool need_write_barrier = true;
199 int slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
200 if (info->scope()->is_script_scope()) {
202 __ Push(info->scope()->GetScopeInfo(info->isolate()));
203 __ CallRuntime(Runtime::kNewScriptContext, 2);
204 } else if (slots <= FastNewContextStub::kMaximumSlots) {
205 FastNewContextStub stub(isolate(), slots);
207 // Result of FastNewContextStub is always in new space.
208 need_write_barrier = false;
211 __ CallRuntime(Runtime::kNewFunctionContext, 1);
213 function_in_register = false;
214 // Context is returned in r3. It replaces the context passed to us.
215 // It's saved in the stack and kept live in cp.
217 __ StoreP(r3, MemOperand(fp, StandardFrameConstants::kContextOffset));
218 // Copy any necessary parameters into the context.
219 int num_parameters = info->scope()->num_parameters();
220 int first_parameter = info->scope()->has_this_declaration() ? -1 : 0;
221 for (int i = first_parameter; i < num_parameters; i++) {
222 Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
223 if (var->IsContextSlot()) {
224 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
225 (num_parameters - 1 - i) * kPointerSize;
226 // Load parameter from stack.
227 __ LoadP(r3, MemOperand(fp, parameter_offset), r0);
228 // Store it in the context.
229 MemOperand target = ContextOperand(cp, var->index());
230 __ StoreP(r3, target, r0);
232 // Update the write barrier.
233 if (need_write_barrier) {
234 __ RecordWriteContextSlot(cp, target.offset(), r3, r6,
235 kLRHasBeenSaved, kDontSaveFPRegs);
236 } else if (FLAG_debug_code) {
238 __ JumpIfInNewSpace(cp, r3, &done);
239 __ Abort(kExpectedNewSpaceObject);
246 // Possibly set up a local binding to the this function which is used in
247 // derived constructors with super calls.
248 Variable* this_function_var = scope()->this_function_var();
249 if (this_function_var != nullptr) {
250 Comment cmnt(masm_, "[ This function");
251 if (!function_in_register) {
252 __ LoadP(r4, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
253 // The write barrier clobbers register again, keep is marked as such.
255 SetVar(this_function_var, r4, r3, r5);
258 Variable* new_target_var = scope()->new_target_var();
259 if (new_target_var != nullptr) {
260 Comment cmnt(masm_, "[ new.target");
262 // Get the frame pointer for the calling frame.
263 __ LoadP(r5, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
265 // Skip the arguments adaptor frame if it exists.
266 __ LoadP(r4, MemOperand(r5, StandardFrameConstants::kContextOffset));
267 __ CmpSmiLiteral(r4, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR), r0);
270 __ LoadP(r5, MemOperand(r5, StandardFrameConstants::kCallerFPOffset));
273 // Check the marker in the calling frame.
274 __ LoadP(r4, MemOperand(r5, StandardFrameConstants::kMarkerOffset));
275 __ CmpSmiLiteral(r4, Smi::FromInt(StackFrame::CONSTRUCT), r0);
276 Label non_construct_frame, done;
278 __ bne(&non_construct_frame);
279 __ LoadP(r3, MemOperand(
280 r5, ConstructFrameConstants::kOriginalConstructorOffset));
283 __ bind(&non_construct_frame);
284 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
287 SetVar(new_target_var, r3, r5, r6);
290 // Possibly allocate RestParameters
292 Variable* rest_param = scope()->rest_parameter(&rest_index);
294 Comment cmnt(masm_, "[ Allocate rest parameter array");
296 int num_parameters = info->scope()->num_parameters();
297 int offset = num_parameters * kPointerSize;
299 __ addi(r6, fp, Operand(StandardFrameConstants::kCallerSPOffset + offset));
300 __ LoadSmiLiteral(r5, Smi::FromInt(num_parameters));
301 __ LoadSmiLiteral(r4, Smi::FromInt(rest_index));
302 __ LoadSmiLiteral(r3, Smi::FromInt(language_mode()));
303 __ Push(r6, r5, r4, r3);
305 RestParamAccessStub stub(isolate());
308 SetVar(rest_param, r3, r4, r5);
311 Variable* arguments = scope()->arguments();
312 if (arguments != NULL) {
313 // Function uses arguments object.
314 Comment cmnt(masm_, "[ Allocate arguments object");
315 if (!function_in_register) {
316 // Load this again, if it's used by the local context below.
317 __ LoadP(r6, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
321 // Receiver is just before the parameters on the caller's stack.
322 int num_parameters = info->scope()->num_parameters();
323 int offset = num_parameters * kPointerSize;
324 __ addi(r5, fp, Operand(StandardFrameConstants::kCallerSPOffset + offset));
325 __ LoadSmiLiteral(r4, Smi::FromInt(num_parameters));
328 // Arguments to ArgumentsAccessStub:
329 // function, receiver address, parameter count.
330 // The stub will rewrite receiever and parameter count if the previous
331 // stack frame was an arguments adapter frame.
332 ArgumentsAccessStub::Type type;
333 if (is_strict(language_mode()) || !is_simple_parameter_list()) {
334 type = ArgumentsAccessStub::NEW_STRICT;
335 } else if (function()->has_duplicate_parameters()) {
336 type = ArgumentsAccessStub::NEW_SLOPPY_SLOW;
338 type = ArgumentsAccessStub::NEW_SLOPPY_FAST;
340 ArgumentsAccessStub stub(isolate(), type);
343 SetVar(arguments, r3, r4, r5);
347 __ CallRuntime(Runtime::kTraceEnter, 0);
350 // Visit the declarations and body unless there is an illegal
352 if (scope()->HasIllegalRedeclaration()) {
353 Comment cmnt(masm_, "[ Declarations");
354 scope()->VisitIllegalRedeclaration(this);
357 PrepareForBailoutForId(BailoutId::FunctionEntry(), NO_REGISTERS);
359 Comment cmnt(masm_, "[ Declarations");
360 VisitDeclarations(scope()->declarations());
364 Comment cmnt(masm_, "[ Stack check");
365 PrepareForBailoutForId(BailoutId::Declarations(), NO_REGISTERS);
367 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
369 __ bc_short(ge, &ok);
370 __ Call(isolate()->builtins()->StackCheck(), RelocInfo::CODE_TARGET);
375 Comment cmnt(masm_, "[ Body");
376 DCHECK(loop_depth() == 0);
377 VisitStatements(function()->body());
378 DCHECK(loop_depth() == 0);
382 // Always emit a 'return undefined' in case control fell off the end of
385 Comment cmnt(masm_, "[ return <undefined>;");
386 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
388 EmitReturnSequence();
390 if (HasStackOverflow()) {
391 masm_->AbortConstantPoolBuilding();
396 void FullCodeGenerator::ClearAccumulator() {
397 __ LoadSmiLiteral(r3, Smi::FromInt(0));
401 void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
402 __ mov(r5, Operand(profiling_counter_));
403 __ LoadP(r6, FieldMemOperand(r5, Cell::kValueOffset));
404 __ SubSmiLiteral(r6, r6, Smi::FromInt(delta), r0);
405 __ StoreP(r6, FieldMemOperand(r5, Cell::kValueOffset), r0);
409 void FullCodeGenerator::EmitProfilingCounterReset() {
410 int reset_value = FLAG_interrupt_budget;
411 if (info_->is_debug()) {
412 // Detect debug break requests as soon as possible.
413 reset_value = FLAG_interrupt_budget >> 4;
415 __ mov(r5, Operand(profiling_counter_));
416 __ LoadSmiLiteral(r6, Smi::FromInt(reset_value));
417 __ StoreP(r6, FieldMemOperand(r5, Cell::kValueOffset), r0);
421 void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
422 Label* back_edge_target) {
423 Comment cmnt(masm_, "[ Back edge bookkeeping");
426 DCHECK(back_edge_target->is_bound());
427 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target) +
428 kCodeSizeMultiplier / 2;
429 int weight = Min(kMaxBackEdgeWeight, Max(1, distance / kCodeSizeMultiplier));
430 EmitProfilingCounterDecrement(weight);
432 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
433 Assembler::BlockConstantPoolEntrySharingScope prevent_entry_sharing(masm_);
434 // BackEdgeTable::PatchAt manipulates this sequence.
435 __ cmpi(r6, Operand::Zero());
436 __ bc_short(ge, &ok);
437 __ Call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
439 // Record a mapping of this PC offset to the OSR id. This is used to find
440 // the AST id from the unoptimized code in order to use it as a key into
441 // the deoptimization input data found in the optimized code.
442 RecordBackEdge(stmt->OsrEntryId());
444 EmitProfilingCounterReset();
447 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
448 // Record a mapping of the OSR id to this PC. This is used if the OSR
449 // entry becomes the target of a bailout. We don't expect it to be, but
450 // we want it to work if it is.
451 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
455 void FullCodeGenerator::EmitReturnSequence() {
456 Comment cmnt(masm_, "[ Return sequence");
457 if (return_label_.is_bound()) {
458 __ b(&return_label_);
460 __ bind(&return_label_);
462 // Push the return value on the stack as the parameter.
463 // Runtime::TraceExit returns its parameter in r3
465 __ CallRuntime(Runtime::kTraceExit, 1);
467 // Pretend that the exit is a backwards jump to the entry.
469 if (info_->ShouldSelfOptimize()) {
470 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
472 int distance = masm_->pc_offset() + kCodeSizeMultiplier / 2;
473 weight = Min(kMaxBackEdgeWeight, Max(1, distance / kCodeSizeMultiplier));
475 EmitProfilingCounterDecrement(weight);
477 __ cmpi(r6, Operand::Zero());
480 __ Call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
482 EmitProfilingCounterReset();
485 // Make sure that the constant pool is not emitted inside of the return
488 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
489 int32_t arg_count = info_->scope()->num_parameters() + 1;
490 int32_t sp_delta = arg_count * kPointerSize;
491 SetReturnPosition(function());
492 int no_frame_start = __ LeaveFrame(StackFrame::JAVA_SCRIPT, sp_delta);
494 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
500 void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
501 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
502 codegen()->GetVar(result_register(), var);
503 __ push(result_register());
507 void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {}
510 void FullCodeGenerator::AccumulatorValueContext::Plug(
511 Heap::RootListIndex index) const {
512 __ LoadRoot(result_register(), index);
516 void FullCodeGenerator::StackValueContext::Plug(
517 Heap::RootListIndex index) const {
518 __ LoadRoot(result_register(), index);
519 __ push(result_register());
523 void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
524 codegen()->PrepareForBailoutBeforeSplit(condition(), true, true_label_,
526 if (index == Heap::kUndefinedValueRootIndex ||
527 index == Heap::kNullValueRootIndex ||
528 index == Heap::kFalseValueRootIndex) {
529 if (false_label_ != fall_through_) __ b(false_label_);
530 } else if (index == Heap::kTrueValueRootIndex) {
531 if (true_label_ != fall_through_) __ b(true_label_);
533 __ LoadRoot(result_register(), index);
534 codegen()->DoTest(this);
539 void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {}
542 void FullCodeGenerator::AccumulatorValueContext::Plug(
543 Handle<Object> lit) const {
544 __ mov(result_register(), Operand(lit));
548 void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
549 // Immediates cannot be pushed directly.
550 __ mov(result_register(), Operand(lit));
551 __ push(result_register());
555 void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
556 codegen()->PrepareForBailoutBeforeSplit(condition(), true, true_label_,
558 DCHECK(!lit->IsUndetectableObject()); // There are no undetectable literals.
559 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
560 if (false_label_ != fall_through_) __ b(false_label_);
561 } else if (lit->IsTrue() || lit->IsJSObject()) {
562 if (true_label_ != fall_through_) __ b(true_label_);
563 } else if (lit->IsString()) {
564 if (String::cast(*lit)->length() == 0) {
565 if (false_label_ != fall_through_) __ b(false_label_);
567 if (true_label_ != fall_through_) __ b(true_label_);
569 } else if (lit->IsSmi()) {
570 if (Smi::cast(*lit)->value() == 0) {
571 if (false_label_ != fall_through_) __ b(false_label_);
573 if (true_label_ != fall_through_) __ b(true_label_);
576 // For simplicity we always test the accumulator register.
577 __ mov(result_register(), Operand(lit));
578 codegen()->DoTest(this);
583 void FullCodeGenerator::EffectContext::DropAndPlug(int count,
584 Register reg) const {
590 void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
591 int count, Register reg) const {
594 __ Move(result_register(), reg);
598 void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
599 Register reg) const {
601 if (count > 1) __ Drop(count - 1);
602 __ StoreP(reg, MemOperand(sp, 0));
606 void FullCodeGenerator::TestContext::DropAndPlug(int count,
607 Register reg) const {
609 // For simplicity we always test the accumulator register.
611 __ Move(result_register(), reg);
612 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
613 codegen()->DoTest(this);
617 void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
618 Label* materialize_false) const {
619 DCHECK(materialize_true == materialize_false);
620 __ bind(materialize_true);
624 void FullCodeGenerator::AccumulatorValueContext::Plug(
625 Label* materialize_true, Label* materialize_false) const {
627 __ bind(materialize_true);
628 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
630 __ bind(materialize_false);
631 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
636 void FullCodeGenerator::StackValueContext::Plug(
637 Label* materialize_true, Label* materialize_false) const {
639 __ bind(materialize_true);
640 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
642 __ bind(materialize_false);
643 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
649 void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
650 Label* materialize_false) const {
651 DCHECK(materialize_true == true_label_);
652 DCHECK(materialize_false == false_label_);
656 void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
657 Heap::RootListIndex value_root_index =
658 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
659 __ LoadRoot(result_register(), value_root_index);
663 void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
664 Heap::RootListIndex value_root_index =
665 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
666 __ LoadRoot(ip, value_root_index);
671 void FullCodeGenerator::TestContext::Plug(bool flag) const {
672 codegen()->PrepareForBailoutBeforeSplit(condition(), true, true_label_,
675 if (true_label_ != fall_through_) __ b(true_label_);
677 if (false_label_ != fall_through_) __ b(false_label_);
682 void FullCodeGenerator::DoTest(Expression* condition, Label* if_true,
683 Label* if_false, Label* fall_through) {
684 Handle<Code> ic = ToBooleanStub::GetUninitialized(isolate());
685 CallIC(ic, condition->test_id());
686 __ cmpi(result_register(), Operand::Zero());
687 Split(ne, if_true, if_false, fall_through);
691 void FullCodeGenerator::Split(Condition cond, Label* if_true, Label* if_false,
692 Label* fall_through, CRegister cr) {
693 if (if_false == fall_through) {
694 __ b(cond, if_true, cr);
695 } else if (if_true == fall_through) {
696 __ b(NegateCondition(cond), if_false, cr);
698 __ b(cond, if_true, cr);
704 MemOperand FullCodeGenerator::StackOperand(Variable* var) {
705 DCHECK(var->IsStackAllocated());
706 // Offset is negative because higher indexes are at lower addresses.
707 int offset = -var->index() * kPointerSize;
708 // Adjust by a (parameter or local) base offset.
709 if (var->IsParameter()) {
710 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
712 offset += JavaScriptFrameConstants::kLocal0Offset;
714 return MemOperand(fp, offset);
718 MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
719 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
720 if (var->IsContextSlot()) {
721 int context_chain_length = scope()->ContextChainLength(var->scope());
722 __ LoadContext(scratch, context_chain_length);
723 return ContextOperand(scratch, var->index());
725 return StackOperand(var);
730 void FullCodeGenerator::GetVar(Register dest, Variable* var) {
731 // Use destination as scratch.
732 MemOperand location = VarOperand(var, dest);
733 __ LoadP(dest, location, r0);
737 void FullCodeGenerator::SetVar(Variable* var, Register src, Register scratch0,
739 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
740 DCHECK(!scratch0.is(src));
741 DCHECK(!scratch0.is(scratch1));
742 DCHECK(!scratch1.is(src));
743 MemOperand location = VarOperand(var, scratch0);
744 __ StoreP(src, location, r0);
746 // Emit the write barrier code if the location is in the heap.
747 if (var->IsContextSlot()) {
748 __ RecordWriteContextSlot(scratch0, location.offset(), src, scratch1,
749 kLRHasBeenSaved, kDontSaveFPRegs);
754 void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
755 bool should_normalize,
758 // Only prepare for bailouts before splits if we're in a test
759 // context. Otherwise, we let the Visit function deal with the
760 // preparation to avoid preparing with the same AST id twice.
761 if (!context()->IsTest() || !info_->IsOptimizable()) return;
764 if (should_normalize) __ b(&skip);
765 PrepareForBailout(expr, TOS_REG);
766 if (should_normalize) {
767 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
769 Split(eq, if_true, if_false, NULL);
775 void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
776 // The variable in the declaration always resides in the current function
778 DCHECK_EQ(0, scope()->ContextChainLength(variable->scope()));
779 if (generate_debug_code_) {
780 // Check that we're not inside a with or catch context.
781 __ LoadP(r4, FieldMemOperand(cp, HeapObject::kMapOffset));
782 __ CompareRoot(r4, Heap::kWithContextMapRootIndex);
783 __ Check(ne, kDeclarationInWithContext);
784 __ CompareRoot(r4, Heap::kCatchContextMapRootIndex);
785 __ Check(ne, kDeclarationInCatchContext);
790 void FullCodeGenerator::VisitVariableDeclaration(
791 VariableDeclaration* declaration) {
792 // If it was not possible to allocate the variable at compile time, we
793 // need to "declare" it at runtime to make sure it actually exists in the
795 VariableProxy* proxy = declaration->proxy();
796 VariableMode mode = declaration->mode();
797 Variable* variable = proxy->var();
798 bool hole_init = mode == LET || mode == CONST || mode == CONST_LEGACY;
799 switch (variable->location()) {
800 case VariableLocation::GLOBAL:
801 case VariableLocation::UNALLOCATED:
802 globals_->Add(variable->name(), zone());
803 globals_->Add(variable->binding_needs_init()
804 ? isolate()->factory()->the_hole_value()
805 : isolate()->factory()->undefined_value(),
809 case VariableLocation::PARAMETER:
810 case VariableLocation::LOCAL:
812 Comment cmnt(masm_, "[ VariableDeclaration");
813 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
814 __ StoreP(ip, StackOperand(variable));
818 case VariableLocation::CONTEXT:
820 Comment cmnt(masm_, "[ VariableDeclaration");
821 EmitDebugCheckDeclarationContext(variable);
822 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
823 __ StoreP(ip, ContextOperand(cp, variable->index()), r0);
824 // No write barrier since the_hole_value is in old space.
825 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
829 case VariableLocation::LOOKUP: {
830 Comment cmnt(masm_, "[ VariableDeclaration");
831 __ mov(r5, Operand(variable->name()));
832 // Declaration nodes are always introduced in one of four modes.
833 DCHECK(IsDeclaredVariableMode(mode));
834 PropertyAttributes attr =
835 IsImmutableVariableMode(mode) ? READ_ONLY : NONE;
836 __ LoadSmiLiteral(r4, Smi::FromInt(attr));
837 // Push initial value, if any.
838 // Note: For variables we must not push an initial value (such as
839 // 'undefined') because we may have a (legal) redeclaration and we
840 // must not destroy the current value.
842 __ LoadRoot(r3, Heap::kTheHoleValueRootIndex);
843 __ Push(cp, r5, r4, r3);
845 __ LoadSmiLiteral(r3, Smi::FromInt(0)); // Indicates no initial value.
846 __ Push(cp, r5, r4, r3);
848 __ CallRuntime(Runtime::kDeclareLookupSlot, 4);
855 void FullCodeGenerator::VisitFunctionDeclaration(
856 FunctionDeclaration* declaration) {
857 VariableProxy* proxy = declaration->proxy();
858 Variable* variable = proxy->var();
859 switch (variable->location()) {
860 case VariableLocation::GLOBAL:
861 case VariableLocation::UNALLOCATED: {
862 globals_->Add(variable->name(), zone());
863 Handle<SharedFunctionInfo> function =
864 Compiler::GetSharedFunctionInfo(declaration->fun(), script(), info_);
865 // Check for stack-overflow exception.
866 if (function.is_null()) return SetStackOverflow();
867 globals_->Add(function, zone());
871 case VariableLocation::PARAMETER:
872 case VariableLocation::LOCAL: {
873 Comment cmnt(masm_, "[ FunctionDeclaration");
874 VisitForAccumulatorValue(declaration->fun());
875 __ StoreP(result_register(), StackOperand(variable));
879 case VariableLocation::CONTEXT: {
880 Comment cmnt(masm_, "[ FunctionDeclaration");
881 EmitDebugCheckDeclarationContext(variable);
882 VisitForAccumulatorValue(declaration->fun());
883 __ StoreP(result_register(), ContextOperand(cp, variable->index()), r0);
884 int offset = Context::SlotOffset(variable->index());
885 // We know that we have written a function, which is not a smi.
886 __ RecordWriteContextSlot(cp, offset, result_register(), r5,
887 kLRHasBeenSaved, kDontSaveFPRegs,
888 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
889 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
893 case VariableLocation::LOOKUP: {
894 Comment cmnt(masm_, "[ FunctionDeclaration");
895 __ mov(r5, Operand(variable->name()));
896 __ LoadSmiLiteral(r4, Smi::FromInt(NONE));
898 // Push initial value for function declaration.
899 VisitForStackValue(declaration->fun());
900 __ CallRuntime(Runtime::kDeclareLookupSlot, 4);
907 void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
908 // Call the runtime to declare the globals.
909 // The context is the first argument.
910 __ mov(r4, Operand(pairs));
911 __ LoadSmiLiteral(r3, Smi::FromInt(DeclareGlobalsFlags()));
913 __ CallRuntime(Runtime::kDeclareGlobals, 3);
914 // Return value is ignored.
918 void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
919 // Call the runtime to declare the modules.
920 __ Push(descriptions);
921 __ CallRuntime(Runtime::kDeclareModules, 1);
922 // Return value is ignored.
926 void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
927 Comment cmnt(masm_, "[ SwitchStatement");
928 Breakable nested_statement(this, stmt);
929 SetStatementPosition(stmt);
931 // Keep the switch value on the stack until a case matches.
932 VisitForStackValue(stmt->tag());
933 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
935 ZoneList<CaseClause*>* clauses = stmt->cases();
936 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
938 Label next_test; // Recycled for each test.
939 // Compile all the tests with branches to their bodies.
940 for (int i = 0; i < clauses->length(); i++) {
941 CaseClause* clause = clauses->at(i);
942 clause->body_target()->Unuse();
944 // The default is not a test, but remember it as final fall through.
945 if (clause->is_default()) {
946 default_clause = clause;
950 Comment cmnt(masm_, "[ Case comparison");
954 // Compile the label expression.
955 VisitForAccumulatorValue(clause->label());
957 // Perform the comparison as if via '==='.
958 __ LoadP(r4, MemOperand(sp, 0)); // Switch value.
959 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
960 JumpPatchSite patch_site(masm_);
961 if (inline_smi_code) {
964 patch_site.EmitJumpIfNotSmi(r5, &slow_case);
968 __ Drop(1); // Switch value is no longer needed.
969 __ b(clause->body_target());
973 // Record position before stub call for type feedback.
974 SetExpressionPosition(clause);
975 Handle<Code> ic = CodeFactory::CompareIC(isolate(), Token::EQ_STRICT,
976 strength(language_mode())).code();
977 CallIC(ic, clause->CompareId());
978 patch_site.EmitPatchInfo();
982 PrepareForBailout(clause, TOS_REG);
983 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
987 __ b(clause->body_target());
990 __ cmpi(r3, Operand::Zero());
992 __ Drop(1); // Switch value is no longer needed.
993 __ b(clause->body_target());
996 // Discard the test value and jump to the default if present, otherwise to
997 // the end of the statement.
999 __ Drop(1); // Switch value is no longer needed.
1000 if (default_clause == NULL) {
1001 __ b(nested_statement.break_label());
1003 __ b(default_clause->body_target());
1006 // Compile all the case bodies.
1007 for (int i = 0; i < clauses->length(); i++) {
1008 Comment cmnt(masm_, "[ Case body");
1009 CaseClause* clause = clauses->at(i);
1010 __ bind(clause->body_target());
1011 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
1012 VisitStatements(clause->statements());
1015 __ bind(nested_statement.break_label());
1016 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1020 void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
1021 Comment cmnt(masm_, "[ ForInStatement");
1022 SetStatementPosition(stmt, SKIP_BREAK);
1024 FeedbackVectorSlot slot = stmt->ForInFeedbackSlot();
1027 ForIn loop_statement(this, stmt);
1028 increment_loop_depth();
1030 // Get the object to enumerate over. If the object is null or undefined, skip
1031 // over the loop. See ECMA-262 version 5, section 12.6.4.
1032 SetExpressionAsStatementPosition(stmt->enumerable());
1033 VisitForAccumulatorValue(stmt->enumerable());
1034 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1037 Register null_value = r7;
1038 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
1039 __ cmp(r3, null_value);
1042 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1044 // Convert the object to a JS object.
1045 Label convert, done_convert;
1046 __ JumpIfSmi(r3, &convert);
1047 __ CompareObjectType(r3, r4, r4, FIRST_SPEC_OBJECT_TYPE);
1048 __ bge(&done_convert);
1051 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1052 __ bind(&done_convert);
1053 PrepareForBailoutForId(stmt->ToObjectId(), TOS_REG);
1056 // Check for proxies.
1058 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1059 __ CompareObjectType(r3, r4, r4, LAST_JS_PROXY_TYPE);
1060 __ ble(&call_runtime);
1062 // Check cache validity in generated code. This is a fast case for
1063 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1064 // guarantee cache validity, call the runtime system to check cache
1065 // validity or get the property names in a fixed array.
1066 __ CheckEnumCache(null_value, &call_runtime);
1068 // The enum cache is valid. Load the map of the object being
1069 // iterated over and use the cache for the iteration.
1071 __ LoadP(r3, FieldMemOperand(r3, HeapObject::kMapOffset));
1074 // Get the set of properties to enumerate.
1075 __ bind(&call_runtime);
1076 __ push(r3); // Duplicate the enumerable object on the stack.
1077 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1078 PrepareForBailoutForId(stmt->EnumId(), TOS_REG);
1080 // If we got a map from the runtime call, we can do a fast
1081 // modification check. Otherwise, we got a fixed array, and we have
1082 // to do a slow check.
1084 __ LoadP(r5, FieldMemOperand(r3, HeapObject::kMapOffset));
1085 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
1087 __ bne(&fixed_array);
1089 // We got a map in register r3. Get the enumeration cache from it.
1090 Label no_descriptors;
1091 __ bind(&use_cache);
1093 __ EnumLength(r4, r3);
1094 __ CmpSmiLiteral(r4, Smi::FromInt(0), r0);
1095 __ beq(&no_descriptors);
1097 __ LoadInstanceDescriptors(r3, r5);
1098 __ LoadP(r5, FieldMemOperand(r5, DescriptorArray::kEnumCacheOffset));
1100 FieldMemOperand(r5, DescriptorArray::kEnumCacheBridgeCacheOffset));
1102 // Set up the four remaining stack slots.
1103 __ push(r3); // Map.
1104 __ LoadSmiLiteral(r3, Smi::FromInt(0));
1105 // Push enumeration cache, enumeration cache length (as smi) and zero.
1106 __ Push(r5, r4, r3);
1109 __ bind(&no_descriptors);
1113 // We got a fixed array in register r3. Iterate through that.
1115 __ bind(&fixed_array);
1117 __ Move(r4, FeedbackVector());
1118 __ mov(r5, Operand(TypeFeedbackVector::MegamorphicSentinel(isolate())));
1119 int vector_index = FeedbackVector()->GetIndex(slot);
1121 r5, FieldMemOperand(r4, FixedArray::OffsetOfElementAt(vector_index)), r0);
1123 __ LoadSmiLiteral(r4, Smi::FromInt(1)); // Smi indicates slow check
1124 __ LoadP(r5, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1125 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1126 __ CompareObjectType(r5, r6, r6, LAST_JS_PROXY_TYPE);
1128 __ LoadSmiLiteral(r4, Smi::FromInt(0)); // Zero indicates proxy
1129 __ bind(&non_proxy);
1130 __ Push(r4, r3); // Smi and array
1131 __ LoadP(r4, FieldMemOperand(r3, FixedArray::kLengthOffset));
1132 __ LoadSmiLiteral(r3, Smi::FromInt(0));
1133 __ Push(r4, r3); // Fixed array length (as smi) and initial index.
1135 // Generate code for doing the condition check.
1136 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
1138 SetExpressionAsStatementPosition(stmt->each());
1140 // Load the current count to r3, load the length to r4.
1141 __ LoadP(r3, MemOperand(sp, 0 * kPointerSize));
1142 __ LoadP(r4, MemOperand(sp, 1 * kPointerSize));
1143 __ cmpl(r3, r4); // Compare to the array length.
1144 __ bge(loop_statement.break_label());
1146 // Get the current entry of the array into register r6.
1147 __ LoadP(r5, MemOperand(sp, 2 * kPointerSize));
1148 __ addi(r5, r5, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1149 __ SmiToPtrArrayOffset(r6, r3);
1150 __ LoadPX(r6, MemOperand(r6, r5));
1152 // Get the expected map from the stack or a smi in the
1153 // permanent slow case into register r5.
1154 __ LoadP(r5, MemOperand(sp, 3 * kPointerSize));
1156 // Check if the expected map still matches that of the enumerable.
1157 // If not, we may have to filter the key.
1159 __ LoadP(r4, MemOperand(sp, 4 * kPointerSize));
1160 __ LoadP(r7, FieldMemOperand(r4, HeapObject::kMapOffset));
1162 __ beq(&update_each);
1164 // For proxies, no filtering is done.
1165 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1166 __ CmpSmiLiteral(r5, Smi::FromInt(0), r0);
1167 __ beq(&update_each);
1169 // Convert the entry to a string or (smi) 0 if it isn't a property
1170 // any more. If the property has been removed while iterating, we
1172 __ Push(r4, r6); // Enumerable and current entry.
1173 __ CallRuntime(Runtime::kForInFilter, 2);
1174 PrepareForBailoutForId(stmt->FilterId(), TOS_REG);
1176 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
1178 __ beq(loop_statement.continue_label());
1180 // Update the 'each' property or variable from the possibly filtered
1181 // entry in register r6.
1182 __ bind(&update_each);
1183 __ mr(result_register(), r6);
1184 // Perform the assignment as if via '='.
1186 EffectContext context(this);
1187 EmitAssignment(stmt->each(), stmt->EachFeedbackSlot());
1188 PrepareForBailoutForId(stmt->AssignmentId(), NO_REGISTERS);
1191 // Generate code for the body of the loop.
1192 Visit(stmt->body());
1194 // Generate code for the going to the next element by incrementing
1195 // the index (smi) stored on top of the stack.
1196 __ bind(loop_statement.continue_label());
1198 __ AddSmiLiteral(r3, r3, Smi::FromInt(1), r0);
1201 EmitBackEdgeBookkeeping(stmt, &loop);
1204 // Remove the pointers stored on the stack.
1205 __ bind(loop_statement.break_label());
1208 // Exit and decrement the loop depth.
1209 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1211 decrement_loop_depth();
1215 void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1217 // Use the fast case closure allocation code that allocates in new
1218 // space for nested functions that don't need literals cloning. If
1219 // we're running with the --always-opt or the --prepare-always-opt
1220 // flag, we need to use the runtime function so that the new function
1221 // we are creating here gets a chance to have its code optimized and
1222 // doesn't just get a copy of the existing unoptimized code.
1223 if (!FLAG_always_opt && !FLAG_prepare_always_opt && !pretenure &&
1224 scope()->is_function_scope() && info->num_literals() == 0) {
1225 FastNewClosureStub stub(isolate(), info->language_mode(), info->kind());
1226 __ mov(r5, Operand(info));
1229 __ mov(r3, Operand(info));
1231 r4, pretenure ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex);
1232 __ Push(cp, r3, r4);
1233 __ CallRuntime(Runtime::kNewClosure, 3);
1235 context()->Plug(r3);
1239 void FullCodeGenerator::EmitSetHomeObjectIfNeeded(Expression* initializer,
1241 FeedbackVectorICSlot slot) {
1242 if (NeedsHomeObject(initializer)) {
1243 __ LoadP(StoreDescriptor::ReceiverRegister(), MemOperand(sp));
1244 __ mov(StoreDescriptor::NameRegister(),
1245 Operand(isolate()->factory()->home_object_symbol()));
1246 __ LoadP(StoreDescriptor::ValueRegister(),
1247 MemOperand(sp, offset * kPointerSize));
1248 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
1254 void FullCodeGenerator::EmitLoadGlobalCheckExtensions(VariableProxy* proxy,
1255 TypeofMode typeof_mode,
1257 Register current = cp;
1263 if (s->num_heap_slots() > 0) {
1264 if (s->calls_sloppy_eval()) {
1265 // Check that extension is NULL.
1266 __ LoadP(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1267 __ cmpi(temp, Operand::Zero());
1270 // Load next context in chain.
1271 __ LoadP(next, ContextOperand(current, Context::PREVIOUS_INDEX));
1272 // Walk the rest of the chain without clobbering cp.
1275 // If no outer scope calls eval, we do not need to check more
1276 // context extensions.
1277 if (!s->outer_scope_calls_sloppy_eval() || s->is_eval_scope()) break;
1278 s = s->outer_scope();
1281 if (s->is_eval_scope()) {
1283 if (!current.is(next)) {
1284 __ Move(next, current);
1287 // Terminate at native context.
1288 __ LoadP(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1289 __ LoadRoot(ip, Heap::kNativeContextMapRootIndex);
1292 // Check that extension is NULL.
1293 __ LoadP(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1294 __ cmpi(temp, Operand::Zero());
1296 // Load next context in chain.
1297 __ LoadP(next, ContextOperand(next, Context::PREVIOUS_INDEX));
1302 // All extension objects were empty and it is safe to use a normal global
1304 EmitGlobalVariableLoad(proxy, typeof_mode);
1308 MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1310 DCHECK(var->IsContextSlot());
1311 Register context = cp;
1315 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
1316 if (s->num_heap_slots() > 0) {
1317 if (s->calls_sloppy_eval()) {
1318 // Check that extension is NULL.
1319 __ LoadP(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1320 __ cmpi(temp, Operand::Zero());
1323 __ LoadP(next, ContextOperand(context, Context::PREVIOUS_INDEX));
1324 // Walk the rest of the chain without clobbering cp.
1328 // Check that last extension is NULL.
1329 __ LoadP(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1330 __ cmpi(temp, Operand::Zero());
1333 // This function is used only for loads, not stores, so it's safe to
1334 // return an cp-based operand (the write barrier cannot be allowed to
1335 // destroy the cp register).
1336 return ContextOperand(context, var->index());
1340 void FullCodeGenerator::EmitDynamicLookupFastCase(VariableProxy* proxy,
1341 TypeofMode typeof_mode,
1342 Label* slow, Label* done) {
1343 // Generate fast-case code for variables that might be shadowed by
1344 // eval-introduced variables. Eval is used a lot without
1345 // introducing variables. In those cases, we do not want to
1346 // perform a runtime call for all variables in the scope
1347 // containing the eval.
1348 Variable* var = proxy->var();
1349 if (var->mode() == DYNAMIC_GLOBAL) {
1350 EmitLoadGlobalCheckExtensions(proxy, typeof_mode, slow);
1352 } else if (var->mode() == DYNAMIC_LOCAL) {
1353 Variable* local = var->local_if_not_shadowed();
1354 __ LoadP(r3, ContextSlotOperandCheckExtensions(local, slow));
1355 if (local->mode() == LET || local->mode() == CONST ||
1356 local->mode() == CONST_LEGACY) {
1357 __ CompareRoot(r3, Heap::kTheHoleValueRootIndex);
1359 if (local->mode() == CONST_LEGACY) {
1360 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
1361 } else { // LET || CONST
1362 __ mov(r3, Operand(var->name()));
1364 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1372 void FullCodeGenerator::EmitGlobalVariableLoad(VariableProxy* proxy,
1373 TypeofMode typeof_mode) {
1374 Variable* var = proxy->var();
1375 DCHECK(var->IsUnallocatedOrGlobalSlot() ||
1376 (var->IsLookupSlot() && var->mode() == DYNAMIC_GLOBAL));
1377 if (var->IsGlobalSlot()) {
1378 DCHECK(var->index() > 0);
1379 DCHECK(var->IsStaticGlobalObjectProperty());
1380 const int slot = var->index();
1381 const int depth = scope()->ContextChainLength(var->scope());
1382 if (depth <= LoadGlobalViaContextStub::kMaximumDepth) {
1383 __ mov(LoadGlobalViaContextDescriptor::SlotRegister(), Operand(slot));
1384 __ mov(LoadGlobalViaContextDescriptor::NameRegister(),
1385 Operand(var->name()));
1386 LoadGlobalViaContextStub stub(isolate(), depth);
1389 __ Push(Smi::FromInt(slot));
1390 __ Push(var->name());
1391 __ CallRuntime(Runtime::kLoadGlobalViaContext, 2);
1394 __ LoadP(LoadDescriptor::ReceiverRegister(), GlobalObjectOperand());
1395 __ mov(LoadDescriptor::NameRegister(), Operand(var->name()));
1396 __ mov(LoadDescriptor::SlotRegister(),
1397 Operand(SmiFromSlot(proxy->VariableFeedbackSlot())));
1398 CallLoadIC(typeof_mode);
1403 void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy,
1404 TypeofMode typeof_mode) {
1405 // Record position before possible IC call.
1406 SetExpressionPosition(proxy);
1407 PrepareForBailoutForId(proxy->BeforeId(), NO_REGISTERS);
1408 Variable* var = proxy->var();
1410 // Three cases: global variables, lookup variables, and all other types of
1412 switch (var->location()) {
1413 case VariableLocation::GLOBAL:
1414 case VariableLocation::UNALLOCATED: {
1415 Comment cmnt(masm_, "[ Global variable");
1416 EmitGlobalVariableLoad(proxy, typeof_mode);
1417 context()->Plug(r3);
1421 case VariableLocation::PARAMETER:
1422 case VariableLocation::LOCAL:
1423 case VariableLocation::CONTEXT: {
1424 DCHECK_EQ(NOT_INSIDE_TYPEOF, typeof_mode);
1425 Comment cmnt(masm_, var->IsContextSlot() ? "[ Context variable"
1426 : "[ Stack variable");
1427 if (var->binding_needs_init()) {
1428 // var->scope() may be NULL when the proxy is located in eval code and
1429 // refers to a potential outside binding. Currently those bindings are
1430 // always looked up dynamically, i.e. in that case
1431 // var->location() == LOOKUP.
1433 DCHECK(var->scope() != NULL);
1435 // Check if the binding really needs an initialization check. The check
1436 // can be skipped in the following situation: we have a LET or CONST
1437 // binding in harmony mode, both the Variable and the VariableProxy have
1438 // the same declaration scope (i.e. they are both in global code, in the
1439 // same function or in the same eval code) and the VariableProxy is in
1440 // the source physically located after the initializer of the variable.
1442 // We cannot skip any initialization checks for CONST in non-harmony
1443 // mode because const variables may be declared but never initialized:
1444 // if (false) { const x; }; var y = x;
1446 // The condition on the declaration scopes is a conservative check for
1447 // nested functions that access a binding and are called before the
1448 // binding is initialized:
1449 // function() { f(); let x = 1; function f() { x = 2; } }
1451 bool skip_init_check;
1452 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1453 skip_init_check = false;
1454 } else if (var->is_this()) {
1455 CHECK(info_->function() != nullptr &&
1456 (info_->function()->kind() & kSubclassConstructor) != 0);
1457 // TODO(dslomov): implement 'this' hole check elimination.
1458 skip_init_check = false;
1460 // Check that we always have valid source position.
1461 DCHECK(var->initializer_position() != RelocInfo::kNoPosition);
1462 DCHECK(proxy->position() != RelocInfo::kNoPosition);
1463 skip_init_check = var->mode() != CONST_LEGACY &&
1464 var->initializer_position() < proxy->position();
1467 if (!skip_init_check) {
1469 // Let and const need a read barrier.
1471 __ CompareRoot(r3, Heap::kTheHoleValueRootIndex);
1473 if (var->mode() == LET || var->mode() == CONST) {
1474 // Throw a reference error when using an uninitialized let/const
1475 // binding in harmony mode.
1476 __ mov(r3, Operand(var->name()));
1478 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1480 // Uninitalized const bindings outside of harmony mode are unholed.
1481 DCHECK(var->mode() == CONST_LEGACY);
1482 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
1485 context()->Plug(r3);
1489 context()->Plug(var);
1493 case VariableLocation::LOOKUP: {
1494 Comment cmnt(masm_, "[ Lookup variable");
1496 // Generate code for loading from variables potentially shadowed
1497 // by eval-introduced variables.
1498 EmitDynamicLookupFastCase(proxy, typeof_mode, &slow, &done);
1500 __ mov(r4, Operand(var->name()));
1501 __ Push(cp, r4); // Context and name.
1502 Runtime::FunctionId function_id =
1503 typeof_mode == NOT_INSIDE_TYPEOF
1504 ? Runtime::kLoadLookupSlot
1505 : Runtime::kLoadLookupSlotNoReferenceError;
1506 __ CallRuntime(function_id, 2);
1508 context()->Plug(r3);
1514 void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1515 Comment cmnt(masm_, "[ RegExpLiteral");
1517 // Registers will be used as follows:
1518 // r8 = materialized value (RegExp literal)
1519 // r7 = JS function, literals array
1520 // r6 = literal index
1521 // r5 = RegExp pattern
1522 // r4 = RegExp flags
1523 // r3 = RegExp literal clone
1524 __ LoadP(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1525 __ LoadP(r7, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
1526 int literal_offset =
1527 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1528 __ LoadP(r8, FieldMemOperand(r7, literal_offset), r0);
1529 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1531 __ bne(&materialized);
1533 // Create regexp literal using runtime function.
1534 // Result will be in r3.
1535 __ LoadSmiLiteral(r6, Smi::FromInt(expr->literal_index()));
1536 __ mov(r5, Operand(expr->pattern()));
1537 __ mov(r4, Operand(expr->flags()));
1538 __ Push(r7, r6, r5, r4);
1539 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1542 __ bind(&materialized);
1543 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1544 Label allocated, runtime_allocate;
1545 __ Allocate(size, r3, r5, r6, &runtime_allocate, TAG_OBJECT);
1548 __ bind(&runtime_allocate);
1549 __ LoadSmiLiteral(r3, Smi::FromInt(size));
1551 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1554 __ bind(&allocated);
1555 // After this, registers are used as follows:
1556 // r3: Newly allocated regexp.
1557 // r8: Materialized regexp.
1559 __ CopyFields(r3, r8, r5.bit(), size / kPointerSize);
1560 context()->Plug(r3);
1564 void FullCodeGenerator::EmitAccessor(Expression* expression) {
1565 if (expression == NULL) {
1566 __ LoadRoot(r4, Heap::kNullValueRootIndex);
1569 VisitForStackValue(expression);
1574 void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1575 Comment cmnt(masm_, "[ ObjectLiteral");
1577 Handle<FixedArray> constant_properties = expr->constant_properties();
1578 __ LoadP(r6, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1579 __ LoadP(r6, FieldMemOperand(r6, JSFunction::kLiteralsOffset));
1580 __ LoadSmiLiteral(r5, Smi::FromInt(expr->literal_index()));
1581 __ mov(r4, Operand(constant_properties));
1582 int flags = expr->ComputeFlags();
1583 __ LoadSmiLiteral(r3, Smi::FromInt(flags));
1584 if (MustCreateObjectLiteralWithRuntime(expr)) {
1585 __ Push(r6, r5, r4, r3);
1586 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1588 FastCloneShallowObjectStub stub(isolate(), expr->properties_count());
1591 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1593 // If result_saved is true the result is on top of the stack. If
1594 // result_saved is false the result is in r3.
1595 bool result_saved = false;
1597 AccessorTable accessor_table(zone());
1598 int property_index = 0;
1599 // store_slot_index points to the vector IC slot for the next store IC used.
1600 // ObjectLiteral::ComputeFeedbackRequirements controls the allocation of slots
1601 // and must be updated if the number of store ICs emitted here changes.
1602 int store_slot_index = 0;
1603 for (; property_index < expr->properties()->length(); property_index++) {
1604 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1605 if (property->is_computed_name()) break;
1606 if (property->IsCompileTimeValue()) continue;
1608 Literal* key = property->key()->AsLiteral();
1609 Expression* value = property->value();
1610 if (!result_saved) {
1611 __ push(r3); // Save result on stack
1612 result_saved = true;
1614 switch (property->kind()) {
1615 case ObjectLiteral::Property::CONSTANT:
1617 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1618 DCHECK(!CompileTimeValue::IsCompileTimeValue(property->value()));
1620 case ObjectLiteral::Property::COMPUTED:
1621 // It is safe to use [[Put]] here because the boilerplate already
1622 // contains computed properties with an uninitialized value.
1623 if (key->value()->IsInternalizedString()) {
1624 if (property->emit_store()) {
1625 VisitForAccumulatorValue(value);
1626 DCHECK(StoreDescriptor::ValueRegister().is(r3));
1627 __ mov(StoreDescriptor::NameRegister(), Operand(key->value()));
1628 __ LoadP(StoreDescriptor::ReceiverRegister(), MemOperand(sp));
1629 if (FLAG_vector_stores) {
1630 EmitLoadStoreICSlot(expr->GetNthSlot(store_slot_index++));
1633 CallStoreIC(key->LiteralFeedbackId());
1635 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1637 if (NeedsHomeObject(value)) {
1638 __ Move(StoreDescriptor::ReceiverRegister(), r3);
1639 __ mov(StoreDescriptor::NameRegister(),
1640 Operand(isolate()->factory()->home_object_symbol()));
1641 __ LoadP(StoreDescriptor::ValueRegister(), MemOperand(sp));
1642 if (FLAG_vector_stores) {
1643 EmitLoadStoreICSlot(expr->GetNthSlot(store_slot_index++));
1648 VisitForEffect(value);
1652 // Duplicate receiver on stack.
1653 __ LoadP(r3, MemOperand(sp));
1655 VisitForStackValue(key);
1656 VisitForStackValue(value);
1657 if (property->emit_store()) {
1658 EmitSetHomeObjectIfNeeded(
1659 value, 2, expr->SlotForHomeObject(value, &store_slot_index));
1660 __ LoadSmiLiteral(r3, Smi::FromInt(SLOPPY)); // PropertyAttributes
1662 __ CallRuntime(Runtime::kSetProperty, 4);
1667 case ObjectLiteral::Property::PROTOTYPE:
1668 // Duplicate receiver on stack.
1669 __ LoadP(r3, MemOperand(sp));
1671 VisitForStackValue(value);
1672 DCHECK(property->emit_store());
1673 __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1675 case ObjectLiteral::Property::GETTER:
1676 if (property->emit_store()) {
1677 accessor_table.lookup(key)->second->getter = value;
1680 case ObjectLiteral::Property::SETTER:
1681 if (property->emit_store()) {
1682 accessor_table.lookup(key)->second->setter = value;
1688 // Emit code to define accessors, using only a single call to the runtime for
1689 // each pair of corresponding getters and setters.
1690 for (AccessorTable::Iterator it = accessor_table.begin();
1691 it != accessor_table.end(); ++it) {
1692 __ LoadP(r3, MemOperand(sp)); // Duplicate receiver.
1694 VisitForStackValue(it->first);
1695 EmitAccessor(it->second->getter);
1696 EmitSetHomeObjectIfNeeded(
1697 it->second->getter, 2,
1698 expr->SlotForHomeObject(it->second->getter, &store_slot_index));
1699 EmitAccessor(it->second->setter);
1700 EmitSetHomeObjectIfNeeded(
1701 it->second->setter, 3,
1702 expr->SlotForHomeObject(it->second->setter, &store_slot_index));
1703 __ LoadSmiLiteral(r3, Smi::FromInt(NONE));
1705 __ CallRuntime(Runtime::kDefineAccessorPropertyUnchecked, 5);
1708 // Object literals have two parts. The "static" part on the left contains no
1709 // computed property names, and so we can compute its map ahead of time; see
1710 // runtime.cc::CreateObjectLiteralBoilerplate. The second "dynamic" part
1711 // starts with the first computed property name, and continues with all
1712 // properties to its right. All the code from above initializes the static
1713 // component of the object literal, and arranges for the map of the result to
1714 // reflect the static order in which the keys appear. For the dynamic
1715 // properties, we compile them into a series of "SetOwnProperty" runtime
1716 // calls. This will preserve insertion order.
1717 for (; property_index < expr->properties()->length(); property_index++) {
1718 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1720 Expression* value = property->value();
1721 if (!result_saved) {
1722 __ push(r3); // Save result on the stack
1723 result_saved = true;
1726 __ LoadP(r3, MemOperand(sp)); // Duplicate receiver.
1729 if (property->kind() == ObjectLiteral::Property::PROTOTYPE) {
1730 DCHECK(!property->is_computed_name());
1731 VisitForStackValue(value);
1732 DCHECK(property->emit_store());
1733 __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1735 EmitPropertyKey(property, expr->GetIdForProperty(property_index));
1736 VisitForStackValue(value);
1737 EmitSetHomeObjectIfNeeded(
1738 value, 2, expr->SlotForHomeObject(value, &store_slot_index));
1740 switch (property->kind()) {
1741 case ObjectLiteral::Property::CONSTANT:
1742 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1743 case ObjectLiteral::Property::COMPUTED:
1744 if (property->emit_store()) {
1745 __ LoadSmiLiteral(r3, Smi::FromInt(NONE));
1747 __ CallRuntime(Runtime::kDefineDataPropertyUnchecked, 4);
1753 case ObjectLiteral::Property::PROTOTYPE:
1757 case ObjectLiteral::Property::GETTER:
1758 __ mov(r3, Operand(Smi::FromInt(NONE)));
1760 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
1763 case ObjectLiteral::Property::SETTER:
1764 __ mov(r3, Operand(Smi::FromInt(NONE)));
1766 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
1772 if (expr->has_function()) {
1773 DCHECK(result_saved);
1774 __ LoadP(r3, MemOperand(sp));
1776 __ CallRuntime(Runtime::kToFastProperties, 1);
1780 context()->PlugTOS();
1782 context()->Plug(r3);
1785 // Verify that compilation exactly consumed the number of store ic slots that
1786 // the ObjectLiteral node had to offer.
1787 DCHECK(!FLAG_vector_stores || store_slot_index == expr->slot_count());
1791 void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1792 Comment cmnt(masm_, "[ ArrayLiteral");
1794 expr->BuildConstantElements(isolate());
1795 Handle<FixedArray> constant_elements = expr->constant_elements();
1796 bool has_fast_elements =
1797 IsFastObjectElementsKind(expr->constant_elements_kind());
1798 Handle<FixedArrayBase> constant_elements_values(
1799 FixedArrayBase::cast(constant_elements->get(1)));
1801 AllocationSiteMode allocation_site_mode = TRACK_ALLOCATION_SITE;
1802 if (has_fast_elements && !FLAG_allocation_site_pretenuring) {
1803 // If the only customer of allocation sites is transitioning, then
1804 // we can turn it off if we don't have anywhere else to transition to.
1805 allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
1808 __ LoadP(r6, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1809 __ LoadP(r6, FieldMemOperand(r6, JSFunction::kLiteralsOffset));
1810 __ LoadSmiLiteral(r5, Smi::FromInt(expr->literal_index()));
1811 __ mov(r4, Operand(constant_elements));
1812 if (MustCreateArrayLiteralWithRuntime(expr)) {
1813 __ LoadSmiLiteral(r3, Smi::FromInt(expr->ComputeFlags()));
1814 __ Push(r6, r5, r4, r3);
1815 __ CallRuntime(Runtime::kCreateArrayLiteral, 4);
1817 FastCloneShallowArrayStub stub(isolate(), allocation_site_mode);
1820 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1822 bool result_saved = false; // Is the result saved to the stack?
1823 ZoneList<Expression*>* subexprs = expr->values();
1824 int length = subexprs->length();
1826 // Emit code to evaluate all the non-constant subexpressions and to store
1827 // them into the newly cloned array.
1828 int array_index = 0;
1829 for (; array_index < length; array_index++) {
1830 Expression* subexpr = subexprs->at(array_index);
1831 if (subexpr->IsSpread()) break;
1832 // If the subexpression is a literal or a simple materialized literal it
1833 // is already set in the cloned array.
1834 if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
1836 if (!result_saved) {
1838 __ Push(Smi::FromInt(expr->literal_index()));
1839 result_saved = true;
1841 VisitForAccumulatorValue(subexpr);
1843 if (has_fast_elements) {
1844 int offset = FixedArray::kHeaderSize + (array_index * kPointerSize);
1845 __ LoadP(r8, MemOperand(sp, kPointerSize)); // Copy of array literal.
1846 __ LoadP(r4, FieldMemOperand(r8, JSObject::kElementsOffset));
1847 __ StoreP(result_register(), FieldMemOperand(r4, offset), r0);
1848 // Update the write barrier for the array store.
1849 __ RecordWriteField(r4, offset, result_register(), r5, kLRHasBeenSaved,
1850 kDontSaveFPRegs, EMIT_REMEMBERED_SET,
1853 __ LoadSmiLiteral(r6, Smi::FromInt(array_index));
1854 StoreArrayLiteralElementStub stub(isolate());
1858 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1861 // In case the array literal contains spread expressions it has two parts. The
1862 // first part is the "static" array which has a literal index is handled
1863 // above. The second part is the part after the first spread expression
1864 // (inclusive) and these elements gets appended to the array. Note that the
1865 // number elements an iterable produces is unknown ahead of time.
1866 if (array_index < length && result_saved) {
1867 __ Drop(1); // literal index
1869 result_saved = false;
1871 for (; array_index < length; array_index++) {
1872 Expression* subexpr = subexprs->at(array_index);
1875 if (subexpr->IsSpread()) {
1876 VisitForStackValue(subexpr->AsSpread()->expression());
1877 __ InvokeBuiltin(Builtins::CONCAT_ITERABLE_TO_ARRAY, CALL_FUNCTION);
1879 VisitForStackValue(subexpr);
1880 __ CallRuntime(Runtime::kAppendElement, 2);
1883 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1887 __ Drop(1); // literal index
1888 context()->PlugTOS();
1890 context()->Plug(r3);
1895 void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1896 DCHECK(expr->target()->IsValidReferenceExpressionOrThis());
1898 Comment cmnt(masm_, "[ Assignment");
1899 SetExpressionPosition(expr, INSERT_BREAK);
1901 Property* property = expr->target()->AsProperty();
1902 LhsKind assign_type = Property::GetAssignType(property);
1904 // Evaluate LHS expression.
1905 switch (assign_type) {
1907 // Nothing to do here.
1909 case NAMED_PROPERTY:
1910 if (expr->is_compound()) {
1911 // We need the receiver both on the stack and in the register.
1912 VisitForStackValue(property->obj());
1913 __ LoadP(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
1915 VisitForStackValue(property->obj());
1918 case NAMED_SUPER_PROPERTY:
1920 property->obj()->AsSuperPropertyReference()->this_var());
1921 VisitForAccumulatorValue(
1922 property->obj()->AsSuperPropertyReference()->home_object());
1923 __ Push(result_register());
1924 if (expr->is_compound()) {
1925 const Register scratch = r4;
1926 __ LoadP(scratch, MemOperand(sp, kPointerSize));
1927 __ Push(scratch, result_register());
1930 case KEYED_SUPER_PROPERTY: {
1931 const Register scratch = r4;
1933 property->obj()->AsSuperPropertyReference()->this_var());
1934 VisitForAccumulatorValue(
1935 property->obj()->AsSuperPropertyReference()->home_object());
1936 __ mr(scratch, result_register());
1937 VisitForAccumulatorValue(property->key());
1938 __ Push(scratch, result_register());
1939 if (expr->is_compound()) {
1940 const Register scratch1 = r5;
1941 __ LoadP(scratch1, MemOperand(sp, 2 * kPointerSize));
1942 __ Push(scratch1, scratch, result_register());
1946 case KEYED_PROPERTY:
1947 if (expr->is_compound()) {
1948 VisitForStackValue(property->obj());
1949 VisitForStackValue(property->key());
1950 __ LoadP(LoadDescriptor::ReceiverRegister(),
1951 MemOperand(sp, 1 * kPointerSize));
1952 __ LoadP(LoadDescriptor::NameRegister(), MemOperand(sp, 0));
1954 VisitForStackValue(property->obj());
1955 VisitForStackValue(property->key());
1960 // For compound assignments we need another deoptimization point after the
1961 // variable/property load.
1962 if (expr->is_compound()) {
1964 AccumulatorValueContext context(this);
1965 switch (assign_type) {
1967 EmitVariableLoad(expr->target()->AsVariableProxy());
1968 PrepareForBailout(expr->target(), TOS_REG);
1970 case NAMED_PROPERTY:
1971 EmitNamedPropertyLoad(property);
1972 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1974 case NAMED_SUPER_PROPERTY:
1975 EmitNamedSuperPropertyLoad(property);
1976 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1978 case KEYED_SUPER_PROPERTY:
1979 EmitKeyedSuperPropertyLoad(property);
1980 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1982 case KEYED_PROPERTY:
1983 EmitKeyedPropertyLoad(property);
1984 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1989 Token::Value op = expr->binary_op();
1990 __ push(r3); // Left operand goes on the stack.
1991 VisitForAccumulatorValue(expr->value());
1993 AccumulatorValueContext context(this);
1994 if (ShouldInlineSmiCase(op)) {
1995 EmitInlineSmiBinaryOp(expr->binary_operation(), op, expr->target(),
1998 EmitBinaryOp(expr->binary_operation(), op);
2001 // Deoptimization point in case the binary operation may have side effects.
2002 PrepareForBailout(expr->binary_operation(), TOS_REG);
2004 VisitForAccumulatorValue(expr->value());
2007 SetExpressionPosition(expr);
2010 switch (assign_type) {
2012 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
2013 expr->op(), expr->AssignmentSlot());
2014 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2015 context()->Plug(r3);
2017 case NAMED_PROPERTY:
2018 EmitNamedPropertyAssignment(expr);
2020 case NAMED_SUPER_PROPERTY:
2021 EmitNamedSuperPropertyStore(property);
2022 context()->Plug(r3);
2024 case KEYED_SUPER_PROPERTY:
2025 EmitKeyedSuperPropertyStore(property);
2026 context()->Plug(r3);
2028 case KEYED_PROPERTY:
2029 EmitKeyedPropertyAssignment(expr);
2035 void FullCodeGenerator::VisitYield(Yield* expr) {
2036 Comment cmnt(masm_, "[ Yield");
2037 SetExpressionPosition(expr);
2039 // Evaluate yielded value first; the initial iterator definition depends on
2040 // this. It stays on the stack while we update the iterator.
2041 VisitForStackValue(expr->expression());
2043 switch (expr->yield_kind()) {
2044 case Yield::kSuspend:
2045 // Pop value from top-of-stack slot; box result into result register.
2046 EmitCreateIteratorResult(false);
2047 __ push(result_register());
2049 case Yield::kInitial: {
2050 Label suspend, continuation, post_runtime, resume;
2053 __ bind(&continuation);
2054 __ RecordGeneratorContinuation();
2058 VisitForAccumulatorValue(expr->generator_object());
2059 DCHECK(continuation.pos() > 0 && Smi::IsValid(continuation.pos()));
2060 __ LoadSmiLiteral(r4, Smi::FromInt(continuation.pos()));
2061 __ StoreP(r4, FieldMemOperand(r3, JSGeneratorObject::kContinuationOffset),
2063 __ StoreP(cp, FieldMemOperand(r3, JSGeneratorObject::kContextOffset), r0);
2065 __ RecordWriteField(r3, JSGeneratorObject::kContextOffset, r4, r5,
2066 kLRHasBeenSaved, kDontSaveFPRegs);
2067 __ addi(r4, fp, Operand(StandardFrameConstants::kExpressionsOffset));
2069 __ beq(&post_runtime);
2070 __ push(r3); // generator object
2071 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
2072 __ LoadP(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2073 __ bind(&post_runtime);
2074 __ pop(result_register());
2075 EmitReturnSequence();
2078 context()->Plug(result_register());
2082 case Yield::kFinal: {
2083 VisitForAccumulatorValue(expr->generator_object());
2084 __ LoadSmiLiteral(r4, Smi::FromInt(JSGeneratorObject::kGeneratorClosed));
2085 __ StoreP(r4, FieldMemOperand(result_register(),
2086 JSGeneratorObject::kContinuationOffset),
2088 // Pop value from top-of-stack slot, box result into result register.
2089 EmitCreateIteratorResult(true);
2090 EmitUnwindBeforeReturn();
2091 EmitReturnSequence();
2095 case Yield::kDelegating: {
2096 VisitForStackValue(expr->generator_object());
2098 // Initial stack layout is as follows:
2099 // [sp + 1 * kPointerSize] iter
2100 // [sp + 0 * kPointerSize] g
2102 Label l_catch, l_try, l_suspend, l_continuation, l_resume;
2103 Label l_next, l_call;
2104 Register load_receiver = LoadDescriptor::ReceiverRegister();
2105 Register load_name = LoadDescriptor::NameRegister();
2107 // Initial send value is undefined.
2108 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
2111 // catch (e) { receiver = iter; f = 'throw'; arg = e; goto l_call; }
2113 __ LoadRoot(load_name, Heap::kthrow_stringRootIndex); // "throw"
2114 __ LoadP(r6, MemOperand(sp, 1 * kPointerSize)); // iter
2115 __ Push(load_name, r6, r3); // "throw", iter, except
2118 // try { received = %yield result }
2119 // Shuffle the received result above a try handler and yield it without
2122 __ pop(r3); // result
2123 int handler_index = NewHandlerTableEntry();
2124 EnterTryBlock(handler_index, &l_catch);
2125 const int try_block_size = TryCatch::kElementCount * kPointerSize;
2126 __ push(r3); // result
2129 __ bind(&l_continuation);
2130 __ RecordGeneratorContinuation();
2133 __ bind(&l_suspend);
2134 const int generator_object_depth = kPointerSize + try_block_size;
2135 __ LoadP(r3, MemOperand(sp, generator_object_depth));
2137 __ Push(Smi::FromInt(handler_index)); // handler-index
2138 DCHECK(l_continuation.pos() > 0 && Smi::IsValid(l_continuation.pos()));
2139 __ LoadSmiLiteral(r4, Smi::FromInt(l_continuation.pos()));
2140 __ StoreP(r4, FieldMemOperand(r3, JSGeneratorObject::kContinuationOffset),
2142 __ StoreP(cp, FieldMemOperand(r3, JSGeneratorObject::kContextOffset), r0);
2144 __ RecordWriteField(r3, JSGeneratorObject::kContextOffset, r4, r5,
2145 kLRHasBeenSaved, kDontSaveFPRegs);
2146 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 2);
2147 __ LoadP(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2148 __ pop(r3); // result
2149 EmitReturnSequence();
2150 __ bind(&l_resume); // received in r3
2151 ExitTryBlock(handler_index);
2153 // receiver = iter; f = 'next'; arg = received;
2156 __ LoadRoot(load_name, Heap::knext_stringRootIndex); // "next"
2157 __ LoadP(r6, MemOperand(sp, 1 * kPointerSize)); // iter
2158 __ Push(load_name, r6, r3); // "next", iter, received
2160 // result = receiver[f](arg);
2162 __ LoadP(load_receiver, MemOperand(sp, kPointerSize));
2163 __ LoadP(load_name, MemOperand(sp, 2 * kPointerSize));
2164 __ mov(LoadDescriptor::SlotRegister(),
2165 Operand(SmiFromSlot(expr->KeyedLoadFeedbackSlot())));
2166 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), SLOPPY).code();
2167 CallIC(ic, TypeFeedbackId::None());
2169 __ StoreP(r4, MemOperand(sp, 2 * kPointerSize));
2170 SetCallPosition(expr, 1);
2171 CallFunctionStub stub(isolate(), 1, CALL_AS_METHOD);
2174 __ LoadP(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2175 __ Drop(1); // The function is still on the stack; drop it.
2177 // if (!result.done) goto l_try;
2178 __ Move(load_receiver, r3);
2180 __ push(load_receiver); // save result
2181 __ LoadRoot(load_name, Heap::kdone_stringRootIndex); // "done"
2182 __ mov(LoadDescriptor::SlotRegister(),
2183 Operand(SmiFromSlot(expr->DoneFeedbackSlot())));
2184 CallLoadIC(NOT_INSIDE_TYPEOF); // r0=result.done
2185 Handle<Code> bool_ic = ToBooleanStub::GetUninitialized(isolate());
2187 __ cmpi(r3, Operand::Zero());
2191 __ pop(load_receiver); // result
2192 __ LoadRoot(load_name, Heap::kvalue_stringRootIndex); // "value"
2193 __ mov(LoadDescriptor::SlotRegister(),
2194 Operand(SmiFromSlot(expr->ValueFeedbackSlot())));
2195 CallLoadIC(NOT_INSIDE_TYPEOF); // r3=result.value
2196 context()->DropAndPlug(2, r3); // drop iter and g
2203 void FullCodeGenerator::EmitGeneratorResume(
2204 Expression* generator, Expression* value,
2205 JSGeneratorObject::ResumeMode resume_mode) {
2206 // The value stays in r3, and is ultimately read by the resumed generator, as
2207 // if CallRuntime(Runtime::kSuspendJSGeneratorObject) returned it. Or it
2208 // is read to throw the value when the resumed generator is already closed.
2209 // r4 will hold the generator object until the activation has been resumed.
2210 VisitForStackValue(generator);
2211 VisitForAccumulatorValue(value);
2214 // Load suspended function and context.
2215 __ LoadP(cp, FieldMemOperand(r4, JSGeneratorObject::kContextOffset));
2216 __ LoadP(r7, FieldMemOperand(r4, JSGeneratorObject::kFunctionOffset));
2218 // Load receiver and store as the first argument.
2219 __ LoadP(r5, FieldMemOperand(r4, JSGeneratorObject::kReceiverOffset));
2222 // Push holes for the rest of the arguments to the generator function.
2223 __ LoadP(r6, FieldMemOperand(r7, JSFunction::kSharedFunctionInfoOffset));
2225 r6, FieldMemOperand(r6, SharedFunctionInfo::kFormalParameterCountOffset));
2226 __ LoadRoot(r5, Heap::kTheHoleValueRootIndex);
2227 Label argument_loop, push_frame;
2228 #if V8_TARGET_ARCH_PPC64
2229 __ cmpi(r6, Operand::Zero());
2230 __ beq(&push_frame);
2232 __ SmiUntag(r6, SetRC);
2233 __ beq(&push_frame, cr0);
2236 __ bind(&argument_loop);
2238 __ bdnz(&argument_loop);
2240 // Enter a new JavaScript frame, and initialize its slots as they were when
2241 // the generator was suspended.
2242 Label resume_frame, done;
2243 __ bind(&push_frame);
2244 __ b(&resume_frame, SetLK);
2246 __ bind(&resume_frame);
2247 // lr = return address.
2248 // fp = caller's frame pointer.
2249 // cp = callee's context,
2250 // r7 = callee's JS function.
2251 __ PushFixedFrame(r7);
2252 // Adjust FP to point to saved FP.
2253 __ addi(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
2255 // Load the operand stack size.
2256 __ LoadP(r6, FieldMemOperand(r4, JSGeneratorObject::kOperandStackOffset));
2257 __ LoadP(r6, FieldMemOperand(r6, FixedArray::kLengthOffset));
2258 __ SmiUntag(r6, SetRC);
2260 // If we are sending a value and there is no operand stack, we can jump back
2263 if (resume_mode == JSGeneratorObject::NEXT) {
2265 __ bne(&slow_resume, cr0);
2266 __ LoadP(ip, FieldMemOperand(r7, JSFunction::kCodeEntryOffset));
2268 ConstantPoolUnavailableScope constant_pool_unavailable(masm_);
2269 if (FLAG_enable_embedded_constant_pool) {
2270 __ LoadConstantPoolPointerRegisterFromCodeTargetAddress(ip);
2272 __ LoadP(r5, FieldMemOperand(r4, JSGeneratorObject::kContinuationOffset));
2275 __ LoadSmiLiteral(r5,
2276 Smi::FromInt(JSGeneratorObject::kGeneratorExecuting));
2277 __ StoreP(r5, FieldMemOperand(r4, JSGeneratorObject::kContinuationOffset),
2280 __ bind(&slow_resume);
2283 __ beq(&call_resume, cr0);
2286 // Otherwise, we push holes for the operand stack and call the runtime to fix
2287 // up the stack and the handlers.
2290 __ bind(&operand_loop);
2292 __ bdnz(&operand_loop);
2294 __ bind(&call_resume);
2295 DCHECK(!result_register().is(r4));
2296 __ Push(r4, result_register());
2297 __ Push(Smi::FromInt(resume_mode));
2298 __ CallRuntime(Runtime::kResumeJSGeneratorObject, 3);
2299 // Not reached: the runtime call returns elsewhere.
2300 __ stop("not-reached");
2303 context()->Plug(result_register());
2307 void FullCodeGenerator::EmitCreateIteratorResult(bool done) {
2311 const int instance_size = 5 * kPointerSize;
2312 DCHECK_EQ(isolate()->native_context()->iterator_result_map()->instance_size(),
2315 __ Allocate(instance_size, r3, r5, r6, &gc_required, TAG_OBJECT);
2318 __ bind(&gc_required);
2319 __ Push(Smi::FromInt(instance_size));
2320 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
2321 __ LoadP(context_register(),
2322 MemOperand(fp, StandardFrameConstants::kContextOffset));
2324 __ bind(&allocated);
2325 __ LoadP(r4, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
2326 __ LoadP(r4, FieldMemOperand(r4, GlobalObject::kNativeContextOffset));
2327 __ LoadP(r4, ContextOperand(r4, Context::ITERATOR_RESULT_MAP_INDEX));
2329 __ mov(r6, Operand(isolate()->factory()->ToBoolean(done)));
2330 __ mov(r7, Operand(isolate()->factory()->empty_fixed_array()));
2331 __ StoreP(r4, FieldMemOperand(r3, HeapObject::kMapOffset), r0);
2332 __ StoreP(r7, FieldMemOperand(r3, JSObject::kPropertiesOffset), r0);
2333 __ StoreP(r7, FieldMemOperand(r3, JSObject::kElementsOffset), r0);
2335 FieldMemOperand(r3, JSGeneratorObject::kResultValuePropertyOffset),
2338 FieldMemOperand(r3, JSGeneratorObject::kResultDonePropertyOffset),
2341 // Only the value field needs a write barrier, as the other values are in the
2343 __ RecordWriteField(r3, JSGeneratorObject::kResultValuePropertyOffset, r5, r6,
2344 kLRHasBeenSaved, kDontSaveFPRegs);
2348 void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
2349 SetExpressionPosition(prop);
2350 Literal* key = prop->key()->AsLiteral();
2351 DCHECK(!prop->IsSuperAccess());
2353 __ mov(LoadDescriptor::NameRegister(), Operand(key->value()));
2354 __ mov(LoadDescriptor::SlotRegister(),
2355 Operand(SmiFromSlot(prop->PropertyFeedbackSlot())));
2356 CallLoadIC(NOT_INSIDE_TYPEOF, language_mode());
2360 void FullCodeGenerator::EmitNamedSuperPropertyLoad(Property* prop) {
2361 // Stack: receiver, home_object.
2362 SetExpressionPosition(prop);
2363 Literal* key = prop->key()->AsLiteral();
2364 DCHECK(!key->value()->IsSmi());
2365 DCHECK(prop->IsSuperAccess());
2367 __ Push(key->value());
2368 __ Push(Smi::FromInt(language_mode()));
2369 __ CallRuntime(Runtime::kLoadFromSuper, 4);
2373 void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
2374 SetExpressionPosition(prop);
2375 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), language_mode()).code();
2376 __ mov(LoadDescriptor::SlotRegister(),
2377 Operand(SmiFromSlot(prop->PropertyFeedbackSlot())));
2382 void FullCodeGenerator::EmitKeyedSuperPropertyLoad(Property* prop) {
2383 // Stack: receiver, home_object, key.
2384 SetExpressionPosition(prop);
2385 __ Push(Smi::FromInt(language_mode()));
2386 __ CallRuntime(Runtime::kLoadKeyedFromSuper, 4);
2390 void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
2392 Expression* left_expr,
2393 Expression* right_expr) {
2394 Label done, smi_case, stub_call;
2396 Register scratch1 = r5;
2397 Register scratch2 = r6;
2399 // Get the arguments.
2401 Register right = r3;
2404 // Perform combined smi check on both operands.
2405 __ orx(scratch1, left, right);
2406 STATIC_ASSERT(kSmiTag == 0);
2407 JumpPatchSite patch_site(masm_);
2408 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
2410 __ bind(&stub_call);
2412 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2413 CallIC(code, expr->BinaryOperationFeedbackId());
2414 patch_site.EmitPatchInfo();
2418 // Smi case. This code works the same way as the smi-smi case in the type
2419 // recording binary operation stub.
2422 __ GetLeastBitsFromSmi(scratch1, right, 5);
2423 __ ShiftRightArith(right, left, scratch1);
2424 __ ClearRightImm(right, right, Operand(kSmiTagSize + kSmiShiftSize));
2427 __ GetLeastBitsFromSmi(scratch2, right, 5);
2428 #if V8_TARGET_ARCH_PPC64
2429 __ ShiftLeft_(right, left, scratch2);
2431 __ SmiUntag(scratch1, left);
2432 __ ShiftLeft_(scratch1, scratch1, scratch2);
2433 // Check that the *signed* result fits in a smi
2434 __ JumpIfNotSmiCandidate(scratch1, scratch2, &stub_call);
2435 __ SmiTag(right, scratch1);
2440 __ SmiUntag(scratch1, left);
2441 __ GetLeastBitsFromSmi(scratch2, right, 5);
2442 __ srw(scratch1, scratch1, scratch2);
2443 // Unsigned shift is not allowed to produce a negative number.
2444 __ JumpIfNotUnsignedSmiCandidate(scratch1, r0, &stub_call);
2445 __ SmiTag(right, scratch1);
2449 __ AddAndCheckForOverflow(scratch1, left, right, scratch2, r0);
2450 __ BranchOnOverflow(&stub_call);
2451 __ mr(right, scratch1);
2455 __ SubAndCheckForOverflow(scratch1, left, right, scratch2, r0);
2456 __ BranchOnOverflow(&stub_call);
2457 __ mr(right, scratch1);
2462 #if V8_TARGET_ARCH_PPC64
2463 // Remove tag from both operands.
2464 __ SmiUntag(ip, right);
2465 __ SmiUntag(r0, left);
2466 __ Mul(scratch1, r0, ip);
2467 // Check for overflowing the smi range - no overflow if higher 33 bits of
2468 // the result are identical.
2469 __ TestIfInt32(scratch1, r0);
2472 __ SmiUntag(ip, right);
2473 __ mullw(scratch1, left, ip);
2474 __ mulhw(scratch2, left, ip);
2475 // Check for overflowing the smi range - no overflow if higher 33 bits of
2476 // the result are identical.
2477 __ TestIfInt32(scratch2, scratch1, ip);
2480 // Go slow on zero result to handle -0.
2481 __ cmpi(scratch1, Operand::Zero());
2483 #if V8_TARGET_ARCH_PPC64
2484 __ SmiTag(right, scratch1);
2486 __ mr(right, scratch1);
2489 // We need -0 if we were multiplying a negative number with 0 to get 0.
2490 // We know one of them was zero.
2492 __ add(scratch2, right, left);
2493 __ cmpi(scratch2, Operand::Zero());
2495 __ LoadSmiLiteral(right, Smi::FromInt(0));
2499 __ orx(right, left, right);
2501 case Token::BIT_AND:
2502 __ and_(right, left, right);
2504 case Token::BIT_XOR:
2505 __ xor_(right, left, right);
2512 context()->Plug(r3);
2516 void FullCodeGenerator::EmitClassDefineProperties(ClassLiteral* lit,
2517 int* used_store_slots) {
2518 // Constructor is in r3.
2519 DCHECK(lit != NULL);
2522 // No access check is needed here since the constructor is created by the
2524 Register scratch = r4;
2526 FieldMemOperand(r3, JSFunction::kPrototypeOrInitialMapOffset));
2529 for (int i = 0; i < lit->properties()->length(); i++) {
2530 ObjectLiteral::Property* property = lit->properties()->at(i);
2531 Expression* value = property->value();
2533 if (property->is_static()) {
2534 __ LoadP(scratch, MemOperand(sp, kPointerSize)); // constructor
2536 __ LoadP(scratch, MemOperand(sp, 0)); // prototype
2539 EmitPropertyKey(property, lit->GetIdForProperty(i));
2541 // The static prototype property is read only. We handle the non computed
2542 // property name case in the parser. Since this is the only case where we
2543 // need to check for an own read only property we special case this so we do
2544 // not need to do this for every property.
2545 if (property->is_static() && property->is_computed_name()) {
2546 __ CallRuntime(Runtime::kThrowIfStaticPrototype, 1);
2550 VisitForStackValue(value);
2551 EmitSetHomeObjectIfNeeded(value, 2,
2552 lit->SlotForHomeObject(value, used_store_slots));
2554 switch (property->kind()) {
2555 case ObjectLiteral::Property::CONSTANT:
2556 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2557 case ObjectLiteral::Property::PROTOTYPE:
2559 case ObjectLiteral::Property::COMPUTED:
2560 __ CallRuntime(Runtime::kDefineClassMethod, 3);
2563 case ObjectLiteral::Property::GETTER:
2564 __ mov(r3, Operand(Smi::FromInt(DONT_ENUM)));
2566 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
2569 case ObjectLiteral::Property::SETTER:
2570 __ mov(r3, Operand(Smi::FromInt(DONT_ENUM)));
2572 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
2581 __ CallRuntime(Runtime::kToFastProperties, 1);
2584 __ CallRuntime(Runtime::kToFastProperties, 1);
2586 if (is_strong(language_mode())) {
2588 FieldMemOperand(r3, JSFunction::kPrototypeOrInitialMapOffset));
2589 __ Push(r3, scratch);
2590 // TODO(conradw): It would be more efficient to define the properties with
2591 // the right attributes the first time round.
2592 // Freeze the prototype.
2593 __ CallRuntime(Runtime::kObjectFreeze, 1);
2594 // Freeze the constructor.
2595 __ CallRuntime(Runtime::kObjectFreeze, 1);
2600 void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr, Token::Value op) {
2603 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2604 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
2605 CallIC(code, expr->BinaryOperationFeedbackId());
2606 patch_site.EmitPatchInfo();
2607 context()->Plug(r3);
2611 void FullCodeGenerator::EmitAssignment(Expression* expr,
2612 FeedbackVectorICSlot slot) {
2613 DCHECK(expr->IsValidReferenceExpressionOrThis());
2615 Property* prop = expr->AsProperty();
2616 LhsKind assign_type = Property::GetAssignType(prop);
2618 switch (assign_type) {
2620 Variable* var = expr->AsVariableProxy()->var();
2621 EffectContext context(this);
2622 EmitVariableAssignment(var, Token::ASSIGN, slot);
2625 case NAMED_PROPERTY: {
2626 __ push(r3); // Preserve value.
2627 VisitForAccumulatorValue(prop->obj());
2628 __ Move(StoreDescriptor::ReceiverRegister(), r3);
2629 __ pop(StoreDescriptor::ValueRegister()); // Restore value.
2630 __ mov(StoreDescriptor::NameRegister(),
2631 Operand(prop->key()->AsLiteral()->value()));
2632 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2636 case NAMED_SUPER_PROPERTY: {
2638 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2639 VisitForAccumulatorValue(
2640 prop->obj()->AsSuperPropertyReference()->home_object());
2641 // stack: value, this; r3: home_object
2642 Register scratch = r5;
2643 Register scratch2 = r6;
2644 __ mr(scratch, result_register()); // home_object
2645 __ LoadP(r3, MemOperand(sp, kPointerSize)); // value
2646 __ LoadP(scratch2, MemOperand(sp, 0)); // this
2647 __ StoreP(scratch2, MemOperand(sp, kPointerSize)); // this
2648 __ StoreP(scratch, MemOperand(sp, 0)); // home_object
2649 // stack: this, home_object; r3: value
2650 EmitNamedSuperPropertyStore(prop);
2653 case KEYED_SUPER_PROPERTY: {
2655 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2657 prop->obj()->AsSuperPropertyReference()->home_object());
2658 VisitForAccumulatorValue(prop->key());
2659 Register scratch = r5;
2660 Register scratch2 = r6;
2661 __ LoadP(scratch2, MemOperand(sp, 2 * kPointerSize)); // value
2662 // stack: value, this, home_object; r3: key, r6: value
2663 __ LoadP(scratch, MemOperand(sp, kPointerSize)); // this
2664 __ StoreP(scratch, MemOperand(sp, 2 * kPointerSize));
2665 __ LoadP(scratch, MemOperand(sp, 0)); // home_object
2666 __ StoreP(scratch, MemOperand(sp, kPointerSize));
2667 __ StoreP(r3, MemOperand(sp, 0));
2668 __ Move(r3, scratch2);
2669 // stack: this, home_object, key; r3: value.
2670 EmitKeyedSuperPropertyStore(prop);
2673 case KEYED_PROPERTY: {
2674 __ push(r3); // Preserve value.
2675 VisitForStackValue(prop->obj());
2676 VisitForAccumulatorValue(prop->key());
2677 __ Move(StoreDescriptor::NameRegister(), r3);
2678 __ Pop(StoreDescriptor::ValueRegister(),
2679 StoreDescriptor::ReceiverRegister());
2680 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2682 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2687 context()->Plug(r3);
2691 void FullCodeGenerator::EmitStoreToStackLocalOrContextSlot(
2692 Variable* var, MemOperand location) {
2693 __ StoreP(result_register(), location, r0);
2694 if (var->IsContextSlot()) {
2695 // RecordWrite may destroy all its register arguments.
2696 __ mr(r6, result_register());
2697 int offset = Context::SlotOffset(var->index());
2698 __ RecordWriteContextSlot(r4, offset, r6, r5, kLRHasBeenSaved,
2704 void FullCodeGenerator::EmitVariableAssignment(Variable* var, Token::Value op,
2705 FeedbackVectorICSlot slot) {
2706 if (var->IsUnallocated()) {
2707 // Global var, const, or let.
2708 __ mov(StoreDescriptor::NameRegister(), Operand(var->name()));
2709 __ LoadP(StoreDescriptor::ReceiverRegister(), GlobalObjectOperand());
2710 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2713 } else if (var->IsGlobalSlot()) {
2714 // Global var, const, or let.
2715 DCHECK(var->index() > 0);
2716 DCHECK(var->IsStaticGlobalObjectProperty());
2717 const int slot = var->index();
2718 const int depth = scope()->ContextChainLength(var->scope());
2719 if (depth <= StoreGlobalViaContextStub::kMaximumDepth) {
2720 __ mov(StoreGlobalViaContextDescriptor::SlotRegister(), Operand(slot));
2721 __ mov(StoreGlobalViaContextDescriptor::NameRegister(),
2722 Operand(var->name()));
2723 DCHECK(StoreGlobalViaContextDescriptor::ValueRegister().is(r3));
2724 StoreGlobalViaContextStub stub(isolate(), depth, language_mode());
2727 __ Push(Smi::FromInt(slot));
2728 __ Push(var->name());
2730 __ CallRuntime(is_strict(language_mode())
2731 ? Runtime::kStoreGlobalViaContext_Strict
2732 : Runtime::kStoreGlobalViaContext_Sloppy,
2735 } else if (var->mode() == LET && op != Token::INIT_LET) {
2736 // Non-initializing assignment to let variable needs a write barrier.
2737 DCHECK(!var->IsLookupSlot());
2738 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2740 MemOperand location = VarOperand(var, r4);
2741 __ LoadP(r6, location);
2742 __ CompareRoot(r6, Heap::kTheHoleValueRootIndex);
2744 __ mov(r6, Operand(var->name()));
2746 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2747 // Perform the assignment.
2749 EmitStoreToStackLocalOrContextSlot(var, location);
2751 } else if (var->mode() == CONST && op != Token::INIT_CONST) {
2752 // Assignment to const variable needs a write barrier.
2753 DCHECK(!var->IsLookupSlot());
2754 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2756 MemOperand location = VarOperand(var, r4);
2757 __ LoadP(r6, location);
2758 __ CompareRoot(r6, Heap::kTheHoleValueRootIndex);
2759 __ bne(&const_error);
2760 __ mov(r6, Operand(var->name()));
2762 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2763 __ bind(&const_error);
2764 __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2766 } else if (var->is_this() && op == Token::INIT_CONST) {
2767 // Initializing assignment to const {this} needs a write barrier.
2768 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2769 Label uninitialized_this;
2770 MemOperand location = VarOperand(var, r4);
2771 __ LoadP(r6, location);
2772 __ CompareRoot(r6, Heap::kTheHoleValueRootIndex);
2773 __ beq(&uninitialized_this);
2774 __ mov(r4, Operand(var->name()));
2776 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2777 __ bind(&uninitialized_this);
2778 EmitStoreToStackLocalOrContextSlot(var, location);
2780 } else if (!var->is_const_mode() || op == Token::INIT_CONST) {
2781 if (var->IsLookupSlot()) {
2782 // Assignment to var.
2783 __ push(r3); // Value.
2784 __ mov(r4, Operand(var->name()));
2785 __ mov(r3, Operand(Smi::FromInt(language_mode())));
2786 __ Push(cp, r4, r3); // Context, name, language mode.
2787 __ CallRuntime(Runtime::kStoreLookupSlot, 4);
2789 // Assignment to var or initializing assignment to let/const in harmony
2791 DCHECK((var->IsStackAllocated() || var->IsContextSlot()));
2792 MemOperand location = VarOperand(var, r4);
2793 if (generate_debug_code_ && op == Token::INIT_LET) {
2794 // Check for an uninitialized let binding.
2795 __ LoadP(r5, location);
2796 __ CompareRoot(r5, Heap::kTheHoleValueRootIndex);
2797 __ Check(eq, kLetBindingReInitialization);
2799 EmitStoreToStackLocalOrContextSlot(var, location);
2801 } else if (op == Token::INIT_CONST_LEGACY) {
2802 // Const initializers need a write barrier.
2803 DCHECK(var->mode() == CONST_LEGACY);
2804 DCHECK(!var->IsParameter()); // No const parameters.
2805 if (var->IsLookupSlot()) {
2807 __ mov(r3, Operand(var->name()));
2808 __ Push(cp, r3); // Context and name.
2809 __ CallRuntime(Runtime::kInitializeLegacyConstLookupSlot, 3);
2811 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2813 MemOperand location = VarOperand(var, r4);
2814 __ LoadP(r5, location);
2815 __ CompareRoot(r5, Heap::kTheHoleValueRootIndex);
2817 EmitStoreToStackLocalOrContextSlot(var, location);
2822 DCHECK(var->mode() == CONST_LEGACY && op != Token::INIT_CONST_LEGACY);
2823 if (is_strict(language_mode())) {
2824 __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2826 // Silently ignore store in sloppy mode.
2831 void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2832 // Assignment to a property, using a named store IC.
2833 Property* prop = expr->target()->AsProperty();
2834 DCHECK(prop != NULL);
2835 DCHECK(prop->key()->IsLiteral());
2837 __ mov(StoreDescriptor::NameRegister(),
2838 Operand(prop->key()->AsLiteral()->value()));
2839 __ pop(StoreDescriptor::ReceiverRegister());
2840 if (FLAG_vector_stores) {
2841 EmitLoadStoreICSlot(expr->AssignmentSlot());
2844 CallStoreIC(expr->AssignmentFeedbackId());
2847 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2848 context()->Plug(r3);
2852 void FullCodeGenerator::EmitNamedSuperPropertyStore(Property* prop) {
2853 // Assignment to named property of super.
2855 // stack : receiver ('this'), home_object
2856 DCHECK(prop != NULL);
2857 Literal* key = prop->key()->AsLiteral();
2858 DCHECK(key != NULL);
2860 __ Push(key->value());
2862 __ CallRuntime((is_strict(language_mode()) ? Runtime::kStoreToSuper_Strict
2863 : Runtime::kStoreToSuper_Sloppy),
2868 void FullCodeGenerator::EmitKeyedSuperPropertyStore(Property* prop) {
2869 // Assignment to named property of super.
2871 // stack : receiver ('this'), home_object, key
2872 DCHECK(prop != NULL);
2876 (is_strict(language_mode()) ? Runtime::kStoreKeyedToSuper_Strict
2877 : Runtime::kStoreKeyedToSuper_Sloppy),
2882 void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2883 // Assignment to a property, using a keyed store IC.
2884 __ Pop(StoreDescriptor::ReceiverRegister(), StoreDescriptor::NameRegister());
2885 DCHECK(StoreDescriptor::ValueRegister().is(r3));
2888 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2889 if (FLAG_vector_stores) {
2890 EmitLoadStoreICSlot(expr->AssignmentSlot());
2893 CallIC(ic, expr->AssignmentFeedbackId());
2896 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2897 context()->Plug(r3);
2901 void FullCodeGenerator::VisitProperty(Property* expr) {
2902 Comment cmnt(masm_, "[ Property");
2903 SetExpressionPosition(expr);
2905 Expression* key = expr->key();
2907 if (key->IsPropertyName()) {
2908 if (!expr->IsSuperAccess()) {
2909 VisitForAccumulatorValue(expr->obj());
2910 __ Move(LoadDescriptor::ReceiverRegister(), r3);
2911 EmitNamedPropertyLoad(expr);
2913 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2915 expr->obj()->AsSuperPropertyReference()->home_object());
2916 EmitNamedSuperPropertyLoad(expr);
2919 if (!expr->IsSuperAccess()) {
2920 VisitForStackValue(expr->obj());
2921 VisitForAccumulatorValue(expr->key());
2922 __ Move(LoadDescriptor::NameRegister(), r3);
2923 __ pop(LoadDescriptor::ReceiverRegister());
2924 EmitKeyedPropertyLoad(expr);
2926 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2928 expr->obj()->AsSuperPropertyReference()->home_object());
2929 VisitForStackValue(expr->key());
2930 EmitKeyedSuperPropertyLoad(expr);
2933 PrepareForBailoutForId(expr->LoadId(), TOS_REG);
2934 context()->Plug(r3);
2938 void FullCodeGenerator::CallIC(Handle<Code> code, TypeFeedbackId ast_id) {
2940 __ Call(code, RelocInfo::CODE_TARGET, ast_id);
2944 // Code common for calls using the IC.
2945 void FullCodeGenerator::EmitCallWithLoadIC(Call* expr) {
2946 Expression* callee = expr->expression();
2948 CallICState::CallType call_type =
2949 callee->IsVariableProxy() ? CallICState::FUNCTION : CallICState::METHOD;
2951 // Get the target function.
2952 if (call_type == CallICState::FUNCTION) {
2954 StackValueContext context(this);
2955 EmitVariableLoad(callee->AsVariableProxy());
2956 PrepareForBailout(callee, NO_REGISTERS);
2958 // Push undefined as receiver. This is patched in the method prologue if it
2959 // is a sloppy mode method.
2960 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
2963 // Load the function from the receiver.
2964 DCHECK(callee->IsProperty());
2965 DCHECK(!callee->AsProperty()->IsSuperAccess());
2966 __ LoadP(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
2967 EmitNamedPropertyLoad(callee->AsProperty());
2968 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2969 // Push the target function under the receiver.
2970 __ LoadP(r0, MemOperand(sp, 0));
2972 __ StoreP(r3, MemOperand(sp, kPointerSize));
2975 EmitCall(expr, call_type);
2979 void FullCodeGenerator::EmitSuperCallWithLoadIC(Call* expr) {
2980 Expression* callee = expr->expression();
2981 DCHECK(callee->IsProperty());
2982 Property* prop = callee->AsProperty();
2983 DCHECK(prop->IsSuperAccess());
2984 SetExpressionPosition(prop);
2986 Literal* key = prop->key()->AsLiteral();
2987 DCHECK(!key->value()->IsSmi());
2988 // Load the function from the receiver.
2989 const Register scratch = r4;
2990 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
2991 VisitForAccumulatorValue(super_ref->home_object());
2993 VisitForAccumulatorValue(super_ref->this_var());
2994 __ Push(scratch, r3, r3, scratch);
2995 __ Push(key->value());
2996 __ Push(Smi::FromInt(language_mode()));
3000 // - this (receiver)
3001 // - this (receiver) <-- LoadFromSuper will pop here and below.
3005 __ CallRuntime(Runtime::kLoadFromSuper, 4);
3007 // Replace home_object with target function.
3008 __ StoreP(r3, MemOperand(sp, kPointerSize));
3011 // - target function
3012 // - this (receiver)
3013 EmitCall(expr, CallICState::METHOD);
3017 // Code common for calls using the IC.
3018 void FullCodeGenerator::EmitKeyedCallWithLoadIC(Call* expr, Expression* key) {
3020 VisitForAccumulatorValue(key);
3022 Expression* callee = expr->expression();
3024 // Load the function from the receiver.
3025 DCHECK(callee->IsProperty());
3026 __ LoadP(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
3027 __ Move(LoadDescriptor::NameRegister(), r3);
3028 EmitKeyedPropertyLoad(callee->AsProperty());
3029 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
3031 // Push the target function under the receiver.
3032 __ LoadP(ip, MemOperand(sp, 0));
3034 __ StoreP(r3, MemOperand(sp, kPointerSize));
3036 EmitCall(expr, CallICState::METHOD);
3040 void FullCodeGenerator::EmitKeyedSuperCallWithLoadIC(Call* expr) {
3041 Expression* callee = expr->expression();
3042 DCHECK(callee->IsProperty());
3043 Property* prop = callee->AsProperty();
3044 DCHECK(prop->IsSuperAccess());
3046 SetExpressionPosition(prop);
3047 // Load the function from the receiver.
3048 const Register scratch = r4;
3049 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
3050 VisitForAccumulatorValue(super_ref->home_object());
3052 VisitForAccumulatorValue(super_ref->this_var());
3053 __ Push(scratch, r3, r3, scratch);
3054 VisitForStackValue(prop->key());
3055 __ Push(Smi::FromInt(language_mode()));
3059 // - this (receiver)
3060 // - this (receiver) <-- LoadKeyedFromSuper will pop here and below.
3064 __ CallRuntime(Runtime::kLoadKeyedFromSuper, 4);
3066 // Replace home_object with target function.
3067 __ StoreP(r3, MemOperand(sp, kPointerSize));
3070 // - target function
3071 // - this (receiver)
3072 EmitCall(expr, CallICState::METHOD);
3076 void FullCodeGenerator::EmitCall(Call* expr, CallICState::CallType call_type) {
3077 // Load the arguments.
3078 ZoneList<Expression*>* args = expr->arguments();
3079 int arg_count = args->length();
3080 for (int i = 0; i < arg_count; i++) {
3081 VisitForStackValue(args->at(i));
3084 SetCallPosition(expr, arg_count);
3085 Handle<Code> ic = CodeFactory::CallIC(isolate(), arg_count, call_type).code();
3086 __ LoadSmiLiteral(r6, SmiFromSlot(expr->CallFeedbackICSlot()));
3087 __ LoadP(r4, MemOperand(sp, (arg_count + 1) * kPointerSize), r0);
3088 // Don't assign a type feedback id to the IC, since type feedback is provided
3089 // by the vector above.
3092 RecordJSReturnSite(expr);
3093 // Restore context register.
3094 __ LoadP(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3095 context()->DropAndPlug(1, r3);
3099 void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
3100 // r7: copy of the first argument or undefined if it doesn't exist.
3101 if (arg_count > 0) {
3102 __ LoadP(r7, MemOperand(sp, arg_count * kPointerSize), r0);
3104 __ LoadRoot(r7, Heap::kUndefinedValueRootIndex);
3107 // r6: the receiver of the enclosing function.
3108 __ LoadP(r6, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3110 // r5: language mode.
3111 __ LoadSmiLiteral(r5, Smi::FromInt(language_mode()));
3113 // r4: the start position of the scope the calls resides in.
3114 __ LoadSmiLiteral(r4, Smi::FromInt(scope()->start_position()));
3116 // Do the runtime call.
3117 __ Push(r7, r6, r5, r4);
3118 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
3122 // See http://www.ecma-international.org/ecma-262/6.0/#sec-function-calls.
3123 void FullCodeGenerator::PushCalleeAndWithBaseObject(Call* expr) {
3124 VariableProxy* callee = expr->expression()->AsVariableProxy();
3125 if (callee->var()->IsLookupSlot()) {
3127 SetExpressionPosition(callee);
3128 // Generate code for loading from variables potentially shadowed by
3129 // eval-introduced variables.
3130 EmitDynamicLookupFastCase(callee, NOT_INSIDE_TYPEOF, &slow, &done);
3133 // Call the runtime to find the function to call (returned in r3) and
3134 // the object holding it (returned in r4).
3135 DCHECK(!context_register().is(r5));
3136 __ mov(r5, Operand(callee->name()));
3137 __ Push(context_register(), r5);
3138 __ CallRuntime(Runtime::kLoadLookupSlot, 2);
3139 __ Push(r3, r4); // Function, receiver.
3140 PrepareForBailoutForId(expr->LookupId(), NO_REGISTERS);
3142 // If fast case code has been generated, emit code to push the function
3143 // and receiver and have the slow path jump around this code.
3144 if (done.is_linked()) {
3150 // Pass undefined as the receiver, which is the WithBaseObject of a
3151 // non-object environment record. If the callee is sloppy, it will patch
3152 // it up to be the global receiver.
3153 __ LoadRoot(r4, Heap::kUndefinedValueRootIndex);
3158 VisitForStackValue(callee);
3159 // refEnv.WithBaseObject()
3160 __ LoadRoot(r5, Heap::kUndefinedValueRootIndex);
3161 __ push(r5); // Reserved receiver slot.
3166 void FullCodeGenerator::VisitCall(Call* expr) {
3168 // We want to verify that RecordJSReturnSite gets called on all paths
3169 // through this function. Avoid early returns.
3170 expr->return_is_recorded_ = false;
3173 Comment cmnt(masm_, "[ Call");
3174 Expression* callee = expr->expression();
3175 Call::CallType call_type = expr->GetCallType(isolate());
3177 if (call_type == Call::POSSIBLY_EVAL_CALL) {
3178 // In a call to eval, we first call RuntimeHidden_ResolvePossiblyDirectEval
3179 // to resolve the function we need to call. Then we call the resolved
3180 // function using the given arguments.
3181 ZoneList<Expression*>* args = expr->arguments();
3182 int arg_count = args->length();
3184 PushCalleeAndWithBaseObject(expr);
3186 // Push the arguments.
3187 for (int i = 0; i < arg_count; i++) {
3188 VisitForStackValue(args->at(i));
3191 // Push a copy of the function (found below the arguments) and
3193 __ LoadP(r4, MemOperand(sp, (arg_count + 1) * kPointerSize), r0);
3195 EmitResolvePossiblyDirectEval(arg_count);
3197 // Touch up the stack with the resolved function.
3198 __ StoreP(r3, MemOperand(sp, (arg_count + 1) * kPointerSize), r0);
3200 PrepareForBailoutForId(expr->EvalId(), NO_REGISTERS);
3202 // Record source position for debugger.
3203 SetCallPosition(expr, arg_count);
3204 CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
3205 __ LoadP(r4, MemOperand(sp, (arg_count + 1) * kPointerSize), r0);
3207 RecordJSReturnSite(expr);
3208 // Restore context register.
3209 __ LoadP(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3210 context()->DropAndPlug(1, r3);
3211 } else if (call_type == Call::GLOBAL_CALL) {
3212 EmitCallWithLoadIC(expr);
3214 } else if (call_type == Call::LOOKUP_SLOT_CALL) {
3215 // Call to a lookup slot (dynamically introduced variable).
3216 PushCalleeAndWithBaseObject(expr);
3218 } else if (call_type == Call::PROPERTY_CALL) {
3219 Property* property = callee->AsProperty();
3220 bool is_named_call = property->key()->IsPropertyName();
3221 if (property->IsSuperAccess()) {
3222 if (is_named_call) {
3223 EmitSuperCallWithLoadIC(expr);
3225 EmitKeyedSuperCallWithLoadIC(expr);
3228 VisitForStackValue(property->obj());
3229 if (is_named_call) {
3230 EmitCallWithLoadIC(expr);
3232 EmitKeyedCallWithLoadIC(expr, property->key());
3235 } else if (call_type == Call::SUPER_CALL) {
3236 EmitSuperConstructorCall(expr);
3238 DCHECK(call_type == Call::OTHER_CALL);
3239 // Call to an arbitrary expression not handled specially above.
3240 VisitForStackValue(callee);
3241 __ LoadRoot(r4, Heap::kUndefinedValueRootIndex);
3243 // Emit function call.
3248 // RecordJSReturnSite should have been called.
3249 DCHECK(expr->return_is_recorded_);
3254 void FullCodeGenerator::VisitCallNew(CallNew* expr) {
3255 Comment cmnt(masm_, "[ CallNew");
3256 // According to ECMA-262, section 11.2.2, page 44, the function
3257 // expression in new calls must be evaluated before the
3260 // Push constructor on the stack. If it's not a function it's used as
3261 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
3263 DCHECK(!expr->expression()->IsSuperPropertyReference());
3264 VisitForStackValue(expr->expression());
3266 // Push the arguments ("left-to-right") on the stack.
3267 ZoneList<Expression*>* args = expr->arguments();
3268 int arg_count = args->length();
3269 for (int i = 0; i < arg_count; i++) {
3270 VisitForStackValue(args->at(i));
3273 // Call the construct call builtin that handles allocation and
3274 // constructor invocation.
3275 SetConstructCallPosition(expr);
3277 // Load function and argument count into r4 and r3.
3278 __ mov(r3, Operand(arg_count));
3279 __ LoadP(r4, MemOperand(sp, arg_count * kPointerSize), r0);
3281 // Record call targets in unoptimized code.
3282 if (FLAG_pretenuring_call_new) {
3283 EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3284 DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3285 expr->CallNewFeedbackSlot().ToInt() + 1);
3288 __ Move(r5, FeedbackVector());
3289 __ LoadSmiLiteral(r6, SmiFromSlot(expr->CallNewFeedbackSlot()));
3291 CallConstructStub stub(isolate(), RECORD_CONSTRUCTOR_TARGET);
3292 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3293 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
3294 context()->Plug(r3);
3298 void FullCodeGenerator::EmitSuperConstructorCall(Call* expr) {
3299 SuperCallReference* super_call_ref =
3300 expr->expression()->AsSuperCallReference();
3301 DCHECK_NOT_NULL(super_call_ref);
3303 EmitLoadSuperConstructor(super_call_ref);
3304 __ push(result_register());
3306 // Push the arguments ("left-to-right") on the stack.
3307 ZoneList<Expression*>* args = expr->arguments();
3308 int arg_count = args->length();
3309 for (int i = 0; i < arg_count; i++) {
3310 VisitForStackValue(args->at(i));
3313 // Call the construct call builtin that handles allocation and
3314 // constructor invocation.
3315 SetConstructCallPosition(expr);
3317 // Load original constructor into r7.
3318 VisitForAccumulatorValue(super_call_ref->new_target_var());
3319 __ mr(r7, result_register());
3321 // Load function and argument count into r1 and r0.
3322 __ mov(r3, Operand(arg_count));
3323 __ LoadP(r4, MemOperand(sp, arg_count * kPointerSize));
3325 // Record call targets in unoptimized code.
3326 if (FLAG_pretenuring_call_new) {
3328 /* TODO(dslomov): support pretenuring.
3329 EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3330 DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3331 expr->CallNewFeedbackSlot().ToInt() + 1);
3335 __ Move(r5, FeedbackVector());
3336 __ LoadSmiLiteral(r6, SmiFromSlot(expr->CallFeedbackSlot()));
3338 CallConstructStub stub(isolate(), SUPER_CALL_RECORD_TARGET);
3339 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3341 RecordJSReturnSite(expr);
3343 context()->Plug(r3);
3347 void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
3348 ZoneList<Expression*>* args = expr->arguments();
3349 DCHECK(args->length() == 1);
3351 VisitForAccumulatorValue(args->at(0));
3353 Label materialize_true, materialize_false;
3354 Label* if_true = NULL;
3355 Label* if_false = NULL;
3356 Label* fall_through = NULL;
3357 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3358 &if_false, &fall_through);
3360 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3361 __ TestIfSmi(r3, r0);
3362 Split(eq, if_true, if_false, fall_through, cr0);
3364 context()->Plug(if_true, if_false);
3368 void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
3369 ZoneList<Expression*>* args = expr->arguments();
3370 DCHECK(args->length() == 1);
3372 VisitForAccumulatorValue(args->at(0));
3374 Label materialize_true, materialize_false;
3375 Label* if_true = NULL;
3376 Label* if_false = NULL;
3377 Label* fall_through = NULL;
3378 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3379 &if_false, &fall_through);
3381 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3382 __ TestIfPositiveSmi(r3, r0);
3383 Split(eq, if_true, if_false, fall_through, cr0);
3385 context()->Plug(if_true, if_false);
3389 void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
3390 ZoneList<Expression*>* args = expr->arguments();
3391 DCHECK(args->length() == 1);
3393 VisitForAccumulatorValue(args->at(0));
3395 Label materialize_true, materialize_false;
3396 Label* if_true = NULL;
3397 Label* if_false = NULL;
3398 Label* fall_through = NULL;
3399 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3400 &if_false, &fall_through);
3402 __ JumpIfSmi(r3, if_false);
3403 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3406 __ LoadP(r5, FieldMemOperand(r3, HeapObject::kMapOffset));
3407 // Undetectable objects behave like undefined when tested with typeof.
3408 __ lbz(r4, FieldMemOperand(r5, Map::kBitFieldOffset));
3409 __ andi(r0, r4, Operand(1 << Map::kIsUndetectable));
3410 __ bne(if_false, cr0);
3411 __ lbz(r4, FieldMemOperand(r5, Map::kInstanceTypeOffset));
3412 __ cmpi(r4, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
3414 __ cmpi(r4, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
3415 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3416 Split(le, if_true, if_false, fall_through);
3418 context()->Plug(if_true, if_false);
3422 void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
3423 ZoneList<Expression*>* args = expr->arguments();
3424 DCHECK(args->length() == 1);
3426 VisitForAccumulatorValue(args->at(0));
3428 Label materialize_true, materialize_false;
3429 Label* if_true = NULL;
3430 Label* if_false = NULL;
3431 Label* fall_through = NULL;
3432 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3433 &if_false, &fall_through);
3435 __ JumpIfSmi(r3, if_false);
3436 __ CompareObjectType(r3, r4, r4, FIRST_SPEC_OBJECT_TYPE);
3437 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3438 Split(ge, if_true, if_false, fall_through);
3440 context()->Plug(if_true, if_false);
3444 void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
3445 ZoneList<Expression*>* args = expr->arguments();
3446 DCHECK(args->length() == 1);
3448 VisitForAccumulatorValue(args->at(0));
3450 Label materialize_true, materialize_false;
3451 Label* if_true = NULL;
3452 Label* if_false = NULL;
3453 Label* fall_through = NULL;
3454 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3455 &if_false, &fall_through);
3457 __ JumpIfSmi(r3, if_false);
3458 __ LoadP(r4, FieldMemOperand(r3, HeapObject::kMapOffset));
3459 __ lbz(r4, FieldMemOperand(r4, Map::kBitFieldOffset));
3460 __ andi(r0, r4, Operand(1 << Map::kIsUndetectable));
3461 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3462 Split(ne, if_true, if_false, fall_through, cr0);
3464 context()->Plug(if_true, if_false);
3468 void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
3469 CallRuntime* expr) {
3470 ZoneList<Expression*>* args = expr->arguments();
3471 DCHECK(args->length() == 1);
3473 VisitForAccumulatorValue(args->at(0));
3475 Label materialize_true, materialize_false, skip_lookup;
3476 Label* if_true = NULL;
3477 Label* if_false = NULL;
3478 Label* fall_through = NULL;
3479 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3480 &if_false, &fall_through);
3482 __ AssertNotSmi(r3);
3484 __ LoadP(r4, FieldMemOperand(r3, HeapObject::kMapOffset));
3485 __ lbz(ip, FieldMemOperand(r4, Map::kBitField2Offset));
3486 __ andi(r0, ip, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
3487 __ bne(&skip_lookup, cr0);
3489 // Check for fast case object. Generate false result for slow case object.
3490 __ LoadP(r5, FieldMemOperand(r3, JSObject::kPropertiesOffset));
3491 __ LoadP(r5, FieldMemOperand(r5, HeapObject::kMapOffset));
3492 __ LoadRoot(ip, Heap::kHashTableMapRootIndex);
3496 // Look for valueOf name in the descriptor array, and indicate false if
3497 // found. Since we omit an enumeration index check, if it is added via a
3498 // transition that shares its descriptor array, this is a false positive.
3499 Label entry, loop, done;
3501 // Skip loop if no descriptors are valid.
3502 __ NumberOfOwnDescriptors(r6, r4);
3503 __ cmpi(r6, Operand::Zero());
3506 __ LoadInstanceDescriptors(r4, r7);
3507 // r7: descriptor array.
3508 // r6: valid entries in the descriptor array.
3509 __ mov(ip, Operand(DescriptorArray::kDescriptorSize));
3511 // Calculate location of the first key name.
3512 __ addi(r7, r7, Operand(DescriptorArray::kFirstOffset - kHeapObjectTag));
3513 // Calculate the end of the descriptor array.
3515 __ ShiftLeftImm(ip, r6, Operand(kPointerSizeLog2));
3518 // Loop through all the keys in the descriptor array. If one of these is the
3519 // string "valueOf" the result is false.
3520 // The use of ip to store the valueOf string assumes that it is not otherwise
3521 // used in the loop below.
3522 __ mov(ip, Operand(isolate()->factory()->value_of_string()));
3525 __ LoadP(r6, MemOperand(r7, 0));
3528 __ addi(r7, r7, Operand(DescriptorArray::kDescriptorSize * kPointerSize));
3535 // Set the bit in the map to indicate that there is no local valueOf field.
3536 __ lbz(r5, FieldMemOperand(r4, Map::kBitField2Offset));
3537 __ ori(r5, r5, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
3538 __ stb(r5, FieldMemOperand(r4, Map::kBitField2Offset));
3540 __ bind(&skip_lookup);
3542 // If a valueOf property is not found on the object check that its
3543 // prototype is the un-modified String prototype. If not result is false.
3544 __ LoadP(r5, FieldMemOperand(r4, Map::kPrototypeOffset));
3545 __ JumpIfSmi(r5, if_false);
3546 __ LoadP(r5, FieldMemOperand(r5, HeapObject::kMapOffset));
3547 __ LoadP(r6, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
3548 __ LoadP(r6, FieldMemOperand(r6, GlobalObject::kNativeContextOffset));
3550 ContextOperand(r6, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
3552 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3553 Split(eq, if_true, if_false, fall_through);
3555 context()->Plug(if_true, if_false);
3559 void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
3560 ZoneList<Expression*>* args = expr->arguments();
3561 DCHECK(args->length() == 1);
3563 VisitForAccumulatorValue(args->at(0));
3565 Label materialize_true, materialize_false;
3566 Label* if_true = NULL;
3567 Label* if_false = NULL;
3568 Label* fall_through = NULL;
3569 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3570 &if_false, &fall_through);
3572 __ JumpIfSmi(r3, if_false);
3573 __ CompareObjectType(r3, r4, r5, JS_FUNCTION_TYPE);
3574 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3575 Split(eq, if_true, if_false, fall_through);
3577 context()->Plug(if_true, if_false);
3581 void FullCodeGenerator::EmitIsMinusZero(CallRuntime* expr) {
3582 ZoneList<Expression*>* args = expr->arguments();
3583 DCHECK(args->length() == 1);
3585 VisitForAccumulatorValue(args->at(0));
3587 Label materialize_true, materialize_false;
3588 Label* if_true = NULL;
3589 Label* if_false = NULL;
3590 Label* fall_through = NULL;
3591 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3592 &if_false, &fall_through);
3594 __ CheckMap(r3, r4, Heap::kHeapNumberMapRootIndex, if_false, DO_SMI_CHECK);
3595 #if V8_TARGET_ARCH_PPC64
3596 __ LoadP(r4, FieldMemOperand(r3, HeapNumber::kValueOffset));
3597 __ li(r5, Operand(1));
3598 __ rotrdi(r5, r5, 1); // r5 = 0x80000000_00000000
3601 __ lwz(r5, FieldMemOperand(r3, HeapNumber::kExponentOffset));
3602 __ lwz(r4, FieldMemOperand(r3, HeapNumber::kMantissaOffset));
3604 __ lis(r0, Operand(SIGN_EXT_IMM16(0x8000)));
3607 __ cmpi(r4, Operand::Zero());
3611 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3612 Split(eq, if_true, if_false, fall_through);
3614 context()->Plug(if_true, if_false);
3618 void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
3619 ZoneList<Expression*>* args = expr->arguments();
3620 DCHECK(args->length() == 1);
3622 VisitForAccumulatorValue(args->at(0));
3624 Label materialize_true, materialize_false;
3625 Label* if_true = NULL;
3626 Label* if_false = NULL;
3627 Label* fall_through = NULL;
3628 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3629 &if_false, &fall_through);
3631 __ JumpIfSmi(r3, if_false);
3632 __ CompareObjectType(r3, r4, r4, JS_ARRAY_TYPE);
3633 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3634 Split(eq, if_true, if_false, fall_through);
3636 context()->Plug(if_true, if_false);
3640 void FullCodeGenerator::EmitIsTypedArray(CallRuntime* expr) {
3641 ZoneList<Expression*>* args = expr->arguments();
3642 DCHECK(args->length() == 1);
3644 VisitForAccumulatorValue(args->at(0));
3646 Label materialize_true, materialize_false;
3647 Label* if_true = NULL;
3648 Label* if_false = NULL;
3649 Label* fall_through = NULL;
3650 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3651 &if_false, &fall_through);
3653 __ JumpIfSmi(r3, if_false);
3654 __ CompareObjectType(r3, r4, r4, JS_TYPED_ARRAY_TYPE);
3655 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3656 Split(eq, if_true, if_false, fall_through);
3658 context()->Plug(if_true, if_false);
3662 void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
3663 ZoneList<Expression*>* args = expr->arguments();
3664 DCHECK(args->length() == 1);
3666 VisitForAccumulatorValue(args->at(0));
3668 Label materialize_true, materialize_false;
3669 Label* if_true = NULL;
3670 Label* if_false = NULL;
3671 Label* fall_through = NULL;
3672 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3673 &if_false, &fall_through);
3675 __ JumpIfSmi(r3, if_false);
3676 __ CompareObjectType(r3, r4, r4, JS_REGEXP_TYPE);
3677 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3678 Split(eq, if_true, if_false, fall_through);
3680 context()->Plug(if_true, if_false);
3684 void FullCodeGenerator::EmitIsJSProxy(CallRuntime* expr) {
3685 ZoneList<Expression*>* args = expr->arguments();
3686 DCHECK(args->length() == 1);
3688 VisitForAccumulatorValue(args->at(0));
3690 Label materialize_true, materialize_false;
3691 Label* if_true = NULL;
3692 Label* if_false = NULL;
3693 Label* fall_through = NULL;
3694 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3695 &if_false, &fall_through);
3697 __ JumpIfSmi(r3, if_false);
3699 Register type_reg = r5;
3700 __ LoadP(map, FieldMemOperand(r3, HeapObject::kMapOffset));
3701 __ lbz(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
3702 __ subi(type_reg, type_reg, Operand(FIRST_JS_PROXY_TYPE));
3703 __ cmpli(type_reg, Operand(LAST_JS_PROXY_TYPE - FIRST_JS_PROXY_TYPE));
3704 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3705 Split(le, if_true, if_false, fall_through);
3707 context()->Plug(if_true, if_false);
3711 void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
3712 DCHECK(expr->arguments()->length() == 0);
3714 Label materialize_true, materialize_false;
3715 Label* if_true = NULL;
3716 Label* if_false = NULL;
3717 Label* fall_through = NULL;
3718 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3719 &if_false, &fall_through);
3721 // Get the frame pointer for the calling frame.
3722 __ LoadP(r5, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3724 // Skip the arguments adaptor frame if it exists.
3725 Label check_frame_marker;
3726 __ LoadP(r4, MemOperand(r5, StandardFrameConstants::kContextOffset));
3727 __ CmpSmiLiteral(r4, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR), r0);
3728 __ bne(&check_frame_marker);
3729 __ LoadP(r5, MemOperand(r5, StandardFrameConstants::kCallerFPOffset));
3731 // Check the marker in the calling frame.
3732 __ bind(&check_frame_marker);
3733 __ LoadP(r4, MemOperand(r5, StandardFrameConstants::kMarkerOffset));
3734 STATIC_ASSERT(StackFrame::CONSTRUCT < 0x4000);
3735 __ CmpSmiLiteral(r4, Smi::FromInt(StackFrame::CONSTRUCT), r0);
3736 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3737 Split(eq, if_true, if_false, fall_through);
3739 context()->Plug(if_true, if_false);
3743 void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
3744 ZoneList<Expression*>* args = expr->arguments();
3745 DCHECK(args->length() == 2);
3747 // Load the two objects into registers and perform the comparison.
3748 VisitForStackValue(args->at(0));
3749 VisitForAccumulatorValue(args->at(1));
3751 Label materialize_true, materialize_false;
3752 Label* if_true = NULL;
3753 Label* if_false = NULL;
3754 Label* fall_through = NULL;
3755 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3756 &if_false, &fall_through);
3760 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3761 Split(eq, if_true, if_false, fall_through);
3763 context()->Plug(if_true, if_false);
3767 void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
3768 ZoneList<Expression*>* args = expr->arguments();
3769 DCHECK(args->length() == 1);
3771 // ArgumentsAccessStub expects the key in r4 and the formal
3772 // parameter count in r3.
3773 VisitForAccumulatorValue(args->at(0));
3775 __ LoadSmiLiteral(r3, Smi::FromInt(info_->scope()->num_parameters()));
3776 ArgumentsAccessStub stub(isolate(), ArgumentsAccessStub::READ_ELEMENT);
3778 context()->Plug(r3);
3782 void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
3783 DCHECK(expr->arguments()->length() == 0);
3785 // Get the number of formal parameters.
3786 __ LoadSmiLiteral(r3, Smi::FromInt(info_->scope()->num_parameters()));
3788 // Check if the calling frame is an arguments adaptor frame.
3789 __ LoadP(r5, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3790 __ LoadP(r6, MemOperand(r5, StandardFrameConstants::kContextOffset));
3791 __ CmpSmiLiteral(r6, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR), r0);
3794 // Arguments adaptor case: Read the arguments length from the
3796 __ LoadP(r3, MemOperand(r5, ArgumentsAdaptorFrameConstants::kLengthOffset));
3799 context()->Plug(r3);
3803 void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
3804 ZoneList<Expression*>* args = expr->arguments();
3805 DCHECK(args->length() == 1);
3806 Label done, null, function, non_function_constructor;
3808 VisitForAccumulatorValue(args->at(0));
3810 // If the object is a smi, we return null.
3811 __ JumpIfSmi(r3, &null);
3813 // Check that the object is a JS object but take special care of JS
3814 // functions to make sure they have 'Function' as their class.
3815 // Assume that there are only two callable types, and one of them is at
3816 // either end of the type range for JS object types. Saves extra comparisons.
3817 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
3818 __ CompareObjectType(r3, r3, r4, FIRST_SPEC_OBJECT_TYPE);
3819 // Map is now in r3.
3821 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3822 FIRST_SPEC_OBJECT_TYPE + 1);
3825 __ cmpi(r4, Operand(LAST_SPEC_OBJECT_TYPE));
3826 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_SPEC_OBJECT_TYPE - 1);
3828 // Assume that there is no larger type.
3829 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
3831 // Check if the constructor in the map is a JS function.
3832 Register instance_type = r5;
3833 __ GetMapConstructor(r3, r3, r4, instance_type);
3834 __ cmpi(instance_type, Operand(JS_FUNCTION_TYPE));
3835 __ bne(&non_function_constructor);
3837 // r3 now contains the constructor function. Grab the
3838 // instance class name from there.
3839 __ LoadP(r3, FieldMemOperand(r3, JSFunction::kSharedFunctionInfoOffset));
3841 FieldMemOperand(r3, SharedFunctionInfo::kInstanceClassNameOffset));
3844 // Functions have class 'Function'.
3846 __ LoadRoot(r3, Heap::kFunction_stringRootIndex);
3849 // Objects with a non-function constructor have class 'Object'.
3850 __ bind(&non_function_constructor);
3851 __ LoadRoot(r3, Heap::kObject_stringRootIndex);
3854 // Non-JS objects have class null.
3856 __ LoadRoot(r3, Heap::kNullValueRootIndex);
3861 context()->Plug(r3);
3865 void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3866 ZoneList<Expression*>* args = expr->arguments();
3867 DCHECK(args->length() == 1);
3868 VisitForAccumulatorValue(args->at(0)); // Load the object.
3871 // If the object is a smi return the object.
3872 __ JumpIfSmi(r3, &done);
3873 // If the object is not a value type, return the object.
3874 __ CompareObjectType(r3, r4, r4, JS_VALUE_TYPE);
3876 __ LoadP(r3, FieldMemOperand(r3, JSValue::kValueOffset));
3879 context()->Plug(r3);
3883 void FullCodeGenerator::EmitIsDate(CallRuntime* expr) {
3884 ZoneList<Expression*>* args = expr->arguments();
3885 DCHECK_EQ(1, args->length());
3887 VisitForAccumulatorValue(args->at(0));
3889 Label materialize_true, materialize_false;
3890 Label* if_true = nullptr;
3891 Label* if_false = nullptr;
3892 Label* fall_through = nullptr;
3893 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3894 &if_false, &fall_through);
3896 __ JumpIfSmi(r3, if_false);
3897 __ CompareObjectType(r3, r4, r4, JS_DATE_TYPE);
3898 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3899 Split(eq, if_true, if_false, fall_through);
3901 context()->Plug(if_true, if_false);
3905 void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3906 ZoneList<Expression*>* args = expr->arguments();
3907 DCHECK(args->length() == 2);
3908 DCHECK_NOT_NULL(args->at(1)->AsLiteral());
3909 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->value()));
3911 VisitForAccumulatorValue(args->at(0)); // Load the object.
3913 Register object = r3;
3914 Register result = r3;
3915 Register scratch0 = r11;
3916 Register scratch1 = r4;
3918 if (index->value() == 0) {
3919 __ LoadP(result, FieldMemOperand(object, JSDate::kValueOffset));
3921 Label runtime, done;
3922 if (index->value() < JSDate::kFirstUncachedField) {
3923 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3924 __ mov(scratch1, Operand(stamp));
3925 __ LoadP(scratch1, MemOperand(scratch1));
3926 __ LoadP(scratch0, FieldMemOperand(object, JSDate::kCacheStampOffset));
3927 __ cmp(scratch1, scratch0);
3930 FieldMemOperand(object, JSDate::kValueOffset +
3931 kPointerSize * index->value()),
3936 __ PrepareCallCFunction(2, scratch1);
3937 __ LoadSmiLiteral(r4, index);
3938 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
3942 context()->Plug(result);
3946 void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
3947 ZoneList<Expression*>* args = expr->arguments();
3948 DCHECK_EQ(3, args->length());
3950 Register string = r3;
3951 Register index = r4;
3952 Register value = r5;
3954 VisitForStackValue(args->at(0)); // index
3955 VisitForStackValue(args->at(1)); // value
3956 VisitForAccumulatorValue(args->at(2)); // string
3957 __ Pop(index, value);
3959 if (FLAG_debug_code) {
3960 __ TestIfSmi(value, r0);
3961 __ Check(eq, kNonSmiValue, cr0);
3962 __ TestIfSmi(index, r0);
3963 __ Check(eq, kNonSmiIndex, cr0);
3964 __ SmiUntag(index, index);
3965 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
3966 __ EmitSeqStringSetCharCheck(string, index, value, one_byte_seq_type);
3967 __ SmiTag(index, index);
3971 __ addi(ip, string, Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
3972 __ SmiToByteArrayOffset(r0, index);
3973 __ stbx(value, MemOperand(ip, r0));
3974 context()->Plug(string);
3978 void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
3979 ZoneList<Expression*>* args = expr->arguments();
3980 DCHECK_EQ(3, args->length());
3982 Register string = r3;
3983 Register index = r4;
3984 Register value = r5;
3986 VisitForStackValue(args->at(0)); // index
3987 VisitForStackValue(args->at(1)); // value
3988 VisitForAccumulatorValue(args->at(2)); // string
3989 __ Pop(index, value);
3991 if (FLAG_debug_code) {
3992 __ TestIfSmi(value, r0);
3993 __ Check(eq, kNonSmiValue, cr0);
3994 __ TestIfSmi(index, r0);
3995 __ Check(eq, kNonSmiIndex, cr0);
3996 __ SmiUntag(index, index);
3997 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
3998 __ EmitSeqStringSetCharCheck(string, index, value, two_byte_seq_type);
3999 __ SmiTag(index, index);
4003 __ addi(ip, string, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4004 __ SmiToShortArrayOffset(r0, index);
4005 __ sthx(value, MemOperand(ip, r0));
4006 context()->Plug(string);
4010 void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
4011 ZoneList<Expression*>* args = expr->arguments();
4012 DCHECK(args->length() == 2);
4013 VisitForStackValue(args->at(0)); // Load the object.
4014 VisitForAccumulatorValue(args->at(1)); // Load the value.
4015 __ pop(r4); // r3 = value. r4 = object.
4018 // If the object is a smi, return the value.
4019 __ JumpIfSmi(r4, &done);
4021 // If the object is not a value type, return the value.
4022 __ CompareObjectType(r4, r5, r5, JS_VALUE_TYPE);
4026 __ StoreP(r3, FieldMemOperand(r4, JSValue::kValueOffset), r0);
4027 // Update the write barrier. Save the value as it will be
4028 // overwritten by the write barrier code and is needed afterward.
4030 __ RecordWriteField(r4, JSValue::kValueOffset, r5, r6, kLRHasBeenSaved,
4034 context()->Plug(r3);
4038 void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
4039 ZoneList<Expression*>* args = expr->arguments();
4040 DCHECK_EQ(args->length(), 1);
4041 // Load the argument into r3 and call the stub.
4042 VisitForAccumulatorValue(args->at(0));
4044 NumberToStringStub stub(isolate());
4046 context()->Plug(r3);
4050 void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
4051 ZoneList<Expression*>* args = expr->arguments();
4052 DCHECK(args->length() == 1);
4053 VisitForAccumulatorValue(args->at(0));
4056 StringCharFromCodeGenerator generator(r3, r4);
4057 generator.GenerateFast(masm_);
4060 NopRuntimeCallHelper call_helper;
4061 generator.GenerateSlow(masm_, call_helper);
4064 context()->Plug(r4);
4068 void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
4069 ZoneList<Expression*>* args = expr->arguments();
4070 DCHECK(args->length() == 2);
4071 VisitForStackValue(args->at(0));
4072 VisitForAccumulatorValue(args->at(1));
4074 Register object = r4;
4075 Register index = r3;
4076 Register result = r6;
4080 Label need_conversion;
4081 Label index_out_of_range;
4083 StringCharCodeAtGenerator generator(object, index, result, &need_conversion,
4084 &need_conversion, &index_out_of_range,
4085 STRING_INDEX_IS_NUMBER);
4086 generator.GenerateFast(masm_);
4089 __ bind(&index_out_of_range);
4090 // When the index is out of range, the spec requires us to return
4092 __ LoadRoot(result, Heap::kNanValueRootIndex);
4095 __ bind(&need_conversion);
4096 // Load the undefined value into the result register, which will
4097 // trigger conversion.
4098 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
4101 NopRuntimeCallHelper call_helper;
4102 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
4105 context()->Plug(result);
4109 void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
4110 ZoneList<Expression*>* args = expr->arguments();
4111 DCHECK(args->length() == 2);
4112 VisitForStackValue(args->at(0));
4113 VisitForAccumulatorValue(args->at(1));
4115 Register object = r4;
4116 Register index = r3;
4117 Register scratch = r6;
4118 Register result = r3;
4122 Label need_conversion;
4123 Label index_out_of_range;
4125 StringCharAtGenerator generator(object, index, scratch, result,
4126 &need_conversion, &need_conversion,
4127 &index_out_of_range, STRING_INDEX_IS_NUMBER);
4128 generator.GenerateFast(masm_);
4131 __ bind(&index_out_of_range);
4132 // When the index is out of range, the spec requires us to return
4133 // the empty string.
4134 __ LoadRoot(result, Heap::kempty_stringRootIndex);
4137 __ bind(&need_conversion);
4138 // Move smi zero into the result register, which will trigger
4140 __ LoadSmiLiteral(result, Smi::FromInt(0));
4143 NopRuntimeCallHelper call_helper;
4144 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
4147 context()->Plug(result);
4151 void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
4152 ZoneList<Expression*>* args = expr->arguments();
4153 DCHECK_EQ(2, args->length());
4154 VisitForStackValue(args->at(0));
4155 VisitForAccumulatorValue(args->at(1));
4158 StringAddStub stub(isolate(), STRING_ADD_CHECK_BOTH, NOT_TENURED);
4160 context()->Plug(r3);
4164 void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
4165 ZoneList<Expression*>* args = expr->arguments();
4166 DCHECK(args->length() >= 2);
4168 int arg_count = args->length() - 2; // 2 ~ receiver and function.
4169 for (int i = 0; i < arg_count + 1; i++) {
4170 VisitForStackValue(args->at(i));
4172 VisitForAccumulatorValue(args->last()); // Function.
4174 Label runtime, done;
4175 // Check for non-function argument (including proxy).
4176 __ JumpIfSmi(r3, &runtime);
4177 __ CompareObjectType(r3, r4, r4, JS_FUNCTION_TYPE);
4180 // InvokeFunction requires the function in r4. Move it in there.
4181 __ mr(r4, result_register());
4182 ParameterCount count(arg_count);
4183 __ InvokeFunction(r4, count, CALL_FUNCTION, NullCallWrapper());
4184 __ LoadP(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4189 __ CallRuntime(Runtime::kCall, args->length());
4192 context()->Plug(r3);
4196 void FullCodeGenerator::EmitDefaultConstructorCallSuper(CallRuntime* expr) {
4197 ZoneList<Expression*>* args = expr->arguments();
4198 DCHECK(args->length() == 2);
4201 VisitForStackValue(args->at(0));
4204 VisitForStackValue(args->at(1));
4205 __ CallRuntime(Runtime::kGetPrototype, 1);
4206 __ mr(r4, result_register());
4209 // Load original constructor into r7.
4210 __ LoadP(r7, MemOperand(sp, 1 * kPointerSize));
4212 // Check if the calling frame is an arguments adaptor frame.
4213 Label adaptor_frame, args_set_up, runtime;
4214 __ LoadP(r5, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4215 __ LoadP(r6, MemOperand(r5, StandardFrameConstants::kContextOffset));
4216 __ CmpSmiLiteral(r6, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR), r0);
4217 __ beq(&adaptor_frame);
4219 // default constructor has no arguments, so no adaptor frame means no args.
4220 __ li(r3, Operand::Zero());
4223 // Copy arguments from adaptor frame.
4225 __ bind(&adaptor_frame);
4226 __ LoadP(r3, MemOperand(r5, ArgumentsAdaptorFrameConstants::kLengthOffset));
4229 // Get arguments pointer in r5.
4230 __ ShiftLeftImm(r0, r3, Operand(kPointerSizeLog2));
4232 __ addi(r5, r5, Operand(StandardFrameConstants::kCallerSPOffset));
4237 // Pre-decrement in order to skip receiver.
4238 __ LoadPU(r6, MemOperand(r5, -kPointerSize));
4243 __ bind(&args_set_up);
4244 __ LoadRoot(r5, Heap::kUndefinedValueRootIndex);
4246 CallConstructStub stub(isolate(), SUPER_CONSTRUCTOR_CALL);
4247 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
4251 context()->Plug(result_register());
4255 void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
4256 RegExpConstructResultStub stub(isolate());
4257 ZoneList<Expression*>* args = expr->arguments();
4258 DCHECK(args->length() == 3);
4259 VisitForStackValue(args->at(0));
4260 VisitForStackValue(args->at(1));
4261 VisitForAccumulatorValue(args->at(2));
4264 context()->Plug(r3);
4268 void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
4269 ZoneList<Expression*>* args = expr->arguments();
4270 DCHECK_EQ(2, args->length());
4271 DCHECK_NOT_NULL(args->at(0)->AsLiteral());
4272 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->value()))->value();
4274 Handle<FixedArray> jsfunction_result_caches(
4275 isolate()->native_context()->jsfunction_result_caches());
4276 if (jsfunction_result_caches->length() <= cache_id) {
4277 __ Abort(kAttemptToUseUndefinedCache);
4278 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
4279 context()->Plug(r3);
4283 VisitForAccumulatorValue(args->at(1));
4286 Register cache = r4;
4287 __ LoadP(cache, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
4288 __ LoadP(cache, FieldMemOperand(cache, GlobalObject::kNativeContextOffset));
4290 ContextOperand(cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
4292 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)), r0);
4294 Label done, not_found;
4295 __ LoadP(r5, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
4296 // r5 now holds finger offset as a smi.
4297 __ addi(r6, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4298 // r6 now points to the start of fixed array elements.
4299 __ SmiToPtrArrayOffset(r5, r5);
4300 __ LoadPUX(r5, MemOperand(r6, r5));
4301 // r6 now points to the key of the pair.
4305 __ LoadP(r3, MemOperand(r6, kPointerSize));
4308 __ bind(¬_found);
4309 // Call runtime to perform the lookup.
4310 __ Push(cache, key);
4311 __ CallRuntime(Runtime::kGetFromCacheRT, 2);
4314 context()->Plug(r3);
4318 void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
4319 ZoneList<Expression*>* args = expr->arguments();
4320 VisitForAccumulatorValue(args->at(0));
4322 Label materialize_true, materialize_false;
4323 Label* if_true = NULL;
4324 Label* if_false = NULL;
4325 Label* fall_through = NULL;
4326 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
4327 &if_false, &fall_through);
4329 __ lwz(r3, FieldMemOperand(r3, String::kHashFieldOffset));
4330 // PPC - assume ip is free
4331 __ mov(ip, Operand(String::kContainsCachedArrayIndexMask));
4332 __ and_(r0, r3, ip, SetRC);
4333 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4334 Split(eq, if_true, if_false, fall_through, cr0);
4336 context()->Plug(if_true, if_false);
4340 void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
4341 ZoneList<Expression*>* args = expr->arguments();
4342 DCHECK(args->length() == 1);
4343 VisitForAccumulatorValue(args->at(0));
4345 __ AssertString(r3);
4347 __ lwz(r3, FieldMemOperand(r3, String::kHashFieldOffset));
4348 __ IndexFromHash(r3, r3);
4350 context()->Plug(r3);
4354 void FullCodeGenerator::EmitFastOneByteArrayJoin(CallRuntime* expr) {
4355 Label bailout, done, one_char_separator, long_separator, non_trivial_array,
4356 not_size_one_array, loop, empty_separator_loop, one_char_separator_loop,
4357 one_char_separator_loop_entry, long_separator_loop;
4358 ZoneList<Expression*>* args = expr->arguments();
4359 DCHECK(args->length() == 2);
4360 VisitForStackValue(args->at(1));
4361 VisitForAccumulatorValue(args->at(0));
4363 // All aliases of the same register have disjoint lifetimes.
4364 Register array = r3;
4365 Register elements = no_reg; // Will be r3.
4366 Register result = no_reg; // Will be r3.
4367 Register separator = r4;
4368 Register array_length = r5;
4369 Register result_pos = no_reg; // Will be r5
4370 Register string_length = r6;
4371 Register string = r7;
4372 Register element = r8;
4373 Register elements_end = r9;
4374 Register scratch1 = r10;
4375 Register scratch2 = r11;
4377 // Separator operand is on the stack.
4380 // Check that the array is a JSArray.
4381 __ JumpIfSmi(array, &bailout);
4382 __ CompareObjectType(array, scratch1, scratch2, JS_ARRAY_TYPE);
4385 // Check that the array has fast elements.
4386 __ CheckFastElements(scratch1, scratch2, &bailout);
4388 // If the array has length zero, return the empty string.
4389 __ LoadP(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
4390 __ SmiUntag(array_length);
4391 __ cmpi(array_length, Operand::Zero());
4392 __ bne(&non_trivial_array);
4393 __ LoadRoot(r3, Heap::kempty_stringRootIndex);
4396 __ bind(&non_trivial_array);
4398 // Get the FixedArray containing array's elements.
4400 __ LoadP(elements, FieldMemOperand(array, JSArray::kElementsOffset));
4401 array = no_reg; // End of array's live range.
4403 // Check that all array elements are sequential one-byte strings, and
4404 // accumulate the sum of their lengths, as a smi-encoded value.
4405 __ li(string_length, Operand::Zero());
4406 __ addi(element, elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4407 __ ShiftLeftImm(elements_end, array_length, Operand(kPointerSizeLog2));
4408 __ add(elements_end, element, elements_end);
4409 // Loop condition: while (element < elements_end).
4410 // Live values in registers:
4411 // elements: Fixed array of strings.
4412 // array_length: Length of the fixed array of strings (not smi)
4413 // separator: Separator string
4414 // string_length: Accumulated sum of string lengths (smi).
4415 // element: Current array element.
4416 // elements_end: Array end.
4417 if (generate_debug_code_) {
4418 __ cmpi(array_length, Operand::Zero());
4419 __ Assert(gt, kNoEmptyArraysHereInEmitFastOneByteArrayJoin);
4422 __ LoadP(string, MemOperand(element));
4423 __ addi(element, element, Operand(kPointerSize));
4424 __ JumpIfSmi(string, &bailout);
4425 __ LoadP(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
4426 __ lbz(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
4427 __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
4428 __ LoadP(scratch1, FieldMemOperand(string, SeqOneByteString::kLengthOffset));
4430 __ AddAndCheckForOverflow(string_length, string_length, scratch1, scratch2,
4432 __ BranchOnOverflow(&bailout);
4434 __ cmp(element, elements_end);
4437 // If array_length is 1, return elements[0], a string.
4438 __ cmpi(array_length, Operand(1));
4439 __ bne(¬_size_one_array);
4440 __ LoadP(r3, FieldMemOperand(elements, FixedArray::kHeaderSize));
4443 __ bind(¬_size_one_array);
4445 // Live values in registers:
4446 // separator: Separator string
4447 // array_length: Length of the array.
4448 // string_length: Sum of string lengths (smi).
4449 // elements: FixedArray of strings.
4451 // Check that the separator is a flat one-byte string.
4452 __ JumpIfSmi(separator, &bailout);
4453 __ LoadP(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
4454 __ lbz(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
4455 __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
4457 // Add (separator length times array_length) - separator length to the
4458 // string_length to get the length of the result string.
4460 FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
4461 __ sub(string_length, string_length, scratch1);
4462 #if V8_TARGET_ARCH_PPC64
4463 __ SmiUntag(scratch1, scratch1);
4464 __ Mul(scratch2, array_length, scratch1);
4465 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
4467 __ ShiftRightImm(ip, scratch2, Operand(31), SetRC);
4468 __ bne(&bailout, cr0);
4469 __ SmiTag(scratch2, scratch2);
4471 // array_length is not smi but the other values are, so the result is a smi
4472 __ mullw(scratch2, array_length, scratch1);
4473 __ mulhw(ip, array_length, scratch1);
4474 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
4476 __ cmpi(ip, Operand::Zero());
4478 __ cmpwi(scratch2, Operand::Zero());
4482 __ AddAndCheckForOverflow(string_length, string_length, scratch2, scratch1,
4484 __ BranchOnOverflow(&bailout);
4485 __ SmiUntag(string_length);
4487 // Get first element in the array to free up the elements register to be used
4489 __ addi(element, elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4490 result = elements; // End of live range for elements.
4492 // Live values in registers:
4493 // element: First array element
4494 // separator: Separator string
4495 // string_length: Length of result string (not smi)
4496 // array_length: Length of the array.
4497 __ AllocateOneByteString(result, string_length, scratch1, scratch2,
4498 elements_end, &bailout);
4499 // Prepare for looping. Set up elements_end to end of the array. Set
4500 // result_pos to the position of the result where to write the first
4502 __ ShiftLeftImm(elements_end, array_length, Operand(kPointerSizeLog2));
4503 __ add(elements_end, element, elements_end);
4504 result_pos = array_length; // End of live range for array_length.
4505 array_length = no_reg;
4506 __ addi(result_pos, result,
4507 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4509 // Check the length of the separator.
4511 FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
4512 __ CmpSmiLiteral(scratch1, Smi::FromInt(1), r0);
4513 __ beq(&one_char_separator);
4514 __ bgt(&long_separator);
4516 // Empty separator case
4517 __ bind(&empty_separator_loop);
4518 // Live values in registers:
4519 // result_pos: the position to which we are currently copying characters.
4520 // element: Current array element.
4521 // elements_end: Array end.
4523 // Copy next array element to the result.
4524 __ LoadP(string, MemOperand(element));
4525 __ addi(element, element, Operand(kPointerSize));
4526 __ LoadP(string_length, FieldMemOperand(string, String::kLengthOffset));
4527 __ SmiUntag(string_length);
4528 __ addi(string, string,
4529 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4530 __ CopyBytes(string, result_pos, string_length, scratch1);
4531 __ cmp(element, elements_end);
4532 __ blt(&empty_separator_loop); // End while (element < elements_end).
4533 DCHECK(result.is(r3));
4536 // One-character separator case
4537 __ bind(&one_char_separator);
4538 // Replace separator with its one-byte character value.
4539 __ lbz(separator, FieldMemOperand(separator, SeqOneByteString::kHeaderSize));
4540 // Jump into the loop after the code that copies the separator, so the first
4541 // element is not preceded by a separator
4542 __ b(&one_char_separator_loop_entry);
4544 __ bind(&one_char_separator_loop);
4545 // Live values in registers:
4546 // result_pos: the position to which we are currently copying characters.
4547 // element: Current array element.
4548 // elements_end: Array end.
4549 // separator: Single separator one-byte char (in lower byte).
4551 // Copy the separator character to the result.
4552 __ stb(separator, MemOperand(result_pos));
4553 __ addi(result_pos, result_pos, Operand(1));
4555 // Copy next array element to the result.
4556 __ bind(&one_char_separator_loop_entry);
4557 __ LoadP(string, MemOperand(element));
4558 __ addi(element, element, Operand(kPointerSize));
4559 __ LoadP(string_length, FieldMemOperand(string, String::kLengthOffset));
4560 __ SmiUntag(string_length);
4561 __ addi(string, string,
4562 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4563 __ CopyBytes(string, result_pos, string_length, scratch1);
4564 __ cmpl(element, elements_end);
4565 __ blt(&one_char_separator_loop); // End while (element < elements_end).
4566 DCHECK(result.is(r3));
4569 // Long separator case (separator is more than one character). Entry is at the
4570 // label long_separator below.
4571 __ bind(&long_separator_loop);
4572 // Live values in registers:
4573 // result_pos: the position to which we are currently copying characters.
4574 // element: Current array element.
4575 // elements_end: Array end.
4576 // separator: Separator string.
4578 // Copy the separator to the result.
4579 __ LoadP(string_length, FieldMemOperand(separator, String::kLengthOffset));
4580 __ SmiUntag(string_length);
4581 __ addi(string, separator,
4582 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4583 __ CopyBytes(string, result_pos, string_length, scratch1);
4585 __ bind(&long_separator);
4586 __ LoadP(string, MemOperand(element));
4587 __ addi(element, element, Operand(kPointerSize));
4588 __ LoadP(string_length, FieldMemOperand(string, String::kLengthOffset));
4589 __ SmiUntag(string_length);
4590 __ addi(string, string,
4591 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4592 __ CopyBytes(string, result_pos, string_length, scratch1);
4593 __ cmpl(element, elements_end);
4594 __ blt(&long_separator_loop); // End while (element < elements_end).
4595 DCHECK(result.is(r3));
4599 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
4601 context()->Plug(r3);
4605 void FullCodeGenerator::EmitDebugIsActive(CallRuntime* expr) {
4606 DCHECK(expr->arguments()->length() == 0);
4607 ExternalReference debug_is_active =
4608 ExternalReference::debug_is_active_address(isolate());
4609 __ mov(ip, Operand(debug_is_active));
4610 __ lbz(r3, MemOperand(ip));
4612 context()->Plug(r3);
4616 void FullCodeGenerator::EmitLoadJSRuntimeFunction(CallRuntime* expr) {
4617 // Push the builtins object as the receiver.
4618 Register receiver = LoadDescriptor::ReceiverRegister();
4619 __ LoadP(receiver, GlobalObjectOperand());
4620 __ LoadP(receiver, FieldMemOperand(receiver, GlobalObject::kBuiltinsOffset));
4623 // Load the function from the receiver.
4624 __ mov(LoadDescriptor::NameRegister(), Operand(expr->name()));
4625 __ mov(LoadDescriptor::SlotRegister(),
4626 Operand(SmiFromSlot(expr->CallRuntimeFeedbackSlot())));
4627 CallLoadIC(NOT_INSIDE_TYPEOF);
4631 void FullCodeGenerator::EmitCallJSRuntimeFunction(CallRuntime* expr) {
4632 ZoneList<Expression*>* args = expr->arguments();
4633 int arg_count = args->length();
4635 SetCallPosition(expr, arg_count);
4636 CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
4637 __ LoadP(r4, MemOperand(sp, (arg_count + 1) * kPointerSize), r0);
4642 void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
4643 ZoneList<Expression*>* args = expr->arguments();
4644 int arg_count = args->length();
4646 if (expr->is_jsruntime()) {
4647 Comment cmnt(masm_, "[ CallRuntime");
4648 EmitLoadJSRuntimeFunction(expr);
4650 // Push the target function under the receiver.
4651 __ LoadP(ip, MemOperand(sp, 0));
4653 __ StoreP(r3, MemOperand(sp, kPointerSize));
4655 // Push the arguments ("left-to-right").
4656 for (int i = 0; i < arg_count; i++) {
4657 VisitForStackValue(args->at(i));
4660 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4661 EmitCallJSRuntimeFunction(expr);
4663 // Restore context register.
4664 __ LoadP(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4666 context()->DropAndPlug(1, r3);
4669 const Runtime::Function* function = expr->function();
4670 switch (function->function_id) {
4671 #define CALL_INTRINSIC_GENERATOR(Name) \
4672 case Runtime::kInline##Name: { \
4673 Comment cmnt(masm_, "[ Inline" #Name); \
4674 return Emit##Name(expr); \
4676 FOR_EACH_FULL_CODE_INTRINSIC(CALL_INTRINSIC_GENERATOR)
4677 #undef CALL_INTRINSIC_GENERATOR
4679 Comment cmnt(masm_, "[ CallRuntime for unhandled intrinsic");
4680 // Push the arguments ("left-to-right").
4681 for (int i = 0; i < arg_count; i++) {
4682 VisitForStackValue(args->at(i));
4685 // Call the C runtime function.
4686 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4687 __ CallRuntime(expr->function(), arg_count);
4688 context()->Plug(r3);
4695 void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
4696 switch (expr->op()) {
4697 case Token::DELETE: {
4698 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
4699 Property* property = expr->expression()->AsProperty();
4700 VariableProxy* proxy = expr->expression()->AsVariableProxy();
4702 if (property != NULL) {
4703 VisitForStackValue(property->obj());
4704 VisitForStackValue(property->key());
4705 __ LoadSmiLiteral(r4, Smi::FromInt(language_mode()));
4707 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4708 context()->Plug(r3);
4709 } else if (proxy != NULL) {
4710 Variable* var = proxy->var();
4711 // Delete of an unqualified identifier is disallowed in strict mode but
4712 // "delete this" is allowed.
4713 bool is_this = var->HasThisName(isolate());
4714 DCHECK(is_sloppy(language_mode()) || is_this);
4715 if (var->IsUnallocatedOrGlobalSlot()) {
4716 __ LoadP(r5, GlobalObjectOperand());
4717 __ mov(r4, Operand(var->name()));
4718 __ LoadSmiLiteral(r3, Smi::FromInt(SLOPPY));
4719 __ Push(r5, r4, r3);
4720 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4721 context()->Plug(r3);
4722 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
4723 // Result of deleting non-global, non-dynamic variables is false.
4724 // The subexpression does not have side effects.
4725 context()->Plug(is_this);
4727 // Non-global variable. Call the runtime to try to delete from the
4728 // context where the variable was introduced.
4729 DCHECK(!context_register().is(r5));
4730 __ mov(r5, Operand(var->name()));
4731 __ Push(context_register(), r5);
4732 __ CallRuntime(Runtime::kDeleteLookupSlot, 2);
4733 context()->Plug(r3);
4736 // Result of deleting non-property, non-variable reference is true.
4737 // The subexpression may have side effects.
4738 VisitForEffect(expr->expression());
4739 context()->Plug(true);
4745 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
4746 VisitForEffect(expr->expression());
4747 context()->Plug(Heap::kUndefinedValueRootIndex);
4752 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
4753 if (context()->IsEffect()) {
4754 // Unary NOT has no side effects so it's only necessary to visit the
4755 // subexpression. Match the optimizing compiler by not branching.
4756 VisitForEffect(expr->expression());
4757 } else if (context()->IsTest()) {
4758 const TestContext* test = TestContext::cast(context());
4759 // The labels are swapped for the recursive call.
4760 VisitForControl(expr->expression(), test->false_label(),
4761 test->true_label(), test->fall_through());
4762 context()->Plug(test->true_label(), test->false_label());
4764 // We handle value contexts explicitly rather than simply visiting
4765 // for control and plugging the control flow into the context,
4766 // because we need to prepare a pair of extra administrative AST ids
4767 // for the optimizing compiler.
4768 DCHECK(context()->IsAccumulatorValue() || context()->IsStackValue());
4769 Label materialize_true, materialize_false, done;
4770 VisitForControl(expr->expression(), &materialize_false,
4771 &materialize_true, &materialize_true);
4772 __ bind(&materialize_true);
4773 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
4774 __ LoadRoot(r3, Heap::kTrueValueRootIndex);
4775 if (context()->IsStackValue()) __ push(r3);
4777 __ bind(&materialize_false);
4778 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
4779 __ LoadRoot(r3, Heap::kFalseValueRootIndex);
4780 if (context()->IsStackValue()) __ push(r3);
4786 case Token::TYPEOF: {
4787 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
4789 AccumulatorValueContext context(this);
4790 VisitForTypeofValue(expr->expression());
4793 TypeofStub typeof_stub(isolate());
4794 __ CallStub(&typeof_stub);
4795 context()->Plug(r3);
4805 void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
4806 DCHECK(expr->expression()->IsValidReferenceExpressionOrThis());
4808 Comment cmnt(masm_, "[ CountOperation");
4810 Property* prop = expr->expression()->AsProperty();
4811 LhsKind assign_type = Property::GetAssignType(prop);
4813 // Evaluate expression and get value.
4814 if (assign_type == VARIABLE) {
4815 DCHECK(expr->expression()->AsVariableProxy()->var() != NULL);
4816 AccumulatorValueContext context(this);
4817 EmitVariableLoad(expr->expression()->AsVariableProxy());
4819 // Reserve space for result of postfix operation.
4820 if (expr->is_postfix() && !context()->IsEffect()) {
4821 __ LoadSmiLiteral(ip, Smi::FromInt(0));
4824 switch (assign_type) {
4825 case NAMED_PROPERTY: {
4826 // Put the object both on the stack and in the register.
4827 VisitForStackValue(prop->obj());
4828 __ LoadP(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
4829 EmitNamedPropertyLoad(prop);
4833 case NAMED_SUPER_PROPERTY: {
4834 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4835 VisitForAccumulatorValue(
4836 prop->obj()->AsSuperPropertyReference()->home_object());
4837 __ Push(result_register());
4838 const Register scratch = r4;
4839 __ LoadP(scratch, MemOperand(sp, kPointerSize));
4840 __ Push(scratch, result_register());
4841 EmitNamedSuperPropertyLoad(prop);
4845 case KEYED_SUPER_PROPERTY: {
4846 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4847 VisitForAccumulatorValue(
4848 prop->obj()->AsSuperPropertyReference()->home_object());
4849 const Register scratch = r4;
4850 const Register scratch1 = r5;
4851 __ mr(scratch, result_register());
4852 VisitForAccumulatorValue(prop->key());
4853 __ Push(scratch, result_register());
4854 __ LoadP(scratch1, MemOperand(sp, 2 * kPointerSize));
4855 __ Push(scratch1, scratch, result_register());
4856 EmitKeyedSuperPropertyLoad(prop);
4860 case KEYED_PROPERTY: {
4861 VisitForStackValue(prop->obj());
4862 VisitForStackValue(prop->key());
4863 __ LoadP(LoadDescriptor::ReceiverRegister(),
4864 MemOperand(sp, 1 * kPointerSize));
4865 __ LoadP(LoadDescriptor::NameRegister(), MemOperand(sp, 0));
4866 EmitKeyedPropertyLoad(prop);
4875 // We need a second deoptimization point after loading the value
4876 // in case evaluating the property load my have a side effect.
4877 if (assign_type == VARIABLE) {
4878 PrepareForBailout(expr->expression(), TOS_REG);
4880 PrepareForBailoutForId(prop->LoadId(), TOS_REG);
4883 // Inline smi case if we are in a loop.
4884 Label stub_call, done;
4885 JumpPatchSite patch_site(masm_);
4887 int count_value = expr->op() == Token::INC ? 1 : -1;
4888 if (ShouldInlineSmiCase(expr->op())) {
4890 patch_site.EmitJumpIfNotSmi(r3, &slow);
4892 // Save result for postfix expressions.
4893 if (expr->is_postfix()) {
4894 if (!context()->IsEffect()) {
4895 // Save the result on the stack. If we have a named or keyed property
4896 // we store the result under the receiver that is currently on top
4898 switch (assign_type) {
4902 case NAMED_PROPERTY:
4903 __ StoreP(r3, MemOperand(sp, kPointerSize));
4905 case NAMED_SUPER_PROPERTY:
4906 __ StoreP(r3, MemOperand(sp, 2 * kPointerSize));
4908 case KEYED_PROPERTY:
4909 __ StoreP(r3, MemOperand(sp, 2 * kPointerSize));
4911 case KEYED_SUPER_PROPERTY:
4912 __ StoreP(r3, MemOperand(sp, 3 * kPointerSize));
4918 Register scratch1 = r4;
4919 Register scratch2 = r5;
4920 __ LoadSmiLiteral(scratch1, Smi::FromInt(count_value));
4921 __ AddAndCheckForOverflow(r3, r3, scratch1, scratch2, r0);
4922 __ BranchOnNoOverflow(&done);
4923 // Call stub. Undo operation first.
4924 __ sub(r3, r3, scratch1);
4928 if (!is_strong(language_mode())) {
4929 ToNumberStub convert_stub(isolate());
4930 __ CallStub(&convert_stub);
4931 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
4934 // Save result for postfix expressions.
4935 if (expr->is_postfix()) {
4936 if (!context()->IsEffect()) {
4937 // Save the result on the stack. If we have a named or keyed property
4938 // we store the result under the receiver that is currently on top
4940 switch (assign_type) {
4944 case NAMED_PROPERTY:
4945 __ StoreP(r3, MemOperand(sp, kPointerSize));
4947 case NAMED_SUPER_PROPERTY:
4948 __ StoreP(r3, MemOperand(sp, 2 * kPointerSize));
4950 case KEYED_PROPERTY:
4951 __ StoreP(r3, MemOperand(sp, 2 * kPointerSize));
4953 case KEYED_SUPER_PROPERTY:
4954 __ StoreP(r3, MemOperand(sp, 3 * kPointerSize));
4960 __ bind(&stub_call);
4962 __ LoadSmiLiteral(r3, Smi::FromInt(count_value));
4964 SetExpressionPosition(expr);
4966 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), Token::ADD,
4967 strength(language_mode())).code();
4968 CallIC(code, expr->CountBinOpFeedbackId());
4969 patch_site.EmitPatchInfo();
4972 if (is_strong(language_mode())) {
4973 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
4975 // Store the value returned in r3.
4976 switch (assign_type) {
4978 if (expr->is_postfix()) {
4980 EffectContext context(this);
4981 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4982 Token::ASSIGN, expr->CountSlot());
4983 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4986 // For all contexts except EffectConstant We have the result on
4987 // top of the stack.
4988 if (!context()->IsEffect()) {
4989 context()->PlugTOS();
4992 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4993 Token::ASSIGN, expr->CountSlot());
4994 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4995 context()->Plug(r3);
4998 case NAMED_PROPERTY: {
4999 __ mov(StoreDescriptor::NameRegister(),
5000 Operand(prop->key()->AsLiteral()->value()));
5001 __ pop(StoreDescriptor::ReceiverRegister());
5002 if (FLAG_vector_stores) {
5003 EmitLoadStoreICSlot(expr->CountSlot());
5006 CallStoreIC(expr->CountStoreFeedbackId());
5008 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5009 if (expr->is_postfix()) {
5010 if (!context()->IsEffect()) {
5011 context()->PlugTOS();
5014 context()->Plug(r3);
5018 case NAMED_SUPER_PROPERTY: {
5019 EmitNamedSuperPropertyStore(prop);
5020 if (expr->is_postfix()) {
5021 if (!context()->IsEffect()) {
5022 context()->PlugTOS();
5025 context()->Plug(r3);
5029 case KEYED_SUPER_PROPERTY: {
5030 EmitKeyedSuperPropertyStore(prop);
5031 if (expr->is_postfix()) {
5032 if (!context()->IsEffect()) {
5033 context()->PlugTOS();
5036 context()->Plug(r3);
5040 case KEYED_PROPERTY: {
5041 __ Pop(StoreDescriptor::ReceiverRegister(),
5042 StoreDescriptor::NameRegister());
5044 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
5045 if (FLAG_vector_stores) {
5046 EmitLoadStoreICSlot(expr->CountSlot());
5049 CallIC(ic, expr->CountStoreFeedbackId());
5051 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5052 if (expr->is_postfix()) {
5053 if (!context()->IsEffect()) {
5054 context()->PlugTOS();
5057 context()->Plug(r3);
5065 void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
5066 Expression* sub_expr,
5067 Handle<String> check) {
5068 Label materialize_true, materialize_false;
5069 Label* if_true = NULL;
5070 Label* if_false = NULL;
5071 Label* fall_through = NULL;
5072 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
5073 &if_false, &fall_through);
5076 AccumulatorValueContext context(this);
5077 VisitForTypeofValue(sub_expr);
5079 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5081 Factory* factory = isolate()->factory();
5082 if (String::Equals(check, factory->number_string())) {
5083 __ JumpIfSmi(r3, if_true);
5084 __ LoadP(r3, FieldMemOperand(r3, HeapObject::kMapOffset));
5085 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
5087 Split(eq, if_true, if_false, fall_through);
5088 } else if (String::Equals(check, factory->string_string())) {
5089 __ JumpIfSmi(r3, if_false);
5090 // Check for undetectable objects => false.
5091 __ CompareObjectType(r3, r3, r4, FIRST_NONSTRING_TYPE);
5093 __ lbz(r4, FieldMemOperand(r3, Map::kBitFieldOffset));
5094 STATIC_ASSERT((1 << Map::kIsUndetectable) < 0x8000);
5095 __ andi(r0, r4, Operand(1 << Map::kIsUndetectable));
5096 Split(eq, if_true, if_false, fall_through, cr0);
5097 } else if (String::Equals(check, factory->symbol_string())) {
5098 __ JumpIfSmi(r3, if_false);
5099 __ CompareObjectType(r3, r3, r4, SYMBOL_TYPE);
5100 Split(eq, if_true, if_false, fall_through);
5101 } else if (String::Equals(check, factory->float32x4_string())) {
5102 __ JumpIfSmi(r3, if_false);
5103 __ CompareObjectType(r3, r3, r4, FLOAT32X4_TYPE);
5104 Split(eq, if_true, if_false, fall_through);
5105 } else if (String::Equals(check, factory->boolean_string())) {
5106 __ CompareRoot(r3, Heap::kTrueValueRootIndex);
5108 __ CompareRoot(r3, Heap::kFalseValueRootIndex);
5109 Split(eq, if_true, if_false, fall_through);
5110 } else if (String::Equals(check, factory->undefined_string())) {
5111 __ CompareRoot(r3, Heap::kUndefinedValueRootIndex);
5113 __ JumpIfSmi(r3, if_false);
5114 // Check for undetectable objects => true.
5115 __ LoadP(r3, FieldMemOperand(r3, HeapObject::kMapOffset));
5116 __ lbz(r4, FieldMemOperand(r3, Map::kBitFieldOffset));
5117 __ andi(r0, r4, Operand(1 << Map::kIsUndetectable));
5118 Split(ne, if_true, if_false, fall_through, cr0);
5120 } else if (String::Equals(check, factory->function_string())) {
5121 __ JumpIfSmi(r3, if_false);
5122 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5123 __ CompareObjectType(r3, r3, r4, JS_FUNCTION_TYPE);
5125 __ cmpi(r4, Operand(JS_FUNCTION_PROXY_TYPE));
5126 Split(eq, if_true, if_false, fall_through);
5127 } else if (String::Equals(check, factory->object_string())) {
5128 __ JumpIfSmi(r3, if_false);
5129 __ CompareRoot(r3, Heap::kNullValueRootIndex);
5131 // Check for JS objects => true.
5132 __ CompareObjectType(r3, r3, r4, FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
5134 __ CompareInstanceType(r3, r4, LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
5136 // Check for undetectable objects => false.
5137 __ lbz(r4, FieldMemOperand(r3, Map::kBitFieldOffset));
5138 __ andi(r0, r4, Operand(1 << Map::kIsUndetectable));
5139 Split(eq, if_true, if_false, fall_through, cr0);
5141 if (if_false != fall_through) __ b(if_false);
5143 context()->Plug(if_true, if_false);
5147 void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
5148 Comment cmnt(masm_, "[ CompareOperation");
5149 SetExpressionPosition(expr);
5151 // First we try a fast inlined version of the compare when one of
5152 // the operands is a literal.
5153 if (TryLiteralCompare(expr)) return;
5155 // Always perform the comparison for its control flow. Pack the result
5156 // into the expression's context after the comparison is performed.
5157 Label materialize_true, materialize_false;
5158 Label* if_true = NULL;
5159 Label* if_false = NULL;
5160 Label* fall_through = NULL;
5161 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
5162 &if_false, &fall_through);
5164 Token::Value op = expr->op();
5165 VisitForStackValue(expr->left());
5168 VisitForStackValue(expr->right());
5169 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
5170 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
5171 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
5173 Split(eq, if_true, if_false, fall_through);
5176 case Token::INSTANCEOF: {
5177 VisitForStackValue(expr->right());
5178 InstanceofStub stub(isolate(), InstanceofStub::kNoFlags);
5180 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5181 // The stub returns 0 for true.
5182 __ cmpi(r3, Operand::Zero());
5183 Split(eq, if_true, if_false, fall_through);
5188 VisitForAccumulatorValue(expr->right());
5189 Condition cond = CompareIC::ComputeCondition(op);
5192 bool inline_smi_code = ShouldInlineSmiCase(op);
5193 JumpPatchSite patch_site(masm_);
5194 if (inline_smi_code) {
5197 patch_site.EmitJumpIfNotSmi(r5, &slow_case);
5199 Split(cond, if_true, if_false, NULL);
5200 __ bind(&slow_case);
5203 Handle<Code> ic = CodeFactory::CompareIC(
5204 isolate(), op, strength(language_mode())).code();
5205 CallIC(ic, expr->CompareOperationFeedbackId());
5206 patch_site.EmitPatchInfo();
5207 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5208 __ cmpi(r3, Operand::Zero());
5209 Split(cond, if_true, if_false, fall_through);
5213 // Convert the result of the comparison into one expected for this
5214 // expression's context.
5215 context()->Plug(if_true, if_false);
5219 void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
5220 Expression* sub_expr,
5222 Label materialize_true, materialize_false;
5223 Label* if_true = NULL;
5224 Label* if_false = NULL;
5225 Label* fall_through = NULL;
5226 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
5227 &if_false, &fall_through);
5229 VisitForAccumulatorValue(sub_expr);
5230 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5231 if (expr->op() == Token::EQ_STRICT) {
5232 Heap::RootListIndex nil_value = nil == kNullValue
5233 ? Heap::kNullValueRootIndex
5234 : Heap::kUndefinedValueRootIndex;
5235 __ LoadRoot(r4, nil_value);
5237 Split(eq, if_true, if_false, fall_through);
5239 Handle<Code> ic = CompareNilICStub::GetUninitialized(isolate(), nil);
5240 CallIC(ic, expr->CompareOperationFeedbackId());
5241 __ cmpi(r3, Operand::Zero());
5242 Split(ne, if_true, if_false, fall_through);
5244 context()->Plug(if_true, if_false);
5248 void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
5249 __ LoadP(r3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5250 context()->Plug(r3);
5254 Register FullCodeGenerator::result_register() { return r3; }
5257 Register FullCodeGenerator::context_register() { return cp; }
5260 void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
5261 DCHECK_EQ(static_cast<int>(POINTER_SIZE_ALIGN(frame_offset)), frame_offset);
5262 __ StoreP(value, MemOperand(fp, frame_offset), r0);
5266 void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
5267 __ LoadP(dst, ContextOperand(cp, context_index), r0);
5271 void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
5272 Scope* closure_scope = scope()->ClosureScope();
5273 if (closure_scope->is_script_scope() ||
5274 closure_scope->is_module_scope()) {
5275 // Contexts nested in the native context have a canonical empty function
5276 // as their closure, not the anonymous closure containing the global
5277 // code. Pass a smi sentinel and let the runtime look up the empty
5279 __ LoadSmiLiteral(ip, Smi::FromInt(0));
5280 } else if (closure_scope->is_eval_scope()) {
5281 // Contexts created by a call to eval have the same closure as the
5282 // context calling eval, not the anonymous closure containing the eval
5283 // code. Fetch it from the context.
5284 __ LoadP(ip, ContextOperand(cp, Context::CLOSURE_INDEX));
5286 DCHECK(closure_scope->is_function_scope());
5287 __ LoadP(ip, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5293 // ----------------------------------------------------------------------------
5294 // Non-local control flow support.
5296 void FullCodeGenerator::EnterFinallyBlock() {
5297 DCHECK(!result_register().is(r4));
5298 // Store result register while executing finally block.
5299 __ push(result_register());
5300 // Cook return address in link register to stack (smi encoded Code* delta)
5302 __ mov(ip, Operand(masm_->CodeObject()));
5306 // Store result register while executing finally block.
5309 // Store pending message while executing finally block.
5310 ExternalReference pending_message_obj =
5311 ExternalReference::address_of_pending_message_obj(isolate());
5312 __ mov(ip, Operand(pending_message_obj));
5313 __ LoadP(r4, MemOperand(ip));
5316 ClearPendingMessage();
5320 void FullCodeGenerator::ExitFinallyBlock() {
5321 DCHECK(!result_register().is(r4));
5322 // Restore pending message from stack.
5324 ExternalReference pending_message_obj =
5325 ExternalReference::address_of_pending_message_obj(isolate());
5326 __ mov(ip, Operand(pending_message_obj));
5327 __ StoreP(r4, MemOperand(ip));
5329 // Restore result register from stack.
5332 // Uncook return address and return.
5333 __ pop(result_register());
5335 __ mov(ip, Operand(masm_->CodeObject()));
5342 void FullCodeGenerator::ClearPendingMessage() {
5343 DCHECK(!result_register().is(r4));
5344 ExternalReference pending_message_obj =
5345 ExternalReference::address_of_pending_message_obj(isolate());
5346 __ LoadRoot(r4, Heap::kTheHoleValueRootIndex);
5347 __ mov(ip, Operand(pending_message_obj));
5348 __ StoreP(r4, MemOperand(ip));
5352 void FullCodeGenerator::EmitLoadStoreICSlot(FeedbackVectorICSlot slot) {
5353 DCHECK(FLAG_vector_stores && !slot.IsInvalid());
5354 __ mov(VectorStoreICTrampolineDescriptor::SlotRegister(),
5355 Operand(SmiFromSlot(slot)));
5362 void BackEdgeTable::PatchAt(Code* unoptimized_code, Address pc,
5363 BackEdgeState target_state,
5364 Code* replacement_code) {
5365 Address mov_address = Assembler::target_address_from_return_address(pc);
5366 Address cmp_address = mov_address - 2 * Assembler::kInstrSize;
5367 CodePatcher patcher(cmp_address, 1);
5369 switch (target_state) {
5371 // <decrement profiling counter>
5373 // bge <ok> ;; not changed
5374 // mov r12, <interrupt stub address>
5377 // <reset profiling counter>
5379 patcher.masm()->cmpi(r6, Operand::Zero());
5382 case ON_STACK_REPLACEMENT:
5383 case OSR_AFTER_STACK_CHECK:
5384 // <decrement profiling counter>
5386 // bge <ok> ;; not changed
5387 // mov r12, <on-stack replacement address>
5390 // <reset profiling counter>
5391 // ok-label ----- pc_after points here
5393 // Set the LT bit such that bge is a NOP
5394 patcher.masm()->crset(Assembler::encode_crbit(cr7, CR_LT));
5398 // Replace the stack check address in the mov sequence with the
5399 // entry address of the replacement code.
5400 Assembler::set_target_address_at(mov_address, unoptimized_code,
5401 replacement_code->entry());
5403 unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
5404 unoptimized_code, mov_address, replacement_code);
5408 BackEdgeTable::BackEdgeState BackEdgeTable::GetBackEdgeState(
5409 Isolate* isolate, Code* unoptimized_code, Address pc) {
5410 Address mov_address = Assembler::target_address_from_return_address(pc);
5411 Address cmp_address = mov_address - 2 * Assembler::kInstrSize;
5412 Address interrupt_address =
5413 Assembler::target_address_at(mov_address, unoptimized_code);
5415 if (Assembler::IsCmpImmediate(Assembler::instr_at(cmp_address))) {
5416 DCHECK(interrupt_address == isolate->builtins()->InterruptCheck()->entry());
5420 DCHECK(Assembler::IsCrSet(Assembler::instr_at(cmp_address)));
5422 if (interrupt_address == isolate->builtins()->OnStackReplacement()->entry()) {
5423 return ON_STACK_REPLACEMENT;
5426 DCHECK(interrupt_address ==
5427 isolate->builtins()->OsrAfterStackCheck()->entry());
5428 return OSR_AFTER_STACK_CHECK;
5430 } // namespace internal
5432 #endif // V8_TARGET_ARCH_PPC