LLVM 23.0.0git
ConstantPools.h
Go to the documentation of this file.
1//===- ConstantPools.h - Keep track of assembler-generated ------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the ConstantPool and AssemblerConstantPools classes.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_MC_CONSTANTPOOLS_H
14#define LLVM_MC_CONSTANTPOOLS_H
15
16#include "llvm/ADT/MapVector.h"
18#include "llvm/Support/SMLoc.h"
19#include <cstdint>
20
21namespace llvm {
22
23class MCContext;
24class MCExpr;
25class MCSection;
26class MCStreamer;
27class MCSymbol;
28class MCSymbolRefExpr;
29
31 ConstantPoolEntry(MCSymbol *L, const MCExpr *Val, unsigned Sz, SMLoc Loc_)
32 : Label(L), Value(Val), Size(Sz), Loc(Loc_) {}
33
35 const MCExpr *Value;
36 unsigned Size;
38};
39
40// A class to keep track of assembler-generated constant pools that are use to
41// implement the ldr-pseudo.
43 using EntryVecTy = SmallVector<ConstantPoolEntry, 4>;
44 EntryVecTy Entries;
45
46 // Caches of entries that already exist, indexed by their contents
47 // and also the size of the constant.
49 CachedConstantEntries;
51 CachedSymbolEntries;
52
53public:
54 // Initialize a new empty constant pool
55 ConstantPool() = default;
56
57 // Add a new entry to the constant pool in the next slot.
58 // \param Value is the new entry to put in the constant pool.
59 // \param Size is the size in bytes of the entry
60 //
61 // \returns a MCExpr that references the newly inserted value
62 LLVM_ABI const MCExpr *addEntry(const MCExpr *Value, MCContext &Context,
63 unsigned Size, SMLoc Loc);
64
65 // Emit the contents of the constant pool using the provided streamer.
66 LLVM_ABI void emitEntries(MCStreamer &Streamer);
67
68 // Return true if the constant pool is empty
69 LLVM_ABI bool empty();
70
71 LLVM_ABI void clearCache();
72};
73
75 // Map type used to keep track of per-Section constant pools used by the
76 // ldr-pseudo opcode. The map associates a section to its constant pool. The
77 // constant pool is a vector of (label, value) pairs. When the ldr
78 // pseudo is parsed we insert a new (label, value) pair into the constant pool
79 // for the current section and add MCSymbolRefExpr to the new label as
80 // an opcode to the ldr. After we have parsed all the user input we
81 // output the (label, value) pairs in each constant pool at the end of the
82 // section.
83 //
84 // We use the MapVector for the map type to ensure stable iteration of
85 // the sections at the end of the parse. We need to iterate over the
86 // sections in a stable order to ensure that we have print the
87 // constant pools in a deterministic order when printing an assembly
88 // file.
89 using ConstantPoolMapTy = MapVector<MCSection *, ConstantPool>;
90 ConstantPoolMapTy ConstantPools;
91
92public:
93 LLVM_ABI void emitAll(MCStreamer &Streamer);
96 LLVM_ABI const MCExpr *addEntry(MCStreamer &Streamer, const MCExpr *Expr,
97 unsigned Size, SMLoc Loc);
98
99private:
100 ConstantPool *getConstantPool(MCSection *Section);
101 ConstantPool &getOrCreateConstantPool(MCSection *Section);
102};
103
104} // end namespace llvm
105
106#endif // LLVM_MC_CONSTANTPOOLS_H
#define LLVM_ABI
Definition Compiler.h:215
This file implements a map that provides insertion order iteration.
This file defines the SmallVector class.
LLVM_ABI void emitAll(MCStreamer &Streamer)
LLVM_ABI void clearCacheForCurrentSection(MCStreamer &Streamer)
LLVM_ABI void emitForCurrentSection(MCStreamer &Streamer)
LLVM_ABI const MCExpr * addEntry(MCStreamer &Streamer, const MCExpr *Expr, unsigned Size, SMLoc Loc)
LLVM_ABI void clearCache()
LLVM_ABI const MCExpr * addEntry(const MCExpr *Value, MCContext &Context, unsigned Size, SMLoc Loc)
ConstantPool()=default
LLVM_ABI void emitEntries(MCStreamer &Streamer)
LLVM_ABI bool empty()
Context object for machine code objects.
Definition MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:573
Streaming machine code generation interface.
Definition MCStreamer.h:222
Represent a reference to a symbol from inside an expression.
Definition MCExpr.h:190
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
Represents a location in source code.
Definition SMLoc.h:22
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
ConstantPoolEntry(MCSymbol *L, const MCExpr *Val, unsigned Sz, SMLoc Loc_)