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" |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCSectionELF.h" |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCSectionCOFF.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCSymbol.h" |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame^] | 16 | #include "llvm/MC/MCLabel.h" |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
| 18 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 21 | typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy; |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 22 | typedef StringMap<const MCSectionELF*> ELFUniqueMapTy; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 23 | typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy; |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 24 | |
| 25 | |
Chris Lattner | c18409a | 2010-03-11 22:53:35 +0000 | [diff] [blame] | 26 | MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) { |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 27 | MachOUniquingMap = 0; |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 28 | ELFUniquingMap = 0; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 29 | COFFUniquingMap = 0; |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | MCContext::~MCContext() { |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 33 | // NOTE: The symbols are all allocated out of a bump pointer allocator, |
Chris Lattner | c9d3152 | 2009-08-13 00:21:53 +0000 | [diff] [blame] | 34 | // we don't need to free them here. |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 35 | |
| 36 | // If we have the MachO uniquing map, free it. |
| 37 | delete (MachOUniqueMapTy*)MachOUniquingMap; |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 38 | delete (ELFUniqueMapTy*)ELFUniquingMap; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 39 | delete (COFFUniqueMapTy*)COFFUniquingMap; |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
| 43 | // Symbol Manipulation |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 46 | MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) { |
Chris Lattner | 00685bb | 2010-03-10 01:29:27 +0000 | [diff] [blame] | 47 | assert(!Name.empty() && "Normal symbols cannot be unnamed!"); |
Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 49 | // Determine whether this is an assembler temporary or normal label. |
| 50 | bool isTemporary = Name.startswith(MAI.getPrivateGlobalPrefix()); |
| 51 | |
Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 52 | // Do the lookup and get the entire StringMapEntry. We want access to the |
| 53 | // key if we are creating the entry. |
| 54 | StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name); |
| 55 | if (Entry.getValue()) return Entry.getValue(); |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 56 | |
Chris Lattner | c28cc09 | 2010-03-15 06:15:35 +0000 | [diff] [blame] | 57 | // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer |
| 58 | // to the copy of the string that is embedded in the StringMapEntry. |
| 59 | MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary); |
| 60 | Entry.setValue(Result); |
| 61 | return Result; |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 64 | MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) { |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 65 | SmallString<128> NameSV; |
| 66 | Name.toVector(NameSV); |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 67 | return GetOrCreateSymbol(NameSV.str()); |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Chris Lattner | 1d72a76 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 70 | MCSymbol *MCContext::CreateTempSymbol() { |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 71 | return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) + |
| 72 | "tmp" + Twine(NextUniqueID++)); |
Chris Lattner | 1d72a76 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame^] | 75 | unsigned MCContext::NextInstance(int64_t LocalLabelVal) { |
| 76 | unsigned Instance; |
| 77 | MCLabel *Label; |
| 78 | Label = Instances[LocalLabelVal]; |
| 79 | if (Label) { |
| 80 | Instance = Label->incInstance(); |
| 81 | } |
| 82 | else { |
| 83 | Instance = 1; |
| 84 | Label = new MCLabel(Instance); |
| 85 | Instances[LocalLabelVal] = Label; |
| 86 | } |
| 87 | return Instance; |
| 88 | } |
| 89 | |
| 90 | unsigned MCContext::GetInstance(int64_t LocalLabelVal) { |
| 91 | int Instance; |
| 92 | MCLabel *Label; |
| 93 | Label = Instances[LocalLabelVal]; |
| 94 | if (Label) { |
| 95 | Instance = Label->getInstance(); |
| 96 | } |
| 97 | else { |
| 98 | Instance = 0; |
| 99 | Label = new MCLabel(Instance); |
| 100 | Instances[LocalLabelVal] = Label; |
| 101 | } |
| 102 | return Instance; |
| 103 | } |
| 104 | |
| 105 | MCSymbol *MCContext::CreateDirectionalLocalSymbol(int64_t LocalLabelVal) { |
| 106 | return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) + |
| 107 | Twine(LocalLabelVal) + |
| 108 | "\2" + |
| 109 | Twine(NextInstance(LocalLabelVal))); |
| 110 | } |
| 111 | MCSymbol *MCContext::GetDirectionalLocalSymbol(int64_t LocalLabelVal, |
| 112 | int bORf) { |
| 113 | return GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix()) + |
| 114 | Twine(LocalLabelVal) + |
| 115 | "\2" + |
| 116 | Twine(GetInstance(LocalLabelVal) + bORf)); |
| 117 | } |
| 118 | |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 119 | MCSymbol *MCContext::LookupSymbol(StringRef Name) const { |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 120 | return Symbols.lookup(Name); |
| 121 | } |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 122 | |
| 123 | //===----------------------------------------------------------------------===// |
| 124 | // Section Management |
| 125 | //===----------------------------------------------------------------------===// |
| 126 | |
| 127 | const MCSectionMachO *MCContext:: |
| 128 | getMachOSection(StringRef Segment, StringRef Section, |
| 129 | unsigned TypeAndAttributes, |
| 130 | unsigned Reserved2, SectionKind Kind) { |
| 131 | |
| 132 | // We unique sections by their segment/section pair. The returned section |
| 133 | // may not have the same flags as the requested section, if so this should be |
| 134 | // diagnosed by the client as an error. |
| 135 | |
| 136 | // Create the map if it doesn't already exist. |
| 137 | if (MachOUniquingMap == 0) |
| 138 | MachOUniquingMap = new MachOUniqueMapTy(); |
| 139 | MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap; |
| 140 | |
| 141 | // Form the name to look up. |
| 142 | SmallString<64> Name; |
| 143 | Name += Segment; |
| 144 | Name.push_back(','); |
| 145 | Name += Section; |
| 146 | |
| 147 | // Do the lookup, if we have a hit, return it. |
| 148 | const MCSectionMachO *&Entry = Map[Name.str()]; |
| 149 | if (Entry) return Entry; |
| 150 | |
| 151 | // Otherwise, return a new section. |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 152 | return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes, |
| 153 | Reserved2, Kind); |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 154 | } |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 155 | |
| 156 | |
| 157 | const MCSection *MCContext:: |
| 158 | getELFSection(StringRef Section, unsigned Type, unsigned Flags, |
| 159 | SectionKind Kind, bool IsExplicit) { |
| 160 | if (ELFUniquingMap == 0) |
| 161 | ELFUniquingMap = new ELFUniqueMapTy(); |
| 162 | ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap; |
| 163 | |
| 164 | // Do the lookup, if we have a hit, return it. |
| 165 | StringMapEntry<const MCSectionELF*> &Entry = Map.GetOrCreateValue(Section); |
| 166 | if (Entry.getValue()) return Entry.getValue(); |
| 167 | |
| 168 | MCSectionELF *Result = new (*this) MCSectionELF(Entry.getKey(), Type, Flags, |
| 169 | Kind, IsExplicit); |
| 170 | Entry.setValue(Result); |
| 171 | return Result; |
| 172 | } |
| 173 | |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 174 | const MCSection *MCContext::getCOFFSection(StringRef Section, |
| 175 | unsigned Characteristics, |
| 176 | int Selection, |
| 177 | SectionKind Kind) { |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 178 | if (COFFUniquingMap == 0) |
| 179 | COFFUniquingMap = new COFFUniqueMapTy(); |
| 180 | COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap; |
| 181 | |
| 182 | // Do the lookup, if we have a hit, return it. |
| 183 | StringMapEntry<const MCSectionCOFF*> &Entry = Map.GetOrCreateValue(Section); |
| 184 | if (Entry.getValue()) return Entry.getValue(); |
| 185 | |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 186 | MCSectionCOFF *Result = new (*this) MCSectionCOFF(Entry.getKey(), |
| 187 | Characteristics, |
| 188 | Selection, Kind); |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 189 | |
| 190 | Entry.setValue(Result); |
| 191 | return Result; |
| 192 | } |