llvm.org GIT mirror llvm / 980e5aa
implement materializeModule, force deallocation of vector memory when we are done with them, start implementing ParseFunctionBody git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36617 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
2 changed file(s) with 107 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
658658 }
659659 }
660660
661 /// ParseFunction - When we see the block for a function body, remember where it
662 /// is and then skip it. This lets us lazily deserialize the functions.
663 bool BitcodeReader::ParseFunction() {
661 /// RememberAndSkipFunctionBody - When we see the block for a function body,
662 /// remember where it is and then skip it. This lets us lazily deserialize the
663 /// functions.
664 bool BitcodeReader::RememberAndSkipFunctionBody() {
664665 // Get the function we are talking about.
665666 if (FunctionsWithBodies.empty())
666667 return Error("Insufficient function protos");
700701 while (!Stream.AtEndOfStream()) {
701702 unsigned Code = Stream.ReadCode();
702703 if (Code == bitc::END_BLOCK) {
704 if (Stream.ReadBlockEnd())
705 return Error("Error at end of module block");
706
707 // Patch the initializers for globals and aliases up.
703708 ResolveGlobalAndAliasInits();
704709 if (!GlobalInits.empty() || !AliasInits.empty())
705710 return Error("Malformed global initializer set");
706711 if (!FunctionsWithBodies.empty())
707712 return Error("Too few function bodies found");
708 if (Stream.ReadBlockEnd())
709 return Error("Error at end of module block");
713
714 // Force deallocation of memory for these vectors to favor the client that
715 // want lazy deserialization.
716 std::vector >().swap(GlobalInits);
717 std::vector >().swap(AliasInits);
718 std::vector().swap(FunctionsWithBodies);
710719 return false;
711720 }
712721
740749 HasReversedFunctionsWithBodies = true;
741750 }
742751
743 if (ParseFunction())
752 if (RememberAndSkipFunctionBody())
744753 return true;
745754 break;
746755 }
955964 F->setLinkage((GlobalValue::LinkageTypes)DFII->second.second);
956965 DeferredFunctionInfo.erase(DFII);
957966
967 if (ParseFunctionBody(F)) {
968 if (ErrInfo) *ErrInfo = ErrorString;
969 return true;
970 }
971
972 return false;
973 }
974
975 Module *BitcodeReader::materializeModule(std::string *ErrInfo) {
976 DenseMap >::iterator I =
977 DeferredFunctionInfo.begin();
978 while (!DeferredFunctionInfo.empty()) {
979 Function *F = (*I++).first;
980 assert(F->hasNotBeenReadFromBytecode() &&
981 "Deserialized function found in map!");
982 if (materializeFunction(F, ErrInfo))
983 return 0;
984 }
985 return TheModule;
986 }
987
988
989 /// ParseFunctionBody - Lazily parse the specified function body block.
990 bool BitcodeReader::ParseFunctionBody(Function *F) {
991 if (Stream.EnterSubBlock())
992 return Error("Malformed block record");
993
994 unsigned ModuleValueListSize = ValueList.size();
995
996 // Add all the function arguments to the value table.
997 for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
998 ValueList.push_back(I);
999
1000 // Read all the records.
1001 SmallVector Record;
1002 while (1) {
1003 unsigned Code = Stream.ReadCode();
1004 if (Code == bitc::END_BLOCK) {
1005 if (Stream.ReadBlockEnd())
1006 return Error("Error at end of function block");
1007 break;
1008 }
1009
1010 if (Code == bitc::ENTER_SUBBLOCK) {
1011 switch (Stream.ReadSubBlockID()) {
1012 default: // Skip unknown content.
1013 if (Stream.SkipBlock())
1014 return Error("Malformed block record");
1015 break;
1016 case bitc::CONSTANTS_BLOCK_ID:
1017 if (ParseConstants()) return true;
1018 break;
1019 case bitc::VALUE_SYMTAB_BLOCK_ID:
1020 if (ParseValueSymbolTable()) return true;
1021 break;
1022 }
1023 continue;
1024 }
1025
1026 if (Code == bitc::DEFINE_ABBREV) {
1027 Stream.ReadAbbrevRecord();
1028 continue;
1029 }
1030
1031 // Read a record.
1032 Record.clear();
1033 switch (Stream.ReadRecord(Code, Record)) {
1034 default: // Default behavior: unknown constant
1035 case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks]
1036 if (Record.size() < 1)
1037 return Error("Invalid FUNC_CODE_DECLAREBLOCKS record");
1038 // Create all the basic blocks for the function.
1039 FunctionBBs.resize(Record.size());
1040 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
1041 FunctionBBs[i] = new BasicBlock("", F);
1042 break;
1043 }
1044 }
1045
1046
1047 // Trim the value list down to the size it was before we parsed this function.
1048 ValueList.shrinkTo(ModuleValueListSize);
1049 std::vector().swap(FunctionBBs);
1050
9581051 return false;
9591052 }
9601053
4343 void pop_back() { Uses.pop_back(); --NumOperands; }
4444 bool empty() const { return NumOperands == 0; }
4545 void shrinkTo(unsigned N) {
46 assert(N < NumOperands && "Invalid shrinkTo request!");
46 assert(N <= NumOperands && "Invalid shrinkTo request!");
4747 Uses.resize(N);
4848 NumOperands = N;
4949 }
6767 BitcodeReaderValueList ValueList;
6868 std::vector > GlobalInits;
6969 std::vector > AliasInits;
70
71 /// FunctionBBs - While parsing a function body, this is a list of the basic
72 /// blocks for the function.
73 std::vector FunctionBBs;
7074
7175 // When reading the module header, this list is populated with functions that
7276 // have bodies later in the file.
9599 }
96100
97101 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
98
99 virtual Module *materializeModule(std::string *ErrInfo = 0) {
100 // FIXME: TODO
101 //if (ParseAllFunctionBodies(ErrMsg))
102 // return 0;
103 return TheModule;
104 }
102 virtual Module *materializeModule(std::string *ErrInfo = 0);
105103
106104 bool Error(const char *Str) {
107105 ErrorString = Str;
120118 bool ParseTypeSymbolTable();
121119 bool ParseValueSymbolTable();
122120 bool ParseConstants();
123 bool ParseFunction();
121 bool RememberAndSkipFunctionBody();
122 bool ParseFunctionBody(Function *F);
124123 bool ResolveGlobalAndAliasInits();
125124 };
126125