Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: src/a64/full-codegen-a64.cc

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

Powered by Google App Engine
This is Rietveld 408576698