blob: 46d38892b219f635590ad6a2af05e93f19929db4 [file] [log] [blame]
Chris Lattner45762472010-02-03 21:24:49 +00001//===-- 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"
Chris Lattner92b1dfe2010-02-03 21:43:43 +000016#include "X86InstrInfo.h"
Chris Lattner45762472010-02-03 21:24:49 +000017#include "llvm/MC/MCCodeEmitter.h"
Chris Lattner92b1dfe2010-02-03 21:43:43 +000018#include "llvm/MC/MCInst.h"
19#include "llvm/Support/raw_ostream.h"
Chris Lattner45762472010-02-03 21:24:49 +000020using namespace llvm;
21
22namespace {
23class X86MCCodeEmitter : public MCCodeEmitter {
24 X86MCCodeEmitter(const X86MCCodeEmitter &); // DO NOT IMPLEMENT
25 void operator=(const X86MCCodeEmitter &); // DO NOT IMPLEMENT
Chris Lattner92b1dfe2010-02-03 21:43:43 +000026 const TargetMachine &TM;
27 const TargetInstrInfo &TII;
Chris Lattner1ac23b12010-02-05 02:18:40 +000028 bool Is64BitMode;
Chris Lattner45762472010-02-03 21:24:49 +000029public:
Chris Lattner00cb3fe2010-02-05 21:51:35 +000030 X86MCCodeEmitter(TargetMachine &tm, bool is64Bit)
Chris Lattner92b1dfe2010-02-03 21:43:43 +000031 : TM(tm), TII(*TM.getInstrInfo()) {
Chris Lattner00cb3fe2010-02-05 21:51:35 +000032 Is64BitMode = is64Bit;
Chris Lattner45762472010-02-03 21:24:49 +000033 }
34
35 ~X86MCCodeEmitter() {}
36
Chris Lattner28249d92010-02-05 01:53:19 +000037 static unsigned GetX86RegNum(const MCOperand &MO) {
38 return X86RegisterInfo::getX86RegNum(MO.getReg());
39 }
40
Chris Lattner92b1dfe2010-02-03 21:43:43 +000041 void EmitByte(unsigned char C, raw_ostream &OS) const {
42 OS << (char)C;
Chris Lattner45762472010-02-03 21:24:49 +000043 }
Chris Lattner92b1dfe2010-02-03 21:43:43 +000044
Chris Lattner28249d92010-02-05 01:53:19 +000045 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
46 // Output the constant in little endian byte order.
47 for (unsigned i = 0; i != Size; ++i) {
48 EmitByte(Val & 255, OS);
49 Val >>= 8;
50 }
51 }
Chris Lattner0e73c392010-02-05 06:16:07 +000052
53 void EmitDisplacementField(const MCOperand *RelocOp, int DispVal,
54 int64_t Adj, bool IsPCRel, raw_ostream &OS) const;
Chris Lattner28249d92010-02-05 01:53:19 +000055
56 inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
57 unsigned RM) {
58 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
59 return RM | (RegOpcode << 3) | (Mod << 6);
60 }
61
62 void EmitRegModRMByte(const MCOperand &ModRMReg, unsigned RegOpcodeFld,
63 raw_ostream &OS) const {
64 EmitByte(ModRMByte(3, RegOpcodeFld, GetX86RegNum(ModRMReg)), OS);
65 }
66
Chris Lattner0e73c392010-02-05 06:16:07 +000067 void EmitSIBByte(unsigned SS, unsigned Index, unsigned Base,
68 raw_ostream &OS) const {
69 // SIB byte is in the same format as the ModRMByte...
70 EmitByte(ModRMByte(SS, Index, Base), OS);
71 }
72
73
Chris Lattner1ac23b12010-02-05 02:18:40 +000074 void EmitMemModRMByte(const MCInst &MI, unsigned Op,
75 unsigned RegOpcodeField, intptr_t PCAdj,
76 raw_ostream &OS) const;
Chris Lattner28249d92010-02-05 01:53:19 +000077
Chris Lattner92b1dfe2010-02-03 21:43:43 +000078 void EncodeInstruction(const MCInst &MI, raw_ostream &OS) const;
79
Chris Lattner45762472010-02-03 21:24:49 +000080};
81
82} // end anonymous namespace
83
84
Chris Lattner00cb3fe2010-02-05 21:51:35 +000085MCCodeEmitter *llvm::createX86_32MCCodeEmitter(const Target &,
86 TargetMachine &TM) {
87 return new X86MCCodeEmitter(TM, false);
88}
89
90MCCodeEmitter *llvm::createX86_64MCCodeEmitter(const Target &,
91 TargetMachine &TM) {
92 return new X86MCCodeEmitter(TM, true);
Chris Lattner92b1dfe2010-02-03 21:43:43 +000093}
94
95
Chris Lattner1ac23b12010-02-05 02:18:40 +000096/// isDisp8 - Return true if this signed displacement fits in a 8-bit
97/// sign-extended field.
98static bool isDisp8(int Value) {
99 return Value == (signed char)Value;
100}
101
Chris Lattner0e73c392010-02-05 06:16:07 +0000102void X86MCCodeEmitter::
103EmitDisplacementField(const MCOperand *RelocOp, int DispVal,
104 int64_t Adj, bool IsPCRel, raw_ostream &OS) const {
105 // If this is a simple integer displacement that doesn't require a relocation,
106 // emit it now.
107 if (!RelocOp) {
108 EmitConstant(DispVal, 4, OS);
109 return;
110 }
111
112 assert(0 && "Reloc not handled yet");
113#if 0
114 // Otherwise, this is something that requires a relocation. Emit it as such
115 // now.
116 unsigned RelocType = Is64BitMode ?
117 (IsPCRel ? X86::reloc_pcrel_word : X86::reloc_absolute_word_sext)
118 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
119 if (RelocOp->isGlobal()) {
120 // In 64-bit static small code model, we could potentially emit absolute.
121 // But it's probably not beneficial. If the MCE supports using RIP directly
122 // do it, otherwise fallback to absolute (this is determined by IsPCRel).
123 // 89 05 00 00 00 00 mov %eax,0(%rip) # PC-relative
124 // 89 04 25 00 00 00 00 mov %eax,0x0 # Absolute
125 bool Indirect = gvNeedsNonLazyPtr(*RelocOp, TM);
126 emitGlobalAddress(RelocOp->getGlobal(), RelocType, RelocOp->getOffset(),
127 Adj, Indirect);
128 } else if (RelocOp->isSymbol()) {
129 emitExternalSymbolAddress(RelocOp->getSymbolName(), RelocType);
130 } else if (RelocOp->isCPI()) {
131 emitConstPoolAddress(RelocOp->getIndex(), RelocType,
132 RelocOp->getOffset(), Adj);
133 } else {
134 assert(RelocOp->isJTI() && "Unexpected machine operand!");
135 emitJumpTableAddress(RelocOp->getIndex(), RelocType, Adj);
136 }
137#endif
138}
139
140
Chris Lattner1ac23b12010-02-05 02:18:40 +0000141void X86MCCodeEmitter::EmitMemModRMByte(const MCInst &MI, unsigned Op,
142 unsigned RegOpcodeField,
143 intptr_t PCAdj,
144 raw_ostream &OS) const {
145 const MCOperand &Op3 = MI.getOperand(Op+3);
146 int DispVal = 0;
147 const MCOperand *DispForReloc = 0;
148
149 // Figure out what sort of displacement we have to handle here.
150 if (Op3.isImm()) {
151 DispVal = Op3.getImm();
152 } else {
Chris Lattnerdaa45552010-02-05 19:04:37 +0000153 assert(0 && "relocatable operand");
Chris Lattner1ac23b12010-02-05 02:18:40 +0000154#if 0
155 if (Op3.isGlobal()) {
156 DispForReloc = &Op3;
157 } else if (Op3.isSymbol()) {
158 DispForReloc = &Op3;
159 } else if (Op3.isCPI()) {
160 if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
161 DispForReloc = &Op3;
162 } else {
163 DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex());
164 DispVal += Op3.getOffset();
165 }
166 } else {
167 assert(Op3.isJTI());
168 if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
169 DispForReloc = &Op3;
170 } else {
171 DispVal += MCE.getJumpTableEntryAddress(Op3.getIndex());
172 }
173#endif
174 }
175
176 const MCOperand &Base = MI.getOperand(Op);
Chris Lattner0e73c392010-02-05 06:16:07 +0000177 const MCOperand &Scale = MI.getOperand(Op+1);
Chris Lattner1ac23b12010-02-05 02:18:40 +0000178 const MCOperand &IndexReg = MI.getOperand(Op+2);
179 unsigned BaseReg = Base.getReg();
180
Chris Lattner0e73c392010-02-05 06:16:07 +0000181 // FIXME: Eliminate!
182 bool IsPCRel = false;
183
Chris Lattner1ac23b12010-02-05 02:18:40 +0000184 // Is a SIB byte needed?
185 // If no BaseReg, issue a RIP relative instruction only if the MCE can
186 // resolve addresses on-the-fly, otherwise use SIB (Intel Manual 2A, table
187 // 2-7) and absolute references.
188 if ((!Is64BitMode || DispForReloc || BaseReg != 0) &&
189 IndexReg.getReg() == 0 &&
190 (BaseReg == X86::RIP || (BaseReg != 0 && BaseReg != X86::ESP))) {
191 if (BaseReg == 0 || BaseReg == X86::RIP) { // Just a displacement?
192 // Emit special case [disp32] encoding
193 EmitByte(ModRMByte(0, RegOpcodeField, 5), OS);
Chris Lattner0e73c392010-02-05 06:16:07 +0000194 EmitDisplacementField(DispForReloc, DispVal, PCAdj, true, OS);
Chris Lattner1ac23b12010-02-05 02:18:40 +0000195 } else {
196 unsigned BaseRegNo = GetX86RegNum(Base);
197 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
198 // Emit simple indirect register encoding... [EAX] f.e.
199 EmitByte(ModRMByte(0, RegOpcodeField, BaseRegNo), OS);
200 } else if (!DispForReloc && isDisp8(DispVal)) {
201 // Emit the disp8 encoding... [REG+disp8]
202 EmitByte(ModRMByte(1, RegOpcodeField, BaseRegNo), OS);
203 EmitConstant(DispVal, 1, OS);
204 } else {
205 // Emit the most general non-SIB encoding: [REG+disp32]
206 EmitByte(ModRMByte(2, RegOpcodeField, BaseRegNo), OS);
Chris Lattner0e73c392010-02-05 06:16:07 +0000207 EmitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel, OS);
Chris Lattner1ac23b12010-02-05 02:18:40 +0000208 }
209 }
Chris Lattner0e73c392010-02-05 06:16:07 +0000210 return;
Chris Lattner1ac23b12010-02-05 02:18:40 +0000211 }
Chris Lattner0e73c392010-02-05 06:16:07 +0000212
213 // We need a SIB byte, so start by outputting the ModR/M byte first
214 assert(IndexReg.getReg() != X86::ESP &&
215 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
216
217 bool ForceDisp32 = false;
218 bool ForceDisp8 = false;
219 if (BaseReg == 0) {
220 // If there is no base register, we emit the special case SIB byte with
221 // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
222 EmitByte(ModRMByte(0, RegOpcodeField, 4), OS);
223 ForceDisp32 = true;
224 } else if (DispForReloc) {
225 // Emit the normal disp32 encoding.
226 EmitByte(ModRMByte(2, RegOpcodeField, 4), OS);
227 ForceDisp32 = true;
228 } else if (DispVal == 0 && BaseReg != X86::EBP) {
229 // Emit no displacement ModR/M byte
230 EmitByte(ModRMByte(0, RegOpcodeField, 4), OS);
231 } else if (isDisp8(DispVal)) {
232 // Emit the disp8 encoding.
233 EmitByte(ModRMByte(1, RegOpcodeField, 4), OS);
234 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
235 } else {
236 // Emit the normal disp32 encoding.
237 EmitByte(ModRMByte(2, RegOpcodeField, 4), OS);
238 }
239
240 // Calculate what the SS field value should be...
241 static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
242 unsigned SS = SSTable[Scale.getImm()];
243
244 if (BaseReg == 0) {
245 // Handle the SIB byte for the case where there is no base, see Intel
246 // Manual 2A, table 2-7. The displacement has already been output.
247 unsigned IndexRegNo;
248 if (IndexReg.getReg())
249 IndexRegNo = GetX86RegNum(IndexReg);
250 else // Examples: [ESP+1*<noreg>+4] or [scaled idx]+disp32 (MOD=0,BASE=5)
251 IndexRegNo = 4;
252 EmitSIBByte(SS, IndexRegNo, 5, OS);
253 } else {
254 unsigned IndexRegNo;
255 if (IndexReg.getReg())
256 IndexRegNo = GetX86RegNum(IndexReg);
257 else
258 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
259 EmitSIBByte(SS, IndexRegNo, GetX86RegNum(Base), OS);
260 }
261
262 // Do we need to output a displacement?
263 if (ForceDisp8)
264 EmitConstant(DispVal, 1, OS);
265 else if (DispVal != 0 || ForceDisp32)
266 EmitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel, OS);
Chris Lattner1ac23b12010-02-05 02:18:40 +0000267}
268
Chris Lattner92b1dfe2010-02-03 21:43:43 +0000269
270void X86MCCodeEmitter::
271EncodeInstruction(const MCInst &MI, raw_ostream &OS) const {
272 unsigned Opcode = MI.getOpcode();
273 const TargetInstrDesc &Desc = TII.get(Opcode);
Chris Lattner1e80f402010-02-03 21:57:59 +0000274 unsigned TSFlags = Desc.TSFlags;
275
276 // FIXME: We should emit the prefixes in exactly the same order as GAS does,
277 // in order to provide diffability.
278
Chris Lattner92b1dfe2010-02-03 21:43:43 +0000279 // Emit the lock opcode prefix as needed.
Chris Lattner1e80f402010-02-03 21:57:59 +0000280 if (TSFlags & X86II::LOCK)
Chris Lattner92b1dfe2010-02-03 21:43:43 +0000281 EmitByte(0xF0, OS);
282
283 // Emit segment override opcode prefix as needed.
Chris Lattner1e80f402010-02-03 21:57:59 +0000284 switch (TSFlags & X86II::SegOvrMask) {
Chris Lattner92b1dfe2010-02-03 21:43:43 +0000285 default: assert(0 && "Invalid segment!");
286 case 0: break; // No segment override!
287 case X86II::FS:
288 EmitByte(0x64, OS);
289 break;
290 case X86II::GS:
291 EmitByte(0x65, OS);
292 break;
293 }
294
Chris Lattner1e80f402010-02-03 21:57:59 +0000295 // Emit the repeat opcode prefix as needed.
296 if ((TSFlags & X86II::Op0Mask) == X86II::REP)
297 EmitByte(0xF3, OS);
Chris Lattner92b1dfe2010-02-03 21:43:43 +0000298
Chris Lattner1e80f402010-02-03 21:57:59 +0000299 // Emit the operand size opcode prefix as needed.
300 if (TSFlags & X86II::OpSize)
301 EmitByte(0x66, OS);
302
303 // Emit the address size opcode prefix as needed.
304 if (TSFlags & X86II::AdSize)
305 EmitByte(0x67, OS);
306
307 bool Need0FPrefix = false;
308 switch (TSFlags & X86II::Op0Mask) {
309 default: assert(0 && "Invalid prefix!");
310 case 0: break; // No prefix!
311 case X86II::REP: break; // already handled.
312 case X86II::TB: // Two-byte opcode prefix
313 case X86II::T8: // 0F 38
314 case X86II::TA: // 0F 3A
315 Need0FPrefix = true;
316 break;
317 case X86II::TF: // F2 0F 38
318 EmitByte(0xF2, OS);
319 Need0FPrefix = true;
320 break;
321 case X86II::XS: // F3 0F
322 EmitByte(0xF3, OS);
323 Need0FPrefix = true;
324 break;
325 case X86II::XD: // F2 0F
326 EmitByte(0xF2, OS);
327 Need0FPrefix = true;
328 break;
329 case X86II::D8: EmitByte(0xD8, OS); break;
330 case X86II::D9: EmitByte(0xD9, OS); break;
331 case X86II::DA: EmitByte(0xDA, OS); break;
332 case X86II::DB: EmitByte(0xDB, OS); break;
333 case X86II::DC: EmitByte(0xDC, OS); break;
334 case X86II::DD: EmitByte(0xDD, OS); break;
335 case X86II::DE: EmitByte(0xDE, OS); break;
336 case X86II::DF: EmitByte(0xDF, OS); break;
337 }
338
339 // Handle REX prefix.
340#if 0 // FIXME: Add in, also, can this come before F2 etc to simplify emission?
341 if (Is64BitMode) {
342 if (unsigned REX = X86InstrInfo::determineREX(MI))
343 EmitByte(0x40 | REX, OS);
344 }
345#endif
346
347 // 0x0F escape code must be emitted just before the opcode.
348 if (Need0FPrefix)
349 EmitByte(0x0F, OS);
350
351 // FIXME: Pull this up into previous switch if REX can be moved earlier.
352 switch (TSFlags & X86II::Op0Mask) {
353 case X86II::TF: // F2 0F 38
354 case X86II::T8: // 0F 38
355 EmitByte(0x38, OS);
356 break;
357 case X86II::TA: // 0F 3A
358 EmitByte(0x3A, OS);
359 break;
360 }
361
362 // If this is a two-address instruction, skip one of the register operands.
363 unsigned NumOps = Desc.getNumOperands();
364 unsigned CurOp = 0;
365 if (NumOps > 1 && Desc.getOperandConstraint(1, TOI::TIED_TO) != -1)
366 ++CurOp;
367 else if (NumOps > 2 && Desc.getOperandConstraint(NumOps-1, TOI::TIED_TO)== 0)
368 // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
369 --NumOps;
370
Chris Lattner74a21512010-02-05 19:24:13 +0000371 unsigned char BaseOpcode = X86II::getBaseOpcodeFor(TSFlags);
Chris Lattner1e80f402010-02-03 21:57:59 +0000372 switch (TSFlags & X86II::FormMask) {
Chris Lattnerbe1778f2010-02-05 21:34:18 +0000373 case X86II::MRMInitReg:
374 assert(0 && "FIXME: Remove this form when the JIT moves to MCCodeEmitter!");
Chris Lattner1ac23b12010-02-05 02:18:40 +0000375 default: errs() << "FORM: " << (TSFlags & X86II::FormMask) << "\n";
376 assert(0 && "Unknown FormMask value in X86MCCodeEmitter!");
Chris Lattner1e80f402010-02-03 21:57:59 +0000377 case X86II::RawFrm: {
378 EmitByte(BaseOpcode, OS);
379
380 if (CurOp == NumOps)
381 break;
382
Chris Lattner28249d92010-02-05 01:53:19 +0000383 assert(0 && "Unimpl RawFrm expr");
Chris Lattner1e80f402010-02-03 21:57:59 +0000384 break;
Chris Lattner1e80f402010-02-03 21:57:59 +0000385 }
Chris Lattner28249d92010-02-05 01:53:19 +0000386
387 case X86II::AddRegFrm: {
388 EmitByte(BaseOpcode + GetX86RegNum(MI.getOperand(CurOp++)),OS);
389 if (CurOp == NumOps)
390 break;
391
392 const MCOperand &MO1 = MI.getOperand(CurOp++);
393 if (MO1.isImm()) {
Chris Lattner74a21512010-02-05 19:24:13 +0000394 unsigned Size = X86II::getSizeOfImm(TSFlags);
Chris Lattner28249d92010-02-05 01:53:19 +0000395 EmitConstant(MO1.getImm(), Size, OS);
396 break;
397 }
398
399 assert(0 && "Unimpl AddRegFrm expr");
400 break;
Chris Lattner1e80f402010-02-03 21:57:59 +0000401 }
Chris Lattner28249d92010-02-05 01:53:19 +0000402
403 case X86II::MRMDestReg:
404 EmitByte(BaseOpcode, OS);
405 EmitRegModRMByte(MI.getOperand(CurOp),
406 GetX86RegNum(MI.getOperand(CurOp+1)), OS);
407 CurOp += 2;
408 if (CurOp != NumOps)
409 EmitConstant(MI.getOperand(CurOp++).getImm(),
Chris Lattner74a21512010-02-05 19:24:13 +0000410 X86II::getSizeOfImm(TSFlags), OS);
Chris Lattner28249d92010-02-05 01:53:19 +0000411 break;
Chris Lattner1ac23b12010-02-05 02:18:40 +0000412
413 case X86II::MRMDestMem:
414 EmitByte(BaseOpcode, OS);
415 EmitMemModRMByte(MI, CurOp,
416 GetX86RegNum(MI.getOperand(CurOp + X86AddrNumOperands)),
417 0, OS);
Chris Lattner82ed17e2010-02-05 19:37:31 +0000418 CurOp += X86AddrNumOperands + 1;
Chris Lattner1ac23b12010-02-05 02:18:40 +0000419 if (CurOp != NumOps)
420 EmitConstant(MI.getOperand(CurOp++).getImm(),
Chris Lattner74a21512010-02-05 19:24:13 +0000421 X86II::getSizeOfImm(TSFlags), OS);
Chris Lattner1ac23b12010-02-05 02:18:40 +0000422 break;
Chris Lattnerdaa45552010-02-05 19:04:37 +0000423
424 case X86II::MRMSrcReg:
425 EmitByte(BaseOpcode, OS);
426 EmitRegModRMByte(MI.getOperand(CurOp+1), GetX86RegNum(MI.getOperand(CurOp)),
427 OS);
428 CurOp += 2;
429 if (CurOp != NumOps)
430 EmitConstant(MI.getOperand(CurOp++).getImm(),
Chris Lattner74a21512010-02-05 19:24:13 +0000431 X86II::getSizeOfImm(TSFlags), OS);
Chris Lattnerdaa45552010-02-05 19:04:37 +0000432 break;
433
434 case X86II::MRMSrcMem: {
435 EmitByte(BaseOpcode, OS);
436
437 // FIXME: Maybe lea should have its own form? This is a horrible hack.
438 int AddrOperands;
439 if (Opcode == X86::LEA64r || Opcode == X86::LEA64_32r ||
440 Opcode == X86::LEA16r || Opcode == X86::LEA32r)
441 AddrOperands = X86AddrNumOperands - 1; // No segment register
442 else
443 AddrOperands = X86AddrNumOperands;
444
445 // FIXME: What is this actually doing?
446 intptr_t PCAdj = (CurOp + AddrOperands + 1 != NumOps) ?
Chris Lattner74a21512010-02-05 19:24:13 +0000447 X86II::getSizeOfImm(TSFlags) : 0;
Chris Lattnerdaa45552010-02-05 19:04:37 +0000448
449 EmitMemModRMByte(MI, CurOp+1, GetX86RegNum(MI.getOperand(CurOp)),
450 PCAdj, OS);
451 CurOp += AddrOperands + 1;
452 if (CurOp != NumOps)
453 EmitConstant(MI.getOperand(CurOp++).getImm(),
Chris Lattner74a21512010-02-05 19:24:13 +0000454 X86II::getSizeOfImm(TSFlags), OS);
Chris Lattnerdaa45552010-02-05 19:04:37 +0000455 break;
456 }
Chris Lattner82ed17e2010-02-05 19:37:31 +0000457
458 case X86II::MRM0r: case X86II::MRM1r:
459 case X86II::MRM2r: case X86II::MRM3r:
460 case X86II::MRM4r: case X86II::MRM5r:
461 case X86II::MRM6r: case X86II::MRM7r: {
462 EmitByte(BaseOpcode, OS);
463
464 // Special handling of lfence, mfence, monitor, and mwait.
465 // FIXME: This is terrible, they should get proper encoding bits in TSFlags.
466 if (Opcode == X86::LFENCE || Opcode == X86::MFENCE ||
467 Opcode == X86::MONITOR || Opcode == X86::MWAIT) {
468 EmitByte(ModRMByte(3, (TSFlags & X86II::FormMask)-X86II::MRM0r, 0), OS);
469
470 switch (Opcode) {
471 default: break;
472 case X86::MONITOR: EmitByte(0xC8, OS); break;
473 case X86::MWAIT: EmitByte(0xC9, OS); break;
474 }
475 } else {
476 EmitRegModRMByte(MI.getOperand(CurOp++),
477 (TSFlags & X86II::FormMask)-X86II::MRM0r,
478 OS);
479 }
480
481 if (CurOp == NumOps)
482 break;
483
484 const MCOperand &MO1 = MI.getOperand(CurOp++);
485 if (MO1.isImm()) {
486 EmitConstant(MO1.getImm(), X86II::getSizeOfImm(TSFlags), OS);
487 break;
488 }
489
490 assert(0 && "relo unimpl");
491#if 0
492 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
493 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
494 if (Opcode == X86::MOV64ri32)
495 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag?
496 if (MO1.isGlobal()) {
497 bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
498 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
499 Indirect);
500 } else if (MO1.isSymbol())
501 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
502 else if (MO1.isCPI())
503 emitConstPoolAddress(MO1.getIndex(), rt);
504 else if (MO1.isJTI())
505 emitJumpTableAddress(MO1.getIndex(), rt);
506 break;
507#endif
508 }
509 case X86II::MRM0m: case X86II::MRM1m:
510 case X86II::MRM2m: case X86II::MRM3m:
511 case X86II::MRM4m: case X86II::MRM5m:
512 case X86II::MRM6m: case X86II::MRM7m: {
513 intptr_t PCAdj = 0;
514 if (CurOp + X86AddrNumOperands != NumOps) {
515 if (MI.getOperand(CurOp+X86AddrNumOperands).isImm())
516 PCAdj = X86II::getSizeOfImm(TSFlags);
517 else
518 PCAdj = 4;
519 }
520
521 EmitByte(BaseOpcode, OS);
522 EmitMemModRMByte(MI, CurOp, (TSFlags & X86II::FormMask)-X86II::MRM0m,
523 PCAdj, OS);
524 CurOp += X86AddrNumOperands;
525
526 if (CurOp == NumOps)
527 break;
528
529 const MCOperand &MO = MI.getOperand(CurOp++);
530 if (MO.isImm()) {
531 EmitConstant(MO.getImm(), X86II::getSizeOfImm(TSFlags), OS);
532 break;
533 }
534
535 assert(0 && "relo not handled");
536#if 0
537 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
538 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
539 if (Opcode == X86::MOV64mi32)
540 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag?
541 if (MO.isGlobal()) {
542 bool Indirect = gvNeedsNonLazyPtr(MO, TM);
543 emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0,
544 Indirect);
545 } else if (MO.isSymbol())
546 emitExternalSymbolAddress(MO.getSymbolName(), rt);
547 else if (MO.isCPI())
548 emitConstPoolAddress(MO.getIndex(), rt);
549 else if (MO.isJTI())
550 emitJumpTableAddress(MO.getIndex(), rt);
551#endif
552 break;
553 }
Chris Lattner28249d92010-02-05 01:53:19 +0000554 }
555
556#ifndef NDEBUG
Chris Lattner82ed17e2010-02-05 19:37:31 +0000557 // FIXME: Verify.
558 if (/*!Desc.isVariadic() &&*/ CurOp != NumOps) {
Chris Lattner28249d92010-02-05 01:53:19 +0000559 errs() << "Cannot encode all operands of: ";
560 MI.dump();
561 errs() << '\n';
562 abort();
563 }
564#endif
Chris Lattner45762472010-02-03 21:24:49 +0000565}