CodeEmitter.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--------------------- CodeEmitter.h ------------------------*- 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. /// \file
  14. ///
  15. /// A utility class used to compute instruction encodings. It buffers encodings
  16. /// for later usage. It exposes a simple API to compute and get the encodings as
  17. /// StringRef.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_MCA_CODEEMITTER_H
  21. #define LLVM_MCA_CODEEMITTER_H
  22. #include "llvm/ADT/ArrayRef.h"
  23. #include "llvm/ADT/SmallString.h"
  24. #include "llvm/ADT/StringRef.h"
  25. #include "llvm/MC/MCAsmBackend.h"
  26. #include "llvm/MC/MCCodeEmitter.h"
  27. #include "llvm/MC/MCInst.h"
  28. #include "llvm/MC/MCSubtargetInfo.h"
  29. #include "llvm/Support/raw_ostream.h"
  30. namespace llvm {
  31. namespace mca {
  32. /// A utility class used to compute instruction encodings for a code region.
  33. ///
  34. /// It provides a simple API to compute and return instruction encodings as
  35. /// strings. Encodings are cached internally for later usage.
  36. class CodeEmitter {
  37. const MCSubtargetInfo &STI;
  38. const MCAsmBackend &MAB;
  39. const MCCodeEmitter &MCE;
  40. SmallString<256> Code;
  41. raw_svector_ostream VecOS;
  42. ArrayRef<MCInst> Sequence;
  43. // An EncodingInfo pair stores <base, length> information. Base (i.e. first)
  44. // is an index to the `Code`. Length (i.e. second) is the encoding size.
  45. using EncodingInfo = std::pair<unsigned, unsigned>;
  46. // A cache of encodings.
  47. SmallVector<EncodingInfo, 16> Encodings;
  48. EncodingInfo getOrCreateEncodingInfo(unsigned MCID);
  49. public:
  50. CodeEmitter(const MCSubtargetInfo &ST, const MCAsmBackend &AB,
  51. const MCCodeEmitter &CE, ArrayRef<MCInst> S)
  52. : STI(ST), MAB(AB), MCE(CE), VecOS(Code), Sequence(S),
  53. Encodings(S.size()) {}
  54. StringRef getEncoding(unsigned MCID) {
  55. EncodingInfo EI = getOrCreateEncodingInfo(MCID);
  56. return StringRef(&Code[EI.first], EI.second);
  57. }
  58. };
  59. } // namespace mca
  60. } // namespace llvm
  61. #endif // LLVM_MCA_CODEEMITTER_H
  62. #ifdef __GNUC__
  63. #pragma GCC diagnostic pop
  64. #endif