Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:12

0001 //===- ConstantPools.h - Keep track of assembler-generated ------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file declares the ConstantPool and AssemblerConstantPools classes.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_MC_CONSTANTPOOLS_H
0014 #define LLVM_MC_CONSTANTPOOLS_H
0015 
0016 #include "llvm/ADT/MapVector.h"
0017 #include "llvm/ADT/SmallVector.h"
0018 #include "llvm/Support/SMLoc.h"
0019 #include <cstdint>
0020 #include <map>
0021 
0022 namespace llvm {
0023 
0024 class MCContext;
0025 class MCExpr;
0026 class MCSection;
0027 class MCStreamer;
0028 class MCSymbol;
0029 class MCSymbolRefExpr;
0030 
0031 struct ConstantPoolEntry {
0032   ConstantPoolEntry(MCSymbol *L, const MCExpr *Val, unsigned Sz, SMLoc Loc_)
0033     : Label(L), Value(Val), Size(Sz), Loc(Loc_) {}
0034 
0035   MCSymbol *Label;
0036   const MCExpr *Value;
0037   unsigned Size;
0038   SMLoc Loc;
0039 };
0040 
0041 // A class to keep track of assembler-generated constant pools that are use to
0042 // implement the ldr-pseudo.
0043 class ConstantPool {
0044   using EntryVecTy = SmallVector<ConstantPoolEntry, 4>;
0045   EntryVecTy Entries;
0046 
0047   // Caches of entries that already exist, indexed by their contents
0048   // and also the size of the constant.
0049   std::map<std::pair<int64_t, unsigned>, const MCSymbolRefExpr *>
0050       CachedConstantEntries;
0051   DenseMap<std::pair<const MCSymbol *, unsigned>, const MCSymbolRefExpr *>
0052       CachedSymbolEntries;
0053 
0054 public:
0055   // Initialize a new empty constant pool
0056   ConstantPool() = default;
0057 
0058   // Add a new entry to the constant pool in the next slot.
0059   // \param Value is the new entry to put in the constant pool.
0060   // \param Size is the size in bytes of the entry
0061   //
0062   // \returns a MCExpr that references the newly inserted value
0063   const MCExpr *addEntry(const MCExpr *Value, MCContext &Context,
0064                          unsigned Size, SMLoc Loc);
0065 
0066   // Emit the contents of the constant pool using the provided streamer.
0067   void emitEntries(MCStreamer &Streamer);
0068 
0069   // Return true if the constant pool is empty
0070   bool empty();
0071 
0072   void clearCache();
0073 };
0074 
0075 class AssemblerConstantPools {
0076   // Map type used to keep track of per-Section constant pools used by the
0077   // ldr-pseudo opcode. The map associates a section to its constant pool. The
0078   // constant pool is a vector of (label, value) pairs. When the ldr
0079   // pseudo is parsed we insert a new (label, value) pair into the constant pool
0080   // for the current section and add MCSymbolRefExpr to the new label as
0081   // an opcode to the ldr. After we have parsed all the user input we
0082   // output the (label, value) pairs in each constant pool at the end of the
0083   // section.
0084   //
0085   // We use the MapVector for the map type to ensure stable iteration of
0086   // the sections at the end of the parse. We need to iterate over the
0087   // sections in a stable order to ensure that we have print the
0088   // constant pools in a deterministic order when printing an assembly
0089   // file.
0090   using ConstantPoolMapTy = MapVector<MCSection *, ConstantPool>;
0091   ConstantPoolMapTy ConstantPools;
0092 
0093 public:
0094   void emitAll(MCStreamer &Streamer);
0095   void emitForCurrentSection(MCStreamer &Streamer);
0096   void clearCacheForCurrentSection(MCStreamer &Streamer);
0097   const MCExpr *addEntry(MCStreamer &Streamer, const MCExpr *Expr,
0098                          unsigned Size, SMLoc Loc);
0099 
0100 private:
0101   ConstantPool *getConstantPool(MCSection *Section);
0102   ConstantPool &getOrCreateConstantPool(MCSection *Section);
0103 };
0104 
0105 } // end namespace llvm
0106 
0107 #endif // LLVM_MC_CONSTANTPOOLS_H