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/ADT/SmallString.h"
18 #include "llvm/ADT/Twine.h"
21 typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
22 typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
23 typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
26 MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) {
31 SecureLogFile = getenv("AS_SECURE_LOG_FILE");
33 SecureLogUsed = false;
36 MCContext::~MCContext() {
37 // NOTE: The symbols are all allocated out of a bump pointer allocator,
38 // we don't need to free them here.
40 // If we have the MachO uniquing map, free it.
41 delete (MachOUniqueMapTy*)MachOUniquingMap;
42 delete (ELFUniqueMapTy*)ELFUniquingMap;
43 delete (COFFUniqueMapTy*)COFFUniquingMap;
45 // If the stream for the .secure_log_unique directive was created free it.
46 delete (raw_ostream*)SecureLog;
49 //===----------------------------------------------------------------------===//
50 // Symbol Manipulation
51 //===----------------------------------------------------------------------===//
53 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
54 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
56 // Determine whether this is an assembler temporary or normal label.
57 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
59 // Do the lookup and get the entire StringMapEntry. We want access to the
60 // key if we are creating the entry.
61 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
62 if (Entry.getValue()) return Entry.getValue();
64 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
65 // to the copy of the string that is embedded in the StringMapEntry.
66 MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary);
67 Entry.setValue(Result);
71 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
72 SmallString<128> NameSV;
73 Name.toVector(NameSV);
74 return GetOrCreateSymbol(NameSV.str());
77 MCSymbol *MCContext::CreateTempSymbol() {
78 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
79 "tmp" + Twine(NextUniqueID++));
82 unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
83 MCLabel *&Label = Instances[LocalLabelVal];
85 Label = new (*this) MCLabel(0);
86 return Label->incInstance();
89 unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
90 MCLabel *&Label = Instances[LocalLabelVal];
92 Label = new (*this) MCLabel(0);
93 return Label->getInstance();
96 MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
97 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
98 Twine(LocalLabelVal) +
100 Twine(NextInstance(LocalLabelVal)));
102 MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
104 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
105 Twine(LocalLabelVal) +
107 Twine(GetInstance(LocalLabelVal) + bORf));
110 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
111 return Symbols.lookup(Name);
114 //===----------------------------------------------------------------------===//
115 // Section Management
116 //===----------------------------------------------------------------------===//
118 const MCSectionMachO *MCContext::
119 getMachOSection(StringRef Segment, StringRef Section,
120 unsigned TypeAndAttributes,
121 unsigned Reserved2, SectionKind Kind) {
123 // We unique sections by their segment/section pair. The returned section
124 // may not have the same flags as the requested section, if so this should be
125 // diagnosed by the client as an error.
127 // Create the map if it doesn't already exist.
128 if (MachOUniquingMap == 0)
129 MachOUniquingMap = new MachOUniqueMapTy();
130 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
132 // Form the name to look up.
133 SmallString<64> Name;
138 // Do the lookup, if we have a hit, return it.
139 const MCSectionMachO *&Entry = Map[Name.str()];
140 if (Entry) return Entry;
142 // Otherwise, return a new section.
143 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
148 const MCSection *MCContext::
149 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
150 SectionKind Kind, bool IsExplicit) {
151 if (ELFUniquingMap == 0)
152 ELFUniquingMap = new ELFUniqueMapTy();
153 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
155 // Do the lookup, if we have a hit, return it.
156 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
157 if (Entry.getValue()) return Entry.getValue();
159 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
161 Entry.setValue(Result);
165 const MCSection *MCContext::getCOFFSection(StringRef Section,
166 unsigned Characteristics,
169 if (COFFUniquingMap == 0)
170 COFFUniquingMap = new COFFUniqueMapTy();
171 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
173 // Do the lookup, if we have a hit, return it.
174 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
175 if (Entry.getValue()) return Entry.getValue();
177 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
181 Entry.setValue(Result);