Simplify some getNode calls.
[oota-llvm.git] / tools / llvm-as / llvm-as.cpp
1 //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This utility may be invoked in the following manner:
11 //   llvm-as --help         - Output information about command line switches
12 //   llvm-as [options]      - Read LLVM asm from stdin, write bitcode to stdout
13 //   llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bitcode
14 //                            to the x.bc file.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include "llvm/Module.h"
19 #include "llvm/Assembly/Parser.h"
20 #include "llvm/Analysis/Verifier.h"
21 #include "llvm/Bitcode/ReaderWriter.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/Streams.h"
25 #include "llvm/Support/SystemUtils.h"
26 #include "llvm/System/Signals.h"
27 #include <fstream>
28 #include <iostream>
29 #include <memory>
30 using namespace llvm;
31
32 static cl::opt<std::string>
33 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
34
35 static cl::opt<std::string>
36 OutputFilename("o", cl::desc("Override output filename"),
37                cl::value_desc("filename"));
38
39 static cl::opt<bool>
40 Force("f", cl::desc("Overwrite output files"));
41
42 static cl::opt<bool>
43 DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
44
45 static cl::opt<bool>
46 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
47
48 static cl::opt<bool>
49 DisableVerify("disable-verify", cl::Hidden,
50               cl::desc("Do not run verifier on input LLVM (dangerous!)"));
51
52 int main(int argc, char **argv) {
53   llvm_shutdown_obj X;  // Call llvm_shutdown() on exit.
54   cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
55   sys::PrintStackTraceOnErrorSignal();
56
57   int exitCode = 0;
58   std::ostream *Out = 0;
59   try {
60     // Parse the file now...
61     ParseError Err;
62     std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename,&Err));
63     if (M.get() == 0) {
64       cerr << argv[0] << ": " << Err.getMessage() << "\n"; 
65       return 1;
66     }
67
68     if (!DisableVerify) {
69       std::string Err;
70       if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
71         cerr << argv[0]
72              << ": assembly parsed, but does not verify as correct!\n";
73         cerr << Err;
74         return 1;
75       } 
76     }
77
78     if (DumpAsm) cerr << "Here's the assembly:\n" << *M.get();
79
80     if (OutputFilename != "") {   // Specified an output filename?
81       if (OutputFilename != "-") {  // Not stdout?
82         if (!Force && std::ifstream(OutputFilename.c_str())) {
83           // If force is not specified, make sure not to overwrite a file!
84           cerr << argv[0] << ": error opening '" << OutputFilename
85                << "': file exists!\n"
86                << "Use -f command line argument to force output\n";
87           return 1;
88         }
89         Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
90                                 std::ios::trunc | std::ios::binary);
91       } else {                      // Specified stdout
92         // FIXME: cout is not binary!
93         Out = &std::cout;
94       }
95     } else {
96       if (InputFilename == "-") {
97         OutputFilename = "-";
98         Out = &std::cout;
99       } else {
100         std::string IFN = InputFilename;
101         int Len = IFN.length();
102         if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
103           // Source ends in .ll
104           OutputFilename = std::string(IFN.begin(), IFN.end()-3);
105         } else {
106           OutputFilename = IFN;   // Append a .bc to it
107         }
108         OutputFilename += ".bc";
109
110         if (!Force && std::ifstream(OutputFilename.c_str())) {
111           // If force is not specified, make sure not to overwrite a file!
112           cerr << argv[0] << ": error opening '" << OutputFilename
113                << "': file exists!\n"
114                << "Use -f command line argument to force output\n";
115           return 1;
116         }
117
118         Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
119                                 std::ios::trunc | std::ios::binary);
120         // Make sure that the Out file gets unlinked from the disk if we get a
121         // SIGINT
122         sys::RemoveFileOnSignal(sys::Path(OutputFilename));
123       }
124     }
125
126     if (!Out->good()) {
127       cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
128       return 1;
129     }
130
131     if (!DisableOutput)
132       if (Force || !CheckBitcodeOutputToConsole(Out,true))
133         WriteBitcodeToFile(M.get(), *Out);
134   } catch (const std::string& msg) {
135     cerr << argv[0] << ": " << msg << "\n";
136     exitCode = 1;
137   } catch (...) {
138     cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
139     exitCode = 1;
140   }
141
142   if (Out != &std::cout) delete Out;
143   return exitCode;
144 }
145