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