blob: 9d1755a2982789215c4e97796cfc2cb799d5b5e2 [file] [log] [blame]
Chris Lattnerf4744492003-09-30 18:28:53 +00001//===-- 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
Misha Brukman29afb642003-09-29 22:38:57 +000017#ifndef TOOLRUNNER_H
18#define TOOLRUNNER_H
19
Misha Brukman29afb642003-09-29 22:38:57 +000020#include "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 =
67 std::vector<std::string>());
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 =
113 std::vector<std::string>()) = 0;
Misha Brukman29afb642003-09-29 22:38:57 +0000114};
115
116//===---------------------------------------------------------------------===//
117// CBE Implementation of AbstractIntepreter interface
118//
119class CBE : public AbstractInterpreter {
Chris Lattnerdf2cf412004-02-17 06:39:48 +0000120 std::string LLCPath; // The path to the `llc' executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000121 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
Misha Brukman29afb642003-09-29 22:38:57 +0000122 GCC *gcc;
123public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000124 CBE(const std::string &llcPath, GCC *Gcc,
125 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
126 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000127 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000128 }
Misha Brukman29afb642003-09-29 22:38:57 +0000129 ~CBE() { delete gcc; }
130
Chris Lattnerf03715c2004-02-18 23:24:29 +0000131 /// compileProgram - Compile the specified program from bytecode to executable
132 /// code. This does not produce any output, it is only used when debugging
133 /// the code generator. If the code generator fails, an exception should be
134 /// thrown, otherwise, this function will just return.
135 virtual void compileProgram(const std::string &Bytecode);
136
Misha Brukman29afb642003-09-29 22:38:57 +0000137 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000138 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000139 const std::string &InputFile,
140 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000141 const std::vector<std::string> &SharedLibs =
142 std::vector<std::string>());
Misha Brukman29afb642003-09-29 22:38:57 +0000143
Chris Lattner8c56be52004-02-18 20:21:57 +0000144 // Sometimes we just want to go half-way and only generate the .c file, not
145 // necessarily compile it with GCC and run the program. This throws an
146 // exception if LLC crashes.
Chris Lattner7915a1e2003-10-14 21:34:11 +0000147 //
Chris Lattner8c56be52004-02-18 20:21:57 +0000148 virtual void OutputC(const std::string &Bytecode, std::string &OutputCFile);
Misha Brukman29afb642003-09-29 22:38:57 +0000149};
150
Misha Brukman29afb642003-09-29 22:38:57 +0000151
152//===---------------------------------------------------------------------===//
153// LLC Implementation of AbstractIntepreter interface
154//
155class LLC : public AbstractInterpreter {
156 std::string LLCPath; // The path to the LLC executable
Brian Gaeked11577b2004-05-04 21:09:01 +0000157 std::vector<std::string> ToolArgs; // Extra args to pass to LLC
Misha Brukman29afb642003-09-29 22:38:57 +0000158 GCC *gcc;
159public:
Brian Gaeked11577b2004-05-04 21:09:01 +0000160 LLC(const std::string &llcPath, GCC *Gcc,
161 const std::vector<std::string> *Args) : LLCPath(llcPath), gcc(Gcc) {
162 ToolArgs.clear ();
Brian Gaeke48b008d2004-05-04 22:02:41 +0000163 if (Args) { ToolArgs = *Args; }
Brian Gaeked11577b2004-05-04 21:09:01 +0000164 }
Misha Brukman29afb642003-09-29 22:38:57 +0000165 ~LLC() { delete gcc; }
166
Chris Lattnerf03715c2004-02-18 23:24:29 +0000167 /// compileProgram - Compile the specified program from bytecode to executable
168 /// code. This does not produce any output, it is only used when debugging
169 /// the code generator. If the code generator fails, an exception should be
170 /// thrown, otherwise, this function will just return.
171 virtual void compileProgram(const std::string &Bytecode);
172
Misha Brukman29afb642003-09-29 22:38:57 +0000173 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000174 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000175 const std::string &InputFile,
176 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000177 const std::vector<std::string> &SharedLibs =
178 std::vector<std::string>());
Misha Brukman29afb642003-09-29 22:38:57 +0000179
Chris Lattner7915a1e2003-10-14 21:34:11 +0000180 // Sometimes we just want to go half-way and only generate the .s file,
Chris Lattner8c56be52004-02-18 20:21:57 +0000181 // not necessarily compile it all the way and run the program. This throws
182 // an exception if execution of LLC fails.
Chris Lattner7915a1e2003-10-14 21:34:11 +0000183 //
Chris Lattner8c56be52004-02-18 20:21:57 +0000184 void OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile);
Misha Brukman29afb642003-09-29 22:38:57 +0000185};
186
Brian Gaeked0fde302003-11-11 22:41:34 +0000187} // End llvm namespace
188
Misha Brukman29afb642003-09-29 22:38:57 +0000189#endif