blob: 7470e8d0031c39e9c478441aaf55492ca849d13c [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
Chris Lattnerc18409a2010-03-11 22:53:35 +000027MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) {
Chris Lattnerf0559e42010-04-08 20:30:37 +000028 MachOUniquingMap = 0;
Chris Lattner74aae472010-04-08 21:26:26 +000029 ELFUniquingMap = 0;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000030 COFFUniquingMap = 0;
Kevin Enderbyf187ac52010-06-28 21:45:58 +000031
32 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
33 SecureLog = 0;
34 SecureLogUsed = false;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000035}
36
37MCContext::~MCContext() {
Chris Lattnerf0559e42010-04-08 20:30:37 +000038 // NOTE: The symbols are all allocated out of a bump pointer allocator,
Chris Lattnerc9d31522009-08-13 00:21:53 +000039 // we don't need to free them here.
Chris Lattnerf0559e42010-04-08 20:30:37 +000040
41 // If we have the MachO uniquing map, free it.
42 delete (MachOUniqueMapTy*)MachOUniquingMap;
Chris Lattner74aae472010-04-08 21:26:26 +000043 delete (ELFUniqueMapTy*)ELFUniquingMap;
Chris Lattnereb40a0f2010-05-07 17:17:41 +000044 delete (COFFUniqueMapTy*)COFFUniquingMap;
Kevin Enderbyf187ac52010-06-28 21:45:58 +000045
46 // If the stream for the .secure_log_unique directive was created free it.
47 delete (raw_ostream*)SecureLog;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000048}
49
Chris Lattnerf0559e42010-04-08 20:30:37 +000050//===----------------------------------------------------------------------===//
51// Symbol Manipulation
52//===----------------------------------------------------------------------===//
53
Chris Lattner9b97a732010-03-30 18:10:53 +000054MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
Chris Lattner00685bb2010-03-10 01:29:27 +000055 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
Chris Lattnerc28cc092010-03-15 06:15:35 +000056
Chris Lattner9b97a732010-03-30 18:10:53 +000057 // Determine whether this is an assembler temporary or normal label.
58 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
59
Chris Lattnerc28cc092010-03-15 06:15:35 +000060 // Do the lookup and get the entire StringMapEntry. We want access to the
61 // key if we are creating the entry.
62 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
63 if (Entry.getValue()) return Entry.getValue();
Chris Lattnerc69485e2009-06-24 04:31:49 +000064
Chris Lattnerc28cc092010-03-15 06:15:35 +000065 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
66 // to the copy of the string that is embedded in the StringMapEntry.
67 MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary);
68 Entry.setValue(Result);
69 return Result;
Chris Lattnerc69485e2009-06-24 04:31:49 +000070}
71
Chris Lattner9b97a732010-03-30 18:10:53 +000072MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
Chris Lattner7c5b0212009-10-19 22:49:00 +000073 SmallString<128> NameSV;
74 Name.toVector(NameSV);
Chris Lattner9b97a732010-03-30 18:10:53 +000075 return GetOrCreateSymbol(NameSV.str());
Chris Lattner7c5b0212009-10-19 22:49:00 +000076}
77
Chris Lattner1d72a762010-03-14 08:23:30 +000078MCSymbol *MCContext::CreateTempSymbol() {
Chris Lattner9b97a732010-03-30 18:10:53 +000079 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
80 "tmp" + Twine(NextUniqueID++));
Chris Lattner1d72a762010-03-14 08:23:30 +000081}
82
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +000083unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +000084 MCLabel *&Label = Instances[LocalLabelVal];
85 if (!Label)
86 Label = new (*this) MCLabel(0);
87 return Label->incInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +000088}
89
90unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
Benjamin Kramer47f9a492010-05-18 12:15:34 +000091 MCLabel *&Label = Instances[LocalLabelVal];
92 if (!Label)
93 Label = new (*this) MCLabel(0);
94 return Label->getInstance();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +000095}
96
97MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
98 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
99 Twine(LocalLabelVal) +
100 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000101 Twine(NextInstance(LocalLabelVal)));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000102}
103MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
104 int bORf) {
105 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
106 Twine(LocalLabelVal) +
107 "\2" +
Duncan Sands34727662010-07-12 08:16:59 +0000108 Twine(GetInstance(LocalLabelVal) + bORf));
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000109}
110
Daniel Dunbar2928c832009-11-06 10:58:06 +0000111MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000112 return Symbols.lookup(Name);
113}
Chris Lattnerf0559e42010-04-08 20:30:37 +0000114
115//===----------------------------------------------------------------------===//
116// Section Management
117//===----------------------------------------------------------------------===//
118
119const MCSectionMachO *MCContext::
120getMachOSection(StringRef Segment, StringRef Section,
121 unsigned TypeAndAttributes,
122 unsigned Reserved2, SectionKind Kind) {
123
124 // We unique sections by their segment/section pair. The returned section
125 // may not have the same flags as the requested section, if so this should be
126 // diagnosed by the client as an error.
127
128 // Create the map if it doesn't already exist.
129 if (MachOUniquingMap == 0)
130 MachOUniquingMap = new MachOUniqueMapTy();
131 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
132
133 // Form the name to look up.
134 SmallString<64> Name;
135 Name += Segment;
136 Name.push_back(',');
137 Name += Section;
138
139 // Do the lookup, if we have a hit, return it.
140 const MCSectionMachO *&Entry = Map[Name.str()];
141 if (Entry) return Entry;
142
143 // Otherwise, return a new section.
Chris Lattner74aae472010-04-08 21:26:26 +0000144 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
145 Reserved2, Kind);
Chris Lattnerf0559e42010-04-08 20:30:37 +0000146}
Chris Lattner74aae472010-04-08 21:26:26 +0000147
148
149const MCSection *MCContext::
150getELFSection(StringRef Section, unsigned Type, unsigned Flags,
151 SectionKind Kind, bool IsExplicit) {
152 if (ELFUniquingMap == 0)
153 ELFUniquingMap = new ELFUniqueMapTy();
154 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
155
156 // Do the lookup, if we have a hit, return it.
157 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
158 if (Entry.getValue()) return Entry.getValue();
159
160 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
161 Kind, IsExplicit);
162 Entry.setValue(Result);
163 return Result;
164}
165
Chris Lattner6e5ce282010-05-07 21:49:09 +0000166const MCSection *MCContext::getCOFFSection(StringRef Section,
167 unsigned Characteristics,
168 int Selection,
169 SectionKind Kind) {
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000170 if (COFFUniquingMap == 0)
171 COFFUniquingMap = new COFFUniqueMapTy();
172 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
173
174 // Do the lookup, if we have a hit, return it.
175 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
176 if (Entry.getValue()) return Entry.getValue();
177
Chris Lattner6e5ce282010-05-07 21:49:09 +0000178 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
179 Characteristics,
180 Selection, Kind);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000181
182 Entry.setValue(Result);
183 return Result;
184}
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000185
186//===----------------------------------------------------------------------===//
187// Dwarf Management
188//===----------------------------------------------------------------------===//
189
190/// GetDwarfFile - takes a file name an number to place in the dwarf file and
191/// directory tables. If the file number has already been allocated it is an
192/// error and zero is returned and the client reports the error, else the
193/// allocated file number is returned. The file numbers may be in any order.
194unsigned MCContext::GetDwarfFile(StringRef FileName, unsigned FileNumber) {
195 // TODO: a FileNumber of zero says to use the next available file number.
196 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
197 // to not be less than one. This needs to be change to be not less than zero.
198
199 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
200 if (FileNumber >= MCDwarfFiles.size()) {
201 MCDwarfFiles.resize(FileNumber + 1);
202 } else {
203 MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
204 if (ExistingFile)
205 // It is an error to use see the same number more than once.
206 return 0;
207 }
208
209 // Get the new MCDwarfFile slot for this FileNumber.
210 MCDwarfFile *&File = MCDwarfFiles[FileNumber];
211
212 // Separate the directory part from the basename of the FileName.
213 std::pair<StringRef, StringRef> Slash = FileName.rsplit('/');
214
215 // Find or make a entry in the MCDwarfDirs vector for this Directory.
216 StringRef Directory;
217 StringRef Name;
218 unsigned DirIndex;
219 // Capture directory name.
220 if (Slash.second.empty()) {
221 Name = Slash.first;
222 DirIndex = 0; // For FileNames with no directories a DirIndex of 0 is used.
223 } else {
224 Directory = Slash.first;
225 Name = Slash.second;
226 for (DirIndex = 1; DirIndex < MCDwarfDirs.size(); DirIndex++) {
227 std::string *&Dir = MCDwarfDirs[DirIndex];
228 if (Directory == *Dir)
229 break;
230 }
231 if (DirIndex >= MCDwarfDirs.size()) {
232 MCDwarfDirs.resize(DirIndex + 1);
233 std::string *&NewDir = MCDwarfDirs[DirIndex];
234 NewDir = new (*this) std::string(Directory);
235 }
236 }
237
238 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
239 // vector.
240 File = new (*this) MCDwarfFile(Name, DirIndex);
241
242 // return the allocated FileNumber.
243 return FileNumber;
244}