blob: 3652ff65ed212d1100e006057a54418aa746c539 [file] [log] [blame]
Chris Lattnerf4744492003-09-30 18:28:53 +00001//===-- Support/ToolRunner.h ------------------------------------*- C++ -*-===//
2//
Chris Lattner7915a1e2003-10-14 21:34:11 +00003// This file exposes an abstraction around a platform C compiler, used to
4// compile C and assembly code. It also exposes an "AbstractIntepreter"
5// interface, which is used to execute code using one of the LLVM execution
6// engines.
Chris Lattnerf4744492003-09-30 18:28:53 +00007//
8//===----------------------------------------------------------------------===//
9
Misha Brukman29afb642003-09-29 22:38:57 +000010#ifndef TOOLRUNNER_H
11#define TOOLRUNNER_H
12
Misha Brukman29afb642003-09-29 22:38:57 +000013#include "Support/SystemUtils.h"
Misha Brukman29afb642003-09-29 22:38:57 +000014#include <vector>
15
Chris Lattner7915a1e2003-10-14 21:34:11 +000016class CBE;
17class LLC;
Misha Brukman29afb642003-09-29 22:38:57 +000018
19//===---------------------------------------------------------------------===//
20// GCC abstraction
21//
Misha Brukman29afb642003-09-29 22:38:57 +000022class GCC {
23 std::string GCCPath; // The path to the gcc executable
Misha Brukman29afb642003-09-29 22:38:57 +000024 GCC(const std::string &gccPath) : GCCPath(gccPath) { }
Chris Lattner7915a1e2003-10-14 21:34:11 +000025public:
26 enum FileType { AsmFile, CFile };
Misha Brukman29afb642003-09-29 22:38:57 +000027
Chris Lattner7915a1e2003-10-14 21:34:11 +000028 static GCC* create(const std::string &ProgramPath, std::string &Message);
29
Chris Lattnereeed9832003-10-14 21:52:52 +000030 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
31 /// either a .s file, or a .c file, specified by FileType), with the specified
32 /// arguments. Standard input is specified with InputFile, and standard
33 /// Output is captured to the specified OutputFile location. The SharedLibs
34 /// option specifies optional native shared objects that can be loaded into
35 /// the program for execution.
36 ///
Chris Lattner7915a1e2003-10-14 21:34:11 +000037 int ExecuteProgram(const std::string &ProgramFile,
38 const std::vector<std::string> &Args,
39 FileType fileType,
40 const std::string &InputFile,
41 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +000042 const std::vector<std::string> &SharedLibs =
43 std::vector<std::string>());
Misha Brukman29afb642003-09-29 22:38:57 +000044
Chris Lattnereeed9832003-10-14 21:52:52 +000045 /// MakeSharedObject - This compiles the specified file (which is either a .c
46 /// file or a .s file) into a shared object.
47 ///
48 int MakeSharedObject(const std::string &InputFile, FileType fileType,
Misha Brukman29afb642003-09-29 22:38:57 +000049 std::string &OutputFile);
50
Chris Lattnereeed9832003-10-14 21:52:52 +000051private:
Misha Brukman29afb642003-09-29 22:38:57 +000052 void ProcessFailure(const char **Args);
53};
54
Misha Brukman29afb642003-09-29 22:38:57 +000055
Chris Lattner7915a1e2003-10-14 21:34:11 +000056//===---------------------------------------------------------------------===//
Misha Brukman29afb642003-09-29 22:38:57 +000057/// AbstractInterpreter Class - Subclasses of this class are used to execute
58/// LLVM bytecode in a variety of ways. This abstract interface hides this
59/// complexity behind a simple interface.
60///
61struct AbstractInterpreter {
Chris Lattner7915a1e2003-10-14 21:34:11 +000062 static CBE* createCBE(const std::string &ProgramPath, std::string &Message);
63 static LLC *createLLC(const std::string &ProgramPath, std::string &Message);
64
65 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
66 std::string &Message);
67
68 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
69 std::string &Message);
70
Misha Brukman29afb642003-09-29 22:38:57 +000071
72 virtual ~AbstractInterpreter() {}
73
74 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
75 /// specified filename. This returns the exit code of the program.
76 ///
77 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +000078 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +000079 const std::string &InputFile,
80 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +000081 const std::vector<std::string> &SharedLibs =
82 std::vector<std::string>()) = 0;
Misha Brukman29afb642003-09-29 22:38:57 +000083};
84
85//===---------------------------------------------------------------------===//
86// CBE Implementation of AbstractIntepreter interface
87//
88class CBE : public AbstractInterpreter {
89 std::string DISPath; // The path to the `llvm-dis' executable
90 GCC *gcc;
91public:
92 CBE(const std::string &disPath, GCC *Gcc) : DISPath(disPath), gcc(Gcc) { }
93 ~CBE() { delete gcc; }
94
95 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +000096 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +000097 const std::string &InputFile,
98 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +000099 const std::vector<std::string> &SharedLibs =
100 std::vector<std::string>());
Misha Brukman29afb642003-09-29 22:38:57 +0000101
Chris Lattner7915a1e2003-10-14 21:34:11 +0000102 // Sometimes we just want to go half-way and only generate the .c file,
103 // not necessarily compile it with GCC and run the program.
104 //
105 virtual int OutputC(const std::string &Bytecode, std::string &OutputCFile);
Misha Brukman29afb642003-09-29 22:38:57 +0000106};
107
Misha Brukman29afb642003-09-29 22:38:57 +0000108
109//===---------------------------------------------------------------------===//
110// LLC Implementation of AbstractIntepreter interface
111//
112class LLC : public AbstractInterpreter {
113 std::string LLCPath; // The path to the LLC executable
114 GCC *gcc;
115public:
116 LLC(const std::string &llcPath, GCC *Gcc)
117 : LLCPath(llcPath), gcc(Gcc) { }
118 ~LLC() { delete gcc; }
119
120 virtual int ExecuteProgram(const std::string &Bytecode,
Chris Lattner7915a1e2003-10-14 21:34:11 +0000121 const std::vector<std::string> &Args,
Misha Brukman29afb642003-09-29 22:38:57 +0000122 const std::string &InputFile,
123 const std::string &OutputFile,
Chris Lattnereeed9832003-10-14 21:52:52 +0000124 const std::vector<std::string> &SharedLibs =
125 std::vector<std::string>());
Misha Brukman29afb642003-09-29 22:38:57 +0000126
Chris Lattner7915a1e2003-10-14 21:34:11 +0000127 // Sometimes we just want to go half-way and only generate the .s file,
128 // not necessarily compile it all the way and run the program.
129 //
130 int OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile);
Misha Brukman29afb642003-09-29 22:38:57 +0000131};
132
Misha Brukman29afb642003-09-29 22:38:57 +0000133#endif