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. |
(...skipping 1521 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1532 // Save the current stream state. | 1532 // Save the current stream state. |
1533 uint64_t CurBit = Stream.GetCurrentBitNo(); | 1533 uint64_t CurBit = Stream.GetCurrentBitNo(); |
1534 DeferredFunctionInfo[Fn] = CurBit; | 1534 DeferredFunctionInfo[Fn] = CurBit; |
1535 | 1535 |
1536 // Skip over the function block for now. | 1536 // Skip over the function block for now. |
1537 if (Stream.SkipBlock()) | 1537 if (Stream.SkipBlock()) |
1538 return Error("Malformed block record"); | 1538 return Error("Malformed block record"); |
1539 return false; | 1539 return false; |
1540 } | 1540 } |
1541 | 1541 |
1542 bool BitcodeReader::ParseModule() { | 1542 bool BitcodeReader::SuspendModuleParse() { |
1543 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) | 1543 // save our current position |
| 1544 NextUnreadBit = Stream.GetCurrentBitNo(); |
| 1545 // Patch the initializers for globals and aliases up. |
| 1546 // TODO(dschuff) this probably only needs to be done once as well |
| 1547 ResolveGlobalAndAliasInits(); |
| 1548 if (!GlobalInits.empty() || !AliasInits.empty()) |
| 1549 return Error("Malformed global initializer set"); |
| 1550 |
| 1551 // Look for intrinsic functions which need to be upgraded at some point |
| 1552 // TODO(dschuff): do these intrinsics have bodies? if not, this can be |
| 1553 // done only once, before we start dealing with function bodies. if so, |
| 1554 // this can probably still be done incrementally instead of this copy/paste |
| 1555 // hack |
| 1556 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end(); |
| 1557 FI != FE; ++FI) { |
| 1558 Function* NewFn; |
| 1559 if (UpgradeIntrinsicFunction(FI, NewFn)) |
| 1560 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn)); |
| 1561 } |
| 1562 |
| 1563 // TODO(dschuff) this probably only needs to be done once (currently it's a |
| 1564 // noop anyway). find the right place to put it |
| 1565 // Look for global variables which need to be renamed. |
| 1566 for (Module::global_iterator |
| 1567 GI = TheModule->global_begin(), GE = TheModule->global_end(); |
| 1568 GI != GE; ++GI) |
| 1569 UpgradeGlobalVariable(GI); |
| 1570 // Force deallocation of memory for these vectors to favor the client that |
| 1571 // want lazy deserialization. |
| 1572 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits); |
| 1573 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits); |
| 1574 return false; |
| 1575 } |
| 1576 |
| 1577 bool BitcodeReader::ParseModule(bool Resume) { |
| 1578 if (Resume) |
| 1579 Stream.JumpToBit(NextUnreadBit); |
| 1580 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
1544 return Error("Malformed block record"); | 1581 return Error("Malformed block record"); |
1545 | 1582 |
1546 SmallVector<uint64_t, 64> Record; | 1583 SmallVector<uint64_t, 64> Record; |
1547 std::vector<std::string> SectionTable; | 1584 std::vector<std::string> SectionTable; |
1548 std::vector<std::string> GCTable; | 1585 std::vector<std::string> GCTable; |
1549 | 1586 |
1550 // Read all the records for this module. | 1587 // Read all the records for this module. |
1551 while (!Stream.AtEndOfStream()) { | 1588 while (!Stream.AtEndOfStream()) { |
1552 unsigned Code = Stream.ReadCode(); | 1589 unsigned Code = Stream.ReadCode(); |
1553 if (Code == bitc::END_BLOCK) { | 1590 if (Code == bitc::END_BLOCK) { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1624 case bitc::FUNCTION_BLOCK_ID: | 1661 case bitc::FUNCTION_BLOCK_ID: |
1625 // If this is the first function body we've seen, reverse the | 1662 // If this is the first function body we've seen, reverse the |
1626 // FunctionsWithBodies list. | 1663 // FunctionsWithBodies list. |
1627 if (!HasReversedFunctionsWithBodies) { | 1664 if (!HasReversedFunctionsWithBodies) { |
1628 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); | 1665 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); |
1629 HasReversedFunctionsWithBodies = true; | 1666 HasReversedFunctionsWithBodies = true; |
1630 } | 1667 } |
1631 | 1668 |
1632 if (RememberAndSkipFunctionBody()) | 1669 if (RememberAndSkipFunctionBody()) |
1633 return true; | 1670 return true; |
| 1671 if (LazyStreamer) return SuspendModuleParse(); |
1634 break; | 1672 break; |
1635 } | 1673 } |
1636 continue; | 1674 continue; |
1637 } | 1675 } |
1638 | 1676 |
1639 if (Code == bitc::DEFINE_ABBREV) { | 1677 if (Code == bitc::DEFINE_ABBREV) { |
1640 Stream.ReadAbbrevRecord(); | 1678 Stream.ReadAbbrevRecord(); |
1641 continue; | 1679 continue; |
1642 } | 1680 } |
1643 | 1681 |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1778 Func->setGC(GCTable[Record[8]-1].c_str()); | 1816 Func->setGC(GCTable[Record[8]-1].c_str()); |
1779 } | 1817 } |
1780 bool UnnamedAddr = false; | 1818 bool UnnamedAddr = false; |
1781 if (Record.size() > 9) | 1819 if (Record.size() > 9) |
1782 UnnamedAddr = Record[9]; | 1820 UnnamedAddr = Record[9]; |
1783 Func->setUnnamedAddr(UnnamedAddr); | 1821 Func->setUnnamedAddr(UnnamedAddr); |
1784 ValueList.push_back(Func); | 1822 ValueList.push_back(Func); |
1785 | 1823 |
1786 // If this is a function with a body, remember the prototype we are | 1824 // If this is a function with a body, remember the prototype we are |
1787 // creating now, so that we can match up the body with them later. | 1825 // creating now, so that we can match up the body with them later. |
1788 if (!isProto) | 1826 if (!isProto) { |
1789 FunctionsWithBodies.push_back(Func); | 1827 FunctionsWithBodies.push_back(Func); |
| 1828 if (LazyStreamer) DeferredFunctionInfo[Func] = 0; |
| 1829 } |
1790 break; | 1830 break; |
1791 } | 1831 } |
1792 // ALIAS: [alias type, aliasee val#, linkage] | 1832 // ALIAS: [alias type, aliasee val#, linkage] |
1793 // ALIAS: [alias type, aliasee val#, linkage, visibility] | 1833 // ALIAS: [alias type, aliasee val#, linkage, visibility] |
1794 case bitc::MODULE_CODE_ALIAS: { | 1834 case bitc::MODULE_CODE_ALIAS: { |
1795 if (Record.size() < 3) | 1835 if (Record.size() < 3) |
1796 return Error("Invalid MODULE_ALIAS record"); | 1836 return Error("Invalid MODULE_ALIAS record"); |
1797 Type *Ty = getTypeByID(Record[0]); | 1837 Type *Ty = getTypeByID(Record[0]); |
1798 if (!Ty) return Error("Invalid MODULE_ALIAS record"); | 1838 if (!Ty) return Error("Invalid MODULE_ALIAS record"); |
1799 if (!Ty->isPointerTy()) | 1839 if (!Ty->isPointerTy()) |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1860 switch (BlockID) { | 1900 switch (BlockID) { |
1861 case bitc::BLOCKINFO_BLOCK_ID: | 1901 case bitc::BLOCKINFO_BLOCK_ID: |
1862 if (Stream.ReadBlockInfoBlock()) | 1902 if (Stream.ReadBlockInfoBlock()) |
1863 return Error("Malformed BlockInfoBlock"); | 1903 return Error("Malformed BlockInfoBlock"); |
1864 break; | 1904 break; |
1865 case bitc::MODULE_BLOCK_ID: | 1905 case bitc::MODULE_BLOCK_ID: |
1866 // Reject multiple MODULE_BLOCK's in a single bitstream. | 1906 // Reject multiple MODULE_BLOCK's in a single bitstream. |
1867 if (TheModule) | 1907 if (TheModule) |
1868 return Error("Multiple MODULE_BLOCKs in same stream"); | 1908 return Error("Multiple MODULE_BLOCKs in same stream"); |
1869 TheModule = M; | 1909 TheModule = M; |
1870 if (ParseModule()) | 1910 if (ParseModule(false)) |
1871 return true; | 1911 return true; |
| 1912 if (LazyStreamer) return false; |
1872 break; | 1913 break; |
1873 default: | 1914 default: |
1874 if (Stream.SkipBlock()) | 1915 if (Stream.SkipBlock()) |
1875 return Error("Malformed block record"); | 1916 return Error("Malformed block record"); |
1876 break; | 1917 break; |
1877 } | 1918 } |
1878 } | 1919 } |
1879 | 1920 |
1880 return false; | 1921 return false; |
1881 } | 1922 } |
(...skipping 928 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2810 BlockAddrFwdRefs.erase(BAFRI); | 2851 BlockAddrFwdRefs.erase(BAFRI); |
2811 } | 2852 } |
2812 | 2853 |
2813 // Trim the value list down to the size it was before we parsed this function. | 2854 // Trim the value list down to the size it was before we parsed this function. |
2814 ValueList.shrinkTo(ModuleValueListSize); | 2855 ValueList.shrinkTo(ModuleValueListSize); |
2815 MDValueList.shrinkTo(ModuleMDValueListSize); | 2856 MDValueList.shrinkTo(ModuleMDValueListSize); |
2816 std::vector<BasicBlock*>().swap(FunctionBBs); | 2857 std::vector<BasicBlock*>().swap(FunctionBBs); |
2817 return false; | 2858 return false; |
2818 } | 2859 } |
2819 | 2860 |
| 2861 bool BitcodeReader::FindFunctionInStream(Function *F, |
| 2862 DenseMap<Function*, uint64_t>::iterator DFII) { |
| 2863 //Stream.JumpToBit(NextUnreadBit); |
| 2864 while (DFII->second == 0) { |
| 2865 if (Stream.AtEndOfStream()) |
| 2866 return Error("Could not find Function in stream"); |
| 2867 if(ParseModule(true)) return true; |
| 2868 } |
| 2869 return false; |
| 2870 } |
| 2871 |
2820 //===----------------------------------------------------------------------===// | 2872 //===----------------------------------------------------------------------===// |
2821 // GVMaterializer implementation | 2873 // GVMaterializer implementation |
2822 //===----------------------------------------------------------------------===// | 2874 //===----------------------------------------------------------------------===// |
2823 | 2875 |
2824 | 2876 |
2825 bool BitcodeReader::isMaterializable(const GlobalValue *GV) const { | 2877 bool BitcodeReader::isMaterializable(const GlobalValue *GV) const { |
2826 if (const Function *F = dyn_cast<Function>(GV)) { | 2878 if (const Function *F = dyn_cast<Function>(GV)) { |
2827 return F->isDeclaration() && | 2879 return F->isDeclaration() && |
2828 DeferredFunctionInfo.count(const_cast<Function*>(F)); | 2880 DeferredFunctionInfo.count(const_cast<Function*>(F)); |
2829 } | 2881 } |
2830 return false; | 2882 return false; |
2831 } | 2883 } |
2832 | 2884 |
2833 bool BitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) { | 2885 bool BitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) { |
2834 Function *F = dyn_cast<Function>(GV); | 2886 Function *F = dyn_cast<Function>(GV); |
2835 // If it's not a function or is already material, ignore the request. | 2887 // If it's not a function or is already material, ignore the request. |
2836 if (!F || !F->isMaterializable()) return false; | 2888 if (!F || !F->isMaterializable()) return false; |
2837 | 2889 |
2838 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); | 2890 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); |
2839 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); | 2891 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); |
| 2892 if (DFII->second == 0) |
| 2893 if (LazyStreamer && FindFunctionInStream(F, DFII)) return true; |
2840 | 2894 |
2841 // Move the bit stream to the saved position of the deferred function body. | 2895 // Move the bit stream to the saved position of the deferred function body. |
2842 Stream.JumpToBit(DFII->second); | 2896 Stream.JumpToBit(DFII->second); |
2843 | 2897 |
2844 if (ParseFunctionBody(F)) { | 2898 if (ParseFunctionBody(F)) { |
2845 if (ErrInfo) *ErrInfo = ErrorString; | 2899 if (ErrInfo) *ErrInfo = ErrorString; |
2846 return true; | 2900 return true; |
2847 } | 2901 } |
2848 | 2902 |
2849 // Upgrade any old intrinsic calls in the function. | 2903 // Upgrade any old intrinsic calls in the function. |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3041 R->setBufferOwned(false); | 3095 R->setBufferOwned(false); |
3042 | 3096 |
3043 std::string Triple(""); | 3097 std::string Triple(""); |
3044 if (R->ParseTriple(Triple)) | 3098 if (R->ParseTriple(Triple)) |
3045 if (ErrMsg) | 3099 if (ErrMsg) |
3046 *ErrMsg = R->getErrorString(); | 3100 *ErrMsg = R->getErrorString(); |
3047 | 3101 |
3048 delete R; | 3102 delete R; |
3049 return Triple; | 3103 return Triple; |
3050 } | 3104 } |
OLD | NEW |