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.
5 #if V8_TARGET_ARCH_MIPS64
7 // Note on Mips implementation:
9 // The result_register() for mips is the 'v0' register, which is defined
10 // by the ABI to contain function return values. However, the first
11 // parameter to a function is defined to be 'a0'. So there are many
12 // places where we have to move a previous result in v0 to a0 for the
13 // next call: mov(a0, v0). This is not needed on the other architectures.
15 #include "src/code-factory.h"
16 #include "src/code-stubs.h"
17 #include "src/codegen.h"
18 #include "src/compiler.h"
19 #include "src/debug/debug.h"
20 #include "src/full-codegen/full-codegen.h"
21 #include "src/ic/ic.h"
22 #include "src/parser.h"
23 #include "src/scopes.h"
25 #include "src/mips64/code-stubs-mips64.h"
26 #include "src/mips64/macro-assembler-mips64.h"
31 #define __ ACCESS_MASM(masm_)
34 // A patch site is a location in the code which it is possible to patch. This
35 // class has a number of methods to emit the code which is patchable and the
36 // method EmitPatchInfo to record a marker back to the patchable code. This
37 // marker is a andi zero_reg, rx, #yyyy instruction, and rx * 0x0000ffff + yyyy
38 // (raw 16 bit immediate value is used) is the delta from the pc to the first
39 // instruction of the patchable code.
40 // The marker instruction is effectively a NOP (dest is zero_reg) and will
41 // never be emitted by normal code.
42 class JumpPatchSite BASE_EMBEDDED {
44 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
46 info_emitted_ = false;
51 DCHECK(patch_site_.is_bound() == info_emitted_);
54 // When initially emitting this ensure that a jump is always generated to skip
55 // the inlined smi code.
56 void EmitJumpIfNotSmi(Register reg, Label* target) {
57 DCHECK(!patch_site_.is_bound() && !info_emitted_);
58 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
59 __ bind(&patch_site_);
61 // Always taken before patched.
62 __ BranchShort(target, eq, at, Operand(zero_reg));
65 // When initially emitting this ensure that a jump is never generated to skip
66 // the inlined smi code.
67 void EmitJumpIfSmi(Register reg, Label* target) {
68 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
69 DCHECK(!patch_site_.is_bound() && !info_emitted_);
70 __ bind(&patch_site_);
72 // Never taken before patched.
73 __ BranchShort(target, ne, at, Operand(zero_reg));
76 void EmitPatchInfo() {
77 if (patch_site_.is_bound()) {
78 int delta_to_patch_site = masm_->InstructionsGeneratedSince(&patch_site_);
79 Register reg = Register::from_code(delta_to_patch_site / kImm16Mask);
80 __ andi(zero_reg, reg, delta_to_patch_site % kImm16Mask);
85 __ nop(); // Signals no inlined code.
90 MacroAssembler* masm_;
98 // Generate code for a JS function. On entry to the function the receiver
99 // and arguments have been pushed on the stack left to right. The actual
100 // argument count matches the formal parameter count expected by the
103 // The live registers are:
104 // o a1: the JS function object being called (i.e. ourselves)
106 // o fp: our caller's frame pointer
107 // o sp: stack pointer
108 // o ra: return address
110 // The function builds a JS frame. Please see JavaScriptFrameConstants in
111 // frames-mips.h for its layout.
112 void FullCodeGenerator::Generate() {
113 CompilationInfo* info = info_;
114 profiling_counter_ = isolate()->factory()->NewCell(
115 Handle<Smi>(Smi::FromInt(FLAG_interrupt_budget), isolate()));
116 SetFunctionPosition(literal());
117 Comment cmnt(masm_, "[ function compiled by full code generator");
119 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
122 if (strlen(FLAG_stop_at) > 0 &&
123 info->literal()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
128 // Sloppy mode functions and builtins need to replace the receiver with the
129 // global proxy when called as functions (without an explicit receiver
131 if (info->MustReplaceUndefinedReceiverWithGlobalProxy()) {
133 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
134 __ ld(at, MemOperand(sp, receiver_offset));
135 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
136 __ Branch(&ok, ne, a2, Operand(at));
138 __ ld(a2, GlobalObjectOperand());
139 __ ld(a2, FieldMemOperand(a2, GlobalObject::kGlobalProxyOffset));
141 __ sd(a2, MemOperand(sp, receiver_offset));
144 // Open a frame scope to indicate that there is a frame on the stack. The
145 // MANUAL indicates that the scope shouldn't actually generate code to set up
146 // the frame (that is done below).
147 FrameScope frame_scope(masm_, StackFrame::MANUAL);
148 info->set_prologue_offset(masm_->pc_offset());
149 __ Prologue(info->IsCodePreAgingActive());
150 info->AddNoFrameRange(0, masm_->pc_offset());
152 { Comment cmnt(masm_, "[ Allocate locals");
153 int locals_count = info->scope()->num_stack_slots();
154 // Generators allocate locals, if any, in context slots.
155 DCHECK(!IsGeneratorFunction(info->literal()->kind()) || locals_count == 0);
156 if (locals_count > 0) {
157 if (locals_count >= 128) {
159 __ Dsubu(t1, sp, Operand(locals_count * kPointerSize));
160 __ LoadRoot(a2, Heap::kRealStackLimitRootIndex);
161 __ Branch(&ok, hs, t1, Operand(a2));
162 __ InvokeBuiltin(Context::STACK_OVERFLOW_BUILTIN_INDEX, CALL_FUNCTION);
165 __ LoadRoot(t1, Heap::kUndefinedValueRootIndex);
166 int kMaxPushes = FLAG_optimize_for_size ? 4 : 32;
167 if (locals_count >= kMaxPushes) {
168 int loop_iterations = locals_count / kMaxPushes;
169 __ li(a2, Operand(loop_iterations));
171 __ bind(&loop_header);
173 __ Dsubu(sp, sp, Operand(kMaxPushes * kPointerSize));
174 for (int i = 0; i < kMaxPushes; i++) {
175 __ sd(t1, MemOperand(sp, i * kPointerSize));
177 // Continue loop if not done.
178 __ Dsubu(a2, a2, Operand(1));
179 __ Branch(&loop_header, ne, a2, Operand(zero_reg));
181 int remaining = locals_count % kMaxPushes;
182 // Emit the remaining pushes.
183 __ Dsubu(sp, sp, Operand(remaining * kPointerSize));
184 for (int i = 0; i < remaining; i++) {
185 __ sd(t1, MemOperand(sp, i * kPointerSize));
190 bool function_in_register = true;
192 // Possibly allocate a local context.
193 if (info->scope()->num_heap_slots() > 0) {
194 Comment cmnt(masm_, "[ Allocate context");
195 // Argument to NewContext is the function, which is still in a1.
196 bool need_write_barrier = true;
197 int slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
198 if (info->scope()->is_script_scope()) {
200 __ Push(info->scope()->GetScopeInfo(info->isolate()));
201 __ CallRuntime(Runtime::kNewScriptContext, 2);
202 } else if (slots <= FastNewContextStub::kMaximumSlots) {
203 FastNewContextStub stub(isolate(), slots);
205 // Result of FastNewContextStub is always in new space.
206 need_write_barrier = false;
209 __ CallRuntime(Runtime::kNewFunctionContext, 1);
211 function_in_register = false;
212 // Context is returned in v0. It replaces the context passed to us.
213 // It's saved in the stack and kept live in cp.
215 __ sd(v0, MemOperand(fp, StandardFrameConstants::kContextOffset));
216 // Copy any necessary parameters into the context.
217 int num_parameters = info->scope()->num_parameters();
218 int first_parameter = info->scope()->has_this_declaration() ? -1 : 0;
219 for (int i = first_parameter; i < num_parameters; i++) {
220 Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
221 if (var->IsContextSlot()) {
222 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
223 (num_parameters - 1 - i) * kPointerSize;
224 // Load parameter from stack.
225 __ ld(a0, MemOperand(fp, parameter_offset));
226 // Store it in the context.
227 MemOperand target = ContextOperand(cp, var->index());
230 // Update the write barrier.
231 if (need_write_barrier) {
232 __ RecordWriteContextSlot(
233 cp, target.offset(), a0, a3, kRAHasBeenSaved, kDontSaveFPRegs);
234 } else if (FLAG_debug_code) {
236 __ JumpIfInNewSpace(cp, a0, &done);
237 __ Abort(kExpectedNewSpaceObject);
244 // Possibly set up a local binding to the this function which is used in
245 // derived constructors with super calls.
246 Variable* this_function_var = scope()->this_function_var();
247 if (this_function_var != nullptr) {
248 Comment cmnt(masm_, "[ This function");
249 if (!function_in_register) {
250 __ ld(a1, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
251 // The write barrier clobbers register again, keep is marked as such.
253 SetVar(this_function_var, a1, a2, a3);
256 Variable* new_target_var = scope()->new_target_var();
257 if (new_target_var != nullptr) {
258 Comment cmnt(masm_, "[ new.target");
259 // Get the frame pointer for the calling frame.
260 __ ld(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
262 // Skip the arguments adaptor frame if it exists.
263 Label check_frame_marker;
264 __ ld(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
265 __ Branch(&check_frame_marker, ne, a1,
266 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
267 __ ld(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
269 // Check the marker in the calling frame.
270 __ bind(&check_frame_marker);
271 __ ld(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
273 Label non_construct_frame, done;
274 __ Branch(&non_construct_frame, ne, a1,
275 Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
278 MemOperand(a2, ConstructFrameConstants::kOriginalConstructorOffset));
281 __ bind(&non_construct_frame);
282 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
285 SetVar(new_target_var, v0, a2, a3);
288 // Possibly allocate RestParameters
290 Variable* rest_param = scope()->rest_parameter(&rest_index);
292 Comment cmnt(masm_, "[ Allocate rest parameter array");
294 int num_parameters = info->scope()->num_parameters();
295 int offset = num_parameters * kPointerSize;
298 Operand(StandardFrameConstants::kCallerSPOffset + offset));
299 __ li(a2, Operand(Smi::FromInt(num_parameters)));
300 __ li(a1, Operand(Smi::FromInt(rest_index)));
301 __ li(a0, Operand(Smi::FromInt(language_mode())));
302 __ Push(a3, a2, a1, a0);
304 RestParamAccessStub stub(isolate());
307 SetVar(rest_param, v0, a1, a2);
310 Variable* arguments = scope()->arguments();
311 if (arguments != NULL) {
312 // Function uses arguments object.
313 Comment cmnt(masm_, "[ Allocate arguments object");
314 if (!function_in_register) {
315 // Load this again, if it's used by the local context below.
316 __ ld(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
320 // Receiver is just before the parameters on the caller's stack.
321 int num_parameters = info->scope()->num_parameters();
322 int offset = num_parameters * kPointerSize;
324 Operand(StandardFrameConstants::kCallerSPOffset + offset));
325 __ li(a1, Operand(Smi::FromInt(num_parameters)));
328 // Arguments to ArgumentsAccessStub:
329 // function, receiver address, parameter count.
330 // The stub will rewrite receiever and parameter count if the previous
331 // stack frame was an arguments adapter frame.
332 ArgumentsAccessStub::Type type;
333 if (is_strict(language_mode()) || !has_simple_parameters()) {
334 type = ArgumentsAccessStub::NEW_STRICT;
335 } else if (literal()->has_duplicate_parameters()) {
336 type = ArgumentsAccessStub::NEW_SLOPPY_SLOW;
338 type = ArgumentsAccessStub::NEW_SLOPPY_FAST;
340 ArgumentsAccessStub stub(isolate(), type);
343 SetVar(arguments, v0, a1, a2);
347 __ CallRuntime(Runtime::kTraceEnter, 0);
349 // Visit the declarations and body unless there is an illegal
351 if (scope()->HasIllegalRedeclaration()) {
352 Comment cmnt(masm_, "[ Declarations");
353 scope()->VisitIllegalRedeclaration(this);
356 PrepareForBailoutForId(BailoutId::FunctionEntry(), NO_REGISTERS);
357 { Comment cmnt(masm_, "[ Declarations");
358 VisitDeclarations(scope()->declarations());
361 // Assert that the declarations do not use ICs. Otherwise the debugger
362 // won't be able to redirect a PC at an IC to the correct IC in newly
364 DCHECK_EQ(0, ic_total_count_);
366 { Comment cmnt(masm_, "[ Stack check");
367 PrepareForBailoutForId(BailoutId::Declarations(), NO_REGISTERS);
369 __ LoadRoot(at, Heap::kStackLimitRootIndex);
370 __ Branch(&ok, hs, sp, Operand(at));
371 Handle<Code> stack_check = isolate()->builtins()->StackCheck();
372 PredictableCodeSizeScope predictable(masm_,
373 masm_->CallSize(stack_check, RelocInfo::CODE_TARGET));
374 __ Call(stack_check, RelocInfo::CODE_TARGET);
378 { Comment cmnt(masm_, "[ Body");
379 DCHECK(loop_depth() == 0);
381 VisitStatements(literal()->body());
383 DCHECK(loop_depth() == 0);
387 // Always emit a 'return undefined' in case control fell off the end of
389 { Comment cmnt(masm_, "[ return <undefined>;");
390 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
392 EmitReturnSequence();
396 void FullCodeGenerator::ClearAccumulator() {
397 DCHECK(Smi::FromInt(0) == 0);
398 __ mov(v0, zero_reg);
402 void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
403 __ li(a2, Operand(profiling_counter_));
404 __ ld(a3, FieldMemOperand(a2, Cell::kValueOffset));
405 __ Dsubu(a3, a3, Operand(Smi::FromInt(delta)));
406 __ sd(a3, FieldMemOperand(a2, Cell::kValueOffset));
410 void FullCodeGenerator::EmitProfilingCounterReset() {
411 int reset_value = FLAG_interrupt_budget;
412 if (info_->is_debug()) {
413 // Detect debug break requests as soon as possible.
414 reset_value = FLAG_interrupt_budget >> 4;
416 __ li(a2, Operand(profiling_counter_));
417 __ li(a3, Operand(Smi::FromInt(reset_value)));
418 __ sd(a3, FieldMemOperand(a2, Cell::kValueOffset));
422 void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
423 Label* back_edge_target) {
424 // The generated code is used in Deoptimizer::PatchStackCheckCodeAt so we need
425 // to make sure it is constant. Branch may emit a skip-or-jump sequence
426 // instead of the normal Branch. It seems that the "skip" part of that
427 // sequence is about as long as this Branch would be so it is safe to ignore
429 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
430 Comment cmnt(masm_, "[ Back edge bookkeeping");
432 DCHECK(back_edge_target->is_bound());
433 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
434 int weight = Min(kMaxBackEdgeWeight,
435 Max(1, distance / kCodeSizeMultiplier));
436 EmitProfilingCounterDecrement(weight);
437 __ slt(at, a3, zero_reg);
438 __ beq(at, zero_reg, &ok);
439 // Call will emit a li t9 first, so it is safe to use the delay slot.
440 __ Call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
441 // Record a mapping of this PC offset to the OSR id. This is used to find
442 // the AST id from the unoptimized code in order to use it as a key into
443 // the deoptimization input data found in the optimized code.
444 RecordBackEdge(stmt->OsrEntryId());
445 EmitProfilingCounterReset();
448 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
449 // Record a mapping of the OSR id to this PC. This is used if the OSR
450 // entry becomes the target of a bailout. We don't expect it to be, but
451 // we want it to work if it is.
452 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
456 void FullCodeGenerator::EmitReturnSequence() {
457 Comment cmnt(masm_, "[ Return sequence");
458 if (return_label_.is_bound()) {
459 __ Branch(&return_label_);
461 __ bind(&return_label_);
463 // Push the return value on the stack as the parameter.
464 // Runtime::TraceExit returns its parameter in v0.
466 __ CallRuntime(Runtime::kTraceExit, 1);
468 // Pretend that the exit is a backwards jump to the entry.
470 if (info_->ShouldSelfOptimize()) {
471 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
473 int distance = masm_->pc_offset();
474 weight = Min(kMaxBackEdgeWeight,
475 Max(1, distance / kCodeSizeMultiplier));
477 EmitProfilingCounterDecrement(weight);
479 __ Branch(&ok, ge, a3, Operand(zero_reg));
481 __ Call(isolate()->builtins()->InterruptCheck(),
482 RelocInfo::CODE_TARGET);
484 EmitProfilingCounterReset();
487 // Make sure that the constant pool is not emitted inside of the return
489 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
490 // Here we use masm_-> instead of the __ macro to avoid the code coverage
491 // tool from instrumenting as we rely on the code size here.
492 int32_t arg_count = info_->scope()->num_parameters() + 1;
493 int32_t sp_delta = arg_count * kPointerSize;
494 SetReturnPosition(literal());
496 int no_frame_start = masm_->pc_offset();
497 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
498 masm_->Daddu(sp, sp, Operand(sp_delta));
500 info_->AddNoFrameRange(no_frame_start, masm_->pc_offset());
506 void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
507 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
508 codegen()->GetVar(result_register(), var);
509 __ push(result_register());
513 void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
517 void FullCodeGenerator::AccumulatorValueContext::Plug(
518 Heap::RootListIndex index) const {
519 __ LoadRoot(result_register(), index);
523 void FullCodeGenerator::StackValueContext::Plug(
524 Heap::RootListIndex index) const {
525 __ LoadRoot(result_register(), index);
526 __ push(result_register());
530 void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
531 codegen()->PrepareForBailoutBeforeSplit(condition(),
535 if (index == Heap::kUndefinedValueRootIndex ||
536 index == Heap::kNullValueRootIndex ||
537 index == Heap::kFalseValueRootIndex) {
538 if (false_label_ != fall_through_) __ Branch(false_label_);
539 } else if (index == Heap::kTrueValueRootIndex) {
540 if (true_label_ != fall_through_) __ Branch(true_label_);
542 __ LoadRoot(result_register(), index);
543 codegen()->DoTest(this);
548 void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
552 void FullCodeGenerator::AccumulatorValueContext::Plug(
553 Handle<Object> lit) const {
554 __ li(result_register(), Operand(lit));
558 void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
559 // Immediates cannot be pushed directly.
560 __ li(result_register(), Operand(lit));
561 __ push(result_register());
565 void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
566 codegen()->PrepareForBailoutBeforeSplit(condition(),
570 DCHECK(!lit->IsUndetectableObject()); // There are no undetectable literals.
571 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
572 if (false_label_ != fall_through_) __ Branch(false_label_);
573 } else if (lit->IsTrue() || lit->IsJSObject()) {
574 if (true_label_ != fall_through_) __ Branch(true_label_);
575 } else if (lit->IsString()) {
576 if (String::cast(*lit)->length() == 0) {
577 if (false_label_ != fall_through_) __ Branch(false_label_);
579 if (true_label_ != fall_through_) __ Branch(true_label_);
581 } else if (lit->IsSmi()) {
582 if (Smi::cast(*lit)->value() == 0) {
583 if (false_label_ != fall_through_) __ Branch(false_label_);
585 if (true_label_ != fall_through_) __ Branch(true_label_);
588 // For simplicity we always test the accumulator register.
589 __ li(result_register(), Operand(lit));
590 codegen()->DoTest(this);
595 void FullCodeGenerator::EffectContext::DropAndPlug(int count,
596 Register reg) const {
602 void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
604 Register reg) const {
607 __ Move(result_register(), reg);
611 void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
612 Register reg) const {
614 if (count > 1) __ Drop(count - 1);
615 __ sd(reg, MemOperand(sp, 0));
619 void FullCodeGenerator::TestContext::DropAndPlug(int count,
620 Register reg) const {
622 // For simplicity we always test the accumulator register.
624 __ Move(result_register(), reg);
625 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
626 codegen()->DoTest(this);
630 void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
631 Label* materialize_false) const {
632 DCHECK(materialize_true == materialize_false);
633 __ bind(materialize_true);
637 void FullCodeGenerator::AccumulatorValueContext::Plug(
638 Label* materialize_true,
639 Label* materialize_false) const {
641 __ bind(materialize_true);
642 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
644 __ bind(materialize_false);
645 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
650 void FullCodeGenerator::StackValueContext::Plug(
651 Label* materialize_true,
652 Label* materialize_false) const {
654 __ bind(materialize_true);
655 __ LoadRoot(at, Heap::kTrueValueRootIndex);
656 // Push the value as the following branch can clobber at in long branch mode.
659 __ bind(materialize_false);
660 __ LoadRoot(at, Heap::kFalseValueRootIndex);
666 void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
667 Label* materialize_false) const {
668 DCHECK(materialize_true == true_label_);
669 DCHECK(materialize_false == false_label_);
673 void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
674 Heap::RootListIndex value_root_index =
675 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
676 __ LoadRoot(result_register(), value_root_index);
680 void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
681 Heap::RootListIndex value_root_index =
682 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
683 __ LoadRoot(at, value_root_index);
688 void FullCodeGenerator::TestContext::Plug(bool flag) const {
689 codegen()->PrepareForBailoutBeforeSplit(condition(),
694 if (true_label_ != fall_through_) __ Branch(true_label_);
696 if (false_label_ != fall_through_) __ Branch(false_label_);
701 void FullCodeGenerator::DoTest(Expression* condition,
704 Label* fall_through) {
705 __ mov(a0, result_register());
706 Handle<Code> ic = ToBooleanStub::GetUninitialized(isolate());
707 CallIC(ic, condition->test_id());
708 __ mov(at, zero_reg);
709 Split(ne, v0, Operand(at), if_true, if_false, fall_through);
713 void FullCodeGenerator::Split(Condition cc,
718 Label* fall_through) {
719 if (if_false == fall_through) {
720 __ Branch(if_true, cc, lhs, rhs);
721 } else if (if_true == fall_through) {
722 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
724 __ Branch(if_true, cc, lhs, rhs);
730 MemOperand FullCodeGenerator::StackOperand(Variable* var) {
731 DCHECK(var->IsStackAllocated());
732 // Offset is negative because higher indexes are at lower addresses.
733 int offset = -var->index() * kPointerSize;
734 // Adjust by a (parameter or local) base offset.
735 if (var->IsParameter()) {
736 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
738 offset += JavaScriptFrameConstants::kLocal0Offset;
740 return MemOperand(fp, offset);
744 MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
745 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
746 if (var->IsContextSlot()) {
747 int context_chain_length = scope()->ContextChainLength(var->scope());
748 __ LoadContext(scratch, context_chain_length);
749 return ContextOperand(scratch, var->index());
751 return StackOperand(var);
756 void FullCodeGenerator::GetVar(Register dest, Variable* var) {
757 // Use destination as scratch.
758 MemOperand location = VarOperand(var, dest);
759 __ ld(dest, location);
763 void FullCodeGenerator::SetVar(Variable* var,
767 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
768 DCHECK(!scratch0.is(src));
769 DCHECK(!scratch0.is(scratch1));
770 DCHECK(!scratch1.is(src));
771 MemOperand location = VarOperand(var, scratch0);
772 __ sd(src, location);
773 // Emit the write barrier code if the location is in the heap.
774 if (var->IsContextSlot()) {
775 __ RecordWriteContextSlot(scratch0,
785 void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
786 bool should_normalize,
789 // Only prepare for bailouts before splits if we're in a test
790 // context. Otherwise, we let the Visit function deal with the
791 // preparation to avoid preparing with the same AST id twice.
792 if (!context()->IsTest()) return;
795 if (should_normalize) __ Branch(&skip);
796 PrepareForBailout(expr, TOS_REG);
797 if (should_normalize) {
798 __ LoadRoot(a4, Heap::kTrueValueRootIndex);
799 Split(eq, a0, Operand(a4), if_true, if_false, NULL);
805 void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
806 // The variable in the declaration always resides in the current function
808 DCHECK_EQ(0, scope()->ContextChainLength(variable->scope()));
809 if (generate_debug_code_) {
810 // Check that we're not inside a with or catch context.
811 __ ld(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
812 __ LoadRoot(a4, Heap::kWithContextMapRootIndex);
813 __ Check(ne, kDeclarationInWithContext,
815 __ LoadRoot(a4, Heap::kCatchContextMapRootIndex);
816 __ Check(ne, kDeclarationInCatchContext,
822 void FullCodeGenerator::VisitVariableDeclaration(
823 VariableDeclaration* declaration) {
824 // If it was not possible to allocate the variable at compile time, we
825 // need to "declare" it at runtime to make sure it actually exists in the
827 VariableProxy* proxy = declaration->proxy();
828 VariableMode mode = declaration->mode();
829 Variable* variable = proxy->var();
830 bool hole_init = mode == LET || mode == CONST || mode == CONST_LEGACY;
831 switch (variable->location()) {
832 case VariableLocation::GLOBAL:
833 case VariableLocation::UNALLOCATED:
834 globals_->Add(variable->name(), zone());
835 globals_->Add(variable->binding_needs_init()
836 ? isolate()->factory()->the_hole_value()
837 : isolate()->factory()->undefined_value(),
841 case VariableLocation::PARAMETER:
842 case VariableLocation::LOCAL:
844 Comment cmnt(masm_, "[ VariableDeclaration");
845 __ LoadRoot(a4, Heap::kTheHoleValueRootIndex);
846 __ sd(a4, StackOperand(variable));
850 case VariableLocation::CONTEXT:
852 Comment cmnt(masm_, "[ VariableDeclaration");
853 EmitDebugCheckDeclarationContext(variable);
854 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
855 __ sd(at, ContextOperand(cp, variable->index()));
856 // No write barrier since the_hole_value is in old space.
857 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
861 case VariableLocation::LOOKUP: {
862 Comment cmnt(masm_, "[ VariableDeclaration");
863 __ li(a2, Operand(variable->name()));
864 // Declaration nodes are always introduced in one of four modes.
865 DCHECK(IsDeclaredVariableMode(mode));
866 // Push initial value, if any.
867 // Note: For variables we must not push an initial value (such as
868 // 'undefined') because we may have a (legal) redeclaration and we
869 // must not destroy the current value.
871 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
873 DCHECK(Smi::FromInt(0) == 0);
874 __ mov(a0, zero_reg); // Smi::FromInt(0) indicates no initial value.
877 __ CallRuntime(IsImmutableVariableMode(mode)
878 ? Runtime::kDeclareReadOnlyLookupSlot
879 : Runtime::kDeclareLookupSlot,
887 void FullCodeGenerator::VisitFunctionDeclaration(
888 FunctionDeclaration* declaration) {
889 VariableProxy* proxy = declaration->proxy();
890 Variable* variable = proxy->var();
891 switch (variable->location()) {
892 case VariableLocation::GLOBAL:
893 case VariableLocation::UNALLOCATED: {
894 globals_->Add(variable->name(), zone());
895 Handle<SharedFunctionInfo> function =
896 Compiler::GetSharedFunctionInfo(declaration->fun(), script(), info_);
897 // Check for stack-overflow exception.
898 if (function.is_null()) return SetStackOverflow();
899 globals_->Add(function, zone());
903 case VariableLocation::PARAMETER:
904 case VariableLocation::LOCAL: {
905 Comment cmnt(masm_, "[ FunctionDeclaration");
906 VisitForAccumulatorValue(declaration->fun());
907 __ sd(result_register(), StackOperand(variable));
911 case VariableLocation::CONTEXT: {
912 Comment cmnt(masm_, "[ FunctionDeclaration");
913 EmitDebugCheckDeclarationContext(variable);
914 VisitForAccumulatorValue(declaration->fun());
915 __ sd(result_register(), ContextOperand(cp, variable->index()));
916 int offset = Context::SlotOffset(variable->index());
917 // We know that we have written a function, which is not a smi.
918 __ RecordWriteContextSlot(cp,
926 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
930 case VariableLocation::LOOKUP: {
931 Comment cmnt(masm_, "[ FunctionDeclaration");
932 __ li(a2, Operand(variable->name()));
934 // Push initial value for function declaration.
935 VisitForStackValue(declaration->fun());
936 __ CallRuntime(Runtime::kDeclareLookupSlot, 2);
943 void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
944 // Call the runtime to declare the globals.
945 __ li(a1, Operand(pairs));
946 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
948 __ CallRuntime(Runtime::kDeclareGlobals, 2);
949 // Return value is ignored.
953 void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
954 // Call the runtime to declare the modules.
955 __ Push(descriptions);
956 __ CallRuntime(Runtime::kDeclareModules, 1);
957 // Return value is ignored.
961 void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
962 Comment cmnt(masm_, "[ SwitchStatement");
963 Breakable nested_statement(this, stmt);
964 SetStatementPosition(stmt);
966 // Keep the switch value on the stack until a case matches.
967 VisitForStackValue(stmt->tag());
968 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
970 ZoneList<CaseClause*>* clauses = stmt->cases();
971 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
973 Label next_test; // Recycled for each test.
974 // Compile all the tests with branches to their bodies.
975 for (int i = 0; i < clauses->length(); i++) {
976 CaseClause* clause = clauses->at(i);
977 clause->body_target()->Unuse();
979 // The default is not a test, but remember it as final fall through.
980 if (clause->is_default()) {
981 default_clause = clause;
985 Comment cmnt(masm_, "[ Case comparison");
989 // Compile the label expression.
990 VisitForAccumulatorValue(clause->label());
991 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
993 // Perform the comparison as if via '==='.
994 __ ld(a1, MemOperand(sp, 0)); // Switch value.
995 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
996 JumpPatchSite patch_site(masm_);
997 if (inline_smi_code) {
1000 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
1002 __ Branch(&next_test, ne, a1, Operand(a0));
1003 __ Drop(1); // Switch value is no longer needed.
1004 __ Branch(clause->body_target());
1006 __ bind(&slow_case);
1009 // Record position before stub call for type feedback.
1010 SetExpressionPosition(clause);
1011 Handle<Code> ic = CodeFactory::CompareIC(isolate(), Token::EQ_STRICT,
1012 strength(language_mode())).code();
1013 CallIC(ic, clause->CompareId());
1014 patch_site.EmitPatchInfo();
1018 PrepareForBailout(clause, TOS_REG);
1019 __ LoadRoot(at, Heap::kTrueValueRootIndex);
1020 __ Branch(&next_test, ne, v0, Operand(at));
1022 __ Branch(clause->body_target());
1025 __ Branch(&next_test, ne, v0, Operand(zero_reg));
1026 __ Drop(1); // Switch value is no longer needed.
1027 __ Branch(clause->body_target());
1030 // Discard the test value and jump to the default if present, otherwise to
1031 // the end of the statement.
1032 __ bind(&next_test);
1033 __ Drop(1); // Switch value is no longer needed.
1034 if (default_clause == NULL) {
1035 __ Branch(nested_statement.break_label());
1037 __ Branch(default_clause->body_target());
1040 // Compile all the case bodies.
1041 for (int i = 0; i < clauses->length(); i++) {
1042 Comment cmnt(masm_, "[ Case body");
1043 CaseClause* clause = clauses->at(i);
1044 __ bind(clause->body_target());
1045 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
1046 VisitStatements(clause->statements());
1049 __ bind(nested_statement.break_label());
1050 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1054 void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
1055 Comment cmnt(masm_, "[ ForInStatement");
1056 SetStatementPosition(stmt, SKIP_BREAK);
1058 FeedbackVectorSlot slot = stmt->ForInFeedbackSlot();
1061 ForIn loop_statement(this, stmt);
1062 increment_loop_depth();
1064 // Get the object to enumerate over. If the object is null or undefined, skip
1065 // over the loop. See ECMA-262 version 5, section 12.6.4.
1066 SetExpressionAsStatementPosition(stmt->enumerable());
1067 VisitForAccumulatorValue(stmt->enumerable());
1068 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
1069 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1070 __ Branch(&exit, eq, a0, Operand(at));
1071 Register null_value = a5;
1072 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
1073 __ Branch(&exit, eq, a0, Operand(null_value));
1074 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1076 // Convert the object to a JS object.
1077 Label convert, done_convert;
1078 __ JumpIfSmi(a0, &convert);
1079 __ GetObjectType(a0, a1, a1);
1080 __ Branch(&done_convert, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
1082 ToObjectStub stub(isolate());
1085 __ bind(&done_convert);
1086 PrepareForBailoutForId(stmt->ToObjectId(), TOS_REG);
1089 // Check for proxies.
1091 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1092 __ GetObjectType(a0, a1, a1);
1093 __ Branch(&call_runtime, le, a1, Operand(LAST_JS_PROXY_TYPE));
1095 // Check cache validity in generated code. This is a fast case for
1096 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1097 // guarantee cache validity, call the runtime system to check cache
1098 // validity or get the property names in a fixed array.
1099 __ CheckEnumCache(null_value, &call_runtime);
1101 // The enum cache is valid. Load the map of the object being
1102 // iterated over and use the cache for the iteration.
1104 __ ld(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
1105 __ Branch(&use_cache);
1107 // Get the set of properties to enumerate.
1108 __ bind(&call_runtime);
1109 __ push(a0); // Duplicate the enumerable object on the stack.
1110 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1111 PrepareForBailoutForId(stmt->EnumId(), TOS_REG);
1113 // If we got a map from the runtime call, we can do a fast
1114 // modification check. Otherwise, we got a fixed array, and we have
1115 // to do a slow check.
1117 __ ld(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
1118 __ LoadRoot(at, Heap::kMetaMapRootIndex);
1119 __ Branch(&fixed_array, ne, a2, Operand(at));
1121 // We got a map in register v0. Get the enumeration cache from it.
1122 Label no_descriptors;
1123 __ bind(&use_cache);
1125 __ EnumLength(a1, v0);
1126 __ Branch(&no_descriptors, eq, a1, Operand(Smi::FromInt(0)));
1128 __ LoadInstanceDescriptors(v0, a2);
1129 __ ld(a2, FieldMemOperand(a2, DescriptorArray::kEnumCacheOffset));
1130 __ ld(a2, FieldMemOperand(a2, DescriptorArray::kEnumCacheBridgeCacheOffset));
1132 // Set up the four remaining stack slots.
1133 __ li(a0, Operand(Smi::FromInt(0)));
1134 // Push map, enumeration cache, enumeration cache length (as smi) and zero.
1135 __ Push(v0, a2, a1, a0);
1138 __ bind(&no_descriptors);
1142 // We got a fixed array in register v0. Iterate through that.
1144 __ bind(&fixed_array);
1146 __ li(a1, FeedbackVector());
1147 __ li(a2, Operand(TypeFeedbackVector::MegamorphicSentinel(isolate())));
1148 int vector_index = FeedbackVector()->GetIndex(slot);
1149 __ sd(a2, FieldMemOperand(a1, FixedArray::OffsetOfElementAt(vector_index)));
1151 __ li(a1, Operand(Smi::FromInt(1))); // Smi indicates slow check
1152 __ ld(a2, MemOperand(sp, 0 * kPointerSize)); // Get enumerated object
1153 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
1154 __ GetObjectType(a2, a3, a3);
1155 __ Branch(&non_proxy, gt, a3, Operand(LAST_JS_PROXY_TYPE));
1156 __ li(a1, Operand(Smi::FromInt(0))); // Zero indicates proxy
1157 __ bind(&non_proxy);
1158 __ Push(a1, v0); // Smi and array
1159 __ ld(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1160 __ li(a0, Operand(Smi::FromInt(0)));
1161 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1163 // Generate code for doing the condition check.
1164 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
1166 SetExpressionAsStatementPosition(stmt->each());
1168 // Load the current count to a0, load the length to a1.
1169 __ ld(a0, MemOperand(sp, 0 * kPointerSize));
1170 __ ld(a1, MemOperand(sp, 1 * kPointerSize));
1171 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
1173 // Get the current entry of the array into register a3.
1174 __ ld(a2, MemOperand(sp, 2 * kPointerSize));
1175 __ Daddu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1176 __ SmiScale(a4, a0, kPointerSizeLog2);
1177 __ daddu(a4, a2, a4); // Array base + scaled (smi) index.
1178 __ ld(a3, MemOperand(a4)); // Current entry.
1180 // Get the expected map from the stack or a smi in the
1181 // permanent slow case into register a2.
1182 __ ld(a2, MemOperand(sp, 3 * kPointerSize));
1184 // Check if the expected map still matches that of the enumerable.
1185 // If not, we may have to filter the key.
1187 __ ld(a1, MemOperand(sp, 4 * kPointerSize));
1188 __ ld(a4, FieldMemOperand(a1, HeapObject::kMapOffset));
1189 __ Branch(&update_each, eq, a4, Operand(a2));
1191 // For proxies, no filtering is done.
1192 // TODO(rossberg): What if only a prototype is a proxy? Not specified yet.
1193 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
1194 __ Branch(&update_each, eq, a2, Operand(zero_reg));
1196 // Convert the entry to a string or (smi) 0 if it isn't a property
1197 // any more. If the property has been removed while iterating, we
1199 __ Push(a1, a3); // Enumerable and current entry.
1200 __ CallRuntime(Runtime::kForInFilter, 2);
1201 PrepareForBailoutForId(stmt->FilterId(), TOS_REG);
1202 __ mov(a3, result_register());
1203 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1204 __ Branch(loop_statement.continue_label(), eq, a3, Operand(at));
1206 // Update the 'each' property or variable from the possibly filtered
1207 // entry in register a3.
1208 __ bind(&update_each);
1209 __ mov(result_register(), a3);
1210 // Perform the assignment as if via '='.
1211 { EffectContext context(this);
1212 EmitAssignment(stmt->each(), stmt->EachFeedbackSlot());
1213 PrepareForBailoutForId(stmt->AssignmentId(), NO_REGISTERS);
1216 // Generate code for the body of the loop.
1217 Visit(stmt->body());
1219 // Generate code for the going to the next element by incrementing
1220 // the index (smi) stored on top of the stack.
1221 __ bind(loop_statement.continue_label());
1223 __ Daddu(a0, a0, Operand(Smi::FromInt(1)));
1226 EmitBackEdgeBookkeeping(stmt, &loop);
1229 // Remove the pointers stored on the stack.
1230 __ bind(loop_statement.break_label());
1233 // Exit and decrement the loop depth.
1234 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1236 decrement_loop_depth();
1240 void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1242 // Use the fast case closure allocation code that allocates in new
1243 // space for nested functions that don't need literals cloning. If
1244 // we're running with the --always-opt or the --prepare-always-opt
1245 // flag, we need to use the runtime function so that the new function
1246 // we are creating here gets a chance to have its code optimized and
1247 // doesn't just get a copy of the existing unoptimized code.
1248 if (!FLAG_always_opt &&
1249 !FLAG_prepare_always_opt &&
1251 scope()->is_function_scope() &&
1252 info->num_literals() == 0) {
1253 FastNewClosureStub stub(isolate(), info->language_mode(), info->kind());
1254 __ li(a2, Operand(info));
1257 __ li(a0, Operand(info));
1258 __ LoadRoot(a1, pretenure ? Heap::kTrueValueRootIndex
1259 : Heap::kFalseValueRootIndex);
1260 __ Push(cp, a0, a1);
1261 __ CallRuntime(Runtime::kNewClosure, 3);
1263 context()->Plug(v0);
1267 void FullCodeGenerator::EmitSetHomeObjectIfNeeded(Expression* initializer,
1269 FeedbackVectorICSlot slot) {
1270 if (NeedsHomeObject(initializer)) {
1271 __ ld(StoreDescriptor::ReceiverRegister(), MemOperand(sp));
1272 __ li(StoreDescriptor::NameRegister(),
1273 Operand(isolate()->factory()->home_object_symbol()));
1274 __ ld(StoreDescriptor::ValueRegister(),
1275 MemOperand(sp, offset * kPointerSize));
1276 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
1282 void FullCodeGenerator::EmitLoadGlobalCheckExtensions(VariableProxy* proxy,
1283 TypeofMode typeof_mode,
1285 Register current = cp;
1291 if (s->num_heap_slots() > 0) {
1292 if (s->calls_sloppy_eval()) {
1293 // Check that extension is NULL.
1294 __ ld(temp, ContextOperand(current, Context::EXTENSION_INDEX));
1295 __ Branch(slow, ne, temp, Operand(zero_reg));
1297 // Load next context in chain.
1298 __ ld(next, ContextOperand(current, Context::PREVIOUS_INDEX));
1299 // Walk the rest of the chain without clobbering cp.
1302 // If no outer scope calls eval, we do not need to check more
1303 // context extensions.
1304 if (!s->outer_scope_calls_sloppy_eval() || s->is_eval_scope()) break;
1305 s = s->outer_scope();
1308 if (s->is_eval_scope()) {
1310 if (!current.is(next)) {
1311 __ Move(next, current);
1314 // Terminate at native context.
1315 __ ld(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1316 __ LoadRoot(a4, Heap::kNativeContextMapRootIndex);
1317 __ Branch(&fast, eq, temp, Operand(a4));
1318 // Check that extension is NULL.
1319 __ ld(temp, ContextOperand(next, Context::EXTENSION_INDEX));
1320 __ Branch(slow, ne, temp, Operand(zero_reg));
1321 // Load next context in chain.
1322 __ ld(next, ContextOperand(next, Context::PREVIOUS_INDEX));
1327 // All extension objects were empty and it is safe to use a normal global
1329 EmitGlobalVariableLoad(proxy, typeof_mode);
1333 MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1335 DCHECK(var->IsContextSlot());
1336 Register context = cp;
1340 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
1341 if (s->num_heap_slots() > 0) {
1342 if (s->calls_sloppy_eval()) {
1343 // Check that extension is NULL.
1344 __ ld(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1345 __ Branch(slow, ne, temp, Operand(zero_reg));
1347 __ ld(next, ContextOperand(context, Context::PREVIOUS_INDEX));
1348 // Walk the rest of the chain without clobbering cp.
1352 // Check that last extension is NULL.
1353 __ ld(temp, ContextOperand(context, Context::EXTENSION_INDEX));
1354 __ Branch(slow, ne, temp, Operand(zero_reg));
1356 // This function is used only for loads, not stores, so it's safe to
1357 // return an cp-based operand (the write barrier cannot be allowed to
1358 // destroy the cp register).
1359 return ContextOperand(context, var->index());
1363 void FullCodeGenerator::EmitDynamicLookupFastCase(VariableProxy* proxy,
1364 TypeofMode typeof_mode,
1365 Label* slow, Label* done) {
1366 // Generate fast-case code for variables that might be shadowed by
1367 // eval-introduced variables. Eval is used a lot without
1368 // introducing variables. In those cases, we do not want to
1369 // perform a runtime call for all variables in the scope
1370 // containing the eval.
1371 Variable* var = proxy->var();
1372 if (var->mode() == DYNAMIC_GLOBAL) {
1373 EmitLoadGlobalCheckExtensions(proxy, typeof_mode, slow);
1375 } else if (var->mode() == DYNAMIC_LOCAL) {
1376 Variable* local = var->local_if_not_shadowed();
1377 __ ld(v0, ContextSlotOperandCheckExtensions(local, slow));
1378 if (local->mode() == LET || local->mode() == CONST ||
1379 local->mode() == CONST_LEGACY) {
1380 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1381 __ dsubu(at, v0, at); // Sub as compare: at == 0 on eq.
1382 if (local->mode() == CONST_LEGACY) {
1383 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1384 __ Movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
1385 } else { // LET || CONST
1386 __ Branch(done, ne, at, Operand(zero_reg));
1387 __ li(a0, Operand(var->name()));
1389 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1397 void FullCodeGenerator::EmitGlobalVariableLoad(VariableProxy* proxy,
1398 TypeofMode typeof_mode) {
1399 Variable* var = proxy->var();
1400 DCHECK(var->IsUnallocatedOrGlobalSlot() ||
1401 (var->IsLookupSlot() && var->mode() == DYNAMIC_GLOBAL));
1402 if (var->IsGlobalSlot()) {
1403 DCHECK(var->index() > 0);
1404 DCHECK(var->IsStaticGlobalObjectProperty());
1405 int const slot = var->index();
1406 int const depth = scope()->ContextChainLength(var->scope());
1407 if (depth <= LoadGlobalViaContextStub::kMaximumDepth) {
1408 __ li(LoadGlobalViaContextDescriptor::SlotRegister(), Operand(slot));
1409 LoadGlobalViaContextStub stub(isolate(), depth);
1412 __ Push(Smi::FromInt(slot));
1413 __ CallRuntime(Runtime::kLoadGlobalViaContext, 1);
1417 __ ld(LoadDescriptor::ReceiverRegister(), GlobalObjectOperand());
1418 __ li(LoadDescriptor::NameRegister(), Operand(var->name()));
1419 __ li(LoadDescriptor::SlotRegister(),
1420 Operand(SmiFromSlot(proxy->VariableFeedbackSlot())));
1421 CallLoadIC(typeof_mode);
1426 void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy,
1427 TypeofMode typeof_mode) {
1428 // Record position before possible IC call.
1429 SetExpressionPosition(proxy);
1430 PrepareForBailoutForId(proxy->BeforeId(), NO_REGISTERS);
1431 Variable* var = proxy->var();
1433 // Three cases: global variables, lookup variables, and all other types of
1435 switch (var->location()) {
1436 case VariableLocation::GLOBAL:
1437 case VariableLocation::UNALLOCATED: {
1438 Comment cmnt(masm_, "[ Global variable");
1439 EmitGlobalVariableLoad(proxy, typeof_mode);
1440 context()->Plug(v0);
1444 case VariableLocation::PARAMETER:
1445 case VariableLocation::LOCAL:
1446 case VariableLocation::CONTEXT: {
1447 DCHECK_EQ(NOT_INSIDE_TYPEOF, typeof_mode);
1448 Comment cmnt(masm_, var->IsContextSlot() ? "[ Context variable"
1449 : "[ Stack variable");
1450 if (var->binding_needs_init()) {
1451 // var->scope() may be NULL when the proxy is located in eval code and
1452 // refers to a potential outside binding. Currently those bindings are
1453 // always looked up dynamically, i.e. in that case
1454 // var->location() == LOOKUP.
1456 DCHECK(var->scope() != NULL);
1458 // Check if the binding really needs an initialization check. The check
1459 // can be skipped in the following situation: we have a LET or CONST
1460 // binding in harmony mode, both the Variable and the VariableProxy have
1461 // the same declaration scope (i.e. they are both in global code, in the
1462 // same function or in the same eval code) and the VariableProxy is in
1463 // the source physically located after the initializer of the variable.
1465 // We cannot skip any initialization checks for CONST in non-harmony
1466 // mode because const variables may be declared but never initialized:
1467 // if (false) { const x; }; var y = x;
1469 // The condition on the declaration scopes is a conservative check for
1470 // nested functions that access a binding and are called before the
1471 // binding is initialized:
1472 // function() { f(); let x = 1; function f() { x = 2; } }
1474 bool skip_init_check;
1475 if (var->scope()->DeclarationScope() != scope()->DeclarationScope()) {
1476 skip_init_check = false;
1477 } else if (var->is_this()) {
1478 CHECK(info_->has_literal() &&
1479 (info_->literal()->kind() & kSubclassConstructor) != 0);
1480 // TODO(dslomov): implement 'this' hole check elimination.
1481 skip_init_check = false;
1483 // Check that we always have valid source position.
1484 DCHECK(var->initializer_position() != RelocInfo::kNoPosition);
1485 DCHECK(proxy->position() != RelocInfo::kNoPosition);
1486 skip_init_check = var->mode() != CONST_LEGACY &&
1487 var->initializer_position() < proxy->position();
1490 if (!skip_init_check) {
1491 // Let and const need a read barrier.
1493 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1494 __ dsubu(at, v0, at); // Sub as compare: at == 0 on eq.
1495 if (var->mode() == LET || var->mode() == CONST) {
1496 // Throw a reference error when using an uninitialized let/const
1497 // binding in harmony mode.
1499 __ Branch(&done, ne, at, Operand(zero_reg));
1500 __ li(a0, Operand(var->name()));
1502 __ CallRuntime(Runtime::kThrowReferenceError, 1);
1505 // Uninitalized const bindings outside of harmony mode are unholed.
1506 DCHECK(var->mode() == CONST_LEGACY);
1507 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1508 __ Movz(v0, a0, at); // Conditional move: Undefined if TheHole.
1510 context()->Plug(v0);
1514 context()->Plug(var);
1518 case VariableLocation::LOOKUP: {
1519 Comment cmnt(masm_, "[ Lookup variable");
1521 // Generate code for loading from variables potentially shadowed
1522 // by eval-introduced variables.
1523 EmitDynamicLookupFastCase(proxy, typeof_mode, &slow, &done);
1525 __ li(a1, Operand(var->name()));
1526 __ Push(cp, a1); // Context and name.
1527 Runtime::FunctionId function_id =
1528 typeof_mode == NOT_INSIDE_TYPEOF
1529 ? Runtime::kLoadLookupSlot
1530 : Runtime::kLoadLookupSlotNoReferenceError;
1531 __ CallRuntime(function_id, 2);
1533 context()->Plug(v0);
1539 void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1540 Comment cmnt(masm_, "[ RegExpLiteral");
1542 // Registers will be used as follows:
1543 // a5 = materialized value (RegExp literal)
1544 // a4 = JS function, literals array
1545 // a3 = literal index
1546 // a2 = RegExp pattern
1547 // a1 = RegExp flags
1548 // a0 = RegExp literal clone
1549 __ ld(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1550 __ ld(a4, FieldMemOperand(a0, JSFunction::kLiteralsOffset));
1551 int literal_offset =
1552 FixedArray::kHeaderSize + expr->literal_index() * kPointerSize;
1553 __ ld(a5, FieldMemOperand(a4, literal_offset));
1554 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1555 __ Branch(&materialized, ne, a5, Operand(at));
1557 // Create regexp literal using runtime function.
1558 // Result will be in v0.
1559 __ li(a3, Operand(Smi::FromInt(expr->literal_index())));
1560 __ li(a2, Operand(expr->pattern()));
1561 __ li(a1, Operand(expr->flags()));
1562 __ Push(a4, a3, a2, a1);
1563 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
1566 __ bind(&materialized);
1567 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
1568 Label allocated, runtime_allocate;
1569 __ Allocate(size, v0, a2, a3, &runtime_allocate, TAG_OBJECT);
1572 __ bind(&runtime_allocate);
1573 __ li(a0, Operand(Smi::FromInt(size)));
1575 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1578 __ bind(&allocated);
1580 // After this, registers are used as follows:
1581 // v0: Newly allocated regexp.
1582 // a5: Materialized regexp.
1584 __ CopyFields(v0, a5, a2.bit(), size / kPointerSize);
1585 context()->Plug(v0);
1589 void FullCodeGenerator::EmitAccessor(Expression* expression) {
1590 if (expression == NULL) {
1591 __ LoadRoot(a1, Heap::kNullValueRootIndex);
1594 VisitForStackValue(expression);
1599 void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1600 Comment cmnt(masm_, "[ ObjectLiteral");
1602 Handle<FixedArray> constant_properties = expr->constant_properties();
1603 __ ld(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1604 __ ld(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1605 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1606 __ li(a1, Operand(constant_properties));
1607 __ li(a0, Operand(Smi::FromInt(expr->ComputeFlags())));
1608 if (MustCreateObjectLiteralWithRuntime(expr)) {
1609 __ Push(a3, a2, a1, a0);
1610 __ CallRuntime(Runtime::kCreateObjectLiteral, 4);
1612 FastCloneShallowObjectStub stub(isolate(), expr->properties_count());
1615 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1617 // If result_saved is true the result is on top of the stack. If
1618 // result_saved is false the result is in v0.
1619 bool result_saved = false;
1621 AccessorTable accessor_table(zone());
1622 int property_index = 0;
1623 // store_slot_index points to the vector IC slot for the next store IC used.
1624 // ObjectLiteral::ComputeFeedbackRequirements controls the allocation of slots
1625 // and must be updated if the number of store ICs emitted here changes.
1626 int store_slot_index = 0;
1627 for (; property_index < expr->properties()->length(); property_index++) {
1628 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1629 if (property->is_computed_name()) break;
1630 if (property->IsCompileTimeValue()) continue;
1632 Literal* key = property->key()->AsLiteral();
1633 Expression* value = property->value();
1634 if (!result_saved) {
1635 __ push(v0); // Save result on stack.
1636 result_saved = true;
1638 switch (property->kind()) {
1639 case ObjectLiteral::Property::CONSTANT:
1641 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1642 DCHECK(!CompileTimeValue::IsCompileTimeValue(property->value()));
1644 case ObjectLiteral::Property::COMPUTED:
1645 // It is safe to use [[Put]] here because the boilerplate already
1646 // contains computed properties with an uninitialized value.
1647 if (key->value()->IsInternalizedString()) {
1648 if (property->emit_store()) {
1649 VisitForAccumulatorValue(value);
1650 __ mov(StoreDescriptor::ValueRegister(), result_register());
1651 DCHECK(StoreDescriptor::ValueRegister().is(a0));
1652 __ li(StoreDescriptor::NameRegister(), Operand(key->value()));
1653 __ ld(StoreDescriptor::ReceiverRegister(), MemOperand(sp));
1654 if (FLAG_vector_stores) {
1655 EmitLoadStoreICSlot(expr->GetNthSlot(store_slot_index++));
1658 CallStoreIC(key->LiteralFeedbackId());
1660 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1662 if (NeedsHomeObject(value)) {
1663 __ Move(StoreDescriptor::ReceiverRegister(), v0);
1664 __ li(StoreDescriptor::NameRegister(),
1665 Operand(isolate()->factory()->home_object_symbol()));
1666 __ ld(StoreDescriptor::ValueRegister(), MemOperand(sp));
1667 if (FLAG_vector_stores) {
1668 EmitLoadStoreICSlot(expr->GetNthSlot(store_slot_index++));
1673 VisitForEffect(value);
1677 // Duplicate receiver on stack.
1678 __ ld(a0, MemOperand(sp));
1680 VisitForStackValue(key);
1681 VisitForStackValue(value);
1682 if (property->emit_store()) {
1683 EmitSetHomeObjectIfNeeded(
1684 value, 2, expr->SlotForHomeObject(value, &store_slot_index));
1685 __ li(a0, Operand(Smi::FromInt(SLOPPY))); // PropertyAttributes.
1687 __ CallRuntime(Runtime::kSetProperty, 4);
1692 case ObjectLiteral::Property::PROTOTYPE:
1693 // Duplicate receiver on stack.
1694 __ ld(a0, MemOperand(sp));
1696 VisitForStackValue(value);
1697 DCHECK(property->emit_store());
1698 __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1700 case ObjectLiteral::Property::GETTER:
1701 if (property->emit_store()) {
1702 accessor_table.lookup(key)->second->getter = value;
1705 case ObjectLiteral::Property::SETTER:
1706 if (property->emit_store()) {
1707 accessor_table.lookup(key)->second->setter = value;
1713 // Emit code to define accessors, using only a single call to the runtime for
1714 // each pair of corresponding getters and setters.
1715 for (AccessorTable::Iterator it = accessor_table.begin();
1716 it != accessor_table.end();
1718 __ ld(a0, MemOperand(sp)); // Duplicate receiver.
1720 VisitForStackValue(it->first);
1721 EmitAccessor(it->second->getter);
1722 EmitSetHomeObjectIfNeeded(
1723 it->second->getter, 2,
1724 expr->SlotForHomeObject(it->second->getter, &store_slot_index));
1725 EmitAccessor(it->second->setter);
1726 EmitSetHomeObjectIfNeeded(
1727 it->second->setter, 3,
1728 expr->SlotForHomeObject(it->second->setter, &store_slot_index));
1729 __ li(a0, Operand(Smi::FromInt(NONE)));
1731 __ CallRuntime(Runtime::kDefineAccessorPropertyUnchecked, 5);
1734 // Object literals have two parts. The "static" part on the left contains no
1735 // computed property names, and so we can compute its map ahead of time; see
1736 // runtime.cc::CreateObjectLiteralBoilerplate. The second "dynamic" part
1737 // starts with the first computed property name, and continues with all
1738 // properties to its right. All the code from above initializes the static
1739 // component of the object literal, and arranges for the map of the result to
1740 // reflect the static order in which the keys appear. For the dynamic
1741 // properties, we compile them into a series of "SetOwnProperty" runtime
1742 // calls. This will preserve insertion order.
1743 for (; property_index < expr->properties()->length(); property_index++) {
1744 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1746 Expression* value = property->value();
1747 if (!result_saved) {
1748 __ push(v0); // Save result on the stack
1749 result_saved = true;
1752 __ ld(a0, MemOperand(sp)); // Duplicate receiver.
1755 if (property->kind() == ObjectLiteral::Property::PROTOTYPE) {
1756 DCHECK(!property->is_computed_name());
1757 VisitForStackValue(value);
1758 DCHECK(property->emit_store());
1759 __ CallRuntime(Runtime::kInternalSetPrototype, 2);
1761 EmitPropertyKey(property, expr->GetIdForProperty(property_index));
1762 VisitForStackValue(value);
1763 EmitSetHomeObjectIfNeeded(
1764 value, 2, expr->SlotForHomeObject(value, &store_slot_index));
1766 switch (property->kind()) {
1767 case ObjectLiteral::Property::CONSTANT:
1768 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1769 case ObjectLiteral::Property::COMPUTED:
1770 if (property->emit_store()) {
1771 __ li(a0, Operand(Smi::FromInt(NONE)));
1773 __ CallRuntime(Runtime::kDefineDataPropertyUnchecked, 4);
1779 case ObjectLiteral::Property::PROTOTYPE:
1783 case ObjectLiteral::Property::GETTER:
1784 __ li(a0, Operand(Smi::FromInt(NONE)));
1786 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
1789 case ObjectLiteral::Property::SETTER:
1790 __ li(a0, Operand(Smi::FromInt(NONE)));
1792 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
1798 if (expr->has_function()) {
1799 DCHECK(result_saved);
1800 __ ld(a0, MemOperand(sp));
1802 __ CallRuntime(Runtime::kToFastProperties, 1);
1806 context()->PlugTOS();
1808 context()->Plug(v0);
1811 // Verify that compilation exactly consumed the number of store ic slots that
1812 // the ObjectLiteral node had to offer.
1813 DCHECK(!FLAG_vector_stores || store_slot_index == expr->slot_count());
1817 void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1818 Comment cmnt(masm_, "[ ArrayLiteral");
1820 expr->BuildConstantElements(isolate());
1822 Handle<FixedArray> constant_elements = expr->constant_elements();
1823 bool has_fast_elements =
1824 IsFastObjectElementsKind(expr->constant_elements_kind());
1826 AllocationSiteMode allocation_site_mode = TRACK_ALLOCATION_SITE;
1827 if (has_fast_elements && !FLAG_allocation_site_pretenuring) {
1828 // If the only customer of allocation sites is transitioning, then
1829 // we can turn it off if we don't have anywhere else to transition to.
1830 allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
1833 __ mov(a0, result_register());
1834 __ ld(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1835 __ ld(a3, FieldMemOperand(a3, JSFunction::kLiteralsOffset));
1836 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1837 __ li(a1, Operand(constant_elements));
1838 if (MustCreateArrayLiteralWithRuntime(expr)) {
1839 __ li(a0, Operand(Smi::FromInt(expr->ComputeFlags())));
1840 __ Push(a3, a2, a1, a0);
1841 __ CallRuntime(Runtime::kCreateArrayLiteral, 4);
1843 FastCloneShallowArrayStub stub(isolate(), allocation_site_mode);
1846 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1848 bool result_saved = false; // Is the result saved to the stack?
1849 ZoneList<Expression*>* subexprs = expr->values();
1850 int length = subexprs->length();
1852 // Emit code to evaluate all the non-constant subexpressions and to store
1853 // them into the newly cloned array.
1854 int array_index = 0;
1855 for (; array_index < length; array_index++) {
1856 Expression* subexpr = subexprs->at(array_index);
1857 if (subexpr->IsSpread()) break;
1859 // If the subexpression is a literal or a simple materialized literal it
1860 // is already set in the cloned array.
1861 if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
1863 if (!result_saved) {
1864 __ push(v0); // array literal
1865 __ Push(Smi::FromInt(expr->literal_index()));
1866 result_saved = true;
1869 VisitForAccumulatorValue(subexpr);
1871 if (has_fast_elements) {
1872 int offset = FixedArray::kHeaderSize + (array_index * kPointerSize);
1873 __ ld(a6, MemOperand(sp, kPointerSize)); // Copy of array literal.
1874 __ ld(a1, FieldMemOperand(a6, JSObject::kElementsOffset));
1875 __ sd(result_register(), FieldMemOperand(a1, offset));
1876 // Update the write barrier for the array store.
1877 __ RecordWriteField(a1, offset, result_register(), a2,
1878 kRAHasBeenSaved, kDontSaveFPRegs,
1879 EMIT_REMEMBERED_SET, INLINE_SMI_CHECK);
1881 __ li(a3, Operand(Smi::FromInt(array_index)));
1882 __ mov(a0, result_register());
1883 StoreArrayLiteralElementStub stub(isolate());
1887 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1890 // In case the array literal contains spread expressions it has two parts. The
1891 // first part is the "static" array which has a literal index is handled
1892 // above. The second part is the part after the first spread expression
1893 // (inclusive) and these elements gets appended to the array. Note that the
1894 // number elements an iterable produces is unknown ahead of time.
1895 if (array_index < length && result_saved) {
1896 __ Pop(); // literal index
1898 result_saved = false;
1900 for (; array_index < length; array_index++) {
1901 Expression* subexpr = subexprs->at(array_index);
1904 if (subexpr->IsSpread()) {
1905 VisitForStackValue(subexpr->AsSpread()->expression());
1906 __ InvokeBuiltin(Context::CONCAT_ITERABLE_TO_ARRAY_BUILTIN_INDEX,
1909 VisitForStackValue(subexpr);
1910 __ CallRuntime(Runtime::kAppendElement, 2);
1913 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1917 __ Pop(); // literal index
1918 context()->PlugTOS();
1920 context()->Plug(v0);
1925 void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1926 DCHECK(expr->target()->IsValidReferenceExpressionOrThis());
1928 Comment cmnt(masm_, "[ Assignment");
1929 SetExpressionPosition(expr, INSERT_BREAK);
1931 Property* property = expr->target()->AsProperty();
1932 LhsKind assign_type = Property::GetAssignType(property);
1934 // Evaluate LHS expression.
1935 switch (assign_type) {
1937 // Nothing to do here.
1939 case NAMED_PROPERTY:
1940 if (expr->is_compound()) {
1941 // We need the receiver both on the stack and in the register.
1942 VisitForStackValue(property->obj());
1943 __ ld(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
1945 VisitForStackValue(property->obj());
1948 case NAMED_SUPER_PROPERTY:
1950 property->obj()->AsSuperPropertyReference()->this_var());
1951 VisitForAccumulatorValue(
1952 property->obj()->AsSuperPropertyReference()->home_object());
1953 __ Push(result_register());
1954 if (expr->is_compound()) {
1955 const Register scratch = a1;
1956 __ ld(scratch, MemOperand(sp, kPointerSize));
1957 __ Push(scratch, result_register());
1960 case KEYED_SUPER_PROPERTY: {
1961 const Register scratch = a1;
1963 property->obj()->AsSuperPropertyReference()->this_var());
1964 VisitForAccumulatorValue(
1965 property->obj()->AsSuperPropertyReference()->home_object());
1966 __ Move(scratch, result_register());
1967 VisitForAccumulatorValue(property->key());
1968 __ Push(scratch, result_register());
1969 if (expr->is_compound()) {
1970 const Register scratch1 = a4;
1971 __ ld(scratch1, MemOperand(sp, 2 * kPointerSize));
1972 __ Push(scratch1, scratch, result_register());
1976 case KEYED_PROPERTY:
1977 // We need the key and receiver on both the stack and in v0 and a1.
1978 if (expr->is_compound()) {
1979 VisitForStackValue(property->obj());
1980 VisitForStackValue(property->key());
1981 __ ld(LoadDescriptor::ReceiverRegister(),
1982 MemOperand(sp, 1 * kPointerSize));
1983 __ ld(LoadDescriptor::NameRegister(), MemOperand(sp, 0));
1985 VisitForStackValue(property->obj());
1986 VisitForStackValue(property->key());
1991 // For compound assignments we need another deoptimization point after the
1992 // variable/property load.
1993 if (expr->is_compound()) {
1994 { AccumulatorValueContext context(this);
1995 switch (assign_type) {
1997 EmitVariableLoad(expr->target()->AsVariableProxy());
1998 PrepareForBailout(expr->target(), TOS_REG);
2000 case NAMED_PROPERTY:
2001 EmitNamedPropertyLoad(property);
2002 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2004 case NAMED_SUPER_PROPERTY:
2005 EmitNamedSuperPropertyLoad(property);
2006 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2008 case KEYED_SUPER_PROPERTY:
2009 EmitKeyedSuperPropertyLoad(property);
2010 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2012 case KEYED_PROPERTY:
2013 EmitKeyedPropertyLoad(property);
2014 PrepareForBailoutForId(property->LoadId(), TOS_REG);
2019 Token::Value op = expr->binary_op();
2020 __ push(v0); // Left operand goes on the stack.
2021 VisitForAccumulatorValue(expr->value());
2023 AccumulatorValueContext context(this);
2024 if (ShouldInlineSmiCase(op)) {
2025 EmitInlineSmiBinaryOp(expr->binary_operation(),
2030 EmitBinaryOp(expr->binary_operation(), op);
2033 // Deoptimization point in case the binary operation may have side effects.
2034 PrepareForBailout(expr->binary_operation(), TOS_REG);
2036 VisitForAccumulatorValue(expr->value());
2039 SetExpressionPosition(expr);
2042 switch (assign_type) {
2044 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
2045 expr->op(), expr->AssignmentSlot());
2046 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2047 context()->Plug(v0);
2049 case NAMED_PROPERTY:
2050 EmitNamedPropertyAssignment(expr);
2052 case NAMED_SUPER_PROPERTY:
2053 EmitNamedSuperPropertyStore(property);
2054 context()->Plug(v0);
2056 case KEYED_SUPER_PROPERTY:
2057 EmitKeyedSuperPropertyStore(property);
2058 context()->Plug(v0);
2060 case KEYED_PROPERTY:
2061 EmitKeyedPropertyAssignment(expr);
2067 void FullCodeGenerator::VisitYield(Yield* expr) {
2068 Comment cmnt(masm_, "[ Yield");
2069 SetExpressionPosition(expr);
2071 // Evaluate yielded value first; the initial iterator definition depends on
2072 // this. It stays on the stack while we update the iterator.
2073 VisitForStackValue(expr->expression());
2075 switch (expr->yield_kind()) {
2076 case Yield::kSuspend:
2077 // Pop value from top-of-stack slot; box result into result register.
2078 EmitCreateIteratorResult(false);
2079 __ push(result_register());
2081 case Yield::kInitial: {
2082 Label suspend, continuation, post_runtime, resume;
2085 __ bind(&continuation);
2086 __ RecordGeneratorContinuation();
2090 VisitForAccumulatorValue(expr->generator_object());
2091 DCHECK(continuation.pos() > 0 && Smi::IsValid(continuation.pos()));
2092 __ li(a1, Operand(Smi::FromInt(continuation.pos())));
2093 __ sd(a1, FieldMemOperand(v0, JSGeneratorObject::kContinuationOffset));
2094 __ sd(cp, FieldMemOperand(v0, JSGeneratorObject::kContextOffset));
2096 __ RecordWriteField(v0, JSGeneratorObject::kContextOffset, a1, a2,
2097 kRAHasBeenSaved, kDontSaveFPRegs);
2098 __ Daddu(a1, fp, Operand(StandardFrameConstants::kExpressionsOffset));
2099 __ Branch(&post_runtime, eq, sp, Operand(a1));
2100 __ push(v0); // generator object
2101 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
2102 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2103 __ bind(&post_runtime);
2104 __ pop(result_register());
2105 EmitReturnSequence();
2108 context()->Plug(result_register());
2112 case Yield::kFinal: {
2113 VisitForAccumulatorValue(expr->generator_object());
2114 __ li(a1, Operand(Smi::FromInt(JSGeneratorObject::kGeneratorClosed)));
2115 __ sd(a1, FieldMemOperand(result_register(),
2116 JSGeneratorObject::kContinuationOffset));
2117 // Pop value from top-of-stack slot, box result into result register.
2118 EmitCreateIteratorResult(true);
2119 EmitUnwindBeforeReturn();
2120 EmitReturnSequence();
2124 case Yield::kDelegating: {
2125 VisitForStackValue(expr->generator_object());
2127 // Initial stack layout is as follows:
2128 // [sp + 1 * kPointerSize] iter
2129 // [sp + 0 * kPointerSize] g
2131 Label l_catch, l_try, l_suspend, l_continuation, l_resume;
2132 Label l_next, l_call;
2133 Register load_receiver = LoadDescriptor::ReceiverRegister();
2134 Register load_name = LoadDescriptor::NameRegister();
2135 // Initial send value is undefined.
2136 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
2139 // catch (e) { receiver = iter; f = 'throw'; arg = e; goto l_call; }
2142 __ LoadRoot(a2, Heap::kthrow_stringRootIndex); // "throw"
2143 __ ld(a3, MemOperand(sp, 1 * kPointerSize)); // iter
2144 __ Push(a2, a3, a0); // "throw", iter, except
2147 // try { received = %yield result }
2148 // Shuffle the received result above a try handler and yield it without
2151 __ pop(a0); // result
2152 int handler_index = NewHandlerTableEntry();
2153 EnterTryBlock(handler_index, &l_catch);
2154 const int try_block_size = TryCatch::kElementCount * kPointerSize;
2155 __ push(a0); // result
2158 __ bind(&l_continuation);
2159 __ RecordGeneratorContinuation();
2163 __ bind(&l_suspend);
2164 const int generator_object_depth = kPointerSize + try_block_size;
2165 __ ld(a0, MemOperand(sp, generator_object_depth));
2167 __ Push(Smi::FromInt(handler_index)); // handler-index
2168 DCHECK(l_continuation.pos() > 0 && Smi::IsValid(l_continuation.pos()));
2169 __ li(a1, Operand(Smi::FromInt(l_continuation.pos())));
2170 __ sd(a1, FieldMemOperand(a0, JSGeneratorObject::kContinuationOffset));
2171 __ sd(cp, FieldMemOperand(a0, JSGeneratorObject::kContextOffset));
2173 __ RecordWriteField(a0, JSGeneratorObject::kContextOffset, a1, a2,
2174 kRAHasBeenSaved, kDontSaveFPRegs);
2175 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 2);
2176 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2177 __ pop(v0); // result
2178 EmitReturnSequence();
2180 __ bind(&l_resume); // received in a0
2181 ExitTryBlock(handler_index);
2183 // receiver = iter; f = 'next'; arg = received;
2185 __ LoadRoot(load_name, Heap::knext_stringRootIndex); // "next"
2186 __ ld(a3, MemOperand(sp, 1 * kPointerSize)); // iter
2187 __ Push(load_name, a3, a0); // "next", iter, received
2189 // result = receiver[f](arg);
2191 __ ld(load_receiver, MemOperand(sp, kPointerSize));
2192 __ ld(load_name, MemOperand(sp, 2 * kPointerSize));
2193 __ li(LoadDescriptor::SlotRegister(),
2194 Operand(SmiFromSlot(expr->KeyedLoadFeedbackSlot())));
2195 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), SLOPPY).code();
2196 CallIC(ic, TypeFeedbackId::None());
2199 __ sd(a1, MemOperand(sp, 2 * kPointerSize));
2200 SetCallPosition(expr, 1);
2201 CallFunctionStub stub(isolate(), 1, CALL_AS_METHOD);
2204 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2205 __ Drop(1); // The function is still on the stack; drop it.
2207 // if (!result.done) goto l_try;
2208 __ Move(load_receiver, v0);
2210 __ push(load_receiver); // save result
2211 __ LoadRoot(load_name, Heap::kdone_stringRootIndex); // "done"
2212 __ li(LoadDescriptor::SlotRegister(),
2213 Operand(SmiFromSlot(expr->DoneFeedbackSlot())));
2214 CallLoadIC(NOT_INSIDE_TYPEOF); // v0=result.done
2216 Handle<Code> bool_ic = ToBooleanStub::GetUninitialized(isolate());
2218 __ Branch(&l_try, eq, v0, Operand(zero_reg));
2221 __ pop(load_receiver); // result
2222 __ LoadRoot(load_name, Heap::kvalue_stringRootIndex); // "value"
2223 __ li(LoadDescriptor::SlotRegister(),
2224 Operand(SmiFromSlot(expr->ValueFeedbackSlot())));
2225 CallLoadIC(NOT_INSIDE_TYPEOF); // v0=result.value
2226 context()->DropAndPlug(2, v0); // drop iter and g
2233 void FullCodeGenerator::EmitGeneratorResume(Expression *generator,
2235 JSGeneratorObject::ResumeMode resume_mode) {
2236 // The value stays in a0, and is ultimately read by the resumed generator, as
2237 // if CallRuntime(Runtime::kSuspendJSGeneratorObject) returned it. Or it
2238 // is read to throw the value when the resumed generator is already closed.
2239 // a1 will hold the generator object until the activation has been resumed.
2240 VisitForStackValue(generator);
2241 VisitForAccumulatorValue(value);
2244 // Load suspended function and context.
2245 __ ld(cp, FieldMemOperand(a1, JSGeneratorObject::kContextOffset));
2246 __ ld(a4, FieldMemOperand(a1, JSGeneratorObject::kFunctionOffset));
2248 // Load receiver and store as the first argument.
2249 __ ld(a2, FieldMemOperand(a1, JSGeneratorObject::kReceiverOffset));
2252 // Push holes for the rest of the arguments to the generator function.
2253 __ ld(a3, FieldMemOperand(a4, JSFunction::kSharedFunctionInfoOffset));
2254 // The argument count is stored as int32_t on 64-bit platforms.
2255 // TODO(plind): Smi on 32-bit platforms.
2257 FieldMemOperand(a3, SharedFunctionInfo::kFormalParameterCountOffset));
2258 __ LoadRoot(a2, Heap::kTheHoleValueRootIndex);
2259 Label push_argument_holes, push_frame;
2260 __ bind(&push_argument_holes);
2261 __ Dsubu(a3, a3, Operand(1));
2262 __ Branch(&push_frame, lt, a3, Operand(zero_reg));
2264 __ jmp(&push_argument_holes);
2266 // Enter a new JavaScript frame, and initialize its slots as they were when
2267 // the generator was suspended.
2268 Label resume_frame, done;
2269 __ bind(&push_frame);
2270 __ Call(&resume_frame);
2272 __ bind(&resume_frame);
2273 // ra = return address.
2274 // fp = caller's frame pointer.
2275 // cp = callee's context,
2276 // a4 = callee's JS function.
2277 __ Push(ra, fp, cp, a4);
2278 // Adjust FP to point to saved FP.
2279 __ Daddu(fp, sp, 2 * kPointerSize);
2281 // Load the operand stack size.
2282 __ ld(a3, FieldMemOperand(a1, JSGeneratorObject::kOperandStackOffset));
2283 __ ld(a3, FieldMemOperand(a3, FixedArray::kLengthOffset));
2286 // If we are sending a value and there is no operand stack, we can jump back
2288 if (resume_mode == JSGeneratorObject::NEXT) {
2290 __ Branch(&slow_resume, ne, a3, Operand(zero_reg));
2291 __ ld(a3, FieldMemOperand(a4, JSFunction::kCodeEntryOffset));
2292 __ ld(a2, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
2294 __ Daddu(a3, a3, Operand(a2));
2295 __ li(a2, Operand(Smi::FromInt(JSGeneratorObject::kGeneratorExecuting)));
2296 __ sd(a2, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
2298 __ bind(&slow_resume);
2301 // Otherwise, we push holes for the operand stack and call the runtime to fix
2302 // up the stack and the handlers.
2303 Label push_operand_holes, call_resume;
2304 __ bind(&push_operand_holes);
2305 __ Dsubu(a3, a3, Operand(1));
2306 __ Branch(&call_resume, lt, a3, Operand(zero_reg));
2308 __ Branch(&push_operand_holes);
2309 __ bind(&call_resume);
2310 DCHECK(!result_register().is(a1));
2311 __ Push(a1, result_register());
2312 __ Push(Smi::FromInt(resume_mode));
2313 __ CallRuntime(Runtime::kResumeJSGeneratorObject, 3);
2314 // Not reached: the runtime call returns elsewhere.
2315 __ stop("not-reached");
2318 context()->Plug(result_register());
2322 void FullCodeGenerator::EmitCreateIteratorResult(bool done) {
2326 const int instance_size = 5 * kPointerSize;
2327 DCHECK_EQ(isolate()->native_context()->iterator_result_map()->instance_size(),
2330 __ Allocate(instance_size, v0, a2, a3, &gc_required, TAG_OBJECT);
2333 __ bind(&gc_required);
2334 __ Push(Smi::FromInt(instance_size));
2335 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
2336 __ ld(context_register(),
2337 MemOperand(fp, StandardFrameConstants::kContextOffset));
2339 __ bind(&allocated);
2340 __ ld(a1, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
2341 __ ld(a1, FieldMemOperand(a1, GlobalObject::kNativeContextOffset));
2342 __ ld(a1, ContextOperand(a1, Context::ITERATOR_RESULT_MAP_INDEX));
2344 __ li(a3, Operand(isolate()->factory()->ToBoolean(done)));
2345 __ li(a4, Operand(isolate()->factory()->empty_fixed_array()));
2346 __ sd(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2347 __ sd(a4, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2348 __ sd(a4, FieldMemOperand(v0, JSObject::kElementsOffset));
2350 FieldMemOperand(v0, JSGeneratorObject::kResultValuePropertyOffset));
2352 FieldMemOperand(v0, JSGeneratorObject::kResultDonePropertyOffset));
2354 // Only the value field needs a write barrier, as the other values are in the
2356 __ RecordWriteField(v0, JSGeneratorObject::kResultValuePropertyOffset,
2357 a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
2361 void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
2362 SetExpressionPosition(prop);
2363 Literal* key = prop->key()->AsLiteral();
2364 DCHECK(!prop->IsSuperAccess());
2366 __ li(LoadDescriptor::NameRegister(), Operand(key->value()));
2367 __ li(LoadDescriptor::SlotRegister(),
2368 Operand(SmiFromSlot(prop->PropertyFeedbackSlot())));
2369 CallLoadIC(NOT_INSIDE_TYPEOF, language_mode());
2373 void FullCodeGenerator::EmitNamedSuperPropertyLoad(Property* prop) {
2374 // Stack: receiver, home_object.
2375 SetExpressionPosition(prop);
2377 Literal* key = prop->key()->AsLiteral();
2378 DCHECK(!key->value()->IsSmi());
2379 DCHECK(prop->IsSuperAccess());
2381 __ Push(key->value());
2382 __ Push(Smi::FromInt(language_mode()));
2383 __ CallRuntime(Runtime::kLoadFromSuper, 4);
2387 void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
2388 // Call keyed load IC. It has register arguments receiver and key.
2389 SetExpressionPosition(prop);
2391 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), language_mode()).code();
2392 __ li(LoadDescriptor::SlotRegister(),
2393 Operand(SmiFromSlot(prop->PropertyFeedbackSlot())));
2398 void FullCodeGenerator::EmitKeyedSuperPropertyLoad(Property* prop) {
2399 // Stack: receiver, home_object, key.
2400 SetExpressionPosition(prop);
2401 __ Push(Smi::FromInt(language_mode()));
2402 __ CallRuntime(Runtime::kLoadKeyedFromSuper, 4);
2406 void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
2408 Expression* left_expr,
2409 Expression* right_expr) {
2410 Label done, smi_case, stub_call;
2412 Register scratch1 = a2;
2413 Register scratch2 = a3;
2415 // Get the arguments.
2417 Register right = a0;
2419 __ mov(a0, result_register());
2421 // Perform combined smi check on both operands.
2422 __ Or(scratch1, left, Operand(right));
2423 STATIC_ASSERT(kSmiTag == 0);
2424 JumpPatchSite patch_site(masm_);
2425 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
2427 __ bind(&stub_call);
2429 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2430 CallIC(code, expr->BinaryOperationFeedbackId());
2431 patch_site.EmitPatchInfo();
2435 // Smi case. This code works the same way as the smi-smi case in the type
2436 // recording binary operation stub, see
2439 __ GetLeastBitsFromSmi(scratch1, right, 5);
2440 __ dsrav(right, left, scratch1);
2441 __ And(v0, right, Operand(0xffffffff00000000L));
2444 __ SmiUntag(scratch1, left);
2445 __ GetLeastBitsFromSmi(scratch2, right, 5);
2446 __ dsllv(scratch1, scratch1, scratch2);
2447 __ SmiTag(v0, scratch1);
2451 __ SmiUntag(scratch1, left);
2452 __ GetLeastBitsFromSmi(scratch2, right, 5);
2453 __ dsrlv(scratch1, scratch1, scratch2);
2454 __ And(scratch2, scratch1, 0x80000000);
2455 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
2456 __ SmiTag(v0, scratch1);
2460 __ DadduAndCheckForOverflow(v0, left, right, scratch1);
2461 __ BranchOnOverflow(&stub_call, scratch1);
2464 __ DsubuAndCheckForOverflow(v0, left, right, scratch1);
2465 __ BranchOnOverflow(&stub_call, scratch1);
2468 __ Dmulh(v0, left, right);
2469 __ dsra32(scratch2, v0, 0);
2470 __ sra(scratch1, v0, 31);
2471 __ Branch(USE_DELAY_SLOT, &stub_call, ne, scratch2, Operand(scratch1));
2473 __ Branch(USE_DELAY_SLOT, &done, ne, v0, Operand(zero_reg));
2474 __ Daddu(scratch2, right, left);
2475 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
2476 DCHECK(Smi::FromInt(0) == 0);
2477 __ mov(v0, zero_reg);
2481 __ Or(v0, left, Operand(right));
2483 case Token::BIT_AND:
2484 __ And(v0, left, Operand(right));
2486 case Token::BIT_XOR:
2487 __ Xor(v0, left, Operand(right));
2494 context()->Plug(v0);
2498 void FullCodeGenerator::EmitClassDefineProperties(ClassLiteral* lit,
2499 int* used_store_slots) {
2500 // Constructor is in v0.
2501 DCHECK(lit != NULL);
2504 // No access check is needed here since the constructor is created by the
2506 Register scratch = a1;
2508 FieldMemOperand(v0, JSFunction::kPrototypeOrInitialMapOffset));
2511 for (int i = 0; i < lit->properties()->length(); i++) {
2512 ObjectLiteral::Property* property = lit->properties()->at(i);
2513 Expression* value = property->value();
2515 if (property->is_static()) {
2516 __ ld(scratch, MemOperand(sp, kPointerSize)); // constructor
2518 __ ld(scratch, MemOperand(sp, 0)); // prototype
2521 EmitPropertyKey(property, lit->GetIdForProperty(i));
2523 // The static prototype property is read only. We handle the non computed
2524 // property name case in the parser. Since this is the only case where we
2525 // need to check for an own read only property we special case this so we do
2526 // not need to do this for every property.
2527 if (property->is_static() && property->is_computed_name()) {
2528 __ CallRuntime(Runtime::kThrowIfStaticPrototype, 1);
2532 VisitForStackValue(value);
2533 EmitSetHomeObjectIfNeeded(value, 2,
2534 lit->SlotForHomeObject(value, used_store_slots));
2536 switch (property->kind()) {
2537 case ObjectLiteral::Property::CONSTANT:
2538 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2539 case ObjectLiteral::Property::PROTOTYPE:
2541 case ObjectLiteral::Property::COMPUTED:
2542 __ CallRuntime(Runtime::kDefineClassMethod, 3);
2545 case ObjectLiteral::Property::GETTER:
2546 __ li(a0, Operand(Smi::FromInt(DONT_ENUM)));
2548 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked, 4);
2551 case ObjectLiteral::Property::SETTER:
2552 __ li(a0, Operand(Smi::FromInt(DONT_ENUM)));
2554 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked, 4);
2562 // Set both the prototype and constructor to have fast properties, and also
2563 // freeze them in strong mode.
2564 __ CallRuntime(Runtime::kFinalizeClassDefinition, 2);
2568 void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr, Token::Value op) {
2569 __ mov(a0, result_register());
2572 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2573 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
2574 CallIC(code, expr->BinaryOperationFeedbackId());
2575 patch_site.EmitPatchInfo();
2576 context()->Plug(v0);
2580 void FullCodeGenerator::EmitAssignment(Expression* expr,
2581 FeedbackVectorICSlot slot) {
2582 DCHECK(expr->IsValidReferenceExpressionOrThis());
2584 Property* prop = expr->AsProperty();
2585 LhsKind assign_type = Property::GetAssignType(prop);
2587 switch (assign_type) {
2589 Variable* var = expr->AsVariableProxy()->var();
2590 EffectContext context(this);
2591 EmitVariableAssignment(var, Token::ASSIGN, slot);
2594 case NAMED_PROPERTY: {
2595 __ push(result_register()); // Preserve value.
2596 VisitForAccumulatorValue(prop->obj());
2597 __ mov(StoreDescriptor::ReceiverRegister(), result_register());
2598 __ pop(StoreDescriptor::ValueRegister()); // Restore value.
2599 __ li(StoreDescriptor::NameRegister(),
2600 Operand(prop->key()->AsLiteral()->value()));
2601 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2605 case NAMED_SUPER_PROPERTY: {
2607 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2608 VisitForAccumulatorValue(
2609 prop->obj()->AsSuperPropertyReference()->home_object());
2610 // stack: value, this; v0: home_object
2611 Register scratch = a2;
2612 Register scratch2 = a3;
2613 __ mov(scratch, result_register()); // home_object
2614 __ ld(v0, MemOperand(sp, kPointerSize)); // value
2615 __ ld(scratch2, MemOperand(sp, 0)); // this
2616 __ sd(scratch2, MemOperand(sp, kPointerSize)); // this
2617 __ sd(scratch, MemOperand(sp, 0)); // home_object
2618 // stack: this, home_object; v0: value
2619 EmitNamedSuperPropertyStore(prop);
2622 case KEYED_SUPER_PROPERTY: {
2624 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2626 prop->obj()->AsSuperPropertyReference()->home_object());
2627 VisitForAccumulatorValue(prop->key());
2628 Register scratch = a2;
2629 Register scratch2 = a3;
2630 __ ld(scratch2, MemOperand(sp, 2 * kPointerSize)); // value
2631 // stack: value, this, home_object; v0: key, a3: value
2632 __ ld(scratch, MemOperand(sp, kPointerSize)); // this
2633 __ sd(scratch, MemOperand(sp, 2 * kPointerSize));
2634 __ ld(scratch, MemOperand(sp, 0)); // home_object
2635 __ sd(scratch, MemOperand(sp, kPointerSize));
2636 __ sd(v0, MemOperand(sp, 0));
2637 __ Move(v0, scratch2);
2638 // stack: this, home_object, key; v0: value.
2639 EmitKeyedSuperPropertyStore(prop);
2642 case KEYED_PROPERTY: {
2643 __ push(result_register()); // Preserve value.
2644 VisitForStackValue(prop->obj());
2645 VisitForAccumulatorValue(prop->key());
2646 __ Move(StoreDescriptor::NameRegister(), result_register());
2647 __ Pop(StoreDescriptor::ValueRegister(),
2648 StoreDescriptor::ReceiverRegister());
2649 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2651 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2656 context()->Plug(v0);
2660 void FullCodeGenerator::EmitStoreToStackLocalOrContextSlot(
2661 Variable* var, MemOperand location) {
2662 __ sd(result_register(), location);
2663 if (var->IsContextSlot()) {
2664 // RecordWrite may destroy all its register arguments.
2665 __ Move(a3, result_register());
2666 int offset = Context::SlotOffset(var->index());
2667 __ RecordWriteContextSlot(
2668 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
2673 void FullCodeGenerator::EmitVariableAssignment(Variable* var, Token::Value op,
2674 FeedbackVectorICSlot slot) {
2675 if (var->IsUnallocated()) {
2676 // Global var, const, or let.
2677 __ mov(StoreDescriptor::ValueRegister(), result_register());
2678 __ li(StoreDescriptor::NameRegister(), Operand(var->name()));
2679 __ ld(StoreDescriptor::ReceiverRegister(), GlobalObjectOperand());
2680 if (FLAG_vector_stores) EmitLoadStoreICSlot(slot);
2683 } else if (var->IsGlobalSlot()) {
2684 // Global var, const, or let.
2685 DCHECK(var->index() > 0);
2686 DCHECK(var->IsStaticGlobalObjectProperty());
2687 DCHECK(StoreGlobalViaContextDescriptor::ValueRegister().is(a0));
2688 __ mov(StoreGlobalViaContextDescriptor::ValueRegister(), result_register());
2689 int const slot = var->index();
2690 int const depth = scope()->ContextChainLength(var->scope());
2691 if (depth <= StoreGlobalViaContextStub::kMaximumDepth) {
2692 __ li(StoreGlobalViaContextDescriptor::SlotRegister(), Operand(slot));
2693 StoreGlobalViaContextStub stub(isolate(), depth, language_mode());
2696 __ Push(Smi::FromInt(slot));
2698 __ CallRuntime(is_strict(language_mode())
2699 ? Runtime::kStoreGlobalViaContext_Strict
2700 : Runtime::kStoreGlobalViaContext_Sloppy,
2704 } else if (var->mode() == LET && op != Token::INIT_LET) {
2705 // Non-initializing assignment to let variable needs a write barrier.
2706 DCHECK(!var->IsLookupSlot());
2707 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2709 MemOperand location = VarOperand(var, a1);
2710 __ ld(a3, location);
2711 __ LoadRoot(a4, Heap::kTheHoleValueRootIndex);
2712 __ Branch(&assign, ne, a3, Operand(a4));
2713 __ li(a3, Operand(var->name()));
2715 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2716 // Perform the assignment.
2718 EmitStoreToStackLocalOrContextSlot(var, location);
2720 } else if (var->mode() == CONST && op != Token::INIT_CONST) {
2721 // Assignment to const variable needs a write barrier.
2722 DCHECK(!var->IsLookupSlot());
2723 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2725 MemOperand location = VarOperand(var, a1);
2726 __ ld(a3, location);
2727 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2728 __ Branch(&const_error, ne, a3, Operand(at));
2729 __ li(a3, Operand(var->name()));
2731 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2732 __ bind(&const_error);
2733 __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2735 } else if (var->is_this() && op == Token::INIT_CONST) {
2736 // Initializing assignment to const {this} needs a write barrier.
2737 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2738 Label uninitialized_this;
2739 MemOperand location = VarOperand(var, a1);
2740 __ ld(a3, location);
2741 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2742 __ Branch(&uninitialized_this, eq, a3, Operand(at));
2743 __ li(a0, Operand(var->name()));
2745 __ CallRuntime(Runtime::kThrowReferenceError, 1);
2746 __ bind(&uninitialized_this);
2747 EmitStoreToStackLocalOrContextSlot(var, location);
2749 } else if (!var->is_const_mode() || op == Token::INIT_CONST) {
2750 if (var->IsLookupSlot()) {
2751 // Assignment to var.
2752 __ li(a4, Operand(var->name()));
2753 __ li(a3, Operand(Smi::FromInt(language_mode())));
2754 // jssp[0] : language mode.
2756 // jssp[16] : context.
2757 // jssp[24] : value.
2758 __ Push(v0, cp, a4, a3);
2759 __ CallRuntime(Runtime::kStoreLookupSlot, 4);
2761 // Assignment to var or initializing assignment to let/const in harmony
2763 DCHECK((var->IsStackAllocated() || var->IsContextSlot()));
2764 MemOperand location = VarOperand(var, a1);
2765 if (generate_debug_code_ && op == Token::INIT_LET) {
2766 // Check for an uninitialized let binding.
2767 __ ld(a2, location);
2768 __ LoadRoot(a4, Heap::kTheHoleValueRootIndex);
2769 __ Check(eq, kLetBindingReInitialization, a2, Operand(a4));
2771 EmitStoreToStackLocalOrContextSlot(var, location);
2774 } else if (op == Token::INIT_CONST_LEGACY) {
2775 // Const initializers need a write barrier.
2776 DCHECK(!var->IsParameter()); // No const parameters.
2777 if (var->IsLookupSlot()) {
2778 __ li(a0, Operand(var->name()));
2779 __ Push(v0, cp, a0); // Context and name.
2780 __ CallRuntime(Runtime::kInitializeLegacyConstLookupSlot, 3);
2782 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2784 MemOperand location = VarOperand(var, a1);
2785 __ ld(a2, location);
2786 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2787 __ Branch(&skip, ne, a2, Operand(at));
2788 EmitStoreToStackLocalOrContextSlot(var, location);
2793 DCHECK(var->mode() == CONST_LEGACY && op != Token::INIT_CONST_LEGACY);
2794 if (is_strict(language_mode())) {
2795 __ CallRuntime(Runtime::kThrowConstAssignError, 0);
2797 // Silently ignore store in sloppy mode.
2802 void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2803 // Assignment to a property, using a named store IC.
2804 Property* prop = expr->target()->AsProperty();
2805 DCHECK(prop != NULL);
2806 DCHECK(prop->key()->IsLiteral());
2808 __ mov(StoreDescriptor::ValueRegister(), result_register());
2809 __ li(StoreDescriptor::NameRegister(),
2810 Operand(prop->key()->AsLiteral()->value()));
2811 __ pop(StoreDescriptor::ReceiverRegister());
2812 if (FLAG_vector_stores) {
2813 EmitLoadStoreICSlot(expr->AssignmentSlot());
2816 CallStoreIC(expr->AssignmentFeedbackId());
2819 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2820 context()->Plug(v0);
2824 void FullCodeGenerator::EmitNamedSuperPropertyStore(Property* prop) {
2825 // Assignment to named property of super.
2827 // stack : receiver ('this'), home_object
2828 DCHECK(prop != NULL);
2829 Literal* key = prop->key()->AsLiteral();
2830 DCHECK(key != NULL);
2832 __ Push(key->value());
2834 __ CallRuntime((is_strict(language_mode()) ? Runtime::kStoreToSuper_Strict
2835 : Runtime::kStoreToSuper_Sloppy),
2840 void FullCodeGenerator::EmitKeyedSuperPropertyStore(Property* prop) {
2841 // Assignment to named property of super.
2843 // stack : receiver ('this'), home_object, key
2844 DCHECK(prop != NULL);
2848 (is_strict(language_mode()) ? Runtime::kStoreKeyedToSuper_Strict
2849 : Runtime::kStoreKeyedToSuper_Sloppy),
2854 void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2855 // Assignment to a property, using a keyed store IC.
2856 // Call keyed store IC.
2857 // The arguments are:
2858 // - a0 is the value,
2860 // - a2 is the receiver.
2861 __ mov(StoreDescriptor::ValueRegister(), result_register());
2862 __ Pop(StoreDescriptor::ReceiverRegister(), StoreDescriptor::NameRegister());
2863 DCHECK(StoreDescriptor::ValueRegister().is(a0));
2866 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2867 if (FLAG_vector_stores) {
2868 EmitLoadStoreICSlot(expr->AssignmentSlot());
2871 CallIC(ic, expr->AssignmentFeedbackId());
2874 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2875 context()->Plug(v0);
2879 void FullCodeGenerator::VisitProperty(Property* expr) {
2880 Comment cmnt(masm_, "[ Property");
2881 SetExpressionPosition(expr);
2883 Expression* key = expr->key();
2885 if (key->IsPropertyName()) {
2886 if (!expr->IsSuperAccess()) {
2887 VisitForAccumulatorValue(expr->obj());
2888 __ Move(LoadDescriptor::ReceiverRegister(), v0);
2889 EmitNamedPropertyLoad(expr);
2891 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2893 expr->obj()->AsSuperPropertyReference()->home_object());
2894 EmitNamedSuperPropertyLoad(expr);
2897 if (!expr->IsSuperAccess()) {
2898 VisitForStackValue(expr->obj());
2899 VisitForAccumulatorValue(expr->key());
2900 __ Move(LoadDescriptor::NameRegister(), v0);
2901 __ pop(LoadDescriptor::ReceiverRegister());
2902 EmitKeyedPropertyLoad(expr);
2904 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2906 expr->obj()->AsSuperPropertyReference()->home_object());
2907 VisitForStackValue(expr->key());
2908 EmitKeyedSuperPropertyLoad(expr);
2911 PrepareForBailoutForId(expr->LoadId(), TOS_REG);
2912 context()->Plug(v0);
2916 void FullCodeGenerator::CallIC(Handle<Code> code,
2917 TypeFeedbackId id) {
2919 __ Call(code, RelocInfo::CODE_TARGET, id);
2923 // Code common for calls using the IC.
2924 void FullCodeGenerator::EmitCallWithLoadIC(Call* expr) {
2925 Expression* callee = expr->expression();
2927 CallICState::CallType call_type =
2928 callee->IsVariableProxy() ? CallICState::FUNCTION : CallICState::METHOD;
2930 // Get the target function.
2931 if (call_type == CallICState::FUNCTION) {
2932 { StackValueContext context(this);
2933 EmitVariableLoad(callee->AsVariableProxy());
2934 PrepareForBailout(callee, NO_REGISTERS);
2936 // Push undefined as receiver. This is patched in the method prologue if it
2937 // is a sloppy mode method.
2938 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
2941 // Load the function from the receiver.
2942 DCHECK(callee->IsProperty());
2943 DCHECK(!callee->AsProperty()->IsSuperAccess());
2944 __ ld(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
2945 EmitNamedPropertyLoad(callee->AsProperty());
2946 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2947 // Push the target function under the receiver.
2948 __ ld(at, MemOperand(sp, 0));
2950 __ sd(v0, MemOperand(sp, kPointerSize));
2953 EmitCall(expr, call_type);
2957 void FullCodeGenerator::EmitSuperCallWithLoadIC(Call* expr) {
2958 SetExpressionPosition(expr);
2959 Expression* callee = expr->expression();
2960 DCHECK(callee->IsProperty());
2961 Property* prop = callee->AsProperty();
2962 DCHECK(prop->IsSuperAccess());
2964 Literal* key = prop->key()->AsLiteral();
2965 DCHECK(!key->value()->IsSmi());
2966 // Load the function from the receiver.
2967 const Register scratch = a1;
2968 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
2969 VisitForAccumulatorValue(super_ref->home_object());
2970 __ mov(scratch, v0);
2971 VisitForAccumulatorValue(super_ref->this_var());
2972 __ Push(scratch, v0, v0, scratch);
2973 __ Push(key->value());
2974 __ Push(Smi::FromInt(language_mode()));
2978 // - this (receiver)
2979 // - this (receiver) <-- LoadFromSuper will pop here and below.
2983 __ CallRuntime(Runtime::kLoadFromSuper, 4);
2985 // Replace home_object with target function.
2986 __ sd(v0, MemOperand(sp, kPointerSize));
2989 // - target function
2990 // - this (receiver)
2991 EmitCall(expr, CallICState::METHOD);
2995 // Code common for calls using the IC.
2996 void FullCodeGenerator::EmitKeyedCallWithLoadIC(Call* expr,
2999 VisitForAccumulatorValue(key);
3001 Expression* callee = expr->expression();
3003 // Load the function from the receiver.
3004 DCHECK(callee->IsProperty());
3005 __ ld(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
3006 __ Move(LoadDescriptor::NameRegister(), v0);
3007 EmitKeyedPropertyLoad(callee->AsProperty());
3008 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
3010 // Push the target function under the receiver.
3011 __ ld(at, MemOperand(sp, 0));
3013 __ sd(v0, MemOperand(sp, kPointerSize));
3015 EmitCall(expr, CallICState::METHOD);
3019 void FullCodeGenerator::EmitKeyedSuperCallWithLoadIC(Call* expr) {
3020 Expression* callee = expr->expression();
3021 DCHECK(callee->IsProperty());
3022 Property* prop = callee->AsProperty();
3023 DCHECK(prop->IsSuperAccess());
3025 SetExpressionPosition(prop);
3026 // Load the function from the receiver.
3027 const Register scratch = a1;
3028 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
3029 VisitForAccumulatorValue(super_ref->home_object());
3030 __ Move(scratch, v0);
3031 VisitForAccumulatorValue(super_ref->this_var());
3032 __ Push(scratch, v0, v0, scratch);
3033 VisitForStackValue(prop->key());
3034 __ Push(Smi::FromInt(language_mode()));
3038 // - this (receiver)
3039 // - this (receiver) <-- LoadKeyedFromSuper will pop here and below.
3043 __ CallRuntime(Runtime::kLoadKeyedFromSuper, 4);
3045 // Replace home_object with target function.
3046 __ sd(v0, MemOperand(sp, kPointerSize));
3049 // - target function
3050 // - this (receiver)
3051 EmitCall(expr, CallICState::METHOD);
3055 void FullCodeGenerator::EmitCall(Call* expr, CallICState::CallType call_type) {
3056 // Load the arguments.
3057 ZoneList<Expression*>* args = expr->arguments();
3058 int arg_count = args->length();
3059 for (int i = 0; i < arg_count; i++) {
3060 VisitForStackValue(args->at(i));
3063 // Record source position of the IC call.
3064 SetCallPosition(expr, arg_count);
3065 Handle<Code> ic = CodeFactory::CallIC(isolate(), arg_count, call_type).code();
3066 __ li(a3, Operand(SmiFromSlot(expr->CallFeedbackICSlot())));
3067 __ ld(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
3068 // Don't assign a type feedback id to the IC, since type feedback is provided
3069 // by the vector above.
3071 RecordJSReturnSite(expr);
3072 // Restore context register.
3073 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3074 context()->DropAndPlug(1, v0);
3078 void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
3079 // a6: copy of the first argument or undefined if it doesn't exist.
3080 if (arg_count > 0) {
3081 __ ld(a6, MemOperand(sp, arg_count * kPointerSize));
3083 __ LoadRoot(a6, Heap::kUndefinedValueRootIndex);
3086 // a5: the receiver of the enclosing function.
3087 __ ld(a5, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3089 // a4: the language mode.
3090 __ li(a4, Operand(Smi::FromInt(language_mode())));
3092 // a1: the start position of the scope the calls resides in.
3093 __ li(a1, Operand(Smi::FromInt(scope()->start_position())));
3095 // Do the runtime call.
3096 __ Push(a6, a5, a4, a1);
3097 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 5);
3101 // See http://www.ecma-international.org/ecma-262/6.0/#sec-function-calls.
3102 void FullCodeGenerator::PushCalleeAndWithBaseObject(Call* expr) {
3103 VariableProxy* callee = expr->expression()->AsVariableProxy();
3104 if (callee->var()->IsLookupSlot()) {
3107 SetExpressionPosition(callee);
3108 // Generate code for loading from variables potentially shadowed by
3109 // eval-introduced variables.
3110 EmitDynamicLookupFastCase(callee, NOT_INSIDE_TYPEOF, &slow, &done);
3113 // Call the runtime to find the function to call (returned in v0)
3114 // and the object holding it (returned in v1).
3115 DCHECK(!context_register().is(a2));
3116 __ li(a2, Operand(callee->name()));
3117 __ Push(context_register(), a2);
3118 __ CallRuntime(Runtime::kLoadLookupSlot, 2);
3119 __ Push(v0, v1); // Function, receiver.
3120 PrepareForBailoutForId(expr->LookupId(), NO_REGISTERS);
3122 // If fast case code has been generated, emit code to push the
3123 // function and receiver and have the slow path jump around this
3125 if (done.is_linked()) {
3131 // The receiver is implicitly the global receiver. Indicate this
3132 // by passing the hole to the call function stub.
3133 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
3138 VisitForStackValue(callee);
3139 // refEnv.WithBaseObject()
3140 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
3141 __ push(a2); // Reserved receiver slot.
3146 void FullCodeGenerator::VisitCall(Call* expr) {
3148 // We want to verify that RecordJSReturnSite gets called on all paths
3149 // through this function. Avoid early returns.
3150 expr->return_is_recorded_ = false;
3153 Comment cmnt(masm_, "[ Call");
3154 Expression* callee = expr->expression();
3155 Call::CallType call_type = expr->GetCallType(isolate());
3157 if (call_type == Call::POSSIBLY_EVAL_CALL) {
3158 // In a call to eval, we first call RuntimeHidden_ResolvePossiblyDirectEval
3159 // to resolve the function we need to call. Then we call the resolved
3160 // function using the given arguments.
3161 ZoneList<Expression*>* args = expr->arguments();
3162 int arg_count = args->length();
3163 PushCalleeAndWithBaseObject(expr);
3165 // Push the arguments.
3166 for (int i = 0; i < arg_count; i++) {
3167 VisitForStackValue(args->at(i));
3170 // Push a copy of the function (found below the arguments) and
3172 __ ld(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
3174 EmitResolvePossiblyDirectEval(arg_count);
3176 // Touch up the stack with the resolved function.
3177 __ sd(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
3179 PrepareForBailoutForId(expr->EvalId(), NO_REGISTERS);
3180 // Record source position for debugger.
3181 SetCallPosition(expr, arg_count);
3182 CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
3183 __ ld(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
3185 RecordJSReturnSite(expr);
3186 // Restore context register.
3187 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3188 context()->DropAndPlug(1, v0);
3189 } else if (call_type == Call::GLOBAL_CALL) {
3190 EmitCallWithLoadIC(expr);
3191 } else if (call_type == Call::LOOKUP_SLOT_CALL) {
3192 // Call to a lookup slot (dynamically introduced variable).
3193 PushCalleeAndWithBaseObject(expr);
3195 } else if (call_type == Call::PROPERTY_CALL) {
3196 Property* property = callee->AsProperty();
3197 bool is_named_call = property->key()->IsPropertyName();
3198 if (property->IsSuperAccess()) {
3199 if (is_named_call) {
3200 EmitSuperCallWithLoadIC(expr);
3202 EmitKeyedSuperCallWithLoadIC(expr);
3205 VisitForStackValue(property->obj());
3206 if (is_named_call) {
3207 EmitCallWithLoadIC(expr);
3209 EmitKeyedCallWithLoadIC(expr, property->key());
3212 } else if (call_type == Call::SUPER_CALL) {
3213 EmitSuperConstructorCall(expr);
3215 DCHECK(call_type == Call::OTHER_CALL);
3216 // Call to an arbitrary expression not handled specially above.
3217 VisitForStackValue(callee);
3218 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
3220 // Emit function call.
3225 // RecordJSReturnSite should have been called.
3226 DCHECK(expr->return_is_recorded_);
3231 void FullCodeGenerator::VisitCallNew(CallNew* expr) {
3232 Comment cmnt(masm_, "[ CallNew");
3233 // According to ECMA-262, section 11.2.2, page 44, the function
3234 // expression in new calls must be evaluated before the
3237 // Push constructor on the stack. If it's not a function it's used as
3238 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
3240 DCHECK(!expr->expression()->IsSuperPropertyReference());
3241 VisitForStackValue(expr->expression());
3243 // Push the arguments ("left-to-right") on the stack.
3244 ZoneList<Expression*>* args = expr->arguments();
3245 int arg_count = args->length();
3246 for (int i = 0; i < arg_count; i++) {
3247 VisitForStackValue(args->at(i));
3250 // Call the construct call builtin that handles allocation and
3251 // constructor invocation.
3252 SetConstructCallPosition(expr);
3254 // Load function and argument count into a1 and a0.
3255 __ li(a0, Operand(arg_count));
3256 __ ld(a1, MemOperand(sp, arg_count * kPointerSize));
3258 // Record call targets in unoptimized code.
3259 if (FLAG_pretenuring_call_new) {
3260 EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3261 DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3262 expr->CallNewFeedbackSlot().ToInt() + 1);
3265 __ li(a2, FeedbackVector());
3266 __ li(a3, Operand(SmiFromSlot(expr->CallNewFeedbackSlot())));
3268 CallConstructStub stub(isolate(), RECORD_CONSTRUCTOR_TARGET);
3269 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3270 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
3271 context()->Plug(v0);
3275 void FullCodeGenerator::EmitSuperConstructorCall(Call* expr) {
3276 SuperCallReference* super_call_ref =
3277 expr->expression()->AsSuperCallReference();
3278 DCHECK_NOT_NULL(super_call_ref);
3280 EmitLoadSuperConstructor(super_call_ref);
3281 __ push(result_register());
3283 // Push the arguments ("left-to-right") on the stack.
3284 ZoneList<Expression*>* args = expr->arguments();
3285 int arg_count = args->length();
3286 for (int i = 0; i < arg_count; i++) {
3287 VisitForStackValue(args->at(i));
3290 // Call the construct call builtin that handles allocation and
3291 // constructor invocation.
3292 SetConstructCallPosition(expr);
3294 // Load original constructor into a4.
3295 VisitForAccumulatorValue(super_call_ref->new_target_var());
3296 __ mov(a4, result_register());
3298 // Load function and argument count into a1 and a0.
3299 __ li(a0, Operand(arg_count));
3300 __ ld(a1, MemOperand(sp, arg_count * kPointerSize));
3302 // Record call targets in unoptimized code.
3303 if (FLAG_pretenuring_call_new) {
3305 /* TODO(dslomov): support pretenuring.
3306 EnsureSlotContainsAllocationSite(expr->AllocationSiteFeedbackSlot());
3307 DCHECK(expr->AllocationSiteFeedbackSlot().ToInt() ==
3308 expr->CallNewFeedbackSlot().ToInt() + 1);
3312 __ li(a2, FeedbackVector());
3313 __ li(a3, Operand(SmiFromSlot(expr->CallFeedbackSlot())));
3315 CallConstructStub stub(isolate(), SUPER_CALL_RECORD_TARGET);
3316 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
3318 RecordJSReturnSite(expr);
3320 context()->Plug(v0);
3324 void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
3325 ZoneList<Expression*>* args = expr->arguments();
3326 DCHECK(args->length() == 1);
3328 VisitForAccumulatorValue(args->at(0));
3330 Label materialize_true, materialize_false;
3331 Label* if_true = NULL;
3332 Label* if_false = NULL;
3333 Label* fall_through = NULL;
3334 context()->PrepareTest(&materialize_true, &materialize_false,
3335 &if_true, &if_false, &fall_through);
3337 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3339 Split(eq, a4, Operand(zero_reg), if_true, if_false, fall_through);
3341 context()->Plug(if_true, if_false);
3345 void FullCodeGenerator::EmitIsNonNegativeSmi(CallRuntime* expr) {
3346 ZoneList<Expression*>* args = expr->arguments();
3347 DCHECK(args->length() == 1);
3349 VisitForAccumulatorValue(args->at(0));
3351 Label materialize_true, materialize_false;
3352 Label* if_true = NULL;
3353 Label* if_false = NULL;
3354 Label* fall_through = NULL;
3355 context()->PrepareTest(&materialize_true, &materialize_false,
3356 &if_true, &if_false, &fall_through);
3358 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3359 __ NonNegativeSmiTst(v0, at);
3360 Split(eq, at, Operand(zero_reg), if_true, if_false, fall_through);
3362 context()->Plug(if_true, if_false);
3366 void FullCodeGenerator::EmitIsSpecObject(CallRuntime* expr) {
3367 ZoneList<Expression*>* args = expr->arguments();
3368 DCHECK(args->length() == 1);
3370 VisitForAccumulatorValue(args->at(0));
3372 Label materialize_true, materialize_false;
3373 Label* if_true = NULL;
3374 Label* if_false = NULL;
3375 Label* fall_through = NULL;
3376 context()->PrepareTest(&materialize_true, &materialize_false,
3377 &if_true, &if_false, &fall_through);
3379 __ JumpIfSmi(v0, if_false);
3380 __ GetObjectType(v0, a1, a1);
3381 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3382 Split(ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE),
3383 if_true, if_false, fall_through);
3385 context()->Plug(if_true, if_false);
3389 void FullCodeGenerator::EmitIsSimdValue(CallRuntime* expr) {
3390 ZoneList<Expression*>* args = expr->arguments();
3391 DCHECK(args->length() == 1);
3393 VisitForAccumulatorValue(args->at(0));
3395 Label materialize_true, materialize_false;
3396 Label* if_true = NULL;
3397 Label* if_false = NULL;
3398 Label* fall_through = NULL;
3399 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3400 &if_false, &fall_through);
3402 __ JumpIfSmi(v0, if_false);
3403 __ GetObjectType(v0, a1, a1);
3404 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3405 Split(eq, a1, Operand(SIMD128_VALUE_TYPE), if_true, if_false, fall_through);
3407 context()->Plug(if_true, if_false);
3411 void FullCodeGenerator::EmitIsStringWrapperSafeForDefaultValueOf(
3412 CallRuntime* expr) {
3413 ZoneList<Expression*>* args = expr->arguments();
3414 DCHECK(args->length() == 1);
3416 VisitForAccumulatorValue(args->at(0));
3418 Label materialize_true, materialize_false, skip_lookup;
3419 Label* if_true = NULL;
3420 Label* if_false = NULL;
3421 Label* fall_through = NULL;
3422 context()->PrepareTest(&materialize_true, &materialize_false,
3423 &if_true, &if_false, &fall_through);
3425 __ AssertNotSmi(v0);
3427 __ ld(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
3428 __ lbu(a4, FieldMemOperand(a1, Map::kBitField2Offset));
3429 __ And(a4, a4, 1 << Map::kStringWrapperSafeForDefaultValueOf);
3430 __ Branch(&skip_lookup, ne, a4, Operand(zero_reg));
3432 // Check for fast case object. Generate false result for slow case object.
3433 __ ld(a2, FieldMemOperand(v0, JSObject::kPropertiesOffset));
3434 __ ld(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
3435 __ LoadRoot(a4, Heap::kHashTableMapRootIndex);
3436 __ Branch(if_false, eq, a2, Operand(a4));
3438 // Look for valueOf name in the descriptor array, and indicate false if
3439 // found. Since we omit an enumeration index check, if it is added via a
3440 // transition that shares its descriptor array, this is a false positive.
3441 Label entry, loop, done;
3443 // Skip loop if no descriptors are valid.
3444 __ NumberOfOwnDescriptors(a3, a1);
3445 __ Branch(&done, eq, a3, Operand(zero_reg));
3447 __ LoadInstanceDescriptors(a1, a4);
3448 // a4: descriptor array.
3449 // a3: valid entries in the descriptor array.
3450 STATIC_ASSERT(kSmiTag == 0);
3451 STATIC_ASSERT(kSmiTagSize == 1);
3453 // STATIC_ASSERT(kPointerSize == 4);
3454 __ li(at, Operand(DescriptorArray::kDescriptorSize));
3455 __ Dmul(a3, a3, at);
3456 // Calculate location of the first key name.
3457 __ Daddu(a4, a4, Operand(DescriptorArray::kFirstOffset - kHeapObjectTag));
3458 // Calculate the end of the descriptor array.
3460 __ dsll(a5, a3, kPointerSizeLog2);
3461 __ Daddu(a2, a2, a5);
3463 // Loop through all the keys in the descriptor array. If one of these is the
3464 // string "valueOf" the result is false.
3465 // The use of a6 to store the valueOf string assumes that it is not otherwise
3466 // used in the loop below.
3467 __ li(a6, Operand(isolate()->factory()->value_of_string()));
3470 __ ld(a3, MemOperand(a4, 0));
3471 __ Branch(if_false, eq, a3, Operand(a6));
3472 __ Daddu(a4, a4, Operand(DescriptorArray::kDescriptorSize * kPointerSize));
3474 __ Branch(&loop, ne, a4, Operand(a2));
3478 // Set the bit in the map to indicate that there is no local valueOf field.
3479 __ lbu(a2, FieldMemOperand(a1, Map::kBitField2Offset));
3480 __ Or(a2, a2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
3481 __ sb(a2, FieldMemOperand(a1, Map::kBitField2Offset));
3483 __ bind(&skip_lookup);
3485 // If a valueOf property is not found on the object check that its
3486 // prototype is the un-modified String prototype. If not result is false.
3487 __ ld(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
3488 __ JumpIfSmi(a2, if_false);
3489 __ ld(a2, FieldMemOperand(a2, HeapObject::kMapOffset));
3490 __ ld(a3, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
3491 __ ld(a3, FieldMemOperand(a3, GlobalObject::kNativeContextOffset));
3492 __ ld(a3, ContextOperand(a3, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
3493 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3494 Split(eq, a2, Operand(a3), if_true, if_false, fall_through);
3496 context()->Plug(if_true, if_false);
3500 void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
3501 ZoneList<Expression*>* args = expr->arguments();
3502 DCHECK(args->length() == 1);
3504 VisitForAccumulatorValue(args->at(0));
3506 Label materialize_true, materialize_false;
3507 Label* if_true = NULL;
3508 Label* if_false = NULL;
3509 Label* fall_through = NULL;
3510 context()->PrepareTest(&materialize_true, &materialize_false,
3511 &if_true, &if_false, &fall_through);
3513 __ JumpIfSmi(v0, if_false);
3514 __ GetObjectType(v0, a1, a2);
3515 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3516 __ Branch(if_true, eq, a2, Operand(JS_FUNCTION_TYPE));
3517 __ Branch(if_false);
3519 context()->Plug(if_true, if_false);
3523 void FullCodeGenerator::EmitIsMinusZero(CallRuntime* expr) {
3524 ZoneList<Expression*>* args = expr->arguments();
3525 DCHECK(args->length() == 1);
3527 VisitForAccumulatorValue(args->at(0));
3529 Label materialize_true, materialize_false;
3530 Label* if_true = NULL;
3531 Label* if_false = NULL;
3532 Label* fall_through = NULL;
3533 context()->PrepareTest(&materialize_true, &materialize_false,
3534 &if_true, &if_false, &fall_through);
3536 __ CheckMap(v0, a1, Heap::kHeapNumberMapRootIndex, if_false, DO_SMI_CHECK);
3537 __ lwu(a2, FieldMemOperand(v0, HeapNumber::kExponentOffset));
3538 __ lwu(a1, FieldMemOperand(v0, HeapNumber::kMantissaOffset));
3539 __ li(a4, 0x80000000);
3541 __ Branch(¬_nan, ne, a2, Operand(a4));
3542 __ mov(a4, zero_reg);
3546 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3547 Split(eq, a2, Operand(a4), if_true, if_false, fall_through);
3549 context()->Plug(if_true, if_false);
3553 void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
3554 ZoneList<Expression*>* args = expr->arguments();
3555 DCHECK(args->length() == 1);
3557 VisitForAccumulatorValue(args->at(0));
3559 Label materialize_true, materialize_false;
3560 Label* if_true = NULL;
3561 Label* if_false = NULL;
3562 Label* fall_through = NULL;
3563 context()->PrepareTest(&materialize_true, &materialize_false,
3564 &if_true, &if_false, &fall_through);
3566 __ JumpIfSmi(v0, if_false);
3567 __ GetObjectType(v0, a1, a1);
3568 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3569 Split(eq, a1, Operand(JS_ARRAY_TYPE),
3570 if_true, if_false, fall_through);
3572 context()->Plug(if_true, if_false);
3576 void FullCodeGenerator::EmitIsTypedArray(CallRuntime* expr) {
3577 ZoneList<Expression*>* args = expr->arguments();
3578 DCHECK(args->length() == 1);
3580 VisitForAccumulatorValue(args->at(0));
3582 Label materialize_true, materialize_false;
3583 Label* if_true = NULL;
3584 Label* if_false = NULL;
3585 Label* fall_through = NULL;
3586 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3587 &if_false, &fall_through);
3589 __ JumpIfSmi(v0, if_false);
3590 __ GetObjectType(v0, a1, a1);
3591 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3592 Split(eq, a1, Operand(JS_TYPED_ARRAY_TYPE), if_true, if_false, fall_through);
3594 context()->Plug(if_true, if_false);
3598 void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
3599 ZoneList<Expression*>* args = expr->arguments();
3600 DCHECK(args->length() == 1);
3602 VisitForAccumulatorValue(args->at(0));
3604 Label materialize_true, materialize_false;
3605 Label* if_true = NULL;
3606 Label* if_false = NULL;
3607 Label* fall_through = NULL;
3608 context()->PrepareTest(&materialize_true, &materialize_false,
3609 &if_true, &if_false, &fall_through);
3611 __ JumpIfSmi(v0, if_false);
3612 __ GetObjectType(v0, a1, a1);
3613 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3614 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
3616 context()->Plug(if_true, if_false);
3620 void FullCodeGenerator::EmitIsJSProxy(CallRuntime* expr) {
3621 ZoneList<Expression*>* args = expr->arguments();
3622 DCHECK(args->length() == 1);
3624 VisitForAccumulatorValue(args->at(0));
3626 Label materialize_true, materialize_false;
3627 Label* if_true = NULL;
3628 Label* if_false = NULL;
3629 Label* fall_through = NULL;
3630 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3631 &if_false, &fall_through);
3633 __ JumpIfSmi(v0, if_false);
3635 Register type_reg = a2;
3636 __ GetObjectType(v0, map, type_reg);
3637 __ Subu(type_reg, type_reg, Operand(FIRST_JS_PROXY_TYPE));
3638 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3639 Split(ls, type_reg, Operand(LAST_JS_PROXY_TYPE - FIRST_JS_PROXY_TYPE),
3640 if_true, if_false, fall_through);
3642 context()->Plug(if_true, if_false);
3646 void FullCodeGenerator::EmitIsConstructCall(CallRuntime* expr) {
3647 DCHECK(expr->arguments()->length() == 0);
3649 Label materialize_true, materialize_false;
3650 Label* if_true = NULL;
3651 Label* if_false = NULL;
3652 Label* fall_through = NULL;
3653 context()->PrepareTest(&materialize_true, &materialize_false,
3654 &if_true, &if_false, &fall_through);
3656 // Get the frame pointer for the calling frame.
3657 __ ld(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3659 // Skip the arguments adaptor frame if it exists.
3660 Label check_frame_marker;
3661 __ ld(a1, MemOperand(a2, StandardFrameConstants::kContextOffset));
3662 __ Branch(&check_frame_marker, ne,
3663 a1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3664 __ ld(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
3666 // Check the marker in the calling frame.
3667 __ bind(&check_frame_marker);
3668 __ ld(a1, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
3669 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3670 Split(eq, a1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)),
3671 if_true, if_false, fall_through);
3673 context()->Plug(if_true, if_false);
3677 void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
3678 ZoneList<Expression*>* args = expr->arguments();
3679 DCHECK(args->length() == 2);
3681 // Load the two objects into registers and perform the comparison.
3682 VisitForStackValue(args->at(0));
3683 VisitForAccumulatorValue(args->at(1));
3685 Label materialize_true, materialize_false;
3686 Label* if_true = NULL;
3687 Label* if_false = NULL;
3688 Label* fall_through = NULL;
3689 context()->PrepareTest(&materialize_true, &materialize_false,
3690 &if_true, &if_false, &fall_through);
3693 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3694 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
3696 context()->Plug(if_true, if_false);
3700 void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
3701 ZoneList<Expression*>* args = expr->arguments();
3702 DCHECK(args->length() == 1);
3704 // ArgumentsAccessStub expects the key in a1 and the formal
3705 // parameter count in a0.
3706 VisitForAccumulatorValue(args->at(0));
3708 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
3709 ArgumentsAccessStub stub(isolate(), ArgumentsAccessStub::READ_ELEMENT);
3711 context()->Plug(v0);
3715 void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
3716 DCHECK(expr->arguments()->length() == 0);
3718 // Get the number of formal parameters.
3719 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
3721 // Check if the calling frame is an arguments adaptor frame.
3722 __ ld(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3723 __ ld(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
3724 __ Branch(&exit, ne, a3,
3725 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3727 // Arguments adaptor case: Read the arguments length from the
3729 __ ld(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3732 context()->Plug(v0);
3736 void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
3737 ZoneList<Expression*>* args = expr->arguments();
3738 DCHECK(args->length() == 1);
3739 Label done, null, function, non_function_constructor;
3741 VisitForAccumulatorValue(args->at(0));
3743 // If the object is a smi, we return null.
3744 __ JumpIfSmi(v0, &null);
3746 // Check that the object is a JS object but take special care of JS
3747 // functions to make sure they have 'Function' as their class.
3748 // Assume that there are only two callable types, and one of them is at
3749 // either end of the type range for JS object types. Saves extra comparisons.
3750 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
3751 __ GetObjectType(v0, v0, a1); // Map is now in v0.
3752 __ Branch(&null, lt, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
3754 STATIC_ASSERT(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3755 FIRST_SPEC_OBJECT_TYPE + 1);
3756 __ Branch(&function, eq, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
3758 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE ==
3759 LAST_SPEC_OBJECT_TYPE - 1);
3760 __ Branch(&function, eq, a1, Operand(LAST_SPEC_OBJECT_TYPE));
3761 // Assume that there is no larger type.
3762 STATIC_ASSERT(LAST_NONCALLABLE_SPEC_OBJECT_TYPE == LAST_TYPE - 1);
3764 // Check if the constructor in the map is a JS function.
3765 Register instance_type = a2;
3766 __ GetMapConstructor(v0, v0, a1, instance_type);
3767 __ Branch(&non_function_constructor, ne, instance_type,
3768 Operand(JS_FUNCTION_TYPE));
3770 // v0 now contains the constructor function. Grab the
3771 // instance class name from there.
3772 __ ld(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
3773 __ ld(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
3776 // Functions have class 'Function'.
3778 __ LoadRoot(v0, Heap::kFunction_stringRootIndex);
3781 // Objects with a non-function constructor have class 'Object'.
3782 __ bind(&non_function_constructor);
3783 __ LoadRoot(v0, Heap::kObject_stringRootIndex);
3786 // Non-JS objects have class null.
3788 __ LoadRoot(v0, Heap::kNullValueRootIndex);
3793 context()->Plug(v0);
3797 void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3798 ZoneList<Expression*>* args = expr->arguments();
3799 DCHECK(args->length() == 1);
3801 VisitForAccumulatorValue(args->at(0)); // Load the object.
3804 // If the object is a smi return the object.
3805 __ JumpIfSmi(v0, &done);
3806 // If the object is not a value type, return the object.
3807 __ GetObjectType(v0, a1, a1);
3808 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
3810 __ ld(v0, FieldMemOperand(v0, JSValue::kValueOffset));
3813 context()->Plug(v0);
3817 void FullCodeGenerator::EmitIsDate(CallRuntime* expr) {
3818 ZoneList<Expression*>* args = expr->arguments();
3819 DCHECK_EQ(1, args->length());
3821 VisitForAccumulatorValue(args->at(0));
3823 Label materialize_true, materialize_false;
3824 Label* if_true = nullptr;
3825 Label* if_false = nullptr;
3826 Label* fall_through = nullptr;
3827 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3828 &if_false, &fall_through);
3830 __ JumpIfSmi(v0, if_false);
3831 __ GetObjectType(v0, a1, a1);
3832 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3833 Split(eq, a1, Operand(JS_DATE_TYPE), if_true, if_false, fall_through);
3835 context()->Plug(if_true, if_false);
3839 void FullCodeGenerator::EmitDateField(CallRuntime* expr) {
3840 ZoneList<Expression*>* args = expr->arguments();
3841 DCHECK(args->length() == 2);
3842 DCHECK_NOT_NULL(args->at(1)->AsLiteral());
3843 Smi* index = Smi::cast(*(args->at(1)->AsLiteral()->value()));
3845 VisitForAccumulatorValue(args->at(0)); // Load the object.
3847 Register object = v0;
3848 Register result = v0;
3849 Register scratch0 = t1;
3850 Register scratch1 = a1;
3852 if (index->value() == 0) {
3853 __ ld(result, FieldMemOperand(object, JSDate::kValueOffset));
3855 Label runtime, done;
3856 if (index->value() < JSDate::kFirstUncachedField) {
3857 ExternalReference stamp = ExternalReference::date_cache_stamp(isolate());
3858 __ li(scratch1, Operand(stamp));
3859 __ ld(scratch1, MemOperand(scratch1));
3860 __ ld(scratch0, FieldMemOperand(object, JSDate::kCacheStampOffset));
3861 __ Branch(&runtime, ne, scratch1, Operand(scratch0));
3862 __ ld(result, FieldMemOperand(object, JSDate::kValueOffset +
3863 kPointerSize * index->value()));
3867 __ PrepareCallCFunction(2, scratch1);
3868 __ li(a1, Operand(index));
3869 __ Move(a0, object);
3870 __ CallCFunction(ExternalReference::get_date_field_function(isolate()), 2);
3874 context()->Plug(result);
3878 void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
3879 ZoneList<Expression*>* args = expr->arguments();
3880 DCHECK_EQ(3, args->length());
3882 Register string = v0;
3883 Register index = a1;
3884 Register value = a2;
3886 VisitForStackValue(args->at(0)); // index
3887 VisitForStackValue(args->at(1)); // value
3888 VisitForAccumulatorValue(args->at(2)); // string
3889 __ Pop(index, value);
3891 if (FLAG_debug_code) {
3892 __ SmiTst(value, at);
3893 __ Check(eq, kNonSmiValue, at, Operand(zero_reg));
3894 __ SmiTst(index, at);
3895 __ Check(eq, kNonSmiIndex, at, Operand(zero_reg));
3896 __ SmiUntag(index, index);
3897 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
3898 Register scratch = t1;
3899 __ EmitSeqStringSetCharCheck(
3900 string, index, value, scratch, one_byte_seq_type);
3901 __ SmiTag(index, index);
3904 __ SmiUntag(value, value);
3907 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
3909 __ Daddu(at, at, index);
3910 __ sb(value, MemOperand(at));
3911 context()->Plug(string);
3915 void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
3916 ZoneList<Expression*>* args = expr->arguments();
3917 DCHECK_EQ(3, args->length());
3919 Register string = v0;
3920 Register index = a1;
3921 Register value = a2;
3923 VisitForStackValue(args->at(0)); // index
3924 VisitForStackValue(args->at(1)); // value
3925 VisitForAccumulatorValue(args->at(2)); // string
3926 __ Pop(index, value);
3928 if (FLAG_debug_code) {
3929 __ SmiTst(value, at);
3930 __ Check(eq, kNonSmiValue, at, Operand(zero_reg));
3931 __ SmiTst(index, at);
3932 __ Check(eq, kNonSmiIndex, at, Operand(zero_reg));
3933 __ SmiUntag(index, index);
3934 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
3935 Register scratch = t1;
3936 __ EmitSeqStringSetCharCheck(
3937 string, index, value, scratch, two_byte_seq_type);
3938 __ SmiTag(index, index);
3941 __ SmiUntag(value, value);
3944 Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3945 __ dsra(index, index, 32 - 1);
3946 __ Daddu(at, at, index);
3947 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
3948 __ sh(value, MemOperand(at));
3949 context()->Plug(string);
3953 void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3954 ZoneList<Expression*>* args = expr->arguments();
3955 DCHECK(args->length() == 2);
3957 VisitForStackValue(args->at(0)); // Load the object.
3958 VisitForAccumulatorValue(args->at(1)); // Load the value.
3959 __ pop(a1); // v0 = value. a1 = object.
3962 // If the object is a smi, return the value.
3963 __ JumpIfSmi(a1, &done);
3965 // If the object is not a value type, return the value.
3966 __ GetObjectType(a1, a2, a2);
3967 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
3970 __ sd(v0, FieldMemOperand(a1, JSValue::kValueOffset));
3971 // Update the write barrier. Save the value as it will be
3972 // overwritten by the write barrier code and is needed afterward.
3974 __ RecordWriteField(
3975 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
3978 context()->Plug(v0);
3982 void FullCodeGenerator::EmitNumberToString(CallRuntime* expr) {
3983 ZoneList<Expression*>* args = expr->arguments();
3984 DCHECK_EQ(args->length(), 1);
3986 // Load the argument into a0 and call the stub.
3987 VisitForAccumulatorValue(args->at(0));
3988 __ mov(a0, result_register());
3990 NumberToStringStub stub(isolate());
3992 context()->Plug(v0);
3996 void FullCodeGenerator::EmitToObject(CallRuntime* expr) {
3997 ZoneList<Expression*>* args = expr->arguments();
3998 DCHECK_EQ(1, args->length());
4000 // Load the argument into a0 and convert it.
4001 VisitForAccumulatorValue(args->at(0));
4002 __ mov(a0, result_register());
4004 ToObjectStub stub(isolate());
4006 context()->Plug(v0);
4010 void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
4011 ZoneList<Expression*>* args = expr->arguments();
4012 DCHECK(args->length() == 1);
4014 VisitForAccumulatorValue(args->at(0));
4017 StringCharFromCodeGenerator generator(v0, a1);
4018 generator.GenerateFast(masm_);
4021 NopRuntimeCallHelper call_helper;
4022 generator.GenerateSlow(masm_, call_helper);
4025 context()->Plug(a1);
4029 void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
4030 ZoneList<Expression*>* args = expr->arguments();
4031 DCHECK(args->length() == 2);
4033 VisitForStackValue(args->at(0));
4034 VisitForAccumulatorValue(args->at(1));
4035 __ mov(a0, result_register());
4037 Register object = a1;
4038 Register index = a0;
4039 Register result = v0;
4043 Label need_conversion;
4044 Label index_out_of_range;
4046 StringCharCodeAtGenerator generator(object,
4051 &index_out_of_range,
4052 STRING_INDEX_IS_NUMBER);
4053 generator.GenerateFast(masm_);
4056 __ bind(&index_out_of_range);
4057 // When the index is out of range, the spec requires us to return
4059 __ LoadRoot(result, Heap::kNanValueRootIndex);
4062 __ bind(&need_conversion);
4063 // Load the undefined value into the result register, which will
4064 // trigger conversion.
4065 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
4068 NopRuntimeCallHelper call_helper;
4069 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
4072 context()->Plug(result);
4076 void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
4077 ZoneList<Expression*>* args = expr->arguments();
4078 DCHECK(args->length() == 2);
4080 VisitForStackValue(args->at(0));
4081 VisitForAccumulatorValue(args->at(1));
4082 __ mov(a0, result_register());
4084 Register object = a1;
4085 Register index = a0;
4086 Register scratch = a3;
4087 Register result = v0;
4091 Label need_conversion;
4092 Label index_out_of_range;
4094 StringCharAtGenerator generator(object,
4100 &index_out_of_range,
4101 STRING_INDEX_IS_NUMBER);
4102 generator.GenerateFast(masm_);
4105 __ bind(&index_out_of_range);
4106 // When the index is out of range, the spec requires us to return
4107 // the empty string.
4108 __ LoadRoot(result, Heap::kempty_stringRootIndex);
4111 __ bind(&need_conversion);
4112 // Move smi zero into the result register, which will trigger
4114 __ li(result, Operand(Smi::FromInt(0)));
4117 NopRuntimeCallHelper call_helper;
4118 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
4121 context()->Plug(result);
4125 void FullCodeGenerator::EmitStringAdd(CallRuntime* expr) {
4126 ZoneList<Expression*>* args = expr->arguments();
4127 DCHECK_EQ(2, args->length());
4128 VisitForStackValue(args->at(0));
4129 VisitForAccumulatorValue(args->at(1));
4132 __ mov(a0, result_register()); // StringAddStub requires args in a0, a1.
4133 StringAddStub stub(isolate(), STRING_ADD_CHECK_BOTH, NOT_TENURED);
4135 context()->Plug(v0);
4139 void FullCodeGenerator::EmitCallFunction(CallRuntime* expr) {
4140 ZoneList<Expression*>* args = expr->arguments();
4141 DCHECK(args->length() >= 2);
4143 int arg_count = args->length() - 2; // 2 ~ receiver and function.
4144 for (int i = 0; i < arg_count + 1; i++) {
4145 VisitForStackValue(args->at(i));
4147 VisitForAccumulatorValue(args->last()); // Function.
4149 Label runtime, done;
4150 // Check for non-function argument (including proxy).
4151 __ JumpIfSmi(v0, &runtime);
4152 __ GetObjectType(v0, a1, a1);
4153 __ Branch(&runtime, ne, a1, Operand(JS_FUNCTION_TYPE));
4155 // InvokeFunction requires the function in a1. Move it in there.
4156 __ mov(a1, result_register());
4157 ParameterCount count(arg_count);
4158 __ InvokeFunction(a1, count, CALL_FUNCTION, NullCallWrapper());
4159 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4164 __ CallRuntime(Runtime::kCall, args->length());
4167 context()->Plug(v0);
4171 void FullCodeGenerator::EmitDefaultConstructorCallSuper(CallRuntime* expr) {
4172 ZoneList<Expression*>* args = expr->arguments();
4173 DCHECK(args->length() == 2);
4176 VisitForStackValue(args->at(0));
4179 VisitForStackValue(args->at(1));
4180 __ CallRuntime(Runtime::kGetPrototype, 1);
4181 __ Push(result_register());
4183 // Load original constructor into a4.
4184 __ ld(a4, MemOperand(sp, 1 * kPointerSize));
4186 // Check if the calling frame is an arguments adaptor frame.
4187 Label adaptor_frame, args_set_up, runtime;
4188 __ ld(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4189 __ ld(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
4190 __ Branch(&adaptor_frame, eq, a3,
4191 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4192 // default constructor has no arguments, so no adaptor frame means no args.
4193 __ mov(a0, zero_reg);
4194 __ Branch(&args_set_up);
4196 // Copy arguments from adaptor frame.
4198 __ bind(&adaptor_frame);
4199 __ ld(a1, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4200 __ SmiUntag(a1, a1);
4204 // Get arguments pointer in a2.
4205 __ dsll(at, a1, kPointerSizeLog2);
4206 __ Daddu(a2, a2, Operand(at));
4207 __ Daddu(a2, a2, Operand(StandardFrameConstants::kCallerSPOffset));
4210 // Pre-decrement a2 with kPointerSize on each iteration.
4211 // Pre-decrement in order to skip receiver.
4212 __ Daddu(a2, a2, Operand(-kPointerSize));
4213 __ ld(a3, MemOperand(a2));
4215 __ Daddu(a1, a1, Operand(-1));
4216 __ Branch(&loop, ne, a1, Operand(zero_reg));
4219 __ bind(&args_set_up);
4220 __ dsll(at, a0, kPointerSizeLog2);
4221 __ Daddu(at, at, Operand(sp));
4222 __ ld(a1, MemOperand(at, 0));
4223 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
4225 CallConstructStub stub(isolate(), SUPER_CONSTRUCTOR_CALL);
4226 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
4230 context()->Plug(result_register());
4234 void FullCodeGenerator::EmitRegExpConstructResult(CallRuntime* expr) {
4235 RegExpConstructResultStub stub(isolate());
4236 ZoneList<Expression*>* args = expr->arguments();
4237 DCHECK(args->length() == 3);
4238 VisitForStackValue(args->at(0));
4239 VisitForStackValue(args->at(1));
4240 VisitForAccumulatorValue(args->at(2));
4241 __ mov(a0, result_register());
4245 context()->Plug(v0);
4249 void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
4250 ZoneList<Expression*>* args = expr->arguments();
4251 VisitForAccumulatorValue(args->at(0));
4253 Label materialize_true, materialize_false;
4254 Label* if_true = NULL;
4255 Label* if_false = NULL;
4256 Label* fall_through = NULL;
4257 context()->PrepareTest(&materialize_true, &materialize_false,
4258 &if_true, &if_false, &fall_through);
4260 __ lwu(a0, FieldMemOperand(v0, String::kHashFieldOffset));
4261 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
4263 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4264 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
4266 context()->Plug(if_true, if_false);
4270 void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
4271 ZoneList<Expression*>* args = expr->arguments();
4272 DCHECK(args->length() == 1);
4273 VisitForAccumulatorValue(args->at(0));
4275 __ AssertString(v0);
4277 __ lwu(v0, FieldMemOperand(v0, String::kHashFieldOffset));
4278 __ IndexFromHash(v0, v0);
4280 context()->Plug(v0);
4284 void FullCodeGenerator::EmitFastOneByteArrayJoin(CallRuntime* expr) {
4285 Label bailout, done, one_char_separator, long_separator,
4286 non_trivial_array, not_size_one_array, loop,
4287 empty_separator_loop, one_char_separator_loop,
4288 one_char_separator_loop_entry, long_separator_loop;
4289 ZoneList<Expression*>* args = expr->arguments();
4290 DCHECK(args->length() == 2);
4291 VisitForStackValue(args->at(1));
4292 VisitForAccumulatorValue(args->at(0));
4294 // All aliases of the same register have disjoint lifetimes.
4295 Register array = v0;
4296 Register elements = no_reg; // Will be v0.
4297 Register result = no_reg; // Will be v0.
4298 Register separator = a1;
4299 Register array_length = a2;
4300 Register result_pos = no_reg; // Will be a2.
4301 Register string_length = a3;
4302 Register string = a4;
4303 Register element = a5;
4304 Register elements_end = a6;
4305 Register scratch1 = a7;
4306 Register scratch2 = t1;
4307 Register scratch3 = t0;
4309 // Separator operand is on the stack.
4312 // Check that the array is a JSArray.
4313 __ JumpIfSmi(array, &bailout);
4314 __ GetObjectType(array, scratch1, scratch2);
4315 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
4317 // Check that the array has fast elements.
4318 __ CheckFastElements(scratch1, scratch2, &bailout);
4320 // If the array has length zero, return the empty string.
4321 __ ld(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
4322 __ SmiUntag(array_length);
4323 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
4324 __ LoadRoot(v0, Heap::kempty_stringRootIndex);
4327 __ bind(&non_trivial_array);
4329 // Get the FixedArray containing array's elements.
4331 __ ld(elements, FieldMemOperand(array, JSArray::kElementsOffset));
4332 array = no_reg; // End of array's live range.
4334 // Check that all array elements are sequential one-byte strings, and
4335 // accumulate the sum of their lengths, as a smi-encoded value.
4336 __ mov(string_length, zero_reg);
4338 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4339 __ dsll(elements_end, array_length, kPointerSizeLog2);
4340 __ Daddu(elements_end, element, elements_end);
4341 // Loop condition: while (element < elements_end).
4342 // Live values in registers:
4343 // elements: Fixed array of strings.
4344 // array_length: Length of the fixed array of strings (not smi)
4345 // separator: Separator string
4346 // string_length: Accumulated sum of string lengths (smi).
4347 // element: Current array element.
4348 // elements_end: Array end.
4349 if (generate_debug_code_) {
4350 __ Assert(gt, kNoEmptyArraysHereInEmitFastOneByteArrayJoin, array_length,
4354 __ ld(string, MemOperand(element));
4355 __ Daddu(element, element, kPointerSize);
4356 __ JumpIfSmi(string, &bailout);
4357 __ ld(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
4358 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
4359 __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
4360 __ ld(scratch1, FieldMemOperand(string, SeqOneByteString::kLengthOffset));
4361 __ DadduAndCheckForOverflow(string_length, string_length, scratch1, scratch3);
4362 __ BranchOnOverflow(&bailout, scratch3);
4363 __ Branch(&loop, lt, element, Operand(elements_end));
4365 // If array_length is 1, return elements[0], a string.
4366 __ Branch(¬_size_one_array, ne, array_length, Operand(1));
4367 __ ld(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
4370 __ bind(¬_size_one_array);
4372 // Live values in registers:
4373 // separator: Separator string
4374 // array_length: Length of the array.
4375 // string_length: Sum of string lengths (smi).
4376 // elements: FixedArray of strings.
4378 // Check that the separator is a flat one-byte string.
4379 __ JumpIfSmi(separator, &bailout);
4380 __ ld(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
4381 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
4382 __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
4384 // Add (separator length times array_length) - separator length to the
4385 // string_length to get the length of the result string. array_length is not
4386 // smi but the other values are, so the result is a smi.
4387 __ ld(scratch1, FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
4388 __ Dsubu(string_length, string_length, Operand(scratch1));
4389 __ SmiUntag(scratch1);
4390 __ Dmul(scratch2, array_length, scratch1);
4391 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
4393 __ dsra32(scratch1, scratch2, 0);
4394 __ Branch(&bailout, ne, scratch2, Operand(zero_reg));
4395 __ SmiUntag(string_length);
4396 __ AdduAndCheckForOverflow(string_length, string_length, scratch2, scratch3);
4397 __ BranchOnOverflow(&bailout, scratch3);
4399 // Get first element in the array to free up the elements register to be used
4402 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4403 result = elements; // End of live range for elements.
4405 // Live values in registers:
4406 // element: First array element
4407 // separator: Separator string
4408 // string_length: Length of result string (not smi)
4409 // array_length: Length of the array.
4410 __ AllocateOneByteString(result, string_length, scratch1, scratch2,
4411 elements_end, &bailout);
4412 // Prepare for looping. Set up elements_end to end of the array. Set
4413 // result_pos to the position of the result where to write the first
4415 __ dsll(elements_end, array_length, kPointerSizeLog2);
4416 __ Daddu(elements_end, element, elements_end);
4417 result_pos = array_length; // End of live range for array_length.
4418 array_length = no_reg;
4419 __ Daddu(result_pos,
4421 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4423 // Check the length of the separator.
4424 __ ld(scratch1, FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
4425 __ li(at, Operand(Smi::FromInt(1)));
4426 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
4427 __ Branch(&long_separator, gt, scratch1, Operand(at));
4429 // Empty separator case.
4430 __ bind(&empty_separator_loop);
4431 // Live values in registers:
4432 // result_pos: the position to which we are currently copying characters.
4433 // element: Current array element.
4434 // elements_end: Array end.
4436 // Copy next array element to the result.
4437 __ ld(string, MemOperand(element));
4438 __ Daddu(element, element, kPointerSize);
4439 __ ld(string_length, FieldMemOperand(string, String::kLengthOffset));
4440 __ SmiUntag(string_length);
4441 __ Daddu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4442 __ CopyBytes(string, result_pos, string_length, scratch1);
4443 // End while (element < elements_end).
4444 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
4445 DCHECK(result.is(v0));
4448 // One-character separator case.
4449 __ bind(&one_char_separator);
4450 // Replace separator with its one-byte character value.
4451 __ lbu(separator, FieldMemOperand(separator, SeqOneByteString::kHeaderSize));
4452 // Jump into the loop after the code that copies the separator, so the first
4453 // element is not preceded by a separator.
4454 __ jmp(&one_char_separator_loop_entry);
4456 __ bind(&one_char_separator_loop);
4457 // Live values in registers:
4458 // result_pos: the position to which we are currently copying characters.
4459 // element: Current array element.
4460 // elements_end: Array end.
4461 // separator: Single separator one-byte char (in lower byte).
4463 // Copy the separator character to the result.
4464 __ sb(separator, MemOperand(result_pos));
4465 __ Daddu(result_pos, result_pos, 1);
4467 // Copy next array element to the result.
4468 __ bind(&one_char_separator_loop_entry);
4469 __ ld(string, MemOperand(element));
4470 __ Daddu(element, element, kPointerSize);
4471 __ ld(string_length, FieldMemOperand(string, String::kLengthOffset));
4472 __ SmiUntag(string_length);
4473 __ Daddu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4474 __ CopyBytes(string, result_pos, string_length, scratch1);
4475 // End while (element < elements_end).
4476 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
4477 DCHECK(result.is(v0));
4480 // Long separator case (separator is more than one character). Entry is at the
4481 // label long_separator below.
4482 __ bind(&long_separator_loop);
4483 // Live values in registers:
4484 // result_pos: the position to which we are currently copying characters.
4485 // element: Current array element.
4486 // elements_end: Array end.
4487 // separator: Separator string.
4489 // Copy the separator to the result.
4490 __ ld(string_length, FieldMemOperand(separator, String::kLengthOffset));
4491 __ SmiUntag(string_length);
4494 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
4495 __ CopyBytes(string, result_pos, string_length, scratch1);
4497 __ bind(&long_separator);
4498 __ ld(string, MemOperand(element));
4499 __ Daddu(element, element, kPointerSize);
4500 __ ld(string_length, FieldMemOperand(string, String::kLengthOffset));
4501 __ SmiUntag(string_length);
4502 __ Daddu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4503 __ CopyBytes(string, result_pos, string_length, scratch1);
4504 // End while (element < elements_end).
4505 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
4506 DCHECK(result.is(v0));
4510 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
4512 context()->Plug(v0);
4516 void FullCodeGenerator::EmitDebugIsActive(CallRuntime* expr) {
4517 DCHECK(expr->arguments()->length() == 0);
4518 ExternalReference debug_is_active =
4519 ExternalReference::debug_is_active_address(isolate());
4520 __ li(at, Operand(debug_is_active));
4521 __ lbu(v0, MemOperand(at));
4523 context()->Plug(v0);
4527 void FullCodeGenerator::EmitLoadJSRuntimeFunction(CallRuntime* expr) {
4528 // Push undefined as the receiver.
4529 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
4532 __ ld(v0, GlobalObjectOperand());
4533 __ ld(v0, FieldMemOperand(v0, GlobalObject::kNativeContextOffset));
4534 __ ld(v0, ContextOperand(v0, expr->context_index()));
4538 void FullCodeGenerator::EmitCallJSRuntimeFunction(CallRuntime* expr) {
4539 ZoneList<Expression*>* args = expr->arguments();
4540 int arg_count = args->length();
4542 SetCallPosition(expr, arg_count);
4543 CallFunctionStub stub(isolate(), arg_count, NO_CALL_FUNCTION_FLAGS);
4544 __ ld(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
4549 void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
4550 ZoneList<Expression*>* args = expr->arguments();
4551 int arg_count = args->length();
4553 if (expr->is_jsruntime()) {
4554 Comment cmnt(masm_, "[ CallRuntime");
4555 EmitLoadJSRuntimeFunction(expr);
4557 // Push the target function under the receiver.
4558 __ ld(at, MemOperand(sp, 0));
4560 __ sd(v0, MemOperand(sp, kPointerSize));
4562 // Push the arguments ("left-to-right").
4563 for (int i = 0; i < arg_count; i++) {
4564 VisitForStackValue(args->at(i));
4567 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4568 EmitCallJSRuntimeFunction(expr);
4570 // Restore context register.
4571 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4573 context()->DropAndPlug(1, v0);
4575 const Runtime::Function* function = expr->function();
4576 switch (function->function_id) {
4577 #define CALL_INTRINSIC_GENERATOR(Name) \
4578 case Runtime::kInline##Name: { \
4579 Comment cmnt(masm_, "[ Inline" #Name); \
4580 return Emit##Name(expr); \
4582 FOR_EACH_FULL_CODE_INTRINSIC(CALL_INTRINSIC_GENERATOR)
4583 #undef CALL_INTRINSIC_GENERATOR
4585 Comment cmnt(masm_, "[ CallRuntime for unhandled intrinsic");
4586 // Push the arguments ("left-to-right").
4587 for (int i = 0; i < arg_count; i++) {
4588 VisitForStackValue(args->at(i));
4591 // Call the C runtime function.
4592 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4593 __ CallRuntime(expr->function(), arg_count);
4594 context()->Plug(v0);
4601 void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
4602 switch (expr->op()) {
4603 case Token::DELETE: {
4604 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
4605 Property* property = expr->expression()->AsProperty();
4606 VariableProxy* proxy = expr->expression()->AsVariableProxy();
4608 if (property != NULL) {
4609 VisitForStackValue(property->obj());
4610 VisitForStackValue(property->key());
4611 __ CallRuntime(is_strict(language_mode())
4612 ? Runtime::kDeleteProperty_Strict
4613 : Runtime::kDeleteProperty_Sloppy,
4615 context()->Plug(v0);
4616 } else if (proxy != NULL) {
4617 Variable* var = proxy->var();
4618 // Delete of an unqualified identifier is disallowed in strict mode but
4619 // "delete this" is allowed.
4620 bool is_this = var->HasThisName(isolate());
4621 DCHECK(is_sloppy(language_mode()) || is_this);
4622 if (var->IsUnallocatedOrGlobalSlot()) {
4623 __ ld(a2, GlobalObjectOperand());
4624 __ li(a1, Operand(var->name()));
4626 __ CallRuntime(Runtime::kDeleteProperty_Sloppy, 2);
4627 context()->Plug(v0);
4628 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
4629 // Result of deleting non-global, non-dynamic variables is false.
4630 // The subexpression does not have side effects.
4631 context()->Plug(is_this);
4633 // Non-global variable. Call the runtime to try to delete from the
4634 // context where the variable was introduced.
4635 DCHECK(!context_register().is(a2));
4636 __ li(a2, Operand(var->name()));
4637 __ Push(context_register(), a2);
4638 __ CallRuntime(Runtime::kDeleteLookupSlot, 2);
4639 context()->Plug(v0);
4642 // Result of deleting non-property, non-variable reference is true.
4643 // The subexpression may have side effects.
4644 VisitForEffect(expr->expression());
4645 context()->Plug(true);
4651 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
4652 VisitForEffect(expr->expression());
4653 context()->Plug(Heap::kUndefinedValueRootIndex);
4658 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
4659 if (context()->IsEffect()) {
4660 // Unary NOT has no side effects so it's only necessary to visit the
4661 // subexpression. Match the optimizing compiler by not branching.
4662 VisitForEffect(expr->expression());
4663 } else if (context()->IsTest()) {
4664 const TestContext* test = TestContext::cast(context());
4665 // The labels are swapped for the recursive call.
4666 VisitForControl(expr->expression(),
4667 test->false_label(),
4669 test->fall_through());
4670 context()->Plug(test->true_label(), test->false_label());
4672 // We handle value contexts explicitly rather than simply visiting
4673 // for control and plugging the control flow into the context,
4674 // because we need to prepare a pair of extra administrative AST ids
4675 // for the optimizing compiler.
4676 DCHECK(context()->IsAccumulatorValue() || context()->IsStackValue());
4677 Label materialize_true, materialize_false, done;
4678 VisitForControl(expr->expression(),
4682 __ bind(&materialize_true);
4683 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
4684 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
4685 if (context()->IsStackValue()) __ push(v0);
4687 __ bind(&materialize_false);
4688 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
4689 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
4690 if (context()->IsStackValue()) __ push(v0);
4696 case Token::TYPEOF: {
4697 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
4699 AccumulatorValueContext context(this);
4700 VisitForTypeofValue(expr->expression());
4703 TypeofStub typeof_stub(isolate());
4704 __ CallStub(&typeof_stub);
4705 context()->Plug(v0);
4715 void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
4716 DCHECK(expr->expression()->IsValidReferenceExpressionOrThis());
4718 Comment cmnt(masm_, "[ CountOperation");
4720 Property* prop = expr->expression()->AsProperty();
4721 LhsKind assign_type = Property::GetAssignType(prop);
4723 // Evaluate expression and get value.
4724 if (assign_type == VARIABLE) {
4725 DCHECK(expr->expression()->AsVariableProxy()->var() != NULL);
4726 AccumulatorValueContext context(this);
4727 EmitVariableLoad(expr->expression()->AsVariableProxy());
4729 // Reserve space for result of postfix operation.
4730 if (expr->is_postfix() && !context()->IsEffect()) {
4731 __ li(at, Operand(Smi::FromInt(0)));
4734 switch (assign_type) {
4735 case NAMED_PROPERTY: {
4736 // Put the object both on the stack and in the register.
4737 VisitForStackValue(prop->obj());
4738 __ ld(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
4739 EmitNamedPropertyLoad(prop);
4743 case NAMED_SUPER_PROPERTY: {
4744 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4745 VisitForAccumulatorValue(
4746 prop->obj()->AsSuperPropertyReference()->home_object());
4747 __ Push(result_register());
4748 const Register scratch = a1;
4749 __ ld(scratch, MemOperand(sp, kPointerSize));
4750 __ Push(scratch, result_register());
4751 EmitNamedSuperPropertyLoad(prop);
4755 case KEYED_SUPER_PROPERTY: {
4756 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4757 VisitForAccumulatorValue(
4758 prop->obj()->AsSuperPropertyReference()->home_object());
4759 const Register scratch = a1;
4760 const Register scratch1 = a4;
4761 __ Move(scratch, result_register());
4762 VisitForAccumulatorValue(prop->key());
4763 __ Push(scratch, result_register());
4764 __ ld(scratch1, MemOperand(sp, 2 * kPointerSize));
4765 __ Push(scratch1, scratch, result_register());
4766 EmitKeyedSuperPropertyLoad(prop);
4770 case KEYED_PROPERTY: {
4771 VisitForStackValue(prop->obj());
4772 VisitForStackValue(prop->key());
4773 __ ld(LoadDescriptor::ReceiverRegister(),
4774 MemOperand(sp, 1 * kPointerSize));
4775 __ ld(LoadDescriptor::NameRegister(), MemOperand(sp, 0));
4776 EmitKeyedPropertyLoad(prop);
4785 // We need a second deoptimization point after loading the value
4786 // in case evaluating the property load my have a side effect.
4787 if (assign_type == VARIABLE) {
4788 PrepareForBailout(expr->expression(), TOS_REG);
4790 PrepareForBailoutForId(prop->LoadId(), TOS_REG);
4793 // Inline smi case if we are in a loop.
4794 Label stub_call, done;
4795 JumpPatchSite patch_site(masm_);
4797 int count_value = expr->op() == Token::INC ? 1 : -1;
4799 if (ShouldInlineSmiCase(expr->op())) {
4801 patch_site.EmitJumpIfNotSmi(v0, &slow);
4803 // Save result for postfix expressions.
4804 if (expr->is_postfix()) {
4805 if (!context()->IsEffect()) {
4806 // Save the result on the stack. If we have a named or keyed property
4807 // we store the result under the receiver that is currently on top
4809 switch (assign_type) {
4813 case NAMED_PROPERTY:
4814 __ sd(v0, MemOperand(sp, kPointerSize));
4816 case NAMED_SUPER_PROPERTY:
4817 __ sd(v0, MemOperand(sp, 2 * kPointerSize));
4819 case KEYED_PROPERTY:
4820 __ sd(v0, MemOperand(sp, 2 * kPointerSize));
4822 case KEYED_SUPER_PROPERTY:
4823 __ sd(v0, MemOperand(sp, 3 * kPointerSize));
4829 Register scratch1 = a1;
4830 Register scratch2 = a4;
4831 __ li(scratch1, Operand(Smi::FromInt(count_value)));
4832 __ DadduAndCheckForOverflow(v0, v0, scratch1, scratch2);
4833 __ BranchOnNoOverflow(&done, scratch2);
4834 // Call stub. Undo operation first.
4839 if (!is_strong(language_mode())) {
4840 ToNumberStub convert_stub(isolate());
4841 __ CallStub(&convert_stub);
4842 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
4845 // Save result for postfix expressions.
4846 if (expr->is_postfix()) {
4847 if (!context()->IsEffect()) {
4848 // Save the result on the stack. If we have a named or keyed property
4849 // we store the result under the receiver that is currently on top
4851 switch (assign_type) {
4855 case NAMED_PROPERTY:
4856 __ sd(v0, MemOperand(sp, kPointerSize));
4858 case NAMED_SUPER_PROPERTY:
4859 __ sd(v0, MemOperand(sp, 2 * kPointerSize));
4861 case KEYED_PROPERTY:
4862 __ sd(v0, MemOperand(sp, 2 * kPointerSize));
4864 case KEYED_SUPER_PROPERTY:
4865 __ sd(v0, MemOperand(sp, 3 * kPointerSize));
4871 __ bind(&stub_call);
4873 __ li(a0, Operand(Smi::FromInt(count_value)));
4875 SetExpressionPosition(expr);
4878 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), Token::ADD,
4879 strength(language_mode())).code();
4880 CallIC(code, expr->CountBinOpFeedbackId());
4881 patch_site.EmitPatchInfo();
4884 if (is_strong(language_mode())) {
4885 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
4887 // Store the value returned in v0.
4888 switch (assign_type) {
4890 if (expr->is_postfix()) {
4891 { EffectContext context(this);
4892 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4893 Token::ASSIGN, expr->CountSlot());
4894 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4897 // For all contexts except EffectConstant we have the result on
4898 // top of the stack.
4899 if (!context()->IsEffect()) {
4900 context()->PlugTOS();
4903 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4904 Token::ASSIGN, expr->CountSlot());
4905 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4906 context()->Plug(v0);
4909 case NAMED_PROPERTY: {
4910 __ mov(StoreDescriptor::ValueRegister(), result_register());
4911 __ li(StoreDescriptor::NameRegister(),
4912 Operand(prop->key()->AsLiteral()->value()));
4913 __ pop(StoreDescriptor::ReceiverRegister());
4914 if (FLAG_vector_stores) {
4915 EmitLoadStoreICSlot(expr->CountSlot());
4918 CallStoreIC(expr->CountStoreFeedbackId());
4920 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4921 if (expr->is_postfix()) {
4922 if (!context()->IsEffect()) {
4923 context()->PlugTOS();
4926 context()->Plug(v0);
4930 case NAMED_SUPER_PROPERTY: {
4931 EmitNamedSuperPropertyStore(prop);
4932 if (expr->is_postfix()) {
4933 if (!context()->IsEffect()) {
4934 context()->PlugTOS();
4937 context()->Plug(v0);
4941 case KEYED_SUPER_PROPERTY: {
4942 EmitKeyedSuperPropertyStore(prop);
4943 if (expr->is_postfix()) {
4944 if (!context()->IsEffect()) {
4945 context()->PlugTOS();
4948 context()->Plug(v0);
4952 case KEYED_PROPERTY: {
4953 __ mov(StoreDescriptor::ValueRegister(), result_register());
4954 __ Pop(StoreDescriptor::ReceiverRegister(),
4955 StoreDescriptor::NameRegister());
4957 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
4958 if (FLAG_vector_stores) {
4959 EmitLoadStoreICSlot(expr->CountSlot());
4962 CallIC(ic, expr->CountStoreFeedbackId());
4964 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4965 if (expr->is_postfix()) {
4966 if (!context()->IsEffect()) {
4967 context()->PlugTOS();
4970 context()->Plug(v0);
4978 void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
4979 Expression* sub_expr,
4980 Handle<String> check) {
4981 Label materialize_true, materialize_false;
4982 Label* if_true = NULL;
4983 Label* if_false = NULL;
4984 Label* fall_through = NULL;
4985 context()->PrepareTest(&materialize_true, &materialize_false,
4986 &if_true, &if_false, &fall_through);
4988 { AccumulatorValueContext context(this);
4989 VisitForTypeofValue(sub_expr);
4991 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4993 Factory* factory = isolate()->factory();
4994 if (String::Equals(check, factory->number_string())) {
4995 __ JumpIfSmi(v0, if_true);
4996 __ ld(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4997 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4998 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4999 } else if (String::Equals(check, factory->string_string())) {
5000 __ JumpIfSmi(v0, if_false);
5001 __ GetObjectType(v0, v0, a1);
5002 Split(lt, a1, Operand(FIRST_NONSTRING_TYPE), if_true, if_false,
5004 } else if (String::Equals(check, factory->symbol_string())) {
5005 __ JumpIfSmi(v0, if_false);
5006 __ GetObjectType(v0, v0, a1);
5007 Split(eq, a1, Operand(SYMBOL_TYPE), if_true, if_false, fall_through);
5008 } else if (String::Equals(check, factory->boolean_string())) {
5009 __ LoadRoot(at, Heap::kTrueValueRootIndex);
5010 __ Branch(if_true, eq, v0, Operand(at));
5011 __ LoadRoot(at, Heap::kFalseValueRootIndex);
5012 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
5013 } else if (String::Equals(check, factory->undefined_string())) {
5014 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
5015 __ Branch(if_true, eq, v0, Operand(at));
5016 __ JumpIfSmi(v0, if_false);
5017 // Check for undetectable objects => true.
5018 __ ld(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
5019 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
5020 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
5021 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
5022 } else if (String::Equals(check, factory->function_string())) {
5023 __ JumpIfSmi(v0, if_false);
5024 STATIC_ASSERT(NUM_OF_CALLABLE_SPEC_OBJECT_TYPES == 2);
5025 __ GetObjectType(v0, v0, a1);
5026 __ Branch(if_true, eq, a1, Operand(JS_FUNCTION_TYPE));
5027 Split(eq, a1, Operand(JS_FUNCTION_PROXY_TYPE),
5028 if_true, if_false, fall_through);
5029 } else if (String::Equals(check, factory->object_string())) {
5030 __ JumpIfSmi(v0, if_false);
5031 __ LoadRoot(at, Heap::kNullValueRootIndex);
5032 __ Branch(if_true, eq, v0, Operand(at));
5033 // Check for JS objects => true.
5034 __ GetObjectType(v0, v0, a1);
5035 __ Branch(if_false, lt, a1, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
5036 __ lbu(a1, FieldMemOperand(v0, Map::kInstanceTypeOffset));
5037 __ Branch(if_false, gt, a1, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
5038 // Check for undetectable objects => false.
5039 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
5040 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
5041 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
5043 #define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
5044 } else if (String::Equals(check, factory->type##_string())) { \
5045 __ JumpIfSmi(v0, if_false); \
5046 __ ld(v0, FieldMemOperand(v0, HeapObject::kMapOffset)); \
5047 __ LoadRoot(at, Heap::k##Type##MapRootIndex); \
5048 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
5049 SIMD128_TYPES(SIMD128_TYPE)
5053 if (if_false != fall_through) __ jmp(if_false);
5055 context()->Plug(if_true, if_false);
5059 void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
5060 Comment cmnt(masm_, "[ CompareOperation");
5061 SetExpressionPosition(expr);
5063 // First we try a fast inlined version of the compare when one of
5064 // the operands is a literal.
5065 if (TryLiteralCompare(expr)) return;
5067 // Always perform the comparison for its control flow. Pack the result
5068 // into the expression's context after the comparison is performed.
5069 Label materialize_true, materialize_false;
5070 Label* if_true = NULL;
5071 Label* if_false = NULL;
5072 Label* fall_through = NULL;
5073 context()->PrepareTest(&materialize_true, &materialize_false,
5074 &if_true, &if_false, &fall_through);
5076 Token::Value op = expr->op();
5077 VisitForStackValue(expr->left());
5080 VisitForStackValue(expr->right());
5081 __ InvokeBuiltin(Context::IN_BUILTIN_INDEX, CALL_FUNCTION);
5082 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
5083 __ LoadRoot(a4, Heap::kTrueValueRootIndex);
5084 Split(eq, v0, Operand(a4), if_true, if_false, fall_through);
5087 case Token::INSTANCEOF: {
5088 VisitForAccumulatorValue(expr->right());
5089 __ mov(a0, result_register());
5091 InstanceOfStub stub(isolate());
5093 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
5094 __ LoadRoot(a4, Heap::kTrueValueRootIndex);
5095 Split(eq, v0, Operand(a4), if_true, if_false, fall_through);
5100 VisitForAccumulatorValue(expr->right());
5101 Condition cc = CompareIC::ComputeCondition(op);
5102 __ mov(a0, result_register());
5105 bool inline_smi_code = ShouldInlineSmiCase(op);
5106 JumpPatchSite patch_site(masm_);
5107 if (inline_smi_code) {
5109 __ Or(a2, a0, Operand(a1));
5110 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
5111 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
5112 __ bind(&slow_case);
5115 Handle<Code> ic = CodeFactory::CompareIC(
5116 isolate(), op, strength(language_mode())).code();
5117 CallIC(ic, expr->CompareOperationFeedbackId());
5118 patch_site.EmitPatchInfo();
5119 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5120 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
5124 // Convert the result of the comparison into one expected for this
5125 // expression's context.
5126 context()->Plug(if_true, if_false);
5130 void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
5131 Expression* sub_expr,
5133 Label materialize_true, materialize_false;
5134 Label* if_true = NULL;
5135 Label* if_false = NULL;
5136 Label* fall_through = NULL;
5137 context()->PrepareTest(&materialize_true, &materialize_false,
5138 &if_true, &if_false, &fall_through);
5140 VisitForAccumulatorValue(sub_expr);
5141 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
5142 __ mov(a0, result_register());
5143 if (expr->op() == Token::EQ_STRICT) {
5144 Heap::RootListIndex nil_value = nil == kNullValue ?
5145 Heap::kNullValueRootIndex :
5146 Heap::kUndefinedValueRootIndex;
5147 __ LoadRoot(a1, nil_value);
5148 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
5150 Handle<Code> ic = CompareNilICStub::GetUninitialized(isolate(), nil);
5151 CallIC(ic, expr->CompareOperationFeedbackId());
5152 Split(ne, v0, Operand(zero_reg), if_true, if_false, fall_through);
5154 context()->Plug(if_true, if_false);
5158 void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
5159 __ ld(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5160 context()->Plug(v0);
5164 Register FullCodeGenerator::result_register() {
5169 Register FullCodeGenerator::context_register() {
5174 void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
5175 // DCHECK_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
5176 DCHECK(IsAligned(frame_offset, kPointerSize));
5177 // __ sw(value, MemOperand(fp, frame_offset));
5178 __ sd(value, MemOperand(fp, frame_offset));
5182 void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
5183 __ ld(dst, ContextOperand(cp, context_index));
5187 void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
5188 Scope* closure_scope = scope()->ClosureScope();
5189 if (closure_scope->is_script_scope() ||
5190 closure_scope->is_module_scope()) {
5191 // Contexts nested in the native context have a canonical empty function
5192 // as their closure, not the anonymous closure containing the global
5193 // code. Pass a smi sentinel and let the runtime look up the empty
5195 __ li(at, Operand(Smi::FromInt(0)));
5196 } else if (closure_scope->is_eval_scope()) {
5197 // Contexts created by a call to eval have the same closure as the
5198 // context calling eval, not the anonymous closure containing the eval
5199 // code. Fetch it from the context.
5200 __ ld(at, ContextOperand(cp, Context::CLOSURE_INDEX));
5202 DCHECK(closure_scope->is_function_scope());
5203 __ ld(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
5209 // ----------------------------------------------------------------------------
5210 // Non-local control flow support.
5212 void FullCodeGenerator::EnterFinallyBlock() {
5213 DCHECK(!result_register().is(a1));
5214 // Store result register while executing finally block.
5215 __ push(result_register());
5216 // Cook return address in link register to stack (smi encoded Code* delta).
5217 __ Dsubu(a1, ra, Operand(masm_->CodeObject()));
5220 // Store result register while executing finally block.
5223 // Store pending message while executing finally block.
5224 ExternalReference pending_message_obj =
5225 ExternalReference::address_of_pending_message_obj(isolate());
5226 __ li(at, Operand(pending_message_obj));
5227 __ ld(a1, MemOperand(at));
5230 ClearPendingMessage();
5234 void FullCodeGenerator::ExitFinallyBlock() {
5235 DCHECK(!result_register().is(a1));
5236 // Restore pending message from stack.
5238 ExternalReference pending_message_obj =
5239 ExternalReference::address_of_pending_message_obj(isolate());
5240 __ li(at, Operand(pending_message_obj));
5241 __ sd(a1, MemOperand(at));
5243 // Restore result register from stack.
5246 // Uncook return address and return.
5247 __ pop(result_register());
5250 __ Daddu(at, a1, Operand(masm_->CodeObject()));
5255 void FullCodeGenerator::ClearPendingMessage() {
5256 DCHECK(!result_register().is(a1));
5257 ExternalReference pending_message_obj =
5258 ExternalReference::address_of_pending_message_obj(isolate());
5259 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
5260 __ li(at, Operand(pending_message_obj));
5261 __ sd(a1, MemOperand(at));
5265 void FullCodeGenerator::EmitLoadStoreICSlot(FeedbackVectorICSlot slot) {
5266 DCHECK(FLAG_vector_stores && !slot.IsInvalid());
5267 __ li(VectorStoreICTrampolineDescriptor::SlotRegister(),
5268 Operand(SmiFromSlot(slot)));
5275 void BackEdgeTable::PatchAt(Code* unoptimized_code,
5277 BackEdgeState target_state,
5278 Code* replacement_code) {
5279 static const int kInstrSize = Assembler::kInstrSize;
5280 Address branch_address = pc - 8 * kInstrSize;
5281 CodePatcher patcher(branch_address, 1);
5283 switch (target_state) {
5285 // slt at, a3, zero_reg (in case of count based interrupts)
5286 // beq at, zero_reg, ok
5287 // lui t9, <interrupt stub address> upper
5288 // ori t9, <interrupt stub address> u-middle
5290 // ori t9, <interrupt stub address> lower
5293 // ok-label ----- pc_after points here
5294 patcher.masm()->slt(at, a3, zero_reg);
5296 case ON_STACK_REPLACEMENT:
5297 case OSR_AFTER_STACK_CHECK:
5298 // addiu at, zero_reg, 1
5299 // beq at, zero_reg, ok ;; Not changed
5300 // lui t9, <on-stack replacement address> upper
5301 // ori t9, <on-stack replacement address> middle
5303 // ori t9, <on-stack replacement address> lower
5304 // jalr t9 ;; Not changed
5305 // nop ;; Not changed
5306 // ok-label ----- pc_after points here
5307 patcher.masm()->daddiu(at, zero_reg, 1);
5310 Address pc_immediate_load_address = pc - 6 * kInstrSize;
5311 // Replace the stack check address in the load-immediate (6-instr sequence)
5312 // with the entry address of the replacement code.
5313 Assembler::set_target_address_at(pc_immediate_load_address,
5314 replacement_code->entry());
5316 unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
5317 unoptimized_code, pc_immediate_load_address, replacement_code);
5321 BackEdgeTable::BackEdgeState BackEdgeTable::GetBackEdgeState(
5323 Code* unoptimized_code,
5325 static const int kInstrSize = Assembler::kInstrSize;
5326 Address branch_address = pc - 8 * kInstrSize;
5327 Address pc_immediate_load_address = pc - 6 * kInstrSize;
5329 DCHECK(Assembler::IsBeq(Assembler::instr_at(pc - 7 * kInstrSize)));
5330 if (!Assembler::IsAddImmediate(Assembler::instr_at(branch_address))) {
5331 DCHECK(reinterpret_cast<uint64_t>(
5332 Assembler::target_address_at(pc_immediate_load_address)) ==
5333 reinterpret_cast<uint64_t>(
5334 isolate->builtins()->InterruptCheck()->entry()));
5338 DCHECK(Assembler::IsAddImmediate(Assembler::instr_at(branch_address)));
5340 if (reinterpret_cast<uint64_t>(
5341 Assembler::target_address_at(pc_immediate_load_address)) ==
5342 reinterpret_cast<uint64_t>(
5343 isolate->builtins()->OnStackReplacement()->entry())) {
5344 return ON_STACK_REPLACEMENT;
5347 DCHECK(reinterpret_cast<uint64_t>(
5348 Assembler::target_address_at(pc_immediate_load_address)) ==
5349 reinterpret_cast<uint64_t>(
5350 isolate->builtins()->OsrAfterStackCheck()->entry()));
5351 return OSR_AFTER_STACK_CHECK;
5355 } // namespace internal
5358 #endif // V8_TARGET_ARCH_MIPS64