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/MCSymbol.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/Twine.h"
18 typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
21 MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) {
25 MCContext::~MCContext() {
26 // NOTE: The symbols are all allocated out of a bump pointer allocator,
27 // we don't need to free them here.
29 // If we have the MachO uniquing map, free it.
30 delete (MachOUniqueMapTy*)MachOUniquingMap;
33 //===----------------------------------------------------------------------===//
34 // Symbol Manipulation
35 //===----------------------------------------------------------------------===//
37 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
38 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
40 // Determine whether this is an assembler temporary or normal label.
41 bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix());
43 // Do the lookup and get the entire StringMapEntry. We want access to the
44 // key if we are creating the entry.
45 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
46 if (Entry.getValue()) return Entry.getValue();
48 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
49 // to the copy of the string that is embedded in the StringMapEntry.
50 MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary);
51 Entry.setValue(Result);
55 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
56 SmallString<128> NameSV;
57 Name.toVector(NameSV);
58 return GetOrCreateSymbol(NameSV.str());
61 MCSymbol *MCContext::CreateTempSymbol() {
62 return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) +
63 "tmp" + Twine(NextUniqueID++));
66 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
67 return Symbols.lookup(Name);
70 //===----------------------------------------------------------------------===//
72 //===----------------------------------------------------------------------===//
74 const MCSectionMachO *MCContext::
75 getMachOSection(StringRef Segment, StringRef Section,
76 unsigned TypeAndAttributes,
77 unsigned Reserved2, SectionKind Kind) {
79 // We unique sections by their segment/section pair. The returned section
80 // may not have the same flags as the requested section, if so this should be
81 // diagnosed by the client as an error.
83 // Create the map if it doesn't already exist.
84 if (MachOUniquingMap == 0)
85 MachOUniquingMap = new MachOUniqueMapTy();
86 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
88 // Form the name to look up.
94 // Do the lookup, if we have a hit, return it.
95 const MCSectionMachO *&Entry = Map[Name.str()];
96 if (Entry) return Entry;
98 // Otherwise, return a new section.
99 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
100 Reserved2, Kind, *this);