Mercurial > hg > CbC > CbC_llvm
comparison lib/MC/MCContext.cpp @ 83:60c9769439b8 LLVM3.7
LLVM 3.7
author | Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 18 Feb 2015 14:55:36 +0900 |
parents | 54457678186b |
children | afa8332a0e37 |
comparison
equal
deleted
inserted
replaced
78:af83660cff7b | 83:60c9769439b8 |
---|---|
71 void MCContext::reset() { | 71 void MCContext::reset() { |
72 UsedNames.clear(); | 72 UsedNames.clear(); |
73 Symbols.clear(); | 73 Symbols.clear(); |
74 Allocator.Reset(); | 74 Allocator.Reset(); |
75 Instances.clear(); | 75 Instances.clear(); |
76 CompilationDir.clear(); | |
77 MainFileName.clear(); | |
76 MCDwarfLineTablesCUMap.clear(); | 78 MCDwarfLineTablesCUMap.clear(); |
79 SectionStartEndSyms.clear(); | |
77 MCGenDwarfLabelEntries.clear(); | 80 MCGenDwarfLabelEntries.clear(); |
78 DwarfDebugFlags = StringRef(); | 81 DwarfDebugFlags = StringRef(); |
79 DwarfCompileUnitID = 0; | 82 DwarfCompileUnitID = 0; |
80 CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0); | 83 CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0); |
81 | 84 |
95 //===----------------------------------------------------------------------===// | 98 //===----------------------------------------------------------------------===// |
96 | 99 |
97 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) { | 100 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) { |
98 assert(!Name.empty() && "Normal symbols cannot be unnamed!"); | 101 assert(!Name.empty() && "Normal symbols cannot be unnamed!"); |
99 | 102 |
100 // Do the lookup and get the entire StringMapEntry. We want access to the | 103 MCSymbol *&Sym = Symbols[Name]; |
101 // key if we are creating the entry. | 104 |
102 StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name); | 105 if (!Sym) |
103 MCSymbol *Sym = Entry.getValue(); | 106 Sym = CreateSymbol(Name); |
104 | 107 |
108 return Sym; | |
109 } | |
110 | |
111 MCSymbol *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) { | |
112 MCSymbol *&Sym = SectionSymbols[&Section]; | |
105 if (Sym) | 113 if (Sym) |
106 return Sym; | 114 return Sym; |
107 | 115 |
108 Sym = CreateSymbol(Name); | 116 StringRef Name = Section.getSectionName(); |
109 Entry.setValue(Sym); | 117 |
118 MCSymbol *&OldSym = Symbols[Name]; | |
119 if (OldSym && OldSym->isUndefined()) { | |
120 Sym = OldSym; | |
121 return OldSym; | |
122 } | |
123 | |
124 auto NameIter = UsedNames.insert(std::make_pair(Name, true)).first; | |
125 Sym = new (*this) MCSymbol(NameIter->getKey(), /*isTemporary*/ false); | |
126 | |
127 if (!OldSym) | |
128 OldSym = Sym; | |
129 | |
110 return Sym; | 130 return Sym; |
131 } | |
132 | |
133 MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName) { | |
134 return GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + | |
135 "frameallocation_" + FuncName); | |
111 } | 136 } |
112 | 137 |
113 MCSymbol *MCContext::CreateSymbol(StringRef Name) { | 138 MCSymbol *MCContext::CreateSymbol(StringRef Name) { |
114 // Determine whether this is an assembler temporary or normal label, if used. | 139 // Determine whether this is an assembler temporary or normal label, if used. |
115 bool isTemporary = false; | 140 bool isTemporary = false; |
116 if (AllowTemporaryLabels) | 141 if (AllowTemporaryLabels) |
117 isTemporary = Name.startswith(MAI->getPrivateGlobalPrefix()); | 142 isTemporary = Name.startswith(MAI->getPrivateGlobalPrefix()); |
118 | 143 |
119 StringMapEntry<bool> *NameEntry = &UsedNames.GetOrCreateValue(Name); | 144 auto NameEntry = UsedNames.insert(std::make_pair(Name, true)); |
120 if (NameEntry->getValue()) { | 145 if (!NameEntry.second) { |
121 assert(isTemporary && "Cannot rename non-temporary symbols"); | 146 assert(isTemporary && "Cannot rename non-temporary symbols"); |
122 SmallString<128> NewName = Name; | 147 SmallString<128> NewName = Name; |
123 do { | 148 do { |
124 NewName.resize(Name.size()); | 149 NewName.resize(Name.size()); |
125 raw_svector_ostream(NewName) << NextUniqueID++; | 150 raw_svector_ostream(NewName) << NextUniqueID++; |
126 NameEntry = &UsedNames.GetOrCreateValue(NewName); | 151 NameEntry = UsedNames.insert(std::make_pair(NewName, true)); |
127 } while (NameEntry->getValue()); | 152 } while (!NameEntry.second); |
128 } | 153 } |
129 NameEntry->setValue(true); | |
130 | 154 |
131 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer | 155 // Ok, the entry doesn't already exist. Have the MCSymbol object itself refer |
132 // to the copy of the string that is embedded in the UsedNames entry. | 156 // to the copy of the string that is embedded in the UsedNames entry. |
133 MCSymbol *Result = new (*this) MCSymbol(NameEntry->getKey(), isTemporary); | 157 MCSymbol *Result = |
158 new (*this) MCSymbol(NameEntry.first->getKey(), isTemporary); | |
134 | 159 |
135 return Result; | 160 return Result; |
136 } | 161 } |
137 | 162 |
138 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) { | 163 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) { |
225 // Otherwise, return a new section. | 250 // Otherwise, return a new section. |
226 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes, | 251 return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes, |
227 Reserved2, Kind); | 252 Reserved2, Kind); |
228 } | 253 } |
229 | 254 |
230 const MCSectionELF *MCContext:: | 255 const MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type, |
231 getELFSection(StringRef Section, unsigned Type, unsigned Flags, | 256 unsigned Flags) { |
232 SectionKind Kind) { | 257 return getELFSection(Section, Type, Flags, 0, ""); |
233 return getELFSection(Section, Type, Flags, Kind, 0, ""); | |
234 } | 258 } |
235 | 259 |
236 void MCContext::renameELFSection(const MCSectionELF *Section, StringRef Name) { | 260 void MCContext::renameELFSection(const MCSectionELF *Section, StringRef Name) { |
237 StringRef GroupName; | 261 StringRef GroupName; |
238 if (const MCSymbol *Group = Section->getGroup()) | 262 if (const MCSymbol *Group = Section->getGroup()) |
244 Section)).first; | 268 Section)).first; |
245 StringRef CachedName = I->first.first; | 269 StringRef CachedName = I->first.first; |
246 const_cast<MCSectionELF*>(Section)->setSectionName(CachedName); | 270 const_cast<MCSectionELF*>(Section)->setSectionName(CachedName); |
247 } | 271 } |
248 | 272 |
249 const MCSectionELF *MCContext:: | 273 const MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type, |
250 getELFSection(StringRef Section, unsigned Type, unsigned Flags, | 274 unsigned Flags, unsigned EntrySize, |
251 SectionKind Kind, unsigned EntrySize, StringRef Group) { | 275 StringRef Group, bool Unique) { |
252 // Do the lookup, if we have a hit, return it. | 276 // Do the lookup, if we have a hit, return it. |
253 auto IterBool = ELFUniquingMap.insert( | 277 auto IterBool = ELFUniquingMap.insert( |
254 std::make_pair(SectionGroupPair(Section, Group), nullptr)); | 278 std::make_pair(SectionGroupPair(Section, Group), nullptr)); |
255 auto &Entry = *IterBool.first; | 279 auto &Entry = *IterBool.first; |
256 if (!IterBool.second) return Entry.second; | 280 if (!IterBool.second && !Unique) |
257 | 281 return Entry.second; |
258 // Possibly refine the entry size first. | |
259 if (!EntrySize) { | |
260 EntrySize = MCSectionELF::DetermineEntrySize(Kind); | |
261 } | |
262 | 282 |
263 MCSymbol *GroupSym = nullptr; | 283 MCSymbol *GroupSym = nullptr; |
264 if (!Group.empty()) | 284 if (!Group.empty()) |
265 GroupSym = GetOrCreateSymbol(Group); | 285 GroupSym = GetOrCreateSymbol(Group); |
266 | 286 |
267 StringRef CachedName = Entry.first.first; | 287 StringRef CachedName = Entry.first.first; |
288 | |
289 SectionKind Kind; | |
290 if (Flags & ELF::SHF_EXECINSTR) | |
291 Kind = SectionKind::getText(); | |
292 else | |
293 Kind = SectionKind::getReadOnly(); | |
294 | |
268 MCSectionELF *Result = new (*this) | 295 MCSectionELF *Result = new (*this) |
269 MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym); | 296 MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym, Unique); |
270 Entry.second = Result; | 297 if (!Unique) |
298 Entry.second = Result; | |
271 return Result; | 299 return Result; |
300 } | |
301 | |
302 const MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type, | |
303 unsigned Flags, unsigned EntrySize, | |
304 StringRef Group) { | |
305 return getELFSection(Section, Type, Flags, EntrySize, Group, false); | |
272 } | 306 } |
273 | 307 |
274 const MCSectionELF *MCContext::CreateELFGroupSection() { | 308 const MCSectionELF *MCContext::CreateELFGroupSection() { |
275 MCSectionELF *Result = | 309 MCSectionELF *Result = |
276 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0, | 310 new (*this) MCSectionELF(".group", ELF::SHT_GROUP, 0, |
277 SectionKind::getReadOnly(), 4, nullptr); | 311 SectionKind::getReadOnly(), 4, nullptr, false); |
278 return Result; | 312 return Result; |
279 } | 313 } |
280 | 314 |
281 const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section, | 315 const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section, |
282 unsigned Characteristics, | 316 unsigned Characteristics, |