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"
26 // OutputType - Allow the user to specify the way code should be run, to test
27 // for miscompilation.
30 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
34 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
35 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
36 clEnumValN(RunLLI, "run-int",
37 "Execute with the interpreter"),
38 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
39 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
40 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
45 CheckProgramExitCode("check-exit-code",
46 cl::desc("Assume nonzero exit code is failure (default on)"),
50 InputFile("input", cl::init("/dev/null"),
51 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
54 AdditionalSOs("additional-so",
55 cl::desc("Additional shared objects to load "
56 "into executing programs"));
60 // Anything specified after the --args option are taken as arguments to the
61 // program being debugged.
63 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
64 cl::ZeroOrMore, cl::PositionalEatsArgs);
67 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
68 cl::ZeroOrMore, cl::PositionalEatsArgs);
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
86 switch (InterpreterSel) {
88 InterpreterSel = RunCBE;
89 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
92 InterpreterSel = RunJIT;
93 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
97 InterpreterSel = RunLLC;
98 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
102 InterpreterSel = RunLLI;
103 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
107 InterpreterSel = AutoPick;
108 Message = "Sorry, I can't automatically select an interpreter!\n";
112 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
116 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
120 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
124 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
128 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
131 std::cerr << Message;
133 // Initialize auxiliary tools for debugging
135 cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
136 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
138 gcc = GCC::create(getToolName(), Message);
139 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
141 // If there was an error creating the selected interpreter, quit with error.
142 return Interpreter == 0;
145 /// compileProgram - Try to compile the specified module, throwing an exception
146 /// if an error occurs, or returning normally if not. This is used for code
147 /// generation crash testing.
149 void BugDriver::compileProgram(Module *M) {
150 // Emit the program to a bytecode file...
151 std::string BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
152 if (writeProgramToFile(BytecodeFile, M)) {
153 std::cerr << ToolName << ": Error emitting bytecode to file '"
154 << BytecodeFile << "'!\n";
158 // Remove the temporary bytecode file when we are done.
159 FileRemover BytecodeFileRemover(BytecodeFile);
161 // Actually compile the program!
162 Interpreter->compileProgram(BytecodeFile);
166 /// executeProgram - This method runs "Program", capturing the output of the
167 /// program to a file, returning the filename of the file. A recommended
168 /// filename may be optionally specified.
170 std::string BugDriver::executeProgram(std::string OutputFile,
171 std::string BytecodeFile,
172 const std::string &SharedObj,
173 AbstractInterpreter *AI,
174 bool *ProgramExitedNonzero) {
175 if (AI == 0) AI = Interpreter;
176 assert(AI && "Interpreter should have been created already!");
177 bool CreatedBytecode = false;
178 if (BytecodeFile.empty()) {
179 // Emit the program to a bytecode file...
180 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
182 if (writeProgramToFile(BytecodeFile, Program)) {
183 std::cerr << ToolName << ": Error emitting bytecode to file '"
184 << BytecodeFile << "'!\n";
187 CreatedBytecode = true;
190 // Remove the temporary bytecode file when we are done.
191 FileRemover BytecodeFileRemover(BytecodeFile, CreatedBytecode);
193 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
195 // Check to see if this is a valid output filename...
196 OutputFile = getUniqueFilename(OutputFile);
198 // Figure out which shared objects to run, if any.
199 std::vector<std::string> SharedObjs(AdditionalSOs);
200 if (!SharedObj.empty())
201 SharedObjs.push_back(SharedObj);
203 // Actually execute the program!
204 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
205 OutputFile, SharedObjs);
207 if (ProgramExitedNonzero != 0)
208 *ProgramExitedNonzero = (RetVal != 0);
210 // Return the filename we captured the output to.
214 /// executeProgramWithCBE - Used to create reference output with the C
215 /// backend, if reference output is not provided.
217 std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
218 bool ProgramExitedNonzero;
219 std::string outFN = executeProgram(OutputFile, "", "",
220 (AbstractInterpreter*)cbe,
221 &ProgramExitedNonzero);
222 if (ProgramExitedNonzero) {
224 << "Warning: While generating reference output, program exited with\n"
225 << "non-zero exit code. This will NOT be treated as a failure.\n";
226 CheckProgramExitCode = false;
231 std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
232 assert(Interpreter && "Interpreter should have been created already!");
233 std::string OutputCFile;
236 cbe->OutputC(BytecodeFile, OutputCFile);
238 #if 0 /* This is an alternative, as yet unimplemented */
241 LLC *llc = createLLCtool(Message);
242 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
243 std::cerr << "Could not generate asm code with `llc', exiting.\n";
248 std::string SharedObjectFile;
249 if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
252 // Remove the intermediate C file
253 removeFile(OutputCFile);
255 return "./" + SharedObjectFile;
259 /// diffProgram - This method executes the specified module and diffs the output
260 /// against the file specified by ReferenceOutputFile. If the output is
261 /// different, true is returned.
263 bool BugDriver::diffProgram(const std::string &BytecodeFile,
264 const std::string &SharedObject,
265 bool RemoveBytecode) {
266 bool ProgramExitedNonzero;
268 // Execute the program, generating an output file...
269 std::string Output = executeProgram("", BytecodeFile, SharedObject, 0,
270 &ProgramExitedNonzero);
272 // If we're checking the program exit code, assume anything nonzero is bad.
273 if (CheckProgramExitCode && ProgramExitedNonzero) {
275 if (RemoveBytecode) removeFile(BytecodeFile);
280 bool FilesDifferent = false;
281 if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
282 if (!Error.empty()) {
283 std::cerr << "While diffing output: " << Error << "\n";
286 FilesDifferent = true;
289 // Remove the generated output.
292 // Remove the bytecode file if we are supposed to.
293 if (RemoveBytecode) removeFile(BytecodeFile);
294 return FilesDifferent;
297 bool BugDriver::isExecutingJIT() {
298 return InterpreterSel == RunJIT;