1 //===-- X86AsmBackend.cpp - X86 Assembler Backend -------------------------===//
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 #include "llvm/Target/TargetAsmBackend.h"
12 #include "X86FixupKinds.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCFixupKindInfo.h"
17 #include "llvm/MC/MCMachObjectWriter.h"
18 #include "llvm/MC/MCObjectFormat.h"
19 #include "llvm/MC/MCObjectWriter.h"
20 #include "llvm/MC/MCSectionCOFF.h"
21 #include "llvm/MC/MCSectionELF.h"
22 #include "llvm/MC/MCSectionMachO.h"
23 #include "llvm/Object/MachOFormat.h"
24 #include "llvm/Support/ELF.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Target/TargetRegistry.h"
28 #include "llvm/Target/TargetAsmBackend.h"
31 static unsigned getFixupKindLog2Size(unsigned Kind) {
33 default: assert(0 && "invalid fixup kind!");
35 case FK_Data_1: return 0;
37 case FK_Data_2: return 1;
39 case X86::reloc_riprel_4byte:
40 case X86::reloc_riprel_4byte_movq_load:
41 case X86::reloc_signed_4byte:
42 case X86::reloc_global_offset_table:
43 case FK_Data_4: return 2;
44 case FK_Data_8: return 3;
49 class X86MachObjectWriter : public MCMachObjectTargetWriter {
52 class X86AsmBackend : public TargetAsmBackend {
54 X86AsmBackend(const Target &T)
55 : TargetAsmBackend() {}
57 unsigned getNumFixupKinds() const {
58 return X86::NumTargetFixupKinds;
61 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
62 const static MCFixupKindInfo Infos[X86::NumTargetFixupKinds] = {
63 { "reloc_riprel_4byte", 0, 4 * 8, MCFixupKindInfo::FKF_IsPCRel },
64 { "reloc_riprel_4byte_movq_load", 0, 4 * 8, MCFixupKindInfo::FKF_IsPCRel},
65 { "reloc_signed_4byte", 0, 4 * 8, 0},
66 { "reloc_global_offset_table", 0, 4 * 8, 0}
69 if (Kind < FirstTargetFixupKind)
70 return TargetAsmBackend::getFixupKindInfo(Kind);
72 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
74 return Infos[Kind - FirstTargetFixupKind];
77 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
78 uint64_t Value) const {
79 unsigned Size = 1 << getFixupKindLog2Size(Fixup.getKind());
81 assert(Fixup.getOffset() + Size <= DataSize &&
82 "Invalid fixup offset!");
83 for (unsigned i = 0; i != Size; ++i)
84 Data[Fixup.getOffset() + i] = uint8_t(Value >> (i * 8));
87 bool MayNeedRelaxation(const MCInst &Inst) const;
89 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
91 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
93 } // end anonymous namespace
95 static unsigned getRelaxedOpcodeBranch(unsigned Op) {
100 case X86::JAE_1: return X86::JAE_4;
101 case X86::JA_1: return X86::JA_4;
102 case X86::JBE_1: return X86::JBE_4;
103 case X86::JB_1: return X86::JB_4;
104 case X86::JE_1: return X86::JE_4;
105 case X86::JGE_1: return X86::JGE_4;
106 case X86::JG_1: return X86::JG_4;
107 case X86::JLE_1: return X86::JLE_4;
108 case X86::JL_1: return X86::JL_4;
109 case X86::JMP_1: return X86::JMP_4;
110 case X86::JNE_1: return X86::JNE_4;
111 case X86::JNO_1: return X86::JNO_4;
112 case X86::JNP_1: return X86::JNP_4;
113 case X86::JNS_1: return X86::JNS_4;
114 case X86::JO_1: return X86::JO_4;
115 case X86::JP_1: return X86::JP_4;
116 case X86::JS_1: return X86::JS_4;
120 static unsigned getRelaxedOpcodeArith(unsigned Op) {
126 case X86::IMUL16rri8: return X86::IMUL16rri;
127 case X86::IMUL16rmi8: return X86::IMUL16rmi;
128 case X86::IMUL32rri8: return X86::IMUL32rri;
129 case X86::IMUL32rmi8: return X86::IMUL32rmi;
130 case X86::IMUL64rri8: return X86::IMUL64rri32;
131 case X86::IMUL64rmi8: return X86::IMUL64rmi32;
134 case X86::AND16ri8: return X86::AND16ri;
135 case X86::AND16mi8: return X86::AND16mi;
136 case X86::AND32ri8: return X86::AND32ri;
137 case X86::AND32mi8: return X86::AND32mi;
138 case X86::AND64ri8: return X86::AND64ri32;
139 case X86::AND64mi8: return X86::AND64mi32;
142 case X86::OR16ri8: return X86::OR16ri;
143 case X86::OR16mi8: return X86::OR16mi;
144 case X86::OR32ri8: return X86::OR32ri;
145 case X86::OR32mi8: return X86::OR32mi;
146 case X86::OR64ri8: return X86::OR64ri32;
147 case X86::OR64mi8: return X86::OR64mi32;
150 case X86::XOR16ri8: return X86::XOR16ri;
151 case X86::XOR16mi8: return X86::XOR16mi;
152 case X86::XOR32ri8: return X86::XOR32ri;
153 case X86::XOR32mi8: return X86::XOR32mi;
154 case X86::XOR64ri8: return X86::XOR64ri32;
155 case X86::XOR64mi8: return X86::XOR64mi32;
158 case X86::ADD16ri8: return X86::ADD16ri;
159 case X86::ADD16mi8: return X86::ADD16mi;
160 case X86::ADD32ri8: return X86::ADD32ri;
161 case X86::ADD32mi8: return X86::ADD32mi;
162 case X86::ADD64ri8: return X86::ADD64ri32;
163 case X86::ADD64mi8: return X86::ADD64mi32;
166 case X86::SUB16ri8: return X86::SUB16ri;
167 case X86::SUB16mi8: return X86::SUB16mi;
168 case X86::SUB32ri8: return X86::SUB32ri;
169 case X86::SUB32mi8: return X86::SUB32mi;
170 case X86::SUB64ri8: return X86::SUB64ri32;
171 case X86::SUB64mi8: return X86::SUB64mi32;
174 case X86::CMP16ri8: return X86::CMP16ri;
175 case X86::CMP16mi8: return X86::CMP16mi;
176 case X86::CMP32ri8: return X86::CMP32ri;
177 case X86::CMP32mi8: return X86::CMP32mi;
178 case X86::CMP64ri8: return X86::CMP64ri32;
179 case X86::CMP64mi8: return X86::CMP64mi32;
183 static unsigned getRelaxedOpcode(unsigned Op) {
184 unsigned R = getRelaxedOpcodeArith(Op);
187 return getRelaxedOpcodeBranch(Op);
190 bool X86AsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
191 // Branches can always be relaxed.
192 if (getRelaxedOpcodeBranch(Inst.getOpcode()) != Inst.getOpcode())
195 // Check if this instruction is ever relaxable.
196 if (getRelaxedOpcodeArith(Inst.getOpcode()) == Inst.getOpcode())
200 // Check if it has an expression and is not RIP relative.
203 for (unsigned i = 0; i < Inst.getNumOperands(); ++i) {
204 const MCOperand &Op = Inst.getOperand(i);
208 if (Op.isReg() && Op.getReg() == X86::RIP)
212 // FIXME: Why exactly do we need the !hasRIP? Is it just a limitation on
213 // how we do relaxations?
214 return hasExp && !hasRIP;
217 // FIXME: Can tblgen help at all here to verify there aren't other instructions
219 void X86AsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
220 // The only relaxations X86 does is from a 1byte pcrel to a 4byte pcrel.
221 unsigned RelaxedOp = getRelaxedOpcode(Inst.getOpcode());
223 if (RelaxedOp == Inst.getOpcode()) {
224 SmallString<256> Tmp;
225 raw_svector_ostream OS(Tmp);
226 Inst.dump_pretty(OS);
228 report_fatal_error("unexpected instruction to relax: " + OS.str());
232 Res.setOpcode(RelaxedOp);
235 /// WriteNopData - Write optimal nops to the output file for the \arg Count
236 /// bytes. This returns the number of bytes written. It may return 0 if
237 /// the \arg Count is more than the maximum optimal nops.
238 bool X86AsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
239 static const uint8_t Nops[10][10] = {
247 {0x0f, 0x1f, 0x40, 0x00},
248 // nopl 0(%[re]ax,%[re]ax,1)
249 {0x0f, 0x1f, 0x44, 0x00, 0x00},
250 // nopw 0(%[re]ax,%[re]ax,1)
251 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
253 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
254 // nopl 0L(%[re]ax,%[re]ax,1)
255 {0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
256 // nopw 0L(%[re]ax,%[re]ax,1)
257 {0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
258 // nopw %cs:0L(%[re]ax,%[re]ax,1)
259 {0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
262 // Write an optimal sequence for the first 15 bytes.
263 const uint64_t OptimalCount = (Count < 16) ? Count : 15;
264 const uint64_t Prefixes = OptimalCount <= 10 ? 0 : OptimalCount - 10;
265 for (uint64_t i = 0, e = Prefixes; i != e; i++)
267 const uint64_t Rest = OptimalCount - Prefixes;
268 for (uint64_t i = 0, e = Rest; i != e; i++)
269 OW->Write8(Nops[Rest - 1][i]);
271 // Finish with single byte nops.
272 for (uint64_t i = OptimalCount, e = Count; i != e; ++i)
281 class ELFX86AsmBackend : public X86AsmBackend {
282 MCELFObjectFormat Format;
285 Triple::OSType OSType;
286 ELFX86AsmBackend(const Target &T, Triple::OSType _OSType)
287 : X86AsmBackend(T), OSType(_OSType) {
288 HasScatteredSymbols = true;
289 HasReliableSymbolDifference = true;
292 virtual const MCObjectFormat &getObjectFormat() const {
296 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
297 const MCSectionELF &ES = static_cast<const MCSectionELF&>(Section);
298 return ES.getFlags() & MCSectionELF::SHF_MERGE;
302 class ELFX86_32AsmBackend : public ELFX86AsmBackend {
304 ELFX86_32AsmBackend(const Target &T, Triple::OSType OSType)
305 : ELFX86AsmBackend(T, OSType) {}
307 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
308 return createELFObjectWriter(OS, /*Is64Bit=*/false,
310 /*IsLittleEndian=*/true,
311 /*HasRelocationAddend=*/false);
315 class ELFX86_64AsmBackend : public ELFX86AsmBackend {
317 ELFX86_64AsmBackend(const Target &T, Triple::OSType OSType)
318 : ELFX86AsmBackend(T, OSType) {}
320 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
321 return createELFObjectWriter(OS, /*Is64Bit=*/true,
322 OSType, ELF::EM_X86_64,
323 /*IsLittleEndian=*/true,
324 /*HasRelocationAddend=*/true);
328 class WindowsX86AsmBackend : public X86AsmBackend {
330 MCCOFFObjectFormat Format;
333 WindowsX86AsmBackend(const Target &T, bool is64Bit)
336 HasScatteredSymbols = true;
339 virtual const MCObjectFormat &getObjectFormat() const {
343 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
344 return createWinCOFFObjectWriter(OS, Is64Bit);
348 class DarwinX86AsmBackend : public X86AsmBackend {
349 MCMachOObjectFormat Format;
352 DarwinX86AsmBackend(const Target &T)
354 HasScatteredSymbols = true;
357 virtual const MCObjectFormat &getObjectFormat() const {
362 class DarwinX86_32AsmBackend : public DarwinX86AsmBackend {
364 DarwinX86_32AsmBackend(const Target &T)
365 : DarwinX86AsmBackend(T) {}
367 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
368 return createMachObjectWriter(new X86MachObjectWriter,
369 OS, /*Is64Bit=*/false,
370 object::mach::CTM_i386,
371 object::mach::CSX86_ALL,
372 /*IsLittleEndian=*/true);
376 class DarwinX86_64AsmBackend : public DarwinX86AsmBackend {
378 DarwinX86_64AsmBackend(const Target &T)
379 : DarwinX86AsmBackend(T) {
380 HasReliableSymbolDifference = true;
383 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
384 return createMachObjectWriter(new X86MachObjectWriter,
385 OS, /*Is64Bit=*/true,
386 object::mach::CTM_x86_64,
387 object::mach::CSX86_ALL,
388 /*IsLittleEndian=*/true);
391 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
392 // Temporary labels in the string literals sections require symbols. The
393 // issue is that the x86_64 relocation format does not allow symbol +
394 // offset, and so the linker does not have enough information to resolve the
395 // access to the appropriate atom unless an external relocation is used. For
396 // non-cstring sections, we expect the compiler to use a non-temporary label
397 // for anything that could have an addend pointing outside the symbol.
399 // See <rdar://problem/4765733>.
400 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
401 return SMO.getType() == MCSectionMachO::S_CSTRING_LITERALS;
404 virtual bool isSectionAtomizable(const MCSection &Section) const {
405 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
406 // Fixed sized data sections are uniqued, they cannot be diced into atoms.
407 switch (SMO.getType()) {
411 case MCSectionMachO::S_4BYTE_LITERALS:
412 case MCSectionMachO::S_8BYTE_LITERALS:
413 case MCSectionMachO::S_16BYTE_LITERALS:
414 case MCSectionMachO::S_LITERAL_POINTERS:
415 case MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS:
416 case MCSectionMachO::S_LAZY_SYMBOL_POINTERS:
417 case MCSectionMachO::S_MOD_INIT_FUNC_POINTERS:
418 case MCSectionMachO::S_MOD_TERM_FUNC_POINTERS:
419 case MCSectionMachO::S_INTERPOSING:
425 } // end anonymous namespace
427 TargetAsmBackend *llvm::createX86_32AsmBackend(const Target &T,
428 const std::string &TT) {
429 switch (Triple(TT).getOS()) {
431 return new DarwinX86_32AsmBackend(T);
432 case Triple::MinGW32:
435 return new WindowsX86AsmBackend(T, false);
437 return new ELFX86_32AsmBackend(T, Triple(TT).getOS());
441 TargetAsmBackend *llvm::createX86_64AsmBackend(const Target &T,
442 const std::string &TT) {
443 switch (Triple(TT).getOS()) {
445 return new DarwinX86_64AsmBackend(T);
446 case Triple::MinGW64:
449 return new WindowsX86AsmBackend(T, true);
451 return new ELFX86_64AsmBackend(T, Triple(TT).getOS());