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