1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===- BitCodes.h - Enum values for the bitcode format ----------*- C++ -*-===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This header Bitcode enum values.
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The enum values defined in this file should be considered permanent.  If
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// new features are added, they should have values added at the end of the
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// respective lists.
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef LLVM_BITCODE_BITCODES_H
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LLVM_BITCODE_BITCODES_H
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/SmallVector.h"
2219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Support/DataTypes.h"
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <cassert>
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm {
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace bitc {
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  enum StandardWidths {
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BlockIDWidth = 8,  // We use VBR-8 for block IDs.
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CodeLenWidth = 4,  // Codelen are VBR-4.
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BlockSizeWidth = 32  // BlockSize up to 2^32 32-bit words = 16GB per block.
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The standard abbrev namespace always has a way to exit a block, enter a
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // nested block, define abbrevs, and define an unabbreviated record.
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  enum FixedAbbrevIDs {
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    END_BLOCK = 0,  // Must be zero to guarantee termination for broken bitcode.
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ENTER_SUBBLOCK = 1,
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// DEFINE_ABBREV - Defines an abbrev for the current block.  It consists
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// of a vbr5 for # operand infos.  Each operand info is emitted with a
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// single bit to indicate if it is a literal encoding.  If so, the value is
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// emitted with a vbr8.  If not, the encoding is emitted as 3 bits followed
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// by the info value as a vbr5 if needed.
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DEFINE_ABBREV = 2,
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // UNABBREV_RECORDs are emitted with a vbr6 for the record code, followed by
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // a vbr6 for the # operands, followed by vbr6's for each operand.
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    UNABBREV_RECORD = 3,
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // This is not a code, this is a marker for the first abbrev assignment.
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FIRST_APPLICATION_ABBREV = 4
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// StandardBlockIDs - All bitcode files can optionally include a BLOCKINFO
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// block, which contains metadata about other blocks in the file.
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  enum StandardBlockIDs {
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// BLOCKINFO_BLOCK is used to define metadata about blocks, for example,
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// standard abbrevs that should be available to all blocks of a specified
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// ID.
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BLOCKINFO_BLOCK_ID = 0,
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Block IDs 1-7 are reserved for future expansion.
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FIRST_APPLICATION_BLOCKID = 8
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// BlockInfoCodes - The blockinfo block contains metadata about user-defined
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// blocks.
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  enum BlockInfoCodes {
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // DEFINE_ABBREV has magic semantics here, applying to the current SETBID'd
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // block, instead of the BlockInfo block.
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BLOCKINFO_CODE_SETBID = 1,       // SETBID: [blockid#]
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BLOCKINFO_CODE_BLOCKNAME = 2,    // BLOCKNAME: [name]
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BLOCKINFO_CODE_SETRECORDNAME = 3 // BLOCKINFO_CODE_SETRECORDNAME: [id, name]
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // End bitc namespace
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This is actually a union of two different things:
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///   1. It could be a literal integer value ("the operand is always 17").
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///   2. It could be an encoding specification ("this operand encoded like so").
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass BitCodeAbbrevOp {
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  uint64_t Val;           // A literal value or data for an encoding.
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool IsLiteral : 1;     // Indicate whether this is a literal value or not.
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Enc   : 3;     // The encoding to use.
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  enum Encoding {
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Fixed = 1,  // A fixed width field, Val specifies number of bits.
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    VBR   = 2,  // A VBR field where Val specifies the width of each chunk.
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Array = 3,  // A sequence of fields, next field species elt encoding.
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Char6 = 4,  // A 6-bit fixed field which maps to [a-zA-Z0-9._].
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Blob  = 5   // 32-bit aligned array of 8-bit characters.
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  explicit BitCodeAbbrevOp(uint64_t V) :  Val(V), IsLiteral(true) {}
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  explicit BitCodeAbbrevOp(Encoding E, uint64_t Data = 0)
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    : Val(Data), IsLiteral(false), Enc(E) {}
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isLiteral() const { return IsLiteral; }
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isEncoding() const { return !IsLiteral; }
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Accessors for literals.
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  uint64_t getLiteralValue() const { assert(isLiteral()); return Val; }
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Accessors for encoding info.
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Encoding getEncoding() const { assert(isEncoding()); return (Encoding)Enc; }
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  uint64_t getEncodingData() const {
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(isEncoding() && hasEncodingData());
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Val;
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool hasEncodingData() const { return hasEncodingData(getEncoding()); }
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static bool hasEncodingData(Encoding E) {
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (E) {
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: assert(0 && "Unknown encoding");
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Fixed:
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case VBR:
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Array:
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Char6:
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Blob:
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// isChar6 - Return true if this character is legal in the Char6 encoding.
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static bool isChar6(char C) {
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C >= 'a' && C <= 'z') return true;
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C >= 'A' && C <= 'Z') return true;
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C >= '0' && C <= '9') return true;
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C == '.' || C == '_') return true;
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static unsigned EncodeChar6(char C) {
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C >= 'a' && C <= 'z') return C-'a';
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C >= 'A' && C <= 'Z') return C-'A'+26;
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C >= '0' && C <= '9') return C-'0'+26+26;
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C == '.') return 62;
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C == '_') return 63;
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(0 && "Not a value Char6 character!");
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 0;
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static char DecodeChar6(unsigned V) {
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert((V & ~63) == 0 && "Not a Char6 encoded character!");
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (V < 26) return V+'a';
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (V < 26+26) return V-26+'A';
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (V < 26+26+10) return V-26-26+'0';
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (V == 62) return '.';
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (V == 63) return '_';
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(0 && "Not a value Char6 character!");
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ' ';
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// BitCodeAbbrev - This class represents an abbreviation record.  An
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// abbreviation allows a complex record that has redundancy to be stored in a
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specialized format instead of the fully-general, fully-vbr, format.
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass BitCodeAbbrev {
163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<BitCodeAbbrevOp, 8> OperandList;
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned char RefCount; // Number of things using this.
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ~BitCodeAbbrev() {}
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BitCodeAbbrev() : RefCount(1) {}
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void addRef() { ++RefCount; }
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void dropRef() { if (--RefCount == 0) delete this; }
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned getNumOperandInfos() const {
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return static_cast<unsigned>(OperandList.size());
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const BitCodeAbbrevOp &getOperandInfo(unsigned N) const {
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return OperandList[N];
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void Add(const BitCodeAbbrevOp &OpInfo) {
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OperandList.push_back(OpInfo);
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // End llvm namespace
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
186