blob: cb73d31d4bd3af5d59fc37d386c471648fa2bd6d [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
Dan Gohman8f092252008-11-03 18:22:42 +000027DarwinTargetAsmInfo::DarwinTargetAsmInfo(const TargetMachine &TM)
28 : TargetAsmInfo(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 Korobeynikov00181a32008-09-24 22:20:27 +000041 ReadOnlySection = getUnnamedSection("\t.const\n", SectionFlags::None);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000042
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);
Dale Johannesen585457e2008-10-08 21:49:47 +000046 ConstTextCoalSection = getNamedSection("\t__TEXT,__const_coal,coalesced",
47 SectionFlags::None);
Anton Korobeynikov00181a32008-09-24 22:20:27 +000048 ConstDataCoalSection = getNamedSection("\t__DATA,__const_coal,coalesced",
Anton Korobeynikovd79cda92008-09-24 22:19:13 +000049 SectionFlags::None);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000050 ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
Anton Korobeynikovd79cda92008-09-24 22:19:13 +000051 DataCoalSection = getNamedSection("\t__DATA,__datacoal_nt,coalesced",
52 SectionFlags::Writeable);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000053}
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);
Duncan Sands5df31862008-09-29 11:25:42 +000078 bool isWeak = GV->mayBeOverridden();
Dan Gohman8f092252008-11-03 18:22:42 +000079 bool isNonStatic = TM.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 Korobeynikov00181a32008-09-24 22:20:27 +000097 (isNonStatic ? ConstDataSection : getReadOnlySection()));
Anton Korobeynikov745e8642008-07-19 13:14:46 +000098 case SectionKind::RODataMergeStr:
Evan Cheng42ccc212008-08-08 17:56:50 +000099 return (isWeak ?
Dale Johannesen585457e2008-10-08 21:49:47 +0000100 ConstTextCoalSection :
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000101 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 {
Dan Gohman8f092252008-11-03 18:22:42 +0000115 const TargetData *TD = TM.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 Korobeynikov745e8642008-07-19 13:14:46 +0000121 unsigned Align = TD->getPreferredAlignment(GV);
122 if (Align <= 32)
123 return getCStringSection_();
124 }
125
Anton Korobeynikov00181a32008-09-24 22:20:27 +0000126 return getReadOnlySection();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000127}
128
129const Section*
130DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
Anton Korobeynikovdbab2d22008-09-24 22:17:27 +0000131 Constant *C = GV->getInitializer();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000132
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000133 return MergeableConstSection(C->getType());
134}
135
136inline const Section*
137DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
Dan Gohman8f092252008-11-03 18:22:42 +0000138 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000139
140 unsigned Size = TD->getABITypeSize(Ty);
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000141 if (Size == 4)
Anton Korobeynikov64818732008-09-24 22:18:54 +0000142 return FourByteConstantSection;
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000143 else if (Size == 8)
Anton Korobeynikov64818732008-09-24 22:18:54 +0000144 return EightByteConstantSection;
145 else if (Size == 16 && SixteenByteConstantSection)
146 return SixteenByteConstantSection;
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000147
Anton Korobeynikov00181a32008-09-24 22:20:27 +0000148 return getReadOnlySection();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000149}
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.
Anton Korobeynikov00181a32008-09-24 22:20:27 +0000156 if (S == getReadOnlySection() &&
Dan Gohman8f092252008-11-03 18:22:42 +0000157 TM.getRelocationModel() != Reloc::Static)
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000158 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}