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" |
| 16 | #include "llvm/GlobalVariable.h" |
| 17 | #include "llvm/Function.h" |
| 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/Type.h" |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetAsmInfo.h" |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetOptions.h" |
Anton Korobeynikov | cee750f | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Dwarf.h" |
Dale Johannesen | 3bb6283 | 2007-04-23 20:00:17 +0000 | [diff] [blame] | 23 | #include <cctype> |
| 24 | #include <cstring> |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | TargetAsmInfo::TargetAsmInfo() : |
Dan Gohman | 9570165 | 2007-05-03 18:46:30 +0000 | [diff] [blame] | 29 | TextSection("\t.text"), |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 30 | TextSection_(0), |
Dan Gohman | 9570165 | 2007-05-03 18:46:30 +0000 | [diff] [blame] | 31 | DataSection("\t.data"), |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 32 | DataSection_(0), |
Dan Gohman | 9570165 | 2007-05-03 18:46:30 +0000 | [diff] [blame] | 33 | BSSSection("\t.bss"), |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 34 | BSSSection_(0), |
| 35 | ReadOnlySection(0), |
| 36 | ReadOnlySection_(0), |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 37 | TLSDataSection("\t.section .tdata,\"awT\",@progbits"), |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 38 | TLSDataSection_(0), |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 39 | TLSBSSSection("\t.section .tbss,\"awT\",@nobits"), |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 40 | TLSBSSSection_(0), |
Chris Lattner | 6f198df | 2007-01-17 17:42:42 +0000 | [diff] [blame] | 41 | ZeroFillDirective(0), |
Dan Gohman | a779a98 | 2008-05-05 00:28:39 +0000 | [diff] [blame] | 42 | NonexecutableStackDirective(0), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 43 | NeedsSet(false), |
Chris Lattner | 4c7b07a | 2006-10-13 17:50:07 +0000 | [diff] [blame] | 44 | MaxInstLength(4), |
Jim Laskey | b82313f | 2007-02-01 16:31:34 +0000 | [diff] [blame] | 45 | PCSymbol("$"), |
Chris Lattner | 4c7b07a | 2006-10-13 17:50:07 +0000 | [diff] [blame] | 46 | SeparatorChar(';'), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 47 | CommentString("#"), |
| 48 | GlobalPrefix(""), |
| 49 | PrivateGlobalPrefix("."), |
Chris Lattner | 393a8ee | 2007-01-18 01:12:56 +0000 | [diff] [blame] | 50 | JumpTableSpecialLabelPrefix(0), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 51 | GlobalVarAddrPrefix(""), |
| 52 | GlobalVarAddrSuffix(""), |
| 53 | FunctionAddrPrefix(""), |
| 54 | FunctionAddrSuffix(""), |
Bill Wendling | d60da49 | 2007-09-11 08:27:17 +0000 | [diff] [blame] | 55 | PersonalityPrefix(""), |
| 56 | PersonalitySuffix(""), |
Bill Wendling | ef4a661 | 2007-09-11 17:20:55 +0000 | [diff] [blame] | 57 | NeedsIndirectEncoding(false), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 58 | InlineAsmStart("#APP"), |
| 59 | InlineAsmEnd("#NO_APP"), |
Bill Wendling | eb9a42c | 2007-01-16 03:42:04 +0000 | [diff] [blame] | 60 | AssemblerDialect(0), |
Dale Johannesen | 428ac54 | 2008-06-03 18:09:06 +0000 | [diff] [blame] | 61 | StringConstantPrefix(".str"), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35: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), |
Lauro Ramos Venancio | f3c630e | 2008-02-28 22:14:09 +0000 | [diff] [blame] | 72 | TextAlignFillValue(0), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 73 | SwitchToSectionDirective("\t.section\t"), |
| 74 | TextSectionStartSuffix(""), |
| 75 | DataSectionStartSuffix(""), |
| 76 | SectionEndDirectiveSuffix(0), |
Dan Gohman | af67ea7 | 2007-06-14 15:00:27 +0000 | [diff] [blame] | 77 | ConstantPoolSection("\t.section .rodata"), |
| 78 | JumpTableDataSection("\t.section .rodata"), |
Andrew Lenharth | beec30e | 2006-09-24 19:45:58 +0000 | [diff] [blame] | 79 | JumpTableDirective(0), |
Reid Spencer | c50209b | 2006-10-27 16:14:06 +0000 | [diff] [blame] | 80 | CStringSection(0), |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 81 | CStringSection_(0), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 82 | StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"), |
| 83 | StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"), |
| 84 | FourByteConstantSection(0), |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 85 | FourByteConstantSection_(0), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 86 | EightByteConstantSection(0), |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 87 | EightByteConstantSection_(0), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 88 | SixteenByteConstantSection(0), |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 89 | SixteenByteConstantSection_(0), |
Gordon Henriksen | 81361d6 | 2007-12-23 20:58:16 +0000 | [diff] [blame] | 90 | GlobalDirective("\t.globl\t"), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 91 | SetDirective(0), |
| 92 | LCOMMDirective(0), |
| 93 | COMMDirective("\t.comm\t"), |
| 94 | COMMDirectiveTakesAlignment(true), |
| 95 | HasDotTypeDotSizeDirective(true), |
Chris Lattner | cb05af8 | 2006-09-26 03:38:18 +0000 | [diff] [blame] | 96 | UsedDirective(0), |
Evan Cheng | 8752ce6 | 2006-12-01 20:47:11 +0000 | [diff] [blame] | 97 | WeakRefDirective(0), |
Dale Johannesen | 1d4ce2a | 2007-11-20 23:24:42 +0000 | [diff] [blame] | 98 | WeakDefDirective(0), |
Chris Lattner | 9784bc7 | 2007-01-14 06:27:21 +0000 | [diff] [blame] | 99 | HiddenDirective("\t.hidden\t"), |
Anton Korobeynikov | 6f9896f | 2007-04-29 18:35:00 +0000 | [diff] [blame] | 100 | ProtectedDirective("\t.protected\t"), |
Anton Korobeynikov | 79dda2b | 2007-05-01 22:23:12 +0000 | [diff] [blame] | 101 | AbsoluteDebugSectionOffsets(false), |
| 102 | AbsoluteEHSectionOffsets(false), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 103 | HasLEB128(false), |
Dan Gohman | 7225273 | 2007-09-24 21:09:53 +0000 | [diff] [blame] | 104 | HasDotLocAndDotFile(false), |
Anton Korobeynikov | 2a07e2f | 2007-05-05 09:04:50 +0000 | [diff] [blame] | 105 | SupportsDebugInformation(false), |
Jim Laskey | 072200c | 2007-01-29 18:51:14 +0000 | [diff] [blame] | 106 | SupportsExceptionHandling(false), |
Reid Spencer | 02b8511 | 2006-10-30 22:32:30 +0000 | [diff] [blame] | 107 | DwarfRequiresFrameSection(true), |
Dale Johannesen | 9305dcb | 2007-11-21 00:45:00 +0000 | [diff] [blame] | 108 | GlobalEHDirective(0), |
Dale Johannesen | 038129d | 2008-01-10 02:03:30 +0000 | [diff] [blame] | 109 | SupportsWeakOmittedEHFrame(true), |
Anton Korobeynikov | a6199c8 | 2007-03-07 02:47:57 +0000 | [diff] [blame] | 110 | DwarfSectionOffsetDirective(0), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 111 | DwarfAbbrevSection(".debug_abbrev"), |
| 112 | DwarfInfoSection(".debug_info"), |
| 113 | DwarfLineSection(".debug_line"), |
| 114 | DwarfFrameSection(".debug_frame"), |
| 115 | DwarfPubNamesSection(".debug_pubnames"), |
| 116 | DwarfPubTypesSection(".debug_pubtypes"), |
| 117 | DwarfStrSection(".debug_str"), |
| 118 | DwarfLocSection(".debug_loc"), |
| 119 | DwarfARangesSection(".debug_aranges"), |
| 120 | DwarfRangesSection(".debug_ranges"), |
Andrew Lenharth | 3655de6 | 2006-11-28 19:52:20 +0000 | [diff] [blame] | 121 | DwarfMacInfoSection(".debug_macinfo"), |
Jim Laskey | b82313f | 2007-02-01 16:31:34 +0000 | [diff] [blame] | 122 | DwarfEHFrameSection(".eh_frame"), |
Jim Laskey | a15be8c | 2007-02-21 22:43:40 +0000 | [diff] [blame] | 123 | DwarfExceptionSection(".gcc_except_table"), |
Andrew Lenharth | 3655de6 | 2006-11-28 19:52:20 +0000 | [diff] [blame] | 124 | AsmTransCBE(0) { |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 125 | TextSection_ = getUnnamedSection(TextSection); |
| 126 | DataSection_ = getUnnamedSection(DataSection); |
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 | |
| 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 | 28a2b54 | 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 | 0e48a0c | 2008-07-09 13:24:38 +0000 | [diff] [blame] | 191 | return SectionKind::ROData; |
Anton Korobeynikov | 28a2b54 | 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 | 0e48a0c | 2008-07-09 13:24:38 +0000 | [diff] [blame] | 196 | return SectionKind::RODataMergeStr; |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 197 | else |
Anton Korobeynikov | 0e48a0c | 2008-07-09 13:24:38 +0000 | [diff] [blame] | 198 | return SectionKind::RODataMergeConst; |
Anton Korobeynikov | 28a2b54 | 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 | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 205 | |
| 206 | unsigned |
| 207 | TargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV, |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 208 | const char* Name) const { |
| 209 | unsigned Flags = SectionFlags::None; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 210 | |
| 211 | // Decode flags from global itself. |
| 212 | if (GV) { |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 213 | SectionKind::Kind Kind = SectionKindForGlobal(GV); |
| 214 | switch (Kind) { |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 215 | case SectionKind::Text: |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 216 | Flags |= SectionFlags::Code; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 217 | break; |
| 218 | case SectionKind::ThreadData: |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 219 | case SectionKind::ThreadBSS: |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 220 | Flags |= SectionFlags::TLS; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 221 | // FALLS THROUGH |
Anton Korobeynikov | 7d51edf | 2008-07-09 13:29:44 +0000 | [diff] [blame^] | 222 | case SectionKind::Data: |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 223 | case SectionKind::BSS: |
Anton Korobeynikov | 7d51edf | 2008-07-09 13:29:44 +0000 | [diff] [blame^] | 224 | Flags |= SectionFlags::Writeable; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 225 | break; |
| 226 | case SectionKind::ROData: |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 227 | case SectionKind::RODataMergeStr: |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 228 | case SectionKind::RODataMergeConst: |
Anton Korobeynikov | 7d51edf | 2008-07-09 13:29:44 +0000 | [diff] [blame^] | 229 | // No additional flags here |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 230 | break; |
| 231 | default: |
| 232 | assert(0 && "Unexpected section kind!"); |
| 233 | } |
| 234 | |
Anton Korobeynikov | c33a744 | 2008-07-09 13:27:59 +0000 | [diff] [blame] | 235 | if (GV->isWeakForLinker()) |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 236 | Flags |= SectionFlags::Linkonce; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | // Add flags from sections, if any. |
Anton Korobeynikov | 2a88917 | 2008-07-09 13:25:46 +0000 | [diff] [blame] | 240 | if (Name && *Name) { |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 241 | Flags |= SectionFlags::Named; |
| 242 | |
| 243 | // Some lame default implementation based on some magic section names. |
| 244 | if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 || |
| 245 | strncmp(Name, ".llvm.linkonce.b.", 17) == 0) |
| 246 | Flags |= SectionFlags::BSS; |
| 247 | else if (strcmp(Name, ".tdata") == 0 || |
| 248 | strncmp(Name, ".tdata.", 7) == 0 || |
| 249 | strncmp(Name, ".gnu.linkonce.td.", 17) == 0 || |
| 250 | strncmp(Name, ".llvm.linkonce.td.", 18) == 0) |
| 251 | Flags |= SectionFlags::TLS; |
| 252 | else if (strcmp(Name, ".tbss") == 0 || |
| 253 | strncmp(Name, ".tbss.", 6) == 0 || |
| 254 | strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 || |
| 255 | strncmp(Name, ".llvm.linkonce.tb.", 18) == 0) |
| 256 | Flags |= SectionFlags::BSS | SectionFlags::TLS; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 259 | return Flags; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 260 | } |
Anton Korobeynikov | 0c60246 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 261 | |
Anton Korobeynikov | c0f41db | 2008-07-09 13:19:08 +0000 | [diff] [blame] | 262 | std::string |
Anton Korobeynikov | 0c60246 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 263 | TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const { |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 264 | const Section* S; |
Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 265 | // Select section name |
| 266 | if (GV->hasSection()) { |
| 267 | // Honour section already set, if any |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 268 | unsigned Flags = SectionFlagsForGlobal(GV, |
| 269 | GV->getSection().c_str()); |
| 270 | S = getNamedSection(GV->getSection().c_str(), Flags); |
Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 271 | } else { |
| 272 | // Use default section depending on the 'type' of global |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 273 | S = SelectSectionForGlobal(GV); |
Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 276 | std::string Name = S->Name; |
| 277 | |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 278 | // If section is named we need to switch into it via special '.section' |
| 279 | // directive and also append funky flags. Otherwise - section name is just |
| 280 | // some magic assembler directive. |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 281 | if (S->isNamed()) |
| 282 | Name = getSwitchToSectionDirective() + Name + PrintSectionFlags(S->Flags); |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 283 | |
Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 284 | return Name; |
| 285 | } |
| 286 | |
| 287 | // Lame default implementation. Calculate the section name for global. |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 288 | const Section* |
Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 289 | TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const { |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 290 | SectionKind::Kind Kind = SectionKindForGlobal(GV); |
Anton Korobeynikov | 0c60246 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 291 | |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 292 | if (GV->isWeakForLinker()) { |
| 293 | std::string Name = UniqueSectionForGlobal(GV, Kind); |
| 294 | unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str()); |
| 295 | return getNamedSection(Name.c_str(), Flags); |
| 296 | } else { |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 297 | if (Kind == SectionKind::Text) |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 298 | return getTextSection_(); |
| 299 | else if (Kind == SectionKind::BSS && getBSSSection_()) |
| 300 | return getBSSSection_(); |
| 301 | else if (getReadOnlySection_() && |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 302 | (Kind == SectionKind::ROData || |
| 303 | Kind == SectionKind::RODataMergeConst || |
| 304 | Kind == SectionKind::RODataMergeStr)) |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 305 | return getReadOnlySection_(); |
Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 306 | } |
Anton Korobeynikov | 0c60246 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 307 | |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 308 | return getDataSection_(); |
Anton Korobeynikov | 0c60246 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 309 | } |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 310 | |
| 311 | std::string |
| 312 | TargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV, |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 313 | SectionKind::Kind Kind) const { |
| 314 | switch (Kind) { |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 315 | case SectionKind::Text: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 316 | return ".gnu.linkonce.t." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 317 | case SectionKind::Data: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 318 | return ".gnu.linkonce.d." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 319 | case SectionKind::BSS: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 320 | return ".gnu.linkonce.b." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 321 | case SectionKind::ROData: |
| 322 | case SectionKind::RODataMergeConst: |
| 323 | case SectionKind::RODataMergeStr: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 324 | return ".gnu.linkonce.r." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 325 | case SectionKind::ThreadData: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 326 | return ".gnu.linkonce.td." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 327 | case SectionKind::ThreadBSS: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 328 | return ".gnu.linkonce.tb." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 329 | default: |
| 330 | assert(0 && "Unknown section kind"); |
| 331 | } |
| 332 | } |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 333 | |
| 334 | const Section* |
| 335 | TargetAsmInfo::getNamedSection(const char *Name, unsigned Flags) const { |
| 336 | Section& S = Sections[Name]; |
| 337 | |
| 338 | // This is newly-created section, set it up properly. |
| 339 | if (S.Flags == SectionFlags::Invalid) { |
| 340 | S.Flags = Flags | SectionFlags::Named; |
| 341 | S.Name = Name; |
| 342 | } |
| 343 | |
| 344 | return &S; |
| 345 | } |
| 346 | |
| 347 | const Section* |
| 348 | TargetAsmInfo::getUnnamedSection(const char *Directive, unsigned Flags) const { |
| 349 | Section& S = Sections[Directive]; |
| 350 | |
| 351 | // This is newly-created section, set it up properly. |
| 352 | if (S.Flags == SectionFlags::Invalid) { |
| 353 | S.Flags = Flags & ~SectionFlags::Named; |
| 354 | S.Name = Directive; |
| 355 | } |
| 356 | |
| 357 | return &S; |
| 358 | } |