1 //===-- X86MCTargetDesc.cpp - X86 Target Descriptions ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides X86 specific target descriptions.
12 //===----------------------------------------------------------------------===//
14 #include "X86MCTargetDesc.h"
15 #include "InstPrinter/X86ATTInstPrinter.h"
16 #include "InstPrinter/X86IntelInstPrinter.h"
17 #include "X86MCAsmInfo.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/MC/MCCodeGenInfo.h"
20 #include "llvm/MC/MCInstrAnalysis.h"
21 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/MC/MachineLocation.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/Host.h"
28 #include "llvm/Support/TargetRegistry.h"
30 #define GET_REGINFO_MC_DESC
31 #include "X86GenRegisterInfo.inc"
33 #define GET_INSTRINFO_MC_DESC
34 #include "X86GenInstrInfo.inc"
36 #define GET_SUBTARGETINFO_MC_DESC
37 #include "X86GenSubtargetInfo.inc"
46 std::string X86_MC::ParseX86Triple(StringRef TT) {
49 if (TheTriple.getArch() == Triple::x86_64)
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)
62 // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
63 asm ("movq\t%%rbx, %%rsi\n\t"
65 "xchgq\t%%rbx, %%rsi\n\t"
72 #elif defined(_MSC_VER)
74 __cpuid(registers, value);
83 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
85 asm ("movl\t%%ebx, %%esi\n\t"
87 "xchgl\t%%ebx, %%esi\n\t"
94 #elif defined(_MSC_VER)
99 mov dword ptr [esi],eax
101 mov dword ptr [esi],ebx
103 mov dword ptr [esi],ecx
105 mov dword ptr [esi],edx
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,
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"
126 "xchgq\t%%rbx, %%rsi\n\t"
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)
138 __cpuidex(registers, value, subleaf);
139 *rEAX = registers[0];
140 *rEBX = registers[1];
141 *rECX = registers[2];
142 *rEDX = registers[3];
150 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
151 #if defined(__GNUC__)
152 asm ("movl\t%%ebx, %%esi\n\t"
154 "xchgl\t%%ebx, %%esi\n\t"
162 #elif defined(_MSC_VER)
168 mov dword ptr [esi],eax
170 mov dword ptr [esi],ebx
172 mov dword ptr [esi],ecx
174 mov dword ptr [esi],edx
185 void X86_MC::DetectFamilyModel(unsigned EAX, unsigned &Family,
187 Family = (EAX >> 8) & 0xf; // Bits 8 - 11
188 Model = (EAX >> 4) & 0xf; // Bits 4 - 7
189 if (Family == 6 || 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
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;
203 if (TheTriple.isOSDarwin())
204 return isEH ? DWARFFlavour::X86_32_DarwinEH : DWARFFlavour::X86_32_Generic;
205 if (TheTriple.getOS() == Triple::MinGW32 ||
206 TheTriple.getOS() == Triple::Cygwin)
207 // Unsupported by now, just quick fallback
208 return DWARFFlavour::X86_32_Generic;
209 return DWARFFlavour::X86_32_Generic;
212 void X86_MC::InitLLVM2SEHRegisterMapping(MCRegisterInfo *MRI) {
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);
220 MCSubtargetInfo *X86_MC::createX86MCSubtargetInfo(StringRef TT, StringRef CPU,
222 std::string ArchFS = X86_MC::ParseX86Triple(TT);
225 ArchFS = ArchFS + "," + FS.str();
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();
240 MCSubtargetInfo *X = new MCSubtargetInfo();
241 InitX86MCSubtargetInfo(X, TT, CPUName, ArchFS);
245 static MCInstrInfo *createX86MCInstrInfo() {
246 MCInstrInfo *X = new MCInstrInfo();
247 InitX86MCInstrInfo(X);
251 static MCRegisterInfo *createX86MCRegisterInfo(StringRef TT) {
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.
257 MCRegisterInfo *X = new MCRegisterInfo();
258 InitX86MCRegisterInfo(X, RA,
259 X86_MC::getDwarfRegFlavour(TT, false),
260 X86_MC::getDwarfRegFlavour(TT, true),
262 X86_MC::InitLLVM2SEHRegisterMapping(X);
266 static MCAsmInfo *createX86MCAsmInfo(const MCRegisterInfo &MRI, StringRef TT) {
267 Triple TheTriple(TT);
268 bool is64Bit = TheTriple.getArch() == Triple::x86_64;
271 if (TheTriple.isOSDarwin() || TheTriple.getEnvironment() == Triple::MachO) {
273 MAI = new X86_64MCAsmInfoDarwin(TheTriple);
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);
284 // The default is ELF.
285 MAI = new X86ELFMCAsmInfo(TheTriple);
288 // Initialize initial frame state.
289 // Calculate amount of bytes used for return address storing
290 int stackGrowth = is64Bit ? -8 : -4;
292 // Initial state of the frame pointer is esp+stackGrowth.
293 unsigned StackPtr = is64Bit ? X86::RSP : X86::ESP;
294 MCCFIInstruction Inst = MCCFIInstruction::createDefCfa(
295 0, MRI.getDwarfRegNum(StackPtr, true), -stackGrowth);
296 MAI->addInitialFrameState(Inst);
298 // Add return address to move list
299 unsigned InstPtr = is64Bit ? X86::RIP : X86::EIP;
300 MCCFIInstruction Inst2 = MCCFIInstruction::createOffset(
301 0, MRI.getDwarfRegNum(InstPtr, true), stackGrowth);
302 MAI->addInitialFrameState(Inst2);
307 static MCCodeGenInfo *createX86MCCodeGenInfo(StringRef TT, Reloc::Model RM,
309 CodeGenOpt::Level OL) {
310 MCCodeGenInfo *X = new MCCodeGenInfo();
313 bool is64Bit = T.getArch() == Triple::x86_64;
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()) {
323 RM = Reloc::DynamicNoPIC;
324 } else if (T.isOSWindows() && is64Bit)
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) {
337 else if (!T.isOSDarwin())
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)
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;
353 X->InitMCCodeGenInfo(RM, CM, OL);
357 static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
358 MCContext &Ctx, MCAsmBackend &MAB,
360 MCCodeEmitter *_Emitter,
363 Triple TheTriple(TT);
365 if (TheTriple.isOSDarwin() || TheTriple.getEnvironment() == Triple::MachO)
366 return createMachOStreamer(Ctx, MAB, _OS, _Emitter, RelaxAll);
368 if (TheTriple.isOSWindows() && TheTriple.getEnvironment() != Triple::ELF)
369 return createWinCOFFStreamer(Ctx, MAB, *_Emitter, _OS, RelaxAll);
371 return createELFStreamer(Ctx, 0, MAB, _OS, _Emitter, RelaxAll, NoExecStack);
374 static MCInstPrinter *createX86MCInstPrinter(const Target &T,
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);
387 static MCRelocationInfo *createX86MCRelocationInfo(StringRef TT,
389 Triple TheTriple(TT);
390 if (TheTriple.isOSBinFormatMachO() && TheTriple.getArch() == Triple::x86_64)
391 return createX86_64MachORelocationInfo(Ctx);
392 else if (TheTriple.isOSBinFormatELF())
393 return createX86_64ELFRelocationInfo(Ctx);
394 // Default to the stock relocation info.
395 return llvm::createMCRelocationInfo(TT, Ctx);
398 static MCInstrAnalysis *createX86MCInstrAnalysis(const MCInstrInfo *Info) {
399 return new MCInstrAnalysis(Info);
402 // Force static initialization.
403 extern "C" void LLVMInitializeX86TargetMC() {
404 // Register the MC asm info.
405 RegisterMCAsmInfoFn A(TheX86_32Target, createX86MCAsmInfo);
406 RegisterMCAsmInfoFn B(TheX86_64Target, createX86MCAsmInfo);
408 // Register the MC codegen info.
409 RegisterMCCodeGenInfoFn C(TheX86_32Target, createX86MCCodeGenInfo);
410 RegisterMCCodeGenInfoFn D(TheX86_64Target, createX86MCCodeGenInfo);
412 // Register the MC instruction info.
413 TargetRegistry::RegisterMCInstrInfo(TheX86_32Target, createX86MCInstrInfo);
414 TargetRegistry::RegisterMCInstrInfo(TheX86_64Target, createX86MCInstrInfo);
416 // Register the MC register info.
417 TargetRegistry::RegisterMCRegInfo(TheX86_32Target, createX86MCRegisterInfo);
418 TargetRegistry::RegisterMCRegInfo(TheX86_64Target, createX86MCRegisterInfo);
420 // Register the MC subtarget info.
421 TargetRegistry::RegisterMCSubtargetInfo(TheX86_32Target,
422 X86_MC::createX86MCSubtargetInfo);
423 TargetRegistry::RegisterMCSubtargetInfo(TheX86_64Target,
424 X86_MC::createX86MCSubtargetInfo);
426 // Register the MC instruction analyzer.
427 TargetRegistry::RegisterMCInstrAnalysis(TheX86_32Target,
428 createX86MCInstrAnalysis);
429 TargetRegistry::RegisterMCInstrAnalysis(TheX86_64Target,
430 createX86MCInstrAnalysis);
432 // Register the code emitter.
433 TargetRegistry::RegisterMCCodeEmitter(TheX86_32Target,
434 createX86MCCodeEmitter);
435 TargetRegistry::RegisterMCCodeEmitter(TheX86_64Target,
436 createX86MCCodeEmitter);
438 // Register the asm backend.
439 TargetRegistry::RegisterMCAsmBackend(TheX86_32Target,
440 createX86_32AsmBackend);
441 TargetRegistry::RegisterMCAsmBackend(TheX86_64Target,
442 createX86_64AsmBackend);
444 // Register the object streamer.
445 TargetRegistry::RegisterMCObjectStreamer(TheX86_32Target,
447 TargetRegistry::RegisterMCObjectStreamer(TheX86_64Target,
450 // Register the MCInstPrinter.
451 TargetRegistry::RegisterMCInstPrinter(TheX86_32Target,
452 createX86MCInstPrinter);
453 TargetRegistry::RegisterMCInstPrinter(TheX86_64Target,
454 createX86MCInstPrinter);
456 // Register the MC relocation info.
457 TargetRegistry::RegisterMCRelocationInfo(TheX86_32Target,
458 createX86MCRelocationInfo);
459 TargetRegistry::RegisterMCRelocationInfo(TheX86_64Target,
460 createX86MCRelocationInfo);