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 )
100 std::cout << " Type: " << Ty->getDescription() << "\n";
103 virtual void handleFunctionDeclaration( const Type* FuncType )
105 std::cout << " Function: " << FuncType->getDescription() << "\n";
108 virtual void handleModuleGlobalsEnd()
110 std::cout << " } END BLOCK: ModuleGlobalInfo\n";
113 void handleCompactionTableBegin()
115 std::cout << " BLOCK: CompactionTable {\n";
118 virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries )
120 std::cout << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
123 virtual void handleCompactionTableType(
129 std::cout << " Type: " << i << " Slot:" << TypSlot
130 << " is " << Ty->getDescription() << "\n";
133 virtual void handleCompactionTableValue(
139 std::cout << " Value: " << i << " Slot:" << ValSlot
140 << " is " << Ty->getDescription() << "\n";
143 virtual void handleCompactionTableEnd()
145 std::cout << " } END BLOCK: CompactionTable\n";
148 virtual void handleSymbolTableBegin()
150 std::cout << " BLOCK: SymbolTable {\n";
153 virtual void handleSymbolTablePlane(
159 std::cout << " Plane: Ty=" << Ty << " Size=" << NumEntries
160 << " Type: " << Typ->getDescription() << "\n";
163 virtual void handleSymbolTableType(
166 const std::string& name
169 std::cout << " Type " << i << " Slot=" << slot
170 << " Name: " << name << "\n";
173 virtual void handleSymbolTableValue(
176 const std::string& name
179 std::cout << " Value " << i << " Slot=" << slot
180 << " Name: " << name << "\n";
183 virtual void handleSymbolTableEnd()
185 std::cout << " } END BLOCK: SymbolTable\n";
188 virtual void handleFunctionBegin(
190 GlobalValue::LinkageTypes linkage
193 std::cout << "BLOCK: Function {\n";
194 std::cout << " Linkage: " << linkage << "\n";
195 std::cout << " Type: " << FType->getDescription() << "\n";
198 virtual void handleFunctionEnd(
202 std::cout << "} END BLOCK: Function\n";
205 virtual void handleBasicBlockBegin(
209 std::cout << " BLOCK: BasicBlock #" << blocknum << "{\n";
212 virtual bool handleInstruction(
215 std::vector<unsigned>& Operands
218 std::cout << " INST: OpCode="
219 << Instruction::getOpcodeName(Opcode) << " Type="
220 << iType->getDescription() << "\n";
221 for ( unsigned i = 0; i < Operands.size(); ++i )
222 std::cout << " Op#" << i << " Slot=" << Operands[i] << "\n";
224 return Instruction::isTerminator(Opcode);
227 virtual void handleBasicBlockEnd(unsigned blocknum)
229 std::cout << " } END BLOCK: BasicBlock #" << blocknum << "{\n";
232 virtual void handleGlobalConstantsBegin()
234 std::cout << " BLOCK: GlobalConstants {\n";
237 virtual void handleConstantExpression(
240 std::vector<std::pair<const Type*,unsigned> > ArgVec
243 std::cout << " EXPR: " << Instruction::getOpcodeName(Opcode)
244 << " Type=" << Typ->getDescription() << "\n";
245 for ( unsigned i = 0; i < ArgVec.size(); ++i )
246 std::cout << " Arg#" << i << " Type="
247 << ArgVec[i].first->getDescription() << " Slot="
248 << ArgVec[i].second << "\n";
251 virtual void handleConstantValue( Constant * c )
253 std::cout << " VALUE: ";
258 virtual void handleConstantArray(
260 std::vector<unsigned>& Elements )
262 std::cout << " ARRAY: " << AT->getDescription() << "\n";
263 for ( unsigned i = 0; i < Elements.size(); ++i )
264 std::cout << " #" << i << " Slot=" << Elements[i] << "\n";
267 virtual void handleConstantStruct(
268 const StructType* ST,
269 std::vector<unsigned>& Elements)
271 std::cout << " STRUC: " << ST->getDescription() << "\n";
272 for ( unsigned i = 0; i < Elements.size(); ++i )
273 std::cout << " #" << i << " Slot=" << Elements[i] << "\n";
276 virtual void handleConstantPointer(
277 const PointerType* PT, unsigned Slot)
279 std::cout << " POINT: " << PT->getDescription()
280 << " Slot=" << Slot << "\n";
283 virtual void handleConstantString( const ConstantArray* CA )
285 std::cout << " STRNG: ";
286 CA->print(std::cout);
290 virtual void handleGlobalConstantsEnd()
292 std::cout << " } END BLOCK: GlobalConstants\n";
298 void BytecodeAnalyzer::DumpBytecode(
299 const unsigned char *Buf,
301 BytecodeAnalysis& bca,
302 const std::string &ModuleID
305 BytecodeDumper TheHandler;
306 AbstractBytecodeParser TheParser(&TheHandler);
307 TheParser.ParseBytecode( Buf, Length, ModuleID );
308 if ( bca.detailedResults )
309 TheParser.ParseAllFunctionBodies();