blob: cbb51521121911a796355b11fa12c9bc27feaa89 [file] [log] [blame]
Reid Spencer8a542ae2004-07-02 03:22:53 +00001//===-- llvm-bcanalyzer.cpp - Byte Code Analyzer --------------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Reid Spencerdac69c82004-06-07 17:53:43 +00003// The LLVM Compiler Infrastructure
4//
Misha Brukman3da94ae2005-04-22 00:00:37 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencer96684ef2004-06-08 05:56:58 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
Reid Spencerdac69c82004-06-07 17:53:43 +00008//===----------------------------------------------------------------------===//
9//
Reid Spencer96684ef2004-06-08 05:56:58 +000010// This tool may be invoked in the following manner:
Reid Spencer8a542ae2004-07-02 03:22:53 +000011// llvm-bcanalyzer [options] - Read LLVM bytecode from stdin
12// llvm-bcanalyzer [options] x.bc - Read LLVM bytecode from the x.bc file
Reid Spencerdac69c82004-06-07 17:53:43 +000013//
Reid Spencer96684ef2004-06-08 05:56:58 +000014// Options:
Reid Spencer23d46e72004-06-10 18:38:44 +000015// --help - Output information about command line switches
Misha Brukman3da94ae2005-04-22 00:00:37 +000016// --nodetails - Don't print out detailed informaton about individual
Reid Spencer23d46e72004-06-10 18:38:44 +000017// blocks and functions
18// --dump - Dump low-level bytecode structure in readable format
Reid Spencer96684ef2004-06-08 05:56:58 +000019//
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 Brukman3da94ae2005-04-22 00:00:37 +000022// produces on std::out a summary of the bytecode file that shows various
Reid Spencer23d46e72004-06-10 18:38:44 +000023// statistics about the contents of the file. By default this information is
24// detailed and contains information about individual bytecode blocks and the
Misha Brukman3da94ae2005-04-22 00:00:37 +000025// functions in the module. To avoid this more detailed output, use the
Reid Spencer23d46e72004-06-10 18:38:44 +000026// -nodetails option to limit the output to just module level information.
Misha Brukman3da94ae2005-04-22 00:00:37 +000027// 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 Spencerdac69c82004-06-07 17:53:43 +000030//===----------------------------------------------------------------------===//
31
Reid Spencer86a9a7a2004-06-29 23:34:27 +000032#include "llvm/Analysis/Verifier.h"
Reid Spencerdac69c82004-06-07 17:53:43 +000033#include "llvm/Bytecode/Analyzer.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000034#include "llvm/Support/CommandLine.h"
Chris Lattnerf2e292c2007-02-07 21:41:02 +000035#include "llvm/Support/Compressor.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000036#include "llvm/Support/ManagedStatic.h"
Reid Spencerdac69c82004-06-07 17:53:43 +000037#include "llvm/System/Signals.h"
38#include <fstream>
39#include <iostream>
40
41using namespace llvm;
42
43static cl::opt<std::string>
44 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
45
Reid Spencer75937452004-08-21 21:00:24 +000046static cl::opt<std::string>
47 OutputFilename("-o", cl::init("-"), cl::desc("<output file>"));
48
Reid Spencer23d46e72004-06-10 18:38:44 +000049static cl::opt<bool> NoDetails ("nodetails", cl::desc("Skip detailed output"));
Reid Spencer86a9a7a2004-06-29 23:34:27 +000050static cl::opt<bool> Dump ("dump", cl::desc("Dump low level bytecode trace"));
51static cl::opt<bool> Verify ("verify", cl::desc("Progressively verify module"));
Reid Spencerdac69c82004-06-07 17:53:43 +000052
Chris Lattnerc30598b2006-12-06 01:18:01 +000053int main(int argc, char **argv) {
54 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000055 try {
Misha Brukman3da94ae2005-04-22 00:00:37 +000056 cl::ParseCommandLineOptions(argc, argv,
Reid Spencer1ef8bda2004-12-30 05:36:08 +000057 " llvm-bcanalyzer Analysis of ByteCode Dumper\n");
Reid Spencerdac69c82004-06-07 17:53:43 +000058
Reid Spencer1ef8bda2004-12-30 05:36:08 +000059 sys::PrintStackTraceOnErrorSignal();
Reid Spencerdac69c82004-06-07 17:53:43 +000060
Reid Spencer1ef8bda2004-12-30 05:36:08 +000061 std::ostream* Out = &std::cout; // Default to printing to stdout...
Reid Spencer1ef8bda2004-12-30 05:36:08 +000062 std::string ErrorMessage;
63 BytecodeAnalysis bca;
Reid Spencerdac69c82004-06-07 17:53:43 +000064
Reid Spencer1ef8bda2004-12-30 05:36:08 +000065 /// Determine what to generate
66 bca.detailedResults = !NoDetails;
67 bca.progressiveVerify = Verify;
Reid Spencer96684ef2004-06-08 05:56:58 +000068
Reid Spencer1ef8bda2004-12-30 05:36:08 +000069 /// Analyze the bytecode file
Chris Lattnerf2e292c2007-02-07 21:41:02 +000070 Module* M = AnalyzeBytecodeFile(InputFilename, bca,
71 Compressor::decompressToNewBuffer,
72 &ErrorMessage, (Dump?Out:0));
Reid Spencer86a9a7a2004-06-29 23:34:27 +000073
Reid Spencer1ef8bda2004-12-30 05:36:08 +000074 // All that bcanalyzer does is write the gathered statistics to the output
75 PrintBytecodeAnalysis(bca,*Out);
Reid Spencer86a9a7a2004-06-29 23:34:27 +000076
Reid Spencer1ef8bda2004-12-30 05:36:08 +000077 if ( M && Verify ) {
78 std::string verificationMsg;
Chris Lattner05ac92c2006-07-06 18:02:27 +000079 if (verifyModule(*M, ReturnStatusAction, &verificationMsg))
Reid Spencer1ef8bda2004-12-30 05:36:08 +000080 std::cerr << "Final Verification Message: " << verificationMsg << "\n";
Reid Spencer86a9a7a2004-06-29 23:34:27 +000081 }
Reid Spencer86a9a7a2004-06-29 23:34:27 +000082
Reid Spencer1ef8bda2004-12-30 05:36:08 +000083 if (Out != &std::cout) {
84 ((std::ofstream*)Out)->close();
85 delete Out;
86 }
87 return 0;
88 } catch (const std::string& msg) {
89 std::cerr << argv[0] << ": " << msg << "\n";
90 } catch (...) {
91 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Reid Spencerdac69c82004-06-07 17:53:43 +000092 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +000093 return 1;
Reid Spencerdac69c82004-06-07 17:53:43 +000094}
95
96// vim: sw=2