add a new CreateTempSymbol method, the use case for
[oota-llvm.git] / lib / MC / MCContext.cpp
1 //===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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"
16 using namespace llvm;
17
18 MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) {
19 }
20
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.
24 }
25
26 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
27   assert(!Name.empty() && "Normal symbols cannot be unnamed!");
28   MCSymbol *&Entry = Symbols[Name];
29   if (Entry) return Entry;
30
31   return Entry = new (*this) MCSymbol(Name, false);
32 }
33
34 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
35   SmallString<128> NameSV;
36   Name.toVector(NameSV);
37   return GetOrCreateSymbol(NameSV.str());
38 }
39
40 MCSymbol *MCContext::CreateTempSymbol() {
41   return GetOrCreateTemporarySymbol(Twine(MAI.getPrivateGlobalPrefix()) +
42                                     "tmp" + Twine(NextUniqueID++));
43 }
44
45
46 MCSymbol *MCContext::GetOrCreateTemporarySymbol(StringRef Name) {
47   // If there is no name, create a new anonymous symbol.
48   // FIXME: Remove this.  This form of the method should always take a name.
49   if (Name.empty())
50     return GetOrCreateTemporarySymbol(Twine(MAI.getPrivateGlobalPrefix()) +
51                                       "tmp" + Twine(NextUniqueID++));
52   
53   // Otherwise create as usual.
54   MCSymbol *&Entry = Symbols[Name];
55   if (Entry) return Entry;
56   return Entry = new (*this) MCSymbol(Name, true);
57 }
58
59 MCSymbol *MCContext::GetOrCreateTemporarySymbol(const Twine &Name) {
60   SmallString<128> NameSV;
61   Name.toVector(NameSV);
62   return GetOrCreateTemporarySymbol(NameSV.str());
63 }
64
65
66 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
67   return Symbols.lookup(Name);
68 }