blob: 992e7fe4f72dd85f70b0cdde38f0e2630d943ded [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.
Ben Murdochda12d292016-06-02 14:46:10 +0100289 PrepareForBailoutForId(BailoutId::FunctionEntry(), NO_REGISTERS);
290 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000291 Comment cmnt(masm_, "[ Declarations");
Ben Murdochda12d292016-06-02 14:46:10 +0100292 VisitDeclarations(scope()->declarations());
293 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000294
Ben Murdochda12d292016-06-02 14:46:10 +0100295 // Assert that the declarations do not use ICs. Otherwise the debugger
296 // won't be able to redirect a PC at an IC to the correct IC in newly
297 // recompiled code.
298 DCHECK_EQ(0, ic_total_count_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000299
Ben Murdochda12d292016-06-02 14:46:10 +0100300 {
301 Comment cmnt(masm_, "[ Stack check");
302 PrepareForBailoutForId(BailoutId::Declarations(), NO_REGISTERS);
303 Label ok;
304 __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
305 __ j(above_equal, &ok, Label::kNear);
306 __ call(isolate()->builtins()->StackCheck(), RelocInfo::CODE_TARGET);
307 __ bind(&ok);
308 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000309
Ben Murdochda12d292016-06-02 14:46:10 +0100310 {
311 Comment cmnt(masm_, "[ Body");
312 DCHECK(loop_depth() == 0);
313 VisitStatements(literal()->body());
314 DCHECK(loop_depth() == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000315 }
316
317 // Always emit a 'return undefined' in case control fell off the end of
318 // the body.
319 { Comment cmnt(masm_, "[ return <undefined>;");
320 __ LoadRoot(rax, Heap::kUndefinedValueRootIndex);
321 EmitReturnSequence();
322 }
323}
324
325
326void FullCodeGenerator::ClearAccumulator() {
327 __ Set(rax, 0);
328}
329
330
331void FullCodeGenerator::EmitProfilingCounterDecrement(int delta) {
332 __ Move(rbx, profiling_counter_, RelocInfo::EMBEDDED_OBJECT);
333 __ SmiAddConstant(FieldOperand(rbx, Cell::kValueOffset),
334 Smi::FromInt(-delta));
335}
336
337
338void FullCodeGenerator::EmitProfilingCounterReset() {
339 int reset_value = FLAG_interrupt_budget;
340 __ Move(rbx, profiling_counter_, RelocInfo::EMBEDDED_OBJECT);
341 __ Move(kScratchRegister, Smi::FromInt(reset_value));
342 __ movp(FieldOperand(rbx, Cell::kValueOffset), kScratchRegister);
343}
344
345
346static const byte kJnsOffset = kPointerSize == kInt64Size ? 0x1d : 0x14;
347
348
349void FullCodeGenerator::EmitBackEdgeBookkeeping(IterationStatement* stmt,
350 Label* back_edge_target) {
351 Comment cmnt(masm_, "[ Back edge bookkeeping");
352 Label ok;
353
354 DCHECK(back_edge_target->is_bound());
355 int distance = masm_->SizeOfCodeGeneratedSince(back_edge_target);
356 int weight = Min(kMaxBackEdgeWeight,
357 Max(1, distance / kCodeSizeMultiplier));
358 EmitProfilingCounterDecrement(weight);
359
360 __ j(positive, &ok, Label::kNear);
361 {
362 PredictableCodeSizeScope predictible_code_size_scope(masm_, kJnsOffset);
363 DontEmitDebugCodeScope dont_emit_debug_code_scope(masm_);
364 __ call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
365
366 // Record a mapping of this PC offset to the OSR id. This is used to find
367 // the AST id from the unoptimized code in order to use it as a key into
368 // the deoptimization input data found in the optimized code.
369 RecordBackEdge(stmt->OsrEntryId());
370
371 EmitProfilingCounterReset();
372 }
373 __ bind(&ok);
374
375 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
376 // Record a mapping of the OSR id to this PC. This is used if the OSR
377 // entry becomes the target of a bailout. We don't expect it to be, but
378 // we want it to work if it is.
379 PrepareForBailoutForId(stmt->OsrEntryId(), NO_REGISTERS);
380}
381
Ben Murdoch097c5b22016-05-18 11:27:45 +0100382void FullCodeGenerator::EmitProfilingCounterHandlingForReturnSequence(
383 bool is_tail_call) {
384 // Pretend that the exit is a backwards jump to the entry.
385 int weight = 1;
386 if (info_->ShouldSelfOptimize()) {
387 weight = FLAG_interrupt_budget / FLAG_self_opt_count;
388 } else {
389 int distance = masm_->pc_offset();
390 weight = Min(kMaxBackEdgeWeight, Max(1, distance / kCodeSizeMultiplier));
391 }
392 EmitProfilingCounterDecrement(weight);
393 Label ok;
394 __ j(positive, &ok, Label::kNear);
395 // Don't need to save result register if we are going to do a tail call.
396 if (!is_tail_call) {
397 __ Push(rax);
398 }
399 __ call(isolate()->builtins()->InterruptCheck(), RelocInfo::CODE_TARGET);
400 if (!is_tail_call) {
401 __ Pop(rax);
402 }
403 EmitProfilingCounterReset();
404 __ bind(&ok);
405}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000406
407void FullCodeGenerator::EmitReturnSequence() {
408 Comment cmnt(masm_, "[ Return sequence");
409 if (return_label_.is_bound()) {
410 __ jmp(&return_label_);
411 } else {
412 __ bind(&return_label_);
413 if (FLAG_trace) {
414 __ Push(rax);
415 __ CallRuntime(Runtime::kTraceExit);
416 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100417 EmitProfilingCounterHandlingForReturnSequence(false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000418
419 SetReturnPosition(literal());
420 __ leave();
421
422 int arg_count = info_->scope()->num_parameters() + 1;
423 int arguments_bytes = arg_count * kPointerSize;
424 __ Ret(arguments_bytes, rcx);
425 }
426}
427
428
429void FullCodeGenerator::StackValueContext::Plug(Variable* var) const {
430 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
431 MemOperand operand = codegen()->VarOperand(var, result_register());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100432 codegen()->PushOperand(operand);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000433}
434
435
436void FullCodeGenerator::EffectContext::Plug(Heap::RootListIndex index) const {
437}
438
439
440void FullCodeGenerator::AccumulatorValueContext::Plug(
441 Heap::RootListIndex index) const {
442 __ LoadRoot(result_register(), index);
443}
444
445
446void FullCodeGenerator::StackValueContext::Plug(
447 Heap::RootListIndex index) const {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100448 codegen()->OperandStackDepthIncrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000449 __ PushRoot(index);
450}
451
452
453void FullCodeGenerator::TestContext::Plug(Heap::RootListIndex index) const {
454 codegen()->PrepareForBailoutBeforeSplit(condition(),
455 true,
456 true_label_,
457 false_label_);
458 if (index == Heap::kUndefinedValueRootIndex ||
459 index == Heap::kNullValueRootIndex ||
460 index == Heap::kFalseValueRootIndex) {
461 if (false_label_ != fall_through_) __ jmp(false_label_);
462 } else if (index == Heap::kTrueValueRootIndex) {
463 if (true_label_ != fall_through_) __ jmp(true_label_);
464 } else {
465 __ LoadRoot(result_register(), index);
466 codegen()->DoTest(this);
467 }
468}
469
470
471void FullCodeGenerator::EffectContext::Plug(Handle<Object> lit) const {
472}
473
474
475void FullCodeGenerator::AccumulatorValueContext::Plug(
476 Handle<Object> lit) const {
477 if (lit->IsSmi()) {
478 __ SafeMove(result_register(), Smi::cast(*lit));
479 } else {
480 __ Move(result_register(), lit);
481 }
482}
483
484
485void FullCodeGenerator::StackValueContext::Plug(Handle<Object> lit) const {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100486 codegen()->OperandStackDepthIncrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000487 if (lit->IsSmi()) {
488 __ SafePush(Smi::cast(*lit));
489 } else {
490 __ Push(lit);
491 }
492}
493
494
495void FullCodeGenerator::TestContext::Plug(Handle<Object> lit) const {
496 codegen()->PrepareForBailoutBeforeSplit(condition(),
497 true,
498 true_label_,
499 false_label_);
Ben Murdochda12d292016-06-02 14:46:10 +0100500 DCHECK(lit->IsNull() || lit->IsUndefined() || !lit->IsUndetectable());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000501 if (lit->IsUndefined() || lit->IsNull() || lit->IsFalse()) {
502 if (false_label_ != fall_through_) __ jmp(false_label_);
503 } else if (lit->IsTrue() || lit->IsJSObject()) {
504 if (true_label_ != fall_through_) __ jmp(true_label_);
505 } else if (lit->IsString()) {
506 if (String::cast(*lit)->length() == 0) {
507 if (false_label_ != fall_through_) __ jmp(false_label_);
508 } else {
509 if (true_label_ != fall_through_) __ jmp(true_label_);
510 }
511 } else if (lit->IsSmi()) {
512 if (Smi::cast(*lit)->value() == 0) {
513 if (false_label_ != fall_through_) __ jmp(false_label_);
514 } else {
515 if (true_label_ != fall_through_) __ jmp(true_label_);
516 }
517 } else {
518 // For simplicity we always test the accumulator register.
519 __ Move(result_register(), lit);
520 codegen()->DoTest(this);
521 }
522}
523
524
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000525void FullCodeGenerator::StackValueContext::DropAndPlug(int count,
526 Register reg) const {
527 DCHECK(count > 0);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100528 if (count > 1) codegen()->DropOperands(count - 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000529 __ movp(Operand(rsp, 0), reg);
530}
531
532
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000533void FullCodeGenerator::EffectContext::Plug(Label* materialize_true,
534 Label* materialize_false) const {
535 DCHECK(materialize_true == materialize_false);
536 __ bind(materialize_true);
537}
538
539
540void FullCodeGenerator::AccumulatorValueContext::Plug(
541 Label* materialize_true,
542 Label* materialize_false) const {
543 Label done;
544 __ bind(materialize_true);
545 __ Move(result_register(), isolate()->factory()->true_value());
546 __ jmp(&done, Label::kNear);
547 __ bind(materialize_false);
548 __ Move(result_register(), isolate()->factory()->false_value());
549 __ bind(&done);
550}
551
552
553void FullCodeGenerator::StackValueContext::Plug(
554 Label* materialize_true,
555 Label* materialize_false) const {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100556 codegen()->OperandStackDepthIncrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000557 Label done;
558 __ bind(materialize_true);
559 __ Push(isolate()->factory()->true_value());
560 __ jmp(&done, Label::kNear);
561 __ bind(materialize_false);
562 __ Push(isolate()->factory()->false_value());
563 __ bind(&done);
564}
565
566
567void FullCodeGenerator::TestContext::Plug(Label* materialize_true,
568 Label* materialize_false) const {
569 DCHECK(materialize_true == true_label_);
570 DCHECK(materialize_false == false_label_);
571}
572
573
574void FullCodeGenerator::AccumulatorValueContext::Plug(bool flag) const {
575 Heap::RootListIndex value_root_index =
576 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
577 __ LoadRoot(result_register(), value_root_index);
578}
579
580
581void FullCodeGenerator::StackValueContext::Plug(bool flag) const {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100582 codegen()->OperandStackDepthIncrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000583 Heap::RootListIndex value_root_index =
584 flag ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex;
585 __ PushRoot(value_root_index);
586}
587
588
589void FullCodeGenerator::TestContext::Plug(bool flag) const {
590 codegen()->PrepareForBailoutBeforeSplit(condition(),
591 true,
592 true_label_,
593 false_label_);
594 if (flag) {
595 if (true_label_ != fall_through_) __ jmp(true_label_);
596 } else {
597 if (false_label_ != fall_through_) __ jmp(false_label_);
598 }
599}
600
601
602void FullCodeGenerator::DoTest(Expression* condition,
603 Label* if_true,
604 Label* if_false,
605 Label* fall_through) {
Ben Murdochda12d292016-06-02 14:46:10 +0100606 Handle<Code> ic = ToBooleanICStub::GetUninitialized(isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000607 CallIC(ic, condition->test_id());
608 __ CompareRoot(result_register(), Heap::kTrueValueRootIndex);
609 Split(equal, if_true, if_false, fall_through);
610}
611
612
613void FullCodeGenerator::Split(Condition cc,
614 Label* if_true,
615 Label* if_false,
616 Label* fall_through) {
617 if (if_false == fall_through) {
618 __ j(cc, if_true);
619 } else if (if_true == fall_through) {
620 __ j(NegateCondition(cc), if_false);
621 } else {
622 __ j(cc, if_true);
623 __ jmp(if_false);
624 }
625}
626
627
628MemOperand FullCodeGenerator::StackOperand(Variable* var) {
629 DCHECK(var->IsStackAllocated());
630 // Offset is negative because higher indexes are at lower addresses.
631 int offset = -var->index() * kPointerSize;
632 // Adjust by a (parameter or local) base offset.
633 if (var->IsParameter()) {
634 offset += kFPOnStackSize + kPCOnStackSize +
635 (info_->scope()->num_parameters() - 1) * kPointerSize;
636 } else {
637 offset += JavaScriptFrameConstants::kLocal0Offset;
638 }
639 return Operand(rbp, offset);
640}
641
642
643MemOperand FullCodeGenerator::VarOperand(Variable* var, Register scratch) {
644 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
645 if (var->IsContextSlot()) {
646 int context_chain_length = scope()->ContextChainLength(var->scope());
647 __ LoadContext(scratch, context_chain_length);
648 return ContextOperand(scratch, var->index());
649 } else {
650 return StackOperand(var);
651 }
652}
653
654
655void FullCodeGenerator::GetVar(Register dest, Variable* var) {
656 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
657 MemOperand location = VarOperand(var, dest);
658 __ movp(dest, location);
659}
660
661
662void FullCodeGenerator::SetVar(Variable* var,
663 Register src,
664 Register scratch0,
665 Register scratch1) {
666 DCHECK(var->IsContextSlot() || var->IsStackAllocated());
667 DCHECK(!scratch0.is(src));
668 DCHECK(!scratch0.is(scratch1));
669 DCHECK(!scratch1.is(src));
670 MemOperand location = VarOperand(var, scratch0);
671 __ movp(location, src);
672
673 // Emit the write barrier code if the location is in the heap.
674 if (var->IsContextSlot()) {
675 int offset = Context::SlotOffset(var->index());
676 __ RecordWriteContextSlot(scratch0, offset, src, scratch1, kDontSaveFPRegs);
677 }
678}
679
680
681void FullCodeGenerator::PrepareForBailoutBeforeSplit(Expression* expr,
682 bool should_normalize,
683 Label* if_true,
684 Label* if_false) {
685 // Only prepare for bailouts before splits if we're in a test
686 // context. Otherwise, we let the Visit function deal with the
687 // preparation to avoid preparing with the same AST id twice.
688 if (!context()->IsTest()) return;
689
690 Label skip;
691 if (should_normalize) __ jmp(&skip, Label::kNear);
692 PrepareForBailout(expr, TOS_REG);
693 if (should_normalize) {
694 __ CompareRoot(rax, Heap::kTrueValueRootIndex);
695 Split(equal, if_true, if_false, NULL);
696 __ bind(&skip);
697 }
698}
699
700
701void FullCodeGenerator::EmitDebugCheckDeclarationContext(Variable* variable) {
702 // The variable in the declaration always resides in the current context.
703 DCHECK_EQ(0, scope()->ContextChainLength(variable->scope()));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100704 if (FLAG_debug_code) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000705 // Check that we're not inside a with or catch context.
706 __ movp(rbx, FieldOperand(rsi, HeapObject::kMapOffset));
707 __ CompareRoot(rbx, Heap::kWithContextMapRootIndex);
708 __ Check(not_equal, kDeclarationInWithContext);
709 __ CompareRoot(rbx, Heap::kCatchContextMapRootIndex);
710 __ Check(not_equal, kDeclarationInCatchContext);
711 }
712}
713
714
715void FullCodeGenerator::VisitVariableDeclaration(
716 VariableDeclaration* declaration) {
717 // If it was not possible to allocate the variable at compile time, we
718 // need to "declare" it at runtime to make sure it actually exists in the
719 // local context.
720 VariableProxy* proxy = declaration->proxy();
721 VariableMode mode = declaration->mode();
722 Variable* variable = proxy->var();
723 bool hole_init = mode == LET || mode == CONST || mode == CONST_LEGACY;
724 switch (variable->location()) {
725 case VariableLocation::GLOBAL:
726 case VariableLocation::UNALLOCATED:
727 globals_->Add(variable->name(), zone());
728 globals_->Add(variable->binding_needs_init()
729 ? isolate()->factory()->the_hole_value()
730 : isolate()->factory()->undefined_value(),
731 zone());
732 break;
733
734 case VariableLocation::PARAMETER:
735 case VariableLocation::LOCAL:
736 if (hole_init) {
737 Comment cmnt(masm_, "[ VariableDeclaration");
738 __ LoadRoot(kScratchRegister, Heap::kTheHoleValueRootIndex);
739 __ movp(StackOperand(variable), kScratchRegister);
740 }
741 break;
742
743 case VariableLocation::CONTEXT:
744 if (hole_init) {
745 Comment cmnt(masm_, "[ VariableDeclaration");
746 EmitDebugCheckDeclarationContext(variable);
747 __ LoadRoot(kScratchRegister, Heap::kTheHoleValueRootIndex);
748 __ movp(ContextOperand(rsi, variable->index()), kScratchRegister);
749 // No write barrier since the hole value is in old space.
750 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
751 }
752 break;
753
754 case VariableLocation::LOOKUP: {
755 Comment cmnt(masm_, "[ VariableDeclaration");
756 __ Push(variable->name());
757 // Declaration nodes are always introduced in one of four modes.
758 DCHECK(IsDeclaredVariableMode(mode));
759 // Push initial value, if any.
760 // Note: For variables we must not push an initial value (such as
761 // 'undefined') because we may have a (legal) redeclaration and we
762 // must not destroy the current value.
763 if (hole_init) {
764 __ PushRoot(Heap::kTheHoleValueRootIndex);
765 } else {
766 __ Push(Smi::FromInt(0)); // Indicates no initial value.
767 }
768 __ Push(Smi::FromInt(variable->DeclarationPropertyAttributes()));
769 __ CallRuntime(Runtime::kDeclareLookupSlot);
770 break;
771 }
772 }
773}
774
775
776void FullCodeGenerator::VisitFunctionDeclaration(
777 FunctionDeclaration* declaration) {
778 VariableProxy* proxy = declaration->proxy();
779 Variable* variable = proxy->var();
780 switch (variable->location()) {
781 case VariableLocation::GLOBAL:
782 case VariableLocation::UNALLOCATED: {
783 globals_->Add(variable->name(), zone());
784 Handle<SharedFunctionInfo> function =
785 Compiler::GetSharedFunctionInfo(declaration->fun(), script(), info_);
786 // Check for stack-overflow exception.
787 if (function.is_null()) return SetStackOverflow();
788 globals_->Add(function, zone());
789 break;
790 }
791
792 case VariableLocation::PARAMETER:
793 case VariableLocation::LOCAL: {
794 Comment cmnt(masm_, "[ FunctionDeclaration");
795 VisitForAccumulatorValue(declaration->fun());
796 __ movp(StackOperand(variable), result_register());
797 break;
798 }
799
800 case VariableLocation::CONTEXT: {
801 Comment cmnt(masm_, "[ FunctionDeclaration");
802 EmitDebugCheckDeclarationContext(variable);
803 VisitForAccumulatorValue(declaration->fun());
804 __ movp(ContextOperand(rsi, variable->index()), result_register());
805 int offset = Context::SlotOffset(variable->index());
806 // We know that we have written a function, which is not a smi.
807 __ RecordWriteContextSlot(rsi,
808 offset,
809 result_register(),
810 rcx,
811 kDontSaveFPRegs,
812 EMIT_REMEMBERED_SET,
813 OMIT_SMI_CHECK);
814 PrepareForBailoutForId(proxy->id(), NO_REGISTERS);
815 break;
816 }
817
818 case VariableLocation::LOOKUP: {
819 Comment cmnt(masm_, "[ FunctionDeclaration");
Ben Murdoch097c5b22016-05-18 11:27:45 +0100820 PushOperand(variable->name());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000821 VisitForStackValue(declaration->fun());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100822 PushOperand(Smi::FromInt(variable->DeclarationPropertyAttributes()));
823 CallRuntimeWithOperands(Runtime::kDeclareLookupSlot);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000824 break;
825 }
826 }
827}
828
829
830void FullCodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
831 // Call the runtime to declare the globals.
832 __ Push(pairs);
833 __ Push(Smi::FromInt(DeclareGlobalsFlags()));
834 __ CallRuntime(Runtime::kDeclareGlobals);
835 // Return value is ignored.
836}
837
838
839void FullCodeGenerator::DeclareModules(Handle<FixedArray> descriptions) {
840 // Call the runtime to declare the modules.
841 __ Push(descriptions);
842 __ CallRuntime(Runtime::kDeclareModules);
843 // Return value is ignored.
844}
845
846
847void FullCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
848 Comment cmnt(masm_, "[ SwitchStatement");
849 Breakable nested_statement(this, stmt);
850 SetStatementPosition(stmt);
851
852 // Keep the switch value on the stack until a case matches.
853 VisitForStackValue(stmt->tag());
854 PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS);
855
856 ZoneList<CaseClause*>* clauses = stmt->cases();
857 CaseClause* default_clause = NULL; // Can occur anywhere in the list.
858
859 Label next_test; // Recycled for each test.
860 // Compile all the tests with branches to their bodies.
861 for (int i = 0; i < clauses->length(); i++) {
862 CaseClause* clause = clauses->at(i);
863 clause->body_target()->Unuse();
864
865 // The default is not a test, but remember it as final fall through.
866 if (clause->is_default()) {
867 default_clause = clause;
868 continue;
869 }
870
871 Comment cmnt(masm_, "[ Case comparison");
872 __ bind(&next_test);
873 next_test.Unuse();
874
875 // Compile the label expression.
876 VisitForAccumulatorValue(clause->label());
877
878 // Perform the comparison as if via '==='.
879 __ movp(rdx, Operand(rsp, 0)); // Switch value.
880 bool inline_smi_code = ShouldInlineSmiCase(Token::EQ_STRICT);
881 JumpPatchSite patch_site(masm_);
882 if (inline_smi_code) {
883 Label slow_case;
884 __ movp(rcx, rdx);
885 __ orp(rcx, rax);
886 patch_site.EmitJumpIfNotSmi(rcx, &slow_case, Label::kNear);
887
888 __ cmpp(rdx, rax);
889 __ j(not_equal, &next_test);
890 __ Drop(1); // Switch value is no longer needed.
891 __ jmp(clause->body_target());
892 __ bind(&slow_case);
893 }
894
895 // Record position before stub call for type feedback.
896 SetExpressionPosition(clause);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100897 Handle<Code> ic =
898 CodeFactory::CompareIC(isolate(), Token::EQ_STRICT).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000899 CallIC(ic, clause->CompareId());
900 patch_site.EmitPatchInfo();
901
902 Label skip;
903 __ jmp(&skip, Label::kNear);
904 PrepareForBailout(clause, TOS_REG);
905 __ CompareRoot(rax, Heap::kTrueValueRootIndex);
906 __ j(not_equal, &next_test);
907 __ Drop(1);
908 __ jmp(clause->body_target());
909 __ bind(&skip);
910
911 __ testp(rax, rax);
912 __ j(not_equal, &next_test);
913 __ Drop(1); // Switch value is no longer needed.
914 __ jmp(clause->body_target());
915 }
916
917 // Discard the test value and jump to the default if present, otherwise to
918 // the end of the statement.
919 __ bind(&next_test);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100920 DropOperands(1); // Switch value is no longer needed.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000921 if (default_clause == NULL) {
922 __ jmp(nested_statement.break_label());
923 } else {
924 __ jmp(default_clause->body_target());
925 }
926
927 // Compile all the case bodies.
928 for (int i = 0; i < clauses->length(); i++) {
929 Comment cmnt(masm_, "[ Case body");
930 CaseClause* clause = clauses->at(i);
931 __ bind(clause->body_target());
932 PrepareForBailoutForId(clause->EntryId(), NO_REGISTERS);
933 VisitStatements(clause->statements());
934 }
935
936 __ bind(nested_statement.break_label());
937 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
938}
939
940
941void FullCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
942 Comment cmnt(masm_, "[ ForInStatement");
943 SetStatementPosition(stmt, SKIP_BREAK);
944
945 FeedbackVectorSlot slot = stmt->ForInFeedbackSlot();
946
Ben Murdoch097c5b22016-05-18 11:27:45 +0100947 // Get the object to enumerate over.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000948 SetExpressionAsStatementPosition(stmt->enumerable());
949 VisitForAccumulatorValue(stmt->enumerable());
Ben Murdochda12d292016-06-02 14:46:10 +0100950 OperandStackDepthIncrement(5);
951
952 Label loop, exit;
953 Iteration loop_statement(this, stmt);
954 increment_loop_depth();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000955
Ben Murdoch097c5b22016-05-18 11:27:45 +0100956 // If the object is null or undefined, skip over the loop, otherwise convert
957 // it to a JS receiver. See ECMA-262 version 5, section 12.6.4.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000958 Label convert, done_convert;
959 __ JumpIfSmi(rax, &convert, Label::kNear);
960 __ CmpObjectType(rax, FIRST_JS_RECEIVER_TYPE, rcx);
961 __ j(above_equal, &done_convert, Label::kNear);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100962 __ CompareRoot(rax, Heap::kNullValueRootIndex);
963 __ j(equal, &exit);
964 __ CompareRoot(rax, Heap::kUndefinedValueRootIndex);
965 __ j(equal, &exit);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000966 __ bind(&convert);
967 ToObjectStub stub(isolate());
968 __ CallStub(&stub);
969 __ bind(&done_convert);
970 PrepareForBailoutForId(stmt->ToObjectId(), TOS_REG);
971 __ Push(rax);
972
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000973 // Check cache validity in generated code. This is a fast case for
974 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
975 // guarantee cache validity, call the runtime system to check cache
976 // validity or get the property names in a fixed array.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100977 // Note: Proxies never have an enum cache, so will always take the
978 // slow path.
979 Label call_runtime;
980 __ CheckEnumCache(&call_runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000981
982 // The enum cache is valid. Load the map of the object being
983 // iterated over and use the cache for the iteration.
984 Label use_cache;
985 __ movp(rax, FieldOperand(rax, HeapObject::kMapOffset));
986 __ jmp(&use_cache, Label::kNear);
987
988 // Get the set of properties to enumerate.
989 __ bind(&call_runtime);
990 __ Push(rax); // Duplicate the enumerable object on the stack.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100991 __ CallRuntime(Runtime::kForInEnumerate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000992 PrepareForBailoutForId(stmt->EnumId(), TOS_REG);
993
994 // If we got a map from the runtime call, we can do a fast
995 // modification check. Otherwise, we got a fixed array, and we have
996 // to do a slow check.
997 Label fixed_array;
998 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
999 Heap::kMetaMapRootIndex);
1000 __ j(not_equal, &fixed_array);
1001
1002 // We got a map in register rax. Get the enumeration cache from it.
1003 __ bind(&use_cache);
1004
1005 Label no_descriptors;
1006
1007 __ EnumLength(rdx, rax);
1008 __ Cmp(rdx, Smi::FromInt(0));
1009 __ j(equal, &no_descriptors);
1010
1011 __ LoadInstanceDescriptors(rax, rcx);
1012 __ movp(rcx, FieldOperand(rcx, DescriptorArray::kEnumCacheOffset));
1013 __ movp(rcx, FieldOperand(rcx, DescriptorArray::kEnumCacheBridgeCacheOffset));
1014
1015 // Set up the four remaining stack slots.
1016 __ Push(rax); // Map.
1017 __ Push(rcx); // Enumeration cache.
1018 __ Push(rdx); // Number of valid entries for the map in the enum cache.
1019 __ Push(Smi::FromInt(0)); // Initial index.
1020 __ jmp(&loop);
1021
1022 __ bind(&no_descriptors);
1023 __ addp(rsp, Immediate(kPointerSize));
1024 __ jmp(&exit);
1025
1026 // We got a fixed array in register rax. Iterate through that.
1027 __ bind(&fixed_array);
1028
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001029 __ movp(rcx, Operand(rsp, 0 * kPointerSize)); // Get enumerated object
1030 __ Push(Smi::FromInt(1)); // Smi(1) indicates slow check
1031 __ Push(rax); // Array
1032 __ movp(rax, FieldOperand(rax, FixedArray::kLengthOffset));
1033 __ Push(rax); // Fixed array length (as smi).
Ben Murdoch097c5b22016-05-18 11:27:45 +01001034 PrepareForBailoutForId(stmt->PrepareId(), NO_REGISTERS);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001035 __ Push(Smi::FromInt(0)); // Initial index.
1036
1037 // Generate code for doing the condition check.
1038 __ bind(&loop);
1039 SetExpressionAsStatementPosition(stmt->each());
1040
1041 __ movp(rax, Operand(rsp, 0 * kPointerSize)); // Get the current index.
1042 __ cmpp(rax, Operand(rsp, 1 * kPointerSize)); // Compare to the array length.
1043 __ j(above_equal, loop_statement.break_label());
1044
1045 // Get the current entry of the array into register rbx.
1046 __ movp(rbx, Operand(rsp, 2 * kPointerSize));
1047 SmiIndex index = masm()->SmiToIndex(rax, rax, kPointerSizeLog2);
1048 __ movp(rbx, FieldOperand(rbx,
1049 index.reg,
1050 index.scale,
1051 FixedArray::kHeaderSize));
1052
1053 // Get the expected map from the stack or a smi in the
1054 // permanent slow case into register rdx.
1055 __ movp(rdx, Operand(rsp, 3 * kPointerSize));
1056
1057 // Check if the expected map still matches that of the enumerable.
1058 // If not, we may have to filter the key.
1059 Label update_each;
1060 __ movp(rcx, Operand(rsp, 4 * kPointerSize));
1061 __ cmpp(rdx, FieldOperand(rcx, HeapObject::kMapOffset));
1062 __ j(equal, &update_each, Label::kNear);
1063
Ben Murdochda12d292016-06-02 14:46:10 +01001064 // We need to filter the key, record slow-path here.
1065 int const vector_index = SmiFromSlot(slot)->value();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001066 __ EmitLoadTypeFeedbackVector(rdx);
1067 __ Move(FieldOperand(rdx, FixedArray::OffsetOfElementAt(vector_index)),
1068 TypeFeedbackVector::MegamorphicSentinel(isolate()));
1069
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001070 // Convert the entry to a string or null if it isn't a property
1071 // anymore. If the property has been removed while iterating, we
1072 // just skip it.
1073 __ Push(rcx); // Enumerable.
1074 __ Push(rbx); // Current entry.
1075 __ CallRuntime(Runtime::kForInFilter);
1076 PrepareForBailoutForId(stmt->FilterId(), TOS_REG);
1077 __ CompareRoot(rax, Heap::kUndefinedValueRootIndex);
1078 __ j(equal, loop_statement.continue_label());
1079 __ movp(rbx, rax);
1080
1081 // Update the 'each' property or variable from the possibly filtered
1082 // entry in register rbx.
1083 __ bind(&update_each);
1084 __ movp(result_register(), rbx);
1085 // Perform the assignment as if via '='.
1086 { EffectContext context(this);
1087 EmitAssignment(stmt->each(), stmt->EachFeedbackSlot());
1088 PrepareForBailoutForId(stmt->AssignmentId(), NO_REGISTERS);
1089 }
1090
1091 // Both Crankshaft and Turbofan expect BodyId to be right before stmt->body().
1092 PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
1093 // Generate code for the body of the loop.
1094 Visit(stmt->body());
1095
1096 // Generate code for going to the next element by incrementing the
1097 // index (smi) stored on top of the stack.
1098 __ bind(loop_statement.continue_label());
1099 __ SmiAddConstant(Operand(rsp, 0 * kPointerSize), Smi::FromInt(1));
1100
1101 EmitBackEdgeBookkeeping(stmt, &loop);
1102 __ jmp(&loop);
1103
1104 // Remove the pointers stored on the stack.
1105 __ bind(loop_statement.break_label());
Ben Murdochda12d292016-06-02 14:46:10 +01001106 DropOperands(5);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001107
1108 // Exit and decrement the loop depth.
1109 PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1110 __ bind(&exit);
1111 decrement_loop_depth();
1112}
1113
1114
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001115void FullCodeGenerator::EmitSetHomeObject(Expression* initializer, int offset,
1116 FeedbackVectorSlot slot) {
1117 DCHECK(NeedsHomeObject(initializer));
1118 __ movp(StoreDescriptor::ReceiverRegister(), Operand(rsp, 0));
1119 __ Move(StoreDescriptor::NameRegister(),
1120 isolate()->factory()->home_object_symbol());
1121 __ movp(StoreDescriptor::ValueRegister(),
1122 Operand(rsp, offset * kPointerSize));
1123 EmitLoadStoreICSlot(slot);
1124 CallStoreIC();
1125}
1126
1127
1128void FullCodeGenerator::EmitSetHomeObjectAccumulator(Expression* initializer,
1129 int offset,
1130 FeedbackVectorSlot slot) {
1131 DCHECK(NeedsHomeObject(initializer));
1132 __ movp(StoreDescriptor::ReceiverRegister(), rax);
1133 __ Move(StoreDescriptor::NameRegister(),
1134 isolate()->factory()->home_object_symbol());
1135 __ movp(StoreDescriptor::ValueRegister(),
1136 Operand(rsp, offset * kPointerSize));
1137 EmitLoadStoreICSlot(slot);
1138 CallStoreIC();
1139}
1140
1141
1142void FullCodeGenerator::EmitLoadGlobalCheckExtensions(VariableProxy* proxy,
1143 TypeofMode typeof_mode,
1144 Label* slow) {
1145 Register context = rsi;
1146 Register temp = rdx;
1147
1148 Scope* s = scope();
1149 while (s != NULL) {
1150 if (s->num_heap_slots() > 0) {
1151 if (s->calls_sloppy_eval()) {
1152 // Check that extension is "the hole".
1153 __ JumpIfNotRoot(ContextOperand(context, Context::EXTENSION_INDEX),
1154 Heap::kTheHoleValueRootIndex, slow);
1155 }
1156 // Load next context in chain.
1157 __ movp(temp, ContextOperand(context, Context::PREVIOUS_INDEX));
1158 // Walk the rest of the chain without clobbering rsi.
1159 context = temp;
1160 }
1161 // If no outer scope calls eval, we do not need to check more
1162 // context extensions. If we have reached an eval scope, we check
1163 // all extensions from this point.
1164 if (!s->outer_scope_calls_sloppy_eval() || s->is_eval_scope()) break;
1165 s = s->outer_scope();
1166 }
1167
1168 if (s != NULL && s->is_eval_scope()) {
1169 // Loop up the context chain. There is no frame effect so it is
1170 // safe to use raw labels here.
1171 Label next, fast;
1172 if (!context.is(temp)) {
1173 __ movp(temp, context);
1174 }
1175 // Load map for comparison into register, outside loop.
1176 __ LoadRoot(kScratchRegister, Heap::kNativeContextMapRootIndex);
1177 __ bind(&next);
1178 // Terminate at native context.
1179 __ cmpp(kScratchRegister, FieldOperand(temp, HeapObject::kMapOffset));
1180 __ j(equal, &fast, Label::kNear);
1181 // Check that extension is "the hole".
1182 __ JumpIfNotRoot(ContextOperand(temp, Context::EXTENSION_INDEX),
1183 Heap::kTheHoleValueRootIndex, slow);
1184 // Load next context in chain.
1185 __ movp(temp, ContextOperand(temp, Context::PREVIOUS_INDEX));
1186 __ jmp(&next);
1187 __ bind(&fast);
1188 }
1189
1190 // All extension objects were empty and it is safe to use a normal global
1191 // load machinery.
1192 EmitGlobalVariableLoad(proxy, typeof_mode);
1193}
1194
1195
1196MemOperand FullCodeGenerator::ContextSlotOperandCheckExtensions(Variable* var,
1197 Label* slow) {
1198 DCHECK(var->IsContextSlot());
1199 Register context = rsi;
1200 Register temp = rbx;
1201
1202 for (Scope* s = scope(); s != var->scope(); s = s->outer_scope()) {
1203 if (s->num_heap_slots() > 0) {
1204 if (s->calls_sloppy_eval()) {
1205 // Check that extension is "the hole".
1206 __ JumpIfNotRoot(ContextOperand(context, Context::EXTENSION_INDEX),
1207 Heap::kTheHoleValueRootIndex, slow);
1208 }
1209 __ movp(temp, ContextOperand(context, Context::PREVIOUS_INDEX));
1210 // Walk the rest of the chain without clobbering rsi.
1211 context = temp;
1212 }
1213 }
1214 // Check that last extension is "the hole".
1215 __ JumpIfNotRoot(ContextOperand(context, Context::EXTENSION_INDEX),
1216 Heap::kTheHoleValueRootIndex, slow);
1217
1218 // This function is used only for loads, not stores, so it's safe to
1219 // return an rsi-based operand (the write barrier cannot be allowed to
1220 // destroy the rsi register).
1221 return ContextOperand(context, var->index());
1222}
1223
1224
1225void FullCodeGenerator::EmitDynamicLookupFastCase(VariableProxy* proxy,
1226 TypeofMode typeof_mode,
1227 Label* slow, Label* done) {
1228 // Generate fast-case code for variables that might be shadowed by
1229 // eval-introduced variables. Eval is used a lot without
1230 // introducing variables. In those cases, we do not want to
1231 // perform a runtime call for all variables in the scope
1232 // containing the eval.
1233 Variable* var = proxy->var();
1234 if (var->mode() == DYNAMIC_GLOBAL) {
1235 EmitLoadGlobalCheckExtensions(proxy, typeof_mode, slow);
1236 __ jmp(done);
1237 } else if (var->mode() == DYNAMIC_LOCAL) {
1238 Variable* local = var->local_if_not_shadowed();
1239 __ movp(rax, ContextSlotOperandCheckExtensions(local, slow));
1240 if (local->mode() == LET || local->mode() == CONST ||
1241 local->mode() == CONST_LEGACY) {
1242 __ CompareRoot(rax, Heap::kTheHoleValueRootIndex);
1243 __ j(not_equal, done);
1244 if (local->mode() == CONST_LEGACY) {
1245 __ LoadRoot(rax, Heap::kUndefinedValueRootIndex);
1246 } else { // LET || CONST
1247 __ Push(var->name());
1248 __ CallRuntime(Runtime::kThrowReferenceError);
1249 }
1250 }
1251 __ jmp(done);
1252 }
1253}
1254
1255
1256void FullCodeGenerator::EmitGlobalVariableLoad(VariableProxy* proxy,
1257 TypeofMode typeof_mode) {
1258 Variable* var = proxy->var();
1259 DCHECK(var->IsUnallocatedOrGlobalSlot() ||
1260 (var->IsLookupSlot() && var->mode() == DYNAMIC_GLOBAL));
1261 __ Move(LoadDescriptor::NameRegister(), var->name());
1262 __ LoadGlobalObject(LoadDescriptor::ReceiverRegister());
1263 __ Move(LoadDescriptor::SlotRegister(),
1264 SmiFromSlot(proxy->VariableFeedbackSlot()));
1265 CallLoadIC(typeof_mode);
1266}
1267
1268
1269void FullCodeGenerator::EmitVariableLoad(VariableProxy* proxy,
1270 TypeofMode typeof_mode) {
1271 // Record position before possible IC call.
1272 SetExpressionPosition(proxy);
1273 PrepareForBailoutForId(proxy->BeforeId(), NO_REGISTERS);
1274 Variable* var = proxy->var();
1275
1276 // Three cases: global variables, lookup variables, and all other types of
1277 // variables.
1278 switch (var->location()) {
1279 case VariableLocation::GLOBAL:
1280 case VariableLocation::UNALLOCATED: {
1281 Comment cmnt(masm_, "[ Global variable");
1282 EmitGlobalVariableLoad(proxy, typeof_mode);
1283 context()->Plug(rax);
1284 break;
1285 }
1286
1287 case VariableLocation::PARAMETER:
1288 case VariableLocation::LOCAL:
1289 case VariableLocation::CONTEXT: {
1290 DCHECK_EQ(NOT_INSIDE_TYPEOF, typeof_mode);
1291 Comment cmnt(masm_, var->IsContextSlot() ? "[ Context slot"
1292 : "[ Stack slot");
1293 if (NeedsHoleCheckForLoad(proxy)) {
1294 // Let and const need a read barrier.
1295 Label done;
1296 GetVar(rax, var);
1297 __ CompareRoot(rax, Heap::kTheHoleValueRootIndex);
1298 __ j(not_equal, &done, Label::kNear);
1299 if (var->mode() == LET || var->mode() == CONST) {
1300 // Throw a reference error when using an uninitialized let/const
1301 // binding in harmony mode.
1302 __ Push(var->name());
1303 __ CallRuntime(Runtime::kThrowReferenceError);
1304 } else {
1305 // Uninitialized legacy const bindings are unholed.
1306 DCHECK(var->mode() == CONST_LEGACY);
1307 __ LoadRoot(rax, Heap::kUndefinedValueRootIndex);
1308 }
1309 __ bind(&done);
1310 context()->Plug(rax);
1311 break;
1312 }
1313 context()->Plug(var);
1314 break;
1315 }
1316
1317 case VariableLocation::LOOKUP: {
1318 Comment cmnt(masm_, "[ Lookup slot");
1319 Label done, slow;
1320 // Generate code for loading from variables potentially shadowed
1321 // by eval-introduced variables.
1322 EmitDynamicLookupFastCase(proxy, typeof_mode, &slow, &done);
1323 __ bind(&slow);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001324 __ Push(var->name());
1325 Runtime::FunctionId function_id =
1326 typeof_mode == NOT_INSIDE_TYPEOF
1327 ? Runtime::kLoadLookupSlot
Ben Murdoch097c5b22016-05-18 11:27:45 +01001328 : Runtime::kLoadLookupSlotInsideTypeof;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001329 __ CallRuntime(function_id);
1330 __ bind(&done);
1331 context()->Plug(rax);
1332 break;
1333 }
1334 }
1335}
1336
1337
1338void FullCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
1339 Comment cmnt(masm_, "[ RegExpLiteral");
1340 __ movp(rdi, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
1341 __ Move(rax, Smi::FromInt(expr->literal_index()));
1342 __ Move(rcx, expr->pattern());
1343 __ Move(rdx, Smi::FromInt(expr->flags()));
1344 FastCloneRegExpStub stub(isolate());
1345 __ CallStub(&stub);
1346 context()->Plug(rax);
1347}
1348
1349
1350void FullCodeGenerator::EmitAccessor(ObjectLiteralProperty* property) {
1351 Expression* expression = (property == NULL) ? NULL : property->value();
1352 if (expression == NULL) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001353 OperandStackDepthIncrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001354 __ PushRoot(Heap::kNullValueRootIndex);
1355 } else {
1356 VisitForStackValue(expression);
1357 if (NeedsHomeObject(expression)) {
1358 DCHECK(property->kind() == ObjectLiteral::Property::GETTER ||
1359 property->kind() == ObjectLiteral::Property::SETTER);
1360 int offset = property->kind() == ObjectLiteral::Property::GETTER ? 2 : 3;
1361 EmitSetHomeObject(expression, offset, property->GetSlot());
1362 }
1363 }
1364}
1365
1366
1367void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
1368 Comment cmnt(masm_, "[ ObjectLiteral");
1369
1370 Handle<FixedArray> constant_properties = expr->constant_properties();
1371 int flags = expr->ComputeFlags();
1372 if (MustCreateObjectLiteralWithRuntime(expr)) {
1373 __ Push(Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
1374 __ Push(Smi::FromInt(expr->literal_index()));
1375 __ Push(constant_properties);
1376 __ Push(Smi::FromInt(flags));
1377 __ CallRuntime(Runtime::kCreateObjectLiteral);
1378 } else {
1379 __ movp(rax, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
1380 __ Move(rbx, Smi::FromInt(expr->literal_index()));
1381 __ Move(rcx, constant_properties);
1382 __ Move(rdx, Smi::FromInt(flags));
1383 FastCloneShallowObjectStub stub(isolate(), expr->properties_count());
1384 __ CallStub(&stub);
1385 }
1386 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1387
1388 // If result_saved is true the result is on top of the stack. If
1389 // result_saved is false the result is in rax.
1390 bool result_saved = false;
1391
1392 AccessorTable accessor_table(zone());
1393 int property_index = 0;
1394 for (; property_index < expr->properties()->length(); property_index++) {
1395 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1396 if (property->is_computed_name()) break;
1397 if (property->IsCompileTimeValue()) continue;
1398
1399 Literal* key = property->key()->AsLiteral();
1400 Expression* value = property->value();
1401 if (!result_saved) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001402 PushOperand(rax); // Save result on the stack
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001403 result_saved = true;
1404 }
1405 switch (property->kind()) {
1406 case ObjectLiteral::Property::CONSTANT:
1407 UNREACHABLE();
1408 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1409 DCHECK(!CompileTimeValue::IsCompileTimeValue(value));
1410 // Fall through.
1411 case ObjectLiteral::Property::COMPUTED:
1412 // It is safe to use [[Put]] here because the boilerplate already
1413 // contains computed properties with an uninitialized value.
1414 if (key->value()->IsInternalizedString()) {
1415 if (property->emit_store()) {
1416 VisitForAccumulatorValue(value);
1417 DCHECK(StoreDescriptor::ValueRegister().is(rax));
1418 __ Move(StoreDescriptor::NameRegister(), key->value());
1419 __ movp(StoreDescriptor::ReceiverRegister(), Operand(rsp, 0));
1420 EmitLoadStoreICSlot(property->GetSlot(0));
1421 CallStoreIC();
1422 PrepareForBailoutForId(key->id(), NO_REGISTERS);
1423
1424 if (NeedsHomeObject(value)) {
1425 EmitSetHomeObjectAccumulator(value, 0, property->GetSlot(1));
1426 }
1427 } else {
1428 VisitForEffect(value);
1429 }
1430 break;
1431 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001432 PushOperand(Operand(rsp, 0)); // Duplicate receiver.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001433 VisitForStackValue(key);
1434 VisitForStackValue(value);
1435 if (property->emit_store()) {
1436 if (NeedsHomeObject(value)) {
1437 EmitSetHomeObject(value, 2, property->GetSlot());
1438 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001439 PushOperand(Smi::FromInt(SLOPPY)); // Language mode
1440 CallRuntimeWithOperands(Runtime::kSetProperty);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001441 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001442 DropOperands(3);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001443 }
1444 break;
1445 case ObjectLiteral::Property::PROTOTYPE:
Ben Murdoch097c5b22016-05-18 11:27:45 +01001446 PushOperand(Operand(rsp, 0)); // Duplicate receiver.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001447 VisitForStackValue(value);
1448 DCHECK(property->emit_store());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001449 CallRuntimeWithOperands(Runtime::kInternalSetPrototype);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001450 PrepareForBailoutForId(expr->GetIdForPropertySet(property_index),
1451 NO_REGISTERS);
1452 break;
1453 case ObjectLiteral::Property::GETTER:
1454 if (property->emit_store()) {
1455 accessor_table.lookup(key)->second->getter = property;
1456 }
1457 break;
1458 case ObjectLiteral::Property::SETTER:
1459 if (property->emit_store()) {
1460 accessor_table.lookup(key)->second->setter = property;
1461 }
1462 break;
1463 }
1464 }
1465
1466 // Emit code to define accessors, using only a single call to the runtime for
1467 // each pair of corresponding getters and setters.
1468 for (AccessorTable::Iterator it = accessor_table.begin();
1469 it != accessor_table.end();
1470 ++it) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001471 PushOperand(Operand(rsp, 0)); // Duplicate receiver.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001472 VisitForStackValue(it->first);
1473 EmitAccessor(it->second->getter);
1474 EmitAccessor(it->second->setter);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001475 PushOperand(Smi::FromInt(NONE));
1476 CallRuntimeWithOperands(Runtime::kDefineAccessorPropertyUnchecked);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001477 }
1478
1479 // Object literals have two parts. The "static" part on the left contains no
1480 // computed property names, and so we can compute its map ahead of time; see
1481 // runtime.cc::CreateObjectLiteralBoilerplate. The second "dynamic" part
1482 // starts with the first computed property name, and continues with all
1483 // properties to its right. All the code from above initializes the static
1484 // component of the object literal, and arranges for the map of the result to
1485 // reflect the static order in which the keys appear. For the dynamic
1486 // properties, we compile them into a series of "SetOwnProperty" runtime
1487 // calls. This will preserve insertion order.
1488 for (; property_index < expr->properties()->length(); property_index++) {
1489 ObjectLiteral::Property* property = expr->properties()->at(property_index);
1490
1491 Expression* value = property->value();
1492 if (!result_saved) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001493 PushOperand(rax); // Save result on the stack
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001494 result_saved = true;
1495 }
1496
Ben Murdoch097c5b22016-05-18 11:27:45 +01001497 PushOperand(Operand(rsp, 0)); // Duplicate receiver.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001498
1499 if (property->kind() == ObjectLiteral::Property::PROTOTYPE) {
1500 DCHECK(!property->is_computed_name());
1501 VisitForStackValue(value);
1502 DCHECK(property->emit_store());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001503 CallRuntimeWithOperands(Runtime::kInternalSetPrototype);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001504 PrepareForBailoutForId(expr->GetIdForPropertySet(property_index),
1505 NO_REGISTERS);
1506 } else {
1507 EmitPropertyKey(property, expr->GetIdForPropertyName(property_index));
1508 VisitForStackValue(value);
1509 if (NeedsHomeObject(value)) {
1510 EmitSetHomeObject(value, 2, property->GetSlot());
1511 }
1512
1513 switch (property->kind()) {
1514 case ObjectLiteral::Property::CONSTANT:
1515 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
1516 case ObjectLiteral::Property::COMPUTED:
1517 if (property->emit_store()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001518 PushOperand(Smi::FromInt(NONE));
1519 PushOperand(Smi::FromInt(property->NeedsSetFunctionName()));
1520 CallRuntimeWithOperands(Runtime::kDefineDataPropertyInLiteral);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001521 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001522 DropOperands(3);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001523 }
1524 break;
1525
1526 case ObjectLiteral::Property::PROTOTYPE:
1527 UNREACHABLE();
1528 break;
1529
1530 case ObjectLiteral::Property::GETTER:
Ben Murdoch097c5b22016-05-18 11:27:45 +01001531 PushOperand(Smi::FromInt(NONE));
1532 CallRuntimeWithOperands(Runtime::kDefineGetterPropertyUnchecked);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001533 break;
1534
1535 case ObjectLiteral::Property::SETTER:
Ben Murdoch097c5b22016-05-18 11:27:45 +01001536 PushOperand(Smi::FromInt(NONE));
1537 CallRuntimeWithOperands(Runtime::kDefineSetterPropertyUnchecked);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001538 break;
1539 }
1540 }
1541 }
1542
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001543 if (result_saved) {
1544 context()->PlugTOS();
1545 } else {
1546 context()->Plug(rax);
1547 }
1548}
1549
1550
1551void FullCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
1552 Comment cmnt(masm_, "[ ArrayLiteral");
1553
1554 Handle<FixedArray> constant_elements = expr->constant_elements();
1555 bool has_constant_fast_elements =
1556 IsFastObjectElementsKind(expr->constant_elements_kind());
1557
1558 AllocationSiteMode allocation_site_mode = TRACK_ALLOCATION_SITE;
1559 if (has_constant_fast_elements && !FLAG_allocation_site_pretenuring) {
1560 // If the only customer of allocation sites is transitioning, then
1561 // we can turn it off if we don't have anywhere else to transition to.
1562 allocation_site_mode = DONT_TRACK_ALLOCATION_SITE;
1563 }
1564
1565 if (MustCreateArrayLiteralWithRuntime(expr)) {
1566 __ Push(Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
1567 __ Push(Smi::FromInt(expr->literal_index()));
1568 __ Push(constant_elements);
1569 __ Push(Smi::FromInt(expr->ComputeFlags()));
1570 __ CallRuntime(Runtime::kCreateArrayLiteral);
1571 } else {
1572 __ movp(rax, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
1573 __ Move(rbx, Smi::FromInt(expr->literal_index()));
1574 __ Move(rcx, constant_elements);
1575 FastCloneShallowArrayStub stub(isolate(), allocation_site_mode);
1576 __ CallStub(&stub);
1577 }
1578 PrepareForBailoutForId(expr->CreateLiteralId(), TOS_REG);
1579
1580 bool result_saved = false; // Is the result saved to the stack?
1581 ZoneList<Expression*>* subexprs = expr->values();
1582 int length = subexprs->length();
1583
1584 // Emit code to evaluate all the non-constant subexpressions and to store
1585 // them into the newly cloned array.
1586 int array_index = 0;
1587 for (; array_index < length; array_index++) {
1588 Expression* subexpr = subexprs->at(array_index);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001589 DCHECK(!subexpr->IsSpread());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001590
1591 // If the subexpression is a literal or a simple materialized literal it
1592 // is already set in the cloned array.
1593 if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
1594
1595 if (!result_saved) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001596 PushOperand(rax); // array literal
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001597 result_saved = true;
1598 }
1599 VisitForAccumulatorValue(subexpr);
1600
1601 __ Move(StoreDescriptor::NameRegister(), Smi::FromInt(array_index));
1602 __ movp(StoreDescriptor::ReceiverRegister(), Operand(rsp, 0));
1603 EmitLoadStoreICSlot(expr->LiteralFeedbackSlot());
1604 Handle<Code> ic =
1605 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
1606 CallIC(ic);
1607
1608 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1609 }
1610
1611 // In case the array literal contains spread expressions it has two parts. The
1612 // first part is the "static" array which has a literal index is handled
1613 // above. The second part is the part after the first spread expression
1614 // (inclusive) and these elements gets appended to the array. Note that the
1615 // number elements an iterable produces is unknown ahead of time.
1616 if (array_index < length && result_saved) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001617 PopOperand(rax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001618 result_saved = false;
1619 }
1620 for (; array_index < length; array_index++) {
1621 Expression* subexpr = subexprs->at(array_index);
1622
Ben Murdoch097c5b22016-05-18 11:27:45 +01001623 PushOperand(rax);
1624 DCHECK(!subexpr->IsSpread());
1625 VisitForStackValue(subexpr);
1626 CallRuntimeWithOperands(Runtime::kAppendElement);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001627
1628 PrepareForBailoutForId(expr->GetIdForElement(array_index), NO_REGISTERS);
1629 }
1630
1631 if (result_saved) {
1632 context()->PlugTOS();
1633 } else {
1634 context()->Plug(rax);
1635 }
1636}
1637
1638
1639void FullCodeGenerator::VisitAssignment(Assignment* expr) {
1640 DCHECK(expr->target()->IsValidReferenceExpressionOrThis());
1641
1642 Comment cmnt(masm_, "[ Assignment");
1643 SetExpressionPosition(expr, INSERT_BREAK);
1644
1645 Property* property = expr->target()->AsProperty();
1646 LhsKind assign_type = Property::GetAssignType(property);
1647
1648 // Evaluate LHS expression.
1649 switch (assign_type) {
1650 case VARIABLE:
1651 // Nothing to do here.
1652 break;
1653 case NAMED_PROPERTY:
1654 if (expr->is_compound()) {
1655 // We need the receiver both on the stack and in the register.
1656 VisitForStackValue(property->obj());
1657 __ movp(LoadDescriptor::ReceiverRegister(), Operand(rsp, 0));
1658 } else {
1659 VisitForStackValue(property->obj());
1660 }
1661 break;
1662 case NAMED_SUPER_PROPERTY:
1663 VisitForStackValue(
1664 property->obj()->AsSuperPropertyReference()->this_var());
1665 VisitForAccumulatorValue(
1666 property->obj()->AsSuperPropertyReference()->home_object());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001667 PushOperand(result_register());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001668 if (expr->is_compound()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001669 PushOperand(MemOperand(rsp, kPointerSize));
1670 PushOperand(result_register());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001671 }
1672 break;
1673 case KEYED_SUPER_PROPERTY:
1674 VisitForStackValue(
1675 property->obj()->AsSuperPropertyReference()->this_var());
1676 VisitForStackValue(
1677 property->obj()->AsSuperPropertyReference()->home_object());
1678 VisitForAccumulatorValue(property->key());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001679 PushOperand(result_register());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001680 if (expr->is_compound()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001681 PushOperand(MemOperand(rsp, 2 * kPointerSize));
1682 PushOperand(MemOperand(rsp, 2 * kPointerSize));
1683 PushOperand(result_register());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001684 }
1685 break;
1686 case KEYED_PROPERTY: {
1687 if (expr->is_compound()) {
1688 VisitForStackValue(property->obj());
1689 VisitForStackValue(property->key());
1690 __ movp(LoadDescriptor::ReceiverRegister(), Operand(rsp, kPointerSize));
1691 __ movp(LoadDescriptor::NameRegister(), Operand(rsp, 0));
1692 } else {
1693 VisitForStackValue(property->obj());
1694 VisitForStackValue(property->key());
1695 }
1696 break;
1697 }
1698 }
1699
1700 // For compound assignments we need another deoptimization point after the
1701 // variable/property load.
1702 if (expr->is_compound()) {
1703 { AccumulatorValueContext context(this);
1704 switch (assign_type) {
1705 case VARIABLE:
1706 EmitVariableLoad(expr->target()->AsVariableProxy());
1707 PrepareForBailout(expr->target(), TOS_REG);
1708 break;
1709 case NAMED_PROPERTY:
1710 EmitNamedPropertyLoad(property);
1711 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1712 break;
1713 case NAMED_SUPER_PROPERTY:
1714 EmitNamedSuperPropertyLoad(property);
1715 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1716 break;
1717 case KEYED_SUPER_PROPERTY:
1718 EmitKeyedSuperPropertyLoad(property);
1719 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1720 break;
1721 case KEYED_PROPERTY:
1722 EmitKeyedPropertyLoad(property);
1723 PrepareForBailoutForId(property->LoadId(), TOS_REG);
1724 break;
1725 }
1726 }
1727
1728 Token::Value op = expr->binary_op();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001729 PushOperand(rax); // Left operand goes on the stack.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001730 VisitForAccumulatorValue(expr->value());
1731
1732 AccumulatorValueContext context(this);
1733 if (ShouldInlineSmiCase(op)) {
1734 EmitInlineSmiBinaryOp(expr->binary_operation(),
1735 op,
1736 expr->target(),
1737 expr->value());
1738 } else {
1739 EmitBinaryOp(expr->binary_operation(), op);
1740 }
1741 // Deoptimization point in case the binary operation may have side effects.
1742 PrepareForBailout(expr->binary_operation(), TOS_REG);
1743 } else {
1744 VisitForAccumulatorValue(expr->value());
1745 }
1746
1747 SetExpressionPosition(expr);
1748
1749 // Store the value.
1750 switch (assign_type) {
1751 case VARIABLE:
1752 EmitVariableAssignment(expr->target()->AsVariableProxy()->var(),
1753 expr->op(), expr->AssignmentSlot());
1754 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
1755 context()->Plug(rax);
1756 break;
1757 case NAMED_PROPERTY:
1758 EmitNamedPropertyAssignment(expr);
1759 break;
1760 case NAMED_SUPER_PROPERTY:
1761 EmitNamedSuperPropertyStore(property);
1762 context()->Plug(rax);
1763 break;
1764 case KEYED_SUPER_PROPERTY:
1765 EmitKeyedSuperPropertyStore(property);
1766 context()->Plug(rax);
1767 break;
1768 case KEYED_PROPERTY:
1769 EmitKeyedPropertyAssignment(expr);
1770 break;
1771 }
1772}
1773
1774
1775void FullCodeGenerator::VisitYield(Yield* expr) {
1776 Comment cmnt(masm_, "[ Yield");
1777 SetExpressionPosition(expr);
1778
1779 // Evaluate yielded value first; the initial iterator definition depends on
1780 // this. It stays on the stack while we update the iterator.
1781 VisitForStackValue(expr->expression());
1782
Ben Murdochda12d292016-06-02 14:46:10 +01001783 Label suspend, continuation, post_runtime, resume;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001784
Ben Murdochda12d292016-06-02 14:46:10 +01001785 __ jmp(&suspend);
1786 __ bind(&continuation);
1787 // When we arrive here, the stack top is the resume mode and
1788 // result_register() holds the input value (the argument given to the
1789 // respective resume operation).
1790 __ RecordGeneratorContinuation();
1791 __ Pop(rbx);
1792 __ SmiCompare(rbx, Smi::FromInt(JSGeneratorObject::RETURN));
1793 __ j(not_equal, &resume);
1794 __ Push(result_register());
1795 EmitCreateIteratorResult(true);
1796 EmitUnwindAndReturn();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001797
Ben Murdochda12d292016-06-02 14:46:10 +01001798 __ bind(&suspend);
1799 OperandStackDepthIncrement(1); // Not popped on this path.
1800 VisitForAccumulatorValue(expr->generator_object());
1801 DCHECK(continuation.pos() > 0 && Smi::IsValid(continuation.pos()));
1802 __ Move(FieldOperand(rax, JSGeneratorObject::kContinuationOffset),
1803 Smi::FromInt(continuation.pos()));
1804 __ movp(FieldOperand(rax, JSGeneratorObject::kContextOffset), rsi);
1805 __ movp(rcx, rsi);
1806 __ RecordWriteField(rax, JSGeneratorObject::kContextOffset, rcx, rdx,
1807 kDontSaveFPRegs);
1808 __ leap(rbx, Operand(rbp, StandardFrameConstants::kExpressionsOffset));
1809 __ cmpp(rsp, rbx);
1810 __ j(equal, &post_runtime);
1811 __ Push(rax); // generator object
1812 __ CallRuntime(Runtime::kSuspendJSGeneratorObject, 1);
1813 __ movp(context_register(),
1814 Operand(rbp, StandardFrameConstants::kContextOffset));
1815 __ bind(&post_runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001816
Ben Murdochda12d292016-06-02 14:46:10 +01001817 PopOperand(result_register());
1818 EmitReturnSequence();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001819
Ben Murdochda12d292016-06-02 14:46:10 +01001820 __ bind(&resume);
1821 context()->Plug(result_register());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001822}
1823
1824
Ben Murdoch097c5b22016-05-18 11:27:45 +01001825void FullCodeGenerator::EmitGeneratorResume(
1826 Expression* generator, Expression* value,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001827 JSGeneratorObject::ResumeMode resume_mode) {
1828 // The value stays in rax, and is ultimately read by the resumed generator, as
1829 // if CallRuntime(Runtime::kSuspendJSGeneratorObject) returned it. Or it
1830 // is read to throw the value when the resumed generator is already closed.
1831 // rbx will hold the generator object until the activation has been resumed.
1832 VisitForStackValue(generator);
1833 VisitForAccumulatorValue(value);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001834 PopOperand(rbx);
1835
1836 // Store input value into generator object.
1837 __ movp(FieldOperand(rbx, JSGeneratorObject::kInputOffset),
1838 result_register());
1839 __ movp(rcx, result_register());
1840 __ RecordWriteField(rbx, JSGeneratorObject::kInputOffset, rcx, rdx,
1841 kDontSaveFPRegs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001842
1843 // Load suspended function and context.
1844 __ movp(rsi, FieldOperand(rbx, JSGeneratorObject::kContextOffset));
1845 __ movp(rdi, FieldOperand(rbx, JSGeneratorObject::kFunctionOffset));
1846
1847 // Push receiver.
1848 __ Push(FieldOperand(rbx, JSGeneratorObject::kReceiverOffset));
1849
Ben Murdochda12d292016-06-02 14:46:10 +01001850 // Push holes for arguments to generator function. Since the parser forced
1851 // context allocation for any variables in generators, the actual argument
1852 // values have already been copied into the context and these dummy values
1853 // will never be used.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001854 __ movp(rdx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
1855 __ LoadSharedFunctionInfoSpecialField(rdx, rdx,
1856 SharedFunctionInfo::kFormalParameterCountOffset);
1857 __ LoadRoot(rcx, Heap::kTheHoleValueRootIndex);
1858 Label push_argument_holes, push_frame;
1859 __ bind(&push_argument_holes);
1860 __ subp(rdx, Immediate(1));
1861 __ j(carry, &push_frame);
1862 __ Push(rcx);
1863 __ jmp(&push_argument_holes);
1864
1865 // Enter a new JavaScript frame, and initialize its slots as they were when
1866 // the generator was suspended.
1867 Label resume_frame, done;
1868 __ bind(&push_frame);
1869 __ call(&resume_frame);
1870 __ jmp(&done);
1871 __ bind(&resume_frame);
1872 __ pushq(rbp); // Caller's frame pointer.
1873 __ movp(rbp, rsp);
1874 __ Push(rsi); // Callee's context.
1875 __ Push(rdi); // Callee's JS Function.
1876
1877 // Load the operand stack size.
1878 __ movp(rdx, FieldOperand(rbx, JSGeneratorObject::kOperandStackOffset));
1879 __ movp(rdx, FieldOperand(rdx, FixedArray::kLengthOffset));
1880 __ SmiToInteger32(rdx, rdx);
1881
1882 // If we are sending a value and there is no operand stack, we can jump back
1883 // in directly.
1884 if (resume_mode == JSGeneratorObject::NEXT) {
1885 Label slow_resume;
1886 __ cmpp(rdx, Immediate(0));
1887 __ j(not_zero, &slow_resume);
1888 __ movp(rdx, FieldOperand(rdi, JSFunction::kCodeEntryOffset));
1889 __ SmiToInteger64(rcx,
1890 FieldOperand(rbx, JSGeneratorObject::kContinuationOffset));
1891 __ addp(rdx, rcx);
1892 __ Move(FieldOperand(rbx, JSGeneratorObject::kContinuationOffset),
1893 Smi::FromInt(JSGeneratorObject::kGeneratorExecuting));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001894 __ Push(Smi::FromInt(resume_mode)); // Consumed in continuation.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001895 __ jmp(rdx);
1896 __ bind(&slow_resume);
1897 }
1898
1899 // Otherwise, we push holes for the operand stack and call the runtime to fix
1900 // up the stack and the handlers.
1901 Label push_operand_holes, call_resume;
1902 __ bind(&push_operand_holes);
1903 __ subp(rdx, Immediate(1));
1904 __ j(carry, &call_resume);
1905 __ Push(rcx);
1906 __ jmp(&push_operand_holes);
1907 __ bind(&call_resume);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001908 __ Push(Smi::FromInt(resume_mode)); // Consumed in continuation.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001909 __ Push(rbx);
1910 __ Push(result_register());
1911 __ Push(Smi::FromInt(resume_mode));
1912 __ CallRuntime(Runtime::kResumeJSGeneratorObject);
1913 // Not reached: the runtime call returns elsewhere.
1914 __ Abort(kGeneratorFailedToResume);
1915
1916 __ bind(&done);
1917 context()->Plug(result_register());
1918}
1919
Ben Murdoch097c5b22016-05-18 11:27:45 +01001920void FullCodeGenerator::PushOperand(MemOperand operand) {
1921 OperandStackDepthIncrement(1);
1922 __ Push(operand);
1923}
1924
1925void FullCodeGenerator::EmitOperandStackDepthCheck() {
1926 if (FLAG_debug_code) {
1927 int expected_diff = StandardFrameConstants::kFixedFrameSizeFromFp +
1928 operand_stack_depth_ * kPointerSize;
1929 __ movp(rax, rbp);
1930 __ subp(rax, rsp);
1931 __ cmpp(rax, Immediate(expected_diff));
1932 __ Assert(equal, kUnexpectedStackDepth);
1933 }
1934}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001935
1936void FullCodeGenerator::EmitCreateIteratorResult(bool done) {
1937 Label allocate, done_allocate;
1938
1939 __ Allocate(JSIteratorResult::kSize, rax, rcx, rdx, &allocate, TAG_OBJECT);
1940 __ jmp(&done_allocate, Label::kNear);
1941
1942 __ bind(&allocate);
1943 __ Push(Smi::FromInt(JSIteratorResult::kSize));
1944 __ CallRuntime(Runtime::kAllocateInNewSpace);
1945
1946 __ bind(&done_allocate);
1947 __ LoadNativeContextSlot(Context::ITERATOR_RESULT_MAP_INDEX, rbx);
1948 __ movp(FieldOperand(rax, HeapObject::kMapOffset), rbx);
1949 __ LoadRoot(rbx, Heap::kEmptyFixedArrayRootIndex);
1950 __ movp(FieldOperand(rax, JSObject::kPropertiesOffset), rbx);
1951 __ movp(FieldOperand(rax, JSObject::kElementsOffset), rbx);
1952 __ Pop(FieldOperand(rax, JSIteratorResult::kValueOffset));
1953 __ LoadRoot(FieldOperand(rax, JSIteratorResult::kDoneOffset),
1954 done ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex);
1955 STATIC_ASSERT(JSIteratorResult::kSize == 5 * kPointerSize);
Ben Murdochda12d292016-06-02 14:46:10 +01001956 OperandStackDepthDecrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001957}
1958
1959
1960void FullCodeGenerator::EmitInlineSmiBinaryOp(BinaryOperation* expr,
1961 Token::Value op,
1962 Expression* left,
1963 Expression* right) {
1964 // Do combined smi check of the operands. Left operand is on the
1965 // stack (popped into rdx). Right operand is in rax but moved into
1966 // rcx to make the shifts easier.
1967 Label done, stub_call, smi_case;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001968 PopOperand(rdx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001969 __ movp(rcx, rax);
1970 __ orp(rax, rdx);
1971 JumpPatchSite patch_site(masm_);
1972 patch_site.EmitJumpIfSmi(rax, &smi_case, Label::kNear);
1973
1974 __ bind(&stub_call);
1975 __ movp(rax, rcx);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001976 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), op).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001977 CallIC(code, expr->BinaryOperationFeedbackId());
1978 patch_site.EmitPatchInfo();
1979 __ jmp(&done, Label::kNear);
1980
1981 __ bind(&smi_case);
1982 switch (op) {
1983 case Token::SAR:
1984 __ SmiShiftArithmeticRight(rax, rdx, rcx);
1985 break;
1986 case Token::SHL:
1987 __ SmiShiftLeft(rax, rdx, rcx, &stub_call);
1988 break;
1989 case Token::SHR:
1990 __ SmiShiftLogicalRight(rax, rdx, rcx, &stub_call);
1991 break;
1992 case Token::ADD:
1993 __ SmiAdd(rax, rdx, rcx, &stub_call);
1994 break;
1995 case Token::SUB:
1996 __ SmiSub(rax, rdx, rcx, &stub_call);
1997 break;
1998 case Token::MUL:
1999 __ SmiMul(rax, rdx, rcx, &stub_call);
2000 break;
2001 case Token::BIT_OR:
2002 __ SmiOr(rax, rdx, rcx);
2003 break;
2004 case Token::BIT_AND:
2005 __ SmiAnd(rax, rdx, rcx);
2006 break;
2007 case Token::BIT_XOR:
2008 __ SmiXor(rax, rdx, rcx);
2009 break;
2010 default:
2011 UNREACHABLE();
2012 break;
2013 }
2014
2015 __ bind(&done);
2016 context()->Plug(rax);
2017}
2018
2019
2020void FullCodeGenerator::EmitClassDefineProperties(ClassLiteral* lit) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002021 for (int i = 0; i < lit->properties()->length(); i++) {
2022 ObjectLiteral::Property* property = lit->properties()->at(i);
2023 Expression* value = property->value();
2024
2025 if (property->is_static()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002026 PushOperand(Operand(rsp, kPointerSize)); // constructor
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002027 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002028 PushOperand(Operand(rsp, 0)); // prototype
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002029 }
2030 EmitPropertyKey(property, lit->GetIdForProperty(i));
2031
2032 // The static prototype property is read only. We handle the non computed
2033 // property name case in the parser. Since this is the only case where we
2034 // need to check for an own read only property we special case this so we do
2035 // not need to do this for every property.
2036 if (property->is_static() && property->is_computed_name()) {
2037 __ CallRuntime(Runtime::kThrowIfStaticPrototype);
2038 __ Push(rax);
2039 }
2040
2041 VisitForStackValue(value);
2042 if (NeedsHomeObject(value)) {
2043 EmitSetHomeObject(value, 2, property->GetSlot());
2044 }
2045
2046 switch (property->kind()) {
2047 case ObjectLiteral::Property::CONSTANT:
2048 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2049 case ObjectLiteral::Property::PROTOTYPE:
2050 UNREACHABLE();
2051 case ObjectLiteral::Property::COMPUTED:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002052 PushOperand(Smi::FromInt(DONT_ENUM));
2053 PushOperand(Smi::FromInt(property->NeedsSetFunctionName()));
2054 CallRuntimeWithOperands(Runtime::kDefineDataPropertyInLiteral);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002055 break;
2056
2057 case ObjectLiteral::Property::GETTER:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002058 PushOperand(Smi::FromInt(DONT_ENUM));
2059 CallRuntimeWithOperands(Runtime::kDefineGetterPropertyUnchecked);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002060 break;
2061
2062 case ObjectLiteral::Property::SETTER:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002063 PushOperand(Smi::FromInt(DONT_ENUM));
2064 CallRuntimeWithOperands(Runtime::kDefineSetterPropertyUnchecked);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002065 break;
2066
2067 default:
2068 UNREACHABLE();
2069 }
2070 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002071}
2072
2073
2074void FullCodeGenerator::EmitBinaryOp(BinaryOperation* expr, Token::Value op) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002075 PopOperand(rdx);
2076 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), op).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002077 JumpPatchSite patch_site(masm_); // unbound, signals no inlined smi code.
2078 CallIC(code, expr->BinaryOperationFeedbackId());
2079 patch_site.EmitPatchInfo();
2080 context()->Plug(rax);
2081}
2082
2083
2084void FullCodeGenerator::EmitAssignment(Expression* expr,
2085 FeedbackVectorSlot slot) {
2086 DCHECK(expr->IsValidReferenceExpressionOrThis());
2087
2088 Property* prop = expr->AsProperty();
2089 LhsKind assign_type = Property::GetAssignType(prop);
2090
2091 switch (assign_type) {
2092 case VARIABLE: {
2093 Variable* var = expr->AsVariableProxy()->var();
2094 EffectContext context(this);
2095 EmitVariableAssignment(var, Token::ASSIGN, slot);
2096 break;
2097 }
2098 case NAMED_PROPERTY: {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002099 PushOperand(rax); // Preserve value.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002100 VisitForAccumulatorValue(prop->obj());
2101 __ Move(StoreDescriptor::ReceiverRegister(), rax);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002102 PopOperand(StoreDescriptor::ValueRegister()); // Restore value.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002103 __ Move(StoreDescriptor::NameRegister(),
2104 prop->key()->AsLiteral()->value());
2105 EmitLoadStoreICSlot(slot);
2106 CallStoreIC();
2107 break;
2108 }
2109 case NAMED_SUPER_PROPERTY: {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002110 PushOperand(rax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002111 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2112 VisitForAccumulatorValue(
2113 prop->obj()->AsSuperPropertyReference()->home_object());
2114 // stack: value, this; rax: home_object
2115 Register scratch = rcx;
2116 Register scratch2 = rdx;
2117 __ Move(scratch, result_register()); // home_object
2118 __ movp(rax, MemOperand(rsp, kPointerSize)); // value
2119 __ movp(scratch2, MemOperand(rsp, 0)); // this
2120 __ movp(MemOperand(rsp, kPointerSize), scratch2); // this
2121 __ movp(MemOperand(rsp, 0), scratch); // home_object
2122 // stack: this, home_object; rax: value
2123 EmitNamedSuperPropertyStore(prop);
2124 break;
2125 }
2126 case KEYED_SUPER_PROPERTY: {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002127 PushOperand(rax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002128 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
2129 VisitForStackValue(
2130 prop->obj()->AsSuperPropertyReference()->home_object());
2131 VisitForAccumulatorValue(prop->key());
2132 Register scratch = rcx;
2133 Register scratch2 = rdx;
2134 __ movp(scratch2, MemOperand(rsp, 2 * kPointerSize)); // value
2135 // stack: value, this, home_object; rax: key, rdx: value
2136 __ movp(scratch, MemOperand(rsp, kPointerSize)); // this
2137 __ movp(MemOperand(rsp, 2 * kPointerSize), scratch);
2138 __ movp(scratch, MemOperand(rsp, 0)); // home_object
2139 __ movp(MemOperand(rsp, kPointerSize), scratch);
2140 __ movp(MemOperand(rsp, 0), rax);
2141 __ Move(rax, scratch2);
2142 // stack: this, home_object, key; rax: value.
2143 EmitKeyedSuperPropertyStore(prop);
2144 break;
2145 }
2146 case KEYED_PROPERTY: {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002147 PushOperand(rax); // Preserve value.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002148 VisitForStackValue(prop->obj());
2149 VisitForAccumulatorValue(prop->key());
2150 __ Move(StoreDescriptor::NameRegister(), rax);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002151 PopOperand(StoreDescriptor::ReceiverRegister());
2152 PopOperand(StoreDescriptor::ValueRegister()); // Restore value.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002153 EmitLoadStoreICSlot(slot);
2154 Handle<Code> ic =
2155 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2156 CallIC(ic);
2157 break;
2158 }
2159 }
2160 context()->Plug(rax);
2161}
2162
2163
2164void FullCodeGenerator::EmitStoreToStackLocalOrContextSlot(
2165 Variable* var, MemOperand location) {
2166 __ movp(location, rax);
2167 if (var->IsContextSlot()) {
2168 __ movp(rdx, rax);
2169 __ RecordWriteContextSlot(
2170 rcx, Context::SlotOffset(var->index()), rdx, rbx, kDontSaveFPRegs);
2171 }
2172}
2173
2174
2175void FullCodeGenerator::EmitVariableAssignment(Variable* var, Token::Value op,
2176 FeedbackVectorSlot slot) {
2177 if (var->IsUnallocated()) {
2178 // Global var, const, or let.
2179 __ Move(StoreDescriptor::NameRegister(), var->name());
2180 __ LoadGlobalObject(StoreDescriptor::ReceiverRegister());
2181 EmitLoadStoreICSlot(slot);
2182 CallStoreIC();
2183
2184 } else if (var->mode() == LET && op != Token::INIT) {
2185 // Non-initializing assignment to let variable needs a write barrier.
2186 DCHECK(!var->IsLookupSlot());
2187 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2188 Label assign;
2189 MemOperand location = VarOperand(var, rcx);
2190 __ movp(rdx, location);
2191 __ CompareRoot(rdx, Heap::kTheHoleValueRootIndex);
2192 __ j(not_equal, &assign, Label::kNear);
2193 __ Push(var->name());
2194 __ CallRuntime(Runtime::kThrowReferenceError);
2195 __ bind(&assign);
2196 EmitStoreToStackLocalOrContextSlot(var, location);
2197
2198 } else if (var->mode() == CONST && op != Token::INIT) {
2199 // Assignment to const variable needs a write barrier.
2200 DCHECK(!var->IsLookupSlot());
2201 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2202 Label const_error;
2203 MemOperand location = VarOperand(var, rcx);
2204 __ movp(rdx, location);
2205 __ CompareRoot(rdx, Heap::kTheHoleValueRootIndex);
2206 __ j(not_equal, &const_error, Label::kNear);
2207 __ Push(var->name());
2208 __ CallRuntime(Runtime::kThrowReferenceError);
2209 __ bind(&const_error);
2210 __ CallRuntime(Runtime::kThrowConstAssignError);
2211
2212 } else if (var->is_this() && var->mode() == CONST && op == Token::INIT) {
2213 // Initializing assignment to const {this} needs a write barrier.
2214 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2215 Label uninitialized_this;
2216 MemOperand location = VarOperand(var, rcx);
2217 __ movp(rdx, location);
2218 __ CompareRoot(rdx, Heap::kTheHoleValueRootIndex);
2219 __ j(equal, &uninitialized_this);
2220 __ Push(var->name());
2221 __ CallRuntime(Runtime::kThrowReferenceError);
2222 __ bind(&uninitialized_this);
2223 EmitStoreToStackLocalOrContextSlot(var, location);
2224
2225 } else if (!var->is_const_mode() ||
2226 (var->mode() == CONST && op == Token::INIT)) {
2227 if (var->IsLookupSlot()) {
2228 // Assignment to var.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002229 __ Push(var->name());
Ben Murdoch097c5b22016-05-18 11:27:45 +01002230 __ Push(rax);
2231 __ CallRuntime(is_strict(language_mode())
2232 ? Runtime::kStoreLookupSlot_Strict
2233 : Runtime::kStoreLookupSlot_Sloppy);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002234 } else {
2235 // Assignment to var or initializing assignment to let/const in harmony
2236 // mode.
2237 DCHECK(var->IsStackAllocated() || var->IsContextSlot());
2238 MemOperand location = VarOperand(var, rcx);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002239 if (FLAG_debug_code && var->mode() == LET && op == Token::INIT) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002240 // Check for an uninitialized let binding.
2241 __ movp(rdx, location);
2242 __ CompareRoot(rdx, Heap::kTheHoleValueRootIndex);
2243 __ Check(equal, kLetBindingReInitialization);
2244 }
2245 EmitStoreToStackLocalOrContextSlot(var, location);
2246 }
2247
2248 } else if (var->mode() == CONST_LEGACY && op == Token::INIT) {
2249 // Const initializers need a write barrier.
2250 DCHECK(!var->IsParameter()); // No const parameters.
2251 if (var->IsLookupSlot()) {
2252 __ Push(rax);
2253 __ Push(rsi);
2254 __ Push(var->name());
2255 __ CallRuntime(Runtime::kInitializeLegacyConstLookupSlot);
2256 } else {
2257 DCHECK(var->IsStackLocal() || var->IsContextSlot());
2258 Label skip;
2259 MemOperand location = VarOperand(var, rcx);
2260 __ movp(rdx, location);
2261 __ CompareRoot(rdx, Heap::kTheHoleValueRootIndex);
2262 __ j(not_equal, &skip);
2263 EmitStoreToStackLocalOrContextSlot(var, location);
2264 __ bind(&skip);
2265 }
2266
2267 } else {
2268 DCHECK(var->mode() == CONST_LEGACY && op != Token::INIT);
2269 if (is_strict(language_mode())) {
2270 __ CallRuntime(Runtime::kThrowConstAssignError);
2271 }
2272 // Silently ignore store in sloppy mode.
2273 }
2274}
2275
2276
2277void FullCodeGenerator::EmitNamedPropertyAssignment(Assignment* expr) {
2278 // Assignment to a property, using a named store IC.
2279 Property* prop = expr->target()->AsProperty();
2280 DCHECK(prop != NULL);
2281 DCHECK(prop->key()->IsLiteral());
2282
2283 __ Move(StoreDescriptor::NameRegister(), prop->key()->AsLiteral()->value());
Ben Murdoch097c5b22016-05-18 11:27:45 +01002284 PopOperand(StoreDescriptor::ReceiverRegister());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002285 EmitLoadStoreICSlot(expr->AssignmentSlot());
2286 CallStoreIC();
2287
2288 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2289 context()->Plug(rax);
2290}
2291
2292
2293void FullCodeGenerator::EmitNamedSuperPropertyStore(Property* prop) {
2294 // Assignment to named property of super.
2295 // rax : value
2296 // stack : receiver ('this'), home_object
2297 DCHECK(prop != NULL);
2298 Literal* key = prop->key()->AsLiteral();
2299 DCHECK(key != NULL);
2300
Ben Murdoch097c5b22016-05-18 11:27:45 +01002301 PushOperand(key->value());
2302 PushOperand(rax);
2303 CallRuntimeWithOperands(is_strict(language_mode())
2304 ? Runtime::kStoreToSuper_Strict
2305 : Runtime::kStoreToSuper_Sloppy);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002306}
2307
2308
2309void FullCodeGenerator::EmitKeyedSuperPropertyStore(Property* prop) {
2310 // Assignment to named property of super.
2311 // rax : value
2312 // stack : receiver ('this'), home_object, key
2313 DCHECK(prop != NULL);
2314
Ben Murdoch097c5b22016-05-18 11:27:45 +01002315 PushOperand(rax);
2316 CallRuntimeWithOperands(is_strict(language_mode())
2317 ? Runtime::kStoreKeyedToSuper_Strict
2318 : Runtime::kStoreKeyedToSuper_Sloppy);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002319}
2320
2321
2322void FullCodeGenerator::EmitKeyedPropertyAssignment(Assignment* expr) {
2323 // Assignment to a property, using a keyed store IC.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002324 PopOperand(StoreDescriptor::NameRegister()); // Key.
2325 PopOperand(StoreDescriptor::ReceiverRegister());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002326 DCHECK(StoreDescriptor::ValueRegister().is(rax));
2327 Handle<Code> ic =
2328 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
2329 EmitLoadStoreICSlot(expr->AssignmentSlot());
2330 CallIC(ic);
2331
2332 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
2333 context()->Plug(rax);
2334}
2335
2336
2337void FullCodeGenerator::VisitProperty(Property* expr) {
2338 Comment cmnt(masm_, "[ Property");
2339 SetExpressionPosition(expr);
2340
2341 Expression* key = expr->key();
2342
2343 if (key->IsPropertyName()) {
2344 if (!expr->IsSuperAccess()) {
2345 VisitForAccumulatorValue(expr->obj());
2346 DCHECK(!rax.is(LoadDescriptor::ReceiverRegister()));
2347 __ movp(LoadDescriptor::ReceiverRegister(), rax);
2348 EmitNamedPropertyLoad(expr);
2349 } else {
2350 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2351 VisitForStackValue(
2352 expr->obj()->AsSuperPropertyReference()->home_object());
2353 EmitNamedSuperPropertyLoad(expr);
2354 }
2355 } else {
2356 if (!expr->IsSuperAccess()) {
2357 VisitForStackValue(expr->obj());
2358 VisitForAccumulatorValue(expr->key());
2359 __ Move(LoadDescriptor::NameRegister(), rax);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002360 PopOperand(LoadDescriptor::ReceiverRegister());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002361 EmitKeyedPropertyLoad(expr);
2362 } else {
2363 VisitForStackValue(expr->obj()->AsSuperPropertyReference()->this_var());
2364 VisitForStackValue(
2365 expr->obj()->AsSuperPropertyReference()->home_object());
2366 VisitForStackValue(expr->key());
2367 EmitKeyedSuperPropertyLoad(expr);
2368 }
2369 }
2370 PrepareForBailoutForId(expr->LoadId(), TOS_REG);
2371 context()->Plug(rax);
2372}
2373
2374
2375void FullCodeGenerator::CallIC(Handle<Code> code,
2376 TypeFeedbackId ast_id) {
2377 ic_total_count_++;
2378 __ call(code, RelocInfo::CODE_TARGET, ast_id);
2379}
2380
2381
2382// Code common for calls using the IC.
2383void FullCodeGenerator::EmitCallWithLoadIC(Call* expr) {
2384 Expression* callee = expr->expression();
2385
2386 // Get the target function.
2387 ConvertReceiverMode convert_mode;
2388 if (callee->IsVariableProxy()) {
2389 { StackValueContext context(this);
2390 EmitVariableLoad(callee->AsVariableProxy());
2391 PrepareForBailout(callee, NO_REGISTERS);
2392 }
2393 // Push undefined as receiver. This is patched in the Call builtin if it
2394 // is a sloppy mode method.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002395 PushOperand(isolate()->factory()->undefined_value());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002396 convert_mode = ConvertReceiverMode::kNullOrUndefined;
2397 } else {
2398 // Load the function from the receiver.
2399 DCHECK(callee->IsProperty());
2400 DCHECK(!callee->AsProperty()->IsSuperAccess());
2401 __ movp(LoadDescriptor::ReceiverRegister(), Operand(rsp, 0));
2402 EmitNamedPropertyLoad(callee->AsProperty());
2403 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2404 // Push the target function under the receiver.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002405 PushOperand(Operand(rsp, 0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002406 __ movp(Operand(rsp, kPointerSize), rax);
2407 convert_mode = ConvertReceiverMode::kNotNullOrUndefined;
2408 }
2409
2410 EmitCall(expr, convert_mode);
2411}
2412
2413
2414void FullCodeGenerator::EmitSuperCallWithLoadIC(Call* expr) {
2415 Expression* callee = expr->expression();
2416 DCHECK(callee->IsProperty());
2417 Property* prop = callee->AsProperty();
2418 DCHECK(prop->IsSuperAccess());
2419 SetExpressionPosition(prop);
2420
2421 Literal* key = prop->key()->AsLiteral();
2422 DCHECK(!key->value()->IsSmi());
2423 // Load the function from the receiver.
2424 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
2425 VisitForStackValue(super_ref->home_object());
2426 VisitForAccumulatorValue(super_ref->this_var());
Ben Murdoch097c5b22016-05-18 11:27:45 +01002427 PushOperand(rax);
2428 PushOperand(rax);
2429 PushOperand(Operand(rsp, kPointerSize * 2));
2430 PushOperand(key->value());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002431
2432 // Stack here:
2433 // - home_object
2434 // - this (receiver)
2435 // - this (receiver) <-- LoadFromSuper will pop here and below.
2436 // - home_object
2437 // - key
Ben Murdoch097c5b22016-05-18 11:27:45 +01002438 CallRuntimeWithOperands(Runtime::kLoadFromSuper);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002439
2440 // Replace home_object with target function.
2441 __ movp(Operand(rsp, kPointerSize), rax);
2442
2443 // Stack here:
2444 // - target function
2445 // - this (receiver)
2446 EmitCall(expr);
2447}
2448
2449
2450// Common code for calls using the IC.
2451void FullCodeGenerator::EmitKeyedCallWithLoadIC(Call* expr,
2452 Expression* key) {
2453 // Load the key.
2454 VisitForAccumulatorValue(key);
2455
2456 Expression* callee = expr->expression();
2457
2458 // Load the function from the receiver.
2459 DCHECK(callee->IsProperty());
2460 __ movp(LoadDescriptor::ReceiverRegister(), Operand(rsp, 0));
2461 __ Move(LoadDescriptor::NameRegister(), rax);
2462 EmitKeyedPropertyLoad(callee->AsProperty());
2463 PrepareForBailoutForId(callee->AsProperty()->LoadId(), TOS_REG);
2464
2465 // Push the target function under the receiver.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002466 PushOperand(Operand(rsp, 0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002467 __ movp(Operand(rsp, kPointerSize), rax);
2468
2469 EmitCall(expr, ConvertReceiverMode::kNotNullOrUndefined);
2470}
2471
2472
2473void FullCodeGenerator::EmitKeyedSuperCallWithLoadIC(Call* expr) {
2474 Expression* callee = expr->expression();
2475 DCHECK(callee->IsProperty());
2476 Property* prop = callee->AsProperty();
2477 DCHECK(prop->IsSuperAccess());
2478
2479 SetExpressionPosition(prop);
2480 // Load the function from the receiver.
2481 SuperPropertyReference* super_ref = prop->obj()->AsSuperPropertyReference();
2482 VisitForStackValue(super_ref->home_object());
2483 VisitForAccumulatorValue(super_ref->this_var());
Ben Murdoch097c5b22016-05-18 11:27:45 +01002484 PushOperand(rax);
2485 PushOperand(rax);
2486 PushOperand(Operand(rsp, kPointerSize * 2));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002487 VisitForStackValue(prop->key());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002488
2489 // Stack here:
2490 // - home_object
2491 // - this (receiver)
2492 // - this (receiver) <-- LoadKeyedFromSuper will pop here and below.
2493 // - home_object
2494 // - key
Ben Murdoch097c5b22016-05-18 11:27:45 +01002495 CallRuntimeWithOperands(Runtime::kLoadKeyedFromSuper);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002496
2497 // Replace home_object with target function.
2498 __ movp(Operand(rsp, kPointerSize), rax);
2499
2500 // Stack here:
2501 // - target function
2502 // - this (receiver)
2503 EmitCall(expr);
2504}
2505
2506
2507void FullCodeGenerator::EmitCall(Call* expr, ConvertReceiverMode mode) {
2508 // Load the arguments.
2509 ZoneList<Expression*>* args = expr->arguments();
2510 int arg_count = args->length();
2511 for (int i = 0; i < arg_count; i++) {
2512 VisitForStackValue(args->at(i));
2513 }
2514
2515 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
Ben Murdochda12d292016-06-02 14:46:10 +01002516 SetCallPosition(expr, expr->tail_call_mode());
Ben Murdoch097c5b22016-05-18 11:27:45 +01002517 if (expr->tail_call_mode() == TailCallMode::kAllow) {
2518 if (FLAG_trace) {
2519 __ CallRuntime(Runtime::kTraceTailCall);
2520 }
2521 // Update profiling counters before the tail call since we will
2522 // not return to this function.
2523 EmitProfilingCounterHandlingForReturnSequence(true);
2524 }
2525 Handle<Code> ic =
2526 CodeFactory::CallIC(isolate(), arg_count, mode, expr->tail_call_mode())
2527 .code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002528 __ Move(rdx, SmiFromSlot(expr->CallFeedbackICSlot()));
2529 __ movp(rdi, Operand(rsp, (arg_count + 1) * kPointerSize));
2530 // Don't assign a type feedback id to the IC, since type feedback is provided
2531 // by the vector above.
2532 CallIC(ic);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002533 OperandStackDepthDecrement(arg_count + 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002534
2535 RecordJSReturnSite(expr);
2536
2537 // Restore context register.
2538 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
2539 // Discard the function left on TOS.
2540 context()->DropAndPlug(1, rax);
2541}
2542
2543
2544void FullCodeGenerator::EmitResolvePossiblyDirectEval(int arg_count) {
2545 // Push copy of the first argument or undefined if it doesn't exist.
2546 if (arg_count > 0) {
2547 __ Push(Operand(rsp, arg_count * kPointerSize));
2548 } else {
2549 __ PushRoot(Heap::kUndefinedValueRootIndex);
2550 }
2551
2552 // Push the enclosing function.
2553 __ Push(Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
2554
2555 // Push the language mode.
2556 __ Push(Smi::FromInt(language_mode()));
2557
2558 // Push the start position of the scope the calls resides in.
2559 __ Push(Smi::FromInt(scope()->start_position()));
2560
2561 // Do the runtime call.
2562 __ CallRuntime(Runtime::kResolvePossiblyDirectEval);
2563}
2564
2565
2566// See http://www.ecma-international.org/ecma-262/6.0/#sec-function-calls.
2567void FullCodeGenerator::PushCalleeAndWithBaseObject(Call* expr) {
2568 VariableProxy* callee = expr->expression()->AsVariableProxy();
2569 if (callee->var()->IsLookupSlot()) {
2570 Label slow, done;
2571 SetExpressionPosition(callee);
2572 // Generate code for loading from variables potentially shadowed by
2573 // eval-introduced variables.
2574 EmitDynamicLookupFastCase(callee, NOT_INSIDE_TYPEOF, &slow, &done);
2575 __ bind(&slow);
2576 // Call the runtime to find the function to call (returned in rax) and
2577 // the object holding it (returned in rdx).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002578 __ Push(callee->name());
Ben Murdoch097c5b22016-05-18 11:27:45 +01002579 __ CallRuntime(Runtime::kLoadLookupSlotForCall);
2580 PushOperand(rax); // Function.
2581 PushOperand(rdx); // Receiver.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002582 PrepareForBailoutForId(expr->LookupId(), NO_REGISTERS);
2583
2584 // If fast case code has been generated, emit code to push the function
2585 // and receiver and have the slow path jump around this code.
2586 if (done.is_linked()) {
2587 Label call;
2588 __ jmp(&call, Label::kNear);
2589 __ bind(&done);
2590 // Push function.
2591 __ Push(rax);
2592 // Pass undefined as the receiver, which is the WithBaseObject of a
2593 // non-object environment record. If the callee is sloppy, it will patch
2594 // it up to be the global receiver.
2595 __ PushRoot(Heap::kUndefinedValueRootIndex);
2596 __ bind(&call);
2597 }
2598 } else {
2599 VisitForStackValue(callee);
2600 // refEnv.WithBaseObject()
Ben Murdoch097c5b22016-05-18 11:27:45 +01002601 OperandStackDepthIncrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002602 __ PushRoot(Heap::kUndefinedValueRootIndex);
2603 }
2604}
2605
2606
2607void FullCodeGenerator::EmitPossiblyEvalCall(Call* expr) {
2608 // In a call to eval, we first call RuntimeHidden_ResolvePossiblyDirectEval
2609 // to resolve the function we need to call. Then we call the resolved
2610 // function using the given arguments.
2611 ZoneList<Expression*>* args = expr->arguments();
2612 int arg_count = args->length();
2613 PushCalleeAndWithBaseObject(expr);
2614
2615 // Push the arguments.
2616 for (int i = 0; i < arg_count; i++) {
2617 VisitForStackValue(args->at(i));
2618 }
2619
2620 // Push a copy of the function (found below the arguments) and resolve
2621 // eval.
2622 __ Push(Operand(rsp, (arg_count + 1) * kPointerSize));
2623 EmitResolvePossiblyDirectEval(arg_count);
2624
2625 // Touch up the callee.
2626 __ movp(Operand(rsp, (arg_count + 1) * kPointerSize), rax);
2627
2628 PrepareForBailoutForId(expr->EvalId(), NO_REGISTERS);
2629
2630 SetCallPosition(expr);
2631 __ movp(rdi, Operand(rsp, (arg_count + 1) * kPointerSize));
2632 __ Set(rax, arg_count);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002633 __ Call(isolate()->builtins()->Call(ConvertReceiverMode::kAny,
2634 expr->tail_call_mode()),
2635 RelocInfo::CODE_TARGET);
2636 OperandStackDepthDecrement(arg_count + 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002637 RecordJSReturnSite(expr);
2638 // Restore context register.
2639 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
2640 context()->DropAndPlug(1, rax);
2641}
2642
2643
2644void FullCodeGenerator::VisitCallNew(CallNew* expr) {
2645 Comment cmnt(masm_, "[ CallNew");
2646 // According to ECMA-262, section 11.2.2, page 44, the function
2647 // expression in new calls must be evaluated before the
2648 // arguments.
2649
2650 // Push constructor on the stack. If it's not a function it's used as
2651 // receiver for CALL_NON_FUNCTION, otherwise the value on the stack is
2652 // ignored.
2653 DCHECK(!expr->expression()->IsSuperPropertyReference());
2654 VisitForStackValue(expr->expression());
2655
2656 // Push the arguments ("left-to-right") on the stack.
2657 ZoneList<Expression*>* args = expr->arguments();
2658 int arg_count = args->length();
2659 for (int i = 0; i < arg_count; i++) {
2660 VisitForStackValue(args->at(i));
2661 }
2662
2663 // Call the construct call builtin that handles allocation and
2664 // constructor invocation.
2665 SetConstructCallPosition(expr);
2666
2667 // Load function and argument count into rdi and rax.
2668 __ Set(rax, arg_count);
2669 __ movp(rdi, Operand(rsp, arg_count * kPointerSize));
2670
2671 // Record call targets in unoptimized code, but not in the snapshot.
2672 __ EmitLoadTypeFeedbackVector(rbx);
2673 __ Move(rdx, SmiFromSlot(expr->CallNewFeedbackSlot()));
2674
2675 CallConstructStub stub(isolate());
2676 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002677 OperandStackDepthDecrement(arg_count + 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002678 PrepareForBailoutForId(expr->ReturnId(), TOS_REG);
2679 // Restore context register.
2680 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
2681 context()->Plug(rax);
2682}
2683
2684
2685void FullCodeGenerator::EmitSuperConstructorCall(Call* expr) {
2686 SuperCallReference* super_call_ref =
2687 expr->expression()->AsSuperCallReference();
2688 DCHECK_NOT_NULL(super_call_ref);
2689
2690 // Push the super constructor target on the stack (may be null,
2691 // but the Construct builtin can deal with that properly).
2692 VisitForAccumulatorValue(super_call_ref->this_function_var());
2693 __ AssertFunction(result_register());
2694 __ movp(result_register(),
2695 FieldOperand(result_register(), HeapObject::kMapOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002696 PushOperand(FieldOperand(result_register(), Map::kPrototypeOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002697
2698 // Push the arguments ("left-to-right") on the stack.
2699 ZoneList<Expression*>* args = expr->arguments();
2700 int arg_count = args->length();
2701 for (int i = 0; i < arg_count; i++) {
2702 VisitForStackValue(args->at(i));
2703 }
2704
2705 // Call the construct call builtin that handles allocation and
2706 // constructor invocation.
2707 SetConstructCallPosition(expr);
2708
2709 // Load new target into rdx.
2710 VisitForAccumulatorValue(super_call_ref->new_target_var());
2711 __ movp(rdx, result_register());
2712
2713 // Load function and argument count into rdi and rax.
2714 __ Set(rax, arg_count);
2715 __ movp(rdi, Operand(rsp, arg_count * kPointerSize));
2716
2717 __ Call(isolate()->builtins()->Construct(), RelocInfo::CODE_TARGET);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002718 OperandStackDepthDecrement(arg_count + 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002719
2720 RecordJSReturnSite(expr);
2721
2722 // Restore context register.
2723 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
2724
2725 context()->Plug(rax);
2726}
2727
2728
2729void FullCodeGenerator::EmitIsSmi(CallRuntime* expr) {
2730 ZoneList<Expression*>* args = expr->arguments();
2731 DCHECK(args->length() == 1);
2732
2733 VisitForAccumulatorValue(args->at(0));
2734
2735 Label materialize_true, materialize_false;
2736 Label* if_true = NULL;
2737 Label* if_false = NULL;
2738 Label* fall_through = NULL;
2739 context()->PrepareTest(&materialize_true, &materialize_false,
2740 &if_true, &if_false, &fall_through);
2741
2742 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
2743 __ JumpIfSmi(rax, if_true);
2744 __ jmp(if_false);
2745
2746 context()->Plug(if_true, if_false);
2747}
2748
2749
2750void FullCodeGenerator::EmitIsJSReceiver(CallRuntime* expr) {
2751 ZoneList<Expression*>* args = expr->arguments();
2752 DCHECK(args->length() == 1);
2753
2754 VisitForAccumulatorValue(args->at(0));
2755
2756 Label materialize_true, materialize_false;
2757 Label* if_true = NULL;
2758 Label* if_false = NULL;
2759 Label* fall_through = NULL;
2760 context()->PrepareTest(&materialize_true, &materialize_false,
2761 &if_true, &if_false, &fall_through);
2762
2763 __ JumpIfSmi(rax, if_false);
2764 __ CmpObjectType(rax, FIRST_JS_RECEIVER_TYPE, rbx);
2765 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
2766 Split(above_equal, if_true, if_false, fall_through);
2767
2768 context()->Plug(if_true, if_false);
2769}
2770
2771
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002772void FullCodeGenerator::EmitIsArray(CallRuntime* expr) {
2773 ZoneList<Expression*>* args = expr->arguments();
2774 DCHECK(args->length() == 1);
2775
2776 VisitForAccumulatorValue(args->at(0));
2777
2778 Label materialize_true, materialize_false;
2779 Label* if_true = NULL;
2780 Label* if_false = NULL;
2781 Label* fall_through = NULL;
2782 context()->PrepareTest(&materialize_true, &materialize_false,
2783 &if_true, &if_false, &fall_through);
2784
2785 __ JumpIfSmi(rax, if_false);
2786 __ CmpObjectType(rax, JS_ARRAY_TYPE, rbx);
2787 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
2788 Split(equal, if_true, if_false, fall_through);
2789
2790 context()->Plug(if_true, if_false);
2791}
2792
2793
2794void FullCodeGenerator::EmitIsTypedArray(CallRuntime* expr) {
2795 ZoneList<Expression*>* args = expr->arguments();
2796 DCHECK(args->length() == 1);
2797
2798 VisitForAccumulatorValue(args->at(0));
2799
2800 Label materialize_true, materialize_false;
2801 Label* if_true = NULL;
2802 Label* if_false = NULL;
2803 Label* fall_through = NULL;
2804 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
2805 &if_false, &fall_through);
2806
2807 __ JumpIfSmi(rax, if_false);
2808 __ CmpObjectType(rax, JS_TYPED_ARRAY_TYPE, rbx);
2809 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
2810 Split(equal, if_true, if_false, fall_through);
2811
2812 context()->Plug(if_true, if_false);
2813}
2814
2815
2816void FullCodeGenerator::EmitIsRegExp(CallRuntime* expr) {
2817 ZoneList<Expression*>* args = expr->arguments();
2818 DCHECK(args->length() == 1);
2819
2820 VisitForAccumulatorValue(args->at(0));
2821
2822 Label materialize_true, materialize_false;
2823 Label* if_true = NULL;
2824 Label* if_false = NULL;
2825 Label* fall_through = NULL;
2826 context()->PrepareTest(&materialize_true, &materialize_false,
2827 &if_true, &if_false, &fall_through);
2828
2829 __ JumpIfSmi(rax, if_false);
2830 __ CmpObjectType(rax, JS_REGEXP_TYPE, rbx);
2831 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
2832 Split(equal, if_true, if_false, fall_through);
2833
2834 context()->Plug(if_true, if_false);
2835}
2836
2837
2838void FullCodeGenerator::EmitIsJSProxy(CallRuntime* expr) {
2839 ZoneList<Expression*>* args = expr->arguments();
2840 DCHECK(args->length() == 1);
2841
2842 VisitForAccumulatorValue(args->at(0));
2843
2844 Label materialize_true, materialize_false;
2845 Label* if_true = NULL;
2846 Label* if_false = NULL;
2847 Label* fall_through = NULL;
2848 context()->PrepareTest(&materialize_true, &materialize_false, &if_true,
2849 &if_false, &fall_through);
2850
2851
2852 __ JumpIfSmi(rax, if_false);
2853 __ CmpObjectType(rax, JS_PROXY_TYPE, rbx);
2854 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
2855 Split(equal, if_true, if_false, fall_through);
2856
2857 context()->Plug(if_true, if_false);
2858}
2859
2860
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002861void FullCodeGenerator::EmitClassOf(CallRuntime* expr) {
2862 ZoneList<Expression*>* args = expr->arguments();
2863 DCHECK(args->length() == 1);
2864 Label done, null, function, non_function_constructor;
2865
2866 VisitForAccumulatorValue(args->at(0));
2867
2868 // If the object is not a JSReceiver, we return null.
2869 __ JumpIfSmi(rax, &null, Label::kNear);
2870 STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
2871 __ CmpObjectType(rax, FIRST_JS_RECEIVER_TYPE, rax);
2872 __ j(below, &null, Label::kNear);
2873
Ben Murdochda12d292016-06-02 14:46:10 +01002874 // Return 'Function' for JSFunction and JSBoundFunction objects.
2875 __ CmpInstanceType(rax, FIRST_FUNCTION_TYPE);
2876 STATIC_ASSERT(LAST_FUNCTION_TYPE == LAST_TYPE);
2877 __ j(above_equal, &function, Label::kNear);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002878
2879 // Check if the constructor in the map is a JS function.
2880 __ GetMapConstructor(rax, rax, rbx);
2881 __ CmpInstanceType(rbx, JS_FUNCTION_TYPE);
2882 __ j(not_equal, &non_function_constructor, Label::kNear);
2883
2884 // rax now contains the constructor function. Grab the
2885 // instance class name from there.
2886 __ movp(rax, FieldOperand(rax, JSFunction::kSharedFunctionInfoOffset));
2887 __ movp(rax, FieldOperand(rax, SharedFunctionInfo::kInstanceClassNameOffset));
2888 __ jmp(&done, Label::kNear);
2889
2890 // Non-JS objects have class null.
2891 __ bind(&null);
2892 __ LoadRoot(rax, Heap::kNullValueRootIndex);
2893 __ jmp(&done, Label::kNear);
2894
2895 // Functions have class 'Function'.
2896 __ bind(&function);
2897 __ LoadRoot(rax, Heap::kFunction_stringRootIndex);
2898 __ jmp(&done, Label::kNear);
2899
2900 // Objects with a non-function constructor have class 'Object'.
2901 __ bind(&non_function_constructor);
2902 __ LoadRoot(rax, Heap::kObject_stringRootIndex);
2903
2904 // All done.
2905 __ bind(&done);
2906
2907 context()->Plug(rax);
2908}
2909
2910
2911void FullCodeGenerator::EmitValueOf(CallRuntime* expr) {
2912 ZoneList<Expression*>* args = expr->arguments();
2913 DCHECK(args->length() == 1);
2914
2915 VisitForAccumulatorValue(args->at(0)); // Load the object.
2916
2917 Label done;
2918 // If the object is a smi return the object.
2919 __ JumpIfSmi(rax, &done);
2920 // If the object is not a value type, return the object.
2921 __ CmpObjectType(rax, JS_VALUE_TYPE, rbx);
2922 __ j(not_equal, &done);
2923 __ movp(rax, FieldOperand(rax, JSValue::kValueOffset));
2924
2925 __ bind(&done);
2926 context()->Plug(rax);
2927}
2928
2929
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002930void FullCodeGenerator::EmitOneByteSeqStringSetChar(CallRuntime* expr) {
2931 ZoneList<Expression*>* args = expr->arguments();
2932 DCHECK_EQ(3, args->length());
2933
2934 Register string = rax;
2935 Register index = rbx;
2936 Register value = rcx;
2937
2938 VisitForStackValue(args->at(0)); // index
2939 VisitForStackValue(args->at(1)); // value
2940 VisitForAccumulatorValue(args->at(2)); // string
Ben Murdoch097c5b22016-05-18 11:27:45 +01002941 PopOperand(value);
2942 PopOperand(index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002943
2944 if (FLAG_debug_code) {
2945 __ Check(__ CheckSmi(value), kNonSmiValue);
2946 __ Check(__ CheckSmi(index), kNonSmiValue);
2947 }
2948
2949 __ SmiToInteger32(value, value);
2950 __ SmiToInteger32(index, index);
2951
2952 if (FLAG_debug_code) {
2953 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
2954 __ EmitSeqStringSetCharCheck(string, index, value, one_byte_seq_type);
2955 }
2956
2957 __ movb(FieldOperand(string, index, times_1, SeqOneByteString::kHeaderSize),
2958 value);
2959 context()->Plug(string);
2960}
2961
2962
2963void FullCodeGenerator::EmitTwoByteSeqStringSetChar(CallRuntime* expr) {
2964 ZoneList<Expression*>* args = expr->arguments();
2965 DCHECK_EQ(3, args->length());
2966
2967 Register string = rax;
2968 Register index = rbx;
2969 Register value = rcx;
2970
2971 VisitForStackValue(args->at(0)); // index
2972 VisitForStackValue(args->at(1)); // value
2973 VisitForAccumulatorValue(args->at(2)); // string
Ben Murdoch097c5b22016-05-18 11:27:45 +01002974 PopOperand(value);
2975 PopOperand(index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002976
2977 if (FLAG_debug_code) {
2978 __ Check(__ CheckSmi(value), kNonSmiValue);
2979 __ Check(__ CheckSmi(index), kNonSmiValue);
2980 }
2981
2982 __ SmiToInteger32(value, value);
2983 __ SmiToInteger32(index, index);
2984
2985 if (FLAG_debug_code) {
2986 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
2987 __ EmitSeqStringSetCharCheck(string, index, value, two_byte_seq_type);
2988 }
2989
2990 __ movw(FieldOperand(string, index, times_2, SeqTwoByteString::kHeaderSize),
2991 value);
2992 context()->Plug(rax);
2993}
2994
2995
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002996void FullCodeGenerator::EmitStringCharFromCode(CallRuntime* expr) {
2997 ZoneList<Expression*>* args = expr->arguments();
2998 DCHECK(args->length() == 1);
2999
3000 VisitForAccumulatorValue(args->at(0));
3001
3002 Label done;
3003 StringCharFromCodeGenerator generator(rax, rbx);
3004 generator.GenerateFast(masm_);
3005 __ jmp(&done);
3006
3007 NopRuntimeCallHelper call_helper;
3008 generator.GenerateSlow(masm_, call_helper);
3009
3010 __ bind(&done);
3011 context()->Plug(rbx);
3012}
3013
3014
3015void FullCodeGenerator::EmitStringCharCodeAt(CallRuntime* expr) {
3016 ZoneList<Expression*>* args = expr->arguments();
3017 DCHECK(args->length() == 2);
3018
3019 VisitForStackValue(args->at(0));
3020 VisitForAccumulatorValue(args->at(1));
3021
3022 Register object = rbx;
3023 Register index = rax;
3024 Register result = rdx;
3025
Ben Murdoch097c5b22016-05-18 11:27:45 +01003026 PopOperand(object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003027
3028 Label need_conversion;
3029 Label index_out_of_range;
3030 Label done;
3031 StringCharCodeAtGenerator generator(object,
3032 index,
3033 result,
3034 &need_conversion,
3035 &need_conversion,
3036 &index_out_of_range,
3037 STRING_INDEX_IS_NUMBER);
3038 generator.GenerateFast(masm_);
3039 __ jmp(&done);
3040
3041 __ bind(&index_out_of_range);
3042 // When the index is out of range, the spec requires us to return
3043 // NaN.
3044 __ LoadRoot(result, Heap::kNanValueRootIndex);
3045 __ jmp(&done);
3046
3047 __ bind(&need_conversion);
3048 // Move the undefined value into the result register, which will
3049 // trigger conversion.
3050 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
3051 __ jmp(&done);
3052
3053 NopRuntimeCallHelper call_helper;
3054 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
3055
3056 __ bind(&done);
3057 context()->Plug(result);
3058}
3059
3060
3061void FullCodeGenerator::EmitStringCharAt(CallRuntime* expr) {
3062 ZoneList<Expression*>* args = expr->arguments();
3063 DCHECK(args->length() == 2);
3064
3065 VisitForStackValue(args->at(0));
3066 VisitForAccumulatorValue(args->at(1));
3067
3068 Register object = rbx;
3069 Register index = rax;
3070 Register scratch = rdx;
3071 Register result = rax;
3072
Ben Murdoch097c5b22016-05-18 11:27:45 +01003073 PopOperand(object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003074
3075 Label need_conversion;
3076 Label index_out_of_range;
3077 Label done;
3078 StringCharAtGenerator generator(object,
3079 index,
3080 scratch,
3081 result,
3082 &need_conversion,
3083 &need_conversion,
3084 &index_out_of_range,
3085 STRING_INDEX_IS_NUMBER);
3086 generator.GenerateFast(masm_);
3087 __ jmp(&done);
3088
3089 __ bind(&index_out_of_range);
3090 // When the index is out of range, the spec requires us to return
3091 // the empty string.
3092 __ LoadRoot(result, Heap::kempty_stringRootIndex);
3093 __ jmp(&done);
3094
3095 __ bind(&need_conversion);
3096 // Move smi zero into the result register, which will trigger
3097 // conversion.
3098 __ Move(result, Smi::FromInt(0));
3099 __ jmp(&done);
3100
3101 NopRuntimeCallHelper call_helper;
3102 generator.GenerateSlow(masm_, NOT_PART_OF_IC_HANDLER, call_helper);
3103
3104 __ bind(&done);
3105 context()->Plug(result);
3106}
3107
3108
3109void FullCodeGenerator::EmitCall(CallRuntime* expr) {
3110 ZoneList<Expression*>* args = expr->arguments();
3111 DCHECK_LE(2, args->length());
3112 // Push target, receiver and arguments onto the stack.
3113 for (Expression* const arg : *args) {
3114 VisitForStackValue(arg);
3115 }
3116 PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
3117 // Move target to rdi.
3118 int const argc = args->length() - 2;
3119 __ movp(rdi, Operand(rsp, (argc + 1) * kPointerSize));
3120 // Call the target.
3121 __ Set(rax, argc);
3122 __ Call(isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003123 OperandStackDepthDecrement(argc + 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003124 // Restore context register.
3125 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
3126 // Discard the function left on TOS.
3127 context()->DropAndPlug(1, rax);
3128}
3129
3130
3131void FullCodeGenerator::EmitHasCachedArrayIndex(CallRuntime* expr) {
3132 ZoneList<Expression*>* args = expr->arguments();
3133 DCHECK(args->length() == 1);
3134
3135 VisitForAccumulatorValue(args->at(0));
3136
3137 Label materialize_true, materialize_false;
3138 Label* if_true = NULL;
3139 Label* if_false = NULL;
3140 Label* fall_through = NULL;
3141 context()->PrepareTest(&materialize_true, &materialize_false,
3142 &if_true, &if_false, &fall_through);
3143
3144 __ testl(FieldOperand(rax, String::kHashFieldOffset),
3145 Immediate(String::kContainsCachedArrayIndexMask));
3146 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3147 __ j(zero, if_true);
3148 __ jmp(if_false);
3149
3150 context()->Plug(if_true, if_false);
3151}
3152
3153
3154void FullCodeGenerator::EmitGetCachedArrayIndex(CallRuntime* expr) {
3155 ZoneList<Expression*>* args = expr->arguments();
3156 DCHECK(args->length() == 1);
3157 VisitForAccumulatorValue(args->at(0));
3158
3159 __ AssertString(rax);
3160
3161 __ movl(rax, FieldOperand(rax, String::kHashFieldOffset));
3162 DCHECK(String::kHashShift >= kSmiTagSize);
3163 __ IndexFromHash(rax, rax);
3164
3165 context()->Plug(rax);
3166}
3167
3168
3169void FullCodeGenerator::EmitGetSuperConstructor(CallRuntime* expr) {
3170 ZoneList<Expression*>* args = expr->arguments();
3171 DCHECK_EQ(1, args->length());
3172 VisitForAccumulatorValue(args->at(0));
3173 __ AssertFunction(rax);
3174 __ movp(rax, FieldOperand(rax, HeapObject::kMapOffset));
3175 __ movp(rax, FieldOperand(rax, Map::kPrototypeOffset));
3176 context()->Plug(rax);
3177}
3178
Ben Murdochda12d292016-06-02 14:46:10 +01003179void FullCodeGenerator::EmitGetOrdinaryHasInstance(CallRuntime* expr) {
3180 DCHECK_EQ(0, expr->arguments()->length());
3181 __ LoadNativeContextSlot(Context::ORDINARY_HAS_INSTANCE_INDEX, rax);
3182 context()->Plug(rax);
3183}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003184
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003185void FullCodeGenerator::EmitDebugIsActive(CallRuntime* expr) {
3186 DCHECK(expr->arguments()->length() == 0);
3187 ExternalReference debug_is_active =
3188 ExternalReference::debug_is_active_address(isolate());
3189 __ Move(kScratchRegister, debug_is_active);
3190 __ movzxbp(rax, Operand(kScratchRegister, 0));
3191 __ Integer32ToSmi(rax, rax);
3192 context()->Plug(rax);
3193}
3194
3195
3196void FullCodeGenerator::EmitCreateIterResultObject(CallRuntime* expr) {
3197 ZoneList<Expression*>* args = expr->arguments();
3198 DCHECK_EQ(2, args->length());
3199 VisitForStackValue(args->at(0));
3200 VisitForStackValue(args->at(1));
3201
3202 Label runtime, done;
3203
3204 __ Allocate(JSIteratorResult::kSize, rax, rcx, rdx, &runtime, TAG_OBJECT);
3205 __ LoadNativeContextSlot(Context::ITERATOR_RESULT_MAP_INDEX, rbx);
3206 __ movp(FieldOperand(rax, HeapObject::kMapOffset), rbx);
3207 __ LoadRoot(rbx, Heap::kEmptyFixedArrayRootIndex);
3208 __ movp(FieldOperand(rax, JSObject::kPropertiesOffset), rbx);
3209 __ movp(FieldOperand(rax, JSObject::kElementsOffset), rbx);
3210 __ Pop(FieldOperand(rax, JSIteratorResult::kDoneOffset));
3211 __ Pop(FieldOperand(rax, JSIteratorResult::kValueOffset));
3212 STATIC_ASSERT(JSIteratorResult::kSize == 5 * kPointerSize);
3213 __ jmp(&done, Label::kNear);
3214
3215 __ bind(&runtime);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003216 CallRuntimeWithOperands(Runtime::kCreateIterResultObject);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003217
3218 __ bind(&done);
3219 context()->Plug(rax);
3220}
3221
3222
3223void FullCodeGenerator::EmitLoadJSRuntimeFunction(CallRuntime* expr) {
Ben Murdochda12d292016-06-02 14:46:10 +01003224 // Push function.
3225 __ LoadNativeContextSlot(expr->context_index(), rax);
3226 PushOperand(rax);
3227
3228 // Push undefined as receiver.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003229 OperandStackDepthIncrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003230 __ PushRoot(Heap::kUndefinedValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003231}
3232
3233
3234void FullCodeGenerator::EmitCallJSRuntimeFunction(CallRuntime* expr) {
3235 ZoneList<Expression*>* args = expr->arguments();
3236 int arg_count = args->length();
3237
3238 SetCallPosition(expr);
3239 __ movp(rdi, Operand(rsp, (arg_count + 1) * kPointerSize));
3240 __ Set(rax, arg_count);
3241 __ Call(isolate()->builtins()->Call(ConvertReceiverMode::kNullOrUndefined),
3242 RelocInfo::CODE_TARGET);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003243 OperandStackDepthDecrement(arg_count + 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003244
Ben Murdochda12d292016-06-02 14:46:10 +01003245 // Restore context register.
3246 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003247}
3248
3249
3250void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
3251 switch (expr->op()) {
3252 case Token::DELETE: {
3253 Comment cmnt(masm_, "[ UnaryOperation (DELETE)");
3254 Property* property = expr->expression()->AsProperty();
3255 VariableProxy* proxy = expr->expression()->AsVariableProxy();
3256
3257 if (property != NULL) {
3258 VisitForStackValue(property->obj());
3259 VisitForStackValue(property->key());
Ben Murdoch097c5b22016-05-18 11:27:45 +01003260 CallRuntimeWithOperands(is_strict(language_mode())
3261 ? Runtime::kDeleteProperty_Strict
3262 : Runtime::kDeleteProperty_Sloppy);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003263 context()->Plug(rax);
3264 } else if (proxy != NULL) {
3265 Variable* var = proxy->var();
3266 // Delete of an unqualified identifier is disallowed in strict mode but
3267 // "delete this" is allowed.
3268 bool is_this = var->HasThisName(isolate());
3269 DCHECK(is_sloppy(language_mode()) || is_this);
3270 if (var->IsUnallocatedOrGlobalSlot()) {
3271 __ movp(rax, NativeContextOperand());
3272 __ Push(ContextOperand(rax, Context::EXTENSION_INDEX));
3273 __ Push(var->name());
3274 __ CallRuntime(Runtime::kDeleteProperty_Sloppy);
3275 context()->Plug(rax);
3276 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
3277 // Result of deleting non-global variables is false. 'this' is
3278 // not really a variable, though we implement it as one. The
3279 // subexpression does not have side effects.
3280 context()->Plug(is_this);
3281 } else {
3282 // Non-global variable. Call the runtime to try to delete from the
3283 // context where the variable was introduced.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003284 __ Push(var->name());
3285 __ CallRuntime(Runtime::kDeleteLookupSlot);
3286 context()->Plug(rax);
3287 }
3288 } else {
3289 // Result of deleting non-property, non-variable reference is true.
3290 // The subexpression may have side effects.
3291 VisitForEffect(expr->expression());
3292 context()->Plug(true);
3293 }
3294 break;
3295 }
3296
3297 case Token::VOID: {
3298 Comment cmnt(masm_, "[ UnaryOperation (VOID)");
3299 VisitForEffect(expr->expression());
3300 context()->Plug(Heap::kUndefinedValueRootIndex);
3301 break;
3302 }
3303
3304 case Token::NOT: {
3305 Comment cmnt(masm_, "[ UnaryOperation (NOT)");
3306 if (context()->IsEffect()) {
3307 // Unary NOT has no side effects so it's only necessary to visit the
3308 // subexpression. Match the optimizing compiler by not branching.
3309 VisitForEffect(expr->expression());
3310 } else if (context()->IsTest()) {
3311 const TestContext* test = TestContext::cast(context());
3312 // The labels are swapped for the recursive call.
3313 VisitForControl(expr->expression(),
3314 test->false_label(),
3315 test->true_label(),
3316 test->fall_through());
3317 context()->Plug(test->true_label(), test->false_label());
3318 } else {
3319 // We handle value contexts explicitly rather than simply visiting
3320 // for control and plugging the control flow into the context,
3321 // because we need to prepare a pair of extra administrative AST ids
3322 // for the optimizing compiler.
3323 DCHECK(context()->IsAccumulatorValue() || context()->IsStackValue());
3324 Label materialize_true, materialize_false, done;
3325 VisitForControl(expr->expression(),
3326 &materialize_false,
3327 &materialize_true,
3328 &materialize_true);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003329 if (!context()->IsAccumulatorValue()) OperandStackDepthIncrement(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003330 __ bind(&materialize_true);
3331 PrepareForBailoutForId(expr->MaterializeTrueId(), NO_REGISTERS);
3332 if (context()->IsAccumulatorValue()) {
3333 __ LoadRoot(rax, Heap::kTrueValueRootIndex);
3334 } else {
3335 __ PushRoot(Heap::kTrueValueRootIndex);
3336 }
3337 __ jmp(&done, Label::kNear);
3338 __ bind(&materialize_false);
3339 PrepareForBailoutForId(expr->MaterializeFalseId(), NO_REGISTERS);
3340 if (context()->IsAccumulatorValue()) {
3341 __ LoadRoot(rax, Heap::kFalseValueRootIndex);
3342 } else {
3343 __ PushRoot(Heap::kFalseValueRootIndex);
3344 }
3345 __ bind(&done);
3346 }
3347 break;
3348 }
3349
3350 case Token::TYPEOF: {
3351 Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
3352 {
3353 AccumulatorValueContext context(this);
3354 VisitForTypeofValue(expr->expression());
3355 }
3356 __ movp(rbx, rax);
3357 TypeofStub typeof_stub(isolate());
3358 __ CallStub(&typeof_stub);
3359 context()->Plug(rax);
3360 break;
3361 }
3362
3363 default:
3364 UNREACHABLE();
3365 }
3366}
3367
3368
3369void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
3370 DCHECK(expr->expression()->IsValidReferenceExpressionOrThis());
3371
3372 Comment cmnt(masm_, "[ CountOperation");
3373
3374 Property* prop = expr->expression()->AsProperty();
3375 LhsKind assign_type = Property::GetAssignType(prop);
3376
3377 // Evaluate expression and get value.
3378 if (assign_type == VARIABLE) {
3379 DCHECK(expr->expression()->AsVariableProxy()->var() != NULL);
3380 AccumulatorValueContext context(this);
3381 EmitVariableLoad(expr->expression()->AsVariableProxy());
3382 } else {
3383 // Reserve space for result of postfix operation.
3384 if (expr->is_postfix() && !context()->IsEffect()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003385 PushOperand(Smi::FromInt(0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003386 }
3387 switch (assign_type) {
3388 case NAMED_PROPERTY: {
3389 VisitForStackValue(prop->obj());
3390 __ movp(LoadDescriptor::ReceiverRegister(), Operand(rsp, 0));
3391 EmitNamedPropertyLoad(prop);
3392 break;
3393 }
3394
3395 case NAMED_SUPER_PROPERTY: {
3396 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
3397 VisitForAccumulatorValue(
3398 prop->obj()->AsSuperPropertyReference()->home_object());
Ben Murdoch097c5b22016-05-18 11:27:45 +01003399 PushOperand(result_register());
3400 PushOperand(MemOperand(rsp, kPointerSize));
3401 PushOperand(result_register());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003402 EmitNamedSuperPropertyLoad(prop);
3403 break;
3404 }
3405
3406 case KEYED_SUPER_PROPERTY: {
3407 VisitForStackValue(prop->obj()->AsSuperPropertyReference()->this_var());
3408 VisitForStackValue(
3409 prop->obj()->AsSuperPropertyReference()->home_object());
3410 VisitForAccumulatorValue(prop->key());
Ben Murdoch097c5b22016-05-18 11:27:45 +01003411 PushOperand(result_register());
3412 PushOperand(MemOperand(rsp, 2 * kPointerSize));
3413 PushOperand(MemOperand(rsp, 2 * kPointerSize));
3414 PushOperand(result_register());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003415 EmitKeyedSuperPropertyLoad(prop);
3416 break;
3417 }
3418
3419 case KEYED_PROPERTY: {
3420 VisitForStackValue(prop->obj());
3421 VisitForStackValue(prop->key());
3422 // Leave receiver on stack
3423 __ movp(LoadDescriptor::ReceiverRegister(), Operand(rsp, kPointerSize));
3424 // Copy of key, needed for later store.
3425 __ movp(LoadDescriptor::NameRegister(), Operand(rsp, 0));
3426 EmitKeyedPropertyLoad(prop);
3427 break;
3428 }
3429
3430 case VARIABLE:
3431 UNREACHABLE();
3432 }
3433 }
3434
3435 // We need a second deoptimization point after loading the value
3436 // in case evaluating the property load my have a side effect.
3437 if (assign_type == VARIABLE) {
3438 PrepareForBailout(expr->expression(), TOS_REG);
3439 } else {
3440 PrepareForBailoutForId(prop->LoadId(), TOS_REG);
3441 }
3442
3443 // Inline smi case if we are in a loop.
3444 Label done, stub_call;
3445 JumpPatchSite patch_site(masm_);
3446 if (ShouldInlineSmiCase(expr->op())) {
3447 Label slow;
3448 patch_site.EmitJumpIfNotSmi(rax, &slow, Label::kNear);
3449
3450 // Save result for postfix expressions.
3451 if (expr->is_postfix()) {
3452 if (!context()->IsEffect()) {
3453 // Save the result on the stack. If we have a named or keyed property
3454 // we store the result under the receiver that is currently on top
3455 // of the stack.
3456 switch (assign_type) {
3457 case VARIABLE:
3458 __ Push(rax);
3459 break;
3460 case NAMED_PROPERTY:
3461 __ movp(Operand(rsp, kPointerSize), rax);
3462 break;
3463 case NAMED_SUPER_PROPERTY:
3464 __ movp(Operand(rsp, 2 * kPointerSize), rax);
3465 break;
3466 case KEYED_PROPERTY:
3467 __ movp(Operand(rsp, 2 * kPointerSize), rax);
3468 break;
3469 case KEYED_SUPER_PROPERTY:
3470 __ movp(Operand(rsp, 3 * kPointerSize), rax);
3471 break;
3472 }
3473 }
3474 }
3475
3476 SmiOperationConstraints constraints =
3477 SmiOperationConstraint::kPreserveSourceRegister |
3478 SmiOperationConstraint::kBailoutOnNoOverflow;
3479 if (expr->op() == Token::INC) {
3480 __ SmiAddConstant(rax, rax, Smi::FromInt(1), constraints, &done,
3481 Label::kNear);
3482 } else {
3483 __ SmiSubConstant(rax, rax, Smi::FromInt(1), constraints, &done,
3484 Label::kNear);
3485 }
3486 __ jmp(&stub_call, Label::kNear);
3487 __ bind(&slow);
3488 }
Ben Murdochda12d292016-06-02 14:46:10 +01003489
3490 // Convert old value into a number.
3491 ToNumberStub convert_stub(isolate());
3492 __ CallStub(&convert_stub);
3493 PrepareForBailoutForId(expr->ToNumberId(), TOS_REG);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003494
3495 // Save result for postfix expressions.
3496 if (expr->is_postfix()) {
3497 if (!context()->IsEffect()) {
3498 // Save the result on the stack. If we have a named or keyed property
3499 // we store the result under the receiver that is currently on top
3500 // of the stack.
3501 switch (assign_type) {
3502 case VARIABLE:
Ben Murdoch097c5b22016-05-18 11:27:45 +01003503 PushOperand(rax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003504 break;
3505 case NAMED_PROPERTY:
3506 __ movp(Operand(rsp, kPointerSize), rax);
3507 break;
3508 case NAMED_SUPER_PROPERTY:
3509 __ movp(Operand(rsp, 2 * kPointerSize), rax);
3510 break;
3511 case KEYED_PROPERTY:
3512 __ movp(Operand(rsp, 2 * kPointerSize), rax);
3513 break;
3514 case KEYED_SUPER_PROPERTY:
3515 __ movp(Operand(rsp, 3 * kPointerSize), rax);
3516 break;
3517 }
3518 }
3519 }
3520
3521 SetExpressionPosition(expr);
3522
3523 // Call stub for +1/-1.
3524 __ bind(&stub_call);
3525 __ movp(rdx, rax);
3526 __ Move(rax, Smi::FromInt(1));
Ben Murdoch097c5b22016-05-18 11:27:45 +01003527 Handle<Code> code =
3528 CodeFactory::BinaryOpIC(isolate(), expr->binary_op()).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003529 CallIC(code, expr->CountBinOpFeedbackId());
3530 patch_site.EmitPatchInfo();
3531 __ bind(&done);
3532
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003533 // Store the value returned in rax.
3534 switch (assign_type) {
3535 case VARIABLE:
3536 if (expr->is_postfix()) {
3537 // Perform the assignment as if via '='.
3538 { EffectContext context(this);
3539 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3540 Token::ASSIGN, expr->CountSlot());
3541 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3542 context.Plug(rax);
3543 }
3544 // For all contexts except kEffect: We have the result on
3545 // top of the stack.
3546 if (!context()->IsEffect()) {
3547 context()->PlugTOS();
3548 }
3549 } else {
3550 // Perform the assignment as if via '='.
3551 EmitVariableAssignment(expr->expression()->AsVariableProxy()->var(),
3552 Token::ASSIGN, expr->CountSlot());
3553 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3554 context()->Plug(rax);
3555 }
3556 break;
3557 case NAMED_PROPERTY: {
3558 __ Move(StoreDescriptor::NameRegister(),
3559 prop->key()->AsLiteral()->value());
Ben Murdoch097c5b22016-05-18 11:27:45 +01003560 PopOperand(StoreDescriptor::ReceiverRegister());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003561 EmitLoadStoreICSlot(expr->CountSlot());
3562 CallStoreIC();
3563 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3564 if (expr->is_postfix()) {
3565 if (!context()->IsEffect()) {
3566 context()->PlugTOS();
3567 }
3568 } else {
3569 context()->Plug(rax);
3570 }
3571 break;
3572 }
3573 case NAMED_SUPER_PROPERTY: {
3574 EmitNamedSuperPropertyStore(prop);
3575 if (expr->is_postfix()) {
3576 if (!context()->IsEffect()) {
3577 context()->PlugTOS();
3578 }
3579 } else {
3580 context()->Plug(rax);
3581 }
3582 break;
3583 }
3584 case KEYED_SUPER_PROPERTY: {
3585 EmitKeyedSuperPropertyStore(prop);
3586 if (expr->is_postfix()) {
3587 if (!context()->IsEffect()) {
3588 context()->PlugTOS();
3589 }
3590 } else {
3591 context()->Plug(rax);
3592 }
3593 break;
3594 }
3595 case KEYED_PROPERTY: {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003596 PopOperand(StoreDescriptor::NameRegister());
3597 PopOperand(StoreDescriptor::ReceiverRegister());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003598 Handle<Code> ic =
3599 CodeFactory::KeyedStoreIC(isolate(), language_mode()).code();
3600 EmitLoadStoreICSlot(expr->CountSlot());
3601 CallIC(ic);
3602 PrepareForBailoutForId(expr->AssignmentId(), TOS_REG);
3603 if (expr->is_postfix()) {
3604 if (!context()->IsEffect()) {
3605 context()->PlugTOS();
3606 }
3607 } else {
3608 context()->Plug(rax);
3609 }
3610 break;
3611 }
3612 }
3613}
3614
3615
3616void FullCodeGenerator::EmitLiteralCompareTypeof(Expression* expr,
3617 Expression* sub_expr,
3618 Handle<String> check) {
3619 Label materialize_true, materialize_false;
3620 Label* if_true = NULL;
3621 Label* if_false = NULL;
3622 Label* fall_through = NULL;
3623 context()->PrepareTest(&materialize_true, &materialize_false,
3624 &if_true, &if_false, &fall_through);
3625
3626 { AccumulatorValueContext context(this);
3627 VisitForTypeofValue(sub_expr);
3628 }
3629 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3630
3631 Factory* factory = isolate()->factory();
3632 if (String::Equals(check, factory->number_string())) {
3633 __ JumpIfSmi(rax, if_true);
3634 __ movp(rax, FieldOperand(rax, HeapObject::kMapOffset));
3635 __ CompareRoot(rax, Heap::kHeapNumberMapRootIndex);
3636 Split(equal, if_true, if_false, fall_through);
3637 } else if (String::Equals(check, factory->string_string())) {
3638 __ JumpIfSmi(rax, if_false);
3639 __ CmpObjectType(rax, FIRST_NONSTRING_TYPE, rdx);
3640 Split(below, if_true, if_false, fall_through);
3641 } else if (String::Equals(check, factory->symbol_string())) {
3642 __ JumpIfSmi(rax, if_false);
3643 __ CmpObjectType(rax, SYMBOL_TYPE, rdx);
3644 Split(equal, if_true, if_false, fall_through);
3645 } else if (String::Equals(check, factory->boolean_string())) {
3646 __ CompareRoot(rax, Heap::kTrueValueRootIndex);
3647 __ j(equal, if_true);
3648 __ CompareRoot(rax, Heap::kFalseValueRootIndex);
3649 Split(equal, if_true, if_false, fall_through);
3650 } else if (String::Equals(check, factory->undefined_string())) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003651 __ CompareRoot(rax, Heap::kNullValueRootIndex);
3652 __ j(equal, if_false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003653 __ JumpIfSmi(rax, if_false);
3654 // Check for undetectable objects => true.
3655 __ movp(rdx, FieldOperand(rax, HeapObject::kMapOffset));
3656 __ testb(FieldOperand(rdx, Map::kBitFieldOffset),
3657 Immediate(1 << Map::kIsUndetectable));
3658 Split(not_zero, if_true, if_false, fall_through);
3659 } else if (String::Equals(check, factory->function_string())) {
3660 __ JumpIfSmi(rax, if_false);
3661 // Check for callable and not undetectable objects => true.
3662 __ movp(rdx, FieldOperand(rax, HeapObject::kMapOffset));
3663 __ movzxbl(rdx, FieldOperand(rdx, Map::kBitFieldOffset));
3664 __ andb(rdx,
3665 Immediate((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable)));
3666 __ cmpb(rdx, Immediate(1 << Map::kIsCallable));
3667 Split(equal, if_true, if_false, fall_through);
3668 } else if (String::Equals(check, factory->object_string())) {
3669 __ JumpIfSmi(rax, if_false);
3670 __ CompareRoot(rax, Heap::kNullValueRootIndex);
3671 __ j(equal, if_true);
3672 STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
3673 __ CmpObjectType(rax, FIRST_JS_RECEIVER_TYPE, rdx);
3674 __ j(below, if_false);
3675 // Check for callable or undetectable objects => false.
3676 __ testb(FieldOperand(rdx, Map::kBitFieldOffset),
3677 Immediate((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable)));
3678 Split(zero, if_true, if_false, fall_through);
3679// clang-format off
3680#define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
3681 } else if (String::Equals(check, factory->type##_string())) { \
3682 __ JumpIfSmi(rax, if_false); \
3683 __ movp(rax, FieldOperand(rax, HeapObject::kMapOffset)); \
3684 __ CompareRoot(rax, Heap::k##Type##MapRootIndex); \
3685 Split(equal, if_true, if_false, fall_through);
3686 SIMD128_TYPES(SIMD128_TYPE)
3687#undef SIMD128_TYPE
3688 // clang-format on
3689 } else {
3690 if (if_false != fall_through) __ jmp(if_false);
3691 }
3692 context()->Plug(if_true, if_false);
3693}
3694
3695
3696void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
3697 Comment cmnt(masm_, "[ CompareOperation");
3698 SetExpressionPosition(expr);
3699
3700 // First we try a fast inlined version of the compare when one of
3701 // the operands is a literal.
3702 if (TryLiteralCompare(expr)) return;
3703
3704 // Always perform the comparison for its control flow. Pack the result
3705 // into the expression's context after the comparison is performed.
3706 Label materialize_true, materialize_false;
3707 Label* if_true = NULL;
3708 Label* if_false = NULL;
3709 Label* fall_through = NULL;
3710 context()->PrepareTest(&materialize_true, &materialize_false,
3711 &if_true, &if_false, &fall_through);
3712
3713 Token::Value op = expr->op();
3714 VisitForStackValue(expr->left());
3715 switch (op) {
3716 case Token::IN:
3717 VisitForStackValue(expr->right());
Ben Murdoch097c5b22016-05-18 11:27:45 +01003718 CallRuntimeWithOperands(Runtime::kHasProperty);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003719 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
3720 __ CompareRoot(rax, Heap::kTrueValueRootIndex);
3721 Split(equal, if_true, if_false, fall_through);
3722 break;
3723
3724 case Token::INSTANCEOF: {
3725 VisitForAccumulatorValue(expr->right());
Ben Murdoch097c5b22016-05-18 11:27:45 +01003726 PopOperand(rdx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003727 InstanceOfStub stub(isolate());
3728 __ CallStub(&stub);
3729 PrepareForBailoutBeforeSplit(expr, false, NULL, NULL);
3730 __ CompareRoot(rax, Heap::kTrueValueRootIndex);
3731 Split(equal, if_true, if_false, fall_through);
3732 break;
3733 }
3734
3735 default: {
3736 VisitForAccumulatorValue(expr->right());
3737 Condition cc = CompareIC::ComputeCondition(op);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003738 PopOperand(rdx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003739
3740 bool inline_smi_code = ShouldInlineSmiCase(op);
3741 JumpPatchSite patch_site(masm_);
3742 if (inline_smi_code) {
3743 Label slow_case;
3744 __ movp(rcx, rdx);
3745 __ orp(rcx, rax);
3746 patch_site.EmitJumpIfNotSmi(rcx, &slow_case, Label::kNear);
3747 __ cmpp(rdx, rax);
3748 Split(cc, if_true, if_false, NULL);
3749 __ bind(&slow_case);
3750 }
3751
Ben Murdoch097c5b22016-05-18 11:27:45 +01003752 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003753 CallIC(ic, expr->CompareOperationFeedbackId());
3754 patch_site.EmitPatchInfo();
3755
3756 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3757 __ testp(rax, rax);
3758 Split(cc, if_true, if_false, fall_through);
3759 }
3760 }
3761
3762 // Convert the result of the comparison into one expected for this
3763 // expression's context.
3764 context()->Plug(if_true, if_false);
3765}
3766
3767
3768void FullCodeGenerator::EmitLiteralCompareNil(CompareOperation* expr,
3769 Expression* sub_expr,
3770 NilValue nil) {
3771 Label materialize_true, materialize_false;
3772 Label* if_true = NULL;
3773 Label* if_false = NULL;
3774 Label* fall_through = NULL;
3775 context()->PrepareTest(&materialize_true, &materialize_false,
3776 &if_true, &if_false, &fall_through);
3777
3778 VisitForAccumulatorValue(sub_expr);
3779 PrepareForBailoutBeforeSplit(expr, true, if_true, if_false);
3780 if (expr->op() == Token::EQ_STRICT) {
3781 Heap::RootListIndex nil_value = nil == kNullValue ?
3782 Heap::kNullValueRootIndex :
3783 Heap::kUndefinedValueRootIndex;
3784 __ CompareRoot(rax, nil_value);
3785 Split(equal, if_true, if_false, fall_through);
3786 } else {
Ben Murdochda12d292016-06-02 14:46:10 +01003787 __ JumpIfSmi(rax, if_false);
3788 __ movp(rax, FieldOperand(rax, HeapObject::kMapOffset));
3789 __ testb(FieldOperand(rax, Map::kBitFieldOffset),
3790 Immediate(1 << Map::kIsUndetectable));
3791 Split(not_zero, if_true, if_false, fall_through);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003792 }
3793 context()->Plug(if_true, if_false);
3794}
3795
3796
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003797Register FullCodeGenerator::result_register() {
3798 return rax;
3799}
3800
3801
3802Register FullCodeGenerator::context_register() {
3803 return rsi;
3804}
3805
Ben Murdochda12d292016-06-02 14:46:10 +01003806void FullCodeGenerator::LoadFromFrameField(int frame_offset, Register value) {
3807 DCHECK(IsAligned(frame_offset, kPointerSize));
3808 __ movp(value, Operand(rbp, frame_offset));
3809}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003810
3811void FullCodeGenerator::StoreToFrameField(int frame_offset, Register value) {
3812 DCHECK(IsAligned(frame_offset, kPointerSize));
3813 __ movp(Operand(rbp, frame_offset), value);
3814}
3815
3816
3817void FullCodeGenerator::LoadContextField(Register dst, int context_index) {
3818 __ movp(dst, ContextOperand(rsi, context_index));
3819}
3820
3821
3822void FullCodeGenerator::PushFunctionArgumentForContextAllocation() {
3823 Scope* closure_scope = scope()->ClosureScope();
3824 if (closure_scope->is_script_scope() ||
3825 closure_scope->is_module_scope()) {
3826 // Contexts nested in the native context have a canonical empty function
3827 // as their closure, not the anonymous closure containing the global
3828 // code.
3829 __ movp(rax, NativeContextOperand());
Ben Murdoch097c5b22016-05-18 11:27:45 +01003830 PushOperand(ContextOperand(rax, Context::CLOSURE_INDEX));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003831 } else if (closure_scope->is_eval_scope()) {
3832 // Contexts created by a call to eval have the same closure as the
3833 // context calling eval, not the anonymous closure containing the eval
3834 // code. Fetch it from the context.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003835 PushOperand(ContextOperand(rsi, Context::CLOSURE_INDEX));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003836 } else {
3837 DCHECK(closure_scope->is_function_scope());
Ben Murdoch097c5b22016-05-18 11:27:45 +01003838 PushOperand(Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003839 }
3840}
3841
3842
3843// ----------------------------------------------------------------------------
3844// Non-local control flow support.
3845
3846
3847void FullCodeGenerator::EnterFinallyBlock() {
3848 DCHECK(!result_register().is(rdx));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003849
3850 // Store pending message while executing finally block.
3851 ExternalReference pending_message_obj =
3852 ExternalReference::address_of_pending_message_obj(isolate());
3853 __ Load(rdx, pending_message_obj);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003854 PushOperand(rdx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003855
3856 ClearPendingMessage();
3857}
3858
3859
3860void FullCodeGenerator::ExitFinallyBlock() {
3861 DCHECK(!result_register().is(rdx));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003862 // Restore pending message from stack.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003863 PopOperand(rdx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003864 ExternalReference pending_message_obj =
3865 ExternalReference::address_of_pending_message_obj(isolate());
3866 __ Store(pending_message_obj, rdx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003867}
3868
3869
3870void FullCodeGenerator::ClearPendingMessage() {
3871 DCHECK(!result_register().is(rdx));
3872 ExternalReference pending_message_obj =
3873 ExternalReference::address_of_pending_message_obj(isolate());
3874 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
3875 __ Store(pending_message_obj, rdx);
3876}
3877
3878
Ben Murdoch097c5b22016-05-18 11:27:45 +01003879void FullCodeGenerator::DeferredCommands::EmitCommands() {
3880 __ Pop(result_register()); // Restore the accumulator.
3881 __ Pop(rdx); // Get the token.
3882 for (DeferredCommand cmd : commands_) {
3883 Label skip;
3884 __ SmiCompare(rdx, Smi::FromInt(cmd.token));
3885 __ j(not_equal, &skip);
3886 switch (cmd.command) {
3887 case kReturn:
3888 codegen_->EmitUnwindAndReturn();
3889 break;
3890 case kThrow:
3891 __ Push(result_register());
3892 __ CallRuntime(Runtime::kReThrow);
3893 break;
3894 case kContinue:
3895 codegen_->EmitContinue(cmd.target);
3896 break;
3897 case kBreak:
3898 codegen_->EmitBreak(cmd.target);
3899 break;
3900 }
3901 __ bind(&skip);
3902 }
3903}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003904
3905#undef __
3906
3907
3908static const byte kJnsInstruction = 0x79;
3909static const byte kNopByteOne = 0x66;
3910static const byte kNopByteTwo = 0x90;
3911#ifdef DEBUG
3912static const byte kCallInstruction = 0xe8;
3913#endif
3914
3915
3916void BackEdgeTable::PatchAt(Code* unoptimized_code,
3917 Address pc,
3918 BackEdgeState target_state,
3919 Code* replacement_code) {
3920 Address call_target_address = pc - kIntSize;
3921 Address jns_instr_address = call_target_address - 3;
3922 Address jns_offset_address = call_target_address - 2;
3923
3924 switch (target_state) {
3925 case INTERRUPT:
3926 // sub <profiling_counter>, <delta> ;; Not changed
3927 // jns ok
3928 // call <interrupt stub>
3929 // ok:
3930 *jns_instr_address = kJnsInstruction;
3931 *jns_offset_address = kJnsOffset;
3932 break;
3933 case ON_STACK_REPLACEMENT:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003934 // sub <profiling_counter>, <delta> ;; Not changed
3935 // nop
3936 // nop
3937 // call <on-stack replacment>
3938 // ok:
3939 *jns_instr_address = kNopByteOne;
3940 *jns_offset_address = kNopByteTwo;
3941 break;
3942 }
3943
3944 Assembler::set_target_address_at(unoptimized_code->GetIsolate(),
3945 call_target_address, unoptimized_code,
3946 replacement_code->entry());
3947 unoptimized_code->GetHeap()->incremental_marking()->RecordCodeTargetPatch(
3948 unoptimized_code, call_target_address, replacement_code);
3949}
3950
3951
3952BackEdgeTable::BackEdgeState BackEdgeTable::GetBackEdgeState(
3953 Isolate* isolate,
3954 Code* unoptimized_code,
3955 Address pc) {
3956 Address call_target_address = pc - kIntSize;
3957 Address jns_instr_address = call_target_address - 3;
3958 DCHECK_EQ(kCallInstruction, *(call_target_address - 1));
3959
3960 if (*jns_instr_address == kJnsInstruction) {
3961 DCHECK_EQ(kJnsOffset, *(call_target_address - 2));
3962 DCHECK_EQ(isolate->builtins()->InterruptCheck()->entry(),
3963 Assembler::target_address_at(call_target_address,
3964 unoptimized_code));
3965 return INTERRUPT;
3966 }
3967
3968 DCHECK_EQ(kNopByteOne, *jns_instr_address);
3969 DCHECK_EQ(kNopByteTwo, *(call_target_address - 2));
3970
Ben Murdochda12d292016-06-02 14:46:10 +01003971 DCHECK_EQ(
3972 isolate->builtins()->OnStackReplacement()->entry(),
3973 Assembler::target_address_at(call_target_address, unoptimized_code));
3974 return ON_STACK_REPLACEMENT;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003975}
3976
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003977} // namespace internal
3978} // namespace v8
3979
3980#endif // V8_TARGET_ARCH_X64