2003-10-14 21:34:11 +00:00
|
|
|
//===-- ToolRunner.cpp ----------------------------------------------------===//
|
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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
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"
|
2003-10-06 18:37:24 +00:00
|
|
|
#include "llvm/Support/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>
|
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;
|
|
|
|
|
|
|
|
return
|
|
|
|
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");
|
|
|
|
ErrorFilename.makeUnique();
|
2004-12-19 18:00:21 +00:00
|
|
|
RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename,
|
|
|
|
ErrorFilename);
|
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();
|
|
|
|
}
|
|
|
|
|
2004-12-16 23:01:34 +00:00
|
|
|
ErrorFilename.destroyFile();
|
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
|
|
|
}
|
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,
|
|
|
|
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,
|
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
|
|
|
|
|
|
|
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],
|
|
|
|
sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
|
|
|
|
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
|
|
|
|
//
|
2004-12-16 23:01:34 +00:00
|
|
|
void LLC::OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile) {
|
2004-12-15 01:51:56 +00:00
|
|
|
sys::Path uniqueFile(Bytecode+".llc.s");
|
|
|
|
uniqueFile.makeUnique();
|
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";
|
|
|
|
);
|
2004-12-19 18:00:21 +00:00
|
|
|
if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
|
|
|
|
sys::Path(), sys::Path(), sys::Path()))
|
|
|
|
ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]);
|
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;
|
2004-02-18 23:24:41 +00:00
|
|
|
OutputAsm(Bytecode, OutputAsmFile);
|
2004-12-16 23:01:34 +00:00
|
|
|
OutputAsmFile.destroyFile();
|
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,
|
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;
|
2004-02-18 20:21:57 +00:00
|
|
|
OutputAsm(Bytecode, OutputAsmFile);
|
|
|
|
FileRemover OutFileRemover(OutputAsmFile);
|
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,
|
2004-07-24 07:49:11 +00:00
|
|
|
InputFile, OutputFile, SharedLibs, 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
|
|
|
}
|
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,
|
|
|
|
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 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,
|
2004-07-24 07:49:11 +00:00
|
|
|
const std::vector<std::string> &SharedLibs,
|
|
|
|
unsigned Timeout) {
|
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],
|
|
|
|
sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-12-16 23:01:34 +00:00
|
|
|
void CBE::OutputC(const std::string &Bytecode, sys::Path& OutputCFile) {
|
2004-12-15 01:51:56 +00:00
|
|
|
sys::Path uniqueFile(Bytecode+".cbe.c");
|
|
|
|
uniqueFile.makeUnique();
|
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";
|
|
|
|
);
|
2004-12-19 18:00:21 +00:00
|
|
|
if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
|
|
|
|
sys::Path()))
|
2004-05-04 21:09:01 +00:00
|
|
|
ProcessFailure(LLCPath, &LLCArgs[0]);
|
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;
|
2004-02-18 23:24:41 +00:00
|
|
|
OutputC(Bytecode, OutputCFile);
|
2004-12-16 23:01:34 +00:00
|
|
|
OutputCFile.destroyFile();
|
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,
|
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;
|
2004-02-18 20:21:57 +00:00
|
|
|
OutputC(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
|
|
|
|
2004-12-16 23:01:34 +00:00
|
|
|
return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile,
|
2004-07-24 07:49:11 +00:00
|
|
|
InputFile, OutputFile, SharedLibs, 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()) {
|
2003-09-29 22:39:25 +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,
|
2004-07-24 07:49:11 +00:00
|
|
|
const std::vector<std::string> &SharedLibs,
|
|
|
|
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 the shared libraries to link in...
|
|
|
|
for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
|
|
|
|
GCCArgs.push_back(SharedLibs[i].c_str());
|
|
|
|
|
|
|
|
// 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");
|
|
|
|
}
|
|
|
|
GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
|
|
|
|
GCCArgs.push_back("-o");
|
2004-12-15 01:51:56 +00:00
|
|
|
sys::Path OutputBinary (ProgramFile+".gcc.exe");
|
|
|
|
OutputBinary.makeUnique();
|
2003-09-29 22:39:25 +00:00
|
|
|
GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
|
|
|
|
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
|
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],
|
|
|
|
sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
|
|
|
|
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,
|
2003-09-29 22:39:25 +00:00
|
|
|
std::string &OutputFile) {
|
2004-12-15 01:51:56 +00:00
|
|
|
sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
|
|
|
|
uniqueFilename.makeUnique();
|
|
|
|
OutputFile = uniqueFilename.toString();
|
|
|
|
|
2003-09-29 22:39:25 +00:00
|
|
|
// Compile the C/asm file into a shared object
|
|
|
|
const char* GCCArgs[] = {
|
|
|
|
GCCPath.c_str(),
|
|
|
|
"-x", (fileType == AsmFile) ? "assembler" : "c",
|
|
|
|
"-fno-strict-aliasing",
|
|
|
|
InputFile.c_str(), // Specify the input filename...
|
|
|
|
#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
|
|
|
|
"-G", // Compile a shared library, `-G' for Sparc
|
2004-07-16 19:45:45 +00:00
|
|
|
#elif (defined(__POWERPC__) || defined(__ppc__)) && defined(__APPLE__)
|
2004-10-17 23:03:32 +00:00
|
|
|
"-single_module", // link all source files into a single module
|
2004-07-16 19:45:45 +00:00
|
|
|
"-dynamiclib", // `-dynamiclib' for MacOS X/PowerPC
|
2004-07-19 06:03:51 +00:00
|
|
|
"-undefined", // in data segment, rather than generating
|
2004-07-19 06:00:17 +00:00
|
|
|
"dynamic_lookup", // blocks. dynamic_lookup requires that you set
|
|
|
|
// MACOSX_DEPLOYMENT_TARGET=10.3 in your env.
|
2004-07-16 19:45:45 +00:00
|
|
|
#else
|
2003-09-29 22:39:25 +00:00
|
|
|
"-shared", // `-shared' for Linux/X86, maybe others
|
|
|
|
#endif
|
|
|
|
"-o", OutputFile.c_str(), // Output to the right filename...
|
|
|
|
"-O2", // Optimize the program a bit...
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
std::cout << "<gcc>" << std::flush;
|
2004-12-19 18:00:21 +00:00
|
|
|
if (RunProgramWithTimeout(GCCPath, GCCArgs, sys::Path(), sys::Path(),
|
|
|
|
sys::Path())) {
|
2004-02-18 20:38:00 +00:00
|
|
|
ProcessFailure(GCCPath, GCCArgs);
|
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);
|
|
|
|
}
|