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" |
| 20 | #include "llvm/Target/DarwinTargetAsmInfo.h" |
| 21 | #include "llvm/Target/TargetMachine.h" |
| 22 | #include "llvm/Target/TargetData.h" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | DarwinTargetAsmInfo::DarwinTargetAsmInfo(const TargetMachine &TM) { |
Anton Korobeynikov | 18f6ed9 | 2008-07-19 13:15:21 +0000 | [diff] [blame] | 27 | DTM = &TM; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 28 | |
| 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 Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 35 | |
Anton Korobeynikov | cff2ea0 | 2008-07-19 13:15:46 +0000 | [diff] [blame] | 36 | // Note: 16-byte constant section is subtarget specific and should be provided |
| 37 | // there. |
| 38 | |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 39 | ReadOnlySection_ = getUnnamedSection("\t.const\n", SectionFlags::None); |
| 40 | |
| 41 | // FIXME: These should be named sections, really. |
| 42 | TextCoalSection = |
| 43 | getUnnamedSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions", |
| 44 | SectionFlags::Code); |
| 45 | ConstDataCoalSection = |
| 46 | getUnnamedSection(".section __DATA,__const_coal,coalesced", |
| 47 | SectionFlags::None); |
| 48 | ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None); |
| 49 | DataCoalSection = getUnnamedSection(".section __DATA,__datacoal_nt,coalesced", |
| 50 | SectionFlags::Writeable); |
| 51 | } |
| 52 | |
| 53 | const Section* |
| 54 | DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const { |
| 55 | SectionKind::Kind Kind = SectionKindForGlobal(GV); |
| 56 | bool isWeak = GV->isWeakForLinker(); |
Anton Korobeynikov | 18f6ed9 | 2008-07-19 13:15:21 +0000 | [diff] [blame] | 57 | bool isNonStatic = (DTM->getRelocationModel() != Reloc::Static); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 58 | |
| 59 | switch (Kind) { |
| 60 | case SectionKind::Text: |
| 61 | if (isWeak) |
| 62 | return TextCoalSection; |
| 63 | else |
| 64 | return getTextSection_(); |
| 65 | case SectionKind::Data: |
| 66 | case SectionKind::ThreadData: |
| 67 | case SectionKind::BSS: |
| 68 | case SectionKind::ThreadBSS: |
| 69 | if (cast<GlobalVariable>(GV)->isConstant()) |
| 70 | return (isWeak ? ConstDataCoalSection : ConstDataSection); |
| 71 | else |
| 72 | return (isWeak ? DataCoalSection : getDataSection_()); |
| 73 | case SectionKind::ROData: |
| 74 | return (isWeak ? ConstDataCoalSection : |
| 75 | (isNonStatic ? ConstDataSection : getReadOnlySection_())); |
| 76 | case SectionKind::RODataMergeStr: |
| 77 | return (isWeak ? |
| 78 | ConstDataCoalSection : |
| 79 | MergeableStringSection(cast<GlobalVariable>(GV))); |
| 80 | case SectionKind::RODataMergeConst: |
| 81 | return (isWeak ? |
| 82 | ConstDataCoalSection: |
| 83 | MergeableConstSection(cast<GlobalVariable>(GV))); |
| 84 | default: |
| 85 | assert(0 && "Unsuported section kind for global"); |
| 86 | } |
| 87 | |
| 88 | // FIXME: Do we have any extra special weird cases? |
| 89 | } |
| 90 | |
| 91 | const Section* |
| 92 | DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const { |
Anton Korobeynikov | 18f6ed9 | 2008-07-19 13:15:21 +0000 | [diff] [blame] | 93 | const TargetData *TD = DTM->getTargetData(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 94 | Constant *C = cast<GlobalVariable>(GV)->getInitializer(); |
| 95 | const Type *Type = cast<ConstantArray>(C)->getType()->getElementType(); |
| 96 | |
| 97 | unsigned Size = TD->getABITypeSize(Type); |
| 98 | if (Size) { |
Anton Korobeynikov | 18f6ed9 | 2008-07-19 13:15:21 +0000 | [diff] [blame] | 99 | const TargetData *TD = DTM->getTargetData(); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 100 | unsigned Align = TD->getPreferredAlignment(GV); |
| 101 | if (Align <= 32) |
| 102 | return getCStringSection_(); |
| 103 | } |
| 104 | |
| 105 | return getReadOnlySection_(); |
| 106 | } |
| 107 | |
| 108 | const Section* |
| 109 | DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const { |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 110 | Constant *C = cast<GlobalVariable>(GV)->getInitializer(); |
| 111 | |
Anton Korobeynikov | 84e160e | 2008-08-07 09:51:02 +0000 | [diff] [blame^] | 112 | return MergeableConstSection(C->getType()); |
| 113 | } |
| 114 | |
| 115 | inline const Section* |
| 116 | DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const { |
| 117 | const TargetData *TD = DTM->getTargetData(); |
| 118 | |
| 119 | unsigned Size = TD->getABITypeSize(Ty); |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 120 | if (Size == 4) |
| 121 | return FourByteConstantSection_; |
| 122 | else if (Size == 8) |
| 123 | return EightByteConstantSection_; |
Anton Korobeynikov | cff2ea0 | 2008-07-19 13:15:46 +0000 | [diff] [blame] | 124 | else if (Size == 16 && SixteenByteConstantSection_) |
| 125 | return SixteenByteConstantSection_; |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 126 | |
| 127 | return getReadOnlySection_(); |
| 128 | } |
| 129 | |
Anton Korobeynikov | 84e160e | 2008-08-07 09:51:02 +0000 | [diff] [blame^] | 130 | const Section* |
| 131 | DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const { |
| 132 | const Section* S = MergeableConstSection(Ty); |
| 133 | |
| 134 | // Handle weird special case, when compiling PIC stuff. |
| 135 | if (S == getReadOnlySection_() && |
| 136 | DTM->getRelocationModel() != Reloc::Static) |
| 137 | return ConstDataSection; |
| 138 | |
| 139 | return S; |
| 140 | } |
| 141 | |
Anton Korobeynikov | 745e864 | 2008-07-19 13:14:46 +0000 | [diff] [blame] | 142 | std::string |
| 143 | DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV, |
| 144 | SectionKind::Kind kind) const { |
| 145 | assert(0 && "Darwin does not use unique sections"); |
| 146 | return ""; |
| 147 | } |