blob: 42643a395a3a7563191b3e544dd0b2c45f52fc65 [file] [log] [blame]
Daniel Dunbar12783d12010-02-21 21:54:14 +00001//===-- X86AsmBackend.cpp - X86 Assembler Backend -------------------------===//
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#include "llvm/Target/TargetAsmBackend.h"
11#include "X86.h"
Daniel Dunbar87190c42010-03-19 09:28:12 +000012#include "X86FixupKinds.h"
Daniel Dunbar82968002010-03-23 01:39:09 +000013#include "llvm/ADT/Twine.h"
Matt Fleming453db502010-08-16 18:36:14 +000014#include "llvm/MC/ELFObjectWriter.h"
Daniel Dunbar87190c42010-03-19 09:28:12 +000015#include "llvm/MC/MCAssembler.h"
Daniel Dunbara5d0b542010-05-06 20:34:01 +000016#include "llvm/MC/MCExpr.h"
Rafael Espindolaf230df92010-10-16 18:23:53 +000017#include "llvm/MC/MCObjectFormat.h"
Daniel Dunbar337055e2010-03-23 03:13:05 +000018#include "llvm/MC/MCObjectWriter.h"
Michael J. Spencerdfd30182010-07-27 06:46:15 +000019#include "llvm/MC/MCSectionCOFF.h"
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +000020#include "llvm/MC/MCSectionELF.h"
Daniel Dunbard6e59082010-03-15 21:56:50 +000021#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbar1a9158c2010-03-19 10:43:26 +000022#include "llvm/MC/MachObjectWriter.h"
Wesley Peckeecb8582010-10-22 15:52:49 +000023#include "llvm/Support/ELF.h"
Jim Grosbachc9d14392010-11-05 18:48:58 +000024#include "llvm/Support/MachO.h"
Daniel Dunbar82968002010-03-23 01:39:09 +000025#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/raw_ostream.h"
Daniel Dunbar12783d12010-02-21 21:54:14 +000027#include "llvm/Target/TargetRegistry.h"
28#include "llvm/Target/TargetAsmBackend.h"
29using namespace llvm;
30
Daniel Dunbar12783d12010-02-21 21:54:14 +000031
Daniel Dunbar87190c42010-03-19 09:28:12 +000032static unsigned getFixupKindLog2Size(unsigned Kind) {
33 switch (Kind) {
34 default: assert(0 && "invalid fixup kind!");
35 case X86::reloc_pcrel_1byte:
36 case FK_Data_1: return 0;
Chris Lattner9fc05222010-07-07 22:27:31 +000037 case X86::reloc_pcrel_2byte:
Daniel Dunbar87190c42010-03-19 09:28:12 +000038 case FK_Data_2: return 1;
39 case X86::reloc_pcrel_4byte:
40 case X86::reloc_riprel_4byte:
41 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +000042 case X86::reloc_signed_4byte:
Rafael Espindola24ba4f72010-10-24 17:35:42 +000043 case X86::reloc_global_offset_table:
Daniel Dunbar87190c42010-03-19 09:28:12 +000044 case FK_Data_4: return 2;
45 case FK_Data_8: return 3;
46 }
47}
48
Chris Lattner9fc05222010-07-07 22:27:31 +000049namespace {
Daniel Dunbar12783d12010-02-21 21:54:14 +000050class X86AsmBackend : public TargetAsmBackend {
51public:
Daniel Dunbar6c27f5e2010-03-11 01:34:16 +000052 X86AsmBackend(const Target &T)
Daniel Dunbar12783d12010-02-21 21:54:14 +000053 : TargetAsmBackend(T) {}
Daniel Dunbar87190c42010-03-19 09:28:12 +000054
Daniel Dunbarc90e30a2010-05-26 15:18:56 +000055 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
Daniel Dunbar87190c42010-03-19 09:28:12 +000056 uint64_t Value) const {
Daniel Dunbar482ad802010-05-26 15:18:31 +000057 unsigned Size = 1 << getFixupKindLog2Size(Fixup.getKind());
Daniel Dunbar87190c42010-03-19 09:28:12 +000058
Daniel Dunbar482ad802010-05-26 15:18:31 +000059 assert(Fixup.getOffset() + Size <= DF.getContents().size() &&
Daniel Dunbar87190c42010-03-19 09:28:12 +000060 "Invalid fixup offset!");
61 for (unsigned i = 0; i != Size; ++i)
Daniel Dunbar482ad802010-05-26 15:18:31 +000062 DF.getContents()[Fixup.getOffset() + i] = uint8_t(Value >> (i * 8));
Daniel Dunbar87190c42010-03-19 09:28:12 +000063 }
Daniel Dunbar82968002010-03-23 01:39:09 +000064
Daniel Dunbar84882522010-05-26 17:45:29 +000065 bool MayNeedRelaxation(const MCInst &Inst) const;
Daniel Dunbar337055e2010-03-23 03:13:05 +000066
Daniel Dunbar95506d42010-05-26 18:15:06 +000067 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +000068
69 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Daniel Dunbar12783d12010-02-21 21:54:14 +000070};
Michael J. Spencerec38de22010-10-10 22:04:20 +000071} // end anonymous namespace
Daniel Dunbar12783d12010-02-21 21:54:14 +000072
Rafael Espindolae4f506f2010-10-26 14:09:12 +000073static unsigned getRelaxedOpcodeBranch(unsigned Op) {
Daniel Dunbar82968002010-03-23 01:39:09 +000074 switch (Op) {
75 default:
76 return Op;
77
78 case X86::JAE_1: return X86::JAE_4;
79 case X86::JA_1: return X86::JA_4;
80 case X86::JBE_1: return X86::JBE_4;
81 case X86::JB_1: return X86::JB_4;
82 case X86::JE_1: return X86::JE_4;
83 case X86::JGE_1: return X86::JGE_4;
84 case X86::JG_1: return X86::JG_4;
85 case X86::JLE_1: return X86::JLE_4;
86 case X86::JL_1: return X86::JL_4;
87 case X86::JMP_1: return X86::JMP_4;
88 case X86::JNE_1: return X86::JNE_4;
89 case X86::JNO_1: return X86::JNO_4;
90 case X86::JNP_1: return X86::JNP_4;
91 case X86::JNS_1: return X86::JNS_4;
92 case X86::JO_1: return X86::JO_4;
93 case X86::JP_1: return X86::JP_4;
94 case X86::JS_1: return X86::JS_4;
95 }
96}
97
Rafael Espindolae4f506f2010-10-26 14:09:12 +000098static unsigned getRelaxedOpcodeArith(unsigned Op) {
99 switch (Op) {
100 default:
101 return Op;
102
103 // IMUL
104 case X86::IMUL16rri8: return X86::IMUL16rri;
105 case X86::IMUL16rmi8: return X86::IMUL16rmi;
106 case X86::IMUL32rri8: return X86::IMUL32rri;
107 case X86::IMUL32rmi8: return X86::IMUL32rmi;
108 case X86::IMUL64rri8: return X86::IMUL64rri32;
109 case X86::IMUL64rmi8: return X86::IMUL64rmi32;
110
111 // AND
112 case X86::AND16ri8: return X86::AND16ri;
113 case X86::AND16mi8: return X86::AND16mi;
114 case X86::AND32ri8: return X86::AND32ri;
115 case X86::AND32mi8: return X86::AND32mi;
116 case X86::AND64ri8: return X86::AND64ri32;
117 case X86::AND64mi8: return X86::AND64mi32;
118
119 // OR
120 case X86::OR16ri8: return X86::OR16ri;
121 case X86::OR16mi8: return X86::OR16mi;
122 case X86::OR32ri8: return X86::OR32ri;
123 case X86::OR32mi8: return X86::OR32mi;
124 case X86::OR64ri8: return X86::OR64ri32;
125 case X86::OR64mi8: return X86::OR64mi32;
126
127 // XOR
128 case X86::XOR16ri8: return X86::XOR16ri;
129 case X86::XOR16mi8: return X86::XOR16mi;
130 case X86::XOR32ri8: return X86::XOR32ri;
131 case X86::XOR32mi8: return X86::XOR32mi;
132 case X86::XOR64ri8: return X86::XOR64ri32;
133 case X86::XOR64mi8: return X86::XOR64mi32;
134
135 // ADD
136 case X86::ADD16ri8: return X86::ADD16ri;
137 case X86::ADD16mi8: return X86::ADD16mi;
138 case X86::ADD32ri8: return X86::ADD32ri;
139 case X86::ADD32mi8: return X86::ADD32mi;
140 case X86::ADD64ri8: return X86::ADD64ri32;
141 case X86::ADD64mi8: return X86::ADD64mi32;
142
143 // SUB
144 case X86::SUB16ri8: return X86::SUB16ri;
145 case X86::SUB16mi8: return X86::SUB16mi;
146 case X86::SUB32ri8: return X86::SUB32ri;
147 case X86::SUB32mi8: return X86::SUB32mi;
148 case X86::SUB64ri8: return X86::SUB64ri32;
149 case X86::SUB64mi8: return X86::SUB64mi32;
150
151 // CMP
152 case X86::CMP16ri8: return X86::CMP16ri;
153 case X86::CMP16mi8: return X86::CMP16mi;
154 case X86::CMP32ri8: return X86::CMP32ri;
155 case X86::CMP32mi8: return X86::CMP32mi;
156 case X86::CMP64ri8: return X86::CMP64ri32;
157 case X86::CMP64mi8: return X86::CMP64mi32;
158 }
159}
160
161static unsigned getRelaxedOpcode(unsigned Op) {
162 unsigned R = getRelaxedOpcodeArith(Op);
163 if (R != Op)
164 return R;
165 return getRelaxedOpcodeBranch(Op);
166}
167
Daniel Dunbar84882522010-05-26 17:45:29 +0000168bool X86AsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
Rafael Espindolae4f506f2010-10-26 14:09:12 +0000169 // Branches can always be relaxed.
170 if (getRelaxedOpcodeBranch(Inst.getOpcode()) != Inst.getOpcode())
171 return true;
172
Daniel Dunbar84882522010-05-26 17:45:29 +0000173 // Check if this instruction is ever relaxable.
Rafael Espindolae4f506f2010-10-26 14:09:12 +0000174 if (getRelaxedOpcodeArith(Inst.getOpcode()) == Inst.getOpcode())
Daniel Dunbar84882522010-05-26 17:45:29 +0000175 return false;
Daniel Dunbar482ad802010-05-26 15:18:31 +0000176
Rafael Espindolae4f506f2010-10-26 14:09:12 +0000177
178 // Check if it has an expression and is not RIP relative.
179 bool hasExp = false;
180 bool hasRIP = false;
181 for (unsigned i = 0; i < Inst.getNumOperands(); ++i) {
182 const MCOperand &Op = Inst.getOperand(i);
183 if (Op.isExpr())
184 hasExp = true;
185
186 if (Op.isReg() && Op.getReg() == X86::RIP)
187 hasRIP = true;
188 }
189
190 // FIXME: Why exactly do we need the !hasRIP? Is it just a limitation on
191 // how we do relaxations?
192 return hasExp && !hasRIP;
Daniel Dunbar337055e2010-03-23 03:13:05 +0000193}
194
Daniel Dunbar82968002010-03-23 01:39:09 +0000195// FIXME: Can tblgen help at all here to verify there aren't other instructions
196// we can relax?
Daniel Dunbar95506d42010-05-26 18:15:06 +0000197void X86AsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
Daniel Dunbar82968002010-03-23 01:39:09 +0000198 // The only relaxations X86 does is from a 1byte pcrel to a 4byte pcrel.
Daniel Dunbar95506d42010-05-26 18:15:06 +0000199 unsigned RelaxedOp = getRelaxedOpcode(Inst.getOpcode());
Daniel Dunbar82968002010-03-23 01:39:09 +0000200
Daniel Dunbar95506d42010-05-26 18:15:06 +0000201 if (RelaxedOp == Inst.getOpcode()) {
Daniel Dunbar82968002010-03-23 01:39:09 +0000202 SmallString<256> Tmp;
203 raw_svector_ostream OS(Tmp);
Daniel Dunbar95506d42010-05-26 18:15:06 +0000204 Inst.dump_pretty(OS);
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000205 OS << "\n";
Chris Lattner75361b62010-04-07 22:58:41 +0000206 report_fatal_error("unexpected instruction to relax: " + OS.str());
Daniel Dunbar82968002010-03-23 01:39:09 +0000207 }
208
Daniel Dunbar95506d42010-05-26 18:15:06 +0000209 Res = Inst;
Daniel Dunbar82968002010-03-23 01:39:09 +0000210 Res.setOpcode(RelaxedOp);
211}
212
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000213/// WriteNopData - Write optimal nops to the output file for the \arg Count
214/// bytes. This returns the number of bytes written. It may return 0 if
215/// the \arg Count is more than the maximum optimal nops.
216///
217/// FIXME this is X86 32-bit specific and should move to a better place.
218bool X86AsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
219 static const uint8_t Nops[16][16] = {
220 // nop
221 {0x90},
222 // xchg %ax,%ax
223 {0x66, 0x90},
224 // nopl (%[re]ax)
225 {0x0f, 0x1f, 0x00},
226 // nopl 0(%[re]ax)
227 {0x0f, 0x1f, 0x40, 0x00},
228 // nopl 0(%[re]ax,%[re]ax,1)
229 {0x0f, 0x1f, 0x44, 0x00, 0x00},
230 // nopw 0(%[re]ax,%[re]ax,1)
231 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
232 // nopl 0L(%[re]ax)
233 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
234 // nopl 0L(%[re]ax,%[re]ax,1)
235 {0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
236 // nopw 0L(%[re]ax,%[re]ax,1)
237 {0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
238 // nopw %cs:0L(%[re]ax,%[re]ax,1)
239 {0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
240 // nopl 0(%[re]ax,%[re]ax,1)
241 // nopw 0(%[re]ax,%[re]ax,1)
242 {0x0f, 0x1f, 0x44, 0x00, 0x00,
243 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
244 // nopw 0(%[re]ax,%[re]ax,1)
245 // nopw 0(%[re]ax,%[re]ax,1)
246 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,
247 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
248 // nopw 0(%[re]ax,%[re]ax,1)
249 // nopl 0L(%[re]ax) */
250 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,
251 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
252 // nopl 0L(%[re]ax)
253 // nopl 0L(%[re]ax)
254 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00,
255 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
256 // nopl 0L(%[re]ax)
257 // nopl 0L(%[re]ax,%[re]ax,1)
258 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00,
259 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}
260 };
261
262 // Write an optimal sequence for the first 15 bytes.
263 uint64_t OptimalCount = (Count < 16) ? Count : 15;
264 for (uint64_t i = 0, e = OptimalCount; i != e; i++)
265 OW->Write8(Nops[OptimalCount - 1][i]);
266
267 // Finish with single byte nops.
268 for (uint64_t i = OptimalCount, e = Count; i != e; ++i)
269 OW->Write8(0x90);
270
271 return true;
272}
273
Daniel Dunbar82968002010-03-23 01:39:09 +0000274/* *** */
275
Chris Lattner9fc05222010-07-07 22:27:31 +0000276namespace {
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000277class ELFX86AsmBackend : public X86AsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000278 MCELFObjectFormat Format;
279
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000280public:
Roman Divacky5baf79e2010-09-09 17:57:50 +0000281 Triple::OSType OSType;
282 ELFX86AsmBackend(const Target &T, Triple::OSType _OSType)
283 : X86AsmBackend(T), OSType(_OSType) {
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000284 HasScatteredSymbols = true;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000285 HasReliableSymbolDifference = true;
286 }
287
Rafael Espindolaf230df92010-10-16 18:23:53 +0000288 virtual const MCObjectFormat &getObjectFormat() const {
289 return Format;
290 }
291
Rafael Espindola73ffea42010-09-25 05:42:19 +0000292 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
293 const MCSectionELF &ES = static_cast<const MCSectionELF&>(Section);
294 return ES.getFlags() & MCSectionELF::SHF_MERGE;
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000295 }
296
297 bool isVirtualSection(const MCSection &Section) const {
298 const MCSectionELF &SE = static_cast<const MCSectionELF&>(Section);
Roman Divacky5baf79e2010-09-09 17:57:50 +0000299 return SE.getType() == MCSectionELF::SHT_NOBITS;
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000300 }
301};
302
Matt Fleming7efaef62010-05-21 11:39:07 +0000303class ELFX86_32AsmBackend : public ELFX86AsmBackend {
304public:
Roman Divacky5baf79e2010-09-09 17:57:50 +0000305 ELFX86_32AsmBackend(const Target &T, Triple::OSType OSType)
306 : ELFX86AsmBackend(T, OSType) {}
Matt Fleming453db502010-08-16 18:36:14 +0000307
Kevin Enderby8ebf6622010-09-30 16:38:07 +0000308 unsigned getPointerSize() const {
309 return 4;
310 }
311
Matt Fleming453db502010-08-16 18:36:14 +0000312 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
313 return new ELFObjectWriter(OS, /*Is64Bit=*/false,
Wesley Peckeecb8582010-10-22 15:52:49 +0000314 OSType, ELF::EM_386,
Matt Fleming453db502010-08-16 18:36:14 +0000315 /*IsLittleEndian=*/true,
316 /*HasRelocationAddend=*/false);
317 }
Matt Fleming7efaef62010-05-21 11:39:07 +0000318};
319
320class ELFX86_64AsmBackend : public ELFX86AsmBackend {
321public:
Roman Divacky5baf79e2010-09-09 17:57:50 +0000322 ELFX86_64AsmBackend(const Target &T, Triple::OSType OSType)
323 : ELFX86AsmBackend(T, OSType) {}
Matt Fleming453db502010-08-16 18:36:14 +0000324
Kevin Enderby8ebf6622010-09-30 16:38:07 +0000325 unsigned getPointerSize() const {
326 return 8;
327 }
328
Matt Fleming453db502010-08-16 18:36:14 +0000329 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
330 return new ELFObjectWriter(OS, /*Is64Bit=*/true,
Wesley Peckeecb8582010-10-22 15:52:49 +0000331 OSType, ELF::EM_X86_64,
Matt Fleming453db502010-08-16 18:36:14 +0000332 /*IsLittleEndian=*/true,
333 /*HasRelocationAddend=*/true);
334 }
Matt Fleming7efaef62010-05-21 11:39:07 +0000335};
336
Michael J. Spencerdfd30182010-07-27 06:46:15 +0000337class WindowsX86AsmBackend : public X86AsmBackend {
Michael J. Spencerda0bfcd2010-08-21 05:58:13 +0000338 bool Is64Bit;
Rafael Espindolaf230df92010-10-16 18:23:53 +0000339 MCCOFFObjectFormat Format;
340
Michael J. Spencerdfd30182010-07-27 06:46:15 +0000341public:
Michael J. Spencerda0bfcd2010-08-21 05:58:13 +0000342 WindowsX86AsmBackend(const Target &T, bool is64Bit)
343 : X86AsmBackend(T)
344 , Is64Bit(is64Bit) {
Michael J. Spencerdfd30182010-07-27 06:46:15 +0000345 HasScatteredSymbols = true;
346 }
347
Rafael Espindolaf230df92010-10-16 18:23:53 +0000348 virtual const MCObjectFormat &getObjectFormat() const {
349 return Format;
350 }
351
Kevin Enderby8ebf6622010-09-30 16:38:07 +0000352 unsigned getPointerSize() const {
353 if (Is64Bit)
354 return 8;
355 else
356 return 4;
357 }
358
Michael J. Spencerdfd30182010-07-27 06:46:15 +0000359 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Michael J. Spencerda0bfcd2010-08-21 05:58:13 +0000360 return createWinCOFFObjectWriter(OS, Is64Bit);
Michael J. Spencerdfd30182010-07-27 06:46:15 +0000361 }
362
363 bool isVirtualSection(const MCSection &Section) const {
364 const MCSectionCOFF &SE = static_cast<const MCSectionCOFF&>(Section);
365 return SE.getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
366 }
367};
368
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000369class DarwinX86AsmBackend : public X86AsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000370 MCMachOObjectFormat Format;
371
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000372public:
373 DarwinX86AsmBackend(const Target &T)
Daniel Dunbar06829512010-03-18 00:58:53 +0000374 : X86AsmBackend(T) {
Daniel Dunbar06829512010-03-18 00:58:53 +0000375 HasScatteredSymbols = true;
376 }
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000377
Rafael Espindolaf230df92010-10-16 18:23:53 +0000378 virtual const MCObjectFormat &getObjectFormat() const {
379 return Format;
380 }
381
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000382 bool isVirtualSection(const MCSection &Section) const {
383 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
384 return (SMO.getType() == MCSectionMachO::S_ZEROFILL ||
Eric Christopher423c9e32010-05-17 21:02:07 +0000385 SMO.getType() == MCSectionMachO::S_GB_ZEROFILL ||
386 SMO.getType() == MCSectionMachO::S_THREAD_LOCAL_ZEROFILL);
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000387 }
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000388};
389
Daniel Dunbard6e59082010-03-15 21:56:50 +0000390class DarwinX86_32AsmBackend : public DarwinX86AsmBackend {
391public:
392 DarwinX86_32AsmBackend(const Target &T)
393 : DarwinX86AsmBackend(T) {}
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000394
Kevin Enderby8ebf6622010-09-30 16:38:07 +0000395 unsigned getPointerSize() const {
396 return 4;
397 }
398
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000399 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000400 return new MachObjectWriter(OS, /*Is64Bit=*/false, MachO::CPUTypeI386,
401 MachO::CPUSubType_I386_ALL);
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000402 }
Daniel Dunbard6e59082010-03-15 21:56:50 +0000403};
404
405class DarwinX86_64AsmBackend : public DarwinX86AsmBackend {
406public:
407 DarwinX86_64AsmBackend(const Target &T)
Daniel Dunbar06829512010-03-18 00:58:53 +0000408 : DarwinX86AsmBackend(T) {
409 HasReliableSymbolDifference = true;
410 }
Daniel Dunbard6e59082010-03-15 21:56:50 +0000411
Kevin Enderby8ebf6622010-09-30 16:38:07 +0000412 unsigned getPointerSize() const {
413 return 8;
414 }
415
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000416 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000417 return new MachObjectWriter(OS, /*Is64Bit=*/true, MachO::CPUTypeX86_64,
418 MachO::CPUSubType_I386_ALL);
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000419 }
420
Daniel Dunbard6e59082010-03-15 21:56:50 +0000421 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
422 // Temporary labels in the string literals sections require symbols. The
423 // issue is that the x86_64 relocation format does not allow symbol +
424 // offset, and so the linker does not have enough information to resolve the
425 // access to the appropriate atom unless an external relocation is used. For
426 // non-cstring sections, we expect the compiler to use a non-temporary label
427 // for anything that could have an addend pointing outside the symbol.
428 //
429 // See <rdar://problem/4765733>.
430 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
431 return SMO.getType() == MCSectionMachO::S_CSTRING_LITERALS;
432 }
Daniel Dunbara5f1d572010-05-12 00:38:17 +0000433
434 virtual bool isSectionAtomizable(const MCSection &Section) const {
435 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
436 // Fixed sized data sections are uniqued, they cannot be diced into atoms.
437 switch (SMO.getType()) {
438 default:
439 return true;
440
441 case MCSectionMachO::S_4BYTE_LITERALS:
442 case MCSectionMachO::S_8BYTE_LITERALS:
443 case MCSectionMachO::S_16BYTE_LITERALS:
444 case MCSectionMachO::S_LITERAL_POINTERS:
445 case MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS:
446 case MCSectionMachO::S_LAZY_SYMBOL_POINTERS:
447 case MCSectionMachO::S_MOD_INIT_FUNC_POINTERS:
448 case MCSectionMachO::S_MOD_TERM_FUNC_POINTERS:
449 case MCSectionMachO::S_INTERPOSING:
450 return false;
451 }
452 }
Daniel Dunbard6e59082010-03-15 21:56:50 +0000453};
454
Michael J. Spencerec38de22010-10-10 22:04:20 +0000455} // end anonymous namespace
Daniel Dunbar12783d12010-02-21 21:54:14 +0000456
457TargetAsmBackend *llvm::createX86_32AsmBackend(const Target &T,
Daniel Dunbar6c27f5e2010-03-11 01:34:16 +0000458 const std::string &TT) {
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000459 switch (Triple(TT).getOS()) {
460 case Triple::Darwin:
Daniel Dunbard6e59082010-03-15 21:56:50 +0000461 return new DarwinX86_32AsmBackend(T);
Benjamin Kramer56d23942010-08-04 15:32:40 +0000462 case Triple::MinGW32:
463 case Triple::Cygwin:
Michael J. Spencerdfd30182010-07-27 06:46:15 +0000464 case Triple::Win32:
Michael J. Spencerda0bfcd2010-08-21 05:58:13 +0000465 return new WindowsX86AsmBackend(T, false);
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000466 default:
Roman Divacky5baf79e2010-09-09 17:57:50 +0000467 return new ELFX86_32AsmBackend(T, Triple(TT).getOS());
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000468 }
Daniel Dunbar12783d12010-02-21 21:54:14 +0000469}
470
471TargetAsmBackend *llvm::createX86_64AsmBackend(const Target &T,
Daniel Dunbar6c27f5e2010-03-11 01:34:16 +0000472 const std::string &TT) {
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000473 switch (Triple(TT).getOS()) {
474 case Triple::Darwin:
Daniel Dunbard6e59082010-03-15 21:56:50 +0000475 return new DarwinX86_64AsmBackend(T);
Michael J. Spencerda0bfcd2010-08-21 05:58:13 +0000476 case Triple::MinGW64:
477 case Triple::Cygwin:
478 case Triple::Win32:
479 return new WindowsX86AsmBackend(T, true);
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000480 default:
Roman Divacky5baf79e2010-09-09 17:57:50 +0000481 return new ELFX86_64AsmBackend(T, Triple(TT).getOS());
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000482 }
Daniel Dunbar12783d12010-02-21 21:54:14 +0000483}