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