blob: 9adcc02b71a4032f5a3acc754d287d82be78f813 [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"
Eric Christopher6c583142012-12-18 00:31:01 +000024#include "llvm/Support/MemoryBuffer.h"
Jim Grosbach82f4ce52012-01-26 23:20:11 +000025#include "llvm/Support/Signals.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/Support/SourceMgr.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000027using namespace llvm;
28
Chris Lattnerf0559e42010-04-08 20:30:37 +000029typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner74aae472010-04-08 21:26:26 +000030typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000031typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
Chris Lattnerf0559e42010-04-08 20:30:37 +000032
33
Evan Cheng0e6a0522011-07-18 20:57:22 +000034MCContext::MCContext(const MCAsmInfo &mai, const MCRegisterInfo &mri,
Pedro Artigas873a1dd2012-12-06 22:12:44 +000035 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
Eric Christopherd57a5982012-12-18 00:42:26 +000036 bool DoAutoReset) :
Jim Grosbach3662e5c2012-01-26 23:20:05 +000037 SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi),
Eli Friedmanf4387d92011-04-18 05:02:31 +000038 Allocator(), Symbols(Allocator), UsedNames(Allocator),
Chandler Carruth6c31d312012-12-17 21:32:42 +000039 NextUniqueID(0),
40 CompilationDir(llvm::sys::Path::GetCurrentDirectory().str()),
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
Chris Lattnerf0559e42010-04-08 20:30:37 +000045 MachOUniquingMap = 0;
Chris Lattner74aae472010-04-08 21:26:26 +000046 ELFUniquingMap = 0;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000047 COFFUniquingMap = 0;
Kevin Enderbyf187ac52010-06-28 21:45:58 +000048
49 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
50 SecureLog = 0;
51 SecureLogUsed = false;
Eric Christopher6c583142012-12-18 00:31:01 +000052
53 if (SrcMgr && SrcMgr->getNumBuffers() > 0)
54 MainFileName = SrcMgr->getMemoryBuffer(0)->getBufferIdentifier();
55 else
56 MainFileName = "";
Pedro Artigas873a1dd2012-12-06 22:12:44 +000057}
58
59MCContext::~MCContext() {
60
Pedro Artigas5399d252012-12-12 22:59:46 +000061 if (AutoReset)
62 reset();
Pedro Artigas873a1dd2012-12-06 22:12:44 +000063
64 // NOTE: The symbols are all allocated out of a bump pointer allocator,
65 // we don't need to free them here.
66
67 // If the stream for the .secure_log_unique directive was created free it.
68 delete (raw_ostream*)SecureLog;
69}
70
71//===----------------------------------------------------------------------===//
72// Module Lifetime Management
73//===----------------------------------------------------------------------===//
74
Pedro Artigas5399d252012-12-12 22:59:46 +000075void MCContext::reset() {
Pedro Artigas873a1dd2012-12-06 22:12:44 +000076 UsedNames.clear();
77 Symbols.clear();
78 Allocator.Reset();
79 Instances.clear();
Manman Ren3de61b42013-03-07 01:42:00 +000080 MCDwarfFilesCUMap.clear();
81 MCDwarfDirsCUMap.clear();
Pedro Artigas873a1dd2012-12-06 22:12:44 +000082 MCGenDwarfLabelEntries.clear();
83 DwarfDebugFlags = StringRef();
84 MCLineSections.clear();
85 MCLineSectionOrder.clear();
Pedro Artigas9e7924d2013-02-20 00:10:29 +000086 DwarfCompileUnitID = 0;
87 MCLineTableSymbols.clear();
Pedro Artigas873a1dd2012-12-06 22:12:44 +000088 CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0);
Michael J. Spencer326990f2010-11-26 04:16:08 +000089
Chris Lattnerf0559e42010-04-08 20:30:37 +000090 // If we have the MachO uniquing map, free it.
91 delete (MachOUniqueMapTy*)MachOUniquingMap;
Chris Lattner74aae472010-04-08 21:26:26 +000092 delete (ELFUniqueMapTy*)ELFUniquingMap;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000093 delete (COFFUniqueMapTy*)COFFUniquingMap;
Pedro Artigas873a1dd2012-12-06 22:12:44 +000094 MachOUniquingMap = 0;
95 ELFUniquingMap = 0;
96 COFFUniquingMap = 0;
Pedro Artigas5399d252012-12-12 22:59:46 +000097
98 NextUniqueID = 0;
99 AllowTemporaryLabels = true;
100 DwarfLocSeen = false;
101 GenDwarfForAssembly = false;
102 GenDwarfFileNumber = 0;
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000103}
104
Chris Lattnerf0559e42010-04-08 20:30:37 +0000105//===----------------------------------------------------------------------===//
106// Symbol Manipulation
107//===----------------------------------------------------------------------===//
108
Chris Lattner9b97a732010-03-30 18:10:53 +0000109MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattner00685bb2010-03-10 01:29:27 +0000110 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Michael J. Spencer326990f2010-11-26 04:16:08 +0000111
Chris Lattnerc28cc092010-03-15 06:15:35 +0000112 // Do the lookup and get the entire StringMapEntry. We want access to the
113 // key if we are creating the entry.
114 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
Rafael Espindola9f447242010-12-01 20:46:11 +0000115 MCSymbol *Sym = Entry.getValue();
116
117 if (Sym)
118 return Sym;
119
120 Sym = CreateSymbol(Name);
121 Entry.setValue(Sym);
122 return Sym;
123}
124
125MCSymbol *MCContext::CreateSymbol(StringRef Name) {
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +0000126 // Determine whether this is an assembler temporary or normal label, if used.
127 bool isTemporary = false;
128 if (AllowTemporaryLabels)
129 isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
Rafael Espindola9f447242010-12-01 20:46:11 +0000130
131 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
132 if (NameEntry->getValue()) {
133 assert(isTemporary && "Cannot rename non temporary symbols");
Benjamin Kramerc18214a2011-04-09 11:26:27 +0000134 SmallString<128> NewName = Name;
Rafael Espindola9f447242010-12-01 20:46:11 +0000135 do {
Benjamin Kramerc18214a2011-04-09 11:26:27 +0000136 NewName.resize(Name.size());
137 raw_svector_ostream(NewName) << NextUniqueID++;
138 NameEntry = &UsedNames.GetOrCreateValue(NewName);
Rafael Espindola9f447242010-12-01 20:46:11 +0000139 } while (NameEntry->getValue());
140 }
141 NameEntry->setValue(true);
Chris Lattnerc69485e2009-06-24 04:31:49 +0000142
Chris Lattnerc28cc092010-03-15 06:15:35 +0000143 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
Rafael Espindola9f447242010-12-01 20:46:11 +0000144 // to the copy of the string that is embedded in the UsedNames entry.
145 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
146
147 return Result;
Chris Lattnerc69485e2009-06-24 04:31:49 +0000148}
149
Chris Lattner9b97a732010-03-30 18:10:53 +0000150MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner7c5b0212009-10-19 22:49:00 +0000151 SmallString<128> NameSV;
152 Name.toVector(NameSV);
Chris Lattner9b97a732010-03-30 18:10:53 +0000153 return GetOrCreateSymbol(NameSV.str());
Chris Lattner7c5b0212009-10-19 22:49:00 +0000154}
155
Chris Lattner1d72a762010-03-14 08:23:30 +0000156MCSymbol *MCContext::CreateTempSymbol() {
Rafael Espindola9f447242010-12-01 20:46:11 +0000157 SmallString<128> NameSV;
Benjamin Kramerc18214a2011-04-09 11:26:27 +0000158 raw_svector_ostream(NameSV)
159 << MAI.getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
Rafael Espindola9f447242010-12-01 20:46:11 +0000160 return CreateSymbol(NameSV);
Chris Lattner1d72a762010-03-14 08:23:30 +0000161}
162
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000163unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000164 MCLabel *&Label = Instances[LocalLabelVal];
165 if (!Label)
166 Label = new (*this) MCLabel(0);
167 return Label->incInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000168}
169
170unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000171 MCLabel *&Label = Instances[LocalLabelVal];
172 if (!Label)
173 Label = new (*this) MCLabel(0);
174 return Label->getInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000175}
176
177MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
178 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
179 Twine(LocalLabelVal) +
180 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000181 Twine(NextInstance(LocalLabelVal)));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000182}
183MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
184 int bORf) {
185 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
186 Twine(LocalLabelVal) +
187 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000188 Twine(GetInstance(LocalLabelVal) + bORf));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000189}
190
Daniel Dunbar2928c832009-11-06 10:58:06 +0000191MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000192 return Symbols.lookup(Name);
193}
Chris Lattnerf0559e42010-04-08 20:30:37 +0000194
Roman Divackyf145c132012-09-18 17:10:37 +0000195MCSymbol *MCContext::LookupSymbol(const Twine &Name) const {
196 SmallString<128> NameSV;
197 Name.toVector(NameSV);
198 return LookupSymbol(NameSV.str());
199}
200
Chris Lattnerf0559e42010-04-08 20:30:37 +0000201//===----------------------------------------------------------------------===//
202// Section Management
203//===----------------------------------------------------------------------===//
204
205const MCSectionMachO *MCContext::
206getMachOSection(StringRef Segment, StringRef Section,
207 unsigned TypeAndAttributes,
208 unsigned Reserved2, SectionKind Kind) {
Michael J. Spencer326990f2010-11-26 04:16:08 +0000209
Chris Lattnerf0559e42010-04-08 20:30:37 +0000210 // We unique sections by their segment/section pair. The returned section
211 // may not have the same flags as the requested section, if so this should be
212 // diagnosed by the client as an error.
Michael J. Spencer326990f2010-11-26 04:16:08 +0000213
Chris Lattnerf0559e42010-04-08 20:30:37 +0000214 // Create the map if it doesn't already exist.
215 if (MachOUniquingMap == 0)
216 MachOUniquingMap = new MachOUniqueMapTy();
217 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000218
Chris Lattnerf0559e42010-04-08 20:30:37 +0000219 // Form the name to look up.
220 SmallString<64> Name;
221 Name += Segment;
222 Name.push_back(',');
223 Name += Section;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000224
Chris Lattnerf0559e42010-04-08 20:30:37 +0000225 // Do the lookup, if we have a hit, return it.
226 const MCSectionMachO *&Entry = Map[Name.str()];
227 if (Entry) return Entry;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000228
Chris Lattnerf0559e42010-04-08 20:30:37 +0000229 // Otherwise, return a new section.
Chris Lattner74aae472010-04-08 21:26:26 +0000230 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
231 Reserved2, Kind);
Chris Lattnerf0559e42010-04-08 20:30:37 +0000232}
Chris Lattner74aae472010-04-08 21:26:26 +0000233
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000234const MCSectionELF *MCContext::
Chris Lattner74aae472010-04-08 21:26:26 +0000235getELFSection(StringRef Section, unsigned Type, unsigned Flags,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000236 SectionKind Kind) {
237 return getELFSection(Section, Type, Flags, Kind, 0, "");
238}
239
240const MCSectionELF *MCContext::
241getELFSection(StringRef Section, unsigned Type, unsigned Flags,
242 SectionKind Kind, unsigned EntrySize, StringRef Group) {
Chris Lattner74aae472010-04-08 21:26:26 +0000243 if (ELFUniquingMap == 0)
244 ELFUniquingMap = new ELFUniqueMapTy();
245 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000246
Chris Lattner74aae472010-04-08 21:26:26 +0000247 // Do the lookup, if we have a hit, return it.
248 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
249 if (Entry.getValue()) return Entry.getValue();
Michael J. Spencer326990f2010-11-26 04:16:08 +0000250
Jan Wen Voung186e7a02010-09-30 05:59:22 +0000251 // Possibly refine the entry size first.
252 if (!EntrySize) {
253 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
254 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000255
256 MCSymbol *GroupSym = NULL;
257 if (!Group.empty())
258 GroupSym = GetOrCreateSymbol(Group);
259
Chris Lattner74aae472010-04-08 21:26:26 +0000260 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000261 Kind, EntrySize, GroupSym);
Chris Lattner74aae472010-04-08 21:26:26 +0000262 Entry.setValue(Result);
263 return Result;
264}
265
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000266const MCSectionELF *MCContext::CreateELFGroupSection() {
267 MCSectionELF *Result =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000268 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000269 SectionKind::getReadOnly(), 4, NULL);
270 return Result;
271}
272
Chris Lattner6e5ce282010-05-07 21:49:09 +0000273const MCSection *MCContext::getCOFFSection(StringRef Section,
274 unsigned Characteristics,
275 int Selection,
276 SectionKind Kind) {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000277 if (COFFUniquingMap == 0)
278 COFFUniquingMap = new COFFUniqueMapTy();
279 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000280
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000281 // Do the lookup, if we have a hit, return it.
282 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
283 if (Entry.getValue()) return Entry.getValue();
Michael J. Spencer326990f2010-11-26 04:16:08 +0000284
Chris Lattner6e5ce282010-05-07 21:49:09 +0000285 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
286 Characteristics,
287 Selection, Kind);
Michael J. Spencer326990f2010-11-26 04:16:08 +0000288
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000289 Entry.setValue(Result);
290 return Result;
291}
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000292
293//===----------------------------------------------------------------------===//
294// Dwarf Management
295//===----------------------------------------------------------------------===//
296
297/// GetDwarfFile - takes a file name an number to place in the dwarf file and
298/// directory tables. If the file number has already been allocated it is an
299/// error and zero is returned and the client reports the error, else the
300/// allocated file number is returned. The file numbers may be in any order.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000301unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren3de61b42013-03-07 01:42:00 +0000302 unsigned FileNumber, unsigned CUID) {
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000303 // TODO: a FileNumber of zero says to use the next available file number.
304 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
305 // to not be less than one. This needs to be change to be not less than zero.
306
Manman Ren9e999ad2013-03-12 20:17:00 +0000307 SmallVectorImpl<MCDwarfFile *>& MCDwarfFiles = MCDwarfFilesCUMap[CUID];
308 SmallVectorImpl<StringRef>& MCDwarfDirs = MCDwarfDirsCUMap[CUID];
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000309 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
310 if (FileNumber >= MCDwarfFiles.size()) {
311 MCDwarfFiles.resize(FileNumber + 1);
312 } else {
313 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
314 if (ExistingFile)
315 // It is an error to use see the same number more than once.
316 return 0;
317 }
318
319 // Get the new MCDwarfFile slot for this FileNumber.
320 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
321
Nick Lewycky44d798d2011-10-17 23:05:28 +0000322 if (Directory.empty()) {
323 // Separate the directory part from the basename of the FileName.
NAKAMURA Takumi4c215c02012-07-03 03:59:29 +0000324 StringRef tFileName = sys::path::filename(FileName);
325 if (!tFileName.empty()) {
326 Directory = sys::path::parent_path(FileName);
327 if (!Directory.empty())
NAKAMURA Takumia6f14532012-07-03 06:01:27 +0000328 FileName = tFileName;
Kevin Enderby064e48a2011-11-01 23:39:05 +0000329 }
Nick Lewycky44d798d2011-10-17 23:05:28 +0000330 }
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000331
332 // Find or make a entry in the MCDwarfDirs vector for this Directory.
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000333 // Capture directory name.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000334 unsigned DirIndex;
335 if (Directory.empty()) {
336 // For FileNames with no directories a DirIndex of 0 is used.
337 DirIndex = 0;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000338 } else {
Nick Lewycky18ad76b2011-10-12 20:20:48 +0000339 DirIndex = 0;
340 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000341 if (Directory == MCDwarfDirs[DirIndex])
Kevin Enderby232ab942010-08-31 22:55:11 +0000342 break;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000343 }
344 if (DirIndex >= MCDwarfDirs.size()) {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000345 char *Buf = static_cast<char *>(Allocate(Directory.size()));
346 memcpy(Buf, Directory.data(), Directory.size());
347 MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000348 }
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000349 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
350 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
Nick Lewycky44d798d2011-10-17 23:05:28 +0000351 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
352 // are stored at MCDwarfFiles[FileNumber].Name .
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000353 DirIndex++;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000354 }
Michael J. Spencer326990f2010-11-26 04:16:08 +0000355
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000356 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
357 // vector.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000358 char *Buf = static_cast<char *>(Allocate(FileName.size()));
359 memcpy(Buf, FileName.data(), FileName.size());
360 File = new (*this) MCDwarfFile(StringRef(Buf, FileName.size()), DirIndex);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000361
362 // return the allocated FileNumber.
363 return FileNumber;
364}
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000365
Kevin Enderby3f55c242010-10-04 20:17:24 +0000366/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000367/// currently is assigned and false otherwise.
Manman Ren3de61b42013-03-07 01:42:00 +0000368bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Manman Ren9e999ad2013-03-12 20:17:00 +0000369 SmallVectorImpl<MCDwarfFile *>& MCDwarfFiles = MCDwarfFilesCUMap[CUID];
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000370 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
371 return false;
372
Kevin Enderby3f55c242010-10-04 20:17:24 +0000373 return MCDwarfFiles[FileNumber] != 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000374}
Jim Grosbach82f4ce52012-01-26 23:20:11 +0000375
376void MCContext::FatalError(SMLoc Loc, const Twine &Msg) {
377 // If we have a source manager and a location, use it. Otherwise just
378 // use the generic report_fatal_error().
379 if (!SrcMgr || Loc == SMLoc())
380 report_fatal_error(Msg);
381
382 // Use the source manager to print the message.
383 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
384
385 // If we reached here, we are failing ungracefully. Run the interrupt handlers
386 // to make sure any special cleanups get done, in particular that we remove
387 // files registered with RemoveFileOnSignal.
388 sys::RunInterruptHandlers();
389 exit(1);
390}