blob: d9fa1fa7f40a721c15926da5622c01c88110183c [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
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>
Devang Patel42e66512008-02-21 01:41:25 +000043DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
44
45static cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
47
48static cl::opt<bool>
49DisableVerify("disable-verify", cl::Hidden,
50 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
51
52int main(int argc, char **argv) {
53 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Dan Gohman6099df82007-10-08 15:45:12 +000054 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 sys::PrintStackTraceOnErrorSignal();
56
57 int exitCode = 0;
58 std::ostream *Out = 0;
59 try {
60 // Parse the file now...
61 ParseError Err;
62 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename,&Err));
63 if (M.get() == 0) {
64 cerr << argv[0] << ": " << Err.getMessage() << "\n";
65 return 1;
66 }
67
68 if (!DisableVerify) {
69 std::string Err;
70 if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
71 cerr << argv[0]
72 << ": assembly parsed, but does not verify as correct!\n";
73 cerr << Err;
74 return 1;
75 }
76 }
77
78 if (DumpAsm) cerr << "Here's the assembly:\n" << *M.get();
79
80 if (OutputFilename != "") { // Specified an output filename?
81 if (OutputFilename != "-") { // Not stdout?
82 if (!Force && std::ifstream(OutputFilename.c_str())) {
83 // If force is not specified, make sure not to overwrite a file!
84 cerr << argv[0] << ": error opening '" << OutputFilename
85 << "': file exists!\n"
86 << "Use -f command line argument to force output\n";
87 return 1;
88 }
89 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
90 std::ios::trunc | std::ios::binary);
91 } else { // Specified stdout
92 // FIXME: cout is not binary!
93 Out = &std::cout;
94 }
95 } else {
96 if (InputFilename == "-") {
97 OutputFilename = "-";
98 Out = &std::cout;
99 } else {
100 std::string IFN = InputFilename;
101 int Len = IFN.length();
102 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
103 // Source ends in .ll
104 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
105 } else {
106 OutputFilename = IFN; // Append a .bc to it
107 }
108 OutputFilename += ".bc";
109
110 if (!Force && std::ifstream(OutputFilename.c_str())) {
111 // If force is not specified, make sure not to overwrite a file!
112 cerr << argv[0] << ": error opening '" << OutputFilename
113 << "': file exists!\n"
114 << "Use -f command line argument to force output\n";
115 return 1;
116 }
117
118 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
119 std::ios::trunc | std::ios::binary);
120 // Make sure that the Out file gets unlinked from the disk if we get a
121 // SIGINT
122 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
123 }
124 }
125
126 if (!Out->good()) {
127 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
128 return 1;
129 }
130
Devang Patel42e66512008-02-21 01:41:25 +0000131 if (!DisableOutput)
132 if (Force || !CheckBitcodeOutputToConsole(Out,true))
133 WriteBitcodeToFile(M.get(), *Out);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 } catch (const std::string& msg) {
135 cerr << argv[0] << ": " << msg << "\n";
136 exitCode = 1;
137 } catch (...) {
138 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
139 exitCode = 1;
140 }
141
142 if (Out != &std::cout) delete Out;
143 return exitCode;
144}
145