1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #if V8_TARGET_ARCH_MIPS
9 // Note on Mips implementation:
11 // The result_register() for mips is the 'v0' register, which is defined
12 // by the ABI to contain function return values. However, the first
13 // parameter to a function is defined to be 'a0'. So there are many
14 // places where we have to move a previous result in v0 to a0 for the
15 // next call: mov(a0, v0). This is not needed on the other architectures.
17 #include "src/code-factory.h"
18 #include "src/code-stubs.h"
19 #include "src/codegen.h"
20 #include "src/compiler.h"
21 #include "src/debug.h"
22 #include "src/full-codegen.h"
23 #include "src/ic/ic.h"
24 #include "src/parser.h"
25 #include "src/scopes.h"
27 #include "src/mips/code-stubs-mips.h"
28 #include "src/mips/macro-assembler-mips.h"
33 #define __ ACCESS_MASM(masm_)
36 // A patch site is a location in the code which it is possible to patch. This
37 // class has a number of methods to emit the code which is patchable and the
38 // method EmitPatchInfo to record a marker back to the patchable code. This
39 // marker is a andi zero_reg, rx, #yyyy instruction, and rx * 0x0000ffff + yyyy
40 // (raw 16 bit immediate value is used) is the delta from the pc to the first
41 // instruction of the patchable code.
42 // The marker instruction is effectively a NOP (dest is zero_reg) and will
43 // never be emitted by normal code.
44 class JumpPatchSite BASE_EMBEDDED {
46 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
48 info_emitted_ = false;
53 DCHECK(patch_site_.is_bound() == info_emitted_);
56 // When initially emitting this ensure that a jump is always generated to skip
57 // the inlined smi code.
58 void EmitJumpIfNotSmi(Register reg, Label* target) {
59 DCHECK(!patch_site_.is_bound() && !info_emitted_);
60 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
61 __ bind(&patch_site_);
63 // Always taken before patched.
64 __ BranchShort(target, eq, at, Operand(zero_reg));
67 // When initially emitting this ensure that a jump is never generated to skip
68 // the inlined smi code.
69 void EmitJumpIfSmi(Register reg, Label* target) {
70 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
71 DCHECK(!patch_site_.is_bound() && !info_emitted_);
72 __ bind(&patch_site_);
74 // Never taken before patched.
75 __ BranchShort(target, ne, at, Operand(zero_reg));
78 void EmitPatchInfo() {
79 if (patch_site_.is_bound()) {
80 int delta_to_patch_site = masm_->InstructionsGeneratedSince(&patch_site_);
81 Register reg = Register::from_code(delta_to_patch_site / kImm16Mask);
82 __ andi(zero_reg, reg, delta_to_patch_site % kImm16Mask);
87 __ nop(); // Signals no inlined code.
92 MacroAssembler* masm_;
100 // Generate code for a JS function. On entry to the function the receiver
101 // and arguments have been pushed on the stack left to right. The actual
102 // argument count matches the formal parameter count expected by the
105 // The live registers are:
106 // o a1: the JS function object being called (i.e. ourselves)
108 // o fp: our caller's frame pointer
109 // o sp: stack pointer
110 // o ra: return address
112 // The function builds a JS frame. Please see JavaScriptFrameConstants in
113 // frames-mips.h for its layout.
114 void FullCodeGenerator::Generate() {
115 CompilationInfo* info = info_;
116 profiling_counter_ = isolate()->factory()->NewCell(
117 Handle<Smi>(Smi::FromInt(FLAG_interrupt_budget), isolate()));
118 SetFunctionPosition(function());
119 Comment cmnt(masm_, "[ function compiled by full code generator");
121 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
124 if (strlen(FLAG_stop_at) > 0 &&
125 info->function()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
130 // Sloppy mode functions and builtins need to replace the receiver with the
131 // global proxy when called as functions (without an explicit receiver
133 if (is_sloppy(info->language_mode()) && !info->is_native() &&
134 info->MayUseThis() && info->scope()->has_this_declaration()) {
136 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
137 __ lw(at, MemOperand(sp, receiver_offset));
138 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
139 __ Branch(&ok, ne, a2, Operand(at));
141 __ lw(a2, GlobalObjectOperand());
142 __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalProxyOffset));
144 __ sw(a2, MemOperand(sp, receiver_offset));
149 // Open a frame scope to indicate that there is a frame on the stack. The
150 // MANUAL indicates that the scope shouldn't actually generate code to set up
151 // the frame (that is done below).
152 FrameScope frame_scope(masm_, StackFrame::MANUAL);
154 info->set_prologue_offset(masm_->pc_offset());
155 __ Prologue(info->IsCodePreAgingActive());
156 info->AddNoFrameRange(0, masm_->pc_offset());
158 { Comment cmnt(masm_, "[ Allocate locals");
159 int locals_count = info->scope()->num_stack_slots();
160 // Generators allocate locals, if any, in context slots.
161 DCHECK(!IsGeneratorFunction(info->function()->kind()) || locals_count == 0);
162 if (locals_count > 0) {
163 if (locals_count >= 128) {
165 __ Subu(t5, sp, Operand(locals_count * kPointerSize));
166 __ LoadRoot(a2, Heap::kRealStackLimitRootIndex);
167 __ Branch(&ok, hs, t5, Operand(a2));
168 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
171 __ LoadRoot(t5, Heap::kUndefinedValueRootIndex);
172 int kMaxPushes = FLAG_optimize_for_size ? 4 : 32;
173 if (locals_count >= kMaxPushes) {
174 int loop_iterations = locals_count / kMaxPushes;
175 __ li(a2, Operand(loop_iterations));
177 __ bind(&loop_header);
179 __ Subu(sp, sp, Operand(kMaxPushes * kPointerSize));
180 for (int i = 0; i < kMaxPushes; i++) {
181 __ sw(t5, MemOperand(sp, i * kPointerSize));
183 // Continue loop if not done.
184 __ Subu(a2, a2, Operand(1));
185 __ Branch(&loop_header, ne, a2, Operand(zero_reg));
187 int remaining = locals_count % kMaxPushes;
188 // Emit the remaining pushes.
189 __ Subu(sp, sp, Operand(remaining * kPointerSize));
190 for (int i = 0; i < remaining; i++) {
191 __ sw(t5, MemOperand(sp, i * kPointerSize));
196 bool function_in_register = true;
198 // Possibly allocate a local context.
199 if (info->scope()->num_heap_slots() > 0) {
200 Comment cmnt(masm_, "[ Allocate context");
201 // Argument to NewContext is the function, which is still in a1.
202 bool need_write_barrier = true;
203 int slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
204 if (info->scope()->is_script_scope()) {
206 __ Push(info->scope()->GetScopeInfo(info->isolate()));
207 __ CallRuntime(Runtime::kNewScriptContext, 2);
208 } else if (slots <= FastNewContextStub::kMaximumSlots) {
209 FastNewContextStub stub(isolate(), slots);
211 // Result of FastNewContextStub is always in new space.
212 need_write_barrier = false;
215 __ CallRuntime(Runtime::kNewFunctionContext, 1);
217 function_in_register = false;
218 // Context is returned in v0. It replaces the context passed to us.
219 // It's saved in the stack and kept live in cp.
221 __ sw(v0, MemOperand(fp, StandardFrameConstants::kContextOffset));
222 // Copy any necessary parameters into the context.
223 int num_parameters = info->scope()->num_parameters();
224 int first_parameter = info->scope()->has_this_declaration() ? -1 : 0;
225 for (int i = first_parameter; i < num_parameters; i++) {
226 Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
227 if (var->IsContextSlot()) {
228 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
229 (num_parameters - 1 - i) * kPointerSize;
230 // Load parameter from stack.
231 __ lw(a0, MemOperand(fp, parameter_offset));
232 // Store it in the context.
233 MemOperand target = ContextOperand(cp, var->index());
236 // Update the write barrier.
237 if (need_write_barrier) {
238 __ RecordWriteContextSlot(
239 cp, target.offset(), a0, a3, kRAHasBeenSaved, kDontSaveFPRegs);
240 } else if (FLAG_debug_code) {
242 __ JumpIfInNewSpace(cp, a0, &done);
243 __ Abort(kExpectedNewSpaceObject);
250 // Possibly set up a local binding to the this function which is used in
251 // derived constructors with super calls.
252 Variable* this_function_var = scope()->this_function_var();
253 if (this_function_var != nullptr) {
254 Comment cmnt(masm_, "[ This function");
255 if (!function_in_register) {
256 __ lw(a1, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
257 // The write barrier clobbers register again, keep is marked as such.
259 SetVar(this_function_var, a1, a2, a3);
262 Variable* new_target_var = scope()->new_target_var();
263 if (new_target_var != nullptr) {
264 Comment cmnt(masm_, "[ new.target");
266 // Get the frame pointer for the calling frame.
267 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
269 // Skip the arguments adaptor frame if it exists.
270 Label check_frame_marker;
271 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
272 __ Branch(&check_frame_marker, ne, a1,
273 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
274 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
276 // Check the marker in the calling frame.
277 __ bind(&check_frame_marker);
278 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
280 Label non_construct_frame, done;
281 __ Branch(&non_construct_frame, ne, a1,
282 Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
285 MemOperand(a2, ConstructFrameConstants::kOriginalConstructorOffset));
288 __ bind(&non_construct_frame);
289 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
292 SetVar(new_target_var, v0, a2, a3);
295 // Possibly allocate RestParameters
297 Variable* rest_param = scope()->rest_parameter(&rest_index);
299 Comment cmnt(masm_, "[ Allocate rest parameter array");
301 int num_parameters = info->scope()->num_parameters();
302 int offset = num_parameters * kPointerSize;
305 Operand(StandardFrameConstants::kCallerSPOffset + offset));
306 __ li(a2, Operand(Smi::FromInt(num_parameters)));
307 __ li(a1, Operand(Smi::FromInt(rest_index)));
308 __ li(a0, Operand(Smi::FromInt(language_mode())));
309 __ Push(a3, a2, a1, a0);
311 RestParamAccessStub stub(isolate());
314 SetVar(rest_param, v0, a1, a2);
317 Variable* arguments = scope()->arguments();
318 if (arguments != NULL) {
319 // Function uses arguments object.
320 Comment cmnt(masm_, "[ Allocate arguments object");
321 if (!function_in_register) {
322 // Load this again, if it's used by the local context below.
323 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
327 // Receiver is just before the parameters on the caller's stack.
328 int num_parameters = info->scope()->num_parameters();
329 int offset = num_parameters * kPointerSize;
331 Operand(StandardFrameConstants::kCallerSPOffset + offset));
332 __ li(a1, Operand(Smi::FromInt(num_parameters)));
335 // Arguments to ArgumentsAccessStub:
336 // function, receiver address, parameter count.
337 // The stub will rewrite receiever and parameter count if the previous
338 // stack frame was an arguments adapter frame.
339 ArgumentsAccessStub::Type type;
340 if (is_strict(language_mode()) || !is_simple_parameter_list()) {
341 type = ArgumentsAccessStub::NEW_STRICT;
342 } else if (function()->has_duplicate_parameters()) {
343 type = ArgumentsAccessStub::NEW_SLOPPY_SLOW;
345 type = ArgumentsAccessStub::NEW_SLOPPY_FAST;
347 ArgumentsAccessStub stub(isolate(), type);
350 SetVar(arguments, v0, a1, a2);
354 __ CallRuntime(Runtime::kTraceEnter, 0);
357 // Visit the declarations and body unless there is an illegal
359 if (scope()->HasIllegalRedeclaration()) {
360 Comment cmnt(masm_, "[ Declarations");
361 scope()->VisitIllegalRedeclaration(this);
364 PrepareForBailoutForId(BailoutId::FunctionEntry(), NO_REGISTERS);
365 { Comment cmnt(masm_, "[ Declarations");
366 // For named function expressions, declare the function name as a
368 if (scope()->is_function_scope() && scope()->function() != NULL) {
369 VariableDeclaration* function = scope()->function();
370 DCHECK(function->proxy()->var()->mode() == CONST ||
371 function->proxy()->var()->mode() == CONST_LEGACY);
372 DCHECK(!function->proxy()->var()->IsUnallocatedOrGlobalSlot());
373 VisitVariableDeclaration(function);
375 VisitDeclarations(scope()->declarations());
378 { Comment cmnt(masm_, "[ Stack check");
379 PrepareForBailoutForId(BailoutId::Declarations(), NO_REGISTERS);
381 __ LoadRoot(at, Heap::kStackLimitRootIndex);
382 __ Branch(&ok, hs, sp, Operand(at));
383 Handle<Code> stack_check = isolate()->builtins()->StackCheck();
384 PredictableCodeSizeScope predictable(masm_,
385 masm_->CallSize(stack_check, RelocInfo::CODE_TARGET));
386 __ Call(stack_check, RelocInfo::CODE_TARGET);
390 { Comment cmnt(masm_, "[ Body");
391 DCHECK(loop_depth() == 0);
392 VisitStatements(function()->body());
393 DCHECK(loop_depth() == 0);
397 // Always emit a 'return undefined' in case control fell off the end of
399 { Comment cmnt(masm_, "[ return <undefined>;");
400 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
402 EmitReturnSequence();
406 void FullCodeGenerator::ClearAccumulator() {
407 DCHECK(Smi::FromInt(0) == 0);
408 __ mov(v0, zero_reg);
412 void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
413 __ li(a2, Operand(profiling_counter_));
414 __ lw(a3, FieldMemOperand(a2, Cell::kValueOffset));
415 __ Subu(a3, a3, Operand(Smi::FromInt(delta)));
416 __ sw(a3, FieldMemOperand(a2, Cell::kValueOffset));
420 void FullCodeGenerator::EmitProfilingCounterReset() {
421 int reset_value = FLAG_interrupt_budget;
422 if (info_->is_debug()) {
423 // Detect debug break requests as soon as possible.
424 reset_value = FLAG_interrupt_budget >> 4;
426 __ li(a2, Operand(profiling_counter_));
427 __ li(a3, Operand(Smi::FromInt(reset_value)));
428 __ sw(a3, FieldMemOperand(a2, Cell::kValueOffset));
432 void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
433 Label* back_edge_target) {
434 // The generated code is used in Deoptimizer::PatchStackCheckCodeAt so we need
435 // to make sure it is constant. Branch may emit a skip-or-jump sequence
436 // instead of the normal Branch. It seems that the "skip" part of that
437 // sequence is about as long as this Branch would be so it is safe to ignore
439 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
440 Comment cmnt(masm_, "[ Back edge bookkeeping");
442 DCHECK(back_edge_target->is_bound());
443 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
444 int weight = Min(kMaxBackEdgeWeight,
445 Max(1, distance / kCodeSizeMultiplier));
446 EmitProfilingCounterDecrement(weight);
447 __ slt(at, a3, zero_reg);
448 __ beq(at, zero_reg, &ok);
449 // Call will emit a li t9 first, so it is safe to use the delay slot.
450 __ Call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
451 // Record a mapping of this PC offset to the OSR id. This is used to find
452 // the AST id from the unoptimized code in order to use it as a key into
453 // the deoptimization input data found in the optimized code.
454 RecordBackEdge(stmt->OsrEntryId());
455 EmitProfilingCounterReset();
458 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
459 // Record a mapping of the OSR id to this PC. This is used if the OSR
460 // entry becomes the target of a bailout. We don't expect it to be, but
461 // we want it to work if it is.
462 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
466 void FullCodeGenerator::EmitReturnSequence() {
467 Comment cmnt(masm_, "[ Return sequence");
468 if (return_label_.is_bound()) {
469 __ Branch(&return_label_);
471 __ bind(&return_label_);
473 // Push the return value on the stack as the parameter.
474 // Runtime::TraceExit returns its parameter in v0.
476 __ CallRuntime(Runtime::kTraceExit, 1);
478 // Pretend that the exit is a backwards jump to the entry.
480 if (info_->ShouldSelfOptimize()) {
481 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
483 int distance = masm_->pc_offset();
484 weight = Min(kMaxBackEdgeWeight,
485 Max(1, distance / kCodeSizeMultiplier));
487 EmitProfilingCounterDecrement(weight);
489 __ Branch(&ok, ge, a3, Operand(zero_reg));
491 __ Call(isolate()->builtins()->InterruptCheck(),
492 RelocInfo::CODE_TARGET);
494 EmitProfilingCounterReset();
497 // Make sure that the constant pool is not emitted inside of the return
499 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
500 // Here we use masm_-> instead of the __ macro to avoid the code coverage
501 // tool from instrumenting as we rely on the code size here.
502 int32_t arg_count = info_->scope()->num_parameters() + 1;
503 int32_t sp_delta = arg_count * kPointerSize;
504 SetReturnPosition(function());
506 int no_frame_start = masm_->pc_offset();
507 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
508 masm_->Addu(sp, sp, Operand(sp_delta));
510 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
516 void FullCodeGenerator::EffectContext::Plug(Variable* var) const {
517 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
521 void FullCodeGenerator::AccumulatorValueContext::Plug(Variable* var) const {
522 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
523 codegen()->GetVar(result_register(), var);
527 void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
528 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
529 codegen()->GetVar(result_register(), var);
530 __ push(result_register());
534 void FullCodeGenerator::TestContext::Plug(Variable* var) const {
535 // For simplicity we always test the accumulator register.
536 codegen()->GetVar(result_register(), var);
537 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
538 codegen()->DoTest(this);
542 void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
546 void FullCodeGenerator::AccumulatorValueContext::Plug(
547 Heap::RootListIndex index) const {
548 __ LoadRoot(result_register(), index);
552 void FullCodeGenerator::StackValueContext::Plug(
553 Heap::RootListIndex index) const {
554 __ LoadRoot(result_register(), index);
555 __ push(result_register());
559 void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
560 codegen()->PrepareForBailoutBeforeSplit(condition(),
564 if (index == Heap::kUndefinedValueRootIndex ||
565 index == Heap::kNullValueRootIndex ||
566 index == Heap::kFalseValueRootIndex) {
567 if (false_label_ != fall_through_) __ Branch(false_label_);
568 } else if (index == Heap::kTrueValueRootIndex) {
569 if (true_label_ != fall_through_) __ Branch(true_label_);
571 __ LoadRoot(result_register(), index);
572 codegen()->DoTest(this);
577 void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
581 void FullCodeGenerator::AccumulatorValueContext::Plug(
582 Handle<Object> lit) const {
583 __ li(result_register(), Operand(lit));
587 void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
588 // Immediates cannot be pushed directly.
589 __ li(result_register(), Operand(lit));
590 __ push(result_register());
594 void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
595 codegen()->PrepareForBailoutBeforeSplit(condition(),
599 DCHECK(!lit->IsUndetectableObject()); // There are no undetectable literals.
600 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
601 if (false_label_ != fall_through_) __ Branch(false_label_);
602 } else if (lit->IsTrue() || lit->IsJSObject()) {
603 if (true_label_ != fall_through_) __ Branch(true_label_);
604 } else if (lit->IsString()) {
605 if (String::cast(*lit)->length() == 0) {
606 if (false_label_ != fall_through_) __ Branch(false_label_);
608 if (true_label_ != fall_through_) __ Branch(true_label_);
610 } else if (lit->IsSmi()) {
611 if (Smi::cast(*lit)->value() == 0) {
612 if (false_label_ != fall_through_) __ Branch(false_label_);
614 if (true_label_ != fall_through_) __ Branch(true_label_);
617 // For simplicity we always test the accumulator register.
618 __ li(result_register(), Operand(lit));
619 codegen()->DoTest(this);
624 void FullCodeGenerator::EffectContext::DropAndPlug(int count,
625 Register reg) const {
631 void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
633 Register reg) const {
636 __ Move(result_register(), reg);
640 void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
641 Register reg) const {
643 if (count > 1) __ Drop(count - 1);
644 __ sw(reg, MemOperand(sp, 0));
648 void FullCodeGenerator::TestContext::DropAndPlug(int count,
649 Register reg) const {
651 // For simplicity we always test the accumulator register.
653 __ Move(result_register(), reg);
654 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
655 codegen()->DoTest(this);
659 void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
660 Label* materialize_false) const {
661 DCHECK(materialize_true == materialize_false);
662 __ bind(materialize_true);
666 void FullCodeGenerator::AccumulatorValueContext::Plug(
667 Label* materialize_true,
668 Label* materialize_false) const {
670 __ bind(materialize_true);
671 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
673 __ bind(materialize_false);
674 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
679 void FullCodeGenerator::StackValueContext::Plug(
680 Label* materialize_true,
681 Label* materialize_false) const {
683 __ bind(materialize_true);
684 __ LoadRoot(at, Heap::kTrueValueRootIndex);
685 // Push the value as the following branch can clobber at in long branch mode.
688 __ bind(materialize_false);
689 __ LoadRoot(at, Heap::kFalseValueRootIndex);
695 void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
696 Label* materialize_false) const {
697 DCHECK(materialize_true == true_label_);
698 DCHECK(materialize_false == false_label_);
702 void FullCodeGenerator::EffectContext::Plug(bool flag) const {
706 void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
707 Heap::RootListIndex value_root_index =
708 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
709 __ LoadRoot(result_register(), value_root_index);
713 void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
714 Heap::RootListIndex value_root_index =
715 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
716 __ LoadRoot(at, value_root_index);
721 void FullCodeGenerator::TestContext::Plug(bool flag) const {
722 codegen()->PrepareForBailoutBeforeSplit(condition(),
727 if (true_label_ != fall_through_) __ Branch(true_label_);
729 if (false_label_ != fall_through_) __ Branch(false_label_);
734 void FullCodeGenerator::DoTest(Expression* condition,
737 Label* fall_through) {
738 __ mov(a0, result_register());
739 Handle<Code> ic = ToBooleanStub::GetUninitialized(isolate());
740 CallIC(ic, condition->test_id());
741 __ mov(at, zero_reg);
742 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
746 void FullCodeGenerator::Split(Condition cc,
751 Label* fall_through) {
752 if (if_false == fall_through) {
753 __ Branch(if_true, cc, lhs, rhs);
754 } else if (if_true == fall_through) {
755 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
757 __ Branch(if_true, cc, lhs, rhs);
763 MemOperand FullCodeGenerator::StackOperand(Variable* var) {
764 DCHECK(var->IsStackAllocated());
765 // Offset is negative because higher indexes are at lower addresses.
766 int offset = -var->index() * kPointerSize;
767 // Adjust by a (parameter or local) base offset.
768 if (var->IsParameter()) {
769 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
771 offset += JavaScriptFrameConstants::kLocal0Offset;
773 return MemOperand(fp, offset);
777 MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
778 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
779 if (var->IsContextSlot()) {
780 int context_chain_length = scope()->ContextChainLength(var->scope());
781 __ LoadContext(scratch, context_chain_length);
782 return ContextOperand(scratch, var->index());
784 return StackOperand(var);
789 void FullCodeGenerator::GetVar(Register dest, Variable* var) {
790 // Use destination as scratch.
791 MemOperand location = VarOperand(var, dest);
792 __ lw(dest, location);
796 void FullCodeGenerator::SetVar(Variable* var,
800 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
801 DCHECK(!scratch0.is(src));
802 DCHECK(!scratch0.is(scratch1));
803 DCHECK(!scratch1.is(src));
804 MemOperand location = VarOperand(var, scratch0);
805 __ sw(src, location);
806 // Emit the write barrier code if the location is in the heap.
807 if (var->IsContextSlot()) {
808 __ RecordWriteContextSlot(scratch0,
818 void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
819 bool should_normalize,
822 // Only prepare for bailouts before splits if we're in a test
823 // context. Otherwise, we let the Visit function deal with the
824 // preparation to avoid preparing with the same AST id twice.
825 if (!context()->IsTest() || !info_->IsOptimizable()) return;
828 if (should_normalize) __ Branch(&skip);
829 PrepareForBailout(expr, TOS_REG);
830 if (should_normalize) {
831 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
832 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
838 void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
839 // The variable in the declaration always resides in the current function
841 DCHECK_EQ(0, scope()->ContextChainLength(variable->scope()));
842 if (generate_debug_code_) {
843 // Check that we're not inside a with or catch context.
844 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
845 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
846 __ Check(ne, kDeclarationInWithContext,
848 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
849 __ Check(ne, kDeclarationInCatchContext,
855 void FullCodeGenerator::VisitVariableDeclaration(
856 VariableDeclaration* declaration) {
857 // If it was not possible to allocate the variable at compile time, we
858 // need to "declare" it at runtime to make sure it actually exists in the
860 VariableProxy* proxy = declaration->proxy();
861 VariableMode mode = declaration->mode();
862 Variable* variable = proxy->var();
863 bool hole_init = mode == LET || mode == CONST || mode == CONST_LEGACY;
864 switch (variable->location()) {
865 case VariableLocation::GLOBAL:
866 case VariableLocation::UNALLOCATED:
867 globals_->Add(variable->name(), zone());
868 globals_->Add(variable->binding_needs_init()
869 ? isolate()->factory()->the_hole_value()
870 : isolate()->factory()->undefined_value(),
874 case VariableLocation::PARAMETER:
875 case VariableLocation::LOCAL:
877 Comment cmnt(masm_, "[ VariableDeclaration");
878 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
879 __ sw(t0, StackOperand(variable));
883 case VariableLocation::CONTEXT:
885 Comment cmnt(masm_, "[ VariableDeclaration");
886 EmitDebugCheckDeclarationContext(variable);
887 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
888 __ sw(at, ContextOperand(cp, variable->index()));
889 // No write barrier since the_hole_value is in old space.
890 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
894 case VariableLocation::LOOKUP: {
895 Comment cmnt(masm_, "[ VariableDeclaration");
896 __ li(a2, Operand(variable->name()));
897 // Declaration nodes are always introduced in one of four modes.
898 DCHECK(IsDeclaredVariableMode(mode));
899 PropertyAttributes attr =
900 IsImmutableVariableMode(mode) ? READ_ONLY : NONE;
901 __ li(a1, Operand(Smi::FromInt(attr)));
902 // Push initial value, if any.
903 // Note: For variables we must not push an initial value (such as
904 // 'undefined') because we may have a (legal) redeclaration and we
905 // must not destroy the current value.
907 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
908 __ Push(cp, a2, a1, a0);
910 DCHECK(Smi::FromInt(0) == 0);
911 __ mov(a0, zero_reg); // Smi::FromInt(0) indicates no initial value.
912 __ Push(cp, a2, a1, a0);
914 __ CallRuntime(Runtime::kDeclareLookupSlot, 4);
921 void FullCodeGenerator::VisitFunctionDeclaration(
922 FunctionDeclaration* declaration) {
923 VariableProxy* proxy = declaration->proxy();
924 Variable* variable = proxy->var();
925 switch (variable->location()) {
926 case VariableLocation::GLOBAL:
927 case VariableLocation::UNALLOCATED: {
928 globals_->Add(variable->name(), zone());
929 Handle<SharedFunctionInfo> function =
930 Compiler::GetSharedFunctionInfo(declaration->fun(), script(), info_);
931 // Check for stack-overflow exception.
932 if (function.is_null()) return SetStackOverflow();
933 globals_->Add(function, zone());
937 case VariableLocation::PARAMETER:
938 case VariableLocation::LOCAL: {
939 Comment cmnt(masm_, "[ FunctionDeclaration");
940 VisitForAccumulatorValue(declaration->fun());
941 __ sw(result_register(), StackOperand(variable));
945 case VariableLocation::CONTEXT: {
946 Comment cmnt(masm_, "[ FunctionDeclaration");
947 EmitDebugCheckDeclarationContext(variable);
948 VisitForAccumulatorValue(declaration->fun());
949 __ sw(result_register(), ContextOperand(cp, variable->index()));
950 int offset = Context::SlotOffset(variable->index());
951 // We know that we have written a function, which is not a smi.
952 __ RecordWriteContextSlot(cp,
960 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
964 case VariableLocation::LOOKUP: {
965 Comment cmnt(masm_, "[ FunctionDeclaration");
966 __ li(a2, Operand(variable->name()));
967 __ li(a1, Operand(Smi::FromInt(NONE)));
969 // Push initial value for function declaration.
970 VisitForStackValue(declaration->fun());
971 __ CallRuntime(Runtime::kDeclareLookupSlot, 4);
978 void FullCodeGenerator::VisitImportDeclaration(ImportDeclaration* declaration) {
979 VariableProxy* proxy = declaration->proxy();
980 Variable* variable = proxy->var();
981 switch (variable->location()) {
982 case VariableLocation::GLOBAL:
983 case VariableLocation::UNALLOCATED:
987 case VariableLocation::CONTEXT: {
988 Comment cmnt(masm_, "[ ImportDeclaration");
989 EmitDebugCheckDeclarationContext(variable);
994 case VariableLocation::PARAMETER:
995 case VariableLocation::LOCAL:
996 case VariableLocation::LOOKUP:
1002 void FullCodeGenerator::VisitExportDeclaration(ExportDeclaration* declaration) {
1007 void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
1008 // Call the runtime to declare the globals.
1009 // The context is the first argument.
1010 __ li(a1, Operand(pairs));
1011 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
1012 __ Push(cp, a1, a0);
1013 __ CallRuntime(Runtime::kDeclareGlobals, 3);
1014 // Return value is ignored.
1018 void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
1019 // Call the runtime to declare the modules.
1020 __ Push(descriptions);
1021 __ CallRuntime(Runtime::kDeclareModules, 1);
1022 // Return value is ignored.
1026 void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
1027 Comment cmnt(masm_, "[ SwitchStatement");
1028 Breakable nested_statement(this, stmt);
1029 SetStatementPosition(stmt);
1031 // Keep the switch value on the stack until a case matches.
1032 VisitForStackValue(stmt->tag());
1033 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
1035 ZoneList<CaseClause*>* clauses = stmt->cases();
1036 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
1038 Label next_test; // Recycled for each test.
1039 // Compile all the tests with branches to their bodies.
1040 for (int i = 0; i < clauses->length(); i++) {
1041 CaseClause* clause = clauses->at(i);
1042 clause->body_target()->Unuse();
1044 // The default is not a test, but remember it as final fall through.
1045 if (clause->is_default()) {
1046 default_clause = clause;
1050 Comment cmnt(masm_, "[ Case comparison");
1051 __ bind(&next_test);
1054 // Compile the label expression.
1055 VisitForAccumulatorValue(clause->label());
1056 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
1058 // Perform the comparison as if via '==='.
1059 __ lw(a1, MemOperand(sp, 0)); // Switch value.
1060 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
1061 JumpPatchSite patch_site(masm_);
1062 if (inline_smi_code) {
1065 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
1067 __ Branch(&next_test, ne, a1, Operand(a0));
1068 __ Drop(1); // Switch value is no longer needed.
1069 __ Branch(clause->body_target());
1071 __ bind(&slow_case);
1074 // Record position before stub call for type feedback.
1075 SetExpressionPosition(clause);
1076 Handle<Code> ic = CodeFactory::CompareIC(isolate(), Token::EQ_STRICT,
1077 strength(language_mode())).code();
1078 CallIC(ic, clause->CompareId());
1079 patch_site.EmitPatchInfo();
1083 PrepareForBailout(clause, TOS_REG);
1084 __ LoadRoot(at, Heap::kTrueValueRootIndex);
1085 __ Branch(&next_test, ne, v0, Operand(at));
1087 __ Branch(clause->body_target());
1090 __ Branch(&next_test, ne, v0, Operand(zero_reg));
1091 __ Drop(1); // Switch value is no longer needed.
1092 __ Branch(clause->body_target());
1095 // Discard the test value and jump to the default if present, otherwise to
1096 // the end of the statement.
1097 __ bind(&next_test);
1098 __ Drop(1); // Switch value is no longer needed.
1099 if (default_clause == NULL) {
1100 __ Branch(nested_statement.break_label());
1102 __ Branch(default_clause->body_target());
1105 // Compile all the case bodies.
1106 for (int i = 0; i < clauses->length(); i++) {
1107 Comment cmnt(masm_, "[ Case body");
1108 CaseClause* clause = clauses->at(i);
1109 __ bind(clause->body_target());
1110 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
1111 VisitStatements(clause->statements());
1114 __ bind(nested_statement.break_label());
1115 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1119 void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
1120 Comment cmnt(masm_, "[ ForInStatement");
1121 SetStatementPosition(stmt, SKIP_BREAK);
1123 FeedbackVectorSlot slot = stmt->ForInFeedbackSlot();
1126 ForIn loop_statement(this, stmt);
1127 increment_loop_depth();
1129 // Get the object to enumerate over. If the object is null or undefined, skip
1130 // over the loop. See ECMA-262 version 5, section 12.6.4.
1131 SetExpressionAsStatementPosition(stmt->enumerable());
1132 VisitForAccumulatorValue(stmt->enumerable());
1133 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
1134 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1135 __ Branch(&exit, eq, a0, Operand(at));
1136 Register null_value = t1;
1137 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
1138 __ Branch(&exit, eq, a0, Operand(null_value));
1139 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1141 // Convert the object to a JS object.
1142 Label convert, done_convert;
1143 __ JumpIfSmi(a0, &convert);
1144 __ GetObjectType(a0, a1, a1);
1145 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
1148 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1150 __ bind(&done_convert);
1151 PrepareForBailoutForId(stmt->ToObjectId(), TOS_REG);
1154 // Check for proxies.
1156 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1157 __ GetObjectType(a0, a1, a1);
1158 __ Branch(&call_runtime, le, a1, Operand(LAST_JS_PROXY_TYPE));
1160 // Check cache validity in generated code. This is a fast case for
1161 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1162 // guarantee cache validity, call the runtime system to check cache
1163 // validity or get the property names in a fixed array.
1164 __ CheckEnumCache(null_value, &call_runtime);
1166 // The enum cache is valid. Load the map of the object being
1167 // iterated over and use the cache for the iteration.
1169 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
1170 __ Branch(&use_cache);
1172 // Get the set of properties to enumerate.
1173 __ bind(&call_runtime);
1174 __ push(a0); // Duplicate the enumerable object on the stack.
1175 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1176 PrepareForBailoutForId(stmt->EnumId(), TOS_REG);
1178 // If we got a map from the runtime call, we can do a fast
1179 // modification check. Otherwise, we got a fixed array, and we have
1180 // to do a slow check.
1182 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
1183 __ LoadRoot(at, Heap::kMetaMapRootIndex);
1184 __ Branch(&fixed_array, ne, a2, Operand(at));
1186 // We got a map in register v0. Get the enumeration cache from it.
1187 Label no_descriptors;
1188 __ bind(&use_cache);
1190 __ EnumLength(a1, v0);
1191 __ Branch(&no_descriptors, eq, a1, Operand(Smi::FromInt(0)));
1193 __ LoadInstanceDescriptors(v0, a2);
1194 __ lw(a2, FieldMemOperand(a2, DescriptorArray::kEnumCacheOffset));
1195 __ lw(a2, FieldMemOperand(a2, DescriptorArray::kEnumCacheBridgeCacheOffset));
1197 // Set up the four remaining stack slots.
1198 __ li(a0, Operand(Smi::FromInt(0)));
1199 // Push map, enumeration cache, enumeration cache length (as smi) and zero.
1200 __ Push(v0, a2, a1, a0);
1203 __ bind(&no_descriptors);
1207 // We got a fixed array in register v0. Iterate through that.
1209 __ bind(&fixed_array);
1211 __ li(a1, FeedbackVector());
1212 __ li(a2, Operand(TypeFeedbackVector::MegamorphicSentinel(isolate())));
1213 int vector_index = FeedbackVector()->GetIndex(slot);
1214 __ sw(a2, FieldMemOperand(a1, FixedArray::OffsetOfElementAt(vector_index)));
1216 __ li(a1, Operand(Smi::FromInt(1))); // Smi indicates slow check
1217 __ lw(a2, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1218 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1219 __ GetObjectType(a2, a3, a3);
1220 __ Branch(&non_proxy, gt, a3, Operand(LAST_JS_PROXY_TYPE));
1221 __ li(a1, Operand(Smi::FromInt(0))); // Zero indicates proxy
1222 __ bind(&non_proxy);
1223 __ Push(a1, v0); // Smi and array
1224 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1225 __ li(a0, Operand(Smi::FromInt(0)));
1226 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1228 // Generate code for doing the condition check.
1229 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
1231 SetExpressionAsStatementPosition(stmt->each());
1233 // Load the current count to a0, load the length to a1.
1234 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1235 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
1236 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
1238 // Get the current entry of the array into register a3.
1239 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1240 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1241 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1242 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1243 __ lw(a3, MemOperand(t0)); // Current entry.
1245 // Get the expected map from the stack or a smi in the
1246 // permanent slow case into register a2.
1247 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1249 // Check if the expected map still matches that of the enumerable.
1250 // If not, we may have to filter the key.
1252 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1253 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1254 __ Branch(&update_each, eq, t0, Operand(a2));
1256 // For proxies, no filtering is done.
1257 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1258 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
1259 __ Branch(&update_each, eq, a2, Operand(zero_reg));
1261 // Convert the entry to a string or (smi) 0 if it isn't a property
1262 // any more. If the property has been removed while iterating, we
1264 __ Push(a1, a3); // Enumerable and current entry.
1265 __ CallRuntime(Runtime::kForInFilter, 2);
1266 PrepareForBailoutForId(stmt->FilterId(), TOS_REG);
1267 __ mov(a3, result_register());
1268 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1269 __ Branch(loop_statement.continue_label(), eq, a3, Operand(at));
1271 // Update the 'each' property or variable from the possibly filtered
1272 // entry in register a3.
1273 __ bind(&update_each);
1274 __ mov(result_register(), a3);
1275 // Perform the assignment as if via '='.
1276 { EffectContext context(this);
1277 EmitAssignment(stmt->each(), stmt->EachFeedbackSlot());
1278 PrepareForBailoutForId(stmt->AssignmentId(), NO_REGISTERS);
1281 // Generate code for the body of the loop.
1282 Visit(stmt->body());
1284 // Generate code for the going to the next element by incrementing
1285 // the index (smi) stored on top of the stack.
1286 __ bind(loop_statement.continue_label());
1288 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1291 EmitBackEdgeBookkeeping(stmt, &loop);
1294 // Remove the pointers stored on the stack.
1295 __ bind(loop_statement.break_label());
1298 // Exit and decrement the loop depth.
1299 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1301 decrement_loop_depth();
1305 void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1307 // Use the fast case closure allocation code that allocates in new
1308 // space for nested functions that don't need literals cloning. If
1309 // we're running with the --always-opt or the --prepare-always-opt
1310 // flag, we need to use the runtime function so that the new function
1311 // we are creating here gets a chance to have its code optimized and
1312 // doesn't just get a copy of the existing unoptimized code.
1313 if (!FLAG_always_opt &&
1314 !FLAG_prepare_always_opt &&
1316 scope()->is_function_scope() &&
1317 info->num_literals() == 0) {
1318 FastNewClosureStub stub(isolate(), info->language_mode(), info->kind());
1319 __ li(a2, Operand(info));
1322 __ li(a0, Operand(info));
1323 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1324 : Heap::kFalseValueRootIndex);
1325 __ Push(cp, a0, a1);
1326 __ CallRuntime(Runtime::kNewClosure, 3);
1328 context()->Plug(v0);
1332 void FullCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
1333 Comment cmnt(masm_, "[ VariableProxy");
1334 EmitVariableLoad(expr);
1338 void FullCodeGenerator::EmitSetHomeObjectIfNeeded(Expression* initializer,
1340 FeedbackVectorICSlot slot) {
1341 if (NeedsHomeObject(initializer)) {
1342 __ lw(StoreDescriptor::ReceiverRegister(), MemOperand(sp));
1343 __ li(StoreDescriptor::NameRegister(),
1344 Operand(isolate()->factory()->home_object_symbol()));
1345 __ lw(StoreDescriptor::ValueRegister(),
1346 MemOperand(sp, offset * kPointerSize));
1347 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
1353 void FullCodeGenerator::EmitLoadGlobalCheckExtensions(VariableProxy* proxy,
1354 TypeofMode typeof_mode,
1356 Register current = cp;
1362 if (s->num_heap_slots() > 0) {
1363 if (s->calls_sloppy_eval()) {
1364 // Check that extension is NULL.
1365 __ lw(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1366 __ Branch(slow, ne, temp, Operand(zero_reg));
1368 // Load next context in chain.
1369 __ lw(next, ContextOperand(current, Context::PREVIOUS_INDEX));
1370 // Walk the rest of the chain without clobbering cp.
1373 // If no outer scope calls eval, we do not need to check more
1374 // context extensions.
1375 if (!s->outer_scope_calls_sloppy_eval() || s->is_eval_scope()) break;
1376 s = s->outer_scope();
1379 if (s->is_eval_scope()) {
1381 if (!current.is(next)) {
1382 __ Move(next, current);
1385 // Terminate at native context.
1386 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1387 __ LoadRoot(t0, Heap::kNativeContextMapRootIndex);
1388 __ Branch(&fast, eq, temp, Operand(t0));
1389 // Check that extension is NULL.
1390 __ lw(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1391 __ Branch(slow, ne, temp, Operand(zero_reg));
1392 // Load next context in chain.
1393 __ lw(next, ContextOperand(next, Context::PREVIOUS_INDEX));
1398 // All extension objects were empty and it is safe to use a normal global
1400 EmitGlobalVariableLoad(proxy, typeof_mode);
1404 MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1406 DCHECK(var->IsContextSlot());
1407 Register context = cp;
1411 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
1412 if (s->num_heap_slots() > 0) {
1413 if (s->calls_sloppy_eval()) {
1414 // Check that extension is NULL.
1415 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1416 __ Branch(slow, ne, temp, Operand(zero_reg));
1418 __ lw(next, ContextOperand(context, Context::PREVIOUS_INDEX));
1419 // Walk the rest of the chain without clobbering cp.
1423 // Check that last extension is NULL.
1424 __ lw(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1425 __ Branch(slow, ne, temp, Operand(zero_reg));
1427 // This function is used only for loads, not stores, so it's safe to
1428 // return an cp-based operand (the write barrier cannot be allowed to
1429 // destroy the cp register).
1430 return ContextOperand(context, var->index());
1434 void FullCodeGenerator::EmitDynamicLookupFastCase(VariableProxy* proxy,
1435 TypeofMode typeof_mode,
1436 Label* slow, Label* done) {
1437 // Generate fast-case code for variables that might be shadowed by
1438 // eval-introduced variables. Eval is used a lot without
1439 // introducing variables. In those cases, we do not want to
1440 // perform a runtime call for all variables in the scope
1441 // containing the eval.
1442 Variable* var = proxy->var();
1443 if (var->mode() == DYNAMIC_GLOBAL) {
1444 EmitLoadGlobalCheckExtensions(proxy, typeof_mode, slow);
1446 } else if (var->mode() == DYNAMIC_LOCAL) {
1447 Variable* local = var->local_if_not_shadowed();
1448 __ lw(v0, ContextSlotOperandCheckExtensions(local, slow));
1449 if (local->mode() == LET || local->mode() == CONST ||
1450 local->mode() == CONST_LEGACY) {
1451 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1452 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1453 if (local->mode() == CONST_LEGACY) {
1454 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1455 __ Movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
1456 } else { // LET || CONST
1457 __ Branch(done, ne, at, Operand(zero_reg));
1458 __ li(a0, Operand(var->name()));
1460 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1468 void FullCodeGenerator::EmitGlobalVariableLoad(VariableProxy* proxy,
1469 TypeofMode typeof_mode) {
1470 Variable* var = proxy->var();
1471 DCHECK(var->IsUnallocatedOrGlobalSlot() ||
1472 (var->IsLookupSlot() && var->mode() == DYNAMIC_GLOBAL));
1473 if (var->IsGlobalSlot()) {
1474 DCHECK(var->index() > 0);
1475 DCHECK(var->IsStaticGlobalObjectProperty());
1476 // Each var occupies two slots in the context: for reads and writes.
1477 int slot_index = var->index();
1478 int depth = scope()->ContextChainLength(var->scope());
1479 __ li(LoadGlobalViaContextDescriptor::DepthRegister(),
1480 Operand(Smi::FromInt(depth)));
1481 __ li(LoadGlobalViaContextDescriptor::SlotRegister(),
1482 Operand(Smi::FromInt(slot_index)));
1483 __ li(LoadGlobalViaContextDescriptor::NameRegister(), Operand(var->name()));
1484 LoadGlobalViaContextStub stub(isolate(), depth);
1488 __ lw(LoadDescriptor::ReceiverRegister(), GlobalObjectOperand());
1489 __ li(LoadDescriptor::NameRegister(), Operand(var->name()));
1490 __ li(LoadDescriptor::SlotRegister(),
1491 Operand(SmiFromSlot(proxy->VariableFeedbackSlot())));
1492 CallLoadIC(typeof_mode);
1497 void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy,
1498 TypeofMode typeof_mode) {
1499 // Record position before possible IC call.
1500 SetExpressionPosition(proxy);
1501 PrepareForBailoutForId(proxy->BeforeId(), NO_REGISTERS);
1502 Variable* var = proxy->var();
1504 // Three cases: global variables, lookup variables, and all other types of
1506 switch (var->location()) {
1507 case VariableLocation::GLOBAL:
1508 case VariableLocation::UNALLOCATED: {
1509 Comment cmnt(masm_, "[ Global variable");
1510 EmitGlobalVariableLoad(proxy, typeof_mode);
1511 context()->Plug(v0);
1515 case VariableLocation::PARAMETER:
1516 case VariableLocation::LOCAL:
1517 case VariableLocation::CONTEXT: {
1518 DCHECK_EQ(NOT_INSIDE_TYPEOF, typeof_mode);
1519 Comment cmnt(masm_, var->IsContextSlot() ? "[ Context variable"
1520 : "[ Stack variable");
1521 if (var->binding_needs_init()) {
1522 // var->scope() may be NULL when the proxy is located in eval code and
1523 // refers to a potential outside binding. Currently those bindings are
1524 // always looked up dynamically, i.e. in that case
1525 // var->location() == LOOKUP.
1527 DCHECK(var->scope() != NULL);
1529 // Check if the binding really needs an initialization check. The check
1530 // can be skipped in the following situation: we have a LET or CONST
1531 // binding in harmony mode, both the Variable and the VariableProxy have
1532 // the same declaration scope (i.e. they are both in global code, in the
1533 // same function or in the same eval code) and the VariableProxy is in
1534 // the source physically located after the initializer of the variable.
1536 // We cannot skip any initialization checks for CONST in non-harmony
1537 // mode because const variables may be declared but never initialized:
1538 // if (false) { const x; }; var y = x;
1540 // The condition on the declaration scopes is a conservative check for
1541 // nested functions that access a binding and are called before the
1542 // binding is initialized:
1543 // function() { f(); let x = 1; function f() { x = 2; } }
1545 bool skip_init_check;
1546 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1547 skip_init_check = false;
1548 } else if (var->is_this()) {
1549 CHECK(info_->function() != nullptr &&
1550 (info_->function()->kind() & kSubclassConstructor) != 0);
1551 // TODO(dslomov): implement 'this' hole check elimination.
1552 skip_init_check = false;
1554 // Check that we always have valid source position.
1555 DCHECK(var->initializer_position() != RelocInfo::kNoPosition);
1556 DCHECK(proxy->position() != RelocInfo::kNoPosition);
1557 skip_init_check = var->mode() != CONST_LEGACY &&
1558 var->initializer_position() < proxy->position();
1561 if (!skip_init_check) {
1562 // Let and const need a read barrier.
1564 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1565 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1566 if (var->mode() == LET || var->mode() == CONST) {
1567 // Throw a reference error when using an uninitialized let/const
1568 // binding in harmony mode.
1570 __ Branch(&done, ne, at, Operand(zero_reg));
1571 __ li(a0, Operand(var->name()));
1573 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1576 // Uninitalized const bindings outside of harmony mode are unholed.
1577 DCHECK(var->mode() == CONST_LEGACY);
1578 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1579 __ Movz(v0, a0, at); // Conditional move: Undefined if TheHole.
1581 context()->Plug(v0);
1585 context()->Plug(var);
1589 case VariableLocation::LOOKUP: {
1590 Comment cmnt(masm_, "[ Lookup variable");
1592 // Generate code for loading from variables potentially shadowed
1593 // by eval-introduced variables.
1594 EmitDynamicLookupFastCase(proxy, typeof_mode, &slow, &done);
1596 __ li(a1, Operand(var->name()));
1597 __ Push(cp, a1); // Context and name.
1598 Runtime::FunctionId function_id =
1599 typeof_mode == NOT_INSIDE_TYPEOF
1600 ? Runtime::kLoadLookupSlot
1601 : Runtime::kLoadLookupSlotNoReferenceError;
1602 __ CallRuntime(function_id, 2);
1604 context()->Plug(v0);
1610 void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1611 Comment cmnt(masm_, "[ RegExpLiteral");
1613 // Registers will be used as follows:
1614 // t1 = materialized value (RegExp literal)
1615 // t0 = JS function, literals array
1616 // a3 = literal index
1617 // a2 = RegExp pattern
1618 // a1 = RegExp flags
1619 // a0 = RegExp literal clone
1620 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1621 __ lw(t0, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1622 int literal_offset =
1623 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1624 __ lw(t1, FieldMemOperand(t0, literal_offset));
1625 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1626 __ Branch(&materialized, ne, t1, Operand(at));
1628 // Create regexp literal using runtime function.
1629 // Result will be in v0.
1630 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1631 __ li(a2, Operand(expr->pattern()));
1632 __ li(a1, Operand(expr->flags()));
1633 __ Push(t0, a3, a2, a1);
1634 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1637 __ bind(&materialized);
1638 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1639 Label allocated, runtime_allocate;
1640 __ Allocate(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1643 __ bind(&runtime_allocate);
1644 __ li(a0, Operand(Smi::FromInt(size)));
1646 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1649 __ bind(&allocated);
1651 // After this, registers are used as follows:
1652 // v0: Newly allocated regexp.
1653 // t1: Materialized regexp.
1655 __ CopyFields(v0, t1, a2.bit(), size / kPointerSize);
1656 context()->Plug(v0);
1660 void FullCodeGenerator::EmitAccessor(Expression* expression) {
1661 if (expression == NULL) {
1662 __ LoadRoot(a1, Heap::kNullValueRootIndex);
1665 VisitForStackValue(expression);
1670 void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1671 Comment cmnt(masm_, "[ ObjectLiteral");
1673 Handle<FixedArray> constant_properties = expr->constant_properties();
1674 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1675 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1676 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1677 __ li(a1, Operand(constant_properties));
1678 __ li(a0, Operand(Smi::FromInt(expr->ComputeFlags())));
1679 if (MustCreateObjectLiteralWithRuntime(expr)) {
1680 __ Push(a3, a2, a1, a0);
1681 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1683 FastCloneShallowObjectStub stub(isolate(), expr->properties_count());
1686 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1688 // If result_saved is true the result is on top of the stack. If
1689 // result_saved is false the result is in v0.
1690 bool result_saved = false;
1692 AccessorTable accessor_table(zone());
1693 int property_index = 0;
1694 // store_slot_index points to the vector IC slot for the next store IC used.
1695 // ObjectLiteral::ComputeFeedbackRequirements controls the allocation of slots
1696 // and must be updated if the number of store ICs emitted here changes.
1697 int store_slot_index = 0;
1698 for (; property_index < expr->properties()->length(); property_index++) {
1699 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1700 if (property->is_computed_name()) break;
1701 if (property->IsCompileTimeValue()) continue;
1703 Literal* key = property->key()->AsLiteral();
1704 Expression* value = property->value();
1705 if (!result_saved) {
1706 __ push(v0); // Save result on stack.
1707 result_saved = true;
1709 switch (property->kind()) {
1710 case ObjectLiteral::Property::CONSTANT:
1712 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1713 DCHECK(!CompileTimeValue::IsCompileTimeValue(property->value()));
1715 case ObjectLiteral::Property::COMPUTED:
1716 // It is safe to use [[Put]] here because the boilerplate already
1717 // contains computed properties with an uninitialized value.
1718 if (key->value()->IsInternalizedString()) {
1719 if (property->emit_store()) {
1720 VisitForAccumulatorValue(value);
1721 __ mov(StoreDescriptor::ValueRegister(), result_register());
1722 DCHECK(StoreDescriptor::ValueRegister().is(a0));
1723 __ li(StoreDescriptor::NameRegister(), Operand(key->value()));
1724 __ lw(StoreDescriptor::ReceiverRegister(), MemOperand(sp));
1725 if (FLAG_vector_stores) {
1726 EmitLoadStoreICSlot(expr->GetNthSlot(store_slot_index++));
1729 CallStoreIC(key->LiteralFeedbackId());
1731 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1733 if (NeedsHomeObject(value)) {
1734 __ Move(StoreDescriptor::ReceiverRegister(), v0);
1735 __ li(StoreDescriptor::NameRegister(),
1736 Operand(isolate()->factory()->home_object_symbol()));
1737 __ lw(StoreDescriptor::ValueRegister(), MemOperand(sp));
1738 if (FLAG_vector_stores) {
1739 EmitLoadStoreICSlot(expr->GetNthSlot(store_slot_index++));
1744 VisitForEffect(value);
1748 // Duplicate receiver on stack.
1749 __ lw(a0, MemOperand(sp));
1751 VisitForStackValue(key);
1752 VisitForStackValue(value);
1753 if (property->emit_store()) {
1754 EmitSetHomeObjectIfNeeded(
1755 value, 2, expr->SlotForHomeObject(value, &store_slot_index));
1756 __ li(a0, Operand(Smi::FromInt(SLOPPY))); // PropertyAttributes.
1758 __ CallRuntime(Runtime::kSetProperty, 4);
1763 case ObjectLiteral::Property::PROTOTYPE:
1764 // Duplicate receiver on stack.
1765 __ lw(a0, MemOperand(sp));
1767 VisitForStackValue(value);
1768 DCHECK(property->emit_store());
1769 __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1771 case ObjectLiteral::Property::GETTER:
1772 if (property->emit_store()) {
1773 accessor_table.lookup(key)->second->getter = value;
1776 case ObjectLiteral::Property::SETTER:
1777 if (property->emit_store()) {
1778 accessor_table.lookup(key)->second->setter = value;
1784 // Emit code to define accessors, using only a single call to the runtime for
1785 // each pair of corresponding getters and setters.
1786 for (AccessorTable::Iterator it = accessor_table.begin();
1787 it != accessor_table.end();
1789 __ lw(a0, MemOperand(sp)); // Duplicate receiver.
1791 VisitForStackValue(it->first);
1792 EmitAccessor(it->second->getter);
1793 EmitSetHomeObjectIfNeeded(
1794 it->second->getter, 2,
1795 expr->SlotForHomeObject(it->second->getter, &store_slot_index));
1796 EmitAccessor(it->second->setter);
1797 EmitSetHomeObjectIfNeeded(
1798 it->second->setter, 3,
1799 expr->SlotForHomeObject(it->second->setter, &store_slot_index));
1800 __ li(a0, Operand(Smi::FromInt(NONE)));
1802 __ CallRuntime(Runtime::kDefineAccessorPropertyUnchecked, 5);
1805 // Object literals have two parts. The "static" part on the left contains no
1806 // computed property names, and so we can compute its map ahead of time; see
1807 // runtime.cc::CreateObjectLiteralBoilerplate. The second "dynamic" part
1808 // starts with the first computed property name, and continues with all
1809 // properties to its right. All the code from above initializes the static
1810 // component of the object literal, and arranges for the map of the result to
1811 // reflect the static order in which the keys appear. For the dynamic
1812 // properties, we compile them into a series of "SetOwnProperty" runtime
1813 // calls. This will preserve insertion order.
1814 for (; property_index < expr->properties()->length(); property_index++) {
1815 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1817 Expression* value = property->value();
1818 if (!result_saved) {
1819 __ push(v0); // Save result on the stack
1820 result_saved = true;
1823 __ lw(a0, MemOperand(sp)); // Duplicate receiver.
1826 if (property->kind() == ObjectLiteral::Property::PROTOTYPE) {
1827 DCHECK(!property->is_computed_name());
1828 VisitForStackValue(value);
1829 DCHECK(property->emit_store());
1830 __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1832 EmitPropertyKey(property, expr->GetIdForProperty(property_index));
1833 VisitForStackValue(value);
1834 EmitSetHomeObjectIfNeeded(
1835 value, 2, expr->SlotForHomeObject(value, &store_slot_index));
1837 switch (property->kind()) {
1838 case ObjectLiteral::Property::CONSTANT:
1839 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1840 case ObjectLiteral::Property::COMPUTED:
1841 if (property->emit_store()) {
1842 __ li(a0, Operand(Smi::FromInt(NONE)));
1844 __ CallRuntime(Runtime::kDefineDataPropertyUnchecked, 4);
1850 case ObjectLiteral::Property::PROTOTYPE:
1854 case ObjectLiteral::Property::GETTER:
1855 __ li(a0, Operand(Smi::FromInt(NONE)));
1857 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
1860 case ObjectLiteral::Property::SETTER:
1861 __ li(a0, Operand(Smi::FromInt(NONE)));
1863 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
1869 if (expr->has_function()) {
1870 DCHECK(result_saved);
1871 __ lw(a0, MemOperand(sp));
1873 __ CallRuntime(Runtime::kToFastProperties, 1);
1877 context()->PlugTOS();
1879 context()->Plug(v0);
1882 // Verify that compilation exactly consumed the number of store ic slots that
1883 // the ObjectLiteral node had to offer.
1884 DCHECK(!FLAG_vector_stores || store_slot_index == expr->slot_count());
1888 void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1889 Comment cmnt(masm_, "[ ArrayLiteral");
1891 expr->BuildConstantElements(isolate());
1893 Handle<FixedArray> constant_elements = expr->constant_elements();
1894 bool has_fast_elements =
1895 IsFastObjectElementsKind(expr->constant_elements_kind());
1897 AllocationSiteMode allocation_site_mode = TRACK_ALLOCATION_SITE;
1898 if (has_fast_elements && !FLAG_allocation_site_pretenuring) {
1899 // If the only customer of allocation sites is transitioning, then
1900 // we can turn it off if we don't have anywhere else to transition to.
1901 allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
1904 __ mov(a0, result_register());
1905 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1906 __ lw(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1907 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1908 __ li(a1, Operand(constant_elements));
1909 if (MustCreateArrayLiteralWithRuntime(expr)) {
1910 __ li(a0, Operand(Smi::FromInt(expr->ComputeFlags())));
1911 __ Push(a3, a2, a1, a0);
1912 __ CallRuntime(Runtime::kCreateArrayLiteral, 4);
1914 FastCloneShallowArrayStub stub(isolate(), allocation_site_mode);
1917 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1919 bool result_saved = false; // Is the result saved to the stack?
1920 ZoneList<Expression*>* subexprs = expr->values();
1921 int length = subexprs->length();
1923 // Emit code to evaluate all the non-constant subexpressions and to store
1924 // them into the newly cloned array.
1925 int array_index = 0;
1926 for (; array_index < length; array_index++) {
1927 Expression* subexpr = subexprs->at(array_index);
1928 if (subexpr->IsSpread()) break;
1930 // If the subexpression is a literal or a simple materialized literal it
1931 // is already set in the cloned array.
1932 if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
1934 if (!result_saved) {
1935 __ push(v0); // array literal
1936 __ Push(Smi::FromInt(expr->literal_index()));
1937 result_saved = true;
1940 VisitForAccumulatorValue(subexpr);
1942 if (has_fast_elements) {
1943 int offset = FixedArray::kHeaderSize + (array_index * kPointerSize);
1944 __ lw(t2, MemOperand(sp, kPointerSize)); // Copy of array literal.
1945 __ lw(a1, FieldMemOperand(t2, JSObject::kElementsOffset));
1946 __ sw(result_register(), FieldMemOperand(a1, offset));
1947 // Update the write barrier for the array store.
1948 __ RecordWriteField(a1, offset, result_register(), a2,
1949 kRAHasBeenSaved, kDontSaveFPRegs,
1950 EMIT_REMEMBERED_SET, INLINE_SMI_CHECK);
1952 __ li(a3, Operand(Smi::FromInt(array_index)));
1953 __ mov(a0, result_register());
1954 StoreArrayLiteralElementStub stub(isolate());
1958 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1961 // In case the array literal contains spread expressions it has two parts. The
1962 // first part is the "static" array which has a literal index is handled
1963 // above. The second part is the part after the first spread expression
1964 // (inclusive) and these elements gets appended to the array. Note that the
1965 // number elements an iterable produces is unknown ahead of time.
1966 if (array_index < length && result_saved) {
1967 __ Pop(); // literal index
1969 result_saved = false;
1971 for (; array_index < length; array_index++) {
1972 Expression* subexpr = subexprs->at(array_index);
1975 if (subexpr->IsSpread()) {
1976 VisitForStackValue(subexpr->AsSpread()->expression());
1977 __ InvokeBuiltin(Builtins::CONCAT_ITERABLE_TO_ARRAY, CALL_FUNCTION);
1979 VisitForStackValue(subexpr);
1980 __ CallRuntime(Runtime::kAppendElement, 2);
1983 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1987 __ Pop(); // literal index
1988 context()->PlugTOS();
1990 context()->Plug(v0);
1995 void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1996 DCHECK(expr->target()->IsValidReferenceExpression());
1998 Comment cmnt(masm_, "[ Assignment");
1999 SetExpressionPosition(expr, INSERT_BREAK);
2001 Property* property = expr->target()->AsProperty();
2002 LhsKind assign_type = Property::GetAssignType(property);
2004 // Evaluate LHS expression.
2005 switch (assign_type) {
2007 // Nothing to do here.
2009 case NAMED_PROPERTY:
2010 if (expr->is_compound()) {
2011 // We need the receiver both on the stack and in the register.
2012 VisitForStackValue(property->obj());
2013 __ lw(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
2015 VisitForStackValue(property->obj());
2018 case NAMED_SUPER_PROPERTY:
2020 property->obj()->AsSuperPropertyReference()->this_var());
2021 VisitForAccumulatorValue(
2022 property->obj()->AsSuperPropertyReference()->home_object());
2023 __ Push(result_register());
2024 if (expr->is_compound()) {
2025 const Register scratch = a1;
2026 __ lw(scratch, MemOperand(sp, kPointerSize));
2027 __ Push(scratch, result_register());
2030 case KEYED_SUPER_PROPERTY: {
2031 const Register scratch = a1;
2033 property->obj()->AsSuperPropertyReference()->this_var());
2034 VisitForAccumulatorValue(
2035 property->obj()->AsSuperPropertyReference()->home_object());
2036 __ Move(scratch, result_register());
2037 VisitForAccumulatorValue(property->key());
2038 __ Push(scratch, result_register());
2039 if (expr->is_compound()) {
2040 const Register scratch1 = t0;
2041 __ lw(scratch1, MemOperand(sp, 2 * kPointerSize));
2042 __ Push(scratch1, scratch, result_register());
2046 case KEYED_PROPERTY:
2047 // We need the key and receiver on both the stack and in v0 and a1.
2048 if (expr->is_compound()) {
2049 VisitForStackValue(property->obj());
2050 VisitForStackValue(property->key());
2051 __ lw(LoadDescriptor::ReceiverRegister(),
2052 MemOperand(sp, 1 * kPointerSize));
2053 __ lw(LoadDescriptor::NameRegister(), MemOperand(sp, 0));
2055 VisitForStackValue(property->obj());
2056 VisitForStackValue(property->key());
2061 // For compound assignments we need another deoptimization point after the
2062 // variable/property load.
2063 if (expr->is_compound()) {
2064 { AccumulatorValueContext context(this);
2065 switch (assign_type) {
2067 EmitVariableLoad(expr->target()->AsVariableProxy());
2068 PrepareForBailout(expr->target(), TOS_REG);
2070 case NAMED_PROPERTY:
2071 EmitNamedPropertyLoad(property);
2072 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2074 case NAMED_SUPER_PROPERTY:
2075 EmitNamedSuperPropertyLoad(property);
2076 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2078 case KEYED_SUPER_PROPERTY:
2079 EmitKeyedSuperPropertyLoad(property);
2080 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2082 case KEYED_PROPERTY:
2083 EmitKeyedPropertyLoad(property);
2084 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2089 Token::Value op = expr->binary_op();
2090 __ push(v0); // Left operand goes on the stack.
2091 VisitForAccumulatorValue(expr->value());
2093 AccumulatorValueContext context(this);
2094 if (ShouldInlineSmiCase(op)) {
2095 EmitInlineSmiBinaryOp(expr->binary_operation(),
2100 EmitBinaryOp(expr->binary_operation(), op);
2103 // Deoptimization point in case the binary operation may have side effects.
2104 PrepareForBailout(expr->binary_operation(), TOS_REG);
2106 VisitForAccumulatorValue(expr->value());
2109 SetExpressionPosition(expr);
2112 switch (assign_type) {
2114 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
2115 expr->op(), expr->AssignmentSlot());
2116 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2117 context()->Plug(v0);
2119 case NAMED_PROPERTY:
2120 EmitNamedPropertyAssignment(expr);
2122 case NAMED_SUPER_PROPERTY:
2123 EmitNamedSuperPropertyStore(property);
2124 context()->Plug(v0);
2126 case KEYED_SUPER_PROPERTY:
2127 EmitKeyedSuperPropertyStore(property);
2128 context()->Plug(v0);
2130 case KEYED_PROPERTY:
2131 EmitKeyedPropertyAssignment(expr);
2137 void FullCodeGenerator::VisitYield(Yield* expr) {
2138 Comment cmnt(masm_, "[ Yield");
2139 SetExpressionPosition(expr);
2141 // Evaluate yielded value first; the initial iterator definition depends on
2142 // this. It stays on the stack while we update the iterator.
2143 VisitForStackValue(expr->expression());
2145 switch (expr->yield_kind()) {
2146 case Yield::kSuspend:
2147 // Pop value from top-of-stack slot; box result into result register.
2148 EmitCreateIteratorResult(false);
2149 __ push(result_register());
2151 case Yield::kInitial: {
2152 Label suspend, continuation, post_runtime, resume;
2155 __ bind(&continuation);
2156 __ RecordGeneratorContinuation();
2160 VisitForAccumulatorValue(expr->generator_object());
2161 DCHECK(continuation.pos() > 0 && Smi::IsValid(continuation.pos()));
2162 __ li(a1, Operand(Smi::FromInt(continuation.pos())));
2163 __ sw(a1, FieldMemOperand(v0, JSGeneratorObject::kContinuationOffset));
2164 __ sw(cp, FieldMemOperand(v0, JSGeneratorObject::kContextOffset));
2166 __ RecordWriteField(v0, JSGeneratorObject::kContextOffset, a1, a2,
2167 kRAHasBeenSaved, kDontSaveFPRegs);
2168 __ Addu(a1, fp, Operand(StandardFrameConstants::kExpressionsOffset));
2169 __ Branch(&post_runtime, eq, sp, Operand(a1));
2170 __ push(v0); // generator object
2171 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
2172 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2173 __ bind(&post_runtime);
2174 __ pop(result_register());
2175 EmitReturnSequence();
2178 context()->Plug(result_register());
2182 case Yield::kFinal: {
2183 VisitForAccumulatorValue(expr->generator_object());
2184 __ li(a1, Operand(Smi::FromInt(JSGeneratorObject::kGeneratorClosed)));
2185 __ sw(a1, FieldMemOperand(result_register(),
2186 JSGeneratorObject::kContinuationOffset));
2187 // Pop value from top-of-stack slot, box result into result register.
2188 EmitCreateIteratorResult(true);
2189 EmitUnwindBeforeReturn();
2190 EmitReturnSequence();
2194 case Yield::kDelegating: {
2195 VisitForStackValue(expr->generator_object());
2197 // Initial stack layout is as follows:
2198 // [sp + 1 * kPointerSize] iter
2199 // [sp + 0 * kPointerSize] g
2201 Label l_catch, l_try, l_suspend, l_continuation, l_resume;
2202 Label l_next, l_call;
2203 Register load_receiver = LoadDescriptor::ReceiverRegister();
2204 Register load_name = LoadDescriptor::NameRegister();
2206 // Initial send value is undefined.
2207 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
2210 // catch (e) { receiver = iter; f = 'throw'; arg = e; goto l_call; }
2213 __ LoadRoot(load_name, Heap::kthrow_stringRootIndex); // "throw"
2214 __ lw(a3, MemOperand(sp, 1 * kPointerSize)); // iter
2215 __ Push(load_name, a3, a0); // "throw", iter, except
2218 // try { received = %yield result }
2219 // Shuffle the received result above a try handler and yield it without
2222 __ pop(a0); // result
2223 int handler_index = NewHandlerTableEntry();
2224 EnterTryBlock(handler_index, &l_catch);
2225 const int try_block_size = TryCatch::kElementCount * kPointerSize;
2226 __ push(a0); // result
2229 __ bind(&l_continuation);
2230 __ RecordGeneratorContinuation();
2234 __ bind(&l_suspend);
2235 const int generator_object_depth = kPointerSize + try_block_size;
2236 __ lw(a0, MemOperand(sp, generator_object_depth));
2238 __ Push(Smi::FromInt(handler_index)); // handler-index
2239 DCHECK(l_continuation.pos() > 0 && Smi::IsValid(l_continuation.pos()));
2240 __ li(a1, Operand(Smi::FromInt(l_continuation.pos())));
2241 __ sw(a1, FieldMemOperand(a0, JSGeneratorObject::kContinuationOffset));
2242 __ sw(cp, FieldMemOperand(a0, JSGeneratorObject::kContextOffset));
2244 __ RecordWriteField(a0, JSGeneratorObject::kContextOffset, a1, a2,
2245 kRAHasBeenSaved, kDontSaveFPRegs);
2246 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 2);
2247 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2248 __ pop(v0); // result
2249 EmitReturnSequence();
2251 __ bind(&l_resume); // received in a0
2252 ExitTryBlock(handler_index);
2254 // receiver = iter; f = 'next'; arg = received;
2257 __ LoadRoot(load_name, Heap::knext_stringRootIndex); // "next"
2258 __ lw(a3, MemOperand(sp, 1 * kPointerSize)); // iter
2259 __ Push(load_name, a3, a0); // "next", iter, received
2261 // result = receiver[f](arg);
2263 __ lw(load_receiver, MemOperand(sp, kPointerSize));
2264 __ lw(load_name, MemOperand(sp, 2 * kPointerSize));
2265 __ li(LoadDescriptor::SlotRegister(),
2266 Operand(SmiFromSlot(expr->KeyedLoadFeedbackSlot())));
2267 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), SLOPPY).code();
2268 CallIC(ic, TypeFeedbackId::None());
2271 __ sw(a1, MemOperand(sp, 2 * kPointerSize));
2272 SetCallPosition(expr, 1);
2273 CallFunctionStub stub(isolate(), 1, CALL_AS_METHOD);
2276 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2277 __ Drop(1); // The function is still on the stack; drop it.
2279 // if (!result.done) goto l_try;
2280 __ Move(load_receiver, v0);
2282 __ push(load_receiver); // save result
2283 __ LoadRoot(load_name, Heap::kdone_stringRootIndex); // "done"
2284 __ li(LoadDescriptor::SlotRegister(),
2285 Operand(SmiFromSlot(expr->DoneFeedbackSlot())));
2286 CallLoadIC(NOT_INSIDE_TYPEOF); // v0=result.done
2288 Handle<Code> bool_ic = ToBooleanStub::GetUninitialized(isolate());
2290 __ Branch(&l_try, eq, v0, Operand(zero_reg));
2293 __ pop(load_receiver); // result
2294 __ LoadRoot(load_name, Heap::kvalue_stringRootIndex); // "value"
2295 __ li(LoadDescriptor::SlotRegister(),
2296 Operand(SmiFromSlot(expr->ValueFeedbackSlot())));
2297 CallLoadIC(NOT_INSIDE_TYPEOF); // v0=result.value
2298 context()->DropAndPlug(2, v0); // drop iter and g
2305 void FullCodeGenerator::EmitGeneratorResume(Expression *generator,
2307 JSGeneratorObject::ResumeMode resume_mode) {
2308 // The value stays in a0, and is ultimately read by the resumed generator, as
2309 // if CallRuntime(Runtime::kSuspendJSGeneratorObject) returned it. Or it
2310 // is read to throw the value when the resumed generator is already closed.
2311 // a1 will hold the generator object until the activation has been resumed.
2312 VisitForStackValue(generator);
2313 VisitForAccumulatorValue(value);
2316 // Load suspended function and context.
2317 __ lw(cp, FieldMemOperand(a1, JSGeneratorObject::kContextOffset));
2318 __ lw(t0, FieldMemOperand(a1, JSGeneratorObject::kFunctionOffset));
2320 // Load receiver and store as the first argument.
2321 __ lw(a2, FieldMemOperand(a1, JSGeneratorObject::kReceiverOffset));
2324 // Push holes for the rest of the arguments to the generator function.
2325 __ lw(a3, FieldMemOperand(t0, JSFunction::kSharedFunctionInfoOffset));
2327 FieldMemOperand(a3, SharedFunctionInfo::kFormalParameterCountOffset));
2328 __ LoadRoot(a2, Heap::kTheHoleValueRootIndex);
2329 Label push_argument_holes, push_frame;
2330 __ bind(&push_argument_holes);
2331 __ Subu(a3, a3, Operand(Smi::FromInt(1)));
2332 __ Branch(&push_frame, lt, a3, Operand(zero_reg));
2334 __ jmp(&push_argument_holes);
2336 // Enter a new JavaScript frame, and initialize its slots as they were when
2337 // the generator was suspended.
2338 Label resume_frame, done;
2339 __ bind(&push_frame);
2340 __ Call(&resume_frame);
2342 __ bind(&resume_frame);
2343 // ra = return address.
2344 // fp = caller's frame pointer.
2345 // cp = callee's context,
2346 // t0 = callee's JS function.
2347 __ Push(ra, fp, cp, t0);
2348 // Adjust FP to point to saved FP.
2349 __ Addu(fp, sp, 2 * kPointerSize);
2351 // Load the operand stack size.
2352 __ lw(a3, FieldMemOperand(a1, JSGeneratorObject::kOperandStackOffset));
2353 __ lw(a3, FieldMemOperand(a3, FixedArray::kLengthOffset));
2356 // If we are sending a value and there is no operand stack, we can jump back
2358 if (resume_mode == JSGeneratorObject::NEXT) {
2360 __ Branch(&slow_resume, ne, a3, Operand(zero_reg));
2361 __ lw(a3, FieldMemOperand(t0, JSFunction::kCodeEntryOffset));
2362 __ lw(a2, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
2364 __ Addu(a3, a3, Operand(a2));
2365 __ li(a2, Operand(Smi::FromInt(JSGeneratorObject::kGeneratorExecuting)));
2366 __ sw(a2, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
2368 __ bind(&slow_resume);
2371 // Otherwise, we push holes for the operand stack and call the runtime to fix
2372 // up the stack and the handlers.
2373 Label push_operand_holes, call_resume;
2374 __ bind(&push_operand_holes);
2375 __ Subu(a3, a3, Operand(1));
2376 __ Branch(&call_resume, lt, a3, Operand(zero_reg));
2378 __ Branch(&push_operand_holes);
2379 __ bind(&call_resume);
2380 DCHECK(!result_register().is(a1));
2381 __ Push(a1, result_register());
2382 __ Push(Smi::FromInt(resume_mode));
2383 __ CallRuntime(Runtime::kResumeJSGeneratorObject, 3);
2384 // Not reached: the runtime call returns elsewhere.
2385 __ stop("not-reached");
2388 context()->Plug(result_register());
2392 void FullCodeGenerator::EmitCreateIteratorResult(bool done) {
2396 const int instance_size = 5 * kPointerSize;
2397 DCHECK_EQ(isolate()->native_context()->iterator_result_map()->instance_size(),
2400 __ Allocate(instance_size, v0, a2, a3, &gc_required, TAG_OBJECT);
2403 __ bind(&gc_required);
2404 __ Push(Smi::FromInt(instance_size));
2405 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
2406 __ lw(context_register(),
2407 MemOperand(fp, StandardFrameConstants::kContextOffset));
2409 __ bind(&allocated);
2410 __ lw(a1, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
2411 __ lw(a1, FieldMemOperand(a1, GlobalObject::kNativeContextOffset));
2412 __ lw(a1, ContextOperand(a1, Context::ITERATOR_RESULT_MAP_INDEX));
2414 __ li(a3, Operand(isolate()->factory()->ToBoolean(done)));
2415 __ li(t0, Operand(isolate()->factory()->empty_fixed_array()));
2416 __ sw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2417 __ sw(t0, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2418 __ sw(t0, FieldMemOperand(v0, JSObject::kElementsOffset));
2420 FieldMemOperand(v0, JSGeneratorObject::kResultValuePropertyOffset));
2422 FieldMemOperand(v0, JSGeneratorObject::kResultDonePropertyOffset));
2424 // Only the value field needs a write barrier, as the other values are in the
2426 __ RecordWriteField(v0, JSGeneratorObject::kResultValuePropertyOffset,
2427 a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
2431 void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
2432 SetExpressionPosition(prop);
2433 Literal* key = prop->key()->AsLiteral();
2434 DCHECK(!prop->IsSuperAccess());
2436 __ li(LoadDescriptor::NameRegister(), Operand(key->value()));
2437 __ li(LoadDescriptor::SlotRegister(),
2438 Operand(SmiFromSlot(prop->PropertyFeedbackSlot())));
2439 CallLoadIC(NOT_INSIDE_TYPEOF, language_mode());
2443 void FullCodeGenerator::EmitNamedSuperPropertyLoad(Property* prop) {
2444 // Stack: receiver, home_object.
2445 SetExpressionPosition(prop);
2447 Literal* key = prop->key()->AsLiteral();
2448 DCHECK(!key->value()->IsSmi());
2449 DCHECK(prop->IsSuperAccess());
2451 __ Push(key->value());
2452 __ Push(Smi::FromInt(language_mode()));
2453 __ CallRuntime(Runtime::kLoadFromSuper, 4);
2457 void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
2458 SetExpressionPosition(prop);
2459 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), language_mode()).code();
2460 __ li(LoadDescriptor::SlotRegister(),
2461 Operand(SmiFromSlot(prop->PropertyFeedbackSlot())));
2466 void FullCodeGenerator::EmitKeyedSuperPropertyLoad(Property* prop) {
2467 // Stack: receiver, home_object, key.
2468 SetExpressionPosition(prop);
2469 __ Push(Smi::FromInt(language_mode()));
2470 __ CallRuntime(Runtime::kLoadKeyedFromSuper, 4);
2474 void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
2476 Expression* left_expr,
2477 Expression* right_expr) {
2478 Label done, smi_case, stub_call;
2480 Register scratch1 = a2;
2481 Register scratch2 = a3;
2483 // Get the arguments.
2485 Register right = a0;
2487 __ mov(a0, result_register());
2489 // Perform combined smi check on both operands.
2490 __ Or(scratch1, left, Operand(right));
2491 STATIC_ASSERT(kSmiTag == 0);
2492 JumpPatchSite patch_site(masm_);
2493 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
2495 __ bind(&stub_call);
2497 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2498 CallIC(code, expr->BinaryOperationFeedbackId());
2499 patch_site.EmitPatchInfo();
2503 // Smi case. This code works the same way as the smi-smi case in the type
2504 // recording binary operation stub, see
2507 __ GetLeastBitsFromSmi(scratch1, right, 5);
2508 __ srav(right, left, scratch1);
2509 __ And(v0, right, Operand(~kSmiTagMask));
2512 __ SmiUntag(scratch1, left);
2513 __ GetLeastBitsFromSmi(scratch2, right, 5);
2514 __ sllv(scratch1, scratch1, scratch2);
2515 __ Addu(scratch2, scratch1, Operand(0x40000000));
2516 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
2517 __ SmiTag(v0, scratch1);
2521 __ SmiUntag(scratch1, left);
2522 __ GetLeastBitsFromSmi(scratch2, right, 5);
2523 __ srlv(scratch1, scratch1, scratch2);
2524 __ And(scratch2, scratch1, 0xc0000000);
2525 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
2526 __ SmiTag(v0, scratch1);
2530 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
2531 __ BranchOnOverflow(&stub_call, scratch1);
2534 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
2535 __ BranchOnOverflow(&stub_call, scratch1);
2538 __ SmiUntag(scratch1, right);
2539 __ Mul(scratch2, v0, left, scratch1);
2540 __ sra(scratch1, v0, 31);
2541 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
2542 __ Branch(&done, ne, v0, Operand(zero_reg));
2543 __ Addu(scratch2, right, left);
2544 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
2545 DCHECK(Smi::FromInt(0) == 0);
2546 __ mov(v0, zero_reg);
2550 __ Or(v0, left, Operand(right));
2552 case Token::BIT_AND:
2553 __ And(v0, left, Operand(right));
2555 case Token::BIT_XOR:
2556 __ Xor(v0, left, Operand(right));
2563 context()->Plug(v0);
2567 void FullCodeGenerator::EmitClassDefineProperties(ClassLiteral* lit,
2568 int* used_store_slots) {
2569 // Constructor is in v0.
2570 DCHECK(lit != NULL);
2573 // No access check is needed here since the constructor is created by the
2575 Register scratch = a1;
2577 FieldMemOperand(v0, JSFunction::kPrototypeOrInitialMapOffset));
2580 for (int i = 0; i < lit->properties()->length(); i++) {
2581 ObjectLiteral::Property* property = lit->properties()->at(i);
2582 Expression* value = property->value();
2584 if (property->is_static()) {
2585 __ lw(scratch, MemOperand(sp, kPointerSize)); // constructor
2587 __ lw(scratch, MemOperand(sp, 0)); // prototype
2590 EmitPropertyKey(property, lit->GetIdForProperty(i));
2592 // The static prototype property is read only. We handle the non computed
2593 // property name case in the parser. Since this is the only case where we
2594 // need to check for an own read only property we special case this so we do
2595 // not need to do this for every property.
2596 if (property->is_static() && property->is_computed_name()) {
2597 __ CallRuntime(Runtime::kThrowIfStaticPrototype, 1);
2601 VisitForStackValue(value);
2602 EmitSetHomeObjectIfNeeded(value, 2,
2603 lit->SlotForHomeObject(value, used_store_slots));
2605 switch (property->kind()) {
2606 case ObjectLiteral::Property::CONSTANT:
2607 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2608 case ObjectLiteral::Property::PROTOTYPE:
2610 case ObjectLiteral::Property::COMPUTED:
2611 __ CallRuntime(Runtime::kDefineClassMethod, 3);
2614 case ObjectLiteral::Property::GETTER:
2615 __ li(a0, Operand(Smi::FromInt(DONT_ENUM)));
2617 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
2620 case ObjectLiteral::Property::SETTER:
2621 __ li(a0, Operand(Smi::FromInt(DONT_ENUM)));
2623 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
2632 __ CallRuntime(Runtime::kToFastProperties, 1);
2635 __ CallRuntime(Runtime::kToFastProperties, 1);
2637 if (is_strong(language_mode())) {
2639 FieldMemOperand(v0, JSFunction::kPrototypeOrInitialMapOffset));
2640 __ Push(v0, scratch);
2641 // TODO(conradw): It would be more efficient to define the properties with
2642 // the right attributes the first time round.
2643 // Freeze the prototype.
2644 __ CallRuntime(Runtime::kObjectFreeze, 1);
2645 // Freeze the constructor.
2646 __ CallRuntime(Runtime::kObjectFreeze, 1);
2651 void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr, Token::Value op) {
2652 __ mov(a0, result_register());
2655 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2656 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
2657 CallIC(code, expr->BinaryOperationFeedbackId());
2658 patch_site.EmitPatchInfo();
2659 context()->Plug(v0);
2663 void FullCodeGenerator::EmitAssignment(Expression* expr,
2664 FeedbackVectorICSlot slot) {
2665 DCHECK(expr->IsValidReferenceExpression());
2667 Property* prop = expr->AsProperty();
2668 LhsKind assign_type = Property::GetAssignType(prop);
2670 switch (assign_type) {
2672 Variable* var = expr->AsVariableProxy()->var();
2673 EffectContext context(this);
2674 EmitVariableAssignment(var, Token::ASSIGN, slot);
2677 case NAMED_PROPERTY: {
2678 __ push(result_register()); // Preserve value.
2679 VisitForAccumulatorValue(prop->obj());
2680 __ mov(StoreDescriptor::ReceiverRegister(), result_register());
2681 __ pop(StoreDescriptor::ValueRegister()); // Restore value.
2682 __ li(StoreDescriptor::NameRegister(),
2683 Operand(prop->key()->AsLiteral()->value()));
2684 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2688 case NAMED_SUPER_PROPERTY: {
2690 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2691 VisitForAccumulatorValue(
2692 prop->obj()->AsSuperPropertyReference()->home_object());
2693 // stack: value, this; v0: home_object
2694 Register scratch = a2;
2695 Register scratch2 = a3;
2696 __ mov(scratch, result_register()); // home_object
2697 __ lw(v0, MemOperand(sp, kPointerSize)); // value
2698 __ lw(scratch2, MemOperand(sp, 0)); // this
2699 __ sw(scratch2, MemOperand(sp, kPointerSize)); // this
2700 __ sw(scratch, MemOperand(sp, 0)); // home_object
2701 // stack: this, home_object; v0: value
2702 EmitNamedSuperPropertyStore(prop);
2705 case KEYED_SUPER_PROPERTY: {
2707 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2709 prop->obj()->AsSuperPropertyReference()->home_object());
2710 VisitForAccumulatorValue(prop->key());
2711 Register scratch = a2;
2712 Register scratch2 = a3;
2713 __ lw(scratch2, MemOperand(sp, 2 * kPointerSize)); // value
2714 // stack: value, this, home_object; v0: key, a3: value
2715 __ lw(scratch, MemOperand(sp, kPointerSize)); // this
2716 __ sw(scratch, MemOperand(sp, 2 * kPointerSize));
2717 __ lw(scratch, MemOperand(sp, 0)); // home_object
2718 __ sw(scratch, MemOperand(sp, kPointerSize));
2719 __ sw(v0, MemOperand(sp, 0));
2720 __ Move(v0, scratch2);
2721 // stack: this, home_object, key; v0: value.
2722 EmitKeyedSuperPropertyStore(prop);
2725 case KEYED_PROPERTY: {
2726 __ push(result_register()); // Preserve value.
2727 VisitForStackValue(prop->obj());
2728 VisitForAccumulatorValue(prop->key());
2729 __ mov(StoreDescriptor::NameRegister(), result_register());
2730 __ Pop(StoreDescriptor::ValueRegister(),
2731 StoreDescriptor::ReceiverRegister());
2732 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2734 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2739 context()->Plug(v0);
2743 void FullCodeGenerator::EmitStoreToStackLocalOrContextSlot(
2744 Variable* var, MemOperand location) {
2745 __ sw(result_register(), location);
2746 if (var->IsContextSlot()) {
2747 // RecordWrite may destroy all its register arguments.
2748 __ Move(a3, result_register());
2749 int offset = Context::SlotOffset(var->index());
2750 __ RecordWriteContextSlot(
2751 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
2756 void FullCodeGenerator::EmitVariableAssignment(Variable* var, Token::Value op,
2757 FeedbackVectorICSlot slot) {
2758 if (var->IsUnallocated()) {
2759 // Global var, const, or let.
2760 __ mov(StoreDescriptor::ValueRegister(), result_register());
2761 __ li(StoreDescriptor::NameRegister(), Operand(var->name()));
2762 __ lw(StoreDescriptor::ReceiverRegister(), GlobalObjectOperand());
2763 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2766 } else if (var->IsGlobalSlot()) {
2767 // Global var, const, or let.
2768 DCHECK(var->index() > 0);
2769 DCHECK(var->IsStaticGlobalObjectProperty());
2770 // Each var occupies two slots in the context: for reads and writes.
2771 int slot_index = var->index() + 1;
2772 int depth = scope()->ContextChainLength(var->scope());
2773 __ li(StoreGlobalViaContextDescriptor::DepthRegister(),
2774 Operand(Smi::FromInt(depth)));
2775 __ li(StoreGlobalViaContextDescriptor::SlotRegister(),
2776 Operand(Smi::FromInt(slot_index)));
2777 __ li(StoreGlobalViaContextDescriptor::NameRegister(),
2778 Operand(var->name()));
2779 __ mov(StoreGlobalViaContextDescriptor::ValueRegister(), result_register());
2780 StoreGlobalViaContextStub stub(isolate(), depth, language_mode());
2783 } else if (var->mode() == LET && op != Token::INIT_LET) {
2784 // Non-initializing assignment to let variable needs a write barrier.
2785 DCHECK(!var->IsLookupSlot());
2786 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2788 MemOperand location = VarOperand(var, a1);
2789 __ lw(a3, location);
2790 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2791 __ Branch(&assign, ne, a3, Operand(t0));
2792 __ li(a3, Operand(var->name()));
2794 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2795 // Perform the assignment.
2797 EmitStoreToStackLocalOrContextSlot(var, location);
2799 } else if (var->mode() == CONST && op != Token::INIT_CONST) {
2800 // Assignment to const variable needs a write barrier.
2801 DCHECK(!var->IsLookupSlot());
2802 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2804 MemOperand location = VarOperand(var, a1);
2805 __ lw(a3, location);
2806 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2807 __ Branch(&const_error, ne, a3, Operand(at));
2808 __ li(a3, Operand(var->name()));
2810 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2811 __ bind(&const_error);
2812 __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2814 } else if (!var->is_const_mode() || op == Token::INIT_CONST) {
2815 if (var->IsLookupSlot()) {
2816 // Assignment to var.
2817 __ li(a1, Operand(var->name()));
2818 __ li(a0, Operand(Smi::FromInt(language_mode())));
2819 __ Push(v0, cp, a1, a0); // Value, context, name, language mode.
2820 __ CallRuntime(Runtime::kStoreLookupSlot, 4);
2822 // Assignment to var or initializing assignment to let/const in harmony
2824 DCHECK((var->IsStackAllocated() || var->IsContextSlot()));
2825 MemOperand location = VarOperand(var, a1);
2826 if (generate_debug_code_ && op == Token::INIT_LET) {
2827 // Check for an uninitialized let binding.
2828 __ lw(a2, location);
2829 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2830 __ Check(eq, kLetBindingReInitialization, a2, Operand(t0));
2832 EmitStoreToStackLocalOrContextSlot(var, location);
2835 } else if (op == Token::INIT_CONST_LEGACY) {
2836 // Const initializers need a write barrier.
2837 DCHECK(!var->IsParameter()); // No const parameters.
2838 if (var->IsLookupSlot()) {
2839 __ li(a0, Operand(var->name()));
2840 __ Push(v0, cp, a0); // Context and name.
2841 __ CallRuntime(Runtime::kInitializeLegacyConstLookupSlot, 3);
2843 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2845 MemOperand location = VarOperand(var, a1);
2846 __ lw(a2, location);
2847 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2848 __ Branch(&skip, ne, a2, Operand(at));
2849 EmitStoreToStackLocalOrContextSlot(var, location);
2854 DCHECK(var->mode() == CONST_LEGACY && op != Token::INIT_CONST_LEGACY);
2855 if (is_strict(language_mode())) {
2856 __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2858 // Silently ignore store in sloppy mode.
2863 void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2864 // Assignment to a property, using a named store IC.
2865 Property* prop = expr->target()->AsProperty();
2866 DCHECK(prop != NULL);
2867 DCHECK(prop->key()->IsLiteral());
2869 __ mov(StoreDescriptor::ValueRegister(), result_register());
2870 __ li(StoreDescriptor::NameRegister(),
2871 Operand(prop->key()->AsLiteral()->value()));
2872 __ pop(StoreDescriptor::ReceiverRegister());
2873 if (FLAG_vector_stores) {
2874 EmitLoadStoreICSlot(expr->AssignmentSlot());
2877 CallStoreIC(expr->AssignmentFeedbackId());
2880 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2881 context()->Plug(v0);
2885 void FullCodeGenerator::EmitNamedSuperPropertyStore(Property* prop) {
2886 // Assignment to named property of super.
2888 // stack : receiver ('this'), home_object
2889 DCHECK(prop != NULL);
2890 Literal* key = prop->key()->AsLiteral();
2891 DCHECK(key != NULL);
2893 __ Push(key->value());
2895 __ CallRuntime((is_strict(language_mode()) ? Runtime::kStoreToSuper_Strict
2896 : Runtime::kStoreToSuper_Sloppy),
2901 void FullCodeGenerator::EmitKeyedSuperPropertyStore(Property* prop) {
2902 // Assignment to named property of super.
2904 // stack : receiver ('this'), home_object, key
2905 DCHECK(prop != NULL);
2909 (is_strict(language_mode()) ? Runtime::kStoreKeyedToSuper_Strict
2910 : Runtime::kStoreKeyedToSuper_Sloppy),
2915 void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2916 // Assignment to a property, using a keyed store IC.
2917 // Call keyed store IC.
2918 // The arguments are:
2919 // - a0 is the value,
2921 // - a2 is the receiver.
2922 __ mov(StoreDescriptor::ValueRegister(), result_register());
2923 __ Pop(StoreDescriptor::ReceiverRegister(), StoreDescriptor::NameRegister());
2924 DCHECK(StoreDescriptor::ValueRegister().is(a0));
2927 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2928 if (FLAG_vector_stores) {
2929 EmitLoadStoreICSlot(expr->AssignmentSlot());
2932 CallIC(ic, expr->AssignmentFeedbackId());
2935 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2936 context()->Plug(v0);
2940 void FullCodeGenerator::VisitProperty(Property* expr) {
2941 Comment cmnt(masm_, "[ Property");
2942 SetExpressionPosition(expr);
2944 Expression* key = expr->key();
2946 if (key->IsPropertyName()) {
2947 if (!expr->IsSuperAccess()) {
2948 VisitForAccumulatorValue(expr->obj());
2949 __ Move(LoadDescriptor::ReceiverRegister(), v0);
2950 EmitNamedPropertyLoad(expr);
2952 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2954 expr->obj()->AsSuperPropertyReference()->home_object());
2955 EmitNamedSuperPropertyLoad(expr);
2958 if (!expr->IsSuperAccess()) {
2959 VisitForStackValue(expr->obj());
2960 VisitForAccumulatorValue(expr->key());
2961 __ Move(LoadDescriptor::NameRegister(), v0);
2962 __ pop(LoadDescriptor::ReceiverRegister());
2963 EmitKeyedPropertyLoad(expr);
2965 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2967 expr->obj()->AsSuperPropertyReference()->home_object());
2968 VisitForStackValue(expr->key());
2969 EmitKeyedSuperPropertyLoad(expr);
2972 PrepareForBailoutForId(expr->LoadId(), TOS_REG);
2973 context()->Plug(v0);
2977 void FullCodeGenerator::CallIC(Handle<Code> code,
2978 TypeFeedbackId id) {
2980 __ Call(code, RelocInfo::CODE_TARGET, id);
2984 // Code common for calls using the IC.
2985 void FullCodeGenerator::EmitCallWithLoadIC(Call* expr) {
2986 Expression* callee = expr->expression();
2988 CallICState::CallType call_type =
2989 callee->IsVariableProxy() ? CallICState::FUNCTION : CallICState::METHOD;
2991 // Get the target function.
2992 if (call_type == CallICState::FUNCTION) {
2993 { StackValueContext context(this);
2994 EmitVariableLoad(callee->AsVariableProxy());
2995 PrepareForBailout(callee, NO_REGISTERS);
2997 // Push undefined as receiver. This is patched in the method prologue if it
2998 // is a sloppy mode method.
2999 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
3002 // Load the function from the receiver.
3003 DCHECK(callee->IsProperty());
3004 DCHECK(!callee->AsProperty()->IsSuperAccess());
3005 __ lw(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
3006 EmitNamedPropertyLoad(callee->AsProperty());
3007 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
3008 // Push the target function under the receiver.
3009 __ lw(at, MemOperand(sp, 0));
3011 __ sw(v0, MemOperand(sp, kPointerSize));
3014 EmitCall(expr, call_type);
3018 void FullCodeGenerator::EmitSuperCallWithLoadIC(Call* expr) {
3019 SetExpressionPosition(expr);
3020 Expression* callee = expr->expression();
3021 DCHECK(callee->IsProperty());
3022 Property* prop = callee->AsProperty();
3023 DCHECK(prop->IsSuperAccess());
3025 Literal* key = prop->key()->AsLiteral();
3026 DCHECK(!key->value()->IsSmi());
3027 // Load the function from the receiver.
3028 const Register scratch = a1;
3029 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
3030 VisitForAccumulatorValue(super_ref->home_object());
3031 __ mov(scratch, v0);
3032 VisitForAccumulatorValue(super_ref->this_var());
3033 __ Push(scratch, v0, v0, scratch);
3034 __ Push(key->value());
3035 __ Push(Smi::FromInt(language_mode()));
3039 // - this (receiver)
3040 // - this (receiver) <-- LoadFromSuper will pop here and below.
3044 __ CallRuntime(Runtime::kLoadFromSuper, 4);
3046 // Replace home_object with target function.
3047 __ sw(v0, MemOperand(sp, kPointerSize));
3050 // - target function
3051 // - this (receiver)
3052 EmitCall(expr, CallICState::METHOD);
3056 // Code common for calls using the IC.
3057 void FullCodeGenerator::EmitKeyedCallWithLoadIC(Call* expr,
3060 VisitForAccumulatorValue(key);
3062 Expression* callee = expr->expression();
3064 // Load the function from the receiver.
3065 DCHECK(callee->IsProperty());
3066 __ lw(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
3067 __ Move(LoadDescriptor::NameRegister(), v0);
3068 EmitKeyedPropertyLoad(callee->AsProperty());
3069 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
3071 // Push the target function under the receiver.
3072 __ lw(at, MemOperand(sp, 0));
3074 __ sw(v0, MemOperand(sp, kPointerSize));
3076 EmitCall(expr, CallICState::METHOD);
3080 void FullCodeGenerator::EmitKeyedSuperCallWithLoadIC(Call* expr) {
3081 Expression* callee = expr->expression();
3082 DCHECK(callee->IsProperty());
3083 Property* prop = callee->AsProperty();
3084 DCHECK(prop->IsSuperAccess());
3086 SetExpressionPosition(prop);
3087 // Load the function from the receiver.
3088 const Register scratch = a1;
3089 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
3090 VisitForAccumulatorValue(super_ref->home_object());
3091 __ Move(scratch, v0);
3092 VisitForAccumulatorValue(super_ref->this_var());
3093 __ Push(scratch, v0, v0, scratch);
3094 VisitForStackValue(prop->key());
3095 __ Push(Smi::FromInt(language_mode()));
3099 // - this (receiver)
3100 // - this (receiver) <-- LoadKeyedFromSuper will pop here and below.
3104 __ CallRuntime(Runtime::kLoadKeyedFromSuper, 4);
3106 // Replace home_object with target function.
3107 __ sw(v0, MemOperand(sp, kPointerSize));
3110 // - target function
3111 // - this (receiver)
3112 EmitCall(expr, CallICState::METHOD);
3116 void FullCodeGenerator::EmitCall(Call* expr, CallICState::CallType call_type) {
3117 // Load the arguments.
3118 ZoneList<Expression*>* args = expr->arguments();
3119 int arg_count = args->length();
3120 for (int i = 0; i < arg_count; i++) {
3121 VisitForStackValue(args->at(i));
3124 // Record source position of the IC call.
3125 SetCallPosition(expr, arg_count);
3126 Handle<Code> ic = CodeFactory::CallIC(isolate(), arg_count, call_type).code();
3127 __ li(a3, Operand(SmiFromSlot(expr->CallFeedbackICSlot())));
3128 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
3129 // Don't assign a type feedback id to the IC, since type feedback is provided
3130 // by the vector above.
3133 RecordJSReturnSite(expr);
3134 // Restore context register.
3135 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3136 context()->DropAndPlug(1, v0);
3140 void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
3141 // t3: copy of the first argument or undefined if it doesn't exist.
3142 if (arg_count > 0) {
3143 __ lw(t3, MemOperand(sp, arg_count * kPointerSize));
3145 __ LoadRoot(t3, Heap::kUndefinedValueRootIndex);
3148 // t2: the receiver of the enclosing function.
3149 __ lw(t2, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3151 // t1: the language mode.
3152 __ li(t1, Operand(Smi::FromInt(language_mode())));
3154 // t0: the start position of the scope the calls resides in.
3155 __ li(t0, Operand(Smi::FromInt(scope()->start_position())));
3157 // Do the runtime call.
3158 __ Push(t3, t2, t1, t0);
3159 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
3163 void FullCodeGenerator::EmitInitializeThisAfterSuper(
3164 SuperCallReference* super_ref, FeedbackVectorICSlot slot) {
3165 Variable* this_var = super_ref->this_var()->var();
3166 GetVar(a1, this_var);
3167 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
3168 Label uninitialized_this;
3169 __ Branch(&uninitialized_this, eq, a1, Operand(at));
3170 __ li(a0, Operand(this_var->name()));
3172 __ CallRuntime(Runtime::kThrowReferenceError, 1);
3173 __ bind(&uninitialized_this);
3175 EmitVariableAssignment(this_var, Token::INIT_CONST, slot);
3179 // See http://www.ecma-international.org/ecma-262/6.0/#sec-function-calls.
3180 void FullCodeGenerator::PushCalleeAndWithBaseObject(Call* expr) {
3181 VariableProxy* callee = expr->expression()->AsVariableProxy();
3182 if (callee->var()->IsLookupSlot()) {
3185 SetExpressionPosition(callee);
3186 // Generate code for loading from variables potentially shadowed by
3187 // eval-introduced variables.
3188 EmitDynamicLookupFastCase(callee, NOT_INSIDE_TYPEOF, &slow, &done);
3191 // Call the runtime to find the function to call (returned in v0)
3192 // and the object holding it (returned in v1).
3193 DCHECK(!context_register().is(a2));
3194 __ li(a2, Operand(callee->name()));
3195 __ Push(context_register(), a2);
3196 __ CallRuntime(Runtime::kLoadLookupSlot, 2);
3197 __ Push(v0, v1); // Function, receiver.
3198 PrepareForBailoutForId(expr->LookupId(), NO_REGISTERS);
3200 // If fast case code has been generated, emit code to push the
3201 // function and receiver and have the slow path jump around this
3203 if (done.is_linked()) {
3209 // The receiver is implicitly the global receiver. Indicate this
3210 // by passing the hole to the call function stub.
3211 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
3216 VisitForStackValue(callee);
3217 // refEnv.WithBaseObject()
3218 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
3219 __ push(a2); // Reserved receiver slot.
3224 void FullCodeGenerator::VisitCall(Call* expr) {
3226 // We want to verify that RecordJSReturnSite gets called on all paths
3227 // through this function. Avoid early returns.
3228 expr->return_is_recorded_ = false;
3231 Comment cmnt(masm_, "[ Call");
3232 Expression* callee = expr->expression();
3233 Call::CallType call_type = expr->GetCallType(isolate());
3235 if (call_type == Call::POSSIBLY_EVAL_CALL) {
3236 // In a call to eval, we first call RuntimeHidden_ResolvePossiblyDirectEval
3237 // to resolve the function we need to call. Then we call the resolved
3238 // function using the given arguments.
3239 ZoneList<Expression*>* args = expr->arguments();
3240 int arg_count = args->length();
3241 PushCalleeAndWithBaseObject(expr);
3243 // Push the arguments.
3244 for (int i = 0; i < arg_count; i++) {
3245 VisitForStackValue(args->at(i));
3248 // Push a copy of the function (found below the arguments) and
3250 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
3252 EmitResolvePossiblyDirectEval(arg_count);
3254 // Touch up the stack with the resolved function.
3255 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
3257 PrepareForBailoutForId(expr->EvalId(), NO_REGISTERS);
3258 // Record source position for debugger.
3259 SetCallPosition(expr, arg_count);
3260 CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
3261 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
3263 RecordJSReturnSite(expr);
3264 // Restore context register.
3265 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3266 context()->DropAndPlug(1, v0);
3267 } else if (call_type == Call::GLOBAL_CALL) {
3268 EmitCallWithLoadIC(expr);
3269 } else if (call_type == Call::LOOKUP_SLOT_CALL) {
3270 // Call to a lookup slot (dynamically introduced variable).
3271 PushCalleeAndWithBaseObject(expr);
3273 } else if (call_type == Call::PROPERTY_CALL) {
3274 Property* property = callee->AsProperty();
3275 bool is_named_call = property->key()->IsPropertyName();
3276 if (property->IsSuperAccess()) {
3277 if (is_named_call) {
3278 EmitSuperCallWithLoadIC(expr);
3280 EmitKeyedSuperCallWithLoadIC(expr);
3283 VisitForStackValue(property->obj());
3284 if (is_named_call) {
3285 EmitCallWithLoadIC(expr);
3287 EmitKeyedCallWithLoadIC(expr, property->key());
3290 } else if (call_type == Call::SUPER_CALL) {
3291 EmitSuperConstructorCall(expr);
3293 DCHECK(call_type == Call::OTHER_CALL);
3294 // Call to an arbitrary expression not handled specially above.
3295 VisitForStackValue(callee);
3296 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
3298 // Emit function call.
3303 // RecordJSReturnSite should have been called.
3304 DCHECK(expr->return_is_recorded_);
3309 void FullCodeGenerator::VisitCallNew(CallNew* expr) {
3310 Comment cmnt(masm_, "[ CallNew");
3311 // According to ECMA-262, section 11.2.2, page 44, the function
3312 // expression in new calls must be evaluated before the
3315 // Push constructor on the stack. If it's not a function it's used as
3316 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
3318 DCHECK(!expr->expression()->IsSuperPropertyReference());
3319 VisitForStackValue(expr->expression());
3321 // Push the arguments ("left-to-right") on the stack.
3322 ZoneList<Expression*>* args = expr->arguments();
3323 int arg_count = args->length();
3324 for (int i = 0; i < arg_count; i++) {
3325 VisitForStackValue(args->at(i));
3328 // Call the construct call builtin that handles allocation and
3329 // constructor invocation.
3330 SetConstructCallPosition(expr);
3332 // Load function and argument count into a1 and a0.
3333 __ li(a0, Operand(arg_count));
3334 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
3336 // Record call targets in unoptimized code.
3337 if (FLAG_pretenuring_call_new) {
3338 EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3339 DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3340 expr->CallNewFeedbackSlot().ToInt() + 1);
3343 __ li(a2, FeedbackVector());
3344 __ li(a3, Operand(SmiFromSlot(expr->CallNewFeedbackSlot())));
3346 CallConstructStub stub(isolate(), RECORD_CONSTRUCTOR_TARGET);
3347 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3348 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
3349 context()->Plug(v0);
3353 void FullCodeGenerator::EmitSuperConstructorCall(Call* expr) {
3354 SuperCallReference* super_call_ref =
3355 expr->expression()->AsSuperCallReference();
3356 DCHECK_NOT_NULL(super_call_ref);
3358 EmitLoadSuperConstructor(super_call_ref);
3359 __ push(result_register());
3361 // Push the arguments ("left-to-right") on the stack.
3362 ZoneList<Expression*>* args = expr->arguments();
3363 int arg_count = args->length();
3364 for (int i = 0; i < arg_count; i++) {
3365 VisitForStackValue(args->at(i));
3368 // Call the construct call builtin that handles allocation and
3369 // constructor invocation.
3370 SetConstructCallPosition(expr);
3372 // Load original constructor into t0.
3373 VisitForAccumulatorValue(super_call_ref->new_target_var());
3374 __ mov(t0, result_register());
3376 // Load function and argument count into a1 and a0.
3377 __ li(a0, Operand(arg_count));
3378 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
3380 // Record call targets in unoptimized code.
3381 if (FLAG_pretenuring_call_new) {
3383 /* TODO(dslomov): support pretenuring.
3384 EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3385 DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3386 expr->CallNewFeedbackSlot().ToInt() + 1);
3390 __ li(a2, FeedbackVector());
3391 __ li(a3, Operand(SmiFromSlot(expr->CallFeedbackSlot())));
3393 CallConstructStub stub(isolate(), SUPER_CALL_RECORD_TARGET);
3394 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3396 RecordJSReturnSite(expr);
3398 EmitInitializeThisAfterSuper(super_call_ref, expr->CallFeedbackICSlot());
3399 context()->Plug(v0);
3403 void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
3404 ZoneList<Expression*>* args = expr->arguments();
3405 DCHECK(args->length() == 1);
3407 VisitForAccumulatorValue(args->at(0));
3409 Label materialize_true, materialize_false;
3410 Label* if_true = NULL;
3411 Label* if_false = NULL;
3412 Label* fall_through = NULL;
3413 context()->PrepareTest(&materialize_true, &materialize_false,
3414 &if_true, &if_false, &fall_through);
3416 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3418 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
3420 context()->Plug(if_true, if_false);
3424 void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
3425 ZoneList<Expression*>* args = expr->arguments();
3426 DCHECK(args->length() == 1);
3428 VisitForAccumulatorValue(args->at(0));
3430 Label materialize_true, materialize_false;
3431 Label* if_true = NULL;
3432 Label* if_false = NULL;
3433 Label* fall_through = NULL;
3434 context()->PrepareTest(&materialize_true, &materialize_false,
3435 &if_true, &if_false, &fall_through);
3437 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3438 __ NonNegativeSmiTst(v0, at);
3439 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
3441 context()->Plug(if_true, if_false);
3445 void FullCodeGenerator::EmitIsObject(CallRuntime* expr) {
3446 ZoneList<Expression*>* args = expr->arguments();
3447 DCHECK(args->length() == 1);
3449 VisitForAccumulatorValue(args->at(0));
3451 Label materialize_true, materialize_false;
3452 Label* if_true = NULL;
3453 Label* if_false = NULL;
3454 Label* fall_through = NULL;
3455 context()->PrepareTest(&materialize_true, &materialize_false,
3456 &if_true, &if_false, &fall_through);
3458 __ JumpIfSmi(v0, if_false);
3459 __ LoadRoot(at, Heap::kNullValueRootIndex);
3460 __ Branch(if_true, eq, v0, Operand(at));
3461 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
3462 // Undetectable objects behave like undefined when tested with typeof.
3463 __ lbu(a1, FieldMemOperand(a2, Map::kBitFieldOffset));
3464 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
3465 __ Branch(if_false, ne, at, Operand(zero_reg));
3466 __ lbu(a1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
3467 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
3468 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3469 Split(le, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE),
3470 if_true, if_false, fall_through);
3472 context()->Plug(if_true, if_false);
3476 void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
3477 ZoneList<Expression*>* args = expr->arguments();
3478 DCHECK(args->length() == 1);
3480 VisitForAccumulatorValue(args->at(0));
3482 Label materialize_true, materialize_false;
3483 Label* if_true = NULL;
3484 Label* if_false = NULL;
3485 Label* fall_through = NULL;
3486 context()->PrepareTest(&materialize_true, &materialize_false,
3487 &if_true, &if_false, &fall_through);
3489 __ JumpIfSmi(v0, if_false);
3490 __ GetObjectType(v0, a1, a1);
3491 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3492 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
3493 if_true, if_false, fall_through);
3495 context()->Plug(if_true, if_false);
3499 void FullCodeGenerator::EmitIsUndetectableObject(CallRuntime* expr) {
3500 ZoneList<Expression*>* args = expr->arguments();
3501 DCHECK(args->length() == 1);
3503 VisitForAccumulatorValue(args->at(0));
3505 Label materialize_true, materialize_false;
3506 Label* if_true = NULL;
3507 Label* if_false = NULL;
3508 Label* fall_through = NULL;
3509 context()->PrepareTest(&materialize_true, &materialize_false,
3510 &if_true, &if_false, &fall_through);
3512 __ JumpIfSmi(v0, if_false);
3513 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
3514 __ lbu(a1, FieldMemOperand(a1, Map::kBitFieldOffset));
3515 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3516 __ And(at, a1, Operand(1 << Map::kIsUndetectable));
3517 Split(ne, at, Operand(zero_reg), if_true, if_false, fall_through);
3519 context()->Plug(if_true, if_false);
3523 void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
3524 CallRuntime* expr) {
3525 ZoneList<Expression*>* args = expr->arguments();
3526 DCHECK(args->length() == 1);
3528 VisitForAccumulatorValue(args->at(0));
3530 Label materialize_true, materialize_false, skip_lookup;
3531 Label* if_true = NULL;
3532 Label* if_false = NULL;
3533 Label* fall_through = NULL;
3534 context()->PrepareTest(&materialize_true, &materialize_false,
3535 &if_true, &if_false, &fall_through);
3537 __ AssertNotSmi(v0);
3539 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
3540 __ lbu(t0, FieldMemOperand(a1, Map::kBitField2Offset));
3541 __ And(t0, t0, 1 << Map::kStringWrapperSafeForDefaultValueOf);
3542 __ Branch(&skip_lookup, ne, t0, Operand(zero_reg));
3544 // Check for fast case object. Generate false result for slow case object.
3545 __ lw(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
3546 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
3547 __ LoadRoot(t0, Heap::kHashTableMapRootIndex);
3548 __ Branch(if_false, eq, a2, Operand(t0));
3550 // Look for valueOf name in the descriptor array, and indicate false if
3551 // found. Since we omit an enumeration index check, if it is added via a
3552 // transition that shares its descriptor array, this is a false positive.
3553 Label entry, loop, done;
3555 // Skip loop if no descriptors are valid.
3556 __ NumberOfOwnDescriptors(a3, a1);
3557 __ Branch(&done, eq, a3, Operand(zero_reg));
3559 __ LoadInstanceDescriptors(a1, t0);
3560 // t0: descriptor array.
3561 // a3: valid entries in the descriptor array.
3562 STATIC_ASSERT(kSmiTag == 0);
3563 STATIC_ASSERT(kSmiTagSize == 1);
3564 STATIC_ASSERT(kPointerSize == 4);
3565 __ li(at, Operand(DescriptorArray::kDescriptorSize));
3567 // Calculate location of the first key name.
3568 __ Addu(t0, t0, Operand(DescriptorArray::kFirstOffset - kHeapObjectTag));
3569 // Calculate the end of the descriptor array.
3571 __ sll(t1, a3, kPointerSizeLog2);
3572 __ Addu(a2, a2, t1);
3574 // Loop through all the keys in the descriptor array. If one of these is the
3575 // string "valueOf" the result is false.
3576 // The use of t2 to store the valueOf string assumes that it is not otherwise
3577 // used in the loop below.
3578 __ li(t2, Operand(isolate()->factory()->value_of_string()));
3581 __ lw(a3, MemOperand(t0, 0));
3582 __ Branch(if_false, eq, a3, Operand(t2));
3583 __ Addu(t0, t0, Operand(DescriptorArray::kDescriptorSize * kPointerSize));
3585 __ Branch(&loop, ne, t0, Operand(a2));
3589 // Set the bit in the map to indicate that there is no local valueOf field.
3590 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
3591 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
3592 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
3594 __ bind(&skip_lookup);
3596 // If a valueOf property is not found on the object check that its
3597 // prototype is the un-modified String prototype. If not result is false.
3598 __ lw(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
3599 __ JumpIfSmi(a2, if_false);
3600 __ lw(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
3601 __ lw(a3, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
3602 __ lw(a3, FieldMemOperand(a3, GlobalObject::kNativeContextOffset));
3603 __ lw(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
3604 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3605 Split(eq, a2, Operand(a3), if_true, if_false, fall_through);
3607 context()->Plug(if_true, if_false);
3611 void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
3612 ZoneList<Expression*>* args = expr->arguments();
3613 DCHECK(args->length() == 1);
3615 VisitForAccumulatorValue(args->at(0));
3617 Label materialize_true, materialize_false;
3618 Label* if_true = NULL;
3619 Label* if_false = NULL;
3620 Label* fall_through = NULL;
3621 context()->PrepareTest(&materialize_true, &materialize_false,
3622 &if_true, &if_false, &fall_through);
3624 __ JumpIfSmi(v0, if_false);
3625 __ GetObjectType(v0, a1, a2);
3626 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3627 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
3628 __ Branch(if_false);
3630 context()->Plug(if_true, if_false);
3634 void FullCodeGenerator::EmitIsMinusZero(CallRuntime* expr) {
3635 ZoneList<Expression*>* args = expr->arguments();
3636 DCHECK(args->length() == 1);
3638 VisitForAccumulatorValue(args->at(0));
3640 Label materialize_true, materialize_false;
3641 Label* if_true = NULL;
3642 Label* if_false = NULL;
3643 Label* fall_through = NULL;
3644 context()->PrepareTest(&materialize_true, &materialize_false,
3645 &if_true, &if_false, &fall_through);
3647 __ CheckMap(v0, a1, Heap::kHeapNumberMapRootIndex, if_false, DO_SMI_CHECK);
3648 __ lw(a2, FieldMemOperand(v0, HeapNumber::kExponentOffset));
3649 __ lw(a1, FieldMemOperand(v0, HeapNumber::kMantissaOffset));
3650 __ li(t0, 0x80000000);
3652 __ Branch(¬_nan, ne, a2, Operand(t0));
3653 __ mov(t0, zero_reg);
3657 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3658 Split(eq, a2, Operand(t0), if_true, if_false, fall_through);
3660 context()->Plug(if_true, if_false);
3664 void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
3665 ZoneList<Expression*>* args = expr->arguments();
3666 DCHECK(args->length() == 1);
3668 VisitForAccumulatorValue(args->at(0));
3670 Label materialize_true, materialize_false;
3671 Label* if_true = NULL;
3672 Label* if_false = NULL;
3673 Label* fall_through = NULL;
3674 context()->PrepareTest(&materialize_true, &materialize_false,
3675 &if_true, &if_false, &fall_through);
3677 __ JumpIfSmi(v0, if_false);
3678 __ GetObjectType(v0, a1, a1);
3679 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3680 Split(eq, a1, Operand(JS_ARRAY_TYPE),
3681 if_true, if_false, fall_through);
3683 context()->Plug(if_true, if_false);
3687 void FullCodeGenerator::EmitIsTypedArray(CallRuntime* expr) {
3688 ZoneList<Expression*>* args = expr->arguments();
3689 DCHECK(args->length() == 1);
3691 VisitForAccumulatorValue(args->at(0));
3693 Label materialize_true, materialize_false;
3694 Label* if_true = NULL;
3695 Label* if_false = NULL;
3696 Label* fall_through = NULL;
3697 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3698 &if_false, &fall_through);
3700 __ JumpIfSmi(v0, if_false);
3701 __ GetObjectType(v0, a1, a1);
3702 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3703 Split(eq, a1, Operand(JS_TYPED_ARRAY_TYPE), if_true, if_false, fall_through);
3705 context()->Plug(if_true, if_false);
3709 void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
3710 ZoneList<Expression*>* args = expr->arguments();
3711 DCHECK(args->length() == 1);
3713 VisitForAccumulatorValue(args->at(0));
3715 Label materialize_true, materialize_false;
3716 Label* if_true = NULL;
3717 Label* if_false = NULL;
3718 Label* fall_through = NULL;
3719 context()->PrepareTest(&materialize_true, &materialize_false,
3720 &if_true, &if_false, &fall_through);
3722 __ JumpIfSmi(v0, if_false);
3723 __ GetObjectType(v0, a1, a1);
3724 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3725 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
3727 context()->Plug(if_true, if_false);
3731 void FullCodeGenerator::EmitIsJSProxy(CallRuntime* expr) {
3732 ZoneList<Expression*>* args = expr->arguments();
3733 DCHECK(args->length() == 1);
3735 VisitForAccumulatorValue(args->at(0));
3737 Label materialize_true, materialize_false;
3738 Label* if_true = NULL;
3739 Label* if_false = NULL;
3740 Label* fall_through = NULL;
3741 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3742 &if_false, &fall_through);
3744 __ JumpIfSmi(v0, if_false);
3746 Register type_reg = a2;
3747 __ GetObjectType(v0, map, type_reg);
3748 __ Subu(type_reg, type_reg, Operand(FIRST_JS_PROXY_TYPE));
3749 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3750 Split(ls, type_reg, Operand(LAST_JS_PROXY_TYPE - FIRST_JS_PROXY_TYPE),
3751 if_true, if_false, fall_through);
3753 context()->Plug(if_true, if_false);
3757 void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
3758 DCHECK(expr->arguments()->length() == 0);
3760 Label materialize_true, materialize_false;
3761 Label* if_true = NULL;
3762 Label* if_false = NULL;
3763 Label* fall_through = NULL;
3764 context()->PrepareTest(&materialize_true, &materialize_false,
3765 &if_true, &if_false, &fall_through);
3767 // Get the frame pointer for the calling frame.
3768 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3770 // Skip the arguments adaptor frame if it exists.
3771 Label check_frame_marker;
3772 __ lw(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
3773 __ Branch(&check_frame_marker, ne,
3774 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3775 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
3777 // Check the marker in the calling frame.
3778 __ bind(&check_frame_marker);
3779 __ lw(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
3780 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3781 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
3782 if_true, if_false, fall_through);
3784 context()->Plug(if_true, if_false);
3788 void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
3789 ZoneList<Expression*>* args = expr->arguments();
3790 DCHECK(args->length() == 2);
3792 // Load the two objects into registers and perform the comparison.
3793 VisitForStackValue(args->at(0));
3794 VisitForAccumulatorValue(args->at(1));
3796 Label materialize_true, materialize_false;
3797 Label* if_true = NULL;
3798 Label* if_false = NULL;
3799 Label* fall_through = NULL;
3800 context()->PrepareTest(&materialize_true, &materialize_false,
3801 &if_true, &if_false, &fall_through);
3804 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3805 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
3807 context()->Plug(if_true, if_false);
3811 void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
3812 ZoneList<Expression*>* args = expr->arguments();
3813 DCHECK(args->length() == 1);
3815 // ArgumentsAccessStub expects the key in a1 and the formal
3816 // parameter count in a0.
3817 VisitForAccumulatorValue(args->at(0));
3819 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
3820 ArgumentsAccessStub stub(isolate(), ArgumentsAccessStub::READ_ELEMENT);
3822 context()->Plug(v0);
3826 void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
3827 DCHECK(expr->arguments()->length() == 0);
3829 // Get the number of formal parameters.
3830 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
3832 // Check if the calling frame is an arguments adaptor frame.
3833 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3834 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
3835 __ Branch(&exit, ne, a3,
3836 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3838 // Arguments adaptor case: Read the arguments length from the
3840 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3843 context()->Plug(v0);
3847 void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
3848 ZoneList<Expression*>* args = expr->arguments();
3849 DCHECK(args->length() == 1);
3850 Label done, null, function, non_function_constructor;
3852 VisitForAccumulatorValue(args->at(0));
3854 // If the object is a smi, we return null.
3855 __ JumpIfSmi(v0, &null);
3857 // Check that the object is a JS object but take special care of JS
3858 // functions to make sure they have 'Function' as their class.
3859 // Assume that there are only two callable types, and one of them is at
3860 // either end of the type range for JS object types. Saves extra comparisons.
3861 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
3862 __ GetObjectType(v0, v0, a1); // Map is now in v0.
3863 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
3865 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3866 FIRST_SPEC_OBJECT_TYPE + 1);
3867 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
3869 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3870 LAST_SPEC_OBJECT_TYPE - 1);
3871 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
3872 // Assume that there is no larger type.
3873 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
3875 // Check if the constructor in the map is a JS function.
3876 Register instance_type = a2;
3877 __ GetMapConstructor(v0, v0, a1, instance_type);
3878 __ Branch(&non_function_constructor, ne, instance_type,
3879 Operand(JS_FUNCTION_TYPE));
3881 // v0 now contains the constructor function. Grab the
3882 // instance class name from there.
3883 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
3884 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
3887 // Functions have class 'Function'.
3889 __ LoadRoot(v0, Heap::kFunction_stringRootIndex);
3892 // Objects with a non-function constructor have class 'Object'.
3893 __ bind(&non_function_constructor);
3894 __ LoadRoot(v0, Heap::kObject_stringRootIndex);
3897 // Non-JS objects have class null.
3899 __ LoadRoot(v0, Heap::kNullValueRootIndex);
3904 context()->Plug(v0);
3908 void FullCodeGenerator::EmitSubString(CallRuntime* expr) {
3909 // Load the arguments on the stack and call the stub.
3910 SubStringStub stub(isolate());
3911 ZoneList<Expression*>* args = expr->arguments();
3912 DCHECK(args->length() == 3);
3913 VisitForStackValue(args->at(0));
3914 VisitForStackValue(args->at(1));
3915 VisitForStackValue(args->at(2));
3917 context()->Plug(v0);
3921 void FullCodeGenerator::EmitRegExpExec(CallRuntime* expr) {
3922 // Load the arguments on the stack and call the stub.
3923 RegExpExecStub stub(isolate());
3924 ZoneList<Expression*>* args = expr->arguments();
3925 DCHECK(args->length() == 4);
3926 VisitForStackValue(args->at(0));
3927 VisitForStackValue(args->at(1));
3928 VisitForStackValue(args->at(2));
3929 VisitForStackValue(args->at(3));
3931 context()->Plug(v0);
3935 void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3936 ZoneList<Expression*>* args = expr->arguments();
3937 DCHECK(args->length() == 1);
3939 VisitForAccumulatorValue(args->at(0)); // Load the object.
3942 // If the object is a smi return the object.
3943 __ JumpIfSmi(v0, &done);
3944 // If the object is not a value type, return the object.
3945 __ GetObjectType(v0, a1, a1);
3946 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
3948 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
3951 context()->Plug(v0);
3955 void FullCodeGenerator::EmitIsDate(CallRuntime* expr) {
3956 ZoneList<Expression*>* args = expr->arguments();
3957 DCHECK_EQ(1, args->length());
3959 VisitForAccumulatorValue(args->at(0));
3961 Label materialize_true, materialize_false;
3962 Label* if_true = nullptr;
3963 Label* if_false = nullptr;
3964 Label* fall_through = nullptr;
3965 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3966 &if_false, &fall_through);
3968 __ JumpIfSmi(v0, if_false);
3969 __ GetObjectType(v0, a1, a1);
3970 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3971 Split(eq, a1, Operand(JS_DATE_TYPE), if_true, if_false, fall_through);
3973 context()->Plug(if_true, if_false);
3977 void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3978 ZoneList<Expression*>* args = expr->arguments();
3979 DCHECK(args->length() == 2);
3980 DCHECK_NOT_NULL(args->at(1)->AsLiteral());
3981 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->value()));
3983 VisitForAccumulatorValue(args->at(0)); // Load the object.
3985 Register object = v0;
3986 Register result = v0;
3987 Register scratch0 = t5;
3988 Register scratch1 = a1;
3990 if (index->value() == 0) {
3991 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset));
3993 Label runtime, done;
3994 if (index->value() < JSDate::kFirstUncachedField) {
3995 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3996 __ li(scratch1, Operand(stamp));
3997 __ lw(scratch1, MemOperand(scratch1));
3998 __ lw(scratch0, FieldMemOperand(object, JSDate::kCacheStampOffset));
3999 __ Branch(&runtime, ne, scratch1, Operand(scratch0));
4000 __ lw(result, FieldMemOperand(object, JSDate::kValueOffset +
4001 kPointerSize * index->value()));
4005 __ PrepareCallCFunction(2, scratch1);
4006 __ li(a1, Operand(index));
4007 __ Move(a0, object);
4008 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
4012 context()->Plug(result);
4016 void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
4017 ZoneList<Expression*>* args = expr->arguments();
4018 DCHECK_EQ(3, args->length());
4020 Register string = v0;
4021 Register index = a1;
4022 Register value = a2;
4024 VisitForStackValue(args->at(0)); // index
4025 VisitForStackValue(args->at(1)); // value
4026 VisitForAccumulatorValue(args->at(2)); // string
4027 __ Pop(index, value);
4029 if (FLAG_debug_code) {
4030 __ SmiTst(value, at);
4031 __ Check(eq, kNonSmiValue, at, Operand(zero_reg));
4032 __ SmiTst(index, at);
4033 __ Check(eq, kNonSmiIndex, at, Operand(zero_reg));
4034 __ SmiUntag(index, index);
4035 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
4036 Register scratch = t5;
4037 __ EmitSeqStringSetCharCheck(
4038 string, index, value, scratch, one_byte_seq_type);
4039 __ SmiTag(index, index);
4042 __ SmiUntag(value, value);
4045 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4047 __ Addu(at, at, index);
4048 __ sb(value, MemOperand(at));
4049 context()->Plug(string);
4053 void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
4054 ZoneList<Expression*>* args = expr->arguments();
4055 DCHECK_EQ(3, args->length());
4057 Register string = v0;
4058 Register index = a1;
4059 Register value = a2;
4061 VisitForStackValue(args->at(0)); // index
4062 VisitForStackValue(args->at(1)); // value
4063 VisitForAccumulatorValue(args->at(2)); // string
4064 __ Pop(index, value);
4066 if (FLAG_debug_code) {
4067 __ SmiTst(value, at);
4068 __ Check(eq, kNonSmiValue, at, Operand(zero_reg));
4069 __ SmiTst(index, at);
4070 __ Check(eq, kNonSmiIndex, at, Operand(zero_reg));
4071 __ SmiUntag(index, index);
4072 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
4073 Register scratch = t5;
4074 __ EmitSeqStringSetCharCheck(
4075 string, index, value, scratch, two_byte_seq_type);
4076 __ SmiTag(index, index);
4079 __ SmiUntag(value, value);
4082 Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4083 __ Addu(at, at, index);
4084 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
4085 __ sh(value, MemOperand(at));
4086 context()->Plug(string);
4090 void FullCodeGenerator::EmitMathPow(CallRuntime* expr) {
4091 // Load the arguments on the stack and call the runtime function.
4092 ZoneList<Expression*>* args = expr->arguments();
4093 DCHECK(args->length() == 2);
4094 VisitForStackValue(args->at(0));
4095 VisitForStackValue(args->at(1));
4096 MathPowStub stub(isolate(), MathPowStub::ON_STACK);
4098 context()->Plug(v0);
4102 void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
4103 ZoneList<Expression*>* args = expr->arguments();
4104 DCHECK(args->length() == 2);
4106 VisitForStackValue(args->at(0)); // Load the object.
4107 VisitForAccumulatorValue(args->at(1)); // Load the value.
4108 __ pop(a1); // v0 = value. a1 = object.
4111 // If the object is a smi, return the value.
4112 __ JumpIfSmi(a1, &done);
4114 // If the object is not a value type, return the value.
4115 __ GetObjectType(a1, a2, a2);
4116 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
4119 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
4120 // Update the write barrier. Save the value as it will be
4121 // overwritten by the write barrier code and is needed afterward.
4123 __ RecordWriteField(
4124 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
4127 context()->Plug(v0);
4131 void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
4132 ZoneList<Expression*>* args = expr->arguments();
4133 DCHECK_EQ(args->length(), 1);
4135 // Load the argument into a0 and call the stub.
4136 VisitForAccumulatorValue(args->at(0));
4137 __ mov(a0, result_register());
4139 NumberToStringStub stub(isolate());
4141 context()->Plug(v0);
4145 void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
4146 ZoneList<Expression*>* args = expr->arguments();
4147 DCHECK(args->length() == 1);
4149 VisitForAccumulatorValue(args->at(0));
4152 StringCharFromCodeGenerator generator(v0, a1);
4153 generator.GenerateFast(masm_);
4156 NopRuntimeCallHelper call_helper;
4157 generator.GenerateSlow(masm_, call_helper);
4160 context()->Plug(a1);
4164 void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
4165 ZoneList<Expression*>* args = expr->arguments();
4166 DCHECK(args->length() == 2);
4168 VisitForStackValue(args->at(0));
4169 VisitForAccumulatorValue(args->at(1));
4170 __ mov(a0, result_register());
4172 Register object = a1;
4173 Register index = a0;
4174 Register result = v0;
4178 Label need_conversion;
4179 Label index_out_of_range;
4181 StringCharCodeAtGenerator generator(object,
4186 &index_out_of_range,
4187 STRING_INDEX_IS_NUMBER);
4188 generator.GenerateFast(masm_);
4191 __ bind(&index_out_of_range);
4192 // When the index is out of range, the spec requires us to return
4194 __ LoadRoot(result, Heap::kNanValueRootIndex);
4197 __ bind(&need_conversion);
4198 // Load the undefined value into the result register, which will
4199 // trigger conversion.
4200 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
4203 NopRuntimeCallHelper call_helper;
4204 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
4207 context()->Plug(result);
4211 void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
4212 ZoneList<Expression*>* args = expr->arguments();
4213 DCHECK(args->length() == 2);
4215 VisitForStackValue(args->at(0));
4216 VisitForAccumulatorValue(args->at(1));
4217 __ mov(a0, result_register());
4219 Register object = a1;
4220 Register index = a0;
4221 Register scratch = a3;
4222 Register result = v0;
4226 Label need_conversion;
4227 Label index_out_of_range;
4229 StringCharAtGenerator generator(object,
4235 &index_out_of_range,
4236 STRING_INDEX_IS_NUMBER);
4237 generator.GenerateFast(masm_);
4240 __ bind(&index_out_of_range);
4241 // When the index is out of range, the spec requires us to return
4242 // the empty string.
4243 __ LoadRoot(result, Heap::kempty_stringRootIndex);
4246 __ bind(&need_conversion);
4247 // Move smi zero into the result register, which will trigger
4249 __ li(result, Operand(Smi::FromInt(0)));
4252 NopRuntimeCallHelper call_helper;
4253 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
4256 context()->Plug(result);
4260 void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
4261 ZoneList<Expression*>* args = expr->arguments();
4262 DCHECK_EQ(2, args->length());
4263 VisitForStackValue(args->at(0));
4264 VisitForAccumulatorValue(args->at(1));
4267 __ mov(a0, result_register()); // StringAddStub requires args in a0, a1.
4268 StringAddStub stub(isolate(), STRING_ADD_CHECK_BOTH, NOT_TENURED);
4270 context()->Plug(v0);
4274 void FullCodeGenerator::EmitStringCompare(CallRuntime* expr) {
4275 ZoneList<Expression*>* args = expr->arguments();
4276 DCHECK_EQ(2, args->length());
4278 VisitForStackValue(args->at(0));
4279 VisitForStackValue(args->at(1));
4281 StringCompareStub stub(isolate());
4283 context()->Plug(v0);
4287 void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
4288 ZoneList<Expression*>* args = expr->arguments();
4289 DCHECK(args->length() >= 2);
4291 int arg_count = args->length() - 2; // 2 ~ receiver and function.
4292 for (int i = 0; i < arg_count + 1; i++) {
4293 VisitForStackValue(args->at(i));
4295 VisitForAccumulatorValue(args->last()); // Function.
4297 Label runtime, done;
4298 // Check for non-function argument (including proxy).
4299 __ JumpIfSmi(v0, &runtime);
4300 __ GetObjectType(v0, a1, a1);
4301 __ Branch(&runtime, ne, a1, Operand(JS_FUNCTION_TYPE));
4303 // InvokeFunction requires the function in a1. Move it in there.
4304 __ mov(a1, result_register());
4305 ParameterCount count(arg_count);
4306 __ InvokeFunction(a1, count, CALL_FUNCTION, NullCallWrapper());
4307 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4312 __ CallRuntime(Runtime::kCall, args->length());
4315 context()->Plug(v0);
4319 void FullCodeGenerator::EmitDefaultConstructorCallSuper(CallRuntime* expr) {
4320 ZoneList<Expression*>* args = expr->arguments();
4321 DCHECK(args->length() == 2);
4324 VisitForStackValue(args->at(0));
4327 VisitForStackValue(args->at(1));
4328 __ CallRuntime(Runtime::kGetPrototype, 1);
4329 __ Push(result_register());
4331 // Load original constructor into t0.
4332 __ lw(t0, MemOperand(sp, 1 * kPointerSize));
4334 // Check if the calling frame is an arguments adaptor frame.
4335 Label adaptor_frame, args_set_up, runtime;
4336 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4337 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
4338 __ Branch(&adaptor_frame, eq, a3,
4339 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4340 // default constructor has no arguments, so no adaptor frame means no args.
4341 __ mov(a0, zero_reg);
4342 __ Branch(&args_set_up);
4344 // Copy arguments from adaptor frame.
4346 __ bind(&adaptor_frame);
4347 __ lw(a1, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4348 __ SmiUntag(a1, a1);
4352 // Get arguments pointer in a2.
4353 __ sll(at, a1, kPointerSizeLog2);
4354 __ addu(a2, a2, at);
4355 __ Addu(a2, a2, Operand(StandardFrameConstants::kCallerSPOffset));
4358 // Pre-decrement a2 with kPointerSize on each iteration.
4359 // Pre-decrement in order to skip receiver.
4360 __ Addu(a2, a2, Operand(-kPointerSize));
4361 __ lw(a3, MemOperand(a2));
4363 __ Addu(a1, a1, Operand(-1));
4364 __ Branch(&loop, ne, a1, Operand(zero_reg));
4367 __ bind(&args_set_up);
4368 __ sll(at, a0, kPointerSizeLog2);
4369 __ Addu(at, at, Operand(sp));
4370 __ lw(a1, MemOperand(at, 0));
4371 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4373 CallConstructStub stub(isolate(), SUPER_CONSTRUCTOR_CALL);
4374 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
4378 context()->Plug(result_register());
4382 void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
4383 RegExpConstructResultStub stub(isolate());
4384 ZoneList<Expression*>* args = expr->arguments();
4385 DCHECK(args->length() == 3);
4386 VisitForStackValue(args->at(0));
4387 VisitForStackValue(args->at(1));
4388 VisitForAccumulatorValue(args->at(2));
4389 __ mov(a0, result_register());
4393 context()->Plug(v0);
4397 void FullCodeGenerator::EmitGetFromCache(CallRuntime* expr) {
4398 ZoneList<Expression*>* args = expr->arguments();
4399 DCHECK_EQ(2, args->length());
4401 DCHECK_NOT_NULL(args->at(0)->AsLiteral());
4402 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->value()))->value();
4404 Handle<FixedArray> jsfunction_result_caches(
4405 isolate()->native_context()->jsfunction_result_caches());
4406 if (jsfunction_result_caches->length() <= cache_id) {
4407 __ Abort(kAttemptToUseUndefinedCache);
4408 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
4409 context()->Plug(v0);
4413 VisitForAccumulatorValue(args->at(1));
4416 Register cache = a1;
4417 __ lw(cache, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
4418 __ lw(cache, FieldMemOperand(cache, GlobalObject::kNativeContextOffset));
4421 cache, Context::JSFUNCTION_RESULT_CACHES_INDEX));
4423 FieldMemOperand(cache, FixedArray::OffsetOfElementAt(cache_id)));
4426 Label done, not_found;
4427 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
4428 __ lw(a2, FieldMemOperand(cache, JSFunctionResultCache::kFingerOffset));
4429 // a2 now holds finger offset as a smi.
4430 __ Addu(a3, cache, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4431 // a3 now points to the start of fixed array elements.
4432 __ sll(at, a2, kPointerSizeLog2 - kSmiTagSize);
4433 __ addu(a3, a3, at);
4434 // a3 now points to key of indexed element of cache.
4435 __ lw(a2, MemOperand(a3));
4436 __ Branch(¬_found, ne, key, Operand(a2));
4438 __ lw(v0, MemOperand(a3, kPointerSize));
4441 __ bind(¬_found);
4442 // Call runtime to perform the lookup.
4443 __ Push(cache, key);
4444 __ CallRuntime(Runtime::kGetFromCacheRT, 2);
4447 context()->Plug(v0);
4451 void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
4452 ZoneList<Expression*>* args = expr->arguments();
4453 VisitForAccumulatorValue(args->at(0));
4455 Label materialize_true, materialize_false;
4456 Label* if_true = NULL;
4457 Label* if_false = NULL;
4458 Label* fall_through = NULL;
4459 context()->PrepareTest(&materialize_true, &materialize_false,
4460 &if_true, &if_false, &fall_through);
4462 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
4463 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
4465 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4466 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
4468 context()->Plug(if_true, if_false);
4472 void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
4473 ZoneList<Expression*>* args = expr->arguments();
4474 DCHECK(args->length() == 1);
4475 VisitForAccumulatorValue(args->at(0));
4477 __ AssertString(v0);
4479 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
4480 __ IndexFromHash(v0, v0);
4482 context()->Plug(v0);
4486 void FullCodeGenerator::EmitFastOneByteArrayJoin(CallRuntime* expr) {
4487 Label bailout, done, one_char_separator, long_separator,
4488 non_trivial_array, not_size_one_array, loop,
4489 empty_separator_loop, one_char_separator_loop,
4490 one_char_separator_loop_entry, long_separator_loop;
4491 ZoneList<Expression*>* args = expr->arguments();
4492 DCHECK(args->length() == 2);
4493 VisitForStackValue(args->at(1));
4494 VisitForAccumulatorValue(args->at(0));
4496 // All aliases of the same register have disjoint lifetimes.
4497 Register array = v0;
4498 Register elements = no_reg; // Will be v0.
4499 Register result = no_reg; // Will be v0.
4500 Register separator = a1;
4501 Register array_length = a2;
4502 Register result_pos = no_reg; // Will be a2.
4503 Register string_length = a3;
4504 Register string = t0;
4505 Register element = t1;
4506 Register elements_end = t2;
4507 Register scratch1 = t3;
4508 Register scratch2 = t5;
4509 Register scratch3 = t4;
4511 // Separator operand is on the stack.
4514 // Check that the array is a JSArray.
4515 __ JumpIfSmi(array, &bailout);
4516 __ GetObjectType(array, scratch1, scratch2);
4517 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
4519 // Check that the array has fast elements.
4520 __ CheckFastElements(scratch1, scratch2, &bailout);
4522 // If the array has length zero, return the empty string.
4523 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
4524 __ SmiUntag(array_length);
4525 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
4526 __ LoadRoot(v0, Heap::kempty_stringRootIndex);
4529 __ bind(&non_trivial_array);
4531 // Get the FixedArray containing array's elements.
4533 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
4534 array = no_reg; // End of array's live range.
4536 // Check that all array elements are sequential one-byte strings, and
4537 // accumulate the sum of their lengths, as a smi-encoded value.
4538 __ mov(string_length, zero_reg);
4540 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4541 __ sll(elements_end, array_length, kPointerSizeLog2);
4542 __ Addu(elements_end, element, elements_end);
4543 // Loop condition: while (element < elements_end).
4544 // Live values in registers:
4545 // elements: Fixed array of strings.
4546 // array_length: Length of the fixed array of strings (not smi)
4547 // separator: Separator string
4548 // string_length: Accumulated sum of string lengths (smi).
4549 // element: Current array element.
4550 // elements_end: Array end.
4551 if (generate_debug_code_) {
4552 __ Assert(gt, kNoEmptyArraysHereInEmitFastOneByteArrayJoin, array_length,
4556 __ lw(string, MemOperand(element));
4557 __ Addu(element, element, kPointerSize);
4558 __ JumpIfSmi(string, &bailout);
4559 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
4560 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
4561 __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
4562 __ lw(scratch1, FieldMemOperand(string, SeqOneByteString::kLengthOffset));
4563 __ AdduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
4564 __ BranchOnOverflow(&bailout, scratch3);
4565 __ Branch(&loop, lt, element, Operand(elements_end));
4567 // If array_length is 1, return elements[0], a string.
4568 __ Branch(¬_size_one_array, ne, array_length, Operand(1));
4569 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
4572 __ bind(¬_size_one_array);
4574 // Live values in registers:
4575 // separator: Separator string
4576 // array_length: Length of the array.
4577 // string_length: Sum of string lengths (smi).
4578 // elements: FixedArray of strings.
4580 // Check that the separator is a flat one-byte string.
4581 __ JumpIfSmi(separator, &bailout);
4582 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
4583 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
4584 __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
4586 // Add (separator length times array_length) - separator length to the
4587 // string_length to get the length of the result string. array_length is not
4588 // smi but the other values are, so the result is a smi.
4589 __ lw(scratch1, FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
4590 __ Subu(string_length, string_length, Operand(scratch1));
4591 __ Mul(scratch3, scratch2, array_length, scratch1);
4592 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
4594 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
4595 __ And(scratch3, scratch2, Operand(0x80000000));
4596 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
4597 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
4598 __ BranchOnOverflow(&bailout, scratch3);
4599 __ SmiUntag(string_length);
4601 // Get first element in the array to free up the elements register to be used
4604 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4605 result = elements; // End of live range for elements.
4607 // Live values in registers:
4608 // element: First array element
4609 // separator: Separator string
4610 // string_length: Length of result string (not smi)
4611 // array_length: Length of the array.
4612 __ AllocateOneByteString(result, string_length, scratch1, scratch2,
4613 elements_end, &bailout);
4614 // Prepare for looping. Set up elements_end to end of the array. Set
4615 // result_pos to the position of the result where to write the first
4617 __ sll(elements_end, array_length, kPointerSizeLog2);
4618 __ Addu(elements_end, element, elements_end);
4619 result_pos = array_length; // End of live range for array_length.
4620 array_length = no_reg;
4623 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4625 // Check the length of the separator.
4626 __ lw(scratch1, FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
4627 __ li(at, Operand(Smi::FromInt(1)));
4628 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
4629 __ Branch(&long_separator, gt, scratch1, Operand(at));
4631 // Empty separator case.
4632 __ bind(&empty_separator_loop);
4633 // Live values in registers:
4634 // result_pos: the position to which we are currently copying characters.
4635 // element: Current array element.
4636 // elements_end: Array end.
4638 // Copy next array element to the result.
4639 __ lw(string, MemOperand(element));
4640 __ Addu(element, element, kPointerSize);
4641 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
4642 __ SmiUntag(string_length);
4643 __ Addu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4644 __ CopyBytes(string, result_pos, string_length, scratch1);
4645 // End while (element < elements_end).
4646 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
4647 DCHECK(result.is(v0));
4650 // One-character separator case.
4651 __ bind(&one_char_separator);
4652 // Replace separator with its one-byte character value.
4653 __ lbu(separator, FieldMemOperand(separator, SeqOneByteString::kHeaderSize));
4654 // Jump into the loop after the code that copies the separator, so the first
4655 // element is not preceded by a separator.
4656 __ jmp(&one_char_separator_loop_entry);
4658 __ bind(&one_char_separator_loop);
4659 // Live values in registers:
4660 // result_pos: the position to which we are currently copying characters.
4661 // element: Current array element.
4662 // elements_end: Array end.
4663 // separator: Single separator one-byte char (in lower byte).
4665 // Copy the separator character to the result.
4666 __ sb(separator, MemOperand(result_pos));
4667 __ Addu(result_pos, result_pos, 1);
4669 // Copy next array element to the result.
4670 __ bind(&one_char_separator_loop_entry);
4671 __ lw(string, MemOperand(element));
4672 __ Addu(element, element, kPointerSize);
4673 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
4674 __ SmiUntag(string_length);
4675 __ Addu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4676 __ CopyBytes(string, result_pos, string_length, scratch1);
4677 // End while (element < elements_end).
4678 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
4679 DCHECK(result.is(v0));
4682 // Long separator case (separator is more than one character). Entry is at the
4683 // label long_separator below.
4684 __ bind(&long_separator_loop);
4685 // Live values in registers:
4686 // result_pos: the position to which we are currently copying characters.
4687 // element: Current array element.
4688 // elements_end: Array end.
4689 // separator: Separator string.
4691 // Copy the separator to the result.
4692 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
4693 __ SmiUntag(string_length);
4696 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4697 __ CopyBytes(string, result_pos, string_length, scratch1);
4699 __ bind(&long_separator);
4700 __ lw(string, MemOperand(element));
4701 __ Addu(element, element, kPointerSize);
4702 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
4703 __ SmiUntag(string_length);
4704 __ Addu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4705 __ CopyBytes(string, result_pos, string_length, scratch1);
4706 // End while (element < elements_end).
4707 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
4708 DCHECK(result.is(v0));
4712 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
4714 context()->Plug(v0);
4718 void FullCodeGenerator::EmitDebugIsActive(CallRuntime* expr) {
4719 DCHECK(expr->arguments()->length() == 0);
4720 ExternalReference debug_is_active =
4721 ExternalReference::debug_is_active_address(isolate());
4722 __ li(at, Operand(debug_is_active));
4723 __ lb(v0, MemOperand(at));
4725 context()->Plug(v0);
4729 void FullCodeGenerator::EmitCallSuperWithSpread(CallRuntime* expr) {
4730 // Assert: expr == CallRuntime("ReflectConstruct")
4731 DCHECK_EQ(1, expr->arguments()->length());
4732 CallRuntime* call = expr->arguments()->at(0)->AsCallRuntime();
4734 ZoneList<Expression*>* args = call->arguments();
4735 DCHECK_EQ(3, args->length());
4737 SuperCallReference* super_call_ref = args->at(0)->AsSuperCallReference();
4738 DCHECK_NOT_NULL(super_call_ref);
4740 // Load ReflectConstruct function
4741 EmitLoadJSRuntimeFunction(call);
4743 // Push the target function under the receiver
4744 __ lw(at, MemOperand(sp, 0));
4746 __ sw(v0, MemOperand(sp, kPointerSize));
4748 // Push super constructor
4749 EmitLoadSuperConstructor(super_call_ref);
4750 __ Push(result_register());
4752 // Push arguments array
4753 VisitForStackValue(args->at(1));
4756 DCHECK(args->at(2)->IsVariableProxy());
4757 VisitForStackValue(args->at(2));
4759 EmitCallJSRuntimeFunction(call);
4761 // Restore context register.
4762 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4763 context()->DropAndPlug(1, v0);
4765 // TODO(mvstanton): with FLAG_vector_stores this needs a slot id.
4766 EmitInitializeThisAfterSuper(super_call_ref);
4770 void FullCodeGenerator::EmitLoadJSRuntimeFunction(CallRuntime* expr) {
4771 // Push the builtins object as the receiver.
4772 Register receiver = LoadDescriptor::ReceiverRegister();
4773 __ lw(receiver, GlobalObjectOperand());
4774 __ lw(receiver, FieldMemOperand(receiver, GlobalObject::kBuiltinsOffset));
4777 // Load the function from the receiver.
4778 __ li(LoadDescriptor::NameRegister(), Operand(expr->name()));
4779 __ li(LoadDescriptor::SlotRegister(),
4780 Operand(SmiFromSlot(expr->CallRuntimeFeedbackSlot())));
4781 CallLoadIC(NOT_INSIDE_TYPEOF);
4785 void FullCodeGenerator::EmitCallJSRuntimeFunction(CallRuntime* expr) {
4786 ZoneList<Expression*>* args = expr->arguments();
4787 int arg_count = args->length();
4789 SetCallPosition(expr, arg_count);
4790 CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
4791 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
4796 void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
4797 ZoneList<Expression*>* args = expr->arguments();
4798 int arg_count = args->length();
4800 if (expr->is_jsruntime()) {
4801 Comment cmnt(masm_, "[ CallRuntime");
4802 EmitLoadJSRuntimeFunction(expr);
4804 // Push the target function under the receiver.
4805 __ lw(at, MemOperand(sp, 0));
4807 __ sw(v0, MemOperand(sp, kPointerSize));
4809 // Push the arguments ("left-to-right").
4810 for (int i = 0; i < arg_count; i++) {
4811 VisitForStackValue(args->at(i));
4814 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4815 EmitCallJSRuntimeFunction(expr);
4817 // Restore context register.
4818 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4820 context()->DropAndPlug(1, v0);
4823 const Runtime::Function* function = expr->function();
4824 switch (function->function_id) {
4825 #define CALL_INTRINSIC_GENERATOR(Name) \
4826 case Runtime::kInline##Name: { \
4827 Comment cmnt(masm_, "[ Inline" #Name); \
4828 return Emit##Name(expr); \
4830 FOR_EACH_FULL_CODE_INTRINSIC(CALL_INTRINSIC_GENERATOR)
4831 #undef CALL_INTRINSIC_GENERATOR
4833 Comment cmnt(masm_, "[ CallRuntime for unhandled intrinsic");
4834 // Push the arguments ("left-to-right").
4835 for (int i = 0; i < arg_count; i++) {
4836 VisitForStackValue(args->at(i));
4839 // Call the C runtime function.
4840 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4841 __ CallRuntime(expr->function(), arg_count);
4842 context()->Plug(v0);
4849 void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
4850 switch (expr->op()) {
4851 case Token::DELETE: {
4852 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
4853 Property* property = expr->expression()->AsProperty();
4854 VariableProxy* proxy = expr->expression()->AsVariableProxy();
4856 if (property != NULL) {
4857 VisitForStackValue(property->obj());
4858 VisitForStackValue(property->key());
4859 __ li(a1, Operand(Smi::FromInt(language_mode())));
4861 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4862 context()->Plug(v0);
4863 } else if (proxy != NULL) {
4864 Variable* var = proxy->var();
4865 // Delete of an unqualified identifier is disallowed in strict mode but
4866 // "delete this" is allowed.
4867 bool is_this = var->HasThisName(isolate());
4868 DCHECK(is_sloppy(language_mode()) || is_this);
4869 if (var->IsUnallocatedOrGlobalSlot()) {
4870 __ lw(a2, GlobalObjectOperand());
4871 __ li(a1, Operand(var->name()));
4872 __ li(a0, Operand(Smi::FromInt(SLOPPY)));
4873 __ Push(a2, a1, a0);
4874 __ InvokeBuiltin(Builtins::DELETE, CALL_FUNCTION);
4875 context()->Plug(v0);
4876 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
4877 // Result of deleting non-global, non-dynamic variables is false.
4878 // The subexpression does not have side effects.
4879 context()->Plug(is_this);
4881 // Non-global variable. Call the runtime to try to delete from the
4882 // context where the variable was introduced.
4883 DCHECK(!context_register().is(a2));
4884 __ li(a2, Operand(var->name()));
4885 __ Push(context_register(), a2);
4886 __ CallRuntime(Runtime::kDeleteLookupSlot, 2);
4887 context()->Plug(v0);
4890 // Result of deleting non-property, non-variable reference is true.
4891 // The subexpression may have side effects.
4892 VisitForEffect(expr->expression());
4893 context()->Plug(true);
4899 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
4900 VisitForEffect(expr->expression());
4901 context()->Plug(Heap::kUndefinedValueRootIndex);
4906 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
4907 if (context()->IsEffect()) {
4908 // Unary NOT has no side effects so it's only necessary to visit the
4909 // subexpression. Match the optimizing compiler by not branching.
4910 VisitForEffect(expr->expression());
4911 } else if (context()->IsTest()) {
4912 const TestContext* test = TestContext::cast(context());
4913 // The labels are swapped for the recursive call.
4914 VisitForControl(expr->expression(),
4915 test->false_label(),
4917 test->fall_through());
4918 context()->Plug(test->true_label(), test->false_label());
4920 // We handle value contexts explicitly rather than simply visiting
4921 // for control and plugging the control flow into the context,
4922 // because we need to prepare a pair of extra administrative AST ids
4923 // for the optimizing compiler.
4924 DCHECK(context()->IsAccumulatorValue() || context()->IsStackValue());
4925 Label materialize_true, materialize_false, done;
4926 VisitForControl(expr->expression(),
4930 __ bind(&materialize_true);
4931 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
4932 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
4933 if (context()->IsStackValue()) __ push(v0);
4935 __ bind(&materialize_false);
4936 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
4937 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
4938 if (context()->IsStackValue()) __ push(v0);
4944 case Token::TYPEOF: {
4945 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
4947 AccumulatorValueContext context(this);
4948 VisitForTypeofValue(expr->expression());
4951 TypeofStub typeof_stub(isolate());
4952 __ CallStub(&typeof_stub);
4953 context()->Plug(v0);
4963 void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
4964 DCHECK(expr->expression()->IsValidReferenceExpression());
4966 Comment cmnt(masm_, "[ CountOperation");
4968 Property* prop = expr->expression()->AsProperty();
4969 LhsKind assign_type = Property::GetAssignType(prop);
4971 // Evaluate expression and get value.
4972 if (assign_type == VARIABLE) {
4973 DCHECK(expr->expression()->AsVariableProxy()->var() != NULL);
4974 AccumulatorValueContext context(this);
4975 EmitVariableLoad(expr->expression()->AsVariableProxy());
4977 // Reserve space for result of postfix operation.
4978 if (expr->is_postfix() && !context()->IsEffect()) {
4979 __ li(at, Operand(Smi::FromInt(0)));
4982 switch (assign_type) {
4983 case NAMED_PROPERTY: {
4984 // Put the object both on the stack and in the register.
4985 VisitForStackValue(prop->obj());
4986 __ lw(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
4987 EmitNamedPropertyLoad(prop);
4991 case NAMED_SUPER_PROPERTY: {
4992 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4993 VisitForAccumulatorValue(
4994 prop->obj()->AsSuperPropertyReference()->home_object());
4995 __ Push(result_register());
4996 const Register scratch = a1;
4997 __ lw(scratch, MemOperand(sp, kPointerSize));
4998 __ Push(scratch, result_register());
4999 EmitNamedSuperPropertyLoad(prop);
5003 case KEYED_SUPER_PROPERTY: {
5004 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
5005 VisitForAccumulatorValue(
5006 prop->obj()->AsSuperPropertyReference()->home_object());
5007 const Register scratch = a1;
5008 const Register scratch1 = t0;
5009 __ Move(scratch, result_register());
5010 VisitForAccumulatorValue(prop->key());
5011 __ Push(scratch, result_register());
5012 __ lw(scratch1, MemOperand(sp, 2 * kPointerSize));
5013 __ Push(scratch1, scratch, result_register());
5014 EmitKeyedSuperPropertyLoad(prop);
5018 case KEYED_PROPERTY: {
5019 VisitForStackValue(prop->obj());
5020 VisitForStackValue(prop->key());
5021 __ lw(LoadDescriptor::ReceiverRegister(),
5022 MemOperand(sp, 1 * kPointerSize));
5023 __ lw(LoadDescriptor::NameRegister(), MemOperand(sp, 0));
5024 EmitKeyedPropertyLoad(prop);
5033 // We need a second deoptimization point after loading the value
5034 // in case evaluating the property load my have a side effect.
5035 if (assign_type == VARIABLE) {
5036 PrepareForBailout(expr->expression(), TOS_REG);
5038 PrepareForBailoutForId(prop->LoadId(), TOS_REG);
5041 // Inline smi case if we are in a loop.
5042 Label stub_call, done;
5043 JumpPatchSite patch_site(masm_);
5045 int count_value = expr->op() == Token::INC ? 1 : -1;
5047 if (ShouldInlineSmiCase(expr->op())) {
5049 patch_site.EmitJumpIfNotSmi(v0, &slow);
5051 // Save result for postfix expressions.
5052 if (expr->is_postfix()) {
5053 if (!context()->IsEffect()) {
5054 // Save the result on the stack. If we have a named or keyed property
5055 // we store the result under the receiver that is currently on top
5057 switch (assign_type) {
5061 case NAMED_PROPERTY:
5062 __ sw(v0, MemOperand(sp, kPointerSize));
5064 case NAMED_SUPER_PROPERTY:
5065 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
5067 case KEYED_PROPERTY:
5068 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
5070 case KEYED_SUPER_PROPERTY:
5071 __ sw(v0, MemOperand(sp, 3 * kPointerSize));
5077 Register scratch1 = a1;
5078 Register scratch2 = t0;
5079 __ li(scratch1, Operand(Smi::FromInt(count_value)));
5080 __ AdduAndCheckForOverflow(v0, v0, scratch1, scratch2);
5081 __ BranchOnNoOverflow(&done, scratch2);
5082 // Call stub. Undo operation first.
5087 if (!is_strong(language_mode())) {
5088 ToNumberStub convert_stub(isolate());
5089 __ CallStub(&convert_stub);
5090 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
5093 // Save result for postfix expressions.
5094 if (expr->is_postfix()) {
5095 if (!context()->IsEffect()) {
5096 // Save the result on the stack. If we have a named or keyed property
5097 // we store the result under the receiver that is currently on top
5099 switch (assign_type) {
5103 case NAMED_PROPERTY:
5104 __ sw(v0, MemOperand(sp, kPointerSize));
5106 case NAMED_SUPER_PROPERTY:
5107 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
5109 case KEYED_PROPERTY:
5110 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
5112 case KEYED_SUPER_PROPERTY:
5113 __ sw(v0, MemOperand(sp, 3 * kPointerSize));
5119 __ bind(&stub_call);
5121 __ li(a0, Operand(Smi::FromInt(count_value)));
5123 SetExpressionPosition(expr);
5126 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), Token::ADD,
5127 strength(language_mode())).code();
5128 CallIC(code, expr->CountBinOpFeedbackId());
5129 patch_site.EmitPatchInfo();
5132 if (is_strong(language_mode())) {
5133 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
5135 // Store the value returned in v0.
5136 switch (assign_type) {
5138 if (expr->is_postfix()) {
5139 { EffectContext context(this);
5140 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
5141 Token::ASSIGN, expr->CountSlot());
5142 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5145 // For all contexts except EffectConstant we have the result on
5146 // top of the stack.
5147 if (!context()->IsEffect()) {
5148 context()->PlugTOS();
5151 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
5152 Token::ASSIGN, expr->CountSlot());
5153 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5154 context()->Plug(v0);
5157 case NAMED_PROPERTY: {
5158 __ mov(StoreDescriptor::ValueRegister(), result_register());
5159 __ li(StoreDescriptor::NameRegister(),
5160 Operand(prop->key()->AsLiteral()->value()));
5161 __ pop(StoreDescriptor::ReceiverRegister());
5162 if (FLAG_vector_stores) {
5163 EmitLoadStoreICSlot(expr->CountSlot());
5166 CallStoreIC(expr->CountStoreFeedbackId());
5168 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5169 if (expr->is_postfix()) {
5170 if (!context()->IsEffect()) {
5171 context()->PlugTOS();
5174 context()->Plug(v0);
5178 case NAMED_SUPER_PROPERTY: {
5179 EmitNamedSuperPropertyStore(prop);
5180 if (expr->is_postfix()) {
5181 if (!context()->IsEffect()) {
5182 context()->PlugTOS();
5185 context()->Plug(v0);
5189 case KEYED_SUPER_PROPERTY: {
5190 EmitKeyedSuperPropertyStore(prop);
5191 if (expr->is_postfix()) {
5192 if (!context()->IsEffect()) {
5193 context()->PlugTOS();
5196 context()->Plug(v0);
5200 case KEYED_PROPERTY: {
5201 __ mov(StoreDescriptor::ValueRegister(), result_register());
5202 __ Pop(StoreDescriptor::ReceiverRegister(),
5203 StoreDescriptor::NameRegister());
5205 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
5206 if (FLAG_vector_stores) {
5207 EmitLoadStoreICSlot(expr->CountSlot());
5210 CallIC(ic, expr->CountStoreFeedbackId());
5212 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
5213 if (expr->is_postfix()) {
5214 if (!context()->IsEffect()) {
5215 context()->PlugTOS();
5218 context()->Plug(v0);
5226 void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
5227 Expression* sub_expr,
5228 Handle<String> check) {
5229 Label materialize_true, materialize_false;
5230 Label* if_true = NULL;
5231 Label* if_false = NULL;
5232 Label* fall_through = NULL;
5233 context()->PrepareTest(&materialize_true, &materialize_false,
5234 &if_true, &if_false, &fall_through);
5236 { AccumulatorValueContext context(this);
5237 VisitForTypeofValue(sub_expr);
5239 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5241 Factory* factory = isolate()->factory();
5242 if (String::Equals(check, factory->number_string())) {
5243 __ JumpIfSmi(v0, if_true);
5244 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
5245 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
5246 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
5247 } else if (String::Equals(check, factory->string_string())) {
5248 __ JumpIfSmi(v0, if_false);
5249 // Check for undetectable objects => false.
5250 __ GetObjectType(v0, v0, a1);
5251 __ Branch(if_false, ge, a1, Operand(FIRST_NONSTRING_TYPE));
5252 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
5253 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
5254 Split(eq, a1, Operand(zero_reg),
5255 if_true, if_false, fall_through);
5256 } else if (String::Equals(check, factory->symbol_string())) {
5257 __ JumpIfSmi(v0, if_false);
5258 __ GetObjectType(v0, v0, a1);
5259 Split(eq, a1, Operand(SYMBOL_TYPE), if_true, if_false, fall_through);
5260 } else if (String::Equals(check, factory->float32x4_string())) {
5261 __ JumpIfSmi(v0, if_false);
5262 __ GetObjectType(v0, v0, a1);
5263 Split(eq, a1, Operand(FLOAT32X4_TYPE), if_true, if_false, fall_through);
5264 } else if (String::Equals(check, factory->boolean_string())) {
5265 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5266 __ Branch(if_true, eq, v0, Operand(at));
5267 __ LoadRoot(at, Heap::kFalseValueRootIndex);
5268 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
5269 } else if (String::Equals(check, factory->undefined_string())) {
5270 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5271 __ Branch(if_true, eq, v0, Operand(at));
5272 __ JumpIfSmi(v0, if_false);
5273 // Check for undetectable objects => true.
5274 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
5275 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
5276 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
5277 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
5278 } else if (String::Equals(check, factory->function_string())) {
5279 __ JumpIfSmi(v0, if_false);
5280 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5281 __ GetObjectType(v0, v0, a1);
5282 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
5283 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
5284 if_true, if_false, fall_through);
5285 } else if (String::Equals(check, factory->object_string())) {
5286 __ JumpIfSmi(v0, if_false);
5287 __ LoadRoot(at, Heap::kNullValueRootIndex);
5288 __ Branch(if_true, eq, v0, Operand(at));
5289 // Check for JS objects => true.
5290 __ GetObjectType(v0, v0, a1);
5291 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
5292 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
5293 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
5294 // Check for undetectable objects => false.
5295 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
5296 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
5297 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
5299 if (if_false != fall_through) __ jmp(if_false);
5301 context()->Plug(if_true, if_false);
5305 void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
5306 Comment cmnt(masm_, "[ CompareOperation");
5307 SetExpressionPosition(expr);
5309 // First we try a fast inlined version of the compare when one of
5310 // the operands is a literal.
5311 if (TryLiteralCompare(expr)) return;
5313 // Always perform the comparison for its control flow. Pack the result
5314 // into the expression's context after the comparison is performed.
5315 Label materialize_true, materialize_false;
5316 Label* if_true = NULL;
5317 Label* if_false = NULL;
5318 Label* fall_through = NULL;
5319 context()->PrepareTest(&materialize_true, &materialize_false,
5320 &if_true, &if_false, &fall_through);
5322 Token::Value op = expr->op();
5323 VisitForStackValue(expr->left());
5326 VisitForStackValue(expr->right());
5327 __ InvokeBuiltin(Builtins::IN, CALL_FUNCTION);
5328 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
5329 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
5330 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
5333 case Token::INSTANCEOF: {
5334 VisitForStackValue(expr->right());
5335 InstanceofStub stub(isolate(), InstanceofStub::kNoFlags);
5337 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5338 // The stub returns 0 for true.
5339 Split(eq, v0, Operand(zero_reg), if_true, if_false, fall_through);
5344 VisitForAccumulatorValue(expr->right());
5345 Condition cc = CompareIC::ComputeCondition(op);
5346 __ mov(a0, result_register());
5349 bool inline_smi_code = ShouldInlineSmiCase(op);
5350 JumpPatchSite patch_site(masm_);
5351 if (inline_smi_code) {
5353 __ Or(a2, a0, Operand(a1));
5354 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
5355 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
5356 __ bind(&slow_case);
5359 Handle<Code> ic = CodeFactory::CompareIC(
5360 isolate(), op, strength(language_mode())).code();
5361 CallIC(ic, expr->CompareOperationFeedbackId());
5362 patch_site.EmitPatchInfo();
5363 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5364 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
5368 // Convert the result of the comparison into one expected for this
5369 // expression's context.
5370 context()->Plug(if_true, if_false);
5374 void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
5375 Expression* sub_expr,
5377 Label materialize_true, materialize_false;
5378 Label* if_true = NULL;
5379 Label* if_false = NULL;
5380 Label* fall_through = NULL;
5381 context()->PrepareTest(&materialize_true, &materialize_false,
5382 &if_true, &if_false, &fall_through);
5384 VisitForAccumulatorValue(sub_expr);
5385 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5386 __ mov(a0, result_register());
5387 if (expr->op() == Token::EQ_STRICT) {
5388 Heap::RootListIndex nil_value = nil == kNullValue ?
5389 Heap::kNullValueRootIndex :
5390 Heap::kUndefinedValueRootIndex;
5391 __ LoadRoot(a1, nil_value);
5392 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
5394 Handle<Code> ic = CompareNilICStub::GetUninitialized(isolate(), nil);
5395 CallIC(ic, expr->CompareOperationFeedbackId());
5396 Split(ne, v0, Operand(zero_reg), if_true, if_false, fall_through);
5398 context()->Plug(if_true, if_false);
5402 void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
5403 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5404 context()->Plug(v0);
5408 Register FullCodeGenerator::result_register() {
5413 Register FullCodeGenerator::context_register() {
5418 void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
5419 DCHECK_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
5420 __ sw(value, MemOperand(fp, frame_offset));
5424 void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
5425 __ lw(dst, ContextOperand(cp, context_index));
5429 void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
5430 Scope* declaration_scope = scope()->DeclarationScope();
5431 if (declaration_scope->is_script_scope() ||
5432 declaration_scope->is_module_scope()) {
5433 // Contexts nested in the native context have a canonical empty function
5434 // as their closure, not the anonymous closure containing the global
5435 // code. Pass a smi sentinel and let the runtime look up the empty
5437 __ li(at, Operand(Smi::FromInt(0)));
5438 } else if (declaration_scope->is_eval_scope()) {
5439 // Contexts created by a call to eval have the same closure as the
5440 // context calling eval, not the anonymous closure containing the eval
5441 // code. Fetch it from the context.
5442 __ lw(at, ContextOperand(cp, Context::CLOSURE_INDEX));
5444 DCHECK(declaration_scope->is_function_scope());
5445 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5451 // ----------------------------------------------------------------------------
5452 // Non-local control flow support.
5454 void FullCodeGenerator::EnterFinallyBlock() {
5455 DCHECK(!result_register().is(a1));
5456 // Store result register while executing finally block.
5457 __ push(result_register());
5458 // Cook return address in link register to stack (smi encoded Code* delta).
5459 __ Subu(a1, ra, Operand(masm_->CodeObject()));
5460 DCHECK_EQ(1, kSmiTagSize + kSmiShiftSize);
5461 STATIC_ASSERT(0 == kSmiTag);
5462 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
5464 // Store result register while executing finally block.
5467 // Store pending message while executing finally block.
5468 ExternalReference pending_message_obj =
5469 ExternalReference::address_of_pending_message_obj(isolate());
5470 __ li(at, Operand(pending_message_obj));
5471 __ lw(a1, MemOperand(at));
5474 ClearPendingMessage();
5478 void FullCodeGenerator::ExitFinallyBlock() {
5479 DCHECK(!result_register().is(a1));
5480 // Restore pending message from stack.
5482 ExternalReference pending_message_obj =
5483 ExternalReference::address_of_pending_message_obj(isolate());
5484 __ li(at, Operand(pending_message_obj));
5485 __ sw(a1, MemOperand(at));
5487 // Restore result register from stack.
5490 // Uncook return address and return.
5491 __ pop(result_register());
5492 DCHECK_EQ(1, kSmiTagSize + kSmiShiftSize);
5493 __ sra(a1, a1, 1); // Un-smi-tag value.
5494 __ Addu(at, a1, Operand(masm_->CodeObject()));
5499 void FullCodeGenerator::ClearPendingMessage() {
5500 DCHECK(!result_register().is(a1));
5501 ExternalReference pending_message_obj =
5502 ExternalReference::address_of_pending_message_obj(isolate());
5503 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
5504 __ li(at, Operand(pending_message_obj));
5505 __ sw(a1, MemOperand(at));
5509 void FullCodeGenerator::EmitLoadStoreICSlot(FeedbackVectorICSlot slot) {
5510 DCHECK(FLAG_vector_stores && !slot.IsInvalid());
5511 __ li(VectorStoreICTrampolineDescriptor::SlotRegister(),
5512 Operand(SmiFromSlot(slot)));
5519 void BackEdgeTable::PatchAt(Code* unoptimized_code,
5521 BackEdgeState target_state,
5522 Code* replacement_code) {
5523 static const int kInstrSize = Assembler::kInstrSize;
5524 Address branch_address = pc - 6 * kInstrSize;
5525 CodePatcher patcher(branch_address, 1);
5527 switch (target_state) {
5529 // slt at, a3, zero_reg (in case of count based interrupts)
5530 // beq at, zero_reg, ok
5531 // lui t9, <interrupt stub address> upper
5532 // ori t9, <interrupt stub address> lower
5535 // ok-label ----- pc_after points here
5536 patcher.masm()->slt(at, a3, zero_reg);
5538 case ON_STACK_REPLACEMENT:
5539 case OSR_AFTER_STACK_CHECK:
5540 // addiu at, zero_reg, 1
5541 // beq at, zero_reg, ok ;; Not changed
5542 // lui t9, <on-stack replacement address> upper
5543 // ori t9, <on-stack replacement address> lower
5544 // jalr t9 ;; Not changed
5545 // nop ;; Not changed
5546 // ok-label ----- pc_after points here
5547 patcher.masm()->addiu(at, zero_reg, 1);
5550 Address pc_immediate_load_address = pc - 4 * kInstrSize;
5551 // Replace the stack check address in the load-immediate (lui/ori pair)
5552 // with the entry address of the replacement code.
5553 Assembler::set_target_address_at(pc_immediate_load_address,
5554 replacement_code->entry());
5556 unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
5557 unoptimized_code, pc_immediate_load_address, replacement_code);
5561 BackEdgeTable::BackEdgeState BackEdgeTable::GetBackEdgeState(
5563 Code* unoptimized_code,
5565 static const int kInstrSize = Assembler::kInstrSize;
5566 Address branch_address = pc - 6 * kInstrSize;
5567 Address pc_immediate_load_address = pc - 4 * kInstrSize;
5569 DCHECK(Assembler::IsBeq(Assembler::instr_at(pc - 5 * kInstrSize)));
5570 if (!Assembler::IsAddImmediate(Assembler::instr_at(branch_address))) {
5571 DCHECK(reinterpret_cast<uint32_t>(
5572 Assembler::target_address_at(pc_immediate_load_address)) ==
5573 reinterpret_cast<uint32_t>(
5574 isolate->builtins()->InterruptCheck()->entry()));
5578 DCHECK(Assembler::IsAddImmediate(Assembler::instr_at(branch_address)));
5580 if (reinterpret_cast<uint32_t>(
5581 Assembler::target_address_at(pc_immediate_load_address)) ==
5582 reinterpret_cast<uint32_t>(
5583 isolate->builtins()->OnStackReplacement()->entry())) {
5584 return ON_STACK_REPLACEMENT;
5587 DCHECK(reinterpret_cast<uint32_t>(
5588 Assembler::target_address_at(pc_immediate_load_address)) ==
5589 reinterpret_cast<uint32_t>(
5590 isolate->builtins()->OsrAfterStackCheck()->entry()));
5591 return OSR_AFTER_STACK_CHECK;
5595 } // namespace internal
5598 #endif // V8_TARGET_ARCH_MIPS