blob: 8c94e2780998f1851048b1f9bf620669e6a802de [file] [log] [blame]
Eugene Zelenko1d435522017-02-07 23:02:00 +00001//===- ConstantPools.cpp - ConstantPool class -----------------------------===//
Weiming Zhao8c899732014-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 Zelenko1d435522017-02-07 23:02:00 +000013
Chandler Carruthd9903882015-01-14 11:23:27 +000014#include "llvm/MC/ConstantPools.h"
Weiming Zhao8c899732014-06-18 18:17:25 +000015#include "llvm/MC/MCContext.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000016#include "llvm/MC/MCDirectives.h"
Weiming Zhao8c899732014-06-18 18:17:25 +000017#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCStreamer.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000019#include "llvm/Support/Casting.h"
Weiming Zhao8c899732014-06-18 18:17:25 +000020
21using namespace llvm;
Eugene Zelenko1d435522017-02-07 23:02:00 +000022
Weiming Zhao8c899732014-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 Zhao8c899732014-06-18 18:17:25 +000030 Streamer.EmitDataRegion(MCDR_DataRegion);
Benjamin Kramer7b4658f2016-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 Zhao8c899732014-06-18 18:17:25 +000035 }
36 Streamer.EmitDataRegion(MCDR_DataRegionEnd);
37 Entries.clear();
38}
39
David Peixottoae5ba762014-07-18 16:05:14 +000040const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context,
Oliver Stannard9327a752015-11-16 16:25:47 +000041 unsigned Size, SMLoc Loc) {
Weiming Zhao61001182016-11-04 19:17:32 +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 Grosbach6f482002015-05-18 18:43:14 +000049 MCSymbol *CPEntryLabel = Context.createTempSymbol();
Weiming Zhao8c899732014-06-18 18:17:25 +000050
Oliver Stannard9327a752015-11-16 16:25:47 +000051 Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
Weiming Zhao61001182016-11-04 19:17:32 +000052 const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
53 if (C)
54 CachedEntries[C->getValue()] = SymRef;
55 return SymRef;
Weiming Zhao8c899732014-06-18 18:17:25 +000056}
57
58bool ConstantPool::empty() { return Entries.empty(); }
59
60//
61// AssemblerConstantPools implementation
62//
Rafael Espindola0709a7b2015-05-21 19:20:38 +000063ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
Weiming Zhao8c899732014-06-18 18:17:25 +000064 ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
65 if (CP == ConstantPools.end())
66 return nullptr;
67
68 return &CP->second;
69}
70
71ConstantPool &
Rafael Espindola0709a7b2015-05-21 19:20:38 +000072AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
Weiming Zhao8c899732014-06-18 18:17:25 +000073 return ConstantPools[Section];
74}
75
Rafael Espindola0709a7b2015-05-21 19:20:38 +000076static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
Weiming Zhao8c899732014-06-18 18:17:25 +000077 ConstantPool &CP) {
78 if (!CP.empty()) {
79 Streamer.SwitchSection(Section);
80 CP.emitEntries(Streamer);
81 }
82}
83
84void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {
85 // Dump contents of assembler constant pools.
Benjamin Kramer7b4658f2016-06-26 14:49:00 +000086 for (auto &CPI : ConstantPools) {
87 MCSection *Section = CPI.first;
88 ConstantPool &CP = CPI.second;
Weiming Zhao8c899732014-06-18 18:17:25 +000089
90 emitConstantPool(Streamer, Section, CP);
91 }
92}
93
94void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
Eric Christopher445c9522016-10-14 05:47:37 +000095 MCSection *Section = Streamer.getCurrentSectionOnly();
Weiming Zhao8c899732014-06-18 18:17:25 +000096 if (ConstantPool *CP = getConstantPool(Section)) {
97 emitConstantPool(Streamer, Section, *CP);
98 }
99}
100
101const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
David Peixottoae5ba762014-07-18 16:05:14 +0000102 const MCExpr *Expr,
Oliver Stannard9327a752015-11-16 16:25:47 +0000103 unsigned Size, SMLoc Loc) {
Eric Christopher445c9522016-10-14 05:47:37 +0000104 MCSection *Section = Streamer.getCurrentSectionOnly();
David Peixottoae5ba762014-07-18 16:05:14 +0000105 return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
Oliver Stannard9327a752015-11-16 16:25:47 +0000106 Size, Loc);
Weiming Zhao8c899732014-06-18 18:17:25 +0000107}