blob: 280a12ba73f0066e9714a2306a4bfefd666d36a8 [file] [log] [blame]
Reid Spencerdac69c82004-06-07 17:53:43 +00001//===-- BytecodeDumper.cpp - Parsing Handler --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
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.
13//
14//===----------------------------------------------------------------------===//
15
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"
22
23using namespace llvm;
24
25namespace {
26
27class BytecodeDumper : public llvm::BytecodeHandler {
28public:
29
30 virtual bool handleError(const std::string& str )
31 {
32 std::cout << "ERROR: " << str << "\n";
33 return true;
34 }
35
36 virtual void handleStart()
37 {
38 std::cout << "Bytecode {\n";
39 }
40
41 virtual void handleFinish()
42 {
43 std::cout << "} End Bytecode\n";
44 }
45
46 virtual void handleModuleBegin(const std::string& id)
47 {
48 std::cout << " Module " << id << " {\n";
49 }
50
51 virtual void handleModuleEnd(const std::string& id)
52 {
53 std::cout << " } End Module " << id << "\n";
54 }
55
56 virtual void handleVersionInfo(
57 unsigned char RevisionNum, ///< Byte code revision number
58 Module::Endianness Endianness, ///< Endianness indicator
59 Module::PointerSize PointerSize ///< PointerSize indicator
60 )
61 {
62 std::cout << " RevisionNum: " << int(RevisionNum)
63 << " Endianness: " << Endianness
64 << " PointerSize: " << PointerSize << "\n";
65 }
66
67 virtual void handleModuleGlobalsBegin()
68 {
69 std::cout << " BLOCK: ModuleGlobalInfo {\n";
70 }
71
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
76 )
77 {
78 std::cout << " GV: Uninitialized, "
79 << ( isConstant? "Constant, " : "Variable, ")
80 << " Linkage=" << Linkage << " Type="
81 << ElemType->getDescription() << "\n";
82 }
83
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
89 )
90 {
91 std::cout << " GV: Initialized, "
92 << ( isConstant? "Constant, " : "Variable, ")
93 << " Linkage=" << Linkage << " Type="
94 << ElemType->getDescription()
95 << " InitializerSlot=" << initSlot << "\n";
96 }
97
Reid Spencer2467a062004-06-10 22:00:29 +000098 virtual void handleType( const Type* Ty ) {
Reid Spencerdac69c82004-06-07 17:53:43 +000099 std::cout << " Type: " << Ty->getDescription() << "\n";
100 }
101
Reid Spencer2467a062004-06-10 22:00:29 +0000102 virtual void handleFunctionDeclaration(
103 Function* Func,
104 const FunctionType* FuncType) {
Reid Spencerdac69c82004-06-07 17:53:43 +0000105 std::cout << " Function: " << FuncType->getDescription() << "\n";
106 }
107
Reid Spencer2467a062004-06-10 22:00:29 +0000108 virtual void handleModuleGlobalsEnd() {
Reid Spencerdac69c82004-06-07 17:53:43 +0000109 std::cout << " } END BLOCK: ModuleGlobalInfo\n";
110 }
111
Reid Spencer2467a062004-06-10 22:00:29 +0000112 virtual void handleCompactionTableBegin() {
Reid Spencerdac69c82004-06-07 17:53:43 +0000113 std::cout << " BLOCK: CompactionTable {\n";
114 }
115
Reid Spencer2467a062004-06-10 22:00:29 +0000116 virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries ) {
Reid Spencerdac69c82004-06-07 17:53:43 +0000117 std::cout << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
118 }
119
120 virtual void handleCompactionTableType(
121 unsigned i,
122 unsigned TypSlot,
Reid Spencer2467a062004-06-10 22:00:29 +0000123 const Type* Ty) {
Reid Spencerdac69c82004-06-07 17:53:43 +0000124 std::cout << " Type: " << i << " Slot:" << TypSlot
125 << " is " << Ty->getDescription() << "\n";
126 }
127
128 virtual void handleCompactionTableValue(
129 unsigned i,
130 unsigned ValSlot,
Reid Spencer2467a062004-06-10 22:00:29 +0000131 const Type* Ty ) {
Reid Spencerdac69c82004-06-07 17:53:43 +0000132 std::cout << " Value: " << i << " Slot:" << ValSlot
133 << " is " << Ty->getDescription() << "\n";
134 }
135
Reid Spencer2467a062004-06-10 22:00:29 +0000136 virtual void handleCompactionTableEnd() {
Reid Spencerdac69c82004-06-07 17:53:43 +0000137 std::cout << " } END BLOCK: CompactionTable\n";
138 }
139
Reid Spencer2467a062004-06-10 22:00:29 +0000140 virtual void handleSymbolTableBegin() {
Reid Spencerdac69c82004-06-07 17:53:43 +0000141 std::cout << " BLOCK: SymbolTable {\n";
142 }
143
144 virtual void handleSymbolTablePlane(
145 unsigned Ty,
146 unsigned NumEntries,
Reid Spencer2467a062004-06-10 22:00:29 +0000147 const Type* Typ) {
Reid Spencerdac69c82004-06-07 17:53:43 +0000148 std::cout << " Plane: Ty=" << Ty << " Size=" << NumEntries
149 << " Type: " << Typ->getDescription() << "\n";
150 }
151
152 virtual void handleSymbolTableType(
153 unsigned i,
154 unsigned slot,
Reid Spencer2467a062004-06-10 22:00:29 +0000155 const std::string& name ) {
Reid Spencerdac69c82004-06-07 17:53:43 +0000156 std::cout << " Type " << i << " Slot=" << slot
157 << " Name: " << name << "\n";
158 }
159
160 virtual void handleSymbolTableValue(
161 unsigned i,
162 unsigned slot,
Reid Spencer2467a062004-06-10 22:00:29 +0000163 const std::string& name ) {
Reid Spencerdac69c82004-06-07 17:53:43 +0000164 std::cout << " Value " << i << " Slot=" << slot
165 << " Name: " << name << "\n";
166 }
167
Reid Spencer2467a062004-06-10 22:00:29 +0000168 virtual void handleSymbolTableEnd() {
Reid Spencerdac69c82004-06-07 17:53:43 +0000169 std::cout << " } END BLOCK: SymbolTable\n";
170 }
171
172 virtual void handleFunctionBegin(
Reid Spencer2467a062004-06-10 22:00:29 +0000173 const Type* FType, GlobalValue::LinkageTypes linkage ) {
Reid Spencer0545b3e2004-06-09 06:16:19 +0000174 std::cout << "BLOCK: Function {\n";
175 std::cout << " Linkage: " << linkage << "\n";
176 std::cout << " Type: " << FType->getDescription() << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000177 }
178
Reid Spencer2467a062004-06-10 22:00:29 +0000179 virtual void handleFunctionEnd( const Type* FType) {
Reid Spencer0545b3e2004-06-09 06:16:19 +0000180 std::cout << "} END BLOCK: Function\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000181 }
182
Reid Spencer2467a062004-06-10 22:00:29 +0000183 virtual void handleBasicBlockBegin( unsigned blocknum) {
Reid Spencer0545b3e2004-06-09 06:16:19 +0000184 std::cout << " BLOCK: BasicBlock #" << blocknum << "{\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000185 }
186
187 virtual bool handleInstruction(
188 unsigned Opcode,
189 const Type* iType,
Reid Spencer00c28a72004-06-10 08:09:13 +0000190 std::vector<unsigned>& Operands,
Reid Spencer2467a062004-06-10 22:00:29 +0000191 unsigned Size) {
Reid Spencer0545b3e2004-06-09 06:16:19 +0000192 std::cout << " INST: OpCode="
Reid Spencerdac69c82004-06-07 17:53:43 +0000193 << Instruction::getOpcodeName(Opcode) << " Type="
194 << iType->getDescription() << "\n";
195 for ( unsigned i = 0; i < Operands.size(); ++i )
Reid Spencer0545b3e2004-06-09 06:16:19 +0000196 std::cout << " Op#" << i << " Slot=" << Operands[i] << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000197
198 return Instruction::isTerminator(Opcode);
199 }
200
Reid Spencer2467a062004-06-10 22:00:29 +0000201 virtual void handleBasicBlockEnd(unsigned blocknum) {
Reid Spencer0545b3e2004-06-09 06:16:19 +0000202 std::cout << " } END BLOCK: BasicBlock #" << blocknum << "{\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000203 }
204
Reid Spencer2467a062004-06-10 22:00:29 +0000205 virtual void handleGlobalConstantsBegin() {
Reid Spencerdac69c82004-06-07 17:53:43 +0000206 std::cout << " BLOCK: GlobalConstants {\n";
207 }
208
209 virtual void handleConstantExpression(
210 unsigned Opcode,
211 const Type* Typ,
212 std::vector<std::pair<const Type*,unsigned> > ArgVec
Reid Spencer2467a062004-06-10 22:00:29 +0000213 ) {
Reid Spencerdac69c82004-06-07 17:53:43 +0000214 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";
220 }
221
Reid Spencer2467a062004-06-10 22:00:29 +0000222 virtual void handleConstantValue( Constant * c ) {
Reid Spencerdac69c82004-06-07 17:53:43 +0000223 std::cout << " VALUE: ";
224 c->print(std::cout);
225 std::cout << "\n";
226 }
227
Reid Spencer2467a062004-06-10 22:00:29 +0000228 virtual void handleConstantArray(const ArrayType* AT,
229 std::vector<unsigned>& Elements ) {
Reid Spencerdac69c82004-06-07 17:53:43 +0000230 std::cout << " ARRAY: " << AT->getDescription() << "\n";
231 for ( unsigned i = 0; i < Elements.size(); ++i )
232 std::cout << " #" << i << " Slot=" << Elements[i] << "\n";
233 }
234
Reid Spencer2467a062004-06-10 22:00:29 +0000235 virtual void handleConstantStruct( const StructType* ST,
236 std::vector<unsigned>& Elements) {
Reid Spencerdac69c82004-06-07 17:53:43 +0000237 std::cout << " STRUC: " << ST->getDescription() << "\n";
238 for ( unsigned i = 0; i < Elements.size(); ++i )
239 std::cout << " #" << i << " Slot=" << Elements[i] << "\n";
240 }
241
242 virtual void handleConstantPointer(
243 const PointerType* PT, unsigned Slot)
244 {
245 std::cout << " POINT: " << PT->getDescription()
246 << " Slot=" << Slot << "\n";
247 }
248
Reid Spencer2467a062004-06-10 22:00:29 +0000249 virtual void handleConstantString( const ConstantArray* CA ) {
Reid Spencerdac69c82004-06-07 17:53:43 +0000250 std::cout << " STRNG: ";
251 CA->print(std::cout);
252 std::cout << "\n";
253 }
254
Reid Spencer2467a062004-06-10 22:00:29 +0000255 virtual void handleGlobalConstantsEnd() {
Reid Spencerdac69c82004-06-07 17:53:43 +0000256 std::cout << " } END BLOCK: GlobalConstants\n";
257 }
258};
259
260}
261
262void BytecodeAnalyzer::DumpBytecode(
263 const unsigned char *Buf,
264 unsigned Length,
265 BytecodeAnalysis& bca,
Reid Spencer2467a062004-06-10 22:00:29 +0000266 const std::string &ModuleID) {
Reid Spencerdac69c82004-06-07 17:53:43 +0000267 BytecodeDumper TheHandler;
268 AbstractBytecodeParser TheParser(&TheHandler);
269 TheParser.ParseBytecode( Buf, Length, ModuleID );
Reid Spencer0545b3e2004-06-09 06:16:19 +0000270 if ( bca.detailedResults )
271 TheParser.ParseAllFunctionBodies();
Reid Spencerdac69c82004-06-07 17:53:43 +0000272}
273
274// vim: sw=2