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() : |
Anton Korobeynikov | d7ca416 | 2008-09-24 22:15:21 +0000 | [diff] [blame] | 29 | TextSection(0), |
Anton Korobeynikov | 315690e | 2008-09-24 22:16:16 +0000 | [diff] [blame^] | 30 | DataSection(0), |
Dan Gohman | 9570165 | 2007-05-03 18:46:30 +0000 | [diff] [blame] | 31 | BSSSection("\t.bss"), |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 32 | BSSSection_(0), |
| 33 | ReadOnlySection(0), |
| 34 | ReadOnlySection_(0), |
Bruno Cardoso Lopes | 62d590c | 2008-07-22 15:26:53 +0000 | [diff] [blame] | 35 | SmallDataSection(0), |
| 36 | SmallBSSSection(0), |
| 37 | SmallRODataSection(0), |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 38 | TLSDataSection("\t.section .tdata,\"awT\",@progbits"), |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 39 | TLSDataSection_(0), |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 40 | TLSBSSSection("\t.section .tbss,\"awT\",@nobits"), |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 41 | TLSBSSSection_(0), |
Chris Lattner | 6f198df | 2007-01-17 17:42:42 +0000 | [diff] [blame] | 42 | ZeroFillDirective(0), |
Dan Gohman | a779a98 | 2008-05-05 00:28:39 +0000 | [diff] [blame] | 43 | NonexecutableStackDirective(0), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 44 | NeedsSet(false), |
Chris Lattner | 4c7b07a | 2006-10-13 17:50:07 +0000 | [diff] [blame] | 45 | MaxInstLength(4), |
Jim Laskey | b82313f | 2007-02-01 16:31:34 +0000 | [diff] [blame] | 46 | PCSymbol("$"), |
Chris Lattner | 4c7b07a | 2006-10-13 17:50:07 +0000 | [diff] [blame] | 47 | SeparatorChar(';'), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 48 | CommentString("#"), |
| 49 | GlobalPrefix(""), |
| 50 | PrivateGlobalPrefix("."), |
Dale Johannesen | b2dfb89 | 2008-09-09 01:21:22 +0000 | [diff] [blame] | 51 | LessPrivateGlobalPrefix(""), |
Chris Lattner | 393a8ee | 2007-01-18 01:12:56 +0000 | [diff] [blame] | 52 | JumpTableSpecialLabelPrefix(0), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 53 | GlobalVarAddrPrefix(""), |
| 54 | GlobalVarAddrSuffix(""), |
| 55 | FunctionAddrPrefix(""), |
| 56 | FunctionAddrSuffix(""), |
Bill Wendling | d60da49 | 2007-09-11 08:27:17 +0000 | [diff] [blame] | 57 | PersonalityPrefix(""), |
| 58 | PersonalitySuffix(""), |
Bill Wendling | ef4a661 | 2007-09-11 17:20:55 +0000 | [diff] [blame] | 59 | NeedsIndirectEncoding(false), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 60 | InlineAsmStart("#APP"), |
| 61 | InlineAsmEnd("#NO_APP"), |
Bill Wendling | eb9a42c | 2007-01-16 03:42:04 +0000 | [diff] [blame] | 62 | AssemblerDialect(0), |
Dale Johannesen | 428ac54 | 2008-06-03 18:09:06 +0000 | [diff] [blame] | 63 | StringConstantPrefix(".str"), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 64 | ZeroDirective("\t.zero\t"), |
| 65 | ZeroDirectiveSuffix(0), |
| 66 | AsciiDirective("\t.ascii\t"), |
| 67 | AscizDirective("\t.asciz\t"), |
| 68 | Data8bitsDirective("\t.byte\t"), |
| 69 | Data16bitsDirective("\t.short\t"), |
| 70 | Data32bitsDirective("\t.long\t"), |
| 71 | Data64bitsDirective("\t.quad\t"), |
| 72 | AlignDirective("\t.align\t"), |
| 73 | AlignmentIsInBytes(true), |
Lauro Ramos Venancio | f3c630e | 2008-02-28 22:14:09 +0000 | [diff] [blame] | 74 | TextAlignFillValue(0), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 75 | SwitchToSectionDirective("\t.section\t"), |
| 76 | TextSectionStartSuffix(""), |
| 77 | DataSectionStartSuffix(""), |
| 78 | SectionEndDirectiveSuffix(0), |
Dan Gohman | af67ea7 | 2007-06-14 15:00:27 +0000 | [diff] [blame] | 79 | ConstantPoolSection("\t.section .rodata"), |
| 80 | JumpTableDataSection("\t.section .rodata"), |
Andrew Lenharth | beec30e | 2006-09-24 19:45:58 +0000 | [diff] [blame] | 81 | JumpTableDirective(0), |
Reid Spencer | c50209b | 2006-10-27 16:14:06 +0000 | [diff] [blame] | 82 | CStringSection(0), |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 83 | CStringSection_(0), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 84 | StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"), |
| 85 | StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"), |
| 86 | FourByteConstantSection(0), |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 87 | FourByteConstantSection_(0), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 88 | EightByteConstantSection(0), |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 89 | EightByteConstantSection_(0), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 90 | SixteenByteConstantSection(0), |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 91 | SixteenByteConstantSection_(0), |
Gordon Henriksen | 81361d6 | 2007-12-23 20:58:16 +0000 | [diff] [blame] | 92 | GlobalDirective("\t.globl\t"), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 93 | SetDirective(0), |
| 94 | LCOMMDirective(0), |
| 95 | COMMDirective("\t.comm\t"), |
| 96 | COMMDirectiveTakesAlignment(true), |
| 97 | HasDotTypeDotSizeDirective(true), |
Chris Lattner | cb05af8 | 2006-09-26 03:38:18 +0000 | [diff] [blame] | 98 | UsedDirective(0), |
Evan Cheng | 8752ce6 | 2006-12-01 20:47:11 +0000 | [diff] [blame] | 99 | WeakRefDirective(0), |
Dale Johannesen | 1d4ce2a | 2007-11-20 23:24:42 +0000 | [diff] [blame] | 100 | WeakDefDirective(0), |
Chris Lattner | 9784bc7 | 2007-01-14 06:27:21 +0000 | [diff] [blame] | 101 | HiddenDirective("\t.hidden\t"), |
Anton Korobeynikov | 6f9896f | 2007-04-29 18:35:00 +0000 | [diff] [blame] | 102 | ProtectedDirective("\t.protected\t"), |
Anton Korobeynikov | 79dda2b | 2007-05-01 22:23:12 +0000 | [diff] [blame] | 103 | AbsoluteDebugSectionOffsets(false), |
| 104 | AbsoluteEHSectionOffsets(false), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 105 | HasLEB128(false), |
Dan Gohman | 7225273 | 2007-09-24 21:09:53 +0000 | [diff] [blame] | 106 | HasDotLocAndDotFile(false), |
Anton Korobeynikov | 2a07e2f | 2007-05-05 09:04:50 +0000 | [diff] [blame] | 107 | SupportsDebugInformation(false), |
Jim Laskey | 072200c | 2007-01-29 18:51:14 +0000 | [diff] [blame] | 108 | SupportsExceptionHandling(false), |
Reid Spencer | 02b8511 | 2006-10-30 22:32:30 +0000 | [diff] [blame] | 109 | DwarfRequiresFrameSection(true), |
Dale Johannesen | 9305dcb | 2007-11-21 00:45:00 +0000 | [diff] [blame] | 110 | GlobalEHDirective(0), |
Dale Johannesen | 038129d | 2008-01-10 02:03:30 +0000 | [diff] [blame] | 111 | SupportsWeakOmittedEHFrame(true), |
Anton Korobeynikov | a6199c8 | 2007-03-07 02:47:57 +0000 | [diff] [blame] | 112 | DwarfSectionOffsetDirective(0), |
Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 113 | DwarfAbbrevSection(".debug_abbrev"), |
| 114 | DwarfInfoSection(".debug_info"), |
| 115 | DwarfLineSection(".debug_line"), |
| 116 | DwarfFrameSection(".debug_frame"), |
| 117 | DwarfPubNamesSection(".debug_pubnames"), |
| 118 | DwarfPubTypesSection(".debug_pubtypes"), |
| 119 | DwarfStrSection(".debug_str"), |
| 120 | DwarfLocSection(".debug_loc"), |
| 121 | DwarfARangesSection(".debug_aranges"), |
| 122 | DwarfRangesSection(".debug_ranges"), |
Andrew Lenharth | 3655de6 | 2006-11-28 19:52:20 +0000 | [diff] [blame] | 123 | DwarfMacInfoSection(".debug_macinfo"), |
Jim Laskey | b82313f | 2007-02-01 16:31:34 +0000 | [diff] [blame] | 124 | DwarfEHFrameSection(".eh_frame"), |
Jim Laskey | a15be8c | 2007-02-21 22:43:40 +0000 | [diff] [blame] | 125 | DwarfExceptionSection(".gcc_except_table"), |
Andrew Lenharth | 3655de6 | 2006-11-28 19:52:20 +0000 | [diff] [blame] | 126 | AsmTransCBE(0) { |
Anton Korobeynikov | d7ca416 | 2008-09-24 22:15:21 +0000 | [diff] [blame] | 127 | TextSection = getUnnamedSection("\t.text", SectionFlags::Code); |
Anton Korobeynikov | 315690e | 2008-09-24 22:16:16 +0000 | [diff] [blame^] | 128 | DataSection = getUnnamedSection("\t.data", SectionFlags::Writeable); |
Chris Lattner | 4c7b07a | 2006-10-13 17:50:07 +0000 | [diff] [blame] | 129 | } |
Chris Lattner | f5b10ec | 2006-10-05 00:35:16 +0000 | [diff] [blame] | 130 | |
| 131 | TargetAsmInfo::~TargetAsmInfo() { |
| 132 | } |
Chris Lattner | 4c7b07a | 2006-10-13 17:50:07 +0000 | [diff] [blame] | 133 | |
| 134 | /// Measure the specified inline asm to determine an approximation of its |
| 135 | /// length. |
Dale Johannesen | 3bb6283 | 2007-04-23 20:00:17 +0000 | [diff] [blame] | 136 | /// Comments (which run till the next SeparatorChar or newline) do not |
| 137 | /// count as an instruction. |
| 138 | /// Any other non-whitespace text is considered an instruction, with |
| 139 | /// multiple instructions separated by SeparatorChar or newlines. |
| 140 | /// Variable-length instructions are not handled here; this function |
| 141 | /// may be overloaded in the target code to do that. |
Chris Lattner | 4c7b07a | 2006-10-13 17:50:07 +0000 | [diff] [blame] | 142 | unsigned TargetAsmInfo::getInlineAsmLength(const char *Str) const { |
| 143 | // Count the number of instructions in the asm. |
Dale Johannesen | 3bb6283 | 2007-04-23 20:00:17 +0000 | [diff] [blame] | 144 | bool atInsnStart = true; |
| 145 | unsigned Length = 0; |
Chris Lattner | 4c7b07a | 2006-10-13 17:50:07 +0000 | [diff] [blame] | 146 | for (; *Str; ++Str) { |
| 147 | if (*Str == '\n' || *Str == SeparatorChar) |
Dale Johannesen | 3bb6283 | 2007-04-23 20:00:17 +0000 | [diff] [blame] | 148 | atInsnStart = true; |
| 149 | if (atInsnStart && !isspace(*Str)) { |
| 150 | Length += MaxInstLength; |
| 151 | atInsnStart = false; |
| 152 | } |
| 153 | if (atInsnStart && strncmp(Str, CommentString, strlen(CommentString))==0) |
| 154 | atInsnStart = false; |
Chris Lattner | 4c7b07a | 2006-10-13 17:50:07 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Dale Johannesen | 3bb6283 | 2007-04-23 20:00:17 +0000 | [diff] [blame] | 157 | return Length; |
Chris Lattner | 4c7b07a | 2006-10-13 17:50:07 +0000 | [diff] [blame] | 158 | } |
Anton Korobeynikov | a6199c8 | 2007-03-07 02:47:57 +0000 | [diff] [blame] | 159 | |
Anton Korobeynikov | 8213f9c | 2008-02-29 22:09:08 +0000 | [diff] [blame] | 160 | unsigned TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason, |
Anton Korobeynikov | cee750f | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 161 | bool Global) const { |
| 162 | return dwarf::DW_EH_PE_absptr; |
| 163 | } |
| 164 | |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 165 | static bool isSuitableForBSS(const GlobalVariable *GV) { |
| 166 | if (!GV->hasInitializer()) |
| 167 | return true; |
| 168 | |
| 169 | // Leave constant zeros in readonly constant sections, so they can be shared |
| 170 | Constant *C = GV->getInitializer(); |
| 171 | return (C->isNullValue() && !GV->isConstant() && !NoZerosInBSS); |
| 172 | } |
| 173 | |
| 174 | SectionKind::Kind |
| 175 | TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const { |
| 176 | // Early exit - functions should be always in text sections. |
| 177 | if (isa<Function>(GV)) |
| 178 | return SectionKind::Text; |
| 179 | |
| 180 | const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV); |
| 181 | bool isThreadLocal = GVar->isThreadLocal(); |
| 182 | assert(GVar && "Invalid global value for section selection"); |
| 183 | |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 184 | if (isSuitableForBSS(GVar)) { |
| 185 | // Variable can be easily put to BSS section. |
| 186 | return (isThreadLocal ? SectionKind::ThreadBSS : SectionKind::BSS); |
| 187 | } else if (GVar->isConstant() && !isThreadLocal) { |
| 188 | // Now we know, that varible has initializer and it is constant. We need to |
| 189 | // check its initializer to decide, which section to output it into. Also |
| 190 | // note, there is no thread-local r/o section. |
| 191 | Constant *C = GVar->getInitializer(); |
| 192 | if (C->ContainsRelocations()) |
Anton Korobeynikov | 0e48a0c | 2008-07-09 13:24:38 +0000 | [diff] [blame] | 193 | return SectionKind::ROData; |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 194 | else { |
| 195 | const ConstantArray *CVA = dyn_cast<ConstantArray>(C); |
| 196 | // Check, if initializer is a null-terminated string |
| 197 | if (CVA && CVA->isCString()) |
Anton Korobeynikov | 0e48a0c | 2008-07-09 13:24:38 +0000 | [diff] [blame] | 198 | return SectionKind::RODataMergeStr; |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 199 | else |
Anton Korobeynikov | 0e48a0c | 2008-07-09 13:24:38 +0000 | [diff] [blame] | 200 | return SectionKind::RODataMergeConst; |
Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | // Variable is not constant or thread-local - emit to generic data section. |
| 205 | return (isThreadLocal ? SectionKind::ThreadData : SectionKind::Data); |
| 206 | } |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 207 | |
| 208 | unsigned |
| 209 | TargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV, |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 210 | const char* Name) const { |
| 211 | unsigned Flags = SectionFlags::None; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 212 | |
| 213 | // Decode flags from global itself. |
| 214 | if (GV) { |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 215 | SectionKind::Kind Kind = SectionKindForGlobal(GV); |
| 216 | switch (Kind) { |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 217 | case SectionKind::Text: |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 218 | Flags |= SectionFlags::Code; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 219 | break; |
| 220 | case SectionKind::ThreadData: |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 221 | case SectionKind::ThreadBSS: |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 222 | Flags |= SectionFlags::TLS; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 223 | // FALLS THROUGH |
Anton Korobeynikov | 7d51edf | 2008-07-09 13:29:44 +0000 | [diff] [blame] | 224 | case SectionKind::Data: |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 225 | case SectionKind::BSS: |
Anton Korobeynikov | 7d51edf | 2008-07-09 13:29:44 +0000 | [diff] [blame] | 226 | Flags |= SectionFlags::Writeable; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 227 | break; |
| 228 | case SectionKind::ROData: |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 229 | case SectionKind::RODataMergeStr: |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 230 | case SectionKind::RODataMergeConst: |
Anton Korobeynikov | 7d51edf | 2008-07-09 13:29:44 +0000 | [diff] [blame] | 231 | // No additional flags here |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 232 | break; |
Anton Korobeynikov | 04dda73 | 2008-07-22 17:09:59 +0000 | [diff] [blame] | 233 | case SectionKind::SmallData: |
| 234 | case SectionKind::SmallBSS: |
| 235 | Flags |= SectionFlags::Writeable; |
| 236 | // FALLS THROUGH |
| 237 | case SectionKind::SmallROData: |
| 238 | Flags |= SectionFlags::Small; |
| 239 | break; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 240 | default: |
| 241 | assert(0 && "Unexpected section kind!"); |
| 242 | } |
| 243 | |
Anton Korobeynikov | c33a744 | 2008-07-09 13:27:59 +0000 | [diff] [blame] | 244 | if (GV->isWeakForLinker()) |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 245 | Flags |= SectionFlags::Linkonce; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | // Add flags from sections, if any. |
Anton Korobeynikov | 2a88917 | 2008-07-09 13:25:46 +0000 | [diff] [blame] | 249 | if (Name && *Name) { |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 250 | Flags |= SectionFlags::Named; |
| 251 | |
| 252 | // Some lame default implementation based on some magic section names. |
| 253 | if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 || |
Anton Korobeynikov | 04dda73 | 2008-07-22 17:09:59 +0000 | [diff] [blame] | 254 | strncmp(Name, ".llvm.linkonce.b.", 17) == 0 || |
| 255 | strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 || |
| 256 | strncmp(Name, ".llvm.linkonce.sb.", 18) == 0) |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 257 | Flags |= SectionFlags::BSS; |
| 258 | else if (strcmp(Name, ".tdata") == 0 || |
| 259 | strncmp(Name, ".tdata.", 7) == 0 || |
| 260 | strncmp(Name, ".gnu.linkonce.td.", 17) == 0 || |
| 261 | strncmp(Name, ".llvm.linkonce.td.", 18) == 0) |
| 262 | Flags |= SectionFlags::TLS; |
| 263 | else if (strcmp(Name, ".tbss") == 0 || |
| 264 | strncmp(Name, ".tbss.", 6) == 0 || |
| 265 | strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 || |
| 266 | strncmp(Name, ".llvm.linkonce.tb.", 18) == 0) |
| 267 | Flags |= SectionFlags::BSS | SectionFlags::TLS; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 270 | return Flags; |
Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 271 | } |
Anton Korobeynikov | 0c60246 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 272 | |
Anton Korobeynikov | c25e1ea | 2008-09-24 22:14:23 +0000 | [diff] [blame] | 273 | const Section* |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 274 | TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const { |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 275 | const Section* S; |
Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 276 | // Select section name |
| 277 | if (GV->hasSection()) { |
| 278 | // Honour section already set, if any |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 279 | unsigned Flags = SectionFlagsForGlobal(GV, |
| 280 | GV->getSection().c_str()); |
| 281 | S = getNamedSection(GV->getSection().c_str(), Flags); |
Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 282 | } else { |
| 283 | // Use default section depending on the 'type' of global |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 284 | S = SelectSectionForGlobal(GV); |
Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Anton Korobeynikov | c25e1ea | 2008-09-24 22:14:23 +0000 | [diff] [blame] | 287 | return S; |
Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // Lame default implementation. Calculate the section name for global. |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 291 | const Section* |
| 292 | TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const { |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 293 | SectionKind::Kind Kind = SectionKindForGlobal(GV); |
Anton Korobeynikov | 0c60246 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 294 | |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 295 | if (GV->isWeakForLinker()) { |
| 296 | std::string Name = UniqueSectionForGlobal(GV, Kind); |
| 297 | unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str()); |
| 298 | return getNamedSection(Name.c_str(), Flags); |
| 299 | } else { |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 300 | if (Kind == SectionKind::Text) |
Anton Korobeynikov | d7ca416 | 2008-09-24 22:15:21 +0000 | [diff] [blame] | 301 | return getTextSection(); |
Anton Korobeynikov | 4578862 | 2008-08-07 09:51:54 +0000 | [diff] [blame] | 302 | else if (isBSS(Kind) && getBSSSection_()) |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 303 | return getBSSSection_(); |
Anton Korobeynikov | 4578862 | 2008-08-07 09:51:54 +0000 | [diff] [blame] | 304 | else if (getReadOnlySection_() && SectionKind::isReadOnly(Kind)) |
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 | 315690e | 2008-09-24 22:16:16 +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 | |
Anton Korobeynikov | 93cacf1 | 2008-08-07 09:50:34 +0000 | [diff] [blame] | 311 | // Lame default implementation. Calculate the section name for machine const. |
| 312 | const Section* |
| 313 | TargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const { |
| 314 | // FIXME: Support data.rel stuff someday |
Anton Korobeynikov | 315690e | 2008-09-24 22:16:16 +0000 | [diff] [blame^] | 315 | return getDataSection(); |
Anton Korobeynikov | 93cacf1 | 2008-08-07 09:50:34 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 318 | std::string |
| 319 | TargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV, |
Anton Korobeynikov | b20015b | 2008-07-09 13:25:26 +0000 | [diff] [blame] | 320 | SectionKind::Kind Kind) const { |
| 321 | switch (Kind) { |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 322 | case SectionKind::Text: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 323 | return ".gnu.linkonce.t." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 324 | case SectionKind::Data: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 325 | return ".gnu.linkonce.d." + GV->getName(); |
Anton Korobeynikov | 04dda73 | 2008-07-22 17:09:59 +0000 | [diff] [blame] | 326 | case SectionKind::SmallData: |
| 327 | return ".gnu.linkonce.s." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 328 | case SectionKind::BSS: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 329 | return ".gnu.linkonce.b." + GV->getName(); |
Anton Korobeynikov | 04dda73 | 2008-07-22 17:09:59 +0000 | [diff] [blame] | 330 | case SectionKind::SmallBSS: |
| 331 | return ".gnu.linkonce.sb." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 332 | case SectionKind::ROData: |
| 333 | case SectionKind::RODataMergeConst: |
| 334 | case SectionKind::RODataMergeStr: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 335 | return ".gnu.linkonce.r." + GV->getName(); |
Anton Korobeynikov | 04dda73 | 2008-07-22 17:09:59 +0000 | [diff] [blame] | 336 | case SectionKind::SmallROData: |
| 337 | return ".gnu.linkonce.s2." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 338 | case SectionKind::ThreadData: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 339 | return ".gnu.linkonce.td." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 340 | case SectionKind::ThreadBSS: |
Anton Korobeynikov | 5248670 | 2008-07-09 13:24:55 +0000 | [diff] [blame] | 341 | return ".gnu.linkonce.tb." + GV->getName(); |
Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 342 | default: |
| 343 | assert(0 && "Unknown section kind"); |
| 344 | } |
| 345 | } |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 346 | |
| 347 | const Section* |
Anton Korobeynikov | 328da65 | 2008-08-16 12:58:12 +0000 | [diff] [blame] | 348 | TargetAsmInfo::getNamedSection(const char *Name, unsigned Flags, |
| 349 | bool Override) const { |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 350 | Section& S = Sections[Name]; |
| 351 | |
| 352 | // This is newly-created section, set it up properly. |
Anton Korobeynikov | 328da65 | 2008-08-16 12:58:12 +0000 | [diff] [blame] | 353 | if (S.Flags == SectionFlags::Invalid || Override) { |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 354 | S.Flags = Flags | SectionFlags::Named; |
| 355 | S.Name = Name; |
| 356 | } |
| 357 | |
| 358 | return &S; |
| 359 | } |
| 360 | |
| 361 | const Section* |
Anton Korobeynikov | 328da65 | 2008-08-16 12:58:12 +0000 | [diff] [blame] | 362 | TargetAsmInfo::getUnnamedSection(const char *Directive, unsigned Flags, |
| 363 | bool Override) const { |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 364 | Section& S = Sections[Directive]; |
| 365 | |
| 366 | // This is newly-created section, set it up properly. |
Anton Korobeynikov | 328da65 | 2008-08-16 12:58:12 +0000 | [diff] [blame] | 367 | if (S.Flags == SectionFlags::Invalid || Override) { |
Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 368 | S.Flags = Flags & ~SectionFlags::Named; |
| 369 | S.Name = Directive; |
| 370 | } |
| 371 | |
| 372 | return &S; |
| 373 | } |
Anton Korobeynikov | d0c1e29 | 2008-08-16 12:57:07 +0000 | [diff] [blame] | 374 | |
| 375 | const std::string& |
| 376 | TargetAsmInfo::getSectionFlags(unsigned Flags) const { |
| 377 | SectionFlags::FlagsStringsMapType::iterator I = FlagsStrings.find(Flags); |
| 378 | |
| 379 | // We didn't print these flags yet, print and save them to map. This reduces |
| 380 | // amount of heap trashing due to std::string construction / concatenation. |
| 381 | if (I == FlagsStrings.end()) |
| 382 | I = FlagsStrings.insert(std::make_pair(Flags, |
| 383 | printSectionFlags(Flags))).first; |
| 384 | |
| 385 | return I->second; |
| 386 | } |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 387 | |
| 388 | unsigned TargetAsmInfo::getULEB128Size(unsigned Value) { |
| 389 | unsigned Size = 0; |
| 390 | do { |
| 391 | Value >>= 7; |
| 392 | Size += sizeof(int8_t); |
| 393 | } while (Value); |
| 394 | return Size; |
| 395 | } |
| 396 | |
| 397 | unsigned TargetAsmInfo::getSLEB128Size(int Value) { |
| 398 | unsigned Size = 0; |
| 399 | int Sign = Value >> (8 * sizeof(Value) - 1); |
| 400 | bool IsMore; |
| 401 | |
| 402 | do { |
| 403 | unsigned Byte = Value & 0x7f; |
| 404 | Value >>= 7; |
| 405 | IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; |
| 406 | Size += sizeof(int8_t); |
| 407 | } while (IsMore); |
| 408 | return Size; |
| 409 | } |