blob: 814726ebd8c0f54615be75624f78515ab03181f3 [file] [log] [blame]
Daniel Dunbarba1da8a2009-06-23 23:39:15 +00001//===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
Daniel Dunbarecc63f82009-06-23 22:01:43 +00002//
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"
Chris Lattner1a5c28f2010-03-11 22:56:10 +000011#include "llvm/MC/MCAsmInfo.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000012#include "llvm/MC/MCObjectFileInfo.h"
Evan Cheng0e6a0522011-07-18 20:57:22 +000013#include "llvm/MC/MCRegisterInfo.h"
Chris Lattnerf0559e42010-04-08 20:30:37 +000014#include "llvm/MC/MCSectionMachO.h"
Chris Lattner74aae472010-04-08 21:26:26 +000015#include "llvm/MC/MCSectionELF.h"
Chris Lattnereb40a0f2010-05-07 17:17:41 +000016#include "llvm/MC/MCSectionCOFF.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000017#include "llvm/MC/MCSymbol.h"
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +000018#include "llvm/MC/MCLabel.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000019#include "llvm/MC/MCDwarf.h"
Chris Lattner7c5b0212009-10-19 22:49:00 +000020#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/Twine.h"
Rafael Espindolac85dca62011-01-23 04:28:49 +000022#include "llvm/Support/ELF.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000023using namespace llvm;
24
Chris Lattnerf0559e42010-04-08 20:30:37 +000025typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner74aae472010-04-08 21:26:26 +000026typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000027typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
Chris Lattnerf0559e42010-04-08 20:30:37 +000028
29
Evan Cheng0e6a0522011-07-18 20:57:22 +000030MCContext::MCContext(const MCAsmInfo &mai, const MCRegisterInfo &mri,
Evan Cheng203576a2011-07-20 19:50:42 +000031 const MCObjectFileInfo *mofi) :
32 MAI(mai), MRI(mri), MOFI(mofi),
Eli Friedmanf4387d92011-04-18 05:02:31 +000033 Allocator(), Symbols(Allocator), UsedNames(Allocator),
34 NextUniqueID(0),
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +000035 CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0),
36 AllowTemporaryLabels(true) {
Chris Lattnerf0559e42010-04-08 20:30:37 +000037 MachOUniquingMap = 0;
Chris Lattner74aae472010-04-08 21:26:26 +000038 ELFUniquingMap = 0;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000039 COFFUniquingMap = 0;
Kevin Enderbyf187ac52010-06-28 21:45:58 +000040
41 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
42 SecureLog = 0;
43 SecureLogUsed = false;
Kevin Enderbyc1840b32010-08-24 20:32:42 +000044
45 DwarfLocSeen = false;
Kevin Enderby613b7572011-11-01 22:27:22 +000046 GenDwarfForAssembly = false;
47 GenDwarfFileNumber = 0;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000048}
49
50MCContext::~MCContext() {
Chris Lattnerf0559e42010-04-08 20:30:37 +000051 // NOTE: The symbols are all allocated out of a bump pointer allocator,
Chris Lattnerc9d31522009-08-13 00:21:53 +000052 // we don't need to free them here.
Michael J. Spencer326990f2010-11-26 04:16:08 +000053
Chris Lattnerf0559e42010-04-08 20:30:37 +000054 // If we have the MachO uniquing map, free it.
55 delete (MachOUniqueMapTy*)MachOUniquingMap;
Chris Lattner74aae472010-04-08 21:26:26 +000056 delete (ELFUniqueMapTy*)ELFUniquingMap;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000057 delete (COFFUniqueMapTy*)COFFUniquingMap;
Kevin Enderbyf187ac52010-06-28 21:45:58 +000058
59 // If the stream for the .secure_log_unique directive was created free it.
60 delete (raw_ostream*)SecureLog;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000061}
62
Chris Lattnerf0559e42010-04-08 20:30:37 +000063//===----------------------------------------------------------------------===//
64// Symbol Manipulation
65//===----------------------------------------------------------------------===//
66
Chris Lattner9b97a732010-03-30 18:10:53 +000067MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattner00685bb2010-03-10 01:29:27 +000068 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Michael J. Spencer326990f2010-11-26 04:16:08 +000069
Chris Lattnerc28cc092010-03-15 06:15:35 +000070 // Do the lookup and get the entire StringMapEntry. We want access to the
71 // key if we are creating the entry.
72 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
Rafael Espindola9f447242010-12-01 20:46:11 +000073 MCSymbol *Sym = Entry.getValue();
74
75 if (Sym)
76 return Sym;
77
78 Sym = CreateSymbol(Name);
79 Entry.setValue(Sym);
80 return Sym;
81}
82
83MCSymbol *MCContext::CreateSymbol(StringRef Name) {
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +000084 // Determine whether this is an assembler temporary or normal label, if used.
85 bool isTemporary = false;
86 if (AllowTemporaryLabels)
87 isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
Rafael Espindola9f447242010-12-01 20:46:11 +000088
89 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
90 if (NameEntry->getValue()) {
91 assert(isTemporary && "Cannot rename non temporary symbols");
Benjamin Kramerc18214a2011-04-09 11:26:27 +000092 SmallString<128> NewName = Name;
Rafael Espindola9f447242010-12-01 20:46:11 +000093 do {
Benjamin Kramerc18214a2011-04-09 11:26:27 +000094 NewName.resize(Name.size());
95 raw_svector_ostream(NewName) << NextUniqueID++;
96 NameEntry = &UsedNames.GetOrCreateValue(NewName);
Rafael Espindola9f447242010-12-01 20:46:11 +000097 } while (NameEntry->getValue());
98 }
99 NameEntry->setValue(true);
Chris Lattnerc69485e2009-06-24 04:31:49 +0000100
Chris Lattnerc28cc092010-03-15 06:15:35 +0000101 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
Rafael Espindola9f447242010-12-01 20:46:11 +0000102 // to the copy of the string that is embedded in the UsedNames entry.
103 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
104
105 return Result;
Chris Lattnerc69485e2009-06-24 04:31:49 +0000106}
107
Chris Lattner9b97a732010-03-30 18:10:53 +0000108MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner7c5b0212009-10-19 22:49:00 +0000109 SmallString<128> NameSV;
110 Name.toVector(NameSV);
Chris Lattner9b97a732010-03-30 18:10:53 +0000111 return GetOrCreateSymbol(NameSV.str());
Chris Lattner7c5b0212009-10-19 22:49:00 +0000112}
113
Chris Lattner1d72a762010-03-14 08:23:30 +0000114MCSymbol *MCContext::CreateTempSymbol() {
Rafael Espindola9f447242010-12-01 20:46:11 +0000115 SmallString<128> NameSV;
Benjamin Kramerc18214a2011-04-09 11:26:27 +0000116 raw_svector_ostream(NameSV)
117 << MAI.getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
Rafael Espindola9f447242010-12-01 20:46:11 +0000118 return CreateSymbol(NameSV);
Chris Lattner1d72a762010-03-14 08:23:30 +0000119}
120
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000121unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000122 MCLabel *&Label = Instances[LocalLabelVal];
123 if (!Label)
124 Label = new (*this) MCLabel(0);
125 return Label->incInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000126}
127
128unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000129 MCLabel *&Label = Instances[LocalLabelVal];
130 if (!Label)
131 Label = new (*this) MCLabel(0);
132 return Label->getInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000133}
134
135MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
136 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
137 Twine(LocalLabelVal) +
138 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000139 Twine(NextInstance(LocalLabelVal)));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000140}
141MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
142 int bORf) {
143 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
144 Twine(LocalLabelVal) +
145 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000146 Twine(GetInstance(LocalLabelVal) + bORf));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000147}
148
Daniel Dunbar2928c832009-11-06 10:58:06 +0000149MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000150 return Symbols.lookup(Name);
151}
Chris Lattnerf0559e42010-04-08 20:30:37 +0000152
153//===----------------------------------------------------------------------===//
154// Section Management
155//===----------------------------------------------------------------------===//
156
157const MCSectionMachO *MCContext::
158getMachOSection(StringRef Segment, StringRef Section,
159 unsigned TypeAndAttributes,
160 unsigned Reserved2, SectionKind Kind) {
Michael J. Spencer326990f2010-11-26 04:16:08 +0000161
Chris Lattnerf0559e42010-04-08 20:30:37 +0000162 // We unique sections by their segment/section pair. The returned section
163 // may not have the same flags as the requested section, if so this should be
164 // diagnosed by the client as an error.
Michael J. Spencer326990f2010-11-26 04:16:08 +0000165
Chris Lattnerf0559e42010-04-08 20:30:37 +0000166 // Create the map if it doesn't already exist.
167 if (MachOUniquingMap == 0)
168 MachOUniquingMap = new MachOUniqueMapTy();
169 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000170
Chris Lattnerf0559e42010-04-08 20:30:37 +0000171 // Form the name to look up.
172 SmallString<64> Name;
173 Name += Segment;
174 Name.push_back(',');
175 Name += Section;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000176
Chris Lattnerf0559e42010-04-08 20:30:37 +0000177 // Do the lookup, if we have a hit, return it.
178 const MCSectionMachO *&Entry = Map[Name.str()];
179 if (Entry) return Entry;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000180
Chris Lattnerf0559e42010-04-08 20:30:37 +0000181 // Otherwise, return a new section.
Chris Lattner74aae472010-04-08 21:26:26 +0000182 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
183 Reserved2, Kind);
Chris Lattnerf0559e42010-04-08 20:30:37 +0000184}
Chris Lattner74aae472010-04-08 21:26:26 +0000185
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000186const MCSectionELF *MCContext::
Chris Lattner74aae472010-04-08 21:26:26 +0000187getELFSection(StringRef Section, unsigned Type, unsigned Flags,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000188 SectionKind Kind) {
189 return getELFSection(Section, Type, Flags, Kind, 0, "");
190}
191
192const MCSectionELF *MCContext::
193getELFSection(StringRef Section, unsigned Type, unsigned Flags,
194 SectionKind Kind, unsigned EntrySize, StringRef Group) {
Chris Lattner74aae472010-04-08 21:26:26 +0000195 if (ELFUniquingMap == 0)
196 ELFUniquingMap = new ELFUniqueMapTy();
197 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000198
Chris Lattner74aae472010-04-08 21:26:26 +0000199 // Do the lookup, if we have a hit, return it.
200 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
201 if (Entry.getValue()) return Entry.getValue();
Michael J. Spencer326990f2010-11-26 04:16:08 +0000202
Jan Wen Voung186e7a02010-09-30 05:59:22 +0000203 // Possibly refine the entry size first.
204 if (!EntrySize) {
205 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
206 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000207
208 MCSymbol *GroupSym = NULL;
209 if (!Group.empty())
210 GroupSym = GetOrCreateSymbol(Group);
211
Chris Lattner74aae472010-04-08 21:26:26 +0000212 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000213 Kind, EntrySize, GroupSym);
Chris Lattner74aae472010-04-08 21:26:26 +0000214 Entry.setValue(Result);
215 return Result;
216}
217
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000218const MCSectionELF *MCContext::CreateELFGroupSection() {
219 MCSectionELF *Result =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000220 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000221 SectionKind::getReadOnly(), 4, NULL);
222 return Result;
223}
224
Chris Lattner6e5ce282010-05-07 21:49:09 +0000225const MCSection *MCContext::getCOFFSection(StringRef Section,
226 unsigned Characteristics,
227 int Selection,
228 SectionKind Kind) {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000229 if (COFFUniquingMap == 0)
230 COFFUniquingMap = new COFFUniqueMapTy();
231 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000232
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000233 // Do the lookup, if we have a hit, return it.
234 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
235 if (Entry.getValue()) return Entry.getValue();
Michael J. Spencer326990f2010-11-26 04:16:08 +0000236
Chris Lattner6e5ce282010-05-07 21:49:09 +0000237 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
238 Characteristics,
239 Selection, Kind);
Michael J. Spencer326990f2010-11-26 04:16:08 +0000240
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000241 Entry.setValue(Result);
242 return Result;
243}
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000244
245//===----------------------------------------------------------------------===//
246// Dwarf Management
247//===----------------------------------------------------------------------===//
248
249/// GetDwarfFile - takes a file name an number to place in the dwarf file and
250/// directory tables. If the file number has already been allocated it is an
251/// error and zero is returned and the client reports the error, else the
252/// allocated file number is returned. The file numbers may be in any order.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000253unsigned MCContext::GetDwarfFile(StringRef Directory, StringRef FileName,
254 unsigned FileNumber) {
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000255 // TODO: a FileNumber of zero says to use the next available file number.
256 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
257 // to not be less than one. This needs to be change to be not less than zero.
258
259 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
260 if (FileNumber >= MCDwarfFiles.size()) {
261 MCDwarfFiles.resize(FileNumber + 1);
262 } else {
263 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
264 if (ExistingFile)
265 // It is an error to use see the same number more than once.
266 return 0;
267 }
268
269 // Get the new MCDwarfFile slot for this FileNumber.
270 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
271
Nick Lewycky44d798d2011-10-17 23:05:28 +0000272 if (Directory.empty()) {
273 // Separate the directory part from the basename of the FileName.
274 std::pair<StringRef, StringRef> Slash = FileName.rsplit('/');
275 Directory = Slash.second;
276 if (!Directory.empty())
277 FileName = Slash.first;
278 }
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000279
280 // Find or make a entry in the MCDwarfDirs vector for this Directory.
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000281 // Capture directory name.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000282 unsigned DirIndex;
283 if (Directory.empty()) {
284 // For FileNames with no directories a DirIndex of 0 is used.
285 DirIndex = 0;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000286 } else {
Nick Lewycky18ad76b2011-10-12 20:20:48 +0000287 DirIndex = 0;
288 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000289 if (Directory == MCDwarfDirs[DirIndex])
Kevin Enderby232ab942010-08-31 22:55:11 +0000290 break;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000291 }
292 if (DirIndex >= MCDwarfDirs.size()) {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000293 char *Buf = static_cast<char *>(Allocate(Directory.size()));
294 memcpy(Buf, Directory.data(), Directory.size());
295 MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000296 }
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000297 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
298 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
Nick Lewycky44d798d2011-10-17 23:05:28 +0000299 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
300 // are stored at MCDwarfFiles[FileNumber].Name .
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000301 DirIndex++;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000302 }
Michael J. Spencer326990f2010-11-26 04:16:08 +0000303
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000304 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
305 // vector.
Nick Lewycky44d798d2011-10-17 23:05:28 +0000306 char *Buf = static_cast<char *>(Allocate(FileName.size()));
307 memcpy(Buf, FileName.data(), FileName.size());
308 File = new (*this) MCDwarfFile(StringRef(Buf, FileName.size()), DirIndex);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000309
310 // return the allocated FileNumber.
311 return FileNumber;
312}
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000313
Kevin Enderby3f55c242010-10-04 20:17:24 +0000314/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000315/// currently is assigned and false otherwise.
Kevin Enderby3f55c242010-10-04 20:17:24 +0000316bool MCContext::isValidDwarfFileNumber(unsigned FileNumber) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000317 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
318 return false;
319
Kevin Enderby3f55c242010-10-04 20:17:24 +0000320 return MCDwarfFiles[FileNumber] != 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000321}