blob: b391b0492f2709569b90e89c2a58a01f43038ac1 [file] [log] [blame]
Chris Lattnerde4aa4c2002-12-23 23:50:16 +00001//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
2//
3// This file contains code used to execute the program utilizing one of the
4// various ways of running LLVM bytecode.
5//
6//===----------------------------------------------------------------------===//
7
8/*
9BUGPOINT NOTES:
10
111. Bugpoint should not leave any files behind if the program works properly
122. There should be an option to specify the program name, which specifies a
13 unique string to put into output files. This allows operation in the
Misha Brukmand792c9b2003-07-24 18:17:43 +000014 SingleSource directory, e.g. default to the first input filename.
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000015*/
16
17#include "BugDriver.h"
18#include "SystemUtils.h"
19#include "Support/CommandLine.h"
20#include <fstream>
Chris Lattner7c0f8622002-12-24 00:44:34 +000021#include <iostream>
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000022
23namespace {
24 // OutputType - Allow the user to specify the way code should be run, to test
25 // for miscompilation.
26 //
27 enum OutputType {
28 RunLLI, RunJIT, RunLLC, RunCBE
29 };
30 cl::opt<OutputType>
31 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
Misha Brukmand792c9b2003-07-24 18:17:43 +000032 cl::values(clEnumValN(RunLLI, "run-lli", "Execute with LLI"),
33 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
34 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
35 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
36 0));
Chris Lattner68efaa72003-04-23 20:31:37 +000037
38 cl::opt<std::string>
39 InputFile("input", cl::init("/dev/null"),
40 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Misha Brukman0fd31722003-07-24 21:59:10 +000041
42 enum FileType { AsmFile, CFile };
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000043}
44
45/// AbstractInterpreter Class - Subclasses of this class are used to execute
46/// LLVM bytecode in a variety of ways. This abstract interface hides this
47/// complexity behind a simple interface.
48///
49struct AbstractInterpreter {
50
51 virtual ~AbstractInterpreter() {}
52
53 /// ExecuteProgram - Run the specified bytecode file, emitting output to the
54 /// specified filename. This returns the exit code of the program.
55 ///
56 virtual int ExecuteProgram(const std::string &Bytecode,
Misha Brukmand792c9b2003-07-24 18:17:43 +000057 const std::string &OutputFile,
58 const std::string &SharedLib = "") = 0;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000059};
60
61
62//===----------------------------------------------------------------------===//
63// LLI Implementation of AbstractIntepreter interface
64//
65class LLI : public AbstractInterpreter {
66 std::string LLIPath; // The path to the LLI executable
67public:
68 LLI(const std::string &Path) : LLIPath(Path) { }
69
70 // LLI create method - Try to find the LLI executable
71 static LLI *create(BugDriver *BD, std::string &Message) {
72 std::string LLIPath = FindExecutable("lli", BD->getToolName());
73 if (!LLIPath.empty()) {
74 Message = "Found lli: " + LLIPath + "\n";
75 return new LLI(LLIPath);
76 }
77
Misha Brukmand792c9b2003-07-24 18:17:43 +000078 Message = "Cannot find `lli' in bugpoint executable directory or PATH!\n";
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000079 return 0;
80 }
81 virtual int ExecuteProgram(const std::string &Bytecode,
Misha Brukmand792c9b2003-07-24 18:17:43 +000082 const std::string &OutputFile,
83 const std::string &SharedLib = "");
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000084};
85
86int LLI::ExecuteProgram(const std::string &Bytecode,
Misha Brukmand792c9b2003-07-24 18:17:43 +000087 const std::string &OutputFile,
88 const std::string &SharedLib) {
Misha Brukman0fd31722003-07-24 21:59:10 +000089 if (!SharedLib.empty()) {
Misha Brukmand792c9b2003-07-24 18:17:43 +000090 std::cerr << "LLI currently does not support loading shared libraries.\n"
91 << "Exiting.\n";
92 exit(1);
93 }
94
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000095 const char *Args[] = {
Misha Brukmand792c9b2003-07-24 18:17:43 +000096 LLIPath.c_str(),
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000097 "-abort-on-exception",
98 "-quiet",
Chris Lattner7709ec52003-05-03 03:19:41 +000099 "-force-interpreter=true",
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000100 Bytecode.c_str(),
101 0
102 };
Misha Brukmand792c9b2003-07-24 18:17:43 +0000103
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000104 return RunProgramWithTimeout(LLIPath, Args,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000105 InputFile, OutputFile, OutputFile);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000106}
107
Chris Lattner7709ec52003-05-03 03:19:41 +0000108//===----------------------------------------------------------------------===//
Misha Brukmand792c9b2003-07-24 18:17:43 +0000109// GCC Implementation of AbstractIntepreter interface
110//
111// This is not a *real* AbstractInterpreter as it does not accept bytecode
112// files, but only input acceptable to GCC, i.e. C, C++, and assembly files
113//
Misha Brukman0fd31722003-07-24 21:59:10 +0000114class GCC {
Misha Brukmand792c9b2003-07-24 18:17:43 +0000115 std::string GCCPath; // The path to the gcc executable
116public:
117 GCC(const std::string &gccPath) : GCCPath(gccPath) { }
Misha Brukman0fd31722003-07-24 21:59:10 +0000118 virtual ~GCC() {}
Misha Brukmand792c9b2003-07-24 18:17:43 +0000119
120 // GCC create method - Try to find the `gcc' executable
121 static GCC *create(BugDriver *BD, std::string &Message) {
122 std::string GCCPath = FindExecutable("gcc", BD->getToolName());
123 if (GCCPath.empty()) {
124 Message = "Cannot find `gcc' in bugpoint executable directory or PATH!\n";
125 return 0;
126 }
127
128 Message = "Found gcc: " + GCCPath + "\n";
129 return new GCC(GCCPath);
130 }
131
132 virtual int ExecuteProgram(const std::string &ProgramFile,
Misha Brukman0fd31722003-07-24 21:59:10 +0000133 FileType fileType,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000134 const std::string &OutputFile,
135 const std::string &SharedLib = "");
136
137 int MakeSharedObject(const std::string &InputFile,
Misha Brukman0fd31722003-07-24 21:59:10 +0000138 FileType fileType,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000139 std::string &OutputFile);
140
141 void ProcessFailure(const char **Args);
142};
143
144int GCC::ExecuteProgram(const std::string &ProgramFile,
Misha Brukman0fd31722003-07-24 21:59:10 +0000145 FileType fileType,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000146 const std::string &OutputFile,
147 const std::string &SharedLib) {
148 std::string OutputBinary = "bugpoint.gcc.exe";
149 const char **GCCArgs;
150
151 const char *ArgsWithoutSO[] = {
152 GCCPath.c_str(),
Misha Brukman0fd31722003-07-24 21:59:10 +0000153 "-x", (fileType == AsmFile) ? "assembler" : "c",
Misha Brukmand792c9b2003-07-24 18:17:43 +0000154 ProgramFile.c_str(), // Specify the input filename...
155 "-o", OutputBinary.c_str(), // Output to the right filename...
156 "-lm", // Hard-code the math library...
157 "-O2", // Optimize the program a bit...
158 0
159 };
160 const char *ArgsWithSO[] = {
161 GCCPath.c_str(),
Misha Brukman0fd31722003-07-24 21:59:10 +0000162 "-x", (fileType == AsmFile) ? "assembler" : "c",
Misha Brukmand792c9b2003-07-24 18:17:43 +0000163 ProgramFile.c_str(), // Specify the input filename...
164 SharedLib.c_str(), // Specify the shared library to link in...
165 "-o", OutputBinary.c_str(), // Output to the right filename...
166 "-lm", // Hard-code the math library...
167 "-O2", // Optimize the program a bit...
168 0
169 };
170
Misha Brukman0fd31722003-07-24 21:59:10 +0000171 GCCArgs = (SharedLib.empty()) ? ArgsWithoutSO : ArgsWithSO;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000172 std::cout << "<gcc>";
Misha Brukman0fd31722003-07-24 21:59:10 +0000173 if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
174 "/dev/null")) {
Misha Brukmand792c9b2003-07-24 18:17:43 +0000175 ProcessFailure(GCCArgs);
Misha Brukman0fd31722003-07-24 21:59:10 +0000176 exit(1);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000177 }
178
179 const char *ProgramArgs[] = {
180 OutputBinary.c_str(),
181 0
182 };
183
184 std::cout << "<program>";
185
186 // Now that we have a binary, run it!
187 int ProgramResult = RunProgramWithTimeout(OutputBinary, ProgramArgs,
188 InputFile, OutputFile, OutputFile);
189 std::cout << "\n";
190 removeFile(OutputBinary);
191 return ProgramResult;
192}
193
194int GCC::MakeSharedObject(const std::string &InputFile,
Misha Brukman0fd31722003-07-24 21:59:10 +0000195 FileType fileType,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000196 std::string &OutputFile) {
197 OutputFile = "./bugpoint.so";
198 // Compile the C/asm file into a shared object
199 const char* GCCArgs[] = {
200 GCCPath.c_str(),
Misha Brukman0fd31722003-07-24 21:59:10 +0000201 "-x", (fileType == AsmFile) ? "assembler" : "c",
Misha Brukmand792c9b2003-07-24 18:17:43 +0000202 InputFile.c_str(), // Specify the input filename...
203#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
204 "-G", // Compile a shared library, `-G' for Sparc
205#else
206 "-shared", // `-shared' for Linux/X86, maybe others
207#endif
208 "-o", OutputFile.c_str(), // Output to the right filename...
209 "-O2", // Optimize the program a bit...
210 0
211 };
212
213 std::cout << "<gcc>";
214 if(RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
215 "/dev/null")) {
216 ProcessFailure(GCCArgs);
217 exit(1);
218 }
219 return 0;
220}
221
222void GCC::ProcessFailure(const char** GCCArgs) {
223 std::cerr << "\n*** bugpoint error: invocation of the C compiler failed!\n";
224 for (const char **Arg = GCCArgs; *Arg; ++Arg)
225 std::cerr << " " << *Arg;
226 std::cerr << "\n";
227
228 // Rerun the compiler, capturing any error messages to print them.
229 std::string ErrorFilename = getUniqueFilename("bugpoint.gcc.errors");
230 RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", ErrorFilename.c_str(),
231 ErrorFilename.c_str());
232
233 // Print out the error messages generated by GCC if possible...
234 std::ifstream ErrorFile(ErrorFilename.c_str());
235 if (ErrorFile) {
236 std::copy(std::istreambuf_iterator<char>(ErrorFile),
237 std::istreambuf_iterator<char>(),
238 std::ostreambuf_iterator<char>(std::cerr));
239 ErrorFile.close();
240 std::cerr << "\n";
241 }
242
243 removeFile(ErrorFilename);
244}
245
246//===----------------------------------------------------------------------===//
247// LLC Implementation of AbstractIntepreter interface
248//
249class LLC : public AbstractInterpreter {
250 std::string LLCPath; // The path to the LLC executable
251 GCC *gcc;
252public:
253 LLC(const std::string &llcPath, GCC *Gcc)
254 : LLCPath(llcPath), gcc(Gcc) { }
255 ~LLC() { delete gcc; }
256
257 // LLC create method - Try to find the LLC executable
258 static LLC *create(BugDriver *BD, std::string &Message) {
259 std::string LLCPath = FindExecutable("llc", BD->getToolName());
260 if (LLCPath.empty()) {
261 Message = "Cannot find `llc' in bugpoint executable directory or PATH!\n";
262 return 0;
263 }
264
265 Message = "Found llc: " + LLCPath + "\n";
266 GCC *gcc = GCC::create(BD, Message);
Misha Brukman0fd31722003-07-24 21:59:10 +0000267 if (!gcc) {
268 std::cerr << Message << "\n";
269 exit(1);
270 }
Misha Brukmand792c9b2003-07-24 18:17:43 +0000271 return new LLC(LLCPath, gcc);
272 }
273
274 virtual int ExecuteProgram(const std::string &Bytecode,
275 const std::string &OutputFile,
276 const std::string &SharedLib = "");
277
278 int OutputAsm(const std::string &Bytecode,
279 std::string &OutputAsmFile);
280
281};
282
283int LLC::OutputAsm(const std::string &Bytecode,
284 std::string &OutputAsmFile) {
285 OutputAsmFile = "bugpoint.llc.s";
286 const char *LLCArgs[] = {
287 LLCPath.c_str(),
288 "-o", OutputAsmFile.c_str(), // Output to the Asm file
289 "-f", // Overwrite as necessary...
290 Bytecode.c_str(), // This is the input bytecode
291 0
292 };
293
294 std::cout << "<llc>";
295 if (RunProgramWithTimeout(LLCPath, LLCArgs, "/dev/null", "/dev/null",
296 "/dev/null")) {
297 // If LLC failed on the bytecode, print error...
298 std::cerr << "bugpoint error: `llc' failed!\n";
299 removeFile(OutputAsmFile);
300 return 1;
301 }
302
303 return 0;
304}
305
306int LLC::ExecuteProgram(const std::string &Bytecode,
307 const std::string &OutputFile,
308 const std::string &SharedLib) {
309
310 std::string OutputAsmFile;
311 if (OutputAsm(Bytecode, OutputAsmFile)) {
312 std::cerr << "Could not generate asm code with `llc', exiting.\n";
313 exit(1);
314 }
315
316 // Assuming LLC worked, compile the result with GCC and run it.
Misha Brukman0fd31722003-07-24 21:59:10 +0000317 int Result = gcc->ExecuteProgram(OutputAsmFile,AsmFile,OutputFile,SharedLib);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000318 removeFile(OutputAsmFile);
319 return Result;
320}
321
322
323//===----------------------------------------------------------------------===//
Chris Lattner7709ec52003-05-03 03:19:41 +0000324// JIT Implementation of AbstractIntepreter interface
325//
326class JIT : public AbstractInterpreter {
327 std::string LLIPath; // The path to the LLI executable
328public:
329 JIT(const std::string &Path) : LLIPath(Path) { }
330
331 // JIT create method - Try to find the LLI executable
332 static JIT *create(BugDriver *BD, std::string &Message) {
333 std::string LLIPath = FindExecutable("lli", BD->getToolName());
334 if (!LLIPath.empty()) {
335 Message = "Found lli: " + LLIPath + "\n";
336 return new JIT(LLIPath);
337 }
338
Misha Brukmand792c9b2003-07-24 18:17:43 +0000339 Message = "Cannot find `lli' in bugpoint executable directory or PATH!\n";
Chris Lattner7709ec52003-05-03 03:19:41 +0000340 return 0;
341 }
342 virtual int ExecuteProgram(const std::string &Bytecode,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000343 const std::string &OutputFile,
344 const std::string &SharedLib = "");
Chris Lattner7709ec52003-05-03 03:19:41 +0000345};
346
347int JIT::ExecuteProgram(const std::string &Bytecode,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000348 const std::string &OutputFile,
349 const std::string &SharedLib) {
Misha Brukman0fd31722003-07-24 21:59:10 +0000350 if (SharedLib.empty()) {
Misha Brukmand792c9b2003-07-24 18:17:43 +0000351 const char* Args[] = {
Misha Brukman0fd31722003-07-24 21:59:10 +0000352 LLIPath.c_str(),
353 "-quiet",
354 "-force-interpreter=false",
355 Bytecode.c_str(),
Misha Brukmand792c9b2003-07-24 18:17:43 +0000356 0
357 };
358 return RunProgramWithTimeout(LLIPath, Args,
359 InputFile, OutputFile, OutputFile);
360 } else {
Misha Brukmand792c9b2003-07-24 18:17:43 +0000361 const char* Args[] = {
362 LLIPath.c_str(), "-quiet", "-force-interpreter=false",
Misha Brukman0fd31722003-07-24 21:59:10 +0000363 "-load", SharedLib.c_str(),
Misha Brukmand792c9b2003-07-24 18:17:43 +0000364 Bytecode.c_str(),
365 0
366 };
367 return RunProgramWithTimeout(LLIPath, Args,
368 InputFile, OutputFile, OutputFile);
369 }
Chris Lattner7709ec52003-05-03 03:19:41 +0000370}
371
372//===----------------------------------------------------------------------===//
373// CBE Implementation of AbstractIntepreter interface
374//
375class CBE : public AbstractInterpreter {
376 std::string DISPath; // The path to the LLVM 'dis' executable
Misha Brukmand792c9b2003-07-24 18:17:43 +0000377 GCC *gcc;
Chris Lattner7709ec52003-05-03 03:19:41 +0000378public:
Misha Brukmand792c9b2003-07-24 18:17:43 +0000379 CBE(const std::string &disPath, GCC *Gcc) : DISPath(disPath), gcc(Gcc) { }
380 ~CBE() { delete gcc; }
Chris Lattner7709ec52003-05-03 03:19:41 +0000381
382 // CBE create method - Try to find the 'dis' executable
383 static CBE *create(BugDriver *BD, std::string &Message) {
384 std::string DISPath = FindExecutable("dis", BD->getToolName());
385 if (DISPath.empty()) {
Misha Brukmand792c9b2003-07-24 18:17:43 +0000386 Message = "Cannot find `dis' in bugpoint executable directory or PATH!\n";
Chris Lattner7709ec52003-05-03 03:19:41 +0000387 return 0;
388 }
389
390 Message = "Found dis: " + DISPath + "\n";
391
Misha Brukmand792c9b2003-07-24 18:17:43 +0000392 GCC *gcc = GCC::create(BD, Message);
Misha Brukman0fd31722003-07-24 21:59:10 +0000393 if (!gcc) {
394 std::cerr << Message << "\n";
395 exit(1);
396 }
Misha Brukmand792c9b2003-07-24 18:17:43 +0000397 return new CBE(DISPath, gcc);
Chris Lattner7709ec52003-05-03 03:19:41 +0000398 }
Misha Brukmand792c9b2003-07-24 18:17:43 +0000399
Chris Lattner7709ec52003-05-03 03:19:41 +0000400 virtual int ExecuteProgram(const std::string &Bytecode,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000401 const std::string &OutputFile,
402 const std::string &SharedLib = "");
403
404 // Sometimes we just want to go half-way and only generate the C file,
405 // not necessarily compile it with GCC and run the program
406 virtual int OutputC(const std::string &Bytecode,
407 std::string &OutputCFile);
408
Chris Lattner7709ec52003-05-03 03:19:41 +0000409};
410
Misha Brukmand792c9b2003-07-24 18:17:43 +0000411int CBE::OutputC(const std::string &Bytecode,
412 std::string &OutputCFile) {
413 OutputCFile = "bugpoint.cbe.c";
Chris Lattner7709ec52003-05-03 03:19:41 +0000414 const char *DisArgs[] = {
415 DISPath.c_str(),
416 "-o", OutputCFile.c_str(), // Output to the C file
417 "-c", // Output to C
418 "-f", // Overwrite as necessary...
419 Bytecode.c_str(), // This is the input bytecode
420 0
421 };
422
423 std::cout << "<cbe>";
424 if (RunProgramWithTimeout(DISPath, DisArgs, "/dev/null", "/dev/null",
425 "/dev/null")) {
426 // If dis failed on the bytecode, print error...
Misha Brukmand792c9b2003-07-24 18:17:43 +0000427 std::cerr << "bugpoint error: `dis -c' failed!\n";
Chris Lattner7709ec52003-05-03 03:19:41 +0000428 return 1;
429 }
430
Misha Brukmand792c9b2003-07-24 18:17:43 +0000431 return 0;
432}
Chris Lattner7709ec52003-05-03 03:19:41 +0000433
Chris Lattner7709ec52003-05-03 03:19:41 +0000434
Misha Brukmand792c9b2003-07-24 18:17:43 +0000435int CBE::ExecuteProgram(const std::string &Bytecode,
436 const std::string &OutputFile,
437 const std::string &SharedLib) {
438 std::string OutputCFile;
439 if (OutputC(Bytecode, OutputCFile)) {
440 std::cerr << "Could not generate C code with `dis', exiting.\n";
441 exit(1);
Chris Lattner7709ec52003-05-03 03:19:41 +0000442 }
443
Misha Brukman0fd31722003-07-24 21:59:10 +0000444 int Result = gcc->ExecuteProgram(OutputCFile, CFile, OutputFile, SharedLib);
Chris Lattner7709ec52003-05-03 03:19:41 +0000445 removeFile(OutputCFile);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000446
Chris Lattner7709ec52003-05-03 03:19:41 +0000447 return Result;
448}
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000449
Misha Brukmand792c9b2003-07-24 18:17:43 +0000450
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000451//===----------------------------------------------------------------------===//
452// BugDriver method implementation
453//
454
455/// initializeExecutionEnvironment - This method is used to set up the
456/// environment for executing LLVM programs.
457///
458bool BugDriver::initializeExecutionEnvironment() {
459 std::cout << "Initializing execution environment: ";
460
461 // FIXME: This should default to searching for the best interpreter to use on
462 // this platform, which would be JIT, then LLC, then CBE, then LLI.
463
464 // Create an instance of the AbstractInterpreter interface as specified on the
465 // command line
466 std::string Message;
Chris Lattner7709ec52003-05-03 03:19:41 +0000467 switch (InterpreterSel) {
468 case RunLLI: Interpreter = LLI::create(this, Message); break;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000469 case RunLLC: Interpreter = LLC::create(this, Message); break;
Chris Lattner7709ec52003-05-03 03:19:41 +0000470 case RunJIT: Interpreter = JIT::create(this, Message); break;
471 case RunCBE: Interpreter = CBE::create(this, Message); break;
472 default:
473 Message = " Sorry, this back-end is not supported by bugpoint right now!\n";
474 break;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000475 }
476
477 std::cout << Message;
478
Misha Brukman0fd31722003-07-24 21:59:10 +0000479 // Initialize auxiliary tools for debugging
480 cbe = CBE::create(this, Message);
481 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
482 gcc = GCC::create(this, Message);
483 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
484
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000485 // If there was an error creating the selected interpreter, quit with error.
486 return Interpreter == 0;
487}
488
489
490/// executeProgram - This method runs "Program", capturing the output of the
491/// program to a file, returning the filename of the file. A recommended
492/// filename may be optionally specified.
493///
494std::string BugDriver::executeProgram(std::string OutputFile,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000495 std::string BytecodeFile,
496 std::string SharedObject,
497 AbstractInterpreter *AI) {
498 assert((Interpreter || AI) &&"Interpreter should have been created already!");
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000499 bool CreatedBytecode = false;
500 if (BytecodeFile.empty()) {
501 // Emit the program to a bytecode file...
502 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
503
504 if (writeProgramToFile(BytecodeFile, Program)) {
505 std::cerr << ToolName << ": Error emitting bytecode to file '"
Misha Brukmand792c9b2003-07-24 18:17:43 +0000506 << BytecodeFile << "'!\n";
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000507 exit(1);
508 }
509 CreatedBytecode = true;
510 }
511
512 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
Misha Brukmand792c9b2003-07-24 18:17:43 +0000513
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000514 // Check to see if this is a valid output filename...
515 OutputFile = getUniqueFilename(OutputFile);
516
517 // Actually execute the program!
Misha Brukmand792c9b2003-07-24 18:17:43 +0000518 int RetVal = (AI != 0) ?
519 AI->ExecuteProgram(BytecodeFile, OutputFile, SharedObject) :
520 Interpreter->ExecuteProgram(BytecodeFile, OutputFile, SharedObject);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000521
522 // Remove the temporary bytecode file.
Misha Brukmand792c9b2003-07-24 18:17:43 +0000523 if (CreatedBytecode) removeFile(BytecodeFile);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000524
525 // Return the filename we captured the output to.
526 return OutputFile;
527}
528
Misha Brukmand792c9b2003-07-24 18:17:43 +0000529std::string BugDriver::executeProgramWithCBE(std::string OutputFile,
530 std::string BytecodeFile,
531 std::string SharedObject) {
Misha Brukman0fd31722003-07-24 21:59:10 +0000532 return executeProgram(OutputFile, BytecodeFile, SharedObject, cbe);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000533}
534
535int BugDriver::compileSharedObject(const std::string &BytecodeFile,
536 std::string &SharedObject) {
537 assert(Interpreter && "Interpreter should have been created already!");
538 std::string Message, OutputCFile;
539
540 // Using CBE
Misha Brukmand792c9b2003-07-24 18:17:43 +0000541 cbe->OutputC(BytecodeFile, OutputCFile);
542
543#if 0 /* This is an alternative, as yet unimplemented */
544 // Using LLC
545 LLC *llc = LLC::create(this, Message);
546 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
547 std::cerr << "Could not generate asm code with `llc', exiting.\n";
548 exit(1);
549 }
550#endif
551
Misha Brukman0fd31722003-07-24 21:59:10 +0000552 gcc->MakeSharedObject(OutputCFile, CFile, SharedObject);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000553
554 // Remove the intermediate C file
555 removeFile(OutputCFile);
556
Misha Brukmand792c9b2003-07-24 18:17:43 +0000557 return 0;
558}
559
560
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000561/// diffProgram - This method executes the specified module and diffs the output
562/// against the file specified by ReferenceOutputFile. If the output is
563/// different, true is returned.
564///
Misha Brukmand792c9b2003-07-24 18:17:43 +0000565bool BugDriver::diffProgram(const std::string &BytecodeFile,
566 const std::string &SharedObject,
Chris Lattner16a41312003-04-24 17:02:17 +0000567 bool RemoveBytecode) {
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000568 // Execute the program, generating an output file...
Misha Brukmand792c9b2003-07-24 18:17:43 +0000569 std::string Output = executeProgram("", BytecodeFile, SharedObject);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000570
571 std::ifstream ReferenceFile(ReferenceOutputFile.c_str());
572 if (!ReferenceFile) {
573 std::cerr << "Couldn't open reference output file '"
Misha Brukmand792c9b2003-07-24 18:17:43 +0000574 << ReferenceOutputFile << "'\n";
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000575 exit(1);
576 }
577
578 std::ifstream OutputFile(Output.c_str());
579 if (!OutputFile) {
580 std::cerr << "Couldn't open output file: " << Output << "'!\n";
581 exit(1);
582 }
583
584 bool FilesDifferent = false;
585
586 // Compare the two files...
587 int C1, C2;
588 do {
589 C1 = ReferenceFile.get();
590 C2 = OutputFile.get();
591 if (C1 != C2) { FilesDifferent = true; break; }
592 } while (C1 != EOF);
593
594 removeFile(Output);
Chris Lattner16a41312003-04-24 17:02:17 +0000595 if (RemoveBytecode) removeFile(BytecodeFile);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000596 return FilesDifferent;
597}