BitcodeReader.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file was developed by Chris Lattner and is distributed under
  6. // the University of Illinois Open Source License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This header defines the BitcodeReader class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef BITCODE_READER_H
  14. #define BITCODE_READER_H
  15. #include "llvm/ModuleProvider.h"
  16. #include "llvm/Type.h"
  17. #include "llvm/User.h"
  18. #include "llvm/Bitcode/BitstreamReader.h"
  19. #include "llvm/Bitcode/LLVMBitCodes.h"
  20. #include "llvm/ADT/DenseMap.h"
  21. #include <vector>
  22. namespace llvm {
  23. class MemoryBuffer;
  24. class ParamAttrsList;
  25. class BitcodeReaderValueList : public User {
  26. std::vector<Use> Uses;
  27. public:
  28. BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {}
  29. // vector compatibility methods
  30. unsigned size() const { return getNumOperands(); }
  31. void push_back(Value *V) {
  32. Uses.push_back(Use(V, this));
  33. OperandList = &Uses[0];
  34. ++NumOperands;
  35. }
  36. Value *operator[](unsigned i) const { return getOperand(i); }
  37. Value *back() const { return Uses.back(); }
  38. void pop_back() { Uses.pop_back(); --NumOperands; }
  39. bool empty() const { return NumOperands == 0; }
  40. void shrinkTo(unsigned N) {
  41. assert(N <= NumOperands && "Invalid shrinkTo request!");
  42. Uses.resize(N);
  43. NumOperands = N;
  44. }
  45. virtual void print(std::ostream&) const {}
  46. Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
  47. Value *getValueFwdRef(unsigned Idx, const Type *Ty);
  48. void AssignValue(Value *V, unsigned Idx) {
  49. if (Idx == size()) {
  50. push_back(V);
  51. } else if (Value *OldV = getOperand(Idx)) {
  52. // If there was a forward reference to this value, replace it.
  53. setOperand(Idx, V);
  54. OldV->replaceAllUsesWith(V);
  55. delete OldV;
  56. } else {
  57. initVal(Idx, V);
  58. }
  59. }
  60. private:
  61. void initVal(unsigned Idx, Value *V) {
  62. assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
  63. Uses[Idx].init(V, this);
  64. }
  65. };
  66. class BitcodeReader : public ModuleProvider {
  67. MemoryBuffer *Buffer;
  68. BitstreamReader Stream;
  69. const char *ErrorString;
  70. std::vector<PATypeHolder> TypeList;
  71. BitcodeReaderValueList ValueList;
  72. std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
  73. std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
  74. /// ParamAttrs - The set of parameter attributes by index. Index zero in the
  75. /// file is for null, and is thus not represented here. As such all indices
  76. /// are off by one.
  77. std::vector<const ParamAttrsList*> ParamAttrs;
  78. /// FunctionBBs - While parsing a function body, this is a list of the basic
  79. /// blocks for the function.
  80. std::vector<BasicBlock*> FunctionBBs;
  81. // When reading the module header, this list is populated with functions that
  82. // have bodies later in the file.
  83. std::vector<Function*> FunctionsWithBodies;
  84. // After the module header has been read, the FunctionsWithBodies list is
  85. // reversed. This keeps track of whether we've done this yet.
  86. bool HasReversedFunctionsWithBodies;
  87. /// DeferredFunctionInfo - When function bodies are initially scanned, this
  88. /// map contains info about where to find deferred function body (in the
  89. /// stream) and what linkage the original function had.
  90. DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
  91. public:
  92. BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {
  93. HasReversedFunctionsWithBodies = false;
  94. }
  95. ~BitcodeReader();
  96. /// releaseMemoryBuffer - This causes the reader to completely forget about
  97. /// the memory buffer it contains, which prevents the buffer from being
  98. /// destroyed when it is deleted.
  99. void releaseMemoryBuffer() {
  100. Buffer = 0;
  101. }
  102. virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
  103. virtual Module *materializeModule(std::string *ErrInfo = 0);
  104. bool Error(const char *Str) {
  105. ErrorString = Str;
  106. return true;
  107. }
  108. const char *getErrorString() const { return ErrorString; }
  109. /// @brief Main interface to parsing a bitcode buffer.
  110. /// @returns true if an error occurred.
  111. bool ParseBitcode();
  112. private:
  113. const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
  114. Value *getFnValueByID(unsigned ID, const Type *Ty) {
  115. return ValueList.getValueFwdRef(ID, Ty);
  116. }
  117. BasicBlock *getBasicBlock(unsigned ID) const {
  118. if (ID >= FunctionBBs.size()) return 0; // Invalid ID
  119. return FunctionBBs[ID];
  120. }
  121. const ParamAttrsList *getParamAttrs(unsigned i) const {
  122. if (i-1 < ParamAttrs.size())
  123. return ParamAttrs[i-1];
  124. return 0;
  125. }
  126. /// getValueTypePair - Read a value/type pair out of the specified record from
  127. /// slot 'Slot'. Increment Slot past the number of slots used in the record.
  128. /// Return true on failure.
  129. bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
  130. unsigned InstNum, Value *&ResVal) {
  131. if (Slot == Record.size()) return true;
  132. unsigned ValNo = (unsigned)Record[Slot++];
  133. if (ValNo < InstNum) {
  134. // If this is not a forward reference, just return the value we already
  135. // have.
  136. ResVal = getFnValueByID(ValNo, 0);
  137. return ResVal == 0;
  138. } else if (Slot == Record.size()) {
  139. return true;
  140. }
  141. unsigned TypeNo = (unsigned)Record[Slot++];
  142. ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
  143. return ResVal == 0;
  144. }
  145. bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
  146. const Type *Ty, Value *&ResVal) {
  147. if (Slot == Record.size()) return true;
  148. unsigned ValNo = (unsigned)Record[Slot++];
  149. ResVal = getFnValueByID(ValNo, Ty);
  150. return ResVal == 0;
  151. }
  152. bool ParseModule(const std::string &ModuleID);
  153. bool ParseParamAttrBlock();
  154. bool ParseTypeTable();
  155. bool ParseTypeSymbolTable();
  156. bool ParseValueSymbolTable();
  157. bool ParseConstants();
  158. bool RememberAndSkipFunctionBody();
  159. bool ParseFunctionBody(Function *F);
  160. bool ResolveGlobalAndAliasInits();
  161. };
  162. } // End llvm namespace
  163. #endif