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