1 //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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
16 //===----------------------------------------------------------------------===//
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/Support/CommandLine.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/Streams.h"
25 #include "llvm/Support/SystemUtils.h"
26 #include "llvm/System/Signals.h"
32 static cl::opt<std::string>
33 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
35 static cl::opt<std::string>
36 OutputFilename("o", cl::desc("Override output filename"),
37 cl::value_desc("filename"));
40 Force("f", cl::desc("Overwrite output files"));
43 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
46 NoCompress("disable-compression", cl::init(true),
47 cl::desc("Don't compress the generated bytecode"));
50 DisableVerify("disable-verify", cl::Hidden,
51 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
53 int main(int argc, char **argv) {
54 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
55 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
56 sys::PrintStackTraceOnErrorSignal();
59 std::ostream *Out = 0;
61 // Parse the file now...
63 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename,&Err));
65 cerr << argv[0] << ": " << Err.getMessage() << "\n";
71 if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
73 << ": assembly parsed, but does not verify as correct!\n";
79 if (DumpAsm) cerr << "Here's the assembly:\n" << *M.get();
81 if (OutputFilename != "") { // Specified an output filename?
82 if (OutputFilename != "-") { // Not stdout?
83 if (!Force && std::ifstream(OutputFilename.c_str())) {
84 // If force is not specified, make sure not to overwrite a file!
85 cerr << argv[0] << ": error opening '" << OutputFilename
86 << "': file exists!\n"
87 << "Use -f command line argument to force output\n";
90 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
91 std::ios::trunc | std::ios::binary);
92 } else { // Specified stdout
93 // FIXME: cout is not binary!
97 if (InputFilename == "-") {
101 std::string IFN = InputFilename;
102 int Len = IFN.length();
103 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
104 // Source ends in .ll
105 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
107 OutputFilename = IFN; // Append a .bc to it
109 OutputFilename += ".bc";
111 if (!Force && std::ifstream(OutputFilename.c_str())) {
112 // If force is not specified, make sure not to overwrite a file!
113 cerr << argv[0] << ": error opening '" << OutputFilename
114 << "': file exists!\n"
115 << "Use -f command line argument to force output\n";
119 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
120 std::ios::trunc | std::ios::binary);
121 // Make sure that the Out file gets unlinked from the disk if we get a
123 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
128 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
132 if (Force || !CheckBytecodeOutputToConsole(Out,true)) {
134 WriteBytecodeToFile(M.get(), L, !NoCompress);
136 } catch (const std::string& msg) {
137 cerr << argv[0] << ": " << msg << "\n";
140 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
144 if (Out != &std::cout) delete Out;