Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit de04c48

Browse files
author
Weiming Zhao
committed
[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
1 parent 7ec69f6 commit de04c48

File tree

4 files changed

+177
-136
lines changed

4 files changed

+177
-136
lines changed

include/llvm/MC/ConstantPools.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//===- ConstantPool.h - Keep track of assembler-generated ------*- 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 declares the ConstantPool and AssemblerConstantPools classes.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
15+
#ifndef LLVM_MC_CONSTANTPOOL_H
16+
#define LLVM_MC_CONSTANTPOOL_H
17+
18+
#include "llvm/ADT/SmallVector.h"
19+
namespace llvm {
20+
class MCContext;
21+
class MCExpr;
22+
class MCSection;
23+
class MCStreamer;
24+
class MCSymbol;
25+
// A class to keep track of assembler-generated constant pools that are use to
26+
// implement the ldr-pseudo.
27+
class ConstantPool {
28+
typedef SmallVector<std::pair<MCSymbol *, const MCExpr *>, 4> EntryVecTy;
29+
EntryVecTy Entries;
30+
31+
public:
32+
// Initialize a new empty constant pool
33+
ConstantPool() {}
34+
35+
// Add a new entry to the constant pool in the next slot.
36+
// \param Value is the new entry to put in the constant pool.
37+
//
38+
// \returns a MCExpr that references the newly inserted value
39+
const MCExpr *addEntry(const MCExpr *Value, MCContext &Context);
40+
41+
// Emit the contents of the constant pool using the provided streamer.
42+
void emitEntries(MCStreamer &Streamer);
43+
44+
// Return true if the constant pool is empty
45+
bool empty();
46+
};
47+
48+
class AssemblerConstantPools {
49+
// Map type used to keep track of per-Section constant pools used by the
50+
// ldr-pseudo opcode. The map associates a section to its constant pool. The
51+
// constant pool is a vector of (label, value) pairs. When the ldr
52+
// pseudo is parsed we insert a new (label, value) pair into the constant pool
53+
// for the current section and add MCSymbolRefExpr to the new label as
54+
// an opcode to the ldr. After we have parsed all the user input we
55+
// output the (label, value) pairs in each constant pool at the end of the
56+
// section.
57+
//
58+
// We use the MapVector for the map type to ensure stable iteration of
59+
// the sections at the end of the parse. We need to iterate over the
60+
// sections in a stable order to ensure that we have print the
61+
// constant pools in a deterministic order when printing an assembly
62+
// file.
63+
typedef MapVector<const MCSection *, ConstantPool> ConstantPoolMapTy;
64+
ConstantPoolMapTy ConstantPools;
65+
66+
public:
67+
AssemblerConstantPools() {}
68+
~AssemblerConstantPools() {}
69+
70+
void emitAll(MCStreamer &Streamer);
71+
void emitForCurrentSection(MCStreamer &Streamer);
72+
const MCExpr *addEntry(MCStreamer &Streamer, const MCExpr *Expr);
73+
74+
private:
75+
ConstantPool *getConstantPool(const MCSection *Section);
76+
ConstantPool &getOrCreateConstantPool(const MCSection *Section);
77+
};
78+
} // end namespace llvm
79+
80+
#endif

lib/MC/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
add_llvm_library(LLVMMC
2+
ConstantPools.cpp
23
ELFObjectWriter.cpp
34
MCAsmBackend.cpp
45
MCAsmInfo.cpp

lib/MC/ConstantPools.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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/MCContext.h"
15+
#include "llvm/MC/MCExpr.h"
16+
#include "llvm/MC/MCStreamer.h"
17+
#include "llvm/MC/ConstantPools.h"
18+
19+
using namespace llvm;
20+
//
21+
// ConstantPool implementation
22+
//
23+
// Emit the contents of the constant pool using the provided streamer.
24+
void ConstantPool::emitEntries(MCStreamer &Streamer) {
25+
if (Entries.empty())
26+
return;
27+
Streamer.EmitCodeAlignment(4); // align to 4-byte address
28+
Streamer.EmitDataRegion(MCDR_DataRegion);
29+
for (EntryVecTy::const_iterator I = Entries.begin(), E = Entries.end();
30+
I != E; ++I) {
31+
Streamer.EmitLabel(I->first);
32+
Streamer.EmitValue(I->second, 4);
33+
}
34+
Streamer.EmitDataRegion(MCDR_DataRegionEnd);
35+
Entries.clear();
36+
}
37+
38+
const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context) {
39+
MCSymbol *CPEntryLabel = Context.CreateTempSymbol();
40+
41+
Entries.push_back(std::make_pair(CPEntryLabel, Value));
42+
return MCSymbolRefExpr::Create(CPEntryLabel, Context);
43+
}
44+
45+
bool ConstantPool::empty() { return Entries.empty(); }
46+
47+
//
48+
// AssemblerConstantPools implementation
49+
//
50+
ConstantPool *
51+
AssemblerConstantPools::getConstantPool(const MCSection *Section) {
52+
ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
53+
if (CP == ConstantPools.end())
54+
return nullptr;
55+
56+
return &CP->second;
57+
}
58+
59+
ConstantPool &
60+
AssemblerConstantPools::getOrCreateConstantPool(const MCSection *Section) {
61+
return ConstantPools[Section];
62+
}
63+
64+
static void emitConstantPool(MCStreamer &Streamer, const MCSection *Section,
65+
ConstantPool &CP) {
66+
if (!CP.empty()) {
67+
Streamer.SwitchSection(Section);
68+
CP.emitEntries(Streamer);
69+
}
70+
}
71+
72+
void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {
73+
// Dump contents of assembler constant pools.
74+
for (ConstantPoolMapTy::iterator CPI = ConstantPools.begin(),
75+
CPE = ConstantPools.end();
76+
CPI != CPE; ++CPI) {
77+
const MCSection *Section = CPI->first;
78+
ConstantPool &CP = CPI->second;
79+
80+
emitConstantPool(Streamer, Section, CP);
81+
}
82+
}
83+
84+
void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
85+
const MCSection *Section = Streamer.getCurrentSection().first;
86+
if (ConstantPool *CP = getConstantPool(Section)) {
87+
emitConstantPool(Streamer, Section, *CP);
88+
}
89+
}
90+
91+
const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
92+
const MCExpr *Expr) {
93+
const MCSection *Section = Streamer.getCurrentSection().first;
94+
return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext());
95+
}

lib/Target/ARM/MCTargetDesc/ARMTargetStreamer.cpp

Lines changed: 1 addition & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -11,147 +11,12 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313
#include "llvm/ADT/MapVector.h"
14+
#include "llvm/MC/ConstantPools.h"
1415
#include "llvm/MC/MCContext.h"
1516
#include "llvm/MC/MCExpr.h"
1617
#include "llvm/MC/MCStreamer.h"
1718

1819
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-
15520
//
15621
// ARMTargetStreamer Implemenation
15722
//

0 commit comments

Comments
 (0)