blob: da199519c3e1e0edda8e3819cc204d83ea675041 [file] [log] [blame]
Reid Spencerf89143c2004-06-29 23:31:01 +00001//===-- Reader.h - Interface To Bytecode Reading ----------------*- C++ -*-===//
Misha Brukman8a96c532005-04-21 21:44:41 +00002//
Reid Spencerf89143c2004-06-29 23:31:01 +00003// The LLVM Compiler Infrastructure
4//
Misha Brukman8a96c532005-04-21 21:44:41 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencerf89143c2004-06-29 23:31:01 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman8a96c532005-04-21 21:44:41 +00007//
Reid Spencerf89143c2004-06-29 23:31:01 +00008//===----------------------------------------------------------------------===//
9//
Misha Brukman8a96c532005-04-21 21:44:41 +000010// This header file defines the interface to the Bytecode Reader which is
Reid Spencerf89143c2004-06-29 23:31:01 +000011// 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"
Reid Spencera86159c2004-07-04 11:04:56 +000024#include "llvm/Bytecode/Analyzer.h"
Reid Spencerf89143c2004-06-29 23:31:01 +000025#include <utility>
26#include <map>
27
28namespace llvm {
29
30class BytecodeHandler; ///< Forward declare the handler interface
31
32/// This class defines the interface for parsing a buffer of bytecode. The
33/// parser itself takes no action except to call the various functions of
34/// the handler interface. The parser's sole responsibility is the correct
Misha Brukman8a96c532005-04-21 21:44:41 +000035/// interpretation of the bytecode buffer. The handler is responsible for
36/// instantiating and keeping track of all values. As a convenience, the parser
Reid Spencerf89143c2004-06-29 23:31:01 +000037/// is responsible for materializing types and will pass them through the
38/// handler interface as necessary.
39/// @see BytecodeHandler
40/// @brief Bytecode Reader interface
41class BytecodeReader : public ModuleProvider {
42
43/// @name Constructors
44/// @{
45public:
46 /// @brief Default constructor. By default, no handler is used.
Misha Brukman8a96c532005-04-21 21:44:41 +000047 BytecodeReader(BytecodeHandler* h = 0) {
Reid Spencerd3539b82004-11-14 22:00:09 +000048 decompressedBlock = 0;
Reid Spencer17f52c52004-11-06 23:17:23 +000049 Handler = h;
Reid Spencerf89143c2004-06-29 23:31:01 +000050 }
51
Misha Brukman8a96c532005-04-21 21:44:41 +000052 ~BytecodeReader() {
53 freeState();
Chris Lattner19925222004-11-15 21:55:06 +000054 if (decompressedBlock) {
Reid Spencerd3539b82004-11-14 22:00:09 +000055 ::free(decompressedBlock);
Chris Lattner19925222004-11-15 21:55:06 +000056 decompressedBlock = 0;
57 }
Reid Spencer17f52c52004-11-06 23:17:23 +000058 }
Reid Spencerf89143c2004-06-29 23:31:01 +000059
60/// @}
61/// @name Types
62/// @{
63public:
Reid Spencerad89bd62004-07-25 18:07:36 +000064
Reid Spencerf89143c2004-06-29 23:31:01 +000065 /// @brief A convenience type for the buffer pointer
66 typedef const unsigned char* BufPtr;
67
68 /// @brief The type used for a vector of potentially abstract types
69 typedef std::vector<PATypeHolder> TypeListTy;
70
71 /// This type provides a vector of Value* via the User class for
72 /// storage of Values that have been constructed when reading the
73 /// bytecode. Because of forward referencing, constant replacement
74 /// can occur so we ensure that our list of Value* is updated
75 /// properly through those transitions. This ensures that the
76 /// correct Value* is in our list when it comes time to associate
77 /// constants with global variables at the end of reading the
78 /// globals section.
79 /// @brief A list of values as a User of those Values.
Chris Lattnercad28bd2005-01-29 00:36:19 +000080 class ValueList : public User {
81 std::vector<Use> Uses;
82 public:
Chris Lattnerfea49302005-08-16 21:59:52 +000083 ValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {}
Reid Spencerf89143c2004-06-29 23:31:01 +000084
85 // vector compatibility methods
86 unsigned size() const { return getNumOperands(); }
Chris Lattnercad28bd2005-01-29 00:36:19 +000087 void push_back(Value *V) {
88 Uses.push_back(Use(V, this));
89 OperandList = &Uses[0];
90 ++NumOperands;
91 }
92 Value *back() const { return Uses.back(); }
93 void pop_back() { Uses.pop_back(); --NumOperands; }
94 bool empty() const { return NumOperands == 0; }
Reid Spencerf89143c2004-06-29 23:31:01 +000095 virtual void print(std::ostream& os) const {
Chris Lattnercad28bd2005-01-29 00:36:19 +000096 for (unsigned i = 0; i < size(); ++i) {
Reid Spencera86159c2004-07-04 11:04:56 +000097 os << i << " ";
98 getOperand(i)->print(os);
99 os << "\n";
Reid Spencerf89143c2004-06-29 23:31:01 +0000100 }
101 }
102 };
103
104 /// @brief A 2 dimensional table of values
105 typedef std::vector<ValueList*> ValueTable;
106
Misha Brukman8a96c532005-04-21 21:44:41 +0000107 /// This map is needed so that forward references to constants can be looked
Reid Spencerf89143c2004-06-29 23:31:01 +0000108 /// up by Type and slot number when resolving those references.
109 /// @brief A mapping of a Type/slot pair to a Constant*.
Chris Lattner389bd042004-12-09 06:19:44 +0000110 typedef std::map<std::pair<unsigned,unsigned>, Constant*> ConstantRefsType;
Reid Spencerf89143c2004-06-29 23:31:01 +0000111
112 /// For lazy read-in of functions, we need to save the location in the
113 /// data stream where the function is located. This structure provides that
114 /// information. Lazy read-in is used mostly by the JIT which only wants to
Misha Brukman8a96c532005-04-21 21:44:41 +0000115 /// resolve functions as it needs them.
Reid Spencerf89143c2004-06-29 23:31:01 +0000116 /// @brief Keeps pointers to function contents for later use.
117 struct LazyFunctionInfo {
118 const unsigned char *Buf, *EndBuf;
119 LazyFunctionInfo(const unsigned char *B = 0, const unsigned char *EB = 0)
120 : Buf(B), EndBuf(EB) {}
121 };
122
123 /// @brief A mapping of functions to their LazyFunctionInfo for lazy reading.
124 typedef std::map<Function*, LazyFunctionInfo> LazyFunctionMap;
125
126 /// @brief A list of global variables and the slot number that initializes
127 /// them.
128 typedef std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitsList;
129
130 /// This type maps a typeslot/valueslot pair to the corresponding Value*.
131 /// It is used for dealing with forward references as values are read in.
132 /// @brief A map for dealing with forward references of values.
133 typedef std::map<std::pair<unsigned,unsigned>,Value*> ForwardReferenceMap;
134
135/// @}
136/// @name Methods
137/// @{
138public:
Reid Spencerf89143c2004-06-29 23:31:01 +0000139 /// @brief Main interface to parsing a bytecode buffer.
140 void ParseBytecode(
Reid Spencer5c15fe52004-07-05 00:57:50 +0000141 const unsigned char *Buf, ///< Beginning of the bytecode buffer
142 unsigned Length, ///< Length of the bytecode buffer
Reid Spencer572c2562004-08-21 20:50:49 +0000143 const std::string &ModuleID ///< An identifier for the module constructed.
Reid Spencerf89143c2004-06-29 23:31:01 +0000144 );
145
Reid Spencerf89143c2004-06-29 23:31:01 +0000146 /// @brief Parse all function bodies
Reid Spencera86159c2004-07-04 11:04:56 +0000147 void ParseAllFunctionBodies();
Reid Spencerf89143c2004-06-29 23:31:01 +0000148
Reid Spencerf89143c2004-06-29 23:31:01 +0000149 /// @brief Parse the next function of specific type
Reid Spencera86159c2004-07-04 11:04:56 +0000150 void ParseFunction(Function* Func) ;
Reid Spencerf89143c2004-06-29 23:31:01 +0000151
152 /// This method is abstract in the parent ModuleProvider class. Its
153 /// implementation is identical to the ParseFunction method.
154 /// @see ParseFunction
155 /// @brief Make a specific function materialize.
Chris Lattner0300f3e2006-07-06 21:35:01 +0000156 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0) {
Reid Spencerf89143c2004-06-29 23:31:01 +0000157 LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.find(F);
Chris Lattner0300f3e2006-07-06 21:35:01 +0000158 if (Fi == LazyFunctionLoadMap.end()) return false;
159 try {
160 ParseFunction(F);
161 } catch (std::string &ErrStr) {
162 if (ErrInfo) *ErrInfo = ErrStr;
163 return true;
164 } catch (...) {
165 return true;
166 }
167 return false;
Reid Spencerf89143c2004-06-29 23:31:01 +0000168 }
169
170 /// This method is abstract in the parent ModuleProvider class. Its
Misha Brukman8a96c532005-04-21 21:44:41 +0000171 /// implementation is identical to ParseAllFunctionBodies.
Reid Spencerf89143c2004-06-29 23:31:01 +0000172 /// @see ParseAllFunctionBodies
173 /// @brief Make the whole module materialize
Chris Lattner0300f3e2006-07-06 21:35:01 +0000174 virtual Module* materializeModule(std::string *ErrInfo = 0) {
175 try {
176 ParseAllFunctionBodies();
177 } catch (std::string &ErrStr) {
178 if (ErrInfo) *ErrInfo = ErrStr;
179 return 0;
180 } catch (...) {
181 return 0;
182 }
Reid Spencerf89143c2004-06-29 23:31:01 +0000183 return TheModule;
184 }
185
186 /// This method is provided by the parent ModuleProvde class and overriden
187 /// here. It simply releases the module from its provided and frees up our
188 /// state.
189 /// @brief Release our hold on the generated module
Chris Lattner94aa7f32006-07-07 06:06:06 +0000190 Module* releaseModule(std::string *ErrInfo = 0) {
Reid Spencerf89143c2004-06-29 23:31:01 +0000191 // Since we're losing control of this Module, we must hand it back complete
192 Module *M = ModuleProvider::releaseModule();
193 freeState();
194 return M;
195 }
196
197/// @}
198/// @name Parsing Units For Subclasses
199/// @{
200protected:
201 /// @brief Parse whole module scope
202 void ParseModule();
203
204 /// @brief Parse the version information block
205 void ParseVersionInfo();
206
207 /// @brief Parse the ModuleGlobalInfo block
208 void ParseModuleGlobalInfo();
209
210 /// @brief Parse a symbol table
211 void ParseSymbolTable( Function* Func, SymbolTable *ST);
212
Reid Spencerf89143c2004-06-29 23:31:01 +0000213 /// @brief Parse functions lazily.
214 void ParseFunctionLazily();
215
216 /// @brief Parse a function body
217 void ParseFunctionBody(Function* Func);
218
Reid Spencera86159c2004-07-04 11:04:56 +0000219 /// @brief Parse the type list portion of a compaction table
Chris Lattner45b5dd22004-08-03 23:41:28 +0000220 void ParseCompactionTypes(unsigned NumEntries);
Reid Spencera86159c2004-07-04 11:04:56 +0000221
Reid Spencerf89143c2004-06-29 23:31:01 +0000222 /// @brief Parse a compaction table
223 void ParseCompactionTable();
224
225 /// @brief Parse global types
226 void ParseGlobalTypes();
227
Reid Spencerf89143c2004-06-29 23:31:01 +0000228 /// @brief Parse a basic block (for LLVM 1.0 basic block blocks)
229 BasicBlock* ParseBasicBlock(unsigned BlockNo);
230
Reid Spencerf89143c2004-06-29 23:31:01 +0000231 /// @brief parse an instruction list (for post LLVM 1.0 instruction lists
232 /// with blocks differentiated by terminating instructions.
233 unsigned ParseInstructionList(
234 Function* F ///< The function into which BBs will be inserted
235 );
Misha Brukman8a96c532005-04-21 21:44:41 +0000236
Reid Spencerf89143c2004-06-29 23:31:01 +0000237 /// @brief Parse a single instruction.
238 void ParseInstruction(
239 std::vector<unsigned>& Args, ///< The arguments to be filled in
240 BasicBlock* BB ///< The BB the instruction goes in
241 );
242
243 /// @brief Parse the whole constant pool
Misha Brukman8a96c532005-04-21 21:44:41 +0000244 void ParseConstantPool(ValueTable& Values, TypeListTy& Types,
Reid Spencera86159c2004-07-04 11:04:56 +0000245 bool isFunction);
Reid Spencerf89143c2004-06-29 23:31:01 +0000246
Chris Lattner3bc5a602006-01-25 23:08:15 +0000247 /// @brief Parse a single constant pool value
248 Value *ParseConstantPoolValue(unsigned TypeID);
Reid Spencerf89143c2004-06-29 23:31:01 +0000249
250 /// @brief Parse a block of types constants
Reid Spencer66906512004-07-11 17:24:05 +0000251 void ParseTypes(TypeListTy &Tab, unsigned NumEntries);
Reid Spencerf89143c2004-06-29 23:31:01 +0000252
253 /// @brief Parse a single type constant
Reid Spencer66906512004-07-11 17:24:05 +0000254 const Type *ParseType();
Reid Spencerf89143c2004-06-29 23:31:01 +0000255
256 /// @brief Parse a string constants block
257 void ParseStringConstants(unsigned NumEntries, ValueTable &Tab);
258
259/// @}
260/// @name Data
261/// @{
262private:
Misha Brukman8a96c532005-04-21 21:44:41 +0000263 char* decompressedBlock; ///< Result of decompression
Reid Spencerf89143c2004-06-29 23:31:01 +0000264 BufPtr MemStart; ///< Start of the memory buffer
265 BufPtr MemEnd; ///< End of the memory buffer
266 BufPtr BlockStart; ///< Start of current block being parsed
267 BufPtr BlockEnd; ///< End of current block being parsed
268 BufPtr At; ///< Where we're currently parsing at
269
Reid Spencera86159c2004-07-04 11:04:56 +0000270 /// Information about the module, extracted from the bytecode revision number.
Chris Lattner45b5dd22004-08-03 23:41:28 +0000271 ///
Reid Spencerf89143c2004-06-29 23:31:01 +0000272 unsigned char RevisionNum; // The rev # itself
273
Reid Spencera86159c2004-07-04 11:04:56 +0000274 /// Flags to distinguish LLVM 1.0 & 1.1 bytecode formats (revision #0)
Reid Spencerf89143c2004-06-29 23:31:01 +0000275
Chris Lattner45b5dd22004-08-03 23:41:28 +0000276 /// Revision #0 had an explicit alignment of data only for the
277 /// ModuleGlobalInfo block. This was fixed to be like all other blocks in 1.2
Reid Spencerf89143c2004-06-29 23:31:01 +0000278 bool hasInconsistentModuleGlobalInfo;
279
Reid Spencera86159c2004-07-04 11:04:56 +0000280 /// Revision #0 also explicitly encoded zero values for primitive types like
281 /// int/sbyte/etc.
Reid Spencerf89143c2004-06-29 23:31:01 +0000282 bool hasExplicitPrimitiveZeros;
283
284 // Flags to control features specific the LLVM 1.2 and before (revision #1)
285
Reid Spencera86159c2004-07-04 11:04:56 +0000286 /// LLVM 1.2 and earlier required that getelementptr structure indices were
287 /// ubyte constants and that sequential type indices were longs.
Reid Spencerf89143c2004-06-29 23:31:01 +0000288 bool hasRestrictedGEPTypes;
289
Reid Spencera86159c2004-07-04 11:04:56 +0000290 /// LLVM 1.2 and earlier had class Type deriving from Value and the Type
291 /// objects were located in the "Type Type" plane of various lists in read
292 /// by the bytecode reader. In LLVM 1.3 this is no longer the case. Types are
293 /// completely distinct from Values. Consequently, Types are written in fixed
294 /// locations in LLVM 1.3. This flag indicates that the older Type derived
295 /// from Value style of bytecode file is being read.
296 bool hasTypeDerivedFromValue;
297
Reid Spencerad89bd62004-07-25 18:07:36 +0000298 /// LLVM 1.2 and earlier encoded block headers as two uint (8 bytes), one for
Chris Lattner45b5dd22004-08-03 23:41:28 +0000299 /// the size and one for the type. This is a bit wasteful, especially for
300 /// small files where the 8 bytes per block is a large fraction of the total
301 /// block size. In LLVM 1.3, the block type and length are encoded into a
302 /// single uint32 by restricting the number of block types (limit 31) and the
303 /// maximum size of a block (limit 2^27-1=134,217,727). Note that the module
304 /// block still uses the 8-byte format so the maximum size of a file can be
Reid Spencerad89bd62004-07-25 18:07:36 +0000305 /// 2^32-1 bytes long.
306 bool hasLongBlockHeaders;
307
Reid Spencerad89bd62004-07-25 18:07:36 +0000308 /// LLVM 1.2 and earlier wrote type slot numbers as vbr_uint32. In LLVM 1.3
Misha Brukman8a96c532005-04-21 21:44:41 +0000309 /// this has been reduced to vbr_uint24. It shouldn't make much difference
Reid Spencerad89bd62004-07-25 18:07:36 +0000310 /// since we haven't run into a module with > 24 million types, but for safety
311 /// the 24-bit restriction has been enforced in 1.3 to free some bits in
312 /// various places and to ensure consistency. In particular, global vars are
313 /// restricted to 24-bits.
314 bool has32BitTypes;
315
Misha Brukman8a96c532005-04-21 21:44:41 +0000316 /// LLVM 1.2 and earlier did not provide a target triple nor a list of
Reid Spencerad89bd62004-07-25 18:07:36 +0000317 /// libraries on which the bytecode is dependent. LLVM 1.3 provides these
318 /// features, for use in future versions of LLVM.
319 bool hasNoDependentLibraries;
320
Reid Spencer38d54be2004-08-17 07:45:14 +0000321 /// LLVM 1.3 and earlier caused blocks and other fields to start on 32-bit
322 /// aligned boundaries. This can lead to as much as 30% bytecode size overhead
323 /// in various corner cases (lots of long instructions). In LLVM 1.4,
324 /// alignment of bytecode fields was done away with completely.
325 bool hasAlignment;
Reid Spencerad89bd62004-07-25 18:07:36 +0000326
Chris Lattnera79e7cc2004-10-16 18:18:16 +0000327 // In version 4 and earlier, the bytecode format did not support the 'undef'
328 // constant.
329 bool hasNoUndefValue;
330
331 // In version 4 and earlier, the bytecode format did not save space for flags
332 // in the global info block for functions.
333 bool hasNoFlagsForFunctions;
334
335 // In version 4 and earlier, there was no opcode space reserved for the
336 // unreachable instruction.
337 bool hasNoUnreachableInst;
338
Reid Spencer3e595462006-01-19 06:57:58 +0000339 /// In release 1.7 we changed intrinsic functions to not be overloaded. There
340 /// is no bytecode change for this, but to optimize the auto-upgrade of calls
Reid Spencere2a5fb02006-01-27 11:49:27 +0000341 /// to intrinsic functions, we save a mapping of old function definitions to
342 /// the new ones so call instructions can be upgraded efficiently.
343 std::map<Function*,Function*> upgradedFunctions;
Reid Spencer3e595462006-01-19 06:57:58 +0000344
Chris Lattner45b5dd22004-08-03 23:41:28 +0000345 /// CompactionTypes - If a compaction table is active in the current function,
346 /// this is the mapping that it contains. We keep track of what resolved type
347 /// it is as well as what global type entry it is.
348 std::vector<std::pair<const Type*, unsigned> > CompactionTypes;
Reid Spencerf89143c2004-06-29 23:31:01 +0000349
350 /// @brief If a compaction table is active in the current function,
351 /// this is the mapping that it contains.
352 std::vector<std::vector<Value*> > CompactionValues;
353
354 /// @brief This vector is used to deal with forward references to types in
355 /// a module.
356 TypeListTy ModuleTypes;
Chris Lattnereebac5f2005-10-03 21:26:53 +0000357
358 /// @brief This is an inverse mapping of ModuleTypes from the type to an
359 /// index. Because refining types causes the index of this map to be
360 /// invalidated, any time we refine a type, we clear this cache and recompute
361 /// it next time we need it. These entries are ordered by the pointer value.
362 std::vector<std::pair<const Type*, unsigned> > ModuleTypeIDCache;
Reid Spencerf89143c2004-06-29 23:31:01 +0000363
364 /// @brief This vector is used to deal with forward references to types in
365 /// a function.
366 TypeListTy FunctionTypes;
367
368 /// When the ModuleGlobalInfo section is read, we create a Function object
369 /// for each function in the module. When the function is loaded, after the
370 /// module global info is read, this Function is populated. Until then, the
371 /// functions in this vector just hold the function signature.
372 std::vector<Function*> FunctionSignatureList;
373
374 /// @brief This is the table of values belonging to the current function
375 ValueTable FunctionValues;
376
377 /// @brief This is the table of values belonging to the module (global)
378 ValueTable ModuleValues;
379
380 /// @brief This keeps track of function level forward references.
381 ForwardReferenceMap ForwardReferences;
382
383 /// @brief The basic blocks we've parsed, while parsing a function.
384 std::vector<BasicBlock*> ParsedBasicBlocks;
385
Chris Lattner1c765b02004-10-14 01:49:34 +0000386 /// This maintains a mapping between <Type, Slot #>'s and forward references
387 /// to constants. Such values may be referenced before they are defined, and
388 /// if so, the temporary object that they represent is held here. @brief
389 /// Temporary place for forward references to constants.
Reid Spencerf89143c2004-06-29 23:31:01 +0000390 ConstantRefsType ConstantFwdRefs;
391
392 /// Constant values are read in after global variables. Because of this, we
393 /// must defer setting the initializers on global variables until after module
Chris Lattner1c765b02004-10-14 01:49:34 +0000394 /// level constants have been read. In the mean time, this list keeps track
395 /// of what we must do.
Reid Spencerf89143c2004-06-29 23:31:01 +0000396 GlobalInitsList GlobalInits;
397
398 // For lazy reading-in of functions, we need to save away several pieces of
399 // information about each function: its begin and end pointer in the buffer
400 // and its FunctionSlot.
401 LazyFunctionMap LazyFunctionLoadMap;
402
Misha Brukman8a96c532005-04-21 21:44:41 +0000403 /// This stores the parser's handler which is used for handling tasks other
404 /// just than reading bytecode into the IR. If this is non-null, calls on
405 /// the (polymorphic) BytecodeHandler interface (see llvm/Bytecode/Handler.h)
406 /// will be made to report the logical structure of the bytecode file. What
407 /// the handler does with the events it receives is completely orthogonal to
Reid Spencerf89143c2004-06-29 23:31:01 +0000408 /// the business of parsing the bytecode and building the IR. This is used,
409 /// for example, by the llvm-abcd tool for analysis of byte code.
410 /// @brief Handler for parsing events.
411 BytecodeHandler* Handler;
412
Reid Spencer3e595462006-01-19 06:57:58 +0000413
Reid Spencerf89143c2004-06-29 23:31:01 +0000414/// @}
415/// @name Implementation Details
416/// @{
417private:
418 /// @brief Determines if this module has a function or not.
419 bool hasFunctions() { return ! FunctionSignatureList.empty(); }
420
421 /// @brief Determines if the type id has an implicit null value.
422 bool hasImplicitNull(unsigned TyID );
423
424 /// @brief Converts a type slot number to its Type*
425 const Type *getType(unsigned ID);
426
Reid Spencera86159c2004-07-04 11:04:56 +0000427 /// @brief Converts a pre-sanitized type slot number to its Type* and
428 /// sanitizes the type id.
429 inline const Type* getSanitizedType(unsigned& ID );
430
431 /// @brief Read in and get a sanitized type id
Chris Lattner19925222004-11-15 21:55:06 +0000432 inline const Type* readSanitizedType();
Reid Spencera86159c2004-07-04 11:04:56 +0000433
Reid Spencerf89143c2004-06-29 23:31:01 +0000434 /// @brief Converts a Type* to its type slot number
435 unsigned getTypeSlot(const Type *Ty);
436
437 /// @brief Converts a normal type slot number to a compacted type slot num.
438 unsigned getCompactionTypeSlot(unsigned type);
439
Reid Spencera86159c2004-07-04 11:04:56 +0000440 /// @brief Gets the global type corresponding to the TypeId
441 const Type *getGlobalTableType(unsigned TypeId);
Reid Spencerf89143c2004-06-29 23:31:01 +0000442
443 /// This is just like getTypeSlot, but when a compaction table is in use,
Misha Brukman8a96c532005-04-21 21:44:41 +0000444 /// it is ignored.
Reid Spencerf89143c2004-06-29 23:31:01 +0000445 unsigned getGlobalTableTypeSlot(const Type *Ty);
Misha Brukman8a96c532005-04-21 21:44:41 +0000446
Reid Spencera86159c2004-07-04 11:04:56 +0000447 /// @brief Get a value from its typeid and slot number
Reid Spencerf89143c2004-06-29 23:31:01 +0000448 Value* getValue(unsigned TypeID, unsigned num, bool Create = true);
449
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000450 /// @brief Get a value from its type and slot number, ignoring compaction
451 /// tables.
452 Value *getGlobalTableValue(unsigned TyID, unsigned SlotNo);
Reid Spencerf89143c2004-06-29 23:31:01 +0000453
Reid Spencerf89143c2004-06-29 23:31:01 +0000454 /// @brief Get a basic block for current function
455 BasicBlock *getBasicBlock(unsigned ID);
456
Reid Spencera86159c2004-07-04 11:04:56 +0000457 /// @brief Get a constant value from its typeid and value slot.
Reid Spencerf89143c2004-06-29 23:31:01 +0000458 Constant* getConstantValue(unsigned typeSlot, unsigned valSlot);
459
460 /// @brief Convenience function for getting a constant value when
461 /// the Type has already been resolved.
462 Constant* getConstantValue(const Type *Ty, unsigned valSlot) {
463 return getConstantValue(getTypeSlot(Ty), valSlot);
464 }
465
Reid Spencerf89143c2004-06-29 23:31:01 +0000466 /// @brief Insert a newly created value
467 unsigned insertValue(Value *V, unsigned Type, ValueTable &Table);
468
469 /// @brief Insert the arguments of a function.
470 void insertArguments(Function* F );
471
Misha Brukman8a96c532005-04-21 21:44:41 +0000472 /// @brief Resolve all references to the placeholder (if any) for the
Reid Spencerf89143c2004-06-29 23:31:01 +0000473 /// given constant.
Chris Lattner389bd042004-12-09 06:19:44 +0000474 void ResolveReferencesToConstant(Constant *C, unsigned Typ, unsigned Slot);
Reid Spencerf89143c2004-06-29 23:31:01 +0000475
476 /// @brief Release our memory.
477 void freeState() {
478 freeTable(FunctionValues);
479 freeTable(ModuleValues);
480 }
481
482 /// @brief Free a table, making sure to free the ValueList in the table.
483 void freeTable(ValueTable &Tab) {
484 while (!Tab.empty()) {
485 delete Tab.back();
486 Tab.pop_back();
487 }
488 }
489
Reid Spencer24399722004-07-09 22:21:33 +0000490 inline void error(std::string errmsg);
491
Reid Spencerf89143c2004-06-29 23:31:01 +0000492 BytecodeReader(const BytecodeReader &); // DO NOT IMPLEMENT
493 void operator=(const BytecodeReader &); // DO NOT IMPLEMENT
494
495/// @}
496/// @name Reader Primitives
497/// @{
498private:
499
500 /// @brief Is there more to parse in the current block?
501 inline bool moreInBlock();
502
503 /// @brief Have we read past the end of the block
504 inline void checkPastBlockEnd(const char * block_name);
505
506 /// @brief Align to 32 bits
507 inline void align32();
508
509 /// @brief Read an unsigned integer as 32-bits
510 inline unsigned read_uint();
511
512 /// @brief Read an unsigned integer with variable bit rate encoding
513 inline unsigned read_vbr_uint();
514
Reid Spencerad89bd62004-07-25 18:07:36 +0000515 /// @brief Read an unsigned integer of no more than 24-bits with variable
516 /// bit rate encoding.
517 inline unsigned read_vbr_uint24();
518
Reid Spencerf89143c2004-06-29 23:31:01 +0000519 /// @brief Read an unsigned 64-bit integer with variable bit rate encoding.
520 inline uint64_t read_vbr_uint64();
521
522 /// @brief Read a signed 64-bit integer with variable bit rate encoding.
523 inline int64_t read_vbr_int64();
524
525 /// @brief Read a string
526 inline std::string read_str();
527
Reid Spencer66906512004-07-11 17:24:05 +0000528 /// @brief Read a float value
529 inline void read_float(float& FloatVal);
530
531 /// @brief Read a double value
532 inline void read_double(double& DoubleVal);
533
Reid Spencerf89143c2004-06-29 23:31:01 +0000534 /// @brief Read an arbitrary data chunk of fixed length
535 inline void read_data(void *Ptr, void *End);
536
Reid Spencera86159c2004-07-04 11:04:56 +0000537 /// @brief Read a bytecode block header
Reid Spencerf89143c2004-06-29 23:31:01 +0000538 inline void read_block(unsigned &Type, unsigned &Size);
539
Reid Spencera86159c2004-07-04 11:04:56 +0000540 /// @brief Read a type identifier and sanitize it.
541 inline bool read_typeid(unsigned &TypeId);
542
543 /// @brief Recalculate type ID for pre 1.3 bytecode files.
544 inline bool sanitizeTypeId(unsigned &TypeId );
Reid Spencerf89143c2004-06-29 23:31:01 +0000545/// @}
546};
547
Reid Spencera86159c2004-07-04 11:04:56 +0000548/// @brief A function for creating a BytecodeAnalzer as a handler
549/// for the Bytecode reader.
Misha Brukman8a96c532005-04-21 21:44:41 +0000550BytecodeHandler* createBytecodeAnalyzerHandler(BytecodeAnalysis& bca,
Reid Spencer572c2562004-08-21 20:50:49 +0000551 std::ostream* output );
Reid Spencera86159c2004-07-04 11:04:56 +0000552
553
Reid Spencerf89143c2004-06-29 23:31:01 +0000554} // End llvm namespace
555
556// vim: sw=2
557#endif