1 //===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains code used to execute the program utilizing one of the
11 // various ways of running LLVM bytecode.
13 //===----------------------------------------------------------------------===//
18 1. Bugpoint should not leave any files behind if the program works properly
19 2. There should be an option to specify the program name, which specifies a
20 unique string to put into output files. This allows operation in the
21 SingleSource directory, e.g. default to the first input filename.
24 #include "BugDriver.h"
25 #include "Support/CommandLine.h"
26 #include "Support/Debug.h"
27 #include "Support/FileUtilities.h"
28 #include "Support/SystemUtils.h"
29 #include "llvm/Support/ToolRunner.h"
34 // OutputType - Allow the user to specify the way code should be run, to test
35 // for miscompilation.
38 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
42 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
43 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
44 clEnumValN(RunLLI, "run-int", "Execute with the interpreter"),
45 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
46 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
47 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
52 InputFile("input", cl::init("/dev/null"),
53 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
56 AdditionalSOs("additional-so",
57 cl::desc("Additional shared objects to load "
58 "into executing programs"));
61 // Anything specified after the --args option are taken as arguments to the
62 // program being debugged.
64 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
67 //===----------------------------------------------------------------------===//
68 // BugDriver method implementation
71 /// initializeExecutionEnvironment - This method is used to set up the
72 /// environment for executing LLVM programs.
74 bool BugDriver::initializeExecutionEnvironment() {
75 std::cout << "Initializing execution environment: ";
77 // Create an instance of the AbstractInterpreter interface as specified on
80 switch (InterpreterSel) {
82 InterpreterSel = RunCBE;
83 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message);
85 InterpreterSel = RunJIT;
86 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
89 InterpreterSel = RunLLC;
90 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
93 InterpreterSel = RunLLI;
94 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
97 InterpreterSel = AutoPick;
98 Message = "Sorry, I can't automatically select an interpreter!\n";
102 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
105 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
108 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
111 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message);
114 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
117 std::cerr << Message;
119 // Initialize auxiliary tools for debugging
120 cbe = AbstractInterpreter::createCBE(getToolName(), Message);
121 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
122 gcc = GCC::create(getToolName(), Message);
123 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
125 // If there was an error creating the selected interpreter, quit with error.
126 return Interpreter == 0;
130 /// executeProgram - This method runs "Program", capturing the output of the
131 /// program to a file, returning the filename of the file. A recommended
132 /// filename may be optionally specified.
134 std::string BugDriver::executeProgram(std::string OutputFile,
135 std::string BytecodeFile,
136 const std::string &SharedObj,
137 AbstractInterpreter *AI) {
138 if (AI == 0) AI = Interpreter;
139 assert(AI && "Interpreter should have been created already!");
140 bool CreatedBytecode = false;
141 if (BytecodeFile.empty()) {
142 // Emit the program to a bytecode file...
143 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
145 if (writeProgramToFile(BytecodeFile, Program)) {
146 std::cerr << ToolName << ": Error emitting bytecode to file '"
147 << BytecodeFile << "'!\n";
150 CreatedBytecode = true;
153 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
155 // Check to see if this is a valid output filename...
156 OutputFile = getUniqueFilename(OutputFile);
158 // Figure out which shared objects to run, if any.
159 std::vector<std::string> SharedObjs(AdditionalSOs);
160 if (!SharedObj.empty())
161 SharedObjs.push_back(SharedObj);
163 // Actually execute the program!
164 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
165 OutputFile, SharedObjs);
168 // Remove the temporary bytecode file.
169 if (CreatedBytecode) removeFile(BytecodeFile);
171 // Return the filename we captured the output to.
176 std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
177 assert(Interpreter && "Interpreter should have been created already!");
178 std::string OutputCFile;
181 cbe->OutputC(BytecodeFile, OutputCFile);
183 #if 0 /* This is an alternative, as yet unimplemented */
186 LLC *llc = createLLCtool(Message);
187 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
188 std::cerr << "Could not generate asm code with `llc', exiting.\n";
193 std::string SharedObjectFile;
194 if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
197 // Remove the intermediate C file
198 removeFile(OutputCFile);
200 return "./" + SharedObjectFile;
204 /// diffProgram - This method executes the specified module and diffs the output
205 /// against the file specified by ReferenceOutputFile. If the output is
206 /// different, true is returned.
208 bool BugDriver::diffProgram(const std::string &BytecodeFile,
209 const std::string &SharedObject,
210 bool RemoveBytecode) {
211 // Execute the program, generating an output file...
212 std::string Output = executeProgram("", BytecodeFile, SharedObject);
215 bool FilesDifferent = false;
216 if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
217 if (!Error.empty()) {
218 std::cerr << "While diffing output: " << Error << "\n";
221 FilesDifferent = true;
224 // Remove the generated output.
227 // Remove the bytecode file if we are supposed to.
228 if (RemoveBytecode) removeFile(BytecodeFile);
229 return FilesDifferent;
232 bool BugDriver::isExecutingJIT() {
233 return InterpreterSel == RunJIT;