BitCodes.h 6.6 KB

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