diff options
Diffstat (limited to 'lib/Bytecode/Analyzer')
-rw-r--r-- | lib/Bytecode/Analyzer/Analyzer.cpp | 242 | ||||
-rw-r--r-- | lib/Bytecode/Analyzer/AnalyzerInternals.h | 65 | ||||
-rw-r--r-- | lib/Bytecode/Analyzer/AnalyzerWrappers.cpp | 208 | ||||
-rw-r--r-- | lib/Bytecode/Analyzer/BytecodeHandler.cpp | 220 | ||||
-rw-r--r-- | lib/Bytecode/Analyzer/BytecodeHandler.h | 247 | ||||
-rw-r--r-- | lib/Bytecode/Analyzer/Dumper.cpp | 311 | ||||
-rw-r--r-- | lib/Bytecode/Analyzer/Makefile | 13 | ||||
-rw-r--r-- | lib/Bytecode/Analyzer/Parser.cpp | 877 | ||||
-rw-r--r-- | lib/Bytecode/Analyzer/Parser.h | 178 | ||||
-rw-r--r-- | lib/Bytecode/Analyzer/ReaderPrimitives.h | 101 |
10 files changed, 2462 insertions, 0 deletions
diff --git a/lib/Bytecode/Analyzer/Analyzer.cpp b/lib/Bytecode/Analyzer/Analyzer.cpp new file mode 100644 index 0000000000..99c3e41f9f --- /dev/null +++ b/lib/Bytecode/Analyzer/Analyzer.cpp @@ -0,0 +1,242 @@ +//===-- BytecodeHandler.cpp - Parsing Handler -------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Reid Spencer and is distributed under the +// University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This header file defines the BytecodeHandler class that gets called by the +// AbstractBytecodeParser when parsing events occur. +// +//===----------------------------------------------------------------------===// + +#include "AnalyzerInternals.h" + +using namespace llvm; + + +namespace { + +class AnalyzerHandler : public BytecodeHandler { +public: + bool handleError(const std::string& str ) + { + return false; + } + + void handleStart() + { + } + + void handleFinish() + { + } + + void handleModuleBegin(const std::string& id) + { + } + + void handleModuleEnd(const std::string& id) + { + } + + void handleVersionInfo( + unsigned char RevisionNum, ///< Byte code revision number + Module::Endianness Endianness, ///< Endianness indicator + Module::PointerSize PointerSize ///< PointerSize indicator + ) + { + } + + void handleModuleGlobalsBegin() + { + } + + void handleGlobalVariable( + const Type* ElemType, ///< The type of the global variable + bool isConstant, ///< Whether the GV is constant or not + GlobalValue::LinkageTypes ///< The linkage type of the GV + ) + { + } + + void handleInitializedGV( + const Type* ElemType, ///< The type of the global variable + bool isConstant, ///< Whether the GV is constant or not + GlobalValue::LinkageTypes,///< The linkage type of the GV + unsigned initSlot ///< Slot number of GV's initializer + ) + { + } + + virtual void handleType( const Type* Ty ) + { + } + + void handleFunctionDeclaration( + const Type* FuncType ///< The type of the function + ) + { + } + + void handleModuleGlobalsEnd() + { + } + + void handleCompactionTableBegin() + { + } + + void handleCompactionTablePlane( + unsigned Ty, + unsigned NumEntries + ) + { + } + + void handleCompactionTableType( + unsigned i, + unsigned TypSlot, + const Type* + ) + { + } + + void handleCompactionTableValue( + unsigned i, + unsigned ValSlot, + const Type* + ) + { + } + + void handleCompactionTableEnd() + { + } + + void handleSymbolTableBegin() + { + } + + void handleSymbolTablePlane( + unsigned Ty, + unsigned NumEntries, + const Type* Typ + ) + { + } + + void handleSymbolTableType( + unsigned i, + unsigned slot, + const std::string& name + ) + { + } + + void handleSymbolTableValue( + unsigned i, + unsigned slot, + const std::string& name + ) + { + } + + void handleSymbolTableEnd() + { + } + + void handleFunctionBegin( + const Type* FType, + GlobalValue::LinkageTypes linkage + ) + { + } + + void handleFunctionEnd( + const Type* FType + ) + { + } + + void handleBasicBlockBegin( + unsigned blocknum + ) + { + } + + bool handleInstruction( + unsigned Opcode, + const Type* iType, + std::vector<unsigned>& Operands + ) + { + return false; + } + + void handleBasicBlockEnd(unsigned blocknum) + { + } + + void handleGlobalConstantsBegin() + { + } + + void handleConstantExpression( + unsigned Opcode, + const Type* Typ, + std::vector<std::pair<const Type*,unsigned> > ArgVec + ) + { + } + + void handleConstantValue( Constant * c ) + { + } + + void handleConstantArray( + const ArrayType* AT, + std::vector<unsigned>& Elements ) + { + } + + void handleConstantStruct( + const StructType* ST, + std::vector<unsigned>& ElementSlots) + { + } + + void handleConstantPointer( + const PointerType* PT, unsigned Slot) + { + } + + void handleConstantString( const ConstantArray* CA ) + { + } + + + void handleGlobalConstantsEnd() + { + } + +}; + +} + +void llvm::BytecodeAnalyzer::AnalyzeBytecode( + const unsigned char *Buf, + unsigned Length, + BytecodeAnalysis& bca, + const std::string &ModuleID +) +{ + AnalyzerHandler TheHandler; + AbstractBytecodeParser TheParser(&TheHandler); + TheParser.ParseBytecode( Buf, Length, ModuleID ); + TheParser.ParseAllFunctionBodies(); +} + +// vim: sw=2 diff --git a/lib/Bytecode/Analyzer/AnalyzerInternals.h b/lib/Bytecode/Analyzer/AnalyzerInternals.h new file mode 100644 index 0000000000..d9a2e843d8 --- /dev/null +++ b/lib/Bytecode/Analyzer/AnalyzerInternals.h @@ -0,0 +1,65 @@ +//===-- ReaderInternals.h - Definitions internal to the reader --*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by the LLVM research group and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This header file defines various stuff that is used by the bytecode reader. +// +//===----------------------------------------------------------------------===// + +#ifndef ANALYZER_INTERNALS_H +#define ANALYZER_INTERNALS_H + +#include "Parser.h" +#include "llvm/Bytecode/Analyzer.h" + +// Enable to trace to figure out what the heck is going on when parsing fails +//#define TRACE_LEVEL 10 +//#define DEBUG_OUTPUT + +#if TRACE_LEVEL // ByteCodeReading_TRACEr +#define BCR_TRACE(n, X) \ + if (n < TRACE_LEVEL) std::cerr << std::string(n*2, ' ') << X +#else +#define BCR_TRACE(n, X) +#endif + +namespace llvm { + +class BytecodeAnalyzer { + BytecodeAnalyzer(const BytecodeAnalyzer &); // DO NOT IMPLEMENT + void operator=(const BytecodeAnalyzer &); // DO NOT IMPLEMENT +public: + BytecodeAnalyzer() { } + ~BytecodeAnalyzer() { } + + void AnalyzeBytecode( + const unsigned char *Buf, + unsigned Length, + BytecodeAnalysis& bca, + const std::string &ModuleID + ); + + void DumpBytecode( + const unsigned char *Buf, + unsigned Length, + BytecodeAnalysis& bca, + const std::string &ModuleID + ); + + void dump() const { + std::cerr << "BytecodeParser instance!\n"; + } +private: + BytecodeAnalysis TheAnalysis; +}; + +} // End llvm namespace + +#endif + +// vim: sw=2 diff --git a/lib/Bytecode/Analyzer/AnalyzerWrappers.cpp b/lib/Bytecode/Analyzer/AnalyzerWrappers.cpp new file mode 100644 index 0000000000..a0e4845a1b --- /dev/null +++ b/lib/Bytecode/Analyzer/AnalyzerWrappers.cpp @@ -0,0 +1,208 @@ +//===- AnalyzerWrappers.cpp - Analyze bytecode from file or buffer -------===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Reid Spencer and is distributed under the +// University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements loading and analysis of a bytecode file and analyzing a +// bytecode buffer. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Bytecode/Analyzer.h" +#include "AnalyzerInternals.h" +#include "Support/FileUtilities.h" +#include "Support/StringExtras.h" +#include "Config/unistd.h" +#include <cerrno> + +using namespace llvm; + +//===----------------------------------------------------------------------===// +// BytecodeFileAnalyzer - Analyze from an mmap'able file descriptor. +// + +namespace { + /// BytecodeFileAnalyzer - parses a bytecode file from a file + class BytecodeFileAnalyzer : public BytecodeAnalyzer { + private: + unsigned char *Buffer; + unsigned Length; + + BytecodeFileAnalyzer(const BytecodeFileAnalyzer&); // Do not implement + void operator=(const BytecodeFileAnalyzer &BFR); // Do not implement + + public: + BytecodeFileAnalyzer(const std::string &Filename, BytecodeAnalysis& bca); + ~BytecodeFileAnalyzer(); + }; +} + +static std::string ErrnoMessage (int savedErrNum, std::string descr) { + return ::strerror(savedErrNum) + std::string(", while trying to ") + descr; +} + +BytecodeFileAnalyzer::BytecodeFileAnalyzer(const std::string &Filename, + BytecodeAnalysis& bca) { + Buffer = (unsigned char*)ReadFileIntoAddressSpace(Filename, Length); + if (Buffer == 0) + throw "Error reading file '" + Filename + "'."; + + try { + // Parse the bytecode we mmapped in + if ( bca.dumpBytecode ) + DumpBytecode(Buffer, Length, bca, Filename); + AnalyzeBytecode(Buffer, Length, bca, Filename); + } catch (...) { + UnmapFileFromAddressSpace(Buffer, Length); + throw; + } +} + +BytecodeFileAnalyzer::~BytecodeFileAnalyzer() { + // Unmmap the bytecode... + UnmapFileFromAddressSpace(Buffer, Length); +} + +//===----------------------------------------------------------------------===// +// BytecodeBufferAnalyzer - Read from a memory buffer +// + +namespace { + /// BytecodeBufferAnalyzer - parses a bytecode file from a buffer + /// + class BytecodeBufferAnalyzer : public BytecodeAnalyzer { + private: + const unsigned char *Buffer; + bool MustDelete; + + BytecodeBufferAnalyzer(const BytecodeBufferAnalyzer&); // Do not implement + void operator=(const BytecodeBufferAnalyzer &BFR); // Do not implement + + public: + BytecodeBufferAnalyzer(const unsigned char *Buf, unsigned Length, + BytecodeAnalysis& bca, const std::string &ModuleID); + ~BytecodeBufferAnalyzer(); + + }; +} + +BytecodeBufferAnalyzer::BytecodeBufferAnalyzer(const unsigned char *Buf, + unsigned Length, + BytecodeAnalysis& bca, + const std::string &ModuleID) { + // If not aligned, allocate a new buffer to hold the bytecode... + const unsigned char *ParseBegin = 0; + if ((intptr_t)Buf & 3) { + Buffer = new unsigned char[Length+4]; + unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned + ParseBegin = Buffer + Offset; + memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over + MustDelete = true; + } else { + // If we don't need to copy it over, just use the caller's copy + ParseBegin = Buffer = Buf; + MustDelete = false; + } + try { + if ( bca.dumpBytecode ) + DumpBytecode(ParseBegin, Length, bca, ModuleID); + AnalyzeBytecode(ParseBegin, Length, bca, ModuleID); + } catch (...) { + if (MustDelete) delete [] Buffer; + throw; + } +} + +BytecodeBufferAnalyzer::~BytecodeBufferAnalyzer() { + if (MustDelete) delete [] Buffer; +} + +//===----------------------------------------------------------------------===// +// BytecodeStdinAnalyzer - Read bytecode from Standard Input +// + +namespace { + /// BytecodeStdinAnalyzer - parses a bytecode file from stdin + /// + class BytecodeStdinAnalyzer : public BytecodeAnalyzer { + private: + std::vector<unsigned char> FileData; + unsigned char *FileBuf; + + BytecodeStdinAnalyzer(const BytecodeStdinAnalyzer&); // Do not implement + void operator=(const BytecodeStdinAnalyzer &BFR); // Do not implement + + public: + BytecodeStdinAnalyzer(BytecodeAnalysis& bca); + }; +} + +BytecodeStdinAnalyzer::BytecodeStdinAnalyzer(BytecodeAnalysis& bca ) { + int BlockSize; + unsigned char Buffer[4096*4]; + + // Read in all of the data from stdin, we cannot mmap stdin... + while ((BlockSize = ::read(0 /*stdin*/, Buffer, 4096*4))) { + if (BlockSize == -1) + throw ErrnoMessage(errno, "read from standard input"); + + FileData.insert(FileData.end(), Buffer, Buffer+BlockSize); + } + + if (FileData.empty()) + throw std::string("Standard Input empty!"); + + FileBuf = &FileData[0]; + if (bca.dumpBytecode) + DumpBytecode(&FileData[0], FileData.size(), bca, "<stdin>"); + AnalyzeBytecode(FileBuf, FileData.size(), bca, "<stdin>"); +} + +//===----------------------------------------------------------------------===// +// Wrapper functions +//===----------------------------------------------------------------------===// + +// AnalyzeBytecodeFile - analyze one file +void llvm::AnalyzeBytecodeFile(const std::string &Filename, + BytecodeAnalysis& bca, + std::string *ErrorStr) +{ + try { + if ( Filename != "-" ) + BytecodeFileAnalyzer bfa(Filename,bca); + else + BytecodeStdinAnalyzer bsa(bca); + } catch (std::string &err) { + if (ErrorStr) *ErrorStr = err; + } +} + +// AnalyzeBytecodeBuffer - analyze a buffer +void llvm::AnalyzeBytecodeBuffer( + const unsigned char* Buffer, ///< Pointer to start of bytecode buffer + unsigned BufferSize, ///< Size of the bytecode buffer + BytecodeAnalysis& Results, ///< The results of the analysis + std::string* ErrorStr ///< Errors, if any. + ) +{ + try { + BytecodeBufferAnalyzer(Buffer, BufferSize, Results, "<buffer>" ); + } catch (std::string& err ) { + if ( ErrorStr) *ErrorStr = err; + } +} + + +/// This function prints the contents of rhe BytecodeAnalysis structure in +/// a human legible form. +/// @brief Print BytecodeAnalysis structure to an ostream +void llvm::PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out ) +{ + Out << "Not Implemented Yet.\n"; +} + +// vim: sw=2 diff --git a/lib/Bytecode/Analyzer/BytecodeHandler.cpp b/lib/Bytecode/Analyzer/BytecodeHandler.cpp new file mode 100644 index 0000000000..2415958867 --- /dev/null +++ b/lib/Bytecode/Analyzer/BytecodeHandler.cpp @@ -0,0 +1,220 @@ +//===-- BytecodeHandler.cpp - Parsing Handler -------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Reid Spencer and is distributed under the +// University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This header file defines the BytecodeHandler class that gets called by the +// AbstractBytecodeParser when parsing events occur. +// +//===----------------------------------------------------------------------===// + +#include "BytecodeHandler.h" + +using namespace llvm; + +bool BytecodeHandler::handleError(const std::string& str ) +{ + return false; +} + +void BytecodeHandler::handleStart() +{ +} + +void BytecodeHandler::handleFinish() +{ +} + +void BytecodeHandler::handleModuleBegin(const std::string& id) +{ +} + +void BytecodeHandler::handleModuleEnd(const std::string& id) +{ +} + +void BytecodeHandler::handleVersionInfo( + unsigned char RevisionNum, ///< Byte code revision number + Module::Endianness Endianness, ///< Endianness indicator + Module::PointerSize PointerSize ///< PointerSize indicator +) +{ +} + +void BytecodeHandler::handleModuleGlobalsBegin() +{ +} + +void BytecodeHandler::handleGlobalVariable( + const Type* ElemType, ///< The type of the global variable + bool isConstant, ///< Whether the GV is constant or not + GlobalValue::LinkageTypes ///< The linkage type of the GV +) +{ +} + +void BytecodeHandler::handleInitializedGV( + const Type* ElemType, ///< The type of the global variable + bool isConstant, ///< Whether the GV is constant or not + GlobalValue::LinkageTypes,///< The linkage type of the GV + unsigned initSlot ///< Slot number of GV's initializer +) +{ +} + +void BytecodeHandler::handleType( const Type* Ty ) +{ +} + +void BytecodeHandler::handleFunctionDeclaration( + const Type* FuncType ///< The type of the function +) +{ +} + +void BytecodeHandler::handleModuleGlobalsEnd() +{ +} + +void BytecodeHandler::handleCompactionTableBegin() +{ +} + +void BytecodeHandler::handleCompactionTablePlane( + unsigned Ty, + unsigned NumEntries +) +{ +} + +void BytecodeHandler::handleCompactionTableType( + unsigned i, + unsigned TypSlot, + const Type* +) +{ +} + +void BytecodeHandler::handleCompactionTableValue( + unsigned i, + unsigned ValSlot, + const Type* +) +{ +} + +void BytecodeHandler::handleCompactionTableEnd() +{ +} + +void BytecodeHandler::handleSymbolTableBegin() +{ +} + +void BytecodeHandler::handleSymbolTablePlane( + unsigned Ty, + unsigned NumEntries, + const Type* Typ +) +{ +} + +void BytecodeHandler::handleSymbolTableType( + unsigned i, + unsigned slot, + const std::string& name +) +{ +} + +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::handleBasicBlockBegin( + unsigned blocknum +) +{ +} + +bool BytecodeHandler::handleInstruction( + unsigned Opcode, + const Type* iType, + std::vector<unsigned>& Operands +) +{ + return false; +} + +void BytecodeHandler::handleBasicBlockEnd(unsigned blocknum) +{ +} + +void BytecodeHandler::handleGlobalConstantsBegin() +{ +} + +void BytecodeHandler::handleConstantExpression( + unsigned Opcode, + const Type* Typ, + std::vector<std::pair<const Type*,unsigned> > ArgVec + ) +{ +} + +void BytecodeHandler::handleConstantValue( Constant * c ) +{ +} + +void BytecodeHandler::handleConstantArray( + const ArrayType* AT, + std::vector<unsigned>& Elements ) +{ +} + +void BytecodeHandler::handleConstantStruct( + const StructType* ST, + std::vector<unsigned>& ElementSlots) +{ +} + +void BytecodeHandler::handleConstantPointer( + const PointerType* PT, unsigned Slot) +{ +} + +void BytecodeHandler::handleConstantString( const ConstantArray* CA ) +{ +} + + +void BytecodeHandler::handleGlobalConstantsEnd() +{ +} + +// vim: sw=2 diff --git a/lib/Bytecode/Analyzer/BytecodeHandler.h b/lib/Bytecode/Analyzer/BytecodeHandler.h new file mode 100644 index 0000000000..2b03e2d332 --- /dev/null +++ b/lib/Bytecode/Analyzer/BytecodeHandler.h @@ -0,0 +1,247 @@ +//===-- BytecodeHandler.h - Parsing Handler ---------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Reid Spencer and is distributed under the +// University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This header file defines the BytecodeHandler class that gets called by the +// AbstractBytecodeParser when parsing events occur. +// +//===----------------------------------------------------------------------===// + +#ifndef BYTECODE_HANDLER_H +#define BYTECODE_HANDLER_H + +#include "llvm/Module.h" +#include "llvm/GlobalValue.h" +#include <vector> + +namespace llvm { + +class ArrayType; +class StructType; +class PointerType; +class ConstantArray; + +/// This class provides the interface for the handling bytecode events during +/// parsing. The methods on this interface are invoked by the +/// AbstractBytecodeParser as it discovers the content of a bytecode stream. +/// This class provides a a clear separation of concerns between recognizing +/// the semantic units of a bytecode file and deciding what to do with them. +/// The AbstractBytecodeParser recognizes the content of the bytecode file and +/// calls the BytecodeHandler methods to determine what should be done. This +/// arrangement allows Bytecode files to be read and handled for a number of +/// purposes simply by creating a subclass of BytecodeHandler. None of the +/// parsing details need to be understood, only the meaning of the calls +/// made on this interface. +/// +/// Another paradigm that uses this design pattern is the XML SAX Parser. The +/// ContentHandler for SAX plays the same role as the BytecodeHandler here. +/// @brief Handle Bytecode Parsing Events +class BytecodeHandler { + +/// @name Constructors And Operators +/// @{ +public: + /// @brief Default constructor (empty) + BytecodeHandler() {} + /// @brief Virtual destructor (empty) + virtual ~BytecodeHandler() {} + +private: + BytecodeHandler(const BytecodeHandler &); // DO NOT IMPLEMENT + void operator=(const BytecodeHandler &); // DO NOT IMPLEMENT + +/// @} +/// @name Handler Methods +/// @{ +public: + + /// This method is called whenever the parser detects an error in the + /// bytecode formatting. Returning true will cause the parser to keep + /// going, however this is inadvisable in most cases. Returning false will + /// cause the parser to throw the message as a std::string. + /// @brief Handle parsing errors. + virtual bool handleError(const std::string& str ); + + /// This method is called at the beginning of a parse before anything is + /// read in order to give the handler a chance to initialize. + /// @brief Handle the start of a bytecode parse + virtual void handleStart(); + + /// This method is called at the end of a parse after everything has been + /// read in order to give the handler a chance to terminate. + /// @brief Handle the end of a bytecode parse + virtual void handleFinish(); + + /// This method is called at the start of a module to indicate that a + /// module is being parsed. + /// @brief Handle the start of a module. + virtual void handleModuleBegin(const std::string& id); + + /// This method is called at the end of a module to indicate that the module + /// previously being parsed has concluded. + /// @brief Handle the end of a module. + virtual void handleModuleEnd(const std::string& id); + + /// This method is called once the version information has been parsed. It + /// provides the information about the version of the bytecode file being + /// read. + /// @brief Handle the bytecode prolog + virtual void handleVersionInfo( + unsigned char RevisionNum, ///< Byte code revision number + Module::Endianness Endianness, ///< Endianness indicator + Module::PointerSize PointerSize ///< PointerSize indicator + ); + + /// This method is called at the start of a module globals block which + /// contains the global variables and the function placeholders + virtual void handleModuleGlobalsBegin(); + + /// This method is called when a non-initialized global variable is + /// recognized. Its type, constness, and linkage type are provided. + /// @brief Handle a non-initialized global variable + virtual void handleGlobalVariable( + const Type* ElemType, ///< The type of the global variable + bool isConstant, ///< Whether the GV is constant or not + GlobalValue::LinkageTypes ///< The linkage type of the GV + ); + + /// This method is called when an initialized global variable is recognized. + /// Its type constness, linkage type, and the slot number of the initializer + /// are provided. + /// @brief Handle an intialized global variable. + virtual void handleInitializedGV( + const Type* ElemType, ///< The type of the global variable + bool isConstant, ///< Whether the GV is constant or not + GlobalValue::LinkageTypes,///< The linkage type of the GV + unsigned initSlot ///< Slot number of GV's initializer + ); + + /// This method is called when a new type is recognized. The type is + /// converted from the bytecode and passed to this method. + /// @brief Handle a type + virtual void handleType( const Type* Ty ); + + /// 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 + ); + + /// This method is called at the end of the module globals block. + /// @brief Handle end of module globals block. + virtual void handleModuleGlobalsEnd(); + + /// This method is called at the beginning of a compaction table. + /// @brief Handle start of compaction table. + virtual void handleCompactionTableBegin(); + virtual void handleCompactionTablePlane( + unsigned Ty, + unsigned NumEntries + ); + + virtual void handleCompactionTableType( + unsigned i, + unsigned TypSlot, + const Type* + ); + + virtual void handleCompactionTableValue( + unsigned i, + unsigned ValSlot, + const Type* + ); + + virtual void handleCompactionTableEnd(); + + virtual void handleSymbolTableBegin(); + + virtual void handleSymbolTablePlane( + unsigned Ty, + unsigned NumEntries, + const Type* Ty + ); + + virtual void handleSymbolTableType( + unsigned i, + unsigned slot, + const std::string& name + ); + + virtual void handleSymbolTableValue( + unsigned i, + unsigned slot, + const std::string& name + ); + + virtual void handleSymbolTableEnd(); + + virtual void handleFunctionBegin( + const Type* FType, + GlobalValue::LinkageTypes linkage + ); + + virtual void handleFunctionEnd( + const Type* FType + ); + + virtual void handleBasicBlockBegin( + unsigned blocknum + ); + + /// This method is called for each instruction that is parsed. + /// @returns true if the instruction is a block terminating instruction + /// @brief Handle an instruction + virtual bool handleInstruction( + unsigned Opcode, + const Type* iType, + std::vector<unsigned>& Operands + ); + + /// This method is called for each block that is parsed. + virtual void handleBasicBlockEnd(unsigned blocknum); + /// This method is called at the start of the global constants block. + /// @brief Handle start of global constants block. + virtual void handleGlobalConstantsBegin(); + + virtual void handleConstantExpression( + unsigned Opcode, + const Type* Typ, + std::vector<std::pair<const Type*,unsigned> > ArgVec + ); + + virtual void handleConstantArray( + const ArrayType* AT, + std::vector<unsigned>& ElementSlots + ); + + virtual void handleConstantStruct( + const StructType* ST, + std::vector<unsigned>& ElementSlots + ); + + virtual void handleConstantPointer( + const PointerType* PT, + unsigned Slot + ); + + virtual void handleConstantString( + const ConstantArray* CA + ); + + virtual void handleConstantValue( Constant * c ); + virtual void handleGlobalConstantsEnd(); + +/// @} + +}; + +} // End llvm namespace + +#endif + +// vim: sw=2 diff --git a/lib/Bytecode/Analyzer/Dumper.cpp b/lib/Bytecode/Analyzer/Dumper.cpp new file mode 100644 index 0000000000..6ff4ea0c79 --- /dev/null +++ b/lib/Bytecode/Analyzer/Dumper.cpp @@ -0,0 +1,311 @@ +//===-- BytecodeDumper.cpp - Parsing Handler --------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Reid Spencer and is distributed under the +// University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This header file defines the BytecodeDumper class that gets called by the +// AbstractBytecodeParser when parsing events occur. It merely dumps the +// information presented to it from the parser. +// +//===----------------------------------------------------------------------===// + +#include "AnalyzerInternals.h" +#include "llvm/Constant.h" +#include "llvm/Constants.h" +#include "llvm/DerivedTypes.h" +#include "llvm/Instruction.h" +#include "llvm/Type.h" + +using namespace llvm; + +namespace { + +class BytecodeDumper : public llvm::BytecodeHandler { +public: + + virtual bool handleError(const std::string& str ) + { + std::cout << "ERROR: " << str << "\n"; + return true; + } + + virtual void handleStart() + { + std::cout << "Bytecode {\n"; + } + + virtual void handleFinish() + { + std::cout << "} End Bytecode\n"; + } + + virtual void handleModuleBegin(const std::string& id) + { + std::cout << " Module " << id << " {\n"; + } + + virtual void handleModuleEnd(const std::string& id) + { + std::cout << " } End Module " << id << "\n"; + } + + virtual void handleVersionInfo( + unsigned char RevisionNum, ///< Byte code revision number + Module::Endianness Endianness, ///< Endianness indicator + Module::PointerSize PointerSize ///< PointerSize indicator + ) + { + std::cout << " RevisionNum: " << int(RevisionNum) + << " Endianness: " << Endianness + << " PointerSize: " << PointerSize << "\n"; + } + + virtual void handleModuleGlobalsBegin() + { + std::cout << " BLOCK: ModuleGlobalInfo {\n"; + } + + virtual void handleGlobalVariable( + const Type* ElemType, ///< The type of the global variable + bool isConstant, ///< Whether the GV is constant or not + GlobalValue::LinkageTypes Linkage ///< The linkage type of the GV + ) + { + std::cout << " GV: Uninitialized, " + << ( isConstant? "Constant, " : "Variable, ") + << " Linkage=" << Linkage << " Type=" + << ElemType->getDescription() << "\n"; + } + + virtual void handleInitializedGV( + const Type* ElemType, ///< The type of the global variable + bool isConstant, ///< Whether the GV is constant or not + GlobalValue::LinkageTypes Linkage,///< The linkage type of the GV + unsigned initSlot ///< Slot number of GV's initializer + ) + { + std::cout << " GV: Initialized, " + << ( isConstant? "Constant, " : "Variable, ")< |