2003-10-14 21:34:11 +00:00
|
|
|
//===-- ToolRunner.cpp ----------------------------------------------------===//
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00: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-21 22:55:34 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-10-14 21:34:11 +00:00
|
|
|
//
|
|
|
|
// This file implements the interfaces described in the ToolRunner.h file.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-10-19 02:27:40 +00:00
|
|
|
#define DEBUG_TYPE "toolrunner"
|
2006-06-06 22:30:59 +00:00
|
|
|
#include "ToolRunner.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Config/config.h" // for HAVE_LINK_R
|
2005-02-13 23:13:47 +00:00
|
|
|
#include "llvm/System/Program.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2003-10-14 21:34:11 +00:00
|
|
|
#include <fstream>
|
2004-02-18 20:38:00 +00:00
|
|
|
#include <sstream>
|
2006-01-22 22:53:01 +00:00
|
|
|
#include <iostream>
|
2003-12-14 21:35:53 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-02-19 07:39:26 +00:00
|
|
|
ToolExecutionError::~ToolExecutionError() throw() { }
|
|
|
|
|
2005-02-13 23:13:47 +00:00
|
|
|
/// RunProgramWithTimeout - This function provides an alternate interface to the
|
|
|
|
/// sys::Program::ExecuteAndWait interface.
|
|
|
|
/// @see sys:Program::ExecuteAndWait
|
|
|
|
static int RunProgramWithTimeout(const sys::Path &ProgramPath,
|
|
|
|
const char **Args,
|
|
|
|
const sys::Path &StdInFile,
|
|
|
|
const sys::Path &StdOutFile,
|
|
|
|
const sys::Path &StdErrFile,
|
|
|
|
unsigned NumSeconds = 0) {
|
|
|
|
const sys::Path* redirects[3];
|
|
|
|
redirects[0] = &StdInFile;
|
|
|
|
redirects[1] = &StdOutFile;
|
|
|
|
redirects[2] = &StdErrFile;
|
2006-09-15 21:29:15 +00:00
|
|
|
|
2006-09-15 23:01:10 +00:00
|
|
|
if (0) {
|
|
|
|
std::cerr << "RUN:";
|
|
|
|
for (unsigned i = 0; Args[i]; ++i)
|
|
|
|
std::cerr << " " << Args[i];
|
|
|
|
std::cerr << "\n";
|
|
|
|
}
|
2005-04-21 22:55:34 +00:00
|
|
|
|
|
|
|
return
|
2005-02-13 23:13:47 +00:00
|
|
|
sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects, NumSeconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-12-19 18:00:21 +00:00
|
|
|
static void ProcessFailure(sys::Path ProgPath, const char** Args) {
|
2004-02-18 20:38:00 +00:00
|
|
|
std::ostringstream OS;
|
2004-02-18 20:58:00 +00:00
|
|
|
OS << "\nError running tool:\n ";
|
2004-02-18 20:38:00 +00:00
|
|
|
for (const char **Arg = Args; *Arg; ++Arg)
|
|
|
|
OS << " " << *Arg;
|
|
|
|
OS << "\n";
|
|
|
|
|
|
|
|
// Rerun the compiler, capturing any error messages to print them.
|
2004-12-15 01:51:56 +00:00
|
|
|
sys::Path ErrorFilename("error_messages");
|
2006-08-23 20:34:57 +00:00
|
|
|
std::string ErrMsg;
|
|
|
|
if (ErrorFilename.makeUnique(true, &ErrMsg)) {
|
|
|
|
std::cerr << "Error making unique filename: " << ErrMsg << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-12-19 18:00:21 +00:00
|
|
|
RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename,
|
2006-08-21 06:04:45 +00:00
|
|
|
ErrorFilename); // FIXME: check return code ?
|
2004-02-18 20:38:00 +00:00
|
|
|
|
|
|
|
// Print out the error messages generated by GCC if possible...
|
|
|
|
std::ifstream ErrorFile(ErrorFilename.c_str());
|
|
|
|
if (ErrorFile) {
|
|
|
|
std::copy(std::istreambuf_iterator<char>(ErrorFile),
|
|
|
|
std::istreambuf_iterator<char>(),
|
|
|
|
std::ostreambuf_iterator<char>(OS));
|
|
|
|
ErrorFile.close();
|
|
|
|
}
|
|
|
|
|
2005-07-08 03:08:58 +00:00
|
|
|
ErrorFilename.eraseFromDisk();
|
2004-02-18 20:38:00 +00:00
|
|
|
throw ToolExecutionError(OS.str());
|
|
|
|
}
|
|
|
|
|
2003-09-29 22:39:25 +00:00
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// LLI Implementation of AbstractIntepreter interface
|
|
|
|
//
|
2003-12-14 21:35:53 +00:00
|
|
|
namespace {
|
|
|
|
class LLI : public AbstractInterpreter {
|
|
|
|
std::string LLIPath; // The path to the LLI executable
|
2004-05-04 21:09:01 +00:00
|
|
|
std::vector<std::string> ToolArgs; // Args to pass to LLI
|
2003-12-14 21:35:53 +00:00
|
|
|
public:
|
2004-05-04 21:09:01 +00:00
|
|
|
LLI(const std::string &Path, const std::vector<std::string> *Args)
|
|
|
|
: LLIPath(Path) {
|
|
|
|
ToolArgs.clear ();
|
2004-05-04 22:02:41 +00:00
|
|
|
if (Args) { ToolArgs = *Args; }
|
2004-05-04 21:09:01 +00:00
|
|
|
}
|
2005-04-21 22:55:34 +00:00
|
|
|
|
2003-12-14 21:35:53 +00:00
|
|
|
virtual int ExecuteProgram(const std::string &Bytecode,
|
|
|
|
const std::vector<std::string> &Args,
|
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
2006-06-06 00:00:42 +00:00
|
|
|
const std::vector<std::string> &GCCArgs,
|
2005-04-21 22:55:34 +00:00
|
|
|
const std::vector<std::string> &SharedLibs =
|
2004-07-24 07:49:11 +00:00
|
|
|
std::vector<std::string>(),
|
|
|
|
unsigned Timeout = 0);
|
2003-12-14 21:35:53 +00:00
|
|
|
};
|
|
|
|
}
|
2003-09-29 22:39:25 +00:00
|
|
|
|
|
|
|
int LLI::ExecuteProgram(const std::string &Bytecode,
|
2003-10-14 21:34:11 +00:00
|
|
|
const std::vector<std::string> &Args,
|
2003-09-29 22:39:25 +00:00
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
2006-06-06 00:00:42 +00:00
|
|
|
const std::vector<std::string> &GCCArgs,
|
2004-07-24 07:49:11 +00:00
|
|
|
const std::vector<std::string> &SharedLibs,
|
|
|
|
unsigned Timeout) {
|
2004-02-18 20:21:57 +00:00
|
|
|
if (!SharedLibs.empty())
|
|
|
|
throw ToolExecutionError("LLI currently does not support "
|
|
|
|
"loading shared libraries.");
|
2003-09-29 22:39:25 +00:00
|
|
|
|
2006-06-06 00:00:42 +00:00
|
|
|
if (!GCCArgs.empty())
|
|
|
|
throw ToolExecutionError("LLI currently does not support "
|
|
|
|
"GCC Arguments.");
|
2003-09-29 22:39:25 +00:00
|
|
|
std::vector<const char*> LLIArgs;
|
|
|
|
LLIArgs.push_back(LLIPath.c_str());
|
|
|
|
LLIArgs.push_back("-force-interpreter=true");
|
2004-05-04 21:09:01 +00:00
|
|
|
|
|
|
|
// Add any extra LLI args.
|
|
|
|
for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
|
|
|
|
LLIArgs.push_back(ToolArgs[i].c_str());
|
|
|
|
|
2003-09-29 22:39:25 +00:00
|
|
|
LLIArgs.push_back(Bytecode.c_str());
|
|
|
|
// Add optional parameters to the running program from Argv
|
|
|
|
for (unsigned i=0, e = Args.size(); i != e; ++i)
|
|
|
|
LLIArgs.push_back(Args[i].c_str());
|
|
|
|
LLIArgs.push_back(0);
|
|
|
|
|
|
|
|
std::cout << "<lli>" << std::flush;
|
2003-10-19 02:27:40 +00:00
|
|
|
DEBUG(std::cerr << "\nAbout to run:\t";
|
2003-10-19 02:14:58 +00:00
|
|
|
for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
|
2003-09-29 22:39:25 +00:00
|
|
|
std::cerr << " " << LLIArgs[i];
|
|
|
|
std::cerr << "\n";
|
|
|
|
);
|
2004-12-19 18:00:21 +00:00
|
|
|
return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
|
2005-04-21 22:55:34 +00:00
|
|
|
sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
|
2004-12-19 18:00:21 +00:00
|
|
|
Timeout);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LLI create method - Try to find the LLI executable
|
2003-10-14 21:34:11 +00:00
|
|
|
AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
|
2004-05-04 21:09:01 +00:00
|
|
|
std::string &Message,
|
|
|
|
const std::vector<std::string> *ToolArgs) {
|
2004-12-13 23:43:44 +00:00
|
|
|
std::string LLIPath = FindExecutable("lli", ProgPath).toString();
|
2003-09-29 22:39:25 +00:00
|
|
|
if (!LLIPath.empty()) {
|
|
|
|
Message = "Found lli: " + LLIPath + "\n";
|
2004-05-04 21:09:01 +00:00
|
|
|
return new LLI(LLIPath, ToolArgs);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Message = "Cannot find `lli' in executable directory or PATH!\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LLC Implementation of AbstractIntepreter interface
|
|
|
|
//
|
2006-09-15 21:29:15 +00:00
|
|
|
GCC::FileType LLC::OutputCode(const std::string &Bytecode,
|
|
|
|
sys::Path &OutputAsmFile) {
|
2004-12-15 01:51:56 +00:00
|
|
|
sys::Path uniqueFile(Bytecode+".llc.s");
|
2006-08-23 20:34:57 +00:00
|
|
|
std::string ErrMsg;
|
|
|
|
if (uniqueFile.makeUnique(true, &ErrMsg)) {
|
|
|
|
std::cerr << "Error making unique filename: " << ErrMsg << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-12-16 23:01:34 +00:00
|
|
|
OutputAsmFile = uniqueFile;
|
2004-05-04 21:09:01 +00:00
|
|
|
std::vector<const char *> LLCArgs;
|
|
|
|
LLCArgs.push_back (LLCPath.c_str());
|
|
|
|
|
|
|
|
// Add any extra LLC args.
|
|
|
|
for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
|
|
|
|
LLCArgs.push_back(ToolArgs[i].c_str());
|
|
|
|
|
|
|
|
LLCArgs.push_back ("-o");
|
|
|
|
LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
|
|
|
|
LLCArgs.push_back ("-f"); // Overwrite as necessary...
|
|
|
|
LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
|
|
|
|
LLCArgs.push_back (0);
|
2003-09-29 22:39:25 +00:00
|
|
|
|
|
|
|
std::cout << "<llc>" << std::flush;
|
2004-05-04 21:09:01 +00:00
|
|
|
DEBUG(std::cerr << "\nAbout to run:\t";
|
|
|
|
for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
|
|
|
|
std::cerr << " " << LLCArgs[i];
|
|
|
|
std::cerr << "\n";
|
|
|
|
);
|
2005-04-21 22:55:34 +00:00
|
|
|
if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
|
2004-12-19 18:00:21 +00:00
|
|
|
sys::Path(), sys::Path(), sys::Path()))
|
|
|
|
ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]);
|
2006-09-15 21:29:15 +00:00
|
|
|
|
|
|
|
return GCC::AsmFile;
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
2004-02-18 23:24:41 +00:00
|
|
|
void LLC::compileProgram(const std::string &Bytecode) {
|
2004-12-16 23:01:34 +00:00
|
|
|
sys::Path OutputAsmFile;
|
2006-09-15 21:29:15 +00:00
|
|
|
OutputCode(Bytecode, OutputAsmFile);
|
2005-07-08 03:08:58 +00:00
|
|
|
OutputAsmFile.eraseFromDisk();
|
2004-02-18 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-29 22:39:25 +00:00
|
|
|
int LLC::ExecuteProgram(const std::string &Bytecode,
|
2003-10-14 21:34:11 +00:00
|
|
|
const std::vector<std::string> &Args,
|
2003-09-29 22:39:25 +00:00
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
2006-06-06 00:00:42 +00:00
|
|
|
const std::vector<std::string> &ArgsForGCC,
|
2004-07-24 07:49:11 +00:00
|
|
|
const std::vector<std::string> &SharedLibs,
|
|
|
|
unsigned Timeout) {
|
2003-10-14 21:52:52 +00:00
|
|
|
|
2004-12-16 23:01:34 +00:00
|
|
|
sys::Path OutputAsmFile;
|
2006-09-15 21:29:15 +00:00
|
|
|
OutputCode(Bytecode, OutputAsmFile);
|
2004-02-18 20:21:57 +00:00
|
|
|
FileRemover OutFileRemover(OutputAsmFile);
|
2003-09-29 22:39:25 +00:00
|
|
|
|
2006-06-06 00:00:42 +00:00
|
|
|
std::vector<std::string> GCCArgs(ArgsForGCC);
|
|
|
|
GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end());
|
|
|
|
|
2003-09-29 22:39:25 +00:00
|
|
|
// Assuming LLC worked, compile the result with GCC and run it.
|
2004-12-16 23:01:34 +00:00
|
|
|
return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile,
|
2006-06-06 00:00:42 +00:00
|
|
|
InputFile, OutputFile, GCCArgs, Timeout);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
2003-10-14 21:34:11 +00:00
|
|
|
/// createLLC - Try to find the LLC executable
|
2003-09-29 22:39:25 +00:00
|
|
|
///
|
2003-10-14 21:34:11 +00:00
|
|
|
LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
|
2004-05-04 21:09:01 +00:00
|
|
|
std::string &Message,
|
|
|
|
const std::vector<std::string> *Args) {
|
2004-12-13 23:43:44 +00:00
|
|
|
std::string LLCPath = FindExecutable("llc", ProgramPath).toString();
|
2003-09-29 22:39:25 +00:00
|
|
|
if (LLCPath.empty()) {
|
|
|
|
Message = "Cannot find `llc' in executable directory or PATH!\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Message = "Found llc: " + LLCPath + "\n";
|
2003-10-14 21:34:11 +00:00
|
|
|
GCC *gcc = GCC::create(ProgramPath, Message);
|
2003-09-29 22:39:25 +00:00
|
|
|
if (!gcc) {
|
|
|
|
std::cerr << Message << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-05-04 21:09:01 +00:00
|
|
|
return new LLC(LLCPath, gcc, Args);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// JIT Implementation of AbstractIntepreter interface
|
|
|
|
//
|
2003-12-14 21:35:53 +00:00
|
|
|
namespace {
|
|
|
|
class JIT : public AbstractInterpreter {
|
|
|
|
std::string LLIPath; // The path to the LLI executable
|
2004-05-04 21:09:01 +00:00
|
|
|
std::vector<std::string> ToolArgs; // Args to pass to LLI
|
2003-12-14 21:35:53 +00:00
|
|
|
public:
|
2004-05-04 21:09:01 +00:00
|
|
|
JIT(const std::string &Path, const std::vector<std::string> *Args)
|
|
|
|
: LLIPath(Path) {
|
|
|
|
ToolArgs.clear ();
|
2004-05-04 22:02:41 +00:00
|
|
|
if (Args) { ToolArgs = *Args; }
|
2004-05-04 21:09:01 +00:00
|
|
|
}
|
2005-04-21 22:55:34 +00:00
|
|
|
|
2003-12-14 21:35:53 +00:00
|
|
|
virtual int ExecuteProgram(const std::string &Bytecode,
|
|
|
|
const std::vector<std::string> &Args,
|
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
2006-06-06 00:00:42 +00:00
|
|
|
const std::vector<std::string> &GCCArgs =
|
|
|
|
std::vector<std::string>(),
|
2005-04-21 22:55:34 +00:00
|
|
|
const std::vector<std::string> &SharedLibs =
|
2006-06-06 00:00:42 +00:00
|
|
|
std::vector<std::string>(),
|
|
|
|
unsigned Timeout =0 );
|
2003-12-14 21:35:53 +00:00
|
|
|
};
|
|
|
|
}
|
2003-09-29 22:39:25 +00:00
|
|
|
|
|
|
|
int JIT::ExecuteProgram(const std::string &Bytecode,
|
2003-10-14 21:34:11 +00:00
|
|
|
const std::vector<std::string> &Args,
|
2003-09-29 22:39:25 +00:00
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
2006-06-06 00:00:42 +00:00
|
|
|
const std::vector<std::string> &GCCArgs,
|
2004-07-24 07:49:11 +00:00
|
|
|
const std::vector<std::string> &SharedLibs,
|
|
|
|
unsigned Timeout) {
|
2006-06-06 00:00:42 +00:00
|
|
|
if (!GCCArgs.empty())
|
|
|
|
throw ToolExecutionError("JIT does not support GCC Arguments.");
|
2003-09-29 22:39:25 +00:00
|
|
|
// Construct a vector of parameters, incorporating those from the command-line
|
|
|
|
std::vector<const char*> JITArgs;
|
|
|
|
JITArgs.push_back(LLIPath.c_str());
|
|
|
|
JITArgs.push_back("-force-interpreter=false");
|
2003-10-14 21:52:52 +00:00
|
|
|
|
2004-05-04 21:09:01 +00:00
|
|
|
// Add any extra LLI args.
|
|
|
|
for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
|
|
|
|
JITArgs.push_back(ToolArgs[i].c_str());
|
|
|
|
|
2003-10-14 21:52:52 +00:00
|
|
|
for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
|
2003-09-29 22:39:25 +00:00
|
|
|
JITArgs.push_back("-load");
|
2003-10-14 21:52:52 +00:00
|
|
|
JITArgs.push_back(SharedLibs[i].c_str());
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
JITArgs.push_back(Bytecode.c_str());
|
|
|
|
// Add optional parameters to the running program from Argv
|
|
|
|
for (unsigned i=0, e = Args.size(); i != e; ++i)
|
|
|
|
JITArgs.push_back(Args[i].c_str());
|
|
|
|
JITArgs.push_back(0);
|
|
|
|
|
|
|
|
std::cout << "<jit>" << std::flush;
|
2003-10-19 02:27:40 +00:00
|
|
|
DEBUG(std::cerr << "\nAbout to run:\t";
|
2003-10-19 02:14:58 +00:00
|
|
|
for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
|
2003-09-29 22:39:25 +00:00
|
|
|
std::cerr << " " << JITArgs[i];
|
|
|
|
std::cerr << "\n";
|
|
|
|
);
|
|
|
|
DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
|
2004-12-19 18:00:21 +00:00
|
|
|
return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0],
|
2005-04-21 22:55:34 +00:00
|
|
|
sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
|
2004-12-19 18:00:21 +00:00
|
|
|
Timeout);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
2003-10-14 21:34:11 +00:00
|
|
|
/// createJIT - Try to find the LLI executable
|
2003-09-29 22:39:25 +00:00
|
|
|
///
|
2003-10-14 21:34:11 +00:00
|
|
|
AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
|
2004-05-04 21:09:01 +00:00
|
|
|
std::string &Message, const std::vector<std::string> *Args) {
|
2004-12-13 23:43:44 +00:00
|
|
|
std::string LLIPath = FindExecutable("lli", ProgPath).toString();
|
2003-09-29 22:39:25 +00:00
|
|
|
if (!LLIPath.empty()) {
|
|
|
|
Message = "Found lli: " + LLIPath + "\n";
|
2004-05-04 21:09:01 +00:00
|
|
|
return new JIT(LLIPath, Args);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Message = "Cannot find `lli' in executable directory or PATH!\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-15 21:29:15 +00:00
|
|
|
GCC::FileType CBE::OutputCode(const std::string &Bytecode,
|
|
|
|
sys::Path &OutputCFile) {
|
2004-12-15 01:51:56 +00:00
|
|
|
sys::Path uniqueFile(Bytecode+".cbe.c");
|
2006-08-23 20:34:57 +00:00
|
|
|
std::string ErrMsg;
|
|
|
|
if (uniqueFile.makeUnique(true, &ErrMsg)) {
|
|
|
|
std::cerr << "Error making unique filename: " << ErrMsg << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-12-16 23:01:34 +00:00
|
|
|
OutputCFile = uniqueFile;
|
2004-05-04 21:09:01 +00:00
|
|
|
std::vector<const char *> LLCArgs;
|
|
|
|
LLCArgs.push_back (LLCPath.c_str());
|
|
|
|
|
|
|
|
// Add any extra LLC args.
|
|
|
|
for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
|
|
|
|
LLCArgs.push_back(ToolArgs[i].c_str());
|
|
|
|
|
|
|
|
LLCArgs.push_back ("-o");
|
|
|
|
LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file
|
|
|
|
LLCArgs.push_back ("-march=c"); // Output C language
|
|
|
|
LLCArgs.push_back ("-f"); // Overwrite as necessary...
|
|
|
|
LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
|
|
|
|
LLCArgs.push_back (0);
|
2003-09-29 22:39:25 +00:00
|
|
|
|
|
|
|
std::cout << "<cbe>" << std::flush;
|
2004-05-04 21:09:01 +00:00
|
|
|
DEBUG(std::cerr << "\nAbout to run:\t";
|
|
|
|
for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
|
|
|
|
std::cerr << " " << LLCArgs[i];
|
|
|
|
std::cerr << "\n";
|
|
|
|
);
|
2005-04-21 22:55:34 +00:00
|
|
|
if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
|
2004-12-19 18:00:21 +00:00
|
|
|
sys::Path()))
|
2004-05-04 21:09:01 +00:00
|
|
|
ProcessFailure(LLCPath, &LLCArgs[0]);
|
2006-09-15 21:29:15 +00:00
|
|
|
return GCC::CFile;
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
2004-02-18 23:24:41 +00:00
|
|
|
void CBE::compileProgram(const std::string &Bytecode) {
|
2004-12-16 23:01:34 +00:00
|
|
|
sys::Path OutputCFile;
|
2006-09-15 21:29:15 +00:00
|
|
|
OutputCode(Bytecode, OutputCFile);
|
2005-07-08 03:08:58 +00:00
|
|
|
OutputCFile.eraseFromDisk();
|
2004-02-18 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-29 22:39:25 +00:00
|
|
|
int CBE::ExecuteProgram(const std::string &Bytecode,
|
2003-10-14 21:34:11 +00:00
|
|
|
const std::vector<std::string> &Args,
|
2003-09-29 22:39:25 +00:00
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
2006-06-06 00:00:42 +00:00
|
|
|
const std::vector<std::string> &ArgsForGCC,
|
2004-07-24 07:49:11 +00:00
|
|
|
const std::vector<std::string> &SharedLibs,
|
|
|
|
unsigned Timeout) {
|
2004-12-16 23:01:34 +00:00
|
|
|
sys::Path OutputCFile;
|
2006-09-15 21:29:15 +00:00
|
|
|
OutputCode(Bytecode, OutputCFile);
|
2003-09-29 22:39:25 +00:00
|
|
|
|
2004-02-18 20:21:57 +00:00
|
|
|
FileRemover CFileRemove(OutputCFile);
|
2003-09-29 22:39:25 +00:00
|
|
|
|
2006-06-06 00:00:42 +00:00
|
|
|
std::vector<std::string> GCCArgs(ArgsForGCC);
|
|
|
|
GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end());
|
2005-04-21 22:55:34 +00:00
|
|
|
return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile,
|
2006-06-06 00:00:42 +00:00
|
|
|
InputFile, OutputFile, GCCArgs, Timeout);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
2004-02-17 06:40:06 +00:00
|
|
|
/// createCBE - Try to find the 'llc' executable
|
2003-09-29 22:39:25 +00:00
|
|
|
///
|
2003-10-14 21:34:11 +00:00
|
|
|
CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
|
2004-05-04 21:09:01 +00:00
|
|
|
std::string &Message,
|
|
|
|
const std::vector<std::string> *Args) {
|
2004-12-19 18:00:21 +00:00
|
|
|
sys::Path LLCPath = FindExecutable("llc", ProgramPath);
|
|
|
|
if (LLCPath.isEmpty()) {
|
2005-04-21 22:55:34 +00:00
|
|
|
Message =
|
2004-02-17 06:40:06 +00:00
|
|
|
"Cannot find `llc' in executable directory or PATH!\n";
|
2003-09-29 22:39:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-19 18:00:21 +00:00
|
|
|
Message = "Found llc: " + LLCPath.toString() + "\n";
|
2003-10-14 21:34:11 +00:00
|
|
|
GCC *gcc = GCC::create(ProgramPath, Message);
|
2003-09-29 22:39:25 +00:00
|
|
|
if (!gcc) {
|
|
|
|
std::cerr << Message << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-05-04 21:09:01 +00:00
|
|
|
return new CBE(LLCPath, gcc, Args);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// GCC abstraction
|
|
|
|
//
|
|
|
|
int GCC::ExecuteProgram(const std::string &ProgramFile,
|
2003-10-14 21:34:11 +00:00
|
|
|
const std::vector<std::string> &Args,
|
2003-09-29 22:39:25 +00:00
|
|
|
FileType fileType,
|
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
2006-06-06 00:00:42 +00:00
|
|
|
const std::vector<std::string> &ArgsForGCC,
|
|
|
|
unsigned Timeout ) {
|
2003-09-29 22:39:25 +00:00
|
|
|
std::vector<const char*> GCCArgs;
|
|
|
|
|
|
|
|
GCCArgs.push_back(GCCPath.c_str());
|
2003-10-14 21:52:52 +00:00
|
|
|
|
|
|
|
// Specify -x explicitly in case the extension is wonky
|
2003-09-29 22:39:25 +00:00
|
|
|
GCCArgs.push_back("-x");
|
|
|
|
if (fileType == CFile) {
|
|
|
|
GCCArgs.push_back("c");
|
|
|
|
GCCArgs.push_back("-fno-strict-aliasing");
|
|
|
|
} else {
|
|
|
|
GCCArgs.push_back("assembler");
|
2005-08-29 13:14:24 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
GCCArgs.push_back("-force_cpusubtype_ALL");
|
|
|
|
#endif
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
|
2006-06-09 21:31:53 +00:00
|
|
|
GCCArgs.push_back("-x");
|
|
|
|
GCCArgs.push_back("none");
|
2003-09-29 22:39:25 +00:00
|
|
|
GCCArgs.push_back("-o");
|
2004-12-15 01:51:56 +00:00
|
|
|
sys::Path OutputBinary (ProgramFile+".gcc.exe");
|
2006-08-23 20:34:57 +00:00
|
|
|
std::string ErrMsg;
|
|
|
|
if (OutputBinary.makeUnique(true, &ErrMsg)) {
|
|
|
|
std::cerr << "Error making unique filename: " << ErrMsg << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-09-29 22:39:25 +00:00
|
|
|
GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
|
2006-06-06 00:00:42 +00:00
|
|
|
|
|
|
|
// Add any arguments intended for GCC. We locate them here because this is
|
|
|
|
// most likely -L and -l options that need to come before other libraries but
|
|
|
|
// after the source. Other options won't be sensitive to placement on the
|
|
|
|
// command line, so this should be safe.
|
|
|
|
for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
|
|
|
|
GCCArgs.push_back(ArgsForGCC[i].c_str());
|
|
|
|
|
2003-09-29 22:39:25 +00:00
|
|
|
GCCArgs.push_back("-lm"); // Hard-code the math library...
|
|
|
|
GCCArgs.push_back("-O2"); // Optimize the program a bit...
|
2003-11-18 06:31:17 +00:00
|
|
|
#if defined (HAVE_LINK_R)
|
2003-10-18 21:54:47 +00:00
|
|
|
GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
|
2006-02-04 05:02:27 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __sparc__
|
|
|
|
GCCArgs.push_back("-mcpu=v9");
|
2003-11-18 06:31:17 +00:00
|
|
|
#endif
|
2003-09-29 22:39:25 +00:00
|
|
|
GCCArgs.push_back(0); // NULL terminator
|
|
|
|
|
|
|
|
std::cout << "<gcc>" << std::flush;
|
2004-12-19 18:00:21 +00:00
|
|
|
if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
|
|
|
|
sys::Path())) {
|
2004-02-18 20:38:00 +00:00
|
|
|
ProcessFailure(GCCPath, &GCCArgs[0]);
|
2003-09-29 22:39:25 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const char*> ProgramArgs;
|
2005-02-13 23:13:47 +00:00
|
|
|
|
2003-09-29 22:39:25 +00:00
|
|
|
ProgramArgs.push_back(OutputBinary.c_str());
|
|
|
|
// Add optional parameters to the running program from Argv
|
|
|
|
for (unsigned i=0, e = Args.size(); i != e; ++i)
|
|
|
|
ProgramArgs.push_back(Args[i].c_str());
|
|
|
|
ProgramArgs.push_back(0); // NULL terminator
|
|
|
|
|
|
|
|
// Now that we have a binary, run it!
|
|
|
|
std::cout << "<program>" << std::flush;
|
2003-10-19 02:27:40 +00:00
|
|
|
DEBUG(std::cerr << "\nAbout to run:\t";
|
2003-10-19 02:14:58 +00:00
|
|
|
for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
|
2003-09-29 22:39:25 +00:00
|
|
|
std::cerr << " " << ProgramArgs[i];
|
|
|
|
std::cerr << "\n";
|
|
|
|
);
|
2004-02-18 20:21:57 +00:00
|
|
|
|
2004-12-16 23:01:34 +00:00
|
|
|
FileRemover OutputBinaryRemover(OutputBinary);
|
2004-12-19 18:00:21 +00:00
|
|
|
return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
|
2005-04-21 22:55:34 +00:00
|
|
|
sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
|
2004-12-19 18:00:21 +00:00
|
|
|
Timeout);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
2003-10-14 21:07:25 +00:00
|
|
|
int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
|
2006-06-27 20:35:36 +00:00
|
|
|
std::string &OutputFile,
|
|
|
|
const std::vector<std::string> &ArgsForGCC) {
|
2004-12-15 01:51:56 +00:00
|
|
|
sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
|
2006-08-23 20:34:57 +00:00
|
|
|
std::string ErrMsg;
|
|
|
|
if (uniqueFilename.makeUnique(true, &ErrMsg)) {
|
|
|
|
std::cerr << "Error making unique filename: " << ErrMsg << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-12-15 01:51:56 +00:00
|
|
|
OutputFile = uniqueFilename.toString();
|
|
|
|
|
2006-06-27 20:35:36 +00:00
|
|
|
std::vector<const char*> GCCArgs;
|
|
|
|
|
|
|
|
GCCArgs.push_back(GCCPath.c_str());
|
|
|
|
|
|
|
|
|
2003-09-29 22:39:25 +00:00
|
|
|
// Compile the C/asm file into a shared object
|
2006-06-27 20:35:36 +00:00
|
|
|
GCCArgs.push_back("-x");
|
|
|
|
GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
|
|
|
|
GCCArgs.push_back("-fno-strict-aliasing");
|
|
|
|
GCCArgs.push_back(InputFile.c_str()); // Specify the input filename.
|
2003-09-29 22:39:25 +00:00
|
|
|
#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
|
2006-06-27 20:35:36 +00:00
|
|
|
GCCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
|
2005-07-08 00:23:26 +00:00
|
|
|
#elif defined(__APPLE__)
|
2006-06-27 20:35:36 +00:00
|
|
|
// link all source files into a single module in data segment, rather than
|
|
|
|
// generating blocks. dynamic_lookup requires that you set
|
|
|
|
// MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
|
|
|
|
// bugpoint to just pass that in the environment of GCC.
|
|
|
|
GCCArgs.push_back("-single_module");
|
|
|
|
GCCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
|
|
|
|
GCCArgs.push_back("-undefined");
|
|
|
|
GCCArgs.push_back("dynamic_lookup");
|
2004-07-16 19:45:45 +00:00
|
|
|
#else
|
2006-06-27 20:35:36 +00:00
|
|
|
GCCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
|
2003-09-29 22:39:25 +00:00
|
|
|
#endif
|
2005-03-09 03:31:02 +00:00
|
|
|
|
2005-03-10 20:15:09 +00:00
|
|
|
#if defined(__ia64__) || defined(__alpha__)
|
2006-06-27 20:35:36 +00:00
|
|
|
GCCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
|
2006-02-04 05:02:27 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __sparc__
|
2006-06-27 20:35:36 +00:00
|
|
|
GCCArgs.push_back("-mcpu=v9");
|
2005-03-09 03:31:02 +00:00
|
|
|
#endif
|
2006-06-27 20:35:36 +00:00
|
|
|
GCCArgs.push_back("-o");
|
|
|
|
GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
|
|
|
|
GCCArgs.push_back("-O2"); // Optimize the program a bit.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add any arguments intended for GCC. We locate them here because this is
|
|
|
|
// most likely -L and -l options that need to come before other libraries but
|
|
|
|
// after the source. Other options won't be sensitive to placement on the
|
|
|
|
// command line, so this should be safe.
|
|
|
|
for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
|
|
|
|
GCCArgs.push_back(ArgsForGCC[i].c_str());
|
|
|
|
GCCArgs.push_back(0); // NULL terminator
|
|
|
|
|
|
|
|
|
2005-04-21 22:55:34 +00:00
|
|
|
|
2003-09-29 22:39:25 +00:00
|
|
|
std::cout << "<gcc>" << std::flush;
|
2006-06-27 20:35:36 +00:00
|
|
|
if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
|
2004-12-19 18:00:21 +00:00
|
|
|
sys::Path())) {
|
2006-06-27 20:35:36 +00:00
|
|
|
ProcessFailure(GCCPath, &GCCArgs[0]);
|
2003-10-14 21:07:25 +00:00
|
|
|
return 1;
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-10-14 21:34:11 +00:00
|
|
|
/// create - Try to find the `gcc' executable
|
2003-09-29 22:39:25 +00:00
|
|
|
///
|
2003-10-14 21:34:11 +00:00
|
|
|
GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
|
2004-12-19 18:00:21 +00:00
|
|
|
sys::Path GCCPath = FindExecutable("gcc", ProgramPath);
|
|
|
|
if (GCCPath.isEmpty()) {
|
2003-09-29 22:39:25 +00:00
|
|
|
Message = "Cannot find `gcc' in executable directory or PATH!\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-19 18:00:21 +00:00
|
|
|
Message = "Found gcc: " + GCCPath.toString() + "\n";
|
2003-09-29 22:39:25 +00:00
|
|
|
return new GCC(GCCPath);
|
|
|
|
}
|