LLVM API Documentation

X86MCTargetDesc.cpp
Go to the documentation of this file.
00001 //===-- X86MCTargetDesc.cpp - X86 Target Descriptions ---------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file provides X86 specific target descriptions.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "X86MCTargetDesc.h"
00015 #include "InstPrinter/X86ATTInstPrinter.h"
00016 #include "InstPrinter/X86IntelInstPrinter.h"
00017 #include "X86MCAsmInfo.h"
00018 #include "llvm/ADT/Triple.h"
00019 #include "llvm/MC/MCCodeGenInfo.h"
00020 #include "llvm/MC/MCInstrAnalysis.h"
00021 #include "llvm/MC/MCInstrInfo.h"
00022 #include "llvm/MC/MCRegisterInfo.h"
00023 #include "llvm/MC/MCStreamer.h"
00024 #include "llvm/MC/MCSubtargetInfo.h"
00025 #include "llvm/MC/MachineLocation.h"
00026 #include "llvm/Support/ErrorHandling.h"
00027 #include "llvm/Support/Host.h"
00028 #include "llvm/Support/TargetRegistry.h"
00029 
00030 #if _MSC_VER
00031 #include <intrin.h>
00032 #endif
00033 
00034 using namespace llvm;
00035 
00036 #define GET_REGINFO_MC_DESC
00037 #include "X86GenRegisterInfo.inc"
00038 
00039 #define GET_INSTRINFO_MC_DESC
00040 #include "X86GenInstrInfo.inc"
00041 
00042 #define GET_SUBTARGETINFO_MC_DESC
00043 #include "X86GenSubtargetInfo.inc"
00044 
00045 std::string X86_MC::ParseX86Triple(StringRef TT) {
00046   Triple TheTriple(TT);
00047   std::string FS;
00048   if (TheTriple.getArch() == Triple::x86_64)
00049     FS = "+64bit-mode,-32bit-mode,-16bit-mode";
00050   else if (TheTriple.getEnvironment() != Triple::CODE16)
00051     FS = "-64bit-mode,+32bit-mode,-16bit-mode";
00052   else
00053     FS = "-64bit-mode,-32bit-mode,+16bit-mode";
00054 
00055   return FS;
00056 }
00057 
00058 /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
00059 /// specified arguments.  If we can't run cpuid on the host, return true.
00060 bool X86_MC::GetCpuIDAndInfo(unsigned value, unsigned *rEAX,
00061                              unsigned *rEBX, unsigned *rECX, unsigned *rEDX) {
00062 #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
00063   #if defined(__GNUC__)
00064     // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
00065     asm ("movq\t%%rbx, %%rsi\n\t"
00066          "cpuid\n\t"
00067          "xchgq\t%%rbx, %%rsi\n\t"
00068          : "=a" (*rEAX),
00069            "=S" (*rEBX),
00070            "=c" (*rECX),
00071            "=d" (*rEDX)
00072          :  "a" (value));
00073     return false;
00074   #elif defined(_MSC_VER)
00075     int registers[4];
00076     __cpuid(registers, value);
00077     *rEAX = registers[0];
00078     *rEBX = registers[1];
00079     *rECX = registers[2];
00080     *rEDX = registers[3];
00081     return false;
00082   #else
00083     return true;
00084   #endif
00085 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
00086   #if defined(__GNUC__)
00087     asm ("movl\t%%ebx, %%esi\n\t"
00088          "cpuid\n\t"
00089          "xchgl\t%%ebx, %%esi\n\t"
00090          : "=a" (*rEAX),
00091            "=S" (*rEBX),
00092            "=c" (*rECX),
00093            "=d" (*rEDX)
00094          :  "a" (value));
00095     return false;
00096   #elif defined(_MSC_VER)
00097     __asm {
00098       mov   eax,value
00099       cpuid
00100       mov   esi,rEAX
00101       mov   dword ptr [esi],eax
00102       mov   esi,rEBX
00103       mov   dword ptr [esi],ebx
00104       mov   esi,rECX
00105       mov   dword ptr [esi],ecx
00106       mov   esi,rEDX
00107       mov   dword ptr [esi],edx
00108     }
00109     return false;
00110   #else
00111     return true;
00112   #endif
00113 #else
00114   return true;
00115 #endif
00116 }
00117 
00118 /// GetCpuIDAndInfoEx - Execute the specified cpuid with subleaf and return the
00119 /// 4 values in the specified arguments.  If we can't run cpuid on the host,
00120 /// return true.
00121 bool X86_MC::GetCpuIDAndInfoEx(unsigned value, unsigned subleaf, unsigned *rEAX,
00122                                unsigned *rEBX, unsigned *rECX, unsigned *rEDX) {
00123 #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
00124   #if defined(__GNUC__)
00125     // gcc desn't know cpuid would clobber ebx/rbx. Preseve it manually.
00126     asm ("movq\t%%rbx, %%rsi\n\t"
00127          "cpuid\n\t"
00128          "xchgq\t%%rbx, %%rsi\n\t"
00129          : "=a" (*rEAX),
00130            "=S" (*rEBX),
00131            "=c" (*rECX),
00132            "=d" (*rEDX)
00133          :  "a" (value),
00134             "c" (subleaf));
00135     return false;
00136   #elif defined(_MSC_VER)
00137     // __cpuidex was added in MSVC++ 9.0 SP1
00138     #if (_MSC_VER > 1500) || (_MSC_VER == 1500 && _MSC_FULL_VER >= 150030729)
00139       int registers[4];
00140       __cpuidex(registers, value, subleaf);
00141       *rEAX = registers[0];
00142       *rEBX = registers[1];
00143       *rECX = registers[2];
00144       *rEDX = registers[3];
00145       return false;
00146     #else
00147       return true;
00148     #endif
00149   #else
00150     return true;
00151   #endif
00152 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
00153   #if defined(__GNUC__)
00154     asm ("movl\t%%ebx, %%esi\n\t"
00155          "cpuid\n\t"
00156          "xchgl\t%%ebx, %%esi\n\t"
00157          : "=a" (*rEAX),
00158            "=S" (*rEBX),
00159            "=c" (*rECX),
00160            "=d" (*rEDX)
00161          :  "a" (value),
00162             "c" (subleaf));
00163     return false;
00164   #elif defined(_MSC_VER)
00165     __asm {
00166       mov   eax,value
00167       mov   ecx,subleaf
00168       cpuid
00169       mov   esi,rEAX
00170       mov   dword ptr [esi],eax
00171       mov   esi,rEBX
00172       mov   dword ptr [esi],ebx
00173       mov   esi,rECX
00174       mov   dword ptr [esi],ecx
00175       mov   esi,rEDX
00176       mov   dword ptr [esi],edx
00177     }
00178     return false;
00179   #else
00180     return true;
00181   #endif
00182 #else
00183   return true;
00184 #endif
00185 }
00186 
00187 void X86_MC::DetectFamilyModel(unsigned EAX, unsigned &Family,
00188                                unsigned &Model) {
00189   Family = (EAX >> 8) & 0xf; // Bits 8 - 11
00190   Model  = (EAX >> 4) & 0xf; // Bits 4 - 7
00191   if (Family == 6 || Family == 0xf) {
00192     if (Family == 0xf)
00193       // Examine extended family ID if family ID is F.
00194       Family += (EAX >> 20) & 0xff;    // Bits 20 - 27
00195     // Examine extended model ID if family ID is 6 or F.
00196     Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
00197   }
00198 }
00199 
00200 unsigned X86_MC::getDwarfRegFlavour(Triple TT, bool isEH) {
00201   if (TT.getArch() == Triple::x86_64)
00202     return DWARFFlavour::X86_64;
00203 
00204   if (TT.isOSDarwin())
00205     return isEH ? DWARFFlavour::X86_32_DarwinEH : DWARFFlavour::X86_32_Generic;
00206   if (TT.isOSCygMing())
00207     // Unsupported by now, just quick fallback
00208     return DWARFFlavour::X86_32_Generic;
00209   return DWARFFlavour::X86_32_Generic;
00210 }
00211 
00212 void X86_MC::InitLLVM2SEHRegisterMapping(MCRegisterInfo *MRI) {
00213   // FIXME: TableGen these.
00214   for (unsigned Reg = X86::NoRegister+1; Reg < X86::NUM_TARGET_REGS; ++Reg) {
00215     unsigned SEH = MRI->getEncodingValue(Reg);
00216     MRI->mapLLVMRegToSEHReg(Reg, SEH);
00217   }
00218 }
00219 
00220 MCSubtargetInfo *X86_MC::createX86MCSubtargetInfo(StringRef TT, StringRef CPU,
00221                                                   StringRef FS) {
00222   std::string ArchFS = X86_MC::ParseX86Triple(TT);
00223   if (!FS.empty()) {
00224     if (!ArchFS.empty())
00225       ArchFS = ArchFS + "," + FS.str();
00226     else
00227       ArchFS = FS;
00228   }
00229 
00230   std::string CPUName = CPU;
00231   if (CPUName.empty())
00232     CPUName = "generic";
00233 
00234   MCSubtargetInfo *X = new MCSubtargetInfo();
00235   InitX86MCSubtargetInfo(X, TT, CPUName, ArchFS);
00236   return X;
00237 }
00238 
00239 static MCInstrInfo *createX86MCInstrInfo() {
00240   MCInstrInfo *X = new MCInstrInfo();
00241   InitX86MCInstrInfo(X);
00242   return X;
00243 }
00244 
00245 static MCRegisterInfo *createX86MCRegisterInfo(StringRef TT) {
00246   Triple TheTriple(TT);
00247   unsigned RA = (TheTriple.getArch() == Triple::x86_64)
00248     ? X86::RIP     // Should have dwarf #16.
00249     : X86::EIP;    // Should have dwarf #8.
00250 
00251   MCRegisterInfo *X = new MCRegisterInfo();
00252   InitX86MCRegisterInfo(X, RA,
00253                         X86_MC::getDwarfRegFlavour(TheTriple, false),
00254                         X86_MC::getDwarfRegFlavour(TheTriple, true),
00255                         RA);
00256   X86_MC::InitLLVM2SEHRegisterMapping(X);
00257   return X;
00258 }
00259 
00260 static MCAsmInfo *createX86MCAsmInfo(const MCRegisterInfo &MRI, StringRef TT) {
00261   Triple TheTriple(TT);
00262   bool is64Bit = TheTriple.getArch() == Triple::x86_64;
00263 
00264   MCAsmInfo *MAI;
00265   if (TheTriple.isOSBinFormatMachO()) {
00266     if (is64Bit)
00267       MAI = new X86_64MCAsmInfoDarwin(TheTriple);
00268     else
00269       MAI = new X86MCAsmInfoDarwin(TheTriple);
00270   } else if (TheTriple.isOSBinFormatELF()) {
00271     // Force the use of an ELF container.
00272     MAI = new X86ELFMCAsmInfo(TheTriple);
00273   } else if (TheTriple.isWindowsMSVCEnvironment()) {
00274     MAI = new X86MCAsmInfoMicrosoft(TheTriple);
00275   } else if (TheTriple.isOSCygMing() ||
00276              TheTriple.isWindowsItaniumEnvironment()) {
00277     MAI = new X86MCAsmInfoGNUCOFF(TheTriple);
00278   } else {
00279     // The default is ELF.
00280     MAI = new X86ELFMCAsmInfo(TheTriple);
00281   }
00282 
00283   // Initialize initial frame state.
00284   // Calculate amount of bytes used for return address storing
00285   int stackGrowth = is64Bit ? -8 : -4;
00286 
00287   // Initial state of the frame pointer is esp+stackGrowth.
00288   unsigned StackPtr = is64Bit ? X86::RSP : X86::ESP;
00289   MCCFIInstruction Inst = MCCFIInstruction::createDefCfa(
00290       nullptr, MRI.getDwarfRegNum(StackPtr, true), -stackGrowth);
00291   MAI->addInitialFrameState(Inst);
00292 
00293   // Add return address to move list
00294   unsigned InstPtr = is64Bit ? X86::RIP : X86::EIP;
00295   MCCFIInstruction Inst2 = MCCFIInstruction::createOffset(
00296       nullptr, MRI.getDwarfRegNum(InstPtr, true), stackGrowth);
00297   MAI->addInitialFrameState(Inst2);
00298 
00299   return MAI;
00300 }
00301 
00302 static MCCodeGenInfo *createX86MCCodeGenInfo(StringRef TT, Reloc::Model RM,
00303                                              CodeModel::Model CM,
00304                                              CodeGenOpt::Level OL) {
00305   MCCodeGenInfo *X = new MCCodeGenInfo();
00306 
00307   Triple T(TT);
00308   bool is64Bit = T.getArch() == Triple::x86_64;
00309 
00310   if (RM == Reloc::Default) {
00311     // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
00312     // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
00313     // use static relocation model by default.
00314     if (T.isOSDarwin()) {
00315       if (is64Bit)
00316         RM = Reloc::PIC_;
00317       else
00318         RM = Reloc::DynamicNoPIC;
00319     } else if (T.isOSWindows() && is64Bit)
00320       RM = Reloc::PIC_;
00321     else
00322       RM = Reloc::Static;
00323   }
00324 
00325   // ELF and X86-64 don't have a distinct DynamicNoPIC model.  DynamicNoPIC
00326   // is defined as a model for code which may be used in static or dynamic
00327   // executables but not necessarily a shared library. On X86-32 we just
00328   // compile in -static mode, in x86-64 we use PIC.
00329   if (RM == Reloc::DynamicNoPIC) {
00330     if (is64Bit)
00331       RM = Reloc::PIC_;
00332     else if (!T.isOSDarwin())
00333       RM = Reloc::Static;
00334   }
00335 
00336   // If we are on Darwin, disallow static relocation model in X86-64 mode, since
00337   // the Mach-O file format doesn't support it.
00338   if (RM == Reloc::Static && T.isOSDarwin() && is64Bit)
00339     RM = Reloc::PIC_;
00340 
00341   // For static codegen, if we're not already set, use Small codegen.
00342   if (CM == CodeModel::Default)
00343     CM = CodeModel::Small;
00344   else if (CM == CodeModel::JITDefault)
00345     // 64-bit JIT places everything in the same buffer except external funcs.
00346     CM = is64Bit ? CodeModel::Large : CodeModel::Small;
00347 
00348   X->InitMCCodeGenInfo(RM, CM, OL);
00349   return X;
00350 }
00351 
00352 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
00353                                     MCContext &Ctx, MCAsmBackend &MAB,
00354                                     raw_ostream &_OS,
00355                                     MCCodeEmitter *_Emitter,
00356                                     const MCSubtargetInfo &STI,
00357                                     bool RelaxAll,
00358                                     bool NoExecStack) {
00359   Triple TheTriple(TT);
00360 
00361   switch (TheTriple.getObjectFormat()) {
00362   default: llvm_unreachable("unsupported object format");
00363   case Triple::MachO:
00364     return createMachOStreamer(Ctx, MAB, _OS, _Emitter, RelaxAll);
00365   case Triple::COFF:
00366     assert(TheTriple.isOSWindows() && "only Windows COFF is supported");
00367     return createX86WinCOFFStreamer(Ctx, MAB, _Emitter, _OS, RelaxAll);
00368   case Triple::ELF:
00369     return createELFStreamer(Ctx, MAB, _OS, _Emitter, RelaxAll, NoExecStack);
00370   }
00371 }
00372 
00373 static MCInstPrinter *createX86MCInstPrinter(const Target &T,
00374                                              unsigned SyntaxVariant,
00375                                              const MCAsmInfo &MAI,
00376                                              const MCInstrInfo &MII,
00377                                              const MCRegisterInfo &MRI,
00378                                              const MCSubtargetInfo &STI) {
00379   if (SyntaxVariant == 0)
00380     return new X86ATTInstPrinter(MAI, MII, MRI, STI);
00381   if (SyntaxVariant == 1)
00382     return new X86IntelInstPrinter(MAI, MII, MRI);
00383   return nullptr;
00384 }
00385 
00386 static MCRelocationInfo *createX86MCRelocationInfo(StringRef TT,
00387                                                    MCContext &Ctx) {
00388   Triple TheTriple(TT);
00389   if (TheTriple.isOSBinFormatMachO() && TheTriple.getArch() == Triple::x86_64)
00390     return createX86_64MachORelocationInfo(Ctx);
00391   else if (TheTriple.isOSBinFormatELF())
00392     return createX86_64ELFRelocationInfo(Ctx);
00393   // Default to the stock relocation info.
00394   return llvm::createMCRelocationInfo(TT, Ctx);
00395 }
00396 
00397 static MCInstrAnalysis *createX86MCInstrAnalysis(const MCInstrInfo *Info) {
00398   return new MCInstrAnalysis(Info);
00399 }
00400 
00401 // Force static initialization.
00402 extern "C" void LLVMInitializeX86TargetMC() {
00403   // Register the MC asm info.
00404   RegisterMCAsmInfoFn A(TheX86_32Target, createX86MCAsmInfo);
00405   RegisterMCAsmInfoFn B(TheX86_64Target, createX86MCAsmInfo);
00406 
00407   // Register the MC codegen info.
00408   RegisterMCCodeGenInfoFn C(TheX86_32Target, createX86MCCodeGenInfo);
00409   RegisterMCCodeGenInfoFn D(TheX86_64Target, createX86MCCodeGenInfo);
00410 
00411   // Register the MC instruction info.
00412   TargetRegistry::RegisterMCInstrInfo(TheX86_32Target, createX86MCInstrInfo);
00413   TargetRegistry::RegisterMCInstrInfo(TheX86_64Target, createX86MCInstrInfo);
00414 
00415   // Register the MC register info.
00416   TargetRegistry::RegisterMCRegInfo(TheX86_32Target, createX86MCRegisterInfo);
00417   TargetRegistry::RegisterMCRegInfo(TheX86_64Target, createX86MCRegisterInfo);
00418 
00419   // Register the MC subtarget info.
00420   TargetRegistry::RegisterMCSubtargetInfo(TheX86_32Target,
00421                                           X86_MC::createX86MCSubtargetInfo);
00422   TargetRegistry::RegisterMCSubtargetInfo(TheX86_64Target,
00423                                           X86_MC::createX86MCSubtargetInfo);
00424 
00425   // Register the MC instruction analyzer.
00426   TargetRegistry::RegisterMCInstrAnalysis(TheX86_32Target,
00427                                           createX86MCInstrAnalysis);
00428   TargetRegistry::RegisterMCInstrAnalysis(TheX86_64Target,
00429                                           createX86MCInstrAnalysis);
00430 
00431   // Register the code emitter.
00432   TargetRegistry::RegisterMCCodeEmitter(TheX86_32Target,
00433                                         createX86MCCodeEmitter);
00434   TargetRegistry::RegisterMCCodeEmitter(TheX86_64Target,
00435                                         createX86MCCodeEmitter);
00436 
00437   // Register the asm backend.
00438   TargetRegistry::RegisterMCAsmBackend(TheX86_32Target,
00439                                        createX86_32AsmBackend);
00440   TargetRegistry::RegisterMCAsmBackend(TheX86_64Target,
00441                                        createX86_64AsmBackend);
00442 
00443   // Register the object streamer.
00444   TargetRegistry::RegisterMCObjectStreamer(TheX86_32Target,
00445                                            createMCStreamer);
00446   TargetRegistry::RegisterMCObjectStreamer(TheX86_64Target,
00447                                            createMCStreamer);
00448 
00449   // Register the MCInstPrinter.
00450   TargetRegistry::RegisterMCInstPrinter(TheX86_32Target,
00451                                         createX86MCInstPrinter);
00452   TargetRegistry::RegisterMCInstPrinter(TheX86_64Target,
00453                                         createX86MCInstPrinter);
00454 
00455   // Register the MC relocation info.
00456   TargetRegistry::RegisterMCRelocationInfo(TheX86_32Target,
00457                                            createX86MCRelocationInfo);
00458   TargetRegistry::RegisterMCRelocationInfo(TheX86_64Target,
00459                                            createX86MCRelocationInfo);
00460 }