blob: 5d36d936c580fc5e0eaa098620fccf1f9686707b [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "X86.h"
Evan Cheng11b0a5d2006-09-08 06:48:29 +000017#include "X86InstrInfo.h"
Evan Cheng880b0802008-01-05 02:26:58 +000018#include "X86JITInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "X86Relocations.h"
Evan Cheng11b0a5d2006-09-08 06:48:29 +000020#include "X86Subtarget.h"
Chris Lattner787a9de2002-12-02 21:24:12 +000021#include "X86TargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/ADT/Statistic.h"
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000023#include "llvm/CodeGen/JITCodeEmitter.h"
Chris Lattnerd24f6332002-12-28 20:24:48 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerdb31bba2002-12-02 21:44:34 +000025#include "llvm/CodeGen/MachineInstr.h"
Nicolas Geoffray21ad4942008-02-13 18:39:37 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner45259762003-12-20 10:20:19 +000027#include "llvm/CodeGen/Passes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/LLVMContext.h"
Daniel Dunbar981a71c2009-08-27 08:12:55 +000029#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000030#include "llvm/MC/MCExpr.h"
Daniel Dunbar981a71c2009-08-27 08:12:55 +000031#include "llvm/MC/MCInst.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/PassManager.h"
Evan Cheng77c8da72008-03-14 07:13:42 +000033#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000034#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000035#include "llvm/Support/raw_ostream.h"
Evan Cheng5caed8a2006-02-18 00:57:10 +000036#include "llvm/Target/TargetOptions.h"
Chris Lattner2e7416c2003-12-12 07:11:18 +000037using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000038
Chris Lattner1ef9cd42006-12-19 22:59:26 +000039STATISTIC(NumEmitted, "Number of machine instructions emitted");
Chris Lattner3bb2a002003-06-01 23:23:50 +000040
Chris Lattner3bb2a002003-06-01 23:23:50 +000041namespace {
Chris Lattner10f605c2009-08-16 02:45:18 +000042 template<class CodeEmitter>
Nick Lewycky02d5f772009-10-25 06:33:48 +000043 class Emitter : public MachineFunctionPass {
Chris Lattnerd24f6332002-12-28 20:24:48 +000044 const X86InstrInfo *II;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000045 const DataLayout *TD;
Dan Gohmaneabd6472008-05-14 01:58:56 +000046 X86TargetMachine &TM;
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +000047 CodeEmitter &MCE;
Chris Lattner34adc8d2010-03-14 01:41:15 +000048 MachineModuleInfo *MMI;
Evan Cheng880b0802008-01-05 02:26:58 +000049 intptr_t PICBaseOffset;
Evan Cheng11b0a5d2006-09-08 06:48:29 +000050 bool Is64BitMode;
Evan Cheng345a00b2007-12-22 09:40:20 +000051 bool IsPIC;
Chris Lattner8052f802002-12-03 06:34:06 +000052 public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000053 static char ID;
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +000054 explicit Emitter(X86TargetMachine &tm, CodeEmitter &mce)
Jakub Staszak33938022012-05-01 23:04:38 +000055 : MachineFunctionPass(ID), II(0), TD(0), TM(tm),
Bill Wendling52ca4472013-06-07 20:59:31 +000056 MCE(mce), PICBaseOffset(0), Is64BitMode(false),
57 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Chris Lattner787a9de2002-12-02 21:24:12 +000058
Chris Lattnerd24f6332002-12-28 20:24:48 +000059 bool runOnMachineFunction(MachineFunction &MF);
Chris Lattnerdb31bba2002-12-02 21:44:34 +000060
Chris Lattnerd06650a2002-12-15 21:13:40 +000061 virtual const char *getPassName() const {
62 return "X86 Machine Code Emitter";
63 }
64
Pete Cooperf76b5fe2012-04-30 03:56:44 +000065 void emitOpcodePrefix(uint64_t TSFlags, int MemOperand,
66 const MachineInstr &MI,
67 const MCInstrDesc *Desc) const;
68
69 void emitVEXOpcodePrefix(uint64_t TSFlags, int MemOperand,
70 const MachineInstr &MI,
71 const MCInstrDesc *Desc) const;
72
73 void emitSegmentOverridePrefix(uint64_t TSFlags,
74 int MemOperand,
75 const MachineInstr &MI) const;
76
Evan Cheng6cc775f2011-06-28 19:10:37 +000077 void emitInstruction(MachineInstr &MI, const MCInstrDesc *Desc);
Jakub Staszak33938022012-05-01 23:04:38 +000078
Nicolas Geoffray21ad4942008-02-13 18:39:37 +000079 void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman82e72322009-07-31 23:44:16 +000080 AU.setPreservesAll();
Nicolas Geoffray21ad4942008-02-13 18:39:37 +000081 AU.addRequired<MachineModuleInfo>();
82 MachineFunctionPass::getAnalysisUsage(AU);
83 }
Alkis Evlogimenos508b4592004-03-09 03:34:53 +000084
Chris Lattner8052f802002-12-03 06:34:06 +000085 private:
Nate Begeman4ca2ea52006-04-22 18:53:45 +000086 void emitPCRelativeBlockAddress(MachineBasicBlock *MBB);
Dan Gohmanbcaf6812010-04-15 01:51:59 +000087 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
Dan Gohman712886f2008-10-24 01:57:54 +000088 intptr_t Disp = 0, intptr_t PCAdj = 0,
Jeffrey Yasskin10d36042009-11-16 22:41:33 +000089 bool Indirect = false);
Evan Cheng563fcc32008-01-03 02:56:28 +000090 void emitExternalSymbolAddress(const char *ES, unsigned Reloc);
Dan Gohman712886f2008-10-24 01:57:54 +000091 void emitConstPoolAddress(unsigned CPI, unsigned Reloc, intptr_t Disp = 0,
Evan Cheng563fcc32008-01-03 02:56:28 +000092 intptr_t PCAdj = 0);
Evan Cheng345a00b2007-12-22 09:40:20 +000093 void emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Cheng563fcc32008-01-03 02:56:28 +000094 intptr_t PCAdj = 0);
Chris Lattner3bb2a002003-06-01 23:23:50 +000095
Evan Cheng11b0a5d2006-09-08 06:48:29 +000096 void emitDisplacementField(const MachineOperand *RelocOp, int DispVal,
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +000097 intptr_t Adj = 0, bool IsPCRel = true);
Chris Lattner2aef59f2006-05-04 00:42:08 +000098
Chris Lattner8052f802002-12-03 06:34:06 +000099 void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
Evan Cheng27c37022008-10-17 17:14:20 +0000100 void emitRegModRMByte(unsigned RegOpcodeField);
Chris Lattner8052f802002-12-03 06:34:06 +0000101 void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000102 void emitConstant(uint64_t Val, unsigned Size);
Chris Lattner8052f802002-12-03 06:34:06 +0000103
104 void emitMemModRMByte(const MachineInstr &MI,
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000105 unsigned Op, unsigned RegOpcodeField,
Evan Cheng345a00b2007-12-22 09:40:20 +0000106 intptr_t PCAdj = 0);
Michael Liaof54249b2012-10-04 19:50:43 +0000107
108 unsigned getX86RegNum(unsigned RegNo) const {
109 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
110 return TRI->getEncodingValue(RegNo) & 0x7;
111 }
112
113 unsigned char getVEXRegisterEncoding(const MachineInstr &MI,
114 unsigned OpNum) const;
Chris Lattner787a9de2002-12-02 21:24:12 +0000115 };
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000116
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000117template<class CodeEmitter>
118 char Emitter<CodeEmitter>::ID = 0;
Chris Lattner10f605c2009-08-16 02:45:18 +0000119} // end anonymous namespace.
Chris Lattner787a9de2002-12-02 21:24:12 +0000120
Chris Lattnerd8312092005-07-11 05:17:48 +0000121/// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
Eli Bendersky530a3bc52013-02-05 16:53:11 +0000122/// to the specified JITCodeEmitter object.
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000123FunctionPass *llvm::createX86JITCodeEmitterPass(X86TargetMachine &TM,
124 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000125 return new Emitter<JITCodeEmitter>(TM, JCE);
Chris Lattner787a9de2002-12-02 21:24:12 +0000126}
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000127
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000128template<class CodeEmitter>
129bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner34adc8d2010-03-14 01:41:15 +0000130 MMI = &getAnalysis<MachineModuleInfo>();
131 MCE.setModuleInfo(MMI);
Jakub Staszak33938022012-05-01 23:04:38 +0000132
Dan Gohmaneabd6472008-05-14 01:58:56 +0000133 II = TM.getInstrInfo();
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000134 TD = TM.getDataLayout();
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000135 Is64BitMode = TM.getSubtarget<X86Subtarget>().is64Bit();
Evan Cheng974722b2008-05-20 01:56:59 +0000136 IsPIC = TM.getRelocationModel() == Reloc::PIC_;
Jakub Staszak33938022012-05-01 23:04:38 +0000137
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000138 do {
Craig Toppera538d832012-08-22 06:07:19 +0000139 DEBUG(dbgs() << "JITTing function '" << MF.getName() << "'\n");
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000140 MCE.startFunction(MF);
Jakub Staszak33938022012-05-01 23:04:38 +0000141 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
Chris Lattner9e689422006-05-03 17:21:32 +0000142 MBB != E; ++MBB) {
143 MCE.StartMachineBasicBlock(MBB);
Chris Lattner8eeb5012010-10-08 23:54:01 +0000144 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Evan Chengf55b7382008-01-05 00:41:47 +0000145 I != E; ++I) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000146 const MCInstrDesc &Desc = I->getDesc();
Chris Lattner03ad8852008-01-07 07:27:27 +0000147 emitInstruction(*I, &Desc);
Evan Chengf55b7382008-01-05 00:41:47 +0000148 // MOVPC32r is basically a call plus a pop instruction.
Chris Lattner03ad8852008-01-07 07:27:27 +0000149 if (Desc.getOpcode() == X86::MOVPC32r)
Evan Chengf55b7382008-01-05 00:41:47 +0000150 emitInstruction(*I, &II->get(X86::POP32r));
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000151 ++NumEmitted; // Keep track of the # of mi's emitted
Evan Chengf55b7382008-01-05 00:41:47 +0000152 }
Chris Lattner9e689422006-05-03 17:21:32 +0000153 }
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000154 } while (MCE.finishFunction(MF));
Chris Lattner3bb2a002003-06-01 23:23:50 +0000155
Chris Lattnerdb31bba2002-12-02 21:44:34 +0000156 return false;
157}
158
Chris Lattner083be4d2010-07-22 21:05:13 +0000159/// determineREX - Determine if the MachineInstr has to be encoded with a X86-64
160/// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand
161/// size, and 3) use of X86-64 extended registers.
162static unsigned determineREX(const MachineInstr &MI) {
163 unsigned REX = 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000164 const MCInstrDesc &Desc = MI.getDesc();
Jakub Staszak33938022012-05-01 23:04:38 +0000165
Chris Lattner083be4d2010-07-22 21:05:13 +0000166 // Pseudo instructions do not need REX prefix byte.
167 if ((Desc.TSFlags & X86II::FormMask) == X86II::Pseudo)
168 return 0;
169 if (Desc.TSFlags & X86II::REX_W)
170 REX |= 1 << 3;
Jakub Staszak33938022012-05-01 23:04:38 +0000171
Chris Lattner083be4d2010-07-22 21:05:13 +0000172 unsigned NumOps = Desc.getNumOperands();
173 if (NumOps) {
174 bool isTwoAddr = NumOps > 1 &&
Craig Topper9fc5c812012-05-23 03:59:53 +0000175 Desc.getOperandConstraint(1, MCOI::TIED_TO) != -1;
Jakub Staszak33938022012-05-01 23:04:38 +0000176
Chris Lattner083be4d2010-07-22 21:05:13 +0000177 // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
178 unsigned i = isTwoAddr ? 1 : 0;
179 for (unsigned e = NumOps; i != e; ++i) {
180 const MachineOperand& MO = MI.getOperand(i);
181 if (MO.isReg()) {
182 unsigned Reg = MO.getReg();
Evan Cheng7e763d82011-07-25 18:43:53 +0000183 if (X86II::isX86_64NonExtLowByteReg(Reg))
Chris Lattner083be4d2010-07-22 21:05:13 +0000184 REX |= 0x40;
185 }
186 }
Jakub Staszak33938022012-05-01 23:04:38 +0000187
Chris Lattner083be4d2010-07-22 21:05:13 +0000188 switch (Desc.TSFlags & X86II::FormMask) {
Chris Lattner083be4d2010-07-22 21:05:13 +0000189 case X86II::MRMSrcReg: {
190 if (X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0)))
191 REX |= 1 << 2;
192 i = isTwoAddr ? 2 : 1;
193 for (unsigned e = NumOps; i != e; ++i) {
194 const MachineOperand& MO = MI.getOperand(i);
195 if (X86InstrInfo::isX86_64ExtendedReg(MO))
196 REX |= 1 << 0;
197 }
198 break;
199 }
200 case X86II::MRMSrcMem: {
201 if (X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0)))
202 REX |= 1 << 2;
203 unsigned Bit = 0;
204 i = isTwoAddr ? 2 : 1;
205 for (; i != NumOps; ++i) {
206 const MachineOperand& MO = MI.getOperand(i);
207 if (MO.isReg()) {
208 if (X86InstrInfo::isX86_64ExtendedReg(MO))
209 REX |= 1 << Bit;
210 Bit++;
211 }
212 }
213 break;
214 }
215 case X86II::MRM0m: case X86II::MRM1m:
216 case X86II::MRM2m: case X86II::MRM3m:
217 case X86II::MRM4m: case X86II::MRM5m:
218 case X86II::MRM6m: case X86II::MRM7m:
219 case X86II::MRMDestMem: {
220 unsigned e = (isTwoAddr ? X86::AddrNumOperands+1 : X86::AddrNumOperands);
221 i = isTwoAddr ? 1 : 0;
222 if (NumOps > e && X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(e)))
223 REX |= 1 << 2;
224 unsigned Bit = 0;
225 for (; i != e; ++i) {
226 const MachineOperand& MO = MI.getOperand(i);
227 if (MO.isReg()) {
228 if (X86InstrInfo::isX86_64ExtendedReg(MO))
229 REX |= 1 << Bit;
230 Bit++;
231 }
232 }
233 break;
234 }
235 default: {
236 if (X86InstrInfo::isX86_64ExtendedReg(MI.getOperand(0)))
237 REX |= 1 << 0;
238 i = isTwoAddr ? 2 : 1;
239 for (unsigned e = NumOps; i != e; ++i) {
240 const MachineOperand& MO = MI.getOperand(i);
241 if (X86InstrInfo::isX86_64ExtendedReg(MO))
242 REX |= 1 << 2;
243 }
244 break;
245 }
246 }
247 }
248 return REX;
249}
250
251
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000252/// emitPCRelativeBlockAddress - This method keeps track of the information
253/// necessary to resolve the address of this block later and emits a dummy
254/// value.
Chris Lattner3bb2a002003-06-01 23:23:50 +0000255///
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000256template<class CodeEmitter>
257void Emitter<CodeEmitter>::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000258 // Remember where this reference was and where it is to so we can
259 // deal with it later.
Evan Cheng78bf1072006-07-27 18:21:10 +0000260 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
261 X86::reloc_pcrel_word, MBB));
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000262 MCE.emitWordLE(0);
Chris Lattner3bb2a002003-06-01 23:23:50 +0000263}
264
Chris Lattner3bb2a002003-06-01 23:23:50 +0000265/// emitGlobalAddress - Emit the specified address to the code stream assuming
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000266/// this is part of a "take the address of a global" instruction.
Chris Lattner3bb2a002003-06-01 23:23:50 +0000267///
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000268template<class CodeEmitter>
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000269void Emitter<CodeEmitter>::emitGlobalAddress(const GlobalValue *GV,
270 unsigned Reloc,
Dan Gohman712886f2008-10-24 01:57:54 +0000271 intptr_t Disp /* = 0 */,
272 intptr_t PCAdj /* = 0 */,
Evan Cheng9f3058f2008-11-10 01:08:07 +0000273 bool Indirect /* = false */) {
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000274 intptr_t RelocCST = Disp;
Evan Cheng563fcc32008-01-03 02:56:28 +0000275 if (Reloc == X86::reloc_picrel_word)
Evan Cheng880b0802008-01-05 02:26:58 +0000276 RelocCST = PICBaseOffset;
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000277 else if (Reloc == X86::reloc_pcrel_word)
278 RelocCST = PCAdj;
Evan Cheng9f3058f2008-11-10 01:08:07 +0000279 MachineRelocation MR = Indirect
280 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc,
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000281 const_cast<GlobalValue *>(GV),
282 RelocCST, false)
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000283 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000284 const_cast<GlobalValue *>(GV), RelocCST, false);
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000285 MCE.addRelocation(MR);
Dan Gohman712886f2008-10-24 01:57:54 +0000286 // The relocated value will be added to the displacement
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000287 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman712886f2008-10-24 01:57:54 +0000288 MCE.emitDWordLE(Disp);
289 else
290 MCE.emitWordLE((int32_t)Disp);
Chris Lattner3bb2a002003-06-01 23:23:50 +0000291}
292
Chris Lattnerd02c9eb2004-11-20 23:55:15 +0000293/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
294/// be emitted to the current location in the function, and allow it to be PC
295/// relative.
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000296template<class CodeEmitter>
297void Emitter<CodeEmitter>::emitExternalSymbolAddress(const char *ES,
298 unsigned Reloc) {
Evan Cheng880b0802008-01-05 02:26:58 +0000299 intptr_t RelocCST = (Reloc == X86::reloc_picrel_word) ? PICBaseOffset : 0;
Evan Phoenixee9d33b2010-02-04 19:56:59 +0000300
301 // X86 never needs stubs because instruction selection will always pick
302 // an instruction sequence that is large enough to hold any address
303 // to a symbol.
304 // (see X86ISelLowering.cpp, near 2039: X86TargetLowering::LowerCall)
305 bool NeedStub = false;
Chris Lattnere3a9c702006-05-03 20:30:20 +0000306 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Evan Phoenixee9d33b2010-02-04 19:56:59 +0000307 Reloc, ES, RelocCST,
308 0, NeedStub));
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000309 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman712886f2008-10-24 01:57:54 +0000310 MCE.emitDWordLE(0);
311 else
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000312 MCE.emitWordLE(0);
Chris Lattnerd02c9eb2004-11-20 23:55:15 +0000313}
Chris Lattner3bb2a002003-06-01 23:23:50 +0000314
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000315/// emitConstPoolAddress - Arrange for the address of an constant pool
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000316/// to be emitted to the current location in the function, and allow it to be PC
317/// relative.
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000318template<class CodeEmitter>
319void Emitter<CodeEmitter>::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
Dan Gohman712886f2008-10-24 01:57:54 +0000320 intptr_t Disp /* = 0 */,
Evan Cheng563fcc32008-01-03 02:56:28 +0000321 intptr_t PCAdj /* = 0 */) {
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000322 intptr_t RelocCST = 0;
Evan Cheng563fcc32008-01-03 02:56:28 +0000323 if (Reloc == X86::reloc_picrel_word)
Evan Cheng880b0802008-01-05 02:26:58 +0000324 RelocCST = PICBaseOffset;
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000325 else if (Reloc == X86::reloc_pcrel_word)
326 RelocCST = PCAdj;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000327 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000328 Reloc, CPI, RelocCST));
Dan Gohman712886f2008-10-24 01:57:54 +0000329 // The relocated value will be added to the displacement
Evan Cheng3b235aa2006-12-05 07:29:55 +0000330 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman712886f2008-10-24 01:57:54 +0000331 MCE.emitDWordLE(Disp);
332 else
333 MCE.emitWordLE((int32_t)Disp);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000334}
335
Evan Cheng62cdc3f2006-12-05 04:01:03 +0000336/// emitJumpTableAddress - Arrange for the address of a jump table to
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000337/// be emitted to the current location in the function, and allow it to be PC
338/// relative.
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000339template<class CodeEmitter>
340void Emitter<CodeEmitter>::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
Evan Cheng563fcc32008-01-03 02:56:28 +0000341 intptr_t PCAdj /* = 0 */) {
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000342 intptr_t RelocCST = 0;
Evan Cheng563fcc32008-01-03 02:56:28 +0000343 if (Reloc == X86::reloc_picrel_word)
Evan Cheng880b0802008-01-05 02:26:58 +0000344 RelocCST = PICBaseOffset;
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000345 else if (Reloc == X86::reloc_pcrel_word)
346 RelocCST = PCAdj;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000347 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000348 Reloc, JTI, RelocCST));
Dan Gohman712886f2008-10-24 01:57:54 +0000349 // The relocated value will be added to the displacement
Evan Cheng3b235aa2006-12-05 07:29:55 +0000350 if (Reloc == X86::reloc_absolute_dword)
Dan Gohman712886f2008-10-24 01:57:54 +0000351 MCE.emitDWordLE(0);
352 else
Evan Cheng3b235aa2006-12-05 07:29:55 +0000353 MCE.emitWordLE(0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000354}
355
Chris Lattner8052f802002-12-03 06:34:06 +0000356inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
357 unsigned RM) {
358 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
359 return RM | (RegOpcode << 3) | (Mod << 6);
360}
361
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000362template<class CodeEmitter>
363void Emitter<CodeEmitter>::emitRegModRMByte(unsigned ModRMReg,
364 unsigned RegOpcodeFld){
Michael Liaof54249b2012-10-04 19:50:43 +0000365 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
Chris Lattner8052f802002-12-03 06:34:06 +0000366}
367
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000368template<class CodeEmitter>
369void Emitter<CodeEmitter>::emitRegModRMByte(unsigned RegOpcodeFld) {
Evan Cheng27c37022008-10-17 17:14:20 +0000370 MCE.emitByte(ModRMByte(3, RegOpcodeFld, 0));
371}
372
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000373template<class CodeEmitter>
Jakub Staszak33938022012-05-01 23:04:38 +0000374void Emitter<CodeEmitter>::emitSIBByte(unsigned SS,
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000375 unsigned Index,
376 unsigned Base) {
Chris Lattner8052f802002-12-03 06:34:06 +0000377 // SIB byte is in the same format as the ModRMByte...
378 MCE.emitByte(ModRMByte(SS, Index, Base));
379}
380
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000381template<class CodeEmitter>
382void Emitter<CodeEmitter>::emitConstant(uint64_t Val, unsigned Size) {
Chris Lattner8052f802002-12-03 06:34:06 +0000383 // Output the constant in little endian byte order...
384 for (unsigned i = 0; i != Size; ++i) {
385 MCE.emitByte(Val & 255);
386 Val >>= 8;
387 }
388}
389
Jakub Staszak33938022012-05-01 23:04:38 +0000390/// isDisp8 - Return true if this signed displacement fits in a 8-bit
391/// sign-extended field.
Chris Lattner8052f802002-12-03 06:34:06 +0000392static bool isDisp8(int Value) {
393 return Value == (signed char)Value;
394}
395
Chris Lattner405d0242009-07-10 05:27:43 +0000396static bool gvNeedsNonLazyPtr(const MachineOperand &GVOp,
397 const TargetMachine &TM) {
Chris Lattner405d0242009-07-10 05:27:43 +0000398 // For Darwin-64, simulate the linktime GOT by using the same non-lazy-pointer
Dale Johannesend4a5e8f2008-08-12 18:23:48 +0000399 // mechanism as 32-bit mode.
Jakub Staszak33938022012-05-01 23:04:38 +0000400 if (TM.getSubtarget<X86Subtarget>().is64Bit() &&
Chris Lattner405d0242009-07-10 05:27:43 +0000401 !TM.getSubtarget<X86Subtarget>().isTargetDarwin())
402 return false;
Jakub Staszak33938022012-05-01 23:04:38 +0000403
Chris Lattnere6d25932009-07-10 06:07:08 +0000404 // Return true if this is a reference to a stub containing the address of the
405 // global, not the global itself.
Chris Lattnerca9d7842009-07-10 06:29:59 +0000406 return isGlobalStubReference(GVOp.getTargetFlags());
Evan Cheng49ff8ec2008-01-04 10:46:51 +0000407}
408
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000409template<class CodeEmitter>
410void Emitter<CodeEmitter>::emitDisplacementField(const MachineOperand *RelocOp,
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000411 int DispVal,
412 intptr_t Adj /* = 0 */,
413 bool IsPCRel /* = true */) {
Chris Lattner2aef59f2006-05-04 00:42:08 +0000414 // If this is a simple integer displacement that doesn't require a relocation,
415 // emit it now.
416 if (!RelocOp) {
417 emitConstant(DispVal, 4);
418 return;
419 }
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000420
Chris Lattner2aef59f2006-05-04 00:42:08 +0000421 // Otherwise, this is something that requires a relocation. Emit it as such
422 // now.
Daniel Dunbarff0e6222009-09-01 22:07:06 +0000423 unsigned RelocType = Is64BitMode ?
424 (IsPCRel ? X86::reloc_pcrel_word : X86::reloc_absolute_word_sext)
425 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000426 if (RelocOp->isGlobal()) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000427 // In 64-bit static small code model, we could potentially emit absolute.
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000428 // But it's probably not beneficial. If the MCE supports using RIP directly
Jakub Staszak33938022012-05-01 23:04:38 +0000429 // do it, otherwise fallback to absolute (this is determined by IsPCRel).
Bill Wendling80d6b872008-02-26 10:57:23 +0000430 // 89 05 00 00 00 00 mov %eax,0(%rip) # PC-relative
431 // 89 04 25 00 00 00 00 mov %eax,0x0 # Absolute
Chris Lattner405d0242009-07-10 05:27:43 +0000432 bool Indirect = gvNeedsNonLazyPtr(*RelocOp, TM);
Daniel Dunbarff0e6222009-09-01 22:07:06 +0000433 emitGlobalAddress(RelocOp->getGlobal(), RelocType, RelocOp->getOffset(),
Jeffrey Yasskin10d36042009-11-16 22:41:33 +0000434 Adj, Indirect);
Daniel Dunbar6c384382009-09-01 22:06:53 +0000435 } else if (RelocOp->isSymbol()) {
Daniel Dunbarff0e6222009-09-01 22:07:06 +0000436 emitExternalSymbolAddress(RelocOp->getSymbolName(), RelocType);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000437 } else if (RelocOp->isCPI()) {
Daniel Dunbarff0e6222009-09-01 22:07:06 +0000438 emitConstPoolAddress(RelocOp->getIndex(), RelocType,
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000439 RelocOp->getOffset(), Adj);
Chris Lattner2aef59f2006-05-04 00:42:08 +0000440 } else {
Daniel Dunbarff0e6222009-09-01 22:07:06 +0000441 assert(RelocOp->isJTI() && "Unexpected machine operand!");
442 emitJumpTableAddress(RelocOp->getIndex(), RelocType, Adj);
Chris Lattner2aef59f2006-05-04 00:42:08 +0000443 }
444}
445
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000446template<class CodeEmitter>
447void Emitter<CodeEmitter>::emitMemModRMByte(const MachineInstr &MI,
Chris Lattner10f605c2009-08-16 02:45:18 +0000448 unsigned Op,unsigned RegOpcodeField,
449 intptr_t PCAdj) {
Chris Lattner3b789382004-10-15 04:53:13 +0000450 const MachineOperand &Op3 = MI.getOperand(Op+3);
Chris Lattner3b789382004-10-15 04:53:13 +0000451 int DispVal = 0;
Chris Lattner2aef59f2006-05-04 00:42:08 +0000452 const MachineOperand *DispForReloc = 0;
Jakub Staszak33938022012-05-01 23:04:38 +0000453
Chris Lattner2aef59f2006-05-04 00:42:08 +0000454 // Figure out what sort of displacement we have to handle here.
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000455 if (Op3.isGlobal()) {
Chris Lattner2aef59f2006-05-04 00:42:08 +0000456 DispForReloc = &Op3;
Daniel Dunbar6c384382009-09-01 22:06:53 +0000457 } else if (Op3.isSymbol()) {
458 DispForReloc = &Op3;
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000459 } else if (Op3.isCPI()) {
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000460 if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000461 DispForReloc = &Op3;
462 } else {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000463 DispVal += MCE.getConstantPoolEntryAddress(Op3.getIndex());
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000464 DispVal += Op3.getOffset();
465 }
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000466 } else if (Op3.isJTI()) {
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000467 if (!MCE.earlyResolveAddresses() || Is64BitMode || IsPIC) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000468 DispForReloc = &Op3;
469 } else {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000470 DispVal += MCE.getJumpTableEntryAddress(Op3.getIndex());
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000471 }
Chris Lattner3b789382004-10-15 04:53:13 +0000472 } else {
Chris Lattnere3d2e1e2006-09-05 02:52:35 +0000473 DispVal = Op3.getImm();
Chris Lattner3b789382004-10-15 04:53:13 +0000474 }
475
Chris Lattner112fd882004-10-17 07:49:45 +0000476 const MachineOperand &Base = MI.getOperand(Op);
Chris Lattner8052f802002-12-03 06:34:06 +0000477 const MachineOperand &Scale = MI.getOperand(Op+1);
478 const MachineOperand &IndexReg = MI.getOperand(Op+2);
Chris Lattner8052f802002-12-03 06:34:06 +0000479
Evan Cheng877ab552006-02-26 09:12:34 +0000480 unsigned BaseReg = Base.getReg();
Jakub Staszak33938022012-05-01 23:04:38 +0000481
Bill Wendling11740302010-04-21 00:34:04 +0000482 // Handle %rip relative addressing.
483 if (BaseReg == X86::RIP ||
484 (Is64BitMode && DispForReloc)) { // [disp32+RIP] in X86-64 mode
485 assert(IndexReg.getReg() == 0 && Is64BitMode &&
486 "Invalid rip-relative address");
487 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
488 emitDisplacementField(DispForReloc, DispVal, PCAdj, true);
489 return;
490 }
Chris Lattner112fd882004-10-17 07:49:45 +0000491
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000492 // Indicate that the displacement will use an pcrel or absolute reference
493 // by default. MCEs able to resolve addresses on-the-fly use pcrel by default
494 // while others, unless explicit asked to use RIP, use absolute references.
495 bool IsPCRel = MCE.earlyResolveAddresses() ? true : false;
496
Chris Lattner8052f802002-12-03 06:34:06 +0000497 // Is a SIB byte needed?
Jakub Staszak33938022012-05-01 23:04:38 +0000498 // If no BaseReg, issue a RIP relative instruction only if the MCE can
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000499 // resolve addresses on-the-fly, otherwise use SIB (Intel Manual 2A, table
500 // 2-7) and absolute references.
Chris Lattnerfbf1f022010-02-11 08:45:56 +0000501 unsigned BaseRegNo = -1U;
502 if (BaseReg != 0 && BaseReg != X86::RIP)
Michael Liaof54249b2012-10-04 19:50:43 +0000503 BaseRegNo = getX86RegNum(BaseReg);
Chris Lattner5a4ec872010-02-11 08:41:21 +0000504
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000505 if (// The SIB byte must be used if there is an index register.
Jakub Staszak33938022012-05-01 23:04:38 +0000506 IndexReg.getReg() == 0 &&
Chris Lattner5a4ec872010-02-11 08:41:21 +0000507 // The SIB byte must be used if the base is ESP/RSP/R12, all of which
508 // encode to an R/M value of 4, which indicates that a SIB byte is
509 // present.
510 BaseRegNo != N86::ESP &&
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000511 // If there is no base register and we're in 64-bit mode, we need a SIB
512 // byte to emit an addr that is just 'disp32' (the non-RIP relative form).
513 (!Is64BitMode || BaseReg != 0)) {
514 if (BaseReg == 0 || // [disp32] in X86-32 mode
515 BaseReg == X86::RIP) { // [disp32+RIP] in X86-64 mode
Chris Lattner8052f802002-12-03 06:34:06 +0000516 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
Bruno Cardoso Lopes1b02cee2009-08-05 00:11:21 +0000517 emitDisplacementField(DispForReloc, DispVal, PCAdj, true);
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000518 return;
Chris Lattner8052f802002-12-03 06:34:06 +0000519 }
Jakub Staszak33938022012-05-01 23:04:38 +0000520
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000521 // If the base is not EBP/ESP and there is no displacement, use simple
522 // indirect register encoding, this handles addresses like [EAX]. The
523 // encoding for [EBP] with no displacement means [disp32] so we handle it
524 // by emitting a displacement of 0 below.
525 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
526 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
527 return;
Chris Lattner8052f802002-12-03 06:34:06 +0000528 }
Jakub Staszak33938022012-05-01 23:04:38 +0000529
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000530 // Otherwise, if the displacement fits in a byte, encode as [REG+disp8].
531 if (!DispForReloc && isDisp8(DispVal)) {
532 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
Chris Lattner2aef59f2006-05-04 00:42:08 +0000533 emitConstant(DispVal, 1);
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000534 return;
Chris Lattner8052f802002-12-03 06:34:06 +0000535 }
Jakub Staszak33938022012-05-01 23:04:38 +0000536
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000537 // Otherwise, emit the most general non-SIB encoding: [REG+disp32]
538 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
539 emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
540 return;
541 }
Jakub Staszak33938022012-05-01 23:04:38 +0000542
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000543 // Otherwise we need a SIB byte, so start by outputting the ModR/M byte first.
544 assert(IndexReg.getReg() != X86::ESP &&
545 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
546
547 bool ForceDisp32 = false;
548 bool ForceDisp8 = false;
549 if (BaseReg == 0) {
550 // If there is no base register, we emit the special case SIB byte with
551 // MOD=0, BASE=4, to JUST get the index, scale, and displacement.
552 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
553 ForceDisp32 = true;
554 } else if (DispForReloc) {
555 // Emit the normal disp32 encoding.
556 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
557 ForceDisp32 = true;
Bill Wendling11740302010-04-21 00:34:04 +0000558 } else if (DispVal == 0 && BaseRegNo != N86::EBP) {
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000559 // Emit no displacement ModR/M byte
560 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
561 } else if (isDisp8(DispVal)) {
562 // Emit the disp8 encoding...
563 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
564 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
565 } else {
566 // Emit the normal disp32 encoding...
567 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
568 }
569
570 // Calculate what the SS field value should be...
Jeffrey Yasskin6381c012011-07-27 06:22:51 +0000571 static const unsigned SSTable[] = { ~0U, 0, 1, ~0U, 2, ~0U, ~0U, ~0U, 3 };
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000572 unsigned SS = SSTable[Scale.getImm()];
573
574 if (BaseReg == 0) {
Jakub Staszak33938022012-05-01 23:04:38 +0000575 // Handle the SIB byte for the case where there is no base, see Intel
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000576 // Manual 2A, table 2-7. The displacement has already been output.
577 unsigned IndexRegNo;
578 if (IndexReg.getReg())
Michael Liaof54249b2012-10-04 19:50:43 +0000579 IndexRegNo = getX86RegNum(IndexReg.getReg());
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000580 else // Examples: [ESP+1*<noreg>+4] or [scaled idx]+disp32 (MOD=0,BASE=5)
581 IndexRegNo = 4;
582 emitSIBByte(SS, IndexRegNo, 5);
583 } else {
Michael Liaof54249b2012-10-04 19:50:43 +0000584 unsigned BaseRegNo = getX86RegNum(BaseReg);
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000585 unsigned IndexRegNo;
586 if (IndexReg.getReg())
Michael Liaof54249b2012-10-04 19:50:43 +0000587 IndexRegNo = getX86RegNum(IndexReg.getReg());
Chris Lattner0c3b66c2010-02-09 21:47:19 +0000588 else
589 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
590 emitSIBByte(SS, IndexRegNo, BaseRegNo);
591 }
592
593 // Do we need to output a displacement?
594 if (ForceDisp8) {
595 emitConstant(DispVal, 1);
596 } else if (DispVal != 0 || ForceDisp32) {
597 emitDisplacementField(DispForReloc, DispVal, PCAdj, IsPCRel);
Chris Lattner8052f802002-12-03 06:34:06 +0000598 }
599}
600
Eli Friedmanb72d5532011-10-24 20:24:21 +0000601static const MCInstrDesc *UpdateOp(MachineInstr &MI, const X86InstrInfo *II,
602 unsigned Opcode) {
603 const MCInstrDesc *Desc = &II->get(Opcode);
604 MI.setDesc(*Desc);
605 return Desc;
606}
607
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000608/// Is16BitMemOperand - Return true if the specified instruction has
609/// a 16-bit memory operand. Op specifies the operand # of the memoperand.
610static bool Is16BitMemOperand(const MachineInstr &MI, unsigned Op) {
611 const MachineOperand &BaseReg = MI.getOperand(Op+X86::AddrBaseReg);
612 const MachineOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg);
613
614 if ((BaseReg.getReg() != 0 &&
615 X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg.getReg())) ||
616 (IndexReg.getReg() != 0 &&
617 X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg.getReg())))
618 return true;
619 return false;
620}
621
622/// Is32BitMemOperand - Return true if the specified instruction has
623/// a 32-bit memory operand. Op specifies the operand # of the memoperand.
624static bool Is32BitMemOperand(const MachineInstr &MI, unsigned Op) {
625 const MachineOperand &BaseReg = MI.getOperand(Op+X86::AddrBaseReg);
626 const MachineOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg);
627
628 if ((BaseReg.getReg() != 0 &&
629 X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg.getReg())) ||
630 (IndexReg.getReg() != 0 &&
631 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg.getReg())))
632 return true;
633 return false;
634}
635
636/// Is64BitMemOperand - Return true if the specified instruction has
637/// a 64-bit memory operand. Op specifies the operand # of the memoperand.
638#ifndef NDEBUG
639static bool Is64BitMemOperand(const MachineInstr &MI, unsigned Op) {
640 const MachineOperand &BaseReg = MI.getOperand(Op+X86::AddrBaseReg);
641 const MachineOperand &IndexReg = MI.getOperand(Op+X86::AddrIndexReg);
642
643 if ((BaseReg.getReg() != 0 &&
644 X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg.getReg())) ||
645 (IndexReg.getReg() != 0 &&
646 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg.getReg())))
647 return true;
648 return false;
649}
650#endif
651
652template<class CodeEmitter>
653void Emitter<CodeEmitter>::emitOpcodePrefix(uint64_t TSFlags,
654 int MemOperand,
655 const MachineInstr &MI,
656 const MCInstrDesc *Desc) const {
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000657 // Emit the operand size opcode prefix as needed.
658 if (TSFlags & X86II::OpSize)
659 MCE.emitByte(0x66);
660
Craig Topper10243c82014-01-31 08:47:06 +0000661 switch (Desc->TSFlags & X86II::OpPrefixMask) {
662 case X86II::PD: // 66
663 MCE.emitByte(0x66);
664 break;
665 case X86II::XS: // F3
666 MCE.emitByte(0xF3);
667 break;
668 case X86II::XD: // F2
669 MCE.emitByte(0xF2);
670 break;
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000671 }
672
673 // Handle REX prefix.
674 if (Is64BitMode) {
675 if (unsigned REX = determineREX(MI))
676 MCE.emitByte(0x40 | REX);
677 }
678
679 // 0x0F escape code must be emitted just before the opcode.
Craig Topper10243c82014-01-31 08:47:06 +0000680 switch (Desc->TSFlags & X86II::OpMapMask) {
681 case X86II::TB: // Two-byte opcode map
682 case X86II::T8: // 0F 38
683 case X86II::TA: // 0F 3A
684 case X86II::A6: // 0F A6
685 case X86II::A7: // 0F A7
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000686 MCE.emitByte(0x0F);
Craig Topper10243c82014-01-31 08:47:06 +0000687 break;
688 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
689 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
690 MCE.emitByte(0xD8+
691 (((Desc->TSFlags & X86II::OpMapMask)-X86II::D8)
692 >> X86II::OpMapShift));
693 break;
694 }
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000695
Craig Topper10243c82014-01-31 08:47:06 +0000696 switch (Desc->TSFlags & X86II::OpMapMask) {
697 case X86II::T8: // 0F 38
698 MCE.emitByte(0x38);
699 break;
700 case X86II::TA: // 0F 3A
701 MCE.emitByte(0x3A);
702 break;
703 case X86II::A6: // 0F A6
704 MCE.emitByte(0xA6);
705 break;
706 case X86II::A7: // 0F A7
707 MCE.emitByte(0xA7);
708 break;
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000709 }
710}
711
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000712// On regular x86, both XMM0-XMM7 and XMM8-XMM15 are encoded in the range
713// 0-7 and the difference between the 2 groups is given by the REX prefix.
714// In the VEX prefix, registers are seen sequencially from 0-15 and encoded
715// in 1's complement form, example:
716//
717// ModRM field => XMM9 => 1
718// VEX.VVVV => XMM9 => ~9
719//
720// See table 4-35 of Intel AVX Programming Reference for details.
Michael Liaof54249b2012-10-04 19:50:43 +0000721template<class CodeEmitter>
722unsigned char
723Emitter<CodeEmitter>::getVEXRegisterEncoding(const MachineInstr &MI,
724 unsigned OpNum) const {
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000725 unsigned SrcReg = MI.getOperand(OpNum).getReg();
Michael Liaof54249b2012-10-04 19:50:43 +0000726 unsigned SrcRegNum = getX86RegNum(MI.getOperand(OpNum).getReg());
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000727 if (X86II::isX86_64ExtendedReg(SrcReg))
728 SrcRegNum |= 8;
729
730 // The registers represented through VEX_VVVV should
731 // be encoded in 1's complement form.
732 return (~SrcRegNum) & 0xf;
733}
734
735/// EmitSegmentOverridePrefix - Emit segment override opcode prefix as needed
736template<class CodeEmitter>
737void Emitter<CodeEmitter>::emitSegmentOverridePrefix(uint64_t TSFlags,
738 int MemOperand,
739 const MachineInstr &MI) const {
Craig Topper7c6baa72014-01-06 06:51:58 +0000740 if (MemOperand < 0)
741 return; // No memory operand
742
743 // Check for explicit segment override on memory operand.
744 switch (MI.getOperand(MemOperand+X86::AddrSegmentReg).getReg()) {
745 default: llvm_unreachable("Unknown segment register!");
746 case 0: break;
747 case X86::CS: MCE.emitByte(0x2E); break;
748 case X86::SS: MCE.emitByte(0x36); break;
749 case X86::DS: MCE.emitByte(0x3E); break;
750 case X86::ES: MCE.emitByte(0x26); break;
751 case X86::FS: MCE.emitByte(0x64); break;
752 case X86::GS: MCE.emitByte(0x65); break;
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000753 }
754}
755
756template<class CodeEmitter>
757void Emitter<CodeEmitter>::emitVEXOpcodePrefix(uint64_t TSFlags,
758 int MemOperand,
759 const MachineInstr &MI,
760 const MCInstrDesc *Desc) const {
761 bool HasVEX_4V = (TSFlags >> X86II::VEXShift) & X86II::VEX_4V;
762 bool HasVEX_4VOp3 = (TSFlags >> X86II::VEXShift) & X86II::VEX_4VOp3;
Craig Topper87299972013-03-14 07:40:52 +0000763 bool HasMemOp4 = (TSFlags >> X86II::VEXShift) & X86II::MemOp4;
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000764
765 // VEX_R: opcode externsion equivalent to REX.R in
766 // 1's complement (inverted) form
767 //
768 // 1: Same as REX_R=0 (must be 1 in 32-bit mode)
769 // 0: Same as REX_R=1 (64 bit mode only)
770 //
771 unsigned char VEX_R = 0x1;
772
773 // VEX_X: equivalent to REX.X, only used when a
774 // register is used for index in SIB Byte.
775 //
776 // 1: Same as REX.X=0 (must be 1 in 32-bit mode)
777 // 0: Same as REX.X=1 (64-bit mode only)
778 unsigned char VEX_X = 0x1;
779
780 // VEX_B:
781 //
782 // 1: Same as REX_B=0 (ignored in 32-bit mode)
783 // 0: Same as REX_B=1 (64 bit mode only)
784 //
785 unsigned char VEX_B = 0x1;
786
787 // VEX_W: opcode specific (use like REX.W, or used for
788 // opcode extension, or ignored, depending on the opcode byte)
789 unsigned char VEX_W = 0;
790
791 // XOP: Use XOP prefix byte 0x8f instead of VEX.
Craig Topper8a60fff2014-01-16 06:14:45 +0000792 bool XOP = (TSFlags >> X86II::VEXShift) & X86II::XOP;
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000793
794 // VEX_5M (VEX m-mmmmm field):
795 //
796 // 0b00000: Reserved for future use
797 // 0b00001: implied 0F leading opcode
798 // 0b00010: implied 0F 38 leading opcode bytes
799 // 0b00011: implied 0F 3A leading opcode bytes
800 // 0b00100-0b11111: Reserved for future use
801 // 0b01000: XOP map select - 08h instructions with imm byte
Craig Toppere75666f2013-09-29 06:31:18 +0000802 // 0b01001: XOP map select - 09h instructions with no imm byte
803 // 0b01010: XOP map select - 0Ah instructions with imm dword
Craig Topper10243c82014-01-31 08:47:06 +0000804 unsigned char VEX_5M = 0;
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000805
806 // VEX_4V (VEX vvvv field): a register specifier
807 // (in 1's complement form) or 1111 if unused.
808 unsigned char VEX_4V = 0xf;
809
810 // VEX_L (Vector Length):
811 //
812 // 0: scalar or 128-bit vector
813 // 1: 256-bit vector
814 //
815 unsigned char VEX_L = 0;
816
817 // VEX_PP: opcode extension providing equivalent
818 // functionality of a SIMD prefix
819 //
820 // 0b00: None
821 // 0b01: 66
822 // 0b10: F3
823 // 0b11: F2
824 //
825 unsigned char VEX_PP = 0;
826
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000827 if ((TSFlags >> X86II::VEXShift) & X86II::VEX_W)
828 VEX_W = 1;
829
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000830 if ((TSFlags >> X86II::VEXShift) & X86II::VEX_L)
831 VEX_L = 1;
832
Craig Topper10243c82014-01-31 08:47:06 +0000833 switch (TSFlags & X86II::OpPrefixMask) {
834 default: break; // VEX_PP already correct
835 case X86II::PD: VEX_PP = 0x1; break; // 66
836 case X86II::XS: VEX_PP = 0x2; break; // F3
837 case X86II::XD: VEX_PP = 0x3; break; // F2
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000838 }
839
Craig Topper10243c82014-01-31 08:47:06 +0000840 switch (TSFlags & X86II::OpMapMask) {
841 default: llvm_unreachable("Invalid prefix!");
842 case X86II::TB: VEX_5M = 0x1; break; // 0F
843 case X86II::T8: VEX_5M = 0x2; break; // 0F 38
844 case X86II::TA: VEX_5M = 0x3; break; // 0F 3A
845 case X86II::XOP8: VEX_5M = 0x8; break;
846 case X86II::XOP9: VEX_5M = 0x9; break;
847 case X86II::XOPA: VEX_5M = 0xA; break;
848 }
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000849
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000850 // Classify VEX_B, VEX_4V, VEX_R, VEX_X
Elena Demikhovsky602f3a22012-05-31 09:20:20 +0000851 unsigned NumOps = Desc->getNumOperands();
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000852 unsigned CurOp = 0;
Craig Topperf7755df2012-07-12 06:52:41 +0000853 if (NumOps > 1 && Desc->getOperandConstraint(1, MCOI::TIED_TO) == 0)
Elena Demikhovsky602f3a22012-05-31 09:20:20 +0000854 ++CurOp;
Craig Topperf7755df2012-07-12 06:52:41 +0000855 else if (NumOps > 3 && Desc->getOperandConstraint(2, MCOI::TIED_TO) == 0) {
856 assert(Desc->getOperandConstraint(NumOps - 1, MCOI::TIED_TO) == 1);
857 // Special case for GATHER with 2 TIED_TO operands
858 // Skip the first 2 operands: dst, mask_wb
859 CurOp += 2;
860 }
861
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000862 switch (TSFlags & X86II::FormMask) {
Craig Topper8a60fff2014-01-16 06:14:45 +0000863 default: llvm_unreachable("Unexpected form in emitVEXOpcodePrefix!");
864 case X86II::RawFrm:
865 break;
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000866 case X86II::MRMDestMem: {
867 // MRMDestMem instructions forms:
868 // MemAddr, src1(ModR/M)
869 // MemAddr, src1(VEX_4V), src2(ModR/M)
870 // MemAddr, src1(ModR/M), imm8
871 //
872 if (X86II::isX86_64ExtendedReg(MI.getOperand(X86::AddrBaseReg).getReg()))
873 VEX_B = 0x0;
874 if (X86II::isX86_64ExtendedReg(MI.getOperand(X86::AddrIndexReg).getReg()))
875 VEX_X = 0x0;
876
877 CurOp = X86::AddrNumOperands;
878 if (HasVEX_4V)
879 VEX_4V = getVEXRegisterEncoding(MI, CurOp++);
880
881 const MachineOperand &MO = MI.getOperand(CurOp);
882 if (MO.isReg() && X86II::isX86_64ExtendedReg(MO.getReg()))
883 VEX_R = 0x0;
884 break;
885 }
886 case X86II::MRMSrcMem:
887 // MRMSrcMem instructions forms:
888 // src1(ModR/M), MemAddr
889 // src1(ModR/M), src2(VEX_4V), MemAddr
890 // src1(ModR/M), MemAddr, imm8
891 // src1(ModR/M), MemAddr, src2(VEX_I8IMM)
892 //
893 // FMA4:
894 // dst(ModR/M.reg), src1(VEX_4V), src2(ModR/M), src3(VEX_I8IMM)
895 // dst(ModR/M.reg), src1(VEX_4V), src2(VEX_I8IMM), src3(ModR/M),
Craig Topper77df9cd2013-08-21 05:57:45 +0000896 if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg()))
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000897 VEX_R = 0x0;
Craig Topper77df9cd2013-08-21 05:57:45 +0000898 CurOp++;
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000899
Nadav Rotem7efc04c2013-08-21 05:03:10 +0000900 if (HasVEX_4V) {
Craig Topper77df9cd2013-08-21 05:57:45 +0000901 VEX_4V = getVEXRegisterEncoding(MI, CurOp);
902 CurOp++;
Nadav Rotem7efc04c2013-08-21 05:03:10 +0000903 }
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000904
905 if (X86II::isX86_64ExtendedReg(
906 MI.getOperand(MemOperand+X86::AddrBaseReg).getReg()))
907 VEX_B = 0x0;
908 if (X86II::isX86_64ExtendedReg(
909 MI.getOperand(MemOperand+X86::AddrIndexReg).getReg()))
910 VEX_X = 0x0;
911
912 if (HasVEX_4VOp3)
Craig Topper77df9cd2013-08-21 05:57:45 +0000913 VEX_4V = getVEXRegisterEncoding(MI, CurOp+X86::AddrNumOperands);
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000914 break;
915 case X86II::MRM0m: case X86II::MRM1m:
916 case X86II::MRM2m: case X86II::MRM3m:
917 case X86II::MRM4m: case X86II::MRM5m:
918 case X86II::MRM6m: case X86II::MRM7m: {
919 // MRM[0-9]m instructions forms:
920 // MemAddr
921 // src1(VEX_4V), MemAddr
922 if (HasVEX_4V)
Craig Topper77df9cd2013-08-21 05:57:45 +0000923 VEX_4V = getVEXRegisterEncoding(MI, CurOp++);
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000924
925 if (X86II::isX86_64ExtendedReg(
926 MI.getOperand(MemOperand+X86::AddrBaseReg).getReg()))
927 VEX_B = 0x0;
928 if (X86II::isX86_64ExtendedReg(
929 MI.getOperand(MemOperand+X86::AddrIndexReg).getReg()))
930 VEX_X = 0x0;
931 break;
932 }
933 case X86II::MRMSrcReg:
934 // MRMSrcReg instructions forms:
935 // dst(ModR/M), src1(VEX_4V), src2(ModR/M), src3(VEX_I8IMM)
936 // dst(ModR/M), src1(ModR/M)
937 // dst(ModR/M), src1(ModR/M), imm8
938 //
939 if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg()))
940 VEX_R = 0x0;
941 CurOp++;
942
943 if (HasVEX_4V)
944 VEX_4V = getVEXRegisterEncoding(MI, CurOp++);
Craig Topper87299972013-03-14 07:40:52 +0000945
Craig Topperba824292013-03-14 07:47:43 +0000946 if (HasMemOp4) // Skip second register source (encoded in I8IMM)
Craig Topper87299972013-03-14 07:40:52 +0000947 CurOp++;
948
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000949 if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg()))
950 VEX_B = 0x0;
951 CurOp++;
952 if (HasVEX_4VOp3)
953 VEX_4V = getVEXRegisterEncoding(MI, CurOp);
954 break;
955 case X86II::MRMDestReg:
956 // MRMDestReg instructions forms:
957 // dst(ModR/M), src(ModR/M)
958 // dst(ModR/M), src(ModR/M), imm8
Craig Topper612f7bf2013-03-16 03:44:31 +0000959 // dst(ModR/M), src1(VEX_4V), src2(ModR/M)
960 if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg()))
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000961 VEX_B = 0x0;
Craig Topper612f7bf2013-03-16 03:44:31 +0000962 CurOp++;
963
964 if (HasVEX_4V)
965 VEX_4V = getVEXRegisterEncoding(MI, CurOp++);
966
967 if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg()))
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000968 VEX_R = 0x0;
969 break;
970 case X86II::MRM0r: case X86II::MRM1r:
971 case X86II::MRM2r: case X86II::MRM3r:
972 case X86II::MRM4r: case X86II::MRM5r:
973 case X86II::MRM6r: case X86II::MRM7r:
974 // MRM0r-MRM7r instructions forms:
975 // dst(VEX_4V), src(ModR/M), imm8
Craig Topper77df9cd2013-08-21 05:57:45 +0000976 VEX_4V = getVEXRegisterEncoding(MI, CurOp);
977 CurOp++;
978
979 if (X86II::isX86_64ExtendedReg(MI.getOperand(CurOp).getReg()))
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000980 VEX_B = 0x0;
981 break;
Pete Cooperf76b5fe2012-04-30 03:56:44 +0000982 }
983
984 // Emit segment override opcode prefix as needed.
985 emitSegmentOverridePrefix(TSFlags, MemOperand, MI);
986
987 // VEX opcode prefix can have 2 or 3 bytes
988 //
989 // 3 bytes:
990 // +-----+ +--------------+ +-------------------+
991 // | C4h | | RXB | m-mmmm | | W | vvvv | L | pp |
992 // +-----+ +--------------+ +-------------------+
993 // 2 bytes:
994 // +-----+ +-------------------+
995 // | C5h | | R | vvvv | L | pp |
996 // +-----+ +-------------------+
997 //
998 unsigned char LastByte = VEX_PP | (VEX_L << 2) | (VEX_4V << 3);
999
1000 if (VEX_B && VEX_X && !VEX_W && !XOP && (VEX_5M == 1)) { // 2 byte VEX prefix
1001 MCE.emitByte(0xC5);
1002 MCE.emitByte(LastByte | (VEX_R << 7));
1003 return;
1004 }
1005
1006 // 3 byte VEX prefix
1007 MCE.emitByte(XOP ? 0x8F : 0xC4);
1008 MCE.emitByte(VEX_R << 7 | VEX_X << 6 | VEX_B << 5 | VEX_5M);
1009 MCE.emitByte(LastByte | (VEX_W << 7));
1010}
1011
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +00001012template<class CodeEmitter>
Chris Lattner8eeb5012010-10-08 23:54:01 +00001013void Emitter<CodeEmitter>::emitInstruction(MachineInstr &MI,
Evan Cheng6cc775f2011-06-28 19:10:37 +00001014 const MCInstrDesc *Desc) {
David Greenea8000352010-01-05 01:28:53 +00001015 DEBUG(dbgs() << MI);
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001016
Chris Lattnerc951cfe2010-10-08 23:59:27 +00001017 // If this is a pseudo instruction, lower it.
1018 switch (Desc->getOpcode()) {
Eli Friedmanb72d5532011-10-24 20:24:21 +00001019 case X86::ADD16rr_DB: Desc = UpdateOp(MI, II, X86::OR16rr); break;
1020 case X86::ADD32rr_DB: Desc = UpdateOp(MI, II, X86::OR32rr); break;
1021 case X86::ADD64rr_DB: Desc = UpdateOp(MI, II, X86::OR64rr); break;
1022 case X86::ADD16ri_DB: Desc = UpdateOp(MI, II, X86::OR16ri); break;
1023 case X86::ADD32ri_DB: Desc = UpdateOp(MI, II, X86::OR32ri); break;
1024 case X86::ADD64ri32_DB: Desc = UpdateOp(MI, II, X86::OR64ri32); break;
1025 case X86::ADD16ri8_DB: Desc = UpdateOp(MI, II, X86::OR16ri8); break;
1026 case X86::ADD32ri8_DB: Desc = UpdateOp(MI, II, X86::OR32ri8); break;
1027 case X86::ADD64ri8_DB: Desc = UpdateOp(MI, II, X86::OR64ri8); break;
1028 case X86::ACQUIRE_MOV8rm: Desc = UpdateOp(MI, II, X86::MOV8rm); break;
1029 case X86::ACQUIRE_MOV16rm: Desc = UpdateOp(MI, II, X86::MOV16rm); break;
1030 case X86::ACQUIRE_MOV32rm: Desc = UpdateOp(MI, II, X86::MOV32rm); break;
1031 case X86::ACQUIRE_MOV64rm: Desc = UpdateOp(MI, II, X86::MOV64rm); break;
1032 case X86::RELEASE_MOV8mr: Desc = UpdateOp(MI, II, X86::MOV8mr); break;
1033 case X86::RELEASE_MOV16mr: Desc = UpdateOp(MI, II, X86::MOV16mr); break;
1034 case X86::RELEASE_MOV32mr: Desc = UpdateOp(MI, II, X86::MOV32mr); break;
1035 case X86::RELEASE_MOV64mr: Desc = UpdateOp(MI, II, X86::MOV64mr); break;
Chris Lattnerc951cfe2010-10-08 23:59:27 +00001036 }
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001037
Evan Cheng77c8da72008-03-14 07:13:42 +00001038
Devang Patel051454a2009-10-06 02:19:11 +00001039 MCE.processDebugLoc(MI.getDebugLoc(), true);
Jeffrey Yasskinefad8e42009-07-16 21:07:26 +00001040
Evan Cheng62cdc3f2006-12-05 04:01:03 +00001041 unsigned Opcode = Desc->Opcode;
Chris Lattnerdb31bba2002-12-02 21:44:34 +00001042
Chris Lattnere3d2e1e2006-09-05 02:52:35 +00001043 // If this is a two-address instruction, skip one of the register operands.
Chris Lattnerb0d06b42008-01-07 03:13:06 +00001044 unsigned NumOps = Desc->getNumOperands();
Chris Lattnere3d2e1e2006-09-05 02:52:35 +00001045 unsigned CurOp = 0;
Craig Topperf7755df2012-07-12 06:52:41 +00001046 if (NumOps > 1 && Desc->getOperandConstraint(1, MCOI::TIED_TO) == 0)
Evan Cheng00bd8d902008-04-18 20:55:36 +00001047 ++CurOp;
Craig Topperf7755df2012-07-12 06:52:41 +00001048 else if (NumOps > 3 && Desc->getOperandConstraint(2, MCOI::TIED_TO) == 0) {
1049 assert(Desc->getOperandConstraint(NumOps - 1, MCOI::TIED_TO) == 1);
1050 // Special case for GATHER with 2 TIED_TO operands
1051 // Skip the first 2 operands: dst, mask_wb
1052 CurOp += 2;
1053 }
Evan Cheng3b235aa2006-12-05 07:29:55 +00001054
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001055 uint64_t TSFlags = Desc->TSFlags;
1056
1057 // Is this instruction encoded using the AVX VEX prefix?
1058 bool HasVEXPrefix = (TSFlags >> X86II::VEXShift) & X86II::VEX;
1059 // It uses the VEX.VVVV field?
1060 bool HasVEX_4V = (TSFlags >> X86II::VEXShift) & X86II::VEX_4V;
1061 bool HasVEX_4VOp3 = (TSFlags >> X86II::VEXShift) & X86II::VEX_4VOp3;
1062 bool HasMemOp4 = (TSFlags >> X86II::VEXShift) & X86II::MemOp4;
Craig Topper61661782012-05-19 08:28:17 +00001063 const unsigned MemOp4_I8IMMOperand = 2;
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001064
1065 // Determine where the memory operand starts, if present.
1066 int MemoryOperand = X86II::getMemoryOperandNo(TSFlags, Opcode);
1067 if (MemoryOperand != -1) MemoryOperand += CurOp;
1068
Craig Topper2cbf38e2014-01-31 05:42:35 +00001069 // Emit the lock opcode prefix as needed.
1070 if (Desc->TSFlags & X86II::LOCK)
1071 MCE.emitByte(0xF0);
1072
1073 // Emit segment override opcode prefix as needed.
1074 emitSegmentOverridePrefix(TSFlags, MemoryOperand, MI);
1075
1076 // Emit the repeat opcode prefix as needed.
Craig Topperec688662014-01-31 07:00:55 +00001077 if (Desc->TSFlags & X86II::REP)
Craig Topper2cbf38e2014-01-31 05:42:35 +00001078 MCE.emitByte(0xF3);
1079
1080 // Emit the address size opcode prefix as needed.
1081 bool need_address_override;
1082 if (TSFlags & X86II::AdSize) {
1083 need_address_override = true;
1084 } else if (MemoryOperand < 0) {
1085 need_address_override = false;
1086 } else if (Is64BitMode) {
1087 assert(!Is16BitMemOperand(MI, MemoryOperand));
1088 need_address_override = Is32BitMemOperand(MI, MemoryOperand);
1089 } else {
1090 assert(!Is64BitMemOperand(MI, MemoryOperand));
1091 need_address_override = Is16BitMemOperand(MI, MemoryOperand);
1092 }
1093
1094 if (need_address_override)
1095 MCE.emitByte(0x67);
1096
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001097 if (!HasVEXPrefix)
1098 emitOpcodePrefix(TSFlags, MemoryOperand, MI, Desc);
1099 else
1100 emitVEXOpcodePrefix(TSFlags, MemoryOperand, MI, Desc);
1101
Chris Lattner50324352010-02-05 19:24:13 +00001102 unsigned char BaseOpcode = X86II::getBaseOpcodeFor(Desc->TSFlags);
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001103 switch (TSFlags & X86II::FormMask) {
Chris Lattner043bb022009-08-16 02:36:40 +00001104 default:
1105 llvm_unreachable("Unknown FormMask value in X86 MachineCodeEmitter!");
Chris Lattner36703cd2002-12-25 05:09:21 +00001106 case X86II::Pseudo:
Evan Chengf55b7382008-01-05 00:41:47 +00001107 // Remember the current PC offset, this is the PIC relocation
1108 // base address.
Chris Lattnerbe089572006-01-28 18:19:37 +00001109 switch (Opcode) {
Jakub Staszak33938022012-05-01 23:04:38 +00001110 default:
Gabor Greif21fed662010-08-23 20:30:51 +00001111 llvm_unreachable("pseudo instructions should be removed before code"
Chris Lattner043bb022009-08-16 02:36:40 +00001112 " emission");
Eric Christopher4d9c3402010-08-05 20:04:36 +00001113 // Do nothing for Int_MemBarrier - it's just a comment. Add a debug
1114 // to make it slightly easier to see.
1115 case X86::Int_MemBarrier:
1116 DEBUG(dbgs() << "#MEMBARRIER\n");
1117 break;
Jakub Staszak33938022012-05-01 23:04:38 +00001118
Chris Lattnerb06015a2010-02-09 19:54:29 +00001119 case TargetOpcode::INLINEASM:
Evan Chengdfb97382008-11-19 23:21:11 +00001120 // We allow inline assembler nodes with empty bodies - they can
1121 // implicitly define registers, which is ok for JIT.
Chris Lattner0840c822009-10-12 04:22:44 +00001122 if (MI.getOperand(0).getSymbolName()[0])
Chris Lattner2104b8d2010-04-07 22:58:41 +00001123 report_fatal_error("JIT does not support inline asm!");
Evan Cheng3bd59642008-03-05 02:34:36 +00001124 break;
Bill Wendling499f7972010-07-16 22:20:36 +00001125 case TargetOpcode::PROLOG_LABEL:
Chris Lattner1065f492010-03-14 07:27:07 +00001126 case TargetOpcode::GC_LABEL:
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001127 case TargetOpcode::EH_LABEL:
1128 MCE.emitLabel(MI.getOperand(0).getMCSymbol());
1129 break;
Jakub Staszak33938022012-05-01 23:04:38 +00001130
Chris Lattnerb06015a2010-02-09 19:54:29 +00001131 case TargetOpcode::IMPLICIT_DEF:
1132 case TargetOpcode::KILL:
Chris Lattnerbe089572006-01-28 18:19:37 +00001133 break;
Evan Cheng880b0802008-01-05 02:26:58 +00001134 case X86::MOVPC32r: {
Evan Chengf55b7382008-01-05 00:41:47 +00001135 // This emits the "call" portion of this pseudo instruction.
1136 MCE.emitByte(BaseOpcode);
Chris Lattner50324352010-02-05 19:24:13 +00001137 emitConstant(0, X86II::getSizeOfImm(Desc->TSFlags));
Evan Cheng880b0802008-01-05 02:26:58 +00001138 // Remember PIC base.
Evan Cheng0b773192008-12-10 02:32:19 +00001139 PICBaseOffset = (intptr_t) MCE.getCurrentPCOffset();
Dan Gohmaneabd6472008-05-14 01:58:56 +00001140 X86JITInfo *JTI = TM.getJITInfo();
Evan Cheng880b0802008-01-05 02:26:58 +00001141 JTI->setPICBase(MCE.getCurrentPCValue());
Evan Chengf55b7382008-01-05 00:41:47 +00001142 break;
1143 }
Evan Cheng880b0802008-01-05 02:26:58 +00001144 }
Evan Cheng14140052006-11-10 01:28:43 +00001145 CurOp = NumOps;
Chris Lattner36703cd2002-12-25 05:09:21 +00001146 break;
Chris Lattner10f605c2009-08-16 02:45:18 +00001147 case X86II::RawFrm: {
Chris Lattner8052f802002-12-03 06:34:06 +00001148 MCE.emitByte(BaseOpcode);
Evan Chengf55b7382008-01-05 00:41:47 +00001149
Chris Lattner10f605c2009-08-16 02:45:18 +00001150 if (CurOp == NumOps)
1151 break;
Jakub Staszak33938022012-05-01 23:04:38 +00001152
Chris Lattner10f605c2009-08-16 02:45:18 +00001153 const MachineOperand &MO = MI.getOperand(CurOp++);
Bill Wendling75eeeb32008-08-21 08:38:54 +00001154
David Greenea8000352010-01-05 01:28:53 +00001155 DEBUG(dbgs() << "RawFrm CurOp " << CurOp << "\n");
1156 DEBUG(dbgs() << "isMBB " << MO.isMBB() << "\n");
1157 DEBUG(dbgs() << "isGlobal " << MO.isGlobal() << "\n");
1158 DEBUG(dbgs() << "isSymbol " << MO.isSymbol() << "\n");
1159 DEBUG(dbgs() << "isImm " << MO.isImm() << "\n");
Bill Wendling75eeeb32008-08-21 08:38:54 +00001160
Chris Lattner10f605c2009-08-16 02:45:18 +00001161 if (MO.isMBB()) {
1162 emitPCRelativeBlockAddress(MO.getMBB());
1163 break;
Chris Lattner8052f802002-12-03 06:34:06 +00001164 }
Jakub Staszak33938022012-05-01 23:04:38 +00001165
Chris Lattner10f605c2009-08-16 02:45:18 +00001166 if (MO.isGlobal()) {
Chris Lattner10f605c2009-08-16 02:45:18 +00001167 emitGlobalAddress(MO.getGlobal(), X86::reloc_pcrel_word,
Jeffrey Yasskin10d36042009-11-16 22:41:33 +00001168 MO.getOffset(), 0);
Chris Lattner10f605c2009-08-16 02:45:18 +00001169 break;
1170 }
Jakub Staszak33938022012-05-01 23:04:38 +00001171
Chris Lattner10f605c2009-08-16 02:45:18 +00001172 if (MO.isSymbol()) {
1173 emitExternalSymbolAddress(MO.getSymbolName(), X86::reloc_pcrel_word);
1174 break;
1175 }
Daniel Dunbar0e42dc02010-02-09 23:00:03 +00001176
1177 // FIXME: Only used by hackish MCCodeEmitter, remove when dead.
1178 if (MO.isJTI()) {
1179 emitJumpTableAddress(MO.getIndex(), X86::reloc_pcrel_word);
1180 break;
1181 }
Jakub Staszak33938022012-05-01 23:04:38 +00001182
Chris Lattner10f605c2009-08-16 02:45:18 +00001183 assert(MO.isImm() && "Unknown RawFrm operand!");
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00001184 if (Opcode == X86::CALLpcrel32 || Opcode == X86::CALL64pcrel32) {
Chris Lattner10f605c2009-08-16 02:45:18 +00001185 // Fix up immediate operand for pc relative calls.
1186 intptr_t Imm = (intptr_t)MO.getImm();
1187 Imm = Imm - MCE.getCurrentPCValue() - 4;
Chris Lattner50324352010-02-05 19:24:13 +00001188 emitConstant(Imm, X86II::getSizeOfImm(Desc->TSFlags));
Chris Lattner10f605c2009-08-16 02:45:18 +00001189 } else
Chris Lattner50324352010-02-05 19:24:13 +00001190 emitConstant(MO.getImm(), X86II::getSizeOfImm(Desc->TSFlags));
Chris Lattner8052f802002-12-03 06:34:06 +00001191 break;
Chris Lattner10f605c2009-08-16 02:45:18 +00001192 }
Jakub Staszak33938022012-05-01 23:04:38 +00001193
Chris Lattner043bb022009-08-16 02:36:40 +00001194 case X86II::AddRegFrm: {
Evan Chengd60fa58b2011-07-18 20:57:22 +00001195 MCE.emitByte(BaseOpcode +
Michael Liaof54249b2012-10-04 19:50:43 +00001196 getX86RegNum(MI.getOperand(CurOp++).getReg()));
Jakub Staszak33938022012-05-01 23:04:38 +00001197
Chris Lattner043bb022009-08-16 02:36:40 +00001198 if (CurOp == NumOps)
1199 break;
Jakub Staszak33938022012-05-01 23:04:38 +00001200
Chris Lattner043bb022009-08-16 02:36:40 +00001201 const MachineOperand &MO1 = MI.getOperand(CurOp++);
Chris Lattner50324352010-02-05 19:24:13 +00001202 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
Chris Lattner043bb022009-08-16 02:36:40 +00001203 if (MO1.isImm()) {
1204 emitConstant(MO1.getImm(), Size);
1205 break;
Chris Lattnerd4ba6222003-01-13 00:33:59 +00001206 }
Jakub Staszak33938022012-05-01 23:04:38 +00001207
Chris Lattner043bb022009-08-16 02:36:40 +00001208 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
1209 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
Tim Northover3a1fd4c2013-06-01 09:55:14 +00001210 if (Opcode == X86::MOV32ri64)
Chris Lattner043bb022009-08-16 02:36:40 +00001211 rt = X86::reloc_absolute_word; // FIXME: add X86II flag?
1212 // This should not occur on Darwin for relocatable objects.
1213 if (Opcode == X86::MOV64ri)
1214 rt = X86::reloc_absolute_dword; // FIXME: add X86II flag?
1215 if (MO1.isGlobal()) {
Chris Lattner043bb022009-08-16 02:36:40 +00001216 bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
1217 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
Jeffrey Yasskin10d36042009-11-16 22:41:33 +00001218 Indirect);
Chris Lattner043bb022009-08-16 02:36:40 +00001219 } else if (MO1.isSymbol())
1220 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
1221 else if (MO1.isCPI())
1222 emitConstPoolAddress(MO1.getIndex(), rt);
1223 else if (MO1.isJTI())
1224 emitJumpTableAddress(MO1.getIndex(), rt);
Chris Lattnerd4ba6222003-01-13 00:33:59 +00001225 break;
Chris Lattner043bb022009-08-16 02:36:40 +00001226 }
Chris Lattnerd4ba6222003-01-13 00:33:59 +00001227
1228 case X86II::MRMDestReg: {
Chris Lattner8052f802002-12-03 06:34:06 +00001229 MCE.emitByte(BaseOpcode);
Craig Topper612f7bf2013-03-16 03:44:31 +00001230
1231 unsigned SrcRegNum = CurOp+1;
1232 if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
1233 SrcRegNum++;
1234
Chris Lattnere3d2e1e2006-09-05 02:52:35 +00001235 emitRegModRMByte(MI.getOperand(CurOp).getReg(),
Craig Topper612f7bf2013-03-16 03:44:31 +00001236 getX86RegNum(MI.getOperand(SrcRegNum).getReg()));
1237 CurOp = SrcRegNum + 1;
Chris Lattner4b1e02d2003-05-06 21:31:47 +00001238 break;
Chris Lattnerd4ba6222003-01-13 00:33:59 +00001239 }
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001240 case X86II::MRMDestMem: {
Chris Lattner8052f802002-12-03 06:34:06 +00001241 MCE.emitByte(BaseOpcode);
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001242
1243 unsigned SrcRegNum = CurOp + X86::AddrNumOperands;
1244 if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
1245 SrcRegNum++;
Rafael Espindolac2a17d32009-03-28 17:03:24 +00001246 emitMemModRMByte(MI, CurOp,
Michael Liaof54249b2012-10-04 19:50:43 +00001247 getX86RegNum(MI.getOperand(SrcRegNum).getReg()));
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001248 CurOp = SrcRegNum + 1;
Chris Lattner8052f802002-12-03 06:34:06 +00001249 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001250 }
Chris Lattnerd4ba6222003-01-13 00:33:59 +00001251
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001252 case X86II::MRMSrcReg: {
Chris Lattner8052f802002-12-03 06:34:06 +00001253 MCE.emitByte(BaseOpcode);
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001254
1255 unsigned SrcRegNum = CurOp+1;
1256 if (HasVEX_4V) // Skip 1st src (which is encoded in VEX_VVVV)
Craig Topper1964b6d2012-05-19 19:14:18 +00001257 ++SrcRegNum;
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001258
Craig Topper1964b6d2012-05-19 19:14:18 +00001259 if (HasMemOp4) // Skip 2nd src (which is encoded in I8IMM)
1260 ++SrcRegNum;
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001261
1262 emitRegModRMByte(MI.getOperand(SrcRegNum).getReg(),
Michael Liaof54249b2012-10-04 19:50:43 +00001263 getX86RegNum(MI.getOperand(CurOp).getReg()));
Craig Topper1964b6d2012-05-19 19:14:18 +00001264 // 2 operands skipped with HasMemOp4, compensate accordingly
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001265 CurOp = HasMemOp4 ? SrcRegNum : SrcRegNum + 1;
1266 if (HasVEX_4VOp3)
1267 ++CurOp;
Chris Lattner8052f802002-12-03 06:34:06 +00001268 break;
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001269 }
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001270 case X86II::MRMSrcMem: {
Chris Lattnerf4693072010-07-08 23:46:44 +00001271 int AddrOperands = X86::AddrNumOperands;
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001272 unsigned FirstMemOp = CurOp+1;
1273 if (HasVEX_4V) {
1274 ++AddrOperands;
1275 ++FirstMemOp; // Skip the register source (which is encoded in VEX_VVVV).
1276 }
Craig Topper1964b6d2012-05-19 19:14:18 +00001277 if (HasMemOp4) // Skip second register source (encoded in I8IMM)
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001278 ++FirstMemOp;
1279
1280 MCE.emitByte(BaseOpcode);
Rafael Espindola3b2df102009-04-08 21:14:34 +00001281
1282 intptr_t PCAdj = (CurOp + AddrOperands + 1 != NumOps) ?
Chris Lattner50324352010-02-05 19:24:13 +00001283 X86II::getSizeOfImm(Desc->TSFlags) : 0;
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001284 emitMemModRMByte(MI, FirstMemOp,
Michael Liaof54249b2012-10-04 19:50:43 +00001285 getX86RegNum(MI.getOperand(CurOp).getReg()),PCAdj);
Rafael Espindola3b2df102009-04-08 21:14:34 +00001286 CurOp += AddrOperands + 1;
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001287 if (HasVEX_4VOp3)
1288 ++CurOp;
Chris Lattner8052f802002-12-03 06:34:06 +00001289 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001290 }
Chris Lattner8052f802002-12-03 06:34:06 +00001291
Alkis Evlogimenos58270fc2004-02-27 18:55:12 +00001292 case X86II::MRM0r: case X86II::MRM1r:
1293 case X86II::MRM2r: case X86II::MRM3r:
1294 case X86II::MRM4r: case X86II::MRM5r:
Evan Cheng27c37022008-10-17 17:14:20 +00001295 case X86II::MRM6r: case X86II::MRM7r: {
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001296 if (HasVEX_4V) // Skip the register dst (which is encoded in VEX_VVVV).
Craig Topper1964b6d2012-05-19 19:14:18 +00001297 ++CurOp;
Chris Lattner8052f802002-12-03 06:34:06 +00001298 MCE.emitByte(BaseOpcode);
Chris Lattner064e9262010-02-12 23:54:57 +00001299 emitRegModRMByte(MI.getOperand(CurOp++).getReg(),
1300 (Desc->TSFlags & X86II::FormMask)-X86II::MRM0r);
Chris Lattner8052f802002-12-03 06:34:06 +00001301
Chris Lattner043bb022009-08-16 02:36:40 +00001302 if (CurOp == NumOps)
1303 break;
Jakub Staszak33938022012-05-01 23:04:38 +00001304
Chris Lattner043bb022009-08-16 02:36:40 +00001305 const MachineOperand &MO1 = MI.getOperand(CurOp++);
Chris Lattner50324352010-02-05 19:24:13 +00001306 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
Chris Lattner043bb022009-08-16 02:36:40 +00001307 if (MO1.isImm()) {
1308 emitConstant(MO1.getImm(), Size);
1309 break;
Evan Cheng62cdc3f2006-12-05 04:01:03 +00001310 }
Jakub Staszak33938022012-05-01 23:04:38 +00001311
Chris Lattner043bb022009-08-16 02:36:40 +00001312 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
1313 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
1314 if (Opcode == X86::MOV64ri32)
1315 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag?
1316 if (MO1.isGlobal()) {
Chris Lattner043bb022009-08-16 02:36:40 +00001317 bool Indirect = gvNeedsNonLazyPtr(MO1, TM);
1318 emitGlobalAddress(MO1.getGlobal(), rt, MO1.getOffset(), 0,
Jeffrey Yasskin10d36042009-11-16 22:41:33 +00001319 Indirect);
Chris Lattner043bb022009-08-16 02:36:40 +00001320 } else if (MO1.isSymbol())
1321 emitExternalSymbolAddress(MO1.getSymbolName(), rt);
1322 else if (MO1.isCPI())
1323 emitConstPoolAddress(MO1.getIndex(), rt);
1324 else if (MO1.isJTI())
1325 emitJumpTableAddress(MO1.getIndex(), rt);
Chris Lattner8052f802002-12-03 06:34:06 +00001326 break;
Evan Cheng27c37022008-10-17 17:14:20 +00001327 }
Chris Lattnerd4ba6222003-01-13 00:33:59 +00001328
Alkis Evlogimenos58270fc2004-02-27 18:55:12 +00001329 case X86II::MRM0m: case X86II::MRM1m:
1330 case X86II::MRM2m: case X86II::MRM3m:
1331 case X86II::MRM4m: case X86II::MRM5m:
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001332 case X86II::MRM6m: case X86II::MRM7m: {
Pete Cooperf76b5fe2012-04-30 03:56:44 +00001333 if (HasVEX_4V) // Skip the register dst (which is encoded in VEX_VVVV).
Craig Topper1964b6d2012-05-19 19:14:18 +00001334 ++CurOp;
Chris Lattnerec536272010-07-08 22:41:28 +00001335 intptr_t PCAdj = (CurOp + X86::AddrNumOperands != NumOps) ?
Jakub Staszak33938022012-05-01 23:04:38 +00001336 (MI.getOperand(CurOp+X86::AddrNumOperands).isImm() ?
Chris Lattner50324352010-02-05 19:24:13 +00001337 X86II::getSizeOfImm(Desc->TSFlags) : 4) : 0;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001338
Chris Lattnerd4ba6222003-01-13 00:33:59 +00001339 MCE.emitByte(BaseOpcode);
Evan Cheng62cdc3f2006-12-05 04:01:03 +00001340 emitMemModRMByte(MI, CurOp, (Desc->TSFlags & X86II::FormMask)-X86II::MRM0m,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001341 PCAdj);
Chris Lattnerec536272010-07-08 22:41:28 +00001342 CurOp += X86::AddrNumOperands;
Chris Lattnerd4ba6222003-01-13 00:33:59 +00001343
Chris Lattner043bb022009-08-16 02:36:40 +00001344 if (CurOp == NumOps)
1345 break;
Jakub Staszak33938022012-05-01 23:04:38 +00001346
Chris Lattner043bb022009-08-16 02:36:40 +00001347 const MachineOperand &MO = MI.getOperand(CurOp++);
Chris Lattner50324352010-02-05 19:24:13 +00001348 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
Chris Lattner043bb022009-08-16 02:36:40 +00001349 if (MO.isImm()) {
1350 emitConstant(MO.getImm(), Size);
1351 break;
Chris Lattnerd4ba6222003-01-13 00:33:59 +00001352 }
Jakub Staszak33938022012-05-01 23:04:38 +00001353
Chris Lattner043bb022009-08-16 02:36:40 +00001354 unsigned rt = Is64BitMode ? X86::reloc_pcrel_word
1355 : (IsPIC ? X86::reloc_picrel_word : X86::reloc_absolute_word);
1356 if (Opcode == X86::MOV64mi32)
1357 rt = X86::reloc_absolute_word_sext; // FIXME: add X86II flag?
1358 if (MO.isGlobal()) {
Chris Lattner043bb022009-08-16 02:36:40 +00001359 bool Indirect = gvNeedsNonLazyPtr(MO, TM);
1360 emitGlobalAddress(MO.getGlobal(), rt, MO.getOffset(), 0,
Jeffrey Yasskin10d36042009-11-16 22:41:33 +00001361 Indirect);
Chris Lattner043bb022009-08-16 02:36:40 +00001362 } else if (MO.isSymbol())
1363 emitExternalSymbolAddress(MO.getSymbolName(), rt);
1364 else if (MO.isCPI())
1365 emitConstPoolAddress(MO.getIndex(), rt);
1366 else if (MO.isJTI())
1367 emitJumpTableAddress(MO.getIndex(), rt);
Chris Lattnerd4ba6222003-01-13 00:33:59 +00001368 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001369 }
Evan Cheng9e350cd2006-02-01 06:13:50 +00001370
Craig Topperdf912ba2013-12-31 03:26:24 +00001371 case X86II::MRM_C1: case X86II::MRM_C2: case X86II::MRM_C3:
1372 case X86II::MRM_C4: case X86II::MRM_C8: case X86II::MRM_C9:
1373 case X86II::MRM_CA: case X86II::MRM_CB: case X86II::MRM_D0:
1374 case X86II::MRM_D1: case X86II::MRM_D4: case X86II::MRM_D5:
1375 case X86II::MRM_D6: case X86II::MRM_D8: case X86II::MRM_D9:
1376 case X86II::MRM_DA: case X86II::MRM_DB: case X86II::MRM_DC:
1377 case X86II::MRM_DD: case X86II::MRM_DE: case X86II::MRM_DF:
1378 case X86II::MRM_E8: case X86II::MRM_F0: case X86II::MRM_F8:
Chris Lattnerf7477e52010-02-12 02:06:33 +00001379 MCE.emitByte(BaseOpcode);
Craig Topperdf912ba2013-12-31 03:26:24 +00001380
1381 unsigned char MRM;
1382 switch (TSFlags & X86II::FormMask) {
1383 default: llvm_unreachable("Invalid Form");
1384 case X86II::MRM_C1: MRM = 0xC1; break;
1385 case X86II::MRM_C2: MRM = 0xC2; break;
1386 case X86II::MRM_C3: MRM = 0xC3; break;
1387 case X86II::MRM_C4: MRM = 0xC4; break;
1388 case X86II::MRM_C8: MRM = 0xC8; break;
1389 case X86II::MRM_C9: MRM = 0xC9; break;
1390 case X86II::MRM_CA: MRM = 0xCA; break;
1391 case X86II::MRM_CB: MRM = 0xCB; break;
1392 case X86II::MRM_D0: MRM = 0xD0; break;
1393 case X86II::MRM_D1: MRM = 0xD1; break;
1394 case X86II::MRM_D4: MRM = 0xD4; break;
1395 case X86II::MRM_D5: MRM = 0xD5; break;
1396 case X86II::MRM_D6: MRM = 0xD6; break;
1397 case X86II::MRM_D8: MRM = 0xD8; break;
1398 case X86II::MRM_D9: MRM = 0xD9; break;
1399 case X86II::MRM_DA: MRM = 0xDA; break;
1400 case X86II::MRM_DB: MRM = 0xDB; break;
1401 case X86II::MRM_DC: MRM = 0xDC; break;
1402 case X86II::MRM_DD: MRM = 0xDD; break;
1403 case X86II::MRM_DE: MRM = 0xDE; break;
1404 case X86II::MRM_DF: MRM = 0xDF; break;
1405 case X86II::MRM_E8: MRM = 0xE8; break;
1406 case X86II::MRM_F0: MRM = 0xF0; break;
1407 case X86II::MRM_F8: MRM = 0xF8; break;
1408 case X86II::MRM_F9: MRM = 0xF9; break;
1409 }
1410 MCE.emitByte(MRM);
Chris Lattnerf7477e52010-02-12 02:06:33 +00001411 break;
Chris Lattnerdb31bba2002-12-02 21:44:34 +00001412 }
Evan Chengac22e542006-09-06 20:24:14 +00001413
Benjamin Kramerf1e0b6c2012-05-30 09:13:55 +00001414 while (CurOp != NumOps && NumOps - CurOp <= 2) {
Craig Topper61661782012-05-19 08:28:17 +00001415 // The last source register of a 4 operand instruction in AVX is encoded
1416 // in bits[7:4] of a immediate byte.
1417 if ((TSFlags >> X86II::VEXShift) & X86II::VEX_I8IMM) {
1418 const MachineOperand &MO = MI.getOperand(HasMemOp4 ? MemOp4_I8IMMOperand
1419 : CurOp);
Craig Topper1964b6d2012-05-19 19:14:18 +00001420 ++CurOp;
Michael Liaof54249b2012-10-04 19:50:43 +00001421 unsigned RegNum = getX86RegNum(MO.getReg()) << 4;
Craig Topper1964b6d2012-05-19 19:14:18 +00001422 if (X86II::isX86_64ExtendedReg(MO.getReg()))
1423 RegNum |= 1 << 7;
Craig Topper61661782012-05-19 08:28:17 +00001424 // If there is an additional 5th operand it must be an immediate, which
1425 // is encoded in bits[3:0]
Craig Topper1964b6d2012-05-19 19:14:18 +00001426 if (CurOp != NumOps) {
Craig Topper61661782012-05-19 08:28:17 +00001427 const MachineOperand &MIMM = MI.getOperand(CurOp++);
Craig Topper1964b6d2012-05-19 19:14:18 +00001428 if (MIMM.isImm()) {
Craig Topper61661782012-05-19 08:28:17 +00001429 unsigned Val = MIMM.getImm();
1430 assert(Val < 16 && "Immediate operand value out of range");
1431 RegNum |= Val;
1432 }
1433 }
1434 emitConstant(RegNum, 1);
1435 } else {
1436 emitConstant(MI.getOperand(CurOp++).getImm(),
1437 X86II::getSizeOfImm(Desc->TSFlags));
1438 }
1439 }
1440
Evan Cheng7f8e5632011-12-07 07:15:52 +00001441 if (!MI.isVariadic() && CurOp != NumOps) {
Torok Edwinfb8d6d52009-07-08 20:53:28 +00001442#ifndef NDEBUG
David Greenea8000352010-01-05 01:28:53 +00001443 dbgs() << "Cannot encode all operands of: " << MI << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +00001444#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001445 llvm_unreachable(0);
Evan Cheng801bfb22008-03-05 02:08:03 +00001446 }
Devang Patel051454a2009-10-06 02:19:11 +00001447
1448 MCE.processDebugLoc(MI.getDebugLoc(), false);
Chris Lattnerdb31bba2002-12-02 21:44:34 +00001449}