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"
35 // OutputType - Allow the user to specify the way code should be run, to test
36 // for miscompilation.
39 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
43 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
44 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
45 clEnumValN(RunLLI, "run-int", "Execute with the interpreter"),
46 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
47 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
48 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
53 InputFile("input", cl::init("/dev/null"),
54 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
57 AdditionalSOs("additional-so",
58 cl::desc("Additional shared objects to load "
59 "into executing programs"));
63 // Anything specified after the --args option are taken as arguments to the
64 // program being debugged.
66 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
70 //===----------------------------------------------------------------------===//
71 // BugDriver method implementation
74 /// initializeExecutionEnvironment - This method is used to set up the
75 /// environment for executing LLVM programs.
77 bool BugDriver::initializeExecutionEnvironment() {
78 std::cout << "Initializing execution environment: ";
80 // Create an instance of the AbstractInterpreter interface as specified on
83 switch (InterpreterSel) {
85 InterpreterSel = RunCBE;
86 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message);
88 InterpreterSel = RunJIT;
89 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
92 InterpreterSel = RunLLC;
93 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
96 InterpreterSel = RunLLI;
97 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
100 InterpreterSel = AutoPick;
101 Message = "Sorry, I can't automatically select an interpreter!\n";
105 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
108 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
111 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
114 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message);
117 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
120 std::cerr << Message;
122 // Initialize auxiliary tools for debugging
123 cbe = AbstractInterpreter::createCBE(getToolName(), Message);
124 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
125 gcc = GCC::create(getToolName(), Message);
126 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
128 // If there was an error creating the selected interpreter, quit with error.
129 return Interpreter == 0;
133 /// executeProgram - This method runs "Program", capturing the output of the
134 /// program to a file, returning the filename of the file. A recommended
135 /// filename may be optionally specified.
137 std::string BugDriver::executeProgram(std::string OutputFile,
138 std::string BytecodeFile,
139 const std::string &SharedObj,
140 AbstractInterpreter *AI) {
141 if (AI == 0) AI = Interpreter;
142 assert(AI && "Interpreter should have been created already!");
143 bool CreatedBytecode = false;
144 if (BytecodeFile.empty()) {
145 // Emit the program to a bytecode file...
146 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
148 if (writeProgramToFile(BytecodeFile, Program)) {
149 std::cerr << ToolName << ": Error emitting bytecode to file '"
150 << BytecodeFile << "'!\n";
153 CreatedBytecode = true;
156 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
158 // Check to see if this is a valid output filename...
159 OutputFile = getUniqueFilename(OutputFile);
161 // Figure out which shared objects to run, if any.
162 std::vector<std::string> SharedObjs(AdditionalSOs);
163 if (!SharedObj.empty())
164 SharedObjs.push_back(SharedObj);
166 // Actually execute the program!
167 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
168 OutputFile, SharedObjs);
171 // Remove the temporary bytecode file.
172 if (CreatedBytecode) removeFile(BytecodeFile);
174 // Return the filename we captured the output to.
179 std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
180 assert(Interpreter && "Interpreter should have been created already!");
181 std::string OutputCFile;
184 cbe->OutputC(BytecodeFile, OutputCFile);
186 #if 0 /* This is an alternative, as yet unimplemented */
189 LLC *llc = createLLCtool(Message);
190 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
191 std::cerr << "Could not generate asm code with `llc', exiting.\n";
196 std::string SharedObjectFile;
197 if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
200 // Remove the intermediate C file
201 removeFile(OutputCFile);
203 return "./" + SharedObjectFile;
207 /// diffProgram - This method executes the specified module and diffs the output
208 /// against the file specified by ReferenceOutputFile. If the output is
209 /// different, true is returned.
211 bool BugDriver::diffProgram(const std::string &BytecodeFile,
212 const std::string &SharedObject,
213 bool RemoveBytecode) {
214 // Execute the program, generating an output file...
215 std::string Output = executeProgram("", BytecodeFile, SharedObject);
218 bool FilesDifferent = false;
219 if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
220 if (!Error.empty()) {
221 std::cerr << "While diffing output: " << Error << "\n";
224 FilesDifferent = true;
227 // Remove the generated output.
230 // Remove the bytecode file if we are supposed to.
231 if (RemoveBytecode) removeFile(BytecodeFile);
232 return FilesDifferent;
235 bool BugDriver::isExecutingJIT() {
236 return InterpreterSel == RunJIT;