blob: 133c1fbaa6f7afee3f2ca5bc62f139c9d49d3c51 [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 {
33 return false;
34 }
35
36 void handleStart()
37 {
Reid Spencer649ee572004-06-09 06:16:43 +000038 bca.ModuleId.clear();
Reid Spencer00c28a72004-06-10 08:09:13 +000039 bca.numBlocks = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000040 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;
Reid Spencer00c28a72004-06-10 08:09:13 +000052 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;
Reid Spencer649ee572004-06-09 06:16:43 +000060 bca.FunctionInfo.clear();
61 bca.BytecodeDump.clear();
Reid Spencer00c28a72004-06-10 08:09:13 +000062 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;
Reid Spencerdac69c82004-06-07 17:53:43 +000071 }
72
73 void handleFinish()
74 {
Reid Spencer00c28a72004-06-10 08:09:13 +000075 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);
Reid Spencerdac69c82004-06-07 17:53:43 +000084 }
85
86 void handleModuleBegin(const std::string& id)
87 {
Reid Spencer649ee572004-06-09 06:16:43 +000088 bca.ModuleId = id;
Reid Spencerdac69c82004-06-07 17:53:43 +000089 }
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
Reid Spencer00c28a72004-06-10 08:09:13 +0000103 void handleModuleGlobalsBegin(unsigned size)
Reid Spencerdac69c82004-06-07 17:53:43 +0000104 {
Reid Spencer00c28a72004-06-10 08:09:13 +0000105 // bca.globalBytesize += size;
Reid Spencerdac69c82004-06-07 17:53:43 +0000106 }
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 {
Reid Spencer649ee572004-06-09 06:16:43 +0000114 bca.numGlobalVars++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000115 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000116 }
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 {
Reid Spencer649ee572004-06-09 06:16:43 +0000125 bca.numGlobalVars++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000126 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000127 }
128
129 virtual void handleType( const Type* Ty )
130 {
Reid Spencer649ee572004-06-09 06:16:43 +0000131 bca.numTypes++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000132 }
133
134 void handleFunctionDeclaration(
135 const Type* FuncType ///< The type of the function
136 )
137 {
Reid Spencer649ee572004-06-09 06:16:43 +0000138 bca.numFunctions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000139 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000140 }
141
142 void handleModuleGlobalsEnd()
143 {
144 }
145
146 void handleCompactionTableBegin()
147 {
148 }
149
150 void handleCompactionTablePlane(
151 unsigned Ty,
152 unsigned NumEntries
153 )
154 {
Reid Spencer649ee572004-06-09 06:16:43 +0000155 bca.numCmpctnTables++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000156 }
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 {
Reid Spencer649ee572004-06-09 06:16:43 +0000180 bca.numSymTab++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000181 }
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 {
Reid Spencer649ee572004-06-09 06:16:43 +0000228 bca.numBasicBlocks++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000229 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000230 }
231
232 bool handleInstruction(
233 unsigned Opcode,
234 const Type* iType,
Reid Spencer00c28a72004-06-10 08:09:13 +0000235 std::vector<unsigned>& Operands,
236 unsigned Size
Reid Spencerdac69c82004-06-07 17:53:43 +0000237 )
238 {
Reid Spencer649ee572004-06-09 06:16:43 +0000239 bca.numInstructions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000240 bca.numValues++;
241 bca.numOperands += Operands.size();
Reid Spencer649ee572004-06-09 06:16:43 +0000242 return Instruction::isTerminator(Opcode);
Reid Spencerdac69c82004-06-07 17:53:43 +0000243 }
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 {
Reid Spencer649ee572004-06-09 06:16:43 +0000259 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000260 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000261 }
262
263 void handleConstantValue( Constant * c )
264 {
Reid Spencer649ee572004-06-09 06:16:43 +0000265 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000266 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000267 }
268
269 void handleConstantArray(
Reid Spencer00c28a72004-06-10 08:09:13 +0000270 const ArrayType* AT,
271 std::vector<unsigned>& Elements )
Reid Spencerdac69c82004-06-07 17:53:43 +0000272 {
Reid Spencer649ee572004-06-09 06:16:43 +0000273 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000274 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000275 }
276
277 void handleConstantStruct(
Reid Spencer00c28a72004-06-10 08:09:13 +0000278 const StructType* ST,
279 std::vector<unsigned>& ElementSlots)
Reid Spencerdac69c82004-06-07 17:53:43 +0000280 {
Reid Spencer649ee572004-06-09 06:16:43 +0000281 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000282 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000283 }
284
285 void handleConstantPointer(
Reid Spencer00c28a72004-06-10 08:09:13 +0000286 const PointerType* PT, unsigned Slot)
Reid Spencerdac69c82004-06-07 17:53:43 +0000287 {
Reid Spencer649ee572004-06-09 06:16:43 +0000288 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000289 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000290 }
291
292 void handleConstantString( const ConstantArray* CA )
293 {
Reid Spencer649ee572004-06-09 06:16:43 +0000294 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000295 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000296 }
297
298
Reid Spencer00c28a72004-06-10 08:09:13 +0000299 void handleGlobalConstantsEnd() { }
300
301 void handleAlignment(unsigned numBytes) {
302 bca.numAlignment += numBytes;
Reid Spencerdac69c82004-06-07 17:53:43 +0000303 }
304
Reid Spencer00c28a72004-06-10 08:09:13 +0000305 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 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000321};
322
323}
324
325void llvm::BytecodeAnalyzer::AnalyzeBytecode(
326 const unsigned char *Buf,
327 unsigned Length,
328 BytecodeAnalysis& bca,
329 const std::string &ModuleID
330)
331{
Reid Spencer649ee572004-06-09 06:16:43 +0000332 bca.byteSize = Length;
333 AnalyzerHandler TheHandler(bca);
Reid Spencer00c28a72004-06-10 08:09:13 +0000334 AbstractBytecodeParser TheParser(&TheHandler, true, true, true);
Reid Spencerdac69c82004-06-07 17:53:43 +0000335 TheParser.ParseBytecode( Buf, Length, ModuleID );
Reid Spencer00c28a72004-06-10 08:09:13 +0000336 TheParser.ParseAllFunctionBodies();
Reid Spencerdac69c82004-06-07 17:53:43 +0000337}
338
339// vim: sw=2