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/SmallVector.h"
     22 #include "llvm/Support/DataTypes.h"
     23 #include <cassert>
     24 
     25 namespace llvm {
     26 namespace 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 = 16GB per block.
     31   };
     32 
     33   // The standard abbrev namespace always has a way to exit a block, enter a
     34   // nested block, define abbrevs, and define an unabbreviated record.
     35   enum FixedAbbrevIDs {
     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_APPLICATION_ABBREV = 4
     52   };
     53 
     54   /// StandardBlockIDs - All bitcode files can optionally include a BLOCKINFO
     55   /// block, which contains metadata about other blocks in the file.
     56   enum StandardBlockIDs {
     57     /// BLOCKINFO_BLOCK is used to define metadata about blocks, for example,
     58     /// standard abbrevs that should be available to all blocks of a specified
     59     /// ID.
     60     BLOCKINFO_BLOCK_ID = 0,
     61 
     62     // Block IDs 1-7 are reserved for future expansion.
     63     FIRST_APPLICATION_BLOCKID = 8
     64   };
     65 
     66   /// BlockInfoCodes - The blockinfo block contains metadata about user-defined
     67   /// blocks.
     68   enum BlockInfoCodes {
     69     // DEFINE_ABBREV has magic semantics here, applying to the current SETBID'd
     70     // block, instead of the BlockInfo block.
     71 
     72     BLOCKINFO_CODE_SETBID = 1,       // SETBID: [blockid#]
     73     BLOCKINFO_CODE_BLOCKNAME = 2,    // BLOCKNAME: [name]
     74     BLOCKINFO_CODE_SETRECORDNAME = 3 // BLOCKINFO_CODE_SETRECORDNAME: [id, name]
     75   };
     76 
     77 } // End bitc namespace
     78 
     79 /// BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
     80 /// This is actually a union of two different things:
     81 ///   1. It could be a literal integer value ("the operand is always 17").
     82 ///   2. It could be an encoding specification ("this operand encoded like so").
     83 ///
     84 class BitCodeAbbrevOp {
     85   uint64_t Val;           // A literal value or data for an encoding.
     86   bool IsLiteral : 1;     // Indicate whether this is a literal value or not.
     87   unsigned Enc   : 3;     // The encoding to use.
     88 public:
     89   enum Encoding {
     90     Fixed = 1,  // A fixed width field, Val specifies number of bits.
     91     VBR   = 2,  // A VBR field where Val specifies the width of each chunk.
     92     Array = 3,  // A sequence of fields, next field species elt encoding.
     93     Char6 = 4,  // A 6-bit fixed field which maps to [a-zA-Z0-9._].
     94     Blob  = 5   // 32-bit aligned array of 8-bit characters.
     95   };
     96 
     97   explicit BitCodeAbbrevOp(uint64_t V) :  Val(V), IsLiteral(true) {}
     98   explicit BitCodeAbbrevOp(Encoding E, uint64_t Data = 0)
     99     : Val(Data), IsLiteral(false), Enc(E) {}
    100 
    101   bool isLiteral() const { return IsLiteral; }
    102   bool isEncoding() const { return !IsLiteral; }
    103 
    104   // Accessors for literals.
    105   uint64_t getLiteralValue() const { assert(isLiteral()); return Val; }
    106 
    107   // Accessors for encoding info.
    108   Encoding getEncoding() const { assert(isEncoding()); return (Encoding)Enc; }
    109   uint64_t getEncodingData() const {
    110     assert(isEncoding() && hasEncodingData());
    111     return Val;
    112   }
    113 
    114   bool hasEncodingData() const { return hasEncodingData(getEncoding()); }
    115   static bool hasEncodingData(Encoding E) {
    116     switch (E) {
    117     default: assert(0 && "Unknown encoding");
    118     case Fixed:
    119     case VBR:
    120       return true;
    121     case Array:
    122     case Char6:
    123     case Blob:
    124       return false;
    125     }
    126   }
    127 
    128   /// isChar6 - Return true if this character is legal in the Char6 encoding.
    129   static bool isChar6(char C) {
    130     if (C >= 'a' && C <= 'z') return true;
    131     if (C >= 'A' && C <= 'Z') return true;
    132     if (C >= '0' && C <= '9') return true;
    133     if (C == '.' || C == '_') return true;
    134     return false;
    135   }
    136   static unsigned EncodeChar6(char C) {
    137     if (C >= 'a' && C <= 'z') return C-'a';
    138     if (C >= 'A' && C <= 'Z') return C-'A'+26;
    139     if (C >= '0' && C <= '9') return C-'0'+26+26;
    140     if (C == '.') return 62;
    141     if (C == '_') return 63;
    142     assert(0 && "Not a value Char6 character!");
    143     return 0;
    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     assert(0 && "Not a value Char6 character!");
    154     return ' ';
    155   }
    156 
    157 };
    158 
    159 /// BitCodeAbbrev - This class represents an abbreviation record.  An
    160 /// abbreviation allows a complex record that has redundancy to be stored in a
    161 /// specialized format instead of the fully-general, fully-vbr, format.
    162 class BitCodeAbbrev {
    163   SmallVector<BitCodeAbbrevOp, 8> OperandList;
    164   unsigned char RefCount; // Number of things using this.
    165   ~BitCodeAbbrev() {}
    166 public:
    167   BitCodeAbbrev() : RefCount(1) {}
    168 
    169   void addRef() { ++RefCount; }
    170   void dropRef() { if (--RefCount == 0) delete this; }
    171 
    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