| OLD | NEW |
| 1 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// | 1 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// |
| 2 // | 2 // |
| 3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
| 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 header defines the BitcodeReader class. | 10 // This header defines the BitcodeReader class. |
| 11 // | 11 // |
| 12 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
| 13 | 13 |
| 14 #include "llvm/Bitcode/ReaderWriter.h" | 14 #include "llvm/Bitcode/ReaderWriter.h" |
| 15 #include "BitcodeReader.h" | 15 #include "BitcodeReader.h" |
| 16 #include "llvm/Bitcode/BitcodeStream.h" |
| 16 #include "llvm/Constants.h" | 17 #include "llvm/Constants.h" |
| 17 #include "llvm/DerivedTypes.h" | 18 #include "llvm/DerivedTypes.h" |
| 18 #include "llvm/InlineAsm.h" | 19 #include "llvm/InlineAsm.h" |
| 19 #include "llvm/IntrinsicInst.h" | 20 #include "llvm/IntrinsicInst.h" |
| 20 #include "llvm/Module.h" | 21 #include "llvm/Module.h" |
| 21 #include "llvm/Operator.h" | 22 #include "llvm/Operator.h" |
| 22 #include "llvm/AutoUpgrade.h" | 23 #include "llvm/AutoUpgrade.h" |
| 23 #include "llvm/ADT/SmallString.h" | 24 #include "llvm/ADT/SmallString.h" |
| 24 #include "llvm/ADT/SmallVector.h" | 25 #include "llvm/ADT/SmallVector.h" |
| 25 #include "llvm/Support/MathExtras.h" | 26 #include "llvm/Support/MathExtras.h" |
| (...skipping 1803 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1829 break; | 1830 break; |
| 1830 } | 1831 } |
| 1831 Record.clear(); | 1832 Record.clear(); |
| 1832 } | 1833 } |
| 1833 | 1834 |
| 1834 return Error("Premature end of bitstream"); | 1835 return Error("Premature end of bitstream"); |
| 1835 } | 1836 } |
| 1836 | 1837 |
| 1837 bool BitcodeReader::ParseBitcodeInto(Module *M) { | 1838 bool BitcodeReader::ParseBitcodeInto(Module *M) { |
| 1838 TheModule = 0; | 1839 TheModule = 0; |
| 1839 | 1840 bool err; |
| 1840 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart(); | 1841 if ((err = InitStream())) return err; |
| 1841 unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); | |
| 1842 | |
| 1843 if (Buffer->getBufferSize() & 3) { | |
| 1844 if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd)) | |
| 1845 return Error("Invalid bitcode signature"); | |
| 1846 else | |
| 1847 return Error("Bitcode stream should be a multiple of 4 bytes in length"); | |
| 1848 } | |
| 1849 | |
| 1850 // If we have a wrapper header, parse it and ignore the non-bc file contents. | |
| 1851 // The magic number is 0x0B17C0DE stored in little endian. | |
| 1852 if (isBitcodeWrapper(BufPtr, BufEnd)) | |
| 1853 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd)) | |
| 1854 return Error("Invalid bitcode wrapper header"); | |
| 1855 | |
| 1856 StreamFile.init(BufPtr, BufEnd); | |
| 1857 Stream.init(StreamFile); | |
| 1858 | 1842 |
| 1859 // Sniff for the signature. | 1843 // Sniff for the signature. |
| 1860 if (Stream.Read(8) != 'B' || | 1844 if (Stream.Read(8) != 'B' || |
| 1861 Stream.Read(8) != 'C' || | 1845 Stream.Read(8) != 'C' || |
| 1862 Stream.Read(4) != 0x0 || | 1846 Stream.Read(4) != 0x0 || |
| 1863 Stream.Read(4) != 0xC || | 1847 Stream.Read(4) != 0xC || |
| 1864 Stream.Read(4) != 0xE || | 1848 Stream.Read(4) != 0xE || |
| 1865 Stream.Read(4) != 0xD) | 1849 Stream.Read(4) != 0xD) |
| 1866 return Error("Invalid bitcode signature"); | 1850 return Error("Invalid bitcode signature"); |
| 1867 | 1851 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1958 break; | 1942 break; |
| 1959 } | 1943 } |
| 1960 } | 1944 } |
| 1961 Record.clear(); | 1945 Record.clear(); |
| 1962 } | 1946 } |
| 1963 | 1947 |
| 1964 return Error("Premature end of bitstream"); | 1948 return Error("Premature end of bitstream"); |
| 1965 } | 1949 } |
| 1966 | 1950 |
| 1967 bool BitcodeReader::ParseTriple(std::string &Triple) { | 1951 bool BitcodeReader::ParseTriple(std::string &Triple) { |
| 1968 if (Buffer->getBufferSize() & 3) | 1952 bool err; |
| 1969 return Error("Bitcode stream should be a multiple of 4 bytes in length"); | 1953 if ((err = InitStream())) return err; |
| 1970 | |
| 1971 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart(); | |
| 1972 unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); | |
| 1973 | |
| 1974 // If we have a wrapper header, parse it and ignore the non-bc file contents. | |
| 1975 // The magic number is 0x0B17C0DE stored in little endian. | |
| 1976 if (isBitcodeWrapper(BufPtr, BufEnd)) | |
| 1977 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd)) | |
| 1978 return Error("Invalid bitcode wrapper header"); | |
| 1979 | |
| 1980 StreamFile.init(BufPtr, BufEnd); | |
| 1981 Stream.init(StreamFile); | |
| 1982 | 1954 |
| 1983 // Sniff for the signature. | 1955 // Sniff for the signature. |
| 1984 if (Stream.Read(8) != 'B' || | 1956 if (Stream.Read(8) != 'B' || |
| 1985 Stream.Read(8) != 'C' || | 1957 Stream.Read(8) != 'C' || |
| 1986 Stream.Read(4) != 0x0 || | 1958 Stream.Read(4) != 0x0 || |
| 1987 Stream.Read(4) != 0xC || | 1959 Stream.Read(4) != 0xC || |
| 1988 Stream.Read(4) != 0xE || | 1960 Stream.Read(4) != 0xE || |
| 1989 Stream.Read(4) != 0xD) | 1961 Stream.Read(4) != 0xD) |
| 1990 return Error("Invalid bitcode signature"); | 1962 return Error("Invalid bitcode signature"); |
| 1991 | 1963 |
| (...skipping 962 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2954 | 2926 |
| 2955 // Upgrade to new EH scheme. N.B. This will go away in 3.1. | 2927 // Upgrade to new EH scheme. N.B. This will go away in 3.1. |
| 2956 UpgradeExceptionHandling(M); | 2928 UpgradeExceptionHandling(M); |
| 2957 | 2929 |
| 2958 // Check debug info intrinsics. | 2930 // Check debug info intrinsics. |
| 2959 CheckDebugInfoIntrinsics(TheModule); | 2931 CheckDebugInfoIntrinsics(TheModule); |
| 2960 | 2932 |
| 2961 return false; | 2933 return false; |
| 2962 } | 2934 } |
| 2963 | 2935 |
| 2936 bool BitcodeReader::InitStream() { |
| 2937 if (StreamCallback) return InitLazyStream(); |
| 2938 else return InitStreamFromBuffer(); |
| 2939 } |
| 2940 |
| 2941 bool BitcodeReader::InitStreamFromBuffer() { |
| 2942 const unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart(); |
| 2943 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); |
| 2944 |
| 2945 if (Buffer->getBufferSize() & 3) { |
| 2946 if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd)) |
| 2947 return Error("Invalid bitcode signature"); |
| 2948 else |
| 2949 return Error("Bitcode stream should be a multiple of 4 bytes in length"); |
| 2950 } |
| 2951 |
| 2952 // If we have a wrapper header, parse it and ignore the non-bc file contents. |
| 2953 // The magic number is 0x0B17C0DE stored in little endian. |
| 2954 if (isBitcodeWrapper(BufPtr, BufEnd)) |
| 2955 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) |
| 2956 return Error("Invalid bitcode wrapper header"); |
| 2957 |
| 2958 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd)); |
| 2959 Stream.init(*StreamFile); |
| 2960 |
| 2961 return false; |
| 2962 } |
| 2963 |
| 2964 bool BitcodeReader::InitLazyStream() { |
| 2965 // Check and strip off the bitcode wrapper; BitstreamReader expects never to |
| 2966 // see it. |
| 2967 LazyBitstreamVector *BSV = new LazyBitstreamVector(StreamCallback); |
| 2968 StreamFile.reset(new BitstreamReader(BSV)); |
| 2969 Stream.init(*StreamFile); |
| 2970 if (!BSV->canSkipToPos(15)) |
| 2971 return Error("Bitcode stream must be at least 16 bytes in length"); |
| 2972 if (!isBitcode(BSV->addressOf(0), BSV->addressOf(16))) { |
| 2973 return Error("Invalid bitcode signature"); |
| 2974 } |
| 2975 if (isBitcodeWrapper(BSV->addressOf(0), BSV->addressOf(4))) { |
| 2976 const unsigned char *bitcodeStart = BSV->addressOf(0); |
| 2977 const unsigned char *bitcodeEnd = BSV->addressOf(16); |
| 2978 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false); |
| 2979 BSV->dropLeadingBytes(bitcodeStart - BSV->addressOf(0)); |
| 2980 BSV->setKnownBitcodeSize(bitcodeEnd - bitcodeStart); |
| 2981 } |
| 2982 return false; |
| 2983 } |
| 2964 | 2984 |
| 2965 //===----------------------------------------------------------------------===// | 2985 //===----------------------------------------------------------------------===// |
| 2966 // External interface | 2986 // External interface |
| 2967 //===----------------------------------------------------------------------===// | 2987 //===----------------------------------------------------------------------===// |
| 2968 | 2988 |
| 2969 /// getLazyBitcodeModule - lazy function-at-a-time loading from a file. | 2989 /// getLazyBitcodeModule - lazy function-at-a-time loading from a file. |
| 2970 /// | 2990 /// |
| 2971 Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer, | 2991 Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer, |
| 2972 LLVMContext& Context, | 2992 LLVMContext& Context, |
| 2973 std::string *ErrMsg) { | 2993 std::string *ErrMsg) { |
| 2974 Module *M = new Module(Buffer->getBufferIdentifier(), Context); | 2994 Module *M = new Module(Buffer->getBufferIdentifier(), Context); |
| 2975 BitcodeReader *R = new BitcodeReader(Buffer, Context); | 2995 BitcodeReader *R = new BitcodeReader(Buffer, Context); |
| 2976 M->setMaterializer(R); | 2996 M->setMaterializer(R); |
| 2977 if (R->ParseBitcodeInto(M)) { | 2997 if (R->ParseBitcodeInto(M)) { |
| 2978 if (ErrMsg) | 2998 if (ErrMsg) |
| 2979 *ErrMsg = R->getErrorString(); | 2999 *ErrMsg = R->getErrorString(); |
| 2980 | 3000 |
| 2981 delete M; // Also deletes R. | 3001 delete M; // Also deletes R. |
| 2982 return 0; | 3002 return 0; |
| 2983 } | 3003 } |
| 2984 // Have the BitcodeReader dtor delete 'Buffer'. | 3004 // Have the BitcodeReader dtor delete 'Buffer'. |
| 2985 R->setBufferOwned(true); | 3005 R->setBufferOwned(true); |
| 2986 return M; | 3006 return M; |
| 2987 } | 3007 } |
| 2988 | 3008 |
| 3009 Module *llvm::getLazyBitcodeStreamModule(const std::string& name, |
| 3010 StreamChunkCallback cb, |
| 3011 LLVMContext& Context, |
| 3012 std::string *ErrMsg) { |
| 3013 Module *M = new Module(name, Context); |
| 3014 BitcodeReader *R = new BitcodeReader(cb, Context); |
| 3015 M->setMaterializer(R); |
| 3016 if (R->ParseBitcodeInto(M)) { |
| 3017 if (ErrMsg) |
| 3018 *ErrMsg = R->getErrorString(); |
| 3019 |
| 3020 delete M; //Also deletes R. |
| 3021 return 0; |
| 3022 } |
| 3023 R->setBufferOwned(false); // no buffer to delete |
| 3024 return M; |
| 3025 } |
| 3026 |
| 2989 /// ParseBitcodeFile - Read the specified bitcode file, returning the module. | 3027 /// ParseBitcodeFile - Read the specified bitcode file, returning the module. |
| 2990 /// If an error occurs, return null and fill in *ErrMsg if non-null. | 3028 /// If an error occurs, return null and fill in *ErrMsg if non-null. |
| 2991 Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context, | 3029 Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context, |
| 2992 std::string *ErrMsg){ | 3030 std::string *ErrMsg){ |
| 2993 Module *M = getLazyBitcodeModule(Buffer, Context, ErrMsg); | 3031 Module *M = getLazyBitcodeModule(Buffer, Context, ErrMsg); |
| 2994 if (!M) return 0; | 3032 if (!M) return 0; |
| 2995 | 3033 |
| 2996 // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether | 3034 // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether |
| 2997 // there was an error. | 3035 // there was an error. |
| 2998 static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false); | 3036 static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 3014 R->setBufferOwned(false); | 3052 R->setBufferOwned(false); |
| 3015 | 3053 |
| 3016 std::string Triple(""); | 3054 std::string Triple(""); |
| 3017 if (R->ParseTriple(Triple)) | 3055 if (R->ParseTriple(Triple)) |
| 3018 if (ErrMsg) | 3056 if (ErrMsg) |
| 3019 *ErrMsg = R->getErrorString(); | 3057 *ErrMsg = R->getErrorString(); |
| 3020 | 3058 |
| 3021 delete R; | 3059 delete R; |
| 3022 return Triple; | 3060 return Triple; |
| 3023 } | 3061 } |
| OLD | NEW |