Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- X86MCTargetDesc.cpp - X86 Target Descriptions ---------------------===// |
Evan Cheng | 2475331 | 2011-06-24 01:44:41 +0000 | [diff] [blame] | 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 | |
Evan Cheng | 3ddfbd3 | 2011-07-06 22:01:53 +0000 | [diff] [blame] | 14 | #include "X86MCTargetDesc.h" |
Evan Cheng | 61faa55 | 2011-07-25 21:20:24 +0000 | [diff] [blame] | 15 | #include "InstPrinter/X86ATTInstPrinter.h" |
| 16 | #include "InstPrinter/X86IntelInstPrinter.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "X86MCAsmInfo.h" |
| 18 | #include "llvm/ADT/Triple.h" |
Evan Cheng | 4d6c9d7 | 2011-08-23 20:15:21 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCCodeGenInfo.h" |
| 20 | #include "llvm/MC/MCInstrAnalysis.h" |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCInstrInfo.h" |
Evan Cheng | 2475331 | 2011-06-24 01:44:41 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCRegisterInfo.h" |
Evan Cheng | b253100 | 2011-07-25 19:33:48 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCStreamer.h" |
Evan Cheng | 0711c4d | 2011-07-01 22:25:04 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSubtargetInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MachineLocation.h" |
Craig Topper | c4965bc | 2012-02-05 07:21:30 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Host.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 28 | #include "llvm/Support/TargetRegistry.h" |
Evan Cheng | d9997ac | 2011-06-27 18:32:37 +0000 | [diff] [blame] | 29 | |
Chandler Carruth | d174b72 | 2014-04-22 02:03:14 +0000 | [diff] [blame] | 30 | #if _MSC_VER |
| 31 | #include <intrin.h> |
| 32 | #endif |
| 33 | |
| 34 | using namespace llvm; |
| 35 | |
Evan Cheng | d9997ac | 2011-06-27 18:32:37 +0000 | [diff] [blame] | 36 | #define GET_REGINFO_MC_DESC |
| 37 | #include "X86GenRegisterInfo.inc" |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 38 | |
| 39 | #define GET_INSTRINFO_MC_DESC |
| 40 | #include "X86GenInstrInfo.inc" |
| 41 | |
Evan Cheng | 0711c4d | 2011-07-01 22:25:04 +0000 | [diff] [blame] | 42 | #define GET_SUBTARGETINFO_MC_DESC |
Evan Cheng | c9c090d | 2011-07-01 22:36:09 +0000 | [diff] [blame] | 43 | #include "X86GenSubtargetInfo.inc" |
Evan Cheng | 0711c4d | 2011-07-01 22:25:04 +0000 | [diff] [blame] | 44 | |
Evan Cheng | 13bcc6c | 2011-07-07 21:06:52 +0000 | [diff] [blame] | 45 | std::string X86_MC::ParseX86Triple(StringRef TT) { |
| 46 | Triple TheTriple(TT); |
Nick Lewycky | 73df7e3 | 2011-09-05 21:51:43 +0000 | [diff] [blame] | 47 | std::string FS; |
Evan Cheng | 13bcc6c | 2011-07-07 21:06:52 +0000 | [diff] [blame] | 48 | if (TheTriple.getArch() == Triple::x86_64) |
Craig Topper | 3c80d62 | 2014-01-06 04:55:54 +0000 | [diff] [blame] | 49 | FS = "+64bit-mode,-32bit-mode,-16bit-mode"; |
David Woodhouse | 71d15ed | 2014-01-20 12:02:25 +0000 | [diff] [blame] | 50 | else if (TheTriple.getEnvironment() != Triple::CODE16) |
Craig Topper | 3c80d62 | 2014-01-06 04:55:54 +0000 | [diff] [blame] | 51 | FS = "-64bit-mode,+32bit-mode,-16bit-mode"; |
David Woodhouse | 71d15ed | 2014-01-20 12:02:25 +0000 | [diff] [blame] | 52 | else |
| 53 | FS = "-64bit-mode,-32bit-mode,+16bit-mode"; |
| 54 | |
Nick Lewycky | 73df7e3 | 2011-09-05 21:51:43 +0000 | [diff] [blame] | 55 | return FS; |
Evan Cheng | 13bcc6c | 2011-07-07 21:06:52 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the |
| 59 | /// specified arguments. If we can't run cpuid on the host, return true. |
| 60 | bool X86_MC::GetCpuIDAndInfo(unsigned value, unsigned *rEAX, |
| 61 | unsigned *rEBX, unsigned *rECX, unsigned *rEDX) { |
| 62 | #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64) |
| 63 | #if defined(__GNUC__) |
| 64 | // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually. |
| 65 | asm ("movq\t%%rbx, %%rsi\n\t" |
| 66 | "cpuid\n\t" |
| 67 | "xchgq\t%%rbx, %%rsi\n\t" |
| 68 | : "=a" (*rEAX), |
| 69 | "=S" (*rEBX), |
| 70 | "=c" (*rECX), |
| 71 | "=d" (*rEDX) |
| 72 | : "a" (value)); |
| 73 | return false; |
| 74 | #elif defined(_MSC_VER) |
| 75 | int registers[4]; |
| 76 | __cpuid(registers, value); |
| 77 | *rEAX = registers[0]; |
| 78 | *rEBX = registers[1]; |
| 79 | *rECX = registers[2]; |
| 80 | *rEDX = registers[3]; |
| 81 | return false; |
David Blaikie | 46a9f01 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 82 | #else |
| 83 | return true; |
Evan Cheng | 13bcc6c | 2011-07-07 21:06:52 +0000 | [diff] [blame] | 84 | #endif |
| 85 | #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86) |
| 86 | #if defined(__GNUC__) |
| 87 | asm ("movl\t%%ebx, %%esi\n\t" |
| 88 | "cpuid\n\t" |
| 89 | "xchgl\t%%ebx, %%esi\n\t" |
| 90 | : "=a" (*rEAX), |
| 91 | "=S" (*rEBX), |
| 92 | "=c" (*rECX), |
| 93 | "=d" (*rEDX) |
| 94 | : "a" (value)); |
| 95 | return false; |
| 96 | #elif defined(_MSC_VER) |
| 97 | __asm { |
| 98 | mov eax,value |
| 99 | cpuid |
| 100 | mov esi,rEAX |
| 101 | mov dword ptr [esi],eax |
| 102 | mov esi,rEBX |
| 103 | mov dword ptr [esi],ebx |
| 104 | mov esi,rECX |
| 105 | mov dword ptr [esi],ecx |
| 106 | mov esi,rEDX |
| 107 | mov dword ptr [esi],edx |
| 108 | } |
| 109 | return false; |
David Blaikie | 46a9f01 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 110 | #else |
| 111 | return true; |
Evan Cheng | 13bcc6c | 2011-07-07 21:06:52 +0000 | [diff] [blame] | 112 | #endif |
David Blaikie | 46a9f01 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 113 | #else |
Evan Cheng | 13bcc6c | 2011-07-07 21:06:52 +0000 | [diff] [blame] | 114 | return true; |
David Blaikie | 46a9f01 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 115 | #endif |
Evan Cheng | 13bcc6c | 2011-07-07 21:06:52 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Craig Topper | 6c8879e | 2011-10-16 00:21:51 +0000 | [diff] [blame] | 118 | /// GetCpuIDAndInfoEx - Execute the specified cpuid with subleaf and return the |
| 119 | /// 4 values in the specified arguments. If we can't run cpuid on the host, |
| 120 | /// return true. |
| 121 | bool X86_MC::GetCpuIDAndInfoEx(unsigned value, unsigned subleaf, unsigned *rEAX, |
| 122 | unsigned *rEBX, unsigned *rECX, unsigned *rEDX) { |
| 123 | #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64) |
| 124 | #if defined(__GNUC__) |
| 125 | // gcc desn't know cpuid would clobber ebx/rbx. Preseve it manually. |
| 126 | asm ("movq\t%%rbx, %%rsi\n\t" |
| 127 | "cpuid\n\t" |
| 128 | "xchgq\t%%rbx, %%rsi\n\t" |
| 129 | : "=a" (*rEAX), |
| 130 | "=S" (*rEBX), |
| 131 | "=c" (*rECX), |
| 132 | "=d" (*rEDX) |
| 133 | : "a" (value), |
| 134 | "c" (subleaf)); |
| 135 | return false; |
| 136 | #elif defined(_MSC_VER) |
Aaron Ballman | da9501b | 2015-02-16 18:34:57 +0000 | [diff] [blame] | 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; |
David Blaikie | 46a9f01 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 144 | #else |
| 145 | return true; |
Craig Topper | 6c8879e | 2011-10-16 00:21:51 +0000 | [diff] [blame] | 146 | #endif |
| 147 | #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86) |
| 148 | #if defined(__GNUC__) |
| 149 | asm ("movl\t%%ebx, %%esi\n\t" |
| 150 | "cpuid\n\t" |
| 151 | "xchgl\t%%ebx, %%esi\n\t" |
| 152 | : "=a" (*rEAX), |
| 153 | "=S" (*rEBX), |
| 154 | "=c" (*rECX), |
| 155 | "=d" (*rEDX) |
| 156 | : "a" (value), |
| 157 | "c" (subleaf)); |
| 158 | return false; |
| 159 | #elif defined(_MSC_VER) |
| 160 | __asm { |
| 161 | mov eax,value |
| 162 | mov ecx,subleaf |
| 163 | cpuid |
| 164 | mov esi,rEAX |
| 165 | mov dword ptr [esi],eax |
| 166 | mov esi,rEBX |
| 167 | mov dword ptr [esi],ebx |
| 168 | mov esi,rECX |
| 169 | mov dword ptr [esi],ecx |
| 170 | mov esi,rEDX |
| 171 | mov dword ptr [esi],edx |
| 172 | } |
| 173 | return false; |
David Blaikie | 46a9f01 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 174 | #else |
| 175 | return true; |
Craig Topper | 6c8879e | 2011-10-16 00:21:51 +0000 | [diff] [blame] | 176 | #endif |
David Blaikie | 46a9f01 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 177 | #else |
Craig Topper | 6c8879e | 2011-10-16 00:21:51 +0000 | [diff] [blame] | 178 | return true; |
David Blaikie | 46a9f01 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 179 | #endif |
Craig Topper | 6c8879e | 2011-10-16 00:21:51 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Evan Cheng | 13bcc6c | 2011-07-07 21:06:52 +0000 | [diff] [blame] | 182 | void X86_MC::DetectFamilyModel(unsigned EAX, unsigned &Family, |
| 183 | unsigned &Model) { |
| 184 | Family = (EAX >> 8) & 0xf; // Bits 8 - 11 |
| 185 | Model = (EAX >> 4) & 0xf; // Bits 4 - 7 |
| 186 | if (Family == 6 || Family == 0xf) { |
| 187 | if (Family == 0xf) |
| 188 | // Examine extended family ID if family ID is F. |
| 189 | Family += (EAX >> 20) & 0xff; // Bits 20 - 27 |
| 190 | // Examine extended model ID if family ID is 6 or F. |
| 191 | Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19 |
| 192 | } |
| 193 | } |
| 194 | |
Eric Christopher | 1f8ad4f | 2014-06-10 22:34:28 +0000 | [diff] [blame] | 195 | unsigned X86_MC::getDwarfRegFlavour(Triple TT, bool isEH) { |
| 196 | if (TT.getArch() == Triple::x86_64) |
Evan Cheng | d60fa58b | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 197 | return DWARFFlavour::X86_64; |
| 198 | |
Eric Christopher | 1f8ad4f | 2014-06-10 22:34:28 +0000 | [diff] [blame] | 199 | if (TT.isOSDarwin()) |
Evan Cheng | d60fa58b | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 200 | return isEH ? DWARFFlavour::X86_32_DarwinEH : DWARFFlavour::X86_32_Generic; |
Eric Christopher | 1f8ad4f | 2014-06-10 22:34:28 +0000 | [diff] [blame] | 201 | if (TT.isOSCygMing()) |
Evan Cheng | d60fa58b | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 202 | // Unsupported by now, just quick fallback |
| 203 | return DWARFFlavour::X86_32_Generic; |
| 204 | return DWARFFlavour::X86_32_Generic; |
| 205 | } |
| 206 | |
Evan Cheng | d60fa58b | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 207 | void X86_MC::InitLLVM2SEHRegisterMapping(MCRegisterInfo *MRI) { |
| 208 | // FIXME: TableGen these. |
| 209 | for (unsigned Reg = X86::NoRegister+1; Reg < X86::NUM_TARGET_REGS; ++Reg) { |
Michael Liao | f54249b | 2012-10-04 19:50:43 +0000 | [diff] [blame] | 210 | unsigned SEH = MRI->getEncodingValue(Reg); |
Evan Cheng | d60fa58b | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 211 | MRI->mapLLVMRegToSEHReg(Reg, SEH); |
| 212 | } |
| 213 | } |
| 214 | |
Evan Cheng | 4d1ca96 | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 215 | MCSubtargetInfo *X86_MC::createX86MCSubtargetInfo(StringRef TT, StringRef CPU, |
| 216 | StringRef FS) { |
Evan Cheng | 13bcc6c | 2011-07-07 21:06:52 +0000 | [diff] [blame] | 217 | std::string ArchFS = X86_MC::ParseX86Triple(TT); |
| 218 | if (!FS.empty()) { |
| 219 | if (!ArchFS.empty()) |
| 220 | ArchFS = ArchFS + "," + FS.str(); |
| 221 | else |
| 222 | ArchFS = FS; |
| 223 | } |
| 224 | |
| 225 | std::string CPUName = CPU; |
Jim Grosbach | a344b6c3 | 2014-04-14 22:23:30 +0000 | [diff] [blame] | 226 | if (CPUName.empty()) |
Evan Cheng | 964cb5f | 2011-07-08 21:14:14 +0000 | [diff] [blame] | 227 | CPUName = "generic"; |
Evan Cheng | 13bcc6c | 2011-07-07 21:06:52 +0000 | [diff] [blame] | 228 | |
Evan Cheng | 0711c4d | 2011-07-01 22:25:04 +0000 | [diff] [blame] | 229 | MCSubtargetInfo *X = new MCSubtargetInfo(); |
Evan Cheng | c5e6d2f | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 230 | InitX86MCSubtargetInfo(X, TT, CPUName, ArchFS); |
Evan Cheng | 4d1ca96 | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 231 | return X; |
| 232 | } |
| 233 | |
Evan Cheng | 1705ab0 | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 234 | static MCInstrInfo *createX86MCInstrInfo() { |
Evan Cheng | 4d1ca96 | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 235 | MCInstrInfo *X = new MCInstrInfo(); |
| 236 | InitX86MCInstrInfo(X); |
| 237 | return X; |
| 238 | } |
| 239 | |
Evan Cheng | d60fa58b | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 240 | static MCRegisterInfo *createX86MCRegisterInfo(StringRef TT) { |
| 241 | Triple TheTriple(TT); |
| 242 | unsigned RA = (TheTriple.getArch() == Triple::x86_64) |
| 243 | ? X86::RIP // Should have dwarf #16. |
| 244 | : X86::EIP; // Should have dwarf #8. |
| 245 | |
Evan Cheng | 1705ab0 | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 246 | MCRegisterInfo *X = new MCRegisterInfo(); |
Evan Cheng | d60fa58b | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 247 | InitX86MCRegisterInfo(X, RA, |
Eric Christopher | 1f8ad4f | 2014-06-10 22:34:28 +0000 | [diff] [blame] | 248 | X86_MC::getDwarfRegFlavour(TheTriple, false), |
| 249 | X86_MC::getDwarfRegFlavour(TheTriple, true), |
Jim Grosbach | 6df9484 | 2012-12-19 23:38:53 +0000 | [diff] [blame] | 250 | RA); |
Evan Cheng | d60fa58b | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 251 | X86_MC::InitLLVM2SEHRegisterMapping(X); |
Evan Cheng | 1705ab0 | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 252 | return X; |
| 253 | } |
| 254 | |
Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 255 | static MCAsmInfo *createX86MCAsmInfo(const MCRegisterInfo &MRI, StringRef TT) { |
Evan Cheng | 1705ab0 | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 256 | Triple TheTriple(TT); |
Evan Cheng | 67c033e | 2011-07-18 22:29:13 +0000 | [diff] [blame] | 257 | bool is64Bit = TheTriple.getArch() == Triple::x86_64; |
Evan Cheng | 1705ab0 | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 258 | |
Evan Cheng | 67c033e | 2011-07-18 22:29:13 +0000 | [diff] [blame] | 259 | MCAsmInfo *MAI; |
Tim Northover | d6a729b | 2014-01-06 14:28:05 +0000 | [diff] [blame] | 260 | if (TheTriple.isOSBinFormatMachO()) { |
Evan Cheng | 67c033e | 2011-07-18 22:29:13 +0000 | [diff] [blame] | 261 | if (is64Bit) |
| 262 | MAI = new X86_64MCAsmInfoDarwin(TheTriple); |
Evan Cheng | 1705ab0 | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 263 | else |
Evan Cheng | 67c033e | 2011-07-18 22:29:13 +0000 | [diff] [blame] | 264 | MAI = new X86MCAsmInfoDarwin(TheTriple); |
Saleem Abdulrasool | 3547633 | 2014-03-06 20:47:11 +0000 | [diff] [blame] | 265 | } else if (TheTriple.isOSBinFormatELF()) { |
Andrew Kaylor | feb805f | 2012-10-02 18:38:34 +0000 | [diff] [blame] | 266 | // Force the use of an ELF container. |
| 267 | MAI = new X86ELFMCAsmInfo(TheTriple); |
Reid Kleckner | d970702 | 2014-11-17 22:55:59 +0000 | [diff] [blame] | 268 | } else if (TheTriple.isWindowsMSVCEnvironment()) { |
Michael J. Spencer | de3a211 | 2011-11-29 18:00:06 +0000 | [diff] [blame] | 269 | MAI = new X86MCAsmInfoMicrosoft(TheTriple); |
Saleem Abdulrasool | 862e60c | 2014-07-17 16:27:40 +0000 | [diff] [blame] | 270 | } else if (TheTriple.isOSCygMing() || |
| 271 | TheTriple.isWindowsItaniumEnvironment()) { |
Michael J. Spencer | de3a211 | 2011-11-29 18:00:06 +0000 | [diff] [blame] | 272 | MAI = new X86MCAsmInfoGNUCOFF(TheTriple); |
Evan Cheng | 67c033e | 2011-07-18 22:29:13 +0000 | [diff] [blame] | 273 | } else { |
Andrew Kaylor | feb805f | 2012-10-02 18:38:34 +0000 | [diff] [blame] | 274 | // The default is ELF. |
Evan Cheng | 67c033e | 2011-07-18 22:29:13 +0000 | [diff] [blame] | 275 | MAI = new X86ELFMCAsmInfo(TheTriple); |
Evan Cheng | 1705ab0 | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Evan Cheng | 67c033e | 2011-07-18 22:29:13 +0000 | [diff] [blame] | 278 | // Initialize initial frame state. |
| 279 | // Calculate amount of bytes used for return address storing |
| 280 | int stackGrowth = is64Bit ? -8 : -4; |
Evan Cheng | 1705ab0 | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 281 | |
Evan Cheng | 67c033e | 2011-07-18 22:29:13 +0000 | [diff] [blame] | 282 | // Initial state of the frame pointer is esp+stackGrowth. |
Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 283 | unsigned StackPtr = is64Bit ? X86::RSP : X86::ESP; |
| 284 | MCCFIInstruction Inst = MCCFIInstruction::createDefCfa( |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 285 | nullptr, MRI.getDwarfRegNum(StackPtr, true), -stackGrowth); |
Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 286 | MAI->addInitialFrameState(Inst); |
Evan Cheng | 67c033e | 2011-07-18 22:29:13 +0000 | [diff] [blame] | 287 | |
| 288 | // Add return address to move list |
Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 289 | unsigned InstPtr = is64Bit ? X86::RIP : X86::EIP; |
| 290 | MCCFIInstruction Inst2 = MCCFIInstruction::createOffset( |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 291 | nullptr, MRI.getDwarfRegNum(InstPtr, true), stackGrowth); |
Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 292 | MAI->addInitialFrameState(Inst2); |
Evan Cheng | 67c033e | 2011-07-18 22:29:13 +0000 | [diff] [blame] | 293 | |
| 294 | return MAI; |
Evan Cheng | 1705ab0 | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Evan Cheng | 6376593 | 2011-07-23 00:01:04 +0000 | [diff] [blame] | 297 | static MCCodeGenInfo *createX86MCCodeGenInfo(StringRef TT, Reloc::Model RM, |
Evan Cheng | ecb2908 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 298 | CodeModel::Model CM, |
| 299 | CodeGenOpt::Level OL) { |
Evan Cheng | 2129f59 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 300 | MCCodeGenInfo *X = new MCCodeGenInfo(); |
| 301 | |
| 302 | Triple T(TT); |
| 303 | bool is64Bit = T.getArch() == Triple::x86_64; |
| 304 | |
| 305 | if (RM == Reloc::Default) { |
| 306 | // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode. |
| 307 | // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we |
| 308 | // use static relocation model by default. |
| 309 | if (T.isOSDarwin()) { |
| 310 | if (is64Bit) |
| 311 | RM = Reloc::PIC_; |
| 312 | else |
| 313 | RM = Reloc::DynamicNoPIC; |
| 314 | } else if (T.isOSWindows() && is64Bit) |
| 315 | RM = Reloc::PIC_; |
| 316 | else |
| 317 | RM = Reloc::Static; |
| 318 | } |
| 319 | |
| 320 | // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC |
| 321 | // is defined as a model for code which may be used in static or dynamic |
| 322 | // executables but not necessarily a shared library. On X86-32 we just |
| 323 | // compile in -static mode, in x86-64 we use PIC. |
| 324 | if (RM == Reloc::DynamicNoPIC) { |
| 325 | if (is64Bit) |
| 326 | RM = Reloc::PIC_; |
| 327 | else if (!T.isOSDarwin()) |
| 328 | RM = Reloc::Static; |
| 329 | } |
| 330 | |
| 331 | // If we are on Darwin, disallow static relocation model in X86-64 mode, since |
| 332 | // the Mach-O file format doesn't support it. |
| 333 | if (RM == Reloc::Static && T.isOSDarwin() && is64Bit) |
| 334 | RM = Reloc::PIC_; |
| 335 | |
Evan Cheng | efd9b42 | 2011-07-20 07:51:56 +0000 | [diff] [blame] | 336 | // For static codegen, if we're not already set, use Small codegen. |
| 337 | if (CM == CodeModel::Default) |
| 338 | CM = CodeModel::Small; |
| 339 | else if (CM == CodeModel::JITDefault) |
| 340 | // 64-bit JIT places everything in the same buffer except external funcs. |
| 341 | CM = is64Bit ? CodeModel::Large : CodeModel::Small; |
| 342 | |
Evan Cheng | ecb2908 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 343 | X->InitMCCodeGenInfo(RM, CM, OL); |
Evan Cheng | 2129f59 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 344 | return X; |
| 345 | } |
| 346 | |
Rafael Espindola | f696df1 | 2015-03-16 22:29:29 +0000 | [diff] [blame] | 347 | static MCStreamer *createMCStreamer(const Triple &T, MCContext &Ctx, |
Rafael Espindola | 9bcf2fc | 2015-03-16 22:06:15 +0000 | [diff] [blame] | 348 | MCAsmBackend &MAB, raw_ostream &OS, |
| 349 | MCCodeEmitter *Emitter, |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 350 | const MCSubtargetInfo &STI, bool RelaxAll) { |
Rafael Espindola | f696df1 | 2015-03-16 22:29:29 +0000 | [diff] [blame] | 351 | switch (T.getObjectFormat()) { |
Saleem Abdulrasool | d4cae62 | 2014-04-25 06:29:36 +0000 | [diff] [blame] | 352 | default: llvm_unreachable("unsupported object format"); |
| 353 | case Triple::MachO: |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 354 | return createMachOStreamer(Ctx, MAB, OS, Emitter, RelaxAll); |
Saleem Abdulrasool | d4cae62 | 2014-04-25 06:29:36 +0000 | [diff] [blame] | 355 | case Triple::COFF: |
Rafael Espindola | f696df1 | 2015-03-16 22:29:29 +0000 | [diff] [blame] | 356 | assert(T.isOSWindows() && "only Windows COFF is supported"); |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 357 | return createX86WinCOFFStreamer(Ctx, MAB, Emitter, OS, RelaxAll); |
Saleem Abdulrasool | d4cae62 | 2014-04-25 06:29:36 +0000 | [diff] [blame] | 358 | case Triple::ELF: |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 359 | return createELFStreamer(Ctx, MAB, OS, Emitter, RelaxAll); |
Saleem Abdulrasool | d4cae62 | 2014-04-25 06:29:36 +0000 | [diff] [blame] | 360 | } |
Evan Cheng | b253100 | 2011-07-25 19:33:48 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Evan Cheng | 61faa55 | 2011-07-25 21:20:24 +0000 | [diff] [blame] | 363 | static MCInstPrinter *createX86MCInstPrinter(const Target &T, |
| 364 | unsigned SyntaxVariant, |
James Molloy | 4c493e8 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 365 | const MCAsmInfo &MAI, |
Craig Topper | 54bfde7 | 2012-04-02 06:09:36 +0000 | [diff] [blame] | 366 | const MCInstrInfo &MII, |
Jim Grosbach | fd93a59 | 2012-03-05 19:33:20 +0000 | [diff] [blame] | 367 | const MCRegisterInfo &MRI, |
James Molloy | 4c493e8 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 368 | const MCSubtargetInfo &STI) { |
Evan Cheng | 61faa55 | 2011-07-25 21:20:24 +0000 | [diff] [blame] | 369 | if (SyntaxVariant == 0) |
Pavel Chupin | e6617fc | 2014-09-09 11:54:12 +0000 | [diff] [blame] | 370 | return new X86ATTInstPrinter(MAI, MII, MRI, STI); |
Evan Cheng | 61faa55 | 2011-07-25 21:20:24 +0000 | [diff] [blame] | 371 | if (SyntaxVariant == 1) |
Craig Topper | 54bfde7 | 2012-04-02 06:09:36 +0000 | [diff] [blame] | 372 | return new X86IntelInstPrinter(MAI, MII, MRI); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 373 | return nullptr; |
Evan Cheng | 61faa55 | 2011-07-25 21:20:24 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Quentin Colombet | f482805 | 2013-05-24 22:51:52 +0000 | [diff] [blame] | 376 | static MCRelocationInfo *createX86MCRelocationInfo(StringRef TT, |
| 377 | MCContext &Ctx) { |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 378 | Triple TheTriple(TT); |
Tim Northover | 9653eb5 | 2013-12-10 16:57:43 +0000 | [diff] [blame] | 379 | if (TheTriple.isOSBinFormatMachO() && TheTriple.getArch() == Triple::x86_64) |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 380 | return createX86_64MachORelocationInfo(Ctx); |
| 381 | else if (TheTriple.isOSBinFormatELF()) |
| 382 | return createX86_64ELFRelocationInfo(Ctx); |
| 383 | // Default to the stock relocation info. |
Quentin Colombet | f482805 | 2013-05-24 22:51:52 +0000 | [diff] [blame] | 384 | return llvm::createMCRelocationInfo(TT, Ctx); |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Evan Cheng | 4d6c9d7 | 2011-08-23 20:15:21 +0000 | [diff] [blame] | 387 | static MCInstrAnalysis *createX86MCInstrAnalysis(const MCInstrInfo *Info) { |
| 388 | return new MCInstrAnalysis(Info); |
| 389 | } |
| 390 | |
Evan Cheng | 8c886a4 | 2011-07-22 21:58:54 +0000 | [diff] [blame] | 391 | // Force static initialization. |
| 392 | extern "C" void LLVMInitializeX86TargetMC() { |
Rafael Espindola | 69244c3 | 2015-03-18 23:15:49 +0000 | [diff] [blame^] | 393 | for (Target *T : {&TheX86_32Target, &TheX86_64Target}) { |
| 394 | // Register the MC asm info. |
| 395 | RegisterMCAsmInfoFn X(*T, createX86MCAsmInfo); |
Evan Cheng | 8c886a4 | 2011-07-22 21:58:54 +0000 | [diff] [blame] | 396 | |
Rafael Espindola | 69244c3 | 2015-03-18 23:15:49 +0000 | [diff] [blame^] | 397 | // Register the MC codegen info. |
| 398 | RegisterMCCodeGenInfoFn Y(*T, createX86MCCodeGenInfo); |
Evan Cheng | 8c886a4 | 2011-07-22 21:58:54 +0000 | [diff] [blame] | 399 | |
Rafael Espindola | 69244c3 | 2015-03-18 23:15:49 +0000 | [diff] [blame^] | 400 | // Register the MC instruction info. |
| 401 | TargetRegistry::RegisterMCInstrInfo(*T, createX86MCInstrInfo); |
Evan Cheng | 8c886a4 | 2011-07-22 21:58:54 +0000 | [diff] [blame] | 402 | |
Rafael Espindola | 69244c3 | 2015-03-18 23:15:49 +0000 | [diff] [blame^] | 403 | // Register the MC register info. |
| 404 | TargetRegistry::RegisterMCRegInfo(*T, createX86MCRegisterInfo); |
Evan Cheng | 8c886a4 | 2011-07-22 21:58:54 +0000 | [diff] [blame] | 405 | |
Rafael Espindola | 69244c3 | 2015-03-18 23:15:49 +0000 | [diff] [blame^] | 406 | // Register the MC subtarget info. |
| 407 | TargetRegistry::RegisterMCSubtargetInfo(*T, |
| 408 | X86_MC::createX86MCSubtargetInfo); |
Evan Cheng | b253100 | 2011-07-25 19:33:48 +0000 | [diff] [blame] | 409 | |
Rafael Espindola | 69244c3 | 2015-03-18 23:15:49 +0000 | [diff] [blame^] | 410 | // Register the MC instruction analyzer. |
| 411 | TargetRegistry::RegisterMCInstrAnalysis(*T, createX86MCInstrAnalysis); |
Evan Cheng | 4d6c9d7 | 2011-08-23 20:15:21 +0000 | [diff] [blame] | 412 | |
Rafael Espindola | 69244c3 | 2015-03-18 23:15:49 +0000 | [diff] [blame^] | 413 | // Register the code emitter. |
| 414 | TargetRegistry::RegisterMCCodeEmitter(*T, createX86MCCodeEmitter); |
| 415 | |
| 416 | // Register the object streamer. |
| 417 | TargetRegistry::RegisterMCObjectStreamer(*T, createMCStreamer); |
| 418 | |
| 419 | // Register the MCInstPrinter. |
| 420 | TargetRegistry::RegisterMCInstPrinter(*T, createX86MCInstPrinter); |
| 421 | |
| 422 | // Register the MC relocation info. |
| 423 | TargetRegistry::RegisterMCRelocationInfo(*T, createX86MCRelocationInfo); |
| 424 | } |
Evan Cheng | b253100 | 2011-07-25 19:33:48 +0000 | [diff] [blame] | 425 | |
| 426 | // Register the asm backend. |
Evan Cheng | 5928e69 | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 427 | TargetRegistry::RegisterMCAsmBackend(TheX86_32Target, |
| 428 | createX86_32AsmBackend); |
| 429 | TargetRegistry::RegisterMCAsmBackend(TheX86_64Target, |
| 430 | createX86_64AsmBackend); |
Evan Cheng | 2129f59 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 431 | } |