blob: 7e4cdf98eef7f68e5033a391d2f5c014eb35779c [file] [log] [blame]
Daniel Dunbarbadeace2009-06-23 23:39:15 +00001//===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
Daniel Dunbarca29e4d2009-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 Lattner86dfd732009-10-19 22:49:00 +000011#include "llvm/ADT/SmallString.h"
12#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-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 Espindolaaea49582011-01-23 04:28:49 +000022#include "llvm/Support/ELF.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000023#include "llvm/Support/ErrorHandling.h"
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000024#include "llvm/Support/FileSystem.h"
Eric Christopher906da232012-12-18 00:31:01 +000025#include "llvm/Support/MemoryBuffer.h"
Jim Grosbachb18b4092012-01-26 23:20:11 +000026#include "llvm/Support/Signals.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Support/SourceMgr.h"
David Blaikie15b25df2013-10-22 23:41:52 +000028
29#include <map>
30
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000031using namespace llvm;
32
David Blaikie15b25df2013-10-22 23:41:52 +000033typedef std::pair<std::string, std::string> SectionGroupPair;
34
Chris Lattner20731122010-04-08 20:30:37 +000035typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
David Blaikie15b25df2013-10-22 23:41:52 +000036typedef std::map<SectionGroupPair, const MCSectionELF *> ELFUniqueMapTy;
Rafael Espindola60ec3832013-11-19 19:52:52 +000037typedef std::map<SectionGroupPair, const MCSectionCOFF *> COFFUniqueMapTy;
Chris Lattner20731122010-04-08 20:30:37 +000038
Bill Wendlingbc07a892013-06-18 07:20:20 +000039MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
Pedro Artigase84b13f2012-12-06 22:12:44 +000040 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
Eric Christopher79f16562012-12-18 00:42:26 +000041 bool DoAutoReset) :
Jim Grosbachdfc5b552012-01-26 23:20:05 +000042 SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi),
Eli Friedman0e402082011-04-18 05:02:31 +000043 Allocator(), Symbols(Allocator), UsedNames(Allocator),
Chandler Carruth10700aad2012-12-17 21:32:42 +000044 NextUniqueID(0),
Pedro Artigas7212ee42012-12-12 22:59:46 +000045 CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0),
46 DwarfLocSeen(false), GenDwarfForAssembly(false), GenDwarfFileNumber(0),
Manman Ren4e042a62013-02-05 21:52:47 +000047 AllowTemporaryLabels(true), DwarfCompileUnitID(0), AutoReset(DoAutoReset) {
Pedro Artigas7212ee42012-12-12 22:59:46 +000048
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000049 error_code EC = llvm::sys::fs::current_path(CompilationDir);
50 assert(!EC && "Could not determine the current directory");
Benjamin Kramere9d5fb02013-06-16 11:29:48 +000051 (void)EC;
Rafael Espindolaef03b9f2013-06-14 20:26:58 +000052
Chris Lattner20731122010-04-08 20:30:37 +000053 MachOUniquingMap = 0;
Chris Lattner5418dd52010-04-08 21:26:26 +000054 ELFUniquingMap = 0;
Chris Lattner87cffa92010-05-07 17:17:41 +000055 COFFUniquingMap = 0;
Kevin Enderbye233dda2010-06-28 21:45:58 +000056
57 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
58 SecureLog = 0;
59 SecureLogUsed = false;
Eric Christopher906da232012-12-18 00:31:01 +000060
61 if (SrcMgr && SrcMgr->getNumBuffers() > 0)
62 MainFileName = SrcMgr->getMemoryBuffer(0)->getBufferIdentifier();
63 else
64 MainFileName = "";
Pedro Artigase84b13f2012-12-06 22:12:44 +000065}
66
67MCContext::~MCContext() {
68
Pedro Artigas7212ee42012-12-12 22:59:46 +000069 if (AutoReset)
70 reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +000071
72 // NOTE: The symbols are all allocated out of a bump pointer allocator,
73 // we don't need to free them here.
74
75 // If the stream for the .secure_log_unique directive was created free it.
76 delete (raw_ostream*)SecureLog;
77}
78
79//===----------------------------------------------------------------------===//
80// Module Lifetime Management
81//===----------------------------------------------------------------------===//
82
Pedro Artigas7212ee42012-12-12 22:59:46 +000083void MCContext::reset() {
Pedro Artigase84b13f2012-12-06 22:12:44 +000084 UsedNames.clear();
85 Symbols.clear();
86 Allocator.Reset();
87 Instances.clear();
Manman Ren1e427202013-03-07 01:42:00 +000088 MCDwarfFilesCUMap.clear();
89 MCDwarfDirsCUMap.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000090 MCGenDwarfLabelEntries.clear();
91 DwarfDebugFlags = StringRef();
92 MCLineSections.clear();
93 MCLineSectionOrder.clear();
Pedro Artigas7ba2edc2013-02-20 00:10:29 +000094 DwarfCompileUnitID = 0;
95 MCLineTableSymbols.clear();
Pedro Artigase84b13f2012-12-06 22:12:44 +000096 CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0);
Michael J. Spencerf13f4422010-11-26 04:16:08 +000097
Chris Lattner20731122010-04-08 20:30:37 +000098 // If we have the MachO uniquing map, free it.
99 delete (MachOUniqueMapTy*)MachOUniquingMap;
Chris Lattner5418dd52010-04-08 21:26:26 +0000100 delete (ELFUniqueMapTy*)ELFUniquingMap;
Chris Lattner87cffa92010-05-07 17:17:41 +0000101 delete (COFFUniqueMapTy*)COFFUniquingMap;
Pedro Artigase84b13f2012-12-06 22:12:44 +0000102 MachOUniquingMap = 0;
103 ELFUniquingMap = 0;
104 COFFUniquingMap = 0;
Pedro Artigas7212ee42012-12-12 22:59:46 +0000105
106 NextUniqueID = 0;
107 AllowTemporaryLabels = true;
108 DwarfLocSeen = false;
109 GenDwarfForAssembly = false;
110 GenDwarfFileNumber = 0;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000111}
112
Chris Lattner20731122010-04-08 20:30:37 +0000113//===----------------------------------------------------------------------===//
114// Symbol Manipulation
115//===----------------------------------------------------------------------===//
116
Chris Lattner98970432010-03-30 18:10:53 +0000117MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattnerb973ea82010-03-10 01:29:27 +0000118 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000119
Chris Lattner13348762010-03-15 06:15:35 +0000120 // Do the lookup and get the entire StringMapEntry. We want access to the
121 // key if we are creating the entry.
122 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000123 MCSymbol *Sym = Entry.getValue();
124
125 if (Sym)
126 return Sym;
127
128 Sym = CreateSymbol(Name);
129 Entry.setValue(Sym);
130 return Sym;
131}
132
133MCSymbol *MCContext::CreateSymbol(StringRef Name) {
Daniel Dunbar4ee0d032011-03-28 22:49:15 +0000134 // Determine whether this is an assembler temporary or normal label, if used.
135 bool isTemporary = false;
136 if (AllowTemporaryLabels)
Bill Wendlingbc07a892013-06-18 07:20:20 +0000137 isTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000138
139 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
140 if (NameEntry->getValue()) {
141 assert(isTemporary && "Cannot rename non temporary symbols");
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000142 SmallString<128> NewName = Name;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000143 do {
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000144 NewName.resize(Name.size());
145 raw_svector_ostream(NewName) << NextUniqueID++;
146 NameEntry = &UsedNames.GetOrCreateValue(NewName);
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000147 } while (NameEntry->getValue());
148 }
149 NameEntry->setValue(true);
Chris Lattner3f5738d2009-06-24 04:31:49 +0000150
Chris Lattner13348762010-03-15 06:15:35 +0000151 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000152 // to the copy of the string that is embedded in the UsedNames entry.
153 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
154
155 return Result;
Chris Lattner3f5738d2009-06-24 04:31:49 +0000156}
157
Chris Lattner98970432010-03-30 18:10:53 +0000158MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner86dfd732009-10-19 22:49:00 +0000159 SmallString<128> NameSV;
David Blaikie8e5283a2013-12-03 18:18:28 +0000160 return GetOrCreateSymbol(Name.toStringRef(NameSV));
Chris Lattner86dfd732009-10-19 22:49:00 +0000161}
162
Chris Lattner073d8172010-03-14 08:23:30 +0000163MCSymbol *MCContext::CreateTempSymbol() {
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000164 SmallString<128> NameSV;
Benjamin Kramer2b6c96b2011-04-09 11:26:27 +0000165 raw_svector_ostream(NameSV)
Bill Wendlingbc07a892013-06-18 07:20:20 +0000166 << MAI->getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
Rafael Espindola5fe5f452010-12-01 20:46:11 +0000167 return CreateSymbol(NameSV);
Chris Lattner073d8172010-03-14 08:23:30 +0000168}
169
Kevin Enderby0510b482010-05-17 23:08:19 +0000170unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000171 MCLabel *&Label = Instances[LocalLabelVal];
172 if (!Label)
173 Label = new (*this) MCLabel(0);
174 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000175}
176
177unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +0000178 MCLabel *&Label = Instances[LocalLabelVal];
179 if (!Label)
180 Label = new (*this) MCLabel(0);
181 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +0000182}
183
184MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000185 return GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) +
Kevin Enderby0510b482010-05-17 23:08:19 +0000186 Twine(LocalLabelVal) +
187 "\2" +
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000188 Twine(NextInstance(LocalLabelVal)));
Kevin Enderby0510b482010-05-17 23:08:19 +0000189}
190MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
191 int bORf) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000192 return GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) +
Kevin Enderby0510b482010-05-17 23:08:19 +0000193 Twine(LocalLabelVal) +
194 "\2" +
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000195 Twine(GetInstance(LocalLabelVal) + bORf));
Kevin Enderby0510b482010-05-17 23:08:19 +0000196}
197
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000198MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000199 return Symbols.lookup(Name);
200}
Chris Lattner20731122010-04-08 20:30:37 +0000201
Roman Divacky0be33592012-09-18 17:10:37 +0000202MCSymbol *MCContext::LookupSymbol(const Twine &Name) const {
203 SmallString<128> NameSV;
204 Name.toVector(NameSV);
205 return LookupSymbol(NameSV.str());
206}
207
Chris Lattner20731122010-04-08 20:30:37 +0000208//===----------------------------------------------------------------------===//
209// Section Management
210//===----------------------------------------------------------------------===//
211
212const MCSectionMachO *MCContext::
213getMachOSection(StringRef Segment, StringRef Section,
214 unsigned TypeAndAttributes,
215 unsigned Reserved2, SectionKind Kind) {
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000216
Chris Lattner20731122010-04-08 20:30:37 +0000217 // We unique sections by their segment/section pair. The returned section
218 // may not have the same flags as the requested section, if so this should be
219 // diagnosed by the client as an error.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000220
Chris Lattner20731122010-04-08 20:30:37 +0000221 // Create the map if it doesn't already exist.
222 if (MachOUniquingMap == 0)
223 MachOUniquingMap = new MachOUniqueMapTy();
224 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000225
Chris Lattner20731122010-04-08 20:30:37 +0000226 // Form the name to look up.
227 SmallString<64> Name;
228 Name += Segment;
229 Name.push_back(',');
230 Name += Section;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000231
Chris Lattner20731122010-04-08 20:30:37 +0000232 // Do the lookup, if we have a hit, return it.
233 const MCSectionMachO *&Entry = Map[Name.str()];
234 if (Entry) return Entry;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000235
Chris Lattner20731122010-04-08 20:30:37 +0000236 // Otherwise, return a new section.
Chris Lattner5418dd52010-04-08 21:26:26 +0000237 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
238 Reserved2, Kind);
Chris Lattner20731122010-04-08 20:30:37 +0000239}
Chris Lattner5418dd52010-04-08 21:26:26 +0000240
Rafael Espindola36ef57d2010-11-10 19:05:07 +0000241const MCSectionELF *MCContext::
Chris Lattner5418dd52010-04-08 21:26:26 +0000242getELFSection(StringRef Section, unsigned Type, unsigned Flags,
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000243 SectionKind Kind) {
244 return getELFSection(Section, Type, Flags, Kind, 0, "");
245}
246
247const MCSectionELF *MCContext::
248getELFSection(StringRef Section, unsigned Type, unsigned Flags,
249 SectionKind Kind, unsigned EntrySize, StringRef Group) {
Chris Lattner5418dd52010-04-08 21:26:26 +0000250 if (ELFUniquingMap == 0)
251 ELFUniquingMap = new ELFUniqueMapTy();
252 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000253
Chris Lattner5418dd52010-04-08 21:26:26 +0000254 // Do the lookup, if we have a hit, return it.
David Blaikie15b25df2013-10-22 23:41:52 +0000255 std::pair<ELFUniqueMapTy::iterator, bool> Entry = Map.insert(
256 std::make_pair(SectionGroupPair(Section, Group), (MCSectionELF *)0));
257 if (!Entry.second) return Entry.first->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000258
Jan Wen Voungefbdbe52010-09-30 05:59:22 +0000259 // Possibly refine the entry size first.
260 if (!EntrySize) {
261 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
262 }
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000263
264 MCSymbol *GroupSym = NULL;
265 if (!Group.empty())
266 GroupSym = GetOrCreateSymbol(Group);
267
David Blaikie15b25df2013-10-22 23:41:52 +0000268 MCSectionELF *Result = new (*this) MCSectionELF(
269 Entry.first->first.first, Type, Flags, Kind, EntrySize, GroupSym);
270 Entry.first->second = Result;
Chris Lattner5418dd52010-04-08 21:26:26 +0000271 return Result;
272}
273
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000274const MCSectionELF *MCContext::CreateELFGroupSection() {
275 MCSectionELF *Result =
Rafael Espindolaaea49582011-01-23 04:28:49 +0000276 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000277 SectionKind::getReadOnly(), 4, NULL);
278 return Result;
279}
280
Rafael Espindola60ec3832013-11-19 19:52:52 +0000281const MCSectionCOFF *
282MCContext::getCOFFSection(StringRef Section, unsigned Characteristics,
283 SectionKind Kind, StringRef COMDATSymName,
284 int Selection, const MCSectionCOFF *Assoc) {
Chris Lattner87cffa92010-05-07 17:17:41 +0000285 if (COFFUniquingMap == 0)
286 COFFUniquingMap = new COFFUniqueMapTy();
287 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000288
Chris Lattner87cffa92010-05-07 17:17:41 +0000289 // Do the lookup, if we have a hit, return it.
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000290
Rafael Espindola60ec3832013-11-19 19:52:52 +0000291 SectionGroupPair P(Section, COMDATSymName);
292 std::pair<COFFUniqueMapTy::iterator, bool> Entry =
293 Map.insert(std::make_pair(P, (MCSectionCOFF *)0));
294 COFFUniqueMapTy::iterator Iter = Entry.first;
295 if (!Entry.second)
296 return Iter->second;
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000297
Rafael Espindola60ec3832013-11-19 19:52:52 +0000298 const MCSymbol *COMDATSymbol = NULL;
299 if (!COMDATSymName.empty())
300 COMDATSymbol = GetOrCreateSymbol(COMDATSymName);
301
302 MCSectionCOFF *Result =
303 new (*this) MCSectionCOFF(Iter->first.first, Characteristics,
304 COMDATSymbol, Selection, Assoc, Kind);
305
306 Iter->second = Result;
Chris Lattner87cffa92010-05-07 17:17:41 +0000307 return Result;
308}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000309
Rafael Espindola60ec3832013-11-19 19:52:52 +0000310const MCSectionCOFF *
311MCContext::getCOFFSection(StringRef Section, unsigned Characteristics,
312 SectionKind Kind) {
313 return getCOFFSection(Section, Characteristics, Kind, "", 0);
314}
315
Nico Riecka37acf72013-07-06 12:13:10 +0000316const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
317 if (COFFUniquingMap == 0)
318 COFFUniquingMap = new COFFUniqueMapTy();
319 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
320
Rafael Espindola60ec3832013-11-19 19:52:52 +0000321 SectionGroupPair P(Section, "");
322 COFFUniqueMapTy::iterator Iter = Map.find(P);
323 if (Iter == Map.end())
324 return 0;
325 return Iter->second;
Nico Riecka37acf72013-07-06 12:13:10 +0000326}
327
Kevin Enderbye5930f12010-07-28 20:55:35 +0000328//===----------------------------------------------------------------------===//
329// Dwarf Management
330//===----------------------------------------------------------------------===//
331
332/// GetDwarfFile - takes a file name an number to place in the dwarf file and
333/// directory tables. If the file number has already been allocated it is an
334/// error and zero is returned and the client reports the error, else the
335/// allocated file number is returned. The file numbers may be in any order.
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000336unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName,
Manman Ren1e427202013-03-07 01:42:00 +0000337 unsigned FileNumber, unsigned CUID) {
Kevin Enderbye5930f12010-07-28 20:55:35 +0000338 // TODO: a FileNumber of zero says to use the next available file number.
339 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
340 // to not be less than one. This needs to be change to be not less than zero.
341
Manman Ren5ce24ff2013-03-12 20:17:00 +0000342 SmallVectorImpl<MCDwarfFile *>& MCDwarfFiles = MCDwarfFilesCUMap[CUID];
343 SmallVectorImpl<StringRef>& MCDwarfDirs = MCDwarfDirsCUMap[CUID];
Kevin Enderbye5930f12010-07-28 20:55:35 +0000344 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
345 if (FileNumber >= MCDwarfFiles.size()) {
346 MCDwarfFiles.resize(FileNumber + 1);
347 } else {
348 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
349 if (ExistingFile)
350 // It is an error to use see the same number more than once.
351 return 0;
352 }
353
354 // Get the new MCDwarfFile slot for this FileNumber.
355 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
356
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000357 if (Directory.empty()) {
358 // Separate the directory part from the basename of the FileName.
NAKAMURA Takumi30396ba2012-07-03 03:59:29 +0000359 StringRef tFileName = sys::path::filename(FileName);
360 if (!tFileName.empty()) {
361 Directory = sys::path::parent_path(FileName);
362 if (!Directory.empty())
NAKAMURA Takumi09e35102012-07-03 06:01:27 +0000363 FileName = tFileName;
Kevin Enderby82ed3be2011-11-01 23:39:05 +0000364 }
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000365 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000366
367 // Find or make a entry in the MCDwarfDirs vector for this Directory.
Kevin Enderbye5930f12010-07-28 20:55:35 +0000368 // Capture directory name.
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000369 unsigned DirIndex;
370 if (Directory.empty()) {
371 // For FileNames with no directories a DirIndex of 0 is used.
372 DirIndex = 0;
Kevin Enderbye5930f12010-07-28 20:55:35 +0000373 } else {
Nick Lewyckyf895efa2011-10-12 20:20:48 +0000374 DirIndex = 0;
375 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
Benjamin Kramerbccfec62010-07-29 13:53:19 +0000376 if (Directory == MCDwarfDirs[DirIndex])
Kevin Enderbyd94dacf2010-08-31 22:55:11 +0000377 break;
Kevin Enderbye5930f12010-07-28 20:55:35 +0000378 }
379 if (DirIndex >= MCDwarfDirs.size()) {
Benjamin Kramerbccfec62010-07-29 13:53:19 +0000380 char *Buf = static_cast<char *>(Allocate(Directory.size()));
381 memcpy(Buf, Directory.data(), Directory.size());
382 MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
Kevin Enderbye5930f12010-07-28 20:55:35 +0000383 }
Kevin Enderby7221b762010-08-09 22:52:14 +0000384 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
385 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000386 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
387 // are stored at MCDwarfFiles[FileNumber].Name .
Kevin Enderby7221b762010-08-09 22:52:14 +0000388 DirIndex++;
Kevin Enderbye5930f12010-07-28 20:55:35 +0000389 }
Michael J. Spencerf13f4422010-11-26 04:16:08 +0000390
Kevin Enderbye5930f12010-07-28 20:55:35 +0000391 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
392 // vector.
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000393 char *Buf = static_cast<char *>(Allocate(FileName.size()));
394 memcpy(Buf, FileName.data(), FileName.size());
395 File = new (*this) MCDwarfFile(StringRef(Buf, FileName.size()), DirIndex);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000396
397 // return the allocated FileNumber.
398 return FileNumber;
399}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000400
Kevin Enderbya68d0042010-10-04 20:17:24 +0000401/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000402/// currently is assigned and false otherwise.
Manman Ren1e427202013-03-07 01:42:00 +0000403bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
Manman Ren5ce24ff2013-03-12 20:17:00 +0000404 SmallVectorImpl<MCDwarfFile *>& MCDwarfFiles = MCDwarfFilesCUMap[CUID];
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000405 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
406 return false;
407
Kevin Enderbya68d0042010-10-04 20:17:24 +0000408 return MCDwarfFiles[FileNumber] != 0;
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000409}
Jim Grosbachb18b4092012-01-26 23:20:11 +0000410
411void MCContext::FatalError(SMLoc Loc, const Twine &Msg) {
412 // If we have a source manager and a location, use it. Otherwise just
413 // use the generic report_fatal_error().
414 if (!SrcMgr || Loc == SMLoc())
415 report_fatal_error(Msg);
416
417 // Use the source manager to print the message.
418 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
419
420 // If we reached here, we are failing ungracefully. Run the interrupt handlers
421 // to make sure any special cleanups get done, in particular that we remove
422 // files registered with RemoveFileOnSignal.
423 sys::RunInterruptHandlers();
424 exit(1);
425}