blob: 6e4d82b6e7650b73dffe78c9162ff8fbe60cf0f1 [file] [log] [blame]
Daniel Dunbarba1da8a2009-06-23 23:39:15 +00001//===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
Daniel Dunbarecc63f82009-06-23 22:01:43 +00002//
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 Lattner7c5b0212009-10-19 22:49:00 +000011#include "llvm/ADT/SmallString.h"
12#include "llvm/ADT/Twine.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000013#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 Espindolac85dca62011-01-23 04:28:49 +000022#include "llvm/Support/ELF.h"
Jim Grosbach82f4ce52012-01-26 23:20:11 +000023#include "llvm/Support/ErrorHandling.h"
Rafael Espindola76858a72013-06-14 20:26:58 +000024#include "llvm/Support/FileSystem.h"
Eric Christopher6c583142012-12-18 00:31:01 +000025#include "llvm/Support/MemoryBuffer.h"
Jim Grosbach82f4ce52012-01-26 23:20:11 +000026#include "llvm/Support/Signals.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/Support/SourceMgr.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000028using namespace llvm;
29
Chris Lattnerf0559e42010-04-08 20:30:37 +000030typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner74aae472010-04-08 21:26:26 +000031typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000032typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
Chris Lattnerf0559e42010-04-08 20:30:37 +000033
34
Bill Wendling99cb6222013-06-18 07:20:20 +000035MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigas873a1dd2012-12-06 22:12:44 +000036 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
Eric Christopherd57a5982012-12-18 00:42:26 +000037 bool DoAutoReset) :
Jim Grosbach3662e5c2012-01-26 23:20:05 +000038 SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi),
Eli Friedmanf4387d92011-04-18 05:02:31 +000039 Allocator(), Symbols(Allocator), UsedNames(Allocator),
Chandler Carruth6c31d312012-12-17 21:32:42 +000040 NextUniqueID(0),
Pedro Artigas5399d252012-12-12 22:59:46 +000041 CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0),
42 DwarfLocSeen(false), GenDwarfForAssembly(false), GenDwarfFileNumber(0),
Manman Ren43213cf2013-02-05 21:52:47 +000043 AllowTemporaryLabels(true), DwarfCompileUnitID(0), AutoReset(DoAutoReset) {
Pedro Artigas5399d252012-12-12 22:59:46 +000044
Rafael Espindola76858a72013-06-14 20:26:58 +000045 error_code EC = llvm::sys::fs::current_path(CompilationDir);
46 assert(!EC && "Could not determine the current directory");
Benjamin Kramer53aaef82013-06-16 11:29:48 +000047 (void)EC;
Rafael Espindola76858a72013-06-14 20:26:58 +000048
Chris Lattnerf0559e42010-04-08 20:30:37 +000049 MachOUniquingMap = 0;
Chris Lattner74aae472010-04-08 21:26:26 +000050 ELFUniquingMap = 0;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000051 COFFUniquingMap = 0;
Kevin Enderbyf187ac52010-06-28 21:45:58 +000052
53 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
54 SecureLog = 0;
55 SecureLogUsed = false;
Eric Christopher6c583142012-12-18 00:31:01 +000056
57 if (SrcMgr && SrcMgr->getNumBuffers() > 0)
58 MainFileName = SrcMgr->getMemoryBuffer(0)->getBufferIdentifier();
59 else
60 MainFileName = "";
Pedro Artigas873a1dd2012-12-06 22:12:44 +000061}
62
63MCContext::~MCContext() {
64
Pedro Artigas5399d252012-12-12 22:59:46 +000065 if (AutoReset)
66 reset();
Pedro Artigas873a1dd2012-12-06 22:12:44 +000067
68 // NOTE: The symbols are all allocated out of a bump pointer allocator,
69 // we don't need to free them here.
70
71 // If the stream for the .secure_log_unique directive was created free it.
72 delete (raw_ostream*)SecureLog;
73}
74
75//===----------------------------------------------------------------------===//
76// Module Lifetime Management
77//===----------------------------------------------------------------------===//
78
Pedro Artigas5399d252012-12-12 22:59:46 +000079void MCContext::reset() {
Pedro Artigas873a1dd2012-12-06 22:12:44 +000080 UsedNames.clear();
81 Symbols.clear();
82 Allocator.Reset();
83 Instances.clear();
Manman Ren3de61b42013-03-07 01:42:00 +000084 MCDwarfFilesCUMap.clear();
85 MCDwarfDirsCUMap.clear();
Pedro Artigas873a1dd2012-12-06 22:12:44 +000086 MCGenDwarfLabelEntries.clear();
87 DwarfDebugFlags = StringRef();
88 MCLineSections.clear();
89 MCLineSectionOrder.clear();
Pedro Artigas9e7924d2013-02-20 00:10:29 +000090 DwarfCompileUnitID = 0;
91 MCLineTableSymbols.clear();
Pedro Artigas873a1dd2012-12-06 22:12:44 +000092 CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0);
Michael J. Spencer326990f2010-11-26 04:16:08 +000093
Chris Lattnerf0559e42010-04-08 20:30:37 +000094 // If we have the MachO uniquing map, free it.
95 delete (MachOUniqueMapTy*)MachOUniquingMap;
Chris Lattner74aae472010-04-08 21:26:26 +000096 delete (ELFUniqueMapTy*)ELFUniquingMap;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000097 delete (COFFUniqueMapTy*)COFFUniquingMap;
Pedro Artigas873a1dd2012-12-06 22:12:44 +000098 MachOUniquingMap = 0;
99 ELFUniquingMap = 0;
100 COFFUniquingMap = 0;
Pedro Artigas5399d252012-12-12 22:59:46 +0000101
102 NextUniqueID = 0;
103 AllowTemporaryLabels = true;
104 DwarfLocSeen = false;
105 GenDwarfForAssembly = false;
106 GenDwarfFileNumber = 0;
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000107}
108
Chris Lattnerf0559e42010-04-08 20:30:37 +0000109//===----------------------------------------------------------------------===//
110// Symbol Manipulation
111//===----------------------------------------------------------------------===//
112
Chris Lattner9b97a732010-03-30 18:10:53 +0000113MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattner00685bb2010-03-10 01:29:27 +0000114 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Michael J. Spencer326990f2010-11-26 04:16:08 +0000115
Chris Lattnerc28cc092010-03-15 06:15:35 +0000116 // Do the lookup and get the entire StringMapEntry. We want access to the
117 // key if we are creating the entry.
118 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
Rafael Espindola9f447242010-12-01 20:46:11 +0000119 MCSymbol *Sym = Entry.getValue();
120
121 if (Sym)
122 return Sym;
123
124 Sym = CreateSymbol(Name);
125 Entry.setValue(Sym);
126 return Sym;
127}
128
129MCSymbol *MCContext::CreateSymbol(StringRef Name) {
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +0000130 // Determine whether this is an assembler temporary or normal label, if used.
131 bool isTemporary = false;
132 if (AllowTemporaryLabels)
Bill Wendling99cb6222013-06-18 07:20:20 +0000133 isTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola9f447242010-12-01 20:46:11 +0000134
135 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
136 if (NameEntry->getValue()) {
137 assert(isTemporary && "Cannot rename non temporary symbols");
Benjamin Kramerc18214a2011-04-09 11:26:27 +0000138 SmallString<128> NewName = Name;
Rafael Espindola9f447242010-12-01 20:46:11 +0000139 do {
Benjamin Kramerc18214a2011-04-09 11:26:27 +0000140 NewName.resize(Name.size());
141 raw_svector_ostream(NewName) << NextUniqueID++;
142 NameEntry = &UsedNames.GetOrCreateValue(NewName);
Rafael Espindola9f447242010-12-01 20:46:11 +0000143 } while (NameEntry->getValue());
144 }
145 NameEntry->setValue(true);
Chris Lattnerc69485e2009-06-24 04:31:49 +0000146
Chris Lattnerc28cc092010-03-15 06:15:35 +0000147 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
Rafael Espindola9f447242010-12-01 20:46:11 +0000148 // to the copy of the string that is embedded in the UsedNames entry.
149 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
150
151 return Result;
Chris Lattnerc69485e2009-06-24 04:31:49 +0000152}
153
Chris Lattner9b97a732010-03-30 18:10:53 +0000154MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner7c5b0212009-10-19 22:49:00 +0000155 SmallString<128> NameSV;
156 Name.toVector(NameSV);
Chris Lattner9b97a732010-03-30 18:10:53 +0000157 return GetOrCreateSymbol(NameSV.str());
Chris Lattner7c5b0212009-10-19 22:49:00 +0000158}
159
Chris Lattner1d72a762010-03-14 08:23:30 +0000160MCSymbol *MCContext::CreateTempSymbol() {
Rafael Espindola9f447242010-12-01 20:46:11 +0000161 SmallString<128> NameSV;
Benjamin Kramerc18214a2011-04-09 11:26:27 +0000162 raw_svector_ostream(NameSV)
Bill Wendling99cb6222013-06-18 07:20:20 +0000163 << MAI->getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
Rafael Espindola9f447242010-12-01 20:46:11 +0000164 return CreateSymbol(NameSV);
Chris Lattner1d72a762010-03-14 08:23:30 +0000165}
166
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000167unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000168 MCLabel *&Label = Instances[LocalLabelVal];
169 if (!Label)
170 Label = new (*this) MCLabel(0);
171 return Label->incInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000172}
173
174unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000175 MCLabel *&Label = Instances[LocalLabelVal];
176 if (!Label)
177 Label = new (*this) MCLabel(0);
178 return Label->getInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000179}
180
181MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
Bill Wendling99cb6222013-06-18 07:20:20 +0000182 return GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) +
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000183 Twine(LocalLabelVal) +
184 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000185 Twine(NextInstance(LocalLabelVal)));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000186}
187MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
188 int bORf) {
Bill Wendling99cb6222013-06-18 07:20:20 +0000189 return GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) +
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000190 Twine(LocalLabelVal) +
191 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000192 Twine(GetInstance(LocalLabelVal) + bORf));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000193}
194
Daniel Dunbar2928c832009-11-06 10:58:06 +0000195MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000196 return Symbols.lookup(Name);
197}
Chris Lattnerf0559e42010-04-08 20:30:37 +0000198
Roman Divackyf145c132012-09-18 17:10:37 +0000199MCSymbol *MCContext::LookupSymbol(const Twine &Name) const {
200 SmallString<128> NameSV;
201 Name.toVector(NameSV);
202 return LookupSymbol(NameSV.str());
203}
204
Chris Lattnerf0559e42010-04-08 20:30:37 +0000205//===----------------------------------------------------------------------===//
206// Section Management
207//===----------------------------------------------------------------------===//
208
209const MCSectionMachO *MCContext::
210getMachOSection(StringRef Segment, StringRef Section,
211 unsigned TypeAndAttributes,
212 unsigned Reserved2, SectionKind Kind) {
Michael J. Spencer326990f2010-11-26 04:16:08 +0000213
Chris Lattnerf0559e42010-04-08 20:30:37 +0000214 // We unique sections by their segment/section pair. The returned section
215 // may not have the same flags as the requested section, if so this should be
216 // diagnosed by the client as an error.
Michael J. Spencer326990f2010-11-26 04:16:08 +0000217
Chris Lattnerf0559e42010-04-08 20:30:37 +0000218 // Create the map if it doesn't already exist.
219 if (MachOUniquingMap == 0)
220 MachOUniquingMap = new MachOUniqueMapTy();
221 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000222
Chris Lattnerf0559e42010-04-08 20:30:37 +0000223 // Form the name to look up.
224 SmallString<64> Name;
225 Name += Segment;
226 Name.push_back(',');
227 Name += Section;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000228
Chris Lattnerf0559e42010-04-08 20:30:37 +0000229 // Do the lookup, if we have a hit, return it.
230 const MCSectionMachO *&Entry = Map[Name.str()];
231 if (Entry) return Entry;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000232
Chris Lattnerf0559e42010-04-08 20:30:37 +0000233 // Otherwise, return a new section.
Chris Lattner74aae472010-04-08 21:26:26 +0000234 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
235 Reserved2, Kind);
Chris Lattnerf0559e42010-04-08 20:30:37 +0000236}
Chris Lattner74aae472010-04-08 21:26:26 +0000237
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000238const MCSectionELF *MCContext::
Chris Lattner74aae472010-04-08 21:26:26 +0000239getELFSection(StringRef Section, unsigned Type, unsigned Flags,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000240 SectionKind Kind) {
241 return getELFSection(Section, Type, Flags, Kind, 0, "");
242}
243
244const MCSectionELF *MCContext::
245getELFSection(StringRef Section, unsigned Type, unsigned Flags,
246 SectionKind Kind, unsigned EntrySize, StringRef Group) {
Chris Lattner74aae472010-04-08 21:26:26 +0000247 if (ELFUniquingMap == 0)
248 ELFUniquingMap = new ELFUniqueMapTy();
249 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000250
Chris Lattner74aae472010-04-08 21:26:26 +0000251 // Do the lookup, if we have a hit, return it.
252 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
253 if (Entry.getValue()) return Entry.getValue();
Michael J. Spencer326990f2010-11-26 04:16:08 +0000254
Jan Wen Voung186e7a02010-09-30 05:59:22 +0000255 // Possibly refine the entry size first.
256 if (!EntrySize) {
257 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
258 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000259
260 MCSymbol *GroupSym = NULL;
261 if (!Group.empty())
262 GroupSym = GetOrCreateSymbol(Group);
263
Chris Lattner74aae472010-04-08 21:26:26 +0000264 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000265 Kind, EntrySize, GroupSym);
Chris Lattner74aae472010-04-08 21:26:26 +0000266 Entry.setValue(Result);
267 return Result;
268}
269
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000270const MCSectionELF *MCContext::CreateELFGroupSection() {
271 MCSectionELF *Result =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000272 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000273 SectionKind::getReadOnly(), 4, NULL);
274 return Result;
275}
276
Nico Rieck80646282013-07-06 12:13:10 +0000277const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
278 unsigned Characteristics,
279 SectionKind Kind, int Selection,
280 const MCSectionCOFF *Assoc) {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000281 if (COFFUniquingMap == 0)
282 COFFUniquingMap = new COFFUniqueMapTy();
283 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000284
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000285 // Do the lookup, if we have a hit, return it.
286 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
287 if (Entry.getValue()) return Entry.getValue();
Michael J. Spencer326990f2010-11-26 04:16:08 +0000288
Chris Lattner6e5ce282010-05-07 21:49:09 +0000289 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
290 Characteristics,
Nico Rieck80646282013-07-06 12:13:10 +0000291 Selection, Assoc, Kind);
Michael J. Spencer326990f2010-11-26 04:16:08 +0000292
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000293 Entry.setValue(Result);
294 return Result;
295}
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000296
Nico Rieck80646282013-07-06 12:13:10 +0000297const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
298 if (COFFUniquingMap == 0)
299 COFFUniquingMap = new COFFUniqueMapTy();
300 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
301
302 return Map.lookup(Section);
303}
304
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000305//===----------------------------------------------------------------------===//
306// Dwarf Management
307//===----------------------------------------------------------------------===//
308
309/// GetDwarfFile - takes a file name an number to place in the dwarf file and
310/// directory tables. If the file number has already been allocated it is an
311/// error and zero is returned and the client reports the error, else the
312/// allocated file number is returned. The file numbers may be in any order.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000313unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren3de61b42013-03-07 01:42:00 +0000314 unsigned FileNumber, unsigned CUID) {
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000315 // TODO: a FileNumber of zero says to use the next available file number.
316 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
317 // to not be less than one. This needs to be change to be not less than zero.
318
Manman Ren9e999ad2013-03-12 20:17:00 +0000319 SmallVectorImpl<MCDwarfFile *>& MCDwarfFiles = MCDwarfFilesCUMap[CUID];
320 SmallVectorImpl<StringRef>& MCDwarfDirs = MCDwarfDirsCUMap[CUID];
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000321 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
322 if (FileNumber >= MCDwarfFiles.size()) {
323 MCDwarfFiles.resize(FileNumber + 1);
324 } else {
325 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
326 if (ExistingFile)
327 // It is an error to use see the same number more than once.
328 return 0;
329 }
330
331 // Get the new MCDwarfFile slot for this FileNumber.
332 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
333
Nick Lewycky44d798d2011-10-17 23:05:28 +0000334 if (Directory.empty()) {
335 // Separate the directory part from the basename of the FileName.
NAKAMURA Takumi4c215c02012-07-03 03:59:29 +0000336 StringRef tFileName = sys::path::filename(FileName);
337 if (!tFileName.empty()) {
338 Directory = sys::path::parent_path(FileName);
339 if (!Directory.empty())
NAKAMURA Takumia6f14532012-07-03 06:01:27 +0000340 FileName = tFileName;
Kevin Enderby064e48a2011-11-01 23:39:05 +0000341 }
Nick Lewycky44d798d2011-10-17 23:05:28 +0000342 }
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000343
344 // Find or make a entry in the MCDwarfDirs vector for this Directory.
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000345 // Capture directory name.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000346 unsigned DirIndex;
347 if (Directory.empty()) {
348 // For FileNames with no directories a DirIndex of 0 is used.
349 DirIndex = 0;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000350 } else {
Nick Lewycky18ad76b2011-10-12 20:20:48 +0000351 DirIndex = 0;
352 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000353 if (Directory == MCDwarfDirs[DirIndex])
Kevin Enderby232ab942010-08-31 22:55:11 +0000354 break;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000355 }
356 if (DirIndex >= MCDwarfDirs.size()) {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000357 char *Buf = static_cast<char *>(Allocate(Directory.size()));
358 memcpy(Buf, Directory.data(), Directory.size());
359 MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000360 }
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000361 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
362 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
Nick Lewycky44d798d2011-10-17 23:05:28 +0000363 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
364 // are stored at MCDwarfFiles[FileNumber].Name .
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000365 DirIndex++;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000366 }
Michael J. Spencer326990f2010-11-26 04:16:08 +0000367
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000368 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
369 // vector.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000370 char *Buf = static_cast<char *>(Allocate(FileName.size()));
371 memcpy(Buf, FileName.data(), FileName.size());
372 File = new (*this) MCDwarfFile(StringRef(Buf, FileName.size()), DirIndex);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000373
374 // return the allocated FileNumber.
375 return FileNumber;
376}
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000377
Kevin Enderby3f55c242010-10-04 20:17:24 +0000378/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000379/// currently is assigned and false otherwise.
Manman Ren3de61b42013-03-07 01:42:00 +0000380bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Manman Ren9e999ad2013-03-12 20:17:00 +0000381 SmallVectorImpl<MCDwarfFile *>& MCDwarfFiles = MCDwarfFilesCUMap[CUID];
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000382 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
383 return false;
384
Kevin Enderby3f55c242010-10-04 20:17:24 +0000385 return MCDwarfFiles[FileNumber] != 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000386}
Jim Grosbach82f4ce52012-01-26 23:20:11 +0000387
388void MCContext::FatalError(SMLoc Loc, const Twine &Msg) {
389 // If we have a source manager and a location, use it. Otherwise just
390 // use the generic report_fatal_error().
391 if (!SrcMgr || Loc == SMLoc())
392 report_fatal_error(Msg);
393
394 // Use the source manager to print the message.
395 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
396
397 // If we reached here, we are failing ungracefully. Run the interrupt handlers
398 // to make sure any special cleanups get done, in particular that we remove
399 // files registered with RemoveFileOnSignal.
400 sys::RunInterruptHandlers();
401 exit(1);
402}