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" |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCDwarf.h" |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
| 19 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 22 | typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy; |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 23 | typedef StringMap<const MCSectionELF*> ELFUniqueMapTy; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 24 | typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy; |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 25 | |
| 26 | |
Chris Lattner | c18409a | 2010-03-11 22:53:35 +0000 | [diff] [blame] | 27 | MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) { |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 28 | MachOUniquingMap = 0; |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 29 | ELFUniquingMap = 0; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 30 | COFFUniquingMap = 0; |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 31 | |
| 32 | SecureLogFile = getenv("AS_SECURE_LOG_FILE"); |
| 33 | SecureLog = 0; |
| 34 | SecureLogUsed = false; |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | MCContext::~MCContext() { |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 38 | // NOTE: The symbols are all allocated out of a bump pointer allocator, |
Chris Lattner | c9d3152 | 2009-08-13 00:21:53 +0000 | [diff] [blame] | 39 | // we don't need to free them here. |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 40 | |
| 41 | // If we have the MachO uniquing map, free it. |
| 42 | delete (MachOUniqueMapTy*)MachOUniquingMap; |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 43 | delete (ELFUniqueMapTy*)ELFUniquingMap; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 44 | delete (COFFUniqueMapTy*)COFFUniquingMap; |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 45 | |
| 46 | // If the stream for the .secure_log_unique directive was created free it. |
| 47 | delete (raw_ostream*)SecureLog; |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 50 | //===----------------------------------------------------------------------===// |
| 51 | // Symbol Manipulation |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 54 | MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) { |
Chris Lattner | 00685bb | 2010-03-10 01:29:27 +0000 | [diff] [blame] | 55 | assert(!Name.empty() && "Normal symbols cannot be unnamed!"); |
Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 57 | // Determine whether this is an assembler temporary or normal label. |
| 58 | bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix()); |
| 59 | |
Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 60 | // Do the lookup and get the entire StringMapEntry. We want access to the |
| 61 | // key if we are creating the entry. |
| 62 | StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name); |
| 63 | if (Entry.getValue()) return Entry.getValue(); |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 64 | |
Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 65 | // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer |
| 66 | // to the copy of the string that is embedded in the StringMapEntry. |
| 67 | MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary); |
| 68 | Entry.setValue(Result); |
| 69 | return Result; |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 72 | MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) { |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 73 | SmallString<128> NameSV; |
| 74 | Name.toVector(NameSV); |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 75 | return GetOrCreateSymbol(NameSV.str()); |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Chris Lattner | 1d72a76 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 78 | MCSymbol *MCContext::CreateTempSymbol() { |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 79 | return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) + |
| 80 | "tmp" + Twine(NextUniqueID++)); |
Chris Lattner | 1d72a76 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 83 | unsigned MCContext::NextInstance(int64_t LocalLabelVal) { |
Benjamin Kramer | 47f9a49 | 2010-05-18 12:15:34 +0000 | [diff] [blame] | 84 | MCLabel *&Label = Instances[LocalLabelVal]; |
| 85 | if (!Label) |
| 86 | Label = new (*this) MCLabel(0); |
| 87 | return Label->incInstance(); |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | unsigned MCContext::GetInstance(int64_t LocalLabelVal) { |
Benjamin Kramer | 47f9a49 | 2010-05-18 12:15:34 +0000 | [diff] [blame] | 91 | MCLabel *&Label = Instances[LocalLabelVal]; |
| 92 | if (!Label) |
| 93 | Label = new (*this) MCLabel(0); |
| 94 | return Label->getInstance(); |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) { |
| 98 | return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) + |
| 99 | Twine(LocalLabelVal) + |
| 100 | "\2" + |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 101 | Twine(NextInstance(LocalLabelVal))); |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 102 | } |
| 103 | MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal, |
| 104 | int bORf) { |
| 105 | return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) + |
| 106 | Twine(LocalLabelVal) + |
| 107 | "\2" + |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 108 | Twine(GetInstance(LocalLabelVal) + bORf)); |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 111 | MCSymbol *MCContext::LookupSymbol(StringRef Name) const { |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 112 | return Symbols.lookup(Name); |
| 113 | } |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 114 | |
| 115 | //===----------------------------------------------------------------------===// |
| 116 | // Section Management |
| 117 | //===----------------------------------------------------------------------===// |
| 118 | |
| 119 | const MCSectionMachO *MCContext:: |
| 120 | getMachOSection(StringRef Segment, StringRef Section, |
| 121 | unsigned TypeAndAttributes, |
| 122 | unsigned Reserved2, SectionKind Kind) { |
| 123 | |
| 124 | // We unique sections by their segment/section pair. The returned section |
| 125 | // may not have the same flags as the requested section, if so this should be |
| 126 | // diagnosed by the client as an error. |
| 127 | |
| 128 | // Create the map if it doesn't already exist. |
| 129 | if (MachOUniquingMap == 0) |
| 130 | MachOUniquingMap = new MachOUniqueMapTy(); |
| 131 | MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap; |
| 132 | |
| 133 | // Form the name to look up. |
| 134 | SmallString<64> Name; |
| 135 | Name += Segment; |
| 136 | Name.push_back(','); |
| 137 | Name += Section; |
| 138 | |
| 139 | // Do the lookup, if we have a hit, return it. |
| 140 | const MCSectionMachO *&Entry = Map[Name.str()]; |
| 141 | if (Entry) return Entry; |
| 142 | |
| 143 | // Otherwise, return a new section. |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 144 | return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes, |
| 145 | Reserved2, Kind); |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 146 | } |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 147 | |
| 148 | |
| 149 | const MCSection *MCContext:: |
| 150 | getELFSection(StringRef Section, unsigned Type, unsigned Flags, |
| 151 | SectionKind Kind, bool IsExplicit) { |
| 152 | if (ELFUniquingMap == 0) |
| 153 | ELFUniquingMap = new ELFUniqueMapTy(); |
| 154 | ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap; |
| 155 | |
| 156 | // Do the lookup, if we have a hit, return it. |
| 157 | StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section); |
| 158 | if (Entry.getValue()) return Entry.getValue(); |
| 159 | |
| 160 | MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags, |
| 161 | Kind, IsExplicit); |
| 162 | Entry.setValue(Result); |
| 163 | return Result; |
| 164 | } |
| 165 | |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 166 | const MCSection *MCContext::getCOFFSection(StringRef Section, |
| 167 | unsigned Characteristics, |
| 168 | int Selection, |
| 169 | SectionKind Kind) { |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 170 | if (COFFUniquingMap == 0) |
| 171 | COFFUniquingMap = new COFFUniqueMapTy(); |
| 172 | COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap; |
| 173 | |
| 174 | // Do the lookup, if we have a hit, return it. |
| 175 | StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section); |
| 176 | if (Entry.getValue()) return Entry.getValue(); |
| 177 | |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 178 | MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(), |
| 179 | Characteristics, |
| 180 | Selection, Kind); |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 181 | |
| 182 | Entry.setValue(Result); |
| 183 | return Result; |
| 184 | } |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 185 | |
| 186 | //===----------------------------------------------------------------------===// |
| 187 | // Dwarf Management |
| 188 | //===----------------------------------------------------------------------===// |
| 189 | |
| 190 | /// GetDwarfFile - takes a file name an number to place in the dwarf file and |
| 191 | /// directory tables. If the file number has already been allocated it is an |
| 192 | /// error and zero is returned and the client reports the error, else the |
| 193 | /// allocated file number is returned. The file numbers may be in any order. |
| 194 | unsigned MCContext::GetDwarfFile(StringRef FileName, unsigned FileNumber) { |
| 195 | // TODO: a FileNumber of zero says to use the next available file number. |
| 196 | // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked |
| 197 | // to not be less than one. This needs to be change to be not less than zero. |
| 198 | |
| 199 | // Make space for this FileNumber in the MCDwarfFiles vector if needed. |
| 200 | if (FileNumber >= MCDwarfFiles.size()) { |
| 201 | MCDwarfFiles.resize(FileNumber + 1); |
| 202 | } else { |
| 203 | MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber]; |
| 204 | if (ExistingFile) |
| 205 | // It is an error to use see the same number more than once. |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | // Get the new MCDwarfFile slot for this FileNumber. |
| 210 | MCDwarfFile *&File = MCDwarfFiles[FileNumber]; |
| 211 | |
| 212 | // Separate the directory part from the basename of the FileName. |
| 213 | std::pair<StringRef, StringRef> Slash = FileName.rsplit('/'); |
| 214 | |
| 215 | // Find or make a entry in the MCDwarfDirs vector for this Directory. |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 216 | StringRef Name; |
| 217 | unsigned DirIndex; |
| 218 | // Capture directory name. |
| 219 | if (Slash.second.empty()) { |
| 220 | Name = Slash.first; |
| 221 | DirIndex = 0; // For FileNames with no directories a DirIndex of 0 is used. |
| 222 | } else { |
Benjamin Kramer | 3bce5ad | 2010-07-29 13:53:19 +0000 | [diff] [blame] | 223 | StringRef Directory = Slash.first; |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 224 | Name = Slash.second; |
Kevin Enderby | b07ce60 | 2010-08-09 22:52:14 +0000 | [diff] [blame^] | 225 | for (DirIndex = 0; DirIndex < MCDwarfDirs.size(); DirIndex++) { |
Benjamin Kramer | 3bce5ad | 2010-07-29 13:53:19 +0000 | [diff] [blame] | 226 | if (Directory == MCDwarfDirs[DirIndex]) |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 227 | break; |
| 228 | } |
| 229 | if (DirIndex >= MCDwarfDirs.size()) { |
Benjamin Kramer | 3bce5ad | 2010-07-29 13:53:19 +0000 | [diff] [blame] | 230 | char *Buf = static_cast<char *>(Allocate(Directory.size())); |
| 231 | memcpy(Buf, Directory.data(), Directory.size()); |
| 232 | MCDwarfDirs.push_back(StringRef(Buf, Directory.size())); |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 233 | } |
Kevin Enderby | b07ce60 | 2010-08-09 22:52:14 +0000 | [diff] [blame^] | 234 | // The DirIndex is one based, as DirIndex of 0 is used for FileNames with |
| 235 | // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the |
| 236 | // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames are |
| 237 | // stored at MCDwarfFiles[FileNumber].Name . |
| 238 | DirIndex++; |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles |
| 242 | // vector. |
Benjamin Kramer | 3bce5ad | 2010-07-29 13:53:19 +0000 | [diff] [blame] | 243 | char *Buf = static_cast<char *>(Allocate(Name.size())); |
| 244 | memcpy(Buf, Name.data(), Name.size()); |
| 245 | File = new (*this) MCDwarfFile(StringRef(Buf, Name.size()), DirIndex); |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 246 | |
| 247 | // return the allocated FileNumber. |
| 248 | return FileNumber; |
| 249 | } |