Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 1 | //===-- BytecodeDumper.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 BytecodeDumper class that gets called by the |
| 11 | // AbstractBytecodeParser when parsing events occur. It merely dumps the |
| 12 | // information presented to it from the parser. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "AnalyzerInternals.h" |
| 17 | #include "llvm/Constant.h" |
| 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Instruction.h" |
| 21 | #include "llvm/Type.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | class BytecodeDumper : public llvm::BytecodeHandler { |
| 28 | public: |
| 29 | |
| 30 | virtual bool handleError(const std::string& str ) |
| 31 | { |
| 32 | std::cout << "ERROR: " << str << "\n"; |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | virtual void handleStart() |
| 37 | { |
| 38 | std::cout << "Bytecode {\n"; |
| 39 | } |
| 40 | |
| 41 | virtual void handleFinish() |
| 42 | { |
| 43 | std::cout << "} End Bytecode\n"; |
| 44 | } |
| 45 | |
| 46 | virtual void handleModuleBegin(const std::string& id) |
| 47 | { |
| 48 | std::cout << " Module " << id << " {\n"; |
| 49 | } |
| 50 | |
| 51 | virtual void handleModuleEnd(const std::string& id) |
| 52 | { |
| 53 | std::cout << " } End Module " << id << "\n"; |
| 54 | } |
| 55 | |
| 56 | virtual void handleVersionInfo( |
| 57 | unsigned char RevisionNum, ///< Byte code revision number |
| 58 | Module::Endianness Endianness, ///< Endianness indicator |
| 59 | Module::PointerSize PointerSize ///< PointerSize indicator |
| 60 | ) |
| 61 | { |
| 62 | std::cout << " RevisionNum: " << int(RevisionNum) |
| 63 | << " Endianness: " << Endianness |
| 64 | << " PointerSize: " << PointerSize << "\n"; |
| 65 | } |
| 66 | |
| 67 | virtual void handleModuleGlobalsBegin() |
| 68 | { |
| 69 | std::cout << " BLOCK: ModuleGlobalInfo {\n"; |
| 70 | } |
| 71 | |
| 72 | virtual void handleGlobalVariable( |
| 73 | const Type* ElemType, ///< The type of the global variable |
| 74 | bool isConstant, ///< Whether the GV is constant or not |
| 75 | GlobalValue::LinkageTypes Linkage ///< The linkage type of the GV |
| 76 | ) |
| 77 | { |
| 78 | std::cout << " GV: Uninitialized, " |
| 79 | << ( isConstant? "Constant, " : "Variable, ") |
| 80 | << " Linkage=" << Linkage << " Type=" |
| 81 | << ElemType->getDescription() << "\n"; |
| 82 | } |
| 83 | |
| 84 | virtual void handleInitializedGV( |
| 85 | const Type* ElemType, ///< The type of the global variable |
| 86 | bool isConstant, ///< Whether the GV is constant or not |
| 87 | GlobalValue::LinkageTypes Linkage,///< The linkage type of the GV |
| 88 | unsigned initSlot ///< Slot number of GV's initializer |
| 89 | ) |
| 90 | { |
| 91 | std::cout << " GV: Initialized, " |
| 92 | << ( isConstant? "Constant, " : "Variable, ") |
| 93 | << " Linkage=" << Linkage << " Type=" |
| 94 | << ElemType->getDescription() |
| 95 | << " InitializerSlot=" << initSlot << "\n"; |
| 96 | } |
| 97 | |
| 98 | virtual void handleType( const Type* Ty ) |
| 99 | { |
| 100 | std::cout << " Type: " << Ty->getDescription() << "\n"; |
| 101 | } |
| 102 | |
| 103 | virtual void handleFunctionDeclaration( const Type* FuncType ) |
| 104 | { |
| 105 | std::cout << " Function: " << FuncType->getDescription() << "\n"; |
| 106 | } |
| 107 | |
| 108 | virtual void handleModuleGlobalsEnd() |
| 109 | { |
| 110 | std::cout << " } END BLOCK: ModuleGlobalInfo\n"; |
| 111 | } |
| 112 | |
| 113 | void handleCompactionTableBegin() |
| 114 | { |
| 115 | std::cout << " BLOCK: CompactionTable {\n"; |
| 116 | } |
| 117 | |
| 118 | virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries ) |
| 119 | { |
| 120 | std::cout << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n"; |
| 121 | } |
| 122 | |
| 123 | virtual void handleCompactionTableType( |
| 124 | unsigned i, |
| 125 | unsigned TypSlot, |
| 126 | const Type* Ty |
| 127 | ) |
| 128 | { |
| 129 | std::cout << " Type: " << i << " Slot:" << TypSlot |
| 130 | << " is " << Ty->getDescription() << "\n"; |
| 131 | } |
| 132 | |
| 133 | virtual void handleCompactionTableValue( |
| 134 | unsigned i, |
| 135 | unsigned ValSlot, |
| 136 | const Type* Ty |
| 137 | ) |
| 138 | { |
| 139 | std::cout << " Value: " << i << " Slot:" << ValSlot |
| 140 | << " is " << Ty->getDescription() << "\n"; |
| 141 | } |
| 142 | |
| 143 | virtual void handleCompactionTableEnd() |
| 144 | { |
| 145 | std::cout << " } END BLOCK: CompactionTable\n"; |
| 146 | } |
| 147 | |
| 148 | virtual void handleSymbolTableBegin() |
| 149 | { |
| 150 | std::cout << " BLOCK: SymbolTable {\n"; |
| 151 | } |
| 152 | |
| 153 | virtual void handleSymbolTablePlane( |
| 154 | unsigned Ty, |
| 155 | unsigned NumEntries, |
| 156 | const Type* Typ |
| 157 | ) |
| 158 | { |
| 159 | std::cout << " Plane: Ty=" << Ty << " Size=" << NumEntries |
| 160 | << " Type: " << Typ->getDescription() << "\n"; |
| 161 | } |
| 162 | |
| 163 | virtual void handleSymbolTableType( |
| 164 | unsigned i, |
| 165 | unsigned slot, |
| 166 | const std::string& name |
| 167 | ) |
| 168 | { |
| 169 | std::cout << " Type " << i << " Slot=" << slot |
| 170 | << " Name: " << name << "\n"; |
| 171 | } |
| 172 | |
| 173 | virtual void handleSymbolTableValue( |
| 174 | unsigned i, |
| 175 | unsigned slot, |
| 176 | const std::string& name |
| 177 | ) |
| 178 | { |
| 179 | std::cout << " Value " << i << " Slot=" << slot |
| 180 | << " Name: " << name << "\n"; |
| 181 | } |
| 182 | |
| 183 | virtual void handleSymbolTableEnd() |
| 184 | { |
| 185 | std::cout << " } END BLOCK: SymbolTable\n"; |
| 186 | } |
| 187 | |
| 188 | virtual void handleFunctionBegin( |
| 189 | const Type* FType, |
| 190 | GlobalValue::LinkageTypes linkage |
| 191 | ) |
| 192 | { |
Reid Spencer | 0545b3e | 2004-06-09 06:16:19 +0000 | [diff] [blame^] | 193 | std::cout << "BLOCK: Function {\n"; |
| 194 | std::cout << " Linkage: " << linkage << "\n"; |
| 195 | std::cout << " Type: " << FType->getDescription() << "\n"; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | virtual void handleFunctionEnd( |
| 199 | const Type* FType |
| 200 | ) |
| 201 | { |
Reid Spencer | 0545b3e | 2004-06-09 06:16:19 +0000 | [diff] [blame^] | 202 | std::cout << "} END BLOCK: Function\n"; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | virtual void handleBasicBlockBegin( |
| 206 | unsigned blocknum |
| 207 | ) |
| 208 | { |
Reid Spencer | 0545b3e | 2004-06-09 06:16:19 +0000 | [diff] [blame^] | 209 | std::cout << " BLOCK: BasicBlock #" << blocknum << "{\n"; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | virtual bool handleInstruction( |
| 213 | unsigned Opcode, |
| 214 | const Type* iType, |
| 215 | std::vector<unsigned>& Operands |
| 216 | ) |
| 217 | { |
Reid Spencer | 0545b3e | 2004-06-09 06:16:19 +0000 | [diff] [blame^] | 218 | std::cout << " INST: OpCode=" |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 219 | << Instruction::getOpcodeName(Opcode) << " Type=" |
| 220 | << iType->getDescription() << "\n"; |
| 221 | for ( unsigned i = 0; i < Operands.size(); ++i ) |
Reid Spencer | 0545b3e | 2004-06-09 06:16:19 +0000 | [diff] [blame^] | 222 | std::cout << " Op#" << i << " Slot=" << Operands[i] << "\n"; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 223 | |
| 224 | return Instruction::isTerminator(Opcode); |
| 225 | } |
| 226 | |
| 227 | virtual void handleBasicBlockEnd(unsigned blocknum) |
| 228 | { |
Reid Spencer | 0545b3e | 2004-06-09 06:16:19 +0000 | [diff] [blame^] | 229 | std::cout << " } END BLOCK: BasicBlock #" << blocknum << "{\n"; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | virtual void handleGlobalConstantsBegin() |
| 233 | { |
| 234 | std::cout << " BLOCK: GlobalConstants {\n"; |
| 235 | } |
| 236 | |
| 237 | virtual void handleConstantExpression( |
| 238 | unsigned Opcode, |
| 239 | const Type* Typ, |
| 240 | std::vector<std::pair<const Type*,unsigned> > ArgVec |
| 241 | ) |
| 242 | { |
| 243 | std::cout << " EXPR: " << Instruction::getOpcodeName(Opcode) |
| 244 | << " Type=" << Typ->getDescription() << "\n"; |
| 245 | for ( unsigned i = 0; i < ArgVec.size(); ++i ) |
| 246 | std::cout << " Arg#" << i << " Type=" |
| 247 | << ArgVec[i].first->getDescription() << " Slot=" |
| 248 | << ArgVec[i].second << "\n"; |
| 249 | } |
| 250 | |
| 251 | virtual void handleConstantValue( Constant * c ) |
| 252 | { |
| 253 | std::cout << " VALUE: "; |
| 254 | c->print(std::cout); |
| 255 | std::cout << "\n"; |
| 256 | } |
| 257 | |
| 258 | virtual void handleConstantArray( |
| 259 | const ArrayType* AT, |
| 260 | std::vector<unsigned>& Elements ) |
| 261 | { |
| 262 | std::cout << " ARRAY: " << AT->getDescription() << "\n"; |
| 263 | for ( unsigned i = 0; i < Elements.size(); ++i ) |
| 264 | std::cout << " #" << i << " Slot=" << Elements[i] << "\n"; |
| 265 | } |
| 266 | |
| 267 | virtual void handleConstantStruct( |
| 268 | const StructType* ST, |
| 269 | std::vector<unsigned>& Elements) |
| 270 | { |
| 271 | std::cout << " STRUC: " << ST->getDescription() << "\n"; |
| 272 | for ( unsigned i = 0; i < Elements.size(); ++i ) |
| 273 | std::cout << " #" << i << " Slot=" << Elements[i] << "\n"; |
| 274 | } |
| 275 | |
| 276 | virtual void handleConstantPointer( |
| 277 | const PointerType* PT, unsigned Slot) |
| 278 | { |
| 279 | std::cout << " POINT: " << PT->getDescription() |
| 280 | << " Slot=" << Slot << "\n"; |
| 281 | } |
| 282 | |
| 283 | virtual void handleConstantString( const ConstantArray* CA ) |
| 284 | { |
| 285 | std::cout << " STRNG: "; |
| 286 | CA->print(std::cout); |
| 287 | std::cout << "\n"; |
| 288 | } |
| 289 | |
| 290 | virtual void handleGlobalConstantsEnd() |
| 291 | { |
| 292 | std::cout << " } END BLOCK: GlobalConstants\n"; |
| 293 | } |
| 294 | }; |
| 295 | |
| 296 | } |
| 297 | |
| 298 | void BytecodeAnalyzer::DumpBytecode( |
| 299 | const unsigned char *Buf, |
| 300 | unsigned Length, |
| 301 | BytecodeAnalysis& bca, |
| 302 | const std::string &ModuleID |
| 303 | ) |
| 304 | { |
| 305 | BytecodeDumper TheHandler; |
| 306 | AbstractBytecodeParser TheParser(&TheHandler); |
| 307 | TheParser.ParseBytecode( Buf, Length, ModuleID ); |
Reid Spencer | 0545b3e | 2004-06-09 06:16:19 +0000 | [diff] [blame^] | 308 | if ( bca.detailedResults ) |
| 309 | TheParser.ParseAllFunctionBodies(); |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // vim: sw=2 |