blob: 53731b6c11e5d77617f5fc37456519398d43c8d8 [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/Support/Streams.h"
27#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"
30#include <fstream>
31#include <iostream>
32#include <memory>
33using namespace llvm;
34
35static cl::opt<std::string>
36InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
37
38static cl::opt<std::string>
39OutputFilename("o", cl::desc("Override output filename"),
40 cl::value_desc("filename"));
41
42static cl::opt<bool>
43Force("f", cl::desc("Overwrite output files"));
44
45static cl::opt<bool>
Devang Patel42e66512008-02-21 01:41:25 +000046DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
47
48static cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
50
51static cl::opt<bool>
52DisableVerify("disable-verify", cl::Hidden,
53 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
54
55int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +000056 // Print a stack trace if we signal out.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere6012df2009-03-06 05:34:10 +000058 PrettyStackTraceProgram X(argc, argv);
Owen Anderson25209b42009-07-01 16:58:40 +000059 LLVMContext Context;
Chris Lattnere6012df2009-03-06 05:34:10 +000060 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
61 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062
63 int exitCode = 0;
64 std::ostream *Out = 0;
65 try {
66 // Parse the file now...
67 ParseError Err;
Owen Andersona148fdd2009-07-01 21:22:36 +000068 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename, Err, Context));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 if (M.get() == 0) {
Chris Lattner103ff152009-01-02 07:01:27 +000070 Err.PrintError(argv[0], errs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 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 Patel42e66512008-02-21 01:41:25 +0000137 if (!DisableOutput)
138 if (Force || !CheckBitcodeOutputToConsole(Out,true))
139 WriteBitcodeToFile(M.get(), *Out);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 } 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