blob: d8e7503253fbb0c6a289e26bc04123c4211f54b7 [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"
Misha Brukman29afb642003-09-29 22:38:57 +000021#include <vector>
22
Brian Gaeked0fde302003-11-11 22:41:34 +000023namespace llvm {
24
Chris Lattner7915a1e2003-10-14 21:34:11 +000025class CBE;
26class LLC;
Misha Brukman29afb642003-09-29 22:38:57 +000027
Chris Lattner8c56be52004-02-18 20:21:57 +000028
29/// ToolExecutionError - An instance of this class is thrown by the
30/// AbstractInterpreter instances if there is an error running a tool (e.g., LLC
31/// crashes) which prevents execution of the program.
32///
33class ToolExecutionError {
34 std::string Message;
35public:
36 ToolExecutionError(const std::string &M) : Message(M) {}
37 const std::string getMessage() const { return Message; }
38};
39
40
Misha Brukman29afb642003-09-29 22:38:57 +000041//===---------------------------------------------------------------------===//
42// GCC abstraction
43//
Misha Brukman29afb642003-09-29 22:38:57 +000044class GCC {
45 std::string GCCPath; // The path to the gcc executable
Misha Brukman29afb642003-09-29 22:38:57 +000046 GCC(const std::string &gccPath) : GCCPath(gccPath) { }
Chris Lattner7915a1e2003-10-14 21:34:11 +000047public:
48 enum FileType { AsmFile, CFile };
Misha Brukman29afb642003-09-29 22:38:57 +000049
Chris Lattner7915a1e2003-10-14 21:34:11 +000050 static GCC* create(const std::string &ProgramPath, std::string &Message);
51
Chris Lattnereeed9832003-10-14 21:52:52 +000052 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
53 /// either a .s file, or a .c file, specified by FileType), with the specified
54 /// arguments. Standard input is specified with InputFile, and standard
55 /// Output is captured to the specified OutputFile location. The SharedLibs
56 /// option specifies optional native shared objects that can be loaded into
57 /// the program for execution.
58 ///
Chris Lattner7915a1e2003-10-14 21:34:11 +000059 int ExecuteProgram(const std::string &ProgramFile,
60 const std::vector<std::string> &Args,
61 FileType fileType,
62 const std::string &InputFile,
63 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +000064 const std::vector<std::string> &SharedLibs =
65 std::vector<std::string>());
Misha Brukman29afb642003-09-29 22:38:57 +000066
Chris Lattnereeed9832003-10-14 21:52:52 +000067 /// MakeSharedObject - This compiles the specified file (which is either a .c
68 /// file or a .s file) into a shared object.
69 ///
70 int MakeSharedObject(const std::string &InputFile, FileType fileType,
Misha Brukman29afb642003-09-29 22:38:57 +000071 std::string &OutputFile);
Misha Brukman29afb642003-09-29 22:38:57 +000072};
73
Misha Brukman29afb642003-09-29 22:38:57 +000074
Chris Lattner7915a1e2003-10-14 21:34:11 +000075//===---------------------------------------------------------------------===//
Misha Brukman29afb642003-09-29 22:38:57 +000076/// AbstractInterpreter Class - Subclasses of this class are used to execute
77/// LLVM bytecode in a variety of ways. This abstract interface hides this
78/// complexity behind a simple interface.
79///
80struct AbstractInterpreter {
Chris Lattner7915a1e2003-10-14 21:34:11 +000081 static CBE* createCBE(const std::string &ProgramPath, std::string &Message);
82 static LLC *createLLC(const std::string &ProgramPath, std::string &Message);
83
84 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
85 std::string &Message);
86
87 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
88 std::string &Message);
89
Misha Brukman29afb642003-09-29 22:38:57 +000090
91 virtual ~AbstractInterpreter() {}
92
Chris Lattnerf03715c2004-02-18 23:24:29 +000093 /// compileProgram - Compile the specified program from bytecode to executable
94 /// code. This does not produce any output, it is only used when debugging
95 /// the code generator. If the code generator fails, an exception should be
96 /// thrown, otherwise, this function will just return.
97 virtual void compileProgram(const std::string &Bytecode) {}
98
Misha Brukman29afb642003-09-29 22:38:57 +000099 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
100 /// specified filename. This returns the exit code of the program.
101 ///
102 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000103 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000104 const std::string &InputFile,
105 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000106 const std::vector<std::string> &SharedLibs =
107 std::vector<std::string>()) = 0;
Misha Brukman29afb642003-09-29 22:38:57 +0000108};
109
110//===---------------------------------------------------------------------===//
111// CBE Implementation of AbstractIntepreter interface
112//
113class CBE : public AbstractInterpreter {
Chris Lattnerdf2cf412004-02-17 06:39:48 +0000114 std::string LLCPath; // The path to the `llc' executable
Misha Brukman29afb642003-09-29 22:38:57 +0000115 GCC *gcc;
116public:
Chris Lattnerdf2cf412004-02-17 06:39:48 +0000117 CBE(const std::string &llcPath, GCC *Gcc) : LLCPath(llcPath), gcc(Gcc) { }
Misha Brukman29afb642003-09-29 22:38:57 +0000118 ~CBE() { delete gcc; }
119
Chris Lattnerf03715c2004-02-18 23:24:29 +0000120 /// compileProgram - Compile the specified program from bytecode to executable
121 /// code. This does not produce any output, it is only used when debugging
122 /// the code generator. If the code generator fails, an exception should be
123 /// thrown, otherwise, this function will just return.
124 virtual void compileProgram(const std::string &Bytecode);
125
Misha Brukman29afb642003-09-29 22:38:57 +0000126 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000127 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000128 const std::string &InputFile,
129 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000130 const std::vector<std::string> &SharedLibs =
131 std::vector<std::string>());
Misha Brukman29afb642003-09-29 22:38:57 +0000132
Chris Lattner8c56be52004-02-18 20:21:57 +0000133 // Sometimes we just want to go half-way and only generate the .c file, not
134 // necessarily compile it with GCC and run the program. This throws an
135 // exception if LLC crashes.
Chris Lattner7915a1e2003-10-14 21:34:11 +0000136 //
Chris Lattner8c56be52004-02-18 20:21:57 +0000137 virtual void OutputC(const std::string &Bytecode, std::string &OutputCFile);
Misha Brukman29afb642003-09-29 22:38:57 +0000138};
139
Misha Brukman29afb642003-09-29 22:38:57 +0000140
141//===---------------------------------------------------------------------===//
142// LLC Implementation of AbstractIntepreter interface
143//
144class LLC : public AbstractInterpreter {
145 std::string LLCPath; // The path to the LLC executable
146 GCC *gcc;
147public:
148 LLC(const std::string &llcPath, GCC *Gcc)
149 : LLCPath(llcPath), gcc(Gcc) { }
150 ~LLC() { delete gcc; }
151
Chris Lattnerf03715c2004-02-18 23:24:29 +0000152
153 /// compileProgram - Compile the specified program from bytecode to executable
154 /// code. This does not produce any output, it is only used when debugging
155 /// the code generator. If the code generator fails, an exception should be
156 /// thrown, otherwise, this function will just return.
157 virtual void compileProgram(const std::string &Bytecode);
158
Misha Brukman29afb642003-09-29 22:38:57 +0000159 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000160 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000161 const std::string &InputFile,
162 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000163 const std::vector<std::string> &SharedLibs =
164 std::vector<std::string>());
Misha Brukman29afb642003-09-29 22:38:57 +0000165
Chris Lattner7915a1e2003-10-14 21:34:11 +0000166 // Sometimes we just want to go half-way and only generate the .s file,
Chris Lattner8c56be52004-02-18 20:21:57 +0000167 // not necessarily compile it all the way and run the program. This throws
168 // an exception if execution of LLC fails.
Chris Lattner7915a1e2003-10-14 21:34:11 +0000169 //
Chris Lattner8c56be52004-02-18 20:21:57 +0000170 void OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile);
Misha Brukman29afb642003-09-29 22:38:57 +0000171};
172
Brian Gaeked0fde302003-11-11 22:41:34 +0000173} // End llvm namespace
174
Misha Brukman29afb642003-09-29 22:38:57 +0000175#endif