blob: 67904f6f74b049bdccfd0a00c674f69cd04f352c [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"
David Blaikie2b3ea3c2013-10-22 23:41:52 +000028
29#include <map>
30
Daniel Dunbarecc63f82009-06-23 22:01:43 +000031using namespace llvm;
32
David Blaikie2b3ea3c2013-10-22 23:41:52 +000033typedef std::pair<std::string, std::string> SectionGroupPair;
34
Chris Lattnerf0559e42010-04-08 20:30:37 +000035typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
David Blaikie2b3ea3c2013-10-22 23:41:52 +000036typedef std::map<SectionGroupPair, const MCSectionELF *> ELFUniqueMapTy;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000037typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
Chris Lattnerf0559e42010-04-08 20:30:37 +000038
39
Bill Wendling99cb6222013-06-18 07:20:20 +000040MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigas873a1dd2012-12-06 22:12:44 +000041 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
Eric Christopherd57a5982012-12-18 00:42:26 +000042 bool DoAutoReset) :
Jim Grosbach3662e5c2012-01-26 23:20:05 +000043 SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi),
Eli Friedmanf4387d92011-04-18 05:02:31 +000044 Allocator(), Symbols(Allocator), UsedNames(Allocator),
Chandler Carruth6c31d312012-12-17 21:32:42 +000045 NextUniqueID(0),
Pedro Artigas5399d252012-12-12 22:59:46 +000046 CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0),
47 DwarfLocSeen(false), GenDwarfForAssembly(false), GenDwarfFileNumber(0),
Manman Ren43213cf2013-02-05 21:52:47 +000048 AllowTemporaryLabels(true), DwarfCompileUnitID(0), AutoReset(DoAutoReset) {
Pedro Artigas5399d252012-12-12 22:59:46 +000049
Rafael Espindola76858a72013-06-14 20:26:58 +000050 error_code EC = llvm::sys::fs::current_path(CompilationDir);
51 assert(!EC && "Could not determine the current directory");
Benjamin Kramer53aaef82013-06-16 11:29:48 +000052 (void)EC;
Rafael Espindola76858a72013-06-14 20:26:58 +000053
Chris Lattnerf0559e42010-04-08 20:30:37 +000054 MachOUniquingMap = 0;
Chris Lattner74aae472010-04-08 21:26:26 +000055 ELFUniquingMap = 0;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000056 COFFUniquingMap = 0;
Kevin Enderbyf187ac52010-06-28 21:45:58 +000057
58 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
59 SecureLog = 0;
60 SecureLogUsed = false;
Eric Christopher6c583142012-12-18 00:31:01 +000061
62 if (SrcMgr && SrcMgr->getNumBuffers() > 0)
63 MainFileName = SrcMgr->getMemoryBuffer(0)->getBufferIdentifier();
64 else
65 MainFileName = "";
Pedro Artigas873a1dd2012-12-06 22:12:44 +000066}
67
68MCContext::~MCContext() {
69
Pedro Artigas5399d252012-12-12 22:59:46 +000070 if (AutoReset)
71 reset();
Pedro Artigas873a1dd2012-12-06 22:12:44 +000072
73 // NOTE: The symbols are all allocated out of a bump pointer allocator,
74 // we don't need to free them here.
75
76 // If the stream for the .secure_log_unique directive was created free it.
77 delete (raw_ostream*)SecureLog;
78}
79
80//===----------------------------------------------------------------------===//
81// Module Lifetime Management
82//===----------------------------------------------------------------------===//
83
Pedro Artigas5399d252012-12-12 22:59:46 +000084void MCContext::reset() {
Pedro Artigas873a1dd2012-12-06 22:12:44 +000085 UsedNames.clear();
86 Symbols.clear();
87 Allocator.Reset();
88 Instances.clear();
Manman Ren3de61b42013-03-07 01:42:00 +000089 MCDwarfFilesCUMap.clear();
90 MCDwarfDirsCUMap.clear();
Pedro Artigas873a1dd2012-12-06 22:12:44 +000091 MCGenDwarfLabelEntries.clear();
92 DwarfDebugFlags = StringRef();
93 MCLineSections.clear();
94 MCLineSectionOrder.clear();
Pedro Artigas9e7924d2013-02-20 00:10:29 +000095 DwarfCompileUnitID = 0;
96 MCLineTableSymbols.clear();
Pedro Artigas873a1dd2012-12-06 22:12:44 +000097 CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0);
Michael J. Spencer326990f2010-11-26 04:16:08 +000098
Chris Lattnerf0559e42010-04-08 20:30:37 +000099 // If we have the MachO uniquing map, free it.
100 delete (MachOUniqueMapTy*)MachOUniquingMap;
Chris Lattner74aae472010-04-08 21:26:26 +0000101 delete (ELFUniqueMapTy*)ELFUniquingMap;
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000102 delete (COFFUniqueMapTy*)COFFUniquingMap;
Pedro Artigas873a1dd2012-12-06 22:12:44 +0000103 MachOUniquingMap = 0;
104 ELFUniquingMap = 0;
105 COFFUniquingMap = 0;
Pedro Artigas5399d252012-12-12 22:59:46 +0000106
107 NextUniqueID = 0;
108 AllowTemporaryLabels = true;
109 DwarfLocSeen = false;
110 GenDwarfForAssembly = false;
111 GenDwarfFileNumber = 0;
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000112}
113
Chris Lattnerf0559e42010-04-08 20:30:37 +0000114//===----------------------------------------------------------------------===//
115// Symbol Manipulation
116//===----------------------------------------------------------------------===//
117
Chris Lattner9b97a732010-03-30 18:10:53 +0000118MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattner00685bb2010-03-10 01:29:27 +0000119 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Michael J. Spencer326990f2010-11-26 04:16:08 +0000120
Chris Lattnerc28cc092010-03-15 06:15:35 +0000121 // Do the lookup and get the entire StringMapEntry. We want access to the
122 // key if we are creating the entry.
123 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
Rafael Espindola9f447242010-12-01 20:46:11 +0000124 MCSymbol *Sym = Entry.getValue();
125
126 if (Sym)
127 return Sym;
128
129 Sym = CreateSymbol(Name);
130 Entry.setValue(Sym);
131 return Sym;
132}
133
134MCSymbol *MCContext::CreateSymbol(StringRef Name) {
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +0000135 // Determine whether this is an assembler temporary or normal label, if used.
136 bool isTemporary = false;
137 if (AllowTemporaryLabels)
Bill Wendling99cb6222013-06-18 07:20:20 +0000138 isTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola9f447242010-12-01 20:46:11 +0000139
140 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
141 if (NameEntry->getValue()) {
142 assert(isTemporary && "Cannot rename non temporary symbols");
Benjamin Kramerc18214a2011-04-09 11:26:27 +0000143 SmallString<128> NewName = Name;
Rafael Espindola9f447242010-12-01 20:46:11 +0000144 do {
Benjamin Kramerc18214a2011-04-09 11:26:27 +0000145 NewName.resize(Name.size());
146 raw_svector_ostream(NewName) << NextUniqueID++;
147 NameEntry = &UsedNames.GetOrCreateValue(NewName);
Rafael Espindola9f447242010-12-01 20:46:11 +0000148 } while (NameEntry->getValue());
149 }
150 NameEntry->setValue(true);
Chris Lattnerc69485e2009-06-24 04:31:49 +0000151
Chris Lattnerc28cc092010-03-15 06:15:35 +0000152 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
Rafael Espindola9f447242010-12-01 20:46:11 +0000153 // to the copy of the string that is embedded in the UsedNames entry.
154 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
155
156 return Result;
Chris Lattnerc69485e2009-06-24 04:31:49 +0000157}
158
Chris Lattner9b97a732010-03-30 18:10:53 +0000159MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner7c5b0212009-10-19 22:49:00 +0000160 SmallString<128> NameSV;
161 Name.toVector(NameSV);
Chris Lattner9b97a732010-03-30 18:10:53 +0000162 return GetOrCreateSymbol(NameSV.str());
Chris Lattner7c5b0212009-10-19 22:49:00 +0000163}
164
Chris Lattner1d72a762010-03-14 08:23:30 +0000165MCSymbol *MCContext::CreateTempSymbol() {
Rafael Espindola9f447242010-12-01 20:46:11 +0000166 SmallString<128> NameSV;
Benjamin Kramerc18214a2011-04-09 11:26:27 +0000167 raw_svector_ostream(NameSV)
Bill Wendling99cb6222013-06-18 07:20:20 +0000168 << MAI->getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
Rafael Espindola9f447242010-12-01 20:46:11 +0000169 return CreateSymbol(NameSV);
Chris Lattner1d72a762010-03-14 08:23:30 +0000170}
171
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000172unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000173 MCLabel *&Label = Instances[LocalLabelVal];
174 if (!Label)
175 Label = new (*this) MCLabel(0);
176 return Label->incInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000177}
178
179unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000180 MCLabel *&Label = Instances[LocalLabelVal];
181 if (!Label)
182 Label = new (*this) MCLabel(0);
183 return Label->getInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000184}
185
186MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
Bill Wendling99cb6222013-06-18 07:20:20 +0000187 return GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) +
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000188 Twine(LocalLabelVal) +
189 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000190 Twine(NextInstance(LocalLabelVal)));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000191}
192MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
193 int bORf) {
Bill Wendling99cb6222013-06-18 07:20:20 +0000194 return GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) +
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000195 Twine(LocalLabelVal) +
196 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000197 Twine(GetInstance(LocalLabelVal) + bORf));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000198}
199
Daniel Dunbar2928c832009-11-06 10:58:06 +0000200MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000201 return Symbols.lookup(Name);
202}
Chris Lattnerf0559e42010-04-08 20:30:37 +0000203
Roman Divackyf145c132012-09-18 17:10:37 +0000204MCSymbol *MCContext::LookupSymbol(const Twine &Name) const {
205 SmallString<128> NameSV;
206 Name.toVector(NameSV);
207 return LookupSymbol(NameSV.str());
208}
209
Chris Lattnerf0559e42010-04-08 20:30:37 +0000210//===----------------------------------------------------------------------===//
211// Section Management
212//===----------------------------------------------------------------------===//
213
214const MCSectionMachO *MCContext::
215getMachOSection(StringRef Segment, StringRef Section,
216 unsigned TypeAndAttributes,
217 unsigned Reserved2, SectionKind Kind) {
Michael J. Spencer326990f2010-11-26 04:16:08 +0000218
Chris Lattnerf0559e42010-04-08 20:30:37 +0000219 // We unique sections by their segment/section pair. The returned section
220 // may not have the same flags as the requested section, if so this should be
221 // diagnosed by the client as an error.
Michael J. Spencer326990f2010-11-26 04:16:08 +0000222
Chris Lattnerf0559e42010-04-08 20:30:37 +0000223 // Create the map if it doesn't already exist.
224 if (MachOUniquingMap == 0)
225 MachOUniquingMap = new MachOUniqueMapTy();
226 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000227
Chris Lattnerf0559e42010-04-08 20:30:37 +0000228 // Form the name to look up.
229 SmallString<64> Name;
230 Name += Segment;
231 Name.push_back(',');
232 Name += Section;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000233
Chris Lattnerf0559e42010-04-08 20:30:37 +0000234 // Do the lookup, if we have a hit, return it.
235 const MCSectionMachO *&Entry = Map[Name.str()];
236 if (Entry) return Entry;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000237
Chris Lattnerf0559e42010-04-08 20:30:37 +0000238 // Otherwise, return a new section.
Chris Lattner74aae472010-04-08 21:26:26 +0000239 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
240 Reserved2, Kind);
Chris Lattnerf0559e42010-04-08 20:30:37 +0000241}
Chris Lattner74aae472010-04-08 21:26:26 +0000242
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000243const MCSectionELF *MCContext::
Chris Lattner74aae472010-04-08 21:26:26 +0000244getELFSection(StringRef Section, unsigned Type, unsigned Flags,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000245 SectionKind Kind) {
246 return getELFSection(Section, Type, Flags, Kind, 0, "");
247}
248
249const MCSectionELF *MCContext::
250getELFSection(StringRef Section, unsigned Type, unsigned Flags,
251 SectionKind Kind, unsigned EntrySize, StringRef Group) {
Chris Lattner74aae472010-04-08 21:26:26 +0000252 if (ELFUniquingMap == 0)
253 ELFUniquingMap = new ELFUniqueMapTy();
254 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000255
Chris Lattner74aae472010-04-08 21:26:26 +0000256 // Do the lookup, if we have a hit, return it.
David Blaikie2b3ea3c2013-10-22 23:41:52 +0000257 std::pair<ELFUniqueMapTy::iterator, bool> Entry = Map.insert(
258 std::make_pair(SectionGroupPair(Section, Group), (MCSectionELF *)0));
259 if (!Entry.second) return Entry.first->second;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000260
Jan Wen Voung186e7a02010-09-30 05:59:22 +0000261 // Possibly refine the entry size first.
262 if (!EntrySize) {
263 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
264 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000265
266 MCSymbol *GroupSym = NULL;
267 if (!Group.empty())
268 GroupSym = GetOrCreateSymbol(Group);
269
David Blaikie2b3ea3c2013-10-22 23:41:52 +0000270 MCSectionELF *Result = new (*this) MCSectionELF(
271 Entry.first->first.first, Type, Flags, Kind, EntrySize, GroupSym);
272 Entry.first->second = Result;
Chris Lattner74aae472010-04-08 21:26:26 +0000273 return Result;
274}
275
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000276const MCSectionELF *MCContext::CreateELFGroupSection() {
277 MCSectionELF *Result =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000278 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000279 SectionKind::getReadOnly(), 4, NULL);
280 return Result;
281}
282
Nico Rieck80646282013-07-06 12:13:10 +0000283const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
284 unsigned Characteristics,
285 SectionKind Kind, int Selection,
286 const MCSectionCOFF *Assoc) {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000287 if (COFFUniquingMap == 0)
288 COFFUniquingMap = new COFFUniqueMapTy();
289 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000290
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000291 // Do the lookup, if we have a hit, return it.
292 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
293 if (Entry.getValue()) return Entry.getValue();
Michael J. Spencer326990f2010-11-26 04:16:08 +0000294
Chris Lattner6e5ce282010-05-07 21:49:09 +0000295 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
296 Characteristics,
Nico Rieck80646282013-07-06 12:13:10 +0000297 Selection, Assoc, Kind);
Michael J. Spencer326990f2010-11-26 04:16:08 +0000298
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000299 Entry.setValue(Result);
300 return Result;
301}
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000302
Nico Rieck80646282013-07-06 12:13:10 +0000303const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
304 if (COFFUniquingMap == 0)
305 COFFUniquingMap = new COFFUniqueMapTy();
306 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
307
308 return Map.lookup(Section);
309}
310
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000311//===----------------------------------------------------------------------===//
312// Dwarf Management
313//===----------------------------------------------------------------------===//
314
315/// GetDwarfFile - takes a file name an number to place in the dwarf file and
316/// directory tables. If the file number has already been allocated it is an
317/// error and zero is returned and the client reports the error, else the
318/// allocated file number is returned. The file numbers may be in any order.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000319unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren3de61b42013-03-07 01:42:00 +0000320 unsigned FileNumber, unsigned CUID) {
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000321 // TODO: a FileNumber of zero says to use the next available file number.
322 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
323 // to not be less than one. This needs to be change to be not less than zero.
324
Manman Ren9e999ad2013-03-12 20:17:00 +0000325 SmallVectorImpl<MCDwarfFile *>& MCDwarfFiles = MCDwarfFilesCUMap[CUID];
326 SmallVectorImpl<StringRef>& MCDwarfDirs = MCDwarfDirsCUMap[CUID];
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000327 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
328 if (FileNumber >= MCDwarfFiles.size()) {
329 MCDwarfFiles.resize(FileNumber + 1);
330 } else {
331 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
332 if (ExistingFile)
333 // It is an error to use see the same number more than once.
334 return 0;
335 }
336
337 // Get the new MCDwarfFile slot for this FileNumber.
338 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
339
Nick Lewycky44d798d2011-10-17 23:05:28 +0000340 if (Directory.empty()) {
341 // Separate the directory part from the basename of the FileName.
NAKAMURA Takumi4c215c02012-07-03 03:59:29 +0000342 StringRef tFileName = sys::path::filename(FileName);
343 if (!tFileName.empty()) {
344 Directory = sys::path::parent_path(FileName);
345 if (!Directory.empty())
NAKAMURA Takumia6f14532012-07-03 06:01:27 +0000346 FileName = tFileName;
Kevin Enderby064e48a2011-11-01 23:39:05 +0000347 }
Nick Lewycky44d798d2011-10-17 23:05:28 +0000348 }
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000349
350 // Find or make a entry in the MCDwarfDirs vector for this Directory.
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000351 // Capture directory name.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000352 unsigned DirIndex;
353 if (Directory.empty()) {
354 // For FileNames with no directories a DirIndex of 0 is used.
355 DirIndex = 0;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000356 } else {
Nick Lewycky18ad76b2011-10-12 20:20:48 +0000357 DirIndex = 0;
358 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000359 if (Directory == MCDwarfDirs[DirIndex])
Kevin Enderby232ab942010-08-31 22:55:11 +0000360 break;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000361 }
362 if (DirIndex >= MCDwarfDirs.size()) {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000363 char *Buf = static_cast<char *>(Allocate(Directory.size()));
364 memcpy(Buf, Directory.data(), Directory.size());
365 MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000366 }
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000367 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
368 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
Nick Lewycky44d798d2011-10-17 23:05:28 +0000369 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
370 // are stored at MCDwarfFiles[FileNumber].Name .
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000371 DirIndex++;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000372 }
Michael J. Spencer326990f2010-11-26 04:16:08 +0000373
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000374 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
375 // vector.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000376 char *Buf = static_cast<char *>(Allocate(FileName.size()));
377 memcpy(Buf, FileName.data(), FileName.size());
378 File = new (*this) MCDwarfFile(StringRef(Buf, FileName.size()), DirIndex);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000379
380 // return the allocated FileNumber.
381 return FileNumber;
382}
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000383
Kevin Enderby3f55c242010-10-04 20:17:24 +0000384/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000385/// currently is assigned and false otherwise.
Manman Ren3de61b42013-03-07 01:42:00 +0000386bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Manman Ren9e999ad2013-03-12 20:17:00 +0000387 SmallVectorImpl<MCDwarfFile *>& MCDwarfFiles = MCDwarfFilesCUMap[CUID];
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000388 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
389 return false;
390
Kevin Enderby3f55c242010-10-04 20:17:24 +0000391 return MCDwarfFiles[FileNumber] != 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000392}
Jim Grosbach82f4ce52012-01-26 23:20:11 +0000393
394void MCContext::FatalError(SMLoc Loc, const Twine &Msg) {
395 // If we have a source manager and a location, use it. Otherwise just
396 // use the generic report_fatal_error().
397 if (!SrcMgr || Loc == SMLoc())
398 report_fatal_error(Msg);
399
400 // Use the source manager to print the message.
401 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
402
403 // If we reached here, we are failing ungracefully. Run the interrupt handlers
404 // to make sure any special cleanups get done, in particular that we remove
405 // files registered with RemoveFileOnSignal.
406 sys::RunInterruptHandlers();
407 exit(1);
408}