1 //===-- tools/bugpoint/ToolRunner.h -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // 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
15 //===----------------------------------------------------------------------===//
17 #ifndef BUGPOINT_TOOLRUNNER_H
18 #define BUGPOINT_TOOLRUNNER_H
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/SystemUtils.h"
28 extern cl::opt
<bool> SaveTemps
;
29 extern Triple TargetTriple
;
34 /// ToolExecutionError - An instance of this class is thrown by the
35 /// AbstractInterpreter instances if there is an error running a tool (e.g., LLC
36 /// crashes) which prevents execution of the program.
38 class ToolExecutionError
: std::exception
{
41 explicit ToolExecutionError(const std::string
&M
) : Message(M
) {}
42 virtual ~ToolExecutionError() throw();
43 virtual const char* what() const throw() { return Message
.c_str(); }
47 //===---------------------------------------------------------------------===//
51 sys::Path GCCPath
; // The path to the gcc executable.
52 sys::Path RemoteClientPath
; // The path to the rsh / ssh executable.
53 std::vector
<std::string
> gccArgs
; // GCC-specific arguments.
54 GCC(const sys::Path
&gccPath
, const sys::Path
&RemotePath
,
55 const std::vector
<std::string
> *GCCArgs
)
56 : GCCPath(gccPath
), RemoteClientPath(RemotePath
) {
57 if (GCCArgs
) gccArgs
= *GCCArgs
;
60 enum FileType
{ AsmFile
, CFile
};
62 static GCC
*create(std::string
&Message
,
63 const std::vector
<std::string
> *Args
);
65 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
66 /// either a .s file, or a .c file, specified by FileType), with the specified
67 /// arguments. Standard input is specified with InputFile, and standard
68 /// Output is captured to the specified OutputFile location. The SharedLibs
69 /// option specifies optional native shared objects that can be loaded into
70 /// the program for execution.
72 int ExecuteProgram(const std::string
&ProgramFile
,
73 const std::vector
<std::string
> &Args
,
75 const std::string
&InputFile
,
76 const std::string
&OutputFile
,
77 const std::vector
<std::string
> &GCCArgs
=
78 std::vector
<std::string
>(),
80 unsigned MemoryLimit
= 0);
82 /// MakeSharedObject - This compiles the specified file (which is either a .c
83 /// file or a .s file) into a shared object.
85 int MakeSharedObject(const std::string
&InputFile
, FileType fileType
,
86 std::string
&OutputFile
,
87 const std::vector
<std::string
> &ArgsForGCC
);
91 //===---------------------------------------------------------------------===//
92 /// AbstractInterpreter Class - Subclasses of this class are used to execute
93 /// LLVM bitcode in a variety of ways. This abstract interface hides this
94 /// complexity behind a simple interface.
96 class AbstractInterpreter
{
98 static CBE
*createCBE(const char *Argv0
, std::string
&Message
,
99 const std::vector
<std::string
> *Args
= 0,
100 const std::vector
<std::string
> *GCCArgs
= 0);
101 static LLC
*createLLC(const char *Argv0
, std::string
&Message
,
102 const std::vector
<std::string
> *Args
= 0,
103 const std::vector
<std::string
> *GCCArgs
= 0);
105 static AbstractInterpreter
* createLLI(const char *Argv0
, std::string
&Message
,
106 const std::vector
<std::string
> *Args
=0);
108 static AbstractInterpreter
* createJIT(const char *Argv0
, std::string
&Message
,
109 const std::vector
<std::string
> *Args
=0);
111 static AbstractInterpreter
* createCustom(std::string
&Message
,
112 const std::string
&ExecCommandLine
);
115 virtual ~AbstractInterpreter() {}
117 /// compileProgram - Compile the specified program from bitcode to executable
118 /// code. This does not produce any output, it is only used when debugging
119 /// the code generator. If the code generator fails, an exception should be
120 /// thrown, otherwise, this function will just return.
121 virtual void compileProgram(const std::string
&Bitcode
) {}
123 /// OutputCode - Compile the specified program from bitcode to code
124 /// understood by the GCC driver (either C or asm). If the code generator
125 /// fails, an exception should be thrown, otherwise, this function returns the
126 /// type of code emitted.
127 virtual GCC::FileType
OutputCode(const std::string
&Bitcode
,
128 sys::Path
&OutFile
) {
129 throw std::string("OutputCode not supported by this AbstractInterpreter!");
132 /// ExecuteProgram - Run the specified bitcode file, emitting output to the
133 /// specified filename. This returns the exit code of the program.
135 virtual int ExecuteProgram(const std::string
&Bitcode
,
136 const std::vector
<std::string
> &Args
,
137 const std::string
&InputFile
,
138 const std::string
&OutputFile
,
139 const std::vector
<std::string
> &GCCArgs
=
140 std::vector
<std::string
>(),
141 const std::vector
<std::string
> &SharedLibs
=
142 std::vector
<std::string
>(),
143 unsigned Timeout
= 0,
144 unsigned MemoryLimit
= 0) = 0;
147 //===---------------------------------------------------------------------===//
148 // CBE Implementation of AbstractIntepreter interface
150 class CBE
: public AbstractInterpreter
{
151 sys::Path LLCPath
; // The path to the `llc' executable.
152 std::vector
<std::string
> ToolArgs
; // Extra args to pass to LLC.
155 CBE(const sys::Path
&llcPath
, GCC
*Gcc
,
156 const std::vector
<std::string
> *Args
)
157 : LLCPath(llcPath
), gcc(Gcc
) {
159 if (Args
) ToolArgs
= *Args
;
161 ~CBE() { delete gcc
; }
163 /// compileProgram - Compile the specified program from bitcode to executable
164 /// code. This does not produce any output, it is only used when debugging
165 /// the code generator. If the code generator fails, an exception should be
166 /// thrown, otherwise, this function will just return.
167 virtual void compileProgram(const std::string
&Bitcode
);
169 virtual int ExecuteProgram(const std::string
&Bitcode
,
170 const std::vector
<std::string
> &Args
,
171 const std::string
&InputFile
,
172 const std::string
&OutputFile
,
173 const std::vector
<std::string
> &GCCArgs
=
174 std::vector
<std::string
>(),
175 const std::vector
<std::string
> &SharedLibs
=
176 std::vector
<std::string
>(),
177 unsigned Timeout
= 0,
178 unsigned MemoryLimit
= 0);
180 /// OutputCode - Compile the specified program from bitcode to code
181 /// understood by the GCC driver (either C or asm). If the code generator
182 /// fails, an exception should be thrown, otherwise, this function returns the
183 /// type of code emitted.
184 virtual GCC::FileType
OutputCode(const std::string
&Bitcode
,
189 //===---------------------------------------------------------------------===//
190 // LLC Implementation of AbstractIntepreter interface
192 class LLC
: public AbstractInterpreter
{
193 std::string LLCPath
; // The path to the LLC executable.
194 std::vector
<std::string
> ToolArgs
; // Extra args to pass to LLC.
195 std::vector
<std::string
> gccArgs
; // Extra args to pass to GCC.
198 LLC(const std::string
&llcPath
, GCC
*Gcc
,
199 const std::vector
<std::string
> *Args
,
200 const std::vector
<std::string
> *GCCArgs
)
201 : LLCPath(llcPath
), gcc(Gcc
) {
203 if (Args
) ToolArgs
= *Args
;
204 if (GCCArgs
) gccArgs
= *GCCArgs
;
206 ~LLC() { delete gcc
; }
208 /// compileProgram - Compile the specified program from bitcode to executable
209 /// code. This does not produce any output, it is only used when debugging
210 /// the code generator. If the code generator fails, an exception should be
211 /// thrown, otherwise, this function will just return.
212 virtual void compileProgram(const std::string
&Bitcode
);
214 virtual int ExecuteProgram(const std::string
&Bitcode
,
215 const std::vector
<std::string
> &Args
,
216 const std::string
&InputFile
,
217 const std::string
&OutputFile
,
218 const std::vector
<std::string
> &GCCArgs
=
219 std::vector
<std::string
>(),
220 const std::vector
<std::string
> &SharedLibs
=
221 std::vector
<std::string
>(),
222 unsigned Timeout
= 0,
223 unsigned MemoryLimit
= 0);
225 virtual GCC::FileType
OutputCode(const std::string
&Bitcode
,
230 } // End llvm namespace