| OLD | NEW |
| 1 //===- NaClBitcodeReader.h ------------------------------------*- C++ -*-===// | 1 //===- NaClBitcodeReader.h ------------------------------------*- C++ -*-===// |
| 2 // Internal NaClBitcodeReader implementation | 2 // Internal NaClBitcodeReader implementation |
| 3 // | 3 // |
| 4 // The LLVM Compiler Infrastructure | 4 // The LLVM Compiler Infrastructure |
| 5 // | 5 // |
| 6 // This file is distributed under the University of Illinois Open Source | 6 // This file is distributed under the University of Illinois Open Source |
| 7 // License. See LICENSE.TXT for details. | 7 // License. See LICENSE.TXT for details. |
| 8 // | 8 // |
| 9 //===----------------------------------------------------------------------===// | 9 //===----------------------------------------------------------------------===// |
| 10 // | 10 // |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 // Assigns Idx to the given value, overwriting the existing entry | 121 // Assigns Idx to the given value, overwriting the existing entry |
| 122 // and possibly modifying the type of the entry. | 122 // and possibly modifying the type of the entry. |
| 123 void OverwriteValue(Value *V, unsigned Idx); | 123 void OverwriteValue(Value *V, unsigned Idx); |
| 124 }; | 124 }; |
| 125 | 125 |
| 126 | 126 |
| 127 class NaClBitcodeReader : public GVMaterializer { | 127 class NaClBitcodeReader : public GVMaterializer { |
| 128 NaClBitcodeHeader Header; // Header fields of the PNaCl bitcode file. | 128 NaClBitcodeHeader Header; // Header fields of the PNaCl bitcode file. |
| 129 LLVMContext &Context; | 129 LLVMContext &Context; |
| 130 Module *TheModule; | 130 Module *TheModule; |
| 131 // If non-null, stream to write verbose errors to. |
| 132 raw_ostream *Verbose; |
| 131 PNaClAllowedIntrinsics AllowedIntrinsics; | 133 PNaClAllowedIntrinsics AllowedIntrinsics; |
| 132 std::unique_ptr<MemoryBuffer> Buffer; | 134 std::unique_ptr<MemoryBuffer> Buffer; |
| 133 std::unique_ptr<NaClBitstreamReader> StreamFile; | 135 std::unique_ptr<NaClBitstreamReader> StreamFile; |
| 134 NaClBitstreamCursor Stream; | 136 NaClBitstreamCursor Stream; |
| 135 StreamingMemoryObject *LazyStreamer; | 137 StreamingMemoryObject *LazyStreamer; |
| 136 uint64_t NextUnreadBit; | 138 uint64_t NextUnreadBit; |
| 137 bool SeenValueSymbolTable; | 139 bool SeenValueSymbolTable; |
| 138 | |
| 139 std::string ErrorString; | |
| 140 | |
| 141 std::vector<Type*> TypeList; | 140 std::vector<Type*> TypeList; |
| 142 NaClBitcodeReaderValueList ValueList; | 141 NaClBitcodeReaderValueList ValueList; |
| 143 | 142 |
| 144 // Holds information about each BasicBlock in the function being read. | 143 // Holds information about each BasicBlock in the function being read. |
| 145 struct BasicBlockInfo { | 144 struct BasicBlockInfo { |
| 146 // A basic block within the function being modeled. | 145 // A basic block within the function being modeled. |
| 147 BasicBlock *BB; | 146 BasicBlock *BB; |
| 148 // The set of generated conversions. | 147 // The set of generated conversions. |
| 149 DenseMap<NaClBitcodeReaderCast, CastInst*> CastMap; | 148 DenseMap<NaClBitcodeReaderCast, CastInst*> CastMap; |
| 150 // The set of generated conversions that were added for phi nodes, | 149 // The set of generated conversions that were added for phi nodes, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 174 /// map contains info about where to find deferred function body in the | 173 /// map contains info about where to find deferred function body in the |
| 175 /// stream. | 174 /// stream. |
| 176 DenseMap<Function*, uint64_t> DeferredFunctionInfo; | 175 DenseMap<Function*, uint64_t> DeferredFunctionInfo; |
| 177 | 176 |
| 178 /// \brief True if we should only accept supported bitcode format. | 177 /// \brief True if we should only accept supported bitcode format. |
| 179 bool AcceptSupportedBitcodeOnly; | 178 bool AcceptSupportedBitcodeOnly; |
| 180 | 179 |
| 181 /// \brief Integer type use for PNaCl conversion of pointers. | 180 /// \brief Integer type use for PNaCl conversion of pointers. |
| 182 Type *IntPtrType; | 181 Type *IntPtrType; |
| 183 | 182 |
| 183 static const std::error_category &BitcodeErrorCategory(); |
| 184 |
| 184 public: | 185 public: |
| 186 |
| 187 /// Types of errors reported. |
| 188 enum ErrorType { |
| 189 CouldNotFindFunctionInStream, // Unable to find function in bitcode stream. |
| 190 InsufficientFunctionProtos, |
| 191 InvalidBitstream, // Error in bitstream format. |
| 192 InvalidConstantReference, // Bad constant reference. |
| 193 InvalidInstructionWithNoBB, // No basic block for instruction. |
| 194 InvalidMultipleBlocks, // Multiple blocks for a kind of block that should |
| 195 // have only one. |
| 196 InvalidRecord, // Record doesn't have expected size or structure. |
| 197 InvalidSkippedBlock, // Unable to skip unknown block in bitcode file. |
| 198 InvalidType, // Invalid type in record. |
| 199 InvalidTypeForValue, // Type of value incorrect. |
| 200 InvalidValue, // Invalid value in record. |
| 201 MalformedBlock, // Unable to advance over block. |
| 202 }; |
| 203 |
| 185 explicit NaClBitcodeReader(MemoryBuffer *buffer, LLVMContext &C, | 204 explicit NaClBitcodeReader(MemoryBuffer *buffer, LLVMContext &C, |
| 186 bool AcceptSupportedOnly = true) | 205 raw_ostream *Verbose, |
| 187 : Context(C), TheModule(0), AllowedIntrinsics(&C), | 206 bool AcceptSupportedOnly) |
| 207 : Context(C), TheModule(nullptr), Verbose(Verbose), AllowedIntrinsics(&C), |
| 188 Buffer(buffer), | 208 Buffer(buffer), |
| 189 LazyStreamer(0), NextUnreadBit(0), SeenValueSymbolTable(false), | 209 LazyStreamer(nullptr), NextUnreadBit(0), SeenValueSymbolTable(false), |
| 190 ValueList(), | 210 ValueList(), |
| 191 SeenFirstFunctionBody(false), | 211 SeenFirstFunctionBody(false), |
| 192 AcceptSupportedBitcodeOnly(AcceptSupportedOnly), | 212 AcceptSupportedBitcodeOnly(AcceptSupportedOnly), |
| 193 IntPtrType(IntegerType::get(C, PNaClIntPtrTypeBitSize)) { | 213 IntPtrType(IntegerType::get(C, PNaClIntPtrTypeBitSize)) { |
| 194 } | 214 } |
| 195 explicit NaClBitcodeReader(StreamingMemoryObject *streamer, | 215 explicit NaClBitcodeReader(StreamingMemoryObject *streamer, |
| 196 LLVMContext &C, | 216 LLVMContext &C, |
| 197 bool AcceptSupportedOnly = true) | 217 raw_ostream *Verbose, |
| 198 : Context(C), TheModule(0), AllowedIntrinsics(&C), | 218 bool AcceptSupportedOnly) |
| 219 : Context(C), TheModule(nullptr), Verbose(Verbose), AllowedIntrinsics(&C), |
| 199 Buffer(nullptr), | 220 Buffer(nullptr), |
| 200 LazyStreamer(streamer), NextUnreadBit(0), SeenValueSymbolTable(false), | 221 LazyStreamer(streamer), NextUnreadBit(0), SeenValueSymbolTable(false), |
| 201 ValueList(), | 222 ValueList(), |
| 202 SeenFirstFunctionBody(false), | 223 SeenFirstFunctionBody(false), |
| 203 AcceptSupportedBitcodeOnly(AcceptSupportedOnly), | 224 AcceptSupportedBitcodeOnly(AcceptSupportedOnly), |
| 204 IntPtrType(IntegerType::get(C, PNaClIntPtrTypeBitSize)) { | 225 IntPtrType(IntegerType::get(C, PNaClIntPtrTypeBitSize)) { |
| 205 } | 226 } |
| 206 ~NaClBitcodeReader() override { | 227 ~NaClBitcodeReader() override { |
| 207 FreeState(); | 228 FreeState(); |
| 208 } | 229 } |
| 209 | 230 |
| 210 void FreeState(); | 231 void FreeState(); |
| 211 | 232 |
| 212 bool isMaterializable(const GlobalValue *GV) const override; | 233 bool isMaterializable(const GlobalValue *GV) const override; |
| 213 bool isDematerializable(const GlobalValue *GV) const override; | 234 bool isDematerializable(const GlobalValue *GV) const override; |
| 214 std::error_code Materialize(GlobalValue *GV) override; | 235 std::error_code Materialize(GlobalValue *GV) override; |
| 215 std::error_code MaterializeModule(Module *M) override; | 236 std::error_code MaterializeModule(Module *M) override; |
| 216 void Dematerialize(GlobalValue *GV) override; | 237 void Dematerialize(GlobalValue *GV) override; |
| 217 void releaseBuffer() override; | 238 void releaseBuffer() override; |
| 218 | 239 |
| 219 bool Error(const std::string &Str) { | 240 std::error_code Error(ErrorType E) const { |
| 220 ErrorString = Str; | 241 return std::error_code(E, BitcodeErrorCategory()); |
| 221 return true; | |
| 222 } | 242 } |
| 223 const std::string &getErrorString() const { return ErrorString; } | 243 |
| 244 /// Generates the corresponding verbose Message, then generates error. |
| 245 std::error_code Error(ErrorType E, const std::string &Message) const; |
| 224 | 246 |
| 225 /// @brief Main interface to parsing a bitcode buffer. | 247 /// @brief Main interface to parsing a bitcode buffer. |
| 226 /// @returns true if an error occurred. | 248 /// @returns true if an error occurred. |
| 227 bool ParseBitcodeInto(Module *M); | 249 std::error_code ParseBitcodeInto(Module *M); |
| 228 | 250 |
| 229 private: | 251 private: |
| 230 // Returns false if Header is acceptable. | 252 // Returns false if Header is acceptable. |
| 231 bool AcceptHeader() const { | 253 bool AcceptHeader() const { |
| 232 return !(Header.IsSupported() || | 254 return !(Header.IsSupported() || |
| 233 (!AcceptSupportedBitcodeOnly && Header.IsReadable())); | 255 (!AcceptSupportedBitcodeOnly && Header.IsReadable())); |
| 234 } | 256 } |
| 235 uint32_t GetPNaClVersion() const { | 257 uint32_t GetPNaClVersion() const { |
| 236 return Header.GetPNaClVersion(); | 258 return Header.GetPNaClVersion(); |
| 237 } | 259 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 Value *ConvertOpToType(Value *Op, Type *T, unsigned BBIndex); | 319 Value *ConvertOpToType(Value *Op, Type *T, unsigned BBIndex); |
| 298 | 320 |
| 299 /// \brief If Op is a scalar value, this is a nop. If Op is a | 321 /// \brief If Op is a scalar value, this is a nop. If Op is a |
| 300 /// pointer value, a PtrToInt instruction is inserted (in BBIndex) | 322 /// pointer value, a PtrToInt instruction is inserted (in BBIndex) |
| 301 /// to convert Op to an integer. For defaults on DeferInsertion, | 323 /// to convert Op to an integer. For defaults on DeferInsertion, |
| 302 /// see comments for method CreateCast. | 324 /// see comments for method CreateCast. |
| 303 Value *ConvertOpToScalar(Value *Op, unsigned BBIndex, | 325 Value *ConvertOpToScalar(Value *Op, unsigned BBIndex, |
| 304 bool DeferInsertion = false); | 326 bool DeferInsertion = false); |
| 305 | 327 |
| 306 /// \brief Install instruction I into basic block BB. | 328 /// \brief Install instruction I into basic block BB. |
| 307 bool InstallInstruction(BasicBlock *BB, Instruction *I); | 329 std::error_code InstallInstruction(BasicBlock *BB, Instruction *I); |
| 308 | 330 |
| 309 FunctionType *AddPointerTypesToIntrinsicType(StringRef Name, | 331 FunctionType *AddPointerTypesToIntrinsicType(StringRef Name, |
| 310 FunctionType *FTy); | 332 FunctionType *FTy); |
| 311 void AddPointerTypesToIntrinsicParams(); | 333 void AddPointerTypesToIntrinsicParams(); |
| 312 bool ParseModule(bool Resume); | 334 std::error_code ParseModule(bool Resume); |
| 313 bool ParseTypeTable(); | 335 std::error_code ParseTypeTable(); |
| 314 bool ParseTypeTableBody(); | 336 std::error_code ParseTypeTableBody(); |
| 315 bool ParseGlobalVars(); | 337 std::error_code ParseGlobalVars(); |
| 316 bool ParseValueSymbolTable(); | 338 std::error_code ParseValueSymbolTable(); |
| 317 bool ParseConstants(); | 339 std::error_code ParseConstants(); |
| 318 bool RememberAndSkipFunctionBody(); | 340 std::error_code RememberAndSkipFunctionBody(); |
| 319 bool ParseFunctionBody(Function *F); | 341 std::error_code ParseFunctionBody(Function *F); |
| 320 bool GlobalCleanup(); | 342 std::error_code GlobalCleanup(); |
| 321 bool InitStream(); | 343 std::error_code InitStream(); |
| 322 bool InitStreamFromBuffer(); | 344 std::error_code InitStreamFromBuffer(); |
| 323 bool InitLazyStream(); | 345 std::error_code InitLazyStream(); |
| 324 bool FindFunctionInStream(Function *F, | 346 std::error_code FindFunctionInStream( |
| 325 DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator); | 347 Function *F, |
| 348 DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator); |
| 326 }; | 349 }; |
| 327 | 350 |
| 328 } // End llvm namespace | 351 } // End llvm namespace |
| 329 | 352 |
| 330 #endif | 353 #endif |
| OLD | NEW |