Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / v8 / src / compiler / x64 / code-generator-x64.cc
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/compiler/code-generator.h"
6
7 #include "src/compiler/code-generator-impl.h"
8 #include "src/compiler/gap-resolver.h"
9 #include "src/compiler/node-matchers.h"
10 #include "src/compiler/node-properties-inl.h"
11 #include "src/scopes.h"
12 #include "src/x64/assembler-x64.h"
13 #include "src/x64/macro-assembler-x64.h"
14
15 namespace v8 {
16 namespace internal {
17 namespace compiler {
18
19 #define __ masm()->
20
21
22 // Adds X64 specific methods for decoding operands.
23 class X64OperandConverter : public InstructionOperandConverter {
24  public:
25   X64OperandConverter(CodeGenerator* gen, Instruction* instr)
26       : InstructionOperandConverter(gen, instr) {}
27
28   Immediate InputImmediate(int index) {
29     return ToImmediate(instr_->InputAt(index));
30   }
31
32   Operand InputOperand(int index) { return ToOperand(instr_->InputAt(index)); }
33
34   Operand OutputOperand() { return ToOperand(instr_->Output()); }
35
36   Immediate ToImmediate(InstructionOperand* operand) {
37     return Immediate(ToConstant(operand).ToInt32());
38   }
39
40   Operand ToOperand(InstructionOperand* op, int extra = 0) {
41     DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
42     // The linkage computes where all spill slots are located.
43     FrameOffset offset = linkage()->GetFrameOffset(op->index(), frame(), extra);
44     return Operand(offset.from_stack_pointer() ? rsp : rbp, offset.offset());
45   }
46
47   static int NextOffset(int* offset) {
48     int i = *offset;
49     (*offset)++;
50     return i;
51   }
52
53   static ScaleFactor ScaleFor(AddressingMode one, AddressingMode mode) {
54     STATIC_ASSERT(0 == static_cast<int>(times_1));
55     STATIC_ASSERT(1 == static_cast<int>(times_2));
56     STATIC_ASSERT(2 == static_cast<int>(times_4));
57     STATIC_ASSERT(3 == static_cast<int>(times_8));
58     int scale = static_cast<int>(mode - one);
59     DCHECK(scale >= 0 && scale < 4);
60     return static_cast<ScaleFactor>(scale);
61   }
62
63   Operand MemoryOperand(int* offset) {
64     AddressingMode mode = AddressingModeField::decode(instr_->opcode());
65     switch (mode) {
66       case kMode_MR: {
67         Register base = InputRegister(NextOffset(offset));
68         int32_t disp = 0;
69         return Operand(base, disp);
70       }
71       case kMode_MRI: {
72         Register base = InputRegister(NextOffset(offset));
73         int32_t disp = InputInt32(NextOffset(offset));
74         return Operand(base, disp);
75       }
76       case kMode_MR1:
77       case kMode_MR2:
78       case kMode_MR4:
79       case kMode_MR8: {
80         Register base = InputRegister(NextOffset(offset));
81         Register index = InputRegister(NextOffset(offset));
82         ScaleFactor scale = ScaleFor(kMode_MR1, mode);
83         int32_t disp = 0;
84         return Operand(base, index, scale, disp);
85       }
86       case kMode_MR1I:
87       case kMode_MR2I:
88       case kMode_MR4I:
89       case kMode_MR8I: {
90         Register base = InputRegister(NextOffset(offset));
91         Register index = InputRegister(NextOffset(offset));
92         ScaleFactor scale = ScaleFor(kMode_MR1I, mode);
93         int32_t disp = InputInt32(NextOffset(offset));
94         return Operand(base, index, scale, disp);
95       }
96       case kMode_M1:
97       case kMode_M2:
98       case kMode_M4:
99       case kMode_M8: {
100         Register index = InputRegister(NextOffset(offset));
101         ScaleFactor scale = ScaleFor(kMode_M1, mode);
102         int32_t disp = 0;
103         return Operand(index, scale, disp);
104       }
105       case kMode_M1I:
106       case kMode_M2I:
107       case kMode_M4I:
108       case kMode_M8I: {
109         Register index = InputRegister(NextOffset(offset));
110         ScaleFactor scale = ScaleFor(kMode_M1I, mode);
111         int32_t disp = InputInt32(NextOffset(offset));
112         return Operand(index, scale, disp);
113       }
114       case kMode_None:
115         UNREACHABLE();
116         return Operand(no_reg, 0);
117     }
118     UNREACHABLE();
119     return Operand(no_reg, 0);
120   }
121
122   Operand MemoryOperand() {
123     int first_input = 0;
124     return MemoryOperand(&first_input);
125   }
126 };
127
128
129 static bool HasImmediateInput(Instruction* instr, int index) {
130   return instr->InputAt(index)->IsImmediate();
131 }
132
133
134 #define ASSEMBLE_UNOP(asm_instr)         \
135   do {                                   \
136     if (instr->Output()->IsRegister()) { \
137       __ asm_instr(i.OutputRegister());  \
138     } else {                             \
139       __ asm_instr(i.OutputOperand());   \
140     }                                    \
141   } while (0)
142
143
144 #define ASSEMBLE_BINOP(asm_instr)                              \
145   do {                                                         \
146     if (HasImmediateInput(instr, 1)) {                         \
147       if (instr->InputAt(0)->IsRegister()) {                   \
148         __ asm_instr(i.InputRegister(0), i.InputImmediate(1)); \
149       } else {                                                 \
150         __ asm_instr(i.InputOperand(0), i.InputImmediate(1));  \
151       }                                                        \
152     } else {                                                   \
153       if (instr->InputAt(1)->IsRegister()) {                   \
154         __ asm_instr(i.InputRegister(0), i.InputRegister(1));  \
155       } else {                                                 \
156         __ asm_instr(i.InputRegister(0), i.InputOperand(1));   \
157       }                                                        \
158     }                                                          \
159   } while (0)
160
161
162 #define ASSEMBLE_MULT(asm_instr)                              \
163   do {                                                        \
164     if (HasImmediateInput(instr, 1)) {                        \
165       if (instr->InputAt(0)->IsRegister()) {                  \
166         __ asm_instr(i.OutputRegister(), i.InputRegister(0),  \
167                      i.InputImmediate(1));                    \
168       } else {                                                \
169         __ asm_instr(i.OutputRegister(), i.InputOperand(0),   \
170                      i.InputImmediate(1));                    \
171       }                                                       \
172     } else {                                                  \
173       if (instr->InputAt(1)->IsRegister()) {                  \
174         __ asm_instr(i.OutputRegister(), i.InputRegister(1)); \
175       } else {                                                \
176         __ asm_instr(i.OutputRegister(), i.InputOperand(1));  \
177       }                                                       \
178     }                                                         \
179   } while (0)
180
181
182 #define ASSEMBLE_SHIFT(asm_instr, width)                                   \
183   do {                                                                     \
184     if (HasImmediateInput(instr, 1)) {                                     \
185       if (instr->Output()->IsRegister()) {                                 \
186         __ asm_instr(i.OutputRegister(), Immediate(i.InputInt##width(1))); \
187       } else {                                                             \
188         __ asm_instr(i.OutputOperand(), Immediate(i.InputInt##width(1)));  \
189       }                                                                    \
190     } else {                                                               \
191       if (instr->Output()->IsRegister()) {                                 \
192         __ asm_instr##_cl(i.OutputRegister());                             \
193       } else {                                                             \
194         __ asm_instr##_cl(i.OutputOperand());                              \
195       }                                                                    \
196     }                                                                      \
197   } while (0)
198
199
200 #define ASSEMBLE_DOUBLE_BINOP(asm_instr)                                \
201   do {                                                                  \
202     if (instr->InputAt(1)->IsDoubleRegister()) {                        \
203       __ asm_instr(i.InputDoubleRegister(0), i.InputDoubleRegister(1)); \
204     } else {                                                            \
205       __ asm_instr(i.InputDoubleRegister(0), i.InputOperand(1));        \
206     }                                                                   \
207   } while (0)
208
209
210 // Assembles an instruction after register allocation, producing machine code.
211 void CodeGenerator::AssembleArchInstruction(Instruction* instr) {
212   X64OperandConverter i(this, instr);
213
214   switch (ArchOpcodeField::decode(instr->opcode())) {
215     case kArchCallCodeObject: {
216       EnsureSpaceForLazyDeopt();
217       if (HasImmediateInput(instr, 0)) {
218         Handle<Code> code = Handle<Code>::cast(i.InputHeapObject(0));
219         __ Call(code, RelocInfo::CODE_TARGET);
220       } else {
221         Register reg = i.InputRegister(0);
222         int entry = Code::kHeaderSize - kHeapObjectTag;
223         __ Call(Operand(reg, entry));
224       }
225       AddSafepointAndDeopt(instr);
226       break;
227     }
228     case kArchCallJSFunction: {
229       EnsureSpaceForLazyDeopt();
230       Register func = i.InputRegister(0);
231       if (FLAG_debug_code) {
232         // Check the function's context matches the context argument.
233         __ cmpp(rsi, FieldOperand(func, JSFunction::kContextOffset));
234         __ Assert(equal, kWrongFunctionContext);
235       }
236       __ Call(FieldOperand(func, JSFunction::kCodeEntryOffset));
237       AddSafepointAndDeopt(instr);
238       break;
239     }
240     case kArchJmp:
241       __ jmp(code_->GetLabel(i.InputRpo(0)));
242       break;
243     case kArchNop:
244       // don't emit code for nops.
245       break;
246     case kArchRet:
247       AssembleReturn();
248       break;
249     case kArchStackPointer:
250       __ movq(i.OutputRegister(), rsp);
251       break;
252     case kArchTruncateDoubleToI:
253       __ TruncateDoubleToI(i.OutputRegister(), i.InputDoubleRegister(0));
254       break;
255     case kX64Add32:
256       ASSEMBLE_BINOP(addl);
257       break;
258     case kX64Add:
259       ASSEMBLE_BINOP(addq);
260       break;
261     case kX64Sub32:
262       ASSEMBLE_BINOP(subl);
263       break;
264     case kX64Sub:
265       ASSEMBLE_BINOP(subq);
266       break;
267     case kX64And32:
268       ASSEMBLE_BINOP(andl);
269       break;
270     case kX64And:
271       ASSEMBLE_BINOP(andq);
272       break;
273     case kX64Cmp32:
274       ASSEMBLE_BINOP(cmpl);
275       break;
276     case kX64Cmp:
277       ASSEMBLE_BINOP(cmpq);
278       break;
279     case kX64Test32:
280       ASSEMBLE_BINOP(testl);
281       break;
282     case kX64Test:
283       ASSEMBLE_BINOP(testq);
284       break;
285     case kX64Imul32:
286       ASSEMBLE_MULT(imull);
287       break;
288     case kX64Imul:
289       ASSEMBLE_MULT(imulq);
290       break;
291     case kX64ImulHigh32:
292       if (instr->InputAt(1)->IsRegister()) {
293         __ imull(i.InputRegister(1));
294       } else {
295         __ imull(i.InputOperand(1));
296       }
297       break;
298     case kX64UmulHigh32:
299       if (instr->InputAt(1)->IsRegister()) {
300         __ mull(i.InputRegister(1));
301       } else {
302         __ mull(i.InputOperand(1));
303       }
304       break;
305     case kX64Idiv32:
306       __ cdq();
307       __ idivl(i.InputRegister(1));
308       break;
309     case kX64Idiv:
310       __ cqo();
311       __ idivq(i.InputRegister(1));
312       break;
313     case kX64Udiv32:
314       __ xorl(rdx, rdx);
315       __ divl(i.InputRegister(1));
316       break;
317     case kX64Udiv:
318       __ xorq(rdx, rdx);
319       __ divq(i.InputRegister(1));
320       break;
321     case kX64Not:
322       ASSEMBLE_UNOP(notq);
323       break;
324     case kX64Not32:
325       ASSEMBLE_UNOP(notl);
326       break;
327     case kX64Neg:
328       ASSEMBLE_UNOP(negq);
329       break;
330     case kX64Neg32:
331       ASSEMBLE_UNOP(negl);
332       break;
333     case kX64Or32:
334       ASSEMBLE_BINOP(orl);
335       break;
336     case kX64Or:
337       ASSEMBLE_BINOP(orq);
338       break;
339     case kX64Xor32:
340       ASSEMBLE_BINOP(xorl);
341       break;
342     case kX64Xor:
343       ASSEMBLE_BINOP(xorq);
344       break;
345     case kX64Shl32:
346       ASSEMBLE_SHIFT(shll, 5);
347       break;
348     case kX64Shl:
349       ASSEMBLE_SHIFT(shlq, 6);
350       break;
351     case kX64Shr32:
352       ASSEMBLE_SHIFT(shrl, 5);
353       break;
354     case kX64Shr:
355       ASSEMBLE_SHIFT(shrq, 6);
356       break;
357     case kX64Sar32:
358       ASSEMBLE_SHIFT(sarl, 5);
359       break;
360     case kX64Sar:
361       ASSEMBLE_SHIFT(sarq, 6);
362       break;
363     case kX64Ror32:
364       ASSEMBLE_SHIFT(rorl, 5);
365       break;
366     case kX64Ror:
367       ASSEMBLE_SHIFT(rorq, 6);
368       break;
369     case kSSEFloat64Cmp:
370       ASSEMBLE_DOUBLE_BINOP(ucomisd);
371       break;
372     case kSSEFloat64Add:
373       ASSEMBLE_DOUBLE_BINOP(addsd);
374       break;
375     case kSSEFloat64Sub:
376       ASSEMBLE_DOUBLE_BINOP(subsd);
377       break;
378     case kSSEFloat64Mul:
379       ASSEMBLE_DOUBLE_BINOP(mulsd);
380       break;
381     case kSSEFloat64Div:
382       ASSEMBLE_DOUBLE_BINOP(divsd);
383       break;
384     case kSSEFloat64Mod: {
385       __ subq(rsp, Immediate(kDoubleSize));
386       // Move values to st(0) and st(1).
387       __ movsd(Operand(rsp, 0), i.InputDoubleRegister(1));
388       __ fld_d(Operand(rsp, 0));
389       __ movsd(Operand(rsp, 0), i.InputDoubleRegister(0));
390       __ fld_d(Operand(rsp, 0));
391       // Loop while fprem isn't done.
392       Label mod_loop;
393       __ bind(&mod_loop);
394       // This instructions traps on all kinds inputs, but we are assuming the
395       // floating point control word is set to ignore them all.
396       __ fprem();
397       // The following 2 instruction implicitly use rax.
398       __ fnstsw_ax();
399       if (CpuFeatures::IsSupported(SAHF) && masm()->IsEnabled(SAHF)) {
400         __ sahf();
401       } else {
402         __ shrl(rax, Immediate(8));
403         __ andl(rax, Immediate(0xFF));
404         __ pushq(rax);
405         __ popfq();
406       }
407       __ j(parity_even, &mod_loop);
408       // Move output to stack and clean up.
409       __ fstp(1);
410       __ fstp_d(Operand(rsp, 0));
411       __ movsd(i.OutputDoubleRegister(), Operand(rsp, 0));
412       __ addq(rsp, Immediate(kDoubleSize));
413       break;
414     }
415     case kSSEFloat64Sqrt:
416       if (instr->InputAt(0)->IsDoubleRegister()) {
417         __ sqrtsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
418       } else {
419         __ sqrtsd(i.OutputDoubleRegister(), i.InputOperand(0));
420       }
421       break;
422     case kSSEFloat64Floor: {
423       CpuFeatureScope sse_scope(masm(), SSE4_1);
424       __ roundsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
425                  v8::internal::Assembler::kRoundDown);
426       break;
427     }
428     case kSSEFloat64Ceil: {
429       CpuFeatureScope sse_scope(masm(), SSE4_1);
430       __ roundsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
431                  v8::internal::Assembler::kRoundUp);
432       break;
433     }
434     case kSSEFloat64RoundTruncate: {
435       CpuFeatureScope sse_scope(masm(), SSE4_1);
436       __ roundsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
437                  v8::internal::Assembler::kRoundToZero);
438       break;
439     }
440     case kSSECvtss2sd:
441       if (instr->InputAt(0)->IsDoubleRegister()) {
442         __ cvtss2sd(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
443       } else {
444         __ cvtss2sd(i.OutputDoubleRegister(), i.InputOperand(0));
445       }
446       break;
447     case kSSECvtsd2ss:
448       if (instr->InputAt(0)->IsDoubleRegister()) {
449         __ cvtsd2ss(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
450       } else {
451         __ cvtsd2ss(i.OutputDoubleRegister(), i.InputOperand(0));
452       }
453       break;
454     case kSSEFloat64ToInt32:
455       if (instr->InputAt(0)->IsDoubleRegister()) {
456         __ cvttsd2si(i.OutputRegister(), i.InputDoubleRegister(0));
457       } else {
458         __ cvttsd2si(i.OutputRegister(), i.InputOperand(0));
459       }
460       break;
461     case kSSEFloat64ToUint32: {
462       if (instr->InputAt(0)->IsDoubleRegister()) {
463         __ cvttsd2siq(i.OutputRegister(), i.InputDoubleRegister(0));
464       } else {
465         __ cvttsd2siq(i.OutputRegister(), i.InputOperand(0));
466       }
467       __ AssertZeroExtended(i.OutputRegister());
468       break;
469     }
470     case kSSEInt32ToFloat64:
471       if (instr->InputAt(0)->IsRegister()) {
472         __ cvtlsi2sd(i.OutputDoubleRegister(), i.InputRegister(0));
473       } else {
474         __ cvtlsi2sd(i.OutputDoubleRegister(), i.InputOperand(0));
475       }
476       break;
477     case kSSEUint32ToFloat64:
478       if (instr->InputAt(0)->IsRegister()) {
479         __ movl(kScratchRegister, i.InputRegister(0));
480       } else {
481         __ movl(kScratchRegister, i.InputOperand(0));
482       }
483       __ cvtqsi2sd(i.OutputDoubleRegister(), kScratchRegister);
484       break;
485     case kX64Movsxbl:
486       __ movsxbl(i.OutputRegister(), i.MemoryOperand());
487       break;
488     case kX64Movzxbl:
489       __ movzxbl(i.OutputRegister(), i.MemoryOperand());
490       break;
491     case kX64Movb: {
492       int index = 0;
493       Operand operand = i.MemoryOperand(&index);
494       if (HasImmediateInput(instr, index)) {
495         __ movb(operand, Immediate(i.InputInt8(index)));
496       } else {
497         __ movb(operand, i.InputRegister(index));
498       }
499       break;
500     }
501     case kX64Movsxwl:
502       __ movsxwl(i.OutputRegister(), i.MemoryOperand());
503       break;
504     case kX64Movzxwl:
505       __ movzxwl(i.OutputRegister(), i.MemoryOperand());
506       break;
507     case kX64Movw: {
508       int index = 0;
509       Operand operand = i.MemoryOperand(&index);
510       if (HasImmediateInput(instr, index)) {
511         __ movw(operand, Immediate(i.InputInt16(index)));
512       } else {
513         __ movw(operand, i.InputRegister(index));
514       }
515       break;
516     }
517     case kX64Movl:
518       if (instr->HasOutput()) {
519         if (instr->addressing_mode() == kMode_None) {
520           if (instr->InputAt(0)->IsRegister()) {
521             __ movl(i.OutputRegister(), i.InputRegister(0));
522           } else {
523             __ movl(i.OutputRegister(), i.InputOperand(0));
524           }
525         } else {
526           __ movl(i.OutputRegister(), i.MemoryOperand());
527         }
528       } else {
529         int index = 0;
530         Operand operand = i.MemoryOperand(&index);
531         if (HasImmediateInput(instr, index)) {
532           __ movl(operand, i.InputImmediate(index));
533         } else {
534           __ movl(operand, i.InputRegister(index));
535         }
536       }
537       break;
538     case kX64Movsxlq: {
539       if (instr->InputAt(0)->IsRegister()) {
540         __ movsxlq(i.OutputRegister(), i.InputRegister(0));
541       } else {
542         __ movsxlq(i.OutputRegister(), i.InputOperand(0));
543       }
544       break;
545     }
546     case kX64Movq:
547       if (instr->HasOutput()) {
548         __ movq(i.OutputRegister(), i.MemoryOperand());
549       } else {
550         int index = 0;
551         Operand operand = i.MemoryOperand(&index);
552         if (HasImmediateInput(instr, index)) {
553           __ movq(operand, i.InputImmediate(index));
554         } else {
555           __ movq(operand, i.InputRegister(index));
556         }
557       }
558       break;
559     case kX64Movss:
560       if (instr->HasOutput()) {
561         __ movss(i.OutputDoubleRegister(), i.MemoryOperand());
562       } else {
563         int index = 0;
564         Operand operand = i.MemoryOperand(&index);
565         __ movss(operand, i.InputDoubleRegister(index));
566       }
567       break;
568     case kX64Movsd:
569       if (instr->HasOutput()) {
570         __ movsd(i.OutputDoubleRegister(), i.MemoryOperand());
571       } else {
572         int index = 0;
573         Operand operand = i.MemoryOperand(&index);
574         __ movsd(operand, i.InputDoubleRegister(index));
575       }
576       break;
577     case kX64Lea32:
578       __ leal(i.OutputRegister(), i.MemoryOperand());
579       break;
580     case kX64Lea:
581       __ leaq(i.OutputRegister(), i.MemoryOperand());
582       break;
583     case kX64Push:
584       if (HasImmediateInput(instr, 0)) {
585         __ pushq(i.InputImmediate(0));
586       } else {
587         if (instr->InputAt(0)->IsRegister()) {
588           __ pushq(i.InputRegister(0));
589         } else {
590           __ pushq(i.InputOperand(0));
591         }
592       }
593       break;
594     case kX64StoreWriteBarrier: {
595       Register object = i.InputRegister(0);
596       Register index = i.InputRegister(1);
597       Register value = i.InputRegister(2);
598       __ movsxlq(index, index);
599       __ movq(Operand(object, index, times_1, 0), value);
600       __ leaq(index, Operand(object, index, times_1, 0));
601       SaveFPRegsMode mode =
602           frame()->DidAllocateDoubleRegisters() ? kSaveFPRegs : kDontSaveFPRegs;
603       __ RecordWrite(object, index, value, mode);
604       break;
605     }
606   }
607 }
608
609
610 // Assembles branches after this instruction.
611 void CodeGenerator::AssembleArchBranch(Instruction* instr,
612                                        FlagsCondition condition) {
613   X64OperandConverter i(this, instr);
614   Label done;
615
616   // Emit a branch. The true and false targets are always the last two inputs
617   // to the instruction.
618   BasicBlock::RpoNumber tblock =
619       i.InputRpo(static_cast<int>(instr->InputCount()) - 2);
620   BasicBlock::RpoNumber fblock =
621       i.InputRpo(static_cast<int>(instr->InputCount()) - 1);
622   bool fallthru = IsNextInAssemblyOrder(fblock);
623   Label* tlabel = code()->GetLabel(tblock);
624   Label* flabel = fallthru ? &done : code()->GetLabel(fblock);
625   Label::Distance flabel_distance = fallthru ? Label::kNear : Label::kFar;
626   switch (condition) {
627     case kUnorderedEqual:
628       __ j(parity_even, flabel, flabel_distance);
629     // Fall through.
630     case kEqual:
631       __ j(equal, tlabel);
632       break;
633     case kUnorderedNotEqual:
634       __ j(parity_even, tlabel);
635     // Fall through.
636     case kNotEqual:
637       __ j(not_equal, tlabel);
638       break;
639     case kSignedLessThan:
640       __ j(less, tlabel);
641       break;
642     case kSignedGreaterThanOrEqual:
643       __ j(greater_equal, tlabel);
644       break;
645     case kSignedLessThanOrEqual:
646       __ j(less_equal, tlabel);
647       break;
648     case kSignedGreaterThan:
649       __ j(greater, tlabel);
650       break;
651     case kUnorderedLessThan:
652       __ j(parity_even, flabel, flabel_distance);
653     // Fall through.
654     case kUnsignedLessThan:
655       __ j(below, tlabel);
656       break;
657     case kUnorderedGreaterThanOrEqual:
658       __ j(parity_even, tlabel);
659     // Fall through.
660     case kUnsignedGreaterThanOrEqual:
661       __ j(above_equal, tlabel);
662       break;
663     case kUnorderedLessThanOrEqual:
664       __ j(parity_even, flabel, flabel_distance);
665     // Fall through.
666     case kUnsignedLessThanOrEqual:
667       __ j(below_equal, tlabel);
668       break;
669     case kUnorderedGreaterThan:
670       __ j(parity_even, tlabel);
671     // Fall through.
672     case kUnsignedGreaterThan:
673       __ j(above, tlabel);
674       break;
675     case kOverflow:
676       __ j(overflow, tlabel);
677       break;
678     case kNotOverflow:
679       __ j(no_overflow, tlabel);
680       break;
681   }
682   if (!fallthru) __ jmp(flabel, flabel_distance);  // no fallthru to flabel.
683   __ bind(&done);
684 }
685
686
687 // Assembles boolean materializations after this instruction.
688 void CodeGenerator::AssembleArchBoolean(Instruction* instr,
689                                         FlagsCondition condition) {
690   X64OperandConverter i(this, instr);
691   Label done;
692
693   // Materialize a full 64-bit 1 or 0 value. The result register is always the
694   // last output of the instruction.
695   Label check;
696   DCHECK_NE(0, static_cast<int>(instr->OutputCount()));
697   Register reg = i.OutputRegister(static_cast<int>(instr->OutputCount() - 1));
698   Condition cc = no_condition;
699   switch (condition) {
700     case kUnorderedEqual:
701       __ j(parity_odd, &check, Label::kNear);
702       __ movl(reg, Immediate(0));
703       __ jmp(&done, Label::kNear);
704     // Fall through.
705     case kEqual:
706       cc = equal;
707       break;
708     case kUnorderedNotEqual:
709       __ j(parity_odd, &check, Label::kNear);
710       __ movl(reg, Immediate(1));
711       __ jmp(&done, Label::kNear);
712     // Fall through.
713     case kNotEqual:
714       cc = not_equal;
715       break;
716     case kSignedLessThan:
717       cc = less;
718       break;
719     case kSignedGreaterThanOrEqual:
720       cc = greater_equal;
721       break;
722     case kSignedLessThanOrEqual:
723       cc = less_equal;
724       break;
725     case kSignedGreaterThan:
726       cc = greater;
727       break;
728     case kUnorderedLessThan:
729       __ j(parity_odd, &check, Label::kNear);
730       __ movl(reg, Immediate(0));
731       __ jmp(&done, Label::kNear);
732     // Fall through.
733     case kUnsignedLessThan:
734       cc = below;
735       break;
736     case kUnorderedGreaterThanOrEqual:
737       __ j(parity_odd, &check, Label::kNear);
738       __ movl(reg, Immediate(1));
739       __ jmp(&done, Label::kNear);
740     // Fall through.
741     case kUnsignedGreaterThanOrEqual:
742       cc = above_equal;
743       break;
744     case kUnorderedLessThanOrEqual:
745       __ j(parity_odd, &check, Label::kNear);
746       __ movl(reg, Immediate(0));
747       __ jmp(&done, Label::kNear);
748     // Fall through.
749     case kUnsignedLessThanOrEqual:
750       cc = below_equal;
751       break;
752     case kUnorderedGreaterThan:
753       __ j(parity_odd, &check, Label::kNear);
754       __ movl(reg, Immediate(1));
755       __ jmp(&done, Label::kNear);
756     // Fall through.
757     case kUnsignedGreaterThan:
758       cc = above;
759       break;
760     case kOverflow:
761       cc = overflow;
762       break;
763     case kNotOverflow:
764       cc = no_overflow;
765       break;
766   }
767   __ bind(&check);
768   __ setcc(cc, reg);
769   __ movzxbl(reg, reg);
770   __ bind(&done);
771 }
772
773
774 void CodeGenerator::AssembleDeoptimizerCall(int deoptimization_id) {
775   Address deopt_entry = Deoptimizer::GetDeoptimizationEntry(
776       isolate(), deoptimization_id, Deoptimizer::LAZY);
777   __ call(deopt_entry, RelocInfo::RUNTIME_ENTRY);
778 }
779
780
781 void CodeGenerator::AssemblePrologue() {
782   CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
783   int stack_slots = frame()->GetSpillSlotCount();
784   if (descriptor->kind() == CallDescriptor::kCallAddress) {
785     __ pushq(rbp);
786     __ movq(rbp, rsp);
787     const RegList saves = descriptor->CalleeSavedRegisters();
788     if (saves != 0) {  // Save callee-saved registers.
789       int register_save_area_size = 0;
790       for (int i = Register::kNumRegisters - 1; i >= 0; i--) {
791         if (!((1 << i) & saves)) continue;
792         __ pushq(Register::from_code(i));
793         register_save_area_size += kPointerSize;
794       }
795       frame()->SetRegisterSaveAreaSize(register_save_area_size);
796     }
797   } else if (descriptor->IsJSFunctionCall()) {
798     CompilationInfo* info = this->info();
799     __ Prologue(info->IsCodePreAgingActive());
800     frame()->SetRegisterSaveAreaSize(
801         StandardFrameConstants::kFixedFrameSizeFromFp);
802
803     // Sloppy mode functions and builtins need to replace the receiver with the
804     // global proxy when called as functions (without an explicit receiver
805     // object).
806     // TODO(mstarzinger/verwaest): Should this be moved back into the CallIC?
807     if (info->strict_mode() == SLOPPY && !info->is_native()) {
808       Label ok;
809       StackArgumentsAccessor args(rbp, info->scope()->num_parameters());
810       __ movp(rcx, args.GetReceiverOperand());
811       __ CompareRoot(rcx, Heap::kUndefinedValueRootIndex);
812       __ j(not_equal, &ok, Label::kNear);
813       __ movp(rcx, GlobalObjectOperand());
814       __ movp(rcx, FieldOperand(rcx, GlobalObject::kGlobalProxyOffset));
815       __ movp(args.GetReceiverOperand(), rcx);
816       __ bind(&ok);
817     }
818
819   } else {
820     __ StubPrologue();
821     frame()->SetRegisterSaveAreaSize(
822         StandardFrameConstants::kFixedFrameSizeFromFp);
823   }
824   if (stack_slots > 0) {
825     __ subq(rsp, Immediate(stack_slots * kPointerSize));
826   }
827 }
828
829
830 void CodeGenerator::AssembleReturn() {
831   CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
832   if (descriptor->kind() == CallDescriptor::kCallAddress) {
833     if (frame()->GetRegisterSaveAreaSize() > 0) {
834       // Remove this frame's spill slots first.
835       int stack_slots = frame()->GetSpillSlotCount();
836       if (stack_slots > 0) {
837         __ addq(rsp, Immediate(stack_slots * kPointerSize));
838       }
839       const RegList saves = descriptor->CalleeSavedRegisters();
840       // Restore registers.
841       if (saves != 0) {
842         for (int i = 0; i < Register::kNumRegisters; i++) {
843           if (!((1 << i) & saves)) continue;
844           __ popq(Register::from_code(i));
845         }
846       }
847       __ popq(rbp);  // Pop caller's frame pointer.
848       __ ret(0);
849     } else {
850       // No saved registers.
851       __ movq(rsp, rbp);  // Move stack pointer back to frame pointer.
852       __ popq(rbp);       // Pop caller's frame pointer.
853       __ ret(0);
854     }
855   } else {
856     __ movq(rsp, rbp);  // Move stack pointer back to frame pointer.
857     __ popq(rbp);       // Pop caller's frame pointer.
858     int pop_count = descriptor->IsJSFunctionCall()
859                         ? static_cast<int>(descriptor->JSParameterCount())
860                         : 0;
861     __ ret(pop_count * kPointerSize);
862   }
863 }
864
865
866 void CodeGenerator::AssembleMove(InstructionOperand* source,
867                                  InstructionOperand* destination) {
868   X64OperandConverter g(this, NULL);
869   // Dispatch on the source and destination operand kinds.  Not all
870   // combinations are possible.
871   if (source->IsRegister()) {
872     DCHECK(destination->IsRegister() || destination->IsStackSlot());
873     Register src = g.ToRegister(source);
874     if (destination->IsRegister()) {
875       __ movq(g.ToRegister(destination), src);
876     } else {
877       __ movq(g.ToOperand(destination), src);
878     }
879   } else if (source->IsStackSlot()) {
880     DCHECK(destination->IsRegister() || destination->IsStackSlot());
881     Operand src = g.ToOperand(source);
882     if (destination->IsRegister()) {
883       Register dst = g.ToRegister(destination);
884       __ movq(dst, src);
885     } else {
886       // Spill on demand to use a temporary register for memory-to-memory
887       // moves.
888       Register tmp = kScratchRegister;
889       Operand dst = g.ToOperand(destination);
890       __ movq(tmp, src);
891       __ movq(dst, tmp);
892     }
893   } else if (source->IsConstant()) {
894     ConstantOperand* constant_source = ConstantOperand::cast(source);
895     Constant src = g.ToConstant(constant_source);
896     if (destination->IsRegister() || destination->IsStackSlot()) {
897       Register dst = destination->IsRegister() ? g.ToRegister(destination)
898                                                : kScratchRegister;
899       switch (src.type()) {
900         case Constant::kInt32:
901           // TODO(dcarney): don't need scratch in this case.
902           __ Set(dst, src.ToInt32());
903           break;
904         case Constant::kInt64:
905           __ Set(dst, src.ToInt64());
906           break;
907         case Constant::kFloat32:
908           __ Move(dst,
909                   isolate()->factory()->NewNumber(src.ToFloat32(), TENURED));
910           break;
911         case Constant::kFloat64:
912           __ Move(dst,
913                   isolate()->factory()->NewNumber(src.ToFloat64(), TENURED));
914           break;
915         case Constant::kExternalReference:
916           __ Move(dst, src.ToExternalReference());
917           break;
918         case Constant::kHeapObject:
919           __ Move(dst, src.ToHeapObject());
920           break;
921       }
922       if (destination->IsStackSlot()) {
923         __ movq(g.ToOperand(destination), kScratchRegister);
924       }
925     } else if (src.type() == Constant::kFloat32) {
926       // TODO(turbofan): Can we do better here?
927       uint32_t src_const = bit_cast<uint32_t>(src.ToFloat32());
928       if (destination->IsDoubleRegister()) {
929         __ Move(g.ToDoubleRegister(destination), src_const);
930       } else {
931         DCHECK(destination->IsDoubleStackSlot());
932         Operand dst = g.ToOperand(destination);
933         __ movl(dst, Immediate(src_const));
934       }
935     } else {
936       DCHECK_EQ(Constant::kFloat64, src.type());
937       uint64_t src_const = bit_cast<uint64_t>(src.ToFloat64());
938       if (destination->IsDoubleRegister()) {
939         __ Move(g.ToDoubleRegister(destination), src_const);
940       } else {
941         DCHECK(destination->IsDoubleStackSlot());
942         __ movq(kScratchRegister, src_const);
943         __ movq(g.ToOperand(destination), kScratchRegister);
944       }
945     }
946   } else if (source->IsDoubleRegister()) {
947     XMMRegister src = g.ToDoubleRegister(source);
948     if (destination->IsDoubleRegister()) {
949       XMMRegister dst = g.ToDoubleRegister(destination);
950       __ movsd(dst, src);
951     } else {
952       DCHECK(destination->IsDoubleStackSlot());
953       Operand dst = g.ToOperand(destination);
954       __ movsd(dst, src);
955     }
956   } else if (source->IsDoubleStackSlot()) {
957     DCHECK(destination->IsDoubleRegister() || destination->IsDoubleStackSlot());
958     Operand src = g.ToOperand(source);
959     if (destination->IsDoubleRegister()) {
960       XMMRegister dst = g.ToDoubleRegister(destination);
961       __ movsd(dst, src);
962     } else {
963       // We rely on having xmm0 available as a fixed scratch register.
964       Operand dst = g.ToOperand(destination);
965       __ movsd(xmm0, src);
966       __ movsd(dst, xmm0);
967     }
968   } else {
969     UNREACHABLE();
970   }
971 }
972
973
974 void CodeGenerator::AssembleSwap(InstructionOperand* source,
975                                  InstructionOperand* destination) {
976   X64OperandConverter g(this, NULL);
977   // Dispatch on the source and destination operand kinds.  Not all
978   // combinations are possible.
979   if (source->IsRegister() && destination->IsRegister()) {
980     // Register-register.
981     __ xchgq(g.ToRegister(source), g.ToRegister(destination));
982   } else if (source->IsRegister() && destination->IsStackSlot()) {
983     Register src = g.ToRegister(source);
984     Operand dst = g.ToOperand(destination);
985     __ xchgq(src, dst);
986   } else if ((source->IsStackSlot() && destination->IsStackSlot()) ||
987              (source->IsDoubleStackSlot() &&
988               destination->IsDoubleStackSlot())) {
989     // Memory-memory.
990     Register tmp = kScratchRegister;
991     Operand src = g.ToOperand(source);
992     Operand dst = g.ToOperand(destination);
993     __ movq(tmp, dst);
994     __ xchgq(tmp, src);
995     __ movq(dst, tmp);
996   } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
997     // XMM register-register swap. We rely on having xmm0
998     // available as a fixed scratch register.
999     XMMRegister src = g.ToDoubleRegister(source);
1000     XMMRegister dst = g.ToDoubleRegister(destination);
1001     __ movsd(xmm0, src);
1002     __ movsd(src, dst);
1003     __ movsd(dst, xmm0);
1004   } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
1005     // XMM register-memory swap.  We rely on having xmm0
1006     // available as a fixed scratch register.
1007     XMMRegister src = g.ToDoubleRegister(source);
1008     Operand dst = g.ToOperand(destination);
1009     __ movsd(xmm0, src);
1010     __ movsd(src, dst);
1011     __ movsd(dst, xmm0);
1012   } else {
1013     // No other combinations are possible.
1014     UNREACHABLE();
1015   }
1016 }
1017
1018
1019 void CodeGenerator::AddNopForSmiCodeInlining() { __ nop(); }
1020
1021
1022 void CodeGenerator::EnsureSpaceForLazyDeopt() {
1023   int space_needed = Deoptimizer::patch_size();
1024   if (!info()->IsStub()) {
1025     // Ensure that we have enough space after the previous lazy-bailout
1026     // instruction for patching the code here.
1027     int current_pc = masm()->pc_offset();
1028     if (current_pc < last_lazy_deopt_pc_ + space_needed) {
1029       int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
1030       __ Nop(padding_size);
1031     }
1032   }
1033   MarkLazyDeoptSite();
1034 }
1035
1036 #undef __
1037
1038 }  // namespace internal
1039 }  // namespace compiler
1040 }  // namespace v8