2003-10-21 01:52:11 +08:00
|
|
|
//===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2003-10-21 01:47:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:44:31 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2003-10-21 01:47:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
|
|
|
// This utility may be invoked in the following manner:
|
2003-08-29 05:32:57 +08:00
|
|
|
// llvm-as --help - Output information about command line switches
|
2007-05-06 17:29:57 +08:00
|
|
|
// llvm-as [options] - Read LLVM asm from stdin, write bitcode to stdout
|
|
|
|
// llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bitcode
|
2003-08-29 05:32:57 +08:00
|
|
|
// to the x.bc file.
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2006-05-30 02:52:52 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2014-01-07 20:34:26 +08:00
|
|
|
#include "llvm/AsmParser/Parser.h"
|
2016-11-11 13:34:58 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2016-04-05 05:06:17 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-04-30 07:26:49 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2006-12-06 09:18:01 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2009-03-06 13:34:10 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2009-07-03 06:46:18 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2005-01-02 08:08:46 +08:00
|
|
|
#include "llvm/Support/SystemUtils.h"
|
2010-10-08 04:32:40 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2002-01-21 06:54:45 +08:00
|
|
|
#include <memory>
|
2003-11-12 06:41:34 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2016-04-05 05:06:17 +08:00
|
|
|
static cl::opt<std::string> InputFilename(cl::Positional,
|
|
|
|
cl::desc("<input .llvm file>"),
|
|
|
|
cl::init("-"));
|
2002-07-22 10:10:13 +08:00
|
|
|
|
2016-04-05 05:06:17 +08:00
|
|
|
static cl::opt<std::string> OutputFilename("o",
|
|
|
|
cl::desc("Override output filename"),
|
|
|
|
cl::value_desc("filename"));
|
2002-07-22 10:10:13 +08:00
|
|
|
|
2016-04-05 05:06:17 +08:00
|
|
|
static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals"));
|
2002-07-22 10:10:13 +08:00
|
|
|
|
2016-04-05 05:06:17 +08:00
|
|
|
static cl::opt<bool> DisableOutput("disable-output", cl::desc("Disable output"),
|
|
|
|
cl::init(false));
|
2008-02-21 09:41:25 +08:00
|
|
|
|
2016-04-01 13:33:11 +08:00
|
|
|
static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
|
|
|
|
cl::init(false));
|
|
|
|
|
2016-04-05 05:06:17 +08:00
|
|
|
static cl::opt<bool> DumpAsm("d", cl::desc("Print assembly as parsed"),
|
|
|
|
cl::Hidden);
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2003-08-19 04:47:13 +08:00
|
|
|
static cl::opt<bool>
|
2016-04-05 05:06:17 +08:00
|
|
|
DisableVerify("disable-verify", cl::Hidden,
|
|
|
|
cl::desc("Do not run verifier on input LLVM (dangerous!)"));
|
2003-08-19 04:47:13 +08:00
|
|
|
|
2015-04-15 11:14:06 +08:00
|
|
|
static cl::opt<bool> PreserveBitcodeUseListOrder(
|
|
|
|
"preserve-bc-uselistorder",
|
|
|
|
cl::desc("Preserve use-list order when writing LLVM bitcode."),
|
|
|
|
cl::init(true), cl::Hidden);
|
|
|
|
|
2009-10-17 11:28:28 +08:00
|
|
|
static void WriteOutputFile(const Module *M) {
|
2009-08-23 10:51:22 +08:00
|
|
|
// Infer the output filename if needed.
|
|
|
|
if (OutputFilename.empty()) {
|
|
|
|
if (InputFilename == "-") {
|
|
|
|
OutputFilename = "-";
|
2001-07-23 10:35:57 +08:00
|
|
|
} else {
|
2015-05-12 05:20:20 +08:00
|
|
|
StringRef IFN = InputFilename;
|
|
|
|
OutputFilename = (IFN.endswith(".ll") ? IFN.drop_back(3) : IFN).str();
|
2009-08-23 10:51:22 +08:00
|
|
|
OutputFilename += ".bc";
|
2002-01-21 06:54:45 +08:00
|
|
|
}
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
2009-09-12 04:46:33 +08:00
|
|
|
|
2014-08-26 02:16:47 +08:00
|
|
|
std::error_code EC;
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<tool_output_file> Out(
|
2014-08-26 02:16:47 +08:00
|
|
|
new tool_output_file(OutputFilename, EC, sys::fs::F_None));
|
|
|
|
if (EC) {
|
|
|
|
errs() << EC.message() << '\n';
|
2009-10-17 11:28:28 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-04-13 05:35:18 +08:00
|
|
|
if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
|
|
|
|
WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder, nullptr,
|
2016-04-11 21:58:45 +08:00
|
|
|
EmitModuleHash);
|
2010-08-20 09:07:01 +08:00
|
|
|
|
|
|
|
// Declare success.
|
|
|
|
Out->keep();
|
2009-10-17 11:28:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
// Print a stack trace if we signal out.
|
2016-06-09 08:53:21 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal(argv[0]);
|
2009-10-17 11:28:28 +08:00
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext Context;
|
2016-04-05 05:06:17 +08:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
2009-10-17 11:28:28 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
|
|
|
|
|
|
|
|
// Parse the file now...
|
|
|
|
SMDiagnostic Err;
|
2014-08-20 00:58:54 +08:00
|
|
|
std::unique_ptr<Module> M = parseAssemblyFile(InputFilename, Err, Context);
|
2014-04-25 12:24:47 +08:00
|
|
|
if (!M.get()) {
|
2011-10-16 12:47:35 +08:00
|
|
|
Err.print(argv[0], errs());
|
2009-08-23 10:51:22 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-10-17 11:28:28 +08:00
|
|
|
if (!DisableVerify) {
|
2014-01-19 10:22:18 +08:00
|
|
|
std::string ErrorStr;
|
|
|
|
raw_string_ostream OS(ErrorStr);
|
|
|
|
if (verifyModule(*M.get(), &OS)) {
|
2009-10-17 11:28:28 +08:00
|
|
|
errs() << argv[0]
|
|
|
|
<< ": assembly parsed, but does not verify as correct!\n";
|
2014-01-19 10:22:18 +08:00
|
|
|
errs() << OS.str();
|
2009-10-17 11:28:28 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 05:06:17 +08:00
|
|
|
if (DumpAsm)
|
|
|
|
errs() << "Here's the assembly:\n" << *M.get();
|
2009-10-17 11:28:28 +08:00
|
|
|
|
2009-08-23 10:51:22 +08:00
|
|
|
if (!DisableOutput)
|
2009-10-17 11:28:28 +08:00
|
|
|
WriteOutputFile(M.get());
|
|
|
|
|
2009-08-23 10:51:22 +08:00
|
|
|
return 0;
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|