ConstantPools.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ConstantPools.h - Keep track of assembler-generated ------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares the ConstantPool and AssemblerConstantPools classes.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_MC_CONSTANTPOOLS_H
  18. #define LLVM_MC_CONSTANTPOOLS_H
  19. #include "llvm/ADT/MapVector.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/Support/SMLoc.h"
  22. #include <cstdint>
  23. #include <map>
  24. namespace llvm {
  25. class MCContext;
  26. class MCExpr;
  27. class MCSection;
  28. class MCStreamer;
  29. class MCSymbol;
  30. class MCSymbolRefExpr;
  31. struct ConstantPoolEntry {
  32. ConstantPoolEntry(MCSymbol *L, const MCExpr *Val, unsigned Sz, SMLoc Loc_)
  33. : Label(L), Value(Val), Size(Sz), Loc(Loc_) {}
  34. MCSymbol *Label;
  35. const MCExpr *Value;
  36. unsigned Size;
  37. SMLoc Loc;
  38. };
  39. // A class to keep track of assembler-generated constant pools that are use to
  40. // implement the ldr-pseudo.
  41. class ConstantPool {
  42. using EntryVecTy = SmallVector<ConstantPoolEntry, 4>;
  43. EntryVecTy Entries;
  44. std::map<int64_t, const MCSymbolRefExpr *> CachedConstantEntries;
  45. DenseMap<const MCSymbol *, const MCSymbolRefExpr *> CachedSymbolEntries;
  46. public:
  47. // Initialize a new empty constant pool
  48. ConstantPool() = default;
  49. // Add a new entry to the constant pool in the next slot.
  50. // \param Value is the new entry to put in the constant pool.
  51. // \param Size is the size in bytes of the entry
  52. //
  53. // \returns a MCExpr that references the newly inserted value
  54. const MCExpr *addEntry(const MCExpr *Value, MCContext &Context,
  55. unsigned Size, SMLoc Loc);
  56. // Emit the contents of the constant pool using the provided streamer.
  57. void emitEntries(MCStreamer &Streamer);
  58. // Return true if the constant pool is empty
  59. bool empty();
  60. void clearCache();
  61. };
  62. class AssemblerConstantPools {
  63. // Map type used to keep track of per-Section constant pools used by the
  64. // ldr-pseudo opcode. The map associates a section to its constant pool. The
  65. // constant pool is a vector of (label, value) pairs. When the ldr
  66. // pseudo is parsed we insert a new (label, value) pair into the constant pool
  67. // for the current section and add MCSymbolRefExpr to the new label as
  68. // an opcode to the ldr. After we have parsed all the user input we
  69. // output the (label, value) pairs in each constant pool at the end of the
  70. // section.
  71. //
  72. // We use the MapVector for the map type to ensure stable iteration of
  73. // the sections at the end of the parse. We need to iterate over the
  74. // sections in a stable order to ensure that we have print the
  75. // constant pools in a deterministic order when printing an assembly
  76. // file.
  77. using ConstantPoolMapTy = MapVector<MCSection *, ConstantPool>;
  78. ConstantPoolMapTy ConstantPools;
  79. public:
  80. void emitAll(MCStreamer &Streamer);
  81. void emitForCurrentSection(MCStreamer &Streamer);
  82. void clearCacheForCurrentSection(MCStreamer &Streamer);
  83. const MCExpr *addEntry(MCStreamer &Streamer, const MCExpr *Expr,
  84. unsigned Size, SMLoc Loc);
  85. private:
  86. ConstantPool *getConstantPool(MCSection *Section);
  87. ConstantPool &getOrCreateConstantPool(MCSection *Section);
  88. };
  89. } // end namespace llvm
  90. #endif // LLVM_MC_CONSTANTPOOLS_H
  91. #ifdef __GNUC__
  92. #pragma GCC diagnostic pop
  93. #endif