63338353a1f71c42f287814f0754866db11c47f0
[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
12 #include "llvm/MC/MCSection.h"
13 #include "llvm/MC/MCSymbol.h"
14 #include "llvm/MC/MCValue.h"
15 using namespace llvm;
16
17 MCContext::MCContext() {
18 }
19
20 MCContext::~MCContext() {
21   // NOTE: The sections are all allocated out of a bump pointer allocator,
22   // we don't need to free them here.
23 }
24
25 MCSection *MCContext::GetSection(const StringRef &Name) const {
26   StringMap<MCSection*>::const_iterator I = Sections.find(Name);
27   return I != Sections.end() ? I->second : 0;
28 }
29
30 MCSymbol *MCContext::CreateSymbol(const StringRef &Name) {
31   assert(Name[0] != '\0' && "Normal symbols cannot be unnamed!");
32
33   // Create and bind the symbol, and ensure that names are unique.
34   MCSymbol *&Entry = Symbols[Name];
35   assert(!Entry && "Duplicate symbol definition!");
36   return Entry = new (*this) MCSymbol(Name, false);
37 }
38
39 MCSymbol *MCContext::GetOrCreateSymbol(const StringRef &Name) {
40   MCSymbol *&Entry = Symbols[Name];
41   if (Entry) return Entry;
42
43   return Entry = new (*this) MCSymbol(Name, false);
44 }
45
46
47 MCSymbol *MCContext::CreateTemporarySymbol(const StringRef &Name) {
48   // If unnamed, just create a symbol.
49   if (Name.empty())
50     new (*this) MCSymbol("", true);
51     
52   // Otherwise create as usual.
53   MCSymbol *&Entry = Symbols[Name];
54   assert(!Entry && "Duplicate symbol definition!");
55   return Entry = new (*this) MCSymbol(Name, true);
56 }
57
58 MCSymbol *MCContext::LookupSymbol(const StringRef &Name) const {
59   return Symbols.lookup(Name);
60 }
61
62 void MCContext::ClearSymbolValue(MCSymbol *Sym) {
63   SymbolValues.erase(Sym);
64 }
65
66 void MCContext::SetSymbolValue(MCSymbol *Sym, const MCValue &Value) {
67   SymbolValues[Sym] = Value;
68 }
69
70 const MCValue *MCContext::GetSymbolValue(MCSymbol *Sym) const {
71   DenseMap<MCSymbol*, MCValue>::iterator it = SymbolValues.find(Sym);
72
73   if (it == SymbolValues.end())
74     return 0;
75
76   return &it->second;
77 }