ConstantPools.cpp 3.2 KB

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