1 //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This utility may be invoked in the following manner:
11 // llvm-dis [options] - Read LLVM bytecode from stdin, write asm to stdout
12 // llvm-dis [options] x.bc - Read LLVM bytecode from the x.bc file, write asm
15 // --help - Output information about command line switches
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Module.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Bytecode/Reader.h"
22 #include "llvm/Assembly/PrintModulePass.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/System/Signals.h"
30 static cl::opt<std::string>
31 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
33 static cl::opt<std::string>
34 OutputFilename("o", cl::desc("Override output filename"),
35 cl::value_desc("filename"));
38 Force("f", cl::desc("Overwrite output files"));
40 int main(int argc, char **argv) {
42 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
43 sys::PrintStackTraceOnErrorSignal();
45 std::ostream *Out = &std::cout; // Default to printing to stdout.
46 std::string ErrorMessage;
48 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
50 std::cerr << argv[0] << ": ";
51 if (ErrorMessage.size())
52 std::cerr << ErrorMessage << "\n";
54 std::cerr << "bytecode didn't read correctly.\n";
58 if (OutputFilename != "") { // Specified an output filename?
59 if (OutputFilename != "-") { // Not stdout?
60 if (!Force && std::ifstream(OutputFilename.c_str())) {
61 // If force is not specified, make sure not to overwrite a file!
62 std::cerr << argv[0] << ": error opening '" << OutputFilename
63 << "': file exists! Sending to standard output.\n";
65 Out = new std::ofstream(OutputFilename.c_str());
69 if (InputFilename == "-") {
72 std::string IFN = InputFilename;
73 int Len = IFN.length();
74 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
76 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
78 OutputFilename = IFN+".ll";
81 if (!Force && std::ifstream(OutputFilename.c_str())) {
82 // If force is not specified, make sure not to overwrite a file!
83 std::cerr << argv[0] << ": error opening '" << OutputFilename
84 << "': file exists! Sending to standard output.\n";
86 Out = new std::ofstream(OutputFilename.c_str());
88 // Make sure that the Out file gets unlinked from the disk if we get a
90 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
96 std::cerr << argv[0] << ": error opening " << OutputFilename
97 << ": sending to stdout instead!\n";
101 // All that llvm-dis does is write the assembly to a file.
103 Passes.add(new PrintModulePass(Out));
104 Passes.run(*M.get());
106 if (Out != &std::cout) {
107 ((std::ofstream*)Out)->close();
111 } catch (const std::string& msg) {
112 std::cerr << argv[0] << ": " << msg << "\n";
114 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";