Add support for: -o -
[oota-llvm.git] / tools / llvm-as / llvm-as.cpp
1 //===------------------------------------------------------------------------===
2 // LLVM 'AS' UTILITY 
3 //
4 //  This utility may be invoked in the following manner:
5 //   as --help     - Output information about command line switches
6 //   as [options]      - Read LLVM assembly from stdin, write bytecode to stdout
7 //   as [options] x.ll - Read LLVM assembly from the x.ll file, write bytecode
8 //                       to the x.bc file.
9 // 
10 //===------------------------------------------------------------------------===
11
12 #include "llvm/Module.h"
13 #include "llvm/Assembly/Parser.h"
14 #include "llvm/Bytecode/Writer.h"
15 #include "llvm/Analysis/Verifier.h"
16 #include "Support/CommandLine.h"
17 #include "Support/Signals.h"
18 #include <fstream>
19 #include <memory>
20
21 static cl::opt<std::string> 
22 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
23
24 static cl::opt<std::string>
25 OutputFilename("o", cl::desc("Override output filename"),
26                cl::value_desc("filename"));
27
28 static cl::opt<bool>
29 Force("f", cl::desc("Overwrite output files"));
30
31 static cl::opt<bool>
32 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
33
34 int main(int argc, char **argv) {
35   cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
36
37   std::ostream *Out = 0;
38   try {
39     // Parse the file now...
40     std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
41     if (M.get() == 0) {
42       std::cerr << argv[0] << ": assembly didn't read correctly.\n";
43       return 1;
44     }
45
46     if (verifyModule(*M.get())) {
47       std::cerr << argv[0]
48                 << ": assembly parsed, but does not verify as correct!\n";
49       return 1;
50     }
51
52   
53     if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
54
55     if (OutputFilename != "") {   // Specified an output filename?
56       if (OutputFilename != "-") {  // Not stdout?
57         if (!Force && std::ifstream(OutputFilename.c_str())) {
58           // If force is not specified, make sure not to overwrite a file!
59           std::cerr << argv[0] << ": error opening '" << OutputFilename
60                     << "': file exists!\n"
61                     << "Use -f command line argument to force output\n";
62           return 1;
63         }
64         Out = new std::ofstream(OutputFilename.c_str());
65       } else {                      // Specified stdout
66         Out = &std::cout;       
67       }
68     } else {
69       if (InputFilename == "-") {
70         OutputFilename = "-";
71         Out = &std::cout;
72       } else {
73         std::string IFN = InputFilename;
74         int Len = IFN.length();
75         if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
76           // Source ends in .ll
77           OutputFilename = std::string(IFN.begin(), IFN.end()-3);
78         } else {
79           OutputFilename = IFN;   // Append a .bc to it
80         }
81         OutputFilename += ".bc";
82
83         if (!Force && std::ifstream(OutputFilename.c_str())) {
84           // If force is not specified, make sure not to overwrite a file!
85           std::cerr << argv[0] << ": error opening '" << OutputFilename
86                     << "': file exists!\n"
87                     << "Use -f command line argument to force output\n";
88           return 1;
89         }
90
91         Out = new std::ofstream(OutputFilename.c_str());
92         // Make sure that the Out file gets unlink'd from the disk if we get a
93         // SIGINT
94         RemoveFileOnSignal(OutputFilename);
95       }
96     }
97   
98     if (!Out->good()) {
99       std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
100       return 1;
101     }
102    
103     WriteBytecodeToFile(M.get(), *Out);
104   } catch (const ParseException &E) {
105     std::cerr << argv[0] << ": " << E.getMessage() << "\n";
106     return 1;
107   }
108
109   if (Out != &std::cout) delete Out;
110   return 0;
111 }
112