blob: 41b6846fe1cb94ab05f2dfb69590524609288221 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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
18#include "llvm/Module.h"
19#include "llvm/Assembly/Parser.h"
20#include "llvm/Analysis/Verifier.h"
21#include "llvm/Bitcode/ReaderWriter.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/ManagedStatic.h"
24#include "llvm/Support/Streams.h"
25#include "llvm/Support/SystemUtils.h"
26#include "llvm/System/Signals.h"
27#include <fstream>
28#include <iostream>
29#include <memory>
30using namespace llvm;
31
32static cl::opt<std::string>
33InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
34
35static cl::opt<std::string>
36OutputFilename("o", cl::desc("Override output filename"),
37 cl::value_desc("filename"));
38
39static cl::opt<bool>
40Force("f", cl::desc("Overwrite output files"));
41
42static cl::opt<bool>
43DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
44
45static cl::opt<bool>
46DisableVerify("disable-verify", cl::Hidden,
47 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
48
49int main(int argc, char **argv) {
50 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
51 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
52 sys::PrintStackTraceOnErrorSignal();
53
54 int exitCode = 0;
55 std::ostream *Out = 0;
56 try {
57 // Parse the file now...
58 ParseError Err;
59 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename,&Err));
60 if (M.get() == 0) {
61 cerr << argv[0] << ": " << Err.getMessage() << "\n";
62 return 1;
63 }
64
65 if (!DisableVerify) {
66 std::string Err;
67 if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
68 cerr << argv[0]
69 << ": assembly parsed, but does not verify as correct!\n";
70 cerr << Err;
71 return 1;
72 }
73 }
74
75 if (DumpAsm) cerr << "Here's the assembly:\n" << *M.get();
76
77 if (OutputFilename != "") { // Specified an output filename?
78 if (OutputFilename != "-") { // Not stdout?
79 if (!Force && std::ifstream(OutputFilename.c_str())) {
80 // If force is not specified, make sure not to overwrite a file!
81 cerr << argv[0] << ": error opening '" << OutputFilename
82 << "': file exists!\n"
83 << "Use -f command line argument to force output\n";
84 return 1;
85 }
86 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
87 std::ios::trunc | std::ios::binary);
88 } else { // Specified stdout
89 // FIXME: cout is not binary!
90 Out = &std::cout;
91 }
92 } else {
93 if (InputFilename == "-") {
94 OutputFilename = "-";
95 Out = &std::cout;
96 } else {
97 std::string IFN = InputFilename;
98 int Len = IFN.length();
99 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
100 // Source ends in .ll
101 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
102 } else {
103 OutputFilename = IFN; // Append a .bc to it
104 }
105 OutputFilename += ".bc";
106
107 if (!Force && std::ifstream(OutputFilename.c_str())) {
108 // If force is not specified, make sure not to overwrite a file!
109 cerr << argv[0] << ": error opening '" << OutputFilename
110 << "': file exists!\n"
111 << "Use -f command line argument to force output\n";
112 return 1;
113 }
114
115 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
116 std::ios::trunc | std::ios::binary);
117 // Make sure that the Out file gets unlinked from the disk if we get a
118 // SIGINT
119 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
120 }
121 }
122
123 if (!Out->good()) {
124 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
125 return 1;
126 }
127
128 if (Force || !CheckBitcodeOutputToConsole(Out,true))
129 WriteBitcodeToFile(M.get(), *Out);
130 } catch (const std::string& msg) {
131 cerr << argv[0] << ": " << msg << "\n";
132 exitCode = 1;
133 } catch (...) {
134 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
135 exitCode = 1;
136 }
137
138 if (Out != &std::cout) delete Out;
139 return exitCode;
140}
141