blob: 910b2cf9f062b578ba7d26d74943e85c3a1856d9 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#if V8_TARGET_ARCH_X64
6
7#include "src/ast/scopes.h"
8#include "src/code-factory.h"
9#include "src/code-stubs.h"
10#include "src/codegen.h"
11#include "src/debug/debug.h"
12#include "src/full-codegen/full-codegen.h"
13#include "src/ic/ic.h"
14#include "src/parsing/parser.h"
15
16namespace v8 {
17namespace internal {
18
Ben Murdoch097c5b22016-05-18 11:27:45 +010019#define __ ACCESS_MASM(masm())
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000020
21class JumpPatchSite BASE_EMBEDDED {
22 public:
23 explicit JumpPatchSite(MacroAssembler* masm) : masm_(masm) {
24#ifdef DEBUG
25 info_emitted_ = false;
26#endif
27 }
28
29 ~JumpPatchSite() {
30 DCHECK(patch_site_.is_bound() == info_emitted_);
31 }
32
33 void EmitJumpIfNotSmi(Register reg,
34 Label* target,
35 Label::Distance near_jump = Label::kFar) {
36 __ testb(reg, Immediate(kSmiTagMask));
37 EmitJump(not_carry, target, near_jump); // Always taken before patched.
38 }
39
40 void EmitJumpIfSmi(Register reg,
41 Label* target,
42 Label::Distance near_jump = Label::kFar) {
43 __ testb(reg, Immediate(kSmiTagMask));
44 EmitJump(carry, target, near_jump); // Never taken before patched.
45 }
46
47 void EmitPatchInfo() {
48 if (patch_site_.is_bound()) {
49 int delta_to_patch_site = masm_->SizeOfCodeGeneratedSince(&patch_site_);
50 DCHECK(is_uint8(delta_to_patch_site));
51 __ testl(rax, Immediate(delta_to_patch_site));
52#ifdef DEBUG
53 info_emitted_ = true;
54#endif
55 } else {
56 __ nop(); // Signals no inlined code.
57 }
58 }
59
60 private:
61 // jc will be patched with jz, jnc will become jnz.
62 void EmitJump(Condition cc, Label* target, Label::Distance near_jump) {
63 DCHECK(!patch_site_.is_bound() && !info_emitted_);
64 DCHECK(cc == carry || cc == not_carry);
65 __ bind(&patch_site_);
66 __ j(cc, target, near_jump);
67 }
68
Ben Murdoch097c5b22016-05-18 11:27:45 +010069 MacroAssembler* masm() { return masm_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000070 MacroAssembler* masm_;
71 Label patch_site_;
72#ifdef DEBUG
73 bool info_emitted_;
74#endif
75};
76
77
78// Generate code for a JS function. On entry to the function the receiver
79// and arguments have been pushed on the stack left to right, with the
80// return address on top of them. The actual argument count matches the
81// formal parameter count expected by the function.
82//
83// The live registers are:
84// o rdi: the JS function object being called (i.e. ourselves)
85// o rdx: the new target value
86// o rsi: our context
87// o rbp: our caller's frame pointer
88// o rsp: stack pointer (pointing to return address)
89//
90// The function builds a JS frame. Please see JavaScriptFrameConstants in
91// frames-x64.h for its layout.
92void FullCodeGenerator::Generate() {
93 CompilationInfo* info = info_;
94 profiling_counter_ = isolate()->factory()->NewCell(
95 Handle<Smi>(Smi::FromInt(FLAG_interrupt_budget), isolate()));
96 SetFunctionPosition(literal());
97 Comment cmnt(masm_, "[ function compiled by full code generator");
98
99 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
100
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000101 if (FLAG_debug_code && info->ExpectsJSReceiverAsReceiver()) {
102 StackArgumentsAccessor args(rsp, info->scope()->num_parameters());
103 __ movp(rcx, args.GetReceiverOperand());
104 __ AssertNotSmi(rcx);
105 __ CmpObjectType(rcx, FIRST_JS_RECEIVER_TYPE, rcx);
106 __ Assert(above_equal, kSloppyFunctionExpectsJSReceiverReceiver);
107 }
108
109 // Open a frame scope to indicate that there is a frame on the stack. The
110 // MANUAL indicates that the scope shouldn't actually generate code to set up
111 // the frame (that is done below).
112 FrameScope frame_scope(masm_, StackFrame::MANUAL);
113
114 info->set_prologue_offset(masm_->pc_offset());
115 __ Prologue(info->GeneratePreagedPrologue());
116
117 { Comment cmnt(masm_, "[ Allocate locals");
118 int locals_count = info->scope()->num_stack_slots();
119 // Generators allocate locals, if any, in context slots.
120 DCHECK(!IsGeneratorFunction(info->literal()->kind()) || locals_count == 0);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100121 OperandStackDepthIncrement(locals_count);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000122 if (locals_count == 1) {
123 __ PushRoot(Heap::kUndefinedValueRootIndex);
124 } else if (locals_count > 1) {
125 if (locals_count >= 128) {
126 Label ok;
127 __ movp(rcx, rsp);
128 __ subp(rcx, Immediate(locals_count * kPointerSize));
129 __ CompareRoot(rcx, Heap::kRealStackLimitRootIndex);
130 __ j(above_equal, &ok, Label::kNear);
131 __ CallRuntime(Runtime::kThrowStackOverflow);
132 __ bind(&ok);
133 }
134 __ LoadRoot(rax, Heap::kUndefinedValueRootIndex);
135 const int kMaxPushes = 32;
136 if (locals_count >= kMaxPushes) {
137 int loop_iterations = locals_count / kMaxPushes;
138 __ movp(rcx, Immediate(loop_iterations));
139 Label loop_header;
140 __ bind(&loop_header);
141 // Do pushes.
142 for (int i = 0; i < kMaxPushes; i++) {
143 __ Push(rax);
144 }
145 // Continue loop if not done.
146 __ decp(rcx);
147 __ j(not_zero, &loop_header, Label::kNear);
148 }
149 int remaining = locals_count % kMaxPushes;
150 // Emit the remaining pushes.
151 for (int i = 0; i < remaining; i++) {
152 __ Push(rax);
153 }
154 }
155 }
156
157 bool function_in_register = true;
158
159 // Possibly allocate a local context.
160 if (info->scope()->num_heap_slots() > 0) {
161 Comment cmnt(masm_, "[ Allocate context");
162 bool need_write_barrier = true;
163 int slots = info->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
164 // Argument to NewContext is the function, which is still in rdi.
165 if (info->scope()->is_script_scope()) {
166 __ Push(rdi);
167 __ Push(info->scope()->GetScopeInfo(info->isolate()));
168 __ CallRuntime(Runtime::kNewScriptContext);
169 PrepareForBailoutForId(BailoutId::ScriptContext(), TOS_REG);
170 // The new target value is not used, clobbering is safe.
171 DCHECK_NULL(info->scope()->new_target_var());
172 } else {
173 if (info->scope()->new_target_var() != nullptr) {
174 __ Push(rdx); // Preserve new target.
175 }
176 if (slots <= FastNewContextStub::kMaximumSlots) {
177 FastNewContextStub stub(isolate(), slots);
178 __ CallStub(&stub);
179 // Result of FastNewContextStub is always in new space.
180 need_write_barrier = false;
181 } else {
182 __ Push(rdi);
183 __ CallRuntime(Runtime::kNewFunctionContext);
184 }
185 if (info->scope()->new_target_var() != nullptr) {
186 __ Pop(rdx); // Restore new target.
187 }
188 }
189 function_in_register = false;
190 // Context is returned in rax. It replaces the context passed to us.
191 // It's saved in the stack and kept live in rsi.
192 __ movp(rsi, rax);
193 __ movp(Operand(rbp, StandardFrameConstants::kContextOffset), rax);
194
195 // Copy any necessary parameters into the context.
196 int num_parameters = info->scope()->num_parameters();
197 int first_parameter = info->scope()->has_this_declaration() ? -1 : 0;
198 for (int i = first_parameter; i < num_parameters; i++) {
199 Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
200 if (var->IsContextSlot()) {
201 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
202 (num_parameters - 1 - i) * kPointerSize;
203 // Load parameter from stack.
204 __ movp(rax, Operand(rbp, parameter_offset));
205 // Store it in the context.
206 int context_offset = Context::SlotOffset(var->index());
207 __ movp(Operand(rsi, context_offset), rax);
208 // Update the write barrier. This clobbers rax and rbx.
209 if (need_write_barrier) {
210 __ RecordWriteContextSlot(
211 rsi, context_offset, rax, rbx, kDontSaveFPRegs);
212 } else if (FLAG_debug_code) {
213 Label done;
214 __ JumpIfInNewSpace(rsi, rax, &done, Label::kNear);
215 __ Abort(kExpectedNewSpaceObject);
216 __ bind(&done);
217 }
218 }
219 }
220 }
221
222 // Register holding this function and new target are both trashed in case we
223 // bailout here. But since that can happen only when new target is not used
224 // and we allocate a context, the value of |function_in_register| is correct.
225 PrepareForBailoutForId(BailoutId::FunctionContext(), NO_REGISTERS);
226
227 // Possibly set up a local binding to the this function which is used in
228 // derived constructors with super calls.
229 Variable* this_function_var = scope()->this_function_var();
230 if (this_function_var != nullptr) {
231 Comment cmnt(masm_, "[ This function");
232 if (!function_in_register) {
233 __ movp(rdi, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
234 // The write barrier clobbers register again, keep it marked as such.
235 }
236 SetVar(this_function_var, rdi, rbx, rcx);
237 }
238
239 // Possibly set up a local binding to the new target value.
240 Variable* new_target_var = scope()->new_target_var();
241 if (new_target_var != nullptr) {
242 Comment cmnt(masm_, "[ new.target");
243 SetVar(new_target_var, rdx, rbx, rcx);
244 }
245
246 // Possibly allocate RestParameters
247 int rest_index;
248 Variable* rest_param = scope()->rest_parameter(&rest_index);
249 if (rest_param) {
250 Comment cmnt(masm_, "[ Allocate rest parameter array");
Ben Murdoch097c5b22016-05-18 11:27:45 +0100251 if (!function_in_register) {
252 __ movp(rdi, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
253 }
254 FastNewRestParameterStub stub(isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000255 __ CallStub(&stub);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100256 function_in_register = false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000257 SetVar(rest_param, rax, rbx, rdx);
258 }
259
260 // Possibly allocate an arguments object.
261 Variable* arguments = scope()->arguments();
262 if (arguments != NULL) {
263 // Arguments object must be allocated after the context object, in
264 // case the "arguments" or ".arguments" variables are in the context.
265 Comment cmnt(masm_, "[ Allocate arguments object");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000266 if (!function_in_register) {
267 __ movp(rdi, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
268 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100269 if (is_strict(language_mode()) || !has_simple_parameters()) {
270 FastNewStrictArgumentsStub stub(isolate());
271 __ CallStub(&stub);
272 } else if (literal()->has_duplicate_parameters()) {
273 __ Push(rdi);
274 __ CallRuntime(Runtime::kNewSloppyArguments_Generic);
275 } else {
276 FastNewSloppyArgumentsStub stub(isolate());
277 __ CallStub(&stub);
278 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000279
280 SetVar(arguments, rax, rbx, rdx);
281 }
282
283 if (FLAG_trace) {
284 __ CallRuntime(Runtime::kTraceEnter);
285 }
286
287 // Visit the declarations and body unless there is an illegal
288 // redeclaration.
289 if (scope()->HasIllegalRedeclaration()) {
290 Comment cmnt(masm_, "[ Declarations");
291 VisitForEffect(scope()->GetIllegalRedeclaration());
292
293 } else {
294 PrepareForBailoutForId(BailoutId::FunctionEntry(), NO_REGISTERS);
295 { Comment cmnt(masm_, "[ Declarations");
296 VisitDeclarations(scope()->declarations());
297 }
298
299 // Assert that the declarations do not use ICs. Otherwise the debugger
300 // won't be able to redirect a PC at an IC to the correct IC in newly
301 // recompiled code.
302 DCHECK_EQ(0, ic_total_count_);
303
304 { Comment cmnt(masm_, "[ Stack check");
305 PrepareForBailoutForId(BailoutId::Declarations(), NO_REGISTERS);
306 Label ok;
307 __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
308 __ j(above_equal, &ok, Label::kNear);
309 __ call(isolate()->builtins()->StackCheck(), RelocInfo::CODE_TARGET);
310 __ bind(&ok);
311 }
312
313 { Comment cmnt(masm_, "[ Body");
314 DCHECK(loop_depth() == 0);
315 VisitStatements(literal()->body());
316 DCHECK(loop_depth() == 0);
317 }
318 }
319
320 // Always emit a 'return undefined' in case control fell off the end of
321 // the body.
322 { Comment cmnt(masm_, "[ return <undefined>;");
323 __ LoadRoot(rax, Heap::kUndefinedValueRootIndex);
324 EmitReturnSequence();
325 }
326}
327
328
329void FullCodeGenerator::ClearAccumulator() {
330 __ Set(rax, 0);
331}
332
333
334void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
335 __ Move(rbx, profiling_counter_, RelocInfo::EMBEDDED_OBJECT);
336 __ SmiAddConstant(FieldOperand(rbx, Cell::kValueOffset),
337 Smi::FromInt(-delta));
338}
339
340
341void FullCodeGenerator::EmitProfilingCounterReset() {
342 int reset_value = FLAG_interrupt_budget;
343 __ Move(rbx, profiling_counter_, RelocInfo::EMBEDDED_OBJECT);
344 __ Move(kScratchRegister, Smi::FromInt(reset_value));
345 __ movp(FieldOperand(rbx, Cell::kValueOffset), kScratchRegister);
346}
347
348
349static const byte kJnsOffset = kPointerSize == kInt64Size ? 0x1d : 0x14;
350
351
352void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
353 Label* back_edge_target) {
354 Comment cmnt(masm_, "[ Back edge bookkeeping");
355 Label ok;
356
357 DCHECK(back_edge_target->is_bound());
358 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
359 int weight = Min(kMaxBackEdgeWeight,
360 Max(1, distance / kCodeSizeMultiplier));
361 EmitProfilingCounterDecrement(weight);
362
363 __ j(positive, &ok, Label::kNear);
364 {
365 PredictableCodeSizeScope predictible_code_size_scope(masm_, kJnsOffset);
366 DontEmitDebugCodeScope dont_emit_debug_code_scope(masm_);
367 __ call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
368
369 // Record a mapping of this PC offset to the OSR id. This is used to find
370 // the AST id from the unoptimized code in order to use it as a key into
371 // the deoptimization input data found in the optimized code.
372 RecordBackEdge(stmt->OsrEntryId());
373
374 EmitProfilingCounterReset();
375 }
376 __ bind(&ok);
377
378 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
379 // Record a mapping of the OSR id to this PC. This is used if the OSR
380 // entry becomes the target of a bailout. We don't expect it to be, but
381 // we want it to work if it is.
382 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
383}
384
Ben Murdoch097c5b22016-05-18 11:27:45 +0100385void FullCodeGenerator::EmitProfilingCounterHandlingForReturnSequence(
386 bool is_tail_call) {
387 // Pretend that the exit is a backwards jump to the entry.
388 int weight = 1;
389 if (info_->ShouldSelfOptimize()) {
390 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
391 } else {
392 int distance = masm_->pc_offset();
393 weight = Min(kMaxBackEdgeWeight, Max(1, distance / kCodeSizeMultiplier));
394 }
395 EmitProfilingCounterDecrement(weight);
396 Label ok;
397 __ j(positive, &ok, Label::kNear);
398 // Don't need to save result register if we are going to do a tail call.
399 if (!is_tail_call) {
400 __ Push(rax);
401 }
402 __ call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
403 if (!is_tail_call) {
404 __ Pop(rax);
405 }
406 EmitProfilingCounterReset();
407 __ bind(&ok);
408}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000409
410void FullCodeGenerator::EmitReturnSequence() {
411 Comment cmnt(masm_, "[ Return sequence");
412 if (return_label_.is_bound()) {
413 __ jmp(&return_label_);
414 } else {
415 __ bind(&return_label_);
416 if (FLAG_trace) {
417 __ Push(rax);
418 __ CallRuntime(Runtime::kTraceExit);
419 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100420 EmitProfilingCounterHandlingForReturnSequence(false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000421
422 SetReturnPosition(literal());
423 __ leave();
424
425 int arg_count = info_->scope()->num_parameters() + 1;
426 int arguments_bytes = arg_count * kPointerSize;
427 __ Ret(arguments_bytes, rcx);
428 }
429}
430
431
432void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
433 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
434 MemOperand operand = codegen()->VarOperand(var, result_register());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100435 codegen()->PushOperand(operand);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000436}
437
438
439void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
440}
441
442
443void FullCodeGenerator::AccumulatorValueContext::Plug(
444 Heap::RootListIndex index) const {
445 __ LoadRoot(result_register(), index);
446}
447
448
449void FullCodeGenerator::StackValueContext::Plug(
450 Heap::RootListIndex index) const {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100451 codegen()->OperandStackDepthIncrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000452 __ PushRoot(index);
453}
454
455
456void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
457 codegen()->PrepareForBailoutBeforeSplit(condition(),
458 true,
459 true_label_,
460 false_label_);
461 if (index == Heap::kUndefinedValueRootIndex ||
462 index == Heap::kNullValueRootIndex ||
463 index == Heap::kFalseValueRootIndex) {
464 if (false_label_ != fall_through_) __ jmp(false_label_);
465 } else if (index == Heap::kTrueValueRootIndex) {
466 if (true_label_ != fall_through_) __ jmp(true_label_);
467 } else {
468 __ LoadRoot(result_register(), index);
469 codegen()->DoTest(this);
470 }
471}
472
473
474void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
475}
476
477
478void FullCodeGenerator::AccumulatorValueContext::Plug(
479 Handle<Object> lit) const {
480 if (lit->IsSmi()) {
481 __ SafeMove(result_register(), Smi::cast(*lit));
482 } else {
483 __ Move(result_register(), lit);
484 }
485}
486
487
488void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100489 codegen()->OperandStackDepthIncrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000490 if (lit->IsSmi()) {
491 __ SafePush(Smi::cast(*lit));
492 } else {
493 __ Push(lit);
494 }
495}
496
497
498void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
499 codegen()->PrepareForBailoutBeforeSplit(condition(),
500 true,
501 true_label_,
502 false_label_);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100503 DCHECK(lit->IsNull() || lit->IsUndefined() || !lit->IsUndetectableObject());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000504 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
505 if (false_label_ != fall_through_) __ jmp(false_label_);
506 } else if (lit->IsTrue() || lit->IsJSObject()) {
507 if (true_label_ != fall_through_) __ jmp(true_label_);
508 } else if (lit->IsString()) {
509 if (String::cast(*lit)->length() == 0) {
510 if (false_label_ != fall_through_) __ jmp(false_label_);
511 } else {
512 if (true_label_ != fall_through_) __ jmp(true_label_);
513 }
514 } else if (lit->IsSmi()) {
515 if (Smi::cast(*lit)->value() == 0) {
516 if (false_label_ != fall_through_) __ jmp(false_label_);
517 } else {
518 if (true_label_ != fall_through_) __ jmp(true_label_);
519 }
520 } else {
521 // For simplicity we always test the accumulator register.
522 __ Move(result_register(), lit);
523 codegen()->DoTest(this);
524 }
525}
526
527
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000528void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
529 Register reg) const {
530 DCHECK(count > 0);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100531 if (count > 1) codegen()->DropOperands(count - 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000532 __ movp(Operand(rsp, 0), reg);
533}
534
535
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000536void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
537 Label* materialize_false) const {
538 DCHECK(materialize_true == materialize_false);
539 __ bind(materialize_true);
540}
541
542
543void FullCodeGenerator::AccumulatorValueContext::Plug(
544 Label* materialize_true,
545 Label* materialize_false) const {
546 Label done;
547 __ bind(materialize_true);
548 __ Move(result_register(), isolate()->factory()->true_value());
549 __ jmp(&done, Label::kNear);
550 __ bind(materialize_false);
551 __ Move(result_register(), isolate()->factory()->false_value());
552 __ bind(&done);
553}
554
555
556void FullCodeGenerator::StackValueContext::Plug(
557 Label* materialize_true,
558 Label* materialize_false) const {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100559 codegen()->OperandStackDepthIncrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000560 Label done;
561 __ bind(materialize_true);
562 __ Push(isolate()->factory()->true_value());
563 __ jmp(&done, Label::kNear);
564 __ bind(materialize_false);
565 __ Push(isolate()->factory()->false_value());
566 __ bind(&done);
567}
568
569
570void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
571 Label* materialize_false) const {
572 DCHECK(materialize_true == true_label_);
573 DCHECK(materialize_false == false_label_);
574}
575
576
577void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
578 Heap::RootListIndex value_root_index =
579 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
580 __ LoadRoot(result_register(), value_root_index);
581}
582
583
584void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100585 codegen()->OperandStackDepthIncrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000586 Heap::RootListIndex value_root_index =
587 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
588 __ PushRoot(value_root_index);
589}
590
591
592void FullCodeGenerator::TestContext::Plug(bool flag) const {
593 codegen()->PrepareForBailoutBeforeSplit(condition(),
594 true,
595 true_label_,
596 false_label_);
597 if (flag) {
598 if (true_label_ != fall_through_) __ jmp(true_label_);
599 } else {
600 if (false_label_ != fall_through_) __ jmp(false_label_);
601 }
602}
603
604
605void FullCodeGenerator::DoTest(Expression* condition,
606 Label* if_true,
607 Label* if_false,
608 Label* fall_through) {
609 Handle<Code> ic = ToBooleanStub::GetUninitialized(isolate());
610 CallIC(ic, condition->test_id());
611 __ CompareRoot(result_register(), Heap::kTrueValueRootIndex);
612 Split(equal, if_true, if_false, fall_through);
613}
614
615
616void FullCodeGenerator::Split(Condition cc,
617 Label* if_true,
618 Label* if_false,
619 Label* fall_through) {
620 if (if_false == fall_through) {
621 __ j(cc, if_true);
622 } else if (if_true == fall_through) {
623 __ j(NegateCondition(cc), if_false);
624 } else {
625 __ j(cc, if_true);
626 __ jmp(if_false);
627 }
628}
629
630
631MemOperand FullCodeGenerator::StackOperand(Variable* var) {
632 DCHECK(var->IsStackAllocated());
633 // Offset is negative because higher indexes are at lower addresses.
634 int offset = -var->index() * kPointerSize;
635 // Adjust by a (parameter or local) base offset.
636 if (var->IsParameter()) {
637 offset += kFPOnStackSize + kPCOnStackSize +
638 (info_->scope()->num_parameters() - 1) * kPointerSize;
639 } else {
640 offset += JavaScriptFrameConstants::kLocal0Offset;
641 }
642 return Operand(rbp, offset);
643}
644
645
646MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
647 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
648 if (var->IsContextSlot()) {
649 int context_chain_length = scope()->ContextChainLength(var->scope());
650 __ LoadContext(scratch, context_chain_length);
651 return ContextOperand(scratch, var->index());
652 } else {
653 return StackOperand(var);
654 }
655}
656
657
658void FullCodeGenerator::GetVar(Register dest, Variable* var) {
659 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
660 MemOperand location = VarOperand(var, dest);
661 __ movp(dest, location);
662}
663
664
665void FullCodeGenerator::SetVar(Variable* var,
666 Register src,
667 Register scratch0,
668 Register scratch1) {
669 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
670 DCHECK(!scratch0.is(src));
671 DCHECK(!scratch0.is(scratch1));
672 DCHECK(!scratch1.is(src));
673 MemOperand location = VarOperand(var, scratch0);
674 __ movp(location, src);
675
676 // Emit the write barrier code if the location is in the heap.
677 if (var->IsContextSlot()) {
678 int offset = Context::SlotOffset(var->index());
679 __ RecordWriteContextSlot(scratch0, offset, src, scratch1, kDontSaveFPRegs);
680 }
681}
682
683
684void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
685 bool should_normalize,
686 Label* if_true,
687 Label* if_false) {
688 // Only prepare for bailouts before splits if we're in a test
689 // context. Otherwise, we let the Visit function deal with the
690 // preparation to avoid preparing with the same AST id twice.
691 if (!context()->IsTest()) return;
692
693 Label skip;
694 if (should_normalize) __ jmp(&skip, Label::kNear);
695 PrepareForBailout(expr, TOS_REG);
696 if (should_normalize) {
697 __ CompareRoot(rax, Heap::kTrueValueRootIndex);
698 Split(equal, if_true, if_false, NULL);
699 __ bind(&skip);
700 }
701}
702
703
704void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
705 // The variable in the declaration always resides in the current context.
706 DCHECK_EQ(0, scope()->ContextChainLength(variable->scope()));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100707 if (FLAG_debug_code) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000708 // Check that we're not inside a with or catch context.
709 __ movp(rbx, FieldOperand(rsi, HeapObject::kMapOffset));
710 __ CompareRoot(rbx, Heap::kWithContextMapRootIndex);
711 __ Check(not_equal, kDeclarationInWithContext);
712 __ CompareRoot(rbx, Heap::kCatchContextMapRootIndex);
713 __ Check(not_equal, kDeclarationInCatchContext);
714 }
715}
716
717
718void FullCodeGenerator::VisitVariableDeclaration(
719 VariableDeclaration* declaration) {
720 // If it was not possible to allocate the variable at compile time, we
721 // need to "declare" it at runtime to make sure it actually exists in the
722 // local context.
723 VariableProxy* proxy = declaration->proxy();
724 VariableMode mode = declaration->mode();
725 Variable* variable = proxy->var();
726 bool hole_init = mode == LET || mode == CONST || mode == CONST_LEGACY;
727 switch (variable->location()) {
728 case VariableLocation::GLOBAL:
729 case VariableLocation::UNALLOCATED:
730 globals_->Add(variable->name(), zone());
731 globals_->Add(variable->binding_needs_init()
732 ? isolate()->factory()->the_hole_value()
733 : isolate()->factory()->undefined_value(),
734 zone());
735 break;
736
737 case VariableLocation::PARAMETER:
738 case VariableLocation::LOCAL:
739 if (hole_init) {
740 Comment cmnt(masm_, "[ VariableDeclaration");
741 __ LoadRoot(kScratchRegister, Heap::kTheHoleValueRootIndex);
742 __ movp(StackOperand(variable), kScratchRegister);
743 }
744 break;
745
746 case VariableLocation::CONTEXT:
747 if (hole_init) {
748 Comment cmnt(masm_, "[ VariableDeclaration");
749 EmitDebugCheckDeclarationContext(variable);
750 __ LoadRoot(kScratchRegister, Heap::kTheHoleValueRootIndex);
751 __ movp(ContextOperand(rsi, variable->index()), kScratchRegister);
752 // No write barrier since the hole value is in old space.
753 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
754 }
755 break;
756
757 case VariableLocation::LOOKUP: {
758 Comment cmnt(masm_, "[ VariableDeclaration");
759 __ Push(variable->name());
760 // Declaration nodes are always introduced in one of four modes.
761 DCHECK(IsDeclaredVariableMode(mode));
762 // Push initial value, if any.
763 // Note: For variables we must not push an initial value (such as
764 // 'undefined') because we may have a (legal) redeclaration and we
765 // must not destroy the current value.
766 if (hole_init) {
767 __ PushRoot(Heap::kTheHoleValueRootIndex);
768 } else {
769 __ Push(Smi::FromInt(0)); // Indicates no initial value.
770 }
771 __ Push(Smi::FromInt(variable->DeclarationPropertyAttributes()));
772 __ CallRuntime(Runtime::kDeclareLookupSlot);
773 break;
774 }
775 }
776}
777
778
779void FullCodeGenerator::VisitFunctionDeclaration(
780 FunctionDeclaration* declaration) {
781 VariableProxy* proxy = declaration->proxy();
782 Variable* variable = proxy->var();
783 switch (variable->location()) {
784 case VariableLocation::GLOBAL:
785 case VariableLocation::UNALLOCATED: {
786 globals_->Add(variable->name(), zone());
787 Handle<SharedFunctionInfo> function =
788 Compiler::GetSharedFunctionInfo(declaration->fun(), script(), info_);
789 // Check for stack-overflow exception.
790 if (function.is_null()) return SetStackOverflow();
791 globals_->Add(function, zone());
792 break;
793 }
794
795 case VariableLocation::PARAMETER:
796 case VariableLocation::LOCAL: {
797 Comment cmnt(masm_, "[ FunctionDeclaration");
798 VisitForAccumulatorValue(declaration->fun());
799 __ movp(StackOperand(variable), result_register());
800 break;
801 }
802
803 case VariableLocation::CONTEXT: {
804 Comment cmnt(masm_, "[ FunctionDeclaration");
805 EmitDebugCheckDeclarationContext(variable);
806 VisitForAccumulatorValue(declaration->fun());
807 __ movp(ContextOperand(rsi, variable->index()), result_register());
808 int offset = Context::SlotOffset(variable->index());
809 // We know that we have written a function, which is not a smi.
810 __ RecordWriteContextSlot(rsi,
811 offset,
812 result_register(),
813 rcx,
814 kDontSaveFPRegs,
815 EMIT_REMEMBERED_SET,
816 OMIT_SMI_CHECK);
817 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
818 break;
819 }
820
821 case VariableLocation::LOOKUP: {
822 Comment cmnt(masm_, "[ FunctionDeclaration");
Ben Murdoch097c5b22016-05-18 11:27:45 +0100823 PushOperand(variable->name());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000824 VisitForStackValue(declaration->fun());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100825 PushOperand(Smi::FromInt(variable->DeclarationPropertyAttributes()));
826 CallRuntimeWithOperands(Runtime::kDeclareLookupSlot);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000827 break;
828 }
829 }
830}
831
832
833void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
834 // Call the runtime to declare the globals.
835 __ Push(pairs);
836 __ Push(Smi::FromInt(DeclareGlobalsFlags()));
837 __ CallRuntime(Runtime::kDeclareGlobals);
838 // Return value is ignored.
839}
840
841
842void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
843 // Call the runtime to declare the modules.
844 __ Push(descriptions);
845 __ CallRuntime(Runtime::kDeclareModules);
846 // Return value is ignored.
847}
848
849
850void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
851 Comment cmnt(masm_, "[ SwitchStatement");
852 Breakable nested_statement(this, stmt);
853 SetStatementPosition(stmt);
854
855 // Keep the switch value on the stack until a case matches.
856 VisitForStackValue(stmt->tag());
857 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
858
859 ZoneList<CaseClause*>* clauses = stmt->cases();
860 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
861
862 Label next_test; // Recycled for each test.
863 // Compile all the tests with branches to their bodies.
864 for (int i = 0; i < clauses->length(); i++) {
865 CaseClause* clause = clauses->at(i);
866 clause->body_target()->Unuse();
867
868 // The default is not a test, but remember it as final fall through.
869 if (clause->is_default()) {
870 default_clause = clause;
871 continue;
872 }
873
874 Comment cmnt(masm_, "[ Case comparison");
875 __ bind(&next_test);
876 next_test.Unuse();
877
878 // Compile the label expression.
879 VisitForAccumulatorValue(clause->label());
880
881 // Perform the comparison as if via '==='.
882 __ movp(rdx, Operand(rsp, 0)); // Switch value.
883 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
884 JumpPatchSite patch_site(masm_);
885 if (inline_smi_code) {
886 Label slow_case;
887 __ movp(rcx, rdx);
888 __ orp(rcx, rax);
889 patch_site.EmitJumpIfNotSmi(rcx, &slow_case, Label::kNear);
890
891 __ cmpp(rdx, rax);
892 __ j(not_equal, &next_test);
893 __ Drop(1); // Switch value is no longer needed.
894 __ jmp(clause->body_target());
895 __ bind(&slow_case);
896 }
897
898 // Record position before stub call for type feedback.
899 SetExpressionPosition(clause);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100900 Handle<Code> ic =
901 CodeFactory::CompareIC(isolate(), Token::EQ_STRICT).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000902 CallIC(ic, clause->CompareId());
903 patch_site.EmitPatchInfo();
904
905 Label skip;
906 __ jmp(&skip, Label::kNear);
907 PrepareForBailout(clause, TOS_REG);
908 __ CompareRoot(rax, Heap::kTrueValueRootIndex);
909 __ j(not_equal, &next_test);
910 __ Drop(1);
911 __ jmp(clause->body_target());
912 __ bind(&skip);
913
914 __ testp(rax, rax);
915 __ j(not_equal, &next_test);
916 __ Drop(1); // Switch value is no longer needed.
917 __ jmp(clause->body_target());
918 }
919
920 // Discard the test value and jump to the default if present, otherwise to
921 // the end of the statement.
922 __ bind(&next_test);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100923 DropOperands(1); // Switch value is no longer needed.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000924 if (default_clause == NULL) {
925 __ jmp(nested_statement.break_label());
926 } else {
927 __ jmp(default_clause->body_target());
928 }
929
930 // Compile all the case bodies.
931 for (int i = 0; i < clauses->length(); i++) {
932 Comment cmnt(masm_, "[ Case body");
933 CaseClause* clause = clauses->at(i);
934 __ bind(clause->body_target());
935 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
936 VisitStatements(clause->statements());
937 }
938
939 __ bind(nested_statement.break_label());
940 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
941}
942
943
944void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
945 Comment cmnt(masm_, "[ ForInStatement");
946 SetStatementPosition(stmt, SKIP_BREAK);
947
948 FeedbackVectorSlot slot = stmt->ForInFeedbackSlot();
949
950 Label loop, exit;
951 ForIn loop_statement(this, stmt);
952 increment_loop_depth();
953
Ben Murdoch097c5b22016-05-18 11:27:45 +0100954 // Get the object to enumerate over.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000955 SetExpressionAsStatementPosition(stmt->enumerable());
956 VisitForAccumulatorValue(stmt->enumerable());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100957 OperandStackDepthIncrement(ForIn::kElementCount);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000958
Ben Murdoch097c5b22016-05-18 11:27:45 +0100959 // If the object is null or undefined, skip over the loop, otherwise convert
960 // it to a JS receiver. See ECMA-262 version 5, section 12.6.4.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000961 Label convert, done_convert;
962 __ JumpIfSmi(rax, &convert, Label::kNear);
963 __ CmpObjectType(rax, FIRST_JS_RECEIVER_TYPE, rcx);
964 __ j(above_equal, &done_convert, Label::kNear);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100965 __ CompareRoot(rax, Heap::kNullValueRootIndex);
966 __ j(equal, &exit);
967 __ CompareRoot(rax, Heap::kUndefinedValueRootIndex);
968 __ j(equal, &exit);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000969 __ bind(&convert);
970 ToObjectStub stub(isolate());
971 __ CallStub(&stub);
972 __ bind(&done_convert);
973 PrepareForBailoutForId(stmt->ToObjectId(), TOS_REG);
974 __ Push(rax);
975
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000976 // Check cache validity in generated code. This is a fast case for
977 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
978 // guarantee cache validity, call the runtime system to check cache
979 // validity or get the property names in a fixed array.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100980 // Note: Proxies never have an enum cache, so will always take the
981 // slow path.
982 Label call_runtime;
983 __ CheckEnumCache(&call_runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000984
985 // The enum cache is valid. Load the map of the object being
986 // iterated over and use the cache for the iteration.
987 Label use_cache;
988 __ movp(rax, FieldOperand(rax, HeapObject::kMapOffset));
989 __ jmp(&use_cache, Label::kNear);
990
991 // Get the set of properties to enumerate.
992 __ bind(&call_runtime);
993 __ Push(rax); // Duplicate the enumerable object on the stack.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100994 __ CallRuntime(Runtime::kForInEnumerate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000995 PrepareForBailoutForId(stmt->EnumId(), TOS_REG);
996
997 // If we got a map from the runtime call, we can do a fast
998 // modification check. Otherwise, we got a fixed array, and we have
999 // to do a slow check.
1000 Label fixed_array;
1001 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
1002 Heap::kMetaMapRootIndex);
1003 __ j(not_equal, &fixed_array);
1004
1005 // We got a map in register rax. Get the enumeration cache from it.
1006 __ bind(&use_cache);
1007
1008 Label no_descriptors;
1009
1010 __ EnumLength(rdx, rax);
1011 __ Cmp(rdx, Smi::FromInt(0));
1012 __ j(equal, &no_descriptors);
1013
1014 __ LoadInstanceDescriptors(rax, rcx);
1015 __ movp(rcx, FieldOperand(rcx, DescriptorArray::kEnumCacheOffset));
1016 __ movp(rcx, FieldOperand(rcx, DescriptorArray::kEnumCacheBridgeCacheOffset));
1017
1018 // Set up the four remaining stack slots.
1019 __ Push(rax); // Map.
1020 __ Push(rcx); // Enumeration cache.
1021 __ Push(rdx); // Number of valid entries for the map in the enum cache.
1022 __ Push(Smi::FromInt(0)); // Initial index.
1023 __ jmp(&loop);
1024
1025 __ bind(&no_descriptors);
1026 __ addp(rsp, Immediate(kPointerSize));
1027 __ jmp(&exit);
1028
1029 // We got a fixed array in register rax. Iterate through that.
1030 __ bind(&fixed_array);
1031
1032 // No need for a write barrier, we are storing a Smi in the feedback vector.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001033 int const vector_index = SmiFromSlot(slot)->value();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001034 __ EmitLoadTypeFeedbackVector(rbx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001035 __ Move(FieldOperand(rbx, FixedArray::OffsetOfElementAt(vector_index)),
1036 TypeFeedbackVector::MegamorphicSentinel(isolate()));
1037 __ movp(rcx, Operand(rsp, 0 * kPointerSize)); // Get enumerated object
1038 __ Push(Smi::FromInt(1)); // Smi(1) indicates slow check
1039 __ Push(rax); // Array
1040 __ movp(rax, FieldOperand(rax, FixedArray::kLengthOffset));
1041 __ Push(rax); // Fixed array length (as smi).
Ben Murdoch097c5b22016-05-18 11:27:45 +01001042 PrepareForBailoutForId(stmt->PrepareId(), NO_REGISTERS);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001043 __ Push(Smi::FromInt(0)); // Initial index.
1044
1045 // Generate code for doing the condition check.
1046 __ bind(&loop);
1047 SetExpressionAsStatementPosition(stmt->each());
1048
1049 __ movp(rax, Operand(rsp, 0 * kPointerSize)); // Get the current index.
1050 __ cmpp(rax, Operand(rsp, 1 * kPointerSize)); // Compare to the array length.
1051 __ j(above_equal, loop_statement.break_label());
1052
1053 // Get the current entry of the array into register rbx.
1054 __ movp(rbx, Operand(rsp, 2 * kPointerSize));
1055 SmiIndex index = masm()->SmiToIndex(rax, rax, kPointerSizeLog2);
1056 __ movp(rbx, FieldOperand(rbx,
1057 index.reg,
1058 index.scale,
1059 FixedArray::kHeaderSize));
1060
1061 // Get the expected map from the stack or a smi in the
1062 // permanent slow case into register rdx.
1063 __ movp(rdx, Operand(rsp, 3 * kPointerSize));
1064
1065 // Check if the expected map still matches that of the enumerable.
1066 // If not, we may have to filter the key.
1067 Label update_each;
1068 __ movp(rcx, Operand(rsp, 4 * kPointerSize));
1069 __ cmpp(rdx, FieldOperand(rcx, HeapObject::kMapOffset));
1070 __ j(equal, &update_each, Label::kNear);
1071
Ben Murdoch097c5b22016-05-18 11:27:45 +01001072 // We might get here from TurboFan or Crankshaft when something in the
1073 // for-in loop body deopts and only now notice in fullcodegen, that we
1074 // can now longer use the enum cache, i.e. left fast mode. So better record
1075 // this information here, in case we later OSR back into this loop or
1076 // reoptimize the whole function w/o rerunning the loop with the slow
1077 // mode object in fullcodegen (which would result in a deopt loop).
1078 __ EmitLoadTypeFeedbackVector(rdx);
1079 __ Move(FieldOperand(rdx, FixedArray::OffsetOfElementAt(vector_index)),
1080 TypeFeedbackVector::MegamorphicSentinel(isolate()));
1081
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001082 // Convert the entry to a string or null if it isn't a property
1083 // anymore. If the property has been removed while iterating, we
1084 // just skip it.
1085 __ Push(rcx); // Enumerable.
1086 __ Push(rbx); // Current entry.
1087 __ CallRuntime(Runtime::kForInFilter);
1088 PrepareForBailoutForId(stmt->FilterId(), TOS_REG);
1089 __ CompareRoot(rax, Heap::kUndefinedValueRootIndex);
1090 __ j(equal, loop_statement.continue_label());
1091 __ movp(rbx, rax);
1092
1093 // Update the 'each' property or variable from the possibly filtered
1094 // entry in register rbx.
1095 __ bind(&update_each);
1096 __ movp(result_register(), rbx);
1097 // Perform the assignment as if via '='.
1098 { EffectContext context(this);
1099 EmitAssignment(stmt->each(), stmt->EachFeedbackSlot());
1100 PrepareForBailoutForId(stmt->AssignmentId(), NO_REGISTERS);
1101 }
1102
1103 // Both Crankshaft and Turbofan expect BodyId to be right before stmt->body().
1104 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
1105 // Generate code for the body of the loop.
1106 Visit(stmt->body());
1107
1108 // Generate code for going to the next element by incrementing the
1109 // index (smi) stored on top of the stack.
1110 __ bind(loop_statement.continue_label());
1111 __ SmiAddConstant(Operand(rsp, 0 * kPointerSize), Smi::FromInt(1));
1112
1113 EmitBackEdgeBookkeeping(stmt, &loop);
1114 __ jmp(&loop);
1115
1116 // Remove the pointers stored on the stack.
1117 __ bind(loop_statement.break_label());
1118 __ addp(rsp, Immediate(5 * kPointerSize));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001119 OperandStackDepthDecrement(ForIn::kElementCount);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001120
1121 // Exit and decrement the loop depth.
1122 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1123 __ bind(&exit);
1124 decrement_loop_depth();
1125}
1126
1127
1128void FullCodeGenerator::EmitNewClosure(Handle<SharedFunctionInfo> info,
1129 bool pretenure) {
1130 // Use the fast case closure allocation code that allocates in new
1131 // space for nested functions that don't need literals cloning. If
1132 // we're running with the --always-opt or the --prepare-always-opt
1133 // flag, we need to use the runtime function so that the new function
1134 // we are creating here gets a chance to have its code optimized and
1135 // doesn't just get a copy of the existing unoptimized code.
1136 if (!FLAG_always_opt &&
1137 !FLAG_prepare_always_opt &&
1138 !pretenure &&
1139 scope()->is_function_scope() &&
1140 info->num_literals() == 0) {
1141 FastNewClosureStub stub(isolate(), info->language_mode(), info->kind());
1142 __ Move(rbx, info);
1143 __ CallStub(&stub);
1144 } else {
1145 __ Push(info);
1146 __ CallRuntime(pretenure ? Runtime::kNewClosure_Tenured
1147 : Runtime::kNewClosure);
1148 }
1149 context()->Plug(rax);
1150}
1151
1152
1153void FullCodeGenerator::EmitSetHomeObject(Expression* initializer, int offset,
1154 FeedbackVectorSlot slot) {
1155 DCHECK(NeedsHomeObject(initializer));
1156 __ movp(StoreDescriptor::ReceiverRegister(), Operand(rsp, 0));
1157 __ Move(StoreDescriptor::NameRegister(),
1158 isolate()->factory()->home_object_symbol());
1159 __ movp(StoreDescriptor::ValueRegister(),
1160 Operand(rsp, offset * kPointerSize));
1161 EmitLoadStoreICSlot(slot);
1162 CallStoreIC();
1163}
1164
1165
1166void FullCodeGenerator::EmitSetHomeObjectAccumulator(Expression* initializer,
1167 int offset,
1168 FeedbackVectorSlot slot) {
1169 DCHECK(NeedsHomeObject(initializer));
1170 __ movp(StoreDescriptor::ReceiverRegister(), rax);
1171 __ Move(StoreDescriptor::NameRegister(),
1172 isolate()->factory()->home_object_symbol());
1173 __ movp(StoreDescriptor::ValueRegister(),
1174 Operand(rsp, offset * kPointerSize));
1175 EmitLoadStoreICSlot(slot);
1176 CallStoreIC();
1177}
1178
1179
1180void FullCodeGenerator::EmitLoadGlobalCheckExtensions(VariableProxy* proxy,
1181 TypeofMode typeof_mode,
1182 Label* slow) {
1183 Register context = rsi;
1184 Register temp = rdx;
1185
1186 Scope* s = scope();
1187 while (s != NULL) {
1188 if (s->num_heap_slots() > 0) {
1189 if (s->calls_sloppy_eval()) {
1190 // Check that extension is "the hole".
1191 __ JumpIfNotRoot(ContextOperand(context, Context::EXTENSION_INDEX),
1192 Heap::kTheHoleValueRootIndex, slow);
1193 }
1194 // Load next context in chain.
1195 __ movp(temp, ContextOperand(context, Context::PREVIOUS_INDEX));
1196 // Walk the rest of the chain without clobbering rsi.
1197 context = temp;
1198 }
1199 // If no outer scope calls eval, we do not need to check more
1200 // context extensions. If we have reached an eval scope, we check
1201 // all extensions from this point.
1202 if (!s->outer_scope_calls_sloppy_eval() || s->is_eval_scope()) break;
1203 s = s->outer_scope();
1204 }
1205
1206 if (s != NULL && s->is_eval_scope()) {
1207 // Loop up the context chain. There is no frame effect so it is
1208 // safe to use raw labels here.
1209 Label next, fast;
1210 if (!context.is(temp)) {
1211 __ movp(temp, context);
1212 }
1213 // Load map for comparison into register, outside loop.
1214 __ LoadRoot(kScratchRegister, Heap::kNativeContextMapRootIndex);
1215 __ bind(&next);
1216 // Terminate at native context.
1217 __ cmpp(kScratchRegister, FieldOperand(temp, HeapObject::kMapOffset));
1218 __ j(equal, &fast, Label::kNear);
1219 // Check that extension is "the hole".
1220 __ JumpIfNotRoot(ContextOperand(temp, Context::EXTENSION_INDEX),
1221 Heap::kTheHoleValueRootIndex, slow);
1222 // Load next context in chain.
1223 __ movp(temp, ContextOperand(temp, Context::PREVIOUS_INDEX));
1224 __ jmp(&next);
1225 __ bind(&fast);
1226 }
1227
1228 // All extension objects were empty and it is safe to use a normal global
1229 // load machinery.
1230 EmitGlobalVariableLoad(proxy, typeof_mode);
1231}
1232
1233
1234MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1235 Label* slow) {
1236 DCHECK(var->IsContextSlot());
1237 Register context = rsi;
1238 Register temp = rbx;
1239
1240 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
1241 if (s->num_heap_slots() > 0) {
1242 if (s->calls_sloppy_eval()) {
1243 // Check that extension is "the hole".
1244 __ JumpIfNotRoot(ContextOperand(context, Context::EXTENSION_INDEX),
1245 Heap::kTheHoleValueRootIndex, slow);
1246 }
1247 __ movp(temp, ContextOperand(context, Context::PREVIOUS_INDEX));
1248 // Walk the rest of the chain without clobbering rsi.
1249 context = temp;
1250 }
1251 }
1252 // Check that last extension is "the hole".
1253 __ JumpIfNotRoot(ContextOperand(context, Context::EXTENSION_INDEX),
1254 Heap::kTheHoleValueRootIndex, slow);
1255
1256 // This function is used only for loads, not stores, so it's safe to
1257 // return an rsi-based operand (the write barrier cannot be allowed to
1258 // destroy the rsi register).
1259 return ContextOperand(context, var->index());
1260}
1261
1262
1263void FullCodeGenerator::EmitDynamicLookupFastCase(VariableProxy* proxy,
1264 TypeofMode typeof_mode,
1265 Label* slow, Label* done) {
1266 // Generate fast-case code for variables that might be shadowed by
1267 // eval-introduced variables. Eval is used a lot without
1268 // introducing variables. In those cases, we do not want to
1269 // perform a runtime call for all variables in the scope
1270 // containing the eval.
1271 Variable* var = proxy->var();
1272 if (var->mode() == DYNAMIC_GLOBAL) {
1273 EmitLoadGlobalCheckExtensions(proxy, typeof_mode, slow);
1274 __ jmp(done);
1275 } else if (var->mode() == DYNAMIC_LOCAL) {
1276 Variable* local = var->local_if_not_shadowed();
1277 __ movp(rax, ContextSlotOperandCheckExtensions(local, slow));
1278 if (local->mode() == LET || local->mode() == CONST ||
1279 local->mode() == CONST_LEGACY) {
1280 __ CompareRoot(rax, Heap::kTheHoleValueRootIndex);
1281 __ j(not_equal, done);
1282 if (local->mode() == CONST_LEGACY) {
1283 __ LoadRoot(rax, Heap::kUndefinedValueRootIndex);
1284 } else { // LET || CONST
1285 __ Push(var->name());
1286 __ CallRuntime(Runtime::kThrowReferenceError);
1287 }
1288 }
1289 __ jmp(done);
1290 }
1291}
1292
1293
1294void FullCodeGenerator::EmitGlobalVariableLoad(VariableProxy* proxy,
1295 TypeofMode typeof_mode) {
1296 Variable* var = proxy->var();
1297 DCHECK(var->IsUnallocatedOrGlobalSlot() ||
1298 (var->IsLookupSlot() && var->mode() == DYNAMIC_GLOBAL));
1299 __ Move(LoadDescriptor::NameRegister(), var->name());
1300 __ LoadGlobalObject(LoadDescriptor::ReceiverRegister());
1301 __ Move(LoadDescriptor::SlotRegister(),
1302 SmiFromSlot(proxy->VariableFeedbackSlot()));
1303 CallLoadIC(typeof_mode);
1304}
1305
1306
1307void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy,
1308 TypeofMode typeof_mode) {
1309 // Record position before possible IC call.
1310 SetExpressionPosition(proxy);
1311 PrepareForBailoutForId(proxy->BeforeId(), NO_REGISTERS);
1312 Variable* var = proxy->var();
1313
1314 // Three cases: global variables, lookup variables, and all other types of
1315 // variables.
1316 switch (var->location()) {
1317 case VariableLocation::GLOBAL:
1318 case VariableLocation::UNALLOCATED: {
1319 Comment cmnt(masm_, "[ Global variable");
1320 EmitGlobalVariableLoad(proxy, typeof_mode);
1321 context()->Plug(rax);
1322 break;
1323 }
1324
1325 case VariableLocation::PARAMETER:
1326 case VariableLocation::LOCAL:
1327 case VariableLocation::CONTEXT: {
1328 DCHECK_EQ(NOT_INSIDE_TYPEOF, typeof_mode);
1329 Comment cmnt(masm_, var->IsContextSlot() ? "[ Context slot"
1330 : "[ Stack slot");
1331 if (NeedsHoleCheckForLoad(proxy)) {
1332 // Let and const need a read barrier.
1333 Label done;
1334 GetVar(rax, var);
1335 __ CompareRoot(rax, Heap::kTheHoleValueRootIndex);
1336 __ j(not_equal, &done, Label::kNear);
1337 if (var->mode() == LET || var->mode() == CONST) {
1338 // Throw a reference error when using an uninitialized let/const
1339 // binding in harmony mode.
1340 __ Push(var->name());
1341 __ CallRuntime(Runtime::kThrowReferenceError);
1342 } else {
1343 // Uninitialized legacy const bindings are unholed.
1344 DCHECK(var->mode() == CONST_LEGACY);
1345 __ LoadRoot(rax, Heap::kUndefinedValueRootIndex);
1346 }
1347 __ bind(&done);
1348 context()->Plug(rax);
1349 break;
1350 }
1351 context()->Plug(var);
1352 break;
1353 }
1354
1355 case VariableLocation::LOOKUP: {
1356 Comment cmnt(masm_, "[ Lookup slot");
1357 Label done, slow;
1358 // Generate code for loading from variables potentially shadowed
1359 // by eval-introduced variables.
1360 EmitDynamicLookupFastCase(proxy, typeof_mode, &slow, &done);
1361 __ bind(&slow);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001362 __ Push(var->name());
1363 Runtime::FunctionId function_id =
1364 typeof_mode == NOT_INSIDE_TYPEOF
1365 ? Runtime::kLoadLookupSlot
Ben Murdoch097c5b22016-05-18 11:27:45 +01001366 : Runtime::kLoadLookupSlotInsideTypeof;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001367 __ CallRuntime(function_id);
1368 __ bind(&done);
1369 context()->Plug(rax);
1370 break;
1371 }
1372 }
1373}
1374
1375
1376void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1377 Comment cmnt(masm_, "[ RegExpLiteral");
1378 __ movp(rdi, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
1379 __ Move(rax, Smi::FromInt(expr->literal_index()));
1380 __ Move(rcx, expr->pattern());
1381 __ Move(rdx, Smi::FromInt(expr->flags()));
1382 FastCloneRegExpStub stub(isolate());
1383 __ CallStub(&stub);
1384 context()->Plug(rax);
1385}
1386
1387
1388void FullCodeGenerator::EmitAccessor(ObjectLiteralProperty* property) {
1389 Expression* expression = (property == NULL) ? NULL : property->value();
1390 if (expression == NULL) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001391 OperandStackDepthIncrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001392 __ PushRoot(Heap::kNullValueRootIndex);
1393 } else {
1394 VisitForStackValue(expression);
1395 if (NeedsHomeObject(expression)) {
1396 DCHECK(property->kind() == ObjectLiteral::Property::GETTER ||
1397 property->kind() == ObjectLiteral::Property::SETTER);
1398 int offset = property->kind() == ObjectLiteral::Property::GETTER ? 2 : 3;
1399 EmitSetHomeObject(expression, offset, property->GetSlot());
1400 }
1401 }
1402}
1403
1404
1405void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1406 Comment cmnt(masm_, "[ ObjectLiteral");
1407
1408 Handle<FixedArray> constant_properties = expr->constant_properties();
1409 int flags = expr->ComputeFlags();
1410 if (MustCreateObjectLiteralWithRuntime(expr)) {
1411 __ Push(Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
1412 __ Push(Smi::FromInt(expr->literal_index()));
1413 __ Push(constant_properties);
1414 __ Push(Smi::FromInt(flags));
1415 __ CallRuntime(Runtime::kCreateObjectLiteral);
1416 } else {
1417 __ movp(rax, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
1418 __ Move(rbx, Smi::FromInt(expr->literal_index()));
1419 __ Move(rcx, constant_properties);
1420 __ Move(rdx, Smi::FromInt(flags));
1421 FastCloneShallowObjectStub stub(isolate(), expr->properties_count());
1422 __ CallStub(&stub);
1423 }
1424 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1425
1426 // If result_saved is true the result is on top of the stack. If
1427 // result_saved is false the result is in rax.
1428 bool result_saved = false;
1429
1430 AccessorTable accessor_table(zone());
1431 int property_index = 0;
1432 for (; property_index < expr->properties()->length(); property_index++) {
1433 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1434 if (property->is_computed_name()) break;
1435 if (property->IsCompileTimeValue()) continue;
1436
1437 Literal* key = property->key()->AsLiteral();
1438 Expression* value = property->value();
1439 if (!result_saved) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001440 PushOperand(rax); // Save result on the stack
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001441 result_saved = true;
1442 }
1443 switch (property->kind()) {
1444 case ObjectLiteral::Property::CONSTANT:
1445 UNREACHABLE();
1446 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1447 DCHECK(!CompileTimeValue::IsCompileTimeValue(value));
1448 // Fall through.
1449 case ObjectLiteral::Property::COMPUTED:
1450 // It is safe to use [[Put]] here because the boilerplate already
1451 // contains computed properties with an uninitialized value.
1452 if (key->value()->IsInternalizedString()) {
1453 if (property->emit_store()) {
1454 VisitForAccumulatorValue(value);
1455 DCHECK(StoreDescriptor::ValueRegister().is(rax));
1456 __ Move(StoreDescriptor::NameRegister(), key->value());
1457 __ movp(StoreDescriptor::ReceiverRegister(), Operand(rsp, 0));
1458 EmitLoadStoreICSlot(property->GetSlot(0));
1459 CallStoreIC();
1460 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1461
1462 if (NeedsHomeObject(value)) {
1463 EmitSetHomeObjectAccumulator(value, 0, property->GetSlot(1));
1464 }
1465 } else {
1466 VisitForEffect(value);
1467 }
1468 break;
1469 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001470 PushOperand(Operand(rsp, 0)); // Duplicate receiver.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001471 VisitForStackValue(key);
1472 VisitForStackValue(value);
1473 if (property->emit_store()) {
1474 if (NeedsHomeObject(value)) {
1475 EmitSetHomeObject(value, 2, property->GetSlot());
1476 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001477 PushOperand(Smi::FromInt(SLOPPY)); // Language mode
1478 CallRuntimeWithOperands(Runtime::kSetProperty);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001479 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001480 DropOperands(3);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001481 }
1482 break;
1483 case ObjectLiteral::Property::PROTOTYPE:
Ben Murdoch097c5b22016-05-18 11:27:45 +01001484 PushOperand(Operand(rsp, 0)); // Duplicate receiver.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001485 VisitForStackValue(value);
1486 DCHECK(property->emit_store());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001487 CallRuntimeWithOperands(Runtime::kInternalSetPrototype);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001488 PrepareForBailoutForId(expr->GetIdForPropertySet(property_index),
1489 NO_REGISTERS);
1490 break;
1491 case ObjectLiteral::Property::GETTER:
1492 if (property->emit_store()) {
1493 accessor_table.lookup(key)->second->getter = property;
1494 }
1495 break;
1496 case ObjectLiteral::Property::SETTER:
1497 if (property->emit_store()) {
1498 accessor_table.lookup(key)->second->setter = property;
1499 }
1500 break;
1501 }
1502 }
1503
1504 // Emit code to define accessors, using only a single call to the runtime for
1505 // each pair of corresponding getters and setters.
1506 for (AccessorTable::Iterator it = accessor_table.begin();
1507 it != accessor_table.end();
1508 ++it) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001509 PushOperand(Operand(rsp, 0)); // Duplicate receiver.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001510 VisitForStackValue(it->first);
1511 EmitAccessor(it->second->getter);
1512 EmitAccessor(it->second->setter);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001513 PushOperand(Smi::FromInt(NONE));
1514 CallRuntimeWithOperands(Runtime::kDefineAccessorPropertyUnchecked);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001515 }
1516
1517 // Object literals have two parts. The "static" part on the left contains no
1518 // computed property names, and so we can compute its map ahead of time; see
1519 // runtime.cc::CreateObjectLiteralBoilerplate. The second "dynamic" part
1520 // starts with the first computed property name, and continues with all
1521 // properties to its right. All the code from above initializes the static
1522 // component of the object literal, and arranges for the map of the result to
1523 // reflect the static order in which the keys appear. For the dynamic
1524 // properties, we compile them into a series of "SetOwnProperty" runtime
1525 // calls. This will preserve insertion order.
1526 for (; property_index < expr->properties()->length(); property_index++) {
1527 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1528
1529 Expression* value = property->value();
1530 if (!result_saved) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001531 PushOperand(rax); // Save result on the stack
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001532 result_saved = true;
1533 }
1534
Ben Murdoch097c5b22016-05-18 11:27:45 +01001535 PushOperand(Operand(rsp, 0)); // Duplicate receiver.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001536
1537 if (property->kind() == ObjectLiteral::Property::PROTOTYPE) {
1538 DCHECK(!property->is_computed_name());
1539 VisitForStackValue(value);
1540 DCHECK(property->emit_store());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001541 CallRuntimeWithOperands(Runtime::kInternalSetPrototype);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001542 PrepareForBailoutForId(expr->GetIdForPropertySet(property_index),
1543 NO_REGISTERS);
1544 } else {
1545 EmitPropertyKey(property, expr->GetIdForPropertyName(property_index));
1546 VisitForStackValue(value);
1547 if (NeedsHomeObject(value)) {
1548 EmitSetHomeObject(value, 2, property->GetSlot());
1549 }
1550
1551 switch (property->kind()) {
1552 case ObjectLiteral::Property::CONSTANT:
1553 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1554 case ObjectLiteral::Property::COMPUTED:
1555 if (property->emit_store()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001556 PushOperand(Smi::FromInt(NONE));
1557 PushOperand(Smi::FromInt(property->NeedsSetFunctionName()));
1558 CallRuntimeWithOperands(Runtime::kDefineDataPropertyInLiteral);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001559 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001560 DropOperands(3);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001561 }
1562 break;
1563
1564 case ObjectLiteral::Property::PROTOTYPE:
1565 UNREACHABLE();
1566 break;
1567
1568 case ObjectLiteral::Property::GETTER:
Ben Murdoch097c5b22016-05-18 11:27:45 +01001569 PushOperand(Smi::FromInt(NONE));
1570 CallRuntimeWithOperands(Runtime::kDefineGetterPropertyUnchecked);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001571 break;
1572
1573 case ObjectLiteral::Property::SETTER:
Ben Murdoch097c5b22016-05-18 11:27:45 +01001574 PushOperand(Smi::FromInt(NONE));
1575 CallRuntimeWithOperands(Runtime::kDefineSetterPropertyUnchecked);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001576 break;
1577 }
1578 }
1579 }
1580
1581 if (expr->has_function()) {
1582 DCHECK(result_saved);
1583 __ Push(Operand(rsp, 0));
1584 __ CallRuntime(Runtime::kToFastProperties);
1585 }
1586
1587 if (result_saved) {
1588 context()->PlugTOS();
1589 } else {
1590 context()->Plug(rax);
1591 }
1592}
1593
1594
1595void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1596 Comment cmnt(masm_, "[ ArrayLiteral");
1597
1598 Handle<FixedArray> constant_elements = expr->constant_elements();
1599 bool has_constant_fast_elements =
1600 IsFastObjectElementsKind(expr->constant_elements_kind());
1601
1602 AllocationSiteMode allocation_site_mode = TRACK_ALLOCATION_SITE;
1603 if (has_constant_fast_elements && !FLAG_allocation_site_pretenuring) {
1604 // If the only customer of allocation sites is transitioning, then
1605 // we can turn it off if we don't have anywhere else to transition to.
1606 allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
1607 }
1608
1609 if (MustCreateArrayLiteralWithRuntime(expr)) {
1610 __ Push(Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
1611 __ Push(Smi::FromInt(expr->literal_index()));
1612 __ Push(constant_elements);
1613 __ Push(Smi::FromInt(expr->ComputeFlags()));
1614 __ CallRuntime(Runtime::kCreateArrayLiteral);
1615 } else {
1616 __ movp(rax, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
1617 __ Move(rbx, Smi::FromInt(expr->literal_index()));
1618 __ Move(rcx, constant_elements);
1619 FastCloneShallowArrayStub stub(isolate(), allocation_site_mode);
1620 __ CallStub(&stub);
1621 }
1622 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1623
1624 bool result_saved = false; // Is the result saved to the stack?
1625 ZoneList<Expression*>* subexprs = expr->values();
1626 int length = subexprs->length();
1627
1628 // Emit code to evaluate all the non-constant subexpressions and to store
1629 // them into the newly cloned array.
1630 int array_index = 0;
1631 for (; array_index < length; array_index++) {
1632 Expression* subexpr = subexprs->at(array_index);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001633 DCHECK(!subexpr->IsSpread());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001634
1635 // If the subexpression is a literal or a simple materialized literal it
1636 // is already set in the cloned array.
1637 if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
1638
1639 if (!result_saved) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001640 PushOperand(rax); // array literal
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001641 result_saved = true;
1642 }
1643 VisitForAccumulatorValue(subexpr);
1644
1645 __ Move(StoreDescriptor::NameRegister(), Smi::FromInt(array_index));
1646 __ movp(StoreDescriptor::ReceiverRegister(), Operand(rsp, 0));
1647 EmitLoadStoreICSlot(expr->LiteralFeedbackSlot());
1648 Handle<Code> ic =
1649 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
1650 CallIC(ic);
1651
1652 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1653 }
1654
1655 // In case the array literal contains spread expressions it has two parts. The
1656 // first part is the "static" array which has a literal index is handled
1657 // above. The second part is the part after the first spread expression
1658 // (inclusive) and these elements gets appended to the array. Note that the
1659 // number elements an iterable produces is unknown ahead of time.
1660 if (array_index < length && result_saved) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001661 PopOperand(rax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001662 result_saved = false;
1663 }
1664 for (; array_index < length; array_index++) {
1665 Expression* subexpr = subexprs->at(array_index);
1666
Ben Murdoch097c5b22016-05-18 11:27:45 +01001667 PushOperand(rax);
1668 DCHECK(!subexpr->IsSpread());
1669 VisitForStackValue(subexpr);
1670 CallRuntimeWithOperands(Runtime::kAppendElement);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001671
1672 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1673 }
1674
1675 if (result_saved) {
1676 context()->PlugTOS();
1677 } else {
1678 context()->Plug(rax);
1679 }
1680}
1681
1682
1683void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1684 DCHECK(expr->target()->IsValidReferenceExpressionOrThis());
1685
1686 Comment cmnt(masm_, "[ Assignment");
1687 SetExpressionPosition(expr, INSERT_BREAK);
1688
1689 Property* property = expr->target()->AsProperty();
1690 LhsKind assign_type = Property::GetAssignType(property);
1691
1692 // Evaluate LHS expression.
1693 switch (assign_type) {
1694 case VARIABLE:
1695 // Nothing to do here.
1696 break;
1697 case NAMED_PROPERTY:
1698 if (expr->is_compound()) {
1699 // We need the receiver both on the stack and in the register.
1700 VisitForStackValue(property->obj());
1701 __ movp(LoadDescriptor::ReceiverRegister(), Operand(rsp, 0));
1702 } else {
1703 VisitForStackValue(property->obj());
1704 }
1705 break;
1706 case NAMED_SUPER_PROPERTY:
1707 VisitForStackValue(
1708 property->obj()->AsSuperPropertyReference()->this_var());
1709 VisitForAccumulatorValue(
1710 property->obj()->AsSuperPropertyReference()->home_object());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001711 PushOperand(result_register());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001712 if (expr->is_compound()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001713 PushOperand(MemOperand(rsp, kPointerSize));
1714 PushOperand(result_register());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001715 }
1716 break;
1717 case KEYED_SUPER_PROPERTY:
1718 VisitForStackValue(
1719 property->obj()->AsSuperPropertyReference()->this_var());
1720 VisitForStackValue(
1721 property->obj()->AsSuperPropertyReference()->home_object());
1722 VisitForAccumulatorValue(property->key());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001723 PushOperand(result_register());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001724 if (expr->is_compound()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001725 PushOperand(MemOperand(rsp, 2 * kPointerSize));
1726 PushOperand(MemOperand(rsp, 2 * kPointerSize));
1727 PushOperand(result_register());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001728 }
1729 break;
1730 case KEYED_PROPERTY: {
1731 if (expr->is_compound()) {
1732 VisitForStackValue(property->obj());
1733 VisitForStackValue(property->key());
1734 __ movp(LoadDescriptor::ReceiverRegister(), Operand(rsp, kPointerSize));
1735 __ movp(LoadDescriptor::NameRegister(), Operand(rsp, 0));
1736 } else {
1737 VisitForStackValue(property->obj());
1738 VisitForStackValue(property->key());
1739 }
1740 break;
1741 }
1742 }
1743
1744 // For compound assignments we need another deoptimization point after the
1745 // variable/property load.
1746 if (expr->is_compound()) {
1747 { AccumulatorValueContext context(this);
1748 switch (assign_type) {
1749 case VARIABLE:
1750 EmitVariableLoad(expr->target()->AsVariableProxy());
1751 PrepareForBailout(expr->target(), TOS_REG);
1752 break;
1753 case NAMED_PROPERTY:
1754 EmitNamedPropertyLoad(property);
1755 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1756 break;
1757 case NAMED_SUPER_PROPERTY:
1758 EmitNamedSuperPropertyLoad(property);
1759 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1760 break;
1761 case KEYED_SUPER_PROPERTY:
1762 EmitKeyedSuperPropertyLoad(property);
1763 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1764 break;
1765 case KEYED_PROPERTY:
1766 EmitKeyedPropertyLoad(property);
1767 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1768 break;
1769 }
1770 }
1771
1772 Token::Value op = expr->binary_op();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001773 PushOperand(rax); // Left operand goes on the stack.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001774 VisitForAccumulatorValue(expr->value());
1775
1776 AccumulatorValueContext context(this);
1777 if (ShouldInlineSmiCase(op)) {
1778 EmitInlineSmiBinaryOp(expr->binary_operation(),
1779 op,
1780 expr->target(),
1781 expr->value());
1782 } else {
1783 EmitBinaryOp(expr->binary_operation(), op);
1784 }
1785 // Deoptimization point in case the binary operation may have side effects.
1786 PrepareForBailout(expr->binary_operation(), TOS_REG);
1787 } else {
1788 VisitForAccumulatorValue(expr->value());
1789 }
1790
1791 SetExpressionPosition(expr);
1792
1793 // Store the value.
1794 switch (assign_type) {
1795 case VARIABLE:
1796 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1797 expr->op(), expr->AssignmentSlot());
1798 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1799 context()->Plug(rax);
1800 break;
1801 case NAMED_PROPERTY:
1802 EmitNamedPropertyAssignment(expr);
1803 break;
1804 case NAMED_SUPER_PROPERTY:
1805 EmitNamedSuperPropertyStore(property);
1806 context()->Plug(rax);
1807 break;
1808 case KEYED_SUPER_PROPERTY:
1809 EmitKeyedSuperPropertyStore(property);
1810 context()->Plug(rax);
1811 break;
1812 case KEYED_PROPERTY:
1813 EmitKeyedPropertyAssignment(expr);
1814 break;
1815 }
1816}
1817
1818
1819void FullCodeGenerator::VisitYield(Yield* expr) {
1820 Comment cmnt(masm_, "[ Yield");
1821 SetExpressionPosition(expr);
1822
1823 // Evaluate yielded value first; the initial iterator definition depends on
1824 // this. It stays on the stack while we update the iterator.
1825 VisitForStackValue(expr->expression());
1826
1827 switch (expr->yield_kind()) {
1828 case Yield::kSuspend:
1829 // Pop value from top-of-stack slot; box result into result register.
1830 EmitCreateIteratorResult(false);
1831 __ Push(result_register());
1832 // Fall through.
1833 case Yield::kInitial: {
1834 Label suspend, continuation, post_runtime, resume;
1835
1836 __ jmp(&suspend);
1837 __ bind(&continuation);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001838 // When we arrive here, the stack top is the resume mode and
1839 // result_register() holds the input value (the argument given to the
1840 // respective resume operation).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001841 __ RecordGeneratorContinuation();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001842 __ Pop(rbx);
1843 __ SmiCompare(rbx, Smi::FromInt(JSGeneratorObject::RETURN));
1844 __ j(not_equal, &resume);
1845 __ Push(result_register());
1846 EmitCreateIteratorResult(true);
1847 EmitUnwindAndReturn();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001848
1849 __ bind(&suspend);
1850 VisitForAccumulatorValue(expr->generator_object());
1851 DCHECK(continuation.pos() > 0 && Smi::IsValid(continuation.pos()));
1852 __ Move(FieldOperand(rax, JSGeneratorObject::kContinuationOffset),
1853 Smi::FromInt(continuation.pos()));
1854 __ movp(FieldOperand(rax, JSGeneratorObject::kContextOffset), rsi);
1855 __ movp(rcx, rsi);
1856 __ RecordWriteField(rax, JSGeneratorObject::kContextOffset, rcx, rdx,
1857 kDontSaveFPRegs);
1858 __ leap(rbx, Operand(rbp, StandardFrameConstants::kExpressionsOffset));
1859 __ cmpp(rsp, rbx);
1860 __ j(equal, &post_runtime);
1861 __ Push(rax); // generator object
1862 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
1863 __ movp(context_register(),
1864 Operand(rbp, StandardFrameConstants::kContextOffset));
1865 __ bind(&post_runtime);
1866
Ben Murdoch097c5b22016-05-18 11:27:45 +01001867 PopOperand(result_register());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001868 EmitReturnSequence();
1869
1870 __ bind(&resume);
1871 context()->Plug(result_register());
1872 break;
1873 }
1874
1875 case Yield::kFinal: {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001876 // Pop value from top-of-stack slot, box result into result register.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001877 OperandStackDepthDecrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001878 EmitCreateIteratorResult(true);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001879 EmitUnwindAndReturn();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001880 break;
1881 }
1882
Ben Murdoch097c5b22016-05-18 11:27:45 +01001883 case Yield::kDelegating:
1884 UNREACHABLE();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001885 }
1886}
1887
1888
Ben Murdoch097c5b22016-05-18 11:27:45 +01001889void FullCodeGenerator::EmitGeneratorResume(
1890 Expression* generator, Expression* value,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001891 JSGeneratorObject::ResumeMode resume_mode) {
1892 // The value stays in rax, and is ultimately read by the resumed generator, as
1893 // if CallRuntime(Runtime::kSuspendJSGeneratorObject) returned it. Or it
1894 // is read to throw the value when the resumed generator is already closed.
1895 // rbx will hold the generator object until the activation has been resumed.
1896 VisitForStackValue(generator);
1897 VisitForAccumulatorValue(value);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001898 PopOperand(rbx);
1899
1900 // Store input value into generator object.
1901 __ movp(FieldOperand(rbx, JSGeneratorObject::kInputOffset),
1902 result_register());
1903 __ movp(rcx, result_register());
1904 __ RecordWriteField(rbx, JSGeneratorObject::kInputOffset, rcx, rdx,
1905 kDontSaveFPRegs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001906
1907 // Load suspended function and context.
1908 __ movp(rsi, FieldOperand(rbx, JSGeneratorObject::kContextOffset));
1909 __ movp(rdi, FieldOperand(rbx, JSGeneratorObject::kFunctionOffset));
1910
1911 // Push receiver.
1912 __ Push(FieldOperand(rbx, JSGeneratorObject::kReceiverOffset));
1913
1914 // Push holes for arguments to generator function.
1915 __ movp(rdx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
1916 __ LoadSharedFunctionInfoSpecialField(rdx, rdx,
1917 SharedFunctionInfo::kFormalParameterCountOffset);
1918 __ LoadRoot(rcx, Heap::kTheHoleValueRootIndex);
1919 Label push_argument_holes, push_frame;
1920 __ bind(&push_argument_holes);
1921 __ subp(rdx, Immediate(1));
1922 __ j(carry, &push_frame);
1923 __ Push(rcx);
1924 __ jmp(&push_argument_holes);
1925
1926 // Enter a new JavaScript frame, and initialize its slots as they were when
1927 // the generator was suspended.
1928 Label resume_frame, done;
1929 __ bind(&push_frame);
1930 __ call(&resume_frame);
1931 __ jmp(&done);
1932 __ bind(&resume_frame);
1933 __ pushq(rbp); // Caller's frame pointer.
1934 __ movp(rbp, rsp);
1935 __ Push(rsi); // Callee's context.
1936 __ Push(rdi); // Callee's JS Function.
1937
1938 // Load the operand stack size.
1939 __ movp(rdx, FieldOperand(rbx, JSGeneratorObject::kOperandStackOffset));
1940 __ movp(rdx, FieldOperand(rdx, FixedArray::kLengthOffset));
1941 __ SmiToInteger32(rdx, rdx);
1942
1943 // If we are sending a value and there is no operand stack, we can jump back
1944 // in directly.
1945 if (resume_mode == JSGeneratorObject::NEXT) {
1946 Label slow_resume;
1947 __ cmpp(rdx, Immediate(0));
1948 __ j(not_zero, &slow_resume);
1949 __ movp(rdx, FieldOperand(rdi, JSFunction::kCodeEntryOffset));
1950 __ SmiToInteger64(rcx,
1951 FieldOperand(rbx, JSGeneratorObject::kContinuationOffset));
1952 __ addp(rdx, rcx);
1953 __ Move(FieldOperand(rbx, JSGeneratorObject::kContinuationOffset),
1954 Smi::FromInt(JSGeneratorObject::kGeneratorExecuting));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001955 __ Push(Smi::FromInt(resume_mode)); // Consumed in continuation.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001956 __ jmp(rdx);
1957 __ bind(&slow_resume);
1958 }
1959
1960 // Otherwise, we push holes for the operand stack and call the runtime to fix
1961 // up the stack and the handlers.
1962 Label push_operand_holes, call_resume;
1963 __ bind(&push_operand_holes);
1964 __ subp(rdx, Immediate(1));
1965 __ j(carry, &call_resume);
1966 __ Push(rcx);
1967 __ jmp(&push_operand_holes);
1968 __ bind(&call_resume);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001969 __ Push(Smi::FromInt(resume_mode)); // Consumed in continuation.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001970 __ Push(rbx);
1971 __ Push(result_register());
1972 __ Push(Smi::FromInt(resume_mode));
1973 __ CallRuntime(Runtime::kResumeJSGeneratorObject);
1974 // Not reached: the runtime call returns elsewhere.
1975 __ Abort(kGeneratorFailedToResume);
1976
1977 __ bind(&done);
1978 context()->Plug(result_register());
1979}
1980
Ben Murdoch097c5b22016-05-18 11:27:45 +01001981void FullCodeGenerator::PushOperand(MemOperand operand) {
1982 OperandStackDepthIncrement(1);
1983 __ Push(operand);
1984}
1985
1986void FullCodeGenerator::EmitOperandStackDepthCheck() {
1987 if (FLAG_debug_code) {
1988 int expected_diff = StandardFrameConstants::kFixedFrameSizeFromFp +
1989 operand_stack_depth_ * kPointerSize;
1990 __ movp(rax, rbp);
1991 __ subp(rax, rsp);
1992 __ cmpp(rax, Immediate(expected_diff));
1993 __ Assert(equal, kUnexpectedStackDepth);
1994 }
1995}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001996
1997void FullCodeGenerator::EmitCreateIteratorResult(bool done) {
1998 Label allocate, done_allocate;
1999
2000 __ Allocate(JSIteratorResult::kSize, rax, rcx, rdx, &allocate, TAG_OBJECT);
2001 __ jmp(&done_allocate, Label::kNear);
2002
2003 __ bind(&allocate);
2004 __ Push(Smi::FromInt(JSIteratorResult::kSize));
2005 __ CallRuntime(Runtime::kAllocateInNewSpace);
2006
2007 __ bind(&done_allocate);
2008 __ LoadNativeContextSlot(Context::ITERATOR_RESULT_MAP_INDEX, rbx);
2009 __ movp(FieldOperand(rax, HeapObject::kMapOffset), rbx);
2010 __ LoadRoot(rbx, Heap::kEmptyFixedArrayRootIndex);
2011 __ movp(FieldOperand(rax, JSObject::kPropertiesOffset), rbx);
2012 __ movp(FieldOperand(rax, JSObject::kElementsOffset), rbx);
2013 __ Pop(FieldOperand(rax, JSIteratorResult::kValueOffset));
2014 __ LoadRoot(FieldOperand(rax, JSIteratorResult::kDoneOffset),
2015 done ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex);
2016 STATIC_ASSERT(JSIteratorResult::kSize == 5 * kPointerSize);
2017}
2018
2019
2020void FullCodeGenerator::EmitNamedPropertyLoad(Property* prop) {
2021 SetExpressionPosition(prop);
2022 Literal* key = prop->key()->AsLiteral();
Ben Murdoch097c5b22016-05-18 11:27:45 +01002023 DCHECK(!key->value()->IsSmi());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002024 DCHECK(!prop->IsSuperAccess());
2025
2026 __ Move(LoadDescriptor::NameRegister(), key->value());
2027 __ Move(LoadDescriptor::SlotRegister(),
2028 SmiFromSlot(prop->PropertyFeedbackSlot()));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002029 CallLoadIC(NOT_INSIDE_TYPEOF);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002030}
2031
2032
2033void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
2034 Token::Value op,
2035 Expression* left,
2036 Expression* right) {
2037 // Do combined smi check of the operands. Left operand is on the
2038 // stack (popped into rdx). Right operand is in rax but moved into
2039 // rcx to make the shifts easier.
2040 Label done, stub_call, smi_case;
Ben Murdoch097c5b22016-05-18 11:27:45 +01002041 PopOperand(rdx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002042 __ movp(rcx, rax);
2043 __ orp(rax, rdx);
2044 JumpPatchSite patch_site(masm_);
2045 patch_site.EmitJumpIfSmi(rax, &smi_case, Label::kNear);
2046
2047 __ bind(&stub_call);
2048 __ movp(rax, rcx);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002049 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), op).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002050 CallIC(code, expr->BinaryOperationFeedbackId());
2051 patch_site.EmitPatchInfo();
2052 __ jmp(&done, Label::kNear);
2053
2054 __ bind(&smi_case);
2055 switch (op) {
2056 case Token::SAR:
2057 __ SmiShiftArithmeticRight(rax, rdx, rcx);
2058 break;
2059 case Token::SHL:
2060 __ SmiShiftLeft(rax, rdx, rcx, &stub_call);
2061 break;
2062 case Token::SHR:
2063 __ SmiShiftLogicalRight(rax, rdx, rcx, &stub_call);
2064 break;
2065 case Token::ADD:
2066 __ SmiAdd(rax, rdx, rcx, &stub_call);
2067 break;
2068 case Token::SUB:
2069 __ SmiSub(rax, rdx, rcx, &stub_call);
2070 break;
2071 case Token::MUL:
2072 __ SmiMul(rax, rdx, rcx, &stub_call);
2073 break;
2074 case Token::BIT_OR:
2075 __ SmiOr(rax, rdx, rcx);
2076 break;
2077 case Token::BIT_AND:
2078 __ SmiAnd(rax, rdx, rcx);
2079 break;
2080 case Token::BIT_XOR:
2081 __ SmiXor(rax, rdx, rcx);
2082 break;
2083 default:
2084 UNREACHABLE();
2085 break;
2086 }
2087
2088 __ bind(&done);
2089 context()->Plug(rax);
2090}
2091
2092
2093void FullCodeGenerator::EmitClassDefineProperties(ClassLiteral* lit) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002094 for (int i = 0; i < lit->properties()->length(); i++) {
2095 ObjectLiteral::Property* property = lit->properties()->at(i);
2096 Expression* value = property->value();
2097
2098 if (property->is_static()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002099 PushOperand(Operand(rsp, kPointerSize)); // constructor
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002100 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002101 PushOperand(Operand(rsp, 0)); // prototype
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002102 }
2103 EmitPropertyKey(property, lit->GetIdForProperty(i));
2104
2105 // The static prototype property is read only. We handle the non computed
2106 // property name case in the parser. Since this is the only case where we
2107 // need to check for an own read only property we special case this so we do
2108 // not need to do this for every property.
2109 if (property->is_static() && property->is_computed_name()) {
2110 __ CallRuntime(Runtime::kThrowIfStaticPrototype);
2111 __ Push(rax);
2112 }
2113
2114 VisitForStackValue(value);
2115 if (NeedsHomeObject(value)) {
2116 EmitSetHomeObject(value, 2, property->GetSlot());
2117 }
2118
2119 switch (property->kind()) {
2120 case ObjectLiteral::Property::CONSTANT:
2121 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2122 case ObjectLiteral::Property::PROTOTYPE:
2123 UNREACHABLE();
2124 case ObjectLiteral::Property::COMPUTED:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002125 PushOperand(Smi::FromInt(DONT_ENUM));
2126 PushOperand(Smi::FromInt(property->NeedsSetFunctionName()));
2127 CallRuntimeWithOperands(Runtime::kDefineDataPropertyInLiteral);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002128 break;
2129
2130 case ObjectLiteral::Property::GETTER:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002131 PushOperand(Smi::FromInt(DONT_ENUM));
2132 CallRuntimeWithOperands(Runtime::kDefineGetterPropertyUnchecked);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002133 break;
2134
2135 case ObjectLiteral::Property::SETTER:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002136 PushOperand(Smi::FromInt(DONT_ENUM));
2137 CallRuntimeWithOperands(Runtime::kDefineSetterPropertyUnchecked);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002138 break;
2139
2140 default:
2141 UNREACHABLE();
2142 }
2143 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002144}
2145
2146
2147void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr, Token::Value op) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002148 PopOperand(rdx);
2149 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), op).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002150 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
2151 CallIC(code, expr->BinaryOperationFeedbackId());
2152 patch_site.EmitPatchInfo();
2153 context()->Plug(rax);
2154}
2155
2156
2157void FullCodeGenerator::EmitAssignment(Expression* expr,
2158 FeedbackVectorSlot slot) {
2159 DCHECK(expr->IsValidReferenceExpressionOrThis());
2160
2161 Property* prop = expr->AsProperty();
2162 LhsKind assign_type = Property::GetAssignType(prop);
2163
2164 switch (assign_type) {
2165 case VARIABLE: {
2166 Variable* var = expr->AsVariableProxy()->var();
2167 EffectContext context(this);
2168 EmitVariableAssignment(var, Token::ASSIGN, slot);
2169 break;
2170 }
2171 case NAMED_PROPERTY: {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002172 PushOperand(rax); // Preserve value.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002173 VisitForAccumulatorValue(prop->obj());
2174 __ Move(StoreDescriptor::ReceiverRegister(), rax);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002175 PopOperand(StoreDescriptor::ValueRegister()); // Restore value.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002176 __ Move(StoreDescriptor::NameRegister(),
2177 prop->key()->AsLiteral()->value());
2178 EmitLoadStoreICSlot(slot);
2179 CallStoreIC();
2180 break;
2181 }
2182 case NAMED_SUPER_PROPERTY: {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002183 PushOperand(rax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002184 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2185 VisitForAccumulatorValue(
2186 prop->obj()->AsSuperPropertyReference()->home_object());
2187 // stack: value, this; rax: home_object
2188 Register scratch = rcx;
2189 Register scratch2 = rdx;
2190 __ Move(scratch, result_register()); // home_object
2191 __ movp(rax, MemOperand(rsp, kPointerSize)); // value
2192 __ movp(scratch2, MemOperand(rsp, 0)); // this
2193 __ movp(MemOperand(rsp, kPointerSize), scratch2); // this
2194 __ movp(MemOperand(rsp, 0), scratch); // home_object
2195 // stack: this, home_object; rax: value
2196 EmitNamedSuperPropertyStore(prop);
2197 break;
2198 }
2199 case KEYED_SUPER_PROPERTY: {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002200 PushOperand(rax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002201 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2202 VisitForStackValue(
2203 prop->obj()->AsSuperPropertyReference()->home_object());
2204 VisitForAccumulatorValue(prop->key());
2205 Register scratch = rcx;
2206 Register scratch2 = rdx;
2207 __ movp(scratch2, MemOperand(rsp, 2 * kPointerSize)); // value
2208 // stack: value, this, home_object; rax: key, rdx: value
2209 __ movp(scratch, MemOperand(rsp, kPointerSize)); // this
2210 __ movp(MemOperand(rsp, 2 * kPointerSize), scratch);
2211 __ movp(scratch, MemOperand(rsp, 0)); // home_object
2212 __ movp(MemOperand(rsp, kPointerSize), scratch);
2213 __ movp(MemOperand(rsp, 0), rax);
2214 __ Move(rax, scratch2);
2215 // stack: this, home_object, key; rax: value.
2216 EmitKeyedSuperPropertyStore(prop);
2217 break;
2218 }
2219 case KEYED_PROPERTY: {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002220 PushOperand(rax); // Preserve value.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002221 VisitForStackValue(prop->obj());
2222 VisitForAccumulatorValue(prop->key());
2223 __ Move(StoreDescriptor::NameRegister(), rax);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002224 PopOperand(StoreDescriptor::ReceiverRegister());
2225 PopOperand(StoreDescriptor::ValueRegister()); // Restore value.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002226 EmitLoadStoreICSlot(slot);
2227 Handle<Code> ic =
2228 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2229 CallIC(ic);
2230 break;
2231 }
2232 }
2233 context()->Plug(rax);
2234}
2235
2236
2237void FullCodeGenerator::EmitStoreToStackLocalOrContextSlot(
2238 Variable* var, MemOperand location) {
2239 __ movp(location, rax);
2240 if (var->IsContextSlot()) {
2241 __ movp(rdx, rax);
2242 __ RecordWriteContextSlot(
2243 rcx, Context::SlotOffset(var->index()), rdx, rbx, kDontSaveFPRegs);
2244 }
2245}
2246
2247
2248void FullCodeGenerator::EmitVariableAssignment(Variable* var, Token::Value op,
2249 FeedbackVectorSlot slot) {
2250 if (var->IsUnallocated()) {
2251 // Global var, const, or let.
2252 __ Move(StoreDescriptor::NameRegister(), var->name());
2253 __ LoadGlobalObject(StoreDescriptor::ReceiverRegister());
2254 EmitLoadStoreICSlot(slot);
2255 CallStoreIC();
2256
2257 } else if (var->mode() == LET && op != Token::INIT) {
2258 // Non-initializing assignment to let variable needs a write barrier.
2259 DCHECK(!var->IsLookupSlot());
2260 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2261 Label assign;
2262 MemOperand location = VarOperand(var, rcx);
2263 __ movp(rdx, location);
2264 __ CompareRoot(rdx, Heap::kTheHoleValueRootIndex);
2265 __ j(not_equal, &assign, Label::kNear);
2266 __ Push(var->name());
2267 __ CallRuntime(Runtime::kThrowReferenceError);
2268 __ bind(&assign);
2269 EmitStoreToStackLocalOrContextSlot(var, location);
2270
2271 } else if (var->mode() == CONST && op != Token::INIT) {
2272 // Assignment to const variable needs a write barrier.
2273 DCHECK(!var->IsLookupSlot());
2274 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2275 Label const_error;
2276 MemOperand location = VarOperand(var, rcx);
2277 __ movp(rdx, location);
2278 __ CompareRoot(rdx, Heap::kTheHoleValueRootIndex);
2279 __ j(not_equal, &const_error, Label::kNear);
2280 __ Push(var->name());
2281 __ CallRuntime(Runtime::kThrowReferenceError);
2282 __ bind(&const_error);
2283 __ CallRuntime(Runtime::kThrowConstAssignError);
2284
2285 } else if (var->is_this() && var->mode() == CONST && op == Token::INIT) {
2286 // Initializing assignment to const {this} needs a write barrier.
2287 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2288 Label uninitialized_this;
2289 MemOperand location = VarOperand(var, rcx);
2290 __ movp(rdx, location);
2291 __ CompareRoot(rdx, Heap::kTheHoleValueRootIndex);
2292 __ j(equal, &uninitialized_this);
2293 __ Push(var->name());
2294 __ CallRuntime(Runtime::kThrowReferenceError);
2295 __ bind(&uninitialized_this);
2296 EmitStoreToStackLocalOrContextSlot(var, location);
2297
2298 } else if (!var->is_const_mode() ||
2299 (var->mode() == CONST && op == Token::INIT)) {
2300 if (var->IsLookupSlot()) {
2301 // Assignment to var.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002302 __ Push(var->name());
Ben Murdoch097c5b22016-05-18 11:27:45 +01002303 __ Push(rax);
2304 __ CallRuntime(is_strict(language_mode())
2305 ? Runtime::kStoreLookupSlot_Strict
2306 : Runtime::kStoreLookupSlot_Sloppy);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002307 } else {
2308 // Assignment to var or initializing assignment to let/const in harmony
2309 // mode.
2310 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2311 MemOperand location = VarOperand(var, rcx);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002312 if (FLAG_debug_code && var->mode() == LET && op == Token::INIT) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002313 // Check for an uninitialized let binding.
2314 __ movp(rdx, location);
2315 __ CompareRoot(rdx, Heap::kTheHoleValueRootIndex);
2316 __ Check(equal, kLetBindingReInitialization);
2317 }
2318 EmitStoreToStackLocalOrContextSlot(var, location);
2319 }
2320
2321 } else if (var->mode() == CONST_LEGACY && op == Token::INIT) {
2322 // Const initializers need a write barrier.
2323 DCHECK(!var->IsParameter()); // No const parameters.
2324 if (var->IsLookupSlot()) {
2325 __ Push(rax);
2326 __ Push(rsi);
2327 __ Push(var->name());
2328 __ CallRuntime(Runtime::kInitializeLegacyConstLookupSlot);
2329 } else {
2330 DCHECK(var->IsStackLocal() || var->IsContextSlot());
2331 Label skip;
2332 MemOperand location = VarOperand(var, rcx);
2333 __ movp(rdx, location);
2334 __ CompareRoot(rdx, Heap::kTheHoleValueRootIndex);
2335 __ j(not_equal, &skip);
2336 EmitStoreToStackLocalOrContextSlot(var, location);
2337 __ bind(&skip);
2338 }
2339
2340 } else {
2341 DCHECK(var->mode() == CONST_LEGACY && op != Token::INIT);
2342 if (is_strict(language_mode())) {
2343 __ CallRuntime(Runtime::kThrowConstAssignError);
2344 }
2345 // Silently ignore store in sloppy mode.
2346 }
2347}
2348
2349
2350void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2351 // Assignment to a property, using a named store IC.
2352 Property* prop = expr->target()->AsProperty();
2353 DCHECK(prop != NULL);
2354 DCHECK(prop->key()->IsLiteral());
2355
2356 __ Move(StoreDescriptor::NameRegister(), prop->key()->AsLiteral()->value());
Ben Murdoch097c5b22016-05-18 11:27:45 +01002357 PopOperand(StoreDescriptor::ReceiverRegister());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002358 EmitLoadStoreICSlot(expr->AssignmentSlot());
2359 CallStoreIC();
2360
2361 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2362 context()->Plug(rax);
2363}
2364
2365
2366void FullCodeGenerator::EmitNamedSuperPropertyStore(Property* prop) {
2367 // Assignment to named property of super.
2368 // rax : value
2369 // stack : receiver ('this'), home_object
2370 DCHECK(prop != NULL);
2371 Literal* key = prop->key()->AsLiteral();
2372 DCHECK(key != NULL);
2373
Ben Murdoch097c5b22016-05-18 11:27:45 +01002374 PushOperand(key->value());
2375 PushOperand(rax);
2376 CallRuntimeWithOperands(is_strict(language_mode())
2377 ? Runtime::kStoreToSuper_Strict
2378 : Runtime::kStoreToSuper_Sloppy);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002379}
2380
2381
2382void FullCodeGenerator::EmitKeyedSuperPropertyStore(Property* prop) {
2383 // Assignment to named property of super.
2384 // rax : value
2385 // stack : receiver ('this'), home_object, key
2386 DCHECK(prop != NULL);
2387
Ben Murdoch097c5b22016-05-18 11:27:45 +01002388 PushOperand(rax);
2389 CallRuntimeWithOperands(is_strict(language_mode())
2390 ? Runtime::kStoreKeyedToSuper_Strict
2391 : Runtime::kStoreKeyedToSuper_Sloppy);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002392}
2393
2394
2395void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2396 // Assignment to a property, using a keyed store IC.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002397 PopOperand(StoreDescriptor::NameRegister()); // Key.
2398 PopOperand(StoreDescriptor::ReceiverRegister());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002399 DCHECK(StoreDescriptor::ValueRegister().is(rax));
2400 Handle<Code> ic =
2401 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2402 EmitLoadStoreICSlot(expr->AssignmentSlot());
2403 CallIC(ic);
2404
2405 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2406 context()->Plug(rax);
2407}
2408
2409
2410void FullCodeGenerator::VisitProperty(Property* expr) {
2411 Comment cmnt(masm_, "[ Property");
2412 SetExpressionPosition(expr);
2413
2414 Expression* key = expr->key();
2415
2416 if (key->IsPropertyName()) {
2417 if (!expr->IsSuperAccess()) {
2418 VisitForAccumulatorValue(expr->obj());
2419 DCHECK(!rax.is(LoadDescriptor::ReceiverRegister()));
2420 __ movp(LoadDescriptor::ReceiverRegister(), rax);
2421 EmitNamedPropertyLoad(expr);
2422 } else {
2423 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2424 VisitForStackValue(
2425 expr->obj()->AsSuperPropertyReference()->home_object());
2426 EmitNamedSuperPropertyLoad(expr);
2427 }
2428 } else {
2429 if (!expr->IsSuperAccess()) {
2430 VisitForStackValue(expr->obj());
2431 VisitForAccumulatorValue(expr->key());
2432 __ Move(LoadDescriptor::NameRegister(), rax);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002433 PopOperand(LoadDescriptor::ReceiverRegister());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002434 EmitKeyedPropertyLoad(expr);
2435 } else {
2436 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2437 VisitForStackValue(
2438 expr->obj()->AsSuperPropertyReference()->home_object());
2439 VisitForStackValue(expr->key());
2440 EmitKeyedSuperPropertyLoad(expr);
2441 }
2442 }
2443 PrepareForBailoutForId(expr->LoadId(), TOS_REG);
2444 context()->Plug(rax);
2445}
2446
2447
2448void FullCodeGenerator::CallIC(Handle<Code> code,
2449 TypeFeedbackId ast_id) {
2450 ic_total_count_++;
2451 __ call(code, RelocInfo::CODE_TARGET, ast_id);
2452}
2453
2454
2455// Code common for calls using the IC.
2456void FullCodeGenerator::EmitCallWithLoadIC(Call* expr) {
2457 Expression* callee = expr->expression();
2458
2459 // Get the target function.
2460 ConvertReceiverMode convert_mode;
2461 if (callee->IsVariableProxy()) {
2462 { StackValueContext context(this);
2463 EmitVariableLoad(callee->AsVariableProxy());
2464 PrepareForBailout(callee, NO_REGISTERS);
2465 }
2466 // Push undefined as receiver. This is patched in the Call builtin if it
2467 // is a sloppy mode method.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002468 PushOperand(isolate()->factory()->undefined_value());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002469 convert_mode = ConvertReceiverMode::kNullOrUndefined;
2470 } else {
2471 // Load the function from the receiver.
2472 DCHECK(callee->IsProperty());
2473 DCHECK(!callee->AsProperty()->IsSuperAccess());
2474 __ movp(LoadDescriptor::ReceiverRegister(), Operand(rsp, 0));
2475 EmitNamedPropertyLoad(callee->AsProperty());
2476 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2477 // Push the target function under the receiver.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002478 PushOperand(Operand(rsp, 0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002479 __ movp(Operand(rsp, kPointerSize), rax);
2480 convert_mode = ConvertReceiverMode::kNotNullOrUndefined;
2481 }
2482
2483 EmitCall(expr, convert_mode);
2484}
2485
2486
2487void FullCodeGenerator::EmitSuperCallWithLoadIC(Call* expr) {
2488 Expression* callee = expr->expression();
2489 DCHECK(callee->IsProperty());
2490 Property* prop = callee->AsProperty();
2491 DCHECK(prop->IsSuperAccess());
2492 SetExpressionPosition(prop);
2493
2494 Literal* key = prop->key()->AsLiteral();
2495 DCHECK(!key->value()->IsSmi());
2496 // Load the function from the receiver.
2497 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
2498 VisitForStackValue(super_ref->home_object());
2499 VisitForAccumulatorValue(super_ref->this_var());
Ben Murdoch097c5b22016-05-18 11:27:45 +01002500 PushOperand(rax);
2501 PushOperand(rax);
2502 PushOperand(Operand(rsp, kPointerSize * 2));
2503 PushOperand(key->value());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002504
2505 // Stack here:
2506 // - home_object
2507 // - this (receiver)
2508 // - this (receiver) <-- LoadFromSuper will pop here and below.
2509 // - home_object
2510 // - key
Ben Murdoch097c5b22016-05-18 11:27:45 +01002511 CallRuntimeWithOperands(Runtime::kLoadFromSuper);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002512
2513 // Replace home_object with target function.
2514 __ movp(Operand(rsp, kPointerSize), rax);
2515
2516 // Stack here:
2517 // - target function
2518 // - this (receiver)
2519 EmitCall(expr);
2520}
2521
2522
2523// Common code for calls using the IC.
2524void FullCodeGenerator::EmitKeyedCallWithLoadIC(Call* expr,
2525 Expression* key) {
2526 // Load the key.
2527 VisitForAccumulatorValue(key);
2528
2529 Expression* callee = expr->expression();
2530
2531 // Load the function from the receiver.
2532 DCHECK(callee->IsProperty());
2533 __ movp(LoadDescriptor::ReceiverRegister(), Operand(rsp, 0));
2534 __ Move(LoadDescriptor::NameRegister(), rax);
2535 EmitKeyedPropertyLoad(callee->AsProperty());
2536 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2537
2538 // Push the target function under the receiver.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002539 PushOperand(Operand(rsp, 0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002540 __ movp(Operand(rsp, kPointerSize), rax);
2541
2542 EmitCall(expr, ConvertReceiverMode::kNotNullOrUndefined);
2543}
2544
2545
2546void FullCodeGenerator::EmitKeyedSuperCallWithLoadIC(Call* expr) {
2547 Expression* callee = expr->expression();
2548 DCHECK(callee->IsProperty());
2549 Property* prop = callee->AsProperty();
2550 DCHECK(prop->IsSuperAccess());
2551
2552 SetExpressionPosition(prop);
2553 // Load the function from the receiver.
2554 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
2555 VisitForStackValue(super_ref->home_object());
2556 VisitForAccumulatorValue(super_ref->this_var());
Ben Murdoch097c5b22016-05-18 11:27:45 +01002557 PushOperand(rax);
2558 PushOperand(rax);
2559 PushOperand(Operand(rsp, kPointerSize * 2));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002560 VisitForStackValue(prop->key());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002561
2562 // Stack here:
2563 // - home_object
2564 // - this (receiver)
2565 // - this (receiver) <-- LoadKeyedFromSuper will pop here and below.
2566 // - home_object
2567 // - key
Ben Murdoch097c5b22016-05-18 11:27:45 +01002568 CallRuntimeWithOperands(Runtime::kLoadKeyedFromSuper);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002569
2570 // Replace home_object with target function.
2571 __ movp(Operand(rsp, kPointerSize), rax);
2572
2573 // Stack here:
2574 // - target function
2575 // - this (receiver)
2576 EmitCall(expr);
2577}
2578
2579
2580void FullCodeGenerator::EmitCall(Call* expr, ConvertReceiverMode mode) {
2581 // Load the arguments.
2582 ZoneList<Expression*>* args = expr->arguments();
2583 int arg_count = args->length();
2584 for (int i = 0; i < arg_count; i++) {
2585 VisitForStackValue(args->at(i));
2586 }
2587
2588 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
2589 SetCallPosition(expr);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002590 if (expr->tail_call_mode() == TailCallMode::kAllow) {
2591 if (FLAG_trace) {
2592 __ CallRuntime(Runtime::kTraceTailCall);
2593 }
2594 // Update profiling counters before the tail call since we will
2595 // not return to this function.
2596 EmitProfilingCounterHandlingForReturnSequence(true);
2597 }
2598 Handle<Code> ic =
2599 CodeFactory::CallIC(isolate(), arg_count, mode, expr->tail_call_mode())
2600 .code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002601 __ Move(rdx, SmiFromSlot(expr->CallFeedbackICSlot()));
2602 __ movp(rdi, Operand(rsp, (arg_count + 1) * kPointerSize));
2603 // Don't assign a type feedback id to the IC, since type feedback is provided
2604 // by the vector above.
2605 CallIC(ic);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002606 OperandStackDepthDecrement(arg_count + 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002607
2608 RecordJSReturnSite(expr);
2609
2610 // Restore context register.
2611 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
2612 // Discard the function left on TOS.
2613 context()->DropAndPlug(1, rax);
2614}
2615
2616
2617void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
2618 // Push copy of the first argument or undefined if it doesn't exist.
2619 if (arg_count > 0) {
2620 __ Push(Operand(rsp, arg_count * kPointerSize));
2621 } else {
2622 __ PushRoot(Heap::kUndefinedValueRootIndex);
2623 }
2624
2625 // Push the enclosing function.
2626 __ Push(Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
2627
2628 // Push the language mode.
2629 __ Push(Smi::FromInt(language_mode()));
2630
2631 // Push the start position of the scope the calls resides in.
2632 __ Push(Smi::FromInt(scope()->start_position()));
2633
2634 // Do the runtime call.
2635 __ CallRuntime(Runtime::kResolvePossiblyDirectEval);
2636}
2637
2638
2639// See http://www.ecma-international.org/ecma-262/6.0/#sec-function-calls.
2640void FullCodeGenerator::PushCalleeAndWithBaseObject(Call* expr) {
2641 VariableProxy* callee = expr->expression()->AsVariableProxy();
2642 if (callee->var()->IsLookupSlot()) {
2643 Label slow, done;
2644 SetExpressionPosition(callee);
2645 // Generate code for loading from variables potentially shadowed by
2646 // eval-introduced variables.
2647 EmitDynamicLookupFastCase(callee, NOT_INSIDE_TYPEOF, &slow, &done);
2648 __ bind(&slow);
2649 // Call the runtime to find the function to call (returned in rax) and
2650 // the object holding it (returned in rdx).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002651 __ Push(callee->name());
Ben Murdoch097c5b22016-05-18 11:27:45 +01002652 __ CallRuntime(Runtime::kLoadLookupSlotForCall);
2653 PushOperand(rax); // Function.
2654 PushOperand(rdx); // Receiver.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002655 PrepareForBailoutForId(expr->LookupId(), NO_REGISTERS);
2656
2657 // If fast case code has been generated, emit code to push the function
2658 // and receiver and have the slow path jump around this code.
2659 if (done.is_linked()) {
2660 Label call;
2661 __ jmp(&call, Label::kNear);
2662 __ bind(&done);
2663 // Push function.
2664 __ Push(rax);
2665 // Pass undefined as the receiver, which is the WithBaseObject of a
2666 // non-object environment record. If the callee is sloppy, it will patch
2667 // it up to be the global receiver.
2668 __ PushRoot(Heap::kUndefinedValueRootIndex);
2669 __ bind(&call);
2670 }
2671 } else {
2672 VisitForStackValue(callee);
2673 // refEnv.WithBaseObject()
Ben Murdoch097c5b22016-05-18 11:27:45 +01002674 OperandStackDepthIncrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002675 __ PushRoot(Heap::kUndefinedValueRootIndex);
2676 }
2677}
2678
2679
2680void FullCodeGenerator::EmitPossiblyEvalCall(Call* expr) {
2681 // In a call to eval, we first call RuntimeHidden_ResolvePossiblyDirectEval
2682 // to resolve the function we need to call. Then we call the resolved
2683 // function using the given arguments.
2684 ZoneList<Expression*>* args = expr->arguments();
2685 int arg_count = args->length();
2686 PushCalleeAndWithBaseObject(expr);
2687
2688 // Push the arguments.
2689 for (int i = 0; i < arg_count; i++) {
2690 VisitForStackValue(args->at(i));
2691 }
2692
2693 // Push a copy of the function (found below the arguments) and resolve
2694 // eval.
2695 __ Push(Operand(rsp, (arg_count + 1) * kPointerSize));
2696 EmitResolvePossiblyDirectEval(arg_count);
2697
2698 // Touch up the callee.
2699 __ movp(Operand(rsp, (arg_count + 1) * kPointerSize), rax);
2700
2701 PrepareForBailoutForId(expr->EvalId(), NO_REGISTERS);
2702
2703 SetCallPosition(expr);
2704 __ movp(rdi, Operand(rsp, (arg_count + 1) * kPointerSize));
2705 __ Set(rax, arg_count);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002706 __ Call(isolate()->builtins()->Call(ConvertReceiverMode::kAny,
2707 expr->tail_call_mode()),
2708 RelocInfo::CODE_TARGET);
2709 OperandStackDepthDecrement(arg_count + 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002710 RecordJSReturnSite(expr);
2711 // Restore context register.
2712 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
2713 context()->DropAndPlug(1, rax);
2714}
2715
2716
2717void FullCodeGenerator::VisitCallNew(CallNew* expr) {
2718 Comment cmnt(masm_, "[ CallNew");
2719 // According to ECMA-262, section 11.2.2, page 44, the function
2720 // expression in new calls must be evaluated before the
2721 // arguments.
2722
2723 // Push constructor on the stack. If it's not a function it's used as
2724 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2725 // ignored.
2726 DCHECK(!expr->expression()->IsSuperPropertyReference());
2727 VisitForStackValue(expr->expression());
2728
2729 // Push the arguments ("left-to-right") on the stack.
2730 ZoneList<Expression*>* args = expr->arguments();
2731 int arg_count = args->length();
2732 for (int i = 0; i < arg_count; i++) {
2733 VisitForStackValue(args->at(i));
2734 }
2735
2736 // Call the construct call builtin that handles allocation and
2737 // constructor invocation.
2738 SetConstructCallPosition(expr);
2739
2740 // Load function and argument count into rdi and rax.
2741 __ Set(rax, arg_count);
2742 __ movp(rdi, Operand(rsp, arg_count * kPointerSize));
2743
2744 // Record call targets in unoptimized code, but not in the snapshot.
2745 __ EmitLoadTypeFeedbackVector(rbx);
2746 __ Move(rdx, SmiFromSlot(expr->CallNewFeedbackSlot()));
2747
2748 CallConstructStub stub(isolate());
2749 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002750 OperandStackDepthDecrement(arg_count + 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002751 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
2752 // Restore context register.
2753 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
2754 context()->Plug(rax);
2755}
2756
2757
2758void FullCodeGenerator::EmitSuperConstructorCall(Call* expr) {
2759 SuperCallReference* super_call_ref =
2760 expr->expression()->AsSuperCallReference();
2761 DCHECK_NOT_NULL(super_call_ref);
2762
2763 // Push the super constructor target on the stack (may be null,
2764 // but the Construct builtin can deal with that properly).
2765 VisitForAccumulatorValue(super_call_ref->this_function_var());
2766 __ AssertFunction(result_register());
2767 __ movp(result_register(),
2768 FieldOperand(result_register(), HeapObject::kMapOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002769 PushOperand(FieldOperand(result_register(), Map::kPrototypeOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002770
2771 // Push the arguments ("left-to-right") on the stack.
2772 ZoneList<Expression*>* args = expr->arguments();
2773 int arg_count = args->length();
2774 for (int i = 0; i < arg_count; i++) {
2775 VisitForStackValue(args->at(i));
2776 }
2777
2778 // Call the construct call builtin that handles allocation and
2779 // constructor invocation.
2780 SetConstructCallPosition(expr);
2781
2782 // Load new target into rdx.
2783 VisitForAccumulatorValue(super_call_ref->new_target_var());
2784 __ movp(rdx, result_register());
2785
2786 // Load function and argument count into rdi and rax.
2787 __ Set(rax, arg_count);
2788 __ movp(rdi, Operand(rsp, arg_count * kPointerSize));
2789
2790 __ Call(isolate()->builtins()->Construct(), RelocInfo::CODE_TARGET);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002791 OperandStackDepthDecrement(arg_count + 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002792
2793 RecordJSReturnSite(expr);
2794
2795 // Restore context register.
2796 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
2797
2798 context()->Plug(rax);
2799}
2800
2801
2802void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2803 ZoneList<Expression*>* args = expr->arguments();
2804 DCHECK(args->length() == 1);
2805
2806 VisitForAccumulatorValue(args->at(0));
2807
2808 Label materialize_true, materialize_false;
2809 Label* if_true = NULL;
2810 Label* if_false = NULL;
2811 Label* fall_through = NULL;
2812 context()->PrepareTest(&materialize_true, &materialize_false,
2813 &if_true, &if_false, &fall_through);
2814
2815 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
2816 __ JumpIfSmi(rax, if_true);
2817 __ jmp(if_false);
2818
2819 context()->Plug(if_true, if_false);
2820}
2821
2822
2823void FullCodeGenerator::EmitIsJSReceiver(CallRuntime* expr) {
2824 ZoneList<Expression*>* args = expr->arguments();
2825 DCHECK(args->length() == 1);
2826
2827 VisitForAccumulatorValue(args->at(0));
2828
2829 Label materialize_true, materialize_false;
2830 Label* if_true = NULL;
2831 Label* if_false = NULL;
2832 Label* fall_through = NULL;
2833 context()->PrepareTest(&materialize_true, &materialize_false,
2834 &if_true, &if_false, &fall_through);
2835
2836 __ JumpIfSmi(rax, if_false);
2837 __ CmpObjectType(rax, FIRST_JS_RECEIVER_TYPE, rbx);
2838 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
2839 Split(above_equal, if_true, if_false, fall_through);
2840
2841 context()->Plug(if_true, if_false);
2842}
2843
2844
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002845void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2846 ZoneList<Expression*>* args = expr->arguments();
2847 DCHECK(args->length() == 1);
2848
2849 VisitForAccumulatorValue(args->at(0));
2850
2851 Label materialize_true, materialize_false;
2852 Label* if_true = NULL;
2853 Label* if_false = NULL;
2854 Label* fall_through = NULL;
2855 context()->PrepareTest(&materialize_true, &materialize_false,
2856 &if_true, &if_false, &fall_through);
2857
2858 __ JumpIfSmi(rax, if_false);
2859 __ CmpObjectType(rax, JS_ARRAY_TYPE, rbx);
2860 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
2861 Split(equal, if_true, if_false, fall_through);
2862
2863 context()->Plug(if_true, if_false);
2864}
2865
2866
2867void FullCodeGenerator::EmitIsTypedArray(CallRuntime* expr) {
2868 ZoneList<Expression*>* args = expr->arguments();
2869 DCHECK(args->length() == 1);
2870
2871 VisitForAccumulatorValue(args->at(0));
2872
2873 Label materialize_true, materialize_false;
2874 Label* if_true = NULL;
2875 Label* if_false = NULL;
2876 Label* fall_through = NULL;
2877 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
2878 &if_false, &fall_through);
2879
2880 __ JumpIfSmi(rax, if_false);
2881 __ CmpObjectType(rax, JS_TYPED_ARRAY_TYPE, rbx);
2882 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
2883 Split(equal, if_true, if_false, fall_through);
2884
2885 context()->Plug(if_true, if_false);
2886}
2887
2888
2889void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2890 ZoneList<Expression*>* args = expr->arguments();
2891 DCHECK(args->length() == 1);
2892
2893 VisitForAccumulatorValue(args->at(0));
2894
2895 Label materialize_true, materialize_false;
2896 Label* if_true = NULL;
2897 Label* if_false = NULL;
2898 Label* fall_through = NULL;
2899 context()->PrepareTest(&materialize_true, &materialize_false,
2900 &if_true, &if_false, &fall_through);
2901
2902 __ JumpIfSmi(rax, if_false);
2903 __ CmpObjectType(rax, JS_REGEXP_TYPE, rbx);
2904 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
2905 Split(equal, if_true, if_false, fall_through);
2906
2907 context()->Plug(if_true, if_false);
2908}
2909
2910
2911void FullCodeGenerator::EmitIsJSProxy(CallRuntime* expr) {
2912 ZoneList<Expression*>* args = expr->arguments();
2913 DCHECK(args->length() == 1);
2914
2915 VisitForAccumulatorValue(args->at(0));
2916
2917 Label materialize_true, materialize_false;
2918 Label* if_true = NULL;
2919 Label* if_false = NULL;
2920 Label* fall_through = NULL;
2921 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
2922 &if_false, &fall_through);
2923
2924
2925 __ JumpIfSmi(rax, if_false);
2926 __ CmpObjectType(rax, JS_PROXY_TYPE, rbx);
2927 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
2928 Split(equal, if_true, if_false, fall_through);
2929
2930 context()->Plug(if_true, if_false);
2931}
2932
2933
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002934void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2935 ZoneList<Expression*>* args = expr->arguments();
2936 DCHECK(args->length() == 1);
2937 Label done, null, function, non_function_constructor;
2938
2939 VisitForAccumulatorValue(args->at(0));
2940
2941 // If the object is not a JSReceiver, we return null.
2942 __ JumpIfSmi(rax, &null, Label::kNear);
2943 STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
2944 __ CmpObjectType(rax, FIRST_JS_RECEIVER_TYPE, rax);
2945 __ j(below, &null, Label::kNear);
2946
2947 // Return 'Function' for JSFunction objects.
2948 __ CmpInstanceType(rax, JS_FUNCTION_TYPE);
2949 __ j(equal, &function, Label::kNear);
2950
2951 // Check if the constructor in the map is a JS function.
2952 __ GetMapConstructor(rax, rax, rbx);
2953 __ CmpInstanceType(rbx, JS_FUNCTION_TYPE);
2954 __ j(not_equal, &non_function_constructor, Label::kNear);
2955
2956 // rax now contains the constructor function. Grab the
2957 // instance class name from there.
2958 __ movp(rax, FieldOperand(rax, JSFunction::kSharedFunctionInfoOffset));
2959 __ movp(rax, FieldOperand(rax, SharedFunctionInfo::kInstanceClassNameOffset));
2960 __ jmp(&done, Label::kNear);
2961
2962 // Non-JS objects have class null.
2963 __ bind(&null);
2964 __ LoadRoot(rax, Heap::kNullValueRootIndex);
2965 __ jmp(&done, Label::kNear);
2966
2967 // Functions have class 'Function'.
2968 __ bind(&function);
2969 __ LoadRoot(rax, Heap::kFunction_stringRootIndex);
2970 __ jmp(&done, Label::kNear);
2971
2972 // Objects with a non-function constructor have class 'Object'.
2973 __ bind(&non_function_constructor);
2974 __ LoadRoot(rax, Heap::kObject_stringRootIndex);
2975
2976 // All done.
2977 __ bind(&done);
2978
2979 context()->Plug(rax);
2980}
2981
2982
2983void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
2984 ZoneList<Expression*>* args = expr->arguments();
2985 DCHECK(args->length() == 1);
2986
2987 VisitForAccumulatorValue(args->at(0)); // Load the object.
2988
2989 Label done;
2990 // If the object is a smi return the object.
2991 __ JumpIfSmi(rax, &done);
2992 // If the object is not a value type, return the object.
2993 __ CmpObjectType(rax, JS_VALUE_TYPE, rbx);
2994 __ j(not_equal, &done);
2995 __ movp(rax, FieldOperand(rax, JSValue::kValueOffset));
2996
2997 __ bind(&done);
2998 context()->Plug(rax);
2999}
3000
3001
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003002void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
3003 ZoneList<Expression*>* args = expr->arguments();
3004 DCHECK_EQ(3, args->length());
3005
3006 Register string = rax;
3007 Register index = rbx;
3008 Register value = rcx;
3009
3010 VisitForStackValue(args->at(0)); // index
3011 VisitForStackValue(args->at(1)); // value
3012 VisitForAccumulatorValue(args->at(2)); // string
Ben Murdoch097c5b22016-05-18 11:27:45 +01003013 PopOperand(value);
3014 PopOperand(index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003015
3016 if (FLAG_debug_code) {
3017 __ Check(__ CheckSmi(value), kNonSmiValue);
3018 __ Check(__ CheckSmi(index), kNonSmiValue);
3019 }
3020
3021 __ SmiToInteger32(value, value);
3022 __ SmiToInteger32(index, index);
3023
3024 if (FLAG_debug_code) {
3025 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
3026 __ EmitSeqStringSetCharCheck(string, index, value, one_byte_seq_type);
3027 }
3028
3029 __ movb(FieldOperand(string, index, times_1, SeqOneByteString::kHeaderSize),
3030 value);
3031 context()->Plug(string);
3032}
3033
3034
3035void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
3036 ZoneList<Expression*>* args = expr->arguments();
3037 DCHECK_EQ(3, args->length());
3038
3039 Register string = rax;
3040 Register index = rbx;
3041 Register value = rcx;
3042
3043 VisitForStackValue(args->at(0)); // index
3044 VisitForStackValue(args->at(1)); // value
3045 VisitForAccumulatorValue(args->at(2)); // string
Ben Murdoch097c5b22016-05-18 11:27:45 +01003046 PopOperand(value);
3047 PopOperand(index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003048
3049 if (FLAG_debug_code) {
3050 __ Check(__ CheckSmi(value), kNonSmiValue);
3051 __ Check(__ CheckSmi(index), kNonSmiValue);
3052 }
3053
3054 __ SmiToInteger32(value, value);
3055 __ SmiToInteger32(index, index);
3056
3057 if (FLAG_debug_code) {
3058 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
3059 __ EmitSeqStringSetCharCheck(string, index, value, two_byte_seq_type);
3060 }
3061
3062 __ movw(FieldOperand(string, index, times_2, SeqTwoByteString::kHeaderSize),
3063 value);
3064 context()->Plug(rax);
3065}
3066
3067
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003068void FullCodeGenerator::EmitToInteger(CallRuntime* expr) {
3069 ZoneList<Expression*>* args = expr->arguments();
3070 DCHECK_EQ(1, args->length());
3071
3072 // Load the argument into rax and convert it.
3073 VisitForAccumulatorValue(args->at(0));
3074
3075 // Convert the object to an integer.
3076 Label done_convert;
3077 __ JumpIfSmi(rax, &done_convert, Label::kNear);
3078 __ Push(rax);
3079 __ CallRuntime(Runtime::kToInteger);
3080 __ bind(&done_convert);
3081 context()->Plug(rax);
3082}
3083
3084
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003085void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
3086 ZoneList<Expression*>* args = expr->arguments();
3087 DCHECK(args->length() == 1);
3088
3089 VisitForAccumulatorValue(args->at(0));
3090
3091 Label done;
3092 StringCharFromCodeGenerator generator(rax, rbx);
3093 generator.GenerateFast(masm_);
3094 __ jmp(&done);
3095
3096 NopRuntimeCallHelper call_helper;
3097 generator.GenerateSlow(masm_, call_helper);
3098
3099 __ bind(&done);
3100 context()->Plug(rbx);
3101}
3102
3103
3104void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3105 ZoneList<Expression*>* args = expr->arguments();
3106 DCHECK(args->length() == 2);
3107
3108 VisitForStackValue(args->at(0));
3109 VisitForAccumulatorValue(args->at(1));
3110
3111 Register object = rbx;
3112 Register index = rax;
3113 Register result = rdx;
3114
Ben Murdoch097c5b22016-05-18 11:27:45 +01003115 PopOperand(object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003116
3117 Label need_conversion;
3118 Label index_out_of_range;
3119 Label done;
3120 StringCharCodeAtGenerator generator(object,
3121 index,
3122 result,
3123 &need_conversion,
3124 &need_conversion,
3125 &index_out_of_range,
3126 STRING_INDEX_IS_NUMBER);
3127 generator.GenerateFast(masm_);
3128 __ jmp(&done);
3129
3130 __ bind(&index_out_of_range);
3131 // When the index is out of range, the spec requires us to return
3132 // NaN.
3133 __ LoadRoot(result, Heap::kNanValueRootIndex);
3134 __ jmp(&done);
3135
3136 __ bind(&need_conversion);
3137 // Move the undefined value into the result register, which will
3138 // trigger conversion.
3139 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3140 __ jmp(&done);
3141
3142 NopRuntimeCallHelper call_helper;
3143 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
3144
3145 __ bind(&done);
3146 context()->Plug(result);
3147}
3148
3149
3150void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3151 ZoneList<Expression*>* args = expr->arguments();
3152 DCHECK(args->length() == 2);
3153
3154 VisitForStackValue(args->at(0));
3155 VisitForAccumulatorValue(args->at(1));
3156
3157 Register object = rbx;
3158 Register index = rax;
3159 Register scratch = rdx;
3160 Register result = rax;
3161
Ben Murdoch097c5b22016-05-18 11:27:45 +01003162 PopOperand(object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003163
3164 Label need_conversion;
3165 Label index_out_of_range;
3166 Label done;
3167 StringCharAtGenerator generator(object,
3168 index,
3169 scratch,
3170 result,
3171 &need_conversion,
3172 &need_conversion,
3173 &index_out_of_range,
3174 STRING_INDEX_IS_NUMBER);
3175 generator.GenerateFast(masm_);
3176 __ jmp(&done);
3177
3178 __ bind(&index_out_of_range);
3179 // When the index is out of range, the spec requires us to return
3180 // the empty string.
3181 __ LoadRoot(result, Heap::kempty_stringRootIndex);
3182 __ jmp(&done);
3183
3184 __ bind(&need_conversion);
3185 // Move smi zero into the result register, which will trigger
3186 // conversion.
3187 __ Move(result, Smi::FromInt(0));
3188 __ jmp(&done);
3189
3190 NopRuntimeCallHelper call_helper;
3191 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
3192
3193 __ bind(&done);
3194 context()->Plug(result);
3195}
3196
3197
3198void FullCodeGenerator::EmitCall(CallRuntime* expr) {
3199 ZoneList<Expression*>* args = expr->arguments();
3200 DCHECK_LE(2, args->length());
3201 // Push target, receiver and arguments onto the stack.
3202 for (Expression* const arg : *args) {
3203 VisitForStackValue(arg);
3204 }
3205 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
3206 // Move target to rdi.
3207 int const argc = args->length() - 2;
3208 __ movp(rdi, Operand(rsp, (argc + 1) * kPointerSize));
3209 // Call the target.
3210 __ Set(rax, argc);
3211 __ Call(isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003212 OperandStackDepthDecrement(argc + 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003213 // Restore context register.
3214 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
3215 // Discard the function left on TOS.
3216 context()->DropAndPlug(1, rax);
3217}
3218
3219
3220void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3221 ZoneList<Expression*>* args = expr->arguments();
3222 DCHECK(args->length() == 1);
3223
3224 VisitForAccumulatorValue(args->at(0));
3225
3226 Label materialize_true, materialize_false;
3227 Label* if_true = NULL;
3228 Label* if_false = NULL;
3229 Label* fall_through = NULL;
3230 context()->PrepareTest(&materialize_true, &materialize_false,
3231 &if_true, &if_false, &fall_through);
3232
3233 __ testl(FieldOperand(rax, String::kHashFieldOffset),
3234 Immediate(String::kContainsCachedArrayIndexMask));
3235 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3236 __ j(zero, if_true);
3237 __ jmp(if_false);
3238
3239 context()->Plug(if_true, if_false);
3240}
3241
3242
3243void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3244 ZoneList<Expression*>* args = expr->arguments();
3245 DCHECK(args->length() == 1);
3246 VisitForAccumulatorValue(args->at(0));
3247
3248 __ AssertString(rax);
3249
3250 __ movl(rax, FieldOperand(rax, String::kHashFieldOffset));
3251 DCHECK(String::kHashShift >= kSmiTagSize);
3252 __ IndexFromHash(rax, rax);
3253
3254 context()->Plug(rax);
3255}
3256
3257
3258void FullCodeGenerator::EmitGetSuperConstructor(CallRuntime* expr) {
3259 ZoneList<Expression*>* args = expr->arguments();
3260 DCHECK_EQ(1, args->length());
3261 VisitForAccumulatorValue(args->at(0));
3262 __ AssertFunction(rax);
3263 __ movp(rax, FieldOperand(rax, HeapObject::kMapOffset));
3264 __ movp(rax, FieldOperand(rax, Map::kPrototypeOffset));
3265 context()->Plug(rax);
3266}
3267
3268
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003269void FullCodeGenerator::EmitDebugIsActive(CallRuntime* expr) {
3270 DCHECK(expr->arguments()->length() == 0);
3271 ExternalReference debug_is_active =
3272 ExternalReference::debug_is_active_address(isolate());
3273 __ Move(kScratchRegister, debug_is_active);
3274 __ movzxbp(rax, Operand(kScratchRegister, 0));
3275 __ Integer32ToSmi(rax, rax);
3276 context()->Plug(rax);
3277}
3278
3279
3280void FullCodeGenerator::EmitCreateIterResultObject(CallRuntime* expr) {
3281 ZoneList<Expression*>* args = expr->arguments();
3282 DCHECK_EQ(2, args->length());
3283 VisitForStackValue(args->at(0));
3284 VisitForStackValue(args->at(1));
3285
3286 Label runtime, done;
3287
3288 __ Allocate(JSIteratorResult::kSize, rax, rcx, rdx, &runtime, TAG_OBJECT);
3289 __ LoadNativeContextSlot(Context::ITERATOR_RESULT_MAP_INDEX, rbx);
3290 __ movp(FieldOperand(rax, HeapObject::kMapOffset), rbx);
3291 __ LoadRoot(rbx, Heap::kEmptyFixedArrayRootIndex);
3292 __ movp(FieldOperand(rax, JSObject::kPropertiesOffset), rbx);
3293 __ movp(FieldOperand(rax, JSObject::kElementsOffset), rbx);
3294 __ Pop(FieldOperand(rax, JSIteratorResult::kDoneOffset));
3295 __ Pop(FieldOperand(rax, JSIteratorResult::kValueOffset));
3296 STATIC_ASSERT(JSIteratorResult::kSize == 5 * kPointerSize);
3297 __ jmp(&done, Label::kNear);
3298
3299 __ bind(&runtime);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003300 CallRuntimeWithOperands(Runtime::kCreateIterResultObject);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003301
3302 __ bind(&done);
3303 context()->Plug(rax);
3304}
3305
3306
3307void FullCodeGenerator::EmitLoadJSRuntimeFunction(CallRuntime* expr) {
3308 // Push the builtins object as receiver.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003309 OperandStackDepthIncrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003310 __ PushRoot(Heap::kUndefinedValueRootIndex);
3311
3312 __ LoadNativeContextSlot(expr->context_index(), rax);
3313}
3314
3315
3316void FullCodeGenerator::EmitCallJSRuntimeFunction(CallRuntime* expr) {
3317 ZoneList<Expression*>* args = expr->arguments();
3318 int arg_count = args->length();
3319
3320 SetCallPosition(expr);
3321 __ movp(rdi, Operand(rsp, (arg_count + 1) * kPointerSize));
3322 __ Set(rax, arg_count);
3323 __ Call(isolate()->builtins()->Call(ConvertReceiverMode::kNullOrUndefined),
3324 RelocInfo::CODE_TARGET);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003325 OperandStackDepthDecrement(arg_count + 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003326}
3327
3328
3329void FullCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
3330 ZoneList<Expression*>* args = expr->arguments();
3331 int arg_count = args->length();
3332
3333 if (expr->is_jsruntime()) {
3334 Comment cmnt(masm_, "[ CallRuntime");
3335
3336 EmitLoadJSRuntimeFunction(expr);
3337
3338 // Push the target function under the receiver.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003339 PushOperand(Operand(rsp, 0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003340 __ movp(Operand(rsp, kPointerSize), rax);
3341
3342 // Push the arguments ("left-to-right").
3343 for (int i = 0; i < arg_count; i++) {
3344 VisitForStackValue(args->at(i));
3345 }
3346
3347 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
3348 EmitCallJSRuntimeFunction(expr);
3349
3350 // Restore context register.
3351 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
3352 context()->DropAndPlug(1, rax);
3353
3354 } else {
3355 const Runtime::Function* function = expr->function();
3356 switch (function->function_id) {
3357#define CALL_INTRINSIC_GENERATOR(Name) \
3358 case Runtime::kInline##Name: { \
3359 Comment cmnt(masm_, "[ Inline" #Name); \
3360 return Emit##Name(expr); \
3361 }
3362 FOR_EACH_FULL_CODE_INTRINSIC(CALL_INTRINSIC_GENERATOR)
3363#undef CALL_INTRINSIC_GENERATOR
3364 default: {
3365 Comment cmnt(masm_, "[ CallRuntime for unhandled intrinsic");
3366 // Push the arguments ("left-to-right").
3367 for (int i = 0; i < arg_count; i++) {
3368 VisitForStackValue(args->at(i));
3369 }
3370
3371 // Call the C runtime.
3372 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
3373 __ CallRuntime(function, arg_count);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003374 OperandStackDepthDecrement(arg_count);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003375 context()->Plug(rax);
3376 }
3377 }
3378 }
3379}
3380
3381
3382void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
3383 switch (expr->op()) {
3384 case Token::DELETE: {
3385 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
3386 Property* property = expr->expression()->AsProperty();
3387 VariableProxy* proxy = expr->expression()->AsVariableProxy();
3388
3389 if (property != NULL) {
3390 VisitForStackValue(property->obj());
3391 VisitForStackValue(property->key());
Ben Murdoch097c5b22016-05-18 11:27:45 +01003392 CallRuntimeWithOperands(is_strict(language_mode())
3393 ? Runtime::kDeleteProperty_Strict
3394 : Runtime::kDeleteProperty_Sloppy);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003395 context()->Plug(rax);
3396 } else if (proxy != NULL) {
3397 Variable* var = proxy->var();
3398 // Delete of an unqualified identifier is disallowed in strict mode but
3399 // "delete this" is allowed.
3400 bool is_this = var->HasThisName(isolate());
3401 DCHECK(is_sloppy(language_mode()) || is_this);
3402 if (var->IsUnallocatedOrGlobalSlot()) {
3403 __ movp(rax, NativeContextOperand());
3404 __ Push(ContextOperand(rax, Context::EXTENSION_INDEX));
3405 __ Push(var->name());
3406 __ CallRuntime(Runtime::kDeleteProperty_Sloppy);
3407 context()->Plug(rax);
3408 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
3409 // Result of deleting non-global variables is false. 'this' is
3410 // not really a variable, though we implement it as one. The
3411 // subexpression does not have side effects.
3412 context()->Plug(is_this);
3413 } else {
3414 // Non-global variable. Call the runtime to try to delete from the
3415 // context where the variable was introduced.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003416 __ Push(var->name());
3417 __ CallRuntime(Runtime::kDeleteLookupSlot);
3418 context()->Plug(rax);
3419 }
3420 } else {
3421 // Result of deleting non-property, non-variable reference is true.
3422 // The subexpression may have side effects.
3423 VisitForEffect(expr->expression());
3424 context()->Plug(true);
3425 }
3426 break;
3427 }
3428
3429 case Token::VOID: {
3430 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3431 VisitForEffect(expr->expression());
3432 context()->Plug(Heap::kUndefinedValueRootIndex);
3433 break;
3434 }
3435
3436 case Token::NOT: {
3437 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3438 if (context()->IsEffect()) {
3439 // Unary NOT has no side effects so it's only necessary to visit the
3440 // subexpression. Match the optimizing compiler by not branching.
3441 VisitForEffect(expr->expression());
3442 } else if (context()->IsTest()) {
3443 const TestContext* test = TestContext::cast(context());
3444 // The labels are swapped for the recursive call.
3445 VisitForControl(expr->expression(),
3446 test->false_label(),
3447 test->true_label(),
3448 test->fall_through());
3449 context()->Plug(test->true_label(), test->false_label());
3450 } else {
3451 // We handle value contexts explicitly rather than simply visiting
3452 // for control and plugging the control flow into the context,
3453 // because we need to prepare a pair of extra administrative AST ids
3454 // for the optimizing compiler.
3455 DCHECK(context()->IsAccumulatorValue() || context()->IsStackValue());
3456 Label materialize_true, materialize_false, done;
3457 VisitForControl(expr->expression(),
3458 &materialize_false,
3459 &materialize_true,
3460 &materialize_true);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003461 if (!context()->IsAccumulatorValue()) OperandStackDepthIncrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003462 __ bind(&materialize_true);
3463 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
3464 if (context()->IsAccumulatorValue()) {
3465 __ LoadRoot(rax, Heap::kTrueValueRootIndex);
3466 } else {
3467 __ PushRoot(Heap::kTrueValueRootIndex);
3468 }
3469 __ jmp(&done, Label::kNear);
3470 __ bind(&materialize_false);
3471 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
3472 if (context()->IsAccumulatorValue()) {
3473 __ LoadRoot(rax, Heap::kFalseValueRootIndex);
3474 } else {
3475 __ PushRoot(Heap::kFalseValueRootIndex);
3476 }
3477 __ bind(&done);
3478 }
3479 break;
3480 }
3481
3482 case Token::TYPEOF: {
3483 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3484 {
3485 AccumulatorValueContext context(this);
3486 VisitForTypeofValue(expr->expression());
3487 }
3488 __ movp(rbx, rax);
3489 TypeofStub typeof_stub(isolate());
3490 __ CallStub(&typeof_stub);
3491 context()->Plug(rax);
3492 break;
3493 }
3494
3495 default:
3496 UNREACHABLE();
3497 }
3498}
3499
3500
3501void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
3502 DCHECK(expr->expression()->IsValidReferenceExpressionOrThis());
3503
3504 Comment cmnt(masm_, "[ CountOperation");
3505
3506 Property* prop = expr->expression()->AsProperty();
3507 LhsKind assign_type = Property::GetAssignType(prop);
3508
3509 // Evaluate expression and get value.
3510 if (assign_type == VARIABLE) {
3511 DCHECK(expr->expression()->AsVariableProxy()->var() != NULL);
3512 AccumulatorValueContext context(this);
3513 EmitVariableLoad(expr->expression()->AsVariableProxy());
3514 } else {
3515 // Reserve space for result of postfix operation.
3516 if (expr->is_postfix() && !context()->IsEffect()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003517 PushOperand(Smi::FromInt(0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003518 }
3519 switch (assign_type) {
3520 case NAMED_PROPERTY: {
3521 VisitForStackValue(prop->obj());
3522 __ movp(LoadDescriptor::ReceiverRegister(), Operand(rsp, 0));
3523 EmitNamedPropertyLoad(prop);
3524 break;
3525 }
3526
3527 case NAMED_SUPER_PROPERTY: {
3528 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
3529 VisitForAccumulatorValue(
3530 prop->obj()->AsSuperPropertyReference()->home_object());
Ben Murdoch097c5b22016-05-18 11:27:45 +01003531 PushOperand(result_register());
3532 PushOperand(MemOperand(rsp, kPointerSize));
3533 PushOperand(result_register());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003534 EmitNamedSuperPropertyLoad(prop);
3535 break;
3536 }
3537
3538 case KEYED_SUPER_PROPERTY: {
3539 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
3540 VisitForStackValue(
3541 prop->obj()->AsSuperPropertyReference()->home_object());
3542 VisitForAccumulatorValue(prop->key());
Ben Murdoch097c5b22016-05-18 11:27:45 +01003543 PushOperand(result_register());
3544 PushOperand(MemOperand(rsp, 2 * kPointerSize));
3545 PushOperand(MemOperand(rsp, 2 * kPointerSize));
3546 PushOperand(result_register());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003547 EmitKeyedSuperPropertyLoad(prop);
3548 break;
3549 }
3550
3551 case KEYED_PROPERTY: {
3552 VisitForStackValue(prop->obj());
3553 VisitForStackValue(prop->key());
3554 // Leave receiver on stack
3555 __ movp(LoadDescriptor::ReceiverRegister(), Operand(rsp, kPointerSize));
3556 // Copy of key, needed for later store.
3557 __ movp(LoadDescriptor::NameRegister(), Operand(rsp, 0));
3558 EmitKeyedPropertyLoad(prop);
3559 break;
3560 }
3561
3562 case VARIABLE:
3563 UNREACHABLE();
3564 }
3565 }
3566
3567 // We need a second deoptimization point after loading the value
3568 // in case evaluating the property load my have a side effect.
3569 if (assign_type == VARIABLE) {
3570 PrepareForBailout(expr->expression(), TOS_REG);
3571 } else {
3572 PrepareForBailoutForId(prop->LoadId(), TOS_REG);
3573 }
3574
3575 // Inline smi case if we are in a loop.
3576 Label done, stub_call;
3577 JumpPatchSite patch_site(masm_);
3578 if (ShouldInlineSmiCase(expr->op())) {
3579 Label slow;
3580 patch_site.EmitJumpIfNotSmi(rax, &slow, Label::kNear);
3581
3582 // Save result for postfix expressions.
3583 if (expr->is_postfix()) {
3584 if (!context()->IsEffect()) {
3585 // Save the result on the stack. If we have a named or keyed property
3586 // we store the result under the receiver that is currently on top
3587 // of the stack.
3588 switch (assign_type) {
3589 case VARIABLE:
3590 __ Push(rax);
3591 break;
3592 case NAMED_PROPERTY:
3593 __ movp(Operand(rsp, kPointerSize), rax);
3594 break;
3595 case NAMED_SUPER_PROPERTY:
3596 __ movp(Operand(rsp, 2 * kPointerSize), rax);
3597 break;
3598 case KEYED_PROPERTY:
3599 __ movp(Operand(rsp, 2 * kPointerSize), rax);
3600 break;
3601 case KEYED_SUPER_PROPERTY:
3602 __ movp(Operand(rsp, 3 * kPointerSize), rax);
3603 break;
3604 }
3605 }
3606 }
3607
3608 SmiOperationConstraints constraints =
3609 SmiOperationConstraint::kPreserveSourceRegister |
3610 SmiOperationConstraint::kBailoutOnNoOverflow;
3611 if (expr->op() == Token::INC) {
3612 __ SmiAddConstant(rax, rax, Smi::FromInt(1), constraints, &done,
3613 Label::kNear);
3614 } else {
3615 __ SmiSubConstant(rax, rax, Smi::FromInt(1), constraints, &done,
3616 Label::kNear);
3617 }
3618 __ jmp(&stub_call, Label::kNear);
3619 __ bind(&slow);
3620 }
3621 if (!is_strong(language_mode())) {
3622 ToNumberStub convert_stub(isolate());
3623 __ CallStub(&convert_stub);
3624 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
3625 }
3626
3627 // Save result for postfix expressions.
3628 if (expr->is_postfix()) {
3629 if (!context()->IsEffect()) {
3630 // Save the result on the stack. If we have a named or keyed property
3631 // we store the result under the receiver that is currently on top
3632 // of the stack.
3633 switch (assign_type) {
3634 case VARIABLE:
Ben Murdoch097c5b22016-05-18 11:27:45 +01003635 PushOperand(rax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003636 break;
3637 case NAMED_PROPERTY:
3638 __ movp(Operand(rsp, kPointerSize), rax);
3639 break;
3640 case NAMED_SUPER_PROPERTY:
3641 __ movp(Operand(rsp, 2 * kPointerSize), rax);
3642 break;
3643 case KEYED_PROPERTY:
3644 __ movp(Operand(rsp, 2 * kPointerSize), rax);
3645 break;
3646 case KEYED_SUPER_PROPERTY:
3647 __ movp(Operand(rsp, 3 * kPointerSize), rax);
3648 break;
3649 }
3650 }
3651 }
3652
3653 SetExpressionPosition(expr);
3654
3655 // Call stub for +1/-1.
3656 __ bind(&stub_call);
3657 __ movp(rdx, rax);
3658 __ Move(rax, Smi::FromInt(1));
Ben Murdoch097c5b22016-05-18 11:27:45 +01003659 Handle<Code> code =
3660 CodeFactory::BinaryOpIC(isolate(), expr->binary_op()).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003661 CallIC(code, expr->CountBinOpFeedbackId());
3662 patch_site.EmitPatchInfo();
3663 __ bind(&done);
3664
3665 if (is_strong(language_mode())) {
3666 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
3667 }
3668 // Store the value returned in rax.
3669 switch (assign_type) {
3670 case VARIABLE:
3671 if (expr->is_postfix()) {
3672 // Perform the assignment as if via '='.
3673 { EffectContext context(this);
3674 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3675 Token::ASSIGN, expr->CountSlot());
3676 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3677 context.Plug(rax);
3678 }
3679 // For all contexts except kEffect: We have the result on
3680 // top of the stack.
3681 if (!context()->IsEffect()) {
3682 context()->PlugTOS();
3683 }
3684 } else {
3685 // Perform the assignment as if via '='.
3686 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3687 Token::ASSIGN, expr->CountSlot());
3688 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3689 context()->Plug(rax);
3690 }
3691 break;
3692 case NAMED_PROPERTY: {
3693 __ Move(StoreDescriptor::NameRegister(),
3694 prop->key()->AsLiteral()->value());
Ben Murdoch097c5b22016-05-18 11:27:45 +01003695 PopOperand(StoreDescriptor::ReceiverRegister());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003696 EmitLoadStoreICSlot(expr->CountSlot());
3697 CallStoreIC();
3698 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3699 if (expr->is_postfix()) {
3700 if (!context()->IsEffect()) {
3701 context()->PlugTOS();
3702 }
3703 } else {
3704 context()->Plug(rax);
3705 }
3706 break;
3707 }
3708 case NAMED_SUPER_PROPERTY: {
3709 EmitNamedSuperPropertyStore(prop);
3710 if (expr->is_postfix()) {
3711 if (!context()->IsEffect()) {
3712 context()->PlugTOS();
3713 }
3714 } else {
3715 context()->Plug(rax);
3716 }
3717 break;
3718 }
3719 case KEYED_SUPER_PROPERTY: {
3720 EmitKeyedSuperPropertyStore(prop);
3721 if (expr->is_postfix()) {
3722 if (!context()->IsEffect()) {
3723 context()->PlugTOS();
3724 }
3725 } else {
3726 context()->Plug(rax);
3727 }
3728 break;
3729 }
3730 case KEYED_PROPERTY: {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003731 PopOperand(StoreDescriptor::NameRegister());
3732 PopOperand(StoreDescriptor::ReceiverRegister());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003733 Handle<Code> ic =
3734 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
3735 EmitLoadStoreICSlot(expr->CountSlot());
3736 CallIC(ic);
3737 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3738 if (expr->is_postfix()) {
3739 if (!context()->IsEffect()) {
3740 context()->PlugTOS();
3741 }
3742 } else {
3743 context()->Plug(rax);
3744 }
3745 break;
3746 }
3747 }
3748}
3749
3750
3751void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
3752 Expression* sub_expr,
3753 Handle<String> check) {
3754 Label materialize_true, materialize_false;
3755 Label* if_true = NULL;
3756 Label* if_false = NULL;
3757 Label* fall_through = NULL;
3758 context()->PrepareTest(&materialize_true, &materialize_false,
3759 &if_true, &if_false, &fall_through);
3760
3761 { AccumulatorValueContext context(this);
3762 VisitForTypeofValue(sub_expr);
3763 }
3764 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3765
3766 Factory* factory = isolate()->factory();
3767 if (String::Equals(check, factory->number_string())) {
3768 __ JumpIfSmi(rax, if_true);
3769 __ movp(rax, FieldOperand(rax, HeapObject::kMapOffset));
3770 __ CompareRoot(rax, Heap::kHeapNumberMapRootIndex);
3771 Split(equal, if_true, if_false, fall_through);
3772 } else if (String::Equals(check, factory->string_string())) {
3773 __ JumpIfSmi(rax, if_false);
3774 __ CmpObjectType(rax, FIRST_NONSTRING_TYPE, rdx);
3775 Split(below, if_true, if_false, fall_through);
3776 } else if (String::Equals(check, factory->symbol_string())) {
3777 __ JumpIfSmi(rax, if_false);
3778 __ CmpObjectType(rax, SYMBOL_TYPE, rdx);
3779 Split(equal, if_true, if_false, fall_through);
3780 } else if (String::Equals(check, factory->boolean_string())) {
3781 __ CompareRoot(rax, Heap::kTrueValueRootIndex);
3782 __ j(equal, if_true);
3783 __ CompareRoot(rax, Heap::kFalseValueRootIndex);
3784 Split(equal, if_true, if_false, fall_through);
3785 } else if (String::Equals(check, factory->undefined_string())) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003786 __ CompareRoot(rax, Heap::kNullValueRootIndex);
3787 __ j(equal, if_false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003788 __ JumpIfSmi(rax, if_false);
3789 // Check for undetectable objects => true.
3790 __ movp(rdx, FieldOperand(rax, HeapObject::kMapOffset));
3791 __ testb(FieldOperand(rdx, Map::kBitFieldOffset),
3792 Immediate(1 << Map::kIsUndetectable));
3793 Split(not_zero, if_true, if_false, fall_through);
3794 } else if (String::Equals(check, factory->function_string())) {
3795 __ JumpIfSmi(rax, if_false);
3796 // Check for callable and not undetectable objects => true.
3797 __ movp(rdx, FieldOperand(rax, HeapObject::kMapOffset));
3798 __ movzxbl(rdx, FieldOperand(rdx, Map::kBitFieldOffset));
3799 __ andb(rdx,
3800 Immediate((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable)));
3801 __ cmpb(rdx, Immediate(1 << Map::kIsCallable));
3802 Split(equal, if_true, if_false, fall_through);
3803 } else if (String::Equals(check, factory->object_string())) {
3804 __ JumpIfSmi(rax, if_false);
3805 __ CompareRoot(rax, Heap::kNullValueRootIndex);
3806 __ j(equal, if_true);
3807 STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
3808 __ CmpObjectType(rax, FIRST_JS_RECEIVER_TYPE, rdx);
3809 __ j(below, if_false);
3810 // Check for callable or undetectable objects => false.
3811 __ testb(FieldOperand(rdx, Map::kBitFieldOffset),
3812 Immediate((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable)));
3813 Split(zero, if_true, if_false, fall_through);
3814// clang-format off
3815#define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
3816 } else if (String::Equals(check, factory->type##_string())) { \
3817 __ JumpIfSmi(rax, if_false); \
3818 __ movp(rax, FieldOperand(rax, HeapObject::kMapOffset)); \
3819 __ CompareRoot(rax, Heap::k##Type##MapRootIndex); \
3820 Split(equal, if_true, if_false, fall_through);
3821 SIMD128_TYPES(SIMD128_TYPE)
3822#undef SIMD128_TYPE
3823 // clang-format on
3824 } else {
3825 if (if_false != fall_through) __ jmp(if_false);
3826 }
3827 context()->Plug(if_true, if_false);
3828}
3829
3830
3831void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
3832 Comment cmnt(masm_, "[ CompareOperation");
3833 SetExpressionPosition(expr);
3834
3835 // First we try a fast inlined version of the compare when one of
3836 // the operands is a literal.
3837 if (TryLiteralCompare(expr)) return;
3838
3839 // Always perform the comparison for its control flow. Pack the result
3840 // into the expression's context after the comparison is performed.
3841 Label materialize_true, materialize_false;
3842 Label* if_true = NULL;
3843 Label* if_false = NULL;
3844 Label* fall_through = NULL;
3845 context()->PrepareTest(&materialize_true, &materialize_false,
3846 &if_true, &if_false, &fall_through);
3847
3848 Token::Value op = expr->op();
3849 VisitForStackValue(expr->left());
3850 switch (op) {
3851 case Token::IN:
3852 VisitForStackValue(expr->right());
Ben Murdoch097c5b22016-05-18 11:27:45 +01003853 CallRuntimeWithOperands(Runtime::kHasProperty);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003854 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
3855 __ CompareRoot(rax, Heap::kTrueValueRootIndex);
3856 Split(equal, if_true, if_false, fall_through);
3857 break;
3858
3859 case Token::INSTANCEOF: {
3860 VisitForAccumulatorValue(expr->right());
Ben Murdoch097c5b22016-05-18 11:27:45 +01003861 PopOperand(rdx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003862 InstanceOfStub stub(isolate());
3863 __ CallStub(&stub);
3864 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
3865 __ CompareRoot(rax, Heap::kTrueValueRootIndex);
3866 Split(equal, if_true, if_false, fall_through);
3867 break;
3868 }
3869
3870 default: {
3871 VisitForAccumulatorValue(expr->right());
3872 Condition cc = CompareIC::ComputeCondition(op);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003873 PopOperand(rdx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003874
3875 bool inline_smi_code = ShouldInlineSmiCase(op);
3876 JumpPatchSite patch_site(masm_);
3877 if (inline_smi_code) {
3878 Label slow_case;
3879 __ movp(rcx, rdx);
3880 __ orp(rcx, rax);
3881 patch_site.EmitJumpIfNotSmi(rcx, &slow_case, Label::kNear);
3882 __ cmpp(rdx, rax);
3883 Split(cc, if_true, if_false, NULL);
3884 __ bind(&slow_case);
3885 }
3886
Ben Murdoch097c5b22016-05-18 11:27:45 +01003887 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003888 CallIC(ic, expr->CompareOperationFeedbackId());
3889 patch_site.EmitPatchInfo();
3890
3891 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3892 __ testp(rax, rax);
3893 Split(cc, if_true, if_false, fall_through);
3894 }
3895 }
3896
3897 // Convert the result of the comparison into one expected for this
3898 // expression's context.
3899 context()->Plug(if_true, if_false);
3900}
3901
3902
3903void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
3904 Expression* sub_expr,
3905 NilValue nil) {
3906 Label materialize_true, materialize_false;
3907 Label* if_true = NULL;
3908 Label* if_false = NULL;
3909 Label* fall_through = NULL;
3910 context()->PrepareTest(&materialize_true, &materialize_false,
3911 &if_true, &if_false, &fall_through);
3912
3913 VisitForAccumulatorValue(sub_expr);
3914 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3915 if (expr->op() == Token::EQ_STRICT) {
3916 Heap::RootListIndex nil_value = nil == kNullValue ?
3917 Heap::kNullValueRootIndex :
3918 Heap::kUndefinedValueRootIndex;
3919 __ CompareRoot(rax, nil_value);
3920 Split(equal, if_true, if_false, fall_through);
3921 } else {
3922 Handle<Code> ic = CompareNilICStub::GetUninitialized(isolate(), nil);
3923 CallIC(ic, expr->CompareOperationFeedbackId());
3924 __ CompareRoot(rax, Heap::kTrueValueRootIndex);
3925 Split(equal, if_true, if_false, fall_through);
3926 }
3927 context()->Plug(if_true, if_false);
3928}
3929
3930
3931void FullCodeGenerator::VisitThisFunction(ThisFunction* expr) {
3932 __ movp(rax, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
3933 context()->Plug(rax);
3934}
3935
3936
3937Register FullCodeGenerator::result_register() {
3938 return rax;
3939}
3940
3941
3942Register FullCodeGenerator::context_register() {
3943 return rsi;
3944}
3945
3946
3947void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
3948 DCHECK(IsAligned(frame_offset, kPointerSize));
3949 __ movp(Operand(rbp, frame_offset), value);
3950}
3951
3952
3953void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
3954 __ movp(dst, ContextOperand(rsi, context_index));
3955}
3956
3957
3958void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
3959 Scope* closure_scope = scope()->ClosureScope();
3960 if (closure_scope->is_script_scope() ||
3961 closure_scope->is_module_scope()) {
3962 // Contexts nested in the native context have a canonical empty function
3963 // as their closure, not the anonymous closure containing the global
3964 // code.
3965 __ movp(rax, NativeContextOperand());
Ben Murdoch097c5b22016-05-18 11:27:45 +01003966 PushOperand(ContextOperand(rax, Context::CLOSURE_INDEX));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003967 } else if (closure_scope->is_eval_scope()) {
3968 // Contexts created by a call to eval have the same closure as the
3969 // context calling eval, not the anonymous closure containing the eval
3970 // code. Fetch it from the context.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003971 PushOperand(ContextOperand(rsi, Context::CLOSURE_INDEX));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003972 } else {
3973 DCHECK(closure_scope->is_function_scope());
Ben Murdoch097c5b22016-05-18 11:27:45 +01003974 PushOperand(Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003975 }
3976}
3977
3978
3979// ----------------------------------------------------------------------------
3980// Non-local control flow support.
3981
3982
3983void FullCodeGenerator::EnterFinallyBlock() {
3984 DCHECK(!result_register().is(rdx));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003985
3986 // Store pending message while executing finally block.
3987 ExternalReference pending_message_obj =
3988 ExternalReference::address_of_pending_message_obj(isolate());
3989 __ Load(rdx, pending_message_obj);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003990 PushOperand(rdx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003991
3992 ClearPendingMessage();
3993}
3994
3995
3996void FullCodeGenerator::ExitFinallyBlock() {
3997 DCHECK(!result_register().is(rdx));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003998 // Restore pending message from stack.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003999 PopOperand(rdx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004000 ExternalReference pending_message_obj =
4001 ExternalReference::address_of_pending_message_obj(isolate());
4002 __ Store(pending_message_obj, rdx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004003}
4004
4005
4006void FullCodeGenerator::ClearPendingMessage() {
4007 DCHECK(!result_register().is(rdx));
4008 ExternalReference pending_message_obj =
4009 ExternalReference::address_of_pending_message_obj(isolate());
4010 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
4011 __ Store(pending_message_obj, rdx);
4012}
4013
4014
4015void FullCodeGenerator::EmitLoadStoreICSlot(FeedbackVectorSlot slot) {
4016 DCHECK(!slot.IsInvalid());
4017 __ Move(VectorStoreICTrampolineDescriptor::SlotRegister(), SmiFromSlot(slot));
4018}
4019
Ben Murdoch097c5b22016-05-18 11:27:45 +01004020void FullCodeGenerator::DeferredCommands::EmitCommands() {
4021 __ Pop(result_register()); // Restore the accumulator.
4022 __ Pop(rdx); // Get the token.
4023 for (DeferredCommand cmd : commands_) {
4024 Label skip;
4025 __ SmiCompare(rdx, Smi::FromInt(cmd.token));
4026 __ j(not_equal, &skip);
4027 switch (cmd.command) {
4028 case kReturn:
4029 codegen_->EmitUnwindAndReturn();
4030 break;
4031 case kThrow:
4032 __ Push(result_register());
4033 __ CallRuntime(Runtime::kReThrow);
4034 break;
4035 case kContinue:
4036 codegen_->EmitContinue(cmd.target);
4037 break;
4038 case kBreak:
4039 codegen_->EmitBreak(cmd.target);
4040 break;
4041 }
4042 __ bind(&skip);
4043 }
4044}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004045
4046#undef __
4047
4048
4049static const byte kJnsInstruction = 0x79;
4050static const byte kNopByteOne = 0x66;
4051static const byte kNopByteTwo = 0x90;
4052#ifdef DEBUG
4053static const byte kCallInstruction = 0xe8;
4054#endif
4055
4056
4057void BackEdgeTable::PatchAt(Code* unoptimized_code,
4058 Address pc,
4059 BackEdgeState target_state,
4060 Code* replacement_code) {
4061 Address call_target_address = pc - kIntSize;
4062 Address jns_instr_address = call_target_address - 3;
4063 Address jns_offset_address = call_target_address - 2;
4064
4065 switch (target_state) {
4066 case INTERRUPT:
4067 // sub <profiling_counter>, <delta> ;; Not changed
4068 // jns ok
4069 // call <interrupt stub>
4070 // ok:
4071 *jns_instr_address = kJnsInstruction;
4072 *jns_offset_address = kJnsOffset;
4073 break;
4074 case ON_STACK_REPLACEMENT:
4075 case OSR_AFTER_STACK_CHECK:
4076 // sub <profiling_counter>, <delta> ;; Not changed
4077 // nop
4078 // nop
4079 // call <on-stack replacment>
4080 // ok:
4081 *jns_instr_address = kNopByteOne;
4082 *jns_offset_address = kNopByteTwo;
4083 break;
4084 }
4085
4086 Assembler::set_target_address_at(unoptimized_code->GetIsolate(),
4087 call_target_address, unoptimized_code,
4088 replacement_code->entry());
4089 unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
4090 unoptimized_code, call_target_address, replacement_code);
4091}
4092
4093
4094BackEdgeTable::BackEdgeState BackEdgeTable::GetBackEdgeState(
4095 Isolate* isolate,
4096 Code* unoptimized_code,
4097 Address pc) {
4098 Address call_target_address = pc - kIntSize;
4099 Address jns_instr_address = call_target_address - 3;
4100 DCHECK_EQ(kCallInstruction, *(call_target_address - 1));
4101
4102 if (*jns_instr_address == kJnsInstruction) {
4103 DCHECK_EQ(kJnsOffset, *(call_target_address - 2));
4104 DCHECK_EQ(isolate->builtins()->InterruptCheck()->entry(),
4105 Assembler::target_address_at(call_target_address,
4106 unoptimized_code));
4107 return INTERRUPT;
4108 }
4109
4110 DCHECK_EQ(kNopByteOne, *jns_instr_address);
4111 DCHECK_EQ(kNopByteTwo, *(call_target_address - 2));
4112
4113 if (Assembler::target_address_at(call_target_address,
4114 unoptimized_code) ==
4115 isolate->builtins()->OnStackReplacement()->entry()) {
4116 return ON_STACK_REPLACEMENT;
4117 }
4118
4119 DCHECK_EQ(isolate->builtins()->OsrAfterStackCheck()->entry(),
4120 Assembler::target_address_at(call_target_address,
4121 unoptimized_code));
4122 return OSR_AFTER_STACK_CHECK;
4123}
4124
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004125} // namespace internal
4126} // namespace v8
4127
4128#endif // V8_TARGET_ARCH_X64