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/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCObjectFileInfo.h"
13 #include "llvm/MC/MCRegisterInfo.h"
14 #include "llvm/MC/MCSectionMachO.h"
15 #include "llvm/MC/MCSectionELF.h"
16 #include "llvm/MC/MCSectionCOFF.h"
17 #include "llvm/MC/MCSymbol.h"
18 #include "llvm/MC/MCLabel.h"
19 #include "llvm/MC/MCDwarf.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Support/ELF.h"
25 typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
26 typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
27 typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
30 MCContext::MCContext(const MCAsmInfo &mai, const MCRegisterInfo &mri,
31 const MCObjectFileInfo *mofi) :
32 MAI(mai), MRI(mri), MOFI(mofi),
33 Allocator(), Symbols(Allocator), UsedNames(Allocator),
35 CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0),
36 AllowTemporaryLabels(true) {
41 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
43 SecureLogUsed = false;
46 GenDwarfForAssembly = false;
47 GenDwarfFileNumber = 0;
50 MCContext::~MCContext() {
51 // NOTE: The symbols are all allocated out of a bump pointer allocator,
52 // we don't need to free them here.
54 // If we have the MachO uniquing map, free it.
55 delete (MachOUniqueMapTy*)MachOUniquingMap;
56 delete (ELFUniqueMapTy*)ELFUniquingMap;
57 delete (COFFUniqueMapTy*)COFFUniquingMap;
59 // If the stream for the .secure_log_unique directive was created free it.
60 delete (raw_ostream*)SecureLog;
63 //===----------------------------------------------------------------------===//
64 // Symbol Manipulation
65 //===----------------------------------------------------------------------===//
67 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
68 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
70 // Do the lookup and get the entire StringMapEntry. We want access to the
71 // key if we are creating the entry.
72 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
73 MCSymbol *Sym = Entry.getValue();
78 Sym = CreateSymbol(Name);
83 MCSymbol *MCContext::CreateSymbol(StringRef Name) {
84 // Determine whether this is an assembler temporary or normal label, if used.
85 bool isTemporary = false;
86 if (AllowTemporaryLabels)
87 isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
89 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
90 if (NameEntry->getValue()) {
91 assert(isTemporary && "Cannot rename non temporary symbols");
92 SmallString<128> NewName = Name;
94 NewName.resize(Name.size());
95 raw_svector_ostream(NewName) << NextUniqueID++;
96 NameEntry = &UsedNames.GetOrCreateValue(NewName);
97 } while (NameEntry->getValue());
99 NameEntry->setValue(true);
101 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
102 // to the copy of the string that is embedded in the UsedNames entry.
103 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
108 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
109 SmallString<128> NameSV;
110 Name.toVector(NameSV);
111 return GetOrCreateSymbol(NameSV.str());
114 MCSymbol *MCContext::CreateTempSymbol() {
115 SmallString<128> NameSV;
116 raw_svector_ostream(NameSV)
117 << MAI.getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
118 return CreateSymbol(NameSV);
121 unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
122 MCLabel *&Label = Instances[LocalLabelVal];
124 Label = new (*this) MCLabel(0);
125 return Label->incInstance();
128 unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
129 MCLabel *&Label = Instances[LocalLabelVal];
131 Label = new (*this) MCLabel(0);
132 return Label->getInstance();
135 MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
136 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
137 Twine(LocalLabelVal) +
139 Twine(NextInstance(LocalLabelVal)));
141 MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
143 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
144 Twine(LocalLabelVal) +
146 Twine(GetInstance(LocalLabelVal) + bORf));
149 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
150 return Symbols.lookup(Name);
153 //===----------------------------------------------------------------------===//
154 // Section Management
155 //===----------------------------------------------------------------------===//
157 const MCSectionMachO *MCContext::
158 getMachOSection(StringRef Segment, StringRef Section,
159 unsigned TypeAndAttributes,
160 unsigned Reserved2, SectionKind Kind) {
162 // We unique sections by their segment/section pair. The returned section
163 // may not have the same flags as the requested section, if so this should be
164 // diagnosed by the client as an error.
166 // Create the map if it doesn't already exist.
167 if (MachOUniquingMap == 0)
168 MachOUniquingMap = new MachOUniqueMapTy();
169 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
171 // Form the name to look up.
172 SmallString<64> Name;
177 // Do the lookup, if we have a hit, return it.
178 const MCSectionMachO *&Entry = Map[Name.str()];
179 if (Entry) return Entry;
181 // Otherwise, return a new section.
182 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
186 const MCSectionELF *MCContext::
187 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
189 return getELFSection(Section, Type, Flags, Kind, 0, "");
192 const MCSectionELF *MCContext::
193 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
194 SectionKind Kind, unsigned EntrySize, StringRef Group) {
195 if (ELFUniquingMap == 0)
196 ELFUniquingMap = new ELFUniqueMapTy();
197 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
199 // Do the lookup, if we have a hit, return it.
200 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
201 if (Entry.getValue()) return Entry.getValue();
203 // Possibly refine the entry size first.
205 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
208 MCSymbol *GroupSym = NULL;
210 GroupSym = GetOrCreateSymbol(Group);
212 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
213 Kind, EntrySize, GroupSym);
214 Entry.setValue(Result);
218 const MCSectionELF *MCContext::CreateELFGroupSection() {
219 MCSectionELF *Result =
220 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
221 SectionKind::getReadOnly(), 4, NULL);
225 const MCSection *MCContext::getCOFFSection(StringRef Section,
226 unsigned Characteristics,
229 if (COFFUniquingMap == 0)
230 COFFUniquingMap = new COFFUniqueMapTy();
231 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
233 // Do the lookup, if we have a hit, return it.
234 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
235 if (Entry.getValue()) return Entry.getValue();
237 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
241 Entry.setValue(Result);
245 //===----------------------------------------------------------------------===//
247 //===----------------------------------------------------------------------===//
249 /// GetDwarfFile - takes a file name an number to place in the dwarf file and
250 /// directory tables. If the file number has already been allocated it is an
251 /// error and zero is returned and the client reports the error, else the
252 /// allocated file number is returned. The file numbers may be in any order.
253 unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName,
254 unsigned FileNumber) {
255 // TODO: a FileNumber of zero says to use the next available file number.
256 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
257 // to not be less than one. This needs to be change to be not less than zero.
259 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
260 if (FileNumber >= MCDwarfFiles.size()) {
261 MCDwarfFiles.resize(FileNumber + 1);
263 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
265 // It is an error to use see the same number more than once.
269 // Get the new MCDwarfFile slot for this FileNumber.
270 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
272 if (Directory.empty()) {
273 // Separate the directory part from the basename of the FileName.
274 std::pair<StringRef, StringRef> Slash = FileName.rsplit('/');
275 Directory = Slash.second;
276 if (!Directory.empty()) {
277 Directory = Slash.first;
278 FileName = Slash.second;
282 // Find or make a entry in the MCDwarfDirs vector for this Directory.
283 // Capture directory name.
285 if (Directory.empty()) {
286 // For FileNames with no directories a DirIndex of 0 is used.
290 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
291 if (Directory == MCDwarfDirs[DirIndex])
294 if (DirIndex >= MCDwarfDirs.size()) {
295 char *Buf = static_cast<char *>(Allocate(Directory.size()));
296 memcpy(Buf, Directory.data(), Directory.size());
297 MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
299 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
300 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
301 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
302 // are stored at MCDwarfFiles[FileNumber].Name .
306 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
308 char *Buf = static_cast<char *>(Allocate(FileName.size()));
309 memcpy(Buf, FileName.data(), FileName.size());
310 File = new (*this) MCDwarfFile(StringRef(Buf, FileName.size()), DirIndex);
312 // return the allocated FileNumber.
316 /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
317 /// currently is assigned and false otherwise.
318 bool MCContext::isValidDwarfFileNumber(unsigned FileNumber) {
319 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
322 return MCDwarfFiles[FileNumber] != 0;