blob: 1e65f877924db65fa476cbd27fac7297e34ee33d [file] [log] [blame]
Daniel Dunbarbadeace2009-06-23 23:39:15 +00001//===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
Daniel Dunbarca29e4d2009-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 Lattnerd3691dd2010-03-11 22:56:10 +000011#include "llvm/MC/MCAsmInfo.h"
Chris Lattner20731122010-04-08 20:30:37 +000012#include "llvm/MC/MCSectionMachO.h"
Chris Lattner5418dd52010-04-08 21:26:26 +000013#include "llvm/MC/MCSectionELF.h"
Chris Lattner87cffa92010-05-07 17:17:41 +000014#include "llvm/MC/MCSectionCOFF.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000015#include "llvm/MC/MCSymbol.h"
Kevin Enderby0510b482010-05-17 23:08:19 +000016#include "llvm/MC/MCLabel.h"
Kevin Enderbye5930f12010-07-28 20:55:35 +000017#include "llvm/MC/MCDwarf.h"
Chris Lattner86dfd732009-10-19 22:49:00 +000018#include "llvm/ADT/SmallString.h"
19#include "llvm/ADT/Twine.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000020using namespace llvm;
21
Chris Lattner20731122010-04-08 20:30:37 +000022typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner5418dd52010-04-08 21:26:26 +000023typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
Chris Lattner87cffa92010-05-07 17:17:41 +000024typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
Chris Lattner20731122010-04-08 20:30:37 +000025
26
Kevin Enderby1264b7c2010-08-24 20:32:42 +000027MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0),
28 CurrentDwarfLoc(0,0,0,0,0) {
Chris Lattner20731122010-04-08 20:30:37 +000029 MachOUniquingMap = 0;
Chris Lattner5418dd52010-04-08 21:26:26 +000030 ELFUniquingMap = 0;
Chris Lattner87cffa92010-05-07 17:17:41 +000031 COFFUniquingMap = 0;
Kevin Enderbye233dda2010-06-28 21:45:58 +000032
33 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
34 SecureLog = 0;
35 SecureLogUsed = false;
Kevin Enderby1264b7c2010-08-24 20:32:42 +000036
37 DwarfLocSeen = false;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000038}
39
40MCContext::~MCContext() {
Chris Lattner20731122010-04-08 20:30:37 +000041 // NOTE: The symbols are all allocated out of a bump pointer allocator,
Chris Lattnerb6913162009-08-13 00:21:53 +000042 // we don't need to free them here.
Chris Lattner20731122010-04-08 20:30:37 +000043
44 // If we have the MachO uniquing map, free it.
45 delete (MachOUniqueMapTy*)MachOUniquingMap;
Chris Lattner5418dd52010-04-08 21:26:26 +000046 delete (ELFUniqueMapTy*)ELFUniquingMap;
Chris Lattner87cffa92010-05-07 17:17:41 +000047 delete (COFFUniqueMapTy*)COFFUniquingMap;
Kevin Enderbye233dda2010-06-28 21:45:58 +000048
49 // If the stream for the .secure_log_unique directive was created free it.
50 delete (raw_ostream*)SecureLog;
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000051}
52
Chris Lattner20731122010-04-08 20:30:37 +000053//===----------------------------------------------------------------------===//
54// Symbol Manipulation
55//===----------------------------------------------------------------------===//
56
Chris Lattner98970432010-03-30 18:10:53 +000057MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattnerb973ea82010-03-10 01:29:27 +000058 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Chris Lattner13348762010-03-15 06:15:35 +000059
Chris Lattner98970432010-03-30 18:10:53 +000060 // Determine whether this is an assembler temporary or normal label.
61 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
62
Chris Lattner13348762010-03-15 06:15:35 +000063 // Do the lookup and get the entire StringMapEntry. We want access to the
64 // key if we are creating the entry.
65 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
66 if (Entry.getValue()) return Entry.getValue();
Chris Lattner3f5738d2009-06-24 04:31:49 +000067
Chris Lattner13348762010-03-15 06:15:35 +000068 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
69 // to the copy of the string that is embedded in the StringMapEntry.
70 MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary);
71 Entry.setValue(Result);
72 return Result;
Chris Lattner3f5738d2009-06-24 04:31:49 +000073}
74
Chris Lattner98970432010-03-30 18:10:53 +000075MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner86dfd732009-10-19 22:49:00 +000076 SmallString<128> NameSV;
77 Name.toVector(NameSV);
Chris Lattner98970432010-03-30 18:10:53 +000078 return GetOrCreateSymbol(NameSV.str());
Chris Lattner86dfd732009-10-19 22:49:00 +000079}
80
Chris Lattner073d8172010-03-14 08:23:30 +000081MCSymbol *MCContext::CreateTempSymbol() {
Chris Lattner98970432010-03-30 18:10:53 +000082 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
83 "tmp" + Twine(NextUniqueID++));
Chris Lattner073d8172010-03-14 08:23:30 +000084}
85
Kevin Enderby0510b482010-05-17 23:08:19 +000086unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +000087 MCLabel *&Label = Instances[LocalLabelVal];
88 if (!Label)
89 Label = new (*this) MCLabel(0);
90 return Label->incInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +000091}
92
93unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
Benjamin Kramerab7be752010-05-18 12:15:34 +000094 MCLabel *&Label = Instances[LocalLabelVal];
95 if (!Label)
96 Label = new (*this) MCLabel(0);
97 return Label->getInstance();
Kevin Enderby0510b482010-05-17 23:08:19 +000098}
99
100MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
101 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
102 Twine(LocalLabelVal) +
103 "\2" +
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000104 Twine(NextInstance(LocalLabelVal)));
Kevin Enderby0510b482010-05-17 23:08:19 +0000105}
106MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
107 int bORf) {
108 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
109 Twine(LocalLabelVal) +
110 "\2" +
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000111 Twine(GetInstance(LocalLabelVal) + bORf));
Kevin Enderby0510b482010-05-17 23:08:19 +0000112}
113
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000114MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarca29e4d2009-06-23 22:01:43 +0000115 return Symbols.lookup(Name);
116}
Chris Lattner20731122010-04-08 20:30:37 +0000117
118//===----------------------------------------------------------------------===//
119// Section Management
120//===----------------------------------------------------------------------===//
121
122const MCSectionMachO *MCContext::
123getMachOSection(StringRef Segment, StringRef Section,
124 unsigned TypeAndAttributes,
125 unsigned Reserved2, SectionKind Kind) {
126
127 // We unique sections by their segment/section pair. The returned section
128 // may not have the same flags as the requested section, if so this should be
129 // diagnosed by the client as an error.
130
131 // Create the map if it doesn't already exist.
132 if (MachOUniquingMap == 0)
133 MachOUniquingMap = new MachOUniqueMapTy();
134 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
135
136 // Form the name to look up.
137 SmallString<64> Name;
138 Name += Segment;
139 Name.push_back(',');
140 Name += Section;
141
142 // Do the lookup, if we have a hit, return it.
143 const MCSectionMachO *&Entry = Map[Name.str()];
144 if (Entry) return Entry;
145
146 // Otherwise, return a new section.
Chris Lattner5418dd52010-04-08 21:26:26 +0000147 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
148 Reserved2, Kind);
Chris Lattner20731122010-04-08 20:30:37 +0000149}
Chris Lattner5418dd52010-04-08 21:26:26 +0000150
151
152const MCSection *MCContext::
153getELFSection(StringRef Section, unsigned Type, unsigned Flags,
Matt Flemingd83b9272010-08-16 18:35:43 +0000154 SectionKind Kind, bool IsExplicit, unsigned EntrySize) {
Chris Lattner5418dd52010-04-08 21:26:26 +0000155 if (ELFUniquingMap == 0)
156 ELFUniquingMap = new ELFUniqueMapTy();
157 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
158
159 // Do the lookup, if we have a hit, return it.
160 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
161 if (Entry.getValue()) return Entry.getValue();
162
Jan Wen Voungefbdbe52010-09-30 05:59:22 +0000163 // Possibly refine the entry size first.
164 if (!EntrySize) {
165 EntrySize = MCSectionELF::DetermineEntrySize(Kind);
166 }
Chris Lattner5418dd52010-04-08 21:26:26 +0000167 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
Matt Flemingd83b9272010-08-16 18:35:43 +0000168 Kind, IsExplicit, EntrySize);
Chris Lattner5418dd52010-04-08 21:26:26 +0000169 Entry.setValue(Result);
170 return Result;
171}
172
Chris Lattner02844932010-05-07 21:49:09 +0000173const MCSection *MCContext::getCOFFSection(StringRef Section,
174 unsigned Characteristics,
175 int Selection,
176 SectionKind Kind) {
Chris Lattner87cffa92010-05-07 17:17:41 +0000177 if (COFFUniquingMap == 0)
178 COFFUniquingMap = new COFFUniqueMapTy();
179 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
180
181 // Do the lookup, if we have a hit, return it.
182 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
183 if (Entry.getValue()) return Entry.getValue();
184
Chris Lattner02844932010-05-07 21:49:09 +0000185 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
186 Characteristics,
187 Selection, Kind);
Chris Lattner87cffa92010-05-07 17:17:41 +0000188
189 Entry.setValue(Result);
190 return Result;
191}
Kevin Enderbye5930f12010-07-28 20:55:35 +0000192
193//===----------------------------------------------------------------------===//
194// Dwarf Management
195//===----------------------------------------------------------------------===//
196
197/// GetDwarfFile - takes a file name an number to place in the dwarf file and
198/// directory tables. If the file number has already been allocated it is an
199/// error and zero is returned and the client reports the error, else the
200/// allocated file number is returned. The file numbers may be in any order.
201unsigned MCContext::GetDwarfFile(StringRef FileName, unsigned FileNumber) {
202 // TODO: a FileNumber of zero says to use the next available file number.
203 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
204 // to not be less than one. This needs to be change to be not less than zero.
205
206 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
207 if (FileNumber >= MCDwarfFiles.size()) {
208 MCDwarfFiles.resize(FileNumber + 1);
209 } else {
210 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
211 if (ExistingFile)
212 // It is an error to use see the same number more than once.
213 return 0;
214 }
215
216 // Get the new MCDwarfFile slot for this FileNumber.
217 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
218
219 // Separate the directory part from the basename of the FileName.
220 std::pair<StringRef, StringRef> Slash = FileName.rsplit('/');
221
222 // Find or make a entry in the MCDwarfDirs vector for this Directory.
Kevin Enderbye5930f12010-07-28 20:55:35 +0000223 StringRef Name;
224 unsigned DirIndex;
225 // Capture directory name.
226 if (Slash.second.empty()) {
227 Name = Slash.first;
228 DirIndex = 0; // For FileNames with no directories a DirIndex of 0 is used.
229 } else {
Benjamin Kramerbccfec62010-07-29 13:53:19 +0000230 StringRef Directory = Slash.first;
Kevin Enderbye5930f12010-07-28 20:55:35 +0000231 Name = Slash.second;
Kevin Enderby7221b762010-08-09 22:52:14 +0000232 for (DirIndex = 0; DirIndex < MCDwarfDirs.size(); DirIndex++) {
Benjamin Kramerbccfec62010-07-29 13:53:19 +0000233 if (Directory == MCDwarfDirs[DirIndex])
Kevin Enderbyd94dacf2010-08-31 22:55:11 +0000234 break;
Kevin Enderbye5930f12010-07-28 20:55:35 +0000235 }
236 if (DirIndex >= MCDwarfDirs.size()) {
Benjamin Kramerbccfec62010-07-29 13:53:19 +0000237 char *Buf = static_cast<char *>(Allocate(Directory.size()));
238 memcpy(Buf, Directory.data(), Directory.size());
239 MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
Kevin Enderbye5930f12010-07-28 20:55:35 +0000240 }
Kevin Enderby7221b762010-08-09 22:52:14 +0000241 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
242 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
243 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames are
244 // stored at MCDwarfFiles[FileNumber].Name .
245 DirIndex++;
Kevin Enderbye5930f12010-07-28 20:55:35 +0000246 }
247
248 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
249 // vector.
Benjamin Kramerbccfec62010-07-29 13:53:19 +0000250 char *Buf = static_cast<char *>(Allocate(Name.size()));
251 memcpy(Buf, Name.data(), Name.size());
252 File = new (*this) MCDwarfFile(StringRef(Buf, Name.size()), DirIndex);
Kevin Enderbye5930f12010-07-28 20:55:35 +0000253
254 // return the allocated FileNumber.
255 return FileNumber;
256}
Kevin Enderby1264b7c2010-08-24 20:32:42 +0000257
258/// ValidateDwarfFileNumber - takes a dwarf file number and returns true if it
259/// currently is assigned and false otherwise.
260bool MCContext::ValidateDwarfFileNumber(unsigned FileNumber) {
261 if(FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
262 return false;
263
264 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
265 if (ExistingFile)
266 return true;
267 else
268 return false;
269}