Home | History | Annotate | Download | only in BitReader_2_7
      1 //===- BitcodeReader.h - Internal BitcodeReader impl ------------*- 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 defines the BitcodeReader class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef BITCODE_READER_H
     15 #define BITCODE_READER_H
     16 
     17 #include "llvm/GVMaterializer.h"
     18 #include "llvm/Attributes.h"
     19 #include "llvm/Type.h"
     20 #include "llvm/OperandTraits.h"
     21 #include "llvm/Bitcode/BitstreamReader.h"
     22 #include "llvm/Bitcode/LLVMBitCodes.h"
     23 #include "llvm/Support/ValueHandle.h"
     24 #include "llvm/ADT/DenseMap.h"
     25 #include <vector>
     26 
     27 namespace llvm {
     28   class MemoryBuffer;
     29   class LLVMContext;
     30 }
     31 
     32 namespace llvm_2_7 {
     33 
     34 using namespace llvm;
     35 
     36 //===----------------------------------------------------------------------===//
     37 //                          BitcodeReaderValueList Class
     38 //===----------------------------------------------------------------------===//
     39 
     40 class BitcodeReaderValueList {
     41   std::vector<WeakVH> ValuePtrs;
     42 
     43   /// ResolveConstants - As we resolve forward-referenced constants, we add
     44   /// information about them to this vector.  This allows us to resolve them in
     45   /// bulk instead of resolving each reference at a time.  See the code in
     46   /// ResolveConstantForwardRefs for more information about this.
     47   ///
     48   /// The key of this vector is the placeholder constant, the value is the slot
     49   /// number that holds the resolved value.
     50   typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
     51   ResolveConstantsTy ResolveConstants;
     52   LLVMContext &Context;
     53 public:
     54   BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
     55   ~BitcodeReaderValueList() {
     56     assert(ResolveConstants.empty() && "Constants not resolved?");
     57   }
     58 
     59   // vector compatibility methods
     60   unsigned size() const { return ValuePtrs.size(); }
     61   void resize(unsigned N) { ValuePtrs.resize(N); }
     62   void push_back(Value *V) {
     63     ValuePtrs.push_back(V);
     64   }
     65 
     66   void clear() {
     67     assert(ResolveConstants.empty() && "Constants not resolved?");
     68     ValuePtrs.clear();
     69   }
     70 
     71   Value *operator[](unsigned i) const {
     72     assert(i < ValuePtrs.size());
     73     return ValuePtrs[i];
     74   }
     75 
     76   Value *back() const { return ValuePtrs.back(); }
     77     void pop_back() { ValuePtrs.pop_back(); }
     78   bool empty() const { return ValuePtrs.empty(); }
     79   void shrinkTo(unsigned N) {
     80     assert(N <= size() && "Invalid shrinkTo request!");
     81     ValuePtrs.resize(N);
     82   }
     83 
     84   Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
     85   Value *getValueFwdRef(unsigned Idx, Type *Ty);
     86 
     87   void AssignValue(Value *V, unsigned Idx);
     88 
     89   /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
     90   /// resolves any forward references.
     91   void ResolveConstantForwardRefs();
     92 };
     93 
     94 
     95 //===----------------------------------------------------------------------===//
     96 //                          BitcodeReaderMDValueList Class
     97 //===----------------------------------------------------------------------===//
     98 
     99 class BitcodeReaderMDValueList {
    100   std::vector<WeakVH> MDValuePtrs;
    101 
    102   LLVMContext &Context;
    103 public:
    104   BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
    105 
    106   // vector compatibility methods
    107   unsigned size() const       { return MDValuePtrs.size(); }
    108   void resize(unsigned N)     { MDValuePtrs.resize(N); }
    109   void push_back(Value *V)    { MDValuePtrs.push_back(V);  }
    110   void clear()                { MDValuePtrs.clear();  }
    111   Value *back() const         { return MDValuePtrs.back(); }
    112   void pop_back()             { MDValuePtrs.pop_back(); }
    113   bool empty() const          { return MDValuePtrs.empty(); }
    114 
    115   Value *operator[](unsigned i) const {
    116     assert(i < MDValuePtrs.size());
    117     return MDValuePtrs[i];
    118   }
    119 
    120   void shrinkTo(unsigned N) {
    121     assert(N <= size() && "Invalid shrinkTo request!");
    122     MDValuePtrs.resize(N);
    123   }
    124 
    125   Value *getValueFwdRef(unsigned Idx);
    126   void AssignValue(Value *V, unsigned Idx);
    127 };
    128 
    129 class BitcodeReader : public GVMaterializer {
    130   LLVMContext &Context;
    131   Module *TheModule;
    132   MemoryBuffer *Buffer;
    133   bool BufferOwned;
    134   BitstreamReader StreamFile;
    135   BitstreamCursor Stream;
    136 
    137   const char *ErrorString;
    138 
    139   std::vector<Type*> TypeList;
    140   BitcodeReaderValueList ValueList;
    141   BitcodeReaderMDValueList MDValueList;
    142   SmallVector<Instruction *, 64> InstructionList;
    143 
    144   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
    145   std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
    146 
    147   /// MAttributes - The set of attributes by index.  Index zero in the
    148   /// file is for null, and is thus not represented here.  As such all indices
    149   /// are off by one.
    150   std::vector<AttrListPtr> MAttributes;
    151 
    152   /// FunctionBBs - While parsing a function body, this is a list of the basic
    153   /// blocks for the function.
    154   std::vector<BasicBlock*> FunctionBBs;
    155 
    156   // When reading the module header, this list is populated with functions that
    157   // have bodies later in the file.
    158   std::vector<Function*> FunctionsWithBodies;
    159 
    160   // When intrinsic functions are encountered which require upgrading they are
    161   // stored here with their replacement function.
    162   typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
    163   UpgradedIntrinsicMap UpgradedIntrinsics;
    164 
    165   // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
    166   DenseMap<unsigned, unsigned> MDKindMap;
    167 
    168   // After the module header has been read, the FunctionsWithBodies list is
    169   // reversed.  This keeps track of whether we've done this yet.
    170   bool HasReversedFunctionsWithBodies;
    171 
    172   /// DeferredFunctionInfo - When function bodies are initially scanned, this
    173   /// map contains info about where to find deferred function body in the
    174   /// stream.
    175   DenseMap<Function*, uint64_t> DeferredFunctionInfo;
    176 
    177   /// BlockAddrFwdRefs - These are blockaddr references to basic blocks.  These
    178   /// are resolved lazily when functions are loaded.
    179   typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
    180   DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
    181 
    182   /// LLVM2_7MetadataDetected - True if metadata produced by LLVM 2.7 or
    183   /// earlier was detected, in which case we behave slightly differently,
    184   /// for compatibility.
    185   /// FIXME: Remove in LLVM 3.0.
    186   bool LLVM2_7MetadataDetected;
    187 
    188 public:
    189   explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
    190     : Context(C), TheModule(0), Buffer(buffer), BufferOwned(false),
    191       ErrorString(0), ValueList(C), MDValueList(C),
    192       LLVM2_7MetadataDetected(false) {
    193     HasReversedFunctionsWithBodies = false;
    194   }
    195   ~BitcodeReader() {
    196     FreeState();
    197   }
    198 
    199   void FreeState();
    200 
    201   /// setBufferOwned - If this is true, the reader will destroy the MemoryBuffer
    202   /// when the reader is destroyed.
    203   void setBufferOwned(bool Owned) { BufferOwned = Owned; }
    204 
    205   virtual bool isMaterializable(const GlobalValue *GV) const;
    206   virtual bool isDematerializable(const GlobalValue *GV) const;
    207   virtual bool Materialize(GlobalValue *GV, std::string *ErrInfo = 0);
    208   virtual bool MaterializeModule(Module *M, std::string *ErrInfo = 0);
    209   virtual void Dematerialize(GlobalValue *GV);
    210 
    211   bool Error(const char *Str) {
    212     ErrorString = Str;
    213     return true;
    214   }
    215   const char *getErrorString() const { return ErrorString; }
    216 
    217   /// @brief Main interface to parsing a bitcode buffer.
    218   /// @returns true if an error occurred.
    219   bool ParseBitcodeInto(Module *M);
    220 
    221   /// @brief Cheap mechanism to just extract module triple
    222   /// @returns true if an error occurred.
    223   bool ParseTriple(std::string &Triple);
    224 private:
    225   Type *getTypeByID(unsigned ID);
    226   Type *getTypeByIDOrNull(unsigned ID);
    227   Value *getFnValueByID(unsigned ID, Type *Ty) {
    228     if (Ty && Ty->isMetadataTy())
    229       return MDValueList.getValueFwdRef(ID);
    230     return ValueList.getValueFwdRef(ID, Ty);
    231   }
    232   BasicBlock *getBasicBlock(unsigned ID) const {
    233     if (ID >= FunctionBBs.size()) return 0; // Invalid ID
    234     return FunctionBBs[ID];
    235   }
    236   AttrListPtr getAttributes(unsigned i) const {
    237     if (i-1 < MAttributes.size())
    238       return MAttributes[i-1];
    239     return AttrListPtr();
    240   }
    241 
    242   /// getValueTypePair - Read a value/type pair out of the specified record from
    243   /// slot 'Slot'.  Increment Slot past the number of slots used in the record.
    244   /// Return true on failure.
    245   bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
    246                         unsigned InstNum, Value *&ResVal) {
    247     if (Slot == Record.size()) return true;
    248     unsigned ValNo = (unsigned)Record[Slot++];
    249     if (ValNo < InstNum) {
    250       // If this is not a forward reference, just return the value we already
    251       // have.
    252       ResVal = getFnValueByID(ValNo, 0);
    253       return ResVal == 0;
    254     } else if (Slot == Record.size()) {
    255       return true;
    256     }
    257 
    258     unsigned TypeNo = (unsigned)Record[Slot++];
    259     ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
    260     return ResVal == 0;
    261   }
    262   bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
    263                 Type *Ty, Value *&ResVal) {
    264     if (Slot == Record.size()) return true;
    265     unsigned ValNo = (unsigned)Record[Slot++];
    266     ResVal = getFnValueByID(ValNo, Ty);
    267     return ResVal == 0;
    268   }
    269 
    270 
    271   bool ParseModule();
    272   bool ParseAttributeBlock();
    273   bool ParseTypeTable();
    274   bool ParseOldTypeTable();         // FIXME: Remove in LLVM 3.1
    275   bool ParseTypeTableBody();
    276 
    277   bool ParseOldTypeSymbolTable();   // FIXME: Remove in LLVM 3.1
    278   bool ParseValueSymbolTable();
    279   bool ParseConstants();
    280   bool RememberAndSkipFunctionBody();
    281   bool ParseFunctionBody(Function *F);
    282   bool ResolveGlobalAndAliasInits();
    283   bool ParseMetadata();
    284   bool ParseMetadataAttachment();
    285   bool ParseModuleTriple(std::string &Triple);
    286 };
    287 
    288 } // End llvm namespace
    289 
    290 #endif
    291