From 2491b0959e09ab9e6bb8f345cb5aecdcdbf61116 Mon Sep 17 00:00:00 2001 From: Reid Spencer Date: Thu, 10 Jun 2004 21:59:20 +0000 Subject: [PATCH] Make the parser deal with functions instead of just function types. llvm-svn: 14120 --- llvm/lib/Bytecode/Analyzer/Parser.cpp | 64 ++++++++++++++------------- llvm/lib/Bytecode/Analyzer/Parser.h | 16 +++---- llvm/lib/Bytecode/Reader/Parser.cpp | 64 ++++++++++++++------------- llvm/lib/Bytecode/Reader/Parser.h | 16 +++---- 4 files changed, 84 insertions(+), 76 deletions(-) diff --git a/llvm/lib/Bytecode/Analyzer/Parser.cpp b/llvm/lib/Bytecode/Analyzer/Parser.cpp index 80800e75a657..a784811f861e 100644 --- a/llvm/lib/Bytecode/Analyzer/Parser.cpp +++ b/llvm/lib/Bytecode/Analyzer/Parser.cpp @@ -312,37 +312,51 @@ void AbstractBytecodeParser::ParseFunctionLazily() { if (FunctionSignatureList.empty()) throw std::string("FunctionSignatureList empty!"); - const Type *FType = FunctionSignatureList.back(); + Function *Func = FunctionSignatureList.back(); FunctionSignatureList.pop_back(); // Save the information for future reading of the function - LazyFunctionLoadMap[FType] = LazyFunctionInfo(BlockStart, BlockEnd); + LazyFunctionLoadMap[Func] = LazyFunctionInfo(BlockStart, BlockEnd); // Pretend we've `parsed' this function At = BlockEnd; } -void AbstractBytecodeParser::ParseNextFunction(Type* FType) { +void AbstractBytecodeParser::ParseNextFunction(Function* Func) { // Find {start, end} pointers and slot in the map. If not there, we're done. - LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.find(FType); + LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.find(Func); // Make sure we found it if ( Fi == LazyFunctionLoadMap.end() ) { - PARSE_ERROR("Unrecognized function of type " << FType->getDescription()); + PARSE_ERROR("Unrecognized function of type " << Func->getType()->getDescription()); return; } BlockStart = At = Fi->second.Buf; BlockEnd = Fi->second.Buf; - assert(Fi->first == FType); + assert(Fi->first == Func); LazyFunctionLoadMap.erase(Fi); - this->ParseFunctionBody( FType ); + this->ParseFunctionBody( Func ); } -void AbstractBytecodeParser::ParseFunctionBody(const Type* FType ) { +void AbstractBytecodeParser::ParseAllFunctionBodies() { + LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.begin(); + LazyFunctionMap::iterator Fe = LazyFunctionLoadMap.end(); + while ( Fi != Fe ) { + Function* Func = Fi->first; + BlockStart = At = Fi->second.Buf; + BlockEnd = Fi->second.EndBuf; + this->ParseFunctionBody(Func); + ++Fi; + } +} + +void AbstractBytecodeParser::ParseFunctionBody(Function* Func ) { + + unsigned FuncSize = BlockEnd - At; GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage; unsigned LinkageType = read_vbr_uint(); @@ -358,7 +372,8 @@ void AbstractBytecodeParser::ParseFunctionBody(const Type* FType ) { break; } - handler->handleFunctionBegin(FType,Linkage); + Func->setLinkage( Linkage ); + handler->handleFunctionBegin(Func,FuncSize); // Keep track of how many basic blocks we have read in... unsigned BlockNum = 0; @@ -405,26 +420,13 @@ void AbstractBytecodeParser::ParseFunctionBody(const Type* FType ) { align32(); } - handler->handleFunctionEnd(FType); + handler->handleFunctionEnd(Func); // Clear out function-level types... FunctionTypes.clear(); CompactionTypeTable.clear(); } -void AbstractBytecodeParser::ParseAllFunctionBodies() { - LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.begin(); - LazyFunctionMap::iterator Fe = LazyFunctionLoadMap.end(); - - while ( Fi != Fe ) { - const Type* FType = Fi->first; - BlockStart = At = Fi->second.Buf; - BlockEnd = Fi->second.EndBuf; - this->ParseFunctionBody(FType); - ++Fi; - } -} - void AbstractBytecodeParser::ParseCompactionTable() { handler->handleCompactionTableBegin(); @@ -819,12 +821,14 @@ void AbstractBytecodeParser::ParseModuleGlobalInfo() { } // We create functions by passing the underlying FunctionType to create... - Ty = cast(Ty)->getElementType(); + const FunctionType* FTy = + cast(cast(Ty)->getElementType()); + Function* Func = new Function(FTy, GlobalValue::ExternalLinkage); // Save this for later so we know type of lazily instantiated functions - FunctionSignatureList.push_back(Ty); + FunctionSignatureList.push_back(Func); - handler->handleFunctionDeclaration(Ty); + handler->handleFunctionDeclaration(Func, FTy); // Get Next function signature FnSignature = read_vbr_uint(); @@ -1010,7 +1014,7 @@ void BytecodeHandler::handleInitializedGV( unsigned initSlot) {} void BytecodeHandler::handleType( const Type* Ty ) {} void BytecodeHandler::handleFunctionDeclaration( - const Type* FuncType) {} + Function* Func, const FunctionType* FuncType) {} void BytecodeHandler::handleModuleGlobalsEnd() { } void BytecodeHandler::handleCompactionTableBegin() { } void BytecodeHandler::handleCompactionTablePlane( unsigned Ty, @@ -1028,9 +1032,9 @@ void BytecodeHandler::handleSymbolTableType( unsigned i, unsigned slot, void BytecodeHandler::handleSymbolTableValue( unsigned i, unsigned slot, const std::string& name ) { } void BytecodeHandler::handleSymbolTableEnd() { } -void BytecodeHandler::handleFunctionBegin( const Type* FType, - GlobalValue::LinkageTypes linkage ) { } -void BytecodeHandler::handleFunctionEnd( const Type* FType) { } +void BytecodeHandler::handleFunctionBegin( Function* Func, + unsigned Size ) {} +void BytecodeHandler::handleFunctionEnd( Function* Func) { } void BytecodeHandler::handleBasicBlockBegin( unsigned blocknum) { } bool BytecodeHandler::handleInstruction( unsigned Opcode, const Type* iType, std::vector& Operands, unsigned Size) { diff --git a/llvm/lib/Bytecode/Analyzer/Parser.h b/llvm/lib/Bytecode/Analyzer/Parser.h index 08758cb5f290..7856fc5447f4 100644 --- a/llvm/lib/Bytecode/Analyzer/Parser.h +++ b/llvm/lib/Bytecode/Analyzer/Parser.h @@ -90,7 +90,7 @@ public: /// @see ParseAllFunctionBodies /// @see ParseBytecode /// @brief Parse the next function of specific type - void ParseNextFunction (Type* FType) ; + void ParseNextFunction (Function* Func) ; /// @} /// @name Parsing Units For Subclasses @@ -116,7 +116,7 @@ protected: void ParseFunctionLazily (); /// @brief Parse a function body - void ParseFunctionBody (const Type* FType); + void ParseFunctionBody (Function* Func); /// @brief Parse a compaction table void ParseCompactionTable (); @@ -205,7 +205,7 @@ private: // for each function in the module. When the function is loaded, this type is // used to instantiate the actual function object. - std::vector FunctionSignatureList; + std::vector FunctionSignatureList; // Constant values are read in after global variables. Because of this, we // must defer setting the initializers on global variables until after module @@ -233,7 +233,7 @@ private: LazyFunctionInfo(const unsigned char *B = 0, const unsigned char *EB = 0) : Buf(B), EndBuf(EB) {} }; - typedef std::map LazyFunctionMap; + typedef std::map LazyFunctionMap; LazyFunctionMap LazyFunctionLoadMap; private: @@ -394,7 +394,8 @@ public: /// This method is called when the function prototype for a function is /// encountered in the module globals block. virtual void handleFunctionDeclaration( - const Type* FuncType ///< The type of the function + Function* Func, + const FunctionType* FuncType ///< The type of the function ); /// This method is called at the end of the module globals block. @@ -458,13 +459,12 @@ public: /// @brief Handle the beginning of a function body virtual void handleFunctionBegin( - const Type* FType, - GlobalValue::LinkageTypes linkage + Function* Func, unsigned Size ); /// @brief Handle the end of a function body virtual void handleFunctionEnd( - const Type* FType + Function* Func ); /// @brief Handle the beginning of a basic block diff --git a/llvm/lib/Bytecode/Reader/Parser.cpp b/llvm/lib/Bytecode/Reader/Parser.cpp index 80800e75a657..a784811f861e 100644 --- a/llvm/lib/Bytecode/Reader/Parser.cpp +++ b/llvm/lib/Bytecode/Reader/Parser.cpp @@ -312,37 +312,51 @@ void AbstractBytecodeParser::ParseFunctionLazily() { if (FunctionSignatureList.empty()) throw std::string("FunctionSignatureList empty!"); - const Type *FType = FunctionSignatureList.back(); + Function *Func = FunctionSignatureList.back(); FunctionSignatureList.pop_back(); // Save the information for future reading of the function - LazyFunctionLoadMap[FType] = LazyFunctionInfo(BlockStart, BlockEnd); + LazyFunctionLoadMap[Func] = LazyFunctionInfo(BlockStart, BlockEnd); // Pretend we've `parsed' this function At = BlockEnd; } -void AbstractBytecodeParser::ParseNextFunction(Type* FType) { +void AbstractBytecodeParser::ParseNextFunction(Function* Func) { // Find {start, end} pointers and slot in the map. If not there, we're done. - LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.find(FType); + LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.find(Func); // Make sure we found it if ( Fi == LazyFunctionLoadMap.end() ) { - PARSE_ERROR("Unrecognized function of type " << FType->getDescription()); + PARSE_ERROR("Unrecognized function of type " << Func->getType()->getDescription()); return; } BlockStart = At = Fi->second.Buf; BlockEnd = Fi->second.Buf; - assert(Fi->first == FType); + assert(Fi->first == Func); LazyFunctionLoadMap.erase(Fi); - this->ParseFunctionBody( FType ); + this->ParseFunctionBody( Func ); } -void AbstractBytecodeParser::ParseFunctionBody(const Type* FType ) { +void AbstractBytecodeParser::ParseAllFunctionBodies() { + LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.begin(); + LazyFunctionMap::iterator Fe = LazyFunctionLoadMap.end(); + while ( Fi != Fe ) { + Function* Func = Fi->first; + BlockStart = At = Fi->second.Buf; + BlockEnd = Fi->second.EndBuf; + this->ParseFunctionBody(Func); + ++Fi; + } +} + +void AbstractBytecodeParser::ParseFunctionBody(Function* Func ) { + + unsigned FuncSize = BlockEnd - At; GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage; unsigned LinkageType = read_vbr_uint(); @@ -358,7 +372,8 @@ void AbstractBytecodeParser::ParseFunctionBody(const Type* FType ) { break; } - handler->handleFunctionBegin(FType,Linkage); + Func->setLinkage( Linkage ); + handler->handleFunctionBegin(Func,FuncSize); // Keep track of how many basic blocks we have read in... unsigned BlockNum = 0; @@ -405,26 +420,13 @@ void AbstractBytecodeParser::ParseFunctionBody(const Type* FType ) { align32(); } - handler->handleFunctionEnd(FType); + handler->handleFunctionEnd(Func); // Clear out function-level types... FunctionTypes.clear(); CompactionTypeTable.clear(); } -void AbstractBytecodeParser::ParseAllFunctionBodies() { - LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.begin(); - LazyFunctionMap::iterator Fe = LazyFunctionLoadMap.end(); - - while ( Fi != Fe ) { - const Type* FType = Fi->first; - BlockStart = At = Fi->second.Buf; - BlockEnd = Fi->second.EndBuf; - this->ParseFunctionBody(FType); - ++Fi; - } -} - void AbstractBytecodeParser::ParseCompactionTable() { handler->handleCompactionTableBegin(); @@ -819,12 +821,14 @@ void AbstractBytecodeParser::ParseModuleGlobalInfo() { } // We create functions by passing the underlying FunctionType to create... - Ty = cast(Ty)->getElementType(); + const FunctionType* FTy = + cast(cast(Ty)->getElementType()); + Function* Func = new Function(FTy, GlobalValue::ExternalLinkage); // Save this for later so we know type of lazily instantiated functions - FunctionSignatureList.push_back(Ty); + FunctionSignatureList.push_back(Func); - handler->handleFunctionDeclaration(Ty); + handler->handleFunctionDeclaration(Func, FTy); // Get Next function signature FnSignature = read_vbr_uint(); @@ -1010,7 +1014,7 @@ void BytecodeHandler::handleInitializedGV( unsigned initSlot) {} void BytecodeHandler::handleType( const Type* Ty ) {} void BytecodeHandler::handleFunctionDeclaration( - const Type* FuncType) {} + Function* Func, const FunctionType* FuncType) {} void BytecodeHandler::handleModuleGlobalsEnd() { } void BytecodeHandler::handleCompactionTableBegin() { } void BytecodeHandler::handleCompactionTablePlane( unsigned Ty, @@ -1028,9 +1032,9 @@ void BytecodeHandler::handleSymbolTableType( unsigned i, unsigned slot, void BytecodeHandler::handleSymbolTableValue( unsigned i, unsigned slot, const std::string& name ) { } void BytecodeHandler::handleSymbolTableEnd() { } -void BytecodeHandler::handleFunctionBegin( const Type* FType, - GlobalValue::LinkageTypes linkage ) { } -void BytecodeHandler::handleFunctionEnd( const Type* FType) { } +void BytecodeHandler::handleFunctionBegin( Function* Func, + unsigned Size ) {} +void BytecodeHandler::handleFunctionEnd( Function* Func) { } void BytecodeHandler::handleBasicBlockBegin( unsigned blocknum) { } bool BytecodeHandler::handleInstruction( unsigned Opcode, const Type* iType, std::vector& Operands, unsigned Size) { diff --git a/llvm/lib/Bytecode/Reader/Parser.h b/llvm/lib/Bytecode/Reader/Parser.h index 08758cb5f290..7856fc5447f4 100644 --- a/llvm/lib/Bytecode/Reader/Parser.h +++ b/llvm/lib/Bytecode/Reader/Parser.h @@ -90,7 +90,7 @@ public: /// @see ParseAllFunctionBodies /// @see ParseBytecode /// @brief Parse the next function of specific type - void ParseNextFunction (Type* FType) ; + void ParseNextFunction (Function* Func) ; /// @} /// @name Parsing Units For Subclasses @@ -116,7 +116,7 @@ protected: void ParseFunctionLazily (); /// @brief Parse a function body - void ParseFunctionBody (const Type* FType); + void ParseFunctionBody (Function* Func); /// @brief Parse a compaction table void ParseCompactionTable (); @@ -205,7 +205,7 @@ private: // for each function in the module. When the function is loaded, this type is // used to instantiate the actual function object. - std::vector FunctionSignatureList; + std::vector FunctionSignatureList; // Constant values are read in after global variables. Because of this, we // must defer setting the initializers on global variables until after module @@ -233,7 +233,7 @@ private: LazyFunctionInfo(const unsigned char *B = 0, const unsigned char *EB = 0) : Buf(B), EndBuf(EB) {} }; - typedef std::map LazyFunctionMap; + typedef std::map LazyFunctionMap; LazyFunctionMap LazyFunctionLoadMap; private: @@ -394,7 +394,8 @@ public: /// This method is called when the function prototype for a function is /// encountered in the module globals block. virtual void handleFunctionDeclaration( - const Type* FuncType ///< The type of the function + Function* Func, + const FunctionType* FuncType ///< The type of the function ); /// This method is called at the end of the module globals block. @@ -458,13 +459,12 @@ public: /// @brief Handle the beginning of a function body virtual void handleFunctionBegin( - const Type* FType, - GlobalValue::LinkageTypes linkage + Function* Func, unsigned Size ); /// @brief Handle the end of a function body virtual void handleFunctionEnd( - const Type* FType + Function* Func ); /// @brief Handle the beginning of a basic block