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" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/Support/SMLoc.h" 19 #include <cstdint> 20 #include <map> 21 22 namespace llvm { 23 24 class MCContext; 25 class MCExpr; 26 class MCSection; 27 class MCStreamer; 28 class MCSymbol; 29 class MCSymbolRefExpr; 30 31 struct ConstantPoolEntry { ConstantPoolEntryConstantPoolEntry32 ConstantPoolEntry(MCSymbol *L, const MCExpr *Val, unsigned Sz, SMLoc Loc_) 33 : Label(L), Value(Val), Size(Sz), Loc(Loc_) {} 34 35 MCSymbol *Label; 36 const MCExpr *Value; 37 unsigned Size; 38 SMLoc Loc; 39 }; 40 41 // A class to keep track of assembler-generated constant pools that are use to 42 // implement the ldr-pseudo. 43 class ConstantPool { 44 using EntryVecTy = SmallVector<ConstantPoolEntry, 4>; 45 EntryVecTy Entries; 46 47 // Caches of entries that already exist, indexed by their contents 48 // and also the size of the constant. 49 std::map<std::pair<int64_t, unsigned>, const MCSymbolRefExpr *> 50 CachedConstantEntries; 51 DenseMap<std::pair<const MCSymbol *, unsigned>, const MCSymbolRefExpr *> 52 CachedSymbolEntries; 53 54 public: 55 // Initialize a new empty constant pool 56 ConstantPool() = default; 57 58 // Add a new entry to the constant pool in the next slot. 59 // \param Value is the new entry to put in the constant pool. 60 // \param Size is the size in bytes of the entry 61 // 62 // \returns a MCExpr that references the newly inserted value 63 const MCExpr *addEntry(const MCExpr *Value, MCContext &Context, 64 unsigned Size, SMLoc Loc); 65 66 // Emit the contents of the constant pool using the provided streamer. 67 void emitEntries(MCStreamer &Streamer); 68 69 // Return true if the constant pool is empty 70 bool empty(); 71 72 void clearCache(); 73 }; 74 75 class AssemblerConstantPools { 76 // Map type used to keep track of per-Section constant pools used by the 77 // ldr-pseudo opcode. The map associates a section to its constant pool. The 78 // constant pool is a vector of (label, value) pairs. When the ldr 79 // pseudo is parsed we insert a new (label, value) pair into the constant pool 80 // for the current section and add MCSymbolRefExpr to the new label as 81 // an opcode to the ldr. After we have parsed all the user input we 82 // output the (label, value) pairs in each constant pool at the end of the 83 // section. 84 // 85 // We use the MapVector for the map type to ensure stable iteration of 86 // the sections at the end of the parse. We need to iterate over the 87 // sections in a stable order to ensure that we have print the 88 // constant pools in a deterministic order when printing an assembly 89 // file. 90 using ConstantPoolMapTy = MapVector<MCSection *, ConstantPool>; 91 ConstantPoolMapTy ConstantPools; 92 93 public: 94 void emitAll(MCStreamer &Streamer); 95 void emitForCurrentSection(MCStreamer &Streamer); 96 void clearCacheForCurrentSection(MCStreamer &Streamer); 97 const MCExpr *addEntry(MCStreamer &Streamer, const MCExpr *Expr, 98 unsigned Size, SMLoc Loc); 99 100 private: 101 ConstantPool *getConstantPool(MCSection *Section); 102 ConstantPool &getOrCreateConstantPool(MCSection *Section); 103 }; 104 105 } // end namespace llvm 106 107 #endif // LLVM_MC_CONSTANTPOOLS_H 108