1 //===-- Support/ToolRunner.h ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
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 //===----------------------------------------------------------------------===//
20 #include "Support/SystemUtils.h"
28 //===---------------------------------------------------------------------===//
32 std::string GCCPath; // The path to the gcc executable
33 GCC(const std::string &gccPath) : GCCPath(gccPath) { }
35 enum FileType { AsmFile, CFile };
37 static GCC* create(const std::string &ProgramPath, std::string &Message);
39 /// ExecuteProgram - Execute the program specified by "ProgramFile" (which is
40 /// either a .s file, or a .c file, specified by FileType), with the specified
41 /// arguments. Standard input is specified with InputFile, and standard
42 /// Output is captured to the specified OutputFile location. The SharedLibs
43 /// option specifies optional native shared objects that can be loaded into
44 /// the program for execution.
46 int ExecuteProgram(const std::string &ProgramFile,
47 const std::vector<std::string> &Args,
49 const std::string &InputFile,
50 const std::string &OutputFile,
51 const std::vector<std::string> &SharedLibs =
52 std::vector<std::string>());
54 /// MakeSharedObject - This compiles the specified file (which is either a .c
55 /// file or a .s file) into a shared object.
57 int MakeSharedObject(const std::string &InputFile, FileType fileType,
58 std::string &OutputFile);
61 void ProcessFailure(const char **Args);
65 //===---------------------------------------------------------------------===//
66 /// AbstractInterpreter Class - Subclasses of this class are used to execute
67 /// LLVM bytecode in a variety of ways. This abstract interface hides this
68 /// complexity behind a simple interface.
70 struct AbstractInterpreter {
71 static CBE* createCBE(const std::string &ProgramPath, std::string &Message);
72 static LLC *createLLC(const std::string &ProgramPath, std::string &Message);
74 static AbstractInterpreter* createLLI(const std::string &ProgramPath,
75 std::string &Message);
77 static AbstractInterpreter* createJIT(const std::string &ProgramPath,
78 std::string &Message);
81 virtual ~AbstractInterpreter() {}
83 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
84 /// specified filename. This returns the exit code of the program.
86 virtual int ExecuteProgram(const std::string &Bytecode,
87 const std::vector<std::string> &Args,
88 const std::string &InputFile,
89 const std::string &OutputFile,
90 const std::vector<std::string> &SharedLibs =
91 std::vector<std::string>()) = 0;
94 //===---------------------------------------------------------------------===//
95 // CBE Implementation of AbstractIntepreter interface
97 class CBE : public AbstractInterpreter {
98 std::string LLCPath; // The path to the `llc' executable
101 CBE(const std::string &llcPath, GCC *Gcc) : LLCPath(llcPath), gcc(Gcc) { }
102 ~CBE() { delete gcc; }
104 virtual int ExecuteProgram(const std::string &Bytecode,
105 const std::vector<std::string> &Args,
106 const std::string &InputFile,
107 const std::string &OutputFile,
108 const std::vector<std::string> &SharedLibs =
109 std::vector<std::string>());
111 // Sometimes we just want to go half-way and only generate the .c file,
112 // not necessarily compile it with GCC and run the program.
114 virtual int OutputC(const std::string &Bytecode, std::string &OutputCFile);
118 //===---------------------------------------------------------------------===//
119 // LLC Implementation of AbstractIntepreter interface
121 class LLC : public AbstractInterpreter {
122 std::string LLCPath; // The path to the LLC executable
125 LLC(const std::string &llcPath, GCC *Gcc)
126 : LLCPath(llcPath), gcc(Gcc) { }
127 ~LLC() { delete gcc; }
129 virtual int ExecuteProgram(const std::string &Bytecode,
130 const std::vector<std::string> &Args,
131 const std::string &InputFile,
132 const std::string &OutputFile,
133 const std::vector<std::string> &SharedLibs =
134 std::vector<std::string>());
136 // Sometimes we just want to go half-way and only generate the .s file,
137 // not necessarily compile it all the way and run the program.
139 int OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile);
142 } // End llvm namespace