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