1 //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
16 //===----------------------------------------------------------------------===//
18 #include "llvm/LLVMContext.h"
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"
25 #include "llvm/Support/PrettyStackTrace.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Support/SystemUtils.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/System/Signals.h"
33 static cl::opt
<std::string
>
34 InputFilename(cl::Positional
, cl::desc("<input .llvm file>"), cl::init("-"));
36 static cl::opt
<std::string
>
37 OutputFilename("o", cl::desc("Override output filename"),
38 cl::value_desc("filename"));
41 Force("f", cl::desc("Overwrite output files"));
44 DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
47 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden
);
50 DisableVerify("disable-verify", cl::Hidden
,
51 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
53 int main(int argc
, char **argv
) {
54 // Print a stack trace if we signal out.
55 sys::PrintStackTraceOnErrorSignal();
56 PrettyStackTraceProgram
X(argc
, argv
);
57 LLVMContext
&Context
= getGlobalContext();
58 llvm_shutdown_obj Y
; // Call llvm_shutdown() on exit.
59 cl::ParseCommandLineOptions(argc
, argv
, "llvm .ll -> .bc assembler\n");
64 // Parse the file now...
66 std::auto_ptr
<Module
> M(ParseAssemblyFile(InputFilename
, Err
, Context
));
68 Err
.Print(argv
[0], errs());
74 if (verifyModule(*M
.get(), ReturnStatusAction
, &Err
)) {
76 << ": assembly parsed, but does not verify as correct!\n";
82 if (DumpAsm
) errs() << "Here's the assembly:\n" << *M
.get();
84 if (OutputFilename
!= "") { // Specified an output filename?
85 if (OutputFilename
!= "-") { // Not stdout?
86 std::string ErrorInfo
;
87 Out
= new raw_fd_ostream(OutputFilename
.c_str(), /*Binary=*/true,
89 if (!ErrorInfo
.empty()) {
90 errs() << ErrorInfo
<< '\n';
92 errs() << "Use -f command line argument to force output\n";
96 } else { // Specified stdout
97 // FIXME: outs() is not binary!
101 if (InputFilename
== "-") {
102 OutputFilename
= "-";
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);
111 OutputFilename
= IFN
; // Append a .bc to it
113 OutputFilename
+= ".bc";
115 std::string ErrorInfo
;
116 Out
= new raw_fd_ostream(OutputFilename
.c_str(), /*Binary=*/true,
118 if (!ErrorInfo
.empty()) {
119 errs() << ErrorInfo
<< '\n';
121 errs() << "Use -f command line argument to force output\n";
125 // Make sure that the Out file gets unlinked from the disk if we get a
127 sys::RemoveFileOnSignal(sys::Path(OutputFilename
));
132 if (Force
|| !CheckBitcodeOutputToConsole(Out
,true))
133 WriteBitcodeToFile(M
.get(), *Out
);
134 } catch (const std::string
& msg
) {
135 errs() << argv
[0] << ": " << msg
<< "\n";
138 errs() << argv
[0] << ": Unexpected unknown exception occurred.\n";
142 if (Out
!= &outs()) delete Out
;