ConstantPools.h 3.0 KB

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