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 //===----------------------------------------------------------------------===//
15 #include "BugDriver.h"
16 #include "llvm/Support/ToolRunner.h"
17 #include "Support/CommandLine.h"
18 #include "Support/Debug.h"
19 #include "Support/FileUtilities.h"
20 #include "Support/SystemUtils.h"
25 // OutputType - Allow the user to specify the way code should be run, to test
26 // for miscompilation.
29 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
33 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
34 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
35 clEnumValN(RunLLI, "run-int",
36 "Execute with the interpreter"),
37 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
38 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
39 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
44 CheckProgramExitCode("check-exit-code",
45 cl::desc("Assume nonzero exit code is failure (default on)"),
49 InputFile("input", cl::init("/dev/null"),
50 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
53 AdditionalSOs("additional-so",
54 cl::desc("Additional shared objects to load "
55 "into executing programs"));
59 // Anything specified after the --args option are taken as arguments to the
60 // program being debugged.
62 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
63 cl::ZeroOrMore, cl::PositionalEatsArgs);
66 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
67 cl::ZeroOrMore, cl::PositionalEatsArgs);
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
85 switch (InterpreterSel) {
87 InterpreterSel = RunCBE;
88 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
91 InterpreterSel = RunJIT;
92 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
96 InterpreterSel = RunLLC;
97 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
101 InterpreterSel = RunLLI;
102 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
106 InterpreterSel = AutoPick;
107 Message = "Sorry, I can't automatically select an interpreter!\n";
111 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
115 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
119 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
123 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
127 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
130 std::cerr << Message;
132 // Initialize auxiliary tools for debugging
134 cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
135 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
137 gcc = GCC::create(getToolName(), Message);
138 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
140 // If there was an error creating the selected interpreter, quit with error.
141 return Interpreter == 0;
144 /// compileProgram - Try to compile the specified module, throwing an exception
145 /// if an error occurs, or returning normally if not. This is used for code
146 /// generation crash testing.
148 void BugDriver::compileProgram(Module *M) {
149 // Emit the program to a bytecode file...
150 std::string BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
151 if (writeProgramToFile(BytecodeFile, M)) {
152 std::cerr << ToolName << ": Error emitting bytecode to file '"
153 << BytecodeFile << "'!\n";
157 // Remove the temporary bytecode file when we are done.
158 FileRemover BytecodeFileRemover(BytecodeFile);
160 // Actually compile the program!
161 Interpreter->compileProgram(BytecodeFile);
165 /// executeProgram - This method runs "Program", capturing the output of the
166 /// program to a file, returning the filename of the file. A recommended
167 /// filename may be optionally specified.
169 std::string BugDriver::executeProgram(std::string OutputFile,
170 std::string BytecodeFile,
171 const std::string &SharedObj,
172 AbstractInterpreter *AI,
173 bool *ProgramExitedNonzero) {
174 if (AI == 0) AI = Interpreter;
175 assert(AI && "Interpreter should have been created already!");
176 bool CreatedBytecode = false;
177 if (BytecodeFile.empty()) {
178 // Emit the program to a bytecode file...
179 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
181 if (writeProgramToFile(BytecodeFile, Program)) {
182 std::cerr << ToolName << ": Error emitting bytecode to file '"
183 << BytecodeFile << "'!\n";
186 CreatedBytecode = true;
189 // Remove the temporary bytecode file when we are done.
190 FileRemover BytecodeFileRemover(BytecodeFile, CreatedBytecode);
192 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
194 // Check to see if this is a valid output filename...
195 OutputFile = getUniqueFilename(OutputFile);
197 // Figure out which shared objects to run, if any.
198 std::vector<std::string> SharedObjs(AdditionalSOs);
199 if (!SharedObj.empty())
200 SharedObjs.push_back(SharedObj);
202 // Actually execute the program!
203 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
204 OutputFile, SharedObjs);
206 if (ProgramExitedNonzero != 0)
207 *ProgramExitedNonzero = (RetVal != 0);
209 // Return the filename we captured the output to.
213 /// executeProgramWithCBE - Used to create reference output with the C
214 /// backend, if reference output is not provided.
216 std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
217 bool ProgramExitedNonzero;
218 std::string outFN = executeProgram(OutputFile, "", "",
219 (AbstractInterpreter*)cbe,
220 &ProgramExitedNonzero);
221 if (ProgramExitedNonzero) {
223 << "Warning: While generating reference output, program exited with\n"
224 << "non-zero exit code. This will NOT be treated as a failure.\n";
225 CheckProgramExitCode = false;
230 std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
231 assert(Interpreter && "Interpreter should have been created already!");
232 std::string OutputCFile;
235 cbe->OutputC(BytecodeFile, OutputCFile);
237 #if 0 /* This is an alternative, as yet unimplemented */
240 LLC *llc = createLLCtool(Message);
241 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
242 std::cerr << "Could not generate asm code with `llc', exiting.\n";
247 std::string SharedObjectFile;
248 if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
251 // Remove the intermediate C file
252 removeFile(OutputCFile);
254 return "./" + SharedObjectFile;
258 /// diffProgram - This method executes the specified module and diffs the output
259 /// against the file specified by ReferenceOutputFile. If the output is
260 /// different, true is returned.
262 bool BugDriver::diffProgram(const std::string &BytecodeFile,
263 const std::string &SharedObject,
264 bool RemoveBytecode) {
265 bool ProgramExitedNonzero;
267 // Execute the program, generating an output file...
268 std::string Output = executeProgram("", BytecodeFile, SharedObject, 0,
269 &ProgramExitedNonzero);
271 // If we're checking the program exit code, assume anything nonzero is bad.
272 if (CheckProgramExitCode && ProgramExitedNonzero) {
274 if (RemoveBytecode) removeFile(BytecodeFile);
279 bool FilesDifferent = false;
280 if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
281 if (!Error.empty()) {
282 std::cerr << "While diffing output: " << Error << '\n';
285 FilesDifferent = true;
288 // Remove the generated output.
291 // Remove the bytecode file if we are supposed to.
292 if (RemoveBytecode) removeFile(BytecodeFile);
293 return FilesDifferent;
296 bool BugDriver::isExecutingJIT() {
297 return InterpreterSel == RunJIT;