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

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

Powered by Google App Engine
This is Rietveld 408576698