Jim Laskey | 7c95ad4 | 2006-09-06 19:21:41 +0000 | [diff] [blame] | 1 | //===-- TargetAsmInfo.cpp - Asm Info ---------------------------------------==// |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 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 | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines target asm properties related what form asm statements |
| 11 | // should take. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Anton Korobeynikov | 72bb402 | 2009-01-27 22:29:24 +0000 | [diff] [blame] | 16 | #include "llvm/DerivedTypes.h" |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 17 | #include "llvm/GlobalVariable.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Module.h" |
| 20 | #include "llvm/Type.h" |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetAsmInfo.h" |
Anton Korobeynikov | db23231 | 2009-03-29 17:13:49 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetMachine.h" |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetOptions.h" |
Anton Korobeynikov | cee750f | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Dwarf.h" |
Dale Johannesen | 3bb6283 | 2007-04-23 20:00:17 +0000 | [diff] [blame] | 25 | #include <cctype> |
| 26 | #include <cstring> |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chris Lattner | 4e0f25b | 2009-06-19 00:08:39 +0000 | [diff] [blame] | 29 | TargetAsmInfo::TargetAsmInfo(const TargetMachine &tm) |
| 30 | : TM(tm) { |
Anton Korobeynikov | 32b952a | 2008-09-25 21:00:33 +0000 | [diff] [blame] | 31 | BSSSection = "\t.bss"; |
| 32 | BSSSection_ = 0; |
| 33 | ReadOnlySection = 0; |
| 34 | SmallDataSection = 0; |
| 35 | SmallBSSSection = 0; |
| 36 | SmallRODataSection = 0; |
| 37 | TLSDataSection = 0; |
| 38 | TLSBSSSection = 0; |
| 39 | ZeroFillDirective = 0; |
| 40 | NonexecutableStackDirective = 0; |
| 41 | NeedsSet = false; |
| 42 | MaxInstLength = 4; |
| 43 | PCSymbol = "$"; |
| 44 | SeparatorChar = ';'; |
| 45 | CommentString = "#"; |
| 46 | GlobalPrefix = ""; |
| 47 | PrivateGlobalPrefix = "."; |
| 48 | LessPrivateGlobalPrefix = ""; |
| 49 | JumpTableSpecialLabelPrefix = 0; |
| 50 | GlobalVarAddrPrefix = ""; |
| 51 | GlobalVarAddrSuffix = ""; |
| 52 | FunctionAddrPrefix = ""; |
| 53 | FunctionAddrSuffix = ""; |
| 54 | PersonalityPrefix = ""; |
| 55 | PersonalitySuffix = ""; |
| 56 | NeedsIndirectEncoding = false; |
| 57 | InlineAsmStart = "#APP"; |
| 58 | InlineAsmEnd = "#NO_APP"; |
| 59 | AssemblerDialect = 0; |
| 60 | StringConstantPrefix = ".str"; |
Chris Lattner | a93ca92 | 2009-06-18 23:41:35 +0000 | [diff] [blame] | 61 | AllowQuotesInName = false; |
Anton Korobeynikov | 32b952a | 2008-09-25 21:00:33 +0000 | [diff] [blame] | 62 | ZeroDirective = "\t.zero\t"; |
| 63 | ZeroDirectiveSuffix = 0; |
| 64 | AsciiDirective = "\t.ascii\t"; |
| 65 | AscizDirective = "\t.asciz\t"; |
| 66 | Data8bitsDirective = "\t.byte\t"; |
| 67 | Data16bitsDirective = "\t.short\t"; |
| 68 | Data32bitsDirective = "\t.long\t"; |
| 69 | Data64bitsDirective = "\t.quad\t"; |
| 70 | AlignDirective = "\t.align\t"; |
| 71 | AlignmentIsInBytes = true; |
| 72 | TextAlignFillValue = 0; |
| 73 | SwitchToSectionDirective = "\t.section\t"; |
| 74 | TextSectionStartSuffix = ""; |
| 75 | DataSectionStartSuffix = ""; |
| 76 | SectionEndDirectiveSuffix = 0; |
| 77 | ConstantPoolSection = "\t.section .rodata"; |
| 78 | JumpTableDataSection = "\t.section .rodata"; |
| 79 | JumpTableDirective = 0; |
| 80 | CStringSection = 0; |
| 81 | CStringSection_ = 0; |
| 82 | // FIXME: Flags are ELFish - replace with normal section stuff. |
| 83 | StaticCtorsSection = "\t.section .ctors,\"aw\",@progbits"; |
| 84 | StaticDtorsSection = "\t.section .dtors,\"aw\",@progbits"; |
| 85 | GlobalDirective = "\t.globl\t"; |
| 86 | SetDirective = 0; |
| 87 | LCOMMDirective = 0; |
| 88 | COMMDirective = "\t.comm\t"; |
| 89 | COMMDirectiveTakesAlignment = true; |
| 90 | HasDotTypeDotSizeDirective = true; |
Rafael Espindola | 952b839 | 2008-12-03 11:01:37 +0000 | [diff] [blame] | 91 | HasSingleParameterDotFile = true; |
Anton Korobeynikov | 32b952a | 2008-09-25 21:00:33 +0000 | [diff] [blame] | 92 | UsedDirective = 0; |
| 93 | WeakRefDirective = 0; |
| 94 | WeakDefDirective = 0; |
| 95 | // FIXME: These are ELFish - move to ELFTAI. |
| 96 | HiddenDirective = "\t.hidden\t"; |
| 97 | ProtectedDirective = "\t.protected\t"; |
| 98 | AbsoluteDebugSectionOffsets = false; |
| 99 | AbsoluteEHSectionOffsets = false; |
| 100 | HasLEB128 = false; |
| 101 | HasDotLocAndDotFile = false; |
| 102 | SupportsDebugInformation = false; |
| 103 | SupportsExceptionHandling = false; |
| 104 | DwarfRequiresFrameSection = true; |
Devang Patel | 0f7fef3 | 2009-04-13 17:02:03 +0000 | [diff] [blame] | 105 | DwarfUsesInlineInfoSection = false; |
Bill Wendling | 722f5f1 | 2008-12-24 08:05:17 +0000 | [diff] [blame] | 106 | NonLocalEHFrameLabel = false; |
Anton Korobeynikov | 32b952a | 2008-09-25 21:00:33 +0000 | [diff] [blame] | 107 | GlobalEHDirective = 0; |
| 108 | SupportsWeakOmittedEHFrame = true; |
| 109 | DwarfSectionOffsetDirective = 0; |
| 110 | DwarfAbbrevSection = ".debug_abbrev"; |
| 111 | DwarfInfoSection = ".debug_info"; |
| 112 | DwarfLineSection = ".debug_line"; |
| 113 | DwarfFrameSection = ".debug_frame"; |
| 114 | DwarfPubNamesSection = ".debug_pubnames"; |
| 115 | DwarfPubTypesSection = ".debug_pubtypes"; |
Devang Patel | 0f7fef3 | 2009-04-13 17:02:03 +0000 | [diff] [blame] | 116 | DwarfDebugInlineSection = ".debug_inlined"; |
Anton Korobeynikov | 32b952a | 2008-09-25 21:00:33 +0000 | [diff] [blame] | 117 | DwarfStrSection = ".debug_str"; |
| 118 | DwarfLocSection = ".debug_loc"; |
| 119 | DwarfARangesSection = ".debug_aranges"; |
| 120 | DwarfRangesSection = ".debug_ranges"; |
Chris Lattner | b839c3f | 2009-06-18 23:31:37 +0000 | [diff] [blame] | 121 | DwarfMacroInfoSection = ".debug_macinfo"; |
Anton Korobeynikov | 32b952a | 2008-09-25 21:00:33 +0000 | [diff] [blame] | 122 | DwarfEHFrameSection = ".eh_frame"; |
| 123 | DwarfExceptionSection = ".gcc_except_table"; |
| 124 | AsmTransCBE = 0; |
Anton Korobeynikov | d7ca416 | 2008-09-24 22:15:21 +0000 | [diff] [blame] | 125 | TextSection = getUnnamedSection("\t.text", SectionFlags::Code); |
Anton Korobeynikov | 315690e | 2008-09-24 22:16:16 +0000 | [diff] [blame] | 126 | DataSection = getUnnamedSection("\t.data", SectionFlags::Writeable); |
Chris Lattner | 4c7b07a | 2006-10-13 17:50:07 +0000 | [diff] [blame] | 127 | } |
Chris Lattner | f5b10ec | 2006-10-05 00:35:16 +0000 | [diff] [blame] | 128 | |
| 129 | TargetAsmInfo::~TargetAsmInfo() { |
| 130 | } |
Chris Lattner | 4c7b07a | 2006-10-13 17:50:07 +0000 | [diff] [blame] | 131 | |
| 132 | /// Measure the specified inline asm to determine an approximation of its |
| 133 | /// length. |
Dale Johannesen | 3bb6283 | 2007-04-23 20:00:17 +0000 | [diff] [blame] | 134 | /// Comments (which run till the next SeparatorChar or newline) do not |
| 135 | /// count as an instruction. |
| 136 | /// Any other non-whitespace text is considered an instruction, with |
| 137 | /// multiple instructions separated by SeparatorChar or newlines. |
| 138 | /// Variable-length instructions are not handled here; this function |
| 139 | /// may be overloaded in the target code to do that. |
Chris Lattner | 4c7b07a | 2006-10-13 17:50:07 +0000 | [diff] [blame] | 140 | unsigned TargetAsmInfo::getInlineAsmLength(const char *Str) const { |
| 141 | // Count the number of instructions in the asm. |
Dale Johannesen | 3bb6283 | 2007-04-23 20:00:17 +0000 | [diff] [blame] | 142 | bool atInsnStart = true; |
| 143 | unsigned Length = 0; |
Chris Lattner | 4c7b07a | 2006-10-13 17:50:07 +0000 | [diff] [blame] | 144 | for (; *Str; ++Str) { |
| 145 | if (*Str == '\n' || *Str == SeparatorChar) |
Dale Johannesen | 3bb6283 | 2007-04-23 20:00:17 +0000 | [diff] [blame] | 146 | atInsnStart = true; |
| 147 | if (atInsnStart && !isspace(*Str)) { |
| 148 | Length += MaxInstLength; |
| 149 | atInsnStart = false; |
| 150 | } |
| 151 | if (atInsnStart && strncmp(Str, CommentString, strlen(CommentString))==0) |
| 152 | atInsnStart = false; |
Chris Lattner | 4c7b07a | 2006-10-13 17:50:07 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Dale Johannesen | 3bb6283 | 2007-04-23 20:00:17 +0000 | [diff] [blame] | 155 | return Length; |
Chris Lattner | 4c7b07a | 2006-10-13 17:50:07 +0000 | [diff] [blame] | 156 | } |
Anton Korobeynikov | a6199c8 | 2007-03-07 02:47:57 +0000 | [diff] [blame] | 157 | |
Anton Korobeynikov | 8213f9c | 2008-02-29 22:09:08 +0000 | [diff] [blame] | 158 | unsigned TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason, |
Anton Korobeynikov | cee750f | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 159 | bool Global) const { |
| 160 | return dwarf::DW_EH_PE_absptr; |
| 161 | } |
| 162 | |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 163 | static bool isSuitableForBSS(const GlobalVariable *GV) { |
| 164 | if (!GV->hasInitializer()) |
| 165 | return true; |
| 166 | |
| 167 | // Leave constant zeros in readonly constant sections, so they can be shared |
| 168 | Constant *C = GV->getInitializer(); |
| 169 | return (C->isNullValue() && !GV->isConstant() && !NoZerosInBSS); |
| 170 | } |
| 171 | |
Anton Korobeynikov | 72bb402 | 2009-01-27 22:29:24 +0000 | [diff] [blame] | 172 | static bool isConstantString(const Constant *C) { |
| 173 | // First check: is we have constant array of i8 terminated with zero |
| 174 | const ConstantArray *CVA = dyn_cast<ConstantArray>(C); |
| 175 | // Check, if initializer is a null-terminated string |
| 176 | if (CVA && CVA->isCString()) |
| 177 | return true; |
| 178 | |
| 179 | // Another possibility: [1 x i8] zeroinitializer |
| 180 | if (isa<ConstantAggregateZero>(C)) { |
| 181 | if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType())) { |
| 182 | return (Ty->getElementType() == Type::Int8Ty && |
| 183 | Ty->getNumElements() == 1); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return false; |
| 188 | } |
| 189 | |
Anton Korobeynikov | db23231 | 2009-03-29 17:13:49 +0000 | [diff] [blame] | 190 | unsigned TargetAsmInfo::RelocBehaviour() const { |
| 191 | // By default - all relocations in PIC mode would force symbol to be |
| 192 | // placed in r/w section. |
| 193 | return (TM.getRelocationModel() != Reloc::Static ? |
| 194 | Reloc::LocalOrGlobal : Reloc::None); |
| 195 | } |
Anton Korobeynikov | 72bb402 | 2009-01-27 22:29:24 +0000 | [diff] [blame] | 196 | |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 197 | SectionKind::Kind |
| 198 | TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const { |
| 199 | // Early exit - functions should be always in text sections. |
| 200 | if (isa<Function>(GV)) |
| 201 | return SectionKind::Text; |
| 202 | |
| 203 | const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV); |
| 204 | bool isThreadLocal = GVar->isThreadLocal(); |
| 205 | assert(GVar && "Invalid global value for section selection"); |
| 206 | |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 207 | if (isSuitableForBSS(GVar)) { |
| 208 | // Variable can be easily put to BSS section. |
| 209 | return (isThreadLocal ? SectionKind::ThreadBSS : SectionKind::BSS); |
| 210 | } else if (GVar->isConstant() && !isThreadLocal) { |
| 211 | // Now we know, that varible has initializer and it is constant. We need to |
| 212 | // check its initializer to decide, which section to output it into. Also |
| 213 | // note, there is no thread-local r/o section. |
| 214 | Constant *C = GVar->getInitializer(); |
Anton Korobeynikov | db23231 | 2009-03-29 17:13:49 +0000 | [diff] [blame] | 215 | if (C->ContainsRelocations(Reloc::LocalOrGlobal)) { |
| 216 | // Decide, whether it is still possible to put symbol into r/o section. |
| 217 | unsigned Reloc = RelocBehaviour(); |
| 218 | |
| 219 | // We already did a query for 'all' relocs, thus - early exits. |
Anton Korobeynikov | fca82de | 2009-03-30 15:27:43 +0000 | [diff] [blame] | 220 | if (Reloc == Reloc::LocalOrGlobal) |
| 221 | return SectionKind::Data; |
| 222 | else if (Reloc == Reloc::None) |
Anton Korobeynikov | db23231 | 2009-03-29 17:13:49 +0000 | [diff] [blame] | 223 | return SectionKind::ROData; |
| 224 | else { |
| 225 | // Ok, target wants something funny. Honour it. |
Anton Korobeynikov | fca82de | 2009-03-30 15:27:43 +0000 | [diff] [blame] | 226 | return (C->ContainsRelocations(Reloc) ? |
| 227 | SectionKind::Data : SectionKind::ROData); |
Anton Korobeynikov | db23231 | 2009-03-29 17:13:49 +0000 | [diff] [blame] | 228 | } |
| 229 | } else { |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 230 | // Check, if initializer is a null-terminated string |
Anton Korobeynikov | 72bb402 | 2009-01-27 22:29:24 +0000 | [diff] [blame] | 231 | if (isConstantString(C)) |
Anton Korobeynikov | 0e48a0c | 2008-07-09 13:24:38 +0000 | [diff] [blame] | 232 | return SectionKind::RODataMergeStr; |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 233 | else |
Anton Korobeynikov | 0e48a0c | 2008-07-09 13:24:38 +0000 | [diff] [blame] | 234 | return SectionKind::RODataMergeConst; |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Anton Korobeynikov | 71a7c6c | 2009-03-30 15:27:03 +0000 | [diff] [blame] | 238 | // Variable either is not constant or thread-local - output to data section. |
Anton Korobeynikov | fca82de | 2009-03-30 15:27:43 +0000 | [diff] [blame] | 239 | return (isThreadLocal ? SectionKind::ThreadData : SectionKind::Data); |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 240 | } |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 241 | |
| 242 | unsigned |
| 243 | TargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV, |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 244 | const char* Name) const { |
| 245 | unsigned Flags = SectionFlags::None; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 246 | |
| 247 | // Decode flags from global itself. |
| 248 | if (GV) { |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 249 | SectionKind::Kind Kind = SectionKindForGlobal(GV); |
| 250 | switch (Kind) { |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 251 | case SectionKind::Text: |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 252 | Flags |= SectionFlags::Code; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 253 | break; |
| 254 | case SectionKind::ThreadData: |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 255 | case SectionKind::ThreadBSS: |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 256 | Flags |= SectionFlags::TLS; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 257 | // FALLS THROUGH |
Anton Korobeynikov | 7d51edf | 2008-07-09 13:29:44 +0000 | [diff] [blame] | 258 | case SectionKind::Data: |
Anton Korobeynikov | 71a7c6c | 2009-03-30 15:27:03 +0000 | [diff] [blame] | 259 | case SectionKind::DataRel: |
| 260 | case SectionKind::DataRelLocal: |
| 261 | case SectionKind::DataRelRO: |
| 262 | case SectionKind::DataRelROLocal: |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 263 | case SectionKind::BSS: |
Anton Korobeynikov | 7d51edf | 2008-07-09 13:29:44 +0000 | [diff] [blame] | 264 | Flags |= SectionFlags::Writeable; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 265 | break; |
| 266 | case SectionKind::ROData: |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 267 | case SectionKind::RODataMergeStr: |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 268 | case SectionKind::RODataMergeConst: |
Anton Korobeynikov | 7d51edf | 2008-07-09 13:29:44 +0000 | [diff] [blame] | 269 | // No additional flags here |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 270 | break; |
Anton Korobeynikov | 04dda73 | 2008-07-22 17:09:59 +0000 | [diff] [blame] | 271 | case SectionKind::SmallData: |
| 272 | case SectionKind::SmallBSS: |
| 273 | Flags |= SectionFlags::Writeable; |
| 274 | // FALLS THROUGH |
| 275 | case SectionKind::SmallROData: |
| 276 | Flags |= SectionFlags::Small; |
| 277 | break; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 278 | default: |
| 279 | assert(0 && "Unexpected section kind!"); |
| 280 | } |
| 281 | |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 282 | if (GV->isWeakForLinker()) |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 283 | Flags |= SectionFlags::Linkonce; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // Add flags from sections, if any. |
Anton Korobeynikov | 2a88917 | 2008-07-09 13:25:46 +0000 | [diff] [blame] | 287 | if (Name && *Name) { |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 288 | Flags |= SectionFlags::Named; |
| 289 | |
| 290 | // Some lame default implementation based on some magic section names. |
| 291 | if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 || |
Anton Korobeynikov | 04dda73 | 2008-07-22 17:09:59 +0000 | [diff] [blame] | 292 | strncmp(Name, ".llvm.linkonce.b.", 17) == 0 || |
| 293 | strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 || |
| 294 | strncmp(Name, ".llvm.linkonce.sb.", 18) == 0) |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 295 | Flags |= SectionFlags::BSS; |
| 296 | else if (strcmp(Name, ".tdata") == 0 || |
| 297 | strncmp(Name, ".tdata.", 7) == 0 || |
| 298 | strncmp(Name, ".gnu.linkonce.td.", 17) == 0 || |
| 299 | strncmp(Name, ".llvm.linkonce.td.", 18) == 0) |
| 300 | Flags |= SectionFlags::TLS; |
| 301 | else if (strcmp(Name, ".tbss") == 0 || |
| 302 | strncmp(Name, ".tbss.", 6) == 0 || |
| 303 | strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 || |
| 304 | strncmp(Name, ".llvm.linkonce.tb.", 18) == 0) |
| 305 | Flags |= SectionFlags::BSS | SectionFlags::TLS; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 308 | return Flags; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 309 | } |
Anton Korobeynikov | 0c60246 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 310 | |
Anton Korobeynikov | c25e1ea | 2008-09-24 22:14:23 +0000 | [diff] [blame] | 311 | const Section* |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 312 | TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const { |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 313 | const Section* S; |
Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 314 | // Select section name |
| 315 | if (GV->hasSection()) { |
| 316 | // Honour section already set, if any |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 317 | unsigned Flags = SectionFlagsForGlobal(GV, |
| 318 | GV->getSection().c_str()); |
| 319 | S = getNamedSection(GV->getSection().c_str(), Flags); |
Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 320 | } else { |
| 321 | // Use default section depending on the 'type' of global |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 322 | S = SelectSectionForGlobal(GV); |
Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Anton Korobeynikov | c25e1ea | 2008-09-24 22:14:23 +0000 | [diff] [blame] | 325 | return S; |
Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | // Lame default implementation. Calculate the section name for global. |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 329 | const Section* |
| 330 | TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const { |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 331 | SectionKind::Kind Kind = SectionKindForGlobal(GV); |
Anton Korobeynikov | 0c60246 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 332 | |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 333 | if (GV->isWeakForLinker()) { |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 334 | std::string Name = UniqueSectionForGlobal(GV, Kind); |
| 335 | unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str()); |
| 336 | return getNamedSection(Name.c_str(), Flags); |
| 337 | } else { |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 338 | if (Kind == SectionKind::Text) |
Anton Korobeynikov | d7ca416 | 2008-09-24 22:15:21 +0000 | [diff] [blame] | 339 | return getTextSection(); |
Anton Korobeynikov | 4578862 | 2008-08-07 09:51:54 +0000 | [diff] [blame] | 340 | else if (isBSS(Kind) && getBSSSection_()) |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 341 | return getBSSSection_(); |
Anton Korobeynikov | 00181a3 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 342 | else if (getReadOnlySection() && SectionKind::isReadOnly(Kind)) |
| 343 | return getReadOnlySection(); |
Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 344 | } |
Anton Korobeynikov | 0c60246 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 345 | |
Anton Korobeynikov | 315690e | 2008-09-24 22:16:16 +0000 | [diff] [blame] | 346 | return getDataSection(); |
Anton Korobeynikov | 0c60246 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 347 | } |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 348 | |
Anton Korobeynikov | 93cacf1 | 2008-08-07 09:50:34 +0000 | [diff] [blame] | 349 | // Lame default implementation. Calculate the section name for machine const. |
| 350 | const Section* |
| 351 | TargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const { |
| 352 | // FIXME: Support data.rel stuff someday |
Anton Korobeynikov | 315690e | 2008-09-24 22:16:16 +0000 | [diff] [blame] | 353 | return getDataSection(); |
Anton Korobeynikov | 93cacf1 | 2008-08-07 09:50:34 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 356 | std::string |
| 357 | TargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV, |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 358 | SectionKind::Kind Kind) const { |
| 359 | switch (Kind) { |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 360 | case SectionKind::Text: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 361 | return ".gnu.linkonce.t." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 362 | case SectionKind::Data: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 363 | return ".gnu.linkonce.d." + GV->getName(); |
Anton Korobeynikov | 71a7c6c | 2009-03-30 15:27:03 +0000 | [diff] [blame] | 364 | case SectionKind::DataRel: |
| 365 | return ".gnu.linkonce.d.rel" + GV->getName(); |
| 366 | case SectionKind::DataRelLocal: |
| 367 | return ".gnu.linkonce.d.rel.local" + GV->getName(); |
| 368 | case SectionKind::DataRelRO: |
| 369 | return ".gnu.linkonce.d.rel.ro" + GV->getName(); |
| 370 | case SectionKind::DataRelROLocal: |
| 371 | return ".gnu.linkonce.d.rel.ro.local" + GV->getName(); |
Anton Korobeynikov | 04dda73 | 2008-07-22 17:09:59 +0000 | [diff] [blame] | 372 | case SectionKind::SmallData: |
| 373 | return ".gnu.linkonce.s." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 374 | case SectionKind::BSS: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 375 | return ".gnu.linkonce.b." + GV->getName(); |
Anton Korobeynikov | 04dda73 | 2008-07-22 17:09:59 +0000 | [diff] [blame] | 376 | case SectionKind::SmallBSS: |
| 377 | return ".gnu.linkonce.sb." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 378 | case SectionKind::ROData: |
| 379 | case SectionKind::RODataMergeConst: |
| 380 | case SectionKind::RODataMergeStr: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 381 | return ".gnu.linkonce.r." + GV->getName(); |
Anton Korobeynikov | 04dda73 | 2008-07-22 17:09:59 +0000 | [diff] [blame] | 382 | case SectionKind::SmallROData: |
| 383 | return ".gnu.linkonce.s2." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 384 | case SectionKind::ThreadData: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 385 | return ".gnu.linkonce.td." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 386 | case SectionKind::ThreadBSS: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 387 | return ".gnu.linkonce.tb." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 388 | default: |
| 389 | assert(0 && "Unknown section kind"); |
| 390 | } |
Devang Patel | 8a84e44 | 2009-01-05 17:31:22 +0000 | [diff] [blame] | 391 | return NULL; |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 392 | } |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 393 | |
| 394 | const Section* |
Anton Korobeynikov | 328da65 | 2008-08-16 12:58:12 +0000 | [diff] [blame] | 395 | TargetAsmInfo::getNamedSection(const char *Name, unsigned Flags, |
| 396 | bool Override) const { |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 397 | Section& S = Sections[Name]; |
| 398 | |
| 399 | // This is newly-created section, set it up properly. |
Anton Korobeynikov | 328da65 | 2008-08-16 12:58:12 +0000 | [diff] [blame] | 400 | if (S.Flags == SectionFlags::Invalid || Override) { |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 401 | S.Flags = Flags | SectionFlags::Named; |
| 402 | S.Name = Name; |
| 403 | } |
| 404 | |
| 405 | return &S; |
| 406 | } |
| 407 | |
| 408 | const Section* |
Anton Korobeynikov | 328da65 | 2008-08-16 12:58:12 +0000 | [diff] [blame] | 409 | TargetAsmInfo::getUnnamedSection(const char *Directive, unsigned Flags, |
| 410 | bool Override) const { |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 411 | Section& S = Sections[Directive]; |
| 412 | |
| 413 | // This is newly-created section, set it up properly. |
Anton Korobeynikov | 328da65 | 2008-08-16 12:58:12 +0000 | [diff] [blame] | 414 | if (S.Flags == SectionFlags::Invalid || Override) { |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 415 | S.Flags = Flags & ~SectionFlags::Named; |
| 416 | S.Name = Directive; |
| 417 | } |
| 418 | |
| 419 | return &S; |
| 420 | } |
Anton Korobeynikov | d0c1e29 | 2008-08-16 12:57:07 +0000 | [diff] [blame] | 421 | |
| 422 | const std::string& |
| 423 | TargetAsmInfo::getSectionFlags(unsigned Flags) const { |
| 424 | SectionFlags::FlagsStringsMapType::iterator I = FlagsStrings.find(Flags); |
| 425 | |
| 426 | // We didn't print these flags yet, print and save them to map. This reduces |
| 427 | // amount of heap trashing due to std::string construction / concatenation. |
| 428 | if (I == FlagsStrings.end()) |
| 429 | I = FlagsStrings.insert(std::make_pair(Flags, |
| 430 | printSectionFlags(Flags))).first; |
| 431 | |
| 432 | return I->second; |
| 433 | } |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 434 | |
| 435 | unsigned TargetAsmInfo::getULEB128Size(unsigned Value) { |
| 436 | unsigned Size = 0; |
| 437 | do { |
| 438 | Value >>= 7; |
| 439 | Size += sizeof(int8_t); |
| 440 | } while (Value); |
| 441 | return Size; |
| 442 | } |
| 443 | |
| 444 | unsigned TargetAsmInfo::getSLEB128Size(int Value) { |
| 445 | unsigned Size = 0; |
| 446 | int Sign = Value >> (8 * sizeof(Value) - 1); |
| 447 | bool IsMore; |
| 448 | |
| 449 | do { |
| 450 | unsigned Byte = Value & 0x7f; |
| 451 | Value >>= 7; |
| 452 | IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; |
| 453 | Size += sizeof(int8_t); |
| 454 | } while (IsMore); |
| 455 | return Size; |
| 456 | } |