Reid Spencer | d9169d3 | 2006-05-29 02:35:29 +0000 | [diff] [blame] | 1 | //===--- llvm2cpp.cpp - LLVM IR to C++ Translator -------------------------===// |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Reid Spencer | ba570d7 | 2006-05-29 18:06:28 +0000 | [diff] [blame] | 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Reid Spencer | d9169d3 | 2006-05-29 02:35:29 +0000 | [diff] [blame] | 10 | // This program converts an input LLVM assembly file (.ll) into a C++ source |
| 11 | // file that makes calls to the LLVM C++ API to produce the same module. The |
| 12 | // generated program verifies what it built and then runs the PrintAssemblyPass |
| 13 | // to reproduce the input originally given to llvm2cpp. |
| 14 | // |
| 15 | // Use the --help option for help with command line options. |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 16 | // |
| 17 | //===------------------------------------------------------------------------=== |
| 18 | |
| 19 | #include "llvm/Module.h" |
Chris Lattner | 8eaafde | 2006-07-06 23:48:57 +0000 | [diff] [blame] | 20 | #include "llvm/Bytecode/Reader.h" |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/Verifier.h" |
| 22 | #include "llvm/Support/CommandLine.h" |
| 23 | #include "llvm/Support/SystemUtils.h" |
| 24 | #include "llvm/System/Signals.h" |
| 25 | #include "CppWriter.h" |
| 26 | #include <fstream> |
| 27 | #include <iostream> |
| 28 | #include <memory> |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | static cl::opt<std::string> |
Chris Lattner | 8eaafde | 2006-07-06 23:48:57 +0000 | [diff] [blame] | 33 | InputFilename(cl::Positional, cl::desc("<input LLVM bytecode file>"), |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 34 | cl::init("-")); |
| 35 | |
| 36 | static cl::opt<std::string> |
| 37 | OutputFilename("o", cl::desc("Override output filename"), |
| 38 | cl::value_desc("filename")); |
| 39 | |
| 40 | static cl::opt<bool> |
| 41 | Force("f", cl::desc("Overwrite output files")); |
| 42 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 43 | int main(int argc, char **argv) { |
| 44 | cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .cpp assembler\n"); |
| 45 | sys::PrintStackTraceOnErrorSignal(); |
| 46 | |
| 47 | int exitCode = 0; |
| 48 | std::ostream *Out = 0; |
Chris Lattner | 8eaafde | 2006-07-06 23:48:57 +0000 | [diff] [blame] | 49 | std::string ErrorMessage; |
| 50 | std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage)); |
| 51 | if (M.get() == 0) { |
| 52 | std::cerr << argv[0] << ": "; |
| 53 | if (ErrorMessage.size()) |
| 54 | std::cerr << ErrorMessage << "\n"; |
| 55 | else |
| 56 | std::cerr << "bytecode didn't read correctly.\n"; |
| 57 | return 1; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Chris Lattner | 8eaafde | 2006-07-06 23:48:57 +0000 | [diff] [blame] | 60 | if (OutputFilename != "") { // Specified an output filename? |
| 61 | if (OutputFilename != "-") { // Not stdout? |
| 62 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
| 63 | // If force is not specified, make sure not to overwrite a file! |
| 64 | std::cerr << argv[0] << ": error opening '" << OutputFilename |
| 65 | << "': file exists!\n" |
| 66 | << "Use -f command line argument to force output\n"; |
| 67 | return 1; |
| 68 | } |
| 69 | Out = new std::ofstream(OutputFilename.c_str(), std::ios::out | |
| 70 | std::ios::trunc | std::ios::binary); |
| 71 | } else { // Specified stdout |
| 72 | Out = &std::cout; |
| 73 | } |
| 74 | } else { |
| 75 | if (InputFilename == "-") { |
| 76 | OutputFilename = "-"; |
| 77 | Out = &std::cout; |
| 78 | } else { |
| 79 | std::string IFN = InputFilename; |
| 80 | int Len = IFN.length(); |
| 81 | if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') { |
| 82 | // Source ends in .ll |
| 83 | OutputFilename = std::string(IFN.begin(), IFN.end()-3); |
| 84 | } else { |
| 85 | OutputFilename = IFN; // Append a .cpp to it |
| 86 | } |
| 87 | OutputFilename += ".cpp"; |
| 88 | |
| 89 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
| 90 | // If force is not specified, make sure not to overwrite a file! |
| 91 | std::cerr << argv[0] << ": error opening '" << OutputFilename |
| 92 | << "': file exists!\n" |
| 93 | << "Use -f command line argument to force output\n"; |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | Out = new std::ofstream(OutputFilename.c_str(), std::ios::out | |
| 98 | std::ios::trunc | std::ios::binary); |
| 99 | // Make sure that the Out file gets unlinked from the disk if we get a |
| 100 | // SIGINT |
| 101 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (!Out->good()) { |
| 106 | std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; |
| 107 | return 1; |
| 108 | } |
| 109 | |
| 110 | WriteModuleToCppFile(M.get(), *Out); |
| 111 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 112 | if (Out != &std::cout) delete Out; |
| 113 | return exitCode; |
| 114 | } |
| 115 | |