Commit r331416 breaks the big-endian PPC bot. On the big endian build, we
[llvm-core.git] / tools / llvm-as / llvm-as.cpp
blob258f5141e11813bea5074e92766b63aa2471e567
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.
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"
30 #include <memory>
31 using namespace llvm;
33 static cl::opt<std::string> InputFilename(cl::Positional,
34 cl::desc("<input .llvm file>"),
35 cl::init("-"));
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"),
44 cl::init(false));
46 static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
47 cl::init(false));
49 static cl::opt<bool> DumpAsm("d", cl::desc("Print assembly as parsed"),
50 cl::Hidden);
52 static cl::opt<bool>
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"),
64 cl::init(""));
66 static void WriteOutputFile(const Module *M) {
67 // Infer the output filename if needed.
68 if (OutputFilename.empty()) {
69 if (InputFilename == "-") {
70 OutputFilename = "-";
71 } else {
72 StringRef IFN = InputFilename;
73 OutputFilename = (IFN.endswith(".ll") ? IFN.drop_back(3) : IFN).str();
74 OutputFilename += ".bc";
78 std::error_code EC;
79 std::unique_ptr<ToolOutputFile> Out(
80 new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
81 if (EC) {
82 errs() << EC.message() << '\n';
83 exit(1);
86 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
87 WriteBitcodeToFile(*M, Out->os(), PreserveBitcodeUseListOrder, nullptr,
88 EmitModuleHash);
90 // Declare success.
91 Out->keep();
94 int main(int argc, char **argv) {
95 InitLLVM X(argc, argv);
96 LLVMContext Context;
97 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
99 // Parse the file now...
100 SMDiagnostic Err;
101 std::unique_ptr<Module> M = parseAssemblyFile(
102 InputFilename, Err, Context, nullptr, !DisableVerify, ClDataLayout);
103 if (!M.get()) {
104 Err.print(argv[0], errs());
105 return 1;
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;
119 if (DumpAsm)
120 errs() << "Here's the assembly:\n" << *M.get();
122 if (!DisableOutput)
123 WriteOutputFile(M.get());
125 return 0;