blob: cda939c87cf3f3e2469ce84e4dd5a97795747987 [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"
16#include "llvm/GlobalVariable.h"
17#include "llvm/Function.h"
18#include "llvm/Module.h"
19#include "llvm/Type.h"
Jim Laskeyec0d9fe2006-09-06 18:35:33 +000020#include "llvm/Target/TargetAsmInfo.h"
Anton Korobeynikov28a2b542008-06-28 13:45:57 +000021#include "llvm/Target/TargetOptions.h"
Anton Korobeynikovcee750f2008-02-27 23:33:50 +000022#include "llvm/Support/Dwarf.h"
Dale Johannesen3bb62832007-04-23 20:00:17 +000023#include <cctype>
24#include <cstring>
Jim Laskeyec0d9fe2006-09-06 18:35:33 +000025
26using namespace llvm;
27
28TargetAsmInfo::TargetAsmInfo() :
Dan Gohman95701652007-05-03 18:46:30 +000029 TextSection("\t.text"),
30 DataSection("\t.data"),
31 BSSSection("\t.bss"),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000032 TLSDataSection("\t.section .tdata,\"awT\",@progbits"),
33 TLSBSSSection("\t.section .tbss,\"awT\",@nobits"),
Chris Lattner6f198df2007-01-17 17:42:42 +000034 ZeroFillDirective(0),
Dan Gohmana779a982008-05-05 00:28:39 +000035 NonexecutableStackDirective(0),
Jim Laskeyec0d9fe2006-09-06 18:35:33 +000036 NeedsSet(false),
Chris Lattner4c7b07a2006-10-13 17:50:07 +000037 MaxInstLength(4),
Jim Laskeyb82313f2007-02-01 16:31:34 +000038 PCSymbol("$"),
Chris Lattner4c7b07a2006-10-13 17:50:07 +000039 SeparatorChar(';'),
Jim Laskeyec0d9fe2006-09-06 18:35:33 +000040 CommentString("#"),
41 GlobalPrefix(""),
42 PrivateGlobalPrefix("."),
Chris Lattner393a8ee2007-01-18 01:12:56 +000043 JumpTableSpecialLabelPrefix(0),
Jim Laskeyec0d9fe2006-09-06 18:35:33 +000044 GlobalVarAddrPrefix(""),
45 GlobalVarAddrSuffix(""),
46 FunctionAddrPrefix(""),
47 FunctionAddrSuffix(""),
Bill Wendlingd60da492007-09-11 08:27:17 +000048 PersonalityPrefix(""),
49 PersonalitySuffix(""),
Bill Wendlingef4a6612007-09-11 17:20:55 +000050 NeedsIndirectEncoding(false),
Jim Laskeyec0d9fe2006-09-06 18:35:33 +000051 InlineAsmStart("#APP"),
52 InlineAsmEnd("#NO_APP"),
Bill Wendlingeb9a42c2007-01-16 03:42:04 +000053 AssemblerDialect(0),
Dale Johannesen428ac542008-06-03 18:09:06 +000054 StringConstantPrefix(".str"),
Jim Laskeyec0d9fe2006-09-06 18:35:33 +000055 ZeroDirective("\t.zero\t"),
56 ZeroDirectiveSuffix(0),
57 AsciiDirective("\t.ascii\t"),
58 AscizDirective("\t.asciz\t"),
59 Data8bitsDirective("\t.byte\t"),
60 Data16bitsDirective("\t.short\t"),
61 Data32bitsDirective("\t.long\t"),
62 Data64bitsDirective("\t.quad\t"),
63 AlignDirective("\t.align\t"),
64 AlignmentIsInBytes(true),
Lauro Ramos Venanciof3c630e2008-02-28 22:14:09 +000065 TextAlignFillValue(0),
Jim Laskeyec0d9fe2006-09-06 18:35:33 +000066 SwitchToSectionDirective("\t.section\t"),
67 TextSectionStartSuffix(""),
68 DataSectionStartSuffix(""),
69 SectionEndDirectiveSuffix(0),
Dan Gohmanaf67ea72007-06-14 15:00:27 +000070 ConstantPoolSection("\t.section .rodata"),
71 JumpTableDataSection("\t.section .rodata"),
Andrew Lenharthbeec30e2006-09-24 19:45:58 +000072 JumpTableDirective(0),
Reid Spencerc50209b2006-10-27 16:14:06 +000073 CStringSection(0),
Jim Laskeyec0d9fe2006-09-06 18:35:33 +000074 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
75 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
76 FourByteConstantSection(0),
77 EightByteConstantSection(0),
78 SixteenByteConstantSection(0),
Evan Chengbe346c92007-03-08 01:00:38 +000079 ReadOnlySection(0),
Gordon Henriksen81361d62007-12-23 20:58:16 +000080 GlobalDirective("\t.globl\t"),
Jim Laskeyec0d9fe2006-09-06 18:35:33 +000081 SetDirective(0),
82 LCOMMDirective(0),
83 COMMDirective("\t.comm\t"),
84 COMMDirectiveTakesAlignment(true),
85 HasDotTypeDotSizeDirective(true),
Chris Lattnercb05af82006-09-26 03:38:18 +000086 UsedDirective(0),
Evan Cheng8752ce62006-12-01 20:47:11 +000087 WeakRefDirective(0),
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +000088 WeakDefDirective(0),
Chris Lattner9784bc72007-01-14 06:27:21 +000089 HiddenDirective("\t.hidden\t"),
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +000090 ProtectedDirective("\t.protected\t"),
Anton Korobeynikov79dda2b2007-05-01 22:23:12 +000091 AbsoluteDebugSectionOffsets(false),
92 AbsoluteEHSectionOffsets(false),
Jim Laskeyec0d9fe2006-09-06 18:35:33 +000093 HasLEB128(false),
Dan Gohman72252732007-09-24 21:09:53 +000094 HasDotLocAndDotFile(false),
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +000095 SupportsDebugInformation(false),
Jim Laskey072200c2007-01-29 18:51:14 +000096 SupportsExceptionHandling(false),
Reid Spencer02b85112006-10-30 22:32:30 +000097 DwarfRequiresFrameSection(true),
Dale Johannesen9305dcb2007-11-21 00:45:00 +000098 GlobalEHDirective(0),
Dale Johannesen038129d2008-01-10 02:03:30 +000099 SupportsWeakOmittedEHFrame(true),
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000100 DwarfSectionOffsetDirective(0),
Jim Laskeyec0d9fe2006-09-06 18:35:33 +0000101 DwarfAbbrevSection(".debug_abbrev"),
102 DwarfInfoSection(".debug_info"),
103 DwarfLineSection(".debug_line"),
104 DwarfFrameSection(".debug_frame"),
105 DwarfPubNamesSection(".debug_pubnames"),
106 DwarfPubTypesSection(".debug_pubtypes"),
107 DwarfStrSection(".debug_str"),
108 DwarfLocSection(".debug_loc"),
109 DwarfARangesSection(".debug_aranges"),
110 DwarfRangesSection(".debug_ranges"),
Andrew Lenharth3655de62006-11-28 19:52:20 +0000111 DwarfMacInfoSection(".debug_macinfo"),
Jim Laskeyb82313f2007-02-01 16:31:34 +0000112 DwarfEHFrameSection(".eh_frame"),
Jim Laskeya15be8c2007-02-21 22:43:40 +0000113 DwarfExceptionSection(".gcc_except_table"),
Andrew Lenharth3655de62006-11-28 19:52:20 +0000114 AsmTransCBE(0) {
Chris Lattner4c7b07a2006-10-13 17:50:07 +0000115}
Chris Lattnerf5b10ec2006-10-05 00:35:16 +0000116
117TargetAsmInfo::~TargetAsmInfo() {
118}
Chris Lattner4c7b07a2006-10-13 17:50:07 +0000119
120/// Measure the specified inline asm to determine an approximation of its
121/// length.
Dale Johannesen3bb62832007-04-23 20:00:17 +0000122/// Comments (which run till the next SeparatorChar or newline) do not
123/// count as an instruction.
124/// Any other non-whitespace text is considered an instruction, with
125/// multiple instructions separated by SeparatorChar or newlines.
126/// Variable-length instructions are not handled here; this function
127/// may be overloaded in the target code to do that.
Chris Lattner4c7b07a2006-10-13 17:50:07 +0000128unsigned TargetAsmInfo::getInlineAsmLength(const char *Str) const {
129 // Count the number of instructions in the asm.
Dale Johannesen3bb62832007-04-23 20:00:17 +0000130 bool atInsnStart = true;
131 unsigned Length = 0;
Chris Lattner4c7b07a2006-10-13 17:50:07 +0000132 for (; *Str; ++Str) {
133 if (*Str == '\n' || *Str == SeparatorChar)
Dale Johannesen3bb62832007-04-23 20:00:17 +0000134 atInsnStart = true;
135 if (atInsnStart && !isspace(*Str)) {
136 Length += MaxInstLength;
137 atInsnStart = false;
138 }
139 if (atInsnStart && strncmp(Str, CommentString, strlen(CommentString))==0)
140 atInsnStart = false;
Chris Lattner4c7b07a2006-10-13 17:50:07 +0000141 }
142
Dale Johannesen3bb62832007-04-23 20:00:17 +0000143 return Length;
Chris Lattner4c7b07a2006-10-13 17:50:07 +0000144}
Anton Korobeynikova6199c82007-03-07 02:47:57 +0000145
Anton Korobeynikov8213f9c2008-02-29 22:09:08 +0000146unsigned TargetAsmInfo::PreferredEHDataFormat(DwarfEncoding::Target Reason,
Anton Korobeynikovcee750f2008-02-27 23:33:50 +0000147 bool Global) const {
148 return dwarf::DW_EH_PE_absptr;
149}
150
Anton Korobeynikov28a2b542008-06-28 13:45:57 +0000151static bool isSuitableForBSS(const GlobalVariable *GV) {
152 if (!GV->hasInitializer())
153 return true;
154
155 // Leave constant zeros in readonly constant sections, so they can be shared
156 Constant *C = GV->getInitializer();
157 return (C->isNullValue() && !GV->isConstant() && !NoZerosInBSS);
158}
159
160SectionKind::Kind
161TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
162 // Early exit - functions should be always in text sections.
163 if (isa<Function>(GV))
164 return SectionKind::Text;
165
166 const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV);
167 bool isThreadLocal = GVar->isThreadLocal();
168 assert(GVar && "Invalid global value for section selection");
169
170 SectionKind::Kind kind;
171 if (isSuitableForBSS(GVar)) {
172 // Variable can be easily put to BSS section.
173 return (isThreadLocal ? SectionKind::ThreadBSS : SectionKind::BSS);
174 } else if (GVar->isConstant() && !isThreadLocal) {
175 // Now we know, that varible has initializer and it is constant. We need to
176 // check its initializer to decide, which section to output it into. Also
177 // note, there is no thread-local r/o section.
178 Constant *C = GVar->getInitializer();
179 if (C->ContainsRelocations())
180 kind = SectionKind::ROData;
181 else {
182 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
183 // Check, if initializer is a null-terminated string
184 if (CVA && CVA->isCString())
185 kind = SectionKind::RODataMergeStr;
186 else
187 kind = SectionKind::RODataMergeConst;
188 }
189 }
190
191 // Variable is not constant or thread-local - emit to generic data section.
192 return (isThreadLocal ? SectionKind::ThreadData : SectionKind::Data);
193}
Anton Korobeynikovbeb9d402008-07-09 13:16:59 +0000194
195unsigned
196TargetAsmInfo::SectionFlagsForGlobal(const GlobalValue *GV,
197 const char* name) {
198 unsigned flags = SectionFlags::None;
199
200 // Decode flags from global itself.
201 if (GV) {
202 SectionKind::Kind kind = SectionKindForGlobal(GV);
203 switch (kind) {
204 case SectionKind::Text:
205 flags |= SectionFlags::Code;
206 break;
207 case SectionKind::ThreadData:
208 flags |= SectionFlags::TLS;
209 // FALLS THROUGH
210 case SectionKind::Data:
211 flags |= SectionFlags::Writeable;
212 break;
213 case SectionKind::ThreadBSS:
214 flags |= SectionFlags::TLS;
215 // FALLS THROUGH
216 case SectionKind::BSS:
217 flags |= SectionFlags::BSS;
218 break;
219 case SectionKind::ROData:
220 // No additional flags here
221 break;
222 case SectionKind::RODataMergeStr:
223 flags |= SectionFlags::Strings;
224 // FALLS THROUGH
225 case SectionKind::RODataMergeConst:
226 flags |= SectionFlags::Mergeable;
227 break;
228 default:
229 assert(0 && "Unexpected section kind!");
230 }
231
232 if (GV->hasLinkOnceLinkage() ||
233 GV->hasWeakLinkage() ||
234 GV->hasCommonLinkage())
235 flags |= SectionFlags::Linkonce;
236 }
237
238 // Add flags from sections, if any.
239 if (name) {
240 // Some lame default implementation
241 if (strcmp(name, ".bss") == 0 ||
242 strncmp(name, ".bss.", 5) == 0 ||
243 strncmp(name, ".gnu.linkonce.b.", 16) == 0)
244 flags |= SectionFlags::BSS;
245 else if (strcmp(name, ".tdata") == 0 ||
246 strncmp(name, ".tdata.", 7) == 0 ||
247 strncmp(name, ".gnu.linkonce.td.", 17) == 0)
248 flags |= SectionFlags::TLS;
249 else if (strcmp(name, ".tbss") == 0 ||
250 strncmp(name, ".tbss.", 6) == 0 ||
251 strncmp(name, ".gnu.linkonce.tb.", 17) == 0)
252 flags |= SectionFlags::BSS | SectionFlags::TLS;
253 }
254
255 return flags;
256}