BitCodes.h 6.5 KB

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