Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 1 | //===-- 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 Johannesen | d2e51af | 2008-09-09 22:29:13 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Mangler.h" |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 21 | #include "llvm/Target/DarwinTargetAsmInfo.h" |
| 22 | #include "llvm/Target/TargetMachine.h" |
| 23 | #include "llvm/Target/TargetData.h" |
| 24 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | DarwinTargetAsmInfo::DarwinTargetAsmInfo(const TargetMachine &TM) { |
Anton Korobeynikov | 18f6ed9 | 2008-07-19 13:15:21 +0000 | [diff] [blame] | 28 | DTM = &TM; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 29 | |
| 30 | CStringSection_ = getUnnamedSection("\t.cstring", |
| 31 | SectionFlags::Mergeable | SectionFlags::Strings); |
| 32 | FourByteConstantSection_ = getUnnamedSection("\t.literal4\n", |
| 33 | SectionFlags::Mergeable); |
| 34 | EightByteConstantSection_ = getUnnamedSection("\t.literal8\n", |
| 35 | SectionFlags::Mergeable); |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 36 | |
Anton Korobeynikov | cff2ea0 | 2008-07-19 13:15:46 +0000 | [diff] [blame] | 37 | // Note: 16-byte constant section is subtarget specific and should be provided |
| 38 | // there. |
| 39 | |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 40 | ReadOnlySection_ = getUnnamedSection("\t.const\n", SectionFlags::None); |
| 41 | |
| 42 | // FIXME: These should be named sections, really. |
| 43 | TextCoalSection = |
| 44 | getUnnamedSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions", |
| 45 | SectionFlags::Code); |
| 46 | ConstDataCoalSection = |
| 47 | getUnnamedSection(".section __DATA,__const_coal,coalesced", |
| 48 | SectionFlags::None); |
| 49 | ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None); |
| 50 | DataCoalSection = getUnnamedSection(".section __DATA,__datacoal_nt,coalesced", |
| 51 | SectionFlags::Writeable); |
| 52 | } |
| 53 | |
Dale Johannesen | d2e51af | 2008-09-09 22:29:13 +0000 | [diff] [blame] | 54 | /// emitUsedDirectiveFor - On Darwin, internally linked data beginning with |
| 55 | /// the PrivateGlobalPrefix or the LessPrivateGlobalPrefix does not have the |
| 56 | /// directive emitted (this occurs in ObjC metadata). |
| 57 | |
| 58 | bool |
| 59 | DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV, |
| 60 | Mangler *Mang) const { |
| 61 | if (GV==0) |
| 62 | return false; |
| 63 | if (GV->hasInternalLinkage() && !isa<Function>(GV) && |
| 64 | ((strlen(getPrivateGlobalPrefix()) != 0 && |
| 65 | Mang->getValueName(GV).substr(0,strlen(getPrivateGlobalPrefix())) == |
| 66 | getPrivateGlobalPrefix()) || |
| 67 | (strlen(getLessPrivateGlobalPrefix()) != 0 && |
| 68 | Mang->getValueName(GV).substr(0,strlen(getLessPrivateGlobalPrefix())) == |
| 69 | getLessPrivateGlobalPrefix()))) |
| 70 | return false; |
| 71 | return true; |
| 72 | } |
| 73 | |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 74 | const Section* |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 75 | DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const { |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 76 | SectionKind::Kind Kind = SectionKindForGlobal(GV); |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 77 | bool isWeak = GV->isWeakForLinker(); |
Anton Korobeynikov | 18f6ed9 | 2008-07-19 13:15:21 +0000 | [diff] [blame] | 78 | bool isNonStatic = (DTM->getRelocationModel() != Reloc::Static); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 79 | |
| 80 | switch (Kind) { |
| 81 | case SectionKind::Text: |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 82 | if (isWeak) |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 83 | return TextCoalSection; |
| 84 | else |
Anton Korobeynikov | 0b501d1 | 2008-09-24 22:16:33 +0000 | [diff] [blame] | 85 | return TextSection; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 86 | case SectionKind::Data: |
| 87 | case SectionKind::ThreadData: |
| 88 | case SectionKind::BSS: |
| 89 | case SectionKind::ThreadBSS: |
| 90 | if (cast<GlobalVariable>(GV)->isConstant()) |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 91 | return (isWeak ? ConstDataCoalSection : ConstDataSection); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 92 | else |
Anton Korobeynikov | 0b501d1 | 2008-09-24 22:16:33 +0000 | [diff] [blame] | 93 | return (isWeak ? DataCoalSection : DataSection); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 94 | case SectionKind::ROData: |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 95 | return (isWeak ? ConstDataCoalSection : |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 96 | (isNonStatic ? ConstDataSection : getReadOnlySection_())); |
| 97 | case SectionKind::RODataMergeStr: |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 98 | return (isWeak ? |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 99 | ConstDataCoalSection : |
| 100 | MergeableStringSection(cast<GlobalVariable>(GV))); |
| 101 | case SectionKind::RODataMergeConst: |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 102 | return (isWeak ? |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 103 | ConstDataCoalSection: |
| 104 | MergeableConstSection(cast<GlobalVariable>(GV))); |
| 105 | default: |
| 106 | assert(0 && "Unsuported section kind for global"); |
| 107 | } |
| 108 | |
| 109 | // FIXME: Do we have any extra special weird cases? |
| 110 | } |
| 111 | |
| 112 | const Section* |
| 113 | DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const { |
Anton Korobeynikov | 18f6ed9 | 2008-07-19 13:15:21 +0000 | [diff] [blame] | 114 | const TargetData *TD = DTM->getTargetData(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 115 | Constant *C = cast<GlobalVariable>(GV)->getInitializer(); |
| 116 | const Type *Type = cast<ConstantArray>(C)->getType()->getElementType(); |
| 117 | |
| 118 | unsigned Size = TD->getABITypeSize(Type); |
| 119 | if (Size) { |
Anton Korobeynikov | 18f6ed9 | 2008-07-19 13:15:21 +0000 | [diff] [blame] | 120 | const TargetData *TD = DTM->getTargetData(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 121 | unsigned Align = TD->getPreferredAlignment(GV); |
| 122 | if (Align <= 32) |
| 123 | return getCStringSection_(); |
| 124 | } |
| 125 | |
| 126 | return getReadOnlySection_(); |
| 127 | } |
| 128 | |
| 129 | const Section* |
| 130 | DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const { |
Anton Korobeynikov | dbab2d2 | 2008-09-24 22:17:27 +0000 | [diff] [blame] | 131 | Constant *C = GV->getInitializer(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 132 | |
Anton Korobeynikov | 84e160e | 2008-08-07 09:51:02 +0000 | [diff] [blame] | 133 | return MergeableConstSection(C->getType()); |
| 134 | } |
| 135 | |
| 136 | inline const Section* |
| 137 | DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const { |
| 138 | const TargetData *TD = DTM->getTargetData(); |
| 139 | |
| 140 | unsigned Size = TD->getABITypeSize(Ty); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 141 | if (Size == 4) |
| 142 | return FourByteConstantSection_; |
| 143 | else if (Size == 8) |
| 144 | return EightByteConstantSection_; |
Anton Korobeynikov | cff2ea0 | 2008-07-19 13:15:46 +0000 | [diff] [blame] | 145 | else if (Size == 16 && SixteenByteConstantSection_) |
| 146 | return SixteenByteConstantSection_; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 147 | |
| 148 | return getReadOnlySection_(); |
| 149 | } |
| 150 | |
Anton Korobeynikov | 84e160e | 2008-08-07 09:51:02 +0000 | [diff] [blame] | 151 | const Section* |
| 152 | DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const { |
| 153 | const Section* S = MergeableConstSection(Ty); |
| 154 | |
| 155 | // Handle weird special case, when compiling PIC stuff. |
| 156 | if (S == getReadOnlySection_() && |
| 157 | DTM->getRelocationModel() != Reloc::Static) |
| 158 | return ConstDataSection; |
| 159 | |
| 160 | return S; |
| 161 | } |
| 162 | |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 163 | std::string |
| 164 | DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV, |
| 165 | SectionKind::Kind kind) const { |
| 166 | assert(0 && "Darwin does not use unique sections"); |
| 167 | return ""; |
| 168 | } |