blob: 5612515027ad415a3b1b248112e0aa459d5176bd [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);
Anton Korobeynikov64818732008-09-24 22:18:54 +000032 FourByteConstantSection = getUnnamedSection("\t.literal4\n",
33 SectionFlags::Mergeable);
34 EightByteConstantSection = getUnnamedSection("\t.literal8\n",
Anton Korobeynikov745e8642008-07-19 13:14:46 +000035 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
Anton Korobeynikov64818732008-09-24 22:18:54 +000038 // there, if needed.
39 SixteenByteConstantSection = 0;
Anton Korobeynikovcff2ea02008-07-19 13:15:46 +000040
Anton Korobeynikov745e8642008-07-19 13:14:46 +000041 ReadOnlySection_ = getUnnamedSection("\t.const\n", SectionFlags::None);
42
43 // FIXME: These should be named sections, really.
44 TextCoalSection =
45 getUnnamedSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
46 SectionFlags::Code);
47 ConstDataCoalSection =
48 getUnnamedSection(".section __DATA,__const_coal,coalesced",
49 SectionFlags::None);
50 ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
51 DataCoalSection = getUnnamedSection(".section __DATA,__datacoal_nt,coalesced",
52 SectionFlags::Writeable);
53}
54
Dale Johannesend2e51af2008-09-09 22:29:13 +000055/// emitUsedDirectiveFor - On Darwin, internally linked data beginning with
56/// the PrivateGlobalPrefix or the LessPrivateGlobalPrefix does not have the
57/// directive emitted (this occurs in ObjC metadata).
58
59bool
60DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV,
61 Mangler *Mang) const {
62 if (GV==0)
63 return false;
64 if (GV->hasInternalLinkage() && !isa<Function>(GV) &&
65 ((strlen(getPrivateGlobalPrefix()) != 0 &&
66 Mang->getValueName(GV).substr(0,strlen(getPrivateGlobalPrefix())) ==
67 getPrivateGlobalPrefix()) ||
68 (strlen(getLessPrivateGlobalPrefix()) != 0 &&
69 Mang->getValueName(GV).substr(0,strlen(getLessPrivateGlobalPrefix())) ==
70 getLessPrivateGlobalPrefix())))
71 return false;
72 return true;
73}
74
Anton Korobeynikov745e8642008-07-19 13:14:46 +000075const Section*
Evan Cheng42ccc212008-08-08 17:56:50 +000076DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
Anton Korobeynikov745e8642008-07-19 13:14:46 +000077 SectionKind::Kind Kind = SectionKindForGlobal(GV);
Evan Cheng42ccc212008-08-08 17:56:50 +000078 bool isWeak = GV->isWeakForLinker();
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +000079 bool isNonStatic = (DTM->getRelocationModel() != Reloc::Static);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000080
81 switch (Kind) {
82 case SectionKind::Text:
Evan Cheng42ccc212008-08-08 17:56:50 +000083 if (isWeak)
Anton Korobeynikov745e8642008-07-19 13:14:46 +000084 return TextCoalSection;
85 else
Anton Korobeynikov0b501d12008-09-24 22:16:33 +000086 return TextSection;
Anton Korobeynikov745e8642008-07-19 13:14:46 +000087 case SectionKind::Data:
88 case SectionKind::ThreadData:
89 case SectionKind::BSS:
90 case SectionKind::ThreadBSS:
91 if (cast<GlobalVariable>(GV)->isConstant())
Evan Cheng42ccc212008-08-08 17:56:50 +000092 return (isWeak ? ConstDataCoalSection : ConstDataSection);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000093 else
Anton Korobeynikov0b501d12008-09-24 22:16:33 +000094 return (isWeak ? DataCoalSection : DataSection);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000095 case SectionKind::ROData:
Evan Cheng42ccc212008-08-08 17:56:50 +000096 return (isWeak ? ConstDataCoalSection :
Anton Korobeynikov745e8642008-07-19 13:14:46 +000097 (isNonStatic ? ConstDataSection : getReadOnlySection_()));
98 case SectionKind::RODataMergeStr:
Evan Cheng42ccc212008-08-08 17:56:50 +000099 return (isWeak ?
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000100 ConstDataCoalSection :
101 MergeableStringSection(cast<GlobalVariable>(GV)));
102 case SectionKind::RODataMergeConst:
Evan Cheng42ccc212008-08-08 17:56:50 +0000103 return (isWeak ?
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000104 ConstDataCoalSection:
105 MergeableConstSection(cast<GlobalVariable>(GV)));
106 default:
107 assert(0 && "Unsuported section kind for global");
108 }
109
110 // FIXME: Do we have any extra special weird cases?
111}
112
113const Section*
114DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +0000115 const TargetData *TD = DTM->getTargetData();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000116 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
117 const Type *Type = cast<ConstantArray>(C)->getType()->getElementType();
118
119 unsigned Size = TD->getABITypeSize(Type);
120 if (Size) {
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +0000121 const TargetData *TD = DTM->getTargetData();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000122 unsigned Align = TD->getPreferredAlignment(GV);
123 if (Align <= 32)
124 return getCStringSection_();
125 }
126
127 return getReadOnlySection_();
128}
129
130const Section*
131DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
Anton Korobeynikovdbab2d22008-09-24 22:17:27 +0000132 Constant *C = GV->getInitializer();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000133
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000134 return MergeableConstSection(C->getType());
135}
136
137inline const Section*
138DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
139 const TargetData *TD = DTM->getTargetData();
140
141 unsigned Size = TD->getABITypeSize(Ty);
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000142 if (Size == 4)
Anton Korobeynikov64818732008-09-24 22:18:54 +0000143 return FourByteConstantSection;
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000144 else if (Size == 8)
Anton Korobeynikov64818732008-09-24 22:18:54 +0000145 return EightByteConstantSection;
146 else if (Size == 16 && SixteenByteConstantSection)
147 return SixteenByteConstantSection;
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000148
149 return getReadOnlySection_();
150}
151
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000152const Section*
153DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
154 const Section* S = MergeableConstSection(Ty);
155
156 // Handle weird special case, when compiling PIC stuff.
157 if (S == getReadOnlySection_() &&
158 DTM->getRelocationModel() != Reloc::Static)
159 return ConstDataSection;
160
161 return S;
162}
163
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000164std::string
165DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
166 SectionKind::Kind kind) const {
167 assert(0 && "Darwin does not use unique sections");
168 return "";
169}