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
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and 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
|
|
|
|
// llvm-as [options] - Read LLVM asm from stdin, write bytecode to stdout
|
|
|
|
// llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bytecode
|
|
|
|
// 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
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Assembly/Parser.h"
|
|
|
|
#include "llvm/Bytecode/Writer.h"
|
2002-08-31 06:54:41 +08:00
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-12-06 09:18:01 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2006-11-29 08:19:40 +08:00
|
|
|
#include "llvm/Support/Streams.h"
|
2005-01-02 08:08:46 +08:00
|
|
|
#include "llvm/Support/SystemUtils.h"
|
2004-05-27 13:41:36 +08:00
|
|
|
#include "llvm/System/Signals.h"
|
2001-11-27 08:03:19 +08:00
|
|
|
#include <fstream>
|
2004-07-04 20:20:55 +08:00
|
|
|
#include <iostream>
|
2002-01-21 06:54:45 +08:00
|
|
|
#include <memory>
|
2003-11-12 06:41:34 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2005-04-22 08:00:37 +08:00
|
|
|
static cl::opt<std::string>
|
2002-07-22 10:10:13 +08:00
|
|
|
InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
|
|
|
|
|
2003-05-23 04:13:16 +08:00
|
|
|
static cl::opt<std::string>
|
2002-07-22 10:10:13 +08:00
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
Force("f", cl::desc("Overwrite output files"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2005-04-22 08:00:37 +08:00
|
|
|
static cl::opt<bool>
|
2007-01-21 14:34:18 +08:00
|
|
|
NoCompress("disable-compression", cl::init(true),
|
2005-01-02 06:10:32 +08:00
|
|
|
cl::desc("Don't compress the generated bytecode"));
|
2004-11-07 07:17:23 +08:00
|
|
|
|
2003-08-19 04:47:13 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
DisableVerify("disable-verify", cl::Hidden,
|
2004-07-12 07:20:54 +08:00
|
|
|
cl::desc("Do not run verifier on input LLVM (dangerous!)"));
|
2003-08-19 04:47:13 +08:00
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
int main(int argc, char **argv) {
|
2006-12-06 09:18:01 +08:00
|
|
|
llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
|
2001-07-23 10:35:57 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
|
2004-08-30 03:28:55 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2004-12-30 13:36:08 +08:00
|
|
|
int exitCode = 0;
|
2002-06-26 05:43:28 +08:00
|
|
|
std::ostream *Out = 0;
|
2001-06-07 04:29:01 +08:00
|
|
|
try {
|
|
|
|
// Parse the file now...
|
2006-08-18 16:43:06 +08:00
|
|
|
ParseError Err;
|
|
|
|
std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename,&Err));
|
2002-04-08 06:34:19 +08:00
|
|
|
if (M.get() == 0) {
|
2006-12-07 09:30:32 +08:00
|
|
|
cerr << argv[0] << ": " << Err.getMessage() << "\n";
|
2001-06-07 04:29:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2002-08-31 06:54:41 +08:00
|
|
|
|
2006-07-07 02:02:27 +08:00
|
|
|
if (!DisableVerify) {
|
|
|
|
std::string Err;
|
|
|
|
if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
|
2006-12-07 09:30:32 +08:00
|
|
|
cerr << argv[0]
|
|
|
|
<< ": assembly parsed, but does not verify as correct!\n";
|
|
|
|
cerr << Err;
|
2006-07-07 02:02:27 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2002-08-31 06:54:41 +08:00
|
|
|
}
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2006-12-07 09:30:32 +08:00
|
|
|
if (DumpAsm) cerr << "Here's the assembly:\n" << *M.get();
|
2001-07-23 10:35:57 +08:00
|
|
|
|
2001-07-24 03:27:24 +08:00
|
|
|
if (OutputFilename != "") { // Specified an output filename?
|
2003-06-01 05:47:16 +08:00
|
|
|
if (OutputFilename != "-") { // Not stdout?
|
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
2006-12-07 09:30:32 +08:00
|
|
|
cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
2003-06-01 05:47:16 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2005-04-22 08:00:37 +08:00
|
|
|
Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
|
2005-01-23 01:36:17 +08:00
|
|
|
std::ios::trunc | std::ios::binary);
|
2003-06-01 05:47:16 +08:00
|
|
|
} else { // Specified stdout
|
2005-01-23 01:36:17 +08:00
|
|
|
// FIXME: cout is not binary!
|
|
|
|
Out = &std::cout;
|
2002-01-21 06:54:45 +08:00
|
|
|
}
|
2001-07-23 10:35:57 +08:00
|
|
|
} else {
|
2001-07-24 03:27:24 +08:00
|
|
|
if (InputFilename == "-") {
|
2005-01-23 01:36:17 +08:00
|
|
|
OutputFilename = "-";
|
|
|
|
Out = &std::cout;
|
2001-07-23 10:35:57 +08:00
|
|
|
} else {
|
2005-01-23 01:36:17 +08: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 10:35:57 +08:00
|
|
|
} else {
|
2005-01-23 01:36:17 +08:00
|
|
|
OutputFilename = IFN; // Append a .bc to it
|
|
|
|
}
|
|
|
|
OutputFilename += ".bc";
|
2002-01-21 06:54:45 +08:00
|
|
|
|
2002-01-23 05:07:24 +08:00
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
2002-01-21 06:54:45 +08:00
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
2006-12-07 09:30:32 +08:00
|
|
|
cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
2002-01-21 06:54:45 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-04-22 08:00:37 +08:00
|
|
|
Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
|
2005-01-23 01:36:17 +08:00
|
|
|
std::ios::trunc | std::ios::binary);
|
2003-10-11 01:56:49 +08:00
|
|
|
// Make sure that the Out file gets unlinked from the disk if we get a
|
2002-04-19 03:55:25 +08:00
|
|
|
// SIGINT
|
2004-11-15 06:30:54 +08:00
|
|
|
sys::RemoveFileOnSignal(sys::Path(OutputFilename));
|
2001-07-23 10:35:57 +08:00
|
|
|
}
|
2002-01-21 06:54:45 +08:00
|
|
|
}
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
if (!Out->good()) {
|
2006-12-07 09:30:32 +08:00
|
|
|
cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
|
2002-01-21 06:54:45 +08:00
|
|
|
return 1;
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2005-01-02 08:08:46 +08:00
|
|
|
if (Force || !CheckBytecodeOutputToConsole(Out,true)) {
|
2006-12-07 09:30:32 +08:00
|
|
|
OStream L(*Out);
|
2006-11-29 08:19:40 +08:00
|
|
|
WriteBytecodeToFile(M.get(), L, !NoCompress);
|
2005-01-02 08:08:46 +08:00
|
|
|
}
|
2004-12-30 13:36:08 +08:00
|
|
|
} catch (const std::string& msg) {
|
2006-12-07 09:30:32 +08:00
|
|
|
cerr << argv[0] << ": " << msg << "\n";
|
2004-12-30 13:36:08 +08:00
|
|
|
exitCode = 1;
|
|
|
|
} catch (...) {
|
2006-12-07 09:30:32 +08:00
|
|
|
cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
|
2004-12-30 13:36:08 +08:00
|
|
|
exitCode = 1;
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2002-06-26 05:43:28 +08:00
|
|
|
if (Out != &std::cout) delete Out;
|
2004-12-30 13:36:08 +08:00
|
|
|
return exitCode;
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|