Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(473)

Side by Side Diff: lib/Bitcode/Reader/BitcodeReader.cpp

Issue 8393017: Bitcode streaming (Closed)
Patch Set: put destructors back, fix trailing whitespace Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/Bitcode/Reader/BitcodeReader.h ('k') | lib/Bitcode/Writer/BitcodeWriter.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/Support/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 1275 matching lines...) Expand 10 before | Expand all | Expand 10 after
1301 // Save the current stream state. 1302 // Save the current stream state.
1302 uint64_t CurBit = Stream.GetCurrentBitNo(); 1303 uint64_t CurBit = Stream.GetCurrentBitNo();
1303 DeferredFunctionInfo[Fn] = CurBit; 1304 DeferredFunctionInfo[Fn] = CurBit;
1304 1305
1305 // Skip over the function block for now. 1306 // Skip over the function block for now.
1306 if (Stream.SkipBlock()) 1307 if (Stream.SkipBlock())
1307 return Error("Malformed block record"); 1308 return Error("Malformed block record");
1308 return false; 1309 return false;
1309 } 1310 }
1310 1311
1311 bool BitcodeReader::ParseModule() { 1312 bool BitcodeReader::SuspendModuleParse() {
1312 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 1313 // save our current position
1314 NextUnreadBit = Stream.GetCurrentBitNo();
1315 return false;
1316 }
1317
1318 bool BitcodeReader::GlobalCleanup() {
1319 // Patch the initializers for globals and aliases up.
1320 ResolveGlobalAndAliasInits();
1321 if (!GlobalInits.empty() || !AliasInits.empty())
1322 return Error("Malformed global initializer set");
1323
1324 // Look for intrinsic functions which need to be upgraded at some point
1325 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
1326 FI != FE; ++FI) {
1327 Function *NewFn;
1328 if (UpgradeIntrinsicFunction(FI, NewFn))
1329 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
1330 }
1331
1332 // Look for global variables which need to be renamed.
1333 for (Module::global_iterator
1334 GI = TheModule->global_begin(), GE = TheModule->global_end();
1335 GI != GE; ++GI)
1336 UpgradeGlobalVariable(GI);
1337 // Force deallocation of memory for these vectors to favor the client that
1338 // want lazy deserialization.
1339 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
1340 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
1341 return false;
1342 }
1343
1344 bool BitcodeReader::ParseModule(bool Resume) {
1345 if (Resume)
1346 Stream.JumpToBit(NextUnreadBit);
1347 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
1313 return Error("Malformed block record"); 1348 return Error("Malformed block record");
1314 1349
1315 SmallVector<uint64_t, 64> Record; 1350 SmallVector<uint64_t, 64> Record;
1316 std::vector<std::string> SectionTable; 1351 std::vector<std::string> SectionTable;
1317 std::vector<std::string> GCTable; 1352 std::vector<std::string> GCTable;
1318 1353
1319 // Read all the records for this module. 1354 // Read all the records for this module.
1320 while (!Stream.AtEndOfStream()) { 1355 while (!Stream.AtEndOfStream()) {
1321 unsigned Code = Stream.ReadCode(); 1356 unsigned Code = Stream.ReadCode();
1322 if (Code == bitc::END_BLOCK) { 1357 if (Code == bitc::END_BLOCK) {
1323 if (Stream.ReadBlockEnd()) 1358 if (Stream.ReadBlockEnd())
1324 return Error("Error at end of module block"); 1359 return Error("Error at end of module block");
1325 1360
1326 // Patch the initializers for globals and aliases up. 1361 return GlobalCleanup();
1327 ResolveGlobalAndAliasInits();
1328 if (!GlobalInits.empty() || !AliasInits.empty())
1329 return Error("Malformed global initializer set");
1330 if (!FunctionsWithBodies.empty())
1331 return Error("Too few function bodies found");
1332
1333 // Look for intrinsic functions which need to be upgraded at some point
1334 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
1335 FI != FE; ++FI) {
1336 Function* NewFn;
1337 if (UpgradeIntrinsicFunction(FI, NewFn))
1338 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
1339 }
1340
1341 // Look for global variables which need to be renamed.
1342 for (Module::global_iterator
1343 GI = TheModule->global_begin(), GE = TheModule->global_end();
1344 GI != GE; ++GI)
1345 UpgradeGlobalVariable(GI);
1346
1347 // Force deallocation of memory for these vectors to favor the client that
1348 // want lazy deserialization.
1349 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
1350 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
1351 std::vector<Function*>().swap(FunctionsWithBodies);
1352 return false;
1353 } 1362 }
1354 1363
1355 if (Code == bitc::ENTER_SUBBLOCK) { 1364 if (Code == bitc::ENTER_SUBBLOCK) {
1356 switch (Stream.ReadSubBlockID()) { 1365 switch (Stream.ReadSubBlockID()) {
1357 default: // Skip unknown content. 1366 default: // Skip unknown content.
1358 if (Stream.SkipBlock()) 1367 if (Stream.SkipBlock())
1359 return Error("Malformed block record"); 1368 return Error("Malformed block record");
1360 break; 1369 break;
1361 case bitc::BLOCKINFO_BLOCK_ID: 1370 case bitc::BLOCKINFO_BLOCK_ID:
1362 if (Stream.ReadBlockInfoBlock()) 1371 if (Stream.ReadBlockInfoBlock())
(...skipping 15 matching lines...) Expand all
1378 if (ParseConstants() || ResolveGlobalAndAliasInits()) 1387 if (ParseConstants() || ResolveGlobalAndAliasInits())
1379 return true; 1388 return true;
1380 break; 1389 break;
1381 case bitc::METADATA_BLOCK_ID: 1390 case bitc::METADATA_BLOCK_ID:
1382 if (ParseMetadata()) 1391 if (ParseMetadata())
1383 return true; 1392 return true;
1384 break; 1393 break;
1385 case bitc::FUNCTION_BLOCK_ID: 1394 case bitc::FUNCTION_BLOCK_ID:
1386 // If this is the first function body we've seen, reverse the 1395 // If this is the first function body we've seen, reverse the
1387 // FunctionsWithBodies list. 1396 // FunctionsWithBodies list.
1388 if (!HasReversedFunctionsWithBodies) { 1397 if (!SeenFirstFunctionBody) {
1389 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); 1398 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
1390 HasReversedFunctionsWithBodies = true; 1399 if (GlobalCleanup())
1400 return true;
1401 SeenFirstFunctionBody = true;
1391 } 1402 }
1392 1403
1393 if (RememberAndSkipFunctionBody()) 1404 if (RememberAndSkipFunctionBody())
1394 return true; 1405 return true;
1406 if (LazyStreamer)
1407 return SuspendModuleParse();
1395 break; 1408 break;
1396 } 1409 }
1397 continue; 1410 continue;
1398 } 1411 }
1399 1412
1400 if (Code == bitc::DEFINE_ABBREV) { 1413 if (Code == bitc::DEFINE_ABBREV) {
1401 Stream.ReadAbbrevRecord(); 1414 Stream.ReadAbbrevRecord();
1402 continue; 1415 continue;
1403 } 1416 }
1404 1417
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
1539 Func->setGC(GCTable[Record[8]-1].c_str()); 1552 Func->setGC(GCTable[Record[8]-1].c_str());
1540 } 1553 }
1541 bool UnnamedAddr = false; 1554 bool UnnamedAddr = false;
1542 if (Record.size() > 9) 1555 if (Record.size() > 9)
1543 UnnamedAddr = Record[9]; 1556 UnnamedAddr = Record[9];
1544 Func->setUnnamedAddr(UnnamedAddr); 1557 Func->setUnnamedAddr(UnnamedAddr);
1545 ValueList.push_back(Func); 1558 ValueList.push_back(Func);
1546 1559
1547 // If this is a function with a body, remember the prototype we are 1560 // If this is a function with a body, remember the prototype we are
1548 // creating now, so that we can match up the body with them later. 1561 // creating now, so that we can match up the body with them later.
1549 if (!isProto) 1562 if (!isProto) {
1550 FunctionsWithBodies.push_back(Func); 1563 FunctionsWithBodies.push_back(Func);
1564 if (LazyStreamer) DeferredFunctionInfo[Func] = 0;
1565 }
1551 break; 1566 break;
1552 } 1567 }
1553 // ALIAS: [alias type, aliasee val#, linkage] 1568 // ALIAS: [alias type, aliasee val#, linkage]
1554 // ALIAS: [alias type, aliasee val#, linkage, visibility] 1569 // ALIAS: [alias type, aliasee val#, linkage, visibility]
1555 case bitc::MODULE_CODE_ALIAS: { 1570 case bitc::MODULE_CODE_ALIAS: {
1556 if (Record.size() < 3) 1571 if (Record.size() < 3)
1557 return Error("Invalid MODULE_ALIAS record"); 1572 return Error("Invalid MODULE_ALIAS record");
1558 Type *Ty = getTypeByID(Record[0]); 1573 Type *Ty = getTypeByID(Record[0]);
1559 if (!Ty) return Error("Invalid MODULE_ALIAS record"); 1574 if (!Ty) return Error("Invalid MODULE_ALIAS record");
1560 if (!Ty->isPointerTy()) 1575 if (!Ty->isPointerTy())
(...skipping 18 matching lines...) Expand all
1579 } 1594 }
1580 Record.clear(); 1595 Record.clear();
1581 } 1596 }
1582 1597
1583 return Error("Premature end of bitstream"); 1598 return Error("Premature end of bitstream");
1584 } 1599 }
1585 1600
1586 bool BitcodeReader::ParseBitcodeInto(Module *M) { 1601 bool BitcodeReader::ParseBitcodeInto(Module *M) {
1587 TheModule = 0; 1602 TheModule = 0;
1588 1603
1589 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart(); 1604 if (InitStream()) return true;
1590 unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
1591
1592 if (Buffer->getBufferSize() & 3) {
1593 if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd))
1594 return Error("Invalid bitcode signature");
1595 else
1596 return Error("Bitcode stream should be a multiple of 4 bytes in length");
1597 }
1598
1599 // If we have a wrapper header, parse it and ignore the non-bc file contents.
1600 // The magic number is 0x0B17C0DE stored in little endian.
1601 if (isBitcodeWrapper(BufPtr, BufEnd))
1602 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd))
1603 return Error("Invalid bitcode wrapper header");
1604
1605 StreamFile.init(BufPtr, BufEnd);
1606 Stream.init(StreamFile);
1607 1605
1608 // Sniff for the signature. 1606 // Sniff for the signature.
1609 if (Stream.Read(8) != 'B' || 1607 if (Stream.Read(8) != 'B' ||
1610 Stream.Read(8) != 'C' || 1608 Stream.Read(8) != 'C' ||
1611 Stream.Read(4) != 0x0 || 1609 Stream.Read(4) != 0x0 ||
1612 Stream.Read(4) != 0xC || 1610 Stream.Read(4) != 0xC ||
1613 Stream.Read(4) != 0xE || 1611 Stream.Read(4) != 0xE ||
1614 Stream.Read(4) != 0xD) 1612 Stream.Read(4) != 0xD)
1615 return Error("Invalid bitcode signature"); 1613 return Error("Invalid bitcode signature");
1616 1614
(...skipping 21 matching lines...) Expand all
1638 switch (BlockID) { 1636 switch (BlockID) {
1639 case bitc::BLOCKINFO_BLOCK_ID: 1637 case bitc::BLOCKINFO_BLOCK_ID:
1640 if (Stream.ReadBlockInfoBlock()) 1638 if (Stream.ReadBlockInfoBlock())
1641 return Error("Malformed BlockInfoBlock"); 1639 return Error("Malformed BlockInfoBlock");
1642 break; 1640 break;
1643 case bitc::MODULE_BLOCK_ID: 1641 case bitc::MODULE_BLOCK_ID:
1644 // Reject multiple MODULE_BLOCK's in a single bitstream. 1642 // Reject multiple MODULE_BLOCK's in a single bitstream.
1645 if (TheModule) 1643 if (TheModule)
1646 return Error("Multiple MODULE_BLOCKs in same stream"); 1644 return Error("Multiple MODULE_BLOCKs in same stream");
1647 TheModule = M; 1645 TheModule = M;
1648 if (ParseModule()) 1646 if (ParseModule(false))
1649 return true; 1647 return true;
1648 if (LazyStreamer) return false;
1650 break; 1649 break;
1651 default: 1650 default:
1652 if (Stream.SkipBlock()) 1651 if (Stream.SkipBlock())
1653 return Error("Malformed block record"); 1652 return Error("Malformed block record");
1654 break; 1653 break;
1655 } 1654 }
1656 } 1655 }
1657 1656
1658 return false; 1657 return false;
1659 } 1658 }
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1707 break; 1706 break;
1708 } 1707 }
1709 } 1708 }
1710 Record.clear(); 1709 Record.clear();
1711 } 1710 }
1712 1711
1713 return Error("Premature end of bitstream"); 1712 return Error("Premature end of bitstream");
1714 } 1713 }
1715 1714
1716 bool BitcodeReader::ParseTriple(std::string &Triple) { 1715 bool BitcodeReader::ParseTriple(std::string &Triple) {
1717 if (Buffer->getBufferSize() & 3) 1716 if (InitStream()) return true;
1718 return Error("Bitcode stream should be a multiple of 4 bytes in length");
1719
1720 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
1721 unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
1722
1723 // If we have a wrapper header, parse it and ignore the non-bc file contents.
1724 // The magic number is 0x0B17C0DE stored in little endian.
1725 if (isBitcodeWrapper(BufPtr, BufEnd))
1726 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd))
1727 return Error("Invalid bitcode wrapper header");
1728
1729 StreamFile.init(BufPtr, BufEnd);
1730 Stream.init(StreamFile);
1731 1717
1732 // Sniff for the signature. 1718 // Sniff for the signature.
1733 if (Stream.Read(8) != 'B' || 1719 if (Stream.Read(8) != 'B' ||
1734 Stream.Read(8) != 'C' || 1720 Stream.Read(8) != 'C' ||
1735 Stream.Read(4) != 0x0 || 1721 Stream.Read(4) != 0x0 ||
1736 Stream.Read(4) != 0xC || 1722 Stream.Read(4) != 0xC ||
1737 Stream.Read(4) != 0xE || 1723 Stream.Read(4) != 0xE ||
1738 Stream.Read(4) != 0xD) 1724 Stream.Read(4) != 0xD)
1739 return Error("Invalid bitcode signature"); 1725 return Error("Invalid bitcode signature");
1740 1726
(...skipping 859 matching lines...) Expand 10 before | Expand all | Expand 10 after
2600 BlockAddrFwdRefs.erase(BAFRI); 2586 BlockAddrFwdRefs.erase(BAFRI);
2601 } 2587 }
2602 2588
2603 // Trim the value list down to the size it was before we parsed this function. 2589 // Trim the value list down to the size it was before we parsed this function.
2604 ValueList.shrinkTo(ModuleValueListSize); 2590 ValueList.shrinkTo(ModuleValueListSize);
2605 MDValueList.shrinkTo(ModuleMDValueListSize); 2591 MDValueList.shrinkTo(ModuleMDValueListSize);
2606 std::vector<BasicBlock*>().swap(FunctionBBs); 2592 std::vector<BasicBlock*>().swap(FunctionBBs);
2607 return false; 2593 return false;
2608 } 2594 }
2609 2595
2596 /// FindFunctionInStream - Find the function body in the bitcode stream
2597 bool BitcodeReader::FindFunctionInStream(Function *F,
2598 DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator) {
2599 while (DeferredFunctionInfoIterator->second == 0) {
2600 if (Stream.AtEndOfStream())
2601 return Error("Could not find Function in stream");
2602 // ParseModule will parse the next body in the stream and set its
2603 // position in the DeferredFunctionInfo map
2604 if (ParseModule(true)) return true;
2605 }
2606 return false;
2607 }
2608
2610 //===----------------------------------------------------------------------===// 2609 //===----------------------------------------------------------------------===//
2611 // GVMaterializer implementation 2610 // GVMaterializer implementation
2612 //===----------------------------------------------------------------------===// 2611 //===----------------------------------------------------------------------===//
2613 2612
2614 2613
2615 bool BitcodeReader::isMaterializable(const GlobalValue *GV) const { 2614 bool BitcodeReader::isMaterializable(const GlobalValue *GV) const {
2616 if (const Function *F = dyn_cast<Function>(GV)) { 2615 if (const Function *F = dyn_cast<Function>(GV)) {
2617 return F->isDeclaration() && 2616 return F->isDeclaration() &&
2618 DeferredFunctionInfo.count(const_cast<Function*>(F)); 2617 DeferredFunctionInfo.count(const_cast<Function*>(F));
2619 } 2618 }
2620 return false; 2619 return false;
2621 } 2620 }
2622 2621
2623 bool BitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) { 2622 bool BitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) {
2624 Function *F = dyn_cast<Function>(GV); 2623 Function *F = dyn_cast<Function>(GV);
2625 // If it's not a function or is already material, ignore the request. 2624 // If it's not a function or is already material, ignore the request.
2626 if (!F || !F->isMaterializable()) return false; 2625 if (!F || !F->isMaterializable()) return false;
2627 2626
2628 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); 2627 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
2629 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); 2628 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
2629 // If its position is recorded as 0, its body is somewhere in the stream
2630 // but we haven't seen it yet.
2631 if (DFII->second == 0)
2632 if (LazyStreamer && FindFunctionInStream(F, DFII)) return true;
2630 2633
2631 // Move the bit stream to the saved position of the deferred function body. 2634 // Move the bit stream to the saved position of the deferred function body.
2632 Stream.JumpToBit(DFII->second); 2635 Stream.JumpToBit(DFII->second);
2633 2636
2634 if (ParseFunctionBody(F)) { 2637 if (ParseFunctionBody(F)) {
2635 if (ErrInfo) *ErrInfo = ErrorString; 2638 if (ErrInfo) *ErrInfo = ErrorString;
2636 return true; 2639 return true;
2637 } 2640 }
2638 2641
2639 // Upgrade any old intrinsic calls in the function. 2642 // Upgrade any old intrinsic calls in the function.
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
2697 if (!I->first->use_empty()) 2700 if (!I->first->use_empty())
2698 I->first->replaceAllUsesWith(I->second); 2701 I->first->replaceAllUsesWith(I->second);
2699 I->first->eraseFromParent(); 2702 I->first->eraseFromParent();
2700 } 2703 }
2701 } 2704 }
2702 std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics); 2705 std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics);
2703 2706
2704 return false; 2707 return false;
2705 } 2708 }
2706 2709
2710 bool BitcodeReader::InitStream() {
2711 if (LazyStreamer) return InitLazyStream();
2712 return InitStreamFromBuffer();
2713 }
2714
2715 bool BitcodeReader::InitStreamFromBuffer() {
2716 const unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
2717 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
2718
2719 if (Buffer->getBufferSize() & 3) {
2720 if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd))
2721 return Error("Invalid bitcode signature");
2722 else
2723 return Error("Bitcode stream should be a multiple of 4 bytes in length");
2724 }
2725
2726 // If we have a wrapper header, parse it and ignore the non-bc file contents.
2727 // The magic number is 0x0B17C0DE stored in little endian.
2728 if (isBitcodeWrapper(BufPtr, BufEnd))
2729 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
2730 return Error("Invalid bitcode wrapper header");
2731
2732 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
2733 Stream.init(*StreamFile);
2734
2735 return false;
2736 }
2737
2738 bool BitcodeReader::InitLazyStream() {
2739 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
2740 // see it.
2741 LazyBitstreamBytes *Bytes = new LazyBitstreamBytes(LazyStreamer);
2742 StreamFile.reset(new BitstreamReader(Bytes));
2743 Stream.init(*StreamFile);
2744 if (!Bytes->canSkipToPos(15))
2745 return Error("Bitcode stream must be at least 16 bytes in length");
2746 unsigned char buf[16];
2747 for (int i = 0; i < 16; i++) buf[i] = Bytes->getByte(i);
2748 if (!isBitcode(buf, buf + 16)) {
2749 return Error("Invalid bitcode signature");
2750 }
2751 if (isBitcodeWrapper(buf, buf + 4)) {
2752 const unsigned char *bitcodeStart = buf;
2753 const unsigned char *bitcodeEnd = buf + 16;
2754 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
2755 Bytes->dropLeadingBytes(bitcodeStart - buf);
2756 Bytes->setKnownBitcodeSize(bitcodeEnd - bitcodeStart);
2757 }
2758 return false;
2759 }
2707 2760
2708 //===----------------------------------------------------------------------===// 2761 //===----------------------------------------------------------------------===//
2709 // External interface 2762 // External interface
2710 //===----------------------------------------------------------------------===// 2763 //===----------------------------------------------------------------------===//
2711 2764
2712 /// getLazyBitcodeModule - lazy function-at-a-time loading from a file. 2765 /// getLazyBitcodeModule - lazy function-at-a-time loading from a file.
2713 /// 2766 ///
2714 Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer, 2767 Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
2715 LLVMContext& Context, 2768 LLVMContext& Context,
2716 std::string *ErrMsg) { 2769 std::string *ErrMsg) {
2717 Module *M = new Module(Buffer->getBufferIdentifier(), Context); 2770 Module *M = new Module(Buffer->getBufferIdentifier(), Context);
2718 BitcodeReader *R = new BitcodeReader(Buffer, Context); 2771 BitcodeReader *R = new BitcodeReader(Buffer, Context);
2719 M->setMaterializer(R); 2772 M->setMaterializer(R);
2720 if (R->ParseBitcodeInto(M)) { 2773 if (R->ParseBitcodeInto(M)) {
2721 if (ErrMsg) 2774 if (ErrMsg)
2722 *ErrMsg = R->getErrorString(); 2775 *ErrMsg = R->getErrorString();
2723 2776
2724 delete M; // Also deletes R. 2777 delete M; // Also deletes R.
2725 return 0; 2778 return 0;
2726 } 2779 }
2727 // Have the BitcodeReader dtor delete 'Buffer'. 2780 // Have the BitcodeReader dtor delete 'Buffer'.
2728 R->setBufferOwned(true); 2781 R->setBufferOwned(true);
2729 return M; 2782 return M;
2730 } 2783 }
2731 2784
2785
2786 Module *llvm::getStreamedBitcodeModule(const std::string &name,
2787 BitcodeStreamer *streamer,
2788 LLVMContext &Context,
2789 std::string *ErrMsg) {
2790 Module *M = new Module(name, Context);
2791 BitcodeReader *R = new BitcodeReader(streamer, Context);
2792 M->setMaterializer(R);
2793 if (R->ParseBitcodeInto(M)) {
2794 if (ErrMsg)
2795 *ErrMsg = R->getErrorString();
2796 delete M; // Also deletes R.
2797 return 0;
2798 }
2799 R->setBufferOwned(false); // no buffer to delete
2800 return M;
2801 }
2802
2732 /// ParseBitcodeFile - Read the specified bitcode file, returning the module. 2803 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
2733 /// If an error occurs, return null and fill in *ErrMsg if non-null. 2804 /// If an error occurs, return null and fill in *ErrMsg if non-null.
2734 Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context, 2805 Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
2735 std::string *ErrMsg){ 2806 std::string *ErrMsg){
2736 Module *M = getLazyBitcodeModule(Buffer, Context, ErrMsg); 2807 Module *M = getLazyBitcodeModule(Buffer, Context, ErrMsg);
2737 if (!M) return 0; 2808 if (!M) return 0;
2738 2809
2739 // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether 2810 // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether
2740 // there was an error. 2811 // there was an error.
2741 static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false); 2812 static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false);
(...skipping 15 matching lines...) Expand all
2757 R->setBufferOwned(false); 2828 R->setBufferOwned(false);
2758 2829
2759 std::string Triple(""); 2830 std::string Triple("");
2760 if (R->ParseTriple(Triple)) 2831 if (R->ParseTriple(Triple))
2761 if (ErrMsg) 2832 if (ErrMsg)
2762 *ErrMsg = R->getErrorString(); 2833 *ErrMsg = R->getErrorString();
2763 2834
2764 delete R; 2835 delete R;
2765 return Triple; 2836 return Triple;
2766 } 2837 }
OLDNEW
« no previous file with comments | « lib/Bitcode/Reader/BitcodeReader.h ('k') | lib/Bitcode/Writer/BitcodeWriter.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698