LLVM API Documentation
00001 //===- BitCodes.h - Enum values for the bitcode format ----------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This header Bitcode enum values. 00011 // 00012 // The enum values defined in this file should be considered permanent. If 00013 // new features are added, they should have values added at the end of the 00014 // respective lists. 00015 // 00016 //===----------------------------------------------------------------------===// 00017 00018 #ifndef LLVM_BITCODE_BITCODES_H 00019 #define LLVM_BITCODE_BITCODES_H 00020 00021 #include "llvm/ADT/SmallVector.h" 00022 #include "llvm/Support/DataTypes.h" 00023 #include <cassert> 00024 00025 namespace llvm { 00026 namespace bitc { 00027 enum StandardWidths { 00028 BlockIDWidth = 8, // We use VBR-8 for block IDs. 00029 CodeLenWidth = 4, // Codelen are VBR-4. 00030 BlockSizeWidth = 32 // BlockSize up to 2^32 32-bit words = 16GB per block. 00031 }; 00032 00033 // The standard abbrev namespace always has a way to exit a block, enter a 00034 // nested block, define abbrevs, and define an unabbreviated record. 00035 enum FixedAbbrevIDs { 00036 END_BLOCK = 0, // Must be zero to guarantee termination for broken bitcode. 00037 ENTER_SUBBLOCK = 1, 00038 00039 /// DEFINE_ABBREV - Defines an abbrev for the current block. It consists 00040 /// of a vbr5 for # operand infos. Each operand info is emitted with a 00041 /// single bit to indicate if it is a literal encoding. If so, the value is 00042 /// emitted with a vbr8. If not, the encoding is emitted as 3 bits followed 00043 /// by the info value as a vbr5 if needed. 00044 DEFINE_ABBREV = 2, 00045 00046 // UNABBREV_RECORDs are emitted with a vbr6 for the record code, followed by 00047 // a vbr6 for the # operands, followed by vbr6's for each operand. 00048 UNABBREV_RECORD = 3, 00049 00050 // This is not a code, this is a marker for the first abbrev assignment. 00051 FIRST_APPLICATION_ABBREV = 4 00052 }; 00053 00054 /// StandardBlockIDs - All bitcode files can optionally include a BLOCKINFO 00055 /// block, which contains metadata about other blocks in the file. 00056 enum StandardBlockIDs { 00057 /// BLOCKINFO_BLOCK is used to define metadata about blocks, for example, 00058 /// standard abbrevs that should be available to all blocks of a specified 00059 /// ID. 00060 BLOCKINFO_BLOCK_ID = 0, 00061 00062 // Block IDs 1-7 are reserved for future expansion. 00063 FIRST_APPLICATION_BLOCKID = 8 00064 }; 00065 00066 /// BlockInfoCodes - The blockinfo block contains metadata about user-defined 00067 /// blocks. 00068 enum BlockInfoCodes { 00069 BLOCKINFO_CODE_SETBID = 1 // SETBID: [blockid#] 00070 // DEFINE_ABBREV has magic semantics here, applying to the current SETBID'd 00071 // block, instead of the BlockInfo block. 00072 // BLOCKNAME: give string name to block, if desired. 00073 }; 00074 00075 } // End bitc namespace 00076 00077 /// BitCodeAbbrevOp - This describes one or more operands in an abbreviation. 00078 /// This is actually a union of two different things: 00079 /// 1. It could be a literal integer value ("the operand is always 17"). 00080 /// 2. It could be an encoding specification ("this operand encoded like so"). 00081 /// 00082 class BitCodeAbbrevOp { 00083 uint64_t Val; // A literal value or data for an encoding. 00084 bool IsLiteral : 1; // Indicate whether this is a literal value or not. 00085 unsigned Enc : 3; // The encoding to use. 00086 public: 00087 enum Encoding { 00088 Fixed = 1, // A fixed width field, Val specifies number of bits. 00089 VBR = 2, // A VBR field where Val specifies the width of each chunk. 00090 Array = 3, // A sequence of fields, next field species elt encoding. 00091 Char6 = 4 // A 6-bit fixed field which maps to [a-zA-Z0-9._]. 00092 }; 00093 00094 explicit BitCodeAbbrevOp(uint64_t V) : Val(V), IsLiteral(true) {} 00095 explicit BitCodeAbbrevOp(Encoding E, uint64_t Data = 0) 00096 : Val(Data), IsLiteral(false), Enc(E) {} 00097 00098 bool isLiteral() const { return IsLiteral; } 00099 bool isEncoding() const { return !IsLiteral; } 00100 00101 // Accessors for literals. 00102 uint64_t getLiteralValue() const { assert(isLiteral()); return Val; } 00103 00104 // Accessors for encoding info. 00105 Encoding getEncoding() const { assert(isEncoding()); return (Encoding)Enc; } 00106 uint64_t getEncodingData() const { 00107 assert(isEncoding() && hasEncodingData()); 00108 return Val; 00109 } 00110 00111 bool hasEncodingData() const { return hasEncodingData(getEncoding()); } 00112 static bool hasEncodingData(Encoding E) { 00113 switch (E) { 00114 default: assert(0 && "Unknown encoding"); 00115 case Fixed: 00116 case VBR: 00117 return true; 00118 case Array: 00119 case Char6: 00120 return false; 00121 } 00122 } 00123 00124 /// isChar6 - Return true if this character is legal in the Char6 encoding. 00125 static bool isChar6(char C) { 00126 if (C >= 'a' && C <= 'z') return true; 00127 if (C >= 'A' && C <= 'Z') return true; 00128 if (C >= '0' && C <= '9') return true; 00129 if (C == '.' || C == '_') return true; 00130 return false; 00131 } 00132 static unsigned EncodeChar6(char C) { 00133 if (C >= 'a' && C <= 'z') return C-'a'; 00134 if (C >= 'A' && C <= 'Z') return C-'A'+26; 00135 if (C >= '0' && C <= '9') return C-'0'+26+26; 00136 if (C == '.') return 62; 00137 if (C == '_') return 63; 00138 assert(0 && "Not a value Char6 character!"); 00139 return 0; 00140 } 00141 00142 static char DecodeChar6(unsigned V) { 00143 assert((V & ~63) == 0 && "Not a Char6 encoded character!"); 00144 if (V < 26) return V+'a'; 00145 if (V < 26+26) return V-26+'A'; 00146 if (V < 26+26+10) return V-26-26+'0'; 00147 if (V == 62) return '.'; 00148 if (V == 63) return '_'; 00149 assert(0 && "Not a value Char6 character!"); 00150 return ' '; 00151 } 00152 00153 }; 00154 00155 /// BitCodeAbbrev - This class represents an abbreviation record. An 00156 /// abbreviation allows a complex record that has redundancy to be stored in a 00157 /// specialized format instead of the fully-general, fully-vbr, format. 00158 class BitCodeAbbrev { 00159 SmallVector<BitCodeAbbrevOp, 8> OperandList; 00160 unsigned char RefCount; // Number of things using this. 00161 ~BitCodeAbbrev() {} 00162 public: 00163 BitCodeAbbrev() : RefCount(1) {} 00164 00165 void addRef() { ++RefCount; } 00166 void dropRef() { if (--RefCount == 0) delete this; } 00167 00168 unsigned getNumOperandInfos() const { 00169 return static_cast<unsigned>(OperandList.size()); 00170 } 00171 const BitCodeAbbrevOp &getOperandInfo(unsigned N) const { 00172 return OperandList[N]; 00173 } 00174 00175 void Add(const BitCodeAbbrevOp &OpInfo) { 00176 OperandList.push_back(OpInfo); 00177 } 00178 }; 00179 } // End llvm namespace 00180 00181 #endif