blob: 6a53b6b25a5f70cae1d53a56a3e35023403f1d78 [file] [log] [blame]
Anton Korobeynikov745e8642008-07-19 13:14:46 +00001//===-- DarwinTargetAsmInfo.cpp - Darwin 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 Darwin-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"
Dale Johannesend2e51af2008-09-09 22:29:13 +000020#include "llvm/Support/Mangler.h"
Anton Korobeynikov745e8642008-07-19 13:14:46 +000021#include "llvm/Target/DarwinTargetAsmInfo.h"
22#include "llvm/Target/TargetMachine.h"
23#include "llvm/Target/TargetData.h"
24
25using namespace llvm;
26
27DarwinTargetAsmInfo::DarwinTargetAsmInfo(const TargetMachine &TM) {
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +000028 DTM = &TM;
Anton Korobeynikov745e8642008-07-19 13:14:46 +000029
30 CStringSection_ = getUnnamedSection("\t.cstring",
31 SectionFlags::Mergeable | SectionFlags::Strings);
32 FourByteConstantSection_ = getUnnamedSection("\t.literal4\n",
33 SectionFlags::Mergeable);
34 EightByteConstantSection_ = getUnnamedSection("\t.literal8\n",
35 SectionFlags::Mergeable);
Anton Korobeynikovae408e62008-07-19 13:16:11 +000036
Anton Korobeynikovcff2ea02008-07-19 13:15:46 +000037 // Note: 16-byte constant section is subtarget specific and should be provided
38 // there.
39
Anton Korobeynikov745e8642008-07-19 13:14:46 +000040 ReadOnlySection_ = getUnnamedSection("\t.const\n", SectionFlags::None);
41
42 // FIXME: These should be named sections, really.
43 TextCoalSection =
44 getUnnamedSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
45 SectionFlags::Code);
46 ConstDataCoalSection =
47 getUnnamedSection(".section __DATA,__const_coal,coalesced",
48 SectionFlags::None);
49 ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
50 DataCoalSection = getUnnamedSection(".section __DATA,__datacoal_nt,coalesced",
51 SectionFlags::Writeable);
52}
53
Dale Johannesend2e51af2008-09-09 22:29:13 +000054/// emitUsedDirectiveFor - On Darwin, internally linked data beginning with
55/// the PrivateGlobalPrefix or the LessPrivateGlobalPrefix does not have the
56/// directive emitted (this occurs in ObjC metadata).
57
58bool
59DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV,
60 Mangler *Mang) const {
61 if (GV==0)
62 return false;
63 if (GV->hasInternalLinkage() && !isa<Function>(GV) &&
64 ((strlen(getPrivateGlobalPrefix()) != 0 &&
65 Mang->getValueName(GV).substr(0,strlen(getPrivateGlobalPrefix())) ==
66 getPrivateGlobalPrefix()) ||
67 (strlen(getLessPrivateGlobalPrefix()) != 0 &&
68 Mang->getValueName(GV).substr(0,strlen(getLessPrivateGlobalPrefix())) ==
69 getLessPrivateGlobalPrefix())))
70 return false;
71 return true;
72}
73
Anton Korobeynikov745e8642008-07-19 13:14:46 +000074const Section*
Evan Cheng42ccc212008-08-08 17:56:50 +000075DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
Anton Korobeynikov745e8642008-07-19 13:14:46 +000076 SectionKind::Kind Kind = SectionKindForGlobal(GV);
Evan Cheng42ccc212008-08-08 17:56:50 +000077 bool isWeak = GV->isWeakForLinker();
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +000078 bool isNonStatic = (DTM->getRelocationModel() != Reloc::Static);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000079
80 switch (Kind) {
81 case SectionKind::Text:
Evan Cheng42ccc212008-08-08 17:56:50 +000082 if (isWeak)
Anton Korobeynikov745e8642008-07-19 13:14:46 +000083 return TextCoalSection;
84 else
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +000085 return getTextSection();
Anton Korobeynikov745e8642008-07-19 13:14:46 +000086 case SectionKind::Data:
87 case SectionKind::ThreadData:
88 case SectionKind::BSS:
89 case SectionKind::ThreadBSS:
90 if (cast<GlobalVariable>(GV)->isConstant())
Evan Cheng42ccc212008-08-08 17:56:50 +000091 return (isWeak ? ConstDataCoalSection : ConstDataSection);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000092 else
Anton Korobeynikov315690e2008-09-24 22:16:16 +000093 return (isWeak ? DataCoalSection : getDataSection());
Anton Korobeynikov745e8642008-07-19 13:14:46 +000094 case SectionKind::ROData:
Evan Cheng42ccc212008-08-08 17:56:50 +000095 return (isWeak ? ConstDataCoalSection :
Anton Korobeynikov745e8642008-07-19 13:14:46 +000096 (isNonStatic ? ConstDataSection : getReadOnlySection_()));
97 case SectionKind::RODataMergeStr:
Evan Cheng42ccc212008-08-08 17:56:50 +000098 return (isWeak ?
Anton Korobeynikov745e8642008-07-19 13:14:46 +000099 ConstDataCoalSection :
100 MergeableStringSection(cast<GlobalVariable>(GV)));
101 case SectionKind::RODataMergeConst:
Evan Cheng42ccc212008-08-08 17:56:50 +0000102 return (isWeak ?
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000103 ConstDataCoalSection:
104 MergeableConstSection(cast<GlobalVariable>(GV)));
105 default:
106 assert(0 && "Unsuported section kind for global");
107 }
108
109 // FIXME: Do we have any extra special weird cases?
110}
111
112const Section*
113DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +0000114 const TargetData *TD = DTM->getTargetData();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000115 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
116 const Type *Type = cast<ConstantArray>(C)->getType()->getElementType();
117
118 unsigned Size = TD->getABITypeSize(Type);
119 if (Size) {
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +0000120 const TargetData *TD = DTM->getTargetData();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000121 unsigned Align = TD->getPreferredAlignment(GV);
122 if (Align <= 32)
123 return getCStringSection_();
124 }
125
126 return getReadOnlySection_();
127}
128
129const Section*
130DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000131 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
132
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000133 return MergeableConstSection(C->getType());
134}
135
136inline const Section*
137DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
138 const TargetData *TD = DTM->getTargetData();
139
140 unsigned Size = TD->getABITypeSize(Ty);
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000141 if (Size == 4)
142 return FourByteConstantSection_;
143 else if (Size == 8)
144 return EightByteConstantSection_;
Anton Korobeynikovcff2ea02008-07-19 13:15:46 +0000145 else if (Size == 16 && SixteenByteConstantSection_)
146 return SixteenByteConstantSection_;
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000147
148 return getReadOnlySection_();
149}
150
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000151const Section*
152DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
153 const Section* S = MergeableConstSection(Ty);
154
155 // Handle weird special case, when compiling PIC stuff.
156 if (S == getReadOnlySection_() &&
157 DTM->getRelocationModel() != Reloc::Static)
158 return ConstDataSection;
159
160 return S;
161}
162
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000163std::string
164DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
165 SectionKind::Kind kind) const {
166 assert(0 && "Darwin does not use unique sections");
167 return "";
168}