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