Merge Dumper.cpp and AnalyzerWrappers.cpp into this file. Also, adjust the
[oota-llvm.git] / lib / Bytecode / Reader / Dumper.cpp
1 //===-- 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
23 using namespace llvm;
24
25 namespace {
26
27 class BytecodeDumper : public llvm::BytecodeHandler {
28 public:
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     std::cout << "      Type: " << Ty->getDescription() << "\n";
100   }
101
102   virtual void handleFunctionDeclaration( 
103     Function* Func,
104     const FunctionType* FuncType) {
105     std::cout << "      Function: " << FuncType->getDescription() << "\n";
106   }
107
108   virtual void handleModuleGlobalsEnd() {
109     std::cout << "    } END BLOCK: ModuleGlobalInfo\n";
110   }
111
112   virtual void handleCompactionTableBegin() {
113     std::cout << "    BLOCK: CompactionTable {\n";
114   }
115
116   virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries ) {
117     std::cout << "      Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
118   }
119
120   virtual void handleCompactionTableType( 
121     unsigned i, 
122     unsigned TypSlot, 
123     const Type* Ty) {
124     std::cout << "        Type: " << i << " Slot:" << TypSlot 
125               << " is " << Ty->getDescription() << "\n"; 
126   }
127
128   virtual void handleCompactionTableValue( 
129     unsigned i, 
130     unsigned ValSlot, 
131     const Type* Ty ) {
132     std::cout << "        Value: " << i << " Slot:" << ValSlot 
133               << " is " << Ty->getDescription() << "\n"; 
134   }
135
136   virtual void handleCompactionTableEnd() {
137     std::cout << "    } END BLOCK: CompactionTable\n";
138   }
139
140   virtual void handleSymbolTableBegin() {
141     std::cout << "    BLOCK: SymbolTable {\n";
142   }
143
144   virtual void handleSymbolTablePlane( 
145     unsigned Ty, 
146     unsigned NumEntries, 
147     const Type* Typ) {
148     std::cout << "      Plane: Ty=" << Ty << " Size=" << NumEntries
149               << " Type: " << Typ->getDescription() << "\n"; 
150   }
151
152   virtual void handleSymbolTableType( 
153     unsigned i, 
154     unsigned slot, 
155     const std::string& name ) {
156     std::cout << "        Type " << i << " Slot=" << slot
157               << " Name: " << name << "\n"; 
158   }
159
160   virtual void handleSymbolTableValue( 
161     unsigned i, 
162     unsigned slot, 
163     const std::string& name ) {
164     std::cout << "        Value " << i << " Slot=" << slot
165               << " Name: " << name << "\n";
166   }
167
168   virtual void handleSymbolTableEnd() {
169     std::cout << "    } END BLOCK: SymbolTable\n";
170   }
171
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";
177   }
178
179   virtual void handleFunctionEnd( const Type* FType) {
180     std::cout << "} END BLOCK: Function\n";
181   }
182
183   virtual void handleBasicBlockBegin( unsigned blocknum) {
184     std::cout << "  BLOCK: BasicBlock #" << blocknum << "{\n";
185   }
186
187   virtual bool handleInstruction(
188     unsigned Opcode, 
189     const Type* iType, 
190     std::vector<unsigned>& Operands,
191     unsigned Size) {
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";
197     
198     return Instruction::isTerminator(Opcode); 
199   }
200
201   virtual void handleBasicBlockEnd(unsigned blocknum) {
202     std::cout << "  } END BLOCK: BasicBlock #" << blocknum << "{\n";
203   }
204
205   virtual void handleGlobalConstantsBegin() {
206     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 
213     ) {
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";
220   }
221
222   virtual void handleConstantValue( Constant * c ) {
223     std::cout << "      VALUE: ";
224     c->print(std::cout);
225     std::cout << "\n";
226   }
227
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";
233   }
234
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";
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
249   virtual void handleConstantString( const ConstantArray* CA ) {
250     std::cout << "      STRNG: ";
251     CA->print(std::cout); 
252     std::cout << "\n";
253   }
254
255   virtual void handleGlobalConstantsEnd() {
256     std::cout << "    } END BLOCK: GlobalConstants\n";
257   }
258 };
259
260 }
261
262 void BytecodeAnalyzer::DumpBytecode(
263     const unsigned char *Buf, 
264     unsigned Length,
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();
272 }
273
274 // vim: sw=2