blob: 749dac5fe2397cc335febf16ca111bbb2fbd88fa [file] [log] [blame]
Chris Lattnerde4aa4c2002-12-23 23:50:16 +00001//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
2//
3// This file contains code used to execute the program utilizing one of the
4// various ways of running LLVM bytecode.
5//
6//===----------------------------------------------------------------------===//
7
8/*
9BUGPOINT NOTES:
10
111. Bugpoint should not leave any files behind if the program works properly
122. There should be an option to specify the program name, which specifies a
13 unique string to put into output files. This allows operation in the
Misha Brukmand792c9b2003-07-24 18:17:43 +000014 SingleSource directory, e.g. default to the first input filename.
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000015*/
16
17#include "BugDriver.h"
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000018#include "Support/CommandLine.h"
Chris Lattnerf0c69642003-08-01 22:13:59 +000019#include "Support/Debug.h"
Chris Lattneraa997fb2003-08-01 20:29:45 +000020#include "Support/FileUtilities.h"
Misha Brukman0c2305b2003-08-07 21:19:30 +000021#include "Support/SystemUtils.h"
Chris Lattner10bd1c22003-10-07 13:45:51 +000022#include "llvm/Support/ToolRunner.h"
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000023#include <fstream>
Chris Lattner7c0f8622002-12-24 00:44:34 +000024#include <iostream>
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000025
26namespace {
27 // OutputType - Allow the user to specify the way code should be run, to test
28 // for miscompilation.
29 //
30 enum OutputType {
31 RunLLI, RunJIT, RunLLC, RunCBE
32 };
Misha Brukman5bc6a8f2003-09-29 22:40:52 +000033
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000034 cl::opt<OutputType>
35 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
Misha Brukmand792c9b2003-07-24 18:17:43 +000036 cl::values(clEnumValN(RunLLI, "run-lli", "Execute with LLI"),
37 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
38 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
39 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
40 0));
Chris Lattner68efaa72003-04-23 20:31:37 +000041
42 cl::opt<std::string>
43 InputFile("input", cl::init("/dev/null"),
44 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000045}
46
Misha Brukman40feb362003-07-30 20:15:44 +000047// Anything specified after the --args option are taken as arguments to the
48// program being debugged.
49cl::list<std::string>
50InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
51 cl::ZeroOrMore);
52
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000053//===----------------------------------------------------------------------===//
54// BugDriver method implementation
55//
56
57/// initializeExecutionEnvironment - This method is used to set up the
58/// environment for executing LLVM programs.
59///
60bool BugDriver::initializeExecutionEnvironment() {
61 std::cout << "Initializing execution environment: ";
62
63 // FIXME: This should default to searching for the best interpreter to use on
64 // this platform, which would be JIT, then LLC, then CBE, then LLI.
65
Misha Brukman5bc6a8f2003-09-29 22:40:52 +000066 // Create an instance of the AbstractInterpreter interface as specified on
67 // the command line
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000068 std::string Message;
Chris Lattner7709ec52003-05-03 03:19:41 +000069 switch (InterpreterSel) {
Chris Lattner3f6e5222003-10-14 21:59:36 +000070 case RunLLI:
71 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message);
72 break;
73 case RunLLC:
74 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message);
75 break;
76 case RunJIT:
77 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message);
78 break;
79 case RunCBE:
80 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message);
81 break;
Chris Lattner7709ec52003-05-03 03:19:41 +000082 default:
Misha Brukman5bc6a8f2003-09-29 22:40:52 +000083 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
Chris Lattner7709ec52003-05-03 03:19:41 +000084 break;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000085 }
Misha Brukman5bc6a8f2003-09-29 22:40:52 +000086 std::cerr << Message;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000087
Misha Brukman0fd31722003-07-24 21:59:10 +000088 // Initialize auxiliary tools for debugging
Chris Lattner3f6e5222003-10-14 21:59:36 +000089 cbe = AbstractInterpreter::createCBE(getToolName(), Message);
Misha Brukman0fd31722003-07-24 21:59:10 +000090 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
Chris Lattner3f6e5222003-10-14 21:59:36 +000091 gcc = GCC::create(getToolName(), Message);
Misha Brukman0fd31722003-07-24 21:59:10 +000092 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
93
Chris Lattnerde4aa4c2002-12-23 23:50:16 +000094 // If there was an error creating the selected interpreter, quit with error.
95 return Interpreter == 0;
96}
97
98
99/// executeProgram - This method runs "Program", capturing the output of the
100/// program to a file, returning the filename of the file. A recommended
101/// filename may be optionally specified.
102///
103std::string BugDriver::executeProgram(std::string OutputFile,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000104 std::string BytecodeFile,
Chris Lattner3f6e5222003-10-14 21:59:36 +0000105 const std::string &SharedObj,
Misha Brukmand792c9b2003-07-24 18:17:43 +0000106 AbstractInterpreter *AI) {
Chris Lattner3f6e5222003-10-14 21:59:36 +0000107 if (AI == 0) AI = Interpreter;
108 assert(AI && "Interpreter should have been created already!");
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000109 bool CreatedBytecode = false;
110 if (BytecodeFile.empty()) {
111 // Emit the program to a bytecode file...
112 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
113
114 if (writeProgramToFile(BytecodeFile, Program)) {
115 std::cerr << ToolName << ": Error emitting bytecode to file '"
Misha Brukmand792c9b2003-07-24 18:17:43 +0000116 << BytecodeFile << "'!\n";
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000117 exit(1);
118 }
119 CreatedBytecode = true;
120 }
121
122 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
Misha Brukmand792c9b2003-07-24 18:17:43 +0000123
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000124 // Check to see if this is a valid output filename...
125 OutputFile = getUniqueFilename(OutputFile);
126
Chris Lattner3f6e5222003-10-14 21:59:36 +0000127 // Figure out which shared objects to run, if any.
128 std::vector<std::string> SharedObjs;
129 if (!SharedObj.empty())
130 SharedObjs.push_back(SharedObj);
131
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000132 // Actually execute the program!
Chris Lattner3f6e5222003-10-14 21:59:36 +0000133 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
134 OutputFile, SharedObjs);
135
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000136
137 // Remove the temporary bytecode file.
Misha Brukmand792c9b2003-07-24 18:17:43 +0000138 if (CreatedBytecode) removeFile(BytecodeFile);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000139
140 // Return the filename we captured the output to.
141 return OutputFile;
142}
143
Misha Brukmand792c9b2003-07-24 18:17:43 +0000144
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000145std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
Misha Brukmand792c9b2003-07-24 18:17:43 +0000146 assert(Interpreter && "Interpreter should have been created already!");
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000147 std::string OutputCFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000148
149 // Using CBE
Misha Brukmand792c9b2003-07-24 18:17:43 +0000150 cbe->OutputC(BytecodeFile, OutputCFile);
151
152#if 0 /* This is an alternative, as yet unimplemented */
153 // Using LLC
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000154 std::string Message;
Misha Brukman5bc6a8f2003-09-29 22:40:52 +0000155 LLC *llc = createLLCtool(Message);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000156 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
157 std::cerr << "Could not generate asm code with `llc', exiting.\n";
158 exit(1);
159 }
160#endif
161
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000162 std::string SharedObjectFile;
Chris Lattner3f6e5222003-10-14 21:59:36 +0000163 if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000164 exit(1);
Misha Brukmand792c9b2003-07-24 18:17:43 +0000165
166 // Remove the intermediate C file
167 removeFile(OutputCFile);
168
Chris Lattnerf8a84db2003-10-14 21:09:11 +0000169 return SharedObjectFile;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000170}
171
172
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000173/// diffProgram - This method executes the specified module and diffs the output
174/// against the file specified by ReferenceOutputFile. If the output is
175/// different, true is returned.
176///
Misha Brukmand792c9b2003-07-24 18:17:43 +0000177bool BugDriver::diffProgram(const std::string &BytecodeFile,
178 const std::string &SharedObject,
Chris Lattner16a41312003-04-24 17:02:17 +0000179 bool RemoveBytecode) {
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000180 // Execute the program, generating an output file...
Misha Brukmand792c9b2003-07-24 18:17:43 +0000181 std::string Output = executeProgram("", BytecodeFile, SharedObject);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000182
Chris Lattneraa997fb2003-08-01 20:29:45 +0000183 std::string Error;
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000184 bool FilesDifferent = false;
Chris Lattneraa997fb2003-08-01 20:29:45 +0000185 if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
186 if (!Error.empty()) {
187 std::cerr << "While diffing output: " << Error << "\n";
188 exit(1);
189 }
190 FilesDifferent = true;
191 }
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000192
Chris Lattner16a41312003-04-24 17:02:17 +0000193 if (RemoveBytecode) removeFile(BytecodeFile);
Chris Lattnerde4aa4c2002-12-23 23:50:16 +0000194 return FilesDifferent;
195}
Misha Brukman539f9592003-07-28 19:16:14 +0000196
197bool BugDriver::isExecutingJIT() {
198 return InterpreterSel == RunJIT;
199}