1 //===--- 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"
19 #include "llvm/AsmParser/Parser.h"
20 #include "llvm/Bitcode/ReaderWriter.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IR/Verifier.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/FileSystem.h"
25 #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 #include <memory>
32 using namespace llvm;
33
34 static cl::opt<std::string>
35 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
36
37 static cl::opt<std::string>
38 OutputFilename("o", cl::desc("Override output filename"),
39 cl::value_desc("filename"));
40
41 static cl::opt<bool>
42 Force("f", cl::desc("Enable binary output on terminals"));
43
44 static cl::opt<bool>
45 DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
46
47 static cl::opt<bool>
48 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
49
50 static cl::opt<bool>
51 DisableVerify("disable-verify", cl::Hidden,
52 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
53
54 static cl::opt<bool> PreserveBitcodeUseListOrder(
55 "preserve-bc-uselistorder",
56 cl::desc("Preserve use-list order when writing LLVM bitcode."),
57 cl::init(true), cl::Hidden);
58
WriteOutputFile(const Module * M)59 static void WriteOutputFile(const Module *M) {
60 // Infer the output filename if needed.
61 if (OutputFilename.empty()) {
62 if (InputFilename == "-") {
63 OutputFilename = "-";
64 } else {
65 std::string IFN = InputFilename;
66 int Len = IFN.length();
67 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
68 // Source ends in .ll
69 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
70 } else {
71 OutputFilename = IFN; // Append a .bc to it
72 }
73 OutputFilename += ".bc";
74 }
75 }
76
77 std::error_code EC;
78 std::unique_ptr<tool_output_file> Out(
79 new tool_output_file(OutputFilename, EC, sys::fs::F_None));
80 if (EC) {
81 errs() << EC.message() << '\n';
82 exit(1);
83 }
84
85 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
86 WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder);
87
88 // Declare success.
89 Out->keep();
90 }
91
main(int argc,char ** argv)92 int main(int argc, char **argv) {
93 // Print a stack trace if we signal out.
94 sys::PrintStackTraceOnErrorSignal();
95 PrettyStackTraceProgram X(argc, argv);
96 LLVMContext &Context = getGlobalContext();
97 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
98 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
99
100 // Parse the file now...
101 SMDiagnostic Err;
102 std::unique_ptr<Module> M = parseAssemblyFile(InputFilename, Err, Context);
103 if (!M.get()) {
104 Err.print(argv[0], errs());
105 return 1;
106 }
107
108 if (!DisableVerify) {
109 std::string ErrorStr;
110 raw_string_ostream OS(ErrorStr);
111 if (verifyModule(*M.get(), &OS)) {
112 errs() << argv[0]
113 << ": assembly parsed, but does not verify as correct!\n";
114 errs() << OS.str();
115 return 1;
116 }
117 }
118
119 if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
120
121 if (!DisableOutput)
122 WriteOutputFile(M.get());
123
124 return 0;
125 }
126