blob: 311126d01134a4d06ef12efe43855a9007d2128f [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 Korobeynikovcff2ea02008-07-19 13:15:46 +000035 // Note: 16-byte constant section is subtarget specific and should be provided
36 // there.
37
Anton Korobeynikov745e8642008-07-19 13:14:46 +000038 ReadOnlySection_ = getUnnamedSection("\t.const\n", SectionFlags::None);
39
40 // FIXME: These should be named sections, really.
41 TextCoalSection =
42 getUnnamedSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
43 SectionFlags::Code);
44 ConstDataCoalSection =
45 getUnnamedSection(".section __DATA,__const_coal,coalesced",
46 SectionFlags::None);
47 ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
48 DataCoalSection = getUnnamedSection(".section __DATA,__datacoal_nt,coalesced",
49 SectionFlags::Writeable);
50}
51
52const Section*
53DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
54 SectionKind::Kind Kind = SectionKindForGlobal(GV);
55 bool isWeak = GV->isWeakForLinker();
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +000056 bool isNonStatic = (DTM->getRelocationModel() != Reloc::Static);
Anton Korobeynikov745e8642008-07-19 13:14:46 +000057
58 switch (Kind) {
59 case SectionKind::Text:
60 if (isWeak)
61 return TextCoalSection;
62 else
63 return getTextSection_();
64 case SectionKind::Data:
65 case SectionKind::ThreadData:
66 case SectionKind::BSS:
67 case SectionKind::ThreadBSS:
68 if (cast<GlobalVariable>(GV)->isConstant())
69 return (isWeak ? ConstDataCoalSection : ConstDataSection);
70 else
71 return (isWeak ? DataCoalSection : getDataSection_());
72 case SectionKind::ROData:
73 return (isWeak ? ConstDataCoalSection :
74 (isNonStatic ? ConstDataSection : getReadOnlySection_()));
75 case SectionKind::RODataMergeStr:
76 return (isWeak ?
77 ConstDataCoalSection :
78 MergeableStringSection(cast<GlobalVariable>(GV)));
79 case SectionKind::RODataMergeConst:
80 return (isWeak ?
81 ConstDataCoalSection:
82 MergeableConstSection(cast<GlobalVariable>(GV)));
83 default:
84 assert(0 && "Unsuported section kind for global");
85 }
86
87 // FIXME: Do we have any extra special weird cases?
88}
89
90const Section*
91DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +000092 const TargetData *TD = DTM->getTargetData();
Anton Korobeynikov745e8642008-07-19 13:14:46 +000093 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
94 const Type *Type = cast<ConstantArray>(C)->getType()->getElementType();
95
96 unsigned Size = TD->getABITypeSize(Type);
97 if (Size) {
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +000098 const TargetData *TD = DTM->getTargetData();
Anton Korobeynikov745e8642008-07-19 13:14:46 +000099 unsigned Align = TD->getPreferredAlignment(GV);
100 if (Align <= 32)
101 return getCStringSection_();
102 }
103
104 return getReadOnlySection_();
105}
106
107const Section*
108DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
Anton Korobeynikov18f6ed92008-07-19 13:15:21 +0000109 const TargetData *TD = DTM->getTargetData();
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000110 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
111
112 unsigned Size = TD->getABITypeSize(C->getType());
113 if (Size == 4)
114 return FourByteConstantSection_;
115 else if (Size == 8)
116 return EightByteConstantSection_;
Anton Korobeynikovcff2ea02008-07-19 13:15:46 +0000117 else if (Size == 16 && SixteenByteConstantSection_)
118 return SixteenByteConstantSection_;
Anton Korobeynikov745e8642008-07-19 13:14:46 +0000119
120 return getReadOnlySection_();
121}
122
123std::string
124DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
125 SectionKind::Kind kind) const {
126 assert(0 && "Darwin does not use unique sections");
127 return "";
128}