1 //===- ConstantPools.cpp - ConstantPool class -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ConstantPool and AssemblerConstantPools classes.
12 //===----------------------------------------------------------------------===//
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"
24 // ConstantPool implementation
26 // Emit the contents of the constant pool using the provided streamer.
27 void ConstantPool::emitEntries(MCStreamer &Streamer) {
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);
36 Streamer.EmitDataRegion(MCDR_DataRegionEnd);
40 const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context,
41 unsigned Size, SMLoc Loc) {
42 const MCConstantExpr *C = dyn_cast<MCConstantExpr>(Value);
44 // Check if there is existing entry for the same constant. If so, reuse it.
45 auto Itr = C ? CachedEntries.find(C->getValue()) : CachedEntries.end();
46 if (Itr != CachedEntries.end())
49 MCSymbol *CPEntryLabel = Context.createTempSymbol();
51 Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
52 const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
54 CachedEntries[C->getValue()] = SymRef;
58 bool ConstantPool::empty() { return Entries.empty(); }
60 void ConstantPool::clearCache() {
61 CachedEntries.clear();
65 // AssemblerConstantPools implementation
67 ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
68 ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
69 if (CP == ConstantPools.end())
76 AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
77 return ConstantPools[Section];
80 static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
83 Streamer.SwitchSection(Section);
84 CP.emitEntries(Streamer);
88 void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {
89 // Dump contents of assembler constant pools.
90 for (auto &CPI : ConstantPools) {
91 MCSection *Section = CPI.first;
92 ConstantPool &CP = CPI.second;
94 emitConstantPool(Streamer, Section, CP);
98 void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
99 MCSection *Section = Streamer.getCurrentSectionOnly();
100 if (ConstantPool *CP = getConstantPool(Section))
101 emitConstantPool(Streamer, Section, *CP);
104 void AssemblerConstantPools::clearCacheForCurrentSection(MCStreamer &Streamer) {
105 MCSection *Section = Streamer.getCurrentSectionOnly();
106 if (ConstantPool *CP = getConstantPool(Section))
110 const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
112 unsigned Size, SMLoc Loc) {
113 MCSection *Section = Streamer.getCurrentSectionOnly();
114 return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),