blob: ee3de9a3f6fe4e30d41d86db86dfe1306de1dd8b [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- 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
Evan Cheng6cc775f2011-06-28 19:10:37 +000071 void emitInstruction(MachineInstr &MI, const MCInstrDesc *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 Lattner787a9de2002-12-02 21:24:12 +0000101 };
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000102
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000103template<class CodeEmitter>
104 char Emitter<CodeEmitter>::ID = 0;
Chris Lattner10f605c2009-08-16 02:45:18 +0000105} // end anonymous namespace.
Chris Lattner787a9de2002-12-02 21:24:12 +0000106
Chris Lattnerd8312092005-07-11 05:17:48 +0000107/// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000108/// to the specified templated MachineCodeEmitter object.
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000109FunctionPass *llvm::createX86JITCodeEmitterPass(X86TargetMachine &TM,
110 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000111 return new Emitter<JITCodeEmitter>(TM, JCE);
Chris Lattner787a9de2002-12-02 21:24:12 +0000112}
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000113
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000114template<class CodeEmitter>
115bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner34adc8d2010-03-14 01:41:15 +0000116 MMI = &getAnalysis<MachineModuleInfo>();
117 MCE.setModuleInfo(MMI);
Nicolas Geoffray21ad4942008-02-13 18:39:37 +0000118
Dan Gohmaneabd6472008-05-14 01:58:56 +0000119 II = TM.getInstrInfo();
120 TD = TM.getTargetData();
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000121 Is64BitMode = TM.getSubtarget<X86Subtarget>().is64Bit();
Evan Cheng974722b2008-05-20 01:56:59 +0000122 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
Nicolas Geoffray21ad4942008-02-13 18:39:37 +0000123
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000124 do {
David Greenea8000352010-01-05 01:28:53 +0000125 DEBUG(dbgs() << "JITTing function '"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000126 << MF.getFunction()->getName() << "'\n");
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000127 MCE.startFunction(MF);
Chris Lattner9e689422006-05-03 17:21:32 +0000128 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
129 MBB != E; ++MBB) {
130 MCE.StartMachineBasicBlock(MBB);
Chris Lattner8eeb5012010-10-08 23:54:01 +0000131 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Evan Chengf55b7382008-01-05 00:41:47 +0000132 I != E; ++I) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000133 const MCInstrDesc &Desc = I->getDesc();
Chris Lattner03ad8852008-01-07 07:27:27 +0000134 emitInstruction(*I, &Desc);
Evan Chengf55b7382008-01-05 00:41:47 +0000135 // MOVPC32r is basically a call plus a pop instruction.
Chris Lattner03ad8852008-01-07 07:27:27 +0000136 if (Desc.getOpcode() == X86::MOVPC32r)
Evan Chengf55b7382008-01-05 00:41:47 +0000137 emitInstruction(*I, &II->get(X86::POP32r));
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000138 ++NumEmitted; // Keep track of the # of mi's emitted
Evan Chengf55b7382008-01-05 00:41:47 +0000139 }
Chris Lattner9e689422006-05-03 17:21:32 +0000140 }
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000141 } while (MCE.finishFunction(MF));
Chris Lattner3bb2a002003-06-01 23:23:50 +0000142
Chris Lattnerdb31bba2002-12-02 21:44:34 +0000143 return false;
144}
145
Chris Lattner083be4d2010-07-22 21:05:13 +0000146/// determineREX - Determine if the MachineInstr has to be encoded with a X86-64
147/// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand
148/// size, and 3) use of X86-64 extended registers.
149static unsigned determineREX(const MachineInstr &MI) {
150 unsigned REX = 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000151 const MCInstrDesc &Desc = MI.getDesc();
Chris Lattner083be4d2010-07-22 21:05:13 +0000152
153 // Pseudo instructions do not need REX prefix byte.
154 if ((Desc.TSFlags & X86II::FormMask) == X86II::Pseudo)
155 return 0;
156 if (Desc.TSFlags & X86II::REX_W)
157 REX |= 1 << 3;
158
159 unsigned NumOps = Desc.getNumOperands();
160 if (NumOps) {
161 bool isTwoAddr = NumOps > 1 &&
Evan Cheng6cc775f2011-06-28 19:10:37 +0000162 Desc.getOperandConstraint(1, MCOI::TIED_TO) != -1;
Chris Lattner083be4d2010-07-22 21:05:13 +0000163
164 // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
165 unsigned i = isTwoAddr ? 1 : 0;
166 for (unsigned e = NumOps; i != e; ++i) {
167 const MachineOperand& MO = MI.getOperand(i);
168 if (MO.isReg()) {
169 unsigned Reg = MO.getReg();
Evan Cheng7e763d82011-07-25 18:43:53 +0000170 if (X86II::isX86_64NonExtLowByteReg(Reg))
Chris Lattner083be4d2010-07-22 21:05:13 +0000171 REX |= 0x40;
172 }
173 }
174
175 switch (Desc.TSFlags & X86II::FormMask) {
176 case X86II::MRMInitReg:
177 if (X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0)))
178 REX |= (1 << 0) | (1 << 2);
179 break;
180 case X86II::MRMSrcReg: {
181 if (X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0)))
182 REX |= 1 << 2;
183 i = isTwoAddr ? 2 : 1;
184 for (unsigned e = NumOps; i != e; ++i) {
185 const MachineOperand& MO = MI.getOperand(i);
186 if (X86InstrInfo::isX86_64ExtendedReg(MO))
187 REX |= 1 << 0;
188 }
189 break;
190 }
191 case X86II::MRMSrcMem: {
192 if (X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0)))
193 REX |= 1 << 2;
194 unsigned Bit = 0;
195 i = isTwoAddr ? 2 : 1;
196 for (; i != NumOps; ++i) {
197 const MachineOperand& MO = MI.getOperand(i);
198 if (MO.isReg()) {
199 if (X86InstrInfo::isX86_64ExtendedReg(MO))
200 REX |= 1 << Bit;
201 Bit++;
202 }
203 }
204 break;
205 }
206 case X86II::MRM0m: case X86II::MRM1m:
207 case X86II::MRM2m: case X86II::MRM3m:
208 case X86II::MRM4m: case X86II::MRM5m:
209 case X86II::MRM6m: case X86II::MRM7m:
210 case X86II::MRMDestMem: {
211 unsigned e = (isTwoAddr ? X86::AddrNumOperands+1 : X86::AddrNumOperands);
212 i = isTwoAddr ? 1 : 0;
213 if (NumOps > e && X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(e)))
214 REX |= 1 << 2;
215 unsigned Bit = 0;
216 for (; i != e; ++i) {
217 const MachineOperand& MO = MI.getOperand(i);
218 if (MO.isReg()) {
219 if (X86InstrInfo::isX86_64ExtendedReg(MO))
220 REX |= 1 << Bit;
221 Bit++;
222 }
223 }
224 break;
225 }
226 default: {
227 if (X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0)))
228 REX |= 1 << 0;
229 i = isTwoAddr ? 2 : 1;
230 for (unsigned e = NumOps; i != e; ++i) {
231 const MachineOperand& MO = MI.getOperand(i);
232 if (X86InstrInfo::isX86_64ExtendedReg(MO))
233 REX |= 1 << 2;
234 }
235 break;
236 }
237 }
238 }
239 return REX;
240}
241
242
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000243/// emitPCRelativeBlockAddress - This method keeps track of the information
244/// necessary to resolve the address of this block later and emits a dummy
245/// value.
Chris Lattner3bb2a002003-06-01 23:23:50 +0000246///
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000247template<class CodeEmitter>
248void Emitter<CodeEmitter>::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000249 // Remember where this reference was and where it is to so we can
250 // deal with it later.
Evan Cheng78bf1072006-07-27 18:21:10 +0000251 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
252 X86::reloc_pcrel_word, MBB));
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000253 MCE.emitWordLE(0);
Chris Lattner3bb2a002003-06-01 23:23:50 +0000254}
255
Chris Lattner3bb2a002003-06-01 23:23:50 +0000256/// emitGlobalAddress - Emit the specified address to the code stream assuming
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000257/// this is part of a "take the address of a global" instruction.
Chris Lattner3bb2a002003-06-01 23:23:50 +0000258///
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000259template<class CodeEmitter>
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000260void Emitter<CodeEmitter>::emitGlobalAddress(const GlobalValue *GV,
261 unsigned Reloc,
Dan Gohman712886f2008-10-24 01:57:54 +0000262 intptr_t Disp /* = 0 */,
263 intptr_t PCAdj /* = 0 */,
Evan Cheng9f3058f2008-11-10 01:08:07 +0000264 bool Indirect /* = false */) {
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000265 intptr_t RelocCST = Disp;
Evan Cheng563fcc32008-01-03 02:56:28 +0000266 if (Reloc == X86::reloc_picrel_word)
Evan Cheng880b0802008-01-05 02:26:58 +0000267 RelocCST = PICBaseOffset;
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000268 else if (Reloc == X86::reloc_pcrel_word)
269 RelocCST = PCAdj;
Evan Cheng9f3058f2008-11-10 01:08:07 +0000270 MachineRelocation MR = Indirect
271 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc,
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000272 const_cast<GlobalValue *>(GV),
273 RelocCST, false)
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000274 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000275 const_cast<GlobalValue *>(GV), RelocCST, false);
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000276 MCE.addRelocation(MR);
Dan Gohman712886f2008-10-24 01:57:54 +0000277 // The relocated value will be added to the displacement
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000278 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman712886f2008-10-24 01:57:54 +0000279 MCE.emitDWordLE(Disp);
280 else
281 MCE.emitWordLE((int32_t)Disp);
Chris Lattner3bb2a002003-06-01 23:23:50 +0000282}
283
Chris Lattnerd02c9eb2004-11-20 23:55:15 +0000284/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
285/// be emitted to the current location in the function, and allow it to be PC
286/// relative.
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000287template<class CodeEmitter>
288void Emitter<CodeEmitter>::emitExternalSymbolAddress(const char *ES,
289 unsigned Reloc) {
Evan Cheng880b0802008-01-05 02:26:58 +0000290 intptr_t RelocCST = (Reloc == X86::reloc_picrel_word) ? PICBaseOffset : 0;
Evan Phoenixee9d33b2010-02-04 19:56:59 +0000291
292 // X86 never needs stubs because instruction selection will always pick
293 // an instruction sequence that is large enough to hold any address
294 // to a symbol.
295 // (see X86ISelLowering.cpp, near 2039: X86TargetLowering::LowerCall)
296 bool NeedStub = false;
Chris Lattnere3a9c702006-05-03 20:30:20 +0000297 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Evan Phoenixee9d33b2010-02-04 19:56:59 +0000298 Reloc, ES, RelocCST,
299 0, NeedStub));
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000300 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman712886f2008-10-24 01:57:54 +0000301 MCE.emitDWordLE(0);
302 else
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000303 MCE.emitWordLE(0);
Chris Lattnerd02c9eb2004-11-20 23:55:15 +0000304}
Chris Lattner3bb2a002003-06-01 23:23:50 +0000305
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000306/// emitConstPoolAddress - Arrange for the address of an constant pool
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000307/// to be emitted to the current location in the function, and allow it to be PC
308/// relative.
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000309template<class CodeEmitter>
310void Emitter<CodeEmitter>::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
Dan Gohman712886f2008-10-24 01:57:54 +0000311 intptr_t Disp /* = 0 */,
Evan Cheng563fcc32008-01-03 02:56:28 +0000312 intptr_t PCAdj /* = 0 */) {
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000313 intptr_t RelocCST = 0;
Evan Cheng563fcc32008-01-03 02:56:28 +0000314 if (Reloc == X86::reloc_picrel_word)
Evan Cheng880b0802008-01-05 02:26:58 +0000315 RelocCST = PICBaseOffset;
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000316 else if (Reloc == X86::reloc_pcrel_word)
317 RelocCST = PCAdj;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000318 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000319 Reloc, CPI, RelocCST));
Dan Gohman712886f2008-10-24 01:57:54 +0000320 // The relocated value will be added to the displacement
Evan Cheng3b235aa2006-12-05 07:29:55 +0000321 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman712886f2008-10-24 01:57:54 +0000322 MCE.emitDWordLE(Disp);
323 else
324 MCE.emitWordLE((int32_t)Disp);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000325}
326
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000327/// emitJumpTableAddress - Arrange for the address of a jump table to
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000328/// be emitted to the current location in the function, and allow it to be PC
329/// relative.
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000330template<class CodeEmitter>
331void Emitter<CodeEmitter>::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Cheng563fcc32008-01-03 02:56:28 +0000332 intptr_t PCAdj /* = 0 */) {
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000333 intptr_t RelocCST = 0;
Evan Cheng563fcc32008-01-03 02:56:28 +0000334 if (Reloc == X86::reloc_picrel_word)
Evan Cheng880b0802008-01-05 02:26:58 +0000335 RelocCST = PICBaseOffset;
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000336 else if (Reloc == X86::reloc_pcrel_word)
337 RelocCST = PCAdj;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000338 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000339 Reloc, JTI, RelocCST));
Dan Gohman712886f2008-10-24 01:57:54 +0000340 // The relocated value will be added to the displacement
Evan Cheng3b235aa2006-12-05 07:29:55 +0000341 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman712886f2008-10-24 01:57:54 +0000342 MCE.emitDWordLE(0);
343 else
Evan Cheng3b235aa2006-12-05 07:29:55 +0000344 MCE.emitWordLE(0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000345}
346
Chris Lattner8052f802002-12-03 06:34:06 +0000347inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
348 unsigned RM) {
349 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
350 return RM | (RegOpcode << 3) | (Mod << 6);
351}
352
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000353template<class CodeEmitter>
354void Emitter<CodeEmitter>::emitRegModRMByte(unsigned ModRMReg,
355 unsigned RegOpcodeFld){
Evan Chengd60fa58b2011-07-18 20:57:22 +0000356 MCE.emitByte(ModRMByte(3, RegOpcodeFld, X86_MC::getX86RegNum(ModRMReg)));
Chris Lattner8052f802002-12-03 06:34:06 +0000357}
358
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000359template<class CodeEmitter>
360void Emitter<CodeEmitter>::emitRegModRMByte(unsigned RegOpcodeFld) {
Evan Cheng27c37022008-10-17 17:14:20 +0000361 MCE.emitByte(ModRMByte(3, RegOpcodeFld, 0));
362}
363
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000364template<class CodeEmitter>
365void Emitter<CodeEmitter>::emitSIBByte(unsigned SS,
366 unsigned Index,
367 unsigned Base) {
Chris Lattner8052f802002-12-03 06:34:06 +0000368 // SIB byte is in the same format as the ModRMByte...
369 MCE.emitByte(ModRMByte(SS, Index, Base));
370}
371
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000372template<class CodeEmitter>
373void Emitter<CodeEmitter>::emitConstant(uint64_t Val, unsigned Size) {
Chris Lattner8052f802002-12-03 06:34:06 +0000374 // Output the constant in little endian byte order...
375 for (unsigned i = 0; i != Size; ++i) {
376 MCE.emitByte(Val & 255);
377 Val >>= 8;
378 }
379}
380
Chris Lattner2aef59f2006-05-04 00:42:08 +0000381/// isDisp8 - Return true if this signed displacement fits in a 8-bit
382/// sign-extended field.
Chris Lattner8052f802002-12-03 06:34:06 +0000383static bool isDisp8(int Value) {
384 return Value == (signed char)Value;
385}
386
Chris Lattner405d0242009-07-10 05:27:43 +0000387static bool gvNeedsNonLazyPtr(const MachineOperand &GVOp,
388 const TargetMachine &TM) {
Chris Lattner405d0242009-07-10 05:27:43 +0000389 // For Darwin-64, simulate the linktime GOT by using the same non-lazy-pointer
Dale Johannesend4a5e8f2008-08-12 18:23:48 +0000390 // mechanism as 32-bit mode.
Chris Lattner405d0242009-07-10 05:27:43 +0000391 if (TM.getSubtarget<X86Subtarget>().is64Bit() &&
392 !TM.getSubtarget<X86Subtarget>().isTargetDarwin())
393 return false;
394
Chris Lattnere6d25932009-07-10 06:07:08 +0000395 // Return true if this is a reference to a stub containing the address of the
396 // global, not the global itself.
Chris Lattnerca9d7842009-07-10 06:29:59 +0000397 return isGlobalStubReference(GVOp.getTargetFlags());
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000398}
399
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000400template<class CodeEmitter>
401void Emitter<CodeEmitter>::emitDisplacementField(const MachineOperand *RelocOp,
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000402 int DispVal,
403 intptr_t Adj /* = 0 */,
404 bool IsPCRel /* = true */) {
Chris Lattner2aef59f2006-05-04 00:42:08 +0000405 // If this is a simple integer displacement that doesn't require a relocation,
406 // emit it now.
407 if (!RelocOp) {
408 emitConstant(DispVal, 4);
409 return;
410 }
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000411
Chris Lattner2aef59f2006-05-04 00:42:08 +0000412 // Otherwise, this is something that requires a relocation. Emit it as such
413 // now.
Daniel Dunbarff0e6222009-09-01 22:07:06 +0000414 unsigned RelocType = Is64BitMode ?
415 (IsPCRel ? X86::reloc_pcrel_word : X86::reloc_absolute_word_sext)
416 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000417 if (RelocOp->isGlobal()) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000418 // In 64-bit static small code model, we could potentially emit absolute.
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000419 // But it's probably not beneficial. If the MCE supports using RIP directly
420 // do it, otherwise fallback to absolute (this is determined by IsPCRel).
Bill Wendling80d6b872008-02-26 10:57:23 +0000421 // 89 05 00 00 00 00 mov %eax,0(%rip) # PC-relative
422 // 89 04 25 00 00 00 00 mov %eax,0x0 # Absolute
Chris Lattner405d0242009-07-10 05:27:43 +0000423 bool Indirect = gvNeedsNonLazyPtr(*RelocOp, TM);
Daniel Dunbarff0e6222009-09-01 22:07:06 +0000424 emitGlobalAddress(RelocOp->getGlobal(), RelocType, RelocOp->getOffset(),
Jeffrey Yasskin10d36042009-11-16 22:41:33 +0000425 Adj, Indirect);
Daniel Dunbar6c384382009-09-01 22:06:53 +0000426 } else if (RelocOp->isSymbol()) {
Daniel Dunbarff0e6222009-09-01 22:07:06 +0000427 emitExternalSymbolAddress(RelocOp->getSymbolName(), RelocType);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000428 } else if (RelocOp->isCPI()) {
Daniel Dunbarff0e6222009-09-01 22:07:06 +0000429 emitConstPoolAddress(RelocOp->getIndex(), RelocType,
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000430 RelocOp->getOffset(), Adj);
Chris Lattner2aef59f2006-05-04 00:42:08 +0000431 } else {
Daniel Dunbarff0e6222009-09-01 22:07:06 +0000432 assert(RelocOp->isJTI() && "Unexpected machine operand!");
433 emitJumpTableAddress(RelocOp->getIndex(), RelocType, Adj);
Chris Lattner2aef59f2006-05-04 00:42:08 +0000434 }
435}
436
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000437template<class CodeEmitter>
438void Emitter<CodeEmitter>::emitMemModRMByte(const MachineInstr &MI,
Chris Lattner10f605c2009-08-16 02:45:18 +0000439 unsigned Op,unsigned RegOpcodeField,
440 intptr_t PCAdj) {
Chris Lattner3b789382004-10-15 04:53:13 +0000441 const MachineOperand &Op3 = MI.getOperand(Op+3);
Chris Lattner3b789382004-10-15 04:53:13 +0000442 int DispVal = 0;
Chris Lattner2aef59f2006-05-04 00:42:08 +0000443 const MachineOperand *DispForReloc = 0;
444
445 // Figure out what sort of displacement we have to handle here.
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000446 if (Op3.isGlobal()) {
Chris Lattner2aef59f2006-05-04 00:42:08 +0000447 DispForReloc = &Op3;
Daniel Dunbar6c384382009-09-01 22:06:53 +0000448 } else if (Op3.isSymbol()) {
449 DispForReloc = &Op3;
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000450 } else if (Op3.isCPI()) {
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000451 if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000452 DispForReloc = &Op3;
453 } else {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000454 DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex());
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000455 DispVal += Op3.getOffset();
456 }
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000457 } else if (Op3.isJTI()) {
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.getJumpTableEntryAddress(Op3.getIndex());
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000462 }
Chris Lattner3b789382004-10-15 04:53:13 +0000463 } else {
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000464 DispVal = Op3.getImm();
Chris Lattner3b789382004-10-15 04:53:13 +0000465 }
466
Chris Lattner112fd882004-10-17 07:49:45 +0000467 const MachineOperand &Base = MI.getOperand(Op);
Chris Lattner8052f802002-12-03 06:34:06 +0000468 const MachineOperand &Scale = MI.getOperand(Op+1);
469 const MachineOperand &IndexReg = MI.getOperand(Op+2);
Chris Lattner8052f802002-12-03 06:34:06 +0000470
Evan Cheng877ab552006-02-26 09:12:34 +0000471 unsigned BaseReg = Base.getReg();
Bill Wendling11740302010-04-21 00:34:04 +0000472
473 // Handle %rip relative addressing.
474 if (BaseReg == X86::RIP ||
475 (Is64BitMode && DispForReloc)) { // [disp32+RIP] in X86-64 mode
476 assert(IndexReg.getReg() == 0 && Is64BitMode &&
477 "Invalid rip-relative address");
478 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
479 emitDisplacementField(DispForReloc, DispVal, PCAdj, true);
480 return;
481 }
Chris Lattner112fd882004-10-17 07:49:45 +0000482
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000483 // Indicate that the displacement will use an pcrel or absolute reference
484 // by default. MCEs able to resolve addresses on-the-fly use pcrel by default
485 // while others, unless explicit asked to use RIP, use absolute references.
486 bool IsPCRel = MCE.earlyResolveAddresses() ? true : false;
487
Chris Lattner8052f802002-12-03 06:34:06 +0000488 // Is a SIB byte needed?
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000489 // If no BaseReg, issue a RIP relative instruction only if the MCE can
490 // resolve addresses on-the-fly, otherwise use SIB (Intel Manual 2A, table
491 // 2-7) and absolute references.
Chris Lattnerfbf1f022010-02-11 08:45:56 +0000492 unsigned BaseRegNo = -1U;
493 if (BaseReg != 0 && BaseReg != X86::RIP)
Evan Chengd60fa58b2011-07-18 20:57:22 +0000494 BaseRegNo = X86_MC::getX86RegNum(BaseReg);
Chris Lattner5a4ec872010-02-11 08:41:21 +0000495
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000496 if (// The SIB byte must be used if there is an index register.
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000497 IndexReg.getReg() == 0 &&
Chris Lattner5a4ec872010-02-11 08:41:21 +0000498 // The SIB byte must be used if the base is ESP/RSP/R12, all of which
499 // encode to an R/M value of 4, which indicates that a SIB byte is
500 // present.
501 BaseRegNo != N86::ESP &&
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000502 // If there is no base register and we're in 64-bit mode, we need a SIB
503 // byte to emit an addr that is just 'disp32' (the non-RIP relative form).
504 (!Is64BitMode || BaseReg != 0)) {
505 if (BaseReg == 0 || // [disp32] in X86-32 mode
506 BaseReg == X86::RIP) { // [disp32+RIP] in X86-64 mode
Chris Lattner8052f802002-12-03 06:34:06 +0000507 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000508 emitDisplacementField(DispForReloc, DispVal, PCAdj, true);
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000509 return;
Chris Lattner8052f802002-12-03 06:34:06 +0000510 }
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000511
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000512 // If the base is not EBP/ESP and there is no displacement, use simple
513 // indirect register encoding, this handles addresses like [EAX]. The
514 // encoding for [EBP] with no displacement means [disp32] so we handle it
515 // by emitting a displacement of 0 below.
516 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
517 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
518 return;
Chris Lattner8052f802002-12-03 06:34:06 +0000519 }
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000520
521 // Otherwise, if the displacement fits in a byte, encode as [REG+disp8].
522 if (!DispForReloc && isDisp8(DispVal)) {
523 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
Chris Lattner2aef59f2006-05-04 00:42:08 +0000524 emitConstant(DispVal, 1);
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000525 return;
Chris Lattner8052f802002-12-03 06:34:06 +0000526 }
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000527
528 // Otherwise, emit the most general non-SIB encoding: [REG+disp32]
529 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
530 emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
531 return;
532 }
533
534 // Otherwise we need a SIB byte, so start by outputting the ModR/M byte first.
535 assert(IndexReg.getReg() != X86::ESP &&
536 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
537
538 bool ForceDisp32 = false;
539 bool ForceDisp8 = false;
540 if (BaseReg == 0) {
541 // If there is no base register, we emit the special case SIB byte with
542 // MOD=0, BASE=4, to JUST get the index, scale, and displacement.
543 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
544 ForceDisp32 = true;
545 } else if (DispForReloc) {
546 // Emit the normal disp32 encoding.
547 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
548 ForceDisp32 = true;
Bill Wendling11740302010-04-21 00:34:04 +0000549 } else if (DispVal == 0 && BaseRegNo != N86::EBP) {
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000550 // Emit no displacement ModR/M byte
551 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
552 } else if (isDisp8(DispVal)) {
553 // Emit the disp8 encoding...
554 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
555 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
556 } else {
557 // Emit the normal disp32 encoding...
558 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
559 }
560
561 // Calculate what the SS field value should be...
Jeffrey Yasskin6381c012011-07-27 06:22:51 +0000562 static const unsigned SSTable[] = { ~0U, 0, 1, ~0U, 2, ~0U, ~0U, ~0U, 3 };
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000563 unsigned SS = SSTable[Scale.getImm()];
564
565 if (BaseReg == 0) {
566 // Handle the SIB byte for the case where there is no base, see Intel
567 // Manual 2A, table 2-7. The displacement has already been output.
568 unsigned IndexRegNo;
569 if (IndexReg.getReg())
Evan Chengd60fa58b2011-07-18 20:57:22 +0000570 IndexRegNo = X86_MC::getX86RegNum(IndexReg.getReg());
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000571 else // Examples: [ESP+1*<noreg>+4] or [scaled idx]+disp32 (MOD=0,BASE=5)
572 IndexRegNo = 4;
573 emitSIBByte(SS, IndexRegNo, 5);
574 } else {
Evan Chengd60fa58b2011-07-18 20:57:22 +0000575 unsigned BaseRegNo = X86_MC::getX86RegNum(BaseReg);
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000576 unsigned IndexRegNo;
577 if (IndexReg.getReg())
Evan Chengd60fa58b2011-07-18 20:57:22 +0000578 IndexRegNo = X86_MC::getX86RegNum(IndexReg.getReg());
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000579 else
580 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
581 emitSIBByte(SS, IndexRegNo, BaseRegNo);
582 }
583
584 // Do we need to output a displacement?
585 if (ForceDisp8) {
586 emitConstant(DispVal, 1);
587 } else if (DispVal != 0 || ForceDisp32) {
588 emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
Chris Lattner8052f802002-12-03 06:34:06 +0000589 }
590}
591
Eli Friedmanb72d5532011-10-24 20:24:21 +0000592static const MCInstrDesc *UpdateOp(MachineInstr &MI, const X86InstrInfo *II,
593 unsigned Opcode) {
594 const MCInstrDesc *Desc = &II->get(Opcode);
595 MI.setDesc(*Desc);
596 return Desc;
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,
Evan Cheng6cc775f2011-06-28 19:10:37 +0000601 const MCInstrDesc *Desc) {
David Greenea8000352010-01-05 01:28:53 +0000602 DEBUG(dbgs() << MI);
Chris Lattnerc951cfe2010-10-08 23:59:27 +0000603
604 // If this is a pseudo instruction, lower it.
605 switch (Desc->getOpcode()) {
Eli Friedmanb72d5532011-10-24 20:24:21 +0000606 case X86::ADD16rr_DB: Desc = UpdateOp(MI, II, X86::OR16rr); break;
607 case X86::ADD32rr_DB: Desc = UpdateOp(MI, II, X86::OR32rr); break;
608 case X86::ADD64rr_DB: Desc = UpdateOp(MI, II, X86::OR64rr); break;
609 case X86::ADD16ri_DB: Desc = UpdateOp(MI, II, X86::OR16ri); break;
610 case X86::ADD32ri_DB: Desc = UpdateOp(MI, II, X86::OR32ri); break;
611 case X86::ADD64ri32_DB: Desc = UpdateOp(MI, II, X86::OR64ri32); break;
612 case X86::ADD16ri8_DB: Desc = UpdateOp(MI, II, X86::OR16ri8); break;
613 case X86::ADD32ri8_DB: Desc = UpdateOp(MI, II, X86::OR32ri8); break;
614 case X86::ADD64ri8_DB: Desc = UpdateOp(MI, II, X86::OR64ri8); break;
615 case X86::ACQUIRE_MOV8rm: Desc = UpdateOp(MI, II, X86::MOV8rm); break;
616 case X86::ACQUIRE_MOV16rm: Desc = UpdateOp(MI, II, X86::MOV16rm); break;
617 case X86::ACQUIRE_MOV32rm: Desc = UpdateOp(MI, II, X86::MOV32rm); break;
618 case X86::ACQUIRE_MOV64rm: Desc = UpdateOp(MI, II, X86::MOV64rm); break;
619 case X86::RELEASE_MOV8mr: Desc = UpdateOp(MI, II, X86::MOV8mr); break;
620 case X86::RELEASE_MOV16mr: Desc = UpdateOp(MI, II, X86::MOV16mr); break;
621 case X86::RELEASE_MOV32mr: Desc = UpdateOp(MI, II, X86::MOV32mr); break;
622 case X86::RELEASE_MOV64mr: Desc = UpdateOp(MI, II, X86::MOV64mr); break;
Chris Lattnerc951cfe2010-10-08 23:59:27 +0000623 }
624
Evan Cheng77c8da72008-03-14 07:13:42 +0000625
Devang Patel051454a2009-10-06 02:19:11 +0000626 MCE.processDebugLoc(MI.getDebugLoc(), true);
Jeffrey Yasskinefad8e42009-07-16 21:07:26 +0000627
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000628 unsigned Opcode = Desc->Opcode;
Chris Lattnerdb31bba2002-12-02 21:44:34 +0000629
Andrew Lenharth0070dd12008-03-01 13:37:02 +0000630 // Emit the lock opcode prefix as needed.
Chris Lattner10f605c2009-08-16 02:45:18 +0000631 if (Desc->TSFlags & X86II::LOCK)
632 MCE.emitByte(0xF0);
Andrew Lenharth0070dd12008-03-01 13:37:02 +0000633
Duncan Sands23ee9812008-10-11 19:34:24 +0000634 // Emit segment override opcode prefix as needed.
Anton Korobeynikov25897772008-10-11 19:09:15 +0000635 switch (Desc->TSFlags & X86II::SegOvrMask) {
Nicolas Geoffray7ea09c92011-08-28 14:52:51 +0000636 case X86II::FS:
637 MCE.emitByte(0x64);
Anton Korobeynikov25897772008-10-11 19:09:15 +0000638 break;
Nicolas Geoffray7ea09c92011-08-28 14:52:51 +0000639 case X86II::GS:
640 MCE.emitByte(0x65);
641 break;
642 default: llvm_unreachable("Invalid segment!");
643 case 0: break; // No segment override!
Anton Korobeynikov25897772008-10-11 19:09:15 +0000644 }
645
Chris Lattner8dc99fe2004-02-12 17:53:22 +0000646 // Emit the repeat opcode prefix as needed.
Chris Lattner10f605c2009-08-16 02:45:18 +0000647 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP)
648 MCE.emitByte(0xF3);
Chris Lattner8dc99fe2004-02-12 17:53:22 +0000649
Nate Begeman8a093362005-07-06 18:59:04 +0000650 // Emit the operand size opcode prefix as needed.
Chris Lattner10f605c2009-08-16 02:45:18 +0000651 if (Desc->TSFlags & X86II::OpSize)
652 MCE.emitByte(0x66);
Nate Begeman8a093362005-07-06 18:59:04 +0000653
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000654 // Emit the address size opcode prefix as needed.
Chris Lattner10f605c2009-08-16 02:45:18 +0000655 if (Desc->TSFlags & X86II::AdSize)
656 MCE.emitByte(0x67);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000657
658 bool Need0FPrefix = false;
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000659 switch (Desc->TSFlags & X86II::Op0Mask) {
Evan Cheng6db4b4c2008-04-03 08:53:17 +0000660 case X86II::TB: // Two-byte opcode prefix
661 case X86II::T8: // 0F 38
662 case X86II::TA: // 0F 3A
Joerg Sonnenbergerfc4789d2011-04-04 16:58:13 +0000663 case X86II::A6: // 0F A6
664 case X86II::A7: // 0F A7
Evan Cheng6db4b4c2008-04-03 08:53:17 +0000665 Need0FPrefix = true;
Bill Wendlingf0998412007-04-10 22:10:25 +0000666 break;
Evan Chengf84774e2006-02-14 21:52:51 +0000667 case X86II::REP: break; // already handled.
Craig Topper96fa5972011-10-16 16:50:08 +0000668 case X86II::T8XS: // F3 0F 38
Evan Chengf84774e2006-02-14 21:52:51 +0000669 case X86II::XS: // F3 0F
670 MCE.emitByte(0xF3);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000671 Need0FPrefix = true;
Evan Chengf84774e2006-02-14 21:52:51 +0000672 break;
Craig Topper96fa5972011-10-16 16:50:08 +0000673 case X86II::T8XD: // F2 0F 38
Craig Topper980d5982011-10-23 07:34:00 +0000674 case X86II::TAXD: // F2 0F 3A
Evan Chengf84774e2006-02-14 21:52:51 +0000675 case X86II::XD: // F2 0F
676 MCE.emitByte(0xF2);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000677 Need0FPrefix = true;
Evan Chengf84774e2006-02-14 21:52:51 +0000678 break;
Chris Lattner36703cd2002-12-25 05:09:21 +0000679 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
680 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000681 MCE.emitByte(0xD8+
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000682 (((Desc->TSFlags & X86II::Op0Mask)-X86II::D8)
Chris Lattner3b789382004-10-15 04:53:13 +0000683 >> X86II::Op0Shift));
Chris Lattner36703cd2002-12-25 05:09:21 +0000684 break; // Two-byte opcode prefix
Torok Edwinfbcc6632009-07-14 16:55:14 +0000685 default: llvm_unreachable("Invalid prefix!");
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000686 case 0: break; // No prefix!
Chris Lattner36703cd2002-12-25 05:09:21 +0000687 }
Chris Lattnerdb31bba2002-12-02 21:44:34 +0000688
Chris Lattner10f605c2009-08-16 02:45:18 +0000689 // Handle REX prefix.
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000690 if (Is64BitMode) {
Chris Lattner083be4d2010-07-22 21:05:13 +0000691 if (unsigned REX = determineREX(MI))
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000692 MCE.emitByte(0x40 | REX);
693 }
694
695 // 0x0F escape code must be emitted just before the opcode.
696 if (Need0FPrefix)
697 MCE.emitByte(0x0F);
698
Evan Cheng6db4b4c2008-04-03 08:53:17 +0000699 switch (Desc->TSFlags & X86II::Op0Mask) {
Craig Topper96fa5972011-10-16 16:50:08 +0000700 case X86II::T8XD: // F2 0F 38
701 case X86II::T8XS: // F3 0F 38
Chris Lattner10f605c2009-08-16 02:45:18 +0000702 case X86II::T8: // 0F 38
Evan Cheng6db4b4c2008-04-03 08:53:17 +0000703 MCE.emitByte(0x38);
704 break;
Craig Topper980d5982011-10-23 07:34:00 +0000705 case X86II::TAXD: // F2 0F 38
Evan Cheng6db4b4c2008-04-03 08:53:17 +0000706 case X86II::TA: // 0F 3A
707 MCE.emitByte(0x3A);
708 break;
Joerg Sonnenbergerfc4789d2011-04-04 16:58:13 +0000709 case X86II::A6: // 0F A6
710 MCE.emitByte(0xA6);
711 break;
712 case X86II::A7: // 0F A7
713 MCE.emitByte(0xA7);
714 break;
Evan Cheng6db4b4c2008-04-03 08:53:17 +0000715 }
716
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000717 // If this is a two-address instruction, skip one of the register operands.
Chris Lattnerb0d06b42008-01-07 03:13:06 +0000718 unsigned NumOps = Desc->getNumOperands();
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000719 unsigned CurOp = 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000720 if (NumOps > 1 && Desc->getOperandConstraint(1, MCOI::TIED_TO) != -1)
Evan Cheng00bd8d902008-04-18 20:55:36 +0000721 ++CurOp;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000722 else if (NumOps > 2 && Desc->getOperandConstraint(NumOps-1,MCOI::TIED_TO)== 0)
Evan Cheng00bd8d902008-04-18 20:55:36 +0000723 // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
724 --NumOps;
Evan Cheng3b235aa2006-12-05 07:29:55 +0000725
Chris Lattner50324352010-02-05 19:24:13 +0000726 unsigned char BaseOpcode = X86II::getBaseOpcodeFor(Desc->TSFlags);
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000727 switch (Desc->TSFlags & X86II::FormMask) {
Chris Lattner043bb022009-08-16 02:36:40 +0000728 default:
729 llvm_unreachable("Unknown FormMask value in X86 MachineCodeEmitter!");
Chris Lattner36703cd2002-12-25 05:09:21 +0000730 case X86II::Pseudo:
Evan Chengf55b7382008-01-05 00:41:47 +0000731 // Remember the current PC offset, this is the PIC relocation
732 // base address.
Chris Lattnerbe089572006-01-28 18:19:37 +0000733 switch (Opcode) {
734 default:
Gabor Greif21fed662010-08-23 20:30:51 +0000735 llvm_unreachable("pseudo instructions should be removed before code"
Chris Lattner043bb022009-08-16 02:36:40 +0000736 " emission");
Evan Cheng3bd59642008-03-05 02:34:36 +0000737 break;
Eric Christopher4d9c3402010-08-05 20:04:36 +0000738 // Do nothing for Int_MemBarrier - it's just a comment. Add a debug
739 // to make it slightly easier to see.
740 case X86::Int_MemBarrier:
741 DEBUG(dbgs() << "#MEMBARRIER\n");
742 break;
743
Chris Lattnerb06015a2010-02-09 19:54:29 +0000744 case TargetOpcode::INLINEASM:
Evan Chengdfb97382008-11-19 23:21:11 +0000745 // We allow inline assembler nodes with empty bodies - they can
746 // implicitly define registers, which is ok for JIT.
Chris Lattner0840c822009-10-12 04:22:44 +0000747 if (MI.getOperand(0).getSymbolName()[0])
Chris Lattner2104b8d2010-04-07 22:58:41 +0000748 report_fatal_error("JIT does not support inline asm!");
Evan Cheng3bd59642008-03-05 02:34:36 +0000749 break;
Bill Wendling499f7972010-07-16 22:20:36 +0000750 case TargetOpcode::PROLOG_LABEL:
Chris Lattner1065f492010-03-14 07:27:07 +0000751 case TargetOpcode::GC_LABEL:
Chris Lattneree2fbbc2010-03-14 02:33:54 +0000752 case TargetOpcode::EH_LABEL:
753 MCE.emitLabel(MI.getOperand(0).getMCSymbol());
754 break;
Eric Christopher4d9c3402010-08-05 20:04:36 +0000755
Chris Lattnerb06015a2010-02-09 19:54:29 +0000756 case TargetOpcode::IMPLICIT_DEF:
757 case TargetOpcode::KILL:
Chris Lattnerbe089572006-01-28 18:19:37 +0000758 break;
Evan Cheng880b0802008-01-05 02:26:58 +0000759 case X86::MOVPC32r: {
Evan Chengf55b7382008-01-05 00:41:47 +0000760 // This emits the "call" portion of this pseudo instruction.
761 MCE.emitByte(BaseOpcode);
Chris Lattner50324352010-02-05 19:24:13 +0000762 emitConstant(0, X86II::getSizeOfImm(Desc->TSFlags));
Evan Cheng880b0802008-01-05 02:26:58 +0000763 // Remember PIC base.
Evan Cheng0b773192008-12-10 02:32:19 +0000764 PICBaseOffset = (intptr_t) MCE.getCurrentPCOffset();
Dan Gohmaneabd6472008-05-14 01:58:56 +0000765 X86JITInfo *JTI = TM.getJITInfo();
Evan Cheng880b0802008-01-05 02:26:58 +0000766 JTI->setPICBase(MCE.getCurrentPCValue());
Evan Chengf55b7382008-01-05 00:41:47 +0000767 break;
768 }
Evan Cheng880b0802008-01-05 02:26:58 +0000769 }
Evan Cheng14140052006-11-10 01:28:43 +0000770 CurOp = NumOps;
Chris Lattner36703cd2002-12-25 05:09:21 +0000771 break;
Chris Lattner10f605c2009-08-16 02:45:18 +0000772 case X86II::RawFrm: {
Chris Lattner8052f802002-12-03 06:34:06 +0000773 MCE.emitByte(BaseOpcode);
Evan Chengf55b7382008-01-05 00:41:47 +0000774
Chris Lattner10f605c2009-08-16 02:45:18 +0000775 if (CurOp == NumOps)
776 break;
777
778 const MachineOperand &MO = MI.getOperand(CurOp++);
Bill Wendling75eeeb32008-08-21 08:38:54 +0000779
David Greenea8000352010-01-05 01:28:53 +0000780 DEBUG(dbgs() << "RawFrm CurOp " << CurOp << "\n");
781 DEBUG(dbgs() << "isMBB " << MO.isMBB() << "\n");
782 DEBUG(dbgs() << "isGlobal " << MO.isGlobal() << "\n");
783 DEBUG(dbgs() << "isSymbol " << MO.isSymbol() << "\n");
784 DEBUG(dbgs() << "isImm " << MO.isImm() << "\n");
Bill Wendling75eeeb32008-08-21 08:38:54 +0000785
Chris Lattner10f605c2009-08-16 02:45:18 +0000786 if (MO.isMBB()) {
787 emitPCRelativeBlockAddress(MO.getMBB());
788 break;
Chris Lattner8052f802002-12-03 06:34:06 +0000789 }
Chris Lattner10f605c2009-08-16 02:45:18 +0000790
791 if (MO.isGlobal()) {
Chris Lattner10f605c2009-08-16 02:45:18 +0000792 emitGlobalAddress(MO.getGlobal(), X86::reloc_pcrel_word,
Jeffrey Yasskin10d36042009-11-16 22:41:33 +0000793 MO.getOffset(), 0);
Chris Lattner10f605c2009-08-16 02:45:18 +0000794 break;
795 }
796
797 if (MO.isSymbol()) {
798 emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
799 break;
800 }
Daniel Dunbar0e42dc02010-02-09 23:00:03 +0000801
802 // FIXME: Only used by hackish MCCodeEmitter, remove when dead.
803 if (MO.isJTI()) {
804 emitJumpTableAddress(MO.getIndex(), X86::reloc_pcrel_word);
805 break;
806 }
Chris Lattner10f605c2009-08-16 02:45:18 +0000807
808 assert(MO.isImm() && "Unknown RawFrm operand!");
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +0000809 if (Opcode == X86::CALLpcrel32 || Opcode == X86::CALL64pcrel32) {
Chris Lattner10f605c2009-08-16 02:45:18 +0000810 // Fix up immediate operand for pc relative calls.
811 intptr_t Imm = (intptr_t)MO.getImm();
812 Imm = Imm - MCE.getCurrentPCValue() - 4;
Chris Lattner50324352010-02-05 19:24:13 +0000813 emitConstant(Imm, X86II::getSizeOfImm(Desc->TSFlags));
Chris Lattner10f605c2009-08-16 02:45:18 +0000814 } else
Chris Lattner50324352010-02-05 19:24:13 +0000815 emitConstant(MO.getImm(), X86II::getSizeOfImm(Desc->TSFlags));
Chris Lattner8052f802002-12-03 06:34:06 +0000816 break;
Chris Lattner10f605c2009-08-16 02:45:18 +0000817 }
818
Chris Lattner043bb022009-08-16 02:36:40 +0000819 case X86II::AddRegFrm: {
Evan Chengd60fa58b2011-07-18 20:57:22 +0000820 MCE.emitByte(BaseOpcode +
821 X86_MC::getX86RegNum(MI.getOperand(CurOp++).getReg()));
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000822
Chris Lattner043bb022009-08-16 02:36:40 +0000823 if (CurOp == NumOps)
824 break;
825
826 const MachineOperand &MO1 = MI.getOperand(CurOp++);
Chris Lattner50324352010-02-05 19:24:13 +0000827 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
Chris Lattner043bb022009-08-16 02:36:40 +0000828 if (MO1.isImm()) {
829 emitConstant(MO1.getImm(), Size);
830 break;
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000831 }
Chris Lattner043bb022009-08-16 02:36:40 +0000832
833 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
834 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
835 if (Opcode == X86::MOV64ri64i32)
836 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
837 // This should not occur on Darwin for relocatable objects.
838 if (Opcode == X86::MOV64ri)
839 rt = X86::reloc_absolute_dword; // FIXME: add X86II flag?
840 if (MO1.isGlobal()) {
Chris Lattner043bb022009-08-16 02:36:40 +0000841 bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
842 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
Jeffrey Yasskin10d36042009-11-16 22:41:33 +0000843 Indirect);
Chris Lattner043bb022009-08-16 02:36:40 +0000844 } else if (MO1.isSymbol())
845 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
846 else if (MO1.isCPI())
847 emitConstPoolAddress(MO1.getIndex(), rt);
848 else if (MO1.isJTI())
849 emitJumpTableAddress(MO1.getIndex(), rt);
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000850 break;
Chris Lattner043bb022009-08-16 02:36:40 +0000851 }
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000852
853 case X86II::MRMDestReg: {
Chris Lattner8052f802002-12-03 06:34:06 +0000854 MCE.emitByte(BaseOpcode);
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000855 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
Evan Chengd60fa58b2011-07-18 20:57:22 +0000856 X86_MC::getX86RegNum(MI.getOperand(CurOp+1).getReg()));
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000857 CurOp += 2;
Evan Cheng14140052006-11-10 01:28:43 +0000858 if (CurOp != NumOps)
Chris Lattner043bb022009-08-16 02:36:40 +0000859 emitConstant(MI.getOperand(CurOp++).getImm(),
Chris Lattner50324352010-02-05 19:24:13 +0000860 X86II::getSizeOfImm(Desc->TSFlags));
Chris Lattner4b1e02d2003-05-06 21:31:47 +0000861 break;
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000862 }
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000863 case X86II::MRMDestMem: {
Chris Lattner8052f802002-12-03 06:34:06 +0000864 MCE.emitByte(BaseOpcode);
Rafael Espindolac2a17d32009-03-28 17:03:24 +0000865 emitMemModRMByte(MI, CurOp,
Evan Chengd60fa58b2011-07-18 20:57:22 +0000866 X86_MC::getX86RegNum(MI.getOperand(CurOp + X86::AddrNumOperands)
Rafael Espindolac2a17d32009-03-28 17:03:24 +0000867 .getReg()));
Chris Lattnerec536272010-07-08 22:41:28 +0000868 CurOp += X86::AddrNumOperands + 1;
Evan Cheng14140052006-11-10 01:28:43 +0000869 if (CurOp != NumOps)
Chris Lattner043bb022009-08-16 02:36:40 +0000870 emitConstant(MI.getOperand(CurOp++).getImm(),
Chris Lattner50324352010-02-05 19:24:13 +0000871 X86II::getSizeOfImm(Desc->TSFlags));
Chris Lattner8052f802002-12-03 06:34:06 +0000872 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000873 }
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000874
Chris Lattner8052f802002-12-03 06:34:06 +0000875 case X86II::MRMSrcReg:
876 MCE.emitByte(BaseOpcode);
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000877 emitRegModRMByte(MI.getOperand(CurOp+1).getReg(),
Evan Chengd60fa58b2011-07-18 20:57:22 +0000878 X86_MC::getX86RegNum(MI.getOperand(CurOp).getReg()));
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000879 CurOp += 2;
Evan Cheng14140052006-11-10 01:28:43 +0000880 if (CurOp != NumOps)
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000881 emitConstant(MI.getOperand(CurOp++).getImm(),
Chris Lattner50324352010-02-05 19:24:13 +0000882 X86II::getSizeOfImm(Desc->TSFlags));
Chris Lattner8052f802002-12-03 06:34:06 +0000883 break;
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000884
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000885 case X86II::MRMSrcMem: {
Chris Lattnerf4693072010-07-08 23:46:44 +0000886 int AddrOperands = X86::AddrNumOperands;
Rafael Espindola3b2df102009-04-08 21:14:34 +0000887
888 intptr_t PCAdj = (CurOp + AddrOperands + 1 != NumOps) ?
Chris Lattner50324352010-02-05 19:24:13 +0000889 X86II::getSizeOfImm(Desc->TSFlags) : 0;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000890
Chris Lattner8052f802002-12-03 06:34:06 +0000891 MCE.emitByte(BaseOpcode);
Evan Chengd60fa58b2011-07-18 20:57:22 +0000892 emitMemModRMByte(MI, CurOp+1,
893 X86_MC::getX86RegNum(MI.getOperand(CurOp).getReg()),PCAdj);
Rafael Espindola3b2df102009-04-08 21:14:34 +0000894 CurOp += AddrOperands + 1;
Evan Cheng14140052006-11-10 01:28:43 +0000895 if (CurOp != NumOps)
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000896 emitConstant(MI.getOperand(CurOp++).getImm(),
Chris Lattner50324352010-02-05 19:24:13 +0000897 X86II::getSizeOfImm(Desc->TSFlags));
Chris Lattner8052f802002-12-03 06:34:06 +0000898 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000899 }
Chris Lattner8052f802002-12-03 06:34:06 +0000900
Alkis Evlogimenos58270fc2004-02-27 18:55:12 +0000901 case X86II::MRM0r: case X86II::MRM1r:
902 case X86II::MRM2r: case X86II::MRM3r:
903 case X86II::MRM4r: case X86II::MRM5r:
Evan Cheng27c37022008-10-17 17:14:20 +0000904 case X86II::MRM6r: case X86II::MRM7r: {
Chris Lattner8052f802002-12-03 06:34:06 +0000905 MCE.emitByte(BaseOpcode);
Chris Lattner064e9262010-02-12 23:54:57 +0000906 emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
907 (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
Chris Lattner8052f802002-12-03 06:34:06 +0000908
Chris Lattner043bb022009-08-16 02:36:40 +0000909 if (CurOp == NumOps)
910 break;
911
912 const MachineOperand &MO1 = MI.getOperand(CurOp++);
Chris Lattner50324352010-02-05 19:24:13 +0000913 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
Chris Lattner043bb022009-08-16 02:36:40 +0000914 if (MO1.isImm()) {
915 emitConstant(MO1.getImm(), Size);
916 break;
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000917 }
Chris Lattner043bb022009-08-16 02:36:40 +0000918
919 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
920 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
921 if (Opcode == X86::MOV64ri32)
922 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag?
923 if (MO1.isGlobal()) {
Chris Lattner043bb022009-08-16 02:36:40 +0000924 bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
925 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
Jeffrey Yasskin10d36042009-11-16 22:41:33 +0000926 Indirect);
Chris Lattner043bb022009-08-16 02:36:40 +0000927 } else if (MO1.isSymbol())
928 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
929 else if (MO1.isCPI())
930 emitConstPoolAddress(MO1.getIndex(), rt);
931 else if (MO1.isJTI())
932 emitJumpTableAddress(MO1.getIndex(), rt);
Chris Lattner8052f802002-12-03 06:34:06 +0000933 break;
Evan Cheng27c37022008-10-17 17:14:20 +0000934 }
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000935
Alkis Evlogimenos58270fc2004-02-27 18:55:12 +0000936 case X86II::MRM0m: case X86II::MRM1m:
937 case X86II::MRM2m: case X86II::MRM3m:
938 case X86II::MRM4m: case X86II::MRM5m:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000939 case X86II::MRM6m: case X86II::MRM7m: {
Chris Lattnerec536272010-07-08 22:41:28 +0000940 intptr_t PCAdj = (CurOp + X86::AddrNumOperands != NumOps) ?
941 (MI.getOperand(CurOp+X86::AddrNumOperands).isImm() ?
Chris Lattner50324352010-02-05 19:24:13 +0000942 X86II::getSizeOfImm(Desc->TSFlags) : 4) : 0;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000943
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000944 MCE.emitByte(BaseOpcode);
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000945 emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m,
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000946 PCAdj);
Chris Lattnerec536272010-07-08 22:41:28 +0000947 CurOp += X86::AddrNumOperands;
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000948
Chris Lattner043bb022009-08-16 02:36:40 +0000949 if (CurOp == NumOps)
950 break;
951
952 const MachineOperand &MO = MI.getOperand(CurOp++);
Chris Lattner50324352010-02-05 19:24:13 +0000953 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
Chris Lattner043bb022009-08-16 02:36:40 +0000954 if (MO.isImm()) {
955 emitConstant(MO.getImm(), Size);
956 break;
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000957 }
Chris Lattner043bb022009-08-16 02:36:40 +0000958
959 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
960 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
961 if (Opcode == X86::MOV64mi32)
962 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag?
963 if (MO.isGlobal()) {
Chris Lattner043bb022009-08-16 02:36:40 +0000964 bool Indirect = gvNeedsNonLazyPtr(MO, TM);
965 emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0,
Jeffrey Yasskin10d36042009-11-16 22:41:33 +0000966 Indirect);
Chris Lattner043bb022009-08-16 02:36:40 +0000967 } else if (MO.isSymbol())
968 emitExternalSymbolAddress(MO.getSymbolName(), rt);
969 else if (MO.isCPI())
970 emitConstPoolAddress(MO.getIndex(), rt);
971 else if (MO.isJTI())
972 emitJumpTableAddress(MO.getIndex(), rt);
Chris Lattnerd4ba6222003-01-13 00:33:59 +0000973 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000974 }
Evan Cheng9e350cd2006-02-01 06:13:50 +0000975
976 case X86II::MRMInitReg:
977 MCE.emitByte(BaseOpcode);
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000978 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
979 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
Evan Chengd60fa58b2011-07-18 20:57:22 +0000980 X86_MC::getX86RegNum(MI.getOperand(CurOp).getReg()));
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000981 ++CurOp;
Evan Cheng9e350cd2006-02-01 06:13:50 +0000982 break;
Chris Lattnerf7477e52010-02-12 02:06:33 +0000983
984 case X86II::MRM_C1:
985 MCE.emitByte(BaseOpcode);
986 MCE.emitByte(0xC1);
987 break;
988 case X86II::MRM_C8:
989 MCE.emitByte(BaseOpcode);
990 MCE.emitByte(0xC8);
991 break;
992 case X86II::MRM_C9:
993 MCE.emitByte(BaseOpcode);
994 MCE.emitByte(0xC9);
995 break;
996 case X86II::MRM_E8:
997 MCE.emitByte(BaseOpcode);
998 MCE.emitByte(0xE8);
999 break;
1000 case X86II::MRM_F0:
1001 MCE.emitByte(BaseOpcode);
1002 MCE.emitByte(0xF0);
1003 break;
Chris Lattnerdb31bba2002-12-02 21:44:34 +00001004 }
Evan Chengac22e542006-09-06 20:24:14 +00001005
Evan Cheng7f8e5632011-12-07 07:15:52 +00001006 if (!MI.isVariadic() && CurOp != NumOps) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00001007#ifndef NDEBUG
David Greenea8000352010-01-05 01:28:53 +00001008 dbgs() << "Cannot encode all operands of: " << MI << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00001009#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001010 llvm_unreachable(0);
Evan Cheng801bfb22008-03-05 02:08:03 +00001011 }
Devang Patel051454a2009-10-06 02:19:11 +00001012
1013 MCE.processDebugLoc(MI.getDebugLoc(), false);
Chris Lattnerdb31bba2002-12-02 21:44:34 +00001014}