blob: 55d0c429c75066c655dcfbc30b6f7704e54bc4d6 [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
Anton Korobeynikov745e8642008-07-19 13:14:46 +000043 TextCoalSection =
Anton Korobeynikovd79cda92008-09-24 22:19:13 +000044 getNamedSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
Anton Korobeynikov745e8642008-07-19 13:14:46 +000045 SectionFlags::Code);
Anton Korobeynikovd79cda92008-09-24 22:19:13 +000046 ConstDataCoalSection = getBamedSection("\t__DATA,__const_coal,coalesced",
47 SectionFlags::None);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000048 ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
Anton Korobeynikovd79cda92008-09-24 22:19:13 +000049 DataCoalSection = getNamedSection("\t__DATA,__datacoal_nt,coalesced",
50 SectionFlags::Writeable);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000051}
52
Dale Johannesend2e51af2008-09-09 22:29:13 +000053/// emitUsedDirectiveFor - On Darwin, internally linked data beginning with
54/// the PrivateGlobalPrefix or the LessPrivateGlobalPrefix does not have the
55/// directive emitted (this occurs in ObjC metadata).
56
57bool
58DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV,
59 Mangler *Mang) const {
60 if (GV==0)
61 return false;
62 if (GV->hasInternalLinkage() && !isa<Function>(GV) &&
63 ((strlen(getPrivateGlobalPrefix()) != 0 &&
64 Mang->getValueName(GV).substr(0,strlen(getPrivateGlobalPrefix())) ==
65 getPrivateGlobalPrefix()) ||
66 (strlen(getLessPrivateGlobalPrefix()) != 0 &&
67 Mang->getValueName(GV).substr(0,strlen(getLessPrivateGlobalPrefix())) ==
68 getLessPrivateGlobalPrefix())))
69 return false;
70 return true;
71}
72
Anton Korobeynikov745e8642008-07-19 13:14:46 +000073const Section*
Evan Cheng42ccc212008-08-08 17:56:50 +000074DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
Anton Korobeynikov745e8642008-07-19 13:14:46 +000075 SectionKind::Kind Kind = SectionKindForGlobal(GV);
Evan Cheng42ccc212008-08-08 17:56:50 +000076 bool isWeak = GV->isWeakForLinker();
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +000077 bool isNonStatic = (DTM->getRelocationModel() != Reloc::Static);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000078
79 switch (Kind) {
80 case SectionKind::Text:
Evan Cheng42ccc212008-08-08 17:56:50 +000081 if (isWeak)
Anton Korobeynikov745e8642008-07-19 13:14:46 +000082 return TextCoalSection;
83 else
Anton Korobeynikov0b501d12008-09-24 22:16:33 +000084 return TextSection;
Anton Korobeynikov745e8642008-07-19 13:14:46 +000085 case SectionKind::Data:
86 case SectionKind::ThreadData:
87 case SectionKind::BSS:
88 case SectionKind::ThreadBSS:
89 if (cast<GlobalVariable>(GV)->isConstant())
Evan Cheng42ccc212008-08-08 17:56:50 +000090 return (isWeak ? ConstDataCoalSection : ConstDataSection);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000091 else
Anton Korobeynikov0b501d12008-09-24 22:16:33 +000092 return (isWeak ? DataCoalSection : DataSection);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000093 case SectionKind::ROData:
Evan Cheng42ccc212008-08-08 17:56:50 +000094 return (isWeak ? ConstDataCoalSection :
Anton Korobeynikov745e8642008-07-19 13:14:46 +000095 (isNonStatic ? ConstDataSection : getReadOnlySection_()));
96 case SectionKind::RODataMergeStr:
Evan Cheng42ccc212008-08-08 17:56:50 +000097 return (isWeak ?
Anton Korobeynikov745e8642008-07-19 13:14:46 +000098 ConstDataCoalSection :
99 MergeableStringSection(cast<GlobalVariable>(GV)));
100 case SectionKind::RODataMergeConst:
Evan Cheng42ccc212008-08-08 17:56:50 +0000101 return (isWeak ?
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000102 ConstDataCoalSection:
103 MergeableConstSection(cast<GlobalVariable>(GV)));
104 default:
105 assert(0 && "Unsuported section kind for global");
106 }
107
108 // FIXME: Do we have any extra special weird cases?
109}
110
111const Section*
112DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +0000113 const TargetData *TD = DTM->getTargetData();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000114 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
115 const Type *Type = cast<ConstantArray>(C)->getType()->getElementType();
116
117 unsigned Size = TD->getABITypeSize(Type);
118 if (Size) {
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +0000119 const TargetData *TD = DTM->getTargetData();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000120 unsigned Align = TD->getPreferredAlignment(GV);
121 if (Align <= 32)
122 return getCStringSection_();
123 }
124
125 return getReadOnlySection_();
126}
127
128const Section*
129DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
Anton Korobeynikovdbab2d22008-09-24 22:17:27 +0000130 Constant *C = GV->getInitializer();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000131
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000132 return MergeableConstSection(C->getType());
133}
134
135inline const Section*
136DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
137 const TargetData *TD = DTM->getTargetData();
138
139 unsigned Size = TD->getABITypeSize(Ty);
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000140 if (Size == 4)
Anton Korobeynikov64818732008-09-24 22:18:54 +0000141 return FourByteConstantSection;
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000142 else if (Size == 8)
Anton Korobeynikov64818732008-09-24 22:18:54 +0000143 return EightByteConstantSection;
144 else if (Size == 16 && SixteenByteConstantSection)
145 return SixteenByteConstantSection;
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000146
147 return getReadOnlySection_();
148}
149
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000150const Section*
151DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
152 const Section* S = MergeableConstSection(Ty);
153
154 // Handle weird special case, when compiling PIC stuff.
155 if (S == getReadOnlySection_() &&
156 DTM->getRelocationModel() != Reloc::Static)
157 return ConstDataSection;
158
159 return S;
160}
161
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000162std::string
163DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
164 SectionKind::Kind kind) const {
165 assert(0 && "Darwin does not use unique sections");
166 return "";
167}