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