blob: d232cd2e97df84c42f6133ecbb9ba74d031f90c7 [file] [log] [blame]
Reid Spencer551ccae2004-09-01 22:55:40 +00001//===-- llvm/Support/ToolRunner.h -------------------------------*- C++ -*-===//
Misha Brukman63b3afa2005-04-21 20:48:15 +00002//
John Criswell6fbcc262003-10-20 20:19:47 +00003// 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.
Misha Brukman63b3afa2005-04-21 20:48:15 +00007//
John Criswell6fbcc262003-10-20 20:19:47 +00008//===----------------------------------------------------------------------===//
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 {
Reid Spencer2418bf92004-12-19 17:59:45 +000047 sys::Path GCCPath; // The path to the gcc executable
48 GCC(const sys::Path &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,
Reid Spencer51ab5c82006-06-06 00:00:42 +000066 const std::vector<std::string> &GCCArgs =
67 std::vector<std::string>(),
68 unsigned Timeout = 0);
Misha Brukman29afb642003-09-29 22:38:57 +000069
Chris Lattnereeed9832003-10-14 21:52:52 +000070 /// MakeSharedObject - This compiles the specified file (which is either a .c
71 /// file or a .s file) into a shared object.
72 ///
73 int MakeSharedObject(const std::string &InputFile, FileType fileType,
Misha Brukman29afb642003-09-29 22:38:57 +000074 std::string &OutputFile);
Misha Brukman29afb642003-09-29 22:38:57 +000075};
76
Misha Brukman29afb642003-09-29 22:38:57 +000077
Chris Lattner7915a1e2003-10-14 21:34:11 +000078//===---------------------------------------------------------------------===//
Misha Brukman29afb642003-09-29 22:38:57 +000079/// AbstractInterpreter Class - Subclasses of this class are used to execute
80/// LLVM bytecode in a variety of ways. This abstract interface hides this
81/// complexity behind a simple interface.
82///
Jeff Cohen83881952005-01-22 16:30:58 +000083class AbstractInterpreter {
84public:
Brian Gaeked11577b2004-05-04 21:09:01 +000085 static CBE *createCBE(const std::string &ProgramPath, std::string &Message,
86 const std::vector<std::string> *Args = 0);
87 static LLC *createLLC(const std::string &ProgramPath, std::string &Message,
88 const std::vector<std::string> *Args = 0);
Chris Lattner7915a1e2003-10-14 21:34:11 +000089
90 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +000091 std::string &Message,
92 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +000093
94 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
Brian Gaeked11577b2004-05-04 21:09:01 +000095 std::string &Message,
96 const std::vector<std::string> *Args=0);
Chris Lattner7915a1e2003-10-14 21:34:11 +000097
Misha Brukman29afb642003-09-29 22:38:57 +000098
99 virtual ~AbstractInterpreter() {}
100
Chris Lattnerf03715c2004-02-18 23:24:29 +0000101 /// compileProgram - Compile the specified program from bytecode to executable
102 /// code. This does not produce any output, it is only used when debugging
103 /// the code generator. If the code generator fails, an exception should be
104 /// thrown, otherwise, this function will just return.
105 virtual void compileProgram(const std::string &Bytecode) {}
106
Misha Brukman29afb642003-09-29 22:38:57 +0000107 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
108 /// specified filename. This returns the exit code of the program.
109 ///
110 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000111 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000112 const std::string &InputFile,
113 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000114 const std::vector<std::string> &GCCArgs =
115 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000116 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000117 std::vector<std::string>(),
118 unsigned Timeout = 0) = 0;
Misha Brukman29afb642003-09-29 22:38:57 +0000119};
120
121//===---------------------------------------------------------------------===//
122// CBE Implementation of AbstractIntepreter interface
123//
124class CBE : public AbstractInterpreter {
Reid Spencer2418bf92004-12-19 17:59:45 +0000125 sys::Path LLCPath; // The path to the `llc' executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000126 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
Misha Brukman29afb642003-09-29 22:38:57 +0000127 GCC *gcc;
128public:
Reid Spencer2418bf92004-12-19 17:59:45 +0000129 CBE(const sys::Path &llcPath, GCC *Gcc,
Brian Gaeked11577b2004-05-04 21:09:01 +0000130 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
131 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000132 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000133 }
Misha Brukman29afb642003-09-29 22:38:57 +0000134 ~CBE() { delete gcc; }
135
Chris Lattnerf03715c2004-02-18 23:24:29 +0000136 /// compileProgram - Compile the specified program from bytecode to executable
137 /// code. This does not produce any output, it is only used when debugging
138 /// the code generator. If the code generator fails, an exception should be
139 /// thrown, otherwise, this function will just return.
140 virtual void compileProgram(const std::string &Bytecode);
141
Misha Brukman29afb642003-09-29 22:38:57 +0000142 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000143 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000144 const std::string &InputFile,
145 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000146 const std::vector<std::string> &GCCArgs =
147 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000148 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000149 std::vector<std::string>(),
150 unsigned Timeout = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000151
Chris Lattner8c56be52004-02-18 20:21:57 +0000152 // Sometimes we just want to go half-way and only generate the .c file, not
153 // necessarily compile it with GCC and run the program. This throws an
154 // exception if LLC crashes.
Chris Lattner7915a1e2003-10-14 21:34:11 +0000155 //
Reid Spencer9ac14182004-12-16 23:01:34 +0000156 virtual void OutputC(const std::string &Bytecode, sys::Path& OutputCFile);
Misha Brukman29afb642003-09-29 22:38:57 +0000157};
158
Misha Brukman29afb642003-09-29 22:38:57 +0000159
160//===---------------------------------------------------------------------===//
161// LLC Implementation of AbstractIntepreter interface
162//
163class LLC : public AbstractInterpreter {
164 std::string LLCPath; // The path to the LLC executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000165 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
Misha Brukman29afb642003-09-29 22:38:57 +0000166 GCC *gcc;
167public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000168 LLC(const std::string &llcPath, GCC *Gcc,
169 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
170 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000171 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000172 }
Misha Brukman29afb642003-09-29 22:38:57 +0000173 ~LLC() { delete gcc; }
174
Chris Lattnerf03715c2004-02-18 23:24:29 +0000175 /// compileProgram - Compile the specified program from bytecode to executable
176 /// code. This does not produce any output, it is only used when debugging
177 /// the code generator. If the code generator fails, an exception should be
178 /// thrown, otherwise, this function will just return.
179 virtual void compileProgram(const std::string &Bytecode);
180
Misha Brukman29afb642003-09-29 22:38:57 +0000181 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000182 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000183 const std::string &InputFile,
184 const std::string &OutputFile,
Reid Spencer51ab5c82006-06-06 00:00:42 +0000185 const std::vector<std::string> &GCCArgs =
186 std::vector<std::string>(),
Misha Brukman63b3afa2005-04-21 20:48:15 +0000187 const std::vector<std::string> &SharedLibs =
Chris Lattner62c91fc2004-07-24 07:48:50 +0000188 std::vector<std::string>(),
189 unsigned Timeout = 0);
Misha Brukman29afb642003-09-29 22:38:57 +0000190
Chris Lattner7915a1e2003-10-14 21:34:11 +0000191 // Sometimes we just want to go half-way and only generate the .s file,
Chris Lattner8c56be52004-02-18 20:21:57 +0000192 // not necessarily compile it all the way and run the program. This throws
193 // an exception if execution of LLC fails.
Chris Lattner7915a1e2003-10-14 21:34:11 +0000194 //
Reid Spencer9ac14182004-12-16 23:01:34 +0000195 void OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile);
Misha Brukman29afb642003-09-29 22:38:57 +0000196};
197
Brian Gaeked0fde302003-11-11 22:41:34 +0000198} // End llvm namespace
199
Misha Brukman29afb642003-09-29 22:38:57 +0000200#endif