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" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Streams.h" |
| 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" |
| 30 | #include <fstream> |
| 31 | #include <iostream> |
| 32 | #include <memory> |
| 33 | using namespace llvm; |
| 34 | |
| 35 | static cl::opt<std::string> |
| 36 | InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-")); |
| 37 | |
| 38 | static cl::opt<std::string> |
| 39 | OutputFilename("o", cl::desc("Override output filename"), |
| 40 | cl::value_desc("filename")); |
| 41 | |
| 42 | static cl::opt<bool> |
| 43 | Force("f", cl::desc("Overwrite output files")); |
| 44 | |
| 45 | static cl::opt<bool> |
Devang Patel | 42e6651 | 2008-02-21 01:41:25 +0000 | [diff] [blame] | 46 | DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false)); |
| 47 | |
| 48 | static cl::opt<bool> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 49 | DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden); |
| 50 | |
| 51 | static cl::opt<bool> |
| 52 | DisableVerify("disable-verify", cl::Hidden, |
| 53 | cl::desc("Do not run verifier on input LLVM (dangerous!)")); |
| 54 | |
| 55 | int main(int argc, char **argv) { |
Chris Lattner | e6012df | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 56 | // Print a stack trace if we signal out. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 57 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | e6012df | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 58 | PrettyStackTraceProgram X(argc, argv); |
Owen Anderson | 25209b4 | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 59 | LLVMContext Context; |
Chris Lattner | e6012df | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 60 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 61 | cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 62 | |
| 63 | int exitCode = 0; |
| 64 | std::ostream *Out = 0; |
| 65 | try { |
| 66 | // Parse the file now... |
| 67 | ParseError Err; |
Owen Anderson | a148fdd | 2009-07-01 21:22:36 +0000 | [diff] [blame^] | 68 | std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename, Err, Context)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | if (M.get() == 0) { |
Chris Lattner | 103ff15 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 70 | Err.PrintError(argv[0], errs()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | if (!DisableVerify) { |
| 75 | std::string Err; |
| 76 | if (verifyModule(*M.get(), ReturnStatusAction, &Err)) { |
| 77 | cerr << argv[0] |
| 78 | << ": assembly parsed, but does not verify as correct!\n"; |
| 79 | cerr << Err; |
| 80 | return 1; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (DumpAsm) cerr << "Here's the assembly:\n" << *M.get(); |
| 85 | |
| 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"; |
| 93 | return 1; |
| 94 | } |
| 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! |
| 99 | Out = &std::cout; |
| 100 | } |
| 101 | } else { |
| 102 | if (InputFilename == "-") { |
| 103 | OutputFilename = "-"; |
| 104 | Out = &std::cout; |
| 105 | } else { |
| 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); |
| 111 | } else { |
| 112 | OutputFilename = IFN; // Append a .bc to it |
| 113 | } |
| 114 | OutputFilename += ".bc"; |
| 115 | |
| 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"; |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 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 |
| 127 | // SIGINT |
| 128 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if (!Out->good()) { |
| 133 | cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; |
| 134 | return 1; |
| 135 | } |
| 136 | |
Devang Patel | 42e6651 | 2008-02-21 01:41:25 +0000 | [diff] [blame] | 137 | if (!DisableOutput) |
| 138 | if (Force || !CheckBitcodeOutputToConsole(Out,true)) |
| 139 | WriteBitcodeToFile(M.get(), *Out); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 140 | } catch (const std::string& msg) { |
| 141 | cerr << argv[0] << ": " << msg << "\n"; |
| 142 | exitCode = 1; |
| 143 | } catch (...) { |
| 144 | cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; |
| 145 | exitCode = 1; |
| 146 | } |
| 147 | |
| 148 | if (Out != &std::cout) delete Out; |
| 149 | return exitCode; |
| 150 | } |
| 151 | |