ConstantPools.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //===- ConstantPools.cpp - ConstantPool class --*- 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 implements the ConstantPool and AssemblerConstantPools classes.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/ADT/MapVector.h"
  14. #include "llvm/MC/ConstantPools.h"
  15. #include "llvm/MC/MCContext.h"
  16. #include "llvm/MC/MCExpr.h"
  17. #include "llvm/MC/MCStreamer.h"
  18. // //
  19. ///////////////////////////////////////////////////////////////////////////////
  20. using namespace llvm;
  21. //
  22. // ConstantPool implementation
  23. //
  24. // Emit the contents of the constant pool using the provided streamer.
  25. void ConstantPool::emitEntries(MCStreamer &Streamer) {
  26. if (Entries.empty())
  27. return;
  28. Streamer.EmitDataRegion(MCDR_DataRegion);
  29. for (EntryVecTy::const_iterator I = Entries.begin(), E = Entries.end();
  30. I != E; ++I) {
  31. Streamer.EmitCodeAlignment(I->Size); // align naturally
  32. Streamer.EmitLabel(I->Label);
  33. Streamer.EmitValue(I->Value, I->Size);
  34. }
  35. Streamer.EmitDataRegion(MCDR_DataRegionEnd);
  36. Entries.clear();
  37. }
  38. const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context,
  39. unsigned Size) {
  40. MCSymbol *CPEntryLabel = Context.createTempSymbol();
  41. Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size));
  42. return MCSymbolRefExpr::create(CPEntryLabel, Context);
  43. }
  44. bool ConstantPool::empty() { return Entries.empty(); }
  45. //
  46. // AssemblerConstantPools implementation
  47. //
  48. ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
  49. ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
  50. if (CP == ConstantPools.end())
  51. return nullptr;
  52. return &CP->second;
  53. }
  54. ConstantPool &
  55. AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
  56. return ConstantPools[Section];
  57. }
  58. static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
  59. ConstantPool &CP) {
  60. if (!CP.empty()) {
  61. Streamer.SwitchSection(Section);
  62. CP.emitEntries(Streamer);
  63. }
  64. }
  65. void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {
  66. // Dump contents of assembler constant pools.
  67. for (ConstantPoolMapTy::iterator CPI = ConstantPools.begin(),
  68. CPE = ConstantPools.end();
  69. CPI != CPE; ++CPI) {
  70. MCSection *Section = CPI->first;
  71. ConstantPool &CP = CPI->second;
  72. emitConstantPool(Streamer, Section, CP);
  73. }
  74. }
  75. void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
  76. MCSection *Section = Streamer.getCurrentSection().first;
  77. if (ConstantPool *CP = getConstantPool(Section)) {
  78. emitConstantPool(Streamer, Section, *CP);
  79. }
  80. }
  81. const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
  82. const MCExpr *Expr,
  83. unsigned Size) {
  84. MCSection *Section = Streamer.getCurrentSection().first;
  85. return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
  86. Size);
  87. }