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) { |
Chris Lattner | 92ef3f6 | 2009-07-27 16:20:58 +0000 | [diff] [blame] | 30 | TextSection = getOrCreateSection("\t.text", true, SectionKind::Text); |
| 31 | DataSection = getOrCreateSection("\t.data", true, SectionKind::DataRel); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 32 | |
Chris Lattner | e346694 | 2009-07-27 06:17:14 +0000 | [diff] [blame] | 33 | CStringSection_ = getOrCreateSection("\t.cstring", true, |
| 34 | SectionKind::MergeableCString); |
| 35 | FourByteConstantSection = getOrCreateSection("\t.literal4\n", true, |
| 36 | SectionKind::MergeableConst4); |
| 37 | EightByteConstantSection = getOrCreateSection("\t.literal8\n", true, |
| 38 | SectionKind::MergeableConst8); |
Chris Lattner | de3a107 | 2009-07-27 15:44:04 +0000 | [diff] [blame] | 39 | SixteenByteConstantSection = |
| 40 | getOrCreateSection("\t.literal16\n", true, SectionKind::MergeableConst16); |
Anton Korobeynikov | cff2ea0 | 2008-07-19 13:15:46 +0000 | [diff] [blame] | 41 | |
Chris Lattner | e346694 | 2009-07-27 06:17:14 +0000 | [diff] [blame] | 42 | ReadOnlySection = getOrCreateSection("\t.const", true, SectionKind::ReadOnly); |
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 = |
Chris Lattner | e346694 | 2009-07-27 06:17:14 +0000 | [diff] [blame] | 45 | getOrCreateSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions", |
| 46 | false, SectionKind::Text); |
| 47 | ConstTextCoalSection = getOrCreateSection("\t__TEXT,__const_coal,coalesced", |
| 48 | false, SectionKind::Text); |
| 49 | ConstDataCoalSection = getOrCreateSection("\t__DATA,__const_coal,coalesced", |
| 50 | false, SectionKind::Text); |
| 51 | ConstDataSection = getOrCreateSection("\t.const_data", true, |
| 52 | SectionKind::ReadOnlyWithRel); |
| 53 | DataCoalSection = getOrCreateSection("\t__DATA,__datacoal_nt,coalesced", |
| 54 | false, SectionKind::DataRel); |
Chris Lattner | 4e0f25b | 2009-06-19 00:08:39 +0000 | [diff] [blame] | 55 | |
| 56 | // Common settings for all Darwin targets. |
| 57 | // Syntax: |
| 58 | GlobalPrefix = "_"; |
| 59 | PrivateGlobalPrefix = "L"; |
Chris Lattner | 90f8b70 | 2009-07-21 17:30:51 +0000 | [diff] [blame] | 60 | LinkerPrivateGlobalPrefix = "l"; // Marker for some ObjC metadata |
Chris Lattner | 4e0f25b | 2009-06-19 00:08:39 +0000 | [diff] [blame] | 61 | NeedsSet = true; |
| 62 | NeedsIndirectEncoding = true; |
| 63 | AllowQuotesInName = true; |
| 64 | HasSingleParameterDotFile = false; |
| 65 | |
| 66 | // In non-PIC modes, emit a special label before jump tables so that the |
| 67 | // linker can perform more accurate dead code stripping. We do not check the |
| 68 | // relocation model here since it can be overridden later. |
| 69 | JumpTableSpecialLabelPrefix = "l"; |
| 70 | |
| 71 | // Directives: |
| 72 | WeakDefDirective = "\t.weak_definition "; |
| 73 | WeakRefDirective = "\t.weak_reference "; |
| 74 | HiddenDirective = "\t.private_extern "; |
| 75 | |
| 76 | // Sections: |
| 77 | CStringSection = "\t.cstring"; |
Chris Lattner | 30c4a3b | 2009-07-26 01:24:18 +0000 | [diff] [blame] | 78 | JumpTableDataSection = "\t.const"; |
Chris Lattner | 4e0f25b | 2009-06-19 00:08:39 +0000 | [diff] [blame] | 79 | BSSSection = 0; |
| 80 | |
| 81 | if (TM.getRelocationModel() == Reloc::Static) { |
| 82 | StaticCtorsSection = ".constructor"; |
| 83 | StaticDtorsSection = ".destructor"; |
| 84 | } else { |
| 85 | StaticCtorsSection = ".mod_init_func"; |
| 86 | StaticDtorsSection = ".mod_term_func"; |
| 87 | } |
| 88 | |
Chris Lattner | e2cf37b | 2009-07-17 20:46:40 +0000 | [diff] [blame] | 89 | // _foo.eh symbols are currently always exported so that the linker knows |
| 90 | // about them. This may not strictly be necessary on 10.6 and later, but it |
| 91 | // doesn't hurt anything. |
| 92 | Is_EHSymbolPrivate = false; |
| 93 | |
Chris Lattner | 4e0f25b | 2009-06-19 00:08:39 +0000 | [diff] [blame] | 94 | DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug"; |
| 95 | DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug"; |
| 96 | DwarfLineSection = ".section __DWARF,__debug_line,regular,debug"; |
| 97 | DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug"; |
| 98 | DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug"; |
| 99 | DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug"; |
| 100 | DwarfStrSection = ".section __DWARF,__debug_str,regular,debug"; |
| 101 | DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug"; |
| 102 | DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug"; |
| 103 | DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug"; |
| 104 | DwarfMacroInfoSection = ".section __DWARF,__debug_macinfo,regular,debug"; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Dale Johannesen | d2e51af | 2008-09-09 22:29:13 +0000 | [diff] [blame] | 107 | /// emitUsedDirectiveFor - On Darwin, internally linked data beginning with |
Chris Lattner | 90f8b70 | 2009-07-21 17:30:51 +0000 | [diff] [blame] | 108 | /// the PrivateGlobalPrefix or the LinkerPrivateGlobalPrefix does not have the |
Dale Johannesen | d2e51af | 2008-09-09 22:29:13 +0000 | [diff] [blame] | 109 | /// directive emitted (this occurs in ObjC metadata). |
Bill Wendling | ecc2da8 | 2009-07-20 21:38:26 +0000 | [diff] [blame] | 110 | bool DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV, |
| 111 | Mangler *Mang) const { |
| 112 | if (!GV) return false; |
Chris Lattner | b8158ac | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 90f8b70 | 2009-07-21 17:30:51 +0000 | [diff] [blame] | 114 | // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix. |
Bill Wendling | ecc2da8 | 2009-07-20 21:38:26 +0000 | [diff] [blame] | 115 | if (GV->hasLocalLinkage() && !isa<Function>(GV)) { |
Chris Lattner | 449e379 | 2009-07-21 22:32:55 +0000 | [diff] [blame] | 116 | // FIXME: ObjC metadata is currently emitted as internal symbols that have |
| 117 | // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and |
| 118 | // this horrible hack can go away. |
Bill Wendling | ecc2da8 | 2009-07-20 21:38:26 +0000 | [diff] [blame] | 119 | const std::string &Name = Mang->getMangledName(GV); |
Chris Lattner | 449e379 | 2009-07-21 22:32:55 +0000 | [diff] [blame] | 120 | if (Name[0] == 'L' || Name[0] == 'l') |
Bill Wendling | ecc2da8 | 2009-07-20 21:38:26 +0000 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | |
Dale Johannesen | d2e51af | 2008-09-09 22:29:13 +0000 | [diff] [blame] | 124 | return true; |
| 125 | } |
| 126 | |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 127 | const Section* |
Chris Lattner | f20f250 | 2009-07-24 18:42:53 +0000 | [diff] [blame] | 128 | DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV, |
Chris Lattner | 460d51e | 2009-07-25 23:21:55 +0000 | [diff] [blame] | 129 | SectionKind Kind) const { |
Chris Lattner | e346e18 | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 130 | assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS"); |
Chris Lattner | e3577da | 2009-07-26 00:51:36 +0000 | [diff] [blame] | 131 | |
Chris Lattner | e346e18 | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 132 | if (Kind.isText()) |
Chris Lattner | 2b421da | 2009-07-26 07:07:01 +0000 | [diff] [blame] | 133 | return Kind.isWeak() ? TextCoalSection : TextSection; |
Chris Lattner | e3577da | 2009-07-26 00:51:36 +0000 | [diff] [blame] | 134 | |
| 135 | // If this is weak/linkonce, put this in a coalescable section, either in text |
| 136 | // or data depending on if it is writable. |
Chris Lattner | 2b421da | 2009-07-26 07:07:01 +0000 | [diff] [blame] | 137 | if (Kind.isWeak()) { |
Chris Lattner | e3577da | 2009-07-26 00:51:36 +0000 | [diff] [blame] | 138 | if (Kind.isReadOnly()) |
| 139 | return ConstTextCoalSection; |
| 140 | return DataCoalSection; |
| 141 | } |
| 142 | |
| 143 | // FIXME: Alignment check should be handled by section classifier. |
Chris Lattner | 2ceb60a | 2009-07-26 06:48:26 +0000 | [diff] [blame] | 144 | if (Kind.isMergeableCString()) |
Chris Lattner | e3577da | 2009-07-26 00:51:36 +0000 | [diff] [blame] | 145 | return MergeableStringSection(cast<GlobalVariable>(GV)); |
| 146 | |
Chris Lattner | 2ceb60a | 2009-07-26 06:48:26 +0000 | [diff] [blame] | 147 | if (Kind.isMergeableConst()) { |
| 148 | if (Kind.isMergeableConst4()) |
Chris Lattner | 8ca5209 | 2009-07-26 06:11:33 +0000 | [diff] [blame] | 149 | return FourByteConstantSection; |
Chris Lattner | 2ceb60a | 2009-07-26 06:48:26 +0000 | [diff] [blame] | 150 | if (Kind.isMergeableConst8()) |
Chris Lattner | 8ca5209 | 2009-07-26 06:11:33 +0000 | [diff] [blame] | 151 | return EightByteConstantSection; |
Chris Lattner | de3a107 | 2009-07-27 15:44:04 +0000 | [diff] [blame] | 152 | if (Kind.isMergeableConst16()) |
Chris Lattner | 8ca5209 | 2009-07-26 06:11:33 +0000 | [diff] [blame] | 153 | return SixteenByteConstantSection; |
| 154 | return ReadOnlySection; // .const |
Chris Lattner | f158156 | 2009-07-22 00:47:11 +0000 | [diff] [blame] | 155 | } |
Chris Lattner | 30c4a3b | 2009-07-26 01:24:18 +0000 | [diff] [blame] | 156 | |
| 157 | // FIXME: ROData -> const in -static mode that is relocatable but they happen |
Chris Lattner | 2ceb60a | 2009-07-26 06:48:26 +0000 | [diff] [blame] | 158 | // by the static linker. Why not mergeable? |
Chris Lattner | 30c4a3b | 2009-07-26 01:24:18 +0000 | [diff] [blame] | 159 | if (Kind.isReadOnly()) |
| 160 | return getReadOnlySection(); |
Chris Lattner | e346e18 | 2009-07-26 05:44:20 +0000 | [diff] [blame] | 161 | |
| 162 | // If this is marked const, put it into a const section. But if the dynamic |
| 163 | // linker needs to write to it, put it in the data segment. |
| 164 | if (Kind.isReadOnlyWithRel()) |
| 165 | return ConstDataSection; |
Chris Lattner | 30c4a3b | 2009-07-26 01:24:18 +0000 | [diff] [blame] | 166 | |
| 167 | // Otherwise, just drop the variable in the normal data section. |
| 168 | return DataSection; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | const Section* |
| 172 | DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const { |
Dan Gohman | 8f09225 | 2008-11-03 18:22:42 +0000 | [diff] [blame] | 173 | const TargetData *TD = TM.getTargetData(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 174 | Constant *C = cast<GlobalVariable>(GV)->getInitializer(); |
Anton Korobeynikov | 72bb402 | 2009-01-27 22:29:24 +0000 | [diff] [blame] | 175 | const Type *Ty = cast<ArrayType>(C->getType())->getElementType(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 176 | |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 177 | unsigned Size = TD->getTypeAllocSize(Ty); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 178 | if (Size) { |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 179 | unsigned Align = TD->getPreferredAlignment(GV); |
| 180 | if (Align <= 32) |
| 181 | return getCStringSection_(); |
| 182 | } |
| 183 | |
Anton Korobeynikov | 00181a3 | 2008-09-24 22:20:27 +0000 | [diff] [blame] | 184 | return getReadOnlySection(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Chris Lattner | 298414e | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 187 | const Section * |
Chris Lattner | 2ceb60a | 2009-07-26 06:48:26 +0000 | [diff] [blame] | 188 | DarwinTargetAsmInfo::getSectionForMergeableConstant(SectionKind Kind) const { |
Chris Lattner | 298414e | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 189 | // If this constant requires a relocation, we have to put it in the data |
| 190 | // segment, not in the text segment. |
Chris Lattner | 5c2f789 | 2009-07-26 06:26:55 +0000 | [diff] [blame] | 191 | if (Kind.isDataRel()) |
Anton Korobeynikov | 84e160e | 2008-08-07 09:51:02 +0000 | [diff] [blame] | 192 | return ConstDataSection; |
Chris Lattner | 298414e | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 2ceb60a | 2009-07-26 06:48:26 +0000 | [diff] [blame] | 194 | if (Kind.isMergeableConst4()) |
Chris Lattner | 298414e | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 195 | return FourByteConstantSection; |
Chris Lattner | 2ceb60a | 2009-07-26 06:48:26 +0000 | [diff] [blame] | 196 | if (Kind.isMergeableConst8()) |
Chris Lattner | 298414e | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 197 | return EightByteConstantSection; |
Chris Lattner | de3a107 | 2009-07-27 15:44:04 +0000 | [diff] [blame] | 198 | if (Kind.isMergeableConst16()) |
Chris Lattner | 5c2f789 | 2009-07-26 06:26:55 +0000 | [diff] [blame] | 199 | return SixteenByteConstantSection; |
Chris Lattner | 298414e | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 200 | return ReadOnlySection; // .const |
Anton Korobeynikov | 84e160e | 2008-08-07 09:51:02 +0000 | [diff] [blame] | 201 | } |
| 202 | |