|
11 | 11 | //
|
12 | 12 | //===----------------------------------------------------------------------===//
|
13 | 13 | #include "llvm/ADT/MapVector.h"
|
| 14 | +#include "llvm/MC/ConstantPools.h" |
14 | 15 | #include "llvm/MC/MCContext.h"
|
15 | 16 | #include "llvm/MC/MCExpr.h"
|
16 | 17 | #include "llvm/MC/MCStreamer.h"
|
17 | 18 |
|
18 | 19 | using namespace llvm;
|
19 |
| - |
20 |
| -namespace { |
21 |
| -// A class to keep track of assembler-generated constant pools that are use to |
22 |
| -// implement the ldr-pseudo. |
23 |
| -class ConstantPool { |
24 |
| - typedef SmallVector<std::pair<MCSymbol *, const MCExpr *>, 4> EntryVecTy; |
25 |
| - EntryVecTy Entries; |
26 |
| - |
27 |
| -public: |
28 |
| - // Initialize a new empty constant pool |
29 |
| - ConstantPool() {} |
30 |
| - |
31 |
| - // Add a new entry to the constant pool in the next slot. |
32 |
| - // \param Value is the new entry to put in the constant pool. |
33 |
| - // |
34 |
| - // \returns a MCExpr that references the newly inserted value |
35 |
| - const MCExpr *addEntry(const MCExpr *Value, MCContext &Context); |
36 |
| - |
37 |
| - // Emit the contents of the constant pool using the provided streamer. |
38 |
| - void emitEntries(MCStreamer &Streamer); |
39 |
| - |
40 |
| - // Return true if the constant pool is empty |
41 |
| - bool empty(); |
42 |
| -}; |
43 |
| -} |
44 |
| - |
45 |
| -namespace llvm { |
46 |
| -class AssemblerConstantPools { |
47 |
| - // Map type used to keep track of per-Section constant pools used by the |
48 |
| - // ldr-pseudo opcode. The map associates a section to its constant pool. The |
49 |
| - // constant pool is a vector of (label, value) pairs. When the ldr |
50 |
| - // pseudo is parsed we insert a new (label, value) pair into the constant pool |
51 |
| - // for the current section and add MCSymbolRefExpr to the new label as |
52 |
| - // an opcode to the ldr. After we have parsed all the user input we |
53 |
| - // output the (label, value) pairs in each constant pool at the end of the |
54 |
| - // section. |
55 |
| - // |
56 |
| - // We use the MapVector for the map type to ensure stable iteration of |
57 |
| - // the sections at the end of the parse. We need to iterate over the |
58 |
| - // sections in a stable order to ensure that we have print the |
59 |
| - // constant pools in a deterministic order when printing an assembly |
60 |
| - // file. |
61 |
| - typedef MapVector<const MCSection *, ConstantPool> ConstantPoolMapTy; |
62 |
| - ConstantPoolMapTy ConstantPools; |
63 |
| - |
64 |
| -public: |
65 |
| - AssemblerConstantPools() {} |
66 |
| - ~AssemblerConstantPools() {} |
67 |
| - |
68 |
| - void emitAll(MCStreamer &Streamer); |
69 |
| - void emitForCurrentSection(MCStreamer &Streamer); |
70 |
| - const MCExpr *addEntry(MCStreamer &Streamer, const MCExpr *Expr); |
71 |
| - |
72 |
| -private: |
73 |
| - ConstantPool *getConstantPool(const MCSection *Section); |
74 |
| - ConstantPool &getOrCreateConstantPool(const MCSection *Section); |
75 |
| -}; |
76 |
| -} |
77 |
| - |
78 |
| -// |
79 |
| -// ConstantPool implementation |
80 |
| -// |
81 |
| -// Emit the contents of the constant pool using the provided streamer. |
82 |
| -void ConstantPool::emitEntries(MCStreamer &Streamer) { |
83 |
| - if (Entries.empty()) |
84 |
| - return; |
85 |
| - Streamer.EmitCodeAlignment(4); // align to 4-byte address |
86 |
| - Streamer.EmitDataRegion(MCDR_DataRegion); |
87 |
| - for (EntryVecTy::const_iterator I = Entries.begin(), E = Entries.end(); |
88 |
| - I != E; ++I) { |
89 |
| - Streamer.EmitLabel(I->first); |
90 |
| - Streamer.EmitValue(I->second, 4); |
91 |
| - } |
92 |
| - Streamer.EmitDataRegion(MCDR_DataRegionEnd); |
93 |
| - Entries.clear(); |
94 |
| -} |
95 |
| - |
96 |
| -const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context) { |
97 |
| - MCSymbol *CPEntryLabel = Context.CreateTempSymbol(); |
98 |
| - |
99 |
| - Entries.push_back(std::make_pair(CPEntryLabel, Value)); |
100 |
| - return MCSymbolRefExpr::Create(CPEntryLabel, Context); |
101 |
| -} |
102 |
| - |
103 |
| -bool ConstantPool::empty() { return Entries.empty(); } |
104 |
| - |
105 |
| -// |
106 |
| -// AssemblerConstantPools implementation |
107 |
| -// |
108 |
| -ConstantPool * |
109 |
| -AssemblerConstantPools::getConstantPool(const MCSection *Section) { |
110 |
| - ConstantPoolMapTy::iterator CP = ConstantPools.find(Section); |
111 |
| - if (CP == ConstantPools.end()) |
112 |
| - return nullptr; |
113 |
| - |
114 |
| - return &CP->second; |
115 |
| -} |
116 |
| - |
117 |
| -ConstantPool & |
118 |
| -AssemblerConstantPools::getOrCreateConstantPool(const MCSection *Section) { |
119 |
| - return ConstantPools[Section]; |
120 |
| -} |
121 |
| - |
122 |
| -static void emitConstantPool(MCStreamer &Streamer, const MCSection *Section, |
123 |
| - ConstantPool &CP) { |
124 |
| - if (!CP.empty()) { |
125 |
| - Streamer.SwitchSection(Section); |
126 |
| - CP.emitEntries(Streamer); |
127 |
| - } |
128 |
| -} |
129 |
| - |
130 |
| -void AssemblerConstantPools::emitAll(MCStreamer &Streamer) { |
131 |
| - // Dump contents of assembler constant pools. |
132 |
| - for (ConstantPoolMapTy::iterator CPI = ConstantPools.begin(), |
133 |
| - CPE = ConstantPools.end(); |
134 |
| - CPI != CPE; ++CPI) { |
135 |
| - const MCSection *Section = CPI->first; |
136 |
| - ConstantPool &CP = CPI->second; |
137 |
| - |
138 |
| - emitConstantPool(Streamer, Section, CP); |
139 |
| - } |
140 |
| -} |
141 |
| - |
142 |
| -void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) { |
143 |
| - const MCSection *Section = Streamer.getCurrentSection().first; |
144 |
| - if (ConstantPool *CP = getConstantPool(Section)) { |
145 |
| - emitConstantPool(Streamer, Section, *CP); |
146 |
| - } |
147 |
| -} |
148 |
| - |
149 |
| -const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer, |
150 |
| - const MCExpr *Expr) { |
151 |
| - const MCSection *Section = Streamer.getCurrentSection().first; |
152 |
| - return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext()); |
153 |
| -} |
154 |
| - |
155 | 20 | //
|
156 | 21 | // ARMTargetStreamer Implemenation
|
157 | 22 | //
|
|
0 commit comments