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