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"
12 #include "llvm/MC/MCSection.h"
13 #include "llvm/MC/MCSymbol.h"
14 #include "llvm/MC/MCValue.h"
17 MCContext::MCContext() {
20 MCContext::~MCContext() {
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;
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");
35 MCSection *MCSection::Create(const StringRef &Name, MCContext &Ctx) {
36 return new (Ctx) MCSection(Name, Ctx);
40 MCSymbol *MCContext::CreateSymbol(const StringRef &Name) {
41 assert(Name[0] != '\0' && "Normal symbols cannot be unnamed!");
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);
49 MCSymbol *MCContext::GetOrCreateSymbol(const StringRef &Name) {
50 MCSymbol *&Entry = Symbols[Name];
51 if (Entry) return Entry;
53 return Entry = new (*this) MCSymbol(Name, false);
57 MCSymbol *MCContext::CreateTemporarySymbol(const StringRef &Name) {
58 // If unnamed, just create a symbol.
60 new (*this) MCSymbol("", true);
62 // Otherwise create as usual.
63 MCSymbol *&Entry = Symbols[Name];
64 assert(!Entry && "Duplicate symbol definition!");
65 return Entry = new (*this) MCSymbol(Name, true);
68 MCSymbol *MCContext::LookupSymbol(const StringRef &Name) const {
69 return Symbols.lookup(Name);
72 void MCContext::ClearSymbolValue(MCSymbol *Sym) {
73 SymbolValues.erase(Sym);
76 void MCContext::SetSymbolValue(MCSymbol *Sym, const MCValue &Value) {
77 SymbolValues[Sym] = Value;
80 const MCValue *MCContext::GetSymbolValue(MCSymbol *Sym) const {
81 DenseMap<MCSymbol*, MCValue>::iterator it = SymbolValues.find(Sym);
83 if (it == SymbolValues.end())