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" |
Chris Lattner | 1a5c28f | 2010-03-11 22:56:10 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCSection.h" |
| 13 | #include "llvm/MC/MCSymbol.h" |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallString.h" |
| 15 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | |
Chris Lattner | c18409a | 2010-03-11 22:53:35 +0000 | [diff] [blame] | 18 | MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) { |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | MCContext::~MCContext() { |
Chris Lattner | c9d3152 | 2009-08-13 00:21:53 +0000 | [diff] [blame] | 22 | // NOTE: The sections are all allocated out of a bump pointer allocator, |
| 23 | // we don't need to free them here. |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 26 | MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) { |
Chris Lattner | 00685bb | 2010-03-10 01:29:27 +0000 | [diff] [blame] | 27 | assert(!Name.empty() && "Normal symbols cannot be unnamed!"); |
Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 29 | // Determine whether this is an assembler temporary or normal label. |
| 30 | bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix()); |
| 31 | |
Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 32 | // Do the lookup and get the entire StringMapEntry. We want access to the |
| 33 | // key if we are creating the entry. |
| 34 | StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name); |
| 35 | if (Entry.getValue()) return Entry.getValue(); |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 36 | |
Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 37 | // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer |
| 38 | // to the copy of the string that is embedded in the StringMapEntry. |
| 39 | MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary); |
| 40 | Entry.setValue(Result); |
| 41 | return Result; |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 44 | MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) { |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 45 | SmallString<128> NameSV; |
| 46 | Name.toVector(NameSV); |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 47 | return GetOrCreateSymbol(NameSV.str()); |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Chris Lattner | 1d72a76 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 50 | MCSymbol *MCContext::CreateTempSymbol() { |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 51 | return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) + |
| 52 | "tmp" + Twine(NextUniqueID++)); |
Chris Lattner | 1d72a76 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 55 | MCSymbol *MCContext::LookupSymbol(StringRef Name) const { |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 56 | return Symbols.lookup(Name); |
| 57 | } |