Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 1 | #include "llvm/Bitcode/ReaderWriter.h" |
Tom Stellard | 976577e | 2013-06-26 18:20:32 +0000 | [diff] [blame] | 2 | #include "llvm/IR/Function.h" |
| 3 | #include "llvm/IR/GlobalVariable.h" |
| 4 | #include "llvm/IR/LLVMContext.h" |
| 5 | #include "llvm/IR/Module.h" |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 6 | #include "llvm/Support/CommandLine.h" |
| 7 | #include "llvm/Support/ManagedStatic.h" |
| 8 | #include "llvm/Support/MemoryBuffer.h" |
Tom Stellard | 98dccb1 | 2014-04-30 18:35:20 +0000 | [diff] [blame] | 9 | #include "llvm/Support/FileSystem.h" |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 10 | #include "llvm/Support/raw_ostream.h" |
Tom Stellard | 8a63b15 | 2014-01-20 20:28:48 +0000 | [diff] [blame] | 11 | #include "llvm/Support/ErrorOr.h" |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 12 | #include "llvm/Support/ToolOutputFile.h" |
Niels Ole Salscheider | 109ce69 | 2014-08-22 22:24:28 +0000 | [diff] [blame^] | 13 | #include "llvm/Config/llvm-config.h" |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 14 | |
Tom Stellard | 0ab1034 | 2014-06-13 01:30:14 +0000 | [diff] [blame] | 15 | #define LLVM_350_AND_NEWER \ |
| 16 | (LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5)) |
| 17 | |
| 18 | #if LLVM_350_AND_NEWER |
Jeroen Ketema | d253e66 | 2014-06-21 09:20:31 +0000 | [diff] [blame] | 19 | #include <system_error> |
| 20 | |
Tom Stellard | 0ab1034 | 2014-06-13 01:30:14 +0000 | [diff] [blame] | 21 | #define ERROR_CODE std::error_code |
Jeroen Ketema | d253e66 | 2014-06-21 09:20:31 +0000 | [diff] [blame] | 22 | #define UNIQUE_PTR std::unique_ptr |
Tom Stellard | 0ab1034 | 2014-06-13 01:30:14 +0000 | [diff] [blame] | 23 | #else |
Jeroen Ketema | d253e66 | 2014-06-21 09:20:31 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/OwningPtr.h" |
| 25 | #include "llvm/Support/system_error.h" |
| 26 | |
Tom Stellard | 0ab1034 | 2014-06-13 01:30:14 +0000 | [diff] [blame] | 27 | #define ERROR_CODE error_code |
Jeroen Ketema | d253e66 | 2014-06-21 09:20:31 +0000 | [diff] [blame] | 28 | #define UNIQUE_PTR OwningPtr |
Tom Stellard | 0ab1034 | 2014-06-13 01:30:14 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
Jeroen Ketema | d253e66 | 2014-06-21 09:20:31 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 33 | static cl::opt<std::string> |
| 34 | InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); |
| 35 | |
| 36 | static cl::opt<std::string> |
| 37 | OutputFilename("o", cl::desc("Output filename"), |
| 38 | cl::value_desc("filename")); |
| 39 | |
| 40 | int main(int argc, char **argv) { |
| 41 | LLVMContext &Context = getGlobalContext(); |
| 42 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 43 | |
| 44 | cl::ParseCommandLineOptions(argc, argv, "libclc builtin preparation tool\n"); |
| 45 | |
| 46 | std::string ErrorMessage; |
| 47 | std::auto_ptr<Module> M; |
| 48 | |
| 49 | { |
Tom Stellard | 8d92c39 | 2014-07-07 17:46:45 +0000 | [diff] [blame] | 50 | #if LLVM_350_AND_NEWER |
| 51 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 52 | MemoryBuffer::getFile(InputFilename); |
| 53 | std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get(); |
| 54 | if (std::error_code ec = BufferOrErr.getError()) |
| 55 | #else |
Jeroen Ketema | d253e66 | 2014-06-21 09:20:31 +0000 | [diff] [blame] | 56 | UNIQUE_PTR<MemoryBuffer> BufferPtr; |
Tom Stellard | 0ab1034 | 2014-06-13 01:30:14 +0000 | [diff] [blame] | 57 | if (ERROR_CODE ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) |
Tom Stellard | 8d92c39 | 2014-07-07 17:46:45 +0000 | [diff] [blame] | 58 | #endif |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 59 | ErrorMessage = ec.message(); |
Tom Stellard | 8a63b15 | 2014-01-20 20:28:48 +0000 | [diff] [blame] | 60 | else { |
| 61 | #if LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR > 4) |
| 62 | ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(BufferPtr.get(), Context); |
Tom Stellard | 0ab1034 | 2014-06-13 01:30:14 +0000 | [diff] [blame] | 63 | if (ERROR_CODE ec = ModuleOrErr.getError()) |
Tom Stellard | 8a63b15 | 2014-01-20 20:28:48 +0000 | [diff] [blame] | 64 | ErrorMessage = ec.message(); |
| 65 | M.reset(ModuleOrErr.get()); |
| 66 | #else |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 67 | M.reset(ParseBitcodeFile(BufferPtr.get(), Context, &ErrorMessage)); |
Tom Stellard | 8a63b15 | 2014-01-20 20:28:48 +0000 | [diff] [blame] | 68 | #endif |
| 69 | } |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | if (M.get() == 0) { |
| 73 | errs() << argv[0] << ": "; |
| 74 | if (ErrorMessage.size()) |
| 75 | errs() << ErrorMessage << "\n"; |
| 76 | else |
| 77 | errs() << "bitcode didn't read correctly.\n"; |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | // Set linkage of every external definition to linkonce_odr. |
| 82 | for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) { |
| 83 | if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage) |
| 84 | i->setLinkage(GlobalValue::LinkOnceODRLinkage); |
| 85 | } |
| 86 | |
| 87 | for (Module::global_iterator i = M->global_begin(), e = M->global_end(); |
| 88 | i != e; ++i) { |
| 89 | if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage) |
| 90 | i->setLinkage(GlobalValue::LinkOnceODRLinkage); |
| 91 | } |
| 92 | |
| 93 | if (OutputFilename.empty()) { |
| 94 | errs() << "no output file\n"; |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | std::string ErrorInfo; |
Jeroen Ketema | d253e66 | 2014-06-21 09:20:31 +0000 | [diff] [blame] | 99 | UNIQUE_PTR<tool_output_file> Out |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 100 | (new tool_output_file(OutputFilename.c_str(), ErrorInfo, |
Tom Stellard | 7aee1cf | 2014-02-24 21:31:56 +0000 | [diff] [blame] | 101 | #if (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR == 4) |
Aaron Watry | 0da3d3b5 | 2013-07-18 21:24:35 +0000 | [diff] [blame] | 102 | sys::fs::F_Binary)); |
Tom Stellard | 7aee1cf | 2014-02-24 21:31:56 +0000 | [diff] [blame] | 103 | #elif LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5) |
| 104 | sys::fs::F_None)); |
Aaron Watry | 0da3d3b5 | 2013-07-18 21:24:35 +0000 | [diff] [blame] | 105 | #else |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 106 | raw_fd_ostream::F_Binary)); |
Aaron Watry | 0da3d3b5 | 2013-07-18 21:24:35 +0000 | [diff] [blame] | 107 | #endif |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 108 | if (!ErrorInfo.empty()) { |
| 109 | errs() << ErrorInfo << '\n'; |
| 110 | exit(1); |
| 111 | } |
| 112 | |
| 113 | WriteBitcodeToFile(M.get(), Out->os()); |
| 114 | |
| 115 | // Declare success. |
| 116 | Out->keep(); |
| 117 | return 0; |
| 118 | } |
| 119 | |