2003-09-30 18:28:53 +00:00
|
|
|
//===-- Support/ToolRunner.h ------------------------------------*- C++ -*-===//
|
2003-10-20 20:19:47 +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-09-30 18:28:53 +00:00
|
|
|
//
|
2003-10-14 21:34:11 +00:00
|
|
|
// This file exposes an abstraction around a platform C compiler, used to
|
|
|
|
// compile C and assembly code. It also exposes an "AbstractIntepreter"
|
|
|
|
// interface, which is used to execute code using one of the LLVM execution
|
|
|
|
// engines.
|
2003-09-30 18:28:53 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-09-29 22:38:57 +00:00
|
|
|
#ifndef TOOLRUNNER_H
|
|
|
|
#define TOOLRUNNER_H
|
|
|
|
|
|
|
|
#include "Support/SystemUtils.h"
|
2004-02-19 07:39:26 +00:00
|
|
|
#include <exception>
|
2003-09-29 22:38:57 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2003-10-14 21:34:11 +00:00
|
|
|
class CBE;
|
|
|
|
class LLC;
|
2003-09-29 22:38:57 +00:00
|
|
|
|
2004-02-18 20:21:57 +00:00
|
|
|
|
|
|
|
/// ToolExecutionError - An instance of this class is thrown by the
|
|
|
|
/// AbstractInterpreter instances if there is an error running a tool (e.g., LLC
|
|
|
|
/// crashes) which prevents execution of the program.
|
|
|
|
///
|
2004-02-19 07:39:26 +00:00
|
|
|
class ToolExecutionError : std::exception {
|
2004-02-18 20:21:57 +00:00
|
|
|
std::string Message;
|
|
|
|
public:
|
2004-02-19 07:39:26 +00:00
|
|
|
explicit ToolExecutionError(const std::string &M) : Message(M) {}
|
|
|
|
virtual ~ToolExecutionError() throw();
|
|
|
|
virtual const char* what() const throw() { return Message.c_str(); }
|
2004-02-18 20:21:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-09-29 22:38:57 +00:00
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// GCC abstraction
|
|
|
|
//
|
|
|
|
class GCC {
|
|
|
|
std::string GCCPath; // The path to the gcc executable
|
|
|
|
GCC(const std::string &gccPath) : GCCPath(gccPath) { }
|
2003-10-14 21:34:11 +00:00
|
|
|
public:
|
|
|
|
enum FileType { AsmFile, CFile };
|
|
|
|
|
|
|
|
static GCC* create(const std::string &ProgramPath, std::string &Message);
|
2003-09-29 22:38:57 +00:00
|
|
|
|
2003-10-14 21:52:52 +00:00
|
|
|
/// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
|
|
|
|
/// either a .s file, or a .c file, specified by FileType), with the specified
|
|
|
|
/// arguments. Standard input is specified with InputFile, and standard
|
|
|
|
/// Output is captured to the specified OutputFile location. The SharedLibs
|
|
|
|
/// option specifies optional native shared objects that can be loaded into
|
|
|
|
/// the program for execution.
|
|
|
|
///
|
2003-10-14 21:34:11 +00:00
|
|
|
int ExecuteProgram(const std::string &ProgramFile,
|
|
|
|
const std::vector<std::string> &Args,
|
|
|
|
FileType fileType,
|
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
2003-10-14 21:52:52 +00:00
|
|
|
const std::vector<std::string> &SharedLibs =
|
|
|
|
std::vector<std::string>());
|
2003-09-29 22:38:57 +00:00
|
|
|
|
2003-10-14 21:52:52 +00:00
|
|
|
/// MakeSharedObject - This compiles the specified file (which is either a .c
|
|
|
|
/// file or a .s file) into a shared object.
|
|
|
|
///
|
|
|
|
int MakeSharedObject(const std::string &InputFile, FileType fileType,
|
2003-09-29 22:38:57 +00:00
|
|
|
std::string &OutputFile);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-10-14 21:34:11 +00:00
|
|
|
//===---------------------------------------------------------------------===//
|
2003-09-29 22:38:57 +00:00
|
|
|
/// AbstractInterpreter Class - Subclasses of this class are used to execute
|
|
|
|
/// LLVM bytecode in a variety of ways. This abstract interface hides this
|
|
|
|
/// complexity behind a simple interface.
|
|
|
|
///
|
|
|
|
struct AbstractInterpreter {
|
2004-05-04 21:09:01 +00:00
|
|
|
static CBE *createCBE(const std::string &ProgramPath, std::string &Message,
|
|
|
|
const std::vector<std::string> *Args = 0);
|
|
|
|
static LLC *createLLC(const std::string &ProgramPath, std::string &Message,
|
|
|
|
const std::vector<std::string> *Args = 0);
|
2003-10-14 21:34:11 +00:00
|
|
|
|
|
|
|
static AbstractInterpreter* createLLI(const std::string &ProgramPath,
|
2004-05-04 21:09:01 +00:00
|
|
|
std::string &Message,
|
|
|
|
const std::vector<std::string> *Args=0);
|
2003-10-14 21:34:11 +00:00
|
|
|
|
|
|
|
static AbstractInterpreter* createJIT(const std::string &ProgramPath,
|
2004-05-04 21:09:01 +00:00
|
|
|
std::string &Message,
|
|
|
|
const std::vector<std::string> *Args=0);
|
2003-10-14 21:34:11 +00:00
|
|
|
|
2003-09-29 22:38:57 +00:00
|
|
|
|
|
|
|
virtual ~AbstractInterpreter() {}
|
|
|
|
|
2004-02-18 23:24:29 +00:00
|
|
|
/// compileProgram - Compile the specified program from bytecode to executable
|
|
|
|
/// code. This does not produce any output, it is only used when debugging
|
|
|
|
/// the code generator. If the code generator fails, an exception should be
|
|
|
|
/// thrown, otherwise, this function will just return.
|
|
|
|
virtual void compileProgram(const std::string &Bytecode) {}
|
|
|
|
|
2003-09-29 22:38:57 +00:00
|
|
|
/// ExecuteProgram - Run the specified bytecode file, emitting output to the
|
|
|
|
/// specified filename. This returns the exit code of the program.
|
|
|
|
///
|
|
|
|
virtual int ExecuteProgram(const std::string &Bytecode,
|
2003-10-14 21:34:11 +00:00
|
|
|
const std::vector<std::string> &Args,
|
2003-09-29 22:38:57 +00:00
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
2003-10-14 21:52:52 +00:00
|
|
|
const std::vector<std::string> &SharedLibs =
|
|
|
|
std::vector<std::string>()) = 0;
|
2003-09-29 22:38:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// CBE Implementation of AbstractIntepreter interface
|
|
|
|
//
|
|
|
|
class CBE : public AbstractInterpreter {
|
2004-02-17 06:39:48 +00:00
|
|
|
std::string LLCPath; // The path to the `llc' executable
|
2004-05-04 21:09:01 +00:00
|
|
|
std::vector<std::string> ToolArgs; // Extra args to pass to LLC
|
2003-09-29 22:38:57 +00:00
|
|
|
GCC *gcc;
|
|
|
|
public:
|
2004-05-04 21:09:01 +00:00
|
|
|
CBE(const std::string &llcPath, GCC *Gcc,
|
|
|
|
const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
|
|
|
|
ToolArgs.clear ();
|
2004-05-04 22:02:41 +00:00
|
|
|
if (Args) { ToolArgs = *Args; }
|
2004-05-04 21:09:01 +00:00
|
|
|
}
|
2003-09-29 22:38:57 +00:00
|
|
|
~CBE() { delete gcc; }
|
|
|
|
|
2004-02-18 23:24:29 +00:00
|
|
|
/// compileProgram - Compile the specified program from bytecode to executable
|
|
|
|
/// code. This does not produce any output, it is only used when debugging
|
|
|
|
/// the code generator. If the code generator fails, an exception should be
|
|
|
|
/// thrown, otherwise, this function will just return.
|
|
|
|
virtual void compileProgram(const std::string &Bytecode);
|
|
|
|
|
2003-09-29 22:38:57 +00:00
|
|
|
virtual int ExecuteProgram(const std::string &Bytecode,
|
2003-10-14 21:34:11 +00:00
|
|
|
const std::vector<std::string> &Args,
|
2003-09-29 22:38:57 +00:00
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
2003-10-14 21:52:52 +00:00
|
|
|
const std::vector<std::string> &SharedLibs =
|
|
|
|
std::vector<std::string>());
|
2003-09-29 22:38:57 +00:00
|
|
|
|
2004-02-18 20:21:57 +00:00
|
|
|
// Sometimes we just want to go half-way and only generate the .c file, not
|
|
|
|
// necessarily compile it with GCC and run the program. This throws an
|
|
|
|
// exception if LLC crashes.
|
2003-10-14 21:34:11 +00:00
|
|
|
//
|
2004-02-18 20:21:57 +00:00
|
|
|
virtual void OutputC(const std::string &Bytecode, std::string &OutputCFile);
|
2003-09-29 22:38:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// LLC Implementation of AbstractIntepreter interface
|
|
|
|
//
|
|
|
|
class LLC : public AbstractInterpreter {
|
|
|
|
std::string LLCPath; // The path to the LLC executable
|
2004-05-04 21:09:01 +00:00
|
|
|
std::vector<std::string> ToolArgs; // Extra args to pass to LLC
|
2003-09-29 22:38:57 +00:00
|
|
|
GCC *gcc;
|
|
|
|
public:
|
2004-05-04 21:09:01 +00:00
|
|
|
LLC(const std::string &llcPath, GCC *Gcc,
|
|
|
|
const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
|
|
|
|
ToolArgs.clear ();
|
2004-05-04 22:02:41 +00:00
|
|
|
if (Args) { ToolArgs = *Args; }
|
2004-05-04 21:09:01 +00:00
|
|
|
}
|
2003-09-29 22:38:57 +00:00
|
|
|
~LLC() { delete gcc; }
|
|
|
|
|
2004-02-18 23:24:29 +00:00
|
|
|
/// compileProgram - Compile the specified program from bytecode to executable
|
|
|
|
/// code. This does not produce any output, it is only used when debugging
|
|
|
|
/// the code generator. If the code generator fails, an exception should be
|
|
|
|
/// thrown, otherwise, this function will just return.
|
|
|
|
virtual void compileProgram(const std::string &Bytecode);
|
|
|
|
|
2003-09-29 22:38:57 +00:00
|
|
|
virtual int ExecuteProgram(const std::string &Bytecode,
|
2003-10-14 21:34:11 +00:00
|
|
|
const std::vector<std::string> &Args,
|
2003-09-29 22:38:57 +00:00
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
2003-10-14 21:52:52 +00:00
|
|
|
const std::vector<std::string> &SharedLibs =
|
|
|
|
std::vector<std::string>());
|
2003-09-29 22:38:57 +00:00
|
|
|
|
2003-10-14 21:34:11 +00:00
|
|
|
// Sometimes we just want to go half-way and only generate the .s file,
|
2004-02-18 20:21:57 +00:00
|
|
|
// not necessarily compile it all the way and run the program. This throws
|
|
|
|
// an exception if execution of LLC fails.
|
2003-10-14 21:34:11 +00:00
|
|
|
//
|
2004-02-18 20:21:57 +00:00
|
|
|
void OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile);
|
2003-09-29 22:38:57 +00:00
|
|
|
};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2003-09-29 22:38:57 +00:00
|
|
|
#endif
|