1b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner//===- BitCodes.h - Enum values for the bitcode format ----------*- C++ -*-===//
2b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner//
3b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner//                     The LLVM Compiler Infrastructure
4b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
7b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner//
8b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner//===----------------------------------------------------------------------===//
9b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner//
10b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner// This header Bitcode enum values.
11b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner//
12b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner// The enum values defined in this file should be considered permanent.  If
13b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner// new features are added, they should have values added at the end of the
14b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner// respective lists.
15b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner//
16b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner//===----------------------------------------------------------------------===//
17b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner
18b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner#ifndef LLVM_BITCODE_BITCODES_H
19b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner#define LLVM_BITCODE_BITCODES_H
20b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner
2137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include "llvm/ADT/IntrusiveRefCntPtr.h"
2236d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner#include "llvm/ADT/SmallVector.h"
231f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/DataTypes.h"
24732f05c41f177a0bc4d47e93a5d02120f146cb4cChandler Carruth#include "llvm/Support/ErrorHandling.h"
2536d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner#include <cassert>
2636d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner
27b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattnernamespace llvm {
28b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattnernamespace bitc {
29b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner  enum StandardWidths {
30170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey    BlockIDWidth   = 8,  // We use VBR-8 for block IDs.
31170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey    CodeLenWidth   = 4,  // Codelen are VBR-4.
321a26daa9c336abd7790d5e3366ed46288248aca0Zhongxing Xu    BlockSizeWidth = 32  // BlockSize up to 2^32 32-bit words = 16GB per block.
33b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner  };
34d3ff4a188e823baa141c288d31eb532ddf632b6eMisha Brukman
35a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner  // The standard abbrev namespace always has a way to exit a block, enter a
36b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner  // nested block, define abbrevs, and define an unabbreviated record.
37a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner  enum FixedAbbrevIDs {
38b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner    END_BLOCK = 0,  // Must be zero to guarantee termination for broken bitcode.
39b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner    ENTER_SUBBLOCK = 1,
4036d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner
4136d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner    /// DEFINE_ABBREV - Defines an abbrev for the current block.  It consists
4236d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner    /// of a vbr5 for # operand infos.  Each operand info is emitted with a
4336d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner    /// single bit to indicate if it is a literal encoding.  If so, the value is
4436d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner    /// emitted with a vbr8.  If not, the encoding is emitted as 3 bits followed
4536d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner    /// by the info value as a vbr5 if needed.
46d3ff4a188e823baa141c288d31eb532ddf632b6eMisha Brukman    DEFINE_ABBREV = 2,
47d3ff4a188e823baa141c288d31eb532ddf632b6eMisha Brukman
4836d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner    // UNABBREV_RECORDs are emitted with a vbr6 for the record code, followed by
4936d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner    // a vbr6 for the # operands, followed by vbr6's for each operand.
50b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner    UNABBREV_RECORD = 3,
51d3ff4a188e823baa141c288d31eb532ddf632b6eMisha Brukman
52b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner    // This is not a code, this is a marker for the first abbrev assignment.
53a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner    FIRST_APPLICATION_ABBREV = 4
54b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner  };
55d3ff4a188e823baa141c288d31eb532ddf632b6eMisha Brukman
56a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner  /// StandardBlockIDs - All bitcode files can optionally include a BLOCKINFO
57a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner  /// block, which contains metadata about other blocks in the file.
58a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner  enum StandardBlockIDs {
59a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner    /// BLOCKINFO_BLOCK is used to define metadata about blocks, for example,
60a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner    /// standard abbrevs that should be available to all blocks of a specified
61a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner    /// ID.
62a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner    BLOCKINFO_BLOCK_ID = 0,
63d3ff4a188e823baa141c288d31eb532ddf632b6eMisha Brukman
64a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner    // Block IDs 1-7 are reserved for future expansion.
65a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner    FIRST_APPLICATION_BLOCKID = 8
66a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner  };
67d3ff4a188e823baa141c288d31eb532ddf632b6eMisha Brukman
68a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner  /// BlockInfoCodes - The blockinfo block contains metadata about user-defined
69a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner  /// blocks.
70a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner  enum BlockInfoCodes {
71e17b658c792abd4a1552144b8a8808e44970da76Chris Lattner    // DEFINE_ABBREV has magic semantics here, applying to the current SETBID'd
72e17b658c792abd4a1552144b8a8808e44970da76Chris Lattner    // block, instead of the BlockInfo block.
73170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
74170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey    BLOCKINFO_CODE_SETBID        = 1, // SETBID: [blockid#]
75170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey    BLOCKINFO_CODE_BLOCKNAME     = 2, // BLOCKNAME: [name]
76170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey    BLOCKINFO_CODE_SETRECORDNAME = 3  // BLOCKINFO_CODE_SETRECORDNAME:
77170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey                                      //                             [id, name]
78a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner  };
79d3ff4a188e823baa141c288d31eb532ddf632b6eMisha Brukman
80b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner} // End bitc namespace
8136d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner
8236d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner/// BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
8336d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner/// This is actually a union of two different things:
8436d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner///   1. It could be a literal integer value ("the operand is always 17").
8536d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner///   2. It could be an encoding specification ("this operand encoded like so").
8636d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner///
8736d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattnerclass BitCodeAbbrevOp {
88299b2d2070165eccea3fb61f7387fa016b847338Chris Lattner  uint64_t Val;           // A literal value or data for an encoding.
89299b2d2070165eccea3fb61f7387fa016b847338Chris Lattner  bool IsLiteral : 1;     // Indicate whether this is a literal value or not.
90299b2d2070165eccea3fb61f7387fa016b847338Chris Lattner  unsigned Enc   : 3;     // The encoding to use.
9136d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattnerpublic:
9236d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner  enum Encoding {
9328e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner    Fixed = 1,  // A fixed width field, Val specifies number of bits.
943c074f61ed2bd1d8d48c3851d827a06b80d2608bChris Lattner    VBR   = 2,  // A VBR field where Val specifies the width of each chunk.
9528e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner    Array = 3,  // A sequence of fields, next field species elt encoding.
96dcd006bf7be859367f35db2417a42c83451431e8Chris Lattner    Char6 = 4,  // A 6-bit fixed field which maps to [a-zA-Z0-9._].
97ce8f9fe3c9ea50127eab28930506fc3b93390553Chris Lattner    Blob  = 5   // 32-bit aligned array of 8-bit characters.
9836d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner  };
99d3ff4a188e823baa141c288d31eb532ddf632b6eMisha Brukman
100cdf2b3b2f88d6f961b664e3f67a8ee37b46b0d27Dan Gohman  explicit BitCodeAbbrevOp(uint64_t V) :  Val(V), IsLiteral(true) {}
101cdf2b3b2f88d6f961b664e3f67a8ee37b46b0d27Dan Gohman  explicit BitCodeAbbrevOp(Encoding E, uint64_t Data = 0)
10236d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner    : Val(Data), IsLiteral(false), Enc(E) {}
103d3ff4a188e823baa141c288d31eb532ddf632b6eMisha Brukman
104170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey  bool isLiteral() const  { return IsLiteral; }
10536d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner  bool isEncoding() const { return !IsLiteral; }
10636d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner
10736d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner  // Accessors for literals.
10836d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner  uint64_t getLiteralValue() const { assert(isLiteral()); return Val; }
109d3ff4a188e823baa141c288d31eb532ddf632b6eMisha Brukman
11036d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner  // Accessors for encoding info.
11136d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner  Encoding getEncoding() const { assert(isEncoding()); return (Encoding)Enc; }
1123c074f61ed2bd1d8d48c3851d827a06b80d2608bChris Lattner  uint64_t getEncodingData() const {
1133c074f61ed2bd1d8d48c3851d827a06b80d2608bChris Lattner    assert(isEncoding() && hasEncodingData());
1143c074f61ed2bd1d8d48c3851d827a06b80d2608bChris Lattner    return Val;
1153c074f61ed2bd1d8d48c3851d827a06b80d2608bChris Lattner  }
116d3ff4a188e823baa141c288d31eb532ddf632b6eMisha Brukman
11736d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner  bool hasEncodingData() const { return hasEncodingData(getEncoding()); }
11836d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner  static bool hasEncodingData(Encoding E) {
1193c074f61ed2bd1d8d48c3851d827a06b80d2608bChris Lattner    switch (E) {
1203c074f61ed2bd1d8d48c3851d827a06b80d2608bChris Lattner    case Fixed:
1213c074f61ed2bd1d8d48c3851d827a06b80d2608bChris Lattner    case VBR:
1223c074f61ed2bd1d8d48c3851d827a06b80d2608bChris Lattner      return true;
1233c074f61ed2bd1d8d48c3851d827a06b80d2608bChris Lattner    case Array:
12428e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner    case Char6:
125dcd006bf7be859367f35db2417a42c83451431e8Chris Lattner    case Blob:
1263c074f61ed2bd1d8d48c3851d827a06b80d2608bChris Lattner      return false;
1273c074f61ed2bd1d8d48c3851d827a06b80d2608bChris Lattner    }
128ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    report_fatal_error("Invalid encoding");
12936d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner  }
130d3ff4a188e823baa141c288d31eb532ddf632b6eMisha Brukman
13128e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner  /// isChar6 - Return true if this character is legal in the Char6 encoding.
13228e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner  static bool isChar6(char C) {
13328e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner    if (C >= 'a' && C <= 'z') return true;
13428e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner    if (C >= 'A' && C <= 'Z') return true;
13528e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner    if (C >= '0' && C <= '9') return true;
13628e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner    if (C == '.' || C == '_') return true;
13728e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner    return false;
13828e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner  }
13928e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner  static unsigned EncodeChar6(char C) {
14028e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner    if (C >= 'a' && C <= 'z') return C-'a';
14128e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner    if (C >= 'A' && C <= 'Z') return C-'A'+26;
14228e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner    if (C >= '0' && C <= '9') return C-'0'+26+26;
143170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey    if (C == '.')             return 62;
144170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey    if (C == '_')             return 63;
14550bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper    llvm_unreachable("Not a value Char6 character!");
14628e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner  }
147d3ff4a188e823baa141c288d31eb532ddf632b6eMisha Brukman
14828e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner  static char DecodeChar6(unsigned V) {
14928e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner    assert((V & ~63) == 0 && "Not a Char6 encoded character!");
150de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._"
151de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar        [V];
15228e4c4c9b3b9bf8939405df24b87062c1f10a9a3Chris Lattner  }
153d3ff4a188e823baa141c288d31eb532ddf632b6eMisha Brukman
15436d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner};
15536d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner
156e19ead0f2452cefc1d8e67e8d0d4cc561a1962d5Benjamin Kramertemplate <> struct isPodLike<BitCodeAbbrevOp> { static const bool value=true; };
157e19ead0f2452cefc1d8e67e8d0d4cc561a1962d5Benjamin Kramer
158a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner/// BitCodeAbbrev - This class represents an abbreviation record.  An
159a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner/// abbreviation allows a complex record that has redundancy to be stored in a
160a727d5502c8e23c090da658bf14c5ebc1169a070Chris Lattner/// specialized format instead of the fully-general, fully-vbr, format.
16137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesclass BitCodeAbbrev : public RefCountedBase<BitCodeAbbrev> {
162e19ead0f2452cefc1d8e67e8d0d4cc561a1962d5Benjamin Kramer  SmallVector<BitCodeAbbrevOp, 32> OperandList;
16337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // Only RefCountedBase is allowed to delete.
1640c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  ~BitCodeAbbrev() = default;
16537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  friend class RefCountedBase<BitCodeAbbrev>;
166299b2d2070165eccea3fb61f7387fa016b847338Chris Lattner
16737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinespublic:
16834cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng  unsigned getNumOperandInfos() const {
16934cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng    return static_cast<unsigned>(OperandList.size());
17034cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng  }
17136d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner  const BitCodeAbbrevOp &getOperandInfo(unsigned N) const {
17236d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner    return OperandList[N];
17336d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner  }
174d3ff4a188e823baa141c288d31eb532ddf632b6eMisha Brukman
17536d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner  void Add(const BitCodeAbbrevOp &OpInfo) {
17636d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner    OperandList.push_back(OpInfo);
17736d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner  }
17836d5e7d31be61f631ace0488f0d6cd71b8f31a16Chris Lattner};
179b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner} // End llvm namespace
180b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner
181b35ca9db9ce25e6b61aa3eaee41464f647d34899Chris Lattner#endif
182