fit in 80 cols
[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 "llvm/Bitcode/ReaderWriter.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/Streams.h"
26 #include "llvm/Support/SystemUtils.h"
27 #include "llvm/System/Signals.h"
28 #include <fstream>
29 #include <iostream>
30 #include <memory>
31 using namespace llvm;
32
33 static cl::opt<std::string>
34 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
35
36 static cl::opt<std::string>
37 OutputFilename("o", cl::desc("Override output filename"),
38                cl::value_desc("filename"));
39
40 static cl::opt<bool>
41 Force("f", cl::desc("Overwrite output files"));
42
43 static cl::opt<bool>
44 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
45
46 static cl::opt<bool>
47 NoCompress("disable-compression", cl::init(true),
48            cl::desc("Don't compress the generated bytecode"));
49
50 static cl::opt<bool>
51 DisableVerify("disable-verify", cl::Hidden,
52               cl::desc("Do not run verifier on input LLVM (dangerous!)"));
53
54 static cl::opt<bool>
55 EnableBitcode("bitcode", cl::desc("Emit bitcode"));
56
57
58 int main(int argc, char **argv) {
59   llvm_shutdown_obj X;  // Call llvm_shutdown() on exit.
60   cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
61   sys::PrintStackTraceOnErrorSignal();
62
63   int exitCode = 0;
64   std::ostream *Out = 0;
65   try {
66     // Parse the file now...
67     ParseError Err;
68     std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename,&Err));
69     if (M.get() == 0) {
70       cerr << argv[0] << ": " << Err.getMessage() << "\n"; 
71       return 1;
72     }
73
74     if (!DisableVerify) {
75       std::string Err;
76       if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
77         cerr << argv[0]
78              << ": assembly parsed, but does not verify as correct!\n";
79         cerr << Err;
80         return 1;
81       } 
82     }
83
84     if (DumpAsm) cerr << "Here's the assembly:\n" << *M.get();
85
86     if (OutputFilename != "") {   // Specified an output filename?
87       if (OutputFilename != "-") {  // Not stdout?
88         if (!Force && std::ifstream(OutputFilename.c_str())) {
89           // If force is not specified, make sure not to overwrite a file!
90           cerr << argv[0] << ": error opening '" << OutputFilename
91                << "': file exists!\n"
92                << "Use -f command line argument to force output\n";
93           return 1;
94         }
95         Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
96                                 std::ios::trunc | std::ios::binary);
97       } else {                      // Specified stdout
98         // FIXME: cout is not binary!
99         Out = &std::cout;
100       }
101     } else {
102       if (InputFilename == "-") {
103         OutputFilename = "-";
104         Out = &std::cout;
105       } else {
106         std::string IFN = InputFilename;
107         int Len = IFN.length();
108         if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
109           // Source ends in .ll
110           OutputFilename = std::string(IFN.begin(), IFN.end()-3);
111         } else {
112           OutputFilename = IFN;   // Append a .bc to it
113         }
114         OutputFilename += ".bc";
115
116         if (!Force && std::ifstream(OutputFilename.c_str())) {
117           // If force is not specified, make sure not to overwrite a file!
118           cerr << argv[0] << ": error opening '" << OutputFilename
119                << "': file exists!\n"
120                << "Use -f command line argument to force output\n";
121           return 1;
122         }
123
124         Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
125                                 std::ios::trunc | std::ios::binary);
126         // Make sure that the Out file gets unlinked from the disk if we get a
127         // SIGINT
128         sys::RemoveFileOnSignal(sys::Path(OutputFilename));
129       }
130     }
131
132     if (!Out->good()) {
133       cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
134       return 1;
135     }
136
137     if (Force || !CheckBytecodeOutputToConsole(Out,true)) {
138       if (EnableBitcode) {
139         WriteBitcodeToFile(M.get(), *Out);
140       } else {
141         OStream L(*Out);
142         WriteBytecodeToFile(M.get(), L, !NoCompress);
143       }
144     }
145   } catch (const std::string& msg) {
146     cerr << argv[0] << ": " << msg << "\n";
147     exitCode = 1;
148   } catch (...) {
149     cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
150     exitCode = 1;
151   }
152
153   if (Out != &std::cout) delete Out;
154   return exitCode;
155 }
156