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