BitcodeReader.h 7.3 KB

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