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