Get rid of ReadOnlySection duplicate
[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   BSSSection_  = getUnnamedSection("\t.bss",
31                                    SectionFlags::Writeable | SectionFlags::BSS);
32   ReadOnlySection = getNamedSection("\t.rodata", SectionFlags::None);
33   TLSDataSection = getNamedSection("\t.tdata",
34                                    SectionFlags::Writeable | SectionFlags::TLS);
35   TLSBSSSection = getNamedSection("\t.tbss",
36                 SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS);
37
38 }
39
40 const Section*
41 ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
42   SectionKind::Kind Kind = SectionKindForGlobal(GV);
43
44   if (const Function *F = dyn_cast<Function>(GV)) {
45     switch (F->getLinkage()) {
46      default: assert(0 && "Unknown linkage type!");
47      case Function::InternalLinkage:
48      case Function::DLLExportLinkage:
49      case Function::ExternalLinkage:
50       return TextSection;
51      case Function::WeakLinkage:
52      case Function::LinkOnceLinkage:
53       std::string Name = UniqueSectionForGlobal(GV, Kind);
54       unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
55       return getNamedSection(Name.c_str(), Flags);
56     }
57   } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
58     if (GVar->isWeakForLinker()) {
59       std::string Name = UniqueSectionForGlobal(GVar, Kind);
60       unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
61       return getNamedSection(Name.c_str(), Flags);
62     } else {
63       switch (Kind) {
64        case SectionKind::Data:
65        case SectionKind::SmallData:
66         return DataSection;
67        case SectionKind::BSS:
68        case SectionKind::SmallBSS:
69         // ELF targets usually have BSS sections
70         return getBSSSection_();
71        case SectionKind::ROData:
72        case SectionKind::SmallROData:
73         return getReadOnlySection();
74        case SectionKind::RODataMergeStr:
75         return MergeableStringSection(GVar);
76        case SectionKind::RODataMergeConst:
77         return MergeableConstSection(GVar);
78        case SectionKind::ThreadData:
79         // ELF targets usually support TLS stuff
80         return TLSDataSection;
81        case SectionKind::ThreadBSS:
82         return TLSBSSSection;
83        default:
84         assert(0 && "Unsuported section kind for global");
85       }
86     }
87   } else
88     assert(0 && "Unsupported global");
89 }
90
91 const Section*
92 ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
93   // FIXME: Support data.rel stuff someday
94   return MergeableConstSection(Ty);
95 }
96
97 const Section*
98 ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
99   Constant *C = GV->getInitializer();
100   return MergeableConstSection(C->getType());
101 }
102
103 inline const Section*
104 ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
105   const TargetData *TD = ETM->getTargetData();
106
107   // FIXME: string here is temporary, until stuff will fully land in.
108   // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
109   // currently directly used by asmprinter.
110   unsigned Size = TD->getABITypeSize(Ty);
111   if (Size == 4 || Size == 8 || Size == 16) {
112     std::string Name =  ".rodata.cst" + utostr(Size);
113
114     return getNamedSection(Name.c_str(),
115                            SectionFlags::setEntitySize(SectionFlags::Mergeable,
116                                                        Size));
117   }
118
119   return getReadOnlySection();
120 }
121
122 const Section*
123 ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
124   const TargetData *TD = ETM->getTargetData();
125   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
126   const ConstantArray *CVA = cast<ConstantArray>(C);
127   const Type *Ty = CVA->getType()->getElementType();
128
129   unsigned Size = TD->getABITypeSize(Ty);
130   if (Size <= 16) {
131     assert(getCStringSection() && "Should have string section prefix");
132
133     // We also need alignment here
134     const TargetData *TD = ETM->getTargetData();
135     unsigned Align = TD->getPrefTypeAlignment(Ty);
136     if (Align < Size)
137       Align = Size;
138
139     std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
140     unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
141                                                  SectionFlags::Strings,
142                                                  Size);
143     return getNamedSection(Name.c_str(), Flags);
144   }
145
146   return getReadOnlySection();
147 }
148
149 std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
150   std::string Flags = ",\"";
151
152   if (!(flags & SectionFlags::Debug))
153     Flags += 'a';
154   if (flags & SectionFlags::Code)
155     Flags += 'x';
156   if (flags & SectionFlags::Writeable)
157     Flags += 'w';
158   if (flags & SectionFlags::Mergeable)
159     Flags += 'M';
160   if (flags & SectionFlags::Strings)
161     Flags += 'S';
162   if (flags & SectionFlags::TLS)
163     Flags += 'T';
164   if (flags & SectionFlags::Small)
165     Flags += 's';
166
167   Flags += "\",";
168
169   // If comment string is '@', e.g. as on ARM - use '%' instead
170   if (strcmp(CommentString, "@") == 0)
171     Flags += '%';
172   else
173     Flags += '@';
174
175   // FIXME: There can be exceptions here
176   if (flags & SectionFlags::BSS)
177     Flags += "nobits";
178   else
179     Flags += "progbits";
180
181   if (unsigned entitySize = SectionFlags::getEntitySize(flags))
182     Flags += "," + utostr(entitySize);
183
184   return Flags;
185 }
186