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 bitcode to stdout
13 // llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bitcode
16 //===----------------------------------------------------------------------===//
18 #include "llvm/Module.h"
19 #include "llvm/Assembly/Parser.h"
20 #include "llvm/Analysis/Verifier.h"
21 #include "llvm/Bitcode/ReaderWriter.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 DisableVerify("disable-verify", cl::Hidden,
47 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
49 int main(int argc, char **argv) {
50 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
51 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
52 sys::PrintStackTraceOnErrorSignal();
55 std::ostream *Out = 0;
57 // Parse the file now...
59 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename,&Err));
61 cerr << argv[0] << ": " << Err.getMessage() << "\n";
67 if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
69 << ": assembly parsed, but does not verify as correct!\n";
75 if (DumpAsm) cerr << "Here's the assembly:\n" << *M.get();
77 if (OutputFilename != "") { // Specified an output filename?
78 if (OutputFilename != "-") { // Not stdout?
79 if (!Force && std::ifstream(OutputFilename.c_str())) {
80 // If force is not specified, make sure not to overwrite a file!
81 cerr << argv[0] << ": error opening '" << OutputFilename
82 << "': file exists!\n"
83 << "Use -f command line argument to force output\n";
86 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
87 std::ios::trunc | std::ios::binary);
88 } else { // Specified stdout
89 // FIXME: cout is not binary!
93 if (InputFilename == "-") {
97 std::string IFN = InputFilename;
98 int Len = IFN.length();
99 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
100 // Source ends in .ll
101 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
103 OutputFilename = IFN; // Append a .bc to it
105 OutputFilename += ".bc";
107 if (!Force && std::ifstream(OutputFilename.c_str())) {
108 // If force is not specified, make sure not to overwrite a file!
109 cerr << argv[0] << ": error opening '" << OutputFilename
110 << "': file exists!\n"
111 << "Use -f command line argument to force output\n";
115 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
116 std::ios::trunc | std::ios::binary);
117 // Make sure that the Out file gets unlinked from the disk if we get a
119 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
124 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
128 if (Force || !CheckBitcodeOutputToConsole(Out,true))
129 WriteBitcodeToFile(M.get(), *Out);
130 } catch (const std::string& msg) {
131 cerr << argv[0] << ": " << msg << "\n";
134 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
138 if (Out != &std::cout) delete Out;