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"
36 // OutputType - Allow the user to specify the way code should be run, to test
37 // for miscompilation.
40 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
44 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
45 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
46 clEnumValN(RunLLI, "run-int", "Execute with the interpreter"),
47 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
48 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
49 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
54 InputFile("input", cl::init("/dev/null"),
55 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
58 AdditionalSOs("additional-so",
59 cl::desc("Additional shared objects to load "
60 "into executing programs"));
65 // Anything specified after the --args option are taken as arguments to the
66 // program being debugged.
68 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
71 //===----------------------------------------------------------------------===//
72 // BugDriver method implementation
75 /// initializeExecutionEnvironment - This method is used to set up the
76 /// environment for executing LLVM programs.
78 bool BugDriver::initializeExecutionEnvironment() {
79 std::cout << "Initializing execution environment: ";
81 // Create an instance of the AbstractInterpreter interface as specified on
84 switch (InterpreterSel) {
86 InterpreterSel = RunCBE;
87 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message);
89 InterpreterSel = RunJIT;
90 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
93 InterpreterSel = RunLLC;
94 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
97 InterpreterSel = RunLLI;
98 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
101 InterpreterSel = AutoPick;
102 Message = "Sorry, I can't automatically select an interpreter!\n";
106 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
109 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
112 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
115 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message);
118 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
121 std::cerr << Message;
123 // Initialize auxiliary tools for debugging
124 cbe = AbstractInterpreter::createCBE(getToolName(), Message);
125 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
126 gcc = GCC::create(getToolName(), Message);
127 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
129 // If there was an error creating the selected interpreter, quit with error.
130 return Interpreter == 0;
134 /// executeProgram - This method runs "Program", capturing the output of the
135 /// program to a file, returning the filename of the file. A recommended
136 /// filename may be optionally specified.
138 std::string BugDriver::executeProgram(std::string OutputFile,
139 std::string BytecodeFile,
140 const std::string &SharedObj,
141 AbstractInterpreter *AI) {
142 if (AI == 0) AI = Interpreter;
143 assert(AI && "Interpreter should have been created already!");
144 bool CreatedBytecode = false;
145 if (BytecodeFile.empty()) {
146 // Emit the program to a bytecode file...
147 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
149 if (writeProgramToFile(BytecodeFile, Program)) {
150 std::cerr << ToolName << ": Error emitting bytecode to file '"
151 << BytecodeFile << "'!\n";
154 CreatedBytecode = true;
157 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
159 // Check to see if this is a valid output filename...
160 OutputFile = getUniqueFilename(OutputFile);
162 // Figure out which shared objects to run, if any.
163 std::vector<std::string> SharedObjs(AdditionalSOs);
164 if (!SharedObj.empty())
165 SharedObjs.push_back(SharedObj);
167 // Actually execute the program!
168 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
169 OutputFile, SharedObjs);
172 // Remove the temporary bytecode file.
173 if (CreatedBytecode) removeFile(BytecodeFile);
175 // Return the filename we captured the output to.
180 std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
181 assert(Interpreter && "Interpreter should have been created already!");
182 std::string OutputCFile;
185 cbe->OutputC(BytecodeFile, OutputCFile);
187 #if 0 /* This is an alternative, as yet unimplemented */
190 LLC *llc = createLLCtool(Message);
191 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
192 std::cerr << "Could not generate asm code with `llc', exiting.\n";
197 std::string SharedObjectFile;
198 if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
201 // Remove the intermediate C file
202 removeFile(OutputCFile);
204 return "./" + SharedObjectFile;
208 /// diffProgram - This method executes the specified module and diffs the output
209 /// against the file specified by ReferenceOutputFile. If the output is
210 /// different, true is returned.
212 bool BugDriver::diffProgram(const std::string &BytecodeFile,
213 const std::string &SharedObject,
214 bool RemoveBytecode) {
215 // Execute the program, generating an output file...
216 std::string Output = executeProgram("", BytecodeFile, SharedObject);
219 bool FilesDifferent = false;
220 if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
221 if (!Error.empty()) {
222 std::cerr << "While diffing output: " << Error << "\n";
225 FilesDifferent = true;
228 // Remove the generated output.
231 // Remove the bytecode file if we are supposed to.
232 if (RemoveBytecode) removeFile(BytecodeFile);
233 return FilesDifferent;
236 bool BugDriver::isExecutingJIT() {
237 return InterpreterSel == RunJIT;
240 } // End llvm namespace