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