blob: 596aeb95a887934a2563034b1a15a42f3458f4b4 [file] [log] [blame]
Chris Lattnerde4aa4c2002-12-23 23:50:16 +00001//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
John Criswell09344dc2003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerde4aa4c2002-12-23 23:50:16 +00009//
10// This file contains code used to execute the program utilizing one of the
11// various ways of running LLVM bytecode.
12//
13//===----------------------------------------------------------------------===//
14
15/*
16BUGPOINT NOTES:
17
181. Bugpoint should not leave any files behind if the program works properly
192. 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
Misha Brukmand792c9b2003-07-24 18:17:43 +000021 SingleSource directory, e.g. default to the first input filename.
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000022*/
23
24#include "BugDriver.h"
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000025#include "Support/CommandLine.h"
Chris Lattnerf0c69642003-08-01 22:13:59 +000026#include "Support/Debug.h"
Chris Lattneraa997fb2003-08-01 20:29:45 +000027#include "Support/FileUtilities.h"
Misha Brukman0c2305b2003-08-07 21:19:30 +000028#include "Support/SystemUtils.h"
Chris Lattner10bd1c22003-10-07 13:45:51 +000029#include "llvm/Support/ToolRunner.h"
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000030#include <fstream>
Chris Lattner7c0f8622002-12-24 00:44:34 +000031#include <iostream>
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000032
33namespace {
34 // OutputType - Allow the user to specify the way code should be run, to test
35 // for miscompilation.
36 //
37 enum OutputType {
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000038 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000039 };
Misha Brukman5bc6a8f2003-09-29 22:40:52 +000040
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000041 cl::opt<OutputType>
42 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000043 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
44 clEnumValN(RunLLI, "run-int", "Execute with the interpreter"),
Misha Brukmand792c9b2003-07-24 18:17:43 +000045 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
46 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
47 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
Chris Lattner16d36a12003-10-18 20:18:20 +000048 0),
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000049 cl::init(AutoPick));
Chris Lattner68efaa72003-04-23 20:31:37 +000050
51 cl::opt<std::string>
52 InputFile("input", cl::init("/dev/null"),
53 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattnerdc92fa62003-10-14 22:24:31 +000054
55 cl::list<std::string>
56 AdditionalSOs("additional-so",
57 cl::desc("Additional shared objects to load "
58 "into executing programs"));
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000059}
60
Misha Brukman40feb362003-07-30 20:15:44 +000061// Anything specified after the --args option are taken as arguments to the
62// program being debugged.
63cl::list<std::string>
64InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
65 cl::ZeroOrMore);
66
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000067//===----------------------------------------------------------------------===//
68// BugDriver method implementation
69//
70
71/// initializeExecutionEnvironment - This method is used to set up the
72/// environment for executing LLVM programs.
73///
74bool BugDriver::initializeExecutionEnvironment() {
75 std::cout << "Initializing execution environment: ";
76
Misha Brukman5bc6a8f2003-09-29 22:40:52 +000077 // Create an instance of the AbstractInterpreter interface as specified on
78 // the command line
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000079 std::string Message;
Chris Lattner7709ec52003-05-03 03:19:41 +000080 switch (InterpreterSel) {
Brian Gaeke9a0bdb12003-10-21 17:41:35 +000081 case AutoPick:
82 InterpreterSel = RunCBE;
83 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message);
84 if (!Interpreter) {
85 InterpreterSel = RunJIT;
86 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
87 }
88 if (!Interpreter) {
89 InterpreterSel = RunLLC;
90 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
91 }
92 if (!Interpreter) {
93 InterpreterSel = RunLLI;
94 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
95 }
96 if (!Interpreter) {
97 InterpreterSel = AutoPick;
98 Message = "Sorry, I can't automatically select an interpreter!\n";
99 }
100 break;
Chris Lattner3f6e5222003-10-14 21:59:36 +0000101 case RunLLI:
102 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
103 break;
104 case RunLLC:
105 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
106 break;
107 case RunJIT:
108 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
109 break;
110 case RunCBE:
111 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message);
112 break;
Chris Lattner7709ec52003-05-03 03:19:41 +0000113 default:
Misha Brukman5bc6a8f2003-09-29 22:40:52 +0000114 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
Chris Lattner7709ec52003-05-03 03:19:41 +0000115 break;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000116 }
Misha Brukman5bc6a8f2003-09-29 22:40:52 +0000117 std::cerr << Message;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000118
Misha Brukman0fd31722003-07-24 21:59:10 +0000119 // Initialize auxiliary tools for debugging
Chris Lattner3f6e5222003-10-14 21:59:36 +0000120 cbe = AbstractInterpreter::createCBE(getToolName(), Message);
Misha Brukman0fd31722003-07-24 21:59:10 +0000121 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
Chris Lattner3f6e5222003-10-14 21:59:36 +0000122 gcc = GCC::create(getToolName(), Message);
Misha Brukman0fd31722003-07-24 21:59:10 +0000123 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
124
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000125 // If there was an error creating the selected interpreter, quit with error.
126 return Interpreter == 0;
127}
128
129
130/// executeProgram - This method runs "Program", capturing the output of the
131/// program to a file, returning the filename of the file. A recommended
132/// filename may be optionally specified.
133///
134std::string BugDriver::executeProgram(std::string OutputFile,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000135 std::string BytecodeFile,
Chris Lattner3f6e5222003-10-14 21:59:36 +0000136 const std::string &SharedObj,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000137 AbstractInterpreter *AI) {
Chris Lattner3f6e5222003-10-14 21:59:36 +0000138 if (AI == 0) AI = Interpreter;
139 assert(AI && "Interpreter should have been created already!");
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000140 bool CreatedBytecode = false;
141 if (BytecodeFile.empty()) {
142 // Emit the program to a bytecode file...
143 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
144
145 if (writeProgramToFile(BytecodeFile, Program)) {
146 std::cerr << ToolName << ": Error emitting bytecode to file '"
Misha Brukmand792c9b2003-07-24 18:17:43 +0000147 << BytecodeFile << "'!\n";
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000148 exit(1);
149 }
150 CreatedBytecode = true;
151 }
152
153 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
Misha Brukmand792c9b2003-07-24 18:17:43 +0000154
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000155 // Check to see if this is a valid output filename...
156 OutputFile = getUniqueFilename(OutputFile);
157
Chris Lattner3f6e5222003-10-14 21:59:36 +0000158 // Figure out which shared objects to run, if any.
Chris Lattnerdc92fa62003-10-14 22:24:31 +0000159 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner3f6e5222003-10-14 21:59:36 +0000160 if (!SharedObj.empty())
161 SharedObjs.push_back(SharedObj);
162
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000163 // Actually execute the program!
Chris Lattner3f6e5222003-10-14 21:59:36 +0000164 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
165 OutputFile, SharedObjs);
166
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000167
168 // Remove the temporary bytecode file.
Misha Brukmand792c9b2003-07-24 18:17:43 +0000169 if (CreatedBytecode) removeFile(BytecodeFile);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000170
171 // Return the filename we captured the output to.
172 return OutputFile;
173}
174
Misha Brukmand792c9b2003-07-24 18:17:43 +0000175
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000176std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
Misha Brukmand792c9b2003-07-24 18:17:43 +0000177 assert(Interpreter && "Interpreter should have been created already!");
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000178 std::string OutputCFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000179
180 // Using CBE
Misha Brukmand792c9b2003-07-24 18:17:43 +0000181 cbe->OutputC(BytecodeFile, OutputCFile);
182
183#if 0 /* This is an alternative, as yet unimplemented */
184 // Using LLC
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000185 std::string Message;
Misha Brukman5bc6a8f2003-09-29 22:40:52 +0000186 LLC *llc = createLLCtool(Message);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000187 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
188 std::cerr << "Could not generate asm code with `llc', exiting.\n";
189 exit(1);
190 }
191#endif
192
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000193 std::string SharedObjectFile;
Chris Lattner3f6e5222003-10-14 21:59:36 +0000194 if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000195 exit(1);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000196
197 // Remove the intermediate C file
198 removeFile(OutputCFile);
199
Chris Lattner2b97d6e2003-10-19 21:54:13 +0000200 return "./" + SharedObjectFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000201}
202
203
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000204/// diffProgram - This method executes the specified module and diffs the output
205/// against the file specified by ReferenceOutputFile. If the output is
206/// different, true is returned.
207///
Misha Brukmand792c9b2003-07-24 18:17:43 +0000208bool BugDriver::diffProgram(const std::string &BytecodeFile,
209 const std::string &SharedObject,
Chris Lattner16a41312003-04-24 17:02:17 +0000210 bool RemoveBytecode) {
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000211 // Execute the program, generating an output file...
Misha Brukmand792c9b2003-07-24 18:17:43 +0000212 std::string Output = executeProgram("", BytecodeFile, SharedObject);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000213
Chris Lattneraa997fb2003-08-01 20:29:45 +0000214 std::string Error;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000215 bool FilesDifferent = false;
Chris Lattneraa997fb2003-08-01 20:29:45 +0000216 if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
217 if (!Error.empty()) {
218 std::cerr << "While diffing output: " << Error << "\n";
219 exit(1);
220 }
221 FilesDifferent = true;
222 }
Chris Lattner759b9932003-10-18 21:02:51 +0000223
224 // Remove the generated output.
225 removeFile(Output);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000226
Chris Lattner759b9932003-10-18 21:02:51 +0000227 // Remove the bytecode file if we are supposed to.
Chris Lattner16a41312003-04-24 17:02:17 +0000228 if (RemoveBytecode) removeFile(BytecodeFile);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000229 return FilesDifferent;
230}
Misha Brukman539f9592003-07-28 19:16:14 +0000231
232bool BugDriver::isExecutingJIT() {
233 return InterpreterSel == RunJIT;
234}