Chromium Code Reviews| 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 bool Verbose; // True if error messages should be printed to errs() | |
| 131 PNaClAllowedIntrinsics AllowedIntrinsics; | 132 PNaClAllowedIntrinsics AllowedIntrinsics; |
| 132 std::unique_ptr<MemoryBuffer> Buffer; | 133 std::unique_ptr<MemoryBuffer> Buffer; |
| 133 std::unique_ptr<NaClBitstreamReader> StreamFile; | 134 std::unique_ptr<NaClBitstreamReader> StreamFile; |
| 134 NaClBitstreamCursor Stream; | 135 NaClBitstreamCursor Stream; |
| 135 StreamingMemoryObject *LazyStreamer; | 136 StreamingMemoryObject *LazyStreamer; |
| 136 uint64_t NextUnreadBit; | 137 uint64_t NextUnreadBit; |
| 137 bool SeenValueSymbolTable; | 138 bool SeenValueSymbolTable; |
| 138 | |
| 139 std::string ErrorString; | |
| 140 | |
| 141 std::vector<Type*> TypeList; | 139 std::vector<Type*> TypeList; |
| 142 NaClBitcodeReaderValueList ValueList; | 140 NaClBitcodeReaderValueList ValueList; |
| 143 | 141 |
| 144 // Holds information about each BasicBlock in the function being read. | 142 // Holds information about each BasicBlock in the function being read. |
| 145 struct BasicBlockInfo { | 143 struct BasicBlockInfo { |
| 146 // A basic block within the function being modeled. | 144 // A basic block within the function being modeled. |
| 147 BasicBlock *BB; | 145 BasicBlock *BB; |
| 148 // The set of generated conversions. | 146 // The set of generated conversions. |
| 149 DenseMap<NaClBitcodeReaderCast, CastInst*> CastMap; | 147 DenseMap<NaClBitcodeReaderCast, CastInst*> CastMap; |
| 150 // The set of generated conversions that were added for phi nodes, | 148 // 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 | 172 /// map contains info about where to find deferred function body in the |
| 175 /// stream. | 173 /// stream. |
| 176 DenseMap<Function*, uint64_t> DeferredFunctionInfo; | 174 DenseMap<Function*, uint64_t> DeferredFunctionInfo; |
| 177 | 175 |
| 178 /// \brief True if we should only accept supported bitcode format. | 176 /// \brief True if we should only accept supported bitcode format. |
| 179 bool AcceptSupportedBitcodeOnly; | 177 bool AcceptSupportedBitcodeOnly; |
| 180 | 178 |
| 181 /// \brief Integer type use for PNaCl conversion of pointers. | 179 /// \brief Integer type use for PNaCl conversion of pointers. |
| 182 Type *IntPtrType; | 180 Type *IntPtrType; |
| 183 | 181 |
| 182 static const std::error_category &BitcodeErrorCategory(); | |
| 183 | |
| 184 public: | 184 public: |
| 185 | |
| 186 /// Types of errors reported. | |
| 187 enum ErrorType { | |
| 188 InvalidBBForInstruction, // No basic block for instruction. | |
|
jvoung (off chromium)
2014/12/01 23:23:50
Is this the same as InvalidInstructionWithNoBB in
Karl
2014/12/03 18:32:09
Yes. I didn't like the name. However, it probably
| |
| 189 InvalidBitstream, // Error in bitstream format. | |
| 190 InvalidConstantReference, // Bad constant reference. | |
| 191 InvalidMultipleBlocks, // Multiple blocks for a kind of block that should | |
| 192 // have only one. | |
| 193 InvalidRecord, // Record doesn't have expected size or structure. | |
| 194 InvalidType, // Invalid type in record. | |
| 195 InvalidTypeForValue, // Type of value incorrect. | |
| 196 InvalidValue, // Invalid value in record. | |
| 197 MalformedBlock, // Unable to advance over block. | |
| 198 NoFunctionInStream, // Unable to find function in bitcode stream. | |
|
jvoung (off chromium)
2014/12/01 23:23:50
Is this the same as "CouldNotFindFunctionInStream"
Karl
2014/12/03 18:32:09
Like above, changing to CouldNotFindFunctionInStre
| |
| 199 }; | |
| 200 | |
| 185 explicit NaClBitcodeReader(MemoryBuffer *buffer, LLVMContext &C, | 201 explicit NaClBitcodeReader(MemoryBuffer *buffer, LLVMContext &C, |
| 186 bool AcceptSupportedOnly = true) | 202 bool Verbose, |
| 187 : Context(C), TheModule(0), AllowedIntrinsics(&C), | 203 bool AcceptSupportedOnly) |
| 204 : Context(C), TheModule(0), Verbose(Verbose), AllowedIntrinsics(&C), | |
|
jvoung (off chromium)
2014/12/01 23:23:49
nullptr instead of 0
Karl
2014/12/03 18:32:09
Done.
| |
| 188 Buffer(buffer), | 205 Buffer(buffer), |
| 189 LazyStreamer(0), NextUnreadBit(0), SeenValueSymbolTable(false), | 206 LazyStreamer(0), NextUnreadBit(0), SeenValueSymbolTable(false), |
|
jvoung (off chromium)
2014/12/01 23:23:49
LazyStreamer(nullptr)
Karl
2014/12/03 18:32:09
Done.
| |
| 190 ValueList(), | 207 ValueList(), |
| 191 SeenFirstFunctionBody(false), | 208 SeenFirstFunctionBody(false), |
| 192 AcceptSupportedBitcodeOnly(AcceptSupportedOnly), | 209 AcceptSupportedBitcodeOnly(AcceptSupportedOnly), |
| 193 IntPtrType(IntegerType::get(C, PNaClIntPtrTypeBitSize)) { | 210 IntPtrType(IntegerType::get(C, PNaClIntPtrTypeBitSize)) { |
| 194 } | 211 } |
| 195 explicit NaClBitcodeReader(StreamingMemoryObject *streamer, | 212 explicit NaClBitcodeReader(StreamingMemoryObject *streamer, |
| 196 LLVMContext &C, | 213 LLVMContext &C, |
| 197 bool AcceptSupportedOnly = true) | 214 bool Verbose, |
| 198 : Context(C), TheModule(0), AllowedIntrinsics(&C), | 215 bool AcceptSupportedOnly) |
| 216 : Context(C), TheModule(0), Verbose(Verbose), AllowedIntrinsics(&C), | |
|
jvoung (off chromium)
2014/12/01 23:23:50
TheModule(nullptr), etc.
Karl
2014/12/03 18:32:09
Done.
| |
| 199 Buffer(nullptr), | 217 Buffer(nullptr), |
| 200 LazyStreamer(streamer), NextUnreadBit(0), SeenValueSymbolTable(false), | 218 LazyStreamer(streamer), NextUnreadBit(0), SeenValueSymbolTable(false), |
| 201 ValueList(), | 219 ValueList(), |
| 202 SeenFirstFunctionBody(false), | 220 SeenFirstFunctionBody(false), |
| 203 AcceptSupportedBitcodeOnly(AcceptSupportedOnly), | 221 AcceptSupportedBitcodeOnly(AcceptSupportedOnly), |
| 204 IntPtrType(IntegerType::get(C, PNaClIntPtrTypeBitSize)) { | 222 IntPtrType(IntegerType::get(C, PNaClIntPtrTypeBitSize)) { |
| 205 } | 223 } |
| 206 ~NaClBitcodeReader() override { | 224 ~NaClBitcodeReader() override { |
| 207 FreeState(); | 225 FreeState(); |
| 208 } | 226 } |
| 209 | 227 |
| 210 void FreeState(); | 228 void FreeState(); |
| 211 | 229 |
| 212 bool isMaterializable(const GlobalValue *GV) const override; | 230 bool isMaterializable(const GlobalValue *GV) const override; |
| 213 bool isDematerializable(const GlobalValue *GV) const override; | 231 bool isDematerializable(const GlobalValue *GV) const override; |
| 214 std::error_code Materialize(GlobalValue *GV) override; | 232 std::error_code Materialize(GlobalValue *GV) override; |
| 215 std::error_code MaterializeModule(Module *M) override; | 233 std::error_code MaterializeModule(Module *M) override; |
| 216 void Dematerialize(GlobalValue *GV) override; | 234 void Dematerialize(GlobalValue *GV) override; |
| 217 void releaseBuffer() override; | 235 void releaseBuffer() override; |
| 218 | 236 |
| 219 bool Error(const std::string &Str) { | 237 std::error_code Error(ErrorType E) const { |
| 220 ErrorString = Str; | 238 return std::error_code(E, BitcodeErrorCategory()); |
| 221 return true; | |
| 222 } | 239 } |
| 223 const std::string &getErrorString() const { return ErrorString; } | 240 |
| 241 /// Generates the corresponding verbose Message, then generates error. | |
| 242 std::error_code Error(ErrorType E, const std::string &Message) const; | |
| 224 | 243 |
| 225 /// @brief Main interface to parsing a bitcode buffer. | 244 /// @brief Main interface to parsing a bitcode buffer. |
| 226 /// @returns true if an error occurred. | 245 /// @returns true if an error occurred. |
| 227 bool ParseBitcodeInto(Module *M); | 246 std::error_code ParseBitcodeInto(Module *M); |
| 228 | 247 |
| 229 private: | 248 private: |
| 230 // Returns false if Header is acceptable. | 249 // Returns false if Header is acceptable. |
| 231 bool AcceptHeader() const { | 250 bool AcceptHeader() const { |
| 232 return !(Header.IsSupported() || | 251 return !(Header.IsSupported() || |
| 233 (!AcceptSupportedBitcodeOnly && Header.IsReadable())); | 252 (!AcceptSupportedBitcodeOnly && Header.IsReadable())); |
| 234 } | 253 } |
| 235 uint32_t GetPNaClVersion() const { | 254 uint32_t GetPNaClVersion() const { |
| 236 return Header.GetPNaClVersion(); | 255 return Header.GetPNaClVersion(); |
| 237 } | 256 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 297 Value *ConvertOpToType(Value *Op, Type *T, unsigned BBIndex); | 316 Value *ConvertOpToType(Value *Op, Type *T, unsigned BBIndex); |
| 298 | 317 |
| 299 /// \brief If Op is a scalar value, this is a nop. If Op is a | 318 /// \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) | 319 /// pointer value, a PtrToInt instruction is inserted (in BBIndex) |
| 301 /// to convert Op to an integer. For defaults on DeferInsertion, | 320 /// to convert Op to an integer. For defaults on DeferInsertion, |
| 302 /// see comments for method CreateCast. | 321 /// see comments for method CreateCast. |
| 303 Value *ConvertOpToScalar(Value *Op, unsigned BBIndex, | 322 Value *ConvertOpToScalar(Value *Op, unsigned BBIndex, |
| 304 bool DeferInsertion = false); | 323 bool DeferInsertion = false); |
| 305 | 324 |
| 306 /// \brief Install instruction I into basic block BB. | 325 /// \brief Install instruction I into basic block BB. |
| 307 bool InstallInstruction(BasicBlock *BB, Instruction *I); | 326 std::error_code InstallInstruction(BasicBlock *BB, Instruction *I); |
| 308 | 327 |
| 309 FunctionType *AddPointerTypesToIntrinsicType(StringRef Name, | 328 FunctionType *AddPointerTypesToIntrinsicType(StringRef Name, |
| 310 FunctionType *FTy); | 329 FunctionType *FTy); |
| 311 void AddPointerTypesToIntrinsicParams(); | 330 void AddPointerTypesToIntrinsicParams(); |
| 312 bool ParseModule(bool Resume); | 331 std::error_code ParseModule(bool Resume); |
| 313 bool ParseTypeTable(); | 332 std::error_code ParseTypeTable(); |
| 314 bool ParseTypeTableBody(); | 333 std::error_code ParseTypeTableBody(); |
| 315 bool ParseGlobalVars(); | 334 std::error_code ParseGlobalVars(); |
| 316 bool ParseValueSymbolTable(); | 335 std::error_code ParseValueSymbolTable(); |
| 317 bool ParseConstants(); | 336 std::error_code ParseConstants(); |
| 318 bool RememberAndSkipFunctionBody(); | 337 std::error_code RememberAndSkipFunctionBody(); |
| 319 bool ParseFunctionBody(Function *F); | 338 std::error_code ParseFunctionBody(Function *F); |
| 320 bool GlobalCleanup(); | 339 std::error_code GlobalCleanup(); |
| 321 bool InitStream(); | 340 std::error_code InitStream(); |
| 322 bool InitStreamFromBuffer(); | 341 std::error_code InitStreamFromBuffer(); |
| 323 bool InitLazyStream(); | 342 std::error_code InitLazyStream(); |
| 324 bool FindFunctionInStream(Function *F, | 343 std::error_code FindFunctionInStream( |
| 325 DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator); | 344 Function *F, |
| 345 DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator); | |
| 326 }; | 346 }; |
| 327 | 347 |
| 328 } // End llvm namespace | 348 } // End llvm namespace |
| 329 | 349 |
|
JF
2014/12/02 00:49:04
It's kind of ugly, but ReaderWriter.h also impleme
Karl
2014/12/03 18:32:09
Not in our local code base yet.
| |
| 330 #endif | 350 #endif |
| OLD | NEW |