blob: eccabd5d14a4b3b67eb5a49abd5a58435dd42b9e [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"
31#include <fstream>
32#include <iostream>
33#include <memory>
34using namespace llvm;
35
36static cl::opt<std::string>
37InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
38
39static cl::opt<std::string>
40OutputFilename("o", cl::desc("Override output filename"),
41 cl::value_desc("filename"));
42
43static cl::opt<bool>
44Force("f", cl::desc("Overwrite output files"));
45
46static cl::opt<bool>
Devang Patel42e66512008-02-21 01:41:25 +000047DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
48
49static cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
51
52static cl::opt<bool>
53DisableVerify("disable-verify", cl::Hidden,
54 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
55
56int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +000057 // Print a stack trace if we signal out.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere6012df2009-03-06 05:34:10 +000059 PrettyStackTraceProgram X(argc, argv);
Owen Anderson25209b42009-07-01 16:58:40 +000060 LLVMContext Context;
Chris Lattnere6012df2009-03-06 05:34:10 +000061 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
62 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063
64 int exitCode = 0;
65 std::ostream *Out = 0;
66 try {
67 // Parse the file now...
Chris Lattner13c68382009-07-02 22:46:18 +000068 SMDiagnostic Err;
Owen Andersona148fdd2009-07-01 21:22:36 +000069 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename, Err, Context));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 if (M.get() == 0) {
Chris Lattner13c68382009-07-02 22:46:18 +000071 Err.Print(argv[0], errs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 return 1;
73 }
74
75 if (!DisableVerify) {
76 std::string Err;
77 if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
78 cerr << argv[0]
79 << ": assembly parsed, but does not verify as correct!\n";
80 cerr << Err;
81 return 1;
82 }
83 }
84
85 if (DumpAsm) cerr << "Here's the assembly:\n" << *M.get();
86
87 if (OutputFilename != "") { // Specified an output filename?
88 if (OutputFilename != "-") { // Not stdout?
89 if (!Force && std::ifstream(OutputFilename.c_str())) {
90 // If force is not specified, make sure not to overwrite a file!
91 cerr << argv[0] << ": error opening '" << OutputFilename
92 << "': file exists!\n"
93 << "Use -f command line argument to force output\n";
94 return 1;
95 }
96 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
97 std::ios::trunc | std::ios::binary);
98 } else { // Specified stdout
99 // FIXME: cout is not binary!
100 Out = &std::cout;
101 }
102 } else {
103 if (InputFilename == "-") {
104 OutputFilename = "-";
105 Out = &std::cout;
106 } else {
107 std::string IFN = InputFilename;
108 int Len = IFN.length();
109 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
110 // Source ends in .ll
111 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
112 } else {
113 OutputFilename = IFN; // Append a .bc to it
114 }
115 OutputFilename += ".bc";
116
117 if (!Force && std::ifstream(OutputFilename.c_str())) {
118 // If force is not specified, make sure not to overwrite a file!
119 cerr << argv[0] << ": error opening '" << OutputFilename
120 << "': file exists!\n"
121 << "Use -f command line argument to force output\n";
122 return 1;
123 }
124
125 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
126 std::ios::trunc | std::ios::binary);
127 // Make sure that the Out file gets unlinked from the disk if we get a
128 // SIGINT
129 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
130 }
131 }
132
133 if (!Out->good()) {
134 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
135 return 1;
136 }
137
Devang Patel42e66512008-02-21 01:41:25 +0000138 if (!DisableOutput)
139 if (Force || !CheckBitcodeOutputToConsole(Out,true))
140 WriteBitcodeToFile(M.get(), *Out);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 } catch (const std::string& msg) {
142 cerr << argv[0] << ": " << msg << "\n";
143 exitCode = 1;
144 } catch (...) {
145 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
146 exitCode = 1;
147 }
148
149 if (Out != &std::cout) delete Out;
150 return exitCode;
151}
152