Chris Lattner | 10cf8fa | 2003-10-20 17:52:11 +0000 | [diff] [blame] | 1 | //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This utility may be invoked in the following manner: |
Misha Brukman | cbb62dd | 2003-08-28 21:32:57 +0000 | [diff] [blame] | 11 | // llvm-as --help - Output information about command line switches |
Chris Lattner | 44dadff | 2007-05-06 09:29:57 +0000 | [diff] [blame] | 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 |
Misha Brukman | cbb62dd | 2003-08-28 21:32:57 +0000 | [diff] [blame] | 14 | // to the x.bc file. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 15 | // |
Chris Lattner | 6d1727c | 2006-05-29 18:52:52 +0000 | [diff] [blame] | 16 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 17 | |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 18 | #include "llvm/LLVMContext.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
| 20 | #include "llvm/Assembly/Parser.h" |
Chris Lattner | 1acbea1 | 2002-08-30 22:54:41 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | 27bb3af | 2007-04-22 06:28:58 +0000 | [diff] [blame] | 22 | #include "llvm/Bitcode/ReaderWriter.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 25 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | 92bcb42 | 2009-07-02 22:46:18 +0000 | [diff] [blame] | 26 | #include "llvm/Support/SourceMgr.h" |
Reid Spencer | b687c0c | 2005-01-02 00:08:46 +0000 | [diff] [blame] | 27 | #include "llvm/Support/SystemUtils.h" |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | bed85ff | 2004-05-27 05:41:36 +0000 | [diff] [blame] | 29 | #include "llvm/System/Signals.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 30 | #include <memory> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 33 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 34 | InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-")); |
| 35 | |
Chris Lattner | 6c8103f | 2003-05-22 20:13:16 +0000 | [diff] [blame] | 36 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 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> |
Devang Patel | ef00f9d | 2008-02-21 01:41:25 +0000 | [diff] [blame] | 44 | DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false)); |
| 45 | |
| 46 | static cl::opt<bool> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 47 | DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 48 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 49 | static cl::opt<bool> |
Chris Lattner | 04aa29d | 2003-08-18 20:47:13 +0000 | [diff] [blame] | 50 | DisableVerify("disable-verify", cl::Hidden, |
Reid Spencer | 0d88616 | 2004-07-11 23:20:54 +0000 | [diff] [blame] | 51 | cl::desc("Do not run verifier on input LLVM (dangerous!)")); |
Chris Lattner | 04aa29d | 2003-08-18 20:47:13 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 53 | int main(int argc, char **argv) { |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 54 | // Print a stack trace if we signal out. |
Reid Spencer | 9de7b33 | 2004-08-29 19:28:55 +0000 | [diff] [blame] | 55 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 56 | PrettyStackTraceProgram X(argc, argv); |
Owen Anderson | 0d7c695 | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 57 | LLVMContext &Context = getGlobalContext(); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 58 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 59 | cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 61 | // Parse the file now... |
| 62 | SMDiagnostic Err; |
| 63 | std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename, Err, Context)); |
| 64 | if (M.get() == 0) { |
| 65 | Err.Print(argv[0], errs()); |
| 66 | return 1; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 69 | if (!DisableVerify) { |
| 70 | std::string Err; |
| 71 | if (verifyModule(*M.get(), ReturnStatusAction, &Err)) { |
| 72 | errs() << argv[0] |
| 73 | << ": assembly parsed, but does not verify as correct!\n"; |
| 74 | errs() << Err; |
| 75 | return 1; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get(); |
| 80 | |
| 81 | // Infer the output filename if needed. |
| 82 | if (OutputFilename.empty()) { |
| 83 | if (InputFilename == "-") { |
| 84 | OutputFilename = "-"; |
| 85 | } else { |
| 86 | std::string IFN = InputFilename; |
| 87 | int Len = IFN.length(); |
| 88 | if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') { |
| 89 | // Source ends in .ll |
| 90 | OutputFilename = std::string(IFN.begin(), IFN.end()-3); |
| 91 | } else { |
| 92 | OutputFilename = IFN; // Append a .bc to it |
| 93 | } |
| 94 | OutputFilename += ".bc"; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | std::string ErrorInfo; |
| 99 | std::auto_ptr<raw_ostream> Out |
| 100 | (new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo, |
| 101 | (Force?raw_fd_ostream::F_Force : 0) | |
| 102 | raw_fd_ostream::F_Binary)); |
| 103 | if (!ErrorInfo.empty()) { |
| 104 | errs() << ErrorInfo << '\n'; |
| 105 | if (!Force) |
| 106 | errs() << "Use -f command line argument to force output\n"; |
| 107 | return 1; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | // Make sure that the Out file gets unlinked from the disk if we get a |
| 112 | // SIGINT. |
| 113 | if (OutputFilename != "-") |
| 114 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 115 | |
| 116 | if (!DisableOutput) |
| 117 | if (Force || !CheckBitcodeOutputToConsole(Out.get(), true)) |
| 118 | WriteBitcodeToFile(M.get(), *Out); |
| 119 | return 0; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 120 | } |
| 121 | |