1 //===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCContext.h"
11 #include "llvm/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCSectionMachO.h"
13 #include "llvm/MC/MCSectionELF.h"
14 #include "llvm/MC/MCSectionCOFF.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/MC/MCLabel.h"
17 #include "llvm/MC/MCDwarf.h"
18 #include "llvm/Target/TargetAsmInfo.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/ELF.h"
24 typedef StringMap
<const MCSectionMachO
*> MachOUniqueMapTy
;
25 typedef StringMap
<const MCSectionELF
*> ELFUniqueMapTy
;
26 typedef StringMap
<const MCSectionCOFF
*> COFFUniqueMapTy
;
29 MCContext::MCContext(const MCAsmInfo
&mai
, const TargetAsmInfo
*tai
) :
31 Allocator(), Symbols(Allocator
), UsedNames(Allocator
),
33 CurrentDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT
,0,0),
34 AllowTemporaryLabels(true) {
39 SecureLogFile
= getenv("AS_SECURE_LOG_FILE");
41 SecureLogUsed
= false;
46 MCContext::~MCContext() {
47 // NOTE: The symbols are all allocated out of a bump pointer allocator,
48 // we don't need to free them here.
50 // If we have the MachO uniquing map, free it.
51 delete (MachOUniqueMapTy
*)MachOUniquingMap
;
52 delete (ELFUniqueMapTy
*)ELFUniquingMap
;
53 delete (COFFUniqueMapTy
*)COFFUniquingMap
;
55 // If the stream for the .secure_log_unique directive was created free it.
56 delete (raw_ostream
*)SecureLog
;
61 //===----------------------------------------------------------------------===//
62 // Symbol Manipulation
63 //===----------------------------------------------------------------------===//
65 MCSymbol
*MCContext::GetOrCreateSymbol(StringRef Name
) {
66 assert(!Name
.empty() && "Normal symbols cannot be unnamed!");
68 // Do the lookup and get the entire StringMapEntry. We want access to the
69 // key if we are creating the entry.
70 StringMapEntry
<MCSymbol
*> &Entry
= Symbols
.GetOrCreateValue(Name
);
71 MCSymbol
*Sym
= Entry
.getValue();
76 Sym
= CreateSymbol(Name
);
81 MCSymbol
*MCContext::CreateSymbol(StringRef Name
) {
82 // Determine whether this is an assembler temporary or normal label, if used.
83 bool isTemporary
= false;
84 if (AllowTemporaryLabels
)
85 isTemporary
= Name
.startswith(MAI
.getPrivateGlobalPrefix());
87 StringMapEntry
<bool> *NameEntry
= &UsedNames
.GetOrCreateValue(Name
);
88 if (NameEntry
->getValue()) {
89 assert(isTemporary
&& "Cannot rename non temporary symbols");
90 SmallString
<128> NewName
= Name
;
92 NewName
.resize(Name
.size());
93 raw_svector_ostream(NewName
) << NextUniqueID
++;
94 NameEntry
= &UsedNames
.GetOrCreateValue(NewName
);
95 } while (NameEntry
->getValue());
97 NameEntry
->setValue(true);
99 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
100 // to the copy of the string that is embedded in the UsedNames entry.
101 MCSymbol
*Result
= new (*this) MCSymbol(NameEntry
->getKey(), isTemporary
);
106 MCSymbol
*MCContext::GetOrCreateSymbol(const Twine
&Name
) {
107 SmallString
<128> NameSV
;
108 Name
.toVector(NameSV
);
109 return GetOrCreateSymbol(NameSV
.str());
112 MCSymbol
*MCContext::CreateTempSymbol() {
113 SmallString
<128> NameSV
;
114 raw_svector_ostream(NameSV
)
115 << MAI
.getPrivateGlobalPrefix() << "tmp" << NextUniqueID
++;
116 return CreateSymbol(NameSV
);
119 unsigned MCContext::NextInstance(int64_t LocalLabelVal
) {
120 MCLabel
*&Label
= Instances
[LocalLabelVal
];
122 Label
= new (*this) MCLabel(0);
123 return Label
->incInstance();
126 unsigned MCContext::GetInstance(int64_t LocalLabelVal
) {
127 MCLabel
*&Label
= Instances
[LocalLabelVal
];
129 Label
= new (*this) MCLabel(0);
130 return Label
->getInstance();
133 MCSymbol
*MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal
) {
134 return GetOrCreateSymbol(Twine(MAI
.getPrivateGlobalPrefix()) +
135 Twine(LocalLabelVal
) +
137 Twine(NextInstance(LocalLabelVal
)));
139 MCSymbol
*MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal
,
141 return GetOrCreateSymbol(Twine(MAI
.getPrivateGlobalPrefix()) +
142 Twine(LocalLabelVal
) +
144 Twine(GetInstance(LocalLabelVal
) + bORf
));
147 MCSymbol
*MCContext::LookupSymbol(StringRef Name
) const {
148 return Symbols
.lookup(Name
);
151 //===----------------------------------------------------------------------===//
152 // Section Management
153 //===----------------------------------------------------------------------===//
155 const MCSectionMachO
*MCContext::
156 getMachOSection(StringRef Segment
, StringRef Section
,
157 unsigned TypeAndAttributes
,
158 unsigned Reserved2
, SectionKind Kind
) {
160 // We unique sections by their segment/section pair. The returned section
161 // may not have the same flags as the requested section, if so this should be
162 // diagnosed by the client as an error.
164 // Create the map if it doesn't already exist.
165 if (MachOUniquingMap
== 0)
166 MachOUniquingMap
= new MachOUniqueMapTy();
167 MachOUniqueMapTy
&Map
= *(MachOUniqueMapTy
*)MachOUniquingMap
;
169 // Form the name to look up.
170 SmallString
<64> Name
;
175 // Do the lookup, if we have a hit, return it.
176 const MCSectionMachO
*&Entry
= Map
[Name
.str()];
177 if (Entry
) return Entry
;
179 // Otherwise, return a new section.
180 return Entry
= new (*this) MCSectionMachO(Segment
, Section
, TypeAndAttributes
,
184 const MCSectionELF
*MCContext::
185 getELFSection(StringRef Section
, unsigned Type
, unsigned Flags
,
187 return getELFSection(Section
, Type
, Flags
, Kind
, 0, "");
190 const MCSectionELF
*MCContext::
191 getELFSection(StringRef Section
, unsigned Type
, unsigned Flags
,
192 SectionKind Kind
, unsigned EntrySize
, StringRef Group
) {
193 if (ELFUniquingMap
== 0)
194 ELFUniquingMap
= new ELFUniqueMapTy();
195 ELFUniqueMapTy
&Map
= *(ELFUniqueMapTy
*)ELFUniquingMap
;
197 // Do the lookup, if we have a hit, return it.
198 StringMapEntry
<const MCSectionELF
*> &Entry
= Map
.GetOrCreateValue(Section
);
199 if (Entry
.getValue()) return Entry
.getValue();
201 // Possibly refine the entry size first.
203 EntrySize
= MCSectionELF::DetermineEntrySize(Kind
);
206 MCSymbol
*GroupSym
= NULL
;
208 GroupSym
= GetOrCreateSymbol(Group
);
210 MCSectionELF
*Result
= new (*this) MCSectionELF(Entry
.getKey(), Type
, Flags
,
211 Kind
, EntrySize
, GroupSym
);
212 Entry
.setValue(Result
);
216 const MCSectionELF
*MCContext::CreateELFGroupSection() {
217 MCSectionELF
*Result
=
218 new (*this) MCSectionELF(".group", ELF::SHT_GROUP
, 0,
219 SectionKind::getReadOnly(), 4, NULL
);
223 const MCSection
*MCContext::getCOFFSection(StringRef Section
,
224 unsigned Characteristics
,
227 if (COFFUniquingMap
== 0)
228 COFFUniquingMap
= new COFFUniqueMapTy();
229 COFFUniqueMapTy
&Map
= *(COFFUniqueMapTy
*)COFFUniquingMap
;
231 // Do the lookup, if we have a hit, return it.
232 StringMapEntry
<const MCSectionCOFF
*> &Entry
= Map
.GetOrCreateValue(Section
);
233 if (Entry
.getValue()) return Entry
.getValue();
235 MCSectionCOFF
*Result
= new (*this) MCSectionCOFF(Entry
.getKey(),
239 Entry
.setValue(Result
);
243 //===----------------------------------------------------------------------===//
245 //===----------------------------------------------------------------------===//
247 /// GetDwarfFile - takes a file name an number to place in the dwarf file and
248 /// directory tables. If the file number has already been allocated it is an
249 /// error and zero is returned and the client reports the error, else the
250 /// allocated file number is returned. The file numbers may be in any order.
251 unsigned MCContext::GetDwarfFile(StringRef FileName
, unsigned FileNumber
) {
252 // TODO: a FileNumber of zero says to use the next available file number.
253 // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
254 // to not be less than one. This needs to be change to be not less than zero.
256 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
257 if (FileNumber
>= MCDwarfFiles
.size()) {
258 MCDwarfFiles
.resize(FileNumber
+ 1);
260 MCDwarfFile
*&ExistingFile
= MCDwarfFiles
[FileNumber
];
262 // It is an error to use see the same number more than once.
266 // Get the new MCDwarfFile slot for this FileNumber.
267 MCDwarfFile
*&File
= MCDwarfFiles
[FileNumber
];
269 // Separate the directory part from the basename of the FileName.
270 std::pair
<StringRef
, StringRef
> Slash
= FileName
.rsplit('/');
272 // Find or make a entry in the MCDwarfDirs vector for this Directory.
275 // Capture directory name.
276 if (Slash
.second
.empty()) {
278 DirIndex
= 0; // For FileNames with no directories a DirIndex of 0 is used.
280 StringRef Directory
= Slash
.first
;
282 for (DirIndex
= 0; DirIndex
< MCDwarfDirs
.size(); DirIndex
++) {
283 if (Directory
== MCDwarfDirs
[DirIndex
])
286 if (DirIndex
>= MCDwarfDirs
.size()) {
287 char *Buf
= static_cast<char *>(Allocate(Directory
.size()));
288 memcpy(Buf
, Directory
.data(), Directory
.size());
289 MCDwarfDirs
.push_back(StringRef(Buf
, Directory
.size()));
291 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
292 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
293 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames are
294 // stored at MCDwarfFiles[FileNumber].Name .
298 // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
300 char *Buf
= static_cast<char *>(Allocate(Name
.size()));
301 memcpy(Buf
, Name
.data(), Name
.size());
302 File
= new (*this) MCDwarfFile(StringRef(Buf
, Name
.size()), DirIndex
);
304 // return the allocated FileNumber.
308 /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
309 /// currently is assigned and false otherwise.
310 bool MCContext::isValidDwarfFileNumber(unsigned FileNumber
) {
311 if(FileNumber
== 0 || FileNumber
>= MCDwarfFiles
.size())
314 return MCDwarfFiles
[FileNumber
] != 0;