Initial revision
[oota-llvm.git] / tools / llvm-dis / dis.cpp
1 //===------------------------------------------------------------------------===
2 // LLVM 'DIS' UTILITY 
3 //
4 // This utility may be invoked in the following manner:
5 //  dis --help     - Output information about command line switches
6 //  dis [options]      - Read LLVM bytecode from stdin, write assembly to stdout
7 //  dis [options] x.bc - Read LLVM bytecode from the x.bc file, write assembly
8 //                       to the x.ll file.
9 //
10 //===------------------------------------------------------------------------===
11
12 #include <iostream.h>
13 #include <fstream.h>
14 #include "llvm/Module.h"
15 #include "llvm/Assembly/Writer.h"
16 #include "llvm/Bytecode/Reader.h"
17 #include "llvm/Tools/CommandLine.h"
18
19 int main(int argc, char **argv) {
20   ToolCommandLine Opts(argc, argv, false);
21
22   // We only support the options that the system parser does... if it left any
23   // then we don't know what to do.
24   //
25   if (argc > 1) {
26     for (int i = 1; i < argc; i++) {
27       if (string(argv[i]) != string("--help"))
28         cerr << argv[0] << ": argument not recognized: '" << argv[i] << "'!\n";
29     }
30     
31     cerr << argv[0] << " usage:\n"
32          << "  " << argv[0] << " --help  - Print this usage information\n" 
33          << "  " << argv[0] << " x.bc    - Parse <x.bc> file and output "
34          << "assembly to x.ll\n"
35          << "  " << argv[0] << "         - Parse stdin and write to stdout.\n";
36     return 1;
37   }
38   
39   ostream *Out = &cout;  // Default to printing to stdout...
40
41   Module *C = ParseBytecodeFile(Opts.getInputFilename());
42   if (C == 0) {
43     cerr << "bytecode didn't read correctly.\n";
44     return 1;
45   }
46   
47   if (Opts.getOutputFilename() != "-") {
48     Out = new ofstream(Opts.getOutputFilename().c_str(), 
49                        (Opts.getForce() ? 0 : ios::noreplace)|ios::out);
50     if (!Out->good()) {
51       cerr << "Error opening " << Opts.getOutputFilename() 
52            << ": sending to stdout instead!\n";
53       Out = &cout;
54       }
55   }
56     
57   // All that dis does is write the assembly out to a file... which is exactly
58   // what the writer library is supposed to do...
59   (*Out) << C;
60   delete C;
61
62   if (Out != &cout) delete Out;
63   return 0;
64 }