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) {
36 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
38 SecureLogUsed = false;
43 MCContext::~MCContext() {
44 // NOTE: The symbols are all allocated out of a bump pointer allocator,
45 // we don't need to free them here.
47 // If we have the MachO uniquing map, free it.
48 delete (MachOUniqueMapTy*)MachOUniquingMap;
49 delete (ELFUniqueMapTy*)ELFUniquingMap;
50 delete (COFFUniqueMapTy*)COFFUniquingMap;
52 // If the stream for the .secure_log_unique directive was created free it.
53 delete (raw_ostream*)SecureLog;
58 //===----------------------------------------------------------------------===//
59 // Symbol Manipulation
60 //===----------------------------------------------------------------------===//
62 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
63 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
65 // Do the lookup and get the entire StringMapEntry. We want access to the
66 // key if we are creating the entry.
67 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
68 MCSymbol *Sym = Entry.getValue();
73 Sym = CreateSymbol(Name);
78 MCSymbol *MCContext::CreateSymbol(StringRef Name) {
79 // Determine whether this is an assembler temporary or normal label.
80 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
82 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
83 if (NameEntry->getValue()) {
84 assert(isTemporary && "Cannot rename non temporary symbols");
85 SmallString<128> NewName;
87 Twine T = Name + Twine(NextUniqueID++);
89 StringRef foo = NewName;
90 NameEntry = &UsedNames.GetOrCreateValue(foo);
91 } while (NameEntry->getValue());
93 NameEntry->setValue(true);
95 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
96 // to the copy of the string that is embedded in the UsedNames entry.
97 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
102 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
103 SmallString<128> NameSV;
104 Name.toVector(NameSV);
105 return GetOrCreateSymbol(NameSV.str());
108 MCSymbol *MCContext::CreateTempSymbol() {
109 SmallString<128> NameSV;
110 Twine Name = Twine(MAI.getPrivateGlobalPrefix()) + "tmp" +
111 Twine(NextUniqueID++);
112 Name.toVector(NameSV);
113 return CreateSymbol(NameSV);
116 unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
117 MCLabel *&Label = Instances[LocalLabelVal];
119 Label = new (*this) MCLabel(0);
120 return Label->incInstance();
123 unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
124 MCLabel *&Label = Instances[LocalLabelVal];
126 Label = new (*this) MCLabel(0);
127 return Label->getInstance();
130 MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
131 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
132 Twine(LocalLabelVal) +
134 Twine(NextInstance(LocalLabelVal)));
136 MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
138 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
139 Twine(LocalLabelVal) +
141 Twine(GetInstance(LocalLabelVal) + bORf));
144 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
145 return Symbols.lookup(Name);
148 //===----------------------------------------------------------------------===//
149 // Section Management
150 //===----------------------------------------------------------------------===//
152 const MCSectionMachO *MCContext::
153 getMachOSection(StringRef Segment, StringRef Section,
154 unsigned TypeAndAttributes,
155 unsigned Reserved2, SectionKind Kind) {
157 // We unique sections by their segment/section pair. The returned section
158 // may not have the same flags as the requested section, if so this should be
159 // diagnosed by the client as an error.
161 // Create the map if it doesn't already exist.
162 if (MachOUniquingMap == 0)
163 MachOUniquingMap = new MachOUniqueMapTy();
164 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
166 // Form the name to look up.
167 SmallString<64> Name;
172 // Do the lookup, if we have a hit, return it.
173 const MCSectionMachO *&Entry = Map[Name.str()];
174 if (Entry) return Entry;
176 // Otherwise, return a new section.
177 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
181 const MCSectionELF *MCContext::
182 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
184 return getELFSection(Section, Type, Flags, Kind, 0, "");
187 const MCSectionELF *MCContext::
188 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
189 SectionKind Kind, unsigned EntrySize, StringRef Group) {
190 if (ELFUniquingMap == 0)
191 ELFUniquingMap = new ELFUniqueMapTy();
192 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
194 // Do the lookup, if we have a hit, return it.
195 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
196 if (Entry.getValue()) return Entry.getValue();
198 // Possibly refine the entry size first.
200 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
203 MCSymbol *GroupSym = NULL;
205 GroupSym = GetOrCreateSymbol(Group);
207 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
208 Kind, EntrySize, GroupSym);
209 Entry.setValue(Result);
213 const MCSectionELF *MCContext::CreateELFGroupSection() {
214 MCSectionELF *Result =
215 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
216 SectionKind::getReadOnly(), 4, NULL);
220 const MCSection *MCContext::getCOFFSection(StringRef Section,
221 unsigned Characteristics,
224 if (COFFUniquingMap == 0)
225 COFFUniquingMap = new COFFUniqueMapTy();
226 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
228 // Do the lookup, if we have a hit, return it.
229 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
230 if (Entry.getValue()) return Entry.getValue();
232 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
236 Entry.setValue(Result);
240 //===----------------------------------------------------------------------===//
242 //===----------------------------------------------------------------------===//
244 /// GetDwarfFile - takes a file name an number to place in the dwarf file and
245 /// directory tables. If the file number has already been allocated it is an
246 /// error and zero is returned and the client reports the error, else the
247 /// allocated file number is returned. The file numbers may be in any order.
248 unsigned MCContext::GetDwarfFile(StringRef FileName, unsigned FileNumber) {
249 // TODO: a FileNumber of zero says to use the next available file number.
250 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
251 // to not be less than one. This needs to be change to be not less than zero.
253 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
254 if (FileNumber >= MCDwarfFiles.size()) {
255 MCDwarfFiles.resize(FileNumber + 1);
257 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
259 // It is an error to use see the same number more than once.
263 // Get the new MCDwarfFile slot for this FileNumber.
264 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
266 // Separate the directory part from the basename of the FileName.
267 std::pair<StringRef, StringRef> Slash = FileName.rsplit('/');
269 // Find or make a entry in the MCDwarfDirs vector for this Directory.
272 // Capture directory name.
273 if (Slash.second.empty()) {
275 DirIndex = 0; // For FileNames with no directories a DirIndex of 0 is used.
277 StringRef Directory = Slash.first;
279 for (DirIndex = 0; DirIndex < MCDwarfDirs.size(); DirIndex++) {
280 if (Directory == MCDwarfDirs[DirIndex])
283 if (DirIndex >= MCDwarfDirs.size()) {
284 char *Buf = static_cast<char *>(Allocate(Directory.size()));
285 memcpy(Buf, Directory.data(), Directory.size());
286 MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
288 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
289 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
290 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames are
291 // stored at MCDwarfFiles[FileNumber].Name .
295 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
297 char *Buf = static_cast<char *>(Allocate(Name.size()));
298 memcpy(Buf, Name.data(), Name.size());
299 File = new (*this) MCDwarfFile(StringRef(Buf, Name.size()), DirIndex);
301 // return the allocated FileNumber.
305 /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
306 /// currently is assigned and false otherwise.
307 bool MCContext::isValidDwarfFileNumber(unsigned FileNumber) {
308 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
311 return MCDwarfFiles[FileNumber] != 0;