blob: cad0d569902b9d5172b297a6679c4e576f897088 [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
12#include "llvm/MC/MCAtom.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000013#include "llvm/MC/MCSection.h"
14#include "llvm/MC/MCSymbol.h"
Daniel Dunbarba1da8a2009-06-23 23:39:15 +000015#include "llvm/MC/MCValue.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000016using namespace llvm;
17
18MCContext::MCContext()
19{
20}
21
22MCContext::~MCContext() {
23}
24
25MCSection *MCContext::GetSection(const char *Name) {
26 MCSection *&Entry = Sections[Name];
27
28 if (!Entry)
Daniel Dunbara11af532009-06-24 01:03:06 +000029 Entry = new (*this) MCSection(Name);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000030
31 return Entry;
32}
33
34MCAtom *MCContext::CreateAtom(MCSection *Section) {
Daniel Dunbara11af532009-06-24 01:03:06 +000035 return new (*this) MCAtom(Section);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000036}
37
38MCSymbol *MCContext::CreateSymbol(MCAtom *Atom, const char *Name) {
39 assert(Name[0] != '\0' && "Normal symbols cannot be unnamed!");
40
41 // Create and bind the symbol, and ensure that names are unique.
42 MCSymbol *&Entry = Symbols[Name];
43 assert(!Entry && "Duplicate symbol definition!");
Daniel Dunbara11af532009-06-24 01:03:06 +000044 return Entry = new (*this) MCSymbol(Atom, Name, false);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000045}
46
47MCSymbol *MCContext::CreateTemporarySymbol(MCAtom *Atom, const char *Name) {
48 // If unnamed, just create a symbol.
49 if (Name[0] == '\0')
Daniel Dunbara11af532009-06-24 01:03:06 +000050 new (*this) MCSymbol(Atom, "", true);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000051
52 // Otherwise create as usual.
53 MCSymbol *&Entry = Symbols[Name];
54 assert(!Entry && "Duplicate symbol definition!");
Daniel Dunbara11af532009-06-24 01:03:06 +000055 return Entry = new (*this) MCSymbol(Atom, Name, true);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000056}
57
58MCSymbol *MCContext::LookupSymbol(const char *Name) const {
59 return Symbols.lookup(Name);
60}
61
62void MCContext::ClearSymbolValue(MCSymbol *Sym) {
63 SymbolValues.erase(Sym);
64}
65
Daniel Dunbarba1da8a2009-06-23 23:39:15 +000066void MCContext::SetSymbolValue(MCSymbol *Sym, const MCValue &Value) {
Daniel Dunbarecc63f82009-06-23 22:01:43 +000067 SymbolValues[Sym] = Value;
68}
69
Daniel Dunbarba1da8a2009-06-23 23:39:15 +000070const MCValue *MCContext::GetSymbolValue(MCSymbol *Sym) const {
71 DenseMap<MCSymbol*, MCValue>::iterator it = SymbolValues.find(Sym);
Daniel Dunbarecc63f82009-06-23 22:01:43 +000072
73 if (it == SymbolValues.end())
74 return 0;
75
76 return &it->second;
77}