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