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