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_MIPS64
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/debug.h"
22 #include "src/full-codegen/full-codegen.h"
23 #include "src/ic/ic.h"
24 #include "src/parser.h"
25 #include "src/scopes.h"
27 #include "src/mips64/code-stubs-mips64.h"
28 #include "src/mips64/macro-assembler-mips64.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 __ ld(at, MemOperand(sp, receiver_offset));
138 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
139 __ Branch(&ok, ne, a2, Operand(at));
141 __ ld(a2, GlobalObjectOperand());
142 __ ld(a2, FieldMemOperand(a2, GlobalObject::kGlobalProxyOffset));
144 __ sd(a2, MemOperand(sp, receiver_offset));
147 // Open a frame scope to indicate that there is a frame on the stack. The
148 // MANUAL indicates that the scope shouldn't actually generate code to set up
149 // the frame (that is done below).
150 FrameScope frame_scope(masm_, StackFrame::MANUAL);
151 info->set_prologue_offset(masm_->pc_offset());
152 __ Prologue(info->IsCodePreAgingActive());
153 info->AddNoFrameRange(0, masm_->pc_offset());
155 { Comment cmnt(masm_, "[ Allocate locals");
156 int locals_count = info->scope()->num_stack_slots();
157 // Generators allocate locals, if any, in context slots.
158 DCHECK(!IsGeneratorFunction(info->function()->kind()) || locals_count == 0);
159 if (locals_count > 0) {
160 if (locals_count >= 128) {
162 __ Dsubu(t1, sp, Operand(locals_count * kPointerSize));
163 __ LoadRoot(a2, Heap::kRealStackLimitRootIndex);
164 __ Branch(&ok, hs, t1, Operand(a2));
165 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
168 __ LoadRoot(t1, Heap::kUndefinedValueRootIndex);
169 int kMaxPushes = FLAG_optimize_for_size ? 4 : 32;
170 if (locals_count >= kMaxPushes) {
171 int loop_iterations = locals_count / kMaxPushes;
172 __ li(a2, Operand(loop_iterations));
174 __ bind(&loop_header);
176 __ Dsubu(sp, sp, Operand(kMaxPushes * kPointerSize));
177 for (int i = 0; i < kMaxPushes; i++) {
178 __ sd(t1, MemOperand(sp, i * kPointerSize));
180 // Continue loop if not done.
181 __ Dsubu(a2, a2, Operand(1));
182 __ Branch(&loop_header, ne, a2, Operand(zero_reg));
184 int remaining = locals_count % kMaxPushes;
185 // Emit the remaining pushes.
186 __ Dsubu(sp, sp, Operand(remaining * kPointerSize));
187 for (int i = 0; i < remaining; i++) {
188 __ sd(t1, MemOperand(sp, i * kPointerSize));
193 bool function_in_register = true;
195 // Possibly allocate a local context.
196 if (info->scope()->num_heap_slots() > 0) {
197 Comment cmnt(masm_, "[ Allocate context");
198 // Argument to NewContext is the function, which is still in a1.
199 bool need_write_barrier = true;
200 int slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
201 if (info->scope()->is_script_scope()) {
203 __ Push(info->scope()->GetScopeInfo(info->isolate()));
204 __ CallRuntime(Runtime::kNewScriptContext, 2);
205 } else if (slots <= FastNewContextStub::kMaximumSlots) {
206 FastNewContextStub stub(isolate(), slots);
208 // Result of FastNewContextStub is always in new space.
209 need_write_barrier = false;
212 __ CallRuntime(Runtime::kNewFunctionContext, 1);
214 function_in_register = false;
215 // Context is returned in v0. It replaces the context passed to us.
216 // It's saved in the stack and kept live in cp.
218 __ sd(v0, MemOperand(fp, StandardFrameConstants::kContextOffset));
219 // Copy any necessary parameters into the context.
220 int num_parameters = info->scope()->num_parameters();
221 int first_parameter = info->scope()->has_this_declaration() ? -1 : 0;
222 for (int i = first_parameter; i < num_parameters; i++) {
223 Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
224 if (var->IsContextSlot()) {
225 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
226 (num_parameters - 1 - i) * kPointerSize;
227 // Load parameter from stack.
228 __ ld(a0, MemOperand(fp, parameter_offset));
229 // Store it in the context.
230 MemOperand target = ContextOperand(cp, var->index());
233 // Update the write barrier.
234 if (need_write_barrier) {
235 __ RecordWriteContextSlot(
236 cp, target.offset(), a0, a3, kRAHasBeenSaved, kDontSaveFPRegs);
237 } else if (FLAG_debug_code) {
239 __ JumpIfInNewSpace(cp, a0, &done);
240 __ Abort(kExpectedNewSpaceObject);
247 // Possibly set up a local binding to the this function which is used in
248 // derived constructors with super calls.
249 Variable* this_function_var = scope()->this_function_var();
250 if (this_function_var != nullptr) {
251 Comment cmnt(masm_, "[ This function");
252 if (!function_in_register) {
253 __ ld(a1, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
254 // The write barrier clobbers register again, keep is marked as such.
256 SetVar(this_function_var, a1, a2, a3);
259 Variable* new_target_var = scope()->new_target_var();
260 if (new_target_var != nullptr) {
261 Comment cmnt(masm_, "[ new.target");
262 // Get the frame pointer for the calling frame.
263 __ ld(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
265 // Skip the arguments adaptor frame if it exists.
266 Label check_frame_marker;
267 __ ld(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
268 __ Branch(&check_frame_marker, ne, a1,
269 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
270 __ ld(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
272 // Check the marker in the calling frame.
273 __ bind(&check_frame_marker);
274 __ ld(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
276 Label non_construct_frame, done;
277 __ Branch(&non_construct_frame, ne, a1,
278 Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
281 MemOperand(a2, ConstructFrameConstants::kOriginalConstructorOffset));
284 __ bind(&non_construct_frame);
285 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
288 SetVar(new_target_var, v0, a2, a3);
291 // Possibly allocate RestParameters
293 Variable* rest_param = scope()->rest_parameter(&rest_index);
295 Comment cmnt(masm_, "[ Allocate rest parameter array");
297 int num_parameters = info->scope()->num_parameters();
298 int offset = num_parameters * kPointerSize;
301 Operand(StandardFrameConstants::kCallerSPOffset + offset));
302 __ li(a2, Operand(Smi::FromInt(num_parameters)));
303 __ li(a1, Operand(Smi::FromInt(rest_index)));
304 __ li(a0, Operand(Smi::FromInt(language_mode())));
305 __ Push(a3, a2, a1, a0);
307 RestParamAccessStub stub(isolate());
310 SetVar(rest_param, v0, a1, a2);
313 Variable* arguments = scope()->arguments();
314 if (arguments != NULL) {
315 // Function uses arguments object.
316 Comment cmnt(masm_, "[ Allocate arguments object");
317 if (!function_in_register) {
318 // Load this again, if it's used by the local context below.
319 __ ld(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
323 // Receiver is just before the parameters on the caller's stack.
324 int num_parameters = info->scope()->num_parameters();
325 int offset = num_parameters * kPointerSize;
327 Operand(StandardFrameConstants::kCallerSPOffset + offset));
328 __ li(a1, Operand(Smi::FromInt(num_parameters)));
331 // Arguments to ArgumentsAccessStub:
332 // function, receiver address, parameter count.
333 // The stub will rewrite receiever and parameter count if the previous
334 // stack frame was an arguments adapter frame.
335 ArgumentsAccessStub::Type type;
336 if (is_strict(language_mode()) || !is_simple_parameter_list()) {
337 type = ArgumentsAccessStub::NEW_STRICT;
338 } else if (function()->has_duplicate_parameters()) {
339 type = ArgumentsAccessStub::NEW_SLOPPY_SLOW;
341 type = ArgumentsAccessStub::NEW_SLOPPY_FAST;
343 ArgumentsAccessStub stub(isolate(), type);
346 SetVar(arguments, v0, a1, a2);
350 __ CallRuntime(Runtime::kTraceEnter, 0);
352 // Visit the declarations and body unless there is an illegal
354 if (scope()->HasIllegalRedeclaration()) {
355 Comment cmnt(masm_, "[ Declarations");
356 scope()->VisitIllegalRedeclaration(this);
359 PrepareForBailoutForId(BailoutId::FunctionEntry(), NO_REGISTERS);
360 { Comment cmnt(masm_, "[ Declarations");
361 VisitDeclarations(scope()->declarations());
364 // Assert that the declarations do not use ICs. Otherwise the debugger
365 // won't be able to redirect a PC at an IC to the correct IC in newly
367 DCHECK_EQ(0, ic_total_count_);
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);
384 VisitStatements(function()->body());
386 DCHECK(loop_depth() == 0);
390 // Always emit a 'return undefined' in case control fell off the end of
392 { Comment cmnt(masm_, "[ return <undefined>;");
393 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
395 EmitReturnSequence();
399 void FullCodeGenerator::ClearAccumulator() {
400 DCHECK(Smi::FromInt(0) == 0);
401 __ mov(v0, zero_reg);
405 void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
406 __ li(a2, Operand(profiling_counter_));
407 __ ld(a3, FieldMemOperand(a2, Cell::kValueOffset));
408 __ Dsubu(a3, a3, Operand(Smi::FromInt(delta)));
409 __ sd(a3, FieldMemOperand(a2, Cell::kValueOffset));
413 void FullCodeGenerator::EmitProfilingCounterReset() {
414 int reset_value = FLAG_interrupt_budget;
415 if (info_->is_debug()) {
416 // Detect debug break requests as soon as possible.
417 reset_value = FLAG_interrupt_budget >> 4;
419 __ li(a2, Operand(profiling_counter_));
420 __ li(a3, Operand(Smi::FromInt(reset_value)));
421 __ sd(a3, FieldMemOperand(a2, Cell::kValueOffset));
425 void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
426 Label* back_edge_target) {
427 // The generated code is used in Deoptimizer::PatchStackCheckCodeAt so we need
428 // to make sure it is constant. Branch may emit a skip-or-jump sequence
429 // instead of the normal Branch. It seems that the "skip" part of that
430 // sequence is about as long as this Branch would be so it is safe to ignore
432 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
433 Comment cmnt(masm_, "[ Back edge bookkeeping");
435 DCHECK(back_edge_target->is_bound());
436 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
437 int weight = Min(kMaxBackEdgeWeight,
438 Max(1, distance / kCodeSizeMultiplier));
439 EmitProfilingCounterDecrement(weight);
440 __ slt(at, a3, zero_reg);
441 __ beq(at, zero_reg, &ok);
442 // Call will emit a li t9 first, so it is safe to use the delay slot.
443 __ Call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
444 // Record a mapping of this PC offset to the OSR id. This is used to find
445 // the AST id from the unoptimized code in order to use it as a key into
446 // the deoptimization input data found in the optimized code.
447 RecordBackEdge(stmt->OsrEntryId());
448 EmitProfilingCounterReset();
451 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
452 // Record a mapping of the OSR id to this PC. This is used if the OSR
453 // entry becomes the target of a bailout. We don't expect it to be, but
454 // we want it to work if it is.
455 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
459 void FullCodeGenerator::EmitReturnSequence() {
460 Comment cmnt(masm_, "[ Return sequence");
461 if (return_label_.is_bound()) {
462 __ Branch(&return_label_);
464 __ bind(&return_label_);
466 // Push the return value on the stack as the parameter.
467 // Runtime::TraceExit returns its parameter in v0.
469 __ CallRuntime(Runtime::kTraceExit, 1);
471 // Pretend that the exit is a backwards jump to the entry.
473 if (info_->ShouldSelfOptimize()) {
474 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
476 int distance = masm_->pc_offset();
477 weight = Min(kMaxBackEdgeWeight,
478 Max(1, distance / kCodeSizeMultiplier));
480 EmitProfilingCounterDecrement(weight);
482 __ Branch(&ok, ge, a3, Operand(zero_reg));
484 __ Call(isolate()->builtins()->InterruptCheck(),
485 RelocInfo::CODE_TARGET);
487 EmitProfilingCounterReset();
490 // Make sure that the constant pool is not emitted inside of the return
492 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
493 // Here we use masm_-> instead of the __ macro to avoid the code coverage
494 // tool from instrumenting as we rely on the code size here.
495 int32_t arg_count = info_->scope()->num_parameters() + 1;
496 int32_t sp_delta = arg_count * kPointerSize;
497 SetReturnPosition(function());
499 int no_frame_start = masm_->pc_offset();
500 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
501 masm_->Daddu(sp, sp, Operand(sp_delta));
503 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
509 void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
510 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
511 codegen()->GetVar(result_register(), var);
512 __ push(result_register());
516 void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
520 void FullCodeGenerator::AccumulatorValueContext::Plug(
521 Heap::RootListIndex index) const {
522 __ LoadRoot(result_register(), index);
526 void FullCodeGenerator::StackValueContext::Plug(
527 Heap::RootListIndex index) const {
528 __ LoadRoot(result_register(), index);
529 __ push(result_register());
533 void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
534 codegen()->PrepareForBailoutBeforeSplit(condition(),
538 if (index == Heap::kUndefinedValueRootIndex ||
539 index == Heap::kNullValueRootIndex ||
540 index == Heap::kFalseValueRootIndex) {
541 if (false_label_ != fall_through_) __ Branch(false_label_);
542 } else if (index == Heap::kTrueValueRootIndex) {
543 if (true_label_ != fall_through_) __ Branch(true_label_);
545 __ LoadRoot(result_register(), index);
546 codegen()->DoTest(this);
551 void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
555 void FullCodeGenerator::AccumulatorValueContext::Plug(
556 Handle<Object> lit) const {
557 __ li(result_register(), Operand(lit));
561 void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
562 // Immediates cannot be pushed directly.
563 __ li(result_register(), Operand(lit));
564 __ push(result_register());
568 void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
569 codegen()->PrepareForBailoutBeforeSplit(condition(),
573 DCHECK(!lit->IsUndetectableObject()); // There are no undetectable literals.
574 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
575 if (false_label_ != fall_through_) __ Branch(false_label_);
576 } else if (lit->IsTrue() || lit->IsJSObject()) {
577 if (true_label_ != fall_through_) __ Branch(true_label_);
578 } else if (lit->IsString()) {
579 if (String::cast(*lit)->length() == 0) {
580 if (false_label_ != fall_through_) __ Branch(false_label_);
582 if (true_label_ != fall_through_) __ Branch(true_label_);
584 } else if (lit->IsSmi()) {
585 if (Smi::cast(*lit)->value() == 0) {
586 if (false_label_ != fall_through_) __ Branch(false_label_);
588 if (true_label_ != fall_through_) __ Branch(true_label_);
591 // For simplicity we always test the accumulator register.
592 __ li(result_register(), Operand(lit));
593 codegen()->DoTest(this);
598 void FullCodeGenerator::EffectContext::DropAndPlug(int count,
599 Register reg) const {
605 void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
607 Register reg) const {
610 __ Move(result_register(), reg);
614 void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
615 Register reg) const {
617 if (count > 1) __ Drop(count - 1);
618 __ sd(reg, MemOperand(sp, 0));
622 void FullCodeGenerator::TestContext::DropAndPlug(int count,
623 Register reg) const {
625 // For simplicity we always test the accumulator register.
627 __ Move(result_register(), reg);
628 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
629 codegen()->DoTest(this);
633 void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
634 Label* materialize_false) const {
635 DCHECK(materialize_true == materialize_false);
636 __ bind(materialize_true);
640 void FullCodeGenerator::AccumulatorValueContext::Plug(
641 Label* materialize_true,
642 Label* materialize_false) const {
644 __ bind(materialize_true);
645 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
647 __ bind(materialize_false);
648 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
653 void FullCodeGenerator::StackValueContext::Plug(
654 Label* materialize_true,
655 Label* materialize_false) const {
657 __ bind(materialize_true);
658 __ LoadRoot(at, Heap::kTrueValueRootIndex);
659 // Push the value as the following branch can clobber at in long branch mode.
662 __ bind(materialize_false);
663 __ LoadRoot(at, Heap::kFalseValueRootIndex);
669 void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
670 Label* materialize_false) const {
671 DCHECK(materialize_true == true_label_);
672 DCHECK(materialize_false == false_label_);
676 void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
677 Heap::RootListIndex value_root_index =
678 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
679 __ LoadRoot(result_register(), value_root_index);
683 void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
684 Heap::RootListIndex value_root_index =
685 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
686 __ LoadRoot(at, value_root_index);
691 void FullCodeGenerator::TestContext::Plug(bool flag) const {
692 codegen()->PrepareForBailoutBeforeSplit(condition(),
697 if (true_label_ != fall_through_) __ Branch(true_label_);
699 if (false_label_ != fall_through_) __ Branch(false_label_);
704 void FullCodeGenerator::DoTest(Expression* condition,
707 Label* fall_through) {
708 __ mov(a0, result_register());
709 Handle<Code> ic = ToBooleanStub::GetUninitialized(isolate());
710 CallIC(ic, condition->test_id());
711 __ mov(at, zero_reg);
712 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
716 void FullCodeGenerator::Split(Condition cc,
721 Label* fall_through) {
722 if (if_false == fall_through) {
723 __ Branch(if_true, cc, lhs, rhs);
724 } else if (if_true == fall_through) {
725 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
727 __ Branch(if_true, cc, lhs, rhs);
733 MemOperand FullCodeGenerator::StackOperand(Variable* var) {
734 DCHECK(var->IsStackAllocated());
735 // Offset is negative because higher indexes are at lower addresses.
736 int offset = -var->index() * kPointerSize;
737 // Adjust by a (parameter or local) base offset.
738 if (var->IsParameter()) {
739 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
741 offset += JavaScriptFrameConstants::kLocal0Offset;
743 return MemOperand(fp, offset);
747 MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
748 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
749 if (var->IsContextSlot()) {
750 int context_chain_length = scope()->ContextChainLength(var->scope());
751 __ LoadContext(scratch, context_chain_length);
752 return ContextOperand(scratch, var->index());
754 return StackOperand(var);
759 void FullCodeGenerator::GetVar(Register dest, Variable* var) {
760 // Use destination as scratch.
761 MemOperand location = VarOperand(var, dest);
762 __ ld(dest, location);
766 void FullCodeGenerator::SetVar(Variable* var,
770 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
771 DCHECK(!scratch0.is(src));
772 DCHECK(!scratch0.is(scratch1));
773 DCHECK(!scratch1.is(src));
774 MemOperand location = VarOperand(var, scratch0);
775 __ sd(src, location);
776 // Emit the write barrier code if the location is in the heap.
777 if (var->IsContextSlot()) {
778 __ RecordWriteContextSlot(scratch0,
788 void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
789 bool should_normalize,
792 // Only prepare for bailouts before splits if we're in a test
793 // context. Otherwise, we let the Visit function deal with the
794 // preparation to avoid preparing with the same AST id twice.
795 if (!context()->IsTest() || !info_->IsOptimizable()) return;
798 if (should_normalize) __ Branch(&skip);
799 PrepareForBailout(expr, TOS_REG);
800 if (should_normalize) {
801 __ LoadRoot(a4, Heap::kTrueValueRootIndex);
802 Split(eq, a0, Operand(a4), if_true, if_false, NULL);
808 void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
809 // The variable in the declaration always resides in the current function
811 DCHECK_EQ(0, scope()->ContextChainLength(variable->scope()));
812 if (generate_debug_code_) {
813 // Check that we're not inside a with or catch context.
814 __ ld(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
815 __ LoadRoot(a4, Heap::kWithContextMapRootIndex);
816 __ Check(ne, kDeclarationInWithContext,
818 __ LoadRoot(a4, Heap::kCatchContextMapRootIndex);
819 __ Check(ne, kDeclarationInCatchContext,
825 void FullCodeGenerator::VisitVariableDeclaration(
826 VariableDeclaration* declaration) {
827 // If it was not possible to allocate the variable at compile time, we
828 // need to "declare" it at runtime to make sure it actually exists in the
830 VariableProxy* proxy = declaration->proxy();
831 VariableMode mode = declaration->mode();
832 Variable* variable = proxy->var();
833 bool hole_init = mode == LET || mode == CONST || mode == CONST_LEGACY;
834 switch (variable->location()) {
835 case VariableLocation::GLOBAL:
836 case VariableLocation::UNALLOCATED:
837 globals_->Add(variable->name(), zone());
838 globals_->Add(variable->binding_needs_init()
839 ? isolate()->factory()->the_hole_value()
840 : isolate()->factory()->undefined_value(),
844 case VariableLocation::PARAMETER:
845 case VariableLocation::LOCAL:
847 Comment cmnt(masm_, "[ VariableDeclaration");
848 __ LoadRoot(a4, Heap::kTheHoleValueRootIndex);
849 __ sd(a4, StackOperand(variable));
853 case VariableLocation::CONTEXT:
855 Comment cmnt(masm_, "[ VariableDeclaration");
856 EmitDebugCheckDeclarationContext(variable);
857 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
858 __ sd(at, ContextOperand(cp, variable->index()));
859 // No write barrier since the_hole_value is in old space.
860 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
864 case VariableLocation::LOOKUP: {
865 Comment cmnt(masm_, "[ VariableDeclaration");
866 __ li(a2, Operand(variable->name()));
867 // Declaration nodes are always introduced in one of four modes.
868 DCHECK(IsDeclaredVariableMode(mode));
869 // Push initial value, if any.
870 // Note: For variables we must not push an initial value (such as
871 // 'undefined') because we may have a (legal) redeclaration and we
872 // must not destroy the current value.
874 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
876 DCHECK(Smi::FromInt(0) == 0);
877 __ mov(a0, zero_reg); // Smi::FromInt(0) indicates no initial value.
880 __ CallRuntime(IsImmutableVariableMode(mode)
881 ? Runtime::kDeclareReadOnlyLookupSlot
882 : Runtime::kDeclareLookupSlot,
890 void FullCodeGenerator::VisitFunctionDeclaration(
891 FunctionDeclaration* declaration) {
892 VariableProxy* proxy = declaration->proxy();
893 Variable* variable = proxy->var();
894 switch (variable->location()) {
895 case VariableLocation::GLOBAL:
896 case VariableLocation::UNALLOCATED: {
897 globals_->Add(variable->name(), zone());
898 Handle<SharedFunctionInfo> function =
899 Compiler::GetSharedFunctionInfo(declaration->fun(), script(), info_);
900 // Check for stack-overflow exception.
901 if (function.is_null()) return SetStackOverflow();
902 globals_->Add(function, zone());
906 case VariableLocation::PARAMETER:
907 case VariableLocation::LOCAL: {
908 Comment cmnt(masm_, "[ FunctionDeclaration");
909 VisitForAccumulatorValue(declaration->fun());
910 __ sd(result_register(), StackOperand(variable));
914 case VariableLocation::CONTEXT: {
915 Comment cmnt(masm_, "[ FunctionDeclaration");
916 EmitDebugCheckDeclarationContext(variable);
917 VisitForAccumulatorValue(declaration->fun());
918 __ sd(result_register(), ContextOperand(cp, variable->index()));
919 int offset = Context::SlotOffset(variable->index());
920 // We know that we have written a function, which is not a smi.
921 __ RecordWriteContextSlot(cp,
929 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
933 case VariableLocation::LOOKUP: {
934 Comment cmnt(masm_, "[ FunctionDeclaration");
935 __ li(a2, Operand(variable->name()));
937 // Push initial value for function declaration.
938 VisitForStackValue(declaration->fun());
939 __ CallRuntime(Runtime::kDeclareLookupSlot, 2);
946 void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
947 // Call the runtime to declare the globals.
948 __ li(a1, Operand(pairs));
949 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
951 __ CallRuntime(Runtime::kDeclareGlobals, 2);
952 // Return value is ignored.
956 void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
957 // Call the runtime to declare the modules.
958 __ Push(descriptions);
959 __ CallRuntime(Runtime::kDeclareModules, 1);
960 // Return value is ignored.
964 void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
965 Comment cmnt(masm_, "[ SwitchStatement");
966 Breakable nested_statement(this, stmt);
967 SetStatementPosition(stmt);
969 // Keep the switch value on the stack until a case matches.
970 VisitForStackValue(stmt->tag());
971 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
973 ZoneList<CaseClause*>* clauses = stmt->cases();
974 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
976 Label next_test; // Recycled for each test.
977 // Compile all the tests with branches to their bodies.
978 for (int i = 0; i < clauses->length(); i++) {
979 CaseClause* clause = clauses->at(i);
980 clause->body_target()->Unuse();
982 // The default is not a test, but remember it as final fall through.
983 if (clause->is_default()) {
984 default_clause = clause;
988 Comment cmnt(masm_, "[ Case comparison");
992 // Compile the label expression.
993 VisitForAccumulatorValue(clause->label());
994 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
996 // Perform the comparison as if via '==='.
997 __ ld(a1, MemOperand(sp, 0)); // Switch value.
998 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
999 JumpPatchSite patch_site(masm_);
1000 if (inline_smi_code) {
1003 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
1005 __ Branch(&next_test, ne, a1, Operand(a0));
1006 __ Drop(1); // Switch value is no longer needed.
1007 __ Branch(clause->body_target());
1009 __ bind(&slow_case);
1012 // Record position before stub call for type feedback.
1013 SetExpressionPosition(clause);
1014 Handle<Code> ic = CodeFactory::CompareIC(isolate(), Token::EQ_STRICT,
1015 strength(language_mode())).code();
1016 CallIC(ic, clause->CompareId());
1017 patch_site.EmitPatchInfo();
1021 PrepareForBailout(clause, TOS_REG);
1022 __ LoadRoot(at, Heap::kTrueValueRootIndex);
1023 __ Branch(&next_test, ne, v0, Operand(at));
1025 __ Branch(clause->body_target());
1028 __ Branch(&next_test, ne, v0, Operand(zero_reg));
1029 __ Drop(1); // Switch value is no longer needed.
1030 __ Branch(clause->body_target());
1033 // Discard the test value and jump to the default if present, otherwise to
1034 // the end of the statement.
1035 __ bind(&next_test);
1036 __ Drop(1); // Switch value is no longer needed.
1037 if (default_clause == NULL) {
1038 __ Branch(nested_statement.break_label());
1040 __ Branch(default_clause->body_target());
1043 // Compile all the case bodies.
1044 for (int i = 0; i < clauses->length(); i++) {
1045 Comment cmnt(masm_, "[ Case body");
1046 CaseClause* clause = clauses->at(i);
1047 __ bind(clause->body_target());
1048 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
1049 VisitStatements(clause->statements());
1052 __ bind(nested_statement.break_label());
1053 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1057 void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
1058 Comment cmnt(masm_, "[ ForInStatement");
1059 SetStatementPosition(stmt, SKIP_BREAK);
1061 FeedbackVectorSlot slot = stmt->ForInFeedbackSlot();
1064 ForIn loop_statement(this, stmt);
1065 increment_loop_depth();
1067 // Get the object to enumerate over. If the object is null or undefined, skip
1068 // over the loop. See ECMA-262 version 5, section 12.6.4.
1069 SetExpressionAsStatementPosition(stmt->enumerable());
1070 VisitForAccumulatorValue(stmt->enumerable());
1071 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
1072 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1073 __ Branch(&exit, eq, a0, Operand(at));
1074 Register null_value = a5;
1075 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
1076 __ Branch(&exit, eq, a0, Operand(null_value));
1077 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1079 // Convert the object to a JS object.
1080 Label convert, done_convert;
1081 __ JumpIfSmi(a0, &convert);
1082 __ GetObjectType(a0, a1, a1);
1083 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
1086 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1088 __ bind(&done_convert);
1089 PrepareForBailoutForId(stmt->ToObjectId(), TOS_REG);
1092 // Check for proxies.
1094 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1095 __ GetObjectType(a0, a1, a1);
1096 __ Branch(&call_runtime, le, a1, Operand(LAST_JS_PROXY_TYPE));
1098 // Check cache validity in generated code. This is a fast case for
1099 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1100 // guarantee cache validity, call the runtime system to check cache
1101 // validity or get the property names in a fixed array.
1102 __ CheckEnumCache(null_value, &call_runtime);
1104 // The enum cache is valid. Load the map of the object being
1105 // iterated over and use the cache for the iteration.
1107 __ ld(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
1108 __ Branch(&use_cache);
1110 // Get the set of properties to enumerate.
1111 __ bind(&call_runtime);
1112 __ push(a0); // Duplicate the enumerable object on the stack.
1113 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1114 PrepareForBailoutForId(stmt->EnumId(), TOS_REG);
1116 // If we got a map from the runtime call, we can do a fast
1117 // modification check. Otherwise, we got a fixed array, and we have
1118 // to do a slow check.
1120 __ ld(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
1121 __ LoadRoot(at, Heap::kMetaMapRootIndex);
1122 __ Branch(&fixed_array, ne, a2, Operand(at));
1124 // We got a map in register v0. Get the enumeration cache from it.
1125 Label no_descriptors;
1126 __ bind(&use_cache);
1128 __ EnumLength(a1, v0);
1129 __ Branch(&no_descriptors, eq, a1, Operand(Smi::FromInt(0)));
1131 __ LoadInstanceDescriptors(v0, a2);
1132 __ ld(a2, FieldMemOperand(a2, DescriptorArray::kEnumCacheOffset));
1133 __ ld(a2, FieldMemOperand(a2, DescriptorArray::kEnumCacheBridgeCacheOffset));
1135 // Set up the four remaining stack slots.
1136 __ li(a0, Operand(Smi::FromInt(0)));
1137 // Push map, enumeration cache, enumeration cache length (as smi) and zero.
1138 __ Push(v0, a2, a1, a0);
1141 __ bind(&no_descriptors);
1145 // We got a fixed array in register v0. Iterate through that.
1147 __ bind(&fixed_array);
1149 __ li(a1, FeedbackVector());
1150 __ li(a2, Operand(TypeFeedbackVector::MegamorphicSentinel(isolate())));
1151 int vector_index = FeedbackVector()->GetIndex(slot);
1152 __ sd(a2, FieldMemOperand(a1, FixedArray::OffsetOfElementAt(vector_index)));
1154 __ li(a1, Operand(Smi::FromInt(1))); // Smi indicates slow check
1155 __ ld(a2, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1156 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1157 __ GetObjectType(a2, a3, a3);
1158 __ Branch(&non_proxy, gt, a3, Operand(LAST_JS_PROXY_TYPE));
1159 __ li(a1, Operand(Smi::FromInt(0))); // Zero indicates proxy
1160 __ bind(&non_proxy);
1161 __ Push(a1, v0); // Smi and array
1162 __ ld(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1163 __ li(a0, Operand(Smi::FromInt(0)));
1164 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1166 // Generate code for doing the condition check.
1167 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
1169 SetExpressionAsStatementPosition(stmt->each());
1171 // Load the current count to a0, load the length to a1.
1172 __ ld(a0, MemOperand(sp, 0 * kPointerSize));
1173 __ ld(a1, MemOperand(sp, 1 * kPointerSize));
1174 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
1176 // Get the current entry of the array into register a3.
1177 __ ld(a2, MemOperand(sp, 2 * kPointerSize));
1178 __ Daddu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1179 __ SmiScale(a4, a0, kPointerSizeLog2);
1180 __ daddu(a4, a2, a4); // Array base + scaled (smi) index.
1181 __ ld(a3, MemOperand(a4)); // Current entry.
1183 // Get the expected map from the stack or a smi in the
1184 // permanent slow case into register a2.
1185 __ ld(a2, MemOperand(sp, 3 * kPointerSize));
1187 // Check if the expected map still matches that of the enumerable.
1188 // If not, we may have to filter the key.
1190 __ ld(a1, MemOperand(sp, 4 * kPointerSize));
1191 __ ld(a4, FieldMemOperand(a1, HeapObject::kMapOffset));
1192 __ Branch(&update_each, eq, a4, Operand(a2));
1194 // For proxies, no filtering is done.
1195 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1196 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
1197 __ Branch(&update_each, eq, a2, Operand(zero_reg));
1199 // Convert the entry to a string or (smi) 0 if it isn't a property
1200 // any more. If the property has been removed while iterating, we
1202 __ Push(a1, a3); // Enumerable and current entry.
1203 __ CallRuntime(Runtime::kForInFilter, 2);
1204 PrepareForBailoutForId(stmt->FilterId(), TOS_REG);
1205 __ mov(a3, result_register());
1206 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1207 __ Branch(loop_statement.continue_label(), eq, a3, Operand(at));
1209 // Update the 'each' property or variable from the possibly filtered
1210 // entry in register a3.
1211 __ bind(&update_each);
1212 __ mov(result_register(), a3);
1213 // Perform the assignment as if via '='.
1214 { EffectContext context(this);
1215 EmitAssignment(stmt->each(), stmt->EachFeedbackSlot());
1216 PrepareForBailoutForId(stmt->AssignmentId(), NO_REGISTERS);
1219 // Generate code for the body of the loop.
1220 Visit(stmt->body());
1222 // Generate code for the going to the next element by incrementing
1223 // the index (smi) stored on top of the stack.
1224 __ bind(loop_statement.continue_label());
1226 __ Daddu(a0, a0, Operand(Smi::FromInt(1)));
1229 EmitBackEdgeBookkeeping(stmt, &loop);
1232 // Remove the pointers stored on the stack.
1233 __ bind(loop_statement.break_label());
1236 // Exit and decrement the loop depth.
1237 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1239 decrement_loop_depth();
1243 void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1245 // Use the fast case closure allocation code that allocates in new
1246 // space for nested functions that don't need literals cloning. If
1247 // we're running with the --always-opt or the --prepare-always-opt
1248 // flag, we need to use the runtime function so that the new function
1249 // we are creating here gets a chance to have its code optimized and
1250 // doesn't just get a copy of the existing unoptimized code.
1251 if (!FLAG_always_opt &&
1252 !FLAG_prepare_always_opt &&
1254 scope()->is_function_scope() &&
1255 info->num_literals() == 0) {
1256 FastNewClosureStub stub(isolate(), info->language_mode(), info->kind());
1257 __ li(a2, Operand(info));
1260 __ li(a0, Operand(info));
1261 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1262 : Heap::kFalseValueRootIndex);
1263 __ Push(cp, a0, a1);
1264 __ CallRuntime(Runtime::kNewClosure, 3);
1266 context()->Plug(v0);
1270 void FullCodeGenerator::EmitSetHomeObjectIfNeeded(Expression* initializer,
1272 FeedbackVectorICSlot slot) {
1273 if (NeedsHomeObject(initializer)) {
1274 __ ld(StoreDescriptor::ReceiverRegister(), MemOperand(sp));
1275 __ li(StoreDescriptor::NameRegister(),
1276 Operand(isolate()->factory()->home_object_symbol()));
1277 __ ld(StoreDescriptor::ValueRegister(),
1278 MemOperand(sp, offset * kPointerSize));
1279 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
1285 void FullCodeGenerator::EmitLoadGlobalCheckExtensions(VariableProxy* proxy,
1286 TypeofMode typeof_mode,
1288 Register current = cp;
1294 if (s->num_heap_slots() > 0) {
1295 if (s->calls_sloppy_eval()) {
1296 // Check that extension is NULL.
1297 __ ld(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1298 __ Branch(slow, ne, temp, Operand(zero_reg));
1300 // Load next context in chain.
1301 __ ld(next, ContextOperand(current, Context::PREVIOUS_INDEX));
1302 // Walk the rest of the chain without clobbering cp.
1305 // If no outer scope calls eval, we do not need to check more
1306 // context extensions.
1307 if (!s->outer_scope_calls_sloppy_eval() || s->is_eval_scope()) break;
1308 s = s->outer_scope();
1311 if (s->is_eval_scope()) {
1313 if (!current.is(next)) {
1314 __ Move(next, current);
1317 // Terminate at native context.
1318 __ ld(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1319 __ LoadRoot(a4, Heap::kNativeContextMapRootIndex);
1320 __ Branch(&fast, eq, temp, Operand(a4));
1321 // Check that extension is NULL.
1322 __ ld(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1323 __ Branch(slow, ne, temp, Operand(zero_reg));
1324 // Load next context in chain.
1325 __ ld(next, ContextOperand(next, Context::PREVIOUS_INDEX));
1330 // All extension objects were empty and it is safe to use a normal global
1332 EmitGlobalVariableLoad(proxy, typeof_mode);
1336 MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1338 DCHECK(var->IsContextSlot());
1339 Register context = cp;
1343 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
1344 if (s->num_heap_slots() > 0) {
1345 if (s->calls_sloppy_eval()) {
1346 // Check that extension is NULL.
1347 __ ld(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1348 __ Branch(slow, ne, temp, Operand(zero_reg));
1350 __ ld(next, ContextOperand(context, Context::PREVIOUS_INDEX));
1351 // Walk the rest of the chain without clobbering cp.
1355 // Check that last extension is NULL.
1356 __ ld(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1357 __ Branch(slow, ne, temp, Operand(zero_reg));
1359 // This function is used only for loads, not stores, so it's safe to
1360 // return an cp-based operand (the write barrier cannot be allowed to
1361 // destroy the cp register).
1362 return ContextOperand(context, var->index());
1366 void FullCodeGenerator::EmitDynamicLookupFastCase(VariableProxy* proxy,
1367 TypeofMode typeof_mode,
1368 Label* slow, Label* done) {
1369 // Generate fast-case code for variables that might be shadowed by
1370 // eval-introduced variables. Eval is used a lot without
1371 // introducing variables. In those cases, we do not want to
1372 // perform a runtime call for all variables in the scope
1373 // containing the eval.
1374 Variable* var = proxy->var();
1375 if (var->mode() == DYNAMIC_GLOBAL) {
1376 EmitLoadGlobalCheckExtensions(proxy, typeof_mode, slow);
1378 } else if (var->mode() == DYNAMIC_LOCAL) {
1379 Variable* local = var->local_if_not_shadowed();
1380 __ ld(v0, ContextSlotOperandCheckExtensions(local, slow));
1381 if (local->mode() == LET || local->mode() == CONST ||
1382 local->mode() == CONST_LEGACY) {
1383 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1384 __ dsubu(at, v0, at); // Sub as compare: at == 0 on eq.
1385 if (local->mode() == CONST_LEGACY) {
1386 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1387 __ Movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
1388 } else { // LET || CONST
1389 __ Branch(done, ne, at, Operand(zero_reg));
1390 __ li(a0, Operand(var->name()));
1392 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1400 void FullCodeGenerator::EmitGlobalVariableLoad(VariableProxy* proxy,
1401 TypeofMode typeof_mode) {
1402 Variable* var = proxy->var();
1403 DCHECK(var->IsUnallocatedOrGlobalSlot() ||
1404 (var->IsLookupSlot() && var->mode() == DYNAMIC_GLOBAL));
1405 if (var->IsGlobalSlot()) {
1406 DCHECK(var->index() > 0);
1407 DCHECK(var->IsStaticGlobalObjectProperty());
1408 int const slot = var->index();
1409 int const depth = scope()->ContextChainLength(var->scope());
1410 if (depth <= LoadGlobalViaContextStub::kMaximumDepth) {
1411 __ li(LoadGlobalViaContextDescriptor::SlotRegister(), Operand(slot));
1412 LoadGlobalViaContextStub stub(isolate(), depth);
1415 __ Push(Smi::FromInt(slot));
1416 __ CallRuntime(Runtime::kLoadGlobalViaContext, 1);
1420 __ ld(LoadDescriptor::ReceiverRegister(), GlobalObjectOperand());
1421 __ li(LoadDescriptor::NameRegister(), Operand(var->name()));
1422 __ li(LoadDescriptor::SlotRegister(),
1423 Operand(SmiFromSlot(proxy->VariableFeedbackSlot())));
1424 CallLoadIC(typeof_mode);
1429 void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy,
1430 TypeofMode typeof_mode) {
1431 // Record position before possible IC call.
1432 SetExpressionPosition(proxy);
1433 PrepareForBailoutForId(proxy->BeforeId(), NO_REGISTERS);
1434 Variable* var = proxy->var();
1436 // Three cases: global variables, lookup variables, and all other types of
1438 switch (var->location()) {
1439 case VariableLocation::GLOBAL:
1440 case VariableLocation::UNALLOCATED: {
1441 Comment cmnt(masm_, "[ Global variable");
1442 EmitGlobalVariableLoad(proxy, typeof_mode);
1443 context()->Plug(v0);
1447 case VariableLocation::PARAMETER:
1448 case VariableLocation::LOCAL:
1449 case VariableLocation::CONTEXT: {
1450 DCHECK_EQ(NOT_INSIDE_TYPEOF, typeof_mode);
1451 Comment cmnt(masm_, var->IsContextSlot() ? "[ Context variable"
1452 : "[ Stack variable");
1453 if (var->binding_needs_init()) {
1454 // var->scope() may be NULL when the proxy is located in eval code and
1455 // refers to a potential outside binding. Currently those bindings are
1456 // always looked up dynamically, i.e. in that case
1457 // var->location() == LOOKUP.
1459 DCHECK(var->scope() != NULL);
1461 // Check if the binding really needs an initialization check. The check
1462 // can be skipped in the following situation: we have a LET or CONST
1463 // binding in harmony mode, both the Variable and the VariableProxy have
1464 // the same declaration scope (i.e. they are both in global code, in the
1465 // same function or in the same eval code) and the VariableProxy is in
1466 // the source physically located after the initializer of the variable.
1468 // We cannot skip any initialization checks for CONST in non-harmony
1469 // mode because const variables may be declared but never initialized:
1470 // if (false) { const x; }; var y = x;
1472 // The condition on the declaration scopes is a conservative check for
1473 // nested functions that access a binding and are called before the
1474 // binding is initialized:
1475 // function() { f(); let x = 1; function f() { x = 2; } }
1477 bool skip_init_check;
1478 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1479 skip_init_check = false;
1480 } else if (var->is_this()) {
1481 CHECK(info_->function() != nullptr &&
1482 (info_->function()->kind() & kSubclassConstructor) != 0);
1483 // TODO(dslomov): implement 'this' hole check elimination.
1484 skip_init_check = false;
1486 // Check that we always have valid source position.
1487 DCHECK(var->initializer_position() != RelocInfo::kNoPosition);
1488 DCHECK(proxy->position() != RelocInfo::kNoPosition);
1489 skip_init_check = var->mode() != CONST_LEGACY &&
1490 var->initializer_position() < proxy->position();
1493 if (!skip_init_check) {
1494 // Let and const need a read barrier.
1496 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1497 __ dsubu(at, v0, at); // Sub as compare: at == 0 on eq.
1498 if (var->mode() == LET || var->mode() == CONST) {
1499 // Throw a reference error when using an uninitialized let/const
1500 // binding in harmony mode.
1502 __ Branch(&done, ne, at, Operand(zero_reg));
1503 __ li(a0, Operand(var->name()));
1505 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1508 // Uninitalized const bindings outside of harmony mode are unholed.
1509 DCHECK(var->mode() == CONST_LEGACY);
1510 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1511 __ Movz(v0, a0, at); // Conditional move: Undefined if TheHole.
1513 context()->Plug(v0);
1517 context()->Plug(var);
1521 case VariableLocation::LOOKUP: {
1522 Comment cmnt(masm_, "[ Lookup variable");
1524 // Generate code for loading from variables potentially shadowed
1525 // by eval-introduced variables.
1526 EmitDynamicLookupFastCase(proxy, typeof_mode, &slow, &done);
1528 __ li(a1, Operand(var->name()));
1529 __ Push(cp, a1); // Context and name.
1530 Runtime::FunctionId function_id =
1531 typeof_mode == NOT_INSIDE_TYPEOF
1532 ? Runtime::kLoadLookupSlot
1533 : Runtime::kLoadLookupSlotNoReferenceError;
1534 __ CallRuntime(function_id, 2);
1536 context()->Plug(v0);
1542 void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1543 Comment cmnt(masm_, "[ RegExpLiteral");
1545 // Registers will be used as follows:
1546 // a5 = materialized value (RegExp literal)
1547 // a4 = JS function, literals array
1548 // a3 = literal index
1549 // a2 = RegExp pattern
1550 // a1 = RegExp flags
1551 // a0 = RegExp literal clone
1552 __ ld(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1553 __ ld(a4, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1554 int literal_offset =
1555 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1556 __ ld(a5, FieldMemOperand(a4, literal_offset));
1557 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1558 __ Branch(&materialized, ne, a5, Operand(at));
1560 // Create regexp literal using runtime function.
1561 // Result will be in v0.
1562 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1563 __ li(a2, Operand(expr->pattern()));
1564 __ li(a1, Operand(expr->flags()));
1565 __ Push(a4, a3, a2, a1);
1566 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1569 __ bind(&materialized);
1570 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1571 Label allocated, runtime_allocate;
1572 __ Allocate(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1575 __ bind(&runtime_allocate);
1576 __ li(a0, Operand(Smi::FromInt(size)));
1578 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1581 __ bind(&allocated);
1583 // After this, registers are used as follows:
1584 // v0: Newly allocated regexp.
1585 // a5: Materialized regexp.
1587 __ CopyFields(v0, a5, a2.bit(), size / kPointerSize);
1588 context()->Plug(v0);
1592 void FullCodeGenerator::EmitAccessor(Expression* expression) {
1593 if (expression == NULL) {
1594 __ LoadRoot(a1, Heap::kNullValueRootIndex);
1597 VisitForStackValue(expression);
1602 void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1603 Comment cmnt(masm_, "[ ObjectLiteral");
1605 Handle<FixedArray> constant_properties = expr->constant_properties();
1606 __ ld(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1607 __ ld(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1608 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1609 __ li(a1, Operand(constant_properties));
1610 __ li(a0, Operand(Smi::FromInt(expr->ComputeFlags())));
1611 if (MustCreateObjectLiteralWithRuntime(expr)) {
1612 __ Push(a3, a2, a1, a0);
1613 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1615 FastCloneShallowObjectStub stub(isolate(), expr->properties_count());
1618 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1620 // If result_saved is true the result is on top of the stack. If
1621 // result_saved is false the result is in v0.
1622 bool result_saved = false;
1624 AccessorTable accessor_table(zone());
1625 int property_index = 0;
1626 // store_slot_index points to the vector IC slot for the next store IC used.
1627 // ObjectLiteral::ComputeFeedbackRequirements controls the allocation of slots
1628 // and must be updated if the number of store ICs emitted here changes.
1629 int store_slot_index = 0;
1630 for (; property_index < expr->properties()->length(); property_index++) {
1631 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1632 if (property->is_computed_name()) break;
1633 if (property->IsCompileTimeValue()) continue;
1635 Literal* key = property->key()->AsLiteral();
1636 Expression* value = property->value();
1637 if (!result_saved) {
1638 __ push(v0); // Save result on stack.
1639 result_saved = true;
1641 switch (property->kind()) {
1642 case ObjectLiteral::Property::CONSTANT:
1644 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1645 DCHECK(!CompileTimeValue::IsCompileTimeValue(property->value()));
1647 case ObjectLiteral::Property::COMPUTED:
1648 // It is safe to use [[Put]] here because the boilerplate already
1649 // contains computed properties with an uninitialized value.
1650 if (key->value()->IsInternalizedString()) {
1651 if (property->emit_store()) {
1652 VisitForAccumulatorValue(value);
1653 __ mov(StoreDescriptor::ValueRegister(), result_register());
1654 DCHECK(StoreDescriptor::ValueRegister().is(a0));
1655 __ li(StoreDescriptor::NameRegister(), Operand(key->value()));
1656 __ ld(StoreDescriptor::ReceiverRegister(), MemOperand(sp));
1657 if (FLAG_vector_stores) {
1658 EmitLoadStoreICSlot(expr->GetNthSlot(store_slot_index++));
1661 CallStoreIC(key->LiteralFeedbackId());
1663 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1665 if (NeedsHomeObject(value)) {
1666 __ Move(StoreDescriptor::ReceiverRegister(), v0);
1667 __ li(StoreDescriptor::NameRegister(),
1668 Operand(isolate()->factory()->home_object_symbol()));
1669 __ ld(StoreDescriptor::ValueRegister(), MemOperand(sp));
1670 if (FLAG_vector_stores) {
1671 EmitLoadStoreICSlot(expr->GetNthSlot(store_slot_index++));
1676 VisitForEffect(value);
1680 // Duplicate receiver on stack.
1681 __ ld(a0, MemOperand(sp));
1683 VisitForStackValue(key);
1684 VisitForStackValue(value);
1685 if (property->emit_store()) {
1686 EmitSetHomeObjectIfNeeded(
1687 value, 2, expr->SlotForHomeObject(value, &store_slot_index));
1688 __ li(a0, Operand(Smi::FromInt(SLOPPY))); // PropertyAttributes.
1690 __ CallRuntime(Runtime::kSetProperty, 4);
1695 case ObjectLiteral::Property::PROTOTYPE:
1696 // Duplicate receiver on stack.
1697 __ ld(a0, MemOperand(sp));
1699 VisitForStackValue(value);
1700 DCHECK(property->emit_store());
1701 __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1703 case ObjectLiteral::Property::GETTER:
1704 if (property->emit_store()) {
1705 accessor_table.lookup(key)->second->getter = value;
1708 case ObjectLiteral::Property::SETTER:
1709 if (property->emit_store()) {
1710 accessor_table.lookup(key)->second->setter = value;
1716 // Emit code to define accessors, using only a single call to the runtime for
1717 // each pair of corresponding getters and setters.
1718 for (AccessorTable::Iterator it = accessor_table.begin();
1719 it != accessor_table.end();
1721 __ ld(a0, MemOperand(sp)); // Duplicate receiver.
1723 VisitForStackValue(it->first);
1724 EmitAccessor(it->second->getter);
1725 EmitSetHomeObjectIfNeeded(
1726 it->second->getter, 2,
1727 expr->SlotForHomeObject(it->second->getter, &store_slot_index));
1728 EmitAccessor(it->second->setter);
1729 EmitSetHomeObjectIfNeeded(
1730 it->second->setter, 3,
1731 expr->SlotForHomeObject(it->second->setter, &store_slot_index));
1732 __ li(a0, Operand(Smi::FromInt(NONE)));
1734 __ CallRuntime(Runtime::kDefineAccessorPropertyUnchecked, 5);
1737 // Object literals have two parts. The "static" part on the left contains no
1738 // computed property names, and so we can compute its map ahead of time; see
1739 // runtime.cc::CreateObjectLiteralBoilerplate. The second "dynamic" part
1740 // starts with the first computed property name, and continues with all
1741 // properties to its right. All the code from above initializes the static
1742 // component of the object literal, and arranges for the map of the result to
1743 // reflect the static order in which the keys appear. For the dynamic
1744 // properties, we compile them into a series of "SetOwnProperty" runtime
1745 // calls. This will preserve insertion order.
1746 for (; property_index < expr->properties()->length(); property_index++) {
1747 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1749 Expression* value = property->value();
1750 if (!result_saved) {
1751 __ push(v0); // Save result on the stack
1752 result_saved = true;
1755 __ ld(a0, MemOperand(sp)); // Duplicate receiver.
1758 if (property->kind() == ObjectLiteral::Property::PROTOTYPE) {
1759 DCHECK(!property->is_computed_name());
1760 VisitForStackValue(value);
1761 DCHECK(property->emit_store());
1762 __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1764 EmitPropertyKey(property, expr->GetIdForProperty(property_index));
1765 VisitForStackValue(value);
1766 EmitSetHomeObjectIfNeeded(
1767 value, 2, expr->SlotForHomeObject(value, &store_slot_index));
1769 switch (property->kind()) {
1770 case ObjectLiteral::Property::CONSTANT:
1771 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1772 case ObjectLiteral::Property::COMPUTED:
1773 if (property->emit_store()) {
1774 __ li(a0, Operand(Smi::FromInt(NONE)));
1776 __ CallRuntime(Runtime::kDefineDataPropertyUnchecked, 4);
1782 case ObjectLiteral::Property::PROTOTYPE:
1786 case ObjectLiteral::Property::GETTER:
1787 __ li(a0, Operand(Smi::FromInt(NONE)));
1789 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
1792 case ObjectLiteral::Property::SETTER:
1793 __ li(a0, Operand(Smi::FromInt(NONE)));
1795 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
1801 if (expr->has_function()) {
1802 DCHECK(result_saved);
1803 __ ld(a0, MemOperand(sp));
1805 __ CallRuntime(Runtime::kToFastProperties, 1);
1809 context()->PlugTOS();
1811 context()->Plug(v0);
1814 // Verify that compilation exactly consumed the number of store ic slots that
1815 // the ObjectLiteral node had to offer.
1816 DCHECK(!FLAG_vector_stores || store_slot_index == expr->slot_count());
1820 void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1821 Comment cmnt(masm_, "[ ArrayLiteral");
1823 expr->BuildConstantElements(isolate());
1825 Handle<FixedArray> constant_elements = expr->constant_elements();
1826 bool has_fast_elements =
1827 IsFastObjectElementsKind(expr->constant_elements_kind());
1829 AllocationSiteMode allocation_site_mode = TRACK_ALLOCATION_SITE;
1830 if (has_fast_elements && !FLAG_allocation_site_pretenuring) {
1831 // If the only customer of allocation sites is transitioning, then
1832 // we can turn it off if we don't have anywhere else to transition to.
1833 allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
1836 __ mov(a0, result_register());
1837 __ ld(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1838 __ ld(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1839 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1840 __ li(a1, Operand(constant_elements));
1841 if (MustCreateArrayLiteralWithRuntime(expr)) {
1842 __ li(a0, Operand(Smi::FromInt(expr->ComputeFlags())));
1843 __ Push(a3, a2, a1, a0);
1844 __ CallRuntime(Runtime::kCreateArrayLiteral, 4);
1846 FastCloneShallowArrayStub stub(isolate(), allocation_site_mode);
1849 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1851 bool result_saved = false; // Is the result saved to the stack?
1852 ZoneList<Expression*>* subexprs = expr->values();
1853 int length = subexprs->length();
1855 // Emit code to evaluate all the non-constant subexpressions and to store
1856 // them into the newly cloned array.
1857 int array_index = 0;
1858 for (; array_index < length; array_index++) {
1859 Expression* subexpr = subexprs->at(array_index);
1860 if (subexpr->IsSpread()) break;
1862 // If the subexpression is a literal or a simple materialized literal it
1863 // is already set in the cloned array.
1864 if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
1866 if (!result_saved) {
1867 __ push(v0); // array literal
1868 __ Push(Smi::FromInt(expr->literal_index()));
1869 result_saved = true;
1872 VisitForAccumulatorValue(subexpr);
1874 if (has_fast_elements) {
1875 int offset = FixedArray::kHeaderSize + (array_index * kPointerSize);
1876 __ ld(a6, MemOperand(sp, kPointerSize)); // Copy of array literal.
1877 __ ld(a1, FieldMemOperand(a6, JSObject::kElementsOffset));
1878 __ sd(result_register(), FieldMemOperand(a1, offset));
1879 // Update the write barrier for the array store.
1880 __ RecordWriteField(a1, offset, result_register(), a2,
1881 kRAHasBeenSaved, kDontSaveFPRegs,
1882 EMIT_REMEMBERED_SET, INLINE_SMI_CHECK);
1884 __ li(a3, Operand(Smi::FromInt(array_index)));
1885 __ mov(a0, result_register());
1886 StoreArrayLiteralElementStub stub(isolate());
1890 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1893 // In case the array literal contains spread expressions it has two parts. The
1894 // first part is the "static" array which has a literal index is handled
1895 // above. The second part is the part after the first spread expression
1896 // (inclusive) and these elements gets appended to the array. Note that the
1897 // number elements an iterable produces is unknown ahead of time.
1898 if (array_index < length && result_saved) {
1899 __ Pop(); // literal index
1901 result_saved = false;
1903 for (; array_index < length; array_index++) {
1904 Expression* subexpr = subexprs->at(array_index);
1907 if (subexpr->IsSpread()) {
1908 VisitForStackValue(subexpr->AsSpread()->expression());
1909 __ InvokeBuiltin(Builtins::CONCAT_ITERABLE_TO_ARRAY, CALL_FUNCTION);
1911 VisitForStackValue(subexpr);
1912 __ CallRuntime(Runtime::kAppendElement, 2);
1915 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1919 __ Pop(); // literal index
1920 context()->PlugTOS();
1922 context()->Plug(v0);
1927 void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1928 DCHECK(expr->target()->IsValidReferenceExpressionOrThis());
1930 Comment cmnt(masm_, "[ Assignment");
1931 SetExpressionPosition(expr, INSERT_BREAK);
1933 Property* property = expr->target()->AsProperty();
1934 LhsKind assign_type = Property::GetAssignType(property);
1936 // Evaluate LHS expression.
1937 switch (assign_type) {
1939 // Nothing to do here.
1941 case NAMED_PROPERTY:
1942 if (expr->is_compound()) {
1943 // We need the receiver both on the stack and in the register.
1944 VisitForStackValue(property->obj());
1945 __ ld(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
1947 VisitForStackValue(property->obj());
1950 case NAMED_SUPER_PROPERTY:
1952 property->obj()->AsSuperPropertyReference()->this_var());
1953 VisitForAccumulatorValue(
1954 property->obj()->AsSuperPropertyReference()->home_object());
1955 __ Push(result_register());
1956 if (expr->is_compound()) {
1957 const Register scratch = a1;
1958 __ ld(scratch, MemOperand(sp, kPointerSize));
1959 __ Push(scratch, result_register());
1962 case KEYED_SUPER_PROPERTY: {
1963 const Register scratch = a1;
1965 property->obj()->AsSuperPropertyReference()->this_var());
1966 VisitForAccumulatorValue(
1967 property->obj()->AsSuperPropertyReference()->home_object());
1968 __ Move(scratch, result_register());
1969 VisitForAccumulatorValue(property->key());
1970 __ Push(scratch, result_register());
1971 if (expr->is_compound()) {
1972 const Register scratch1 = a4;
1973 __ ld(scratch1, MemOperand(sp, 2 * kPointerSize));
1974 __ Push(scratch1, scratch, result_register());
1978 case KEYED_PROPERTY:
1979 // We need the key and receiver on both the stack and in v0 and a1.
1980 if (expr->is_compound()) {
1981 VisitForStackValue(property->obj());
1982 VisitForStackValue(property->key());
1983 __ ld(LoadDescriptor::ReceiverRegister(),
1984 MemOperand(sp, 1 * kPointerSize));
1985 __ ld(LoadDescriptor::NameRegister(), MemOperand(sp, 0));
1987 VisitForStackValue(property->obj());
1988 VisitForStackValue(property->key());
1993 // For compound assignments we need another deoptimization point after the
1994 // variable/property load.
1995 if (expr->is_compound()) {
1996 { AccumulatorValueContext context(this);
1997 switch (assign_type) {
1999 EmitVariableLoad(expr->target()->AsVariableProxy());
2000 PrepareForBailout(expr->target(), TOS_REG);
2002 case NAMED_PROPERTY:
2003 EmitNamedPropertyLoad(property);
2004 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2006 case NAMED_SUPER_PROPERTY:
2007 EmitNamedSuperPropertyLoad(property);
2008 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2010 case KEYED_SUPER_PROPERTY:
2011 EmitKeyedSuperPropertyLoad(property);
2012 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2014 case KEYED_PROPERTY:
2015 EmitKeyedPropertyLoad(property);
2016 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2021 Token::Value op = expr->binary_op();
2022 __ push(v0); // Left operand goes on the stack.
2023 VisitForAccumulatorValue(expr->value());
2025 AccumulatorValueContext context(this);
2026 if (ShouldInlineSmiCase(op)) {
2027 EmitInlineSmiBinaryOp(expr->binary_operation(),
2032 EmitBinaryOp(expr->binary_operation(), op);
2035 // Deoptimization point in case the binary operation may have side effects.
2036 PrepareForBailout(expr->binary_operation(), TOS_REG);
2038 VisitForAccumulatorValue(expr->value());
2041 SetExpressionPosition(expr);
2044 switch (assign_type) {
2046 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
2047 expr->op(), expr->AssignmentSlot());
2048 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2049 context()->Plug(v0);
2051 case NAMED_PROPERTY:
2052 EmitNamedPropertyAssignment(expr);
2054 case NAMED_SUPER_PROPERTY:
2055 EmitNamedSuperPropertyStore(property);
2056 context()->Plug(v0);
2058 case KEYED_SUPER_PROPERTY:
2059 EmitKeyedSuperPropertyStore(property);
2060 context()->Plug(v0);
2062 case KEYED_PROPERTY:
2063 EmitKeyedPropertyAssignment(expr);
2069 void FullCodeGenerator::VisitYield(Yield* expr) {
2070 Comment cmnt(masm_, "[ Yield");
2071 SetExpressionPosition(expr);
2073 // Evaluate yielded value first; the initial iterator definition depends on
2074 // this. It stays on the stack while we update the iterator.
2075 VisitForStackValue(expr->expression());
2077 switch (expr->yield_kind()) {
2078 case Yield::kSuspend:
2079 // Pop value from top-of-stack slot; box result into result register.
2080 EmitCreateIteratorResult(false);
2081 __ push(result_register());
2083 case Yield::kInitial: {
2084 Label suspend, continuation, post_runtime, resume;
2087 __ bind(&continuation);
2088 __ RecordGeneratorContinuation();
2092 VisitForAccumulatorValue(expr->generator_object());
2093 DCHECK(continuation.pos() > 0 && Smi::IsValid(continuation.pos()));
2094 __ li(a1, Operand(Smi::FromInt(continuation.pos())));
2095 __ sd(a1, FieldMemOperand(v0, JSGeneratorObject::kContinuationOffset));
2096 __ sd(cp, FieldMemOperand(v0, JSGeneratorObject::kContextOffset));
2098 __ RecordWriteField(v0, JSGeneratorObject::kContextOffset, a1, a2,
2099 kRAHasBeenSaved, kDontSaveFPRegs);
2100 __ Daddu(a1, fp, Operand(StandardFrameConstants::kExpressionsOffset));
2101 __ Branch(&post_runtime, eq, sp, Operand(a1));
2102 __ push(v0); // generator object
2103 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
2104 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2105 __ bind(&post_runtime);
2106 __ pop(result_register());
2107 EmitReturnSequence();
2110 context()->Plug(result_register());
2114 case Yield::kFinal: {
2115 VisitForAccumulatorValue(expr->generator_object());
2116 __ li(a1, Operand(Smi::FromInt(JSGeneratorObject::kGeneratorClosed)));
2117 __ sd(a1, FieldMemOperand(result_register(),
2118 JSGeneratorObject::kContinuationOffset));
2119 // Pop value from top-of-stack slot, box result into result register.
2120 EmitCreateIteratorResult(true);
2121 EmitUnwindBeforeReturn();
2122 EmitReturnSequence();
2126 case Yield::kDelegating: {
2127 VisitForStackValue(expr->generator_object());
2129 // Initial stack layout is as follows:
2130 // [sp + 1 * kPointerSize] iter
2131 // [sp + 0 * kPointerSize] g
2133 Label l_catch, l_try, l_suspend, l_continuation, l_resume;
2134 Label l_next, l_call;
2135 Register load_receiver = LoadDescriptor::ReceiverRegister();
2136 Register load_name = LoadDescriptor::NameRegister();
2137 // Initial send value is undefined.
2138 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
2141 // catch (e) { receiver = iter; f = 'throw'; arg = e; goto l_call; }
2144 __ LoadRoot(a2, Heap::kthrow_stringRootIndex); // "throw"
2145 __ ld(a3, MemOperand(sp, 1 * kPointerSize)); // iter
2146 __ Push(a2, a3, a0); // "throw", iter, except
2149 // try { received = %yield result }
2150 // Shuffle the received result above a try handler and yield it without
2153 __ pop(a0); // result
2154 int handler_index = NewHandlerTableEntry();
2155 EnterTryBlock(handler_index, &l_catch);
2156 const int try_block_size = TryCatch::kElementCount * kPointerSize;
2157 __ push(a0); // result
2160 __ bind(&l_continuation);
2161 __ RecordGeneratorContinuation();
2165 __ bind(&l_suspend);
2166 const int generator_object_depth = kPointerSize + try_block_size;
2167 __ ld(a0, MemOperand(sp, generator_object_depth));
2169 __ Push(Smi::FromInt(handler_index)); // handler-index
2170 DCHECK(l_continuation.pos() > 0 && Smi::IsValid(l_continuation.pos()));
2171 __ li(a1, Operand(Smi::FromInt(l_continuation.pos())));
2172 __ sd(a1, FieldMemOperand(a0, JSGeneratorObject::kContinuationOffset));
2173 __ sd(cp, FieldMemOperand(a0, JSGeneratorObject::kContextOffset));
2175 __ RecordWriteField(a0, JSGeneratorObject::kContextOffset, a1, a2,
2176 kRAHasBeenSaved, kDontSaveFPRegs);
2177 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 2);
2178 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2179 __ pop(v0); // result
2180 EmitReturnSequence();
2182 __ bind(&l_resume); // received in a0
2183 ExitTryBlock(handler_index);
2185 // receiver = iter; f = 'next'; arg = received;
2187 __ LoadRoot(load_name, Heap::knext_stringRootIndex); // "next"
2188 __ ld(a3, MemOperand(sp, 1 * kPointerSize)); // iter
2189 __ Push(load_name, a3, a0); // "next", iter, received
2191 // result = receiver[f](arg);
2193 __ ld(load_receiver, MemOperand(sp, kPointerSize));
2194 __ ld(load_name, MemOperand(sp, 2 * kPointerSize));
2195 __ li(LoadDescriptor::SlotRegister(),
2196 Operand(SmiFromSlot(expr->KeyedLoadFeedbackSlot())));
2197 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), SLOPPY).code();
2198 CallIC(ic, TypeFeedbackId::None());
2201 __ sd(a1, MemOperand(sp, 2 * kPointerSize));
2202 SetCallPosition(expr, 1);
2203 CallFunctionStub stub(isolate(), 1, CALL_AS_METHOD);
2206 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2207 __ Drop(1); // The function is still on the stack; drop it.
2209 // if (!result.done) goto l_try;
2210 __ Move(load_receiver, v0);
2212 __ push(load_receiver); // save result
2213 __ LoadRoot(load_name, Heap::kdone_stringRootIndex); // "done"
2214 __ li(LoadDescriptor::SlotRegister(),
2215 Operand(SmiFromSlot(expr->DoneFeedbackSlot())));
2216 CallLoadIC(NOT_INSIDE_TYPEOF); // v0=result.done
2218 Handle<Code> bool_ic = ToBooleanStub::GetUninitialized(isolate());
2220 __ Branch(&l_try, eq, v0, Operand(zero_reg));
2223 __ pop(load_receiver); // result
2224 __ LoadRoot(load_name, Heap::kvalue_stringRootIndex); // "value"
2225 __ li(LoadDescriptor::SlotRegister(),
2226 Operand(SmiFromSlot(expr->ValueFeedbackSlot())));
2227 CallLoadIC(NOT_INSIDE_TYPEOF); // v0=result.value
2228 context()->DropAndPlug(2, v0); // drop iter and g
2235 void FullCodeGenerator::EmitGeneratorResume(Expression *generator,
2237 JSGeneratorObject::ResumeMode resume_mode) {
2238 // The value stays in a0, and is ultimately read by the resumed generator, as
2239 // if CallRuntime(Runtime::kSuspendJSGeneratorObject) returned it. Or it
2240 // is read to throw the value when the resumed generator is already closed.
2241 // a1 will hold the generator object until the activation has been resumed.
2242 VisitForStackValue(generator);
2243 VisitForAccumulatorValue(value);
2246 // Load suspended function and context.
2247 __ ld(cp, FieldMemOperand(a1, JSGeneratorObject::kContextOffset));
2248 __ ld(a4, FieldMemOperand(a1, JSGeneratorObject::kFunctionOffset));
2250 // Load receiver and store as the first argument.
2251 __ ld(a2, FieldMemOperand(a1, JSGeneratorObject::kReceiverOffset));
2254 // Push holes for the rest of the arguments to the generator function.
2255 __ ld(a3, FieldMemOperand(a4, JSFunction::kSharedFunctionInfoOffset));
2256 // The argument count is stored as int32_t on 64-bit platforms.
2257 // TODO(plind): Smi on 32-bit platforms.
2259 FieldMemOperand(a3, SharedFunctionInfo::kFormalParameterCountOffset));
2260 __ LoadRoot(a2, Heap::kTheHoleValueRootIndex);
2261 Label push_argument_holes, push_frame;
2262 __ bind(&push_argument_holes);
2263 __ Dsubu(a3, a3, Operand(1));
2264 __ Branch(&push_frame, lt, a3, Operand(zero_reg));
2266 __ jmp(&push_argument_holes);
2268 // Enter a new JavaScript frame, and initialize its slots as they were when
2269 // the generator was suspended.
2270 Label resume_frame, done;
2271 __ bind(&push_frame);
2272 __ Call(&resume_frame);
2274 __ bind(&resume_frame);
2275 // ra = return address.
2276 // fp = caller's frame pointer.
2277 // cp = callee's context,
2278 // a4 = callee's JS function.
2279 __ Push(ra, fp, cp, a4);
2280 // Adjust FP to point to saved FP.
2281 __ Daddu(fp, sp, 2 * kPointerSize);
2283 // Load the operand stack size.
2284 __ ld(a3, FieldMemOperand(a1, JSGeneratorObject::kOperandStackOffset));
2285 __ ld(a3, FieldMemOperand(a3, FixedArray::kLengthOffset));
2288 // If we are sending a value and there is no operand stack, we can jump back
2290 if (resume_mode == JSGeneratorObject::NEXT) {
2292 __ Branch(&slow_resume, ne, a3, Operand(zero_reg));
2293 __ ld(a3, FieldMemOperand(a4, JSFunction::kCodeEntryOffset));
2294 __ ld(a2, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
2296 __ Daddu(a3, a3, Operand(a2));
2297 __ li(a2, Operand(Smi::FromInt(JSGeneratorObject::kGeneratorExecuting)));
2298 __ sd(a2, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
2300 __ bind(&slow_resume);
2303 // Otherwise, we push holes for the operand stack and call the runtime to fix
2304 // up the stack and the handlers.
2305 Label push_operand_holes, call_resume;
2306 __ bind(&push_operand_holes);
2307 __ Dsubu(a3, a3, Operand(1));
2308 __ Branch(&call_resume, lt, a3, Operand(zero_reg));
2310 __ Branch(&push_operand_holes);
2311 __ bind(&call_resume);
2312 DCHECK(!result_register().is(a1));
2313 __ Push(a1, result_register());
2314 __ Push(Smi::FromInt(resume_mode));
2315 __ CallRuntime(Runtime::kResumeJSGeneratorObject, 3);
2316 // Not reached: the runtime call returns elsewhere.
2317 __ stop("not-reached");
2320 context()->Plug(result_register());
2324 void FullCodeGenerator::EmitCreateIteratorResult(bool done) {
2328 const int instance_size = 5 * kPointerSize;
2329 DCHECK_EQ(isolate()->native_context()->iterator_result_map()->instance_size(),
2332 __ Allocate(instance_size, v0, a2, a3, &gc_required, TAG_OBJECT);
2335 __ bind(&gc_required);
2336 __ Push(Smi::FromInt(instance_size));
2337 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
2338 __ ld(context_register(),
2339 MemOperand(fp, StandardFrameConstants::kContextOffset));
2341 __ bind(&allocated);
2342 __ ld(a1, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
2343 __ ld(a1, FieldMemOperand(a1, GlobalObject::kNativeContextOffset));
2344 __ ld(a1, ContextOperand(a1, Context::ITERATOR_RESULT_MAP_INDEX));
2346 __ li(a3, Operand(isolate()->factory()->ToBoolean(done)));
2347 __ li(a4, Operand(isolate()->factory()->empty_fixed_array()));
2348 __ sd(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2349 __ sd(a4, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2350 __ sd(a4, FieldMemOperand(v0, JSObject::kElementsOffset));
2352 FieldMemOperand(v0, JSGeneratorObject::kResultValuePropertyOffset));
2354 FieldMemOperand(v0, JSGeneratorObject::kResultDonePropertyOffset));
2356 // Only the value field needs a write barrier, as the other values are in the
2358 __ RecordWriteField(v0, JSGeneratorObject::kResultValuePropertyOffset,
2359 a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
2363 void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
2364 SetExpressionPosition(prop);
2365 Literal* key = prop->key()->AsLiteral();
2366 DCHECK(!prop->IsSuperAccess());
2368 __ li(LoadDescriptor::NameRegister(), Operand(key->value()));
2369 __ li(LoadDescriptor::SlotRegister(),
2370 Operand(SmiFromSlot(prop->PropertyFeedbackSlot())));
2371 CallLoadIC(NOT_INSIDE_TYPEOF, language_mode());
2375 void FullCodeGenerator::EmitNamedSuperPropertyLoad(Property* prop) {
2376 // Stack: receiver, home_object.
2377 SetExpressionPosition(prop);
2379 Literal* key = prop->key()->AsLiteral();
2380 DCHECK(!key->value()->IsSmi());
2381 DCHECK(prop->IsSuperAccess());
2383 __ Push(key->value());
2384 __ Push(Smi::FromInt(language_mode()));
2385 __ CallRuntime(Runtime::kLoadFromSuper, 4);
2389 void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
2390 // Call keyed load IC. It has register arguments receiver and key.
2391 SetExpressionPosition(prop);
2393 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), language_mode()).code();
2394 __ li(LoadDescriptor::SlotRegister(),
2395 Operand(SmiFromSlot(prop->PropertyFeedbackSlot())));
2400 void FullCodeGenerator::EmitKeyedSuperPropertyLoad(Property* prop) {
2401 // Stack: receiver, home_object, key.
2402 SetExpressionPosition(prop);
2403 __ Push(Smi::FromInt(language_mode()));
2404 __ CallRuntime(Runtime::kLoadKeyedFromSuper, 4);
2408 void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
2410 Expression* left_expr,
2411 Expression* right_expr) {
2412 Label done, smi_case, stub_call;
2414 Register scratch1 = a2;
2415 Register scratch2 = a3;
2417 // Get the arguments.
2419 Register right = a0;
2421 __ mov(a0, result_register());
2423 // Perform combined smi check on both operands.
2424 __ Or(scratch1, left, Operand(right));
2425 STATIC_ASSERT(kSmiTag == 0);
2426 JumpPatchSite patch_site(masm_);
2427 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
2429 __ bind(&stub_call);
2431 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2432 CallIC(code, expr->BinaryOperationFeedbackId());
2433 patch_site.EmitPatchInfo();
2437 // Smi case. This code works the same way as the smi-smi case in the type
2438 // recording binary operation stub, see
2441 __ GetLeastBitsFromSmi(scratch1, right, 5);
2442 __ dsrav(right, left, scratch1);
2443 __ And(v0, right, Operand(0xffffffff00000000L));
2446 __ SmiUntag(scratch1, left);
2447 __ GetLeastBitsFromSmi(scratch2, right, 5);
2448 __ dsllv(scratch1, scratch1, scratch2);
2449 __ SmiTag(v0, scratch1);
2453 __ SmiUntag(scratch1, left);
2454 __ GetLeastBitsFromSmi(scratch2, right, 5);
2455 __ dsrlv(scratch1, scratch1, scratch2);
2456 __ And(scratch2, scratch1, 0x80000000);
2457 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
2458 __ SmiTag(v0, scratch1);
2462 __ DadduAndCheckForOverflow(v0, left, right, scratch1);
2463 __ BranchOnOverflow(&stub_call, scratch1);
2466 __ DsubuAndCheckForOverflow(v0, left, right, scratch1);
2467 __ BranchOnOverflow(&stub_call, scratch1);
2470 __ Dmulh(v0, left, right);
2471 __ dsra32(scratch2, v0, 0);
2472 __ sra(scratch1, v0, 31);
2473 __ Branch(USE_DELAY_SLOT, &stub_call, ne, scratch2, Operand(scratch1));
2475 __ Branch(USE_DELAY_SLOT, &done, ne, v0, Operand(zero_reg));
2476 __ Daddu(scratch2, right, left);
2477 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
2478 DCHECK(Smi::FromInt(0) == 0);
2479 __ mov(v0, zero_reg);
2483 __ Or(v0, left, Operand(right));
2485 case Token::BIT_AND:
2486 __ And(v0, left, Operand(right));
2488 case Token::BIT_XOR:
2489 __ Xor(v0, left, Operand(right));
2496 context()->Plug(v0);
2500 void FullCodeGenerator::EmitClassDefineProperties(ClassLiteral* lit,
2501 int* used_store_slots) {
2502 // Constructor is in v0.
2503 DCHECK(lit != NULL);
2506 // No access check is needed here since the constructor is created by the
2508 Register scratch = a1;
2510 FieldMemOperand(v0, JSFunction::kPrototypeOrInitialMapOffset));
2513 for (int i = 0; i < lit->properties()->length(); i++) {
2514 ObjectLiteral::Property* property = lit->properties()->at(i);
2515 Expression* value = property->value();
2517 if (property->is_static()) {
2518 __ ld(scratch, MemOperand(sp, kPointerSize)); // constructor
2520 __ ld(scratch, MemOperand(sp, 0)); // prototype
2523 EmitPropertyKey(property, lit->GetIdForProperty(i));
2525 // The static prototype property is read only. We handle the non computed
2526 // property name case in the parser. Since this is the only case where we
2527 // need to check for an own read only property we special case this so we do
2528 // not need to do this for every property.
2529 if (property->is_static() && property->is_computed_name()) {
2530 __ CallRuntime(Runtime::kThrowIfStaticPrototype, 1);
2534 VisitForStackValue(value);
2535 EmitSetHomeObjectIfNeeded(value, 2,
2536 lit->SlotForHomeObject(value, used_store_slots));
2538 switch (property->kind()) {
2539 case ObjectLiteral::Property::CONSTANT:
2540 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2541 case ObjectLiteral::Property::PROTOTYPE:
2543 case ObjectLiteral::Property::COMPUTED:
2544 __ CallRuntime(Runtime::kDefineClassMethod, 3);
2547 case ObjectLiteral::Property::GETTER:
2548 __ li(a0, Operand(Smi::FromInt(DONT_ENUM)));
2550 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
2553 case ObjectLiteral::Property::SETTER:
2554 __ li(a0, Operand(Smi::FromInt(DONT_ENUM)));
2556 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
2564 // Set both the prototype and constructor to have fast properties, and also
2565 // freeze them in strong mode.
2566 __ CallRuntime(is_strong(language_mode())
2567 ? Runtime::kFinalizeClassDefinitionStrong
2568 : Runtime::kFinalizeClassDefinition,
2573 void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr, Token::Value op) {
2574 __ mov(a0, result_register());
2577 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2578 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
2579 CallIC(code, expr->BinaryOperationFeedbackId());
2580 patch_site.EmitPatchInfo();
2581 context()->Plug(v0);
2585 void FullCodeGenerator::EmitAssignment(Expression* expr,
2586 FeedbackVectorICSlot slot) {
2587 DCHECK(expr->IsValidReferenceExpressionOrThis());
2589 Property* prop = expr->AsProperty();
2590 LhsKind assign_type = Property::GetAssignType(prop);
2592 switch (assign_type) {
2594 Variable* var = expr->AsVariableProxy()->var();
2595 EffectContext context(this);
2596 EmitVariableAssignment(var, Token::ASSIGN, slot);
2599 case NAMED_PROPERTY: {
2600 __ push(result_register()); // Preserve value.
2601 VisitForAccumulatorValue(prop->obj());
2602 __ mov(StoreDescriptor::ReceiverRegister(), result_register());
2603 __ pop(StoreDescriptor::ValueRegister()); // Restore value.
2604 __ li(StoreDescriptor::NameRegister(),
2605 Operand(prop->key()->AsLiteral()->value()));
2606 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2610 case NAMED_SUPER_PROPERTY: {
2612 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2613 VisitForAccumulatorValue(
2614 prop->obj()->AsSuperPropertyReference()->home_object());
2615 // stack: value, this; v0: home_object
2616 Register scratch = a2;
2617 Register scratch2 = a3;
2618 __ mov(scratch, result_register()); // home_object
2619 __ ld(v0, MemOperand(sp, kPointerSize)); // value
2620 __ ld(scratch2, MemOperand(sp, 0)); // this
2621 __ sd(scratch2, MemOperand(sp, kPointerSize)); // this
2622 __ sd(scratch, MemOperand(sp, 0)); // home_object
2623 // stack: this, home_object; v0: value
2624 EmitNamedSuperPropertyStore(prop);
2627 case KEYED_SUPER_PROPERTY: {
2629 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2631 prop->obj()->AsSuperPropertyReference()->home_object());
2632 VisitForAccumulatorValue(prop->key());
2633 Register scratch = a2;
2634 Register scratch2 = a3;
2635 __ ld(scratch2, MemOperand(sp, 2 * kPointerSize)); // value
2636 // stack: value, this, home_object; v0: key, a3: value
2637 __ ld(scratch, MemOperand(sp, kPointerSize)); // this
2638 __ sd(scratch, MemOperand(sp, 2 * kPointerSize));
2639 __ ld(scratch, MemOperand(sp, 0)); // home_object
2640 __ sd(scratch, MemOperand(sp, kPointerSize));
2641 __ sd(v0, MemOperand(sp, 0));
2642 __ Move(v0, scratch2);
2643 // stack: this, home_object, key; v0: value.
2644 EmitKeyedSuperPropertyStore(prop);
2647 case KEYED_PROPERTY: {
2648 __ push(result_register()); // Preserve value.
2649 VisitForStackValue(prop->obj());
2650 VisitForAccumulatorValue(prop->key());
2651 __ Move(StoreDescriptor::NameRegister(), result_register());
2652 __ Pop(StoreDescriptor::ValueRegister(),
2653 StoreDescriptor::ReceiverRegister());
2654 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2656 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2661 context()->Plug(v0);
2665 void FullCodeGenerator::EmitStoreToStackLocalOrContextSlot(
2666 Variable* var, MemOperand location) {
2667 __ sd(result_register(), location);
2668 if (var->IsContextSlot()) {
2669 // RecordWrite may destroy all its register arguments.
2670 __ Move(a3, result_register());
2671 int offset = Context::SlotOffset(var->index());
2672 __ RecordWriteContextSlot(
2673 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
2678 void FullCodeGenerator::EmitVariableAssignment(Variable* var, Token::Value op,
2679 FeedbackVectorICSlot slot) {
2680 if (var->IsUnallocated()) {
2681 // Global var, const, or let.
2682 __ mov(StoreDescriptor::ValueRegister(), result_register());
2683 __ li(StoreDescriptor::NameRegister(), Operand(var->name()));
2684 __ ld(StoreDescriptor::ReceiverRegister(), GlobalObjectOperand());
2685 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2688 } else if (var->IsGlobalSlot()) {
2689 // Global var, const, or let.
2690 DCHECK(var->index() > 0);
2691 DCHECK(var->IsStaticGlobalObjectProperty());
2692 DCHECK(StoreGlobalViaContextDescriptor::ValueRegister().is(a0));
2693 __ mov(StoreGlobalViaContextDescriptor::ValueRegister(), result_register());
2694 int const slot = var->index();
2695 int const depth = scope()->ContextChainLength(var->scope());
2696 if (depth <= StoreGlobalViaContextStub::kMaximumDepth) {
2697 __ li(StoreGlobalViaContextDescriptor::SlotRegister(), Operand(slot));
2698 StoreGlobalViaContextStub stub(isolate(), depth, language_mode());
2701 __ Push(Smi::FromInt(slot));
2703 __ CallRuntime(is_strict(language_mode())
2704 ? Runtime::kStoreGlobalViaContext_Strict
2705 : Runtime::kStoreGlobalViaContext_Sloppy,
2709 } else if (var->mode() == LET && op != Token::INIT_LET) {
2710 // Non-initializing assignment to let variable needs a write barrier.
2711 DCHECK(!var->IsLookupSlot());
2712 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2714 MemOperand location = VarOperand(var, a1);
2715 __ ld(a3, location);
2716 __ LoadRoot(a4, Heap::kTheHoleValueRootIndex);
2717 __ Branch(&assign, ne, a3, Operand(a4));
2718 __ li(a3, Operand(var->name()));
2720 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2721 // Perform the assignment.
2723 EmitStoreToStackLocalOrContextSlot(var, location);
2725 } else if (var->mode() == CONST && op != Token::INIT_CONST) {
2726 // Assignment to const variable needs a write barrier.
2727 DCHECK(!var->IsLookupSlot());
2728 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2730 MemOperand location = VarOperand(var, a1);
2731 __ ld(a3, location);
2732 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2733 __ Branch(&const_error, ne, a3, Operand(at));
2734 __ li(a3, Operand(var->name()));
2736 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2737 __ bind(&const_error);
2738 __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2740 } else if (var->is_this() && op == Token::INIT_CONST) {
2741 // Initializing assignment to const {this} needs a write barrier.
2742 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2743 Label uninitialized_this;
2744 MemOperand location = VarOperand(var, a1);
2745 __ ld(a3, location);
2746 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2747 __ Branch(&uninitialized_this, eq, a3, Operand(at));
2748 __ li(a0, Operand(var->name()));
2750 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2751 __ bind(&uninitialized_this);
2752 EmitStoreToStackLocalOrContextSlot(var, location);
2754 } else if (!var->is_const_mode() || op == Token::INIT_CONST) {
2755 if (var->IsLookupSlot()) {
2756 // Assignment to var.
2757 __ li(a4, Operand(var->name()));
2758 __ li(a3, Operand(Smi::FromInt(language_mode())));
2759 // jssp[0] : language mode.
2761 // jssp[16] : context.
2762 // jssp[24] : value.
2763 __ Push(v0, cp, a4, a3);
2764 __ CallRuntime(Runtime::kStoreLookupSlot, 4);
2766 // Assignment to var or initializing assignment to let/const in harmony
2768 DCHECK((var->IsStackAllocated() || var->IsContextSlot()));
2769 MemOperand location = VarOperand(var, a1);
2770 if (generate_debug_code_ && op == Token::INIT_LET) {
2771 // Check for an uninitialized let binding.
2772 __ ld(a2, location);
2773 __ LoadRoot(a4, Heap::kTheHoleValueRootIndex);
2774 __ Check(eq, kLetBindingReInitialization, a2, Operand(a4));
2776 EmitStoreToStackLocalOrContextSlot(var, location);
2779 } else if (op == Token::INIT_CONST_LEGACY) {
2780 // Const initializers need a write barrier.
2781 DCHECK(!var->IsParameter()); // No const parameters.
2782 if (var->IsLookupSlot()) {
2783 __ li(a0, Operand(var->name()));
2784 __ Push(v0, cp, a0); // Context and name.
2785 __ CallRuntime(Runtime::kInitializeLegacyConstLookupSlot, 3);
2787 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2789 MemOperand location = VarOperand(var, a1);
2790 __ ld(a2, location);
2791 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2792 __ Branch(&skip, ne, a2, Operand(at));
2793 EmitStoreToStackLocalOrContextSlot(var, location);
2798 DCHECK(var->mode() == CONST_LEGACY && op != Token::INIT_CONST_LEGACY);
2799 if (is_strict(language_mode())) {
2800 __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2802 // Silently ignore store in sloppy mode.
2807 void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2808 // Assignment to a property, using a named store IC.
2809 Property* prop = expr->target()->AsProperty();
2810 DCHECK(prop != NULL);
2811 DCHECK(prop->key()->IsLiteral());
2813 __ mov(StoreDescriptor::ValueRegister(), result_register());
2814 __ li(StoreDescriptor::NameRegister(),
2815 Operand(prop->key()->AsLiteral()->value()));
2816 __ pop(StoreDescriptor::ReceiverRegister());
2817 if (FLAG_vector_stores) {
2818 EmitLoadStoreICSlot(expr->AssignmentSlot());
2821 CallStoreIC(expr->AssignmentFeedbackId());
2824 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2825 context()->Plug(v0);
2829 void FullCodeGenerator::EmitNamedSuperPropertyStore(Property* prop) {
2830 // Assignment to named property of super.
2832 // stack : receiver ('this'), home_object
2833 DCHECK(prop != NULL);
2834 Literal* key = prop->key()->AsLiteral();
2835 DCHECK(key != NULL);
2837 __ Push(key->value());
2839 __ CallRuntime((is_strict(language_mode()) ? Runtime::kStoreToSuper_Strict
2840 : Runtime::kStoreToSuper_Sloppy),
2845 void FullCodeGenerator::EmitKeyedSuperPropertyStore(Property* prop) {
2846 // Assignment to named property of super.
2848 // stack : receiver ('this'), home_object, key
2849 DCHECK(prop != NULL);
2853 (is_strict(language_mode()) ? Runtime::kStoreKeyedToSuper_Strict
2854 : Runtime::kStoreKeyedToSuper_Sloppy),
2859 void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2860 // Assignment to a property, using a keyed store IC.
2861 // Call keyed store IC.
2862 // The arguments are:
2863 // - a0 is the value,
2865 // - a2 is the receiver.
2866 __ mov(StoreDescriptor::ValueRegister(), result_register());
2867 __ Pop(StoreDescriptor::ReceiverRegister(), StoreDescriptor::NameRegister());
2868 DCHECK(StoreDescriptor::ValueRegister().is(a0));
2871 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2872 if (FLAG_vector_stores) {
2873 EmitLoadStoreICSlot(expr->AssignmentSlot());
2876 CallIC(ic, expr->AssignmentFeedbackId());
2879 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2880 context()->Plug(v0);
2884 void FullCodeGenerator::VisitProperty(Property* expr) {
2885 Comment cmnt(masm_, "[ Property");
2886 SetExpressionPosition(expr);
2888 Expression* key = expr->key();
2890 if (key->IsPropertyName()) {
2891 if (!expr->IsSuperAccess()) {
2892 VisitForAccumulatorValue(expr->obj());
2893 __ Move(LoadDescriptor::ReceiverRegister(), v0);
2894 EmitNamedPropertyLoad(expr);
2896 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2898 expr->obj()->AsSuperPropertyReference()->home_object());
2899 EmitNamedSuperPropertyLoad(expr);
2902 if (!expr->IsSuperAccess()) {
2903 VisitForStackValue(expr->obj());
2904 VisitForAccumulatorValue(expr->key());
2905 __ Move(LoadDescriptor::NameRegister(), v0);
2906 __ pop(LoadDescriptor::ReceiverRegister());
2907 EmitKeyedPropertyLoad(expr);
2909 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2911 expr->obj()->AsSuperPropertyReference()->home_object());
2912 VisitForStackValue(expr->key());
2913 EmitKeyedSuperPropertyLoad(expr);
2916 PrepareForBailoutForId(expr->LoadId(), TOS_REG);
2917 context()->Plug(v0);
2921 void FullCodeGenerator::CallIC(Handle<Code> code,
2922 TypeFeedbackId id) {
2924 __ Call(code, RelocInfo::CODE_TARGET, id);
2928 // Code common for calls using the IC.
2929 void FullCodeGenerator::EmitCallWithLoadIC(Call* expr) {
2930 Expression* callee = expr->expression();
2932 CallICState::CallType call_type =
2933 callee->IsVariableProxy() ? CallICState::FUNCTION : CallICState::METHOD;
2935 // Get the target function.
2936 if (call_type == CallICState::FUNCTION) {
2937 { StackValueContext context(this);
2938 EmitVariableLoad(callee->AsVariableProxy());
2939 PrepareForBailout(callee, NO_REGISTERS);
2941 // Push undefined as receiver. This is patched in the method prologue if it
2942 // is a sloppy mode method.
2943 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
2946 // Load the function from the receiver.
2947 DCHECK(callee->IsProperty());
2948 DCHECK(!callee->AsProperty()->IsSuperAccess());
2949 __ ld(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
2950 EmitNamedPropertyLoad(callee->AsProperty());
2951 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2952 // Push the target function under the receiver.
2953 __ ld(at, MemOperand(sp, 0));
2955 __ sd(v0, MemOperand(sp, kPointerSize));
2958 EmitCall(expr, call_type);
2962 void FullCodeGenerator::EmitSuperCallWithLoadIC(Call* expr) {
2963 SetExpressionPosition(expr);
2964 Expression* callee = expr->expression();
2965 DCHECK(callee->IsProperty());
2966 Property* prop = callee->AsProperty();
2967 DCHECK(prop->IsSuperAccess());
2969 Literal* key = prop->key()->AsLiteral();
2970 DCHECK(!key->value()->IsSmi());
2971 // Load the function from the receiver.
2972 const Register scratch = a1;
2973 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
2974 VisitForAccumulatorValue(super_ref->home_object());
2975 __ mov(scratch, v0);
2976 VisitForAccumulatorValue(super_ref->this_var());
2977 __ Push(scratch, v0, v0, scratch);
2978 __ Push(key->value());
2979 __ Push(Smi::FromInt(language_mode()));
2983 // - this (receiver)
2984 // - this (receiver) <-- LoadFromSuper will pop here and below.
2988 __ CallRuntime(Runtime::kLoadFromSuper, 4);
2990 // Replace home_object with target function.
2991 __ sd(v0, MemOperand(sp, kPointerSize));
2994 // - target function
2995 // - this (receiver)
2996 EmitCall(expr, CallICState::METHOD);
3000 // Code common for calls using the IC.
3001 void FullCodeGenerator::EmitKeyedCallWithLoadIC(Call* expr,
3004 VisitForAccumulatorValue(key);
3006 Expression* callee = expr->expression();
3008 // Load the function from the receiver.
3009 DCHECK(callee->IsProperty());
3010 __ ld(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
3011 __ Move(LoadDescriptor::NameRegister(), v0);
3012 EmitKeyedPropertyLoad(callee->AsProperty());
3013 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
3015 // Push the target function under the receiver.
3016 __ ld(at, MemOperand(sp, 0));
3018 __ sd(v0, MemOperand(sp, kPointerSize));
3020 EmitCall(expr, CallICState::METHOD);
3024 void FullCodeGenerator::EmitKeyedSuperCallWithLoadIC(Call* expr) {
3025 Expression* callee = expr->expression();
3026 DCHECK(callee->IsProperty());
3027 Property* prop = callee->AsProperty();
3028 DCHECK(prop->IsSuperAccess());
3030 SetExpressionPosition(prop);
3031 // Load the function from the receiver.
3032 const Register scratch = a1;
3033 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
3034 VisitForAccumulatorValue(super_ref->home_object());
3035 __ Move(scratch, v0);
3036 VisitForAccumulatorValue(super_ref->this_var());
3037 __ Push(scratch, v0, v0, scratch);
3038 VisitForStackValue(prop->key());
3039 __ Push(Smi::FromInt(language_mode()));
3043 // - this (receiver)
3044 // - this (receiver) <-- LoadKeyedFromSuper will pop here and below.
3048 __ CallRuntime(Runtime::kLoadKeyedFromSuper, 4);
3050 // Replace home_object with target function.
3051 __ sd(v0, MemOperand(sp, kPointerSize));
3054 // - target function
3055 // - this (receiver)
3056 EmitCall(expr, CallICState::METHOD);
3060 void FullCodeGenerator::EmitCall(Call* expr, CallICState::CallType call_type) {
3061 // Load the arguments.
3062 ZoneList<Expression*>* args = expr->arguments();
3063 int arg_count = args->length();
3064 for (int i = 0; i < arg_count; i++) {
3065 VisitForStackValue(args->at(i));
3068 // Record source position of the IC call.
3069 SetCallPosition(expr, arg_count);
3070 Handle<Code> ic = CodeFactory::CallIC(isolate(), arg_count, call_type).code();
3071 __ li(a3, Operand(SmiFromSlot(expr->CallFeedbackICSlot())));
3072 __ ld(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
3073 // Don't assign a type feedback id to the IC, since type feedback is provided
3074 // by the vector above.
3076 RecordJSReturnSite(expr);
3077 // Restore context register.
3078 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3079 context()->DropAndPlug(1, v0);
3083 void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
3084 // a6: copy of the first argument or undefined if it doesn't exist.
3085 if (arg_count > 0) {
3086 __ ld(a6, MemOperand(sp, arg_count * kPointerSize));
3088 __ LoadRoot(a6, Heap::kUndefinedValueRootIndex);
3091 // a5: the receiver of the enclosing function.
3092 __ ld(a5, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3094 // a4: the language mode.
3095 __ li(a4, Operand(Smi::FromInt(language_mode())));
3097 // a1: the start position of the scope the calls resides in.
3098 __ li(a1, Operand(Smi::FromInt(scope()->start_position())));
3100 // Do the runtime call.
3101 __ Push(a6, a5, a4, a1);
3102 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
3106 // See http://www.ecma-international.org/ecma-262/6.0/#sec-function-calls.
3107 void FullCodeGenerator::PushCalleeAndWithBaseObject(Call* expr) {
3108 VariableProxy* callee = expr->expression()->AsVariableProxy();
3109 if (callee->var()->IsLookupSlot()) {
3112 SetExpressionPosition(callee);
3113 // Generate code for loading from variables potentially shadowed by
3114 // eval-introduced variables.
3115 EmitDynamicLookupFastCase(callee, NOT_INSIDE_TYPEOF, &slow, &done);
3118 // Call the runtime to find the function to call (returned in v0)
3119 // and the object holding it (returned in v1).
3120 DCHECK(!context_register().is(a2));
3121 __ li(a2, Operand(callee->name()));
3122 __ Push(context_register(), a2);
3123 __ CallRuntime(Runtime::kLoadLookupSlot, 2);
3124 __ Push(v0, v1); // Function, receiver.
3125 PrepareForBailoutForId(expr->LookupId(), NO_REGISTERS);
3127 // If fast case code has been generated, emit code to push the
3128 // function and receiver and have the slow path jump around this
3130 if (done.is_linked()) {
3136 // The receiver is implicitly the global receiver. Indicate this
3137 // by passing the hole to the call function stub.
3138 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
3143 VisitForStackValue(callee);
3144 // refEnv.WithBaseObject()
3145 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
3146 __ push(a2); // Reserved receiver slot.
3151 void FullCodeGenerator::VisitCall(Call* expr) {
3153 // We want to verify that RecordJSReturnSite gets called on all paths
3154 // through this function. Avoid early returns.
3155 expr->return_is_recorded_ = false;
3158 Comment cmnt(masm_, "[ Call");
3159 Expression* callee = expr->expression();
3160 Call::CallType call_type = expr->GetCallType(isolate());
3162 if (call_type == Call::POSSIBLY_EVAL_CALL) {
3163 // In a call to eval, we first call RuntimeHidden_ResolvePossiblyDirectEval
3164 // to resolve the function we need to call. Then we call the resolved
3165 // function using the given arguments.
3166 ZoneList<Expression*>* args = expr->arguments();
3167 int arg_count = args->length();
3168 PushCalleeAndWithBaseObject(expr);
3170 // Push the arguments.
3171 for (int i = 0; i < arg_count; i++) {
3172 VisitForStackValue(args->at(i));
3175 // Push a copy of the function (found below the arguments) and
3177 __ ld(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
3179 EmitResolvePossiblyDirectEval(arg_count);
3181 // Touch up the stack with the resolved function.
3182 __ sd(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
3184 PrepareForBailoutForId(expr->EvalId(), NO_REGISTERS);
3185 // Record source position for debugger.
3186 SetCallPosition(expr, arg_count);
3187 CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
3188 __ ld(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
3190 RecordJSReturnSite(expr);
3191 // Restore context register.
3192 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3193 context()->DropAndPlug(1, v0);
3194 } else if (call_type == Call::GLOBAL_CALL) {
3195 EmitCallWithLoadIC(expr);
3196 } else if (call_type == Call::LOOKUP_SLOT_CALL) {
3197 // Call to a lookup slot (dynamically introduced variable).
3198 PushCalleeAndWithBaseObject(expr);
3200 } else if (call_type == Call::PROPERTY_CALL) {
3201 Property* property = callee->AsProperty();
3202 bool is_named_call = property->key()->IsPropertyName();
3203 if (property->IsSuperAccess()) {
3204 if (is_named_call) {
3205 EmitSuperCallWithLoadIC(expr);
3207 EmitKeyedSuperCallWithLoadIC(expr);
3210 VisitForStackValue(property->obj());
3211 if (is_named_call) {
3212 EmitCallWithLoadIC(expr);
3214 EmitKeyedCallWithLoadIC(expr, property->key());
3217 } else if (call_type == Call::SUPER_CALL) {
3218 EmitSuperConstructorCall(expr);
3220 DCHECK(call_type == Call::OTHER_CALL);
3221 // Call to an arbitrary expression not handled specially above.
3222 VisitForStackValue(callee);
3223 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
3225 // Emit function call.
3230 // RecordJSReturnSite should have been called.
3231 DCHECK(expr->return_is_recorded_);
3236 void FullCodeGenerator::VisitCallNew(CallNew* expr) {
3237 Comment cmnt(masm_, "[ CallNew");
3238 // According to ECMA-262, section 11.2.2, page 44, the function
3239 // expression in new calls must be evaluated before the
3242 // Push constructor on the stack. If it's not a function it's used as
3243 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
3245 DCHECK(!expr->expression()->IsSuperPropertyReference());
3246 VisitForStackValue(expr->expression());
3248 // Push the arguments ("left-to-right") on the stack.
3249 ZoneList<Expression*>* args = expr->arguments();
3250 int arg_count = args->length();
3251 for (int i = 0; i < arg_count; i++) {
3252 VisitForStackValue(args->at(i));
3255 // Call the construct call builtin that handles allocation and
3256 // constructor invocation.
3257 SetConstructCallPosition(expr);
3259 // Load function and argument count into a1 and a0.
3260 __ li(a0, Operand(arg_count));
3261 __ ld(a1, MemOperand(sp, arg_count * kPointerSize));
3263 // Record call targets in unoptimized code.
3264 if (FLAG_pretenuring_call_new) {
3265 EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3266 DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3267 expr->CallNewFeedbackSlot().ToInt() + 1);
3270 __ li(a2, FeedbackVector());
3271 __ li(a3, Operand(SmiFromSlot(expr->CallNewFeedbackSlot())));
3273 CallConstructStub stub(isolate(), RECORD_CONSTRUCTOR_TARGET);
3274 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3275 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
3276 context()->Plug(v0);
3280 void FullCodeGenerator::EmitSuperConstructorCall(Call* expr) {
3281 SuperCallReference* super_call_ref =
3282 expr->expression()->AsSuperCallReference();
3283 DCHECK_NOT_NULL(super_call_ref);
3285 EmitLoadSuperConstructor(super_call_ref);
3286 __ push(result_register());
3288 // Push the arguments ("left-to-right") on the stack.
3289 ZoneList<Expression*>* args = expr->arguments();
3290 int arg_count = args->length();
3291 for (int i = 0; i < arg_count; i++) {
3292 VisitForStackValue(args->at(i));
3295 // Call the construct call builtin that handles allocation and
3296 // constructor invocation.
3297 SetConstructCallPosition(expr);
3299 // Load original constructor into a4.
3300 VisitForAccumulatorValue(super_call_ref->new_target_var());
3301 __ mov(a4, result_register());
3303 // Load function and argument count into a1 and a0.
3304 __ li(a0, Operand(arg_count));
3305 __ ld(a1, MemOperand(sp, arg_count * kPointerSize));
3307 // Record call targets in unoptimized code.
3308 if (FLAG_pretenuring_call_new) {
3310 /* TODO(dslomov): support pretenuring.
3311 EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3312 DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3313 expr->CallNewFeedbackSlot().ToInt() + 1);
3317 __ li(a2, FeedbackVector());
3318 __ li(a3, Operand(SmiFromSlot(expr->CallFeedbackSlot())));
3320 CallConstructStub stub(isolate(), SUPER_CALL_RECORD_TARGET);
3321 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3323 RecordJSReturnSite(expr);
3325 context()->Plug(v0);
3329 void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
3330 ZoneList<Expression*>* args = expr->arguments();
3331 DCHECK(args->length() == 1);
3333 VisitForAccumulatorValue(args->at(0));
3335 Label materialize_true, materialize_false;
3336 Label* if_true = NULL;
3337 Label* if_false = NULL;
3338 Label* fall_through = NULL;
3339 context()->PrepareTest(&materialize_true, &materialize_false,
3340 &if_true, &if_false, &fall_through);
3342 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3344 Split(eq, a4, Operand(zero_reg), if_true, if_false, fall_through);
3346 context()->Plug(if_true, if_false);
3350 void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
3351 ZoneList<Expression*>* args = expr->arguments();
3352 DCHECK(args->length() == 1);
3354 VisitForAccumulatorValue(args->at(0));
3356 Label materialize_true, materialize_false;
3357 Label* if_true = NULL;
3358 Label* if_false = NULL;
3359 Label* fall_through = NULL;
3360 context()->PrepareTest(&materialize_true, &materialize_false,
3361 &if_true, &if_false, &fall_through);
3363 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3364 __ NonNegativeSmiTst(v0, at);
3365 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
3367 context()->Plug(if_true, if_false);
3371 void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
3372 ZoneList<Expression*>* args = expr->arguments();
3373 DCHECK(args->length() == 1);
3375 VisitForAccumulatorValue(args->at(0));
3377 Label materialize_true, materialize_false;
3378 Label* if_true = NULL;
3379 Label* if_false = NULL;
3380 Label* fall_through = NULL;
3381 context()->PrepareTest(&materialize_true, &materialize_false,
3382 &if_true, &if_false, &fall_through);
3384 __ JumpIfSmi(v0, if_false);
3385 __ LoadRoot(at, Heap::kNullValueRootIndex);
3386 __ Branch(if_true, eq, v0, Operand(at));
3387 __ ld(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
3388 // Undetectable objects behave like undefined when tested with typeof.
3389 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
3390 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
3391 __ Branch(if_false, ne, at, Operand(zero_reg));
3392 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
3393 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
3394 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3395 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
3396 if_true, if_false, fall_through);
3398 context()->Plug(if_true, if_false);
3402 void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
3403 ZoneList<Expression*>* args = expr->arguments();
3404 DCHECK(args->length() == 1);
3406 VisitForAccumulatorValue(args->at(0));
3408 Label materialize_true, materialize_false;
3409 Label* if_true = NULL;
3410 Label* if_false = NULL;
3411 Label* fall_through = NULL;
3412 context()->PrepareTest(&materialize_true, &materialize_false,
3413 &if_true, &if_false, &fall_through);
3415 __ JumpIfSmi(v0, if_false);
3416 __ GetObjectType(v0, a1, a1);
3417 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3418 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
3419 if_true, if_false, fall_through);
3421 context()->Plug(if_true, if_false);
3425 void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
3426 ZoneList<Expression*>* args = expr->arguments();
3427 DCHECK(args->length() == 1);
3429 VisitForAccumulatorValue(args->at(0));
3431 Label materialize_true, materialize_false;
3432 Label* if_true = NULL;
3433 Label* if_false = NULL;
3434 Label* fall_through = NULL;
3435 context()->PrepareTest(&materialize_true, &materialize_false,
3436 &if_true, &if_false, &fall_through);
3438 __ JumpIfSmi(v0, if_false);
3439 __ ld(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
3440 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
3441 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3442 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
3443 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
3445 context()->Plug(if_true, if_false);
3449 void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
3450 CallRuntime* expr) {
3451 ZoneList<Expression*>* args = expr->arguments();
3452 DCHECK(args->length() == 1);
3454 VisitForAccumulatorValue(args->at(0));
3456 Label materialize_true, materialize_false, skip_lookup;
3457 Label* if_true = NULL;
3458 Label* if_false = NULL;
3459 Label* fall_through = NULL;
3460 context()->PrepareTest(&materialize_true, &materialize_false,
3461 &if_true, &if_false, &fall_through);
3463 __ AssertNotSmi(v0);
3465 __ ld(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
3466 __ lbu(a4, FieldMemOperand(a1, Map::kBitField2Offset));
3467 __ And(a4, a4, 1 << Map::kStringWrapperSafeForDefaultValueOf);
3468 __ Branch(&skip_lookup, ne, a4, Operand(zero_reg));
3470 // Check for fast case object. Generate false result for slow case object.
3471 __ ld(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
3472 __ ld(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
3473 __ LoadRoot(a4, Heap::kHashTableMapRootIndex);
3474 __ Branch(if_false, eq, a2, Operand(a4));
3476 // Look for valueOf name in the descriptor array, and indicate false if
3477 // found. Since we omit an enumeration index check, if it is added via a
3478 // transition that shares its descriptor array, this is a false positive.
3479 Label entry, loop, done;
3481 // Skip loop if no descriptors are valid.
3482 __ NumberOfOwnDescriptors(a3, a1);
3483 __ Branch(&done, eq, a3, Operand(zero_reg));
3485 __ LoadInstanceDescriptors(a1, a4);
3486 // a4: descriptor array.
3487 // a3: valid entries in the descriptor array.
3488 STATIC_ASSERT(kSmiTag == 0);
3489 STATIC_ASSERT(kSmiTagSize == 1);
3491 // STATIC_ASSERT(kPointerSize == 4);
3492 __ li(at, Operand(DescriptorArray::kDescriptorSize));
3493 __ Dmul(a3, a3, at);
3494 // Calculate location of the first key name.
3495 __ Daddu(a4, a4, Operand(DescriptorArray::kFirstOffset - kHeapObjectTag));
3496 // Calculate the end of the descriptor array.
3498 __ dsll(a5, a3, kPointerSizeLog2);
3499 __ Daddu(a2, a2, a5);
3501 // Loop through all the keys in the descriptor array. If one of these is the
3502 // string "valueOf" the result is false.
3503 // The use of a6 to store the valueOf string assumes that it is not otherwise
3504 // used in the loop below.
3505 __ li(a6, Operand(isolate()->factory()->value_of_string()));
3508 __ ld(a3, MemOperand(a4, 0));
3509 __ Branch(if_false, eq, a3, Operand(a6));
3510 __ Daddu(a4, a4, Operand(DescriptorArray::kDescriptorSize * kPointerSize));
3512 __ Branch(&loop, ne, a4, Operand(a2));
3516 // Set the bit in the map to indicate that there is no local valueOf field.
3517 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
3518 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
3519 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
3521 __ bind(&skip_lookup);
3523 // If a valueOf property is not found on the object check that its
3524 // prototype is the un-modified String prototype. If not result is false.
3525 __ ld(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
3526 __ JumpIfSmi(a2, if_false);
3527 __ ld(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
3528 __ ld(a3, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
3529 __ ld(a3, FieldMemOperand(a3, GlobalObject::kNativeContextOffset));
3530 __ ld(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
3531 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3532 Split(eq, a2, Operand(a3), if_true, if_false, fall_through);
3534 context()->Plug(if_true, if_false);
3538 void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
3539 ZoneList<Expression*>* args = expr->arguments();
3540 DCHECK(args->length() == 1);
3542 VisitForAccumulatorValue(args->at(0));
3544 Label materialize_true, materialize_false;
3545 Label* if_true = NULL;
3546 Label* if_false = NULL;
3547 Label* fall_through = NULL;
3548 context()->PrepareTest(&materialize_true, &materialize_false,
3549 &if_true, &if_false, &fall_through);
3551 __ JumpIfSmi(v0, if_false);
3552 __ GetObjectType(v0, a1, a2);
3553 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3554 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
3555 __ Branch(if_false);
3557 context()->Plug(if_true, if_false);
3561 void FullCodeGenerator::EmitIsMinusZero(CallRuntime* expr) {
3562 ZoneList<Expression*>* args = expr->arguments();
3563 DCHECK(args->length() == 1);
3565 VisitForAccumulatorValue(args->at(0));
3567 Label materialize_true, materialize_false;
3568 Label* if_true = NULL;
3569 Label* if_false = NULL;
3570 Label* fall_through = NULL;
3571 context()->PrepareTest(&materialize_true, &materialize_false,
3572 &if_true, &if_false, &fall_through);
3574 __ CheckMap(v0, a1, Heap::kHeapNumberMapRootIndex, if_false, DO_SMI_CHECK);
3575 __ lwu(a2, FieldMemOperand(v0, HeapNumber::kExponentOffset));
3576 __ lwu(a1, FieldMemOperand(v0, HeapNumber::kMantissaOffset));
3577 __ li(a4, 0x80000000);
3579 __ Branch(¬_nan, ne, a2, Operand(a4));
3580 __ mov(a4, zero_reg);
3584 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3585 Split(eq, a2, Operand(a4), if_true, if_false, fall_through);
3587 context()->Plug(if_true, if_false);
3591 void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
3592 ZoneList<Expression*>* args = expr->arguments();
3593 DCHECK(args->length() == 1);
3595 VisitForAccumulatorValue(args->at(0));
3597 Label materialize_true, materialize_false;
3598 Label* if_true = NULL;
3599 Label* if_false = NULL;
3600 Label* fall_through = NULL;
3601 context()->PrepareTest(&materialize_true, &materialize_false,
3602 &if_true, &if_false, &fall_through);
3604 __ JumpIfSmi(v0, if_false);
3605 __ GetObjectType(v0, a1, a1);
3606 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3607 Split(eq, a1, Operand(JS_ARRAY_TYPE),
3608 if_true, if_false, fall_through);
3610 context()->Plug(if_true, if_false);
3614 void FullCodeGenerator::EmitIsTypedArray(CallRuntime* expr) {
3615 ZoneList<Expression*>* args = expr->arguments();
3616 DCHECK(args->length() == 1);
3618 VisitForAccumulatorValue(args->at(0));
3620 Label materialize_true, materialize_false;
3621 Label* if_true = NULL;
3622 Label* if_false = NULL;
3623 Label* fall_through = NULL;
3624 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3625 &if_false, &fall_through);
3627 __ JumpIfSmi(v0, if_false);
3628 __ GetObjectType(v0, a1, a1);
3629 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3630 Split(eq, a1, Operand(JS_TYPED_ARRAY_TYPE), if_true, if_false, fall_through);
3632 context()->Plug(if_true, if_false);
3636 void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
3637 ZoneList<Expression*>* args = expr->arguments();
3638 DCHECK(args->length() == 1);
3640 VisitForAccumulatorValue(args->at(0));
3642 Label materialize_true, materialize_false;
3643 Label* if_true = NULL;
3644 Label* if_false = NULL;
3645 Label* fall_through = NULL;
3646 context()->PrepareTest(&materialize_true, &materialize_false,
3647 &if_true, &if_false, &fall_through);
3649 __ JumpIfSmi(v0, if_false);
3650 __ GetObjectType(v0, a1, a1);
3651 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3652 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
3654 context()->Plug(if_true, if_false);
3658 void FullCodeGenerator::EmitIsJSProxy(CallRuntime* expr) {
3659 ZoneList<Expression*>* args = expr->arguments();
3660 DCHECK(args->length() == 1);
3662 VisitForAccumulatorValue(args->at(0));
3664 Label materialize_true, materialize_false;
3665 Label* if_true = NULL;
3666 Label* if_false = NULL;
3667 Label* fall_through = NULL;
3668 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3669 &if_false, &fall_through);
3671 __ JumpIfSmi(v0, if_false);
3673 Register type_reg = a2;
3674 __ GetObjectType(v0, map, type_reg);
3675 __ Subu(type_reg, type_reg, Operand(FIRST_JS_PROXY_TYPE));
3676 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3677 Split(ls, type_reg, Operand(LAST_JS_PROXY_TYPE - FIRST_JS_PROXY_TYPE),
3678 if_true, if_false, fall_through);
3680 context()->Plug(if_true, if_false);
3684 void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
3685 DCHECK(expr->arguments()->length() == 0);
3687 Label materialize_true, materialize_false;
3688 Label* if_true = NULL;
3689 Label* if_false = NULL;
3690 Label* fall_through = NULL;
3691 context()->PrepareTest(&materialize_true, &materialize_false,
3692 &if_true, &if_false, &fall_through);
3694 // Get the frame pointer for the calling frame.
3695 __ ld(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3697 // Skip the arguments adaptor frame if it exists.
3698 Label check_frame_marker;
3699 __ ld(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
3700 __ Branch(&check_frame_marker, ne,
3701 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3702 __ ld(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
3704 // Check the marker in the calling frame.
3705 __ bind(&check_frame_marker);
3706 __ ld(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
3707 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3708 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
3709 if_true, if_false, fall_through);
3711 context()->Plug(if_true, if_false);
3715 void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
3716 ZoneList<Expression*>* args = expr->arguments();
3717 DCHECK(args->length() == 2);
3719 // Load the two objects into registers and perform the comparison.
3720 VisitForStackValue(args->at(0));
3721 VisitForAccumulatorValue(args->at(1));
3723 Label materialize_true, materialize_false;
3724 Label* if_true = NULL;
3725 Label* if_false = NULL;
3726 Label* fall_through = NULL;
3727 context()->PrepareTest(&materialize_true, &materialize_false,
3728 &if_true, &if_false, &fall_through);
3731 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3732 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
3734 context()->Plug(if_true, if_false);
3738 void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
3739 ZoneList<Expression*>* args = expr->arguments();
3740 DCHECK(args->length() == 1);
3742 // ArgumentsAccessStub expects the key in a1 and the formal
3743 // parameter count in a0.
3744 VisitForAccumulatorValue(args->at(0));
3746 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
3747 ArgumentsAccessStub stub(isolate(), ArgumentsAccessStub::READ_ELEMENT);
3749 context()->Plug(v0);
3753 void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
3754 DCHECK(expr->arguments()->length() == 0);
3756 // Get the number of formal parameters.
3757 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
3759 // Check if the calling frame is an arguments adaptor frame.
3760 __ ld(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3761 __ ld(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
3762 __ Branch(&exit, ne, a3,
3763 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3765 // Arguments adaptor case: Read the arguments length from the
3767 __ ld(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3770 context()->Plug(v0);
3774 void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
3775 ZoneList<Expression*>* args = expr->arguments();
3776 DCHECK(args->length() == 1);
3777 Label done, null, function, non_function_constructor;
3779 VisitForAccumulatorValue(args->at(0));
3781 // If the object is a smi, we return null.
3782 __ JumpIfSmi(v0, &null);
3784 // Check that the object is a JS object but take special care of JS
3785 // functions to make sure they have 'Function' as their class.
3786 // Assume that there are only two callable types, and one of them is at
3787 // either end of the type range for JS object types. Saves extra comparisons.
3788 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
3789 __ GetObjectType(v0, v0, a1); // Map is now in v0.
3790 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
3792 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3793 FIRST_SPEC_OBJECT_TYPE + 1);
3794 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
3796 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3797 LAST_SPEC_OBJECT_TYPE - 1);
3798 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
3799 // Assume that there is no larger type.
3800 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
3802 // Check if the constructor in the map is a JS function.
3803 Register instance_type = a2;
3804 __ GetMapConstructor(v0, v0, a1, instance_type);
3805 __ Branch(&non_function_constructor, ne, instance_type,
3806 Operand(JS_FUNCTION_TYPE));
3808 // v0 now contains the constructor function. Grab the
3809 // instance class name from there.
3810 __ ld(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
3811 __ ld(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
3814 // Functions have class 'Function'.
3816 __ LoadRoot(v0, Heap::kFunction_stringRootIndex);
3819 // Objects with a non-function constructor have class 'Object'.
3820 __ bind(&non_function_constructor);
3821 __ LoadRoot(v0, Heap::kObject_stringRootIndex);
3824 // Non-JS objects have class null.
3826 __ LoadRoot(v0, Heap::kNullValueRootIndex);
3831 context()->Plug(v0);
3835 void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3836 ZoneList<Expression*>* args = expr->arguments();
3837 DCHECK(args->length() == 1);
3839 VisitForAccumulatorValue(args->at(0)); // Load the object.
3842 // If the object is a smi return the object.
3843 __ JumpIfSmi(v0, &done);
3844 // If the object is not a value type, return the object.
3845 __ GetObjectType(v0, a1, a1);
3846 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
3848 __ ld(v0, FieldMemOperand(v0, JSValue::kValueOffset));
3851 context()->Plug(v0);
3855 void FullCodeGenerator::EmitIsDate(CallRuntime* expr) {
3856 ZoneList<Expression*>* args = expr->arguments();
3857 DCHECK_EQ(1, args->length());
3859 VisitForAccumulatorValue(args->at(0));
3861 Label materialize_true, materialize_false;
3862 Label* if_true = nullptr;
3863 Label* if_false = nullptr;
3864 Label* fall_through = nullptr;
3865 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3866 &if_false, &fall_through);
3868 __ JumpIfSmi(v0, if_false);
3869 __ GetObjectType(v0, a1, a1);
3870 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3871 Split(eq, a1, Operand(JS_DATE_TYPE), if_true, if_false, fall_through);
3873 context()->Plug(if_true, if_false);
3877 void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3878 ZoneList<Expression*>* args = expr->arguments();
3879 DCHECK(args->length() == 2);
3880 DCHECK_NOT_NULL(args->at(1)->AsLiteral());
3881 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->value()));
3883 VisitForAccumulatorValue(args->at(0)); // Load the object.
3885 Register object = v0;
3886 Register result = v0;
3887 Register scratch0 = t1;
3888 Register scratch1 = a1;
3890 if (index->value() == 0) {
3891 __ ld(result, FieldMemOperand(object, JSDate::kValueOffset));
3893 Label runtime, done;
3894 if (index->value() < JSDate::kFirstUncachedField) {
3895 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3896 __ li(scratch1, Operand(stamp));
3897 __ ld(scratch1, MemOperand(scratch1));
3898 __ ld(scratch0, FieldMemOperand(object, JSDate::kCacheStampOffset));
3899 __ Branch(&runtime, ne, scratch1, Operand(scratch0));
3900 __ ld(result, FieldMemOperand(object, JSDate::kValueOffset +
3901 kPointerSize * index->value()));
3905 __ PrepareCallCFunction(2, scratch1);
3906 __ li(a1, Operand(index));
3907 __ Move(a0, object);
3908 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
3912 context()->Plug(result);
3916 void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
3917 ZoneList<Expression*>* args = expr->arguments();
3918 DCHECK_EQ(3, args->length());
3920 Register string = v0;
3921 Register index = a1;
3922 Register value = a2;
3924 VisitForStackValue(args->at(0)); // index
3925 VisitForStackValue(args->at(1)); // value
3926 VisitForAccumulatorValue(args->at(2)); // string
3927 __ Pop(index, value);
3929 if (FLAG_debug_code) {
3930 __ SmiTst(value, at);
3931 __ Check(eq, kNonSmiValue, at, Operand(zero_reg));
3932 __ SmiTst(index, at);
3933 __ Check(eq, kNonSmiIndex, at, Operand(zero_reg));
3934 __ SmiUntag(index, index);
3935 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
3936 Register scratch = t1;
3937 __ EmitSeqStringSetCharCheck(
3938 string, index, value, scratch, one_byte_seq_type);
3939 __ SmiTag(index, index);
3942 __ SmiUntag(value, value);
3945 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
3947 __ Daddu(at, at, index);
3948 __ sb(value, MemOperand(at));
3949 context()->Plug(string);
3953 void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
3954 ZoneList<Expression*>* args = expr->arguments();
3955 DCHECK_EQ(3, args->length());
3957 Register string = v0;
3958 Register index = a1;
3959 Register value = a2;
3961 VisitForStackValue(args->at(0)); // index
3962 VisitForStackValue(args->at(1)); // value
3963 VisitForAccumulatorValue(args->at(2)); // string
3964 __ Pop(index, value);
3966 if (FLAG_debug_code) {
3967 __ SmiTst(value, at);
3968 __ Check(eq, kNonSmiValue, at, Operand(zero_reg));
3969 __ SmiTst(index, at);
3970 __ Check(eq, kNonSmiIndex, at, Operand(zero_reg));
3971 __ SmiUntag(index, index);
3972 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
3973 Register scratch = t1;
3974 __ EmitSeqStringSetCharCheck(
3975 string, index, value, scratch, two_byte_seq_type);
3976 __ SmiTag(index, index);
3979 __ SmiUntag(value, value);
3982 Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3983 __ dsra(index, index, 32 - 1);
3984 __ Daddu(at, at, index);
3985 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
3986 __ sh(value, MemOperand(at));
3987 context()->Plug(string);
3991 void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3992 ZoneList<Expression*>* args = expr->arguments();
3993 DCHECK(args->length() == 2);
3995 VisitForStackValue(args->at(0)); // Load the object.
3996 VisitForAccumulatorValue(args->at(1)); // Load the value.
3997 __ pop(a1); // v0 = value. a1 = object.
4000 // If the object is a smi, return the value.
4001 __ JumpIfSmi(a1, &done);
4003 // If the object is not a value type, return the value.
4004 __ GetObjectType(a1, a2, a2);
4005 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
4008 __ sd(v0, FieldMemOperand(a1, JSValue::kValueOffset));
4009 // Update the write barrier. Save the value as it will be
4010 // overwritten by the write barrier code and is needed afterward.
4012 __ RecordWriteField(
4013 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
4016 context()->Plug(v0);
4020 void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
4021 ZoneList<Expression*>* args = expr->arguments();
4022 DCHECK_EQ(args->length(), 1);
4024 // Load the argument into a0 and call the stub.
4025 VisitForAccumulatorValue(args->at(0));
4026 __ mov(a0, result_register());
4028 NumberToStringStub stub(isolate());
4030 context()->Plug(v0);
4034 void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
4035 ZoneList<Expression*>* args = expr->arguments();
4036 DCHECK(args->length() == 1);
4038 VisitForAccumulatorValue(args->at(0));
4041 StringCharFromCodeGenerator generator(v0, a1);
4042 generator.GenerateFast(masm_);
4045 NopRuntimeCallHelper call_helper;
4046 generator.GenerateSlow(masm_, call_helper);
4049 context()->Plug(a1);
4053 void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
4054 ZoneList<Expression*>* args = expr->arguments();
4055 DCHECK(args->length() == 2);
4057 VisitForStackValue(args->at(0));
4058 VisitForAccumulatorValue(args->at(1));
4059 __ mov(a0, result_register());
4061 Register object = a1;
4062 Register index = a0;
4063 Register result = v0;
4067 Label need_conversion;
4068 Label index_out_of_range;
4070 StringCharCodeAtGenerator generator(object,
4075 &index_out_of_range,
4076 STRING_INDEX_IS_NUMBER);
4077 generator.GenerateFast(masm_);
4080 __ bind(&index_out_of_range);
4081 // When the index is out of range, the spec requires us to return
4083 __ LoadRoot(result, Heap::kNanValueRootIndex);
4086 __ bind(&need_conversion);
4087 // Load the undefined value into the result register, which will
4088 // trigger conversion.
4089 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
4092 NopRuntimeCallHelper call_helper;
4093 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
4096 context()->Plug(result);
4100 void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
4101 ZoneList<Expression*>* args = expr->arguments();
4102 DCHECK(args->length() == 2);
4104 VisitForStackValue(args->at(0));
4105 VisitForAccumulatorValue(args->at(1));
4106 __ mov(a0, result_register());
4108 Register object = a1;
4109 Register index = a0;
4110 Register scratch = a3;
4111 Register result = v0;
4115 Label need_conversion;
4116 Label index_out_of_range;
4118 StringCharAtGenerator generator(object,
4124 &index_out_of_range,
4125 STRING_INDEX_IS_NUMBER);
4126 generator.GenerateFast(masm_);
4129 __ bind(&index_out_of_range);
4130 // When the index is out of range, the spec requires us to return
4131 // the empty string.
4132 __ LoadRoot(result, Heap::kempty_stringRootIndex);
4135 __ bind(&need_conversion);
4136 // Move smi zero into the result register, which will trigger
4138 __ li(result, Operand(Smi::FromInt(0)));
4141 NopRuntimeCallHelper call_helper;
4142 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
4145 context()->Plug(result);
4149 void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
4150 ZoneList<Expression*>* args = expr->arguments();
4151 DCHECK_EQ(2, args->length());
4152 VisitForStackValue(args->at(0));
4153 VisitForAccumulatorValue(args->at(1));
4156 __ mov(a0, result_register()); // StringAddStub requires args in a0, a1.
4157 StringAddStub stub(isolate(), STRING_ADD_CHECK_BOTH, NOT_TENURED);
4159 context()->Plug(v0);
4163 void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
4164 ZoneList<Expression*>* args = expr->arguments();
4165 DCHECK(args->length() >= 2);
4167 int arg_count = args->length() - 2; // 2 ~ receiver and function.
4168 for (int i = 0; i < arg_count + 1; i++) {
4169 VisitForStackValue(args->at(i));
4171 VisitForAccumulatorValue(args->last()); // Function.
4173 Label runtime, done;
4174 // Check for non-function argument (including proxy).
4175 __ JumpIfSmi(v0, &runtime);
4176 __ GetObjectType(v0, a1, a1);
4177 __ Branch(&runtime, ne, a1, Operand(JS_FUNCTION_TYPE));
4179 // InvokeFunction requires the function in a1. Move it in there.
4180 __ mov(a1, result_register());
4181 ParameterCount count(arg_count);
4182 __ InvokeFunction(a1, count, CALL_FUNCTION, NullCallWrapper());
4183 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4188 __ CallRuntime(Runtime::kCall, args->length());
4191 context()->Plug(v0);
4195 void FullCodeGenerator::EmitDefaultConstructorCallSuper(CallRuntime* expr) {
4196 ZoneList<Expression*>* args = expr->arguments();
4197 DCHECK(args->length() == 2);
4200 VisitForStackValue(args->at(0));
4203 VisitForStackValue(args->at(1));
4204 __ CallRuntime(Runtime::kGetPrototype, 1);
4205 __ Push(result_register());
4207 // Load original constructor into a4.
4208 __ ld(a4, MemOperand(sp, 1 * kPointerSize));
4210 // Check if the calling frame is an arguments adaptor frame.
4211 Label adaptor_frame, args_set_up, runtime;
4212 __ ld(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4213 __ ld(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
4214 __ Branch(&adaptor_frame, eq, a3,
4215 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4216 // default constructor has no arguments, so no adaptor frame means no args.
4217 __ mov(a0, zero_reg);
4218 __ Branch(&args_set_up);
4220 // Copy arguments from adaptor frame.
4222 __ bind(&adaptor_frame);
4223 __ ld(a1, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4224 __ SmiUntag(a1, a1);
4228 // Get arguments pointer in a2.
4229 __ dsll(at, a1, kPointerSizeLog2);
4230 __ Daddu(a2, a2, Operand(at));
4231 __ Daddu(a2, a2, Operand(StandardFrameConstants::kCallerSPOffset));
4234 // Pre-decrement a2 with kPointerSize on each iteration.
4235 // Pre-decrement in order to skip receiver.
4236 __ Daddu(a2, a2, Operand(-kPointerSize));
4237 __ ld(a3, MemOperand(a2));
4239 __ Daddu(a1, a1, Operand(-1));
4240 __ Branch(&loop, ne, a1, Operand(zero_reg));
4243 __ bind(&args_set_up);
4244 __ dsll(at, a0, kPointerSizeLog2);
4245 __ Daddu(at, at, Operand(sp));
4246 __ ld(a1, MemOperand(at, 0));
4247 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4249 CallConstructStub stub(isolate(), SUPER_CONSTRUCTOR_CALL);
4250 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
4254 context()->Plug(result_register());
4258 void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
4259 RegExpConstructResultStub stub(isolate());
4260 ZoneList<Expression*>* args = expr->arguments();
4261 DCHECK(args->length() == 3);
4262 VisitForStackValue(args->at(0));
4263 VisitForStackValue(args->at(1));
4264 VisitForAccumulatorValue(args->at(2));
4265 __ mov(a0, result_register());
4269 context()->Plug(v0);
4273 void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
4274 ZoneList<Expression*>* args = expr->arguments();
4275 DCHECK_EQ(2, args->length());
4277 DCHECK_NOT_NULL(args->at(0)->AsLiteral());
4278 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->value()))->value();
4280 Handle<FixedArray> jsfunction_result_caches(
4281 isolate()->native_context()->jsfunction_result_caches());
4282 if (jsfunction_result_caches->length() <= cache_id) {
4283 __ Abort(kAttemptToUseUndefinedCache);
4284 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
4285 context()->Plug(v0);
4289 VisitForAccumulatorValue(args->at(1));
4292 Register cache = a1;
4293 __ ld(cache, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
4294 __ ld(cache, FieldMemOperand(cache, GlobalObject::kNativeContextOffset));
4297 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
4299 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
4302 Label done, not_found;
4303 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
4304 __ ld(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
4305 // a2 now holds finger offset as a smi.
4306 __ Daddu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4307 // a3 now points to the start of fixed array elements.
4308 __ SmiScale(at, a2, kPointerSizeLog2);
4309 __ daddu(a3, a3, at);
4310 // a3 now points to key of indexed element of cache.
4311 __ ld(a2, MemOperand(a3));
4312 __ Branch(¬_found, ne, key, Operand(a2));
4314 __ ld(v0, MemOperand(a3, kPointerSize));
4317 __ bind(¬_found);
4318 // Call runtime to perform the lookup.
4319 __ Push(cache, key);
4320 __ CallRuntime(Runtime::kGetFromCacheRT, 2);
4323 context()->Plug(v0);
4327 void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
4328 ZoneList<Expression*>* args = expr->arguments();
4329 VisitForAccumulatorValue(args->at(0));
4331 Label materialize_true, materialize_false;
4332 Label* if_true = NULL;
4333 Label* if_false = NULL;
4334 Label* fall_through = NULL;
4335 context()->PrepareTest(&materialize_true, &materialize_false,
4336 &if_true, &if_false, &fall_through);
4338 __ lwu(a0, FieldMemOperand(v0, String::kHashFieldOffset));
4339 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
4341 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4342 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
4344 context()->Plug(if_true, if_false);
4348 void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
4349 ZoneList<Expression*>* args = expr->arguments();
4350 DCHECK(args->length() == 1);
4351 VisitForAccumulatorValue(args->at(0));
4353 __ AssertString(v0);
4355 __ lwu(v0, FieldMemOperand(v0, String::kHashFieldOffset));
4356 __ IndexFromHash(v0, v0);
4358 context()->Plug(v0);
4362 void FullCodeGenerator::EmitFastOneByteArrayJoin(CallRuntime* expr) {
4363 Label bailout, done, one_char_separator, long_separator,
4364 non_trivial_array, not_size_one_array, loop,
4365 empty_separator_loop, one_char_separator_loop,
4366 one_char_separator_loop_entry, long_separator_loop;
4367 ZoneList<Expression*>* args = expr->arguments();
4368 DCHECK(args->length() == 2);
4369 VisitForStackValue(args->at(1));
4370 VisitForAccumulatorValue(args->at(0));
4372 // All aliases of the same register have disjoint lifetimes.
4373 Register array = v0;
4374 Register elements = no_reg; // Will be v0.
4375 Register result = no_reg; // Will be v0.
4376 Register separator = a1;
4377 Register array_length = a2;
4378 Register result_pos = no_reg; // Will be a2.
4379 Register string_length = a3;
4380 Register string = a4;
4381 Register element = a5;
4382 Register elements_end = a6;
4383 Register scratch1 = a7;
4384 Register scratch2 = t1;
4385 Register scratch3 = t0;
4387 // Separator operand is on the stack.
4390 // Check that the array is a JSArray.
4391 __ JumpIfSmi(array, &bailout);
4392 __ GetObjectType(array, scratch1, scratch2);
4393 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
4395 // Check that the array has fast elements.
4396 __ CheckFastElements(scratch1, scratch2, &bailout);
4398 // If the array has length zero, return the empty string.
4399 __ ld(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
4400 __ SmiUntag(array_length);
4401 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
4402 __ LoadRoot(v0, Heap::kempty_stringRootIndex);
4405 __ bind(&non_trivial_array);
4407 // Get the FixedArray containing array's elements.
4409 __ ld(elements, FieldMemOperand(array, JSArray::kElementsOffset));
4410 array = no_reg; // End of array's live range.
4412 // Check that all array elements are sequential one-byte strings, and
4413 // accumulate the sum of their lengths, as a smi-encoded value.
4414 __ mov(string_length, zero_reg);
4416 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4417 __ dsll(elements_end, array_length, kPointerSizeLog2);
4418 __ Daddu(elements_end, element, elements_end);
4419 // Loop condition: while (element < elements_end).
4420 // Live values in registers:
4421 // elements: Fixed array of strings.
4422 // array_length: Length of the fixed array of strings (not smi)
4423 // separator: Separator string
4424 // string_length: Accumulated sum of string lengths (smi).
4425 // element: Current array element.
4426 // elements_end: Array end.
4427 if (generate_debug_code_) {
4428 __ Assert(gt, kNoEmptyArraysHereInEmitFastOneByteArrayJoin, array_length,
4432 __ ld(string, MemOperand(element));
4433 __ Daddu(element, element, kPointerSize);
4434 __ JumpIfSmi(string, &bailout);
4435 __ ld(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
4436 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
4437 __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
4438 __ ld(scratch1, FieldMemOperand(string, SeqOneByteString::kLengthOffset));
4439 __ DadduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
4440 __ BranchOnOverflow(&bailout, scratch3);
4441 __ Branch(&loop, lt, element, Operand(elements_end));
4443 // If array_length is 1, return elements[0], a string.
4444 __ Branch(¬_size_one_array, ne, array_length, Operand(1));
4445 __ ld(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
4448 __ bind(¬_size_one_array);
4450 // Live values in registers:
4451 // separator: Separator string
4452 // array_length: Length of the array.
4453 // string_length: Sum of string lengths (smi).
4454 // elements: FixedArray of strings.
4456 // Check that the separator is a flat one-byte string.
4457 __ JumpIfSmi(separator, &bailout);
4458 __ ld(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
4459 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
4460 __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
4462 // Add (separator length times array_length) - separator length to the
4463 // string_length to get the length of the result string. array_length is not
4464 // smi but the other values are, so the result is a smi.
4465 __ ld(scratch1, FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
4466 __ Dsubu(string_length, string_length, Operand(scratch1));
4467 __ SmiUntag(scratch1);
4468 __ Dmul(scratch2, array_length, scratch1);
4469 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
4471 __ dsra32(scratch1, scratch2, 0);
4472 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
4473 __ SmiUntag(string_length);
4474 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
4475 __ BranchOnOverflow(&bailout, scratch3);
4477 // Get first element in the array to free up the elements register to be used
4480 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4481 result = elements; // End of live range for elements.
4483 // Live values in registers:
4484 // element: First array element
4485 // separator: Separator string
4486 // string_length: Length of result string (not smi)
4487 // array_length: Length of the array.
4488 __ AllocateOneByteString(result, string_length, scratch1, scratch2,
4489 elements_end, &bailout);
4490 // Prepare for looping. Set up elements_end to end of the array. Set
4491 // result_pos to the position of the result where to write the first
4493 __ dsll(elements_end, array_length, kPointerSizeLog2);
4494 __ Daddu(elements_end, element, elements_end);
4495 result_pos = array_length; // End of live range for array_length.
4496 array_length = no_reg;
4497 __ Daddu(result_pos,
4499 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4501 // Check the length of the separator.
4502 __ ld(scratch1, FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
4503 __ li(at, Operand(Smi::FromInt(1)));
4504 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
4505 __ Branch(&long_separator, gt, scratch1, Operand(at));
4507 // Empty separator case.
4508 __ bind(&empty_separator_loop);
4509 // Live values in registers:
4510 // result_pos: the position to which we are currently copying characters.
4511 // element: Current array element.
4512 // elements_end: Array end.
4514 // Copy next array element to the result.
4515 __ ld(string, MemOperand(element));
4516 __ Daddu(element, element, kPointerSize);
4517 __ ld(string_length, FieldMemOperand(string, String::kLengthOffset));
4518 __ SmiUntag(string_length);
4519 __ Daddu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4520 __ CopyBytes(string, result_pos, string_length, scratch1);
4521 // End while (element < elements_end).
4522 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
4523 DCHECK(result.is(v0));
4526 // One-character separator case.
4527 __ bind(&one_char_separator);
4528 // Replace separator with its one-byte character value.
4529 __ lbu(separator, FieldMemOperand(separator, SeqOneByteString::kHeaderSize));
4530 // Jump into the loop after the code that copies the separator, so the first
4531 // element is not preceded by a separator.
4532 __ jmp(&one_char_separator_loop_entry);
4534 __ bind(&one_char_separator_loop);
4535 // Live values in registers:
4536 // result_pos: the position to which we are currently copying characters.
4537 // element: Current array element.
4538 // elements_end: Array end.
4539 // separator: Single separator one-byte char (in lower byte).
4541 // Copy the separator character to the result.
4542 __ sb(separator, MemOperand(result_pos));
4543 __ Daddu(result_pos, result_pos, 1);
4545 // Copy next array element to the result.
4546 __ bind(&one_char_separator_loop_entry);
4547 __ ld(string, MemOperand(element));
4548 __ Daddu(element, element, kPointerSize);
4549 __ ld(string_length, FieldMemOperand(string, String::kLengthOffset));
4550 __ SmiUntag(string_length);
4551 __ Daddu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4552 __ CopyBytes(string, result_pos, string_length, scratch1);
4553 // End while (element < elements_end).
4554 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
4555 DCHECK(result.is(v0));
4558 // Long separator case (separator is more than one character). Entry is at the
4559 // label long_separator below.
4560 __ bind(&long_separator_loop);
4561 // Live values in registers:
4562 // result_pos: the position to which we are currently copying characters.
4563 // element: Current array element.
4564 // elements_end: Array end.
4565 // separator: Separator string.
4567 // Copy the separator to the result.
4568 __ ld(string_length, FieldMemOperand(separator, String::kLengthOffset));
4569 __ SmiUntag(string_length);
4572 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4573 __ CopyBytes(string, result_pos, string_length, scratch1);
4575 __ bind(&long_separator);
4576 __ ld(string, MemOperand(element));
4577 __ Daddu(element, element, kPointerSize);
4578 __ ld(string_length, FieldMemOperand(string, String::kLengthOffset));
4579 __ SmiUntag(string_length);
4580 __ Daddu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4581 __ CopyBytes(string, result_pos, string_length, scratch1);
4582 // End while (element < elements_end).
4583 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
4584 DCHECK(result.is(v0));
4588 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
4590 context()->Plug(v0);
4594 void FullCodeGenerator::EmitDebugIsActive(CallRuntime* expr) {
4595 DCHECK(expr->arguments()->length() == 0);
4596 ExternalReference debug_is_active =
4597 ExternalReference::debug_is_active_address(isolate());
4598 __ li(at, Operand(debug_is_active));
4599 __ lbu(v0, MemOperand(at));
4601 context()->Plug(v0);
4605 void FullCodeGenerator::EmitLoadJSRuntimeFunction(CallRuntime* expr) {
4606 // Push the builtins object as the receiver.
4607 Register receiver = LoadDescriptor::ReceiverRegister();
4608 __ ld(receiver, GlobalObjectOperand());
4609 __ ld(receiver, FieldMemOperand(receiver, GlobalObject::kBuiltinsOffset));
4612 // Load the function from the receiver.
4613 __ li(LoadDescriptor::NameRegister(), Operand(expr->name()));
4614 __ li(LoadDescriptor::SlotRegister(),
4615 Operand(SmiFromSlot(expr->CallRuntimeFeedbackSlot())));
4616 CallLoadIC(NOT_INSIDE_TYPEOF);
4620 void FullCodeGenerator::EmitCallJSRuntimeFunction(CallRuntime* expr) {
4621 ZoneList<Expression*>* args = expr->arguments();
4622 int arg_count = args->length();
4624 SetCallPosition(expr, arg_count);
4625 CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
4626 __ ld(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
4631 void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
4632 ZoneList<Expression*>* args = expr->arguments();
4633 int arg_count = args->length();
4635 if (expr->is_jsruntime()) {
4636 Comment cmnt(masm_, "[ CallRuntime");
4637 EmitLoadJSRuntimeFunction(expr);
4639 // Push the target function under the receiver.
4640 __ ld(at, MemOperand(sp, 0));
4642 __ sd(v0, MemOperand(sp, kPointerSize));
4644 // Push the arguments ("left-to-right").
4645 for (int i = 0; i < arg_count; i++) {
4646 VisitForStackValue(args->at(i));
4649 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4650 EmitCallJSRuntimeFunction(expr);
4652 // Restore context register.
4653 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4655 context()->DropAndPlug(1, v0);
4657 const Runtime::Function* function = expr->function();
4658 switch (function->function_id) {
4659 #define CALL_INTRINSIC_GENERATOR(Name) \
4660 case Runtime::kInline##Name: { \
4661 Comment cmnt(masm_, "[ Inline" #Name); \
4662 return Emit##Name(expr); \
4664 FOR_EACH_FULL_CODE_INTRINSIC(CALL_INTRINSIC_GENERATOR)
4665 #undef CALL_INTRINSIC_GENERATOR
4667 Comment cmnt(masm_, "[ CallRuntime for unhandled intrinsic");
4668 // Push the arguments ("left-to-right").
4669 for (int i = 0; i < arg_count; i++) {
4670 VisitForStackValue(args->at(i));
4673 // Call the C runtime function.
4674 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4675 __ CallRuntime(expr->function(), arg_count);
4676 context()->Plug(v0);
4683 void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
4684 switch (expr->op()) {
4685 case Token::DELETE: {
4686 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
4687 Property* property = expr->expression()->AsProperty();
4688 VariableProxy* proxy = expr->expression()->AsVariableProxy();
4690 if (property != NULL) {
4691 VisitForStackValue(property->obj());
4692 VisitForStackValue(property->key());
4693 __ li(a1, Operand(Smi::FromInt(language_mode())));
4695 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4696 context()->Plug(v0);
4697 } else if (proxy != NULL) {
4698 Variable* var = proxy->var();
4699 // Delete of an unqualified identifier is disallowed in strict mode but
4700 // "delete this" is allowed.
4701 bool is_this = var->HasThisName(isolate());
4702 DCHECK(is_sloppy(language_mode()) || is_this);
4703 if (var->IsUnallocatedOrGlobalSlot()) {
4704 __ ld(a2, GlobalObjectOperand());
4705 __ li(a1, Operand(var->name()));
4706 __ li(a0, Operand(Smi::FromInt(SLOPPY)));
4707 __ Push(a2, a1, a0);
4708 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4709 context()->Plug(v0);
4710 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
4711 // Result of deleting non-global, non-dynamic variables is false.
4712 // The subexpression does not have side effects.
4713 context()->Plug(is_this);
4715 // Non-global variable. Call the runtime to try to delete from the
4716 // context where the variable was introduced.
4717 DCHECK(!context_register().is(a2));
4718 __ li(a2, Operand(var->name()));
4719 __ Push(context_register(), a2);
4720 __ CallRuntime(Runtime::kDeleteLookupSlot, 2);
4721 context()->Plug(v0);
4724 // Result of deleting non-property, non-variable reference is true.
4725 // The subexpression may have side effects.
4726 VisitForEffect(expr->expression());
4727 context()->Plug(true);
4733 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
4734 VisitForEffect(expr->expression());
4735 context()->Plug(Heap::kUndefinedValueRootIndex);
4740 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
4741 if (context()->IsEffect()) {
4742 // Unary NOT has no side effects so it's only necessary to visit the
4743 // subexpression. Match the optimizing compiler by not branching.
4744 VisitForEffect(expr->expression());
4745 } else if (context()->IsTest()) {
4746 const TestContext* test = TestContext::cast(context());
4747 // The labels are swapped for the recursive call.
4748 VisitForControl(expr->expression(),
4749 test->false_label(),
4751 test->fall_through());
4752 context()->Plug(test->true_label(), test->false_label());
4754 // We handle value contexts explicitly rather than simply visiting
4755 // for control and plugging the control flow into the context,
4756 // because we need to prepare a pair of extra administrative AST ids
4757 // for the optimizing compiler.
4758 DCHECK(context()->IsAccumulatorValue() || context()->IsStackValue());
4759 Label materialize_true, materialize_false, done;
4760 VisitForControl(expr->expression(),
4764 __ bind(&materialize_true);
4765 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
4766 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
4767 if (context()->IsStackValue()) __ push(v0);
4769 __ bind(&materialize_false);
4770 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
4771 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
4772 if (context()->IsStackValue()) __ push(v0);
4778 case Token::TYPEOF: {
4779 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
4781 AccumulatorValueContext context(this);
4782 VisitForTypeofValue(expr->expression());
4785 TypeofStub typeof_stub(isolate());
4786 __ CallStub(&typeof_stub);
4787 context()->Plug(v0);
4797 void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
4798 DCHECK(expr->expression()->IsValidReferenceExpressionOrThis());
4800 Comment cmnt(masm_, "[ CountOperation");
4802 Property* prop = expr->expression()->AsProperty();
4803 LhsKind assign_type = Property::GetAssignType(prop);
4805 // Evaluate expression and get value.
4806 if (assign_type == VARIABLE) {
4807 DCHECK(expr->expression()->AsVariableProxy()->var() != NULL);
4808 AccumulatorValueContext context(this);
4809 EmitVariableLoad(expr->expression()->AsVariableProxy());
4811 // Reserve space for result of postfix operation.
4812 if (expr->is_postfix() && !context()->IsEffect()) {
4813 __ li(at, Operand(Smi::FromInt(0)));
4816 switch (assign_type) {
4817 case NAMED_PROPERTY: {
4818 // Put the object both on the stack and in the register.
4819 VisitForStackValue(prop->obj());
4820 __ ld(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
4821 EmitNamedPropertyLoad(prop);
4825 case NAMED_SUPER_PROPERTY: {
4826 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4827 VisitForAccumulatorValue(
4828 prop->obj()->AsSuperPropertyReference()->home_object());
4829 __ Push(result_register());
4830 const Register scratch = a1;
4831 __ ld(scratch, MemOperand(sp, kPointerSize));
4832 __ Push(scratch, result_register());
4833 EmitNamedSuperPropertyLoad(prop);
4837 case KEYED_SUPER_PROPERTY: {
4838 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4839 VisitForAccumulatorValue(
4840 prop->obj()->AsSuperPropertyReference()->home_object());
4841 const Register scratch = a1;
4842 const Register scratch1 = a4;
4843 __ Move(scratch, result_register());
4844 VisitForAccumulatorValue(prop->key());
4845 __ Push(scratch, result_register());
4846 __ ld(scratch1, MemOperand(sp, 2 * kPointerSize));
4847 __ Push(scratch1, scratch, result_register());
4848 EmitKeyedSuperPropertyLoad(prop);
4852 case KEYED_PROPERTY: {
4853 VisitForStackValue(prop->obj());
4854 VisitForStackValue(prop->key());
4855 __ ld(LoadDescriptor::ReceiverRegister(),
4856 MemOperand(sp, 1 * kPointerSize));
4857 __ ld(LoadDescriptor::NameRegister(), MemOperand(sp, 0));
4858 EmitKeyedPropertyLoad(prop);
4867 // We need a second deoptimization point after loading the value
4868 // in case evaluating the property load my have a side effect.
4869 if (assign_type == VARIABLE) {
4870 PrepareForBailout(expr->expression(), TOS_REG);
4872 PrepareForBailoutForId(prop->LoadId(), TOS_REG);
4875 // Inline smi case if we are in a loop.
4876 Label stub_call, done;
4877 JumpPatchSite patch_site(masm_);
4879 int count_value = expr->op() == Token::INC ? 1 : -1;
4881 if (ShouldInlineSmiCase(expr->op())) {
4883 patch_site.EmitJumpIfNotSmi(v0, &slow);
4885 // Save result for postfix expressions.
4886 if (expr->is_postfix()) {
4887 if (!context()->IsEffect()) {
4888 // Save the result on the stack. If we have a named or keyed property
4889 // we store the result under the receiver that is currently on top
4891 switch (assign_type) {
4895 case NAMED_PROPERTY:
4896 __ sd(v0, MemOperand(sp, kPointerSize));
4898 case NAMED_SUPER_PROPERTY:
4899 __ sd(v0, MemOperand(sp, 2 * kPointerSize));
4901 case KEYED_PROPERTY:
4902 __ sd(v0, MemOperand(sp, 2 * kPointerSize));
4904 case KEYED_SUPER_PROPERTY:
4905 __ sd(v0, MemOperand(sp, 3 * kPointerSize));
4911 Register scratch1 = a1;
4912 Register scratch2 = a4;
4913 __ li(scratch1, Operand(Smi::FromInt(count_value)));
4914 __ DadduAndCheckForOverflow(v0, v0, scratch1, scratch2);
4915 __ BranchOnNoOverflow(&done, scratch2);
4916 // Call stub. Undo operation first.
4921 if (!is_strong(language_mode())) {
4922 ToNumberStub convert_stub(isolate());
4923 __ CallStub(&convert_stub);
4924 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
4927 // Save result for postfix expressions.
4928 if (expr->is_postfix()) {
4929 if (!context()->IsEffect()) {
4930 // Save the result on the stack. If we have a named or keyed property
4931 // we store the result under the receiver that is currently on top
4933 switch (assign_type) {
4937 case NAMED_PROPERTY:
4938 __ sd(v0, MemOperand(sp, kPointerSize));
4940 case NAMED_SUPER_PROPERTY:
4941 __ sd(v0, MemOperand(sp, 2 * kPointerSize));
4943 case KEYED_PROPERTY:
4944 __ sd(v0, MemOperand(sp, 2 * kPointerSize));
4946 case KEYED_SUPER_PROPERTY:
4947 __ sd(v0, MemOperand(sp, 3 * kPointerSize));
4953 __ bind(&stub_call);
4955 __ li(a0, Operand(Smi::FromInt(count_value)));
4957 SetExpressionPosition(expr);
4960 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), Token::ADD,
4961 strength(language_mode())).code();
4962 CallIC(code, expr->CountBinOpFeedbackId());
4963 patch_site.EmitPatchInfo();
4966 if (is_strong(language_mode())) {
4967 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
4969 // Store the value returned in v0.
4970 switch (assign_type) {
4972 if (expr->is_postfix()) {
4973 { EffectContext context(this);
4974 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4975 Token::ASSIGN, expr->CountSlot());
4976 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4979 // For all contexts except EffectConstant we have the result on
4980 // top of the stack.
4981 if (!context()->IsEffect()) {
4982 context()->PlugTOS();
4985 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4986 Token::ASSIGN, expr->CountSlot());
4987 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4988 context()->Plug(v0);
4991 case NAMED_PROPERTY: {
4992 __ mov(StoreDescriptor::ValueRegister(), result_register());
4993 __ li(StoreDescriptor::NameRegister(),
4994 Operand(prop->key()->AsLiteral()->value()));
4995 __ pop(StoreDescriptor::ReceiverRegister());
4996 if (FLAG_vector_stores) {
4997 EmitLoadStoreICSlot(expr->CountSlot());
5000 CallStoreIC(expr->CountStoreFeedbackId());
5002 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5003 if (expr->is_postfix()) {
5004 if (!context()->IsEffect()) {
5005 context()->PlugTOS();
5008 context()->Plug(v0);
5012 case NAMED_SUPER_PROPERTY: {
5013 EmitNamedSuperPropertyStore(prop);
5014 if (expr->is_postfix()) {
5015 if (!context()->IsEffect()) {
5016 context()->PlugTOS();
5019 context()->Plug(v0);
5023 case KEYED_SUPER_PROPERTY: {
5024 EmitKeyedSuperPropertyStore(prop);
5025 if (expr->is_postfix()) {
5026 if (!context()->IsEffect()) {
5027 context()->PlugTOS();
5030 context()->Plug(v0);
5034 case KEYED_PROPERTY: {
5035 __ mov(StoreDescriptor::ValueRegister(), result_register());
5036 __ Pop(StoreDescriptor::ReceiverRegister(),
5037 StoreDescriptor::NameRegister());
5039 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
5040 if (FLAG_vector_stores) {
5041 EmitLoadStoreICSlot(expr->CountSlot());
5044 CallIC(ic, expr->CountStoreFeedbackId());
5046 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5047 if (expr->is_postfix()) {
5048 if (!context()->IsEffect()) {
5049 context()->PlugTOS();
5052 context()->Plug(v0);
5060 void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
5061 Expression* sub_expr,
5062 Handle<String> check) {
5063 Label materialize_true, materialize_false;
5064 Label* if_true = NULL;
5065 Label* if_false = NULL;
5066 Label* fall_through = NULL;
5067 context()->PrepareTest(&materialize_true, &materialize_false,
5068 &if_true, &if_false, &fall_through);
5070 { AccumulatorValueContext context(this);
5071 VisitForTypeofValue(sub_expr);
5073 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5075 Factory* factory = isolate()->factory();
5076 if (String::Equals(check, factory->number_string())) {
5077 __ JumpIfSmi(v0, if_true);
5078 __ ld(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
5079 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5080 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
5081 } else if (String::Equals(check, factory->string_string())) {
5082 __ JumpIfSmi(v0, if_false);
5083 // Check for undetectable objects => false.
5084 __ GetObjectType(v0, v0, a1);
5085 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
5086 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
5087 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
5088 Split(eq, a1, Operand(zero_reg),
5089 if_true, if_false, fall_through);
5090 } else if (String::Equals(check, factory->symbol_string())) {
5091 __ JumpIfSmi(v0, if_false);
5092 __ GetObjectType(v0, v0, a1);
5093 Split(eq, a1, Operand(SYMBOL_TYPE), if_true, if_false, fall_through);
5094 } else if (String::Equals(check, factory->float32x4_string())) {
5095 __ JumpIfSmi(v0, if_false);
5096 __ GetObjectType(v0, v0, a1);
5097 Split(eq, a1, Operand(FLOAT32X4_TYPE), if_true, if_false, fall_through);
5098 } else if (String::Equals(check, factory->boolean_string())) {
5099 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5100 __ Branch(if_true, eq, v0, Operand(at));
5101 __ LoadRoot(at, Heap::kFalseValueRootIndex);
5102 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
5103 } else if (String::Equals(check, factory->undefined_string())) {
5104 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5105 __ Branch(if_true, eq, v0, Operand(at));
5106 __ JumpIfSmi(v0, if_false);
5107 // Check for undetectable objects => true.
5108 __ ld(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
5109 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
5110 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
5111 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
5112 } else if (String::Equals(check, factory->function_string())) {
5113 __ JumpIfSmi(v0, if_false);
5114 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5115 __ GetObjectType(v0, v0, a1);
5116 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
5117 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
5118 if_true, if_false, fall_through);
5119 } else if (String::Equals(check, factory->object_string())) {
5120 __ JumpIfSmi(v0, if_false);
5121 __ LoadRoot(at, Heap::kNullValueRootIndex);
5122 __ Branch(if_true, eq, v0, Operand(at));
5123 // Check for JS objects => true.
5124 __ GetObjectType(v0, v0, a1);
5125 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
5126 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
5127 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
5128 // Check for undetectable objects => false.
5129 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
5130 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
5131 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
5133 if (if_false != fall_through) __ jmp(if_false);
5135 context()->Plug(if_true, if_false);
5139 void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
5140 Comment cmnt(masm_, "[ CompareOperation");
5141 SetExpressionPosition(expr);
5143 // First we try a fast inlined version of the compare when one of
5144 // the operands is a literal.
5145 if (TryLiteralCompare(expr)) return;
5147 // Always perform the comparison for its control flow. Pack the result
5148 // into the expression's context after the comparison is performed.
5149 Label materialize_true, materialize_false;
5150 Label* if_true = NULL;
5151 Label* if_false = NULL;
5152 Label* fall_through = NULL;
5153 context()->PrepareTest(&materialize_true, &materialize_false,
5154 &if_true, &if_false, &fall_through);
5156 Token::Value op = expr->op();
5157 VisitForStackValue(expr->left());
5160 VisitForStackValue(expr->right());
5161 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
5162 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
5163 __ LoadRoot(a4, Heap::kTrueValueRootIndex);
5164 Split(eq, v0, Operand(a4), if_true, if_false, fall_through);
5167 case Token::INSTANCEOF: {
5168 VisitForStackValue(expr->right());
5169 InstanceofStub stub(isolate(), InstanceofStub::kNoFlags);
5171 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5172 // The stub returns 0 for true.
5173 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
5178 VisitForAccumulatorValue(expr->right());
5179 Condition cc = CompareIC::ComputeCondition(op);
5180 __ mov(a0, result_register());
5183 bool inline_smi_code = ShouldInlineSmiCase(op);
5184 JumpPatchSite patch_site(masm_);
5185 if (inline_smi_code) {
5187 __ Or(a2, a0, Operand(a1));
5188 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
5189 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
5190 __ bind(&slow_case);
5193 Handle<Code> ic = CodeFactory::CompareIC(
5194 isolate(), op, strength(language_mode())).code();
5195 CallIC(ic, expr->CompareOperationFeedbackId());
5196 patch_site.EmitPatchInfo();
5197 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5198 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
5202 // Convert the result of the comparison into one expected for this
5203 // expression's context.
5204 context()->Plug(if_true, if_false);
5208 void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
5209 Expression* sub_expr,
5211 Label materialize_true, materialize_false;
5212 Label* if_true = NULL;
5213 Label* if_false = NULL;
5214 Label* fall_through = NULL;
5215 context()->PrepareTest(&materialize_true, &materialize_false,
5216 &if_true, &if_false, &fall_through);
5218 VisitForAccumulatorValue(sub_expr);
5219 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5220 __ mov(a0, result_register());
5221 if (expr->op() == Token::EQ_STRICT) {
5222 Heap::RootListIndex nil_value = nil == kNullValue ?
5223 Heap::kNullValueRootIndex :
5224 Heap::kUndefinedValueRootIndex;
5225 __ LoadRoot(a1, nil_value);
5226 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
5228 Handle<Code> ic = CompareNilICStub::GetUninitialized(isolate(), nil);
5229 CallIC(ic, expr->CompareOperationFeedbackId());
5230 Split(ne, v0, Operand(zero_reg), if_true, if_false, fall_through);
5232 context()->Plug(if_true, if_false);
5236 void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
5237 __ ld(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5238 context()->Plug(v0);
5242 Register FullCodeGenerator::result_register() {
5247 Register FullCodeGenerator::context_register() {
5252 void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
5253 // DCHECK_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
5254 DCHECK(IsAligned(frame_offset, kPointerSize));
5255 // __ sw(value, MemOperand(fp, frame_offset));
5256 __ sd(value, MemOperand(fp, frame_offset));
5260 void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
5261 __ ld(dst, ContextOperand(cp, context_index));
5265 void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
5266 Scope* closure_scope = scope()->ClosureScope();
5267 if (closure_scope->is_script_scope() ||
5268 closure_scope->is_module_scope()) {
5269 // Contexts nested in the native context have a canonical empty function
5270 // as their closure, not the anonymous closure containing the global
5271 // code. Pass a smi sentinel and let the runtime look up the empty
5273 __ li(at, Operand(Smi::FromInt(0)));
5274 } else if (closure_scope->is_eval_scope()) {
5275 // Contexts created by a call to eval have the same closure as the
5276 // context calling eval, not the anonymous closure containing the eval
5277 // code. Fetch it from the context.
5278 __ ld(at, ContextOperand(cp, Context::CLOSURE_INDEX));
5280 DCHECK(closure_scope->is_function_scope());
5281 __ ld(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5287 // ----------------------------------------------------------------------------
5288 // Non-local control flow support.
5290 void FullCodeGenerator::EnterFinallyBlock() {
5291 DCHECK(!result_register().is(a1));
5292 // Store result register while executing finally block.
5293 __ push(result_register());
5294 // Cook return address in link register to stack (smi encoded Code* delta).
5295 __ Dsubu(a1, ra, Operand(masm_->CodeObject()));
5298 // Store result register while executing finally block.
5301 // Store pending message while executing finally block.
5302 ExternalReference pending_message_obj =
5303 ExternalReference::address_of_pending_message_obj(isolate());
5304 __ li(at, Operand(pending_message_obj));
5305 __ ld(a1, MemOperand(at));
5308 ClearPendingMessage();
5312 void FullCodeGenerator::ExitFinallyBlock() {
5313 DCHECK(!result_register().is(a1));
5314 // Restore pending message from stack.
5316 ExternalReference pending_message_obj =
5317 ExternalReference::address_of_pending_message_obj(isolate());
5318 __ li(at, Operand(pending_message_obj));
5319 __ sd(a1, MemOperand(at));
5321 // Restore result register from stack.
5324 // Uncook return address and return.
5325 __ pop(result_register());
5328 __ Daddu(at, a1, Operand(masm_->CodeObject()));
5333 void FullCodeGenerator::ClearPendingMessage() {
5334 DCHECK(!result_register().is(a1));
5335 ExternalReference pending_message_obj =
5336 ExternalReference::address_of_pending_message_obj(isolate());
5337 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
5338 __ li(at, Operand(pending_message_obj));
5339 __ sd(a1, MemOperand(at));
5343 void FullCodeGenerator::EmitLoadStoreICSlot(FeedbackVectorICSlot slot) {
5344 DCHECK(FLAG_vector_stores && !slot.IsInvalid());
5345 __ li(VectorStoreICTrampolineDescriptor::SlotRegister(),
5346 Operand(SmiFromSlot(slot)));
5353 void BackEdgeTable::PatchAt(Code* unoptimized_code,
5355 BackEdgeState target_state,
5356 Code* replacement_code) {
5357 static const int kInstrSize = Assembler::kInstrSize;
5358 Address branch_address = pc - 8 * kInstrSize;
5359 CodePatcher patcher(branch_address, 1);
5361 switch (target_state) {
5363 // slt at, a3, zero_reg (in case of count based interrupts)
5364 // beq at, zero_reg, ok
5365 // lui t9, <interrupt stub address> upper
5366 // ori t9, <interrupt stub address> u-middle
5368 // ori t9, <interrupt stub address> lower
5371 // ok-label ----- pc_after points here
5372 patcher.masm()->slt(at, a3, zero_reg);
5374 case ON_STACK_REPLACEMENT:
5375 case OSR_AFTER_STACK_CHECK:
5376 // addiu at, zero_reg, 1
5377 // beq at, zero_reg, ok ;; Not changed
5378 // lui t9, <on-stack replacement address> upper
5379 // ori t9, <on-stack replacement address> middle
5381 // ori t9, <on-stack replacement address> lower
5382 // jalr t9 ;; Not changed
5383 // nop ;; Not changed
5384 // ok-label ----- pc_after points here
5385 patcher.masm()->daddiu(at, zero_reg, 1);
5388 Address pc_immediate_load_address = pc - 6 * kInstrSize;
5389 // Replace the stack check address in the load-immediate (6-instr sequence)
5390 // with the entry address of the replacement code.
5391 Assembler::set_target_address_at(pc_immediate_load_address,
5392 replacement_code->entry());
5394 unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
5395 unoptimized_code, pc_immediate_load_address, replacement_code);
5399 BackEdgeTable::BackEdgeState BackEdgeTable::GetBackEdgeState(
5401 Code* unoptimized_code,
5403 static const int kInstrSize = Assembler::kInstrSize;
5404 Address branch_address = pc - 8 * kInstrSize;
5405 Address pc_immediate_load_address = pc - 6 * kInstrSize;
5407 DCHECK(Assembler::IsBeq(Assembler::instr_at(pc - 7 * kInstrSize)));
5408 if (!Assembler::IsAddImmediate(Assembler::instr_at(branch_address))) {
5409 DCHECK(reinterpret_cast<uint64_t>(
5410 Assembler::target_address_at(pc_immediate_load_address)) ==
5411 reinterpret_cast<uint64_t>(
5412 isolate->builtins()->InterruptCheck()->entry()));
5416 DCHECK(Assembler::IsAddImmediate(Assembler::instr_at(branch_address)));
5418 if (reinterpret_cast<uint64_t>(
5419 Assembler::target_address_at(pc_immediate_load_address)) ==
5420 reinterpret_cast<uint64_t>(
5421 isolate->builtins()->OnStackReplacement()->entry())) {
5422 return ON_STACK_REPLACEMENT;
5425 DCHECK(reinterpret_cast<uint64_t>(
5426 Assembler::target_address_at(pc_immediate_load_address)) ==
5427 reinterpret_cast<uint64_t>(
5428 isolate->builtins()->OsrAfterStackCheck()->entry()));
5429 return OSR_AFTER_STACK_CHECK;
5433 } // namespace internal
5436 #endif // V8_TARGET_ARCH_MIPS64