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" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame^] | 20 | #include "llvm/Support/ErrorHandling.h" |
Dale Johannesen | d2e51af | 2008-09-09 22:29:13 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Mangler.h" |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 22 | #include "llvm/Target/DarwinTargetAsmInfo.h" |
| 23 | #include "llvm/Target/TargetMachine.h" |
| 24 | #include "llvm/Target/TargetData.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
Dan Gohman | 8f09225 | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 28 | DarwinTargetAsmInfo::DarwinTargetAsmInfo(const TargetMachine &TM) |
| 29 | : TargetAsmInfo(TM) { |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 30 | |
| 31 | CStringSection_ = getUnnamedSection("\t.cstring", |
| 32 | SectionFlags::Mergeable | SectionFlags::Strings); |
Anton Korobeynikov | 6481873 | 2008-09-24 22:18:54 +0000 | [diff] [blame] | 33 | FourByteConstantSection = getUnnamedSection("\t.literal4\n", |
| 34 | SectionFlags::Mergeable); |
| 35 | EightByteConstantSection = getUnnamedSection("\t.literal8\n", |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 36 | SectionFlags::Mergeable); |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 37 | |
Anton Korobeynikov | cff2ea0 | 2008-07-19 13:15:46 +0000 | [diff] [blame] | 38 | // Note: 16-byte constant section is subtarget specific and should be provided |
Anton Korobeynikov | 6481873 | 2008-09-24 22:18:54 +0000 | [diff] [blame] | 39 | // there, if needed. |
| 40 | SixteenByteConstantSection = 0; |
Anton Korobeynikov | cff2ea0 | 2008-07-19 13:15:46 +0000 | [diff] [blame] | 41 | |
Anton Korobeynikov | 00181a3 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 42 | ReadOnlySection = getUnnamedSection("\t.const\n", SectionFlags::None); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 43 | |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 44 | TextCoalSection = |
Anton Korobeynikov | d79cda9 | 2008-09-24 22:19:13 +0000 | [diff] [blame] | 45 | getNamedSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions", |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 46 | SectionFlags::Code); |
Dale Johannesen | 585457e | 2008-10-08 21:49:47 +0000 | [diff] [blame] | 47 | ConstTextCoalSection = getNamedSection("\t__TEXT,__const_coal,coalesced", |
| 48 | SectionFlags::None); |
Anton Korobeynikov | 00181a3 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 49 | ConstDataCoalSection = getNamedSection("\t__DATA,__const_coal,coalesced", |
Anton Korobeynikov | d79cda9 | 2008-09-24 22:19:13 +0000 | [diff] [blame] | 50 | SectionFlags::None); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 51 | ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None); |
Anton Korobeynikov | d79cda9 | 2008-09-24 22:19:13 +0000 | [diff] [blame] | 52 | DataCoalSection = getNamedSection("\t__DATA,__datacoal_nt,coalesced", |
| 53 | SectionFlags::Writeable); |
Chris Lattner | 4e0f25b | 2009-06-19 00:08:39 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | // Common settings for all Darwin targets. |
| 57 | // Syntax: |
| 58 | GlobalPrefix = "_"; |
| 59 | PrivateGlobalPrefix = "L"; |
| 60 | LessPrivateGlobalPrefix = "l"; // Marker for some ObjC metadata |
| 61 | StringConstantPrefix = "\1LC"; |
| 62 | NeedsSet = true; |
| 63 | NeedsIndirectEncoding = true; |
| 64 | AllowQuotesInName = true; |
| 65 | HasSingleParameterDotFile = false; |
| 66 | |
| 67 | // In non-PIC modes, emit a special label before jump tables so that the |
| 68 | // linker can perform more accurate dead code stripping. We do not check the |
| 69 | // relocation model here since it can be overridden later. |
| 70 | JumpTableSpecialLabelPrefix = "l"; |
| 71 | |
| 72 | // Directives: |
| 73 | WeakDefDirective = "\t.weak_definition "; |
| 74 | WeakRefDirective = "\t.weak_reference "; |
| 75 | HiddenDirective = "\t.private_extern "; |
| 76 | |
| 77 | // Sections: |
| 78 | CStringSection = "\t.cstring"; |
| 79 | JumpTableDataSection = "\t.const\n"; |
| 80 | BSSSection = 0; |
| 81 | |
| 82 | if (TM.getRelocationModel() == Reloc::Static) { |
| 83 | StaticCtorsSection = ".constructor"; |
| 84 | StaticDtorsSection = ".destructor"; |
| 85 | } else { |
| 86 | StaticCtorsSection = ".mod_init_func"; |
| 87 | StaticDtorsSection = ".mod_term_func"; |
| 88 | } |
| 89 | |
| 90 | DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug"; |
| 91 | DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug"; |
| 92 | DwarfLineSection = ".section __DWARF,__debug_line,regular,debug"; |
| 93 | DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug"; |
| 94 | DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug"; |
| 95 | DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug"; |
| 96 | DwarfStrSection = ".section __DWARF,__debug_str,regular,debug"; |
| 97 | DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug"; |
| 98 | DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug"; |
| 99 | DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug"; |
| 100 | DwarfMacroInfoSection = ".section __DWARF,__debug_macinfo,regular,debug"; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Dale Johannesen | d2e51af | 2008-09-09 22:29:13 +0000 | [diff] [blame] | 103 | /// emitUsedDirectiveFor - On Darwin, internally linked data beginning with |
| 104 | /// the PrivateGlobalPrefix or the LessPrivateGlobalPrefix does not have the |
| 105 | /// directive emitted (this occurs in ObjC metadata). |
| 106 | |
| 107 | bool |
| 108 | DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV, |
| 109 | Mangler *Mang) const { |
| 110 | if (GV==0) |
| 111 | return false; |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 112 | if (GV->hasLocalLinkage() && !isa<Function>(GV) && |
Dale Johannesen | d2e51af | 2008-09-09 22:29:13 +0000 | [diff] [blame] | 113 | ((strlen(getPrivateGlobalPrefix()) != 0 && |
| 114 | Mang->getValueName(GV).substr(0,strlen(getPrivateGlobalPrefix())) == |
| 115 | getPrivateGlobalPrefix()) || |
| 116 | (strlen(getLessPrivateGlobalPrefix()) != 0 && |
| 117 | Mang->getValueName(GV).substr(0,strlen(getLessPrivateGlobalPrefix())) == |
| 118 | getLessPrivateGlobalPrefix()))) |
| 119 | return false; |
| 120 | return true; |
| 121 | } |
| 122 | |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 123 | const Section* |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 124 | DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const { |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 125 | SectionKind::Kind Kind = SectionKindForGlobal(GV); |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 126 | bool isWeak = GV->isWeakForLinker(); |
Dan Gohman | 8f09225 | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 127 | bool isNonStatic = TM.getRelocationModel() != Reloc::Static; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 128 | |
| 129 | switch (Kind) { |
| 130 | case SectionKind::Text: |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 131 | if (isWeak) |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 132 | return TextCoalSection; |
| 133 | else |
Anton Korobeynikov | 0b501d1 | 2008-09-24 22:16:33 +0000 | [diff] [blame] | 134 | return TextSection; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 135 | case SectionKind::Data: |
| 136 | case SectionKind::ThreadData: |
| 137 | case SectionKind::BSS: |
| 138 | case SectionKind::ThreadBSS: |
| 139 | if (cast<GlobalVariable>(GV)->isConstant()) |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 140 | return (isWeak ? ConstDataCoalSection : ConstDataSection); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 141 | else |
Anton Korobeynikov | 0b501d1 | 2008-09-24 22:16:33 +0000 | [diff] [blame] | 142 | return (isWeak ? DataCoalSection : DataSection); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 143 | case SectionKind::ROData: |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 144 | return (isWeak ? ConstDataCoalSection : |
Anton Korobeynikov | 00181a3 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 145 | (isNonStatic ? ConstDataSection : getReadOnlySection())); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 146 | case SectionKind::RODataMergeStr: |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 147 | return (isWeak ? |
Dale Johannesen | 585457e | 2008-10-08 21:49:47 +0000 | [diff] [blame] | 148 | ConstTextCoalSection : |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 149 | MergeableStringSection(cast<GlobalVariable>(GV))); |
| 150 | case SectionKind::RODataMergeConst: |
Evan Cheng | 42ccc21 | 2008-08-08 17:56:50 +0000 | [diff] [blame] | 151 | return (isWeak ? |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 152 | ConstDataCoalSection: |
| 153 | MergeableConstSection(cast<GlobalVariable>(GV))); |
| 154 | default: |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame^] | 155 | LLVM_UNREACHABLE("Unsuported section kind for global"); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | // FIXME: Do we have any extra special weird cases? |
Devang Patel | 8a84e44 | 2009-01-05 17:31:22 +0000 | [diff] [blame] | 159 | return NULL; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | const Section* |
| 163 | DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const { |
Dan Gohman | 8f09225 | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 164 | const TargetData *TD = TM.getTargetData(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 165 | Constant *C = cast<GlobalVariable>(GV)->getInitializer(); |
Anton Korobeynikov | 72bb402 | 2009-01-27 22:29:24 +0000 | [diff] [blame] | 166 | const Type *Ty = cast<ArrayType>(C->getType())->getElementType(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 167 | |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 168 | unsigned Size = TD->getTypeAllocSize(Ty); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 169 | if (Size) { |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 170 | unsigned Align = TD->getPreferredAlignment(GV); |
| 171 | if (Align <= 32) |
| 172 | return getCStringSection_(); |
| 173 | } |
| 174 | |
Anton Korobeynikov | 00181a3 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 175 | return getReadOnlySection(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | const Section* |
| 179 | DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const { |
Anton Korobeynikov | dbab2d2 | 2008-09-24 22:17:27 +0000 | [diff] [blame] | 180 | Constant *C = GV->getInitializer(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 181 | |
Anton Korobeynikov | 84e160e | 2008-08-07 09:51:02 +0000 | [diff] [blame] | 182 | return MergeableConstSection(C->getType()); |
| 183 | } |
| 184 | |
| 185 | inline const Section* |
| 186 | DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const { |
Dan Gohman | 8f09225 | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 187 | const TargetData *TD = TM.getTargetData(); |
Anton Korobeynikov | 84e160e | 2008-08-07 09:51:02 +0000 | [diff] [blame] | 188 | |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 189 | unsigned Size = TD->getTypeAllocSize(Ty); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 190 | if (Size == 4) |
Anton Korobeynikov | 6481873 | 2008-09-24 22:18:54 +0000 | [diff] [blame] | 191 | return FourByteConstantSection; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 192 | else if (Size == 8) |
Anton Korobeynikov | 6481873 | 2008-09-24 22:18:54 +0000 | [diff] [blame] | 193 | return EightByteConstantSection; |
| 194 | else if (Size == 16 && SixteenByteConstantSection) |
| 195 | return SixteenByteConstantSection; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 196 | |
Anton Korobeynikov | 00181a3 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 197 | return getReadOnlySection(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Anton Korobeynikov | 84e160e | 2008-08-07 09:51:02 +0000 | [diff] [blame] | 200 | const Section* |
| 201 | DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const { |
| 202 | const Section* S = MergeableConstSection(Ty); |
| 203 | |
| 204 | // Handle weird special case, when compiling PIC stuff. |
Anton Korobeynikov | 00181a3 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 205 | if (S == getReadOnlySection() && |
Dan Gohman | 8f09225 | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 206 | TM.getRelocationModel() != Reloc::Static) |
Anton Korobeynikov | 84e160e | 2008-08-07 09:51:02 +0000 | [diff] [blame] | 207 | return ConstDataSection; |
| 208 | |
| 209 | return S; |
| 210 | } |
| 211 | |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 212 | std::string |
| 213 | DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV, |
| 214 | SectionKind::Kind kind) const { |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame^] | 215 | LLVM_UNREACHABLE("Darwin does not use unique sections"); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 216 | return ""; |
| 217 | } |