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). |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Reid Spencer | 86a9a7a | 2004-06-29 23:34:27 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/Verifier.h" |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 33 | #include "llvm/Bytecode/Analyzer.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ManagedStatic.h" |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 36 | #include "llvm/System/Signals.h" |
| 37 | #include <fstream> |
| 38 | #include <iostream> |
| 39 | |
| 40 | using namespace llvm; |
| 41 | |
| 42 | static cl::opt<std::string> |
| 43 | InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-")); |
| 44 | |
Reid Spencer | 7593745 | 2004-08-21 21:00:24 +0000 | [diff] [blame] | 45 | static cl::opt<std::string> |
| 46 | OutputFilename("-o", cl::init("-"), cl::desc("<output file>")); |
| 47 | |
Reid Spencer | 23d46e7 | 2004-06-10 18:38:44 +0000 | [diff] [blame] | 48 | static cl::opt<bool> NoDetails ("nodetails", cl::desc("Skip detailed output")); |
Reid Spencer | 86a9a7a | 2004-06-29 23:34:27 +0000 | [diff] [blame] | 49 | static cl::opt<bool> Dump ("dump", cl::desc("Dump low level bytecode trace")); |
| 50 | static cl::opt<bool> Verify ("verify", cl::desc("Progressively verify module")); |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 51 | |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 52 | int main(int argc, char **argv) { |
| 53 | llvm_shutdown_obj X; // Call llvm_shutdown() on exit. |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 54 | try { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 55 | cl::ParseCommandLineOptions(argc, argv, |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 56 | " llvm-bcanalyzer Analysis of ByteCode Dumper\n"); |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 57 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 58 | sys::PrintStackTraceOnErrorSignal(); |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 59 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 60 | std::ostream* Out = &std::cout; // Default to printing to stdout... |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 61 | std::string ErrorMessage; |
| 62 | BytecodeAnalysis bca; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 63 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 64 | /// Determine what to generate |
| 65 | bca.detailedResults = !NoDetails; |
| 66 | bca.progressiveVerify = Verify; |
Reid Spencer | 96684ef | 2004-06-08 05:56:58 +0000 | [diff] [blame] | 67 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 68 | /// Analyze the bytecode file |
| 69 | Module* M = AnalyzeBytecodeFile(InputFilename, bca, &ErrorMessage, (Dump?Out:0)); |
Reid Spencer | 86a9a7a | 2004-06-29 23:34:27 +0000 | [diff] [blame] | 70 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 71 | // All that bcanalyzer does is write the gathered statistics to the output |
| 72 | PrintBytecodeAnalysis(bca,*Out); |
Reid Spencer | 86a9a7a | 2004-06-29 23:34:27 +0000 | [diff] [blame] | 73 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 74 | if ( M && Verify ) { |
| 75 | std::string verificationMsg; |
Chris Lattner | 05ac92c | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 76 | if (verifyModule(*M, ReturnStatusAction, &verificationMsg)) |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 77 | std::cerr << "Final Verification Message: " << verificationMsg << "\n"; |
Reid Spencer | 86a9a7a | 2004-06-29 23:34:27 +0000 | [diff] [blame] | 78 | } |
Reid Spencer | 86a9a7a | 2004-06-29 23:34:27 +0000 | [diff] [blame] | 79 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 80 | if (Out != &std::cout) { |
| 81 | ((std::ofstream*)Out)->close(); |
| 82 | delete Out; |
| 83 | } |
| 84 | return 0; |
| 85 | } catch (const std::string& msg) { |
| 86 | std::cerr << argv[0] << ": " << msg << "\n"; |
| 87 | } catch (...) { |
| 88 | std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 89 | } |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 90 | return 1; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // vim: sw=2 |