When promoting an alloca to registers discard any lifetime intrinsics.
[llvm/stm8.git] / lib / MC / MCContext.cpp
blob8faa72ecb4e8895471eee54a725998963d7a10be
1 //===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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"
22 using namespace llvm;
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),
31 Allocator(), Symbols(Allocator), UsedNames(Allocator),
32 NextUniqueID(0),
33 CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0),
34 AllowTemporaryLabels(true) {
35 MachOUniquingMap = 0;
36 ELFUniquingMap = 0;
37 COFFUniquingMap = 0;
39 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
40 SecureLog = 0;
41 SecureLogUsed = false;
43 DwarfLocSeen = false;
46 MCContext::~MCContext() {
47 // NOTE: The symbols are all allocated out of a bump pointer allocator,
48 // we don't need to free them here.
50 // If we have the MachO uniquing map, free it.
51 delete (MachOUniqueMapTy*)MachOUniquingMap;
52 delete (ELFUniqueMapTy*)ELFUniquingMap;
53 delete (COFFUniqueMapTy*)COFFUniquingMap;
55 // If the stream for the .secure_log_unique directive was created free it.
56 delete (raw_ostream*)SecureLog;
58 delete TAI;
61 //===----------------------------------------------------------------------===//
62 // Symbol Manipulation
63 //===----------------------------------------------------------------------===//
65 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
66 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
68 // Do the lookup and get the entire StringMapEntry. We want access to the
69 // key if we are creating the entry.
70 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
71 MCSymbol *Sym = Entry.getValue();
73 if (Sym)
74 return Sym;
76 Sym = CreateSymbol(Name);
77 Entry.setValue(Sym);
78 return Sym;
81 MCSymbol *MCContext::CreateSymbol(StringRef Name) {
82 // Determine whether this is an assembler temporary or normal label, if used.
83 bool isTemporary = false;
84 if (AllowTemporaryLabels)
85 isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
87 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
88 if (NameEntry->getValue()) {
89 assert(isTemporary && "Cannot rename non temporary symbols");
90 SmallString<128> NewName = Name;
91 do {
92 NewName.resize(Name.size());
93 raw_svector_ostream(NewName) << NextUniqueID++;
94 NameEntry = &UsedNames.GetOrCreateValue(NewName);
95 } while (NameEntry->getValue());
97 NameEntry->setValue(true);
99 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
100 // to the copy of the string that is embedded in the UsedNames entry.
101 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
103 return Result;
106 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
107 SmallString<128> NameSV;
108 Name.toVector(NameSV);
109 return GetOrCreateSymbol(NameSV.str());
112 MCSymbol *MCContext::CreateTempSymbol() {
113 SmallString<128> NameSV;
114 raw_svector_ostream(NameSV)
115 << MAI.getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
116 return CreateSymbol(NameSV);
119 unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
120 MCLabel *&Label = Instances[LocalLabelVal];
121 if (!Label)
122 Label = new (*this) MCLabel(0);
123 return Label->incInstance();
126 unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
127 MCLabel *&Label = Instances[LocalLabelVal];
128 if (!Label)
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) +
136 "\2" +
137 Twine(NextInstance(LocalLabelVal)));
139 MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
140 int bORf) {
141 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
142 Twine(LocalLabelVal) +
143 "\2" +
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;
171 Name += Segment;
172 Name.push_back(',');
173 Name += Section;
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,
181 Reserved2, Kind);
184 const MCSectionELF *MCContext::
185 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
186 SectionKind Kind) {
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.
202 if (!EntrySize) {
203 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
206 MCSymbol *GroupSym = NULL;
207 if (!Group.empty())
208 GroupSym = GetOrCreateSymbol(Group);
210 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
211 Kind, EntrySize, GroupSym);
212 Entry.setValue(Result);
213 return Result;
216 const MCSectionELF *MCContext::CreateELFGroupSection() {
217 MCSectionELF *Result =
218 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
219 SectionKind::getReadOnly(), 4, NULL);
220 return Result;
223 const MCSection *MCContext::getCOFFSection(StringRef Section,
224 unsigned Characteristics,
225 int Selection,
226 SectionKind Kind) {
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(),
236 Characteristics,
237 Selection, Kind);
239 Entry.setValue(Result);
240 return Result;
243 //===----------------------------------------------------------------------===//
244 // Dwarf Management
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);
259 } else {
260 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
261 if (ExistingFile)
262 // It is an error to use see the same number more than once.
263 return 0;
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.
273 StringRef Name;
274 unsigned DirIndex;
275 // Capture directory name.
276 if (Slash.second.empty()) {
277 Name = Slash.first;
278 DirIndex = 0; // For FileNames with no directories a DirIndex of 0 is used.
279 } else {
280 StringRef Directory = Slash.first;
281 Name = Slash.second;
282 for (DirIndex = 0; DirIndex < MCDwarfDirs.size(); DirIndex++) {
283 if (Directory == MCDwarfDirs[DirIndex])
284 break;
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 .
295 DirIndex++;
298 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
299 // vector.
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.
305 return 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())
312 return false;
314 return MCDwarfFiles[FileNumber] != 0;