LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
X86MCTargetDesc.cpp
Go to the documentation of this file.
1 //===-- X86MCTargetDesc.cpp - X86 Target Descriptions ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides X86 specific target descriptions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "X86MCTargetDesc.h"
17 #include "X86MCAsmInfo.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/MC/MCCodeGenInfo.h"
21 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/Support/Host.h"
29 
30 #define GET_REGINFO_MC_DESC
31 #include "X86GenRegisterInfo.inc"
32 
33 #define GET_INSTRINFO_MC_DESC
34 #include "X86GenInstrInfo.inc"
35 
36 #define GET_SUBTARGETINFO_MC_DESC
37 #include "X86GenSubtargetInfo.inc"
38 
39 #if _MSC_VER
40 #include <intrin.h>
41 #endif
42 
43 using namespace llvm;
44 
45 
47  Triple TheTriple(TT);
48  std::string FS;
49  if (TheTriple.getArch() == Triple::x86_64)
50  FS = "+64bit-mode";
51  else
52  FS = "-64bit-mode";
53  return FS;
54 }
55 
56 /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
57 /// specified arguments. If we can't run cpuid on the host, return true.
58 bool X86_MC::GetCpuIDAndInfo(unsigned value, unsigned *rEAX,
59  unsigned *rEBX, unsigned *rECX, unsigned *rEDX) {
60 #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
61  #if defined(__GNUC__)
62  // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
63  asm ("movq\t%%rbx, %%rsi\n\t"
64  "cpuid\n\t"
65  "xchgq\t%%rbx, %%rsi\n\t"
66  : "=a" (*rEAX),
67  "=S" (*rEBX),
68  "=c" (*rECX),
69  "=d" (*rEDX)
70  : "a" (value));
71  return false;
72  #elif defined(_MSC_VER)
73  int registers[4];
74  __cpuid(registers, value);
75  *rEAX = registers[0];
76  *rEBX = registers[1];
77  *rECX = registers[2];
78  *rEDX = registers[3];
79  return false;
80  #else
81  return true;
82  #endif
83 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
84  #if defined(__GNUC__)
85  asm ("movl\t%%ebx, %%esi\n\t"
86  "cpuid\n\t"
87  "xchgl\t%%ebx, %%esi\n\t"
88  : "=a" (*rEAX),
89  "=S" (*rEBX),
90  "=c" (*rECX),
91  "=d" (*rEDX)
92  : "a" (value));
93  return false;
94  #elif defined(_MSC_VER)
95  __asm {
96  mov eax,value
97  cpuid
98  mov esi,rEAX
99  mov dword ptr [esi],eax
100  mov esi,rEBX
101  mov dword ptr [esi],ebx
102  mov esi,rECX
103  mov dword ptr [esi],ecx
104  mov esi,rEDX
105  mov dword ptr [esi],edx
106  }
107  return false;
108  #else
109  return true;
110  #endif
111 #else
112  return true;
113 #endif
114 }
115 
116 /// GetCpuIDAndInfoEx - Execute the specified cpuid with subleaf and return the
117 /// 4 values in the specified arguments. If we can't run cpuid on the host,
118 /// return true.
119 bool X86_MC::GetCpuIDAndInfoEx(unsigned value, unsigned subleaf, unsigned *rEAX,
120  unsigned *rEBX, unsigned *rECX, unsigned *rEDX) {
121 #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
122  #if defined(__GNUC__)
123  // gcc desn't know cpuid would clobber ebx/rbx. Preseve it manually.
124  asm ("movq\t%%rbx, %%rsi\n\t"
125  "cpuid\n\t"
126  "xchgq\t%%rbx, %%rsi\n\t"
127  : "=a" (*rEAX),
128  "=S" (*rEBX),
129  "=c" (*rECX),
130  "=d" (*rEDX)
131  : "a" (value),
132  "c" (subleaf));
133  return false;
134  #elif defined(_MSC_VER)
135  // __cpuidex was added in MSVC++ 9.0 SP1
136  #if (_MSC_VER > 1500) || (_MSC_VER == 1500 && _MSC_FULL_VER >= 150030729)
137  int registers[4];
138  __cpuidex(registers, value, subleaf);
139  *rEAX = registers[0];
140  *rEBX = registers[1];
141  *rECX = registers[2];
142  *rEDX = registers[3];
143  return false;
144  #else
145  return true;
146  #endif
147  #else
148  return true;
149  #endif
150 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
151  #if defined(__GNUC__)
152  asm ("movl\t%%ebx, %%esi\n\t"
153  "cpuid\n\t"
154  "xchgl\t%%ebx, %%esi\n\t"
155  : "=a" (*rEAX),
156  "=S" (*rEBX),
157  "=c" (*rECX),
158  "=d" (*rEDX)
159  : "a" (value),
160  "c" (subleaf));
161  return false;
162  #elif defined(_MSC_VER)
163  __asm {
164  mov eax,value
165  mov ecx,subleaf
166  cpuid
167  mov esi,rEAX
168  mov dword ptr [esi],eax
169  mov esi,rEBX
170  mov dword ptr [esi],ebx
171  mov esi,rECX
172  mov dword ptr [esi],ecx
173  mov esi,rEDX
174  mov dword ptr [esi],edx
175  }
176  return false;
177  #else
178  return true;
179  #endif
180 #else
181  return true;
182 #endif
183 }
184 
185 void X86_MC::DetectFamilyModel(unsigned EAX, unsigned &Family,
186  unsigned &Model) {
187  Family = (EAX >> 8) & 0xf; // Bits 8 - 11
188  Model = (EAX >> 4) & 0xf; // Bits 4 - 7
189  if (Family == 6 || Family == 0xf) {
190  if (Family == 0xf)
191  // Examine extended family ID if family ID is F.
192  Family += (EAX >> 20) & 0xff; // Bits 20 - 27
193  // Examine extended model ID if family ID is 6 or F.
194  Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
195  }
196 }
197 
198 unsigned X86_MC::getDwarfRegFlavour(StringRef TT, bool isEH) {
199  Triple TheTriple(TT);
200  if (TheTriple.getArch() == Triple::x86_64)
201  return DWARFFlavour::X86_64;
202 
203  if (TheTriple.isOSDarwin())
205  if (TheTriple.getOS() == Triple::MinGW32 ||
206  TheTriple.getOS() == Triple::Cygwin)
207  // Unsupported by now, just quick fallback
210 }
211 
213  // FIXME: TableGen these.
214  for (unsigned Reg = X86::NoRegister+1; Reg < X86::NUM_TARGET_REGS; ++Reg) {
215  unsigned SEH = MRI->getEncodingValue(Reg);
216  MRI->mapLLVMRegToSEHReg(Reg, SEH);
217  }
218 }
219 
221  StringRef FS) {
222  std::string ArchFS = X86_MC::ParseX86Triple(TT);
223  if (!FS.empty()) {
224  if (!ArchFS.empty())
225  ArchFS = ArchFS + "," + FS.str();
226  else
227  ArchFS = FS;
228  }
229 
230  std::string CPUName = CPU;
231  if (CPUName.empty()) {
232 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)\
233  || defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
234  CPUName = sys::getHostCPUName();
235 #else
236  CPUName = "generic";
237 #endif
238  }
239 
241  InitX86MCSubtargetInfo(X, TT, CPUName, ArchFS);
242  return X;
243 }
244 
246  MCInstrInfo *X = new MCInstrInfo();
247  InitX86MCInstrInfo(X);
248  return X;
249 }
250 
252  Triple TheTriple(TT);
253  unsigned RA = (TheTriple.getArch() == Triple::x86_64)
254  ? X86::RIP // Should have dwarf #16.
255  : X86::EIP; // Should have dwarf #8.
256 
258  InitX86MCRegisterInfo(X, RA,
259  X86_MC::getDwarfRegFlavour(TT, false),
260  X86_MC::getDwarfRegFlavour(TT, true),
261  RA);
263  return X;
264 }
265 
267  Triple TheTriple(TT);
268  bool is64Bit = TheTriple.getArch() == Triple::x86_64;
269 
270  MCAsmInfo *MAI;
271  if (TheTriple.isOSDarwin() || TheTriple.getEnvironment() == Triple::MachO) {
272  if (is64Bit)
273  MAI = new X86_64MCAsmInfoDarwin(TheTriple);
274  else
275  MAI = new X86MCAsmInfoDarwin(TheTriple);
276  } else if (TheTriple.getEnvironment() == Triple::ELF) {
277  // Force the use of an ELF container.
278  MAI = new X86ELFMCAsmInfo(TheTriple);
279  } else if (TheTriple.getOS() == Triple::Win32) {
280  MAI = new X86MCAsmInfoMicrosoft(TheTriple);
281  } else if (TheTriple.getOS() == Triple::MinGW32 || TheTriple.getOS() == Triple::Cygwin) {
282  MAI = new X86MCAsmInfoGNUCOFF(TheTriple);
283  } else {
284  // The default is ELF.
285  MAI = new X86ELFMCAsmInfo(TheTriple);
286  }
287 
288  // Initialize initial frame state.
289  // Calculate amount of bytes used for return address storing
290  int stackGrowth = is64Bit ? -8 : -4;
291 
292  // Initial state of the frame pointer is esp+stackGrowth.
293  unsigned StackPtr = is64Bit ? X86::RSP : X86::ESP;
295  0, MRI.getDwarfRegNum(StackPtr, true), -stackGrowth);
296  MAI->addInitialFrameState(Inst);
297 
298  // Add return address to move list
299  unsigned InstPtr = is64Bit ? X86::RIP : X86::EIP;
301  0, MRI.getDwarfRegNum(InstPtr, true), stackGrowth);
302  MAI->addInitialFrameState(Inst2);
303 
304  return MAI;
305 }
306 
308  CodeModel::Model CM,
309  CodeGenOpt::Level OL) {
310  MCCodeGenInfo *X = new MCCodeGenInfo();
311 
312  Triple T(TT);
313  bool is64Bit = T.getArch() == Triple::x86_64;
314 
315  if (RM == Reloc::Default) {
316  // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
317  // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
318  // use static relocation model by default.
319  if (T.isOSDarwin()) {
320  if (is64Bit)
321  RM = Reloc::PIC_;
322  else
323  RM = Reloc::DynamicNoPIC;
324  } else if (T.isOSWindows() && is64Bit)
325  RM = Reloc::PIC_;
326  else
327  RM = Reloc::Static;
328  }
329 
330  // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
331  // is defined as a model for code which may be used in static or dynamic
332  // executables but not necessarily a shared library. On X86-32 we just
333  // compile in -static mode, in x86-64 we use PIC.
334  if (RM == Reloc::DynamicNoPIC) {
335  if (is64Bit)
336  RM = Reloc::PIC_;
337  else if (!T.isOSDarwin())
338  RM = Reloc::Static;
339  }
340 
341  // If we are on Darwin, disallow static relocation model in X86-64 mode, since
342  // the Mach-O file format doesn't support it.
343  if (RM == Reloc::Static && T.isOSDarwin() && is64Bit)
344  RM = Reloc::PIC_;
345 
346  // For static codegen, if we're not already set, use Small codegen.
347  if (CM == CodeModel::Default)
348  CM = CodeModel::Small;
349  else if (CM == CodeModel::JITDefault)
350  // 64-bit JIT places everything in the same buffer except external funcs.
351  CM = is64Bit ? CodeModel::Large : CodeModel::Small;
352 
353  X->InitMCCodeGenInfo(RM, CM, OL);
354  return X;
355 }
356 
358  MCContext &Ctx, MCAsmBackend &MAB,
359  raw_ostream &_OS,
360  MCCodeEmitter *_Emitter,
361  bool RelaxAll,
362  bool NoExecStack) {
363  Triple TheTriple(TT);
364 
365  if (TheTriple.isOSDarwin() || TheTriple.getEnvironment() == Triple::MachO)
366  return createMachOStreamer(Ctx, MAB, _OS, _Emitter, RelaxAll);
367 
368  if (TheTriple.isOSWindows() && TheTriple.getEnvironment() != Triple::ELF)
369  return createWinCOFFStreamer(Ctx, MAB, *_Emitter, _OS, RelaxAll);
370 
371  return createELFStreamer(Ctx, 0, MAB, _OS, _Emitter, RelaxAll, NoExecStack);
372 }
373 
375  unsigned SyntaxVariant,
376  const MCAsmInfo &MAI,
377  const MCInstrInfo &MII,
378  const MCRegisterInfo &MRI,
379  const MCSubtargetInfo &STI) {
380  if (SyntaxVariant == 0)
381  return new X86ATTInstPrinter(MAI, MII, MRI);
382  if (SyntaxVariant == 1)
383  return new X86IntelInstPrinter(MAI, MII, MRI);
384  return 0;
385 }
386 
388  MCContext &Ctx) {
389  Triple TheTriple(TT);
390  if (TheTriple.isEnvironmentMachO() && TheTriple.getArch() == Triple::x86_64)
392  else if (TheTriple.isOSBinFormatELF())
393  return createX86_64ELFRelocationInfo(Ctx);
394  // Default to the stock relocation info.
395  return llvm::createMCRelocationInfo(TT, Ctx);
396 }
397 
399  return new MCInstrAnalysis(Info);
400 }
401 
402 // Force static initialization.
403 extern "C" void LLVMInitializeX86TargetMC() {
404  // Register the MC asm info.
407 
408  // Register the MC codegen info.
411 
412  // Register the MC instruction info.
415 
416  // Register the MC register info.
419 
420  // Register the MC subtarget info.
425 
426  // Register the MC instruction analyzer.
431 
432  // Register the code emitter.
437 
438  // Register the asm backend.
443 
444  // Register the object streamer.
449 
450  // Register the MCInstPrinter.
455 
456  // Register the MC relocation info.
461 }
OSType getOS() const
getOS - Get the parsed operating system type of this triple.
Definition: Triple.h:178
static MCCodeGenInfo * createX86MCCodeGenInfo(StringRef TT, Reloc::Model RM, CodeModel::Model CM, CodeGenOpt::Level OL)
static MCInstPrinter * createX86MCInstPrinter(const Target &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI, const MCSubtargetInfo &STI)
int getDwarfRegNum(unsigned RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number. Returns -1 if there is no equivalent va...
static void RegisterMCInstrAnalysis(Target &T, Target::MCInstrAnalysisCtorFnTy Fn)
static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register, int Offset)
.cfi_offset Previous value of Register is saved at offset Offset from CFA.
Definition: MCDwarf.h:348
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:181
static MCAsmInfo * createX86MCAsmInfo(const MCRegisterInfo &MRI, StringRef TT)
static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn)
static MCRelocationInfo * createX86MCRelocationInfo(StringRef TT, MCContext &Ctx)
MCStreamer * createELFStreamer(MCContext &Ctx, MCTargetStreamer *TargetStreamer, MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *CE, bool RelaxAll, bool NoExecStack)
bool isOSWindows() const
Tests whether the OS is Windows.
Definition: Triple.h:328
MCCodeEmitter * createX86MCCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo &MRI, const MCSubtargetInfo &STI, MCContext &Ctx)
MCStreamer * createMachOStreamer(MCContext &Ctx, MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *CE, bool RelaxAll=false)
bool GetCpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX, unsigned *rECX, unsigned *rEDX)
Target TheX86_64Target
static MCRegisterInfo * createX86MCRegisterInfo(StringRef TT)
bool isEnvironmentMachO() const
Tests whether the environment is MachO.
Definition: Triple.h:354
void addInitialFrameState(const MCCFIInstruction &Inst)
Definition: MCAsmInfo.h:535
#define T
MCRelocationInfo * createX86_64ELFRelocationInfo(MCContext &Ctx)
createX86_64ELFORelocationInfo - Construct X86-64 ELF relocation info.
void InitMCCodeGenInfo(Reloc::Model RM=Reloc::Default, CodeModel::Model CM=CodeModel::Default, CodeGenOpt::Level OL=CodeGenOpt::Default)
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:172
MCStreamer * createWinCOFFStreamer(MCContext &Ctx, MCAsmBackend &TAB, MCCodeEmitter &CE, raw_ostream &OS, bool RelaxAll=false)
void LLVMInitializeX86TargetMC()
unsigned getDwarfRegFlavour(StringRef TT, bool isEH)
static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn)
static MCCFIInstruction createDefCfa(MCSymbol *L, unsigned Register, int Offset)
.cfi_def_cfa defines a rule for computing CFA as: take address from Register and add Offset to it...
Definition: MCDwarf.h:321
static MCStreamer * createMCStreamer(const Target &T, StringRef TT, MCContext &Ctx, MCAsmBackend &MAB, raw_ostream &_OS, MCCodeEmitter *_Emitter, bool RelaxAll, bool NoExecStack)
static void RegisterMCObjectStreamer(Target &T, Target::MCObjectStreamerCtorTy Fn)
const MCInstrInfo & MII
std::string ParseX86Triple(StringRef TT)
Target TheX86_32Target
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:22
std::string getHostCPUName()
Definition: Host.cpp:667
bool GetCpuIDAndInfoEx(unsigned value, unsigned subleaf, unsigned *rEAX, unsigned *rEBX, unsigned *rECX, unsigned *rEDX)
MCAsmBackend * createX86_32AsmBackend(const Target &T, const MCRegisterInfo &MRI, StringRef TT, StringRef CPU)
Create MCExprs from relocations found in an object file.
MCAsmBackend * createX86_64AsmBackend(const Target &T, const MCRegisterInfo &MRI, StringRef TT, StringRef CPU)
static void RegisterMCSubtargetInfo(Target &T, Target::MCSubtargetInfoCtorFnTy Fn)
void DetectFamilyModel(unsigned EAX, unsigned &Family, unsigned &Model)
MCRelocationInfo * createMCRelocationInfo(StringRef TT, MCContext &Ctx)
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
Definition: Triple.h:313
void InitLLVM2SEHRegisterMapping(MCRegisterInfo *MRI)
static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn)
MCRelocationInfo * createX86_64MachORelocationInfo(MCContext &Ctx)
createX86_64MachORelocationInfo - Construct X86-64 Mach-O relocation info.
static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn)
static MCInstrInfo * createX86MCInstrInfo()
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:343
static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn)
MCSubtargetInfo * createX86MCSubtargetInfo(StringRef TT, StringRef CPU, StringRef FS)
EnvironmentType getEnvironment() const
getEnvironment - Get the parsed environment type of this triple.
Definition: Triple.h:187
uint16_t getEncodingValue(unsigned RegNo) const
Returns the encoding for RegNo.
MCAsmBackend - Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:34
cl::opt< bool > RelaxAll("mc-relax-all", cl::desc("When used with filetype=obj, ""relax all fixups in the emitted object file"))
static void RegisterMCRelocationInfo(Target &T, Target::MCRelocationInfoCtorTy Fn)
const MCRegisterInfo & MRI
static MCInstrAnalysis * createX86MCInstrAnalysis(const MCInstrInfo *Info)
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
void mapLLVMRegToSEHReg(unsigned LLVMReg, int SEHReg)
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110