BitCodes.h revision 299b2d2070165eccea3fb61f7387fa016b847338
1//===- BitCodes.h - Enum values for the bitcode format ----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
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.
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 <cassert>
24
25namespace llvm {
26namespace bitc {
27  enum StandardWidths {
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.
31  };
32
33  // The standard code namespace always has a way to exit a block, enter a
34  // nested block, define abbrevs, and define an unabbreviated record.
35  enum FixedCodes {
36    END_BLOCK = 0,  // Must be zero to guarantee termination for broken bitcode.
37    ENTER_SUBBLOCK = 1,
38
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.
44    DEFINE_ABBREV = 2,
45
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.
48    UNABBREV_RECORD = 3,
49
50    // This is not a code, this is a marker for the first abbrev assignment.
51    FIRST_ABBREV = 4
52  };
53} // End bitc namespace
54
55/// BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
56/// This is actually a union of two different things:
57///   1. It could be a literal integer value ("the operand is always 17").
58///   2. It could be an encoding specification ("this operand encoded like so").
59///
60class BitCodeAbbrevOp {
61  uint64_t Val;           // A literal value or data for an encoding.
62  bool IsLiteral : 1;     // Indicate whether this is a literal value or not.
63  unsigned Enc   : 3;     // The encoding to use.
64public:
65  enum Encoding {
66    FixedWidth = 1,   // A fixed with field, Val specifies number of bits.
67    VBR        = 2   // A VBR field where Val specifies the width of each chunk.
68  };
69
70  BitCodeAbbrevOp(uint64_t V) :  Val(V), IsLiteral(true) {}
71  BitCodeAbbrevOp(Encoding E, uint64_t Data)
72    : Val(Data), IsLiteral(false), Enc(E) {}
73
74  bool isLiteral() const { return IsLiteral; }
75  bool isEncoding() const { return !IsLiteral; }
76
77  // Accessors for literals.
78  uint64_t getLiteralValue() const { assert(isLiteral()); return Val; }
79
80  // Accessors for encoding info.
81  Encoding getEncoding() const { assert(isEncoding()); return (Encoding)Enc; }
82  uint64_t getEncodingData() const { assert(isEncoding()); return Val; }
83
84  bool hasEncodingData() const { return hasEncodingData(getEncoding()); }
85  static bool hasEncodingData(Encoding E) {
86    return true;
87  }
88};
89
90class BitCodeAbbrev {
91  SmallVector<BitCodeAbbrevOp, 8> OperandList;
92  unsigned char RefCount; // Number of things using this.
93  ~BitCodeAbbrev() {}
94public:
95  BitCodeAbbrev() : RefCount(1) {}
96
97  void addRef() { ++RefCount; }
98  void dropRef() { if (--RefCount == 0) delete this; }
99
100  unsigned getNumOperandInfos() const { return OperandList.size(); }
101  const BitCodeAbbrevOp &getOperandInfo(unsigned N) const {
102    return OperandList[N];
103  }
104
105  void Add(const BitCodeAbbrevOp &OpInfo) {
106    OperandList.push_back(OpInfo);
107  }
108};
109} // End llvm namespace
110
111#endif
112