blob: c29e8cf4f5ef6521567afe58b7256cb88eef067d [file] [log] [blame]
Misha Brukman29848412003-06-02 03:28:00 +00001//===-- X86/X86CodeEmitter.cpp - Convert X86 code to machine code ---------===//
Misha Brukmanc88330a2005-04-21 23:38:14 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanc88330a2005-04-21 23:38:14 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner787a9de2002-12-02 21:24:12 +00009//
10// This file contains the pass that transforms the X86 machine instructions into
Chris Lattnerd02c9eb2004-11-20 23:55:15 +000011// relocatable machine code.
Chris Lattner787a9de2002-12-02 21:24:12 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner1ef9cd42006-12-19 22:59:26 +000015#define DEBUG_TYPE "x86-emitter"
Evan Cheng11b0a5d2006-09-08 06:48:29 +000016#include "X86InstrInfo.h"
Evan Cheng880b0802008-01-05 02:26:58 +000017#include "X86JITInfo.h"
Evan Cheng11b0a5d2006-09-08 06:48:29 +000018#include "X86Subtarget.h"
Chris Lattner787a9de2002-12-02 21:24:12 +000019#include "X86TargetMachine.h"
Chris Lattnerd02c9eb2004-11-20 23:55:15 +000020#include "X86Relocations.h"
Chris Lattner8052f802002-12-03 06:34:06 +000021#include "X86.h"
Chris Lattnerfb22a852009-10-27 17:01:03 +000022#include "llvm/LLVMContext.h"
Chris Lattner787a9de2002-12-02 21:24:12 +000023#include "llvm/PassManager.h"
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000024#include "llvm/CodeGen/JITCodeEmitter.h"
Chris Lattnerd24f6332002-12-28 20:24:48 +000025#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerdb31bba2002-12-02 21:44:34 +000026#include "llvm/CodeGen/MachineInstr.h"
Nicolas Geoffray21ad4942008-02-13 18:39:37 +000027#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner45259762003-12-20 10:20:19 +000028#include "llvm/CodeGen/Passes.h"
Chris Lattner97e1b552003-10-20 03:42:58 +000029#include "llvm/Function.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000030#include "llvm/ADT/Statistic.h"
Daniel Dunbar981a71c2009-08-27 08:12:55 +000031#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000032#include "llvm/MC/MCExpr.h"
Daniel Dunbar981a71c2009-08-27 08:12:55 +000033#include "llvm/MC/MCInst.h"
Evan Cheng77c8da72008-03-14 07:13:42 +000034#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000035#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000036#include "llvm/Support/raw_ostream.h"
Evan Cheng5caed8a2006-02-18 00:57:10 +000037#include "llvm/Target/TargetOptions.h"
Chris Lattner2e7416c2003-12-12 07:11:18 +000038using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000039
Chris Lattner1ef9cd42006-12-19 22:59:26 +000040STATISTIC(NumEmitted, "Number of machine instructions emitted");
Chris Lattner3bb2a002003-06-01 23:23:50 +000041
Chris Lattner3bb2a002003-06-01 23:23:50 +000042namespace {
Chris Lattner10f605c2009-08-16 02:45:18 +000043 template<class CodeEmitter>
Nick Lewycky02d5f772009-10-25 06:33:48 +000044 class Emitter : public MachineFunctionPass {
Chris Lattnerd24f6332002-12-28 20:24:48 +000045 const X86InstrInfo *II;
Evan Cheng11b0a5d2006-09-08 06:48:29 +000046 const TargetData *TD;
Dan Gohmaneabd6472008-05-14 01:58:56 +000047 X86TargetMachine &TM;
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +000048 CodeEmitter &MCE;
Chris Lattner34adc8d2010-03-14 01:41:15 +000049 MachineModuleInfo *MMI;
Evan Cheng880b0802008-01-05 02:26:58 +000050 intptr_t PICBaseOffset;
Evan Cheng11b0a5d2006-09-08 06:48:29 +000051 bool Is64BitMode;
Evan Cheng345a00b2007-12-22 09:40:20 +000052 bool IsPIC;
Chris Lattner8052f802002-12-03 06:34:06 +000053 public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000054 static char ID;
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +000055 explicit Emitter(X86TargetMachine &tm, CodeEmitter &mce)
Owen Andersona7aed182010-08-06 18:33:48 +000056 : MachineFunctionPass(ID), II(0), TD(0), TM(tm),
Evan Cheng880b0802008-01-05 02:26:58 +000057 MCE(mce), PICBaseOffset(0), Is64BitMode(false),
Evan Cheng49ff8ec2008-01-04 10:46:51 +000058 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +000059 Emitter(X86TargetMachine &tm, CodeEmitter &mce,
Evan Cheng11b0a5d2006-09-08 06:48:29 +000060 const X86InstrInfo &ii, const TargetData &td, bool is64)
Owen Andersona7aed182010-08-06 18:33:48 +000061 : MachineFunctionPass(ID), II(&ii), TD(&td), TM(tm),
Evan Cheng880b0802008-01-05 02:26:58 +000062 MCE(mce), PICBaseOffset(0), Is64BitMode(is64),
Evan Cheng49ff8ec2008-01-04 10:46:51 +000063 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Chris Lattner787a9de2002-12-02 21:24:12 +000064
Chris Lattnerd24f6332002-12-28 20:24:48 +000065 bool runOnMachineFunction(MachineFunction &MF);
Chris Lattnerdb31bba2002-12-02 21:44:34 +000066
Chris Lattnerd06650a2002-12-15 21:13:40 +000067 virtual const char *getPassName() const {
68 return "X86 Machine Code Emitter";
69 }
70
Chris Lattner8eeb5012010-10-08 23:54:01 +000071 void emitInstruction(MachineInstr &MI, const TargetInstrDesc *Desc);
Nicolas Geoffray21ad4942008-02-13 18:39:37 +000072
73 void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman82e72322009-07-31 23:44:16 +000074 AU.setPreservesAll();
Nicolas Geoffray21ad4942008-02-13 18:39:37 +000075 AU.addRequired<MachineModuleInfo>();
76 MachineFunctionPass::getAnalysisUsage(AU);
77 }
Alkis Evlogimenos508b4592004-03-09 03:34:53 +000078
Chris Lattner8052f802002-12-03 06:34:06 +000079 private:
Nate Begeman4ca2ea52006-04-22 18:53:45 +000080 void emitPCRelativeBlockAddress(MachineBasicBlock *MBB);
Dan Gohmanbcaf6812010-04-15 01:51:59 +000081 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
Dan Gohman712886f2008-10-24 01:57:54 +000082 intptr_t Disp = 0, intptr_t PCAdj = 0,
Jeffrey Yasskin10d36042009-11-16 22:41:33 +000083 bool Indirect = false);
Evan Cheng563fcc32008-01-03 02:56:28 +000084 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
Dan Gohman712886f2008-10-24 01:57:54 +000085 void emitConstPoolAddress(unsigned CPI, unsigned Reloc, intptr_t Disp = 0,
Evan Cheng563fcc32008-01-03 02:56:28 +000086 intptr_t PCAdj = 0);
Evan Cheng345a00b2007-12-22 09:40:20 +000087 void emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Cheng563fcc32008-01-03 02:56:28 +000088 intptr_t PCAdj = 0);
Chris Lattner3bb2a002003-06-01 23:23:50 +000089
Evan Cheng11b0a5d2006-09-08 06:48:29 +000090 void emitDisplacementField(const MachineOperand *RelocOp, int DispVal,
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +000091 intptr_t Adj = 0, bool IsPCRel = true);
Chris Lattner2aef59f2006-05-04 00:42:08 +000092
Chris Lattner8052f802002-12-03 06:34:06 +000093 void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
Evan Cheng27c37022008-10-17 17:14:20 +000094 void emitRegModRMByte(unsigned RegOpcodeField);
Chris Lattner8052f802002-12-03 06:34:06 +000095 void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
Evan Cheng11b0a5d2006-09-08 06:48:29 +000096 void emitConstant(uint64_t Val, unsigned Size);
Chris Lattner8052f802002-12-03 06:34:06 +000097
98 void emitMemModRMByte(const MachineInstr &MI,
Evan Cheng11b0a5d2006-09-08 06:48:29 +000099 unsigned Op, unsigned RegOpcodeField,
Evan Cheng345a00b2007-12-22 09:40:20 +0000100 intptr_t PCAdj = 0);
Chris Lattner8052f802002-12-03 06:34:06 +0000101
Dan Gohman7a55a942008-02-08 03:29:40 +0000102 unsigned getX86RegNum(unsigned RegNo) const;
Chris Lattner787a9de2002-12-02 21:24:12 +0000103 };
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000104
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000105template<class CodeEmitter>
106 char Emitter<CodeEmitter>::ID = 0;
Chris Lattner10f605c2009-08-16 02:45:18 +0000107} // end anonymous namespace.
Chris Lattner787a9de2002-12-02 21:24:12 +0000108
Chris Lattnerd8312092005-07-11 05:17:48 +0000109/// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000110/// to the specified templated MachineCodeEmitter object.
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000111FunctionPass *llvm::createX86JITCodeEmitterPass(X86TargetMachine &TM,
112 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000113 return new Emitter<JITCodeEmitter>(TM, JCE);
Chris Lattner787a9de2002-12-02 21:24:12 +0000114}
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000115
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000116template<class CodeEmitter>
117bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner34adc8d2010-03-14 01:41:15 +0000118 MMI = &getAnalysis<MachineModuleInfo>();
119 MCE.setModuleInfo(MMI);
Nicolas Geoffray21ad4942008-02-13 18:39:37 +0000120
Dan Gohmaneabd6472008-05-14 01:58:56 +0000121 II = TM.getInstrInfo();
122 TD = TM.getTargetData();
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000123 Is64BitMode = TM.getSubtarget<X86Subtarget>().is64Bit();
Evan Cheng974722b2008-05-20 01:56:59 +0000124 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
Nicolas Geoffray21ad4942008-02-13 18:39:37 +0000125
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000126 do {
David Greenea8000352010-01-05 01:28:53 +0000127 DEBUG(dbgs() << "JITTing function '"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000128 << MF.getFunction()->getName() << "'\n");
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000129 MCE.startFunction(MF);
Chris Lattner9e689422006-05-03 17:21:32 +0000130 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
131 MBB != E; ++MBB) {
132 MCE.StartMachineBasicBlock(MBB);
Chris Lattner8eeb5012010-10-08 23:54:01 +0000133 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Evan Chengf55b7382008-01-05 00:41:47 +0000134 I != E; ++I) {
Chris Lattner03ad8852008-01-07 07:27:27 +0000135 const TargetInstrDesc &Desc = I->getDesc();
136 emitInstruction(*I, &Desc);
Evan Chengf55b7382008-01-05 00:41:47 +0000137 // MOVPC32r is basically a call plus a pop instruction.
Chris Lattner03ad8852008-01-07 07:27:27 +0000138 if (Desc.getOpcode() == X86::MOVPC32r)
Evan Chengf55b7382008-01-05 00:41:47 +0000139 emitInstruction(*I, &II->get(X86::POP32r));
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000140 ++NumEmitted; // Keep track of the # of mi's emitted
Evan Chengf55b7382008-01-05 00:41:47 +0000141 }
Chris Lattner9e689422006-05-03 17:21:32 +0000142 }
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000143 } while (MCE.finishFunction(MF));
Chris Lattner3bb2a002003-06-01 23:23:50 +0000144
Chris Lattnerdb31bba2002-12-02 21:44:34 +0000145 return false;
146}
147
Chris Lattner083be4d2010-07-22 21:05:13 +0000148/// determineREX - Determine if the MachineInstr has to be encoded with a X86-64
149/// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand
150/// size, and 3) use of X86-64 extended registers.
151static unsigned determineREX(const MachineInstr &MI) {
152 unsigned REX = 0;
153 const TargetInstrDesc &Desc = MI.getDesc();
154
155 // Pseudo instructions do not need REX prefix byte.
156 if ((Desc.TSFlags & X86II::FormMask) == X86II::Pseudo)
157 return 0;
158 if (Desc.TSFlags & X86II::REX_W)
159 REX |= 1 << 3;
160
161 unsigned NumOps = Desc.getNumOperands();
162 if (NumOps) {
163 bool isTwoAddr = NumOps > 1 &&
164 Desc.getOperandConstraint(1, TOI::TIED_TO) != -1;
165
166 // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
167 unsigned i = isTwoAddr ? 1 : 0;
168 for (unsigned e = NumOps; i != e; ++i) {
169 const MachineOperand& MO = MI.getOperand(i);
170 if (MO.isReg()) {
171 unsigned Reg = MO.getReg();
172 if (X86InstrInfo::isX86_64NonExtLowByteReg(Reg))
173 REX |= 0x40;
174 }
175 }
176
177 switch (Desc.TSFlags & X86II::FormMask) {
178 case X86II::MRMInitReg:
179 if (X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0)))
180 REX |= (1 << 0) | (1 << 2);
181 break;
182 case X86II::MRMSrcReg: {
183 if (X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0)))
184 REX |= 1 << 2;
185 i = isTwoAddr ? 2 : 1;
186 for (unsigned e = NumOps; i != e; ++i) {
187 const MachineOperand& MO = MI.getOperand(i);
188 if (X86InstrInfo::isX86_64ExtendedReg(MO))
189 REX |= 1 << 0;
190 }
191 break;
192 }
193 case X86II::MRMSrcMem: {
194 if (X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0)))
195 REX |= 1 << 2;
196 unsigned Bit = 0;
197 i = isTwoAddr ? 2 : 1;
198 for (; i != NumOps; ++i) {
199 const MachineOperand& MO = MI.getOperand(i);
200 if (MO.isReg()) {
201 if (X86InstrInfo::isX86_64ExtendedReg(MO))
202 REX |= 1 << Bit;
203 Bit++;
204 }
205 }
206 break;
207 }
208 case X86II::MRM0m: case X86II::MRM1m:
209 case X86II::MRM2m: case X86II::MRM3m:
210 case X86II::MRM4m: case X86II::MRM5m:
211 case X86II::MRM6m: case X86II::MRM7m:
212 case X86II::MRMDestMem: {
213 unsigned e = (isTwoAddr ? X86::AddrNumOperands+1 : X86::AddrNumOperands);
214 i = isTwoAddr ? 1 : 0;
215 if (NumOps > e && X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(e)))
216 REX |= 1 << 2;
217 unsigned Bit = 0;
218 for (; i != e; ++i) {
219 const MachineOperand& MO = MI.getOperand(i);
220 if (MO.isReg()) {
221 if (X86InstrInfo::isX86_64ExtendedReg(MO))
222 REX |= 1 << Bit;
223 Bit++;
224 }
225 }
226 break;
227 }
228 default: {
229 if (X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0)))
230 REX |= 1 << 0;
231 i = isTwoAddr ? 2 : 1;
232 for (unsigned e = NumOps; i != e; ++i) {
233 const MachineOperand& MO = MI.getOperand(i);
234 if (X86InstrInfo::isX86_64ExtendedReg(MO))
235 REX |= 1 << 2;
236 }
237 break;
238 }
239 }
240 }
241 return REX;
242}
243
244
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000245/// emitPCRelativeBlockAddress - This method keeps track of the information
246/// necessary to resolve the address of this block later and emits a dummy
247/// value.
Chris Lattner3bb2a002003-06-01 23:23:50 +0000248///
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000249template<class CodeEmitter>
250void Emitter<CodeEmitter>::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000251 // Remember where this reference was and where it is to so we can
252 // deal with it later.
Evan Cheng78bf1072006-07-27 18:21:10 +0000253 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
254 X86::reloc_pcrel_word, MBB));
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000255 MCE.emitWordLE(0);
Chris Lattner3bb2a002003-06-01 23:23:50 +0000256}
257
Chris Lattner3bb2a002003-06-01 23:23:50 +0000258/// emitGlobalAddress - Emit the specified address to the code stream assuming
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000259/// this is part of a "take the address of a global" instruction.
Chris Lattner3bb2a002003-06-01 23:23:50 +0000260///
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000261template<class CodeEmitter>
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000262void Emitter<CodeEmitter>::emitGlobalAddress(const GlobalValue *GV,
263 unsigned Reloc,
Dan Gohman712886f2008-10-24 01:57:54 +0000264 intptr_t Disp /* = 0 */,
265 intptr_t PCAdj /* = 0 */,
Evan Cheng9f3058f2008-11-10 01:08:07 +0000266 bool Indirect /* = false */) {
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000267 intptr_t RelocCST = Disp;
Evan Cheng563fcc32008-01-03 02:56:28 +0000268 if (Reloc == X86::reloc_picrel_word)
Evan Cheng880b0802008-01-05 02:26:58 +0000269 RelocCST = PICBaseOffset;
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000270 else if (Reloc == X86::reloc_pcrel_word)
271 RelocCST = PCAdj;
Evan Cheng9f3058f2008-11-10 01:08:07 +0000272 MachineRelocation MR = Indirect
273 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc,
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000274 const_cast<GlobalValue *>(GV),
275 RelocCST, false)
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000276 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000277 const_cast<GlobalValue *>(GV), RelocCST, false);
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000278 MCE.addRelocation(MR);
Dan Gohman712886f2008-10-24 01:57:54 +0000279 // The relocated value will be added to the displacement
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000280 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman712886f2008-10-24 01:57:54 +0000281 MCE.emitDWordLE(Disp);
282 else
283 MCE.emitWordLE((int32_t)Disp);
Chris Lattner3bb2a002003-06-01 23:23:50 +0000284}
285
Chris Lattnerd02c9eb2004-11-20 23:55:15 +0000286/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
287/// be emitted to the current location in the function, and allow it to be PC
288/// relative.
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000289template<class CodeEmitter>
290void Emitter<CodeEmitter>::emitExternalSymbolAddress(const char *ES,
291 unsigned Reloc) {
Evan Cheng880b0802008-01-05 02:26:58 +0000292 intptr_t RelocCST = (Reloc == X86::reloc_picrel_word) ? PICBaseOffset : 0;
Evan Phoenixee9d33b2010-02-04 19:56:59 +0000293
294 // X86 never needs stubs because instruction selection will always pick
295 // an instruction sequence that is large enough to hold any address
296 // to a symbol.
297 // (see X86ISelLowering.cpp, near 2039: X86TargetLowering::LowerCall)
298 bool NeedStub = false;
Chris Lattnere3a9c702006-05-03 20:30:20 +0000299 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Evan Phoenixee9d33b2010-02-04 19:56:59 +0000300 Reloc, ES, RelocCST,
301 0, NeedStub));
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000302 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman712886f2008-10-24 01:57:54 +0000303 MCE.emitDWordLE(0);
304 else
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000305 MCE.emitWordLE(0);
Chris Lattnerd02c9eb2004-11-20 23:55:15 +0000306}
Chris Lattner3bb2a002003-06-01 23:23:50 +0000307
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000308/// emitConstPoolAddress - Arrange for the address of an constant pool
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000309/// to be emitted to the current location in the function, and allow it to be PC
310/// relative.
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000311template<class CodeEmitter>
312void Emitter<CodeEmitter>::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
Dan Gohman712886f2008-10-24 01:57:54 +0000313 intptr_t Disp /* = 0 */,
Evan Cheng563fcc32008-01-03 02:56:28 +0000314 intptr_t PCAdj /* = 0 */) {
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000315 intptr_t RelocCST = 0;
Evan Cheng563fcc32008-01-03 02:56:28 +0000316 if (Reloc == X86::reloc_picrel_word)
Evan Cheng880b0802008-01-05 02:26:58 +0000317 RelocCST = PICBaseOffset;
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000318 else if (Reloc == X86::reloc_pcrel_word)
319 RelocCST = PCAdj;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000320 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000321 Reloc, CPI, RelocCST));
Dan Gohman712886f2008-10-24 01:57:54 +0000322 // The relocated value will be added to the displacement
Evan Cheng3b235aa2006-12-05 07:29:55 +0000323 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman712886f2008-10-24 01:57:54 +0000324 MCE.emitDWordLE(Disp);
325 else
326 MCE.emitWordLE((int32_t)Disp);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000327}
328
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000329/// emitJumpTableAddress - Arrange for the address of a jump table to
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000330/// be emitted to the current location in the function, and allow it to be PC
331/// relative.
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000332template<class CodeEmitter>
333void Emitter<CodeEmitter>::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Cheng563fcc32008-01-03 02:56:28 +0000334 intptr_t PCAdj /* = 0 */) {
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000335 intptr_t RelocCST = 0;
Evan Cheng563fcc32008-01-03 02:56:28 +0000336 if (Reloc == X86::reloc_picrel_word)
Evan Cheng880b0802008-01-05 02:26:58 +0000337 RelocCST = PICBaseOffset;
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000338 else if (Reloc == X86::reloc_pcrel_word)
339 RelocCST = PCAdj;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000340 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000341 Reloc, JTI, RelocCST));
Dan Gohman712886f2008-10-24 01:57:54 +0000342 // The relocated value will be added to the displacement
Evan Cheng3b235aa2006-12-05 07:29:55 +0000343 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman712886f2008-10-24 01:57:54 +0000344 MCE.emitDWordLE(0);
345 else
Evan Cheng3b235aa2006-12-05 07:29:55 +0000346 MCE.emitWordLE(0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000347}
348
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000349template<class CodeEmitter>
350unsigned Emitter<CodeEmitter>::getX86RegNum(unsigned RegNo) const {
Chris Lattner4f627ba2010-02-05 01:53:19 +0000351 return X86RegisterInfo::getX86RegNum(RegNo);
Chris Lattner8052f802002-12-03 06:34:06 +0000352}
353
354inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
355 unsigned RM) {
356 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
357 return RM | (RegOpcode << 3) | (Mod << 6);
358}
359
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000360template<class CodeEmitter>
361void Emitter<CodeEmitter>::emitRegModRMByte(unsigned ModRMReg,
362 unsigned RegOpcodeFld){
Chris Lattner8052f802002-12-03 06:34:06 +0000363 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
364}
365
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000366template<class CodeEmitter>
367void Emitter<CodeEmitter>::emitRegModRMByte(unsigned RegOpcodeFld) {
Evan Cheng27c37022008-10-17 17:14:20 +0000368 MCE.emitByte(ModRMByte(3, RegOpcodeFld, 0));
369}
370
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000371template<class CodeEmitter>
372void Emitter<CodeEmitter>::emitSIBByte(unsigned SS,
373 unsigned Index,
374 unsigned Base) {
Chris Lattner8052f802002-12-03 06:34:06 +0000375 // SIB byte is in the same format as the ModRMByte...
376 MCE.emitByte(ModRMByte(SS, Index, Base));
377}
378
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000379template<class CodeEmitter>
380void Emitter<CodeEmitter>::emitConstant(uint64_t Val, unsigned Size) {
Chris Lattner8052f802002-12-03 06:34:06 +0000381 // Output the constant in little endian byte order...
382 for (unsigned i = 0; i != Size; ++i) {
383 MCE.emitByte(Val & 255);
384 Val >>= 8;
385 }
386}
387
Chris Lattner2aef59f2006-05-04 00:42:08 +0000388/// isDisp8 - Return true if this signed displacement fits in a 8-bit
389/// sign-extended field.
Chris Lattner8052f802002-12-03 06:34:06 +0000390static bool isDisp8(int Value) {
391 return Value == (signed char)Value;
392}
393
Chris Lattner405d0242009-07-10 05:27:43 +0000394static bool gvNeedsNonLazyPtr(const MachineOperand &GVOp,
395 const TargetMachine &TM) {
Chris Lattner405d0242009-07-10 05:27:43 +0000396 // For Darwin-64, simulate the linktime GOT by using the same non-lazy-pointer
Dale Johannesend4a5e8f2008-08-12 18:23:48 +0000397 // mechanism as 32-bit mode.
Chris Lattner405d0242009-07-10 05:27:43 +0000398 if (TM.getSubtarget<X86Subtarget>().is64Bit() &&
399 !TM.getSubtarget<X86Subtarget>().isTargetDarwin())
400 return false;
401
Chris Lattnere6d25932009-07-10 06:07:08 +0000402 // Return true if this is a reference to a stub containing the address of the
403 // global, not the global itself.
Chris Lattnerca9d7842009-07-10 06:29:59 +0000404 return isGlobalStubReference(GVOp.getTargetFlags());
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000405}
406
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000407template<class CodeEmitter>
408void Emitter<CodeEmitter>::emitDisplacementField(const MachineOperand *RelocOp,
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000409 int DispVal,
410 intptr_t Adj /* = 0 */,
411 bool IsPCRel /* = true */) {
Chris Lattner2aef59f2006-05-04 00:42:08 +0000412 // If this is a simple integer displacement that doesn't require a relocation,
413 // emit it now.
414 if (!RelocOp) {
415 emitConstant(DispVal, 4);
416 return;
417 }
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000418
Chris Lattner2aef59f2006-05-04 00:42:08 +0000419 // Otherwise, this is something that requires a relocation. Emit it as such
420 // now.
Daniel Dunbarff0e6222009-09-01 22:07:06 +0000421 unsigned RelocType = Is64BitMode ?
422 (IsPCRel ? X86::reloc_pcrel_word : X86::reloc_absolute_word_sext)
423 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000424 if (RelocOp->isGlobal()) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000425 // In 64-bit static small code model, we could potentially emit absolute.
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000426 // But it's probably not beneficial. If the MCE supports using RIP directly
427 // do it, otherwise fallback to absolute (this is determined by IsPCRel).
Bill Wendling80d6b872008-02-26 10:57:23 +0000428 // 89 05 00 00 00 00 mov %eax,0(%rip) # PC-relative
429 // 89 04 25 00 00 00 00 mov %eax,0x0 # Absolute
Chris Lattner405d0242009-07-10 05:27:43 +0000430 bool Indirect = gvNeedsNonLazyPtr(*RelocOp, TM);
Daniel Dunbarff0e6222009-09-01 22:07:06 +0000431 emitGlobalAddress(RelocOp->getGlobal(), RelocType, RelocOp->getOffset(),
Jeffrey Yasskin10d36042009-11-16 22:41:33 +0000432 Adj, Indirect);
Daniel Dunbar6c384382009-09-01 22:06:53 +0000433 } else if (RelocOp->isSymbol()) {
Daniel Dunbarff0e6222009-09-01 22:07:06 +0000434 emitExternalSymbolAddress(RelocOp->getSymbolName(), RelocType);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000435 } else if (RelocOp->isCPI()) {
Daniel Dunbarff0e6222009-09-01 22:07:06 +0000436 emitConstPoolAddress(RelocOp->getIndex(), RelocType,
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000437 RelocOp->getOffset(), Adj);
Chris Lattner2aef59f2006-05-04 00:42:08 +0000438 } else {
Daniel Dunbarff0e6222009-09-01 22:07:06 +0000439 assert(RelocOp->isJTI() && "Unexpected machine operand!");
440 emitJumpTableAddress(RelocOp->getIndex(), RelocType, Adj);
Chris Lattner2aef59f2006-05-04 00:42:08 +0000441 }
442}
443
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000444template<class CodeEmitter>
445void Emitter<CodeEmitter>::emitMemModRMByte(const MachineInstr &MI,
Chris Lattner10f605c2009-08-16 02:45:18 +0000446 unsigned Op,unsigned RegOpcodeField,
447 intptr_t PCAdj) {
Chris Lattner3b789382004-10-15 04:53:13 +0000448 const MachineOperand &Op3 = MI.getOperand(Op+3);
Chris Lattner3b789382004-10-15 04:53:13 +0000449 int DispVal = 0;
Chris Lattner2aef59f2006-05-04 00:42:08 +0000450 const MachineOperand *DispForReloc = 0;
451
452 // Figure out what sort of displacement we have to handle here.
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000453 if (Op3.isGlobal()) {
Chris Lattner2aef59f2006-05-04 00:42:08 +0000454 DispForReloc = &Op3;
Daniel Dunbar6c384382009-09-01 22:06:53 +0000455 } else if (Op3.isSymbol()) {
456 DispForReloc = &Op3;
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000457 } else if (Op3.isCPI()) {
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000458 if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000459 DispForReloc = &Op3;
460 } else {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000461 DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex());
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000462 DispVal += Op3.getOffset();
463 }
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000464 } else if (Op3.isJTI()) {
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000465 if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000466 DispForReloc = &Op3;
467 } else {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000468 DispVal += MCE.getJumpTableEntryAddress(Op3.getIndex());
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000469 }
Chris Lattner3b789382004-10-15 04:53:13 +0000470 } else {
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000471 DispVal = Op3.getImm();
Chris Lattner3b789382004-10-15 04:53:13 +0000472 }
473
Chris Lattner112fd882004-10-17 07:49:45 +0000474 const MachineOperand &Base = MI.getOperand(Op);
Chris Lattner8052f802002-12-03 06:34:06 +0000475 const MachineOperand &Scale = MI.getOperand(Op+1);
476 const MachineOperand &IndexReg = MI.getOperand(Op+2);
Chris Lattner8052f802002-12-03 06:34:06 +0000477
Evan Cheng877ab552006-02-26 09:12:34 +0000478 unsigned BaseReg = Base.getReg();
Bill Wendling11740302010-04-21 00:34:04 +0000479
480 // Handle %rip relative addressing.
481 if (BaseReg == X86::RIP ||
482 (Is64BitMode && DispForReloc)) { // [disp32+RIP] in X86-64 mode
483 assert(IndexReg.getReg() == 0 && Is64BitMode &&
484 "Invalid rip-relative address");
485 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
486 emitDisplacementField(DispForReloc, DispVal, PCAdj, true);
487 return;
488 }
Chris Lattner112fd882004-10-17 07:49:45 +0000489
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000490 // Indicate that the displacement will use an pcrel or absolute reference
491 // by default. MCEs able to resolve addresses on-the-fly use pcrel by default
492 // while others, unless explicit asked to use RIP, use absolute references.
493 bool IsPCRel = MCE.earlyResolveAddresses() ? true : false;
494
Chris Lattner8052f802002-12-03 06:34:06 +0000495 // Is a SIB byte needed?
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000496 // If no BaseReg, issue a RIP relative instruction only if the MCE can
497 // resolve addresses on-the-fly, otherwise use SIB (Intel Manual 2A, table
498 // 2-7) and absolute references.
Chris Lattnerfbf1f022010-02-11 08:45:56 +0000499 unsigned BaseRegNo = -1U;
500 if (BaseReg != 0 && BaseReg != X86::RIP)
501 BaseRegNo = getX86RegNum(BaseReg);
Chris Lattner5a4ec872010-02-11 08:41:21 +0000502
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000503 if (// The SIB byte must be used if there is an index register.
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000504 IndexReg.getReg() == 0 &&
Chris Lattner5a4ec872010-02-11 08:41:21 +0000505 // The SIB byte must be used if the base is ESP/RSP/R12, all of which
506 // encode to an R/M value of 4, which indicates that a SIB byte is
507 // present.
508 BaseRegNo != N86::ESP &&
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000509 // If there is no base register and we're in 64-bit mode, we need a SIB
510 // byte to emit an addr that is just 'disp32' (the non-RIP relative form).
511 (!Is64BitMode || BaseReg != 0)) {
512 if (BaseReg == 0 || // [disp32] in X86-32 mode
513 BaseReg == X86::RIP) { // [disp32+RIP] in X86-64 mode
Chris Lattner8052f802002-12-03 06:34:06 +0000514 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000515 emitDisplacementField(DispForReloc, DispVal, PCAdj, true);
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000516 return;
Chris Lattner8052f802002-12-03 06:34:06 +0000517 }
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000518
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000519 // If the base is not EBP/ESP and there is no displacement, use simple
520 // indirect register encoding, this handles addresses like [EAX]. The
521 // encoding for [EBP] with no displacement means [disp32] so we handle it
522 // by emitting a displacement of 0 below.
523 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
524 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
525 return;
Chris Lattner8052f802002-12-03 06:34:06 +0000526 }
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000527
528 // Otherwise, if the displacement fits in a byte, encode as [REG+disp8].
529 if (!DispForReloc && isDisp8(DispVal)) {
530 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
Chris Lattner2aef59f2006-05-04 00:42:08 +0000531 emitConstant(DispVal, 1);
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000532 return;
Chris Lattner8052f802002-12-03 06:34:06 +0000533 }
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000534
535 // Otherwise, emit the most general non-SIB encoding: [REG+disp32]
536 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
537 emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
538 return;
539 }
540
541 // Otherwise we need a SIB byte, so start by outputting the ModR/M byte first.
542 assert(IndexReg.getReg() != X86::ESP &&
543 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
544
545 bool ForceDisp32 = false;
546 bool ForceDisp8 = false;
547 if (BaseReg == 0) {
548 // If there is no base register, we emit the special case SIB byte with
549 // MOD=0, BASE=4, to JUST get the index, scale, and displacement.
550 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
551 ForceDisp32 = true;
552 } else if (DispForReloc) {
553 // Emit the normal disp32 encoding.
554 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
555 ForceDisp32 = true;
Bill Wendling11740302010-04-21 00:34:04 +0000556 } else if (DispVal == 0 && BaseRegNo != N86::EBP) {
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000557 // Emit no displacement ModR/M byte
558 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
559 } else if (isDisp8(DispVal)) {
560 // Emit the disp8 encoding...
561 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
562 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
563 } else {
564 // Emit the normal disp32 encoding...
565 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
566 }
567
568 // Calculate what the SS field value should be...
569 static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
570 unsigned SS = SSTable[Scale.getImm()];
571
572 if (BaseReg == 0) {
573 // Handle the SIB byte for the case where there is no base, see Intel
574 // Manual 2A, table 2-7. The displacement has already been output.
575 unsigned IndexRegNo;
576 if (IndexReg.getReg())
577 IndexRegNo = getX86RegNum(IndexReg.getReg());
578 else // Examples: [ESP+1*<noreg>+4] or [scaled idx]+disp32 (MOD=0,BASE=5)
579 IndexRegNo = 4;
580 emitSIBByte(SS, IndexRegNo, 5);
581 } else {
582 unsigned BaseRegNo = getX86RegNum(BaseReg);
583 unsigned IndexRegNo;
584 if (IndexReg.getReg())
585 IndexRegNo = getX86RegNum(IndexReg.getReg());
586 else
587 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
588 emitSIBByte(SS, IndexRegNo, BaseRegNo);
589 }
590
591 // Do we need to output a displacement?
592 if (ForceDisp8) {
593 emitConstant(DispVal, 1);
594 } else if (DispVal != 0 || ForceDisp32) {
595 emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
Chris Lattner8052f802002-12-03 06:34:06 +0000596 }
597}
598
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000599template<class CodeEmitter>
Chris Lattner8eeb5012010-10-08 23:54:01 +0000600void Emitter<CodeEmitter>::emitInstruction(MachineInstr &MI,
Chris Lattner10f605c2009-08-16 02:45:18 +0000601 const TargetInstrDesc *Desc) {
David Greenea8000352010-01-05 01:28:53 +0000602 DEBUG(dbgs() << MI);
Evan Cheng77c8da72008-03-14 07:13:42 +0000603
Devang Patel051454a2009-10-06 02:19:11 +0000604 MCE.processDebugLoc(MI.getDebugLoc(), true);
Jeffrey Yasskinefad8e42009-07-16 21:07:26 +0000605
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000606 unsigned Opcode = Desc->Opcode;
Chris Lattnerdb31bba2002-12-02 21:44:34 +0000607
Andrew Lenharth0070dd12008-03-01 13:37:02 +0000608 // Emit the lock opcode prefix as needed.
Chris Lattner10f605c2009-08-16 02:45:18 +0000609 if (Desc->TSFlags & X86II::LOCK)
610 MCE.emitByte(0xF0);
Andrew Lenharth0070dd12008-03-01 13:37:02 +0000611
Duncan Sands23ee9812008-10-11 19:34:24 +0000612 // Emit segment override opcode prefix as needed.
Anton Korobeynikov25897772008-10-11 19:09:15 +0000613 switch (Desc->TSFlags & X86II::SegOvrMask) {
614 case X86II::FS:
615 MCE.emitByte(0x64);
616 break;
617 case X86II::GS:
618 MCE.emitByte(0x65);
619 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +0000620 default: llvm_unreachable("Invalid segment!");
Anton Korobeynikov46a9c012008-10-12 10:30:11 +0000621 case 0: break; // No segment override!
Anton Korobeynikov25897772008-10-11 19:09:15 +0000622 }
623
Chris Lattner8dc99fe2004-02-12 17:53:22 +0000624 // Emit the repeat opcode prefix as needed.
Chris Lattner10f605c2009-08-16 02:45:18 +0000625 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP)
626 MCE.emitByte(0xF3);
Chris Lattner8dc99fe2004-02-12 17:53:22 +0000627
Nate Begeman8a093362005-07-06 18:59:04 +0000628 // Emit the operand size opcode prefix as needed.
Chris Lattner10f605c2009-08-16 02:45:18 +0000629 if (Desc->TSFlags & X86II::OpSize)
630 MCE.emitByte(0x66);
Nate Begeman8a093362005-07-06 18:59:04 +0000631
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000632 // Emit the address size opcode prefix as needed.
Chris Lattner10f605c2009-08-16 02:45:18 +0000633 if (Desc->TSFlags & X86II::AdSize)
634 MCE.emitByte(0x67);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000635
636 bool Need0FPrefix = false;
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000637 switch (Desc->TSFlags & X86II::Op0Mask) {
Evan Cheng6db4b4c2008-04-03 08:53:17 +0000638 case X86II::TB: // Two-byte opcode prefix
639 case X86II::T8: // 0F 38
640 case X86II::TA: // 0F 3A
641 Need0FPrefix = true;
Bill Wendlingf0998412007-04-10 22:10:25 +0000642 break;
Eric Christopher7dfa9f22009-08-08 21:55:08 +0000643 case X86II::TF: // F2 0F 38
644 MCE.emitByte(0xF2);
645 Need0FPrefix = true;
646 break;
Evan Chengf84774e2006-02-14 21:52:51 +0000647 case X86II::REP: break; // already handled.
648 case X86II::XS: // F3 0F
649 MCE.emitByte(0xF3);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000650 Need0FPrefix = true;
Evan Chengf84774e2006-02-14 21:52:51 +0000651 break;
652 case X86II::XD: // F2 0F
653 MCE.emitByte(0xF2);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000654 Need0FPrefix = true;
Evan Chengf84774e2006-02-14 21:52:51 +0000655 break;
Chris Lattner36703cd2002-12-25 05:09:21 +0000656 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
657 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000658 MCE.emitByte(0xD8+
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000659 (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8)
Chris Lattner3b789382004-10-15 04:53:13 +0000660 >> X86II::Op0Shift));
Chris Lattner36703cd2002-12-25 05:09:21 +0000661 break; // Two-byte opcode prefix
Torok Edwinfbcc6632009-07-14 16:55:14 +0000662 default: llvm_unreachable("Invalid prefix!");
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000663 case 0: break; // No prefix!
Chris Lattner36703cd2002-12-25 05:09:21 +0000664 }
Chris Lattnerdb31bba2002-12-02 21:44:34 +0000665
Chris Lattner10f605c2009-08-16 02:45:18 +0000666 // Handle REX prefix.
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000667 if (Is64BitMode) {
Chris Lattner083be4d2010-07-22 21:05:13 +0000668 if (unsigned REX = determineREX(MI))
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000669 MCE.emitByte(0x40 | REX);
670 }
671
672 // 0x0F escape code must be emitted just before the opcode.
673 if (Need0FPrefix)
674 MCE.emitByte(0x0F);
675
Evan Cheng6db4b4c2008-04-03 08:53:17 +0000676 switch (Desc->TSFlags & X86II::Op0Mask) {
Chris Lattner10f605c2009-08-16 02:45:18 +0000677 case X86II::TF: // F2 0F 38
678 case X86II::T8: // 0F 38
Evan Cheng6db4b4c2008-04-03 08:53:17 +0000679 MCE.emitByte(0x38);
680 break;
681 case X86II::TA: // 0F 3A
682 MCE.emitByte(0x3A);
683 break;
684 }
685
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000686 // If this is a two-address instruction, skip one of the register operands.
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000687 unsigned NumOps = Desc->getNumOperands();
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000688 unsigned CurOp = 0;
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000689 if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
Evan Cheng00bd8d902008-04-18 20:55:36 +0000690 ++CurOp;
691 else if (NumOps > 2 && Desc->getOperandConstraint(NumOps-1, TOI::TIED_TO)== 0)
692 // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
693 --NumOps;
Evan Cheng3b235aa2006-12-05 07:29:55 +0000694
Chris Lattner50324352010-02-05 19:24:13 +0000695 unsigned char BaseOpcode = X86II::getBaseOpcodeFor(Desc->TSFlags);
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000696 switch (Desc->TSFlags & X86II::FormMask) {
Chris Lattner043bb022009-08-16 02:36:40 +0000697 default:
698 llvm_unreachable("Unknown FormMask value in X86 MachineCodeEmitter!");
Chris Lattner36703cd2002-12-25 05:09:21 +0000699 case X86II::Pseudo:
Evan Chengf55b7382008-01-05 00:41:47 +0000700 // Remember the current PC offset, this is the PIC relocation
701 // base address.
Chris Lattnerbe089572006-01-28 18:19:37 +0000702 switch (Opcode) {
703 default:
Gabor Greif21fed662010-08-23 20:30:51 +0000704 llvm_unreachable("pseudo instructions should be removed before code"
Chris Lattner043bb022009-08-16 02:36:40 +0000705 " emission");
Evan Cheng3bd59642008-03-05 02:34:36 +0000706 break;
Eric Christopher4d9c3402010-08-05 20:04:36 +0000707 // Do nothing for Int_MemBarrier - it's just a comment. Add a debug
708 // to make it slightly easier to see.
709 case X86::Int_MemBarrier:
710 DEBUG(dbgs() << "#MEMBARRIER\n");
711 break;
712
Chris Lattnerb06015a2010-02-09 19:54:29 +0000713 case TargetOpcode::INLINEASM:
Evan Chengdfb97382008-11-19 23:21:11 +0000714 // We allow inline assembler nodes with empty bodies - they can
715 // implicitly define registers, which is ok for JIT.
Chris Lattner0840c822009-10-12 04:22:44 +0000716 if (MI.getOperand(0).getSymbolName()[0])
Chris Lattner2104b8d2010-04-07 22:58:41 +0000717 report_fatal_error("JIT does not support inline asm!");
Evan Cheng3bd59642008-03-05 02:34:36 +0000718 break;
Bill Wendling499f7972010-07-16 22:20:36 +0000719 case TargetOpcode::PROLOG_LABEL:
Chris Lattner1065f492010-03-14 07:27:07 +0000720 case TargetOpcode::GC_LABEL:
Chris Lattneree2fbbc2010-03-14 02:33:54 +0000721 case TargetOpcode::EH_LABEL:
722 MCE.emitLabel(MI.getOperand(0).getMCSymbol());
723 break;
Eric Christopher4d9c3402010-08-05 20:04:36 +0000724
Chris Lattnerb06015a2010-02-09 19:54:29 +0000725 case TargetOpcode::IMPLICIT_DEF:
726 case TargetOpcode::KILL:
Chris Lattnerbe089572006-01-28 18:19:37 +0000727 break;
Evan Cheng880b0802008-01-05 02:26:58 +0000728 case X86::MOVPC32r: {
Evan Chengf55b7382008-01-05 00:41:47 +0000729 // This emits the "call" portion of this pseudo instruction.
730 MCE.emitByte(BaseOpcode);
Chris Lattner50324352010-02-05 19:24:13 +0000731 emitConstant(0, X86II::getSizeOfImm(Desc->TSFlags));
Evan Cheng880b0802008-01-05 02:26:58 +0000732 // Remember PIC base.
Evan Cheng0b773192008-12-10 02:32:19 +0000733 PICBaseOffset = (intptr_t) MCE.getCurrentPCOffset();
Dan Gohmaneabd6472008-05-14 01:58:56 +0000734 X86JITInfo *JTI = TM.getJITInfo();
Evan Cheng880b0802008-01-05 02:26:58 +0000735 JTI->setPICBase(MCE.getCurrentPCValue());
Evan Chengf55b7382008-01-05 00:41:47 +0000736 break;
737 }
Evan Cheng880b0802008-01-05 02:26:58 +0000738 }
Evan Cheng14140052006-11-10 01:28:43 +0000739 CurOp = NumOps;
Chris Lattner36703cd2002-12-25 05:09:21 +0000740 break;
Chris Lattner10f605c2009-08-16 02:45:18 +0000741 case X86II::RawFrm: {
Chris Lattner8052f802002-12-03 06:34:06 +0000742 MCE.emitByte(BaseOpcode);
Evan Chengf55b7382008-01-05 00:41:47 +0000743
Chris Lattner10f605c2009-08-16 02:45:18 +0000744 if (CurOp == NumOps)
745 break;
746
747 const MachineOperand &MO = MI.getOperand(CurOp++);
Bill Wendling75eeeb32008-08-21 08:38:54 +0000748
David Greenea8000352010-01-05 01:28:53 +0000749 DEBUG(dbgs() << "RawFrm CurOp " << CurOp << "\n");
750 DEBUG(dbgs() << "isMBB " << MO.isMBB() << "\n");
751 DEBUG(dbgs() << "isGlobal " << MO.isGlobal() << "\n");
752 DEBUG(dbgs() << "isSymbol " << MO.isSymbol() << "\n");
753 DEBUG(dbgs() << "isImm " << MO.isImm() << "\n");
Bill Wendling75eeeb32008-08-21 08:38:54 +0000754
Chris Lattner10f605c2009-08-16 02:45:18 +0000755 if (MO.isMBB()) {
756 emitPCRelativeBlockAddress(MO.getMBB());
757 break;
Chris Lattner8052f802002-12-03 06:34:06 +0000758 }
Chris Lattner10f605c2009-08-16 02:45:18 +0000759
760 if (MO.isGlobal()) {
Chris Lattner10f605c2009-08-16 02:45:18 +0000761 emitGlobalAddress(MO.getGlobal(), X86::reloc_pcrel_word,
Jeffrey Yasskin10d36042009-11-16 22:41:33 +0000762 MO.getOffset(), 0);
Chris Lattner10f605c2009-08-16 02:45:18 +0000763 break;
764 }
765
766 if (MO.isSymbol()) {
767 emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
768 break;
769 }
Daniel Dunbar0e42dc02010-02-09 23:00:03 +0000770
771 // FIXME: Only used by hackish MCCodeEmitter, remove when dead.
772 if (MO.isJTI()) {
773 emitJumpTableAddress(MO.getIndex(), X86::reloc_pcrel_word);
774 break;
775 }
Chris Lattner10f605c2009-08-16 02:45:18 +0000776
777 assert(MO.isImm() && "Unknown RawFrm operand!");
Anton Korobeynikov231ab842010-08-17 21:06:07 +0000778 if (Opcode == X86::CALLpcrel32 || Opcode == X86::CALL64pcrel32 ||
779 Opcode == X86::WINCALL64pcrel32) {
Chris Lattner10f605c2009-08-16 02:45:18 +0000780 // Fix up immediate operand for pc relative calls.
781 intptr_t Imm = (intptr_t)MO.getImm();
782 Imm = Imm - MCE.getCurrentPCValue() - 4;
Chris Lattner50324352010-02-05 19:24:13 +0000783 emitConstant(Imm, X86II::getSizeOfImm(Desc->TSFlags));
Chris Lattner10f605c2009-08-16 02:45:18 +0000784 } else
Chris Lattner50324352010-02-05 19:24:13 +0000785 emitConstant(MO.getImm(), X86II::getSizeOfImm(Desc->TSFlags));
Chris Lattner8052f802002-12-03 06:34:06 +0000786 break;
Chris Lattner10f605c2009-08-16 02:45:18 +0000787 }
788
Chris Lattner043bb022009-08-16 02:36:40 +0000789 case X86II::AddRegFrm: {
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000790 MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(CurOp++).getReg()));
791
Chris Lattner043bb022009-08-16 02:36:40 +0000792 if (CurOp == NumOps)
793 break;
794
795 const MachineOperand &MO1 = MI.getOperand(CurOp++);
Chris Lattner50324352010-02-05 19:24:13 +0000796 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
Chris Lattner043bb022009-08-16 02:36:40 +0000797 if (MO1.isImm()) {
798 emitConstant(MO1.getImm(), Size);
799 break;
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000800 }
Chris Lattner043bb022009-08-16 02:36:40 +0000801
802 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
803 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
804 if (Opcode == X86::MOV64ri64i32)
805 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
806 // This should not occur on Darwin for relocatable objects.
807 if (Opcode == X86::MOV64ri)
808 rt = X86::reloc_absolute_dword; // FIXME: add X86II flag?
809 if (MO1.isGlobal()) {
Chris Lattner043bb022009-08-16 02:36:40 +0000810 bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
811 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
Jeffrey Yasskin10d36042009-11-16 22:41:33 +0000812 Indirect);
Chris Lattner043bb022009-08-16 02:36:40 +0000813 } else if (MO1.isSymbol())
814 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
815 else if (MO1.isCPI())
816 emitConstPoolAddress(MO1.getIndex(), rt);
817 else if (MO1.isJTI())
818 emitJumpTableAddress(MO1.getIndex(), rt);
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000819 break;
Chris Lattner043bb022009-08-16 02:36:40 +0000820 }
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000821
822 case X86II::MRMDestReg: {
Chris Lattner8052f802002-12-03 06:34:06 +0000823 MCE.emitByte(BaseOpcode);
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000824 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
825 getX86RegNum(MI.getOperand(CurOp+1).getReg()));
826 CurOp += 2;
Evan Cheng14140052006-11-10 01:28:43 +0000827 if (CurOp != NumOps)
Chris Lattner043bb022009-08-16 02:36:40 +0000828 emitConstant(MI.getOperand(CurOp++).getImm(),
Chris Lattner50324352010-02-05 19:24:13 +0000829 X86II::getSizeOfImm(Desc->TSFlags));
Chris Lattner4b1e02d2003-05-06 21:31:47 +0000830 break;
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000831 }
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000832 case X86II::MRMDestMem: {
Chris Lattner8052f802002-12-03 06:34:06 +0000833 MCE.emitByte(BaseOpcode);
Rafael Espindolac2a17d32009-03-28 17:03:24 +0000834 emitMemModRMByte(MI, CurOp,
Chris Lattnerec536272010-07-08 22:41:28 +0000835 getX86RegNum(MI.getOperand(CurOp + X86::AddrNumOperands)
Rafael Espindolac2a17d32009-03-28 17:03:24 +0000836 .getReg()));
Chris Lattnerec536272010-07-08 22:41:28 +0000837 CurOp += X86::AddrNumOperands + 1;
Evan Cheng14140052006-11-10 01:28:43 +0000838 if (CurOp != NumOps)
Chris Lattner043bb022009-08-16 02:36:40 +0000839 emitConstant(MI.getOperand(CurOp++).getImm(),
Chris Lattner50324352010-02-05 19:24:13 +0000840 X86II::getSizeOfImm(Desc->TSFlags));
Chris Lattner8052f802002-12-03 06:34:06 +0000841 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000842 }
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000843
Chris Lattner8052f802002-12-03 06:34:06 +0000844 case X86II::MRMSrcReg:
845 MCE.emitByte(BaseOpcode);
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000846 emitRegModRMByte(MI.getOperand(CurOp+1).getReg(),
847 getX86RegNum(MI.getOperand(CurOp).getReg()));
848 CurOp += 2;
Evan Cheng14140052006-11-10 01:28:43 +0000849 if (CurOp != NumOps)
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000850 emitConstant(MI.getOperand(CurOp++).getImm(),
Chris Lattner50324352010-02-05 19:24:13 +0000851 X86II::getSizeOfImm(Desc->TSFlags));
Chris Lattner8052f802002-12-03 06:34:06 +0000852 break;
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000853
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000854 case X86II::MRMSrcMem: {
Chris Lattnerf4693072010-07-08 23:46:44 +0000855 int AddrOperands = X86::AddrNumOperands;
Rafael Espindola3b2df102009-04-08 21:14:34 +0000856
857 intptr_t PCAdj = (CurOp + AddrOperands + 1 != NumOps) ?
Chris Lattner50324352010-02-05 19:24:13 +0000858 X86II::getSizeOfImm(Desc->TSFlags) : 0;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000859
Chris Lattner8052f802002-12-03 06:34:06 +0000860 MCE.emitByte(BaseOpcode);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000861 emitMemModRMByte(MI, CurOp+1, getX86RegNum(MI.getOperand(CurOp).getReg()),
862 PCAdj);
Rafael Espindola3b2df102009-04-08 21:14:34 +0000863 CurOp += AddrOperands + 1;
Evan Cheng14140052006-11-10 01:28:43 +0000864 if (CurOp != NumOps)
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000865 emitConstant(MI.getOperand(CurOp++).getImm(),
Chris Lattner50324352010-02-05 19:24:13 +0000866 X86II::getSizeOfImm(Desc->TSFlags));
Chris Lattner8052f802002-12-03 06:34:06 +0000867 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000868 }
Chris Lattner8052f802002-12-03 06:34:06 +0000869
Alkis Evlogimenos58270fc2004-02-27 18:55:12 +0000870 case X86II::MRM0r: case X86II::MRM1r:
871 case X86II::MRM2r: case X86II::MRM3r:
872 case X86II::MRM4r: case X86II::MRM5r:
Evan Cheng27c37022008-10-17 17:14:20 +0000873 case X86II::MRM6r: case X86II::MRM7r: {
Chris Lattner8052f802002-12-03 06:34:06 +0000874 MCE.emitByte(BaseOpcode);
Chris Lattner064e9262010-02-12 23:54:57 +0000875 emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
876 (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
Chris Lattner8052f802002-12-03 06:34:06 +0000877
Chris Lattner043bb022009-08-16 02:36:40 +0000878 if (CurOp == NumOps)
879 break;
880
881 const MachineOperand &MO1 = MI.getOperand(CurOp++);
Chris Lattner50324352010-02-05 19:24:13 +0000882 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
Chris Lattner043bb022009-08-16 02:36:40 +0000883 if (MO1.isImm()) {
884 emitConstant(MO1.getImm(), Size);
885 break;
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000886 }
Chris Lattner043bb022009-08-16 02:36:40 +0000887
888 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
889 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
890 if (Opcode == X86::MOV64ri32)
891 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag?
892 if (MO1.isGlobal()) {
Chris Lattner043bb022009-08-16 02:36:40 +0000893 bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
894 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
Jeffrey Yasskin10d36042009-11-16 22:41:33 +0000895 Indirect);
Chris Lattner043bb022009-08-16 02:36:40 +0000896 } else if (MO1.isSymbol())
897 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
898 else if (MO1.isCPI())
899 emitConstPoolAddress(MO1.getIndex(), rt);
900 else if (MO1.isJTI())
901 emitJumpTableAddress(MO1.getIndex(), rt);
Chris Lattner8052f802002-12-03 06:34:06 +0000902 break;
Evan Cheng27c37022008-10-17 17:14:20 +0000903 }
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000904
Alkis Evlogimenos58270fc2004-02-27 18:55:12 +0000905 case X86II::MRM0m: case X86II::MRM1m:
906 case X86II::MRM2m: case X86II::MRM3m:
907 case X86II::MRM4m: case X86II::MRM5m:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000908 case X86II::MRM6m: case X86II::MRM7m: {
Chris Lattnerec536272010-07-08 22:41:28 +0000909 intptr_t PCAdj = (CurOp + X86::AddrNumOperands != NumOps) ?
910 (MI.getOperand(CurOp+X86::AddrNumOperands).isImm() ?
Chris Lattner50324352010-02-05 19:24:13 +0000911 X86II::getSizeOfImm(Desc->TSFlags) : 4) : 0;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000912
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000913 MCE.emitByte(BaseOpcode);
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000914 emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m,
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000915 PCAdj);
Chris Lattnerec536272010-07-08 22:41:28 +0000916 CurOp += X86::AddrNumOperands;
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000917
Chris Lattner043bb022009-08-16 02:36:40 +0000918 if (CurOp == NumOps)
919 break;
920
921 const MachineOperand &MO = MI.getOperand(CurOp++);
Chris Lattner50324352010-02-05 19:24:13 +0000922 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
Chris Lattner043bb022009-08-16 02:36:40 +0000923 if (MO.isImm()) {
924 emitConstant(MO.getImm(), Size);
925 break;
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000926 }
Chris Lattner043bb022009-08-16 02:36:40 +0000927
928 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
929 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
930 if (Opcode == X86::MOV64mi32)
931 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag?
932 if (MO.isGlobal()) {
Chris Lattner043bb022009-08-16 02:36:40 +0000933 bool Indirect = gvNeedsNonLazyPtr(MO, TM);
934 emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0,
Jeffrey Yasskin10d36042009-11-16 22:41:33 +0000935 Indirect);
Chris Lattner043bb022009-08-16 02:36:40 +0000936 } else if (MO.isSymbol())
937 emitExternalSymbolAddress(MO.getSymbolName(), rt);
938 else if (MO.isCPI())
939 emitConstPoolAddress(MO.getIndex(), rt);
940 else if (MO.isJTI())
941 emitJumpTableAddress(MO.getIndex(), rt);
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000942 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000943 }
Evan Cheng9e350cd2006-02-01 06:13:50 +0000944
945 case X86II::MRMInitReg:
946 MCE.emitByte(BaseOpcode);
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000947 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
948 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
949 getX86RegNum(MI.getOperand(CurOp).getReg()));
950 ++CurOp;
Evan Cheng9e350cd2006-02-01 06:13:50 +0000951 break;
Chris Lattnerf7477e52010-02-12 02:06:33 +0000952
953 case X86II::MRM_C1:
954 MCE.emitByte(BaseOpcode);
955 MCE.emitByte(0xC1);
956 break;
957 case X86II::MRM_C8:
958 MCE.emitByte(BaseOpcode);
959 MCE.emitByte(0xC8);
960 break;
961 case X86II::MRM_C9:
962 MCE.emitByte(BaseOpcode);
963 MCE.emitByte(0xC9);
964 break;
965 case X86II::MRM_E8:
966 MCE.emitByte(BaseOpcode);
967 MCE.emitByte(0xE8);
968 break;
969 case X86II::MRM_F0:
970 MCE.emitByte(BaseOpcode);
971 MCE.emitByte(0xF0);
972 break;
Chris Lattnerdb31bba2002-12-02 21:44:34 +0000973 }
Evan Chengac22e542006-09-06 20:24:14 +0000974
Evan Cheng801bfb22008-03-05 02:08:03 +0000975 if (!Desc->isVariadic() && CurOp != NumOps) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000976#ifndef NDEBUG
David Greenea8000352010-01-05 01:28:53 +0000977 dbgs() << "Cannot encode all operands of: " << MI << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000978#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +0000979 llvm_unreachable(0);
Evan Cheng801bfb22008-03-05 02:08:03 +0000980 }
Devang Patel051454a2009-10-06 02:19:11 +0000981
982 MCE.processDebugLoc(MI.getDebugLoc(), false);
Chris Lattnerdb31bba2002-12-02 21:44:34 +0000983}