BitCodes.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- BitCodes.h - Enum values for the bitstream format --------*- 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 header defines bitstream enum values.
  15. //
  16. // The enum values defined in this file should be considered permanent. If
  17. // new features are added, they should have values added at the end of the
  18. // respective lists.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_BITSTREAM_BITCODES_H
  22. #define LLVM_BITSTREAM_BITCODES_H
  23. #include "llvm/ADT/SmallVector.h"
  24. #include "llvm/ADT/StringExtras.h"
  25. #include "llvm/Bitstream/BitCodeEnums.h"
  26. #include "llvm/Support/DataTypes.h"
  27. #include "llvm/Support/ErrorHandling.h"
  28. #include <cassert>
  29. namespace llvm {
  30. /// BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
  31. /// This is actually a union of two different things:
  32. /// 1. It could be a literal integer value ("the operand is always 17").
  33. /// 2. It could be an encoding specification ("this operand encoded like so").
  34. ///
  35. class BitCodeAbbrevOp {
  36. uint64_t Val; // A literal value or data for an encoding.
  37. bool IsLiteral : 1; // Indicate whether this is a literal value or not.
  38. unsigned Enc : 3; // The encoding to use.
  39. public:
  40. enum Encoding {
  41. Fixed = 1, // A fixed width field, Val specifies number of bits.
  42. VBR = 2, // A VBR field where Val specifies the width of each chunk.
  43. Array = 3, // A sequence of fields, next field species elt encoding.
  44. Char6 = 4, // A 6-bit fixed field which maps to [a-zA-Z0-9._].
  45. Blob = 5 // 32-bit aligned array of 8-bit characters.
  46. };
  47. static bool isValidEncoding(uint64_t E) {
  48. return E >= 1 && E <= 5;
  49. }
  50. explicit BitCodeAbbrevOp(uint64_t V) : Val(V), IsLiteral(true) {}
  51. explicit BitCodeAbbrevOp(Encoding E, uint64_t Data = 0)
  52. : Val(Data), IsLiteral(false), Enc(E) {}
  53. bool isLiteral() const { return IsLiteral; }
  54. bool isEncoding() const { return !IsLiteral; }
  55. // Accessors for literals.
  56. uint64_t getLiteralValue() const { assert(isLiteral()); return Val; }
  57. // Accessors for encoding info.
  58. Encoding getEncoding() const { assert(isEncoding()); return (Encoding)Enc; }
  59. uint64_t getEncodingData() const {
  60. assert(isEncoding() && hasEncodingData());
  61. return Val;
  62. }
  63. bool hasEncodingData() const { return hasEncodingData(getEncoding()); }
  64. static bool hasEncodingData(Encoding E) {
  65. switch (E) {
  66. case Fixed:
  67. case VBR:
  68. return true;
  69. case Array:
  70. case Char6:
  71. case Blob:
  72. return false;
  73. }
  74. report_fatal_error("Invalid encoding");
  75. }
  76. /// isChar6 - Return true if this character is legal in the Char6 encoding.
  77. static bool isChar6(char C) { return isAlnum(C) || C == '.' || C == '_'; }
  78. static unsigned EncodeChar6(char C) {
  79. if (C >= 'a' && C <= 'z') return C-'a';
  80. if (C >= 'A' && C <= 'Z') return C-'A'+26;
  81. if (C >= '0' && C <= '9') return C-'0'+26+26;
  82. if (C == '.') return 62;
  83. if (C == '_') return 63;
  84. llvm_unreachable("Not a value Char6 character!");
  85. }
  86. static char DecodeChar6(unsigned V) {
  87. assert((V & ~63) == 0 && "Not a Char6 encoded character!");
  88. return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._"
  89. [V];
  90. }
  91. };
  92. /// BitCodeAbbrev - This class represents an abbreviation record. An
  93. /// abbreviation allows a complex record that has redundancy to be stored in a
  94. /// specialized format instead of the fully-general, fully-vbr, format.
  95. class BitCodeAbbrev {
  96. SmallVector<BitCodeAbbrevOp, 32> OperandList;
  97. public:
  98. BitCodeAbbrev() = default;
  99. explicit BitCodeAbbrev(std::initializer_list<BitCodeAbbrevOp> OperandList)
  100. : OperandList(OperandList) {}
  101. unsigned getNumOperandInfos() const {
  102. return static_cast<unsigned>(OperandList.size());
  103. }
  104. const BitCodeAbbrevOp &getOperandInfo(unsigned N) const {
  105. return OperandList[N];
  106. }
  107. void Add(const BitCodeAbbrevOp &OpInfo) {
  108. OperandList.push_back(OpInfo);
  109. }
  110. };
  111. } // namespace llvm
  112. #endif
  113. #ifdef __GNUC__
  114. #pragma GCC diagnostic pop
  115. #endif