2012-01-09 06:09:58 +08:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2013-06-27 02:20:32 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2012-01-09 06:09:58 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2014-05-01 02:35:20 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-01-09 06:09:58 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-01-21 04:28:48 +08:00
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2012-01-09 06:09:58 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2014-08-23 06:24:28 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2012-01-09 06:09:58 +08:00
|
|
|
|
2014-06-21 17:20:31 +08:00
|
|
|
#include <system_error>
|
|
|
|
|
2015-06-25 01:03:50 +08:00
|
|
|
#define LLVM_360 \
|
|
|
|
(LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR == 6)
|
|
|
|
|
|
|
|
|
2014-06-21 17:20:31 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2012-01-09 06:09:58 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
OutputFilename("o", cl::desc("Output filename"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
LLVMContext &Context = getGlobalContext();
|
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "libclc builtin preparation tool\n");
|
|
|
|
|
|
|
|
std::string ErrorMessage;
|
2015-06-25 01:03:50 +08:00
|
|
|
Module *M;
|
2012-01-09 06:09:58 +08:00
|
|
|
|
|
|
|
{
|
2014-07-08 01:46:45 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
|
|
|
|
MemoryBuffer::getFile(InputFilename);
|
|
|
|
std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get();
|
|
|
|
if (std::error_code ec = BufferOrErr.getError())
|
2012-01-09 06:09:58 +08:00
|
|
|
ErrorMessage = ec.message();
|
2014-01-21 04:28:48 +08:00
|
|
|
else {
|
2015-06-25 01:03:50 +08:00
|
|
|
#if LLVM_360
|
|
|
|
ErrorOr<Module *>
|
|
|
|
#else
|
|
|
|
ErrorOr<std::unique_ptr<Module>>
|
|
|
|
#endif
|
|
|
|
ModuleOrErr =
|
|
|
|
parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context);
|
2014-12-31 23:27:53 +08:00
|
|
|
if (std::error_code ec = ModuleOrErr.getError())
|
2014-01-21 04:28:48 +08:00
|
|
|
ErrorMessage = ec.message();
|
2015-06-25 01:03:50 +08:00
|
|
|
#if LLVM_360
|
|
|
|
M = ModuleOrErr.get().get();
|
|
|
|
#else
|
|
|
|
M = ModuleOrErr.get().release();
|
|
|
|
#endif
|
2014-01-21 04:28:48 +08:00
|
|
|
}
|
2012-01-09 06:09:58 +08:00
|
|
|
}
|
|
|
|
|
2015-06-25 01:03:50 +08:00
|
|
|
if (!M) {
|
2012-01-09 06:09:58 +08:00
|
|
|
errs() << argv[0] << ": ";
|
|
|
|
if (ErrorMessage.size())
|
|
|
|
errs() << ErrorMessage << "\n";
|
|
|
|
else
|
|
|
|
errs() << "bitcode didn't read correctly.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set linkage of every external definition to linkonce_odr.
|
|
|
|
for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {
|
|
|
|
if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
|
|
|
|
i->setLinkage(GlobalValue::LinkOnceODRLinkage);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Module::global_iterator i = M->global_begin(), e = M->global_end();
|
|
|
|
i != e; ++i) {
|
|
|
|
if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
|
|
|
|
i->setLinkage(GlobalValue::LinkOnceODRLinkage);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OutputFilename.empty()) {
|
|
|
|
errs() << "no output file\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-28 14:19:33 +08:00
|
|
|
std::error_code EC;
|
2014-12-31 23:27:53 +08:00
|
|
|
std::unique_ptr<tool_output_file> Out
|
2014-08-28 14:19:33 +08:00
|
|
|
(new tool_output_file(OutputFilename, EC, sys::fs::F_None));
|
|
|
|
if (EC) {
|
|
|
|
errs() << EC.message() << '\n';
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-01-09 06:09:58 +08:00
|
|
|
|
2015-06-25 01:03:50 +08:00
|
|
|
WriteBitcodeToFile(M, Out->os());
|
2012-01-09 06:09:58 +08:00
|
|
|
|
|
|
|
// Declare success.
|
|
|
|
Out->keep();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|