blob: d3a4a27cefdebfe629aa5166bc5ef9caa5234fe1 [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"
Chris Lattnerf0559e42010-04-08 20:30:37 +000012#include "llvm/MC/MCSectionMachO.h"
Chris Lattner74aae472010-04-08 21:26:26 +000013#include "llvm/MC/MCSectionELF.h"
Chris Lattnereb40a0f2010-05-07 17:17:41 +000014#include "llvm/MC/MCSectionCOFF.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000015#include "llvm/MC/MCSymbol.h"
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +000016#include "llvm/MC/MCLabel.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000017#include "llvm/MC/MCDwarf.h"
Chris Lattner7c5b0212009-10-19 22:49:00 +000018#include "llvm/ADT/SmallString.h"
19#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000020using namespace llvm;
21
Chris Lattnerf0559e42010-04-08 20:30:37 +000022typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner74aae472010-04-08 21:26:26 +000023typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000024typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
Chris Lattnerf0559e42010-04-08 20:30:37 +000025
26
Rafael Espindola1c952b92010-12-09 23:48:29 +000027MCContext::MCContext(const MCAsmInfo &mai, const TargetAsmInfo *tai) :
28 MAI(mai), TAI(tai), NextUniqueID(0),
29 CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0) {
Chris Lattnerf0559e42010-04-08 20:30:37 +000030 MachOUniquingMap = 0;
Chris Lattner74aae472010-04-08 21:26:26 +000031 ELFUniquingMap = 0;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000032 COFFUniquingMap = 0;
Kevin Enderbyf187ac52010-06-28 21:45:58 +000033
34 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
35 SecureLog = 0;
36 SecureLogUsed = false;
Kevin Enderbyc1840b32010-08-24 20:32:42 +000037
38 DwarfLocSeen = false;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000039}
40
41MCContext::~MCContext() {
Chris Lattnerf0559e42010-04-08 20:30:37 +000042 // NOTE: The symbols are all allocated out of a bump pointer allocator,
Chris Lattnerc9d31522009-08-13 00:21:53 +000043 // we don't need to free them here.
Michael J. Spencer326990f2010-11-26 04:16:08 +000044
Chris Lattnerf0559e42010-04-08 20:30:37 +000045 // If we have the MachO uniquing map, free it.
46 delete (MachOUniqueMapTy*)MachOUniquingMap;
Chris Lattner74aae472010-04-08 21:26:26 +000047 delete (ELFUniqueMapTy*)ELFUniquingMap;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000048 delete (COFFUniqueMapTy*)COFFUniquingMap;
Kevin Enderbyf187ac52010-06-28 21:45:58 +000049
50 // If the stream for the .secure_log_unique directive was created free it.
51 delete (raw_ostream*)SecureLog;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000052}
53
Chris Lattnerf0559e42010-04-08 20:30:37 +000054//===----------------------------------------------------------------------===//
55// Symbol Manipulation
56//===----------------------------------------------------------------------===//
57
Chris Lattner9b97a732010-03-30 18:10:53 +000058MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattner00685bb2010-03-10 01:29:27 +000059 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Michael J. Spencer326990f2010-11-26 04:16:08 +000060
Chris Lattnerc28cc092010-03-15 06:15:35 +000061 // Do the lookup and get the entire StringMapEntry. We want access to the
62 // key if we are creating the entry.
63 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
Rafael Espindola9f447242010-12-01 20:46:11 +000064 MCSymbol *Sym = Entry.getValue();
65
66 if (Sym)
67 return Sym;
68
69 Sym = CreateSymbol(Name);
70 Entry.setValue(Sym);
71 return Sym;
72}
73
74MCSymbol *MCContext::CreateSymbol(StringRef Name) {
75 // Determine whether this is an assembler temporary or normal label.
76 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
77
78 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
79 if (NameEntry->getValue()) {
80 assert(isTemporary && "Cannot rename non temporary symbols");
81 SmallString<128> NewName;
82 do {
83 Twine T = Name + Twine(NextUniqueID++);
84 T.toVector(NewName);
85 StringRef foo = NewName;
86 NameEntry = &UsedNames.GetOrCreateValue(foo);
87 } while (NameEntry->getValue());
88 }
89 NameEntry->setValue(true);
Chris Lattnerc69485e2009-06-24 04:31:49 +000090
Chris Lattnerc28cc092010-03-15 06:15:35 +000091 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
Rafael Espindola9f447242010-12-01 20:46:11 +000092 // to the copy of the string that is embedded in the UsedNames entry.
93 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
94
95 return Result;
Chris Lattnerc69485e2009-06-24 04:31:49 +000096}
97
Chris Lattner9b97a732010-03-30 18:10:53 +000098MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner7c5b0212009-10-19 22:49:00 +000099 SmallString<128> NameSV;
100 Name.toVector(NameSV);
Chris Lattner9b97a732010-03-30 18:10:53 +0000101 return GetOrCreateSymbol(NameSV.str());
Chris Lattner7c5b0212009-10-19 22:49:00 +0000102}
103
Chris Lattner1d72a762010-03-14 08:23:30 +0000104MCSymbol *MCContext::CreateTempSymbol() {
Rafael Espindola9f447242010-12-01 20:46:11 +0000105 SmallString<128> NameSV;
106 Twine Name = Twine(MAI.getPrivateGlobalPrefix()) + "tmp" +
107 Twine(NextUniqueID++);
108 Name.toVector(NameSV);
109 return CreateSymbol(NameSV);
Chris Lattner1d72a762010-03-14 08:23:30 +0000110}
111
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000112unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000113 MCLabel *&Label = Instances[LocalLabelVal];
114 if (!Label)
115 Label = new (*this) MCLabel(0);
116 return Label->incInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000117}
118
119unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000120 MCLabel *&Label = Instances[LocalLabelVal];
121 if (!Label)
122 Label = new (*this) MCLabel(0);
123 return Label->getInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000124}
125
126MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
127 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
128 Twine(LocalLabelVal) +
129 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000130 Twine(NextInstance(LocalLabelVal)));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000131}
132MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
133 int bORf) {
134 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
135 Twine(LocalLabelVal) +
136 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000137 Twine(GetInstance(LocalLabelVal) + bORf));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000138}
139
Daniel Dunbar2928c832009-11-06 10:58:06 +0000140MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000141 return Symbols.lookup(Name);
142}
Chris Lattnerf0559e42010-04-08 20:30:37 +0000143
144//===----------------------------------------------------------------------===//
145// Section Management
146//===----------------------------------------------------------------------===//
147
148const MCSectionMachO *MCContext::
149getMachOSection(StringRef Segment, StringRef Section,
150 unsigned TypeAndAttributes,
151 unsigned Reserved2, SectionKind Kind) {
Michael J. Spencer326990f2010-11-26 04:16:08 +0000152
Chris Lattnerf0559e42010-04-08 20:30:37 +0000153 // We unique sections by their segment/section pair. The returned section
154 // may not have the same flags as the requested section, if so this should be
155 // diagnosed by the client as an error.
Michael J. Spencer326990f2010-11-26 04:16:08 +0000156
Chris Lattnerf0559e42010-04-08 20:30:37 +0000157 // Create the map if it doesn't already exist.
158 if (MachOUniquingMap == 0)
159 MachOUniquingMap = new MachOUniqueMapTy();
160 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000161
Chris Lattnerf0559e42010-04-08 20:30:37 +0000162 // Form the name to look up.
163 SmallString<64> Name;
164 Name += Segment;
165 Name.push_back(',');
166 Name += Section;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000167
Chris Lattnerf0559e42010-04-08 20:30:37 +0000168 // Do the lookup, if we have a hit, return it.
169 const MCSectionMachO *&Entry = Map[Name.str()];
170 if (Entry) return Entry;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000171
Chris Lattnerf0559e42010-04-08 20:30:37 +0000172 // Otherwise, return a new section.
Chris Lattner74aae472010-04-08 21:26:26 +0000173 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
174 Reserved2, Kind);
Chris Lattnerf0559e42010-04-08 20:30:37 +0000175}
Chris Lattner74aae472010-04-08 21:26:26 +0000176
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000177const MCSectionELF *MCContext::
Chris Lattner74aae472010-04-08 21:26:26 +0000178getELFSection(StringRef Section, unsigned Type, unsigned Flags,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000179 SectionKind Kind) {
180 return getELFSection(Section, Type, Flags, Kind, 0, "");
181}
182
183const MCSectionELF *MCContext::
184getELFSection(StringRef Section, unsigned Type, unsigned Flags,
185 SectionKind Kind, unsigned EntrySize, StringRef Group) {
Chris Lattner74aae472010-04-08 21:26:26 +0000186 if (ELFUniquingMap == 0)
187 ELFUniquingMap = new ELFUniqueMapTy();
188 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000189
Chris Lattner74aae472010-04-08 21:26:26 +0000190 // Do the lookup, if we have a hit, return it.
191 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
192 if (Entry.getValue()) return Entry.getValue();
Michael J. Spencer326990f2010-11-26 04:16:08 +0000193
Jan Wen Voung186e7a02010-09-30 05:59:22 +0000194 // Possibly refine the entry size first.
195 if (!EntrySize) {
196 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
197 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000198
199 MCSymbol *GroupSym = NULL;
200 if (!Group.empty())
201 GroupSym = GetOrCreateSymbol(Group);
202
Chris Lattner74aae472010-04-08 21:26:26 +0000203 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000204 Kind, EntrySize, GroupSym);
Chris Lattner74aae472010-04-08 21:26:26 +0000205 Entry.setValue(Result);
206 return Result;
207}
208
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000209const MCSectionELF *MCContext::CreateELFGroupSection() {
210 MCSectionELF *Result =
211 new (*this) MCSectionELF(".group", MCSectionELF::SHT_GROUP, 0,
212 SectionKind::getReadOnly(), 4, NULL);
213 return Result;
214}
215
Chris Lattner6e5ce282010-05-07 21:49:09 +0000216const MCSection *MCContext::getCOFFSection(StringRef Section,
217 unsigned Characteristics,
218 int Selection,
219 SectionKind Kind) {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000220 if (COFFUniquingMap == 0)
221 COFFUniquingMap = new COFFUniqueMapTy();
222 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000223
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000224 // Do the lookup, if we have a hit, return it.
225 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
226 if (Entry.getValue()) return Entry.getValue();
Michael J. Spencer326990f2010-11-26 04:16:08 +0000227
Chris Lattner6e5ce282010-05-07 21:49:09 +0000228 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
229 Characteristics,
230 Selection, Kind);
Michael J. Spencer326990f2010-11-26 04:16:08 +0000231
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000232 Entry.setValue(Result);
233 return Result;
234}
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000235
236//===----------------------------------------------------------------------===//
237// Dwarf Management
238//===----------------------------------------------------------------------===//
239
240/// GetDwarfFile - takes a file name an number to place in the dwarf file and
241/// directory tables. If the file number has already been allocated it is an
242/// error and zero is returned and the client reports the error, else the
243/// allocated file number is returned. The file numbers may be in any order.
244unsigned MCContext::GetDwarfFile(StringRef FileName, unsigned FileNumber) {
245 // TODO: a FileNumber of zero says to use the next available file number.
246 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
247 // to not be less than one. This needs to be change to be not less than zero.
248
249 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
250 if (FileNumber >= MCDwarfFiles.size()) {
251 MCDwarfFiles.resize(FileNumber + 1);
252 } else {
253 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
254 if (ExistingFile)
255 // It is an error to use see the same number more than once.
256 return 0;
257 }
258
259 // Get the new MCDwarfFile slot for this FileNumber.
260 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
261
262 // Separate the directory part from the basename of the FileName.
263 std::pair<StringRef, StringRef> Slash = FileName.rsplit('/');
264
265 // Find or make a entry in the MCDwarfDirs vector for this Directory.
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000266 StringRef Name;
267 unsigned DirIndex;
268 // Capture directory name.
269 if (Slash.second.empty()) {
270 Name = Slash.first;
271 DirIndex = 0; // For FileNames with no directories a DirIndex of 0 is used.
272 } else {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000273 StringRef Directory = Slash.first;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000274 Name = Slash.second;
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000275 for (DirIndex = 0; DirIndex < MCDwarfDirs.size(); DirIndex++) {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000276 if (Directory == MCDwarfDirs[DirIndex])
Kevin Enderby232ab942010-08-31 22:55:11 +0000277 break;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000278 }
279 if (DirIndex >= MCDwarfDirs.size()) {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000280 char *Buf = static_cast<char *>(Allocate(Directory.size()));
281 memcpy(Buf, Directory.data(), Directory.size());
282 MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000283 }
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000284 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
285 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
286 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames are
287 // stored at MCDwarfFiles[FileNumber].Name .
288 DirIndex++;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000289 }
Michael J. Spencer326990f2010-11-26 04:16:08 +0000290
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000291 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
292 // vector.
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000293 char *Buf = static_cast<char *>(Allocate(Name.size()));
294 memcpy(Buf, Name.data(), Name.size());
295 File = new (*this) MCDwarfFile(StringRef(Buf, Name.size()), DirIndex);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000296
297 // return the allocated FileNumber.
298 return FileNumber;
299}
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000300
Kevin Enderby3f55c242010-10-04 20:17:24 +0000301/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000302/// currently is assigned and false otherwise.
Kevin Enderby3f55c242010-10-04 20:17:24 +0000303bool MCContext::isValidDwarfFileNumber(unsigned FileNumber) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000304 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
305 return false;
306
Kevin Enderby3f55c242010-10-04 20:17:24 +0000307 return MCDwarfFiles[FileNumber] != 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000308}