2003-09-10 21:42:51 +02:00
|
|
|
//===- extract.cpp - LLVM function extraction utility ---------------------===//
|
2003-10-20 19:47:21 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-05-22 22:27:00 +02:00
|
|
|
//
|
|
|
|
// This utility changes the input module to only contain a single function,
|
|
|
|
// which is primarily used for debugging transformations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/PassManager.h"
|
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.h"
|
2002-07-24 00:04:40 +02:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2003-10-28 23:22:16 +01:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2004-05-27 07:41:36 +02:00
|
|
|
#include "llvm/System/Signals.h"
|
2002-05-22 22:27:00 +02:00
|
|
|
#include <memory>
|
2004-02-18 17:53:59 +01:00
|
|
|
#include <fstream>
|
2003-11-11 23:41:34 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2002-07-22 04:10:13 +02:00
|
|
|
// InputFilename - The filename to read from.
|
2002-07-25 18:31:09 +02:00
|
|
|
static cl::opt<std::string>
|
2002-07-22 04:10:13 +02:00
|
|
|
InputFilename(cl::Positional, cl::desc("<input bytecode file>"),
|
|
|
|
cl::init("-"), cl::value_desc("filename"));
|
|
|
|
|
2004-02-18 17:53:59 +01:00
|
|
|
static cl::opt<std::string>
|
|
|
|
OutputFilename("o", cl::desc("Specify output filename"),
|
|
|
|
cl::value_desc("filename"), cl::init("-"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
Force("f", cl::desc("Overwrite output files"));
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2004-04-23 01:07:39 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
DeleteFn("delete", cl::desc("Delete specified function from Module"));
|
|
|
|
|
2002-07-22 04:10:13 +02:00
|
|
|
// ExtractFunc - The function to extract from the module... defaults to main.
|
2002-07-25 18:31:09 +02:00
|
|
|
static cl::opt<std::string>
|
2002-07-22 04:10:13 +02:00
|
|
|
ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
|
|
|
|
cl::value_desc("function"));
|
|
|
|
|
2002-05-22 22:27:00 +02:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
|
2004-08-29 21:28:55 +02:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
2002-05-22 22:27:00 +02:00
|
|
|
|
|
|
|
std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
|
|
|
|
if (M.get() == 0) {
|
2002-07-30 23:43:22 +02:00
|
|
|
std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
|
2002-05-22 22:27:00 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-11-19 19:42:59 +01:00
|
|
|
// Figure out which function we should extract
|
|
|
|
Function *F = M.get()->getNamedFunction(ExtractFunc);
|
|
|
|
if (F == 0) {
|
|
|
|
std::cerr << argv[0] << ": program doesn't contain function named '"
|
|
|
|
<< ExtractFunc << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-02-18 17:53:59 +01:00
|
|
|
// In addition to deleting all other functions, we also want to spiff it up a
|
|
|
|
// little bit. Do this now.
|
2002-05-22 22:27:00 +02:00
|
|
|
//
|
|
|
|
PassManager Passes;
|
2003-10-28 23:22:16 +01:00
|
|
|
Passes.add(new TargetData("extract", M.get())); // Use correct TargetData
|
2004-04-23 01:07:39 +02:00
|
|
|
// Either isolate the function or delete it from the Module
|
|
|
|
Passes.add(createFunctionExtractionPass(F, DeleteFn));
|
2002-05-22 22:27:00 +02:00
|
|
|
Passes.add(createGlobalDCEPass()); // Delete unreachable globals
|
2002-10-12 22:50:16 +02:00
|
|
|
Passes.add(createFunctionResolvingPass()); // Delete prototypes
|
2002-07-24 00:04:40 +02:00
|
|
|
Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
|
2002-05-22 22:27:00 +02:00
|
|
|
|
2004-02-18 17:53:59 +01:00
|
|
|
std::ostream *Out = 0;
|
|
|
|
|
|
|
|
if (OutputFilename != "-") { // Not stdout?
|
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
|
|
|
std::cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
|
|
|
} else { // Specified stdout
|
|
|
|
Out = &std::cout;
|
|
|
|
}
|
|
|
|
|
|
|
|
Passes.add(new WriteBytecodePass(Out)); // Write bytecode to file...
|
2002-06-25 18:13:24 +02:00
|
|
|
Passes.run(*M.get());
|
2004-02-18 17:53:59 +01:00
|
|
|
|
|
|
|
if (Out != &std::cout)
|
|
|
|
delete Out;
|
2002-05-22 22:27:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|