LLVM  3.7.0
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 #if _MSC_VER
31 #include <intrin.h>
32 #endif
33 
34 using namespace llvm;
35 
36 #define GET_REGINFO_MC_DESC
37 #include "X86GenRegisterInfo.inc"
38 
39 #define GET_INSTRINFO_MC_DESC
40 #include "X86GenInstrInfo.inc"
41 
42 #define GET_SUBTARGETINFO_MC_DESC
43 #include "X86GenSubtargetInfo.inc"
44 
45 std::string X86_MC::ParseX86Triple(const Triple &TT) {
46  std::string FS;
47  if (TT.getArch() == Triple::x86_64)
48  FS = "+64bit-mode,-32bit-mode,-16bit-mode";
49  else if (TT.getEnvironment() != Triple::CODE16)
50  FS = "-64bit-mode,+32bit-mode,-16bit-mode";
51  else
52  FS = "-64bit-mode,-32bit-mode,+16bit-mode";
53 
54  return FS;
55 }
56 
57 unsigned X86_MC::getDwarfRegFlavour(const Triple &TT, bool isEH) {
58  if (TT.getArch() == Triple::x86_64)
59  return DWARFFlavour::X86_64;
60 
61  if (TT.isOSDarwin())
63  if (TT.isOSCygMing())
64  // Unsupported by now, just quick fallback
67 }
68 
70  // FIXME: TableGen these.
71  for (unsigned Reg = X86::NoRegister+1; Reg < X86::NUM_TARGET_REGS; ++Reg) {
72  unsigned SEH = MRI->getEncodingValue(Reg);
73  MRI->mapLLVMRegToSEHReg(Reg, SEH);
74  }
75 }
76 
78  StringRef CPU, StringRef FS) {
79  std::string ArchFS = X86_MC::ParseX86Triple(TT);
80  if (!FS.empty()) {
81  if (!ArchFS.empty())
82  ArchFS = (Twine(ArchFS) + "," + FS).str();
83  else
84  ArchFS = FS;
85  }
86 
87  std::string CPUName = CPU;
88  if (CPUName.empty())
89  CPUName = "generic";
90 
91  return createX86MCSubtargetInfoImpl(TT, CPUName, ArchFS);
92 }
93 
95  MCInstrInfo *X = new MCInstrInfo();
96  InitX86MCInstrInfo(X);
97  return X;
98 }
99 
101  unsigned RA = (TT.getArch() == Triple::x86_64)
102  ? X86::RIP // Should have dwarf #16.
103  : X86::EIP; // Should have dwarf #8.
104 
106  InitX86MCRegisterInfo(X, RA, X86_MC::getDwarfRegFlavour(TT, false),
107  X86_MC::getDwarfRegFlavour(TT, true), RA);
109  return X;
110 }
111 
113  const Triple &TheTriple) {
114  bool is64Bit = TheTriple.getArch() == Triple::x86_64;
115 
116  MCAsmInfo *MAI;
117  if (TheTriple.isOSBinFormatMachO()) {
118  if (is64Bit)
119  MAI = new X86_64MCAsmInfoDarwin(TheTriple);
120  else
121  MAI = new X86MCAsmInfoDarwin(TheTriple);
122  } else if (TheTriple.isOSBinFormatELF()) {
123  // Force the use of an ELF container.
124  MAI = new X86ELFMCAsmInfo(TheTriple);
125  } else if (TheTriple.isWindowsMSVCEnvironment()) {
126  MAI = new X86MCAsmInfoMicrosoft(TheTriple);
127  } else if (TheTriple.isOSCygMing() ||
128  TheTriple.isWindowsItaniumEnvironment()) {
129  MAI = new X86MCAsmInfoGNUCOFF(TheTriple);
130  } else {
131  // The default is ELF.
132  MAI = new X86ELFMCAsmInfo(TheTriple);
133  }
134 
135  // Initialize initial frame state.
136  // Calculate amount of bytes used for return address storing
137  int stackGrowth = is64Bit ? -8 : -4;
138 
139  // Initial state of the frame pointer is esp+stackGrowth.
140  unsigned StackPtr = is64Bit ? X86::RSP : X86::ESP;
142  nullptr, MRI.getDwarfRegNum(StackPtr, true), -stackGrowth);
143  MAI->addInitialFrameState(Inst);
144 
145  // Add return address to move list
146  unsigned InstPtr = is64Bit ? X86::RIP : X86::EIP;
148  nullptr, MRI.getDwarfRegNum(InstPtr, true), stackGrowth);
149  MAI->addInitialFrameState(Inst2);
150 
151  return MAI;
152 }
153 
155  CodeModel::Model CM,
156  CodeGenOpt::Level OL) {
157  MCCodeGenInfo *X = new MCCodeGenInfo();
158 
159  bool is64Bit = TT.getArch() == Triple::x86_64;
160 
161  if (RM == Reloc::Default) {
162  // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
163  // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
164  // use static relocation model by default.
165  if (TT.isOSDarwin()) {
166  if (is64Bit)
167  RM = Reloc::PIC_;
168  else
169  RM = Reloc::DynamicNoPIC;
170  } else if (TT.isOSWindows() && is64Bit)
171  RM = Reloc::PIC_;
172  else
173  RM = Reloc::Static;
174  }
175 
176  // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
177  // is defined as a model for code which may be used in static or dynamic
178  // executables but not necessarily a shared library. On X86-32 we just
179  // compile in -static mode, in x86-64 we use PIC.
180  if (RM == Reloc::DynamicNoPIC) {
181  if (is64Bit)
182  RM = Reloc::PIC_;
183  else if (!TT.isOSDarwin())
184  RM = Reloc::Static;
185  }
186 
187  // If we are on Darwin, disallow static relocation model in X86-64 mode, since
188  // the Mach-O file format doesn't support it.
189  if (RM == Reloc::Static && TT.isOSDarwin() && is64Bit)
190  RM = Reloc::PIC_;
191 
192  // For static codegen, if we're not already set, use Small codegen.
193  if (CM == CodeModel::Default)
194  CM = CodeModel::Small;
195  else if (CM == CodeModel::JITDefault)
196  // 64-bit JIT places everything in the same buffer except external funcs.
197  CM = is64Bit ? CodeModel::Large : CodeModel::Small;
198 
199  X->initMCCodeGenInfo(RM, CM, OL);
200  return X;
201 }
202 
204  unsigned SyntaxVariant,
205  const MCAsmInfo &MAI,
206  const MCInstrInfo &MII,
207  const MCRegisterInfo &MRI) {
208  if (SyntaxVariant == 0)
209  return new X86ATTInstPrinter(MAI, MII, MRI);
210  if (SyntaxVariant == 1)
211  return new X86IntelInstPrinter(MAI, MII, MRI);
212  return nullptr;
213 }
214 
216  MCContext &Ctx) {
217  if (TheTriple.isOSBinFormatMachO() && TheTriple.getArch() == Triple::x86_64)
219  else if (TheTriple.isOSBinFormatELF())
220  return createX86_64ELFRelocationInfo(Ctx);
221  // Default to the stock relocation info.
222  return llvm::createMCRelocationInfo(TheTriple, Ctx);
223 }
224 
226  return new MCInstrAnalysis(Info);
227 }
228 
229 // Force static initialization.
230 extern "C" void LLVMInitializeX86TargetMC() {
231  for (Target *T : {&TheX86_32Target, &TheX86_64Target}) {
232  // Register the MC asm info.
234 
235  // Register the MC codegen info.
237 
238  // Register the MC instruction info.
240 
241  // Register the MC register info.
243 
244  // Register the MC subtarget info.
247 
248  // Register the MC instruction analyzer.
250 
251  // Register the code emitter.
253 
254  // Register the object streamer.
256 
257  // Register the MCInstPrinter.
259 
260  // Register the MC relocation info.
262  }
263 
264  // Register the asm backend.
269 }
int getDwarfRegNum(unsigned RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Definition: Triple.h:489
MCAsmBackend * createX86_64AsmBackend(const Target &T, const MCRegisterInfo &MRI, const Triple &TT, StringRef CPU)
MCAsmBackend * createX86_32AsmBackend(const Target &T, const MCRegisterInfo &MRI, const Triple &TT, StringRef CPU)
static void RegisterMCInstrAnalysis(Target &T, Target::MCInstrAnalysisCtorFnTy Fn)
RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for the given target...
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:381
static MCRegisterInfo * createX86MCRegisterInfo(const Triple &TT)
static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn)
RegisterMCInstPrinter - Register a MCInstPrinter implementation for the given target.
unsigned getDwarfRegFlavour(const Triple &TT, bool isEH)
std::string ParseX86Triple(const Triple &TT)
bool isOSWindows() const
Tests whether the OS is Windows.
Definition: Triple.h:464
bool isOSCygMing() const
Tests for either Cygwin or MinGW OS.
Definition: Triple.h:453
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
MCCodeEmitter * createX86MCCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo &MRI, MCContext &Ctx)
Target TheX86_64Target
Reg
All possible values of the reg field in the ModR/M byte.
static void RegisterCOFFStreamer(Target &T, Target::COFFStreamerCtorTy Fn)
bool isWindowsMSVCEnvironment() const
Definition: Triple.h:430
Context object for machine code objects.
Definition: MCContext.h:48
static MCRelocationInfo * createX86MCRelocationInfo(const Triple &TheTriple, MCContext &Ctx)
static MCCodeGenInfo * createX86MCCodeGenInfo(const Triple &TT, Reloc::Model RM, CodeModel::Model CM, CodeGenOpt::Level OL)
void addInitialFrameState(const MCCFIInstruction &Inst)
Definition: MCAsmInfo.h:534
MCRelocationInfo * createX86_64ELFRelocationInfo(MCContext &Ctx)
Construct X86-64 ELF relocation info.
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:242
void LLVMInitializeX86TargetMC()
MCRelocationInfo * createMCRelocationInfo(const Triple &TT, MCContext &Ctx)
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:58
static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn)
RegisterMCAsmBackend - Register a MCAsmBackend implementation for the given target.
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:354
static bool is64Bit(const char *name)
Target TheX86_32Target
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
Create MCExprs from relocations found in an object file.
static void RegisterMCSubtargetInfo(Target &T, Target::MCSubtargetInfoCtorFnTy Fn)
RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for the given target...
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
MCSubtargetInfo * createX86MCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS)
Create a X86 MCSubtargetInfo instance.
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
Definition: Triple.h:404
void InitLLVM2SEHRegisterMapping(MCRegisterInfo *MRI)
static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn)
RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the given target.
MCRelocationInfo * createX86_64MachORelocationInfo(MCContext &Ctx)
Construct X86-64 Mach-O relocation info.
static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn)
RegisterMCRegInfo - Register a MCRegisterInfo implementation for the given target.
Target - Wrapper for Target specific information.
static MCAsmInfo * createX86MCAsmInfo(const MCRegisterInfo &MRI, const Triple &TheTriple)
RegisterMCAsmInfoFn - Helper template for registering a target assembly info implementation.
static MCInstrInfo * createX86MCInstrInfo()
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:38
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:479
void initMCCodeGenInfo(Reloc::Model RM=Reloc::Default, CodeModel::Model CM=CodeModel::Default, CodeGenOpt::Level OL=CodeGenOpt::Default)
static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn)
RegisterMCInstrInfo - Register a MCInstrInfo implementation for the given target. ...
MCSubtargetInfo - Generic base class for all target subtargets.
MCStreamer * createX86WinCOFFStreamer(MCContext &C, MCAsmBackend &AB, raw_pwrite_stream &OS, MCCodeEmitter *CE, bool RelaxAll)
Construct an X86 Windows COFF machine code streamer which will generate PE/COFF format object files...
EnvironmentType getEnvironment() const
getEnvironment - Get the parsed environment type of this triple.
Definition: Triple.h:260
uint16_t getEncodingValue(unsigned RegNo) const
Returns the encoding for RegNo.
bool isWindowsItaniumEnvironment() const
Definition: Triple.h:440
static void RegisterMCRelocationInfo(Target &T, Target::MCRelocationInfoCtorTy Fn)
RegisterMCRelocationInfo - Register an MCRelocationInfo implementation for the given target...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
static MCInstrAnalysis * createX86MCInstrAnalysis(const MCInstrInfo *Info)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
RegisterMCCodeGenInfoFn - Helper template for registering a target codegen info implementation.
static MCInstPrinter * createX86MCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI)
void mapLLVMRegToSEHReg(unsigned LLVMReg, int SEHReg)
mapLLVMRegToSEHReg - Used to initialize LLVM register to SEH register number mapping.
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110