summaryrefslogtreecommitdiff
path: root/lib/Bytecode/Analyzer/Dumper.cpp
blob: 6ff4ea0c79619c4d8d3f8f874436f222b2d6652d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//===-- BytecodeDumper.cpp - Parsing Handler --------------------*- C++ -*-===//
// 
//                     The LLVM Compiler Infrastructure
//
// This file was developed by Reid Spencer and is distributed under the 
// University of Illinois Open Source License. See LICENSE.TXT for details.
// 
//===----------------------------------------------------------------------===//
//
//  This header file defines the BytecodeDumper class that gets called by the
//  AbstractBytecodeParser when parsing events occur. It merely dumps the
//  information presented to it from the parser.
//
//===----------------------------------------------------------------------===//

#include "AnalyzerInternals.h"
#include "llvm/Constant.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Instruction.h"
#include "llvm/Type.h"

using namespace llvm;

namespace {

class BytecodeDumper : public llvm::BytecodeHandler {
public:

  virtual bool handleError(const std::string& str )
  {
    std::cout << "ERROR: " << str << "\n";
    return true;
  }

  virtual void handleStart()
  {
    std::cout << "Bytecode {\n";
  }

  virtual void handleFinish()
  {
    std::cout << "} End Bytecode\n";
  }

  virtual void handleModuleBegin(const std::string& id)
  {
    std::cout << "  Module " << id << " {\n";
  }

  virtual void handleModuleEnd(const std::string& id)
  {
    std::cout << "  } End Module " << id << "\n";
  }

  virtual void handleVersionInfo(
    unsigned char RevisionNum,        ///< Byte code revision number
    Module::Endianness Endianness,    ///< Endianness indicator
    Module::PointerSize PointerSize   ///< PointerSize indicator
  )
  {
    std::cout << "    RevisionNum: " << int(RevisionNum) 
	      << " Endianness: " << Endianness
	      << " PointerSize: " << PointerSize << "\n";
  }

  virtual void handleModuleGlobalsBegin()
  {
    std::cout << "    BLOCK: ModuleGlobalInfo {\n";
  }

  virtual void handleGlobalVariable( 
    const Type* ElemType,     ///< The type of the global variable
    bool isConstant,          ///< Whether the GV is constant or not
    GlobalValue::LinkageTypes Linkage ///< The linkage type of the GV
  )
  {
    std::cout << "      GV: Uninitialized, " 
	     << ( isConstant? "Constant, " : "Variable, ")
	     << " Linkage=" << Linkage << " Type=" 
	     << ElemType->getDescription() << "\n"; 
  }

  virtual void handleInitializedGV( 
    const Type* ElemType,     ///< The type of the global variable
    bool isConstant,          ///< Whether the GV is constant or not
    GlobalValue::LinkageTypes Linkage,///< The linkage type of the GV
    unsigned initSlot         ///< Slot number of GV's initializer
  )
  {
    std::cout << "      GV: Initialized, " 
	     << ( isConstant? "Constant, " : "Variable, ")
	     << " Linkage=" << Linkage << " Type=" 
	     << ElemType->getDescription()
	     << " InitializerSlot=" << initSlot << "\n"; 
  }

  virtual void handleType( const Type* Ty ) 
  {
    std::cout << "      Type: " << Ty->getDescription() << "\n";
  }

  virtual void handleFunctionDeclaration( const Type* FuncType )
  {
    std::cout << "      Function: " << FuncType->getDescription() << "\n";
  }

  virtual void handleModuleGlobalsEnd()
  {
    std::cout << "    } END BLOCK: ModuleGlobalInfo\n";
  }

  void handleCompactionTableBegin()
  {
    std::cout << "    BLOCK: CompactionTable {\n";
  }

  virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries )
  {
    std::cout << "      Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
  }

  virtual void handleCompactionTableType( 
    unsigned i, 
    unsigned TypSlot, 
    const Type* Ty
  )
  {
    std::cout << "        Type: " << i << " Slot:" << TypSlot 
	      << " is " << Ty->getDescription() << "\n"; 
  }

  virtual void handleCompactionTableValue( 
    unsigned i, 
    unsigned ValSlot, 
    const Type* Ty 
  )
  {
    std::cout << "        Value: " << i << " Slot:" << ValSlot 
	      << " is " << Ty->getDescription() << "\n"; 
  }

  virtual void handleCompactionTableEnd()
  {
    std::cout << "    } END BLOCK: CompactionTable\n";
  }

  virtual void handleSymbolTableBegin()
  {
    std::cout << "    BLOCK: SymbolTable {\n";
  }

