blob: 80156c79c030c9871433e34d3e1de2bcba1b16ce [file] [log] [blame]
Jim Laskey8e8de8f2006-09-07 22:05:02 +00001//===-- X86TargetAsmInfo.cpp - X86 asm properties ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskey8e8de8f2006-09-07 22:05:02 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the declarations of the X86TargetAsmInfo properties.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86TargetAsmInfo.h"
15#include "X86TargetMachine.h"
16#include "X86Subtarget.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000017#include "llvm/DerivedTypes.h"
Chris Lattner625bd052006-11-29 01:14:06 +000018#include "llvm/InlineAsm.h"
19#include "llvm/Instructions.h"
Chris Lattner3e9f1d02007-04-01 20:49:36 +000020#include "llvm/Intrinsics.h"
Chris Lattner625bd052006-11-29 01:14:06 +000021#include "llvm/Module.h"
22#include "llvm/ADT/StringExtras.h"
Anton Korobeynikovcee750f2008-02-27 23:33:50 +000023#include "llvm/Support/Dwarf.h"
24
Jim Laskey8e8de8f2006-09-07 22:05:02 +000025using namespace llvm;
Anton Korobeynikovcee750f2008-02-27 23:33:50 +000026using namespace llvm::dwarf;
Jim Laskey8e8de8f2006-09-07 22:05:02 +000027
Dan Gohmancfbb2f02008-03-25 21:45:14 +000028static const char *const x86_asm_table[] = {
29 "{si}", "S",
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000030 "{di}", "D",
31 "{ax}", "a",
32 "{cx}", "c",
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000033 "{memory}", "memory",
34 "{flags}", "",
35 "{dirflag}", "",
36 "{fpsr}", "",
37 "{cc}", "cc",
38 0,0};
39
Jim Laskey8e8de8f2006-09-07 22:05:02 +000040X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
41 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
Anton Korobeynikovcee750f2008-02-27 23:33:50 +000042 X86TM = &TM;
43
Chris Lattnerafbfded2006-10-05 02:43:52 +000044 // FIXME - Should be simplified.
Andrew Lenharth6c0695f2006-11-28 19:52:49 +000045
46 AsmTransCBE = x86_asm_table;
47
Jim Laskey8e8de8f2006-09-07 22:05:02 +000048 switch (Subtarget->TargetType) {
49 case X86Subtarget::isDarwin:
50 AlignmentIsInBytes = false;
Evan Chengfb8075d2008-02-28 00:43:03 +000051 TextAlignFillValue = 0x90;
Jim Laskey8e8de8f2006-09-07 22:05:02 +000052 GlobalPrefix = "_";
Evan Cheng25ab6902006-09-08 06:48:29 +000053 if (!Subtarget->is64Bit())
54 Data64bitsDirective = 0; // we can't emit a 64-bit unit
Jim Laskey8e8de8f2006-09-07 22:05:02 +000055 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
56 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattner42ca4502007-01-17 17:43:33 +000057 BSSSection = 0; // no BSS section.
58 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
Jim Laskey8e8de8f2006-09-07 22:05:02 +000059 ConstantPoolSection = "\t.const\n";
Chris Lattnerafbfded2006-10-05 02:43:52 +000060 JumpTableDataSection = "\t.const\n";
Evan Cheng8910c1c2006-10-26 19:18:18 +000061 CStringSection = "\t.cstring";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000062 FourByteConstantSection = "\t.literal4\n";
63 EightByteConstantSection = "\t.literal8\n";
Evan Cheng25ab6902006-09-08 06:48:29 +000064 if (Subtarget->is64Bit())
65 SixteenByteConstantSection = "\t.literal16\n";
Evan Chengf0b5d562007-03-08 01:07:07 +000066 ReadOnlySection = "\t.const\n";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000067 LCOMMDirective = "\t.lcomm\t";
Dale Johannesen4af34942008-01-15 23:24:56 +000068 SwitchToSectionDirective = "\t.section ";
Dale Johannesen428ac542008-06-03 18:09:06 +000069 StringConstantPrefix = "\1LC";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000070 COMMDirectiveTakesAlignment = false;
71 HasDotTypeDotSizeDirective = false;
Evan Chengb267ca12007-01-30 08:04:53 +000072 if (TM.getRelocationModel() == Reloc::Static) {
73 StaticCtorsSection = ".constructor";
74 StaticDtorsSection = ".destructor";
75 } else {
76 StaticCtorsSection = ".mod_init_func";
77 StaticDtorsSection = ".mod_term_func";
78 }
Dale Johannesen4af34942008-01-15 23:24:56 +000079 if (Subtarget->is64Bit()) {
80 PersonalityPrefix = "";
81 PersonalitySuffix = "+4@GOTPCREL";
82 } else {
83 PersonalityPrefix = "L";
84 PersonalitySuffix = "$non_lazy_ptr";
85 }
Bill Wendlingef4a6612007-09-11 17:20:55 +000086 NeedsIndirectEncoding = true;
Dale Johannesen27c31052008-03-25 23:29:30 +000087 InlineAsmStart = "## InlineAsm Start";
88 InlineAsmEnd = "## InlineAsm End";
89 CommentString = "##";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000090 SetDirective = "\t.set";
Bill Wendlingadbda022007-08-22 18:44:05 +000091 PCSymbol = ".";
Chris Lattnera53115b2006-09-26 03:39:53 +000092 UsedDirective = "\t.no_dead_strip\t";
Dale Johannesen038129d2008-01-10 02:03:30 +000093 WeakDefDirective = "\t.weak_definition ";
94 WeakRefDirective = "\t.weak_reference ";
Dale Johannesenc7406ae2008-01-11 00:54:37 +000095 HiddenDirective = "\t.private_extern ";
Bill Wendlingbebc3642008-03-18 23:38:12 +000096 ProtectedDirective = "\t.globl\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +000097
Chris Lattnerb266ccd2007-01-18 01:15:58 +000098 // In non-PIC modes, emit a special label before jump tables so that the
99 // linker can perform more accurate dead code stripping.
100 if (TM.getRelocationModel() != Reloc::PIC_) {
101 // Emit a local label that is preserved until the linker runs.
102 JumpTableSpecialLabelPrefix = "l";
103 }
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000104
105 SupportsDebugInformation = true;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000106 NeedsSet = true;
107 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
108 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
109 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
110 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
111 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
112 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
113 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
114 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
115 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
116 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
117 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
Anton Korobeynikov095546c2007-08-21 00:31:30 +0000118
119 // Exceptions handling
Dale Johannesen4af34942008-01-15 23:24:56 +0000120 SupportsExceptionHandling = true;
Dale Johannesen038129d2008-01-10 02:03:30 +0000121 GlobalEHDirective = "\t.globl\t";
122 SupportsWeakOmittedEHFrame = false;
Anton Korobeynikov095546c2007-08-21 00:31:30 +0000123 AbsoluteEHSectionOffsets = false;
124 DwarfEHFrameSection =
125 ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support";
126 DwarfExceptionSection = ".section __DATA,__gcc_except_tab";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000127 break;
Reid Spencer02b85112006-10-30 22:32:30 +0000128
129 case X86Subtarget::isELF:
Dan Gohman6445f612007-06-27 15:09:47 +0000130 ReadOnlySection = "\t.section\t.rodata";
Dan Gohman6f858e22007-05-03 16:38:57 +0000131 FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
132 EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000133 SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16";
Dan Gohman6f858e22007-05-03 16:38:57 +0000134 CStringSection = "\t.section\t.rodata.str1.1,\"aMS\",@progbits,1";
Chris Lattnera3bfdd42007-01-03 18:16:48 +0000135 PrivateGlobalPrefix = ".L";
Evan Cheng8752ce62006-12-01 20:47:11 +0000136 WeakRefDirective = "\t.weak\t";
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000137 SetDirective = "\t.set\t";
Anton Korobeynikovedb4a6b2007-05-01 10:19:31 +0000138 PCSymbol = ".";
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000139
140 // Set up DWARF directives
141 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +0000142
143 // Debug Information
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000144 AbsoluteDebugSectionOffsets = true;
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000145 SupportsDebugInformation = true;
Reid Spencer02b85112006-10-30 22:32:30 +0000146 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits";
147 DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits";
148 DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits";
149 DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits";
150 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
151 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
152 DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits";
153 DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits";
154 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
155 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits";
156 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000157
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +0000158 // Exceptions handling
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000159 if (!Subtarget->is64Bit())
160 SupportsExceptionHandling = true;
Anton Korobeynikov59db3ec2007-07-25 00:06:28 +0000161 AbsoluteEHSectionOffsets = false;
Anton Korobeynikov71144d82007-05-01 10:16:06 +0000162 DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits";
163 DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits";
Reid Spencer02b85112006-10-30 22:32:30 +0000164 break;
165
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000166 case X86Subtarget::isCygwin:
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000167 case X86Subtarget::isMingw:
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000168 GlobalPrefix = "_";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000169 LCOMMDirective = "\t.lcomm\t";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000170 COMMDirectiveTakesAlignment = false;
171 HasDotTypeDotSizeDirective = false;
172 StaticCtorsSection = "\t.section .ctors,\"aw\"";
173 StaticDtorsSection = "\t.section .dtors,\"aw\"";
Anton Korobeynikov1a856452007-01-14 11:49:39 +0000174 HiddenDirective = NULL;
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000175 PrivateGlobalPrefix = "L"; // Prefix for private global symbols
176 WeakRefDirective = "\t.weak\t";
177 SetDirective = "\t.set\t";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000178
179 // Set up DWARF directives
180 HasLEB128 = true; // Target asm supports leb128 directives (little-endian)
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +0000181 AbsoluteDebugSectionOffsets = true;
182 AbsoluteEHSectionOffsets = false;
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000183 SupportsDebugInformation = true;
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000184 DwarfSectionOffsetDirective = "\t.secrel32\t";
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000185 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\"";
186 DwarfInfoSection = "\t.section\t.debug_info,\"dr\"";
187 DwarfLineSection = "\t.section\t.debug_line,\"dr\"";
188 DwarfFrameSection = "\t.section\t.debug_frame,\"dr\"";
189 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
190 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
191 DwarfStrSection = "\t.section\t.debug_str,\"dr\"";
192 DwarfLocSection = "\t.section\t.debug_loc,\"dr\"";
193 DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
194 DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\"";
195 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
196 break;
Bill Wendling11502862007-01-16 04:13:03 +0000197
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000198 case X86Subtarget::isWindows:
199 GlobalPrefix = "_";
200 HasDotTypeDotSizeDirective = false;
201 break;
Bill Wendling11502862007-01-16 04:13:03 +0000202
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000203 default: break;
204 }
205
206 if (Subtarget->isFlavorIntel()) {
207 GlobalPrefix = "_";
208 CommentString = ";";
209
210 PrivateGlobalPrefix = "$";
211 AlignDirective = "\talign\t";
212 ZeroDirective = "\tdb\t";
213 ZeroDirectiveSuffix = " dup(0)";
214 AsciiDirective = "\tdb\t";
215 AscizDirective = 0;
216 Data8bitsDirective = "\tdb\t";
217 Data16bitsDirective = "\tdw\t";
218 Data32bitsDirective = "\tdd\t";
219 Data64bitsDirective = "\tdq\t";
220 HasDotTypeDotSizeDirective = false;
221
222 TextSection = "_text";
223 DataSection = "_data";
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000224 JumpTableDataSection = NULL;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000225 SwitchToSectionDirective = "";
226 TextSectionStartSuffix = "\tsegment 'CODE'";
227 DataSectionStartSuffix = "\tsegment 'DATA'";
228 SectionEndDirectiveSuffix = "\tends\n";
229 }
Bill Wendlingcb900992007-01-16 09:29:17 +0000230
Dan Gohmana779a982008-05-05 00:28:39 +0000231 // On Linux we must declare when we can use a non-executable stack.
Dan Gohman600bf162008-05-05 16:11:31 +0000232 if (Subtarget->isLinux())
Dan Gohmana779a982008-05-05 00:28:39 +0000233 NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits";
234
Bill Wendlingcb900992007-01-16 09:29:17 +0000235 AssemblerDialect = Subtarget->getAsmFlavor();
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000236}
Chris Lattnerafbfded2006-10-05 02:43:52 +0000237
Chris Lattner625bd052006-11-29 01:14:06 +0000238bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
239 // FIXME: this should verify that we are targetting a 486 or better. If not,
240 // we will turn this bswap into something that will be lowered to logical ops
241 // instead of emitting the bswap asm. For now, we don't support 486 or lower
242 // so don't worry about this.
243
244 // Verify this is a simple bswap.
245 if (CI->getNumOperands() != 2 ||
246 CI->getType() != CI->getOperand(1)->getType() ||
Chris Lattner42a75512007-01-15 02:27:26 +0000247 !CI->getType()->isInteger())
Chris Lattner625bd052006-11-29 01:14:06 +0000248 return false;
249
Chris Lattner3e9f1d02007-04-01 20:49:36 +0000250 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
251 if (!Ty || Ty->getBitWidth() % 16 != 0)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000252 return false;
Chris Lattner3e9f1d02007-04-01 20:49:36 +0000253
Chris Lattner625bd052006-11-29 01:14:06 +0000254 // Okay, we can do this xform, do so now.
Chandler Carruth69940402007-08-04 01:51:18 +0000255 const Type *Tys[] = { Ty };
Chris Lattner625bd052006-11-29 01:14:06 +0000256 Module *M = CI->getParent()->getParent()->getParent();
Chandler Carruth69940402007-08-04 01:51:18 +0000257 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Chris Lattner625bd052006-11-29 01:14:06 +0000258
259 Value *Op = CI->getOperand(1);
Gabor Greif051a9502008-04-06 20:25:17 +0000260 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Chris Lattner625bd052006-11-29 01:14:06 +0000261
Chris Lattner625bd052006-11-29 01:14:06 +0000262 CI->replaceAllUsesWith(Op);
263 CI->eraseFromParent();
264 return true;
265}
266
267
268bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
269 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattner5d521352006-11-29 01:48:01 +0000270 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
Chris Lattner625bd052006-11-29 01:14:06 +0000271
272 std::string AsmStr = IA->getAsmString();
273
274 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
275 std::vector<std::string> AsmPieces;
276 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
277
278 switch (AsmPieces.size()) {
279 default: return false;
280 case 1:
281 AsmStr = AsmPieces[0];
282 AsmPieces.clear();
283 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
284
Chris Lattner5d521352006-11-29 01:48:01 +0000285 // bswap $0
Chris Lattner625bd052006-11-29 01:14:06 +0000286 if (AsmPieces.size() == 2 &&
287 AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
288 // No need to check constraints, nothing other than the equivalent of
289 // "=r,0" would be valid here.
290 return LowerToBSwap(CI);
291 }
292 break;
Chris Lattner5d521352006-11-29 01:48:01 +0000293 case 3:
Reid Spencer47857812006-12-31 05:55:36 +0000294 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
Chris Lattner5d521352006-11-29 01:48:01 +0000295 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
296 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
297 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
298 std::vector<std::string> Words;
299 SplitString(AsmPieces[0], Words, " \t");
300 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
301 Words.clear();
302 SplitString(AsmPieces[1], Words, " \t");
303 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
304 Words.clear();
305 SplitString(AsmPieces[2], Words, " \t,");
306 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
307 Words[2] == "%edx") {
308 return LowerToBSwap(CI);
309 }
310 }
311 }
312 }
313 break;
Chris Lattner625bd052006-11-29 01:14:06 +0000314 }
315 return false;
316}
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000317
318/// PreferredEHDataFormat - This hook allows the target to select data
319/// format used for encoding pointers in exception handling data. Reason is
320/// 0 for data, 1 for code labels, 2 for function pointers. Global is true
321/// if the symbol can be relocated.
Anton Korobeynikov8213f9c2008-02-29 22:09:08 +0000322unsigned X86TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000323 bool Global) const {
324 const X86Subtarget *Subtarget = &X86TM->getSubtarget<X86Subtarget>();
325
326 switch (Subtarget->TargetType) {
327 case X86Subtarget::isDarwin:
Anton Korobeynikov8213f9c2008-02-29 22:09:08 +0000328 if (Reason == DwarfEncoding::Functions && Global)
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000329 return (DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
Anton Korobeynikov8213f9c2008-02-29 22:09:08 +0000330 else if (Reason == DwarfEncoding::CodeLabels || !Global)
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000331 return DW_EH_PE_pcrel;
332 else
333 return DW_EH_PE_absptr;
334
335 case X86Subtarget::isELF:
336 case X86Subtarget::isCygwin:
337 case X86Subtarget::isMingw: {
338 CodeModel::Model CM = X86TM->getCodeModel();
339
340 if (X86TM->getRelocationModel() == Reloc::PIC_) {
341 unsigned Format = 0;
342
343 if (!Subtarget->is64Bit())
344 // 32 bit targets always encode pointers as 4 bytes
345 Format = DW_EH_PE_sdata4;
346 else {
347 // 64 bit targets encode pointers in 4 bytes iff:
348 // - code model is small OR
Anton Korobeynikovc0f41db2008-07-09 13:19:08 +0000349 // - code model is medium and we're emitting externally visible symbols
350 // or any code symbols
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000351 if (CM == CodeModel::Small ||
Anton Korobeynikov8213f9c2008-02-29 22:09:08 +0000352 (CM == CodeModel::Medium && (Global ||
353 Reason != DwarfEncoding::Data)))
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000354 Format = DW_EH_PE_sdata4;
355 else
356 Format = DW_EH_PE_sdata8;
357 }
358
359 if (Global)
360 Format |= DW_EH_PE_indirect;
361
362 return (Format | DW_EH_PE_pcrel);
363 } else {
364 if (Subtarget->is64Bit() &&
365 (CM == CodeModel::Small ||
Anton Korobeynikov8213f9c2008-02-29 22:09:08 +0000366 (CM == CodeModel::Medium && Reason != DwarfEncoding::Data)))
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000367 return DW_EH_PE_udata4;
368 else
369 return DW_EH_PE_absptr;
370 }
371 }
372
373 default:
374 return TargetAsmInfo::PreferredEHDataFormat(Reason, Global);
375 }
376}
377
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000378std::string X86TargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
379 SectionKind::Kind kind) const {
380 const X86Subtarget *Subtarget = &X86TM->getSubtarget<X86Subtarget>();
381
382 switch (Subtarget->TargetType) {
383 case X86Subtarget::isDarwin:
384 if (kind == SectionKind::Text)
385 return "__TEXT,__textcoal_nt,coalesced,pure_instructions";
386 else
387 return "__DATA,__datacoal_nt,coalesced";
388 case X86Subtarget::isCygwin:
389 case X86Subtarget::isMingw:
390 switch (kind) {
391 case SectionKind::Text:
392 return ".text$linkonce" + GV->getName();
393 case SectionKind::Data:
394 case SectionKind::BSS:
395 case SectionKind::ThreadData:
396 case SectionKind::ThreadBSS:
397 return ".data$linkonce" + GV->getName();
398 case SectionKind::ROData:
399 case SectionKind::RODataMergeConst:
400 case SectionKind::RODataMergeStr:
401 return ".rdata$linkonce" + GV->getName();
402 default:
403 assert(0 && "Unknown section kind");
404 }
405 case X86Subtarget::isELF:
406 return TargetAsmInfo::UniqueSectionForGlobal(GV, kind);
407 default:
408 return "";
409 }
410}
411
412
Anton Korobeynikovc0f41db2008-07-09 13:19:08 +0000413std::string X86TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
414 const X86Subtarget *Subtarget = &X86TM->getSubtarget<X86Subtarget>();
415 SectionKind::Kind kind = SectionKindForGlobal(GV);
416 unsigned flags = SectionFlagsForGlobal(GV, GV->getSection().c_str());
417 std::string Name;
418
419 // FIXME: Should we use some hashing based on section name and just check
420 // flags?
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000421 // FIXME: It seems, that Darwin uses much more sections.
Anton Korobeynikovc0f41db2008-07-09 13:19:08 +0000422
423 // Select section name
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000424 if (GV->hasSection()) {
425 // Honour section already set, if any
426 Name = GV->getSection();
427 } else {
428 // Use default section depending on the 'type' of global
429 if (const Function *F = dyn_cast<Function>(GV)) {
430 switch (F->getLinkage()) {
431 default: assert(0 && "Unknown linkage type!");
432 case Function::InternalLinkage:
433 case Function::DLLExportLinkage:
434 case Function::ExternalLinkage:
435 Name = TextSection;
Anton Korobeynikovc0f41db2008-07-09 13:19:08 +0000436 break;
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000437 case Function::WeakLinkage:
438 case Function::LinkOnceLinkage:
439 Name = UniqueSectionForGlobal(F, kind);
Anton Korobeynikovc0f41db2008-07-09 13:19:08 +0000440 break;
Anton Korobeynikovc0f41db2008-07-09 13:19:08 +0000441 }
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000442 } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
443 if (GVar->hasCommonLinkage() ||
444 GVar->hasLinkOnceLinkage() ||
445 GVar->hasWeakLinkage())
446 Name = UniqueSectionForGlobal(GVar, kind);
447 else {
448 switch (kind) {
449 case SectionKind::Data:
450 Name = DataSection;
451 break;
452 case SectionKind::BSS:
453 Name = (BSSSection ? BSSSection : DataSection);
454 break;
455 case SectionKind::ROData:
456 case SectionKind::RODataMergeStr:
457 case SectionKind::RODataMergeConst:
458 // FIXME: Temporary
459 Name = DataSection;
460 break;
461 case SectionKind::ThreadData:
462 Name = (TLSDataSection ? TLSDataSection : DataSection);
463 break;
464 case SectionKind::ThreadBSS:
465 Name = (TLSBSSSection ? TLSBSSSection : DataSection);
466 default:
467 assert(0 && "Unsuported section kind for global");
468 }
469 }
470 } else
471 assert(0 && "Unsupported global");
472 }
Anton Korobeynikovc0f41db2008-07-09 13:19:08 +0000473
474 // Add all special flags, etc
475 switch (Subtarget->TargetType) {
476 case X86Subtarget::isELF:
477 Name += ",\"";
478
479 if (!(flags & SectionFlags::Debug))
480 Name += 'a';
481 if (flags & SectionFlags::Code)
482 Name += 'x';
483 if (flags & SectionFlags::Writeable)
484 Name += 'w';
485 if (flags & SectionFlags::Mergeable)
486 Name += 'M';
487 if (flags & SectionFlags::Strings)
488 Name += 'S';
489 if (flags & SectionFlags::TLS)
490 Name += 'T';
491
492 Name += "\"";
493
494 // FIXME: There can be exceptions here
495 if (flags & SectionFlags::BSS)
496 Name += ",@nobits";
497 else
498 Name += ",@progbits";
499
500 // FIXME: entity size for mergeable sections
501 break;
502 case X86Subtarget::isCygwin:
503 case X86Subtarget::isMingw:
504 Name += ",\"";
505
506 if (flags & SectionFlags::Code)
507 Name += 'x';
508 if (flags & SectionFlags::Writeable)
509 Name += 'w';
510
511 Name += "\"";
512
513 break;
514 case X86Subtarget::isDarwin:
515 // Darwin does not use any special flags
516 default:
517 break;
518 }
519
520 return Name;
521}
522