Parse and remember discriminators in .loc line. I try to output them with
[oota-llvm.git] / lib / MC / MCContext.cpp
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 //===----------------------------------------------------------------------===//
9
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"
20 using namespace llvm;
21
22 typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
23 typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
24 typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
25
26
27 MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0),
28                      CurrentDwarfLoc(0,0,0,0,0,0) {
29   MachOUniquingMap = 0;
30   ELFUniquingMap = 0;
31   COFFUniquingMap = 0;
32
33   SecureLogFile = getenv("AS_SECURE_LOG_FILE");
34   SecureLog = 0;
35   SecureLogUsed = false;
36
37   DwarfLocSeen = false;
38 }
39
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.
43   
44   // If we have the MachO uniquing map, free it.
45   delete (MachOUniqueMapTy*)MachOUniquingMap;
46   delete (ELFUniqueMapTy*)ELFUniquingMap;
47   delete (COFFUniqueMapTy*)COFFUniquingMap;
48
49   // If the stream for the .secure_log_unique directive was created free it.
50   delete (raw_ostream*)SecureLog;
51 }
52
53 //===----------------------------------------------------------------------===//
54 // Symbol Manipulation
55 //===----------------------------------------------------------------------===//
56
57 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
58   assert(!Name.empty() && "Normal symbols cannot be unnamed!");
59   
60   // Determine whether this is an assembler temporary or normal label.
61   bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
62   
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();
67
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);
72   return Result; 
73 }
74
75 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
76   SmallString<128> NameSV;
77   Name.toVector(NameSV);
78   return GetOrCreateSymbol(NameSV.str());
79 }
80
81 MCSymbol *MCContext::CreateTempSymbol() {
82   return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
83                            "tmp" + Twine(NextUniqueID++));
84 }
85
86 unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
87   MCLabel *&Label = Instances[LocalLabelVal];
88   if (!Label)
89     Label = new (*this) MCLabel(0);
90   return Label->incInstance();
91 }
92
93 unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
94   MCLabel *&Label = Instances[LocalLabelVal];
95   if (!Label)
96     Label = new (*this) MCLabel(0);
97   return Label->getInstance();
98 }
99
100 MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
101   return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
102                            Twine(LocalLabelVal) +
103                            "\2" +
104                            Twine(NextInstance(LocalLabelVal)));
105 }
106 MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
107                                                int bORf) {
108   return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
109                            Twine(LocalLabelVal) +
110                            "\2" +
111                            Twine(GetInstance(LocalLabelVal) + bORf));
112 }
113
114 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
115   return Symbols.lookup(Name);
116 }
117
118 //===----------------------------------------------------------------------===//
119 // Section Management
120 //===----------------------------------------------------------------------===//
121
122 const MCSectionMachO *MCContext::
123 getMachOSection(StringRef Segment, StringRef Section,
124                 unsigned TypeAndAttributes,
125                 unsigned Reserved2, SectionKind Kind) {
126   
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.
130   
131   // Create the map if it doesn't already exist.
132   if (MachOUniquingMap == 0)
133     MachOUniquingMap = new MachOUniqueMapTy();
134   MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
135   
136   // Form the name to look up.
137   SmallString<64> Name;
138   Name += Segment;
139   Name.push_back(',');
140   Name += Section;
141   
142   // Do the lookup, if we have a hit, return it.
143   const MCSectionMachO *&Entry = Map[Name.str()];
144   if (Entry) return Entry;
145   
146   // Otherwise, return a new section.
147   return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
148                                             Reserved2, Kind);
149 }
150
151 const MCSectionELF *MCContext::
152 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
153               SectionKind Kind) {
154   return getELFSection(Section, Type, Flags, Kind, 0, "");
155 }
156
157 const MCSectionELF *MCContext::
158 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
159               SectionKind Kind, unsigned EntrySize, StringRef Group) {
160   if (ELFUniquingMap == 0)
161     ELFUniquingMap = new ELFUniqueMapTy();
162   ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
163   
164   // Do the lookup, if we have a hit, return it.
165   StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
166   if (Entry.getValue()) return Entry.getValue();
167   
168   // Possibly refine the entry size first.
169   if (!EntrySize) {
170     EntrySize = MCSectionELF::DetermineEntrySize(Kind);
171   }
172
173   MCSymbol *GroupSym = NULL;
174   if (!Group.empty())
175     GroupSym = GetOrCreateSymbol(Group);
176
177   MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
178                                                   Kind, EntrySize, GroupSym);
179   Entry.setValue(Result);
180   return Result;
181 }
182
183 const MCSectionELF *MCContext::CreateELFGroupSection() {
184   MCSectionELF *Result =
185     new (*this) MCSectionELF(".group", MCSectionELF::SHT_GROUP, 0,
186                              SectionKind::getReadOnly(), 4, NULL);
187   return Result;
188 }
189
190 const MCSection *MCContext::getCOFFSection(StringRef Section,
191                                            unsigned Characteristics,
192                                            int Selection,
193                                            SectionKind Kind) {
194   if (COFFUniquingMap == 0)
195     COFFUniquingMap = new COFFUniqueMapTy();
196   COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
197   
198   // Do the lookup, if we have a hit, return it.
199   StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
200   if (Entry.getValue()) return Entry.getValue();
201   
202   MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
203                                                     Characteristics,
204                                                     Selection, Kind);
205   
206   Entry.setValue(Result);
207   return Result;
208 }
209
210 //===----------------------------------------------------------------------===//
211 // Dwarf Management
212 //===----------------------------------------------------------------------===//
213
214 /// GetDwarfFile - takes a file name an number to place in the dwarf file and
215 /// directory tables.  If the file number has already been allocated it is an
216 /// error and zero is returned and the client reports the error, else the
217 /// allocated file number is returned.  The file numbers may be in any order.
218 unsigned MCContext::GetDwarfFile(StringRef FileName, unsigned FileNumber) {
219   // TODO: a FileNumber of zero says to use the next available file number.
220   // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
221   // to not be less than one.  This needs to be change to be not less than zero.
222
223   // Make space for this FileNumber in the MCDwarfFiles vector if needed.
224   if (FileNumber >= MCDwarfFiles.size()) {
225     MCDwarfFiles.resize(FileNumber + 1);
226   } else {
227     MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
228     if (ExistingFile)
229       // It is an error to use see the same number more than once.
230       return 0;
231   }
232
233   // Get the new MCDwarfFile slot for this FileNumber.
234   MCDwarfFile *&File = MCDwarfFiles[FileNumber];
235
236   // Separate the directory part from the basename of the FileName.
237   std::pair<StringRef, StringRef> Slash = FileName.rsplit('/');
238
239   // Find or make a entry in the MCDwarfDirs vector for this Directory.
240   StringRef Name;
241   unsigned DirIndex;
242   // Capture directory name.
243   if (Slash.second.empty()) {
244     Name = Slash.first;
245     DirIndex = 0; // For FileNames with no directories a DirIndex of 0 is used.
246   } else {
247     StringRef Directory = Slash.first;
248     Name = Slash.second;
249     for (DirIndex = 0; DirIndex < MCDwarfDirs.size(); DirIndex++) {
250       if (Directory == MCDwarfDirs[DirIndex])
251         break;
252     }
253     if (DirIndex >= MCDwarfDirs.size()) {
254       char *Buf = static_cast<char *>(Allocate(Directory.size()));
255       memcpy(Buf, Directory.data(), Directory.size());
256       MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
257     }
258     // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
259     // no directories.  MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
260     // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames are
261     // stored at MCDwarfFiles[FileNumber].Name .
262     DirIndex++;
263   }
264   
265   // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
266   // vector.
267   char *Buf = static_cast<char *>(Allocate(Name.size()));
268   memcpy(Buf, Name.data(), Name.size());
269   File = new (*this) MCDwarfFile(StringRef(Buf, Name.size()), DirIndex);
270
271   // return the allocated FileNumber.
272   return FileNumber;
273 }
274
275 /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
276 /// currently is assigned and false otherwise.
277 bool MCContext::isValidDwarfFileNumber(unsigned FileNumber) {
278   if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
279     return false;
280
281   return MCDwarfFiles[FileNumber] != 0;
282 }