create sections with MCSection::Create instead of Context->getOrCreateSection.
[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 }
22
23 MCSection *MCContext::GetSection(const StringRef &Name) const {
24   StringMap<MCSection*>::const_iterator I = Sections.find(Name);
25   return I != Sections.end() ? I->second : 0;
26 }
27
28
29 MCSection::MCSection(const StringRef &_Name, MCContext &Ctx) : Name(_Name) {
30   MCSection *&Entry = Ctx.Sections[Name];
31   assert(Entry == 0 && "Multiple sections with the same name created");
32   Entry = this;
33 }
34
35 MCSection *MCSection::Create(const StringRef &Name, MCContext &Ctx) {
36   return new (Ctx) MCSection(Name, Ctx);
37 }
38
39
40 MCSymbol *MCContext::CreateSymbol(const StringRef &Name) {
41   assert(Name[0] != '\0' && "Normal symbols cannot be unnamed!");
42
43   // Create and bind the symbol, and ensure that names are unique.
44   MCSymbol *&Entry = Symbols[Name];
45   assert(!Entry && "Duplicate symbol definition!");
46   return Entry = new (*this) MCSymbol(Name, false);
47 }
48
49 MCSymbol *MCContext::GetOrCreateSymbol(const StringRef &Name) {
50   MCSymbol *&Entry = Symbols[Name];
51   if (Entry) return Entry;
52
53   return Entry = new (*this) MCSymbol(Name, false);
54 }
55
56
57 MCSymbol *MCContext::CreateTemporarySymbol(const StringRef &Name) {
58   // If unnamed, just create a symbol.
59   if (Name.empty())
60     new (*this) MCSymbol("", true);
61     
62   // Otherwise create as usual.
63   MCSymbol *&Entry = Symbols[Name];
64   assert(!Entry && "Duplicate symbol definition!");
65   return Entry = new (*this) MCSymbol(Name, true);
66 }
67
68 MCSymbol *MCContext::LookupSymbol(const StringRef &Name) const {
69   return Symbols.lookup(Name);
70 }
71
72 void MCContext::ClearSymbolValue(MCSymbol *Sym) {
73   SymbolValues.erase(Sym);
74 }
75
76 void MCContext::SetSymbolValue(MCSymbol *Sym, const MCValue &Value) {
77   SymbolValues[Sym] = Value;
78 }
79
80 const MCValue *MCContext::GetSymbolValue(MCSymbol *Sym) const {
81   DenseMap<MCSymbol*, MCValue>::iterator it = SymbolValues.find(Sym);
82
83   if (it == SymbolValues.end())
84     return 0;
85
86   return &it->second;
87 }