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 // DEFINE_ABBREV has magic semantics here, applying to the current SETBID'd 00070 // block, instead of the BlockInfo block. 00071 00072 BLOCKINFO_CODE_SETBID = 1, // SETBID: [blockid#] 00073 BLOCKINFO_CODE_BLOCKNAME = 2, // BLOCKNAME: [name] 00074 BLOCKINFO_CODE_SETRECORDNAME = 3 // BLOCKINFO_CODE_SETRECORDNAME: [id, name] 00075 }; 00076 00077 } // End bitc namespace 00078 00079 /// BitCodeAbbrevOp - This describes one or more operands in an abbreviation. 00080 /// This is actually a union of two different things: 00081 /// 1. It could be a literal integer value ("the operand is always 17"). 00082 /// 2. It could be an encoding specification ("this operand encoded like so"). 00083 /// 00084 class BitCodeAbbrevOp { 00085 uint64_t Val; // A literal value or data for an encoding. 00086 bool IsLiteral : 1; // Indicate whether this is a literal value or not. 00087 unsigned Enc : 3; // The encoding to use. 00088 public: 00089 enum Encoding { 00090 Fixed = 1, // A fixed width field, Val specifies number of bits. 00091 VBR = 2, // A VBR field where Val specifies the width of each chunk. 00092 Array = 3, // A sequence of fields, next field species elt encoding. 00093 Char6 = 4, // A 6-bit fixed field which maps to [a-zA-Z0-9._]. 00094 Blob = 5 // 32-bit aligned array of 8-bit characters. 00095 }; 00096 00097 explicit BitCodeAbbrevOp(uint64_t V) : Val(V), IsLiteral(true) {} 00098 explicit BitCodeAbbrevOp(Encoding E, uint64_t Data = 0) 00099 : Val(Data), IsLiteral(false), Enc(E) {} 00100 00101 bool isLiteral() const { return IsLiteral; } 00102 bool isEncoding() const { return !IsLiteral; } 00103 00104 // Accessors for literals. 00105 uint64_t getLiteralValue() const { assert(isLiteral()); return Val; } 00106 00107 // Accessors for encoding info. 00108 Encoding getEncoding() const { assert(isEncoding()); return (Encoding)Enc; } 00109 uint64_t getEncodingData() const { 00110 assert(isEncoding() && hasEncodingData()); 00111 return Val; 00112 } 00113 00114 bool hasEncodingData() const { return hasEncodingData(getEncoding()); } 00115 static bool hasEncodingData(Encoding E) { 00116 switch (E) { 00117 default: assert(0 && "Unknown encoding"); 00118 case Fixed: 00119 case VBR: 00120 return true; 00121 case Array: 00122 case Char6: 00123 case Blob: 00124 return false; 00125 } 00126 } 00127 00128 /// isChar6 - Return true if this character is legal in the Char6 encoding. 00129 static bool isChar6(char C) { 00130 if (C >= 'a' && C <= 'z') return true; 00131 if (C >= 'A' && C <= 'Z') return true; 00132 if (C >= '0' && C <= '9') return true; 00133 if (C == '.' || C == '_') return true; 00134 return false; 00135 } 00136 static unsigned EncodeChar6(char C) { 00137 if (C >= 'a' && C <= 'z') return C-'a'; 00138 if (C >= 'A' && C <= 'Z') return C-'A'+26; 00139 if (C >= '0' && C <= '9') return C-'0'+26+26; 00140 if (C == '.') return 62; 00141 if (C == '_') return 63; 00142 assert(0 && "Not a value Char6 character!"); 00143 return 0; 00144 } 00145 00146 static char DecodeChar6(unsigned V) { 00147 assert((V & ~63) == 0 && "Not a Char6 encoded character!"); 00148 if (V < 26) return V+'a'; 00149 if (V < 26+26) return V-26+'A'; 00150 if (V < 26+26+10) return V-26-26+'0'; 00151 if (V == 62) return '.'; 00152 if (V == 63) return '_'; 00153 assert(0 && "Not a value Char6 character!"); 00154 return ' '; 00155 } 00156 00157 }; 00158 00159 /// BitCodeAbbrev - This class represents an abbreviation record. An 00160 /// abbreviation allows a complex record that has redundancy to be stored in a 00161 /// specialized format instead of the fully-general, fully-vbr, format. 00162 class BitCodeAbbrev { 00163 SmallVector<BitCodeAbbrevOp, 8> OperandList; 00164 unsigned char RefCount; // Number of things using this. 00165 ~BitCodeAbbrev() {} 00166 public: 00167 BitCodeAbbrev() : RefCount(1) {} 00168 00169 void addRef() { ++RefCount; } 00170 void dropRef() { if (--RefCount == 0) delete this; } 00171 00172 unsigned getNumOperandInfos() const { 00173 return static_cast<unsigned>(OperandList.size()); 00174 } 00175 const BitCodeAbbrevOp &getOperandInfo(unsigned N) const { 00176 return OperandList[N]; 00177 } 00178 00179 void Add(const BitCodeAbbrevOp &OpInfo) { 00180 OperandList.push_back(OpInfo); 00181 } 00182 }; 00183 } // End llvm namespace 00184 00185 #endif