1 //===-- BytecodeHandler.cpp - Parsing Handler -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This header file defines the BytecodeHandler class that gets called by the
11 // AbstractBytecodeParser when parsing events occur.
13 //===----------------------------------------------------------------------===//
15 #include "AnalyzerInternals.h"
23 class AnalyzerHandler : public BytecodeHandler {
24 BytecodeAnalysis& bca;
26 AnalyzerHandler(BytecodeAnalysis& TheBca)
31 bool handleError(const std::string& str )
44 bca.numGlobalVars = 0;
45 bca.numInstructions = 0;
46 bca.numBasicBlocks = 0;
48 bca.numCmpctnTables = 0;
53 bca.fileDensity = 0.0;
54 bca.globalsDensity = 0.0;
55 bca.functionDensity = 0.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;
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 +
82 bca.functionDensity = double(bca.BlockSizes[BytecodeFormat::Function]) /
83 double(bca.numFunctions);
86 void handleModuleBegin(const std::string& id)
91 void handleModuleEnd(const std::string& id)
95 void handleVersionInfo(
96 unsigned char RevisionNum, ///< Byte code revision number
97 Module::Endianness Endianness, ///< Endianness indicator
98 Module::PointerSize PointerSize ///< PointerSize indicator
103 void handleModuleGlobalsBegin(unsigned size)
105 // bca.globalBytesize += size;
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
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
129 virtual void handleType( const Type* Ty )
134 void handleFunctionDeclaration(
135 const Type* FuncType ///< The type of the function
142 void handleModuleGlobalsEnd()
146 void handleCompactionTableBegin()
150 void handleCompactionTablePlane(
155 bca.numCmpctnTables++;
158 void handleCompactionTableType(
166 void handleCompactionTableValue(
174 void handleCompactionTableEnd()
178 void handleSymbolTableBegin()
183 void handleSymbolTablePlane(
191 void handleSymbolTableType(
194 const std::string& name
199 void handleSymbolTableValue(
202 const std::string& name
207 void handleSymbolTableEnd()
211 void handleFunctionBegin(
213 GlobalValue::LinkageTypes linkage
218 void handleFunctionEnd(
224 void handleBasicBlockBegin(
228 bca.numBasicBlocks++;
232 bool handleInstruction(
235 std::vector<unsigned>& Operands,
239 bca.numInstructions++;
241 bca.numOperands += Operands.size();
242 return Instruction::isTerminator(Opcode);
245 void handleBasicBlockEnd(unsigned blocknum)
249 void handleGlobalConstantsBegin()
253 void handleConstantExpression(
256 std::vector<std::pair<const Type*,unsigned> > ArgVec
263 void handleConstantValue( Constant * c )
269 void handleConstantArray(
271 std::vector<unsigned>& Elements )
277 void handleConstantStruct(
278 const StructType* ST,
279 std::vector<unsigned>& ElementSlots)
285 void handleConstantPointer(
286 const PointerType* PT, unsigned Slot)
292 void handleConstantString( const ConstantArray* CA )
299 void handleGlobalConstantsEnd() { }
301 void handleAlignment(unsigned numBytes) {
302 bca.numAlignment += numBytes;
306 unsigned BType, const unsigned char* StartPtr, unsigned Size) {
308 bca.BlockSizes[llvm::BytecodeFormat::FileBlockIDs(BType)] += Size;
311 virtual void handleVBR32(unsigned Size ) {
313 bca.vbrCompBytes += Size;
314 bca.vbrExpdBytes += sizeof(uint32_t);
316 virtual void handleVBR64(unsigned Size ) {
318 bca.vbrCompBytes += Size;
319 bca.vbrExpdBytes += sizeof(uint64_t);
325 void llvm::BytecodeAnalyzer::AnalyzeBytecode(
326 const unsigned char *Buf,
328 BytecodeAnalysis& bca,
329 const std::string &ModuleID
332 bca.byteSize = Length;
333 AnalyzerHandler TheHandler(bca);
334 AbstractBytecodeParser TheParser(&TheHandler, true, true, true);
335 TheParser.ParseBytecode( Buf, Length, ModuleID );
336 TheParser.ParseAllFunctionBodies();