compute this value correctly
[oota-llvm.git] / include / llvm / Bitcode / BitstreamReader.h
index ceae5afbe98c4232421e8757acb9086f4c216982..c6394b6c99a539109f55dfe7564838005f9f48a1 100644 (file)
@@ -16,8 +16,7 @@
 #define BITSTREAM_READER_H
 
 #include "llvm/Bitcode/BitCodes.h"
-#include "llvm/ADT/SmallVector.h"
-#include <cassert>
+#include <vector>
 
 namespace llvm {
   
@@ -36,21 +35,51 @@ class BitstreamReader {
   // CurCodeSize - This is the declared size of code values used for the current
   // block, in bits.
   unsigned CurCodeSize;
+
+  /// CurAbbrevs - Abbrevs installed at in this block.
+  std::vector<BitCodeAbbrev*> CurAbbrevs;
   
-  /// BlockScope - This tracks the codesize of parent blocks.
-  SmallVector<unsigned, 8> BlockScope;
+  struct Block {
+    unsigned PrevCodeSize;
+    std::vector<BitCodeAbbrev*> PrevAbbrevs;
+    explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
+  };
   
+  /// BlockScope - This tracks the codesize of parent blocks.
+  SmallVector<Block, 8> BlockScope;
+
+  /// FirstChar - This remembers the first byte of the stream.
+  const unsigned char *FirstChar;
 public:
   BitstreamReader(const unsigned char *Start, const unsigned char *End)
-    : NextChar(Start), LastChar(End) {
+    : NextChar(Start), LastChar(End), FirstChar(Start) {
     assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes");
     CurWord = 0;
     BitsInCurWord = 0;
     CurCodeSize = 2;
   }
   
+  ~BitstreamReader() {
+    // Abbrevs could still exist if the stream was broken.  If so, don't leak
+    // them.
+    for (unsigned i = 0, e = CurAbbrevs.size(); i != e; ++i)
+      delete CurAbbrevs[i];
+
+    for (unsigned S = 0, e = BlockScope.size(); S != e; ++S) {
+      std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
+      for (unsigned i = 0, e = Abbrevs.size(); i != e; ++i)
+        delete Abbrevs[i];
+    }
+  }
+  
   bool AtEndOfStream() const { return NextChar == LastChar; }
   
+  /// GetCurrentBitNo - Return the bit # of the bit we are reading.
+  uint64_t GetCurrentBitNo() const {
+    return (NextChar-FirstChar)*8 + (32-BitsInCurWord);
+  }
+  
+  
   uint32_t Read(unsigned NumBits) {
     // If the field is fully contained by CurWord, return it quickly.
     if (BitsInCurWord >= NumBits) {
@@ -89,9 +118,16 @@ public:
     return R;
   }
   
+  uint64_t Read64(unsigned NumBits) {
+    if (NumBits <= 32) return Read(NumBits);
+    
+    uint64_t V = Read(32);
+    return V | (uint64_t)Read(NumBits-32) << 32;
+  }
+  
   uint32_t ReadVBR(unsigned NumBits) {
     uint32_t Piece = Read(NumBits);
-    if ((Piece & (1U << NumBits-1)) == 0)
+    if ((Piece & (1U << (NumBits-1))) == 0)
       return Piece;
 
     uint32_t Result = 0;
@@ -99,7 +135,7 @@ public:
     while (1) {
       Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
 
-      if ((Piece & (1U << NumBits-1)) == 0)
+      if ((Piece & (1U << (NumBits-1))) == 0)
         return Result;
       
       NextBit += NumBits-1;
@@ -109,7 +145,7 @@ public:
   
   uint64_t ReadVBR64(unsigned NumBits) {
     uint64_t Piece = Read(NumBits);
-    if ((Piece & (1U << NumBits-1)) == 0)
+    if ((Piece & (1U << (NumBits-1))) == 0)
       return Piece;
     
     uint64_t Result = 0;
@@ -117,7 +153,7 @@ public:
     while (1) {
       Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
       
-      if ((Piece & (1U << NumBits-1)) == 0)
+      if ((Piece & (1U << (NumBits-1))) == 0)
         return Result;
       
       NextBit += NumBits-1;
@@ -170,7 +206,8 @@ public:
   /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, read and enter
   /// the block, returning the BlockID of the block we just entered.
   bool EnterSubBlock() {
-    BlockScope.push_back(CurCodeSize);
+    BlockScope.push_back(Block(CurCodeSize));
+    BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
     
     // Get the codesize of this block.
     CurCodeSize = ReadVBR(bitc::CodeLenWidth);
@@ -190,7 +227,13 @@ public:
     // Block tail:
     //    [END_BLOCK, <align4bytes>]
     SkipToWord();
-    CurCodeSize = BlockScope.back();
+    CurCodeSize = BlockScope.back().PrevCodeSize;
+    
+    // Delete abbrevs from popped scope.
+    for (unsigned i = 0, e = CurAbbrevs.size(); i != e; ++i)
+      delete CurAbbrevs[i];
+    
+    BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
     BlockScope.pop_back();
     return false;
   }
@@ -208,13 +251,59 @@ public:
       return Code;
     }
     
-    assert(0 && "Reading with abbrevs not implemented!");
+    unsigned AbbrevNo = AbbrevID-bitc::FIRST_ABBREV;
+    assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
+    BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo];
+
+    for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
+      const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
+      if (Op.isLiteral()) {
+        // If the abbrev specifies the literal value to use, use it.
+        Vals.push_back(Op.getLiteralValue());
+      } else {
+        // Decode the value as we are commanded.
+        switch (Op.getEncoding()) {
+        default: assert(0 && "Unknown encoding!");
+        case BitCodeAbbrevOp::FixedWidth:
+          Vals.push_back(Read(Op.getEncodingData()));
+          break;
+        case BitCodeAbbrevOp::VBR:
+          Vals.push_back(ReadVBR64(Op.getEncodingData()));
+          break;
+        }
+      }
+    }
+    
+    unsigned Code = Vals[0];
+    Vals.erase(Vals.begin());
+    return Code;
   }
   
+  //===--------------------------------------------------------------------===//
+  // Abbrev Processing
+  //===--------------------------------------------------------------------===//
+  
+  void ReadAbbrevRecord() {
+    BitCodeAbbrev *Abbv = new BitCodeAbbrev();
+    unsigned NumOpInfo = ReadVBR(5);
+    for (unsigned i = 0; i != NumOpInfo; ++i) {
+      bool IsLiteral = Read(1);
+      if (IsLiteral) {
+        Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
+        continue;
+      }
+
+      BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
+      if (BitCodeAbbrevOp::hasEncodingData(E)) {
+        Abbv->Add(BitCodeAbbrevOp(E, ReadVBR64(5)));
+      } else {
+        assert(0 && "unimp");
+      }
+    }
+    CurAbbrevs.push_back(Abbv);
+  }
 };
 
 } // End llvm namespace
 
 #endif
-
-    
\ No newline at end of file