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.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());
363 { Comment cmnt(masm_, "[ Stack check");
364 PrepareForBailoutForId(BailoutId::Declarations(), NO_REGISTERS);
366 __ LoadRoot(at, Heap::kStackLimitRootIndex);
367 __ Branch(&ok, hs, sp, Operand(at));
368 Handle<Code> stack_check = isolate()->builtins()->StackCheck();
369 PredictableCodeSizeScope predictable(masm_,
370 masm_->CallSize(stack_check, RelocInfo::CODE_TARGET));
371 __ Call(stack_check, RelocInfo::CODE_TARGET);
375 { Comment cmnt(masm_, "[ Body");
376 DCHECK(loop_depth() == 0);
378 VisitStatements(function()->body());
380 DCHECK(loop_depth() == 0);
384 // Always emit a 'return undefined' in case control fell off the end of
386 { Comment cmnt(masm_, "[ return <undefined>;");
387 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
389 EmitReturnSequence();
393 void FullCodeGenerator::ClearAccumulator() {
394 DCHECK(Smi::FromInt(0) == 0);
395 __ mov(v0, zero_reg);
399 void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
400 __ li(a2, Operand(profiling_counter_));
401 __ ld(a3, FieldMemOperand(a2, Cell::kValueOffset));
402 __ Dsubu(a3, a3, Operand(Smi::FromInt(delta)));
403 __ sd(a3, FieldMemOperand(a2, Cell::kValueOffset));
407 void FullCodeGenerator::EmitProfilingCounterReset() {
408 int reset_value = FLAG_interrupt_budget;
409 if (info_->is_debug()) {
410 // Detect debug break requests as soon as possible.
411 reset_value = FLAG_interrupt_budget >> 4;
413 __ li(a2, Operand(profiling_counter_));
414 __ li(a3, Operand(Smi::FromInt(reset_value)));
415 __ sd(a3, FieldMemOperand(a2, Cell::kValueOffset));
419 void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
420 Label* back_edge_target) {
421 // The generated code is used in Deoptimizer::PatchStackCheckCodeAt so we need
422 // to make sure it is constant. Branch may emit a skip-or-jump sequence
423 // instead of the normal Branch. It seems that the "skip" part of that
424 // sequence is about as long as this Branch would be so it is safe to ignore
426 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
427 Comment cmnt(masm_, "[ Back edge bookkeeping");
429 DCHECK(back_edge_target->is_bound());
430 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
431 int weight = Min(kMaxBackEdgeWeight,
432 Max(1, distance / kCodeSizeMultiplier));
433 EmitProfilingCounterDecrement(weight);
434 __ slt(at, a3, zero_reg);
435 __ beq(at, zero_reg, &ok);
436 // Call will emit a li t9 first, so it is safe to use the delay slot.
437 __ Call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
438 // Record a mapping of this PC offset to the OSR id. This is used to find
439 // the AST id from the unoptimized code in order to use it as a key into
440 // the deoptimization input data found in the optimized code.
441 RecordBackEdge(stmt->OsrEntryId());
442 EmitProfilingCounterReset();
445 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
446 // Record a mapping of the OSR id to this PC. This is used if the OSR
447 // entry becomes the target of a bailout. We don't expect it to be, but
448 // we want it to work if it is.
449 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
453 void FullCodeGenerator::EmitReturnSequence() {
454 Comment cmnt(masm_, "[ Return sequence");
455 if (return_label_.is_bound()) {
456 __ Branch(&return_label_);
458 __ bind(&return_label_);
460 // Push the return value on the stack as the parameter.
461 // Runtime::TraceExit returns its parameter in v0.
463 __ CallRuntime(Runtime::kTraceExit, 1);
465 // Pretend that the exit is a backwards jump to the entry.
467 if (info_->ShouldSelfOptimize()) {
468 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
470 int distance = masm_->pc_offset();
471 weight = Min(kMaxBackEdgeWeight,
472 Max(1, distance / kCodeSizeMultiplier));
474 EmitProfilingCounterDecrement(weight);
476 __ Branch(&ok, ge, a3, Operand(zero_reg));
478 __ Call(isolate()->builtins()->InterruptCheck(),
479 RelocInfo::CODE_TARGET);
481 EmitProfilingCounterReset();
484 // Make sure that the constant pool is not emitted inside of the return
486 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
487 // Here we use masm_-> instead of the __ macro to avoid the code coverage
488 // tool from instrumenting as we rely on the code size here.
489 int32_t arg_count = info_->scope()->num_parameters() + 1;
490 int32_t sp_delta = arg_count * kPointerSize;
491 SetReturnPosition(function());
493 int no_frame_start = masm_->pc_offset();
494 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
495 masm_->Daddu(sp, sp, Operand(sp_delta));
497 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
503 void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
504 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
505 codegen()->GetVar(result_register(), var);
506 __ push(result_register());
510 void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
514 void FullCodeGenerator::AccumulatorValueContext::Plug(
515 Heap::RootListIndex index) const {
516 __ LoadRoot(result_register(), index);
520 void FullCodeGenerator::StackValueContext::Plug(
521 Heap::RootListIndex index) const {
522 __ LoadRoot(result_register(), index);
523 __ push(result_register());
527 void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
528 codegen()->PrepareForBailoutBeforeSplit(condition(),
532 if (index == Heap::kUndefinedValueRootIndex ||
533 index == Heap::kNullValueRootIndex ||
534 index == Heap::kFalseValueRootIndex) {
535 if (false_label_ != fall_through_) __ Branch(false_label_);
536 } else if (index == Heap::kTrueValueRootIndex) {
537 if (true_label_ != fall_through_) __ Branch(true_label_);
539 __ LoadRoot(result_register(), index);
540 codegen()->DoTest(this);
545 void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
549 void FullCodeGenerator::AccumulatorValueContext::Plug(
550 Handle<Object> lit) const {
551 __ li(result_register(), Operand(lit));
555 void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
556 // Immediates cannot be pushed directly.
557 __ li(result_register(), Operand(lit));
558 __ push(result_register());
562 void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
563 codegen()->PrepareForBailoutBeforeSplit(condition(),
567 DCHECK(!lit->IsUndetectableObject()); // There are no undetectable literals.
568 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
569 if (false_label_ != fall_through_) __ Branch(false_label_);
570 } else if (lit->IsTrue() || lit->IsJSObject()) {
571 if (true_label_ != fall_through_) __ Branch(true_label_);
572 } else if (lit->IsString()) {
573 if (String::cast(*lit)->length() == 0) {
574 if (false_label_ != fall_through_) __ Branch(false_label_);
576 if (true_label_ != fall_through_) __ Branch(true_label_);
578 } else if (lit->IsSmi()) {
579 if (Smi::cast(*lit)->value() == 0) {
580 if (false_label_ != fall_through_) __ Branch(false_label_);
582 if (true_label_ != fall_through_) __ Branch(true_label_);
585 // For simplicity we always test the accumulator register.
586 __ li(result_register(), Operand(lit));
587 codegen()->DoTest(this);
592 void FullCodeGenerator::EffectContext::DropAndPlug(int count,
593 Register reg) const {
599 void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
601 Register reg) const {
604 __ Move(result_register(), reg);
608 void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
609 Register reg) const {
611 if (count > 1) __ Drop(count - 1);
612 __ sd(reg, MemOperand(sp, 0));
616 void FullCodeGenerator::TestContext::DropAndPlug(int count,
617 Register reg) const {
619 // For simplicity we always test the accumulator register.
621 __ Move(result_register(), reg);
622 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
623 codegen()->DoTest(this);
627 void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
628 Label* materialize_false) const {
629 DCHECK(materialize_true == materialize_false);
630 __ bind(materialize_true);
634 void FullCodeGenerator::AccumulatorValueContext::Plug(
635 Label* materialize_true,
636 Label* materialize_false) const {
638 __ bind(materialize_true);
639 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
641 __ bind(materialize_false);
642 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
647 void FullCodeGenerator::StackValueContext::Plug(
648 Label* materialize_true,
649 Label* materialize_false) const {
651 __ bind(materialize_true);
652 __ LoadRoot(at, Heap::kTrueValueRootIndex);
653 // Push the value as the following branch can clobber at in long branch mode.
656 __ bind(materialize_false);
657 __ LoadRoot(at, Heap::kFalseValueRootIndex);
663 void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
664 Label* materialize_false) const {
665 DCHECK(materialize_true == true_label_);
666 DCHECK(materialize_false == false_label_);
670 void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
671 Heap::RootListIndex value_root_index =
672 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
673 __ LoadRoot(result_register(), value_root_index);
677 void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
678 Heap::RootListIndex value_root_index =
679 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
680 __ LoadRoot(at, value_root_index);
685 void FullCodeGenerator::TestContext::Plug(bool flag) const {
686 codegen()->PrepareForBailoutBeforeSplit(condition(),
691 if (true_label_ != fall_through_) __ Branch(true_label_);
693 if (false_label_ != fall_through_) __ Branch(false_label_);
698 void FullCodeGenerator::DoTest(Expression* condition,
701 Label* fall_through) {
702 __ mov(a0, result_register());
703 Handle<Code> ic = ToBooleanStub::GetUninitialized(isolate());
704 CallIC(ic, condition->test_id());
705 __ mov(at, zero_reg);
706 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
710 void FullCodeGenerator::Split(Condition cc,
715 Label* fall_through) {
716 if (if_false == fall_through) {
717 __ Branch(if_true, cc, lhs, rhs);
718 } else if (if_true == fall_through) {
719 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
721 __ Branch(if_true, cc, lhs, rhs);
727 MemOperand FullCodeGenerator::StackOperand(Variable* var) {
728 DCHECK(var->IsStackAllocated());
729 // Offset is negative because higher indexes are at lower addresses.
730 int offset = -var->index() * kPointerSize;
731 // Adjust by a (parameter or local) base offset.
732 if (var->IsParameter()) {
733 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
735 offset += JavaScriptFrameConstants::kLocal0Offset;
737 return MemOperand(fp, offset);
741 MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
742 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
743 if (var->IsContextSlot()) {
744 int context_chain_length = scope()->ContextChainLength(var->scope());
745 __ LoadContext(scratch, context_chain_length);
746 return ContextOperand(scratch, var->index());
748 return StackOperand(var);
753 void FullCodeGenerator::GetVar(Register dest, Variable* var) {
754 // Use destination as scratch.
755 MemOperand location = VarOperand(var, dest);
756 __ ld(dest, location);
760 void FullCodeGenerator::SetVar(Variable* var,
764 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
765 DCHECK(!scratch0.is(src));
766 DCHECK(!scratch0.is(scratch1));
767 DCHECK(!scratch1.is(src));
768 MemOperand location = VarOperand(var, scratch0);
769 __ sd(src, location);
770 // Emit the write barrier code if the location is in the heap.
771 if (var->IsContextSlot()) {
772 __ RecordWriteContextSlot(scratch0,
782 void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
783 bool should_normalize,
786 // Only prepare for bailouts before splits if we're in a test
787 // context. Otherwise, we let the Visit function deal with the
788 // preparation to avoid preparing with the same AST id twice.
789 if (!context()->IsTest() || !info_->IsOptimizable()) return;
792 if (should_normalize) __ Branch(&skip);
793 PrepareForBailout(expr, TOS_REG);
794 if (should_normalize) {
795 __ LoadRoot(a4, Heap::kTrueValueRootIndex);
796 Split(eq, a0, Operand(a4), if_true, if_false, NULL);
802 void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
803 // The variable in the declaration always resides in the current function
805 DCHECK_EQ(0, scope()->ContextChainLength(variable->scope()));
806 if (generate_debug_code_) {
807 // Check that we're not inside a with or catch context.
808 __ ld(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
809 __ LoadRoot(a4, Heap::kWithContextMapRootIndex);
810 __ Check(ne, kDeclarationInWithContext,
812 __ LoadRoot(a4, Heap::kCatchContextMapRootIndex);
813 __ Check(ne, kDeclarationInCatchContext,
819 void FullCodeGenerator::VisitVariableDeclaration(
820 VariableDeclaration* declaration) {
821 // If it was not possible to allocate the variable at compile time, we
822 // need to "declare" it at runtime to make sure it actually exists in the
824 VariableProxy* proxy = declaration->proxy();
825 VariableMode mode = declaration->mode();
826 Variable* variable = proxy->var();
827 bool hole_init = mode == LET || mode == CONST || mode == CONST_LEGACY;
828 switch (variable->location()) {
829 case VariableLocation::GLOBAL:
830 case VariableLocation::UNALLOCATED:
831 globals_->Add(variable->name(), zone());
832 globals_->Add(variable->binding_needs_init()
833 ? isolate()->factory()->the_hole_value()
834 : isolate()->factory()->undefined_value(),
838 case VariableLocation::PARAMETER:
839 case VariableLocation::LOCAL:
841 Comment cmnt(masm_, "[ VariableDeclaration");
842 __ LoadRoot(a4, Heap::kTheHoleValueRootIndex);
843 __ sd(a4, StackOperand(variable));
847 case VariableLocation::CONTEXT:
849 Comment cmnt(masm_, "[ VariableDeclaration");
850 EmitDebugCheckDeclarationContext(variable);
851 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
852 __ sd(at, ContextOperand(cp, variable->index()));
853 // No write barrier since the_hole_value is in old space.
854 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
858 case VariableLocation::LOOKUP: {
859 Comment cmnt(masm_, "[ VariableDeclaration");
860 __ li(a2, Operand(variable->name()));
861 // Declaration nodes are always introduced in one of four modes.
862 DCHECK(IsDeclaredVariableMode(mode));
863 PropertyAttributes attr =
864 IsImmutableVariableMode(mode) ? READ_ONLY : NONE;
865 __ li(a1, Operand(Smi::FromInt(attr)));
866 // Push initial value, if any.
867 // Note: For variables we must not push an initial value (such as
868 // 'undefined') because we may have a (legal) redeclaration and we
869 // must not destroy the current value.
871 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
872 __ Push(cp, a2, a1, a0);
874 DCHECK(Smi::FromInt(0) == 0);
875 __ mov(a0, zero_reg); // Smi::FromInt(0) indicates no initial value.
876 __ Push(cp, a2, a1, a0);
878 __ CallRuntime(Runtime::kDeclareLookupSlot, 4);
885 void FullCodeGenerator::VisitFunctionDeclaration(
886 FunctionDeclaration* declaration) {
887 VariableProxy* proxy = declaration->proxy();
888 Variable* variable = proxy->var();
889 switch (variable->location()) {
890 case VariableLocation::GLOBAL:
891 case VariableLocation::UNALLOCATED: {
892 globals_->Add(variable->name(), zone());
893 Handle<SharedFunctionInfo> function =
894 Compiler::GetSharedFunctionInfo(declaration->fun(), script(), info_);
895 // Check for stack-overflow exception.
896 if (function.is_null()) return SetStackOverflow();
897 globals_->Add(function, zone());
901 case VariableLocation::PARAMETER:
902 case VariableLocation::LOCAL: {
903 Comment cmnt(masm_, "[ FunctionDeclaration");
904 VisitForAccumulatorValue(declaration->fun());
905 __ sd(result_register(), StackOperand(variable));
909 case VariableLocation::CONTEXT: {
910 Comment cmnt(masm_, "[ FunctionDeclaration");
911 EmitDebugCheckDeclarationContext(variable);
912 VisitForAccumulatorValue(declaration->fun());
913 __ sd(result_register(), ContextOperand(cp, variable->index()));
914 int offset = Context::SlotOffset(variable->index());
915 // We know that we have written a function, which is not a smi.
916 __ RecordWriteContextSlot(cp,
924 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
928 case VariableLocation::LOOKUP: {
929 Comment cmnt(masm_, "[ FunctionDeclaration");
930 __ li(a2, Operand(variable->name()));
931 __ li(a1, Operand(Smi::FromInt(NONE)));
933 // Push initial value for function declaration.
934 VisitForStackValue(declaration->fun());
935 __ CallRuntime(Runtime::kDeclareLookupSlot, 4);
942 void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
943 // Call the runtime to declare the globals.
944 // The context is the first argument.
945 __ li(a1, Operand(pairs));
946 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
948 __ CallRuntime(Runtime::kDeclareGlobals, 3);
949 // Return value is ignored.
953 void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
954 // Call the runtime to declare the modules.
955 __ Push(descriptions);
956 __ CallRuntime(Runtime::kDeclareModules, 1);
957 // Return value is ignored.
961 void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
962 Comment cmnt(masm_, "[ SwitchStatement");
963 Breakable nested_statement(this, stmt);
964 SetStatementPosition(stmt);
966 // Keep the switch value on the stack until a case matches.
967 VisitForStackValue(stmt->tag());
968 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
970 ZoneList<CaseClause*>* clauses = stmt->cases();
971 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
973 Label next_test; // Recycled for each test.
974 // Compile all the tests with branches to their bodies.
975 for (int i = 0; i < clauses->length(); i++) {
976 CaseClause* clause = clauses->at(i);
977 clause->body_target()->Unuse();
979 // The default is not a test, but remember it as final fall through.
980 if (clause->is_default()) {
981 default_clause = clause;
985 Comment cmnt(masm_, "[ Case comparison");
989 // Compile the label expression.
990 VisitForAccumulatorValue(clause->label());
991 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
993 // Perform the comparison as if via '==='.
994 __ ld(a1, MemOperand(sp, 0)); // Switch value.
995 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
996 JumpPatchSite patch_site(masm_);
997 if (inline_smi_code) {
1000 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
1002 __ Branch(&next_test, ne, a1, Operand(a0));
1003 __ Drop(1); // Switch value is no longer needed.
1004 __ Branch(clause->body_target());
1006 __ bind(&slow_case);
1009 // Record position before stub call for type feedback.
1010 SetExpressionPosition(clause);
1011 Handle<Code> ic = CodeFactory::CompareIC(isolate(), Token::EQ_STRICT,
1012 strength(language_mode())).code();
1013 CallIC(ic, clause->CompareId());
1014 patch_site.EmitPatchInfo();
1018 PrepareForBailout(clause, TOS_REG);
1019 __ LoadRoot(at, Heap::kTrueValueRootIndex);
1020 __ Branch(&next_test, ne, v0, Operand(at));
1022 __ Branch(clause->body_target());
1025 __ Branch(&next_test, ne, v0, Operand(zero_reg));
1026 __ Drop(1); // Switch value is no longer needed.
1027 __ Branch(clause->body_target());
1030 // Discard the test value and jump to the default if present, otherwise to
1031 // the end of the statement.
1032 __ bind(&next_test);
1033 __ Drop(1); // Switch value is no longer needed.
1034 if (default_clause == NULL) {
1035 __ Branch(nested_statement.break_label());
1037 __ Branch(default_clause->body_target());
1040 // Compile all the case bodies.
1041 for (int i = 0; i < clauses->length(); i++) {
1042 Comment cmnt(masm_, "[ Case body");
1043 CaseClause* clause = clauses->at(i);
1044 __ bind(clause->body_target());
1045 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
1046 VisitStatements(clause->statements());
1049 __ bind(nested_statement.break_label());
1050 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1054 void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
1055 Comment cmnt(masm_, "[ ForInStatement");
1056 SetStatementPosition(stmt, SKIP_BREAK);
1058 FeedbackVectorSlot slot = stmt->ForInFeedbackSlot();
1061 ForIn loop_statement(this, stmt);
1062 increment_loop_depth();
1064 // Get the object to enumerate over. If the object is null or undefined, skip
1065 // over the loop. See ECMA-262 version 5, section 12.6.4.
1066 SetExpressionAsStatementPosition(stmt->enumerable());
1067 VisitForAccumulatorValue(stmt->enumerable());
1068 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
1069 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1070 __ Branch(&exit, eq, a0, Operand(at));
1071 Register null_value = a5;
1072 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
1073 __ Branch(&exit, eq, a0, Operand(null_value));
1074 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1076 // Convert the object to a JS object.
1077 Label convert, done_convert;
1078 __ JumpIfSmi(a0, &convert);
1079 __ GetObjectType(a0, a1, a1);
1080 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
1083 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1085 __ bind(&done_convert);
1086 PrepareForBailoutForId(stmt->ToObjectId(), TOS_REG);
1089 // Check for proxies.
1091 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1092 __ GetObjectType(a0, a1, a1);
1093 __ Branch(&call_runtime, le, a1, Operand(LAST_JS_PROXY_TYPE));
1095 // Check cache validity in generated code. This is a fast case for
1096 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1097 // guarantee cache validity, call the runtime system to check cache
1098 // validity or get the property names in a fixed array.
1099 __ CheckEnumCache(null_value, &call_runtime);
1101 // The enum cache is valid. Load the map of the object being
1102 // iterated over and use the cache for the iteration.
1104 __ ld(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
1105 __ Branch(&use_cache);
1107 // Get the set of properties to enumerate.
1108 __ bind(&call_runtime);
1109 __ push(a0); // Duplicate the enumerable object on the stack.
1110 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1111 PrepareForBailoutForId(stmt->EnumId(), TOS_REG);
1113 // If we got a map from the runtime call, we can do a fast
1114 // modification check. Otherwise, we got a fixed array, and we have
1115 // to do a slow check.
1117 __ ld(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
1118 __ LoadRoot(at, Heap::kMetaMapRootIndex);
1119 __ Branch(&fixed_array, ne, a2, Operand(at));
1121 // We got a map in register v0. Get the enumeration cache from it.
1122 Label no_descriptors;
1123 __ bind(&use_cache);
1125 __ EnumLength(a1, v0);
1126 __ Branch(&no_descriptors, eq, a1, Operand(Smi::FromInt(0)));
1128 __ LoadInstanceDescriptors(v0, a2);
1129 __ ld(a2, FieldMemOperand(a2, DescriptorArray::kEnumCacheOffset));
1130 __ ld(a2, FieldMemOperand(a2, DescriptorArray::kEnumCacheBridgeCacheOffset));
1132 // Set up the four remaining stack slots.
1133 __ li(a0, Operand(Smi::FromInt(0)));
1134 // Push map, enumeration cache, enumeration cache length (as smi) and zero.
1135 __ Push(v0, a2, a1, a0);
1138 __ bind(&no_descriptors);
1142 // We got a fixed array in register v0. Iterate through that.
1144 __ bind(&fixed_array);
1146 __ li(a1, FeedbackVector());
1147 __ li(a2, Operand(TypeFeedbackVector::MegamorphicSentinel(isolate())));
1148 int vector_index = FeedbackVector()->GetIndex(slot);
1149 __ sd(a2, FieldMemOperand(a1, FixedArray::OffsetOfElementAt(vector_index)));
1151 __ li(a1, Operand(Smi::FromInt(1))); // Smi indicates slow check
1152 __ ld(a2, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1153 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1154 __ GetObjectType(a2, a3, a3);
1155 __ Branch(&non_proxy, gt, a3, Operand(LAST_JS_PROXY_TYPE));
1156 __ li(a1, Operand(Smi::FromInt(0))); // Zero indicates proxy
1157 __ bind(&non_proxy);
1158 __ Push(a1, v0); // Smi and array
1159 __ ld(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1160 __ li(a0, Operand(Smi::FromInt(0)));
1161 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1163 // Generate code for doing the condition check.
1164 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
1166 SetExpressionAsStatementPosition(stmt->each());
1168 // Load the current count to a0, load the length to a1.
1169 __ ld(a0, MemOperand(sp, 0 * kPointerSize));
1170 __ ld(a1, MemOperand(sp, 1 * kPointerSize));
1171 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
1173 // Get the current entry of the array into register a3.
1174 __ ld(a2, MemOperand(sp, 2 * kPointerSize));
1175 __ Daddu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1176 __ SmiScale(a4, a0, kPointerSizeLog2);
1177 __ daddu(a4, a2, a4); // Array base + scaled (smi) index.
1178 __ ld(a3, MemOperand(a4)); // Current entry.
1180 // Get the expected map from the stack or a smi in the
1181 // permanent slow case into register a2.
1182 __ ld(a2, MemOperand(sp, 3 * kPointerSize));
1184 // Check if the expected map still matches that of the enumerable.
1185 // If not, we may have to filter the key.
1187 __ ld(a1, MemOperand(sp, 4 * kPointerSize));
1188 __ ld(a4, FieldMemOperand(a1, HeapObject::kMapOffset));
1189 __ Branch(&update_each, eq, a4, Operand(a2));
1191 // For proxies, no filtering is done.
1192 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1193 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
1194 __ Branch(&update_each, eq, a2, Operand(zero_reg));
1196 // Convert the entry to a string or (smi) 0 if it isn't a property
1197 // any more. If the property has been removed while iterating, we
1199 __ Push(a1, a3); // Enumerable and current entry.
1200 __ CallRuntime(Runtime::kForInFilter, 2);
1201 PrepareForBailoutForId(stmt->FilterId(), TOS_REG);
1202 __ mov(a3, result_register());
1203 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1204 __ Branch(loop_statement.continue_label(), eq, a3, Operand(at));
1206 // Update the 'each' property or variable from the possibly filtered
1207 // entry in register a3.
1208 __ bind(&update_each);
1209 __ mov(result_register(), a3);
1210 // Perform the assignment as if via '='.
1211 { EffectContext context(this);
1212 EmitAssignment(stmt->each(), stmt->EachFeedbackSlot());
1213 PrepareForBailoutForId(stmt->AssignmentId(), NO_REGISTERS);
1216 // Generate code for the body of the loop.
1217 Visit(stmt->body());
1219 // Generate code for the going to the next element by incrementing
1220 // the index (smi) stored on top of the stack.
1221 __ bind(loop_statement.continue_label());
1223 __ Daddu(a0, a0, Operand(Smi::FromInt(1)));
1226 EmitBackEdgeBookkeeping(stmt, &loop);
1229 // Remove the pointers stored on the stack.
1230 __ bind(loop_statement.break_label());
1233 // Exit and decrement the loop depth.
1234 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1236 decrement_loop_depth();
1240 void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1242 // Use the fast case closure allocation code that allocates in new
1243 // space for nested functions that don't need literals cloning. If
1244 // we're running with the --always-opt or the --prepare-always-opt
1245 // flag, we need to use the runtime function so that the new function
1246 // we are creating here gets a chance to have its code optimized and
1247 // doesn't just get a copy of the existing unoptimized code.
1248 if (!FLAG_always_opt &&
1249 !FLAG_prepare_always_opt &&
1251 scope()->is_function_scope() &&
1252 info->num_literals() == 0) {
1253 FastNewClosureStub stub(isolate(), info->language_mode(), info->kind());
1254 __ li(a2, Operand(info));
1257 __ li(a0, Operand(info));
1258 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1259 : Heap::kFalseValueRootIndex);
1260 __ Push(cp, a0, a1);
1261 __ CallRuntime(Runtime::kNewClosure, 3);
1263 context()->Plug(v0);
1267 void FullCodeGenerator::EmitSetHomeObjectIfNeeded(Expression* initializer,
1269 FeedbackVectorICSlot slot) {
1270 if (NeedsHomeObject(initializer)) {
1271 __ ld(StoreDescriptor::ReceiverRegister(), MemOperand(sp));
1272 __ li(StoreDescriptor::NameRegister(),
1273 Operand(isolate()->factory()->home_object_symbol()));
1274 __ ld(StoreDescriptor::ValueRegister(),
1275 MemOperand(sp, offset * kPointerSize));
1276 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
1282 void FullCodeGenerator::EmitLoadGlobalCheckExtensions(VariableProxy* proxy,
1283 TypeofMode typeof_mode,
1285 Register current = cp;
1291 if (s->num_heap_slots() > 0) {
1292 if (s->calls_sloppy_eval()) {
1293 // Check that extension is NULL.
1294 __ ld(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1295 __ Branch(slow, ne, temp, Operand(zero_reg));
1297 // Load next context in chain.
1298 __ ld(next, ContextOperand(current, Context::PREVIOUS_INDEX));
1299 // Walk the rest of the chain without clobbering cp.
1302 // If no outer scope calls eval, we do not need to check more
1303 // context extensions.
1304 if (!s->outer_scope_calls_sloppy_eval() || s->is_eval_scope()) break;
1305 s = s->outer_scope();
1308 if (s->is_eval_scope()) {
1310 if (!current.is(next)) {
1311 __ Move(next, current);
1314 // Terminate at native context.
1315 __ ld(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1316 __ LoadRoot(a4, Heap::kNativeContextMapRootIndex);
1317 __ Branch(&fast, eq, temp, Operand(a4));
1318 // Check that extension is NULL.
1319 __ ld(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1320 __ Branch(slow, ne, temp, Operand(zero_reg));
1321 // Load next context in chain.
1322 __ ld(next, ContextOperand(next, Context::PREVIOUS_INDEX));
1327 // All extension objects were empty and it is safe to use a normal global
1329 EmitGlobalVariableLoad(proxy, typeof_mode);
1333 MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1335 DCHECK(var->IsContextSlot());
1336 Register context = cp;
1340 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
1341 if (s->num_heap_slots() > 0) {
1342 if (s->calls_sloppy_eval()) {
1343 // Check that extension is NULL.
1344 __ ld(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1345 __ Branch(slow, ne, temp, Operand(zero_reg));
1347 __ ld(next, ContextOperand(context, Context::PREVIOUS_INDEX));
1348 // Walk the rest of the chain without clobbering cp.
1352 // Check that last extension is NULL.
1353 __ ld(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1354 __ Branch(slow, ne, temp, Operand(zero_reg));
1356 // This function is used only for loads, not stores, so it's safe to
1357 // return an cp-based operand (the write barrier cannot be allowed to
1358 // destroy the cp register).
1359 return ContextOperand(context, var->index());
1363 void FullCodeGenerator::EmitDynamicLookupFastCase(VariableProxy* proxy,
1364 TypeofMode typeof_mode,
1365 Label* slow, Label* done) {
1366 // Generate fast-case code for variables that might be shadowed by
1367 // eval-introduced variables. Eval is used a lot without
1368 // introducing variables. In those cases, we do not want to
1369 // perform a runtime call for all variables in the scope
1370 // containing the eval.
1371 Variable* var = proxy->var();
1372 if (var->mode() == DYNAMIC_GLOBAL) {
1373 EmitLoadGlobalCheckExtensions(proxy, typeof_mode, slow);
1375 } else if (var->mode() == DYNAMIC_LOCAL) {
1376 Variable* local = var->local_if_not_shadowed();
1377 __ ld(v0, ContextSlotOperandCheckExtensions(local, slow));
1378 if (local->mode() == LET || local->mode() == CONST ||
1379 local->mode() == CONST_LEGACY) {
1380 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1381 __ dsubu(at, v0, at); // Sub as compare: at == 0 on eq.
1382 if (local->mode() == CONST_LEGACY) {
1383 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1384 __ Movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
1385 } else { // LET || CONST
1386 __ Branch(done, ne, at, Operand(zero_reg));
1387 __ li(a0, Operand(var->name()));
1389 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1397 void FullCodeGenerator::EmitGlobalVariableLoad(VariableProxy* proxy,
1398 TypeofMode typeof_mode) {
1399 Variable* var = proxy->var();
1400 DCHECK(var->IsUnallocatedOrGlobalSlot() ||
1401 (var->IsLookupSlot() && var->mode() == DYNAMIC_GLOBAL));
1402 if (var->IsGlobalSlot()) {
1403 DCHECK(var->index() > 0);
1404 DCHECK(var->IsStaticGlobalObjectProperty());
1405 // Each var occupies two slots in the context: for reads and writes.
1406 int const slot = var->index();
1407 int const depth = scope()->ContextChainLength(var->scope());
1408 if (depth <= LoadGlobalViaContextStub::kMaximumDepth) {
1409 __ li(LoadGlobalViaContextDescriptor::SlotRegister(), Operand(slot));
1410 LoadGlobalViaContextStub stub(isolate(), depth);
1413 __ Push(Smi::FromInt(slot));
1414 __ CallRuntime(Runtime::kLoadGlobalViaContext, 1);
1418 __ ld(LoadDescriptor::ReceiverRegister(), GlobalObjectOperand());
1419 __ li(LoadDescriptor::NameRegister(), Operand(var->name()));
1420 __ li(LoadDescriptor::SlotRegister(),
1421 Operand(SmiFromSlot(proxy->VariableFeedbackSlot())));
1422 CallLoadIC(typeof_mode);
1427 void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy,
1428 TypeofMode typeof_mode) {
1429 // Record position before possible IC call.
1430 SetExpressionPosition(proxy);
1431 PrepareForBailoutForId(proxy->BeforeId(), NO_REGISTERS);
1432 Variable* var = proxy->var();
1434 // Three cases: global variables, lookup variables, and all other types of
1436 switch (var->location()) {
1437 case VariableLocation::GLOBAL:
1438 case VariableLocation::UNALLOCATED: {
1439 Comment cmnt(masm_, "[ Global variable");
1440 EmitGlobalVariableLoad(proxy, typeof_mode);
1441 context()->Plug(v0);
1445 case VariableLocation::PARAMETER:
1446 case VariableLocation::LOCAL:
1447 case VariableLocation::CONTEXT: {
1448 DCHECK_EQ(NOT_INSIDE_TYPEOF, typeof_mode);
1449 Comment cmnt(masm_, var->IsContextSlot() ? "[ Context variable"
1450 : "[ Stack variable");
1451 if (var->binding_needs_init()) {
1452 // var->scope() may be NULL when the proxy is located in eval code and
1453 // refers to a potential outside binding. Currently those bindings are
1454 // always looked up dynamically, i.e. in that case
1455 // var->location() == LOOKUP.
1457 DCHECK(var->scope() != NULL);
1459 // Check if the binding really needs an initialization check. The check
1460 // can be skipped in the following situation: we have a LET or CONST
1461 // binding in harmony mode, both the Variable and the VariableProxy have
1462 // the same declaration scope (i.e. they are both in global code, in the
1463 // same function or in the same eval code) and the VariableProxy is in
1464 // the source physically located after the initializer of the variable.
1466 // We cannot skip any initialization checks for CONST in non-harmony
1467 // mode because const variables may be declared but never initialized:
1468 // if (false) { const x; }; var y = x;
1470 // The condition on the declaration scopes is a conservative check for
1471 // nested functions that access a binding and are called before the
1472 // binding is initialized:
1473 // function() { f(); let x = 1; function f() { x = 2; } }
1475 bool skip_init_check;
1476 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1477 skip_init_check = false;
1478 } else if (var->is_this()) {
1479 CHECK(info_->function() != nullptr &&
1480 (info_->function()->kind() & kSubclassConstructor) != 0);
1481 // TODO(dslomov): implement 'this' hole check elimination.
1482 skip_init_check = false;
1484 // Check that we always have valid source position.
1485 DCHECK(var->initializer_position() != RelocInfo::kNoPosition);
1486 DCHECK(proxy->position() != RelocInfo::kNoPosition);
1487 skip_init_check = var->mode() != CONST_LEGACY &&
1488 var->initializer_position() < proxy->position();
1491 if (!skip_init_check) {
1492 // Let and const need a read barrier.
1494 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1495 __ dsubu(at, v0, at); // Sub as compare: at == 0 on eq.
1496 if (var->mode() == LET || var->mode() == CONST) {
1497 // Throw a reference error when using an uninitialized let/const
1498 // binding in harmony mode.
1500 __ Branch(&done, ne, at, Operand(zero_reg));
1501 __ li(a0, Operand(var->name()));
1503 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1506 // Uninitalized const bindings outside of harmony mode are unholed.
1507 DCHECK(var->mode() == CONST_LEGACY);
1508 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1509 __ Movz(v0, a0, at); // Conditional move: Undefined if TheHole.
1511 context()->Plug(v0);
1515 context()->Plug(var);
1519 case VariableLocation::LOOKUP: {
1520 Comment cmnt(masm_, "[ Lookup variable");
1522 // Generate code for loading from variables potentially shadowed
1523 // by eval-introduced variables.
1524 EmitDynamicLookupFastCase(proxy, typeof_mode, &slow, &done);
1526 __ li(a1, Operand(var->name()));
1527 __ Push(cp, a1); // Context and name.
1528 Runtime::FunctionId function_id =
1529 typeof_mode == NOT_INSIDE_TYPEOF
1530 ? Runtime::kLoadLookupSlot
1531 : Runtime::kLoadLookupSlotNoReferenceError;
1532 __ CallRuntime(function_id, 2);
1534 context()->Plug(v0);
1540 void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1541 Comment cmnt(masm_, "[ RegExpLiteral");
1543 // Registers will be used as follows:
1544 // a5 = materialized value (RegExp literal)
1545 // a4 = JS function, literals array
1546 // a3 = literal index
1547 // a2 = RegExp pattern
1548 // a1 = RegExp flags
1549 // a0 = RegExp literal clone
1550 __ ld(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1551 __ ld(a4, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1552 int literal_offset =
1553 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1554 __ ld(a5, FieldMemOperand(a4, literal_offset));
1555 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1556 __ Branch(&materialized, ne, a5, Operand(at));
1558 // Create regexp literal using runtime function.
1559 // Result will be in v0.
1560 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1561 __ li(a2, Operand(expr->pattern()));
1562 __ li(a1, Operand(expr->flags()));
1563 __ Push(a4, a3, a2, a1);
1564 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1567 __ bind(&materialized);
1568 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1569 Label allocated, runtime_allocate;
1570 __ Allocate(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1573 __ bind(&runtime_allocate);
1574 __ li(a0, Operand(Smi::FromInt(size)));
1576 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1579 __ bind(&allocated);
1581 // After this, registers are used as follows:
1582 // v0: Newly allocated regexp.
1583 // a5: Materialized regexp.
1585 __ CopyFields(v0, a5, a2.bit(), size / kPointerSize);
1586 context()->Plug(v0);
1590 void FullCodeGenerator::EmitAccessor(Expression* expression) {
1591 if (expression == NULL) {
1592 __ LoadRoot(a1, Heap::kNullValueRootIndex);
1595 VisitForStackValue(expression);
1600 void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1601 Comment cmnt(masm_, "[ ObjectLiteral");
1603 Handle<FixedArray> constant_properties = expr->constant_properties();
1604 __ ld(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1605 __ ld(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1606 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1607 __ li(a1, Operand(constant_properties));
1608 __ li(a0, Operand(Smi::FromInt(expr->ComputeFlags())));
1609 if (MustCreateObjectLiteralWithRuntime(expr)) {
1610 __ Push(a3, a2, a1, a0);
1611 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1613 FastCloneShallowObjectStub stub(isolate(), expr->properties_count());
1616 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1618 // If result_saved is true the result is on top of the stack. If
1619 // result_saved is false the result is in v0.
1620 bool result_saved = false;
1622 AccessorTable accessor_table(zone());
1623 int property_index = 0;
1624 // store_slot_index points to the vector IC slot for the next store IC used.
1625 // ObjectLiteral::ComputeFeedbackRequirements controls the allocation of slots
1626 // and must be updated if the number of store ICs emitted here changes.
1627 int store_slot_index = 0;
1628 for (; property_index < expr->properties()->length(); property_index++) {
1629 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1630 if (property->is_computed_name()) break;
1631 if (property->IsCompileTimeValue()) continue;
1633 Literal* key = property->key()->AsLiteral();
1634 Expression* value = property->value();
1635 if (!result_saved) {
1636 __ push(v0); // Save result on stack.
1637 result_saved = true;
1639 switch (property->kind()) {
1640 case ObjectLiteral::Property::CONSTANT:
1642 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1643 DCHECK(!CompileTimeValue::IsCompileTimeValue(property->value()));
1645 case ObjectLiteral::Property::COMPUTED:
1646 // It is safe to use [[Put]] here because the boilerplate already
1647 // contains computed properties with an uninitialized value.
1648 if (key->value()->IsInternalizedString()) {
1649 if (property->emit_store()) {
1650 VisitForAccumulatorValue(value);
1651 __ mov(StoreDescriptor::ValueRegister(), result_register());
1652 DCHECK(StoreDescriptor::ValueRegister().is(a0));
1653 __ li(StoreDescriptor::NameRegister(), Operand(key->value()));
1654 __ ld(StoreDescriptor::ReceiverRegister(), MemOperand(sp));
1655 if (FLAG_vector_stores) {
1656 EmitLoadStoreICSlot(expr->GetNthSlot(store_slot_index++));
1659 CallStoreIC(key->LiteralFeedbackId());
1661 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1663 if (NeedsHomeObject(value)) {
1664 __ Move(StoreDescriptor::ReceiverRegister(), v0);
1665 __ li(StoreDescriptor::NameRegister(),
1666 Operand(isolate()->factory()->home_object_symbol()));
1667 __ ld(StoreDescriptor::ValueRegister(), MemOperand(sp));
1668 if (FLAG_vector_stores) {
1669 EmitLoadStoreICSlot(expr->GetNthSlot(store_slot_index++));
1674 VisitForEffect(value);
1678 // Duplicate receiver on stack.
1679 __ ld(a0, MemOperand(sp));
1681 VisitForStackValue(key);
1682 VisitForStackValue(value);
1683 if (property->emit_store()) {
1684 EmitSetHomeObjectIfNeeded(
1685 value, 2, expr->SlotForHomeObject(value, &store_slot_index));
1686 __ li(a0, Operand(Smi::FromInt(SLOPPY))); // PropertyAttributes.
1688 __ CallRuntime(Runtime::kSetProperty, 4);
1693 case ObjectLiteral::Property::PROTOTYPE:
1694 // Duplicate receiver on stack.
1695 __ ld(a0, MemOperand(sp));
1697 VisitForStackValue(value);
1698 DCHECK(property->emit_store());
1699 __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1701 case ObjectLiteral::Property::GETTER:
1702 if (property->emit_store()) {
1703 accessor_table.lookup(key)->second->getter = value;
1706 case ObjectLiteral::Property::SETTER:
1707 if (property->emit_store()) {
1708 accessor_table.lookup(key)->second->setter = value;
1714 // Emit code to define accessors, using only a single call to the runtime for
1715 // each pair of corresponding getters and setters.
1716 for (AccessorTable::Iterator it = accessor_table.begin();
1717 it != accessor_table.end();
1719 __ ld(a0, MemOperand(sp)); // Duplicate receiver.
1721 VisitForStackValue(it->first);
1722 EmitAccessor(it->second->getter);
1723 EmitSetHomeObjectIfNeeded(
1724 it->second->getter, 2,
1725 expr->SlotForHomeObject(it->second->getter, &store_slot_index));
1726 EmitAccessor(it->second->setter);
1727 EmitSetHomeObjectIfNeeded(
1728 it->second->setter, 3,
1729 expr->SlotForHomeObject(it->second->setter, &store_slot_index));
1730 __ li(a0, Operand(Smi::FromInt(NONE)));
1732 __ CallRuntime(Runtime::kDefineAccessorPropertyUnchecked, 5);
1735 // Object literals have two parts. The "static" part on the left contains no
1736 // computed property names, and so we can compute its map ahead of time; see
1737 // runtime.cc::CreateObjectLiteralBoilerplate. The second "dynamic" part
1738 // starts with the first computed property name, and continues with all
1739 // properties to its right. All the code from above initializes the static
1740 // component of the object literal, and arranges for the map of the result to
1741 // reflect the static order in which the keys appear. For the dynamic
1742 // properties, we compile them into a series of "SetOwnProperty" runtime
1743 // calls. This will preserve insertion order.
1744 for (; property_index < expr->properties()->length(); property_index++) {
1745 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1747 Expression* value = property->value();
1748 if (!result_saved) {
1749 __ push(v0); // Save result on the stack
1750 result_saved = true;
1753 __ ld(a0, MemOperand(sp)); // Duplicate receiver.
1756 if (property->kind() == ObjectLiteral::Property::PROTOTYPE) {
1757 DCHECK(!property->is_computed_name());
1758 VisitForStackValue(value);
1759 DCHECK(property->emit_store());
1760 __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1762 EmitPropertyKey(property, expr->GetIdForProperty(property_index));
1763 VisitForStackValue(value);
1764 EmitSetHomeObjectIfNeeded(
1765 value, 2, expr->SlotForHomeObject(value, &store_slot_index));
1767 switch (property->kind()) {
1768 case ObjectLiteral::Property::CONSTANT:
1769 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1770 case ObjectLiteral::Property::COMPUTED:
1771 if (property->emit_store()) {
1772 __ li(a0, Operand(Smi::FromInt(NONE)));
1774 __ CallRuntime(Runtime::kDefineDataPropertyUnchecked, 4);
1780 case ObjectLiteral::Property::PROTOTYPE:
1784 case ObjectLiteral::Property::GETTER:
1785 __ li(a0, Operand(Smi::FromInt(NONE)));
1787 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
1790 case ObjectLiteral::Property::SETTER:
1791 __ li(a0, Operand(Smi::FromInt(NONE)));
1793 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
1799 if (expr->has_function()) {
1800 DCHECK(result_saved);
1801 __ ld(a0, MemOperand(sp));
1803 __ CallRuntime(Runtime::kToFastProperties, 1);
1807 context()->PlugTOS();
1809 context()->Plug(v0);
1812 // Verify that compilation exactly consumed the number of store ic slots that
1813 // the ObjectLiteral node had to offer.
1814 DCHECK(!FLAG_vector_stores || store_slot_index == expr->slot_count());
1818 void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1819 Comment cmnt(masm_, "[ ArrayLiteral");
1821 expr->BuildConstantElements(isolate());
1823 Handle<FixedArray> constant_elements = expr->constant_elements();
1824 bool has_fast_elements =
1825 IsFastObjectElementsKind(expr->constant_elements_kind());
1827 AllocationSiteMode allocation_site_mode = TRACK_ALLOCATION_SITE;
1828 if (has_fast_elements && !FLAG_allocation_site_pretenuring) {
1829 // If the only customer of allocation sites is transitioning, then
1830 // we can turn it off if we don't have anywhere else to transition to.
1831 allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
1834 __ mov(a0, result_register());
1835 __ ld(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1836 __ ld(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1837 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1838 __ li(a1, Operand(constant_elements));
1839 if (MustCreateArrayLiteralWithRuntime(expr)) {
1840 __ li(a0, Operand(Smi::FromInt(expr->ComputeFlags())));
1841 __ Push(a3, a2, a1, a0);
1842 __ CallRuntime(Runtime::kCreateArrayLiteral, 4);
1844 FastCloneShallowArrayStub stub(isolate(), allocation_site_mode);
1847 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1849 bool result_saved = false; // Is the result saved to the stack?
1850 ZoneList<Expression*>* subexprs = expr->values();
1851 int length = subexprs->length();
1853 // Emit code to evaluate all the non-constant subexpressions and to store
1854 // them into the newly cloned array.
1855 int array_index = 0;
1856 for (; array_index < length; array_index++) {
1857 Expression* subexpr = subexprs->at(array_index);
1858 if (subexpr->IsSpread()) break;
1860 // If the subexpression is a literal or a simple materialized literal it
1861 // is already set in the cloned array.
1862 if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
1864 if (!result_saved) {
1865 __ push(v0); // array literal
1866 __ Push(Smi::FromInt(expr->literal_index()));
1867 result_saved = true;
1870 VisitForAccumulatorValue(subexpr);
1872 if (has_fast_elements) {
1873 int offset = FixedArray::kHeaderSize + (array_index * kPointerSize);
1874 __ ld(a6, MemOperand(sp, kPointerSize)); // Copy of array literal.
1875 __ ld(a1, FieldMemOperand(a6, JSObject::kElementsOffset));
1876 __ sd(result_register(), FieldMemOperand(a1, offset));
1877 // Update the write barrier for the array store.
1878 __ RecordWriteField(a1, offset, result_register(), a2,
1879 kRAHasBeenSaved, kDontSaveFPRegs,
1880 EMIT_REMEMBERED_SET, INLINE_SMI_CHECK);
1882 __ li(a3, Operand(Smi::FromInt(array_index)));
1883 __ mov(a0, result_register());
1884 StoreArrayLiteralElementStub stub(isolate());
1888 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1891 // In case the array literal contains spread expressions it has two parts. The
1892 // first part is the "static" array which has a literal index is handled
1893 // above. The second part is the part after the first spread expression
1894 // (inclusive) and these elements gets appended to the array. Note that the
1895 // number elements an iterable produces is unknown ahead of time.
1896 if (array_index < length && result_saved) {
1897 __ Pop(); // literal index
1899 result_saved = false;
1901 for (; array_index < length; array_index++) {
1902 Expression* subexpr = subexprs->at(array_index);
1905 if (subexpr->IsSpread()) {
1906 VisitForStackValue(subexpr->AsSpread()->expression());
1907 __ InvokeBuiltin(Builtins::CONCAT_ITERABLE_TO_ARRAY, CALL_FUNCTION);
1909 VisitForStackValue(subexpr);
1910 __ CallRuntime(Runtime::kAppendElement, 2);
1913 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1917 __ Pop(); // literal index
1918 context()->PlugTOS();
1920 context()->Plug(v0);
1925 void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1926 DCHECK(expr->target()->IsValidReferenceExpressionOrThis());
1928 Comment cmnt(masm_, "[ Assignment");
1929 SetExpressionPosition(expr, INSERT_BREAK);
1931 Property* property = expr->target()->AsProperty();
1932 LhsKind assign_type = Property::GetAssignType(property);
1934 // Evaluate LHS expression.
1935 switch (assign_type) {
1937 // Nothing to do here.
1939 case NAMED_PROPERTY:
1940 if (expr->is_compound()) {
1941 // We need the receiver both on the stack and in the register.
1942 VisitForStackValue(property->obj());
1943 __ ld(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
1945 VisitForStackValue(property->obj());
1948 case NAMED_SUPER_PROPERTY:
1950 property->obj()->AsSuperPropertyReference()->this_var());
1951 VisitForAccumulatorValue(
1952 property->obj()->AsSuperPropertyReference()->home_object());
1953 __ Push(result_register());
1954 if (expr->is_compound()) {
1955 const Register scratch = a1;
1956 __ ld(scratch, MemOperand(sp, kPointerSize));
1957 __ Push(scratch, result_register());
1960 case KEYED_SUPER_PROPERTY: {
1961 const Register scratch = a1;
1963 property->obj()->AsSuperPropertyReference()->this_var());
1964 VisitForAccumulatorValue(
1965 property->obj()->AsSuperPropertyReference()->home_object());
1966 __ Move(scratch, result_register());
1967 VisitForAccumulatorValue(property->key());
1968 __ Push(scratch, result_register());
1969 if (expr->is_compound()) {
1970 const Register scratch1 = a4;
1971 __ ld(scratch1, MemOperand(sp, 2 * kPointerSize));
1972 __ Push(scratch1, scratch, result_register());
1976 case KEYED_PROPERTY:
1977 // We need the key and receiver on both the stack and in v0 and a1.
1978 if (expr->is_compound()) {
1979 VisitForStackValue(property->obj());
1980 VisitForStackValue(property->key());
1981 __ ld(LoadDescriptor::ReceiverRegister(),
1982 MemOperand(sp, 1 * kPointerSize));
1983 __ ld(LoadDescriptor::NameRegister(), MemOperand(sp, 0));
1985 VisitForStackValue(property->obj());
1986 VisitForStackValue(property->key());
1991 // For compound assignments we need another deoptimization point after the
1992 // variable/property load.
1993 if (expr->is_compound()) {
1994 { AccumulatorValueContext context(this);
1995 switch (assign_type) {
1997 EmitVariableLoad(expr->target()->AsVariableProxy());
1998 PrepareForBailout(expr->target(), TOS_REG);
2000 case NAMED_PROPERTY:
2001 EmitNamedPropertyLoad(property);
2002 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2004 case NAMED_SUPER_PROPERTY:
2005 EmitNamedSuperPropertyLoad(property);
2006 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2008 case KEYED_SUPER_PROPERTY:
2009 EmitKeyedSuperPropertyLoad(property);
2010 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2012 case KEYED_PROPERTY:
2013 EmitKeyedPropertyLoad(property);
2014 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2019 Token::Value op = expr->binary_op();
2020 __ push(v0); // Left operand goes on the stack.
2021 VisitForAccumulatorValue(expr->value());
2023 AccumulatorValueContext context(this);
2024 if (ShouldInlineSmiCase(op)) {
2025 EmitInlineSmiBinaryOp(expr->binary_operation(),
2030 EmitBinaryOp(expr->binary_operation(), op);
2033 // Deoptimization point in case the binary operation may have side effects.
2034 PrepareForBailout(expr->binary_operation(), TOS_REG);
2036 VisitForAccumulatorValue(expr->value());
2039 SetExpressionPosition(expr);
2042 switch (assign_type) {
2044 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
2045 expr->op(), expr->AssignmentSlot());
2046 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2047 context()->Plug(v0);
2049 case NAMED_PROPERTY:
2050 EmitNamedPropertyAssignment(expr);
2052 case NAMED_SUPER_PROPERTY:
2053 EmitNamedSuperPropertyStore(property);
2054 context()->Plug(v0);
2056 case KEYED_SUPER_PROPERTY:
2057 EmitKeyedSuperPropertyStore(property);
2058 context()->Plug(v0);
2060 case KEYED_PROPERTY:
2061 EmitKeyedPropertyAssignment(expr);
2067 void FullCodeGenerator::VisitYield(Yield* expr) {
2068 Comment cmnt(masm_, "[ Yield");
2069 SetExpressionPosition(expr);
2071 // Evaluate yielded value first; the initial iterator definition depends on
2072 // this. It stays on the stack while we update the iterator.
2073 VisitForStackValue(expr->expression());
2075 switch (expr->yield_kind()) {
2076 case Yield::kSuspend:
2077 // Pop value from top-of-stack slot; box result into result register.
2078 EmitCreateIteratorResult(false);
2079 __ push(result_register());
2081 case Yield::kInitial: {
2082 Label suspend, continuation, post_runtime, resume;
2085 __ bind(&continuation);
2086 __ RecordGeneratorContinuation();
2090 VisitForAccumulatorValue(expr->generator_object());
2091 DCHECK(continuation.pos() > 0 && Smi::IsValid(continuation.pos()));
2092 __ li(a1, Operand(Smi::FromInt(continuation.pos())));
2093 __ sd(a1, FieldMemOperand(v0, JSGeneratorObject::kContinuationOffset));
2094 __ sd(cp, FieldMemOperand(v0, JSGeneratorObject::kContextOffset));
2096 __ RecordWriteField(v0, JSGeneratorObject::kContextOffset, a1, a2,
2097 kRAHasBeenSaved, kDontSaveFPRegs);
2098 __ Daddu(a1, fp, Operand(StandardFrameConstants::kExpressionsOffset));
2099 __ Branch(&post_runtime, eq, sp, Operand(a1));
2100 __ push(v0); // generator object
2101 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
2102 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2103 __ bind(&post_runtime);
2104 __ pop(result_register());
2105 EmitReturnSequence();
2108 context()->Plug(result_register());
2112 case Yield::kFinal: {
2113 VisitForAccumulatorValue(expr->generator_object());
2114 __ li(a1, Operand(Smi::FromInt(JSGeneratorObject::kGeneratorClosed)));
2115 __ sd(a1, FieldMemOperand(result_register(),
2116 JSGeneratorObject::kContinuationOffset));
2117 // Pop value from top-of-stack slot, box result into result register.
2118 EmitCreateIteratorResult(true);
2119 EmitUnwindBeforeReturn();
2120 EmitReturnSequence();
2124 case Yield::kDelegating: {
2125 VisitForStackValue(expr->generator_object());
2127 // Initial stack layout is as follows:
2128 // [sp + 1 * kPointerSize] iter
2129 // [sp + 0 * kPointerSize] g
2131 Label l_catch, l_try, l_suspend, l_continuation, l_resume;
2132 Label l_next, l_call;
2133 Register load_receiver = LoadDescriptor::ReceiverRegister();
2134 Register load_name = LoadDescriptor::NameRegister();
2135 // Initial send value is undefined.
2136 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
2139 // catch (e) { receiver = iter; f = 'throw'; arg = e; goto l_call; }
2142 __ LoadRoot(a2, Heap::kthrow_stringRootIndex); // "throw"
2143 __ ld(a3, MemOperand(sp, 1 * kPointerSize)); // iter
2144 __ Push(a2, a3, a0); // "throw", iter, except
2147 // try { received = %yield result }
2148 // Shuffle the received result above a try handler and yield it without
2151 __ pop(a0); // result
2152 int handler_index = NewHandlerTableEntry();
2153 EnterTryBlock(handler_index, &l_catch);
2154 const int try_block_size = TryCatch::kElementCount * kPointerSize;
2155 __ push(a0); // result
2158 __ bind(&l_continuation);
2159 __ RecordGeneratorContinuation();
2163 __ bind(&l_suspend);
2164 const int generator_object_depth = kPointerSize + try_block_size;
2165 __ ld(a0, MemOperand(sp, generator_object_depth));
2167 __ Push(Smi::FromInt(handler_index)); // handler-index
2168 DCHECK(l_continuation.pos() > 0 && Smi::IsValid(l_continuation.pos()));
2169 __ li(a1, Operand(Smi::FromInt(l_continuation.pos())));
2170 __ sd(a1, FieldMemOperand(a0, JSGeneratorObject::kContinuationOffset));
2171 __ sd(cp, FieldMemOperand(a0, JSGeneratorObject::kContextOffset));
2173 __ RecordWriteField(a0, JSGeneratorObject::kContextOffset, a1, a2,
2174 kRAHasBeenSaved, kDontSaveFPRegs);
2175 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 2);
2176 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2177 __ pop(v0); // result
2178 EmitReturnSequence();
2180 __ bind(&l_resume); // received in a0
2181 ExitTryBlock(handler_index);
2183 // receiver = iter; f = 'next'; arg = received;
2185 __ LoadRoot(load_name, Heap::knext_stringRootIndex); // "next"
2186 __ ld(a3, MemOperand(sp, 1 * kPointerSize)); // iter
2187 __ Push(load_name, a3, a0); // "next", iter, received
2189 // result = receiver[f](arg);
2191 __ ld(load_receiver, MemOperand(sp, kPointerSize));
2192 __ ld(load_name, MemOperand(sp, 2 * kPointerSize));
2193 __ li(LoadDescriptor::SlotRegister(),
2194 Operand(SmiFromSlot(expr->KeyedLoadFeedbackSlot())));
2195 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), SLOPPY).code();
2196 CallIC(ic, TypeFeedbackId::None());
2199 __ sd(a1, MemOperand(sp, 2 * kPointerSize));
2200 SetCallPosition(expr, 1);
2201 CallFunctionStub stub(isolate(), 1, CALL_AS_METHOD);
2204 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2205 __ Drop(1); // The function is still on the stack; drop it.
2207 // if (!result.done) goto l_try;
2208 __ Move(load_receiver, v0);
2210 __ push(load_receiver); // save result
2211 __ LoadRoot(load_name, Heap::kdone_stringRootIndex); // "done"
2212 __ li(LoadDescriptor::SlotRegister(),
2213 Operand(SmiFromSlot(expr->DoneFeedbackSlot())));
2214 CallLoadIC(NOT_INSIDE_TYPEOF); // v0=result.done
2216 Handle<Code> bool_ic = ToBooleanStub::GetUninitialized(isolate());
2218 __ Branch(&l_try, eq, v0, Operand(zero_reg));
2221 __ pop(load_receiver); // result
2222 __ LoadRoot(load_name, Heap::kvalue_stringRootIndex); // "value"
2223 __ li(LoadDescriptor::SlotRegister(),
2224 Operand(SmiFromSlot(expr->ValueFeedbackSlot())));
2225 CallLoadIC(NOT_INSIDE_TYPEOF); // v0=result.value
2226 context()->DropAndPlug(2, v0); // drop iter and g
2233 void FullCodeGenerator::EmitGeneratorResume(Expression *generator,
2235 JSGeneratorObject::ResumeMode resume_mode) {
2236 // The value stays in a0, and is ultimately read by the resumed generator, as
2237 // if CallRuntime(Runtime::kSuspendJSGeneratorObject) returned it. Or it
2238 // is read to throw the value when the resumed generator is already closed.
2239 // a1 will hold the generator object until the activation has been resumed.
2240 VisitForStackValue(generator);
2241 VisitForAccumulatorValue(value);
2244 // Load suspended function and context.
2245 __ ld(cp, FieldMemOperand(a1, JSGeneratorObject::kContextOffset));
2246 __ ld(a4, FieldMemOperand(a1, JSGeneratorObject::kFunctionOffset));
2248 // Load receiver and store as the first argument.
2249 __ ld(a2, FieldMemOperand(a1, JSGeneratorObject::kReceiverOffset));
2252 // Push holes for the rest of the arguments to the generator function.
2253 __ ld(a3, FieldMemOperand(a4, JSFunction::kSharedFunctionInfoOffset));
2254 // The argument count is stored as int32_t on 64-bit platforms.
2255 // TODO(plind): Smi on 32-bit platforms.
2257 FieldMemOperand(a3, SharedFunctionInfo::kFormalParameterCountOffset));
2258 __ LoadRoot(a2, Heap::kTheHoleValueRootIndex);
2259 Label push_argument_holes, push_frame;
2260 __ bind(&push_argument_holes);
2261 __ Dsubu(a3, a3, Operand(1));
2262 __ Branch(&push_frame, lt, a3, Operand(zero_reg));
2264 __ jmp(&push_argument_holes);
2266 // Enter a new JavaScript frame, and initialize its slots as they were when
2267 // the generator was suspended.
2268 Label resume_frame, done;
2269 __ bind(&push_frame);
2270 __ Call(&resume_frame);
2272 __ bind(&resume_frame);
2273 // ra = return address.
2274 // fp = caller's frame pointer.
2275 // cp = callee's context,
2276 // a4 = callee's JS function.
2277 __ Push(ra, fp, cp, a4);
2278 // Adjust FP to point to saved FP.
2279 __ Daddu(fp, sp, 2 * kPointerSize);
2281 // Load the operand stack size.
2282 __ ld(a3, FieldMemOperand(a1, JSGeneratorObject::kOperandStackOffset));
2283 __ ld(a3, FieldMemOperand(a3, FixedArray::kLengthOffset));
2286 // If we are sending a value and there is no operand stack, we can jump back
2288 if (resume_mode == JSGeneratorObject::NEXT) {
2290 __ Branch(&slow_resume, ne, a3, Operand(zero_reg));
2291 __ ld(a3, FieldMemOperand(a4, JSFunction::kCodeEntryOffset));
2292 __ ld(a2, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
2294 __ Daddu(a3, a3, Operand(a2));
2295 __ li(a2, Operand(Smi::FromInt(JSGeneratorObject::kGeneratorExecuting)));
2296 __ sd(a2, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
2298 __ bind(&slow_resume);
2301 // Otherwise, we push holes for the operand stack and call the runtime to fix
2302 // up the stack and the handlers.
2303 Label push_operand_holes, call_resume;
2304 __ bind(&push_operand_holes);
2305 __ Dsubu(a3, a3, Operand(1));
2306 __ Branch(&call_resume, lt, a3, Operand(zero_reg));
2308 __ Branch(&push_operand_holes);
2309 __ bind(&call_resume);
2310 DCHECK(!result_register().is(a1));
2311 __ Push(a1, result_register());
2312 __ Push(Smi::FromInt(resume_mode));
2313 __ CallRuntime(Runtime::kResumeJSGeneratorObject, 3);
2314 // Not reached: the runtime call returns elsewhere.
2315 __ stop("not-reached");
2318 context()->Plug(result_register());
2322 void FullCodeGenerator::EmitCreateIteratorResult(bool done) {
2326 const int instance_size = 5 * kPointerSize;
2327 DCHECK_EQ(isolate()->native_context()->iterator_result_map()->instance_size(),
2330 __ Allocate(instance_size, v0, a2, a3, &gc_required, TAG_OBJECT);
2333 __ bind(&gc_required);
2334 __ Push(Smi::FromInt(instance_size));
2335 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
2336 __ ld(context_register(),
2337 MemOperand(fp, StandardFrameConstants::kContextOffset));
2339 __ bind(&allocated);
2340 __ ld(a1, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
2341 __ ld(a1, FieldMemOperand(a1, GlobalObject::kNativeContextOffset));
2342 __ ld(a1, ContextOperand(a1, Context::ITERATOR_RESULT_MAP_INDEX));
2344 __ li(a3, Operand(isolate()->factory()->ToBoolean(done)));
2345 __ li(a4, Operand(isolate()->factory()->empty_fixed_array()));
2346 __ sd(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2347 __ sd(a4, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2348 __ sd(a4, FieldMemOperand(v0, JSObject::kElementsOffset));
2350 FieldMemOperand(v0, JSGeneratorObject::kResultValuePropertyOffset));
2352 FieldMemOperand(v0, JSGeneratorObject::kResultDonePropertyOffset));
2354 // Only the value field needs a write barrier, as the other values are in the
2356 __ RecordWriteField(v0, JSGeneratorObject::kResultValuePropertyOffset,
2357 a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
2361 void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
2362 SetExpressionPosition(prop);
2363 Literal* key = prop->key()->AsLiteral();
2364 DCHECK(!prop->IsSuperAccess());
2366 __ li(LoadDescriptor::NameRegister(), Operand(key->value()));
2367 __ li(LoadDescriptor::SlotRegister(),
2368 Operand(SmiFromSlot(prop->PropertyFeedbackSlot())));
2369 CallLoadIC(NOT_INSIDE_TYPEOF, language_mode());
2373 void FullCodeGenerator::EmitNamedSuperPropertyLoad(Property* prop) {
2374 // Stack: receiver, home_object.
2375 SetExpressionPosition(prop);
2377 Literal* key = prop->key()->AsLiteral();
2378 DCHECK(!key->value()->IsSmi());
2379 DCHECK(prop->IsSuperAccess());
2381 __ Push(key->value());
2382 __ Push(Smi::FromInt(language_mode()));
2383 __ CallRuntime(Runtime::kLoadFromSuper, 4);
2387 void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
2388 // Call keyed load IC. It has register arguments receiver and key.
2389 SetExpressionPosition(prop);
2391 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), language_mode()).code();
2392 __ li(LoadDescriptor::SlotRegister(),
2393 Operand(SmiFromSlot(prop->PropertyFeedbackSlot())));
2398 void FullCodeGenerator::EmitKeyedSuperPropertyLoad(Property* prop) {
2399 // Stack: receiver, home_object, key.
2400 SetExpressionPosition(prop);
2401 __ Push(Smi::FromInt(language_mode()));
2402 __ CallRuntime(Runtime::kLoadKeyedFromSuper, 4);
2406 void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
2408 Expression* left_expr,
2409 Expression* right_expr) {
2410 Label done, smi_case, stub_call;
2412 Register scratch1 = a2;
2413 Register scratch2 = a3;
2415 // Get the arguments.
2417 Register right = a0;
2419 __ mov(a0, result_register());
2421 // Perform combined smi check on both operands.
2422 __ Or(scratch1, left, Operand(right));
2423 STATIC_ASSERT(kSmiTag == 0);
2424 JumpPatchSite patch_site(masm_);
2425 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
2427 __ bind(&stub_call);
2429 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2430 CallIC(code, expr->BinaryOperationFeedbackId());
2431 patch_site.EmitPatchInfo();
2435 // Smi case. This code works the same way as the smi-smi case in the type
2436 // recording binary operation stub, see
2439 __ GetLeastBitsFromSmi(scratch1, right, 5);
2440 __ dsrav(right, left, scratch1);
2441 __ And(v0, right, Operand(0xffffffff00000000L));
2444 __ SmiUntag(scratch1, left);
2445 __ GetLeastBitsFromSmi(scratch2, right, 5);
2446 __ dsllv(scratch1, scratch1, scratch2);
2447 __ SmiTag(v0, scratch1);
2451 __ SmiUntag(scratch1, left);
2452 __ GetLeastBitsFromSmi(scratch2, right, 5);
2453 __ dsrlv(scratch1, scratch1, scratch2);
2454 __ And(scratch2, scratch1, 0x80000000);
2455 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
2456 __ SmiTag(v0, scratch1);
2460 __ DadduAndCheckForOverflow(v0, left, right, scratch1);
2461 __ BranchOnOverflow(&stub_call, scratch1);
2464 __ DsubuAndCheckForOverflow(v0, left, right, scratch1);
2465 __ BranchOnOverflow(&stub_call, scratch1);
2468 __ Dmulh(v0, left, right);
2469 __ dsra32(scratch2, v0, 0);
2470 __ sra(scratch1, v0, 31);
2471 __ Branch(USE_DELAY_SLOT, &stub_call, ne, scratch2, Operand(scratch1));
2473 __ Branch(USE_DELAY_SLOT, &done, ne, v0, Operand(zero_reg));
2474 __ Daddu(scratch2, right, left);
2475 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
2476 DCHECK(Smi::FromInt(0) == 0);
2477 __ mov(v0, zero_reg);
2481 __ Or(v0, left, Operand(right));
2483 case Token::BIT_AND:
2484 __ And(v0, left, Operand(right));
2486 case Token::BIT_XOR:
2487 __ Xor(v0, left, Operand(right));
2494 context()->Plug(v0);
2498 void FullCodeGenerator::EmitClassDefineProperties(ClassLiteral* lit,
2499 int* used_store_slots) {
2500 // Constructor is in v0.
2501 DCHECK(lit != NULL);
2504 // No access check is needed here since the constructor is created by the
2506 Register scratch = a1;
2508 FieldMemOperand(v0, JSFunction::kPrototypeOrInitialMapOffset));
2511 for (int i = 0; i < lit->properties()->length(); i++) {
2512 ObjectLiteral::Property* property = lit->properties()->at(i);
2513 Expression* value = property->value();
2515 if (property->is_static()) {
2516 __ ld(scratch, MemOperand(sp, kPointerSize)); // constructor
2518 __ ld(scratch, MemOperand(sp, 0)); // prototype
2521 EmitPropertyKey(property, lit->GetIdForProperty(i));
2523 // The static prototype property is read only. We handle the non computed
2524 // property name case in the parser. Since this is the only case where we
2525 // need to check for an own read only property we special case this so we do
2526 // not need to do this for every property.
2527 if (property->is_static() && property->is_computed_name()) {
2528 __ CallRuntime(Runtime::kThrowIfStaticPrototype, 1);
2532 VisitForStackValue(value);
2533 EmitSetHomeObjectIfNeeded(value, 2,
2534 lit->SlotForHomeObject(value, used_store_slots));
2536 switch (property->kind()) {
2537 case ObjectLiteral::Property::CONSTANT:
2538 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2539 case ObjectLiteral::Property::PROTOTYPE:
2541 case ObjectLiteral::Property::COMPUTED:
2542 __ CallRuntime(Runtime::kDefineClassMethod, 3);
2545 case ObjectLiteral::Property::GETTER:
2546 __ li(a0, Operand(Smi::FromInt(DONT_ENUM)));
2548 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
2551 case ObjectLiteral::Property::SETTER:
2552 __ li(a0, Operand(Smi::FromInt(DONT_ENUM)));
2554 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
2563 __ CallRuntime(Runtime::kToFastProperties, 1);
2566 __ CallRuntime(Runtime::kToFastProperties, 1);
2568 if (is_strong(language_mode())) {
2570 FieldMemOperand(v0, JSFunction::kPrototypeOrInitialMapOffset));
2571 __ Push(v0, scratch);
2572 // TODO(conradw): It would be more efficient to define the properties with
2573 // the right attributes the first time round.
2574 // Freeze the prototype.
2575 __ CallRuntime(Runtime::kObjectFreeze, 1);
2576 // Freeze the constructor.
2577 __ CallRuntime(Runtime::kObjectFreeze, 1);
2582 void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr, Token::Value op) {
2583 __ mov(a0, result_register());
2586 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2587 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
2588 CallIC(code, expr->BinaryOperationFeedbackId());
2589 patch_site.EmitPatchInfo();
2590 context()->Plug(v0);
2594 void FullCodeGenerator::EmitAssignment(Expression* expr,
2595 FeedbackVectorICSlot slot) {
2596 DCHECK(expr->IsValidReferenceExpressionOrThis());
2598 Property* prop = expr->AsProperty();
2599 LhsKind assign_type = Property::GetAssignType(prop);
2601 switch (assign_type) {
2603 Variable* var = expr->AsVariableProxy()->var();
2604 EffectContext context(this);
2605 EmitVariableAssignment(var, Token::ASSIGN, slot);
2608 case NAMED_PROPERTY: {
2609 __ push(result_register()); // Preserve value.
2610 VisitForAccumulatorValue(prop->obj());
2611 __ mov(StoreDescriptor::ReceiverRegister(), result_register());
2612 __ pop(StoreDescriptor::ValueRegister()); // Restore value.
2613 __ li(StoreDescriptor::NameRegister(),
2614 Operand(prop->key()->AsLiteral()->value()));
2615 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2619 case NAMED_SUPER_PROPERTY: {
2621 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2622 VisitForAccumulatorValue(
2623 prop->obj()->AsSuperPropertyReference()->home_object());
2624 // stack: value, this; v0: home_object
2625 Register scratch = a2;
2626 Register scratch2 = a3;
2627 __ mov(scratch, result_register()); // home_object
2628 __ ld(v0, MemOperand(sp, kPointerSize)); // value
2629 __ ld(scratch2, MemOperand(sp, 0)); // this
2630 __ sd(scratch2, MemOperand(sp, kPointerSize)); // this
2631 __ sd(scratch, MemOperand(sp, 0)); // home_object
2632 // stack: this, home_object; v0: value
2633 EmitNamedSuperPropertyStore(prop);
2636 case KEYED_SUPER_PROPERTY: {
2638 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2640 prop->obj()->AsSuperPropertyReference()->home_object());
2641 VisitForAccumulatorValue(prop->key());
2642 Register scratch = a2;
2643 Register scratch2 = a3;
2644 __ ld(scratch2, MemOperand(sp, 2 * kPointerSize)); // value
2645 // stack: value, this, home_object; v0: key, a3: value
2646 __ ld(scratch, MemOperand(sp, kPointerSize)); // this
2647 __ sd(scratch, MemOperand(sp, 2 * kPointerSize));
2648 __ ld(scratch, MemOperand(sp, 0)); // home_object
2649 __ sd(scratch, MemOperand(sp, kPointerSize));
2650 __ sd(v0, MemOperand(sp, 0));
2651 __ Move(v0, scratch2);
2652 // stack: this, home_object, key; v0: value.
2653 EmitKeyedSuperPropertyStore(prop);
2656 case KEYED_PROPERTY: {
2657 __ push(result_register()); // Preserve value.
2658 VisitForStackValue(prop->obj());
2659 VisitForAccumulatorValue(prop->key());
2660 __ Move(StoreDescriptor::NameRegister(), result_register());
2661 __ Pop(StoreDescriptor::ValueRegister(),
2662 StoreDescriptor::ReceiverRegister());
2663 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2665 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2670 context()->Plug(v0);
2674 void FullCodeGenerator::EmitStoreToStackLocalOrContextSlot(
2675 Variable* var, MemOperand location) {
2676 __ sd(result_register(), location);
2677 if (var->IsContextSlot()) {
2678 // RecordWrite may destroy all its register arguments.
2679 __ Move(a3, result_register());
2680 int offset = Context::SlotOffset(var->index());
2681 __ RecordWriteContextSlot(
2682 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
2687 void FullCodeGenerator::EmitVariableAssignment(Variable* var, Token::Value op,
2688 FeedbackVectorICSlot slot) {
2689 if (var->IsUnallocated()) {
2690 // Global var, const, or let.
2691 __ mov(StoreDescriptor::ValueRegister(), result_register());
2692 __ li(StoreDescriptor::NameRegister(), Operand(var->name()));
2693 __ ld(StoreDescriptor::ReceiverRegister(), GlobalObjectOperand());
2694 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2697 } else if (var->IsGlobalSlot()) {
2698 // Global var, const, or let.
2699 DCHECK(var->index() > 0);
2700 DCHECK(var->IsStaticGlobalObjectProperty());
2701 DCHECK(StoreGlobalViaContextDescriptor::ValueRegister().is(a0));
2702 __ mov(StoreGlobalViaContextDescriptor::ValueRegister(), result_register());
2703 // Each var occupies two slots in the context: for reads and writes.
2704 int const slot = var->index() + 1;
2705 int const depth = scope()->ContextChainLength(var->scope());
2706 if (depth <= StoreGlobalViaContextStub::kMaximumDepth) {
2707 __ li(StoreGlobalViaContextDescriptor::SlotRegister(), Operand(slot));
2708 StoreGlobalViaContextStub stub(isolate(), depth, language_mode());
2711 __ Push(Smi::FromInt(slot));
2713 __ CallRuntime(is_strict(language_mode())
2714 ? Runtime::kStoreGlobalViaContext_Strict
2715 : Runtime::kStoreGlobalViaContext_Sloppy,
2719 } else if (var->mode() == LET && op != Token::INIT_LET) {
2720 // Non-initializing assignment to let variable needs a write barrier.
2721 DCHECK(!var->IsLookupSlot());
2722 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2724 MemOperand location = VarOperand(var, a1);
2725 __ ld(a3, location);
2726 __ LoadRoot(a4, Heap::kTheHoleValueRootIndex);
2727 __ Branch(&assign, ne, a3, Operand(a4));
2728 __ li(a3, Operand(var->name()));
2730 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2731 // Perform the assignment.
2733 EmitStoreToStackLocalOrContextSlot(var, location);
2735 } else if (var->mode() == CONST && op != Token::INIT_CONST) {
2736 // Assignment to const variable needs a write barrier.
2737 DCHECK(!var->IsLookupSlot());
2738 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2740 MemOperand location = VarOperand(var, a1);
2741 __ ld(a3, location);
2742 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2743 __ Branch(&const_error, ne, a3, Operand(at));
2744 __ li(a3, Operand(var->name()));
2746 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2747 __ bind(&const_error);
2748 __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2750 } else if (var->is_this() && op == Token::INIT_CONST) {
2751 // Initializing assignment to const {this} needs a write barrier.
2752 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2753 Label uninitialized_this;
2754 MemOperand location = VarOperand(var, a1);
2755 __ ld(a3, location);
2756 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2757 __ Branch(&uninitialized_this, eq, a3, Operand(at));
2758 __ li(a0, Operand(var->name()));
2760 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2761 __ bind(&uninitialized_this);
2762 EmitStoreToStackLocalOrContextSlot(var, location);
2764 } else if (!var->is_const_mode() || op == Token::INIT_CONST) {
2765 if (var->IsLookupSlot()) {
2766 // Assignment to var.
2767 __ li(a4, Operand(var->name()));
2768 __ li(a3, Operand(Smi::FromInt(language_mode())));
2769 // jssp[0] : language mode.
2771 // jssp[16] : context.
2772 // jssp[24] : value.
2773 __ Push(v0, cp, a4, a3);
2774 __ CallRuntime(Runtime::kStoreLookupSlot, 4);
2776 // Assignment to var or initializing assignment to let/const in harmony
2778 DCHECK((var->IsStackAllocated() || var->IsContextSlot()));
2779 MemOperand location = VarOperand(var, a1);
2780 if (generate_debug_code_ && op == Token::INIT_LET) {
2781 // Check for an uninitialized let binding.
2782 __ ld(a2, location);
2783 __ LoadRoot(a4, Heap::kTheHoleValueRootIndex);
2784 __ Check(eq, kLetBindingReInitialization, a2, Operand(a4));
2786 EmitStoreToStackLocalOrContextSlot(var, location);
2789 } else if (op == Token::INIT_CONST_LEGACY) {
2790 // Const initializers need a write barrier.
2791 DCHECK(!var->IsParameter()); // No const parameters.
2792 if (var->IsLookupSlot()) {
2793 __ li(a0, Operand(var->name()));
2794 __ Push(v0, cp, a0); // Context and name.
2795 __ CallRuntime(Runtime::kInitializeLegacyConstLookupSlot, 3);
2797 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2799 MemOperand location = VarOperand(var, a1);
2800 __ ld(a2, location);
2801 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2802 __ Branch(&skip, ne, a2, Operand(at));
2803 EmitStoreToStackLocalOrContextSlot(var, location);
2808 DCHECK(var->mode() == CONST_LEGACY && op != Token::INIT_CONST_LEGACY);
2809 if (is_strict(language_mode())) {
2810 __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2812 // Silently ignore store in sloppy mode.
2817 void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2818 // Assignment to a property, using a named store IC.
2819 Property* prop = expr->target()->AsProperty();
2820 DCHECK(prop != NULL);
2821 DCHECK(prop->key()->IsLiteral());
2823 __ mov(StoreDescriptor::ValueRegister(), result_register());
2824 __ li(StoreDescriptor::NameRegister(),
2825 Operand(prop->key()->AsLiteral()->value()));
2826 __ pop(StoreDescriptor::ReceiverRegister());
2827 if (FLAG_vector_stores) {
2828 EmitLoadStoreICSlot(expr->AssignmentSlot());
2831 CallStoreIC(expr->AssignmentFeedbackId());
2834 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2835 context()->Plug(v0);
2839 void FullCodeGenerator::EmitNamedSuperPropertyStore(Property* prop) {
2840 // Assignment to named property of super.
2842 // stack : receiver ('this'), home_object
2843 DCHECK(prop != NULL);
2844 Literal* key = prop->key()->AsLiteral();
2845 DCHECK(key != NULL);
2847 __ Push(key->value());
2849 __ CallRuntime((is_strict(language_mode()) ? Runtime::kStoreToSuper_Strict
2850 : Runtime::kStoreToSuper_Sloppy),
2855 void FullCodeGenerator::EmitKeyedSuperPropertyStore(Property* prop) {
2856 // Assignment to named property of super.
2858 // stack : receiver ('this'), home_object, key
2859 DCHECK(prop != NULL);
2863 (is_strict(language_mode()) ? Runtime::kStoreKeyedToSuper_Strict
2864 : Runtime::kStoreKeyedToSuper_Sloppy),
2869 void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2870 // Assignment to a property, using a keyed store IC.
2871 // Call keyed store IC.
2872 // The arguments are:
2873 // - a0 is the value,
2875 // - a2 is the receiver.
2876 __ mov(StoreDescriptor::ValueRegister(), result_register());
2877 __ Pop(StoreDescriptor::ReceiverRegister(), StoreDescriptor::NameRegister());
2878 DCHECK(StoreDescriptor::ValueRegister().is(a0));
2881 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2882 if (FLAG_vector_stores) {
2883 EmitLoadStoreICSlot(expr->AssignmentSlot());
2886 CallIC(ic, expr->AssignmentFeedbackId());
2889 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2890 context()->Plug(v0);
2894 void FullCodeGenerator::VisitProperty(Property* expr) {
2895 Comment cmnt(masm_, "[ Property");
2896 SetExpressionPosition(expr);
2898 Expression* key = expr->key();
2900 if (key->IsPropertyName()) {
2901 if (!expr->IsSuperAccess()) {
2902 VisitForAccumulatorValue(expr->obj());
2903 __ Move(LoadDescriptor::ReceiverRegister(), v0);
2904 EmitNamedPropertyLoad(expr);
2906 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2908 expr->obj()->AsSuperPropertyReference()->home_object());
2909 EmitNamedSuperPropertyLoad(expr);
2912 if (!expr->IsSuperAccess()) {
2913 VisitForStackValue(expr->obj());
2914 VisitForAccumulatorValue(expr->key());
2915 __ Move(LoadDescriptor::NameRegister(), v0);
2916 __ pop(LoadDescriptor::ReceiverRegister());
2917 EmitKeyedPropertyLoad(expr);
2919 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2921 expr->obj()->AsSuperPropertyReference()->home_object());
2922 VisitForStackValue(expr->key());
2923 EmitKeyedSuperPropertyLoad(expr);
2926 PrepareForBailoutForId(expr->LoadId(), TOS_REG);
2927 context()->Plug(v0);
2931 void FullCodeGenerator::CallIC(Handle<Code> code,
2932 TypeFeedbackId id) {
2934 __ Call(code, RelocInfo::CODE_TARGET, id);
2938 // Code common for calls using the IC.
2939 void FullCodeGenerator::EmitCallWithLoadIC(Call* expr) {
2940 Expression* callee = expr->expression();
2942 CallICState::CallType call_type =
2943 callee->IsVariableProxy() ? CallICState::FUNCTION : CallICState::METHOD;
2945 // Get the target function.
2946 if (call_type == CallICState::FUNCTION) {
2947 { StackValueContext context(this);
2948 EmitVariableLoad(callee->AsVariableProxy());
2949 PrepareForBailout(callee, NO_REGISTERS);
2951 // Push undefined as receiver. This is patched in the method prologue if it
2952 // is a sloppy mode method.
2953 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
2956 // Load the function from the receiver.
2957 DCHECK(callee->IsProperty());
2958 DCHECK(!callee->AsProperty()->IsSuperAccess());
2959 __ ld(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
2960 EmitNamedPropertyLoad(callee->AsProperty());
2961 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2962 // Push the target function under the receiver.
2963 __ ld(at, MemOperand(sp, 0));
2965 __ sd(v0, MemOperand(sp, kPointerSize));
2968 EmitCall(expr, call_type);
2972 void FullCodeGenerator::EmitSuperCallWithLoadIC(Call* expr) {
2973 SetExpressionPosition(expr);
2974 Expression* callee = expr->expression();
2975 DCHECK(callee->IsProperty());
2976 Property* prop = callee->AsProperty();
2977 DCHECK(prop->IsSuperAccess());
2979 Literal* key = prop->key()->AsLiteral();
2980 DCHECK(!key->value()->IsSmi());
2981 // Load the function from the receiver.
2982 const Register scratch = a1;
2983 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
2984 VisitForAccumulatorValue(super_ref->home_object());
2985 __ mov(scratch, v0);
2986 VisitForAccumulatorValue(super_ref->this_var());
2987 __ Push(scratch, v0, v0, scratch);
2988 __ Push(key->value());
2989 __ Push(Smi::FromInt(language_mode()));
2993 // - this (receiver)
2994 // - this (receiver) <-- LoadFromSuper will pop here and below.
2998 __ CallRuntime(Runtime::kLoadFromSuper, 4);
3000 // Replace home_object with target function.
3001 __ sd(v0, MemOperand(sp, kPointerSize));
3004 // - target function
3005 // - this (receiver)
3006 EmitCall(expr, CallICState::METHOD);
3010 // Code common for calls using the IC.
3011 void FullCodeGenerator::EmitKeyedCallWithLoadIC(Call* expr,
3014 VisitForAccumulatorValue(key);
3016 Expression* callee = expr->expression();
3018 // Load the function from the receiver.
3019 DCHECK(callee->IsProperty());
3020 __ ld(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
3021 __ Move(LoadDescriptor::NameRegister(), v0);
3022 EmitKeyedPropertyLoad(callee->AsProperty());
3023 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
3025 // Push the target function under the receiver.
3026 __ ld(at, MemOperand(sp, 0));
3028 __ sd(v0, MemOperand(sp, kPointerSize));
3030 EmitCall(expr, CallICState::METHOD);
3034 void FullCodeGenerator::EmitKeyedSuperCallWithLoadIC(Call* expr) {
3035 Expression* callee = expr->expression();
3036 DCHECK(callee->IsProperty());
3037 Property* prop = callee->AsProperty();
3038 DCHECK(prop->IsSuperAccess());
3040 SetExpressionPosition(prop);
3041 // Load the function from the receiver.
3042 const Register scratch = a1;
3043 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
3044 VisitForAccumulatorValue(super_ref->home_object());
3045 __ Move(scratch, v0);
3046 VisitForAccumulatorValue(super_ref->this_var());
3047 __ Push(scratch, v0, v0, scratch);
3048 VisitForStackValue(prop->key());
3049 __ Push(Smi::FromInt(language_mode()));
3053 // - this (receiver)
3054 // - this (receiver) <-- LoadKeyedFromSuper will pop here and below.
3058 __ CallRuntime(Runtime::kLoadKeyedFromSuper, 4);
3060 // Replace home_object with target function.
3061 __ sd(v0, MemOperand(sp, kPointerSize));
3064 // - target function
3065 // - this (receiver)
3066 EmitCall(expr, CallICState::METHOD);
3070 void FullCodeGenerator::EmitCall(Call* expr, CallICState::CallType call_type) {
3071 // Load the arguments.
3072 ZoneList<Expression*>* args = expr->arguments();
3073 int arg_count = args->length();
3074 for (int i = 0; i < arg_count; i++) {
3075 VisitForStackValue(args->at(i));
3078 // Record source position of the IC call.
3079 SetCallPosition(expr, arg_count);
3080 Handle<Code> ic = CodeFactory::CallIC(isolate(), arg_count, call_type).code();
3081 __ li(a3, Operand(SmiFromSlot(expr->CallFeedbackICSlot())));
3082 __ ld(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
3083 // Don't assign a type feedback id to the IC, since type feedback is provided
3084 // by the vector above.
3086 RecordJSReturnSite(expr);
3087 // Restore context register.
3088 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3089 context()->DropAndPlug(1, v0);
3093 void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
3094 // a6: copy of the first argument or undefined if it doesn't exist.
3095 if (arg_count > 0) {
3096 __ ld(a6, MemOperand(sp, arg_count * kPointerSize));
3098 __ LoadRoot(a6, Heap::kUndefinedValueRootIndex);
3101 // a5: the receiver of the enclosing function.
3102 __ ld(a5, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3104 // a4: the language mode.
3105 __ li(a4, Operand(Smi::FromInt(language_mode())));
3107 // a1: the start position of the scope the calls resides in.
3108 __ li(a1, Operand(Smi::FromInt(scope()->start_position())));
3110 // Do the runtime call.
3111 __ Push(a6, a5, a4, a1);
3112 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
3116 // See http://www.ecma-international.org/ecma-262/6.0/#sec-function-calls.
3117 void FullCodeGenerator::PushCalleeAndWithBaseObject(Call* expr) {
3118 VariableProxy* callee = expr->expression()->AsVariableProxy();
3119 if (callee->var()->IsLookupSlot()) {
3122 SetExpressionPosition(callee);
3123 // Generate code for loading from variables potentially shadowed by
3124 // eval-introduced variables.
3125 EmitDynamicLookupFastCase(callee, NOT_INSIDE_TYPEOF, &slow, &done);
3128 // Call the runtime to find the function to call (returned in v0)
3129 // and the object holding it (returned in v1).
3130 DCHECK(!context_register().is(a2));
3131 __ li(a2, Operand(callee->name()));
3132 __ Push(context_register(), a2);
3133 __ CallRuntime(Runtime::kLoadLookupSlot, 2);
3134 __ Push(v0, v1); // Function, receiver.
3135 PrepareForBailoutForId(expr->LookupId(), NO_REGISTERS);
3137 // If fast case code has been generated, emit code to push the
3138 // function and receiver and have the slow path jump around this
3140 if (done.is_linked()) {
3146 // The receiver is implicitly the global receiver. Indicate this
3147 // by passing the hole to the call function stub.
3148 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
3153 VisitForStackValue(callee);
3154 // refEnv.WithBaseObject()
3155 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
3156 __ push(a2); // Reserved receiver slot.
3161 void FullCodeGenerator::VisitCall(Call* expr) {
3163 // We want to verify that RecordJSReturnSite gets called on all paths
3164 // through this function. Avoid early returns.
3165 expr->return_is_recorded_ = false;
3168 Comment cmnt(masm_, "[ Call");
3169 Expression* callee = expr->expression();
3170 Call::CallType call_type = expr->GetCallType(isolate());
3172 if (call_type == Call::POSSIBLY_EVAL_CALL) {
3173 // In a call to eval, we first call RuntimeHidden_ResolvePossiblyDirectEval
3174 // to resolve the function we need to call. Then we call the resolved
3175 // function using the given arguments.
3176 ZoneList<Expression*>* args = expr->arguments();
3177 int arg_count = args->length();
3178 PushCalleeAndWithBaseObject(expr);
3180 // Push the arguments.
3181 for (int i = 0; i < arg_count; i++) {
3182 VisitForStackValue(args->at(i));
3185 // Push a copy of the function (found below the arguments) and
3187 __ ld(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
3189 EmitResolvePossiblyDirectEval(arg_count);
3191 // Touch up the stack with the resolved function.
3192 __ sd(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
3194 PrepareForBailoutForId(expr->EvalId(), NO_REGISTERS);
3195 // Record source position for debugger.
3196 SetCallPosition(expr, arg_count);
3197 CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
3198 __ ld(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
3200 RecordJSReturnSite(expr);
3201 // Restore context register.
3202 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3203 context()->DropAndPlug(1, v0);
3204 } else if (call_type == Call::GLOBAL_CALL) {
3205 EmitCallWithLoadIC(expr);
3206 } else if (call_type == Call::LOOKUP_SLOT_CALL) {
3207 // Call to a lookup slot (dynamically introduced variable).
3208 PushCalleeAndWithBaseObject(expr);
3210 } else if (call_type == Call::PROPERTY_CALL) {
3211 Property* property = callee->AsProperty();
3212 bool is_named_call = property->key()->IsPropertyName();
3213 if (property->IsSuperAccess()) {
3214 if (is_named_call) {
3215 EmitSuperCallWithLoadIC(expr);
3217 EmitKeyedSuperCallWithLoadIC(expr);
3220 VisitForStackValue(property->obj());
3221 if (is_named_call) {
3222 EmitCallWithLoadIC(expr);
3224 EmitKeyedCallWithLoadIC(expr, property->key());
3227 } else if (call_type == Call::SUPER_CALL) {
3228 EmitSuperConstructorCall(expr);
3230 DCHECK(call_type == Call::OTHER_CALL);
3231 // Call to an arbitrary expression not handled specially above.
3232 VisitForStackValue(callee);
3233 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
3235 // Emit function call.
3240 // RecordJSReturnSite should have been called.
3241 DCHECK(expr->return_is_recorded_);
3246 void FullCodeGenerator::VisitCallNew(CallNew* expr) {
3247 Comment cmnt(masm_, "[ CallNew");
3248 // According to ECMA-262, section 11.2.2, page 44, the function
3249 // expression in new calls must be evaluated before the
3252 // Push constructor on the stack. If it's not a function it's used as
3253 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
3255 DCHECK(!expr->expression()->IsSuperPropertyReference());
3256 VisitForStackValue(expr->expression());
3258 // Push the arguments ("left-to-right") on the stack.
3259 ZoneList<Expression*>* args = expr->arguments();
3260 int arg_count = args->length();
3261 for (int i = 0; i < arg_count; i++) {
3262 VisitForStackValue(args->at(i));
3265 // Call the construct call builtin that handles allocation and
3266 // constructor invocation.
3267 SetConstructCallPosition(expr);
3269 // Load function and argument count into a1 and a0.
3270 __ li(a0, Operand(arg_count));
3271 __ ld(a1, MemOperand(sp, arg_count * kPointerSize));
3273 // Record call targets in unoptimized code.
3274 if (FLAG_pretenuring_call_new) {
3275 EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3276 DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3277 expr->CallNewFeedbackSlot().ToInt() + 1);
3280 __ li(a2, FeedbackVector());
3281 __ li(a3, Operand(SmiFromSlot(expr->CallNewFeedbackSlot())));
3283 CallConstructStub stub(isolate(), RECORD_CONSTRUCTOR_TARGET);
3284 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3285 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
3286 context()->Plug(v0);
3290 void FullCodeGenerator::EmitSuperConstructorCall(Call* expr) {
3291 SuperCallReference* super_call_ref =
3292 expr->expression()->AsSuperCallReference();
3293 DCHECK_NOT_NULL(super_call_ref);
3295 EmitLoadSuperConstructor(super_call_ref);
3296 __ push(result_register());
3298 // Push the arguments ("left-to-right") on the stack.
3299 ZoneList<Expression*>* args = expr->arguments();
3300 int arg_count = args->length();
3301 for (int i = 0; i < arg_count; i++) {
3302 VisitForStackValue(args->at(i));
3305 // Call the construct call builtin that handles allocation and
3306 // constructor invocation.
3307 SetConstructCallPosition(expr);
3309 // Load original constructor into a4.
3310 VisitForAccumulatorValue(super_call_ref->new_target_var());
3311 __ mov(a4, result_register());
3313 // Load function and argument count into a1 and a0.
3314 __ li(a0, Operand(arg_count));
3315 __ ld(a1, MemOperand(sp, arg_count * kPointerSize));
3317 // Record call targets in unoptimized code.
3318 if (FLAG_pretenuring_call_new) {
3320 /* TODO(dslomov): support pretenuring.
3321 EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3322 DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3323 expr->CallNewFeedbackSlot().ToInt() + 1);
3327 __ li(a2, FeedbackVector());
3328 __ li(a3, Operand(SmiFromSlot(expr->CallFeedbackSlot())));
3330 CallConstructStub stub(isolate(), SUPER_CALL_RECORD_TARGET);
3331 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3333 RecordJSReturnSite(expr);
3335 context()->Plug(v0);
3339 void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
3340 ZoneList<Expression*>* args = expr->arguments();
3341 DCHECK(args->length() == 1);
3343 VisitForAccumulatorValue(args->at(0));
3345 Label materialize_true, materialize_false;
3346 Label* if_true = NULL;
3347 Label* if_false = NULL;
3348 Label* fall_through = NULL;
3349 context()->PrepareTest(&materialize_true, &materialize_false,
3350 &if_true, &if_false, &fall_through);
3352 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3354 Split(eq, a4, Operand(zero_reg), if_true, if_false, fall_through);
3356 context()->Plug(if_true, if_false);
3360 void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
3361 ZoneList<Expression*>* args = expr->arguments();
3362 DCHECK(args->length() == 1);
3364 VisitForAccumulatorValue(args->at(0));
3366 Label materialize_true, materialize_false;
3367 Label* if_true = NULL;
3368 Label* if_false = NULL;
3369 Label* fall_through = NULL;
3370 context()->PrepareTest(&materialize_true, &materialize_false,
3371 &if_true, &if_false, &fall_through);
3373 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3374 __ NonNegativeSmiTst(v0, at);
3375 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
3377 context()->Plug(if_true, if_false);
3381 void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
3382 ZoneList<Expression*>* args = expr->arguments();
3383 DCHECK(args->length() == 1);
3385 VisitForAccumulatorValue(args->at(0));
3387 Label materialize_true, materialize_false;
3388 Label* if_true = NULL;
3389 Label* if_false = NULL;
3390 Label* fall_through = NULL;
3391 context()->PrepareTest(&materialize_true, &materialize_false,
3392 &if_true, &if_false, &fall_through);
3394 __ JumpIfSmi(v0, if_false);
3395 __ LoadRoot(at, Heap::kNullValueRootIndex);
3396 __ Branch(if_true, eq, v0, Operand(at));
3397 __ ld(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
3398 // Undetectable objects behave like undefined when tested with typeof.
3399 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
3400 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
3401 __ Branch(if_false, ne, at, Operand(zero_reg));
3402 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
3403 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
3404 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3405 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
3406 if_true, if_false, fall_through);
3408 context()->Plug(if_true, if_false);
3412 void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
3413 ZoneList<Expression*>* args = expr->arguments();
3414 DCHECK(args->length() == 1);
3416 VisitForAccumulatorValue(args->at(0));
3418 Label materialize_true, materialize_false;
3419 Label* if_true = NULL;
3420 Label* if_false = NULL;
3421 Label* fall_through = NULL;
3422 context()->PrepareTest(&materialize_true, &materialize_false,
3423 &if_true, &if_false, &fall_through);
3425 __ JumpIfSmi(v0, if_false);
3426 __ GetObjectType(v0, a1, a1);
3427 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3428 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
3429 if_true, if_false, fall_through);
3431 context()->Plug(if_true, if_false);
3435 void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
3436 ZoneList<Expression*>* args = expr->arguments();
3437 DCHECK(args->length() == 1);
3439 VisitForAccumulatorValue(args->at(0));
3441 Label materialize_true, materialize_false;
3442 Label* if_true = NULL;
3443 Label* if_false = NULL;
3444 Label* fall_through = NULL;
3445 context()->PrepareTest(&materialize_true, &materialize_false,
3446 &if_true, &if_false, &fall_through);
3448 __ JumpIfSmi(v0, if_false);
3449 __ ld(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
3450 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
3451 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3452 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
3453 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
3455 context()->Plug(if_true, if_false);
3459 void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
3460 CallRuntime* expr) {
3461 ZoneList<Expression*>* args = expr->arguments();
3462 DCHECK(args->length() == 1);
3464 VisitForAccumulatorValue(args->at(0));
3466 Label materialize_true, materialize_false, skip_lookup;
3467 Label* if_true = NULL;
3468 Label* if_false = NULL;
3469 Label* fall_through = NULL;
3470 context()->PrepareTest(&materialize_true, &materialize_false,
3471 &if_true, &if_false, &fall_through);
3473 __ AssertNotSmi(v0);
3475 __ ld(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
3476 __ lbu(a4, FieldMemOperand(a1, Map::kBitField2Offset));
3477 __ And(a4, a4, 1 << Map::kStringWrapperSafeForDefaultValueOf);
3478 __ Branch(&skip_lookup, ne, a4, Operand(zero_reg));
3480 // Check for fast case object. Generate false result for slow case object.
3481 __ ld(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
3482 __ ld(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
3483 __ LoadRoot(a4, Heap::kHashTableMapRootIndex);
3484 __ Branch(if_false, eq, a2, Operand(a4));
3486 // Look for valueOf name in the descriptor array, and indicate false if
3487 // found. Since we omit an enumeration index check, if it is added via a
3488 // transition that shares its descriptor array, this is a false positive.
3489 Label entry, loop, done;
3491 // Skip loop if no descriptors are valid.
3492 __ NumberOfOwnDescriptors(a3, a1);
3493 __ Branch(&done, eq, a3, Operand(zero_reg));
3495 __ LoadInstanceDescriptors(a1, a4);
3496 // a4: descriptor array.
3497 // a3: valid entries in the descriptor array.
3498 STATIC_ASSERT(kSmiTag == 0);
3499 STATIC_ASSERT(kSmiTagSize == 1);
3501 // STATIC_ASSERT(kPointerSize == 4);
3502 __ li(at, Operand(DescriptorArray::kDescriptorSize));
3503 __ Dmul(a3, a3, at);
3504 // Calculate location of the first key name.
3505 __ Daddu(a4, a4, Operand(DescriptorArray::kFirstOffset - kHeapObjectTag));
3506 // Calculate the end of the descriptor array.
3508 __ dsll(a5, a3, kPointerSizeLog2);
3509 __ Daddu(a2, a2, a5);
3511 // Loop through all the keys in the descriptor array. If one of these is the
3512 // string "valueOf" the result is false.
3513 // The use of a6 to store the valueOf string assumes that it is not otherwise
3514 // used in the loop below.
3515 __ li(a6, Operand(isolate()->factory()->value_of_string()));
3518 __ ld(a3, MemOperand(a4, 0));
3519 __ Branch(if_false, eq, a3, Operand(a6));
3520 __ Daddu(a4, a4, Operand(DescriptorArray::kDescriptorSize * kPointerSize));
3522 __ Branch(&loop, ne, a4, Operand(a2));
3526 // Set the bit in the map to indicate that there is no local valueOf field.
3527 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
3528 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
3529 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
3531 __ bind(&skip_lookup);
3533 // If a valueOf property is not found on the object check that its
3534 // prototype is the un-modified String prototype. If not result is false.
3535 __ ld(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
3536 __ JumpIfSmi(a2, if_false);
3537 __ ld(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
3538 __ ld(a3, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
3539 __ ld(a3, FieldMemOperand(a3, GlobalObject::kNativeContextOffset));
3540 __ ld(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
3541 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3542 Split(eq, a2, Operand(a3), if_true, if_false, fall_through);
3544 context()->Plug(if_true, if_false);
3548 void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
3549 ZoneList<Expression*>* args = expr->arguments();
3550 DCHECK(args->length() == 1);
3552 VisitForAccumulatorValue(args->at(0));
3554 Label materialize_true, materialize_false;
3555 Label* if_true = NULL;
3556 Label* if_false = NULL;
3557 Label* fall_through = NULL;
3558 context()->PrepareTest(&materialize_true, &materialize_false,
3559 &if_true, &if_false, &fall_through);
3561 __ JumpIfSmi(v0, if_false);
3562 __ GetObjectType(v0, a1, a2);
3563 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3564 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
3565 __ Branch(if_false);
3567 context()->Plug(if_true, if_false);
3571 void FullCodeGenerator::EmitIsMinusZero(CallRuntime* expr) {
3572 ZoneList<Expression*>* args = expr->arguments();
3573 DCHECK(args->length() == 1);
3575 VisitForAccumulatorValue(args->at(0));
3577 Label materialize_true, materialize_false;
3578 Label* if_true = NULL;
3579 Label* if_false = NULL;
3580 Label* fall_through = NULL;
3581 context()->PrepareTest(&materialize_true, &materialize_false,
3582 &if_true, &if_false, &fall_through);
3584 __ CheckMap(v0, a1, Heap::kHeapNumberMapRootIndex, if_false, DO_SMI_CHECK);
3585 __ lwu(a2, FieldMemOperand(v0, HeapNumber::kExponentOffset));
3586 __ lwu(a1, FieldMemOperand(v0, HeapNumber::kMantissaOffset));
3587 __ li(a4, 0x80000000);
3589 __ Branch(¬_nan, ne, a2, Operand(a4));
3590 __ mov(a4, zero_reg);
3594 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3595 Split(eq, a2, Operand(a4), if_true, if_false, fall_through);
3597 context()->Plug(if_true, if_false);
3601 void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
3602 ZoneList<Expression*>* args = expr->arguments();
3603 DCHECK(args->length() == 1);
3605 VisitForAccumulatorValue(args->at(0));
3607 Label materialize_true, materialize_false;
3608 Label* if_true = NULL;
3609 Label* if_false = NULL;
3610 Label* fall_through = NULL;
3611 context()->PrepareTest(&materialize_true, &materialize_false,
3612 &if_true, &if_false, &fall_through);
3614 __ JumpIfSmi(v0, if_false);
3615 __ GetObjectType(v0, a1, a1);
3616 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3617 Split(eq, a1, Operand(JS_ARRAY_TYPE),
3618 if_true, if_false, fall_through);
3620 context()->Plug(if_true, if_false);
3624 void FullCodeGenerator::EmitIsTypedArray(CallRuntime* expr) {
3625 ZoneList<Expression*>* args = expr->arguments();
3626 DCHECK(args->length() == 1);
3628 VisitForAccumulatorValue(args->at(0));
3630 Label materialize_true, materialize_false;
3631 Label* if_true = NULL;
3632 Label* if_false = NULL;
3633 Label* fall_through = NULL;
3634 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3635 &if_false, &fall_through);
3637 __ JumpIfSmi(v0, if_false);
3638 __ GetObjectType(v0, a1, a1);
3639 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3640 Split(eq, a1, Operand(JS_TYPED_ARRAY_TYPE), if_true, if_false, fall_through);
3642 context()->Plug(if_true, if_false);
3646 void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
3647 ZoneList<Expression*>* args = expr->arguments();
3648 DCHECK(args->length() == 1);
3650 VisitForAccumulatorValue(args->at(0));
3652 Label materialize_true, materialize_false;
3653 Label* if_true = NULL;
3654 Label* if_false = NULL;
3655 Label* fall_through = NULL;
3656 context()->PrepareTest(&materialize_true, &materialize_false,
3657 &if_true, &if_false, &fall_through);
3659 __ JumpIfSmi(v0, if_false);
3660 __ GetObjectType(v0, a1, a1);
3661 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3662 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
3664 context()->Plug(if_true, if_false);
3668 void FullCodeGenerator::EmitIsJSProxy(CallRuntime* expr) {
3669 ZoneList<Expression*>* args = expr->arguments();
3670 DCHECK(args->length() == 1);
3672 VisitForAccumulatorValue(args->at(0));
3674 Label materialize_true, materialize_false;
3675 Label* if_true = NULL;
3676 Label* if_false = NULL;
3677 Label* fall_through = NULL;
3678 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3679 &if_false, &fall_through);
3681 __ JumpIfSmi(v0, if_false);
3683 Register type_reg = a2;
3684 __ GetObjectType(v0, map, type_reg);
3685 __ Subu(type_reg, type_reg, Operand(FIRST_JS_PROXY_TYPE));
3686 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3687 Split(ls, type_reg, Operand(LAST_JS_PROXY_TYPE - FIRST_JS_PROXY_TYPE),
3688 if_true, if_false, fall_through);
3690 context()->Plug(if_true, if_false);
3694 void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
3695 DCHECK(expr->arguments()->length() == 0);
3697 Label materialize_true, materialize_false;
3698 Label* if_true = NULL;
3699 Label* if_false = NULL;
3700 Label* fall_through = NULL;
3701 context()->PrepareTest(&materialize_true, &materialize_false,
3702 &if_true, &if_false, &fall_through);
3704 // Get the frame pointer for the calling frame.
3705 __ ld(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3707 // Skip the arguments adaptor frame if it exists.
3708 Label check_frame_marker;
3709 __ ld(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
3710 __ Branch(&check_frame_marker, ne,
3711 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3712 __ ld(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
3714 // Check the marker in the calling frame.
3715 __ bind(&check_frame_marker);
3716 __ ld(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
3717 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3718 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
3719 if_true, if_false, fall_through);
3721 context()->Plug(if_true, if_false);
3725 void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
3726 ZoneList<Expression*>* args = expr->arguments();
3727 DCHECK(args->length() == 2);
3729 // Load the two objects into registers and perform the comparison.
3730 VisitForStackValue(args->at(0));
3731 VisitForAccumulatorValue(args->at(1));
3733 Label materialize_true, materialize_false;
3734 Label* if_true = NULL;
3735 Label* if_false = NULL;
3736 Label* fall_through = NULL;
3737 context()->PrepareTest(&materialize_true, &materialize_false,
3738 &if_true, &if_false, &fall_through);
3741 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3742 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
3744 context()->Plug(if_true, if_false);
3748 void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
3749 ZoneList<Expression*>* args = expr->arguments();
3750 DCHECK(args->length() == 1);
3752 // ArgumentsAccessStub expects the key in a1 and the formal
3753 // parameter count in a0.
3754 VisitForAccumulatorValue(args->at(0));
3756 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
3757 ArgumentsAccessStub stub(isolate(), ArgumentsAccessStub::READ_ELEMENT);
3759 context()->Plug(v0);
3763 void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
3764 DCHECK(expr->arguments()->length() == 0);
3766 // Get the number of formal parameters.
3767 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
3769 // Check if the calling frame is an arguments adaptor frame.
3770 __ ld(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3771 __ ld(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
3772 __ Branch(&exit, ne, a3,
3773 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3775 // Arguments adaptor case: Read the arguments length from the
3777 __ ld(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3780 context()->Plug(v0);
3784 void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
3785 ZoneList<Expression*>* args = expr->arguments();
3786 DCHECK(args->length() == 1);
3787 Label done, null, function, non_function_constructor;
3789 VisitForAccumulatorValue(args->at(0));
3791 // If the object is a smi, we return null.
3792 __ JumpIfSmi(v0, &null);
3794 // Check that the object is a JS object but take special care of JS
3795 // functions to make sure they have 'Function' as their class.
3796 // Assume that there are only two callable types, and one of them is at
3797 // either end of the type range for JS object types. Saves extra comparisons.
3798 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
3799 __ GetObjectType(v0, v0, a1); // Map is now in v0.
3800 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
3802 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3803 FIRST_SPEC_OBJECT_TYPE + 1);
3804 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
3806 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3807 LAST_SPEC_OBJECT_TYPE - 1);
3808 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
3809 // Assume that there is no larger type.
3810 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
3812 // Check if the constructor in the map is a JS function.
3813 Register instance_type = a2;
3814 __ GetMapConstructor(v0, v0, a1, instance_type);
3815 __ Branch(&non_function_constructor, ne, instance_type,
3816 Operand(JS_FUNCTION_TYPE));
3818 // v0 now contains the constructor function. Grab the
3819 // instance class name from there.
3820 __ ld(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
3821 __ ld(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
3824 // Functions have class 'Function'.
3826 __ LoadRoot(v0, Heap::kFunction_stringRootIndex);
3829 // Objects with a non-function constructor have class 'Object'.
3830 __ bind(&non_function_constructor);
3831 __ LoadRoot(v0, Heap::kObject_stringRootIndex);
3834 // Non-JS objects have class null.
3836 __ LoadRoot(v0, Heap::kNullValueRootIndex);
3841 context()->Plug(v0);
3845 void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3846 ZoneList<Expression*>* args = expr->arguments();
3847 DCHECK(args->length() == 1);
3849 VisitForAccumulatorValue(args->at(0)); // Load the object.
3852 // If the object is a smi return the object.
3853 __ JumpIfSmi(v0, &done);
3854 // If the object is not a value type, return the object.
3855 __ GetObjectType(v0, a1, a1);
3856 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
3858 __ ld(v0, FieldMemOperand(v0, JSValue::kValueOffset));
3861 context()->Plug(v0);
3865 void FullCodeGenerator::EmitIsDate(CallRuntime* expr) {
3866 ZoneList<Expression*>* args = expr->arguments();
3867 DCHECK_EQ(1, args->length());
3869 VisitForAccumulatorValue(args->at(0));
3871 Label materialize_true, materialize_false;
3872 Label* if_true = nullptr;
3873 Label* if_false = nullptr;
3874 Label* fall_through = nullptr;
3875 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3876 &if_false, &fall_through);
3878 __ JumpIfSmi(v0, if_false);
3879 __ GetObjectType(v0, a1, a1);
3880 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3881 Split(eq, a1, Operand(JS_DATE_TYPE), if_true, if_false, fall_through);
3883 context()->Plug(if_true, if_false);
3887 void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3888 ZoneList<Expression*>* args = expr->arguments();
3889 DCHECK(args->length() == 2);
3890 DCHECK_NOT_NULL(args->at(1)->AsLiteral());
3891 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->value()));
3893 VisitForAccumulatorValue(args->at(0)); // Load the object.
3895 Register object = v0;
3896 Register result = v0;
3897 Register scratch0 = t1;
3898 Register scratch1 = a1;
3900 if (index->value() == 0) {
3901 __ ld(result, FieldMemOperand(object, JSDate::kValueOffset));
3903 Label runtime, done;
3904 if (index->value() < JSDate::kFirstUncachedField) {
3905 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3906 __ li(scratch1, Operand(stamp));
3907 __ ld(scratch1, MemOperand(scratch1));
3908 __ ld(scratch0, FieldMemOperand(object, JSDate::kCacheStampOffset));
3909 __ Branch(&runtime, ne, scratch1, Operand(scratch0));
3910 __ ld(result, FieldMemOperand(object, JSDate::kValueOffset +
3911 kPointerSize * index->value()));
3915 __ PrepareCallCFunction(2, scratch1);
3916 __ li(a1, Operand(index));
3917 __ Move(a0, object);
3918 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
3922 context()->Plug(result);
3926 void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
3927 ZoneList<Expression*>* args = expr->arguments();
3928 DCHECK_EQ(3, args->length());
3930 Register string = v0;
3931 Register index = a1;
3932 Register value = a2;
3934 VisitForStackValue(args->at(0)); // index
3935 VisitForStackValue(args->at(1)); // value
3936 VisitForAccumulatorValue(args->at(2)); // string
3937 __ Pop(index, value);
3939 if (FLAG_debug_code) {
3940 __ SmiTst(value, at);
3941 __ Check(eq, kNonSmiValue, at, Operand(zero_reg));
3942 __ SmiTst(index, at);
3943 __ Check(eq, kNonSmiIndex, at, Operand(zero_reg));
3944 __ SmiUntag(index, index);
3945 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
3946 Register scratch = t1;
3947 __ EmitSeqStringSetCharCheck(
3948 string, index, value, scratch, one_byte_seq_type);
3949 __ SmiTag(index, index);
3952 __ SmiUntag(value, value);
3955 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
3957 __ Daddu(at, at, index);
3958 __ sb(value, MemOperand(at));
3959 context()->Plug(string);
3963 void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
3964 ZoneList<Expression*>* args = expr->arguments();
3965 DCHECK_EQ(3, args->length());
3967 Register string = v0;
3968 Register index = a1;
3969 Register value = a2;
3971 VisitForStackValue(args->at(0)); // index
3972 VisitForStackValue(args->at(1)); // value
3973 VisitForAccumulatorValue(args->at(2)); // string
3974 __ Pop(index, value);
3976 if (FLAG_debug_code) {
3977 __ SmiTst(value, at);
3978 __ Check(eq, kNonSmiValue, at, Operand(zero_reg));
3979 __ SmiTst(index, at);
3980 __ Check(eq, kNonSmiIndex, at, Operand(zero_reg));
3981 __ SmiUntag(index, index);
3982 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
3983 Register scratch = t1;
3984 __ EmitSeqStringSetCharCheck(
3985 string, index, value, scratch, two_byte_seq_type);
3986 __ SmiTag(index, index);
3989 __ SmiUntag(value, value);
3992 Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3993 __ dsra(index, index, 32 - 1);
3994 __ Daddu(at, at, index);
3995 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
3996 __ sh(value, MemOperand(at));
3997 context()->Plug(string);
4001 void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
4002 ZoneList<Expression*>* args = expr->arguments();
4003 DCHECK(args->length() == 2);
4005 VisitForStackValue(args->at(0)); // Load the object.
4006 VisitForAccumulatorValue(args->at(1)); // Load the value.
4007 __ pop(a1); // v0 = value. a1 = object.
4010 // If the object is a smi, return the value.
4011 __ JumpIfSmi(a1, &done);
4013 // If the object is not a value type, return the value.
4014 __ GetObjectType(a1, a2, a2);
4015 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
4018 __ sd(v0, FieldMemOperand(a1, JSValue::kValueOffset));
4019 // Update the write barrier. Save the value as it will be
4020 // overwritten by the write barrier code and is needed afterward.
4022 __ RecordWriteField(
4023 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
4026 context()->Plug(v0);
4030 void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
4031 ZoneList<Expression*>* args = expr->arguments();
4032 DCHECK_EQ(args->length(), 1);
4034 // Load the argument into a0 and call the stub.
4035 VisitForAccumulatorValue(args->at(0));
4036 __ mov(a0, result_register());
4038 NumberToStringStub stub(isolate());
4040 context()->Plug(v0);
4044 void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
4045 ZoneList<Expression*>* args = expr->arguments();
4046 DCHECK(args->length() == 1);
4048 VisitForAccumulatorValue(args->at(0));
4051 StringCharFromCodeGenerator generator(v0, a1);
4052 generator.GenerateFast(masm_);
4055 NopRuntimeCallHelper call_helper;
4056 generator.GenerateSlow(masm_, call_helper);
4059 context()->Plug(a1);
4063 void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
4064 ZoneList<Expression*>* args = expr->arguments();
4065 DCHECK(args->length() == 2);
4067 VisitForStackValue(args->at(0));
4068 VisitForAccumulatorValue(args->at(1));
4069 __ mov(a0, result_register());
4071 Register object = a1;
4072 Register index = a0;
4073 Register result = v0;
4077 Label need_conversion;
4078 Label index_out_of_range;
4080 StringCharCodeAtGenerator generator(object,
4085 &index_out_of_range,
4086 STRING_INDEX_IS_NUMBER);
4087 generator.GenerateFast(masm_);
4090 __ bind(&index_out_of_range);
4091 // When the index is out of range, the spec requires us to return
4093 __ LoadRoot(result, Heap::kNanValueRootIndex);
4096 __ bind(&need_conversion);
4097 // Load the undefined value into the result register, which will
4098 // trigger conversion.
4099 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
4102 NopRuntimeCallHelper call_helper;
4103 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
4106 context()->Plug(result);
4110 void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
4111 ZoneList<Expression*>* args = expr->arguments();
4112 DCHECK(args->length() == 2);
4114 VisitForStackValue(args->at(0));
4115 VisitForAccumulatorValue(args->at(1));
4116 __ mov(a0, result_register());
4118 Register object = a1;
4119 Register index = a0;
4120 Register scratch = a3;
4121 Register result = v0;
4125 Label need_conversion;
4126 Label index_out_of_range;
4128 StringCharAtGenerator generator(object,
4134 &index_out_of_range,
4135 STRING_INDEX_IS_NUMBER);
4136 generator.GenerateFast(masm_);
4139 __ bind(&index_out_of_range);
4140 // When the index is out of range, the spec requires us to return
4141 // the empty string.
4142 __ LoadRoot(result, Heap::kempty_stringRootIndex);
4145 __ bind(&need_conversion);
4146 // Move smi zero into the result register, which will trigger
4148 __ li(result, Operand(Smi::FromInt(0)));
4151 NopRuntimeCallHelper call_helper;
4152 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
4155 context()->Plug(result);
4159 void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
4160 ZoneList<Expression*>* args = expr->arguments();
4161 DCHECK_EQ(2, args->length());
4162 VisitForStackValue(args->at(0));
4163 VisitForAccumulatorValue(args->at(1));
4166 __ mov(a0, result_register()); // StringAddStub requires args in a0, a1.
4167 StringAddStub stub(isolate(), STRING_ADD_CHECK_BOTH, NOT_TENURED);
4169 context()->Plug(v0);
4173 void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
4174 ZoneList<Expression*>* args = expr->arguments();
4175 DCHECK(args->length() >= 2);
4177 int arg_count = args->length() - 2; // 2 ~ receiver and function.
4178 for (int i = 0; i < arg_count + 1; i++) {
4179 VisitForStackValue(args->at(i));
4181 VisitForAccumulatorValue(args->last()); // Function.
4183 Label runtime, done;
4184 // Check for non-function argument (including proxy).
4185 __ JumpIfSmi(v0, &runtime);
4186 __ GetObjectType(v0, a1, a1);
4187 __ Branch(&runtime, ne, a1, Operand(JS_FUNCTION_TYPE));
4189 // InvokeFunction requires the function in a1. Move it in there.
4190 __ mov(a1, result_register());
4191 ParameterCount count(arg_count);
4192 __ InvokeFunction(a1, count, CALL_FUNCTION, NullCallWrapper());
4193 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4198 __ CallRuntime(Runtime::kCall, args->length());
4201 context()->Plug(v0);
4205 void FullCodeGenerator::EmitDefaultConstructorCallSuper(CallRuntime* expr) {
4206 ZoneList<Expression*>* args = expr->arguments();
4207 DCHECK(args->length() == 2);
4210 VisitForStackValue(args->at(0));
4213 VisitForStackValue(args->at(1));
4214 __ CallRuntime(Runtime::kGetPrototype, 1);
4215 __ Push(result_register());
4217 // Load original constructor into a4.
4218 __ ld(a4, MemOperand(sp, 1 * kPointerSize));
4220 // Check if the calling frame is an arguments adaptor frame.
4221 Label adaptor_frame, args_set_up, runtime;
4222 __ ld(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4223 __ ld(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
4224 __ Branch(&adaptor_frame, eq, a3,
4225 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4226 // default constructor has no arguments, so no adaptor frame means no args.
4227 __ mov(a0, zero_reg);
4228 __ Branch(&args_set_up);
4230 // Copy arguments from adaptor frame.
4232 __ bind(&adaptor_frame);
4233 __ ld(a1, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4234 __ SmiUntag(a1, a1);
4238 // Get arguments pointer in a2.
4239 __ dsll(at, a1, kPointerSizeLog2);
4240 __ Daddu(a2, a2, Operand(at));
4241 __ Daddu(a2, a2, Operand(StandardFrameConstants::kCallerSPOffset));
4244 // Pre-decrement a2 with kPointerSize on each iteration.
4245 // Pre-decrement in order to skip receiver.
4246 __ Daddu(a2, a2, Operand(-kPointerSize));
4247 __ ld(a3, MemOperand(a2));
4249 __ Daddu(a1, a1, Operand(-1));
4250 __ Branch(&loop, ne, a1, Operand(zero_reg));
4253 __ bind(&args_set_up);
4254 __ dsll(at, a0, kPointerSizeLog2);
4255 __ Daddu(at, at, Operand(sp));
4256 __ ld(a1, MemOperand(at, 0));
4257 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4259 CallConstructStub stub(isolate(), SUPER_CONSTRUCTOR_CALL);
4260 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
4264 context()->Plug(result_register());
4268 void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
4269 RegExpConstructResultStub stub(isolate());
4270 ZoneList<Expression*>* args = expr->arguments();
4271 DCHECK(args->length() == 3);
4272 VisitForStackValue(args->at(0));
4273 VisitForStackValue(args->at(1));
4274 VisitForAccumulatorValue(args->at(2));
4275 __ mov(a0, result_register());
4279 context()->Plug(v0);
4283 void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
4284 ZoneList<Expression*>* args = expr->arguments();
4285 DCHECK_EQ(2, args->length());
4287 DCHECK_NOT_NULL(args->at(0)->AsLiteral());
4288 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->value()))->value();
4290 Handle<FixedArray> jsfunction_result_caches(
4291 isolate()->native_context()->jsfunction_result_caches());
4292 if (jsfunction_result_caches->length() <= cache_id) {
4293 __ Abort(kAttemptToUseUndefinedCache);
4294 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
4295 context()->Plug(v0);
4299 VisitForAccumulatorValue(args->at(1));
4302 Register cache = a1;
4303 __ ld(cache, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
4304 __ ld(cache, FieldMemOperand(cache, GlobalObject::kNativeContextOffset));
4307 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
4309 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
4312 Label done, not_found;
4313 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
4314 __ ld(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
4315 // a2 now holds finger offset as a smi.
4316 __ Daddu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4317 // a3 now points to the start of fixed array elements.
4318 __ SmiScale(at, a2, kPointerSizeLog2);
4319 __ daddu(a3, a3, at);
4320 // a3 now points to key of indexed element of cache.
4321 __ ld(a2, MemOperand(a3));
4322 __ Branch(¬_found, ne, key, Operand(a2));
4324 __ ld(v0, MemOperand(a3, kPointerSize));
4327 __ bind(¬_found);
4328 // Call runtime to perform the lookup.
4329 __ Push(cache, key);
4330 __ CallRuntime(Runtime::kGetFromCacheRT, 2);
4333 context()->Plug(v0);
4337 void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
4338 ZoneList<Expression*>* args = expr->arguments();
4339 VisitForAccumulatorValue(args->at(0));
4341 Label materialize_true, materialize_false;
4342 Label* if_true = NULL;
4343 Label* if_false = NULL;
4344 Label* fall_through = NULL;
4345 context()->PrepareTest(&materialize_true, &materialize_false,
4346 &if_true, &if_false, &fall_through);
4348 __ lwu(a0, FieldMemOperand(v0, String::kHashFieldOffset));
4349 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
4351 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4352 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
4354 context()->Plug(if_true, if_false);
4358 void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
4359 ZoneList<Expression*>* args = expr->arguments();
4360 DCHECK(args->length() == 1);
4361 VisitForAccumulatorValue(args->at(0));
4363 __ AssertString(v0);
4365 __ lwu(v0, FieldMemOperand(v0, String::kHashFieldOffset));
4366 __ IndexFromHash(v0, v0);
4368 context()->Plug(v0);
4372 void FullCodeGenerator::EmitFastOneByteArrayJoin(CallRuntime* expr) {
4373 Label bailout, done, one_char_separator, long_separator,
4374 non_trivial_array, not_size_one_array, loop,
4375 empty_separator_loop, one_char_separator_loop,
4376 one_char_separator_loop_entry, long_separator_loop;
4377 ZoneList<Expression*>* args = expr->arguments();
4378 DCHECK(args->length() == 2);
4379 VisitForStackValue(args->at(1));
4380 VisitForAccumulatorValue(args->at(0));
4382 // All aliases of the same register have disjoint lifetimes.
4383 Register array = v0;
4384 Register elements = no_reg; // Will be v0.
4385 Register result = no_reg; // Will be v0.
4386 Register separator = a1;
4387 Register array_length = a2;
4388 Register result_pos = no_reg; // Will be a2.
4389 Register string_length = a3;
4390 Register string = a4;
4391 Register element = a5;
4392 Register elements_end = a6;
4393 Register scratch1 = a7;
4394 Register scratch2 = t1;
4395 Register scratch3 = t0;
4397 // Separator operand is on the stack.
4400 // Check that the array is a JSArray.
4401 __ JumpIfSmi(array, &bailout);
4402 __ GetObjectType(array, scratch1, scratch2);
4403 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
4405 // Check that the array has fast elements.
4406 __ CheckFastElements(scratch1, scratch2, &bailout);
4408 // If the array has length zero, return the empty string.
4409 __ ld(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
4410 __ SmiUntag(array_length);
4411 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
4412 __ LoadRoot(v0, Heap::kempty_stringRootIndex);
4415 __ bind(&non_trivial_array);
4417 // Get the FixedArray containing array's elements.
4419 __ ld(elements, FieldMemOperand(array, JSArray::kElementsOffset));
4420 array = no_reg; // End of array's live range.
4422 // Check that all array elements are sequential one-byte strings, and
4423 // accumulate the sum of their lengths, as a smi-encoded value.
4424 __ mov(string_length, zero_reg);
4426 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4427 __ dsll(elements_end, array_length, kPointerSizeLog2);
4428 __ Daddu(elements_end, element, elements_end);
4429 // Loop condition: while (element < elements_end).
4430 // Live values in registers:
4431 // elements: Fixed array of strings.
4432 // array_length: Length of the fixed array of strings (not smi)
4433 // separator: Separator string
4434 // string_length: Accumulated sum of string lengths (smi).
4435 // element: Current array element.
4436 // elements_end: Array end.
4437 if (generate_debug_code_) {
4438 __ Assert(gt, kNoEmptyArraysHereInEmitFastOneByteArrayJoin, array_length,
4442 __ ld(string, MemOperand(element));
4443 __ Daddu(element, element, kPointerSize);
4444 __ JumpIfSmi(string, &bailout);
4445 __ ld(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
4446 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
4447 __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
4448 __ ld(scratch1, FieldMemOperand(string, SeqOneByteString::kLengthOffset));
4449 __ DadduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
4450 __ BranchOnOverflow(&bailout, scratch3);
4451 __ Branch(&loop, lt, element, Operand(elements_end));
4453 // If array_length is 1, return elements[0], a string.
4454 __ Branch(¬_size_one_array, ne, array_length, Operand(1));
4455 __ ld(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
4458 __ bind(¬_size_one_array);
4460 // Live values in registers:
4461 // separator: Separator string
4462 // array_length: Length of the array.
4463 // string_length: Sum of string lengths (smi).
4464 // elements: FixedArray of strings.
4466 // Check that the separator is a flat one-byte string.
4467 __ JumpIfSmi(separator, &bailout);
4468 __ ld(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
4469 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
4470 __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
4472 // Add (separator length times array_length) - separator length to the
4473 // string_length to get the length of the result string. array_length is not
4474 // smi but the other values are, so the result is a smi.
4475 __ ld(scratch1, FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
4476 __ Dsubu(string_length, string_length, Operand(scratch1));
4477 __ SmiUntag(scratch1);
4478 __ Dmul(scratch2, array_length, scratch1);
4479 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
4481 __ dsra32(scratch1, scratch2, 0);
4482 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
4483 __ SmiUntag(string_length);
4484 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
4485 __ BranchOnOverflow(&bailout, scratch3);
4487 // Get first element in the array to free up the elements register to be used
4490 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4491 result = elements; // End of live range for elements.
4493 // Live values in registers:
4494 // element: First array element
4495 // separator: Separator string
4496 // string_length: Length of result string (not smi)
4497 // array_length: Length of the array.
4498 __ AllocateOneByteString(result, string_length, scratch1, scratch2,
4499 elements_end, &bailout);
4500 // Prepare for looping. Set up elements_end to end of the array. Set
4501 // result_pos to the position of the result where to write the first
4503 __ dsll(elements_end, array_length, kPointerSizeLog2);
4504 __ Daddu(elements_end, element, elements_end);
4505 result_pos = array_length; // End of live range for array_length.
4506 array_length = no_reg;
4507 __ Daddu(result_pos,
4509 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4511 // Check the length of the separator.
4512 __ ld(scratch1, FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
4513 __ li(at, Operand(Smi::FromInt(1)));
4514 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
4515 __ Branch(&long_separator, gt, scratch1, Operand(at));
4517 // Empty separator case.
4518 __ bind(&empty_separator_loop);
4519 // Live values in registers:
4520 // result_pos: the position to which we are currently copying characters.
4521 // element: Current array element.
4522 // elements_end: Array end.
4524 // Copy next array element to the result.
4525 __ ld(string, MemOperand(element));
4526 __ Daddu(element, element, kPointerSize);
4527 __ ld(string_length, FieldMemOperand(string, String::kLengthOffset));
4528 __ SmiUntag(string_length);
4529 __ Daddu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4530 __ CopyBytes(string, result_pos, string_length, scratch1);
4531 // End while (element < elements_end).
4532 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
4533 DCHECK(result.is(v0));
4536 // One-character separator case.
4537 __ bind(&one_char_separator);
4538 // Replace separator with its one-byte character value.
4539 __ lbu(separator, FieldMemOperand(separator, SeqOneByteString::kHeaderSize));
4540 // Jump into the loop after the code that copies the separator, so the first
4541 // element is not preceded by a separator.
4542 __ jmp(&one_char_separator_loop_entry);
4544 __ bind(&one_char_separator_loop);
4545 // Live values in registers:
4546 // result_pos: the position to which we are currently copying characters.
4547 // element: Current array element.
4548 // elements_end: Array end.
4549 // separator: Single separator one-byte char (in lower byte).
4551 // Copy the separator character to the result.
4552 __ sb(separator, MemOperand(result_pos));
4553 __ Daddu(result_pos, result_pos, 1);
4555 // Copy next array element to the result.
4556 __ bind(&one_char_separator_loop_entry);
4557 __ ld(string, MemOperand(element));
4558 __ Daddu(element, element, kPointerSize);
4559 __ ld(string_length, FieldMemOperand(string, String::kLengthOffset));
4560 __ SmiUntag(string_length);
4561 __ Daddu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4562 __ CopyBytes(string, result_pos, string_length, scratch1);
4563 // End while (element < elements_end).
4564 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
4565 DCHECK(result.is(v0));
4568 // Long separator case (separator is more than one character). Entry is at the
4569 // label long_separator below.
4570 __ bind(&long_separator_loop);
4571 // Live values in registers:
4572 // result_pos: the position to which we are currently copying characters.
4573 // element: Current array element.
4574 // elements_end: Array end.
4575 // separator: Separator string.
4577 // Copy the separator to the result.
4578 __ ld(string_length, FieldMemOperand(separator, String::kLengthOffset));
4579 __ SmiUntag(string_length);
4582 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4583 __ CopyBytes(string, result_pos, string_length, scratch1);
4585 __ bind(&long_separator);
4586 __ ld(string, MemOperand(element));
4587 __ Daddu(element, element, kPointerSize);
4588 __ ld(string_length, FieldMemOperand(string, String::kLengthOffset));
4589 __ SmiUntag(string_length);
4590 __ Daddu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4591 __ CopyBytes(string, result_pos, string_length, scratch1);
4592 // End while (element < elements_end).
4593 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
4594 DCHECK(result.is(v0));
4598 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
4600 context()->Plug(v0);
4604 void FullCodeGenerator::EmitDebugIsActive(CallRuntime* expr) {
4605 DCHECK(expr->arguments()->length() == 0);
4606 ExternalReference debug_is_active =
4607 ExternalReference::debug_is_active_address(isolate());
4608 __ li(at, Operand(debug_is_active));
4609 __ lbu(v0, MemOperand(at));
4611 context()->Plug(v0);
4615 void FullCodeGenerator::EmitLoadJSRuntimeFunction(CallRuntime* expr) {
4616 // Push the builtins object as the receiver.
4617 Register receiver = LoadDescriptor::ReceiverRegister();
4618 __ ld(receiver, GlobalObjectOperand());
4619 __ ld(receiver, FieldMemOperand(receiver, GlobalObject::kBuiltinsOffset));
4622 // Load the function from the receiver.
4623 __ li(LoadDescriptor::NameRegister(), Operand(expr->name()));
4624 __ li(LoadDescriptor::SlotRegister(),
4625 Operand(SmiFromSlot(expr->CallRuntimeFeedbackSlot())));
4626 CallLoadIC(NOT_INSIDE_TYPEOF);
4630 void FullCodeGenerator::EmitCallJSRuntimeFunction(CallRuntime* expr) {
4631 ZoneList<Expression*>* args = expr->arguments();
4632 int arg_count = args->length();
4634 SetCallPosition(expr, arg_count);
4635 CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
4636 __ ld(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
4641 void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
4642 ZoneList<Expression*>* args = expr->arguments();
4643 int arg_count = args->length();
4645 if (expr->is_jsruntime()) {
4646 Comment cmnt(masm_, "[ CallRuntime");
4647 EmitLoadJSRuntimeFunction(expr);
4649 // Push the target function under the receiver.
4650 __ ld(at, MemOperand(sp, 0));
4652 __ sd(v0, MemOperand(sp, kPointerSize));
4654 // Push the arguments ("left-to-right").
4655 for (int i = 0; i < arg_count; i++) {
4656 VisitForStackValue(args->at(i));
4659 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4660 EmitCallJSRuntimeFunction(expr);
4662 // Restore context register.
4663 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4665 context()->DropAndPlug(1, v0);
4667 const Runtime::Function* function = expr->function();
4668 switch (function->function_id) {
4669 #define CALL_INTRINSIC_GENERATOR(Name) \
4670 case Runtime::kInline##Name: { \
4671 Comment cmnt(masm_, "[ Inline" #Name); \
4672 return Emit##Name(expr); \
4674 FOR_EACH_FULL_CODE_INTRINSIC(CALL_INTRINSIC_GENERATOR)
4675 #undef CALL_INTRINSIC_GENERATOR
4677 Comment cmnt(masm_, "[ CallRuntime for unhandled intrinsic");
4678 // Push the arguments ("left-to-right").
4679 for (int i = 0; i < arg_count; i++) {
4680 VisitForStackValue(args->at(i));
4683 // Call the C runtime function.
4684 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4685 __ CallRuntime(expr->function(), arg_count);
4686 context()->Plug(v0);
4693 void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
4694 switch (expr->op()) {
4695 case Token::DELETE: {
4696 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
4697 Property* property = expr->expression()->AsProperty();
4698 VariableProxy* proxy = expr->expression()->AsVariableProxy();
4700 if (property != NULL) {
4701 VisitForStackValue(property->obj());
4702 VisitForStackValue(property->key());
4703 __ li(a1, Operand(Smi::FromInt(language_mode())));
4705 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4706 context()->Plug(v0);
4707 } else if (proxy != NULL) {
4708 Variable* var = proxy->var();
4709 // Delete of an unqualified identifier is disallowed in strict mode but
4710 // "delete this" is allowed.
4711 bool is_this = var->HasThisName(isolate());
4712 DCHECK(is_sloppy(language_mode()) || is_this);
4713 if (var->IsUnallocatedOrGlobalSlot()) {
4714 __ ld(a2, GlobalObjectOperand());
4715 __ li(a1, Operand(var->name()));
4716 __ li(a0, Operand(Smi::FromInt(SLOPPY)));
4717 __ Push(a2, a1, a0);
4718 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4719 context()->Plug(v0);
4720 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
4721 // Result of deleting non-global, non-dynamic variables is false.
4722 // The subexpression does not have side effects.
4723 context()->Plug(is_this);
4725 // Non-global variable. Call the runtime to try to delete from the
4726 // context where the variable was introduced.
4727 DCHECK(!context_register().is(a2));
4728 __ li(a2, Operand(var->name()));
4729 __ Push(context_register(), a2);
4730 __ CallRuntime(Runtime::kDeleteLookupSlot, 2);
4731 context()->Plug(v0);
4734 // Result of deleting non-property, non-variable reference is true.
4735 // The subexpression may have side effects.
4736 VisitForEffect(expr->expression());
4737 context()->Plug(true);
4743 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
4744 VisitForEffect(expr->expression());
4745 context()->Plug(Heap::kUndefinedValueRootIndex);
4750 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
4751 if (context()->IsEffect()) {
4752 // Unary NOT has no side effects so it's only necessary to visit the
4753 // subexpression. Match the optimizing compiler by not branching.
4754 VisitForEffect(expr->expression());
4755 } else if (context()->IsTest()) {
4756 const TestContext* test = TestContext::cast(context());
4757 // The labels are swapped for the recursive call.
4758 VisitForControl(expr->expression(),
4759 test->false_label(),
4761 test->fall_through());
4762 context()->Plug(test->true_label(), test->false_label());
4764 // We handle value contexts explicitly rather than simply visiting
4765 // for control and plugging the control flow into the context,
4766 // because we need to prepare a pair of extra administrative AST ids
4767 // for the optimizing compiler.
4768 DCHECK(context()->IsAccumulatorValue() || context()->IsStackValue());
4769 Label materialize_true, materialize_false, done;
4770 VisitForControl(expr->expression(),
4774 __ bind(&materialize_true);
4775 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
4776 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
4777 if (context()->IsStackValue()) __ push(v0);
4779 __ bind(&materialize_false);
4780 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
4781 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
4782 if (context()->IsStackValue()) __ push(v0);
4788 case Token::TYPEOF: {
4789 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
4791 AccumulatorValueContext context(this);
4792 VisitForTypeofValue(expr->expression());
4795 TypeofStub typeof_stub(isolate());
4796 __ CallStub(&typeof_stub);
4797 context()->Plug(v0);
4807 void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
4808 DCHECK(expr->expression()->IsValidReferenceExpressionOrThis());
4810 Comment cmnt(masm_, "[ CountOperation");
4812 Property* prop = expr->expression()->AsProperty();
4813 LhsKind assign_type = Property::GetAssignType(prop);
4815 // Evaluate expression and get value.
4816 if (assign_type == VARIABLE) {
4817 DCHECK(expr->expression()->AsVariableProxy()->var() != NULL);
4818 AccumulatorValueContext context(this);
4819 EmitVariableLoad(expr->expression()->AsVariableProxy());
4821 // Reserve space for result of postfix operation.
4822 if (expr->is_postfix() && !context()->IsEffect()) {
4823 __ li(at, Operand(Smi::FromInt(0)));
4826 switch (assign_type) {
4827 case NAMED_PROPERTY: {
4828 // Put the object both on the stack and in the register.
4829 VisitForStackValue(prop->obj());
4830 __ ld(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
4831 EmitNamedPropertyLoad(prop);
4835 case NAMED_SUPER_PROPERTY: {
4836 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4837 VisitForAccumulatorValue(
4838 prop->obj()->AsSuperPropertyReference()->home_object());
4839 __ Push(result_register());
4840 const Register scratch = a1;
4841 __ ld(scratch, MemOperand(sp, kPointerSize));
4842 __ Push(scratch, result_register());
4843 EmitNamedSuperPropertyLoad(prop);
4847 case KEYED_SUPER_PROPERTY: {
4848 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4849 VisitForAccumulatorValue(
4850 prop->obj()->AsSuperPropertyReference()->home_object());
4851 const Register scratch = a1;
4852 const Register scratch1 = a4;
4853 __ Move(scratch, result_register());
4854 VisitForAccumulatorValue(prop->key());
4855 __ Push(scratch, result_register());
4856 __ ld(scratch1, MemOperand(sp, 2 * kPointerSize));
4857 __ Push(scratch1, scratch, result_register());
4858 EmitKeyedSuperPropertyLoad(prop);
4862 case KEYED_PROPERTY: {
4863 VisitForStackValue(prop->obj());
4864 VisitForStackValue(prop->key());
4865 __ ld(LoadDescriptor::ReceiverRegister(),
4866 MemOperand(sp, 1 * kPointerSize));
4867 __ ld(LoadDescriptor::NameRegister(), MemOperand(sp, 0));
4868 EmitKeyedPropertyLoad(prop);
4877 // We need a second deoptimization point after loading the value
4878 // in case evaluating the property load my have a side effect.
4879 if (assign_type == VARIABLE) {
4880 PrepareForBailout(expr->expression(), TOS_REG);
4882 PrepareForBailoutForId(prop->LoadId(), TOS_REG);
4885 // Inline smi case if we are in a loop.
4886 Label stub_call, done;
4887 JumpPatchSite patch_site(masm_);
4889 int count_value = expr->op() == Token::INC ? 1 : -1;
4891 if (ShouldInlineSmiCase(expr->op())) {
4893 patch_site.EmitJumpIfNotSmi(v0, &slow);
4895 // Save result for postfix expressions.
4896 if (expr->is_postfix()) {
4897 if (!context()->IsEffect()) {
4898 // Save the result on the stack. If we have a named or keyed property
4899 // we store the result under the receiver that is currently on top
4901 switch (assign_type) {
4905 case NAMED_PROPERTY:
4906 __ sd(v0, MemOperand(sp, kPointerSize));
4908 case NAMED_SUPER_PROPERTY:
4909 __ sd(v0, MemOperand(sp, 2 * kPointerSize));
4911 case KEYED_PROPERTY:
4912 __ sd(v0, MemOperand(sp, 2 * kPointerSize));
4914 case KEYED_SUPER_PROPERTY:
4915 __ sd(v0, MemOperand(sp, 3 * kPointerSize));
4921 Register scratch1 = a1;
4922 Register scratch2 = a4;
4923 __ li(scratch1, Operand(Smi::FromInt(count_value)));
4924 __ DadduAndCheckForOverflow(v0, v0, scratch1, scratch2);
4925 __ BranchOnNoOverflow(&done, scratch2);
4926 // Call stub. Undo operation first.
4931 if (!is_strong(language_mode())) {
4932 ToNumberStub convert_stub(isolate());
4933 __ CallStub(&convert_stub);
4934 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
4937 // Save result for postfix expressions.
4938 if (expr->is_postfix()) {
4939 if (!context()->IsEffect()) {
4940 // Save the result on the stack. If we have a named or keyed property
4941 // we store the result under the receiver that is currently on top
4943 switch (assign_type) {
4947 case NAMED_PROPERTY:
4948 __ sd(v0, MemOperand(sp, kPointerSize));
4950 case NAMED_SUPER_PROPERTY:
4951 __ sd(v0, MemOperand(sp, 2 * kPointerSize));
4953 case KEYED_PROPERTY:
4954 __ sd(v0, MemOperand(sp, 2 * kPointerSize));
4956 case KEYED_SUPER_PROPERTY:
4957 __ sd(v0, MemOperand(sp, 3 * kPointerSize));
4963 __ bind(&stub_call);
4965 __ li(a0, Operand(Smi::FromInt(count_value)));
4967 SetExpressionPosition(expr);
4970 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), Token::ADD,
4971 strength(language_mode())).code();
4972 CallIC(code, expr->CountBinOpFeedbackId());
4973 patch_site.EmitPatchInfo();
4976 if (is_strong(language_mode())) {
4977 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
4979 // Store the value returned in v0.
4980 switch (assign_type) {
4982 if (expr->is_postfix()) {
4983 { EffectContext context(this);
4984 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4985 Token::ASSIGN, expr->CountSlot());
4986 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4989 // For all contexts except EffectConstant we have the result on
4990 // top of the stack.
4991 if (!context()->IsEffect()) {
4992 context()->PlugTOS();
4995 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4996 Token::ASSIGN, expr->CountSlot());
4997 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4998 context()->Plug(v0);
5001 case NAMED_PROPERTY: {
5002 __ mov(StoreDescriptor::ValueRegister(), result_register());
5003 __ li(StoreDescriptor::NameRegister(),
5004 Operand(prop->key()->AsLiteral()->value()));
5005 __ pop(StoreDescriptor::ReceiverRegister());
5006 if (FLAG_vector_stores) {
5007 EmitLoadStoreICSlot(expr->CountSlot());
5010 CallStoreIC(expr->CountStoreFeedbackId());
5012 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5013 if (expr->is_postfix()) {
5014 if (!context()->IsEffect()) {
5015 context()->PlugTOS();
5018 context()->Plug(v0);
5022 case NAMED_SUPER_PROPERTY: {
5023 EmitNamedSuperPropertyStore(prop);
5024 if (expr->is_postfix()) {
5025 if (!context()->IsEffect()) {
5026 context()->PlugTOS();
5029 context()->Plug(v0);
5033 case KEYED_SUPER_PROPERTY: {
5034 EmitKeyedSuperPropertyStore(prop);
5035 if (expr->is_postfix()) {
5036 if (!context()->IsEffect()) {
5037 context()->PlugTOS();
5040 context()->Plug(v0);
5044 case KEYED_PROPERTY: {
5045 __ mov(StoreDescriptor::ValueRegister(), result_register());
5046 __ Pop(StoreDescriptor::ReceiverRegister(),
5047 StoreDescriptor::NameRegister());
5049 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
5050 if (FLAG_vector_stores) {
5051 EmitLoadStoreICSlot(expr->CountSlot());
5054 CallIC(ic, expr->CountStoreFeedbackId());
5056 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5057 if (expr->is_postfix()) {
5058 if (!context()->IsEffect()) {
5059 context()->PlugTOS();
5062 context()->Plug(v0);
5070 void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
5071 Expression* sub_expr,
5072 Handle<String> check) {
5073 Label materialize_true, materialize_false;
5074 Label* if_true = NULL;
5075 Label* if_false = NULL;
5076 Label* fall_through = NULL;
5077 context()->PrepareTest(&materialize_true, &materialize_false,
5078 &if_true, &if_false, &fall_through);
5080 { AccumulatorValueContext context(this);
5081 VisitForTypeofValue(sub_expr);
5083 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5085 Factory* factory = isolate()->factory();
5086 if (String::Equals(check, factory->number_string())) {
5087 __ JumpIfSmi(v0, if_true);
5088 __ ld(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
5089 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5090 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
5091 } else if (String::Equals(check, factory->string_string())) {
5092 __ JumpIfSmi(v0, if_false);
5093 // Check for undetectable objects => false.
5094 __ GetObjectType(v0, v0, a1);
5095 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
5096 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
5097 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
5098 Split(eq, a1, Operand(zero_reg),
5099 if_true, if_false, fall_through);
5100 } else if (String::Equals(check, factory->symbol_string())) {
5101 __ JumpIfSmi(v0, if_false);
5102 __ GetObjectType(v0, v0, a1);
5103 Split(eq, a1, Operand(SYMBOL_TYPE), if_true, if_false, fall_through);
5104 } else if (String::Equals(check, factory->float32x4_string())) {
5105 __ JumpIfSmi(v0, if_false);
5106 __ GetObjectType(v0, v0, a1);
5107 Split(eq, a1, Operand(FLOAT32X4_TYPE), if_true, if_false, fall_through);
5108 } else if (String::Equals(check, factory->boolean_string())) {
5109 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5110 __ Branch(if_true, eq, v0, Operand(at));
5111 __ LoadRoot(at, Heap::kFalseValueRootIndex);
5112 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
5113 } else if (String::Equals(check, factory->undefined_string())) {
5114 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5115 __ Branch(if_true, eq, v0, Operand(at));
5116 __ JumpIfSmi(v0, if_false);
5117 // Check for undetectable objects => true.
5118 __ ld(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
5119 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
5120 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
5121 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
5122 } else if (String::Equals(check, factory->function_string())) {
5123 __ JumpIfSmi(v0, if_false);
5124 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5125 __ GetObjectType(v0, v0, a1);
5126 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
5127 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
5128 if_true, if_false, fall_through);
5129 } else if (String::Equals(check, factory->object_string())) {
5130 __ JumpIfSmi(v0, if_false);
5131 __ LoadRoot(at, Heap::kNullValueRootIndex);
5132 __ Branch(if_true, eq, v0, Operand(at));
5133 // Check for JS objects => true.
5134 __ GetObjectType(v0, v0, a1);
5135 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
5136 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
5137 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
5138 // Check for undetectable objects => false.
5139 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
5140 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
5141 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
5143 if (if_false != fall_through) __ jmp(if_false);
5145 context()->Plug(if_true, if_false);
5149 void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
5150 Comment cmnt(masm_, "[ CompareOperation");
5151 SetExpressionPosition(expr);
5153 // First we try a fast inlined version of the compare when one of
5154 // the operands is a literal.
5155 if (TryLiteralCompare(expr)) return;
5157 // Always perform the comparison for its control flow. Pack the result
5158 // into the expression's context after the comparison is performed.
5159 Label materialize_true, materialize_false;
5160 Label* if_true = NULL;
5161 Label* if_false = NULL;
5162 Label* fall_through = NULL;
5163 context()->PrepareTest(&materialize_true, &materialize_false,
5164 &if_true, &if_false, &fall_through);
5166 Token::Value op = expr->op();
5167 VisitForStackValue(expr->left());
5170 VisitForStackValue(expr->right());
5171 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
5172 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
5173 __ LoadRoot(a4, Heap::kTrueValueRootIndex);
5174 Split(eq, v0, Operand(a4), if_true, if_false, fall_through);
5177 case Token::INSTANCEOF: {
5178 VisitForStackValue(expr->right());
5179 InstanceofStub stub(isolate(), InstanceofStub::kNoFlags);
5181 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5182 // The stub returns 0 for true.
5183 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
5188 VisitForAccumulatorValue(expr->right());
5189 Condition cc = CompareIC::ComputeCondition(op);
5190 __ mov(a0, result_register());
5193 bool inline_smi_code = ShouldInlineSmiCase(op);
5194 JumpPatchSite patch_site(masm_);
5195 if (inline_smi_code) {
5197 __ Or(a2, a0, Operand(a1));
5198 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
5199 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
5200 __ bind(&slow_case);
5203 Handle<Code> ic = CodeFactory::CompareIC(
5204 isolate(), op, strength(language_mode())).code();
5205 CallIC(ic, expr->CompareOperationFeedbackId());
5206 patch_site.EmitPatchInfo();
5207 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5208 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
5212 // Convert the result of the comparison into one expected for this
5213 // expression's context.
5214 context()->Plug(if_true, if_false);
5218 void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
5219 Expression* sub_expr,
5221 Label materialize_true, materialize_false;
5222 Label* if_true = NULL;
5223 Label* if_false = NULL;
5224 Label* fall_through = NULL;
5225 context()->PrepareTest(&materialize_true, &materialize_false,
5226 &if_true, &if_false, &fall_through);
5228 VisitForAccumulatorValue(sub_expr);
5229 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5230 __ mov(a0, result_register());
5231 if (expr->op() == Token::EQ_STRICT) {
5232 Heap::RootListIndex nil_value = nil == kNullValue ?
5233 Heap::kNullValueRootIndex :
5234 Heap::kUndefinedValueRootIndex;
5235 __ LoadRoot(a1, nil_value);
5236 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
5238 Handle<Code> ic = CompareNilICStub::GetUninitialized(isolate(), nil);
5239 CallIC(ic, expr->CompareOperationFeedbackId());
5240 Split(ne, v0, Operand(zero_reg), if_true, if_false, fall_through);
5242 context()->Plug(if_true, if_false);
5246 void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
5247 __ ld(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5248 context()->Plug(v0);
5252 Register FullCodeGenerator::result_register() {
5257 Register FullCodeGenerator::context_register() {
5262 void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
5263 // DCHECK_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
5264 DCHECK(IsAligned(frame_offset, kPointerSize));
5265 // __ sw(value, MemOperand(fp, frame_offset));
5266 __ sd(value, MemOperand(fp, frame_offset));
5270 void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
5271 __ ld(dst, ContextOperand(cp, context_index));
5275 void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
5276 Scope* closure_scope = scope()->ClosureScope();
5277 if (closure_scope->is_script_scope() ||
5278 closure_scope->is_module_scope()) {
5279 // Contexts nested in the native context have a canonical empty function
5280 // as their closure, not the anonymous closure containing the global
5281 // code. Pass a smi sentinel and let the runtime look up the empty
5283 __ li(at, Operand(Smi::FromInt(0)));
5284 } else if (closure_scope->is_eval_scope()) {
5285 // Contexts created by a call to eval have the same closure as the
5286 // context calling eval, not the anonymous closure containing the eval
5287 // code. Fetch it from the context.
5288 __ ld(at, ContextOperand(cp, Context::CLOSURE_INDEX));
5290 DCHECK(closure_scope->is_function_scope());
5291 __ ld(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5297 // ----------------------------------------------------------------------------
5298 // Non-local control flow support.
5300 void FullCodeGenerator::EnterFinallyBlock() {
5301 DCHECK(!result_register().is(a1));
5302 // Store result register while executing finally block.
5303 __ push(result_register());
5304 // Cook return address in link register to stack (smi encoded Code* delta).
5305 __ Dsubu(a1, ra, Operand(masm_->CodeObject()));
5308 // Store result register while executing finally block.
5311 // Store pending message while executing finally block.
5312 ExternalReference pending_message_obj =
5313 ExternalReference::address_of_pending_message_obj(isolate());
5314 __ li(at, Operand(pending_message_obj));
5315 __ ld(a1, MemOperand(at));
5318 ClearPendingMessage();
5322 void FullCodeGenerator::ExitFinallyBlock() {
5323 DCHECK(!result_register().is(a1));
5324 // Restore pending message from stack.
5326 ExternalReference pending_message_obj =
5327 ExternalReference::address_of_pending_message_obj(isolate());
5328 __ li(at, Operand(pending_message_obj));
5329 __ sd(a1, MemOperand(at));
5331 // Restore result register from stack.
5334 // Uncook return address and return.
5335 __ pop(result_register());
5338 __ Daddu(at, a1, Operand(masm_->CodeObject()));
5343 void FullCodeGenerator::ClearPendingMessage() {
5344 DCHECK(!result_register().is(a1));
5345 ExternalReference pending_message_obj =
5346 ExternalReference::address_of_pending_message_obj(isolate());
5347 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
5348 __ li(at, Operand(pending_message_obj));
5349 __ sd(a1, MemOperand(at));
5353 void FullCodeGenerator::EmitLoadStoreICSlot(FeedbackVectorICSlot slot) {
5354 DCHECK(FLAG_vector_stores && !slot.IsInvalid());
5355 __ li(VectorStoreICTrampolineDescriptor::SlotRegister(),
5356 Operand(SmiFromSlot(slot)));
5363 void BackEdgeTable::PatchAt(Code* unoptimized_code,
5365 BackEdgeState target_state,
5366 Code* replacement_code) {
5367 static const int kInstrSize = Assembler::kInstrSize;
5368 Address branch_address = pc - 8 * kInstrSize;
5369 CodePatcher patcher(branch_address, 1);
5371 switch (target_state) {
5373 // slt at, a3, zero_reg (in case of count based interrupts)
5374 // beq at, zero_reg, ok
5375 // lui t9, <interrupt stub address> upper
5376 // ori t9, <interrupt stub address> u-middle
5378 // ori t9, <interrupt stub address> lower
5381 // ok-label ----- pc_after points here
5382 patcher.masm()->slt(at, a3, zero_reg);
5384 case ON_STACK_REPLACEMENT:
5385 case OSR_AFTER_STACK_CHECK:
5386 // addiu at, zero_reg, 1
5387 // beq at, zero_reg, ok ;; Not changed
5388 // lui t9, <on-stack replacement address> upper
5389 // ori t9, <on-stack replacement address> middle
5391 // ori t9, <on-stack replacement address> lower
5392 // jalr t9 ;; Not changed
5393 // nop ;; Not changed
5394 // ok-label ----- pc_after points here
5395 patcher.masm()->daddiu(at, zero_reg, 1);
5398 Address pc_immediate_load_address = pc - 6 * kInstrSize;
5399 // Replace the stack check address in the load-immediate (6-instr sequence)
5400 // with the entry address of the replacement code.
5401 Assembler::set_target_address_at(pc_immediate_load_address,
5402 replacement_code->entry());
5404 unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
5405 unoptimized_code, pc_immediate_load_address, replacement_code);
5409 BackEdgeTable::BackEdgeState BackEdgeTable::GetBackEdgeState(
5411 Code* unoptimized_code,
5413 static const int kInstrSize = Assembler::kInstrSize;
5414 Address branch_address = pc - 8 * kInstrSize;
5415 Address pc_immediate_load_address = pc - 6 * kInstrSize;
5417 DCHECK(Assembler::IsBeq(Assembler::instr_at(pc - 7 * kInstrSize)));
5418 if (!Assembler::IsAddImmediate(Assembler::instr_at(branch_address))) {
5419 DCHECK(reinterpret_cast<uint64_t>(
5420 Assembler::target_address_at(pc_immediate_load_address)) ==
5421 reinterpret_cast<uint64_t>(
5422 isolate->builtins()->InterruptCheck()->entry()));
5426 DCHECK(Assembler::IsAddImmediate(Assembler::instr_at(branch_address)));
5428 if (reinterpret_cast<uint64_t>(
5429 Assembler::target_address_at(pc_immediate_load_address)) ==
5430 reinterpret_cast<uint64_t>(
5431 isolate->builtins()->OnStackReplacement()->entry())) {
5432 return ON_STACK_REPLACEMENT;
5435 DCHECK(reinterpret_cast<uint64_t>(
5436 Assembler::target_address_at(pc_immediate_load_address)) ==
5437 reinterpret_cast<uint64_t>(
5438 isolate->builtins()->OsrAfterStackCheck()->entry()));
5439 return OSR_AFTER_STACK_CHECK;
5443 } // namespace internal
5446 #endif // V8_TARGET_ARCH_MIPS64