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 RunLLI, RunJIT, RunLLC, RunCBE
42 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
43 cl::values(clEnumValN(RunLLI, "run-int", "Execute with the interpreter"),
44 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
45 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
46 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
51 InputFile("input", cl::init("/dev/null"),
52 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
55 AdditionalSOs("additional-so",
56 cl::desc("Additional shared objects to load "
57 "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>..."),
66 //===----------------------------------------------------------------------===//
67 // BugDriver method implementation
70 /// initializeExecutionEnvironment - This method is used to set up the
71 /// environment for executing LLVM programs.
73 bool BugDriver::initializeExecutionEnvironment() {
74 std::cout << "Initializing execution environment: ";
76 // FIXME: This should default to searching for the best interpreter to use on
77 // this platform, which would be JIT, then LLC, then CBE, then LLI.
79 // Create an instance of the AbstractInterpreter interface as specified on
82 switch (InterpreterSel) {
84 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
87 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
90 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
93 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message);
96 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
101 // Initialize auxiliary tools for debugging
102 cbe = AbstractInterpreter::createCBE(getToolName(), Message);
103 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
104 gcc = GCC::create(getToolName(), Message);
105 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
107 // If there was an error creating the selected interpreter, quit with error.
108 return Interpreter == 0;
112 /// executeProgram - This method runs "Program", capturing the output of the
113 /// program to a file, returning the filename of the file. A recommended
114 /// filename may be optionally specified.
116 std::string BugDriver::executeProgram(std::string OutputFile,
117 std::string BytecodeFile,
118 const std::string &SharedObj,
119 AbstractInterpreter *AI) {
120 if (AI == 0) AI = Interpreter;
121 assert(AI && "Interpreter should have been created already!");
122 bool CreatedBytecode = false;
123 if (BytecodeFile.empty()) {
124 // Emit the program to a bytecode file...
125 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
127 if (writeProgramToFile(BytecodeFile, Program)) {
128 std::cerr << ToolName << ": Error emitting bytecode to file '"
129 << BytecodeFile << "'!\n";
132 CreatedBytecode = true;
135 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
137 // Check to see if this is a valid output filename...
138 OutputFile = getUniqueFilename(OutputFile);
140 // Figure out which shared objects to run, if any.
141 std::vector<std::string> SharedObjs(AdditionalSOs);
142 if (!SharedObj.empty())
143 SharedObjs.push_back(SharedObj);
145 // Actually execute the program!
146 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
147 OutputFile, SharedObjs);
150 // Remove the temporary bytecode file.
151 if (CreatedBytecode) removeFile(BytecodeFile);
153 // Return the filename we captured the output to.
158 std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
159 assert(Interpreter && "Interpreter should have been created already!");
160 std::string OutputCFile;
163 cbe->OutputC(BytecodeFile, OutputCFile);
165 #if 0 /* This is an alternative, as yet unimplemented */
168 LLC *llc = createLLCtool(Message);
169 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
170 std::cerr << "Could not generate asm code with `llc', exiting.\n";
175 std::string SharedObjectFile;
176 if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
179 // Remove the intermediate C file
180 removeFile(OutputCFile);
182 return "./" + SharedObjectFile;
186 /// diffProgram - This method executes the specified module and diffs the output
187 /// against the file specified by ReferenceOutputFile. If the output is
188 /// different, true is returned.
190 bool BugDriver::diffProgram(const std::string &BytecodeFile,
191 const std::string &SharedObject,
192 bool RemoveBytecode) {
193 // Execute the program, generating an output file...
194 std::string Output = executeProgram("", BytecodeFile, SharedObject);
197 bool FilesDifferent = false;
198 if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
199 if (!Error.empty()) {
200 std::cerr << "While diffing output: " << Error << "\n";
203 FilesDifferent = true;
206 // Remove the generated output.
209 // Remove the bytecode file if we are supposed to.
210 if (RemoveBytecode) removeFile(BytecodeFile);
211 return FilesDifferent;
214 bool BugDriver::isExecutingJIT() {
215 return InterpreterSel == RunJIT;