blob: 5f3f12516960993ff1a2b461da6eaede04ee3f1f [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
Chris Lattnerfcdbf4e2009-07-31 16:43:49 +000028
29MCSection::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
35MCSection *MCSection::Create(const StringRef &Name, MCContext &Ctx) {
36 return new (Ctx) MCSection(Name, Ctx);
37}
38
39
Daniel Dunbarb5261eb2009-07-27 21:22:30 +000040MCSymbol *MCContext::CreateSymbol(const StringRef &Name) {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000041 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!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +000046 return Entry = new (*this) MCSymbol(Name, false);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000047}
48
Daniel Dunbarb5261eb2009-07-27 21:22:30 +000049MCSymbol *MCContext::GetOrCreateSymbol(const StringRef &Name) {
Chris Lattnerc69485e2009-06-24 04:31:49 +000050 MCSymbol *&Entry = Symbols[Name];
51 if (Entry) return Entry;
52
Daniel Dunbar71d259b2009-06-24 17:00:42 +000053 return Entry = new (*this) MCSymbol(Name, false);
Chris Lattnerc69485e2009-06-24 04:31:49 +000054}
55
56
Daniel Dunbarb5261eb2009-07-27 21:22:30 +000057MCSymbol *MCContext::CreateTemporarySymbol(const StringRef &Name) {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000058 // If unnamed, just create a symbol.
Daniel Dunbarb5261eb2009-07-27 21:22:30 +000059 if (Name.empty())
Daniel Dunbar71d259b2009-06-24 17:00:42 +000060 new (*this) MCSymbol("", true);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000061
62 // Otherwise create as usual.
63 MCSymbol *&Entry = Symbols[Name];
64 assert(!Entry && "Duplicate symbol definition!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +000065 return Entry = new (*this) MCSymbol(Name, true);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000066}
67
Daniel Dunbarb5261eb2009-07-27 21:22:30 +000068MCSymbol *MCContext::LookupSymbol(const StringRef &Name) const {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000069 return Symbols.lookup(Name);
70}
71
72void MCContext::ClearSymbolValue(MCSymbol *Sym) {
73 SymbolValues.erase(Sym);
74}
75
Daniel Dunbarba1da8a2009-06-23 23:39:15 +000076void MCContext::SetSymbolValue(MCSymbol *Sym, const MCValue &Value) {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000077 SymbolValues[Sym] = Value;
78}
79
Daniel Dunbarba1da8a2009-06-23 23:39:15 +000080const MCValue *MCContext::GetSymbolValue(MCSymbol *Sym) const {
81 DenseMap<MCSymbol*, MCValue>::iterator it = SymbolValues.find(Sym);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000082
83 if (it == SymbolValues.end())
84 return 0;
85
86 return &it->second;
87}