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