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