blob: d61afe425581fbf1553889f467f1c2e1a68a8830 [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
98 virtual void handleType( const Type* Ty )
99 {
100 std::cout << " Type: " << Ty->getDescription() << "\n";
101 }
102
103 virtual void handleFunctionDeclaration( const Type* FuncType )
104 {
105 std::cout << " Function: " << FuncType->getDescription() << "\n";
106 }
107
108 virtual void handleModuleGlobalsEnd()
109 {
110 std::cout << " } END BLOCK: ModuleGlobalInfo\n";
111 }
112
113 void handleCompactionTableBegin()
114 {
115 std::cout << " BLOCK: CompactionTable {\n";
116 }
117
118 virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries )
119 {
120 std::cout << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
121 }
122
123 virtual void handleCompactionTableType(
124 unsigned i,
125 unsigned TypSlot,
126 const Type* Ty
127 )
128 {
129 std::cout << " Type: " << i << " Slot:" << TypSlot
130 << " is " << Ty->getDescription() << "\n";
131 }
132
133 virtual void handleCompactionTableValue(
134 unsigned i,
135 unsigned ValSlot,
136 const Type* Ty
137 )
138 {
139 std::cout << " Value: " << i << " Slot:" << ValSlot
140 << " is " << Ty->getDescription() << "\n";
141 }
142
143 virtual void handleCompactionTableEnd()
144 {
145 std::cout << " } END BLOCK: CompactionTable\n";
146 }
147
148 virtual void handleSymbolTableBegin()
149 {
150 std::cout << " BLOCK: SymbolTable {\n";
151 }
152
153 virtual void handleSymbolTablePlane(
154 unsigned Ty,
155 unsigned NumEntries,
156 const Type* Typ
157 )
158 {
159 std::cout << " Plane: Ty=" << Ty << " Size=" << NumEntries
160 << " Type: " << Typ->getDescription() << "\n";
161 }
162
163 virtual void handleSymbolTableType(
164 unsigned i,
165 unsigned slot,
166 const std::string& name
167 )
168 {
169 std::cout << " Type " << i << " Slot=" << slot
170 << " Name: " << name << "\n";
171 }
172
173 virtual void handleSymbolTableValue(
174 unsigned i,
175 unsigned slot,
176 const std::string& name
177 )
178 {
179 std::cout << " Value " << i << " Slot=" << slot
180 << " Name: " << name << "\n";
181 }
182
183 virtual void handleSymbolTableEnd()
184 {
185 std::cout << " } END BLOCK: SymbolTable\n";
186 }
187
188 virtual void handleFunctionBegin(
189 const Type* FType,
190 GlobalValue::LinkageTypes linkage
191 )
192 {
Reid Spencer0545b3e2004-06-09 06:16:19 +0000193 std::cout << "BLOCK: Function {\n";
194 std::cout << " Linkage: " << linkage << "\n";
195 std::cout << " Type: " << FType->getDescription() << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000196 }
197
198 virtual void handleFunctionEnd(
199 const Type* FType
200 )
201 {
Reid Spencer0545b3e2004-06-09 06:16:19 +0000202 std::cout << "} END BLOCK: Function\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000203 }
204
205 virtual void handleBasicBlockBegin(
206 unsigned blocknum
207 )
208 {
Reid Spencer0545b3e2004-06-09 06:16:19 +0000209 std::cout << " BLOCK: BasicBlock #" << blocknum << "{\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000210 }
211
212 virtual bool handleInstruction(
213 unsigned Opcode,
214 const Type* iType,
Reid Spencer00c28a72004-06-10 08:09:13 +0000215 std::vector<unsigned>& Operands,
216 unsigned Size
Reid Spencerdac69c82004-06-07 17:53:43 +0000217 )
218 {
Reid Spencer0545b3e2004-06-09 06:16:19 +0000219 std::cout << " INST: OpCode="
Reid Spencerdac69c82004-06-07 17:53:43 +0000220 << Instruction::getOpcodeName(Opcode) << " Type="
221 << iType->getDescription() << "\n";
222 for ( unsigned i = 0; i < Operands.size(); ++i )
Reid Spencer0545b3e2004-06-09 06:16:19 +0000223 std::cout << " Op#" << i << " Slot=" << Operands[i] << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000224
225 return Instruction::isTerminator(Opcode);
226 }
227
228 virtual void handleBasicBlockEnd(unsigned blocknum)
229 {
Reid Spencer0545b3e2004-06-09 06:16:19 +0000230 std::cout << " } END BLOCK: BasicBlock #" << blocknum << "{\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000231 }
232
233 virtual void handleGlobalConstantsBegin()
234 {
235 std::cout << " BLOCK: GlobalConstants {\n";
236 }
237
238 virtual void handleConstantExpression(
239 unsigned Opcode,
240 const Type* Typ,
241 std::vector<std::pair<const Type*,unsigned> > ArgVec
242 )
243 {
244 std::cout << " EXPR: " << Instruction::getOpcodeName(Opcode)
245 << " Type=" << Typ->getDescription() << "\n";
246 for ( unsigned i = 0; i < ArgVec.size(); ++i )
247 std::cout << " Arg#" << i << " Type="
248 << ArgVec[i].first->getDescription() << " Slot="
249 << ArgVec[i].second << "\n";
250 }
251
252 virtual void handleConstantValue( Constant * c )
253 {
254 std::cout << " VALUE: ";
255 c->print(std::cout);
256 std::cout << "\n";
257 }
258
259 virtual void handleConstantArray(
260 const ArrayType* AT,
261 std::vector<unsigned>& Elements )
262 {
263 std::cout << " ARRAY: " << AT->getDescription() << "\n";
264 for ( unsigned i = 0; i < Elements.size(); ++i )
265 std::cout << " #" << i << " Slot=" << Elements[i] << "\n";
266 }
267
268 virtual void handleConstantStruct(
269 const StructType* ST,
270 std::vector<unsigned>& Elements)
271 {
272 std::cout << " STRUC: " << ST->getDescription() << "\n";
273 for ( unsigned i = 0; i < Elements.size(); ++i )
274 std::cout << " #" << i << " Slot=" << Elements[i] << "\n";
275 }
276
277 virtual void handleConstantPointer(
278 const PointerType* PT, unsigned Slot)
279 {
280 std::cout << " POINT: " << PT->getDescription()
281 << " Slot=" << Slot << "\n";
282 }
283
284 virtual void handleConstantString( const ConstantArray* CA )
285 {
286 std::cout << " STRNG: ";
287 CA->print(std::cout);
288 std::cout << "\n";
289 }
290
291 virtual void handleGlobalConstantsEnd()
292 {
293 std::cout << " } END BLOCK: GlobalConstants\n";
294 }
295};
296
297}
298
299void BytecodeAnalyzer::DumpBytecode(
300 const unsigned char *Buf,
301 unsigned Length,
302 BytecodeAnalysis& bca,
303 const std::string &ModuleID
304 )
305{
306 BytecodeDumper TheHandler;
307 AbstractBytecodeParser TheParser(&TheHandler);
308 TheParser.ParseBytecode( Buf, Length, ModuleID );
Reid Spencer0545b3e2004-06-09 06:16:19 +0000309 if ( bca.detailedResults )
310 TheParser.ParseAllFunctionBodies();
Reid Spencerdac69c82004-06-07 17:53:43 +0000311}
312
313// vim: sw=2