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) {
32 MCContext::~MCContext() {
33 // NOTE: The symbols are all allocated out of a bump pointer allocator,
34 // we don't need to free them here.
36 // If we have the MachO uniquing map, free it.
37 delete (MachOUniqueMapTy*)MachOUniquingMap;
38 delete (ELFUniqueMapTy*)ELFUniquingMap;
39 delete (COFFUniqueMapTy*)COFFUniquingMap;
42 //===----------------------------------------------------------------------===//
43 // Symbol Manipulation
44 //===----------------------------------------------------------------------===//
46 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
47 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
49 // Determine whether this is an assembler temporary or normal label.
50 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
52 // Do the lookup and get the entire StringMapEntry. We want access to the
53 // key if we are creating the entry.
54 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
55 if (Entry.getValue()) return Entry.getValue();
57 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
58 // to the copy of the string that is embedded in the StringMapEntry.
59 MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary);
60 Entry.setValue(Result);
64 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
65 SmallString<128> NameSV;
66 Name.toVector(NameSV);
67 return GetOrCreateSymbol(NameSV.str());
70 MCSymbol *MCContext::CreateTempSymbol() {
71 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
72 "tmp" + Twine(NextUniqueID++));
75 unsigned MCContext::NextInstance(int64_t LocalLabelVal) {
76 MCLabel *&Label = Instances[LocalLabelVal];
78 Label = new (*this) MCLabel(0);
79 return Label->incInstance();
82 unsigned MCContext::GetInstance(int64_t LocalLabelVal) {
83 MCLabel *&Label = Instances[LocalLabelVal];
85 Label = new (*this) MCLabel(0);
86 return Label->getInstance();
89 MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) {
90 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
91 Twine(LocalLabelVal) +
93 Twine(NextInstance(LocalLabelVal)));
95 MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal,
97 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
98 Twine(LocalLabelVal) +
100 Twine(GetInstance(LocalLabelVal) + bORf));
103 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
104 return Symbols.lookup(Name);
107 //===----------------------------------------------------------------------===//
108 // Section Management
109 //===----------------------------------------------------------------------===//
111 const MCSectionMachO *MCContext::
112 getMachOSection(StringRef Segment, StringRef Section,
113 unsigned TypeAndAttributes,
114 unsigned Reserved2, SectionKind Kind) {
116 // We unique sections by their segment/section pair. The returned section
117 // may not have the same flags as the requested section, if so this should be
118 // diagnosed by the client as an error.
120 // Create the map if it doesn't already exist.
121 if (MachOUniquingMap == 0)
122 MachOUniquingMap = new MachOUniqueMapTy();
123 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
125 // Form the name to look up.
126 SmallString<64> Name;
131 // Do the lookup, if we have a hit, return it.
132 const MCSectionMachO *&Entry = Map[Name.str()];
133 if (Entry) return Entry;
135 // Otherwise, return a new section.
136 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
141 const MCSection *MCContext::
142 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
143 SectionKind Kind, bool IsExplicit) {
144 if (ELFUniquingMap == 0)
145 ELFUniquingMap = new ELFUniqueMapTy();
146 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
148 // Do the lookup, if we have a hit, return it.
149 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
150 if (Entry.getValue()) return Entry.getValue();
152 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
154 Entry.setValue(Result);
158 const MCSection *MCContext::getCOFFSection(StringRef Section,
159 unsigned Characteristics,
162 if (COFFUniquingMap == 0)
163 COFFUniquingMap = new COFFUniqueMapTy();
164 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap;
166 // Do the lookup, if we have a hit, return it.
167 StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section);
168 if (Entry.getValue()) return Entry.getValue();
170 MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(),
174 Entry.setValue(Result);