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 | |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCSection.h" |
| 13 | #include "llvm/MC/MCSymbol.h" |
Daniel Dunbar | ba1da8a | 2009-06-23 23:39:15 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCValue.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 15 | using namespace llvm; |
| 16 | |
| 17 | MCContext::MCContext() |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | MCContext::~MCContext() { |
| 22 | } |
| 23 | |
Daniel Dunbar | b5261eb | 2009-07-27 21:22:30 +0000 | [diff] [blame^] | 24 | MCSection *MCContext::GetSection(const StringRef &Name) { |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 25 | MCSection *&Entry = Sections[Name]; |
| 26 | |
| 27 | if (!Entry) |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 28 | Entry = new (*this) MCSection(Name); |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 29 | |
| 30 | return Entry; |
| 31 | } |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 32 | |
Daniel Dunbar | b5261eb | 2009-07-27 21:22:30 +0000 | [diff] [blame^] | 33 | MCSymbol *MCContext::CreateSymbol(const StringRef &Name) { |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 34 | assert(Name[0] != '\0' && "Normal symbols cannot be unnamed!"); |
| 35 | |
| 36 | // Create and bind the symbol, and ensure that names are unique. |
| 37 | MCSymbol *&Entry = Symbols[Name]; |
| 38 | assert(!Entry && "Duplicate symbol definition!"); |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 39 | return Entry = new (*this) MCSymbol(Name, false); |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Daniel Dunbar | b5261eb | 2009-07-27 21:22:30 +0000 | [diff] [blame^] | 42 | MCSymbol *MCContext::GetOrCreateSymbol(const StringRef &Name) { |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 43 | MCSymbol *&Entry = Symbols[Name]; |
| 44 | if (Entry) return Entry; |
| 45 | |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 46 | return Entry = new (*this) MCSymbol(Name, false); |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | |
Daniel Dunbar | b5261eb | 2009-07-27 21:22:30 +0000 | [diff] [blame^] | 50 | MCSymbol *MCContext::CreateTemporarySymbol(const StringRef &Name) { |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 51 | // If unnamed, just create a symbol. |
Daniel Dunbar | b5261eb | 2009-07-27 21:22:30 +0000 | [diff] [blame^] | 52 | if (Name.empty()) |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 53 | new (*this) MCSymbol("", true); |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 54 | |
| 55 | // Otherwise create as usual. |
| 56 | MCSymbol *&Entry = Symbols[Name]; |
| 57 | assert(!Entry && "Duplicate symbol definition!"); |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 58 | return Entry = new (*this) MCSymbol(Name, true); |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Daniel Dunbar | b5261eb | 2009-07-27 21:22:30 +0000 | [diff] [blame^] | 61 | MCSymbol *MCContext::LookupSymbol(const StringRef &Name) const { |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 62 | return Symbols.lookup(Name); |
| 63 | } |
| 64 | |
| 65 | void MCContext::ClearSymbolValue(MCSymbol *Sym) { |
| 66 | SymbolValues.erase(Sym); |
| 67 | } |
| 68 | |
Daniel Dunbar | ba1da8a | 2009-06-23 23:39:15 +0000 | [diff] [blame] | 69 | void MCContext::SetSymbolValue(MCSymbol *Sym, const MCValue &Value) { |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 70 | SymbolValues[Sym] = Value; |
| 71 | } |
| 72 | |
Daniel Dunbar | ba1da8a | 2009-06-23 23:39:15 +0000 | [diff] [blame] | 73 | const MCValue *MCContext::GetSymbolValue(MCSymbol *Sym) const { |
| 74 | DenseMap<MCSymbol*, MCValue>::iterator it = SymbolValues.find(Sym); |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 75 | |
| 76 | if (it == SymbolValues.end()) |
| 77 | return 0; |
| 78 | |
| 79 | return &it->second; |
| 80 | } |