blob: 839c08d8a26875ada0889a18094433c4625ce778 [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"
Rafael Espindola89b93722010-12-10 07:39:47 +000018#include "llvm/Target/TargetAsmInfo.h"
Chris Lattner7c5b0212009-10-19 22:49:00 +000019#include "llvm/ADT/SmallString.h"
20#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000021using namespace llvm;
22
Chris Lattnerf0559e42010-04-08 20:30:37 +000023typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner74aae472010-04-08 21:26:26 +000024typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000025typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
Chris Lattnerf0559e42010-04-08 20:30:37 +000026
27
Rafael Espindola89b93722010-12-10 07:39:47 +000028MCContext::MCContext(const MCAsmInfo &mai, const TargetAsmInfo *tai) :
29 MAI(mai), TAI(tai), NextUniqueID(0),
30 CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0) {
Chris Lattnerf0559e42010-04-08 20:30:37 +000031 MachOUniquingMap = 0;
Chris Lattner74aae472010-04-08 21:26:26 +000032 ELFUniquingMap = 0;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000033 COFFUniquingMap = 0;
Kevin Enderbyf187ac52010-06-28 21:45:58 +000034
35 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
36 SecureLog = 0;
37 SecureLogUsed = false;
Kevin Enderbyc1840b32010-08-24 20:32:42 +000038
39 DwarfLocSeen = false;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000040}
41
42MCContext::~MCContext() {
Chris Lattnerf0559e42010-04-08 20:30:37 +000043 // NOTE: The symbols are all allocated out of a bump pointer allocator,
Chris Lattnerc9d31522009-08-13 00:21:53 +000044 // we don't need to free them here.
Michael J. Spencer326990f2010-11-26 04:16:08 +000045
Chris Lattnerf0559e42010-04-08 20:30:37 +000046 // If we have the MachO uniquing map, free it.
47 delete (MachOUniqueMapTy*)MachOUniquingMap;
Chris Lattner74aae472010-04-08 21:26:26 +000048 delete (ELFUniqueMapTy*)ELFUniquingMap;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000049 delete (COFFUniqueMapTy*)COFFUniquingMap;
Kevin Enderbyf187ac52010-06-28 21:45:58 +000050
51 // If the stream for the .secure_log_unique directive was created free it.
52 delete (raw_ostream*)SecureLog;
Rafael Espindola89b93722010-12-10 07:39:47 +000053
54 delete TAI;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000055}
56
Chris Lattnerf0559e42010-04-08 20:30:37 +000057//===----------------------------------------------------------------------===//
58// Symbol Manipulation
59//===----------------------------------------------------------------------===//
60
Chris Lattner9b97a732010-03-30 18:10:53 +000061MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattner00685bb2010-03-10 01:29:27 +000062 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Michael J. Spencer326990f2010-11-26 04:16:08 +000063
Chris Lattnerc28cc092010-03-15 06:15:35 +000064 // Do the lookup and get the entire StringMapEntry. We want access to the
65 // key if we are creating the entry.
66 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
Rafael Espindola9f447242010-12-01 20:46:11 +000067 MCSymbol *Sym = Entry.getValue();
68
69 if (Sym)
70 return Sym;
71
72 Sym = CreateSymbol(Name);
73 Entry.setValue(Sym);
74 return Sym;
75}
76
77MCSymbol *MCContext::CreateSymbol(StringRef Name) {
78 // Determine whether this is an assembler temporary or normal label.
79 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
80
81 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
82 if (NameEntry->getValue()) {
83 assert(isTemporary && "Cannot rename non temporary symbols");
84 SmallString<128> NewName;
85 do {
86 Twine T = Name + Twine(NextUniqueID++);
87 T.toVector(NewName);
88 StringRef foo = NewName;
89 NameEntry = &UsedNames.GetOrCreateValue(foo);
90 } while (NameEntry->getValue());
91 }
92 NameEntry->setValue(true);
Chris Lattnerc69485e2009-06-24 04:31:49 +000093
Chris Lattnerc28cc092010-03-15 06:15:35 +000094 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
Rafael Espindola9f447242010-12-01 20:46:11 +000095 // to the copy of the string that is embedded in the UsedNames entry.
96 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
97
98 return Result;
Chris Lattnerc69485e2009-06-24 04:31:49 +000099}
100
Chris Lattner9b97a732010-03-30 18:10:53 +0000101MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner7c5b0212009-10-19 22:49:00 +0000102 SmallString<128> NameSV;
103 Name.toVector(NameSV);
Chris Lattner9b97a732010-03-30 18:10:53 +0000104 return GetOrCreateSymbol(NameSV.str());
Chris Lattner7c5b0212009-10-19 22:49:00 +0000105}
106
Chris Lattner1d72a762010-03-14 08:23:30 +0000107MCSymbol *MCContext::CreateTempSymbol() {
Rafael Espindola9f447242010-12-01 20:46:11 +0000108 SmallString<128> NameSV;
109 Twine Name = Twine(MAI.getPrivateGlobalPrefix()) + "tmp" +
110 Twine(NextUniqueID++);
111 Name.toVector(NameSV);
112 return CreateSymbol(NameSV);
Chris Lattner1d72a762010-03-14 08:23:30 +0000113}
114
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000115unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000116 MCLabel *&Label = Instances[LocalLabelVal];
117 if (!Label)
118 Label = new (*this) MCLabel(0);
119 return Label->incInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000120}
121
122unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000123 MCLabel *&Label = Instances[LocalLabelVal];
124 if (!Label)
125 Label = new (*this) MCLabel(0);
126 return Label->getInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000127}
128
129MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
130 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
131 Twine(LocalLabelVal) +
132 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000133 Twine(NextInstance(LocalLabelVal)));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000134}
135MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
136 int bORf) {
137 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
138 Twine(LocalLabelVal) +
139 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000140 Twine(GetInstance(LocalLabelVal) + bORf));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000141}
142
Daniel Dunbar2928c832009-11-06 10:58:06 +0000143MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000144 return Symbols.lookup(Name);
145}
Chris Lattnerf0559e42010-04-08 20:30:37 +0000146
147//===----------------------------------------------------------------------===//
148// Section Management
149//===----------------------------------------------------------------------===//
150
151const MCSectionMachO *MCContext::
152getMachOSection(StringRef Segment, StringRef Section,
153 unsigned TypeAndAttributes,
154 unsigned Reserved2, SectionKind Kind) {
Michael J. Spencer326990f2010-11-26 04:16:08 +0000155
Chris Lattnerf0559e42010-04-08 20:30:37 +0000156 // We unique sections by their segment/section pair. The returned section
157 // may not have the same flags as the requested section, if so this should be
158 // diagnosed by the client as an error.
Michael J. Spencer326990f2010-11-26 04:16:08 +0000159
Chris Lattnerf0559e42010-04-08 20:30:37 +0000160 // Create the map if it doesn't already exist.
161 if (MachOUniquingMap == 0)
162 MachOUniquingMap = new MachOUniqueMapTy();
163 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000164
Chris Lattnerf0559e42010-04-08 20:30:37 +0000165 // Form the name to look up.
166 SmallString<64> Name;
167 Name += Segment;
168 Name.push_back(',');
169 Name += Section;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000170
Chris Lattnerf0559e42010-04-08 20:30:37 +0000171 // Do the lookup, if we have a hit, return it.
172 const MCSectionMachO *&Entry = Map[Name.str()];
173 if (Entry) return Entry;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000174
Chris Lattnerf0559e42010-04-08 20:30:37 +0000175 // Otherwise, return a new section.
Chris Lattner74aae472010-04-08 21:26:26 +0000176 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
177 Reserved2, Kind);
Chris Lattnerf0559e42010-04-08 20:30:37 +0000178}
Chris Lattner74aae472010-04-08 21:26:26 +0000179
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000180const MCSectionELF *MCContext::
Chris Lattner74aae472010-04-08 21:26:26 +0000181getELFSection(StringRef Section, unsigned Type, unsigned Flags,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000182 SectionKind Kind) {
183 return getELFSection(Section, Type, Flags, Kind, 0, "");
184}
185
186const MCSectionELF *MCContext::
187getELFSection(StringRef Section, unsigned Type, unsigned Flags,
188 SectionKind Kind, unsigned EntrySize, StringRef Group) {
Chris Lattner74aae472010-04-08 21:26:26 +0000189 if (ELFUniquingMap == 0)
190 ELFUniquingMap = new ELFUniqueMapTy();
191 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000192
Chris Lattner74aae472010-04-08 21:26:26 +0000193 // Do the lookup, if we have a hit, return it.
194 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
195 if (Entry.getValue()) return Entry.getValue();
Michael J. Spencer326990f2010-11-26 04:16:08 +0000196
Jan Wen Voung186e7a02010-09-30 05:59:22 +0000197 // Possibly refine the entry size first.
198 if (!EntrySize) {
199 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
200 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000201
202 MCSymbol *GroupSym = NULL;
203 if (!Group.empty())
204 GroupSym = GetOrCreateSymbol(Group);
205
Chris Lattner74aae472010-04-08 21:26:26 +0000206 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000207 Kind, EntrySize, GroupSym);
Chris Lattner74aae472010-04-08 21:26:26 +0000208 Entry.setValue(Result);
209 return Result;
210}
211
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000212const MCSectionELF *MCContext::CreateELFGroupSection() {
213 MCSectionELF *Result =
214 new (*this) MCSectionELF(".group", MCSectionELF::SHT_GROUP, 0,
215 SectionKind::getReadOnly(), 4, NULL);
216 return Result;
217}
218
Chris Lattner6e5ce282010-05-07 21:49:09 +0000219const MCSection *MCContext::getCOFFSection(StringRef Section,
220 unsigned Characteristics,
221 int Selection,
222 SectionKind Kind) {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000223 if (COFFUniquingMap == 0)
224 COFFUniquingMap = new COFFUniqueMapTy();
225 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000226
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000227 // Do the lookup, if we have a hit, return it.
228 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
229 if (Entry.getValue()) return Entry.getValue();
Michael J. Spencer326990f2010-11-26 04:16:08 +0000230
Chris Lattner6e5ce282010-05-07 21:49:09 +0000231 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
232 Characteristics,
233 Selection, Kind);
Michael J. Spencer326990f2010-11-26 04:16:08 +0000234
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000235 Entry.setValue(Result);
236 return Result;
237}
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000238
239//===----------------------------------------------------------------------===//
240// Dwarf Management
241//===----------------------------------------------------------------------===//
242
243/// GetDwarfFile - takes a file name an number to place in the dwarf file and
244/// directory tables. If the file number has already been allocated it is an
245/// error and zero is returned and the client reports the error, else the
246/// allocated file number is returned. The file numbers may be in any order.
247unsigned MCContext::GetDwarfFile(StringRef FileName, unsigned FileNumber) {
248 // TODO: a FileNumber of zero says to use the next available file number.
249 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
250 // to not be less than one. This needs to be change to be not less than zero.
251
252 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
253 if (FileNumber >= MCDwarfFiles.size()) {
254 MCDwarfFiles.resize(FileNumber + 1);
255 } else {
256 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
257 if (ExistingFile)
258 // It is an error to use see the same number more than once.
259 return 0;
260 }
261
262 // Get the new MCDwarfFile slot for this FileNumber.
263 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
264
265 // Separate the directory part from the basename of the FileName.
266 std::pair<StringRef, StringRef> Slash = FileName.rsplit('/');
267
268 // Find or make a entry in the MCDwarfDirs vector for this Directory.
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000269 StringRef Name;
270 unsigned DirIndex;
271 // Capture directory name.
272 if (Slash.second.empty()) {
273 Name = Slash.first;
274 DirIndex = 0; // For FileNames with no directories a DirIndex of 0 is used.
275 } else {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000276 StringRef Directory = Slash.first;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000277 Name = Slash.second;
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000278 for (DirIndex = 0; DirIndex < MCDwarfDirs.size(); DirIndex++) {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000279 if (Directory == MCDwarfDirs[DirIndex])
Kevin Enderby232ab942010-08-31 22:55:11 +0000280 break;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000281 }
282 if (DirIndex >= MCDwarfDirs.size()) {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000283 char *Buf = static_cast<char *>(Allocate(Directory.size()));
284 memcpy(Buf, Directory.data(), Directory.size());
285 MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000286 }
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000287 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
288 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
289 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames are
290 // stored at MCDwarfFiles[FileNumber].Name .
291 DirIndex++;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000292 }
Michael J. Spencer326990f2010-11-26 04:16:08 +0000293
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000294 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
295 // vector.
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000296 char *Buf = static_cast<char *>(Allocate(Name.size()));
297 memcpy(Buf, Name.data(), Name.size());
298 File = new (*this) MCDwarfFile(StringRef(Buf, Name.size()), DirIndex);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000299
300 // return the allocated FileNumber.
301 return FileNumber;
302}
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000303
Kevin Enderby3f55c242010-10-04 20:17:24 +0000304/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000305/// currently is assigned and false otherwise.
Kevin Enderby3f55c242010-10-04 20:17:24 +0000306bool MCContext::isValidDwarfFileNumber(unsigned FileNumber) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000307 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
308 return false;
309
Kevin Enderby3f55c242010-10-04 20:17:24 +0000310 return MCDwarfFiles[FileNumber] != 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000311}