Fix file header
[oota-llvm.git] / tools / llvm-as / llvm-as.cpp
1 //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 //  This utility may be invoked in the following manner:
11 //   llvm-as --help         - Output information about command line switches
12 //   llvm-as [options]      - Read LLVM asm from stdin, write bytecode to stdout
13 //   llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bytecode
14 //                            to the x.bc file.
15 // 
16 //===------------------------------------------------------------------------===
17
18 #include "llvm/Module.h"
19 #include "llvm/Assembly/Parser.h"
20 #include "llvm/Bytecode/Writer.h"
21 #include "llvm/Analysis/Verifier.h"
22 #include "Support/CommandLine.h"
23 #include "Support/Signals.h"
24 #include <fstream>
25 #include <memory>
26
27 static cl::opt<std::string> 
28 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
29
30 static cl::opt<std::string>
31 OutputFilename("o", cl::desc("Override output filename"),
32                cl::value_desc("filename"));
33
34 static cl::opt<bool>
35 Force("f", cl::desc("Overwrite output files"));
36
37 static cl::opt<bool>
38 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
39
40 static cl::opt<bool>
41 DisableVerify("disable-verify", cl::Hidden,
42               cl::desc("Do not run verifier on input LLVM (dangerous!"));
43
44 int main(int argc, char **argv) {
45   cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
46
47   std::ostream *Out = 0;
48   try {
49     // Parse the file now...
50     std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
51     if (M.get() == 0) {
52       std::cerr << argv[0] << ": assembly didn't read correctly.\n";
53       return 1;
54     }
55
56     if (!DisableVerify && verifyModule(*M.get())) {
57       std::cerr << argv[0]
58                 << ": assembly parsed, but does not verify as correct!\n";
59       return 1;
60     }
61   
62     if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
63
64     if (OutputFilename != "") {   // Specified an output filename?
65       if (OutputFilename != "-") {  // Not stdout?
66         if (!Force && std::ifstream(OutputFilename.c_str())) {
67           // If force is not specified, make sure not to overwrite a file!
68           std::cerr << argv[0] << ": error opening '" << OutputFilename
69                     << "': file exists!\n"
70                     << "Use -f command line argument to force output\n";
71           return 1;
72         }
73         Out = new std::ofstream(OutputFilename.c_str());
74       } else {                      // Specified stdout
75         Out = &std::cout;       
76       }
77     } else {
78       if (InputFilename == "-") {
79         OutputFilename = "-";
80         Out = &std::cout;
81       } else {
82         std::string IFN = InputFilename;
83         int Len = IFN.length();
84         if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
85           // Source ends in .ll
86           OutputFilename = std::string(IFN.begin(), IFN.end()-3);
87         } else {
88           OutputFilename = IFN;   // Append a .bc to it
89         }
90         OutputFilename += ".bc";
91
92         if (!Force && std::ifstream(OutputFilename.c_str())) {
93           // If force is not specified, make sure not to overwrite a file!
94           std::cerr << argv[0] << ": error opening '" << OutputFilename
95                     << "': file exists!\n"
96                     << "Use -f command line argument to force output\n";
97           return 1;
98         }
99
100         Out = new std::ofstream(OutputFilename.c_str());
101         // Make sure that the Out file gets unlinked from the disk if we get a
102         // SIGINT
103         RemoveFileOnSignal(OutputFilename);
104       }
105     }
106   
107     if (!Out->good()) {
108       std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
109       return 1;
110     }
111    
112     WriteBytecodeToFile(M.get(), *Out);
113   } catch (const ParseException &E) {
114     std::cerr << argv[0] << ": " << E.getMessage() << "\n";
115     return 1;
116   }
117
118   if (Out != &std::cout) delete Out;
119   return 0;
120 }
121