Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- TargetAsmInfo.cpp - Asm Info ---------------------------------------==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +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 | b4f658c | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
| 16 | #include "llvm/GlobalVariable.h" |
| 17 | #include "llvm/Function.h" |
| 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/Type.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetAsmInfo.h" |
Anton Korobeynikov | b4f658c | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetOptions.h" |
Anton Korobeynikov | 65c0d87 | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Dwarf.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | #include <cctype> |
| 24 | #include <cstring> |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | TargetAsmInfo::TargetAsmInfo() : |
| 29 | TextSection("\t.text"), |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 30 | TextSection_(0), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 31 | DataSection("\t.data"), |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 32 | DataSection_(0), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 33 | BSSSection("\t.bss"), |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 34 | BSSSection_(0), |
| 35 | ReadOnlySection(0), |
| 36 | ReadOnlySection_(0), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 37 | TLSDataSection("\t.section .tdata,\"awT\",@progbits"), |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 38 | TLSDataSection_(0), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 39 | TLSBSSSection("\t.section .tbss,\"awT\",@nobits"), |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 40 | TLSBSSSection_(0), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 41 | ZeroFillDirective(0), |
Dan Gohman | a65530a | 2008-05-05 00:28:39 +0000 | [diff] [blame] | 42 | NonexecutableStackDirective(0), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 43 | NeedsSet(false), |
| 44 | MaxInstLength(4), |
| 45 | PCSymbol("$"), |
| 46 | SeparatorChar(';'), |
| 47 | CommentString("#"), |
| 48 | GlobalPrefix(""), |
| 49 | PrivateGlobalPrefix("."), |
| 50 | JumpTableSpecialLabelPrefix(0), |
| 51 | GlobalVarAddrPrefix(""), |
| 52 | GlobalVarAddrSuffix(""), |
| 53 | FunctionAddrPrefix(""), |
| 54 | FunctionAddrSuffix(""), |
Bill Wendling | d1bda4f | 2007-09-11 08:27:17 +0000 | [diff] [blame] | 55 | PersonalityPrefix(""), |
| 56 | PersonalitySuffix(""), |
Bill Wendling | 2d36992 | 2007-09-11 17:20:55 +0000 | [diff] [blame] | 57 | NeedsIndirectEncoding(false), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 58 | InlineAsmStart("#APP"), |
| 59 | InlineAsmEnd("#NO_APP"), |
| 60 | AssemblerDialect(0), |
Dale Johannesen | 8f03a20 | 2008-06-03 18:09:06 +0000 | [diff] [blame] | 61 | StringConstantPrefix(".str"), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +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), |
Lauro Ramos Venancio | 9190bc4 | 2008-02-28 22:14:09 +0000 | [diff] [blame] | 72 | TextAlignFillValue(0), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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), |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 81 | CStringSection_(0), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 82 | StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"), |
| 83 | StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"), |
| 84 | FourByteConstantSection(0), |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 85 | FourByteConstantSection_(0), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 86 | EightByteConstantSection(0), |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 87 | EightByteConstantSection_(0), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 88 | SixteenByteConstantSection(0), |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 89 | SixteenByteConstantSection_(0), |
Gordon Henriksen | e383f2a | 2007-12-23 20:58:16 +0000 | [diff] [blame] | 90 | GlobalDirective("\t.globl\t"), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 91 | SetDirective(0), |
| 92 | LCOMMDirective(0), |
| 93 | COMMDirective("\t.comm\t"), |
| 94 | COMMDirectiveTakesAlignment(true), |
| 95 | HasDotTypeDotSizeDirective(true), |
| 96 | UsedDirective(0), |
| 97 | WeakRefDirective(0), |
Dale Johannesen | fb3ac73 | 2007-11-20 23:24:42 +0000 | [diff] [blame] | 98 | WeakDefDirective(0), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 99 | HiddenDirective("\t.hidden\t"), |
| 100 | ProtectedDirective("\t.protected\t"), |
| 101 | AbsoluteDebugSectionOffsets(false), |
| 102 | AbsoluteEHSectionOffsets(false), |
| 103 | HasLEB128(false), |
Dan Gohman | 307ad20 | 2007-09-24 21:09:53 +0000 | [diff] [blame] | 104 | HasDotLocAndDotFile(false), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 105 | SupportsDebugInformation(false), |
| 106 | SupportsExceptionHandling(false), |
| 107 | DwarfRequiresFrameSection(true), |
Dale Johannesen | 23b89a6 | 2007-11-21 00:45:00 +0000 | [diff] [blame] | 108 | GlobalEHDirective(0), |
Dale Johannesen | f09b599 | 2008-01-10 02:03:30 +0000 | [diff] [blame] | 109 | SupportsWeakOmittedEHFrame(true), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 110 | DwarfSectionOffsetDirective(0), |
| 111 | DwarfAbbrevSection(".debug_abbrev"), |
| 112 | DwarfInfoSection(".debug_info"), |
| 113 | DwarfLineSection(".debug_line"), |
| 114 | DwarfFrameSection(".debug_frame"), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 115 | DwarfPubNamesSection(".debug_pubnames"), |
| 116 | DwarfPubTypesSection(".debug_pubtypes"), |
| 117 | DwarfStrSection(".debug_str"), |
| 118 | DwarfLocSection(".debug_loc"), |
| 119 | DwarfARangesSection(".debug_aranges"), |
| 120 | DwarfRangesSection(".debug_ranges"), |
| 121 | DwarfMacInfoSection(".debug_macinfo"), |
| 122 | DwarfEHFrameSection(".eh_frame"), |
| 123 | DwarfExceptionSection(".gcc_except_table"), |
| 124 | AsmTransCBE(0) { |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 125 | TextSection_ = getUnnamedSection(TextSection); |
| 126 | DataSection_ = getUnnamedSection(DataSection); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | TargetAsmInfo::~TargetAsmInfo() { |
| 130 | } |
| 131 | |
| 132 | /// Measure the specified inline asm to determine an approximation of its |
| 133 | /// length. |
| 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. |
| 140 | unsigned TargetAsmInfo::getInlineAsmLength(const char *Str) const { |
| 141 | // Count the number of instructions in the asm. |
| 142 | bool atInsnStart = true; |
| 143 | unsigned Length = 0; |
| 144 | for (; *Str; ++Str) { |
| 145 | if (*Str == '\n' || *Str == SeparatorChar) |
| 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; |
| 153 | } |
| 154 | |
| 155 | return Length; |
| 156 | } |
| 157 | |
Anton Korobeynikov | 8750d7c | 2008-02-29 22:09:08 +0000 | [diff] [blame] | 158 | unsigned TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason, |
Anton Korobeynikov | 65c0d87 | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 159 | bool Global) const { |
| 160 | return dwarf::DW_EH_PE_absptr; |
| 161 | } |
| 162 | |
Anton Korobeynikov | b4f658c | 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 | |
| 172 | SectionKind::Kind |
| 173 | TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const { |
| 174 | // Early exit - functions should be always in text sections. |
| 175 | if (isa<Function>(GV)) |
| 176 | return SectionKind::Text; |
| 177 | |
| 178 | const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV); |
| 179 | bool isThreadLocal = GVar->isThreadLocal(); |
| 180 | assert(GVar && "Invalid global value for section selection"); |
| 181 | |
Anton Korobeynikov | b4f658c | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 182 | if (isSuitableForBSS(GVar)) { |
| 183 | // Variable can be easily put to BSS section. |
| 184 | return (isThreadLocal ? SectionKind::ThreadBSS : SectionKind::BSS); |
| 185 | } else if (GVar->isConstant() && !isThreadLocal) { |
| 186 | // Now we know, that varible has initializer and it is constant. We need to |
| 187 | // check its initializer to decide, which section to output it into. Also |
| 188 | // note, there is no thread-local r/o section. |
| 189 | Constant *C = GVar->getInitializer(); |
| 190 | if (C->ContainsRelocations()) |
Anton Korobeynikov | b81503c | 2008-07-09 13:24:38 +0000 | [diff] [blame] | 191 | return SectionKind::ROData; |
Anton Korobeynikov | b4f658c | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 192 | else { |
| 193 | const ConstantArray *CVA = dyn_cast<ConstantArray>(C); |
| 194 | // Check, if initializer is a null-terminated string |
| 195 | if (CVA && CVA->isCString()) |
Anton Korobeynikov | b81503c | 2008-07-09 13:24:38 +0000 | [diff] [blame] | 196 | return SectionKind::RODataMergeStr; |
Anton Korobeynikov | b4f658c | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 197 | else |
Anton Korobeynikov | b81503c | 2008-07-09 13:24:38 +0000 | [diff] [blame] | 198 | return SectionKind::RODataMergeConst; |
Anton Korobeynikov | b4f658c | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | // Variable is not constant or thread-local - emit to generic data section. |
| 203 | return (isThreadLocal ? SectionKind::ThreadData : SectionKind::Data); |
| 204 | } |
Anton Korobeynikov | b3593bf | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 205 | |
| 206 | unsigned |
| 207 | TargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV, |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 208 | const char* Name) const { |
| 209 | unsigned Flags = SectionFlags::None; |
Anton Korobeynikov | b3593bf | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 210 | |
| 211 | // Decode flags from global itself. |
| 212 | if (GV) { |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 213 | SectionKind::Kind Kind = SectionKindForGlobal(GV); |
| 214 | switch (Kind) { |
Anton Korobeynikov | b3593bf | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 215 | case SectionKind::Text: |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 216 | Flags |= SectionFlags::Code; |
Anton Korobeynikov | b3593bf | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 217 | break; |
| 218 | case SectionKind::ThreadData: |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 219 | Flags |= SectionFlags::TLS; |
Anton Korobeynikov | b3593bf | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 220 | // FALLS THROUGH |
| 221 | case SectionKind::Data: |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 222 | Flags |= SectionFlags::Writeable; |
Anton Korobeynikov | b3593bf | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 223 | break; |
| 224 | case SectionKind::ThreadBSS: |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 225 | Flags |= SectionFlags::TLS; |
Anton Korobeynikov | b3593bf | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 226 | // FALLS THROUGH |
| 227 | case SectionKind::BSS: |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 228 | Flags |= SectionFlags::BSS; |
Anton Korobeynikov | b3593bf | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 229 | break; |
| 230 | case SectionKind::ROData: |
| 231 | // No additional flags here |
| 232 | break; |
| 233 | case SectionKind::RODataMergeStr: |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 234 | Flags |= SectionFlags::Strings; |
Anton Korobeynikov | b3593bf | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 235 | // FALLS THROUGH |
| 236 | case SectionKind::RODataMergeConst: |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 237 | Flags |= SectionFlags::Mergeable; |
Anton Korobeynikov | b3593bf | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 238 | break; |
| 239 | default: |
| 240 | assert(0 && "Unexpected section kind!"); |
| 241 | } |
| 242 | |
Anton Korobeynikov | f681654 | 2008-07-09 13:27:59 +0000 | [diff] [blame] | 243 | if (GV->isWeakForLinker()) |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 244 | Flags |= SectionFlags::Linkonce; |
Anton Korobeynikov | b3593bf | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // Add flags from sections, if any. |
Anton Korobeynikov | f7a8294 | 2008-07-09 13:25:46 +0000 | [diff] [blame] | 248 | if (Name && *Name) { |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 249 | Flags |= SectionFlags::Named; |
| 250 | |
| 251 | // Some lame default implementation based on some magic section names. |
| 252 | if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 || |
| 253 | strncmp(Name, ".llvm.linkonce.b.", 17) == 0) |
| 254 | Flags |= SectionFlags::BSS; |
| 255 | else if (strcmp(Name, ".tdata") == 0 || |
| 256 | strncmp(Name, ".tdata.", 7) == 0 || |
| 257 | strncmp(Name, ".gnu.linkonce.td.", 17) == 0 || |
| 258 | strncmp(Name, ".llvm.linkonce.td.", 18) == 0) |
| 259 | Flags |= SectionFlags::TLS; |
| 260 | else if (strcmp(Name, ".tbss") == 0 || |
| 261 | strncmp(Name, ".tbss.", 6) == 0 || |
| 262 | strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 || |
| 263 | strncmp(Name, ".llvm.linkonce.tb.", 18) == 0) |
| 264 | Flags |= SectionFlags::BSS | SectionFlags::TLS; |
Anton Korobeynikov | b3593bf | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 267 | return Flags; |
Anton Korobeynikov | b3593bf | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 268 | } |
Anton Korobeynikov | 562c3f2 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 269 | |
Anton Korobeynikov | 8bf5d34 | 2008-07-09 13:19:08 +0000 | [diff] [blame] | 270 | std::string |
Anton Korobeynikov | 562c3f2 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 271 | TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const { |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 272 | const Section* S; |
Anton Korobeynikov | 676a625 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 273 | // Select section name |
| 274 | if (GV->hasSection()) { |
| 275 | // Honour section already set, if any |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 276 | unsigned Flags = SectionFlagsForGlobal(GV, |
| 277 | GV->getSection().c_str()); |
| 278 | S = getNamedSection(GV->getSection().c_str(), Flags); |
Anton Korobeynikov | 676a625 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 279 | } else { |
| 280 | // Use default section depending on the 'type' of global |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 281 | S = SelectSectionForGlobal(GV); |
Anton Korobeynikov | 676a625 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 284 | std::string Name = S->Name; |
| 285 | |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 286 | // If section is named we need to switch into it via special '.section' |
| 287 | // directive and also append funky flags. Otherwise - section name is just |
| 288 | // some magic assembler directive. |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 289 | if (S->isNamed()) |
| 290 | Name = getSwitchToSectionDirective() + Name + PrintSectionFlags(S->Flags); |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 291 | |
Anton Korobeynikov | 676a625 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 292 | return Name; |
| 293 | } |
| 294 | |
| 295 | // Lame default implementation. Calculate the section name for global. |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 296 | const Section* |
Anton Korobeynikov | 676a625 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 297 | TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const { |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 298 | SectionKind::Kind Kind = SectionKindForGlobal(GV); |
Anton Korobeynikov | 562c3f2 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 299 | |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 300 | if (GV->isWeakForLinker()) { |
| 301 | std::string Name = UniqueSectionForGlobal(GV, Kind); |
| 302 | unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str()); |
| 303 | return getNamedSection(Name.c_str(), Flags); |
| 304 | } else { |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 305 | if (Kind == SectionKind::Text) |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 306 | return getTextSection_(); |
| 307 | else if (Kind == SectionKind::BSS && getBSSSection_()) |
| 308 | return getBSSSection_(); |
| 309 | else if (getReadOnlySection_() && |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 310 | (Kind == SectionKind::ROData || |
| 311 | Kind == SectionKind::RODataMergeConst || |
| 312 | Kind == SectionKind::RODataMergeStr)) |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 313 | return getReadOnlySection_(); |
Anton Korobeynikov | 676a625 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 314 | } |
Anton Korobeynikov | 562c3f2 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 315 | |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 316 | return getDataSection_(); |
Anton Korobeynikov | 562c3f2 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 317 | } |
Anton Korobeynikov | 3819e2d | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 318 | |
| 319 | std::string |
| 320 | TargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV, |
Anton Korobeynikov | 03db677 | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 321 | SectionKind::Kind Kind) const { |
| 322 | switch (Kind) { |
Anton Korobeynikov | 3819e2d | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 323 | case SectionKind::Text: |
Anton Korobeynikov | a3d07de | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 324 | return ".gnu.linkonce.t." + GV->getName(); |
Anton Korobeynikov | 3819e2d | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 325 | case SectionKind::Data: |
Anton Korobeynikov | a3d07de | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 326 | return ".gnu.linkonce.d." + GV->getName(); |
Anton Korobeynikov | 3819e2d | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 327 | case SectionKind::BSS: |
Anton Korobeynikov | a3d07de | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 328 | return ".gnu.linkonce.b." + GV->getName(); |
Anton Korobeynikov | 3819e2d | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 329 | case SectionKind::ROData: |
| 330 | case SectionKind::RODataMergeConst: |
| 331 | case SectionKind::RODataMergeStr: |
Anton Korobeynikov | a3d07de | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 332 | return ".gnu.linkonce.r." + GV->getName(); |
Anton Korobeynikov | 3819e2d | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 333 | case SectionKind::ThreadData: |
Anton Korobeynikov | a3d07de | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 334 | return ".gnu.linkonce.td." + GV->getName(); |
Anton Korobeynikov | 3819e2d | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 335 | case SectionKind::ThreadBSS: |
Anton Korobeynikov | a3d07de | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 336 | return ".gnu.linkonce.tb." + GV->getName(); |
Anton Korobeynikov | 3819e2d | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 337 | default: |
| 338 | assert(0 && "Unknown section kind"); |
| 339 | } |
| 340 | } |
Anton Korobeynikov | c33a1ff | 2008-07-09 13:28:49 +0000 | [diff] [blame^] | 341 | |
| 342 | const Section* |
| 343 | TargetAsmInfo::getNamedSection(const char *Name, unsigned Flags) const { |
| 344 | Section& S = Sections[Name]; |
| 345 | |
| 346 | // This is newly-created section, set it up properly. |
| 347 | if (S.Flags == SectionFlags::Invalid) { |
| 348 | S.Flags = Flags | SectionFlags::Named; |
| 349 | S.Name = Name; |
| 350 | } |
| 351 | |
| 352 | return &S; |
| 353 | } |
| 354 | |
| 355 | const Section* |
| 356 | TargetAsmInfo::getUnnamedSection(const char *Directive, unsigned Flags) const { |
| 357 | Section& S = Sections[Directive]; |
| 358 | |
| 359 | // This is newly-created section, set it up properly. |
| 360 | if (S.Flags == SectionFlags::Invalid) { |
| 361 | S.Flags = Flags & ~SectionFlags::Named; |
| 362 | S.Name = Directive; |
| 363 | } |
| 364 | |
| 365 | return &S; |
| 366 | } |