For PR1187:
[oota-llvm.git] / lib / Bytecode / Reader / Reader.h
1 //===-- Reader.h - Interface To Bytecode Reading ----------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This header file defines the interface to the Bytecode Reader which is
11 //  responsible for correctly interpreting bytecode files (backwards compatible)
12 //  and materializing a module from the bytecode read.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef BYTECODE_PARSER_H
17 #define BYTECODE_PARSER_H
18
19 #include "llvm/Constants.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/GlobalValue.h"
22 #include "llvm/Function.h"
23 #include "llvm/ModuleProvider.h"
24 #include "llvm/Bytecode/Analyzer.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include <utility>
27 #include <setjmp.h>
28
29 namespace llvm {
30
31 // Forward declarations
32 class BytecodeHandler; 
33 class TypeSymbolTable; 
34 class ValueSymbolTable; 
35
36 /// This class defines the interface for parsing a buffer of bytecode. The
37 /// parser itself takes no action except to call the various functions of
38 /// the handler interface. The parser's sole responsibility is the correct
39 /// interpretation of the bytecode buffer. The handler is responsible for
40 /// instantiating and keeping track of all values. As a convenience, the parser
41 /// is responsible for materializing types and will pass them through the
42 /// handler interface as necessary.
43 /// @see BytecodeHandler
44 /// @brief Bytecode Reader interface
45 class BytecodeReader : public ModuleProvider {
46
47 /// @name Constructors
48 /// @{
49 public:
50   /// @brief Default constructor. By default, no handler is used.
51   BytecodeReader(BytecodeHandler* h = 0) {
52     decompressedBlock = 0;
53     Handler = h;
54   }
55
56   ~BytecodeReader() {
57     freeState();
58     if (decompressedBlock) {
59       ::free(decompressedBlock);
60       decompressedBlock = 0;
61     }
62   }
63
64 /// @}
65 /// @name Types
66 /// @{
67 public:
68
69   /// @brief A convenience type for the buffer pointer
70   typedef const unsigned char* BufPtr;
71
72   /// @brief The type used for a vector of potentially abstract types
73   typedef std::vector<PATypeHolder> TypeListTy;
74
75   /// This type provides a vector of Value* via the User class for
76   /// storage of Values that have been constructed when reading the
77   /// bytecode. Because of forward referencing, constant replacement
78   /// can occur so we ensure that our list of Value* is updated
79   /// properly through those transitions. This ensures that the
80   /// correct Value* is in our list when it comes time to associate
81   /// constants with global variables at the end of reading the
82   /// globals section.
83   /// @brief A list of values as a User of those Values.
84   class ValueList : public User {
85     std::vector<Use> Uses;
86   public:
87     ValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {}
88
89     // vector compatibility methods
90     unsigned size() const { return getNumOperands(); }
91     void push_back(Value *V) {
92       Uses.push_back(Use(V, this));
93       OperandList = &Uses[0];
94       ++NumOperands;
95     }
96     Value *back() const { return Uses.back(); }
97     void pop_back() { Uses.pop_back(); --NumOperands; }
98     bool empty() const { return NumOperands == 0; }
99     virtual void print(std::ostream& os) const {
100       for (unsigned i = 0; i < size(); ++i) {
101         os << i << " ";
102         getOperand(i)->print(os);
103         os << "\n";
104       }
105     }
106   };
107
108   /// @brief A 2 dimensional table of values
109   typedef std::vector<ValueList*> ValueTable;
110
111   /// This map is needed so that forward references to constants can be looked
112   /// up by Type and slot number when resolving those references.
113   /// @brief A mapping of a Type/slot pair to a Constant*.
114   typedef std::map<std::pair<unsigned,unsigned>, Constant*> ConstantRefsType;
115
116   /// For lazy read-in of functions, we need to save the location in the
117   /// data stream where the function is located. This structure provides that
118   /// information. Lazy read-in is used mostly by the JIT which only wants to
119   /// resolve functions as it needs them.
120   /// @brief Keeps pointers to function contents for later use.
121   struct LazyFunctionInfo {
122     const unsigned char *Buf, *EndBuf;
123     LazyFunctionInfo(const unsigned char *B = 0, const unsigned char *EB = 0)
124       : Buf(B), EndBuf(EB) {}
125   };
126
127   /// @brief A mapping of functions to their LazyFunctionInfo for lazy reading.
128   typedef std::map<Function*, LazyFunctionInfo> LazyFunctionMap;
129
130   /// @brief A list of global variables and the slot number that initializes
131   /// them.
132   typedef std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitsList;
133
134   /// This type maps a typeslot/valueslot pair to the corresponding Value*.
135   /// It is used for dealing with forward references as values are read in.
136   /// @brief A map for dealing with forward references of values.
137   typedef std::map<std::pair<unsigned,unsigned>,Value*> ForwardReferenceMap;
138
139 /// @}
140 /// @name Methods
141 /// @{
142 public:
143   typedef size_t BCDecompressor_t(const char *, size_t, char*&, std::string*);
144
145   /// @returns true if an error occurred
146   /// @brief Main interface to parsing a bytecode buffer.
147   bool ParseBytecode(
148      volatile BufPtr Buf,         ///< Beginning of the bytecode buffer
149      unsigned Length,             ///< Length of the bytecode buffer
150      const std::string &ModuleID, ///< An identifier for the module constructed.
151      BCDecompressor_t *Decompressor = 0, ///< Optional decompressor.
152      std::string* ErrMsg = 0      ///< Optional place for error message 
153   );
154
155   /// @brief Parse all function bodies
156   bool ParseAllFunctionBodies(std::string* ErrMsg);
157
158   /// @brief Parse the next function of specific type
159   bool ParseFunction(Function* Func, std::string* ErrMsg) ;
160
161   /// This method is abstract in the parent ModuleProvider class. Its
162   /// implementation is identical to the ParseFunction method.
163   /// @see ParseFunction
164   /// @brief Make a specific function materialize.
165   virtual bool materializeFunction(Function *F, std::string *ErrMsg = 0) {
166     LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.find(F);
167     if (Fi == LazyFunctionLoadMap.end()) 
168       return false;
169     if (ParseFunction(F,ErrMsg))
170       return true;
171     return false;
172   }
173
174   /// This method is abstract in the parent ModuleProvider class. Its
175   /// implementation is identical to ParseAllFunctionBodies.
176   /// @see ParseAllFunctionBodies
177   /// @brief Make the whole module materialize
178   virtual Module* materializeModule(std::string *ErrMsg = 0) {
179     if (ParseAllFunctionBodies(ErrMsg))
180       return 0;
181     return TheModule;
182   }
183
184   /// This method is provided by the parent ModuleProvde class and overriden
185   /// here. It simply releases the module from its provided and frees up our
186   /// state.
187   /// @brief Release our hold on the generated module
188   Module* releaseModule(std::string *ErrInfo = 0) {
189     // Since we're losing control of this Module, we must hand it back complete
190     Module *M = ModuleProvider::releaseModule(ErrInfo);
191     freeState();
192     return M;
193   }
194
195 /// @}
196 /// @name Parsing Units For Subclasses
197 /// @{
198 protected:
199   /// @brief Parse whole module scope
200   void ParseModule();
201
202   /// @brief Parse the version information block
203   void ParseVersionInfo();
204
205   /// @brief Parse the ModuleGlobalInfo block
206   void ParseModuleGlobalInfo();
207
208   /// @brief Parse a value symbol table
209   void ParseTypeSymbolTable(TypeSymbolTable *ST);
210
211   /// @brief Parse a value symbol table
212   void ParseValueSymbolTable(Function* Func, ValueSymbolTable *ST);
213
214   /// @brief Parse functions lazily.
215   void ParseFunctionLazily();
216
217   ///  @brief Parse a function body
218   void ParseFunctionBody(Function* Func);
219
220   /// @brief Parse global types
221   void ParseGlobalTypes();
222
223   /// @brief Parse a basic block (for LLVM 1.0 basic block blocks)
224   BasicBlock* ParseBasicBlock(unsigned BlockNo);
225
226   /// @brief parse an instruction list (for post LLVM 1.0 instruction lists
227   /// with blocks differentiated by terminating instructions.
228   unsigned ParseInstructionList(
229     Function* F   ///< The function into which BBs will be inserted
230   );
231
232   /// @brief Parse a single instruction.
233   void ParseInstruction(
234     SmallVector <unsigned, 8>& Args,   ///< The arguments to be filled in
235     BasicBlock* BB             ///< The BB the instruction goes in
236   );
237
238   /// @brief Parse the whole constant pool
239   void ParseConstantPool(ValueTable& Values, TypeListTy& Types,
240                          bool isFunction);
241
242   /// @brief Parse a single constant pool value
243   Value *ParseConstantPoolValue(unsigned TypeID);
244
245   /// @brief Parse a block of types constants
246   void ParseTypes(TypeListTy &Tab, unsigned NumEntries);
247
248   /// @brief Parse a single type constant
249   const Type *ParseType();
250
251   /// @brief Parse a string constants block
252   void ParseStringConstants(unsigned NumEntries, ValueTable &Tab);
253
254   /// @brief Release our memory.
255   void freeState() {
256     freeTable(FunctionValues);
257     freeTable(ModuleValues);
258   }
259   
260 /// @}
261 /// @name Data
262 /// @{
263 private:
264   std::string ErrorMsg; ///< A place to hold an error message through longjmp
265   jmp_buf context;      ///< Where to return to if an error occurs.
266   char*  decompressedBlock; ///< Result of decompression
267   BufPtr MemStart;     ///< Start of the memory buffer
268   BufPtr MemEnd;       ///< End of the memory buffer
269   BufPtr BlockStart;   ///< Start of current block being parsed
270   BufPtr BlockEnd;     ///< End of current block being parsed
271   BufPtr At;           ///< Where we're currently parsing at
272
273   /// Information about the module, extracted from the bytecode revision number.
274   ///
275   unsigned char RevisionNum;        // The rev # itself
276
277   /// @brief This vector is used to deal with forward references to types in
278   /// a module.
279   TypeListTy ModuleTypes;
280   
281   /// @brief This is an inverse mapping of ModuleTypes from the type to an
282   /// index.  Because refining types causes the index of this map to be
283   /// invalidated, any time we refine a type, we clear this cache and recompute
284   /// it next time we need it.  These entries are ordered by the pointer value.
285   std::vector<std::pair<const Type*, unsigned> > ModuleTypeIDCache;
286
287   /// @brief This vector is used to deal with forward references to types in
288   /// a function.
289   TypeListTy FunctionTypes;
290
291   /// When the ModuleGlobalInfo section is read, we create a Function object
292   /// for each function in the module. When the function is loaded, after the
293   /// module global info is read, this Function is populated. Until then, the
294   /// functions in this vector just hold the function signature.
295   std::vector<Function*> FunctionSignatureList;
296
297   /// @brief This is the table of values belonging to the current function
298   ValueTable FunctionValues;
299
300   /// @brief This is the table of values belonging to the module (global)
301   ValueTable ModuleValues;
302
303   /// @brief This keeps track of function level forward references.
304   ForwardReferenceMap ForwardReferences;
305
306   /// @brief The basic blocks we've parsed, while parsing a function.
307   std::vector<BasicBlock*> ParsedBasicBlocks;
308
309   /// This maintains a mapping between <Type, Slot #>'s and forward references
310   /// to constants.  Such values may be referenced before they are defined, and
311   /// if so, the temporary object that they represent is held here.  @brief
312   /// Temporary place for forward references to constants.
313   ConstantRefsType ConstantFwdRefs;
314
315   /// Constant values are read in after global variables.  Because of this, we
316   /// must defer setting the initializers on global variables until after module
317   /// level constants have been read.  In the mean time, this list keeps track
318   /// of what we must do.
319   GlobalInitsList GlobalInits;
320
321   // For lazy reading-in of functions, we need to save away several pieces of
322   // information about each function: its begin and end pointer in the buffer
323   // and its FunctionSlot.
324   LazyFunctionMap LazyFunctionLoadMap;
325
326   /// This stores the parser's handler which is used for handling tasks other
327   /// just than reading bytecode into the IR. If this is non-null, calls on
328   /// the (polymorphic) BytecodeHandler interface (see llvm/Bytecode/Handler.h)
329   /// will be made to report the logical structure of the bytecode file. What
330   /// the handler does with the events it receives is completely orthogonal to
331   /// the business of parsing the bytecode and building the IR.  This is used,
332   /// for example, by the llvm-abcd tool for analysis of byte code.
333   /// @brief Handler for parsing events.
334   BytecodeHandler* Handler;
335
336
337 /// @}
338 /// @name Implementation Details
339 /// @{
340 private:
341   /// @brief Determines if this module has a function or not.
342   bool hasFunctions() { return ! FunctionSignatureList.empty(); }
343
344   /// @brief Determines if the type id has an implicit null value.
345   bool hasImplicitNull(unsigned TyID );
346
347   /// @brief Converts a type slot number to its Type*
348   const Type *getType(unsigned ID);
349
350   /// @brief Read in a type id and turn it into a Type* 
351   inline const Type* readType();
352
353   /// @brief Converts a Type* to its type slot number
354   unsigned getTypeSlot(const Type *Ty);
355
356   /// @brief Gets the global type corresponding to the TypeId
357   const Type *getGlobalTableType(unsigned TypeId);
358
359   /// @brief Get a value from its typeid and slot number
360   Value* getValue(unsigned TypeID, unsigned num, bool Create = true);
361
362   /// @brief Get a basic block for current function
363   BasicBlock *getBasicBlock(unsigned ID);
364
365   /// @brief Get a constant value from its typeid and value slot.
366   Constant* getConstantValue(unsigned typeSlot, unsigned valSlot);
367
368   /// @brief Convenience function for getting a constant value when
369   /// the Type has already been resolved.
370   Constant* getConstantValue(const Type *Ty, unsigned valSlot) {
371     return getConstantValue(getTypeSlot(Ty), valSlot);
372   }
373
374   /// @brief Insert a newly created value
375   unsigned insertValue(Value *V, unsigned Type, ValueTable &Table);
376
377   /// @brief Insert the arguments of a function.
378   void insertArguments(Function* F );
379
380   /// @brief Resolve all references to the placeholder (if any) for the
381   /// given constant.
382   void ResolveReferencesToConstant(Constant *C, unsigned Typ, unsigned Slot);
383
384   /// @brief Free a table, making sure to free the ValueList in the table.
385   void freeTable(ValueTable &Tab) {
386     while (!Tab.empty()) {
387       delete Tab.back();
388       Tab.pop_back();
389     }
390   }
391
392   inline void error(const std::string& errmsg);
393
394   BytecodeReader(const BytecodeReader &);  // DO NOT IMPLEMENT
395   void operator=(const BytecodeReader &);  // DO NOT IMPLEMENT
396
397   // This enum provides the values of the well-known type slots that are always
398   // emitted as the first few types in the table by the BytecodeWriter class.
399   enum WellKnownTypeSlots {
400     VoidTypeSlot = 0, ///< TypeID == VoidTyID
401     FloatTySlot  = 1, ///< TypeID == FloatTyID
402     DoubleTySlot = 2, ///< TypeID == DoubleTyID
403     LabelTySlot  = 3, ///< TypeID == LabelTyID
404     BoolTySlot   = 4, ///< TypeID == IntegerTyID, width = 1
405     Int8TySlot   = 5, ///< TypeID == IntegerTyID, width = 8
406     Int16TySlot  = 6, ///< TypeID == IntegerTyID, width = 16
407     Int32TySlot  = 7, ///< TypeID == IntegerTyID, width = 32
408     Int64TySlot  = 8  ///< TypeID == IntegerTyID, width = 64
409   };
410
411 /// @}
412 /// @name Reader Primitives
413 /// @{
414 private:
415
416   /// @brief Is there more to parse in the current block?
417   inline bool moreInBlock();
418
419   /// @brief Have we read past the end of the block
420   inline void checkPastBlockEnd(const char * block_name);
421
422   /// @brief Align to 32 bits
423   inline void align32();
424
425   /// @brief Read an unsigned integer as 32-bits
426   inline unsigned read_uint();
427
428   /// @brief Read an unsigned integer with variable bit rate encoding
429   inline unsigned read_vbr_uint();
430
431   /// @brief Read an unsigned integer of no more than 24-bits with variable
432   /// bit rate encoding.
433   inline unsigned read_vbr_uint24();
434
435   /// @brief Read an unsigned 64-bit integer with variable bit rate encoding.
436   inline uint64_t read_vbr_uint64();
437
438   /// @brief Read a signed 64-bit integer with variable bit rate encoding.
439   inline int64_t read_vbr_int64();
440
441   /// @brief Read a string
442   inline std::string read_str();
443
444   /// @brief Read a float value
445   inline void read_float(float& FloatVal);
446
447   /// @brief Read a double value
448   inline void read_double(double& DoubleVal);
449
450   /// @brief Read an arbitrary data chunk of fixed length
451   inline void read_data(void *Ptr, void *End);
452
453   /// @brief Read a bytecode block header
454   inline void read_block(unsigned &Type, unsigned &Size);
455 /// @}
456 };
457
458 /// @brief A function for creating a BytecodeAnalzer as a handler
459 /// for the Bytecode reader.
460 BytecodeHandler* createBytecodeAnalyzerHandler(BytecodeAnalysis& bca,
461                                                std::ostream* output );
462
463
464 } // End llvm namespace
465
466 // vim: sw=2
467 #endif