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