BitcodeReader.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/Attributes.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/Support/ValueHandle.h"
  22. #include "llvm/ADT/DenseMap.h"
  23. #include <vector>
  24. namespace llvm {
  25. class MemoryBuffer;
  26. class LLVMContext;
  27. //===----------------------------------------------------------------------===//
  28. // BitcodeReaderValueList Class
  29. //===----------------------------------------------------------------------===//
  30. class BitcodeReaderValueList {
  31. std::vector<WeakVH> ValuePtrs;
  32. /// ResolveConstants - As we resolve forward-referenced constants, we add
  33. /// information about them to this vector. This allows us to resolve them in
  34. /// bulk instead of resolving each reference at a time. See the code in
  35. /// ResolveConstantForwardRefs for more information about this.
  36. ///
  37. /// The key of this vector is the placeholder constant, the value is the slot
  38. /// number that holds the resolved value.
  39. typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
  40. ResolveConstantsTy ResolveConstants;
  41. LLVMContext& Context;
  42. public:
  43. BitcodeReaderValueList(LLVMContext& C) : Context(C) {}
  44. ~BitcodeReaderValueList() {
  45. assert(ResolveConstants.empty() && "Constants not resolved?");
  46. }
  47. // vector compatibility methods
  48. unsigned size() const { return ValuePtrs.size(); }
  49. void resize(unsigned N) { ValuePtrs.resize(N); }
  50. void push_back(Value *V) {
  51. ValuePtrs.push_back(V);
  52. }
  53. void clear() {
  54. assert(ResolveConstants.empty() && "Constants not resolved?");
  55. ValuePtrs.clear();
  56. }
  57. Value *operator[](unsigned i) const {
  58. assert(i < ValuePtrs.size());
  59. return ValuePtrs[i];
  60. }
  61. Value *back() const { return ValuePtrs.back(); }
  62. void pop_back() { ValuePtrs.pop_back(); }
  63. bool empty() const { return ValuePtrs.empty(); }
  64. void shrinkTo(unsigned N) {
  65. assert(N <= size() && "Invalid shrinkTo request!");
  66. ValuePtrs.resize(N);
  67. }
  68. Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
  69. Value *getValueFwdRef(unsigned Idx, const Type *Ty);
  70. void AssignValue(Value *V, unsigned Idx);
  71. /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
  72. /// resolves any forward references.
  73. void ResolveConstantForwardRefs();
  74. };
  75. //===----------------------------------------------------------------------===//
  76. // BitcodeReaderMDValueList Class
  77. //===----------------------------------------------------------------------===//
  78. class BitcodeReaderMDValueList {
  79. std::vector<WeakVH> MDValuePtrs;
  80. LLVMContext& Context;
  81. public:
  82. BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
  83. // vector compatibility methods
  84. unsigned size() const { return MDValuePtrs.size(); }
  85. void resize(unsigned N) { MDValuePtrs.resize(N); }
  86. void push_back(Value *V) { MDValuePtrs.push_back(V); }
  87. void clear() { MDValuePtrs.clear(); }
  88. Value *back() const { return MDValuePtrs.back(); }
  89. void pop_back() { MDValuePtrs.pop_back(); }
  90. bool empty() const { return MDValuePtrs.empty(); }
  91. Value *operator[](unsigned i) const {
  92. assert(i < MDValuePtrs.size());
  93. return MDValuePtrs[i];
  94. }
  95. void shrinkTo(unsigned N) {
  96. assert(N <= size() && "Invalid shrinkTo request!");
  97. MDValuePtrs.resize(N);
  98. }
  99. Value *getValueFwdRef(unsigned Idx);
  100. void AssignValue(Value *V, unsigned Idx);
  101. };
  102. class BitcodeReader : public ModuleProvider {
  103. LLVMContext& Context;
  104. MemoryBuffer *Buffer;
  105. BitstreamReader StreamFile;
  106. BitstreamCursor Stream;
  107. const char *ErrorString;
  108. std::vector<PATypeHolder> TypeList;
  109. BitcodeReaderValueList ValueList;
  110. BitcodeReaderMDValueList MDValueList;
  111. std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
  112. std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
  113. /// MAttributes - The set of attributes by index. Index zero in the
  114. /// file is for null, and is thus not represented here. As such all indices
  115. /// are off by one.
  116. std::vector<AttrListPtr> MAttributes;
  117. /// FunctionBBs - While parsing a function body, this is a list of the basic
  118. /// blocks for the function.
  119. std::vector<BasicBlock*> FunctionBBs;
  120. // When reading the module header, this list is populated with functions that
  121. // have bodies later in the file.
  122. std::vector<Function*> FunctionsWithBodies;
  123. // When intrinsic functions are encountered which require upgrading they are
  124. // stored here with their replacement function.
  125. typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
  126. UpgradedIntrinsicMap UpgradedIntrinsics;
  127. // After the module header has been read, the FunctionsWithBodies list is
  128. // reversed. This keeps track of whether we've done this yet.
  129. bool HasReversedFunctionsWithBodies;
  130. /// DeferredFunctionInfo - When function bodies are initially scanned, this
  131. /// map contains info about where to find deferred function body (in the
  132. /// stream) and what linkage the original function had.
  133. DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
  134. public:
  135. explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext& C)
  136. : Context(C), Buffer(buffer), ErrorString(0), ValueList(C), MDValueList(C) {
  137. HasReversedFunctionsWithBodies = false;
  138. }
  139. ~BitcodeReader() {
  140. FreeState();
  141. }
  142. void FreeState();
  143. /// releaseMemoryBuffer - This causes the reader to completely forget about
  144. /// the memory buffer it contains, which prevents the buffer from being
  145. /// destroyed when it is deleted.
  146. void releaseMemoryBuffer() {
  147. Buffer = 0;
  148. }
  149. virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
  150. virtual Module *materializeModule(std::string *ErrInfo = 0);
  151. virtual void dematerializeFunction(Function *F);
  152. virtual Module *releaseModule(std::string *ErrInfo = 0);
  153. bool Error(const char *Str) {
  154. ErrorString = Str;
  155. return true;
  156. }
  157. const char *getErrorString() const { return ErrorString; }
  158. /// @brief Main interface to parsing a bitcode buffer.
  159. /// @returns true if an error occurred.
  160. bool ParseBitcode();
  161. private:
  162. const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
  163. Value *getFnValueByID(unsigned ID, const Type *Ty) {
  164. if (Ty == Type::getMetadataTy(Context))
  165. return MDValueList.getValueFwdRef(ID);
  166. else
  167. return ValueList.getValueFwdRef(ID, Ty);
  168. }
  169. BasicBlock *getBasicBlock(unsigned ID) const {
  170. if (ID >= FunctionBBs.size()) return 0; // Invalid ID
  171. return FunctionBBs[ID];
  172. }
  173. AttrListPtr getAttributes(unsigned i) const {
  174. if (i-1 < MAttributes.size())
  175. return MAttributes[i-1];
  176. return AttrListPtr();
  177. }
  178. /// getValueTypePair - Read a value/type pair out of the specified record from
  179. /// slot 'Slot'. Increment Slot past the number of slots used in the record.
  180. /// Return true on failure.
  181. bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
  182. unsigned InstNum, Value *&ResVal) {
  183. if (Slot == Record.size()) return true;
  184. unsigned ValNo = (unsigned)Record[Slot++];
  185. if (ValNo < InstNum) {
  186. // If this is not a forward reference, just return the value we already
  187. // have.
  188. ResVal = getFnValueByID(ValNo, 0);
  189. return ResVal == 0;
  190. } else if (Slot == Record.size()) {
  191. return true;
  192. }
  193. unsigned TypeNo = (unsigned)Record[Slot++];
  194. ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
  195. return ResVal == 0;
  196. }
  197. bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
  198. const Type *Ty, Value *&ResVal) {
  199. if (Slot == Record.size()) return true;
  200. unsigned ValNo = (unsigned)Record[Slot++];
  201. ResVal = getFnValueByID(ValNo, Ty);
  202. return ResVal == 0;
  203. }
  204. bool ParseModule(const std::string &ModuleID);
  205. bool ParseAttributeBlock();
  206. bool ParseTypeTable();
  207. bool ParseTypeSymbolTable();
  208. bool ParseValueSymbolTable();
  209. bool ParseConstants();
  210. bool RememberAndSkipFunctionBody();
  211. bool ParseFunctionBody(Function *F);
  212. bool ResolveGlobalAndAliasInits();
  213. bool ParseMetadata();
  214. };
  215. } // End llvm namespace
  216. #endif