Remove extraneous space
[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 <iostream.h>
13 #include <fstream.h>
14 #include <string>
15 #include "llvm/Module.h"
16 #include "llvm/Assembly/Parser.h"
17 #include "llvm/Assembly/Writer.h"
18 #include "llvm/Bytecode/Writer.h"
19 #include "llvm/Support/CommandLine.h"
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     Module *C = ParseAssemblyFile(InputFilename);
33     if (C == 0) {
34       cerr << "assembly didn't read correctly.\n";
35       return 1;
36     }
37   
38     if (DumpAsm)
39       cerr << "Here's the assembly:\n" << C;
40
41     if (OutputFilename != "") {   // Specified an output filename?
42       Out = new ofstream(OutputFilename.c_str(), 
43                          (Force ? 0 : ios::noreplace)|ios::out);
44     } else {
45       if (InputFilename == "-") {
46         OutputFilename = "-";
47         Out = &cout;
48       } else {
49         string IFN = InputFilename;
50         int Len = IFN.length();
51         if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
52           // Source ends in .ll
53           OutputFilename = string(IFN.begin(), IFN.end()-3);
54         } else {
55           OutputFilename = IFN;   // Append a .bc to it
56         }
57         OutputFilename += ".bc";
58         Out = new ofstream(OutputFilename.c_str(), 
59                            (Force ? 0 : ios::noreplace)|ios::out);
60       }
61   
62       if (!Out->good()) {
63         cerr << "Error opening " << OutputFilename << "!\n";
64         delete C;
65         return 1;
66       }
67     }
68    
69     WriteBytecodeToFile(C, *Out);
70
71     delete C;
72   } catch (const ParseException &E) {
73     cerr << E.getMessage() << endl;
74     return 1;
75   }
76
77   if (Out != &cout) delete Out;
78   return 0;
79 }
80