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