blob: c5d89da46df27cb73360145a59c5de7d8ee953fb [file] [log] [blame]
Reid Spencer551ccae2004-09-01 22:55:40 +00001//===-- llvm/Support/ToolRunner.h -------------------------------*- C++ -*-===//
John Criswell6fbcc262003-10-20 20:19:47 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerf4744492003-09-30 18:28:53 +00009//
Chris Lattner7915a1e2003-10-14 21:34:11 +000010// This file exposes an abstraction around a platform C compiler, used to
11// compile C and assembly code. It also exposes an "AbstractIntepreter"
12// interface, which is used to execute code using one of the LLVM execution
13// engines.
Chris Lattnerf4744492003-09-30 18:28:53 +000014//
15//===----------------------------------------------------------------------===//
16
Reid Spencer551ccae2004-09-01 22:55:40 +000017#ifndef LLVM_SUPPORT_TOOLRUNNER_H
18#define LLVM_SUPPORT_TOOLRUNNER_H
Misha Brukman29afb642003-09-29 22:38:57 +000019
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/SystemUtils.h"
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000021#include <exception>
Misha Brukman29afb642003-09-29 22:38:57 +000022#include <vector>
23
Brian Gaeked0fde302003-11-11 22:41:34 +000024namespace llvm {
25
Chris Lattner7915a1e2003-10-14 21:34:11 +000026class CBE;
27class LLC;
Misha Brukman29afb642003-09-29 22:38:57 +000028
Chris Lattner8c56be52004-02-18 20:21:57 +000029
30/// ToolExecutionError - An instance of this class is thrown by the
31/// AbstractInterpreter instances if there is an error running a tool (e.g., LLC
32/// crashes) which prevents execution of the program.
33///
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000034class ToolExecutionError : std::exception {
Chris Lattner8c56be52004-02-18 20:21:57 +000035 std::string Message;
36public:
Alkis Evlogimenos1d29a6d2004-02-19 07:39:26 +000037 explicit ToolExecutionError(const std::string &M) : Message(M) {}
38 virtual ~ToolExecutionError() throw();
39 virtual const char* what() const throw() { return Message.c_str(); }
Chris Lattner8c56be52004-02-18 20:21:57 +000040};
41
42
Misha Brukman29afb642003-09-29 22:38:57 +000043//===---------------------------------------------------------------------===//
44// GCC abstraction
45//
Misha Brukman29afb642003-09-29 22:38:57 +000046class GCC {
47 std::string GCCPath; // The path to the gcc executable
Misha Brukman29afb642003-09-29 22:38:57 +000048 GCC(const std::string &gccPath) : GCCPath(gccPath) { }
Chris Lattner7915a1e2003-10-14 21:34:11 +000049public:
50 enum FileType { AsmFile, CFile };
Misha Brukman29afb642003-09-29 22:38:57 +000051
Chris Lattner7915a1e2003-10-14 21:34:11 +000052 static GCC* create(const std::string &ProgramPath, std::string &Message);
53
Chris Lattnereeed9832003-10-14 21:52:52 +000054 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
55 /// either a .s file, or a .c file, specified by FileType), with the specified
56 /// arguments. Standard input is specified with InputFile, and standard
57 /// Output is captured to the specified OutputFile location. The SharedLibs
58 /// option specifies optional native shared objects that can be loaded into
59 /// the program for execution.
60 ///
Chris Lattner7915a1e2003-10-14 21:34:11 +000061 int ExecuteProgram(const std::string &ProgramFile,
62 const std::vector<std::string> &Args,
63 FileType fileType,
64 const std::string &InputFile,
65 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +000066 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +000067 std::vector<std::string>(), unsigned Timeout = 0);
Misha Brukman29afb642003-09-29 22:38:57 +000068
Chris Lattnereeed9832003-10-14 21:52:52 +000069 /// MakeSharedObject - This compiles the specified file (which is either a .c
70 /// file or a .s file) into a shared object.
71 ///
72 int MakeSharedObject(const std::string &InputFile, FileType fileType,
Misha Brukman29afb642003-09-29 22:38:57 +000073 std::string &OutputFile);
Misha Brukman29afb642003-09-29 22:38:57 +000074};
75
Misha Brukman29afb642003-09-29 22:38:57 +000076
Chris Lattner7915a1e2003-10-14 21:34:11 +000077//===---------------------------------------------------------------------===//
Misha Brukman29afb642003-09-29 22:38:57 +000078/// AbstractInterpreter Class - Subclasses of this class are used to execute
79/// LLVM bytecode in a variety of ways. This abstract interface hides this
80/// complexity behind a simple interface.
81///
82struct AbstractInterpreter {
Brian Gaeked11577b2004-05-04 21:09:01 +000083 static CBE *createCBE(const std::string &ProgramPath, std::string &Message,
84 const std::vector<std::string> *Args = 0);
85 static LLC *createLLC(const std::string &ProgramPath, std::string &Message,
86 const std::vector<std::string> *Args = 0);
Chris Lattner7915a1e2003-10-14 21:34:11 +000087
88 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +000089 std::string &Message,
90 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +000091
92 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +000093 std::string &Message,
94 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +000095
Misha Brukman29afb642003-09-29 22:38:57 +000096
97 virtual ~AbstractInterpreter() {}
98
Chris Lattnerf03715c2004-02-18 23:24:29 +000099 /// compileProgram - Compile the specified program from bytecode to executable
100 /// code. This does not produce any output, it is only used when debugging
101 /// the code generator. If the code generator fails, an exception should be
102 /// thrown, otherwise, this function will just return.
103 virtual void compileProgram(const std::string &Bytecode) {}
104
Misha Brukman29afb642003-09-29 22:38:57 +0000105 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
106 /// specified filename. This returns the exit code of the program.
107 ///
108 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000109 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000110 const std::string &InputFile,
111 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000112 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000113 std::vector<std::string>(),
114 unsigned Timeout = 0) = 0;
Misha Brukman29afb642003-09-29 22:38:57 +0000115};
116
117//===---------------------------------------------------------------------===//
118// CBE Implementation of AbstractIntepreter interface
119//
120class CBE : public AbstractInterpreter {
Chris Lattnerdf2cf412004-02-17 06:39:48 +0000121 std::string LLCPath; // The path to the `llc' executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000122 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
Misha Brukman29afb642003-09-29 22:38:57 +0000123 GCC *gcc;
124public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000125 CBE(const std::string &llcPath, GCC *Gcc,
126 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
127 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000128 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000129 }
Misha Brukman29afb642003-09-29 22:38:57 +0000130 ~CBE() { delete gcc; }
131
Chris Lattnerf03715c2004-02-18 23:24:29 +0000132 /// compileProgram - Compile the specified program from bytecode to executable
133 /// code. This does not produce any output, it is only used when debugging
134 /// the code generator. If the code generator fails, an exception should be
135 /// thrown, otherwise, this function will just return.
136 virtual void compileProgram(const std::string &Bytecode);
137
Misha Brukman29afb642003-09-29 22:38:57 +0000138 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000139 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000140 const std::string &InputFile,
141 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000142 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000143 std::vector<std::string>(),
144 unsigned Timeout = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000145
Chris Lattner8c56be52004-02-18 20:21:57 +0000146 // Sometimes we just want to go half-way and only generate the .c file, not
147 // necessarily compile it with GCC and run the program. This throws an
148 // exception if LLC crashes.
Chris Lattner7915a1e2003-10-14 21:34:11 +0000149 //
Chris Lattner8c56be52004-02-18 20:21:57 +0000150 virtual void OutputC(const std::string &Bytecode, std::string &OutputCFile);
Misha Brukman29afb642003-09-29 22:38:57 +0000151};
152
Misha Brukman29afb642003-09-29 22:38:57 +0000153
154//===---------------------------------------------------------------------===//
155// LLC Implementation of AbstractIntepreter interface
156//
157class LLC : public AbstractInterpreter {
158 std::string LLCPath; // The path to the LLC executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000159 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
Misha Brukman29afb642003-09-29 22:38:57 +0000160 GCC *gcc;
161public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000162 LLC(const std::string &llcPath, GCC *Gcc,
163 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
164 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000165 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000166 }
Misha Brukman29afb642003-09-29 22:38:57 +0000167 ~LLC() { delete gcc; }
168
Chris Lattnerf03715c2004-02-18 23:24:29 +0000169 /// compileProgram - Compile the specified program from bytecode to executable
170 /// code. This does not produce any output, it is only used when debugging
171 /// the code generator. If the code generator fails, an exception should be
172 /// thrown, otherwise, this function will just return.
173 virtual void compileProgram(const std::string &Bytecode);
174
Misha Brukman29afb642003-09-29 22:38:57 +0000175 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000176 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000177 const std::string &InputFile,
178 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000179 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000180 std::vector<std::string>(),
181 unsigned Timeout = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000182
Chris Lattner7915a1e2003-10-14 21:34:11 +0000183 // Sometimes we just want to go half-way and only generate the .s file,
Chris Lattner8c56be52004-02-18 20:21:57 +0000184 // not necessarily compile it all the way and run the program. This throws
185 // an exception if execution of LLC fails.
Chris Lattner7915a1e2003-10-14 21:34:11 +0000186 //
Chris Lattner8c56be52004-02-18 20:21:57 +0000187 void OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile);
Misha Brukman29afb642003-09-29 22:38:57 +0000188};
189
Brian Gaeked0fde302003-11-11 22:41:34 +0000190} // End llvm namespace
191
Misha Brukman29afb642003-09-29 22:38:57 +0000192#endif