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