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