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/ADT/DenseMap.h"
18 #include "llvm/Bitcode/BitstreamReader.h"
19 #include "llvm/Bitcode/LLVMBitCodes.h"
20 #include "llvm/IR/Attributes.h"
21 #include "llvm/IR/GVMaterializer.h"
22 #include "llvm/IR/OperandTraits.h"
23 #include "llvm/IR/Type.h"
24 #include "llvm/IR/ValueHandle.h"
25 #include <vector>
26 
27 namespace llvm {
28   class MemoryBuffer;
29   class LLVMContext;
30 }
31 
32 namespace llvm_3_0 {
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:
BitcodeReaderValueList(LLVMContext & C)54   BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
~BitcodeReaderValueList()55   ~BitcodeReaderValueList() {
56     assert(ResolveConstants.empty() && "Constants not resolved?");
57   }
58 
59   // vector compatibility methods
size()60   unsigned size() const { return ValuePtrs.size(); }
resize(unsigned N)61   void resize(unsigned N) { ValuePtrs.resize(N); }
push_back(Value * V)62   void push_back(Value *V) {
63     ValuePtrs.push_back(V);
64   }
65 
clear()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 
back()76   Value *back() const { return ValuePtrs.back(); }
pop_back()77     void pop_back() { ValuePtrs.pop_back(); }
empty()78   bool empty() const { return ValuePtrs.empty(); }
shrinkTo(unsigned N)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:
BitcodeReaderMDValueList(LLVMContext & C)104   BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
105 
106   // vector compatibility methods
size()107   unsigned size() const       { return MDValuePtrs.size(); }
resize(unsigned N)108   void resize(unsigned N)     { MDValuePtrs.resize(N); }
push_back(Value * V)109   void push_back(Value *V)    { MDValuePtrs.push_back(V);  }
clear()110   void clear()                { MDValuePtrs.clear();  }
back()111   Value *back() const         { return MDValuePtrs.back(); }
pop_back()112   void pop_back()             { MDValuePtrs.pop_back(); }
empty()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 
shrinkTo(unsigned N)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   std::unique_ptr<BitstreamReader> StreamFile;
135   BitstreamCursor Stream;
136   DataStreamer *LazyStreamer;
137   uint64_t NextUnreadBit;
138   bool SeenValueSymbolTable;
139 
140   std::vector<Type*> TypeList;
141   BitcodeReaderValueList ValueList;
142   BitcodeReaderMDValueList MDValueList;
143   SmallVector<Instruction *, 64> InstructionList;
144 
145   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
146   std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
147 
148   /// MAttributes - The set of attributes by index.  Index zero in the
149   /// file is for null, and is thus not represented here.  As such all indices
150   /// are off by one.
151   std::vector<AttributeSet> MAttributes;
152 
153   /// \brief The set of attribute groups.
154   std::map<unsigned, AttributeSet> MAttributeGroups;
155 
156   /// FunctionBBs - While parsing a function body, this is a list of the basic
157   /// blocks for the function.
158   std::vector<BasicBlock*> FunctionBBs;
159 
160   // When reading the module header, this list is populated with functions that
161   // have bodies later in the file.
162   std::vector<Function*> FunctionsWithBodies;
163 
164   // When intrinsic functions are encountered which require upgrading they are
165   // stored here with their replacement function.
166   typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
167   UpgradedIntrinsicMap UpgradedIntrinsics;
168 
169   // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
170   DenseMap<unsigned, unsigned> MDKindMap;
171 
172   // Several operations happen after the module header has been read, but
173   // before function bodies are processed. This keeps track of whether
174   // we've done this yet.
175   bool SeenFirstFunctionBody;
176 
177   /// DeferredFunctionInfo - When function bodies are initially scanned, this
178   /// map contains info about where to find deferred function body in the
179   /// stream.
180   DenseMap<Function*, uint64_t> DeferredFunctionInfo;
181 
182   /// BlockAddrFwdRefs - These are blockaddr references to basic blocks.  These
183   /// are resolved lazily when functions are loaded.
184   typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
185   DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
186 
187   static const std::error_category &BitcodeErrorCategory();
188 
189 public:
190   enum ErrorType {
191     BitcodeStreamInvalidSize,
192     ConflictingMETADATA_KINDRecords,
193     CouldNotFindFunctionInStream,
194     ExpectedConstant,
195     InsufficientFunctionProtos,
196     InvalidBitcodeSignature,
197     InvalidBitcodeWrapperHeader,
198     InvalidConstantReference,
199     InvalidID, // A read identifier is not found in the table it should be in.
200     InvalidInstructionWithNoBB,
201     InvalidRecord, // A read record doesn't have the expected size or structure
202     InvalidTypeForValue, // Type read OK, but is invalid for its use
203     InvalidTYPETable,
204     InvalidType, // We were unable to read a type
205     MalformedBlock, // We are unable to advance in the stream.
206     MalformedGlobalInitializerSet,
207     InvalidMultipleBlocks, // We found multiple blocks of a kind that should
208                            // have only one
209     NeverResolvedValueFoundInFunction,
210     InvalidValue // Invalid version, inst number, attr number, etc
211   };
212 
Error(ErrorType E)213   std::error_code Error(ErrorType E) {
214     return std::error_code(E, BitcodeErrorCategory());
215   }
216 
BitcodeReader(MemoryBuffer * buffer,LLVMContext & C)217   explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
218     : Context(C), TheModule(0), Buffer(buffer), BufferOwned(false),
219       LazyStreamer(0), NextUnreadBit(0), SeenValueSymbolTable(false),
220       ValueList(C), MDValueList(C),
221       SeenFirstFunctionBody(false) {
222   }
~BitcodeReader()223   ~BitcodeReader() {
224     FreeState();
225   }
226 
227   void FreeState();
228 
229   /// setBufferOwned - If this is true, the reader will destroy the MemoryBuffer
230   /// when the reader is destroyed.
setBufferOwned(bool Owned)231   void setBufferOwned(bool Owned) { BufferOwned = Owned; }
232 
releaseBuffer()233   void releaseBuffer() {
234     Buffer = nullptr;
235   }
236 
237   virtual bool isMaterializable(const GlobalValue *GV) const;
238   virtual bool isDematerializable(const GlobalValue *GV) const;
239   virtual std::error_code Materialize(GlobalValue *GV);
240   virtual std::error_code MaterializeModule(Module *M);
241   virtual void Dematerialize(GlobalValue *GV);
242 
243   /// @brief Main interface to parsing a bitcode buffer.
244   /// @returns true if an error occurred.
245   std::error_code ParseBitcodeInto(Module *M);
246 
247   /// @brief Cheap mechanism to just extract module triple
248   /// @returns true if an error occurred.
249   std::error_code ParseTriple(std::string &Triple);
250 
251   static uint64_t decodeSignRotatedValue(uint64_t V);
252 
253 private:
254   Type *getTypeByID(unsigned ID);
255   Type *getTypeByIDOrNull(unsigned ID);
getFnValueByID(unsigned ID,Type * Ty)256   Value *getFnValueByID(unsigned ID, Type *Ty) {
257     if (Ty && Ty->isMetadataTy())
258       return MDValueList.getValueFwdRef(ID);
259     return ValueList.getValueFwdRef(ID, Ty);
260   }
getBasicBlock(unsigned ID)261   BasicBlock *getBasicBlock(unsigned ID) const {
262     if (ID >= FunctionBBs.size()) return 0; // Invalid ID
263     return FunctionBBs[ID];
264   }
getAttributes(unsigned i)265   AttributeSet getAttributes(unsigned i) const {
266     if (i-1 < MAttributes.size())
267       return MAttributes[i-1];
268     return AttributeSet();
269   }
270 
271   /// getValueTypePair - Read a value/type pair out of the specified record from
272   /// slot 'Slot'.  Increment Slot past the number of slots used in the record.
273   /// Return true on failure.
getValueTypePair(SmallVector<uint64_t,64> & Record,unsigned & Slot,unsigned InstNum,Value * & ResVal)274   bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
275                         unsigned InstNum, Value *&ResVal) {
276     if (Slot == Record.size()) return true;
277     unsigned ValNo = (unsigned)Record[Slot++];
278     if (ValNo < InstNum) {
279       // If this is not a forward reference, just return the value we already
280       // have.
281       ResVal = getFnValueByID(ValNo, 0);
282       return ResVal == 0;
283     } else if (Slot == Record.size()) {
284       return true;
285     }
286 
287     unsigned TypeNo = (unsigned)Record[Slot++];
288     ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
289     return ResVal == 0;
290   }
getValue(SmallVector<uint64_t,64> & Record,unsigned & Slot,Type * Ty,Value * & ResVal)291   bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
292                 Type *Ty, Value *&ResVal) {
293     if (Slot == Record.size()) return true;
294     unsigned ValNo = (unsigned)Record[Slot++];
295     ResVal = getFnValueByID(ValNo, Ty);
296     return ResVal == 0;
297   }
298 
299 
300   std::error_code ParseModule(bool Resume);
301   std::error_code ParseAttributeBlock();
302   std::error_code ParseTypeTable();
303   std::error_code ParseOldTypeTable();         // FIXME: Remove in LLVM 3.1
304   std::error_code ParseTypeTableBody();
305 
306   std::error_code ParseOldTypeSymbolTable();   // FIXME: Remove in LLVM 3.1
307   std::error_code ParseValueSymbolTable();
308   std::error_code ParseConstants();
309   std::error_code RememberAndSkipFunctionBody();
310   std::error_code ParseFunctionBody(Function *F);
311   std::error_code GlobalCleanup();
312   std::error_code ResolveGlobalAndAliasInits();
313   std::error_code ParseMetadata();
314   std::error_code ParseMetadataAttachment();
315   std::error_code ParseModuleTriple(std::string &Triple);
316   std::error_code InitStream();
317   std::error_code InitStreamFromBuffer();
318   std::error_code InitLazyStream();
319 };
320 
321 } // End llvm_3_0 namespace
322 
323 #endif
324