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/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"
33 static cl::opt<std::string>
34 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
36 static cl::opt<std::string>
37 OutputFilename("o", cl::desc("Override output filename"),
38 cl::value_desc("filename"));
41 Force("f", cl::desc("Overwrite output files"));
44 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
47 NoCompress("disable-compression", cl::init(true),
48 cl::desc("Don't compress the generated bytecode"));
51 DisableVerify("disable-verify", cl::Hidden,
52 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
55 EnableBitcode("bitcode", cl::desc("Emit bitcode"));
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();
64 std::ostream *Out = 0;
66 // Parse the file now...
68 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename,&Err));
70 cerr << argv[0] << ": " << Err.getMessage() << "\n";
76 if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
78 << ": assembly parsed, but does not verify as correct!\n";
84 if (DumpAsm) cerr << "Here's the assembly:\n" << *M.get();
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";
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!
102 if (InputFilename == "-") {
103 OutputFilename = "-";
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);
112 OutputFilename = IFN; // Append a .bc to it
114 OutputFilename += ".bc";
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";
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
128 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
133 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
137 if (Force || !CheckBytecodeOutputToConsole(Out,true)) {
139 WriteBitcodeToFile(M.get(), *Out);
142 WriteBytecodeToFile(M.get(), L, !NoCompress);
145 } catch (const std::string& msg) {
146 cerr << argv[0] << ": " << msg << "\n";
149 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
153 if (Out != &std::cout) delete Out;