blob: 18807f6530e7208ff9bb1825ce920ada117a789f [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- TargetAsmInfo.cpp - Asm Info ---------------------------------------==//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines target asm properties related what form asm statements
11// should take.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikovb4f658c2008-06-28 13:45:57 +000015#include "llvm/Constants.h"
asl4eeee012009-01-27 22:29:24 +000016#include "llvm/DerivedTypes.h"
Anton Korobeynikovb4f658c2008-06-28 13:45:57 +000017#include "llvm/GlobalVariable.h"
18#include "llvm/Function.h"
19#include "llvm/Module.h"
20#include "llvm/Type.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Target/TargetAsmInfo.h"
Chris Lattner2a7dd7d2009-07-26 06:11:33 +000022#include "llvm/Target/TargetData.h"
Anton Korobeynikov1dc008a2009-03-29 17:13:49 +000023#include "llvm/Target/TargetMachine.h"
Anton Korobeynikovb4f658c2008-06-28 13:45:57 +000024#include "llvm/Target/TargetOptions.h"
Anton Korobeynikov65c0d872008-02-27 23:33:50 +000025#include "llvm/Support/Dwarf.h"
Edwin Török675d5622009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include <cctype>
28#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029using namespace llvm;
30
Chris Lattner224957e2009-07-24 20:14:10 +000031TargetAsmInfo::TargetAsmInfo(const TargetMachine &tm) : TM(tm) {
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +000032 BSSSection = "\t.bss";
33 BSSSection_ = 0;
34 ReadOnlySection = 0;
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +000035 TLSDataSection = 0;
36 TLSBSSSection = 0;
37 ZeroFillDirective = 0;
38 NonexecutableStackDirective = 0;
39 NeedsSet = false;
40 MaxInstLength = 4;
41 PCSymbol = "$";
42 SeparatorChar = ';';
David Greene63486122009-07-13 20:25:48 +000043 CommentColumn = 60;
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +000044 CommentString = "#";
David Greene47974bf2009-07-20 22:02:59 +000045 FirstOperandColumn = 0;
46 MaxOperandLength = 0;
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +000047 GlobalPrefix = "";
48 PrivateGlobalPrefix = ".";
Chris Lattnerc889b3b2009-07-21 17:30:51 +000049 LinkerPrivateGlobalPrefix = "";
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +000050 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 Lattner690b02c2009-06-18 23:41:35 +000061 AllowQuotesInName = false;
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +000062 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 Espindola5cf2e552008-12-03 11:01:37 +000091 HasSingleParameterDotFile = true;
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +000092 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 Patel88bf96e2009-04-13 17:02:03 +0000105 DwarfUsesInlineInfoSection = false;
Chris Lattner5c3475e2009-07-17 20:46:40 +0000106 Is_EHSymbolPrivate = true;
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +0000107 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 Patel88bf96e2009-04-13 17:02:03 +0000116 DwarfDebugInlineSection = ".debug_inlined";
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +0000117 DwarfStrSection = ".debug_str";
118 DwarfLocSection = ".debug_loc";
119 DwarfARangesSection = ".debug_aranges";
120 DwarfRangesSection = ".debug_ranges";
Chris Lattnerecbf73b2009-06-18 23:31:37 +0000121 DwarfMacroInfoSection = ".debug_macinfo";
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +0000122 DwarfEHFrameSection = ".eh_frame";
123 DwarfExceptionSection = ".gcc_except_table";
124 AsmTransCBE = 0;
Anton Korobeynikov55b94962008-09-24 22:15:21 +0000125 TextSection = getUnnamedSection("\t.text", SectionFlags::Code);
Chris Lattner4c530ee2009-07-25 18:57:34 +0000126 DataSection = getUnnamedSection("\t.data", SectionFlags::Writable);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127}
128
129TargetAsmInfo::~TargetAsmInfo() {
130}
131
132/// Measure the specified inline asm to determine an approximation of its
133/// length.
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.
140unsigned TargetAsmInfo::getInlineAsmLength(const char *Str) const {
141 // Count the number of instructions in the asm.
142 bool atInsnStart = true;
143 unsigned Length = 0;
144 for (; *Str; ++Str) {
145 if (*Str == '\n' || *Str == SeparatorChar)
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;
153 }
154
155 return Length;
156}
157
Anton Korobeynikov8750d7c2008-02-29 22:09:08 +0000158unsigned TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
Anton Korobeynikov65c0d872008-02-27 23:33:50 +0000159 bool Global) const {
160 return dwarf::DW_EH_PE_absptr;
161}
162
Anton Korobeynikovb4f658c2008-06-28 13:45:57 +0000163static bool isSuitableForBSS(const GlobalVariable *GV) {
Anton Korobeynikovb4f658c2008-06-28 13:45:57 +0000164 Constant *C = GV->getInitializer();
Chris Lattner4c530ee2009-07-25 18:57:34 +0000165
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 Lattnercc195212009-07-25 23:21:55 +0000178 // 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 Korobeynikovb4f658c2008-06-28 13:45:57 +0000184}
185
Owen Anderson8d9397a2009-07-13 21:27:19 +0000186static bool isConstantString(const Constant *C) {
asl4eeee012009-01-27 22:29:24 +0000187 // 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 Anderson8d9397a2009-07-13 21:27:19 +0000190 if (CVA && CVA->isCString())
asl4eeee012009-01-27 22:29:24 +0000191 return true;
192
193 // Another possibility: [1 x i8] zeroinitializer
Chris Lattner224957e2009-07-24 20:14:10 +0000194 if (isa<ConstantAggregateZero>(C))
195 if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType()))
asl4eeee012009-01-27 22:29:24 +0000196 return (Ty->getElementType() == Type::Int8Ty &&
197 Ty->getNumElements() == 1);
asl4eeee012009-01-27 22:29:24 +0000198
199 return false;
200}
201
Chris Lattnerb0a50a52009-07-24 16:46:50 +0000202static unsigned SectionFlagsForGlobal(const GlobalValue *GV,
Chris Lattnercc195212009-07-25 23:21:55 +0000203 SectionKind Kind) {
Chris Lattner4c530ee2009-07-25 18:57:34 +0000204 // Decode flags from global and section kind.
Chris Lattner5925eb62009-07-24 16:44:01 +0000205 unsigned Flags = SectionFlags::None;
Chris Lattner5925eb62009-07-24 16:44:01 +0000206 if (GV->isWeakForLinker())
207 Flags |= SectionFlags::Linkonce;
Chris Lattnerdc9d8cf2009-07-26 05:44:20 +0000208 if (Kind.isBSS() || Kind.isThreadBSS())
Chris Lattner4c530ee2009-07-25 18:57:34 +0000209 Flags |= SectionFlags::BSS;
Chris Lattnerdc9d8cf2009-07-26 05:44:20 +0000210 if (Kind.isThreadLocal())
Chris Lattner4c530ee2009-07-25 18:57:34 +0000211 Flags |= SectionFlags::TLS;
Chris Lattnerdc9d8cf2009-07-26 05:44:20 +0000212 if (Kind.isText())
Chris Lattner4c530ee2009-07-25 18:57:34 +0000213 Flags |= SectionFlags::Code;
Chris Lattner1c379242009-07-26 06:48:26 +0000214 if (Kind.isWriteable())
Chris Lattner4c530ee2009-07-25 18:57:34 +0000215 Flags |= SectionFlags::Writable;
Chris Lattner5925eb62009-07-24 16:44:01 +0000216
Chris Lattner8f2dd992009-07-24 16:50:24 +0000217 return Flags;
218}
Chris Lattner5925eb62009-07-24 16:44:01 +0000219
Chris Lattnercc195212009-07-25 23:21:55 +0000220static SectionKind SectionKindForGlobal(const GlobalValue *GV,
Chris Lattner2a7dd7d2009-07-26 06:11:33 +0000221 const TargetMachine &TM) {
222 Reloc::Model ReloModel = TM.getRelocationModel();
Chris Lattnered0c6762009-07-26 07:00:12 +0000223 bool isWeak = GV->isWeakForLinker();
Chris Lattner2a7dd7d2009-07-26 06:11:33 +0000224
Anton Korobeynikovb4f658c2008-06-28 13:45:57 +0000225 // Early exit - functions should be always in text sections.
Chris Lattner2e932372009-07-24 16:53:52 +0000226 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
227 if (GVar == 0)
Chris Lattnered0c6762009-07-26 07:00:12 +0000228 return SectionKind::get(SectionKind::Text, isWeak);
Anton Korobeynikovb4f658c2008-06-28 13:45:57 +0000229
Chris Lattner3081d4a2009-07-26 06:34:33 +0000230
231 // Handle thread-local data first.
232 if (GVar->isThreadLocal()) {
233 if (isSuitableForBSS(GVar))
Chris Lattnered0c6762009-07-26 07:00:12 +0000234 return SectionKind::get(SectionKind::ThreadBSS, isWeak);
235 return SectionKind::get(SectionKind::ThreadData, isWeak);
Chris Lattner3081d4a2009-07-26 06:34:33 +0000236 }
Anton Korobeynikovb4f658c2008-06-28 13:45:57 +0000237
Chris Lattnerd398a8d2009-07-24 19:15:47 +0000238 // Variable can be easily put to BSS section.
239 if (isSuitableForBSS(GVar))
Chris Lattnered0c6762009-07-26 07:00:12 +0000240 return SectionKind::get(SectionKind::BSS, isWeak);
Chris Lattnerd398a8d2009-07-24 19:15:47 +0000241
Chris Lattnerd398a8d2009-07-24 19:15:47 +0000242 Constant *C = GVar->getInitializer();
243
244 // If the global is marked constant, we can put it into a mergable section,
245 // a mergable string section, or general .data if it contains relocations.
246 if (GVar->isConstant()) {
247 // If the initializer for the global contains something that requires a
248 // relocation, then we may have to drop this into a wriable data section
249 // even though it is marked const.
250 switch (C->getRelocationInfo()) {
251 default: llvm_unreachable("unknown relocation info kind");
252 case Constant::NoRelocation:
253 // If initializer is a null-terminated string, put it in a "cstring"
254 // section if the target has it.
Owen Anderson8d9397a2009-07-13 21:27:19 +0000255 if (isConstantString(C))
Chris Lattnered0c6762009-07-26 07:00:12 +0000256 return SectionKind::get(SectionKind::MergeableCString, isWeak);
Chris Lattnerd398a8d2009-07-24 19:15:47 +0000257
Chris Lattner2a7dd7d2009-07-26 06:11:33 +0000258 // Otherwise, just drop it into a mergable constant section. If we have
259 // a section for this size, use it, otherwise use the arbitrary sized
260 // mergable section.
261 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattnered0c6762009-07-26 07:00:12 +0000262 case 4: return SectionKind::get(SectionKind::MergeableConst4, isWeak);
263 case 8: return SectionKind::get(SectionKind::MergeableConst8, isWeak);
264 case 16: return SectionKind::get(SectionKind::MergeableConst16, isWeak);
265 default: return SectionKind::get(SectionKind::MergeableConst, isWeak);
Chris Lattner2a7dd7d2009-07-26 06:11:33 +0000266 }
Chris Lattnerd398a8d2009-07-24 19:15:47 +0000267
268 case Constant::LocalRelocation:
269 // In static relocation model, the linker will resolve all addresses, so
270 // the relocation entries will actually be constants by the time the app
271 // starts up.
Chris Lattner224957e2009-07-24 20:14:10 +0000272 if (ReloModel == Reloc::Static)
Chris Lattnered0c6762009-07-26 07:00:12 +0000273 return SectionKind::get(SectionKind::ReadOnly, isWeak);
Chris Lattnerd398a8d2009-07-24 19:15:47 +0000274
275 // Otherwise, the dynamic linker needs to fix it up, put it in the
276 // writable data.rel.local section.
Chris Lattnered0c6762009-07-26 07:00:12 +0000277 return SectionKind::get(SectionKind::ReadOnlyWithRelLocal, isWeak);
Chris Lattnerd398a8d2009-07-24 19:15:47 +0000278
279 case Constant::GlobalRelocations:
280 // In static relocation model, the linker will resolve all addresses, so
281 // the relocation entries will actually be constants by the time the app
282 // starts up.
Chris Lattner224957e2009-07-24 20:14:10 +0000283 if (ReloModel == Reloc::Static)
Chris Lattnered0c6762009-07-26 07:00:12 +0000284 return SectionKind::get(SectionKind::ReadOnly, isWeak);
Chris Lattnerd398a8d2009-07-24 19:15:47 +0000285
286 // Otherwise, the dynamic linker needs to fix it up, put it in the
287 // writable data.rel section.
Chris Lattnered0c6762009-07-26 07:00:12 +0000288 return SectionKind::get(SectionKind::ReadOnlyWithRel, isWeak);
Anton Korobeynikovb4f658c2008-06-28 13:45:57 +0000289 }
290 }
291
Chris Lattnerd398a8d2009-07-24 19:15:47 +0000292 // Okay, this isn't a constant. If the initializer for the global is going
293 // to require a runtime relocation by the dynamic linker, put it into a more
294 // specific section to improve startup time of the app. This coalesces these
295 // globals together onto fewer pages, improving the locality of the dynamic
296 // linker.
Chris Lattner224957e2009-07-24 20:14:10 +0000297 if (ReloModel == Reloc::Static)
Chris Lattnered0c6762009-07-26 07:00:12 +0000298 return SectionKind::get(SectionKind::DataNoRel, isWeak);
Chris Lattnerd398a8d2009-07-24 19:15:47 +0000299
300 switch (C->getRelocationInfo()) {
301 default: llvm_unreachable("unknown relocation info kind");
Chris Lattner3081d4a2009-07-26 06:34:33 +0000302 case Constant::NoRelocation:
Chris Lattnered0c6762009-07-26 07:00:12 +0000303 return SectionKind::get(SectionKind::DataNoRel, isWeak);
Chris Lattner3081d4a2009-07-26 06:34:33 +0000304 case Constant::LocalRelocation:
Chris Lattnered0c6762009-07-26 07:00:12 +0000305 return SectionKind::get(SectionKind::DataRelLocal, isWeak);
Chris Lattner3081d4a2009-07-26 06:34:33 +0000306 case Constant::GlobalRelocations:
Chris Lattnered0c6762009-07-26 07:00:12 +0000307 return SectionKind::get(SectionKind::DataRel, isWeak);
Chris Lattnerd398a8d2009-07-24 19:15:47 +0000308 }
Anton Korobeynikovb4f658c2008-06-28 13:45:57 +0000309}
Anton Korobeynikovb3593bf2008-07-09 13:16:59 +0000310
Chris Lattner4bf1d272009-07-25 18:11:58 +0000311/// SectionForGlobal - This method computes the appropriate section to emit
312/// the specified global variable or function definition. This should not
313/// be passed external (or available externally) globals.
Chris Lattner506d5802009-07-21 21:09:35 +0000314const Section *TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
Chris Lattner4bf1d272009-07-25 18:11:58 +0000315 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
316 "Can only be used for global definitions");
317
Chris Lattner2a7dd7d2009-07-26 06:11:33 +0000318 SectionKind Kind = SectionKindForGlobal(GV, TM);
Chris Lattner0ad34562009-07-24 18:34:27 +0000319
320 // Select section name.
Anton Korobeynikov676a6252008-07-09 13:22:46 +0000321 if (GV->hasSection()) {
Chris Lattner0ad34562009-07-24 18:34:27 +0000322 // If the target has special section hacks for specifically named globals,
323 // return them now.
324 if (const Section *TS = getSpecialCasedSectionGlobals(GV, Kind))
325 return TS;
326
Chris Lattner506d5802009-07-21 21:09:35 +0000327 // Honour section already set, if any.
Chris Lattner0ad34562009-07-24 18:34:27 +0000328 unsigned Flags = SectionFlagsForGlobal(GV, Kind);
Chris Lattner8f2dd992009-07-24 16:50:24 +0000329
330 // This is an explicitly named section.
331 Flags |= SectionFlags::Named;
332
333 // If the target has magic semantics for certain section names, make sure to
334 // pick up the flags. This allows the user to write things with attribute
335 // section and still get the appropriate section flags printed.
Chris Lattnerdfd15982009-07-24 17:02:17 +0000336 Flags |= getFlagsForNamedSection(GV->getSection().c_str());
Chris Lattner8f2dd992009-07-24 16:50:24 +0000337
Chris Lattner506d5802009-07-21 21:09:35 +0000338 return getNamedSection(GV->getSection().c_str(), Flags);
Anton Korobeynikov676a6252008-07-09 13:22:46 +0000339 }
Daniel Dunbarc6425db2009-07-22 20:26:37 +0000340
Chris Lattner88025bd2009-07-24 05:10:25 +0000341 // If this global is linkonce/weak and the target handles this by emitting it
342 // into a 'uniqued' section name, create and return the section now.
343 if (GV->isWeakForLinker()) {
Chris Lattner0ad34562009-07-24 18:34:27 +0000344 if (const char *Prefix = getSectionPrefixForUniqueGlobal(Kind)) {
345 unsigned Flags = SectionFlagsForGlobal(GV, Kind);
Chris Lattner2e932372009-07-24 16:53:52 +0000346
Chris Lattner88025bd2009-07-24 05:10:25 +0000347 // FIXME: Use mangler interface (PR4584).
Daniel Dunbar4b037aa2009-07-24 12:21:08 +0000348 std::string Name = Prefix+GV->getNameStr();
Chris Lattner88025bd2009-07-24 05:10:25 +0000349 return getNamedSection(Name.c_str(), Flags);
350 }
351 }
352
Chris Lattner506d5802009-07-21 21:09:35 +0000353 // Use default section depending on the 'type' of global
Chris Lattnerbb0c9bf2009-07-24 18:42:53 +0000354 return SelectSectionForGlobal(GV, Kind);
Anton Korobeynikov676a6252008-07-09 13:22:46 +0000355}
356
357// Lame default implementation. Calculate the section name for global.
Evan Chengeb774e12008-08-08 17:56:50 +0000358const Section*
Chris Lattnerbb0c9bf2009-07-24 18:42:53 +0000359TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnercc195212009-07-25 23:21:55 +0000360 SectionKind Kind) const {
Chris Lattnerdc9d8cf2009-07-26 05:44:20 +0000361 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
362
363 if (Kind.isText())
Chris Lattner88025bd2009-07-24 05:10:25 +0000364 return getTextSection();
365
Chris Lattnercc195212009-07-25 23:21:55 +0000366 if (Kind.isBSS())
Chris Lattner88025bd2009-07-24 05:10:25 +0000367 if (const Section *S = getBSSSection_())
368 return S;
369
Chris Lattnercc195212009-07-25 23:21:55 +0000370 if (Kind.isReadOnly())
Chris Lattner88025bd2009-07-24 05:10:25 +0000371 if (const Section *S = getReadOnlySection())
372 return S;
Anton Korobeynikov562c3f22008-07-09 13:18:02 +0000373
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +0000374 return getDataSection();
Anton Korobeynikov562c3f22008-07-09 13:18:02 +0000375}
Anton Korobeynikov3819e2d2008-07-09 13:19:38 +0000376
Chris Lattner680c6f62009-07-22 00:28:43 +0000377/// getSectionForMergableConstant - Given a mergable constant with the
378/// specified size and relocation information, return a section that it
379/// should be placed in.
380const Section *
Chris Lattner1c379242009-07-26 06:48:26 +0000381TargetAsmInfo::getSectionForMergeableConstant(SectionKind Kind) const {
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000382 if (Kind.isReadOnly())
Chris Lattner56acf5c2009-07-26 05:57:07 +0000383 if (const Section *S = getReadOnlySection())
384 return S;
385
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +0000386 return getDataSection();
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000387}
388
Chris Lattner680c6f62009-07-22 00:28:43 +0000389
Chris Lattner506d5802009-07-21 21:09:35 +0000390const Section *TargetAsmInfo::getNamedSection(const char *Name, unsigned Flags,
391 bool Override) const {
392 Section &S = Sections[Name];
Anton Korobeynikovc33a1ff2008-07-09 13:28:49 +0000393
394 // This is newly-created section, set it up properly.
asla1c96962008-08-16 12:58:12 +0000395 if (S.Flags == SectionFlags::Invalid || Override) {
Anton Korobeynikovc33a1ff2008-07-09 13:28:49 +0000396 S.Flags = Flags | SectionFlags::Named;
397 S.Name = Name;
398 }
399
400 return &S;
401}
402
403const Section*
asla1c96962008-08-16 12:58:12 +0000404TargetAsmInfo::getUnnamedSection(const char *Directive, unsigned Flags,
405 bool Override) const {
Anton Korobeynikovc33a1ff2008-07-09 13:28:49 +0000406 Section& S = Sections[Directive];
407
408 // This is newly-created section, set it up properly.
asla1c96962008-08-16 12:58:12 +0000409 if (S.Flags == SectionFlags::Invalid || Override) {
Anton Korobeynikovc33a1ff2008-07-09 13:28:49 +0000410 S.Flags = Flags & ~SectionFlags::Named;
411 S.Name = Directive;
412 }
413
414 return &S;
415}
asl27ffd262008-08-16 12:57:07 +0000416
417const std::string&
418TargetAsmInfo::getSectionFlags(unsigned Flags) const {
419 SectionFlags::FlagsStringsMapType::iterator I = FlagsStrings.find(Flags);
420
421 // We didn't print these flags yet, print and save them to map. This reduces
422 // amount of heap trashing due to std::string construction / concatenation.
423 if (I == FlagsStrings.end())
424 I = FlagsStrings.insert(std::make_pair(Flags,
425 printSectionFlags(Flags))).first;
426
427 return I->second;
428}
aslc200b112008-08-16 12:57:46 +0000429
430unsigned TargetAsmInfo::getULEB128Size(unsigned Value) {
431 unsigned Size = 0;
432 do {
433 Value >>= 7;
434 Size += sizeof(int8_t);
435 } while (Value);
436 return Size;
437}
438
439unsigned TargetAsmInfo::getSLEB128Size(int Value) {
440 unsigned Size = 0;
441 int Sign = Value >> (8 * sizeof(Value) - 1);
442 bool IsMore;
443
444 do {
445 unsigned Byte = Value & 0x7f;
446 Value >>= 7;
447 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
448 Size += sizeof(int8_t);
449 } while (IsMore);
450 return Size;
451}