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