| 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" | 
| Chris Lattner | 8ca5209 | 2009-07-26 06:11:33 +0000 | [diff] [blame^] | 22 | #include "llvm/Target/TargetData.h" | 
| Anton Korobeynikov | db23231 | 2009-03-29 17:13:49 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" | 
| Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetOptions.h" | 
| Anton Korobeynikov | cee750f | 2008-02-27 23:33:50 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Dwarf.h" | 
| Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" | 
| Dale Johannesen | 3bb6283 | 2007-04-23 20:00:17 +0000 | [diff] [blame] | 27 | #include <cctype> | 
|  | 28 | #include <cstring> | 
| Jim Laskey | ec0d9fe | 2006-09-06 18:35:33 +0000 | [diff] [blame] | 29 | using namespace llvm; | 
|  | 30 |  | 
| Chris Lattner | 0d4960c3 | 2009-07-24 20:14:10 +0000 | [diff] [blame] | 31 | TargetAsmInfo::TargetAsmInfo(const TargetMachine &tm) : TM(tm) { | 
| Anton Korobeynikov | 32b952a | 2008-09-25 21:00:33 +0000 | [diff] [blame] | 32 | BSSSection = "\t.bss"; | 
|  | 33 | BSSSection_ = 0; | 
|  | 34 | ReadOnlySection = 0; | 
| Anton Korobeynikov | 32b952a | 2008-09-25 21:00:33 +0000 | [diff] [blame] | 35 | TLSDataSection = 0; | 
|  | 36 | TLSBSSSection = 0; | 
|  | 37 | ZeroFillDirective = 0; | 
|  | 38 | NonexecutableStackDirective = 0; | 
|  | 39 | NeedsSet = false; | 
|  | 40 | MaxInstLength = 4; | 
|  | 41 | PCSymbol = "$"; | 
|  | 42 | SeparatorChar = ';'; | 
| David Greene | 014700c | 2009-07-13 20:25:48 +0000 | [diff] [blame] | 43 | CommentColumn = 60; | 
| Anton Korobeynikov | 32b952a | 2008-09-25 21:00:33 +0000 | [diff] [blame] | 44 | CommentString = "#"; | 
| David Greene | 76081c4 | 2009-07-20 22:02:59 +0000 | [diff] [blame] | 45 | FirstOperandColumn = 0; | 
|  | 46 | MaxOperandLength = 0; | 
| Anton Korobeynikov | 32b952a | 2008-09-25 21:00:33 +0000 | [diff] [blame] | 47 | GlobalPrefix = ""; | 
|  | 48 | PrivateGlobalPrefix = "."; | 
| Chris Lattner | 90f8b70 | 2009-07-21 17:30:51 +0000 | [diff] [blame] | 49 | LinkerPrivateGlobalPrefix = ""; | 
| Anton Korobeynikov | 32b952a | 2008-09-25 21:00:33 +0000 | [diff] [blame] | 50 | JumpTableSpecialLabelPrefix = 0; | 
|  | 51 | GlobalVarAddrPrefix = ""; | 
|  | 52 | GlobalVarAddrSuffix = ""; | 
|  | 53 | FunctionAddrPrefix = ""; | 
|  | 54 | FunctionAddrSuffix = ""; | 
|  | 55 | PersonalityPrefix = ""; | 
|  | 56 | PersonalitySuffix = ""; | 
|  | 57 | NeedsIndirectEncoding = false; | 
|  | 58 | InlineAsmStart = "#APP"; | 
|  | 59 | InlineAsmEnd = "#NO_APP"; | 
|  | 60 | AssemblerDialect = 0; | 
| Chris Lattner | a93ca92 | 2009-06-18 23:41:35 +0000 | [diff] [blame] | 61 | AllowQuotesInName = false; | 
| Anton Korobeynikov | 32b952a | 2008-09-25 21:00: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; | 
|  | 72 | TextAlignFillValue = 0; | 
|  | 73 | SwitchToSectionDirective = "\t.section\t"; | 
|  | 74 | TextSectionStartSuffix = ""; | 
|  | 75 | DataSectionStartSuffix = ""; | 
|  | 76 | SectionEndDirectiveSuffix = 0; | 
|  | 77 | ConstantPoolSection = "\t.section .rodata"; | 
|  | 78 | JumpTableDataSection = "\t.section .rodata"; | 
|  | 79 | JumpTableDirective = 0; | 
|  | 80 | CStringSection = 0; | 
|  | 81 | CStringSection_ = 0; | 
|  | 82 | // FIXME: Flags are ELFish - replace with normal section stuff. | 
|  | 83 | StaticCtorsSection = "\t.section .ctors,\"aw\",@progbits"; | 
|  | 84 | StaticDtorsSection = "\t.section .dtors,\"aw\",@progbits"; | 
|  | 85 | GlobalDirective = "\t.globl\t"; | 
|  | 86 | SetDirective = 0; | 
|  | 87 | LCOMMDirective = 0; | 
|  | 88 | COMMDirective = "\t.comm\t"; | 
|  | 89 | COMMDirectiveTakesAlignment = true; | 
|  | 90 | HasDotTypeDotSizeDirective = true; | 
| Rafael Espindola | 952b839 | 2008-12-03 11:01:37 +0000 | [diff] [blame] | 91 | HasSingleParameterDotFile = true; | 
| Anton Korobeynikov | 32b952a | 2008-09-25 21:00:33 +0000 | [diff] [blame] | 92 | UsedDirective = 0; | 
|  | 93 | WeakRefDirective = 0; | 
|  | 94 | WeakDefDirective = 0; | 
|  | 95 | // FIXME: These are ELFish - move to ELFTAI. | 
|  | 96 | HiddenDirective = "\t.hidden\t"; | 
|  | 97 | ProtectedDirective = "\t.protected\t"; | 
|  | 98 | AbsoluteDebugSectionOffsets = false; | 
|  | 99 | AbsoluteEHSectionOffsets = false; | 
|  | 100 | HasLEB128 = false; | 
|  | 101 | HasDotLocAndDotFile = false; | 
|  | 102 | SupportsDebugInformation = false; | 
|  | 103 | SupportsExceptionHandling = false; | 
|  | 104 | DwarfRequiresFrameSection = true; | 
| Devang Patel | 0f7fef3 | 2009-04-13 17:02:03 +0000 | [diff] [blame] | 105 | DwarfUsesInlineInfoSection = false; | 
| Chris Lattner | e2cf37b | 2009-07-17 20:46:40 +0000 | [diff] [blame] | 106 | Is_EHSymbolPrivate = true; | 
| Anton Korobeynikov | 32b952a | 2008-09-25 21:00:33 +0000 | [diff] [blame] | 107 | GlobalEHDirective = 0; | 
|  | 108 | SupportsWeakOmittedEHFrame = true; | 
|  | 109 | DwarfSectionOffsetDirective = 0; | 
|  | 110 | DwarfAbbrevSection = ".debug_abbrev"; | 
|  | 111 | DwarfInfoSection = ".debug_info"; | 
|  | 112 | DwarfLineSection = ".debug_line"; | 
|  | 113 | DwarfFrameSection = ".debug_frame"; | 
|  | 114 | DwarfPubNamesSection = ".debug_pubnames"; | 
|  | 115 | DwarfPubTypesSection = ".debug_pubtypes"; | 
| Devang Patel | 0f7fef3 | 2009-04-13 17:02:03 +0000 | [diff] [blame] | 116 | DwarfDebugInlineSection = ".debug_inlined"; | 
| Anton Korobeynikov | 32b952a | 2008-09-25 21:00:33 +0000 | [diff] [blame] | 117 | DwarfStrSection = ".debug_str"; | 
|  | 118 | DwarfLocSection = ".debug_loc"; | 
|  | 119 | DwarfARangesSection = ".debug_aranges"; | 
|  | 120 | DwarfRangesSection = ".debug_ranges"; | 
| Chris Lattner | b839c3f | 2009-06-18 23:31:37 +0000 | [diff] [blame] | 121 | DwarfMacroInfoSection = ".debug_macinfo"; | 
| Anton Korobeynikov | 32b952a | 2008-09-25 21:00:33 +0000 | [diff] [blame] | 122 | DwarfEHFrameSection = ".eh_frame"; | 
|  | 123 | DwarfExceptionSection = ".gcc_except_table"; | 
|  | 124 | AsmTransCBE = 0; | 
| Anton Korobeynikov | d7ca416 | 2008-09-24 22:15:21 +0000 | [diff] [blame] | 125 | TextSection = getUnnamedSection("\t.text", SectionFlags::Code); | 
| Chris Lattner | b4fc419 | 2009-07-25 18:57:34 +0000 | [diff] [blame] | 126 | DataSection = getUnnamedSection("\t.data", SectionFlags::Writable); | 
| 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) { | 
| Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 164 | Constant *C = GV->getInitializer(); | 
| Chris Lattner | b4fc419 | 2009-07-25 18:57:34 +0000 | [diff] [blame] | 165 |  | 
|  | 166 | // Must have zero initializer. | 
|  | 167 | if (!C->isNullValue()) | 
|  | 168 | return false; | 
|  | 169 |  | 
|  | 170 | // Leave constant zeros in readonly constant sections, so they can be shared. | 
|  | 171 | if (GV->isConstant()) | 
|  | 172 | return false; | 
|  | 173 |  | 
|  | 174 | // If the global has an explicit section specified, don't put it in BSS. | 
|  | 175 | if (!GV->getSection().empty()) | 
|  | 176 | return false; | 
|  | 177 |  | 
| Chris Lattner | 460d51e | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 178 | // If -nozero-initialized-in-bss is specified, don't ever use BSS. | 
|  | 179 | if (NoZerosInBSS) | 
|  | 180 | return false; | 
|  | 181 |  | 
|  | 182 | // Otherwise, put it in BSS! | 
|  | 183 | return true; | 
| Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 184 | } | 
|  | 185 |  | 
| Owen Anderson | 1ca29d3 | 2009-07-13 21:27:19 +0000 | [diff] [blame] | 186 | static bool isConstantString(const Constant *C) { | 
| Anton Korobeynikov | 72bb402 | 2009-01-27 22:29:24 +0000 | [diff] [blame] | 187 | // First check: is we have constant array of i8 terminated with zero | 
|  | 188 | const ConstantArray *CVA = dyn_cast<ConstantArray>(C); | 
|  | 189 | // Check, if initializer is a null-terminated string | 
| Owen Anderson | 1ca29d3 | 2009-07-13 21:27:19 +0000 | [diff] [blame] | 190 | if (CVA && CVA->isCString()) | 
| Anton Korobeynikov | 72bb402 | 2009-01-27 22:29:24 +0000 | [diff] [blame] | 191 | return true; | 
|  | 192 |  | 
|  | 193 | // Another possibility: [1 x i8] zeroinitializer | 
| Chris Lattner | 0d4960c3 | 2009-07-24 20:14:10 +0000 | [diff] [blame] | 194 | if (isa<ConstantAggregateZero>(C)) | 
|  | 195 | if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType())) | 
| Anton Korobeynikov | 72bb402 | 2009-01-27 22:29:24 +0000 | [diff] [blame] | 196 | return (Ty->getElementType() == Type::Int8Ty && | 
|  | 197 | Ty->getNumElements() == 1); | 
| Anton Korobeynikov | 72bb402 | 2009-01-27 22:29:24 +0000 | [diff] [blame] | 198 |  | 
|  | 199 | return false; | 
|  | 200 | } | 
|  | 201 |  | 
| Chris Lattner | 8adc547 | 2009-07-24 16:46:50 +0000 | [diff] [blame] | 202 | static unsigned SectionFlagsForGlobal(const GlobalValue *GV, | 
| Chris Lattner | 460d51e | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 203 | SectionKind Kind) { | 
| Chris Lattner | b4fc419 | 2009-07-25 18:57:34 +0000 | [diff] [blame] | 204 | // Decode flags from global and section kind. | 
| Chris Lattner | 104bbd1 | 2009-07-24 16:44:01 +0000 | [diff] [blame] | 205 | unsigned Flags = SectionFlags::None; | 
| Chris Lattner | 104bbd1 | 2009-07-24 16:44:01 +0000 | [diff] [blame] | 206 | if (GV->isWeakForLinker()) | 
|  | 207 | Flags |= SectionFlags::Linkonce; | 
| Chris Lattner | e346e18 | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 208 | if (Kind.isBSS() || Kind.isThreadBSS()) | 
| Chris Lattner | b4fc419 | 2009-07-25 18:57:34 +0000 | [diff] [blame] | 209 | Flags |= SectionFlags::BSS; | 
| Chris Lattner | e346e18 | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 210 | if (Kind.isThreadLocal()) | 
| Chris Lattner | b4fc419 | 2009-07-25 18:57:34 +0000 | [diff] [blame] | 211 | Flags |= SectionFlags::TLS; | 
| Chris Lattner | e346e18 | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 212 | if (Kind.isText()) | 
| Chris Lattner | b4fc419 | 2009-07-25 18:57:34 +0000 | [diff] [blame] | 213 | Flags |= SectionFlags::Code; | 
| Chris Lattner | 460d51e | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 214 | if (Kind.isWritable()) | 
| Chris Lattner | b4fc419 | 2009-07-25 18:57:34 +0000 | [diff] [blame] | 215 | Flags |= SectionFlags::Writable; | 
| Chris Lattner | 104bbd1 | 2009-07-24 16:44:01 +0000 | [diff] [blame] | 216 |  | 
| Chris Lattner | 27f3065 | 2009-07-24 16:50:24 +0000 | [diff] [blame] | 217 | return Flags; | 
|  | 218 | } | 
| Chris Lattner | 104bbd1 | 2009-07-24 16:44:01 +0000 | [diff] [blame] | 219 |  | 
| Chris Lattner | 460d51e | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 220 | static SectionKind SectionKindForGlobal(const GlobalValue *GV, | 
| Chris Lattner | 8ca5209 | 2009-07-26 06:11:33 +0000 | [diff] [blame^] | 221 | const TargetMachine &TM) { | 
|  | 222 | Reloc::Model ReloModel = TM.getRelocationModel(); | 
|  | 223 |  | 
| Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 224 | // Early exit - functions should be always in text sections. | 
| Chris Lattner | e5ca0ac | 2009-07-24 16:53:52 +0000 | [diff] [blame] | 225 | const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); | 
|  | 226 | if (GVar == 0) | 
| Chris Lattner | 460d51e | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 227 | return SectionKind::getText(); | 
| Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 228 |  | 
| Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 229 | bool isThreadLocal = GVar->isThreadLocal(); | 
| Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 230 |  | 
| Chris Lattner | b303504 | 2009-07-24 19:15:47 +0000 | [diff] [blame] | 231 | // Variable can be easily put to BSS section. | 
|  | 232 | if (isSuitableForBSS(GVar)) | 
| Chris Lattner | 460d51e | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 233 | return isThreadLocal ? SectionKind::getThreadBSS() : SectionKind::getBSS(); | 
| Chris Lattner | b303504 | 2009-07-24 19:15:47 +0000 | [diff] [blame] | 234 |  | 
|  | 235 | // If this is thread-local, put it in the general "thread_data" section. | 
|  | 236 | if (isThreadLocal) | 
| Chris Lattner | 460d51e | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 237 | return SectionKind::getThreadData(); | 
| Chris Lattner | b303504 | 2009-07-24 19:15:47 +0000 | [diff] [blame] | 238 |  | 
|  | 239 | Constant *C = GVar->getInitializer(); | 
|  | 240 |  | 
|  | 241 | // If the global is marked constant, we can put it into a mergable section, | 
|  | 242 | // a mergable string section, or general .data if it contains relocations. | 
|  | 243 | if (GVar->isConstant()) { | 
|  | 244 | // If the initializer for the global contains something that requires a | 
|  | 245 | // relocation, then we may have to drop this into a wriable data section | 
|  | 246 | // even though it is marked const. | 
|  | 247 | switch (C->getRelocationInfo()) { | 
|  | 248 | default: llvm_unreachable("unknown relocation info kind"); | 
|  | 249 | case Constant::NoRelocation: | 
|  | 250 | // If initializer is a null-terminated string, put it in a "cstring" | 
|  | 251 | // section if the target has it. | 
| Owen Anderson | 1ca29d3 | 2009-07-13 21:27:19 +0000 | [diff] [blame] | 252 | if (isConstantString(C)) | 
| Chris Lattner | e346e18 | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 253 | return SectionKind::getMergableCString(); | 
| Chris Lattner | b303504 | 2009-07-24 19:15:47 +0000 | [diff] [blame] | 254 |  | 
| Chris Lattner | 8ca5209 | 2009-07-26 06:11:33 +0000 | [diff] [blame^] | 255 | // Otherwise, just drop it into a mergable constant section.  If we have | 
|  | 256 | // a section for this size, use it, otherwise use the arbitrary sized | 
|  | 257 | // mergable section. | 
|  | 258 | switch (TM.getTargetData()->getTypeAllocSize(C->getType())) { | 
|  | 259 | case 4:  return SectionKind::getMergableConst4(); | 
|  | 260 | case 8:  return SectionKind::getMergableConst8(); | 
|  | 261 | case 16: return SectionKind::getMergableConst16(); | 
|  | 262 | default: return SectionKind::getMergableConst(); | 
|  | 263 | } | 
| Chris Lattner | b303504 | 2009-07-24 19:15:47 +0000 | [diff] [blame] | 264 |  | 
|  | 265 | case Constant::LocalRelocation: | 
|  | 266 | // In static relocation model, the linker will resolve all addresses, so | 
|  | 267 | // the relocation entries will actually be constants by the time the app | 
|  | 268 | // starts up. | 
| Chris Lattner | 0d4960c3 | 2009-07-24 20:14:10 +0000 | [diff] [blame] | 269 | if (ReloModel == Reloc::Static) | 
| Chris Lattner | e346e18 | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 270 | return SectionKind::getReadOnly(); | 
| Chris Lattner | b303504 | 2009-07-24 19:15:47 +0000 | [diff] [blame] | 271 |  | 
|  | 272 | // Otherwise, the dynamic linker needs to fix it up, put it in the | 
|  | 273 | // writable data.rel.local section. | 
| Chris Lattner | e346e18 | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 274 | return SectionKind::getReadOnlyWithRelLocal(); | 
| Chris Lattner | b303504 | 2009-07-24 19:15:47 +0000 | [diff] [blame] | 275 |  | 
|  | 276 | case Constant::GlobalRelocations: | 
|  | 277 | // In static relocation model, the linker will resolve all addresses, so | 
|  | 278 | // the relocation entries will actually be constants by the time the app | 
|  | 279 | // starts up. | 
| Chris Lattner | 0d4960c3 | 2009-07-24 20:14:10 +0000 | [diff] [blame] | 280 | if (ReloModel == Reloc::Static) | 
| Chris Lattner | e346e18 | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 281 | return SectionKind::getReadOnly(); | 
| Chris Lattner | b303504 | 2009-07-24 19:15:47 +0000 | [diff] [blame] | 282 |  | 
|  | 283 | // Otherwise, the dynamic linker needs to fix it up, put it in the | 
|  | 284 | // writable data.rel section. | 
| Chris Lattner | e346e18 | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 285 | return SectionKind::getReadOnlyWithRel(); | 
| Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 286 | } | 
|  | 287 | } | 
|  | 288 |  | 
| Chris Lattner | b303504 | 2009-07-24 19:15:47 +0000 | [diff] [blame] | 289 | // Okay, this isn't a constant.  If the initializer for the global is going | 
|  | 290 | // to require a runtime relocation by the dynamic linker, put it into a more | 
|  | 291 | // specific section to improve startup time of the app.  This coalesces these | 
|  | 292 | // globals together onto fewer pages, improving the locality of the dynamic | 
|  | 293 | // linker. | 
| Chris Lattner | 0d4960c3 | 2009-07-24 20:14:10 +0000 | [diff] [blame] | 294 | if (ReloModel == Reloc::Static) | 
| Chris Lattner | e346e18 | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 295 | return SectionKind::getDataNoRel(); | 
| Chris Lattner | b303504 | 2009-07-24 19:15:47 +0000 | [diff] [blame] | 296 |  | 
|  | 297 | switch (C->getRelocationInfo()) { | 
|  | 298 | default: llvm_unreachable("unknown relocation info kind"); | 
| Chris Lattner | e346e18 | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 299 | case Constant::NoRelocation:      return SectionKind::getDataNoRel(); | 
| Chris Lattner | 460d51e | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 300 | case Constant::LocalRelocation:   return SectionKind::getDataRelLocal(); | 
|  | 301 | case Constant::GlobalRelocations: return SectionKind::getDataRel(); | 
| Chris Lattner | b303504 | 2009-07-24 19:15:47 +0000 | [diff] [blame] | 302 | } | 
| Anton Korobeynikov | 28a2b54 | 2008-06-28 13:45:57 +0000 | [diff] [blame] | 303 | } | 
| Anton Korobeynikov | beb9d40 | 2008-07-09 13:16:59 +0000 | [diff] [blame] | 304 |  | 
| Chris Lattner | 4f05591 | 2009-07-25 18:11:58 +0000 | [diff] [blame] | 305 | /// SectionForGlobal - This method computes the appropriate section to emit | 
|  | 306 | /// the specified global variable or function definition.  This should not | 
|  | 307 | /// be passed external (or available externally) globals. | 
| Chris Lattner | 7558f11 | 2009-07-21 21:09:35 +0000 | [diff] [blame] | 308 | const Section *TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const { | 
| Chris Lattner | 4f05591 | 2009-07-25 18:11:58 +0000 | [diff] [blame] | 309 | assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() && | 
|  | 310 | "Can only be used for global definitions"); | 
|  | 311 |  | 
| Chris Lattner | 8ca5209 | 2009-07-26 06:11:33 +0000 | [diff] [blame^] | 312 | SectionKind Kind = SectionKindForGlobal(GV, TM); | 
| Chris Lattner | 7420ab2 | 2009-07-24 18:34:27 +0000 | [diff] [blame] | 313 |  | 
|  | 314 | // Select section name. | 
| Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 315 | if (GV->hasSection()) { | 
| Chris Lattner | 7420ab2 | 2009-07-24 18:34:27 +0000 | [diff] [blame] | 316 | // If the target has special section hacks for specifically named globals, | 
|  | 317 | // return them now. | 
|  | 318 | if (const Section *TS = getSpecialCasedSectionGlobals(GV, Kind)) | 
|  | 319 | return TS; | 
|  | 320 |  | 
| Chris Lattner | 7558f11 | 2009-07-21 21:09:35 +0000 | [diff] [blame] | 321 | // Honour section already set, if any. | 
| Chris Lattner | 7420ab2 | 2009-07-24 18:34:27 +0000 | [diff] [blame] | 322 | unsigned Flags = SectionFlagsForGlobal(GV, Kind); | 
| Chris Lattner | 27f3065 | 2009-07-24 16:50:24 +0000 | [diff] [blame] | 323 |  | 
|  | 324 | // This is an explicitly named section. | 
|  | 325 | Flags |= SectionFlags::Named; | 
|  | 326 |  | 
|  | 327 | // If the target has magic semantics for certain section names, make sure to | 
|  | 328 | // pick up the flags.  This allows the user to write things with attribute | 
|  | 329 | // section and still get the appropriate section flags printed. | 
| Chris Lattner | 2dcafe4 | 2009-07-24 17:02:17 +0000 | [diff] [blame] | 330 | Flags |= getFlagsForNamedSection(GV->getSection().c_str()); | 
| Chris Lattner | 27f3065 | 2009-07-24 16:50:24 +0000 | [diff] [blame] | 331 |  | 
| Chris Lattner | 7558f11 | 2009-07-21 21:09:35 +0000 | [diff] [blame] | 332 | return getNamedSection(GV->getSection().c_str(), Flags); | 
| Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 333 | } | 
| Daniel Dunbar | 7384e1b | 2009-07-22 20:26:37 +0000 | [diff] [blame] | 334 |  | 
| Chris Lattner | eed05b8 | 2009-07-24 05:10:25 +0000 | [diff] [blame] | 335 | // If this global is linkonce/weak and the target handles this by emitting it | 
|  | 336 | // into a 'uniqued' section name, create and return the section now. | 
|  | 337 | if (GV->isWeakForLinker()) { | 
| Chris Lattner | 7420ab2 | 2009-07-24 18:34:27 +0000 | [diff] [blame] | 338 | if (const char *Prefix = getSectionPrefixForUniqueGlobal(Kind)) { | 
|  | 339 | unsigned Flags = SectionFlagsForGlobal(GV, Kind); | 
| Chris Lattner | e5ca0ac | 2009-07-24 16:53:52 +0000 | [diff] [blame] | 340 |  | 
| Chris Lattner | eed05b8 | 2009-07-24 05:10:25 +0000 | [diff] [blame] | 341 | // FIXME: Use mangler interface (PR4584). | 
| Daniel Dunbar | 895c9ed | 2009-07-24 12:21:08 +0000 | [diff] [blame] | 342 | std::string Name = Prefix+GV->getNameStr(); | 
| Chris Lattner | eed05b8 | 2009-07-24 05:10:25 +0000 | [diff] [blame] | 343 | return getNamedSection(Name.c_str(), Flags); | 
|  | 344 | } | 
|  | 345 | } | 
|  | 346 |  | 
| Chris Lattner | 7558f11 | 2009-07-21 21:09:35 +0000 | [diff] [blame] | 347 | // Use default section depending on the 'type' of global | 
| Chris Lattner | f20f250 | 2009-07-24 18:42:53 +0000 | [diff] [blame] | 348 | return SelectSectionForGlobal(GV, Kind); | 
| Anton Korobeynikov | 265c525 | 2008-07-09 13:22:46 +0000 | [diff] [blame] | 349 | } | 
|  | 350 |  | 
|  | 351 | // Lame default implementation. Calculate the section name for global. | 
| Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 352 | const Section* | 
| Chris Lattner | f20f250 | 2009-07-24 18:42:53 +0000 | [diff] [blame] | 353 | TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV, | 
| Chris Lattner | 460d51e | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 354 | SectionKind Kind) const { | 
| Chris Lattner | e346e18 | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 355 | assert(!Kind.isThreadLocal() && "Doesn't support TLS"); | 
|  | 356 |  | 
|  | 357 | if (Kind.isText()) | 
| Chris Lattner | eed05b8 | 2009-07-24 05:10:25 +0000 | [diff] [blame] | 358 | return getTextSection(); | 
|  | 359 |  | 
| Chris Lattner | 460d51e | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 360 | if (Kind.isBSS()) | 
| Chris Lattner | eed05b8 | 2009-07-24 05:10:25 +0000 | [diff] [blame] | 361 | if (const Section *S = getBSSSection_()) | 
|  | 362 | return S; | 
|  | 363 |  | 
| Chris Lattner | 460d51e | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 364 | if (Kind.isReadOnly()) | 
| Chris Lattner | eed05b8 | 2009-07-24 05:10:25 +0000 | [diff] [blame] | 365 | if (const Section *S = getReadOnlySection()) | 
|  | 366 | return S; | 
| Anton Korobeynikov | 0c60246 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 367 |  | 
| Anton Korobeynikov | 315690e | 2008-09-24 22:16:16 +0000 | [diff] [blame] | 368 | return getDataSection(); | 
| Anton Korobeynikov | 0c60246 | 2008-07-09 13:18:02 +0000 | [diff] [blame] | 369 | } | 
| Anton Korobeynikov | 29b03f7 | 2008-07-09 13:19:38 +0000 | [diff] [blame] | 370 |  | 
| Chris Lattner | 298414e | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 371 | /// getSectionForMergableConstant - Given a mergable constant with the | 
|  | 372 | /// specified size and relocation information, return a section that it | 
|  | 373 | /// should be placed in. | 
|  | 374 | const Section * | 
|  | 375 | TargetAsmInfo::getSectionForMergableConstant(uint64_t Size, | 
|  | 376 | unsigned ReloInfo) const { | 
| Chris Lattner | 7f88a20 | 2009-07-26 05:57:07 +0000 | [diff] [blame] | 377 | if (ReloInfo == 0) | 
|  | 378 | if (const Section *S = getReadOnlySection()) | 
|  | 379 | return S; | 
|  | 380 |  | 
| Anton Korobeynikov | 315690e | 2008-09-24 22:16:16 +0000 | [diff] [blame] | 381 | return getDataSection(); | 
| Anton Korobeynikov | 93cacf1 | 2008-08-07 09:50:34 +0000 | [diff] [blame] | 382 | } | 
|  | 383 |  | 
| Chris Lattner | 298414e | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 384 |  | 
| Chris Lattner | 7558f11 | 2009-07-21 21:09:35 +0000 | [diff] [blame] | 385 | const Section *TargetAsmInfo::getNamedSection(const char *Name, unsigned Flags, | 
|  | 386 | bool Override) const { | 
|  | 387 | Section &S = Sections[Name]; | 
| Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 388 |  | 
|  | 389 | // This is newly-created section, set it up properly. | 
| Anton Korobeynikov | 328da65 | 2008-08-16 12:58:12 +0000 | [diff] [blame] | 390 | if (S.Flags == SectionFlags::Invalid || Override) { | 
| Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 391 | S.Flags = Flags | SectionFlags::Named; | 
|  | 392 | S.Name = Name; | 
|  | 393 | } | 
|  | 394 |  | 
|  | 395 | return &S; | 
|  | 396 | } | 
|  | 397 |  | 
|  | 398 | const Section* | 
| Anton Korobeynikov | 328da65 | 2008-08-16 12:58:12 +0000 | [diff] [blame] | 399 | TargetAsmInfo::getUnnamedSection(const char *Directive, unsigned Flags, | 
|  | 400 | bool Override) const { | 
| Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 401 | Section& S = Sections[Directive]; | 
|  | 402 |  | 
|  | 403 | // This is newly-created section, set it up properly. | 
| Anton Korobeynikov | 328da65 | 2008-08-16 12:58:12 +0000 | [diff] [blame] | 404 | if (S.Flags == SectionFlags::Invalid || Override) { | 
| Anton Korobeynikov | 0d44ba8 | 2008-07-09 13:28:49 +0000 | [diff] [blame] | 405 | S.Flags = Flags & ~SectionFlags::Named; | 
|  | 406 | S.Name = Directive; | 
|  | 407 | } | 
|  | 408 |  | 
|  | 409 | return &S; | 
|  | 410 | } | 
| Anton Korobeynikov | d0c1e29 | 2008-08-16 12:57:07 +0000 | [diff] [blame] | 411 |  | 
|  | 412 | const std::string& | 
|  | 413 | TargetAsmInfo::getSectionFlags(unsigned Flags) const { | 
|  | 414 | SectionFlags::FlagsStringsMapType::iterator I = FlagsStrings.find(Flags); | 
|  | 415 |  | 
|  | 416 | // We didn't print these flags yet, print and save them to map. This reduces | 
|  | 417 | // amount of heap trashing due to std::string construction / concatenation. | 
|  | 418 | if (I == FlagsStrings.end()) | 
|  | 419 | I = FlagsStrings.insert(std::make_pair(Flags, | 
|  | 420 | printSectionFlags(Flags))).first; | 
|  | 421 |  | 
|  | 422 | return I->second; | 
|  | 423 | } | 
| Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 424 |  | 
|  | 425 | unsigned TargetAsmInfo::getULEB128Size(unsigned Value) { | 
|  | 426 | unsigned Size = 0; | 
|  | 427 | do { | 
|  | 428 | Value >>= 7; | 
|  | 429 | Size += sizeof(int8_t); | 
|  | 430 | } while (Value); | 
|  | 431 | return Size; | 
|  | 432 | } | 
|  | 433 |  | 
|  | 434 | unsigned TargetAsmInfo::getSLEB128Size(int Value) { | 
|  | 435 | unsigned Size = 0; | 
|  | 436 | int Sign = Value >> (8 * sizeof(Value) - 1); | 
|  | 437 | bool IsMore; | 
|  | 438 |  | 
|  | 439 | do { | 
|  | 440 | unsigned Byte = Value & 0x7f; | 
|  | 441 | Value >>= 7; | 
|  | 442 | IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; | 
|  | 443 | Size += sizeof(int8_t); | 
|  | 444 | } while (IsMore); | 
|  | 445 | return Size; | 
|  | 446 | } |