Use operator << to print modules
[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 "Support/CommandLine.h"
16 #include "Support/Signals.h"
17 #include <fstream>
18 #include <string>
19 #include <memory>
20
21 cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", 0, "-");
22 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
23 cl::Flag   Force         ("f", "Overwrite output files", cl::NoFlags, false);
24 cl::Flag   DumpAsm       ("d", "Print assembly as parsed", cl::Hidden, false);
25
26 int main(int argc, char **argv) {
27   cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
28
29   ostream *Out = 0;
30   try {
31     // Parse the file now...
32     std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
33     if (M.get() == 0) {
34       cerr << "assembly didn't read correctly.\n";
35       return 1;
36     }
37   
38     if (DumpAsm) cerr << "Here's the assembly:\n" << M.get();
39
40     if (OutputFilename != "") {   // Specified an output filename?
41       if (!Force && std::ifstream(OutputFilename.c_str())) {
42         // If force is not specified, make sure not to overwrite a file!
43         cerr << "Error opening '" << OutputFilename << "': File exists!\n"
44              << "Use -f command line argument to force output\n";
45         return 1;
46       }
47       Out = new std::ofstream(OutputFilename.c_str());
48     } else {
49       if (InputFilename == "-") {
50         OutputFilename = "-";
51         Out = &cout;
52       } else {
53         std::string IFN = InputFilename;
54         int Len = IFN.length();
55         if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
56           // Source ends in .ll
57           OutputFilename = std::string(IFN.begin(), IFN.end()-3);
58         } else {
59           OutputFilename = IFN;   // Append a .bc to it
60         }
61         OutputFilename += ".bc";
62
63         if (!Force && std::ifstream(OutputFilename.c_str())) {
64           // If force is not specified, make sure not to overwrite a file!
65           cerr << "Error opening '" << OutputFilename << "': File exists!\n"
66                << "Use -f command line argument to force output\n";
67           return 1;
68         }
69
70         Out = new std::ofstream(OutputFilename.c_str());
71         // Make sure that the Out file gets unlink'd from the disk if we get a
72         // SIGINT
73         RemoveFileOnSignal(OutputFilename);
74       }
75     }
76   
77     if (!Out->good()) {
78       cerr << "Error opening " << OutputFilename << "!\n";
79       return 1;
80     }
81    
82     WriteBytecodeToFile(M.get(), *Out);
83   } catch (const ParseException &E) {
84     cerr << E.getMessage() << endl;
85     return 1;
86   }
87
88   if (Out != &cout) delete Out;
89   return 0;
90 }
91