OLD | NEW |
1 //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===// | 1 //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===// |
2 // | 2 // |
3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This file implements the PNaCl bitcode file to Ice, to machine code | 10 // This file implements the PNaCl bitcode file to Ice, to machine code |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 TopLevelParser &operator=(const TopLevelParser &) = delete; | 160 TopLevelParser &operator=(const TopLevelParser &) = delete; |
161 | 161 |
162 public: | 162 public: |
163 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType; | 163 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType; |
164 | 164 |
165 TopLevelParser(Ice::Translator &Translator, NaClBitstreamCursor &Cursor, | 165 TopLevelParser(Ice::Translator &Translator, NaClBitstreamCursor &Cursor, |
166 Ice::ErrorCode &ErrorStatus) | 166 Ice::ErrorCode &ErrorStatus) |
167 : NaClBitcodeParser(Cursor), Translator(Translator), | 167 : NaClBitcodeParser(Cursor), Translator(Translator), |
168 ErrorStatus(ErrorStatus), NumErrors(0), NextDefiningFunctionID(0), | 168 ErrorStatus(ErrorStatus), NumErrors(0), NextDefiningFunctionID(0), |
169 VariableDeclarations(new Ice::VariableDeclarationList()), | 169 VariableDeclarations(new Ice::VariableDeclarationList()), |
170 BlockParser(nullptr), StubbedConstCallValue(nullptr) {} | 170 BlockParser(nullptr) {} |
171 | 171 |
172 ~TopLevelParser() override {} | 172 ~TopLevelParser() override {} |
173 | 173 |
174 Ice::Translator &getTranslator() const { return Translator; } | 174 Ice::Translator &getTranslator() const { return Translator; } |
175 | 175 |
176 void setBlockParser(BlockParserBaseClass *NewBlockParser) { | 176 void setBlockParser(BlockParserBaseClass *NewBlockParser) { |
177 BlockParser = NewBlockParser; | 177 BlockParser = NewBlockParser; |
178 } | 178 } |
179 | 179 |
180 /// Generates error with given Message, occurring at BitPosition | 180 /// Generates error with given Message, occurring at BitPosition |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 } | 269 } |
270 | 270 |
271 void createValueIDs() { | 271 void createValueIDs() { |
272 assert(VariableDeclarations); | 272 assert(VariableDeclarations); |
273 ValueIDConstants.reserve(VariableDeclarations->size() + | 273 ValueIDConstants.reserve(VariableDeclarations->size() + |
274 FunctionDeclarationList.size()); | 274 FunctionDeclarationList.size()); |
275 createValueIDsForFunctions(); | 275 createValueIDsForFunctions(); |
276 createValueIDsForGlobalVars(); | 276 createValueIDsForGlobalVars(); |
277 } | 277 } |
278 | 278 |
279 /// Returns a defined function reference to be used in place of | |
280 /// called constant addresses. Returns the corresponding operand | |
281 /// to replace the calling address with. Reports an error if | |
282 /// a stub could not be found, returning the CallValue. | |
283 Ice::Operand *getStubbedConstCallValue(Ice::Operand *CallValue) { | |
284 if (StubbedConstCallValue) | |
285 return StubbedConstCallValue; | |
286 for (unsigned i = 0; i < getNumFunctionIDs(); ++i) { | |
287 Ice::FunctionDeclaration *Func = getFunctionByID(i); | |
288 if (!Func->isProto()) { | |
289 StubbedConstCallValue = getGlobalConstantByID(i); | |
290 return StubbedConstCallValue; | |
291 } | |
292 } | |
293 Error("Unable to find function definition to stub constant calls with"); | |
294 return CallValue; | |
295 } | |
296 | |
297 /// Returns the number of function declarations in the bitcode file. | 279 /// Returns the number of function declarations in the bitcode file. |
298 unsigned getNumFunctionIDs() const { return FunctionDeclarationList.size(); } | 280 unsigned getNumFunctionIDs() const { return FunctionDeclarationList.size(); } |
299 | 281 |
300 /// Returns the number of global declarations (i.e. IDs) defined in | 282 /// Returns the number of global declarations (i.e. IDs) defined in |
301 /// the bitcode file. | 283 /// the bitcode file. |
302 unsigned getNumGlobalIDs() const { | 284 unsigned getNumGlobalIDs() const { |
303 if (VariableDeclarations) { | 285 if (VariableDeclarations) { |
304 return FunctionDeclarationList.size() + VariableDeclarations->size(); | 286 return FunctionDeclarationList.size() + VariableDeclarations->size(); |
305 } else { | 287 } else { |
306 return ValueIDConstants.size(); | 288 return ValueIDConstants.size(); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 size_t NextDefiningFunctionID; | 358 size_t NextDefiningFunctionID; |
377 // The set of global variables. | 359 // The set of global variables. |
378 std::unique_ptr<Ice::VariableDeclarationList> VariableDeclarations; | 360 std::unique_ptr<Ice::VariableDeclarationList> VariableDeclarations; |
379 // Relocatable constants associated with global declarations. | 361 // Relocatable constants associated with global declarations. |
380 std::vector<Ice::Constant *> ValueIDConstants; | 362 std::vector<Ice::Constant *> ValueIDConstants; |
381 // Error recovery value to use when getFuncSigTypeByID fails. | 363 // Error recovery value to use when getFuncSigTypeByID fails. |
382 Ice::FuncSigType UndefinedFuncSigType; | 364 Ice::FuncSigType UndefinedFuncSigType; |
383 // The block parser currently being applied. Used for error | 365 // The block parser currently being applied. Used for error |
384 // reporting. | 366 // reporting. |
385 BlockParserBaseClass *BlockParser; | 367 BlockParserBaseClass *BlockParser; |
386 // Value to use to stub constant calls. | |
387 Ice::Operand *StubbedConstCallValue; | |
388 | 368 |
389 bool ParseBlock(unsigned BlockID) override; | 369 bool ParseBlock(unsigned BlockID) override; |
390 | 370 |
391 // Gets extended type associated with the given index, assuming the | 371 // Gets extended type associated with the given index, assuming the |
392 // extended type is of the WantedKind. Generates error message if | 372 // extended type is of the WantedKind. Generates error message if |
393 // corresponding extended type of WantedKind can't be found, and | 373 // corresponding extended type of WantedKind can't be found, and |
394 // returns nullptr. | 374 // returns nullptr. |
395 ExtendedType *getTypeByIDAsKind(unsigned ID, | 375 ExtendedType *getTypeByIDAsKind(unsigned ID, |
396 ExtendedType::TypeKind WantedKind) { | 376 ExtendedType::TypeKind WantedKind) { |
397 ExtendedType *Ty = nullptr; | 377 ExtendedType *Ty = nullptr; |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 | 483 |
504 bool TopLevelParser::ErrorAt(naclbitc::ErrorLevel Level, uint64_t Bit, | 484 bool TopLevelParser::ErrorAt(naclbitc::ErrorLevel Level, uint64_t Bit, |
505 const std::string &Message) { | 485 const std::string &Message) { |
506 ErrorStatus.assign(Ice::EC_Bitcode); | 486 ErrorStatus.assign(Ice::EC_Bitcode); |
507 ++NumErrors; | 487 ++NumErrors; |
508 Ice::GlobalContext *Context = Translator.getContext(); | 488 Ice::GlobalContext *Context = Translator.getContext(); |
509 Ice::OstreamLocker L(Context); | 489 Ice::OstreamLocker L(Context); |
510 raw_ostream &OldErrStream = setErrStream(Context->getStrDump()); | 490 raw_ostream &OldErrStream = setErrStream(Context->getStrDump()); |
511 NaClBitcodeParser::ErrorAt(Level, Bit, Message); | 491 NaClBitcodeParser::ErrorAt(Level, Bit, Message); |
512 setErrStream(OldErrStream); | 492 setErrStream(OldErrStream); |
513 if (Level >= naclbitc::Error | 493 if (Level >= naclbitc::Error && |
514 && !Translator.getFlags().getAllowErrorRecovery()) | 494 !Translator.getFlags().getAllowErrorRecovery()) |
515 Fatal(); | 495 Fatal(); |
516 return true; | 496 return true; |
517 } | 497 } |
518 | 498 |
519 void TopLevelParser::reportBadTypeIDAs(unsigned ID, const ExtendedType *Ty, | 499 void TopLevelParser::reportBadTypeIDAs(unsigned ID, const ExtendedType *Ty, |
520 ExtendedType::TypeKind WantedType) { | 500 ExtendedType::TypeKind WantedType) { |
521 std::string Buffer; | 501 std::string Buffer; |
522 raw_string_ostream StrBuf(Buffer); | 502 raw_string_ostream StrBuf(Buffer); |
523 if (Ty == nullptr) { | 503 if (Ty == nullptr) { |
524 StrBuf << "Can't find extended type for type id: " << ID; | 504 StrBuf << "Can't find extended type for type id: " << ID; |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
670 }; | 650 }; |
671 | 651 |
672 bool TopLevelParser::BlockError(const std::string &Message) { | 652 bool TopLevelParser::BlockError(const std::string &Message) { |
673 if (BlockParser) | 653 if (BlockParser) |
674 return BlockParser->Error(Message); | 654 return BlockParser->Error(Message); |
675 else | 655 else |
676 return Error(Message); | 656 return Error(Message); |
677 } | 657 } |
678 | 658 |
679 // Generates an error Message with the bit address prefixed to it. | 659 // Generates an error Message with the bit address prefixed to it. |
680 bool BlockParserBaseClass::ErrorAt( | 660 bool BlockParserBaseClass::ErrorAt(naclbitc::ErrorLevel Level, uint64_t Bit, |
681 naclbitc::ErrorLevel Level, uint64_t Bit, const std::string &Message) { | 661 const std::string &Message) { |
682 std::string Buffer; | 662 std::string Buffer; |
683 raw_string_ostream StrBuf(Buffer); | 663 raw_string_ostream StrBuf(Buffer); |
684 // Note: If dump routines have been turned off, the error messages | 664 // Note: If dump routines have been turned off, the error messages |
685 // will not be readable. Hence, replace with simple error. We also | 665 // will not be readable. Hence, replace with simple error. We also |
686 // use the simple form for unit tests. | 666 // use the simple form for unit tests. |
687 if (getFlags().getGenerateUnitTestMessages()) { | 667 if (getFlags().getGenerateUnitTestMessages()) { |
688 StrBuf << "Invalid " << getBlockName() << " record: <" << Record.GetCode(); | 668 StrBuf << "Invalid " << getBlockName() << " record: <" << Record.GetCode(); |
689 for (const uint64_t Val : Record.GetValues()) { | 669 for (const uint64_t Val : Record.GetValues()) { |
690 StrBuf << " " << Val; | 670 StrBuf << " " << Val; |
691 } | 671 } |
(...skipping 1808 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2500 Name, BadIntrinsic); | 2480 Name, BadIntrinsic); |
2501 if (BadIntrinsic) { | 2481 if (BadIntrinsic) { |
2502 std::string Buffer; | 2482 std::string Buffer; |
2503 raw_string_ostream StrBuf(Buffer); | 2483 raw_string_ostream StrBuf(Buffer); |
2504 StrBuf << "Invalid PNaCl intrinsic call to " << Name; | 2484 StrBuf << "Invalid PNaCl intrinsic call to " << Name; |
2505 Error(StrBuf.str()); | 2485 Error(StrBuf.str()); |
2506 appendErrorInstruction(ReturnType); | 2486 appendErrorInstruction(ReturnType); |
2507 return; | 2487 return; |
2508 } | 2488 } |
2509 } else { | 2489 } else { |
2510 if (getFlags().getStubConstantCalls() && | |
2511 llvm::isa<Ice::ConstantInteger32>(Callee)) { | |
2512 Callee = Context->getStubbedConstCallValue(Callee); | |
2513 } | |
2514 ReturnType = Context->getSimpleTypeByID(Values[2]); | 2490 ReturnType = Context->getSimpleTypeByID(Values[2]); |
2515 } | 2491 } |
2516 | 2492 |
2517 // Extract call information. | 2493 // Extract call information. |
2518 uint64_t CCInfo = Values[0]; | 2494 uint64_t CCInfo = Values[0]; |
2519 CallingConv::ID CallingConv; | 2495 CallingConv::ID CallingConv; |
2520 if (!naclbitc::DecodeCallingConv(CCInfo >> 1, CallingConv)) { | 2496 if (!naclbitc::DecodeCallingConv(CCInfo >> 1, CallingConv)) { |
2521 std::string Buffer; | 2497 std::string Buffer; |
2522 raw_string_ostream StrBuf(Buffer); | 2498 raw_string_ostream StrBuf(Buffer); |
2523 StrBuf << "Function call calling convention value " << (CCInfo >> 1) | 2499 StrBuf << "Function call calling convention value " << (CCInfo >> 1) |
(...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3038 } | 3014 } |
3039 if (InputStreamFile.getBitcodeBytes().getExtent() % 4 != 0) { | 3015 if (InputStreamFile.getBitcodeBytes().getExtent() % 4 != 0) { |
3040 errs() << IRFilename | 3016 errs() << IRFilename |
3041 << ": Bitcode stream should be a multiple of 4 bytes in length.\n"; | 3017 << ": Bitcode stream should be a multiple of 4 bytes in length.\n"; |
3042 ErrorStatus.assign(EC_Bitcode); | 3018 ErrorStatus.assign(EC_Bitcode); |
3043 return; | 3019 return; |
3044 } | 3020 } |
3045 } | 3021 } |
3046 | 3022 |
3047 } // end of namespace Ice | 3023 } // end of namespace Ice |
OLD | NEW |