1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #if V8_TARGET_ARCH_MIPS
9 // Note on Mips implementation:
11 // The result_register() for mips is the 'v0' register, which is defined
12 // by the ABI to contain function return values. However, the first
13 // parameter to a function is defined to be 'a0'. So there are many
14 // places where we have to move a previous result in v0 to a0 for the
15 // next call: mov(a0, v0). This is not needed on the other architectures.
17 #include "src/code-factory.h"
18 #include "src/code-stubs.h"
19 #include "src/codegen.h"
20 #include "src/compiler.h"
21 #include "src/debug.h"
22 #include "src/full-codegen.h"
23 #include "src/ic/ic.h"
24 #include "src/parser.h"
25 #include "src/scopes.h"
27 #include "src/mips/code-stubs-mips.h"
28 #include "src/mips/macro-assembler-mips.h"
33 #define __ ACCESS_MASM(masm_)
36 // A patch site is a location in the code which it is possible to patch. This
37 // class has a number of methods to emit the code which is patchable and the
38 // method EmitPatchInfo to record a marker back to the patchable code. This
39 // marker is a andi zero_reg, rx, #yyyy instruction, and rx * 0x0000ffff + yyyy
40 // (raw 16 bit immediate value is used) is the delta from the pc to the first
41 // instruction of the patchable code.
42 // The marker instruction is effectively a NOP (dest is zero_reg) and will
43 // never be emitted by normal code.
44 class JumpPatchSite BASE_EMBEDDED {
46 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
48 info_emitted_ = false;
53 DCHECK(patch_site_.is_bound() == info_emitted_);
56 // When initially emitting this ensure that a jump is always generated to skip
57 // the inlined smi code.
58 void EmitJumpIfNotSmi(Register reg, Label* target) {
59 DCHECK(!patch_site_.is_bound() && !info_emitted_);
60 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
61 __ bind(&patch_site_);
63 // Always taken before patched.
64 __ BranchShort(target, eq, at, Operand(zero_reg));
67 // When initially emitting this ensure that a jump is never generated to skip
68 // the inlined smi code.
69 void EmitJumpIfSmi(Register reg, Label* target) {
70 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
71 DCHECK(!patch_site_.is_bound() && !info_emitted_);
72 __ bind(&patch_site_);
74 // Never taken before patched.
75 __ BranchShort(target, ne, at, Operand(zero_reg));
78 void EmitPatchInfo() {
79 if (patch_site_.is_bound()) {
80 int delta_to_patch_site = masm_->InstructionsGeneratedSince(&patch_site_);
81 Register reg = Register::from_code(delta_to_patch_site / kImm16Mask);
82 __ andi(zero_reg, reg, delta_to_patch_site % kImm16Mask);
87 __ nop(); // Signals no inlined code.
92 MacroAssembler* masm_;
100 // Generate code for a JS function. On entry to the function the receiver
101 // and arguments have been pushed on the stack left to right. The actual
102 // argument count matches the formal parameter count expected by the
105 // The live registers are:
106 // o a1: the JS function object being called (i.e. ourselves)
108 // o fp: our caller's frame pointer
109 // o sp: stack pointer
110 // o ra: return address
112 // The function builds a JS frame. Please see JavaScriptFrameConstants in
113 // frames-mips.h for its layout.
114 void FullCodeGenerator::Generate() {
115 CompilationInfo* info = info_;
116 profiling_counter_ = isolate()->factory()->NewCell(
117 Handle<Smi>(Smi::FromInt(FLAG_interrupt_budget), isolate()));
118 SetFunctionPosition(function());
119 Comment cmnt(masm_, "[ function compiled by full code generator");
121 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
124 if (strlen(FLAG_stop_at) > 0 &&
125 info->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
130 // Sloppy mode functions and builtins need to replace the receiver with the
131 // global proxy when called as functions (without an explicit receiver
133 if (is_sloppy(info->language_mode()) && !info->is_native() &&
134 info->MayUseThis() && info->scope()->has_this_declaration()) {
136 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
137 __ lw(at, MemOperand(sp, receiver_offset));
138 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
139 __ Branch(&ok, ne, a2, Operand(at));
141 __ lw(a2, GlobalObjectOperand());
142 __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalProxyOffset));
144 __ sw(a2, MemOperand(sp, receiver_offset));
149 // Open a frame scope to indicate that there is a frame on the stack. The
150 // MANUAL indicates that the scope shouldn't actually generate code to set up
151 // the frame (that is done below).
152 FrameScope frame_scope(masm_, StackFrame::MANUAL);
154 info->set_prologue_offset(masm_->pc_offset());
155 __ Prologue(info->IsCodePreAgingActive());
156 info->AddNoFrameRange(0, masm_->pc_offset());
158 { Comment cmnt(masm_, "[ Allocate locals");
159 int locals_count = info->scope()->num_stack_slots();
160 // Generators allocate locals, if any, in context slots.
161 DCHECK(!IsGeneratorFunction(info->function()->kind()) || locals_count == 0);
162 if (locals_count > 0) {
163 if (locals_count >= 128) {
165 __ Subu(t5, sp, Operand(locals_count * kPointerSize));
166 __ LoadRoot(a2, Heap::kRealStackLimitRootIndex);
167 __ Branch(&ok, hs, t5, Operand(a2));
168 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
171 __ LoadRoot(t5, Heap::kUndefinedValueRootIndex);
172 int kMaxPushes = FLAG_optimize_for_size ? 4 : 32;
173 if (locals_count >= kMaxPushes) {
174 int loop_iterations = locals_count / kMaxPushes;
175 __ li(a2, Operand(loop_iterations));
177 __ bind(&loop_header);
179 __ Subu(sp, sp, Operand(kMaxPushes * kPointerSize));
180 for (int i = 0; i < kMaxPushes; i++) {
181 __ sw(t5, MemOperand(sp, i * kPointerSize));
183 // Continue loop if not done.
184 __ Subu(a2, a2, Operand(1));
185 __ Branch(&loop_header, ne, a2, Operand(zero_reg));
187 int remaining = locals_count % kMaxPushes;
188 // Emit the remaining pushes.
189 __ Subu(sp, sp, Operand(remaining * kPointerSize));
190 for (int i = 0; i < remaining; i++) {
191 __ sw(t5, MemOperand(sp, i * kPointerSize));
196 bool function_in_register = true;
198 // Possibly allocate a local context.
199 if (info->scope()->num_heap_slots() > 0) {
200 Comment cmnt(masm_, "[ Allocate context");
201 // Argument to NewContext is the function, which is still in a1.
202 bool need_write_barrier = true;
203 int slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
204 if (info->scope()->is_script_scope()) {
206 __ Push(info->scope()->GetScopeInfo(info->isolate()));
207 __ CallRuntime(Runtime::kNewScriptContext, 2);
208 } else if (slots <= FastNewContextStub::kMaximumSlots) {
209 FastNewContextStub stub(isolate(), slots);
211 // Result of FastNewContextStub is always in new space.
212 need_write_barrier = false;
215 __ CallRuntime(Runtime::kNewFunctionContext, 1);
217 function_in_register = false;
218 // Context is returned in v0. It replaces the context passed to us.
219 // It's saved in the stack and kept live in cp.
221 __ sw(v0, MemOperand(fp, StandardFrameConstants::kContextOffset));
222 // Copy any necessary parameters into the context.
223 int num_parameters = info->scope()->num_parameters();
224 int first_parameter = info->scope()->has_this_declaration() ? -1 : 0;
225 for (int i = first_parameter; i < num_parameters; i++) {
226 Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
227 if (var->IsContextSlot()) {
228 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
229 (num_parameters - 1 - i) * kPointerSize;
230 // Load parameter from stack.
231 __ lw(a0, MemOperand(fp, parameter_offset));
232 // Store it in the context.
233 MemOperand target = ContextOperand(cp, var->index());
236 // Update the write barrier.
237 if (need_write_barrier) {
238 __ RecordWriteContextSlot(
239 cp, target.offset(), a0, a3, kRAHasBeenSaved, kDontSaveFPRegs);
240 } else if (FLAG_debug_code) {
242 __ JumpIfInNewSpace(cp, a0, &done);
243 __ Abort(kExpectedNewSpaceObject);
250 // Possibly set up a local binding to the this function which is used in
251 // derived constructors with super calls.
252 Variable* this_function_var = scope()->this_function_var();
253 if (this_function_var != nullptr) {
254 Comment cmnt(masm_, "[ This function");
255 if (!function_in_register) {
256 __ lw(a1, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
257 // The write barrier clobbers register again, keep is marked as such.
259 SetVar(this_function_var, a1, a2, a3);
262 Variable* new_target_var = scope()->new_target_var();
263 if (new_target_var != nullptr) {
264 Comment cmnt(masm_, "[ new.target");
266 // Get the frame pointer for the calling frame.
267 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
269 // Skip the arguments adaptor frame if it exists.
270 Label check_frame_marker;
271 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
272 __ Branch(&check_frame_marker, ne, a1,
273 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
274 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
276 // Check the marker in the calling frame.
277 __ bind(&check_frame_marker);
278 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
280 Label non_construct_frame, done;
281 __ Branch(&non_construct_frame, ne, a1,
282 Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
285 MemOperand(a2, ConstructFrameConstants::kOriginalConstructorOffset));
288 __ bind(&non_construct_frame);
289 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
292 SetVar(new_target_var, v0, a2, a3);
295 // Possibly allocate RestParameters
297 Variable* rest_param = scope()->rest_parameter(&rest_index);
299 Comment cmnt(masm_, "[ Allocate rest parameter array");
301 int num_parameters = info->scope()->num_parameters();
302 int offset = num_parameters * kPointerSize;
305 Operand(StandardFrameConstants::kCallerSPOffset + offset));
306 __ li(a2, Operand(Smi::FromInt(num_parameters)));
307 __ li(a1, Operand(Smi::FromInt(rest_index)));
308 __ li(a0, Operand(Smi::FromInt(language_mode())));
309 __ Push(a3, a2, a1, a0);
311 RestParamAccessStub stub(isolate());
314 SetVar(rest_param, v0, a1, a2);
317 Variable* arguments = scope()->arguments();
318 if (arguments != NULL) {
319 // Function uses arguments object.
320 Comment cmnt(masm_, "[ Allocate arguments object");
321 if (!function_in_register) {
322 // Load this again, if it's used by the local context below.
323 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
327 // Receiver is just before the parameters on the caller's stack.
328 int num_parameters = info->scope()->num_parameters();
329 int offset = num_parameters * kPointerSize;
331 Operand(StandardFrameConstants::kCallerSPOffset + offset));
332 __ li(a1, Operand(Smi::FromInt(num_parameters)));
335 // Arguments to ArgumentsAccessStub:
336 // function, receiver address, parameter count.
337 // The stub will rewrite receiever and parameter count if the previous
338 // stack frame was an arguments adapter frame.
339 ArgumentsAccessStub::Type type;
340 if (is_strict(language_mode()) || !is_simple_parameter_list()) {
341 type = ArgumentsAccessStub::NEW_STRICT;
342 } else if (function()->has_duplicate_parameters()) {
343 type = ArgumentsAccessStub::NEW_SLOPPY_SLOW;
345 type = ArgumentsAccessStub::NEW_SLOPPY_FAST;
347 ArgumentsAccessStub stub(isolate(), type);
350 SetVar(arguments, v0, a1, a2);
354 __ CallRuntime(Runtime::kTraceEnter, 0);
357 // Visit the declarations and body unless there is an illegal
359 if (scope()->HasIllegalRedeclaration()) {
360 Comment cmnt(masm_, "[ Declarations");
361 scope()->VisitIllegalRedeclaration(this);
364 PrepareForBailoutForId(BailoutId::FunctionEntry(), NO_REGISTERS);
365 { Comment cmnt(masm_, "[ Declarations");
366 VisitDeclarations(scope()->declarations());
369 { Comment cmnt(masm_, "[ Stack check");
370 PrepareForBailoutForId(BailoutId::Declarations(), NO_REGISTERS);
372 __ LoadRoot(at, Heap::kStackLimitRootIndex);
373 __ Branch(&ok, hs, sp, Operand(at));
374 Handle<Code> stack_check = isolate()->builtins()->StackCheck();
375 PredictableCodeSizeScope predictable(masm_,
376 masm_->CallSize(stack_check, RelocInfo::CODE_TARGET));
377 __ Call(stack_check, RelocInfo::CODE_TARGET);
381 { Comment cmnt(masm_, "[ Body");
382 DCHECK(loop_depth() == 0);
383 VisitStatements(function()->body());
384 DCHECK(loop_depth() == 0);
388 // Always emit a 'return undefined' in case control fell off the end of
390 { Comment cmnt(masm_, "[ return <undefined>;");
391 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
393 EmitReturnSequence();
397 void FullCodeGenerator::ClearAccumulator() {
398 DCHECK(Smi::FromInt(0) == 0);
399 __ mov(v0, zero_reg);
403 void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
404 __ li(a2, Operand(profiling_counter_));
405 __ lw(a3, FieldMemOperand(a2, Cell::kValueOffset));
406 __ Subu(a3, a3, Operand(Smi::FromInt(delta)));
407 __ sw(a3, FieldMemOperand(a2, Cell::kValueOffset));
411 void FullCodeGenerator::EmitProfilingCounterReset() {
412 int reset_value = FLAG_interrupt_budget;
413 if (info_->is_debug()) {
414 // Detect debug break requests as soon as possible.
415 reset_value = FLAG_interrupt_budget >> 4;
417 __ li(a2, Operand(profiling_counter_));
418 __ li(a3, Operand(Smi::FromInt(reset_value)));
419 __ sw(a3, FieldMemOperand(a2, Cell::kValueOffset));
423 void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
424 Label* back_edge_target) {
425 // The generated code is used in Deoptimizer::PatchStackCheckCodeAt so we need
426 // to make sure it is constant. Branch may emit a skip-or-jump sequence
427 // instead of the normal Branch. It seems that the "skip" part of that
428 // sequence is about as long as this Branch would be so it is safe to ignore
430 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
431 Comment cmnt(masm_, "[ Back edge bookkeeping");
433 DCHECK(back_edge_target->is_bound());
434 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
435 int weight = Min(kMaxBackEdgeWeight,
436 Max(1, distance / kCodeSizeMultiplier));
437 EmitProfilingCounterDecrement(weight);
438 __ slt(at, a3, zero_reg);
439 __ beq(at, zero_reg, &ok);
440 // Call will emit a li t9 first, so it is safe to use the delay slot.
441 __ Call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
442 // Record a mapping of this PC offset to the OSR id. This is used to find
443 // the AST id from the unoptimized code in order to use it as a key into
444 // the deoptimization input data found in the optimized code.
445 RecordBackEdge(stmt->OsrEntryId());
446 EmitProfilingCounterReset();
449 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
450 // Record a mapping of the OSR id to this PC. This is used if the OSR
451 // entry becomes the target of a bailout. We don't expect it to be, but
452 // we want it to work if it is.
453 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
457 void FullCodeGenerator::EmitReturnSequence() {
458 Comment cmnt(masm_, "[ Return sequence");
459 if (return_label_.is_bound()) {
460 __ Branch(&return_label_);
462 __ bind(&return_label_);
464 // Push the return value on the stack as the parameter.
465 // Runtime::TraceExit returns its parameter in v0.
467 __ CallRuntime(Runtime::kTraceExit, 1);
469 // Pretend that the exit is a backwards jump to the entry.
471 if (info_->ShouldSelfOptimize()) {
472 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
474 int distance = masm_->pc_offset();
475 weight = Min(kMaxBackEdgeWeight,
476 Max(1, distance / kCodeSizeMultiplier));
478 EmitProfilingCounterDecrement(weight);
480 __ Branch(&ok, ge, a3, Operand(zero_reg));
482 __ Call(isolate()->builtins()->InterruptCheck(),
483 RelocInfo::CODE_TARGET);
485 EmitProfilingCounterReset();
488 // Make sure that the constant pool is not emitted inside of the return
490 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
491 // Here we use masm_-> instead of the __ macro to avoid the code coverage
492 // tool from instrumenting as we rely on the code size here.
493 int32_t arg_count = info_->scope()->num_parameters() + 1;
494 int32_t sp_delta = arg_count * kPointerSize;
495 SetReturnPosition(function());
497 int no_frame_start = masm_->pc_offset();
498 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
499 masm_->Addu(sp, sp, Operand(sp_delta));
501 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
507 void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
508 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
509 codegen()->GetVar(result_register(), var);
510 __ push(result_register());
514 void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
518 void FullCodeGenerator::AccumulatorValueContext::Plug(
519 Heap::RootListIndex index) const {
520 __ LoadRoot(result_register(), index);
524 void FullCodeGenerator::StackValueContext::Plug(
525 Heap::RootListIndex index) const {
526 __ LoadRoot(result_register(), index);
527 __ push(result_register());
531 void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
532 codegen()->PrepareForBailoutBeforeSplit(condition(),
536 if (index == Heap::kUndefinedValueRootIndex ||
537 index == Heap::kNullValueRootIndex ||
538 index == Heap::kFalseValueRootIndex) {
539 if (false_label_ != fall_through_) __ Branch(false_label_);
540 } else if (index == Heap::kTrueValueRootIndex) {
541 if (true_label_ != fall_through_) __ Branch(true_label_);
543 __ LoadRoot(result_register(), index);
544 codegen()->DoTest(this);
549 void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
553 void FullCodeGenerator::AccumulatorValueContext::Plug(
554 Handle<Object> lit) const {
555 __ li(result_register(), Operand(lit));
559 void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
560 // Immediates cannot be pushed directly.
561 __ li(result_register(), Operand(lit));
562 __ push(result_register());
566 void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
567 codegen()->PrepareForBailoutBeforeSplit(condition(),
571 DCHECK(!lit->IsUndetectableObject()); // There are no undetectable literals.
572 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
573 if (false_label_ != fall_through_) __ Branch(false_label_);
574 } else if (lit->IsTrue() || lit->IsJSObject()) {
575 if (true_label_ != fall_through_) __ Branch(true_label_);
576 } else if (lit->IsString()) {
577 if (String::cast(*lit)->length() == 0) {
578 if (false_label_ != fall_through_) __ Branch(false_label_);
580 if (true_label_ != fall_through_) __ Branch(true_label_);
582 } else if (lit->IsSmi()) {
583 if (Smi::cast(*lit)->value() == 0) {
584 if (false_label_ != fall_through_) __ Branch(false_label_);
586 if (true_label_ != fall_through_) __ Branch(true_label_);
589 // For simplicity we always test the accumulator register.
590 __ li(result_register(), Operand(lit));
591 codegen()->DoTest(this);
596 void FullCodeGenerator::EffectContext::DropAndPlug(int count,
597 Register reg) const {
603 void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
605 Register reg) const {
608 __ Move(result_register(), reg);
612 void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
613 Register reg) const {
615 if (count > 1) __ Drop(count - 1);
616 __ sw(reg, MemOperand(sp, 0));
620 void FullCodeGenerator::TestContext::DropAndPlug(int count,
621 Register reg) const {
623 // For simplicity we always test the accumulator register.
625 __ Move(result_register(), reg);
626 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
627 codegen()->DoTest(this);
631 void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
632 Label* materialize_false) const {
633 DCHECK(materialize_true == materialize_false);
634 __ bind(materialize_true);
638 void FullCodeGenerator::AccumulatorValueContext::Plug(
639 Label* materialize_true,
640 Label* materialize_false) const {
642 __ bind(materialize_true);
643 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
645 __ bind(materialize_false);
646 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
651 void FullCodeGenerator::StackValueContext::Plug(
652 Label* materialize_true,
653 Label* materialize_false) const {
655 __ bind(materialize_true);
656 __ LoadRoot(at, Heap::kTrueValueRootIndex);
657 // Push the value as the following branch can clobber at in long branch mode.
660 __ bind(materialize_false);
661 __ LoadRoot(at, Heap::kFalseValueRootIndex);
667 void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
668 Label* materialize_false) const {
669 DCHECK(materialize_true == true_label_);
670 DCHECK(materialize_false == false_label_);
674 void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
675 Heap::RootListIndex value_root_index =
676 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
677 __ LoadRoot(result_register(), value_root_index);
681 void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
682 Heap::RootListIndex value_root_index =
683 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
684 __ LoadRoot(at, value_root_index);
689 void FullCodeGenerator::TestContext::Plug(bool flag) const {
690 codegen()->PrepareForBailoutBeforeSplit(condition(),
695 if (true_label_ != fall_through_) __ Branch(true_label_);
697 if (false_label_ != fall_through_) __ Branch(false_label_);
702 void FullCodeGenerator::DoTest(Expression* condition,
705 Label* fall_through) {
706 __ mov(a0, result_register());
707 Handle<Code> ic = ToBooleanStub::GetUninitialized(isolate());
708 CallIC(ic, condition->test_id());
709 __ mov(at, zero_reg);
710 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
714 void FullCodeGenerator::Split(Condition cc,
719 Label* fall_through) {
720 if (if_false == fall_through) {
721 __ Branch(if_true, cc, lhs, rhs);
722 } else if (if_true == fall_through) {
723 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
725 __ Branch(if_true, cc, lhs, rhs);
731 MemOperand FullCodeGenerator::StackOperand(Variable* var) {
732 DCHECK(var->IsStackAllocated());
733 // Offset is negative because higher indexes are at lower addresses.
734 int offset = -var->index() * kPointerSize;
735 // Adjust by a (parameter or local) base offset.
736 if (var->IsParameter()) {
737 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
739 offset += JavaScriptFrameConstants::kLocal0Offset;
741 return MemOperand(fp, offset);
745 MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
746 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
747 if (var->IsContextSlot()) {
748 int context_chain_length = scope()->ContextChainLength(var->scope());
749 __ LoadContext(scratch, context_chain_length);
750 return ContextOperand(scratch, var->index());
752 return StackOperand(var);
757 void FullCodeGenerator::GetVar(Register dest, Variable* var) {
758 // Use destination as scratch.
759 MemOperand location = VarOperand(var, dest);
760 __ lw(dest, location);
764 void FullCodeGenerator::SetVar(Variable* var,
768 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
769 DCHECK(!scratch0.is(src));
770 DCHECK(!scratch0.is(scratch1));
771 DCHECK(!scratch1.is(src));
772 MemOperand location = VarOperand(var, scratch0);
773 __ sw(src, location);
774 // Emit the write barrier code if the location is in the heap.
775 if (var->IsContextSlot()) {
776 __ RecordWriteContextSlot(scratch0,
786 void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
787 bool should_normalize,
790 // Only prepare for bailouts before splits if we're in a test
791 // context. Otherwise, we let the Visit function deal with the
792 // preparation to avoid preparing with the same AST id twice.
793 if (!context()->IsTest() || !info_->IsOptimizable()) return;
796 if (should_normalize) __ Branch(&skip);
797 PrepareForBailout(expr, TOS_REG);
798 if (should_normalize) {
799 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
800 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
806 void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
807 // The variable in the declaration always resides in the current function
809 DCHECK_EQ(0, scope()->ContextChainLength(variable->scope()));
810 if (generate_debug_code_) {
811 // Check that we're not inside a with or catch context.
812 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
813 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
814 __ Check(ne, kDeclarationInWithContext,
816 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
817 __ Check(ne, kDeclarationInCatchContext,
823 void FullCodeGenerator::VisitVariableDeclaration(
824 VariableDeclaration* declaration) {
825 // If it was not possible to allocate the variable at compile time, we
826 // need to "declare" it at runtime to make sure it actually exists in the
828 VariableProxy* proxy = declaration->proxy();
829 VariableMode mode = declaration->mode();
830 Variable* variable = proxy->var();
831 bool hole_init = mode == LET || mode == CONST || mode == CONST_LEGACY;
832 switch (variable->location()) {
833 case VariableLocation::GLOBAL:
834 case VariableLocation::UNALLOCATED:
835 globals_->Add(variable->name(), zone());
836 globals_->Add(variable->binding_needs_init()
837 ? isolate()->factory()->the_hole_value()
838 : isolate()->factory()->undefined_value(),
842 case VariableLocation::PARAMETER:
843 case VariableLocation::LOCAL:
845 Comment cmnt(masm_, "[ VariableDeclaration");
846 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
847 __ sw(t0, StackOperand(variable));
851 case VariableLocation::CONTEXT:
853 Comment cmnt(masm_, "[ VariableDeclaration");
854 EmitDebugCheckDeclarationContext(variable);
855 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
856 __ sw(at, ContextOperand(cp, variable->index()));
857 // No write barrier since the_hole_value is in old space.
858 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
862 case VariableLocation::LOOKUP: {
863 Comment cmnt(masm_, "[ VariableDeclaration");
864 __ li(a2, Operand(variable->name()));
865 // Declaration nodes are always introduced in one of four modes.
866 DCHECK(IsDeclaredVariableMode(mode));
867 PropertyAttributes attr =
868 IsImmutableVariableMode(mode) ? READ_ONLY : NONE;
869 __ li(a1, Operand(Smi::FromInt(attr)));
870 // Push initial value, if any.
871 // Note: For variables we must not push an initial value (such as
872 // 'undefined') because we may have a (legal) redeclaration and we
873 // must not destroy the current value.
875 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
876 __ Push(cp, a2, a1, a0);
878 DCHECK(Smi::FromInt(0) == 0);
879 __ mov(a0, zero_reg); // Smi::FromInt(0) indicates no initial value.
880 __ Push(cp, a2, a1, a0);
882 __ CallRuntime(Runtime::kDeclareLookupSlot, 4);
889 void FullCodeGenerator::VisitFunctionDeclaration(
890 FunctionDeclaration* declaration) {
891 VariableProxy* proxy = declaration->proxy();
892 Variable* variable = proxy->var();
893 switch (variable->location()) {
894 case VariableLocation::GLOBAL:
895 case VariableLocation::UNALLOCATED: {
896 globals_->Add(variable->name(), zone());
897 Handle<SharedFunctionInfo> function =
898 Compiler::GetSharedFunctionInfo(declaration->fun(), script(), info_);
899 // Check for stack-overflow exception.
900 if (function.is_null()) return SetStackOverflow();
901 globals_->Add(function, zone());
905 case VariableLocation::PARAMETER:
906 case VariableLocation::LOCAL: {
907 Comment cmnt(masm_, "[ FunctionDeclaration");
908 VisitForAccumulatorValue(declaration->fun());
909 __ sw(result_register(), StackOperand(variable));
913 case VariableLocation::CONTEXT: {
914 Comment cmnt(masm_, "[ FunctionDeclaration");
915 EmitDebugCheckDeclarationContext(variable);
916 VisitForAccumulatorValue(declaration->fun());
917 __ sw(result_register(), ContextOperand(cp, variable->index()));
918 int offset = Context::SlotOffset(variable->index());
919 // We know that we have written a function, which is not a smi.
920 __ RecordWriteContextSlot(cp,
928 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
932 case VariableLocation::LOOKUP: {
933 Comment cmnt(masm_, "[ FunctionDeclaration");
934 __ li(a2, Operand(variable->name()));
935 __ li(a1, Operand(Smi::FromInt(NONE)));
937 // Push initial value for function declaration.
938 VisitForStackValue(declaration->fun());
939 __ CallRuntime(Runtime::kDeclareLookupSlot, 4);
946 void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
947 // Call the runtime to declare the globals.
948 // The context is the first argument.
949 __ li(a1, Operand(pairs));
950 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
952 __ CallRuntime(Runtime::kDeclareGlobals, 3);
953 // Return value is ignored.
957 void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
958 // Call the runtime to declare the modules.
959 __ Push(descriptions);
960 __ CallRuntime(Runtime::kDeclareModules, 1);
961 // Return value is ignored.
965 void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
966 Comment cmnt(masm_, "[ SwitchStatement");
967 Breakable nested_statement(this, stmt);
968 SetStatementPosition(stmt);
970 // Keep the switch value on the stack until a case matches.
971 VisitForStackValue(stmt->tag());
972 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
974 ZoneList<CaseClause*>* clauses = stmt->cases();
975 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
977 Label next_test; // Recycled for each test.
978 // Compile all the tests with branches to their bodies.
979 for (int i = 0; i < clauses->length(); i++) {
980 CaseClause* clause = clauses->at(i);
981 clause->body_target()->Unuse();
983 // The default is not a test, but remember it as final fall through.
984 if (clause->is_default()) {
985 default_clause = clause;
989 Comment cmnt(masm_, "[ Case comparison");
993 // Compile the label expression.
994 VisitForAccumulatorValue(clause->label());
995 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
997 // Perform the comparison as if via '==='.
998 __ lw(a1, MemOperand(sp, 0)); // Switch value.
999 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
1000 JumpPatchSite patch_site(masm_);
1001 if (inline_smi_code) {
1004 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
1006 __ Branch(&next_test, ne, a1, Operand(a0));
1007 __ Drop(1); // Switch value is no longer needed.
1008 __ Branch(clause->body_target());
1010 __ bind(&slow_case);
1013 // Record position before stub call for type feedback.
1014 SetExpressionPosition(clause);
1015 Handle<Code> ic = CodeFactory::CompareIC(isolate(), Token::EQ_STRICT,
1016 strength(language_mode())).code();
1017 CallIC(ic, clause->CompareId());
1018 patch_site.EmitPatchInfo();
1022 PrepareForBailout(clause, TOS_REG);
1023 __ LoadRoot(at, Heap::kTrueValueRootIndex);
1024 __ Branch(&next_test, ne, v0, Operand(at));
1026 __ Branch(clause->body_target());
1029 __ Branch(&next_test, ne, v0, Operand(zero_reg));
1030 __ Drop(1); // Switch value is no longer needed.
1031 __ Branch(clause->body_target());
1034 // Discard the test value and jump to the default if present, otherwise to
1035 // the end of the statement.
1036 __ bind(&next_test);
1037 __ Drop(1); // Switch value is no longer needed.
1038 if (default_clause == NULL) {
1039 __ Branch(nested_statement.break_label());
1041 __ Branch(default_clause->body_target());
1044 // Compile all the case bodies.
1045 for (int i = 0; i < clauses->length(); i++) {
1046 Comment cmnt(masm_, "[ Case body");
1047 CaseClause* clause = clauses->at(i);
1048 __ bind(clause->body_target());
1049 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
1050 VisitStatements(clause->statements());
1053 __ bind(nested_statement.break_label());
1054 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1058 void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
1059 Comment cmnt(masm_, "[ ForInStatement");
1060 SetStatementPosition(stmt, SKIP_BREAK);
1062 FeedbackVectorSlot slot = stmt->ForInFeedbackSlot();
1065 ForIn loop_statement(this, stmt);
1066 increment_loop_depth();
1068 // Get the object to enumerate over. If the object is null or undefined, skip
1069 // over the loop. See ECMA-262 version 5, section 12.6.4.
1070 SetExpressionAsStatementPosition(stmt->enumerable());
1071 VisitForAccumulatorValue(stmt->enumerable());
1072 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
1073 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1074 __ Branch(&exit, eq, a0, Operand(at));
1075 Register null_value = t1;
1076 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
1077 __ Branch(&exit, eq, a0, Operand(null_value));
1078 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1080 // Convert the object to a JS object.
1081 Label convert, done_convert;
1082 __ JumpIfSmi(a0, &convert);
1083 __ GetObjectType(a0, a1, a1);
1084 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
1087 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1089 __ bind(&done_convert);
1090 PrepareForBailoutForId(stmt->ToObjectId(), TOS_REG);
1093 // Check for proxies.
1095 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1096 __ GetObjectType(a0, a1, a1);
1097 __ Branch(&call_runtime, le, a1, Operand(LAST_JS_PROXY_TYPE));
1099 // Check cache validity in generated code. This is a fast case for
1100 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1101 // guarantee cache validity, call the runtime system to check cache
1102 // validity or get the property names in a fixed array.
1103 __ CheckEnumCache(null_value, &call_runtime);
1105 // The enum cache is valid. Load the map of the object being
1106 // iterated over and use the cache for the iteration.
1108 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
1109 __ Branch(&use_cache);
1111 // Get the set of properties to enumerate.
1112 __ bind(&call_runtime);
1113 __ push(a0); // Duplicate the enumerable object on the stack.
1114 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1115 PrepareForBailoutForId(stmt->EnumId(), TOS_REG);
1117 // If we got a map from the runtime call, we can do a fast
1118 // modification check. Otherwise, we got a fixed array, and we have
1119 // to do a slow check.
1121 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
1122 __ LoadRoot(at, Heap::kMetaMapRootIndex);
1123 __ Branch(&fixed_array, ne, a2, Operand(at));
1125 // We got a map in register v0. Get the enumeration cache from it.
1126 Label no_descriptors;
1127 __ bind(&use_cache);
1129 __ EnumLength(a1, v0);
1130 __ Branch(&no_descriptors, eq, a1, Operand(Smi::FromInt(0)));
1132 __ LoadInstanceDescriptors(v0, a2);
1133 __ lw(a2, FieldMemOperand(a2, DescriptorArray::kEnumCacheOffset));
1134 __ lw(a2, FieldMemOperand(a2, DescriptorArray::kEnumCacheBridgeCacheOffset));
1136 // Set up the four remaining stack slots.
1137 __ li(a0, Operand(Smi::FromInt(0)));
1138 // Push map, enumeration cache, enumeration cache length (as smi) and zero.
1139 __ Push(v0, a2, a1, a0);
1142 __ bind(&no_descriptors);
1146 // We got a fixed array in register v0. Iterate through that.
1148 __ bind(&fixed_array);
1150 __ li(a1, FeedbackVector());
1151 __ li(a2, Operand(TypeFeedbackVector::MegamorphicSentinel(isolate())));
1152 int vector_index = FeedbackVector()->GetIndex(slot);
1153 __ sw(a2, FieldMemOperand(a1, FixedArray::OffsetOfElementAt(vector_index)));
1155 __ li(a1, Operand(Smi::FromInt(1))); // Smi indicates slow check
1156 __ lw(a2, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1157 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1158 __ GetObjectType(a2, a3, a3);
1159 __ Branch(&non_proxy, gt, a3, Operand(LAST_JS_PROXY_TYPE));
1160 __ li(a1, Operand(Smi::FromInt(0))); // Zero indicates proxy
1161 __ bind(&non_proxy);
1162 __ Push(a1, v0); // Smi and array
1163 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1164 __ li(a0, Operand(Smi::FromInt(0)));
1165 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1167 // Generate code for doing the condition check.
1168 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
1170 SetExpressionAsStatementPosition(stmt->each());
1172 // Load the current count to a0, load the length to a1.
1173 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1174 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
1175 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
1177 // Get the current entry of the array into register a3.
1178 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1179 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1180 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1181 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1182 __ lw(a3, MemOperand(t0)); // Current entry.
1184 // Get the expected map from the stack or a smi in the
1185 // permanent slow case into register a2.
1186 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1188 // Check if the expected map still matches that of the enumerable.
1189 // If not, we may have to filter the key.
1191 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1192 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1193 __ Branch(&update_each, eq, t0, Operand(a2));
1195 // For proxies, no filtering is done.
1196 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1197 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
1198 __ Branch(&update_each, eq, a2, Operand(zero_reg));
1200 // Convert the entry to a string or (smi) 0 if it isn't a property
1201 // any more. If the property has been removed while iterating, we
1203 __ Push(a1, a3); // Enumerable and current entry.
1204 __ CallRuntime(Runtime::kForInFilter, 2);
1205 PrepareForBailoutForId(stmt->FilterId(), TOS_REG);
1206 __ mov(a3, result_register());
1207 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1208 __ Branch(loop_statement.continue_label(), eq, a3, Operand(at));
1210 // Update the 'each' property or variable from the possibly filtered
1211 // entry in register a3.
1212 __ bind(&update_each);
1213 __ mov(result_register(), a3);
1214 // Perform the assignment as if via '='.
1215 { EffectContext context(this);
1216 EmitAssignment(stmt->each(), stmt->EachFeedbackSlot());
1217 PrepareForBailoutForId(stmt->AssignmentId(), NO_REGISTERS);
1220 // Generate code for the body of the loop.
1221 Visit(stmt->body());
1223 // Generate code for the going to the next element by incrementing
1224 // the index (smi) stored on top of the stack.
1225 __ bind(loop_statement.continue_label());
1227 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1230 EmitBackEdgeBookkeeping(stmt, &loop);
1233 // Remove the pointers stored on the stack.
1234 __ bind(loop_statement.break_label());
1237 // Exit and decrement the loop depth.
1238 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1240 decrement_loop_depth();
1244 void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1246 // Use the fast case closure allocation code that allocates in new
1247 // space for nested functions that don't need literals cloning. If
1248 // we're running with the --always-opt or the --prepare-always-opt
1249 // flag, we need to use the runtime function so that the new function
1250 // we are creating here gets a chance to have its code optimized and
1251 // doesn't just get a copy of the existing unoptimized code.
1252 if (!FLAG_always_opt &&
1253 !FLAG_prepare_always_opt &&
1255 scope()->is_function_scope() &&
1256 info->num_literals() == 0) {
1257 FastNewClosureStub stub(isolate(), info->language_mode(), info->kind());
1258 __ li(a2, Operand(info));
1261 __ li(a0, Operand(info));
1262 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1263 : Heap::kFalseValueRootIndex);
1264 __ Push(cp, a0, a1);
1265 __ CallRuntime(Runtime::kNewClosure, 3);
1267 context()->Plug(v0);
1271 void FullCodeGenerator::EmitSetHomeObjectIfNeeded(Expression* initializer,
1273 FeedbackVectorICSlot slot) {
1274 if (NeedsHomeObject(initializer)) {
1275 __ lw(StoreDescriptor::ReceiverRegister(), MemOperand(sp));
1276 __ li(StoreDescriptor::NameRegister(),
1277 Operand(isolate()->factory()->home_object_symbol()));
1278 __ lw(StoreDescriptor::ValueRegister(),
1279 MemOperand(sp, offset * kPointerSize));
1280 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
1286 void FullCodeGenerator::EmitLoadGlobalCheckExtensions(VariableProxy* proxy,
1287 TypeofMode typeof_mode,
1289 Register current = cp;
1295 if (s->num_heap_slots() > 0) {
1296 if (s->calls_sloppy_eval()) {
1297 // Check that extension is NULL.
1298 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1299 __ Branch(slow, ne, temp, Operand(zero_reg));
1301 // Load next context in chain.
1302 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
1303 // Walk the rest of the chain without clobbering cp.
1306 // If no outer scope calls eval, we do not need to check more
1307 // context extensions.
1308 if (!s->outer_scope_calls_sloppy_eval() || s->is_eval_scope()) break;
1309 s = s->outer_scope();
1312 if (s->is_eval_scope()) {
1314 if (!current.is(next)) {
1315 __ Move(next, current);
1318 // Terminate at native context.
1319 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1320 __ LoadRoot(t0, Heap::kNativeContextMapRootIndex);
1321 __ Branch(&fast, eq, temp, Operand(t0));
1322 // Check that extension is NULL.
1323 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1324 __ Branch(slow, ne, temp, Operand(zero_reg));
1325 // Load next context in chain.
1326 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
1331 // All extension objects were empty and it is safe to use a normal global
1333 EmitGlobalVariableLoad(proxy, typeof_mode);
1337 MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1339 DCHECK(var->IsContextSlot());
1340 Register context = cp;
1344 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
1345 if (s->num_heap_slots() > 0) {
1346 if (s->calls_sloppy_eval()) {
1347 // Check that extension is NULL.
1348 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1349 __ Branch(slow, ne, temp, Operand(zero_reg));
1351 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
1352 // Walk the rest of the chain without clobbering cp.
1356 // Check that last extension is NULL.
1357 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1358 __ Branch(slow, ne, temp, Operand(zero_reg));
1360 // This function is used only for loads, not stores, so it's safe to
1361 // return an cp-based operand (the write barrier cannot be allowed to
1362 // destroy the cp register).
1363 return ContextOperand(context, var->index());
1367 void FullCodeGenerator::EmitDynamicLookupFastCase(VariableProxy* proxy,
1368 TypeofMode typeof_mode,
1369 Label* slow, Label* done) {
1370 // Generate fast-case code for variables that might be shadowed by
1371 // eval-introduced variables. Eval is used a lot without
1372 // introducing variables. In those cases, we do not want to
1373 // perform a runtime call for all variables in the scope
1374 // containing the eval.
1375 Variable* var = proxy->var();
1376 if (var->mode() == DYNAMIC_GLOBAL) {
1377 EmitLoadGlobalCheckExtensions(proxy, typeof_mode, slow);
1379 } else if (var->mode() == DYNAMIC_LOCAL) {
1380 Variable* local = var->local_if_not_shadowed();
1381 __ lw(v0, ContextSlotOperandCheckExtensions(local, slow));
1382 if (local->mode() == LET || local->mode() == CONST ||
1383 local->mode() == CONST_LEGACY) {
1384 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1385 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1386 if (local->mode() == CONST_LEGACY) {
1387 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1388 __ Movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
1389 } else { // LET || CONST
1390 __ Branch(done, ne, at, Operand(zero_reg));
1391 __ li(a0, Operand(var->name()));
1393 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1401 void FullCodeGenerator::EmitGlobalVariableLoad(VariableProxy* proxy,
1402 TypeofMode typeof_mode) {
1403 Variable* var = proxy->var();
1404 DCHECK(var->IsUnallocatedOrGlobalSlot() ||
1405 (var->IsLookupSlot() && var->mode() == DYNAMIC_GLOBAL));
1406 if (var->IsGlobalSlot()) {
1407 DCHECK(var->index() > 0);
1408 DCHECK(var->IsStaticGlobalObjectProperty());
1409 // Each var occupies two slots in the context: for reads and writes.
1410 int const slot = var->index();
1411 int const depth = scope()->ContextChainLength(var->scope());
1412 if (depth <= LoadGlobalViaContextStub::kMaximumDepth) {
1413 __ li(LoadGlobalViaContextDescriptor::SlotRegister(), Operand(slot));
1414 __ li(LoadGlobalViaContextDescriptor::NameRegister(), var->name());
1415 LoadGlobalViaContextStub stub(isolate(), depth);
1418 __ Push(Smi::FromInt(slot));
1419 __ Push(var->name());
1420 __ CallRuntime(Runtime::kLoadGlobalViaContext, 2);
1424 __ lw(LoadDescriptor::ReceiverRegister(), GlobalObjectOperand());
1425 __ li(LoadDescriptor::NameRegister(), Operand(var->name()));
1426 __ li(LoadDescriptor::SlotRegister(),
1427 Operand(SmiFromSlot(proxy->VariableFeedbackSlot())));
1428 CallLoadIC(typeof_mode);
1433 void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy,
1434 TypeofMode typeof_mode) {
1435 // Record position before possible IC call.
1436 SetExpressionPosition(proxy);
1437 PrepareForBailoutForId(proxy->BeforeId(), NO_REGISTERS);
1438 Variable* var = proxy->var();
1440 // Three cases: global variables, lookup variables, and all other types of
1442 switch (var->location()) {
1443 case VariableLocation::GLOBAL:
1444 case VariableLocation::UNALLOCATED: {
1445 Comment cmnt(masm_, "[ Global variable");
1446 EmitGlobalVariableLoad(proxy, typeof_mode);
1447 context()->Plug(v0);
1451 case VariableLocation::PARAMETER:
1452 case VariableLocation::LOCAL:
1453 case VariableLocation::CONTEXT: {
1454 DCHECK_EQ(NOT_INSIDE_TYPEOF, typeof_mode);
1455 Comment cmnt(masm_, var->IsContextSlot() ? "[ Context variable"
1456 : "[ Stack variable");
1457 if (var->binding_needs_init()) {
1458 // var->scope() may be NULL when the proxy is located in eval code and
1459 // refers to a potential outside binding. Currently those bindings are
1460 // always looked up dynamically, i.e. in that case
1461 // var->location() == LOOKUP.
1463 DCHECK(var->scope() != NULL);
1465 // Check if the binding really needs an initialization check. The check
1466 // can be skipped in the following situation: we have a LET or CONST
1467 // binding in harmony mode, both the Variable and the VariableProxy have
1468 // the same declaration scope (i.e. they are both in global code, in the
1469 // same function or in the same eval code) and the VariableProxy is in
1470 // the source physically located after the initializer of the variable.
1472 // We cannot skip any initialization checks for CONST in non-harmony
1473 // mode because const variables may be declared but never initialized:
1474 // if (false) { const x; }; var y = x;
1476 // The condition on the declaration scopes is a conservative check for
1477 // nested functions that access a binding and are called before the
1478 // binding is initialized:
1479 // function() { f(); let x = 1; function f() { x = 2; } }
1481 bool skip_init_check;
1482 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1483 skip_init_check = false;
1484 } else if (var->is_this()) {
1485 CHECK(info_->function() != nullptr &&
1486 (info_->function()->kind() & kSubclassConstructor) != 0);
1487 // TODO(dslomov): implement 'this' hole check elimination.
1488 skip_init_check = false;
1490 // Check that we always have valid source position.
1491 DCHECK(var->initializer_position() != RelocInfo::kNoPosition);
1492 DCHECK(proxy->position() != RelocInfo::kNoPosition);
1493 skip_init_check = var->mode() != CONST_LEGACY &&
1494 var->initializer_position() < proxy->position();
1497 if (!skip_init_check) {
1498 // Let and const need a read barrier.
1500 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1501 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1502 if (var->mode() == LET || var->mode() == CONST) {
1503 // Throw a reference error when using an uninitialized let/const
1504 // binding in harmony mode.
1506 __ Branch(&done, ne, at, Operand(zero_reg));
1507 __ li(a0, Operand(var->name()));
1509 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1512 // Uninitalized const bindings outside of harmony mode are unholed.
1513 DCHECK(var->mode() == CONST_LEGACY);
1514 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1515 __ Movz(v0, a0, at); // Conditional move: Undefined if TheHole.
1517 context()->Plug(v0);
1521 context()->Plug(var);
1525 case VariableLocation::LOOKUP: {
1526 Comment cmnt(masm_, "[ Lookup variable");
1528 // Generate code for loading from variables potentially shadowed
1529 // by eval-introduced variables.
1530 EmitDynamicLookupFastCase(proxy, typeof_mode, &slow, &done);
1532 __ li(a1, Operand(var->name()));
1533 __ Push(cp, a1); // Context and name.
1534 Runtime::FunctionId function_id =
1535 typeof_mode == NOT_INSIDE_TYPEOF
1536 ? Runtime::kLoadLookupSlot
1537 : Runtime::kLoadLookupSlotNoReferenceError;
1538 __ CallRuntime(function_id, 2);
1540 context()->Plug(v0);
1546 void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1547 Comment cmnt(masm_, "[ RegExpLiteral");
1549 // Registers will be used as follows:
1550 // t1 = materialized value (RegExp literal)
1551 // t0 = JS function, literals array
1552 // a3 = literal index
1553 // a2 = RegExp pattern
1554 // a1 = RegExp flags
1555 // a0 = RegExp literal clone
1556 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1557 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1558 int literal_offset =
1559 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1560 __ lw(t1, FieldMemOperand(t0, literal_offset));
1561 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1562 __ Branch(&materialized, ne, t1, Operand(at));
1564 // Create regexp literal using runtime function.
1565 // Result will be in v0.
1566 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1567 __ li(a2, Operand(expr->pattern()));
1568 __ li(a1, Operand(expr->flags()));
1569 __ Push(t0, a3, a2, a1);
1570 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1573 __ bind(&materialized);
1574 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1575 Label allocated, runtime_allocate;
1576 __ Allocate(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1579 __ bind(&runtime_allocate);
1580 __ li(a0, Operand(Smi::FromInt(size)));
1582 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1585 __ bind(&allocated);
1587 // After this, registers are used as follows:
1588 // v0: Newly allocated regexp.
1589 // t1: Materialized regexp.
1591 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1592 context()->Plug(v0);
1596 void FullCodeGenerator::EmitAccessor(Expression* expression) {
1597 if (expression == NULL) {
1598 __ LoadRoot(a1, Heap::kNullValueRootIndex);
1601 VisitForStackValue(expression);
1606 void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1607 Comment cmnt(masm_, "[ ObjectLiteral");
1609 Handle<FixedArray> constant_properties = expr->constant_properties();
1610 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1611 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1612 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1613 __ li(a1, Operand(constant_properties));
1614 __ li(a0, Operand(Smi::FromInt(expr->ComputeFlags())));
1615 if (MustCreateObjectLiteralWithRuntime(expr)) {
1616 __ Push(a3, a2, a1, a0);
1617 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1619 FastCloneShallowObjectStub stub(isolate(), expr->properties_count());
1622 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1624 // If result_saved is true the result is on top of the stack. If
1625 // result_saved is false the result is in v0.
1626 bool result_saved = false;
1628 AccessorTable accessor_table(zone());
1629 int property_index = 0;
1630 // store_slot_index points to the vector IC slot for the next store IC used.
1631 // ObjectLiteral::ComputeFeedbackRequirements controls the allocation of slots
1632 // and must be updated if the number of store ICs emitted here changes.
1633 int store_slot_index = 0;
1634 for (; property_index < expr->properties()->length(); property_index++) {
1635 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1636 if (property->is_computed_name()) break;
1637 if (property->IsCompileTimeValue()) continue;
1639 Literal* key = property->key()->AsLiteral();
1640 Expression* value = property->value();
1641 if (!result_saved) {
1642 __ push(v0); // Save result on stack.
1643 result_saved = true;
1645 switch (property->kind()) {
1646 case ObjectLiteral::Property::CONSTANT:
1648 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1649 DCHECK(!CompileTimeValue::IsCompileTimeValue(property->value()));
1651 case ObjectLiteral::Property::COMPUTED:
1652 // It is safe to use [[Put]] here because the boilerplate already
1653 // contains computed properties with an uninitialized value.
1654 if (key->value()->IsInternalizedString()) {
1655 if (property->emit_store()) {
1656 VisitForAccumulatorValue(value);
1657 __ mov(StoreDescriptor::ValueRegister(), result_register());
1658 DCHECK(StoreDescriptor::ValueRegister().is(a0));
1659 __ li(StoreDescriptor::NameRegister(), Operand(key->value()));
1660 __ lw(StoreDescriptor::ReceiverRegister(), MemOperand(sp));
1661 if (FLAG_vector_stores) {
1662 EmitLoadStoreICSlot(expr->GetNthSlot(store_slot_index++));
1665 CallStoreIC(key->LiteralFeedbackId());
1667 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1669 if (NeedsHomeObject(value)) {
1670 __ Move(StoreDescriptor::ReceiverRegister(), v0);
1671 __ li(StoreDescriptor::NameRegister(),
1672 Operand(isolate()->factory()->home_object_symbol()));
1673 __ lw(StoreDescriptor::ValueRegister(), MemOperand(sp));
1674 if (FLAG_vector_stores) {
1675 EmitLoadStoreICSlot(expr->GetNthSlot(store_slot_index++));
1680 VisitForEffect(value);
1684 // Duplicate receiver on stack.
1685 __ lw(a0, MemOperand(sp));
1687 VisitForStackValue(key);
1688 VisitForStackValue(value);
1689 if (property->emit_store()) {
1690 EmitSetHomeObjectIfNeeded(
1691 value, 2, expr->SlotForHomeObject(value, &store_slot_index));
1692 __ li(a0, Operand(Smi::FromInt(SLOPPY))); // PropertyAttributes.
1694 __ CallRuntime(Runtime::kSetProperty, 4);
1699 case ObjectLiteral::Property::PROTOTYPE:
1700 // Duplicate receiver on stack.
1701 __ lw(a0, MemOperand(sp));
1703 VisitForStackValue(value);
1704 DCHECK(property->emit_store());
1705 __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1707 case ObjectLiteral::Property::GETTER:
1708 if (property->emit_store()) {
1709 accessor_table.lookup(key)->second->getter = value;
1712 case ObjectLiteral::Property::SETTER:
1713 if (property->emit_store()) {
1714 accessor_table.lookup(key)->second->setter = value;
1720 // Emit code to define accessors, using only a single call to the runtime for
1721 // each pair of corresponding getters and setters.
1722 for (AccessorTable::Iterator it = accessor_table.begin();
1723 it != accessor_table.end();
1725 __ lw(a0, MemOperand(sp)); // Duplicate receiver.
1727 VisitForStackValue(it->first);
1728 EmitAccessor(it->second->getter);
1729 EmitSetHomeObjectIfNeeded(
1730 it->second->getter, 2,
1731 expr->SlotForHomeObject(it->second->getter, &store_slot_index));
1732 EmitAccessor(it->second->setter);
1733 EmitSetHomeObjectIfNeeded(
1734 it->second->setter, 3,
1735 expr->SlotForHomeObject(it->second->setter, &store_slot_index));
1736 __ li(a0, Operand(Smi::FromInt(NONE)));
1738 __ CallRuntime(Runtime::kDefineAccessorPropertyUnchecked, 5);
1741 // Object literals have two parts. The "static" part on the left contains no
1742 // computed property names, and so we can compute its map ahead of time; see
1743 // runtime.cc::CreateObjectLiteralBoilerplate. The second "dynamic" part
1744 // starts with the first computed property name, and continues with all
1745 // properties to its right. All the code from above initializes the static
1746 // component of the object literal, and arranges for the map of the result to
1747 // reflect the static order in which the keys appear. For the dynamic
1748 // properties, we compile them into a series of "SetOwnProperty" runtime
1749 // calls. This will preserve insertion order.
1750 for (; property_index < expr->properties()->length(); property_index++) {
1751 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1753 Expression* value = property->value();
1754 if (!result_saved) {
1755 __ push(v0); // Save result on the stack
1756 result_saved = true;
1759 __ lw(a0, MemOperand(sp)); // Duplicate receiver.
1762 if (property->kind() == ObjectLiteral::Property::PROTOTYPE) {
1763 DCHECK(!property->is_computed_name());
1764 VisitForStackValue(value);
1765 DCHECK(property->emit_store());
1766 __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1768 EmitPropertyKey(property, expr->GetIdForProperty(property_index));
1769 VisitForStackValue(value);
1770 EmitSetHomeObjectIfNeeded(
1771 value, 2, expr->SlotForHomeObject(value, &store_slot_index));
1773 switch (property->kind()) {
1774 case ObjectLiteral::Property::CONSTANT:
1775 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1776 case ObjectLiteral::Property::COMPUTED:
1777 if (property->emit_store()) {
1778 __ li(a0, Operand(Smi::FromInt(NONE)));
1780 __ CallRuntime(Runtime::kDefineDataPropertyUnchecked, 4);
1786 case ObjectLiteral::Property::PROTOTYPE:
1790 case ObjectLiteral::Property::GETTER:
1791 __ li(a0, Operand(Smi::FromInt(NONE)));
1793 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
1796 case ObjectLiteral::Property::SETTER:
1797 __ li(a0, Operand(Smi::FromInt(NONE)));
1799 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
1805 if (expr->has_function()) {
1806 DCHECK(result_saved);
1807 __ lw(a0, MemOperand(sp));
1809 __ CallRuntime(Runtime::kToFastProperties, 1);
1813 context()->PlugTOS();
1815 context()->Plug(v0);
1818 // Verify that compilation exactly consumed the number of store ic slots that
1819 // the ObjectLiteral node had to offer.
1820 DCHECK(!FLAG_vector_stores || store_slot_index == expr->slot_count());
1824 void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1825 Comment cmnt(masm_, "[ ArrayLiteral");
1827 expr->BuildConstantElements(isolate());
1829 Handle<FixedArray> constant_elements = expr->constant_elements();
1830 bool has_fast_elements =
1831 IsFastObjectElementsKind(expr->constant_elements_kind());
1833 AllocationSiteMode allocation_site_mode = TRACK_ALLOCATION_SITE;
1834 if (has_fast_elements && !FLAG_allocation_site_pretenuring) {
1835 // If the only customer of allocation sites is transitioning, then
1836 // we can turn it off if we don't have anywhere else to transition to.
1837 allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
1840 __ mov(a0, result_register());
1841 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1842 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1843 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1844 __ li(a1, Operand(constant_elements));
1845 if (MustCreateArrayLiteralWithRuntime(expr)) {
1846 __ li(a0, Operand(Smi::FromInt(expr->ComputeFlags())));
1847 __ Push(a3, a2, a1, a0);
1848 __ CallRuntime(Runtime::kCreateArrayLiteral, 4);
1850 FastCloneShallowArrayStub stub(isolate(), allocation_site_mode);
1853 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1855 bool result_saved = false; // Is the result saved to the stack?
1856 ZoneList<Expression*>* subexprs = expr->values();
1857 int length = subexprs->length();
1859 // Emit code to evaluate all the non-constant subexpressions and to store
1860 // them into the newly cloned array.
1861 int array_index = 0;
1862 for (; array_index < length; array_index++) {
1863 Expression* subexpr = subexprs->at(array_index);
1864 if (subexpr->IsSpread()) break;
1866 // If the subexpression is a literal or a simple materialized literal it
1867 // is already set in the cloned array.
1868 if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
1870 if (!result_saved) {
1871 __ push(v0); // array literal
1872 __ Push(Smi::FromInt(expr->literal_index()));
1873 result_saved = true;
1876 VisitForAccumulatorValue(subexpr);
1878 if (has_fast_elements) {
1879 int offset = FixedArray::kHeaderSize + (array_index * kPointerSize);
1880 __ lw(t2, MemOperand(sp, kPointerSize)); // Copy of array literal.
1881 __ lw(a1, FieldMemOperand(t2, JSObject::kElementsOffset));
1882 __ sw(result_register(), FieldMemOperand(a1, offset));
1883 // Update the write barrier for the array store.
1884 __ RecordWriteField(a1, offset, result_register(), a2,
1885 kRAHasBeenSaved, kDontSaveFPRegs,
1886 EMIT_REMEMBERED_SET, INLINE_SMI_CHECK);
1888 __ li(a3, Operand(Smi::FromInt(array_index)));
1889 __ mov(a0, result_register());
1890 StoreArrayLiteralElementStub stub(isolate());
1894 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1897 // In case the array literal contains spread expressions it has two parts. The
1898 // first part is the "static" array which has a literal index is handled
1899 // above. The second part is the part after the first spread expression
1900 // (inclusive) and these elements gets appended to the array. Note that the
1901 // number elements an iterable produces is unknown ahead of time.
1902 if (array_index < length && result_saved) {
1903 __ Pop(); // literal index
1905 result_saved = false;
1907 for (; array_index < length; array_index++) {
1908 Expression* subexpr = subexprs->at(array_index);
1911 if (subexpr->IsSpread()) {
1912 VisitForStackValue(subexpr->AsSpread()->expression());
1913 __ InvokeBuiltin(Builtins::CONCAT_ITERABLE_TO_ARRAY, CALL_FUNCTION);
1915 VisitForStackValue(subexpr);
1916 __ CallRuntime(Runtime::kAppendElement, 2);
1919 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1923 __ Pop(); // literal index
1924 context()->PlugTOS();
1926 context()->Plug(v0);
1931 void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1932 DCHECK(expr->target()->IsValidReferenceExpressionOrThis());
1934 Comment cmnt(masm_, "[ Assignment");
1935 SetExpressionPosition(expr, INSERT_BREAK);
1937 Property* property = expr->target()->AsProperty();
1938 LhsKind assign_type = Property::GetAssignType(property);
1940 // Evaluate LHS expression.
1941 switch (assign_type) {
1943 // Nothing to do here.
1945 case NAMED_PROPERTY:
1946 if (expr->is_compound()) {
1947 // We need the receiver both on the stack and in the register.
1948 VisitForStackValue(property->obj());
1949 __ lw(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
1951 VisitForStackValue(property->obj());
1954 case NAMED_SUPER_PROPERTY:
1956 property->obj()->AsSuperPropertyReference()->this_var());
1957 VisitForAccumulatorValue(
1958 property->obj()->AsSuperPropertyReference()->home_object());
1959 __ Push(result_register());
1960 if (expr->is_compound()) {
1961 const Register scratch = a1;
1962 __ lw(scratch, MemOperand(sp, kPointerSize));
1963 __ Push(scratch, result_register());
1966 case KEYED_SUPER_PROPERTY: {
1967 const Register scratch = a1;
1969 property->obj()->AsSuperPropertyReference()->this_var());
1970 VisitForAccumulatorValue(
1971 property->obj()->AsSuperPropertyReference()->home_object());
1972 __ Move(scratch, result_register());
1973 VisitForAccumulatorValue(property->key());
1974 __ Push(scratch, result_register());
1975 if (expr->is_compound()) {
1976 const Register scratch1 = t0;
1977 __ lw(scratch1, MemOperand(sp, 2 * kPointerSize));
1978 __ Push(scratch1, scratch, result_register());
1982 case KEYED_PROPERTY:
1983 // We need the key and receiver on both the stack and in v0 and a1.
1984 if (expr->is_compound()) {
1985 VisitForStackValue(property->obj());
1986 VisitForStackValue(property->key());
1987 __ lw(LoadDescriptor::ReceiverRegister(),
1988 MemOperand(sp, 1 * kPointerSize));
1989 __ lw(LoadDescriptor::NameRegister(), MemOperand(sp, 0));
1991 VisitForStackValue(property->obj());
1992 VisitForStackValue(property->key());
1997 // For compound assignments we need another deoptimization point after the
1998 // variable/property load.
1999 if (expr->is_compound()) {
2000 { AccumulatorValueContext context(this);
2001 switch (assign_type) {
2003 EmitVariableLoad(expr->target()->AsVariableProxy());
2004 PrepareForBailout(expr->target(), TOS_REG);
2006 case NAMED_PROPERTY:
2007 EmitNamedPropertyLoad(property);
2008 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2010 case NAMED_SUPER_PROPERTY:
2011 EmitNamedSuperPropertyLoad(property);
2012 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2014 case KEYED_SUPER_PROPERTY:
2015 EmitKeyedSuperPropertyLoad(property);
2016 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2018 case KEYED_PROPERTY:
2019 EmitKeyedPropertyLoad(property);
2020 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2025 Token::Value op = expr->binary_op();
2026 __ push(v0); // Left operand goes on the stack.
2027 VisitForAccumulatorValue(expr->value());
2029 AccumulatorValueContext context(this);
2030 if (ShouldInlineSmiCase(op)) {
2031 EmitInlineSmiBinaryOp(expr->binary_operation(),
2036 EmitBinaryOp(expr->binary_operation(), op);
2039 // Deoptimization point in case the binary operation may have side effects.
2040 PrepareForBailout(expr->binary_operation(), TOS_REG);
2042 VisitForAccumulatorValue(expr->value());
2045 SetExpressionPosition(expr);
2048 switch (assign_type) {
2050 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
2051 expr->op(), expr->AssignmentSlot());
2052 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2053 context()->Plug(v0);
2055 case NAMED_PROPERTY:
2056 EmitNamedPropertyAssignment(expr);
2058 case NAMED_SUPER_PROPERTY:
2059 EmitNamedSuperPropertyStore(property);
2060 context()->Plug(v0);
2062 case KEYED_SUPER_PROPERTY:
2063 EmitKeyedSuperPropertyStore(property);
2064 context()->Plug(v0);
2066 case KEYED_PROPERTY:
2067 EmitKeyedPropertyAssignment(expr);
2073 void FullCodeGenerator::VisitYield(Yield* expr) {
2074 Comment cmnt(masm_, "[ Yield");
2075 SetExpressionPosition(expr);
2077 // Evaluate yielded value first; the initial iterator definition depends on
2078 // this. It stays on the stack while we update the iterator.
2079 VisitForStackValue(expr->expression());
2081 switch (expr->yield_kind()) {
2082 case Yield::kSuspend:
2083 // Pop value from top-of-stack slot; box result into result register.
2084 EmitCreateIteratorResult(false);
2085 __ push(result_register());
2087 case Yield::kInitial: {
2088 Label suspend, continuation, post_runtime, resume;
2091 __ bind(&continuation);
2092 __ RecordGeneratorContinuation();
2096 VisitForAccumulatorValue(expr->generator_object());
2097 DCHECK(continuation.pos() > 0 && Smi::IsValid(continuation.pos()));
2098 __ li(a1, Operand(Smi::FromInt(continuation.pos())));
2099 __ sw(a1, FieldMemOperand(v0, JSGeneratorObject::kContinuationOffset));
2100 __ sw(cp, FieldMemOperand(v0, JSGeneratorObject::kContextOffset));
2102 __ RecordWriteField(v0, JSGeneratorObject::kContextOffset, a1, a2,
2103 kRAHasBeenSaved, kDontSaveFPRegs);
2104 __ Addu(a1, fp, Operand(StandardFrameConstants::kExpressionsOffset));
2105 __ Branch(&post_runtime, eq, sp, Operand(a1));
2106 __ push(v0); // generator object
2107 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
2108 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2109 __ bind(&post_runtime);
2110 __ pop(result_register());
2111 EmitReturnSequence();
2114 context()->Plug(result_register());
2118 case Yield::kFinal: {
2119 VisitForAccumulatorValue(expr->generator_object());
2120 __ li(a1, Operand(Smi::FromInt(JSGeneratorObject::kGeneratorClosed)));
2121 __ sw(a1, FieldMemOperand(result_register(),
2122 JSGeneratorObject::kContinuationOffset));
2123 // Pop value from top-of-stack slot, box result into result register.
2124 EmitCreateIteratorResult(true);
2125 EmitUnwindBeforeReturn();
2126 EmitReturnSequence();
2130 case Yield::kDelegating: {
2131 VisitForStackValue(expr->generator_object());
2133 // Initial stack layout is as follows:
2134 // [sp + 1 * kPointerSize] iter
2135 // [sp + 0 * kPointerSize] g
2137 Label l_catch, l_try, l_suspend, l_continuation, l_resume;
2138 Label l_next, l_call;
2139 Register load_receiver = LoadDescriptor::ReceiverRegister();
2140 Register load_name = LoadDescriptor::NameRegister();
2142 // Initial send value is undefined.
2143 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
2146 // catch (e) { receiver = iter; f = 'throw'; arg = e; goto l_call; }
2149 __ LoadRoot(load_name, Heap::kthrow_stringRootIndex); // "throw"
2150 __ lw(a3, MemOperand(sp, 1 * kPointerSize)); // iter
2151 __ Push(load_name, a3, a0); // "throw", iter, except
2154 // try { received = %yield result }
2155 // Shuffle the received result above a try handler and yield it without
2158 __ pop(a0); // result
2159 int handler_index = NewHandlerTableEntry();
2160 EnterTryBlock(handler_index, &l_catch);
2161 const int try_block_size = TryCatch::kElementCount * kPointerSize;
2162 __ push(a0); // result
2165 __ bind(&l_continuation);
2166 __ RecordGeneratorContinuation();
2170 __ bind(&l_suspend);
2171 const int generator_object_depth = kPointerSize + try_block_size;
2172 __ lw(a0, MemOperand(sp, generator_object_depth));
2174 __ Push(Smi::FromInt(handler_index)); // handler-index
2175 DCHECK(l_continuation.pos() > 0 && Smi::IsValid(l_continuation.pos()));
2176 __ li(a1, Operand(Smi::FromInt(l_continuation.pos())));
2177 __ sw(a1, FieldMemOperand(a0, JSGeneratorObject::kContinuationOffset));
2178 __ sw(cp, FieldMemOperand(a0, JSGeneratorObject::kContextOffset));
2180 __ RecordWriteField(a0, JSGeneratorObject::kContextOffset, a1, a2,
2181 kRAHasBeenSaved, kDontSaveFPRegs);
2182 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 2);
2183 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2184 __ pop(v0); // result
2185 EmitReturnSequence();
2187 __ bind(&l_resume); // received in a0
2188 ExitTryBlock(handler_index);
2190 // receiver = iter; f = 'next'; arg = received;
2193 __ LoadRoot(load_name, Heap::knext_stringRootIndex); // "next"
2194 __ lw(a3, MemOperand(sp, 1 * kPointerSize)); // iter
2195 __ Push(load_name, a3, a0); // "next", iter, received
2197 // result = receiver[f](arg);
2199 __ lw(load_receiver, MemOperand(sp, kPointerSize));
2200 __ lw(load_name, MemOperand(sp, 2 * kPointerSize));
2201 __ li(LoadDescriptor::SlotRegister(),
2202 Operand(SmiFromSlot(expr->KeyedLoadFeedbackSlot())));
2203 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), SLOPPY).code();
2204 CallIC(ic, TypeFeedbackId::None());
2207 __ sw(a1, MemOperand(sp, 2 * kPointerSize));
2208 SetCallPosition(expr, 1);
2209 CallFunctionStub stub(isolate(), 1, CALL_AS_METHOD);
2212 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2213 __ Drop(1); // The function is still on the stack; drop it.
2215 // if (!result.done) goto l_try;
2216 __ Move(load_receiver, v0);
2218 __ push(load_receiver); // save result
2219 __ LoadRoot(load_name, Heap::kdone_stringRootIndex); // "done"
2220 __ li(LoadDescriptor::SlotRegister(),
2221 Operand(SmiFromSlot(expr->DoneFeedbackSlot())));
2222 CallLoadIC(NOT_INSIDE_TYPEOF); // v0=result.done
2224 Handle<Code> bool_ic = ToBooleanStub::GetUninitialized(isolate());
2226 __ Branch(&l_try, eq, v0, Operand(zero_reg));
2229 __ pop(load_receiver); // result
2230 __ LoadRoot(load_name, Heap::kvalue_stringRootIndex); // "value"
2231 __ li(LoadDescriptor::SlotRegister(),
2232 Operand(SmiFromSlot(expr->ValueFeedbackSlot())));
2233 CallLoadIC(NOT_INSIDE_TYPEOF); // v0=result.value
2234 context()->DropAndPlug(2, v0); // drop iter and g
2241 void FullCodeGenerator::EmitGeneratorResume(Expression *generator,
2243 JSGeneratorObject::ResumeMode resume_mode) {
2244 // The value stays in a0, and is ultimately read by the resumed generator, as
2245 // if CallRuntime(Runtime::kSuspendJSGeneratorObject) returned it. Or it
2246 // is read to throw the value when the resumed generator is already closed.
2247 // a1 will hold the generator object until the activation has been resumed.
2248 VisitForStackValue(generator);
2249 VisitForAccumulatorValue(value);
2252 // Load suspended function and context.
2253 __ lw(cp, FieldMemOperand(a1, JSGeneratorObject::kContextOffset));
2254 __ lw(t0, FieldMemOperand(a1, JSGeneratorObject::kFunctionOffset));
2256 // Load receiver and store as the first argument.
2257 __ lw(a2, FieldMemOperand(a1, JSGeneratorObject::kReceiverOffset));
2260 // Push holes for the rest of the arguments to the generator function.
2261 __ lw(a3, FieldMemOperand(t0, JSFunction::kSharedFunctionInfoOffset));
2263 FieldMemOperand(a3, SharedFunctionInfo::kFormalParameterCountOffset));
2264 __ LoadRoot(a2, Heap::kTheHoleValueRootIndex);
2265 Label push_argument_holes, push_frame;
2266 __ bind(&push_argument_holes);
2267 __ Subu(a3, a3, Operand(Smi::FromInt(1)));
2268 __ Branch(&push_frame, lt, a3, Operand(zero_reg));
2270 __ jmp(&push_argument_holes);
2272 // Enter a new JavaScript frame, and initialize its slots as they were when
2273 // the generator was suspended.
2274 Label resume_frame, done;
2275 __ bind(&push_frame);
2276 __ Call(&resume_frame);
2278 __ bind(&resume_frame);
2279 // ra = return address.
2280 // fp = caller's frame pointer.
2281 // cp = callee's context,
2282 // t0 = callee's JS function.
2283 __ Push(ra, fp, cp, t0);
2284 // Adjust FP to point to saved FP.
2285 __ Addu(fp, sp, 2 * kPointerSize);
2287 // Load the operand stack size.
2288 __ lw(a3, FieldMemOperand(a1, JSGeneratorObject::kOperandStackOffset));
2289 __ lw(a3, FieldMemOperand(a3, FixedArray::kLengthOffset));
2292 // If we are sending a value and there is no operand stack, we can jump back
2294 if (resume_mode == JSGeneratorObject::NEXT) {
2296 __ Branch(&slow_resume, ne, a3, Operand(zero_reg));
2297 __ lw(a3, FieldMemOperand(t0, JSFunction::kCodeEntryOffset));
2298 __ lw(a2, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
2300 __ Addu(a3, a3, Operand(a2));
2301 __ li(a2, Operand(Smi::FromInt(JSGeneratorObject::kGeneratorExecuting)));
2302 __ sw(a2, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
2304 __ bind(&slow_resume);
2307 // Otherwise, we push holes for the operand stack and call the runtime to fix
2308 // up the stack and the handlers.
2309 Label push_operand_holes, call_resume;
2310 __ bind(&push_operand_holes);
2311 __ Subu(a3, a3, Operand(1));
2312 __ Branch(&call_resume, lt, a3, Operand(zero_reg));
2314 __ Branch(&push_operand_holes);
2315 __ bind(&call_resume);
2316 DCHECK(!result_register().is(a1));
2317 __ Push(a1, result_register());
2318 __ Push(Smi::FromInt(resume_mode));
2319 __ CallRuntime(Runtime::kResumeJSGeneratorObject, 3);
2320 // Not reached: the runtime call returns elsewhere.
2321 __ stop("not-reached");
2324 context()->Plug(result_register());
2328 void FullCodeGenerator::EmitCreateIteratorResult(bool done) {
2332 const int instance_size = 5 * kPointerSize;
2333 DCHECK_EQ(isolate()->native_context()->iterator_result_map()->instance_size(),
2336 __ Allocate(instance_size, v0, a2, a3, &gc_required, TAG_OBJECT);
2339 __ bind(&gc_required);
2340 __ Push(Smi::FromInt(instance_size));
2341 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
2342 __ lw(context_register(),
2343 MemOperand(fp, StandardFrameConstants::kContextOffset));
2345 __ bind(&allocated);
2346 __ lw(a1, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
2347 __ lw(a1, FieldMemOperand(a1, GlobalObject::kNativeContextOffset));
2348 __ lw(a1, ContextOperand(a1, Context::ITERATOR_RESULT_MAP_INDEX));
2350 __ li(a3, Operand(isolate()->factory()->ToBoolean(done)));
2351 __ li(t0, Operand(isolate()->factory()->empty_fixed_array()));
2352 __ sw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2353 __ sw(t0, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2354 __ sw(t0, FieldMemOperand(v0, JSObject::kElementsOffset));
2356 FieldMemOperand(v0, JSGeneratorObject::kResultValuePropertyOffset));
2358 FieldMemOperand(v0, JSGeneratorObject::kResultDonePropertyOffset));
2360 // Only the value field needs a write barrier, as the other values are in the
2362 __ RecordWriteField(v0, JSGeneratorObject::kResultValuePropertyOffset,
2363 a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
2367 void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
2368 SetExpressionPosition(prop);
2369 Literal* key = prop->key()->AsLiteral();
2370 DCHECK(!prop->IsSuperAccess());
2372 __ li(LoadDescriptor::NameRegister(), Operand(key->value()));
2373 __ li(LoadDescriptor::SlotRegister(),
2374 Operand(SmiFromSlot(prop->PropertyFeedbackSlot())));
2375 CallLoadIC(NOT_INSIDE_TYPEOF, language_mode());
2379 void FullCodeGenerator::EmitNamedSuperPropertyLoad(Property* prop) {
2380 // Stack: receiver, home_object.
2381 SetExpressionPosition(prop);
2383 Literal* key = prop->key()->AsLiteral();
2384 DCHECK(!key->value()->IsSmi());
2385 DCHECK(prop->IsSuperAccess());
2387 __ Push(key->value());
2388 __ Push(Smi::FromInt(language_mode()));
2389 __ CallRuntime(Runtime::kLoadFromSuper, 4);
2393 void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
2394 SetExpressionPosition(prop);
2395 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), language_mode()).code();
2396 __ li(LoadDescriptor::SlotRegister(),
2397 Operand(SmiFromSlot(prop->PropertyFeedbackSlot())));
2402 void FullCodeGenerator::EmitKeyedSuperPropertyLoad(Property* prop) {
2403 // Stack: receiver, home_object, key.
2404 SetExpressionPosition(prop);
2405 __ Push(Smi::FromInt(language_mode()));
2406 __ CallRuntime(Runtime::kLoadKeyedFromSuper, 4);
2410 void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
2412 Expression* left_expr,
2413 Expression* right_expr) {
2414 Label done, smi_case, stub_call;
2416 Register scratch1 = a2;
2417 Register scratch2 = a3;
2419 // Get the arguments.
2421 Register right = a0;
2423 __ mov(a0, result_register());
2425 // Perform combined smi check on both operands.
2426 __ Or(scratch1, left, Operand(right));
2427 STATIC_ASSERT(kSmiTag == 0);
2428 JumpPatchSite patch_site(masm_);
2429 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
2431 __ bind(&stub_call);
2433 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2434 CallIC(code, expr->BinaryOperationFeedbackId());
2435 patch_site.EmitPatchInfo();
2439 // Smi case. This code works the same way as the smi-smi case in the type
2440 // recording binary operation stub, see
2443 __ GetLeastBitsFromSmi(scratch1, right, 5);
2444 __ srav(right, left, scratch1);
2445 __ And(v0, right, Operand(~kSmiTagMask));
2448 __ SmiUntag(scratch1, left);
2449 __ GetLeastBitsFromSmi(scratch2, right, 5);
2450 __ sllv(scratch1, scratch1, scratch2);
2451 __ Addu(scratch2, scratch1, Operand(0x40000000));
2452 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
2453 __ SmiTag(v0, scratch1);
2457 __ SmiUntag(scratch1, left);
2458 __ GetLeastBitsFromSmi(scratch2, right, 5);
2459 __ srlv(scratch1, scratch1, scratch2);
2460 __ And(scratch2, scratch1, 0xc0000000);
2461 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
2462 __ SmiTag(v0, scratch1);
2466 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
2467 __ BranchOnOverflow(&stub_call, scratch1);
2470 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
2471 __ BranchOnOverflow(&stub_call, scratch1);
2474 __ SmiUntag(scratch1, right);
2475 __ Mul(scratch2, v0, left, scratch1);
2476 __ sra(scratch1, v0, 31);
2477 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
2478 __ Branch(&done, ne, v0, Operand(zero_reg));
2479 __ Addu(scratch2, right, left);
2480 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
2481 DCHECK(Smi::FromInt(0) == 0);
2482 __ mov(v0, zero_reg);
2486 __ Or(v0, left, Operand(right));
2488 case Token::BIT_AND:
2489 __ And(v0, left, Operand(right));
2491 case Token::BIT_XOR:
2492 __ Xor(v0, left, Operand(right));
2499 context()->Plug(v0);
2503 void FullCodeGenerator::EmitClassDefineProperties(ClassLiteral* lit,
2504 int* used_store_slots) {
2505 // Constructor is in v0.
2506 DCHECK(lit != NULL);
2509 // No access check is needed here since the constructor is created by the
2511 Register scratch = a1;
2513 FieldMemOperand(v0, JSFunction::kPrototypeOrInitialMapOffset));
2516 for (int i = 0; i < lit->properties()->length(); i++) {
2517 ObjectLiteral::Property* property = lit->properties()->at(i);
2518 Expression* value = property->value();
2520 if (property->is_static()) {
2521 __ lw(scratch, MemOperand(sp, kPointerSize)); // constructor
2523 __ lw(scratch, MemOperand(sp, 0)); // prototype
2526 EmitPropertyKey(property, lit->GetIdForProperty(i));
2528 // The static prototype property is read only. We handle the non computed
2529 // property name case in the parser. Since this is the only case where we
2530 // need to check for an own read only property we special case this so we do
2531 // not need to do this for every property.
2532 if (property->is_static() && property->is_computed_name()) {
2533 __ CallRuntime(Runtime::kThrowIfStaticPrototype, 1);
2537 VisitForStackValue(value);
2538 EmitSetHomeObjectIfNeeded(value, 2,
2539 lit->SlotForHomeObject(value, used_store_slots));
2541 switch (property->kind()) {
2542 case ObjectLiteral::Property::CONSTANT:
2543 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2544 case ObjectLiteral::Property::PROTOTYPE:
2546 case ObjectLiteral::Property::COMPUTED:
2547 __ CallRuntime(Runtime::kDefineClassMethod, 3);
2550 case ObjectLiteral::Property::GETTER:
2551 __ li(a0, Operand(Smi::FromInt(DONT_ENUM)));
2553 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
2556 case ObjectLiteral::Property::SETTER:
2557 __ li(a0, Operand(Smi::FromInt(DONT_ENUM)));
2559 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
2568 __ CallRuntime(Runtime::kToFastProperties, 1);
2571 __ CallRuntime(Runtime::kToFastProperties, 1);
2573 if (is_strong(language_mode())) {
2575 FieldMemOperand(v0, JSFunction::kPrototypeOrInitialMapOffset));
2576 __ Push(v0, scratch);
2577 // TODO(conradw): It would be more efficient to define the properties with
2578 // the right attributes the first time round.
2579 // Freeze the prototype.
2580 __ CallRuntime(Runtime::kObjectFreeze, 1);
2581 // Freeze the constructor.
2582 __ CallRuntime(Runtime::kObjectFreeze, 1);
2587 void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr, Token::Value op) {
2588 __ mov(a0, result_register());
2591 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2592 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
2593 CallIC(code, expr->BinaryOperationFeedbackId());
2594 patch_site.EmitPatchInfo();
2595 context()->Plug(v0);
2599 void FullCodeGenerator::EmitAssignment(Expression* expr,
2600 FeedbackVectorICSlot slot) {
2601 DCHECK(expr->IsValidReferenceExpressionOrThis());
2603 Property* prop = expr->AsProperty();
2604 LhsKind assign_type = Property::GetAssignType(prop);
2606 switch (assign_type) {
2608 Variable* var = expr->AsVariableProxy()->var();
2609 EffectContext context(this);
2610 EmitVariableAssignment(var, Token::ASSIGN, slot);
2613 case NAMED_PROPERTY: {
2614 __ push(result_register()); // Preserve value.
2615 VisitForAccumulatorValue(prop->obj());
2616 __ mov(StoreDescriptor::ReceiverRegister(), result_register());
2617 __ pop(StoreDescriptor::ValueRegister()); // Restore value.
2618 __ li(StoreDescriptor::NameRegister(),
2619 Operand(prop->key()->AsLiteral()->value()));
2620 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2624 case NAMED_SUPER_PROPERTY: {
2626 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2627 VisitForAccumulatorValue(
2628 prop->obj()->AsSuperPropertyReference()->home_object());
2629 // stack: value, this; v0: home_object
2630 Register scratch = a2;
2631 Register scratch2 = a3;
2632 __ mov(scratch, result_register()); // home_object
2633 __ lw(v0, MemOperand(sp, kPointerSize)); // value
2634 __ lw(scratch2, MemOperand(sp, 0)); // this
2635 __ sw(scratch2, MemOperand(sp, kPointerSize)); // this
2636 __ sw(scratch, MemOperand(sp, 0)); // home_object
2637 // stack: this, home_object; v0: value
2638 EmitNamedSuperPropertyStore(prop);
2641 case KEYED_SUPER_PROPERTY: {
2643 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2645 prop->obj()->AsSuperPropertyReference()->home_object());
2646 VisitForAccumulatorValue(prop->key());
2647 Register scratch = a2;
2648 Register scratch2 = a3;
2649 __ lw(scratch2, MemOperand(sp, 2 * kPointerSize)); // value
2650 // stack: value, this, home_object; v0: key, a3: value
2651 __ lw(scratch, MemOperand(sp, kPointerSize)); // this
2652 __ sw(scratch, MemOperand(sp, 2 * kPointerSize));
2653 __ lw(scratch, MemOperand(sp, 0)); // home_object
2654 __ sw(scratch, MemOperand(sp, kPointerSize));
2655 __ sw(v0, MemOperand(sp, 0));
2656 __ Move(v0, scratch2);
2657 // stack: this, home_object, key; v0: value.
2658 EmitKeyedSuperPropertyStore(prop);
2661 case KEYED_PROPERTY: {
2662 __ push(result_register()); // Preserve value.
2663 VisitForStackValue(prop->obj());
2664 VisitForAccumulatorValue(prop->key());
2665 __ mov(StoreDescriptor::NameRegister(), result_register());
2666 __ Pop(StoreDescriptor::ValueRegister(),
2667 StoreDescriptor::ReceiverRegister());
2668 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2670 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2675 context()->Plug(v0);
2679 void FullCodeGenerator::EmitStoreToStackLocalOrContextSlot(
2680 Variable* var, MemOperand location) {
2681 __ sw(result_register(), location);
2682 if (var->IsContextSlot()) {
2683 // RecordWrite may destroy all its register arguments.
2684 __ Move(a3, result_register());
2685 int offset = Context::SlotOffset(var->index());
2686 __ RecordWriteContextSlot(
2687 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
2692 void FullCodeGenerator::EmitVariableAssignment(Variable* var, Token::Value op,
2693 FeedbackVectorICSlot slot) {
2694 if (var->IsUnallocated()) {
2695 // Global var, const, or let.
2696 __ mov(StoreDescriptor::ValueRegister(), result_register());
2697 __ li(StoreDescriptor::NameRegister(), Operand(var->name()));
2698 __ lw(StoreDescriptor::ReceiverRegister(), GlobalObjectOperand());
2699 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2702 } else if (var->IsGlobalSlot()) {
2703 // Global var, const, or let.
2704 DCHECK(var->index() > 0);
2705 DCHECK(var->IsStaticGlobalObjectProperty());
2706 DCHECK(StoreGlobalViaContextDescriptor::ValueRegister().is(a0));
2707 __ mov(StoreGlobalViaContextDescriptor::ValueRegister(), result_register());
2708 // Each var occupies two slots in the context: for reads and writes.
2709 int const slot = var->index() + 1;
2710 int const depth = scope()->ContextChainLength(var->scope());
2711 if (depth <= StoreGlobalViaContextStub::kMaximumDepth) {
2712 __ li(StoreGlobalViaContextDescriptor::SlotRegister(), Operand(slot));
2713 __ li(StoreGlobalViaContextDescriptor::NameRegister(), var->name());
2714 StoreGlobalViaContextStub stub(isolate(), depth, language_mode());
2717 __ Push(Smi::FromInt(slot));
2718 __ Push(var->name());
2720 __ CallRuntime(is_strict(language_mode())
2721 ? Runtime::kStoreGlobalViaContext_Strict
2722 : Runtime::kStoreGlobalViaContext_Sloppy,
2726 } else if (var->mode() == LET && op != Token::INIT_LET) {
2727 // Non-initializing assignment to let variable needs a write barrier.
2728 DCHECK(!var->IsLookupSlot());
2729 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2731 MemOperand location = VarOperand(var, a1);
2732 __ lw(a3, location);
2733 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2734 __ Branch(&assign, ne, a3, Operand(t0));
2735 __ li(a3, Operand(var->name()));
2737 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2738 // Perform the assignment.
2740 EmitStoreToStackLocalOrContextSlot(var, location);
2742 } else if (var->mode() == CONST && op != Token::INIT_CONST) {
2743 // Assignment to const variable needs a write barrier.
2744 DCHECK(!var->IsLookupSlot());
2745 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2747 MemOperand location = VarOperand(var, a1);
2748 __ lw(a3, location);
2749 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2750 __ Branch(&const_error, ne, a3, Operand(at));
2751 __ li(a3, Operand(var->name()));
2753 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2754 __ bind(&const_error);
2755 __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2757 } else if (var->is_this() && op == Token::INIT_CONST) {
2758 // Initializing assignment to const {this} needs a write barrier.
2759 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2760 Label uninitialized_this;
2761 MemOperand location = VarOperand(var, a1);
2762 __ lw(a3, location);
2763 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2764 __ Branch(&uninitialized_this, eq, a3, Operand(at));
2765 __ li(a0, Operand(var->name()));
2767 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2768 __ bind(&uninitialized_this);
2769 EmitStoreToStackLocalOrContextSlot(var, location);
2771 } else if (!var->is_const_mode() || op == Token::INIT_CONST) {
2772 if (var->IsLookupSlot()) {
2773 // Assignment to var.
2774 __ li(a1, Operand(var->name()));
2775 __ li(a0, Operand(Smi::FromInt(language_mode())));
2776 __ Push(v0, cp, a1, a0); // Value, context, name, language mode.
2777 __ CallRuntime(Runtime::kStoreLookupSlot, 4);
2779 // Assignment to var or initializing assignment to let/const in harmony
2781 DCHECK((var->IsStackAllocated() || var->IsContextSlot()));
2782 MemOperand location = VarOperand(var, a1);
2783 if (generate_debug_code_ && op == Token::INIT_LET) {
2784 // Check for an uninitialized let binding.
2785 __ lw(a2, location);
2786 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2787 __ Check(eq, kLetBindingReInitialization, a2, Operand(t0));
2789 EmitStoreToStackLocalOrContextSlot(var, location);
2792 } else if (op == Token::INIT_CONST_LEGACY) {
2793 // Const initializers need a write barrier.
2794 DCHECK(!var->IsParameter()); // No const parameters.
2795 if (var->IsLookupSlot()) {
2796 __ li(a0, Operand(var->name()));
2797 __ Push(v0, cp, a0); // Context and name.
2798 __ CallRuntime(Runtime::kInitializeLegacyConstLookupSlot, 3);
2800 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2802 MemOperand location = VarOperand(var, a1);
2803 __ lw(a2, location);
2804 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2805 __ Branch(&skip, ne, a2, Operand(at));
2806 EmitStoreToStackLocalOrContextSlot(var, location);
2811 DCHECK(var->mode() == CONST_LEGACY && op != Token::INIT_CONST_LEGACY);
2812 if (is_strict(language_mode())) {
2813 __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2815 // Silently ignore store in sloppy mode.
2820 void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2821 // Assignment to a property, using a named store IC.
2822 Property* prop = expr->target()->AsProperty();
2823 DCHECK(prop != NULL);
2824 DCHECK(prop->key()->IsLiteral());
2826 __ mov(StoreDescriptor::ValueRegister(), result_register());
2827 __ li(StoreDescriptor::NameRegister(),
2828 Operand(prop->key()->AsLiteral()->value()));
2829 __ pop(StoreDescriptor::ReceiverRegister());
2830 if (FLAG_vector_stores) {
2831 EmitLoadStoreICSlot(expr->AssignmentSlot());
2834 CallStoreIC(expr->AssignmentFeedbackId());
2837 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2838 context()->Plug(v0);
2842 void FullCodeGenerator::EmitNamedSuperPropertyStore(Property* prop) {
2843 // Assignment to named property of super.
2845 // stack : receiver ('this'), home_object
2846 DCHECK(prop != NULL);
2847 Literal* key = prop->key()->AsLiteral();
2848 DCHECK(key != NULL);
2850 __ Push(key->value());
2852 __ CallRuntime((is_strict(language_mode()) ? Runtime::kStoreToSuper_Strict
2853 : Runtime::kStoreToSuper_Sloppy),
2858 void FullCodeGenerator::EmitKeyedSuperPropertyStore(Property* prop) {
2859 // Assignment to named property of super.
2861 // stack : receiver ('this'), home_object, key
2862 DCHECK(prop != NULL);
2866 (is_strict(language_mode()) ? Runtime::kStoreKeyedToSuper_Strict
2867 : Runtime::kStoreKeyedToSuper_Sloppy),
2872 void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2873 // Assignment to a property, using a keyed store IC.
2874 // Call keyed store IC.
2875 // The arguments are:
2876 // - a0 is the value,
2878 // - a2 is the receiver.
2879 __ mov(StoreDescriptor::ValueRegister(), result_register());
2880 __ Pop(StoreDescriptor::ReceiverRegister(), StoreDescriptor::NameRegister());
2881 DCHECK(StoreDescriptor::ValueRegister().is(a0));
2884 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2885 if (FLAG_vector_stores) {
2886 EmitLoadStoreICSlot(expr->AssignmentSlot());
2889 CallIC(ic, expr->AssignmentFeedbackId());
2892 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2893 context()->Plug(v0);
2897 void FullCodeGenerator::VisitProperty(Property* expr) {
2898 Comment cmnt(masm_, "[ Property");
2899 SetExpressionPosition(expr);
2901 Expression* key = expr->key();
2903 if (key->IsPropertyName()) {
2904 if (!expr->IsSuperAccess()) {
2905 VisitForAccumulatorValue(expr->obj());
2906 __ Move(LoadDescriptor::ReceiverRegister(), v0);
2907 EmitNamedPropertyLoad(expr);
2909 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2911 expr->obj()->AsSuperPropertyReference()->home_object());
2912 EmitNamedSuperPropertyLoad(expr);
2915 if (!expr->IsSuperAccess()) {
2916 VisitForStackValue(expr->obj());
2917 VisitForAccumulatorValue(expr->key());
2918 __ Move(LoadDescriptor::NameRegister(), v0);
2919 __ pop(LoadDescriptor::ReceiverRegister());
2920 EmitKeyedPropertyLoad(expr);
2922 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2924 expr->obj()->AsSuperPropertyReference()->home_object());
2925 VisitForStackValue(expr->key());
2926 EmitKeyedSuperPropertyLoad(expr);
2929 PrepareForBailoutForId(expr->LoadId(), TOS_REG);
2930 context()->Plug(v0);
2934 void FullCodeGenerator::CallIC(Handle<Code> code,
2935 TypeFeedbackId id) {
2937 __ Call(code, RelocInfo::CODE_TARGET, id);
2941 // Code common for calls using the IC.
2942 void FullCodeGenerator::EmitCallWithLoadIC(Call* expr) {
2943 Expression* callee = expr->expression();
2945 CallICState::CallType call_type =
2946 callee->IsVariableProxy() ? CallICState::FUNCTION : CallICState::METHOD;
2948 // Get the target function.
2949 if (call_type == CallICState::FUNCTION) {
2950 { StackValueContext context(this);
2951 EmitVariableLoad(callee->AsVariableProxy());
2952 PrepareForBailout(callee, NO_REGISTERS);
2954 // Push undefined as receiver. This is patched in the method prologue if it
2955 // is a sloppy mode method.
2956 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
2959 // Load the function from the receiver.
2960 DCHECK(callee->IsProperty());
2961 DCHECK(!callee->AsProperty()->IsSuperAccess());
2962 __ lw(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
2963 EmitNamedPropertyLoad(callee->AsProperty());
2964 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2965 // Push the target function under the receiver.
2966 __ lw(at, MemOperand(sp, 0));
2968 __ sw(v0, MemOperand(sp, kPointerSize));
2971 EmitCall(expr, call_type);
2975 void FullCodeGenerator::EmitSuperCallWithLoadIC(Call* expr) {
2976 SetExpressionPosition(expr);
2977 Expression* callee = expr->expression();
2978 DCHECK(callee->IsProperty());
2979 Property* prop = callee->AsProperty();
2980 DCHECK(prop->IsSuperAccess());
2982 Literal* key = prop->key()->AsLiteral();
2983 DCHECK(!key->value()->IsSmi());
2984 // Load the function from the receiver.
2985 const Register scratch = a1;
2986 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
2987 VisitForAccumulatorValue(super_ref->home_object());
2988 __ mov(scratch, v0);
2989 VisitForAccumulatorValue(super_ref->this_var());
2990 __ Push(scratch, v0, v0, scratch);
2991 __ Push(key->value());
2992 __ Push(Smi::FromInt(language_mode()));
2996 // - this (receiver)
2997 // - this (receiver) <-- LoadFromSuper will pop here and below.
3001 __ CallRuntime(Runtime::kLoadFromSuper, 4);
3003 // Replace home_object with target function.
3004 __ sw(v0, MemOperand(sp, kPointerSize));
3007 // - target function
3008 // - this (receiver)
3009 EmitCall(expr, CallICState::METHOD);
3013 // Code common for calls using the IC.
3014 void FullCodeGenerator::EmitKeyedCallWithLoadIC(Call* expr,
3017 VisitForAccumulatorValue(key);
3019 Expression* callee = expr->expression();
3021 // Load the function from the receiver.
3022 DCHECK(callee->IsProperty());
3023 __ lw(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
3024 __ Move(LoadDescriptor::NameRegister(), v0);
3025 EmitKeyedPropertyLoad(callee->AsProperty());
3026 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
3028 // Push the target function under the receiver.
3029 __ lw(at, MemOperand(sp, 0));
3031 __ sw(v0, MemOperand(sp, kPointerSize));
3033 EmitCall(expr, CallICState::METHOD);
3037 void FullCodeGenerator::EmitKeyedSuperCallWithLoadIC(Call* expr) {
3038 Expression* callee = expr->expression();
3039 DCHECK(callee->IsProperty());
3040 Property* prop = callee->AsProperty();
3041 DCHECK(prop->IsSuperAccess());
3043 SetExpressionPosition(prop);
3044 // Load the function from the receiver.
3045 const Register scratch = a1;
3046 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
3047 VisitForAccumulatorValue(super_ref->home_object());
3048 __ Move(scratch, v0);
3049 VisitForAccumulatorValue(super_ref->this_var());
3050 __ Push(scratch, v0, v0, scratch);
3051 VisitForStackValue(prop->key());
3052 __ Push(Smi::FromInt(language_mode()));
3056 // - this (receiver)
3057 // - this (receiver) <-- LoadKeyedFromSuper will pop here and below.
3061 __ CallRuntime(Runtime::kLoadKeyedFromSuper, 4);
3063 // Replace home_object with target function.
3064 __ sw(v0, MemOperand(sp, kPointerSize));
3067 // - target function
3068 // - this (receiver)
3069 EmitCall(expr, CallICState::METHOD);
3073 void FullCodeGenerator::EmitCall(Call* expr, CallICState::CallType call_type) {
3074 // Load the arguments.
3075 ZoneList<Expression*>* args = expr->arguments();
3076 int arg_count = args->length();
3077 for (int i = 0; i < arg_count; i++) {
3078 VisitForStackValue(args->at(i));
3081 // Record source position of the IC call.
3082 SetCallPosition(expr, arg_count);
3083 Handle<Code> ic = CodeFactory::CallIC(isolate(), arg_count, call_type).code();
3084 __ li(a3, Operand(SmiFromSlot(expr->CallFeedbackICSlot())));
3085 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
3086 // Don't assign a type feedback id to the IC, since type feedback is provided
3087 // by the vector above.
3090 RecordJSReturnSite(expr);
3091 // Restore context register.
3092 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3093 context()->DropAndPlug(1, v0);
3097 void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
3098 // t3: copy of the first argument or undefined if it doesn't exist.
3099 if (arg_count > 0) {
3100 __ lw(t3, MemOperand(sp, arg_count * kPointerSize));
3102 __ LoadRoot(t3, Heap::kUndefinedValueRootIndex);
3105 // t2: the receiver of the enclosing function.
3106 __ lw(t2, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3108 // t1: the language mode.
3109 __ li(t1, Operand(Smi::FromInt(language_mode())));
3111 // t0: the start position of the scope the calls resides in.
3112 __ li(t0, Operand(Smi::FromInt(scope()->start_position())));
3114 // Do the runtime call.
3115 __ Push(t3, t2, t1, t0);
3116 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
3120 // See http://www.ecma-international.org/ecma-262/6.0/#sec-function-calls.
3121 void FullCodeGenerator::PushCalleeAndWithBaseObject(Call* expr) {
3122 VariableProxy* callee = expr->expression()->AsVariableProxy();
3123 if (callee->var()->IsLookupSlot()) {
3126 SetExpressionPosition(callee);
3127 // Generate code for loading from variables potentially shadowed by
3128 // eval-introduced variables.
3129 EmitDynamicLookupFastCase(callee, NOT_INSIDE_TYPEOF, &slow, &done);
3132 // Call the runtime to find the function to call (returned in v0)
3133 // and the object holding it (returned in v1).
3134 DCHECK(!context_register().is(a2));
3135 __ li(a2, Operand(callee->name()));
3136 __ Push(context_register(), a2);
3137 __ CallRuntime(Runtime::kLoadLookupSlot, 2);
3138 __ Push(v0, v1); // Function, receiver.
3139 PrepareForBailoutForId(expr->LookupId(), NO_REGISTERS);
3141 // If fast case code has been generated, emit code to push the
3142 // function and receiver and have the slow path jump around this
3144 if (done.is_linked()) {
3150 // The receiver is implicitly the global receiver. Indicate this
3151 // by passing the hole to the call function stub.
3152 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
3157 VisitForStackValue(callee);
3158 // refEnv.WithBaseObject()
3159 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
3160 __ push(a2); // Reserved receiver slot.
3165 void FullCodeGenerator::VisitCall(Call* expr) {
3167 // We want to verify that RecordJSReturnSite gets called on all paths
3168 // through this function. Avoid early returns.
3169 expr->return_is_recorded_ = false;
3172 Comment cmnt(masm_, "[ Call");
3173 Expression* callee = expr->expression();
3174 Call::CallType call_type = expr->GetCallType(isolate());
3176 if (call_type == Call::POSSIBLY_EVAL_CALL) {
3177 // In a call to eval, we first call RuntimeHidden_ResolvePossiblyDirectEval
3178 // to resolve the function we need to call. Then we call the resolved
3179 // function using the given arguments.
3180 ZoneList<Expression*>* args = expr->arguments();
3181 int arg_count = args->length();
3182 PushCalleeAndWithBaseObject(expr);
3184 // Push the arguments.
3185 for (int i = 0; i < arg_count; i++) {
3186 VisitForStackValue(args->at(i));
3189 // Push a copy of the function (found below the arguments) and
3191 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
3193 EmitResolvePossiblyDirectEval(arg_count);
3195 // Touch up the stack with the resolved function.
3196 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
3198 PrepareForBailoutForId(expr->EvalId(), NO_REGISTERS);
3199 // Record source position for debugger.
3200 SetCallPosition(expr, arg_count);
3201 CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
3202 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
3204 RecordJSReturnSite(expr);
3205 // Restore context register.
3206 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3207 context()->DropAndPlug(1, v0);
3208 } else if (call_type == Call::GLOBAL_CALL) {
3209 EmitCallWithLoadIC(expr);
3210 } else if (call_type == Call::LOOKUP_SLOT_CALL) {
3211 // Call to a lookup slot (dynamically introduced variable).
3212 PushCalleeAndWithBaseObject(expr);
3214 } else if (call_type == Call::PROPERTY_CALL) {
3215 Property* property = callee->AsProperty();
3216 bool is_named_call = property->key()->IsPropertyName();
3217 if (property->IsSuperAccess()) {
3218 if (is_named_call) {
3219 EmitSuperCallWithLoadIC(expr);
3221 EmitKeyedSuperCallWithLoadIC(expr);
3224 VisitForStackValue(property->obj());
3225 if (is_named_call) {
3226 EmitCallWithLoadIC(expr);
3228 EmitKeyedCallWithLoadIC(expr, property->key());
3231 } else if (call_type == Call::SUPER_CALL) {
3232 EmitSuperConstructorCall(expr);
3234 DCHECK(call_type == Call::OTHER_CALL);
3235 // Call to an arbitrary expression not handled specially above.
3236 VisitForStackValue(callee);
3237 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
3239 // Emit function call.
3244 // RecordJSReturnSite should have been called.
3245 DCHECK(expr->return_is_recorded_);
3250 void FullCodeGenerator::VisitCallNew(CallNew* expr) {
3251 Comment cmnt(masm_, "[ CallNew");
3252 // According to ECMA-262, section 11.2.2, page 44, the function
3253 // expression in new calls must be evaluated before the
3256 // Push constructor on the stack. If it's not a function it's used as
3257 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
3259 DCHECK(!expr->expression()->IsSuperPropertyReference());
3260 VisitForStackValue(expr->expression());
3262 // Push the arguments ("left-to-right") on the stack.
3263 ZoneList<Expression*>* args = expr->arguments();
3264 int arg_count = args->length();
3265 for (int i = 0; i < arg_count; i++) {
3266 VisitForStackValue(args->at(i));
3269 // Call the construct call builtin that handles allocation and
3270 // constructor invocation.
3271 SetConstructCallPosition(expr);
3273 // Load function and argument count into a1 and a0.
3274 __ li(a0, Operand(arg_count));
3275 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
3277 // Record call targets in unoptimized code.
3278 if (FLAG_pretenuring_call_new) {
3279 EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3280 DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3281 expr->CallNewFeedbackSlot().ToInt() + 1);
3284 __ li(a2, FeedbackVector());
3285 __ li(a3, Operand(SmiFromSlot(expr->CallNewFeedbackSlot())));
3287 CallConstructStub stub(isolate(), RECORD_CONSTRUCTOR_TARGET);
3288 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3289 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
3290 context()->Plug(v0);
3294 void FullCodeGenerator::EmitSuperConstructorCall(Call* expr) {
3295 SuperCallReference* super_call_ref =
3296 expr->expression()->AsSuperCallReference();
3297 DCHECK_NOT_NULL(super_call_ref);
3299 EmitLoadSuperConstructor(super_call_ref);
3300 __ push(result_register());
3302 // Push the arguments ("left-to-right") on the stack.
3303 ZoneList<Expression*>* args = expr->arguments();
3304 int arg_count = args->length();
3305 for (int i = 0; i < arg_count; i++) {
3306 VisitForStackValue(args->at(i));
3309 // Call the construct call builtin that handles allocation and
3310 // constructor invocation.
3311 SetConstructCallPosition(expr);
3313 // Load original constructor into t0.
3314 VisitForAccumulatorValue(super_call_ref->new_target_var());
3315 __ mov(t0, result_register());
3317 // Load function and argument count into a1 and a0.
3318 __ li(a0, Operand(arg_count));
3319 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
3321 // Record call targets in unoptimized code.
3322 if (FLAG_pretenuring_call_new) {
3324 /* TODO(dslomov): support pretenuring.
3325 EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3326 DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3327 expr->CallNewFeedbackSlot().ToInt() + 1);
3331 __ li(a2, FeedbackVector());
3332 __ li(a3, Operand(SmiFromSlot(expr->CallFeedbackSlot())));
3334 CallConstructStub stub(isolate(), SUPER_CALL_RECORD_TARGET);
3335 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3337 RecordJSReturnSite(expr);
3339 context()->Plug(v0);
3343 void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
3344 ZoneList<Expression*>* args = expr->arguments();
3345 DCHECK(args->length() == 1);
3347 VisitForAccumulatorValue(args->at(0));
3349 Label materialize_true, materialize_false;
3350 Label* if_true = NULL;
3351 Label* if_false = NULL;
3352 Label* fall_through = NULL;
3353 context()->PrepareTest(&materialize_true, &materialize_false,
3354 &if_true, &if_false, &fall_through);
3356 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3358 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
3360 context()->Plug(if_true, if_false);
3364 void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
3365 ZoneList<Expression*>* args = expr->arguments();
3366 DCHECK(args->length() == 1);
3368 VisitForAccumulatorValue(args->at(0));
3370 Label materialize_true, materialize_false;
3371 Label* if_true = NULL;
3372 Label* if_false = NULL;
3373 Label* fall_through = NULL;
3374 context()->PrepareTest(&materialize_true, &materialize_false,
3375 &if_true, &if_false, &fall_through);
3377 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3378 __ NonNegativeSmiTst(v0, at);
3379 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
3381 context()->Plug(if_true, if_false);
3385 void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
3386 ZoneList<Expression*>* args = expr->arguments();
3387 DCHECK(args->length() == 1);
3389 VisitForAccumulatorValue(args->at(0));
3391 Label materialize_true, materialize_false;
3392 Label* if_true = NULL;
3393 Label* if_false = NULL;
3394 Label* fall_through = NULL;
3395 context()->PrepareTest(&materialize_true, &materialize_false,
3396 &if_true, &if_false, &fall_through);
3398 __ JumpIfSmi(v0, if_false);
3399 __ LoadRoot(at, Heap::kNullValueRootIndex);
3400 __ Branch(if_true, eq, v0, Operand(at));
3401 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
3402 // Undetectable objects behave like undefined when tested with typeof.
3403 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
3404 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
3405 __ Branch(if_false, ne, at, Operand(zero_reg));
3406 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
3407 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
3408 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3409 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
3410 if_true, if_false, fall_through);
3412 context()->Plug(if_true, if_false);
3416 void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
3417 ZoneList<Expression*>* args = expr->arguments();
3418 DCHECK(args->length() == 1);
3420 VisitForAccumulatorValue(args->at(0));
3422 Label materialize_true, materialize_false;
3423 Label* if_true = NULL;
3424 Label* if_false = NULL;
3425 Label* fall_through = NULL;
3426 context()->PrepareTest(&materialize_true, &materialize_false,
3427 &if_true, &if_false, &fall_through);
3429 __ JumpIfSmi(v0, if_false);
3430 __ GetObjectType(v0, a1, a1);
3431 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3432 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
3433 if_true, if_false, fall_through);
3435 context()->Plug(if_true, if_false);
3439 void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
3440 ZoneList<Expression*>* args = expr->arguments();
3441 DCHECK(args->length() == 1);
3443 VisitForAccumulatorValue(args->at(0));
3445 Label materialize_true, materialize_false;
3446 Label* if_true = NULL;
3447 Label* if_false = NULL;
3448 Label* fall_through = NULL;
3449 context()->PrepareTest(&materialize_true, &materialize_false,
3450 &if_true, &if_false, &fall_through);
3452 __ JumpIfSmi(v0, if_false);
3453 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
3454 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
3455 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3456 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
3457 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
3459 context()->Plug(if_true, if_false);
3463 void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
3464 CallRuntime* expr) {
3465 ZoneList<Expression*>* args = expr->arguments();
3466 DCHECK(args->length() == 1);
3468 VisitForAccumulatorValue(args->at(0));
3470 Label materialize_true, materialize_false, skip_lookup;
3471 Label* if_true = NULL;
3472 Label* if_false = NULL;
3473 Label* fall_through = NULL;
3474 context()->PrepareTest(&materialize_true, &materialize_false,
3475 &if_true, &if_false, &fall_through);
3477 __ AssertNotSmi(v0);
3479 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
3480 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
3481 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
3482 __ Branch(&skip_lookup, ne, t0, Operand(zero_reg));
3484 // Check for fast case object. Generate false result for slow case object.
3485 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
3486 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
3487 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
3488 __ Branch(if_false, eq, a2, Operand(t0));
3490 // Look for valueOf name in the descriptor array, and indicate false if
3491 // found. Since we omit an enumeration index check, if it is added via a
3492 // transition that shares its descriptor array, this is a false positive.
3493 Label entry, loop, done;
3495 // Skip loop if no descriptors are valid.
3496 __ NumberOfOwnDescriptors(a3, a1);
3497 __ Branch(&done, eq, a3, Operand(zero_reg));
3499 __ LoadInstanceDescriptors(a1, t0);
3500 // t0: descriptor array.
3501 // a3: valid entries in the descriptor array.
3502 STATIC_ASSERT(kSmiTag == 0);
3503 STATIC_ASSERT(kSmiTagSize == 1);
3504 STATIC_ASSERT(kPointerSize == 4);
3505 __ li(at, Operand(DescriptorArray::kDescriptorSize));
3507 // Calculate location of the first key name.
3508 __ Addu(t0, t0, Operand(DescriptorArray::kFirstOffset - kHeapObjectTag));
3509 // Calculate the end of the descriptor array.
3511 __ sll(t1, a3, kPointerSizeLog2);
3512 __ Addu(a2, a2, t1);
3514 // Loop through all the keys in the descriptor array. If one of these is the
3515 // string "valueOf" the result is false.
3516 // The use of t2 to store the valueOf string assumes that it is not otherwise
3517 // used in the loop below.
3518 __ li(t2, Operand(isolate()->factory()->value_of_string()));
3521 __ lw(a3, MemOperand(t0, 0));
3522 __ Branch(if_false, eq, a3, Operand(t2));
3523 __ Addu(t0, t0, Operand(DescriptorArray::kDescriptorSize * kPointerSize));
3525 __ Branch(&loop, ne, t0, Operand(a2));
3529 // Set the bit in the map to indicate that there is no local valueOf field.
3530 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
3531 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
3532 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
3534 __ bind(&skip_lookup);
3536 // If a valueOf property is not found on the object check that its
3537 // prototype is the un-modified String prototype. If not result is false.
3538 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
3539 __ JumpIfSmi(a2, if_false);
3540 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
3541 __ lw(a3, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
3542 __ lw(a3, FieldMemOperand(a3, GlobalObject::kNativeContextOffset));
3543 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
3544 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3545 Split(eq, a2, Operand(a3), if_true, if_false, fall_through);
3547 context()->Plug(if_true, if_false);
3551 void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
3552 ZoneList<Expression*>* args = expr->arguments();
3553 DCHECK(args->length() == 1);
3555 VisitForAccumulatorValue(args->at(0));
3557 Label materialize_true, materialize_false;
3558 Label* if_true = NULL;
3559 Label* if_false = NULL;
3560 Label* fall_through = NULL;
3561 context()->PrepareTest(&materialize_true, &materialize_false,
3562 &if_true, &if_false, &fall_through);
3564 __ JumpIfSmi(v0, if_false);
3565 __ GetObjectType(v0, a1, a2);
3566 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3567 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
3568 __ Branch(if_false);
3570 context()->Plug(if_true, if_false);
3574 void FullCodeGenerator::EmitIsMinusZero(CallRuntime* expr) {
3575 ZoneList<Expression*>* args = expr->arguments();
3576 DCHECK(args->length() == 1);
3578 VisitForAccumulatorValue(args->at(0));
3580 Label materialize_true, materialize_false;
3581 Label* if_true = NULL;
3582 Label* if_false = NULL;
3583 Label* fall_through = NULL;
3584 context()->PrepareTest(&materialize_true, &materialize_false,
3585 &if_true, &if_false, &fall_through);
3587 __ CheckMap(v0, a1, Heap::kHeapNumberMapRootIndex, if_false, DO_SMI_CHECK);
3588 __ lw(a2, FieldMemOperand(v0, HeapNumber::kExponentOffset));
3589 __ lw(a1, FieldMemOperand(v0, HeapNumber::kMantissaOffset));
3590 __ li(t0, 0x80000000);
3592 __ Branch(¬_nan, ne, a2, Operand(t0));
3593 __ mov(t0, zero_reg);
3597 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3598 Split(eq, a2, Operand(t0), if_true, if_false, fall_through);
3600 context()->Plug(if_true, if_false);
3604 void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
3605 ZoneList<Expression*>* args = expr->arguments();
3606 DCHECK(args->length() == 1);
3608 VisitForAccumulatorValue(args->at(0));
3610 Label materialize_true, materialize_false;
3611 Label* if_true = NULL;
3612 Label* if_false = NULL;
3613 Label* fall_through = NULL;
3614 context()->PrepareTest(&materialize_true, &materialize_false,
3615 &if_true, &if_false, &fall_through);
3617 __ JumpIfSmi(v0, if_false);
3618 __ GetObjectType(v0, a1, a1);
3619 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3620 Split(eq, a1, Operand(JS_ARRAY_TYPE),
3621 if_true, if_false, fall_through);
3623 context()->Plug(if_true, if_false);
3627 void FullCodeGenerator::EmitIsTypedArray(CallRuntime* expr) {
3628 ZoneList<Expression*>* args = expr->arguments();
3629 DCHECK(args->length() == 1);
3631 VisitForAccumulatorValue(args->at(0));
3633 Label materialize_true, materialize_false;
3634 Label* if_true = NULL;
3635 Label* if_false = NULL;
3636 Label* fall_through = NULL;
3637 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3638 &if_false, &fall_through);
3640 __ JumpIfSmi(v0, if_false);
3641 __ GetObjectType(v0, a1, a1);
3642 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3643 Split(eq, a1, Operand(JS_TYPED_ARRAY_TYPE), if_true, if_false, fall_through);
3645 context()->Plug(if_true, if_false);
3649 void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
3650 ZoneList<Expression*>* args = expr->arguments();
3651 DCHECK(args->length() == 1);
3653 VisitForAccumulatorValue(args->at(0));
3655 Label materialize_true, materialize_false;
3656 Label* if_true = NULL;
3657 Label* if_false = NULL;
3658 Label* fall_through = NULL;
3659 context()->PrepareTest(&materialize_true, &materialize_false,
3660 &if_true, &if_false, &fall_through);
3662 __ JumpIfSmi(v0, if_false);
3663 __ GetObjectType(v0, a1, a1);
3664 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3665 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
3667 context()->Plug(if_true, if_false);
3671 void FullCodeGenerator::EmitIsJSProxy(CallRuntime* expr) {
3672 ZoneList<Expression*>* args = expr->arguments();
3673 DCHECK(args->length() == 1);
3675 VisitForAccumulatorValue(args->at(0));
3677 Label materialize_true, materialize_false;
3678 Label* if_true = NULL;
3679 Label* if_false = NULL;
3680 Label* fall_through = NULL;
3681 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3682 &if_false, &fall_through);
3684 __ JumpIfSmi(v0, if_false);
3686 Register type_reg = a2;
3687 __ GetObjectType(v0, map, type_reg);
3688 __ Subu(type_reg, type_reg, Operand(FIRST_JS_PROXY_TYPE));
3689 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3690 Split(ls, type_reg, Operand(LAST_JS_PROXY_TYPE - FIRST_JS_PROXY_TYPE),
3691 if_true, if_false, fall_through);
3693 context()->Plug(if_true, if_false);
3697 void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
3698 DCHECK(expr->arguments()->length() == 0);
3700 Label materialize_true, materialize_false;
3701 Label* if_true = NULL;
3702 Label* if_false = NULL;
3703 Label* fall_through = NULL;
3704 context()->PrepareTest(&materialize_true, &materialize_false,
3705 &if_true, &if_false, &fall_through);
3707 // Get the frame pointer for the calling frame.
3708 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3710 // Skip the arguments adaptor frame if it exists.
3711 Label check_frame_marker;
3712 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
3713 __ Branch(&check_frame_marker, ne,
3714 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3715 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
3717 // Check the marker in the calling frame.
3718 __ bind(&check_frame_marker);
3719 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
3720 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3721 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
3722 if_true, if_false, fall_through);
3724 context()->Plug(if_true, if_false);
3728 void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
3729 ZoneList<Expression*>* args = expr->arguments();
3730 DCHECK(args->length() == 2);
3732 // Load the two objects into registers and perform the comparison.
3733 VisitForStackValue(args->at(0));
3734 VisitForAccumulatorValue(args->at(1));
3736 Label materialize_true, materialize_false;
3737 Label* if_true = NULL;
3738 Label* if_false = NULL;
3739 Label* fall_through = NULL;
3740 context()->PrepareTest(&materialize_true, &materialize_false,
3741 &if_true, &if_false, &fall_through);
3744 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3745 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
3747 context()->Plug(if_true, if_false);
3751 void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
3752 ZoneList<Expression*>* args = expr->arguments();
3753 DCHECK(args->length() == 1);
3755 // ArgumentsAccessStub expects the key in a1 and the formal
3756 // parameter count in a0.
3757 VisitForAccumulatorValue(args->at(0));
3759 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
3760 ArgumentsAccessStub stub(isolate(), ArgumentsAccessStub::READ_ELEMENT);
3762 context()->Plug(v0);
3766 void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
3767 DCHECK(expr->arguments()->length() == 0);
3769 // Get the number of formal parameters.
3770 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
3772 // Check if the calling frame is an arguments adaptor frame.
3773 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3774 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
3775 __ Branch(&exit, ne, a3,
3776 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3778 // Arguments adaptor case: Read the arguments length from the
3780 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3783 context()->Plug(v0);
3787 void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
3788 ZoneList<Expression*>* args = expr->arguments();
3789 DCHECK(args->length() == 1);
3790 Label done, null, function, non_function_constructor;
3792 VisitForAccumulatorValue(args->at(0));
3794 // If the object is a smi, we return null.
3795 __ JumpIfSmi(v0, &null);
3797 // Check that the object is a JS object but take special care of JS
3798 // functions to make sure they have 'Function' as their class.
3799 // Assume that there are only two callable types, and one of them is at
3800 // either end of the type range for JS object types. Saves extra comparisons.
3801 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
3802 __ GetObjectType(v0, v0, a1); // Map is now in v0.
3803 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
3805 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3806 FIRST_SPEC_OBJECT_TYPE + 1);
3807 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
3809 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3810 LAST_SPEC_OBJECT_TYPE - 1);
3811 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
3812 // Assume that there is no larger type.
3813 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
3815 // Check if the constructor in the map is a JS function.
3816 Register instance_type = a2;
3817 __ GetMapConstructor(v0, v0, a1, instance_type);
3818 __ Branch(&non_function_constructor, ne, instance_type,
3819 Operand(JS_FUNCTION_TYPE));
3821 // v0 now contains the constructor function. Grab the
3822 // instance class name from there.
3823 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
3824 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
3827 // Functions have class 'Function'.
3829 __ LoadRoot(v0, Heap::kFunction_stringRootIndex);
3832 // Objects with a non-function constructor have class 'Object'.
3833 __ bind(&non_function_constructor);
3834 __ LoadRoot(v0, Heap::kObject_stringRootIndex);
3837 // Non-JS objects have class null.
3839 __ LoadRoot(v0, Heap::kNullValueRootIndex);
3844 context()->Plug(v0);
3848 void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3849 ZoneList<Expression*>* args = expr->arguments();
3850 DCHECK(args->length() == 1);
3852 VisitForAccumulatorValue(args->at(0)); // Load the object.
3855 // If the object is a smi return the object.
3856 __ JumpIfSmi(v0, &done);
3857 // If the object is not a value type, return the object.
3858 __ GetObjectType(v0, a1, a1);
3859 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
3861 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
3864 context()->Plug(v0);
3868 void FullCodeGenerator::EmitIsDate(CallRuntime* expr) {
3869 ZoneList<Expression*>* args = expr->arguments();
3870 DCHECK_EQ(1, args->length());
3872 VisitForAccumulatorValue(args->at(0));
3874 Label materialize_true, materialize_false;
3875 Label* if_true = nullptr;
3876 Label* if_false = nullptr;
3877 Label* fall_through = nullptr;
3878 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3879 &if_false, &fall_through);
3881 __ JumpIfSmi(v0, if_false);
3882 __ GetObjectType(v0, a1, a1);
3883 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3884 Split(eq, a1, Operand(JS_DATE_TYPE), if_true, if_false, fall_through);
3886 context()->Plug(if_true, if_false);
3890 void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3891 ZoneList<Expression*>* args = expr->arguments();
3892 DCHECK(args->length() == 2);
3893 DCHECK_NOT_NULL(args->at(1)->AsLiteral());
3894 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->value()));
3896 VisitForAccumulatorValue(args->at(0)); // Load the object.
3898 Register object = v0;
3899 Register result = v0;
3900 Register scratch0 = t5;
3901 Register scratch1 = a1;
3903 if (index->value() == 0) {
3904 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset));
3906 Label runtime, done;
3907 if (index->value() < JSDate::kFirstUncachedField) {
3908 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3909 __ li(scratch1, Operand(stamp));
3910 __ lw(scratch1, MemOperand(scratch1));
3911 __ lw(scratch0, FieldMemOperand(object, JSDate::kCacheStampOffset));
3912 __ Branch(&runtime, ne, scratch1, Operand(scratch0));
3913 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset +
3914 kPointerSize * index->value()));
3918 __ PrepareCallCFunction(2, scratch1);
3919 __ li(a1, Operand(index));
3920 __ Move(a0, object);
3921 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
3925 context()->Plug(result);
3929 void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
3930 ZoneList<Expression*>* args = expr->arguments();
3931 DCHECK_EQ(3, args->length());
3933 Register string = v0;
3934 Register index = a1;
3935 Register value = a2;
3937 VisitForStackValue(args->at(0)); // index
3938 VisitForStackValue(args->at(1)); // value
3939 VisitForAccumulatorValue(args->at(2)); // string
3940 __ Pop(index, value);
3942 if (FLAG_debug_code) {
3943 __ SmiTst(value, at);
3944 __ Check(eq, kNonSmiValue, at, Operand(zero_reg));
3945 __ SmiTst(index, at);
3946 __ Check(eq, kNonSmiIndex, at, Operand(zero_reg));
3947 __ SmiUntag(index, index);
3948 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
3949 Register scratch = t5;
3950 __ EmitSeqStringSetCharCheck(
3951 string, index, value, scratch, one_byte_seq_type);
3952 __ SmiTag(index, index);
3955 __ SmiUntag(value, value);
3958 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
3960 __ Addu(at, at, index);
3961 __ sb(value, MemOperand(at));
3962 context()->Plug(string);
3966 void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
3967 ZoneList<Expression*>* args = expr->arguments();
3968 DCHECK_EQ(3, args->length());
3970 Register string = v0;
3971 Register index = a1;
3972 Register value = a2;
3974 VisitForStackValue(args->at(0)); // index
3975 VisitForStackValue(args->at(1)); // value
3976 VisitForAccumulatorValue(args->at(2)); // string
3977 __ Pop(index, value);
3979 if (FLAG_debug_code) {
3980 __ SmiTst(value, at);
3981 __ Check(eq, kNonSmiValue, at, Operand(zero_reg));
3982 __ SmiTst(index, at);
3983 __ Check(eq, kNonSmiIndex, at, Operand(zero_reg));
3984 __ SmiUntag(index, index);
3985 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
3986 Register scratch = t5;
3987 __ EmitSeqStringSetCharCheck(
3988 string, index, value, scratch, two_byte_seq_type);
3989 __ SmiTag(index, index);
3992 __ SmiUntag(value, value);
3995 Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3996 __ Addu(at, at, index);
3997 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
3998 __ sh(value, MemOperand(at));
3999 context()->Plug(string);
4003 void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
4004 ZoneList<Expression*>* args = expr->arguments();
4005 DCHECK(args->length() == 2);
4007 VisitForStackValue(args->at(0)); // Load the object.
4008 VisitForAccumulatorValue(args->at(1)); // Load the value.
4009 __ pop(a1); // v0 = value. a1 = object.
4012 // If the object is a smi, return the value.
4013 __ JumpIfSmi(a1, &done);
4015 // If the object is not a value type, return the value.
4016 __ GetObjectType(a1, a2, a2);
4017 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
4020 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
4021 // Update the write barrier. Save the value as it will be
4022 // overwritten by the write barrier code and is needed afterward.
4024 __ RecordWriteField(
4025 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
4028 context()->Plug(v0);
4032 void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
4033 ZoneList<Expression*>* args = expr->arguments();
4034 DCHECK_EQ(args->length(), 1);
4036 // Load the argument into a0 and call the stub.
4037 VisitForAccumulatorValue(args->at(0));
4038 __ mov(a0, result_register());
4040 NumberToStringStub stub(isolate());
4042 context()->Plug(v0);
4046 void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
4047 ZoneList<Expression*>* args = expr->arguments();
4048 DCHECK(args->length() == 1);
4050 VisitForAccumulatorValue(args->at(0));
4053 StringCharFromCodeGenerator generator(v0, a1);
4054 generator.GenerateFast(masm_);
4057 NopRuntimeCallHelper call_helper;
4058 generator.GenerateSlow(masm_, call_helper);
4061 context()->Plug(a1);
4065 void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
4066 ZoneList<Expression*>* args = expr->arguments();
4067 DCHECK(args->length() == 2);
4069 VisitForStackValue(args->at(0));
4070 VisitForAccumulatorValue(args->at(1));
4071 __ mov(a0, result_register());
4073 Register object = a1;
4074 Register index = a0;
4075 Register result = v0;
4079 Label need_conversion;
4080 Label index_out_of_range;
4082 StringCharCodeAtGenerator generator(object,
4087 &index_out_of_range,
4088 STRING_INDEX_IS_NUMBER);
4089 generator.GenerateFast(masm_);
4092 __ bind(&index_out_of_range);
4093 // When the index is out of range, the spec requires us to return
4095 __ LoadRoot(result, Heap::kNanValueRootIndex);
4098 __ bind(&need_conversion);
4099 // Load the undefined value into the result register, which will
4100 // trigger conversion.
4101 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
4104 NopRuntimeCallHelper call_helper;
4105 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
4108 context()->Plug(result);
4112 void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
4113 ZoneList<Expression*>* args = expr->arguments();
4114 DCHECK(args->length() == 2);
4116 VisitForStackValue(args->at(0));
4117 VisitForAccumulatorValue(args->at(1));
4118 __ mov(a0, result_register());
4120 Register object = a1;
4121 Register index = a0;
4122 Register scratch = a3;
4123 Register result = v0;
4127 Label need_conversion;
4128 Label index_out_of_range;
4130 StringCharAtGenerator generator(object,
4136 &index_out_of_range,
4137 STRING_INDEX_IS_NUMBER);
4138 generator.GenerateFast(masm_);
4141 __ bind(&index_out_of_range);
4142 // When the index is out of range, the spec requires us to return
4143 // the empty string.
4144 __ LoadRoot(result, Heap::kempty_stringRootIndex);
4147 __ bind(&need_conversion);
4148 // Move smi zero into the result register, which will trigger
4150 __ li(result, Operand(Smi::FromInt(0)));
4153 NopRuntimeCallHelper call_helper;
4154 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
4157 context()->Plug(result);
4161 void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
4162 ZoneList<Expression*>* args = expr->arguments();
4163 DCHECK_EQ(2, args->length());
4164 VisitForStackValue(args->at(0));
4165 VisitForAccumulatorValue(args->at(1));
4168 __ mov(a0, result_register()); // StringAddStub requires args in a0, a1.
4169 StringAddStub stub(isolate(), STRING_ADD_CHECK_BOTH, NOT_TENURED);
4171 context()->Plug(v0);
4175 void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
4176 ZoneList<Expression*>* args = expr->arguments();
4177 DCHECK(args->length() >= 2);
4179 int arg_count = args->length() - 2; // 2 ~ receiver and function.
4180 for (int i = 0; i < arg_count + 1; i++) {
4181 VisitForStackValue(args->at(i));
4183 VisitForAccumulatorValue(args->last()); // Function.
4185 Label runtime, done;
4186 // Check for non-function argument (including proxy).
4187 __ JumpIfSmi(v0, &runtime);
4188 __ GetObjectType(v0, a1, a1);
4189 __ Branch(&runtime, ne, a1, Operand(JS_FUNCTION_TYPE));
4191 // InvokeFunction requires the function in a1. Move it in there.
4192 __ mov(a1, result_register());
4193 ParameterCount count(arg_count);
4194 __ InvokeFunction(a1, count, CALL_FUNCTION, NullCallWrapper());
4195 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4200 __ CallRuntime(Runtime::kCall, args->length());
4203 context()->Plug(v0);
4207 void FullCodeGenerator::EmitDefaultConstructorCallSuper(CallRuntime* expr) {
4208 ZoneList<Expression*>* args = expr->arguments();
4209 DCHECK(args->length() == 2);
4212 VisitForStackValue(args->at(0));
4215 VisitForStackValue(args->at(1));
4216 __ CallRuntime(Runtime::kGetPrototype, 1);
4217 __ Push(result_register());
4219 // Load original constructor into t0.
4220 __ lw(t0, MemOperand(sp, 1 * kPointerSize));
4222 // Check if the calling frame is an arguments adaptor frame.
4223 Label adaptor_frame, args_set_up, runtime;
4224 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4225 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
4226 __ Branch(&adaptor_frame, eq, a3,
4227 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4228 // default constructor has no arguments, so no adaptor frame means no args.
4229 __ mov(a0, zero_reg);
4230 __ Branch(&args_set_up);
4232 // Copy arguments from adaptor frame.
4234 __ bind(&adaptor_frame);
4235 __ lw(a1, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4236 __ SmiUntag(a1, a1);
4240 // Get arguments pointer in a2.
4241 __ sll(at, a1, kPointerSizeLog2);
4242 __ addu(a2, a2, at);
4243 __ Addu(a2, a2, Operand(StandardFrameConstants::kCallerSPOffset));
4246 // Pre-decrement a2 with kPointerSize on each iteration.
4247 // Pre-decrement in order to skip receiver.
4248 __ Addu(a2, a2, Operand(-kPointerSize));
4249 __ lw(a3, MemOperand(a2));
4251 __ Addu(a1, a1, Operand(-1));
4252 __ Branch(&loop, ne, a1, Operand(zero_reg));
4255 __ bind(&args_set_up);
4256 __ sll(at, a0, kPointerSizeLog2);
4257 __ Addu(at, at, Operand(sp));
4258 __ lw(a1, MemOperand(at, 0));
4259 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4261 CallConstructStub stub(isolate(), SUPER_CONSTRUCTOR_CALL);
4262 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
4266 context()->Plug(result_register());
4270 void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
4271 RegExpConstructResultStub stub(isolate());
4272 ZoneList<Expression*>* args = expr->arguments();
4273 DCHECK(args->length() == 3);
4274 VisitForStackValue(args->at(0));
4275 VisitForStackValue(args->at(1));
4276 VisitForAccumulatorValue(args->at(2));
4277 __ mov(a0, result_register());
4281 context()->Plug(v0);
4285 void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
4286 ZoneList<Expression*>* args = expr->arguments();
4287 DCHECK_EQ(2, args->length());
4289 DCHECK_NOT_NULL(args->at(0)->AsLiteral());
4290 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->value()))->value();
4292 Handle<FixedArray> jsfunction_result_caches(
4293 isolate()->native_context()->jsfunction_result_caches());
4294 if (jsfunction_result_caches->length() <= cache_id) {
4295 __ Abort(kAttemptToUseUndefinedCache);
4296 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
4297 context()->Plug(v0);
4301 VisitForAccumulatorValue(args->at(1));
4304 Register cache = a1;
4305 __ lw(cache, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
4306 __ lw(cache, FieldMemOperand(cache, GlobalObject::kNativeContextOffset));
4309 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
4311 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
4314 Label done, not_found;
4315 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
4316 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
4317 // a2 now holds finger offset as a smi.
4318 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4319 // a3 now points to the start of fixed array elements.
4320 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
4321 __ addu(a3, a3, at);
4322 // a3 now points to key of indexed element of cache.
4323 __ lw(a2, MemOperand(a3));
4324 __ Branch(¬_found, ne, key, Operand(a2));
4326 __ lw(v0, MemOperand(a3, kPointerSize));
4329 __ bind(¬_found);
4330 // Call runtime to perform the lookup.
4331 __ Push(cache, key);
4332 __ CallRuntime(Runtime::kGetFromCacheRT, 2);
4335 context()->Plug(v0);
4339 void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
4340 ZoneList<Expression*>* args = expr->arguments();
4341 VisitForAccumulatorValue(args->at(0));
4343 Label materialize_true, materialize_false;
4344 Label* if_true = NULL;
4345 Label* if_false = NULL;
4346 Label* fall_through = NULL;
4347 context()->PrepareTest(&materialize_true, &materialize_false,
4348 &if_true, &if_false, &fall_through);
4350 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
4351 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
4353 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4354 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
4356 context()->Plug(if_true, if_false);
4360 void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
4361 ZoneList<Expression*>* args = expr->arguments();
4362 DCHECK(args->length() == 1);
4363 VisitForAccumulatorValue(args->at(0));
4365 __ AssertString(v0);
4367 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
4368 __ IndexFromHash(v0, v0);
4370 context()->Plug(v0);
4374 void FullCodeGenerator::EmitFastOneByteArrayJoin(CallRuntime* expr) {
4375 Label bailout, done, one_char_separator, long_separator,
4376 non_trivial_array, not_size_one_array, loop,
4377 empty_separator_loop, one_char_separator_loop,
4378 one_char_separator_loop_entry, long_separator_loop;
4379 ZoneList<Expression*>* args = expr->arguments();
4380 DCHECK(args->length() == 2);
4381 VisitForStackValue(args->at(1));
4382 VisitForAccumulatorValue(args->at(0));
4384 // All aliases of the same register have disjoint lifetimes.
4385 Register array = v0;
4386 Register elements = no_reg; // Will be v0.
4387 Register result = no_reg; // Will be v0.
4388 Register separator = a1;
4389 Register array_length = a2;
4390 Register result_pos = no_reg; // Will be a2.
4391 Register string_length = a3;
4392 Register string = t0;
4393 Register element = t1;
4394 Register elements_end = t2;
4395 Register scratch1 = t3;
4396 Register scratch2 = t5;
4397 Register scratch3 = t4;
4399 // Separator operand is on the stack.
4402 // Check that the array is a JSArray.
4403 __ JumpIfSmi(array, &bailout);
4404 __ GetObjectType(array, scratch1, scratch2);
4405 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
4407 // Check that the array has fast elements.
4408 __ CheckFastElements(scratch1, scratch2, &bailout);
4410 // If the array has length zero, return the empty string.
4411 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
4412 __ SmiUntag(array_length);
4413 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
4414 __ LoadRoot(v0, Heap::kempty_stringRootIndex);
4417 __ bind(&non_trivial_array);
4419 // Get the FixedArray containing array's elements.
4421 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
4422 array = no_reg; // End of array's live range.
4424 // Check that all array elements are sequential one-byte strings, and
4425 // accumulate the sum of their lengths, as a smi-encoded value.
4426 __ mov(string_length, zero_reg);
4428 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4429 __ sll(elements_end, array_length, kPointerSizeLog2);
4430 __ Addu(elements_end, element, elements_end);
4431 // Loop condition: while (element < elements_end).
4432 // Live values in registers:
4433 // elements: Fixed array of strings.
4434 // array_length: Length of the fixed array of strings (not smi)
4435 // separator: Separator string
4436 // string_length: Accumulated sum of string lengths (smi).
4437 // element: Current array element.
4438 // elements_end: Array end.
4439 if (generate_debug_code_) {
4440 __ Assert(gt, kNoEmptyArraysHereInEmitFastOneByteArrayJoin, array_length,
4444 __ lw(string, MemOperand(element));
4445 __ Addu(element, element, kPointerSize);
4446 __ JumpIfSmi(string, &bailout);
4447 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
4448 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
4449 __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
4450 __ lw(scratch1, FieldMemOperand(string, SeqOneByteString::kLengthOffset));
4451 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
4452 __ BranchOnOverflow(&bailout, scratch3);
4453 __ Branch(&loop, lt, element, Operand(elements_end));
4455 // If array_length is 1, return elements[0], a string.
4456 __ Branch(¬_size_one_array, ne, array_length, Operand(1));
4457 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
4460 __ bind(¬_size_one_array);
4462 // Live values in registers:
4463 // separator: Separator string
4464 // array_length: Length of the array.
4465 // string_length: Sum of string lengths (smi).
4466 // elements: FixedArray of strings.
4468 // Check that the separator is a flat one-byte string.
4469 __ JumpIfSmi(separator, &bailout);
4470 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
4471 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
4472 __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
4474 // Add (separator length times array_length) - separator length to the
4475 // string_length to get the length of the result string. array_length is not
4476 // smi but the other values are, so the result is a smi.
4477 __ lw(scratch1, FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
4478 __ Subu(string_length, string_length, Operand(scratch1));
4479 __ Mul(scratch3, scratch2, array_length, scratch1);
4480 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
4482 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
4483 __ And(scratch3, scratch2, Operand(0x80000000));
4484 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
4485 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
4486 __ BranchOnOverflow(&bailout, scratch3);
4487 __ SmiUntag(string_length);
4489 // Get first element in the array to free up the elements register to be used
4492 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4493 result = elements; // End of live range for elements.
4495 // Live values in registers:
4496 // element: First array element
4497 // separator: Separator string
4498 // string_length: Length of result string (not smi)
4499 // array_length: Length of the array.
4500 __ AllocateOneByteString(result, string_length, scratch1, scratch2,
4501 elements_end, &bailout);
4502 // Prepare for looping. Set up elements_end to end of the array. Set
4503 // result_pos to the position of the result where to write the first
4505 __ sll(elements_end, array_length, kPointerSizeLog2);
4506 __ Addu(elements_end, element, elements_end);
4507 result_pos = array_length; // End of live range for array_length.
4508 array_length = no_reg;
4511 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4513 // Check the length of the separator.
4514 __ lw(scratch1, FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
4515 __ li(at, Operand(Smi::FromInt(1)));
4516 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
4517 __ Branch(&long_separator, gt, scratch1, Operand(at));
4519 // Empty separator case.
4520 __ bind(&empty_separator_loop);
4521 // Live values in registers:
4522 // result_pos: the position to which we are currently copying characters.
4523 // element: Current array element.
4524 // elements_end: Array end.
4526 // Copy next array element to the result.
4527 __ lw(string, MemOperand(element));
4528 __ Addu(element, element, kPointerSize);
4529 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
4530 __ SmiUntag(string_length);
4531 __ Addu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4532 __ CopyBytes(string, result_pos, string_length, scratch1);
4533 // End while (element < elements_end).
4534 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
4535 DCHECK(result.is(v0));
4538 // One-character separator case.
4539 __ bind(&one_char_separator);
4540 // Replace separator with its one-byte character value.
4541 __ lbu(separator, FieldMemOperand(separator, SeqOneByteString::kHeaderSize));
4542 // Jump into the loop after the code that copies the separator, so the first
4543 // element is not preceded by a separator.
4544 __ jmp(&one_char_separator_loop_entry);
4546 __ bind(&one_char_separator_loop);
4547 // Live values in registers:
4548 // result_pos: the position to which we are currently copying characters.
4549 // element: Current array element.
4550 // elements_end: Array end.
4551 // separator: Single separator one-byte char (in lower byte).
4553 // Copy the separator character to the result.
4554 __ sb(separator, MemOperand(result_pos));
4555 __ Addu(result_pos, result_pos, 1);
4557 // Copy next array element to the result.
4558 __ bind(&one_char_separator_loop_entry);
4559 __ lw(string, MemOperand(element));
4560 __ Addu(element, element, kPointerSize);
4561 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
4562 __ SmiUntag(string_length);
4563 __ Addu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4564 __ CopyBytes(string, result_pos, string_length, scratch1);
4565 // End while (element < elements_end).
4566 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
4567 DCHECK(result.is(v0));
4570 // Long separator case (separator is more than one character). Entry is at the
4571 // label long_separator below.
4572 __ bind(&long_separator_loop);
4573 // Live values in registers:
4574 // result_pos: the position to which we are currently copying characters.
4575 // element: Current array element.
4576 // elements_end: Array end.
4577 // separator: Separator string.
4579 // Copy the separator to the result.
4580 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
4581 __ SmiUntag(string_length);
4584 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4585 __ CopyBytes(string, result_pos, string_length, scratch1);
4587 __ bind(&long_separator);
4588 __ lw(string, MemOperand(element));
4589 __ Addu(element, element, kPointerSize);
4590 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
4591 __ SmiUntag(string_length);
4592 __ Addu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4593 __ CopyBytes(string, result_pos, string_length, scratch1);
4594 // End while (element < elements_end).
4595 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
4596 DCHECK(result.is(v0));
4600 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
4602 context()->Plug(v0);
4606 void FullCodeGenerator::EmitDebugIsActive(CallRuntime* expr) {
4607 DCHECK(expr->arguments()->length() == 0);
4608 ExternalReference debug_is_active =
4609 ExternalReference::debug_is_active_address(isolate());
4610 __ li(at, Operand(debug_is_active));
4611 __ lb(v0, MemOperand(at));
4613 context()->Plug(v0);
4617 void FullCodeGenerator::EmitLoadJSRuntimeFunction(CallRuntime* expr) {
4618 // Push the builtins object as the receiver.
4619 Register receiver = LoadDescriptor::ReceiverRegister();
4620 __ lw(receiver, GlobalObjectOperand());
4621 __ lw(receiver, FieldMemOperand(receiver, GlobalObject::kBuiltinsOffset));
4624 // Load the function from the receiver.
4625 __ li(LoadDescriptor::NameRegister(), Operand(expr->name()));
4626 __ li(LoadDescriptor::SlotRegister(),
4627 Operand(SmiFromSlot(expr->CallRuntimeFeedbackSlot())));
4628 CallLoadIC(NOT_INSIDE_TYPEOF);
4632 void FullCodeGenerator::EmitCallJSRuntimeFunction(CallRuntime* expr) {
4633 ZoneList<Expression*>* args = expr->arguments();
4634 int arg_count = args->length();
4636 SetCallPosition(expr, arg_count);
4637 CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
4638 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
4643 void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
4644 ZoneList<Expression*>* args = expr->arguments();
4645 int arg_count = args->length();
4647 if (expr->is_jsruntime()) {
4648 Comment cmnt(masm_, "[ CallRuntime");
4649 EmitLoadJSRuntimeFunction(expr);
4651 // Push the target function under the receiver.
4652 __ lw(at, MemOperand(sp, 0));
4654 __ sw(v0, MemOperand(sp, kPointerSize));
4656 // Push the arguments ("left-to-right").
4657 for (int i = 0; i < arg_count; i++) {
4658 VisitForStackValue(args->at(i));
4661 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4662 EmitCallJSRuntimeFunction(expr);
4664 // Restore context register.
4665 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4667 context()->DropAndPlug(1, v0);
4670 const Runtime::Function* function = expr->function();
4671 switch (function->function_id) {
4672 #define CALL_INTRINSIC_GENERATOR(Name) \
4673 case Runtime::kInline##Name: { \
4674 Comment cmnt(masm_, "[ Inline" #Name); \
4675 return Emit##Name(expr); \
4677 FOR_EACH_FULL_CODE_INTRINSIC(CALL_INTRINSIC_GENERATOR)
4678 #undef CALL_INTRINSIC_GENERATOR
4680 Comment cmnt(masm_, "[ CallRuntime for unhandled intrinsic");
4681 // Push the arguments ("left-to-right").
4682 for (int i = 0; i < arg_count; i++) {
4683 VisitForStackValue(args->at(i));
4686 // Call the C runtime function.
4687 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4688 __ CallRuntime(expr->function(), arg_count);
4689 context()->Plug(v0);
4696 void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
4697 switch (expr->op()) {
4698 case Token::DELETE: {
4699 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
4700 Property* property = expr->expression()->AsProperty();
4701 VariableProxy* proxy = expr->expression()->AsVariableProxy();
4703 if (property != NULL) {
4704 VisitForStackValue(property->obj());
4705 VisitForStackValue(property->key());
4706 __ li(a1, Operand(Smi::FromInt(language_mode())));
4708 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4709 context()->Plug(v0);
4710 } else if (proxy != NULL) {
4711 Variable* var = proxy->var();
4712 // Delete of an unqualified identifier is disallowed in strict mode but
4713 // "delete this" is allowed.
4714 bool is_this = var->HasThisName(isolate());
4715 DCHECK(is_sloppy(language_mode()) || is_this);
4716 if (var->IsUnallocatedOrGlobalSlot()) {
4717 __ lw(a2, GlobalObjectOperand());
4718 __ li(a1, Operand(var->name()));
4719 __ li(a0, Operand(Smi::FromInt(SLOPPY)));
4720 __ Push(a2, a1, a0);
4721 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4722 context()->Plug(v0);
4723 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
4724 // Result of deleting non-global, non-dynamic variables is false.
4725 // The subexpression does not have side effects.
4726 context()->Plug(is_this);
4728 // Non-global variable. Call the runtime to try to delete from the
4729 // context where the variable was introduced.
4730 DCHECK(!context_register().is(a2));
4731 __ li(a2, Operand(var->name()));
4732 __ Push(context_register(), a2);
4733 __ CallRuntime(Runtime::kDeleteLookupSlot, 2);
4734 context()->Plug(v0);
4737 // Result of deleting non-property, non-variable reference is true.
4738 // The subexpression may have side effects.
4739 VisitForEffect(expr->expression());
4740 context()->Plug(true);
4746 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
4747 VisitForEffect(expr->expression());
4748 context()->Plug(Heap::kUndefinedValueRootIndex);
4753 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
4754 if (context()->IsEffect()) {
4755 // Unary NOT has no side effects so it's only necessary to visit the
4756 // subexpression. Match the optimizing compiler by not branching.
4757 VisitForEffect(expr->expression());
4758 } else if (context()->IsTest()) {
4759 const TestContext* test = TestContext::cast(context());
4760 // The labels are swapped for the recursive call.
4761 VisitForControl(expr->expression(),
4762 test->false_label(),
4764 test->fall_through());
4765 context()->Plug(test->true_label(), test->false_label());
4767 // We handle value contexts explicitly rather than simply visiting
4768 // for control and plugging the control flow into the context,
4769 // because we need to prepare a pair of extra administrative AST ids
4770 // for the optimizing compiler.
4771 DCHECK(context()->IsAccumulatorValue() || context()->IsStackValue());
4772 Label materialize_true, materialize_false, done;
4773 VisitForControl(expr->expression(),
4777 __ bind(&materialize_true);
4778 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
4779 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
4780 if (context()->IsStackValue()) __ push(v0);
4782 __ bind(&materialize_false);
4783 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
4784 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
4785 if (context()->IsStackValue()) __ push(v0);
4791 case Token::TYPEOF: {
4792 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
4794 AccumulatorValueContext context(this);
4795 VisitForTypeofValue(expr->expression());
4798 TypeofStub typeof_stub(isolate());
4799 __ CallStub(&typeof_stub);
4800 context()->Plug(v0);
4810 void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
4811 DCHECK(expr->expression()->IsValidReferenceExpressionOrThis());
4813 Comment cmnt(masm_, "[ CountOperation");
4815 Property* prop = expr->expression()->AsProperty();
4816 LhsKind assign_type = Property::GetAssignType(prop);
4818 // Evaluate expression and get value.
4819 if (assign_type == VARIABLE) {
4820 DCHECK(expr->expression()->AsVariableProxy()->var() != NULL);
4821 AccumulatorValueContext context(this);
4822 EmitVariableLoad(expr->expression()->AsVariableProxy());
4824 // Reserve space for result of postfix operation.
4825 if (expr->is_postfix() && !context()->IsEffect()) {
4826 __ li(at, Operand(Smi::FromInt(0)));
4829 switch (assign_type) {
4830 case NAMED_PROPERTY: {
4831 // Put the object both on the stack and in the register.
4832 VisitForStackValue(prop->obj());
4833 __ lw(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
4834 EmitNamedPropertyLoad(prop);
4838 case NAMED_SUPER_PROPERTY: {
4839 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4840 VisitForAccumulatorValue(
4841 prop->obj()->AsSuperPropertyReference()->home_object());
4842 __ Push(result_register());
4843 const Register scratch = a1;
4844 __ lw(scratch, MemOperand(sp, kPointerSize));
4845 __ Push(scratch, result_register());
4846 EmitNamedSuperPropertyLoad(prop);
4850 case KEYED_SUPER_PROPERTY: {
4851 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4852 VisitForAccumulatorValue(
4853 prop->obj()->AsSuperPropertyReference()->home_object());
4854 const Register scratch = a1;
4855 const Register scratch1 = t0;
4856 __ Move(scratch, result_register());
4857 VisitForAccumulatorValue(prop->key());
4858 __ Push(scratch, result_register());
4859 __ lw(scratch1, MemOperand(sp, 2 * kPointerSize));
4860 __ Push(scratch1, scratch, result_register());
4861 EmitKeyedSuperPropertyLoad(prop);
4865 case KEYED_PROPERTY: {
4866 VisitForStackValue(prop->obj());
4867 VisitForStackValue(prop->key());
4868 __ lw(LoadDescriptor::ReceiverRegister(),
4869 MemOperand(sp, 1 * kPointerSize));
4870 __ lw(LoadDescriptor::NameRegister(), MemOperand(sp, 0));
4871 EmitKeyedPropertyLoad(prop);
4880 // We need a second deoptimization point after loading the value
4881 // in case evaluating the property load my have a side effect.
4882 if (assign_type == VARIABLE) {
4883 PrepareForBailout(expr->expression(), TOS_REG);
4885 PrepareForBailoutForId(prop->LoadId(), TOS_REG);
4888 // Inline smi case if we are in a loop.
4889 Label stub_call, done;
4890 JumpPatchSite patch_site(masm_);
4892 int count_value = expr->op() == Token::INC ? 1 : -1;
4894 if (ShouldInlineSmiCase(expr->op())) {
4896 patch_site.EmitJumpIfNotSmi(v0, &slow);
4898 // Save result for postfix expressions.
4899 if (expr->is_postfix()) {
4900 if (!context()->IsEffect()) {
4901 // Save the result on the stack. If we have a named or keyed property
4902 // we store the result under the receiver that is currently on top
4904 switch (assign_type) {
4908 case NAMED_PROPERTY:
4909 __ sw(v0, MemOperand(sp, kPointerSize));
4911 case NAMED_SUPER_PROPERTY:
4912 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4914 case KEYED_PROPERTY:
4915 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4917 case KEYED_SUPER_PROPERTY:
4918 __ sw(v0, MemOperand(sp, 3 * kPointerSize));
4924 Register scratch1 = a1;
4925 Register scratch2 = t0;
4926 __ li(scratch1, Operand(Smi::FromInt(count_value)));
4927 __ AdduAndCheckForOverflow(v0, v0, scratch1, scratch2);
4928 __ BranchOnNoOverflow(&done, scratch2);
4929 // Call stub. Undo operation first.
4934 if (!is_strong(language_mode())) {
4935 ToNumberStub convert_stub(isolate());
4936 __ CallStub(&convert_stub);
4937 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
4940 // Save result for postfix expressions.
4941 if (expr->is_postfix()) {
4942 if (!context()->IsEffect()) {
4943 // Save the result on the stack. If we have a named or keyed property
4944 // we store the result under the receiver that is currently on top
4946 switch (assign_type) {
4950 case NAMED_PROPERTY:
4951 __ sw(v0, MemOperand(sp, kPointerSize));
4953 case NAMED_SUPER_PROPERTY:
4954 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4956 case KEYED_PROPERTY:
4957 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4959 case KEYED_SUPER_PROPERTY:
4960 __ sw(v0, MemOperand(sp, 3 * kPointerSize));
4966 __ bind(&stub_call);
4968 __ li(a0, Operand(Smi::FromInt(count_value)));
4970 SetExpressionPosition(expr);
4973 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), Token::ADD,
4974 strength(language_mode())).code();
4975 CallIC(code, expr->CountBinOpFeedbackId());
4976 patch_site.EmitPatchInfo();
4979 if (is_strong(language_mode())) {
4980 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
4982 // Store the value returned in v0.
4983 switch (assign_type) {
4985 if (expr->is_postfix()) {
4986 { EffectContext context(this);
4987 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4988 Token::ASSIGN, expr->CountSlot());
4989 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4992 // For all contexts except EffectConstant we have the result on
4993 // top of the stack.
4994 if (!context()->IsEffect()) {
4995 context()->PlugTOS();
4998 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4999 Token::ASSIGN, expr->CountSlot());
5000 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5001 context()->Plug(v0);
5004 case NAMED_PROPERTY: {
5005 __ mov(StoreDescriptor::ValueRegister(), result_register());
5006 __ li(StoreDescriptor::NameRegister(),
5007 Operand(prop->key()->AsLiteral()->value()));
5008 __ pop(StoreDescriptor::ReceiverRegister());
5009 if (FLAG_vector_stores) {
5010 EmitLoadStoreICSlot(expr->CountSlot());
5013 CallStoreIC(expr->CountStoreFeedbackId());
5015 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5016 if (expr->is_postfix()) {
5017 if (!context()->IsEffect()) {
5018 context()->PlugTOS();
5021 context()->Plug(v0);
5025 case NAMED_SUPER_PROPERTY: {
5026 EmitNamedSuperPropertyStore(prop);
5027 if (expr->is_postfix()) {
5028 if (!context()->IsEffect()) {
5029 context()->PlugTOS();
5032 context()->Plug(v0);
5036 case KEYED_SUPER_PROPERTY: {
5037 EmitKeyedSuperPropertyStore(prop);
5038 if (expr->is_postfix()) {
5039 if (!context()->IsEffect()) {
5040 context()->PlugTOS();
5043 context()->Plug(v0);
5047 case KEYED_PROPERTY: {
5048 __ mov(StoreDescriptor::ValueRegister(), result_register());
5049 __ Pop(StoreDescriptor::ReceiverRegister(),
5050 StoreDescriptor::NameRegister());
5052 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
5053 if (FLAG_vector_stores) {
5054 EmitLoadStoreICSlot(expr->CountSlot());
5057 CallIC(ic, expr->CountStoreFeedbackId());
5059 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5060 if (expr->is_postfix()) {
5061 if (!context()->IsEffect()) {
5062 context()->PlugTOS();
5065 context()->Plug(v0);
5073 void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
5074 Expression* sub_expr,
5075 Handle<String> check) {
5076 Label materialize_true, materialize_false;
5077 Label* if_true = NULL;
5078 Label* if_false = NULL;
5079 Label* fall_through = NULL;
5080 context()->PrepareTest(&materialize_true, &materialize_false,
5081 &if_true, &if_false, &fall_through);
5083 { AccumulatorValueContext context(this);
5084 VisitForTypeofValue(sub_expr);
5086 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5088 Factory* factory = isolate()->factory();
5089 if (String::Equals(check, factory->number_string())) {
5090 __ JumpIfSmi(v0, if_true);
5091 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
5092 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5093 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
5094 } else if (String::Equals(check, factory->string_string())) {
5095 __ JumpIfSmi(v0, if_false);
5096 // Check for undetectable objects => false.
5097 __ GetObjectType(v0, v0, a1);
5098 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
5099 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
5100 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
5101 Split(eq, a1, Operand(zero_reg),
5102 if_true, if_false, fall_through);
5103 } else if (String::Equals(check, factory->symbol_string())) {
5104 __ JumpIfSmi(v0, if_false);
5105 __ GetObjectType(v0, v0, a1);
5106 Split(eq, a1, Operand(SYMBOL_TYPE), if_true, if_false, fall_through);
5107 } else if (String::Equals(check, factory->float32x4_string())) {
5108 __ JumpIfSmi(v0, if_false);
5109 __ GetObjectType(v0, v0, a1);
5110 Split(eq, a1, Operand(FLOAT32X4_TYPE), if_true, if_false, fall_through);
5111 } else if (String::Equals(check, factory->boolean_string())) {
5112 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5113 __ Branch(if_true, eq, v0, Operand(at));
5114 __ LoadRoot(at, Heap::kFalseValueRootIndex);
5115 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
5116 } else if (String::Equals(check, factory->undefined_string())) {
5117 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5118 __ Branch(if_true, eq, v0, Operand(at));
5119 __ JumpIfSmi(v0, if_false);
5120 // Check for undetectable objects => true.
5121 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
5122 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
5123 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
5124 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
5125 } else if (String::Equals(check, factory->function_string())) {
5126 __ JumpIfSmi(v0, if_false);
5127 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5128 __ GetObjectType(v0, v0, a1);
5129 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
5130 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
5131 if_true, if_false, fall_through);
5132 } else if (String::Equals(check, factory->object_string())) {
5133 __ JumpIfSmi(v0, if_false);
5134 __ LoadRoot(at, Heap::kNullValueRootIndex);
5135 __ Branch(if_true, eq, v0, Operand(at));
5136 // Check for JS objects => true.
5137 __ GetObjectType(v0, v0, a1);
5138 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
5139 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
5140 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
5141 // Check for undetectable objects => false.
5142 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
5143 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
5144 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
5146 if (if_false != fall_through) __ jmp(if_false);
5148 context()->Plug(if_true, if_false);
5152 void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
5153 Comment cmnt(masm_, "[ CompareOperation");
5154 SetExpressionPosition(expr);
5156 // First we try a fast inlined version of the compare when one of
5157 // the operands is a literal.
5158 if (TryLiteralCompare(expr)) return;
5160 // Always perform the comparison for its control flow. Pack the result
5161 // into the expression's context after the comparison is performed.
5162 Label materialize_true, materialize_false;
5163 Label* if_true = NULL;
5164 Label* if_false = NULL;
5165 Label* fall_through = NULL;
5166 context()->PrepareTest(&materialize_true, &materialize_false,
5167 &if_true, &if_false, &fall_through);
5169 Token::Value op = expr->op();
5170 VisitForStackValue(expr->left());
5173 VisitForStackValue(expr->right());
5174 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
5175 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
5176 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
5177 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
5180 case Token::INSTANCEOF: {
5181 VisitForStackValue(expr->right());
5182 InstanceofStub stub(isolate(), InstanceofStub::kNoFlags);
5184 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5185 // The stub returns 0 for true.
5186 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
5191 VisitForAccumulatorValue(expr->right());
5192 Condition cc = CompareIC::ComputeCondition(op);
5193 __ mov(a0, result_register());
5196 bool inline_smi_code = ShouldInlineSmiCase(op);
5197 JumpPatchSite patch_site(masm_);
5198 if (inline_smi_code) {
5200 __ Or(a2, a0, Operand(a1));
5201 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
5202 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
5203 __ bind(&slow_case);
5206 Handle<Code> ic = CodeFactory::CompareIC(
5207 isolate(), op, strength(language_mode())).code();
5208 CallIC(ic, expr->CompareOperationFeedbackId());
5209 patch_site.EmitPatchInfo();
5210 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5211 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
5215 // Convert the result of the comparison into one expected for this
5216 // expression's context.
5217 context()->Plug(if_true, if_false);
5221 void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
5222 Expression* sub_expr,
5224 Label materialize_true, materialize_false;
5225 Label* if_true = NULL;
5226 Label* if_false = NULL;
5227 Label* fall_through = NULL;
5228 context()->PrepareTest(&materialize_true, &materialize_false,
5229 &if_true, &if_false, &fall_through);
5231 VisitForAccumulatorValue(sub_expr);
5232 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5233 __ mov(a0, result_register());
5234 if (expr->op() == Token::EQ_STRICT) {
5235 Heap::RootListIndex nil_value = nil == kNullValue ?
5236 Heap::kNullValueRootIndex :
5237 Heap::kUndefinedValueRootIndex;
5238 __ LoadRoot(a1, nil_value);
5239 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
5241 Handle<Code> ic = CompareNilICStub::GetUninitialized(isolate(), nil);
5242 CallIC(ic, expr->CompareOperationFeedbackId());
5243 Split(ne, v0, Operand(zero_reg), if_true, if_false, fall_through);
5245 context()->Plug(if_true, if_false);
5249 void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
5250 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5251 context()->Plug(v0);
5255 Register FullCodeGenerator::result_register() {
5260 Register FullCodeGenerator::context_register() {
5265 void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
5266 DCHECK_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
5267 __ sw(value, MemOperand(fp, frame_offset));
5271 void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
5272 __ lw(dst, ContextOperand(cp, context_index));
5276 void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
5277 Scope* declaration_scope = scope()->DeclarationScope();
5278 if (declaration_scope->is_script_scope() ||
5279 declaration_scope->is_module_scope()) {
5280 // Contexts nested in the native context have a canonical empty function
5281 // as their closure, not the anonymous closure containing the global
5282 // code. Pass a smi sentinel and let the runtime look up the empty
5284 __ li(at, Operand(Smi::FromInt(0)));
5285 } else if (declaration_scope->is_eval_scope()) {
5286 // Contexts created by a call to eval have the same closure as the
5287 // context calling eval, not the anonymous closure containing the eval
5288 // code. Fetch it from the context.
5289 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
5291 DCHECK(declaration_scope->is_function_scope());
5292 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5298 // ----------------------------------------------------------------------------
5299 // Non-local control flow support.
5301 void FullCodeGenerator::EnterFinallyBlock() {
5302 DCHECK(!result_register().is(a1));
5303 // Store result register while executing finally block.
5304 __ push(result_register());
5305 // Cook return address in link register to stack (smi encoded Code* delta).
5306 __ Subu(a1, ra, Operand(masm_->CodeObject()));
5307 DCHECK_EQ(1, kSmiTagSize + kSmiShiftSize);
5308 STATIC_ASSERT(0 == kSmiTag);
5309 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
5311 // Store result register while executing finally block.
5314 // Store pending message while executing finally block.
5315 ExternalReference pending_message_obj =
5316 ExternalReference::address_of_pending_message_obj(isolate());
5317 __ li(at, Operand(pending_message_obj));
5318 __ lw(a1, MemOperand(at));
5321 ClearPendingMessage();
5325 void FullCodeGenerator::ExitFinallyBlock() {
5326 DCHECK(!result_register().is(a1));
5327 // Restore pending message from stack.
5329 ExternalReference pending_message_obj =
5330 ExternalReference::address_of_pending_message_obj(isolate());
5331 __ li(at, Operand(pending_message_obj));
5332 __ sw(a1, MemOperand(at));
5334 // Restore result register from stack.
5337 // Uncook return address and return.
5338 __ pop(result_register());
5339 DCHECK_EQ(1, kSmiTagSize + kSmiShiftSize);
5340 __ sra(a1, a1, 1); // Un-smi-tag value.
5341 __ Addu(at, a1, Operand(masm_->CodeObject()));
5346 void FullCodeGenerator::ClearPendingMessage() {
5347 DCHECK(!result_register().is(a1));
5348 ExternalReference pending_message_obj =
5349 ExternalReference::address_of_pending_message_obj(isolate());
5350 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
5351 __ li(at, Operand(pending_message_obj));
5352 __ sw(a1, MemOperand(at));
5356 void FullCodeGenerator::EmitLoadStoreICSlot(FeedbackVectorICSlot slot) {
5357 DCHECK(FLAG_vector_stores && !slot.IsInvalid());
5358 __ li(VectorStoreICTrampolineDescriptor::SlotRegister(),
5359 Operand(SmiFromSlot(slot)));
5366 void BackEdgeTable::PatchAt(Code* unoptimized_code,
5368 BackEdgeState target_state,
5369 Code* replacement_code) {
5370 static const int kInstrSize = Assembler::kInstrSize;
5371 Address branch_address = pc - 6 * kInstrSize;
5372 CodePatcher patcher(branch_address, 1);
5374 switch (target_state) {
5376 // slt at, a3, zero_reg (in case of count based interrupts)
5377 // beq at, zero_reg, ok
5378 // lui t9, <interrupt stub address> upper
5379 // ori t9, <interrupt stub address> lower
5382 // ok-label ----- pc_after points here
5383 patcher.masm()->slt(at, a3, zero_reg);
5385 case ON_STACK_REPLACEMENT:
5386 case OSR_AFTER_STACK_CHECK:
5387 // addiu at, zero_reg, 1
5388 // beq at, zero_reg, ok ;; Not changed
5389 // lui t9, <on-stack replacement address> upper
5390 // ori t9, <on-stack replacement address> lower
5391 // jalr t9 ;; Not changed
5392 // nop ;; Not changed
5393 // ok-label ----- pc_after points here
5394 patcher.masm()->addiu(at, zero_reg, 1);
5397 Address pc_immediate_load_address = pc - 4 * kInstrSize;
5398 // Replace the stack check address in the load-immediate (lui/ori pair)
5399 // with the entry address of the replacement code.
5400 Assembler::set_target_address_at(pc_immediate_load_address,
5401 replacement_code->entry());
5403 unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
5404 unoptimized_code, pc_immediate_load_address, replacement_code);
5408 BackEdgeTable::BackEdgeState BackEdgeTable::GetBackEdgeState(
5410 Code* unoptimized_code,
5412 static const int kInstrSize = Assembler::kInstrSize;
5413 Address branch_address = pc - 6 * kInstrSize;
5414 Address pc_immediate_load_address = pc - 4 * kInstrSize;
5416 DCHECK(Assembler::IsBeq(Assembler::instr_at(pc - 5 * kInstrSize)));
5417 if (!Assembler::IsAddImmediate(Assembler::instr_at(branch_address))) {
5418 DCHECK(reinterpret_cast<uint32_t>(
5419 Assembler::target_address_at(pc_immediate_load_address)) ==
5420 reinterpret_cast<uint32_t>(
5421 isolate->builtins()->InterruptCheck()->entry()));
5425 DCHECK(Assembler::IsAddImmediate(Assembler::instr_at(branch_address)));
5427 if (reinterpret_cast<uint32_t>(
5428 Assembler::target_address_at(pc_immediate_load_address)) ==
5429 reinterpret_cast<uint32_t>(
5430 isolate->builtins()->OnStackReplacement()->entry())) {
5431 return ON_STACK_REPLACEMENT;
5434 DCHECK(reinterpret_cast<uint32_t>(
5435 Assembler::target_address_at(pc_immediate_load_address)) ==
5436 reinterpret_cast<uint32_t>(
5437 isolate->builtins()->OsrAfterStackCheck()->entry()));
5438 return OSR_AFTER_STACK_CHECK;
5442 } // namespace internal
5445 #endif // V8_TARGET_ARCH_MIPS