BitCodes.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/Support/DataTypes.h"
  26. #include "llvm/Support/ErrorHandling.h"
  27. #include <cassert>
  28. namespace llvm {
  29. /// Offsets of the 32-bit fields of bitstream wrapper header.
  30. enum BitstreamWrapperHeader : unsigned {
  31. BWH_MagicField = 0 * 4,
  32. BWH_VersionField = 1 * 4,
  33. BWH_OffsetField = 2 * 4,
  34. BWH_SizeField = 3 * 4,
  35. BWH_CPUTypeField = 4 * 4,
  36. BWH_HeaderSize = 5 * 4
  37. };
  38. namespace bitc {
  39. enum StandardWidths {
  40. BlockIDWidth = 8, // We use VBR-8 for block IDs.
  41. CodeLenWidth = 4, // Codelen are VBR-4.
  42. BlockSizeWidth = 32 // BlockSize up to 2^32 32-bit words = 16GB per block.
  43. };
  44. // The standard abbrev namespace always has a way to exit a block, enter a
  45. // nested block, define abbrevs, and define an unabbreviated record.
  46. enum FixedAbbrevIDs {
  47. END_BLOCK = 0, // Must be zero to guarantee termination for broken bitcode.
  48. ENTER_SUBBLOCK = 1,
  49. /// DEFINE_ABBREV - Defines an abbrev for the current block. It consists
  50. /// of a vbr5 for # operand infos. Each operand info is emitted with a
  51. /// single bit to indicate if it is a literal encoding. If so, the value is
  52. /// emitted with a vbr8. If not, the encoding is emitted as 3 bits followed
  53. /// by the info value as a vbr5 if needed.
  54. DEFINE_ABBREV = 2,
  55. // UNABBREV_RECORDs are emitted with a vbr6 for the record code, followed by
  56. // a vbr6 for the # operands, followed by vbr6's for each operand.
  57. UNABBREV_RECORD = 3,
  58. // This is not a code, this is a marker for the first abbrev assignment.
  59. FIRST_APPLICATION_ABBREV = 4
  60. };
  61. /// StandardBlockIDs - All bitcode files can optionally include a BLOCKINFO
  62. /// block, which contains metadata about other blocks in the file.
  63. enum StandardBlockIDs {
  64. /// BLOCKINFO_BLOCK is used to define metadata about blocks, for example,
  65. /// standard abbrevs that should be available to all blocks of a specified
  66. /// ID.
  67. BLOCKINFO_BLOCK_ID = 0,
  68. // Block IDs 1-7 are reserved for future expansion.
  69. FIRST_APPLICATION_BLOCKID = 8
  70. };
  71. /// BlockInfoCodes - The blockinfo block contains metadata about user-defined
  72. /// blocks.
  73. enum BlockInfoCodes {
  74. // DEFINE_ABBREV has magic semantics here, applying to the current SETBID'd
  75. // block, instead of the BlockInfo block.
  76. BLOCKINFO_CODE_SETBID = 1, // SETBID: [blockid#]
  77. BLOCKINFO_CODE_BLOCKNAME = 2, // BLOCKNAME: [name]
  78. BLOCKINFO_CODE_SETRECORDNAME = 3 // BLOCKINFO_CODE_SETRECORDNAME:
  79. // [id, name]
  80. };
  81. } // End bitc namespace
  82. /// BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
  83. /// This is actually a union of two different things:
  84. /// 1. It could be a literal integer value ("the operand is always 17").
  85. /// 2. It could be an encoding specification ("this operand encoded like so").
  86. ///
  87. class BitCodeAbbrevOp {
  88. uint64_t Val; // A literal value or data for an encoding.
  89. bool IsLiteral : 1; // Indicate whether this is a literal value or not.
  90. unsigned Enc : 3; // The encoding to use.
  91. public:
  92. enum Encoding {
  93. Fixed = 1, // A fixed width field, Val specifies number of bits.
  94. VBR = 2, // A VBR field where Val specifies the width of each chunk.
  95. Array = 3, // A sequence of fields, next field species elt encoding.
  96. Char6 = 4, // A 6-bit fixed field which maps to [a-zA-Z0-9._].
  97. Blob = 5 // 32-bit aligned array of 8-bit characters.
  98. };
  99. explicit BitCodeAbbrevOp(uint64_t V) : Val(V), IsLiteral(true) {}
  100. explicit BitCodeAbbrevOp(Encoding E, uint64_t Data = 0)
  101. : Val(Data), IsLiteral(false), Enc(E) {}
  102. bool isLiteral() const { return IsLiteral; }
  103. bool isEncoding() const { return !IsLiteral; }
  104. // Accessors for literals.
  105. uint64_t getLiteralValue() const { assert(isLiteral()); return Val; }
  106. // Accessors for encoding info.
  107. Encoding getEncoding() const { assert(isEncoding()); return (Encoding)Enc; }
  108. uint64_t getEncodingData() const {
  109. assert(isEncoding() && hasEncodingData());
  110. return Val;
  111. }
  112. bool hasEncodingData() const { return hasEncodingData(getEncoding()); }
  113. static bool hasEncodingData(Encoding E) {
  114. switch (E) {
  115. case Fixed:
  116. case VBR:
  117. return true;
  118. case Array:
  119. case Char6:
  120. case Blob:
  121. return false;
  122. }
  123. report_fatal_error("Invalid encoding");
  124. }
  125. /// isChar6 - Return true if this character is legal in the Char6 encoding.
  126. static bool isChar6(char C) { return isAlnum(C) || C == '.' || C == '_'; }
  127. static unsigned EncodeChar6(char C) {
  128. if (C >= 'a' && C <= 'z') return C-'a';
  129. if (C >= 'A' && C <= 'Z') return C-'A'+26;
  130. if (C >= '0' && C <= '9') return C-'0'+26+26;
  131. if (C == '.') return 62;
  132. if (C == '_') return 63;
  133. llvm_unreachable("Not a value Char6 character!");
  134. }
  135. static char DecodeChar6(unsigned V) {
  136. assert((V & ~63) == 0 && "Not a Char6 encoded character!");
  137. return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._"
  138. [V];
  139. }
  140. };
  141. /// BitCodeAbbrev - This class represents an abbreviation record. An
  142. /// abbreviation allows a complex record that has redundancy to be stored in a
  143. /// specialized format instead of the fully-general, fully-vbr, format.
  144. class BitCodeAbbrev {
  145. SmallVector<BitCodeAbbrevOp, 32> OperandList;
  146. public:
  147. BitCodeAbbrev() = default;
  148. explicit BitCodeAbbrev(std::initializer_list<BitCodeAbbrevOp> OperandList)
  149. : OperandList(OperandList) {}
  150. unsigned getNumOperandInfos() const {
  151. return static_cast<unsigned>(OperandList.size());
  152. }
  153. const BitCodeAbbrevOp &getOperandInfo(unsigned N) const {
  154. return OperandList[N];
  155. }
  156. void Add(const BitCodeAbbrevOp &OpInfo) {
  157. OperandList.push_back(OpInfo);
  158. }
  159. };
  160. } // End llvm namespace
  161. #endif
  162. #ifdef __GNUC__
  163. #pragma GCC diagnostic pop
  164. #endif