133c1fbaa6f7afee3f2ca5bc62f139c9d49d3c51
[oota-llvm.git] / lib / Bytecode / Analyzer / Analyzer.cpp
1 //===-- BytecodeHandler.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 BytecodeHandler class that gets called by the
11 //  AbstractBytecodeParser when parsing events occur.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "AnalyzerInternals.h"
16 #include <iostream>
17
18 using namespace llvm;
19
20
21 namespace {
22
23 class AnalyzerHandler : public BytecodeHandler {
24   BytecodeAnalysis& bca;
25 public:
26   AnalyzerHandler(BytecodeAnalysis& TheBca) 
27     : bca(TheBca)
28   {
29   }  
30
31   bool handleError(const std::string& str )
32   {
33     return false;
34   }
35
36   void handleStart()
37   {
38     bca.ModuleId.clear();
39     bca.numBlocks = 0;
40     bca.numTypes = 0;
41     bca.numValues = 0;
42     bca.numFunctions = 0;
43     bca.numConstants = 0;
44     bca.numGlobalVars = 0;
45     bca.numInstructions = 0;
46     bca.numBasicBlocks = 0;
47     bca.numOperands = 0;
48     bca.numCmpctnTables = 0;
49     bca.numSymTab = 0;
50     bca.maxTypeSlot = 0;
51     bca.maxValueSlot = 0;
52     bca.numAlignment = 0;
53     bca.fileDensity = 0.0;
54     bca.globalsDensity = 0.0;
55     bca.functionDensity = 0.0;
56     bca.vbrCount32 = 0;
57     bca.vbrCount64 = 0;
58     bca.vbrCompBytes = 0;
59     bca.vbrExpdBytes = 0;
60     bca.FunctionInfo.clear();
61     bca.BytecodeDump.clear();
62     bca.BlockSizes[BytecodeFormat::Module] = 0;
63     bca.BlockSizes[BytecodeFormat::Function] = 0;
64     bca.BlockSizes[BytecodeFormat::ConstantPool] = 0;
65     bca.BlockSizes[BytecodeFormat::SymbolTable] = 0;
66     bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo] = 0;
67     bca.BlockSizes[BytecodeFormat::GlobalTypePlane] = 0;
68     bca.BlockSizes[BytecodeFormat::BasicBlock] = 0;
69     bca.BlockSizes[BytecodeFormat::InstructionList] = 0;
70     bca.BlockSizes[BytecodeFormat::CompactionTable] = 0;
71   }
72
73   void handleFinish()
74   {
75     bca.fileDensity = double(bca.byteSize) / double( bca.numTypes + bca.numValues );
76     double globalSize = 0.0;
77     globalSize += double(bca.BlockSizes[BytecodeFormat::ConstantPool]);
78     globalSize += double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo]);
79     globalSize += double(bca.BlockSizes[BytecodeFormat::GlobalTypePlane]);
80     bca.globalsDensity = globalSize / double( bca.numTypes + bca.numConstants + 
81       bca.numGlobalVars );
82     bca.functionDensity = double(bca.BlockSizes[BytecodeFormat::Function]) / 
83       double(bca.numFunctions);
84   }
85
86   void handleModuleBegin(const std::string& id)
87   {
88     bca.ModuleId = id;
89   }
90
91   void handleModuleEnd(const std::string& id)
92   {
93   }
94
95   void handleVersionInfo(
96     unsigned char RevisionNum,        ///< Byte code revision number
97     Module::Endianness Endianness,    ///< Endianness indicator
98     Module::PointerSize PointerSize   ///< PointerSize indicator
99   )
100   {
101   }
102
103   void handleModuleGlobalsBegin(unsigned size)
104   {
105     // bca.globalBytesize += size;
106   }
107
108   void handleGlobalVariable( 
109     const Type* ElemType,     ///< The type of the global variable
110     bool isConstant,          ///< Whether the GV is constant or not
111     GlobalValue::LinkageTypes ///< The linkage type of the GV
112   )
113   {
114     bca.numGlobalVars++;
115     bca.numValues++;
116   }
117
118   void handleInitializedGV( 
119     const Type* ElemType,     ///< The type of the global variable
120     bool isConstant,          ///< Whether the GV is constant or not
121     GlobalValue::LinkageTypes,///< The linkage type of the GV
122     unsigned initSlot         ///< Slot number of GV's initializer
123   )
124   {
125     bca.numGlobalVars++;
126     bca.numValues++;
127   }
128
129   virtual void handleType( const Type* Ty ) 
130   {
131     bca.numTypes++;
132   }
133
134   void handleFunctionDeclaration( 
135     const Type* FuncType      ///< The type of the function
136   )
137   {
138     bca.numFunctions++;
139     bca.numValues++;
140   }
141
142   void handleModuleGlobalsEnd()
143   {
144   }
145
146   void handleCompactionTableBegin()
147   {
148   }
149
150   void handleCompactionTablePlane( 
151     unsigned Ty, 
152     unsigned NumEntries
153   )
154   {
155     bca.numCmpctnTables++;
156   }
157
158   void handleCompactionTableType( 
159     unsigned i, 
160     unsigned TypSlot, 
161     const Type* 
162   )
163   {
164   }
165
166   void handleCompactionTableValue( 
167     unsigned i, 
168     unsigned ValSlot, 
169     const Type* 
170   )
171   {
172   }
173
174   void handleCompactionTableEnd()
175   {
176   }
177
178   void handleSymbolTableBegin()
179   {
180     bca.numSymTab++;
181   }
182
183   void handleSymbolTablePlane( 
184     unsigned Ty, 
185     unsigned NumEntries, 
186     const Type* Typ
187   )
188   {
189   }
190
191   void handleSymbolTableType( 
192     unsigned i, 
193     unsigned slot, 
194     const std::string& name 
195   )
196   {
197   }
198
199   void handleSymbolTableValue( 
200     unsigned i, 
201     unsigned slot, 
202     const std::string& name 
203   )
204   {
205   }
206
207   void handleSymbolTableEnd()
208   {
209   }
210
211   void handleFunctionBegin(
212     const Type* FType, 
213     GlobalValue::LinkageTypes linkage 
214   )
215   {
216   }
217
218   void handleFunctionEnd(
219     const Type* FType
220   )
221   {
222   }
223
224   void handleBasicBlockBegin(
225     unsigned blocknum
226   )
227   {
228     bca.numBasicBlocks++;
229     bca.numValues++;
230   }
231
232   bool handleInstruction(
233     unsigned Opcode, 
234     const Type* iType, 
235     std::vector<unsigned>& Operands,
236     unsigned Size
237   )
238   {
239     bca.numInstructions++;
240     bca.numValues++;
241     bca.numOperands += Operands.size();
242     return Instruction::isTerminator(Opcode); 
243   }
244
245   void handleBasicBlockEnd(unsigned blocknum)
246   {
247   }
248
249   void handleGlobalConstantsBegin()
250   {
251   }
252
253   void handleConstantExpression( 
254       unsigned Opcode, 
255       const Type* Typ, 
256       std::vector<std::pair<const Type*,unsigned> > ArgVec 
257     )
258   {
259     bca.numConstants++;
260     bca.numValues++;
261   }
262
263   void handleConstantValue( Constant * c )
264   {
265     bca.numConstants++;
266     bca.numValues++;
267   }
268
269   void handleConstantArray( 
270           const ArrayType* AT, 
271           std::vector<unsigned>& Elements )
272   {
273     bca.numConstants++;
274     bca.numValues++;
275   }
276
277   void handleConstantStruct(
278         const StructType* ST,
279         std::vector<unsigned>& ElementSlots)
280   {
281     bca.numConstants++;
282     bca.numValues++;
283   }
284
285   void handleConstantPointer(
286         const PointerType* PT, unsigned Slot)
287   {
288     bca.numConstants++;
289     bca.numValues++;
290   }
291
292   void handleConstantString( const ConstantArray* CA ) 
293   {
294     bca.numConstants++;
295     bca.numValues++;
296   }
297
298
299   void handleGlobalConstantsEnd() { }
300
301   void handleAlignment(unsigned numBytes) {
302     bca.numAlignment += numBytes;
303   }
304
305   void handleBlock(
306     unsigned BType, const unsigned char* StartPtr, unsigned Size) {
307     bca.numBlocks++;
308     bca.BlockSizes[llvm::BytecodeFormat::FileBlockIDs(BType)] += Size;
309   }
310
311   virtual void handleVBR32(unsigned Size ) {
312     bca.vbrCount32++;
313     bca.vbrCompBytes += Size;
314     bca.vbrExpdBytes += sizeof(uint32_t);
315   }
316   virtual void handleVBR64(unsigned Size ) {
317     bca.vbrCount64++;
318     bca.vbrCompBytes += Size;
319     bca.vbrExpdBytes += sizeof(uint64_t);
320   }
321 };
322
323 }
324
325 void llvm::BytecodeAnalyzer::AnalyzeBytecode(
326     const unsigned char *Buf, 
327     unsigned Length,
328     BytecodeAnalysis& bca,
329     const std::string &ModuleID
330 )
331 {
332   bca.byteSize = Length;
333   AnalyzerHandler TheHandler(bca);
334   AbstractBytecodeParser TheParser(&TheHandler, true, true, true);
335   TheParser.ParseBytecode( Buf, Length, ModuleID );
336   TheParser.ParseAllFunctionBodies();
337 }
338
339 // vim: sw=2