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