1 //===- BitCodes.h - Enum values for the bitcode format ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header Bitcode enum values.
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
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_BITCODE_BITCODES_H
19 #define LLVM_BITCODE_BITCODES_H
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/Support/DataTypes.h"
28 BlockIDWidth = 8, // We use VBR-8 for block IDs.
29 CodeLenWidth = 4, // Codelen are VBR-4.
30 BlockSizeWidth = 32 // BlockSize up to 2^32 32-bit words = 32GB per block.
33 // The standard abbrev namespace always has a way to exit a block, enter a
34 // nested block, define abbrevs, and define an unabbreviated record.
36 END_BLOCK = 0, // Must be zero to guarantee termination for broken bitcode.
39 /// DEFINE_ABBREV - Defines an abbrev for the current block. It consists
40 /// of a vbr5 for # operand infos. Each operand info is emitted with a
41 /// single bit to indicate if it is a literal encoding. If so, the value is
42 /// emitted with a vbr8. If not, the encoding is emitted as 3 bits followed
43 /// by the info value as a vbr5 if needed.
46 // UNABBREV_RECORDs are emitted with a vbr6 for the record code, followed by
47 // a vbr6 for the # operands, followed by vbr6's for each operand.
50 // This is not a code, this is a marker for the first abbrev assignment.
51 FIRST_APPLICATION_ABBREV = 4
54 /// StandardBlockIDs - All bitcode files can optionally include a BLOCKINFO
55 /// block, which contains metadata about other blocks in the file.
56 enum StandardBlockIDs {
57 /// BLOCKINFO_BLOCK is used to define metadata about blocks, for example,
58 /// standard abbrevs that should be available to all blocks of a specified
60 BLOCKINFO_BLOCK_ID = 0,
62 // Block IDs 1-7 are reserved for future expansion.
63 FIRST_APPLICATION_BLOCKID = 8
66 /// BlockInfoCodes - The blockinfo block contains metadata about user-defined
69 BLOCKINFO_CODE_SETBID = 1 // SETBID: [blockid#]
70 // DEFINE_ABBREV has magic semantics here, applying to the current SETBID'd
71 // block, instead of the BlockInfo block.
72 // BLOCKNAME: give string name to block, if desired.
75 } // 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").
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.
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._].
94 BitCodeAbbrevOp(uint64_t V) : Val(V), IsLiteral(true) {}
95 BitCodeAbbrevOp(Encoding E, uint64_t Data = 0)
96 : Val(Data), IsLiteral(false), Enc(E) {}
98 bool isLiteral() const { return IsLiteral; }
99 bool isEncoding() const { return !IsLiteral; }
101 // Accessors for literals.
102 uint64_t getLiteralValue() const { assert(isLiteral()); return Val; }
104 // Accessors for encoding info.
105 Encoding getEncoding() const { assert(isEncoding()); return (Encoding)Enc; }
106 uint64_t getEncodingData() const {
107 assert(isEncoding() && hasEncodingData());
111 bool hasEncodingData() const { return hasEncodingData(getEncoding()); }
112 static bool hasEncodingData(Encoding E) {
114 default: assert(0 && "Unknown encoding");
124 /// isChar6 - Return true if this character is legal in the Char6 encoding.
125 static bool isChar6(char C) {
126 if (C >= 'a' && C <= 'z') return true;
127 if (C >= 'A' && C <= 'Z') return true;
128 if (C >= '0' && C <= '9') return true;
129 if (C == '.' || C == '_') return true;
132 static unsigned EncodeChar6(char C) {
133 if (C >= 'a' && C <= 'z') return C-'a';
134 if (C >= 'A' && C <= 'Z') return C-'A'+26;
135 if (C >= '0' && C <= '9') return C-'0'+26+26;
136 if (C == '.') return 62;
137 if (C == '_') return 63;
138 assert(0 && "Not a value Char6 character!");
142 static char DecodeChar6(unsigned V) {
143 assert((V & ~63) == 0 && "Not a Char6 encoded character!");
144 if (V < 26) return V+'a';
145 if (V < 26+26) return V-26+'A';
146 if (V < 26+26+10) return V-26-26+'0';
147 if (V == 62) return '.';
148 if (V == 63) return '_';
149 assert(0 && "Not a value Char6 character!");
155 /// BitCodeAbbrev - This class represents an abbreviation record. An
156 /// abbreviation allows a complex record that has redundancy to be stored in a
157 /// specialized format instead of the fully-general, fully-vbr, format.
158 class BitCodeAbbrev {
159 SmallVector<BitCodeAbbrevOp, 8> OperandList;
160 unsigned char RefCount; // Number of things using this.
163 BitCodeAbbrev() : RefCount(1) {}
165 void addRef() { ++RefCount; }
166 void dropRef() { if (--RefCount == 0) delete this; }
168 unsigned getNumOperandInfos() const { return OperandList.size(); }
169 const BitCodeAbbrevOp &getOperandInfo(unsigned N) const {
170 return OperandList[N];
173 void Add(const BitCodeAbbrevOp &OpInfo) {
174 OperandList.push_back(OpInfo);
177 } // End llvm namespace