Reid Spencer | 8a542ae | 2004-07-02 03:22:53 +0000 | [diff] [blame] | 1 | //===-- llvm-bcanalyzer.cpp - Byte Code Analyzer --------------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 5 | // This file was developed by Reid Spencer and is distributed under the |
Reid Spencer | 96684ef | 2004-06-08 05:56:58 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Reid Spencer | 96684ef | 2004-06-08 05:56:58 +0000 | [diff] [blame] | 10 | // This tool may be invoked in the following manner: |
Reid Spencer | 8a542ae | 2004-07-02 03:22:53 +0000 | [diff] [blame] | 11 | // llvm-bcanalyzer [options] - Read LLVM bytecode from stdin |
| 12 | // llvm-bcanalyzer [options] x.bc - Read LLVM bytecode from the x.bc file |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 13 | // |
Reid Spencer | 96684ef | 2004-06-08 05:56:58 +0000 | [diff] [blame] | 14 | // Options: |
Reid Spencer | 23d46e7 | 2004-06-10 18:38:44 +0000 | [diff] [blame] | 15 | // --help - Output information about command line switches |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 16 | // --nodetails - Don't print out detailed informaton about individual |
Reid Spencer | 23d46e7 | 2004-06-10 18:38:44 +0000 | [diff] [blame] | 17 | // blocks and functions |
| 18 | // --dump - Dump low-level bytecode structure in readable format |
Reid Spencer | 96684ef | 2004-06-08 05:56:58 +0000 | [diff] [blame] | 19 | // |
| 20 | // This tool provides analytical information about a bytecode file. It is |
| 21 | // intended as an aid to developers of bytecode reading and writing software. It |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 22 | // produces on std::out a summary of the bytecode file that shows various |
Reid Spencer | 23d46e7 | 2004-06-10 18:38:44 +0000 | [diff] [blame] | 23 | // statistics about the contents of the file. By default this information is |
| 24 | // detailed and contains information about individual bytecode blocks and the |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 25 | // functions in the module. To avoid this more detailed output, use the |
Reid Spencer | 23d46e7 | 2004-06-10 18:38:44 +0000 | [diff] [blame] | 26 | // -nodetails option to limit the output to just module level information. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 27 | // The tool is also able to print a bytecode file in a straight forward text |
| 28 | // format that shows the containment and relationships of the information in |
| 29 | // the bytecode file (-dump option). |
Chris Lattner | 63db485 | 2007-04-29 05:51:00 +0000 | [diff] [blame] | 30 | // |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Reid Spencer | 86a9a7a | 2004-06-29 23:34:27 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 34 | #include "llvm/Bitcode/BitstreamReader.h" |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 35 | #include "llvm/Bytecode/Analyzer.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 36 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | f2e292c | 2007-02-07 21:41:02 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Compressor.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 39 | #include "llvm/Support/MemoryBuffer.h" |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 40 | #include "llvm/System/Signals.h" |
| 41 | #include <fstream> |
| 42 | #include <iostream> |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 43 | using namespace llvm; |
| 44 | |
| 45 | static cl::opt<std::string> |
| 46 | InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-")); |
| 47 | |
Reid Spencer | 7593745 | 2004-08-21 21:00:24 +0000 | [diff] [blame] | 48 | static cl::opt<std::string> |
| 49 | OutputFilename("-o", cl::init("-"), cl::desc("<output file>")); |
| 50 | |
Chris Lattner | 63db485 | 2007-04-29 05:51:00 +0000 | [diff] [blame] | 51 | static cl::opt<bool> NoDetails("nodetails", cl::desc("Skip detailed output")); |
| 52 | static cl::opt<bool> Dump("dump", cl::desc("Dump low level bytecode trace")); |
| 53 | static cl::opt<bool> Verify("verify", cl::desc("Progressively verify module")); |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 54 | |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | // Bitcode specific analysis. |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 59 | static cl::opt<bool> Bitcode("bitcode", cl::desc("Read a bitcode file")); |
| 60 | |
| 61 | /// CurStreamType - If we can sniff the flavor of this stream, we can produce |
| 62 | /// better dump info. |
| 63 | static enum { |
| 64 | UnknownBitstream, |
| 65 | LLVMIRBitstream |
| 66 | } CurStreamType; |
| 67 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 68 | /// Error - All bitcode analysis errors go through this function, making this a |
| 69 | /// good place to breakpoint if debugging. |
| 70 | static bool Error(const std::string &Err) { |
| 71 | std::cerr << Err << "\n"; |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | /// ParseBlock - Read a block, updating statistics, etc. |
| 76 | static bool ParseBlock(BitstreamReader &Stream) { |
| 77 | unsigned BlockID = Stream.ReadSubBlockID(); |
| 78 | |
| 79 | // TODO: Compute per-block-id stats. |
| 80 | BlockID = BlockID; |
| 81 | |
| 82 | if (Stream.EnterSubBlock()) |
| 83 | return Error("Malformed block record"); |
| 84 | |
| 85 | SmallVector<uint64_t, 64> Record; |
| 86 | |
| 87 | // Read all the records for this block. |
| 88 | while (1) { |
| 89 | if (Stream.AtEndOfStream()) |
| 90 | return Error("Premature end of bitstream"); |
| 91 | |
| 92 | // Read the code for this record. |
| 93 | unsigned AbbrevID = Stream.ReadCode(); |
| 94 | switch (AbbrevID) { |
| 95 | case bitc::END_BLOCK: |
| 96 | if (Stream.ReadBlockEnd()) |
| 97 | return Error("Error at end of block"); |
| 98 | return false; |
| 99 | case bitc::ENTER_SUBBLOCK: |
| 100 | if (ParseBlock(Stream)) |
| 101 | return true; |
| 102 | break; |
| 103 | case bitc::DEFINE_ABBREV: |
| 104 | Stream.ReadAbbrevRecord(); |
| 105 | break; |
| 106 | default: |
| 107 | Record.clear(); |
| 108 | unsigned Code = Stream.ReadRecord(AbbrevID, Record); |
| 109 | // TODO: Compute per-blockid/code stats. |
| 110 | Code = Code; |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 116 | /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename. |
| 117 | static int AnalyzeBitcode() { |
| 118 | // Read the input file. |
| 119 | MemoryBuffer *Buffer; |
| 120 | if (InputFilename == "-") |
| 121 | Buffer = MemoryBuffer::getSTDIN(); |
| 122 | else |
| 123 | Buffer = MemoryBuffer::getFile(&InputFilename[0], InputFilename.size()); |
| 124 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 125 | if (Buffer == 0) |
| 126 | return Error("Error reading '" + InputFilename + "'."); |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 128 | if (Buffer->getBufferSize() & 3) |
| 129 | return Error("Bitcode stream should be a multiple of 4 bytes in length"); |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 130 | |
| 131 | unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart(); |
| 132 | BitstreamReader Stream(BufPtr, BufPtr+Buffer->getBufferSize()); |
| 133 | |
| 134 | |
| 135 | // Read the stream signature. |
| 136 | char Signature[6]; |
| 137 | Signature[0] = Stream.Read(8); |
| 138 | Signature[1] = Stream.Read(8); |
| 139 | Signature[2] = Stream.Read(4); |
| 140 | Signature[3] = Stream.Read(4); |
| 141 | Signature[4] = Stream.Read(4); |
| 142 | Signature[5] = Stream.Read(4); |
| 143 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 144 | // Autodetect the file contents, if it is one we know. |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 145 | CurStreamType = UnknownBitstream; |
| 146 | if (Signature[0] == 'B' && Signature[1] == 'C' && |
| 147 | Signature[2] == 0x0 && Signature[3] == 0xC && |
| 148 | Signature[4] == 0xE && Signature[5] == 0xD) |
| 149 | CurStreamType = LLVMIRBitstream; |
| 150 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 151 | // Parse the top-level structure. We only allow blocks at the top-level. |
| 152 | while (!Stream.AtEndOfStream()) { |
| 153 | unsigned Code = Stream.ReadCode(); |
| 154 | if (Code != bitc::ENTER_SUBBLOCK) |
| 155 | return Error("Invalid record at top-level"); |
| 156 | |
| 157 | if (ParseBlock(Stream)) |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | // Print a summary of the read file. |
| 162 | |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 163 | std::cerr << "Summary of " << InputFilename << ":\n"; |
| 164 | std::cerr << " Stream type: "; |
| 165 | switch (CurStreamType) { |
| 166 | default: assert(0 && "Unknown bitstream type"); |
| 167 | case UnknownBitstream: std::cerr << "unknown\n"; break; |
| 168 | case LLVMIRBitstream: std::cerr << "LLVM IR\n"; break; |
| 169 | } |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 170 | |
| 171 | // TODO: Stats! |
| 172 | |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 173 | return 0; |
| 174 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 176 | |
| 177 | //===----------------------------------------------------------------------===// |
| 178 | // Bytecode specific analysis. |
| 179 | //===----------------------------------------------------------------------===// |
| 180 | |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 181 | int main(int argc, char **argv) { |
| 182 | llvm_shutdown_obj X; // Call llvm_shutdown() on exit. |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 183 | cl::ParseCommandLineOptions(argc, argv, " llvm-bcanalyzer file analyzer\n"); |
| 184 | |
| 185 | sys::PrintStackTraceOnErrorSignal(); |
| 186 | |
| 187 | if (Bitcode) |
| 188 | return AnalyzeBitcode(); |
| 189 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 190 | try { |
Chris Lattner | 63db485 | 2007-04-29 05:51:00 +0000 | [diff] [blame] | 191 | std::ostream *Out = &std::cout; // Default to printing to stdout... |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 192 | std::string ErrorMessage; |
| 193 | BytecodeAnalysis bca; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 194 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 195 | /// Determine what to generate |
| 196 | bca.detailedResults = !NoDetails; |
| 197 | bca.progressiveVerify = Verify; |
Reid Spencer | 96684ef | 2004-06-08 05:56:58 +0000 | [diff] [blame] | 198 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 199 | /// Analyze the bytecode file |
Chris Lattner | f2e292c | 2007-02-07 21:41:02 +0000 | [diff] [blame] | 200 | Module* M = AnalyzeBytecodeFile(InputFilename, bca, |
| 201 | Compressor::decompressToNewBuffer, |
| 202 | &ErrorMessage, (Dump?Out:0)); |
Reid Spencer | 86a9a7a | 2004-06-29 23:34:27 +0000 | [diff] [blame] | 203 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 204 | // All that bcanalyzer does is write the gathered statistics to the output |
| 205 | PrintBytecodeAnalysis(bca,*Out); |
Reid Spencer | 86a9a7a | 2004-06-29 23:34:27 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 207 | if (M && Verify) { |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 208 | std::string verificationMsg; |
Chris Lattner | 05ac92c | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 209 | if (verifyModule(*M, ReturnStatusAction, &verificationMsg)) |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 210 | std::cerr << "Final Verification Message: " << verificationMsg << "\n"; |
Reid Spencer | 86a9a7a | 2004-06-29 23:34:27 +0000 | [diff] [blame] | 211 | } |
Reid Spencer | 86a9a7a | 2004-06-29 23:34:27 +0000 | [diff] [blame] | 212 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 213 | if (Out != &std::cout) { |
| 214 | ((std::ofstream*)Out)->close(); |
| 215 | delete Out; |
| 216 | } |
| 217 | return 0; |
| 218 | } catch (const std::string& msg) { |
| 219 | std::cerr << argv[0] << ": " << msg << "\n"; |
| 220 | } catch (...) { |
| 221 | std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 222 | } |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 223 | return 1; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 224 | } |