Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 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" |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 16 | #include <iostream> |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | class AnalyzerHandler : public BytecodeHandler { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 24 | BytecodeAnalysis& bca; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 25 | public: |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 26 | AnalyzerHandler(BytecodeAnalysis& TheBca) |
| 27 | : bca(TheBca) |
| 28 | { |
| 29 | } |
| 30 | |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 31 | bool handleError(const std::string& str ) |
| 32 | { |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | void handleStart() |
| 37 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 38 | bca.ModuleId.clear(); |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 39 | bca.numBlocks = 0; |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 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; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 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; |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 60 | bca.FunctionInfo.clear(); |
| 61 | bca.BytecodeDump.clear(); |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 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; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void handleFinish() |
| 74 | { |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 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); |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void handleModuleBegin(const std::string& id) |
| 87 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 88 | bca.ModuleId = id; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 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 | |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 103 | void handleModuleGlobalsBegin(unsigned size) |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 104 | { |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 105 | // bca.globalBytesize += size; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 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 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 114 | bca.numGlobalVars++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 115 | bca.numValues++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 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 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 125 | bca.numGlobalVars++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 126 | bca.numValues++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | virtual void handleType( const Type* Ty ) |
| 130 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 131 | bca.numTypes++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void handleFunctionDeclaration( |
| 135 | const Type* FuncType ///< The type of the function |
| 136 | ) |
| 137 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 138 | bca.numFunctions++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 139 | bca.numValues++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 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 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 155 | bca.numCmpctnTables++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 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 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 180 | bca.numSymTab++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 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 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 228 | bca.numBasicBlocks++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 229 | bca.numValues++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | bool handleInstruction( |
| 233 | unsigned Opcode, |
| 234 | const Type* iType, |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 235 | std::vector<unsigned>& Operands, |
| 236 | unsigned Size |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 237 | ) |
| 238 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 239 | bca.numInstructions++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 240 | bca.numValues++; |
| 241 | bca.numOperands += Operands.size(); |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 242 | return Instruction::isTerminator(Opcode); |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 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 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 259 | bca.numConstants++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 260 | bca.numValues++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void handleConstantValue( Constant * c ) |
| 264 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 265 | bca.numConstants++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 266 | bca.numValues++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void handleConstantArray( |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 270 | const ArrayType* AT, |
| 271 | std::vector<unsigned>& Elements ) |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 272 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 273 | bca.numConstants++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 274 | bca.numValues++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void handleConstantStruct( |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 278 | const StructType* ST, |
| 279 | std::vector<unsigned>& ElementSlots) |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 280 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 281 | bca.numConstants++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 282 | bca.numValues++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void handleConstantPointer( |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 286 | const PointerType* PT, unsigned Slot) |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 287 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 288 | bca.numConstants++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 289 | bca.numValues++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void handleConstantString( const ConstantArray* CA ) |
| 293 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 294 | bca.numConstants++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 295 | bca.numValues++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 299 | void handleGlobalConstantsEnd() { } |
| 300 | |
| 301 | void handleAlignment(unsigned numBytes) { |
| 302 | bca.numAlignment += numBytes; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 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 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 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 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 332 | bca.byteSize = Length; |
| 333 | AnalyzerHandler TheHandler(bca); |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 334 | AbstractBytecodeParser TheParser(&TheHandler, true, true, true); |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 335 | TheParser.ParseBytecode( Buf, Length, ModuleID ); |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 336 | TheParser.ParseAllFunctionBodies(); |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | // vim: sw=2 |