4529d0158d9cafbb18bc55242a99dfb6ae5b48a9
[oota-llvm.git] / lib / Target / ELFTargetAsmInfo.cpp
1 //===-- ELFTargetAsmInfo.cpp - ELF asm properties ---------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines target asm properties related what form asm statements
11 // should take in general on ELF-based targets
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/GlobalVariable.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/Target/ELFTargetAsmInfo.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetData.h"
24
25 using namespace llvm;
26
27 ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM) {
28   ETM = &TM;
29
30   DataSection_ = getUnnamedSection("\t.data", SectionFlags::Writeable);
31   BSSSection_  = getUnnamedSection("\t.bss",
32                                    SectionFlags::Writeable | SectionFlags::BSS);
33   ReadOnlySection_ = getNamedSection("\t.rodata", SectionFlags::None);
34   TLSDataSection_ = getNamedSection("\t.tdata",
35                                     SectionFlags::Writeable | SectionFlags::TLS);
36   TLSBSSSection_ = getNamedSection("\t.tbss",
37                 SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS);
38
39 }
40
41 const Section*
42 ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
43   SectionKind::Kind Kind = SectionKindForGlobal(GV);
44
45   if (const Function *F = dyn_cast<Function>(GV)) {
46     switch (F->getLinkage()) {
47      default: assert(0 && "Unknown linkage type!");
48      case Function::InternalLinkage:
49      case Function::DLLExportLinkage:
50      case Function::ExternalLinkage:
51       return getTextSection();
52      case Function::WeakLinkage:
53      case Function::LinkOnceLinkage:
54       std::string Name = UniqueSectionForGlobal(GV, Kind);
55       unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
56       return getNamedSection(Name.c_str(), Flags);
57     }
58   } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
59     if (GVar->isWeakForLinker()) {
60       std::string Name = UniqueSectionForGlobal(GVar, Kind);
61       unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
62       return getNamedSection(Name.c_str(), Flags);
63     } else {
64       switch (Kind) {
65        case SectionKind::Data:
66        case SectionKind::SmallData:
67         return getDataSection_();
68        case SectionKind::BSS:
69        case SectionKind::SmallBSS:
70         // ELF targets usually have BSS sections
71         return getBSSSection_();
72        case SectionKind::ROData:
73        case SectionKind::SmallROData:
74         return getReadOnlySection_();
75        case SectionKind::RODataMergeStr:
76         return MergeableStringSection(GVar);
77        case SectionKind::RODataMergeConst:
78         return MergeableConstSection(GVar);
79        case SectionKind::ThreadData:
80         // ELF targets usually support TLS stuff
81         return getTLSDataSection_();
82        case SectionKind::ThreadBSS:
83         return getTLSBSSSection_();
84        default:
85         assert(0 && "Unsuported section kind for global");
86       }
87     }
88   } else
89     assert(0 && "Unsupported global");
90 }
91
92 const Section*
93 ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
94   // FIXME: Support data.rel stuff someday
95   return MergeableConstSection(Ty);
96 }
97
98 const Section*
99 ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
100   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
101   return MergeableConstSection(C->getType());
102 }
103
104 inline const Section*
105 ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
106   const TargetData *TD = ETM->getTargetData();
107
108   // FIXME: string here is temporary, until stuff will fully land in.
109   // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
110   // currently directly used by asmprinter.
111   unsigned Size = TD->getABITypeSize(Ty);
112   if (Size == 4 || Size == 8 || Size == 16) {
113     std::string Name =  ".rodata.cst" + utostr(Size);
114
115     return getNamedSection(Name.c_str(),
116                            SectionFlags::setEntitySize(SectionFlags::Mergeable,
117                                                        Size));
118   }
119
120   return getReadOnlySection_();
121 }
122
123 const Section*
124 ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
125   const TargetData *TD = ETM->getTargetData();
126   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
127   const ConstantArray *CVA = cast<ConstantArray>(C);
128   const Type *Ty = CVA->getType()->getElementType();
129
130   unsigned Size = TD->getABITypeSize(Ty);
131   if (Size <= 16) {
132     assert(getCStringSection() && "Should have string section prefix");
133
134     // We also need alignment here
135     const TargetData *TD = ETM->getTargetData();
136     unsigned Align = TD->getPrefTypeAlignment(Ty);
137     if (Align < Size)
138       Align = Size;
139
140     std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
141     unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
142                                                  SectionFlags::Strings,
143                                                  Size);
144     return getNamedSection(Name.c_str(), Flags);
145   }
146
147   return getReadOnlySection_();
148 }
149
150 std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
151   std::string Flags = ",\"";
152
153   if (!(flags & SectionFlags::Debug))
154     Flags += 'a';
155   if (flags & SectionFlags::Code)
156     Flags += 'x';
157   if (flags & SectionFlags::Writeable)
158     Flags += 'w';
159   if (flags & SectionFlags::Mergeable)
160     Flags += 'M';
161   if (flags & SectionFlags::Strings)
162     Flags += 'S';
163   if (flags & SectionFlags::TLS)
164     Flags += 'T';
165   if (flags & SectionFlags::Small)
166     Flags += 's';
167
168   Flags += "\",";
169
170   // If comment string is '@', e.g. as on ARM - use '%' instead
171   if (strcmp(CommentString, "@") == 0)
172     Flags += '%';
173   else
174     Flags += '@';
175
176   // FIXME: There can be exceptions here
177   if (flags & SectionFlags::BSS)
178     Flags += "nobits";
179   else
180     Flags += "progbits";
181
182   if (unsigned entitySize = SectionFlags::getEntitySize(flags))
183     Flags += "," + utostr(entitySize);
184
185   return Flags;
186 }
187