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 AdditionalSOs("additional-so",
48 cl::desc("Additional shared objects to load "
49 "into executing programs"));
52 // Anything specified after the --args option are taken as arguments to the
53 // program being debugged.
55 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
58 //===----------------------------------------------------------------------===//
59 // BugDriver method implementation
62 /// initializeExecutionEnvironment - This method is used to set up the
63 /// environment for executing LLVM programs.
65 bool BugDriver::initializeExecutionEnvironment() {
66 std::cout << "Initializing execution environment: ";
68 // FIXME: This should default to searching for the best interpreter to use on
69 // this platform, which would be JIT, then LLC, then CBE, then LLI.
71 // Create an instance of the AbstractInterpreter interface as specified on
74 switch (InterpreterSel) {
76 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
79 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
82 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
85 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message);
88 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
93 // Initialize auxiliary tools for debugging
94 cbe = AbstractInterpreter::createCBE(getToolName(), Message);
95 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
96 gcc = GCC::create(getToolName(), Message);
97 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
99 // If there was an error creating the selected interpreter, quit with error.
100 return Interpreter == 0;
104 /// executeProgram - This method runs "Program", capturing the output of the
105 /// program to a file, returning the filename of the file. A recommended
106 /// filename may be optionally specified.
108 std::string BugDriver::executeProgram(std::string OutputFile,
109 std::string BytecodeFile,
110 const std::string &SharedObj,
111 AbstractInterpreter *AI) {
112 if (AI == 0) AI = Interpreter;
113 assert(AI && "Interpreter should have been created already!");
114 bool CreatedBytecode = false;
115 if (BytecodeFile.empty()) {
116 // Emit the program to a bytecode file...
117 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
119 if (writeProgramToFile(BytecodeFile, Program)) {
120 std::cerr << ToolName << ": Error emitting bytecode to file '"
121 << BytecodeFile << "'!\n";
124 CreatedBytecode = true;
127 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
129 // Check to see if this is a valid output filename...
130 OutputFile = getUniqueFilename(OutputFile);
132 // Figure out which shared objects to run, if any.
133 std::vector<std::string> SharedObjs(AdditionalSOs);
134 if (!SharedObj.empty())
135 SharedObjs.push_back(SharedObj);
137 // Actually execute the program!
138 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
139 OutputFile, SharedObjs);
142 // Remove the temporary bytecode file.
143 if (CreatedBytecode) removeFile(BytecodeFile);
145 // Return the filename we captured the output to.
150 std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
151 assert(Interpreter && "Interpreter should have been created already!");
152 std::string OutputCFile;
155 cbe->OutputC(BytecodeFile, OutputCFile);
157 #if 0 /* This is an alternative, as yet unimplemented */
160 LLC *llc = createLLCtool(Message);
161 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
162 std::cerr << "Could not generate asm code with `llc', exiting.\n";
167 std::string SharedObjectFile;
168 if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
171 // Remove the intermediate C file
172 removeFile(OutputCFile);
174 return SharedObjectFile;
178 /// diffProgram - This method executes the specified module and diffs the output
179 /// against the file specified by ReferenceOutputFile. If the output is
180 /// different, true is returned.
182 bool BugDriver::diffProgram(const std::string &BytecodeFile,
183 const std::string &SharedObject,
184 bool RemoveBytecode) {
185 // Execute the program, generating an output file...
186 std::string Output = executeProgram("", BytecodeFile, SharedObject);
189 bool FilesDifferent = false;
190 if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
191 if (!Error.empty()) {
192 std::cerr << "While diffing output: " << Error << "\n";
195 FilesDifferent = true;
198 if (RemoveBytecode) removeFile(BytecodeFile);
199 return FilesDifferent;
202 bool BugDriver::isExecutingJIT() {
203 return InterpreterSel == RunJIT;