Daniel Dunbar | ba1da8a | 2009-06-23 23:39:15 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===// |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 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 | #include "llvm/MC/MCContext.h" |
Chris Lattner | 1a5c28f | 2010-03-11 22:56:10 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAsmInfo.h" |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCSectionMachO.h" |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCSectionELF.h" |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCSectionCOFF.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCSymbol.h" |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCLabel.h" |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
| 18 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 21 | typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy; |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 22 | typedef StringMap<const MCSectionELF*> ELFUniqueMapTy; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 23 | typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy; |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 24 | |
| 25 | |
Chris Lattner | c18409a | 2010-03-11 22:53:35 +0000 | [diff] [blame] | 26 | MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) { |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 27 | MachOUniquingMap = 0; |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 28 | ELFUniquingMap = 0; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 29 | COFFUniquingMap = 0; |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 30 | |
| 31 | SecureLogFile = getenv("AS_SECURE_LOG_FILE"); |
| 32 | SecureLog = 0; |
| 33 | SecureLogUsed = false; |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | MCContext::~MCContext() { |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 37 | // NOTE: The symbols are all allocated out of a bump pointer allocator, |
Chris Lattner | c9d3152 | 2009-08-13 00:21:53 +0000 | [diff] [blame] | 38 | // we don't need to free them here. |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 39 | |
| 40 | // If we have the MachO uniquing map, free it. |
| 41 | delete (MachOUniqueMapTy*)MachOUniquingMap; |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 42 | delete (ELFUniqueMapTy*)ELFUniquingMap; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 43 | delete (COFFUniqueMapTy*)COFFUniquingMap; |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 44 | |
| 45 | // If the stream for the .secure_log_unique directive was created free it. |
| 46 | delete (raw_ostream*)SecureLog; |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 49 | //===----------------------------------------------------------------------===// |
| 50 | // Symbol Manipulation |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 53 | MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) { |
Chris Lattner | 00685bb | 2010-03-10 01:29:27 +0000 | [diff] [blame] | 54 | assert(!Name.empty() && "Normal symbols cannot be unnamed!"); |
Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 56 | // Determine whether this is an assembler temporary or normal label. |
| 57 | bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix()); |
| 58 | |
Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 59 | // Do the lookup and get the entire StringMapEntry. We want access to the |
| 60 | // key if we are creating the entry. |
| 61 | StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name); |
| 62 | if (Entry.getValue()) return Entry.getValue(); |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 63 | |
Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 64 | // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer |
| 65 | // to the copy of the string that is embedded in the StringMapEntry. |
| 66 | MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary); |
| 67 | Entry.setValue(Result); |
| 68 | return Result; |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 71 | MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) { |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 72 | SmallString<128> NameSV; |
| 73 | Name.toVector(NameSV); |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 74 | return GetOrCreateSymbol(NameSV.str()); |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Chris Lattner | 1d72a76 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 77 | MCSymbol *MCContext::CreateTempSymbol() { |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 78 | return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) + |
| 79 | "tmp" + Twine(NextUniqueID++)); |
Chris Lattner | 1d72a76 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 82 | unsigned MCContext::NextInstance(int64_t LocalLabelVal) { |
Benjamin Kramer | 47f9a49 | 2010-05-18 12:15:34 +0000 | [diff] [blame] | 83 | MCLabel *&Label = Instances[LocalLabelVal]; |
| 84 | if (!Label) |
| 85 | Label = new (*this) MCLabel(0); |
| 86 | return Label->incInstance(); |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | unsigned MCContext::GetInstance(int64_t LocalLabelVal) { |
Benjamin Kramer | 47f9a49 | 2010-05-18 12:15:34 +0000 | [diff] [blame] | 90 | MCLabel *&Label = Instances[LocalLabelVal]; |
| 91 | if (!Label) |
| 92 | Label = new (*this) MCLabel(0); |
| 93 | return Label->getInstance(); |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) { |
| 97 | return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) + |
| 98 | Twine(LocalLabelVal) + |
| 99 | "\2" + |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame^] | 100 | Twine(NextInstance(LocalLabelVal))); |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 101 | } |
| 102 | MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal, |
| 103 | int bORf) { |
| 104 | return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) + |
| 105 | Twine(LocalLabelVal) + |
| 106 | "\2" + |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame^] | 107 | Twine(GetInstance(LocalLabelVal) + bORf)); |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 110 | MCSymbol *MCContext::LookupSymbol(StringRef Name) const { |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 111 | return Symbols.lookup(Name); |
| 112 | } |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 113 | |
| 114 | //===----------------------------------------------------------------------===// |
| 115 | // Section Management |
| 116 | //===----------------------------------------------------------------------===// |
| 117 | |
| 118 | const MCSectionMachO *MCContext:: |
| 119 | getMachOSection(StringRef Segment, StringRef Section, |
| 120 | unsigned TypeAndAttributes, |
| 121 | unsigned Reserved2, SectionKind Kind) { |
| 122 | |
| 123 | // We unique sections by their segment/section pair. The returned section |
| 124 | // may not have the same flags as the requested section, if so this should be |
| 125 | // diagnosed by the client as an error. |
| 126 | |
| 127 | // Create the map if it doesn't already exist. |
| 128 | if (MachOUniquingMap == 0) |
| 129 | MachOUniquingMap = new MachOUniqueMapTy(); |
| 130 | MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap; |
| 131 | |
| 132 | // Form the name to look up. |
| 133 | SmallString<64> Name; |
| 134 | Name += Segment; |
| 135 | Name.push_back(','); |
| 136 | Name += Section; |
| 137 | |
| 138 | // Do the lookup, if we have a hit, return it. |
| 139 | const MCSectionMachO *&Entry = Map[Name.str()]; |
| 140 | if (Entry) return Entry; |
| 141 | |
| 142 | // Otherwise, return a new section. |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 143 | return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes, |
| 144 | Reserved2, Kind); |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 145 | } |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 146 | |
| 147 | |
| 148 | const MCSection *MCContext:: |
| 149 | getELFSection(StringRef Section, unsigned Type, unsigned Flags, |
| 150 | SectionKind Kind, bool IsExplicit) { |
| 151 | if (ELFUniquingMap == 0) |
| 152 | ELFUniquingMap = new ELFUniqueMapTy(); |
| 153 | ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap; |
| 154 | |
| 155 | // Do the lookup, if we have a hit, return it. |
| 156 | StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section); |
| 157 | if (Entry.getValue()) return Entry.getValue(); |
| 158 | |
| 159 | MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags, |
| 160 | Kind, IsExplicit); |
| 161 | Entry.setValue(Result); |
| 162 | return Result; |
| 163 | } |
| 164 | |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 165 | const MCSection *MCContext::getCOFFSection(StringRef Section, |
| 166 | unsigned Characteristics, |
| 167 | int Selection, |
| 168 | SectionKind Kind) { |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 169 | if (COFFUniquingMap == 0) |
| 170 | COFFUniquingMap = new COFFUniqueMapTy(); |
| 171 | COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap; |
| 172 | |
| 173 | // Do the lookup, if we have a hit, return it. |
| 174 | StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section); |
| 175 | if (Entry.getValue()) return Entry.getValue(); |
| 176 | |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 177 | MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(), |
| 178 | Characteristics, |
| 179 | Selection, Kind); |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 180 | |
| 181 | Entry.setValue(Result); |
| 182 | return Result; |
| 183 | } |