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