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/Signals.h"
25 #include "llvm/Support/SourceMgr.h"
28 typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
29 typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
30 typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
33 MCContext::MCContext(const MCAsmInfo &mai, const MCRegisterInfo &mri,
34 const MCObjectFileInfo *mofi, const SourceMgr *mgr) :
35 SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi),
36 Allocator(), Symbols(Allocator), UsedNames(Allocator),
38 CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0),
39 AllowTemporaryLabels(true) {
44 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
46 SecureLogUsed = false;
49 MCContext::~MCContext() {
50 // NOTE: The symbols are all allocated out of a bump pointer allocator,
51 // we don't need to free them here.
53 // If the stream for the .secure_log_unique directive was created free it.
54 delete (raw_ostream*)SecureLog;
57 //===----------------------------------------------------------------------===//
58 // Module Lifetime Management
59 //===----------------------------------------------------------------------===//
61 void MCContext::doInitialization() {
63 AllowTemporaryLabels = true;
65 GenDwarfForAssembly = false;
66 GenDwarfFileNumber = 0;
69 void MCContext::doFinalization() {
76 MCGenDwarfLabelEntries.clear();
77 DwarfDebugFlags = StringRef();
78 MCLineSections.clear();
79 MCLineSectionOrder.clear();
80 CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0);
82 // If we have the MachO uniquing map, free it.
83 delete (MachOUniqueMapTy*)MachOUniquingMap;
84 delete (ELFUniqueMapTy*)ELFUniquingMap;
85 delete (COFFUniqueMapTy*)COFFUniquingMap;
91 //===----------------------------------------------------------------------===//
92 // Symbol Manipulation
93 //===----------------------------------------------------------------------===//
95 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
96 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
98 // Do the lookup and get the entire StringMapEntry. We want access to the
99 // key if we are creating the entry.
100 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
101 MCSymbol *Sym = Entry.getValue();
106 Sym = CreateSymbol(Name);
111 MCSymbol *MCContext::CreateSymbol(StringRef Name) {
112 // Determine whether this is an assembler temporary or normal label, if used.
113 bool isTemporary = false;
114 if (AllowTemporaryLabels)
115 isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
117 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
118 if (NameEntry->getValue()) {
119 assert(isTemporary && "Cannot rename non temporary symbols");
120 SmallString<128> NewName = Name;
122 NewName.resize(Name.size());
123 raw_svector_ostream(NewName) << NextUniqueID++;
124 NameEntry = &UsedNames.GetOrCreateValue(NewName);
125 } while (NameEntry->getValue());
127 NameEntry->setValue(true);
129 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
130 // to the copy of the string that is embedded in the UsedNames entry.
131 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
136 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
137 SmallString<128> NameSV;
138 Name.toVector(NameSV);
139 return GetOrCreateSymbol(NameSV.str());
142 MCSymbol *MCContext::CreateTempSymbol() {
143 SmallString<128> NameSV;
144 raw_svector_ostream(NameSV)
145 << MAI.getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
146 return CreateSymbol(NameSV);
149 unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
150 MCLabel *&Label = Instances[LocalLabelVal];
152 Label = new (*this) MCLabel(0);
153 return Label->incInstance();
156 unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
157 MCLabel *&Label = Instances[LocalLabelVal];
159 Label = new (*this) MCLabel(0);
160 return Label->getInstance();
163 MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
164 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
165 Twine(LocalLabelVal) +
167 Twine(NextInstance(LocalLabelVal)));
169 MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
171 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
172 Twine(LocalLabelVal) +
174 Twine(GetInstance(LocalLabelVal) + bORf));
177 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
178 return Symbols.lookup(Name);
181 MCSymbol *MCContext::LookupSymbol(const Twine &Name) const {
182 SmallString<128> NameSV;
183 Name.toVector(NameSV);
184 return LookupSymbol(NameSV.str());
187 //===----------------------------------------------------------------------===//
188 // Section Management
189 //===----------------------------------------------------------------------===//
191 const MCSectionMachO *MCContext::
192 getMachOSection(StringRef Segment, StringRef Section,
193 unsigned TypeAndAttributes,
194 unsigned Reserved2, SectionKind Kind) {
196 // We unique sections by their segment/section pair. The returned section
197 // may not have the same flags as the requested section, if so this should be
198 // diagnosed by the client as an error.
200 // Create the map if it doesn't already exist.
201 if (MachOUniquingMap == 0)
202 MachOUniquingMap = new MachOUniqueMapTy();
203 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
205 // Form the name to look up.
206 SmallString<64> Name;
211 // Do the lookup, if we have a hit, return it.
212 const MCSectionMachO *&Entry = Map[Name.str()];
213 if (Entry) return Entry;
215 // Otherwise, return a new section.
216 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
220 const MCSectionELF *MCContext::
221 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
223 return getELFSection(Section, Type, Flags, Kind, 0, "");
226 const MCSectionELF *MCContext::
227 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
228 SectionKind Kind, unsigned EntrySize, StringRef Group) {
229 if (ELFUniquingMap == 0)
230 ELFUniquingMap = new ELFUniqueMapTy();
231 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
233 // Do the lookup, if we have a hit, return it.
234 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
235 if (Entry.getValue()) return Entry.getValue();
237 // Possibly refine the entry size first.
239 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
242 MCSymbol *GroupSym = NULL;
244 GroupSym = GetOrCreateSymbol(Group);
246 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
247 Kind, EntrySize, GroupSym);
248 Entry.setValue(Result);
252 const MCSectionELF *MCContext::CreateELFGroupSection() {
253 MCSectionELF *Result =
254 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
255 SectionKind::getReadOnly(), 4, NULL);
259 const MCSection *MCContext::getCOFFSection(StringRef Section,
260 unsigned Characteristics,
263 if (COFFUniquingMap == 0)
264 COFFUniquingMap = new COFFUniqueMapTy();
265 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
267 // Do the lookup, if we have a hit, return it.
268 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
269 if (Entry.getValue()) return Entry.getValue();
271 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
275 Entry.setValue(Result);
279 //===----------------------------------------------------------------------===//
281 //===----------------------------------------------------------------------===//
283 /// GetDwarfFile - takes a file name an number to place in the dwarf file and
284 /// directory tables. If the file number has already been allocated it is an
285 /// error and zero is returned and the client reports the error, else the
286 /// allocated file number is returned. The file numbers may be in any order.
287 unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName,
288 unsigned FileNumber) {
289 // TODO: a FileNumber of zero says to use the next available file number.
290 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
291 // to not be less than one. This needs to be change to be not less than zero.
293 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
294 if (FileNumber >= MCDwarfFiles.size()) {
295 MCDwarfFiles.resize(FileNumber + 1);
297 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
299 // It is an error to use see the same number more than once.
303 // Get the new MCDwarfFile slot for this FileNumber.
304 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
306 if (Directory.empty()) {
307 // Separate the directory part from the basename of the FileName.
308 StringRef tFileName = sys::path::filename(FileName);
309 if (!tFileName.empty()) {
310 Directory = sys::path::parent_path(FileName);
311 if (!Directory.empty())
312 FileName = tFileName;
316 // Find or make a entry in the MCDwarfDirs vector for this Directory.
317 // Capture directory name.
319 if (Directory.empty()) {
320 // For FileNames with no directories a DirIndex of 0 is used.
324 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
325 if (Directory == MCDwarfDirs[DirIndex])
328 if (DirIndex >= MCDwarfDirs.size()) {
329 char *Buf = static_cast<char *>(Allocate(Directory.size()));
330 memcpy(Buf, Directory.data(), Directory.size());
331 MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
333 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
334 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
335 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
336 // are stored at MCDwarfFiles[FileNumber].Name .
340 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
342 char *Buf = static_cast<char *>(Allocate(FileName.size()));
343 memcpy(Buf, FileName.data(), FileName.size());
344 File = new (*this) MCDwarfFile(StringRef(Buf, FileName.size()), DirIndex);
346 // return the allocated FileNumber.
350 /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
351 /// currently is assigned and false otherwise.
352 bool MCContext::isValidDwarfFileNumber(unsigned FileNumber) {
353 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
356 return MCDwarfFiles[FileNumber] != 0;
359 void MCContext::FatalError(SMLoc Loc, const Twine &Msg) {
360 // If we have a source manager and a location, use it. Otherwise just
361 // use the generic report_fatal_error().
362 if (!SrcMgr || Loc == SMLoc())
363 report_fatal_error(Msg);
365 // Use the source manager to print the message.
366 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
368 // If we reached here, we are failing ungracefully. Run the interrupt handlers
369 // to make sure any special cleanups get done, in particular that we remove
370 // files registered with RemoveFileOnSignal.
371 sys::RunInterruptHandlers();