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/MCSymbol.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/Twine.h"
19 typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
20 typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
23 MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) {
28 MCContext::~MCContext() {
29 // NOTE: The symbols are all allocated out of a bump pointer allocator,
30 // we don't need to free them here.
32 // If we have the MachO uniquing map, free it.
33 delete (MachOUniqueMapTy*)MachOUniquingMap;
34 delete (ELFUniqueMapTy*)ELFUniquingMap;
37 //===----------------------------------------------------------------------===//
38 // Symbol Manipulation
39 //===----------------------------------------------------------------------===//
41 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
42 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
44 // Determine whether this is an assembler temporary or normal label.
45 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
47 // Do the lookup and get the entire StringMapEntry. We want access to the
48 // key if we are creating the entry.
49 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
50 if (Entry.getValue()) return Entry.getValue();
52 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
53 // to the copy of the string that is embedded in the StringMapEntry.
54 MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary);
55 Entry.setValue(Result);
59 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
60 SmallString<128> NameSV;
61 Name.toVector(NameSV);
62 return GetOrCreateSymbol(NameSV.str());
65 MCSymbol *MCContext::CreateTempSymbol() {
66 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
67 "tmp" + Twine(NextUniqueID++));
70 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
71 return Symbols.lookup(Name);
74 //===----------------------------------------------------------------------===//
76 //===----------------------------------------------------------------------===//
78 const MCSectionMachO *MCContext::
79 getMachOSection(StringRef Segment, StringRef Section,
80 unsigned TypeAndAttributes,
81 unsigned Reserved2, SectionKind Kind) {
83 // We unique sections by their segment/section pair. The returned section
84 // may not have the same flags as the requested section, if so this should be
85 // diagnosed by the client as an error.
87 // Create the map if it doesn't already exist.
88 if (MachOUniquingMap == 0)
89 MachOUniquingMap = new MachOUniqueMapTy();
90 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
92 // Form the name to look up.
98 // Do the lookup, if we have a hit, return it.
99 const MCSectionMachO *&Entry = Map[Name.str()];
100 if (Entry) return Entry;
102 // Otherwise, return a new section.
103 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
108 const MCSection *MCContext::
109 getELFSection(StringRef Section, unsigned Type, unsigned Flags,
110 SectionKind Kind, bool IsExplicit) {
111 if (ELFUniquingMap == 0)
112 ELFUniquingMap = new ELFUniqueMapTy();
113 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap;
115 // Do the lookup, if we have a hit, return it.
116 StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section);
117 if (Entry.getValue()) return Entry.getValue();
119 MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags,
121 Entry.setValue(Result);