Allow inline asm nodes with empty bodies inside JIT.
[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   return MergeableConstSection(C->getType());
103 }
104
105 inline const Section*
106 ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
107   const TargetData *TD = ETM->getTargetData();
108
109   // FIXME: string here is temporary, until stuff will fully land in.
110   // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
111   // currently directly used by asmprinter.
112   unsigned Size = TD->getABITypeSize(Ty);
113   if (Size == 4 || Size == 8 || Size == 16) {
114     std::string Name =  ".rodata.cst" + utostr(Size);
115
116     return getNamedSection(Name.c_str(),
117                            SectionFlags::setEntitySize(SectionFlags::Mergeable,
118                                                        Size));
119   }
120
121   return getReadOnlySection_();
122 }
123
124 const Section*
125 ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
126   const TargetData *TD = ETM->getTargetData();
127   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
128   const ConstantArray *CVA = cast<ConstantArray>(C);
129   const Type *Ty = CVA->getType()->getElementType();
130
131   unsigned Size = TD->getABITypeSize(Ty);
132   if (Size <= 16) {
133     assert(getCStringSection() && "Should have string section prefix");
134
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   // If comment string is '@', e.g. as on ARM - use '%' instead
172   if (strcmp(CommentString, "@") == 0)
173     Flags += '%';
174   else
175     Flags += '@';
176
177   // FIXME: There can be exceptions here
178   if (flags & SectionFlags::BSS)
179     Flags += "nobits";
180   else
181     Flags += "progbits";
182
183   if (unsigned entitySize = SectionFlags::getEntitySize(flags))
184     Flags += "," + utostr(entitySize);
185
186   return Flags;
187 }
188