blob: e6fb5c8809ba7853408d74643e2bdfe164a4353f [file] [log] [blame]
Daniel Dunbarba1da8a2009-06-23 23:39:15 +00001//===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
Daniel Dunbarecc63f82009-06-23 22:01:43 +00002//
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
Daniel Dunbarecc63f82009-06-23 22:01:43 +000012#include "llvm/MC/MCSection.h"
13#include "llvm/MC/MCSymbol.h"
Daniel Dunbarba1da8a2009-06-23 23:39:15 +000014#include "llvm/MC/MCValue.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000015using namespace llvm;
16
Chris Lattnerfcdbf4e2009-07-31 16:43:49 +000017MCContext::MCContext() {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000018}
19
20MCContext::~MCContext() {
21}
22
Chris Lattnerfcdbf4e2009-07-31 16:43:49 +000023MCSection *MCContext::GetSection(const StringRef &Name) const {
24 StringMap<MCSection*>::const_iterator I = Sections.find(Name);
25 return I != Sections.end() ? I->second : 0;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000026}
Daniel Dunbarecc63f82009-06-23 22:01:43 +000027
Daniel Dunbarb5261eb2009-07-27 21:22:30 +000028MCSymbol *MCContext::CreateSymbol(const StringRef &Name) {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000029 assert(Name[0] != '\0' && "Normal symbols cannot be unnamed!");
30
31 // Create and bind the symbol, and ensure that names are unique.
32 MCSymbol *&Entry = Symbols[Name];
33 assert(!Entry && "Duplicate symbol definition!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +000034 return Entry = new (*this) MCSymbol(Name, false);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000035}
36
Daniel Dunbarb5261eb2009-07-27 21:22:30 +000037MCSymbol *MCContext::GetOrCreateSymbol(const StringRef &Name) {
Chris Lattnerc69485e2009-06-24 04:31:49 +000038 MCSymbol *&Entry = Symbols[Name];
39 if (Entry) return Entry;
40
Daniel Dunbar71d259b2009-06-24 17:00:42 +000041 return Entry = new (*this) MCSymbol(Name, false);
Chris Lattnerc69485e2009-06-24 04:31:49 +000042}
43
44
Daniel Dunbarb5261eb2009-07-27 21:22:30 +000045MCSymbol *MCContext::CreateTemporarySymbol(const StringRef &Name) {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000046 // If unnamed, just create a symbol.
Daniel Dunbarb5261eb2009-07-27 21:22:30 +000047 if (Name.empty())
Daniel Dunbar71d259b2009-06-24 17:00:42 +000048 new (*this) MCSymbol("", true);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000049
50 // Otherwise create as usual.
51 MCSymbol *&Entry = Symbols[Name];
52 assert(!Entry && "Duplicate symbol definition!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +000053 return Entry = new (*this) MCSymbol(Name, true);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000054}
55
Daniel Dunbarb5261eb2009-07-27 21:22:30 +000056MCSymbol *MCContext::LookupSymbol(const StringRef &Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000057 return Symbols.lookup(Name);
58}
59
60void MCContext::ClearSymbolValue(MCSymbol *Sym) {
61 SymbolValues.erase(Sym);
62}
63
Daniel Dunbarba1da8a2009-06-23 23:39:15 +000064void MCContext::SetSymbolValue(MCSymbol *Sym, const MCValue &Value) {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000065 SymbolValues[Sym] = Value;
66}
67
Daniel Dunbarba1da8a2009-06-23 23:39:15 +000068const MCValue *MCContext::GetSymbolValue(MCSymbol *Sym) const {
69 DenseMap<MCSymbol*, MCValue>::iterator it = SymbolValues.find(Sym);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000070
71 if (it == SymbolValues.end())
72 return 0;
73
74 return &it->second;
75}