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/ADT/SmallString.h"
19 #include "llvm/ADT/Twine.h"
22 typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
23 typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
24 typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
27 MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) {
32 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
34 SecureLogUsed = false;
37 MCContext::~MCContext() {
38 // NOTE: The symbols are all allocated out of a bump pointer allocator,
39 // we don't need to free them here.
41 // If we have the MachO uniquing map, free it.
42 delete (MachOUniqueMapTy*)MachOUniquingMap;
43 delete (ELFUniqueMapTy*)ELFUniquingMap;
44 delete (COFFUniqueMapTy*)COFFUniquingMap;
46 // If the stream for the .secure_log_unique directive was created free it.
47 delete (raw_ostream*)SecureLog;
50 //===----------------------------------------------------------------------===//
51 // Symbol Manipulation
52 //===----------------------------------------------------------------------===//
54 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
55 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
57 // Determine whether this is an assembler temporary or normal label.
58 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
60 // Do the lookup and get the entire StringMapEntry. We want access to the
61 // key if we are creating the entry.
62 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
63 if (Entry.getValue()) return Entry.getValue();
65 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
66 // to the copy of the string that is embedded in the StringMapEntry.
67 MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary);
68 Entry.setValue(Result);
72 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
73 SmallString<128> NameSV;
74 Name.toVector(NameSV);
75 return GetOrCreateSymbol(NameSV.str());
78 MCSymbol *MCContext::CreateTempSymbol() {
79 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
80 "tmp" + Twine(NextUniqueID++));
83 unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
84 MCLabel *&Label = Instances[LocalLabelVal];
86 Label = new (*this) MCLabel(0);
87 return Label->incInstance();
90 unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
91 MCLabel *&Label = Instances[LocalLabelVal];
93 Label = new (*this) MCLabel(0);
94 return Label->getInstance();
97 MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
98 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
99 Twine(LocalLabelVal) +
101 Twine(NextInstance(LocalLabelVal)));
103 MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
105 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
106 Twine(LocalLabelVal) +
108 Twine(GetInstance(LocalLabelVal) + bORf));
111 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
112 return Symbols.lookup(Name);
115 //===----------------------------------------------------------------------===//
116 // Section Management
117 //===----------------------------------------------------------------------===//
119 const MCSectionMachO *MCContext::
120 getMachOSection(StringRef Segment, StringRef Section,
121 unsigned TypeAndAttributes,
122 unsigned Reserved2, SectionKind Kind) {
124 // We unique sections by their segment/section pair. The returned section
125 // may not have the same flags as the requested section, if so this should be
126 // diagnosed by the client as an error.
128 // Create the map if it doesn't already exist.
129 if (MachOUniquingMap == 0)
130 MachOUniquingMap = new MachOUniqueMapTy();
131 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
133 // Form the name to look up.
134 SmallString<64> Name;
139 // Do the lookup, if we have a hit, return it.
140 const MCSectionMachO *&Entry = Map[Name.str()];
141 if (Entry) return Entry;
143 // Otherwise, return a new section.
144 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
149 const MCSection *MCContext::
150 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
151 SectionKind Kind, bool IsExplicit, unsigned EntrySize) {
152 if (ELFUniquingMap == 0)
153 ELFUniquingMap = new ELFUniqueMapTy();
154 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
156 // Do the lookup, if we have a hit, return it.
157 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
158 if (Entry.getValue()) return Entry.getValue();
160 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
161 Kind, IsExplicit, EntrySize);
162 Entry.setValue(Result);
166 const MCSection *MCContext::getCOFFSection(StringRef Section,
167 unsigned Characteristics,
170 if (COFFUniquingMap == 0)
171 COFFUniquingMap = new COFFUniqueMapTy();
172 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
174 // Do the lookup, if we have a hit, return it.
175 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
176 if (Entry.getValue()) return Entry.getValue();
178 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
182 Entry.setValue(Result);
186 //===----------------------------------------------------------------------===//
188 //===----------------------------------------------------------------------===//
190 /// GetDwarfFile - takes a file name an number to place in the dwarf file and
191 /// directory tables. If the file number has already been allocated it is an
192 /// error and zero is returned and the client reports the error, else the
193 /// allocated file number is returned. The file numbers may be in any order.
194 unsigned MCContext::GetDwarfFile(StringRef FileName, unsigned FileNumber) {
195 // TODO: a FileNumber of zero says to use the next available file number.
196 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
197 // to not be less than one. This needs to be change to be not less than zero.
199 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
200 if (FileNumber >= MCDwarfFiles.size()) {
201 MCDwarfFiles.resize(FileNumber + 1);
203 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
205 // It is an error to use see the same number more than once.
209 // Get the new MCDwarfFile slot for this FileNumber.
210 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
212 // Separate the directory part from the basename of the FileName.
213 std::pair<StringRef, StringRef> Slash = FileName.rsplit('/');
215 // Find or make a entry in the MCDwarfDirs vector for this Directory.
218 // Capture directory name.
219 if (Slash.second.empty()) {
221 DirIndex = 0; // For FileNames with no directories a DirIndex of 0 is used.
223 StringRef Directory = Slash.first;
225 for (DirIndex = 0; DirIndex < MCDwarfDirs.size(); DirIndex++) {
226 if (Directory == MCDwarfDirs[DirIndex])
229 if (DirIndex >= MCDwarfDirs.size()) {
230 char *Buf = static_cast<char *>(Allocate(Directory.size()));
231 memcpy(Buf, Directory.data(), Directory.size());
232 MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
234 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
235 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
236 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames are
237 // stored at MCDwarfFiles[FileNumber].Name .
241 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
243 char *Buf = static_cast<char *>(Allocate(Name.size()));
244 memcpy(Buf, Name.data(), Name.size());
245 File = new (*this) MCDwarfFile(StringRef(Buf, Name.size()), DirIndex);
247 // return the allocated FileNumber.