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