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 | |
Dan Gohman | 8f09225 | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 27 | DarwinTargetAsmInfo::DarwinTargetAsmInfo(const TargetMachine &TM) |
| 28 | : TargetAsmInfo(TM) { |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 29 | |
| 30 | CStringSection_ = getUnnamedSection("\t.cstring", |
| 31 | SectionFlags::Mergeable | SectionFlags::Strings); |
Anton Korobeynikov | 6481873 | 2008-09-24 22:18:54 +0000 | [diff] [blame] | 32 | FourByteConstantSection = getUnnamedSection("\t.literal4\n", |
| 33 | SectionFlags::Mergeable); |
| 34 | EightByteConstantSection = getUnnamedSection("\t.literal8\n", |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 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 |
Anton Korobeynikov | 6481873 | 2008-09-24 22:18:54 +0000 | [diff] [blame] | 38 | // there, if needed. |
| 39 | SixteenByteConstantSection = 0; |
Anton Korobeynikov | cff2ea0 | 2008-07-19 13:15:46 +0000 | [diff] [blame] | 40 | |
Anton Korobeynikov | 00181a3 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 41 | ReadOnlySection = getUnnamedSection("\t.const\n", SectionFlags::None); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 42 | |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 43 | TextCoalSection = |
Anton Korobeynikov | d79cda9 | 2008-09-24 22:19:13 +0000 | [diff] [blame] | 44 | getNamedSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions", |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 45 | SectionFlags::Code); |
Dale Johannesen | 585457e | 2008-10-08 21:49:47 +0000 | [diff] [blame] | 46 | ConstTextCoalSection = getNamedSection("\t__TEXT,__const_coal,coalesced", |
| 47 | SectionFlags::None); |
Anton Korobeynikov | 00181a3 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 48 | ConstDataCoalSection = getNamedSection("\t__DATA,__const_coal,coalesced", |
Anton Korobeynikov | d79cda9 | 2008-09-24 22:19:13 +0000 | [diff] [blame] | 49 | SectionFlags::None); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 50 | ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None); |
Anton Korobeynikov | d79cda9 | 2008-09-24 22:19:13 +0000 | [diff] [blame] | 51 | DataCoalSection = getNamedSection("\t__DATA,__datacoal_nt,coalesced", |
| 52 | SectionFlags::Writeable); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Dale Johannesen | d2e51af | 2008-09-09 22:29:13 +0000 | [diff] [blame] | 55 | /// 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 | |
| 59 | bool |
| 60 | DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV, |
| 61 | Mangler *Mang) const { |
| 62 | if (GV==0) |
| 63 | return false; |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 64 | if (GV->hasLocalLinkage() && !isa<Function>(GV) && |
Dale Johannesen | d2e51af | 2008-09-09 22:29:13 +0000 | [diff] [blame] | 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 Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 75 | const Section* |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 76 | DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const { |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 77 | SectionKind::Kind Kind = SectionKindForGlobal(GV); |
Duncan Sands | 5df3186 | 2008-09-29 11:25:42 +0000 | [diff] [blame] | 78 | bool isWeak = GV->mayBeOverridden(); |
Dan Gohman | 8f09225 | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 79 | bool isNonStatic = TM.getRelocationModel() != Reloc::Static; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 80 | |
| 81 | switch (Kind) { |
| 82 | case SectionKind::Text: |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 83 | if (isWeak) |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 84 | return TextCoalSection; |
| 85 | else |
Anton Korobeynikov | 0b501d1 | 2008-09-24 22:16:33 +0000 | [diff] [blame] | 86 | return TextSection; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 87 | case SectionKind::Data: |
| 88 | case SectionKind::ThreadData: |
| 89 | case SectionKind::BSS: |
| 90 | case SectionKind::ThreadBSS: |
| 91 | if (cast<GlobalVariable>(GV)->isConstant()) |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 92 | return (isWeak ? ConstDataCoalSection : ConstDataSection); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 93 | else |
Anton Korobeynikov | 0b501d1 | 2008-09-24 22:16:33 +0000 | [diff] [blame] | 94 | return (isWeak ? DataCoalSection : DataSection); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 95 | case SectionKind::ROData: |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 96 | return (isWeak ? ConstDataCoalSection : |
Anton Korobeynikov | 00181a3 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 97 | (isNonStatic ? ConstDataSection : getReadOnlySection())); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 98 | case SectionKind::RODataMergeStr: |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 99 | return (isWeak ? |
Dale Johannesen | 585457e | 2008-10-08 21:49:47 +0000 | [diff] [blame] | 100 | ConstTextCoalSection : |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 101 | MergeableStringSection(cast<GlobalVariable>(GV))); |
| 102 | case SectionKind::RODataMergeConst: |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 103 | return (isWeak ? |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 104 | 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? |
Devang Patel | 8a84e44 | 2009-01-05 17:31:22 +0000 | [diff] [blame] | 111 | return NULL; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | const Section* |
| 115 | DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const { |
Dan Gohman | 8f09225 | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 116 | const TargetData *TD = TM.getTargetData(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 117 | Constant *C = cast<GlobalVariable>(GV)->getInitializer(); |
Anton Korobeynikov | 72bb402 | 2009-01-27 22:29:24 +0000 | [diff] [blame] | 118 | const Type *Ty = cast<ArrayType>(C->getType())->getElementType(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 119 | |
Anton Korobeynikov | 72bb402 | 2009-01-27 22:29:24 +0000 | [diff] [blame] | 120 | unsigned Size = TD->getTypePaddedSize(Ty); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 121 | if (Size) { |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 122 | unsigned Align = TD->getPreferredAlignment(GV); |
| 123 | if (Align <= 32) |
| 124 | return getCStringSection_(); |
| 125 | } |
| 126 | |
Anton Korobeynikov | 00181a3 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 127 | return getReadOnlySection(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | const Section* |
| 131 | DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const { |
Anton Korobeynikov | dbab2d2 | 2008-09-24 22:17:27 +0000 | [diff] [blame] | 132 | Constant *C = GV->getInitializer(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 133 | |
Anton Korobeynikov | 84e160e | 2008-08-07 09:51:02 +0000 | [diff] [blame] | 134 | return MergeableConstSection(C->getType()); |
| 135 | } |
| 136 | |
| 137 | inline const Section* |
| 138 | DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const { |
Dan Gohman | 8f09225 | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 139 | const TargetData *TD = TM.getTargetData(); |
Anton Korobeynikov | 84e160e | 2008-08-07 09:51:02 +0000 | [diff] [blame] | 140 | |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 141 | unsigned Size = TD->getTypePaddedSize(Ty); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 142 | if (Size == 4) |
Anton Korobeynikov | 6481873 | 2008-09-24 22:18:54 +0000 | [diff] [blame] | 143 | return FourByteConstantSection; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 144 | else if (Size == 8) |
Anton Korobeynikov | 6481873 | 2008-09-24 22:18:54 +0000 | [diff] [blame] | 145 | return EightByteConstantSection; |
| 146 | else if (Size == 16 && SixteenByteConstantSection) |
| 147 | return SixteenByteConstantSection; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 148 | |
Anton Korobeynikov | 00181a3 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 149 | return getReadOnlySection(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Anton Korobeynikov | 84e160e | 2008-08-07 09:51:02 +0000 | [diff] [blame] | 152 | const Section* |
| 153 | DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const { |
| 154 | const Section* S = MergeableConstSection(Ty); |
| 155 | |
| 156 | // Handle weird special case, when compiling PIC stuff. |
Anton Korobeynikov | 00181a3 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 157 | if (S == getReadOnlySection() && |
Dan Gohman | 8f09225 | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 158 | TM.getRelocationModel() != Reloc::Static) |
Anton Korobeynikov | 84e160e | 2008-08-07 09:51:02 +0000 | [diff] [blame] | 159 | return ConstDataSection; |
| 160 | |
| 161 | return S; |
| 162 | } |
| 163 | |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 164 | std::string |
| 165 | DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV, |
| 166 | SectionKind::Kind kind) const { |
| 167 | assert(0 && "Darwin does not use unique sections"); |
| 168 | return ""; |
| 169 | } |