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