1 //===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This header defines the BitcodeReader class.
12 //===----------------------------------------------------------------------===//
14 #ifndef BITCODE_READER_H
15 #define BITCODE_READER_H
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/Type.h"
19 #include "llvm/User.h"
20 #include "llvm/Bitcode/BitstreamReader.h"
21 #include "llvm/Bitcode/LLVMBitCodes.h"
22 #include "llvm/ADT/DenseMap.h"
28 class BitcodeReaderValueList : public User {
29 std::vector<Use> Uses;
31 BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {}
33 // vector compatibility methods
34 unsigned size() const { return getNumOperands(); }
35 void push_back(Value *V) {
36 Uses.push_back(Use(V, this));
37 OperandList = &Uses[0];
41 Value *operator[](unsigned i) const { return getOperand(i); }
43 Value *back() const { return Uses.back(); }
44 void pop_back() { Uses.pop_back(); --NumOperands; }
45 bool empty() const { return NumOperands == 0; }
46 void shrinkTo(unsigned N) {
47 assert(N <= NumOperands && "Invalid shrinkTo request!");
51 virtual void print(std::ostream&) const {}
53 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
54 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
56 void AssignValue(Value *V, unsigned Idx) {
59 } else if (Value *OldV = getOperand(Idx)) {
60 // If there was a forward reference to this value, replace it.
62 OldV->replaceAllUsesWith(V);
70 void initVal(unsigned Idx, Value *V) {
71 assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
72 Uses[Idx].init(V, this);
77 class BitcodeReader : public ModuleProvider {
79 BitstreamReader Stream;
81 const char *ErrorString;
83 std::vector<PATypeHolder> TypeList;
84 BitcodeReaderValueList ValueList;
85 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
86 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
88 /// FunctionBBs - While parsing a function body, this is a list of the basic
89 /// blocks for the function.
90 std::vector<BasicBlock*> FunctionBBs;
92 // When reading the module header, this list is populated with functions that
93 // have bodies later in the file.
94 std::vector<Function*> FunctionsWithBodies;
96 // After the module header has been read, the FunctionsWithBodies list is
97 // reversed. This keeps track of whether we've done this yet.
98 bool HasReversedFunctionsWithBodies;
100 /// DeferredFunctionInfo - When function bodies are initially scanned, this
101 /// map contains info about where to find deferred function body (in the
102 /// stream) and what linkage the original function had.
103 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
105 BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {
106 HasReversedFunctionsWithBodies = false;
111 /// releaseMemoryBuffer - This causes the reader to completely forget about
112 /// the memory buffer it contains, which prevents the buffer from being
113 /// destroyed when it is deleted.
114 void releaseMemoryBuffer() {
118 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
119 virtual Module *materializeModule(std::string *ErrInfo = 0);
121 bool Error(const char *Str) {
125 const char *getErrorString() const { return ErrorString; }
127 /// @brief Main interface to parsing a bitcode buffer.
128 /// @returns true if an error occurred.
131 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
132 Value *getFnValueByID(unsigned ID, const Type *Ty) {
133 return ValueList.getValueFwdRef(ID, Ty);
135 BasicBlock *getBasicBlock(unsigned ID) const {
136 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
137 return FunctionBBs[ID];
140 bool ParseModule(const std::string &ModuleID);
141 bool ParseTypeTable();
142 bool ParseTypeSymbolTable();
143 bool ParseValueSymbolTable();
144 bool ParseConstants();
145 bool RememberAndSkipFunctionBody();
146 bool ParseFunctionBody(Function *F);
147 bool ResolveGlobalAndAliasInits();
150 } // End llvm namespace