*** empty log message ***
[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 using std::cerr;
20
21 static cl::opt<string> 
22 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
23
24 static cl::opt<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       cerr << "assembly didn't read correctly.\n";
43       return 1;
44     }
45   
46     if (DumpAsm) cerr << "Here's the assembly:\n" << M.get();
47
48     if (OutputFilename != "") {   // Specified an output filename?
49       if (!Force && std::ifstream(OutputFilename.c_str())) {
50         // If force is not specified, make sure not to overwrite a file!
51         cerr << "Error opening '" << OutputFilename << "': File exists!\n"
52              << "Use -f command line argument to force output\n";
53         return 1;
54       }
55       Out = new std::ofstream(OutputFilename.c_str());
56     } else {
57       if (InputFilename == "-") {
58         OutputFilename = "-";
59         Out = &std::cout;
60       } else {
61         std::string IFN = InputFilename;
62         int Len = IFN.length();
63         if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
64           // Source ends in .ll
65           OutputFilename = std::string(IFN.begin(), IFN.end()-3);
66         } else {
67           OutputFilename = IFN;   // Append a .bc to it
68         }
69         OutputFilename += ".bc";
70
71         if (!Force && std::ifstream(OutputFilename.c_str())) {
72           // If force is not specified, make sure not to overwrite a file!
73           cerr << "Error opening '" << OutputFilename << "': File exists!\n"
74                << "Use -f command line argument to force output\n";
75           return 1;
76         }
77
78         Out = new std::ofstream(OutputFilename.c_str());
79         // Make sure that the Out file gets unlink'd from the disk if we get a
80         // SIGINT
81         RemoveFileOnSignal(OutputFilename);
82       }
83     }
84   
85     if (!Out->good()) {
86       cerr << "Error opening " << OutputFilename << "!\n";
87       return 1;
88     }
89    
90     WriteBytecodeToFile(M.get(), *Out);
91   } catch (const ParseException &E) {
92     cerr << E.getMessage() << std::endl;
93     return 1;
94   }
95
96   if (Out != &std::cout) delete Out;
97   return 0;
98 }
99