1 //===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCContext.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/Twine.h"
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"
22 #include "llvm/Support/ELF.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/Signals.h"
27 #include "llvm/Support/SourceMgr.h"
32 typedef std::pair<std::string, std::string> SectionGroupPair;
34 typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
35 typedef std::map<SectionGroupPair, const MCSectionELF *> ELFUniqueMapTy;
36 typedef std::map<SectionGroupPair, const MCSectionCOFF *> COFFUniqueMapTy;
38 MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
39 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
41 SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi),
42 Allocator(), Symbols(Allocator), UsedNames(Allocator),
44 CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0),
45 DwarfLocSeen(false), GenDwarfForAssembly(false), GenDwarfFileNumber(0),
46 AllowTemporaryLabels(true), DwarfCompileUnitID(0), AutoReset(DoAutoReset) {
48 error_code EC = llvm::sys::fs::current_path(CompilationDir);
50 CompilationDir.clear();
56 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
58 SecureLogUsed = false;
60 if (SrcMgr && SrcMgr->getNumBuffers() > 0)
61 MainFileName = SrcMgr->getMemoryBuffer(0)->getBufferIdentifier();
66 MCContext::~MCContext() {
71 // NOTE: The symbols are all allocated out of a bump pointer allocator,
72 // we don't need to free them here.
74 // If the stream for the .secure_log_unique directive was created free it.
75 delete (raw_ostream*)SecureLog;
78 //===----------------------------------------------------------------------===//
79 // Module Lifetime Management
80 //===----------------------------------------------------------------------===//
82 void MCContext::reset() {
87 MCDwarfLineTablesCUMap.clear();
88 MCGenDwarfLabelEntries.clear();
89 DwarfDebugFlags = StringRef();
90 DwarfCompileUnitID = 0;
91 CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0);
93 // If we have the MachO uniquing map, free it.
94 delete (MachOUniqueMapTy*)MachOUniquingMap;
95 delete (ELFUniqueMapTy*)ELFUniquingMap;
96 delete (COFFUniqueMapTy*)COFFUniquingMap;
102 AllowTemporaryLabels = true;
103 DwarfLocSeen = false;
104 GenDwarfForAssembly = false;
105 GenDwarfFileNumber = 0;
108 //===----------------------------------------------------------------------===//
109 // Symbol Manipulation
110 //===----------------------------------------------------------------------===//
112 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
113 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
115 // Do the lookup and get the entire StringMapEntry. We want access to the
116 // key if we are creating the entry.
117 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
118 MCSymbol *Sym = Entry.getValue();
123 Sym = CreateSymbol(Name);
128 MCSymbol *MCContext::CreateSymbol(StringRef Name) {
129 // Determine whether this is an assembler temporary or normal label, if used.
130 bool isTemporary = false;
131 if (AllowTemporaryLabels)
132 isTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
134 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
135 if (NameEntry->getValue()) {
136 assert(isTemporary && "Cannot rename non-temporary symbols");
137 SmallString<128> NewName = Name;
139 NewName.resize(Name.size());
140 raw_svector_ostream(NewName) << NextUniqueID++;
141 NameEntry = &UsedNames.GetOrCreateValue(NewName);
142 } while (NameEntry->getValue());
144 NameEntry->setValue(true);
146 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
147 // to the copy of the string that is embedded in the UsedNames entry.
148 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
153 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
154 SmallString<128> NameSV;
155 return GetOrCreateSymbol(Name.toStringRef(NameSV));
158 MCSymbol *MCContext::CreateTempSymbol() {
159 SmallString<128> NameSV;
160 raw_svector_ostream(NameSV)
161 << MAI->getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
162 return CreateSymbol(NameSV);
165 unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
166 MCLabel *&Label = Instances[LocalLabelVal];
168 Label = new (*this) MCLabel(0);
169 return Label->incInstance();
172 unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
173 MCLabel *&Label = Instances[LocalLabelVal];
175 Label = new (*this) MCLabel(0);
176 return Label->getInstance();
179 MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
181 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
183 Sym = CreateTempSymbol();
187 MCSymbol *MCContext::CreateDirectionalLocalSymbol(unsigned LocalLabelVal) {
188 unsigned Instance = NextInstance(LocalLabelVal);
189 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
192 MCSymbol *MCContext::GetDirectionalLocalSymbol(unsigned LocalLabelVal,
194 unsigned Instance = GetInstance(LocalLabelVal);
197 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
200 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
201 return Symbols.lookup(Name);
204 MCSymbol *MCContext::LookupSymbol(const Twine &Name) const {
205 SmallString<128> NameSV;
206 Name.toVector(NameSV);
207 return LookupSymbol(NameSV.str());
210 //===----------------------------------------------------------------------===//
211 // Section Management
212 //===----------------------------------------------------------------------===//
214 const MCSectionMachO *MCContext::
215 getMachOSection(StringRef Segment, StringRef Section,
216 unsigned TypeAndAttributes,
217 unsigned Reserved2, SectionKind Kind) {
219 // 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.
223 // Create the map if it doesn't already exist.
224 if (MachOUniquingMap == 0)
225 MachOUniquingMap = new MachOUniqueMapTy();
226 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
228 // Form the name to look up.
229 SmallString<64> Name;
234 // Do the lookup, if we have a hit, return it.
235 const MCSectionMachO *&Entry = Map[Name.str()];
236 if (Entry) return Entry;
238 // Otherwise, return a new section.
239 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
243 const MCSectionELF *MCContext::
244 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
246 return getELFSection(Section, Type, Flags, Kind, 0, "");
249 const MCSectionELF *MCContext::
250 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
251 SectionKind Kind, unsigned EntrySize, StringRef Group) {
252 if (ELFUniquingMap == 0)
253 ELFUniquingMap = new ELFUniqueMapTy();
254 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
256 // Do the lookup, if we have a hit, return it.
257 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;
261 // Possibly refine the entry size first.
263 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
266 MCSymbol *GroupSym = NULL;
268 GroupSym = GetOrCreateSymbol(Group);
270 MCSectionELF *Result = new (*this) MCSectionELF(
271 Entry.first->first.first, Type, Flags, Kind, EntrySize, GroupSym);
272 Entry.first->second = Result;
276 const MCSectionELF *MCContext::CreateELFGroupSection() {
277 MCSectionELF *Result =
278 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
279 SectionKind::getReadOnly(), 4, NULL);
283 const MCSectionCOFF *
284 MCContext::getCOFFSection(StringRef Section, unsigned Characteristics,
285 SectionKind Kind, StringRef COMDATSymName,
286 int Selection, const MCSectionCOFF *Assoc) {
287 if (COFFUniquingMap == 0)
288 COFFUniquingMap = new COFFUniqueMapTy();
289 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
291 // Do the lookup, if we have a hit, return it.
293 SectionGroupPair P(Section, COMDATSymName);
294 std::pair<COFFUniqueMapTy::iterator, bool> Entry =
295 Map.insert(std::make_pair(P, (MCSectionCOFF *)0));
296 COFFUniqueMapTy::iterator Iter = Entry.first;
300 const MCSymbol *COMDATSymbol = NULL;
301 if (!COMDATSymName.empty())
302 COMDATSymbol = GetOrCreateSymbol(COMDATSymName);
304 MCSectionCOFF *Result =
305 new (*this) MCSectionCOFF(Iter->first.first, Characteristics,
306 COMDATSymbol, Selection, Assoc, Kind);
308 Iter->second = Result;
312 const MCSectionCOFF *
313 MCContext::getCOFFSection(StringRef Section, unsigned Characteristics,
315 return getCOFFSection(Section, Characteristics, Kind, "", 0);
318 const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
319 if (COFFUniquingMap == 0)
320 COFFUniquingMap = new COFFUniqueMapTy();
321 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
323 SectionGroupPair P(Section, "");
324 COFFUniqueMapTy::iterator Iter = Map.find(P);
325 if (Iter == Map.end())
330 //===----------------------------------------------------------------------===//
332 //===----------------------------------------------------------------------===//
334 /// GetDwarfFile - takes a file name an number to place in the dwarf file and
335 /// directory tables. If the file number has already been allocated it is an
336 /// error and zero is returned and the client reports the error, else the
337 /// allocated file number is returned. The file numbers may be in any order.
338 unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName,
339 unsigned FileNumber, unsigned CUID) {
340 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
341 return Table.getFile(Directory, FileName, FileNumber);
344 /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
345 /// currently is assigned and false otherwise.
346 bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
347 const SmallVectorImpl<MCDwarfFile>& MCDwarfFiles = getMCDwarfFiles(CUID);
348 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
351 return !MCDwarfFiles[FileNumber].Name.empty();
354 void MCContext::FatalError(SMLoc Loc, const Twine &Msg) {
355 // If we have a source manager and a location, use it. Otherwise just
356 // use the generic report_fatal_error().
357 if (!SrcMgr || Loc == SMLoc())
358 report_fatal_error(Msg);
360 // Use the source manager to print the message.
361 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
363 // If we reached here, we are failing ungracefully. Run the interrupt handlers
364 // to make sure any special cleanups get done, in particular that we remove
365 // files registered with RemoveFileOnSignal.
366 sys::RunInterruptHandlers();