1 //===- ConstantPools.cpp - ConstantPool class --*- C++ -*---------===//
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 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/MapVector.h"
14 #include "llvm/MC/ConstantPools.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCStreamer.h"
21 // ConstantPool implementation
23 // Emit the contents of the constant pool using the provided streamer.
24 void ConstantPool::emitEntries(MCStreamer &Streamer) {
27 Streamer.EmitDataRegion(MCDR_DataRegion);
28 for (const ConstantPoolEntry &Entry : Entries) {
29 Streamer.EmitCodeAlignment(Entry.Size); // align naturally
30 Streamer.EmitLabel(Entry.Label);
31 Streamer.EmitValue(Entry.Value, Entry.Size, Entry.Loc);
33 Streamer.EmitDataRegion(MCDR_DataRegionEnd);
37 const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context,
38 unsigned Size, SMLoc Loc) {
39 MCSymbol *CPEntryLabel = Context.createTempSymbol();
41 Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
42 return MCSymbolRefExpr::create(CPEntryLabel, Context);
45 bool ConstantPool::empty() { return Entries.empty(); }
48 // AssemblerConstantPools implementation
50 ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
51 ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
52 if (CP == ConstantPools.end())
59 AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
60 return ConstantPools[Section];
63 static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
66 Streamer.SwitchSection(Section);
67 CP.emitEntries(Streamer);
71 void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {
72 // Dump contents of assembler constant pools.
73 for (auto &CPI : ConstantPools) {
74 MCSection *Section = CPI.first;
75 ConstantPool &CP = CPI.second;
77 emitConstantPool(Streamer, Section, CP);
81 void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
82 MCSection *Section = Streamer.getCurrentSection().first;
83 if (ConstantPool *CP = getConstantPool(Section)) {
84 emitConstantPool(Streamer, Section, *CP);
88 const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
90 unsigned Size, SMLoc Loc) {
91 MCSection *Section = Streamer.getCurrentSection().first;
92 return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),