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/MCSectionMachO.h"
13 #include "llvm/MC/MCSectionELF.h"
14 #include "llvm/MC/MCSectionCOFF.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/MC/MCLabel.h"
17 #include "llvm/MC/MCDwarf.h"
18 #include "llvm/Target/TargetAsmInfo.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/ELF.h"
24 typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
25 typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
26 typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
29 MCContext::MCContext(const MCAsmInfo &mai, const TargetAsmInfo *tai) :
30 MAI(mai), TAI(tai), NextUniqueID(0),
31 CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0),
32 AllowTemporaryLabels(true) {
37 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
39 SecureLogUsed = false;
44 MCContext::~MCContext() {
45 // NOTE: The symbols are all allocated out of a bump pointer allocator,
46 // we don't need to free them here.
48 // If we have the MachO uniquing map, free it.
49 delete (MachOUniqueMapTy*)MachOUniquingMap;
50 delete (ELFUniqueMapTy*)ELFUniquingMap;
51 delete (COFFUniqueMapTy*)COFFUniquingMap;
53 // If the stream for the .secure_log_unique directive was created free it.
54 delete (raw_ostream*)SecureLog;
59 //===----------------------------------------------------------------------===//
60 // Symbol Manipulation
61 //===----------------------------------------------------------------------===//
63 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
64 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
66 // Do the lookup and get the entire StringMapEntry. We want access to the
67 // key if we are creating the entry.
68 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
69 MCSymbol *Sym = Entry.getValue();
74 Sym = CreateSymbol(Name);
79 MCSymbol *MCContext::CreateSymbol(StringRef Name) {
80 // Determine whether this is an assembler temporary or normal label, if used.
81 bool isTemporary = false;
82 if (AllowTemporaryLabels)
83 isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
85 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
86 if (NameEntry->getValue()) {
87 assert(isTemporary && "Cannot rename non temporary symbols");
88 SmallString<128> NewName;
90 Twine T = Name + Twine(NextUniqueID++);
92 StringRef foo = NewName;
93 NameEntry = &UsedNames.GetOrCreateValue(foo);
94 } while (NameEntry->getValue());
96 NameEntry->setValue(true);
98 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
99 // to the copy of the string that is embedded in the UsedNames entry.
100 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
105 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
106 SmallString<128> NameSV;
107 Name.toVector(NameSV);
108 return GetOrCreateSymbol(NameSV.str());
111 MCSymbol *MCContext::CreateTempSymbol() {
112 SmallString<128> NameSV;
113 Twine Name = Twine(MAI.getPrivateGlobalPrefix()) + "tmp" +
114 Twine(NextUniqueID++);
115 Name.toVector(NameSV);
116 return CreateSymbol(NameSV);
119 unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
120 MCLabel *&Label = Instances[LocalLabelVal];
122 Label = new (*this) MCLabel(0);
123 return Label->incInstance();
126 unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
127 MCLabel *&Label = Instances[LocalLabelVal];
129 Label = new (*this) MCLabel(0);
130 return Label->getInstance();
133 MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
134 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
135 Twine(LocalLabelVal) +
137 Twine(NextInstance(LocalLabelVal)));
139 MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
141 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
142 Twine(LocalLabelVal) +
144 Twine(GetInstance(LocalLabelVal) + bORf));
147 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
148 return Symbols.lookup(Name);
151 //===----------------------------------------------------------------------===//
152 // Section Management
153 //===----------------------------------------------------------------------===//
155 const MCSectionMachO *MCContext::
156 getMachOSection(StringRef Segment, StringRef Section,
157 unsigned TypeAndAttributes,
158 unsigned Reserved2, SectionKind Kind) {
160 // We unique sections by their segment/section pair. The returned section
161 // may not have the same flags as the requested section, if so this should be
162 // diagnosed by the client as an error.
164 // Create the map if it doesn't already exist.
165 if (MachOUniquingMap == 0)
166 MachOUniquingMap = new MachOUniqueMapTy();
167 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
169 // Form the name to look up.
170 SmallString<64> Name;
175 // Do the lookup, if we have a hit, return it.
176 const MCSectionMachO *&Entry = Map[Name.str()];
177 if (Entry) return Entry;
179 // Otherwise, return a new section.
180 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
184 const MCSectionELF *MCContext::
185 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
187 return getELFSection(Section, Type, Flags, Kind, 0, "");
190 const MCSectionELF *MCContext::
191 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
192 SectionKind Kind, unsigned EntrySize, StringRef Group) {
193 if (ELFUniquingMap == 0)
194 ELFUniquingMap = new ELFUniqueMapTy();
195 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
197 // Do the lookup, if we have a hit, return it.
198 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
199 if (Entry.getValue()) return Entry.getValue();
201 // Possibly refine the entry size first.
203 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
206 MCSymbol *GroupSym = NULL;
208 GroupSym = GetOrCreateSymbol(Group);
210 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
211 Kind, EntrySize, GroupSym);
212 Entry.setValue(Result);
216 const MCSectionELF *MCContext::CreateELFGroupSection() {
217 MCSectionELF *Result =
218 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
219 SectionKind::getReadOnly(), 4, NULL);
223 const MCSection *MCContext::getCOFFSection(StringRef Section,
224 unsigned Characteristics,
227 if (COFFUniquingMap == 0)
228 COFFUniquingMap = new COFFUniqueMapTy();
229 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
231 // Do the lookup, if we have a hit, return it.
232 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
233 if (Entry.getValue()) return Entry.getValue();
235 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
239 Entry.setValue(Result);
243 //===----------------------------------------------------------------------===//
245 //===----------------------------------------------------------------------===//
247 /// GetDwarfFile - takes a file name an number to place in the dwarf file and
248 /// directory tables. If the file number has already been allocated it is an
249 /// error and zero is returned and the client reports the error, else the
250 /// allocated file number is returned. The file numbers may be in any order.
251 unsigned MCContext::GetDwarfFile(StringRef FileName, unsigned FileNumber) {
252 // TODO: a FileNumber of zero says to use the next available file number.
253 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
254 // to not be less than one. This needs to be change to be not less than zero.
256 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
257 if (FileNumber >= MCDwarfFiles.size()) {
258 MCDwarfFiles.resize(FileNumber + 1);
260 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
262 // It is an error to use see the same number more than once.
266 // Get the new MCDwarfFile slot for this FileNumber.
267 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
269 // Separate the directory part from the basename of the FileName.
270 std::pair<StringRef, StringRef> Slash = FileName.rsplit('/');
272 // Find or make a entry in the MCDwarfDirs vector for this Directory.
275 // Capture directory name.
276 if (Slash.second.empty()) {
278 DirIndex = 0; // For FileNames with no directories a DirIndex of 0 is used.
280 StringRef Directory = Slash.first;
282 for (DirIndex = 0; DirIndex < MCDwarfDirs.size(); DirIndex++) {
283 if (Directory == MCDwarfDirs[DirIndex])
286 if (DirIndex >= MCDwarfDirs.size()) {
287 char *Buf = static_cast<char *>(Allocate(Directory.size()));
288 memcpy(Buf, Directory.data(), Directory.size());
289 MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
291 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
292 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
293 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames are
294 // stored at MCDwarfFiles[FileNumber].Name .
298 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
300 char *Buf = static_cast<char *>(Allocate(Name.size()));
301 memcpy(Buf, Name.data(), Name.size());
302 File = new (*this) MCDwarfFile(StringRef(Buf, Name.size()), DirIndex);
304 // return the allocated FileNumber.
308 /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
309 /// currently is assigned and false otherwise.
310 bool MCContext::isValidDwarfFileNumber(unsigned FileNumber) {
311 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
314 return MCDwarfFiles[FileNumber] != 0;