summaryrefslogtreecommitdiff
path: root/tools/llvm-dis/llvm-dis.cpp
blob: 3f45ee3c0d8052ef89cdb2db32efa427213ffb1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//===------------------------------------------------------------------------===
// LLVM 'DIS' UTILITY 
//
// This utility may be invoked in the following manner:
//  dis --help     - Output information about command line switches
//  dis [options]      - Read LLVM bytecode from stdin, write assembly to stdout
//  dis [options] x.bc - Read LLVM bytecode from the x.bc file, write assembly
//                       to the x.ll file.
//
//===------------------------------------------------------------------------===

#include <iostream.h>
#include <fstream.h>
#include "llvm/Module.h"
#include "llvm/Assembly/Writer.h"
#include "llvm/Bytecode/Reader.h"
#include "llvm/Tools/CommandLine.h"

int main(int argc, char **argv) {
  ToolCommandLine Opts(argc, argv, false);

  // We only support the options that the system parser does... if it left any
  // then we don't know what to do.
  //
  if (argc > 1) {
    for (int i = 1; i < argc; i++) {
      if (string(argv[i]) != string("--help"))
	cerr << argv[0] << ": argument not recognized: '" << argv[i] << "'!\n";
    }
    
    cerr << argv[0] << " usage:\n"
	 << "  " << argv[0] << " --help  - Print this usage information\n" 
	 << "  " << argv[0] << " x.bc    - Parse <x.bc> file and output "
	 << "assembly to x.ll\n"
	 << "  " << argv[0] << "         - Parse stdin and write to stdout.\n";
    return 1;
  }
  
  ostream *Out = &cout;  // Default to printing to stdout...

  Module *C = ParseBytecodeFile(Opts.getInputFilename());
  if (C == 0) {
    cerr << "bytecode didn't read correctly.\n";
    return 1;
  }
  
  if (Opts.getOutputFilename() != "-") {
    Out = new ofstream(Opts.getOutputFilename().c_str(), 
                       (Opts.getForce() ? 0 : ios::noreplace)|ios::out);
    if (!Out->good()) {
      cerr << "Error opening " << Opts.getOutputFilename() 
           << ": sending to stdout instead!\n";
      Out = &cout;
      }
  }
    
  // All that dis does is write the assembly out to a file... which is exactly
  // what the writer library is supposed to do...
  (*Out) << C;
  delete C;

  if (Out != &cout) delete Out;
  return 0;
}