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