Add ELF ObjectWriter and Streamer support.
[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   MachOUniquingMap = 0;
29   ELFUniquingMap = 0;
30   COFFUniquingMap = 0;
31
32   SecureLogFile = getenv("AS_SECURE_LOG_FILE");
33   SecureLog = 0;
34   SecureLogUsed = false;
35 }
36
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.
40   
41   // If we have the MachO uniquing map, free it.
42   delete (MachOUniqueMapTy*)MachOUniquingMap;
43   delete (ELFUniqueMapTy*)ELFUniquingMap;
44   delete (COFFUniqueMapTy*)COFFUniquingMap;
45
46   // If the stream for the .secure_log_unique directive was created free it.
47   delete (raw_ostream*)SecureLog;
48 }
49
50 //===----------------------------------------------------------------------===//
51 // Symbol Manipulation
52 //===----------------------------------------------------------------------===//
53
54 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
55   assert(!Name.empty() && "Normal symbols cannot be unnamed!");
56   
57   // Determine whether this is an assembler temporary or normal label.
58   bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
59   
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();
64
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);
69   return Result; 
70 }
71
72 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
73   SmallString<128> NameSV;
74   Name.toVector(NameSV);
75   return GetOrCreateSymbol(NameSV.str());
76 }
77
78 MCSymbol *MCContext::CreateTempSymbol() {
79   return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
80                            "tmp" + Twine(NextUniqueID++));
81 }
82
83 unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
84   MCLabel *&Label = Instances[LocalLabelVal];
85   if (!Label)
86     Label = new (*this) MCLabel(0);
87   return Label->incInstance();
88 }
89
90 unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
91   MCLabel *&Label = Instances[LocalLabelVal];
92   if (!Label)
93     Label = new (*this) MCLabel(0);
94   return Label->getInstance();
95 }
96
97 MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
98   return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
99                            Twine(LocalLabelVal) +
100                            "\2" +
101                            Twine(NextInstance(LocalLabelVal)));
102 }
103 MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
104                                                int bORf) {
105   return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
106                            Twine(LocalLabelVal) +
107                            "\2" +
108                            Twine(GetInstance(LocalLabelVal) + bORf));
109 }
110
111 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
112   return Symbols.lookup(Name);
113 }
114
115 //===----------------------------------------------------------------------===//
116 // Section Management
117 //===----------------------------------------------------------------------===//
118
119 const MCSectionMachO *MCContext::
120 getMachOSection(StringRef Segment, StringRef Section,
121                 unsigned TypeAndAttributes,
122                 unsigned Reserved2, SectionKind Kind) {
123   
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.
127   
128   // Create the map if it doesn't already exist.
129   if (MachOUniquingMap == 0)
130     MachOUniquingMap = new MachOUniqueMapTy();
131   MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
132   
133   // Form the name to look up.
134   SmallString<64> Name;
135   Name += Segment;
136   Name.push_back(',');
137   Name += Section;
138   
139   // Do the lookup, if we have a hit, return it.
140   const MCSectionMachO *&Entry = Map[Name.str()];
141   if (Entry) return Entry;
142   
143   // Otherwise, return a new section.
144   return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
145                                             Reserved2, Kind);
146 }
147
148
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;
155   
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();
159   
160   MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
161                                                   Kind, IsExplicit, EntrySize);
162   Entry.setValue(Result);
163   return Result;
164 }
165
166 const MCSection *MCContext::getCOFFSection(StringRef Section,
167                                            unsigned Characteristics,
168                                            int Selection,
169                                            SectionKind Kind) {
170   if (COFFUniquingMap == 0)
171     COFFUniquingMap = new COFFUniqueMapTy();
172   COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
173   
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();
177   
178   MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
179                                                     Characteristics,
180                                                     Selection, Kind);
181   
182   Entry.setValue(Result);
183   return Result;
184 }
185
186 //===----------------------------------------------------------------------===//
187 // Dwarf Management
188 //===----------------------------------------------------------------------===//
189
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.
198
199   // Make space for this FileNumber in the MCDwarfFiles vector if needed.
200   if (FileNumber >= MCDwarfFiles.size()) {
201     MCDwarfFiles.resize(FileNumber + 1);
202   } else {
203     MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
204     if (ExistingFile)
205       // It is an error to use see the same number more than once.
206       return 0;
207   }
208
209   // Get the new MCDwarfFile slot for this FileNumber.
210   MCDwarfFile *&File = MCDwarfFiles[FileNumber];
211
212   // Separate the directory part from the basename of the FileName.
213   std::pair<StringRef, StringRef> Slash = FileName.rsplit('/');
214
215   // Find or make a entry in the MCDwarfDirs vector for this Directory.
216   StringRef Name;
217   unsigned DirIndex;
218   // Capture directory name.
219   if (Slash.second.empty()) {
220     Name = Slash.first;
221     DirIndex = 0; // For FileNames with no directories a DirIndex of 0 is used.
222   } else {
223     StringRef Directory = Slash.first;
224     Name = Slash.second;
225     for (DirIndex = 0; DirIndex < MCDwarfDirs.size(); DirIndex++) {
226       if (Directory == MCDwarfDirs[DirIndex])
227         break;
228     }
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()));
233     }
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 .
238     DirIndex++;
239   }
240   
241   // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
242   // vector.
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);
246
247   // return the allocated FileNumber.
248   return FileNumber;
249 }