start adding MRMDestMem, which requires memory form mod/rm encoding
[oota-llvm.git] / lib / Target / X86 / X86MCCodeEmitter.cpp
1 //===-- X86/X86MCCodeEmitter.cpp - Convert X86 code to machine code -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the X86MCCodeEmitter class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "x86-emitter"
15 #include "X86.h"
16 #include "X86InstrInfo.h"
17 #include "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm;
21
22 namespace {
23 class X86MCCodeEmitter : public MCCodeEmitter {
24   X86MCCodeEmitter(const X86MCCodeEmitter &); // DO NOT IMPLEMENT
25   void operator=(const X86MCCodeEmitter &); // DO NOT IMPLEMENT
26   const TargetMachine &TM;
27   const TargetInstrInfo &TII;
28   bool Is64BitMode;
29 public:
30   X86MCCodeEmitter(TargetMachine &tm) 
31     : TM(tm), TII(*TM.getInstrInfo()) {
32     // FIXME: Get this from the right place.
33     Is64BitMode = false;
34   }
35
36   ~X86MCCodeEmitter() {}
37   
38   static unsigned GetX86RegNum(const MCOperand &MO) {
39     return X86RegisterInfo::getX86RegNum(MO.getReg());
40   }
41   
42   void EmitByte(unsigned char C, raw_ostream &OS) const {
43     OS << (char)C;
44   }
45   
46   void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
47     // Output the constant in little endian byte order.
48     for (unsigned i = 0; i != Size; ++i) {
49       EmitByte(Val & 255, OS);
50       Val >>= 8;
51     }
52   }
53   
54   inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
55                                         unsigned RM) {
56     assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
57     return RM | (RegOpcode << 3) | (Mod << 6);
58   }
59   
60   void EmitRegModRMByte(const MCOperand &ModRMReg, unsigned RegOpcodeFld,
61                         raw_ostream &OS) const {
62     EmitByte(ModRMByte(3, RegOpcodeFld, GetX86RegNum(ModRMReg)), OS);
63   }
64   
65   void EmitMemModRMByte(const MCInst &MI, unsigned Op,
66                         unsigned RegOpcodeField, intptr_t PCAdj,
67                         raw_ostream &OS) const;
68   
69   void EncodeInstruction(const MCInst &MI, raw_ostream &OS) const;
70   
71 };
72
73 } // end anonymous namespace
74
75
76 MCCodeEmitter *llvm::createX86MCCodeEmitter(const Target &,
77                                             TargetMachine &TM) {
78   return new X86MCCodeEmitter(TM);
79 }
80
81
82 /// isDisp8 - Return true if this signed displacement fits in a 8-bit 
83 /// sign-extended field. 
84 static bool isDisp8(int Value) {
85   return Value == (signed char)Value;
86 }
87
88 void X86MCCodeEmitter::EmitMemModRMByte(const MCInst &MI, unsigned Op,
89                                         unsigned RegOpcodeField,
90                                         intptr_t PCAdj,
91                                         raw_ostream &OS) const {
92   const MCOperand &Op3 = MI.getOperand(Op+3);
93   int DispVal = 0;
94   const MCOperand *DispForReloc = 0;
95   
96   // Figure out what sort of displacement we have to handle here.
97   if (Op3.isImm()) {
98     DispVal = Op3.getImm();
99   } else {
100 #if 0
101   if (Op3.isGlobal()) {
102     DispForReloc = &Op3;
103   } else if (Op3.isSymbol()) {
104     DispForReloc = &Op3;
105   } else if (Op3.isCPI()) {
106     if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
107       DispForReloc = &Op3;
108     } else {
109       DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex());
110       DispVal += Op3.getOffset();
111     }
112   } else {
113     assert(Op3.isJTI());
114     if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
115       DispForReloc = &Op3;
116     } else {
117       DispVal += MCE.getJumpTableEntryAddress(Op3.getIndex());
118     }
119 #endif
120   }
121   
122   const MCOperand &Base     = MI.getOperand(Op);
123   //const MCOperand &Scale    = MI.getOperand(Op+1);
124   const MCOperand &IndexReg = MI.getOperand(Op+2);
125   unsigned BaseReg = Base.getReg();
126
127   // Is a SIB byte needed?
128   // If no BaseReg, issue a RIP relative instruction only if the MCE can 
129   // resolve addresses on-the-fly, otherwise use SIB (Intel Manual 2A, table
130   // 2-7) and absolute references.
131   if ((!Is64BitMode || DispForReloc || BaseReg != 0) &&
132       IndexReg.getReg() == 0 && 
133       (BaseReg == X86::RIP || (BaseReg != 0 && BaseReg != X86::ESP))) {
134     if (BaseReg == 0 || BaseReg == X86::RIP) {  // Just a displacement?
135       // Emit special case [disp32] encoding
136       EmitByte(ModRMByte(0, RegOpcodeField, 5), OS);
137 #if 0
138       emitDisplacementField(DispForReloc, DispVal, PCAdj, true);
139 #endif
140     } else {
141       unsigned BaseRegNo = GetX86RegNum(Base);
142       if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
143         // Emit simple indirect register encoding... [EAX] f.e.
144         EmitByte(ModRMByte(0, RegOpcodeField, BaseRegNo), OS);
145       } else if (!DispForReloc && isDisp8(DispVal)) {
146         // Emit the disp8 encoding... [REG+disp8]
147         EmitByte(ModRMByte(1, RegOpcodeField, BaseRegNo), OS);
148         EmitConstant(DispVal, 1, OS);
149       } else {
150         // Emit the most general non-SIB encoding: [REG+disp32]
151         EmitByte(ModRMByte(2, RegOpcodeField, BaseRegNo), OS);
152 #if 0
153         emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
154 #endif
155       }
156     }
157     
158   } else {  // We need a SIB byte, so start by outputting the ModR/M byte first
159     assert(IndexReg.getReg() != X86::ESP &&
160            IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
161     
162     bool ForceDisp32 = false;
163     bool ForceDisp8  = false;
164     if (BaseReg == 0) {
165       // If there is no base register, we emit the special case SIB byte with
166       // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
167       EmitByte(ModRMByte(0, RegOpcodeField, 4), OS);
168       ForceDisp32 = true;
169     } else if (DispForReloc) {
170       // Emit the normal disp32 encoding.
171       EmitByte(ModRMByte(2, RegOpcodeField, 4), OS);
172       ForceDisp32 = true;
173     } else if (DispVal == 0 && BaseReg != X86::EBP) {
174       // Emit no displacement ModR/M byte
175       EmitByte(ModRMByte(0, RegOpcodeField, 4), OS);
176     } else if (isDisp8(DispVal)) {
177       // Emit the disp8 encoding.
178       EmitByte(ModRMByte(1, RegOpcodeField, 4), OS);
179       ForceDisp8 = true;           // Make sure to force 8 bit disp if Base=EBP
180     } else {
181       // Emit the normal disp32 encoding.
182       EmitByte(ModRMByte(2, RegOpcodeField, 4), OS);
183     }
184     
185 #if 0
186     // Calculate what the SS field value should be...
187     static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
188     unsigned SS = SSTable[Scale.getImm()];
189     
190     if (BaseReg == 0) {
191       // Handle the SIB byte for the case where there is no base, see Intel 
192       // Manual 2A, table 2-7. The displacement has already been output.
193       unsigned IndexRegNo;
194       if (IndexReg.getReg())
195         IndexRegNo = getX86RegNum(IndexReg.getReg());
196       else // Examples: [ESP+1*<noreg>+4] or [scaled idx]+disp32 (MOD=0,BASE=5)
197         IndexRegNo = 4;
198       emitSIBByte(SS, IndexRegNo, 5);
199     } else {
200       unsigned BaseRegNo = getX86RegNum(BaseReg);
201       unsigned IndexRegNo;
202       if (IndexReg.getReg())
203         IndexRegNo = getX86RegNum(IndexReg.getReg());
204       else
205         IndexRegNo = 4;   // For example [ESP+1*<noreg>+4]
206       emitSIBByte(SS, IndexRegNo, BaseRegNo);
207     }
208     
209     // Do we need to output a displacement?
210     if (ForceDisp8) {
211       emitConstant(DispVal, 1);
212     } else if (DispVal != 0 || ForceDisp32) {
213       emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
214     }
215 #endif
216   }
217 }
218
219
220 void X86MCCodeEmitter::
221 EncodeInstruction(const MCInst &MI, raw_ostream &OS) const {
222   unsigned Opcode = MI.getOpcode();
223   const TargetInstrDesc &Desc = TII.get(Opcode);
224   unsigned TSFlags = Desc.TSFlags;
225
226   // FIXME: We should emit the prefixes in exactly the same order as GAS does,
227   // in order to provide diffability.
228
229   // Emit the lock opcode prefix as needed.
230   if (TSFlags & X86II::LOCK)
231     EmitByte(0xF0, OS);
232   
233   // Emit segment override opcode prefix as needed.
234   switch (TSFlags & X86II::SegOvrMask) {
235   default: assert(0 && "Invalid segment!");
236   case 0: break;  // No segment override!
237   case X86II::FS:
238     EmitByte(0x64, OS);
239     break;
240   case X86II::GS:
241     EmitByte(0x65, OS);
242     break;
243   }
244   
245   // Emit the repeat opcode prefix as needed.
246   if ((TSFlags & X86II::Op0Mask) == X86II::REP)
247     EmitByte(0xF3, OS);
248   
249   // Emit the operand size opcode prefix as needed.
250   if (TSFlags & X86II::OpSize)
251     EmitByte(0x66, OS);
252   
253   // Emit the address size opcode prefix as needed.
254   if (TSFlags & X86II::AdSize)
255     EmitByte(0x67, OS);
256   
257   bool Need0FPrefix = false;
258   switch (TSFlags & X86II::Op0Mask) {
259   default: assert(0 && "Invalid prefix!");
260   case 0: break;  // No prefix!
261   case X86II::REP: break; // already handled.
262   case X86II::TB:  // Two-byte opcode prefix
263   case X86II::T8:  // 0F 38
264   case X86II::TA:  // 0F 3A
265     Need0FPrefix = true;
266     break;
267   case X86II::TF: // F2 0F 38
268     EmitByte(0xF2, OS);
269     Need0FPrefix = true;
270     break;
271   case X86II::XS:   // F3 0F
272     EmitByte(0xF3, OS);
273     Need0FPrefix = true;
274     break;
275   case X86II::XD:   // F2 0F
276     EmitByte(0xF2, OS);
277     Need0FPrefix = true;
278     break;
279   case X86II::D8: EmitByte(0xD8, OS); break;
280   case X86II::D9: EmitByte(0xD9, OS); break;
281   case X86II::DA: EmitByte(0xDA, OS); break;
282   case X86II::DB: EmitByte(0xDB, OS); break;
283   case X86II::DC: EmitByte(0xDC, OS); break;
284   case X86II::DD: EmitByte(0xDD, OS); break;
285   case X86II::DE: EmitByte(0xDE, OS); break;
286   case X86II::DF: EmitByte(0xDF, OS); break;
287   }
288   
289   // Handle REX prefix.
290 #if 0 // FIXME: Add in, also, can this come before F2 etc to simplify emission?
291   if (Is64BitMode) {
292     if (unsigned REX = X86InstrInfo::determineREX(MI))
293       EmitByte(0x40 | REX, OS);
294   }
295 #endif
296   
297   // 0x0F escape code must be emitted just before the opcode.
298   if (Need0FPrefix)
299     EmitByte(0x0F, OS);
300   
301   // FIXME: Pull this up into previous switch if REX can be moved earlier.
302   switch (TSFlags & X86II::Op0Mask) {
303   case X86II::TF:    // F2 0F 38
304   case X86II::T8:    // 0F 38
305     EmitByte(0x38, OS);
306     break;
307   case X86II::TA:    // 0F 3A
308     EmitByte(0x3A, OS);
309     break;
310   }
311   
312   // If this is a two-address instruction, skip one of the register operands.
313   unsigned NumOps = Desc.getNumOperands();
314   unsigned CurOp = 0;
315   if (NumOps > 1 && Desc.getOperandConstraint(1, TOI::TIED_TO) != -1)
316     ++CurOp;
317   else if (NumOps > 2 && Desc.getOperandConstraint(NumOps-1, TOI::TIED_TO)== 0)
318     // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
319     --NumOps;
320   
321   // FIXME: Can we kill off MRMInitReg??
322   
323   unsigned char BaseOpcode = X86InstrInfo::getBaseOpcodeFor(Desc);
324   switch (TSFlags & X86II::FormMask) {
325   default: errs() << "FORM: " << (TSFlags & X86II::FormMask) << "\n";
326       assert(0 && "Unknown FormMask value in X86MCCodeEmitter!");
327   case X86II::RawFrm: {
328     EmitByte(BaseOpcode, OS);
329     
330     if (CurOp == NumOps)
331       break;
332     
333     assert(0 && "Unimpl RawFrm expr");
334     break;
335   }
336       
337   case X86II::AddRegFrm: {
338     EmitByte(BaseOpcode + GetX86RegNum(MI.getOperand(CurOp++)),OS);
339     if (CurOp == NumOps)
340       break;
341
342     const MCOperand &MO1 = MI.getOperand(CurOp++);
343     if (MO1.isImm()) {
344       unsigned Size = X86InstrInfo::sizeOfImm(&Desc);
345       EmitConstant(MO1.getImm(), Size, OS);
346       break;
347     }
348
349     assert(0 && "Unimpl AddRegFrm expr");
350     break;
351   }
352       
353   case X86II::MRMDestReg:
354     EmitByte(BaseOpcode, OS);
355     EmitRegModRMByte(MI.getOperand(CurOp),
356                      GetX86RegNum(MI.getOperand(CurOp+1)), OS);
357     CurOp += 2;
358     if (CurOp != NumOps)
359       EmitConstant(MI.getOperand(CurOp++).getImm(),
360                    X86InstrInfo::sizeOfImm(&Desc), OS);
361     break;
362   
363   case X86II::MRMDestMem:
364     EmitByte(BaseOpcode, OS);
365     EmitMemModRMByte(MI, CurOp,
366                      GetX86RegNum(MI.getOperand(CurOp + X86AddrNumOperands)),
367                      0, OS);
368     CurOp +=  X86AddrNumOperands + 1;
369     if (CurOp != NumOps)
370       EmitConstant(MI.getOperand(CurOp++).getImm(),
371                    X86InstrInfo::sizeOfImm(&Desc), OS);
372     break;
373   }
374   
375 #ifndef NDEBUG
376   if (!Desc.isVariadic() && CurOp != NumOps) {
377     errs() << "Cannot encode all operands of: ";
378     MI.dump();
379     errs() << '\n';
380     abort();
381   }
382 #endif
383 }