| 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 14 matching lines...) Expand all Loading... |
| 25 #include "IceAPFloat.h" | 25 #include "IceAPFloat.h" |
| 26 #include "IceCfg.h" | 26 #include "IceCfg.h" |
| 27 #include "IceCfgNode.h" | 27 #include "IceCfgNode.h" |
| 28 #include "IceClFlags.h" | 28 #include "IceClFlags.h" |
| 29 #include "IceDefs.h" | 29 #include "IceDefs.h" |
| 30 #include "IceGlobalInits.h" | 30 #include "IceGlobalInits.h" |
| 31 #include "IceInst.h" | 31 #include "IceInst.h" |
| 32 #include "IceOperand.h" | 32 #include "IceOperand.h" |
| 33 #include "PNaClTranslator.h" | 33 #include "PNaClTranslator.h" |
| 34 | 34 |
| 35 #include <memory> | |
| 36 | |
| 37 namespace { | 35 namespace { |
| 38 using namespace llvm; | 36 using namespace llvm; |
| 39 | 37 |
| 40 // Models elements in the list of types defined in the types block. | 38 // Models elements in the list of types defined in the types block. |
| 41 // These elements can be undefined, a (simple) type, or a function type | 39 // These elements can be undefined, a (simple) type, or a function type |
| 42 // signature. Note that an extended type is undefined on construction. | 40 // signature. Note that an extended type is undefined on construction. |
| 43 // Use methods setAsSimpleType and setAsFuncSigType to define | 41 // Use methods setAsSimpleType and setAsFuncSigType to define |
| 44 // the extended type. | 42 // the extended type. |
| 45 class ExtendedType { | 43 class ExtendedType { |
| 46 ExtendedType &operator=(const ExtendedType &Ty) = delete; | 44 ExtendedType &operator=(const ExtendedType &Ty) = delete; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 | 153 |
| 156 // Top-level class to read PNaCl bitcode files, and translate to ICE. | 154 // Top-level class to read PNaCl bitcode files, and translate to ICE. |
| 157 class TopLevelParser : public NaClBitcodeParser { | 155 class TopLevelParser : public NaClBitcodeParser { |
| 158 TopLevelParser(const TopLevelParser &) = delete; | 156 TopLevelParser(const TopLevelParser &) = delete; |
| 159 TopLevelParser &operator=(const TopLevelParser &) = delete; | 157 TopLevelParser &operator=(const TopLevelParser &) = delete; |
| 160 | 158 |
| 161 public: | 159 public: |
| 162 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType; | 160 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType; |
| 163 | 161 |
| 164 TopLevelParser(Ice::Translator &Translator, NaClBitcodeHeader &Header, | 162 TopLevelParser(Ice::Translator &Translator, NaClBitcodeHeader &Header, |
| 165 NaClBitstreamCursor &Cursor, bool &ErrorStatus) | 163 NaClBitstreamCursor &Cursor, std::error_code &ErrorStatus) |
| 166 : NaClBitcodeParser(Cursor), Translator(Translator), Header(Header), | 164 : NaClBitcodeParser(Cursor), Translator(Translator), Header(Header), |
| 167 ErrorStatus(ErrorStatus), NumErrors(0), NumFunctionIds(0), | 165 ErrorStatus(ErrorStatus), NumErrors(0), NumFunctionIds(0), |
| 168 NumFunctionBlocks(0), BlockParser(nullptr) {} | 166 NumFunctionBlocks(0), BlockParser(nullptr) {} |
| 169 | 167 |
| 170 ~TopLevelParser() override {} | 168 ~TopLevelParser() override {} |
| 171 | 169 |
| 172 Ice::Translator &getTranslator() { return Translator; } | 170 Ice::Translator &getTranslator() { return Translator; } |
| 173 | 171 |
| 174 void setBlockParser(BlockParserBaseClass *NewBlockParser) { | 172 void setBlockParser(BlockParserBaseClass *NewBlockParser) { |
| 175 BlockParser = NewBlockParser; | 173 BlockParser = NewBlockParser; |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 const Ice::Translator::VariableDeclarationListType &getGlobalVariables() { | 356 const Ice::Translator::VariableDeclarationListType &getGlobalVariables() { |
| 359 return VariableDeclarations; | 357 return VariableDeclarations; |
| 360 } | 358 } |
| 361 | 359 |
| 362 private: | 360 private: |
| 363 // The translator associated with the parser. | 361 // The translator associated with the parser. |
| 364 Ice::Translator &Translator; | 362 Ice::Translator &Translator; |
| 365 // The bitcode header. | 363 // The bitcode header. |
| 366 NaClBitcodeHeader &Header; | 364 NaClBitcodeHeader &Header; |
| 367 // The exit status that should be set to true if an error occurs. | 365 // The exit status that should be set to true if an error occurs. |
| 368 bool &ErrorStatus; | 366 std::error_code &ErrorStatus; |
| 369 // The number of errors reported. | 367 // The number of errors reported. |
| 370 unsigned NumErrors; | 368 unsigned NumErrors; |
| 371 // The types associated with each type ID. | 369 // The types associated with each type ID. |
| 372 std::vector<ExtendedType> TypeIDValues; | 370 std::vector<ExtendedType> TypeIDValues; |
| 373 // The set of functions. | 371 // The set of functions. |
| 374 FunctionDeclarationListType FunctionDeclarationList; | 372 FunctionDeclarationListType FunctionDeclarationList; |
| 375 // The set of global variables. | 373 // The set of global variables. |
| 376 Ice::Translator::VariableDeclarationListType VariableDeclarations; | 374 Ice::Translator::VariableDeclarationListType VariableDeclarations; |
| 377 // Relocatable constants associated with global declarations. | 375 // Relocatable constants associated with global declarations. |
| 378 std::vector<Ice::Constant *> ValueIDConstants; | 376 std::vector<Ice::Constant *> ValueIDConstants; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 // Reports that there is not global variable declaration for | 419 // Reports that there is not global variable declaration for |
| 422 // ID. Returns an error recovery value to use. | 420 // ID. Returns an error recovery value to use. |
| 423 Ice::VariableDeclaration *reportGetGlobalVariableByIDError(unsigned Index); | 421 Ice::VariableDeclaration *reportGetGlobalVariableByIDError(unsigned Index); |
| 424 | 422 |
| 425 // Reports that there is no corresponding ICE type for LLVMTy, and | 423 // Reports that there is no corresponding ICE type for LLVMTy, and |
| 426 // returns ICE::IceType_void. | 424 // returns ICE::IceType_void. |
| 427 Ice::Type convertToIceTypeError(Type *LLVMTy); | 425 Ice::Type convertToIceTypeError(Type *LLVMTy); |
| 428 }; | 426 }; |
| 429 | 427 |
| 430 bool TopLevelParser::Error(const std::string &Message) { | 428 bool TopLevelParser::Error(const std::string &Message) { |
| 431 ErrorStatus = true; | 429 ErrorStatus.assign(1, std::generic_category()); |
| 432 ++NumErrors; | 430 ++NumErrors; |
| 433 Ice::GlobalContext *Context = Translator.getContext(); | 431 Ice::GlobalContext *Context = Translator.getContext(); |
| 434 Ice::OstreamLocker L(Context); | 432 Ice::OstreamLocker L(Context); |
| 435 raw_ostream &OldErrStream = setErrStream(Context->getStrDump()); | 433 raw_ostream &OldErrStream = setErrStream(Context->getStrDump()); |
| 436 NaClBitcodeParser::Error(Message); | 434 NaClBitcodeParser::Error(Message); |
| 437 setErrStream(OldErrStream); | 435 setErrStream(OldErrStream); |
| 438 if (!Translator.getFlags().AllowErrorRecovery) | 436 if (!Translator.getFlags().AllowErrorRecovery) |
| 439 report_fatal_error("Unable to continue"); | 437 report_fatal_error("Unable to continue"); |
| 440 return true; | 438 return true; |
| 441 } | 439 } |
| (...skipping 2373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2815 ++NameIndex; | 2813 ++NameIndex; |
| 2816 } else { | 2814 } else { |
| 2817 Trans.checkIfUnnamedNameSafe(Decl->getName(), Context, Prefix); | 2815 Trans.checkIfUnnamedNameSafe(Decl->getName(), Context, Prefix); |
| 2818 } | 2816 } |
| 2819 } | 2817 } |
| 2820 | 2818 |
| 2821 bool ParseBlock(unsigned BlockID) override; | 2819 bool ParseBlock(unsigned BlockID) override; |
| 2822 | 2820 |
| 2823 void ExitBlock() override { | 2821 void ExitBlock() override { |
| 2824 InstallGlobalNamesAndGlobalVarInitializers(); | 2822 InstallGlobalNamesAndGlobalVarInitializers(); |
| 2825 getTranslator().emitConstants(); | |
| 2826 } | 2823 } |
| 2827 | 2824 |
| 2828 void ProcessRecord() override; | 2825 void ProcessRecord() override; |
| 2829 }; | 2826 }; |
| 2830 | 2827 |
| 2831 class ModuleValuesymtabParser : public ValuesymtabParser { | 2828 class ModuleValuesymtabParser : public ValuesymtabParser { |
| 2832 ModuleValuesymtabParser(const ModuleValuesymtabParser &) = delete; | 2829 ModuleValuesymtabParser(const ModuleValuesymtabParser &) = delete; |
| 2833 void operator=(const ModuleValuesymtabParser &) = delete; | 2830 void operator=(const ModuleValuesymtabParser &) = delete; |
| 2834 | 2831 |
| 2835 public: | 2832 public: |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2948 | 2945 |
| 2949 } // end of anonymous namespace | 2946 } // end of anonymous namespace |
| 2950 | 2947 |
| 2951 namespace Ice { | 2948 namespace Ice { |
| 2952 | 2949 |
| 2953 void PNaClTranslator::translate(const std::string &IRFilename) { | 2950 void PNaClTranslator::translate(const std::string &IRFilename) { |
| 2954 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrOrFile = | 2951 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrOrFile = |
| 2955 MemoryBuffer::getFileOrSTDIN(IRFilename); | 2952 MemoryBuffer::getFileOrSTDIN(IRFilename); |
| 2956 if (std::error_code EC = ErrOrFile.getError()) { | 2953 if (std::error_code EC = ErrOrFile.getError()) { |
| 2957 errs() << "Error reading '" << IRFilename << "': " << EC.message() << "\n"; | 2954 errs() << "Error reading '" << IRFilename << "': " << EC.message() << "\n"; |
| 2958 ErrorStatus = true; | 2955 ErrorStatus.assign(1, std::generic_category()); |
| 2959 return; | 2956 return; |
| 2960 } | 2957 } |
| 2961 | 2958 |
| 2962 std::unique_ptr<MemoryBuffer> MemBuf(ErrOrFile.get().release()); | 2959 std::unique_ptr<MemoryBuffer> MemBuf(ErrOrFile.get().release()); |
| 2963 translateBuffer(IRFilename, MemBuf.get()); | 2960 translateBuffer(IRFilename, MemBuf.get()); |
| 2964 } | 2961 } |
| 2965 | 2962 |
| 2966 void PNaClTranslator::translateBuffer(const std::string &IRFilename, | 2963 void PNaClTranslator::translateBuffer(const std::string &IRFilename, |
| 2967 MemoryBuffer *MemBuf) { | 2964 MemoryBuffer *MemBuf) { |
| 2968 if (MemBuf->getBufferSize() % 4 != 0) { | 2965 if (MemBuf->getBufferSize() % 4 != 0) { |
| 2969 errs() << IRFilename | 2966 errs() << IRFilename |
| 2970 << ": Bitcode stream should be a multiple of 4 bytes in length.\n"; | 2967 << ": Bitcode stream should be a multiple of 4 bytes in length.\n"; |
| 2971 ErrorStatus = true; | 2968 ErrorStatus.assign(1, std::generic_category()); |
| 2972 return; | 2969 return; |
| 2973 } | 2970 } |
| 2974 | 2971 |
| 2975 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart(); | 2972 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart(); |
| 2976 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize(); | 2973 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize(); |
| 2977 | 2974 |
| 2978 // Read header and verify it is good. | 2975 // Read header and verify it is good. |
| 2979 NaClBitcodeHeader Header; | 2976 NaClBitcodeHeader Header; |
| 2980 if (Header.Read(BufPtr, EndBufPtr) || !Header.IsSupported()) { | 2977 if (Header.Read(BufPtr, EndBufPtr) || !Header.IsSupported()) { |
| 2981 errs() << "Invalid PNaCl bitcode header.\n"; | 2978 errs() << "Invalid PNaCl bitcode header.\n"; |
| 2982 ErrorStatus = true; | 2979 ErrorStatus.assign(1, std::generic_category()); |
| 2983 return; | 2980 return; |
| 2984 } | 2981 } |
| 2985 | 2982 |
| 2986 // Create a bitstream reader to read the bitcode file. | 2983 // Create a bitstream reader to read the bitcode file. |
| 2987 NaClBitstreamReader InputStreamFile(BufPtr, EndBufPtr); | 2984 NaClBitstreamReader InputStreamFile(BufPtr, EndBufPtr); |
| 2988 NaClBitstreamCursor InputStream(InputStreamFile); | 2985 NaClBitstreamCursor InputStream(InputStreamFile); |
| 2989 | 2986 |
| 2990 TopLevelParser Parser(*this, Header, InputStream, ErrorStatus); | 2987 TopLevelParser Parser(*this, Header, InputStream, ErrorStatus); |
| 2991 int TopLevelBlocks = 0; | 2988 int TopLevelBlocks = 0; |
| 2992 while (!InputStream.AtEndOfStream()) { | 2989 while (!InputStream.AtEndOfStream()) { |
| 2993 if (Parser.Parse()) { | 2990 if (Parser.Parse()) { |
| 2994 ErrorStatus = true; | 2991 ErrorStatus.assign(1, std::generic_category()); |
| 2995 return; | 2992 return; |
| 2996 } | 2993 } |
| 2997 ++TopLevelBlocks; | 2994 ++TopLevelBlocks; |
| 2998 } | 2995 } |
| 2999 | 2996 |
| 3000 if (TopLevelBlocks != 1) { | 2997 if (TopLevelBlocks != 1) { |
| 3001 errs() << IRFilename | 2998 errs() << IRFilename |
| 3002 << ": Contains more than one module. Found: " << TopLevelBlocks | 2999 << ": Contains more than one module. Found: " << TopLevelBlocks |
| 3003 << "\n"; | 3000 << "\n"; |
| 3004 ErrorStatus = true; | 3001 ErrorStatus.assign(1, std::generic_category()); |
| 3005 } | 3002 } |
| 3006 } | 3003 } |
| 3007 | 3004 |
| 3008 } // end of namespace Ice | 3005 } // end of namespace Ice |
| OLD | NEW |