| 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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 | 150 |
| 153 // Top-level class to read PNaCl bitcode files, and translate to ICE. | 151 // Top-level class to read PNaCl bitcode files, and translate to ICE. |
| 154 class TopLevelParser : public NaClBitcodeParser { | 152 class TopLevelParser : public NaClBitcodeParser { |
| 155 TopLevelParser(const TopLevelParser &) = delete; | 153 TopLevelParser(const TopLevelParser &) = delete; |
| 156 TopLevelParser &operator=(const TopLevelParser &) = delete; | 154 TopLevelParser &operator=(const TopLevelParser &) = delete; |
| 157 | 155 |
| 158 public: | 156 public: |
| 159 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType; | 157 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType; |
| 160 | 158 |
| 161 TopLevelParser(Ice::Translator &Translator, NaClBitcodeHeader &Header, | 159 TopLevelParser(Ice::Translator &Translator, NaClBitcodeHeader &Header, |
| 162 NaClBitstreamCursor &Cursor, bool &ErrorStatus) | 160 NaClBitstreamCursor &Cursor, Ice::ErrorCode &ErrorStatus) |
| 163 : NaClBitcodeParser(Cursor), Translator(Translator), Header(Header), | 161 : NaClBitcodeParser(Cursor), Translator(Translator), Header(Header), |
| 164 ErrorStatus(ErrorStatus), NumErrors(0), NumFunctionIds(0), | 162 ErrorStatus(ErrorStatus), NumErrors(0), NumFunctionIds(0), |
| 165 NumFunctionBlocks(0), BlockParser(nullptr) {} | 163 NumFunctionBlocks(0), BlockParser(nullptr) {} |
| 166 | 164 |
| 167 ~TopLevelParser() override {} | 165 ~TopLevelParser() override {} |
| 168 | 166 |
| 169 Ice::Translator &getTranslator() { return Translator; } | 167 Ice::Translator &getTranslator() { return Translator; } |
| 170 | 168 |
| 171 void setBlockParser(BlockParserBaseClass *NewBlockParser) { | 169 void setBlockParser(BlockParserBaseClass *NewBlockParser) { |
| 172 BlockParser = NewBlockParser; | 170 BlockParser = NewBlockParser; |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 const Ice::Translator::VariableDeclarationListType &getGlobalVariables() { | 353 const Ice::Translator::VariableDeclarationListType &getGlobalVariables() { |
| 356 return VariableDeclarations; | 354 return VariableDeclarations; |
| 357 } | 355 } |
| 358 | 356 |
| 359 private: | 357 private: |
| 360 // The translator associated with the parser. | 358 // The translator associated with the parser. |
| 361 Ice::Translator &Translator; | 359 Ice::Translator &Translator; |
| 362 // The bitcode header. | 360 // The bitcode header. |
| 363 NaClBitcodeHeader &Header; | 361 NaClBitcodeHeader &Header; |
| 364 // The exit status that should be set to true if an error occurs. | 362 // The exit status that should be set to true if an error occurs. |
| 365 bool &ErrorStatus; | 363 Ice::ErrorCode &ErrorStatus; |
| 366 // The number of errors reported. | 364 // The number of errors reported. |
| 367 unsigned NumErrors; | 365 unsigned NumErrors; |
| 368 // The types associated with each type ID. | 366 // The types associated with each type ID. |
| 369 std::vector<ExtendedType> TypeIDValues; | 367 std::vector<ExtendedType> TypeIDValues; |
| 370 // The set of functions. | 368 // The set of functions. |
| 371 FunctionDeclarationListType FunctionDeclarationList; | 369 FunctionDeclarationListType FunctionDeclarationList; |
| 372 // The set of global variables. | 370 // The set of global variables. |
| 373 Ice::Translator::VariableDeclarationListType VariableDeclarations; | 371 Ice::Translator::VariableDeclarationListType VariableDeclarations; |
| 374 // Relocatable constants associated with global declarations. | 372 // Relocatable constants associated with global declarations. |
| 375 std::vector<Ice::Constant *> ValueIDConstants; | 373 std::vector<Ice::Constant *> ValueIDConstants; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 // Reports that there is not global variable declaration for | 416 // Reports that there is not global variable declaration for |
| 419 // ID. Returns an error recovery value to use. | 417 // ID. Returns an error recovery value to use. |
| 420 Ice::VariableDeclaration *reportGetGlobalVariableByIDError(unsigned Index); | 418 Ice::VariableDeclaration *reportGetGlobalVariableByIDError(unsigned Index); |
| 421 | 419 |
| 422 // Reports that there is no corresponding ICE type for LLVMTy, and | 420 // Reports that there is no corresponding ICE type for LLVMTy, and |
| 423 // returns ICE::IceType_void. | 421 // returns ICE::IceType_void. |
| 424 Ice::Type convertToIceTypeError(Type *LLVMTy); | 422 Ice::Type convertToIceTypeError(Type *LLVMTy); |
| 425 }; | 423 }; |
| 426 | 424 |
| 427 bool TopLevelParser::Error(const std::string &Message) { | 425 bool TopLevelParser::Error(const std::string &Message) { |
| 428 ErrorStatus = true; | 426 ErrorStatus.assign(Ice::EC_Bitcode); |
| 429 ++NumErrors; | 427 ++NumErrors; |
| 430 Ice::GlobalContext *Context = Translator.getContext(); | 428 Ice::GlobalContext *Context = Translator.getContext(); |
| 431 Ice::OstreamLocker L(Context); | 429 Ice::OstreamLocker L(Context); |
| 432 raw_ostream &OldErrStream = setErrStream(Context->getStrDump()); | 430 raw_ostream &OldErrStream = setErrStream(Context->getStrDump()); |
| 433 NaClBitcodeParser::Error(Message); | 431 NaClBitcodeParser::Error(Message); |
| 434 setErrStream(OldErrStream); | 432 setErrStream(OldErrStream); |
| 435 if (!Translator.getFlags().AllowErrorRecovery) | 433 if (!Translator.getFlags().AllowErrorRecovery) |
| 436 report_fatal_error("Unable to continue"); | 434 report_fatal_error("Unable to continue"); |
| 437 return true; | 435 return true; |
| 438 } | 436 } |
| (...skipping 2366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2805 ++NameIndex; | 2803 ++NameIndex; |
| 2806 } else { | 2804 } else { |
| 2807 Trans.checkIfUnnamedNameSafe(Decl->getName(), Context, Prefix); | 2805 Trans.checkIfUnnamedNameSafe(Decl->getName(), Context, Prefix); |
| 2808 } | 2806 } |
| 2809 } | 2807 } |
| 2810 | 2808 |
| 2811 bool ParseBlock(unsigned BlockID) override; | 2809 bool ParseBlock(unsigned BlockID) override; |
| 2812 | 2810 |
| 2813 void ExitBlock() override { | 2811 void ExitBlock() override { |
| 2814 InstallGlobalNamesAndGlobalVarInitializers(); | 2812 InstallGlobalNamesAndGlobalVarInitializers(); |
| 2815 getTranslator().emitConstants(); | |
| 2816 } | 2813 } |
| 2817 | 2814 |
| 2818 void ProcessRecord() override; | 2815 void ProcessRecord() override; |
| 2819 }; | 2816 }; |
| 2820 | 2817 |
| 2821 class ModuleValuesymtabParser : public ValuesymtabParser { | 2818 class ModuleValuesymtabParser : public ValuesymtabParser { |
| 2822 ModuleValuesymtabParser(const ModuleValuesymtabParser &) = delete; | 2819 ModuleValuesymtabParser(const ModuleValuesymtabParser &) = delete; |
| 2823 void operator=(const ModuleValuesymtabParser &) = delete; | 2820 void operator=(const ModuleValuesymtabParser &) = delete; |
| 2824 | 2821 |
| 2825 public: | 2822 public: |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2938 | 2935 |
| 2939 } // end of anonymous namespace | 2936 } // end of anonymous namespace |
| 2940 | 2937 |
| 2941 namespace Ice { | 2938 namespace Ice { |
| 2942 | 2939 |
| 2943 void PNaClTranslator::translate(const std::string &IRFilename) { | 2940 void PNaClTranslator::translate(const std::string &IRFilename) { |
| 2944 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrOrFile = | 2941 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrOrFile = |
| 2945 MemoryBuffer::getFileOrSTDIN(IRFilename); | 2942 MemoryBuffer::getFileOrSTDIN(IRFilename); |
| 2946 if (std::error_code EC = ErrOrFile.getError()) { | 2943 if (std::error_code EC = ErrOrFile.getError()) { |
| 2947 errs() << "Error reading '" << IRFilename << "': " << EC.message() << "\n"; | 2944 errs() << "Error reading '" << IRFilename << "': " << EC.message() << "\n"; |
| 2948 ErrorStatus = true; | 2945 ErrorStatus.assign(EC.value()); |
| 2949 return; | 2946 return; |
| 2950 } | 2947 } |
| 2951 | 2948 |
| 2952 std::unique_ptr<MemoryBuffer> MemBuf(ErrOrFile.get().release()); | 2949 std::unique_ptr<MemoryBuffer> MemBuf(ErrOrFile.get().release()); |
| 2953 translateBuffer(IRFilename, MemBuf.get()); | 2950 translateBuffer(IRFilename, MemBuf.get()); |
| 2954 } | 2951 } |
| 2955 | 2952 |
| 2956 void PNaClTranslator::translateBuffer(const std::string &IRFilename, | 2953 void PNaClTranslator::translateBuffer(const std::string &IRFilename, |
| 2957 MemoryBuffer *MemBuf) { | 2954 MemoryBuffer *MemBuf) { |
| 2958 if (MemBuf->getBufferSize() % 4 != 0) { | 2955 if (MemBuf->getBufferSize() % 4 != 0) { |
| 2959 errs() << IRFilename | 2956 errs() << IRFilename |
| 2960 << ": Bitcode stream should be a multiple of 4 bytes in length.\n"; | 2957 << ": Bitcode stream should be a multiple of 4 bytes in length.\n"; |
| 2961 ErrorStatus = true; | 2958 ErrorStatus.assign(EC_Bitcode); |
| 2962 return; | 2959 return; |
| 2963 } | 2960 } |
| 2964 | 2961 |
| 2965 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart(); | 2962 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart(); |
| 2966 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize(); | 2963 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize(); |
| 2967 | 2964 |
| 2968 // Read header and verify it is good. | 2965 // Read header and verify it is good. |
| 2969 NaClBitcodeHeader Header; | 2966 NaClBitcodeHeader Header; |
| 2970 if (Header.Read(BufPtr, EndBufPtr) || !Header.IsSupported()) { | 2967 if (Header.Read(BufPtr, EndBufPtr) || !Header.IsSupported()) { |
| 2971 errs() << "Invalid PNaCl bitcode header.\n"; | 2968 errs() << "Invalid PNaCl bitcode header.\n"; |
| 2972 ErrorStatus = true; | 2969 ErrorStatus.assign(EC_Bitcode); |
| 2973 return; | 2970 return; |
| 2974 } | 2971 } |
| 2975 | 2972 |
| 2976 // Create a bitstream reader to read the bitcode file. | 2973 // Create a bitstream reader to read the bitcode file. |
| 2977 NaClBitstreamReader InputStreamFile(BufPtr, EndBufPtr); | 2974 NaClBitstreamReader InputStreamFile(BufPtr, EndBufPtr); |
| 2978 NaClBitstreamCursor InputStream(InputStreamFile); | 2975 NaClBitstreamCursor InputStream(InputStreamFile); |
| 2979 | 2976 |
| 2980 TopLevelParser Parser(*this, Header, InputStream, ErrorStatus); | 2977 TopLevelParser Parser(*this, Header, InputStream, ErrorStatus); |
| 2981 int TopLevelBlocks = 0; | 2978 int TopLevelBlocks = 0; |
| 2982 while (!InputStream.AtEndOfStream()) { | 2979 while (!InputStream.AtEndOfStream()) { |
| 2983 if (Parser.Parse()) { | 2980 if (Parser.Parse()) { |
| 2984 ErrorStatus = true; | 2981 ErrorStatus.assign(EC_Bitcode); |
| 2985 return; | 2982 return; |
| 2986 } | 2983 } |
| 2987 ++TopLevelBlocks; | 2984 ++TopLevelBlocks; |
| 2988 } | 2985 } |
| 2989 | 2986 |
| 2990 if (TopLevelBlocks != 1) { | 2987 if (TopLevelBlocks != 1) { |
| 2991 errs() << IRFilename | 2988 errs() << IRFilename |
| 2992 << ": Contains more than one module. Found: " << TopLevelBlocks | 2989 << ": Contains more than one module. Found: " << TopLevelBlocks |
| 2993 << "\n"; | 2990 << "\n"; |
| 2994 ErrorStatus = true; | 2991 ErrorStatus.assign(EC_Bitcode); |
| 2995 } | 2992 } |
| 2996 } | 2993 } |
| 2997 | 2994 |
| 2998 } // end of namespace Ice | 2995 } // end of namespace Ice |
| OLD | NEW |