LLVM  3.7.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 (EntryVecTy::const_iterator I = Entries.begin(), E = Entries.end();
29  I != E; ++I) {
30  Streamer.EmitCodeAlignment(I->Size); // align naturally
31  Streamer.EmitLabel(I->Label);
32  Streamer.EmitValue(I->Value, I->Size);
33  }
35  Entries.clear();
36 }
37 
39  unsigned Size) {
40  MCSymbol *CPEntryLabel = Context.createTempSymbol();
41 
42  Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size));
43  return MCSymbolRefExpr::create(CPEntryLabel, Context);
44 }
45 
46 bool ConstantPool::empty() { return Entries.empty(); }
47 
48 //
49 // AssemblerConstantPools implementation
50 //
51 ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
52  ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
53  if (CP == ConstantPools.end())
54  return nullptr;
55 
56  return &CP->second;
57 }
58 
60 AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
61  return ConstantPools[Section];
62 }
63 
64 static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
65  ConstantPool &CP) {
66  if (!CP.empty()) {
67  Streamer.SwitchSection(Section);
68  CP.emitEntries(Streamer);
69  }
70 }
71 
73  // Dump contents of assembler constant pools.
74  for (ConstantPoolMapTy::iterator CPI = ConstantPools.begin(),
75  CPE = ConstantPools.end();
76  CPI != CPE; ++CPI) {
77  MCSection *Section = CPI->first;
78  ConstantPool &CP = CPI->second;
79 
80  emitConstantPool(Streamer, Section, CP);
81  }
82 }
83 
85  MCSection *Section = Streamer.getCurrentSection().first;
86  if (ConstantPool *CP = getConstantPool(Section)) {
87  emitConstantPool(Streamer, Section, *CP);
88  }
89 }
90 
92  const MCExpr *Expr,
93  unsigned Size) {
94  MCSection *Section = Streamer.getCurrentSection().first;
95  return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
96  Size);
97 }
std::vector< std::pair< MCSection *, ConstantPool > >::iterator iterator
Definition: MapVector.h:39
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:48
void push_back(const T &Elt)
Definition: SmallVector.h:222
const MCExpr * addEntry(MCStreamer &Streamer, const MCExpr *Expr, unsigned Size)
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:315
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
MCSectionSubPair getCurrentSection() const
Return the current section that the streamer is emitting code to.
Definition: MCStreamer.h:274
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:33
MCContext & getContext() const
Definition: MCStreamer.h:210
Context object for machine code objects.
Definition: MCContext.h:48
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:57
const MCExpr * addEntry(const MCExpr *Value, MCContext &Context, unsigned Size)
iterator find(const KeyT &Key)
Definition: MapVector.h:108
Streaming machine code generation interface.
Definition: MCStreamer.h:157
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:222
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:701
void emitForCurrentSection(MCStreamer &Streamer)
virtual void EmitDataRegion(MCDataRegionType Kind)
Note in the output the specified region Kind.
Definition: MCStreamer.h:385
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:203
virtual void EmitCodeAlignment(unsigned ByteAlignment, unsigned MaxBytesToEmit=0)
Emit nops until the byte alignment ByteAlignment is reached.
Definition: MCStreamer.cpp:691
void emitEntries(MCStreamer &Streamer)
#define I(x, y, z)
Definition: MD5.cpp:54
void EmitValue(const MCExpr *Value, unsigned Size, const SMLoc &Loc=SMLoc())
Definition: MCStreamer.cpp:110
iterator begin()
Definition: MapVector.h:46
iterator end()
Definition: MapVector.h:48
LLVM Value Representation.
Definition: Value.h:69
.end_data_region
Definition: MCDirectives.h:60