blob: 100c5c0075d02a6dd3112b720ee1d4a181e33a5e [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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 Anderson25209b42009-07-01 16:58:40 +000018#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#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 Lattnere6012df2009-03-06 05:34:10 +000025#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner13c68382009-07-02 22:46:18 +000026#include "llvm/Support/SourceMgr.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Support/Streams.h"
28#include "llvm/Support/SystemUtils.h"
Chris Lattner103ff152009-01-02 07:01:27 +000029#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/System/Signals.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include <memory>
32using namespace llvm;
33
34static cl::opt<std::string>
35InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
36
37static cl::opt<std::string>
38OutputFilename("o", cl::desc("Override output filename"),
39 cl::value_desc("filename"));
40
41static cl::opt<bool>
42Force("f", cl::desc("Overwrite output files"));
43
44static cl::opt<bool>
Devang Patel42e66512008-02-21 01:41:25 +000045DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
46
47static cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
49
50static cl::opt<bool>
51DisableVerify("disable-verify", cl::Hidden,
52 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
53
54int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +000055 // Print a stack trace if we signal out.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere6012df2009-03-06 05:34:10 +000057 PrettyStackTraceProgram X(argc, argv);
Owen Andersone84b8b32009-07-15 22:16:10 +000058 LLVMContext &Context = getGlobalContext();
Chris Lattnere6012df2009-03-06 05:34:10 +000059 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
60 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061
62 int exitCode = 0;
Dan Gohmandd7ca8e2009-07-15 17:29:42 +000063 raw_ostream *Out = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 try {
65 // Parse the file now...
Chris Lattner13c68382009-07-02 22:46:18 +000066 SMDiagnostic Err;
Owen Andersona148fdd2009-07-01 21:22:36 +000067 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename, Err, Context));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 if (M.get() == 0) {
Chris Lattner13c68382009-07-02 22:46:18 +000069 Err.Print(argv[0], errs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 return 1;
71 }
72
73 if (!DisableVerify) {
74 std::string Err;
75 if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
76 cerr << argv[0]
77 << ": assembly parsed, but does not verify as correct!\n";
78 cerr << Err;
79 return 1;
80 }
81 }
82
83 if (DumpAsm) cerr << "Here's the assembly:\n" << *M.get();
84
85 if (OutputFilename != "") { // Specified an output filename?
86 if (OutputFilename != "-") { // Not stdout?
Dan Gohmandd7ca8e2009-07-15 17:29:42 +000087 std::string ErrorInfo;
88 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
89 Force, ErrorInfo);
90 if (!ErrorInfo.empty()) {
91 errs() << ErrorInfo << '\n';
92 if (!Force)
93 errs() << "Use -f command line argument to force output\n";
94 delete Out;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 return 1;
96 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 } else { // Specified stdout
Dan Gohmandd7ca8e2009-07-15 17:29:42 +000098 // FIXME: outs() is not binary!
99 Out = &outs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 }
101 } else {
102 if (InputFilename == "-") {
103 OutputFilename = "-";
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000104 Out = &outs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 } 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
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000116 std::string ErrorInfo;
117 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
118 Force, ErrorInfo);
119 if (!ErrorInfo.empty()) {
120 errs() << ErrorInfo << '\n';
121 if (!Force)
122 errs() << "Use -f command line argument to force output\n";
123 delete Out;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 return 1;
125 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 // 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
Devang Patel42e66512008-02-21 01:41:25 +0000132 if (!DisableOutput)
133 if (Force || !CheckBitcodeOutputToConsole(Out,true))
134 WriteBitcodeToFile(M.get(), *Out);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 } catch (const std::string& msg) {
136 cerr << argv[0] << ": " << msg << "\n";
137 exitCode = 1;
138 } catch (...) {
139 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
140 exitCode = 1;
141 }
142
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000143 if (Out != &outs()) delete Out;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 return exitCode;
145}
146