blob: 350543ef88cf6042a8569eb92734ae79b4c14be3 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2013 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/crankshaft/x64/lithium-codegen-x64.h"
8
9#include "src/base/bits.h"
10#include "src/code-factory.h"
11#include "src/code-stubs.h"
12#include "src/crankshaft/hydrogen-osr.h"
13#include "src/ic/ic.h"
14#include "src/ic/stub-cache.h"
15#include "src/profiler/cpu-profiler.h"
16
17namespace v8 {
18namespace internal {
19
20
21// When invoking builtins, we need to record the safepoint in the middle of
22// the invoke instruction sequence generated by the macro assembler.
23class SafepointGenerator final : public CallWrapper {
24 public:
25 SafepointGenerator(LCodeGen* codegen,
26 LPointerMap* pointers,
27 Safepoint::DeoptMode mode)
28 : codegen_(codegen),
29 pointers_(pointers),
30 deopt_mode_(mode) { }
31 virtual ~SafepointGenerator() {}
32
33 void BeforeCall(int call_size) const override {}
34
35 void AfterCall() const override {
36 codegen_->RecordSafepoint(pointers_, deopt_mode_);
37 }
38
39 private:
40 LCodeGen* codegen_;
41 LPointerMap* pointers_;
42 Safepoint::DeoptMode deopt_mode_;
43};
44
45
46#define __ masm()->
47
48bool LCodeGen::GenerateCode() {
49 LPhase phase("Z_Code generation", chunk());
50 DCHECK(is_unused());
51 status_ = GENERATING;
52
53 // Open a frame scope to indicate that there is a frame on the stack. The
54 // MANUAL indicates that the scope shouldn't actually generate code to set up
55 // the frame (that is done in GeneratePrologue).
56 FrameScope frame_scope(masm_, StackFrame::MANUAL);
57
58 return GeneratePrologue() &&
59 GenerateBody() &&
60 GenerateDeferredCode() &&
61 GenerateJumpTable() &&
62 GenerateSafepointTable();
63}
64
65
66void LCodeGen::FinishCode(Handle<Code> code) {
67 DCHECK(is_done());
Ben Murdoch097c5b22016-05-18 11:27:45 +010068 code->set_stack_slots(GetTotalFrameSlotCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000069 code->set_safepoint_table_offset(safepoints_.GetCodeOffset());
70 PopulateDeoptimizationData(code);
71}
72
73
74#ifdef _MSC_VER
75void LCodeGen::MakeSureStackPagesMapped(int offset) {
76 const int kPageSize = 4 * KB;
77 for (offset -= kPageSize; offset > 0; offset -= kPageSize) {
78 __ movp(Operand(rsp, offset), rax);
79 }
80}
81#endif
82
83
84void LCodeGen::SaveCallerDoubles() {
85 DCHECK(info()->saves_caller_doubles());
86 DCHECK(NeedsEagerFrame());
87 Comment(";;; Save clobbered callee double registers");
88 int count = 0;
89 BitVector* doubles = chunk()->allocated_double_registers();
90 BitVector::Iterator save_iterator(doubles);
91 while (!save_iterator.Done()) {
92 __ Movsd(MemOperand(rsp, count * kDoubleSize),
93 XMMRegister::from_code(save_iterator.Current()));
94 save_iterator.Advance();
95 count++;
96 }
97}
98
99
100void LCodeGen::RestoreCallerDoubles() {
101 DCHECK(info()->saves_caller_doubles());
102 DCHECK(NeedsEagerFrame());
103 Comment(";;; Restore clobbered callee double registers");
104 BitVector* doubles = chunk()->allocated_double_registers();
105 BitVector::Iterator save_iterator(doubles);
106 int count = 0;
107 while (!save_iterator.Done()) {
108 __ Movsd(XMMRegister::from_code(save_iterator.Current()),
109 MemOperand(rsp, count * kDoubleSize));
110 save_iterator.Advance();
111 count++;
112 }
113}
114
115
116bool LCodeGen::GeneratePrologue() {
117 DCHECK(is_generating());
118
119 if (info()->IsOptimizing()) {
120 ProfileEntryHookStub::MaybeCallEntryHook(masm_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000121 }
122
123 info()->set_prologue_offset(masm_->pc_offset());
124 if (NeedsEagerFrame()) {
125 DCHECK(!frame_is_built_);
126 frame_is_built_ = true;
127 if (info()->IsStub()) {
Ben Murdochda12d292016-06-02 14:46:10 +0100128 __ StubPrologue(StackFrame::STUB);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000129 } else {
130 __ Prologue(info()->GeneratePreagedPrologue());
131 }
132 }
133
134 // Reserve space for the stack slots needed by the code.
135 int slots = GetStackSlotCount();
136 if (slots > 0) {
137 if (FLAG_debug_code) {
138 __ subp(rsp, Immediate(slots * kPointerSize));
139#ifdef _MSC_VER
140 MakeSureStackPagesMapped(slots * kPointerSize);
141#endif
142 __ Push(rax);
143 __ Set(rax, slots);
144 __ Set(kScratchRegister, kSlotsZapValue);
145 Label loop;
146 __ bind(&loop);
147 __ movp(MemOperand(rsp, rax, times_pointer_size, 0),
148 kScratchRegister);
149 __ decl(rax);
150 __ j(not_zero, &loop);
151 __ Pop(rax);
152 } else {
153 __ subp(rsp, Immediate(slots * kPointerSize));
154#ifdef _MSC_VER
155 MakeSureStackPagesMapped(slots * kPointerSize);
156#endif
157 }
158
159 if (info()->saves_caller_doubles()) {
160 SaveCallerDoubles();
161 }
162 }
163 return !is_aborted();
164}
165
166
167void LCodeGen::DoPrologue(LPrologue* instr) {
168 Comment(";;; Prologue begin");
169
170 // Possibly allocate a local context.
Ben Murdochc5610432016-08-08 18:44:38 +0100171 if (info_->scope()->num_heap_slots() > 0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000172 Comment(";;; Allocate local context");
173 bool need_write_barrier = true;
174 // Argument to NewContext is the function, which is still in rdi.
Ben Murdochc5610432016-08-08 18:44:38 +0100175 int slots = info_->scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000176 Safepoint::DeoptMode deopt_mode = Safepoint::kNoLazyDeopt;
177 if (info()->scope()->is_script_scope()) {
178 __ Push(rdi);
179 __ Push(info()->scope()->GetScopeInfo(info()->isolate()));
180 __ CallRuntime(Runtime::kNewScriptContext);
181 deopt_mode = Safepoint::kLazyDeopt;
182 } else if (slots <= FastNewContextStub::kMaximumSlots) {
183 FastNewContextStub stub(isolate(), slots);
184 __ CallStub(&stub);
185 // Result of FastNewContextStub is always in new space.
186 need_write_barrier = false;
187 } else {
188 __ Push(rdi);
189 __ CallRuntime(Runtime::kNewFunctionContext);
190 }
191 RecordSafepoint(deopt_mode);
192
193 // Context is returned in rax. It replaces the context passed to us.
194 // It's saved in the stack and kept live in rsi.
195 __ movp(rsi, rax);
196 __ movp(Operand(rbp, StandardFrameConstants::kContextOffset), rax);
197
198 // Copy any necessary parameters into the context.
199 int num_parameters = scope()->num_parameters();
200 int first_parameter = scope()->has_this_declaration() ? -1 : 0;
201 for (int i = first_parameter; i < num_parameters; i++) {
202 Variable* var = (i == -1) ? scope()->receiver() : scope()->parameter(i);
203 if (var->IsContextSlot()) {
204 int parameter_offset = StandardFrameConstants::kCallerSPOffset +
205 (num_parameters - 1 - i) * kPointerSize;
206 // Load parameter from stack.
207 __ movp(rax, Operand(rbp, parameter_offset));
208 // Store it in the context.
209 int context_offset = Context::SlotOffset(var->index());
210 __ movp(Operand(rsi, context_offset), rax);
211 // Update the write barrier. This clobbers rax and rbx.
212 if (need_write_barrier) {
213 __ RecordWriteContextSlot(rsi, context_offset, rax, rbx, kSaveFPRegs);
214 } else if (FLAG_debug_code) {
215 Label done;
216 __ JumpIfInNewSpace(rsi, rax, &done, Label::kNear);
217 __ Abort(kExpectedNewSpaceObject);
218 __ bind(&done);
219 }
220 }
221 }
222 Comment(";;; End allocate local context");
223 }
224
225 Comment(";;; Prologue end");
226}
227
228
229void LCodeGen::GenerateOsrPrologue() {
230 // Generate the OSR entry prologue at the first unknown OSR value, or if there
231 // are none, at the OSR entrypoint instruction.
232 if (osr_pc_offset_ >= 0) return;
233
234 osr_pc_offset_ = masm()->pc_offset();
235
236 // Adjust the frame size, subsuming the unoptimized frame into the
237 // optimized frame.
238 int slots = GetStackSlotCount() - graph()->osr()->UnoptimizedFrameSlots();
239 DCHECK(slots >= 0);
240 __ subp(rsp, Immediate(slots * kPointerSize));
241}
242
243
244void LCodeGen::GenerateBodyInstructionPre(LInstruction* instr) {
245 if (instr->IsCall()) {
246 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
247 }
248 if (!instr->IsLazyBailout() && !instr->IsGap()) {
249 safepoints_.BumpLastLazySafepointIndex();
250 }
251}
252
253
254void LCodeGen::GenerateBodyInstructionPost(LInstruction* instr) {
255 if (FLAG_debug_code && FLAG_enable_slow_asserts && instr->HasResult() &&
256 instr->hydrogen_value()->representation().IsInteger32() &&
257 instr->result()->IsRegister()) {
258 __ AssertZeroExtended(ToRegister(instr->result()));
259 }
260
261 if (instr->HasResult() && instr->MustSignExtendResult(chunk())) {
262 // We sign extend the dehoisted key at the definition point when the pointer
263 // size is 64-bit. For x32 port, we sign extend the dehoisted key at the use
264 // points and MustSignExtendResult is always false. We can't use
265 // STATIC_ASSERT here as the pointer size is 32-bit for x32.
266 DCHECK(kPointerSize == kInt64Size);
267 if (instr->result()->IsRegister()) {
268 Register result_reg = ToRegister(instr->result());
269 __ movsxlq(result_reg, result_reg);
270 } else {
271 // Sign extend the 32bit result in the stack slots.
272 DCHECK(instr->result()->IsStackSlot());
273 Operand src = ToOperand(instr->result());
274 __ movsxlq(kScratchRegister, src);
275 __ movq(src, kScratchRegister);
276 }
277 }
278}
279
280
281bool LCodeGen::GenerateJumpTable() {
282 if (jump_table_.length() == 0) return !is_aborted();
283
284 Label needs_frame;
285 Comment(";;; -------------------- Jump table --------------------");
286 for (int i = 0; i < jump_table_.length(); i++) {
287 Deoptimizer::JumpTableEntry* table_entry = &jump_table_[i];
288 __ bind(&table_entry->label);
289 Address entry = table_entry->address;
290 DeoptComment(table_entry->deopt_info);
291 if (table_entry->needs_frame) {
292 DCHECK(!info()->saves_caller_doubles());
293 __ Move(kScratchRegister, ExternalReference::ForDeoptEntry(entry));
294 __ call(&needs_frame);
295 } else {
296 if (info()->saves_caller_doubles()) {
297 DCHECK(info()->IsStub());
298 RestoreCallerDoubles();
299 }
300 __ call(entry, RelocInfo::RUNTIME_ENTRY);
301 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000302 }
303
304 if (needs_frame.is_linked()) {
305 __ bind(&needs_frame);
306 /* stack layout
Ben Murdochda12d292016-06-02 14:46:10 +0100307 3: return address <-- rsp
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000308 2: garbage
309 1: garbage
310 0: garbage
311 */
Ben Murdochda12d292016-06-02 14:46:10 +0100312 // Reserve space for stub marker.
313 __ subp(rsp, Immediate(TypedFrameConstants::kFrameTypeSize));
314 __ Push(MemOperand(
315 rsp, TypedFrameConstants::kFrameTypeSize)); // Copy return address.
316 __ Push(kScratchRegister);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000317
318 /* stack layout
Ben Murdochda12d292016-06-02 14:46:10 +0100319 3: return address
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000320 2: garbage
321 1: return address
322 0: entry address <-- rsp
323 */
324
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000325 // Create a stack frame.
Ben Murdochda12d292016-06-02 14:46:10 +0100326 __ movp(MemOperand(rsp, 3 * kPointerSize), rbp);
327 __ leap(rbp, MemOperand(rsp, 3 * kPointerSize));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000328
329 // This variant of deopt can only be used with stubs. Since we don't
330 // have a function pointer to install in the stack frame that we're
331 // building, install a special marker there instead.
332 DCHECK(info()->IsStub());
333 __ Move(MemOperand(rsp, 2 * kPointerSize), Smi::FromInt(StackFrame::STUB));
334
335 /* stack layout
Ben Murdochda12d292016-06-02 14:46:10 +0100336 3: old rbp
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000337 2: stub marker
338 1: return address
339 0: entry address <-- rsp
340 */
341 __ ret(0);
342 }
343
344 return !is_aborted();
345}
346
347
348bool LCodeGen::GenerateDeferredCode() {
349 DCHECK(is_generating());
350 if (deferred_.length() > 0) {
351 for (int i = 0; !is_aborted() && i < deferred_.length(); i++) {
352 LDeferredCode* code = deferred_[i];
353
354 HValue* value =
355 instructions_->at(code->instruction_index())->hydrogen_value();
356 RecordAndWritePosition(
357 chunk()->graph()->SourcePositionToScriptPosition(value->position()));
358
359 Comment(";;; <@%d,#%d> "
360 "-------------------- Deferred %s --------------------",
361 code->instruction_index(),
362 code->instr()->hydrogen_value()->id(),
363 code->instr()->Mnemonic());
364 __ bind(code->entry());
365 if (NeedsDeferredFrame()) {
366 Comment(";;; Build frame");
367 DCHECK(!frame_is_built_);
368 DCHECK(info()->IsStub());
369 frame_is_built_ = true;
370 // Build the frame in such a way that esi isn't trashed.
371 __ pushq(rbp); // Caller's frame pointer.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000372 __ Push(Smi::FromInt(StackFrame::STUB));
Ben Murdochda12d292016-06-02 14:46:10 +0100373 __ leap(rbp, Operand(rsp, TypedFrameConstants::kFixedFrameSizeFromFp));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000374 Comment(";;; Deferred code");
375 }
376 code->Generate();
377 if (NeedsDeferredFrame()) {
378 __ bind(code->done());
379 Comment(";;; Destroy frame");
380 DCHECK(frame_is_built_);
381 frame_is_built_ = false;
382 __ movp(rsp, rbp);
383 __ popq(rbp);
384 }
385 __ jmp(code->exit());
386 }
387 }
388
389 // Deferred code is the last part of the instruction sequence. Mark
390 // the generated code as done unless we bailed out.
391 if (!is_aborted()) status_ = DONE;
392 return !is_aborted();
393}
394
395
396bool LCodeGen::GenerateSafepointTable() {
397 DCHECK(is_done());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100398 safepoints_.Emit(masm(), GetTotalFrameSlotCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000399 return !is_aborted();
400}
401
402
403Register LCodeGen::ToRegister(int index) const {
404 return Register::from_code(index);
405}
406
407
408XMMRegister LCodeGen::ToDoubleRegister(int index) const {
409 return XMMRegister::from_code(index);
410}
411
412
413Register LCodeGen::ToRegister(LOperand* op) const {
414 DCHECK(op->IsRegister());
415 return ToRegister(op->index());
416}
417
418
419XMMRegister LCodeGen::ToDoubleRegister(LOperand* op) const {
420 DCHECK(op->IsDoubleRegister());
421 return ToDoubleRegister(op->index());
422}
423
424
425bool LCodeGen::IsInteger32Constant(LConstantOperand* op) const {
426 return chunk_->LookupLiteralRepresentation(op).IsSmiOrInteger32();
427}
428
429
430bool LCodeGen::IsExternalConstant(LConstantOperand* op) const {
431 return chunk_->LookupLiteralRepresentation(op).IsExternal();
432}
433
434
435bool LCodeGen::IsDehoistedKeyConstant(LConstantOperand* op) const {
436 return op->IsConstantOperand() &&
437 chunk_->IsDehoistedKey(chunk_->LookupConstant(op));
438}
439
440
441bool LCodeGen::IsSmiConstant(LConstantOperand* op) const {
442 return chunk_->LookupLiteralRepresentation(op).IsSmi();
443}
444
445
446int32_t LCodeGen::ToInteger32(LConstantOperand* op) const {
447 return ToRepresentation(op, Representation::Integer32());
448}
449
450
451int32_t LCodeGen::ToRepresentation(LConstantOperand* op,
452 const Representation& r) const {
453 HConstant* constant = chunk_->LookupConstant(op);
454 int32_t value = constant->Integer32Value();
455 if (r.IsInteger32()) return value;
456 DCHECK(SmiValuesAre31Bits() && r.IsSmiOrTagged());
457 return static_cast<int32_t>(reinterpret_cast<intptr_t>(Smi::FromInt(value)));
458}
459
460
461Smi* LCodeGen::ToSmi(LConstantOperand* op) const {
462 HConstant* constant = chunk_->LookupConstant(op);
463 return Smi::FromInt(constant->Integer32Value());
464}
465
466
467double LCodeGen::ToDouble(LConstantOperand* op) const {
468 HConstant* constant = chunk_->LookupConstant(op);
469 DCHECK(constant->HasDoubleValue());
470 return constant->DoubleValue();
471}
472
473
474ExternalReference LCodeGen::ToExternalReference(LConstantOperand* op) const {
475 HConstant* constant = chunk_->LookupConstant(op);
476 DCHECK(constant->HasExternalReferenceValue());
477 return constant->ExternalReferenceValue();
478}
479
480
481Handle<Object> LCodeGen::ToHandle(LConstantOperand* op) const {
482 HConstant* constant = chunk_->LookupConstant(op);
483 DCHECK(chunk_->LookupLiteralRepresentation(op).IsSmiOrTagged());
484 return constant->handle(isolate());
485}
486
487
488static int ArgumentsOffsetWithoutFrame(int index) {
489 DCHECK(index < 0);
490 return -(index + 1) * kPointerSize + kPCOnStackSize;
491}
492
493
494Operand LCodeGen::ToOperand(LOperand* op) const {
495 // Does not handle registers. In X64 assembler, plain registers are not
496 // representable as an Operand.
497 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
498 if (NeedsEagerFrame()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100499 return Operand(rbp, FrameSlotToFPOffset(op->index()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000500 } else {
501 // Retrieve parameter without eager stack-frame relative to the
502 // stack-pointer.
503 return Operand(rsp, ArgumentsOffsetWithoutFrame(op->index()));
504 }
505}
506
507
508void LCodeGen::WriteTranslation(LEnvironment* environment,
509 Translation* translation) {
510 if (environment == NULL) return;
511
512 // The translation includes one command per value in the environment.
513 int translation_size = environment->translation_size();
514
515 WriteTranslation(environment->outer(), translation);
516 WriteTranslationFrame(environment, translation);
517
518 int object_index = 0;
519 int dematerialized_index = 0;
520 for (int i = 0; i < translation_size; ++i) {
521 LOperand* value = environment->values()->at(i);
522 AddToTranslation(
523 environment, translation, value, environment->HasTaggedValueAt(i),
524 environment->HasUint32ValueAt(i), &object_index, &dematerialized_index);
525 }
526}
527
528
529void LCodeGen::AddToTranslation(LEnvironment* environment,
530 Translation* translation,
531 LOperand* op,
532 bool is_tagged,
533 bool is_uint32,
534 int* object_index_pointer,
535 int* dematerialized_index_pointer) {
536 if (op == LEnvironment::materialization_marker()) {
537 int object_index = (*object_index_pointer)++;
538 if (environment->ObjectIsDuplicateAt(object_index)) {
539 int dupe_of = environment->ObjectDuplicateOfAt(object_index);
540 translation->DuplicateObject(dupe_of);
541 return;
542 }
543 int object_length = environment->ObjectLengthAt(object_index);
544 if (environment->ObjectIsArgumentsAt(object_index)) {
545 translation->BeginArgumentsObject(object_length);
546 } else {
547 translation->BeginCapturedObject(object_length);
548 }
549 int dematerialized_index = *dematerialized_index_pointer;
550 int env_offset = environment->translation_size() + dematerialized_index;
551 *dematerialized_index_pointer += object_length;
552 for (int i = 0; i < object_length; ++i) {
553 LOperand* value = environment->values()->at(env_offset + i);
554 AddToTranslation(environment,
555 translation,
556 value,
557 environment->HasTaggedValueAt(env_offset + i),
558 environment->HasUint32ValueAt(env_offset + i),
559 object_index_pointer,
560 dematerialized_index_pointer);
561 }
562 return;
563 }
564
565 if (op->IsStackSlot()) {
566 int index = op->index();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000567 if (is_tagged) {
568 translation->StoreStackSlot(index);
569 } else if (is_uint32) {
570 translation->StoreUint32StackSlot(index);
571 } else {
572 translation->StoreInt32StackSlot(index);
573 }
574 } else if (op->IsDoubleStackSlot()) {
575 int index = op->index();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000576 translation->StoreDoubleStackSlot(index);
577 } else if (op->IsRegister()) {
578 Register reg = ToRegister(op);
579 if (is_tagged) {
580 translation->StoreRegister(reg);
581 } else if (is_uint32) {
582 translation->StoreUint32Register(reg);
583 } else {
584 translation->StoreInt32Register(reg);
585 }
586 } else if (op->IsDoubleRegister()) {
587 XMMRegister reg = ToDoubleRegister(op);
588 translation->StoreDoubleRegister(reg);
589 } else if (op->IsConstantOperand()) {
590 HConstant* constant = chunk()->LookupConstant(LConstantOperand::cast(op));
591 int src_index = DefineDeoptimizationLiteral(constant->handle(isolate()));
592 translation->StoreLiteral(src_index);
593 } else {
594 UNREACHABLE();
595 }
596}
597
598
599void LCodeGen::CallCodeGeneric(Handle<Code> code,
600 RelocInfo::Mode mode,
601 LInstruction* instr,
602 SafepointMode safepoint_mode,
603 int argc) {
604 DCHECK(instr != NULL);
605 __ call(code, mode);
606 RecordSafepointWithLazyDeopt(instr, safepoint_mode, argc);
607
608 // Signal that we don't inline smi code before these stubs in the
609 // optimizing code generator.
610 if (code->kind() == Code::BINARY_OP_IC ||
611 code->kind() == Code::COMPARE_IC) {
612 __ nop();
613 }
614}
615
616
617void LCodeGen::CallCode(Handle<Code> code,
618 RelocInfo::Mode mode,
619 LInstruction* instr) {
620 CallCodeGeneric(code, mode, instr, RECORD_SIMPLE_SAFEPOINT, 0);
621}
622
623
624void LCodeGen::CallRuntime(const Runtime::Function* function,
625 int num_arguments,
626 LInstruction* instr,
627 SaveFPRegsMode save_doubles) {
628 DCHECK(instr != NULL);
629 DCHECK(instr->HasPointerMap());
630
631 __ CallRuntime(function, num_arguments, save_doubles);
632
633 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT, 0);
634}
635
636
637void LCodeGen::LoadContextFromDeferred(LOperand* context) {
638 if (context->IsRegister()) {
639 if (!ToRegister(context).is(rsi)) {
640 __ movp(rsi, ToRegister(context));
641 }
642 } else if (context->IsStackSlot()) {
643 __ movp(rsi, ToOperand(context));
644 } else if (context->IsConstantOperand()) {
645 HConstant* constant =
646 chunk_->LookupConstant(LConstantOperand::cast(context));
647 __ Move(rsi, Handle<Object>::cast(constant->handle(isolate())));
648 } else {
649 UNREACHABLE();
650 }
651}
652
653
654
655void LCodeGen::CallRuntimeFromDeferred(Runtime::FunctionId id,
656 int argc,
657 LInstruction* instr,
658 LOperand* context) {
659 LoadContextFromDeferred(context);
660
661 __ CallRuntimeSaveDoubles(id);
662 RecordSafepointWithRegisters(
663 instr->pointer_map(), argc, Safepoint::kNoLazyDeopt);
664}
665
666
667void LCodeGen::RegisterEnvironmentForDeoptimization(LEnvironment* environment,
668 Safepoint::DeoptMode mode) {
669 environment->set_has_been_used();
670 if (!environment->HasBeenRegistered()) {
671 // Physical stack frame layout:
672 // -x ............. -4 0 ..................................... y
673 // [incoming arguments] [spill slots] [pushed outgoing arguments]
674
675 // Layout of the environment:
676 // 0 ..................................................... size-1
677 // [parameters] [locals] [expression stack including arguments]
678
679 // Layout of the translation:
680 // 0 ........................................................ size - 1 + 4
681 // [expression stack including arguments] [locals] [4 words] [parameters]
682 // |>------------ translation_size ------------<|
683
684 int frame_count = 0;
685 int jsframe_count = 0;
686 for (LEnvironment* e = environment; e != NULL; e = e->outer()) {
687 ++frame_count;
688 if (e->frame_type() == JS_FUNCTION) {
689 ++jsframe_count;
690 }
691 }
692 Translation translation(&translations_, frame_count, jsframe_count, zone());
693 WriteTranslation(environment, &translation);
694 int deoptimization_index = deoptimizations_.length();
695 int pc_offset = masm()->pc_offset();
696 environment->Register(deoptimization_index,
697 translation.index(),
698 (mode == Safepoint::kLazyDeopt) ? pc_offset : -1);
699 deoptimizations_.Add(environment, environment->zone());
700 }
701}
702
703
704void LCodeGen::DeoptimizeIf(Condition cc, LInstruction* instr,
705 Deoptimizer::DeoptReason deopt_reason,
706 Deoptimizer::BailoutType bailout_type) {
707 LEnvironment* environment = instr->environment();
708 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
709 DCHECK(environment->HasBeenRegistered());
710 int id = environment->deoptimization_index();
711 Address entry =
712 Deoptimizer::GetDeoptimizationEntry(isolate(), id, bailout_type);
713 if (entry == NULL) {
714 Abort(kBailoutWasNotPrepared);
715 return;
716 }
717
718 if (DeoptEveryNTimes()) {
719 ExternalReference count = ExternalReference::stress_deopt_count(isolate());
720 Label no_deopt;
721 __ pushfq();
722 __ pushq(rax);
723 Operand count_operand = masm()->ExternalOperand(count, kScratchRegister);
724 __ movl(rax, count_operand);
725 __ subl(rax, Immediate(1));
726 __ j(not_zero, &no_deopt, Label::kNear);
727 if (FLAG_trap_on_deopt) __ int3();
728 __ movl(rax, Immediate(FLAG_deopt_every_n_times));
729 __ movl(count_operand, rax);
730 __ popq(rax);
731 __ popfq();
732 DCHECK(frame_is_built_);
733 __ call(entry, RelocInfo::RUNTIME_ENTRY);
734 __ bind(&no_deopt);
735 __ movl(count_operand, rax);
736 __ popq(rax);
737 __ popfq();
738 }
739
740 if (info()->ShouldTrapOnDeopt()) {
741 Label done;
742 if (cc != no_condition) {
743 __ j(NegateCondition(cc), &done, Label::kNear);
744 }
745 __ int3();
746 __ bind(&done);
747 }
748
Ben Murdochc5610432016-08-08 18:44:38 +0100749 Deoptimizer::DeoptInfo deopt_info = MakeDeoptInfo(instr, deopt_reason, id);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000750
751 DCHECK(info()->IsStub() || frame_is_built_);
752 // Go through jump table if we need to handle condition, build frame, or
753 // restore caller doubles.
754 if (cc == no_condition && frame_is_built_ &&
755 !info()->saves_caller_doubles()) {
756 DeoptComment(deopt_info);
757 __ call(entry, RelocInfo::RUNTIME_ENTRY);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000758 } else {
759 Deoptimizer::JumpTableEntry table_entry(entry, deopt_info, bailout_type,
760 !frame_is_built_);
761 // We often have several deopts to the same entry, reuse the last
762 // jump entry if this is the case.
763 if (FLAG_trace_deopt || isolate()->cpu_profiler()->is_profiling() ||
764 jump_table_.is_empty() ||
765 !table_entry.IsEquivalentTo(jump_table_.last())) {
766 jump_table_.Add(table_entry, zone());
767 }
768 if (cc == no_condition) {
769 __ jmp(&jump_table_.last().label);
770 } else {
771 __ j(cc, &jump_table_.last().label);
772 }
773 }
774}
775
776
777void LCodeGen::DeoptimizeIf(Condition cc, LInstruction* instr,
778 Deoptimizer::DeoptReason deopt_reason) {
779 Deoptimizer::BailoutType bailout_type = info()->IsStub()
780 ? Deoptimizer::LAZY
781 : Deoptimizer::EAGER;
782 DeoptimizeIf(cc, instr, deopt_reason, bailout_type);
783}
784
785
786void LCodeGen::RecordSafepointWithLazyDeopt(
787 LInstruction* instr, SafepointMode safepoint_mode, int argc) {
788 if (safepoint_mode == RECORD_SIMPLE_SAFEPOINT) {
789 RecordSafepoint(instr->pointer_map(), Safepoint::kLazyDeopt);
790 } else {
791 DCHECK(safepoint_mode == RECORD_SAFEPOINT_WITH_REGISTERS);
792 RecordSafepointWithRegisters(
793 instr->pointer_map(), argc, Safepoint::kLazyDeopt);
794 }
795}
796
797
798void LCodeGen::RecordSafepoint(
799 LPointerMap* pointers,
800 Safepoint::Kind kind,
801 int arguments,
802 Safepoint::DeoptMode deopt_mode) {
803 DCHECK(kind == expected_safepoint_kind_);
804
805 const ZoneList<LOperand*>* operands = pointers->GetNormalizedOperands();
806
807 Safepoint safepoint = safepoints_.DefineSafepoint(masm(),
808 kind, arguments, deopt_mode);
809 for (int i = 0; i < operands->length(); i++) {
810 LOperand* pointer = operands->at(i);
811 if (pointer->IsStackSlot()) {
812 safepoint.DefinePointerSlot(pointer->index(), zone());
813 } else if (pointer->IsRegister() && (kind & Safepoint::kWithRegisters)) {
814 safepoint.DefinePointerRegister(ToRegister(pointer), zone());
815 }
816 }
817}
818
819
820void LCodeGen::RecordSafepoint(LPointerMap* pointers,
821 Safepoint::DeoptMode deopt_mode) {
822 RecordSafepoint(pointers, Safepoint::kSimple, 0, deopt_mode);
823}
824
825
826void LCodeGen::RecordSafepoint(Safepoint::DeoptMode deopt_mode) {
827 LPointerMap empty_pointers(zone());
828 RecordSafepoint(&empty_pointers, deopt_mode);
829}
830
831
832void LCodeGen::RecordSafepointWithRegisters(LPointerMap* pointers,
833 int arguments,
834 Safepoint::DeoptMode deopt_mode) {
835 RecordSafepoint(pointers, Safepoint::kWithRegisters, arguments, deopt_mode);
836}
837
838
839void LCodeGen::RecordAndWritePosition(int position) {
840 if (position == RelocInfo::kNoPosition) return;
841 masm()->positions_recorder()->RecordPosition(position);
842 masm()->positions_recorder()->WriteRecordedPositions();
843}
844
845
846static const char* LabelType(LLabel* label) {
847 if (label->is_loop_header()) return " (loop header)";
848 if (label->is_osr_entry()) return " (OSR entry)";
849 return "";
850}
851
852
853void LCodeGen::DoLabel(LLabel* label) {
854 Comment(";;; <@%d,#%d> -------------------- B%d%s --------------------",
855 current_instruction_,
856 label->hydrogen_value()->id(),
857 label->block_id(),
858 LabelType(label));
859 __ bind(label->label());
860 current_block_ = label->block_id();
861 DoGap(label);
862}
863
864
865void LCodeGen::DoParallelMove(LParallelMove* move) {
866 resolver_.Resolve(move);
867}
868
869
870void LCodeGen::DoGap(LGap* gap) {
871 for (int i = LGap::FIRST_INNER_POSITION;
872 i <= LGap::LAST_INNER_POSITION;
873 i++) {
874 LGap::InnerPosition inner_pos = static_cast<LGap::InnerPosition>(i);
875 LParallelMove* move = gap->GetParallelMove(inner_pos);
876 if (move != NULL) DoParallelMove(move);
877 }
878}
879
880
881void LCodeGen::DoInstructionGap(LInstructionGap* instr) {
882 DoGap(instr);
883}
884
885
886void LCodeGen::DoParameter(LParameter* instr) {
887 // Nothing to do.
888}
889
890
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000891void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
892 GenerateOsrPrologue();
893}
894
895
896void LCodeGen::DoModByPowerOf2I(LModByPowerOf2I* instr) {
897 Register dividend = ToRegister(instr->dividend());
898 int32_t divisor = instr->divisor();
899 DCHECK(dividend.is(ToRegister(instr->result())));
900
901 // Theoretically, a variation of the branch-free code for integer division by
902 // a power of 2 (calculating the remainder via an additional multiplication
903 // (which gets simplified to an 'and') and subtraction) should be faster, and
904 // this is exactly what GCC and clang emit. Nevertheless, benchmarks seem to
905 // indicate that positive dividends are heavily favored, so the branching
906 // version performs better.
907 HMod* hmod = instr->hydrogen();
908 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
909 Label dividend_is_not_negative, done;
910 if (hmod->CheckFlag(HValue::kLeftCanBeNegative)) {
911 __ testl(dividend, dividend);
912 __ j(not_sign, &dividend_is_not_negative, Label::kNear);
913 // Note that this is correct even for kMinInt operands.
914 __ negl(dividend);
915 __ andl(dividend, Immediate(mask));
916 __ negl(dividend);
917 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
918 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
919 }
920 __ jmp(&done, Label::kNear);
921 }
922
923 __ bind(&dividend_is_not_negative);
924 __ andl(dividend, Immediate(mask));
925 __ bind(&done);
926}
927
928
929void LCodeGen::DoModByConstI(LModByConstI* instr) {
930 Register dividend = ToRegister(instr->dividend());
931 int32_t divisor = instr->divisor();
932 DCHECK(ToRegister(instr->result()).is(rax));
933
934 if (divisor == 0) {
935 DeoptimizeIf(no_condition, instr, Deoptimizer::kDivisionByZero);
936 return;
937 }
938
939 __ TruncatingDiv(dividend, Abs(divisor));
940 __ imull(rdx, rdx, Immediate(Abs(divisor)));
941 __ movl(rax, dividend);
942 __ subl(rax, rdx);
943
944 // Check for negative zero.
945 HMod* hmod = instr->hydrogen();
946 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
947 Label remainder_not_zero;
948 __ j(not_zero, &remainder_not_zero, Label::kNear);
949 __ cmpl(dividend, Immediate(0));
950 DeoptimizeIf(less, instr, Deoptimizer::kMinusZero);
951 __ bind(&remainder_not_zero);
952 }
953}
954
955
956void LCodeGen::DoModI(LModI* instr) {
957 HMod* hmod = instr->hydrogen();
958
959 Register left_reg = ToRegister(instr->left());
960 DCHECK(left_reg.is(rax));
961 Register right_reg = ToRegister(instr->right());
962 DCHECK(!right_reg.is(rax));
963 DCHECK(!right_reg.is(rdx));
964 Register result_reg = ToRegister(instr->result());
965 DCHECK(result_reg.is(rdx));
966
967 Label done;
968 // Check for x % 0, idiv would signal a divide error. We have to
969 // deopt in this case because we can't return a NaN.
970 if (hmod->CheckFlag(HValue::kCanBeDivByZero)) {
971 __ testl(right_reg, right_reg);
972 DeoptimizeIf(zero, instr, Deoptimizer::kDivisionByZero);
973 }
974
975 // Check for kMinInt % -1, idiv would signal a divide error. We
976 // have to deopt if we care about -0, because we can't return that.
977 if (hmod->CheckFlag(HValue::kCanOverflow)) {
978 Label no_overflow_possible;
979 __ cmpl(left_reg, Immediate(kMinInt));
980 __ j(not_zero, &no_overflow_possible, Label::kNear);
981 __ cmpl(right_reg, Immediate(-1));
982 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
983 DeoptimizeIf(equal, instr, Deoptimizer::kMinusZero);
984 } else {
985 __ j(not_equal, &no_overflow_possible, Label::kNear);
986 __ Set(result_reg, 0);
987 __ jmp(&done, Label::kNear);
988 }
989 __ bind(&no_overflow_possible);
990 }
991
992 // Sign extend dividend in eax into edx:eax, since we are using only the low
993 // 32 bits of the values.
994 __ cdq();
995
996 // If we care about -0, test if the dividend is <0 and the result is 0.
997 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
998 Label positive_left;
999 __ testl(left_reg, left_reg);
1000 __ j(not_sign, &positive_left, Label::kNear);
1001 __ idivl(right_reg);
1002 __ testl(result_reg, result_reg);
1003 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1004 __ jmp(&done, Label::kNear);
1005 __ bind(&positive_left);
1006 }
1007 __ idivl(right_reg);
1008 __ bind(&done);
1009}
1010
1011
1012void LCodeGen::DoFlooringDivByPowerOf2I(LFlooringDivByPowerOf2I* instr) {
1013 Register dividend = ToRegister(instr->dividend());
1014 int32_t divisor = instr->divisor();
1015 DCHECK(dividend.is(ToRegister(instr->result())));
1016
1017 // If the divisor is positive, things are easy: There can be no deopts and we
1018 // can simply do an arithmetic right shift.
1019 if (divisor == 1) return;
1020 int32_t shift = WhichPowerOf2Abs(divisor);
1021 if (divisor > 1) {
1022 __ sarl(dividend, Immediate(shift));
1023 return;
1024 }
1025
1026 // If the divisor is negative, we have to negate and handle edge cases.
1027 __ negl(dividend);
1028 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1029 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1030 }
1031
1032 // Dividing by -1 is basically negation, unless we overflow.
1033 if (divisor == -1) {
1034 if (instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1035 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1036 }
1037 return;
1038 }
1039
1040 // If the negation could not overflow, simply shifting is OK.
1041 if (!instr->hydrogen()->CheckFlag(HValue::kLeftCanBeMinInt)) {
1042 __ sarl(dividend, Immediate(shift));
1043 return;
1044 }
1045
1046 Label not_kmin_int, done;
1047 __ j(no_overflow, &not_kmin_int, Label::kNear);
1048 __ movl(dividend, Immediate(kMinInt / divisor));
1049 __ jmp(&done, Label::kNear);
1050 __ bind(&not_kmin_int);
1051 __ sarl(dividend, Immediate(shift));
1052 __ bind(&done);
1053}
1054
1055
1056void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
1057 Register dividend = ToRegister(instr->dividend());
1058 int32_t divisor = instr->divisor();
1059 DCHECK(ToRegister(instr->result()).is(rdx));
1060
1061 if (divisor == 0) {
1062 DeoptimizeIf(no_condition, instr, Deoptimizer::kDivisionByZero);
1063 return;
1064 }
1065
1066 // Check for (0 / -x) that will produce negative zero.
1067 HMathFloorOfDiv* hdiv = instr->hydrogen();
1068 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1069 __ testl(dividend, dividend);
1070 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1071 }
1072
1073 // Easy case: We need no dynamic check for the dividend and the flooring
1074 // division is the same as the truncating division.
1075 if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
1076 (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
1077 __ TruncatingDiv(dividend, Abs(divisor));
1078 if (divisor < 0) __ negl(rdx);
1079 return;
1080 }
1081
1082 // In the general case we may need to adjust before and after the truncating
1083 // division to get a flooring division.
1084 Register temp = ToRegister(instr->temp3());
1085 DCHECK(!temp.is(dividend) && !temp.is(rax) && !temp.is(rdx));
1086 Label needs_adjustment, done;
1087 __ cmpl(dividend, Immediate(0));
1088 __ j(divisor > 0 ? less : greater, &needs_adjustment, Label::kNear);
1089 __ TruncatingDiv(dividend, Abs(divisor));
1090 if (divisor < 0) __ negl(rdx);
1091 __ jmp(&done, Label::kNear);
1092 __ bind(&needs_adjustment);
1093 __ leal(temp, Operand(dividend, divisor > 0 ? 1 : -1));
1094 __ TruncatingDiv(temp, Abs(divisor));
1095 if (divisor < 0) __ negl(rdx);
1096 __ decl(rdx);
1097 __ bind(&done);
1098}
1099
1100
1101// TODO(svenpanne) Refactor this to avoid code duplication with DoDivI.
1102void LCodeGen::DoFlooringDivI(LFlooringDivI* instr) {
1103 HBinaryOperation* hdiv = instr->hydrogen();
1104 Register dividend = ToRegister(instr->dividend());
1105 Register divisor = ToRegister(instr->divisor());
1106 Register remainder = ToRegister(instr->temp());
1107 Register result = ToRegister(instr->result());
1108 DCHECK(dividend.is(rax));
1109 DCHECK(remainder.is(rdx));
1110 DCHECK(result.is(rax));
1111 DCHECK(!divisor.is(rax));
1112 DCHECK(!divisor.is(rdx));
1113
1114 // Check for x / 0.
1115 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1116 __ testl(divisor, divisor);
1117 DeoptimizeIf(zero, instr, Deoptimizer::kDivisionByZero);
1118 }
1119
1120 // Check for (0 / -x) that will produce negative zero.
1121 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1122 Label dividend_not_zero;
1123 __ testl(dividend, dividend);
1124 __ j(not_zero, &dividend_not_zero, Label::kNear);
1125 __ testl(divisor, divisor);
1126 DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero);
1127 __ bind(&dividend_not_zero);
1128 }
1129
1130 // Check for (kMinInt / -1).
1131 if (hdiv->CheckFlag(HValue::kCanOverflow)) {
1132 Label dividend_not_min_int;
1133 __ cmpl(dividend, Immediate(kMinInt));
1134 __ j(not_zero, &dividend_not_min_int, Label::kNear);
1135 __ cmpl(divisor, Immediate(-1));
1136 DeoptimizeIf(zero, instr, Deoptimizer::kOverflow);
1137 __ bind(&dividend_not_min_int);
1138 }
1139
1140 // Sign extend to rdx (= remainder).
1141 __ cdq();
1142 __ idivl(divisor);
1143
1144 Label done;
1145 __ testl(remainder, remainder);
1146 __ j(zero, &done, Label::kNear);
1147 __ xorl(remainder, divisor);
1148 __ sarl(remainder, Immediate(31));
1149 __ addl(result, remainder);
1150 __ bind(&done);
1151}
1152
1153
1154void LCodeGen::DoDivByPowerOf2I(LDivByPowerOf2I* instr) {
1155 Register dividend = ToRegister(instr->dividend());
1156 int32_t divisor = instr->divisor();
1157 Register result = ToRegister(instr->result());
1158 DCHECK(divisor == kMinInt || base::bits::IsPowerOfTwo32(Abs(divisor)));
1159 DCHECK(!result.is(dividend));
1160
1161 // Check for (0 / -x) that will produce negative zero.
1162 HDiv* hdiv = instr->hydrogen();
1163 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1164 __ testl(dividend, dividend);
1165 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1166 }
1167 // Check for (kMinInt / -1).
1168 if (hdiv->CheckFlag(HValue::kCanOverflow) && divisor == -1) {
1169 __ cmpl(dividend, Immediate(kMinInt));
1170 DeoptimizeIf(zero, instr, Deoptimizer::kOverflow);
1171 }
1172 // Deoptimize if remainder will not be 0.
1173 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1174 divisor != 1 && divisor != -1) {
1175 int32_t mask = divisor < 0 ? -(divisor + 1) : (divisor - 1);
1176 __ testl(dividend, Immediate(mask));
1177 DeoptimizeIf(not_zero, instr, Deoptimizer::kLostPrecision);
1178 }
1179 __ Move(result, dividend);
1180 int32_t shift = WhichPowerOf2Abs(divisor);
1181 if (shift > 0) {
1182 // The arithmetic shift is always OK, the 'if' is an optimization only.
1183 if (shift > 1) __ sarl(result, Immediate(31));
1184 __ shrl(result, Immediate(32 - shift));
1185 __ addl(result, dividend);
1186 __ sarl(result, Immediate(shift));
1187 }
1188 if (divisor < 0) __ negl(result);
1189}
1190
1191
1192void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
1193 Register dividend = ToRegister(instr->dividend());
1194 int32_t divisor = instr->divisor();
1195 DCHECK(ToRegister(instr->result()).is(rdx));
1196
1197 if (divisor == 0) {
1198 DeoptimizeIf(no_condition, instr, Deoptimizer::kDivisionByZero);
1199 return;
1200 }
1201
1202 // Check for (0 / -x) that will produce negative zero.
1203 HDiv* hdiv = instr->hydrogen();
1204 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) {
1205 __ testl(dividend, dividend);
1206 DeoptimizeIf(zero, instr, Deoptimizer::kMinusZero);
1207 }
1208
1209 __ TruncatingDiv(dividend, Abs(divisor));
1210 if (divisor < 0) __ negl(rdx);
1211
1212 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1213 __ movl(rax, rdx);
1214 __ imull(rax, rax, Immediate(divisor));
1215 __ subl(rax, dividend);
1216 DeoptimizeIf(not_equal, instr, Deoptimizer::kLostPrecision);
1217 }
1218}
1219
1220
1221// TODO(svenpanne) Refactor this to avoid code duplication with DoFlooringDivI.
1222void LCodeGen::DoDivI(LDivI* instr) {
1223 HBinaryOperation* hdiv = instr->hydrogen();
1224 Register dividend = ToRegister(instr->dividend());
1225 Register divisor = ToRegister(instr->divisor());
1226 Register remainder = ToRegister(instr->temp());
1227 DCHECK(dividend.is(rax));
1228 DCHECK(remainder.is(rdx));
1229 DCHECK(ToRegister(instr->result()).is(rax));
1230 DCHECK(!divisor.is(rax));
1231 DCHECK(!divisor.is(rdx));
1232
1233 // Check for x / 0.
1234 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
1235 __ testl(divisor, divisor);
1236 DeoptimizeIf(zero, instr, Deoptimizer::kDivisionByZero);
1237 }
1238
1239 // Check for (0 / -x) that will produce negative zero.
1240 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
1241 Label dividend_not_zero;
1242 __ testl(dividend, dividend);
1243 __ j(not_zero, &dividend_not_zero, Label::kNear);
1244 __ testl(divisor, divisor);
1245 DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero);
1246 __ bind(&dividend_not_zero);
1247 }
1248
1249 // Check for (kMinInt / -1).
1250 if (hdiv->CheckFlag(HValue::kCanOverflow)) {
1251 Label dividend_not_min_int;
1252 __ cmpl(dividend, Immediate(kMinInt));
1253 __ j(not_zero, &dividend_not_min_int, Label::kNear);
1254 __ cmpl(divisor, Immediate(-1));
1255 DeoptimizeIf(zero, instr, Deoptimizer::kOverflow);
1256 __ bind(&dividend_not_min_int);
1257 }
1258
1259 // Sign extend to rdx (= remainder).
1260 __ cdq();
1261 __ idivl(divisor);
1262
1263 if (!hdiv->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1264 // Deoptimize if remainder is not 0.
1265 __ testl(remainder, remainder);
1266 DeoptimizeIf(not_zero, instr, Deoptimizer::kLostPrecision);
1267 }
1268}
1269
1270
1271void LCodeGen::DoMulI(LMulI* instr) {
1272 Register left = ToRegister(instr->left());
1273 LOperand* right = instr->right();
1274
1275 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1276 if (instr->hydrogen_value()->representation().IsSmi()) {
1277 __ movp(kScratchRegister, left);
1278 } else {
1279 __ movl(kScratchRegister, left);
1280 }
1281 }
1282
1283 bool can_overflow =
1284 instr->hydrogen()->CheckFlag(HValue::kCanOverflow);
1285 if (right->IsConstantOperand()) {
1286 int32_t right_value = ToInteger32(LConstantOperand::cast(right));
1287 if (right_value == -1) {
1288 __ negl(left);
1289 } else if (right_value == 0) {
1290 __ xorl(left, left);
1291 } else if (right_value == 2) {
1292 __ addl(left, left);
1293 } else if (!can_overflow) {
1294 // If the multiplication is known to not overflow, we
1295 // can use operations that don't set the overflow flag
1296 // correctly.
1297 switch (right_value) {
1298 case 1:
1299 // Do nothing.
1300 break;
1301 case 3:
1302 __ leal(left, Operand(left, left, times_2, 0));
1303 break;
1304 case 4:
1305 __ shll(left, Immediate(2));
1306 break;
1307 case 5:
1308 __ leal(left, Operand(left, left, times_4, 0));
1309 break;
1310 case 8:
1311 __ shll(left, Immediate(3));
1312 break;
1313 case 9:
1314 __ leal(left, Operand(left, left, times_8, 0));
1315 break;
1316 case 16:
1317 __ shll(left, Immediate(4));
1318 break;
1319 default:
1320 __ imull(left, left, Immediate(right_value));
1321 break;
1322 }
1323 } else {
1324 __ imull(left, left, Immediate(right_value));
1325 }
1326 } else if (right->IsStackSlot()) {
1327 if (instr->hydrogen_value()->representation().IsSmi()) {
1328 __ SmiToInteger64(left, left);
1329 __ imulp(left, ToOperand(right));
1330 } else {
1331 __ imull(left, ToOperand(right));
1332 }
1333 } else {
1334 if (instr->hydrogen_value()->representation().IsSmi()) {
1335 __ SmiToInteger64(left, left);
1336 __ imulp(left, ToRegister(right));
1337 } else {
1338 __ imull(left, ToRegister(right));
1339 }
1340 }
1341
1342 if (can_overflow) {
1343 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1344 }
1345
1346 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1347 // Bail out if the result is supposed to be negative zero.
1348 Label done;
1349 if (instr->hydrogen_value()->representation().IsSmi()) {
1350 __ testp(left, left);
1351 } else {
1352 __ testl(left, left);
1353 }
1354 __ j(not_zero, &done, Label::kNear);
1355 if (right->IsConstantOperand()) {
1356 // Constant can't be represented as 32-bit Smi due to immediate size
1357 // limit.
1358 DCHECK(SmiValuesAre32Bits()
1359 ? !instr->hydrogen_value()->representation().IsSmi()
1360 : SmiValuesAre31Bits());
1361 if (ToInteger32(LConstantOperand::cast(right)) < 0) {
1362 DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero);
1363 } else if (ToInteger32(LConstantOperand::cast(right)) == 0) {
1364 __ cmpl(kScratchRegister, Immediate(0));
1365 DeoptimizeIf(less, instr, Deoptimizer::kMinusZero);
1366 }
1367 } else if (right->IsStackSlot()) {
1368 if (instr->hydrogen_value()->representation().IsSmi()) {
1369 __ orp(kScratchRegister, ToOperand(right));
1370 } else {
1371 __ orl(kScratchRegister, ToOperand(right));
1372 }
1373 DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero);
1374 } else {
1375 // Test the non-zero operand for negative sign.
1376 if (instr->hydrogen_value()->representation().IsSmi()) {
1377 __ orp(kScratchRegister, ToRegister(right));
1378 } else {
1379 __ orl(kScratchRegister, ToRegister(right));
1380 }
1381 DeoptimizeIf(sign, instr, Deoptimizer::kMinusZero);
1382 }
1383 __ bind(&done);
1384 }
1385}
1386
1387
1388void LCodeGen::DoBitI(LBitI* instr) {
1389 LOperand* left = instr->left();
1390 LOperand* right = instr->right();
1391 DCHECK(left->Equals(instr->result()));
1392 DCHECK(left->IsRegister());
1393
1394 if (right->IsConstantOperand()) {
1395 int32_t right_operand =
1396 ToRepresentation(LConstantOperand::cast(right),
1397 instr->hydrogen()->right()->representation());
1398 switch (instr->op()) {
1399 case Token::BIT_AND:
1400 __ andl(ToRegister(left), Immediate(right_operand));
1401 break;
1402 case Token::BIT_OR:
1403 __ orl(ToRegister(left), Immediate(right_operand));
1404 break;
1405 case Token::BIT_XOR:
1406 if (right_operand == int32_t(~0)) {
1407 __ notl(ToRegister(left));
1408 } else {
1409 __ xorl(ToRegister(left), Immediate(right_operand));
1410 }
1411 break;
1412 default:
1413 UNREACHABLE();
1414 break;
1415 }
1416 } else if (right->IsStackSlot()) {
1417 switch (instr->op()) {
1418 case Token::BIT_AND:
1419 if (instr->IsInteger32()) {
1420 __ andl(ToRegister(left), ToOperand(right));
1421 } else {
1422 __ andp(ToRegister(left), ToOperand(right));
1423 }
1424 break;
1425 case Token::BIT_OR:
1426 if (instr->IsInteger32()) {
1427 __ orl(ToRegister(left), ToOperand(right));
1428 } else {
1429 __ orp(ToRegister(left), ToOperand(right));
1430 }
1431 break;
1432 case Token::BIT_XOR:
1433 if (instr->IsInteger32()) {
1434 __ xorl(ToRegister(left), ToOperand(right));
1435 } else {
1436 __ xorp(ToRegister(left), ToOperand(right));
1437 }
1438 break;
1439 default:
1440 UNREACHABLE();
1441 break;
1442 }
1443 } else {
1444 DCHECK(right->IsRegister());
1445 switch (instr->op()) {
1446 case Token::BIT_AND:
1447 if (instr->IsInteger32()) {
1448 __ andl(ToRegister(left), ToRegister(right));
1449 } else {
1450 __ andp(ToRegister(left), ToRegister(right));
1451 }
1452 break;
1453 case Token::BIT_OR:
1454 if (instr->IsInteger32()) {
1455 __ orl(ToRegister(left), ToRegister(right));
1456 } else {
1457 __ orp(ToRegister(left), ToRegister(right));
1458 }
1459 break;
1460 case Token::BIT_XOR:
1461 if (instr->IsInteger32()) {
1462 __ xorl(ToRegister(left), ToRegister(right));
1463 } else {
1464 __ xorp(ToRegister(left), ToRegister(right));
1465 }
1466 break;
1467 default:
1468 UNREACHABLE();
1469 break;
1470 }
1471 }
1472}
1473
1474
1475void LCodeGen::DoShiftI(LShiftI* instr) {
1476 LOperand* left = instr->left();
1477 LOperand* right = instr->right();
1478 DCHECK(left->Equals(instr->result()));
1479 DCHECK(left->IsRegister());
1480 if (right->IsRegister()) {
1481 DCHECK(ToRegister(right).is(rcx));
1482
1483 switch (instr->op()) {
1484 case Token::ROR:
1485 __ rorl_cl(ToRegister(left));
1486 break;
1487 case Token::SAR:
1488 __ sarl_cl(ToRegister(left));
1489 break;
1490 case Token::SHR:
1491 __ shrl_cl(ToRegister(left));
1492 if (instr->can_deopt()) {
1493 __ testl(ToRegister(left), ToRegister(left));
1494 DeoptimizeIf(negative, instr, Deoptimizer::kNegativeValue);
1495 }
1496 break;
1497 case Token::SHL:
1498 __ shll_cl(ToRegister(left));
1499 break;
1500 default:
1501 UNREACHABLE();
1502 break;
1503 }
1504 } else {
1505 int32_t value = ToInteger32(LConstantOperand::cast(right));
1506 uint8_t shift_count = static_cast<uint8_t>(value & 0x1F);
1507 switch (instr->op()) {
1508 case Token::ROR:
1509 if (shift_count != 0) {
1510 __ rorl(ToRegister(left), Immediate(shift_count));
1511 }
1512 break;
1513 case Token::SAR:
1514 if (shift_count != 0) {
1515 __ sarl(ToRegister(left), Immediate(shift_count));
1516 }
1517 break;
1518 case Token::SHR:
1519 if (shift_count != 0) {
1520 __ shrl(ToRegister(left), Immediate(shift_count));
1521 } else if (instr->can_deopt()) {
1522 __ testl(ToRegister(left), ToRegister(left));
1523 DeoptimizeIf(negative, instr, Deoptimizer::kNegativeValue);
1524 }
1525 break;
1526 case Token::SHL:
1527 if (shift_count != 0) {
1528 if (instr->hydrogen_value()->representation().IsSmi()) {
1529 if (SmiValuesAre32Bits()) {
1530 __ shlp(ToRegister(left), Immediate(shift_count));
1531 } else {
1532 DCHECK(SmiValuesAre31Bits());
1533 if (instr->can_deopt()) {
1534 if (shift_count != 1) {
1535 __ shll(ToRegister(left), Immediate(shift_count - 1));
1536 }
1537 __ Integer32ToSmi(ToRegister(left), ToRegister(left));
1538 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1539 } else {
1540 __ shll(ToRegister(left), Immediate(shift_count));
1541 }
1542 }
1543 } else {
1544 __ shll(ToRegister(left), Immediate(shift_count));
1545 }
1546 }
1547 break;
1548 default:
1549 UNREACHABLE();
1550 break;
1551 }
1552 }
1553}
1554
1555
1556void LCodeGen::DoSubI(LSubI* instr) {
1557 LOperand* left = instr->left();
1558 LOperand* right = instr->right();
1559 DCHECK(left->Equals(instr->result()));
1560
1561 if (right->IsConstantOperand()) {
1562 int32_t right_operand =
1563 ToRepresentation(LConstantOperand::cast(right),
1564 instr->hydrogen()->right()->representation());
1565 __ subl(ToRegister(left), Immediate(right_operand));
1566 } else if (right->IsRegister()) {
1567 if (instr->hydrogen_value()->representation().IsSmi()) {
1568 __ subp(ToRegister(left), ToRegister(right));
1569 } else {
1570 __ subl(ToRegister(left), ToRegister(right));
1571 }
1572 } else {
1573 if (instr->hydrogen_value()->representation().IsSmi()) {
1574 __ subp(ToRegister(left), ToOperand(right));
1575 } else {
1576 __ subl(ToRegister(left), ToOperand(right));
1577 }
1578 }
1579
1580 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1581 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1582 }
1583}
1584
1585
1586void LCodeGen::DoConstantI(LConstantI* instr) {
1587 Register dst = ToRegister(instr->result());
1588 if (instr->value() == 0) {
1589 __ xorl(dst, dst);
1590 } else {
1591 __ movl(dst, Immediate(instr->value()));
1592 }
1593}
1594
1595
1596void LCodeGen::DoConstantS(LConstantS* instr) {
1597 __ Move(ToRegister(instr->result()), instr->value());
1598}
1599
1600
1601void LCodeGen::DoConstantD(LConstantD* instr) {
1602 __ Move(ToDoubleRegister(instr->result()), instr->bits());
1603}
1604
1605
1606void LCodeGen::DoConstantE(LConstantE* instr) {
1607 __ LoadAddress(ToRegister(instr->result()), instr->value());
1608}
1609
1610
1611void LCodeGen::DoConstantT(LConstantT* instr) {
1612 Handle<Object> object = instr->value(isolate());
1613 AllowDeferredHandleDereference smi_check;
1614 __ Move(ToRegister(instr->result()), object);
1615}
1616
1617
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001618Operand LCodeGen::BuildSeqStringOperand(Register string,
1619 LOperand* index,
1620 String::Encoding encoding) {
1621 if (index->IsConstantOperand()) {
1622 int offset = ToInteger32(LConstantOperand::cast(index));
1623 if (encoding == String::TWO_BYTE_ENCODING) {
1624 offset *= kUC16Size;
1625 }
1626 STATIC_ASSERT(kCharSize == 1);
1627 return FieldOperand(string, SeqString::kHeaderSize + offset);
1628 }
1629 return FieldOperand(
1630 string, ToRegister(index),
1631 encoding == String::ONE_BYTE_ENCODING ? times_1 : times_2,
1632 SeqString::kHeaderSize);
1633}
1634
1635
1636void LCodeGen::DoSeqStringGetChar(LSeqStringGetChar* instr) {
1637 String::Encoding encoding = instr->hydrogen()->encoding();
1638 Register result = ToRegister(instr->result());
1639 Register string = ToRegister(instr->string());
1640
1641 if (FLAG_debug_code) {
1642 __ Push(string);
1643 __ movp(string, FieldOperand(string, HeapObject::kMapOffset));
1644 __ movzxbp(string, FieldOperand(string, Map::kInstanceTypeOffset));
1645
1646 __ andb(string, Immediate(kStringRepresentationMask | kStringEncodingMask));
1647 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1648 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1649 __ cmpp(string, Immediate(encoding == String::ONE_BYTE_ENCODING
1650 ? one_byte_seq_type : two_byte_seq_type));
1651 __ Check(equal, kUnexpectedStringType);
1652 __ Pop(string);
1653 }
1654
1655 Operand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1656 if (encoding == String::ONE_BYTE_ENCODING) {
1657 __ movzxbl(result, operand);
1658 } else {
1659 __ movzxwl(result, operand);
1660 }
1661}
1662
1663
1664void LCodeGen::DoSeqStringSetChar(LSeqStringSetChar* instr) {
1665 String::Encoding encoding = instr->hydrogen()->encoding();
1666 Register string = ToRegister(instr->string());
1667
1668 if (FLAG_debug_code) {
1669 Register value = ToRegister(instr->value());
1670 Register index = ToRegister(instr->index());
1671 static const uint32_t one_byte_seq_type = kSeqStringTag | kOneByteStringTag;
1672 static const uint32_t two_byte_seq_type = kSeqStringTag | kTwoByteStringTag;
1673 int encoding_mask =
1674 instr->hydrogen()->encoding() == String::ONE_BYTE_ENCODING
1675 ? one_byte_seq_type : two_byte_seq_type;
1676 __ EmitSeqStringSetCharCheck(string, index, value, encoding_mask);
1677 }
1678
1679 Operand operand = BuildSeqStringOperand(string, instr->index(), encoding);
1680 if (instr->value()->IsConstantOperand()) {
1681 int value = ToInteger32(LConstantOperand::cast(instr->value()));
1682 DCHECK_LE(0, value);
1683 if (encoding == String::ONE_BYTE_ENCODING) {
1684 DCHECK_LE(value, String::kMaxOneByteCharCode);
1685 __ movb(operand, Immediate(value));
1686 } else {
1687 DCHECK_LE(value, String::kMaxUtf16CodeUnit);
1688 __ movw(operand, Immediate(value));
1689 }
1690 } else {
1691 Register value = ToRegister(instr->value());
1692 if (encoding == String::ONE_BYTE_ENCODING) {
1693 __ movb(operand, value);
1694 } else {
1695 __ movw(operand, value);
1696 }
1697 }
1698}
1699
1700
1701void LCodeGen::DoAddI(LAddI* instr) {
1702 LOperand* left = instr->left();
1703 LOperand* right = instr->right();
1704
1705 Representation target_rep = instr->hydrogen()->representation();
1706 bool is_p = target_rep.IsSmi() || target_rep.IsExternal();
1707
1708 if (LAddI::UseLea(instr->hydrogen()) && !left->Equals(instr->result())) {
1709 if (right->IsConstantOperand()) {
1710 // No support for smi-immediates for 32-bit SMI.
1711 DCHECK(SmiValuesAre32Bits() ? !target_rep.IsSmi() : SmiValuesAre31Bits());
1712 int32_t offset =
1713 ToRepresentation(LConstantOperand::cast(right),
1714 instr->hydrogen()->right()->representation());
1715 if (is_p) {
1716 __ leap(ToRegister(instr->result()),
1717 MemOperand(ToRegister(left), offset));
1718 } else {
1719 __ leal(ToRegister(instr->result()),
1720 MemOperand(ToRegister(left), offset));
1721 }
1722 } else {
1723 Operand address(ToRegister(left), ToRegister(right), times_1, 0);
1724 if (is_p) {
1725 __ leap(ToRegister(instr->result()), address);
1726 } else {
1727 __ leal(ToRegister(instr->result()), address);
1728 }
1729 }
1730 } else {
1731 if (right->IsConstantOperand()) {
1732 // No support for smi-immediates for 32-bit SMI.
1733 DCHECK(SmiValuesAre32Bits() ? !target_rep.IsSmi() : SmiValuesAre31Bits());
1734 int32_t right_operand =
1735 ToRepresentation(LConstantOperand::cast(right),
1736 instr->hydrogen()->right()->representation());
1737 if (is_p) {
1738 __ addp(ToRegister(left), Immediate(right_operand));
1739 } else {
1740 __ addl(ToRegister(left), Immediate(right_operand));
1741 }
1742 } else if (right->IsRegister()) {
1743 if (is_p) {
1744 __ addp(ToRegister(left), ToRegister(right));
1745 } else {
1746 __ addl(ToRegister(left), ToRegister(right));
1747 }
1748 } else {
1749 if (is_p) {
1750 __ addp(ToRegister(left), ToOperand(right));
1751 } else {
1752 __ addl(ToRegister(left), ToOperand(right));
1753 }
1754 }
1755 if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
1756 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
1757 }
1758 }
1759}
1760
1761
1762void LCodeGen::DoMathMinMax(LMathMinMax* instr) {
1763 LOperand* left = instr->left();
1764 LOperand* right = instr->right();
1765 DCHECK(left->Equals(instr->result()));
1766 HMathMinMax::Operation operation = instr->hydrogen()->operation();
1767 if (instr->hydrogen()->representation().IsSmiOrInteger32()) {
1768 Label return_left;
1769 Condition condition = (operation == HMathMinMax::kMathMin)
1770 ? less_equal
1771 : greater_equal;
1772 Register left_reg = ToRegister(left);
1773 if (right->IsConstantOperand()) {
1774 Immediate right_imm = Immediate(
1775 ToRepresentation(LConstantOperand::cast(right),
1776 instr->hydrogen()->right()->representation()));
1777 DCHECK(SmiValuesAre32Bits()
1778 ? !instr->hydrogen()->representation().IsSmi()
1779 : SmiValuesAre31Bits());
1780 __ cmpl(left_reg, right_imm);
1781 __ j(condition, &return_left, Label::kNear);
1782 __ movp(left_reg, right_imm);
1783 } else if (right->IsRegister()) {
1784 Register right_reg = ToRegister(right);
1785 if (instr->hydrogen_value()->representation().IsSmi()) {
1786 __ cmpp(left_reg, right_reg);
1787 } else {
1788 __ cmpl(left_reg, right_reg);
1789 }
1790 __ j(condition, &return_left, Label::kNear);
1791 __ movp(left_reg, right_reg);
1792 } else {
1793 Operand right_op = ToOperand(right);
1794 if (instr->hydrogen_value()->representation().IsSmi()) {
1795 __ cmpp(left_reg, right_op);
1796 } else {
1797 __ cmpl(left_reg, right_op);
1798 }
1799 __ j(condition, &return_left, Label::kNear);
1800 __ movp(left_reg, right_op);
1801 }
1802 __ bind(&return_left);
1803 } else {
1804 DCHECK(instr->hydrogen()->representation().IsDouble());
1805 Label not_nan, distinct, return_left, return_right;
1806 Condition condition = (operation == HMathMinMax::kMathMin) ? below : above;
1807 XMMRegister left_reg = ToDoubleRegister(left);
1808 XMMRegister right_reg = ToDoubleRegister(right);
1809 __ Ucomisd(left_reg, right_reg);
1810 __ j(parity_odd, &not_nan, Label::kNear); // Both are not NaN.
1811
1812 // One of the numbers is NaN. Find which one and return it.
1813 __ Ucomisd(left_reg, left_reg);
1814 __ j(parity_even, &return_left, Label::kNear); // left is NaN.
1815 __ jmp(&return_right, Label::kNear); // right is NaN.
1816
1817 __ bind(&not_nan);
1818 __ j(not_equal, &distinct, Label::kNear); // left != right.
1819
1820 // left == right
1821 XMMRegister xmm_scratch = double_scratch0();
1822 __ Xorpd(xmm_scratch, xmm_scratch);
1823 __ Ucomisd(left_reg, xmm_scratch);
1824 __ j(not_equal, &return_left, Label::kNear); // left == right != 0.
1825
1826 // At this point, both left and right are either +0 or -0.
1827 if (operation == HMathMinMax::kMathMin) {
1828 __ Orpd(left_reg, right_reg);
1829 } else {
1830 __ Andpd(left_reg, right_reg);
1831 }
1832 __ jmp(&return_left, Label::kNear);
1833
1834 __ bind(&distinct);
1835 __ j(condition, &return_left, Label::kNear);
1836
1837 __ bind(&return_right);
1838 __ Movapd(left_reg, right_reg);
1839
1840 __ bind(&return_left);
1841 }
1842}
1843
1844
1845void LCodeGen::DoArithmeticD(LArithmeticD* instr) {
1846 XMMRegister left = ToDoubleRegister(instr->left());
1847 XMMRegister right = ToDoubleRegister(instr->right());
1848 XMMRegister result = ToDoubleRegister(instr->result());
1849 switch (instr->op()) {
1850 case Token::ADD:
1851 if (CpuFeatures::IsSupported(AVX)) {
1852 CpuFeatureScope scope(masm(), AVX);
1853 __ vaddsd(result, left, right);
1854 } else {
1855 DCHECK(result.is(left));
1856 __ addsd(left, right);
1857 }
1858 break;
1859 case Token::SUB:
1860 if (CpuFeatures::IsSupported(AVX)) {
1861 CpuFeatureScope scope(masm(), AVX);
1862 __ vsubsd(result, left, right);
1863 } else {
1864 DCHECK(result.is(left));
1865 __ subsd(left, right);
1866 }
1867 break;
1868 case Token::MUL:
1869 if (CpuFeatures::IsSupported(AVX)) {
1870 CpuFeatureScope scope(masm(), AVX);
1871 __ vmulsd(result, left, right);
1872 } else {
1873 DCHECK(result.is(left));
1874 __ mulsd(left, right);
1875 }
1876 break;
1877 case Token::DIV:
1878 if (CpuFeatures::IsSupported(AVX)) {
1879 CpuFeatureScope scope(masm(), AVX);
1880 __ vdivsd(result, left, right);
1881 } else {
1882 DCHECK(result.is(left));
1883 __ divsd(left, right);
1884 }
1885 // Don't delete this mov. It may improve performance on some CPUs,
1886 // when there is a (v)mulsd depending on the result
1887 __ Movapd(result, result);
1888 break;
1889 case Token::MOD: {
1890 XMMRegister xmm_scratch = double_scratch0();
1891 __ PrepareCallCFunction(2);
1892 __ Movapd(xmm_scratch, left);
1893 DCHECK(right.is(xmm1));
1894 __ CallCFunction(
1895 ExternalReference::mod_two_doubles_operation(isolate()), 2);
1896 __ Movapd(result, xmm_scratch);
1897 break;
1898 }
1899 default:
1900 UNREACHABLE();
1901 break;
1902 }
1903}
1904
1905
1906void LCodeGen::DoArithmeticT(LArithmeticT* instr) {
1907 DCHECK(ToRegister(instr->context()).is(rsi));
1908 DCHECK(ToRegister(instr->left()).is(rdx));
1909 DCHECK(ToRegister(instr->right()).is(rax));
1910 DCHECK(ToRegister(instr->result()).is(rax));
1911
Ben Murdoch097c5b22016-05-18 11:27:45 +01001912 Handle<Code> code = CodeFactory::BinaryOpIC(isolate(), instr->op()).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001913 CallCode(code, RelocInfo::CODE_TARGET, instr);
1914}
1915
1916
1917template<class InstrType>
1918void LCodeGen::EmitBranch(InstrType instr, Condition cc) {
1919 int left_block = instr->TrueDestination(chunk_);
1920 int right_block = instr->FalseDestination(chunk_);
1921
1922 int next_block = GetNextEmittedBlock();
1923
1924 if (right_block == left_block || cc == no_condition) {
1925 EmitGoto(left_block);
1926 } else if (left_block == next_block) {
1927 __ j(NegateCondition(cc), chunk_->GetAssemblyLabel(right_block));
1928 } else if (right_block == next_block) {
1929 __ j(cc, chunk_->GetAssemblyLabel(left_block));
1930 } else {
1931 __ j(cc, chunk_->GetAssemblyLabel(left_block));
1932 if (cc != always) {
1933 __ jmp(chunk_->GetAssemblyLabel(right_block));
1934 }
1935 }
1936}
1937
1938
1939template <class InstrType>
1940void LCodeGen::EmitTrueBranch(InstrType instr, Condition cc) {
1941 int true_block = instr->TrueDestination(chunk_);
1942 __ j(cc, chunk_->GetAssemblyLabel(true_block));
1943}
1944
1945
1946template <class InstrType>
1947void LCodeGen::EmitFalseBranch(InstrType instr, Condition cc) {
1948 int false_block = instr->FalseDestination(chunk_);
1949 __ j(cc, chunk_->GetAssemblyLabel(false_block));
1950}
1951
1952
1953void LCodeGen::DoDebugBreak(LDebugBreak* instr) {
1954 __ int3();
1955}
1956
1957
1958void LCodeGen::DoBranch(LBranch* instr) {
1959 Representation r = instr->hydrogen()->value()->representation();
1960 if (r.IsInteger32()) {
1961 DCHECK(!info()->IsStub());
1962 Register reg = ToRegister(instr->value());
1963 __ testl(reg, reg);
1964 EmitBranch(instr, not_zero);
1965 } else if (r.IsSmi()) {
1966 DCHECK(!info()->IsStub());
1967 Register reg = ToRegister(instr->value());
1968 __ testp(reg, reg);
1969 EmitBranch(instr, not_zero);
1970 } else if (r.IsDouble()) {
1971 DCHECK(!info()->IsStub());
1972 XMMRegister reg = ToDoubleRegister(instr->value());
1973 XMMRegister xmm_scratch = double_scratch0();
1974 __ Xorpd(xmm_scratch, xmm_scratch);
1975 __ Ucomisd(reg, xmm_scratch);
1976 EmitBranch(instr, not_equal);
1977 } else {
1978 DCHECK(r.IsTagged());
1979 Register reg = ToRegister(instr->value());
1980 HType type = instr->hydrogen()->value()->type();
1981 if (type.IsBoolean()) {
1982 DCHECK(!info()->IsStub());
1983 __ CompareRoot(reg, Heap::kTrueValueRootIndex);
1984 EmitBranch(instr, equal);
1985 } else if (type.IsSmi()) {
1986 DCHECK(!info()->IsStub());
1987 __ SmiCompare(reg, Smi::FromInt(0));
1988 EmitBranch(instr, not_equal);
1989 } else if (type.IsJSArray()) {
1990 DCHECK(!info()->IsStub());
1991 EmitBranch(instr, no_condition);
1992 } else if (type.IsHeapNumber()) {
1993 DCHECK(!info()->IsStub());
1994 XMMRegister xmm_scratch = double_scratch0();
1995 __ Xorpd(xmm_scratch, xmm_scratch);
1996 __ Ucomisd(xmm_scratch, FieldOperand(reg, HeapNumber::kValueOffset));
1997 EmitBranch(instr, not_equal);
1998 } else if (type.IsString()) {
1999 DCHECK(!info()->IsStub());
2000 __ cmpp(FieldOperand(reg, String::kLengthOffset), Immediate(0));
2001 EmitBranch(instr, not_equal);
2002 } else {
Ben Murdochda12d292016-06-02 14:46:10 +01002003 ToBooleanICStub::Types expected =
2004 instr->hydrogen()->expected_input_types();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002005 // Avoid deopts in the case where we've never executed this path before.
Ben Murdochda12d292016-06-02 14:46:10 +01002006 if (expected.IsEmpty()) expected = ToBooleanICStub::Types::Generic();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002007
Ben Murdochda12d292016-06-02 14:46:10 +01002008 if (expected.Contains(ToBooleanICStub::UNDEFINED)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002009 // undefined -> false.
2010 __ CompareRoot(reg, Heap::kUndefinedValueRootIndex);
2011 __ j(equal, instr->FalseLabel(chunk_));
2012 }
Ben Murdochda12d292016-06-02 14:46:10 +01002013 if (expected.Contains(ToBooleanICStub::BOOLEAN)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002014 // true -> true.
2015 __ CompareRoot(reg, Heap::kTrueValueRootIndex);
2016 __ j(equal, instr->TrueLabel(chunk_));
2017 // false -> false.
2018 __ CompareRoot(reg, Heap::kFalseValueRootIndex);
2019 __ j(equal, instr->FalseLabel(chunk_));
2020 }
Ben Murdochda12d292016-06-02 14:46:10 +01002021 if (expected.Contains(ToBooleanICStub::NULL_TYPE)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002022 // 'null' -> false.
2023 __ CompareRoot(reg, Heap::kNullValueRootIndex);
2024 __ j(equal, instr->FalseLabel(chunk_));
2025 }
2026
Ben Murdochda12d292016-06-02 14:46:10 +01002027 if (expected.Contains(ToBooleanICStub::SMI)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002028 // Smis: 0 -> false, all other -> true.
2029 __ Cmp(reg, Smi::FromInt(0));
2030 __ j(equal, instr->FalseLabel(chunk_));
2031 __ JumpIfSmi(reg, instr->TrueLabel(chunk_));
2032 } else if (expected.NeedsMap()) {
2033 // If we need a map later and have a Smi -> deopt.
2034 __ testb(reg, Immediate(kSmiTagMask));
2035 DeoptimizeIf(zero, instr, Deoptimizer::kSmi);
2036 }
2037
2038 const Register map = kScratchRegister;
2039 if (expected.NeedsMap()) {
2040 __ movp(map, FieldOperand(reg, HeapObject::kMapOffset));
2041
2042 if (expected.CanBeUndetectable()) {
2043 // Undetectable -> false.
2044 __ testb(FieldOperand(map, Map::kBitFieldOffset),
2045 Immediate(1 << Map::kIsUndetectable));
2046 __ j(not_zero, instr->FalseLabel(chunk_));
2047 }
2048 }
2049
Ben Murdochda12d292016-06-02 14:46:10 +01002050 if (expected.Contains(ToBooleanICStub::SPEC_OBJECT)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002051 // spec object -> true.
2052 __ CmpInstanceType(map, FIRST_JS_RECEIVER_TYPE);
2053 __ j(above_equal, instr->TrueLabel(chunk_));
2054 }
2055
Ben Murdochda12d292016-06-02 14:46:10 +01002056 if (expected.Contains(ToBooleanICStub::STRING)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002057 // String value -> false iff empty.
2058 Label not_string;
2059 __ CmpInstanceType(map, FIRST_NONSTRING_TYPE);
2060 __ j(above_equal, &not_string, Label::kNear);
2061 __ cmpp(FieldOperand(reg, String::kLengthOffset), Immediate(0));
2062 __ j(not_zero, instr->TrueLabel(chunk_));
2063 __ jmp(instr->FalseLabel(chunk_));
2064 __ bind(&not_string);
2065 }
2066
Ben Murdochda12d292016-06-02 14:46:10 +01002067 if (expected.Contains(ToBooleanICStub::SYMBOL)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002068 // Symbol value -> true.
2069 __ CmpInstanceType(map, SYMBOL_TYPE);
2070 __ j(equal, instr->TrueLabel(chunk_));
2071 }
2072
Ben Murdochda12d292016-06-02 14:46:10 +01002073 if (expected.Contains(ToBooleanICStub::SIMD_VALUE)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002074 // SIMD value -> true.
2075 __ CmpInstanceType(map, SIMD128_VALUE_TYPE);
2076 __ j(equal, instr->TrueLabel(chunk_));
2077 }
2078
Ben Murdochda12d292016-06-02 14:46:10 +01002079 if (expected.Contains(ToBooleanICStub::HEAP_NUMBER)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002080 // heap number -> false iff +0, -0, or NaN.
2081 Label not_heap_number;
2082 __ CompareRoot(map, Heap::kHeapNumberMapRootIndex);
2083 __ j(not_equal, &not_heap_number, Label::kNear);
2084 XMMRegister xmm_scratch = double_scratch0();
2085 __ Xorpd(xmm_scratch, xmm_scratch);
2086 __ Ucomisd(xmm_scratch, FieldOperand(reg, HeapNumber::kValueOffset));
2087 __ j(zero, instr->FalseLabel(chunk_));
2088 __ jmp(instr->TrueLabel(chunk_));
2089 __ bind(&not_heap_number);
2090 }
2091
2092 if (!expected.IsGeneric()) {
2093 // We've seen something for the first time -> deopt.
2094 // This can only happen if we are not generic already.
2095 DeoptimizeIf(no_condition, instr, Deoptimizer::kUnexpectedObject);
2096 }
2097 }
2098 }
2099}
2100
2101
2102void LCodeGen::EmitGoto(int block) {
2103 if (!IsNextEmittedBlock(block)) {
2104 __ jmp(chunk_->GetAssemblyLabel(chunk_->LookupDestination(block)));
2105 }
2106}
2107
2108
2109void LCodeGen::DoGoto(LGoto* instr) {
2110 EmitGoto(instr->block_id());
2111}
2112
2113
2114inline Condition LCodeGen::TokenToCondition(Token::Value op, bool is_unsigned) {
2115 Condition cond = no_condition;
2116 switch (op) {
2117 case Token::EQ:
2118 case Token::EQ_STRICT:
2119 cond = equal;
2120 break;
2121 case Token::NE:
2122 case Token::NE_STRICT:
2123 cond = not_equal;
2124 break;
2125 case Token::LT:
2126 cond = is_unsigned ? below : less;
2127 break;
2128 case Token::GT:
2129 cond = is_unsigned ? above : greater;
2130 break;
2131 case Token::LTE:
2132 cond = is_unsigned ? below_equal : less_equal;
2133 break;
2134 case Token::GTE:
2135 cond = is_unsigned ? above_equal : greater_equal;
2136 break;
2137 case Token::IN:
2138 case Token::INSTANCEOF:
2139 default:
2140 UNREACHABLE();
2141 }
2142 return cond;
2143}
2144
2145
2146void LCodeGen::DoCompareNumericAndBranch(LCompareNumericAndBranch* instr) {
2147 LOperand* left = instr->left();
2148 LOperand* right = instr->right();
2149 bool is_unsigned =
2150 instr->is_double() ||
2151 instr->hydrogen()->left()->CheckFlag(HInstruction::kUint32) ||
2152 instr->hydrogen()->right()->CheckFlag(HInstruction::kUint32);
2153 Condition cc = TokenToCondition(instr->op(), is_unsigned);
2154
2155 if (left->IsConstantOperand() && right->IsConstantOperand()) {
2156 // We can statically evaluate the comparison.
2157 double left_val = ToDouble(LConstantOperand::cast(left));
2158 double right_val = ToDouble(LConstantOperand::cast(right));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002159 int next_block = Token::EvalComparison(instr->op(), left_val, right_val)
2160 ? instr->TrueDestination(chunk_)
2161 : instr->FalseDestination(chunk_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002162 EmitGoto(next_block);
2163 } else {
2164 if (instr->is_double()) {
2165 // Don't base result on EFLAGS when a NaN is involved. Instead
2166 // jump to the false block.
2167 __ Ucomisd(ToDoubleRegister(left), ToDoubleRegister(right));
2168 __ j(parity_even, instr->FalseLabel(chunk_));
2169 } else {
2170 int32_t value;
2171 if (right->IsConstantOperand()) {
2172 value = ToInteger32(LConstantOperand::cast(right));
2173 if (instr->hydrogen_value()->representation().IsSmi()) {
2174 __ Cmp(ToRegister(left), Smi::FromInt(value));
2175 } else {
2176 __ cmpl(ToRegister(left), Immediate(value));
2177 }
2178 } else if (left->IsConstantOperand()) {
2179 value = ToInteger32(LConstantOperand::cast(left));
2180 if (instr->hydrogen_value()->representation().IsSmi()) {
2181 if (right->IsRegister()) {
2182 __ Cmp(ToRegister(right), Smi::FromInt(value));
2183 } else {
2184 __ Cmp(ToOperand(right), Smi::FromInt(value));
2185 }
2186 } else if (right->IsRegister()) {
2187 __ cmpl(ToRegister(right), Immediate(value));
2188 } else {
2189 __ cmpl(ToOperand(right), Immediate(value));
2190 }
2191 // We commuted the operands, so commute the condition.
2192 cc = CommuteCondition(cc);
2193 } else if (instr->hydrogen_value()->representation().IsSmi()) {
2194 if (right->IsRegister()) {
2195 __ cmpp(ToRegister(left), ToRegister(right));
2196 } else {
2197 __ cmpp(ToRegister(left), ToOperand(right));
2198 }
2199 } else {
2200 if (right->IsRegister()) {
2201 __ cmpl(ToRegister(left), ToRegister(right));
2202 } else {
2203 __ cmpl(ToRegister(left), ToOperand(right));
2204 }
2205 }
2206 }
2207 EmitBranch(instr, cc);
2208 }
2209}
2210
2211
2212void LCodeGen::DoCmpObjectEqAndBranch(LCmpObjectEqAndBranch* instr) {
2213 Register left = ToRegister(instr->left());
2214
2215 if (instr->right()->IsConstantOperand()) {
2216 Handle<Object> right = ToHandle(LConstantOperand::cast(instr->right()));
2217 __ Cmp(left, right);
2218 } else {
2219 Register right = ToRegister(instr->right());
2220 __ cmpp(left, right);
2221 }
2222 EmitBranch(instr, equal);
2223}
2224
2225
2226void LCodeGen::DoCmpHoleAndBranch(LCmpHoleAndBranch* instr) {
2227 if (instr->hydrogen()->representation().IsTagged()) {
2228 Register input_reg = ToRegister(instr->object());
2229 __ Cmp(input_reg, factory()->the_hole_value());
2230 EmitBranch(instr, equal);
2231 return;
2232 }
2233
2234 XMMRegister input_reg = ToDoubleRegister(instr->object());
2235 __ Ucomisd(input_reg, input_reg);
2236 EmitFalseBranch(instr, parity_odd);
2237
2238 __ subp(rsp, Immediate(kDoubleSize));
2239 __ Movsd(MemOperand(rsp, 0), input_reg);
2240 __ addp(rsp, Immediate(kDoubleSize));
2241
2242 int offset = sizeof(kHoleNanUpper32);
2243 __ cmpl(MemOperand(rsp, -offset), Immediate(kHoleNanUpper32));
2244 EmitBranch(instr, equal);
2245}
2246
2247
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002248Condition LCodeGen::EmitIsString(Register input,
2249 Register temp1,
2250 Label* is_not_string,
2251 SmiCheck check_needed = INLINE_SMI_CHECK) {
2252 if (check_needed == INLINE_SMI_CHECK) {
2253 __ JumpIfSmi(input, is_not_string);
2254 }
2255
2256 Condition cond = masm_->IsObjectStringType(input, temp1, temp1);
2257
2258 return cond;
2259}
2260
2261
2262void LCodeGen::DoIsStringAndBranch(LIsStringAndBranch* instr) {
2263 Register reg = ToRegister(instr->value());
2264 Register temp = ToRegister(instr->temp());
2265
2266 SmiCheck check_needed =
2267 instr->hydrogen()->value()->type().IsHeapObject()
2268 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2269
2270 Condition true_cond = EmitIsString(
2271 reg, temp, instr->FalseLabel(chunk_), check_needed);
2272
2273 EmitBranch(instr, true_cond);
2274}
2275
2276
2277void LCodeGen::DoIsSmiAndBranch(LIsSmiAndBranch* instr) {
2278 Condition is_smi;
2279 if (instr->value()->IsRegister()) {
2280 Register input = ToRegister(instr->value());
2281 is_smi = masm()->CheckSmi(input);
2282 } else {
2283 Operand input = ToOperand(instr->value());
2284 is_smi = masm()->CheckSmi(input);
2285 }
2286 EmitBranch(instr, is_smi);
2287}
2288
2289
2290void LCodeGen::DoIsUndetectableAndBranch(LIsUndetectableAndBranch* instr) {
2291 Register input = ToRegister(instr->value());
2292 Register temp = ToRegister(instr->temp());
2293
2294 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2295 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2296 }
2297 __ movp(temp, FieldOperand(input, HeapObject::kMapOffset));
2298 __ testb(FieldOperand(temp, Map::kBitFieldOffset),
2299 Immediate(1 << Map::kIsUndetectable));
2300 EmitBranch(instr, not_zero);
2301}
2302
2303
2304void LCodeGen::DoStringCompareAndBranch(LStringCompareAndBranch* instr) {
2305 DCHECK(ToRegister(instr->context()).is(rsi));
2306 DCHECK(ToRegister(instr->left()).is(rdx));
2307 DCHECK(ToRegister(instr->right()).is(rax));
2308
Ben Murdochda12d292016-06-02 14:46:10 +01002309 Handle<Code> code = CodeFactory::StringCompare(isolate(), instr->op()).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002310 CallCode(code, RelocInfo::CODE_TARGET, instr);
Ben Murdochda12d292016-06-02 14:46:10 +01002311 __ CompareRoot(rax, Heap::kTrueValueRootIndex);
2312 EmitBranch(instr, equal);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002313}
2314
2315
2316static InstanceType TestType(HHasInstanceTypeAndBranch* instr) {
2317 InstanceType from = instr->from();
2318 InstanceType to = instr->to();
2319 if (from == FIRST_TYPE) return to;
2320 DCHECK(from == to || to == LAST_TYPE);
2321 return from;
2322}
2323
2324
2325static Condition BranchCondition(HHasInstanceTypeAndBranch* instr) {
2326 InstanceType from = instr->from();
2327 InstanceType to = instr->to();
2328 if (from == to) return equal;
2329 if (to == LAST_TYPE) return above_equal;
2330 if (from == FIRST_TYPE) return below_equal;
2331 UNREACHABLE();
2332 return equal;
2333}
2334
2335
2336void LCodeGen::DoHasInstanceTypeAndBranch(LHasInstanceTypeAndBranch* instr) {
2337 Register input = ToRegister(instr->value());
2338
2339 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
2340 __ JumpIfSmi(input, instr->FalseLabel(chunk_));
2341 }
2342
2343 __ CmpObjectType(input, TestType(instr->hydrogen()), kScratchRegister);
2344 EmitBranch(instr, BranchCondition(instr->hydrogen()));
2345}
2346
2347
2348void LCodeGen::DoGetCachedArrayIndex(LGetCachedArrayIndex* instr) {
2349 Register input = ToRegister(instr->value());
2350 Register result = ToRegister(instr->result());
2351
2352 __ AssertString(input);
2353
2354 __ movl(result, FieldOperand(input, String::kHashFieldOffset));
2355 DCHECK(String::kHashShift >= kSmiTagSize);
2356 __ IndexFromHash(result, result);
2357}
2358
2359
2360void LCodeGen::DoHasCachedArrayIndexAndBranch(
2361 LHasCachedArrayIndexAndBranch* instr) {
2362 Register input = ToRegister(instr->value());
2363
2364 __ testl(FieldOperand(input, String::kHashFieldOffset),
2365 Immediate(String::kContainsCachedArrayIndexMask));
2366 EmitBranch(instr, equal);
2367}
2368
2369
2370// Branches to a label or falls through with the answer in the z flag.
2371// Trashes the temp register.
2372void LCodeGen::EmitClassOfTest(Label* is_true,
2373 Label* is_false,
2374 Handle<String> class_name,
2375 Register input,
2376 Register temp,
2377 Register temp2) {
2378 DCHECK(!input.is(temp));
2379 DCHECK(!input.is(temp2));
2380 DCHECK(!temp.is(temp2));
2381
2382 __ JumpIfSmi(input, is_false);
2383
Ben Murdochda12d292016-06-02 14:46:10 +01002384 __ CmpObjectType(input, FIRST_FUNCTION_TYPE, temp);
2385 STATIC_ASSERT(LAST_FUNCTION_TYPE == LAST_TYPE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002386 if (String::Equals(isolate()->factory()->Function_string(), class_name)) {
Ben Murdochda12d292016-06-02 14:46:10 +01002387 __ j(above_equal, is_true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002388 } else {
Ben Murdochda12d292016-06-02 14:46:10 +01002389 __ j(above_equal, is_false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002390 }
2391
2392 // Check if the constructor in the map is a function.
2393 __ GetMapConstructor(temp, temp, kScratchRegister);
2394
2395 // Objects with a non-function constructor have class 'Object'.
2396 __ CmpInstanceType(kScratchRegister, JS_FUNCTION_TYPE);
2397 if (String::Equals(class_name, isolate()->factory()->Object_string())) {
2398 __ j(not_equal, is_true);
2399 } else {
2400 __ j(not_equal, is_false);
2401 }
2402
2403 // temp now contains the constructor function. Grab the
2404 // instance class name from there.
2405 __ movp(temp, FieldOperand(temp, JSFunction::kSharedFunctionInfoOffset));
2406 __ movp(temp, FieldOperand(temp,
2407 SharedFunctionInfo::kInstanceClassNameOffset));
2408 // The class name we are testing against is internalized since it's a literal.
2409 // The name in the constructor is internalized because of the way the context
2410 // is booted. This routine isn't expected to work for random API-created
2411 // classes and it doesn't have to because you can't access it with natives
2412 // syntax. Since both sides are internalized it is sufficient to use an
2413 // identity comparison.
2414 DCHECK(class_name->IsInternalizedString());
2415 __ Cmp(temp, class_name);
2416 // End with the answer in the z flag.
2417}
2418
2419
2420void LCodeGen::DoClassOfTestAndBranch(LClassOfTestAndBranch* instr) {
2421 Register input = ToRegister(instr->value());
2422 Register temp = ToRegister(instr->temp());
2423 Register temp2 = ToRegister(instr->temp2());
2424 Handle<String> class_name = instr->hydrogen()->class_name();
2425
2426 EmitClassOfTest(instr->TrueLabel(chunk_), instr->FalseLabel(chunk_),
2427 class_name, input, temp, temp2);
2428
2429 EmitBranch(instr, equal);
2430}
2431
2432
2433void LCodeGen::DoCmpMapAndBranch(LCmpMapAndBranch* instr) {
2434 Register reg = ToRegister(instr->value());
2435
2436 __ Cmp(FieldOperand(reg, HeapObject::kMapOffset), instr->map());
2437 EmitBranch(instr, equal);
2438}
2439
2440
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002441void LCodeGen::DoHasInPrototypeChainAndBranch(
2442 LHasInPrototypeChainAndBranch* instr) {
2443 Register const object = ToRegister(instr->object());
2444 Register const object_map = kScratchRegister;
2445 Register const object_prototype = object_map;
2446 Register const prototype = ToRegister(instr->prototype());
2447
2448 // The {object} must be a spec object. It's sufficient to know that {object}
2449 // is not a smi, since all other non-spec objects have {null} prototypes and
2450 // will be ruled out below.
2451 if (instr->hydrogen()->ObjectNeedsSmiCheck()) {
2452 Condition is_smi = __ CheckSmi(object);
2453 EmitFalseBranch(instr, is_smi);
2454 }
2455
2456 // Loop through the {object}s prototype chain looking for the {prototype}.
2457 __ movp(object_map, FieldOperand(object, HeapObject::kMapOffset));
2458 Label loop;
2459 __ bind(&loop);
2460
2461
2462 // Deoptimize if the object needs to be access checked.
2463 __ testb(FieldOperand(object_map, Map::kBitFieldOffset),
2464 Immediate(1 << Map::kIsAccessCheckNeeded));
2465 DeoptimizeIf(not_zero, instr, Deoptimizer::kAccessCheck);
2466 // Deoptimize for proxies.
2467 __ CmpInstanceType(object_map, JS_PROXY_TYPE);
2468 DeoptimizeIf(equal, instr, Deoptimizer::kProxy);
2469
2470 __ movp(object_prototype, FieldOperand(object_map, Map::kPrototypeOffset));
2471 __ cmpp(object_prototype, prototype);
2472 EmitTrueBranch(instr, equal);
2473 __ CompareRoot(object_prototype, Heap::kNullValueRootIndex);
2474 EmitFalseBranch(instr, equal);
2475 __ movp(object_map, FieldOperand(object_prototype, HeapObject::kMapOffset));
2476 __ jmp(&loop);
2477}
2478
2479
2480void LCodeGen::DoCmpT(LCmpT* instr) {
2481 DCHECK(ToRegister(instr->context()).is(rsi));
2482 Token::Value op = instr->op();
2483
Ben Murdoch097c5b22016-05-18 11:27:45 +01002484 Handle<Code> ic = CodeFactory::CompareIC(isolate(), op).code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002485 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2486
2487 Condition condition = TokenToCondition(op, false);
2488 Label true_value, done;
2489 __ testp(rax, rax);
2490 __ j(condition, &true_value, Label::kNear);
2491 __ LoadRoot(ToRegister(instr->result()), Heap::kFalseValueRootIndex);
2492 __ jmp(&done, Label::kNear);
2493 __ bind(&true_value);
2494 __ LoadRoot(ToRegister(instr->result()), Heap::kTrueValueRootIndex);
2495 __ bind(&done);
2496}
2497
2498
2499void LCodeGen::DoReturn(LReturn* instr) {
2500 if (FLAG_trace && info()->IsOptimizing()) {
2501 // Preserve the return value on the stack and rely on the runtime call
2502 // to return the value in the same register. We're leaving the code
2503 // managed by the register allocator and tearing down the frame, it's
2504 // safe to write to the context register.
2505 __ Push(rax);
2506 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
2507 __ CallRuntime(Runtime::kTraceExit);
2508 }
2509 if (info()->saves_caller_doubles()) {
2510 RestoreCallerDoubles();
2511 }
2512 if (NeedsEagerFrame()) {
2513 __ movp(rsp, rbp);
2514 __ popq(rbp);
2515 }
2516 if (instr->has_constant_parameter_count()) {
2517 __ Ret((ToInteger32(instr->constant_parameter_count()) + 1) * kPointerSize,
2518 rcx);
2519 } else {
2520 DCHECK(info()->IsStub()); // Functions would need to drop one more value.
2521 Register reg = ToRegister(instr->parameter_count());
2522 // The argument count parameter is a smi
2523 __ SmiToInteger32(reg, reg);
2524 Register return_addr_reg = reg.is(rcx) ? rbx : rcx;
2525 __ PopReturnAddressTo(return_addr_reg);
2526 __ shlp(reg, Immediate(kPointerSizeLog2));
2527 __ addp(rsp, reg);
2528 __ jmp(return_addr_reg);
2529 }
2530}
2531
2532
2533template <class T>
2534void LCodeGen::EmitVectorLoadICRegisters(T* instr) {
2535 Register vector_register = ToRegister(instr->temp_vector());
2536 Register slot_register = LoadWithVectorDescriptor::SlotRegister();
2537 DCHECK(vector_register.is(LoadWithVectorDescriptor::VectorRegister()));
2538 DCHECK(slot_register.is(rax));
2539
2540 AllowDeferredHandleDereference vector_structure_check;
2541 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2542 __ Move(vector_register, vector);
2543 // No need to allocate this register.
2544 FeedbackVectorSlot slot = instr->hydrogen()->slot();
2545 int index = vector->GetIndex(slot);
2546 __ Move(slot_register, Smi::FromInt(index));
2547}
2548
2549
2550template <class T>
2551void LCodeGen::EmitVectorStoreICRegisters(T* instr) {
2552 Register vector_register = ToRegister(instr->temp_vector());
2553 Register slot_register = ToRegister(instr->temp_slot());
2554
2555 AllowDeferredHandleDereference vector_structure_check;
2556 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
2557 __ Move(vector_register, vector);
2558 FeedbackVectorSlot slot = instr->hydrogen()->slot();
2559 int index = vector->GetIndex(slot);
2560 __ Move(slot_register, Smi::FromInt(index));
2561}
2562
2563
2564void LCodeGen::DoLoadGlobalGeneric(LLoadGlobalGeneric* instr) {
2565 DCHECK(ToRegister(instr->context()).is(rsi));
2566 DCHECK(ToRegister(instr->global_object())
2567 .is(LoadDescriptor::ReceiverRegister()));
2568 DCHECK(ToRegister(instr->result()).is(rax));
2569
2570 __ Move(LoadDescriptor::NameRegister(), instr->name());
2571 EmitVectorLoadICRegisters<LLoadGlobalGeneric>(instr);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002572 Handle<Code> ic = CodeFactory::LoadICInOptimizedCode(
2573 isolate(), instr->typeof_mode(), PREMONOMORPHIC)
2574 .code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002575 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2576}
2577
2578
2579void LCodeGen::DoLoadContextSlot(LLoadContextSlot* instr) {
2580 Register context = ToRegister(instr->context());
2581 Register result = ToRegister(instr->result());
2582 __ movp(result, ContextOperand(context, instr->slot_index()));
2583 if (instr->hydrogen()->RequiresHoleCheck()) {
2584 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
2585 if (instr->hydrogen()->DeoptimizesOnHole()) {
2586 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
2587 } else {
2588 Label is_not_hole;
2589 __ j(not_equal, &is_not_hole, Label::kNear);
2590 __ LoadRoot(result, Heap::kUndefinedValueRootIndex);
2591 __ bind(&is_not_hole);
2592 }
2593 }
2594}
2595
2596
2597void LCodeGen::DoStoreContextSlot(LStoreContextSlot* instr) {
2598 Register context = ToRegister(instr->context());
2599 Register value = ToRegister(instr->value());
2600
2601 Operand target = ContextOperand(context, instr->slot_index());
2602
2603 Label skip_assignment;
2604 if (instr->hydrogen()->RequiresHoleCheck()) {
2605 __ CompareRoot(target, Heap::kTheHoleValueRootIndex);
2606 if (instr->hydrogen()->DeoptimizesOnHole()) {
2607 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
2608 } else {
2609 __ j(not_equal, &skip_assignment);
2610 }
2611 }
2612 __ movp(target, value);
2613
2614 if (instr->hydrogen()->NeedsWriteBarrier()) {
2615 SmiCheck check_needed =
2616 instr->hydrogen()->value()->type().IsHeapObject()
2617 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
2618 int offset = Context::SlotOffset(instr->slot_index());
2619 Register scratch = ToRegister(instr->temp());
2620 __ RecordWriteContextSlot(context,
2621 offset,
2622 value,
2623 scratch,
2624 kSaveFPRegs,
2625 EMIT_REMEMBERED_SET,
2626 check_needed);
2627 }
2628
2629 __ bind(&skip_assignment);
2630}
2631
2632
2633void LCodeGen::DoLoadNamedField(LLoadNamedField* instr) {
2634 HObjectAccess access = instr->hydrogen()->access();
2635 int offset = access.offset();
2636
2637 if (access.IsExternalMemory()) {
2638 Register result = ToRegister(instr->result());
2639 if (instr->object()->IsConstantOperand()) {
2640 DCHECK(result.is(rax));
2641 __ load_rax(ToExternalReference(LConstantOperand::cast(instr->object())));
2642 } else {
2643 Register object = ToRegister(instr->object());
2644 __ Load(result, MemOperand(object, offset), access.representation());
2645 }
2646 return;
2647 }
2648
2649 Register object = ToRegister(instr->object());
2650 if (instr->hydrogen()->representation().IsDouble()) {
2651 DCHECK(access.IsInobject());
2652 XMMRegister result = ToDoubleRegister(instr->result());
2653 __ Movsd(result, FieldOperand(object, offset));
2654 return;
2655 }
2656
2657 Register result = ToRegister(instr->result());
2658 if (!access.IsInobject()) {
2659 __ movp(result, FieldOperand(object, JSObject::kPropertiesOffset));
2660 object = result;
2661 }
2662
2663 Representation representation = access.representation();
2664 if (representation.IsSmi() && SmiValuesAre32Bits() &&
2665 instr->hydrogen()->representation().IsInteger32()) {
2666 if (FLAG_debug_code) {
2667 Register scratch = kScratchRegister;
2668 __ Load(scratch, FieldOperand(object, offset), representation);
2669 __ AssertSmi(scratch);
2670 }
2671
2672 // Read int value directly from upper half of the smi.
2673 STATIC_ASSERT(kSmiTag == 0);
2674 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
2675 offset += kPointerSize / 2;
2676 representation = Representation::Integer32();
2677 }
2678 __ Load(result, FieldOperand(object, offset), representation);
2679}
2680
2681
2682void LCodeGen::DoLoadNamedGeneric(LLoadNamedGeneric* instr) {
2683 DCHECK(ToRegister(instr->context()).is(rsi));
2684 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
2685 DCHECK(ToRegister(instr->result()).is(rax));
2686
2687 __ Move(LoadDescriptor::NameRegister(), instr->name());
2688 EmitVectorLoadICRegisters<LLoadNamedGeneric>(instr);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002689 Handle<Code> ic = CodeFactory::LoadICInOptimizedCode(
2690 isolate(), NOT_INSIDE_TYPEOF,
2691 instr->hydrogen()->initialization_state())
2692 .code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002693 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2694}
2695
2696
2697void LCodeGen::DoLoadFunctionPrototype(LLoadFunctionPrototype* instr) {
2698 Register function = ToRegister(instr->function());
2699 Register result = ToRegister(instr->result());
2700
2701 // Get the prototype or initial map from the function.
2702 __ movp(result,
2703 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2704
2705 // Check that the function has a prototype or an initial map.
2706 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
2707 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
2708
2709 // If the function does not have an initial map, we're done.
2710 Label done;
2711 __ CmpObjectType(result, MAP_TYPE, kScratchRegister);
2712 __ j(not_equal, &done, Label::kNear);
2713
2714 // Get the prototype from the initial map.
2715 __ movp(result, FieldOperand(result, Map::kPrototypeOffset));
2716
2717 // All done.
2718 __ bind(&done);
2719}
2720
2721
2722void LCodeGen::DoLoadRoot(LLoadRoot* instr) {
2723 Register result = ToRegister(instr->result());
2724 __ LoadRoot(result, instr->index());
2725}
2726
2727
2728void LCodeGen::DoAccessArgumentsAt(LAccessArgumentsAt* instr) {
2729 Register arguments = ToRegister(instr->arguments());
2730 Register result = ToRegister(instr->result());
2731
2732 if (instr->length()->IsConstantOperand() &&
2733 instr->index()->IsConstantOperand()) {
2734 int32_t const_index = ToInteger32(LConstantOperand::cast(instr->index()));
2735 int32_t const_length = ToInteger32(LConstantOperand::cast(instr->length()));
2736 if (const_index >= 0 && const_index < const_length) {
2737 StackArgumentsAccessor args(arguments, const_length,
2738 ARGUMENTS_DONT_CONTAIN_RECEIVER);
2739 __ movp(result, args.GetArgumentOperand(const_index));
2740 } else if (FLAG_debug_code) {
2741 __ int3();
2742 }
2743 } else {
2744 Register length = ToRegister(instr->length());
2745 // There are two words between the frame pointer and the last argument.
2746 // Subtracting from length accounts for one of them add one more.
2747 if (instr->index()->IsRegister()) {
2748 __ subl(length, ToRegister(instr->index()));
2749 } else {
2750 __ subl(length, ToOperand(instr->index()));
2751 }
2752 StackArgumentsAccessor args(arguments, length,
2753 ARGUMENTS_DONT_CONTAIN_RECEIVER);
2754 __ movp(result, args.GetArgumentOperand(0));
2755 }
2756}
2757
2758
2759void LCodeGen::DoLoadKeyedExternalArray(LLoadKeyed* instr) {
2760 ElementsKind elements_kind = instr->elements_kind();
2761 LOperand* key = instr->key();
2762 if (kPointerSize == kInt32Size && !key->IsConstantOperand()) {
2763 Register key_reg = ToRegister(key);
2764 Representation key_representation =
2765 instr->hydrogen()->key()->representation();
2766 if (ExternalArrayOpRequiresTemp(key_representation, elements_kind)) {
2767 __ SmiToInteger64(key_reg, key_reg);
2768 } else if (instr->hydrogen()->IsDehoisted()) {
2769 // Sign extend key because it could be a 32 bit negative value
2770 // and the dehoisted address computation happens in 64 bits
2771 __ movsxlq(key_reg, key_reg);
2772 }
2773 }
2774 Operand operand(BuildFastArrayOperand(
2775 instr->elements(),
2776 key,
2777 instr->hydrogen()->key()->representation(),
2778 elements_kind,
2779 instr->base_offset()));
2780
2781 if (elements_kind == FLOAT32_ELEMENTS) {
2782 XMMRegister result(ToDoubleRegister(instr->result()));
2783 __ Cvtss2sd(result, operand);
2784 } else if (elements_kind == FLOAT64_ELEMENTS) {
2785 __ Movsd(ToDoubleRegister(instr->result()), operand);
2786 } else {
2787 Register result(ToRegister(instr->result()));
2788 switch (elements_kind) {
2789 case INT8_ELEMENTS:
2790 __ movsxbl(result, operand);
2791 break;
2792 case UINT8_ELEMENTS:
2793 case UINT8_CLAMPED_ELEMENTS:
2794 __ movzxbl(result, operand);
2795 break;
2796 case INT16_ELEMENTS:
2797 __ movsxwl(result, operand);
2798 break;
2799 case UINT16_ELEMENTS:
2800 __ movzxwl(result, operand);
2801 break;
2802 case INT32_ELEMENTS:
2803 __ movl(result, operand);
2804 break;
2805 case UINT32_ELEMENTS:
2806 __ movl(result, operand);
2807 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
2808 __ testl(result, result);
2809 DeoptimizeIf(negative, instr, Deoptimizer::kNegativeValue);
2810 }
2811 break;
2812 case FLOAT32_ELEMENTS:
2813 case FLOAT64_ELEMENTS:
2814 case FAST_ELEMENTS:
2815 case FAST_SMI_ELEMENTS:
2816 case FAST_DOUBLE_ELEMENTS:
2817 case FAST_HOLEY_ELEMENTS:
2818 case FAST_HOLEY_SMI_ELEMENTS:
2819 case FAST_HOLEY_DOUBLE_ELEMENTS:
2820 case DICTIONARY_ELEMENTS:
2821 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
2822 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002823 case FAST_STRING_WRAPPER_ELEMENTS:
2824 case SLOW_STRING_WRAPPER_ELEMENTS:
2825 case NO_ELEMENTS:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002826 UNREACHABLE();
2827 break;
2828 }
2829 }
2830}
2831
2832
2833void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
2834 XMMRegister result(ToDoubleRegister(instr->result()));
2835 LOperand* key = instr->key();
2836 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
2837 instr->hydrogen()->IsDehoisted()) {
2838 // Sign extend key because it could be a 32 bit negative value
2839 // and the dehoisted address computation happens in 64 bits
2840 __ movsxlq(ToRegister(key), ToRegister(key));
2841 }
2842 if (instr->hydrogen()->RequiresHoleCheck()) {
2843 Operand hole_check_operand = BuildFastArrayOperand(
2844 instr->elements(),
2845 key,
2846 instr->hydrogen()->key()->representation(),
2847 FAST_DOUBLE_ELEMENTS,
2848 instr->base_offset() + sizeof(kHoleNanLower32));
2849 __ cmpl(hole_check_operand, Immediate(kHoleNanUpper32));
2850 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
2851 }
2852
2853 Operand double_load_operand = BuildFastArrayOperand(
2854 instr->elements(),
2855 key,
2856 instr->hydrogen()->key()->representation(),
2857 FAST_DOUBLE_ELEMENTS,
2858 instr->base_offset());
2859 __ Movsd(result, double_load_operand);
2860}
2861
2862
2863void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
2864 HLoadKeyed* hinstr = instr->hydrogen();
2865 Register result = ToRegister(instr->result());
2866 LOperand* key = instr->key();
2867 bool requires_hole_check = hinstr->RequiresHoleCheck();
2868 Representation representation = hinstr->representation();
2869 int offset = instr->base_offset();
2870
2871 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
2872 instr->hydrogen()->IsDehoisted()) {
2873 // Sign extend key because it could be a 32 bit negative value
2874 // and the dehoisted address computation happens in 64 bits
2875 __ movsxlq(ToRegister(key), ToRegister(key));
2876 }
2877 if (representation.IsInteger32() && SmiValuesAre32Bits() &&
2878 hinstr->elements_kind() == FAST_SMI_ELEMENTS) {
2879 DCHECK(!requires_hole_check);
2880 if (FLAG_debug_code) {
2881 Register scratch = kScratchRegister;
2882 __ Load(scratch,
2883 BuildFastArrayOperand(instr->elements(),
2884 key,
2885 instr->hydrogen()->key()->representation(),
2886 FAST_ELEMENTS,
2887 offset),
2888 Representation::Smi());
2889 __ AssertSmi(scratch);
2890 }
2891 // Read int value directly from upper half of the smi.
2892 STATIC_ASSERT(kSmiTag == 0);
2893 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
2894 offset += kPointerSize / 2;
2895 }
2896
2897 __ Load(result,
2898 BuildFastArrayOperand(instr->elements(), key,
2899 instr->hydrogen()->key()->representation(),
2900 FAST_ELEMENTS, offset),
2901 representation);
2902
2903 // Check for the hole value.
2904 if (requires_hole_check) {
2905 if (IsFastSmiElementsKind(hinstr->elements_kind())) {
2906 Condition smi = __ CheckSmi(result);
2907 DeoptimizeIf(NegateCondition(smi), instr, Deoptimizer::kNotASmi);
2908 } else {
2909 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
2910 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
2911 }
2912 } else if (hinstr->hole_mode() == CONVERT_HOLE_TO_UNDEFINED) {
2913 DCHECK(hinstr->elements_kind() == FAST_HOLEY_ELEMENTS);
2914 Label done;
2915 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
2916 __ j(not_equal, &done);
2917 if (info()->IsStub()) {
2918 // A stub can safely convert the hole to undefined only if the array
2919 // protector cell contains (Smi) Isolate::kArrayProtectorValid. Otherwise
2920 // it needs to bail out.
2921 __ LoadRoot(result, Heap::kArrayProtectorRootIndex);
2922 __ Cmp(FieldOperand(result, Cell::kValueOffset),
2923 Smi::FromInt(Isolate::kArrayProtectorValid));
2924 DeoptimizeIf(not_equal, instr, Deoptimizer::kHole);
2925 }
2926 __ Move(result, isolate()->factory()->undefined_value());
2927 __ bind(&done);
2928 }
2929}
2930
2931
2932void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
2933 if (instr->is_fixed_typed_array()) {
2934 DoLoadKeyedExternalArray(instr);
2935 } else if (instr->hydrogen()->representation().IsDouble()) {
2936 DoLoadKeyedFixedDoubleArray(instr);
2937 } else {
2938 DoLoadKeyedFixedArray(instr);
2939 }
2940}
2941
2942
2943Operand LCodeGen::BuildFastArrayOperand(
2944 LOperand* elements_pointer,
2945 LOperand* key,
2946 Representation key_representation,
2947 ElementsKind elements_kind,
2948 uint32_t offset) {
2949 Register elements_pointer_reg = ToRegister(elements_pointer);
2950 int shift_size = ElementsKindToShiftSize(elements_kind);
2951 if (key->IsConstantOperand()) {
2952 int32_t constant_value = ToInteger32(LConstantOperand::cast(key));
2953 if (constant_value & 0xF0000000) {
2954 Abort(kArrayIndexConstantValueTooBig);
2955 }
2956 return Operand(elements_pointer_reg,
2957 (constant_value << shift_size) + offset);
2958 } else {
2959 // Guaranteed by ArrayInstructionInterface::KeyedAccessIndexRequirement().
2960 DCHECK(key_representation.IsInteger32());
2961
2962 ScaleFactor scale_factor = static_cast<ScaleFactor>(shift_size);
2963 return Operand(elements_pointer_reg,
2964 ToRegister(key),
2965 scale_factor,
2966 offset);
2967 }
2968}
2969
2970
2971void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
2972 DCHECK(ToRegister(instr->context()).is(rsi));
2973 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
2974 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
2975
2976 if (instr->hydrogen()->HasVectorAndSlot()) {
2977 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
2978 }
2979
2980 Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(
Ben Murdoch097c5b22016-05-18 11:27:45 +01002981 isolate(), instr->hydrogen()->initialization_state())
2982 .code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002983 CallCode(ic, RelocInfo::CODE_TARGET, instr);
2984}
2985
2986
2987void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
2988 Register result = ToRegister(instr->result());
2989
2990 if (instr->hydrogen()->from_inlined()) {
2991 __ leap(result, Operand(rsp, -kFPOnStackSize + -kPCOnStackSize));
Ben Murdochda12d292016-06-02 14:46:10 +01002992 } else if (instr->hydrogen()->arguments_adaptor()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002993 // Check for arguments adapter frame.
2994 Label done, adapted;
2995 __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01002996 __ Cmp(Operand(result, CommonFrameConstants::kContextOrFrameTypeOffset),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002997 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
2998 __ j(equal, &adapted, Label::kNear);
2999
3000 // No arguments adaptor frame.
3001 __ movp(result, rbp);
3002 __ jmp(&done, Label::kNear);
3003
3004 // Arguments adaptor frame present.
3005 __ bind(&adapted);
3006 __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
3007
3008 // Result is the frame pointer for the frame if not adapted and for the real
3009 // frame below the adaptor frame if adapted.
3010 __ bind(&done);
Ben Murdochda12d292016-06-02 14:46:10 +01003011 } else {
3012 __ movp(result, rbp);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003013 }
3014}
3015
3016
3017void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
3018 Register result = ToRegister(instr->result());
3019
3020 Label done;
3021
3022 // If no arguments adaptor frame the number of arguments is fixed.
3023 if (instr->elements()->IsRegister()) {
3024 __ cmpp(rbp, ToRegister(instr->elements()));
3025 } else {
3026 __ cmpp(rbp, ToOperand(instr->elements()));
3027 }
3028 __ movl(result, Immediate(scope()->num_parameters()));
3029 __ j(equal, &done, Label::kNear);
3030
3031 // Arguments adaptor frame present. Get argument length from there.
3032 __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
3033 __ SmiToInteger32(result,
3034 Operand(result,
3035 ArgumentsAdaptorFrameConstants::kLengthOffset));
3036
3037 // Argument length is in result register.
3038 __ bind(&done);
3039}
3040
3041
3042void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
3043 Register receiver = ToRegister(instr->receiver());
3044 Register function = ToRegister(instr->function());
3045
3046 // If the receiver is null or undefined, we have to pass the global
3047 // object as a receiver to normal functions. Values have to be
3048 // passed unchanged to builtins and strict-mode functions.
3049 Label global_object, receiver_ok;
3050 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
3051
3052 if (!instr->hydrogen()->known_function()) {
3053 // Do not transform the receiver to object for strict mode
3054 // functions.
3055 __ movp(kScratchRegister,
3056 FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
3057 __ testb(FieldOperand(kScratchRegister,
3058 SharedFunctionInfo::kStrictModeByteOffset),
3059 Immediate(1 << SharedFunctionInfo::kStrictModeBitWithinByte));
3060 __ j(not_equal, &receiver_ok, dist);
3061
3062 // Do not transform the receiver to object for builtins.
3063 __ testb(FieldOperand(kScratchRegister,
3064 SharedFunctionInfo::kNativeByteOffset),
3065 Immediate(1 << SharedFunctionInfo::kNativeBitWithinByte));
3066 __ j(not_equal, &receiver_ok, dist);
3067 }
3068
3069 // Normal function. Replace undefined or null with global receiver.
3070 __ CompareRoot(receiver, Heap::kNullValueRootIndex);
3071 __ j(equal, &global_object, Label::kNear);
3072 __ CompareRoot(receiver, Heap::kUndefinedValueRootIndex);
3073 __ j(equal, &global_object, Label::kNear);
3074
3075 // The receiver should be a JS object.
3076 Condition is_smi = __ CheckSmi(receiver);
3077 DeoptimizeIf(is_smi, instr, Deoptimizer::kSmi);
3078 __ CmpObjectType(receiver, FIRST_JS_RECEIVER_TYPE, kScratchRegister);
3079 DeoptimizeIf(below, instr, Deoptimizer::kNotAJavaScriptObject);
3080
3081 __ jmp(&receiver_ok, Label::kNear);
3082 __ bind(&global_object);
3083 __ movp(receiver, FieldOperand(function, JSFunction::kContextOffset));
3084 __ movp(receiver, ContextOperand(receiver, Context::NATIVE_CONTEXT_INDEX));
3085 __ movp(receiver, ContextOperand(receiver, Context::GLOBAL_PROXY_INDEX));
3086
3087 __ bind(&receiver_ok);
3088}
3089
3090
3091void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3092 Register receiver = ToRegister(instr->receiver());
3093 Register function = ToRegister(instr->function());
3094 Register length = ToRegister(instr->length());
3095 Register elements = ToRegister(instr->elements());
3096 DCHECK(receiver.is(rax)); // Used for parameter count.
3097 DCHECK(function.is(rdi)); // Required by InvokeFunction.
3098 DCHECK(ToRegister(instr->result()).is(rax));
3099
3100 // Copy the arguments to this function possibly from the
3101 // adaptor frame below it.
3102 const uint32_t kArgumentsLimit = 1 * KB;
3103 __ cmpp(length, Immediate(kArgumentsLimit));
3104 DeoptimizeIf(above, instr, Deoptimizer::kTooManyArguments);
3105
3106 __ Push(receiver);
3107 __ movp(receiver, length);
3108
3109 // Loop through the arguments pushing them onto the execution
3110 // stack.
3111 Label invoke, loop;
3112 // length is a small non-negative integer, due to the test above.
3113 __ testl(length, length);
3114 __ j(zero, &invoke, Label::kNear);
3115 __ bind(&loop);
3116 StackArgumentsAccessor args(elements, length,
3117 ARGUMENTS_DONT_CONTAIN_RECEIVER);
3118 __ Push(args.GetArgumentOperand(0));
3119 __ decl(length);
3120 __ j(not_zero, &loop);
3121
3122 // Invoke the function.
3123 __ bind(&invoke);
Ben Murdochda12d292016-06-02 14:46:10 +01003124
3125 InvokeFlag flag = CALL_FUNCTION;
3126 if (instr->hydrogen()->tail_call_mode() == TailCallMode::kAllow) {
3127 DCHECK(!info()->saves_caller_doubles());
3128 // TODO(ishell): drop current frame before pushing arguments to the stack.
3129 flag = JUMP_FUNCTION;
3130 ParameterCount actual(rax);
3131 // It is safe to use rbx, rcx and r8 as scratch registers here given that
3132 // 1) we are not going to return to caller function anyway,
3133 // 2) rbx (expected number of arguments) will be initialized below.
3134 PrepareForTailCall(actual, rbx, rcx, r8);
3135 }
3136
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003137 DCHECK(instr->HasPointerMap());
3138 LPointerMap* pointers = instr->pointer_map();
Ben Murdochda12d292016-06-02 14:46:10 +01003139 SafepointGenerator safepoint_generator(this, pointers, Safepoint::kLazyDeopt);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003140 ParameterCount actual(rax);
Ben Murdochda12d292016-06-02 14:46:10 +01003141 __ InvokeFunction(function, no_reg, actual, flag, safepoint_generator);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003142}
3143
3144
3145void LCodeGen::DoPushArgument(LPushArgument* instr) {
3146 LOperand* argument = instr->value();
3147 EmitPushTaggedOperand(argument);
3148}
3149
3150
3151void LCodeGen::DoDrop(LDrop* instr) {
3152 __ Drop(instr->count());
3153}
3154
3155
3156void LCodeGen::DoThisFunction(LThisFunction* instr) {
3157 Register result = ToRegister(instr->result());
3158 __ movp(result, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
3159}
3160
3161
3162void LCodeGen::DoContext(LContext* instr) {
3163 Register result = ToRegister(instr->result());
3164 if (info()->IsOptimizing()) {
3165 __ movp(result, Operand(rbp, StandardFrameConstants::kContextOffset));
3166 } else {
3167 // If there is no frame, the context must be in rsi.
3168 DCHECK(result.is(rsi));
3169 }
3170}
3171
3172
3173void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
3174 DCHECK(ToRegister(instr->context()).is(rsi));
3175 __ Push(instr->hydrogen()->pairs());
3176 __ Push(Smi::FromInt(instr->hydrogen()->flags()));
3177 CallRuntime(Runtime::kDeclareGlobals, instr);
3178}
3179
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003180void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
3181 int formal_parameter_count, int arity,
Ben Murdochda12d292016-06-02 14:46:10 +01003182 bool is_tail_call, LInstruction* instr) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003183 bool dont_adapt_arguments =
3184 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3185 bool can_invoke_directly =
3186 dont_adapt_arguments || formal_parameter_count == arity;
3187
3188 Register function_reg = rdi;
3189 LPointerMap* pointers = instr->pointer_map();
3190
3191 if (can_invoke_directly) {
3192 // Change context.
3193 __ movp(rsi, FieldOperand(function_reg, JSFunction::kContextOffset));
3194
3195 // Always initialize new target and number of actual arguments.
3196 __ LoadRoot(rdx, Heap::kUndefinedValueRootIndex);
3197 __ Set(rax, arity);
3198
Ben Murdochda12d292016-06-02 14:46:10 +01003199 bool is_self_call = function.is_identical_to(info()->closure());
3200
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003201 // Invoke function.
Ben Murdochda12d292016-06-02 14:46:10 +01003202 if (is_self_call) {
3203 Handle<Code> self(reinterpret_cast<Code**>(__ CodeObject().location()));
3204 if (is_tail_call) {
3205 __ Jump(self, RelocInfo::CODE_TARGET);
3206 } else {
3207 __ Call(self, RelocInfo::CODE_TARGET);
3208 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003209 } else {
Ben Murdochda12d292016-06-02 14:46:10 +01003210 Operand target = FieldOperand(function_reg, JSFunction::kCodeEntryOffset);
3211 if (is_tail_call) {
3212 __ Jump(target);
3213 } else {
3214 __ Call(target);
3215 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003216 }
3217
Ben Murdochda12d292016-06-02 14:46:10 +01003218 if (!is_tail_call) {
3219 // Set up deoptimization.
3220 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT, 0);
3221 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003222 } else {
3223 // We need to adapt arguments.
Ben Murdochda12d292016-06-02 14:46:10 +01003224 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3225 ParameterCount actual(arity);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003226 ParameterCount expected(formal_parameter_count);
Ben Murdochda12d292016-06-02 14:46:10 +01003227 InvokeFlag flag = is_tail_call ? JUMP_FUNCTION : CALL_FUNCTION;
3228 __ InvokeFunction(function_reg, no_reg, expected, actual, flag, generator);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003229 }
3230}
3231
3232
3233void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
3234 DCHECK(ToRegister(instr->result()).is(rax));
3235
3236 if (instr->hydrogen()->IsTailCall()) {
3237 if (NeedsEagerFrame()) __ leave();
3238
3239 if (instr->target()->IsConstantOperand()) {
3240 LConstantOperand* target = LConstantOperand::cast(instr->target());
3241 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3242 __ jmp(code, RelocInfo::CODE_TARGET);
3243 } else {
3244 DCHECK(instr->target()->IsRegister());
3245 Register target = ToRegister(instr->target());
3246 __ addp(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
3247 __ jmp(target);
3248 }
3249 } else {
3250 LPointerMap* pointers = instr->pointer_map();
3251 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3252
3253 if (instr->target()->IsConstantOperand()) {
3254 LConstantOperand* target = LConstantOperand::cast(instr->target());
3255 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3256 generator.BeforeCall(__ CallSize(code));
3257 __ call(code, RelocInfo::CODE_TARGET);
3258 } else {
3259 DCHECK(instr->target()->IsRegister());
3260 Register target = ToRegister(instr->target());
3261 generator.BeforeCall(__ CallSize(target));
3262 __ addp(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
3263 __ call(target);
3264 }
3265 generator.AfterCall();
3266 }
3267}
3268
3269
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003270void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3271 Register input_reg = ToRegister(instr->value());
3272 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
3273 Heap::kHeapNumberMapRootIndex);
3274 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
3275
3276 Label slow, allocated, done;
Ben Murdochc5610432016-08-08 18:44:38 +01003277 uint32_t available_regs = rax.bit() | rcx.bit() | rdx.bit() | rbx.bit();
3278 available_regs &= ~input_reg.bit();
3279 if (instr->context()->IsRegister()) {
3280 // Make sure that the context isn't overwritten in the AllocateHeapNumber
3281 // macro below.
3282 available_regs &= ~ToRegister(instr->context()).bit();
3283 }
3284
3285 Register tmp =
3286 Register::from_code(base::bits::CountTrailingZeros32(available_regs));
3287 available_regs &= ~tmp.bit();
3288 Register tmp2 =
3289 Register::from_code(base::bits::CountTrailingZeros32(available_regs));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003290
3291 // Preserve the value of all registers.
3292 PushSafepointRegistersScope scope(this);
3293
3294 __ movl(tmp, FieldOperand(input_reg, HeapNumber::kExponentOffset));
3295 // Check the sign of the argument. If the argument is positive, just
3296 // return it. We do not need to patch the stack since |input| and
3297 // |result| are the same register and |input| will be restored
3298 // unchanged by popping safepoint registers.
3299 __ testl(tmp, Immediate(HeapNumber::kSignMask));
3300 __ j(zero, &done);
3301
3302 __ AllocateHeapNumber(tmp, tmp2, &slow);
3303 __ jmp(&allocated, Label::kNear);
3304
3305 // Slow case: Call the runtime system to do the number allocation.
3306 __ bind(&slow);
3307 CallRuntimeFromDeferred(
3308 Runtime::kAllocateHeapNumber, 0, instr, instr->context());
3309 // Set the pointer to the new heap number in tmp.
3310 if (!tmp.is(rax)) __ movp(tmp, rax);
3311 // Restore input_reg after call to runtime.
3312 __ LoadFromSafepointRegisterSlot(input_reg, input_reg);
3313
3314 __ bind(&allocated);
3315 __ movq(tmp2, FieldOperand(input_reg, HeapNumber::kValueOffset));
3316 __ shlq(tmp2, Immediate(1));
3317 __ shrq(tmp2, Immediate(1));
3318 __ movq(FieldOperand(tmp, HeapNumber::kValueOffset), tmp2);
3319 __ StoreToSafepointRegisterSlot(input_reg, tmp);
3320
3321 __ bind(&done);
3322}
3323
3324
3325void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3326 Register input_reg = ToRegister(instr->value());
3327 __ testl(input_reg, input_reg);
3328 Label is_positive;
3329 __ j(not_sign, &is_positive, Label::kNear);
3330 __ negl(input_reg); // Sets flags.
3331 DeoptimizeIf(negative, instr, Deoptimizer::kOverflow);
3332 __ bind(&is_positive);
3333}
3334
3335
3336void LCodeGen::EmitSmiMathAbs(LMathAbs* instr) {
3337 Register input_reg = ToRegister(instr->value());
3338 __ testp(input_reg, input_reg);
3339 Label is_positive;
3340 __ j(not_sign, &is_positive, Label::kNear);
3341 __ negp(input_reg); // Sets flags.
3342 DeoptimizeIf(negative, instr, Deoptimizer::kOverflow);
3343 __ bind(&is_positive);
3344}
3345
3346
3347void LCodeGen::DoMathAbs(LMathAbs* instr) {
3348 // Class for deferred case.
3349 class DeferredMathAbsTaggedHeapNumber final : public LDeferredCode {
3350 public:
3351 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr)
3352 : LDeferredCode(codegen), instr_(instr) { }
3353 void Generate() override {
3354 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3355 }
3356 LInstruction* instr() override { return instr_; }
3357
3358 private:
3359 LMathAbs* instr_;
3360 };
3361
3362 DCHECK(instr->value()->Equals(instr->result()));
3363 Representation r = instr->hydrogen()->value()->representation();
3364
3365 if (r.IsDouble()) {
3366 XMMRegister scratch = double_scratch0();
3367 XMMRegister input_reg = ToDoubleRegister(instr->value());
3368 __ Xorpd(scratch, scratch);
3369 __ Subsd(scratch, input_reg);
3370 __ Andpd(input_reg, scratch);
3371 } else if (r.IsInteger32()) {
3372 EmitIntegerMathAbs(instr);
3373 } else if (r.IsSmi()) {
3374 EmitSmiMathAbs(instr);
3375 } else { // Tagged case.
3376 DeferredMathAbsTaggedHeapNumber* deferred =
3377 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3378 Register input_reg = ToRegister(instr->value());
3379 // Smi check.
3380 __ JumpIfNotSmi(input_reg, deferred->entry());
3381 EmitSmiMathAbs(instr);
3382 __ bind(deferred->exit());
3383 }
3384}
3385
Ben Murdochda12d292016-06-02 14:46:10 +01003386void LCodeGen::DoMathFloorD(LMathFloorD* instr) {
3387 XMMRegister output_reg = ToDoubleRegister(instr->result());
3388 XMMRegister input_reg = ToDoubleRegister(instr->value());
3389 CpuFeatureScope scope(masm(), SSE4_1);
3390 __ Roundsd(output_reg, input_reg, kRoundDown);
3391}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003392
Ben Murdochda12d292016-06-02 14:46:10 +01003393void LCodeGen::DoMathFloorI(LMathFloorI* instr) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003394 XMMRegister xmm_scratch = double_scratch0();
3395 Register output_reg = ToRegister(instr->result());
3396 XMMRegister input_reg = ToDoubleRegister(instr->value());
3397
3398 if (CpuFeatures::IsSupported(SSE4_1)) {
3399 CpuFeatureScope scope(masm(), SSE4_1);
3400 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3401 // Deoptimize if minus zero.
3402 __ Movq(output_reg, input_reg);
3403 __ subq(output_reg, Immediate(1));
3404 DeoptimizeIf(overflow, instr, Deoptimizer::kMinusZero);
3405 }
3406 __ Roundsd(xmm_scratch, input_reg, kRoundDown);
3407 __ Cvttsd2si(output_reg, xmm_scratch);
3408 __ cmpl(output_reg, Immediate(0x1));
3409 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3410 } else {
3411 Label negative_sign, done;
3412 // Deoptimize on unordered.
3413 __ Xorpd(xmm_scratch, xmm_scratch); // Zero the register.
3414 __ Ucomisd(input_reg, xmm_scratch);
3415 DeoptimizeIf(parity_even, instr, Deoptimizer::kNaN);
3416 __ j(below, &negative_sign, Label::kNear);
3417
3418 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3419 // Check for negative zero.
3420 Label positive_sign;
3421 __ j(above, &positive_sign, Label::kNear);
3422 __ Movmskpd(output_reg, input_reg);
3423 __ testl(output_reg, Immediate(1));
3424 DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero);
3425 __ Set(output_reg, 0);
3426 __ jmp(&done);
3427 __ bind(&positive_sign);
3428 }
3429
3430 // Use truncating instruction (OK because input is positive).
3431 __ Cvttsd2si(output_reg, input_reg);
3432 // Overflow is signalled with minint.
3433 __ cmpl(output_reg, Immediate(0x1));
3434 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3435 __ jmp(&done, Label::kNear);
3436
3437 // Non-zero negative reaches here.
3438 __ bind(&negative_sign);
3439 // Truncate, then compare and compensate.
3440 __ Cvttsd2si(output_reg, input_reg);
3441 __ Cvtlsi2sd(xmm_scratch, output_reg);
3442 __ Ucomisd(input_reg, xmm_scratch);
3443 __ j(equal, &done, Label::kNear);
3444 __ subl(output_reg, Immediate(1));
3445 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3446
3447 __ bind(&done);
3448 }
3449}
3450
Ben Murdochda12d292016-06-02 14:46:10 +01003451void LCodeGen::DoMathRoundD(LMathRoundD* instr) {
3452 XMMRegister xmm_scratch = double_scratch0();
3453 XMMRegister output_reg = ToDoubleRegister(instr->result());
3454 XMMRegister input_reg = ToDoubleRegister(instr->value());
3455 CpuFeatureScope scope(masm(), SSE4_1);
3456 Label done;
3457 __ Roundsd(output_reg, input_reg, kRoundUp);
3458 __ Move(xmm_scratch, -0.5);
3459 __ Addsd(xmm_scratch, output_reg);
3460 __ Ucomisd(xmm_scratch, input_reg);
3461 __ j(below_equal, &done, Label::kNear);
3462 __ Move(xmm_scratch, 1.0);
3463 __ Subsd(output_reg, xmm_scratch);
3464 __ bind(&done);
3465}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003466
Ben Murdochda12d292016-06-02 14:46:10 +01003467void LCodeGen::DoMathRoundI(LMathRoundI* instr) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003468 const XMMRegister xmm_scratch = double_scratch0();
3469 Register output_reg = ToRegister(instr->result());
3470 XMMRegister input_reg = ToDoubleRegister(instr->value());
3471 XMMRegister input_temp = ToDoubleRegister(instr->temp());
3472 static int64_t one_half = V8_INT64_C(0x3FE0000000000000); // 0.5
3473 static int64_t minus_one_half = V8_INT64_C(0xBFE0000000000000); // -0.5
3474
3475 Label done, round_to_zero, below_one_half;
3476 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
3477 __ movq(kScratchRegister, one_half);
3478 __ Movq(xmm_scratch, kScratchRegister);
3479 __ Ucomisd(xmm_scratch, input_reg);
3480 __ j(above, &below_one_half, Label::kNear);
3481
3482 // CVTTSD2SI rounds towards zero, since 0.5 <= x, we use floor(0.5 + x).
3483 __ Addsd(xmm_scratch, input_reg);
3484 __ Cvttsd2si(output_reg, xmm_scratch);
3485 // Overflow is signalled with minint.
3486 __ cmpl(output_reg, Immediate(0x1));
3487 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3488 __ jmp(&done, dist);
3489
3490 __ bind(&below_one_half);
3491 __ movq(kScratchRegister, minus_one_half);
3492 __ Movq(xmm_scratch, kScratchRegister);
3493 __ Ucomisd(xmm_scratch, input_reg);
3494 __ j(below_equal, &round_to_zero, Label::kNear);
3495
3496 // CVTTSD2SI rounds towards zero, we use ceil(x - (-0.5)) and then
3497 // compare and compensate.
3498 __ Movapd(input_temp, input_reg); // Do not alter input_reg.
3499 __ Subsd(input_temp, xmm_scratch);
3500 __ Cvttsd2si(output_reg, input_temp);
3501 // Catch minint due to overflow, and to prevent overflow when compensating.
3502 __ cmpl(output_reg, Immediate(0x1));
3503 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3504
3505 __ Cvtlsi2sd(xmm_scratch, output_reg);
3506 __ Ucomisd(xmm_scratch, input_temp);
3507 __ j(equal, &done, dist);
3508 __ subl(output_reg, Immediate(1));
3509 // No overflow because we already ruled out minint.
3510 __ jmp(&done, dist);
3511
3512 __ bind(&round_to_zero);
3513 // We return 0 for the input range [+0, 0.5[, or [-0.5, 0.5[ if
3514 // we can ignore the difference between a result of -0 and +0.
3515 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3516 __ Movq(output_reg, input_reg);
3517 __ testq(output_reg, output_reg);
3518 DeoptimizeIf(negative, instr, Deoptimizer::kMinusZero);
3519 }
3520 __ Set(output_reg, 0);
3521 __ bind(&done);
3522}
3523
3524
3525void LCodeGen::DoMathFround(LMathFround* instr) {
3526 XMMRegister input_reg = ToDoubleRegister(instr->value());
3527 XMMRegister output_reg = ToDoubleRegister(instr->result());
3528 __ Cvtsd2ss(output_reg, input_reg);
3529 __ Cvtss2sd(output_reg, output_reg);
3530}
3531
3532
3533void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3534 XMMRegister output = ToDoubleRegister(instr->result());
3535 if (instr->value()->IsDoubleRegister()) {
3536 XMMRegister input = ToDoubleRegister(instr->value());
3537 __ Sqrtsd(output, input);
3538 } else {
3539 Operand input = ToOperand(instr->value());
3540 __ Sqrtsd(output, input);
3541 }
3542}
3543
3544
3545void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3546 XMMRegister xmm_scratch = double_scratch0();
3547 XMMRegister input_reg = ToDoubleRegister(instr->value());
3548 DCHECK(ToDoubleRegister(instr->result()).is(input_reg));
3549
3550 // Note that according to ECMA-262 15.8.2.13:
3551 // Math.pow(-Infinity, 0.5) == Infinity
3552 // Math.sqrt(-Infinity) == NaN
3553 Label done, sqrt;
3554 // Check base for -Infinity. According to IEEE-754, double-precision
3555 // -Infinity has the highest 12 bits set and the lowest 52 bits cleared.
3556 __ movq(kScratchRegister, V8_INT64_C(0xFFF0000000000000));
3557 __ Movq(xmm_scratch, kScratchRegister);
3558 __ Ucomisd(xmm_scratch, input_reg);
3559 // Comparing -Infinity with NaN results in "unordered", which sets the
3560 // zero flag as if both were equal. However, it also sets the carry flag.
3561 __ j(not_equal, &sqrt, Label::kNear);
3562 __ j(carry, &sqrt, Label::kNear);
3563 // If input is -Infinity, return Infinity.
3564 __ Xorpd(input_reg, input_reg);
3565 __ Subsd(input_reg, xmm_scratch);
3566 __ jmp(&done, Label::kNear);
3567
3568 // Square root.
3569 __ bind(&sqrt);
3570 __ Xorpd(xmm_scratch, xmm_scratch);
3571 __ Addsd(input_reg, xmm_scratch); // Convert -0 to +0.
3572 __ Sqrtsd(input_reg, input_reg);
3573 __ bind(&done);
3574}
3575
3576
3577void LCodeGen::DoPower(LPower* instr) {
3578 Representation exponent_type = instr->hydrogen()->right()->representation();
3579 // Having marked this as a call, we can use any registers.
3580 // Just make sure that the input/output registers are the expected ones.
3581
3582 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3583 DCHECK(!instr->right()->IsRegister() ||
3584 ToRegister(instr->right()).is(tagged_exponent));
3585 DCHECK(!instr->right()->IsDoubleRegister() ||
3586 ToDoubleRegister(instr->right()).is(xmm1));
3587 DCHECK(ToDoubleRegister(instr->left()).is(xmm2));
3588 DCHECK(ToDoubleRegister(instr->result()).is(xmm3));
3589
3590 if (exponent_type.IsSmi()) {
3591 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3592 __ CallStub(&stub);
3593 } else if (exponent_type.IsTagged()) {
3594 Label no_deopt;
3595 __ JumpIfSmi(tagged_exponent, &no_deopt, Label::kNear);
3596 __ CmpObjectType(tagged_exponent, HEAP_NUMBER_TYPE, rcx);
3597 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
3598 __ bind(&no_deopt);
3599 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3600 __ CallStub(&stub);
3601 } else if (exponent_type.IsInteger32()) {
3602 MathPowStub stub(isolate(), MathPowStub::INTEGER);
3603 __ CallStub(&stub);
3604 } else {
3605 DCHECK(exponent_type.IsDouble());
3606 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
3607 __ CallStub(&stub);
3608 }
3609}
3610
3611
3612void LCodeGen::DoMathExp(LMathExp* instr) {
3613 XMMRegister input = ToDoubleRegister(instr->value());
3614 XMMRegister result = ToDoubleRegister(instr->result());
3615 XMMRegister temp0 = double_scratch0();
3616 Register temp1 = ToRegister(instr->temp1());
3617 Register temp2 = ToRegister(instr->temp2());
3618
3619 MathExpGenerator::EmitMathExp(masm(), input, result, temp0, temp1, temp2);
3620}
3621
3622
3623void LCodeGen::DoMathLog(LMathLog* instr) {
3624 DCHECK(instr->value()->Equals(instr->result()));
3625 XMMRegister input_reg = ToDoubleRegister(instr->value());
3626 XMMRegister xmm_scratch = double_scratch0();
3627 Label positive, done, zero;
3628 __ Xorpd(xmm_scratch, xmm_scratch);
3629 __ Ucomisd(input_reg, xmm_scratch);
3630 __ j(above, &positive, Label::kNear);
3631 __ j(not_carry, &zero, Label::kNear);
3632 __ Pcmpeqd(input_reg, input_reg);
3633 __ jmp(&done, Label::kNear);
3634 __ bind(&zero);
3635 ExternalReference ninf =
3636 ExternalReference::address_of_negative_infinity();
3637 Operand ninf_operand = masm()->ExternalOperand(ninf);
3638 __ Movsd(input_reg, ninf_operand);
3639 __ jmp(&done, Label::kNear);
3640 __ bind(&positive);
3641 __ fldln2();
3642 __ subp(rsp, Immediate(kDoubleSize));
3643 __ Movsd(Operand(rsp, 0), input_reg);
3644 __ fld_d(Operand(rsp, 0));
3645 __ fyl2x();
3646 __ fstp_d(Operand(rsp, 0));
3647 __ Movsd(input_reg, Operand(rsp, 0));
3648 __ addp(rsp, Immediate(kDoubleSize));
3649 __ bind(&done);
3650}
3651
3652
3653void LCodeGen::DoMathClz32(LMathClz32* instr) {
3654 Register input = ToRegister(instr->value());
3655 Register result = ToRegister(instr->result());
3656
3657 __ Lzcntl(result, input);
3658}
3659
Ben Murdochda12d292016-06-02 14:46:10 +01003660void LCodeGen::PrepareForTailCall(const ParameterCount& actual,
3661 Register scratch1, Register scratch2,
3662 Register scratch3) {
3663#if DEBUG
3664 if (actual.is_reg()) {
3665 DCHECK(!AreAliased(actual.reg(), scratch1, scratch2, scratch3));
3666 } else {
3667 DCHECK(!AreAliased(scratch1, scratch2, scratch3));
3668 }
3669#endif
3670 if (FLAG_code_comments) {
3671 if (actual.is_reg()) {
3672 Comment(";;; PrepareForTailCall, actual: %s {", actual.reg().ToString());
3673 } else {
3674 Comment(";;; PrepareForTailCall, actual: %d {", actual.immediate());
3675 }
3676 }
3677
3678 // Check if next frame is an arguments adaptor frame.
3679 Register caller_args_count_reg = scratch1;
3680 Label no_arguments_adaptor, formal_parameter_count_loaded;
3681 __ movp(scratch2, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
3682 __ Cmp(Operand(scratch2, StandardFrameConstants::kContextOffset),
3683 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
3684 __ j(not_equal, &no_arguments_adaptor, Label::kNear);
3685
3686 // Drop current frame and load arguments count from arguments adaptor frame.
3687 __ movp(rbp, scratch2);
3688 __ SmiToInteger32(
3689 caller_args_count_reg,
3690 Operand(rbp, ArgumentsAdaptorFrameConstants::kLengthOffset));
3691 __ jmp(&formal_parameter_count_loaded, Label::kNear);
3692
3693 __ bind(&no_arguments_adaptor);
3694 // Load caller's formal parameter count.
3695 __ movp(caller_args_count_reg,
3696 Immediate(info()->literal()->parameter_count()));
3697
3698 __ bind(&formal_parameter_count_loaded);
3699 __ PrepareForTailCall(actual, caller_args_count_reg, scratch2, scratch3,
3700 ReturnAddressState::kNotOnStack);
3701 Comment(";;; }");
3702}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003703
3704void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
Ben Murdochda12d292016-06-02 14:46:10 +01003705 HInvokeFunction* hinstr = instr->hydrogen();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003706 DCHECK(ToRegister(instr->context()).is(rsi));
3707 DCHECK(ToRegister(instr->function()).is(rdi));
3708 DCHECK(instr->HasPointerMap());
3709
Ben Murdochda12d292016-06-02 14:46:10 +01003710 bool is_tail_call = hinstr->tail_call_mode() == TailCallMode::kAllow;
3711
3712 if (is_tail_call) {
3713 DCHECK(!info()->saves_caller_doubles());
3714 ParameterCount actual(instr->arity());
3715 // It is safe to use rbx, rcx and r8 as scratch registers here given that
3716 // 1) we are not going to return to caller function anyway,
3717 // 2) rbx (expected number of arguments) will be initialized below.
3718 PrepareForTailCall(actual, rbx, rcx, r8);
3719 }
3720
3721 Handle<JSFunction> known_function = hinstr->known_function();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003722 if (known_function.is_null()) {
3723 LPointerMap* pointers = instr->pointer_map();
3724 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
Ben Murdochda12d292016-06-02 14:46:10 +01003725 ParameterCount actual(instr->arity());
3726 InvokeFlag flag = is_tail_call ? JUMP_FUNCTION : CALL_FUNCTION;
3727 __ InvokeFunction(rdi, no_reg, actual, flag, generator);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003728 } else {
Ben Murdochda12d292016-06-02 14:46:10 +01003729 CallKnownFunction(known_function, hinstr->formal_parameter_count(),
3730 instr->arity(), is_tail_call, instr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003731 }
3732}
3733
3734
3735void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
3736 DCHECK(ToRegister(instr->context()).is(rsi));
3737 DCHECK(ToRegister(instr->constructor()).is(rdi));
3738 DCHECK(ToRegister(instr->result()).is(rax));
3739
3740 __ Set(rax, instr->arity());
3741 if (instr->arity() == 1) {
3742 // We only need the allocation site for the case we have a length argument.
3743 // The case may bail out to the runtime, which will determine the correct
3744 // elements kind with the site.
3745 __ Move(rbx, instr->hydrogen()->site());
3746 } else {
3747 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
3748 }
3749
3750 ElementsKind kind = instr->hydrogen()->elements_kind();
3751 AllocationSiteOverrideMode override_mode =
3752 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
3753 ? DISABLE_ALLOCATION_SITES
3754 : DONT_OVERRIDE;
3755
3756 if (instr->arity() == 0) {
3757 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
3758 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3759 } else if (instr->arity() == 1) {
3760 Label done;
3761 if (IsFastPackedElementsKind(kind)) {
3762 Label packed_case;
3763 // We might need a change here
3764 // look at the first argument
3765 __ movp(rcx, Operand(rsp, 0));
3766 __ testp(rcx, rcx);
3767 __ j(zero, &packed_case, Label::kNear);
3768
3769 ElementsKind holey_kind = GetHoleyElementsKind(kind);
3770 ArraySingleArgumentConstructorStub stub(isolate(),
3771 holey_kind,
3772 override_mode);
3773 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3774 __ jmp(&done, Label::kNear);
3775 __ bind(&packed_case);
3776 }
3777
3778 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
3779 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3780 __ bind(&done);
3781 } else {
3782 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
3783 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3784 }
3785}
3786
3787
3788void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
3789 DCHECK(ToRegister(instr->context()).is(rsi));
3790 CallRuntime(instr->function(), instr->arity(), instr, instr->save_doubles());
3791}
3792
3793
3794void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
3795 Register function = ToRegister(instr->function());
3796 Register code_object = ToRegister(instr->code_object());
3797 __ leap(code_object, FieldOperand(code_object, Code::kHeaderSize));
3798 __ movp(FieldOperand(function, JSFunction::kCodeEntryOffset), code_object);
3799}
3800
3801
3802void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
3803 Register result = ToRegister(instr->result());
3804 Register base = ToRegister(instr->base_object());
3805 if (instr->offset()->IsConstantOperand()) {
3806 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
3807 __ leap(result, Operand(base, ToInteger32(offset)));
3808 } else {
3809 Register offset = ToRegister(instr->offset());
3810 __ leap(result, Operand(base, offset, times_1, 0));
3811 }
3812}
3813
3814
3815void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
3816 HStoreNamedField* hinstr = instr->hydrogen();
3817 Representation representation = instr->representation();
3818
3819 HObjectAccess access = hinstr->access();
3820 int offset = access.offset();
3821
3822 if (access.IsExternalMemory()) {
3823 DCHECK(!hinstr->NeedsWriteBarrier());
3824 Register value = ToRegister(instr->value());
3825 if (instr->object()->IsConstantOperand()) {
3826 DCHECK(value.is(rax));
3827 LConstantOperand* object = LConstantOperand::cast(instr->object());
3828 __ store_rax(ToExternalReference(object));
3829 } else {
3830 Register object = ToRegister(instr->object());
3831 __ Store(MemOperand(object, offset), value, representation);
3832 }
3833 return;
3834 }
3835
3836 Register object = ToRegister(instr->object());
3837 __ AssertNotSmi(object);
3838
3839 DCHECK(!representation.IsSmi() ||
3840 !instr->value()->IsConstantOperand() ||
3841 IsInteger32Constant(LConstantOperand::cast(instr->value())));
3842 if (!FLAG_unbox_double_fields && representation.IsDouble()) {
3843 DCHECK(access.IsInobject());
3844 DCHECK(!hinstr->has_transition());
3845 DCHECK(!hinstr->NeedsWriteBarrier());
3846 XMMRegister value = ToDoubleRegister(instr->value());
3847 __ Movsd(FieldOperand(object, offset), value);
3848 return;
3849 }
3850
3851 if (hinstr->has_transition()) {
3852 Handle<Map> transition = hinstr->transition_map();
3853 AddDeprecationDependency(transition);
3854 if (!hinstr->NeedsWriteBarrierForMap()) {
3855 __ Move(FieldOperand(object, HeapObject::kMapOffset), transition);
3856 } else {
3857 Register temp = ToRegister(instr->temp());
3858 __ Move(kScratchRegister, transition);
3859 __ movp(FieldOperand(object, HeapObject::kMapOffset), kScratchRegister);
3860 // Update the write barrier for the map field.
3861 __ RecordWriteForMap(object,
3862 kScratchRegister,
3863 temp,
3864 kSaveFPRegs);
3865 }
3866 }
3867
3868 // Do the store.
3869 Register write_register = object;
3870 if (!access.IsInobject()) {
3871 write_register = ToRegister(instr->temp());
3872 __ movp(write_register, FieldOperand(object, JSObject::kPropertiesOffset));
3873 }
3874
3875 if (representation.IsSmi() && SmiValuesAre32Bits() &&
3876 hinstr->value()->representation().IsInteger32()) {
3877 DCHECK(hinstr->store_mode() == STORE_TO_INITIALIZED_ENTRY);
3878 if (FLAG_debug_code) {
3879 Register scratch = kScratchRegister;
3880 __ Load(scratch, FieldOperand(write_register, offset), representation);
3881 __ AssertSmi(scratch);
3882 }
3883 // Store int value directly to upper half of the smi.
3884 STATIC_ASSERT(kSmiTag == 0);
3885 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
3886 offset += kPointerSize / 2;
3887 representation = Representation::Integer32();
3888 }
3889
3890 Operand operand = FieldOperand(write_register, offset);
3891
3892 if (FLAG_unbox_double_fields && representation.IsDouble()) {
3893 DCHECK(access.IsInobject());
3894 XMMRegister value = ToDoubleRegister(instr->value());
3895 __ Movsd(operand, value);
3896
3897 } else if (instr->value()->IsRegister()) {
3898 Register value = ToRegister(instr->value());
3899 __ Store(operand, value, representation);
3900 } else {
3901 LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
3902 if (IsInteger32Constant(operand_value)) {
3903 DCHECK(!hinstr->NeedsWriteBarrier());
3904 int32_t value = ToInteger32(operand_value);
3905 if (representation.IsSmi()) {
3906 __ Move(operand, Smi::FromInt(value));
3907
3908 } else {
3909 __ movl(operand, Immediate(value));
3910 }
3911
3912 } else if (IsExternalConstant(operand_value)) {
3913 DCHECK(!hinstr->NeedsWriteBarrier());
3914 ExternalReference ptr = ToExternalReference(operand_value);
3915 __ Move(kScratchRegister, ptr);
3916 __ movp(operand, kScratchRegister);
3917 } else {
3918 Handle<Object> handle_value = ToHandle(operand_value);
3919 DCHECK(!hinstr->NeedsWriteBarrier());
3920 __ Move(operand, handle_value);
3921 }
3922 }
3923
3924 if (hinstr->NeedsWriteBarrier()) {
3925 Register value = ToRegister(instr->value());
3926 Register temp = access.IsInobject() ? ToRegister(instr->temp()) : object;
3927 // Update the write barrier for the object for in-object properties.
3928 __ RecordWriteField(write_register,
3929 offset,
3930 value,
3931 temp,
3932 kSaveFPRegs,
3933 EMIT_REMEMBERED_SET,
3934 hinstr->SmiCheckForWriteBarrier(),
3935 hinstr->PointersToHereCheckForValue());
3936 }
3937}
3938
3939
3940void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
3941 DCHECK(ToRegister(instr->context()).is(rsi));
3942 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
3943 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
3944
3945 if (instr->hydrogen()->HasVectorAndSlot()) {
3946 EmitVectorStoreICRegisters<LStoreNamedGeneric>(instr);
3947 }
3948
3949 __ Move(StoreDescriptor::NameRegister(), instr->hydrogen()->name());
3950 Handle<Code> ic = CodeFactory::StoreICInOptimizedCode(
3951 isolate(), instr->language_mode(),
3952 instr->hydrogen()->initialization_state()).code();
3953 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3954}
3955
3956
3957void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
3958 Representation representation = instr->hydrogen()->length()->representation();
3959 DCHECK(representation.Equals(instr->hydrogen()->index()->representation()));
3960 DCHECK(representation.IsSmiOrInteger32());
3961
3962 Condition cc = instr->hydrogen()->allow_equality() ? below : below_equal;
3963 if (instr->length()->IsConstantOperand()) {
3964 int32_t length = ToInteger32(LConstantOperand::cast(instr->length()));
3965 Register index = ToRegister(instr->index());
3966 if (representation.IsSmi()) {
3967 __ Cmp(index, Smi::FromInt(length));
3968 } else {
3969 __ cmpl(index, Immediate(length));
3970 }
3971 cc = CommuteCondition(cc);
3972 } else if (instr->index()->IsConstantOperand()) {
3973 int32_t index = ToInteger32(LConstantOperand::cast(instr->index()));
3974 if (instr->length()->IsRegister()) {
3975 Register length = ToRegister(instr->length());
3976 if (representation.IsSmi()) {
3977 __ Cmp(length, Smi::FromInt(index));
3978 } else {
3979 __ cmpl(length, Immediate(index));
3980 }
3981 } else {
3982 Operand length = ToOperand(instr->length());
3983 if (representation.IsSmi()) {
3984 __ Cmp(length, Smi::FromInt(index));
3985 } else {
3986 __ cmpl(length, Immediate(index));
3987 }
3988 }
3989 } else {
3990 Register index = ToRegister(instr->index());
3991 if (instr->length()->IsRegister()) {
3992 Register length = ToRegister(instr->length());
3993 if (representation.IsSmi()) {
3994 __ cmpp(length, index);
3995 } else {
3996 __ cmpl(length, index);
3997 }
3998 } else {
3999 Operand length = ToOperand(instr->length());
4000 if (representation.IsSmi()) {
4001 __ cmpp(length, index);
4002 } else {
4003 __ cmpl(length, index);
4004 }
4005 }
4006 }
4007 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
4008 Label done;
4009 __ j(NegateCondition(cc), &done, Label::kNear);
4010 __ int3();
4011 __ bind(&done);
4012 } else {
4013 DeoptimizeIf(cc, instr, Deoptimizer::kOutOfBounds);
4014 }
4015}
4016
4017
4018void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
4019 ElementsKind elements_kind = instr->elements_kind();
4020 LOperand* key = instr->key();
4021 if (kPointerSize == kInt32Size && !key->IsConstantOperand()) {
4022 Register key_reg = ToRegister(key);
4023 Representation key_representation =
4024 instr->hydrogen()->key()->representation();
4025 if (ExternalArrayOpRequiresTemp(key_representation, elements_kind)) {
4026 __ SmiToInteger64(key_reg, key_reg);
4027 } else if (instr->hydrogen()->IsDehoisted()) {
4028 // Sign extend key because it could be a 32 bit negative value
4029 // and the dehoisted address computation happens in 64 bits
4030 __ movsxlq(key_reg, key_reg);
4031 }
4032 }
4033 Operand operand(BuildFastArrayOperand(
4034 instr->elements(),
4035 key,
4036 instr->hydrogen()->key()->representation(),
4037 elements_kind,
4038 instr->base_offset()));
4039
4040 if (elements_kind == FLOAT32_ELEMENTS) {
4041 XMMRegister value(ToDoubleRegister(instr->value()));
4042 __ Cvtsd2ss(value, value);
4043 __ Movss(operand, value);
4044 } else if (elements_kind == FLOAT64_ELEMENTS) {
4045 __ Movsd(operand, ToDoubleRegister(instr->value()));
4046 } else {
4047 Register value(ToRegister(instr->value()));
4048 switch (elements_kind) {
4049 case INT8_ELEMENTS:
4050 case UINT8_ELEMENTS:
4051 case UINT8_CLAMPED_ELEMENTS:
4052 __ movb(operand, value);
4053 break;
4054 case INT16_ELEMENTS:
4055 case UINT16_ELEMENTS:
4056 __ movw(operand, value);
4057 break;
4058 case INT32_ELEMENTS:
4059 case UINT32_ELEMENTS:
4060 __ movl(operand, value);
4061 break;
4062 case FLOAT32_ELEMENTS:
4063 case FLOAT64_ELEMENTS:
4064 case FAST_ELEMENTS:
4065 case FAST_SMI_ELEMENTS:
4066 case FAST_DOUBLE_ELEMENTS:
4067 case FAST_HOLEY_ELEMENTS:
4068 case FAST_HOLEY_SMI_ELEMENTS:
4069 case FAST_HOLEY_DOUBLE_ELEMENTS:
4070 case DICTIONARY_ELEMENTS:
4071 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
4072 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
Ben Murdoch097c5b22016-05-18 11:27:45 +01004073 case FAST_STRING_WRAPPER_ELEMENTS:
4074 case SLOW_STRING_WRAPPER_ELEMENTS:
4075 case NO_ELEMENTS:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004076 UNREACHABLE();
4077 break;
4078 }
4079 }
4080}
4081
4082
4083void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
4084 XMMRegister value = ToDoubleRegister(instr->value());
4085 LOperand* key = instr->key();
4086 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
4087 instr->hydrogen()->IsDehoisted()) {
4088 // Sign extend key because it could be a 32 bit negative value
4089 // and the dehoisted address computation happens in 64 bits
4090 __ movsxlq(ToRegister(key), ToRegister(key));
4091 }
4092 if (instr->NeedsCanonicalization()) {
4093 XMMRegister xmm_scratch = double_scratch0();
4094 // Turn potential sNaN value into qNaN.
4095 __ Xorpd(xmm_scratch, xmm_scratch);
4096 __ Subsd(value, xmm_scratch);
4097 }
4098
4099 Operand double_store_operand = BuildFastArrayOperand(
4100 instr->elements(),
4101 key,
4102 instr->hydrogen()->key()->representation(),
4103 FAST_DOUBLE_ELEMENTS,
4104 instr->base_offset());
4105
4106 __ Movsd(double_store_operand, value);
4107}
4108
4109
4110void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4111 HStoreKeyed* hinstr = instr->hydrogen();
4112 LOperand* key = instr->key();
4113 int offset = instr->base_offset();
4114 Representation representation = hinstr->value()->representation();
4115
4116 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
4117 instr->hydrogen()->IsDehoisted()) {
4118 // Sign extend key because it could be a 32 bit negative value
4119 // and the dehoisted address computation happens in 64 bits
4120 __ movsxlq(ToRegister(key), ToRegister(key));
4121 }
4122 if (representation.IsInteger32() && SmiValuesAre32Bits()) {
4123 DCHECK(hinstr->store_mode() == STORE_TO_INITIALIZED_ENTRY);
4124 DCHECK(hinstr->elements_kind() == FAST_SMI_ELEMENTS);
4125 if (FLAG_debug_code) {
4126 Register scratch = kScratchRegister;
4127 __ Load(scratch,
4128 BuildFastArrayOperand(instr->elements(),
4129 key,
4130 instr->hydrogen()->key()->representation(),
4131 FAST_ELEMENTS,
4132 offset),
4133 Representation::Smi());
4134 __ AssertSmi(scratch);
4135 }
4136 // Store int value directly to upper half of the smi.
4137 STATIC_ASSERT(kSmiTag == 0);
4138 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
4139 offset += kPointerSize / 2;
4140 }
4141
4142 Operand operand =
4143 BuildFastArrayOperand(instr->elements(),
4144 key,
4145 instr->hydrogen()->key()->representation(),
4146 FAST_ELEMENTS,
4147 offset);
4148 if (instr->value()->IsRegister()) {
4149 __ Store(operand, ToRegister(instr->value()), representation);
4150 } else {
4151 LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
4152 if (IsInteger32Constant(operand_value)) {
4153 int32_t value = ToInteger32(operand_value);
4154 if (representation.IsSmi()) {
4155 __ Move(operand, Smi::FromInt(value));
4156
4157 } else {
4158 __ movl(operand, Immediate(value));
4159 }
4160 } else {
4161 Handle<Object> handle_value = ToHandle(operand_value);
4162 __ Move(operand, handle_value);
4163 }
4164 }
4165
4166 if (hinstr->NeedsWriteBarrier()) {
4167 Register elements = ToRegister(instr->elements());
4168 DCHECK(instr->value()->IsRegister());
4169 Register value = ToRegister(instr->value());
4170 DCHECK(!key->IsConstantOperand());
4171 SmiCheck check_needed = hinstr->value()->type().IsHeapObject()
4172 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4173 // Compute address of modified element and store it into key register.
4174 Register key_reg(ToRegister(key));
4175 __ leap(key_reg, operand);
4176 __ RecordWrite(elements,
4177 key_reg,
4178 value,
4179 kSaveFPRegs,
4180 EMIT_REMEMBERED_SET,
4181 check_needed,
4182 hinstr->PointersToHereCheckForValue());
4183 }
4184}
4185
4186
4187void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4188 if (instr->is_fixed_typed_array()) {
4189 DoStoreKeyedExternalArray(instr);
4190 } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4191 DoStoreKeyedFixedDoubleArray(instr);
4192 } else {
4193 DoStoreKeyedFixedArray(instr);
4194 }
4195}
4196
4197
4198void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4199 DCHECK(ToRegister(instr->context()).is(rsi));
4200 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4201 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4202 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4203
4204 if (instr->hydrogen()->HasVectorAndSlot()) {
4205 EmitVectorStoreICRegisters<LStoreKeyedGeneric>(instr);
4206 }
4207
4208 Handle<Code> ic = CodeFactory::KeyedStoreICInOptimizedCode(
4209 isolate(), instr->language_mode(),
4210 instr->hydrogen()->initialization_state()).code();
4211 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4212}
4213
4214
4215void LCodeGen::DoMaybeGrowElements(LMaybeGrowElements* instr) {
4216 class DeferredMaybeGrowElements final : public LDeferredCode {
4217 public:
4218 DeferredMaybeGrowElements(LCodeGen* codegen, LMaybeGrowElements* instr)
4219 : LDeferredCode(codegen), instr_(instr) {}
4220 void Generate() override { codegen()->DoDeferredMaybeGrowElements(instr_); }
4221 LInstruction* instr() override { return instr_; }
4222
4223 private:
4224 LMaybeGrowElements* instr_;
4225 };
4226
4227 Register result = rax;
4228 DeferredMaybeGrowElements* deferred =
4229 new (zone()) DeferredMaybeGrowElements(this, instr);
4230 LOperand* key = instr->key();
4231 LOperand* current_capacity = instr->current_capacity();
4232
4233 DCHECK(instr->hydrogen()->key()->representation().IsInteger32());
4234 DCHECK(instr->hydrogen()->current_capacity()->representation().IsInteger32());
4235 DCHECK(key->IsConstantOperand() || key->IsRegister());
4236 DCHECK(current_capacity->IsConstantOperand() ||
4237 current_capacity->IsRegister());
4238
4239 if (key->IsConstantOperand() && current_capacity->IsConstantOperand()) {
4240 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4241 int32_t constant_capacity =
4242 ToInteger32(LConstantOperand::cast(current_capacity));
4243 if (constant_key >= constant_capacity) {
4244 // Deferred case.
4245 __ jmp(deferred->entry());
4246 }
4247 } else if (key->IsConstantOperand()) {
4248 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4249 __ cmpl(ToRegister(current_capacity), Immediate(constant_key));
4250 __ j(less_equal, deferred->entry());
4251 } else if (current_capacity->IsConstantOperand()) {
4252 int32_t constant_capacity =
4253 ToInteger32(LConstantOperand::cast(current_capacity));
4254 __ cmpl(ToRegister(key), Immediate(constant_capacity));
4255 __ j(greater_equal, deferred->entry());
4256 } else {
4257 __ cmpl(ToRegister(key), ToRegister(current_capacity));
4258 __ j(greater_equal, deferred->entry());
4259 }
4260
4261 if (instr->elements()->IsRegister()) {
4262 __ movp(result, ToRegister(instr->elements()));
4263 } else {
4264 __ movp(result, ToOperand(instr->elements()));
4265 }
4266
4267 __ bind(deferred->exit());
4268}
4269
4270
4271void LCodeGen::DoDeferredMaybeGrowElements(LMaybeGrowElements* instr) {
4272 // TODO(3095996): Get rid of this. For now, we need to make the
4273 // result register contain a valid pointer because it is already
4274 // contained in the register pointer map.
4275 Register result = rax;
4276 __ Move(result, Smi::FromInt(0));
4277
4278 // We have to call a stub.
4279 {
4280 PushSafepointRegistersScope scope(this);
4281 if (instr->object()->IsConstantOperand()) {
4282 LConstantOperand* constant_object =
4283 LConstantOperand::cast(instr->object());
4284 if (IsSmiConstant(constant_object)) {
4285 Smi* immediate = ToSmi(constant_object);
4286 __ Move(result, immediate);
4287 } else {
4288 Handle<Object> handle_value = ToHandle(constant_object);
4289 __ Move(result, handle_value);
4290 }
4291 } else if (instr->object()->IsRegister()) {
4292 __ Move(result, ToRegister(instr->object()));
4293 } else {
4294 __ movp(result, ToOperand(instr->object()));
4295 }
4296
4297 LOperand* key = instr->key();
4298 if (key->IsConstantOperand()) {
4299 __ Move(rbx, ToSmi(LConstantOperand::cast(key)));
4300 } else {
4301 __ Move(rbx, ToRegister(key));
4302 __ Integer32ToSmi(rbx, rbx);
4303 }
4304
4305 GrowArrayElementsStub stub(isolate(), instr->hydrogen()->is_js_array(),
4306 instr->hydrogen()->kind());
4307 __ CallStub(&stub);
4308 RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0);
4309 __ StoreToSafepointRegisterSlot(result, result);
4310 }
4311
4312 // Deopt on smi, which means the elements array changed to dictionary mode.
4313 Condition is_smi = __ CheckSmi(result);
4314 DeoptimizeIf(is_smi, instr, Deoptimizer::kSmi);
4315}
4316
4317
4318void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4319 Register object_reg = ToRegister(instr->object());
4320
4321 Handle<Map> from_map = instr->original_map();
4322 Handle<Map> to_map = instr->transitioned_map();
4323 ElementsKind from_kind = instr->from_kind();
4324 ElementsKind to_kind = instr->to_kind();
4325
4326 Label not_applicable;
4327 __ Cmp(FieldOperand(object_reg, HeapObject::kMapOffset), from_map);
4328 __ j(not_equal, &not_applicable);
4329 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
4330 Register new_map_reg = ToRegister(instr->new_map_temp());
4331 __ Move(new_map_reg, to_map, RelocInfo::EMBEDDED_OBJECT);
4332 __ movp(FieldOperand(object_reg, HeapObject::kMapOffset), new_map_reg);
4333 // Write barrier.
4334 __ RecordWriteForMap(object_reg, new_map_reg, ToRegister(instr->temp()),
4335 kDontSaveFPRegs);
4336 } else {
4337 DCHECK(object_reg.is(rax));
4338 DCHECK(ToRegister(instr->context()).is(rsi));
4339 PushSafepointRegistersScope scope(this);
4340 __ Move(rbx, to_map);
4341 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4342 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4343 __ CallStub(&stub);
4344 RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0);
4345 }
4346 __ bind(&not_applicable);
4347}
4348
4349
4350void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4351 Register object = ToRegister(instr->object());
4352 Register temp = ToRegister(instr->temp());
4353 Label no_memento_found;
4354 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found);
4355 DeoptimizeIf(equal, instr, Deoptimizer::kMementoFound);
4356 __ bind(&no_memento_found);
4357}
4358
4359
4360void LCodeGen::DoStringAdd(LStringAdd* instr) {
4361 DCHECK(ToRegister(instr->context()).is(rsi));
4362 DCHECK(ToRegister(instr->left()).is(rdx));
4363 DCHECK(ToRegister(instr->right()).is(rax));
4364 StringAddStub stub(isolate(),
4365 instr->hydrogen()->flags(),
4366 instr->hydrogen()->pretenure_flag());
4367 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4368}
4369
4370
4371void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
4372 class DeferredStringCharCodeAt final : public LDeferredCode {
4373 public:
4374 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
4375 : LDeferredCode(codegen), instr_(instr) { }
4376 void Generate() override { codegen()->DoDeferredStringCharCodeAt(instr_); }
4377 LInstruction* instr() override { return instr_; }
4378
4379 private:
4380 LStringCharCodeAt* instr_;
4381 };
4382
4383 DeferredStringCharCodeAt* deferred =
4384 new(zone()) DeferredStringCharCodeAt(this, instr);
4385
4386 StringCharLoadGenerator::Generate(masm(),
4387 ToRegister(instr->string()),
4388 ToRegister(instr->index()),
4389 ToRegister(instr->result()),
4390 deferred->entry());
4391 __ bind(deferred->exit());
4392}
4393
4394
4395void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4396 Register string = ToRegister(instr->string());
4397 Register result = ToRegister(instr->result());
4398
4399 // TODO(3095996): Get rid of this. For now, we need to make the
4400 // result register contain a valid pointer because it is already
4401 // contained in the register pointer map.
4402 __ Set(result, 0);
4403
4404 PushSafepointRegistersScope scope(this);
4405 __ Push(string);
4406 // Push the index as a smi. This is safe because of the checks in
4407 // DoStringCharCodeAt above.
4408 STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue);
4409 if (instr->index()->IsConstantOperand()) {
4410 int32_t const_index = ToInteger32(LConstantOperand::cast(instr->index()));
4411 __ Push(Smi::FromInt(const_index));
4412 } else {
4413 Register index = ToRegister(instr->index());
4414 __ Integer32ToSmi(index, index);
4415 __ Push(index);
4416 }
4417 CallRuntimeFromDeferred(
4418 Runtime::kStringCharCodeAtRT, 2, instr, instr->context());
4419 __ AssertSmi(rax);
4420 __ SmiToInteger32(rax, rax);
4421 __ StoreToSafepointRegisterSlot(result, rax);
4422}
4423
4424
4425void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
4426 class DeferredStringCharFromCode final : public LDeferredCode {
4427 public:
4428 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
4429 : LDeferredCode(codegen), instr_(instr) { }
4430 void Generate() override {
4431 codegen()->DoDeferredStringCharFromCode(instr_);
4432 }
4433 LInstruction* instr() override { return instr_; }
4434
4435 private:
4436 LStringCharFromCode* instr_;
4437 };
4438
4439 DeferredStringCharFromCode* deferred =
4440 new(zone()) DeferredStringCharFromCode(this, instr);
4441
4442 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
4443 Register char_code = ToRegister(instr->char_code());
4444 Register result = ToRegister(instr->result());
4445 DCHECK(!char_code.is(result));
4446
4447 __ cmpl(char_code, Immediate(String::kMaxOneByteCharCode));
4448 __ j(above, deferred->entry());
4449 __ movsxlq(char_code, char_code);
4450 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
4451 __ movp(result, FieldOperand(result,
4452 char_code, times_pointer_size,
4453 FixedArray::kHeaderSize));
4454 __ CompareRoot(result, Heap::kUndefinedValueRootIndex);
4455 __ j(equal, deferred->entry());
4456 __ bind(deferred->exit());
4457}
4458
4459
4460void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4461 Register char_code = ToRegister(instr->char_code());
4462 Register result = ToRegister(instr->result());
4463
4464 // TODO(3095996): Get rid of this. For now, we need to make the
4465 // result register contain a valid pointer because it is already
4466 // contained in the register pointer map.
4467 __ Set(result, 0);
4468
4469 PushSafepointRegistersScope scope(this);
4470 __ Integer32ToSmi(char_code, char_code);
4471 __ Push(char_code);
4472 CallRuntimeFromDeferred(Runtime::kStringCharFromCode, 1, instr,
4473 instr->context());
4474 __ StoreToSafepointRegisterSlot(result, rax);
4475}
4476
4477
4478void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
4479 LOperand* input = instr->value();
4480 DCHECK(input->IsRegister() || input->IsStackSlot());
4481 LOperand* output = instr->result();
4482 DCHECK(output->IsDoubleRegister());
4483 if (input->IsRegister()) {
4484 __ Cvtlsi2sd(ToDoubleRegister(output), ToRegister(input));
4485 } else {
4486 __ Cvtlsi2sd(ToDoubleRegister(output), ToOperand(input));
4487 }
4488}
4489
4490
4491void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4492 LOperand* input = instr->value();
4493 LOperand* output = instr->result();
4494
4495 __ LoadUint32(ToDoubleRegister(output), ToRegister(input));
4496}
4497
4498
4499void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
4500 class DeferredNumberTagI final : public LDeferredCode {
4501 public:
4502 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
4503 : LDeferredCode(codegen), instr_(instr) { }
4504 void Generate() override {
4505 codegen()->DoDeferredNumberTagIU(instr_, instr_->value(), instr_->temp1(),
4506 instr_->temp2(), SIGNED_INT32);
4507 }
4508 LInstruction* instr() override { return instr_; }
4509
4510 private:
4511 LNumberTagI* instr_;
4512 };
4513
4514 LOperand* input = instr->value();
4515 DCHECK(input->IsRegister() && input->Equals(instr->result()));
4516 Register reg = ToRegister(input);
4517
4518 if (SmiValuesAre32Bits()) {
4519 __ Integer32ToSmi(reg, reg);
4520 } else {
4521 DCHECK(SmiValuesAre31Bits());
4522 DeferredNumberTagI* deferred = new(zone()) DeferredNumberTagI(this, instr);
4523 __ Integer32ToSmi(reg, reg);
4524 __ j(overflow, deferred->entry());
4525 __ bind(deferred->exit());
4526 }
4527}
4528
4529
4530void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4531 class DeferredNumberTagU final : public LDeferredCode {
4532 public:
4533 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4534 : LDeferredCode(codegen), instr_(instr) { }
4535 void Generate() override {
4536 codegen()->DoDeferredNumberTagIU(instr_, instr_->value(), instr_->temp1(),
4537 instr_->temp2(), UNSIGNED_INT32);
4538 }
4539 LInstruction* instr() override { return instr_; }
4540
4541 private:
4542 LNumberTagU* instr_;
4543 };
4544
4545 LOperand* input = instr->value();
4546 DCHECK(input->IsRegister() && input->Equals(instr->result()));
4547 Register reg = ToRegister(input);
4548
4549 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4550 __ cmpl(reg, Immediate(Smi::kMaxValue));
4551 __ j(above, deferred->entry());
4552 __ Integer32ToSmi(reg, reg);
4553 __ bind(deferred->exit());
4554}
4555
4556
4557void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4558 LOperand* value,
4559 LOperand* temp1,
4560 LOperand* temp2,
4561 IntegerSignedness signedness) {
4562 Label done, slow;
4563 Register reg = ToRegister(value);
4564 Register tmp = ToRegister(temp1);
4565 XMMRegister temp_xmm = ToDoubleRegister(temp2);
4566
4567 // Load value into temp_xmm which will be preserved across potential call to
4568 // runtime (MacroAssembler::EnterExitFrameEpilogue preserves only allocatable
4569 // XMM registers on x64).
4570 if (signedness == SIGNED_INT32) {
4571 DCHECK(SmiValuesAre31Bits());
4572 // There was overflow, so bits 30 and 31 of the original integer
4573 // disagree. Try to allocate a heap number in new space and store
4574 // the value in there. If that fails, call the runtime system.
4575 __ SmiToInteger32(reg, reg);
4576 __ xorl(reg, Immediate(0x80000000));
4577 __ Cvtlsi2sd(temp_xmm, reg);
4578 } else {
4579 DCHECK(signedness == UNSIGNED_INT32);
4580 __ LoadUint32(temp_xmm, reg);
4581 }
4582
4583 if (FLAG_inline_new) {
4584 __ AllocateHeapNumber(reg, tmp, &slow);
4585 __ jmp(&done, kPointerSize == kInt64Size ? Label::kNear : Label::kFar);
4586 }
4587
4588 // Slow case: Call the runtime system to do the number allocation.
4589 __ bind(&slow);
4590 {
4591 // Put a valid pointer value in the stack slot where the result
4592 // register is stored, as this register is in the pointer map, but contains
4593 // an integer value.
4594 __ Set(reg, 0);
4595
4596 // Preserve the value of all registers.
4597 PushSafepointRegistersScope scope(this);
4598
4599 // NumberTagIU uses the context from the frame, rather than
4600 // the environment's HContext or HInlinedContext value.
4601 // They only call Runtime::kAllocateHeapNumber.
4602 // The corresponding HChange instructions are added in a phase that does
4603 // not have easy access to the local context.
4604 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
4605 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4606 RecordSafepointWithRegisters(
4607 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4608 __ StoreToSafepointRegisterSlot(reg, rax);
4609 }
4610
4611 // Done. Put the value in temp_xmm into the value of the allocated heap
4612 // number.
4613 __ bind(&done);
4614 __ Movsd(FieldOperand(reg, HeapNumber::kValueOffset), temp_xmm);
4615}
4616
4617
4618void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
4619 class DeferredNumberTagD final : public LDeferredCode {
4620 public:
4621 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4622 : LDeferredCode(codegen), instr_(instr) { }
4623 void Generate() override { codegen()->DoDeferredNumberTagD(instr_); }
4624 LInstruction* instr() override { return instr_; }
4625
4626 private:
4627 LNumberTagD* instr_;
4628 };
4629
4630 XMMRegister input_reg = ToDoubleRegister(instr->value());
4631 Register reg = ToRegister(instr->result());
4632 Register tmp = ToRegister(instr->temp());
4633
4634 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
4635 if (FLAG_inline_new) {
4636 __ AllocateHeapNumber(reg, tmp, deferred->entry());
4637 } else {
4638 __ jmp(deferred->entry());
4639 }
4640 __ bind(deferred->exit());
4641 __ Movsd(FieldOperand(reg, HeapNumber::kValueOffset), input_reg);
4642}
4643
4644
4645void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4646 // TODO(3095996): Get rid of this. For now, we need to make the
4647 // result register contain a valid pointer because it is already
4648 // contained in the register pointer map.
4649 Register reg = ToRegister(instr->result());
4650 __ Move(reg, Smi::FromInt(0));
4651
4652 {
4653 PushSafepointRegistersScope scope(this);
4654 // NumberTagD uses the context from the frame, rather than
4655 // the environment's HContext or HInlinedContext value.
4656 // They only call Runtime::kAllocateHeapNumber.
4657 // The corresponding HChange instructions are added in a phase that does
4658 // not have easy access to the local context.
4659 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
4660 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4661 RecordSafepointWithRegisters(
4662 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4663 __ movp(kScratchRegister, rax);
4664 }
4665 __ movp(reg, kScratchRegister);
4666}
4667
4668
4669void LCodeGen::DoSmiTag(LSmiTag* instr) {
4670 HChange* hchange = instr->hydrogen();
4671 Register input = ToRegister(instr->value());
4672 Register output = ToRegister(instr->result());
4673 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4674 hchange->value()->CheckFlag(HValue::kUint32)) {
4675 Condition is_smi = __ CheckUInteger32ValidSmiValue(input);
4676 DeoptimizeIf(NegateCondition(is_smi), instr, Deoptimizer::kOverflow);
4677 }
4678 __ Integer32ToSmi(output, input);
4679 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4680 !hchange->value()->CheckFlag(HValue::kUint32)) {
4681 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
4682 }
4683}
4684
4685
4686void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4687 DCHECK(instr->value()->Equals(instr->result()));
4688 Register input = ToRegister(instr->value());
4689 if (instr->needs_check()) {
4690 Condition is_smi = __ CheckSmi(input);
4691 DeoptimizeIf(NegateCondition(is_smi), instr, Deoptimizer::kNotASmi);
4692 } else {
4693 __ AssertSmi(input);
4694 }
4695 __ SmiToInteger32(input, input);
4696}
4697
4698
4699void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
4700 XMMRegister result_reg, NumberUntagDMode mode) {
4701 bool can_convert_undefined_to_nan =
4702 instr->hydrogen()->can_convert_undefined_to_nan();
4703 bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
4704
4705 Label convert, load_smi, done;
4706
4707 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4708 // Smi check.
4709 __ JumpIfSmi(input_reg, &load_smi, Label::kNear);
4710
4711 // Heap number map check.
4712 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
4713 Heap::kHeapNumberMapRootIndex);
4714
4715 // On x64 it is safe to load at heap number offset before evaluating the map
4716 // check, since all heap objects are at least two words long.
4717 __ Movsd(result_reg, FieldOperand(input_reg, HeapNumber::kValueOffset));
4718
4719 if (can_convert_undefined_to_nan) {
4720 __ j(not_equal, &convert, Label::kNear);
4721 } else {
4722 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
4723 }
4724
4725 if (deoptimize_on_minus_zero) {
4726 XMMRegister xmm_scratch = double_scratch0();
4727 __ Xorpd(xmm_scratch, xmm_scratch);
4728 __ Ucomisd(xmm_scratch, result_reg);
4729 __ j(not_equal, &done, Label::kNear);
4730 __ Movmskpd(kScratchRegister, result_reg);
4731 __ testl(kScratchRegister, Immediate(1));
4732 DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero);
4733 }
4734 __ jmp(&done, Label::kNear);
4735
4736 if (can_convert_undefined_to_nan) {
4737 __ bind(&convert);
4738
4739 // Convert undefined (and hole) to NaN. Compute NaN as 0/0.
4740 __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex);
4741 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumberUndefined);
4742
4743 __ Pcmpeqd(result_reg, result_reg);
4744 __ jmp(&done, Label::kNear);
4745 }
4746 } else {
4747 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
4748 }
4749
4750 // Smi to XMM conversion
4751 __ bind(&load_smi);
4752 __ SmiToInteger32(kScratchRegister, input_reg);
4753 __ Cvtlsi2sd(result_reg, kScratchRegister);
4754 __ bind(&done);
4755}
4756
4757
4758void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr, Label* done) {
4759 Register input_reg = ToRegister(instr->value());
4760
4761 if (instr->truncating()) {
4762 Label no_heap_number, check_bools, check_false;
4763
4764 // Heap number map check.
4765 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
4766 Heap::kHeapNumberMapRootIndex);
4767 __ j(not_equal, &no_heap_number, Label::kNear);
4768 __ TruncateHeapNumberToI(input_reg, input_reg);
4769 __ jmp(done);
4770
4771 __ bind(&no_heap_number);
4772 // Check for Oddballs. Undefined/False is converted to zero and True to one
4773 // for truncating conversions.
4774 __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex);
4775 __ j(not_equal, &check_bools, Label::kNear);
4776 __ Set(input_reg, 0);
4777 __ jmp(done);
4778
4779 __ bind(&check_bools);
4780 __ CompareRoot(input_reg, Heap::kTrueValueRootIndex);
4781 __ j(not_equal, &check_false, Label::kNear);
4782 __ Set(input_reg, 1);
4783 __ jmp(done);
4784
4785 __ bind(&check_false);
4786 __ CompareRoot(input_reg, Heap::kFalseValueRootIndex);
4787 DeoptimizeIf(not_equal, instr,
4788 Deoptimizer::kNotAHeapNumberUndefinedBoolean);
4789 __ Set(input_reg, 0);
4790 } else {
4791 XMMRegister scratch = ToDoubleRegister(instr->temp());
4792 DCHECK(!scratch.is(xmm0));
4793 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
4794 Heap::kHeapNumberMapRootIndex);
4795 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
4796 __ Movsd(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset));
4797 __ Cvttsd2si(input_reg, xmm0);
4798 __ Cvtlsi2sd(scratch, input_reg);
4799 __ Ucomisd(xmm0, scratch);
4800 DeoptimizeIf(not_equal, instr, Deoptimizer::kLostPrecision);
4801 DeoptimizeIf(parity_even, instr, Deoptimizer::kNaN);
4802 if (instr->hydrogen()->GetMinusZeroMode() == FAIL_ON_MINUS_ZERO) {
4803 __ testl(input_reg, input_reg);
4804 __ j(not_zero, done);
4805 __ Movmskpd(input_reg, xmm0);
4806 __ andl(input_reg, Immediate(1));
4807 DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero);
4808 }
4809 }
4810}
4811
4812
4813void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
4814 class DeferredTaggedToI final : public LDeferredCode {
4815 public:
4816 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
4817 : LDeferredCode(codegen), instr_(instr) { }
4818 void Generate() override { codegen()->DoDeferredTaggedToI(instr_, done()); }
4819 LInstruction* instr() override { return instr_; }
4820
4821 private:
4822 LTaggedToI* instr_;
4823 };
4824
4825 LOperand* input = instr->value();
4826 DCHECK(input->IsRegister());
4827 DCHECK(input->Equals(instr->result()));
4828 Register input_reg = ToRegister(input);
4829
4830 if (instr->hydrogen()->value()->representation().IsSmi()) {
4831 __ SmiToInteger32(input_reg, input_reg);
4832 } else {
4833 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
4834 __ JumpIfNotSmi(input_reg, deferred->entry());
4835 __ SmiToInteger32(input_reg, input_reg);
4836 __ bind(deferred->exit());
4837 }
4838}
4839
4840
4841void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
4842 LOperand* input = instr->value();
4843 DCHECK(input->IsRegister());
4844 LOperand* result = instr->result();
4845 DCHECK(result->IsDoubleRegister());
4846
4847 Register input_reg = ToRegister(input);
4848 XMMRegister result_reg = ToDoubleRegister(result);
4849
4850 HValue* value = instr->hydrogen()->value();
4851 NumberUntagDMode mode = value->representation().IsSmi()
4852 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
4853
4854 EmitNumberUntagD(instr, input_reg, result_reg, mode);
4855}
4856
4857
4858void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
4859 LOperand* input = instr->value();
4860 DCHECK(input->IsDoubleRegister());
4861 LOperand* result = instr->result();
4862 DCHECK(result->IsRegister());
4863
4864 XMMRegister input_reg = ToDoubleRegister(input);
4865 Register result_reg = ToRegister(result);
4866
4867 if (instr->truncating()) {
4868 __ TruncateDoubleToI(result_reg, input_reg);
4869 } else {
4870 Label lost_precision, is_nan, minus_zero, done;
4871 XMMRegister xmm_scratch = double_scratch0();
4872 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
4873 __ DoubleToI(result_reg, input_reg, xmm_scratch,
4874 instr->hydrogen()->GetMinusZeroMode(), &lost_precision,
4875 &is_nan, &minus_zero, dist);
4876 __ jmp(&done, dist);
4877 __ bind(&lost_precision);
4878 DeoptimizeIf(no_condition, instr, Deoptimizer::kLostPrecision);
4879 __ bind(&is_nan);
4880 DeoptimizeIf(no_condition, instr, Deoptimizer::kNaN);
4881 __ bind(&minus_zero);
4882 DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero);
4883 __ bind(&done);
4884 }
4885}
4886
4887
4888void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
4889 LOperand* input = instr->value();
4890 DCHECK(input->IsDoubleRegister());
4891 LOperand* result = instr->result();
4892 DCHECK(result->IsRegister());
4893
4894 XMMRegister input_reg = ToDoubleRegister(input);
4895 Register result_reg = ToRegister(result);
4896
4897 Label lost_precision, is_nan, minus_zero, done;
4898 XMMRegister xmm_scratch = double_scratch0();
4899 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
4900 __ DoubleToI(result_reg, input_reg, xmm_scratch,
4901 instr->hydrogen()->GetMinusZeroMode(), &lost_precision, &is_nan,
4902 &minus_zero, dist);
4903 __ jmp(&done, dist);
4904 __ bind(&lost_precision);
4905 DeoptimizeIf(no_condition, instr, Deoptimizer::kLostPrecision);
4906 __ bind(&is_nan);
4907 DeoptimizeIf(no_condition, instr, Deoptimizer::kNaN);
4908 __ bind(&minus_zero);
4909 DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero);
4910 __ bind(&done);
4911 __ Integer32ToSmi(result_reg, result_reg);
4912 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
4913}
4914
4915
4916void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
4917 LOperand* input = instr->value();
4918 Condition cc = masm()->CheckSmi(ToRegister(input));
4919 DeoptimizeIf(NegateCondition(cc), instr, Deoptimizer::kNotASmi);
4920}
4921
4922
4923void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
4924 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
4925 LOperand* input = instr->value();
4926 Condition cc = masm()->CheckSmi(ToRegister(input));
4927 DeoptimizeIf(cc, instr, Deoptimizer::kSmi);
4928 }
4929}
4930
4931
4932void LCodeGen::DoCheckArrayBufferNotNeutered(
4933 LCheckArrayBufferNotNeutered* instr) {
4934 Register view = ToRegister(instr->view());
4935
4936 __ movp(kScratchRegister,
4937 FieldOperand(view, JSArrayBufferView::kBufferOffset));
4938 __ testb(FieldOperand(kScratchRegister, JSArrayBuffer::kBitFieldOffset),
4939 Immediate(1 << JSArrayBuffer::WasNeutered::kShift));
4940 DeoptimizeIf(not_zero, instr, Deoptimizer::kOutOfBounds);
4941}
4942
4943
4944void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
4945 Register input = ToRegister(instr->value());
4946
4947 __ movp(kScratchRegister, FieldOperand(input, HeapObject::kMapOffset));
4948
4949 if (instr->hydrogen()->is_interval_check()) {
4950 InstanceType first;
4951 InstanceType last;
4952 instr->hydrogen()->GetCheckInterval(&first, &last);
4953
4954 __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
4955 Immediate(static_cast<int8_t>(first)));
4956
4957 // If there is only one type in the interval check for equality.
4958 if (first == last) {
4959 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongInstanceType);
4960 } else {
4961 DeoptimizeIf(below, instr, Deoptimizer::kWrongInstanceType);
4962 // Omit check for the last type.
4963 if (last != LAST_TYPE) {
4964 __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
4965 Immediate(static_cast<int8_t>(last)));
4966 DeoptimizeIf(above, instr, Deoptimizer::kWrongInstanceType);
4967 }
4968 }
4969 } else {
4970 uint8_t mask;
4971 uint8_t tag;
4972 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
4973
4974 if (base::bits::IsPowerOfTwo32(mask)) {
4975 DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
4976 __ testb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
4977 Immediate(mask));
4978 DeoptimizeIf(tag == 0 ? not_zero : zero, instr,
4979 Deoptimizer::kWrongInstanceType);
4980 } else {
4981 __ movzxbl(kScratchRegister,
4982 FieldOperand(kScratchRegister, Map::kInstanceTypeOffset));
4983 __ andb(kScratchRegister, Immediate(mask));
4984 __ cmpb(kScratchRegister, Immediate(tag));
4985 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongInstanceType);
4986 }
4987 }
4988}
4989
4990
4991void LCodeGen::DoCheckValue(LCheckValue* instr) {
4992 Register reg = ToRegister(instr->value());
4993 __ Cmp(reg, instr->hydrogen()->object().handle());
4994 DeoptimizeIf(not_equal, instr, Deoptimizer::kValueMismatch);
4995}
4996
4997
4998void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
4999 {
5000 PushSafepointRegistersScope scope(this);
5001 __ Push(object);
5002 __ Set(rsi, 0);
5003 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
5004 RecordSafepointWithRegisters(
5005 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
5006
5007 __ testp(rax, Immediate(kSmiTagMask));
5008 }
5009 DeoptimizeIf(zero, instr, Deoptimizer::kInstanceMigrationFailed);
5010}
5011
5012
5013void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
5014 class DeferredCheckMaps final : public LDeferredCode {
5015 public:
5016 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
5017 : LDeferredCode(codegen), instr_(instr), object_(object) {
5018 SetExit(check_maps());
5019 }
5020 void Generate() override {
5021 codegen()->DoDeferredInstanceMigration(instr_, object_);
5022 }
5023 Label* check_maps() { return &check_maps_; }
5024 LInstruction* instr() override { return instr_; }
5025
5026 private:
5027 LCheckMaps* instr_;
5028 Label check_maps_;
5029 Register object_;
5030 };
5031
5032 if (instr->hydrogen()->IsStabilityCheck()) {
5033 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5034 for (int i = 0; i < maps->size(); ++i) {
5035 AddStabilityDependency(maps->at(i).handle());
5036 }
5037 return;
5038 }
5039
5040 LOperand* input = instr->value();
5041 DCHECK(input->IsRegister());
5042 Register reg = ToRegister(input);
5043
5044 DeferredCheckMaps* deferred = NULL;
5045 if (instr->hydrogen()->HasMigrationTarget()) {
5046 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5047 __ bind(deferred->check_maps());
5048 }
5049
5050 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5051 Label success;
5052 for (int i = 0; i < maps->size() - 1; i++) {
5053 Handle<Map> map = maps->at(i).handle();
5054 __ CompareMap(reg, map);
5055 __ j(equal, &success, Label::kNear);
5056 }
5057
5058 Handle<Map> map = maps->at(maps->size() - 1).handle();
5059 __ CompareMap(reg, map);
5060 if (instr->hydrogen()->HasMigrationTarget()) {
5061 __ j(not_equal, deferred->entry());
5062 } else {
5063 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongMap);
5064 }
5065
5066 __ bind(&success);
5067}
5068
5069
5070void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
5071 XMMRegister value_reg = ToDoubleRegister(instr->unclamped());
5072 XMMRegister xmm_scratch = double_scratch0();
5073 Register result_reg = ToRegister(instr->result());
5074 __ ClampDoubleToUint8(value_reg, xmm_scratch, result_reg);
5075}
5076
5077
5078void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
5079 DCHECK(instr->unclamped()->Equals(instr->result()));
5080 Register value_reg = ToRegister(instr->result());
5081 __ ClampUint8(value_reg);
5082}
5083
5084
5085void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
5086 DCHECK(instr->unclamped()->Equals(instr->result()));
5087 Register input_reg = ToRegister(instr->unclamped());
5088 XMMRegister temp_xmm_reg = ToDoubleRegister(instr->temp_xmm());
5089 XMMRegister xmm_scratch = double_scratch0();
5090 Label is_smi, done, heap_number;
5091 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
5092 __ JumpIfSmi(input_reg, &is_smi, dist);
5093
5094 // Check for heap number
5095 __ Cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
5096 factory()->heap_number_map());
5097 __ j(equal, &heap_number, Label::kNear);
5098
5099 // Check for undefined. Undefined is converted to zero for clamping
5100 // conversions.
5101 __ Cmp(input_reg, factory()->undefined_value());
5102 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumberUndefined);
5103 __ xorl(input_reg, input_reg);
5104 __ jmp(&done, Label::kNear);
5105
5106 // Heap number
5107 __ bind(&heap_number);
5108 __ Movsd(xmm_scratch, FieldOperand(input_reg, HeapNumber::kValueOffset));
5109 __ ClampDoubleToUint8(xmm_scratch, temp_xmm_reg, input_reg);
5110 __ jmp(&done, Label::kNear);
5111
5112 // smi
5113 __ bind(&is_smi);
5114 __ SmiToInteger32(input_reg, input_reg);
5115 __ ClampUint8(input_reg);
5116
5117 __ bind(&done);
5118}
5119
5120
5121void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5122 XMMRegister value_reg = ToDoubleRegister(instr->value());
5123 Register result_reg = ToRegister(instr->result());
5124 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5125 __ Movq(result_reg, value_reg);
5126 __ shrq(result_reg, Immediate(32));
5127 } else {
5128 __ Movd(result_reg, value_reg);
5129 }
5130}
5131
5132
5133void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5134 Register hi_reg = ToRegister(instr->hi());
5135 Register lo_reg = ToRegister(instr->lo());
5136 XMMRegister result_reg = ToDoubleRegister(instr->result());
5137 __ movl(kScratchRegister, hi_reg);
5138 __ shlq(kScratchRegister, Immediate(32));
5139 __ orq(kScratchRegister, lo_reg);
5140 __ Movq(result_reg, kScratchRegister);
5141}
5142
5143
5144void LCodeGen::DoAllocate(LAllocate* instr) {
5145 class DeferredAllocate final : public LDeferredCode {
5146 public:
5147 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
5148 : LDeferredCode(codegen), instr_(instr) { }
5149 void Generate() override { codegen()->DoDeferredAllocate(instr_); }
5150 LInstruction* instr() override { return instr_; }
5151
5152 private:
5153 LAllocate* instr_;
5154 };
5155
5156 DeferredAllocate* deferred =
5157 new(zone()) DeferredAllocate(this, instr);
5158
5159 Register result = ToRegister(instr->result());
5160 Register temp = ToRegister(instr->temp());
5161
5162 // Allocate memory for the object.
Ben Murdochc5610432016-08-08 18:44:38 +01005163 AllocationFlags flags = NO_ALLOCATION_FLAGS;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005164 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5165 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5166 }
5167 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5168 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5169 flags = static_cast<AllocationFlags>(flags | PRETENURE);
5170 }
5171
Ben Murdochc5610432016-08-08 18:44:38 +01005172 if (instr->hydrogen()->IsAllocationFoldingDominator()) {
5173 flags = static_cast<AllocationFlags>(flags | ALLOCATION_FOLDING_DOMINATOR);
5174 }
5175 DCHECK(!instr->hydrogen()->IsAllocationFolded());
5176
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005177 if (instr->size()->IsConstantOperand()) {
5178 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5179 CHECK(size <= Page::kMaxRegularHeapObjectSize);
5180 __ Allocate(size, result, temp, no_reg, deferred->entry(), flags);
5181 } else {
5182 Register size = ToRegister(instr->size());
5183 __ Allocate(size, result, temp, no_reg, deferred->entry(), flags);
5184 }
5185
5186 __ bind(deferred->exit());
5187
5188 if (instr->hydrogen()->MustPrefillWithFiller()) {
5189 if (instr->size()->IsConstantOperand()) {
5190 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5191 __ movl(temp, Immediate((size / kPointerSize) - 1));
5192 } else {
5193 temp = ToRegister(instr->size());
5194 __ sarp(temp, Immediate(kPointerSizeLog2));
5195 __ decl(temp);
5196 }
5197 Label loop;
5198 __ bind(&loop);
5199 __ Move(FieldOperand(result, temp, times_pointer_size, 0),
5200 isolate()->factory()->one_pointer_filler_map());
5201 __ decl(temp);
5202 __ j(not_zero, &loop);
5203 }
5204}
5205
Ben Murdochc5610432016-08-08 18:44:38 +01005206void LCodeGen::DoFastAllocate(LFastAllocate* instr) {
5207 DCHECK(instr->hydrogen()->IsAllocationFolded());
5208 DCHECK(!instr->hydrogen()->IsAllocationFoldingDominator());
5209 Register result = ToRegister(instr->result());
5210 Register temp = ToRegister(instr->temp());
5211
5212 AllocationFlags flags = ALLOCATION_FOLDED;
5213 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5214 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5215 }
5216 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5217 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5218 flags = static_cast<AllocationFlags>(flags | PRETENURE);
5219 }
5220 if (instr->size()->IsConstantOperand()) {
5221 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5222 CHECK(size <= Page::kMaxRegularHeapObjectSize);
5223 __ FastAllocate(size, result, temp, flags);
5224 } else {
5225 Register size = ToRegister(instr->size());
5226 __ FastAllocate(size, result, temp, flags);
5227 }
5228}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005229
5230void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
5231 Register result = ToRegister(instr->result());
5232
5233 // TODO(3095996): Get rid of this. For now, we need to make the
5234 // result register contain a valid pointer because it is already
5235 // contained in the register pointer map.
5236 __ Move(result, Smi::FromInt(0));
5237
5238 PushSafepointRegistersScope scope(this);
5239 if (instr->size()->IsRegister()) {
5240 Register size = ToRegister(instr->size());
5241 DCHECK(!size.is(result));
5242 __ Integer32ToSmi(size, size);
5243 __ Push(size);
5244 } else {
5245 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5246 __ Push(Smi::FromInt(size));
5247 }
5248
5249 int flags = 0;
5250 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5251 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5252 flags = AllocateTargetSpace::update(flags, OLD_SPACE);
5253 } else {
5254 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5255 }
5256 __ Push(Smi::FromInt(flags));
5257
5258 CallRuntimeFromDeferred(
5259 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
5260 __ StoreToSafepointRegisterSlot(result, rax);
Ben Murdochc5610432016-08-08 18:44:38 +01005261
5262 if (instr->hydrogen()->IsAllocationFoldingDominator()) {
5263 AllocationFlags allocation_flags = NO_ALLOCATION_FLAGS;
5264 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5265 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5266 allocation_flags = static_cast<AllocationFlags>(flags | PRETENURE);
5267 }
5268 // If the allocation folding dominator allocate triggered a GC, allocation
5269 // happend in the runtime. We have to reset the top pointer to virtually
5270 // undo the allocation.
5271 ExternalReference allocation_top =
5272 AllocationUtils::GetAllocationTopReference(isolate(), allocation_flags);
5273 __ subp(rax, Immediate(kHeapObjectTag));
5274 __ Store(allocation_top, rax);
5275 __ addp(rax, Immediate(kHeapObjectTag));
5276 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005277}
5278
5279
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005280void LCodeGen::DoTypeof(LTypeof* instr) {
5281 DCHECK(ToRegister(instr->context()).is(rsi));
5282 DCHECK(ToRegister(instr->value()).is(rbx));
5283 Label end, do_call;
5284 Register value_register = ToRegister(instr->value());
5285 __ JumpIfNotSmi(value_register, &do_call);
5286 __ Move(rax, isolate()->factory()->number_string());
5287 __ jmp(&end);
5288 __ bind(&do_call);
5289 TypeofStub stub(isolate());
5290 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5291 __ bind(&end);
5292}
5293
5294
5295void LCodeGen::EmitPushTaggedOperand(LOperand* operand) {
5296 DCHECK(!operand->IsDoubleRegister());
5297 if (operand->IsConstantOperand()) {
5298 __ Push(ToHandle(LConstantOperand::cast(operand)));
5299 } else if (operand->IsRegister()) {
5300 __ Push(ToRegister(operand));
5301 } else {
5302 __ Push(ToOperand(operand));
5303 }
5304}
5305
5306
5307void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5308 Register input = ToRegister(instr->value());
5309 Condition final_branch_condition = EmitTypeofIs(instr, input);
5310 if (final_branch_condition != no_condition) {
5311 EmitBranch(instr, final_branch_condition);
5312 }
5313}
5314
5315
5316Condition LCodeGen::EmitTypeofIs(LTypeofIsAndBranch* instr, Register input) {
5317 Label* true_label = instr->TrueLabel(chunk_);
5318 Label* false_label = instr->FalseLabel(chunk_);
5319 Handle<String> type_name = instr->type_literal();
5320 int left_block = instr->TrueDestination(chunk_);
5321 int right_block = instr->FalseDestination(chunk_);
5322 int next_block = GetNextEmittedBlock();
5323
5324 Label::Distance true_distance = left_block == next_block ? Label::kNear
5325 : Label::kFar;
5326 Label::Distance false_distance = right_block == next_block ? Label::kNear
5327 : Label::kFar;
5328 Condition final_branch_condition = no_condition;
5329 Factory* factory = isolate()->factory();
5330 if (String::Equals(type_name, factory->number_string())) {
5331 __ JumpIfSmi(input, true_label, true_distance);
5332 __ CompareRoot(FieldOperand(input, HeapObject::kMapOffset),
5333 Heap::kHeapNumberMapRootIndex);
5334
5335 final_branch_condition = equal;
5336
5337 } else if (String::Equals(type_name, factory->string_string())) {
5338 __ JumpIfSmi(input, false_label, false_distance);
5339 __ CmpObjectType(input, FIRST_NONSTRING_TYPE, input);
5340 final_branch_condition = below;
5341
5342 } else if (String::Equals(type_name, factory->symbol_string())) {
5343 __ JumpIfSmi(input, false_label, false_distance);
5344 __ CmpObjectType(input, SYMBOL_TYPE, input);
5345 final_branch_condition = equal;
5346
5347 } else if (String::Equals(type_name, factory->boolean_string())) {
5348 __ CompareRoot(input, Heap::kTrueValueRootIndex);
5349 __ j(equal, true_label, true_distance);
5350 __ CompareRoot(input, Heap::kFalseValueRootIndex);
5351 final_branch_condition = equal;
5352
5353 } else if (String::Equals(type_name, factory->undefined_string())) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01005354 __ CompareRoot(input, Heap::kNullValueRootIndex);
5355 __ j(equal, false_label, false_distance);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005356 __ JumpIfSmi(input, false_label, false_distance);
5357 // Check for undetectable objects => true.
5358 __ movp(input, FieldOperand(input, HeapObject::kMapOffset));
5359 __ testb(FieldOperand(input, Map::kBitFieldOffset),
5360 Immediate(1 << Map::kIsUndetectable));
5361 final_branch_condition = not_zero;
5362
5363 } else if (String::Equals(type_name, factory->function_string())) {
5364 __ JumpIfSmi(input, false_label, false_distance);
5365 // Check for callable and not undetectable objects => true.
5366 __ movp(input, FieldOperand(input, HeapObject::kMapOffset));
5367 __ movzxbl(input, FieldOperand(input, Map::kBitFieldOffset));
5368 __ andb(input,
5369 Immediate((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable)));
5370 __ cmpb(input, Immediate(1 << Map::kIsCallable));
5371 final_branch_condition = equal;
5372
5373 } else if (String::Equals(type_name, factory->object_string())) {
5374 __ JumpIfSmi(input, false_label, false_distance);
5375 __ CompareRoot(input, Heap::kNullValueRootIndex);
5376 __ j(equal, true_label, true_distance);
5377 STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
5378 __ CmpObjectType(input, FIRST_JS_RECEIVER_TYPE, input);
5379 __ j(below, false_label, false_distance);
5380 // Check for callable or undetectable objects => false.
5381 __ testb(FieldOperand(input, Map::kBitFieldOffset),
5382 Immediate((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable)));
5383 final_branch_condition = zero;
5384
5385// clang-format off
5386#define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
5387 } else if (String::Equals(type_name, factory->type##_string())) { \
5388 __ JumpIfSmi(input, false_label, false_distance); \
5389 __ CompareRoot(FieldOperand(input, HeapObject::kMapOffset), \
5390 Heap::k##Type##MapRootIndex); \
5391 final_branch_condition = equal;
5392 SIMD128_TYPES(SIMD128_TYPE)
5393#undef SIMD128_TYPE
5394 // clang-format on
5395
5396 } else {
5397 __ jmp(false_label, false_distance);
5398 }
5399
5400 return final_branch_condition;
5401}
5402
5403
5404void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5405 if (info()->ShouldEnsureSpaceForLazyDeopt()) {
5406 // Ensure that we have enough space after the previous lazy-bailout
5407 // instruction for patching the code here.
5408 int current_pc = masm()->pc_offset();
5409 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5410 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5411 __ Nop(padding_size);
5412 }
5413 }
5414 last_lazy_deopt_pc_ = masm()->pc_offset();
5415}
5416
5417
5418void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
5419 last_lazy_deopt_pc_ = masm()->pc_offset();
5420 DCHECK(instr->HasEnvironment());
5421 LEnvironment* env = instr->environment();
5422 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5423 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5424}
5425
5426
5427void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
5428 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5429 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5430 // needed return address), even though the implementation of LAZY and EAGER is
5431 // now identical. When LAZY is eventually completely folded into EAGER, remove
5432 // the special case below.
5433 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5434 type = Deoptimizer::LAZY;
5435 }
5436 DeoptimizeIf(no_condition, instr, instr->hydrogen()->reason(), type);
5437}
5438
5439
5440void LCodeGen::DoDummy(LDummy* instr) {
5441 // Nothing to see here, move on!
5442}
5443
5444
5445void LCodeGen::DoDummyUse(LDummyUse* instr) {
5446 // Nothing to see here, move on!
5447}
5448
5449
5450void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
5451 PushSafepointRegistersScope scope(this);
5452 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
5453 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5454 RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0);
5455 DCHECK(instr->HasEnvironment());
5456 LEnvironment* env = instr->environment();
5457 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5458}
5459
5460
5461void LCodeGen::DoStackCheck(LStackCheck* instr) {
5462 class DeferredStackCheck final : public LDeferredCode {
5463 public:
5464 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5465 : LDeferredCode(codegen), instr_(instr) { }
5466 void Generate() override { codegen()->DoDeferredStackCheck(instr_); }
5467 LInstruction* instr() override { return instr_; }
5468
5469 private:
5470 LStackCheck* instr_;
5471 };
5472
5473 DCHECK(instr->HasEnvironment());
5474 LEnvironment* env = instr->environment();
5475 // There is no LLazyBailout instruction for stack-checks. We have to
5476 // prepare for lazy deoptimization explicitly here.
5477 if (instr->hydrogen()->is_function_entry()) {
5478 // Perform stack overflow check.
5479 Label done;
5480 __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
5481 __ j(above_equal, &done, Label::kNear);
5482
5483 DCHECK(instr->context()->IsRegister());
5484 DCHECK(ToRegister(instr->context()).is(rsi));
5485 CallCode(isolate()->builtins()->StackCheck(),
5486 RelocInfo::CODE_TARGET,
5487 instr);
5488 __ bind(&done);
5489 } else {
5490 DCHECK(instr->hydrogen()->is_backwards_branch());
5491 // Perform stack overflow check if this goto needs it before jumping.
5492 DeferredStackCheck* deferred_stack_check =
5493 new(zone()) DeferredStackCheck(this, instr);
5494 __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
5495 __ j(below, deferred_stack_check->entry());
5496 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
5497 __ bind(instr->done_label());
5498 deferred_stack_check->SetExit(instr->done_label());
5499 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5500 // Don't record a deoptimization index for the safepoint here.
5501 // This will be done explicitly when emitting call and the safepoint in
5502 // the deferred code.
5503 }
5504}
5505
5506
5507void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
5508 // This is a pseudo-instruction that ensures that the environment here is
5509 // properly registered for deoptimization and records the assembler's PC
5510 // offset.
5511 LEnvironment* environment = instr->environment();
5512
5513 // If the environment were already registered, we would have no way of
5514 // backpatching it with the spill slot operands.
5515 DCHECK(!environment->HasBeenRegistered());
5516 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
5517
5518 GenerateOsrPrologue();
5519}
5520
5521
5522void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
5523 DCHECK(ToRegister(instr->context()).is(rsi));
5524
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005525 Label use_cache, call_runtime;
Ben Murdoch097c5b22016-05-18 11:27:45 +01005526 __ CheckEnumCache(&call_runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005527
5528 __ movp(rax, FieldOperand(rax, HeapObject::kMapOffset));
5529 __ jmp(&use_cache, Label::kNear);
5530
5531 // Get the set of properties to enumerate.
5532 __ bind(&call_runtime);
5533 __ Push(rax);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005534 CallRuntime(Runtime::kForInEnumerate, instr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005535 __ bind(&use_cache);
5536}
5537
5538
5539void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5540 Register map = ToRegister(instr->map());
5541 Register result = ToRegister(instr->result());
5542 Label load_cache, done;
5543 __ EnumLength(result, map);
5544 __ Cmp(result, Smi::FromInt(0));
5545 __ j(not_equal, &load_cache, Label::kNear);
5546 __ LoadRoot(result, Heap::kEmptyFixedArrayRootIndex);
5547 __ jmp(&done, Label::kNear);
5548 __ bind(&load_cache);
5549 __ LoadInstanceDescriptors(map, result);
5550 __ movp(result,
5551 FieldOperand(result, DescriptorArray::kEnumCacheOffset));
5552 __ movp(result,
5553 FieldOperand(result, FixedArray::SizeFor(instr->idx())));
5554 __ bind(&done);
5555 Condition cc = masm()->CheckSmi(result);
5556 DeoptimizeIf(cc, instr, Deoptimizer::kNoCache);
5557}
5558
5559
5560void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5561 Register object = ToRegister(instr->value());
5562 __ cmpp(ToRegister(instr->map()),
5563 FieldOperand(object, HeapObject::kMapOffset));
5564 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongMap);
5565}
5566
5567
5568void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5569 Register object,
5570 Register index) {
5571 PushSafepointRegistersScope scope(this);
5572 __ Push(object);
5573 __ Push(index);
5574 __ xorp(rsi, rsi);
5575 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
5576 RecordSafepointWithRegisters(
5577 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
5578 __ StoreToSafepointRegisterSlot(object, rax);
5579}
5580
5581
5582void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
5583 class DeferredLoadMutableDouble final : public LDeferredCode {
5584 public:
5585 DeferredLoadMutableDouble(LCodeGen* codegen,
5586 LLoadFieldByIndex* instr,
5587 Register object,
5588 Register index)
5589 : LDeferredCode(codegen),
5590 instr_(instr),
5591 object_(object),
5592 index_(index) {
5593 }
5594 void Generate() override {
5595 codegen()->DoDeferredLoadMutableDouble(instr_, object_, index_);
5596 }
5597 LInstruction* instr() override { return instr_; }
5598
5599 private:
5600 LLoadFieldByIndex* instr_;
5601 Register object_;
5602 Register index_;
5603 };
5604
5605 Register object = ToRegister(instr->object());
5606 Register index = ToRegister(instr->index());
5607
5608 DeferredLoadMutableDouble* deferred;
5609 deferred = new(zone()) DeferredLoadMutableDouble(this, instr, object, index);
5610
5611 Label out_of_object, done;
5612 __ Move(kScratchRegister, Smi::FromInt(1));
5613 __ testp(index, kScratchRegister);
5614 __ j(not_zero, deferred->entry());
5615
5616 __ sarp(index, Immediate(1));
5617
5618 __ SmiToInteger32(index, index);
5619 __ cmpl(index, Immediate(0));
5620 __ j(less, &out_of_object, Label::kNear);
5621 __ movp(object, FieldOperand(object,
5622 index,
5623 times_pointer_size,
5624 JSObject::kHeaderSize));
5625 __ jmp(&done, Label::kNear);
5626
5627 __ bind(&out_of_object);
5628 __ movp(object, FieldOperand(object, JSObject::kPropertiesOffset));
5629 __ negl(index);
5630 // Index is now equal to out of object property index plus 1.
5631 __ movp(object, FieldOperand(object,
5632 index,
5633 times_pointer_size,
5634 FixedArray::kHeaderSize - kPointerSize));
5635 __ bind(deferred->exit());
5636 __ bind(&done);
5637}
5638
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005639#undef __
5640
5641} // namespace internal
5642} // namespace v8
5643
5644#endif // V8_TARGET_ARCH_X64