blob: 18277a225640459eedcceb76b28c016ab8795d2c [file] [log] [blame]
Eugene Zelenkof31871c2017-02-07 23:02:00 +00001//===- ConstantPools.cpp - ConstantPool class -----------------------------===//
Weiming Zhaode04c482014-06-18 18:17:25 +00002//
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//===----------------------------------------------------------------------===//
Eugene Zelenkof31871c2017-02-07 23:02:00 +000013
Chandler Carruth1b279142015-01-14 11:23:27 +000014#include "llvm/MC/ConstantPools.h"
Weiming Zhaode04c482014-06-18 18:17:25 +000015#include "llvm/MC/MCContext.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000016#include "llvm/MC/MCDirectives.h"
Weiming Zhaode04c482014-06-18 18:17:25 +000017#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCStreamer.h"
Eugene Zelenkof31871c2017-02-07 23:02:00 +000019#include "llvm/Support/Casting.h"
Weiming Zhaode04c482014-06-18 18:17:25 +000020
21using namespace llvm;
Eugene Zelenkof31871c2017-02-07 23:02:00 +000022
Weiming Zhaode04c482014-06-18 18:17:25 +000023//
24// ConstantPool implementation
25//
26// Emit the contents of the constant pool using the provided streamer.
27void ConstantPool::emitEntries(MCStreamer &Streamer) {
28 if (Entries.empty())
29 return;
Weiming Zhaode04c482014-06-18 18:17:25 +000030 Streamer.EmitDataRegion(MCDR_DataRegion);
Benjamin Kramerdd1d7a42016-06-26 14:49:00 +000031 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);
Weiming Zhaode04c482014-06-18 18:17:25 +000035 }
36 Streamer.EmitDataRegion(MCDR_DataRegionEnd);
37 Entries.clear();
38}
39
David Peixotto12f33da2014-07-18 16:05:14 +000040const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context,
Oliver Stannard20bb0422015-11-16 16:25:47 +000041 unsigned Size, SMLoc Loc) {
James Molloy4e5148c2017-05-22 09:42:01 +000042 const MCConstantExpr *C = dyn_cast<MCConstantExpr>(Value);
43
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())
47 return Itr->second;
48
Jim Grosbach19696da2015-05-18 18:43:14 +000049 MCSymbol *CPEntryLabel = Context.createTempSymbol();
Weiming Zhaode04c482014-06-18 18:17:25 +000050
Oliver Stannard20bb0422015-11-16 16:25:47 +000051 Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
James Molloy4e5148c2017-05-22 09:42:01 +000052 const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
53 if (C)
54 CachedEntries[C->getValue()] = SymRef;
55 return SymRef;
Weiming Zhaode04c482014-06-18 18:17:25 +000056}
57
58bool ConstantPool::empty() { return Entries.empty(); }
59
James Molloy11824fc2017-05-22 09:42:07 +000060void ConstantPool::clearCache() {
61 CachedEntries.clear();
62}
63
Weiming Zhaode04c482014-06-18 18:17:25 +000064//
65// AssemblerConstantPools implementation
66//
Rafael Espindola75219642015-05-21 19:20:38 +000067ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
Weiming Zhaode04c482014-06-18 18:17:25 +000068 ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
69 if (CP == ConstantPools.end())
70 return nullptr;
71
72 return &CP->second;
73}
74
75ConstantPool &
Rafael Espindola75219642015-05-21 19:20:38 +000076AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
Weiming Zhaode04c482014-06-18 18:17:25 +000077 return ConstantPools[Section];
78}
79
Rafael Espindola75219642015-05-21 19:20:38 +000080static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
Weiming Zhaode04c482014-06-18 18:17:25 +000081 ConstantPool &CP) {
82 if (!CP.empty()) {
83 Streamer.SwitchSection(Section);
84 CP.emitEntries(Streamer);
85 }
86}
87
88void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {
89 // Dump contents of assembler constant pools.
Benjamin Kramerdd1d7a42016-06-26 14:49:00 +000090 for (auto &CPI : ConstantPools) {
91 MCSection *Section = CPI.first;
92 ConstantPool &CP = CPI.second;
Weiming Zhaode04c482014-06-18 18:17:25 +000093
94 emitConstantPool(Streamer, Section, CP);
95 }
96}
97
98void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
Eric Christopherea40df32016-10-14 05:47:37 +000099 MCSection *Section = Streamer.getCurrentSectionOnly();
Kristina Brooksde01dd12018-09-03 03:48:39 +0000100 if (ConstantPool *CP = getConstantPool(Section))
Weiming Zhaode04c482014-06-18 18:17:25 +0000101 emitConstantPool(Streamer, Section, *CP);
Weiming Zhaode04c482014-06-18 18:17:25 +0000102}
103
James Molloy11824fc2017-05-22 09:42:07 +0000104void AssemblerConstantPools::clearCacheForCurrentSection(MCStreamer &Streamer) {
105 MCSection *Section = Streamer.getCurrentSectionOnly();
Kristina Brooksde01dd12018-09-03 03:48:39 +0000106 if (ConstantPool *CP = getConstantPool(Section))
James Molloy11824fc2017-05-22 09:42:07 +0000107 CP->clearCache();
James Molloy11824fc2017-05-22 09:42:07 +0000108}
109
Weiming Zhaode04c482014-06-18 18:17:25 +0000110const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
David Peixotto12f33da2014-07-18 16:05:14 +0000111 const MCExpr *Expr,
Oliver Stannard20bb0422015-11-16 16:25:47 +0000112 unsigned Size, SMLoc Loc) {
Eric Christopherea40df32016-10-14 05:47:37 +0000113 MCSection *Section = Streamer.getCurrentSectionOnly();
David Peixotto12f33da2014-07-18 16:05:14 +0000114 return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
Oliver Stannard20bb0422015-11-16 16:25:47 +0000115 Size, Loc);
Weiming Zhaode04c482014-06-18 18:17:25 +0000116}