1 //===-- llvm-bcanalyzer.cpp - Byte Code Analyzer --------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This tool may be invoked in the following manner:
11 // llvm-bcanalyzer [options] - Read LLVM bytecode from stdin
12 // llvm-bcanalyzer [options] x.bc - Read LLVM bytecode from the x.bc file
15 // --help - Output information about command line switches
16 // --nodetails - Don't print out detailed informaton about individual
17 // blocks and functions
18 // --dump - Dump low-level bytecode structure in readable format
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
22 // produces on std::out a summary of the bytecode file that shows various
23 // statistics about the contents of the file. By default this information is
24 // detailed and contains information about individual bytecode blocks and the
25 // functions in the module. To avoid this more detailed output, use the
26 // -nodetails option to limit the output to just module level information.
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).
30 //===----------------------------------------------------------------------===//
32 #include "llvm/Analysis/Verifier.h"
33 #include "llvm/Bytecode/Analyzer.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/System/Signals.h"
41 static cl::opt<std::string>
42 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
44 static cl::opt<std::string>
45 OutputFilename("-o", cl::init("-"), cl::desc("<output file>"));
47 static cl::opt<bool> NoDetails ("nodetails", cl::desc("Skip detailed output"));
48 static cl::opt<bool> Dump ("dump", cl::desc("Dump low level bytecode trace"));
49 static cl::opt<bool> Verify ("verify", cl::desc("Progressively verify module"));
52 main(int argc, char **argv) {
54 cl::ParseCommandLineOptions(argc, argv,
55 " llvm-bcanalyzer Analysis of ByteCode Dumper\n");
57 sys::PrintStackTraceOnErrorSignal();
59 std::ostream* Out = &std::cout; // Default to printing to stdout...
60 std::istream* In = &std::cin; // Default to reading stdin
61 std::string ErrorMessage;
64 /// Determine what to generate
65 bca.detailedResults = !NoDetails;
66 bca.progressiveVerify = Verify;
68 /// Analyze the bytecode file
69 Module* M = AnalyzeBytecodeFile(InputFilename, bca, &ErrorMessage, (Dump?Out:0));
71 // All that bcanalyzer does is write the gathered statistics to the output
72 PrintBytecodeAnalysis(bca,*Out);
75 std::string verificationMsg;
77 verifyModule( *M, ThrowExceptionAction );
78 } catch (std::string& errmsg ) {
79 verificationMsg = errmsg;
81 if ( verificationMsg.length() > 0 )
82 std::cerr << "Final Verification Message: " << verificationMsg << "\n";
86 // If there was an error, print it and stop.
87 if ( ErrorMessage.size() ) {
88 std::cerr << argv[0] << ": " << ErrorMessage << "\n";
93 if (Out != &std::cout) {
94 ((std::ofstream*)Out)->close();
98 } catch (const std::string& msg) {
99 std::cerr << argv[0] << ": " << msg << "\n";
101 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";