Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5f5a573 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 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 |
| 14 | // to the x.bc file. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Owen Anderson | 25209b4 | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 18 | #include "llvm/LLVMContext.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
| 20 | #include "llvm/Assembly/Parser.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" |
Chris Lattner | e6012df | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 25 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | 13c6838 | 2009-07-02 22:46:18 +0000 | [diff] [blame] | 26 | #include "llvm/Support/SourceMgr.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 27 | #include "llvm/Support/SystemUtils.h" |
Chris Lattner | 103ff15 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 29 | #include "llvm/System/Signals.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 30 | #include <memory> |
| 31 | using namespace llvm; |
| 32 | |
| 33 | static cl::opt<std::string> |
| 34 | InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-")); |
| 35 | |
| 36 | static cl::opt<std::string> |
| 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 | 42e6651 | 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> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 47 | DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden); |
| 48 | |
| 49 | static cl::opt<bool> |
| 50 | DisableVerify("disable-verify", cl::Hidden, |
| 51 | cl::desc("Do not run verifier on input LLVM (dangerous!)")); |
| 52 | |
| 53 | int main(int argc, char **argv) { |
Chris Lattner | e6012df | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 54 | // Print a stack trace if we signal out. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 55 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | e6012df | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 56 | PrettyStackTraceProgram X(argc, argv); |
Owen Anderson | e84b8b3 | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 57 | LLVMContext &Context = getGlobalContext(); |
Chris Lattner | e6012df | 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"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 60 | |
| 61 | int exitCode = 0; |
Dan Gohman | dd7ca8e | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 62 | raw_ostream *Out = 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 63 | try { |
| 64 | // Parse the file now... |
Chris Lattner | 13c6838 | 2009-07-02 22:46:18 +0000 | [diff] [blame] | 65 | SMDiagnostic Err; |
Owen Anderson | a148fdd | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 66 | std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename, Err, Context)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 67 | if (M.get() == 0) { |
Chris Lattner | 13c6838 | 2009-07-02 22:46:18 +0000 | [diff] [blame] | 68 | Err.Print(argv[0], errs()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | if (!DisableVerify) { |
| 73 | std::string Err; |
| 74 | if (verifyModule(*M.get(), ReturnStatusAction, &Err)) { |
Dan Gohman | b714fab | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 75 | errs() << argv[0] |
| 76 | << ": assembly parsed, but does not verify as correct!\n"; |
| 77 | errs() << Err; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 78 | return 1; |
| 79 | } |
| 80 | } |
| 81 | |
Dan Gohman | b714fab | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 82 | if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 83 | |
| 84 | if (OutputFilename != "") { // Specified an output filename? |
| 85 | if (OutputFilename != "-") { // Not stdout? |
Dan Gohman | dd7ca8e | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 86 | std::string ErrorInfo; |
| 87 | Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true, |
| 88 | Force, ErrorInfo); |
| 89 | if (!ErrorInfo.empty()) { |
| 90 | errs() << ErrorInfo << '\n'; |
| 91 | if (!Force) |
| 92 | errs() << "Use -f command line argument to force output\n"; |
| 93 | delete Out; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 94 | return 1; |
| 95 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 96 | } else { // Specified stdout |
Dan Gohman | dd7ca8e | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 97 | // FIXME: outs() is not binary! |
| 98 | Out = &outs(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 99 | } |
| 100 | } else { |
| 101 | if (InputFilename == "-") { |
| 102 | OutputFilename = "-"; |
Dan Gohman | dd7ca8e | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 103 | Out = &outs(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 104 | } else { |
| 105 | std::string IFN = InputFilename; |
| 106 | int Len = IFN.length(); |
| 107 | if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') { |
| 108 | // Source ends in .ll |
| 109 | OutputFilename = std::string(IFN.begin(), IFN.end()-3); |
| 110 | } else { |
| 111 | OutputFilename = IFN; // Append a .bc to it |
| 112 | } |
| 113 | OutputFilename += ".bc"; |
| 114 | |
Dan Gohman | dd7ca8e | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 115 | std::string ErrorInfo; |
| 116 | Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true, |
| 117 | Force, ErrorInfo); |
| 118 | if (!ErrorInfo.empty()) { |
| 119 | errs() << ErrorInfo << '\n'; |
| 120 | if (!Force) |
| 121 | errs() << "Use -f command line argument to force output\n"; |
| 122 | delete Out; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 123 | return 1; |
| 124 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 125 | // Make sure that the Out file gets unlinked from the disk if we get a |
| 126 | // SIGINT |
| 127 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 128 | } |
| 129 | } |
| 130 | |
Devang Patel | 42e6651 | 2008-02-21 01:41:25 +0000 | [diff] [blame] | 131 | if (!DisableOutput) |
| 132 | if (Force || !CheckBitcodeOutputToConsole(Out,true)) |
| 133 | WriteBitcodeToFile(M.get(), *Out); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 134 | } catch (const std::string& msg) { |
Dan Gohman | b714fab | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 135 | errs() << argv[0] << ": " << msg << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 136 | exitCode = 1; |
| 137 | } catch (...) { |
Dan Gohman | b714fab | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 138 | errs() << argv[0] << ": Unexpected unknown exception occurred.\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 139 | exitCode = 1; |
| 140 | } |
| 141 | |
Dan Gohman | dd7ca8e | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 142 | if (Out != &outs()) delete Out; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 143 | return exitCode; |
| 144 | } |
| 145 | |