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),
28 CurrentDwarfLoc(0,0,0,0,0) {
33 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
35 SecureLogUsed = false;
40 MCContext::~MCContext() {
41 // NOTE: The symbols are all allocated out of a bump pointer allocator,
42 // we don't need to free them here.
44 // If we have the MachO uniquing map, free it.
45 delete (MachOUniqueMapTy*)MachOUniquingMap;
46 delete (ELFUniqueMapTy*)ELFUniquingMap;
47 delete (COFFUniqueMapTy*)COFFUniquingMap;
49 // If the stream for the .secure_log_unique directive was created free it.
50 delete (raw_ostream*)SecureLog;
53 //===----------------------------------------------------------------------===//
54 // Symbol Manipulation
55 //===----------------------------------------------------------------------===//
57 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
58 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
60 // Determine whether this is an assembler temporary or normal label.
61 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
63 // Do the lookup and get the entire StringMapEntry. We want access to the
64 // key if we are creating the entry.
65 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
66 if (Entry.getValue()) return Entry.getValue();
68 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
69 // to the copy of the string that is embedded in the StringMapEntry.
70 MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary);
71 Entry.setValue(Result);
75 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
76 SmallString<128> NameSV;
77 Name.toVector(NameSV);
78 return GetOrCreateSymbol(NameSV.str());
81 MCSymbol *MCContext::CreateTempSymbol() {
82 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
83 "tmp" + Twine(NextUniqueID++));
86 unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
87 MCLabel *&Label = Instances[LocalLabelVal];
89 Label = new (*this) MCLabel(0);
90 return Label->incInstance();
93 unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
94 MCLabel *&Label = Instances[LocalLabelVal];
96 Label = new (*this) MCLabel(0);
97 return Label->getInstance();
100 MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
101 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
102 Twine(LocalLabelVal) +
104 Twine(NextInstance(LocalLabelVal)));
106 MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
108 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
109 Twine(LocalLabelVal) +
111 Twine(GetInstance(LocalLabelVal) + bORf));
114 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
115 return Symbols.lookup(Name);
118 //===----------------------------------------------------------------------===//
119 // Section Management
120 //===----------------------------------------------------------------------===//
122 const MCSectionMachO *MCContext::
123 getMachOSection(StringRef Segment, StringRef Section,
124 unsigned TypeAndAttributes,
125 unsigned Reserved2, SectionKind Kind) {
127 // We unique sections by their segment/section pair. The returned section
128 // may not have the same flags as the requested section, if so this should be
129 // diagnosed by the client as an error.
131 // Create the map if it doesn't already exist.
132 if (MachOUniquingMap == 0)
133 MachOUniquingMap = new MachOUniqueMapTy();
134 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
136 // Form the name to look up.
137 SmallString<64> Name;
142 // Do the lookup, if we have a hit, return it.
143 const MCSectionMachO *&Entry = Map[Name.str()];
144 if (Entry) return Entry;
146 // Otherwise, return a new section.
147 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
152 const MCSection *MCContext::
153 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
154 SectionKind Kind, bool IsExplicit, unsigned EntrySize) {
155 if (ELFUniquingMap == 0)
156 ELFUniquingMap = new ELFUniqueMapTy();
157 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
159 // Do the lookup, if we have a hit, return it.
160 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
161 if (Entry.getValue()) return Entry.getValue();
163 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
164 Kind, IsExplicit, EntrySize);
165 Entry.setValue(Result);
169 const MCSection *MCContext::getCOFFSection(StringRef Section,
170 unsigned Characteristics,
173 if (COFFUniquingMap == 0)
174 COFFUniquingMap = new COFFUniqueMapTy();
175 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
177 // Do the lookup, if we have a hit, return it.
178 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
179 if (Entry.getValue()) return Entry.getValue();
181 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
185 Entry.setValue(Result);
189 //===----------------------------------------------------------------------===//
191 //===----------------------------------------------------------------------===//
193 /// GetDwarfFile - takes a file name an number to place in the dwarf file and
194 /// directory tables. If the file number has already been allocated it is an
195 /// error and zero is returned and the client reports the error, else the
196 /// allocated file number is returned. The file numbers may be in any order.
197 unsigned MCContext::GetDwarfFile(StringRef FileName, unsigned FileNumber) {
198 // TODO: a FileNumber of zero says to use the next available file number.
199 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
200 // to not be less than one. This needs to be change to be not less than zero.
202 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
203 if (FileNumber >= MCDwarfFiles.size()) {
204 MCDwarfFiles.resize(FileNumber + 1);
206 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
208 // It is an error to use see the same number more than once.
212 // Get the new MCDwarfFile slot for this FileNumber.
213 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
215 // Separate the directory part from the basename of the FileName.
216 std::pair<StringRef, StringRef> Slash = FileName.rsplit('/');
218 // Find or make a entry in the MCDwarfDirs vector for this Directory.
221 // Capture directory name.
222 if (Slash.second.empty()) {
224 DirIndex = 0; // For FileNames with no directories a DirIndex of 0 is used.
226 StringRef Directory = Slash.first;
228 for (DirIndex = 0; DirIndex < MCDwarfDirs.size(); DirIndex++) {
229 if (Directory == MCDwarfDirs[DirIndex])
232 if (DirIndex >= MCDwarfDirs.size()) {
233 char *Buf = static_cast<char *>(Allocate(Directory.size()));
234 memcpy(Buf, Directory.data(), Directory.size());
235 MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
237 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
238 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
239 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames are
240 // stored at MCDwarfFiles[FileNumber].Name .
244 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
246 char *Buf = static_cast<char *>(Allocate(Name.size()));
247 memcpy(Buf, Name.data(), Name.size());
248 File = new (*this) MCDwarfFile(StringRef(Buf, Name.size()), DirIndex);
250 // return the allocated FileNumber.
254 /// ValidateDwarfFileNumber - takes a dwarf file number and returns true if it
255 /// currently is assigned and false otherwise.
256 bool MCContext::ValidateDwarfFileNumber(unsigned FileNumber) {
257 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
260 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];