1 //===-- BytecodeDumper.cpp - Parsing Handler --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header file defines the BytecodeDumper class that gets called by the
11 // AbstractBytecodeParser when parsing events occur. It merely dumps the
12 // information presented to it from the parser.
14 //===----------------------------------------------------------------------===//
16 #include "AnalyzerInternals.h"
17 #include "llvm/Constant.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Instruction.h"
21 #include "llvm/Type.h"
27 class BytecodeDumper : public llvm::BytecodeHandler {
30 virtual bool handleError(const std::string& str )
32 std::cout << "ERROR: " << str << "\n";
36 virtual void handleStart()
38 std::cout << "Bytecode {\n";
41 virtual void handleFinish()
43 std::cout << "} End Bytecode\n";
46 virtual void handleModuleBegin(const std::string& id)
48 std::cout << " Module " << id << " {\n";
51 virtual void handleModuleEnd(const std::string& id)
53 std::cout << " } End Module " << id << "\n";
56 virtual void handleVersionInfo(
57 unsigned char RevisionNum, ///< Byte code revision number
58 Module::Endianness Endianness, ///< Endianness indicator
59 Module::PointerSize PointerSize ///< PointerSize indicator
62 std::cout << " RevisionNum: " << int(RevisionNum)
63 << " Endianness: " << Endianness
64 << " PointerSize: " << PointerSize << "\n";
67 virtual void handleModuleGlobalsBegin()
69 std::cout << " BLOCK: ModuleGlobalInfo {\n";
72 virtual void handleGlobalVariable(
73 const Type* ElemType, ///< The type of the global variable
74 bool isConstant, ///< Whether the GV is constant or not
75 GlobalValue::LinkageTypes Linkage ///< The linkage type of the GV
78 std::cout << " GV: Uninitialized, "
79 << ( isConstant? "Constant, " : "Variable, ")
80 << " Linkage=" << Linkage << " Type="
81 << ElemType->getDescription() << "\n";
84 virtual void handleInitializedGV(
85 const Type* ElemType, ///< The type of the global variable
86 bool isConstant, ///< Whether the GV is constant or not
87 GlobalValue::LinkageTypes Linkage,///< The linkage type of the GV
88 unsigned initSlot ///< Slot number of GV's initializer
91 std::cout << " GV: Initialized, "
92 << ( isConstant? "Constant, " : "Variable, ")
93 << " Linkage=" << Linkage << " Type="
94 << ElemType->getDescription()
95 << " InitializerSlot=" << initSlot << "\n";
98 virtual void handleType( const Type* Ty ) {
99 std::cout << " Type: " << Ty->getDescription() << "\n";
102 virtual void handleFunctionDeclaration(
104 const FunctionType* FuncType) {
105 std::cout << " Function: " << FuncType->getDescription() << "\n";
108 virtual void handleModuleGlobalsEnd() {
109 std::cout << " } END BLOCK: ModuleGlobalInfo\n";
112 virtual void handleCompactionTableBegin() {
113 std::cout << " BLOCK: CompactionTable {\n";
116 virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries ) {
117 std::cout << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
120 virtual void handleCompactionTableType(
124 std::cout << " Type: " << i << " Slot:" << TypSlot
125 << " is " << Ty->getDescription() << "\n";
128 virtual void handleCompactionTableValue(
132 std::cout << " Value: " << i << " Slot:" << ValSlot
133 << " is " << Ty->getDescription() << "\n";
136 virtual void handleCompactionTableEnd() {
137 std::cout << " } END BLOCK: CompactionTable\n";
140 virtual void handleSymbolTableBegin() {
141 std::cout << " BLOCK: SymbolTable {\n";
144 virtual void handleSymbolTablePlane(
148 std::cout << " Plane: Ty=" << Ty << " Size=" << NumEntries
149 << " Type: " << Typ->getDescription() << "\n";
152 virtual void handleSymbolTableType(
155 const std::string& name ) {
156 std::cout << " Type " << i << " Slot=" << slot
157 << " Name: " << name << "\n";
160 virtual void handleSymbolTableValue(
163 const std::string& name ) {
164 std::cout << " Value " << i << " Slot=" << slot
165 << " Name: " << name << "\n";
168 virtual void handleSymbolTableEnd() {
169 std::cout << " } END BLOCK: SymbolTable\n";
172 virtual void handleFunctionBegin(
173 const Type* FType, GlobalValue::LinkageTypes linkage ) {
174 std::cout << "BLOCK: Function {\n";
175 std::cout << " Linkage: " << linkage << "\n";
176 std::cout << " Type: " << FType->getDescription() << "\n";
179 virtual void handleFunctionEnd( const Type* FType) {
180 std::cout << "} END BLOCK: Function\n";
183 virtual void handleBasicBlockBegin( unsigned blocknum) {
184 std::cout << " BLOCK: BasicBlock #" << blocknum << "{\n";
187 virtual bool handleInstruction(
190 std::vector<unsigned>& Operands,
192 std::cout << " INST: OpCode="
193 << Instruction::getOpcodeName(Opcode) << " Type="
194 << iType->getDescription() << "\n";
195 for ( unsigned i = 0; i < Operands.size(); ++i )
196 std::cout << " Op#" << i << " Slot=" << Operands[i] << "\n";
198 return Instruction::isTerminator(Opcode);
201 virtual void handleBasicBlockEnd(unsigned blocknum) {
202 std::cout << " } END BLOCK: BasicBlock #" << blocknum << "{\n";
205 virtual void handleGlobalConstantsBegin() {
206 std::cout << " BLOCK: GlobalConstants {\n";
209 virtual void handleConstantExpression(
212 std::vector<std::pair<const Type*,unsigned> > ArgVec
214 std::cout << " EXPR: " << Instruction::getOpcodeName(Opcode)
215 << " Type=" << Typ->getDescription() << "\n";
216 for ( unsigned i = 0; i < ArgVec.size(); ++i )
217 std::cout << " Arg#" << i << " Type="
218 << ArgVec[i].first->getDescription() << " Slot="
219 << ArgVec[i].second << "\n";
222 virtual void handleConstantValue( Constant * c ) {
223 std::cout << " VALUE: ";
228 virtual void handleConstantArray(const ArrayType* AT,
229 std::vector<unsigned>& Elements ) {
230 std::cout << " ARRAY: " << AT->getDescription() << "\n";
231 for ( unsigned i = 0; i < Elements.size(); ++i )
232 std::cout << " #" << i << " Slot=" << Elements[i] << "\n";
235 virtual void handleConstantStruct( const StructType* ST,
236 std::vector<unsigned>& Elements) {
237 std::cout << " STRUC: " << ST->getDescription() << "\n";
238 for ( unsigned i = 0; i < Elements.size(); ++i )
239 std::cout << " #" << i << " Slot=" << Elements[i] << "\n";
242 virtual void handleConstantPointer(
243 const PointerType* PT, unsigned Slot)
245 std::cout << " POINT: " << PT->getDescription()
246 << " Slot=" << Slot << "\n";
249 virtual void handleConstantString( const ConstantArray* CA ) {
250 std::cout << " STRNG: ";
251 CA->print(std::cout);
255 virtual void handleGlobalConstantsEnd() {
256 std::cout << " } END BLOCK: GlobalConstants\n";
262 void BytecodeAnalyzer::DumpBytecode(
263 const unsigned char *Buf,
265 BytecodeAnalysis& bca,
266 const std::string &ModuleID) {
267 BytecodeDumper TheHandler;
268 AbstractBytecodeParser TheParser(&TheHandler);
269 TheParser.ParseBytecode( Buf, Length, ModuleID );
270 if ( bca.detailedResults )
271 TheParser.ParseAllFunctionBodies();