blob: 9e613956e1f766965bd3efb10e8a52096fe2c2ea [file] [log] [blame]
Tobias Grosserc908b902013-06-21 11:28:49 -07001//===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// 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
Tim Murrayee4016d2014-04-10 15:49:08 -070018#include "llvm/ADT/OwningPtr.h"
Tobias Grosserc908b902013-06-21 11:28:49 -070019#include "llvm/IR/LLVMContext.h"
Tim Murrayee4016d2014-04-10 15:49:08 -070020#include "llvm/IR/Verifier.h"
21#include "llvm/AsmParser/Parser.h"
Tobias Grosserc908b902013-06-21 11:28:49 -070022#include "llvm/Bitcode/ReaderWriter.h"
23#include "llvm/IR/Module.h"
24#include "llvm/Support/CommandLine.h"
Stephen Hines75d47182014-05-28 09:43:18 -070025#include "llvm/Support/FileSystem.h"
Tobias Grosserc908b902013-06-21 11:28:49 -070026#include "llvm/Support/ManagedStatic.h"
27#include "llvm/Support/PrettyStackTrace.h"
28#include "llvm/Support/Signals.h"
29#include "llvm/Support/SourceMgr.h"
30#include "llvm/Support/SystemUtils.h"
31#include "llvm/Support/ToolOutputFile.h"
32
33#include "BitWriter_3_2/ReaderWriter_3_2.h"
34#include "BitWriter_2_9/ReaderWriter_2_9.h"
35#include "BitWriter_2_9_func/ReaderWriter_2_9_func.h"
36
37#include <memory>
38using namespace llvm;
39
40static cl::opt<std::string>
41InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
42
43static cl::opt<std::string>
44OutputFilename("o", cl::desc("Override output filename"),
45 cl::value_desc("filename"));
46
47static cl::opt<bool>
48Force("f", cl::desc("Enable binary output on terminals"));
49
50static cl::opt<bool>
51DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
52
53static cl::opt<bool>
54DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
55
56static cl::opt<bool>
57DisableVerify("disable-verify", cl::Hidden,
58 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
59
60enum BCVersion {
61 BC29, BC29Func, BC32, BCHEAD
62};
63
64cl::opt<BCVersion> BitcodeVersion("bitcode-version",
65 cl::desc("Set the bitcode version to be written:"),
66 cl::values(
67 clEnumValN(BC29, "BC29", "Version 2.9"),
68 clEnumVal(BC29Func, "Version 2.9 func"),
69 clEnumVal(BC32, "Version 3.2"),
70 clEnumVal(BCHEAD, "Most current version"),
71 clEnumValEnd), cl::init(BC32));
72
73static void WriteOutputFile(const Module *M) {
74 // Infer the output filename if needed.
75 if (OutputFilename.empty()) {
76 if (InputFilename == "-") {
77 OutputFilename = "-";
78 } else {
79 std::string IFN = InputFilename;
80 int Len = IFN.length();
81 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
82 // Source ends in .ll
83 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
84 } else {
85 OutputFilename = IFN; // Append a .bc to it
86 }
87 OutputFilename += ".bc";
88 }
89 }
90
91 std::string ErrorInfo;
92 OwningPtr<tool_output_file> Out
93 (new tool_output_file(OutputFilename.c_str(), ErrorInfo,
Tim Murrayee4016d2014-04-10 15:49:08 -070094 llvm::sys::fs::F_None));
Tobias Grosserc908b902013-06-21 11:28:49 -070095 if (!ErrorInfo.empty()) {
96 errs() << ErrorInfo << '\n';
97 exit(1);
98 }
99
100 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) {
101 switch(BitcodeVersion) {
102 case BC29:
103 llvm_2_9::WriteBitcodeToFile(M, Out->os());
104 break;
105 case BC29Func:
106 llvm_2_9_func::WriteBitcodeToFile(M, Out->os());
107 break;
108 case BC32:
109 llvm_3_2::WriteBitcodeToFile(M, Out->os());
110 break;
111 case BCHEAD:
112 llvm::WriteBitcodeToFile(M, Out->os());
113 break;
114 }
115 }
116
117 // Declare success.
118 Out->keep();
119}
120
121int main(int argc, char **argv) {
122 // Print a stack trace if we signal out.
123 sys::PrintStackTraceOnErrorSignal();
124 PrettyStackTraceProgram X(argc, argv);
125 LLVMContext &Context = getGlobalContext();
126 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
127 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
128
129 // Parse the file now...
130 SMDiagnostic Err;
131 OwningPtr<Module> M(ParseAssemblyFile(InputFilename, Err, Context));
132 if (M.get() == 0) {
133 Err.print(argv[0], errs());
134 return 1;
135 }
136
137 if (!DisableVerify) {
138 std::string Err;
Tim Murrayee4016d2014-04-10 15:49:08 -0700139 raw_string_ostream stream(Err);
140 if (verifyModule(*M.get(), &stream)) {
Tobias Grosserc908b902013-06-21 11:28:49 -0700141 errs() << argv[0]
142 << ": assembly parsed, but does not verify as correct!\n";
143 errs() << Err;
144 return 1;
145 }
146 }
147
148 if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
149
150 if (!DisableOutput)
151 WriteOutputFile(M.get());
152
153 return 0;
154}