2003-10-20 19:52:11 +02:00
|
|
|
//===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:44:31 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
|
|
|
// This utility may be invoked in the following manner:
|
2003-08-28 23:32:57 +02:00
|
|
|
// llvm-as --help - Output information about command line switches
|
2007-05-06 11:29:57 +02: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 23:32:57 +02:00
|
|
|
// to the x.bc file.
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2006-05-29 20:52:52 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2009-07-01 18:58:40 +02:00
|
|
|
#include "llvm/LLVMContext.h"
|
2001-06-06 22:29:01 +02:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Assembly/Parser.h"
|
2002-08-31 00:54:41 +02:00
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2007-04-22 08:28:58 +02:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-12-06 02:18:01 +01:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2009-03-06 06:34:10 +01:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2009-07-03 00:46:18 +02:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2005-01-02 01:08:46 +01:00
|
|
|
#include "llvm/Support/SystemUtils.h"
|
2009-01-02 08:01:27 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-05-27 07:41:36 +02:00
|
|
|
#include "llvm/System/Signals.h"
|
2002-01-20 23:54:45 +01:00
|
|
|
#include <memory>
|
2003-11-11 23:41:34 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2005-04-22 02:00:37 +02:00
|
|
|
static cl::opt<std::string>
|
2002-07-22 04:10:13 +02:00
|
|
|
InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
|
|
|
|
|
2003-05-22 22:13:16 +02:00
|
|
|
static cl::opt<std::string>
|
2002-07-22 04:10:13 +02:00
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
2009-08-25 17:34:52 +02:00
|
|
|
Force("f", cl::desc("Enable binary output on terminals"));
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2008-02-21 02:41:25 +01:00
|
|
|
static cl::opt<bool>
|
|
|
|
DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
|
|
|
|
|
2002-07-22 04:10:13 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-08-18 22:47:13 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
DisableVerify("disable-verify", cl::Hidden,
|
2004-07-12 01:20:54 +02:00
|
|
|
cl::desc("Do not run verifier on input LLVM (dangerous!)"));
|
2003-08-18 22:47:13 +02:00
|
|
|
|
2009-10-17 05:28:28 +02:00
|
|
|
static void WriteOutputFile(const Module *M) {
|
2009-08-23 04:51:22 +02:00
|
|
|
// Infer the output filename if needed.
|
|
|
|
if (OutputFilename.empty()) {
|
|
|
|
if (InputFilename == "-") {
|
|
|
|
OutputFilename = "-";
|
2001-07-23 04:35:57 +02:00
|
|
|
} else {
|
2009-08-23 04:51:22 +02: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 04:35:57 +02:00
|
|
|
} else {
|
2009-08-23 04:51:22 +02:00
|
|
|
OutputFilename = IFN; // Append a .bc to it
|
2001-07-23 04:35:57 +02:00
|
|
|
}
|
2009-08-23 04:51:22 +02:00
|
|
|
OutputFilename += ".bc";
|
2002-01-20 23:54:45 +01:00
|
|
|
}
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
2009-09-11 22:46:33 +02:00
|
|
|
|
|
|
|
// Make sure that the Out file gets unlinked from the disk if we get a
|
|
|
|
// SIGINT.
|
|
|
|
if (OutputFilename != "-")
|
|
|
|
sys::RemoveFileOnSignal(sys::Path(OutputFilename));
|
|
|
|
|
2009-08-23 04:51:22 +02:00
|
|
|
std::string ErrorInfo;
|
|
|
|
std::auto_ptr<raw_ostream> Out
|
|
|
|
(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
|
|
|
|
raw_fd_ostream::F_Binary));
|
|
|
|
if (!ErrorInfo.empty()) {
|
|
|
|
errs() << ErrorInfo << '\n';
|
2009-10-17 05:28:28 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Force || !CheckBitcodeOutputToConsole(*Out, true))
|
|
|
|
WriteBitcodeToFile(M, *Out);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename, Err, Context));
|
|
|
|
if (M.get() == 0) {
|
|
|
|
Err.Print(argv[0], errs());
|
2009-08-23 04:51:22 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-10-17 05:28:28 +02:00
|
|
|
if (!DisableVerify) {
|
|
|
|
std::string Err;
|
|
|
|
if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
|
|
|
|
errs() << argv[0]
|
|
|
|
<< ": assembly parsed, but does not verify as correct!\n";
|
|
|
|
errs() << Err;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
|
|
|
|
|
2009-08-23 04:51:22 +02:00
|
|
|
if (!DisableOutput)
|
2009-10-17 05:28:28 +02:00
|
|
|
WriteOutputFile(M.get());
|
|
|
|
|
2009-08-23 04:51:22 +02:00
|
|
|
return 0;
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|