blob: 487e904900623ef1fe6d151db563790de791e784 [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/SystemUtils.h"
Chris Lattner103ff152009-01-02 07:01:27 +000028#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/System/Signals.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include <memory>
31using namespace llvm;
32
33static cl::opt<std::string>
34InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
35
36static cl::opt<std::string>
37OutputFilename("o", cl::desc("Override output filename"),
38 cl::value_desc("filename"));
39
40static cl::opt<bool>
41Force("f", cl::desc("Overwrite output files"));
42
43static cl::opt<bool>
Devang Patel42e66512008-02-21 01:41:25 +000044DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
45
46static cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
48
49static cl::opt<bool>
50DisableVerify("disable-verify", cl::Hidden,
51 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
52
53int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +000054 // Print a stack trace if we signal out.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere6012df2009-03-06 05:34:10 +000056 PrettyStackTraceProgram X(argc, argv);
Owen Andersone84b8b32009-07-15 22:16:10 +000057 LLVMContext &Context = getGlobalContext();
Chris Lattnere6012df2009-03-06 05:34:10 +000058 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
59 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060
Chris Lattnerfdcd46e2009-08-23 02:51:22 +000061 // 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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 }
68
Chris Lattnerfdcd46e2009-08-23 02:51:22 +000069 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)
Chris Lattner3a71a1a2009-08-23 21:36:09 +0000117 if (Force || !CheckBitcodeOutputToConsole(*Out, true))
Chris Lattnerfdcd46e2009-08-23 02:51:22 +0000118 WriteBitcodeToFile(M.get(), *Out);
119 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120}
121