llvm-rs-as.cpp revision c908b90b45448af6c39ce407b607f46b0e0461d6
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/Analysis/Verifier.h"
20#include "llvm/Assembly/Parser.h"
21#include "llvm/Bitcode/ReaderWriter.h"
22#include "llvm/IR/Module.h"
23#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/ManagedStatic.h"
25#include "llvm/Support/PrettyStackTrace.h"
26#include "llvm/Support/Signals.h"
27#include "llvm/Support/SourceMgr.h"
28#include "llvm/Support/SystemUtils.h"
29#include "llvm/Support/ToolOutputFile.h"
30
31#include "BitWriter_3_2/ReaderWriter_3_2.h"
32#include "BitWriter_2_9/ReaderWriter_2_9.h"
33#include "BitWriter_2_9_func/ReaderWriter_2_9_func.h"
34
35#include <memory>
36using namespace llvm;
37
38static cl::opt<std::string>
39InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
40
41static cl::opt<std::string>
42OutputFilename("o", cl::desc("Override output filename"),
43               cl::value_desc("filename"));
44
45static cl::opt<bool>
46Force("f", cl::desc("Enable binary output on terminals"));
47
48static cl::opt<bool>
49DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
50
51static cl::opt<bool>
52DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
53
54static cl::opt<bool>
55DisableVerify("disable-verify", cl::Hidden,
56              cl::desc("Do not run verifier on input LLVM (dangerous!)"));
57
58enum BCVersion {
59  BC29, BC29Func, BC32, BCHEAD
60};
61
62cl::opt<BCVersion> BitcodeVersion("bitcode-version",
63  cl::desc("Set the bitcode version to be written:"),
64  cl::values(
65    clEnumValN(BC29, "BC29", "Version 2.9"),
66     clEnumVal(BC29Func,     "Version 2.9 func"),
67     clEnumVal(BC32,         "Version 3.2"),
68     clEnumVal(BCHEAD,       "Most current version"),
69    clEnumValEnd), cl::init(BC32));
70
71static void WriteOutputFile(const Module *M) {
72  // Infer the output filename if needed.
73  if (OutputFilename.empty()) {
74    if (InputFilename == "-") {
75      OutputFilename = "-";
76    } else {
77      std::string IFN = InputFilename;
78      int Len = IFN.length();
79      if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
80        // Source ends in .ll
81        OutputFilename = std::string(IFN.begin(), IFN.end()-3);
82      } else {
83        OutputFilename = IFN;   // Append a .bc to it
84      }
85      OutputFilename += ".bc";
86    }
87  }
88
89  std::string ErrorInfo;
90  OwningPtr<tool_output_file> Out
91  (new tool_output_file(OutputFilename.c_str(), ErrorInfo,
92                        raw_fd_ostream::F_Binary));
93  if (!ErrorInfo.empty()) {
94    errs() << ErrorInfo << '\n';
95    exit(1);
96  }
97
98  if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) {
99    switch(BitcodeVersion) {
100      case BC29:
101        llvm_2_9::WriteBitcodeToFile(M, Out->os());
102        break;
103      case BC29Func:
104        llvm_2_9_func::WriteBitcodeToFile(M, Out->os());
105        break;
106      case BC32:
107        llvm_3_2::WriteBitcodeToFile(M, Out->os());
108        break;
109      case BCHEAD:
110        llvm::WriteBitcodeToFile(M, Out->os());
111        break;
112    }
113  }
114
115  // Declare success.
116  Out->keep();
117}
118
119int main(int argc, char **argv) {
120  // Print a stack trace if we signal out.
121  sys::PrintStackTraceOnErrorSignal();
122  PrettyStackTraceProgram X(argc, argv);
123  LLVMContext &Context = getGlobalContext();
124  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
125  cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
126
127  // Parse the file now...
128  SMDiagnostic Err;
129  OwningPtr<Module> M(ParseAssemblyFile(InputFilename, Err, Context));
130  if (M.get() == 0) {
131    Err.print(argv[0], errs());
132    return 1;
133  }
134
135  if (!DisableVerify) {
136    std::string Err;
137    if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
138      errs() << argv[0]
139             << ": assembly parsed, but does not verify as correct!\n";
140      errs() << Err;
141      return 1;
142    }
143  }
144
145  if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
146
147  if (!DisableOutput)
148    WriteOutputFile(M.get());
149
150  return 0;
151}
152