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/MCSection.h"
13 #include "llvm/MC/MCSymbol.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/Twine.h"
18 MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) {
21 MCContext::~MCContext() {
22 // NOTE: The sections are all allocated out of a bump pointer allocator,
23 // we don't need to free them here.
26 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name, bool isTemporary) {
27 assert(!Name.empty() && "Normal symbols cannot be unnamed!");
29 // Do the lookup and get the entire StringMapEntry. We want access to the
30 // key if we are creating the entry.
31 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
32 if (Entry.getValue()) return Entry.getValue();
34 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
35 // to the copy of the string that is embedded in the StringMapEntry.
36 MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary);
37 Entry.setValue(Result);
41 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name, bool isTemporary) {
42 SmallString<128> NameSV;
43 Name.toVector(NameSV);
44 return GetOrCreateSymbol(NameSV.str(), isTemporary);
47 MCSymbol *MCContext::CreateTempSymbol() {
48 return GetOrCreateTemporarySymbol(Twine(MAI.getPrivateGlobalPrefix()) +
49 "tmp" + Twine(NextUniqueID++));
52 MCSymbol *MCContext::GetOrCreateTemporarySymbol(const Twine &Name) {
53 SmallString<128> NameSV;
54 Name.toVector(NameSV);
55 return GetOrCreateTemporarySymbol(NameSV.str());
59 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
60 return Symbols.lookup(Name);