blob: 07e9fdfc947e390237878c5bc430fbfabbf96a94 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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.
4
5#if V8_TARGET_ARCH_MIPS
6
7// Note on Mips implementation:
8//
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.
14
15#include "src/ast/scopes.h"
16#include "src/code-factory.h"
17#include "src/code-stubs.h"
18#include "src/codegen.h"
19#include "src/debug/debug.h"
20#include "src/full-codegen/full-codegen.h"
21#include "src/ic/ic.h"
22#include "src/parsing/parser.h"
23
24#include "src/mips/code-stubs-mips.h"
25#include "src/mips/macro-assembler-mips.h"
26
27namespace v8 {
28namespace internal {
29
30#define __ ACCESS_MASM(masm_)
31
32
33// A patch site is a location in the code which it is possible to patch. This
34// class has a number of methods to emit the code which is patchable and the
35// method EmitPatchInfo to record a marker back to the patchable code. This
36// marker is a andi zero_reg, rx, #yyyy instruction, and rx * 0x0000ffff + yyyy
37// (raw 16 bit immediate value is used) is the delta from the pc to the first
38// instruction of the patchable code.
39// The marker instruction is effectively a NOP (dest is zero_reg) and will
40// never be emitted by normal code.
41class JumpPatchSite BASE_EMBEDDED {
42 public:
43 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
44#ifdef DEBUG
45 info_emitted_ = false;
46#endif
47 }
48
49 ~JumpPatchSite() {
50 DCHECK(patch_site_.is_bound() == info_emitted_);
51 }
52
53 // When initially emitting this ensure that a jump is always generated to skip
54 // the inlined smi code.
55 void EmitJumpIfNotSmi(Register reg, Label* target) {
56 DCHECK(!patch_site_.is_bound() && !info_emitted_);
57 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
58 __ bind(&patch_site_);
59 __ andi(at, reg, 0);
60 // Always taken before patched.
61 __ BranchShort(target, eq, at, Operand(zero_reg));
62 }
63
64 // When initially emitting this ensure that a jump is never generated to skip
65 // the inlined smi code.
66 void EmitJumpIfSmi(Register reg, Label* target) {
67 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
68 DCHECK(!patch_site_.is_bound() && !info_emitted_);
69 __ bind(&patch_site_);
70 __ andi(at, reg, 0);
71 // Never taken before patched.
72 __ BranchShort(target, ne, at, Operand(zero_reg));
73 }
74
75 void EmitPatchInfo() {
76 if (patch_site_.is_bound()) {
77 int delta_to_patch_site = masm_->InstructionsGeneratedSince(&patch_site_);
78 Register reg = Register::from_code(delta_to_patch_site / kImm16Mask);
79 __ andi(zero_reg, reg, delta_to_patch_site % kImm16Mask);
80#ifdef DEBUG
81 info_emitted_ = true;
82#endif
83 } else {
84 __ nop(); // Signals no inlined code.
85 }
86 }
87
88 private:
89 MacroAssembler* masm_;
90 Label patch_site_;
91#ifdef DEBUG
92 bool info_emitted_;
93#endif
94};
95
96
97// Generate code for a JS function. On entry to the function the receiver
98// and arguments have been pushed on the stack left to right. The actual
99// argument count matches the formal parameter count expected by the
100// function.
101//
102// The live registers are:
103// o a1: the JS function object being called (i.e. ourselves)
104// o a3: the new target value
105// o cp: our context
106// o fp: our caller's frame pointer
107// o sp: stack pointer
108// o ra: return address
109//
110// The function builds a JS frame. Please see JavaScriptFrameConstants in
111// frames-mips.h for its layout.
112void 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");
118
119 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
120
121#ifdef DEBUG
122 if (strlen(FLAG_stop_at) > 0 &&
123 info->literal()->name()->IsUtf8EqualTo(CStrVector(FLAG_stop_at))) {
124 __ stop("stop-at");
125 }
126#endif
127
128 if (FLAG_debug_code && info->ExpectsJSReceiverAsReceiver()) {
129 int receiver_offset = info->scope()->num_parameters() * kPointerSize;
130 __ lw(a2, MemOperand(sp, receiver_offset));
131 __ AssertNotSmi(a2);
132 __ GetObjectType(a2, a2, a2);
133 __ Check(ge, kSloppyFunctionExpectsJSReceiverReceiver, a2,
134 Operand(FIRST_JS_RECEIVER_TYPE));
135 }
136
137 // Open a frame scope to indicate that there is a frame on the stack. The
138 // MANUAL indicates that the scope shouldn't actually generate code to set up
139 // the frame (that is done below).
140 FrameScope frame_scope(masm_, StackFrame::MANUAL);
141
142 info->set_prologue_offset(masm_->pc_offset());
143 __ Prologue(info->GeneratePreagedPrologue());
144
145 { Comment cmnt(masm_, "[ Allocate locals");
146 int locals_count = info->scope()->num_stack_slots();
147 // Generators allocate locals, if any, in context slots.
148 DCHECK(!IsGeneratorFunction(info->literal()->kind()) || locals_count == 0);
149 if (locals_count > 0) {
150 if (locals_count >= 128) {
151 Label ok;
152 __ Subu(t5, sp, Operand(locals_count * kPointerSize));
153 __ LoadRoot(a2, Heap::kRealStackLimitRootIndex);
154 __ Branch(&ok, hs, t5, Operand(a2));
155 __ CallRuntime(Runtime::kThrowStackOverflow);
156 __ bind(&ok);
157 }
158 __ LoadRoot(t5, Heap::kUndefinedValueRootIndex);
159 int kMaxPushes = FLAG_optimize_for_size ? 4 : 32;
160 if (locals_count >= kMaxPushes) {
161 int loop_iterations = locals_count / kMaxPushes;
162 __ li(a2, Operand(loop_iterations));
163 Label loop_header;
164 __ bind(&loop_header);
165 // Do pushes.
166 __ Subu(sp, sp, Operand(kMaxPushes * kPointerSize));
167 for (int i = 0; i < kMaxPushes; i++) {
168 __ sw(t5, MemOperand(sp, i * kPointerSize));
169 }
170 // Continue loop if not done.
171 __ Subu(a2, a2, Operand(1));
172 __ Branch(&loop_header, ne, a2, Operand(zero_reg));
173 }
174 int remaining = locals_count % kMaxPushes;
175 // Emit the remaining pushes.
176 __ Subu(sp, sp, Operand(remaining * kPointerSize));
177 for (int i = 0; i < remaining; i++) {
178 __ sw(t5, MemOperand(sp, i * kPointerSize));
179 }
180 }
181 }
182
183 bool function_in_register_a1 = true;
184
185 // Possibly allocate a local context.
186 if (info->scope()->num_heap_slots() > 0) {
187 Comment cmnt(masm_, "[ Allocate context");
188 // Argument to NewContext is the function, which is still in a1.
189 bool need_write_barrier = true;
190 int slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
191 if (info->scope()->is_script_scope()) {
192 __ push(a1);
193 __ Push(info->scope()->GetScopeInfo(info->isolate()));
194 __ CallRuntime(Runtime::kNewScriptContext);
195 PrepareForBailoutForId(BailoutId::ScriptContext(), TOS_REG);
196 // The new target value is not used, clobbering is safe.
197 DCHECK_NULL(info->scope()->new_target_var());
198 } else {
199 if (info->scope()->new_target_var() != nullptr) {
200 __ push(a3); // Preserve new target.
201 }
202 if (slots <= FastNewContextStub::kMaximumSlots) {
203 FastNewContextStub stub(isolate(), slots);
204 __ CallStub(&stub);
205 // Result of FastNewContextStub is always in new space.
206 need_write_barrier = false;
207 } else {
208 __ push(a1);
209 __ CallRuntime(Runtime::kNewFunctionContext);
210 }
211 if (info->scope()->new_target_var() != nullptr) {
212 __ pop(a3); // Restore new target.
213 }
214 }
215 function_in_register_a1 = false;
216 // Context is returned in v0. It replaces the context passed to us.
217 // It's saved in the stack and kept live in cp.
218 __ mov(cp, v0);
219 __ sw(v0, MemOperand(fp, StandardFrameConstants::kContextOffset));
220 // Copy any necessary parameters into the context.
221 int num_parameters = info->scope()->num_parameters();
222 int first_parameter = info->scope()->has_this_declaration() ? -1 : 0;
223 for (int i = first_parameter; i < num_parameters; i++) {
224 Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
225 if (var->IsContextSlot()) {
226 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
227 (num_parameters - 1 - i) * kPointerSize;
228 // Load parameter from stack.
229 __ lw(a0, MemOperand(fp, parameter_offset));
230 // Store it in the context.
231 MemOperand target = ContextMemOperand(cp, var->index());
232 __ sw(a0, target);
233
234 // Update the write barrier.
235 if (need_write_barrier) {
236 __ RecordWriteContextSlot(cp, target.offset(), a0, a2,
237 kRAHasBeenSaved, kDontSaveFPRegs);
238 } else if (FLAG_debug_code) {
239 Label done;
240 __ JumpIfInNewSpace(cp, a0, &done);
241 __ Abort(kExpectedNewSpaceObject);
242 __ bind(&done);
243 }
244 }
245 }
246 }
247
248 // Register holding this function and new target are both trashed in case we
249 // bailout here. But since that can happen only when new target is not used
250 // and we allocate a context, the value of |function_in_register| is correct.
251 PrepareForBailoutForId(BailoutId::FunctionContext(), NO_REGISTERS);
252
253 // Possibly set up a local binding to the this function which is used in
254 // derived constructors with super calls.
255 Variable* this_function_var = scope()->this_function_var();
256 if (this_function_var != nullptr) {
257 Comment cmnt(masm_, "[ This function");
258 if (!function_in_register_a1) {
259 __ lw(a1, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
260 // The write barrier clobbers register again, keep it marked as such.
261 }
262 SetVar(this_function_var, a1, a0, a2);
263 }
264
265 // Possibly set up a local binding to the new target value.
266 Variable* new_target_var = scope()->new_target_var();
267 if (new_target_var != nullptr) {
268 Comment cmnt(masm_, "[ new.target");
269 SetVar(new_target_var, a3, a0, a2);
270 }
271
272 // Possibly allocate RestParameters
273 int rest_index;
274 Variable* rest_param = scope()->rest_parameter(&rest_index);
275 if (rest_param) {
276 Comment cmnt(masm_, "[ Allocate rest parameter array");
277
278 int num_parameters = info->scope()->num_parameters();
279 int offset = num_parameters * kPointerSize;
280
281 __ li(RestParamAccessDescriptor::parameter_count(),
282 Operand(Smi::FromInt(num_parameters)));
283 __ Addu(RestParamAccessDescriptor::parameter_pointer(), fp,
284 Operand(StandardFrameConstants::kCallerSPOffset + offset));
285 __ li(RestParamAccessDescriptor::rest_parameter_index(),
286 Operand(Smi::FromInt(rest_index)));
287 DCHECK(a1.is(RestParamAccessDescriptor::rest_parameter_index()));
288 function_in_register_a1 = false;
289
290 RestParamAccessStub stub(isolate());
291 __ CallStub(&stub);
292
293 SetVar(rest_param, v0, a1, a2);
294 }
295
296 Variable* arguments = scope()->arguments();
297 if (arguments != NULL) {
298 // Function uses arguments object.
299 Comment cmnt(masm_, "[ Allocate arguments object");
300 DCHECK(a1.is(ArgumentsAccessNewDescriptor::function()));
301 if (!function_in_register_a1) {
302 // Load this again, if it's used by the local context below.
303 __ lw(a1, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
304 }
305 // Receiver is just before the parameters on the caller's stack.
306 int num_parameters = info->scope()->num_parameters();
307 int offset = num_parameters * kPointerSize;
308 __ li(ArgumentsAccessNewDescriptor::parameter_count(),
309 Operand(Smi::FromInt(num_parameters)));
310 __ Addu(ArgumentsAccessNewDescriptor::parameter_pointer(), fp,
311 Operand(StandardFrameConstants::kCallerSPOffset + offset));
312
313 // Arguments to ArgumentsAccessStub:
314 // function, parameter pointer, parameter count.
315 // The stub will rewrite parameter pointer and parameter count if the
316 // previous stack frame was an arguments adapter frame.
317 bool is_unmapped = is_strict(language_mode()) || !has_simple_parameters();
318 ArgumentsAccessStub::Type type = ArgumentsAccessStub::ComputeType(
319 is_unmapped, literal()->has_duplicate_parameters());
320 ArgumentsAccessStub stub(isolate(), type);
321 __ CallStub(&stub);
322
323 SetVar(arguments, v0, a1, a2);
324 }
325
326 if (FLAG_trace) {
327 __ CallRuntime(Runtime::kTraceEnter);
328 }
329
330 // Visit the declarations and body unless there is an illegal
331 // redeclaration.
332 if (scope()->HasIllegalRedeclaration()) {
333 Comment cmnt(masm_, "[ Declarations");
334 VisitForEffect(scope()->GetIllegalRedeclaration());
335
336 } else {
337 PrepareForBailoutForId(BailoutId::FunctionEntry(), NO_REGISTERS);
338 { Comment cmnt(masm_, "[ Declarations");
339 VisitDeclarations(scope()->declarations());
340 }
341
342 // Assert that the declarations do not use ICs. Otherwise the debugger
343 // won't be able to redirect a PC at an IC to the correct IC in newly
344 // recompiled code.
345 DCHECK_EQ(0, ic_total_count_);
346
347 { Comment cmnt(masm_, "[ Stack check");
348 PrepareForBailoutForId(BailoutId::Declarations(), NO_REGISTERS);
349 Label ok;
350 __ LoadRoot(at, Heap::kStackLimitRootIndex);
351 __ Branch(&ok, hs, sp, Operand(at));
352 Handle<Code> stack_check = isolate()->builtins()->StackCheck();
353 PredictableCodeSizeScope predictable(masm_,
354 masm_->CallSize(stack_check, RelocInfo::CODE_TARGET));
355 __ Call(stack_check, RelocInfo::CODE_TARGET);
356 __ bind(&ok);
357 }
358
359 { Comment cmnt(masm_, "[ Body");
360 DCHECK(loop_depth() == 0);
361 VisitStatements(literal()->body());
362 DCHECK(loop_depth() == 0);
363 }
364 }
365
366 // Always emit a 'return undefined' in case control fell off the end of
367 // the body.
368 { Comment cmnt(masm_, "[ return <undefined>;");
369 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
370 }
371 EmitReturnSequence();
372}
373
374
375void FullCodeGenerator::ClearAccumulator() {
376 DCHECK(Smi::FromInt(0) == 0);
377 __ mov(v0, zero_reg);
378}
379
380
381void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
382 __ li(a2, Operand(profiling_counter_));
383 __ lw(a3, FieldMemOperand(a2, Cell::kValueOffset));
384 __ Subu(a3, a3, Operand(Smi::FromInt(delta)));
385 __ sw(a3, FieldMemOperand(a2, Cell::kValueOffset));
386}
387
388
389void FullCodeGenerator::EmitProfilingCounterReset() {
390 int reset_value = FLAG_interrupt_budget;
391 if (info_->is_debug()) {
392 // Detect debug break requests as soon as possible.
393 reset_value = FLAG_interrupt_budget >> 4;
394 }
395 __ li(a2, Operand(profiling_counter_));
396 __ li(a3, Operand(Smi::FromInt(reset_value)));
397 __ sw(a3, FieldMemOperand(a2, Cell::kValueOffset));
398}
399
400
401void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
402 Label* back_edge_target) {
403 // The generated code is used in Deoptimizer::PatchStackCheckCodeAt so we need
404 // to make sure it is constant. Branch may emit a skip-or-jump sequence
405 // instead of the normal Branch. It seems that the "skip" part of that
406 // sequence is about as long as this Branch would be so it is safe to ignore
407 // that.
408 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
409 Comment cmnt(masm_, "[ Back edge bookkeeping");
410 Label ok;
411 DCHECK(back_edge_target->is_bound());
412 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
413 int weight = Min(kMaxBackEdgeWeight,
414 Max(1, distance / kCodeSizeMultiplier));
415 EmitProfilingCounterDecrement(weight);
416 __ slt(at, a3, zero_reg);
417 __ beq(at, zero_reg, &ok);
418 // Call will emit a li t9 first, so it is safe to use the delay slot.
419 __ Call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
420 // Record a mapping of this PC offset to the OSR id. This is used to find
421 // the AST id from the unoptimized code in order to use it as a key into
422 // the deoptimization input data found in the optimized code.
423 RecordBackEdge(stmt->OsrEntryId());
424 EmitProfilingCounterReset();
425
426 __ bind(&ok);
427 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
428 // Record a mapping of the OSR id to this PC. This is used if the OSR
429 // entry becomes the target of a bailout. We don't expect it to be, but
430 // we want it to work if it is.
431 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
432}
433
434
435void FullCodeGenerator::EmitReturnSequence() {
436 Comment cmnt(masm_, "[ Return sequence");
437 if (return_label_.is_bound()) {
438 __ Branch(&return_label_);
439 } else {
440 __ bind(&return_label_);
441 if (FLAG_trace) {
442 // Push the return value on the stack as the parameter.
443 // Runtime::TraceExit returns its parameter in v0.
444 __ push(v0);
445 __ CallRuntime(Runtime::kTraceExit);
446 }
447 // Pretend that the exit is a backwards jump to the entry.
448 int weight = 1;
449 if (info_->ShouldSelfOptimize()) {
450 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
451 } else {
452 int distance = masm_->pc_offset();
453 weight = Min(kMaxBackEdgeWeight,
454 Max(1, distance / kCodeSizeMultiplier));
455 }
456 EmitProfilingCounterDecrement(weight);
457 Label ok;
458 __ Branch(&ok, ge, a3, Operand(zero_reg));
459 __ push(v0);
460 __ Call(isolate()->builtins()->InterruptCheck(),
461 RelocInfo::CODE_TARGET);
462 __ pop(v0);
463 EmitProfilingCounterReset();
464 __ bind(&ok);
465
466 // Make sure that the constant pool is not emitted inside of the return
467 // sequence.
468 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm_);
469 // Here we use masm_-> instead of the __ macro to avoid the code coverage
470 // tool from instrumenting as we rely on the code size here.
471 int32_t arg_count = info_->scope()->num_parameters() + 1;
472 int32_t sp_delta = arg_count * kPointerSize;
473 SetReturnPosition(literal());
474 masm_->mov(sp, fp);
475 masm_->MultiPop(static_cast<RegList>(fp.bit() | ra.bit()));
476 masm_->Addu(sp, sp, Operand(sp_delta));
477 masm_->Jump(ra);
478 }
479 }
480}
481
482
483void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
484 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
485 codegen()->GetVar(result_register(), var);
486 __ push(result_register());
487}
488
489
490void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
491}
492
493
494void FullCodeGenerator::AccumulatorValueContext::Plug(
495 Heap::RootListIndex index) const {
496 __ LoadRoot(result_register(), index);
497}
498
499
500void FullCodeGenerator::StackValueContext::Plug(
501 Heap::RootListIndex index) const {
502 __ LoadRoot(result_register(), index);
503 __ push(result_register());
504}
505
506
507void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
508 codegen()->PrepareForBailoutBeforeSplit(condition(),
509 true,
510 true_label_,
511 false_label_);
512 if (index == Heap::kUndefinedValueRootIndex ||
513 index == Heap::kNullValueRootIndex ||
514 index == Heap::kFalseValueRootIndex) {
515 if (false_label_ != fall_through_) __ Branch(false_label_);
516 } else if (index == Heap::kTrueValueRootIndex) {
517 if (true_label_ != fall_through_) __ Branch(true_label_);
518 } else {
519 __ LoadRoot(result_register(), index);
520 codegen()->DoTest(this);
521 }
522}
523
524
525void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
526}
527
528
529void FullCodeGenerator::AccumulatorValueContext::Plug(
530 Handle<Object> lit) const {
531 __ li(result_register(), Operand(lit));
532}
533
534
535void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
536 // Immediates cannot be pushed directly.
537 __ li(result_register(), Operand(lit));
538 __ push(result_register());
539}
540
541
542void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
543 codegen()->PrepareForBailoutBeforeSplit(condition(),
544 true,
545 true_label_,
546 false_label_);
547 DCHECK(!lit->IsUndetectableObject()); // There are no undetectable literals.
548 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
549 if (false_label_ != fall_through_) __ Branch(false_label_);
550 } else if (lit->IsTrue() || lit->IsJSObject()) {
551 if (true_label_ != fall_through_) __ Branch(true_label_);
552 } else if (lit->IsString()) {
553 if (String::cast(*lit)->length() == 0) {
554 if (false_label_ != fall_through_) __ Branch(false_label_);
555 } else {
556 if (true_label_ != fall_through_) __ Branch(true_label_);
557 }
558 } else if (lit->IsSmi()) {
559 if (Smi::cast(*lit)->value() == 0) {
560 if (false_label_ != fall_through_) __ Branch(false_label_);
561 } else {
562 if (true_label_ != fall_through_) __ Branch(true_label_);
563 }
564 } else {
565 // For simplicity we always test the accumulator register.
566 __ li(result_register(), Operand(lit));
567 codegen()->DoTest(this);
568 }
569}
570
571
572void FullCodeGenerator::EffectContext::DropAndPlug(int count,
573 Register reg) const {
574 DCHECK(count > 0);
575 __ Drop(count);
576}
577
578
579void FullCodeGenerator::AccumulatorValueContext::DropAndPlug(
580 int count,
581 Register reg) const {
582 DCHECK(count > 0);
583 __ Drop(count);
584 __ Move(result_register(), reg);
585}
586
587
588void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
589 Register reg) const {
590 DCHECK(count > 0);
591 if (count > 1) __ Drop(count - 1);
592 __ sw(reg, MemOperand(sp, 0));
593}
594
595
596void FullCodeGenerator::TestContext::DropAndPlug(int count,
597 Register reg) const {
598 DCHECK(count > 0);
599 // For simplicity we always test the accumulator register.
600 __ Drop(count);
601 __ Move(result_register(), reg);
602 codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL);
603 codegen()->DoTest(this);
604}
605
606
607void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
608 Label* materialize_false) const {
609 DCHECK(materialize_true == materialize_false);
610 __ bind(materialize_true);
611}
612
613
614void FullCodeGenerator::AccumulatorValueContext::Plug(
615 Label* materialize_true,
616 Label* materialize_false) const {
617 Label done;
618 __ bind(materialize_true);
619 __ LoadRoot(result_register(), Heap::kTrueValueRootIndex);
620 __ Branch(&done);
621 __ bind(materialize_false);
622 __ LoadRoot(result_register(), Heap::kFalseValueRootIndex);
623 __ bind(&done);
624}
625
626
627void FullCodeGenerator::StackValueContext::Plug(
628 Label* materialize_true,
629 Label* materialize_false) const {
630 Label done;
631 __ bind(materialize_true);
632 __ LoadRoot(at, Heap::kTrueValueRootIndex);
633 // Push the value as the following branch can clobber at in long branch mode.
634 __ push(at);
635 __ Branch(&done);
636 __ bind(materialize_false);
637 __ LoadRoot(at, Heap::kFalseValueRootIndex);
638 __ push(at);
639 __ bind(&done);
640}
641
642
643void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
644 Label* materialize_false) const {
645 DCHECK(materialize_true == true_label_);
646 DCHECK(materialize_false == false_label_);
647}
648
649
650void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
651 Heap::RootListIndex value_root_index =
652 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
653 __ LoadRoot(result_register(), value_root_index);
654}
655
656
657void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
658 Heap::RootListIndex value_root_index =
659 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
660 __ LoadRoot(at, value_root_index);
661 __ push(at);
662}
663
664
665void FullCodeGenerator::TestContext::Plug(bool flag) const {
666 codegen()->PrepareForBailoutBeforeSplit(condition(),
667 true,
668 true_label_,
669 false_label_);
670 if (flag) {
671 if (true_label_ != fall_through_) __ Branch(true_label_);
672 } else {
673 if (false_label_ != fall_through_) __ Branch(false_label_);
674 }
675}
676
677
678void FullCodeGenerator::DoTest(Expression* condition,
679 Label* if_true,
680 Label* if_false,
681 Label* fall_through) {
682 __ mov(a0, result_register());
683 Handle<Code> ic = ToBooleanStub::GetUninitialized(isolate());
684 CallIC(ic, condition->test_id());
685 __ LoadRoot(at, Heap::kTrueValueRootIndex);
686 Split(eq, result_register(), Operand(at), if_true, if_false, fall_through);
687}
688
689
690void FullCodeGenerator::Split(Condition cc,
691 Register lhs,
692 const Operand& rhs,
693 Label* if_true,
694 Label* if_false,
695 Label* fall_through) {
696 if (if_false == fall_through) {
697 __ Branch(if_true, cc, lhs, rhs);
698 } else if (if_true == fall_through) {
699 __ Branch(if_false, NegateCondition(cc), lhs, rhs);
700 } else {
701 __ Branch(if_true, cc, lhs, rhs);
702 __ Branch(if_false);
703 }
704}
705
706
707MemOperand FullCodeGenerator::StackOperand(Variable* var) {
708 DCHECK(var->IsStackAllocated());
709 // Offset is negative because higher indexes are at lower addresses.
710 int offset = -var->index() * kPointerSize;
711 // Adjust by a (parameter or local) base offset.
712 if (var->IsParameter()) {
713 offset += (info_->scope()->num_parameters() + 1) * kPointerSize;
714 } else {
715 offset += JavaScriptFrameConstants::kLocal0Offset;
716 }
717 return MemOperand(fp, offset);
718}
719
720
721MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
722 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
723 if (var->IsContextSlot()) {
724 int context_chain_length = scope()->ContextChainLength(var->scope());
725 __ LoadContext(scratch, context_chain_length);
726 return ContextMemOperand(scratch, var->index());
727 } else {
728 return StackOperand(var);
729 }
730}
731
732
733void FullCodeGenerator::GetVar(Register dest, Variable* var) {
734 // Use destination as scratch.
735 MemOperand location = VarOperand(var, dest);
736 __ lw(dest, location);
737}
738
739
740void FullCodeGenerator::SetVar(Variable* var,
741 Register src,
742 Register scratch0,
743 Register scratch1) {
744 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
745 DCHECK(!scratch0.is(src));
746 DCHECK(!scratch0.is(scratch1));
747 DCHECK(!scratch1.is(src));
748 MemOperand location = VarOperand(var, scratch0);
749 __ sw(src, location);
750 // Emit the write barrier code if the location is in the heap.
751 if (var->IsContextSlot()) {
752 __ RecordWriteContextSlot(scratch0,
753 location.offset(),
754 src,
755 scratch1,
756 kRAHasBeenSaved,
757 kDontSaveFPRegs);
758 }
759}
760
761
762void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
763 bool should_normalize,
764 Label* if_true,
765 Label* if_false) {
766 // Only prepare for bailouts before splits if we're in a test
767 // context. Otherwise, we let the Visit function deal with the
768 // preparation to avoid preparing with the same AST id twice.
769 if (!context()->IsTest()) return;
770
771 Label skip;
772 if (should_normalize) __ Branch(&skip);
773 PrepareForBailout(expr, TOS_REG);
774 if (should_normalize) {
775 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
776 Split(eq, a0, Operand(t0), if_true, if_false, NULL);
777 __ bind(&skip);
778 }
779}
780
781
782void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
783 // The variable in the declaration always resides in the current function
784 // context.
785 DCHECK_EQ(0, scope()->ContextChainLength(variable->scope()));
786 if (generate_debug_code_) {
787 // Check that we're not inside a with or catch context.
788 __ lw(a1, FieldMemOperand(cp, HeapObject::kMapOffset));
789 __ LoadRoot(t0, Heap::kWithContextMapRootIndex);
790 __ Check(ne, kDeclarationInWithContext,
791 a1, Operand(t0));
792 __ LoadRoot(t0, Heap::kCatchContextMapRootIndex);
793 __ Check(ne, kDeclarationInCatchContext,
794 a1, Operand(t0));
795 }
796}
797
798
799void FullCodeGenerator::VisitVariableDeclaration(
800 VariableDeclaration* declaration) {
801 // If it was not possible to allocate the variable at compile time, we
802 // need to "declare" it at runtime to make sure it actually exists in the
803 // local context.
804 VariableProxy* proxy = declaration->proxy();
805 VariableMode mode = declaration->mode();
806 Variable* variable = proxy->var();
807 bool hole_init = mode == LET || mode == CONST || mode == CONST_LEGACY;
808 switch (variable->location()) {
809 case VariableLocation::GLOBAL:
810 case VariableLocation::UNALLOCATED:
811 globals_->Add(variable->name(), zone());
812 globals_->Add(variable->binding_needs_init()
813 ? isolate()->factory()->the_hole_value()
814 : isolate()->factory()->undefined_value(),
815 zone());
816 break;
817
818 case VariableLocation::PARAMETER:
819 case VariableLocation::LOCAL:
820 if (hole_init) {
821 Comment cmnt(masm_, "[ VariableDeclaration");
822 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
823 __ sw(t0, StackOperand(variable));
824 }
825 break;
826
827 case VariableLocation::CONTEXT:
828 if (hole_init) {
829 Comment cmnt(masm_, "[ VariableDeclaration");
830 EmitDebugCheckDeclarationContext(variable);
831 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
832 __ sw(at, ContextMemOperand(cp, variable->index()));
833 // No write barrier since the_hole_value is in old space.
834 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
835 }
836 break;
837
838 case VariableLocation::LOOKUP: {
839 Comment cmnt(masm_, "[ VariableDeclaration");
840 __ li(a2, Operand(variable->name()));
841 // Declaration nodes are always introduced in one of four modes.
842 DCHECK(IsDeclaredVariableMode(mode));
843 // Push initial value, if any.
844 // Note: For variables we must not push an initial value (such as
845 // 'undefined') because we may have a (legal) redeclaration and we
846 // must not destroy the current value.
847 if (hole_init) {
848 __ LoadRoot(a0, Heap::kTheHoleValueRootIndex);
849 } else {
850 DCHECK(Smi::FromInt(0) == 0);
851 __ mov(a0, zero_reg); // Smi::FromInt(0) indicates no initial value.
852 }
853 __ Push(a2, a0);
854 __ Push(Smi::FromInt(variable->DeclarationPropertyAttributes()));
855 __ CallRuntime(Runtime::kDeclareLookupSlot);
856 break;
857 }
858 }
859}
860
861
862void FullCodeGenerator::VisitFunctionDeclaration(
863 FunctionDeclaration* declaration) {
864 VariableProxy* proxy = declaration->proxy();
865 Variable* variable = proxy->var();
866 switch (variable->location()) {
867 case VariableLocation::GLOBAL:
868 case VariableLocation::UNALLOCATED: {
869 globals_->Add(variable->name(), zone());
870 Handle<SharedFunctionInfo> function =
871 Compiler::GetSharedFunctionInfo(declaration->fun(), script(), info_);
872 // Check for stack-overflow exception.
873 if (function.is_null()) return SetStackOverflow();
874 globals_->Add(function, zone());
875 break;
876 }
877
878 case VariableLocation::PARAMETER:
879 case VariableLocation::LOCAL: {
880 Comment cmnt(masm_, "[ FunctionDeclaration");
881 VisitForAccumulatorValue(declaration->fun());
882 __ sw(result_register(), StackOperand(variable));
883 break;
884 }
885
886 case VariableLocation::CONTEXT: {
887 Comment cmnt(masm_, "[ FunctionDeclaration");
888 EmitDebugCheckDeclarationContext(variable);
889 VisitForAccumulatorValue(declaration->fun());
890 __ sw(result_register(), ContextMemOperand(cp, variable->index()));
891 int offset = Context::SlotOffset(variable->index());
892 // We know that we have written a function, which is not a smi.
893 __ RecordWriteContextSlot(cp,
894 offset,
895 result_register(),
896 a2,
897 kRAHasBeenSaved,
898 kDontSaveFPRegs,
899 EMIT_REMEMBERED_SET,
900 OMIT_SMI_CHECK);
901 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
902 break;
903 }
904
905 case VariableLocation::LOOKUP: {
906 Comment cmnt(masm_, "[ FunctionDeclaration");
907 __ li(a2, Operand(variable->name()));
908 __ Push(a2);
909 // Push initial value for function declaration.
910 VisitForStackValue(declaration->fun());
911 __ Push(Smi::FromInt(variable->DeclarationPropertyAttributes()));
912 __ CallRuntime(Runtime::kDeclareLookupSlot);
913 break;
914 }
915 }
916}
917
918
919void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
920 // Call the runtime to declare the globals.
921 __ li(a1, Operand(pairs));
922 __ li(a0, Operand(Smi::FromInt(DeclareGlobalsFlags())));
923 __ Push(a1, a0);
924 __ CallRuntime(Runtime::kDeclareGlobals);
925 // Return value is ignored.
926}
927
928
929void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
930 // Call the runtime to declare the modules.
931 __ Push(descriptions);
932 __ CallRuntime(Runtime::kDeclareModules);
933 // Return value is ignored.
934}
935
936
937void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
938 Comment cmnt(masm_, "[ SwitchStatement");
939 Breakable nested_statement(this, stmt);
940 SetStatementPosition(stmt);
941
942 // Keep the switch value on the stack until a case matches.
943 VisitForStackValue(stmt->tag());
944 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
945
946 ZoneList<CaseClause*>* clauses = stmt->cases();
947 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
948
949 Label next_test; // Recycled for each test.
950 // Compile all the tests with branches to their bodies.
951 for (int i = 0; i < clauses->length(); i++) {
952 CaseClause* clause = clauses->at(i);
953 clause->body_target()->Unuse();
954
955 // The default is not a test, but remember it as final fall through.
956 if (clause->is_default()) {
957 default_clause = clause;
958 continue;
959 }
960
961 Comment cmnt(masm_, "[ Case comparison");
962 __ bind(&next_test);
963 next_test.Unuse();
964
965 // Compile the label expression.
966 VisitForAccumulatorValue(clause->label());
967 __ mov(a0, result_register()); // CompareStub requires args in a0, a1.
968
969 // Perform the comparison as if via '==='.
970 __ lw(a1, MemOperand(sp, 0)); // Switch value.
971 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
972 JumpPatchSite patch_site(masm_);
973 if (inline_smi_code) {
974 Label slow_case;
975 __ or_(a2, a1, a0);
976 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
977
978 __ Branch(&next_test, ne, a1, Operand(a0));
979 __ Drop(1); // Switch value is no longer needed.
980 __ Branch(clause->body_target());
981
982 __ bind(&slow_case);
983 }
984
985 // Record position before stub call for type feedback.
986 SetExpressionPosition(clause);
987 Handle<Code> ic = CodeFactory::CompareIC(isolate(), Token::EQ_STRICT,
988 strength(language_mode())).code();
989 CallIC(ic, clause->CompareId());
990 patch_site.EmitPatchInfo();
991
992 Label skip;
993 __ Branch(&skip);
994 PrepareForBailout(clause, TOS_REG);
995 __ LoadRoot(at, Heap::kTrueValueRootIndex);
996 __ Branch(&next_test, ne, v0, Operand(at));
997 __ Drop(1);
998 __ Branch(clause->body_target());
999 __ bind(&skip);
1000
1001 __ Branch(&next_test, ne, v0, Operand(zero_reg));
1002 __ Drop(1); // Switch value is no longer needed.
1003 __ Branch(clause->body_target());
1004 }
1005
1006 // Discard the test value and jump to the default if present, otherwise to
1007 // the end of the statement.
1008 __ bind(&next_test);
1009 __ Drop(1); // Switch value is no longer needed.
1010 if (default_clause == NULL) {
1011 __ Branch(nested_statement.break_label());
1012 } else {
1013 __ Branch(default_clause->body_target());
1014 }
1015
1016 // Compile all the case bodies.
1017 for (int i = 0; i < clauses->length(); i++) {
1018 Comment cmnt(masm_, "[ Case body");
1019 CaseClause* clause = clauses->at(i);
1020 __ bind(clause->body_target());
1021 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
1022 VisitStatements(clause->statements());
1023 }
1024
1025 __ bind(nested_statement.break_label());
1026 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1027}
1028
1029
1030void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
1031 Comment cmnt(masm_, "[ ForInStatement");
1032 SetStatementPosition(stmt, SKIP_BREAK);
1033
1034 FeedbackVectorSlot slot = stmt->ForInFeedbackSlot();
1035
1036 Label loop, exit;
1037 ForIn loop_statement(this, stmt);
1038 increment_loop_depth();
1039
1040 // Get the object to enumerate over. If the object is null or undefined, skip
1041 // over the loop. See ECMA-262 version 5, section 12.6.4.
1042 SetExpressionAsStatementPosition(stmt->enumerable());
1043 VisitForAccumulatorValue(stmt->enumerable());
1044 __ mov(a0, result_register()); // Result as param to InvokeBuiltin below.
1045 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1046 __ Branch(&exit, eq, a0, Operand(at));
1047 Register null_value = t1;
1048 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
1049 __ Branch(&exit, eq, a0, Operand(null_value));
1050 PrepareForBailoutForId(stmt->PrepareId(), TOS_REG);
1051 __ mov(a0, v0);
1052 // Convert the object to a JS object.
1053 Label convert, done_convert;
1054 __ JumpIfSmi(a0, &convert);
1055 __ GetObjectType(a0, a1, a1);
1056 __ Branch(&done_convert, ge, a1, Operand(FIRST_JS_RECEIVER_TYPE));
1057 __ bind(&convert);
1058 ToObjectStub stub(isolate());
1059 __ CallStub(&stub);
1060 __ mov(a0, v0);
1061 __ bind(&done_convert);
1062 PrepareForBailoutForId(stmt->ToObjectId(), TOS_REG);
1063 __ push(a0);
1064
1065 // Check for proxies.
1066 Label call_runtime;
1067 __ GetObjectType(a0, a1, a1);
1068 __ Branch(&call_runtime, eq, a1, Operand(JS_PROXY_TYPE));
1069
1070 // Check cache validity in generated code. This is a fast case for
1071 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1072 // guarantee cache validity, call the runtime system to check cache
1073 // validity or get the property names in a fixed array.
1074 __ CheckEnumCache(null_value, &call_runtime);
1075
1076 // The enum cache is valid. Load the map of the object being
1077 // iterated over and use the cache for the iteration.
1078 Label use_cache;
1079 __ lw(v0, FieldMemOperand(a0, HeapObject::kMapOffset));
1080 __ Branch(&use_cache);
1081
1082 // Get the set of properties to enumerate.
1083 __ bind(&call_runtime);
1084 __ push(a0); // Duplicate the enumerable object on the stack.
1085 __ CallRuntime(Runtime::kGetPropertyNamesFast);
1086 PrepareForBailoutForId(stmt->EnumId(), TOS_REG);
1087
1088 // If we got a map from the runtime call, we can do a fast
1089 // modification check. Otherwise, we got a fixed array, and we have
1090 // to do a slow check.
1091 Label fixed_array;
1092 __ lw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
1093 __ LoadRoot(at, Heap::kMetaMapRootIndex);
1094 __ Branch(&fixed_array, ne, a2, Operand(at));
1095
1096 // We got a map in register v0. Get the enumeration cache from it.
1097 Label no_descriptors;
1098 __ bind(&use_cache);
1099
1100 __ EnumLength(a1, v0);
1101 __ Branch(&no_descriptors, eq, a1, Operand(Smi::FromInt(0)));
1102
1103 __ LoadInstanceDescriptors(v0, a2);
1104 __ lw(a2, FieldMemOperand(a2, DescriptorArray::kEnumCacheOffset));
1105 __ lw(a2, FieldMemOperand(a2, DescriptorArray::kEnumCacheBridgeCacheOffset));
1106
1107 // Set up the four remaining stack slots.
1108 __ li(a0, Operand(Smi::FromInt(0)));
1109 // Push map, enumeration cache, enumeration cache length (as smi) and zero.
1110 __ Push(v0, a2, a1, a0);
1111 __ jmp(&loop);
1112
1113 __ bind(&no_descriptors);
1114 __ Drop(1);
1115 __ jmp(&exit);
1116
1117 // We got a fixed array in register v0. Iterate through that.
1118 __ bind(&fixed_array);
1119
1120 __ EmitLoadTypeFeedbackVector(a1);
1121 __ li(a2, Operand(TypeFeedbackVector::MegamorphicSentinel(isolate())));
1122 int vector_index = SmiFromSlot(slot)->value();
1123 __ sw(a2, FieldMemOperand(a1, FixedArray::OffsetOfElementAt(vector_index)));
1124
1125 __ li(a1, Operand(Smi::FromInt(1))); // Smi(1) indicates slow check
1126 __ Push(a1, v0); // Smi and array
1127 __ lw(a1, FieldMemOperand(v0, FixedArray::kLengthOffset));
1128 __ li(a0, Operand(Smi::FromInt(0)));
1129 __ Push(a1, a0); // Fixed array length (as smi) and initial index.
1130
1131 // Generate code for doing the condition check.
1132 __ bind(&loop);
1133 SetExpressionAsStatementPosition(stmt->each());
1134
1135 // Load the current count to a0, load the length to a1.
1136 __ lw(a0, MemOperand(sp, 0 * kPointerSize));
1137 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
1138 __ Branch(loop_statement.break_label(), hs, a0, Operand(a1));
1139
1140 // Get the current entry of the array into register a3.
1141 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
1142 __ Addu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1143 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
1144 __ addu(t0, a2, t0); // Array base + scaled (smi) index.
1145 __ lw(a3, MemOperand(t0)); // Current entry.
1146
1147 // Get the expected map from the stack or a smi in the
1148 // permanent slow case into register a2.
1149 __ lw(a2, MemOperand(sp, 3 * kPointerSize));
1150
1151 // Check if the expected map still matches that of the enumerable.
1152 // If not, we may have to filter the key.
1153 Label update_each;
1154 __ lw(a1, MemOperand(sp, 4 * kPointerSize));
1155 __ lw(t0, FieldMemOperand(a1, HeapObject::kMapOffset));
1156 __ Branch(&update_each, eq, t0, Operand(a2));
1157
1158 // Convert the entry to a string or (smi) 0 if it isn't a property
1159 // any more. If the property has been removed while iterating, we
1160 // just skip it.
1161 __ Push(a1, a3); // Enumerable and current entry.
1162 __ CallRuntime(Runtime::kForInFilter);
1163 PrepareForBailoutForId(stmt->FilterId(), TOS_REG);
1164 __ mov(a3, result_register());
1165 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1166 __ Branch(loop_statement.continue_label(), eq, a3, Operand(at));
1167
1168 // Update the 'each' property or variable from the possibly filtered
1169 // entry in register a3.
1170 __ bind(&update_each);
1171 __ mov(result_register(), a3);
1172 // Perform the assignment as if via '='.
1173 { EffectContext context(this);
1174 EmitAssignment(stmt->each(), stmt->EachFeedbackSlot());
1175 PrepareForBailoutForId(stmt->AssignmentId(), NO_REGISTERS);
1176 }
1177
1178 // Both Crankshaft and Turbofan expect BodyId to be right before stmt->body().
1179 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
1180 // Generate code for the body of the loop.
1181 Visit(stmt->body());
1182
1183 // Generate code for the going to the next element by incrementing
1184 // the index (smi) stored on top of the stack.
1185 __ bind(loop_statement.continue_label());
1186 __ pop(a0);
1187 __ Addu(a0, a0, Operand(Smi::FromInt(1)));
1188 __ push(a0);
1189
1190 EmitBackEdgeBookkeeping(stmt, &loop);
1191 __ Branch(&loop);
1192
1193 // Remove the pointers stored on the stack.
1194 __ bind(loop_statement.break_label());
1195 __ Drop(5);
1196
1197 // Exit and decrement the loop depth.
1198 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1199 __ bind(&exit);
1200 decrement_loop_depth();
1201}
1202
1203
1204void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1205 bool pretenure) {
1206 // Use the fast case closure allocation code that allocates in new
1207 // space for nested functions that don't need literals cloning. If
1208 // we're running with the --always-opt or the --prepare-always-opt
1209 // flag, we need to use the runtime function so that the new function
1210 // we are creating here gets a chance to have its code optimized and
1211 // doesn't just get a copy of the existing unoptimized code.
1212 if (!FLAG_always_opt &&
1213 !FLAG_prepare_always_opt &&
1214 !pretenure &&
1215 scope()->is_function_scope() &&
1216 info->num_literals() == 0) {
1217 FastNewClosureStub stub(isolate(), info->language_mode(), info->kind());
1218 __ li(a2, Operand(info));
1219 __ CallStub(&stub);
1220 } else {
1221 __ Push(info);
1222 __ CallRuntime(pretenure ? Runtime::kNewClosure_Tenured
1223 : Runtime::kNewClosure);
1224 }
1225 context()->Plug(v0);
1226}
1227
1228
1229void FullCodeGenerator::EmitSetHomeObject(Expression* initializer, int offset,
1230 FeedbackVectorSlot slot) {
1231 DCHECK(NeedsHomeObject(initializer));
1232 __ lw(StoreDescriptor::ReceiverRegister(), MemOperand(sp));
1233 __ li(StoreDescriptor::NameRegister(),
1234 Operand(isolate()->factory()->home_object_symbol()));
1235 __ lw(StoreDescriptor::ValueRegister(),
1236 MemOperand(sp, offset * kPointerSize));
1237 EmitLoadStoreICSlot(slot);
1238 CallStoreIC();
1239}
1240
1241
1242void FullCodeGenerator::EmitSetHomeObjectAccumulator(Expression* initializer,
1243 int offset,
1244 FeedbackVectorSlot slot) {
1245 DCHECK(NeedsHomeObject(initializer));
1246 __ Move(StoreDescriptor::ReceiverRegister(), v0);
1247 __ li(StoreDescriptor::NameRegister(),
1248 Operand(isolate()->factory()->home_object_symbol()));
1249 __ lw(StoreDescriptor::ValueRegister(),
1250 MemOperand(sp, offset * kPointerSize));
1251 EmitLoadStoreICSlot(slot);
1252 CallStoreIC();
1253}
1254
1255
1256void FullCodeGenerator::EmitLoadGlobalCheckExtensions(VariableProxy* proxy,
1257 TypeofMode typeof_mode,
1258 Label* slow) {
1259 Register current = cp;
1260 Register next = a1;
1261 Register temp = a2;
1262
1263 Scope* s = scope();
1264 while (s != NULL) {
1265 if (s->num_heap_slots() > 0) {
1266 if (s->calls_sloppy_eval()) {
1267 // Check that extension is "the hole".
1268 __ lw(temp, ContextMemOperand(current, Context::EXTENSION_INDEX));
1269 __ JumpIfNotRoot(temp, Heap::kTheHoleValueRootIndex, slow);
1270 }
1271 // Load next context in chain.
1272 __ lw(next, ContextMemOperand(current, Context::PREVIOUS_INDEX));
1273 // Walk the rest of the chain without clobbering cp.
1274 current = next;
1275 }
1276 // If no outer scope calls eval, we do not need to check more
1277 // context extensions.
1278 if (!s->outer_scope_calls_sloppy_eval() || s->is_eval_scope()) break;
1279 s = s->outer_scope();
1280 }
1281
1282 if (s->is_eval_scope()) {
1283 Label loop, fast;
1284 if (!current.is(next)) {
1285 __ Move(next, current);
1286 }
1287 __ bind(&loop);
1288 // Terminate at native context.
1289 __ lw(temp, FieldMemOperand(next, HeapObject::kMapOffset));
1290 __ LoadRoot(t0, Heap::kNativeContextMapRootIndex);
1291 __ Branch(&fast, eq, temp, Operand(t0));
1292 // Check that extension is "the hole".
1293 __ lw(temp, ContextMemOperand(next, Context::EXTENSION_INDEX));
1294 __ JumpIfNotRoot(temp, Heap::kTheHoleValueRootIndex, slow);
1295 // Load next context in chain.
1296 __ lw(next, ContextMemOperand(next, Context::PREVIOUS_INDEX));
1297 __ Branch(&loop);
1298 __ bind(&fast);
1299 }
1300
1301 // All extension objects were empty and it is safe to use a normal global
1302 // load machinery.
1303 EmitGlobalVariableLoad(proxy, typeof_mode);
1304}
1305
1306
1307MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1308 Label* slow) {
1309 DCHECK(var->IsContextSlot());
1310 Register context = cp;
1311 Register next = a3;
1312 Register temp = t0;
1313
1314 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
1315 if (s->num_heap_slots() > 0) {
1316 if (s->calls_sloppy_eval()) {
1317 // Check that extension is "the hole".
1318 __ lw(temp, ContextMemOperand(context, Context::EXTENSION_INDEX));
1319 __ JumpIfNotRoot(temp, Heap::kTheHoleValueRootIndex, slow);
1320 }
1321 __ lw(next, ContextMemOperand(context, Context::PREVIOUS_INDEX));
1322 // Walk the rest of the chain without clobbering cp.
1323 context = next;
1324 }
1325 }
1326 // Check that last extension is "the hole".
1327 __ lw(temp, ContextMemOperand(context, Context::EXTENSION_INDEX));
1328 __ JumpIfNotRoot(temp, Heap::kTheHoleValueRootIndex, slow);
1329
1330 // This function is used only for loads, not stores, so it's safe to
1331 // return an cp-based operand (the write barrier cannot be allowed to
1332 // destroy the cp register).
1333 return ContextMemOperand(context, var->index());
1334}
1335
1336
1337void FullCodeGenerator::EmitDynamicLookupFastCase(VariableProxy* proxy,
1338 TypeofMode typeof_mode,
1339 Label* slow, Label* done) {
1340 // Generate fast-case code for variables that might be shadowed by
1341 // eval-introduced variables. Eval is used a lot without
1342 // introducing variables. In those cases, we do not want to
1343 // perform a runtime call for all variables in the scope
1344 // containing the eval.
1345 Variable* var = proxy->var();
1346 if (var->mode() == DYNAMIC_GLOBAL) {
1347 EmitLoadGlobalCheckExtensions(proxy, typeof_mode, slow);
1348 __ Branch(done);
1349 } else if (var->mode() == DYNAMIC_LOCAL) {
1350 Variable* local = var->local_if_not_shadowed();
1351 __ lw(v0, ContextSlotOperandCheckExtensions(local, slow));
1352 if (local->mode() == LET || local->mode() == CONST ||
1353 local->mode() == CONST_LEGACY) {
1354 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1355 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1356 if (local->mode() == CONST_LEGACY) {
1357 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1358 __ Movz(v0, a0, at); // Conditional move: return Undefined if TheHole.
1359 } else { // LET || CONST
1360 __ Branch(done, ne, at, Operand(zero_reg));
1361 __ li(a0, Operand(var->name()));
1362 __ push(a0);
1363 __ CallRuntime(Runtime::kThrowReferenceError);
1364 }
1365 }
1366 __ Branch(done);
1367 }
1368}
1369
1370
1371void FullCodeGenerator::EmitGlobalVariableLoad(VariableProxy* proxy,
1372 TypeofMode typeof_mode) {
1373 Variable* var = proxy->var();
1374 DCHECK(var->IsUnallocatedOrGlobalSlot() ||
1375 (var->IsLookupSlot() && var->mode() == DYNAMIC_GLOBAL));
1376 __ LoadGlobalObject(LoadDescriptor::ReceiverRegister());
1377 __ li(LoadDescriptor::NameRegister(), Operand(var->name()));
1378 __ li(LoadDescriptor::SlotRegister(),
1379 Operand(SmiFromSlot(proxy->VariableFeedbackSlot())));
1380 CallLoadIC(typeof_mode);
1381}
1382
1383
1384void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy,
1385 TypeofMode typeof_mode) {
1386 // Record position before possible IC call.
1387 SetExpressionPosition(proxy);
1388 PrepareForBailoutForId(proxy->BeforeId(), NO_REGISTERS);
1389 Variable* var = proxy->var();
1390
1391 // Three cases: global variables, lookup variables, and all other types of
1392 // variables.
1393 switch (var->location()) {
1394 case VariableLocation::GLOBAL:
1395 case VariableLocation::UNALLOCATED: {
1396 Comment cmnt(masm_, "[ Global variable");
1397 EmitGlobalVariableLoad(proxy, typeof_mode);
1398 context()->Plug(v0);
1399 break;
1400 }
1401
1402 case VariableLocation::PARAMETER:
1403 case VariableLocation::LOCAL:
1404 case VariableLocation::CONTEXT: {
1405 DCHECK_EQ(NOT_INSIDE_TYPEOF, typeof_mode);
1406 Comment cmnt(masm_, var->IsContextSlot() ? "[ Context variable"
1407 : "[ Stack variable");
1408 if (NeedsHoleCheckForLoad(proxy)) {
1409 // Let and const need a read barrier.
1410 GetVar(v0, var);
1411 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
1412 __ subu(at, v0, at); // Sub as compare: at == 0 on eq.
1413 if (var->mode() == LET || var->mode() == CONST) {
1414 // Throw a reference error when using an uninitialized let/const
1415 // binding in harmony mode.
1416 Label done;
1417 __ Branch(&done, ne, at, Operand(zero_reg));
1418 __ li(a0, Operand(var->name()));
1419 __ push(a0);
1420 __ CallRuntime(Runtime::kThrowReferenceError);
1421 __ bind(&done);
1422 } else {
1423 // Uninitialized legacy const bindings are unholed.
1424 DCHECK(var->mode() == CONST_LEGACY);
1425 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1426 __ Movz(v0, a0, at); // Conditional move: Undefined if TheHole.
1427 }
1428 context()->Plug(v0);
1429 break;
1430 }
1431 context()->Plug(var);
1432 break;
1433 }
1434
1435 case VariableLocation::LOOKUP: {
1436 Comment cmnt(masm_, "[ Lookup variable");
1437 Label done, slow;
1438 // Generate code for loading from variables potentially shadowed
1439 // by eval-introduced variables.
1440 EmitDynamicLookupFastCase(proxy, typeof_mode, &slow, &done);
1441 __ bind(&slow);
1442 __ li(a1, Operand(var->name()));
1443 __ Push(cp, a1); // Context and name.
1444 Runtime::FunctionId function_id =
1445 typeof_mode == NOT_INSIDE_TYPEOF
1446 ? Runtime::kLoadLookupSlot
1447 : Runtime::kLoadLookupSlotNoReferenceError;
1448 __ CallRuntime(function_id);
1449 __ bind(&done);
1450 context()->Plug(v0);
1451 }
1452 }
1453}
1454
1455
1456void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1457 Comment cmnt(masm_, "[ RegExpLiteral");
1458 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1459 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1460 __ li(a1, Operand(expr->pattern()));
1461 __ li(a0, Operand(Smi::FromInt(expr->flags())));
1462 FastCloneRegExpStub stub(isolate());
1463 __ CallStub(&stub);
1464 context()->Plug(v0);
1465}
1466
1467
1468void FullCodeGenerator::EmitAccessor(ObjectLiteralProperty* property) {
1469 Expression* expression = (property == NULL) ? NULL : property->value();
1470 if (expression == NULL) {
1471 __ LoadRoot(a1, Heap::kNullValueRootIndex);
1472 __ push(a1);
1473 } else {
1474 VisitForStackValue(expression);
1475 if (NeedsHomeObject(expression)) {
1476 DCHECK(property->kind() == ObjectLiteral::Property::GETTER ||
1477 property->kind() == ObjectLiteral::Property::SETTER);
1478 int offset = property->kind() == ObjectLiteral::Property::GETTER ? 2 : 3;
1479 EmitSetHomeObject(expression, offset, property->GetSlot());
1480 }
1481 }
1482}
1483
1484
1485void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1486 Comment cmnt(masm_, "[ ObjectLiteral");
1487
1488 Handle<FixedArray> constant_properties = expr->constant_properties();
1489 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1490 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1491 __ li(a1, Operand(constant_properties));
1492 __ li(a0, Operand(Smi::FromInt(expr->ComputeFlags())));
1493 if (MustCreateObjectLiteralWithRuntime(expr)) {
1494 __ Push(a3, a2, a1, a0);
1495 __ CallRuntime(Runtime::kCreateObjectLiteral);
1496 } else {
1497 FastCloneShallowObjectStub stub(isolate(), expr->properties_count());
1498 __ CallStub(&stub);
1499 }
1500 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1501
1502 // If result_saved is true the result is on top of the stack. If
1503 // result_saved is false the result is in v0.
1504 bool result_saved = false;
1505
1506 AccessorTable accessor_table(zone());
1507 int property_index = 0;
1508 for (; property_index < expr->properties()->length(); property_index++) {
1509 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1510 if (property->is_computed_name()) break;
1511 if (property->IsCompileTimeValue()) continue;
1512
1513 Literal* key = property->key()->AsLiteral();
1514 Expression* value = property->value();
1515 if (!result_saved) {
1516 __ push(v0); // Save result on stack.
1517 result_saved = true;
1518 }
1519 switch (property->kind()) {
1520 case ObjectLiteral::Property::CONSTANT:
1521 UNREACHABLE();
1522 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1523 DCHECK(!CompileTimeValue::IsCompileTimeValue(property->value()));
1524 // Fall through.
1525 case ObjectLiteral::Property::COMPUTED:
1526 // It is safe to use [[Put]] here because the boilerplate already
1527 // contains computed properties with an uninitialized value.
1528 if (key->value()->IsInternalizedString()) {
1529 if (property->emit_store()) {
1530 VisitForAccumulatorValue(value);
1531 __ mov(StoreDescriptor::ValueRegister(), result_register());
1532 DCHECK(StoreDescriptor::ValueRegister().is(a0));
1533 __ li(StoreDescriptor::NameRegister(), Operand(key->value()));
1534 __ lw(StoreDescriptor::ReceiverRegister(), MemOperand(sp));
1535 EmitLoadStoreICSlot(property->GetSlot(0));
1536 CallStoreIC();
1537 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1538
1539 if (NeedsHomeObject(value)) {
1540 EmitSetHomeObjectAccumulator(value, 0, property->GetSlot(1));
1541 }
1542 } else {
1543 VisitForEffect(value);
1544 }
1545 break;
1546 }
1547 // Duplicate receiver on stack.
1548 __ lw(a0, MemOperand(sp));
1549 __ push(a0);
1550 VisitForStackValue(key);
1551 VisitForStackValue(value);
1552 if (property->emit_store()) {
1553 if (NeedsHomeObject(value)) {
1554 EmitSetHomeObject(value, 2, property->GetSlot());
1555 }
1556 __ li(a0, Operand(Smi::FromInt(SLOPPY))); // PropertyAttributes.
1557 __ push(a0);
1558 __ CallRuntime(Runtime::kSetProperty);
1559 } else {
1560 __ Drop(3);
1561 }
1562 break;
1563 case ObjectLiteral::Property::PROTOTYPE:
1564 // Duplicate receiver on stack.
1565 __ lw(a0, MemOperand(sp));
1566 __ push(a0);
1567 VisitForStackValue(value);
1568 DCHECK(property->emit_store());
1569 __ CallRuntime(Runtime::kInternalSetPrototype);
1570 PrepareForBailoutForId(expr->GetIdForPropertySet(property_index),
1571 NO_REGISTERS);
1572 break;
1573 case ObjectLiteral::Property::GETTER:
1574 if (property->emit_store()) {
1575 accessor_table.lookup(key)->second->getter = property;
1576 }
1577 break;
1578 case ObjectLiteral::Property::SETTER:
1579 if (property->emit_store()) {
1580 accessor_table.lookup(key)->second->setter = property;
1581 }
1582 break;
1583 }
1584 }
1585
1586 // Emit code to define accessors, using only a single call to the runtime for
1587 // each pair of corresponding getters and setters.
1588 for (AccessorTable::Iterator it = accessor_table.begin();
1589 it != accessor_table.end();
1590 ++it) {
1591 __ lw(a0, MemOperand(sp)); // Duplicate receiver.
1592 __ push(a0);
1593 VisitForStackValue(it->first);
1594 EmitAccessor(it->second->getter);
1595 EmitAccessor(it->second->setter);
1596 __ li(a0, Operand(Smi::FromInt(NONE)));
1597 __ push(a0);
1598 __ CallRuntime(Runtime::kDefineAccessorPropertyUnchecked);
1599 }
1600
1601 // Object literals have two parts. The "static" part on the left contains no
1602 // computed property names, and so we can compute its map ahead of time; see
1603 // runtime.cc::CreateObjectLiteralBoilerplate. The second "dynamic" part
1604 // starts with the first computed property name, and continues with all
1605 // properties to its right. All the code from above initializes the static
1606 // component of the object literal, and arranges for the map of the result to
1607 // reflect the static order in which the keys appear. For the dynamic
1608 // properties, we compile them into a series of "SetOwnProperty" runtime
1609 // calls. This will preserve insertion order.
1610 for (; property_index < expr->properties()->length(); property_index++) {
1611 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1612
1613 Expression* value = property->value();
1614 if (!result_saved) {
1615 __ push(v0); // Save result on the stack
1616 result_saved = true;
1617 }
1618
1619 __ lw(a0, MemOperand(sp)); // Duplicate receiver.
1620 __ push(a0);
1621
1622 if (property->kind() == ObjectLiteral::Property::PROTOTYPE) {
1623 DCHECK(!property->is_computed_name());
1624 VisitForStackValue(value);
1625 DCHECK(property->emit_store());
1626 __ CallRuntime(Runtime::kInternalSetPrototype);
1627 PrepareForBailoutForId(expr->GetIdForPropertySet(property_index),
1628 NO_REGISTERS);
1629 } else {
1630 EmitPropertyKey(property, expr->GetIdForPropertyName(property_index));
1631 VisitForStackValue(value);
1632 if (NeedsHomeObject(value)) {
1633 EmitSetHomeObject(value, 2, property->GetSlot());
1634 }
1635
1636 switch (property->kind()) {
1637 case ObjectLiteral::Property::CONSTANT:
1638 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1639 case ObjectLiteral::Property::COMPUTED:
1640 if (property->emit_store()) {
1641 __ li(a0, Operand(Smi::FromInt(NONE)));
1642 __ push(a0);
1643 __ CallRuntime(Runtime::kDefineDataPropertyUnchecked);
1644 } else {
1645 __ Drop(3);
1646 }
1647 break;
1648
1649 case ObjectLiteral::Property::PROTOTYPE:
1650 UNREACHABLE();
1651 break;
1652
1653 case ObjectLiteral::Property::GETTER:
1654 __ li(a0, Operand(Smi::FromInt(NONE)));
1655 __ push(a0);
1656 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked);
1657 break;
1658
1659 case ObjectLiteral::Property::SETTER:
1660 __ li(a0, Operand(Smi::FromInt(NONE)));
1661 __ push(a0);
1662 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked);
1663 break;
1664 }
1665 }
1666 }
1667
1668 if (expr->has_function()) {
1669 DCHECK(result_saved);
1670 __ lw(a0, MemOperand(sp));
1671 __ push(a0);
1672 __ CallRuntime(Runtime::kToFastProperties);
1673 }
1674
1675 if (result_saved) {
1676 context()->PlugTOS();
1677 } else {
1678 context()->Plug(v0);
1679 }
1680}
1681
1682
1683void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1684 Comment cmnt(masm_, "[ ArrayLiteral");
1685
1686 Handle<FixedArray> constant_elements = expr->constant_elements();
1687 bool has_fast_elements =
1688 IsFastObjectElementsKind(expr->constant_elements_kind());
1689
1690 AllocationSiteMode allocation_site_mode = TRACK_ALLOCATION_SITE;
1691 if (has_fast_elements && !FLAG_allocation_site_pretenuring) {
1692 // If the only customer of allocation sites is transitioning, then
1693 // we can turn it off if we don't have anywhere else to transition to.
1694 allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
1695 }
1696
1697 __ mov(a0, result_register());
1698 __ lw(a3, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1699 __ li(a2, Operand(Smi::FromInt(expr->literal_index())));
1700 __ li(a1, Operand(constant_elements));
1701 if (MustCreateArrayLiteralWithRuntime(expr)) {
1702 __ li(a0, Operand(Smi::FromInt(expr->ComputeFlags())));
1703 __ Push(a3, a2, a1, a0);
1704 __ CallRuntime(Runtime::kCreateArrayLiteral);
1705 } else {
1706 FastCloneShallowArrayStub stub(isolate(), allocation_site_mode);
1707 __ CallStub(&stub);
1708 }
1709 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1710
1711 bool result_saved = false; // Is the result saved to the stack?
1712 ZoneList<Expression*>* subexprs = expr->values();
1713 int length = subexprs->length();
1714
1715 // Emit code to evaluate all the non-constant subexpressions and to store
1716 // them into the newly cloned array.
1717 int array_index = 0;
1718 for (; array_index < length; array_index++) {
1719 Expression* subexpr = subexprs->at(array_index);
1720 if (subexpr->IsSpread()) break;
1721
1722 // If the subexpression is a literal or a simple materialized literal it
1723 // is already set in the cloned array.
1724 if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
1725
1726 if (!result_saved) {
1727 __ push(v0); // array literal
1728 result_saved = true;
1729 }
1730
1731 VisitForAccumulatorValue(subexpr);
1732
1733 __ li(StoreDescriptor::NameRegister(), Operand(Smi::FromInt(array_index)));
1734 __ lw(StoreDescriptor::ReceiverRegister(), MemOperand(sp, 0));
1735 __ mov(StoreDescriptor::ValueRegister(), result_register());
1736 EmitLoadStoreICSlot(expr->LiteralFeedbackSlot());
1737 Handle<Code> ic =
1738 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
1739 CallIC(ic);
1740
1741 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1742 }
1743
1744 // In case the array literal contains spread expressions it has two parts. The
1745 // first part is the "static" array which has a literal index is handled
1746 // above. The second part is the part after the first spread expression
1747 // (inclusive) and these elements gets appended to the array. Note that the
1748 // number elements an iterable produces is unknown ahead of time.
1749 if (array_index < length && result_saved) {
1750 __ Pop(v0);
1751 result_saved = false;
1752 }
1753 for (; array_index < length; array_index++) {
1754 Expression* subexpr = subexprs->at(array_index);
1755
1756 __ Push(v0);
1757 if (subexpr->IsSpread()) {
1758 VisitForStackValue(subexpr->AsSpread()->expression());
1759 __ InvokeBuiltin(Context::CONCAT_ITERABLE_TO_ARRAY_BUILTIN_INDEX,
1760 CALL_FUNCTION);
1761 } else {
1762 VisitForStackValue(subexpr);
1763 __ CallRuntime(Runtime::kAppendElement);
1764 }
1765
1766 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1767 }
1768
1769 if (result_saved) {
1770 context()->PlugTOS();
1771 } else {
1772 context()->Plug(v0);
1773 }
1774}
1775
1776
1777void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1778 DCHECK(expr->target()->IsValidReferenceExpressionOrThis());
1779
1780 Comment cmnt(masm_, "[ Assignment");
1781 SetExpressionPosition(expr, INSERT_BREAK);
1782
1783 Property* property = expr->target()->AsProperty();
1784 LhsKind assign_type = Property::GetAssignType(property);
1785
1786 // Evaluate LHS expression.
1787 switch (assign_type) {
1788 case VARIABLE:
1789 // Nothing to do here.
1790 break;
1791 case NAMED_PROPERTY:
1792 if (expr->is_compound()) {
1793 // We need the receiver both on the stack and in the register.
1794 VisitForStackValue(property->obj());
1795 __ lw(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
1796 } else {
1797 VisitForStackValue(property->obj());
1798 }
1799 break;
1800 case NAMED_SUPER_PROPERTY:
1801 VisitForStackValue(
1802 property->obj()->AsSuperPropertyReference()->this_var());
1803 VisitForAccumulatorValue(
1804 property->obj()->AsSuperPropertyReference()->home_object());
1805 __ Push(result_register());
1806 if (expr->is_compound()) {
1807 const Register scratch = a1;
1808 __ lw(scratch, MemOperand(sp, kPointerSize));
1809 __ Push(scratch, result_register());
1810 }
1811 break;
1812 case KEYED_SUPER_PROPERTY: {
1813 const Register scratch = a1;
1814 VisitForStackValue(
1815 property->obj()->AsSuperPropertyReference()->this_var());
1816 VisitForAccumulatorValue(
1817 property->obj()->AsSuperPropertyReference()->home_object());
1818 __ Move(scratch, result_register());
1819 VisitForAccumulatorValue(property->key());
1820 __ Push(scratch, result_register());
1821 if (expr->is_compound()) {
1822 const Register scratch1 = t0;
1823 __ lw(scratch1, MemOperand(sp, 2 * kPointerSize));
1824 __ Push(scratch1, scratch, result_register());
1825 }
1826 break;
1827 }
1828 case KEYED_PROPERTY:
1829 // We need the key and receiver on both the stack and in v0 and a1.
1830 if (expr->is_compound()) {
1831 VisitForStackValue(property->obj());
1832 VisitForStackValue(property->key());
1833 __ lw(LoadDescriptor::ReceiverRegister(),
1834 MemOperand(sp, 1 * kPointerSize));
1835 __ lw(LoadDescriptor::NameRegister(), MemOperand(sp, 0));
1836 } else {
1837 VisitForStackValue(property->obj());
1838 VisitForStackValue(property->key());
1839 }
1840 break;
1841 }
1842
1843 // For compound assignments we need another deoptimization point after the
1844 // variable/property load.
1845 if (expr->is_compound()) {
1846 { AccumulatorValueContext context(this);
1847 switch (assign_type) {
1848 case VARIABLE:
1849 EmitVariableLoad(expr->target()->AsVariableProxy());
1850 PrepareForBailout(expr->target(), TOS_REG);
1851 break;
1852 case NAMED_PROPERTY:
1853 EmitNamedPropertyLoad(property);
1854 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1855 break;
1856 case NAMED_SUPER_PROPERTY:
1857 EmitNamedSuperPropertyLoad(property);
1858 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1859 break;
1860 case KEYED_SUPER_PROPERTY:
1861 EmitKeyedSuperPropertyLoad(property);
1862 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1863 break;
1864 case KEYED_PROPERTY:
1865 EmitKeyedPropertyLoad(property);
1866 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1867 break;
1868 }
1869 }
1870
1871 Token::Value op = expr->binary_op();
1872 __ push(v0); // Left operand goes on the stack.
1873 VisitForAccumulatorValue(expr->value());
1874
1875 AccumulatorValueContext context(this);
1876 if (ShouldInlineSmiCase(op)) {
1877 EmitInlineSmiBinaryOp(expr->binary_operation(),
1878 op,
1879 expr->target(),
1880 expr->value());
1881 } else {
1882 EmitBinaryOp(expr->binary_operation(), op);
1883 }
1884
1885 // Deoptimization point in case the binary operation may have side effects.
1886 PrepareForBailout(expr->binary_operation(), TOS_REG);
1887 } else {
1888 VisitForAccumulatorValue(expr->value());
1889 }
1890
1891 SetExpressionPosition(expr);
1892
1893 // Store the value.
1894 switch (assign_type) {
1895 case VARIABLE:
1896 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1897 expr->op(), expr->AssignmentSlot());
1898 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1899 context()->Plug(v0);
1900 break;
1901 case NAMED_PROPERTY:
1902 EmitNamedPropertyAssignment(expr);
1903 break;
1904 case NAMED_SUPER_PROPERTY:
1905 EmitNamedSuperPropertyStore(property);
1906 context()->Plug(v0);
1907 break;
1908 case KEYED_SUPER_PROPERTY:
1909 EmitKeyedSuperPropertyStore(property);
1910 context()->Plug(v0);
1911 break;
1912 case KEYED_PROPERTY:
1913 EmitKeyedPropertyAssignment(expr);
1914 break;
1915 }
1916}
1917
1918
1919void FullCodeGenerator::VisitYield(Yield* expr) {
1920 Comment cmnt(masm_, "[ Yield");
1921 SetExpressionPosition(expr);
1922
1923 // Evaluate yielded value first; the initial iterator definition depends on
1924 // this. It stays on the stack while we update the iterator.
1925 VisitForStackValue(expr->expression());
1926
1927 switch (expr->yield_kind()) {
1928 case Yield::kSuspend:
1929 // Pop value from top-of-stack slot; box result into result register.
1930 EmitCreateIteratorResult(false);
1931 __ push(result_register());
1932 // Fall through.
1933 case Yield::kInitial: {
1934 Label suspend, continuation, post_runtime, resume;
1935
1936 __ jmp(&suspend);
1937 __ bind(&continuation);
1938 __ RecordGeneratorContinuation();
1939 __ jmp(&resume);
1940
1941 __ bind(&suspend);
1942 VisitForAccumulatorValue(expr->generator_object());
1943 DCHECK(continuation.pos() > 0 && Smi::IsValid(continuation.pos()));
1944 __ li(a1, Operand(Smi::FromInt(continuation.pos())));
1945 __ sw(a1, FieldMemOperand(v0, JSGeneratorObject::kContinuationOffset));
1946 __ sw(cp, FieldMemOperand(v0, JSGeneratorObject::kContextOffset));
1947 __ mov(a1, cp);
1948 __ RecordWriteField(v0, JSGeneratorObject::kContextOffset, a1, a2,
1949 kRAHasBeenSaved, kDontSaveFPRegs);
1950 __ Addu(a1, fp, Operand(StandardFrameConstants::kExpressionsOffset));
1951 __ Branch(&post_runtime, eq, sp, Operand(a1));
1952 __ push(v0); // generator object
1953 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
1954 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1955 __ bind(&post_runtime);
1956 __ pop(result_register());
1957 EmitReturnSequence();
1958
1959 __ bind(&resume);
1960 context()->Plug(result_register());
1961 break;
1962 }
1963
1964 case Yield::kFinal: {
1965 VisitForAccumulatorValue(expr->generator_object());
1966 __ li(a1, Operand(Smi::FromInt(JSGeneratorObject::kGeneratorClosed)));
1967 __ sw(a1, FieldMemOperand(result_register(),
1968 JSGeneratorObject::kContinuationOffset));
1969 // Pop value from top-of-stack slot, box result into result register.
1970 EmitCreateIteratorResult(true);
1971 EmitUnwindBeforeReturn();
1972 EmitReturnSequence();
1973 break;
1974 }
1975
1976 case Yield::kDelegating: {
1977 VisitForStackValue(expr->generator_object());
1978
1979 // Initial stack layout is as follows:
1980 // [sp + 1 * kPointerSize] iter
1981 // [sp + 0 * kPointerSize] g
1982
1983 Label l_catch, l_try, l_suspend, l_continuation, l_resume;
1984 Label l_next, l_call;
1985 Register load_receiver = LoadDescriptor::ReceiverRegister();
1986 Register load_name = LoadDescriptor::NameRegister();
1987
1988 // Initial send value is undefined.
1989 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex);
1990 __ Branch(&l_next);
1991
1992 // catch (e) { receiver = iter; f = 'throw'; arg = e; goto l_call; }
1993 __ bind(&l_catch);
1994 __ mov(a0, v0);
1995 __ LoadRoot(load_name, Heap::kthrow_stringRootIndex); // "throw"
1996 __ lw(a3, MemOperand(sp, 1 * kPointerSize)); // iter
1997 __ Push(load_name, a3, a0); // "throw", iter, except
1998 __ jmp(&l_call);
1999
2000 // try { received = %yield result }
2001 // Shuffle the received result above a try handler and yield it without
2002 // re-boxing.
2003 __ bind(&l_try);
2004 __ pop(a0); // result
2005 int handler_index = NewHandlerTableEntry();
2006 EnterTryBlock(handler_index, &l_catch);
2007 const int try_block_size = TryCatch::kElementCount * kPointerSize;
2008 __ push(a0); // result
2009
2010 __ jmp(&l_suspend);
2011 __ bind(&l_continuation);
2012 __ RecordGeneratorContinuation();
2013 __ mov(a0, v0);
2014 __ jmp(&l_resume);
2015
2016 __ bind(&l_suspend);
2017 const int generator_object_depth = kPointerSize + try_block_size;
2018 __ lw(a0, MemOperand(sp, generator_object_depth));
2019 __ push(a0); // g
2020 __ Push(Smi::FromInt(handler_index)); // handler-index
2021 DCHECK(l_continuation.pos() > 0 && Smi::IsValid(l_continuation.pos()));
2022 __ li(a1, Operand(Smi::FromInt(l_continuation.pos())));
2023 __ sw(a1, FieldMemOperand(a0, JSGeneratorObject::kContinuationOffset));
2024 __ sw(cp, FieldMemOperand(a0, JSGeneratorObject::kContextOffset));
2025 __ mov(a1, cp);
2026 __ RecordWriteField(a0, JSGeneratorObject::kContextOffset, a1, a2,
2027 kRAHasBeenSaved, kDontSaveFPRegs);
2028 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 2);
2029 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2030 __ pop(v0); // result
2031 EmitReturnSequence();
2032 __ mov(a0, v0);
2033 __ bind(&l_resume); // received in a0
2034 ExitTryBlock(handler_index);
2035
2036 // receiver = iter; f = 'next'; arg = received;
2037 __ bind(&l_next);
2038
2039 __ LoadRoot(load_name, Heap::knext_stringRootIndex); // "next"
2040 __ lw(a3, MemOperand(sp, 1 * kPointerSize)); // iter
2041 __ Push(load_name, a3, a0); // "next", iter, received
2042
2043 // result = receiver[f](arg);
2044 __ bind(&l_call);
2045 __ lw(load_receiver, MemOperand(sp, kPointerSize));
2046 __ lw(load_name, MemOperand(sp, 2 * kPointerSize));
2047 __ li(LoadDescriptor::SlotRegister(),
2048 Operand(SmiFromSlot(expr->KeyedLoadFeedbackSlot())));
2049 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), SLOPPY).code();
2050 CallIC(ic, TypeFeedbackId::None());
2051 __ mov(a0, v0);
2052 __ mov(a1, a0);
2053 __ sw(a1, MemOperand(sp, 2 * kPointerSize));
2054 SetCallPosition(expr);
2055 __ li(a0, Operand(1));
2056 __ Call(
2057 isolate()->builtins()->Call(ConvertReceiverMode::kNotNullOrUndefined),
2058 RelocInfo::CODE_TARGET);
2059
2060 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2061 __ Drop(1); // The function is still on the stack; drop it.
2062
2063 // if (!result.done) goto l_try;
2064 __ Move(load_receiver, v0);
2065
2066 __ push(load_receiver); // save result
2067 __ LoadRoot(load_name, Heap::kdone_stringRootIndex); // "done"
2068 __ li(LoadDescriptor::SlotRegister(),
2069 Operand(SmiFromSlot(expr->DoneFeedbackSlot())));
2070 CallLoadIC(NOT_INSIDE_TYPEOF); // v0=result.done
2071 __ mov(a0, v0);
2072 Handle<Code> bool_ic = ToBooleanStub::GetUninitialized(isolate());
2073 CallIC(bool_ic);
2074 __ LoadRoot(at, Heap::kTrueValueRootIndex);
2075 __ Branch(&l_try, ne, result_register(), Operand(at));
2076
2077 // result.value
2078 __ pop(load_receiver); // result
2079 __ LoadRoot(load_name, Heap::kvalue_stringRootIndex); // "value"
2080 __ li(LoadDescriptor::SlotRegister(),
2081 Operand(SmiFromSlot(expr->ValueFeedbackSlot())));
2082 CallLoadIC(NOT_INSIDE_TYPEOF); // v0=result.value
2083 context()->DropAndPlug(2, v0); // drop iter and g
2084 break;
2085 }
2086 }
2087}
2088
2089
2090void FullCodeGenerator::EmitGeneratorResume(Expression *generator,
2091 Expression *value,
2092 JSGeneratorObject::ResumeMode resume_mode) {
2093 // The value stays in a0, and is ultimately read by the resumed generator, as
2094 // if CallRuntime(Runtime::kSuspendJSGeneratorObject) returned it. Or it
2095 // is read to throw the value when the resumed generator is already closed.
2096 // a1 will hold the generator object until the activation has been resumed.
2097 VisitForStackValue(generator);
2098 VisitForAccumulatorValue(value);
2099 __ pop(a1);
2100
2101 // Load suspended function and context.
2102 __ lw(cp, FieldMemOperand(a1, JSGeneratorObject::kContextOffset));
2103 __ lw(t0, FieldMemOperand(a1, JSGeneratorObject::kFunctionOffset));
2104
2105 // Load receiver and store as the first argument.
2106 __ lw(a2, FieldMemOperand(a1, JSGeneratorObject::kReceiverOffset));
2107 __ push(a2);
2108
2109 // Push holes for the rest of the arguments to the generator function.
2110 __ lw(a3, FieldMemOperand(t0, JSFunction::kSharedFunctionInfoOffset));
2111 __ lw(a3,
2112 FieldMemOperand(a3, SharedFunctionInfo::kFormalParameterCountOffset));
2113 __ LoadRoot(a2, Heap::kTheHoleValueRootIndex);
2114 Label push_argument_holes, push_frame;
2115 __ bind(&push_argument_holes);
2116 __ Subu(a3, a3, Operand(Smi::FromInt(1)));
2117 __ Branch(&push_frame, lt, a3, Operand(zero_reg));
2118 __ push(a2);
2119 __ jmp(&push_argument_holes);
2120
2121 // Enter a new JavaScript frame, and initialize its slots as they were when
2122 // the generator was suspended.
2123 Label resume_frame, done;
2124 __ bind(&push_frame);
2125 __ Call(&resume_frame);
2126 __ jmp(&done);
2127 __ bind(&resume_frame);
2128 // ra = return address.
2129 // fp = caller's frame pointer.
2130 // cp = callee's context,
2131 // t0 = callee's JS function.
2132 __ Push(ra, fp, cp, t0);
2133 // Adjust FP to point to saved FP.
2134 __ Addu(fp, sp, 2 * kPointerSize);
2135
2136 // Load the operand stack size.
2137 __ lw(a3, FieldMemOperand(a1, JSGeneratorObject::kOperandStackOffset));
2138 __ lw(a3, FieldMemOperand(a3, FixedArray::kLengthOffset));
2139 __ SmiUntag(a3);
2140
2141 // If we are sending a value and there is no operand stack, we can jump back
2142 // in directly.
2143 if (resume_mode == JSGeneratorObject::NEXT) {
2144 Label slow_resume;
2145 __ Branch(&slow_resume, ne, a3, Operand(zero_reg));
2146 __ lw(a3, FieldMemOperand(t0, JSFunction::kCodeEntryOffset));
2147 __ lw(a2, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
2148 __ SmiUntag(a2);
2149 __ Addu(a3, a3, Operand(a2));
2150 __ li(a2, Operand(Smi::FromInt(JSGeneratorObject::kGeneratorExecuting)));
2151 __ sw(a2, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
2152 __ Jump(a3);
2153 __ bind(&slow_resume);
2154 }
2155
2156 // Otherwise, we push holes for the operand stack and call the runtime to fix
2157 // up the stack and the handlers.
2158 Label push_operand_holes, call_resume;
2159 __ bind(&push_operand_holes);
2160 __ Subu(a3, a3, Operand(1));
2161 __ Branch(&call_resume, lt, a3, Operand(zero_reg));
2162 __ push(a2);
2163 __ Branch(&push_operand_holes);
2164 __ bind(&call_resume);
2165 DCHECK(!result_register().is(a1));
2166 __ Push(a1, result_register());
2167 __ Push(Smi::FromInt(resume_mode));
2168 __ CallRuntime(Runtime::kResumeJSGeneratorObject);
2169 // Not reached: the runtime call returns elsewhere.
2170 __ stop("not-reached");
2171
2172 __ bind(&done);
2173 context()->Plug(result_register());
2174}
2175
2176
2177void FullCodeGenerator::EmitCreateIteratorResult(bool done) {
2178 Label allocate, done_allocate;
2179
2180 __ Allocate(JSIteratorResult::kSize, v0, a2, a3, &allocate, TAG_OBJECT);
2181 __ jmp(&done_allocate);
2182
2183 __ bind(&allocate);
2184 __ Push(Smi::FromInt(JSIteratorResult::kSize));
2185 __ CallRuntime(Runtime::kAllocateInNewSpace);
2186
2187 __ bind(&done_allocate);
2188 __ LoadNativeContextSlot(Context::ITERATOR_RESULT_MAP_INDEX, a1);
2189 __ pop(a2);
2190 __ LoadRoot(a3,
2191 done ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex);
2192 __ LoadRoot(t0, Heap::kEmptyFixedArrayRootIndex);
2193 __ sw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
2194 __ sw(t0, FieldMemOperand(v0, JSObject::kPropertiesOffset));
2195 __ sw(t0, FieldMemOperand(v0, JSObject::kElementsOffset));
2196 __ sw(a2, FieldMemOperand(v0, JSIteratorResult::kValueOffset));
2197 __ sw(a3, FieldMemOperand(v0, JSIteratorResult::kDoneOffset));
2198 STATIC_ASSERT(JSIteratorResult::kSize == 5 * kPointerSize);
2199}
2200
2201
2202void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
2203 SetExpressionPosition(prop);
2204 Literal* key = prop->key()->AsLiteral();
2205 DCHECK(!prop->IsSuperAccess());
2206
2207 __ li(LoadDescriptor::NameRegister(), Operand(key->value()));
2208 __ li(LoadDescriptor::SlotRegister(),
2209 Operand(SmiFromSlot(prop->PropertyFeedbackSlot())));
2210 CallLoadIC(NOT_INSIDE_TYPEOF, language_mode());
2211}
2212
2213
2214void FullCodeGenerator::EmitNamedSuperPropertyLoad(Property* prop) {
2215 // Stack: receiver, home_object.
2216 SetExpressionPosition(prop);
2217
2218 Literal* key = prop->key()->AsLiteral();
2219 DCHECK(!key->value()->IsSmi());
2220 DCHECK(prop->IsSuperAccess());
2221
2222 __ Push(key->value());
2223 __ Push(Smi::FromInt(language_mode()));
2224 __ CallRuntime(Runtime::kLoadFromSuper);
2225}
2226
2227
2228void FullCodeGenerator::EmitKeyedPropertyLoad(Property* prop) {
2229 SetExpressionPosition(prop);
2230 Handle<Code> ic = CodeFactory::KeyedLoadIC(isolate(), language_mode()).code();
2231 __ li(LoadDescriptor::SlotRegister(),
2232 Operand(SmiFromSlot(prop->PropertyFeedbackSlot())));
2233 CallIC(ic);
2234}
2235
2236
2237void FullCodeGenerator::EmitKeyedSuperPropertyLoad(Property* prop) {
2238 // Stack: receiver, home_object, key.
2239 SetExpressionPosition(prop);
2240 __ Push(Smi::FromInt(language_mode()));
2241 __ CallRuntime(Runtime::kLoadKeyedFromSuper);
2242}
2243
2244
2245void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
2246 Token::Value op,
2247 Expression* left_expr,
2248 Expression* right_expr) {
2249 Label done, smi_case, stub_call;
2250
2251 Register scratch1 = a2;
2252 Register scratch2 = a3;
2253
2254 // Get the arguments.
2255 Register left = a1;
2256 Register right = a0;
2257 __ pop(left);
2258 __ mov(a0, result_register());
2259
2260 // Perform combined smi check on both operands.
2261 __ Or(scratch1, left, Operand(right));
2262 STATIC_ASSERT(kSmiTag == 0);
2263 JumpPatchSite patch_site(masm_);
2264 patch_site.EmitJumpIfSmi(scratch1, &smi_case);
2265
2266 __ bind(&stub_call);
2267 Handle<Code> code =
2268 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2269 CallIC(code, expr->BinaryOperationFeedbackId());
2270 patch_site.EmitPatchInfo();
2271 __ jmp(&done);
2272
2273 __ bind(&smi_case);
2274 // Smi case. This code works the same way as the smi-smi case in the type
2275 // recording binary operation stub, see
2276 switch (op) {
2277 case Token::SAR:
2278 __ GetLeastBitsFromSmi(scratch1, right, 5);
2279 __ srav(right, left, scratch1);
2280 __ And(v0, right, Operand(~kSmiTagMask));
2281 break;
2282 case Token::SHL: {
2283 __ SmiUntag(scratch1, left);
2284 __ GetLeastBitsFromSmi(scratch2, right, 5);
2285 __ sllv(scratch1, scratch1, scratch2);
2286 __ Addu(scratch2, scratch1, Operand(0x40000000));
2287 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
2288 __ SmiTag(v0, scratch1);
2289 break;
2290 }
2291 case Token::SHR: {
2292 __ SmiUntag(scratch1, left);
2293 __ GetLeastBitsFromSmi(scratch2, right, 5);
2294 __ srlv(scratch1, scratch1, scratch2);
2295 __ And(scratch2, scratch1, 0xc0000000);
2296 __ Branch(&stub_call, ne, scratch2, Operand(zero_reg));
2297 __ SmiTag(v0, scratch1);
2298 break;
2299 }
2300 case Token::ADD:
2301 __ AddBranchOvf(v0, left, Operand(right), &stub_call);
2302 break;
2303 case Token::SUB:
2304 __ SubBranchOvf(v0, left, Operand(right), &stub_call);
2305 break;
2306 case Token::MUL: {
2307 __ SmiUntag(scratch1, right);
2308 __ Mul(scratch2, v0, left, scratch1);
2309 __ sra(scratch1, v0, 31);
2310 __ Branch(&stub_call, ne, scratch1, Operand(scratch2));
2311 __ Branch(&done, ne, v0, Operand(zero_reg));
2312 __ Addu(scratch2, right, left);
2313 __ Branch(&stub_call, lt, scratch2, Operand(zero_reg));
2314 DCHECK(Smi::FromInt(0) == 0);
2315 __ mov(v0, zero_reg);
2316 break;
2317 }
2318 case Token::BIT_OR:
2319 __ Or(v0, left, Operand(right));
2320 break;
2321 case Token::BIT_AND:
2322 __ And(v0, left, Operand(right));
2323 break;
2324 case Token::BIT_XOR:
2325 __ Xor(v0, left, Operand(right));
2326 break;
2327 default:
2328 UNREACHABLE();
2329 }
2330
2331 __ bind(&done);
2332 context()->Plug(v0);
2333}
2334
2335
2336void FullCodeGenerator::EmitClassDefineProperties(ClassLiteral* lit) {
2337 // Constructor is in v0.
2338 DCHECK(lit != NULL);
2339 __ push(v0);
2340
2341 // No access check is needed here since the constructor is created by the
2342 // class literal.
2343 Register scratch = a1;
2344 __ lw(scratch,
2345 FieldMemOperand(v0, JSFunction::kPrototypeOrInitialMapOffset));
2346 __ push(scratch);
2347
2348 for (int i = 0; i < lit->properties()->length(); i++) {
2349 ObjectLiteral::Property* property = lit->properties()->at(i);
2350 Expression* value = property->value();
2351
2352 if (property->is_static()) {
2353 __ lw(scratch, MemOperand(sp, kPointerSize)); // constructor
2354 } else {
2355 __ lw(scratch, MemOperand(sp, 0)); // prototype
2356 }
2357 __ push(scratch);
2358 EmitPropertyKey(property, lit->GetIdForProperty(i));
2359
2360 // The static prototype property is read only. We handle the non computed
2361 // property name case in the parser. Since this is the only case where we
2362 // need to check for an own read only property we special case this so we do
2363 // not need to do this for every property.
2364 if (property->is_static() && property->is_computed_name()) {
2365 __ CallRuntime(Runtime::kThrowIfStaticPrototype);
2366 __ push(v0);
2367 }
2368
2369 VisitForStackValue(value);
2370 if (NeedsHomeObject(value)) {
2371 EmitSetHomeObject(value, 2, property->GetSlot());
2372 }
2373
2374 switch (property->kind()) {
2375 case ObjectLiteral::Property::CONSTANT:
2376 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2377 case ObjectLiteral::Property::PROTOTYPE:
2378 UNREACHABLE();
2379 case ObjectLiteral::Property::COMPUTED:
2380 __ CallRuntime(Runtime::kDefineClassMethod);
2381 break;
2382
2383 case ObjectLiteral::Property::GETTER:
2384 __ li(a0, Operand(Smi::FromInt(DONT_ENUM)));
2385 __ push(a0);
2386 __ CallRuntime(Runtime::kDefineGetterPropertyUnchecked);
2387 break;
2388
2389 case ObjectLiteral::Property::SETTER:
2390 __ li(a0, Operand(Smi::FromInt(DONT_ENUM)));
2391 __ push(a0);
2392 __ CallRuntime(Runtime::kDefineSetterPropertyUnchecked);
2393 break;
2394
2395 default:
2396 UNREACHABLE();
2397 }
2398 }
2399
2400 // Set both the prototype and constructor to have fast properties, and also
2401 // freeze them in strong mode.
2402 __ CallRuntime(Runtime::kFinalizeClassDefinition);
2403}
2404
2405
2406void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr, Token::Value op) {
2407 __ mov(a0, result_register());
2408 __ pop(a1);
2409 Handle<Code> code =
2410 CodeFactory::BinaryOpIC(isolate(), op, strength(language_mode())).code();
2411 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
2412 CallIC(code, expr->BinaryOperationFeedbackId());
2413 patch_site.EmitPatchInfo();
2414 context()->Plug(v0);
2415}
2416
2417
2418void FullCodeGenerator::EmitAssignment(Expression* expr,
2419 FeedbackVectorSlot slot) {
2420 DCHECK(expr->IsValidReferenceExpressionOrThis());
2421
2422 Property* prop = expr->AsProperty();
2423 LhsKind assign_type = Property::GetAssignType(prop);
2424
2425 switch (assign_type) {
2426 case VARIABLE: {
2427 Variable* var = expr->AsVariableProxy()->var();
2428 EffectContext context(this);
2429 EmitVariableAssignment(var, Token::ASSIGN, slot);
2430 break;
2431 }
2432 case NAMED_PROPERTY: {
2433 __ push(result_register()); // Preserve value.
2434 VisitForAccumulatorValue(prop->obj());
2435 __ mov(StoreDescriptor::ReceiverRegister(), result_register());
2436 __ pop(StoreDescriptor::ValueRegister()); // Restore value.
2437 __ li(StoreDescriptor::NameRegister(),
2438 Operand(prop->key()->AsLiteral()->value()));
2439 EmitLoadStoreICSlot(slot);
2440 CallStoreIC();
2441 break;
2442 }
2443 case NAMED_SUPER_PROPERTY: {
2444 __ Push(v0);
2445 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2446 VisitForAccumulatorValue(
2447 prop->obj()->AsSuperPropertyReference()->home_object());
2448 // stack: value, this; v0: home_object
2449 Register scratch = a2;
2450 Register scratch2 = a3;
2451 __ mov(scratch, result_register()); // home_object
2452 __ lw(v0, MemOperand(sp, kPointerSize)); // value
2453 __ lw(scratch2, MemOperand(sp, 0)); // this
2454 __ sw(scratch2, MemOperand(sp, kPointerSize)); // this
2455 __ sw(scratch, MemOperand(sp, 0)); // home_object
2456 // stack: this, home_object; v0: value
2457 EmitNamedSuperPropertyStore(prop);
2458 break;
2459 }
2460 case KEYED_SUPER_PROPERTY: {
2461 __ Push(v0);
2462 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2463 VisitForStackValue(
2464 prop->obj()->AsSuperPropertyReference()->home_object());
2465 VisitForAccumulatorValue(prop->key());
2466 Register scratch = a2;
2467 Register scratch2 = a3;
2468 __ lw(scratch2, MemOperand(sp, 2 * kPointerSize)); // value
2469 // stack: value, this, home_object; v0: key, a3: value
2470 __ lw(scratch, MemOperand(sp, kPointerSize)); // this
2471 __ sw(scratch, MemOperand(sp, 2 * kPointerSize));
2472 __ lw(scratch, MemOperand(sp, 0)); // home_object
2473 __ sw(scratch, MemOperand(sp, kPointerSize));
2474 __ sw(v0, MemOperand(sp, 0));
2475 __ Move(v0, scratch2);
2476 // stack: this, home_object, key; v0: value.
2477 EmitKeyedSuperPropertyStore(prop);
2478 break;
2479 }
2480 case KEYED_PROPERTY: {
2481 __ push(result_register()); // Preserve value.
2482 VisitForStackValue(prop->obj());
2483 VisitForAccumulatorValue(prop->key());
2484 __ mov(StoreDescriptor::NameRegister(), result_register());
2485 __ Pop(StoreDescriptor::ValueRegister(),
2486 StoreDescriptor::ReceiverRegister());
2487 EmitLoadStoreICSlot(slot);
2488 Handle<Code> ic =
2489 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2490 CallIC(ic);
2491 break;
2492 }
2493 }
2494 context()->Plug(v0);
2495}
2496
2497
2498void FullCodeGenerator::EmitStoreToStackLocalOrContextSlot(
2499 Variable* var, MemOperand location) {
2500 __ sw(result_register(), location);
2501 if (var->IsContextSlot()) {
2502 // RecordWrite may destroy all its register arguments.
2503 __ Move(a3, result_register());
2504 int offset = Context::SlotOffset(var->index());
2505 __ RecordWriteContextSlot(
2506 a1, offset, a3, a2, kRAHasBeenSaved, kDontSaveFPRegs);
2507 }
2508}
2509
2510
2511void FullCodeGenerator::EmitVariableAssignment(Variable* var, Token::Value op,
2512 FeedbackVectorSlot slot) {
2513 if (var->IsUnallocated()) {
2514 // Global var, const, or let.
2515 __ mov(StoreDescriptor::ValueRegister(), result_register());
2516 __ li(StoreDescriptor::NameRegister(), Operand(var->name()));
2517 __ LoadGlobalObject(StoreDescriptor::ReceiverRegister());
2518 EmitLoadStoreICSlot(slot);
2519 CallStoreIC();
2520
2521 } else if (var->mode() == LET && op != Token::INIT) {
2522 // Non-initializing assignment to let variable needs a write barrier.
2523 DCHECK(!var->IsLookupSlot());
2524 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2525 Label assign;
2526 MemOperand location = VarOperand(var, a1);
2527 __ lw(a3, location);
2528 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2529 __ Branch(&assign, ne, a3, Operand(t0));
2530 __ li(a3, Operand(var->name()));
2531 __ push(a3);
2532 __ CallRuntime(Runtime::kThrowReferenceError);
2533 // Perform the assignment.
2534 __ bind(&assign);
2535 EmitStoreToStackLocalOrContextSlot(var, location);
2536
2537 } else if (var->mode() == CONST && op != Token::INIT) {
2538 // Assignment to const variable needs a write barrier.
2539 DCHECK(!var->IsLookupSlot());
2540 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2541 Label const_error;
2542 MemOperand location = VarOperand(var, a1);
2543 __ lw(a3, location);
2544 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2545 __ Branch(&const_error, ne, a3, Operand(at));
2546 __ li(a3, Operand(var->name()));
2547 __ push(a3);
2548 __ CallRuntime(Runtime::kThrowReferenceError);
2549 __ bind(&const_error);
2550 __ CallRuntime(Runtime::kThrowConstAssignError);
2551
2552 } else if (var->is_this() && var->mode() == CONST && op == Token::INIT) {
2553 // Initializing assignment to const {this} needs a write barrier.
2554 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2555 Label uninitialized_this;
2556 MemOperand location = VarOperand(var, a1);
2557 __ lw(a3, location);
2558 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2559 __ Branch(&uninitialized_this, eq, a3, Operand(at));
2560 __ li(a0, Operand(var->name()));
2561 __ Push(a0);
2562 __ CallRuntime(Runtime::kThrowReferenceError);
2563 __ bind(&uninitialized_this);
2564 EmitStoreToStackLocalOrContextSlot(var, location);
2565
2566 } else if (!var->is_const_mode() ||
2567 (var->mode() == CONST && op == Token::INIT)) {
2568 if (var->IsLookupSlot()) {
2569 // Assignment to var.
2570 __ li(a1, Operand(var->name()));
2571 __ li(a0, Operand(Smi::FromInt(language_mode())));
2572 __ Push(v0, cp, a1, a0); // Value, context, name, language mode.
2573 __ CallRuntime(Runtime::kStoreLookupSlot);
2574 } else {
2575 // Assignment to var or initializing assignment to let/const in harmony
2576 // mode.
2577 DCHECK((var->IsStackAllocated() || var->IsContextSlot()));
2578 MemOperand location = VarOperand(var, a1);
2579 if (generate_debug_code_ && var->mode() == LET && op == Token::INIT) {
2580 // Check for an uninitialized let binding.
2581 __ lw(a2, location);
2582 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
2583 __ Check(eq, kLetBindingReInitialization, a2, Operand(t0));
2584 }
2585 EmitStoreToStackLocalOrContextSlot(var, location);
2586 }
2587
2588 } else if (var->mode() == CONST_LEGACY && op == Token::INIT) {
2589 // Const initializers need a write barrier.
2590 DCHECK(!var->IsParameter()); // No const parameters.
2591 if (var->IsLookupSlot()) {
2592 __ li(a0, Operand(var->name()));
2593 __ Push(v0, cp, a0); // Context and name.
2594 __ CallRuntime(Runtime::kInitializeLegacyConstLookupSlot);
2595 } else {
2596 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2597 Label skip;
2598 MemOperand location = VarOperand(var, a1);
2599 __ lw(a2, location);
2600 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
2601 __ Branch(&skip, ne, a2, Operand(at));
2602 EmitStoreToStackLocalOrContextSlot(var, location);
2603 __ bind(&skip);
2604 }
2605
2606 } else {
2607 DCHECK(var->mode() == CONST_LEGACY && op != Token::INIT);
2608 if (is_strict(language_mode())) {
2609 __ CallRuntime(Runtime::kThrowConstAssignError);
2610 }
2611 // Silently ignore store in sloppy mode.
2612 }
2613}
2614
2615
2616void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2617 // Assignment to a property, using a named store IC.
2618 Property* prop = expr->target()->AsProperty();
2619 DCHECK(prop != NULL);
2620 DCHECK(prop->key()->IsLiteral());
2621
2622 __ mov(StoreDescriptor::ValueRegister(), result_register());
2623 __ li(StoreDescriptor::NameRegister(),
2624 Operand(prop->key()->AsLiteral()->value()));
2625 __ pop(StoreDescriptor::ReceiverRegister());
2626 EmitLoadStoreICSlot(expr->AssignmentSlot());
2627 CallStoreIC();
2628
2629 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2630 context()->Plug(v0);
2631}
2632
2633
2634void FullCodeGenerator::EmitNamedSuperPropertyStore(Property* prop) {
2635 // Assignment to named property of super.
2636 // v0 : value
2637 // stack : receiver ('this'), home_object
2638 DCHECK(prop != NULL);
2639 Literal* key = prop->key()->AsLiteral();
2640 DCHECK(key != NULL);
2641
2642 __ Push(key->value());
2643 __ Push(v0);
2644 __ CallRuntime((is_strict(language_mode()) ? Runtime::kStoreToSuper_Strict
2645 : Runtime::kStoreToSuper_Sloppy));
2646}
2647
2648
2649void FullCodeGenerator::EmitKeyedSuperPropertyStore(Property* prop) {
2650 // Assignment to named property of super.
2651 // v0 : value
2652 // stack : receiver ('this'), home_object, key
2653 DCHECK(prop != NULL);
2654
2655 __ Push(v0);
2656 __ CallRuntime((is_strict(language_mode())
2657 ? Runtime::kStoreKeyedToSuper_Strict
2658 : Runtime::kStoreKeyedToSuper_Sloppy));
2659}
2660
2661
2662void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2663 // Assignment to a property, using a keyed store IC.
2664 // Call keyed store IC.
2665 // The arguments are:
2666 // - a0 is the value,
2667 // - a1 is the key,
2668 // - a2 is the receiver.
2669 __ mov(StoreDescriptor::ValueRegister(), result_register());
2670 __ Pop(StoreDescriptor::ReceiverRegister(), StoreDescriptor::NameRegister());
2671 DCHECK(StoreDescriptor::ValueRegister().is(a0));
2672
2673 Handle<Code> ic =
2674 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2675 EmitLoadStoreICSlot(expr->AssignmentSlot());
2676 CallIC(ic);
2677
2678 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2679 context()->Plug(v0);
2680}
2681
2682
2683void FullCodeGenerator::VisitProperty(Property* expr) {
2684 Comment cmnt(masm_, "[ Property");
2685 SetExpressionPosition(expr);
2686
2687 Expression* key = expr->key();
2688
2689 if (key->IsPropertyName()) {
2690 if (!expr->IsSuperAccess()) {
2691 VisitForAccumulatorValue(expr->obj());
2692 __ Move(LoadDescriptor::ReceiverRegister(), v0);
2693 EmitNamedPropertyLoad(expr);
2694 } else {
2695 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2696 VisitForStackValue(
2697 expr->obj()->AsSuperPropertyReference()->home_object());
2698 EmitNamedSuperPropertyLoad(expr);
2699 }
2700 } else {
2701 if (!expr->IsSuperAccess()) {
2702 VisitForStackValue(expr->obj());
2703 VisitForAccumulatorValue(expr->key());
2704 __ Move(LoadDescriptor::NameRegister(), v0);
2705 __ pop(LoadDescriptor::ReceiverRegister());
2706 EmitKeyedPropertyLoad(expr);
2707 } else {
2708 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2709 VisitForStackValue(
2710 expr->obj()->AsSuperPropertyReference()->home_object());
2711 VisitForStackValue(expr->key());
2712 EmitKeyedSuperPropertyLoad(expr);
2713 }
2714 }
2715 PrepareForBailoutForId(expr->LoadId(), TOS_REG);
2716 context()->Plug(v0);
2717}
2718
2719
2720void FullCodeGenerator::CallIC(Handle<Code> code,
2721 TypeFeedbackId id) {
2722 ic_total_count_++;
2723 __ Call(code, RelocInfo::CODE_TARGET, id);
2724}
2725
2726
2727// Code common for calls using the IC.
2728void FullCodeGenerator::EmitCallWithLoadIC(Call* expr) {
2729 Expression* callee = expr->expression();
2730
2731 // Get the target function.
2732 ConvertReceiverMode convert_mode;
2733 if (callee->IsVariableProxy()) {
2734 { StackValueContext context(this);
2735 EmitVariableLoad(callee->AsVariableProxy());
2736 PrepareForBailout(callee, NO_REGISTERS);
2737 }
2738 // Push undefined as receiver. This is patched in the method prologue if it
2739 // is a sloppy mode method.
2740 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
2741 __ push(at);
2742 convert_mode = ConvertReceiverMode::kNullOrUndefined;
2743 } else {
2744 // Load the function from the receiver.
2745 DCHECK(callee->IsProperty());
2746 DCHECK(!callee->AsProperty()->IsSuperAccess());
2747 __ lw(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
2748 EmitNamedPropertyLoad(callee->AsProperty());
2749 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2750 // Push the target function under the receiver.
2751 __ lw(at, MemOperand(sp, 0));
2752 __ push(at);
2753 __ sw(v0, MemOperand(sp, kPointerSize));
2754 convert_mode = ConvertReceiverMode::kNotNullOrUndefined;
2755 }
2756
2757 EmitCall(expr, convert_mode);
2758}
2759
2760
2761void FullCodeGenerator::EmitSuperCallWithLoadIC(Call* expr) {
2762 SetExpressionPosition(expr);
2763 Expression* callee = expr->expression();
2764 DCHECK(callee->IsProperty());
2765 Property* prop = callee->AsProperty();
2766 DCHECK(prop->IsSuperAccess());
2767
2768 Literal* key = prop->key()->AsLiteral();
2769 DCHECK(!key->value()->IsSmi());
2770 // Load the function from the receiver.
2771 const Register scratch = a1;
2772 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
2773 VisitForAccumulatorValue(super_ref->home_object());
2774 __ mov(scratch, v0);
2775 VisitForAccumulatorValue(super_ref->this_var());
2776 __ Push(scratch, v0, v0, scratch);
2777 __ Push(key->value());
2778 __ Push(Smi::FromInt(language_mode()));
2779
2780 // Stack here:
2781 // - home_object
2782 // - this (receiver)
2783 // - this (receiver) <-- LoadFromSuper will pop here and below.
2784 // - home_object
2785 // - key
2786 // - language_mode
2787 __ CallRuntime(Runtime::kLoadFromSuper);
2788
2789 // Replace home_object with target function.
2790 __ sw(v0, MemOperand(sp, kPointerSize));
2791
2792 // Stack here:
2793 // - target function
2794 // - this (receiver)
2795 EmitCall(expr);
2796}
2797
2798
2799// Code common for calls using the IC.
2800void FullCodeGenerator::EmitKeyedCallWithLoadIC(Call* expr,
2801 Expression* key) {
2802 // Load the key.
2803 VisitForAccumulatorValue(key);
2804
2805 Expression* callee = expr->expression();
2806
2807 // Load the function from the receiver.
2808 DCHECK(callee->IsProperty());
2809 __ lw(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
2810 __ Move(LoadDescriptor::NameRegister(), v0);
2811 EmitKeyedPropertyLoad(callee->AsProperty());
2812 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2813
2814 // Push the target function under the receiver.
2815 __ lw(at, MemOperand(sp, 0));
2816 __ push(at);
2817 __ sw(v0, MemOperand(sp, kPointerSize));
2818
2819 EmitCall(expr, ConvertReceiverMode::kNotNullOrUndefined);
2820}
2821
2822
2823void FullCodeGenerator::EmitKeyedSuperCallWithLoadIC(Call* expr) {
2824 Expression* callee = expr->expression();
2825 DCHECK(callee->IsProperty());
2826 Property* prop = callee->AsProperty();
2827 DCHECK(prop->IsSuperAccess());
2828
2829 SetExpressionPosition(prop);
2830 // Load the function from the receiver.
2831 const Register scratch = a1;
2832 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
2833 VisitForAccumulatorValue(super_ref->home_object());
2834 __ Move(scratch, v0);
2835 VisitForAccumulatorValue(super_ref->this_var());
2836 __ Push(scratch, v0, v0, scratch);
2837 VisitForStackValue(prop->key());
2838 __ Push(Smi::FromInt(language_mode()));
2839
2840 // Stack here:
2841 // - home_object
2842 // - this (receiver)
2843 // - this (receiver) <-- LoadKeyedFromSuper will pop here and below.
2844 // - home_object
2845 // - key
2846 // - language_mode
2847 __ CallRuntime(Runtime::kLoadKeyedFromSuper);
2848
2849 // Replace home_object with target function.
2850 __ sw(v0, MemOperand(sp, kPointerSize));
2851
2852 // Stack here:
2853 // - target function
2854 // - this (receiver)
2855 EmitCall(expr);
2856}
2857
2858
2859void FullCodeGenerator::EmitCall(Call* expr, ConvertReceiverMode mode) {
2860 // Load the arguments.
2861 ZoneList<Expression*>* args = expr->arguments();
2862 int arg_count = args->length();
2863 for (int i = 0; i < arg_count; i++) {
2864 VisitForStackValue(args->at(i));
2865 }
2866
2867 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
2868 // Record source position of the IC call.
2869 SetCallPosition(expr);
2870 Handle<Code> ic = CodeFactory::CallIC(isolate(), arg_count, mode).code();
2871 __ li(a3, Operand(SmiFromSlot(expr->CallFeedbackICSlot())));
2872 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2873 // Don't assign a type feedback id to the IC, since type feedback is provided
2874 // by the vector above.
2875 CallIC(ic);
2876
2877 RecordJSReturnSite(expr);
2878 // Restore context register.
2879 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2880 context()->DropAndPlug(1, v0);
2881}
2882
2883
2884void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
2885 // t3: copy of the first argument or undefined if it doesn't exist.
2886 if (arg_count > 0) {
2887 __ lw(t3, MemOperand(sp, arg_count * kPointerSize));
2888 } else {
2889 __ LoadRoot(t3, Heap::kUndefinedValueRootIndex);
2890 }
2891
2892 // t2: the receiver of the enclosing function.
2893 __ lw(t2, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
2894
2895 // t1: the language mode.
2896 __ li(t1, Operand(Smi::FromInt(language_mode())));
2897
2898 // t0: the start position of the scope the calls resides in.
2899 __ li(t0, Operand(Smi::FromInt(scope()->start_position())));
2900
2901 // Do the runtime call.
2902 __ Push(t3, t2, t1, t0);
2903 __ CallRuntime(Runtime::kResolvePossiblyDirectEval);
2904}
2905
2906
2907// See http://www.ecma-international.org/ecma-262/6.0/#sec-function-calls.
2908void FullCodeGenerator::PushCalleeAndWithBaseObject(Call* expr) {
2909 VariableProxy* callee = expr->expression()->AsVariableProxy();
2910 if (callee->var()->IsLookupSlot()) {
2911 Label slow, done;
2912
2913 SetExpressionPosition(callee);
2914 // Generate code for loading from variables potentially shadowed by
2915 // eval-introduced variables.
2916 EmitDynamicLookupFastCase(callee, NOT_INSIDE_TYPEOF, &slow, &done);
2917
2918 __ bind(&slow);
2919 // Call the runtime to find the function to call (returned in v0)
2920 // and the object holding it (returned in v1).
2921 DCHECK(!context_register().is(a2));
2922 __ li(a2, Operand(callee->name()));
2923 __ Push(context_register(), a2);
2924 __ CallRuntime(Runtime::kLoadLookupSlot);
2925 __ Push(v0, v1); // Function, receiver.
2926 PrepareForBailoutForId(expr->LookupId(), NO_REGISTERS);
2927
2928 // If fast case code has been generated, emit code to push the
2929 // function and receiver and have the slow path jump around this
2930 // code.
2931 if (done.is_linked()) {
2932 Label call;
2933 __ Branch(&call);
2934 __ bind(&done);
2935 // Push function.
2936 __ push(v0);
2937 // The receiver is implicitly the global receiver. Indicate this
2938 // by passing the hole to the call function stub.
2939 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
2940 __ push(a1);
2941 __ bind(&call);
2942 }
2943 } else {
2944 VisitForStackValue(callee);
2945 // refEnv.WithBaseObject()
2946 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2947 __ push(a2); // Reserved receiver slot.
2948 }
2949}
2950
2951
2952void FullCodeGenerator::EmitPossiblyEvalCall(Call* expr) {
2953 // In a call to eval, we first call RuntimeHidden_ResolvePossiblyDirectEval
2954 // to resolve the function we need to call. Then we call the resolved
2955 // function using the given arguments.
2956 ZoneList<Expression*>* args = expr->arguments();
2957 int arg_count = args->length();
2958 PushCalleeAndWithBaseObject(expr);
2959
2960 // Push the arguments.
2961 for (int i = 0; i < arg_count; i++) {
2962 VisitForStackValue(args->at(i));
2963 }
2964
2965 // Push a copy of the function (found below the arguments) and
2966 // resolve eval.
2967 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2968 __ push(a1);
2969 EmitResolvePossiblyDirectEval(arg_count);
2970
2971 // Touch up the stack with the resolved function.
2972 __ sw(v0, MemOperand(sp, (arg_count + 1) * kPointerSize));
2973
2974 PrepareForBailoutForId(expr->EvalId(), NO_REGISTERS);
2975 // Record source position for debugger.
2976 SetCallPosition(expr);
2977 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2978 __ li(a0, Operand(arg_count));
2979 __ Call(isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
2980 RecordJSReturnSite(expr);
2981 // Restore context register.
2982 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2983 context()->DropAndPlug(1, v0);
2984}
2985
2986
2987void FullCodeGenerator::VisitCallNew(CallNew* expr) {
2988 Comment cmnt(masm_, "[ CallNew");
2989 // According to ECMA-262, section 11.2.2, page 44, the function
2990 // expression in new calls must be evaluated before the
2991 // arguments.
2992
2993 // Push constructor on the stack. If it's not a function it's used as
2994 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2995 // ignored.g
2996 DCHECK(!expr->expression()->IsSuperPropertyReference());
2997 VisitForStackValue(expr->expression());
2998
2999 // Push the arguments ("left-to-right") on the stack.
3000 ZoneList<Expression*>* args = expr->arguments();
3001 int arg_count = args->length();
3002 for (int i = 0; i < arg_count; i++) {
3003 VisitForStackValue(args->at(i));
3004 }
3005
3006 // Call the construct call builtin that handles allocation and
3007 // constructor invocation.
3008 SetConstructCallPosition(expr);
3009
3010 // Load function and argument count into a1 and a0.
3011 __ li(a0, Operand(arg_count));
3012 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
3013
3014 // Record call targets in unoptimized code.
3015 __ EmitLoadTypeFeedbackVector(a2);
3016 __ li(a3, Operand(SmiFromSlot(expr->CallNewFeedbackSlot())));
3017
3018 CallConstructStub stub(isolate());
3019 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
3020 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
3021 // Restore context register.
3022 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3023 context()->Plug(v0);
3024}
3025
3026
3027void FullCodeGenerator::EmitSuperConstructorCall(Call* expr) {
3028 SuperCallReference* super_call_ref =
3029 expr->expression()->AsSuperCallReference();
3030 DCHECK_NOT_NULL(super_call_ref);
3031
3032 // Push the super constructor target on the stack (may be null,
3033 // but the Construct builtin can deal with that properly).
3034 VisitForAccumulatorValue(super_call_ref->this_function_var());
3035 __ AssertFunction(result_register());
3036 __ lw(result_register(),
3037 FieldMemOperand(result_register(), HeapObject::kMapOffset));
3038 __ lw(result_register(),
3039 FieldMemOperand(result_register(), Map::kPrototypeOffset));
3040 __ Push(result_register());
3041
3042 // Push the arguments ("left-to-right") on the stack.
3043 ZoneList<Expression*>* args = expr->arguments();
3044 int arg_count = args->length();
3045 for (int i = 0; i < arg_count; i++) {
3046 VisitForStackValue(args->at(i));
3047 }
3048
3049 // Call the construct call builtin that handles allocation and
3050 // constructor invocation.
3051 SetConstructCallPosition(expr);
3052
3053 // Load new target into a3.
3054 VisitForAccumulatorValue(super_call_ref->new_target_var());
3055 __ mov(a3, result_register());
3056
3057 // Load function and argument count into a1 and a0.
3058 __ li(a0, Operand(arg_count));
3059 __ lw(a1, MemOperand(sp, arg_count * kPointerSize));
3060
3061 __ Call(isolate()->builtins()->Construct(), RelocInfo::CODE_TARGET);
3062
3063 RecordJSReturnSite(expr);
3064
3065 // Restore context register.
3066 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3067 context()->Plug(v0);
3068}
3069
3070
3071void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
3072 ZoneList<Expression*>* args = expr->arguments();
3073 DCHECK(args->length() == 1);
3074
3075 VisitForAccumulatorValue(args->at(0));
3076
3077 Label materialize_true, materialize_false;
3078 Label* if_true = NULL;
3079 Label* if_false = NULL;
3080 Label* fall_through = NULL;
3081 context()->PrepareTest(&materialize_true, &materialize_false,
3082 &if_true, &if_false, &fall_through);
3083
3084 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3085 __ SmiTst(v0, t0);
3086 Split(eq, t0, Operand(zero_reg), if_true, if_false, fall_through);
3087
3088 context()->Plug(if_true, if_false);
3089}
3090
3091
3092void FullCodeGenerator::EmitIsJSReceiver(CallRuntime* expr) {
3093 ZoneList<Expression*>* args = expr->arguments();
3094 DCHECK(args->length() == 1);
3095
3096 VisitForAccumulatorValue(args->at(0));
3097
3098 Label materialize_true, materialize_false;
3099 Label* if_true = NULL;
3100 Label* if_false = NULL;
3101 Label* fall_through = NULL;
3102 context()->PrepareTest(&materialize_true, &materialize_false,
3103 &if_true, &if_false, &fall_through);
3104
3105 __ JumpIfSmi(v0, if_false);
3106 __ GetObjectType(v0, a1, a1);
3107 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3108 Split(ge, a1, Operand(FIRST_JS_RECEIVER_TYPE),
3109 if_true, if_false, fall_through);
3110
3111 context()->Plug(if_true, if_false);
3112}
3113
3114
3115void FullCodeGenerator::EmitIsSimdValue(CallRuntime* expr) {
3116 ZoneList<Expression*>* args = expr->arguments();
3117 DCHECK(args->length() == 1);
3118
3119 VisitForAccumulatorValue(args->at(0));
3120
3121 Label materialize_true, materialize_false;
3122 Label* if_true = NULL;
3123 Label* if_false = NULL;
3124 Label* fall_through = NULL;
3125 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3126 &if_false, &fall_through);
3127
3128 __ JumpIfSmi(v0, if_false);
3129 __ GetObjectType(v0, a1, a1);
3130 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3131 Split(eq, a1, Operand(SIMD128_VALUE_TYPE), if_true, if_false, fall_through);
3132
3133 context()->Plug(if_true, if_false);
3134}
3135
3136
3137void FullCodeGenerator::EmitIsFunction(CallRuntime* expr) {
3138 ZoneList<Expression*>* args = expr->arguments();
3139 DCHECK(args->length() == 1);
3140
3141 VisitForAccumulatorValue(args->at(0));
3142
3143 Label materialize_true, materialize_false;
3144 Label* if_true = NULL;
3145 Label* if_false = NULL;
3146 Label* fall_through = NULL;
3147 context()->PrepareTest(&materialize_true, &materialize_false,
3148 &if_true, &if_false, &fall_through);
3149
3150 __ JumpIfSmi(v0, if_false);
3151 __ GetObjectType(v0, a1, a2);
3152 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3153 __ Branch(if_true, hs, a2, Operand(FIRST_FUNCTION_TYPE));
3154 __ Branch(if_false);
3155
3156 context()->Plug(if_true, if_false);
3157}
3158
3159
3160void FullCodeGenerator::EmitIsMinusZero(CallRuntime* expr) {
3161 ZoneList<Expression*>* args = expr->arguments();
3162 DCHECK(args->length() == 1);
3163
3164 VisitForAccumulatorValue(args->at(0));
3165
3166 Label materialize_true, materialize_false;
3167 Label* if_true = NULL;
3168 Label* if_false = NULL;
3169 Label* fall_through = NULL;
3170 context()->PrepareTest(&materialize_true, &materialize_false,
3171 &if_true, &if_false, &fall_through);
3172
3173 __ CheckMap(v0, a1, Heap::kHeapNumberMapRootIndex, if_false, DO_SMI_CHECK);
3174 __ lw(a2, FieldMemOperand(v0, HeapNumber::kExponentOffset));
3175 __ lw(a1, FieldMemOperand(v0, HeapNumber::kMantissaOffset));
3176 __ li(t0, 0x80000000);
3177 Label not_nan;
3178 __ Branch(&not_nan, ne, a2, Operand(t0));
3179 __ mov(t0, zero_reg);
3180 __ mov(a2, a1);
3181 __ bind(&not_nan);
3182
3183 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3184 Split(eq, a2, Operand(t0), if_true, if_false, fall_through);
3185
3186 context()->Plug(if_true, if_false);
3187}
3188
3189
3190void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
3191 ZoneList<Expression*>* args = expr->arguments();
3192 DCHECK(args->length() == 1);
3193
3194 VisitForAccumulatorValue(args->at(0));
3195
3196 Label materialize_true, materialize_false;
3197 Label* if_true = NULL;
3198 Label* if_false = NULL;
3199 Label* fall_through = NULL;
3200 context()->PrepareTest(&materialize_true, &materialize_false,
3201 &if_true, &if_false, &fall_through);
3202
3203 __ JumpIfSmi(v0, if_false);
3204 __ GetObjectType(v0, a1, a1);
3205 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3206 Split(eq, a1, Operand(JS_ARRAY_TYPE),
3207 if_true, if_false, fall_through);
3208
3209 context()->Plug(if_true, if_false);
3210}
3211
3212
3213void FullCodeGenerator::EmitIsTypedArray(CallRuntime* expr) {
3214 ZoneList<Expression*>* args = expr->arguments();
3215 DCHECK(args->length() == 1);
3216
3217 VisitForAccumulatorValue(args->at(0));
3218
3219 Label materialize_true, materialize_false;
3220 Label* if_true = NULL;
3221 Label* if_false = NULL;
3222 Label* fall_through = NULL;
3223 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3224 &if_false, &fall_through);
3225
3226 __ JumpIfSmi(v0, if_false);
3227 __ GetObjectType(v0, a1, a1);
3228 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3229 Split(eq, a1, Operand(JS_TYPED_ARRAY_TYPE), if_true, if_false, fall_through);
3230
3231 context()->Plug(if_true, if_false);
3232}
3233
3234
3235void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
3236 ZoneList<Expression*>* args = expr->arguments();
3237 DCHECK(args->length() == 1);
3238
3239 VisitForAccumulatorValue(args->at(0));
3240
3241 Label materialize_true, materialize_false;
3242 Label* if_true = NULL;
3243 Label* if_false = NULL;
3244 Label* fall_through = NULL;
3245 context()->PrepareTest(&materialize_true, &materialize_false,
3246 &if_true, &if_false, &fall_through);
3247
3248 __ JumpIfSmi(v0, if_false);
3249 __ GetObjectType(v0, a1, a1);
3250 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3251 Split(eq, a1, Operand(JS_REGEXP_TYPE), if_true, if_false, fall_through);
3252
3253 context()->Plug(if_true, if_false);
3254}
3255
3256
3257void FullCodeGenerator::EmitIsJSProxy(CallRuntime* expr) {
3258 ZoneList<Expression*>* args = expr->arguments();
3259 DCHECK(args->length() == 1);
3260
3261 VisitForAccumulatorValue(args->at(0));
3262
3263 Label materialize_true, materialize_false;
3264 Label* if_true = NULL;
3265 Label* if_false = NULL;
3266 Label* fall_through = NULL;
3267 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3268 &if_false, &fall_through);
3269
3270 __ JumpIfSmi(v0, if_false);
3271 __ GetObjectType(v0, a1, a1);
3272 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3273 Split(eq, a1, Operand(JS_PROXY_TYPE), if_true, if_false, fall_through);
3274
3275 context()->Plug(if_true, if_false);
3276}
3277
3278
3279void FullCodeGenerator::EmitObjectEquals(CallRuntime* expr) {
3280 ZoneList<Expression*>* args = expr->arguments();
3281 DCHECK(args->length() == 2);
3282
3283 // Load the two objects into registers and perform the comparison.
3284 VisitForStackValue(args->at(0));
3285 VisitForAccumulatorValue(args->at(1));
3286
3287 Label materialize_true, materialize_false;
3288 Label* if_true = NULL;
3289 Label* if_false = NULL;
3290 Label* fall_through = NULL;
3291 context()->PrepareTest(&materialize_true, &materialize_false,
3292 &if_true, &if_false, &fall_through);
3293
3294 __ pop(a1);
3295 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3296 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
3297
3298 context()->Plug(if_true, if_false);
3299}
3300
3301
3302void FullCodeGenerator::EmitArguments(CallRuntime* expr) {
3303 ZoneList<Expression*>* args = expr->arguments();
3304 DCHECK(args->length() == 1);
3305
3306 // ArgumentsAccessStub expects the key in a1 and the formal
3307 // parameter count in a0.
3308 VisitForAccumulatorValue(args->at(0));
3309 __ mov(a1, v0);
3310 __ li(a0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
3311 ArgumentsAccessStub stub(isolate(), ArgumentsAccessStub::READ_ELEMENT);
3312 __ CallStub(&stub);
3313 context()->Plug(v0);
3314}
3315
3316
3317void FullCodeGenerator::EmitArgumentsLength(CallRuntime* expr) {
3318 DCHECK(expr->arguments()->length() == 0);
3319 Label exit;
3320 // Get the number of formal parameters.
3321 __ li(v0, Operand(Smi::FromInt(info_->scope()->num_parameters())));
3322
3323 // Check if the calling frame is an arguments adaptor frame.
3324 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3325 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
3326 __ Branch(&exit, ne, a3,
3327 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3328
3329 // Arguments adaptor case: Read the arguments length from the
3330 // adaptor frame.
3331 __ lw(v0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3332
3333 __ bind(&exit);
3334 context()->Plug(v0);
3335}
3336
3337
3338void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
3339 ZoneList<Expression*>* args = expr->arguments();
3340 DCHECK(args->length() == 1);
3341 Label done, null, function, non_function_constructor;
3342
3343 VisitForAccumulatorValue(args->at(0));
3344
3345 // If the object is not a JSReceiver, we return null.
3346 __ JumpIfSmi(v0, &null);
3347 STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
3348 __ GetObjectType(v0, v0, a1); // Map is now in v0.
3349 __ Branch(&null, lt, a1, Operand(FIRST_JS_RECEIVER_TYPE));
3350
3351 // Return 'Function' for JSFunction objects.
3352 __ Branch(&function, eq, a1, Operand(JS_FUNCTION_TYPE));
3353
3354 // Check if the constructor in the map is a JS function.
3355 Register instance_type = a2;
3356 __ GetMapConstructor(v0, v0, a1, instance_type);
3357 __ Branch(&non_function_constructor, ne, instance_type,
3358 Operand(JS_FUNCTION_TYPE));
3359
3360 // v0 now contains the constructor function. Grab the
3361 // instance class name from there.
3362 __ lw(v0, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
3363 __ lw(v0, FieldMemOperand(v0, SharedFunctionInfo::kInstanceClassNameOffset));
3364 __ Branch(&done);
3365
3366 // Functions have class 'Function'.
3367 __ bind(&function);
3368 __ LoadRoot(v0, Heap::kFunction_stringRootIndex);
3369 __ jmp(&done);
3370
3371 // Objects with a non-function constructor have class 'Object'.
3372 __ bind(&non_function_constructor);
3373 __ LoadRoot(v0, Heap::kObject_stringRootIndex);
3374 __ jmp(&done);
3375
3376 // Non-JS objects have class null.
3377 __ bind(&null);
3378 __ LoadRoot(v0, Heap::kNullValueRootIndex);
3379
3380 // All done.
3381 __ bind(&done);
3382
3383 context()->Plug(v0);
3384}
3385
3386
3387void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
3388 ZoneList<Expression*>* args = expr->arguments();
3389 DCHECK(args->length() == 1);
3390
3391 VisitForAccumulatorValue(args->at(0)); // Load the object.
3392
3393 Label done;
3394 // If the object is a smi return the object.
3395 __ JumpIfSmi(v0, &done);
3396 // If the object is not a value type, return the object.
3397 __ GetObjectType(v0, a1, a1);
3398 __ Branch(&done, ne, a1, Operand(JS_VALUE_TYPE));
3399
3400 __ lw(v0, FieldMemOperand(v0, JSValue::kValueOffset));
3401
3402 __ bind(&done);
3403 context()->Plug(v0);
3404}
3405
3406
3407void FullCodeGenerator::EmitIsDate(CallRuntime* expr) {
3408 ZoneList<Expression*>* args = expr->arguments();
3409 DCHECK_EQ(1, args->length());
3410
3411 VisitForAccumulatorValue(args->at(0));
3412
3413 Label materialize_true, materialize_false;
3414 Label* if_true = nullptr;
3415 Label* if_false = nullptr;
3416 Label* fall_through = nullptr;
3417 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
3418 &if_false, &fall_through);
3419
3420 __ JumpIfSmi(v0, if_false);
3421 __ GetObjectType(v0, a1, a1);
3422 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3423 Split(eq, a1, Operand(JS_DATE_TYPE), if_true, if_false, fall_through);
3424
3425 context()->Plug(if_true, if_false);
3426}
3427
3428
3429void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
3430 ZoneList<Expression*>* args = expr->arguments();
3431 DCHECK_EQ(3, args->length());
3432
3433 Register string = v0;
3434 Register index = a1;
3435 Register value = a2;
3436
3437 VisitForStackValue(args->at(0)); // index
3438 VisitForStackValue(args->at(1)); // value
3439 VisitForAccumulatorValue(args->at(2)); // string
3440 __ Pop(index, value);
3441
3442 if (FLAG_debug_code) {
3443 __ SmiTst(value, at);
3444 __ Check(eq, kNonSmiValue, at, Operand(zero_reg));
3445 __ SmiTst(index, at);
3446 __ Check(eq, kNonSmiIndex, at, Operand(zero_reg));
3447 __ SmiUntag(index, index);
3448 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
3449 Register scratch = t5;
3450 __ EmitSeqStringSetCharCheck(
3451 string, index, value, scratch, one_byte_seq_type);
3452 __ SmiTag(index, index);
3453 }
3454
3455 __ SmiUntag(value, value);
3456 __ Addu(at,
3457 string,
3458 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
3459 __ SmiUntag(index);
3460 __ Addu(at, at, index);
3461 __ sb(value, MemOperand(at));
3462 context()->Plug(string);
3463}
3464
3465
3466void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
3467 ZoneList<Expression*>* args = expr->arguments();
3468 DCHECK_EQ(3, args->length());
3469
3470 Register string = v0;
3471 Register index = a1;
3472 Register value = a2;
3473
3474 VisitForStackValue(args->at(0)); // index
3475 VisitForStackValue(args->at(1)); // value
3476 VisitForAccumulatorValue(args->at(2)); // string
3477 __ Pop(index, value);
3478
3479 if (FLAG_debug_code) {
3480 __ SmiTst(value, at);
3481 __ Check(eq, kNonSmiValue, at, Operand(zero_reg));
3482 __ SmiTst(index, at);
3483 __ Check(eq, kNonSmiIndex, at, Operand(zero_reg));
3484 __ SmiUntag(index, index);
3485 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
3486 Register scratch = t5;
3487 __ EmitSeqStringSetCharCheck(
3488 string, index, value, scratch, two_byte_seq_type);
3489 __ SmiTag(index, index);
3490 }
3491
3492 __ SmiUntag(value, value);
3493 __ Addu(at,
3494 string,
3495 Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3496 __ Addu(at, at, index);
3497 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
3498 __ sh(value, MemOperand(at));
3499 context()->Plug(string);
3500}
3501
3502
3503void FullCodeGenerator::EmitSetValueOf(CallRuntime* expr) {
3504 ZoneList<Expression*>* args = expr->arguments();
3505 DCHECK(args->length() == 2);
3506
3507 VisitForStackValue(args->at(0)); // Load the object.
3508 VisitForAccumulatorValue(args->at(1)); // Load the value.
3509 __ pop(a1); // v0 = value. a1 = object.
3510
3511 Label done;
3512 // If the object is a smi, return the value.
3513 __ JumpIfSmi(a1, &done);
3514
3515 // If the object is not a value type, return the value.
3516 __ GetObjectType(a1, a2, a2);
3517 __ Branch(&done, ne, a2, Operand(JS_VALUE_TYPE));
3518
3519 // Store the value.
3520 __ sw(v0, FieldMemOperand(a1, JSValue::kValueOffset));
3521 // Update the write barrier. Save the value as it will be
3522 // overwritten by the write barrier code and is needed afterward.
3523 __ mov(a2, v0);
3524 __ RecordWriteField(
3525 a1, JSValue::kValueOffset, a2, a3, kRAHasBeenSaved, kDontSaveFPRegs);
3526
3527 __ bind(&done);
3528 context()->Plug(v0);
3529}
3530
3531
3532void FullCodeGenerator::EmitToInteger(CallRuntime* expr) {
3533 ZoneList<Expression*>* args = expr->arguments();
3534 DCHECK_EQ(1, args->length());
3535
3536 // Load the argument into v0 and convert it.
3537 VisitForAccumulatorValue(args->at(0));
3538
3539 // Convert the object to an integer.
3540 Label done_convert;
3541 __ JumpIfSmi(v0, &done_convert);
3542 __ Push(v0);
3543 __ CallRuntime(Runtime::kToInteger);
3544 __ bind(&done_convert);
3545 context()->Plug(v0);
3546}
3547
3548
3549void FullCodeGenerator::EmitToName(CallRuntime* expr) {
3550 ZoneList<Expression*>* args = expr->arguments();
3551 DCHECK_EQ(1, args->length());
3552
3553 // Load the argument into v0 and convert it.
3554 VisitForAccumulatorValue(args->at(0));
3555
3556 Label convert, done_convert;
3557 __ JumpIfSmi(v0, &convert);
3558 STATIC_ASSERT(FIRST_NAME_TYPE == FIRST_TYPE);
3559 __ GetObjectType(v0, a1, a1);
3560 __ Branch(&done_convert, le, a1, Operand(LAST_NAME_TYPE));
3561 __ bind(&convert);
3562 __ Push(v0);
3563 __ CallRuntime(Runtime::kToName);
3564 __ bind(&done_convert);
3565 context()->Plug(v0);
3566}
3567
3568
3569void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3570 ZoneList<Expression*>* args = expr->arguments();
3571 DCHECK(args->length() == 1);
3572
3573 VisitForAccumulatorValue(args->at(0));
3574
3575 Label done;
3576 StringCharFromCodeGenerator generator(v0, a1);
3577 generator.GenerateFast(masm_);
3578 __ jmp(&done);
3579
3580 NopRuntimeCallHelper call_helper;
3581 generator.GenerateSlow(masm_, call_helper);
3582
3583 __ bind(&done);
3584 context()->Plug(a1);
3585}
3586
3587
3588void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3589 ZoneList<Expression*>* args = expr->arguments();
3590 DCHECK(args->length() == 2);
3591
3592 VisitForStackValue(args->at(0));
3593 VisitForAccumulatorValue(args->at(1));
3594 __ mov(a0, result_register());
3595
3596 Register object = a1;
3597 Register index = a0;
3598 Register result = v0;
3599
3600 __ pop(object);
3601
3602 Label need_conversion;
3603 Label index_out_of_range;
3604 Label done;
3605 StringCharCodeAtGenerator generator(object,
3606 index,
3607 result,
3608 &need_conversion,
3609 &need_conversion,
3610 &index_out_of_range,
3611 STRING_INDEX_IS_NUMBER);
3612 generator.GenerateFast(masm_);
3613 __ jmp(&done);
3614
3615 __ bind(&index_out_of_range);
3616 // When the index is out of range, the spec requires us to return
3617 // NaN.
3618 __ LoadRoot(result, Heap::kNanValueRootIndex);
3619 __ jmp(&done);
3620
3621 __ bind(&need_conversion);
3622 // Load the undefined value into the result register, which will
3623 // trigger conversion.
3624 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3625 __ jmp(&done);
3626
3627 NopRuntimeCallHelper call_helper;
3628 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
3629
3630 __ bind(&done);
3631 context()->Plug(result);
3632}
3633
3634
3635void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3636 ZoneList<Expression*>* args = expr->arguments();
3637 DCHECK(args->length() == 2);
3638
3639 VisitForStackValue(args->at(0));
3640 VisitForAccumulatorValue(args->at(1));
3641 __ mov(a0, result_register());
3642
3643 Register object = a1;
3644 Register index = a0;
3645 Register scratch = a3;
3646 Register result = v0;
3647
3648 __ pop(object);
3649
3650 Label need_conversion;
3651 Label index_out_of_range;
3652 Label done;
3653 StringCharAtGenerator generator(object,
3654 index,
3655 scratch,
3656 result,
3657 &need_conversion,
3658 &need_conversion,
3659 &index_out_of_range,
3660 STRING_INDEX_IS_NUMBER);
3661 generator.GenerateFast(masm_);
3662 __ jmp(&done);
3663
3664 __ bind(&index_out_of_range);
3665 // When the index is out of range, the spec requires us to return
3666 // the empty string.
3667 __ LoadRoot(result, Heap::kempty_stringRootIndex);
3668 __ jmp(&done);
3669
3670 __ bind(&need_conversion);
3671 // Move smi zero into the result register, which will trigger
3672 // conversion.
3673 __ li(result, Operand(Smi::FromInt(0)));
3674 __ jmp(&done);
3675
3676 NopRuntimeCallHelper call_helper;
3677 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
3678
3679 __ bind(&done);
3680 context()->Plug(result);
3681}
3682
3683
3684void FullCodeGenerator::EmitCall(CallRuntime* expr) {
3685 ZoneList<Expression*>* args = expr->arguments();
3686 DCHECK_LE(2, args->length());
3687 // Push target, receiver and arguments onto the stack.
3688 for (Expression* const arg : *args) {
3689 VisitForStackValue(arg);
3690 }
3691 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
3692 // Move target to a1.
3693 int const argc = args->length() - 2;
3694 __ lw(a1, MemOperand(sp, (argc + 1) * kPointerSize));
3695 // Call the target.
3696 __ li(a0, Operand(argc));
3697 __ Call(isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
3698 // Restore context register.
3699 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
3700 // Discard the function left on TOS.
3701 context()->DropAndPlug(1, v0);
3702}
3703
3704
3705void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3706 ZoneList<Expression*>* args = expr->arguments();
3707 VisitForAccumulatorValue(args->at(0));
3708
3709 Label materialize_true, materialize_false;
3710 Label* if_true = NULL;
3711 Label* if_false = NULL;
3712 Label* fall_through = NULL;
3713 context()->PrepareTest(&materialize_true, &materialize_false,
3714 &if_true, &if_false, &fall_through);
3715
3716 __ lw(a0, FieldMemOperand(v0, String::kHashFieldOffset));
3717 __ And(a0, a0, Operand(String::kContainsCachedArrayIndexMask));
3718
3719 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3720 Split(eq, a0, Operand(zero_reg), if_true, if_false, fall_through);
3721
3722 context()->Plug(if_true, if_false);
3723}
3724
3725
3726void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3727 ZoneList<Expression*>* args = expr->arguments();
3728 DCHECK(args->length() == 1);
3729 VisitForAccumulatorValue(args->at(0));
3730
3731 __ AssertString(v0);
3732
3733 __ lw(v0, FieldMemOperand(v0, String::kHashFieldOffset));
3734 __ IndexFromHash(v0, v0);
3735
3736 context()->Plug(v0);
3737}
3738
3739
3740void FullCodeGenerator::EmitGetSuperConstructor(CallRuntime* expr) {
3741 ZoneList<Expression*>* args = expr->arguments();
3742 DCHECK_EQ(1, args->length());
3743 VisitForAccumulatorValue(args->at(0));
3744 __ AssertFunction(v0);
3745 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
3746 __ lw(v0, FieldMemOperand(v0, Map::kPrototypeOffset));
3747 context()->Plug(v0);
3748}
3749
3750
3751void FullCodeGenerator::EmitFastOneByteArrayJoin(CallRuntime* expr) {
3752 Label bailout, done, one_char_separator, long_separator,
3753 non_trivial_array, not_size_one_array, loop,
3754 empty_separator_loop, one_char_separator_loop,
3755 one_char_separator_loop_entry, long_separator_loop;
3756 ZoneList<Expression*>* args = expr->arguments();
3757 DCHECK(args->length() == 2);
3758 VisitForStackValue(args->at(1));
3759 VisitForAccumulatorValue(args->at(0));
3760
3761 // All aliases of the same register have disjoint lifetimes.
3762 Register array = v0;
3763 Register elements = no_reg; // Will be v0.
3764 Register result = no_reg; // Will be v0.
3765 Register separator = a1;
3766 Register array_length = a2;
3767 Register result_pos = no_reg; // Will be a2.
3768 Register string_length = a3;
3769 Register string = t0;
3770 Register element = t1;
3771 Register elements_end = t2;
3772 Register scratch1 = t3;
3773 Register scratch2 = t5;
3774 Register scratch3 = t4;
3775
3776 // Separator operand is on the stack.
3777 __ pop(separator);
3778
3779 // Check that the array is a JSArray.
3780 __ JumpIfSmi(array, &bailout);
3781 __ GetObjectType(array, scratch1, scratch2);
3782 __ Branch(&bailout, ne, scratch2, Operand(JS_ARRAY_TYPE));
3783
3784 // Check that the array has fast elements.
3785 __ CheckFastElements(scratch1, scratch2, &bailout);
3786
3787 // If the array has length zero, return the empty string.
3788 __ lw(array_length, FieldMemOperand(array, JSArray::kLengthOffset));
3789 __ SmiUntag(array_length);
3790 __ Branch(&non_trivial_array, ne, array_length, Operand(zero_reg));
3791 __ LoadRoot(v0, Heap::kempty_stringRootIndex);
3792 __ Branch(&done);
3793
3794 __ bind(&non_trivial_array);
3795
3796 // Get the FixedArray containing array's elements.
3797 elements = array;
3798 __ lw(elements, FieldMemOperand(array, JSArray::kElementsOffset));
3799 array = no_reg; // End of array's live range.
3800
3801 // Check that all array elements are sequential one-byte strings, and
3802 // accumulate the sum of their lengths, as a smi-encoded value.
3803 __ mov(string_length, zero_reg);
3804 __ Addu(element,
3805 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3806 __ sll(elements_end, array_length, kPointerSizeLog2);
3807 __ Addu(elements_end, element, elements_end);
3808 // Loop condition: while (element < elements_end).
3809 // Live values in registers:
3810 // elements: Fixed array of strings.
3811 // array_length: Length of the fixed array of strings (not smi)
3812 // separator: Separator string
3813 // string_length: Accumulated sum of string lengths (smi).
3814 // element: Current array element.
3815 // elements_end: Array end.
3816 if (generate_debug_code_) {
3817 __ Assert(gt, kNoEmptyArraysHereInEmitFastOneByteArrayJoin, array_length,
3818 Operand(zero_reg));
3819 }
3820 __ bind(&loop);
3821 __ lw(string, MemOperand(element));
3822 __ Addu(element, element, kPointerSize);
3823 __ JumpIfSmi(string, &bailout);
3824 __ lw(scratch1, FieldMemOperand(string, HeapObject::kMapOffset));
3825 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3826 __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
3827 __ lw(scratch1, FieldMemOperand(string, SeqOneByteString::kLengthOffset));
3828 __ AddBranchOvf(string_length, string_length, Operand(scratch1), &bailout);
3829 __ Branch(&loop, lt, element, Operand(elements_end));
3830
3831 // If array_length is 1, return elements[0], a string.
3832 __ Branch(&not_size_one_array, ne, array_length, Operand(1));
3833 __ lw(v0, FieldMemOperand(elements, FixedArray::kHeaderSize));
3834 __ Branch(&done);
3835
3836 __ bind(&not_size_one_array);
3837
3838 // Live values in registers:
3839 // separator: Separator string
3840 // array_length: Length of the array.
3841 // string_length: Sum of string lengths (smi).
3842 // elements: FixedArray of strings.
3843
3844 // Check that the separator is a flat one-byte string.
3845 __ JumpIfSmi(separator, &bailout);
3846 __ lw(scratch1, FieldMemOperand(separator, HeapObject::kMapOffset));
3847 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
3848 __ JumpIfInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, &bailout);
3849
3850 // Add (separator length times array_length) - separator length to the
3851 // string_length to get the length of the result string. array_length is not
3852 // smi but the other values are, so the result is a smi.
3853 __ lw(scratch1, FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
3854 __ Subu(string_length, string_length, Operand(scratch1));
3855 __ Mul(scratch3, scratch2, array_length, scratch1);
3856 // Check for smi overflow. No overflow if higher 33 bits of 64-bit result are
3857 // zero.
3858 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3859 __ And(scratch3, scratch2, Operand(0x80000000));
3860 __ Branch(&bailout, ne, scratch3, Operand(zero_reg));
3861 __ AddBranchOvf(string_length, string_length, Operand(scratch2), &bailout);
3862 __ SmiUntag(string_length);
3863
3864 // Bailout for large object allocations.
3865 __ Branch(&bailout, gt, string_length,
3866 Operand(Page::kMaxRegularHeapObjectSize));
3867
3868 // Get first element in the array to free up the elements register to be used
3869 // for the result.
3870 __ Addu(element,
3871 elements, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3872 result = elements; // End of live range for elements.
3873 elements = no_reg;
3874 // Live values in registers:
3875 // element: First array element
3876 // separator: Separator string
3877 // string_length: Length of result string (not smi)
3878 // array_length: Length of the array.
3879 __ AllocateOneByteString(result, string_length, scratch1, scratch2,
3880 elements_end, &bailout);
3881 // Prepare for looping. Set up elements_end to end of the array. Set
3882 // result_pos to the position of the result where to write the first
3883 // character.
3884 __ sll(elements_end, array_length, kPointerSizeLog2);
3885 __ Addu(elements_end, element, elements_end);
3886 result_pos = array_length; // End of live range for array_length.
3887 array_length = no_reg;
3888 __ Addu(result_pos,
3889 result,
3890 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
3891
3892 // Check the length of the separator.
3893 __ lw(scratch1, FieldMemOperand(separator, SeqOneByteString::kLengthOffset));
3894 __ li(at, Operand(Smi::FromInt(1)));
3895 __ Branch(&one_char_separator, eq, scratch1, Operand(at));
3896 __ Branch(&long_separator, gt, scratch1, Operand(at));
3897
3898 // Empty separator case.
3899 __ bind(&empty_separator_loop);
3900 // Live values in registers:
3901 // result_pos: the position to which we are currently copying characters.
3902 // element: Current array element.
3903 // elements_end: Array end.
3904
3905 // Copy next array element to the result.
3906 __ lw(string, MemOperand(element));
3907 __ Addu(element, element, kPointerSize);
3908 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3909 __ SmiUntag(string_length);
3910 __ Addu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
3911 __ CopyBytes(string, result_pos, string_length, scratch1);
3912 // End while (element < elements_end).
3913 __ Branch(&empty_separator_loop, lt, element, Operand(elements_end));
3914 DCHECK(result.is(v0));
3915 __ Branch(&done);
3916
3917 // One-character separator case.
3918 __ bind(&one_char_separator);
3919 // Replace separator with its one-byte character value.
3920 __ lbu(separator, FieldMemOperand(separator, SeqOneByteString::kHeaderSize));
3921 // Jump into the loop after the code that copies the separator, so the first
3922 // element is not preceded by a separator.
3923 __ jmp(&one_char_separator_loop_entry);
3924
3925 __ bind(&one_char_separator_loop);
3926 // Live values in registers:
3927 // result_pos: the position to which we are currently copying characters.
3928 // element: Current array element.
3929 // elements_end: Array end.
3930 // separator: Single separator one-byte char (in lower byte).
3931
3932 // Copy the separator character to the result.
3933 __ sb(separator, MemOperand(result_pos));
3934 __ Addu(result_pos, result_pos, 1);
3935
3936 // Copy next array element to the result.
3937 __ bind(&one_char_separator_loop_entry);
3938 __ lw(string, MemOperand(element));
3939 __ Addu(element, element, kPointerSize);
3940 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3941 __ SmiUntag(string_length);
3942 __ Addu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
3943 __ CopyBytes(string, result_pos, string_length, scratch1);
3944 // End while (element < elements_end).
3945 __ Branch(&one_char_separator_loop, lt, element, Operand(elements_end));
3946 DCHECK(result.is(v0));
3947 __ Branch(&done);
3948
3949 // Long separator case (separator is more than one character). Entry is at the
3950 // label long_separator below.
3951 __ bind(&long_separator_loop);
3952 // Live values in registers:
3953 // result_pos: the position to which we are currently copying characters.
3954 // element: Current array element.
3955 // elements_end: Array end.
3956 // separator: Separator string.
3957
3958 // Copy the separator to the result.
3959 __ lw(string_length, FieldMemOperand(separator, String::kLengthOffset));
3960 __ SmiUntag(string_length);
3961 __ Addu(string,
3962 separator,
3963 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
3964 __ CopyBytes(string, result_pos, string_length, scratch1);
3965
3966 __ bind(&long_separator);
3967 __ lw(string, MemOperand(element));
3968 __ Addu(element, element, kPointerSize);
3969 __ lw(string_length, FieldMemOperand(string, String::kLengthOffset));
3970 __ SmiUntag(string_length);
3971 __ Addu(string, string, SeqOneByteString::kHeaderSize - kHeapObjectTag);
3972 __ CopyBytes(string, result_pos, string_length, scratch1);
3973 // End while (element < elements_end).
3974 __ Branch(&long_separator_loop, lt, element, Operand(elements_end));
3975 DCHECK(result.is(v0));
3976 __ Branch(&done);
3977
3978 __ bind(&bailout);
3979 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3980 __ bind(&done);
3981 context()->Plug(v0);
3982}
3983
3984
3985void FullCodeGenerator::EmitDebugIsActive(CallRuntime* expr) {
3986 DCHECK(expr->arguments()->length() == 0);
3987 ExternalReference debug_is_active =
3988 ExternalReference::debug_is_active_address(isolate());
3989 __ li(at, Operand(debug_is_active));
3990 __ lb(v0, MemOperand(at));
3991 __ SmiTag(v0);
3992 context()->Plug(v0);
3993}
3994
3995
3996void FullCodeGenerator::EmitCreateIterResultObject(CallRuntime* expr) {
3997 ZoneList<Expression*>* args = expr->arguments();
3998 DCHECK_EQ(2, args->length());
3999 VisitForStackValue(args->at(0));
4000 VisitForStackValue(args->at(1));
4001
4002 Label runtime, done;
4003
4004 __ Allocate(JSIteratorResult::kSize, v0, a2, a3, &runtime, TAG_OBJECT);
4005 __ LoadNativeContextSlot(Context::ITERATOR_RESULT_MAP_INDEX, a1);
4006 __ Pop(a2, a3);
4007 __ LoadRoot(t0, Heap::kEmptyFixedArrayRootIndex);
4008 __ sw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
4009 __ sw(t0, FieldMemOperand(v0, JSObject::kPropertiesOffset));
4010 __ sw(t0, FieldMemOperand(v0, JSObject::kElementsOffset));
4011 __ sw(a2, FieldMemOperand(v0, JSIteratorResult::kValueOffset));
4012 __ sw(a3, FieldMemOperand(v0, JSIteratorResult::kDoneOffset));
4013 STATIC_ASSERT(JSIteratorResult::kSize == 5 * kPointerSize);
4014 __ jmp(&done);
4015
4016 __ bind(&runtime);
4017 __ CallRuntime(Runtime::kCreateIterResultObject);
4018
4019 __ bind(&done);
4020 context()->Plug(v0);
4021}
4022
4023
4024void FullCodeGenerator::EmitLoadJSRuntimeFunction(CallRuntime* expr) {
4025 // Push undefined as the receiver.
4026 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
4027 __ push(v0);
4028
4029 __ LoadNativeContextSlot(expr->context_index(), v0);
4030}
4031
4032
4033void FullCodeGenerator::EmitCallJSRuntimeFunction(CallRuntime* expr) {
4034 ZoneList<Expression*>* args = expr->arguments();
4035 int arg_count = args->length();
4036
4037 SetCallPosition(expr);
4038 __ lw(a1, MemOperand(sp, (arg_count + 1) * kPointerSize));
4039 __ li(a0, Operand(arg_count));
4040 __ Call(isolate()->builtins()->Call(ConvertReceiverMode::kNullOrUndefined),
4041 RelocInfo::CODE_TARGET);
4042}
4043
4044
4045void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
4046 ZoneList<Expression*>* args = expr->arguments();
4047 int arg_count = args->length();
4048
4049 if (expr->is_jsruntime()) {
4050 Comment cmnt(masm_, "[ CallRuntime");
4051 EmitLoadJSRuntimeFunction(expr);
4052
4053 // Push the target function under the receiver.
4054 __ lw(at, MemOperand(sp, 0));
4055 __ push(at);
4056 __ sw(v0, MemOperand(sp, kPointerSize));
4057
4058 // Push the arguments ("left-to-right").
4059 for (int i = 0; i < arg_count; i++) {
4060 VisitForStackValue(args->at(i));
4061 }
4062
4063 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4064 EmitCallJSRuntimeFunction(expr);
4065
4066 // Restore context register.
4067 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
4068
4069 context()->DropAndPlug(1, v0);
4070
4071 } else {
4072 const Runtime::Function* function = expr->function();
4073 switch (function->function_id) {
4074#define CALL_INTRINSIC_GENERATOR(Name) \
4075 case Runtime::kInline##Name: { \
4076 Comment cmnt(masm_, "[ Inline" #Name); \
4077 return Emit##Name(expr); \
4078 }
4079 FOR_EACH_FULL_CODE_INTRINSIC(CALL_INTRINSIC_GENERATOR)
4080#undef CALL_INTRINSIC_GENERATOR
4081 default: {
4082 Comment cmnt(masm_, "[ CallRuntime for unhandled intrinsic");
4083 // Push the arguments ("left-to-right").
4084 for (int i = 0; i < arg_count; i++) {
4085 VisitForStackValue(args->at(i));
4086 }
4087
4088 // Call the C runtime function.
4089 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
4090 __ CallRuntime(expr->function(), arg_count);
4091 context()->Plug(v0);
4092 }
4093 }
4094 }
4095}
4096
4097
4098void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
4099 switch (expr->op()) {
4100 case Token::DELETE: {
4101 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
4102 Property* property = expr->expression()->AsProperty();
4103 VariableProxy* proxy = expr->expression()->AsVariableProxy();
4104
4105 if (property != NULL) {
4106 VisitForStackValue(property->obj());
4107 VisitForStackValue(property->key());
4108 __ CallRuntime(is_strict(language_mode())
4109 ? Runtime::kDeleteProperty_Strict
4110 : Runtime::kDeleteProperty_Sloppy);
4111 context()->Plug(v0);
4112 } else if (proxy != NULL) {
4113 Variable* var = proxy->var();
4114 // Delete of an unqualified identifier is disallowed in strict mode but
4115 // "delete this" is allowed.
4116 bool is_this = var->HasThisName(isolate());
4117 DCHECK(is_sloppy(language_mode()) || is_this);
4118 if (var->IsUnallocatedOrGlobalSlot()) {
4119 __ LoadGlobalObject(a2);
4120 __ li(a1, Operand(var->name()));
4121 __ Push(a2, a1);
4122 __ CallRuntime(Runtime::kDeleteProperty_Sloppy);
4123 context()->Plug(v0);
4124 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
4125 // Result of deleting non-global, non-dynamic variables is false.
4126 // The subexpression does not have side effects.
4127 context()->Plug(is_this);
4128 } else {
4129 // Non-global variable. Call the runtime to try to delete from the
4130 // context where the variable was introduced.
4131 DCHECK(!context_register().is(a2));
4132 __ li(a2, Operand(var->name()));
4133 __ Push(context_register(), a2);
4134 __ CallRuntime(Runtime::kDeleteLookupSlot);
4135 context()->Plug(v0);
4136 }
4137 } else {
4138 // Result of deleting non-property, non-variable reference is true.
4139 // The subexpression may have side effects.
4140 VisitForEffect(expr->expression());
4141 context()->Plug(true);
4142 }
4143 break;
4144 }
4145
4146 case Token::VOID: {
4147 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
4148 VisitForEffect(expr->expression());
4149 context()->Plug(Heap::kUndefinedValueRootIndex);
4150 break;
4151 }
4152
4153 case Token::NOT: {
4154 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
4155 if (context()->IsEffect()) {
4156 // Unary NOT has no side effects so it's only necessary to visit the
4157 // subexpression. Match the optimizing compiler by not branching.
4158 VisitForEffect(expr->expression());
4159 } else if (context()->IsTest()) {
4160 const TestContext* test = TestContext::cast(context());
4161 // The labels are swapped for the recursive call.
4162 VisitForControl(expr->expression(),
4163 test->false_label(),
4164 test->true_label(),
4165 test->fall_through());
4166 context()->Plug(test->true_label(), test->false_label());
4167 } else {
4168 // We handle value contexts explicitly rather than simply visiting
4169 // for control and plugging the control flow into the context,
4170 // because we need to prepare a pair of extra administrative AST ids
4171 // for the optimizing compiler.
4172 DCHECK(context()->IsAccumulatorValue() || context()->IsStackValue());
4173 Label materialize_true, materialize_false, done;
4174 VisitForControl(expr->expression(),
4175 &materialize_false,
4176 &materialize_true,
4177 &materialize_true);
4178 __ bind(&materialize_true);
4179 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
4180 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
4181 if (context()->IsStackValue()) __ push(v0);
4182 __ jmp(&done);
4183 __ bind(&materialize_false);
4184 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
4185 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
4186 if (context()->IsStackValue()) __ push(v0);
4187 __ bind(&done);
4188 }
4189 break;
4190 }
4191
4192 case Token::TYPEOF: {
4193 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
4194 {
4195 AccumulatorValueContext context(this);
4196 VisitForTypeofValue(expr->expression());
4197 }
4198 __ mov(a3, v0);
4199 TypeofStub typeof_stub(isolate());
4200 __ CallStub(&typeof_stub);
4201 context()->Plug(v0);
4202 break;
4203 }
4204
4205 default:
4206 UNREACHABLE();
4207 }
4208}
4209
4210
4211void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
4212 DCHECK(expr->expression()->IsValidReferenceExpressionOrThis());
4213
4214 Comment cmnt(masm_, "[ CountOperation");
4215
4216 Property* prop = expr->expression()->AsProperty();
4217 LhsKind assign_type = Property::GetAssignType(prop);
4218
4219 // Evaluate expression and get value.
4220 if (assign_type == VARIABLE) {
4221 DCHECK(expr->expression()->AsVariableProxy()->var() != NULL);
4222 AccumulatorValueContext context(this);
4223 EmitVariableLoad(expr->expression()->AsVariableProxy());
4224 } else {
4225 // Reserve space for result of postfix operation.
4226 if (expr->is_postfix() && !context()->IsEffect()) {
4227 __ li(at, Operand(Smi::FromInt(0)));
4228 __ push(at);
4229 }
4230 switch (assign_type) {
4231 case NAMED_PROPERTY: {
4232 // Put the object both on the stack and in the register.
4233 VisitForStackValue(prop->obj());
4234 __ lw(LoadDescriptor::ReceiverRegister(), MemOperand(sp, 0));
4235 EmitNamedPropertyLoad(prop);
4236 break;
4237 }
4238
4239 case NAMED_SUPER_PROPERTY: {
4240 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4241 VisitForAccumulatorValue(
4242 prop->obj()->AsSuperPropertyReference()->home_object());
4243 __ Push(result_register());
4244 const Register scratch = a1;
4245 __ lw(scratch, MemOperand(sp, kPointerSize));
4246 __ Push(scratch, result_register());
4247 EmitNamedSuperPropertyLoad(prop);
4248 break;
4249 }
4250
4251 case KEYED_SUPER_PROPERTY: {
4252 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
4253 VisitForAccumulatorValue(
4254 prop->obj()->AsSuperPropertyReference()->home_object());
4255 const Register scratch = a1;
4256 const Register scratch1 = t0;
4257 __ Move(scratch, result_register());
4258 VisitForAccumulatorValue(prop->key());
4259 __ Push(scratch, result_register());
4260 __ lw(scratch1, MemOperand(sp, 2 * kPointerSize));
4261 __ Push(scratch1, scratch, result_register());
4262 EmitKeyedSuperPropertyLoad(prop);
4263 break;
4264 }
4265
4266 case KEYED_PROPERTY: {
4267 VisitForStackValue(prop->obj());
4268 VisitForStackValue(prop->key());
4269 __ lw(LoadDescriptor::ReceiverRegister(),
4270 MemOperand(sp, 1 * kPointerSize));
4271 __ lw(LoadDescriptor::NameRegister(), MemOperand(sp, 0));
4272 EmitKeyedPropertyLoad(prop);
4273 break;
4274 }
4275
4276 case VARIABLE:
4277 UNREACHABLE();
4278 }
4279 }
4280
4281 // We need a second deoptimization point after loading the value
4282 // in case evaluating the property load my have a side effect.
4283 if (assign_type == VARIABLE) {
4284 PrepareForBailout(expr->expression(), TOS_REG);
4285 } else {
4286 PrepareForBailoutForId(prop->LoadId(), TOS_REG);
4287 }
4288
4289 // Inline smi case if we are in a loop.
4290 Label stub_call, done;
4291 JumpPatchSite patch_site(masm_);
4292
4293 int count_value = expr->op() == Token::INC ? 1 : -1;
4294 __ mov(a0, v0);
4295 if (ShouldInlineSmiCase(expr->op())) {
4296 Label slow;
4297 patch_site.EmitJumpIfNotSmi(v0, &slow);
4298
4299 // Save result for postfix expressions.
4300 if (expr->is_postfix()) {
4301 if (!context()->IsEffect()) {
4302 // Save the result on the stack. If we have a named or keyed property
4303 // we store the result under the receiver that is currently on top
4304 // of the stack.
4305 switch (assign_type) {
4306 case VARIABLE:
4307 __ push(v0);
4308 break;
4309 case NAMED_PROPERTY:
4310 __ sw(v0, MemOperand(sp, kPointerSize));
4311 break;
4312 case NAMED_SUPER_PROPERTY:
4313 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4314 break;
4315 case KEYED_PROPERTY:
4316 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4317 break;
4318 case KEYED_SUPER_PROPERTY:
4319 __ sw(v0, MemOperand(sp, 3 * kPointerSize));
4320 break;
4321 }
4322 }
4323 }
4324
4325 Register scratch1 = a1;
4326 __ li(scratch1, Operand(Smi::FromInt(count_value)));
4327 __ AddBranchNoOvf(v0, v0, Operand(scratch1), &done);
4328 // Call stub. Undo operation first.
4329 __ Move(v0, a0);
4330 __ jmp(&stub_call);
4331 __ bind(&slow);
4332 }
4333 if (!is_strong(language_mode())) {
4334 ToNumberStub convert_stub(isolate());
4335 __ CallStub(&convert_stub);
4336 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
4337 }
4338
4339 // Save result for postfix expressions.
4340 if (expr->is_postfix()) {
4341 if (!context()->IsEffect()) {
4342 // Save the result on the stack. If we have a named or keyed property
4343 // we store the result under the receiver that is currently on top
4344 // of the stack.
4345 switch (assign_type) {
4346 case VARIABLE:
4347 __ push(v0);
4348 break;
4349 case NAMED_PROPERTY:
4350 __ sw(v0, MemOperand(sp, kPointerSize));
4351 break;
4352 case NAMED_SUPER_PROPERTY:
4353 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4354 break;
4355 case KEYED_PROPERTY:
4356 __ sw(v0, MemOperand(sp, 2 * kPointerSize));
4357 break;
4358 case KEYED_SUPER_PROPERTY:
4359 __ sw(v0, MemOperand(sp, 3 * kPointerSize));
4360 break;
4361 }
4362 }
4363 }
4364
4365 __ bind(&stub_call);
4366 __ mov(a1, v0);
4367 __ li(a0, Operand(Smi::FromInt(count_value)));
4368
4369 SetExpressionPosition(expr);
4370
4371
4372 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), Token::ADD,
4373 strength(language_mode())).code();
4374 CallIC(code, expr->CountBinOpFeedbackId());
4375 patch_site.EmitPatchInfo();
4376 __ bind(&done);
4377
4378 if (is_strong(language_mode())) {
4379 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
4380 }
4381 // Store the value returned in v0.
4382 switch (assign_type) {
4383 case VARIABLE:
4384 if (expr->is_postfix()) {
4385 { EffectContext context(this);
4386 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4387 Token::ASSIGN, expr->CountSlot());
4388 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4389 context.Plug(v0);
4390 }
4391 // For all contexts except EffectConstant we have the result on
4392 // top of the stack.
4393 if (!context()->IsEffect()) {
4394 context()->PlugTOS();
4395 }
4396 } else {
4397 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
4398 Token::ASSIGN, expr->CountSlot());
4399 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4400 context()->Plug(v0);
4401 }
4402 break;
4403 case NAMED_PROPERTY: {
4404 __ mov(StoreDescriptor::ValueRegister(), result_register());
4405 __ li(StoreDescriptor::NameRegister(),
4406 Operand(prop->key()->AsLiteral()->value()));
4407 __ pop(StoreDescriptor::ReceiverRegister());
4408 EmitLoadStoreICSlot(expr->CountSlot());
4409 CallStoreIC();
4410 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4411 if (expr->is_postfix()) {
4412 if (!context()->IsEffect()) {
4413 context()->PlugTOS();
4414 }
4415 } else {
4416 context()->Plug(v0);
4417 }
4418 break;
4419 }
4420 case NAMED_SUPER_PROPERTY: {
4421 EmitNamedSuperPropertyStore(prop);
4422 if (expr->is_postfix()) {
4423 if (!context()->IsEffect()) {
4424 context()->PlugTOS();
4425 }
4426 } else {
4427 context()->Plug(v0);
4428 }
4429 break;
4430 }
4431 case KEYED_SUPER_PROPERTY: {
4432 EmitKeyedSuperPropertyStore(prop);
4433 if (expr->is_postfix()) {
4434 if (!context()->IsEffect()) {
4435 context()->PlugTOS();
4436 }
4437 } else {
4438 context()->Plug(v0);
4439 }
4440 break;
4441 }
4442 case KEYED_PROPERTY: {
4443 __ mov(StoreDescriptor::ValueRegister(), result_register());
4444 __ Pop(StoreDescriptor::ReceiverRegister(),
4445 StoreDescriptor::NameRegister());
4446 Handle<Code> ic =
4447 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
4448 EmitLoadStoreICSlot(expr->CountSlot());
4449 CallIC(ic);
4450 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
4451 if (expr->is_postfix()) {
4452 if (!context()->IsEffect()) {
4453 context()->PlugTOS();
4454 }
4455 } else {
4456 context()->Plug(v0);
4457 }
4458 break;
4459 }
4460 }
4461}
4462
4463
4464void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
4465 Expression* sub_expr,
4466 Handle<String> check) {
4467 Label materialize_true, materialize_false;
4468 Label* if_true = NULL;
4469 Label* if_false = NULL;
4470 Label* fall_through = NULL;
4471 context()->PrepareTest(&materialize_true, &materialize_false,
4472 &if_true, &if_false, &fall_through);
4473
4474 { AccumulatorValueContext context(this);
4475 VisitForTypeofValue(sub_expr);
4476 }
4477 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4478
4479 Factory* factory = isolate()->factory();
4480 if (String::Equals(check, factory->number_string())) {
4481 __ JumpIfSmi(v0, if_true);
4482 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4483 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
4484 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4485 } else if (String::Equals(check, factory->string_string())) {
4486 __ JumpIfSmi(v0, if_false);
4487 __ GetObjectType(v0, v0, a1);
4488 Split(lt, a1, Operand(FIRST_NONSTRING_TYPE), if_true, if_false,
4489 fall_through);
4490 } else if (String::Equals(check, factory->symbol_string())) {
4491 __ JumpIfSmi(v0, if_false);
4492 __ GetObjectType(v0, v0, a1);
4493 Split(eq, a1, Operand(SYMBOL_TYPE), if_true, if_false, fall_through);
4494 } else if (String::Equals(check, factory->boolean_string())) {
4495 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4496 __ Branch(if_true, eq, v0, Operand(at));
4497 __ LoadRoot(at, Heap::kFalseValueRootIndex);
4498 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4499 } else if (String::Equals(check, factory->undefined_string())) {
4500 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4501 __ Branch(if_true, eq, v0, Operand(at));
4502 __ JumpIfSmi(v0, if_false);
4503 // Check for undetectable objects => true.
4504 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4505 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4506 __ And(a1, a1, Operand(1 << Map::kIsUndetectable));
4507 Split(ne, a1, Operand(zero_reg), if_true, if_false, fall_through);
4508 } else if (String::Equals(check, factory->function_string())) {
4509 __ JumpIfSmi(v0, if_false);
4510 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset));
4511 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4512 __ And(a1, a1,
4513 Operand((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable)));
4514 Split(eq, a1, Operand(1 << Map::kIsCallable), if_true, if_false,
4515 fall_through);
4516 } else if (String::Equals(check, factory->object_string())) {
4517 __ JumpIfSmi(v0, if_false);
4518 __ LoadRoot(at, Heap::kNullValueRootIndex);
4519 __ Branch(if_true, eq, v0, Operand(at));
4520 STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
4521 __ GetObjectType(v0, v0, a1);
4522 __ Branch(if_false, lt, a1, Operand(FIRST_JS_RECEIVER_TYPE));
4523 // Check for callable or undetectable objects => false.
4524 __ lbu(a1, FieldMemOperand(v0, Map::kBitFieldOffset));
4525 __ And(a1, a1,
4526 Operand((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable)));
4527 Split(eq, a1, Operand(zero_reg), if_true, if_false, fall_through);
4528// clang-format off
4529#define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
4530 } else if (String::Equals(check, factory->type##_string())) { \
4531 __ JumpIfSmi(v0, if_false); \
4532 __ lw(v0, FieldMemOperand(v0, HeapObject::kMapOffset)); \
4533 __ LoadRoot(at, Heap::k##Type##MapRootIndex); \
4534 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4535 SIMD128_TYPES(SIMD128_TYPE)
4536#undef SIMD128_TYPE
4537 // clang-format on
4538 } else {
4539 if (if_false != fall_through) __ jmp(if_false);
4540 }
4541 context()->Plug(if_true, if_false);
4542}
4543
4544
4545void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
4546 Comment cmnt(masm_, "[ CompareOperation");
4547 SetExpressionPosition(expr);
4548
4549 // First we try a fast inlined version of the compare when one of
4550 // the operands is a literal.
4551 if (TryLiteralCompare(expr)) return;
4552
4553 // Always perform the comparison for its control flow. Pack the result
4554 // into the expression's context after the comparison is performed.
4555 Label materialize_true, materialize_false;
4556 Label* if_true = NULL;
4557 Label* if_false = NULL;
4558 Label* fall_through = NULL;
4559 context()->PrepareTest(&materialize_true, &materialize_false,
4560 &if_true, &if_false, &fall_through);
4561
4562 Token::Value op = expr->op();
4563 VisitForStackValue(expr->left());
4564 switch (op) {
4565 case Token::IN:
4566 VisitForStackValue(expr->right());
4567 __ CallRuntime(Runtime::kHasProperty);
4568 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
4569 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
4570 Split(eq, v0, Operand(t0), if_true, if_false, fall_through);
4571 break;
4572
4573 case Token::INSTANCEOF: {
4574 VisitForAccumulatorValue(expr->right());
4575 __ mov(a0, result_register());
4576 __ pop(a1);
4577 InstanceOfStub stub(isolate());
4578 __ CallStub(&stub);
4579 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
4580 __ LoadRoot(at, Heap::kTrueValueRootIndex);
4581 Split(eq, v0, Operand(at), if_true, if_false, fall_through);
4582 break;
4583 }
4584
4585 default: {
4586 VisitForAccumulatorValue(expr->right());
4587 Condition cc = CompareIC::ComputeCondition(op);
4588 __ mov(a0, result_register());
4589 __ pop(a1);
4590
4591 bool inline_smi_code = ShouldInlineSmiCase(op);
4592 JumpPatchSite patch_site(masm_);
4593 if (inline_smi_code) {
4594 Label slow_case;
4595 __ Or(a2, a0, Operand(a1));
4596 patch_site.EmitJumpIfNotSmi(a2, &slow_case);
4597 Split(cc, a1, Operand(a0), if_true, if_false, NULL);
4598 __ bind(&slow_case);
4599 }
4600
4601 Handle<Code> ic = CodeFactory::CompareIC(
4602 isolate(), op, strength(language_mode())).code();
4603 CallIC(ic, expr->CompareOperationFeedbackId());
4604 patch_site.EmitPatchInfo();
4605 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4606 Split(cc, v0, Operand(zero_reg), if_true, if_false, fall_through);
4607 }
4608 }
4609
4610 // Convert the result of the comparison into one expected for this
4611 // expression's context.
4612 context()->Plug(if_true, if_false);
4613}
4614
4615
4616void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
4617 Expression* sub_expr,
4618 NilValue nil) {
4619 Label materialize_true, materialize_false;
4620 Label* if_true = NULL;
4621 Label* if_false = NULL;
4622 Label* fall_through = NULL;
4623 context()->PrepareTest(&materialize_true, &materialize_false,
4624 &if_true, &if_false, &fall_through);
4625
4626 VisitForAccumulatorValue(sub_expr);
4627 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
4628 __ mov(a0, result_register());
4629 if (expr->op() == Token::EQ_STRICT) {
4630 Heap::RootListIndex nil_value = nil == kNullValue ?
4631 Heap::kNullValueRootIndex :
4632 Heap::kUndefinedValueRootIndex;
4633 __ LoadRoot(a1, nil_value);
4634 Split(eq, a0, Operand(a1), if_true, if_false, fall_through);
4635 } else {
4636 Handle<Code> ic = CompareNilICStub::GetUninitialized(isolate(), nil);
4637 CallIC(ic, expr->CompareOperationFeedbackId());
4638 __ LoadRoot(a1, Heap::kTrueValueRootIndex);
4639 Split(eq, v0, Operand(a1), if_true, if_false, fall_through);
4640 }
4641 context()->Plug(if_true, if_false);
4642}
4643
4644
4645void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
4646 __ lw(v0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4647 context()->Plug(v0);
4648}
4649
4650
4651Register FullCodeGenerator::result_register() {
4652 return v0;
4653}
4654
4655
4656Register FullCodeGenerator::context_register() {
4657 return cp;
4658}
4659
4660
4661void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
4662 DCHECK_EQ(POINTER_SIZE_ALIGN(frame_offset), frame_offset);
4663 __ sw(value, MemOperand(fp, frame_offset));
4664}
4665
4666
4667void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
4668 __ lw(dst, ContextMemOperand(cp, context_index));
4669}
4670
4671
4672void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
4673 Scope* closure_scope = scope()->ClosureScope();
4674 if (closure_scope->is_script_scope() ||
4675 closure_scope->is_module_scope()) {
4676 // Contexts nested in the native context have a canonical empty function
4677 // as their closure, not the anonymous closure containing the global
4678 // code.
4679 __ LoadNativeContextSlot(Context::CLOSURE_INDEX, at);
4680 } else if (closure_scope->is_eval_scope()) {
4681 // Contexts created by a call to eval have the same closure as the
4682 // context calling eval, not the anonymous closure containing the eval
4683 // code. Fetch it from the context.
4684 __ lw(at, ContextMemOperand(cp, Context::CLOSURE_INDEX));
4685 } else {
4686 DCHECK(closure_scope->is_function_scope());
4687 __ lw(at, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4688 }
4689 __ push(at);
4690}
4691
4692
4693// ----------------------------------------------------------------------------
4694// Non-local control flow support.
4695
4696void FullCodeGenerator::EnterFinallyBlock() {
4697 DCHECK(!result_register().is(a1));
4698 // Store result register while executing finally block.
4699 __ push(result_register());
4700 // Cook return address in link register to stack (smi encoded Code* delta).
4701 __ Subu(a1, ra, Operand(masm_->CodeObject()));
4702 DCHECK_EQ(1, kSmiTagSize + kSmiShiftSize);
4703 STATIC_ASSERT(0 == kSmiTag);
4704 __ Addu(a1, a1, Operand(a1)); // Convert to smi.
4705
4706 // Store result register while executing finally block.
4707 __ push(a1);
4708
4709 // Store pending message while executing finally block.
4710 ExternalReference pending_message_obj =
4711 ExternalReference::address_of_pending_message_obj(isolate());
4712 __ li(at, Operand(pending_message_obj));
4713 __ lw(a1, MemOperand(at));
4714 __ push(a1);
4715
4716 ClearPendingMessage();
4717}
4718
4719
4720void FullCodeGenerator::ExitFinallyBlock() {
4721 DCHECK(!result_register().is(a1));
4722 // Restore pending message from stack.
4723 __ pop(a1);
4724 ExternalReference pending_message_obj =
4725 ExternalReference::address_of_pending_message_obj(isolate());
4726 __ li(at, Operand(pending_message_obj));
4727 __ sw(a1, MemOperand(at));
4728
4729 // Restore result register from stack.
4730 __ pop(a1);
4731
4732 // Uncook return address and return.
4733 __ pop(result_register());
4734 DCHECK_EQ(1, kSmiTagSize + kSmiShiftSize);
4735 __ sra(a1, a1, 1); // Un-smi-tag value.
4736 __ Addu(at, a1, Operand(masm_->CodeObject()));
4737 __ Jump(at);
4738}
4739
4740
4741void FullCodeGenerator::ClearPendingMessage() {
4742 DCHECK(!result_register().is(a1));
4743 ExternalReference pending_message_obj =
4744 ExternalReference::address_of_pending_message_obj(isolate());
4745 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
4746 __ li(at, Operand(pending_message_obj));
4747 __ sw(a1, MemOperand(at));
4748}
4749
4750
4751void FullCodeGenerator::EmitLoadStoreICSlot(FeedbackVectorSlot slot) {
4752 DCHECK(!slot.IsInvalid());
4753 __ li(VectorStoreICTrampolineDescriptor::SlotRegister(),
4754 Operand(SmiFromSlot(slot)));
4755}
4756
4757
4758#undef __
4759
4760
4761void BackEdgeTable::PatchAt(Code* unoptimized_code,
4762 Address pc,
4763 BackEdgeState target_state,
4764 Code* replacement_code) {
4765 static const int kInstrSize = Assembler::kInstrSize;
4766 Address branch_address = pc - 6 * kInstrSize;
4767 Isolate* isolate = unoptimized_code->GetIsolate();
4768 CodePatcher patcher(isolate, branch_address, 1);
4769
4770 switch (target_state) {
4771 case INTERRUPT:
4772 // slt at, a3, zero_reg (in case of count based interrupts)
4773 // beq at, zero_reg, ok
4774 // lui t9, <interrupt stub address> upper
4775 // ori t9, <interrupt stub address> lower
4776 // jalr t9
4777 // nop
4778 // ok-label ----- pc_after points here
4779 patcher.masm()->slt(at, a3, zero_reg);
4780 break;
4781 case ON_STACK_REPLACEMENT:
4782 case OSR_AFTER_STACK_CHECK:
4783 // addiu at, zero_reg, 1
4784 // beq at, zero_reg, ok ;; Not changed
4785 // lui t9, <on-stack replacement address> upper
4786 // ori t9, <on-stack replacement address> lower
4787 // jalr t9 ;; Not changed
4788 // nop ;; Not changed
4789 // ok-label ----- pc_after points here
4790 patcher.masm()->addiu(at, zero_reg, 1);
4791 break;
4792 }
4793 Address pc_immediate_load_address = pc - 4 * kInstrSize;
4794 // Replace the stack check address in the load-immediate (lui/ori pair)
4795 // with the entry address of the replacement code.
4796 Assembler::set_target_address_at(isolate, pc_immediate_load_address,
4797 replacement_code->entry());
4798
4799 unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
4800 unoptimized_code, pc_immediate_load_address, replacement_code);
4801}
4802
4803
4804BackEdgeTable::BackEdgeState BackEdgeTable::GetBackEdgeState(
4805 Isolate* isolate,
4806 Code* unoptimized_code,
4807 Address pc) {
4808 static const int kInstrSize = Assembler::kInstrSize;
4809 Address branch_address = pc - 6 * kInstrSize;
4810 Address pc_immediate_load_address = pc - 4 * kInstrSize;
4811
4812 DCHECK(Assembler::IsBeq(Assembler::instr_at(pc - 5 * kInstrSize)));
4813 if (!Assembler::IsAddImmediate(Assembler::instr_at(branch_address))) {
4814 DCHECK(reinterpret_cast<uint32_t>(
4815 Assembler::target_address_at(pc_immediate_load_address)) ==
4816 reinterpret_cast<uint32_t>(
4817 isolate->builtins()->InterruptCheck()->entry()));
4818 return INTERRUPT;
4819 }
4820
4821 DCHECK(Assembler::IsAddImmediate(Assembler::instr_at(branch_address)));
4822
4823 if (reinterpret_cast<uint32_t>(
4824 Assembler::target_address_at(pc_immediate_load_address)) ==
4825 reinterpret_cast<uint32_t>(
4826 isolate->builtins()->OnStackReplacement()->entry())) {
4827 return ON_STACK_REPLACEMENT;
4828 }
4829
4830 DCHECK(reinterpret_cast<uint32_t>(
4831 Assembler::target_address_at(pc_immediate_load_address)) ==
4832 reinterpret_cast<uint32_t>(
4833 isolate->builtins()->OsrAfterStackCheck()->entry()));
4834 return OSR_AFTER_STACK_CHECK;
4835}
4836
4837
4838} // namespace internal
4839} // namespace v8
4840
4841#endif // V8_TARGET_ARCH_MIPS