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

Side by Side Diff: src/x64/lithium-codegen-x64.cc

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

Powered by Google App Engine
This is Rietveld 408576698