blob: eb710711d989a3b1a8541df91d9f92cab04a1236 [file] [log] [blame]
Reid Spencerdac69c82004-06-07 17:53:43 +00001//===-- 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"
Reid Spencer649ee572004-06-09 06:16:43 +000016#include <iostream>
Reid Spencerdac69c82004-06-07 17:53:43 +000017
18using namespace llvm;
19
20
21namespace {
22
23class AnalyzerHandler : public BytecodeHandler {
Reid Spencer649ee572004-06-09 06:16:43 +000024 BytecodeAnalysis& bca;
Reid Spencerdac69c82004-06-07 17:53:43 +000025public:
Reid Spencer649ee572004-06-09 06:16:43 +000026 AnalyzerHandler(BytecodeAnalysis& TheBca)
27 : bca(TheBca)
28 {
29 }
30
Reid Spencerdac69c82004-06-07 17:53:43 +000031 bool handleError(const std::string& str )
32 {
Reid Spencer649ee572004-06-09 06:16:43 +000033 std::cerr << "Analysis Error: " << str;
Reid Spencerdac69c82004-06-07 17:53:43 +000034 return false;
35 }
36
37 void handleStart()
38 {
Reid Spencer649ee572004-06-09 06:16:43 +000039 bca.ModuleId.clear();
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.density = 0.0;
53 bca.FunctionInfo.clear();
54 bca.BytecodeDump.clear();
Reid Spencerdac69c82004-06-07 17:53:43 +000055 }
56
57 void handleFinish()
58 {
Reid Spencer649ee572004-06-09 06:16:43 +000059 bca.density = bca.numTypes + bca.numFunctions + bca.numConstants +
60 bca.numGlobalVars + bca.numInstructions;
61 bca.density /= bca.byteSize;
Reid Spencerdac69c82004-06-07 17:53:43 +000062 }
63
64 void handleModuleBegin(const std::string& id)
65 {
Reid Spencer649ee572004-06-09 06:16:43 +000066 bca.ModuleId = id;
Reid Spencerdac69c82004-06-07 17:53:43 +000067 }
68
69 void handleModuleEnd(const std::string& id)
70 {
71 }
72
73 void handleVersionInfo(
74 unsigned char RevisionNum, ///< Byte code revision number
75 Module::Endianness Endianness, ///< Endianness indicator
76 Module::PointerSize PointerSize ///< PointerSize indicator
77 )
78 {
79 }
80
81 void handleModuleGlobalsBegin()
82 {
83 }
84
85 void handleGlobalVariable(
86 const Type* ElemType, ///< The type of the global variable
87 bool isConstant, ///< Whether the GV is constant or not
88 GlobalValue::LinkageTypes ///< The linkage type of the GV
89 )
90 {
Reid Spencer649ee572004-06-09 06:16:43 +000091 bca.numGlobalVars++;
Reid Spencerdac69c82004-06-07 17:53:43 +000092 }
93
94 void handleInitializedGV(
95 const Type* ElemType, ///< The type of the global variable
96 bool isConstant, ///< Whether the GV is constant or not
97 GlobalValue::LinkageTypes,///< The linkage type of the GV
98 unsigned initSlot ///< Slot number of GV's initializer
99 )
100 {
Reid Spencer649ee572004-06-09 06:16:43 +0000101 bca.numGlobalVars++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000102 }
103
104 virtual void handleType( const Type* Ty )
105 {
Reid Spencer649ee572004-06-09 06:16:43 +0000106 bca.numTypes++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000107 }
108
109 void handleFunctionDeclaration(
110 const Type* FuncType ///< The type of the function
111 )
112 {
Reid Spencer649ee572004-06-09 06:16:43 +0000113 bca.numFunctions++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000114 }
115
116 void handleModuleGlobalsEnd()
117 {
118 }
119
120 void handleCompactionTableBegin()
121 {
122 }
123
124 void handleCompactionTablePlane(
125 unsigned Ty,
126 unsigned NumEntries
127 )
128 {
Reid Spencer649ee572004-06-09 06:16:43 +0000129 bca.numCmpctnTables++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000130 }
131
132 void handleCompactionTableType(
133 unsigned i,
134 unsigned TypSlot,
135 const Type*
136 )
137 {
138 }
139
140 void handleCompactionTableValue(
141 unsigned i,
142 unsigned ValSlot,
143 const Type*
144 )
145 {
146 }
147
148 void handleCompactionTableEnd()
149 {
150 }
151
152 void handleSymbolTableBegin()
153 {
Reid Spencer649ee572004-06-09 06:16:43 +0000154 bca.numSymTab++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000155 }
156
157 void handleSymbolTablePlane(
158 unsigned Ty,
159 unsigned NumEntries,
160 const Type* Typ
161 )
162 {
163 }
164
165 void handleSymbolTableType(
166 unsigned i,
167 unsigned slot,
168 const std::string& name
169 )
170 {
171 }
172
173 void handleSymbolTableValue(
174 unsigned i,
175 unsigned slot,
176 const std::string& name
177 )
178 {
179 }
180
181 void handleSymbolTableEnd()
182 {
183 }
184
185 void handleFunctionBegin(
186 const Type* FType,
187 GlobalValue::LinkageTypes linkage
188 )
189 {
190 }
191
192 void handleFunctionEnd(
193 const Type* FType
194 )
195 {
196 }
197
198 void handleBasicBlockBegin(
199 unsigned blocknum
200 )
201 {
Reid Spencer649ee572004-06-09 06:16:43 +0000202 bca.numBasicBlocks++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000203 }
204
205 bool handleInstruction(
206 unsigned Opcode,
207 const Type* iType,
208 std::vector<unsigned>& Operands
209 )
210 {
Reid Spencer649ee572004-06-09 06:16:43 +0000211 bca.numInstructions++;
212 return Instruction::isTerminator(Opcode);
Reid Spencerdac69c82004-06-07 17:53:43 +0000213 }
214
215 void handleBasicBlockEnd(unsigned blocknum)
216 {
217 }
218
219 void handleGlobalConstantsBegin()
220 {
221 }
222
223 void handleConstantExpression(
224 unsigned Opcode,
225 const Type* Typ,
226 std::vector<std::pair<const Type*,unsigned> > ArgVec
227 )
228 {
Reid Spencer649ee572004-06-09 06:16:43 +0000229 bca.numConstants++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000230 }
231
232 void handleConstantValue( Constant * c )
233 {
Reid Spencer649ee572004-06-09 06:16:43 +0000234 bca.numConstants++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000235 }
236
237 void handleConstantArray(
238 const ArrayType* AT,
239 std::vector<unsigned>& Elements )
240 {
Reid Spencer649ee572004-06-09 06:16:43 +0000241 bca.numConstants++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000242 }
243
244 void handleConstantStruct(
245 const StructType* ST,
246 std::vector<unsigned>& ElementSlots)
247 {
Reid Spencer649ee572004-06-09 06:16:43 +0000248 bca.numConstants++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000249 }
250
251 void handleConstantPointer(
252 const PointerType* PT, unsigned Slot)
253 {
Reid Spencer649ee572004-06-09 06:16:43 +0000254 bca.numConstants++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000255 }
256
257 void handleConstantString( const ConstantArray* CA )
258 {
Reid Spencer649ee572004-06-09 06:16:43 +0000259 bca.numConstants++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000260 }
261
262
263 void handleGlobalConstantsEnd()
264 {
265 }
266
267};
268
269}
270
271void llvm::BytecodeAnalyzer::AnalyzeBytecode(
272 const unsigned char *Buf,
273 unsigned Length,
274 BytecodeAnalysis& bca,
275 const std::string &ModuleID
276)
277{
Reid Spencer649ee572004-06-09 06:16:43 +0000278 bca.byteSize = Length;
279 AnalyzerHandler TheHandler(bca);
Reid Spencerdac69c82004-06-07 17:53:43 +0000280 AbstractBytecodeParser TheParser(&TheHandler);
281 TheParser.ParseBytecode( Buf, Length, ModuleID );
Reid Spencer649ee572004-06-09 06:16:43 +0000282 if ( bca.detailedResults )
283 TheParser.ParseAllFunctionBodies();
Reid Spencerdac69c82004-06-07 17:53:43 +0000284}
285
286// vim: sw=2