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