Daniel Dunbar | ba1da8a | 2009-06-23 23:39:15 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===// |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 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/MCAtom.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCSection.h" |
| 14 | #include "llvm/MC/MCSymbol.h" |
Daniel Dunbar | ba1da8a | 2009-06-23 23:39:15 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCValue.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | |
| 18 | MCContext::MCContext() |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | MCContext::~MCContext() { |
| 23 | } |
| 24 | |
| 25 | MCSection *MCContext::GetSection(const char *Name) { |
| 26 | MCSection *&Entry = Sections[Name]; |
| 27 | |
| 28 | if (!Entry) |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 29 | Entry = new (*this) MCSection(Name); |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 30 | |
| 31 | return Entry; |
| 32 | } |
| 33 | |
| 34 | MCAtom *MCContext::CreateAtom(MCSection *Section) { |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 35 | return new (*this) MCAtom(Section); |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | MCSymbol *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 Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 44 | return Entry = new (*this) MCSymbol(Atom, Name, false); |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 47 | /// GetOrCreateSymbol - Lookup the symbol inside with the specified |
| 48 | /// @param Name. If it exists, return it. If not, create a forward |
| 49 | /// reference and return it. |
| 50 | /// |
| 51 | /// @param Name - The symbol name, which must be unique across all symbols. |
| 52 | MCSymbol *MCContext::GetOrCreateSymbol(const char *Name) { |
| 53 | MCSymbol *&Entry = Symbols[Name]; |
| 54 | if (Entry) return Entry; |
| 55 | |
| 56 | // FIXME: is a null atom the right way to make a forward ref? |
| 57 | return Entry = new (*this) MCSymbol(0, Name, false); |
| 58 | } |
| 59 | |
| 60 | |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 61 | MCSymbol *MCContext::CreateTemporarySymbol(MCAtom *Atom, const char *Name) { |
| 62 | // If unnamed, just create a symbol. |
| 63 | if (Name[0] == '\0') |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 64 | new (*this) MCSymbol(Atom, "", true); |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 65 | |
| 66 | // Otherwise create as usual. |
| 67 | MCSymbol *&Entry = Symbols[Name]; |
| 68 | assert(!Entry && "Duplicate symbol definition!"); |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 69 | return Entry = new (*this) MCSymbol(Atom, Name, true); |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | MCSymbol *MCContext::LookupSymbol(const char *Name) const { |
| 73 | return Symbols.lookup(Name); |
| 74 | } |
| 75 | |
| 76 | void MCContext::ClearSymbolValue(MCSymbol *Sym) { |
| 77 | SymbolValues.erase(Sym); |
| 78 | } |
| 79 | |
Daniel Dunbar | ba1da8a | 2009-06-23 23:39:15 +0000 | [diff] [blame] | 80 | void MCContext::SetSymbolValue(MCSymbol *Sym, const MCValue &Value) { |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 81 | SymbolValues[Sym] = Value; |
| 82 | } |
| 83 | |
Daniel Dunbar | ba1da8a | 2009-06-23 23:39:15 +0000 | [diff] [blame] | 84 | const MCValue *MCContext::GetSymbolValue(MCSymbol *Sym) const { |
| 85 | DenseMap<MCSymbol*, MCValue>::iterator it = SymbolValues.find(Sym); |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 86 | |
| 87 | if (it == SymbolValues.end()) |
| 88 | return 0; |
| 89 | |
| 90 | return &it->second; |
| 91 | } |