blob: 782e7b4d8714909a43e29ddd63c1269d854b674a [file] [log] [blame]
Jim Laskey7c95ad42006-09-06 19:21:41 +00001//===-- TargetAsmInfo.cpp - Asm Info ---------------------------------------==//
Jim Laskeyec0d9fe2006-09-06 18:35:33 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskeyec0d9fe2006-09-06 18:35:33 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines target asm properties related what form asm statements
11// should take.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov28a2b542008-06-28 13:45:57 +000015#include "llvm/Constants.h"
Anton Korobeynikov72bb4022009-01-27 22:29:24 +000016#include "llvm/DerivedTypes.h"
Anton Korobeynikov28a2b542008-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"
Jim Laskeyec0d9fe2006-09-06 18:35:33 +000021#include "llvm/Target/TargetAsmInfo.h"
Anton Korobeynikovdb232312009-03-29 17:13:49 +000022#include "llvm/Target/TargetMachine.h"
Anton Korobeynikov28a2b542008-06-28 13:45:57 +000023#include "llvm/Target/TargetOptions.h"
Anton Korobeynikovcee750f2008-02-27 23:33:50 +000024#include "llvm/Support/Dwarf.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000025#include "llvm/Support/ErrorHandling.h"
Dale Johannesen3bb62832007-04-23 20:00:17 +000026#include <cctype>
27#include <cstring>
Jim Laskeyec0d9fe2006-09-06 18:35:33 +000028using namespace llvm;
29
Chris Lattner4e0f25b2009-06-19 00:08:39 +000030TargetAsmInfo::TargetAsmInfo(const TargetMachine &tm)
31: TM(tm) {
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000032 BSSSection = "\t.bss";
33 BSSSection_ = 0;
34 ReadOnlySection = 0;
35 SmallDataSection = 0;
36 SmallBSSSection = 0;
37 SmallRODataSection = 0;
38 TLSDataSection = 0;
39 TLSBSSSection = 0;
40 ZeroFillDirective = 0;
41 NonexecutableStackDirective = 0;
42 NeedsSet = false;
43 MaxInstLength = 4;
44 PCSymbol = "$";
45 SeparatorChar = ';';
46 CommentString = "#";
47 GlobalPrefix = "";
48 PrivateGlobalPrefix = ".";
49 LessPrivateGlobalPrefix = "";
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;
61 StringConstantPrefix = ".str";
Chris Lattnera93ca922009-06-18 23:41:35 +000062 AllowQuotesInName = false;
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000063 ZeroDirective = "\t.zero\t";
64 ZeroDirectiveSuffix = 0;
65 AsciiDirective = "\t.ascii\t";
66 AscizDirective = "\t.asciz\t";
67 Data8bitsDirective = "\t.byte\t";
68 Data16bitsDirective = "\t.short\t";
69 Data32bitsDirective = "\t.long\t";
70 Data64bitsDirective = "\t.quad\t";
71 AlignDirective = "\t.align\t";
72 AlignmentIsInBytes = true;
73 TextAlignFillValue = 0;
74 SwitchToSectionDirective = "\t.section\t";
75 TextSectionStartSuffix = "";
76 DataSectionStartSuffix = "";
77 SectionEndDirectiveSuffix = 0;
78 ConstantPoolSection = "\t.section .rodata";
79 JumpTableDataSection = "\t.section .rodata";
80 JumpTableDirective = 0;
81 CStringSection = 0;
82 CStringSection_ = 0;
83 // FIXME: Flags are ELFish - replace with normal section stuff.
84 StaticCtorsSection = "\t.section .ctors,\"aw\",@progbits";
85 StaticDtorsSection = "\t.section .dtors,\"aw\",@progbits";
86 GlobalDirective = "\t.globl\t";
87 SetDirective = 0;
88 LCOMMDirective = 0;
89 COMMDirective = "\t.comm\t";
90 COMMDirectiveTakesAlignment = true;
91 HasDotTypeDotSizeDirective = true;
Rafael Espindola952b8392008-12-03 11:01:37 +000092 HasSingleParameterDotFile = true;
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000093 UsedDirective = 0;
94 WeakRefDirective = 0;
95 WeakDefDirective = 0;
96 // FIXME: These are ELFish - move to ELFTAI.
97 HiddenDirective = "\t.hidden\t";
98 ProtectedDirective = "\t.protected\t";
99 AbsoluteDebugSectionOffsets = false;
100 AbsoluteEHSectionOffsets = false;
101 HasLEB128 = false;
102 HasDotLocAndDotFile = false;
103 SupportsDebugInformation = false;
104 SupportsExceptionHandling = false;
105 DwarfRequiresFrameSection = true;
Devang Patel0f7fef32009-04-13 17:02:03 +0000106 DwarfUsesInlineInfoSection = false;
Bill Wendling722f5f12008-12-24 08:05:17 +0000107 NonLocalEHFrameLabel = false;
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000108 GlobalEHDirective = 0;
109 SupportsWeakOmittedEHFrame = true;
110 DwarfSectionOffsetDirective = 0;
111 DwarfAbbrevSection = ".debug_abbrev";
112 DwarfInfoSection = ".debug_info";
113 DwarfLineSection = ".debug_line";
114 DwarfFrameSection = ".debug_frame";
115 DwarfPubNamesSection = ".debug_pubnames";
116 DwarfPubTypesSection = ".debug_pubtypes";
Devang Patel0f7fef32009-04-13 17:02:03 +0000117 DwarfDebugInlineSection = ".debug_inlined";
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000118 DwarfStrSection = ".debug_str";
119 DwarfLocSection = ".debug_loc";
120 DwarfARangesSection = ".debug_aranges";
121 DwarfRangesSection = ".debug_ranges";
Chris Lattnerb839c3f2009-06-18 23:31:37 +0000122 DwarfMacroInfoSection = ".debug_macinfo";
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000123 DwarfEHFrameSection = ".eh_frame";
124 DwarfExceptionSection = ".gcc_except_table";
125 AsmTransCBE = 0;
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +0000126 TextSection = getUnnamedSection("\t.text", SectionFlags::Code);
Anton Korobeynikov315690e2008-09-24 22:16:16 +0000127 DataSection = getUnnamedSection("\t.data", SectionFlags::Writeable);
Chris Lattner4c7b07a2006-10-13 17:50:07 +0000128}
Chris Lattnerf5b10ec2006-10-05 00:35:16 +0000129
130TargetAsmInfo::~TargetAsmInfo() {
131}
Chris Lattner4c7b07a2006-10-13 17:50:07 +0000132
133/// Measure the specified inline asm to determine an approximation of its
134/// length.
Dale Johannesen3bb62832007-04-23 20:00:17 +0000135/// Comments (which run till the next SeparatorChar or newline) do not
136/// count as an instruction.
137/// Any other non-whitespace text is considered an instruction, with
138/// multiple instructions separated by SeparatorChar or newlines.
139/// Variable-length instructions are not handled here; this function
140/// may be overloaded in the target code to do that.
Chris Lattner4c7b07a2006-10-13 17:50:07 +0000141unsigned TargetAsmInfo::getInlineAsmLength(const char *Str) const {
142 // Count the number of instructions in the asm.
Dale Johannesen3bb62832007-04-23 20:00:17 +0000143 bool atInsnStart = true;
144 unsigned Length = 0;
Chris Lattner4c7b07a2006-10-13 17:50:07 +0000145 for (; *Str; ++Str) {
146 if (*Str == '\n' || *Str == SeparatorChar)
Dale Johannesen3bb62832007-04-23 20:00:17 +0000147 atInsnStart = true;
148 if (atInsnStart && !isspace(*Str)) {
149 Length += MaxInstLength;
150 atInsnStart = false;
151 }
152 if (atInsnStart && strncmp(Str, CommentString, strlen(CommentString))==0)
153 atInsnStart = false;
Chris Lattner4c7b07a2006-10-13 17:50:07 +0000154 }
155
Dale Johannesen3bb62832007-04-23 20:00:17 +0000156 return Length;
Chris Lattner4c7b07a2006-10-13 17:50:07 +0000157}
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000158
Anton Korobeynikov8213f9c2008-02-29 22:09:08 +0000159unsigned TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000160 bool Global) const {
161 return dwarf::DW_EH_PE_absptr;
162}
163
Anton Korobeynikov28a2b542008-06-28 13:45:57 +0000164static bool isSuitableForBSS(const GlobalVariable *GV) {
165 if (!GV->hasInitializer())
166 return true;
167
168 // Leave constant zeros in readonly constant sections, so they can be shared
169 Constant *C = GV->getInitializer();
170 return (C->isNullValue() && !GV->isConstant() && !NoZerosInBSS);
171}
172
Anton Korobeynikov72bb4022009-01-27 22:29:24 +0000173static bool isConstantString(const Constant *C) {
174 // First check: is we have constant array of i8 terminated with zero
175 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
176 // Check, if initializer is a null-terminated string
177 if (CVA && CVA->isCString())
178 return true;
179
180 // Another possibility: [1 x i8] zeroinitializer
181 if (isa<ConstantAggregateZero>(C)) {
182 if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType())) {
183 return (Ty->getElementType() == Type::Int8Ty &&
184 Ty->getNumElements() == 1);
185 }
186 }
187
188 return false;
189}
190
Anton Korobeynikovdb232312009-03-29 17:13:49 +0000191unsigned TargetAsmInfo::RelocBehaviour() const {
192 // By default - all relocations in PIC mode would force symbol to be
193 // placed in r/w section.
194 return (TM.getRelocationModel() != Reloc::Static ?
195 Reloc::LocalOrGlobal : Reloc::None);
196}
Anton Korobeynikov72bb4022009-01-27 22:29:24 +0000197
Anton Korobeynikov28a2b542008-06-28 13:45:57 +0000198SectionKind::Kind
199TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
200 // Early exit - functions should be always in text sections.
201 if (isa<Function>(GV))
202 return SectionKind::Text;
203
204 const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV);
205 bool isThreadLocal = GVar->isThreadLocal();
206 assert(GVar && "Invalid global value for section selection");
207
Anton Korobeynikov28a2b542008-06-28 13:45:57 +0000208 if (isSuitableForBSS(GVar)) {
209 // Variable can be easily put to BSS section.
210 return (isThreadLocal ? SectionKind::ThreadBSS : SectionKind::BSS);
211 } else if (GVar->isConstant() && !isThreadLocal) {
212 // Now we know, that varible has initializer and it is constant. We need to
213 // check its initializer to decide, which section to output it into. Also
214 // note, there is no thread-local r/o section.
215 Constant *C = GVar->getInitializer();
Anton Korobeynikovdb232312009-03-29 17:13:49 +0000216 if (C->ContainsRelocations(Reloc::LocalOrGlobal)) {
217 // Decide, whether it is still possible to put symbol into r/o section.
218 unsigned Reloc = RelocBehaviour();
219
220 // We already did a query for 'all' relocs, thus - early exits.
Anton Korobeynikovfca82de2009-03-30 15:27:43 +0000221 if (Reloc == Reloc::LocalOrGlobal)
222 return SectionKind::Data;
223 else if (Reloc == Reloc::None)
Anton Korobeynikovdb232312009-03-29 17:13:49 +0000224 return SectionKind::ROData;
225 else {
226 // Ok, target wants something funny. Honour it.
Anton Korobeynikovfca82de2009-03-30 15:27:43 +0000227 return (C->ContainsRelocations(Reloc) ?
228 SectionKind::Data : SectionKind::ROData);
Anton Korobeynikovdb232312009-03-29 17:13:49 +0000229 }
230 } else {
Anton Korobeynikov28a2b542008-06-28 13:45:57 +0000231 // Check, if initializer is a null-terminated string
Anton Korobeynikov72bb4022009-01-27 22:29:24 +0000232 if (isConstantString(C))
Anton Korobeynikov0e48a0c2008-07-09 13:24:38 +0000233 return SectionKind::RODataMergeStr;
Anton Korobeynikov28a2b542008-06-28 13:45:57 +0000234 else
Anton Korobeynikov0e48a0c2008-07-09 13:24:38 +0000235 return SectionKind::RODataMergeConst;
Anton Korobeynikov28a2b542008-06-28 13:45:57 +0000236 }
237 }
238
Anton Korobeynikov71a7c6c2009-03-30 15:27:03 +0000239 // Variable either is not constant or thread-local - output to data section.
Anton Korobeynikovfca82de2009-03-30 15:27:43 +0000240 return (isThreadLocal ? SectionKind::ThreadData : SectionKind::Data);
Anton Korobeynikov28a2b542008-06-28 13:45:57 +0000241}
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000242
243unsigned
244TargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV,
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000245 const char* Name) const {
246 unsigned Flags = SectionFlags::None;
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000247
248 // Decode flags from global itself.
249 if (GV) {
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000250 SectionKind::Kind Kind = SectionKindForGlobal(GV);
251 switch (Kind) {
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000252 case SectionKind::Text:
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000253 Flags |= SectionFlags::Code;
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000254 break;
255 case SectionKind::ThreadData:
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000256 case SectionKind::ThreadBSS:
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000257 Flags |= SectionFlags::TLS;
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000258 // FALLS THROUGH
Anton Korobeynikov7d51edf2008-07-09 13:29:44 +0000259 case SectionKind::Data:
Anton Korobeynikov71a7c6c2009-03-30 15:27:03 +0000260 case SectionKind::DataRel:
261 case SectionKind::DataRelLocal:
262 case SectionKind::DataRelRO:
263 case SectionKind::DataRelROLocal:
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000264 case SectionKind::BSS:
Anton Korobeynikov7d51edf2008-07-09 13:29:44 +0000265 Flags |= SectionFlags::Writeable;
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000266 break;
267 case SectionKind::ROData:
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000268 case SectionKind::RODataMergeStr:
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000269 case SectionKind::RODataMergeConst:
Anton Korobeynikov7d51edf2008-07-09 13:29:44 +0000270 // No additional flags here
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000271 break;
Anton Korobeynikov04dda732008-07-22 17:09:59 +0000272 case SectionKind::SmallData:
273 case SectionKind::SmallBSS:
274 Flags |= SectionFlags::Writeable;
275 // FALLS THROUGH
276 case SectionKind::SmallROData:
277 Flags |= SectionFlags::Small;
278 break;
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000279 default:
Torok Edwinc25e7582009-07-11 20:10:48 +0000280 LLVM_UNREACHABLE("Unexpected section kind!");
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000281 }
282
Duncan Sands667d4b82009-03-07 15:45:40 +0000283 if (GV->isWeakForLinker())
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000284 Flags |= SectionFlags::Linkonce;
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000285 }
286
287 // Add flags from sections, if any.
Anton Korobeynikov2a889172008-07-09 13:25:46 +0000288 if (Name && *Name) {
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000289 Flags |= SectionFlags::Named;
290
291 // Some lame default implementation based on some magic section names.
292 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
Anton Korobeynikov04dda732008-07-22 17:09:59 +0000293 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
294 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
295 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000296 Flags |= SectionFlags::BSS;
297 else if (strcmp(Name, ".tdata") == 0 ||
298 strncmp(Name, ".tdata.", 7) == 0 ||
299 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
300 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
301 Flags |= SectionFlags::TLS;
302 else if (strcmp(Name, ".tbss") == 0 ||
303 strncmp(Name, ".tbss.", 6) == 0 ||
304 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
305 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
306 Flags |= SectionFlags::BSS | SectionFlags::TLS;
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000307 }
308
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000309 return Flags;
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000310}
Anton Korobeynikov0c602462008-07-09 13:18:02 +0000311
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000312const Section*
Evan Cheng42ccc212008-08-08 17:56:50 +0000313TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const {
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000314 const Section* S;
Anton Korobeynikov265c5252008-07-09 13:22:46 +0000315 // Select section name
316 if (GV->hasSection()) {
317 // Honour section already set, if any
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000318 unsigned Flags = SectionFlagsForGlobal(GV,
319 GV->getSection().c_str());
320 S = getNamedSection(GV->getSection().c_str(), Flags);
Anton Korobeynikov265c5252008-07-09 13:22:46 +0000321 } else {
322 // Use default section depending on the 'type' of global
Evan Cheng42ccc212008-08-08 17:56:50 +0000323 S = SelectSectionForGlobal(GV);
Anton Korobeynikov265c5252008-07-09 13:22:46 +0000324 }
325
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000326 return S;
Anton Korobeynikov265c5252008-07-09 13:22:46 +0000327}
328
329// Lame default implementation. Calculate the section name for global.
Evan Cheng42ccc212008-08-08 17:56:50 +0000330const Section*
331TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000332 SectionKind::Kind Kind = SectionKindForGlobal(GV);
Anton Korobeynikov0c602462008-07-09 13:18:02 +0000333
Duncan Sands667d4b82009-03-07 15:45:40 +0000334 if (GV->isWeakForLinker()) {
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000335 std::string Name = UniqueSectionForGlobal(GV, Kind);
336 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
337 return getNamedSection(Name.c_str(), Flags);
338 } else {
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000339 if (Kind == SectionKind::Text)
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +0000340 return getTextSection();
Anton Korobeynikov45788622008-08-07 09:51:54 +0000341 else if (isBSS(Kind) && getBSSSection_())
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000342 return getBSSSection_();
Anton Korobeynikov00181a32008-09-24 22:20:27 +0000343 else if (getReadOnlySection() && SectionKind::isReadOnly(Kind))
344 return getReadOnlySection();
Anton Korobeynikov265c5252008-07-09 13:22:46 +0000345 }
Anton Korobeynikov0c602462008-07-09 13:18:02 +0000346
Anton Korobeynikov315690e2008-09-24 22:16:16 +0000347 return getDataSection();
Anton Korobeynikov0c602462008-07-09 13:18:02 +0000348}
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000349
Anton Korobeynikov93cacf12008-08-07 09:50:34 +0000350// Lame default implementation. Calculate the section name for machine const.
351const Section*
352TargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
353 // FIXME: Support data.rel stuff someday
Anton Korobeynikov315690e2008-09-24 22:16:16 +0000354 return getDataSection();
Anton Korobeynikov93cacf12008-08-07 09:50:34 +0000355}
356
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000357std::string
358TargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
Anton Korobeynikovb20015b2008-07-09 13:25:26 +0000359 SectionKind::Kind Kind) const {
360 switch (Kind) {
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000361 case SectionKind::Text:
Anton Korobeynikov52486702008-07-09 13:24:55 +0000362 return ".gnu.linkonce.t." + GV->getName();
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000363 case SectionKind::Data:
Anton Korobeynikov52486702008-07-09 13:24:55 +0000364 return ".gnu.linkonce.d." + GV->getName();
Anton Korobeynikov71a7c6c2009-03-30 15:27:03 +0000365 case SectionKind::DataRel:
366 return ".gnu.linkonce.d.rel" + GV->getName();
367 case SectionKind::DataRelLocal:
368 return ".gnu.linkonce.d.rel.local" + GV->getName();
369 case SectionKind::DataRelRO:
370 return ".gnu.linkonce.d.rel.ro" + GV->getName();
371 case SectionKind::DataRelROLocal:
372 return ".gnu.linkonce.d.rel.ro.local" + GV->getName();
Anton Korobeynikov04dda732008-07-22 17:09:59 +0000373 case SectionKind::SmallData:
374 return ".gnu.linkonce.s." + GV->getName();
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000375 case SectionKind::BSS:
Anton Korobeynikov52486702008-07-09 13:24:55 +0000376 return ".gnu.linkonce.b." + GV->getName();
Anton Korobeynikov04dda732008-07-22 17:09:59 +0000377 case SectionKind::SmallBSS:
378 return ".gnu.linkonce.sb." + GV->getName();
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000379 case SectionKind::ROData:
380 case SectionKind::RODataMergeConst:
381 case SectionKind::RODataMergeStr:
Anton Korobeynikov52486702008-07-09 13:24:55 +0000382 return ".gnu.linkonce.r." + GV->getName();
Anton Korobeynikov04dda732008-07-22 17:09:59 +0000383 case SectionKind::SmallROData:
384 return ".gnu.linkonce.s2." + GV->getName();
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000385 case SectionKind::ThreadData:
Anton Korobeynikov52486702008-07-09 13:24:55 +0000386 return ".gnu.linkonce.td." + GV->getName();
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000387 case SectionKind::ThreadBSS:
Anton Korobeynikov52486702008-07-09 13:24:55 +0000388 return ".gnu.linkonce.tb." + GV->getName();
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000389 default:
Torok Edwinc25e7582009-07-11 20:10:48 +0000390 LLVM_UNREACHABLE("Unknown section kind");
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000391 }
Devang Patel8a84e442009-01-05 17:31:22 +0000392 return NULL;
Anton Korobeynikov29b03f72008-07-09 13:19:38 +0000393}
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000394
395const Section*
Anton Korobeynikov328da652008-08-16 12:58:12 +0000396TargetAsmInfo::getNamedSection(const char *Name, unsigned Flags,
397 bool Override) const {
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000398 Section& S = Sections[Name];
399
400 // This is newly-created section, set it up properly.
Anton Korobeynikov328da652008-08-16 12:58:12 +0000401 if (S.Flags == SectionFlags::Invalid || Override) {
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000402 S.Flags = Flags | SectionFlags::Named;
403 S.Name = Name;
404 }
405
406 return &S;
407}
408
409const Section*
Anton Korobeynikov328da652008-08-16 12:58:12 +0000410TargetAsmInfo::getUnnamedSection(const char *Directive, unsigned Flags,
411 bool Override) const {
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000412 Section& S = Sections[Directive];
413
414 // This is newly-created section, set it up properly.
Anton Korobeynikov328da652008-08-16 12:58:12 +0000415 if (S.Flags == SectionFlags::Invalid || Override) {
Anton Korobeynikov0d44ba82008-07-09 13:28:49 +0000416 S.Flags = Flags & ~SectionFlags::Named;
417 S.Name = Directive;
418 }
419
420 return &S;
421}
Anton Korobeynikovd0c1e292008-08-16 12:57:07 +0000422
423const std::string&
424TargetAsmInfo::getSectionFlags(unsigned Flags) const {
425 SectionFlags::FlagsStringsMapType::iterator I = FlagsStrings.find(Flags);
426
427 // We didn't print these flags yet, print and save them to map. This reduces
428 // amount of heap trashing due to std::string construction / concatenation.
429 if (I == FlagsStrings.end())
430 I = FlagsStrings.insert(std::make_pair(Flags,
431 printSectionFlags(Flags))).first;
432
433 return I->second;
434}
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000435
436unsigned TargetAsmInfo::getULEB128Size(unsigned Value) {
437 unsigned Size = 0;
438 do {
439 Value >>= 7;
440 Size += sizeof(int8_t);
441 } while (Value);
442 return Size;
443}
444
445unsigned TargetAsmInfo::getSLEB128Size(int Value) {
446 unsigned Size = 0;
447 int Sign = Value >> (8 * sizeof(Value) - 1);
448 bool IsMore;
449
450 do {
451 unsigned Byte = Value & 0x7f;
452 Value >>= 7;
453 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
454 Size += sizeof(int8_t);
455 } while (IsMore);
456 return Size;
457}