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