  virtual void handleSymbolTablePlane( 
    unsigned Ty, 
    unsigned NumEntries, 
    const Type* Typ
  )
  {
    std::cout << "      Plane: Ty=" << Ty << " Size=" << NumEntries
	      << " Type: " << Typ->getDescription() << "\n"; 
  }

  virtual void handleSymbolTableType( 
    unsigned i, 
    unsigned slot, 
    const std::string& name 
  )
  {
    std::cout << "        Type " << i << " Slot=" << slot
	      << " Name: " << name << "\n"; 
  }

  virtual void handleSymbolTableValue( 
    unsigned i, 
    unsigned slot, 
    const std::string& name 
  )
  {
    std::cout << "        Value " << i << " Slot=" << slot
	      << " Name: " << name << "\n";
  }

  virtual void handleSymbolTableEnd()
  {
    std::cout << "    } END BLOCK: SymbolTable\n";
  }

  virtual void handleFunctionBegin(
    const Type* FType, 
    GlobalValue::LinkageTypes linkage 
  )
  {
    std::cout << "    BLOCK: Function {\n";
    std::cout << "      Linkage: " << linkage << "\n";
    std::cout << "      Type: " << FType->getDescription() << "\n";
  }

  virtual void handleFunctionEnd(
    const Type* FType
  )
  {
    std::cout << "    } END BLOCK: Function\n";
  }

  virtual void handleBasicBlockBegin(
    unsigned blocknum
  )
  {
    std::cout << "      BLOCK: BasicBlock #" << blocknum << "{\n";
  }

  virtual bool handleInstruction(
    unsigned Opcode, 
    const Type* iType, 
    std::vector<unsigned>& Operands
  )
  {
    std::cout << "        INST: OpCode=" 
	      << Instruction::getOpcodeName(Opcode) << " Type=" 
	      << iType->getDescription() << "\n";
    for ( unsigned i = 0; i < Operands.size(); ++i ) 
      std::cout << "          Op#" << i << " Slot=" << Operands[i] << "\n";
    
    return Instruction::isTerminator(Opcode); 
  }

  virtual void handleBasicBlockEnd(unsigned blocknum)
  {
    std::cout << "      } END BLOCK: BasicBlock #" << blocknum << "{\n";
  }

  virtual void handleGlobalConstantsBegin()
  {
    std::cout << "    BLOCK: GlobalConstants {\n";
  }

  virtual void handleConstantExpression( 
      unsigned Opcode, 
      const Type* Typ, 
      std::vector<std::pair<const Type*,unsigned> > ArgVec 
    )
  {
    std::cout << "      EXPR: " << Instruction::getOpcodeName(Opcode)
	      << " Type=" << Typ->getDescription() << "\n";
    for ( unsigned i = 0; i < ArgVec.size(); ++i ) 
      std::cout << "        Arg#" << i << " Type=" 
	<< ArgVec[i].first->getDescription() << " Slot=" 
	<< ArgVec[i].second << "\n";
  }

  virtual void handleConstantValue( Constant * c )
  {
    std::cout << "      VALUE: ";
    c->print(std::cout);
    std::cout << "\n";
  }

  virtual void handleConstantArray( 
	  const ArrayType* AT, 
	  std::vector<unsigned>& Elements )
  {
    std::cout << "      ARRAY: " << AT->getDescription() << "\n";
    for ( unsigned i = 0; i < Elements.size(); ++i ) 
      std::cout << "        #" << i << " Slot=" << Elements[i] << "\n";
  }

  virtual void handleConstantStruct(
	const StructType* ST,
	std::vector<unsigned>& Elements)
  {
    std::cout << "      STRUC: " << ST->getDescription() << "\n";
    for ( unsigned i = 0; i < Elements.size(); ++i ) 
      std::cout << "        #" << i << " Slot=" << Elements[i] << "\n";
  }

  virtual void handleConstantPointer(
	const PointerType* PT, unsigned Slot)
  {
    std::cout << "      POINT: " << PT->getDescription() 
	      << " Slot=" << Slot << "\n";
  }

  virtual void handleConstantString( const ConstantArray* CA ) 
  {
    std::cout << "      STRNG: ";
    CA->print(std::cout); 
    std::cout << "\n";
  }

  virtual void handleGlobalConstantsEnd()
  {
    std::cout << "    } END BLOCK: GlobalConstants\n";
  }
};

}

void BytecodeAnalyzer::DumpBytecode(
    const unsigned char *Buf, 
    unsigned Length,
    BytecodeAnalysis& bca,
    const std::string &ModuleID
  )
{
  BytecodeDumper TheHandler;
  AbstractBytecodeParser TheParser(&TheHandler);
  TheParser.ParseBytecode( Buf, Length, ModuleID );
  TheParser.ParseAllFunctionBodies();
}

// vim: sw=2