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/SystemUtils.h"
24 #include "llvm/System/Signals.h"
31 static cl::opt<std::string>
32 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
34 static cl::opt<std::string>
35 OutputFilename("o", cl::desc("Override output filename"),
36 cl::value_desc("filename"));
39 Force("f", cl::desc("Overwrite output files"));
42 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
45 NoCompress("disable-compression", cl::init(false),
46 cl::desc("Don't compress the generated bytecode"));
49 DisableVerify("disable-verify", cl::Hidden,
50 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
52 int main(int argc, char **argv) {
53 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
54 sys::PrintStackTraceOnErrorSignal();
57 std::ostream *Out = 0;
59 // Parse the file now...
60 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
62 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
68 verifyModule(*M.get(), ThrowExceptionAction);
69 } catch (const std::string &Err) {
71 << ": assembly parsed, but does not verify as correct!\n";
76 if (DumpAsm) std::cerr << "Here's the assembly:\n" << *M.get();
78 if (OutputFilename != "") { // Specified an output filename?
79 if (OutputFilename != "-") { // Not stdout?
80 if (!Force && std::ifstream(OutputFilename.c_str())) {
81 // If force is not specified, make sure not to overwrite a file!
82 std::cerr << argv[0] << ": error opening '" << OutputFilename
83 << "': file exists!\n"
84 << "Use -f command line argument to force output\n";
87 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
88 std::ios::trunc | std::ios::binary);
89 } else { // Specified stdout
90 // FIXME: cout is not binary!
94 if (InputFilename == "-") {
98 std::string IFN = InputFilename;
99 int Len = IFN.length();
100 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
101 // Source ends in .ll
102 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
104 OutputFilename = IFN; // Append a .bc to it
106 OutputFilename += ".bc";
108 if (!Force && std::ifstream(OutputFilename.c_str())) {
109 // If force is not specified, make sure not to overwrite a file!
110 std::cerr << argv[0] << ": error opening '" << OutputFilename
111 << "': file exists!\n"
112 << "Use -f command line argument to force output\n";
116 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
117 std::ios::trunc | std::ios::binary);
118 // Make sure that the Out file gets unlinked from the disk if we get a
120 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
125 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
129 if (Force || !CheckBytecodeOutputToConsole(Out,true)) {
130 WriteBytecodeToFile(M.get(), *Out, !NoCompress);
132 } catch (const ParseException &E) {
133 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
135 } catch (const std::string& msg) {
136 std::cerr << argv[0] << ": " << msg << "\n";
139 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
143 if (Out != &std::cout) delete Out;