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

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: rebased 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
« no previous file with comments | « src/x64/lithium-codegen-x64.h ('k') | src/x64/lithium-gap-resolver-x64.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // 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 __ Cvtss2sd(result, operand);
3003 } else if (elements_kind == FLOAT64_ELEMENTS) {
3004 __ Movsd(ToDoubleRegister(instr->result()), operand);
3005 } else {
3006 Register result(ToRegister(instr->result()));
3007 switch (elements_kind) {
3008 case INT8_ELEMENTS:
3009 __ movsxbl(result, operand);
3010 break;
3011 case UINT8_ELEMENTS:
3012 case UINT8_CLAMPED_ELEMENTS:
3013 __ movzxbl(result, operand);
3014 break;
3015 case INT16_ELEMENTS:
3016 __ movsxwl(result, operand);
3017 break;
3018 case UINT16_ELEMENTS:
3019 __ movzxwl(result, operand);
3020 break;
3021 case INT32_ELEMENTS:
3022 __ movl(result, operand);
3023 break;
3024 case UINT32_ELEMENTS:
3025 __ movl(result, operand);
3026 if (!instr->hydrogen()->CheckFlag(HInstruction::kUint32)) {
3027 __ testl(result, result);
3028 DeoptimizeIf(negative, instr, Deoptimizer::kNegativeValue);
3029 }
3030 break;
3031 case FLOAT32_ELEMENTS:
3032 case FLOAT64_ELEMENTS:
3033 case FAST_ELEMENTS:
3034 case FAST_SMI_ELEMENTS:
3035 case FAST_DOUBLE_ELEMENTS:
3036 case FAST_HOLEY_ELEMENTS:
3037 case FAST_HOLEY_SMI_ELEMENTS:
3038 case FAST_HOLEY_DOUBLE_ELEMENTS:
3039 case DICTIONARY_ELEMENTS:
3040 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
3041 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
3042 UNREACHABLE();
3043 break;
3044 }
3045 }
3046 }
3047
3048
3049 void LCodeGen::DoLoadKeyedFixedDoubleArray(LLoadKeyed* instr) {
3050 XMMRegister result(ToDoubleRegister(instr->result()));
3051 LOperand* key = instr->key();
3052 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
3053 instr->hydrogen()->IsDehoisted()) {
3054 // Sign extend key because it could be a 32 bit negative value
3055 // and the dehoisted address computation happens in 64 bits
3056 __ movsxlq(ToRegister(key), ToRegister(key));
3057 }
3058 if (instr->hydrogen()->RequiresHoleCheck()) {
3059 Operand hole_check_operand = BuildFastArrayOperand(
3060 instr->elements(),
3061 key,
3062 instr->hydrogen()->key()->representation(),
3063 FAST_DOUBLE_ELEMENTS,
3064 instr->base_offset() + sizeof(kHoleNanLower32));
3065 __ cmpl(hole_check_operand, Immediate(kHoleNanUpper32));
3066 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
3067 }
3068
3069 Operand double_load_operand = BuildFastArrayOperand(
3070 instr->elements(),
3071 key,
3072 instr->hydrogen()->key()->representation(),
3073 FAST_DOUBLE_ELEMENTS,
3074 instr->base_offset());
3075 __ Movsd(result, double_load_operand);
3076 }
3077
3078
3079 void LCodeGen::DoLoadKeyedFixedArray(LLoadKeyed* instr) {
3080 HLoadKeyed* hinstr = instr->hydrogen();
3081 Register result = ToRegister(instr->result());
3082 LOperand* key = instr->key();
3083 bool requires_hole_check = hinstr->RequiresHoleCheck();
3084 Representation representation = hinstr->representation();
3085 int offset = instr->base_offset();
3086
3087 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
3088 instr->hydrogen()->IsDehoisted()) {
3089 // Sign extend key because it could be a 32 bit negative value
3090 // and the dehoisted address computation happens in 64 bits
3091 __ movsxlq(ToRegister(key), ToRegister(key));
3092 }
3093 if (representation.IsInteger32() && SmiValuesAre32Bits() &&
3094 hinstr->elements_kind() == FAST_SMI_ELEMENTS) {
3095 DCHECK(!requires_hole_check);
3096 if (FLAG_debug_code) {
3097 Register scratch = kScratchRegister;
3098 __ Load(scratch,
3099 BuildFastArrayOperand(instr->elements(),
3100 key,
3101 instr->hydrogen()->key()->representation(),
3102 FAST_ELEMENTS,
3103 offset),
3104 Representation::Smi());
3105 __ AssertSmi(scratch);
3106 }
3107 // Read int value directly from upper half of the smi.
3108 STATIC_ASSERT(kSmiTag == 0);
3109 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
3110 offset += kPointerSize / 2;
3111 }
3112
3113 __ Load(result,
3114 BuildFastArrayOperand(instr->elements(), key,
3115 instr->hydrogen()->key()->representation(),
3116 FAST_ELEMENTS, offset),
3117 representation);
3118
3119 // Check for the hole value.
3120 if (requires_hole_check) {
3121 if (IsFastSmiElementsKind(hinstr->elements_kind())) {
3122 Condition smi = __ CheckSmi(result);
3123 DeoptimizeIf(NegateCondition(smi), instr, Deoptimizer::kNotASmi);
3124 } else {
3125 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
3126 DeoptimizeIf(equal, instr, Deoptimizer::kHole);
3127 }
3128 } else if (hinstr->hole_mode() == CONVERT_HOLE_TO_UNDEFINED) {
3129 DCHECK(hinstr->elements_kind() == FAST_HOLEY_ELEMENTS);
3130 Label done;
3131 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
3132 __ j(not_equal, &done);
3133 if (info()->IsStub()) {
3134 // A stub can safely convert the hole to undefined only if the array
3135 // protector cell contains (Smi) Isolate::kArrayProtectorValid. Otherwise
3136 // it needs to bail out.
3137 __ LoadRoot(result, Heap::kArrayProtectorRootIndex);
3138 __ Cmp(FieldOperand(result, Cell::kValueOffset),
3139 Smi::FromInt(Isolate::kArrayProtectorValid));
3140 DeoptimizeIf(not_equal, instr, Deoptimizer::kHole);
3141 }
3142 __ Move(result, isolate()->factory()->undefined_value());
3143 __ bind(&done);
3144 }
3145 }
3146
3147
3148 void LCodeGen::DoLoadKeyed(LLoadKeyed* instr) {
3149 if (instr->is_fixed_typed_array()) {
3150 DoLoadKeyedExternalArray(instr);
3151 } else if (instr->hydrogen()->representation().IsDouble()) {
3152 DoLoadKeyedFixedDoubleArray(instr);
3153 } else {
3154 DoLoadKeyedFixedArray(instr);
3155 }
3156 }
3157
3158
3159 Operand LCodeGen::BuildFastArrayOperand(
3160 LOperand* elements_pointer,
3161 LOperand* key,
3162 Representation key_representation,
3163 ElementsKind elements_kind,
3164 uint32_t offset) {
3165 Register elements_pointer_reg = ToRegister(elements_pointer);
3166 int shift_size = ElementsKindToShiftSize(elements_kind);
3167 if (key->IsConstantOperand()) {
3168 int32_t constant_value = ToInteger32(LConstantOperand::cast(key));
3169 if (constant_value & 0xF0000000) {
3170 Abort(kArrayIndexConstantValueTooBig);
3171 }
3172 return Operand(elements_pointer_reg,
3173 (constant_value << shift_size) + offset);
3174 } else {
3175 // Guaranteed by ArrayInstructionInterface::KeyedAccessIndexRequirement().
3176 DCHECK(key_representation.IsInteger32());
3177
3178 ScaleFactor scale_factor = static_cast<ScaleFactor>(shift_size);
3179 return Operand(elements_pointer_reg,
3180 ToRegister(key),
3181 scale_factor,
3182 offset);
3183 }
3184 }
3185
3186
3187 void LCodeGen::DoLoadKeyedGeneric(LLoadKeyedGeneric* instr) {
3188 DCHECK(ToRegister(instr->context()).is(rsi));
3189 DCHECK(ToRegister(instr->object()).is(LoadDescriptor::ReceiverRegister()));
3190 DCHECK(ToRegister(instr->key()).is(LoadDescriptor::NameRegister()));
3191
3192 if (instr->hydrogen()->HasVectorAndSlot()) {
3193 EmitVectorLoadICRegisters<LLoadKeyedGeneric>(instr);
3194 }
3195
3196 Handle<Code> ic = CodeFactory::KeyedLoadICInOptimizedCode(
3197 isolate(), instr->hydrogen()->language_mode(),
3198 instr->hydrogen()->initialization_state()).code();
3199 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3200 }
3201
3202
3203 void LCodeGen::DoArgumentsElements(LArgumentsElements* instr) {
3204 Register result = ToRegister(instr->result());
3205
3206 if (instr->hydrogen()->from_inlined()) {
3207 __ leap(result, Operand(rsp, -kFPOnStackSize + -kPCOnStackSize));
3208 } else {
3209 // Check for arguments adapter frame.
3210 Label done, adapted;
3211 __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
3212 __ Cmp(Operand(result, StandardFrameConstants::kContextOffset),
3213 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
3214 __ j(equal, &adapted, Label::kNear);
3215
3216 // No arguments adaptor frame.
3217 __ movp(result, rbp);
3218 __ jmp(&done, Label::kNear);
3219
3220 // Arguments adaptor frame present.
3221 __ bind(&adapted);
3222 __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
3223
3224 // Result is the frame pointer for the frame if not adapted and for the real
3225 // frame below the adaptor frame if adapted.
3226 __ bind(&done);
3227 }
3228 }
3229
3230
3231 void LCodeGen::DoArgumentsLength(LArgumentsLength* instr) {
3232 Register result = ToRegister(instr->result());
3233
3234 Label done;
3235
3236 // If no arguments adaptor frame the number of arguments is fixed.
3237 if (instr->elements()->IsRegister()) {
3238 __ cmpp(rbp, ToRegister(instr->elements()));
3239 } else {
3240 __ cmpp(rbp, ToOperand(instr->elements()));
3241 }
3242 __ movl(result, Immediate(scope()->num_parameters()));
3243 __ j(equal, &done, Label::kNear);
3244
3245 // Arguments adaptor frame present. Get argument length from there.
3246 __ movp(result, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
3247 __ SmiToInteger32(result,
3248 Operand(result,
3249 ArgumentsAdaptorFrameConstants::kLengthOffset));
3250
3251 // Argument length is in result register.
3252 __ bind(&done);
3253 }
3254
3255
3256 void LCodeGen::DoWrapReceiver(LWrapReceiver* instr) {
3257 Register receiver = ToRegister(instr->receiver());
3258 Register function = ToRegister(instr->function());
3259
3260 // If the receiver is null or undefined, we have to pass the global
3261 // object as a receiver to normal functions. Values have to be
3262 // passed unchanged to builtins and strict-mode functions.
3263 Label global_object, receiver_ok;
3264 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
3265
3266 if (!instr->hydrogen()->known_function()) {
3267 // Do not transform the receiver to object for strict mode
3268 // functions.
3269 __ movp(kScratchRegister,
3270 FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
3271 __ testb(FieldOperand(kScratchRegister,
3272 SharedFunctionInfo::kStrictModeByteOffset),
3273 Immediate(1 << SharedFunctionInfo::kStrictModeBitWithinByte));
3274 __ j(not_equal, &receiver_ok, dist);
3275
3276 // Do not transform the receiver to object for builtins.
3277 __ testb(FieldOperand(kScratchRegister,
3278 SharedFunctionInfo::kNativeByteOffset),
3279 Immediate(1 << SharedFunctionInfo::kNativeBitWithinByte));
3280 __ j(not_equal, &receiver_ok, dist);
3281 }
3282
3283 // Normal function. Replace undefined or null with global receiver.
3284 __ CompareRoot(receiver, Heap::kNullValueRootIndex);
3285 __ j(equal, &global_object, Label::kNear);
3286 __ CompareRoot(receiver, Heap::kUndefinedValueRootIndex);
3287 __ j(equal, &global_object, Label::kNear);
3288
3289 // The receiver should be a JS object.
3290 Condition is_smi = __ CheckSmi(receiver);
3291 DeoptimizeIf(is_smi, instr, Deoptimizer::kSmi);
3292 __ CmpObjectType(receiver, FIRST_SPEC_OBJECT_TYPE, kScratchRegister);
3293 DeoptimizeIf(below, instr, Deoptimizer::kNotAJavaScriptObject);
3294
3295 __ jmp(&receiver_ok, Label::kNear);
3296 __ bind(&global_object);
3297 __ movp(receiver, FieldOperand(function, JSFunction::kContextOffset));
3298 __ movp(receiver,
3299 Operand(receiver,
3300 Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
3301 __ movp(receiver, FieldOperand(receiver, GlobalObject::kGlobalProxyOffset));
3302
3303 __ bind(&receiver_ok);
3304 }
3305
3306
3307 void LCodeGen::DoApplyArguments(LApplyArguments* instr) {
3308 Register receiver = ToRegister(instr->receiver());
3309 Register function = ToRegister(instr->function());
3310 Register length = ToRegister(instr->length());
3311 Register elements = ToRegister(instr->elements());
3312 DCHECK(receiver.is(rax)); // Used for parameter count.
3313 DCHECK(function.is(rdi)); // Required by InvokeFunction.
3314 DCHECK(ToRegister(instr->result()).is(rax));
3315
3316 // Copy the arguments to this function possibly from the
3317 // adaptor frame below it.
3318 const uint32_t kArgumentsLimit = 1 * KB;
3319 __ cmpp(length, Immediate(kArgumentsLimit));
3320 DeoptimizeIf(above, instr, Deoptimizer::kTooManyArguments);
3321
3322 __ Push(receiver);
3323 __ movp(receiver, length);
3324
3325 // Loop through the arguments pushing them onto the execution
3326 // stack.
3327 Label invoke, loop;
3328 // length is a small non-negative integer, due to the test above.
3329 __ testl(length, length);
3330 __ j(zero, &invoke, Label::kNear);
3331 __ bind(&loop);
3332 StackArgumentsAccessor args(elements, length,
3333 ARGUMENTS_DONT_CONTAIN_RECEIVER);
3334 __ Push(args.GetArgumentOperand(0));
3335 __ decl(length);
3336 __ j(not_zero, &loop);
3337
3338 // Invoke the function.
3339 __ bind(&invoke);
3340 DCHECK(instr->HasPointerMap());
3341 LPointerMap* pointers = instr->pointer_map();
3342 SafepointGenerator safepoint_generator(
3343 this, pointers, Safepoint::kLazyDeopt);
3344 ParameterCount actual(rax);
3345 __ InvokeFunction(function, actual, CALL_FUNCTION, safepoint_generator);
3346 }
3347
3348
3349 void LCodeGen::DoPushArgument(LPushArgument* instr) {
3350 LOperand* argument = instr->value();
3351 EmitPushTaggedOperand(argument);
3352 }
3353
3354
3355 void LCodeGen::DoDrop(LDrop* instr) {
3356 __ Drop(instr->count());
3357 }
3358
3359
3360 void LCodeGen::DoThisFunction(LThisFunction* instr) {
3361 Register result = ToRegister(instr->result());
3362 __ movp(result, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
3363 }
3364
3365
3366 void LCodeGen::DoContext(LContext* instr) {
3367 Register result = ToRegister(instr->result());
3368 if (info()->IsOptimizing()) {
3369 __ movp(result, Operand(rbp, StandardFrameConstants::kContextOffset));
3370 } else {
3371 // If there is no frame, the context must be in rsi.
3372 DCHECK(result.is(rsi));
3373 }
3374 }
3375
3376
3377 void LCodeGen::DoDeclareGlobals(LDeclareGlobals* instr) {
3378 DCHECK(ToRegister(instr->context()).is(rsi));
3379 __ Push(instr->hydrogen()->pairs());
3380 __ Push(Smi::FromInt(instr->hydrogen()->flags()));
3381 CallRuntime(Runtime::kDeclareGlobals, 2, instr);
3382 }
3383
3384
3385 void LCodeGen::CallKnownFunction(Handle<JSFunction> function,
3386 int formal_parameter_count, int arity,
3387 LInstruction* instr) {
3388 bool dont_adapt_arguments =
3389 formal_parameter_count == SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3390 bool can_invoke_directly =
3391 dont_adapt_arguments || formal_parameter_count == arity;
3392
3393 Register function_reg = rdi;
3394 LPointerMap* pointers = instr->pointer_map();
3395
3396 if (can_invoke_directly) {
3397 // Change context.
3398 __ movp(rsi, FieldOperand(function_reg, JSFunction::kContextOffset));
3399
3400 // Always initialize rax to the number of actual arguments.
3401 __ Set(rax, arity);
3402
3403 // Invoke function.
3404 if (function.is_identical_to(info()->closure())) {
3405 __ CallSelf();
3406 } else {
3407 __ Call(FieldOperand(function_reg, JSFunction::kCodeEntryOffset));
3408 }
3409
3410 // Set up deoptimization.
3411 RecordSafepointWithLazyDeopt(instr, RECORD_SIMPLE_SAFEPOINT, 0);
3412 } else {
3413 // We need to adapt arguments.
3414 SafepointGenerator generator(
3415 this, pointers, Safepoint::kLazyDeopt);
3416 ParameterCount count(arity);
3417 ParameterCount expected(formal_parameter_count);
3418 __ InvokeFunction(function_reg, expected, count, CALL_FUNCTION, generator);
3419 }
3420 }
3421
3422
3423 void LCodeGen::DoCallWithDescriptor(LCallWithDescriptor* instr) {
3424 DCHECK(ToRegister(instr->result()).is(rax));
3425
3426 if (instr->hydrogen()->IsTailCall()) {
3427 if (NeedsEagerFrame()) __ leave();
3428
3429 if (instr->target()->IsConstantOperand()) {
3430 LConstantOperand* target = LConstantOperand::cast(instr->target());
3431 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3432 __ jmp(code, RelocInfo::CODE_TARGET);
3433 } else {
3434 DCHECK(instr->target()->IsRegister());
3435 Register target = ToRegister(instr->target());
3436 __ addp(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
3437 __ jmp(target);
3438 }
3439 } else {
3440 LPointerMap* pointers = instr->pointer_map();
3441 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3442
3443 if (instr->target()->IsConstantOperand()) {
3444 LConstantOperand* target = LConstantOperand::cast(instr->target());
3445 Handle<Code> code = Handle<Code>::cast(ToHandle(target));
3446 generator.BeforeCall(__ CallSize(code));
3447 __ call(code, RelocInfo::CODE_TARGET);
3448 } else {
3449 DCHECK(instr->target()->IsRegister());
3450 Register target = ToRegister(instr->target());
3451 generator.BeforeCall(__ CallSize(target));
3452 __ addp(target, Immediate(Code::kHeaderSize - kHeapObjectTag));
3453 __ call(target);
3454 }
3455 generator.AfterCall();
3456 }
3457 }
3458
3459
3460 void LCodeGen::DoCallJSFunction(LCallJSFunction* instr) {
3461 DCHECK(ToRegister(instr->function()).is(rdi));
3462 DCHECK(ToRegister(instr->result()).is(rax));
3463
3464 __ Set(rax, instr->arity());
3465
3466 // Change context.
3467 __ movp(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
3468
3469 LPointerMap* pointers = instr->pointer_map();
3470 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3471
3472 bool is_self_call = false;
3473 if (instr->hydrogen()->function()->IsConstant()) {
3474 Handle<JSFunction> jsfun = Handle<JSFunction>::null();
3475 HConstant* fun_const = HConstant::cast(instr->hydrogen()->function());
3476 jsfun = Handle<JSFunction>::cast(fun_const->handle(isolate()));
3477 is_self_call = jsfun.is_identical_to(info()->closure());
3478 }
3479
3480 if (is_self_call) {
3481 __ CallSelf();
3482 } else {
3483 Operand target = FieldOperand(rdi, JSFunction::kCodeEntryOffset);
3484 generator.BeforeCall(__ CallSize(target));
3485 __ Call(target);
3486 }
3487 generator.AfterCall();
3488 }
3489
3490
3491 void LCodeGen::DoDeferredMathAbsTaggedHeapNumber(LMathAbs* instr) {
3492 Register input_reg = ToRegister(instr->value());
3493 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
3494 Heap::kHeapNumberMapRootIndex);
3495 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
3496
3497 Label slow, allocated, done;
3498 Register tmp = input_reg.is(rax) ? rcx : rax;
3499 Register tmp2 = tmp.is(rcx) ? rdx : input_reg.is(rcx) ? rdx : rcx;
3500
3501 // Preserve the value of all registers.
3502 PushSafepointRegistersScope scope(this);
3503
3504 __ movl(tmp, FieldOperand(input_reg, HeapNumber::kExponentOffset));
3505 // Check the sign of the argument. If the argument is positive, just
3506 // return it. We do not need to patch the stack since |input| and
3507 // |result| are the same register and |input| will be restored
3508 // unchanged by popping safepoint registers.
3509 __ testl(tmp, Immediate(HeapNumber::kSignMask));
3510 __ j(zero, &done);
3511
3512 __ AllocateHeapNumber(tmp, tmp2, &slow);
3513 __ jmp(&allocated, Label::kNear);
3514
3515 // Slow case: Call the runtime system to do the number allocation.
3516 __ bind(&slow);
3517 CallRuntimeFromDeferred(
3518 Runtime::kAllocateHeapNumber, 0, instr, instr->context());
3519 // Set the pointer to the new heap number in tmp.
3520 if (!tmp.is(rax)) __ movp(tmp, rax);
3521 // Restore input_reg after call to runtime.
3522 __ LoadFromSafepointRegisterSlot(input_reg, input_reg);
3523
3524 __ bind(&allocated);
3525 __ movq(tmp2, FieldOperand(input_reg, HeapNumber::kValueOffset));
3526 __ shlq(tmp2, Immediate(1));
3527 __ shrq(tmp2, Immediate(1));
3528 __ movq(FieldOperand(tmp, HeapNumber::kValueOffset), tmp2);
3529 __ StoreToSafepointRegisterSlot(input_reg, tmp);
3530
3531 __ bind(&done);
3532 }
3533
3534
3535 void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) {
3536 Register input_reg = ToRegister(instr->value());
3537 __ testl(input_reg, input_reg);
3538 Label is_positive;
3539 __ j(not_sign, &is_positive, Label::kNear);
3540 __ negl(input_reg); // Sets flags.
3541 DeoptimizeIf(negative, instr, Deoptimizer::kOverflow);
3542 __ bind(&is_positive);
3543 }
3544
3545
3546 void LCodeGen::EmitSmiMathAbs(LMathAbs* instr) {
3547 Register input_reg = ToRegister(instr->value());
3548 __ testp(input_reg, input_reg);
3549 Label is_positive;
3550 __ j(not_sign, &is_positive, Label::kNear);
3551 __ negp(input_reg); // Sets flags.
3552 DeoptimizeIf(negative, instr, Deoptimizer::kOverflow);
3553 __ bind(&is_positive);
3554 }
3555
3556
3557 void LCodeGen::DoMathAbs(LMathAbs* instr) {
3558 // Class for deferred case.
3559 class DeferredMathAbsTaggedHeapNumber final : public LDeferredCode {
3560 public:
3561 DeferredMathAbsTaggedHeapNumber(LCodeGen* codegen, LMathAbs* instr)
3562 : LDeferredCode(codegen), instr_(instr) { }
3563 void Generate() override {
3564 codegen()->DoDeferredMathAbsTaggedHeapNumber(instr_);
3565 }
3566 LInstruction* instr() override { return instr_; }
3567
3568 private:
3569 LMathAbs* instr_;
3570 };
3571
3572 DCHECK(instr->value()->Equals(instr->result()));
3573 Representation r = instr->hydrogen()->value()->representation();
3574
3575 if (r.IsDouble()) {
3576 XMMRegister scratch = double_scratch0();
3577 XMMRegister input_reg = ToDoubleRegister(instr->value());
3578 __ Xorpd(scratch, scratch);
3579 __ subsd(scratch, input_reg);
3580 __ andps(input_reg, scratch);
3581 } else if (r.IsInteger32()) {
3582 EmitIntegerMathAbs(instr);
3583 } else if (r.IsSmi()) {
3584 EmitSmiMathAbs(instr);
3585 } else { // Tagged case.
3586 DeferredMathAbsTaggedHeapNumber* deferred =
3587 new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr);
3588 Register input_reg = ToRegister(instr->value());
3589 // Smi check.
3590 __ JumpIfNotSmi(input_reg, deferred->entry());
3591 EmitSmiMathAbs(instr);
3592 __ bind(deferred->exit());
3593 }
3594 }
3595
3596
3597 void LCodeGen::DoMathFloor(LMathFloor* instr) {
3598 XMMRegister xmm_scratch = double_scratch0();
3599 Register output_reg = ToRegister(instr->result());
3600 XMMRegister input_reg = ToDoubleRegister(instr->value());
3601
3602 if (CpuFeatures::IsSupported(SSE4_1)) {
3603 CpuFeatureScope scope(masm(), SSE4_1);
3604 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3605 // Deoptimize if minus zero.
3606 __ Movq(output_reg, input_reg);
3607 __ subq(output_reg, Immediate(1));
3608 DeoptimizeIf(overflow, instr, Deoptimizer::kMinusZero);
3609 }
3610 __ roundsd(xmm_scratch, input_reg, kRoundDown);
3611 __ Cvttsd2si(output_reg, xmm_scratch);
3612 __ cmpl(output_reg, Immediate(0x1));
3613 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3614 } else {
3615 Label negative_sign, done;
3616 // Deoptimize on unordered.
3617 __ Xorpd(xmm_scratch, xmm_scratch); // Zero the register.
3618 __ Ucomisd(input_reg, xmm_scratch);
3619 DeoptimizeIf(parity_even, instr, Deoptimizer::kNaN);
3620 __ j(below, &negative_sign, Label::kNear);
3621
3622 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3623 // Check for negative zero.
3624 Label positive_sign;
3625 __ j(above, &positive_sign, Label::kNear);
3626 __ Movmskpd(output_reg, input_reg);
3627 __ testl(output_reg, Immediate(1));
3628 DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero);
3629 __ Set(output_reg, 0);
3630 __ jmp(&done);
3631 __ bind(&positive_sign);
3632 }
3633
3634 // Use truncating instruction (OK because input is positive).
3635 __ Cvttsd2si(output_reg, input_reg);
3636 // Overflow is signalled with minint.
3637 __ cmpl(output_reg, Immediate(0x1));
3638 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3639 __ jmp(&done, Label::kNear);
3640
3641 // Non-zero negative reaches here.
3642 __ bind(&negative_sign);
3643 // Truncate, then compare and compensate.
3644 __ Cvttsd2si(output_reg, input_reg);
3645 __ Cvtlsi2sd(xmm_scratch, output_reg);
3646 __ Ucomisd(input_reg, xmm_scratch);
3647 __ j(equal, &done, Label::kNear);
3648 __ subl(output_reg, Immediate(1));
3649 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3650
3651 __ bind(&done);
3652 }
3653 }
3654
3655
3656 void LCodeGen::DoMathRound(LMathRound* instr) {
3657 const XMMRegister xmm_scratch = double_scratch0();
3658 Register output_reg = ToRegister(instr->result());
3659 XMMRegister input_reg = ToDoubleRegister(instr->value());
3660 XMMRegister input_temp = ToDoubleRegister(instr->temp());
3661 static int64_t one_half = V8_INT64_C(0x3FE0000000000000); // 0.5
3662 static int64_t minus_one_half = V8_INT64_C(0xBFE0000000000000); // -0.5
3663
3664 Label done, round_to_zero, below_one_half;
3665 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
3666 __ movq(kScratchRegister, one_half);
3667 __ Movq(xmm_scratch, kScratchRegister);
3668 __ Ucomisd(xmm_scratch, input_reg);
3669 __ j(above, &below_one_half, Label::kNear);
3670
3671 // CVTTSD2SI rounds towards zero, since 0.5 <= x, we use floor(0.5 + x).
3672 __ addsd(xmm_scratch, input_reg);
3673 __ Cvttsd2si(output_reg, xmm_scratch);
3674 // Overflow is signalled with minint.
3675 __ cmpl(output_reg, Immediate(0x1));
3676 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3677 __ jmp(&done, dist);
3678
3679 __ bind(&below_one_half);
3680 __ movq(kScratchRegister, minus_one_half);
3681 __ Movq(xmm_scratch, kScratchRegister);
3682 __ Ucomisd(xmm_scratch, input_reg);
3683 __ j(below_equal, &round_to_zero, Label::kNear);
3684
3685 // CVTTSD2SI rounds towards zero, we use ceil(x - (-0.5)) and then
3686 // compare and compensate.
3687 __ Movapd(input_temp, input_reg); // Do not alter input_reg.
3688 __ subsd(input_temp, xmm_scratch);
3689 __ Cvttsd2si(output_reg, input_temp);
3690 // Catch minint due to overflow, and to prevent overflow when compensating.
3691 __ cmpl(output_reg, Immediate(0x1));
3692 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
3693
3694 __ Cvtlsi2sd(xmm_scratch, output_reg);
3695 __ Ucomisd(xmm_scratch, input_temp);
3696 __ j(equal, &done, dist);
3697 __ subl(output_reg, Immediate(1));
3698 // No overflow because we already ruled out minint.
3699 __ jmp(&done, dist);
3700
3701 __ bind(&round_to_zero);
3702 // We return 0 for the input range [+0, 0.5[, or [-0.5, 0.5[ if
3703 // we can ignore the difference between a result of -0 and +0.
3704 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
3705 __ Movq(output_reg, input_reg);
3706 __ testq(output_reg, output_reg);
3707 DeoptimizeIf(negative, instr, Deoptimizer::kMinusZero);
3708 }
3709 __ Set(output_reg, 0);
3710 __ bind(&done);
3711 }
3712
3713
3714 void LCodeGen::DoMathFround(LMathFround* instr) {
3715 XMMRegister input_reg = ToDoubleRegister(instr->value());
3716 XMMRegister output_reg = ToDoubleRegister(instr->result());
3717 __ Cvtsd2ss(output_reg, input_reg);
3718 __ Cvtss2sd(output_reg, output_reg);
3719 }
3720
3721
3722 void LCodeGen::DoMathSqrt(LMathSqrt* instr) {
3723 XMMRegister output = ToDoubleRegister(instr->result());
3724 if (instr->value()->IsDoubleRegister()) {
3725 XMMRegister input = ToDoubleRegister(instr->value());
3726 __ sqrtsd(output, input);
3727 } else {
3728 Operand input = ToOperand(instr->value());
3729 __ sqrtsd(output, input);
3730 }
3731 }
3732
3733
3734 void LCodeGen::DoMathPowHalf(LMathPowHalf* instr) {
3735 XMMRegister xmm_scratch = double_scratch0();
3736 XMMRegister input_reg = ToDoubleRegister(instr->value());
3737 DCHECK(ToDoubleRegister(instr->result()).is(input_reg));
3738
3739 // Note that according to ECMA-262 15.8.2.13:
3740 // Math.pow(-Infinity, 0.5) == Infinity
3741 // Math.sqrt(-Infinity) == NaN
3742 Label done, sqrt;
3743 // Check base for -Infinity. According to IEEE-754, double-precision
3744 // -Infinity has the highest 12 bits set and the lowest 52 bits cleared.
3745 __ movq(kScratchRegister, V8_INT64_C(0xFFF0000000000000));
3746 __ Movq(xmm_scratch, kScratchRegister);
3747 __ Ucomisd(xmm_scratch, input_reg);
3748 // Comparing -Infinity with NaN results in "unordered", which sets the
3749 // zero flag as if both were equal. However, it also sets the carry flag.
3750 __ j(not_equal, &sqrt, Label::kNear);
3751 __ j(carry, &sqrt, Label::kNear);
3752 // If input is -Infinity, return Infinity.
3753 __ Xorpd(input_reg, input_reg);
3754 __ subsd(input_reg, xmm_scratch);
3755 __ jmp(&done, Label::kNear);
3756
3757 // Square root.
3758 __ bind(&sqrt);
3759 __ Xorpd(xmm_scratch, xmm_scratch);
3760 __ addsd(input_reg, xmm_scratch); // Convert -0 to +0.
3761 __ sqrtsd(input_reg, input_reg);
3762 __ bind(&done);
3763 }
3764
3765
3766 void LCodeGen::DoPower(LPower* instr) {
3767 Representation exponent_type = instr->hydrogen()->right()->representation();
3768 // Having marked this as a call, we can use any registers.
3769 // Just make sure that the input/output registers are the expected ones.
3770
3771 Register tagged_exponent = MathPowTaggedDescriptor::exponent();
3772 DCHECK(!instr->right()->IsRegister() ||
3773 ToRegister(instr->right()).is(tagged_exponent));
3774 DCHECK(!instr->right()->IsDoubleRegister() ||
3775 ToDoubleRegister(instr->right()).is(xmm1));
3776 DCHECK(ToDoubleRegister(instr->left()).is(xmm2));
3777 DCHECK(ToDoubleRegister(instr->result()).is(xmm3));
3778
3779 if (exponent_type.IsSmi()) {
3780 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3781 __ CallStub(&stub);
3782 } else if (exponent_type.IsTagged()) {
3783 Label no_deopt;
3784 __ JumpIfSmi(tagged_exponent, &no_deopt, Label::kNear);
3785 __ CmpObjectType(tagged_exponent, HEAP_NUMBER_TYPE, rcx);
3786 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
3787 __ bind(&no_deopt);
3788 MathPowStub stub(isolate(), MathPowStub::TAGGED);
3789 __ CallStub(&stub);
3790 } else if (exponent_type.IsInteger32()) {
3791 MathPowStub stub(isolate(), MathPowStub::INTEGER);
3792 __ CallStub(&stub);
3793 } else {
3794 DCHECK(exponent_type.IsDouble());
3795 MathPowStub stub(isolate(), MathPowStub::DOUBLE);
3796 __ CallStub(&stub);
3797 }
3798 }
3799
3800
3801 void LCodeGen::DoMathExp(LMathExp* instr) {
3802 XMMRegister input = ToDoubleRegister(instr->value());
3803 XMMRegister result = ToDoubleRegister(instr->result());
3804 XMMRegister temp0 = double_scratch0();
3805 Register temp1 = ToRegister(instr->temp1());
3806 Register temp2 = ToRegister(instr->temp2());
3807
3808 MathExpGenerator::EmitMathExp(masm(), input, result, temp0, temp1, temp2);
3809 }
3810
3811
3812 void LCodeGen::DoMathLog(LMathLog* instr) {
3813 DCHECK(instr->value()->Equals(instr->result()));
3814 XMMRegister input_reg = ToDoubleRegister(instr->value());
3815 XMMRegister xmm_scratch = double_scratch0();
3816 Label positive, done, zero;
3817 __ Xorpd(xmm_scratch, xmm_scratch);
3818 __ Ucomisd(input_reg, xmm_scratch);
3819 __ j(above, &positive, Label::kNear);
3820 __ j(not_carry, &zero, Label::kNear);
3821 __ pcmpeqd(input_reg, input_reg);
3822 __ jmp(&done, Label::kNear);
3823 __ bind(&zero);
3824 ExternalReference ninf =
3825 ExternalReference::address_of_negative_infinity();
3826 Operand ninf_operand = masm()->ExternalOperand(ninf);
3827 __ Movsd(input_reg, ninf_operand);
3828 __ jmp(&done, Label::kNear);
3829 __ bind(&positive);
3830 __ fldln2();
3831 __ subp(rsp, Immediate(kDoubleSize));
3832 __ Movsd(Operand(rsp, 0), input_reg);
3833 __ fld_d(Operand(rsp, 0));
3834 __ fyl2x();
3835 __ fstp_d(Operand(rsp, 0));
3836 __ Movsd(input_reg, Operand(rsp, 0));
3837 __ addp(rsp, Immediate(kDoubleSize));
3838 __ bind(&done);
3839 }
3840
3841
3842 void LCodeGen::DoMathClz32(LMathClz32* instr) {
3843 Register input = ToRegister(instr->value());
3844 Register result = ToRegister(instr->result());
3845
3846 __ Lzcntl(result, input);
3847 }
3848
3849
3850 void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
3851 DCHECK(ToRegister(instr->context()).is(rsi));
3852 DCHECK(ToRegister(instr->function()).is(rdi));
3853 DCHECK(instr->HasPointerMap());
3854
3855 Handle<JSFunction> known_function = instr->hydrogen()->known_function();
3856 if (known_function.is_null()) {
3857 LPointerMap* pointers = instr->pointer_map();
3858 SafepointGenerator generator(this, pointers, Safepoint::kLazyDeopt);
3859 ParameterCount count(instr->arity());
3860 __ InvokeFunction(rdi, count, CALL_FUNCTION, generator);
3861 } else {
3862 CallKnownFunction(known_function,
3863 instr->hydrogen()->formal_parameter_count(),
3864 instr->arity(), instr);
3865 }
3866 }
3867
3868
3869 void LCodeGen::DoCallFunction(LCallFunction* instr) {
3870 DCHECK(ToRegister(instr->context()).is(rsi));
3871 DCHECK(ToRegister(instr->function()).is(rdi));
3872 DCHECK(ToRegister(instr->result()).is(rax));
3873
3874 int arity = instr->arity();
3875 CallFunctionFlags flags = instr->hydrogen()->function_flags();
3876 if (instr->hydrogen()->HasVectorAndSlot()) {
3877 Register slot_register = ToRegister(instr->temp_slot());
3878 Register vector_register = ToRegister(instr->temp_vector());
3879 DCHECK(slot_register.is(rdx));
3880 DCHECK(vector_register.is(rbx));
3881
3882 AllowDeferredHandleDereference vector_structure_check;
3883 Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
3884 int index = vector->GetIndex(instr->hydrogen()->slot());
3885
3886 __ Move(vector_register, vector);
3887 __ Move(slot_register, Smi::FromInt(index));
3888
3889 CallICState::CallType call_type =
3890 (flags & CALL_AS_METHOD) ? CallICState::METHOD : CallICState::FUNCTION;
3891
3892 Handle<Code> ic =
3893 CodeFactory::CallICInOptimizedCode(isolate(), arity, call_type).code();
3894 CallCode(ic, RelocInfo::CODE_TARGET, instr);
3895 } else {
3896 CallFunctionStub stub(isolate(), arity, flags);
3897 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
3898 }
3899 }
3900
3901
3902 void LCodeGen::DoCallNew(LCallNew* instr) {
3903 DCHECK(ToRegister(instr->context()).is(rsi));
3904 DCHECK(ToRegister(instr->constructor()).is(rdi));
3905 DCHECK(ToRegister(instr->result()).is(rax));
3906
3907 __ Set(rax, instr->arity());
3908 // No cell in ebx for construct type feedback in optimized code
3909 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
3910 CallConstructStub stub(isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
3911 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
3912 }
3913
3914
3915 void LCodeGen::DoCallNewArray(LCallNewArray* instr) {
3916 DCHECK(ToRegister(instr->context()).is(rsi));
3917 DCHECK(ToRegister(instr->constructor()).is(rdi));
3918 DCHECK(ToRegister(instr->result()).is(rax));
3919
3920 __ Set(rax, instr->arity());
3921 if (instr->arity() == 1) {
3922 // We only need the allocation site for the case we have a length argument.
3923 // The case may bail out to the runtime, which will determine the correct
3924 // elements kind with the site.
3925 __ Move(rbx, instr->hydrogen()->site());
3926 } else {
3927 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
3928 }
3929
3930 ElementsKind kind = instr->hydrogen()->elements_kind();
3931 AllocationSiteOverrideMode override_mode =
3932 (AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE)
3933 ? DISABLE_ALLOCATION_SITES
3934 : DONT_OVERRIDE;
3935
3936 if (instr->arity() == 0) {
3937 ArrayNoArgumentConstructorStub stub(isolate(), kind, override_mode);
3938 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
3939 } else if (instr->arity() == 1) {
3940 Label done;
3941 if (IsFastPackedElementsKind(kind)) {
3942 Label packed_case;
3943 // We might need a change here
3944 // look at the first argument
3945 __ movp(rcx, Operand(rsp, 0));
3946 __ testp(rcx, rcx);
3947 __ j(zero, &packed_case, Label::kNear);
3948
3949 ElementsKind holey_kind = GetHoleyElementsKind(kind);
3950 ArraySingleArgumentConstructorStub stub(isolate(),
3951 holey_kind,
3952 override_mode);
3953 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
3954 __ jmp(&done, Label::kNear);
3955 __ bind(&packed_case);
3956 }
3957
3958 ArraySingleArgumentConstructorStub stub(isolate(), kind, override_mode);
3959 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
3960 __ bind(&done);
3961 } else {
3962 ArrayNArgumentsConstructorStub stub(isolate(), kind, override_mode);
3963 CallCode(stub.GetCode(), RelocInfo::CONSTRUCT_CALL, instr);
3964 }
3965 }
3966
3967
3968 void LCodeGen::DoCallRuntime(LCallRuntime* instr) {
3969 DCHECK(ToRegister(instr->context()).is(rsi));
3970 CallRuntime(instr->function(), instr->arity(), instr, instr->save_doubles());
3971 }
3972
3973
3974 void LCodeGen::DoStoreCodeEntry(LStoreCodeEntry* instr) {
3975 Register function = ToRegister(instr->function());
3976 Register code_object = ToRegister(instr->code_object());
3977 __ leap(code_object, FieldOperand(code_object, Code::kHeaderSize));
3978 __ movp(FieldOperand(function, JSFunction::kCodeEntryOffset), code_object);
3979 }
3980
3981
3982 void LCodeGen::DoInnerAllocatedObject(LInnerAllocatedObject* instr) {
3983 Register result = ToRegister(instr->result());
3984 Register base = ToRegister(instr->base_object());
3985 if (instr->offset()->IsConstantOperand()) {
3986 LConstantOperand* offset = LConstantOperand::cast(instr->offset());
3987 __ leap(result, Operand(base, ToInteger32(offset)));
3988 } else {
3989 Register offset = ToRegister(instr->offset());
3990 __ leap(result, Operand(base, offset, times_1, 0));
3991 }
3992 }
3993
3994
3995 void LCodeGen::DoStoreNamedField(LStoreNamedField* instr) {
3996 HStoreNamedField* hinstr = instr->hydrogen();
3997 Representation representation = instr->representation();
3998
3999 HObjectAccess access = hinstr->access();
4000 int offset = access.offset();
4001
4002 if (access.IsExternalMemory()) {
4003 DCHECK(!hinstr->NeedsWriteBarrier());
4004 Register value = ToRegister(instr->value());
4005 if (instr->object()->IsConstantOperand()) {
4006 DCHECK(value.is(rax));
4007 LConstantOperand* object = LConstantOperand::cast(instr->object());
4008 __ store_rax(ToExternalReference(object));
4009 } else {
4010 Register object = ToRegister(instr->object());
4011 __ Store(MemOperand(object, offset), value, representation);
4012 }
4013 return;
4014 }
4015
4016 Register object = ToRegister(instr->object());
4017 __ AssertNotSmi(object);
4018
4019 DCHECK(!representation.IsSmi() ||
4020 !instr->value()->IsConstantOperand() ||
4021 IsInteger32Constant(LConstantOperand::cast(instr->value())));
4022 if (!FLAG_unbox_double_fields && representation.IsDouble()) {
4023 DCHECK(access.IsInobject());
4024 DCHECK(!hinstr->has_transition());
4025 DCHECK(!hinstr->NeedsWriteBarrier());
4026 XMMRegister value = ToDoubleRegister(instr->value());
4027 __ Movsd(FieldOperand(object, offset), value);
4028 return;
4029 }
4030
4031 if (hinstr->has_transition()) {
4032 Handle<Map> transition = hinstr->transition_map();
4033 AddDeprecationDependency(transition);
4034 if (!hinstr->NeedsWriteBarrierForMap()) {
4035 __ Move(FieldOperand(object, HeapObject::kMapOffset), transition);
4036 } else {
4037 Register temp = ToRegister(instr->temp());
4038 __ Move(kScratchRegister, transition);
4039 __ movp(FieldOperand(object, HeapObject::kMapOffset), kScratchRegister);
4040 // Update the write barrier for the map field.
4041 __ RecordWriteForMap(object,
4042 kScratchRegister,
4043 temp,
4044 kSaveFPRegs);
4045 }
4046 }
4047
4048 // Do the store.
4049 Register write_register = object;
4050 if (!access.IsInobject()) {
4051 write_register = ToRegister(instr->temp());
4052 __ movp(write_register, FieldOperand(object, JSObject::kPropertiesOffset));
4053 }
4054
4055 if (representation.IsSmi() && SmiValuesAre32Bits() &&
4056 hinstr->value()->representation().IsInteger32()) {
4057 DCHECK(hinstr->store_mode() == STORE_TO_INITIALIZED_ENTRY);
4058 if (FLAG_debug_code) {
4059 Register scratch = kScratchRegister;
4060 __ Load(scratch, FieldOperand(write_register, offset), representation);
4061 __ AssertSmi(scratch);
4062 }
4063 // Store int value directly to upper half of the smi.
4064 STATIC_ASSERT(kSmiTag == 0);
4065 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
4066 offset += kPointerSize / 2;
4067 representation = Representation::Integer32();
4068 }
4069
4070 Operand operand = FieldOperand(write_register, offset);
4071
4072 if (FLAG_unbox_double_fields && representation.IsDouble()) {
4073 DCHECK(access.IsInobject());
4074 XMMRegister value = ToDoubleRegister(instr->value());
4075 __ Movsd(operand, value);
4076
4077 } else if (instr->value()->IsRegister()) {
4078 Register value = ToRegister(instr->value());
4079 __ Store(operand, value, representation);
4080 } else {
4081 LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
4082 if (IsInteger32Constant(operand_value)) {
4083 DCHECK(!hinstr->NeedsWriteBarrier());
4084 int32_t value = ToInteger32(operand_value);
4085 if (representation.IsSmi()) {
4086 __ Move(operand, Smi::FromInt(value));
4087
4088 } else {
4089 __ movl(operand, Immediate(value));
4090 }
4091
4092 } else if (IsExternalConstant(operand_value)) {
4093 DCHECK(!hinstr->NeedsWriteBarrier());
4094 ExternalReference ptr = ToExternalReference(operand_value);
4095 __ Move(kScratchRegister, ptr);
4096 __ movp(operand, kScratchRegister);
4097 } else {
4098 Handle<Object> handle_value = ToHandle(operand_value);
4099 DCHECK(!hinstr->NeedsWriteBarrier());
4100 __ Move(operand, handle_value);
4101 }
4102 }
4103
4104 if (hinstr->NeedsWriteBarrier()) {
4105 Register value = ToRegister(instr->value());
4106 Register temp = access.IsInobject() ? ToRegister(instr->temp()) : object;
4107 // Update the write barrier for the object for in-object properties.
4108 __ RecordWriteField(write_register,
4109 offset,
4110 value,
4111 temp,
4112 kSaveFPRegs,
4113 EMIT_REMEMBERED_SET,
4114 hinstr->SmiCheckForWriteBarrier(),
4115 hinstr->PointersToHereCheckForValue());
4116 }
4117 }
4118
4119
4120 void LCodeGen::DoStoreNamedGeneric(LStoreNamedGeneric* instr) {
4121 DCHECK(ToRegister(instr->context()).is(rsi));
4122 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4123 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4124
4125 if (instr->hydrogen()->HasVectorAndSlot()) {
4126 EmitVectorStoreICRegisters<LStoreNamedGeneric>(instr);
4127 }
4128
4129 __ Move(StoreDescriptor::NameRegister(), instr->hydrogen()->name());
4130 Handle<Code> ic = CodeFactory::StoreICInOptimizedCode(
4131 isolate(), instr->language_mode(),
4132 instr->hydrogen()->initialization_state()).code();
4133 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4134 }
4135
4136
4137 void LCodeGen::DoStoreGlobalViaContext(LStoreGlobalViaContext* instr) {
4138 DCHECK(ToRegister(instr->context()).is(rsi));
4139 DCHECK(ToRegister(instr->value())
4140 .is(StoreGlobalViaContextDescriptor::ValueRegister()));
4141 int const slot = instr->slot_index();
4142 int const depth = instr->depth();
4143 if (depth <= StoreGlobalViaContextStub::kMaximumDepth) {
4144 __ Set(StoreGlobalViaContextDescriptor::SlotRegister(), slot);
4145 Handle<Code> stub = CodeFactory::StoreGlobalViaContext(
4146 isolate(), depth, instr->language_mode())
4147 .code();
4148 CallCode(stub, RelocInfo::CODE_TARGET, instr);
4149 } else {
4150 __ Push(Smi::FromInt(slot));
4151 __ Push(StoreGlobalViaContextDescriptor::ValueRegister());
4152 __ CallRuntime(is_strict(instr->language_mode())
4153 ? Runtime::kStoreGlobalViaContext_Strict
4154 : Runtime::kStoreGlobalViaContext_Sloppy,
4155 2);
4156 }
4157 }
4158
4159
4160 void LCodeGen::DoBoundsCheck(LBoundsCheck* instr) {
4161 Representation representation = instr->hydrogen()->length()->representation();
4162 DCHECK(representation.Equals(instr->hydrogen()->index()->representation()));
4163 DCHECK(representation.IsSmiOrInteger32());
4164
4165 Condition cc = instr->hydrogen()->allow_equality() ? below : below_equal;
4166 if (instr->length()->IsConstantOperand()) {
4167 int32_t length = ToInteger32(LConstantOperand::cast(instr->length()));
4168 Register index = ToRegister(instr->index());
4169 if (representation.IsSmi()) {
4170 __ Cmp(index, Smi::FromInt(length));
4171 } else {
4172 __ cmpl(index, Immediate(length));
4173 }
4174 cc = CommuteCondition(cc);
4175 } else if (instr->index()->IsConstantOperand()) {
4176 int32_t index = ToInteger32(LConstantOperand::cast(instr->index()));
4177 if (instr->length()->IsRegister()) {
4178 Register length = ToRegister(instr->length());
4179 if (representation.IsSmi()) {
4180 __ Cmp(length, Smi::FromInt(index));
4181 } else {
4182 __ cmpl(length, Immediate(index));
4183 }
4184 } else {
4185 Operand length = ToOperand(instr->length());
4186 if (representation.IsSmi()) {
4187 __ Cmp(length, Smi::FromInt(index));
4188 } else {
4189 __ cmpl(length, Immediate(index));
4190 }
4191 }
4192 } else {
4193 Register index = ToRegister(instr->index());
4194 if (instr->length()->IsRegister()) {
4195 Register length = ToRegister(instr->length());
4196 if (representation.IsSmi()) {
4197 __ cmpp(length, index);
4198 } else {
4199 __ cmpl(length, index);
4200 }
4201 } else {
4202 Operand length = ToOperand(instr->length());
4203 if (representation.IsSmi()) {
4204 __ cmpp(length, index);
4205 } else {
4206 __ cmpl(length, index);
4207 }
4208 }
4209 }
4210 if (FLAG_debug_code && instr->hydrogen()->skip_check()) {
4211 Label done;
4212 __ j(NegateCondition(cc), &done, Label::kNear);
4213 __ int3();
4214 __ bind(&done);
4215 } else {
4216 DeoptimizeIf(cc, instr, Deoptimizer::kOutOfBounds);
4217 }
4218 }
4219
4220
4221 void LCodeGen::DoStoreKeyedExternalArray(LStoreKeyed* instr) {
4222 ElementsKind elements_kind = instr->elements_kind();
4223 LOperand* key = instr->key();
4224 if (kPointerSize == kInt32Size && !key->IsConstantOperand()) {
4225 Register key_reg = ToRegister(key);
4226 Representation key_representation =
4227 instr->hydrogen()->key()->representation();
4228 if (ExternalArrayOpRequiresTemp(key_representation, elements_kind)) {
4229 __ SmiToInteger64(key_reg, key_reg);
4230 } else if (instr->hydrogen()->IsDehoisted()) {
4231 // Sign extend key because it could be a 32 bit negative value
4232 // and the dehoisted address computation happens in 64 bits
4233 __ movsxlq(key_reg, key_reg);
4234 }
4235 }
4236 Operand operand(BuildFastArrayOperand(
4237 instr->elements(),
4238 key,
4239 instr->hydrogen()->key()->representation(),
4240 elements_kind,
4241 instr->base_offset()));
4242
4243 if (elements_kind == FLOAT32_ELEMENTS) {
4244 XMMRegister value(ToDoubleRegister(instr->value()));
4245 __ Cvtsd2ss(value, value);
4246 __ Movss(operand, value);
4247 } else if (elements_kind == FLOAT64_ELEMENTS) {
4248 __ Movsd(operand, ToDoubleRegister(instr->value()));
4249 } else {
4250 Register value(ToRegister(instr->value()));
4251 switch (elements_kind) {
4252 case INT8_ELEMENTS:
4253 case UINT8_ELEMENTS:
4254 case UINT8_CLAMPED_ELEMENTS:
4255 __ movb(operand, value);
4256 break;
4257 case INT16_ELEMENTS:
4258 case UINT16_ELEMENTS:
4259 __ movw(operand, value);
4260 break;
4261 case INT32_ELEMENTS:
4262 case UINT32_ELEMENTS:
4263 __ movl(operand, value);
4264 break;
4265 case FLOAT32_ELEMENTS:
4266 case FLOAT64_ELEMENTS:
4267 case FAST_ELEMENTS:
4268 case FAST_SMI_ELEMENTS:
4269 case FAST_DOUBLE_ELEMENTS:
4270 case FAST_HOLEY_ELEMENTS:
4271 case FAST_HOLEY_SMI_ELEMENTS:
4272 case FAST_HOLEY_DOUBLE_ELEMENTS:
4273 case DICTIONARY_ELEMENTS:
4274 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
4275 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
4276 UNREACHABLE();
4277 break;
4278 }
4279 }
4280 }
4281
4282
4283 void LCodeGen::DoStoreKeyedFixedDoubleArray(LStoreKeyed* instr) {
4284 XMMRegister value = ToDoubleRegister(instr->value());
4285 LOperand* key = instr->key();
4286 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
4287 instr->hydrogen()->IsDehoisted()) {
4288 // Sign extend key because it could be a 32 bit negative value
4289 // and the dehoisted address computation happens in 64 bits
4290 __ movsxlq(ToRegister(key), ToRegister(key));
4291 }
4292 if (instr->NeedsCanonicalization()) {
4293 XMMRegister xmm_scratch = double_scratch0();
4294 // Turn potential sNaN value into qNaN.
4295 __ Xorpd(xmm_scratch, xmm_scratch);
4296 __ subsd(value, xmm_scratch);
4297 }
4298
4299 Operand double_store_operand = BuildFastArrayOperand(
4300 instr->elements(),
4301 key,
4302 instr->hydrogen()->key()->representation(),
4303 FAST_DOUBLE_ELEMENTS,
4304 instr->base_offset());
4305
4306 __ Movsd(double_store_operand, value);
4307 }
4308
4309
4310 void LCodeGen::DoStoreKeyedFixedArray(LStoreKeyed* instr) {
4311 HStoreKeyed* hinstr = instr->hydrogen();
4312 LOperand* key = instr->key();
4313 int offset = instr->base_offset();
4314 Representation representation = hinstr->value()->representation();
4315
4316 if (kPointerSize == kInt32Size && !key->IsConstantOperand() &&
4317 instr->hydrogen()->IsDehoisted()) {
4318 // Sign extend key because it could be a 32 bit negative value
4319 // and the dehoisted address computation happens in 64 bits
4320 __ movsxlq(ToRegister(key), ToRegister(key));
4321 }
4322 if (representation.IsInteger32() && SmiValuesAre32Bits()) {
4323 DCHECK(hinstr->store_mode() == STORE_TO_INITIALIZED_ENTRY);
4324 DCHECK(hinstr->elements_kind() == FAST_SMI_ELEMENTS);
4325 if (FLAG_debug_code) {
4326 Register scratch = kScratchRegister;
4327 __ Load(scratch,
4328 BuildFastArrayOperand(instr->elements(),
4329 key,
4330 instr->hydrogen()->key()->representation(),
4331 FAST_ELEMENTS,
4332 offset),
4333 Representation::Smi());
4334 __ AssertSmi(scratch);
4335 }
4336 // Store int value directly to upper half of the smi.
4337 STATIC_ASSERT(kSmiTag == 0);
4338 DCHECK(kSmiTagSize + kSmiShiftSize == 32);
4339 offset += kPointerSize / 2;
4340 }
4341
4342 Operand operand =
4343 BuildFastArrayOperand(instr->elements(),
4344 key,
4345 instr->hydrogen()->key()->representation(),
4346 FAST_ELEMENTS,
4347 offset);
4348 if (instr->value()->IsRegister()) {
4349 __ Store(operand, ToRegister(instr->value()), representation);
4350 } else {
4351 LConstantOperand* operand_value = LConstantOperand::cast(instr->value());
4352 if (IsInteger32Constant(operand_value)) {
4353 int32_t value = ToInteger32(operand_value);
4354 if (representation.IsSmi()) {
4355 __ Move(operand, Smi::FromInt(value));
4356
4357 } else {
4358 __ movl(operand, Immediate(value));
4359 }
4360 } else {
4361 Handle<Object> handle_value = ToHandle(operand_value);
4362 __ Move(operand, handle_value);
4363 }
4364 }
4365
4366 if (hinstr->NeedsWriteBarrier()) {
4367 Register elements = ToRegister(instr->elements());
4368 DCHECK(instr->value()->IsRegister());
4369 Register value = ToRegister(instr->value());
4370 DCHECK(!key->IsConstantOperand());
4371 SmiCheck check_needed = hinstr->value()->type().IsHeapObject()
4372 ? OMIT_SMI_CHECK : INLINE_SMI_CHECK;
4373 // Compute address of modified element and store it into key register.
4374 Register key_reg(ToRegister(key));
4375 __ leap(key_reg, operand);
4376 __ RecordWrite(elements,
4377 key_reg,
4378 value,
4379 kSaveFPRegs,
4380 EMIT_REMEMBERED_SET,
4381 check_needed,
4382 hinstr->PointersToHereCheckForValue());
4383 }
4384 }
4385
4386
4387 void LCodeGen::DoStoreKeyed(LStoreKeyed* instr) {
4388 if (instr->is_fixed_typed_array()) {
4389 DoStoreKeyedExternalArray(instr);
4390 } else if (instr->hydrogen()->value()->representation().IsDouble()) {
4391 DoStoreKeyedFixedDoubleArray(instr);
4392 } else {
4393 DoStoreKeyedFixedArray(instr);
4394 }
4395 }
4396
4397
4398 void LCodeGen::DoStoreKeyedGeneric(LStoreKeyedGeneric* instr) {
4399 DCHECK(ToRegister(instr->context()).is(rsi));
4400 DCHECK(ToRegister(instr->object()).is(StoreDescriptor::ReceiverRegister()));
4401 DCHECK(ToRegister(instr->key()).is(StoreDescriptor::NameRegister()));
4402 DCHECK(ToRegister(instr->value()).is(StoreDescriptor::ValueRegister()));
4403
4404 if (instr->hydrogen()->HasVectorAndSlot()) {
4405 EmitVectorStoreICRegisters<LStoreKeyedGeneric>(instr);
4406 }
4407
4408 Handle<Code> ic = CodeFactory::KeyedStoreICInOptimizedCode(
4409 isolate(), instr->language_mode(),
4410 instr->hydrogen()->initialization_state()).code();
4411 CallCode(ic, RelocInfo::CODE_TARGET, instr);
4412 }
4413
4414
4415 void LCodeGen::DoMaybeGrowElements(LMaybeGrowElements* instr) {
4416 class DeferredMaybeGrowElements final : public LDeferredCode {
4417 public:
4418 DeferredMaybeGrowElements(LCodeGen* codegen, LMaybeGrowElements* instr)
4419 : LDeferredCode(codegen), instr_(instr) {}
4420 void Generate() override { codegen()->DoDeferredMaybeGrowElements(instr_); }
4421 LInstruction* instr() override { return instr_; }
4422
4423 private:
4424 LMaybeGrowElements* instr_;
4425 };
4426
4427 Register result = rax;
4428 DeferredMaybeGrowElements* deferred =
4429 new (zone()) DeferredMaybeGrowElements(this, instr);
4430 LOperand* key = instr->key();
4431 LOperand* current_capacity = instr->current_capacity();
4432
4433 DCHECK(instr->hydrogen()->key()->representation().IsInteger32());
4434 DCHECK(instr->hydrogen()->current_capacity()->representation().IsInteger32());
4435 DCHECK(key->IsConstantOperand() || key->IsRegister());
4436 DCHECK(current_capacity->IsConstantOperand() ||
4437 current_capacity->IsRegister());
4438
4439 if (key->IsConstantOperand() && current_capacity->IsConstantOperand()) {
4440 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4441 int32_t constant_capacity =
4442 ToInteger32(LConstantOperand::cast(current_capacity));
4443 if (constant_key >= constant_capacity) {
4444 // Deferred case.
4445 __ jmp(deferred->entry());
4446 }
4447 } else if (key->IsConstantOperand()) {
4448 int32_t constant_key = ToInteger32(LConstantOperand::cast(key));
4449 __ cmpl(ToRegister(current_capacity), Immediate(constant_key));
4450 __ j(less_equal, deferred->entry());
4451 } else if (current_capacity->IsConstantOperand()) {
4452 int32_t constant_capacity =
4453 ToInteger32(LConstantOperand::cast(current_capacity));
4454 __ cmpl(ToRegister(key), Immediate(constant_capacity));
4455 __ j(greater_equal, deferred->entry());
4456 } else {
4457 __ cmpl(ToRegister(key), ToRegister(current_capacity));
4458 __ j(greater_equal, deferred->entry());
4459 }
4460
4461 if (instr->elements()->IsRegister()) {
4462 __ movp(result, ToRegister(instr->elements()));
4463 } else {
4464 __ movp(result, ToOperand(instr->elements()));
4465 }
4466
4467 __ bind(deferred->exit());
4468 }
4469
4470
4471 void LCodeGen::DoDeferredMaybeGrowElements(LMaybeGrowElements* instr) {
4472 // TODO(3095996): Get rid of this. For now, we need to make the
4473 // result register contain a valid pointer because it is already
4474 // contained in the register pointer map.
4475 Register result = rax;
4476 __ Move(result, Smi::FromInt(0));
4477
4478 // We have to call a stub.
4479 {
4480 PushSafepointRegistersScope scope(this);
4481 if (instr->object()->IsConstantOperand()) {
4482 LConstantOperand* constant_object =
4483 LConstantOperand::cast(instr->object());
4484 if (IsSmiConstant(constant_object)) {
4485 Smi* immediate = ToSmi(constant_object);
4486 __ Move(result, immediate);
4487 } else {
4488 Handle<Object> handle_value = ToHandle(constant_object);
4489 __ Move(result, handle_value);
4490 }
4491 } else if (instr->object()->IsRegister()) {
4492 __ Move(result, ToRegister(instr->object()));
4493 } else {
4494 __ movp(result, ToOperand(instr->object()));
4495 }
4496
4497 LOperand* key = instr->key();
4498 if (key->IsConstantOperand()) {
4499 __ Move(rbx, ToSmi(LConstantOperand::cast(key)));
4500 } else {
4501 __ Move(rbx, ToRegister(key));
4502 __ Integer32ToSmi(rbx, rbx);
4503 }
4504
4505 GrowArrayElementsStub stub(isolate(), instr->hydrogen()->is_js_array(),
4506 instr->hydrogen()->kind());
4507 __ CallStub(&stub);
4508 RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0);
4509 __ StoreToSafepointRegisterSlot(result, result);
4510 }
4511
4512 // Deopt on smi, which means the elements array changed to dictionary mode.
4513 Condition is_smi = __ CheckSmi(result);
4514 DeoptimizeIf(is_smi, instr, Deoptimizer::kSmi);
4515 }
4516
4517
4518 void LCodeGen::DoTransitionElementsKind(LTransitionElementsKind* instr) {
4519 Register object_reg = ToRegister(instr->object());
4520
4521 Handle<Map> from_map = instr->original_map();
4522 Handle<Map> to_map = instr->transitioned_map();
4523 ElementsKind from_kind = instr->from_kind();
4524 ElementsKind to_kind = instr->to_kind();
4525
4526 Label not_applicable;
4527 __ Cmp(FieldOperand(object_reg, HeapObject::kMapOffset), from_map);
4528 __ j(not_equal, &not_applicable);
4529 if (IsSimpleMapChangeTransition(from_kind, to_kind)) {
4530 Register new_map_reg = ToRegister(instr->new_map_temp());
4531 __ Move(new_map_reg, to_map, RelocInfo::EMBEDDED_OBJECT);
4532 __ movp(FieldOperand(object_reg, HeapObject::kMapOffset), new_map_reg);
4533 // Write barrier.
4534 __ RecordWriteForMap(object_reg, new_map_reg, ToRegister(instr->temp()),
4535 kDontSaveFPRegs);
4536 } else {
4537 DCHECK(object_reg.is(rax));
4538 DCHECK(ToRegister(instr->context()).is(rsi));
4539 PushSafepointRegistersScope scope(this);
4540 __ Move(rbx, to_map);
4541 bool is_js_array = from_map->instance_type() == JS_ARRAY_TYPE;
4542 TransitionElementsKindStub stub(isolate(), from_kind, to_kind, is_js_array);
4543 __ CallStub(&stub);
4544 RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0);
4545 }
4546 __ bind(&not_applicable);
4547 }
4548
4549
4550 void LCodeGen::DoTrapAllocationMemento(LTrapAllocationMemento* instr) {
4551 Register object = ToRegister(instr->object());
4552 Register temp = ToRegister(instr->temp());
4553 Label no_memento_found;
4554 __ TestJSArrayForAllocationMemento(object, temp, &no_memento_found);
4555 DeoptimizeIf(equal, instr, Deoptimizer::kMementoFound);
4556 __ bind(&no_memento_found);
4557 }
4558
4559
4560 void LCodeGen::DoStringAdd(LStringAdd* instr) {
4561 DCHECK(ToRegister(instr->context()).is(rsi));
4562 DCHECK(ToRegister(instr->left()).is(rdx));
4563 DCHECK(ToRegister(instr->right()).is(rax));
4564 StringAddStub stub(isolate(),
4565 instr->hydrogen()->flags(),
4566 instr->hydrogen()->pretenure_flag());
4567 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
4568 }
4569
4570
4571 void LCodeGen::DoStringCharCodeAt(LStringCharCodeAt* instr) {
4572 class DeferredStringCharCodeAt final : public LDeferredCode {
4573 public:
4574 DeferredStringCharCodeAt(LCodeGen* codegen, LStringCharCodeAt* instr)
4575 : LDeferredCode(codegen), instr_(instr) { }
4576 void Generate() override { codegen()->DoDeferredStringCharCodeAt(instr_); }
4577 LInstruction* instr() override { return instr_; }
4578
4579 private:
4580 LStringCharCodeAt* instr_;
4581 };
4582
4583 DeferredStringCharCodeAt* deferred =
4584 new(zone()) DeferredStringCharCodeAt(this, instr);
4585
4586 StringCharLoadGenerator::Generate(masm(),
4587 ToRegister(instr->string()),
4588 ToRegister(instr->index()),
4589 ToRegister(instr->result()),
4590 deferred->entry());
4591 __ bind(deferred->exit());
4592 }
4593
4594
4595 void LCodeGen::DoDeferredStringCharCodeAt(LStringCharCodeAt* instr) {
4596 Register string = ToRegister(instr->string());
4597 Register result = ToRegister(instr->result());
4598
4599 // TODO(3095996): Get rid of this. For now, we need to make the
4600 // result register contain a valid pointer because it is already
4601 // contained in the register pointer map.
4602 __ Set(result, 0);
4603
4604 PushSafepointRegistersScope scope(this);
4605 __ Push(string);
4606 // Push the index as a smi. This is safe because of the checks in
4607 // DoStringCharCodeAt above.
4608 STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue);
4609 if (instr->index()->IsConstantOperand()) {
4610 int32_t const_index = ToInteger32(LConstantOperand::cast(instr->index()));
4611 __ Push(Smi::FromInt(const_index));
4612 } else {
4613 Register index = ToRegister(instr->index());
4614 __ Integer32ToSmi(index, index);
4615 __ Push(index);
4616 }
4617 CallRuntimeFromDeferred(
4618 Runtime::kStringCharCodeAtRT, 2, instr, instr->context());
4619 __ AssertSmi(rax);
4620 __ SmiToInteger32(rax, rax);
4621 __ StoreToSafepointRegisterSlot(result, rax);
4622 }
4623
4624
4625 void LCodeGen::DoStringCharFromCode(LStringCharFromCode* instr) {
4626 class DeferredStringCharFromCode final : public LDeferredCode {
4627 public:
4628 DeferredStringCharFromCode(LCodeGen* codegen, LStringCharFromCode* instr)
4629 : LDeferredCode(codegen), instr_(instr) { }
4630 void Generate() override {
4631 codegen()->DoDeferredStringCharFromCode(instr_);
4632 }
4633 LInstruction* instr() override { return instr_; }
4634
4635 private:
4636 LStringCharFromCode* instr_;
4637 };
4638
4639 DeferredStringCharFromCode* deferred =
4640 new(zone()) DeferredStringCharFromCode(this, instr);
4641
4642 DCHECK(instr->hydrogen()->value()->representation().IsInteger32());
4643 Register char_code = ToRegister(instr->char_code());
4644 Register result = ToRegister(instr->result());
4645 DCHECK(!char_code.is(result));
4646
4647 __ cmpl(char_code, Immediate(String::kMaxOneByteCharCode));
4648 __ j(above, deferred->entry());
4649 __ movsxlq(char_code, char_code);
4650 __ LoadRoot(result, Heap::kSingleCharacterStringCacheRootIndex);
4651 __ movp(result, FieldOperand(result,
4652 char_code, times_pointer_size,
4653 FixedArray::kHeaderSize));
4654 __ CompareRoot(result, Heap::kUndefinedValueRootIndex);
4655 __ j(equal, deferred->entry());
4656 __ bind(deferred->exit());
4657 }
4658
4659
4660 void LCodeGen::DoDeferredStringCharFromCode(LStringCharFromCode* instr) {
4661 Register char_code = ToRegister(instr->char_code());
4662 Register result = ToRegister(instr->result());
4663
4664 // TODO(3095996): Get rid of this. For now, we need to make the
4665 // result register contain a valid pointer because it is already
4666 // contained in the register pointer map.
4667 __ Set(result, 0);
4668
4669 PushSafepointRegistersScope scope(this);
4670 __ Integer32ToSmi(char_code, char_code);
4671 __ Push(char_code);
4672 CallRuntimeFromDeferred(Runtime::kCharFromCode, 1, instr, instr->context());
4673 __ StoreToSafepointRegisterSlot(result, rax);
4674 }
4675
4676
4677 void LCodeGen::DoInteger32ToDouble(LInteger32ToDouble* instr) {
4678 LOperand* input = instr->value();
4679 DCHECK(input->IsRegister() || input->IsStackSlot());
4680 LOperand* output = instr->result();
4681 DCHECK(output->IsDoubleRegister());
4682 if (input->IsRegister()) {
4683 __ Cvtlsi2sd(ToDoubleRegister(output), ToRegister(input));
4684 } else {
4685 __ Cvtlsi2sd(ToDoubleRegister(output), ToOperand(input));
4686 }
4687 }
4688
4689
4690 void LCodeGen::DoUint32ToDouble(LUint32ToDouble* instr) {
4691 LOperand* input = instr->value();
4692 LOperand* output = instr->result();
4693
4694 __ LoadUint32(ToDoubleRegister(output), ToRegister(input));
4695 }
4696
4697
4698 void LCodeGen::DoNumberTagI(LNumberTagI* instr) {
4699 class DeferredNumberTagI final : public LDeferredCode {
4700 public:
4701 DeferredNumberTagI(LCodeGen* codegen, LNumberTagI* instr)
4702 : LDeferredCode(codegen), instr_(instr) { }
4703 void Generate() override {
4704 codegen()->DoDeferredNumberTagIU(instr_, instr_->value(), instr_->temp1(),
4705 instr_->temp2(), SIGNED_INT32);
4706 }
4707 LInstruction* instr() override { return instr_; }
4708
4709 private:
4710 LNumberTagI* instr_;
4711 };
4712
4713 LOperand* input = instr->value();
4714 DCHECK(input->IsRegister() && input->Equals(instr->result()));
4715 Register reg = ToRegister(input);
4716
4717 if (SmiValuesAre32Bits()) {
4718 __ Integer32ToSmi(reg, reg);
4719 } else {
4720 DCHECK(SmiValuesAre31Bits());
4721 DeferredNumberTagI* deferred = new(zone()) DeferredNumberTagI(this, instr);
4722 __ Integer32ToSmi(reg, reg);
4723 __ j(overflow, deferred->entry());
4724 __ bind(deferred->exit());
4725 }
4726 }
4727
4728
4729 void LCodeGen::DoNumberTagU(LNumberTagU* instr) {
4730 class DeferredNumberTagU final : public LDeferredCode {
4731 public:
4732 DeferredNumberTagU(LCodeGen* codegen, LNumberTagU* instr)
4733 : LDeferredCode(codegen), instr_(instr) { }
4734 void Generate() override {
4735 codegen()->DoDeferredNumberTagIU(instr_, instr_->value(), instr_->temp1(),
4736 instr_->temp2(), UNSIGNED_INT32);
4737 }
4738 LInstruction* instr() override { return instr_; }
4739
4740 private:
4741 LNumberTagU* instr_;
4742 };
4743
4744 LOperand* input = instr->value();
4745 DCHECK(input->IsRegister() && input->Equals(instr->result()));
4746 Register reg = ToRegister(input);
4747
4748 DeferredNumberTagU* deferred = new(zone()) DeferredNumberTagU(this, instr);
4749 __ cmpl(reg, Immediate(Smi::kMaxValue));
4750 __ j(above, deferred->entry());
4751 __ Integer32ToSmi(reg, reg);
4752 __ bind(deferred->exit());
4753 }
4754
4755
4756 void LCodeGen::DoDeferredNumberTagIU(LInstruction* instr,
4757 LOperand* value,
4758 LOperand* temp1,
4759 LOperand* temp2,
4760 IntegerSignedness signedness) {
4761 Label done, slow;
4762 Register reg = ToRegister(value);
4763 Register tmp = ToRegister(temp1);
4764 XMMRegister temp_xmm = ToDoubleRegister(temp2);
4765
4766 // Load value into temp_xmm which will be preserved across potential call to
4767 // runtime (MacroAssembler::EnterExitFrameEpilogue preserves only allocatable
4768 // XMM registers on x64).
4769 if (signedness == SIGNED_INT32) {
4770 DCHECK(SmiValuesAre31Bits());
4771 // There was overflow, so bits 30 and 31 of the original integer
4772 // disagree. Try to allocate a heap number in new space and store
4773 // the value in there. If that fails, call the runtime system.
4774 __ SmiToInteger32(reg, reg);
4775 __ xorl(reg, Immediate(0x80000000));
4776 __ Cvtlsi2sd(temp_xmm, reg);
4777 } else {
4778 DCHECK(signedness == UNSIGNED_INT32);
4779 __ LoadUint32(temp_xmm, reg);
4780 }
4781
4782 if (FLAG_inline_new) {
4783 __ AllocateHeapNumber(reg, tmp, &slow);
4784 __ jmp(&done, kPointerSize == kInt64Size ? Label::kNear : Label::kFar);
4785 }
4786
4787 // Slow case: Call the runtime system to do the number allocation.
4788 __ bind(&slow);
4789 {
4790 // Put a valid pointer value in the stack slot where the result
4791 // register is stored, as this register is in the pointer map, but contains
4792 // an integer value.
4793 __ Set(reg, 0);
4794
4795 // Preserve the value of all registers.
4796 PushSafepointRegistersScope scope(this);
4797
4798 // NumberTagIU uses the context from the frame, rather than
4799 // the environment's HContext or HInlinedContext value.
4800 // They only call Runtime::kAllocateHeapNumber.
4801 // The corresponding HChange instructions are added in a phase that does
4802 // not have easy access to the local context.
4803 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
4804 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4805 RecordSafepointWithRegisters(
4806 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4807 __ StoreToSafepointRegisterSlot(reg, rax);
4808 }
4809
4810 // Done. Put the value in temp_xmm into the value of the allocated heap
4811 // number.
4812 __ bind(&done);
4813 __ Movsd(FieldOperand(reg, HeapNumber::kValueOffset), temp_xmm);
4814 }
4815
4816
4817 void LCodeGen::DoNumberTagD(LNumberTagD* instr) {
4818 class DeferredNumberTagD final : public LDeferredCode {
4819 public:
4820 DeferredNumberTagD(LCodeGen* codegen, LNumberTagD* instr)
4821 : LDeferredCode(codegen), instr_(instr) { }
4822 void Generate() override { codegen()->DoDeferredNumberTagD(instr_); }
4823 LInstruction* instr() override { return instr_; }
4824
4825 private:
4826 LNumberTagD* instr_;
4827 };
4828
4829 XMMRegister input_reg = ToDoubleRegister(instr->value());
4830 Register reg = ToRegister(instr->result());
4831 Register tmp = ToRegister(instr->temp());
4832
4833 DeferredNumberTagD* deferred = new(zone()) DeferredNumberTagD(this, instr);
4834 if (FLAG_inline_new) {
4835 __ AllocateHeapNumber(reg, tmp, deferred->entry());
4836 } else {
4837 __ jmp(deferred->entry());
4838 }
4839 __ bind(deferred->exit());
4840 __ Movsd(FieldOperand(reg, HeapNumber::kValueOffset), input_reg);
4841 }
4842
4843
4844 void LCodeGen::DoDeferredNumberTagD(LNumberTagD* instr) {
4845 // TODO(3095996): Get rid of this. For now, we need to make the
4846 // result register contain a valid pointer because it is already
4847 // contained in the register pointer map.
4848 Register reg = ToRegister(instr->result());
4849 __ Move(reg, Smi::FromInt(0));
4850
4851 {
4852 PushSafepointRegistersScope scope(this);
4853 // NumberTagD uses the context from the frame, rather than
4854 // the environment's HContext or HInlinedContext value.
4855 // They only call Runtime::kAllocateHeapNumber.
4856 // The corresponding HChange instructions are added in a phase that does
4857 // not have easy access to the local context.
4858 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
4859 __ CallRuntimeSaveDoubles(Runtime::kAllocateHeapNumber);
4860 RecordSafepointWithRegisters(
4861 instr->pointer_map(), 0, Safepoint::kNoLazyDeopt);
4862 __ movp(kScratchRegister, rax);
4863 }
4864 __ movp(reg, kScratchRegister);
4865 }
4866
4867
4868 void LCodeGen::DoSmiTag(LSmiTag* instr) {
4869 HChange* hchange = instr->hydrogen();
4870 Register input = ToRegister(instr->value());
4871 Register output = ToRegister(instr->result());
4872 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4873 hchange->value()->CheckFlag(HValue::kUint32)) {
4874 Condition is_smi = __ CheckUInteger32ValidSmiValue(input);
4875 DeoptimizeIf(NegateCondition(is_smi), instr, Deoptimizer::kOverflow);
4876 }
4877 __ Integer32ToSmi(output, input);
4878 if (hchange->CheckFlag(HValue::kCanOverflow) &&
4879 !hchange->value()->CheckFlag(HValue::kUint32)) {
4880 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
4881 }
4882 }
4883
4884
4885 void LCodeGen::DoSmiUntag(LSmiUntag* instr) {
4886 DCHECK(instr->value()->Equals(instr->result()));
4887 Register input = ToRegister(instr->value());
4888 if (instr->needs_check()) {
4889 Condition is_smi = __ CheckSmi(input);
4890 DeoptimizeIf(NegateCondition(is_smi), instr, Deoptimizer::kNotASmi);
4891 } else {
4892 __ AssertSmi(input);
4893 }
4894 __ SmiToInteger32(input, input);
4895 }
4896
4897
4898 void LCodeGen::EmitNumberUntagD(LNumberUntagD* instr, Register input_reg,
4899 XMMRegister result_reg, NumberUntagDMode mode) {
4900 bool can_convert_undefined_to_nan =
4901 instr->hydrogen()->can_convert_undefined_to_nan();
4902 bool deoptimize_on_minus_zero = instr->hydrogen()->deoptimize_on_minus_zero();
4903
4904 Label convert, load_smi, done;
4905
4906 if (mode == NUMBER_CANDIDATE_IS_ANY_TAGGED) {
4907 // Smi check.
4908 __ JumpIfSmi(input_reg, &load_smi, Label::kNear);
4909
4910 // Heap number map check.
4911 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
4912 Heap::kHeapNumberMapRootIndex);
4913
4914 // On x64 it is safe to load at heap number offset before evaluating the map
4915 // check, since all heap objects are at least two words long.
4916 __ Movsd(result_reg, FieldOperand(input_reg, HeapNumber::kValueOffset));
4917
4918 if (can_convert_undefined_to_nan) {
4919 __ j(not_equal, &convert, Label::kNear);
4920 } else {
4921 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
4922 }
4923
4924 if (deoptimize_on_minus_zero) {
4925 XMMRegister xmm_scratch = double_scratch0();
4926 __ Xorpd(xmm_scratch, xmm_scratch);
4927 __ Ucomisd(xmm_scratch, result_reg);
4928 __ j(not_equal, &done, Label::kNear);
4929 __ Movmskpd(kScratchRegister, result_reg);
4930 __ testl(kScratchRegister, Immediate(1));
4931 DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero);
4932 }
4933 __ jmp(&done, Label::kNear);
4934
4935 if (can_convert_undefined_to_nan) {
4936 __ bind(&convert);
4937
4938 // Convert undefined (and hole) to NaN. Compute NaN as 0/0.
4939 __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex);
4940 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumberUndefined);
4941
4942 __ pcmpeqd(result_reg, result_reg);
4943 __ jmp(&done, Label::kNear);
4944 }
4945 } else {
4946 DCHECK(mode == NUMBER_CANDIDATE_IS_SMI);
4947 }
4948
4949 // Smi to XMM conversion
4950 __ bind(&load_smi);
4951 __ SmiToInteger32(kScratchRegister, input_reg);
4952 __ Cvtlsi2sd(result_reg, kScratchRegister);
4953 __ bind(&done);
4954 }
4955
4956
4957 void LCodeGen::DoDeferredTaggedToI(LTaggedToI* instr, Label* done) {
4958 Register input_reg = ToRegister(instr->value());
4959
4960 if (instr->truncating()) {
4961 Label no_heap_number, check_bools, check_false;
4962
4963 // Heap number map check.
4964 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
4965 Heap::kHeapNumberMapRootIndex);
4966 __ j(not_equal, &no_heap_number, Label::kNear);
4967 __ TruncateHeapNumberToI(input_reg, input_reg);
4968 __ jmp(done);
4969
4970 __ bind(&no_heap_number);
4971 // Check for Oddballs. Undefined/False is converted to zero and True to one
4972 // for truncating conversions.
4973 __ CompareRoot(input_reg, Heap::kUndefinedValueRootIndex);
4974 __ j(not_equal, &check_bools, Label::kNear);
4975 __ Set(input_reg, 0);
4976 __ jmp(done);
4977
4978 __ bind(&check_bools);
4979 __ CompareRoot(input_reg, Heap::kTrueValueRootIndex);
4980 __ j(not_equal, &check_false, Label::kNear);
4981 __ Set(input_reg, 1);
4982 __ jmp(done);
4983
4984 __ bind(&check_false);
4985 __ CompareRoot(input_reg, Heap::kFalseValueRootIndex);
4986 DeoptimizeIf(not_equal, instr,
4987 Deoptimizer::kNotAHeapNumberUndefinedBoolean);
4988 __ Set(input_reg, 0);
4989 } else {
4990 XMMRegister scratch = ToDoubleRegister(instr->temp());
4991 DCHECK(!scratch.is(xmm0));
4992 __ CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
4993 Heap::kHeapNumberMapRootIndex);
4994 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumber);
4995 __ Movsd(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset));
4996 __ Cvttsd2si(input_reg, xmm0);
4997 __ Cvtlsi2sd(scratch, input_reg);
4998 __ Ucomisd(xmm0, scratch);
4999 DeoptimizeIf(not_equal, instr, Deoptimizer::kLostPrecision);
5000 DeoptimizeIf(parity_even, instr, Deoptimizer::kNaN);
5001 if (instr->hydrogen()->GetMinusZeroMode() == FAIL_ON_MINUS_ZERO) {
5002 __ testl(input_reg, input_reg);
5003 __ j(not_zero, done);
5004 __ Movmskpd(input_reg, xmm0);
5005 __ andl(input_reg, Immediate(1));
5006 DeoptimizeIf(not_zero, instr, Deoptimizer::kMinusZero);
5007 }
5008 }
5009 }
5010
5011
5012 void LCodeGen::DoTaggedToI(LTaggedToI* instr) {
5013 class DeferredTaggedToI final : public LDeferredCode {
5014 public:
5015 DeferredTaggedToI(LCodeGen* codegen, LTaggedToI* instr)
5016 : LDeferredCode(codegen), instr_(instr) { }
5017 void Generate() override { codegen()->DoDeferredTaggedToI(instr_, done()); }
5018 LInstruction* instr() override { return instr_; }
5019
5020 private:
5021 LTaggedToI* instr_;
5022 };
5023
5024 LOperand* input = instr->value();
5025 DCHECK(input->IsRegister());
5026 DCHECK(input->Equals(instr->result()));
5027 Register input_reg = ToRegister(input);
5028
5029 if (instr->hydrogen()->value()->representation().IsSmi()) {
5030 __ SmiToInteger32(input_reg, input_reg);
5031 } else {
5032 DeferredTaggedToI* deferred = new(zone()) DeferredTaggedToI(this, instr);
5033 __ JumpIfNotSmi(input_reg, deferred->entry());
5034 __ SmiToInteger32(input_reg, input_reg);
5035 __ bind(deferred->exit());
5036 }
5037 }
5038
5039
5040 void LCodeGen::DoNumberUntagD(LNumberUntagD* instr) {
5041 LOperand* input = instr->value();
5042 DCHECK(input->IsRegister());
5043 LOperand* result = instr->result();
5044 DCHECK(result->IsDoubleRegister());
5045
5046 Register input_reg = ToRegister(input);
5047 XMMRegister result_reg = ToDoubleRegister(result);
5048
5049 HValue* value = instr->hydrogen()->value();
5050 NumberUntagDMode mode = value->representation().IsSmi()
5051 ? NUMBER_CANDIDATE_IS_SMI : NUMBER_CANDIDATE_IS_ANY_TAGGED;
5052
5053 EmitNumberUntagD(instr, input_reg, result_reg, mode);
5054 }
5055
5056
5057 void LCodeGen::DoDoubleToI(LDoubleToI* instr) {
5058 LOperand* input = instr->value();
5059 DCHECK(input->IsDoubleRegister());
5060 LOperand* result = instr->result();
5061 DCHECK(result->IsRegister());
5062
5063 XMMRegister input_reg = ToDoubleRegister(input);
5064 Register result_reg = ToRegister(result);
5065
5066 if (instr->truncating()) {
5067 __ TruncateDoubleToI(result_reg, input_reg);
5068 } else {
5069 Label lost_precision, is_nan, minus_zero, done;
5070 XMMRegister xmm_scratch = double_scratch0();
5071 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
5072 __ DoubleToI(result_reg, input_reg, xmm_scratch,
5073 instr->hydrogen()->GetMinusZeroMode(), &lost_precision,
5074 &is_nan, &minus_zero, dist);
5075 __ jmp(&done, dist);
5076 __ bind(&lost_precision);
5077 DeoptimizeIf(no_condition, instr, Deoptimizer::kLostPrecision);
5078 __ bind(&is_nan);
5079 DeoptimizeIf(no_condition, instr, Deoptimizer::kNaN);
5080 __ bind(&minus_zero);
5081 DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero);
5082 __ bind(&done);
5083 }
5084 }
5085
5086
5087 void LCodeGen::DoDoubleToSmi(LDoubleToSmi* instr) {
5088 LOperand* input = instr->value();
5089 DCHECK(input->IsDoubleRegister());
5090 LOperand* result = instr->result();
5091 DCHECK(result->IsRegister());
5092
5093 XMMRegister input_reg = ToDoubleRegister(input);
5094 Register result_reg = ToRegister(result);
5095
5096 Label lost_precision, is_nan, minus_zero, done;
5097 XMMRegister xmm_scratch = double_scratch0();
5098 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
5099 __ DoubleToI(result_reg, input_reg, xmm_scratch,
5100 instr->hydrogen()->GetMinusZeroMode(), &lost_precision, &is_nan,
5101 &minus_zero, dist);
5102 __ jmp(&done, dist);
5103 __ bind(&lost_precision);
5104 DeoptimizeIf(no_condition, instr, Deoptimizer::kLostPrecision);
5105 __ bind(&is_nan);
5106 DeoptimizeIf(no_condition, instr, Deoptimizer::kNaN);
5107 __ bind(&minus_zero);
5108 DeoptimizeIf(no_condition, instr, Deoptimizer::kMinusZero);
5109 __ bind(&done);
5110 __ Integer32ToSmi(result_reg, result_reg);
5111 DeoptimizeIf(overflow, instr, Deoptimizer::kOverflow);
5112 }
5113
5114
5115 void LCodeGen::DoCheckSmi(LCheckSmi* instr) {
5116 LOperand* input = instr->value();
5117 Condition cc = masm()->CheckSmi(ToRegister(input));
5118 DeoptimizeIf(NegateCondition(cc), instr, Deoptimizer::kNotASmi);
5119 }
5120
5121
5122 void LCodeGen::DoCheckNonSmi(LCheckNonSmi* instr) {
5123 if (!instr->hydrogen()->value()->type().IsHeapObject()) {
5124 LOperand* input = instr->value();
5125 Condition cc = masm()->CheckSmi(ToRegister(input));
5126 DeoptimizeIf(cc, instr, Deoptimizer::kSmi);
5127 }
5128 }
5129
5130
5131 void LCodeGen::DoCheckArrayBufferNotNeutered(
5132 LCheckArrayBufferNotNeutered* instr) {
5133 Register view = ToRegister(instr->view());
5134
5135 __ movp(kScratchRegister,
5136 FieldOperand(view, JSArrayBufferView::kBufferOffset));
5137 __ testb(FieldOperand(kScratchRegister, JSArrayBuffer::kBitFieldOffset),
5138 Immediate(1 << JSArrayBuffer::WasNeutered::kShift));
5139 DeoptimizeIf(not_zero, instr, Deoptimizer::kOutOfBounds);
5140 }
5141
5142
5143 void LCodeGen::DoCheckInstanceType(LCheckInstanceType* instr) {
5144 Register input = ToRegister(instr->value());
5145
5146 __ movp(kScratchRegister, FieldOperand(input, HeapObject::kMapOffset));
5147
5148 if (instr->hydrogen()->is_interval_check()) {
5149 InstanceType first;
5150 InstanceType last;
5151 instr->hydrogen()->GetCheckInterval(&first, &last);
5152
5153 __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
5154 Immediate(static_cast<int8_t>(first)));
5155
5156 // If there is only one type in the interval check for equality.
5157 if (first == last) {
5158 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongInstanceType);
5159 } else {
5160 DeoptimizeIf(below, instr, Deoptimizer::kWrongInstanceType);
5161 // Omit check for the last type.
5162 if (last != LAST_TYPE) {
5163 __ cmpb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
5164 Immediate(static_cast<int8_t>(last)));
5165 DeoptimizeIf(above, instr, Deoptimizer::kWrongInstanceType);
5166 }
5167 }
5168 } else {
5169 uint8_t mask;
5170 uint8_t tag;
5171 instr->hydrogen()->GetCheckMaskAndTag(&mask, &tag);
5172
5173 if (base::bits::IsPowerOfTwo32(mask)) {
5174 DCHECK(tag == 0 || base::bits::IsPowerOfTwo32(tag));
5175 __ testb(FieldOperand(kScratchRegister, Map::kInstanceTypeOffset),
5176 Immediate(mask));
5177 DeoptimizeIf(tag == 0 ? not_zero : zero, instr,
5178 Deoptimizer::kWrongInstanceType);
5179 } else {
5180 __ movzxbl(kScratchRegister,
5181 FieldOperand(kScratchRegister, Map::kInstanceTypeOffset));
5182 __ andb(kScratchRegister, Immediate(mask));
5183 __ cmpb(kScratchRegister, Immediate(tag));
5184 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongInstanceType);
5185 }
5186 }
5187 }
5188
5189
5190 void LCodeGen::DoCheckValue(LCheckValue* instr) {
5191 Register reg = ToRegister(instr->value());
5192 __ Cmp(reg, instr->hydrogen()->object().handle());
5193 DeoptimizeIf(not_equal, instr, Deoptimizer::kValueMismatch);
5194 }
5195
5196
5197 void LCodeGen::DoDeferredInstanceMigration(LCheckMaps* instr, Register object) {
5198 {
5199 PushSafepointRegistersScope scope(this);
5200 __ Push(object);
5201 __ Set(rsi, 0);
5202 __ CallRuntimeSaveDoubles(Runtime::kTryMigrateInstance);
5203 RecordSafepointWithRegisters(
5204 instr->pointer_map(), 1, Safepoint::kNoLazyDeopt);
5205
5206 __ testp(rax, Immediate(kSmiTagMask));
5207 }
5208 DeoptimizeIf(zero, instr, Deoptimizer::kInstanceMigrationFailed);
5209 }
5210
5211
5212 void LCodeGen::DoCheckMaps(LCheckMaps* instr) {
5213 class DeferredCheckMaps final : public LDeferredCode {
5214 public:
5215 DeferredCheckMaps(LCodeGen* codegen, LCheckMaps* instr, Register object)
5216 : LDeferredCode(codegen), instr_(instr), object_(object) {
5217 SetExit(check_maps());
5218 }
5219 void Generate() override {
5220 codegen()->DoDeferredInstanceMigration(instr_, object_);
5221 }
5222 Label* check_maps() { return &check_maps_; }
5223 LInstruction* instr() override { return instr_; }
5224
5225 private:
5226 LCheckMaps* instr_;
5227 Label check_maps_;
5228 Register object_;
5229 };
5230
5231 if (instr->hydrogen()->IsStabilityCheck()) {
5232 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5233 for (int i = 0; i < maps->size(); ++i) {
5234 AddStabilityDependency(maps->at(i).handle());
5235 }
5236 return;
5237 }
5238
5239 LOperand* input = instr->value();
5240 DCHECK(input->IsRegister());
5241 Register reg = ToRegister(input);
5242
5243 DeferredCheckMaps* deferred = NULL;
5244 if (instr->hydrogen()->HasMigrationTarget()) {
5245 deferred = new(zone()) DeferredCheckMaps(this, instr, reg);
5246 __ bind(deferred->check_maps());
5247 }
5248
5249 const UniqueSet<Map>* maps = instr->hydrogen()->maps();
5250 Label success;
5251 for (int i = 0; i < maps->size() - 1; i++) {
5252 Handle<Map> map = maps->at(i).handle();
5253 __ CompareMap(reg, map);
5254 __ j(equal, &success, Label::kNear);
5255 }
5256
5257 Handle<Map> map = maps->at(maps->size() - 1).handle();
5258 __ CompareMap(reg, map);
5259 if (instr->hydrogen()->HasMigrationTarget()) {
5260 __ j(not_equal, deferred->entry());
5261 } else {
5262 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongMap);
5263 }
5264
5265 __ bind(&success);
5266 }
5267
5268
5269 void LCodeGen::DoClampDToUint8(LClampDToUint8* instr) {
5270 XMMRegister value_reg = ToDoubleRegister(instr->unclamped());
5271 XMMRegister xmm_scratch = double_scratch0();
5272 Register result_reg = ToRegister(instr->result());
5273 __ ClampDoubleToUint8(value_reg, xmm_scratch, result_reg);
5274 }
5275
5276
5277 void LCodeGen::DoClampIToUint8(LClampIToUint8* instr) {
5278 DCHECK(instr->unclamped()->Equals(instr->result()));
5279 Register value_reg = ToRegister(instr->result());
5280 __ ClampUint8(value_reg);
5281 }
5282
5283
5284 void LCodeGen::DoClampTToUint8(LClampTToUint8* instr) {
5285 DCHECK(instr->unclamped()->Equals(instr->result()));
5286 Register input_reg = ToRegister(instr->unclamped());
5287 XMMRegister temp_xmm_reg = ToDoubleRegister(instr->temp_xmm());
5288 XMMRegister xmm_scratch = double_scratch0();
5289 Label is_smi, done, heap_number;
5290 Label::Distance dist = DeoptEveryNTimes() ? Label::kFar : Label::kNear;
5291 __ JumpIfSmi(input_reg, &is_smi, dist);
5292
5293 // Check for heap number
5294 __ Cmp(FieldOperand(input_reg, HeapObject::kMapOffset),
5295 factory()->heap_number_map());
5296 __ j(equal, &heap_number, Label::kNear);
5297
5298 // Check for undefined. Undefined is converted to zero for clamping
5299 // conversions.
5300 __ Cmp(input_reg, factory()->undefined_value());
5301 DeoptimizeIf(not_equal, instr, Deoptimizer::kNotAHeapNumberUndefined);
5302 __ xorl(input_reg, input_reg);
5303 __ jmp(&done, Label::kNear);
5304
5305 // Heap number
5306 __ bind(&heap_number);
5307 __ Movsd(xmm_scratch, FieldOperand(input_reg, HeapNumber::kValueOffset));
5308 __ ClampDoubleToUint8(xmm_scratch, temp_xmm_reg, input_reg);
5309 __ jmp(&done, Label::kNear);
5310
5311 // smi
5312 __ bind(&is_smi);
5313 __ SmiToInteger32(input_reg, input_reg);
5314 __ ClampUint8(input_reg);
5315
5316 __ bind(&done);
5317 }
5318
5319
5320 void LCodeGen::DoDoubleBits(LDoubleBits* instr) {
5321 XMMRegister value_reg = ToDoubleRegister(instr->value());
5322 Register result_reg = ToRegister(instr->result());
5323 if (instr->hydrogen()->bits() == HDoubleBits::HIGH) {
5324 __ Movq(result_reg, value_reg);
5325 __ shrq(result_reg, Immediate(32));
5326 } else {
5327 __ Movd(result_reg, value_reg);
5328 }
5329 }
5330
5331
5332 void LCodeGen::DoConstructDouble(LConstructDouble* instr) {
5333 Register hi_reg = ToRegister(instr->hi());
5334 Register lo_reg = ToRegister(instr->lo());
5335 XMMRegister result_reg = ToDoubleRegister(instr->result());
5336 __ movl(kScratchRegister, hi_reg);
5337 __ shlq(kScratchRegister, Immediate(32));
5338 __ orq(kScratchRegister, lo_reg);
5339 __ Movq(result_reg, kScratchRegister);
5340 }
5341
5342
5343 void LCodeGen::DoAllocate(LAllocate* instr) {
5344 class DeferredAllocate final : public LDeferredCode {
5345 public:
5346 DeferredAllocate(LCodeGen* codegen, LAllocate* instr)
5347 : LDeferredCode(codegen), instr_(instr) { }
5348 void Generate() override { codegen()->DoDeferredAllocate(instr_); }
5349 LInstruction* instr() override { return instr_; }
5350
5351 private:
5352 LAllocate* instr_;
5353 };
5354
5355 DeferredAllocate* deferred =
5356 new(zone()) DeferredAllocate(this, instr);
5357
5358 Register result = ToRegister(instr->result());
5359 Register temp = ToRegister(instr->temp());
5360
5361 // Allocate memory for the object.
5362 AllocationFlags flags = TAG_OBJECT;
5363 if (instr->hydrogen()->MustAllocateDoubleAligned()) {
5364 flags = static_cast<AllocationFlags>(flags | DOUBLE_ALIGNMENT);
5365 }
5366 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5367 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5368 flags = static_cast<AllocationFlags>(flags | PRETENURE);
5369 }
5370
5371 if (instr->size()->IsConstantOperand()) {
5372 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5373 CHECK(size <= Page::kMaxRegularHeapObjectSize);
5374 __ Allocate(size, result, temp, no_reg, deferred->entry(), flags);
5375 } else {
5376 Register size = ToRegister(instr->size());
5377 __ Allocate(size, result, temp, no_reg, deferred->entry(), flags);
5378 }
5379
5380 __ bind(deferred->exit());
5381
5382 if (instr->hydrogen()->MustPrefillWithFiller()) {
5383 if (instr->size()->IsConstantOperand()) {
5384 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5385 __ movl(temp, Immediate((size / kPointerSize) - 1));
5386 } else {
5387 temp = ToRegister(instr->size());
5388 __ sarp(temp, Immediate(kPointerSizeLog2));
5389 __ decl(temp);
5390 }
5391 Label loop;
5392 __ bind(&loop);
5393 __ Move(FieldOperand(result, temp, times_pointer_size, 0),
5394 isolate()->factory()->one_pointer_filler_map());
5395 __ decl(temp);
5396 __ j(not_zero, &loop);
5397 }
5398 }
5399
5400
5401 void LCodeGen::DoDeferredAllocate(LAllocate* instr) {
5402 Register result = ToRegister(instr->result());
5403
5404 // TODO(3095996): Get rid of this. For now, we need to make the
5405 // result register contain a valid pointer because it is already
5406 // contained in the register pointer map.
5407 __ Move(result, Smi::FromInt(0));
5408
5409 PushSafepointRegistersScope scope(this);
5410 if (instr->size()->IsRegister()) {
5411 Register size = ToRegister(instr->size());
5412 DCHECK(!size.is(result));
5413 __ Integer32ToSmi(size, size);
5414 __ Push(size);
5415 } else {
5416 int32_t size = ToInteger32(LConstantOperand::cast(instr->size()));
5417 __ Push(Smi::FromInt(size));
5418 }
5419
5420 int flags = 0;
5421 if (instr->hydrogen()->IsOldSpaceAllocation()) {
5422 DCHECK(!instr->hydrogen()->IsNewSpaceAllocation());
5423 flags = AllocateTargetSpace::update(flags, OLD_SPACE);
5424 } else {
5425 flags = AllocateTargetSpace::update(flags, NEW_SPACE);
5426 }
5427 __ Push(Smi::FromInt(flags));
5428
5429 CallRuntimeFromDeferred(
5430 Runtime::kAllocateInTargetSpace, 2, instr, instr->context());
5431 __ StoreToSafepointRegisterSlot(result, rax);
5432 }
5433
5434
5435 void LCodeGen::DoToFastProperties(LToFastProperties* instr) {
5436 DCHECK(ToRegister(instr->value()).is(rax));
5437 __ Push(rax);
5438 CallRuntime(Runtime::kToFastProperties, 1, instr);
5439 }
5440
5441
5442 void LCodeGen::DoRegExpLiteral(LRegExpLiteral* instr) {
5443 DCHECK(ToRegister(instr->context()).is(rsi));
5444 Label materialized;
5445 // Registers will be used as follows:
5446 // rcx = literals array.
5447 // rbx = regexp literal.
5448 // rax = regexp literal clone.
5449 int literal_offset =
5450 LiteralsArray::OffsetOfLiteralAt(instr->hydrogen()->literal_index());
5451 __ Move(rcx, instr->hydrogen()->literals());
5452 __ movp(rbx, FieldOperand(rcx, literal_offset));
5453 __ CompareRoot(rbx, Heap::kUndefinedValueRootIndex);
5454 __ j(not_equal, &materialized, Label::kNear);
5455
5456 // Create regexp literal using runtime function
5457 // Result will be in rax.
5458 __ Push(rcx);
5459 __ Push(Smi::FromInt(instr->hydrogen()->literal_index()));
5460 __ Push(instr->hydrogen()->pattern());
5461 __ Push(instr->hydrogen()->flags());
5462 CallRuntime(Runtime::kMaterializeRegExpLiteral, 4, instr);
5463 __ movp(rbx, rax);
5464
5465 __ bind(&materialized);
5466 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
5467 Label allocated, runtime_allocate;
5468 __ Allocate(size, rax, rcx, rdx, &runtime_allocate, TAG_OBJECT);
5469 __ jmp(&allocated, Label::kNear);
5470
5471 __ bind(&runtime_allocate);
5472 __ Push(rbx);
5473 __ Push(Smi::FromInt(size));
5474 CallRuntime(Runtime::kAllocateInNewSpace, 1, instr);
5475 __ Pop(rbx);
5476
5477 __ bind(&allocated);
5478 // Copy the content into the newly allocated memory.
5479 // (Unroll copy loop once for better throughput).
5480 for (int i = 0; i < size - kPointerSize; i += 2 * kPointerSize) {
5481 __ movp(rdx, FieldOperand(rbx, i));
5482 __ movp(rcx, FieldOperand(rbx, i + kPointerSize));
5483 __ movp(FieldOperand(rax, i), rdx);
5484 __ movp(FieldOperand(rax, i + kPointerSize), rcx);
5485 }
5486 if ((size % (2 * kPointerSize)) != 0) {
5487 __ movp(rdx, FieldOperand(rbx, size - kPointerSize));
5488 __ movp(FieldOperand(rax, size - kPointerSize), rdx);
5489 }
5490 }
5491
5492
5493 void LCodeGen::DoTypeof(LTypeof* instr) {
5494 DCHECK(ToRegister(instr->context()).is(rsi));
5495 DCHECK(ToRegister(instr->value()).is(rbx));
5496 Label end, do_call;
5497 Register value_register = ToRegister(instr->value());
5498 __ JumpIfNotSmi(value_register, &do_call);
5499 __ Move(rax, isolate()->factory()->number_string());
5500 __ jmp(&end);
5501 __ bind(&do_call);
5502 TypeofStub stub(isolate());
5503 CallCode(stub.GetCode(), RelocInfo::CODE_TARGET, instr);
5504 __ bind(&end);
5505 }
5506
5507
5508 void LCodeGen::EmitPushTaggedOperand(LOperand* operand) {
5509 DCHECK(!operand->IsDoubleRegister());
5510 if (operand->IsConstantOperand()) {
5511 __ Push(ToHandle(LConstantOperand::cast(operand)));
5512 } else if (operand->IsRegister()) {
5513 __ Push(ToRegister(operand));
5514 } else {
5515 __ Push(ToOperand(operand));
5516 }
5517 }
5518
5519
5520 void LCodeGen::DoTypeofIsAndBranch(LTypeofIsAndBranch* instr) {
5521 Register input = ToRegister(instr->value());
5522 Condition final_branch_condition = EmitTypeofIs(instr, input);
5523 if (final_branch_condition != no_condition) {
5524 EmitBranch(instr, final_branch_condition);
5525 }
5526 }
5527
5528
5529 Condition LCodeGen::EmitTypeofIs(LTypeofIsAndBranch* instr, Register input) {
5530 Label* true_label = instr->TrueLabel(chunk_);
5531 Label* false_label = instr->FalseLabel(chunk_);
5532 Handle<String> type_name = instr->type_literal();
5533 int left_block = instr->TrueDestination(chunk_);
5534 int right_block = instr->FalseDestination(chunk_);
5535 int next_block = GetNextEmittedBlock();
5536
5537 Label::Distance true_distance = left_block == next_block ? Label::kNear
5538 : Label::kFar;
5539 Label::Distance false_distance = right_block == next_block ? Label::kNear
5540 : Label::kFar;
5541 Condition final_branch_condition = no_condition;
5542 Factory* factory = isolate()->factory();
5543 if (String::Equals(type_name, factory->number_string())) {
5544 __ JumpIfSmi(input, true_label, true_distance);
5545 __ CompareRoot(FieldOperand(input, HeapObject::kMapOffset),
5546 Heap::kHeapNumberMapRootIndex);
5547
5548 final_branch_condition = equal;
5549
5550 } else if (String::Equals(type_name, factory->string_string())) {
5551 __ JumpIfSmi(input, false_label, false_distance);
5552 __ CmpObjectType(input, FIRST_NONSTRING_TYPE, input);
5553 final_branch_condition = below;
5554
5555 } else if (String::Equals(type_name, factory->symbol_string())) {
5556 __ JumpIfSmi(input, false_label, false_distance);
5557 __ CmpObjectType(input, SYMBOL_TYPE, input);
5558 final_branch_condition = equal;
5559
5560 } else if (String::Equals(type_name, factory->boolean_string())) {
5561 __ CompareRoot(input, Heap::kTrueValueRootIndex);
5562 __ j(equal, true_label, true_distance);
5563 __ CompareRoot(input, Heap::kFalseValueRootIndex);
5564 final_branch_condition = equal;
5565
5566 } else if (String::Equals(type_name, factory->undefined_string())) {
5567 __ CompareRoot(input, Heap::kUndefinedValueRootIndex);
5568 __ j(equal, true_label, true_distance);
5569 __ JumpIfSmi(input, false_label, false_distance);
5570 // Check for undetectable objects => true.
5571 __ movp(input, FieldOperand(input, HeapObject::kMapOffset));
5572 __ testb(FieldOperand(input, Map::kBitFieldOffset),
5573 Immediate(1 << Map::kIsUndetectable));
5574 final_branch_condition = not_zero;
5575
5576 } else if (String::Equals(type_name, factory->function_string())) {
5577 __ JumpIfSmi(input, false_label, false_distance);
5578 // Check for callable and not undetectable objects => true.
5579 __ movp(input, FieldOperand(input, HeapObject::kMapOffset));
5580 __ movzxbl(input, FieldOperand(input, Map::kBitFieldOffset));
5581 __ andb(input,
5582 Immediate((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable)));
5583 __ cmpb(input, Immediate(1 << Map::kIsCallable));
5584 final_branch_condition = equal;
5585
5586 } else if (String::Equals(type_name, factory->object_string())) {
5587 __ JumpIfSmi(input, false_label, false_distance);
5588 __ CompareRoot(input, Heap::kNullValueRootIndex);
5589 __ j(equal, true_label, true_distance);
5590 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
5591 __ CmpObjectType(input, FIRST_SPEC_OBJECT_TYPE, input);
5592 __ j(below, false_label, false_distance);
5593 // Check for callable or undetectable objects => false.
5594 __ testb(FieldOperand(input, Map::kBitFieldOffset),
5595 Immediate((1 << Map::kIsCallable) | (1 << Map::kIsUndetectable)));
5596 final_branch_condition = zero;
5597
5598 // clang-format off
5599 #define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
5600 } else if (String::Equals(type_name, factory->type##_string())) { \
5601 __ JumpIfSmi(input, false_label, false_distance); \
5602 __ CompareRoot(FieldOperand(input, HeapObject::kMapOffset), \
5603 Heap::k##Type##MapRootIndex); \
5604 final_branch_condition = equal;
5605 SIMD128_TYPES(SIMD128_TYPE)
5606 #undef SIMD128_TYPE
5607 // clang-format on
5608
5609 } else {
5610 __ jmp(false_label, false_distance);
5611 }
5612
5613 return final_branch_condition;
5614 }
5615
5616
5617 void LCodeGen::DoIsConstructCallAndBranch(LIsConstructCallAndBranch* instr) {
5618 Register temp = ToRegister(instr->temp());
5619
5620 EmitIsConstructCall(temp);
5621 EmitBranch(instr, equal);
5622 }
5623
5624
5625 void LCodeGen::EmitIsConstructCall(Register temp) {
5626 // Get the frame pointer for the calling frame.
5627 __ movp(temp, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
5628
5629 // Skip the arguments adaptor frame if it exists.
5630 Label check_frame_marker;
5631 __ Cmp(Operand(temp, StandardFrameConstants::kContextOffset),
5632 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
5633 __ j(not_equal, &check_frame_marker, Label::kNear);
5634 __ movp(temp, Operand(temp, StandardFrameConstants::kCallerFPOffset));
5635
5636 // Check the marker in the calling frame.
5637 __ bind(&check_frame_marker);
5638 __ Cmp(Operand(temp, StandardFrameConstants::kMarkerOffset),
5639 Smi::FromInt(StackFrame::CONSTRUCT));
5640 }
5641
5642
5643 void LCodeGen::EnsureSpaceForLazyDeopt(int space_needed) {
5644 if (info()->ShouldEnsureSpaceForLazyDeopt()) {
5645 // Ensure that we have enough space after the previous lazy-bailout
5646 // instruction for patching the code here.
5647 int current_pc = masm()->pc_offset();
5648 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
5649 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
5650 __ Nop(padding_size);
5651 }
5652 }
5653 last_lazy_deopt_pc_ = masm()->pc_offset();
5654 }
5655
5656
5657 void LCodeGen::DoLazyBailout(LLazyBailout* instr) {
5658 last_lazy_deopt_pc_ = masm()->pc_offset();
5659 DCHECK(instr->HasEnvironment());
5660 LEnvironment* env = instr->environment();
5661 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5662 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5663 }
5664
5665
5666 void LCodeGen::DoDeoptimize(LDeoptimize* instr) {
5667 Deoptimizer::BailoutType type = instr->hydrogen()->type();
5668 // TODO(danno): Stubs expect all deopts to be lazy for historical reasons (the
5669 // needed return address), even though the implementation of LAZY and EAGER is
5670 // now identical. When LAZY is eventually completely folded into EAGER, remove
5671 // the special case below.
5672 if (info()->IsStub() && type == Deoptimizer::EAGER) {
5673 type = Deoptimizer::LAZY;
5674 }
5675 DeoptimizeIf(no_condition, instr, instr->hydrogen()->reason(), type);
5676 }
5677
5678
5679 void LCodeGen::DoDummy(LDummy* instr) {
5680 // Nothing to see here, move on!
5681 }
5682
5683
5684 void LCodeGen::DoDummyUse(LDummyUse* instr) {
5685 // Nothing to see here, move on!
5686 }
5687
5688
5689 void LCodeGen::DoDeferredStackCheck(LStackCheck* instr) {
5690 PushSafepointRegistersScope scope(this);
5691 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
5692 __ CallRuntimeSaveDoubles(Runtime::kStackGuard);
5693 RecordSafepointWithLazyDeopt(instr, RECORD_SAFEPOINT_WITH_REGISTERS, 0);
5694 DCHECK(instr->HasEnvironment());
5695 LEnvironment* env = instr->environment();
5696 safepoints_.RecordLazyDeoptimizationIndex(env->deoptimization_index());
5697 }
5698
5699
5700 void LCodeGen::DoStackCheck(LStackCheck* instr) {
5701 class DeferredStackCheck final : public LDeferredCode {
5702 public:
5703 DeferredStackCheck(LCodeGen* codegen, LStackCheck* instr)
5704 : LDeferredCode(codegen), instr_(instr) { }
5705 void Generate() override { codegen()->DoDeferredStackCheck(instr_); }
5706 LInstruction* instr() override { return instr_; }
5707
5708 private:
5709 LStackCheck* instr_;
5710 };
5711
5712 DCHECK(instr->HasEnvironment());
5713 LEnvironment* env = instr->environment();
5714 // There is no LLazyBailout instruction for stack-checks. We have to
5715 // prepare for lazy deoptimization explicitly here.
5716 if (instr->hydrogen()->is_function_entry()) {
5717 // Perform stack overflow check.
5718 Label done;
5719 __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
5720 __ j(above_equal, &done, Label::kNear);
5721
5722 DCHECK(instr->context()->IsRegister());
5723 DCHECK(ToRegister(instr->context()).is(rsi));
5724 CallCode(isolate()->builtins()->StackCheck(),
5725 RelocInfo::CODE_TARGET,
5726 instr);
5727 __ bind(&done);
5728 } else {
5729 DCHECK(instr->hydrogen()->is_backwards_branch());
5730 // Perform stack overflow check if this goto needs it before jumping.
5731 DeferredStackCheck* deferred_stack_check =
5732 new(zone()) DeferredStackCheck(this, instr);
5733 __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
5734 __ j(below, deferred_stack_check->entry());
5735 EnsureSpaceForLazyDeopt(Deoptimizer::patch_size());
5736 __ bind(instr->done_label());
5737 deferred_stack_check->SetExit(instr->done_label());
5738 RegisterEnvironmentForDeoptimization(env, Safepoint::kLazyDeopt);
5739 // Don't record a deoptimization index for the safepoint here.
5740 // This will be done explicitly when emitting call and the safepoint in
5741 // the deferred code.
5742 }
5743 }
5744
5745
5746 void LCodeGen::DoOsrEntry(LOsrEntry* instr) {
5747 // This is a pseudo-instruction that ensures that the environment here is
5748 // properly registered for deoptimization and records the assembler's PC
5749 // offset.
5750 LEnvironment* environment = instr->environment();
5751
5752 // If the environment were already registered, we would have no way of
5753 // backpatching it with the spill slot operands.
5754 DCHECK(!environment->HasBeenRegistered());
5755 RegisterEnvironmentForDeoptimization(environment, Safepoint::kNoLazyDeopt);
5756
5757 GenerateOsrPrologue();
5758 }
5759
5760
5761 void LCodeGen::DoForInPrepareMap(LForInPrepareMap* instr) {
5762 DCHECK(ToRegister(instr->context()).is(rsi));
5763
5764 Condition cc = masm()->CheckSmi(rax);
5765 DeoptimizeIf(cc, instr, Deoptimizer::kSmi);
5766
5767 STATIC_ASSERT(FIRST_JS_PROXY_TYPE == FIRST_SPEC_OBJECT_TYPE);
5768 __ CmpObjectType(rax, LAST_JS_PROXY_TYPE, rcx);
5769 DeoptimizeIf(below_equal, instr, Deoptimizer::kWrongInstanceType);
5770
5771 Label use_cache, call_runtime;
5772 Register null_value = rdi;
5773 __ LoadRoot(null_value, Heap::kNullValueRootIndex);
5774 __ CheckEnumCache(null_value, &call_runtime);
5775
5776 __ movp(rax, FieldOperand(rax, HeapObject::kMapOffset));
5777 __ jmp(&use_cache, Label::kNear);
5778
5779 // Get the set of properties to enumerate.
5780 __ bind(&call_runtime);
5781 __ Push(rax);
5782 CallRuntime(Runtime::kGetPropertyNamesFast, 1, instr);
5783
5784 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
5785 Heap::kMetaMapRootIndex);
5786 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongMap);
5787 __ bind(&use_cache);
5788 }
5789
5790
5791 void LCodeGen::DoForInCacheArray(LForInCacheArray* instr) {
5792 Register map = ToRegister(instr->map());
5793 Register result = ToRegister(instr->result());
5794 Label load_cache, done;
5795 __ EnumLength(result, map);
5796 __ Cmp(result, Smi::FromInt(0));
5797 __ j(not_equal, &load_cache, Label::kNear);
5798 __ LoadRoot(result, Heap::kEmptyFixedArrayRootIndex);
5799 __ jmp(&done, Label::kNear);
5800 __ bind(&load_cache);
5801 __ LoadInstanceDescriptors(map, result);
5802 __ movp(result,
5803 FieldOperand(result, DescriptorArray::kEnumCacheOffset));
5804 __ movp(result,
5805 FieldOperand(result, FixedArray::SizeFor(instr->idx())));
5806 __ bind(&done);
5807 Condition cc = masm()->CheckSmi(result);
5808 DeoptimizeIf(cc, instr, Deoptimizer::kNoCache);
5809 }
5810
5811
5812 void LCodeGen::DoCheckMapValue(LCheckMapValue* instr) {
5813 Register object = ToRegister(instr->value());
5814 __ cmpp(ToRegister(instr->map()),
5815 FieldOperand(object, HeapObject::kMapOffset));
5816 DeoptimizeIf(not_equal, instr, Deoptimizer::kWrongMap);
5817 }
5818
5819
5820 void LCodeGen::DoDeferredLoadMutableDouble(LLoadFieldByIndex* instr,
5821 Register object,
5822 Register index) {
5823 PushSafepointRegistersScope scope(this);
5824 __ Push(object);
5825 __ Push(index);
5826 __ xorp(rsi, rsi);
5827 __ CallRuntimeSaveDoubles(Runtime::kLoadMutableDouble);
5828 RecordSafepointWithRegisters(
5829 instr->pointer_map(), 2, Safepoint::kNoLazyDeopt);
5830 __ StoreToSafepointRegisterSlot(object, rax);
5831 }
5832
5833
5834 void LCodeGen::DoLoadFieldByIndex(LLoadFieldByIndex* instr) {
5835 class DeferredLoadMutableDouble final : public LDeferredCode {
5836 public:
5837 DeferredLoadMutableDouble(LCodeGen* codegen,
5838 LLoadFieldByIndex* instr,
5839 Register object,
5840 Register index)
5841 : LDeferredCode(codegen),
5842 instr_(instr),
5843 object_(object),
5844 index_(index) {
5845 }
5846 void Generate() override {
5847 codegen()->DoDeferredLoadMutableDouble(instr_, object_, index_);
5848 }
5849 LInstruction* instr() override { return instr_; }
5850
5851 private:
5852 LLoadFieldByIndex* instr_;
5853 Register object_;
5854 Register index_;
5855 };
5856
5857 Register object = ToRegister(instr->object());
5858 Register index = ToRegister(instr->index());
5859
5860 DeferredLoadMutableDouble* deferred;
5861 deferred = new(zone()) DeferredLoadMutableDouble(this, instr, object, index);
5862
5863 Label out_of_object, done;
5864 __ Move(kScratchRegister, Smi::FromInt(1));
5865 __ testp(index, kScratchRegister);
5866 __ j(not_zero, deferred->entry());
5867
5868 __ sarp(index, Immediate(1));
5869
5870 __ SmiToInteger32(index, index);
5871 __ cmpl(index, Immediate(0));
5872 __ j(less, &out_of_object, Label::kNear);
5873 __ movp(object, FieldOperand(object,
5874 index,
5875 times_pointer_size,
5876 JSObject::kHeaderSize));
5877 __ jmp(&done, Label::kNear);
5878
5879 __ bind(&out_of_object);
5880 __ movp(object, FieldOperand(object, JSObject::kPropertiesOffset));
5881 __ negl(index);
5882 // Index is now equal to out of object property index plus 1.
5883 __ movp(object, FieldOperand(object,
5884 index,
5885 times_pointer_size,
5886 FixedArray::kHeaderSize - kPointerSize));
5887 __ bind(deferred->exit());
5888 __ bind(&done);
5889 }
5890
5891
5892 void LCodeGen::DoStoreFrameContext(LStoreFrameContext* instr) {
5893 Register context = ToRegister(instr->context());
5894 __ movp(Operand(rbp, StandardFrameConstants::kContextOffset), context);
5895 }
5896
5897
5898 void LCodeGen::DoAllocateBlockContext(LAllocateBlockContext* instr) {
5899 Handle<ScopeInfo> scope_info = instr->scope_info();
5900 __ Push(scope_info);
5901 __ Push(ToRegister(instr->function()));
5902 CallRuntime(Runtime::kPushBlockContext, 2, instr);
5903 RecordSafepoint(Safepoint::kNoLazyDeopt);
5904 }
5905
5906
5907 #undef __
5908
5909 } // namespace internal
5910 } // namespace v8
5911
5912 #endif // V8_TARGET_ARCH_X64
OLDNEW
« no previous file with comments | « src/x64/lithium-codegen-x64.h ('k') | src/x64/lithium-gap-resolver-x64.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698