1 //===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
3 // This file contains code used to execute the program utilizing one of the
4 // various ways of running LLVM bytecode.
6 //===----------------------------------------------------------------------===//
11 1. Bugpoint should not leave any files behind if the program works properly
12 2. 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
14 SingleSource directory, e.g. default to the first input filename.
17 #include "BugDriver.h"
18 #include "Support/CommandLine.h"
19 #include "Support/Debug.h"
20 #include "Support/FileUtilities.h"
21 #include "Support/SystemUtils.h"
22 #include "llvm/Support/ToolRunner.h"
27 // OutputType - Allow the user to specify the way code should be run, to test
28 // for miscompilation.
31 RunLLI, RunJIT, RunLLC, RunCBE
35 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
36 cl::values(clEnumValN(RunLLI, "run-lli", "Execute with LLI"),
37 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
38 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
39 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
43 InputFile("input", cl::init("/dev/null"),
44 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
47 // Anything specified after the --args option are taken as arguments to the
48 // program being debugged.
50 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
53 //===----------------------------------------------------------------------===//
54 // BugDriver method implementation
57 /// initializeExecutionEnvironment - This method is used to set up the
58 /// environment for executing LLVM programs.
60 bool BugDriver::initializeExecutionEnvironment() {
61 std::cout << "Initializing execution environment: ";
63 // FIXME: This should default to searching for the best interpreter to use on
64 // this platform, which would be JIT, then LLC, then CBE, then LLI.
66 // Create an instance of the AbstractInterpreter interface as specified on
69 switch (InterpreterSel) {
70 case RunLLI: Interpreter = createLLItool(getToolName(), Message); break;
71 case RunLLC: Interpreter = createLLCtool(getToolName(), Message); break;
72 case RunJIT: Interpreter = createJITtool(getToolName(), Message); break;
73 case RunCBE: Interpreter = createCBEtool(getToolName(), Message); break;
75 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
80 // Initialize auxiliary tools for debugging
81 cbe = createCBEtool(getToolName(), Message);
82 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
83 gcc = createGCCtool(getToolName(), Message);
84 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
86 // If there was an error creating the selected interpreter, quit with error.
87 return Interpreter == 0;
91 /// executeProgram - This method runs "Program", capturing the output of the
92 /// program to a file, returning the filename of the file. A recommended
93 /// filename may be optionally specified.
95 std::string BugDriver::executeProgram(std::string OutputFile,
96 std::string BytecodeFile,
97 std::string SharedObject,
98 AbstractInterpreter *AI) {
99 assert((Interpreter||AI) && "Interpreter should have been created already!");
100 bool CreatedBytecode = false;
101 if (BytecodeFile.empty()) {
102 // Emit the program to a bytecode file...
103 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
105 if (writeProgramToFile(BytecodeFile, Program)) {
106 std::cerr << ToolName << ": Error emitting bytecode to file '"
107 << BytecodeFile << "'!\n";
110 CreatedBytecode = true;
113 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
115 // Check to see if this is a valid output filename...
116 OutputFile = getUniqueFilename(OutputFile);
118 // Actually execute the program!
119 int RetVal = (AI != 0) ?
120 AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile, OutputFile,
122 Interpreter->ExecuteProgram(BytecodeFile, InputArgv,
123 InputFile, OutputFile, SharedObject);
125 // Remove the temporary bytecode file.
126 if (CreatedBytecode) removeFile(BytecodeFile);
128 // Return the filename we captured the output to.
132 std::string BugDriver::executeProgramWithCBE(std::string OutputFile,
133 std::string BytecodeFile,
134 std::string SharedObject) {
135 return executeProgram(OutputFile, BytecodeFile, SharedObject, cbe);
138 int BugDriver::compileSharedObject(const std::string &BytecodeFile,
139 std::string &SharedObject) {
140 assert(Interpreter && "Interpreter should have been created already!");
141 std::string Message, OutputCFile;
144 cbe->OutputC(BytecodeFile, OutputCFile);
146 #if 0 /* This is an alternative, as yet unimplemented */
148 LLC *llc = createLLCtool(Message);
149 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
150 std::cerr << "Could not generate asm code with `llc', exiting.\n";
155 gcc->MakeSharedObject(OutputCFile, CFile, SharedObject);
157 // Remove the intermediate C file
158 removeFile(OutputCFile);
164 /// diffProgram - This method executes the specified module and diffs the output
165 /// against the file specified by ReferenceOutputFile. If the output is
166 /// different, true is returned.
168 bool BugDriver::diffProgram(const std::string &BytecodeFile,
169 const std::string &SharedObject,
170 bool RemoveBytecode) {
171 // Execute the program, generating an output file...
172 std::string Output = executeProgram("", BytecodeFile, SharedObject);
175 bool FilesDifferent = false;
176 if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
177 if (!Error.empty()) {
178 std::cerr << "While diffing output: " << Error << "\n";
181 FilesDifferent = true;
184 if (RemoveBytecode) removeFile(BytecodeFile);
185 return FilesDifferent;
188 bool BugDriver::isExecutingJIT() {
189 return InterpreterSel == RunJIT;