blob: 978f5cf36ac15c5672534d50bc7315168456ba0e [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"
20#include "llvm/Target/DarwinTargetAsmInfo.h"
21#include "llvm/Target/TargetMachine.h"
22#include "llvm/Target/TargetData.h"
23
24using namespace llvm;
25
26DarwinTargetAsmInfo::DarwinTargetAsmInfo(const TargetMachine &TM) {
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +000027 DTM = &TM;
Anton Korobeynikov745e8642008-07-19 13:14:46 +000028
29 CStringSection_ = getUnnamedSection("\t.cstring",
30 SectionFlags::Mergeable | SectionFlags::Strings);
31 FourByteConstantSection_ = getUnnamedSection("\t.literal4\n",
32 SectionFlags::Mergeable);
33 EightByteConstantSection_ = getUnnamedSection("\t.literal8\n",
34 SectionFlags::Mergeable);
Anton Korobeynikovae408e62008-07-19 13:16:11 +000035
Anton Korobeynikovcff2ea02008-07-19 13:15:46 +000036 // Note: 16-byte constant section is subtarget specific and should be provided
37 // there.
38
Anton Korobeynikov745e8642008-07-19 13:14:46 +000039 ReadOnlySection_ = getUnnamedSection("\t.const\n", SectionFlags::None);
40
41 // FIXME: These should be named sections, really.
42 TextCoalSection =
43 getUnnamedSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
44 SectionFlags::Code);
45 ConstDataCoalSection =
46 getUnnamedSection(".section __DATA,__const_coal,coalesced",
47 SectionFlags::None);
48 ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
49 DataCoalSection = getUnnamedSection(".section __DATA,__datacoal_nt,coalesced",
50 SectionFlags::Writeable);
51}
52
53const Section*
Evan Cheng711b6dc2008-08-08 06:56:16 +000054DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
55 bool NoCoalesce) const {
Anton Korobeynikov745e8642008-07-19 13:14:46 +000056 SectionKind::Kind Kind = SectionKindForGlobal(GV);
Evan Cheng711b6dc2008-08-08 06:56:16 +000057 bool CanCoalesce = !NoCoalesce && GV->isWeakForLinker();
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +000058 bool isNonStatic = (DTM->getRelocationModel() != Reloc::Static);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000059
60 switch (Kind) {
61 case SectionKind::Text:
Evan Cheng711b6dc2008-08-08 06:56:16 +000062 if (CanCoalesce)
Anton Korobeynikov745e8642008-07-19 13:14:46 +000063 return TextCoalSection;
64 else
65 return getTextSection_();
66 case SectionKind::Data:
67 case SectionKind::ThreadData:
68 case SectionKind::BSS:
69 case SectionKind::ThreadBSS:
70 if (cast<GlobalVariable>(GV)->isConstant())
Evan Cheng711b6dc2008-08-08 06:56:16 +000071 return (CanCoalesce ? ConstDataCoalSection : ConstDataSection);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000072 else
Evan Cheng711b6dc2008-08-08 06:56:16 +000073 return (CanCoalesce ? DataCoalSection : getDataSection_());
Anton Korobeynikov745e8642008-07-19 13:14:46 +000074 case SectionKind::ROData:
Evan Cheng711b6dc2008-08-08 06:56:16 +000075 return (CanCoalesce ? ConstDataCoalSection :
Anton Korobeynikov745e8642008-07-19 13:14:46 +000076 (isNonStatic ? ConstDataSection : getReadOnlySection_()));
77 case SectionKind::RODataMergeStr:
Evan Cheng711b6dc2008-08-08 06:56:16 +000078 return (CanCoalesce ?
Anton Korobeynikov745e8642008-07-19 13:14:46 +000079 ConstDataCoalSection :
80 MergeableStringSection(cast<GlobalVariable>(GV)));
81 case SectionKind::RODataMergeConst:
Evan Cheng711b6dc2008-08-08 06:56:16 +000082 return (CanCoalesce ?
Anton Korobeynikov745e8642008-07-19 13:14:46 +000083 ConstDataCoalSection:
84 MergeableConstSection(cast<GlobalVariable>(GV)));
85 default:
86 assert(0 && "Unsuported section kind for global");
87 }
88
89 // FIXME: Do we have any extra special weird cases?
90}
91
92const Section*
93DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +000094 const TargetData *TD = DTM->getTargetData();
Anton Korobeynikov745e8642008-07-19 13:14:46 +000095 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
96 const Type *Type = cast<ConstantArray>(C)->getType()->getElementType();
97
98 unsigned Size = TD->getABITypeSize(Type);
99 if (Size) {
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +0000100 const TargetData *TD = DTM->getTargetData();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000101 unsigned Align = TD->getPreferredAlignment(GV);
102 if (Align <= 32)
103 return getCStringSection_();
104 }
105
106 return getReadOnlySection_();
107}
108
109const Section*
110DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000111 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
112
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000113 return MergeableConstSection(C->getType());
114}
115
116inline const Section*
117DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
118 const TargetData *TD = DTM->getTargetData();
119
120 unsigned Size = TD->getABITypeSize(Ty);
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000121 if (Size == 4)
122 return FourByteConstantSection_;
123 else if (Size == 8)
124 return EightByteConstantSection_;
Anton Korobeynikovcff2ea02008-07-19 13:15:46 +0000125 else if (Size == 16 && SixteenByteConstantSection_)
126 return SixteenByteConstantSection_;
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000127
128 return getReadOnlySection_();
129}
130
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000131const Section*
132DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
133 const Section* S = MergeableConstSection(Ty);
134
135 // Handle weird special case, when compiling PIC stuff.
136 if (S == getReadOnlySection_() &&
137 DTM->getRelocationModel() != Reloc::Static)
138 return ConstDataSection;
139
140 return S;
141}
142
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000143std::string
144DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
145 SectionKind::Kind kind) const {
146 assert(0 && "Darwin does not use unique sections");
147 return "";
148}