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