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" |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame^] | 12 | #include "llvm/MC/MCSectionMachO.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 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 | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame^] | 18 | typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy; |
| 19 | |
| 20 | |
Chris Lattner | c18409a | 2010-03-11 22:53:35 +0000 | [diff] [blame] | 21 | MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) { |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame^] | 22 | MachOUniquingMap = 0; |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | MCContext::~MCContext() { |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame^] | 26 | // NOTE: The symbols are all allocated out of a bump pointer allocator, |
Chris Lattner | c9d3152 | 2009-08-13 00:21:53 +0000 | [diff] [blame] | 27 | // we don't need to free them here. |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame^] | 28 | |
| 29 | // If we have the MachO uniquing map, free it. |
| 30 | delete (MachOUniqueMapTy*)MachOUniquingMap; |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame^] | 33 | //===----------------------------------------------------------------------===// |
| 34 | // Symbol Manipulation |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 37 | MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) { |
Chris Lattner | 00685bb | 2010-03-10 01:29:27 +0000 | [diff] [blame] | 38 | assert(!Name.empty() && "Normal symbols cannot be unnamed!"); |
Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 40 | // Determine whether this is an assembler temporary or normal label. |
| 41 | bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix()); |
| 42 | |
Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 43 | // Do the lookup and get the entire StringMapEntry. We want access to the |
| 44 | // key if we are creating the entry. |
| 45 | StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name); |
| 46 | if (Entry.getValue()) return Entry.getValue(); |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 47 | |
Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 48 | // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer |
| 49 | // to the copy of the string that is embedded in the StringMapEntry. |
| 50 | MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary); |
| 51 | Entry.setValue(Result); |
| 52 | return Result; |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 55 | MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) { |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 56 | SmallString<128> NameSV; |
| 57 | Name.toVector(NameSV); |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 58 | return GetOrCreateSymbol(NameSV.str()); |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Chris Lattner | 1d72a76 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 61 | MCSymbol *MCContext::CreateTempSymbol() { |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 62 | return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) + |
| 63 | "tmp" + Twine(NextUniqueID++)); |
Chris Lattner | 1d72a76 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 66 | MCSymbol *MCContext::LookupSymbol(StringRef Name) const { |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 67 | return Symbols.lookup(Name); |
| 68 | } |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame^] | 69 | |
| 70 | //===----------------------------------------------------------------------===// |
| 71 | // Section Management |
| 72 | //===----------------------------------------------------------------------===// |
| 73 | |
| 74 | const MCSectionMachO *MCContext:: |
| 75 | getMachOSection(StringRef Segment, StringRef Section, |
| 76 | unsigned TypeAndAttributes, |
| 77 | unsigned Reserved2, SectionKind Kind) { |
| 78 | |
| 79 | // We unique sections by their segment/section pair. The returned section |
| 80 | // may not have the same flags as the requested section, if so this should be |
| 81 | // diagnosed by the client as an error. |
| 82 | |
| 83 | // Create the map if it doesn't already exist. |
| 84 | if (MachOUniquingMap == 0) |
| 85 | MachOUniquingMap = new MachOUniqueMapTy(); |
| 86 | MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap; |
| 87 | |
| 88 | // Form the name to look up. |
| 89 | SmallString<64> Name; |
| 90 | Name += Segment; |
| 91 | Name.push_back(','); |
| 92 | Name += Section; |
| 93 | |
| 94 | // Do the lookup, if we have a hit, return it. |
| 95 | const MCSectionMachO *&Entry = Map[Name.str()]; |
| 96 | if (Entry) return Entry; |
| 97 | |
| 98 | // Otherwise, return a new section. |
| 99 | return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes, |
| 100 | Reserved2, Kind, *this); |
| 101 | } |