deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / compiler / ia32 / code-generator-ia32.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/ia32/assembler-ia32.h"
11 #include "src/ia32/macro-assembler-ia32.h"
12 #include "src/scopes.h"
13
14 namespace v8 {
15 namespace internal {
16 namespace compiler {
17
18 #define __ masm()->
19
20
21 // Adds IA-32 specific methods for decoding operands.
22 class IA32OperandConverter : public InstructionOperandConverter {
23  public:
24   IA32OperandConverter(CodeGenerator* gen, Instruction* instr)
25       : InstructionOperandConverter(gen, instr) {}
26
27   Operand InputOperand(size_t index, int extra = 0) {
28     return ToOperand(instr_->InputAt(index), extra);
29   }
30
31   Immediate InputImmediate(size_t index) {
32     return ToImmediate(instr_->InputAt(index));
33   }
34
35   Operand OutputOperand() { return ToOperand(instr_->Output()); }
36
37   Operand ToOperand(InstructionOperand* op, int extra = 0) {
38     if (op->IsRegister()) {
39       DCHECK(extra == 0);
40       return Operand(ToRegister(op));
41     } else if (op->IsDoubleRegister()) {
42       DCHECK(extra == 0);
43       return Operand(ToDoubleRegister(op));
44     }
45     DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
46     // The linkage computes where all spill slots are located.
47     FrameOffset offset = linkage()->GetFrameOffset(op->index(), frame(), extra);
48     return Operand(offset.from_stack_pointer() ? esp : ebp, offset.offset());
49   }
50
51   Operand HighOperand(InstructionOperand* op) {
52     DCHECK(op->IsDoubleStackSlot());
53     return ToOperand(op, kPointerSize);
54   }
55
56   Immediate ToImmediate(InstructionOperand* operand) {
57     Constant constant = ToConstant(operand);
58     switch (constant.type()) {
59       case Constant::kInt32:
60         return Immediate(constant.ToInt32());
61       case Constant::kFloat32:
62         return Immediate(
63             isolate()->factory()->NewNumber(constant.ToFloat32(), TENURED));
64       case Constant::kFloat64:
65         return Immediate(
66             isolate()->factory()->NewNumber(constant.ToFloat64(), TENURED));
67       case Constant::kExternalReference:
68         return Immediate(constant.ToExternalReference());
69       case Constant::kHeapObject:
70         return Immediate(constant.ToHeapObject());
71       case Constant::kInt64:
72         break;
73       case Constant::kRpoNumber:
74         return Immediate::CodeRelativeOffset(ToLabel(operand));
75     }
76     UNREACHABLE();
77     return Immediate(-1);
78   }
79
80   static size_t NextOffset(size_t* offset) {
81     size_t i = *offset;
82     (*offset)++;
83     return i;
84   }
85
86   static ScaleFactor ScaleFor(AddressingMode one, AddressingMode mode) {
87     STATIC_ASSERT(0 == static_cast<int>(times_1));
88     STATIC_ASSERT(1 == static_cast<int>(times_2));
89     STATIC_ASSERT(2 == static_cast<int>(times_4));
90     STATIC_ASSERT(3 == static_cast<int>(times_8));
91     int scale = static_cast<int>(mode - one);
92     DCHECK(scale >= 0 && scale < 4);
93     return static_cast<ScaleFactor>(scale);
94   }
95
96   Operand MemoryOperand(size_t* offset) {
97     AddressingMode mode = AddressingModeField::decode(instr_->opcode());
98     switch (mode) {
99       case kMode_MR: {
100         Register base = InputRegister(NextOffset(offset));
101         int32_t disp = 0;
102         return Operand(base, disp);
103       }
104       case kMode_MRI: {
105         Register base = InputRegister(NextOffset(offset));
106         int32_t disp = InputInt32(NextOffset(offset));
107         return Operand(base, disp);
108       }
109       case kMode_MR1:
110       case kMode_MR2:
111       case kMode_MR4:
112       case kMode_MR8: {
113         Register base = InputRegister(NextOffset(offset));
114         Register index = InputRegister(NextOffset(offset));
115         ScaleFactor scale = ScaleFor(kMode_MR1, mode);
116         int32_t disp = 0;
117         return Operand(base, index, scale, disp);
118       }
119       case kMode_MR1I:
120       case kMode_MR2I:
121       case kMode_MR4I:
122       case kMode_MR8I: {
123         Register base = InputRegister(NextOffset(offset));
124         Register index = InputRegister(NextOffset(offset));
125         ScaleFactor scale = ScaleFor(kMode_MR1I, mode);
126         int32_t disp = InputInt32(NextOffset(offset));
127         return Operand(base, index, scale, disp);
128       }
129       case kMode_M1:
130       case kMode_M2:
131       case kMode_M4:
132       case kMode_M8: {
133         Register index = InputRegister(NextOffset(offset));
134         ScaleFactor scale = ScaleFor(kMode_M1, mode);
135         int32_t disp = 0;
136         return Operand(index, scale, disp);
137       }
138       case kMode_M1I:
139       case kMode_M2I:
140       case kMode_M4I:
141       case kMode_M8I: {
142         Register index = InputRegister(NextOffset(offset));
143         ScaleFactor scale = ScaleFor(kMode_M1I, mode);
144         int32_t disp = InputInt32(NextOffset(offset));
145         return Operand(index, scale, disp);
146       }
147       case kMode_MI: {
148         int32_t disp = InputInt32(NextOffset(offset));
149         return Operand(Immediate(disp));
150       }
151       case kMode_None:
152         UNREACHABLE();
153         return Operand(no_reg, 0);
154     }
155     UNREACHABLE();
156     return Operand(no_reg, 0);
157   }
158
159   Operand MemoryOperand(size_t first_input = 0) {
160     return MemoryOperand(&first_input);
161   }
162 };
163
164
165 namespace {
166
167 bool HasImmediateInput(Instruction* instr, size_t index) {
168   return instr->InputAt(index)->IsImmediate();
169 }
170
171
172 class OutOfLineLoadInteger FINAL : public OutOfLineCode {
173  public:
174   OutOfLineLoadInteger(CodeGenerator* gen, Register result)
175       : OutOfLineCode(gen), result_(result) {}
176
177   void Generate() FINAL { __ xor_(result_, result_); }
178
179  private:
180   Register const result_;
181 };
182
183
184 class OutOfLineLoadFloat FINAL : public OutOfLineCode {
185  public:
186   OutOfLineLoadFloat(CodeGenerator* gen, XMMRegister result)
187       : OutOfLineCode(gen), result_(result) {}
188
189   void Generate() FINAL { __ pcmpeqd(result_, result_); }
190
191  private:
192   XMMRegister const result_;
193 };
194
195
196 class OutOfLineTruncateDoubleToI FINAL : public OutOfLineCode {
197  public:
198   OutOfLineTruncateDoubleToI(CodeGenerator* gen, Register result,
199                              XMMRegister input)
200       : OutOfLineCode(gen), result_(result), input_(input) {}
201
202   void Generate() FINAL {
203     __ sub(esp, Immediate(kDoubleSize));
204     __ movsd(MemOperand(esp, 0), input_);
205     __ SlowTruncateToI(result_, esp, 0);
206     __ add(esp, Immediate(kDoubleSize));
207   }
208
209  private:
210   Register const result_;
211   XMMRegister const input_;
212 };
213
214 }  // namespace
215
216
217 #define ASSEMBLE_CHECKED_LOAD_FLOAT(asm_instr)                          \
218   do {                                                                  \
219     auto result = i.OutputDoubleRegister();                             \
220     auto offset = i.InputRegister(0);                                   \
221     if (instr->InputAt(1)->IsRegister()) {                              \
222       __ cmp(offset, i.InputRegister(1));                               \
223     } else {                                                            \
224       __ cmp(offset, i.InputImmediate(1));                              \
225     }                                                                   \
226     OutOfLineCode* ool = new (zone()) OutOfLineLoadFloat(this, result); \
227     __ j(above_equal, ool->entry());                                    \
228     __ asm_instr(result, i.MemoryOperand(2));                           \
229     __ bind(ool->exit());                                               \
230   } while (false)
231
232
233 #define ASSEMBLE_CHECKED_LOAD_INTEGER(asm_instr)                          \
234   do {                                                                    \
235     auto result = i.OutputRegister();                                     \
236     auto offset = i.InputRegister(0);                                     \
237     if (instr->InputAt(1)->IsRegister()) {                                \
238       __ cmp(offset, i.InputRegister(1));                                 \
239     } else {                                                              \
240       __ cmp(offset, i.InputImmediate(1));                                \
241     }                                                                     \
242     OutOfLineCode* ool = new (zone()) OutOfLineLoadInteger(this, result); \
243     __ j(above_equal, ool->entry());                                      \
244     __ asm_instr(result, i.MemoryOperand(2));                             \
245     __ bind(ool->exit());                                                 \
246   } while (false)
247
248
249 #define ASSEMBLE_CHECKED_STORE_FLOAT(asm_instr)                 \
250   do {                                                          \
251     auto offset = i.InputRegister(0);                           \
252     if (instr->InputAt(1)->IsRegister()) {                      \
253       __ cmp(offset, i.InputRegister(1));                       \
254     } else {                                                    \
255       __ cmp(offset, i.InputImmediate(1));                      \
256     }                                                           \
257     Label done;                                                 \
258     __ j(above_equal, &done, Label::kNear);                     \
259     __ asm_instr(i.MemoryOperand(3), i.InputDoubleRegister(2)); \
260     __ bind(&done);                                             \
261   } while (false)
262
263
264 #define ASSEMBLE_CHECKED_STORE_INTEGER(asm_instr)            \
265   do {                                                       \
266     auto offset = i.InputRegister(0);                        \
267     if (instr->InputAt(1)->IsRegister()) {                   \
268       __ cmp(offset, i.InputRegister(1));                    \
269     } else {                                                 \
270       __ cmp(offset, i.InputImmediate(1));                   \
271     }                                                        \
272     Label done;                                              \
273     __ j(above_equal, &done, Label::kNear);                  \
274     if (instr->InputAt(2)->IsRegister()) {                   \
275       __ asm_instr(i.MemoryOperand(3), i.InputRegister(2));  \
276     } else {                                                 \
277       __ asm_instr(i.MemoryOperand(3), i.InputImmediate(2)); \
278     }                                                        \
279     __ bind(&done);                                          \
280   } while (false)
281
282
283 // Assembles an instruction after register allocation, producing machine code.
284 void CodeGenerator::AssembleArchInstruction(Instruction* instr) {
285   IA32OperandConverter i(this, instr);
286
287   switch (ArchOpcodeField::decode(instr->opcode())) {
288     case kArchCallCodeObject: {
289       EnsureSpaceForLazyDeopt();
290       if (HasImmediateInput(instr, 0)) {
291         Handle<Code> code = Handle<Code>::cast(i.InputHeapObject(0));
292         __ call(code, RelocInfo::CODE_TARGET);
293       } else {
294         Register reg = i.InputRegister(0);
295         __ call(Operand(reg, Code::kHeaderSize - kHeapObjectTag));
296       }
297       RecordCallPosition(instr);
298       break;
299     }
300     case kArchCallJSFunction: {
301       EnsureSpaceForLazyDeopt();
302       Register func = i.InputRegister(0);
303       if (FLAG_debug_code) {
304         // Check the function's context matches the context argument.
305         __ cmp(esi, FieldOperand(func, JSFunction::kContextOffset));
306         __ Assert(equal, kWrongFunctionContext);
307       }
308       __ call(FieldOperand(func, JSFunction::kCodeEntryOffset));
309       RecordCallPosition(instr);
310       break;
311     }
312     case kArchJmp:
313       AssembleArchJump(i.InputRpo(0));
314       break;
315     case kArchLookupSwitch:
316       AssembleArchLookupSwitch(instr);
317       break;
318     case kArchTableSwitch:
319       AssembleArchTableSwitch(instr);
320       break;
321     case kArchNop:
322       // don't emit code for nops.
323       break;
324     case kArchDeoptimize: {
325       int deopt_state_id =
326           BuildTranslation(instr, -1, 0, OutputFrameStateCombine::Ignore());
327       AssembleDeoptimizerCall(deopt_state_id, Deoptimizer::EAGER);
328       break;
329     }
330     case kArchRet:
331       AssembleReturn();
332       break;
333     case kArchStackPointer:
334       __ mov(i.OutputRegister(), esp);
335       break;
336     case kArchTruncateDoubleToI: {
337       auto result = i.OutputRegister();
338       auto input = i.InputDoubleRegister(0);
339       auto ool = new (zone()) OutOfLineTruncateDoubleToI(this, result, input);
340       __ cvttsd2si(result, Operand(input));
341       __ cmp(result, 1);
342       __ j(overflow, ool->entry());
343       __ bind(ool->exit());
344       break;
345     }
346     case kIA32Add:
347       if (HasImmediateInput(instr, 1)) {
348         __ add(i.InputOperand(0), i.InputImmediate(1));
349       } else {
350         __ add(i.InputRegister(0), i.InputOperand(1));
351       }
352       break;
353     case kIA32And:
354       if (HasImmediateInput(instr, 1)) {
355         __ and_(i.InputOperand(0), i.InputImmediate(1));
356       } else {
357         __ and_(i.InputRegister(0), i.InputOperand(1));
358       }
359       break;
360     case kIA32Cmp:
361       if (HasImmediateInput(instr, 1)) {
362         __ cmp(i.InputOperand(0), i.InputImmediate(1));
363       } else {
364         __ cmp(i.InputRegister(0), i.InputOperand(1));
365       }
366       break;
367     case kIA32Test:
368       if (HasImmediateInput(instr, 1)) {
369         __ test(i.InputOperand(0), i.InputImmediate(1));
370       } else {
371         __ test(i.InputRegister(0), i.InputOperand(1));
372       }
373       break;
374     case kIA32Imul:
375       if (HasImmediateInput(instr, 1)) {
376         __ imul(i.OutputRegister(), i.InputOperand(0), i.InputInt32(1));
377       } else {
378         __ imul(i.OutputRegister(), i.InputOperand(1));
379       }
380       break;
381     case kIA32ImulHigh:
382       __ imul(i.InputRegister(1));
383       break;
384     case kIA32UmulHigh:
385       __ mul(i.InputRegister(1));
386       break;
387     case kIA32Idiv:
388       __ cdq();
389       __ idiv(i.InputOperand(1));
390       break;
391     case kIA32Udiv:
392       __ Move(edx, Immediate(0));
393       __ div(i.InputOperand(1));
394       break;
395     case kIA32Not:
396       __ not_(i.OutputOperand());
397       break;
398     case kIA32Neg:
399       __ neg(i.OutputOperand());
400       break;
401     case kIA32Or:
402       if (HasImmediateInput(instr, 1)) {
403         __ or_(i.InputOperand(0), i.InputImmediate(1));
404       } else {
405         __ or_(i.InputRegister(0), i.InputOperand(1));
406       }
407       break;
408     case kIA32Xor:
409       if (HasImmediateInput(instr, 1)) {
410         __ xor_(i.InputOperand(0), i.InputImmediate(1));
411       } else {
412         __ xor_(i.InputRegister(0), i.InputOperand(1));
413       }
414       break;
415     case kIA32Sub:
416       if (HasImmediateInput(instr, 1)) {
417         __ sub(i.InputOperand(0), i.InputImmediate(1));
418       } else {
419         __ sub(i.InputRegister(0), i.InputOperand(1));
420       }
421       break;
422     case kIA32Shl:
423       if (HasImmediateInput(instr, 1)) {
424         __ shl(i.OutputOperand(), i.InputInt5(1));
425       } else {
426         __ shl_cl(i.OutputOperand());
427       }
428       break;
429     case kIA32Shr:
430       if (HasImmediateInput(instr, 1)) {
431         __ shr(i.OutputOperand(), i.InputInt5(1));
432       } else {
433         __ shr_cl(i.OutputOperand());
434       }
435       break;
436     case kIA32Sar:
437       if (HasImmediateInput(instr, 1)) {
438         __ sar(i.OutputOperand(), i.InputInt5(1));
439       } else {
440         __ sar_cl(i.OutputOperand());
441       }
442       break;
443     case kIA32Ror:
444       if (HasImmediateInput(instr, 1)) {
445         __ ror(i.OutputOperand(), i.InputInt5(1));
446       } else {
447         __ ror_cl(i.OutputOperand());
448       }
449       break;
450     case kIA32Lzcnt:
451       __ Lzcnt(i.OutputRegister(), i.InputOperand(0));
452       break;
453     case kSSEFloat64Cmp:
454       __ ucomisd(i.InputDoubleRegister(0), i.InputOperand(1));
455       break;
456     case kSSEFloat64Add:
457       __ addsd(i.InputDoubleRegister(0), i.InputOperand(1));
458       break;
459     case kSSEFloat64Sub:
460       __ subsd(i.InputDoubleRegister(0), i.InputOperand(1));
461       break;
462     case kSSEFloat64Mul:
463       __ mulsd(i.InputDoubleRegister(0), i.InputOperand(1));
464       break;
465     case kSSEFloat64Div:
466       __ divsd(i.InputDoubleRegister(0), i.InputOperand(1));
467       break;
468     case kSSEFloat64Max:
469       __ maxsd(i.InputDoubleRegister(0), i.InputOperand(1));
470       break;
471     case kSSEFloat64Min:
472       __ minsd(i.InputDoubleRegister(0), i.InputOperand(1));
473       break;
474     case kSSEFloat64Mod: {
475       // TODO(dcarney): alignment is wrong.
476       __ sub(esp, Immediate(kDoubleSize));
477       // Move values to st(0) and st(1).
478       __ movsd(Operand(esp, 0), i.InputDoubleRegister(1));
479       __ fld_d(Operand(esp, 0));
480       __ movsd(Operand(esp, 0), i.InputDoubleRegister(0));
481       __ fld_d(Operand(esp, 0));
482       // Loop while fprem isn't done.
483       Label mod_loop;
484       __ bind(&mod_loop);
485       // This instructions traps on all kinds inputs, but we are assuming the
486       // floating point control word is set to ignore them all.
487       __ fprem();
488       // The following 2 instruction implicitly use eax.
489       __ fnstsw_ax();
490       __ sahf();
491       __ j(parity_even, &mod_loop);
492       // Move output to stack and clean up.
493       __ fstp(1);
494       __ fstp_d(Operand(esp, 0));
495       __ movsd(i.OutputDoubleRegister(), Operand(esp, 0));
496       __ add(esp, Immediate(kDoubleSize));
497       break;
498     }
499     case kSSEFloat64Sqrt:
500       __ sqrtsd(i.OutputDoubleRegister(), i.InputOperand(0));
501       break;
502     case kSSEFloat64Round: {
503       CpuFeatureScope sse_scope(masm(), SSE4_1);
504       RoundingMode const mode =
505           static_cast<RoundingMode>(MiscField::decode(instr->opcode()));
506       __ roundsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0), mode);
507       break;
508     }
509     case kSSECvtss2sd:
510       __ cvtss2sd(i.OutputDoubleRegister(), i.InputOperand(0));
511       break;
512     case kSSECvtsd2ss:
513       __ cvtsd2ss(i.OutputDoubleRegister(), i.InputOperand(0));
514       break;
515     case kSSEFloat64ToInt32:
516       __ cvttsd2si(i.OutputRegister(), i.InputOperand(0));
517       break;
518     case kSSEFloat64ToUint32: {
519       XMMRegister scratch = xmm0;
520       __ Move(scratch, -2147483648.0);
521       __ addsd(scratch, i.InputOperand(0));
522       __ cvttsd2si(i.OutputRegister(), scratch);
523       __ add(i.OutputRegister(), Immediate(0x80000000));
524       break;
525     }
526     case kSSEInt32ToFloat64:
527       __ cvtsi2sd(i.OutputDoubleRegister(), i.InputOperand(0));
528       break;
529     case kSSEUint32ToFloat64:
530       __ LoadUint32(i.OutputDoubleRegister(), i.InputOperand(0));
531       break;
532     case kSSEFloat64ExtractLowWord32:
533       if (instr->InputAt(0)->IsDoubleStackSlot()) {
534         __ mov(i.OutputRegister(), i.InputOperand(0));
535       } else {
536         __ movd(i.OutputRegister(), i.InputDoubleRegister(0));
537       }
538       break;
539     case kSSEFloat64ExtractHighWord32:
540       if (instr->InputAt(0)->IsDoubleStackSlot()) {
541         __ mov(i.OutputRegister(), i.InputOperand(0, kDoubleSize / 2));
542       } else {
543         __ Pextrd(i.OutputRegister(), i.InputDoubleRegister(0), 1);
544       }
545       break;
546     case kSSEFloat64InsertLowWord32:
547       __ Pinsrd(i.OutputDoubleRegister(), i.InputOperand(1), 0);
548       break;
549     case kSSEFloat64InsertHighWord32:
550       __ Pinsrd(i.OutputDoubleRegister(), i.InputOperand(1), 1);
551       break;
552     case kSSEFloat64LoadLowWord32:
553       __ movd(i.OutputDoubleRegister(), i.InputOperand(0));
554       break;
555     case kAVXFloat64Add: {
556       CpuFeatureScope avx_scope(masm(), AVX);
557       __ vaddsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
558                 i.InputOperand(1));
559       break;
560     }
561     case kAVXFloat64Sub: {
562       CpuFeatureScope avx_scope(masm(), AVX);
563       __ vsubsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
564                 i.InputOperand(1));
565       break;
566     }
567     case kAVXFloat64Mul: {
568       CpuFeatureScope avx_scope(masm(), AVX);
569       __ vmulsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
570                 i.InputOperand(1));
571       break;
572     }
573     case kAVXFloat64Div: {
574       CpuFeatureScope avx_scope(masm(), AVX);
575       __ vdivsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
576                 i.InputOperand(1));
577       break;
578     }
579     case kAVXFloat64Max: {
580       CpuFeatureScope avx_scope(masm(), AVX);
581       __ vmaxsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
582                 i.InputOperand(1));
583       break;
584     }
585     case kAVXFloat64Min: {
586       CpuFeatureScope avx_scope(masm(), AVX);
587       __ vminsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
588                 i.InputOperand(1));
589       break;
590     }
591     case kIA32Movsxbl:
592       __ movsx_b(i.OutputRegister(), i.MemoryOperand());
593       break;
594     case kIA32Movzxbl:
595       __ movzx_b(i.OutputRegister(), i.MemoryOperand());
596       break;
597     case kIA32Movb: {
598       size_t index = 0;
599       Operand operand = i.MemoryOperand(&index);
600       if (HasImmediateInput(instr, index)) {
601         __ mov_b(operand, i.InputInt8(index));
602       } else {
603         __ mov_b(operand, i.InputRegister(index));
604       }
605       break;
606     }
607     case kIA32Movsxwl:
608       __ movsx_w(i.OutputRegister(), i.MemoryOperand());
609       break;
610     case kIA32Movzxwl:
611       __ movzx_w(i.OutputRegister(), i.MemoryOperand());
612       break;
613     case kIA32Movw: {
614       size_t index = 0;
615       Operand operand = i.MemoryOperand(&index);
616       if (HasImmediateInput(instr, index)) {
617         __ mov_w(operand, i.InputInt16(index));
618       } else {
619         __ mov_w(operand, i.InputRegister(index));
620       }
621       break;
622     }
623     case kIA32Movl:
624       if (instr->HasOutput()) {
625         __ mov(i.OutputRegister(), i.MemoryOperand());
626       } else {
627         size_t index = 0;
628         Operand operand = i.MemoryOperand(&index);
629         if (HasImmediateInput(instr, index)) {
630           __ mov(operand, i.InputImmediate(index));
631         } else {
632           __ mov(operand, i.InputRegister(index));
633         }
634       }
635       break;
636     case kIA32Movsd:
637       if (instr->HasOutput()) {
638         __ movsd(i.OutputDoubleRegister(), i.MemoryOperand());
639       } else {
640         size_t index = 0;
641         Operand operand = i.MemoryOperand(&index);
642         __ movsd(operand, i.InputDoubleRegister(index));
643       }
644       break;
645     case kIA32Movss:
646       if (instr->HasOutput()) {
647         __ movss(i.OutputDoubleRegister(), i.MemoryOperand());
648       } else {
649         size_t index = 0;
650         Operand operand = i.MemoryOperand(&index);
651         __ movss(operand, i.InputDoubleRegister(index));
652       }
653       break;
654     case kIA32Lea: {
655       AddressingMode mode = AddressingModeField::decode(instr->opcode());
656       // Shorten "leal" to "addl", "subl" or "shll" if the register allocation
657       // and addressing mode just happens to work out. The "addl"/"subl" forms
658       // in these cases are faster based on measurements.
659       if (mode == kMode_MI) {
660         __ Move(i.OutputRegister(), Immediate(i.InputInt32(0)));
661       } else if (i.InputRegister(0).is(i.OutputRegister())) {
662         if (mode == kMode_MRI) {
663           int32_t constant_summand = i.InputInt32(1);
664           if (constant_summand > 0) {
665             __ add(i.OutputRegister(), Immediate(constant_summand));
666           } else if (constant_summand < 0) {
667             __ sub(i.OutputRegister(), Immediate(-constant_summand));
668           }
669         } else if (mode == kMode_MR1) {
670           if (i.InputRegister(1).is(i.OutputRegister())) {
671             __ shl(i.OutputRegister(), 1);
672           } else {
673             __ lea(i.OutputRegister(), i.MemoryOperand());
674           }
675         } else if (mode == kMode_M2) {
676           __ shl(i.OutputRegister(), 1);
677         } else if (mode == kMode_M4) {
678           __ shl(i.OutputRegister(), 2);
679         } else if (mode == kMode_M8) {
680           __ shl(i.OutputRegister(), 3);
681         } else {
682           __ lea(i.OutputRegister(), i.MemoryOperand());
683         }
684       } else {
685         __ lea(i.OutputRegister(), i.MemoryOperand());
686       }
687       break;
688     }
689     case kIA32Push:
690       if (HasImmediateInput(instr, 0)) {
691         __ push(i.InputImmediate(0));
692       } else {
693         __ push(i.InputOperand(0));
694       }
695       break;
696     case kIA32StoreWriteBarrier: {
697       Register object = i.InputRegister(0);
698       Register index = i.InputRegister(1);
699       Register value = i.InputRegister(2);
700       __ mov(Operand(object, index, times_1, 0), value);
701       __ lea(index, Operand(object, index, times_1, 0));
702       SaveFPRegsMode mode =
703           frame()->DidAllocateDoubleRegisters() ? kSaveFPRegs : kDontSaveFPRegs;
704       __ RecordWrite(object, index, value, mode);
705       break;
706     }
707     case kCheckedLoadInt8:
708       ASSEMBLE_CHECKED_LOAD_INTEGER(movsx_b);
709       break;
710     case kCheckedLoadUint8:
711       ASSEMBLE_CHECKED_LOAD_INTEGER(movzx_b);
712       break;
713     case kCheckedLoadInt16:
714       ASSEMBLE_CHECKED_LOAD_INTEGER(movsx_w);
715       break;
716     case kCheckedLoadUint16:
717       ASSEMBLE_CHECKED_LOAD_INTEGER(movzx_w);
718       break;
719     case kCheckedLoadWord32:
720       ASSEMBLE_CHECKED_LOAD_INTEGER(mov);
721       break;
722     case kCheckedLoadFloat32:
723       ASSEMBLE_CHECKED_LOAD_FLOAT(movss);
724       break;
725     case kCheckedLoadFloat64:
726       ASSEMBLE_CHECKED_LOAD_FLOAT(movsd);
727       break;
728     case kCheckedStoreWord8:
729       ASSEMBLE_CHECKED_STORE_INTEGER(mov_b);
730       break;
731     case kCheckedStoreWord16:
732       ASSEMBLE_CHECKED_STORE_INTEGER(mov_w);
733       break;
734     case kCheckedStoreWord32:
735       ASSEMBLE_CHECKED_STORE_INTEGER(mov);
736       break;
737     case kCheckedStoreFloat32:
738       ASSEMBLE_CHECKED_STORE_FLOAT(movss);
739       break;
740     case kCheckedStoreFloat64:
741       ASSEMBLE_CHECKED_STORE_FLOAT(movsd);
742       break;
743     case kIA32StackCheck: {
744       ExternalReference const stack_limit =
745           ExternalReference::address_of_stack_limit(isolate());
746       __ cmp(esp, Operand::StaticVariable(stack_limit));
747       break;
748     }
749   }
750 }
751
752
753 // Assembles a branch after an instruction.
754 void CodeGenerator::AssembleArchBranch(Instruction* instr, BranchInfo* branch) {
755   IA32OperandConverter i(this, instr);
756   Label::Distance flabel_distance =
757       branch->fallthru ? Label::kNear : Label::kFar;
758   Label* tlabel = branch->true_label;
759   Label* flabel = branch->false_label;
760   switch (branch->condition) {
761     case kUnorderedEqual:
762       __ j(parity_even, flabel, flabel_distance);
763     // Fall through.
764     case kEqual:
765       __ j(equal, tlabel);
766       break;
767     case kUnorderedNotEqual:
768       __ j(parity_even, tlabel);
769     // Fall through.
770     case kNotEqual:
771       __ j(not_equal, tlabel);
772       break;
773     case kSignedLessThan:
774       __ j(less, tlabel);
775       break;
776     case kSignedGreaterThanOrEqual:
777       __ j(greater_equal, tlabel);
778       break;
779     case kSignedLessThanOrEqual:
780       __ j(less_equal, tlabel);
781       break;
782     case kSignedGreaterThan:
783       __ j(greater, tlabel);
784       break;
785     case kUnsignedLessThan:
786       __ j(below, tlabel);
787       break;
788     case kUnsignedGreaterThanOrEqual:
789       __ j(above_equal, tlabel);
790       break;
791     case kUnsignedLessThanOrEqual:
792       __ j(below_equal, tlabel);
793       break;
794     case kUnsignedGreaterThan:
795       __ j(above, tlabel);
796       break;
797     case kOverflow:
798       __ j(overflow, tlabel);
799       break;
800     case kNotOverflow:
801       __ j(no_overflow, tlabel);
802       break;
803   }
804   // Add a jump if not falling through to the next block.
805   if (!branch->fallthru) __ jmp(flabel);
806 }
807
808
809 void CodeGenerator::AssembleArchJump(RpoNumber target) {
810   if (!IsNextInAssemblyOrder(target)) __ jmp(GetLabel(target));
811 }
812
813
814 // Assembles boolean materializations after an instruction.
815 void CodeGenerator::AssembleArchBoolean(Instruction* instr,
816                                         FlagsCondition condition) {
817   IA32OperandConverter i(this, instr);
818   Label done;
819
820   // Materialize a full 32-bit 1 or 0 value. The result register is always the
821   // last output of the instruction.
822   Label check;
823   DCHECK_NE(0u, instr->OutputCount());
824   Register reg = i.OutputRegister(instr->OutputCount() - 1);
825   Condition cc = no_condition;
826   switch (condition) {
827     case kUnorderedEqual:
828       __ j(parity_odd, &check, Label::kNear);
829       __ Move(reg, Immediate(0));
830       __ jmp(&done, Label::kNear);
831     // Fall through.
832     case kEqual:
833       cc = equal;
834       break;
835     case kUnorderedNotEqual:
836       __ j(parity_odd, &check, Label::kNear);
837       __ mov(reg, Immediate(1));
838       __ jmp(&done, Label::kNear);
839     // Fall through.
840     case kNotEqual:
841       cc = not_equal;
842       break;
843     case kSignedLessThan:
844       cc = less;
845       break;
846     case kSignedGreaterThanOrEqual:
847       cc = greater_equal;
848       break;
849     case kSignedLessThanOrEqual:
850       cc = less_equal;
851       break;
852     case kSignedGreaterThan:
853       cc = greater;
854       break;
855     case kUnsignedLessThan:
856       cc = below;
857       break;
858     case kUnsignedGreaterThanOrEqual:
859       cc = above_equal;
860       break;
861     case kUnsignedLessThanOrEqual:
862       cc = below_equal;
863       break;
864     case kUnsignedGreaterThan:
865       cc = above;
866       break;
867     case kOverflow:
868       cc = overflow;
869       break;
870     case kNotOverflow:
871       cc = no_overflow;
872       break;
873   }
874   __ bind(&check);
875   if (reg.is_byte_register()) {
876     // setcc for byte registers (al, bl, cl, dl).
877     __ setcc(cc, reg);
878     __ movzx_b(reg, reg);
879   } else {
880     // Emit a branch to set a register to either 1 or 0.
881     Label set;
882     __ j(cc, &set, Label::kNear);
883     __ Move(reg, Immediate(0));
884     __ jmp(&done, Label::kNear);
885     __ bind(&set);
886     __ mov(reg, Immediate(1));
887   }
888   __ bind(&done);
889 }
890
891
892 void CodeGenerator::AssembleArchLookupSwitch(Instruction* instr) {
893   IA32OperandConverter i(this, instr);
894   Register input = i.InputRegister(0);
895   for (size_t index = 2; index < instr->InputCount(); index += 2) {
896     __ cmp(input, Immediate(i.InputInt32(index + 0)));
897     __ j(equal, GetLabel(i.InputRpo(index + 1)));
898   }
899   AssembleArchJump(i.InputRpo(1));
900 }
901
902
903 void CodeGenerator::AssembleArchTableSwitch(Instruction* instr) {
904   IA32OperandConverter i(this, instr);
905   Register input = i.InputRegister(0);
906   size_t const case_count = instr->InputCount() - 2;
907   Label** cases = zone()->NewArray<Label*>(case_count);
908   for (size_t index = 0; index < case_count; ++index) {
909     cases[index] = GetLabel(i.InputRpo(index + 2));
910   }
911   Label* const table = AddJumpTable(cases, case_count);
912   __ cmp(input, Immediate(case_count));
913   __ j(above_equal, GetLabel(i.InputRpo(1)));
914   __ jmp(Operand::JumpTable(input, times_4, table));
915 }
916
917
918 void CodeGenerator::AssembleDeoptimizerCall(
919     int deoptimization_id, Deoptimizer::BailoutType bailout_type) {
920   Address deopt_entry = Deoptimizer::GetDeoptimizationEntry(
921       isolate(), deoptimization_id, bailout_type);
922   __ call(deopt_entry, RelocInfo::RUNTIME_ENTRY);
923 }
924
925
926 // The calling convention for JSFunctions on IA32 passes arguments on the
927 // stack and the JSFunction and context in EDI and ESI, respectively, thus
928 // the steps of the call look as follows:
929
930 // --{ before the call instruction }--------------------------------------------
931 //                                                         |  caller frame |
932 //                                                         ^ esp           ^ ebp
933
934 // --{ push arguments and setup ESI, EDI }--------------------------------------
935 //                                       | args + receiver |  caller frame |
936 //                                       ^ esp                             ^ ebp
937 //                 [edi = JSFunction, esi = context]
938
939 // --{ call [edi + kCodeEntryOffset] }------------------------------------------
940 //                                 | RET | args + receiver |  caller frame |
941 //                                 ^ esp                                   ^ ebp
942
943 // =={ prologue of called function }============================================
944 // --{ push ebp }---------------------------------------------------------------
945 //                            | FP | RET | args + receiver |  caller frame |
946 //                            ^ esp                                        ^ ebp
947
948 // --{ mov ebp, esp }-----------------------------------------------------------
949 //                            | FP | RET | args + receiver |  caller frame |
950 //                            ^ ebp,esp
951
952 // --{ push esi }---------------------------------------------------------------
953 //                      | CTX | FP | RET | args + receiver |  caller frame |
954 //                      ^esp  ^ ebp
955
956 // --{ push edi }---------------------------------------------------------------
957 //                | FNC | CTX | FP | RET | args + receiver |  caller frame |
958 //                ^esp        ^ ebp
959
960 // --{ subi esp, #N }-----------------------------------------------------------
961 // | callee frame | FNC | CTX | FP | RET | args + receiver |  caller frame |
962 // ^esp                       ^ ebp
963
964 // =={ body of called function }================================================
965
966 // =={ epilogue of called function }============================================
967 // --{ mov esp, ebp }-----------------------------------------------------------
968 //                            | FP | RET | args + receiver |  caller frame |
969 //                            ^ esp,ebp
970
971 // --{ pop ebp }-----------------------------------------------------------
972 // |                               | RET | args + receiver |  caller frame |
973 //                                 ^ esp                                   ^ ebp
974
975 // --{ ret #A+1 }-----------------------------------------------------------
976 // |                                                       |  caller frame |
977 //                                                         ^ esp           ^ ebp
978
979
980 // Runtime function calls are accomplished by doing a stub call to the
981 // CEntryStub (a real code object). On IA32 passes arguments on the
982 // stack, the number of arguments in EAX, the address of the runtime function
983 // in EBX, and the context in ESI.
984
985 // --{ before the call instruction }--------------------------------------------
986 //                                                         |  caller frame |
987 //                                                         ^ esp           ^ ebp
988
989 // --{ push arguments and setup EAX, EBX, and ESI }-----------------------------
990 //                                       | args + receiver |  caller frame |
991 //                                       ^ esp                             ^ ebp
992 //              [eax = #args, ebx = runtime function, esi = context]
993
994 // --{ call #CEntryStub }-------------------------------------------------------
995 //                                 | RET | args + receiver |  caller frame |
996 //                                 ^ esp                                   ^ ebp
997
998 // =={ body of runtime function }===============================================
999
1000 // --{ runtime returns }--------------------------------------------------------
1001 //                                                         |  caller frame |
1002 //                                                         ^ esp           ^ ebp
1003
1004 // Other custom linkages (e.g. for calling directly into and out of C++) may
1005 // need to save callee-saved registers on the stack, which is done in the
1006 // function prologue of generated code.
1007
1008 // --{ before the call instruction }--------------------------------------------
1009 //                                                         |  caller frame |
1010 //                                                         ^ esp           ^ ebp
1011
1012 // --{ set up arguments in registers on stack }---------------------------------
1013 //                                                  | args |  caller frame |
1014 //                                                  ^ esp                  ^ ebp
1015 //                  [r0 = arg0, r1 = arg1, ...]
1016
1017 // --{ call code }--------------------------------------------------------------
1018 //                                            | RET | args |  caller frame |
1019 //                                            ^ esp                        ^ ebp
1020
1021 // =={ prologue of called function }============================================
1022 // --{ push ebp }---------------------------------------------------------------
1023 //                                       | FP | RET | args |  caller frame |
1024 //                                       ^ esp                             ^ ebp
1025
1026 // --{ mov ebp, esp }-----------------------------------------------------------
1027 //                                       | FP | RET | args |  caller frame |
1028 //                                       ^ ebp,esp
1029
1030 // --{ save registers }---------------------------------------------------------
1031 //                                | regs | FP | RET | args |  caller frame |
1032 //                                ^ esp  ^ ebp
1033
1034 // --{ subi esp, #N }-----------------------------------------------------------
1035 //                 | callee frame | regs | FP | RET | args |  caller frame |
1036 //                 ^esp                  ^ ebp
1037
1038 // =={ body of called function }================================================
1039
1040 // =={ epilogue of called function }============================================
1041 // --{ restore registers }------------------------------------------------------
1042 //                                | regs | FP | RET | args |  caller frame |
1043 //                                ^ esp  ^ ebp
1044
1045 // --{ mov esp, ebp }-----------------------------------------------------------
1046 //                                       | FP | RET | args |  caller frame |
1047 //                                       ^ esp,ebp
1048
1049 // --{ pop ebp }----------------------------------------------------------------
1050 //                                            | RET | args |  caller frame |
1051 //                                            ^ esp                        ^ ebp
1052
1053
1054 void CodeGenerator::AssemblePrologue() {
1055   CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
1056   int stack_slots = frame()->GetSpillSlotCount();
1057   if (descriptor->kind() == CallDescriptor::kCallAddress) {
1058     // Assemble a prologue similar the to cdecl calling convention.
1059     __ push(ebp);
1060     __ mov(ebp, esp);
1061     const RegList saves = descriptor->CalleeSavedRegisters();
1062     if (saves != 0) {  // Save callee-saved registers.
1063       int register_save_area_size = 0;
1064       for (int i = Register::kNumRegisters - 1; i >= 0; i--) {
1065         if (!((1 << i) & saves)) continue;
1066         __ push(Register::from_code(i));
1067         register_save_area_size += kPointerSize;
1068       }
1069       frame()->SetRegisterSaveAreaSize(register_save_area_size);
1070     }
1071   } else if (descriptor->IsJSFunctionCall()) {
1072     // TODO(turbofan): this prologue is redundant with OSR, but needed for
1073     // code aging.
1074     CompilationInfo* info = this->info();
1075     __ Prologue(info->IsCodePreAgingActive());
1076     frame()->SetRegisterSaveAreaSize(
1077         StandardFrameConstants::kFixedFrameSizeFromFp);
1078   } else if (stack_slots > 0) {
1079     __ StubPrologue();
1080     frame()->SetRegisterSaveAreaSize(
1081         StandardFrameConstants::kFixedFrameSizeFromFp);
1082   }
1083
1084   if (info()->is_osr()) {
1085     // TurboFan OSR-compiled functions cannot be entered directly.
1086     __ Abort(kShouldNotDirectlyEnterOsrFunction);
1087
1088     // Unoptimized code jumps directly to this entrypoint while the unoptimized
1089     // frame is still on the stack. Optimized code uses OSR values directly from
1090     // the unoptimized frame. Thus, all that needs to be done is to allocate the
1091     // remaining stack slots.
1092     if (FLAG_code_comments) __ RecordComment("-- OSR entrypoint --");
1093     osr_pc_offset_ = __ pc_offset();
1094     // TODO(titzer): cannot address target function == local #-1
1095     __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1096     DCHECK(stack_slots >= frame()->GetOsrStackSlotCount());
1097     stack_slots -= frame()->GetOsrStackSlotCount();
1098   }
1099
1100   if (stack_slots > 0) {
1101     // Allocate the stack slots used by this frame.
1102     __ sub(esp, Immediate(stack_slots * kPointerSize));
1103   }
1104 }
1105
1106
1107 void CodeGenerator::AssembleReturn() {
1108   CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
1109   int stack_slots = frame()->GetSpillSlotCount();
1110   if (descriptor->kind() == CallDescriptor::kCallAddress) {
1111     const RegList saves = descriptor->CalleeSavedRegisters();
1112     if (frame()->GetRegisterSaveAreaSize() > 0) {
1113       // Remove this frame's spill slots first.
1114       if (stack_slots > 0) {
1115         __ add(esp, Immediate(stack_slots * kPointerSize));
1116       }
1117       // Restore registers.
1118       if (saves != 0) {
1119         for (int i = 0; i < Register::kNumRegisters; i++) {
1120           if (!((1 << i) & saves)) continue;
1121           __ pop(Register::from_code(i));
1122         }
1123       }
1124       __ pop(ebp);  // Pop caller's frame pointer.
1125       __ ret(0);
1126     } else {
1127       // No saved registers.
1128       __ mov(esp, ebp);  // Move stack pointer back to frame pointer.
1129       __ pop(ebp);       // Pop caller's frame pointer.
1130       __ ret(0);
1131     }
1132   } else if (descriptor->IsJSFunctionCall() || stack_slots > 0) {
1133     __ mov(esp, ebp);  // Move stack pointer back to frame pointer.
1134     __ pop(ebp);       // Pop caller's frame pointer.
1135     int pop_count = descriptor->IsJSFunctionCall()
1136                         ? static_cast<int>(descriptor->JSParameterCount())
1137                         : 0;
1138     __ ret(pop_count * kPointerSize);
1139   } else {
1140     __ ret(0);
1141   }
1142 }
1143
1144
1145 void CodeGenerator::AssembleMove(InstructionOperand* source,
1146                                  InstructionOperand* destination) {
1147   IA32OperandConverter g(this, NULL);
1148   // Dispatch on the source and destination operand kinds.  Not all
1149   // combinations are possible.
1150   if (source->IsRegister()) {
1151     DCHECK(destination->IsRegister() || destination->IsStackSlot());
1152     Register src = g.ToRegister(source);
1153     Operand dst = g.ToOperand(destination);
1154     __ mov(dst, src);
1155   } else if (source->IsStackSlot()) {
1156     DCHECK(destination->IsRegister() || destination->IsStackSlot());
1157     Operand src = g.ToOperand(source);
1158     if (destination->IsRegister()) {
1159       Register dst = g.ToRegister(destination);
1160       __ mov(dst, src);
1161     } else {
1162       Operand dst = g.ToOperand(destination);
1163       __ push(src);
1164       __ pop(dst);
1165     }
1166   } else if (source->IsConstant()) {
1167     Constant src_constant = g.ToConstant(source);
1168     if (src_constant.type() == Constant::kHeapObject) {
1169       Handle<HeapObject> src = src_constant.ToHeapObject();
1170       if (info()->IsOptimizing() && src.is_identical_to(info()->context())) {
1171         // Loading the context from the frame is way cheaper than materializing
1172         // the actual context heap object address.
1173         if (destination->IsRegister()) {
1174           Register dst = g.ToRegister(destination);
1175           __ mov(dst, Operand(ebp, StandardFrameConstants::kContextOffset));
1176         } else {
1177           DCHECK(destination->IsStackSlot());
1178           Operand dst = g.ToOperand(destination);
1179           __ push(Operand(ebp, StandardFrameConstants::kContextOffset));
1180           __ pop(dst);
1181         }
1182       } else if (destination->IsRegister()) {
1183         Register dst = g.ToRegister(destination);
1184         __ LoadHeapObject(dst, src);
1185       } else {
1186         DCHECK(destination->IsStackSlot());
1187         Operand dst = g.ToOperand(destination);
1188         AllowDeferredHandleDereference embedding_raw_address;
1189         if (isolate()->heap()->InNewSpace(*src)) {
1190           __ PushHeapObject(src);
1191           __ pop(dst);
1192         } else {
1193           __ mov(dst, src);
1194         }
1195       }
1196     } else if (destination->IsRegister()) {
1197       Register dst = g.ToRegister(destination);
1198       __ Move(dst, g.ToImmediate(source));
1199     } else if (destination->IsStackSlot()) {
1200       Operand dst = g.ToOperand(destination);
1201       __ Move(dst, g.ToImmediate(source));
1202     } else if (src_constant.type() == Constant::kFloat32) {
1203       // TODO(turbofan): Can we do better here?
1204       uint32_t src = bit_cast<uint32_t>(src_constant.ToFloat32());
1205       if (destination->IsDoubleRegister()) {
1206         XMMRegister dst = g.ToDoubleRegister(destination);
1207         __ Move(dst, src);
1208       } else {
1209         DCHECK(destination->IsDoubleStackSlot());
1210         Operand dst = g.ToOperand(destination);
1211         __ Move(dst, Immediate(src));
1212       }
1213     } else {
1214       DCHECK_EQ(Constant::kFloat64, src_constant.type());
1215       uint64_t src = bit_cast<uint64_t>(src_constant.ToFloat64());
1216       uint32_t lower = static_cast<uint32_t>(src);
1217       uint32_t upper = static_cast<uint32_t>(src >> 32);
1218       if (destination->IsDoubleRegister()) {
1219         XMMRegister dst = g.ToDoubleRegister(destination);
1220         __ Move(dst, src);
1221       } else {
1222         DCHECK(destination->IsDoubleStackSlot());
1223         Operand dst0 = g.ToOperand(destination);
1224         Operand dst1 = g.HighOperand(destination);
1225         __ Move(dst0, Immediate(lower));
1226         __ Move(dst1, Immediate(upper));
1227       }
1228     }
1229   } else if (source->IsDoubleRegister()) {
1230     XMMRegister src = g.ToDoubleRegister(source);
1231     if (destination->IsDoubleRegister()) {
1232       XMMRegister dst = g.ToDoubleRegister(destination);
1233       __ movaps(dst, src);
1234     } else {
1235       DCHECK(destination->IsDoubleStackSlot());
1236       Operand dst = g.ToOperand(destination);
1237       __ movsd(dst, src);
1238     }
1239   } else if (source->IsDoubleStackSlot()) {
1240     DCHECK(destination->IsDoubleRegister() || destination->IsDoubleStackSlot());
1241     Operand src = g.ToOperand(source);
1242     if (destination->IsDoubleRegister()) {
1243       XMMRegister dst = g.ToDoubleRegister(destination);
1244       __ movsd(dst, src);
1245     } else {
1246       // We rely on having xmm0 available as a fixed scratch register.
1247       Operand dst = g.ToOperand(destination);
1248       __ movsd(xmm0, src);
1249       __ movsd(dst, xmm0);
1250     }
1251   } else {
1252     UNREACHABLE();
1253   }
1254 }
1255
1256
1257 void CodeGenerator::AssembleSwap(InstructionOperand* source,
1258                                  InstructionOperand* destination) {
1259   IA32OperandConverter g(this, NULL);
1260   // Dispatch on the source and destination operand kinds.  Not all
1261   // combinations are possible.
1262   if (source->IsRegister() && destination->IsRegister()) {
1263     // Register-register.
1264     Register src = g.ToRegister(source);
1265     Register dst = g.ToRegister(destination);
1266     __ xchg(dst, src);
1267   } else if (source->IsRegister() && destination->IsStackSlot()) {
1268     // Register-memory.
1269     __ xchg(g.ToRegister(source), g.ToOperand(destination));
1270   } else if (source->IsStackSlot() && destination->IsStackSlot()) {
1271     // Memory-memory.
1272     Operand src = g.ToOperand(source);
1273     Operand dst = g.ToOperand(destination);
1274     __ push(dst);
1275     __ push(src);
1276     __ pop(dst);
1277     __ pop(src);
1278   } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
1279     // XMM register-register swap. We rely on having xmm0
1280     // available as a fixed scratch register.
1281     XMMRegister src = g.ToDoubleRegister(source);
1282     XMMRegister dst = g.ToDoubleRegister(destination);
1283     __ movaps(xmm0, src);
1284     __ movaps(src, dst);
1285     __ movaps(dst, xmm0);
1286   } else if (source->IsDoubleRegister() && destination->IsDoubleStackSlot()) {
1287     // XMM register-memory swap.  We rely on having xmm0
1288     // available as a fixed scratch register.
1289     XMMRegister reg = g.ToDoubleRegister(source);
1290     Operand other = g.ToOperand(destination);
1291     __ movsd(xmm0, other);
1292     __ movsd(other, reg);
1293     __ movaps(reg, xmm0);
1294   } else if (source->IsDoubleStackSlot() && destination->IsDoubleStackSlot()) {
1295     // Double-width memory-to-memory.
1296     Operand src0 = g.ToOperand(source);
1297     Operand src1 = g.HighOperand(source);
1298     Operand dst0 = g.ToOperand(destination);
1299     Operand dst1 = g.HighOperand(destination);
1300     __ movsd(xmm0, dst0);  // Save destination in xmm0.
1301     __ push(src0);         // Then use stack to copy source to destination.
1302     __ pop(dst0);
1303     __ push(src1);
1304     __ pop(dst1);
1305     __ movsd(src0, xmm0);
1306   } else {
1307     // No other combinations are possible.
1308     UNREACHABLE();
1309   }
1310 }
1311
1312
1313 void CodeGenerator::AssembleJumpTable(Label** targets, size_t target_count) {
1314   for (size_t index = 0; index < target_count; ++index) {
1315     __ dd(targets[index]);
1316   }
1317 }
1318
1319
1320 void CodeGenerator::AddNopForSmiCodeInlining() { __ nop(); }
1321
1322
1323 void CodeGenerator::EnsureSpaceForLazyDeopt() {
1324   int space_needed = Deoptimizer::patch_size();
1325   if (!info()->IsStub()) {
1326     // Ensure that we have enough space after the previous lazy-bailout
1327     // instruction for patching the code here.
1328     int current_pc = masm()->pc_offset();
1329     if (current_pc < last_lazy_deopt_pc_ + space_needed) {
1330       int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
1331       __ Nop(padding_size);
1332     }
1333   }
1334   MarkLazyDeoptSite();
1335 }
1336
1337 #undef __
1338
1339 }  // namespace compiler
1340 }  // namespace internal
1341 }  // namespace v8