LLVM  4.0.0
ConstantPools.cpp
Go to the documentation of this file.
1 //===- 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"
14 #include "llvm/MC/ConstantPools.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCStreamer.h"
18 
19 using namespace llvm;
20 //
21 // ConstantPool implementation
22 //
23 // Emit the contents of the constant pool using the provided streamer.
25  if (Entries.empty())
26  return;
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);
32  }
34  Entries.clear();
35 }
36 
38  unsigned Size, SMLoc Loc) {
39  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 
46  MCSymbol *CPEntryLabel = Context.createTempSymbol();
47 
48  Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
49  const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
50  if (C)
51  CachedEntries[C->getValue()] = SymRef;
52  return SymRef;
53 }
54 
55 bool ConstantPool::empty() { return Entries.empty(); }
56 
57 //
58 // AssemblerConstantPools implementation
59 //
60 ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
61  ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
62  if (CP == ConstantPools.end())
63  return nullptr;
64 
65  return &CP->second;
66 }
67 
69 AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
70  return ConstantPools[Section];
71 }
72 
73 static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
74  ConstantPool &CP) {
75  if (!CP.empty()) {
76  Streamer.SwitchSection(Section);
77  CP.emitEntries(Streamer);
78  }
79 }
80 
82  // Dump contents of assembler constant pools.
83  for (auto &CPI : ConstantPools) {
84  MCSection *Section = CPI.first;
85  ConstantPool &CP = CPI.second;
86 
87  emitConstantPool(Streamer, Section, CP);
88  }
89 }
90 
92  MCSection *Section = Streamer.getCurrentSectionOnly();
93  if (ConstantPool *CP = getConstantPool(Section)) {
94  emitConstantPool(Streamer, Section, *CP);
95  }
96 }
97 
99  const MCExpr *Expr,
100  unsigned Size, SMLoc Loc) {
101  MCSection *Section = Streamer.getCurrentSectionOnly();
102  return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
103  Size, Loc);
104 }
std::vector< std::pair< MCSection *, ConstantPool > >::iterator iterator
Definition: MapVector.h:40
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:40
void push_back(const T &Elt)
Definition: SmallVector.h:211
LLVMContext & Context
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:298
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:302
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
MCContext & getContext() const
Definition: MCStreamer.h:221
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
Context object for machine code objects.
Definition: MCContext.h:51
const MCExpr * addEntry(const MCExpr *Value, MCContext &Context, unsigned Size, SMLoc Loc)
iterator find(const KeyT &Key)
Definition: MapVector.h:131
void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:116
Streaming machine code generation interface.
Definition: MCStreamer.h:161
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:218
int64_t getValue() const
Definition: MCExpr.h:147
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:829
void emitForCurrentSection(MCStreamer &Streamer)
const MCExpr * addEntry(MCStreamer &Streamer, const MCExpr *Expr, unsigned Size, SMLoc Loc)
virtual void EmitDataRegion(MCDataRegionType Kind)
Note in the output the specified region Kind.
Definition: MCStreamer.h:408
void emitAll(MCStreamer &Streamer)
static void emitConstantPool(MCStreamer &Streamer, MCSection *Section, ConstantPool &CP)
virtual void EmitLabel(MCSymbol *Symbol)
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:293
virtual void EmitCodeAlignment(unsigned ByteAlignment, unsigned MaxBytesToEmit=0)
Emit nops until the byte alignment ByteAlignment is reached.
Definition: MCStreamer.cpp:820
void emitEntries(MCStreamer &Streamer)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:287
iterator end()
Definition: MapVector.h:55
LLVM Value Representation.
Definition: Value.h:71
Represents a location in source code.
Definition: SMLoc.h:24
.end_data_region
Definition: MCDirectives.h:61