llvm.org GIT mirror llvm / de04c48
[ARM] [MC] Refactor the constant pool classes ARMTargetStreamer implements ConstantPool and AssmeblerConstantPools to keep track of assembler-generated constant pools that are used for ldr-pseudo. When implementing ldr-pseudo for AArch64, these two classes can be reused. So this patch factors them out from ARM target to the general MC lib. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211198 91177308-0d34-0410-b5e6-96231b3b80d8 Weiming Zhao 6 years ago
4 changed file(s) with 177 addition(s) and 136 deletion(s). Raw diff Collapse all Expand all
0 //===- ConstantPool.h - Keep track of assembler-generated ------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the ConstantPool and AssemblerConstantPools classes.
10 //
11 //===----------------------------------------------------------------------===//
12
13
14 #ifndef LLVM_MC_CONSTANTPOOL_H
15 #define LLVM_MC_CONSTANTPOOL_H
16
17 #include "llvm/ADT/SmallVector.h"
18 namespace llvm {
19 class MCContext;
20 class MCExpr;
21 class MCSection;
22 class MCStreamer;
23 class MCSymbol;
24 // A class to keep track of assembler-generated constant pools that are use to
25 // implement the ldr-pseudo.
26 class ConstantPool {
27 typedef SmallVector, 4> EntryVecTy;
28 EntryVecTy Entries;
29
30 public:
31 // Initialize a new empty constant pool
32 ConstantPool() {}
33
34 // Add a new entry to the constant pool in the next slot.
35 // \param Value is the new entry to put in the constant pool.
36 //
37 // \returns a MCExpr that references the newly inserted value
38 const MCExpr *addEntry(const MCExpr *Value, MCContext &Context);
39
40 // Emit the contents of the constant pool using the provided streamer.
41 void emitEntries(MCStreamer &Streamer);
42
43 // Return true if the constant pool is empty
44 bool empty();
45 };
46
47 class AssemblerConstantPools {
48 // Map type used to keep track of per-Section constant pools used by the
49 // ldr-pseudo opcode. The map associates a section to its constant pool. The
50 // constant pool is a vector of (label, value) pairs. When the ldr
51 // pseudo is parsed we insert a new (label, value) pair into the constant pool
52 // for the current section and add MCSymbolRefExpr to the new label as
53 // an opcode to the ldr. After we have parsed all the user input we
54 // output the (label, value) pairs in each constant pool at the end of the
55 // section.
56 //
57 // We use the MapVector for the map type to ensure stable iteration of
58 // the sections at the end of the parse. We need to iterate over the
59 // sections in a stable order to ensure that we have print the
60 // constant pools in a deterministic order when printing an assembly
61 // file.
62 typedef MapVector ConstantPoolMapTy;
63 ConstantPoolMapTy ConstantPools;
64
65 public:
66 AssemblerConstantPools() {}
67 ~AssemblerConstantPools() {}
68
69 void emitAll(MCStreamer &Streamer);
70 void emitForCurrentSection(MCStreamer &Streamer);
71 const MCExpr *addEntry(MCStreamer &Streamer, const MCExpr *Expr);
72
73 private:
74 ConstantPool *getConstantPool(const MCSection *Section);
75 ConstantPool &getOrCreateConstantPool(const MCSection *Section);
76 };
77 } // end namespace llvm
78
79 #endif
0 add_llvm_library(LLVMMC
1 ConstantPools.cpp
12 ELFObjectWriter.cpp
23 MCAsmBackend.cpp
34 MCAsmInfo.cpp
0 //===- ConstantPools.cpp - ConstantPool class --*- C++ -*---------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the ConstantPool and AssemblerConstantPools classes.
10 //
11 //===----------------------------------------------------------------------===//
12 #include "llvm/ADT/MapVector.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/MC/ConstantPools.h"
17
18 using namespace llvm;
19 //
20 // ConstantPool implementation
21 //
22 // Emit the contents of the constant pool using the provided streamer.
23 void ConstantPool::emitEntries(MCStreamer &Streamer) {
24 if (Entries.empty())
25 return;
26 Streamer.EmitCodeAlignment(4); // align to 4-byte address
27 Streamer.EmitDataRegion(MCDR_DataRegion);
28 for (EntryVecTy::const_iterator I = Entries.begin(), E = Entries.end();
29 I != E; ++I) {
30 Streamer.EmitLabel(I->first);
31 Streamer.EmitValue(I->second, 4);
32 }
33 Streamer.EmitDataRegion(MCDR_DataRegionEnd);
34 Entries.clear();
35 }
36
37 const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context) {
38 MCSymbol *CPEntryLabel = Context.CreateTempSymbol();
39
40 Entries.push_back(std::make_pair(CPEntryLabel, Value));
41 return MCSymbolRefExpr::Create(CPEntryLabel, Context);
42 }
43
44 bool ConstantPool::empty() { return Entries.empty(); }
45
46 //
47 // AssemblerConstantPools implementation
48 //
49 ConstantPool *
50 AssemblerConstantPools::getConstantPool(const MCSection *Section) {
51 ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
52 if (CP == ConstantPools.end())
53 return nullptr;
54
55 return &CP->second;
56 }
57
58 ConstantPool &
59 AssemblerConstantPools::getOrCreateConstantPool(const MCSection *Section) {
60 return ConstantPools[Section];
61 }
62
63 static void emitConstantPool(MCStreamer &Streamer, const MCSection *Section,
64 ConstantPool &CP) {
65 if (!CP.empty()) {
66 Streamer.SwitchSection(Section);
67 CP.emitEntries(Streamer);
68 }
69 }
70
71 void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {
72 // Dump contents of assembler constant pools.
73 for (ConstantPoolMapTy::iterator CPI = ConstantPools.begin(),
74 CPE = ConstantPools.end();
75 CPI != CPE; ++CPI) {
76 const MCSection *Section = CPI->first;
77 ConstantPool &CP = CPI->second;
78
79 emitConstantPool(Streamer, Section, CP);
80 }
81 }
82
83 void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
84 const MCSection *Section = Streamer.getCurrentSection().first;
85 if (ConstantPool *CP = getConstantPool(Section)) {
86 emitConstantPool(Streamer, Section, *CP);
87 }
88 }
89
90 const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
91 const MCExpr *Expr) {
92 const MCSection *Section = Streamer.getCurrentSection().first;
93 return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext());
94 }
1010 //
1111 //===----------------------------------------------------------------------===//
1212 #include "llvm/ADT/MapVector.h"
13 #include "llvm/MC/ConstantPools.h"
1314 #include "llvm/MC/MCContext.h"
1415 #include "llvm/MC/MCExpr.h"
1516 #include "llvm/MC/MCStreamer.h"
1617
1718 using namespace llvm;
18
19 namespace {
20 // A class to keep track of assembler-generated constant pools that are use to
21 // implement the ldr-pseudo.
22 class ConstantPool {
23 typedef SmallVector, 4> EntryVecTy;
24 EntryVecTy Entries;
25
26 public:
27 // Initialize a new empty constant pool
28 ConstantPool() {}
29
30 // Add a new entry to the constant pool in the next slot.
31 // \param Value is the new entry to put in the constant pool.
32 //
33 // \returns a MCExpr that references the newly inserted value
34 const MCExpr *addEntry(const MCExpr *Value, MCContext &Context);
35
36 // Emit the contents of the constant pool using the provided streamer.
37 void emitEntries(MCStreamer &Streamer);
38
39 // Return true if the constant pool is empty
40 bool empty();
41 };
42 }
43
44 namespace llvm {
45 class AssemblerConstantPools {
46 // Map type used to keep track of per-Section constant pools used by the
47 // ldr-pseudo opcode. The map associates a section to its constant pool. The
48 // constant pool is a vector of (label, value) pairs. When the ldr
49 // pseudo is parsed we insert a new (label, value) pair into the constant pool
50 // for the current section and add MCSymbolRefExpr to the new label as
51 // an opcode to the ldr. After we have parsed all the user input we
52 // output the (label, value) pairs in each constant pool at the end of the
53 // section.
54 //
55 // We use the MapVector for the map type to ensure stable iteration of
56 // the sections at the end of the parse. We need to iterate over the
57 // sections in a stable order to ensure that we have print the
58 // constant pools in a deterministic order when printing an assembly
59 // file.
60 typedef MapVector ConstantPoolMapTy;
61 ConstantPoolMapTy ConstantPools;
62
63 public:
64 AssemblerConstantPools() {}
65 ~AssemblerConstantPools() {}
66
67 void emitAll(MCStreamer &Streamer);
68 void emitForCurrentSection(MCStreamer &Streamer);
69 const MCExpr *addEntry(MCStreamer &Streamer, const MCExpr *Expr);
70
71 private:
72 ConstantPool *getConstantPool(const MCSection *Section);
73 ConstantPool &getOrCreateConstantPool(const MCSection *Section);
74 };
75 }
76
77 //
78 // ConstantPool implementation
79 //
80 // Emit the contents of the constant pool using the provided streamer.
81 void ConstantPool::emitEntries(MCStreamer &Streamer) {
82 if (Entries.empty())
83 return;
84 Streamer.EmitCodeAlignment(4); // align to 4-byte address
85 Streamer.EmitDataRegion(MCDR_DataRegion);
86 for (EntryVecTy::const_iterator I = Entries.begin(), E = Entries.end();
87 I != E; ++I) {
88 Streamer.EmitLabel(I->first);
89 Streamer.EmitValue(I->second, 4);
90 }
91 Streamer.EmitDataRegion(MCDR_DataRegionEnd);
92 Entries.clear();
93 }
94
95 const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context) {
96 MCSymbol *CPEntryLabel = Context.CreateTempSymbol();
97
98 Entries.push_back(std::make_pair(CPEntryLabel, Value));
99 return MCSymbolRefExpr::Create(CPEntryLabel, Context);
100 }
101
102 bool ConstantPool::empty() { return Entries.empty(); }
103
104 //
105 // AssemblerConstantPools implementation
106 //
107 ConstantPool *
108 AssemblerConstantPools::getConstantPool(const MCSection *Section) {
109 ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
110 if (CP == ConstantPools.end())
111 return nullptr;
112
113 return &CP->second;
114 }
115
116 ConstantPool &
117 AssemblerConstantPools::getOrCreateConstantPool(const MCSection *Section) {
118 return ConstantPools[Section];
119 }
120
121 static void emitConstantPool(MCStreamer &Streamer, const MCSection *Section,
122 ConstantPool &CP) {
123 if (!CP.empty()) {
124 Streamer.SwitchSection(Section);
125 CP.emitEntries(Streamer);
126 }
127 }
128
129 void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {
130 // Dump contents of assembler constant pools.
131 for (ConstantPoolMapTy::iterator CPI = ConstantPools.begin(),
132 CPE = ConstantPools.end();
133 CPI != CPE; ++CPI) {
134 const MCSection *Section = CPI->first;
135 ConstantPool &CP = CPI->second;
136
137 emitConstantPool(Streamer, Section, CP);
138 }
139 }
140
141 void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
142 const MCSection *Section = Streamer.getCurrentSection().first;
143 if (ConstantPool *CP = getConstantPool(Section)) {
144 emitConstantPool(Streamer, Section, *CP);
145 }
146 }
147
148 const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
149 const MCExpr *Expr) {
150 const MCSection *Section = Streamer.getCurrentSection().first;
151 return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext());
152 }
153
15419 //
15520 // ARMTargetStreamer Implemenation
15621 //