1 //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
16 //===----------------------------------------------------------------------===//
18 #include "llvm/AsmParser/Parser.h"
19 #include "llvm/Bitcode/BitcodeWriter.h"
20 #include "llvm/IR/LLVMContext.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/InitLLVM.h"
26 #include "llvm/Support/ManagedStatic.h"
27 #include "llvm/Support/SourceMgr.h"
28 #include "llvm/Support/SystemUtils.h"
29 #include "llvm/Support/ToolOutputFile.h"
33 static cl::opt
<std::string
> InputFilename(cl::Positional
,
34 cl::desc("<input .llvm file>"),
37 static cl::opt
<std::string
> OutputFilename("o",
38 cl::desc("Override output filename"),
39 cl::value_desc("filename"));
41 static cl::opt
<bool> Force("f", cl::desc("Enable binary output on terminals"));
43 static cl::opt
<bool> DisableOutput("disable-output", cl::desc("Disable output"),
46 static cl::opt
<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
49 static cl::opt
<bool> DumpAsm("d", cl::desc("Print assembly as parsed"),
53 DisableVerify("disable-verify", cl::Hidden
,
54 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
56 static cl::opt
<bool> PreserveBitcodeUseListOrder(
57 "preserve-bc-uselistorder",
58 cl::desc("Preserve use-list order when writing LLVM bitcode."),
59 cl::init(true), cl::Hidden
);
61 static cl::opt
<std::string
> ClDataLayout("data-layout",
62 cl::desc("data layout string to use"),
63 cl::value_desc("layout-string"),
66 static void WriteOutputFile(const Module
*M
) {
67 // Infer the output filename if needed.
68 if (OutputFilename
.empty()) {
69 if (InputFilename
== "-") {
72 StringRef IFN
= InputFilename
;
73 OutputFilename
= (IFN
.endswith(".ll") ? IFN
.drop_back(3) : IFN
).str();
74 OutputFilename
+= ".bc";
79 std::unique_ptr
<ToolOutputFile
> Out(
80 new ToolOutputFile(OutputFilename
, EC
, sys::fs::F_None
));
82 errs() << EC
.message() << '\n';
86 if (Force
|| !CheckBitcodeOutputToConsole(Out
->os(), true))
87 WriteBitcodeToFile(*M
, Out
->os(), PreserveBitcodeUseListOrder
, nullptr,
94 int main(int argc
, char **argv
) {
95 InitLLVM
X(argc
, argv
);
97 cl::ParseCommandLineOptions(argc
, argv
, "llvm .ll -> .bc assembler\n");
99 // Parse the file now...
101 std::unique_ptr
<Module
> M
= parseAssemblyFile(
102 InputFilename
, Err
, Context
, nullptr, !DisableVerify
, ClDataLayout
);
104 Err
.print(argv
[0], errs());
108 if (!DisableVerify
) {
109 std::string ErrorStr
;
110 raw_string_ostream
OS(ErrorStr
);
111 if (verifyModule(*M
.get(), &OS
)) {
113 << ": assembly parsed, but does not verify as correct!\n";
120 errs() << "Here's the assembly:\n" << *M
.get();
123 WriteOutputFile(M
.get());