blob: af8cd8eb141ee51d29ddb626b80e40404b7b0831 [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"
Rafael Espindolac85dca62011-01-23 04:28:49 +000021#include "llvm/Support/ELF.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000022using namespace llvm;
23
Chris Lattnerf0559e42010-04-08 20:30:37 +000024typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner74aae472010-04-08 21:26:26 +000025typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000026typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
Chris Lattnerf0559e42010-04-08 20:30:37 +000027
28
Rafael Espindola89b93722010-12-10 07:39:47 +000029MCContext::MCContext(const MCAsmInfo &mai, const TargetAsmInfo *tai) :
30 MAI(mai), TAI(tai), NextUniqueID(0),
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +000031 CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0),
32 AllowTemporaryLabels(true) {
Chris Lattnerf0559e42010-04-08 20:30:37 +000033 MachOUniquingMap = 0;
Chris Lattner74aae472010-04-08 21:26:26 +000034 ELFUniquingMap = 0;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000035 COFFUniquingMap = 0;
Kevin Enderbyf187ac52010-06-28 21:45:58 +000036
37 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
38 SecureLog = 0;
39 SecureLogUsed = false;
Kevin Enderbyc1840b32010-08-24 20:32:42 +000040
41 DwarfLocSeen = false;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000042}
43
44MCContext::~MCContext() {
Chris Lattnerf0559e42010-04-08 20:30:37 +000045 // NOTE: The symbols are all allocated out of a bump pointer allocator,
Chris Lattnerc9d31522009-08-13 00:21:53 +000046 // we don't need to free them here.
Michael J. Spencer326990f2010-11-26 04:16:08 +000047
Chris Lattnerf0559e42010-04-08 20:30:37 +000048 // If we have the MachO uniquing map, free it.
49 delete (MachOUniqueMapTy*)MachOUniquingMap;
Chris Lattner74aae472010-04-08 21:26:26 +000050 delete (ELFUniqueMapTy*)ELFUniquingMap;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000051 delete (COFFUniqueMapTy*)COFFUniquingMap;
Kevin Enderbyf187ac52010-06-28 21:45:58 +000052
53 // If the stream for the .secure_log_unique directive was created free it.
54 delete (raw_ostream*)SecureLog;
Rafael Espindola89b93722010-12-10 07:39:47 +000055
56 delete TAI;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000057}
58
Chris Lattnerf0559e42010-04-08 20:30:37 +000059//===----------------------------------------------------------------------===//
60// Symbol Manipulation
61//===----------------------------------------------------------------------===//
62
Chris Lattner9b97a732010-03-30 18:10:53 +000063MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattner00685bb2010-03-10 01:29:27 +000064 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Michael J. Spencer326990f2010-11-26 04:16:08 +000065
Chris Lattnerc28cc092010-03-15 06:15:35 +000066 // Do the lookup and get the entire StringMapEntry. We want access to the
67 // key if we are creating the entry.
68 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
Rafael Espindola9f447242010-12-01 20:46:11 +000069 MCSymbol *Sym = Entry.getValue();
70
71 if (Sym)
72 return Sym;
73
74 Sym = CreateSymbol(Name);
75 Entry.setValue(Sym);
76 return Sym;
77}
78
79MCSymbol *MCContext::CreateSymbol(StringRef Name) {
Daniel Dunbarc6cf43d2011-03-28 22:49:15 +000080 // Determine whether this is an assembler temporary or normal label, if used.
81 bool isTemporary = false;
82 if (AllowTemporaryLabels)
83 isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
Rafael Espindola9f447242010-12-01 20:46:11 +000084
85 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name);
86 if (NameEntry->getValue()) {
87 assert(isTemporary && "Cannot rename non temporary symbols");
Benjamin Kramerc18214a2011-04-09 11:26:27 +000088 SmallString<128> NewName = Name;
Rafael Espindola9f447242010-12-01 20:46:11 +000089 do {
Benjamin Kramerc18214a2011-04-09 11:26:27 +000090 NewName.resize(Name.size());
91 raw_svector_ostream(NewName) << NextUniqueID++;
92 NameEntry = &UsedNames.GetOrCreateValue(NewName);
Rafael Espindola9f447242010-12-01 20:46:11 +000093 } while (NameEntry->getValue());
94 }
95 NameEntry->setValue(true);
Chris Lattnerc69485e2009-06-24 04:31:49 +000096
Chris Lattnerc28cc092010-03-15 06:15:35 +000097 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
Rafael Espindola9f447242010-12-01 20:46:11 +000098 // to the copy of the string that is embedded in the UsedNames entry.
99 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary);
100
101 return Result;
Chris Lattnerc69485e2009-06-24 04:31:49 +0000102}
103
Chris Lattner9b97a732010-03-30 18:10:53 +0000104MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner7c5b0212009-10-19 22:49:00 +0000105 SmallString<128> NameSV;
106 Name.toVector(NameSV);
Chris Lattner9b97a732010-03-30 18:10:53 +0000107 return GetOrCreateSymbol(NameSV.str());
Chris Lattner7c5b0212009-10-19 22:49:00 +0000108}
109
Chris Lattner1d72a762010-03-14 08:23:30 +0000110MCSymbol *MCContext::CreateTempSymbol() {
Rafael Espindola9f447242010-12-01 20:46:11 +0000111 SmallString<128> NameSV;
Benjamin Kramerc18214a2011-04-09 11:26:27 +0000112 raw_svector_ostream(NameSV)
113 << MAI.getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
Rafael Espindola9f447242010-12-01 20:46:11 +0000114 return CreateSymbol(NameSV);
Chris Lattner1d72a762010-03-14 08:23:30 +0000115}
116
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000117unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000118 MCLabel *&Label = Instances[LocalLabelVal];
119 if (!Label)
120 Label = new (*this) MCLabel(0);
121 return Label->incInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000122}
123
124unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +0000125 MCLabel *&Label = Instances[LocalLabelVal];
126 if (!Label)
127 Label = new (*this) MCLabel(0);
128 return Label->getInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000129}
130
131MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
132 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
133 Twine(LocalLabelVal) +
134 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000135 Twine(NextInstance(LocalLabelVal)));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000136}
137MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
138 int bORf) {
139 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
140 Twine(LocalLabelVal) +
141 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000142 Twine(GetInstance(LocalLabelVal) + bORf));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000143}
144
Daniel Dunbar2928c832009-11-06 10:58:06 +0000145MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000146 return Symbols.lookup(Name);
147}
Chris Lattnerf0559e42010-04-08 20:30:37 +0000148
149//===----------------------------------------------------------------------===//
150// Section Management
151//===----------------------------------------------------------------------===//
152
153const MCSectionMachO *MCContext::
154getMachOSection(StringRef Segment, StringRef Section,
155 unsigned TypeAndAttributes,
156 unsigned Reserved2, SectionKind Kind) {
Michael J. Spencer326990f2010-11-26 04:16:08 +0000157
Chris Lattnerf0559e42010-04-08 20:30:37 +0000158 // We unique sections by their segment/section pair. The returned section
159 // may not have the same flags as the requested section, if so this should be
160 // diagnosed by the client as an error.
Michael J. Spencer326990f2010-11-26 04:16:08 +0000161
Chris Lattnerf0559e42010-04-08 20:30:37 +0000162 // Create the map if it doesn't already exist.
163 if (MachOUniquingMap == 0)
164 MachOUniquingMap = new MachOUniqueMapTy();
165 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000166
Chris Lattnerf0559e42010-04-08 20:30:37 +0000167 // Form the name to look up.
168 SmallString<64> Name;
169 Name += Segment;
170 Name.push_back(',');
171 Name += Section;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000172
Chris Lattnerf0559e42010-04-08 20:30:37 +0000173 // Do the lookup, if we have a hit, return it.
174 const MCSectionMachO *&Entry = Map[Name.str()];
175 if (Entry) return Entry;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000176
Chris Lattnerf0559e42010-04-08 20:30:37 +0000177 // Otherwise, return a new section.
Chris Lattner74aae472010-04-08 21:26:26 +0000178 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
179 Reserved2, Kind);
Chris Lattnerf0559e42010-04-08 20:30:37 +0000180}
Chris Lattner74aae472010-04-08 21:26:26 +0000181
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000182const MCSectionELF *MCContext::
Chris Lattner74aae472010-04-08 21:26:26 +0000183getELFSection(StringRef Section, unsigned Type, unsigned Flags,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000184 SectionKind Kind) {
185 return getELFSection(Section, Type, Flags, Kind, 0, "");
186}
187
188const MCSectionELF *MCContext::
189getELFSection(StringRef Section, unsigned Type, unsigned Flags,
190 SectionKind Kind, unsigned EntrySize, StringRef Group) {
Chris Lattner74aae472010-04-08 21:26:26 +0000191 if (ELFUniquingMap == 0)
192 ELFUniquingMap = new ELFUniqueMapTy();
193 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000194
Chris Lattner74aae472010-04-08 21:26:26 +0000195 // Do the lookup, if we have a hit, return it.
196 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
197 if (Entry.getValue()) return Entry.getValue();
Michael J. Spencer326990f2010-11-26 04:16:08 +0000198
Jan Wen Voung186e7a02010-09-30 05:59:22 +0000199 // Possibly refine the entry size first.
200 if (!EntrySize) {
201 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
202 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000203
204 MCSymbol *GroupSym = NULL;
205 if (!Group.empty())
206 GroupSym = GetOrCreateSymbol(Group);
207
Chris Lattner74aae472010-04-08 21:26:26 +0000208 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000209 Kind, EntrySize, GroupSym);
Chris Lattner74aae472010-04-08 21:26:26 +0000210 Entry.setValue(Result);
211 return Result;
212}
213
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000214const MCSectionELF *MCContext::CreateELFGroupSection() {
215 MCSectionELF *Result =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000216 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000217 SectionKind::getReadOnly(), 4, NULL);
218 return Result;
219}
220
Chris Lattner6e5ce282010-05-07 21:49:09 +0000221const MCSection *MCContext::getCOFFSection(StringRef Section,
222 unsigned Characteristics,
223 int Selection,
224 SectionKind Kind) {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000225 if (COFFUniquingMap == 0)
226 COFFUniquingMap = new COFFUniqueMapTy();
227 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
Michael J. Spencer326990f2010-11-26 04:16:08 +0000228
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000229 // Do the lookup, if we have a hit, return it.
230 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
231 if (Entry.getValue()) return Entry.getValue();
Michael J. Spencer326990f2010-11-26 04:16:08 +0000232
Chris Lattner6e5ce282010-05-07 21:49:09 +0000233 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
234 Characteristics,
235 Selection, Kind);
Michael J. Spencer326990f2010-11-26 04:16:08 +0000236
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000237 Entry.setValue(Result);
238 return Result;
239}
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000240
241//===----------------------------------------------------------------------===//
242// Dwarf Management
243//===----------------------------------------------------------------------===//
244
245/// GetDwarfFile - takes a file name an number to place in the dwarf file and
246/// directory tables. If the file number has already been allocated it is an
247/// error and zero is returned and the client reports the error, else the
248/// allocated file number is returned. The file numbers may be in any order.
249unsigned MCContext::GetDwarfFile(StringRef FileName, unsigned FileNumber) {
250 // TODO: a FileNumber of zero says to use the next available file number.
251 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
252 // to not be less than one. This needs to be change to be not less than zero.
253
254 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
255 if (FileNumber >= MCDwarfFiles.size()) {
256 MCDwarfFiles.resize(FileNumber + 1);
257 } else {
258 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
259 if (ExistingFile)
260 // It is an error to use see the same number more than once.
261 return 0;
262 }
263
264 // Get the new MCDwarfFile slot for this FileNumber.
265 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
266
267 // Separate the directory part from the basename of the FileName.
268 std::pair<StringRef, StringRef> Slash = FileName.rsplit('/');
269
270 // Find or make a entry in the MCDwarfDirs vector for this Directory.
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000271 StringRef Name;
272 unsigned DirIndex;
273 // Capture directory name.
274 if (Slash.second.empty()) {
275 Name = Slash.first;
276 DirIndex = 0; // For FileNames with no directories a DirIndex of 0 is used.
277 } else {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000278 StringRef Directory = Slash.first;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000279 Name = Slash.second;
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000280 for (DirIndex = 0; DirIndex < MCDwarfDirs.size(); DirIndex++) {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000281 if (Directory == MCDwarfDirs[DirIndex])
Kevin Enderby232ab942010-08-31 22:55:11 +0000282 break;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000283 }
284 if (DirIndex >= MCDwarfDirs.size()) {
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000285 char *Buf = static_cast<char *>(Allocate(Directory.size()));
286 memcpy(Buf, Directory.data(), Directory.size());
287 MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000288 }
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000289 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
290 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
291 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames are
292 // stored at MCDwarfFiles[FileNumber].Name .
293 DirIndex++;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000294 }
Michael J. Spencer326990f2010-11-26 04:16:08 +0000295
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000296 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
297 // vector.
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +0000298 char *Buf = static_cast<char *>(Allocate(Name.size()));
299 memcpy(Buf, Name.data(), Name.size());
300 File = new (*this) MCDwarfFile(StringRef(Buf, Name.size()), DirIndex);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000301
302 // return the allocated FileNumber.
303 return FileNumber;
304}
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000305
Kevin Enderby3f55c242010-10-04 20:17:24 +0000306/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000307/// currently is assigned and false otherwise.
Kevin Enderby3f55c242010-10-04 20:17:24 +0000308bool MCContext::isValidDwarfFileNumber(unsigned FileNumber) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000309 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
310 return false;
311
Kevin Enderby3f55c242010-10-04 20:17:24 +0000312 return MCDwarfFiles[FileNumber] != 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000313}