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();
64 MCContext::~MCContext() {
69 // NOTE: The symbols are all allocated out of a bump pointer allocator,
70 // we don't need to free them here.
72 // If the stream for the .secure_log_unique directive was created free it.
73 delete (raw_ostream*)SecureLog;
76 //===----------------------------------------------------------------------===//
77 // Module Lifetime Management
78 //===----------------------------------------------------------------------===//
80 void MCContext::reset() {
85 MCDwarfLineTablesCUMap.clear();
86 MCGenDwarfLabelEntries.clear();
87 DwarfDebugFlags = StringRef();
88 DwarfCompileUnitID = 0;
89 CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0);
91 // If we have the MachO uniquing map, free it.
92 delete (MachOUniqueMapTy*)MachOUniquingMap;
93 delete (ELFUniqueMapTy*)ELFUniquingMap;
94 delete (COFFUniqueMapTy*)COFFUniquingMap;
100 AllowTemporaryLabels = true;
101 DwarfLocSeen = false;
102 GenDwarfForAssembly = false;
103 GenDwarfFileNumber = 0;
106 //===----------------------------------------------------------------------===//
107 // Symbol Manipulation
108 //===----------------------------------------------------------------------===//
110 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
111 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
113 // Do the lookup and get the entire StringMapEntry. We want access to the
114 // key if we are creating the entry.
115 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
116 MCSymbol *Sym = Entry.getValue();
121 Sym = CreateSymbol(Name);
126 MCSymbol *MCContext::CreateSymbol(StringRef Name) {
127 // Determine whether this is an assembler temporary or normal label, if used.
128 bool isTemporary = false;
129 if (AllowTemporaryLabels)
130 isTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
132 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
133 if (NameEntry->getValue()) {
134 assert(isTemporary && "Cannot rename non-temporary symbols");
135 SmallString<128> NewName = Name;
137 NewName.resize(Name.size());
138 raw_svector_ostream(NewName) << NextUniqueID++;
139 NameEntry = &UsedNames.GetOrCreateValue(NewName);
140 } while (NameEntry->getValue());
142 NameEntry->setValue(true);
144 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
145 // to the copy of the string that is embedded in the UsedNames entry.
146 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
151 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
152 SmallString<128> NameSV;
153 return GetOrCreateSymbol(Name.toStringRef(NameSV));
156 MCSymbol *MCContext::CreateTempSymbol() {
157 SmallString<128> NameSV;
158 raw_svector_ostream(NameSV)
159 << MAI->getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
160 return CreateSymbol(NameSV);
163 unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
164 MCLabel *&Label = Instances[LocalLabelVal];
166 Label = new (*this) MCLabel(0);
167 return Label->incInstance();
170 unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
171 MCLabel *&Label = Instances[LocalLabelVal];
173 Label = new (*this) MCLabel(0);
174 return Label->getInstance();
177 MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
179 MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
181 Sym = CreateTempSymbol();
185 MCSymbol *MCContext::CreateDirectionalLocalSymbol(unsigned LocalLabelVal) {
186 unsigned Instance = NextInstance(LocalLabelVal);
187 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
190 MCSymbol *MCContext::GetDirectionalLocalSymbol(unsigned LocalLabelVal,
192 unsigned Instance = GetInstance(LocalLabelVal);
195 return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
198 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
199 return Symbols.lookup(Name);
202 MCSymbol *MCContext::LookupSymbol(const Twine &Name) const {
203 SmallString<128> NameSV;
204 Name.toVector(NameSV);
205 return LookupSymbol(NameSV.str());
208 //===----------------------------------------------------------------------===//
209 // Section Management
210 //===----------------------------------------------------------------------===//
212 const MCSectionMachO *MCContext::
213 getMachOSection(StringRef Segment, StringRef Section,
214 unsigned TypeAndAttributes,
215 unsigned Reserved2, SectionKind Kind) {
217 // 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.
221 // Create the map if it doesn't already exist.
222 if (MachOUniquingMap == 0)
223 MachOUniquingMap = new MachOUniqueMapTy();
224 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
226 // Form the name to look up.
227 SmallString<64> Name;
232 // Do the lookup, if we have a hit, return it.
233 const MCSectionMachO *&Entry = Map[Name.str()];
234 if (Entry) return Entry;
236 // Otherwise, return a new section.
237 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
241 const MCSectionELF *MCContext::
242 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
244 return getELFSection(Section, Type, Flags, Kind, 0, "");
247 const MCSectionELF *MCContext::
248 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
249 SectionKind Kind, unsigned EntrySize, StringRef Group) {
250 if (ELFUniquingMap == 0)
251 ELFUniquingMap = new ELFUniqueMapTy();
252 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
254 // Do the lookup, if we have a hit, return it.
255 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;
259 // Possibly refine the entry size first.
261 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
264 MCSymbol *GroupSym = NULL;
266 GroupSym = GetOrCreateSymbol(Group);
268 MCSectionELF *Result = new (*this) MCSectionELF(
269 Entry.first->first.first, Type, Flags, Kind, EntrySize, GroupSym);
270 Entry.first->second = Result;
274 const MCSectionELF *MCContext::CreateELFGroupSection() {
275 MCSectionELF *Result =
276 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
277 SectionKind::getReadOnly(), 4, NULL);
281 const MCSectionCOFF *
282 MCContext::getCOFFSection(StringRef Section, unsigned Characteristics,
283 SectionKind Kind, StringRef COMDATSymName,
284 int Selection, const MCSectionCOFF *Assoc) {
285 if (COFFUniquingMap == 0)
286 COFFUniquingMap = new COFFUniqueMapTy();
287 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
289 // Do the lookup, if we have a hit, return it.
291 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;
298 const MCSymbol *COMDATSymbol = NULL;
299 if (!COMDATSymName.empty())
300 COMDATSymbol = GetOrCreateSymbol(COMDATSymName);
302 MCSectionCOFF *Result =
303 new (*this) MCSectionCOFF(Iter->first.first, Characteristics,
304 COMDATSymbol, Selection, Assoc, Kind);
306 Iter->second = Result;
310 const MCSectionCOFF *
311 MCContext::getCOFFSection(StringRef Section, unsigned Characteristics,
313 return getCOFFSection(Section, Characteristics, Kind, "", 0);
316 const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
317 if (COFFUniquingMap == 0)
318 COFFUniquingMap = new COFFUniqueMapTy();
319 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
321 SectionGroupPair P(Section, "");
322 COFFUniqueMapTy::iterator Iter = Map.find(P);
323 if (Iter == Map.end())
328 //===----------------------------------------------------------------------===//
330 //===----------------------------------------------------------------------===//
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.
336 unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName,
337 unsigned FileNumber, unsigned CUID) {
338 MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
339 return Table.getFile(Directory, FileName, FileNumber);
342 /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
343 /// currently is assigned and false otherwise.
344 bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
345 const SmallVectorImpl<MCDwarfFile>& MCDwarfFiles = getMCDwarfFiles(CUID);
346 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
349 return !MCDwarfFiles[FileNumber].Name.empty();
352 void MCContext::FatalError(SMLoc Loc, const Twine &Msg) {
353 // If we have a source manager and a location, use it. Otherwise just
354 // use the generic report_fatal_error().
355 if (!SrcMgr || Loc == SMLoc())
356 report_fatal_error(Msg, false);
358 // Use the source manager to print the message.
359 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
361 // If we reached here, we are failing ungracefully. Run the interrupt handlers
362 // to make sure any special cleanups get done, in particular that we remove
363 // files registered with RemoveFileOnSignal.
364 sys::RunInterruptHandlers();