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