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 1790 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1816 break; | 1817 break; |
1817 } | 1818 } |
1818 Record.clear(); | 1819 Record.clear(); |
1819 } | 1820 } |
1820 | 1821 |
1821 return Error("Premature end of bitstream"); | 1822 return Error("Premature end of bitstream"); |
1822 } | 1823 } |
1823 | 1824 |
1824 bool BitcodeReader::ParseBitcodeInto(Module *M) { | 1825 bool BitcodeReader::ParseBitcodeInto(Module *M) { |
1825 TheModule = 0; | 1826 TheModule = 0; |
1826 | 1827 bool err; |
1827 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart(); | 1828 if ((err = InitStream())) return err; |
nlewycky
2011/11/05 00:45:06
if (InitStream()) return true;
(google.com) Derek Schuff
2011/11/07 22:33:50
Done.
| |
1828 unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); | |
1829 | |
1830 if (Buffer->getBufferSize() & 3) { | |
1831 if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd)) | |
1832 return Error("Invalid bitcode signature"); | |
1833 else | |
1834 return Error("Bitcode stream should be a multiple of 4 bytes in length"); | |
1835 } | |
1836 | |
1837 // If we have a wrapper header, parse it and ignore the non-bc file contents. | |
1838 // The magic number is 0x0B17C0DE stored in little endian. | |
1839 if (isBitcodeWrapper(BufPtr, BufEnd)) | |
1840 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd)) | |
1841 return Error("Invalid bitcode wrapper header"); | |
1842 | |
1843 StreamFile.init(BufPtr, BufEnd); | |
1844 Stream.init(StreamFile); | |
1845 | 1829 |
1846 // Sniff for the signature. | 1830 // Sniff for the signature. |
1847 if (Stream.Read(8) != 'B' || | 1831 if (Stream.Read(8) != 'B' || |
1848 Stream.Read(8) != 'C' || | 1832 Stream.Read(8) != 'C' || |
1849 Stream.Read(4) != 0x0 || | 1833 Stream.Read(4) != 0x0 || |
1850 Stream.Read(4) != 0xC || | 1834 Stream.Read(4) != 0xC || |
1851 Stream.Read(4) != 0xE || | 1835 Stream.Read(4) != 0xE || |
1852 Stream.Read(4) != 0xD) | 1836 Stream.Read(4) != 0xD) |
1853 return Error("Invalid bitcode signature"); | 1837 return Error("Invalid bitcode signature"); |
1854 | 1838 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1945 break; | 1929 break; |
1946 } | 1930 } |
1947 } | 1931 } |
1948 Record.clear(); | 1932 Record.clear(); |
1949 } | 1933 } |
1950 | 1934 |
1951 return Error("Premature end of bitstream"); | 1935 return Error("Premature end of bitstream"); |
1952 } | 1936 } |
1953 | 1937 |
1954 bool BitcodeReader::ParseTriple(std::string &Triple) { | 1938 bool BitcodeReader::ParseTriple(std::string &Triple) { |
1955 if (Buffer->getBufferSize() & 3) | 1939 bool err; |
1956 return Error("Bitcode stream should be a multiple of 4 bytes in length"); | 1940 if ((err = InitStream())) return err; |
nlewycky
2011/11/05 00:45:06
again
(google.com) Derek Schuff
2011/11/07 22:33:50
Done.
| |
1957 | |
1958 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart(); | |
1959 unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); | |
1960 | |
1961 // If we have a wrapper header, parse it and ignore the non-bc file contents. | |
1962 // The magic number is 0x0B17C0DE stored in little endian. | |
1963 if (isBitcodeWrapper(BufPtr, BufEnd)) | |
1964 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd)) | |
1965 return Error("Invalid bitcode wrapper header"); | |
1966 | |
1967 StreamFile.init(BufPtr, BufEnd); | |
1968 Stream.init(StreamFile); | |
1969 | 1941 |
1970 // Sniff for the signature. | 1942 // Sniff for the signature. |
1971 if (Stream.Read(8) != 'B' || | 1943 if (Stream.Read(8) != 'B' || |
1972 Stream.Read(8) != 'C' || | 1944 Stream.Read(8) != 'C' || |
1973 Stream.Read(4) != 0x0 || | 1945 Stream.Read(4) != 0x0 || |
1974 Stream.Read(4) != 0xC || | 1946 Stream.Read(4) != 0xC || |
1975 Stream.Read(4) != 0xE || | 1947 Stream.Read(4) != 0xE || |
1976 Stream.Read(4) != 0xD) | 1948 Stream.Read(4) != 0xD) |
1977 return Error("Invalid bitcode signature"); | 1949 return Error("Invalid bitcode signature"); |
1978 | 1950 |
(...skipping 962 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2941 | 2913 |
2942 // Upgrade to new EH scheme. N.B. This will go away in 3.1. | 2914 // Upgrade to new EH scheme. N.B. This will go away in 3.1. |
2943 UpgradeExceptionHandling(M); | 2915 UpgradeExceptionHandling(M); |
2944 | 2916 |
2945 // Check debug info intrinsics. | 2917 // Check debug info intrinsics. |
2946 CheckDebugInfoIntrinsics(TheModule); | 2918 CheckDebugInfoIntrinsics(TheModule); |
2947 | 2919 |
2948 return false; | 2920 return false; |
2949 } | 2921 } |
2950 | 2922 |
2923 bool BitcodeReader::InitStream() { | |
2924 if (LazyStreamer) return InitLazyStream(); | |
2925 else return InitStreamFromBuffer(); | |
nlewycky
2011/11/05 00:45:06
Delete the "else". See http://llvm.org/docs/Coding
| |
2926 } | |
2927 | |
2928 bool BitcodeReader::InitStreamFromBuffer() { | |
2929 const unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart(); | |
2930 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); | |
2931 | |
2932 if (Buffer->getBufferSize() & 3) { | |
2933 if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd)) | |
2934 return Error("Invalid bitcode signature"); | |
2935 else | |
2936 return Error("Bitcode stream should be a multiple of 4 bytes in length"); | |
2937 } | |
2938 | |
2939 // If we have a wrapper header, parse it and ignore the non-bc file contents. | |
2940 // The magic number is 0x0B17C0DE stored in little endian. | |
2941 if (isBitcodeWrapper(BufPtr, BufEnd)) | |
2942 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) | |
2943 return Error("Invalid bitcode wrapper header"); | |
2944 | |
2945 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd)); | |
2946 Stream.init(*StreamFile); | |
2947 | |
2948 return false; | |
2949 } | |
2950 | |
2951 bool BitcodeReader::InitLazyStream() { | |
2952 // Check and strip off the bitcode wrapper; BitstreamReader expects never to | |
2953 // see it. | |
2954 LazyBitstreamVector *BSV = new LazyBitstreamVector(LazyStreamer); | |
2955 StreamFile.reset(new BitstreamReader(BSV)); | |
2956 Stream.init(*StreamFile); | |
2957 if (!BSV->canSkipToPos(15)) | |
2958 return Error("Bitcode stream must be at least 16 bytes in length"); | |
2959 unsigned char buf[16]; | |
2960 for (int i = 0; i < 16; i++) buf[i] = BSV->operator[](i); | |
nlewycky
2011/11/05 00:45:06
(*BSV)[i]
(google.com) Derek Schuff
2011/11/07 22:33:50
Done.
| |
2961 if (!isBitcode(buf, buf + 16)) { | |
2962 return Error("Invalid bitcode signature"); | |
2963 } | |
2964 if (isBitcodeWrapper(buf, buf + 4)) { | |
2965 const unsigned char *bitcodeStart = buf; | |
2966 const unsigned char *bitcodeEnd = buf + 16; | |
2967 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false); | |
2968 BSV->dropLeadingBytes(bitcodeStart - buf); | |
2969 BSV->setKnownBitcodeSize(bitcodeEnd - bitcodeStart); | |
2970 } | |
2971 return false; | |
2972 } | |
2951 | 2973 |
2952 //===----------------------------------------------------------------------===// | 2974 //===----------------------------------------------------------------------===// |
2953 // External interface | 2975 // External interface |
2954 //===----------------------------------------------------------------------===// | 2976 //===----------------------------------------------------------------------===// |
2955 | 2977 |
2956 /// getLazyBitcodeModule - lazy function-at-a-time loading from a file. | 2978 /// getLazyBitcodeModule - lazy function-at-a-time loading from a file. |
2957 /// | 2979 /// |
2958 Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer, | 2980 Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer, |
2959 LLVMContext& Context, | 2981 LLVMContext& Context, |
2960 std::string *ErrMsg) { | 2982 std::string *ErrMsg) { |
2961 Module *M = new Module(Buffer->getBufferIdentifier(), Context); | 2983 Module *M = new Module(Buffer->getBufferIdentifier(), Context); |
2962 BitcodeReader *R = new BitcodeReader(Buffer, Context); | 2984 BitcodeReader *R = new BitcodeReader(Buffer, Context); |
2963 M->setMaterializer(R); | 2985 M->setMaterializer(R); |
2964 if (R->ParseBitcodeInto(M)) { | 2986 if (R->ParseBitcodeInto(M)) { |
2965 if (ErrMsg) | 2987 if (ErrMsg) |
2966 *ErrMsg = R->getErrorString(); | 2988 *ErrMsg = R->getErrorString(); |
2967 | 2989 |
2968 delete M; // Also deletes R. | 2990 delete M; // Also deletes R. |
2969 return 0; | 2991 return 0; |
2970 } | 2992 } |
2971 // Have the BitcodeReader dtor delete 'Buffer'. | 2993 // Have the BitcodeReader dtor delete 'Buffer'. |
2972 R->setBufferOwned(true); | 2994 R->setBufferOwned(true); |
2973 return M; | 2995 return M; |
2974 } | 2996 } |
2975 | 2997 |
2998 | |
2999 Module *llvm::getStreamedBitcodeModule(const std::string& name, | |
3000 BitcodeStreamer* streamer, | |
3001 LLVMContext& Context, | |
3002 std::string *ErrMsg) { | |
3003 Module *M = new Module(name, Context); | |
3004 BitcodeReader *R = new BitcodeReader(streamer, Context); | |
3005 M->setMaterializer(R); | |
3006 if (R->ParseBitcodeInto(M)) { | |
3007 if (ErrMsg) | |
3008 *ErrMsg = R->getErrorString(); | |
3009 delete M; //Also deletes R. | |
nlewycky
2011/11/05 00:45:06
Space after //
(google.com) Derek Schuff
2011/11/07 22:33:50
Done.
| |
3010 return 0; | |
3011 } | |
3012 R->setBufferOwned(false); // no buffer to delete | |
3013 return M; | |
3014 } | |
3015 | |
2976 /// ParseBitcodeFile - Read the specified bitcode file, returning the module. | 3016 /// ParseBitcodeFile - Read the specified bitcode file, returning the module. |
2977 /// If an error occurs, return null and fill in *ErrMsg if non-null. | 3017 /// If an error occurs, return null and fill in *ErrMsg if non-null. |
2978 Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context, | 3018 Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context, |
2979 std::string *ErrMsg){ | 3019 std::string *ErrMsg){ |
2980 Module *M = getLazyBitcodeModule(Buffer, Context, ErrMsg); | 3020 Module *M = getLazyBitcodeModule(Buffer, Context, ErrMsg); |
2981 if (!M) return 0; | 3021 if (!M) return 0; |
2982 | 3022 |
2983 // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether | 3023 // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether |
2984 // there was an error. | 3024 // there was an error. |
2985 static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false); | 3025 static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false); |
(...skipping 15 matching lines...) Expand all Loading... | |
3001 R->setBufferOwned(false); | 3041 R->setBufferOwned(false); |
3002 | 3042 |
3003 std::string Triple(""); | 3043 std::string Triple(""); |
3004 if (R->ParseTriple(Triple)) | 3044 if (R->ParseTriple(Triple)) |
3005 if (ErrMsg) | 3045 if (ErrMsg) |
3006 *ErrMsg = R->getErrorString(); | 3046 *ErrMsg = R->getErrorString(); |
3007 | 3047 |
3008 delete R; | 3048 delete R; |
3009 return Triple; | 3049 return Triple; |
3010 } | 3050 } |
OLD | NEW |