Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 1 | //===-- ToolRunner.cpp ----------------------------------------------------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the interfaces described in the ToolRunner.h file. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "toolrunner" |
Chris Lattner | f1b20d8 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 15 | #include "ToolRunner.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 16 | #include "llvm/Config/config.h" // for HAVE_LINK_R |
Chris Lattner | 45495c5 | 2005-02-13 23:13:47 +0000 | [diff] [blame] | 17 | #include "llvm/System/Program.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
| 19 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 20 | #include <fstream> |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 21 | #include <sstream> |
Chris Lattner | 86a5484 | 2006-01-22 22:53:01 +0000 | [diff] [blame] | 22 | #include <iostream> |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Alkis Evlogimenos | 1d29a6d | 2004-02-19 07:39:26 +0000 | [diff] [blame] | 25 | ToolExecutionError::~ToolExecutionError() throw() { } |
| 26 | |
Chris Lattner | 45495c5 | 2005-02-13 23:13:47 +0000 | [diff] [blame] | 27 | /// RunProgramWithTimeout - This function provides an alternate interface to the |
| 28 | /// sys::Program::ExecuteAndWait interface. |
| 29 | /// @see sys:Program::ExecuteAndWait |
| 30 | static int RunProgramWithTimeout(const sys::Path &ProgramPath, |
| 31 | const char **Args, |
| 32 | const sys::Path &StdInFile, |
| 33 | const sys::Path &StdOutFile, |
| 34 | const sys::Path &StdErrFile, |
| 35 | unsigned NumSeconds = 0) { |
| 36 | const sys::Path* redirects[3]; |
| 37 | redirects[0] = &StdInFile; |
| 38 | redirects[1] = &StdOutFile; |
| 39 | redirects[2] = &StdErrFile; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 40 | |
| 41 | return |
Chris Lattner | 45495c5 | 2005-02-13 23:13:47 +0000 | [diff] [blame] | 42 | sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects, NumSeconds); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 47 | static void ProcessFailure(sys::Path ProgPath, const char** Args) { |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 48 | std::ostringstream OS; |
Chris Lattner | a3de117 | 2004-02-18 20:58:00 +0000 | [diff] [blame] | 49 | OS << "\nError running tool:\n "; |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 50 | for (const char **Arg = Args; *Arg; ++Arg) |
| 51 | OS << " " << *Arg; |
| 52 | OS << "\n"; |
| 53 | |
| 54 | // Rerun the compiler, capturing any error messages to print them. |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 55 | sys::Path ErrorFilename("error_messages"); |
| 56 | ErrorFilename.makeUnique(); |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 57 | RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename, |
| 58 | ErrorFilename); |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 59 | |
| 60 | // Print out the error messages generated by GCC if possible... |
| 61 | std::ifstream ErrorFile(ErrorFilename.c_str()); |
| 62 | if (ErrorFile) { |
| 63 | std::copy(std::istreambuf_iterator<char>(ErrorFile), |
| 64 | std::istreambuf_iterator<char>(), |
| 65 | std::ostreambuf_iterator<char>(OS)); |
| 66 | ErrorFile.close(); |
| 67 | } |
| 68 | |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 69 | ErrorFilename.eraseFromDisk(); |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 70 | throw ToolExecutionError(OS.str()); |
| 71 | } |
| 72 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 73 | //===---------------------------------------------------------------------===// |
| 74 | // LLI Implementation of AbstractIntepreter interface |
| 75 | // |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 76 | namespace { |
| 77 | class LLI : public AbstractInterpreter { |
| 78 | std::string LLIPath; // The path to the LLI executable |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 79 | std::vector<std::string> ToolArgs; // Args to pass to LLI |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 80 | public: |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 81 | LLI(const std::string &Path, const std::vector<std::string> *Args) |
| 82 | : LLIPath(Path) { |
| 83 | ToolArgs.clear (); |
Brian Gaeke | 48b008d | 2004-05-04 22:02:41 +0000 | [diff] [blame] | 84 | if (Args) { ToolArgs = *Args; } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 85 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 87 | virtual int ExecuteProgram(const std::string &Bytecode, |
| 88 | const std::vector<std::string> &Args, |
| 89 | const std::string &InputFile, |
| 90 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 91 | const std::vector<std::string> &GCCArgs, |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 92 | const std::vector<std::string> &SharedLibs = |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 93 | std::vector<std::string>(), |
| 94 | unsigned Timeout = 0); |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 95 | }; |
| 96 | } |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 97 | |
| 98 | int LLI::ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 99 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 100 | const std::string &InputFile, |
| 101 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 102 | const std::vector<std::string> &GCCArgs, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 103 | const std::vector<std::string> &SharedLibs, |
| 104 | unsigned Timeout) { |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 105 | if (!SharedLibs.empty()) |
| 106 | throw ToolExecutionError("LLI currently does not support " |
| 107 | "loading shared libraries."); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 108 | |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 109 | if (!GCCArgs.empty()) |
| 110 | throw ToolExecutionError("LLI currently does not support " |
| 111 | "GCC Arguments."); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 112 | std::vector<const char*> LLIArgs; |
| 113 | LLIArgs.push_back(LLIPath.c_str()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 114 | LLIArgs.push_back("-force-interpreter=true"); |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 115 | |
| 116 | // Add any extra LLI args. |
| 117 | for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) |
| 118 | LLIArgs.push_back(ToolArgs[i].c_str()); |
| 119 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 120 | LLIArgs.push_back(Bytecode.c_str()); |
| 121 | // Add optional parameters to the running program from Argv |
| 122 | for (unsigned i=0, e = Args.size(); i != e; ++i) |
| 123 | LLIArgs.push_back(Args[i].c_str()); |
| 124 | LLIArgs.push_back(0); |
| 125 | |
| 126 | std::cout << "<lli>" << std::flush; |
Chris Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 127 | DEBUG(std::cerr << "\nAbout to run:\t"; |
Chris Lattner | 7b2ccff | 2003-10-19 02:14:58 +0000 | [diff] [blame] | 128 | for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i) |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 129 | std::cerr << " " << LLIArgs[i]; |
| 130 | std::cerr << "\n"; |
| 131 | ); |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 132 | return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0], |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 133 | sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 134 | Timeout); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // LLI create method - Try to find the LLI executable |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 138 | AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 139 | std::string &Message, |
| 140 | const std::vector<std::string> *ToolArgs) { |
Reid Spencer | 51ab8ec | 2004-12-13 23:43:44 +0000 | [diff] [blame] | 141 | std::string LLIPath = FindExecutable("lli", ProgPath).toString(); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 142 | if (!LLIPath.empty()) { |
| 143 | Message = "Found lli: " + LLIPath + "\n"; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 144 | return new LLI(LLIPath, ToolArgs); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | Message = "Cannot find `lli' in executable directory or PATH!\n"; |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | //===----------------------------------------------------------------------===// |
| 152 | // LLC Implementation of AbstractIntepreter interface |
| 153 | // |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 154 | void LLC::OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile) { |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 155 | sys::Path uniqueFile(Bytecode+".llc.s"); |
| 156 | uniqueFile.makeUnique(); |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 157 | OutputAsmFile = uniqueFile; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 158 | std::vector<const char *> LLCArgs; |
| 159 | LLCArgs.push_back (LLCPath.c_str()); |
| 160 | |
| 161 | // Add any extra LLC args. |
| 162 | for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) |
| 163 | LLCArgs.push_back(ToolArgs[i].c_str()); |
| 164 | |
| 165 | LLCArgs.push_back ("-o"); |
| 166 | LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file |
| 167 | LLCArgs.push_back ("-f"); // Overwrite as necessary... |
| 168 | LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode |
| 169 | LLCArgs.push_back (0); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 170 | |
| 171 | std::cout << "<llc>" << std::flush; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 172 | DEBUG(std::cerr << "\nAbout to run:\t"; |
| 173 | for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i) |
| 174 | std::cerr << " " << LLCArgs[i]; |
| 175 | std::cerr << "\n"; |
| 176 | ); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 177 | if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0], |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 178 | sys::Path(), sys::Path(), sys::Path())) |
| 179 | ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 182 | void LLC::compileProgram(const std::string &Bytecode) { |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 183 | sys::Path OutputAsmFile; |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 184 | OutputAsm(Bytecode, OutputAsmFile); |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 185 | OutputAsmFile.eraseFromDisk(); |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 188 | int LLC::ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 189 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 190 | const std::string &InputFile, |
| 191 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 192 | const std::vector<std::string> &ArgsForGCC, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 193 | const std::vector<std::string> &SharedLibs, |
| 194 | unsigned Timeout) { |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 195 | |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 196 | sys::Path OutputAsmFile; |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 197 | OutputAsm(Bytecode, OutputAsmFile); |
| 198 | FileRemover OutFileRemover(OutputAsmFile); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 199 | |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 200 | std::vector<std::string> GCCArgs(ArgsForGCC); |
| 201 | GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end()); |
| 202 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 203 | // Assuming LLC worked, compile the result with GCC and run it. |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 204 | return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 205 | InputFile, OutputFile, GCCArgs, Timeout); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 208 | /// createLLC - Try to find the LLC executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 209 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 210 | LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 211 | std::string &Message, |
| 212 | const std::vector<std::string> *Args) { |
Reid Spencer | 51ab8ec | 2004-12-13 23:43:44 +0000 | [diff] [blame] | 213 | std::string LLCPath = FindExecutable("llc", ProgramPath).toString(); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 214 | if (LLCPath.empty()) { |
| 215 | Message = "Cannot find `llc' in executable directory or PATH!\n"; |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | Message = "Found llc: " + LLCPath + "\n"; |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 220 | GCC *gcc = GCC::create(ProgramPath, Message); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 221 | if (!gcc) { |
| 222 | std::cerr << Message << "\n"; |
| 223 | exit(1); |
| 224 | } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 225 | return new LLC(LLCPath, gcc, Args); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | //===---------------------------------------------------------------------===// |
| 229 | // JIT Implementation of AbstractIntepreter interface |
| 230 | // |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 231 | namespace { |
| 232 | class JIT : public AbstractInterpreter { |
| 233 | std::string LLIPath; // The path to the LLI executable |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 234 | std::vector<std::string> ToolArgs; // Args to pass to LLI |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 235 | public: |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 236 | JIT(const std::string &Path, const std::vector<std::string> *Args) |
| 237 | : LLIPath(Path) { |
| 238 | ToolArgs.clear (); |
Brian Gaeke | 48b008d | 2004-05-04 22:02:41 +0000 | [diff] [blame] | 239 | if (Args) { ToolArgs = *Args; } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 240 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 242 | virtual int ExecuteProgram(const std::string &Bytecode, |
| 243 | const std::vector<std::string> &Args, |
| 244 | const std::string &InputFile, |
| 245 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 246 | const std::vector<std::string> &GCCArgs = |
| 247 | std::vector<std::string>(), |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 248 | const std::vector<std::string> &SharedLibs = |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 249 | std::vector<std::string>(), |
| 250 | unsigned Timeout =0 ); |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 251 | }; |
| 252 | } |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 253 | |
| 254 | int JIT::ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 255 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 256 | const std::string &InputFile, |
| 257 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 258 | const std::vector<std::string> &GCCArgs, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 259 | const std::vector<std::string> &SharedLibs, |
| 260 | unsigned Timeout) { |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 261 | if (!GCCArgs.empty()) |
| 262 | throw ToolExecutionError("JIT does not support GCC Arguments."); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 263 | // Construct a vector of parameters, incorporating those from the command-line |
| 264 | std::vector<const char*> JITArgs; |
| 265 | JITArgs.push_back(LLIPath.c_str()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 266 | JITArgs.push_back("-force-interpreter=false"); |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 267 | |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 268 | // Add any extra LLI args. |
| 269 | for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) |
| 270 | JITArgs.push_back(ToolArgs[i].c_str()); |
| 271 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 272 | for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 273 | JITArgs.push_back("-load"); |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 274 | JITArgs.push_back(SharedLibs[i].c_str()); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 275 | } |
| 276 | JITArgs.push_back(Bytecode.c_str()); |
| 277 | // Add optional parameters to the running program from Argv |
| 278 | for (unsigned i=0, e = Args.size(); i != e; ++i) |
| 279 | JITArgs.push_back(Args[i].c_str()); |
| 280 | JITArgs.push_back(0); |
| 281 | |
| 282 | std::cout << "<jit>" << std::flush; |
Chris Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 283 | DEBUG(std::cerr << "\nAbout to run:\t"; |
Chris Lattner | 7b2ccff | 2003-10-19 02:14:58 +0000 | [diff] [blame] | 284 | for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i) |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 285 | std::cerr << " " << JITArgs[i]; |
| 286 | std::cerr << "\n"; |
| 287 | ); |
| 288 | DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n"); |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 289 | return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0], |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 290 | sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 291 | Timeout); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 294 | /// createJIT - Try to find the LLI executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 295 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 296 | AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 297 | std::string &Message, const std::vector<std::string> *Args) { |
Reid Spencer | 51ab8ec | 2004-12-13 23:43:44 +0000 | [diff] [blame] | 298 | std::string LLIPath = FindExecutable("lli", ProgPath).toString(); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 299 | if (!LLIPath.empty()) { |
| 300 | Message = "Found lli: " + LLIPath + "\n"; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 301 | return new JIT(LLIPath, Args); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | Message = "Cannot find `lli' in executable directory or PATH!\n"; |
| 305 | return 0; |
| 306 | } |
| 307 | |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 308 | void CBE::OutputC(const std::string &Bytecode, sys::Path& OutputCFile) { |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 309 | sys::Path uniqueFile(Bytecode+".cbe.c"); |
| 310 | uniqueFile.makeUnique(); |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 311 | OutputCFile = uniqueFile; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 312 | std::vector<const char *> LLCArgs; |
| 313 | LLCArgs.push_back (LLCPath.c_str()); |
| 314 | |
| 315 | // Add any extra LLC args. |
| 316 | for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) |
| 317 | LLCArgs.push_back(ToolArgs[i].c_str()); |
| 318 | |
| 319 | LLCArgs.push_back ("-o"); |
| 320 | LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file |
| 321 | LLCArgs.push_back ("-march=c"); // Output C language |
| 322 | LLCArgs.push_back ("-f"); // Overwrite as necessary... |
| 323 | LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode |
| 324 | LLCArgs.push_back (0); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 325 | |
| 326 | std::cout << "<cbe>" << std::flush; |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 327 | DEBUG(std::cerr << "\nAbout to run:\t"; |
| 328 | for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i) |
| 329 | std::cerr << " " << LLCArgs[i]; |
| 330 | std::cerr << "\n"; |
| 331 | ); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 332 | if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 333 | sys::Path())) |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 334 | ProcessFailure(LLCPath, &LLCArgs[0]); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 337 | void CBE::compileProgram(const std::string &Bytecode) { |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 338 | sys::Path OutputCFile; |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 339 | OutputC(Bytecode, OutputCFile); |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 340 | OutputCFile.eraseFromDisk(); |
Chris Lattner | 9cbbee3 | 2004-02-18 23:24:41 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 343 | int CBE::ExecuteProgram(const std::string &Bytecode, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 344 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 345 | const std::string &InputFile, |
| 346 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 347 | const std::vector<std::string> &ArgsForGCC, |
Chris Lattner | e96b2ed | 2004-07-24 07:49:11 +0000 | [diff] [blame] | 348 | const std::vector<std::string> &SharedLibs, |
| 349 | unsigned Timeout) { |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 350 | sys::Path OutputCFile; |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 351 | OutputC(Bytecode, OutputCFile); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 352 | |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 353 | FileRemover CFileRemove(OutputCFile); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 354 | |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 355 | std::vector<std::string> GCCArgs(ArgsForGCC); |
| 356 | GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end()); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 357 | return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 358 | InputFile, OutputFile, GCCArgs, Timeout); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Chris Lattner | 9915cd9 | 2004-02-17 06:40:06 +0000 | [diff] [blame] | 361 | /// createCBE - Try to find the 'llc' executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 362 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 363 | CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath, |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 364 | std::string &Message, |
| 365 | const std::vector<std::string> *Args) { |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 366 | sys::Path LLCPath = FindExecutable("llc", ProgramPath); |
| 367 | if (LLCPath.isEmpty()) { |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 368 | Message = |
Chris Lattner | 9915cd9 | 2004-02-17 06:40:06 +0000 | [diff] [blame] | 369 | "Cannot find `llc' in executable directory or PATH!\n"; |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 370 | return 0; |
| 371 | } |
| 372 | |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 373 | Message = "Found llc: " + LLCPath.toString() + "\n"; |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 374 | GCC *gcc = GCC::create(ProgramPath, Message); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 375 | if (!gcc) { |
| 376 | std::cerr << Message << "\n"; |
| 377 | exit(1); |
| 378 | } |
Brian Gaeke | d11577b | 2004-05-04 21:09:01 +0000 | [diff] [blame] | 379 | return new CBE(LLCPath, gcc, Args); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | //===---------------------------------------------------------------------===// |
| 383 | // GCC abstraction |
| 384 | // |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 385 | int GCC::ExecuteProgram(const std::string &ProgramFile, |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 386 | const std::vector<std::string> &Args, |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 387 | FileType fileType, |
| 388 | const std::string &InputFile, |
| 389 | const std::string &OutputFile, |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 390 | const std::vector<std::string> &ArgsForGCC, |
| 391 | unsigned Timeout ) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 392 | std::vector<const char*> GCCArgs; |
| 393 | |
| 394 | GCCArgs.push_back(GCCPath.c_str()); |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 395 | |
Chris Lattner | eeed983 | 2003-10-14 21:52:52 +0000 | [diff] [blame] | 396 | // Specify -x explicitly in case the extension is wonky |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 397 | GCCArgs.push_back("-x"); |
| 398 | if (fileType == CFile) { |
| 399 | GCCArgs.push_back("c"); |
| 400 | GCCArgs.push_back("-fno-strict-aliasing"); |
| 401 | } else { |
| 402 | GCCArgs.push_back("assembler"); |
Chris Lattner | d00b288 | 2005-08-29 13:14:24 +0000 | [diff] [blame] | 403 | #ifdef __APPLE__ |
| 404 | GCCArgs.push_back("-force_cpusubtype_ALL"); |
| 405 | #endif |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 406 | } |
| 407 | GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename... |
Chris Lattner | 629e487 | 2006-06-09 21:31:53 +0000 | [diff] [blame] | 408 | GCCArgs.push_back("-x"); |
| 409 | GCCArgs.push_back("none"); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 410 | GCCArgs.push_back("-o"); |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 411 | sys::Path OutputBinary (ProgramFile+".gcc.exe"); |
| 412 | OutputBinary.makeUnique(); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 413 | GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file... |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 414 | |
| 415 | // Add any arguments intended for GCC. We locate them here because this is |
| 416 | // most likely -L and -l options that need to come before other libraries but |
| 417 | // after the source. Other options won't be sensitive to placement on the |
| 418 | // command line, so this should be safe. |
| 419 | for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i) |
| 420 | GCCArgs.push_back(ArgsForGCC[i].c_str()); |
| 421 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 422 | GCCArgs.push_back("-lm"); // Hard-code the math library... |
| 423 | GCCArgs.push_back("-O2"); // Optimize the program a bit... |
Brian Gaeke | c8db76c | 2003-11-18 06:31:17 +0000 | [diff] [blame] | 424 | #if defined (HAVE_LINK_R) |
Chris Lattner | 1f0f162 | 2003-10-18 21:54:47 +0000 | [diff] [blame] | 425 | GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files |
Brian Gaeke | c8db76c | 2003-11-18 06:31:17 +0000 | [diff] [blame] | 426 | #endif |
Chris Lattner | fdcc71e | 2006-02-04 05:02:27 +0000 | [diff] [blame] | 427 | #ifdef __sparc__ |
| 428 | GCCArgs.push_back("-mcpu=v9"); |
| 429 | #endif |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 430 | GCCArgs.push_back(0); // NULL terminator |
| 431 | |
| 432 | std::cout << "<gcc>" << std::flush; |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 433 | if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(), |
| 434 | sys::Path())) { |
Chris Lattner | 89bf9ea | 2004-02-18 20:38:00 +0000 | [diff] [blame] | 435 | ProcessFailure(GCCPath, &GCCArgs[0]); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 436 | exit(1); |
| 437 | } |
| 438 | |
| 439 | std::vector<const char*> ProgramArgs; |
Chris Lattner | 45495c5 | 2005-02-13 23:13:47 +0000 | [diff] [blame] | 440 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 441 | ProgramArgs.push_back(OutputBinary.c_str()); |
| 442 | // Add optional parameters to the running program from Argv |
| 443 | for (unsigned i=0, e = Args.size(); i != e; ++i) |
| 444 | ProgramArgs.push_back(Args[i].c_str()); |
| 445 | ProgramArgs.push_back(0); // NULL terminator |
| 446 | |
| 447 | // Now that we have a binary, run it! |
| 448 | std::cout << "<program>" << std::flush; |
Chris Lattner | 0b1fe84 | 2003-10-19 02:27:40 +0000 | [diff] [blame] | 449 | DEBUG(std::cerr << "\nAbout to run:\t"; |
Chris Lattner | 7b2ccff | 2003-10-19 02:14:58 +0000 | [diff] [blame] | 450 | for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i) |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 451 | std::cerr << " " << ProgramArgs[i]; |
| 452 | std::cerr << "\n"; |
| 453 | ); |
Chris Lattner | 8c56be5 | 2004-02-18 20:21:57 +0000 | [diff] [blame] | 454 | |
Reid Spencer | 9ac1418 | 2004-12-16 23:01:34 +0000 | [diff] [blame] | 455 | FileRemover OutputBinaryRemover(OutputBinary); |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 456 | return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0], |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 457 | sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 458 | Timeout); |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Chris Lattner | 1798e4a | 2003-10-14 21:07:25 +0000 | [diff] [blame] | 461 | int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType, |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 462 | std::string &OutputFile, |
| 463 | const std::vector<std::string> &ArgsForGCC) { |
Reid Spencer | cda985e | 2004-12-15 01:51:56 +0000 | [diff] [blame] | 464 | sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT); |
| 465 | uniqueFilename.makeUnique(); |
| 466 | OutputFile = uniqueFilename.toString(); |
| 467 | |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 468 | std::vector<const char*> GCCArgs; |
| 469 | |
| 470 | GCCArgs.push_back(GCCPath.c_str()); |
| 471 | |
| 472 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 473 | // Compile the C/asm file into a shared object |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 474 | GCCArgs.push_back("-x"); |
| 475 | GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c"); |
| 476 | GCCArgs.push_back("-fno-strict-aliasing"); |
| 477 | GCCArgs.push_back(InputFile.c_str()); // Specify the input filename. |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 478 | #if defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 479 | GCCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 480 | #elif defined(__APPLE__) |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 481 | // link all source files into a single module in data segment, rather than |
| 482 | // generating blocks. dynamic_lookup requires that you set |
| 483 | // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for |
| 484 | // bugpoint to just pass that in the environment of GCC. |
| 485 | GCCArgs.push_back("-single_module"); |
| 486 | GCCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC |
| 487 | GCCArgs.push_back("-undefined"); |
| 488 | GCCArgs.push_back("dynamic_lookup"); |
Misha Brukman | b3998ec | 2004-07-16 19:45:45 +0000 | [diff] [blame] | 489 | #else |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 490 | GCCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 491 | #endif |
Chris Lattner | 1c81f13 | 2005-03-09 03:31:02 +0000 | [diff] [blame] | 492 | |
Andrew Lenharth | 572668a | 2005-03-10 20:15:09 +0000 | [diff] [blame] | 493 | #if defined(__ia64__) || defined(__alpha__) |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 494 | GCCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC |
Chris Lattner | 1c81f13 | 2005-03-09 03:31:02 +0000 | [diff] [blame] | 495 | #endif |
Chris Lattner | fdcc71e | 2006-02-04 05:02:27 +0000 | [diff] [blame] | 496 | #ifdef __sparc__ |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 497 | GCCArgs.push_back("-mcpu=v9"); |
Chris Lattner | fdcc71e | 2006-02-04 05:02:27 +0000 | [diff] [blame] | 498 | #endif |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 499 | GCCArgs.push_back("-o"); |
| 500 | GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename. |
| 501 | GCCArgs.push_back("-O2"); // Optimize the program a bit. |
| 502 | |
| 503 | |
| 504 | |
| 505 | // Add any arguments intended for GCC. We locate them here because this is |
| 506 | // most likely -L and -l options that need to come before other libraries but |
| 507 | // after the source. Other options won't be sensitive to placement on the |
| 508 | // command line, so this should be safe. |
| 509 | for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i) |
| 510 | GCCArgs.push_back(ArgsForGCC[i].c_str()); |
| 511 | GCCArgs.push_back(0); // NULL terminator |
| 512 | |
| 513 | |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 514 | |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 515 | std::cout << "<gcc>" << std::flush; |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 516 | if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(), |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 517 | sys::Path())) { |
Chris Lattner | 130e2a3 | 2006-06-27 20:35:36 +0000 | [diff] [blame] | 518 | ProcessFailure(GCCPath, &GCCArgs[0]); |
Chris Lattner | 1798e4a | 2003-10-14 21:07:25 +0000 | [diff] [blame] | 519 | return 1; |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 520 | } |
| 521 | return 0; |
| 522 | } |
| 523 | |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 524 | /// create - Try to find the `gcc' executable |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 525 | /// |
Chris Lattner | 7915a1e | 2003-10-14 21:34:11 +0000 | [diff] [blame] | 526 | GCC *GCC::create(const std::string &ProgramPath, std::string &Message) { |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 527 | sys::Path GCCPath = FindExecutable("gcc", ProgramPath); |
| 528 | if (GCCPath.isEmpty()) { |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 529 | Message = "Cannot find `gcc' in executable directory or PATH!\n"; |
| 530 | return 0; |
| 531 | } |
| 532 | |
Reid Spencer | b31baa8 | 2004-12-19 18:00:21 +0000 | [diff] [blame] | 533 | Message = "Found gcc: " + GCCPath.toString() + "\n"; |
Misha Brukman | 9558c6a | 2003-09-29 22:39:25 +0000 | [diff] [blame] | 534 | return new GCC(GCCPath); |
| 535 | } |