blob: 9608c2c656b7c0a8162fa0f1235c7044792473da [file] [log] [blame]
Weiming Zhao8c899732014-06-18 18:17:25 +00001//===- ConstantPools.cpp - ConstantPool class --*- C++ -*---------===//
2//
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//===----------------------------------------------------------------------===//
13#include "llvm/ADT/MapVector.h"
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"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCStreamer.h"
Weiming Zhao8c899732014-06-18 18:17:25 +000018
19using namespace llvm;
20//
21// ConstantPool implementation
22//
23// Emit the contents of the constant pool using the provided streamer.
24void ConstantPool::emitEntries(MCStreamer &Streamer) {
25 if (Entries.empty())
26 return;
Weiming Zhao8c899732014-06-18 18:17:25 +000027 Streamer.EmitDataRegion(MCDR_DataRegion);
Benjamin Kramer7b4658f2016-06-26 14:49:00 +000028 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);
Weiming Zhao8c899732014-06-18 18:17:25 +000032 }
33 Streamer.EmitDataRegion(MCDR_DataRegionEnd);
34 Entries.clear();
35}
36
David Peixottoae5ba762014-07-18 16:05:14 +000037const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context,
Oliver Stannard9327a752015-11-16 16:25:47 +000038 unsigned Size, SMLoc Loc) {
Weiming Zhao61001182016-11-04 19:17:32 +000039 const MCConstantExpr *C = dyn_cast<MCConstantExpr>(Value);
40
41 // Check if there is existing entry for the same constant. If so, reuse it.
42 auto Itr = C ? CachedEntries.find(C->getValue()) : CachedEntries.end();
43 if (Itr != CachedEntries.end())
44 return Itr->second;
45
Jim Grosbach6f482002015-05-18 18:43:14 +000046 MCSymbol *CPEntryLabel = Context.createTempSymbol();
Weiming Zhao8c899732014-06-18 18:17:25 +000047
Oliver Stannard9327a752015-11-16 16:25:47 +000048 Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
Weiming Zhao61001182016-11-04 19:17:32 +000049 const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
50 if (C)
51 CachedEntries[C->getValue()] = SymRef;
52 return SymRef;
Weiming Zhao8c899732014-06-18 18:17:25 +000053}
54
55bool ConstantPool::empty() { return Entries.empty(); }
56
57//
58// AssemblerConstantPools implementation
59//
Rafael Espindola0709a7b2015-05-21 19:20:38 +000060ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
Weiming Zhao8c899732014-06-18 18:17:25 +000061 ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
62 if (CP == ConstantPools.end())
63 return nullptr;
64
65 return &CP->second;
66}
67
68ConstantPool &
Rafael Espindola0709a7b2015-05-21 19:20:38 +000069AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
Weiming Zhao8c899732014-06-18 18:17:25 +000070 return ConstantPools[Section];
71}
72
Rafael Espindola0709a7b2015-05-21 19:20:38 +000073static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
Weiming Zhao8c899732014-06-18 18:17:25 +000074 ConstantPool &CP) {
75 if (!CP.empty()) {
76 Streamer.SwitchSection(Section);
77 CP.emitEntries(Streamer);
78 }
79}
80
81void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {
82 // Dump contents of assembler constant pools.
Benjamin Kramer7b4658f2016-06-26 14:49:00 +000083 for (auto &CPI : ConstantPools) {
84 MCSection *Section = CPI.first;
85 ConstantPool &CP = CPI.second;
Weiming Zhao8c899732014-06-18 18:17:25 +000086
87 emitConstantPool(Streamer, Section, CP);
88 }
89}
90
91void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
Eric Christopher445c9522016-10-14 05:47:37 +000092 MCSection *Section = Streamer.getCurrentSectionOnly();
Weiming Zhao8c899732014-06-18 18:17:25 +000093 if (ConstantPool *CP = getConstantPool(Section)) {
94 emitConstantPool(Streamer, Section, *CP);
95 }
96}
97
98const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
David Peixottoae5ba762014-07-18 16:05:14 +000099 const MCExpr *Expr,
Oliver Stannard9327a752015-11-16 16:25:47 +0000100 unsigned Size, SMLoc Loc) {
Eric Christopher445c9522016-10-14 05:47:37 +0000101 MCSection *Section = Streamer.getCurrentSectionOnly();
David Peixottoae5ba762014-07-18 16:05:14 +0000102 return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
Oliver Stannard9327a752015-11-16 16:25:47 +0000103 Size, Loc);
Weiming Zhao8c899732014-06-18 18:17:25 +0000104}