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 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 33 | std::cerr << "Analysis Error: " << str; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 34 | return false; |
| 35 | } |
| 36 | |
| 37 | void handleStart() |
| 38 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 39 | 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 Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void handleFinish() |
| 58 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 59 | bca.density = bca.numTypes + bca.numFunctions + bca.numConstants + |
| 60 | bca.numGlobalVars + bca.numInstructions; |
| 61 | bca.density /= bca.byteSize; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void handleModuleBegin(const std::string& id) |
| 65 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 66 | bca.ModuleId = id; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 67 | } |
| 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 Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 91 | bca.numGlobalVars++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 92 | } |
| 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 Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 101 | bca.numGlobalVars++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | virtual void handleType( const Type* Ty ) |
| 105 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 106 | bca.numTypes++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void handleFunctionDeclaration( |
| 110 | const Type* FuncType ///< The type of the function |
| 111 | ) |
| 112 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 113 | bca.numFunctions++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 114 | } |
| 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 Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 129 | bca.numCmpctnTables++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 130 | } |
| 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 Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 154 | bca.numSymTab++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 155 | } |
| 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 Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 202 | bca.numBasicBlocks++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | bool handleInstruction( |
| 206 | unsigned Opcode, |
| 207 | const Type* iType, |
| 208 | std::vector<unsigned>& Operands |
| 209 | ) |
| 210 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 211 | bca.numInstructions++; |
| 212 | return Instruction::isTerminator(Opcode); |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 213 | } |
| 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 Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 229 | bca.numConstants++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void handleConstantValue( Constant * c ) |
| 233 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 234 | bca.numConstants++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void handleConstantArray( |
| 238 | const ArrayType* AT, |
| 239 | std::vector<unsigned>& Elements ) |
| 240 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 241 | bca.numConstants++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | void handleConstantStruct( |
| 245 | const StructType* ST, |
| 246 | std::vector<unsigned>& ElementSlots) |
| 247 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 248 | bca.numConstants++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void handleConstantPointer( |
| 252 | const PointerType* PT, unsigned Slot) |
| 253 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 254 | bca.numConstants++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void handleConstantString( const ConstantArray* CA ) |
| 258 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 259 | bca.numConstants++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
| 263 | void handleGlobalConstantsEnd() |
| 264 | { |
| 265 | } |
| 266 | |
| 267 | }; |
| 268 | |
| 269 | } |
| 270 | |
| 271 | void llvm::BytecodeAnalyzer::AnalyzeBytecode( |
| 272 | const unsigned char *Buf, |
| 273 | unsigned Length, |
| 274 | BytecodeAnalysis& bca, |
| 275 | const std::string &ModuleID |
| 276 | ) |
| 277 | { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 278 | bca.byteSize = Length; |
| 279 | AnalyzerHandler TheHandler(bca); |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 280 | AbstractBytecodeParser TheParser(&TheHandler); |
| 281 | TheParser.ParseBytecode( Buf, Length, ModuleID ); |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame^] | 282 | if ( bca.detailedResults ) |
| 283 | TheParser.ParseAllFunctionBodies(); |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // vim: sw=2 |