#include <algorithm>
#include <set>
-// Passed as a command-line argument to Bugpoint
-extern cl::opt<std::string> Output;
-
class ReduceMisCodegenFunctions : public ListReducer<Function*> {
BugDriver &BD;
public:
for (Module::giterator I=TestModule->gbegin(),E = TestModule->gend();I!=E;++I)
I->setInitializer(0); // Delete the initializer to make it external
- // Remove the Test functions from the Safe module, and
- // all of the global variables.
+ // Remove the Test functions from the Safe module
for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
Function *TNOF = SafeModule->getFunction(Funcs[i]->getName(),
Funcs[i]->getFunctionType());
// Run the code generator on the `Test' code, loading the shared library.
// The function returns whether or not the new output differs from reference.
- return BD.diffProgram(TestModuleBC, SharedObject, false);
+ int Result = BD.diffProgram(TestModuleBC, SharedObject, false);
+ removeFile(SharedObject);
+ return Result;
}
namespace {
- struct Disambiguator /*: public unary_function<GlobalValue&, void>*/ {
+ struct Disambiguator {
std::set<std::string> SymbolNames;
- std::set<Value*> Symbols;
uint64_t uniqueCounter;
bool externalOnly;
-
+ public:
Disambiguator() : uniqueCounter(0), externalOnly(true) {}
void setExternalOnly(bool value) { externalOnly = value; }
- void operator() (GlobalValue &V) {
+ void add(GlobalValue &V) {
if (externalOnly && !V.isExternal()) return;
if (SymbolNames.count(V.getName()) == 0) {
DEBUG(std::cerr << "Disambiguator: adding " << V.getName()
<< ", no conflicts.\n");
- Symbols.insert(&V);
SymbolNames.insert(V.getName());
} else {
// Mangle name before adding
<< ", adding: " << newName << "\n");
V.setName(newName);
SymbolNames.insert(newName);
- Symbols.insert(&V);
}
}
};
void ReduceMisCodegenFunctions::DisambiguateGlobalSymbols(Module *M) {
// First, try not to cause collisions by minimizing chances of renaming an
// already-external symbol, so take in external globals and functions as-is.
- Disambiguator D = std::for_each(M->gbegin(), M->gend(), Disambiguator());
- std::for_each(M->begin(), M->end(), D);
+ Disambiguator D;
+ for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) D.add(*I);
+ for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) D.add(*I);
// Now just rename functions and globals as necessary, keeping what's already
// in the set unique.
D.setExternalOnly(false);
- std::for_each(M->gbegin(), M->gend(), D);
- std::for_each(M->begin(), M->end(), D);
+ for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) D.add(*I);
+ for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) D.add(*I);
}
std::cout << "\n";
// Output a bunch of bytecode files for the user...
- ReduceMisCodegenFunctions(*this).TestFuncs(MisCodegenFunctions);
+ // ReduceMisCodegenFunctions(*this).TestFuncs(MisCodegenFunctions);
return false;
}
cl::opt<std::string>
InputFile("input", cl::init("/dev/null"),
cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
+
+ enum FileType { AsmFile, CFile };
}
/// AbstractInterpreter Class - Subclasses of this class are used to execute
int LLI::ExecuteProgram(const std::string &Bytecode,
const std::string &OutputFile,
const std::string &SharedLib) {
- if (SharedLib != "") {
+ if (!SharedLib.empty()) {
std::cerr << "LLI currently does not support loading shared libraries.\n"
<< "Exiting.\n";
exit(1);
// This is not a *real* AbstractInterpreter as it does not accept bytecode
// files, but only input acceptable to GCC, i.e. C, C++, and assembly files
//
-class GCC : public AbstractInterpreter {
+class GCC {
std::string GCCPath; // The path to the gcc executable
public:
GCC(const std::string &gccPath) : GCCPath(gccPath) { }
+ virtual ~GCC() {}
// GCC create method - Try to find the `gcc' executable
static GCC *create(BugDriver *BD, std::string &Message) {
}
virtual int ExecuteProgram(const std::string &ProgramFile,
+ FileType fileType,
const std::string &OutputFile,
const std::string &SharedLib = "");
int MakeSharedObject(const std::string &InputFile,
+ FileType fileType,
std::string &OutputFile);
void ProcessFailure(const char **Args);
};
int GCC::ExecuteProgram(const std::string &ProgramFile,
+ FileType fileType,
const std::string &OutputFile,
const std::string &SharedLib) {
std::string OutputBinary = "bugpoint.gcc.exe";
const char *ArgsWithoutSO[] = {
GCCPath.c_str(),
+ "-x", (fileType == AsmFile) ? "assembler" : "c",
ProgramFile.c_str(), // Specify the input filename...
"-o", OutputBinary.c_str(), // Output to the right filename...
"-lm", // Hard-code the math library...
};
const char *ArgsWithSO[] = {
GCCPath.c_str(),
+ "-x", (fileType == AsmFile) ? "assembler" : "c",
ProgramFile.c_str(), // Specify the input filename...
SharedLib.c_str(), // Specify the shared library to link in...
"-o", OutputBinary.c_str(), // Output to the right filename...
0
};
- GCCArgs = (SharedLib == "") ? ArgsWithoutSO : ArgsWithSO;
+ GCCArgs = (SharedLib.empty()) ? ArgsWithoutSO : ArgsWithSO;
std::cout << "<gcc>";
- if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null",
- "/dev/null", "/dev/null")) {
+ if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
+ "/dev/null")) {
ProcessFailure(GCCArgs);
- exit(1); // Leave stuff around for the user to inspect or debug the CBE
+ exit(1);
}
const char *ProgramArgs[] = {
}
int GCC::MakeSharedObject(const std::string &InputFile,
+ FileType fileType,
std::string &OutputFile) {
OutputFile = "./bugpoint.so";
// Compile the C/asm file into a shared object
const char* GCCArgs[] = {
GCCPath.c_str(),
+ "-x", (fileType == AsmFile) ? "assembler" : "c",
InputFile.c_str(), // Specify the input filename...
#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
"-G", // Compile a shared library, `-G' for Sparc
Message = "Found llc: " + LLCPath + "\n";
GCC *gcc = GCC::create(BD, Message);
+ if (!gcc) {
+ std::cerr << Message << "\n";
+ exit(1);
+ }
return new LLC(LLCPath, gcc);
}
}
// Assuming LLC worked, compile the result with GCC and run it.
- int Result = gcc->ExecuteProgram(OutputAsmFile, OutputFile, SharedLib);
+ int Result = gcc->ExecuteProgram(OutputAsmFile,AsmFile,OutputFile,SharedLib);
removeFile(OutputAsmFile);
return Result;
}
int JIT::ExecuteProgram(const std::string &Bytecode,
const std::string &OutputFile,
const std::string &SharedLib) {
- if (SharedLib == "") {
+ if (SharedLib.empty()) {
const char* Args[] = {
- LLIPath.c_str(), "-quiet", "-force-interpreter=false", Bytecode.c_str(),
+ LLIPath.c_str(),
+ "-quiet",
+ "-force-interpreter=false",
+ Bytecode.c_str(),
0
};
return RunProgramWithTimeout(LLIPath, Args,
InputFile, OutputFile, OutputFile);
} else {
- std::string SharedLibOpt = "-load=" + SharedLib;
const char* Args[] = {
LLIPath.c_str(), "-quiet", "-force-interpreter=false",
- SharedLibOpt.c_str(),
+ "-load", SharedLib.c_str(),
Bytecode.c_str(),
0
};
Message = "Found dis: " + DISPath + "\n";
GCC *gcc = GCC::create(BD, Message);
+ if (!gcc) {
+ std::cerr << Message << "\n";
+ exit(1);
+ }
return new CBE(DISPath, gcc);
}
exit(1);
}
- int Result = gcc->ExecuteProgram(OutputCFile, OutputFile, SharedLib);
+ int Result = gcc->ExecuteProgram(OutputCFile, CFile, OutputFile, SharedLib);
removeFile(OutputCFile);
return Result;
std::cout << Message;
+ // Initialize auxiliary tools for debugging
+ cbe = CBE::create(this, Message);
+ if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
+ gcc = GCC::create(this, Message);
+ if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
+
// If there was an error creating the selected interpreter, quit with error.
return Interpreter == 0;
}
std::string BugDriver::executeProgramWithCBE(std::string OutputFile,
std::string BytecodeFile,
std::string SharedObject) {
- std::string Output;
- CBE *cbe = CBE::create(this, Output);
- Output = executeProgram(OutputFile, BytecodeFile, SharedObject, cbe);
- delete cbe;
- return Output;
+ return executeProgram(OutputFile, BytecodeFile, SharedObject, cbe);
}
int BugDriver::compileSharedObject(const std::string &BytecodeFile,
std::string Message, OutputCFile;
// Using CBE
- CBE *cbe = CBE::create(this, Message);
cbe->OutputC(BytecodeFile, OutputCFile);
#if 0 /* This is an alternative, as yet unimplemented */
}
#endif
- GCC *gcc = GCC::create(this, Message);
- gcc->MakeSharedObject(OutputCFile, SharedObject);
+ gcc->MakeSharedObject(OutputCFile, CFile, SharedObject);
// Remove the intermediate C file
removeFile(OutputCFile);
- // We are done with the CBE & GCC
- delete cbe;
- delete gcc;
-
return 0;
}