Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 1 | //===-- Reader.h - Interface To Bytecode Reading ----------------*- C++ -*-===// |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 5 | // This file was developed by Reid Spencer and is distributed under the |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 10 | // This header file defines the interface to the Bytecode Reader which is |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 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" |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 24 | #include "llvm/Bytecode/Analyzer.h" |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 25 | #include <utility> |
| 26 | #include <map> |
| 27 | |
| 28 | namespace llvm { |
| 29 | |
| 30 | class 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 Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 35 | /// interpretation of the bytecode buffer. The handler is responsible for |
| 36 | /// instantiating and keeping track of all values. As a convenience, the parser |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 37 | /// is responsible for materializing types and will pass them through the |
| 38 | /// handler interface as necessary. |
| 39 | /// @see BytecodeHandler |
| 40 | /// @brief Bytecode Reader interface |
| 41 | class BytecodeReader : public ModuleProvider { |
| 42 | |
| 43 | /// @name Constructors |
| 44 | /// @{ |
| 45 | public: |
| 46 | /// @brief Default constructor. By default, no handler is used. |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 47 | BytecodeReader(BytecodeHandler* h = 0) { |
Reid Spencer | d3539b8 | 2004-11-14 22:00:09 +0000 | [diff] [blame] | 48 | decompressedBlock = 0; |
Reid Spencer | 17f52c5 | 2004-11-06 23:17:23 +0000 | [diff] [blame] | 49 | Handler = h; |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 52 | ~BytecodeReader() { |
| 53 | freeState(); |
Chris Lattner | 1992522 | 2004-11-15 21:55:06 +0000 | [diff] [blame] | 54 | if (decompressedBlock) { |
Reid Spencer | d3539b8 | 2004-11-14 22:00:09 +0000 | [diff] [blame] | 55 | ::free(decompressedBlock); |
Chris Lattner | 1992522 | 2004-11-15 21:55:06 +0000 | [diff] [blame] | 56 | decompressedBlock = 0; |
| 57 | } |
Reid Spencer | 17f52c5 | 2004-11-06 23:17:23 +0000 | [diff] [blame] | 58 | } |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 59 | |
| 60 | /// @} |
| 61 | /// @name Types |
| 62 | /// @{ |
| 63 | public: |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 64 | |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 65 | /// @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 Lattner | cad28bd | 2005-01-29 00:36:19 +0000 | [diff] [blame] | 80 | class ValueList : public User { |
| 81 | std::vector<Use> Uses; |
| 82 | public: |
Chris Lattner | fea4930 | 2005-08-16 21:59:52 +0000 | [diff] [blame] | 83 | ValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {} |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 84 | |
| 85 | // vector compatibility methods |
| 86 | unsigned size() const { return getNumOperands(); } |
Chris Lattner | cad28bd | 2005-01-29 00:36:19 +0000 | [diff] [blame] | 87 | 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 Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 95 | virtual void print(std::ostream& os) const { |
Chris Lattner | cad28bd | 2005-01-29 00:36:19 +0000 | [diff] [blame] | 96 | for (unsigned i = 0; i < size(); ++i) { |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 97 | os << i << " "; |
| 98 | getOperand(i)->print(os); |
| 99 | os << "\n"; |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | /// @brief A 2 dimensional table of values |
| 105 | typedef std::vector<ValueList*> ValueTable; |
| 106 | |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 107 | /// This map is needed so that forward references to constants can be looked |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 108 | /// up by Type and slot number when resolving those references. |
| 109 | /// @brief A mapping of a Type/slot pair to a Constant*. |
Chris Lattner | 389bd04 | 2004-12-09 06:19:44 +0000 | [diff] [blame] | 110 | typedef std::map<std::pair<unsigned,unsigned>, Constant*> ConstantRefsType; |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 111 | |
| 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 Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 115 | /// resolve functions as it needs them. |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 116 | /// @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 | /// @{ |
| 138 | public: |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 139 | /// @brief Main interface to parsing a bytecode buffer. |
| 140 | void ParseBytecode( |
Reid Spencer | 5c15fe5 | 2004-07-05 00:57:50 +0000 | [diff] [blame] | 141 | const unsigned char *Buf, ///< Beginning of the bytecode buffer |
| 142 | unsigned Length, ///< Length of the bytecode buffer |
Reid Spencer | 572c256 | 2004-08-21 20:50:49 +0000 | [diff] [blame] | 143 | const std::string &ModuleID ///< An identifier for the module constructed. |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 144 | ); |
| 145 | |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 146 | /// @brief Parse all function bodies |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 147 | void ParseAllFunctionBodies(); |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 148 | |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 149 | /// @brief Parse the next function of specific type |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 150 | void ParseFunction(Function* Func) ; |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 151 | |
| 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. |
| 156 | virtual void materializeFunction(Function *F) { |
| 157 | LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.find(F); |
| 158 | if (Fi == LazyFunctionLoadMap.end()) return; |
| 159 | ParseFunction(F); |
| 160 | } |
| 161 | |
| 162 | /// This method is abstract in the parent ModuleProvider class. Its |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 163 | /// implementation is identical to ParseAllFunctionBodies. |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 164 | /// @see ParseAllFunctionBodies |
| 165 | /// @brief Make the whole module materialize |
| 166 | virtual Module* materializeModule() { |
| 167 | ParseAllFunctionBodies(); |
| 168 | return TheModule; |
| 169 | } |
| 170 | |
| 171 | /// This method is provided by the parent ModuleProvde class and overriden |
| 172 | /// here. It simply releases the module from its provided and frees up our |
| 173 | /// state. |
| 174 | /// @brief Release our hold on the generated module |
| 175 | Module* releaseModule() { |
| 176 | // Since we're losing control of this Module, we must hand it back complete |
| 177 | Module *M = ModuleProvider::releaseModule(); |
| 178 | freeState(); |
| 179 | return M; |
| 180 | } |
| 181 | |
| 182 | /// @} |
| 183 | /// @name Parsing Units For Subclasses |
| 184 | /// @{ |
| 185 | protected: |
| 186 | /// @brief Parse whole module scope |
| 187 | void ParseModule(); |
| 188 | |
| 189 | /// @brief Parse the version information block |
| 190 | void ParseVersionInfo(); |
| 191 | |
| 192 | /// @brief Parse the ModuleGlobalInfo block |
| 193 | void ParseModuleGlobalInfo(); |
| 194 | |
| 195 | /// @brief Parse a symbol table |
| 196 | void ParseSymbolTable( Function* Func, SymbolTable *ST); |
| 197 | |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 198 | /// @brief Parse functions lazily. |
| 199 | void ParseFunctionLazily(); |
| 200 | |
| 201 | /// @brief Parse a function body |
| 202 | void ParseFunctionBody(Function* Func); |
| 203 | |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 204 | /// @brief Parse the type list portion of a compaction table |
Chris Lattner | 45b5dd2 | 2004-08-03 23:41:28 +0000 | [diff] [blame] | 205 | void ParseCompactionTypes(unsigned NumEntries); |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 206 | |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 207 | /// @brief Parse a compaction table |
| 208 | void ParseCompactionTable(); |
| 209 | |
| 210 | /// @brief Parse global types |
| 211 | void ParseGlobalTypes(); |
| 212 | |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 213 | /// @brief Parse a basic block (for LLVM 1.0 basic block blocks) |
| 214 | BasicBlock* ParseBasicBlock(unsigned BlockNo); |
| 215 | |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 216 | /// @brief parse an instruction list (for post LLVM 1.0 instruction lists |
| 217 | /// with blocks differentiated by terminating instructions. |
| 218 | unsigned ParseInstructionList( |
| 219 | Function* F ///< The function into which BBs will be inserted |
| 220 | ); |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 221 | |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 222 | /// @brief Parse a single instruction. |
| 223 | void ParseInstruction( |
| 224 | std::vector<unsigned>& Args, ///< The arguments to be filled in |
| 225 | BasicBlock* BB ///< The BB the instruction goes in |
| 226 | ); |
| 227 | |
| 228 | /// @brief Parse the whole constant pool |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 229 | void ParseConstantPool(ValueTable& Values, TypeListTy& Types, |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 230 | bool isFunction); |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 231 | |
| 232 | /// @brief Parse a single constant value |
| 233 | Constant* ParseConstantValue(unsigned TypeID); |
| 234 | |
| 235 | /// @brief Parse a block of types constants |
Reid Spencer | 6690651 | 2004-07-11 17:24:05 +0000 | [diff] [blame] | 236 | void ParseTypes(TypeListTy &Tab, unsigned NumEntries); |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 237 | |
| 238 | /// @brief Parse a single type constant |
Reid Spencer | 6690651 | 2004-07-11 17:24:05 +0000 | [diff] [blame] | 239 | const Type *ParseType(); |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 240 | |
| 241 | /// @brief Parse a string constants block |
| 242 | void ParseStringConstants(unsigned NumEntries, ValueTable &Tab); |
| 243 | |
| 244 | /// @} |
| 245 | /// @name Data |
| 246 | /// @{ |
| 247 | private: |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 248 | char* decompressedBlock; ///< Result of decompression |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 249 | BufPtr MemStart; ///< Start of the memory buffer |
| 250 | BufPtr MemEnd; ///< End of the memory buffer |
| 251 | BufPtr BlockStart; ///< Start of current block being parsed |
| 252 | BufPtr BlockEnd; ///< End of current block being parsed |
| 253 | BufPtr At; ///< Where we're currently parsing at |
| 254 | |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 255 | /// Information about the module, extracted from the bytecode revision number. |
Chris Lattner | 45b5dd2 | 2004-08-03 23:41:28 +0000 | [diff] [blame] | 256 | /// |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 257 | unsigned char RevisionNum; // The rev # itself |
| 258 | |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 259 | /// Flags to distinguish LLVM 1.0 & 1.1 bytecode formats (revision #0) |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 260 | |
Chris Lattner | 45b5dd2 | 2004-08-03 23:41:28 +0000 | [diff] [blame] | 261 | /// Revision #0 had an explicit alignment of data only for the |
| 262 | /// ModuleGlobalInfo block. This was fixed to be like all other blocks in 1.2 |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 263 | bool hasInconsistentModuleGlobalInfo; |
| 264 | |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 265 | /// Revision #0 also explicitly encoded zero values for primitive types like |
| 266 | /// int/sbyte/etc. |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 267 | bool hasExplicitPrimitiveZeros; |
| 268 | |
| 269 | // Flags to control features specific the LLVM 1.2 and before (revision #1) |
| 270 | |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 271 | /// LLVM 1.2 and earlier required that getelementptr structure indices were |
| 272 | /// ubyte constants and that sequential type indices were longs. |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 273 | bool hasRestrictedGEPTypes; |
| 274 | |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 275 | /// LLVM 1.2 and earlier had class Type deriving from Value and the Type |
| 276 | /// objects were located in the "Type Type" plane of various lists in read |
| 277 | /// by the bytecode reader. In LLVM 1.3 this is no longer the case. Types are |
| 278 | /// completely distinct from Values. Consequently, Types are written in fixed |
| 279 | /// locations in LLVM 1.3. This flag indicates that the older Type derived |
| 280 | /// from Value style of bytecode file is being read. |
| 281 | bool hasTypeDerivedFromValue; |
| 282 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 283 | /// LLVM 1.2 and earlier encoded block headers as two uint (8 bytes), one for |
Chris Lattner | 45b5dd2 | 2004-08-03 23:41:28 +0000 | [diff] [blame] | 284 | /// the size and one for the type. This is a bit wasteful, especially for |
| 285 | /// small files where the 8 bytes per block is a large fraction of the total |
| 286 | /// block size. In LLVM 1.3, the block type and length are encoded into a |
| 287 | /// single uint32 by restricting the number of block types (limit 31) and the |
| 288 | /// maximum size of a block (limit 2^27-1=134,217,727). Note that the module |
| 289 | /// block still uses the 8-byte format so the maximum size of a file can be |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 290 | /// 2^32-1 bytes long. |
| 291 | bool hasLongBlockHeaders; |
| 292 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 293 | /// LLVM 1.2 and earlier wrote type slot numbers as vbr_uint32. In LLVM 1.3 |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 294 | /// this has been reduced to vbr_uint24. It shouldn't make much difference |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 295 | /// since we haven't run into a module with > 24 million types, but for safety |
| 296 | /// the 24-bit restriction has been enforced in 1.3 to free some bits in |
| 297 | /// various places and to ensure consistency. In particular, global vars are |
| 298 | /// restricted to 24-bits. |
| 299 | bool has32BitTypes; |
| 300 | |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 301 | /// LLVM 1.2 and earlier did not provide a target triple nor a list of |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 302 | /// libraries on which the bytecode is dependent. LLVM 1.3 provides these |
| 303 | /// features, for use in future versions of LLVM. |
| 304 | bool hasNoDependentLibraries; |
| 305 | |
Reid Spencer | 38d54be | 2004-08-17 07:45:14 +0000 | [diff] [blame] | 306 | /// LLVM 1.3 and earlier caused blocks and other fields to start on 32-bit |
| 307 | /// aligned boundaries. This can lead to as much as 30% bytecode size overhead |
| 308 | /// in various corner cases (lots of long instructions). In LLVM 1.4, |
| 309 | /// alignment of bytecode fields was done away with completely. |
| 310 | bool hasAlignment; |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 311 | |
Chris Lattner | a79e7cc | 2004-10-16 18:18:16 +0000 | [diff] [blame] | 312 | // In version 4 and earlier, the bytecode format did not support the 'undef' |
| 313 | // constant. |
| 314 | bool hasNoUndefValue; |
| 315 | |
| 316 | // In version 4 and earlier, the bytecode format did not save space for flags |
| 317 | // in the global info block for functions. |
| 318 | bool hasNoFlagsForFunctions; |
| 319 | |
| 320 | // In version 4 and earlier, there was no opcode space reserved for the |
| 321 | // unreachable instruction. |
| 322 | bool hasNoUnreachableInst; |
| 323 | |
Reid Spencer | 3e59546 | 2006-01-19 06:57:58 +0000 | [diff] [blame^] | 324 | /// In release 1.7 we changed intrinsic functions to not be overloaded. There |
| 325 | /// is no bytecode change for this, but to optimize the auto-upgrade of calls |
| 326 | /// to intrinsic functions, we set this flag to identify when a module has |
| 327 | /// been read that contains intrinsics that were upgraded. |
| 328 | bool hasUpgradedIntrinsicFunctions; |
| 329 | |
Chris Lattner | 45b5dd2 | 2004-08-03 23:41:28 +0000 | [diff] [blame] | 330 | /// CompactionTypes - If a compaction table is active in the current function, |
| 331 | /// this is the mapping that it contains. We keep track of what resolved type |
| 332 | /// it is as well as what global type entry it is. |
| 333 | std::vector<std::pair<const Type*, unsigned> > CompactionTypes; |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 334 | |
| 335 | /// @brief If a compaction table is active in the current function, |
| 336 | /// this is the mapping that it contains. |
| 337 | std::vector<std::vector<Value*> > CompactionValues; |
| 338 | |
| 339 | /// @brief This vector is used to deal with forward references to types in |
| 340 | /// a module. |
| 341 | TypeListTy ModuleTypes; |
Chris Lattner | eebac5f | 2005-10-03 21:26:53 +0000 | [diff] [blame] | 342 | |
| 343 | /// @brief This is an inverse mapping of ModuleTypes from the type to an |
| 344 | /// index. Because refining types causes the index of this map to be |
| 345 | /// invalidated, any time we refine a type, we clear this cache and recompute |
| 346 | /// it next time we need it. These entries are ordered by the pointer value. |
| 347 | std::vector<std::pair<const Type*, unsigned> > ModuleTypeIDCache; |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 348 | |
| 349 | /// @brief This vector is used to deal with forward references to types in |
| 350 | /// a function. |
| 351 | TypeListTy FunctionTypes; |
| 352 | |
| 353 | /// When the ModuleGlobalInfo section is read, we create a Function object |
| 354 | /// for each function in the module. When the function is loaded, after the |
| 355 | /// module global info is read, this Function is populated. Until then, the |
| 356 | /// functions in this vector just hold the function signature. |
| 357 | std::vector<Function*> FunctionSignatureList; |
| 358 | |
| 359 | /// @brief This is the table of values belonging to the current function |
| 360 | ValueTable FunctionValues; |
| 361 | |
| 362 | /// @brief This is the table of values belonging to the module (global) |
| 363 | ValueTable ModuleValues; |
| 364 | |
| 365 | /// @brief This keeps track of function level forward references. |
| 366 | ForwardReferenceMap ForwardReferences; |
| 367 | |
| 368 | /// @brief The basic blocks we've parsed, while parsing a function. |
| 369 | std::vector<BasicBlock*> ParsedBasicBlocks; |
| 370 | |
Chris Lattner | 1c765b0 | 2004-10-14 01:49:34 +0000 | [diff] [blame] | 371 | /// This maintains a mapping between <Type, Slot #>'s and forward references |
| 372 | /// to constants. Such values may be referenced before they are defined, and |
| 373 | /// if so, the temporary object that they represent is held here. @brief |
| 374 | /// Temporary place for forward references to constants. |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 375 | ConstantRefsType ConstantFwdRefs; |
| 376 | |
| 377 | /// Constant values are read in after global variables. Because of this, we |
| 378 | /// must defer setting the initializers on global variables until after module |
Chris Lattner | 1c765b0 | 2004-10-14 01:49:34 +0000 | [diff] [blame] | 379 | /// level constants have been read. In the mean time, this list keeps track |
| 380 | /// of what we must do. |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 381 | GlobalInitsList GlobalInits; |
| 382 | |
| 383 | // For lazy reading-in of functions, we need to save away several pieces of |
| 384 | // information about each function: its begin and end pointer in the buffer |
| 385 | // and its FunctionSlot. |
| 386 | LazyFunctionMap LazyFunctionLoadMap; |
| 387 | |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 388 | /// This stores the parser's handler which is used for handling tasks other |
| 389 | /// just than reading bytecode into the IR. If this is non-null, calls on |
| 390 | /// the (polymorphic) BytecodeHandler interface (see llvm/Bytecode/Handler.h) |
| 391 | /// will be made to report the logical structure of the bytecode file. What |
| 392 | /// the handler does with the events it receives is completely orthogonal to |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 393 | /// the business of parsing the bytecode and building the IR. This is used, |
| 394 | /// for example, by the llvm-abcd tool for analysis of byte code. |
| 395 | /// @brief Handler for parsing events. |
| 396 | BytecodeHandler* Handler; |
| 397 | |
Reid Spencer | 3e59546 | 2006-01-19 06:57:58 +0000 | [diff] [blame^] | 398 | |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 399 | /// @} |
| 400 | /// @name Implementation Details |
| 401 | /// @{ |
| 402 | private: |
| 403 | /// @brief Determines if this module has a function or not. |
| 404 | bool hasFunctions() { return ! FunctionSignatureList.empty(); } |
| 405 | |
| 406 | /// @brief Determines if the type id has an implicit null value. |
| 407 | bool hasImplicitNull(unsigned TyID ); |
| 408 | |
| 409 | /// @brief Converts a type slot number to its Type* |
| 410 | const Type *getType(unsigned ID); |
| 411 | |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 412 | /// @brief Converts a pre-sanitized type slot number to its Type* and |
| 413 | /// sanitizes the type id. |
| 414 | inline const Type* getSanitizedType(unsigned& ID ); |
| 415 | |
| 416 | /// @brief Read in and get a sanitized type id |
Chris Lattner | 1992522 | 2004-11-15 21:55:06 +0000 | [diff] [blame] | 417 | inline const Type* readSanitizedType(); |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 418 | |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 419 | /// @brief Converts a Type* to its type slot number |
| 420 | unsigned getTypeSlot(const Type *Ty); |
| 421 | |
| 422 | /// @brief Converts a normal type slot number to a compacted type slot num. |
| 423 | unsigned getCompactionTypeSlot(unsigned type); |
| 424 | |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 425 | /// @brief Gets the global type corresponding to the TypeId |
| 426 | const Type *getGlobalTableType(unsigned TypeId); |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 427 | |
| 428 | /// This is just like getTypeSlot, but when a compaction table is in use, |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 429 | /// it is ignored. |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 430 | unsigned getGlobalTableTypeSlot(const Type *Ty); |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 431 | |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 432 | /// @brief Get a value from its typeid and slot number |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 433 | Value* getValue(unsigned TypeID, unsigned num, bool Create = true); |
| 434 | |
Chris Lattner | 2c6c14d | 2004-08-04 00:19:23 +0000 | [diff] [blame] | 435 | /// @brief Get a value from its type and slot number, ignoring compaction |
| 436 | /// tables. |
| 437 | Value *getGlobalTableValue(unsigned TyID, unsigned SlotNo); |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 438 | |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 439 | /// @brief Get a basic block for current function |
| 440 | BasicBlock *getBasicBlock(unsigned ID); |
| 441 | |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 442 | /// @brief Get a constant value from its typeid and value slot. |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 443 | Constant* getConstantValue(unsigned typeSlot, unsigned valSlot); |
| 444 | |
| 445 | /// @brief Convenience function for getting a constant value when |
| 446 | /// the Type has already been resolved. |
| 447 | Constant* getConstantValue(const Type *Ty, unsigned valSlot) { |
| 448 | return getConstantValue(getTypeSlot(Ty), valSlot); |
| 449 | } |
| 450 | |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 451 | /// @brief Insert a newly created value |
| 452 | unsigned insertValue(Value *V, unsigned Type, ValueTable &Table); |
| 453 | |
| 454 | /// @brief Insert the arguments of a function. |
| 455 | void insertArguments(Function* F ); |
| 456 | |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 457 | /// @brief Resolve all references to the placeholder (if any) for the |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 458 | /// given constant. |
Chris Lattner | 389bd04 | 2004-12-09 06:19:44 +0000 | [diff] [blame] | 459 | void ResolveReferencesToConstant(Constant *C, unsigned Typ, unsigned Slot); |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 460 | |
| 461 | /// @brief Release our memory. |
| 462 | void freeState() { |
| 463 | freeTable(FunctionValues); |
| 464 | freeTable(ModuleValues); |
| 465 | } |
| 466 | |
| 467 | /// @brief Free a table, making sure to free the ValueList in the table. |
| 468 | void freeTable(ValueTable &Tab) { |
| 469 | while (!Tab.empty()) { |
| 470 | delete Tab.back(); |
| 471 | Tab.pop_back(); |
| 472 | } |
| 473 | } |
| 474 | |
Reid Spencer | 2439972 | 2004-07-09 22:21:33 +0000 | [diff] [blame] | 475 | inline void error(std::string errmsg); |
| 476 | |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 477 | BytecodeReader(const BytecodeReader &); // DO NOT IMPLEMENT |
| 478 | void operator=(const BytecodeReader &); // DO NOT IMPLEMENT |
| 479 | |
| 480 | /// @} |
| 481 | /// @name Reader Primitives |
| 482 | /// @{ |
| 483 | private: |
| 484 | |
| 485 | /// @brief Is there more to parse in the current block? |
| 486 | inline bool moreInBlock(); |
| 487 | |
| 488 | /// @brief Have we read past the end of the block |
| 489 | inline void checkPastBlockEnd(const char * block_name); |
| 490 | |
| 491 | /// @brief Align to 32 bits |
| 492 | inline void align32(); |
| 493 | |
| 494 | /// @brief Read an unsigned integer as 32-bits |
| 495 | inline unsigned read_uint(); |
| 496 | |
| 497 | /// @brief Read an unsigned integer with variable bit rate encoding |
| 498 | inline unsigned read_vbr_uint(); |
| 499 | |
Reid Spencer | ad89bd6 | 2004-07-25 18:07:36 +0000 | [diff] [blame] | 500 | /// @brief Read an unsigned integer of no more than 24-bits with variable |
| 501 | /// bit rate encoding. |
| 502 | inline unsigned read_vbr_uint24(); |
| 503 | |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 504 | /// @brief Read an unsigned 64-bit integer with variable bit rate encoding. |
| 505 | inline uint64_t read_vbr_uint64(); |
| 506 | |
| 507 | /// @brief Read a signed 64-bit integer with variable bit rate encoding. |
| 508 | inline int64_t read_vbr_int64(); |
| 509 | |
| 510 | /// @brief Read a string |
| 511 | inline std::string read_str(); |
| 512 | |
Reid Spencer | 6690651 | 2004-07-11 17:24:05 +0000 | [diff] [blame] | 513 | /// @brief Read a float value |
| 514 | inline void read_float(float& FloatVal); |
| 515 | |
| 516 | /// @brief Read a double value |
| 517 | inline void read_double(double& DoubleVal); |
| 518 | |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 519 | /// @brief Read an arbitrary data chunk of fixed length |
| 520 | inline void read_data(void *Ptr, void *End); |
| 521 | |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 522 | /// @brief Read a bytecode block header |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 523 | inline void read_block(unsigned &Type, unsigned &Size); |
| 524 | |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 525 | /// @brief Read a type identifier and sanitize it. |
| 526 | inline bool read_typeid(unsigned &TypeId); |
| 527 | |
| 528 | /// @brief Recalculate type ID for pre 1.3 bytecode files. |
| 529 | inline bool sanitizeTypeId(unsigned &TypeId ); |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 530 | /// @} |
| 531 | }; |
| 532 | |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 533 | /// @brief A function for creating a BytecodeAnalzer as a handler |
| 534 | /// for the Bytecode reader. |
Misha Brukman | 8a96c53 | 2005-04-21 21:44:41 +0000 | [diff] [blame] | 535 | BytecodeHandler* createBytecodeAnalyzerHandler(BytecodeAnalysis& bca, |
Reid Spencer | 572c256 | 2004-08-21 20:50:49 +0000 | [diff] [blame] | 536 | std::ostream* output ); |
Reid Spencer | a86159c | 2004-07-04 11:04:56 +0000 | [diff] [blame] | 537 | |
| 538 | |
Reid Spencer | f89143c | 2004-06-29 23:31:01 +0000 | [diff] [blame] | 539 | } // End llvm namespace |
| 540 | |
| 541 | // vim: sw=2 |
| 542 | #endif |