1 //===- ConstantPools.cpp - ConstantPool class -----------------------------===// 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 // This file implements the ConstantPool and AssemblerConstantPools classes. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/MC/ConstantPools.h" 15 #include "llvm/MC/MCContext.h" 16 #include "llvm/MC/MCDirectives.h" 17 #include "llvm/MC/MCExpr.h" 18 #include "llvm/MC/MCStreamer.h" 19 #include "llvm/Support/Casting.h" 20 21 using namespace llvm; 22 23 // 24 // ConstantPool implementation 25 // 26 // Emit the contents of the constant pool using the provided streamer. 27 void ConstantPool::emitEntries(MCStreamer &Streamer) { 28 if (Entries.empty()) 29 return; 30 Streamer.EmitDataRegion(MCDR_DataRegion); 31 for (const ConstantPoolEntry &Entry : Entries) { 32 Streamer.EmitCodeAlignment(Entry.Size); // align naturally 33 Streamer.EmitLabel(Entry.Label); 34 Streamer.EmitValue(Entry.Value, Entry.Size, Entry.Loc); 35 } 36 Streamer.EmitDataRegion(MCDR_DataRegionEnd); 37 Entries.clear(); 38 } 39 40 const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context, 41 unsigned Size, SMLoc Loc) { 42 MCSymbol *CPEntryLabel = Context.createTempSymbol(); 43 44 Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc)); 45 return MCSymbolRefExpr::create(CPEntryLabel, Context); 46 } 47 48 bool ConstantPool::empty() { return Entries.empty(); } 49 50 void ConstantPool::clearCache() { 51 CachedEntries.clear(); 52 } 53 54 // 55 // AssemblerConstantPools implementation 56 // 57 ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) { 58 ConstantPoolMapTy::iterator CP = ConstantPools.find(Section); 59 if (CP == ConstantPools.end()) 60 return nullptr; 61 62 return &CP->second; 63 } 64 65 ConstantPool & 66 AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) { 67 return ConstantPools[Section]; 68 } 69 70 static void emitConstantPool(MCStreamer &Streamer, MCSection *Section, 71 ConstantPool &CP) { 72 if (!CP.empty()) { 73 Streamer.SwitchSection(Section); 74 CP.emitEntries(Streamer); 75 } 76 } 77 78 void AssemblerConstantPools::emitAll(MCStreamer &Streamer) { 79 // Dump contents of assembler constant pools. 80 for (auto &CPI : ConstantPools) { 81 MCSection *Section = CPI.first; 82 ConstantPool &CP = CPI.second; 83 84 emitConstantPool(Streamer, Section, CP); 85 } 86 } 87 88 void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) { 89 MCSection *Section = Streamer.getCurrentSectionOnly(); 90 if (ConstantPool *CP = getConstantPool(Section)) { 91 emitConstantPool(Streamer, Section, *CP); 92 } 93 } 94 95 void AssemblerConstantPools::clearCacheForCurrentSection(MCStreamer &Streamer) { 96 MCSection *Section = Streamer.getCurrentSectionOnly(); 97 if (ConstantPool *CP = getConstantPool(Section)) { 98 CP->clearCache(); 99 } 100 } 101 102 const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer, 103 const MCExpr *Expr, 104 unsigned Size, SMLoc Loc) { 105 MCSection *Section = Streamer.getCurrentSectionOnly(); 106 return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(), 107 Size, Loc); 108 } 109