blob: a08d63268261467a0caccf94592231cb227a164b [file] [log] [blame]
Chris Lattner4a106452002-12-23 23:50:16 +00001//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
John Criswell7c0e0222003-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 Lattner4a106452002-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
Chris Lattner4a106452002-12-23 23:50:16 +000015#include "BugDriver.h"
Misha Brukmanb687d822004-04-19 03:12:35 +000016#include "llvm/Support/ToolRunner.h"
Chris Lattner4a106452002-12-23 23:50:16 +000017#include "Support/CommandLine.h"
Chris Lattnerc648dab2003-08-01 22:13:59 +000018#include "Support/Debug.h"
Chris Lattner65f62792003-08-01 20:29:45 +000019#include "Support/FileUtilities.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000020#include "Support/SystemUtils.h"
Chris Lattner4a106452002-12-23 23:50:16 +000021#include <fstream>
Chris Lattnere1b52b72002-12-24 00:44:34 +000022#include <iostream>
Brian Gaeked0fde302003-11-11 22:41:34 +000023using namespace llvm;
24
Chris Lattner4a106452002-12-23 23:50:16 +000025namespace {
26 // OutputType - Allow the user to specify the way code should be run, to test
27 // for miscompilation.
28 //
29 enum OutputType {
Brian Gaekeb5ee5092003-10-21 17:41:35 +000030 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
Chris Lattner4a106452002-12-23 23:50:16 +000031 };
Misha Brukman41485562003-09-29 22:40:52 +000032
Chris Lattner4a106452002-12-23 23:50:16 +000033 cl::opt<OutputType>
34 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000035 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
Misha Brukmanb687d822004-04-19 03:12:35 +000036 clEnumValN(RunLLI, "run-int",
37 "Execute with the interpreter"),
Misha Brukman50733362003-07-24 18:17:43 +000038 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
39 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
40 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
Chris Lattner25d66472003-10-18 20:18:20 +000041 0),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000042 cl::init(AutoPick));
Chris Lattner3c053a02003-04-23 20:31:37 +000043
Brian Gaekec5cad212004-02-11 18:37:32 +000044 cl::opt<bool>
45 CheckProgramExitCode("check-exit-code",
46 cl::desc("Assume nonzero exit code is failure (default on)"),
47 cl::init(true));
48
Chris Lattner3c053a02003-04-23 20:31:37 +000049 cl::opt<std::string>
50 InputFile("input", cl::init("/dev/null"),
51 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattner7dac6582003-10-14 22:24:31 +000052
53 cl::list<std::string>
54 AdditionalSOs("additional-so",
55 cl::desc("Additional shared objects to load "
56 "into executing programs"));
Chris Lattner4a106452002-12-23 23:50:16 +000057}
58
Brian Gaeked0fde302003-11-11 22:41:34 +000059namespace llvm {
Chris Lattnerfa761832004-01-14 03:38:37 +000060 // Anything specified after the --args option are taken as arguments to the
61 // program being debugged.
62 cl::list<std::string>
63 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +000064 cl::ZeroOrMore, cl::PositionalEatsArgs);
Brian Gaeke636df3d2004-05-04 21:09:16 +000065
66 cl::list<std::string>
67 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +000068 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattnerfa761832004-01-14 03:38:37 +000069}
Misha Brukman9d679cb2003-07-30 20:15:44 +000070
Chris Lattner4a106452002-12-23 23:50:16 +000071//===----------------------------------------------------------------------===//
72// BugDriver method implementation
73//
74
75/// initializeExecutionEnvironment - This method is used to set up the
76/// environment for executing LLVM programs.
77///
78bool BugDriver::initializeExecutionEnvironment() {
79 std::cout << "Initializing execution environment: ";
80
Misha Brukman41485562003-09-29 22:40:52 +000081 // Create an instance of the AbstractInterpreter interface as specified on
82 // the command line
Chris Lattner7bb11542004-02-18 20:52:02 +000083 cbe = 0;
Chris Lattner4a106452002-12-23 23:50:16 +000084 std::string Message;
Brian Gaeke636df3d2004-05-04 21:09:16 +000085
Chris Lattnercc876a72003-05-03 03:19:41 +000086 switch (InterpreterSel) {
Brian Gaekeb5ee5092003-10-21 17:41:35 +000087 case AutoPick:
88 InterpreterSel = RunCBE;
Brian Gaeke636df3d2004-05-04 21:09:16 +000089 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
90 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +000091 if (!Interpreter) {
92 InterpreterSel = RunJIT;
Brian Gaeke636df3d2004-05-04 21:09:16 +000093 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
94 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +000095 }
96 if (!Interpreter) {
97 InterpreterSel = RunLLC;
Brian Gaeke636df3d2004-05-04 21:09:16 +000098 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
99 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000100 }
101 if (!Interpreter) {
102 InterpreterSel = RunLLI;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000103 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
104 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000105 }
106 if (!Interpreter) {
107 InterpreterSel = AutoPick;
108 Message = "Sorry, I can't automatically select an interpreter!\n";
109 }
110 break;
Chris Lattner769f1fe2003-10-14 21:59:36 +0000111 case RunLLI:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000112 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
113 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000114 break;
115 case RunLLC:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000116 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
117 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000118 break;
119 case RunJIT:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000120 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
121 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000122 break;
123 case RunCBE:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000124 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
125 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000126 break;
Chris Lattnercc876a72003-05-03 03:19:41 +0000127 default:
Misha Brukman41485562003-09-29 22:40:52 +0000128 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
Chris Lattnercc876a72003-05-03 03:19:41 +0000129 break;
Chris Lattner4a106452002-12-23 23:50:16 +0000130 }
Misha Brukman41485562003-09-29 22:40:52 +0000131 std::cerr << Message;
Chris Lattner4a106452002-12-23 23:50:16 +0000132
Misha Brukmana259c9b2003-07-24 21:59:10 +0000133 // Initialize auxiliary tools for debugging
Chris Lattner7bb11542004-02-18 20:52:02 +0000134 if (!cbe) {
Brian Gaeke636df3d2004-05-04 21:09:16 +0000135 cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
Chris Lattner7bb11542004-02-18 20:52:02 +0000136 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
137 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000138 gcc = GCC::create(getToolName(), Message);
Misha Brukmana259c9b2003-07-24 21:59:10 +0000139 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
140
Chris Lattner4a106452002-12-23 23:50:16 +0000141 // If there was an error creating the selected interpreter, quit with error.
142 return Interpreter == 0;
143}
144
Chris Lattnerea9212c2004-02-18 23:25:22 +0000145/// 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.
148///
149void 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";
155 exit(1);
156 }
157
158 // Remove the temporary bytecode file when we are done.
159 FileRemover BytecodeFileRemover(BytecodeFile);
160
161 // Actually compile the program!
162 Interpreter->compileProgram(BytecodeFile);
163}
164
Chris Lattner4a106452002-12-23 23:50:16 +0000165
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.
169///
170std::string BugDriver::executeProgram(std::string OutputFile,
Misha Brukman50733362003-07-24 18:17:43 +0000171 std::string BytecodeFile,
Chris Lattner769f1fe2003-10-14 21:59:36 +0000172 const std::string &SharedObj,
Brian Gaekec5cad212004-02-11 18:37:32 +0000173 AbstractInterpreter *AI,
174 bool *ProgramExitedNonzero) {
Chris Lattner769f1fe2003-10-14 21:59:36 +0000175 if (AI == 0) AI = Interpreter;
176 assert(AI && "Interpreter should have been created already!");
Chris Lattner4a106452002-12-23 23:50:16 +0000177 bool CreatedBytecode = false;
178 if (BytecodeFile.empty()) {
179 // Emit the program to a bytecode file...
180 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
181
182 if (writeProgramToFile(BytecodeFile, Program)) {
183 std::cerr << ToolName << ": Error emitting bytecode to file '"
Misha Brukman50733362003-07-24 18:17:43 +0000184 << BytecodeFile << "'!\n";
Chris Lattner4a106452002-12-23 23:50:16 +0000185 exit(1);
186 }
187 CreatedBytecode = true;
188 }
189
Chris Lattner97092722004-02-18 22:01:21 +0000190 // Remove the temporary bytecode file when we are done.
191 FileRemover BytecodeFileRemover(BytecodeFile, CreatedBytecode);
192
Chris Lattner4a106452002-12-23 23:50:16 +0000193 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
Misha Brukman50733362003-07-24 18:17:43 +0000194
Chris Lattner4a106452002-12-23 23:50:16 +0000195 // Check to see if this is a valid output filename...
196 OutputFile = getUniqueFilename(OutputFile);
197
Chris Lattner769f1fe2003-10-14 21:59:36 +0000198 // Figure out which shared objects to run, if any.
Chris Lattner7dac6582003-10-14 22:24:31 +0000199 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000200 if (!SharedObj.empty())
201 SharedObjs.push_back(SharedObj);
202
Chris Lattner4a106452002-12-23 23:50:16 +0000203 // Actually execute the program!
Chris Lattner769f1fe2003-10-14 21:59:36 +0000204 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
205 OutputFile, SharedObjs);
206
Brian Gaekec5cad212004-02-11 18:37:32 +0000207 if (ProgramExitedNonzero != 0)
208 *ProgramExitedNonzero = (RetVal != 0);
Chris Lattner4a106452002-12-23 23:50:16 +0000209
Chris Lattner4a106452002-12-23 23:50:16 +0000210 // Return the filename we captured the output to.
211 return OutputFile;
212}
213
Brian Gaekec5cad212004-02-11 18:37:32 +0000214/// executeProgramWithCBE - Used to create reference output with the C
215/// backend, if reference output is not provided.
216///
217std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
218 bool ProgramExitedNonzero;
219 std::string outFN = executeProgram(OutputFile, "", "",
220 (AbstractInterpreter*)cbe,
221 &ProgramExitedNonzero);
222 if (ProgramExitedNonzero) {
223 std::cerr
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;
227 }
228 return outFN;
229}
Misha Brukman50733362003-07-24 18:17:43 +0000230
Chris Lattnera0f5b152003-10-14 21:09:11 +0000231std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
Misha Brukman50733362003-07-24 18:17:43 +0000232 assert(Interpreter && "Interpreter should have been created already!");
Chris Lattnera0f5b152003-10-14 21:09:11 +0000233 std::string OutputCFile;
Misha Brukman50733362003-07-24 18:17:43 +0000234
235 // Using CBE
Misha Brukman50733362003-07-24 18:17:43 +0000236 cbe->OutputC(BytecodeFile, OutputCFile);
237
238#if 0 /* This is an alternative, as yet unimplemented */
239 // Using LLC
Chris Lattnera0f5b152003-10-14 21:09:11 +0000240 std::string Message;
Misha Brukman41485562003-09-29 22:40:52 +0000241 LLC *llc = createLLCtool(Message);
Misha Brukman50733362003-07-24 18:17:43 +0000242 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
243 std::cerr << "Could not generate asm code with `llc', exiting.\n";
244 exit(1);
245 }
246#endif
247
Chris Lattnera0f5b152003-10-14 21:09:11 +0000248 std::string SharedObjectFile;
Chris Lattner769f1fe2003-10-14 21:59:36 +0000249 if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
Chris Lattnera0f5b152003-10-14 21:09:11 +0000250 exit(1);
Misha Brukman50733362003-07-24 18:17:43 +0000251
252 // Remove the intermediate C file
253 removeFile(OutputCFile);
254
Chris Lattner6ebe44d2003-10-19 21:54:13 +0000255 return "./" + SharedObjectFile;
Misha Brukman50733362003-07-24 18:17:43 +0000256}
257
258
Chris Lattner4a106452002-12-23 23:50:16 +0000259/// 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.
262///
Misha Brukman50733362003-07-24 18:17:43 +0000263bool BugDriver::diffProgram(const std::string &BytecodeFile,
264 const std::string &SharedObject,
Chris Lattner640f22e2003-04-24 17:02:17 +0000265 bool RemoveBytecode) {
Brian Gaekec5cad212004-02-11 18:37:32 +0000266 bool ProgramExitedNonzero;
267
Chris Lattner4a106452002-12-23 23:50:16 +0000268 // Execute the program, generating an output file...
Brian Gaekec5cad212004-02-11 18:37:32 +0000269 std::string Output = executeProgram("", BytecodeFile, SharedObject, 0,
270 &ProgramExitedNonzero);
271
272 // If we're checking the program exit code, assume anything nonzero is bad.
Chris Lattner58d84ce2004-04-02 05:33:06 +0000273 if (CheckProgramExitCode && ProgramExitedNonzero) {
274 removeFile(Output);
275 if (RemoveBytecode) removeFile(BytecodeFile);
Brian Gaekec5cad212004-02-11 18:37:32 +0000276 return true;
Chris Lattner58d84ce2004-04-02 05:33:06 +0000277 }
Chris Lattner4a106452002-12-23 23:50:16 +0000278
Chris Lattner65f62792003-08-01 20:29:45 +0000279 std::string Error;
Chris Lattner4a106452002-12-23 23:50:16 +0000280 bool FilesDifferent = false;
Chris Lattner65f62792003-08-01 20:29:45 +0000281 if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
282 if (!Error.empty()) {
283 std::cerr << "While diffing output: " << Error << "\n";
284 exit(1);
285 }
286 FilesDifferent = true;
287 }
Chris Lattner1a28a2b2003-10-18 21:02:51 +0000288
289 // Remove the generated output.
290 removeFile(Output);
Chris Lattner4a106452002-12-23 23:50:16 +0000291
Chris Lattner1a28a2b2003-10-18 21:02:51 +0000292 // Remove the bytecode file if we are supposed to.
Chris Lattner640f22e2003-04-24 17:02:17 +0000293 if (RemoveBytecode) removeFile(BytecodeFile);
Chris Lattner4a106452002-12-23 23:50:16 +0000294 return FilesDifferent;
295}
Misha Brukman91eabc12003-07-28 19:16:14 +0000296
297bool BugDriver::isExecutingJIT() {
298 return InterpreterSel == RunJIT;
299}
Brian Gaeked0fde302003-11-11 22:41:34 +0000300