blob: fd823bf52448d3ec1843cafeb0a22bfd63ad6325 [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"
Daniel Dunbar87190c42010-03-19 09:28:12 +000014#include "llvm/MC/MCAssembler.h"
Daniel Dunbara5d0b542010-05-06 20:34:01 +000015#include "llvm/MC/MCExpr.h"
Rafael Espindolaf230df92010-10-16 18:23:53 +000016#include "llvm/MC/MCObjectFormat.h"
Daniel Dunbar337055e2010-03-23 03:13:05 +000017#include "llvm/MC/MCObjectWriter.h"
Michael J. Spencerdfd30182010-07-27 06:46:15 +000018#include "llvm/MC/MCSectionCOFF.h"
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +000019#include "llvm/MC/MCSectionELF.h"
Daniel Dunbard6e59082010-03-15 21:56:50 +000020#include "llvm/MC/MCSectionMachO.h"
Wesley Peckeecb8582010-10-22 15:52:49 +000021#include "llvm/Support/ELF.h"
Jim Grosbachc9d14392010-11-05 18:48:58 +000022#include "llvm/Support/MachO.h"
Daniel Dunbar82968002010-03-23 01:39:09 +000023#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/raw_ostream.h"
Daniel Dunbar12783d12010-02-21 21:54:14 +000025#include "llvm/Target/TargetRegistry.h"
26#include "llvm/Target/TargetAsmBackend.h"
27using namespace llvm;
28
Daniel Dunbar12783d12010-02-21 21:54:14 +000029
Daniel Dunbar87190c42010-03-19 09:28:12 +000030static unsigned getFixupKindLog2Size(unsigned Kind) {
31 switch (Kind) {
32 default: assert(0 && "invalid fixup kind!");
33 case X86::reloc_pcrel_1byte:
34 case FK_Data_1: return 0;
Chris Lattner9fc05222010-07-07 22:27:31 +000035 case X86::reloc_pcrel_2byte:
Daniel Dunbar87190c42010-03-19 09:28:12 +000036 case FK_Data_2: return 1;
37 case X86::reloc_pcrel_4byte:
38 case X86::reloc_riprel_4byte:
39 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +000040 case X86::reloc_signed_4byte:
Rafael Espindola24ba4f72010-10-24 17:35:42 +000041 case X86::reloc_global_offset_table:
Daniel Dunbar87190c42010-03-19 09:28:12 +000042 case FK_Data_4: return 2;
43 case FK_Data_8: return 3;
44 }
45}
46
Chris Lattner9fc05222010-07-07 22:27:31 +000047namespace {
Daniel Dunbar12783d12010-02-21 21:54:14 +000048class X86AsmBackend : public TargetAsmBackend {
49public:
Daniel Dunbar6c27f5e2010-03-11 01:34:16 +000050 X86AsmBackend(const Target &T)
Daniel Dunbar12783d12010-02-21 21:54:14 +000051 : TargetAsmBackend(T) {}
Daniel Dunbar87190c42010-03-19 09:28:12 +000052
Daniel Dunbarc90e30a2010-05-26 15:18:56 +000053 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
Daniel Dunbar87190c42010-03-19 09:28:12 +000054 uint64_t Value) const {
Daniel Dunbar482ad802010-05-26 15:18:31 +000055 unsigned Size = 1 << getFixupKindLog2Size(Fixup.getKind());
Daniel Dunbar87190c42010-03-19 09:28:12 +000056
Daniel Dunbar482ad802010-05-26 15:18:31 +000057 assert(Fixup.getOffset() + Size <= DF.getContents().size() &&
Daniel Dunbar87190c42010-03-19 09:28:12 +000058 "Invalid fixup offset!");
59 for (unsigned i = 0; i != Size; ++i)
Daniel Dunbar482ad802010-05-26 15:18:31 +000060 DF.getContents()[Fixup.getOffset() + i] = uint8_t(Value >> (i * 8));
Daniel Dunbar87190c42010-03-19 09:28:12 +000061 }
Daniel Dunbar82968002010-03-23 01:39:09 +000062
Daniel Dunbar84882522010-05-26 17:45:29 +000063 bool MayNeedRelaxation(const MCInst &Inst) const;
Daniel Dunbar337055e2010-03-23 03:13:05 +000064
Daniel Dunbar95506d42010-05-26 18:15:06 +000065 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +000066
67 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Daniel Dunbar12783d12010-02-21 21:54:14 +000068};
Michael J. Spencerec38de22010-10-10 22:04:20 +000069} // end anonymous namespace
Daniel Dunbar12783d12010-02-21 21:54:14 +000070
Rafael Espindolae4f506f2010-10-26 14:09:12 +000071static unsigned getRelaxedOpcodeBranch(unsigned Op) {
Daniel Dunbar82968002010-03-23 01:39:09 +000072 switch (Op) {
73 default:
74 return Op;
75
76 case X86::JAE_1: return X86::JAE_4;
77 case X86::JA_1: return X86::JA_4;
78 case X86::JBE_1: return X86::JBE_4;
79 case X86::JB_1: return X86::JB_4;
80 case X86::JE_1: return X86::JE_4;
81 case X86::JGE_1: return X86::JGE_4;
82 case X86::JG_1: return X86::JG_4;
83 case X86::JLE_1: return X86::JLE_4;
84 case X86::JL_1: return X86::JL_4;
85 case X86::JMP_1: return X86::JMP_4;
86 case X86::JNE_1: return X86::JNE_4;
87 case X86::JNO_1: return X86::JNO_4;
88 case X86::JNP_1: return X86::JNP_4;
89 case X86::JNS_1: return X86::JNS_4;
90 case X86::JO_1: return X86::JO_4;
91 case X86::JP_1: return X86::JP_4;
92 case X86::JS_1: return X86::JS_4;
93 }
94}
95
Rafael Espindolae4f506f2010-10-26 14:09:12 +000096static unsigned getRelaxedOpcodeArith(unsigned Op) {
97 switch (Op) {
98 default:
99 return Op;
100
101 // IMUL
102 case X86::IMUL16rri8: return X86::IMUL16rri;
103 case X86::IMUL16rmi8: return X86::IMUL16rmi;
104 case X86::IMUL32rri8: return X86::IMUL32rri;
105 case X86::IMUL32rmi8: return X86::IMUL32rmi;
106 case X86::IMUL64rri8: return X86::IMUL64rri32;
107 case X86::IMUL64rmi8: return X86::IMUL64rmi32;
108
109 // AND
110 case X86::AND16ri8: return X86::AND16ri;
111 case X86::AND16mi8: return X86::AND16mi;
112 case X86::AND32ri8: return X86::AND32ri;
113 case X86::AND32mi8: return X86::AND32mi;
114 case X86::AND64ri8: return X86::AND64ri32;
115 case X86::AND64mi8: return X86::AND64mi32;
116
117 // OR
118 case X86::OR16ri8: return X86::OR16ri;
119 case X86::OR16mi8: return X86::OR16mi;
120 case X86::OR32ri8: return X86::OR32ri;
121 case X86::OR32mi8: return X86::OR32mi;
122 case X86::OR64ri8: return X86::OR64ri32;
123 case X86::OR64mi8: return X86::OR64mi32;
124
125 // XOR
126 case X86::XOR16ri8: return X86::XOR16ri;
127 case X86::XOR16mi8: return X86::XOR16mi;
128 case X86::XOR32ri8: return X86::XOR32ri;
129 case X86::XOR32mi8: return X86::XOR32mi;
130 case X86::XOR64ri8: return X86::XOR64ri32;
131 case X86::XOR64mi8: return X86::XOR64mi32;
132
133 // ADD
134 case X86::ADD16ri8: return X86::ADD16ri;
135 case X86::ADD16mi8: return X86::ADD16mi;
136 case X86::ADD32ri8: return X86::ADD32ri;
137 case X86::ADD32mi8: return X86::ADD32mi;
138 case X86::ADD64ri8: return X86::ADD64ri32;
139 case X86::ADD64mi8: return X86::ADD64mi32;
140
141 // SUB
142 case X86::SUB16ri8: return X86::SUB16ri;
143 case X86::SUB16mi8: return X86::SUB16mi;
144 case X86::SUB32ri8: return X86::SUB32ri;
145 case X86::SUB32mi8: return X86::SUB32mi;
146 case X86::SUB64ri8: return X86::SUB64ri32;
147 case X86::SUB64mi8: return X86::SUB64mi32;
148
149 // CMP
150 case X86::CMP16ri8: return X86::CMP16ri;
151 case X86::CMP16mi8: return X86::CMP16mi;
152 case X86::CMP32ri8: return X86::CMP32ri;
153 case X86::CMP32mi8: return X86::CMP32mi;
154 case X86::CMP64ri8: return X86::CMP64ri32;
155 case X86::CMP64mi8: return X86::CMP64mi32;
156 }
157}
158
159static unsigned getRelaxedOpcode(unsigned Op) {
160 unsigned R = getRelaxedOpcodeArith(Op);
161 if (R != Op)
162 return R;
163 return getRelaxedOpcodeBranch(Op);
164}
165
Daniel Dunbar84882522010-05-26 17:45:29 +0000166bool X86AsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
Rafael Espindolae4f506f2010-10-26 14:09:12 +0000167 // Branches can always be relaxed.
168 if (getRelaxedOpcodeBranch(Inst.getOpcode()) != Inst.getOpcode())
169 return true;
170
Daniel Dunbar84882522010-05-26 17:45:29 +0000171 // Check if this instruction is ever relaxable.
Rafael Espindolae4f506f2010-10-26 14:09:12 +0000172 if (getRelaxedOpcodeArith(Inst.getOpcode()) == Inst.getOpcode())
Daniel Dunbar84882522010-05-26 17:45:29 +0000173 return false;
Daniel Dunbar482ad802010-05-26 15:18:31 +0000174
Rafael Espindolae4f506f2010-10-26 14:09:12 +0000175
176 // Check if it has an expression and is not RIP relative.
177 bool hasExp = false;
178 bool hasRIP = false;
179 for (unsigned i = 0; i < Inst.getNumOperands(); ++i) {
180 const MCOperand &Op = Inst.getOperand(i);
181 if (Op.isExpr())
182 hasExp = true;
183
184 if (Op.isReg() && Op.getReg() == X86::RIP)
185 hasRIP = true;
186 }
187
188 // FIXME: Why exactly do we need the !hasRIP? Is it just a limitation on
189 // how we do relaxations?
190 return hasExp && !hasRIP;
Daniel Dunbar337055e2010-03-23 03:13:05 +0000191}
192
Daniel Dunbar82968002010-03-23 01:39:09 +0000193// FIXME: Can tblgen help at all here to verify there aren't other instructions
194// we can relax?
Daniel Dunbar95506d42010-05-26 18:15:06 +0000195void X86AsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
Daniel Dunbar82968002010-03-23 01:39:09 +0000196 // The only relaxations X86 does is from a 1byte pcrel to a 4byte pcrel.
Daniel Dunbar95506d42010-05-26 18:15:06 +0000197 unsigned RelaxedOp = getRelaxedOpcode(Inst.getOpcode());
Daniel Dunbar82968002010-03-23 01:39:09 +0000198
Daniel Dunbar95506d42010-05-26 18:15:06 +0000199 if (RelaxedOp == Inst.getOpcode()) {
Daniel Dunbar82968002010-03-23 01:39:09 +0000200 SmallString<256> Tmp;
201 raw_svector_ostream OS(Tmp);
Daniel Dunbar95506d42010-05-26 18:15:06 +0000202 Inst.dump_pretty(OS);
Daniel Dunbarc9adb8c2010-05-26 15:18:13 +0000203 OS << "\n";
Chris Lattner75361b62010-04-07 22:58:41 +0000204 report_fatal_error("unexpected instruction to relax: " + OS.str());
Daniel Dunbar82968002010-03-23 01:39:09 +0000205 }
206
Daniel Dunbar95506d42010-05-26 18:15:06 +0000207 Res = Inst;
Daniel Dunbar82968002010-03-23 01:39:09 +0000208 Res.setOpcode(RelaxedOp);
209}
210
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000211/// WriteNopData - Write optimal nops to the output file for the \arg Count
212/// bytes. This returns the number of bytes written. It may return 0 if
213/// the \arg Count is more than the maximum optimal nops.
214///
215/// FIXME this is X86 32-bit specific and should move to a better place.
216bool X86AsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
217 static const uint8_t Nops[16][16] = {
218 // nop
219 {0x90},
220 // xchg %ax,%ax
221 {0x66, 0x90},
222 // nopl (%[re]ax)
223 {0x0f, 0x1f, 0x00},
224 // nopl 0(%[re]ax)
225 {0x0f, 0x1f, 0x40, 0x00},
226 // nopl 0(%[re]ax,%[re]ax,1)
227 {0x0f, 0x1f, 0x44, 0x00, 0x00},
228 // nopw 0(%[re]ax,%[re]ax,1)
229 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
230 // nopl 0L(%[re]ax)
231 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
232 // nopl 0L(%[re]ax,%[re]ax,1)
233 {0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
234 // nopw 0L(%[re]ax,%[re]ax,1)
235 {0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
236 // nopw %cs:0L(%[re]ax,%[re]ax,1)
237 {0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
Rafael Espindola7339fb52010-11-15 23:10:30 +0000238 // nopw %cs:0L(%[re]ax,%[re]ax,1)
239 {0x66, 0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000240 // nopw 0(%[re]ax,%[re]ax,1)
241 // nopw 0(%[re]ax,%[re]ax,1)
242 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,
243 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
244 // nopw 0(%[re]ax,%[re]ax,1)
245 // nopl 0L(%[re]ax) */
246 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,
247 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
248 // nopl 0L(%[re]ax)
249 // nopl 0L(%[re]ax)
250 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00,
251 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
252 // nopl 0L(%[re]ax)
253 // nopl 0L(%[re]ax,%[re]ax,1)
254 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00,
255 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}
256 };
257
258 // Write an optimal sequence for the first 15 bytes.
259 uint64_t OptimalCount = (Count < 16) ? Count : 15;
260 for (uint64_t i = 0, e = OptimalCount; i != e; i++)
261 OW->Write8(Nops[OptimalCount - 1][i]);
262
263 // Finish with single byte nops.
264 for (uint64_t i = OptimalCount, e = Count; i != e; ++i)
265 OW->Write8(0x90);
266
267 return true;
268}
269
Daniel Dunbar82968002010-03-23 01:39:09 +0000270/* *** */
271
Chris Lattner9fc05222010-07-07 22:27:31 +0000272namespace {
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000273class ELFX86AsmBackend : public X86AsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000274 MCELFObjectFormat Format;
275
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000276public:
Roman Divacky5baf79e2010-09-09 17:57:50 +0000277 Triple::OSType OSType;
278 ELFX86AsmBackend(const Target &T, Triple::OSType _OSType)
279 : X86AsmBackend(T), OSType(_OSType) {
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000280 HasScatteredSymbols = true;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000281 HasReliableSymbolDifference = true;
282 }
283
Rafael Espindolaf230df92010-10-16 18:23:53 +0000284 virtual const MCObjectFormat &getObjectFormat() const {
285 return Format;
286 }
287
Rafael Espindola73ffea42010-09-25 05:42:19 +0000288 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
289 const MCSectionELF &ES = static_cast<const MCSectionELF&>(Section);
290 return ES.getFlags() & MCSectionELF::SHF_MERGE;
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000291 }
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000292};
293
Matt Fleming7efaef62010-05-21 11:39:07 +0000294class ELFX86_32AsmBackend : public ELFX86AsmBackend {
295public:
Roman Divacky5baf79e2010-09-09 17:57:50 +0000296 ELFX86_32AsmBackend(const Target &T, Triple::OSType OSType)
297 : ELFX86AsmBackend(T, OSType) {}
Matt Fleming453db502010-08-16 18:36:14 +0000298
Kevin Enderby8ebf6622010-09-30 16:38:07 +0000299 unsigned getPointerSize() const {
300 return 4;
301 }
302
Matt Fleming453db502010-08-16 18:36:14 +0000303 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000304 return createELFObjectWriter(OS, /*Is64Bit=*/false,
305 OSType, ELF::EM_386,
306 /*IsLittleEndian=*/true,
307 /*HasRelocationAddend=*/false);
Matt Fleming453db502010-08-16 18:36:14 +0000308 }
Matt Fleming7efaef62010-05-21 11:39:07 +0000309};
310
311class ELFX86_64AsmBackend : public ELFX86AsmBackend {
312public:
Roman Divacky5baf79e2010-09-09 17:57:50 +0000313 ELFX86_64AsmBackend(const Target &T, Triple::OSType OSType)
314 : ELFX86AsmBackend(T, OSType) {}
Matt Fleming453db502010-08-16 18:36:14 +0000315
Kevin Enderby8ebf6622010-09-30 16:38:07 +0000316 unsigned getPointerSize() const {
317 return 8;
318 }
319
Matt Fleming453db502010-08-16 18:36:14 +0000320 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000321 return createELFObjectWriter(OS, /*Is64Bit=*/true,
322 OSType, ELF::EM_X86_64,
323 /*IsLittleEndian=*/true,
324 /*HasRelocationAddend=*/true);
Matt Fleming453db502010-08-16 18:36:14 +0000325 }
Matt Fleming7efaef62010-05-21 11:39:07 +0000326};
327
Michael J. Spencerdfd30182010-07-27 06:46:15 +0000328class WindowsX86AsmBackend : public X86AsmBackend {
Michael J. Spencerda0bfcd2010-08-21 05:58:13 +0000329 bool Is64Bit;
Rafael Espindolaf230df92010-10-16 18:23:53 +0000330 MCCOFFObjectFormat Format;
331
Michael J. Spencerdfd30182010-07-27 06:46:15 +0000332public:
Michael J. Spencerda0bfcd2010-08-21 05:58:13 +0000333 WindowsX86AsmBackend(const Target &T, bool is64Bit)
334 : X86AsmBackend(T)
335 , Is64Bit(is64Bit) {
Michael J. Spencerdfd30182010-07-27 06:46:15 +0000336 HasScatteredSymbols = true;
337 }
338
Rafael Espindolaf230df92010-10-16 18:23:53 +0000339 virtual const MCObjectFormat &getObjectFormat() const {
340 return Format;
341 }
342
Kevin Enderby8ebf6622010-09-30 16:38:07 +0000343 unsigned getPointerSize() const {
344 if (Is64Bit)
345 return 8;
346 else
347 return 4;
348 }
349
Michael J. Spencerdfd30182010-07-27 06:46:15 +0000350 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Michael J. Spencerda0bfcd2010-08-21 05:58:13 +0000351 return createWinCOFFObjectWriter(OS, Is64Bit);
Michael J. Spencerdfd30182010-07-27 06:46:15 +0000352 }
Michael J. Spencerdfd30182010-07-27 06:46:15 +0000353};
354
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000355class DarwinX86AsmBackend : public X86AsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000356 MCMachOObjectFormat Format;
357
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000358public:
359 DarwinX86AsmBackend(const Target &T)
Daniel Dunbar06829512010-03-18 00:58:53 +0000360 : X86AsmBackend(T) {
Daniel Dunbar06829512010-03-18 00:58:53 +0000361 HasScatteredSymbols = true;
362 }
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000363
Rafael Espindolaf230df92010-10-16 18:23:53 +0000364 virtual const MCObjectFormat &getObjectFormat() const {
365 return Format;
366 }
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000367};
368
Daniel Dunbard6e59082010-03-15 21:56:50 +0000369class DarwinX86_32AsmBackend : public DarwinX86AsmBackend {
370public:
371 DarwinX86_32AsmBackend(const Target &T)
372 : DarwinX86AsmBackend(T) {}
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000373
Kevin Enderby8ebf6622010-09-30 16:38:07 +0000374 unsigned getPointerSize() const {
375 return 4;
376 }
377
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000378 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000379 return createMachObjectWriter(OS, /*Is64Bit=*/false, MachO::CPUTypeI386,
380 MachO::CPUSubType_I386_ALL,
381 /*IsLittleEndian=*/true);
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000382 }
Daniel Dunbard6e59082010-03-15 21:56:50 +0000383};
384
385class DarwinX86_64AsmBackend : public DarwinX86AsmBackend {
386public:
387 DarwinX86_64AsmBackend(const Target &T)
Daniel Dunbar06829512010-03-18 00:58:53 +0000388 : DarwinX86AsmBackend(T) {
389 HasReliableSymbolDifference = true;
390 }
Daniel Dunbard6e59082010-03-15 21:56:50 +0000391
Kevin Enderby8ebf6622010-09-30 16:38:07 +0000392 unsigned getPointerSize() const {
393 return 8;
394 }
395
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000396 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000397 return createMachObjectWriter(OS, /*Is64Bit=*/true, MachO::CPUTypeX86_64,
398 MachO::CPUSubType_I386_ALL,
399 /*IsLittleEndian=*/true);
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000400 }
401
Daniel Dunbard6e59082010-03-15 21:56:50 +0000402 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
403 // Temporary labels in the string literals sections require symbols. The
404 // issue is that the x86_64 relocation format does not allow symbol +
405 // offset, and so the linker does not have enough information to resolve the
406 // access to the appropriate atom unless an external relocation is used. For
407 // non-cstring sections, we expect the compiler to use a non-temporary label
408 // for anything that could have an addend pointing outside the symbol.
409 //
410 // See <rdar://problem/4765733>.
411 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
412 return SMO.getType() == MCSectionMachO::S_CSTRING_LITERALS;
413 }
Daniel Dunbara5f1d572010-05-12 00:38:17 +0000414
415 virtual bool isSectionAtomizable(const MCSection &Section) const {
416 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
417 // Fixed sized data sections are uniqued, they cannot be diced into atoms.
418 switch (SMO.getType()) {
419 default:
420 return true;
421
422 case MCSectionMachO::S_4BYTE_LITERALS:
423 case MCSectionMachO::S_8BYTE_LITERALS:
424 case MCSectionMachO::S_16BYTE_LITERALS:
425 case MCSectionMachO::S_LITERAL_POINTERS:
426 case MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS:
427 case MCSectionMachO::S_LAZY_SYMBOL_POINTERS:
428 case MCSectionMachO::S_MOD_INIT_FUNC_POINTERS:
429 case MCSectionMachO::S_MOD_TERM_FUNC_POINTERS:
430 case MCSectionMachO::S_INTERPOSING:
431 return false;
432 }
433 }
Daniel Dunbard6e59082010-03-15 21:56:50 +0000434};
435
Michael J. Spencerec38de22010-10-10 22:04:20 +0000436} // end anonymous namespace
Daniel Dunbar12783d12010-02-21 21:54:14 +0000437
438TargetAsmBackend *llvm::createX86_32AsmBackend(const Target &T,
Daniel Dunbar6c27f5e2010-03-11 01:34:16 +0000439 const std::string &TT) {
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000440 switch (Triple(TT).getOS()) {
441 case Triple::Darwin:
Daniel Dunbard6e59082010-03-15 21:56:50 +0000442 return new DarwinX86_32AsmBackend(T);
Benjamin Kramer56d23942010-08-04 15:32:40 +0000443 case Triple::MinGW32:
444 case Triple::Cygwin:
Michael J. Spencerdfd30182010-07-27 06:46:15 +0000445 case Triple::Win32:
Michael J. Spencerda0bfcd2010-08-21 05:58:13 +0000446 return new WindowsX86AsmBackend(T, false);
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000447 default:
Roman Divacky5baf79e2010-09-09 17:57:50 +0000448 return new ELFX86_32AsmBackend(T, Triple(TT).getOS());
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000449 }
Daniel Dunbar12783d12010-02-21 21:54:14 +0000450}
451
452TargetAsmBackend *llvm::createX86_64AsmBackend(const Target &T,
Daniel Dunbar6c27f5e2010-03-11 01:34:16 +0000453 const std::string &TT) {
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000454 switch (Triple(TT).getOS()) {
455 case Triple::Darwin:
Daniel Dunbard6e59082010-03-15 21:56:50 +0000456 return new DarwinX86_64AsmBackend(T);
Michael J. Spencerda0bfcd2010-08-21 05:58:13 +0000457 case Triple::MinGW64:
458 case Triple::Cygwin:
459 case Triple::Win32:
460 return new WindowsX86AsmBackend(T, true);
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000461 default:
Roman Divacky5baf79e2010-09-09 17:57:50 +0000462 return new ELFX86_64AsmBackend(T, Triple(TT).getOS());
Daniel Dunbar23ac7c72010-03-11 01:34:21 +0000463 }
Daniel Dunbar12783d12010-02-21 21:54:14 +0000464}