Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 1 | //===-- X86TargetAsmInfo.cpp - X86 asm properties ---------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 7 | // |
| 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 Spencer | 7aa8a45 | 2007-01-12 23:22:14 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 18 | #include "llvm/InlineAsm.h" |
| 19 | #include "llvm/Instructions.h" |
Chris Lattner | 3e9f1d0 | 2007-04-01 20:49:36 +0000 | [diff] [blame] | 20 | #include "llvm/Intrinsics.h" |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 21 | #include "llvm/Module.h" |
| 22 | #include "llvm/ADT/StringExtras.h" |
Anton Korobeynikov | cee750f | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Dwarf.h" |
| 24 | |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Anton Korobeynikov | cee750f | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 26 | using namespace llvm::dwarf; |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 27 | |
Dan Gohman | cfbb2f0 | 2008-03-25 21:45:14 +0000 | [diff] [blame] | 28 | static const char *const x86_asm_table[] = { |
| 29 | "{si}", "S", |
Andrew Lenharth | 6c0695f | 2006-11-28 19:52:49 +0000 | [diff] [blame] | 30 | "{di}", "D", |
| 31 | "{ax}", "a", |
| 32 | "{cx}", "c", |
Andrew Lenharth | 6c0695f | 2006-11-28 19:52:49 +0000 | [diff] [blame] | 33 | "{memory}", "memory", |
| 34 | "{flags}", "", |
| 35 | "{dirflag}", "", |
| 36 | "{fpsr}", "", |
| 37 | "{cc}", "cc", |
| 38 | 0,0}; |
| 39 | |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 40 | X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) { |
| 41 | const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>(); |
Anton Korobeynikov | cee750f | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 42 | X86TM = &TM; |
| 43 | |
Andrew Lenharth | 6c0695f | 2006-11-28 19:52:49 +0000 | [diff] [blame] | 44 | AsmTransCBE = x86_asm_table; |
Anton Korobeynikov | 8210045 | 2008-07-09 13:20:27 +0000 | [diff] [blame] | 45 | |
Bill Wendling | cb90099 | 2007-01-16 09:29:17 +0000 | [diff] [blame] | 46 | AssemblerDialect = Subtarget->getAsmFlavor(); |
Jim Laskey | 8e8de8f | 2006-09-07 22:05:02 +0000 | [diff] [blame] | 47 | } |
Chris Lattner | afbfded | 2006-10-05 02:43:52 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 49 | bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const { |
| 50 | // FIXME: this should verify that we are targetting a 486 or better. If not, |
| 51 | // we will turn this bswap into something that will be lowered to logical ops |
| 52 | // instead of emitting the bswap asm. For now, we don't support 486 or lower |
| 53 | // so don't worry about this. |
Anton Korobeynikov | 8210045 | 2008-07-09 13:20:27 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 55 | // Verify this is a simple bswap. |
| 56 | if (CI->getNumOperands() != 2 || |
| 57 | CI->getType() != CI->getOperand(1)->getType() || |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 58 | !CI->getType()->isInteger()) |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 59 | return false; |
Anton Korobeynikov | 8210045 | 2008-07-09 13:20:27 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 3e9f1d0 | 2007-04-01 20:49:36 +0000 | [diff] [blame] | 61 | const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType()); |
| 62 | if (!Ty || Ty->getBitWidth() % 16 != 0) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 63 | return false; |
Anton Korobeynikov | 8210045 | 2008-07-09 13:20:27 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 65 | // Okay, we can do this xform, do so now. |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 66 | const Type *Tys[] = { Ty }; |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 67 | Module *M = CI->getParent()->getParent()->getParent(); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 68 | Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1); |
Anton Korobeynikov | 8210045 | 2008-07-09 13:20:27 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 70 | Value *Op = CI->getOperand(1); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 71 | Op = CallInst::Create(Int, Op, CI->getName(), CI); |
Anton Korobeynikov | 8210045 | 2008-07-09 13:20:27 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 73 | CI->replaceAllUsesWith(Op); |
| 74 | CI->eraseFromParent(); |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const { |
| 80 | InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue()); |
Chris Lattner | 5d52135 | 2006-11-29 01:48:01 +0000 | [diff] [blame] | 81 | std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints(); |
Anton Korobeynikov | 8210045 | 2008-07-09 13:20:27 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 83 | std::string AsmStr = IA->getAsmString(); |
Anton Korobeynikov | 8210045 | 2008-07-09 13:20:27 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 85 | // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a" |
| 86 | std::vector<std::string> AsmPieces; |
| 87 | SplitString(AsmStr, AsmPieces, "\n"); // ; as separator? |
Anton Korobeynikov | 8210045 | 2008-07-09 13:20:27 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 89 | switch (AsmPieces.size()) { |
Anton Korobeynikov | 8210045 | 2008-07-09 13:20:27 +0000 | [diff] [blame] | 90 | default: return false; |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 91 | case 1: |
| 92 | AsmStr = AsmPieces[0]; |
| 93 | AsmPieces.clear(); |
| 94 | SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace. |
Anton Korobeynikov | 8210045 | 2008-07-09 13:20:27 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 5d52135 | 2006-11-29 01:48:01 +0000 | [diff] [blame] | 96 | // bswap $0 |
Anton Korobeynikov | 8210045 | 2008-07-09 13:20:27 +0000 | [diff] [blame] | 97 | if (AsmPieces.size() == 2 && |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 98 | AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") { |
| 99 | // No need to check constraints, nothing other than the equivalent of |
| 100 | // "=r,0" would be valid here. |
| 101 | return LowerToBSwap(CI); |
| 102 | } |
| 103 | break; |
Chris Lattner | 5d52135 | 2006-11-29 01:48:01 +0000 | [diff] [blame] | 104 | case 3: |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 105 | if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 && |
Chris Lattner | 5d52135 | 2006-11-29 01:48:01 +0000 | [diff] [blame] | 106 | Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" && |
| 107 | Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") { |
| 108 | // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64 |
| 109 | std::vector<std::string> Words; |
| 110 | SplitString(AsmPieces[0], Words, " \t"); |
| 111 | if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") { |
| 112 | Words.clear(); |
| 113 | SplitString(AsmPieces[1], Words, " \t"); |
| 114 | if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") { |
| 115 | Words.clear(); |
| 116 | SplitString(AsmPieces[2], Words, " \t,"); |
| 117 | if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" && |
| 118 | Words[2] == "%edx") { |
| 119 | return LowerToBSwap(CI); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | break; |
Chris Lattner | 625bd05 | 2006-11-29 01:14:06 +0000 | [diff] [blame] | 125 | } |
| 126 | return false; |
| 127 | } |
Anton Korobeynikov | cee750f | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 128 | |
Anton Korobeynikov | 4468b7a | 2008-07-09 13:20:48 +0000 | [diff] [blame] | 129 | X86DarwinTargetAsmInfo::X86DarwinTargetAsmInfo(const X86TargetMachine &TM): |
| 130 | X86TargetAsmInfo(TM) { |
| 131 | bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit(); |
| 132 | |
| 133 | AlignmentIsInBytes = false; |
| 134 | TextAlignFillValue = 0x90; |
| 135 | GlobalPrefix = "_"; |
| 136 | if (!is64Bit) |
| 137 | Data64bitsDirective = 0; // we can't emit a 64-bit unit |
| 138 | ZeroDirective = "\t.space\t"; // ".space N" emits N zeros. |
| 139 | PrivateGlobalPrefix = "L"; // Marker for constant pool idxs |
| 140 | BSSSection = 0; // no BSS section. |
| 141 | ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill |
| 142 | ConstantPoolSection = "\t.const\n"; |
| 143 | JumpTableDataSection = "\t.const\n"; |
| 144 | CStringSection = "\t.cstring"; |
| 145 | FourByteConstantSection = "\t.literal4\n"; |
| 146 | EightByteConstantSection = "\t.literal8\n"; |
| 147 | // FIXME: Why don't always use this section? |
| 148 | if (is64Bit) |
| 149 | SixteenByteConstantSection = "\t.literal16\n"; |
| 150 | ReadOnlySection = "\t.const\n"; |
| 151 | LCOMMDirective = "\t.lcomm\t"; |
| 152 | SwitchToSectionDirective = "\t.section "; |
| 153 | StringConstantPrefix = "\1LC"; |
| 154 | COMMDirectiveTakesAlignment = false; |
| 155 | HasDotTypeDotSizeDirective = false; |
| 156 | if (TM.getRelocationModel() == Reloc::Static) { |
| 157 | StaticCtorsSection = ".constructor"; |
| 158 | StaticDtorsSection = ".destructor"; |
| 159 | } else { |
| 160 | StaticCtorsSection = ".mod_init_func"; |
| 161 | StaticDtorsSection = ".mod_term_func"; |
| 162 | } |
| 163 | if (is64Bit) { |
| 164 | PersonalityPrefix = ""; |
| 165 | PersonalitySuffix = "+4@GOTPCREL"; |
| 166 | } else { |
| 167 | PersonalityPrefix = "L"; |
| 168 | PersonalitySuffix = "$non_lazy_ptr"; |
| 169 | } |
| 170 | NeedsIndirectEncoding = true; |
| 171 | InlineAsmStart = "## InlineAsm Start"; |
| 172 | InlineAsmEnd = "## InlineAsm End"; |
| 173 | CommentString = "##"; |
| 174 | SetDirective = "\t.set"; |
| 175 | PCSymbol = "."; |
| 176 | UsedDirective = "\t.no_dead_strip\t"; |
| 177 | WeakDefDirective = "\t.weak_definition "; |
| 178 | WeakRefDirective = "\t.weak_reference "; |
| 179 | HiddenDirective = "\t.private_extern "; |
| 180 | ProtectedDirective = "\t.globl\t"; |
| 181 | |
| 182 | // In non-PIC modes, emit a special label before jump tables so that the |
| 183 | // linker can perform more accurate dead code stripping. |
| 184 | if (TM.getRelocationModel() != Reloc::PIC_) { |
| 185 | // Emit a local label that is preserved until the linker runs. |
| 186 | JumpTableSpecialLabelPrefix = "l"; |
| 187 | } |
| 188 | |
| 189 | SupportsDebugInformation = true; |
| 190 | NeedsSet = true; |
| 191 | DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug"; |
| 192 | DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug"; |
| 193 | DwarfLineSection = ".section __DWARF,__debug_line,regular,debug"; |
| 194 | DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug"; |
| 195 | DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug"; |
| 196 | DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug"; |
| 197 | DwarfStrSection = ".section __DWARF,__debug_str,regular,debug"; |
| 198 | DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug"; |
| 199 | DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug"; |
| 200 | DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug"; |
| 201 | DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug"; |
| 202 | |
| 203 | // Exceptions handling |
| 204 | SupportsExceptionHandling = true; |
| 205 | GlobalEHDirective = "\t.globl\t"; |
| 206 | SupportsWeakOmittedEHFrame = false; |
| 207 | AbsoluteEHSectionOffsets = false; |
| 208 | DwarfEHFrameSection = |
| 209 | ".section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support"; |
| 210 | DwarfExceptionSection = ".section __DATA,__gcc_except_tab"; |
| 211 | } |
| 212 | |
Anton Korobeynikov | b9e58ef | 2008-07-09 13:21:08 +0000 | [diff] [blame] | 213 | unsigned |
| 214 | X86DarwinTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason, |
| 215 | bool Global) const { |
| 216 | if (Reason == DwarfEncoding::Functions && Global) |
| 217 | return (DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4); |
| 218 | else if (Reason == DwarfEncoding::CodeLabels || !Global) |
| 219 | return DW_EH_PE_pcrel; |
| 220 | else |
| 221 | return DW_EH_PE_absptr; |
| 222 | } |
| 223 | |
Anton Korobeynikov | b9a02fc | 2008-07-09 13:21:29 +0000 | [diff] [blame] | 224 | std::string |
Anton Korobeynikov | 4039e68 | 2008-07-09 13:23:57 +0000 | [diff] [blame] | 225 | X86DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const { |
| 226 | SectionKind::Kind Kind = SectionKindForGlobal(GV); |
| 227 | bool isWeak = GV->hasWeakLinkage() || |
| 228 | GV->hasCommonLinkage() || |
| 229 | GV->hasLinkOnceLinkage(); |
| 230 | |
| 231 | switch (Kind) { |
| 232 | case SectionKind::Text: |
| 233 | if (isWeak) |
| 234 | return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions"; |
| 235 | else |
| 236 | return getTextSection(); |
| 237 | case SectionKind::Data: |
| 238 | case SectionKind::ThreadData: |
| 239 | case SectionKind::BSS: |
| 240 | case SectionKind::ThreadBSS: |
| 241 | if (cast<GlobalVariable>(GV)->isConstant()) { |
| 242 | if (isWeak) |
| 243 | return ".section __DATA,__const_coal,coalesced"; |
| 244 | else |
| 245 | return ".const_data"; |
| 246 | } else { |
| 247 | if (isWeak) |
| 248 | return ".section __DATA,__datacoal_nt,coalesced"; |
| 249 | else |
| 250 | return getDataSection(); |
| 251 | } |
| 252 | case SectionKind::ROData: |
| 253 | if (isWeak) |
| 254 | return ".section __DATA,__const_coal,coalesced"; |
| 255 | else |
| 256 | return getReadOnlySection(); |
| 257 | case SectionKind::RODataMergeStr: |
| 258 | return MergeableStringSection(cast<GlobalVariable>(GV)); |
| 259 | case SectionKind::RODataMergeConst: |
| 260 | return MergeableConstSection(cast<GlobalVariable>(GV)); |
| 261 | default: |
| 262 | assert(0 && "Unsuported section kind for global"); |
| 263 | } |
| 264 | |
| 265 | // FIXME: Do we have any extra special weird cases? |
| 266 | } |
| 267 | |
| 268 | std::string |
| 269 | X86DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const { |
| 270 | unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str()); |
| 271 | unsigned Size = SectionFlags::getEntitySize(Flags); |
| 272 | |
| 273 | if (Size) { |
| 274 | const TargetData *TD = X86TM->getTargetData(); |
| 275 | unsigned Align = TD->getPreferredAlignment(GV); |
| 276 | if (Align <= 32) |
| 277 | return getCStringSection(); |
| 278 | } |
| 279 | |
| 280 | return getReadOnlySection(); |
| 281 | } |
| 282 | |
| 283 | std::string |
| 284 | X86DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const { |
| 285 | unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str()); |
| 286 | unsigned Size = SectionFlags::getEntitySize(Flags); |
| 287 | |
| 288 | if (Size == 4) |
| 289 | return FourByteConstantSection; |
| 290 | else if (Size == 8) |
| 291 | return EightByteConstantSection; |
| 292 | else if (Size == 16 && X86TM->getSubtarget<X86Subtarget>().is64Bit()) |
| 293 | return SixteenByteConstantSection; |
| 294 | |
| 295 | return getReadOnlySection(); |
| 296 | } |
| 297 | |
| 298 | std::string |
Anton Korobeynikov | b9a02fc | 2008-07-09 13:21:29 +0000 | [diff] [blame] | 299 | X86DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV, |
| 300 | SectionKind::Kind kind) const { |
Anton Korobeynikov | 4039e68 | 2008-07-09 13:23:57 +0000 | [diff] [blame] | 301 | assert(0 && "Darwin does not use unique sections"); |
| 302 | return ""; |
| 303 | } |
| 304 | |
| 305 | unsigned |
| 306 | X86DarwinTargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV, |
| 307 | const char* name) const { |
| 308 | unsigned Flags = |
| 309 | TargetAsmInfo::SectionFlagsForGlobal(GV, |
| 310 | GV->getSection().c_str()); |
| 311 | |
| 312 | // If there was decision to put stuff into mergeable section - calculate |
| 313 | // entity size |
| 314 | if (Flags & SectionFlags::Mergeable) { |
| 315 | const TargetData *TD = X86TM->getTargetData(); |
| 316 | Constant *C = cast<GlobalVariable>(GV)->getInitializer(); |
| 317 | const Type *Type; |
| 318 | |
| 319 | if (Flags & SectionFlags::Strings) { |
| 320 | const ConstantArray *CVA = cast<ConstantArray>(C); |
| 321 | Type = CVA->getType()->getElementType(); |
| 322 | } else |
| 323 | Type = C->getType(); |
| 324 | |
| 325 | unsigned Size = TD->getABITypeSize(Type); |
| 326 | if (Size > 16) |
| 327 | Size = 0; |
| 328 | Flags = SectionFlags::setEntitySize(Flags, Size); |
| 329 | } |
| 330 | |
| 331 | return Flags; |
Anton Korobeynikov | b9a02fc | 2008-07-09 13:21:29 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Anton Korobeynikov | 4468b7a | 2008-07-09 13:20:48 +0000 | [diff] [blame] | 334 | X86ELFTargetAsmInfo::X86ELFTargetAsmInfo(const X86TargetMachine &TM): |
| 335 | X86TargetAsmInfo(TM) { |
| 336 | bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit(); |
| 337 | |
| 338 | ReadOnlySection = "\t.section\t.rodata"; |
| 339 | FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4"; |
| 340 | EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8"; |
| 341 | SixteenByteConstantSection = "\t.section\t.rodata.cst16,\"aM\",@progbits,16"; |
| 342 | CStringSection = "\t.section\t.rodata.str1.1,\"aMS\",@progbits,1"; |
| 343 | PrivateGlobalPrefix = ".L"; |
| 344 | WeakRefDirective = "\t.weak\t"; |
| 345 | SetDirective = "\t.set\t"; |
| 346 | PCSymbol = "."; |
| 347 | |
| 348 | // Set up DWARF directives |
| 349 | HasLEB128 = true; // Target asm supports leb128 directives (little-endian) |
| 350 | |
| 351 | // Debug Information |
| 352 | AbsoluteDebugSectionOffsets = true; |
| 353 | SupportsDebugInformation = true; |
| 354 | DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits"; |
| 355 | DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits"; |
| 356 | DwarfLineSection = "\t.section\t.debug_line,\"\",@progbits"; |
| 357 | DwarfFrameSection = "\t.section\t.debug_frame,\"\",@progbits"; |
| 358 | DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits"; |
| 359 | DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits"; |
| 360 | DwarfStrSection = "\t.section\t.debug_str,\"\",@progbits"; |
| 361 | DwarfLocSection = "\t.section\t.debug_loc,\"\",@progbits"; |
| 362 | DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits"; |
| 363 | DwarfRangesSection = "\t.section\t.debug_ranges,\"\",@progbits"; |
| 364 | DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits"; |
| 365 | |
| 366 | // Exceptions handling |
| 367 | if (!is64Bit) |
| 368 | SupportsExceptionHandling = true; |
| 369 | AbsoluteEHSectionOffsets = false; |
| 370 | DwarfEHFrameSection = "\t.section\t.eh_frame,\"aw\",@progbits"; |
| 371 | DwarfExceptionSection = "\t.section\t.gcc_except_table,\"a\",@progbits"; |
| 372 | |
| 373 | // On Linux we must declare when we can use a non-executable stack. |
| 374 | if (X86TM->getSubtarget<X86Subtarget>().isLinux()) |
| 375 | NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits"; |
| 376 | } |
| 377 | |
Anton Korobeynikov | b9e58ef | 2008-07-09 13:21:08 +0000 | [diff] [blame] | 378 | unsigned |
| 379 | X86ELFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason, |
| 380 | bool Global) const { |
| 381 | CodeModel::Model CM = X86TM->getCodeModel(); |
| 382 | bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit(); |
| 383 | |
| 384 | if (X86TM->getRelocationModel() == Reloc::PIC_) { |
| 385 | unsigned Format = 0; |
| 386 | |
| 387 | if (!is64Bit) |
| 388 | // 32 bit targets always encode pointers as 4 bytes |
| 389 | Format = DW_EH_PE_sdata4; |
| 390 | else { |
| 391 | // 64 bit targets encode pointers in 4 bytes iff: |
| 392 | // - code model is small OR |
| 393 | // - code model is medium and we're emitting externally visible symbols |
| 394 | // or any code symbols |
| 395 | if (CM == CodeModel::Small || |
| 396 | (CM == CodeModel::Medium && (Global || |
| 397 | Reason != DwarfEncoding::Data))) |
| 398 | Format = DW_EH_PE_sdata4; |
| 399 | else |
| 400 | Format = DW_EH_PE_sdata8; |
| 401 | } |
| 402 | |
| 403 | if (Global) |
| 404 | Format |= DW_EH_PE_indirect; |
| 405 | |
| 406 | return (Format | DW_EH_PE_pcrel); |
| 407 | } else { |
| 408 | if (is64Bit && |
| 409 | (CM == CodeModel::Small || |
| 410 | (CM == CodeModel::Medium && Reason != DwarfEncoding::Data))) |
| 411 | return DW_EH_PE_udata4; |
| 412 | else |
| 413 | return DW_EH_PE_absptr; |
| 414 | } |
| 415 | } |
| 416 | |
Anton Korobeynikov | 4260ad3 | 2008-07-09 13:23:08 +0000 | [diff] [blame] | 417 | std::string |
| 418 | X86ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const { |
| 419 | SectionKind::Kind kind = SectionKindForGlobal(GV); |
| 420 | |
| 421 | if (const Function *F = dyn_cast<Function>(GV)) { |
| 422 | switch (F->getLinkage()) { |
| 423 | default: assert(0 && "Unknown linkage type!"); |
| 424 | case Function::InternalLinkage: |
| 425 | case Function::DLLExportLinkage: |
| 426 | case Function::ExternalLinkage: |
| 427 | return getTextSection(); |
| 428 | case Function::WeakLinkage: |
| 429 | case Function::LinkOnceLinkage: |
| 430 | return UniqueSectionForGlobal(F, kind); |
| 431 | } |
| 432 | } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) { |
| 433 | if (GVar->hasCommonLinkage() || |
| 434 | GVar->hasLinkOnceLinkage() || |
| 435 | GVar->hasWeakLinkage()) |
| 436 | return UniqueSectionForGlobal(GVar, kind); |
| 437 | else { |
| 438 | switch (kind) { |
| 439 | case SectionKind::Data: |
| 440 | return getDataSection(); |
| 441 | case SectionKind::BSS: |
| 442 | // ELF targets usually have BSS sections |
| 443 | return getBSSSection(); |
| 444 | case SectionKind::ROData: |
Anton Korobeynikov | 4260ad3 | 2008-07-09 13:23:08 +0000 | [diff] [blame] | 445 | return getReadOnlySection(); |
Anton Korobeynikov | 01b0e24 | 2008-07-09 13:23:37 +0000 | [diff] [blame] | 446 | case SectionKind::RODataMergeStr: |
| 447 | return MergeableStringSection(GVar); |
| 448 | case SectionKind::RODataMergeConst: |
| 449 | return MergeableConstSection(GVar); |
Anton Korobeynikov | 4260ad3 | 2008-07-09 13:23:08 +0000 | [diff] [blame] | 450 | case SectionKind::ThreadData: |
| 451 | // ELF targets usually support TLS stuff |
| 452 | return getTLSDataSection(); |
| 453 | case SectionKind::ThreadBSS: |
| 454 | return getTLSBSSSection(); |
| 455 | default: |
| 456 | assert(0 && "Unsuported section kind for global"); |
| 457 | } |
| 458 | } |
| 459 | } else |
| 460 | assert(0 && "Unsupported global"); |
| 461 | } |
| 462 | |
Anton Korobeynikov | 01b0e24 | 2008-07-09 13:23:37 +0000 | [diff] [blame] | 463 | std::string |
| 464 | X86ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const { |
| 465 | unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str()); |
| 466 | unsigned Size = SectionFlags::getEntitySize(Flags); |
| 467 | |
| 468 | // FIXME: string here is temporary, until stuff will fully land in. |
Anton Korobeynikov | 4039e68 | 2008-07-09 13:23:57 +0000 | [diff] [blame] | 469 | if (Size == 4) |
| 470 | return FourByteConstantSection; |
| 471 | else if (Size == 8) |
| 472 | return EightByteConstantSection; |
| 473 | else if (Size == 16) |
| 474 | return SixteenByteConstantSection; |
| 475 | |
| 476 | return getReadOnlySection(); |
Anton Korobeynikov | 01b0e24 | 2008-07-09 13:23:37 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | std::string |
| 480 | X86ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const { |
| 481 | unsigned Flags = SectionFlagsForGlobal(GV, GV->getSection().c_str()); |
| 482 | unsigned Size = SectionFlags::getEntitySize(Flags); |
| 483 | |
| 484 | if (Size) { |
| 485 | // We also need alignment here |
| 486 | const TargetData *TD = X86TM->getTargetData(); |
| 487 | unsigned Align = TD->getPreferredAlignment(GV); |
| 488 | if (Align < Size) |
| 489 | Align = Size; |
| 490 | |
Anton Korobeynikov | 4039e68 | 2008-07-09 13:23:57 +0000 | [diff] [blame] | 491 | return getCStringSection() + utostr(Size) + ',' + utostr(Align); |
| 492 | } |
| 493 | |
| 494 | return getReadOnlySection(); |
Anton Korobeynikov | 01b0e24 | 2008-07-09 13:23:37 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | unsigned |
| 498 | X86ELFTargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV, |
| 499 | const char* name) const { |
| 500 | unsigned Flags = |
| 501 | TargetAsmInfo::SectionFlagsForGlobal(GV, |
| 502 | GV->getSection().c_str()); |
| 503 | |
| 504 | // If there was decision to put stuff into mergeable section - calculate |
| 505 | // entity size |
| 506 | if (Flags & SectionFlags::Mergeable) { |
| 507 | const TargetData *TD = X86TM->getTargetData(); |
| 508 | Constant *C = cast<GlobalVariable>(GV)->getInitializer(); |
| 509 | const Type *Type; |
| 510 | |
| 511 | if (Flags & SectionFlags::Strings) { |
| 512 | const ConstantArray *CVA = cast<ConstantArray>(C); |
| 513 | Type = CVA->getType()->getElementType(); |
| 514 | } else |
| 515 | Type = C->getType(); |
| 516 | |
| 517 | unsigned Size = TD->getABITypeSize(Type); |
Anton Korobeynikov | 4039e68 | 2008-07-09 13:23:57 +0000 | [diff] [blame] | 518 | if (Size > 16) |
Anton Korobeynikov | 01b0e24 | 2008-07-09 13:23:37 +0000 | [diff] [blame] | 519 | Size = 0; |
| 520 | Flags = SectionFlags::setEntitySize(Flags, Size); |
| 521 | } |
| 522 | |
| 523 | return Flags; |
| 524 | } |
| 525 | |
| 526 | |
Anton Korobeynikov | f447e3d | 2008-07-09 13:21:49 +0000 | [diff] [blame] | 527 | std::string X86ELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const { |
| 528 | std::string Flags = ",\""; |
| 529 | |
| 530 | if (!(flags & SectionFlags::Debug)) |
| 531 | Flags += 'a'; |
| 532 | if (flags & SectionFlags::Code) |
| 533 | Flags += 'x'; |
| 534 | if (flags & SectionFlags::Writeable) |
| 535 | Flags += 'w'; |
| 536 | if (flags & SectionFlags::Mergeable) |
| 537 | Flags += 'M'; |
| 538 | if (flags & SectionFlags::Strings) |
| 539 | Flags += 'S'; |
| 540 | if (flags & SectionFlags::TLS) |
| 541 | Flags += 'T'; |
| 542 | |
| 543 | Flags += "\""; |
| 544 | |
| 545 | // FIXME: There can be exceptions here |
| 546 | if (flags & SectionFlags::BSS) |
| 547 | Flags += ",@nobits"; |
| 548 | else |
| 549 | Flags += ",@progbits"; |
| 550 | |
Anton Korobeynikov | 6d82942 | 2008-07-09 13:22:17 +0000 | [diff] [blame] | 551 | if (unsigned entitySize = SectionFlags::getEntitySize(flags)) |
| 552 | Flags += "," + utostr(entitySize); |
Anton Korobeynikov | f447e3d | 2008-07-09 13:21:49 +0000 | [diff] [blame] | 553 | |
| 554 | return Flags; |
| 555 | } |
| 556 | |
Anton Korobeynikov | 4468b7a | 2008-07-09 13:20:48 +0000 | [diff] [blame] | 557 | X86COFFTargetAsmInfo::X86COFFTargetAsmInfo(const X86TargetMachine &TM): |
| 558 | X86TargetAsmInfo(TM) { |
| 559 | GlobalPrefix = "_"; |
| 560 | LCOMMDirective = "\t.lcomm\t"; |
| 561 | COMMDirectiveTakesAlignment = false; |
| 562 | HasDotTypeDotSizeDirective = false; |
| 563 | StaticCtorsSection = "\t.section .ctors,\"aw\""; |
| 564 | StaticDtorsSection = "\t.section .dtors,\"aw\""; |
| 565 | HiddenDirective = NULL; |
| 566 | PrivateGlobalPrefix = "L"; // Prefix for private global symbols |
| 567 | WeakRefDirective = "\t.weak\t"; |
| 568 | SetDirective = "\t.set\t"; |
| 569 | |
| 570 | // Set up DWARF directives |
| 571 | HasLEB128 = true; // Target asm supports leb128 directives (little-endian) |
| 572 | AbsoluteDebugSectionOffsets = true; |
| 573 | AbsoluteEHSectionOffsets = false; |
| 574 | SupportsDebugInformation = true; |
| 575 | DwarfSectionOffsetDirective = "\t.secrel32\t"; |
| 576 | DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\""; |
| 577 | DwarfInfoSection = "\t.section\t.debug_info,\"dr\""; |
| 578 | DwarfLineSection = "\t.section\t.debug_line,\"dr\""; |
| 579 | DwarfFrameSection = "\t.section\t.debug_frame,\"dr\""; |
| 580 | DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\""; |
| 581 | DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\""; |
| 582 | DwarfStrSection = "\t.section\t.debug_str,\"dr\""; |
| 583 | DwarfLocSection = "\t.section\t.debug_loc,\"dr\""; |
| 584 | DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\""; |
| 585 | DwarfRangesSection = "\t.section\t.debug_ranges,\"dr\""; |
| 586 | DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\""; |
| 587 | } |
| 588 | |
Anton Korobeynikov | b9e58ef | 2008-07-09 13:21:08 +0000 | [diff] [blame] | 589 | unsigned |
| 590 | X86COFFTargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason, |
| 591 | bool Global) const { |
| 592 | CodeModel::Model CM = X86TM->getCodeModel(); |
| 593 | bool is64Bit = X86TM->getSubtarget<X86Subtarget>().is64Bit(); |
Anton Korobeynikov | cee750f | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 594 | |
Anton Korobeynikov | b9e58ef | 2008-07-09 13:21:08 +0000 | [diff] [blame] | 595 | if (X86TM->getRelocationModel() == Reloc::PIC_) { |
| 596 | unsigned Format = 0; |
Anton Korobeynikov | cee750f | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 597 | |
Anton Korobeynikov | b9e58ef | 2008-07-09 13:21:08 +0000 | [diff] [blame] | 598 | if (!is64Bit) |
| 599 | // 32 bit targets always encode pointers as 4 bytes |
| 600 | Format = DW_EH_PE_sdata4; |
| 601 | else { |
| 602 | // 64 bit targets encode pointers in 4 bytes iff: |
| 603 | // - code model is small OR |
| 604 | // - code model is medium and we're emitting externally visible symbols |
| 605 | // or any code symbols |
| 606 | if (CM == CodeModel::Small || |
| 607 | (CM == CodeModel::Medium && (Global || |
| 608 | Reason != DwarfEncoding::Data))) |
Anton Korobeynikov | cee750f | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 609 | Format = DW_EH_PE_sdata4; |
Anton Korobeynikov | cee750f | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 610 | else |
Anton Korobeynikov | b9e58ef | 2008-07-09 13:21:08 +0000 | [diff] [blame] | 611 | Format = DW_EH_PE_sdata8; |
Anton Korobeynikov | cee750f | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 612 | } |
Anton Korobeynikov | cee750f | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 613 | |
Anton Korobeynikov | b9e58ef | 2008-07-09 13:21:08 +0000 | [diff] [blame] | 614 | if (Global) |
| 615 | Format |= DW_EH_PE_indirect; |
| 616 | |
| 617 | return (Format | DW_EH_PE_pcrel); |
| 618 | } else { |
| 619 | if (is64Bit && |
| 620 | (CM == CodeModel::Small || |
| 621 | (CM == CodeModel::Medium && Reason != DwarfEncoding::Data))) |
| 622 | return DW_EH_PE_udata4; |
| 623 | else |
| 624 | return DW_EH_PE_absptr; |
Anton Korobeynikov | cee750f | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
Anton Korobeynikov | b9a02fc | 2008-07-09 13:21:29 +0000 | [diff] [blame] | 628 | std::string |
| 629 | X86COFFTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV, |
| 630 | SectionKind::Kind kind) const { |
| 631 | switch (kind) { |
| 632 | case SectionKind::Text: |
| 633 | return ".text$linkonce" + GV->getName(); |
| 634 | case SectionKind::Data: |
| 635 | case SectionKind::BSS: |
| 636 | case SectionKind::ThreadData: |
| 637 | case SectionKind::ThreadBSS: |
| 638 | return ".data$linkonce" + GV->getName(); |
| 639 | case SectionKind::ROData: |
| 640 | case SectionKind::RODataMergeConst: |
| 641 | case SectionKind::RODataMergeStr: |
| 642 | return ".rdata$linkonce" + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 643 | default: |
Anton Korobeynikov | b9a02fc | 2008-07-09 13:21:29 +0000 | [diff] [blame] | 644 | assert(0 && "Unknown section kind"); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
Anton Korobeynikov | f447e3d | 2008-07-09 13:21:49 +0000 | [diff] [blame] | 648 | std::string X86COFFTargetAsmInfo::PrintSectionFlags(unsigned flags) const { |
| 649 | std::string Flags = ",\""; |
| 650 | |
| 651 | if (flags & SectionFlags::Code) |
| 652 | Flags += 'x'; |
| 653 | if (flags & SectionFlags::Writeable) |
| 654 | Flags += 'w'; |
| 655 | |
| 656 | Flags += "\""; |
| 657 | |
| 658 | return Flags; |
| 659 | } |
| 660 | |
| 661 | X86WinTargetAsmInfo::X86WinTargetAsmInfo(const X86TargetMachine &TM): |
| 662 | X86TargetAsmInfo(TM) { |
| 663 | GlobalPrefix = "_"; |
| 664 | CommentString = ";"; |
| 665 | |
| 666 | PrivateGlobalPrefix = "$"; |
| 667 | AlignDirective = "\talign\t"; |
| 668 | ZeroDirective = "\tdb\t"; |
| 669 | ZeroDirectiveSuffix = " dup(0)"; |
| 670 | AsciiDirective = "\tdb\t"; |
| 671 | AscizDirective = 0; |
| 672 | Data8bitsDirective = "\tdb\t"; |
| 673 | Data16bitsDirective = "\tdw\t"; |
| 674 | Data32bitsDirective = "\tdd\t"; |
| 675 | Data64bitsDirective = "\tdq\t"; |
| 676 | HasDotTypeDotSizeDirective = false; |
| 677 | |
| 678 | TextSection = "_text"; |
| 679 | DataSection = "_data"; |
| 680 | JumpTableDataSection = NULL; |
| 681 | SwitchToSectionDirective = ""; |
| 682 | TextSectionStartSuffix = "\tsegment 'CODE'"; |
| 683 | DataSectionStartSuffix = "\tsegment 'DATA'"; |
| 684 | SectionEndDirectiveSuffix = "\tends\n"; |
| 685 | } |