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/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/MC/ConstantPools.h"
21 // ConstantPool implementation
23 // Emit the contents of the constant pool using the provided streamer.
24 void ConstantPool::emitEntries(MCStreamer &Streamer) {
27 Streamer.EmitCodeAlignment(4); // align to 4-byte address
28 Streamer.EmitDataRegion(MCDR_DataRegion);
29 for (EntryVecTy::const_iterator I = Entries.begin(), E = Entries.end();
31 Streamer.EmitLabel(I->first);
32 Streamer.EmitValue(I->second, 4);
34 Streamer.EmitDataRegion(MCDR_DataRegionEnd);
38 const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context) {
39 MCSymbol *CPEntryLabel = Context.CreateTempSymbol();
41 Entries.push_back(std::make_pair(CPEntryLabel, Value));
42 return MCSymbolRefExpr::Create(CPEntryLabel, Context);
45 bool ConstantPool::empty() { return Entries.empty(); }
48 // AssemblerConstantPools implementation
51 AssemblerConstantPools::getConstantPool(const MCSection *Section) {
52 ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
53 if (CP == ConstantPools.end())
60 AssemblerConstantPools::getOrCreateConstantPool(const MCSection *Section) {
61 return ConstantPools[Section];
64 static void emitConstantPool(MCStreamer &Streamer, const MCSection *Section,
67 Streamer.SwitchSection(Section);
68 CP.emitEntries(Streamer);
72 void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {
73 // Dump contents of assembler constant pools.
74 for (ConstantPoolMapTy::iterator CPI = ConstantPools.begin(),
75 CPE = ConstantPools.end();
77 const MCSection *Section = CPI->first;
78 ConstantPool &CP = CPI->second;
80 emitConstantPool(Streamer, Section, CP);
84 void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
85 const MCSection *Section = Streamer.getCurrentSection().first;
86 if (ConstantPool *CP = getConstantPool(Section)) {
87 emitConstantPool(Streamer, Section, *CP);
91 const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
93 const MCSection *Section = Streamer.getCurrentSection().first;
94 return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext());