1 //===--- llvm2cpp.cpp - LLVM IR to C++ Translator -------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
15 // Use the --help option for help with command line options.
17 //===------------------------------------------------------------------------===
19 #include "llvm/Module.h"
20 #include "llvm/Bitcode/ReaderWriter.h"
21 #include "llvm/Analysis/Verifier.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/SystemUtils.h"
26 #include "llvm/System/Signals.h"
27 #include "CppWriter.h"
33 static cl::opt<std::string>
34 InputFilename(cl::Positional, cl::desc("<input LLVM bytecode file>"),
37 static cl::opt<std::string>
38 OutputFilename("o", cl::desc("Override output filename"),
39 cl::value_desc("filename"));
42 Force("f", cl::desc("Overwrite output files"));
44 int main(int argc, char **argv) {
45 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
46 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .cpp assembler\n");
47 sys::PrintStackTraceOnErrorSignal();
50 std::ostream *Out = 0;
51 std::string ErrorMessage;
53 std::auto_ptr<Module> M;
54 std::auto_ptr<MemoryBuffer> Buffer(
55 MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage));
57 M.reset(ParseBitcodeFile(Buffer.get(), &ErrorMessage));
59 std::cerr << argv[0] << ": ";
60 if (ErrorMessage.size())
61 std::cerr << ErrorMessage << "\n";
63 std::cerr << "bytecode didn't read correctly.\n";
67 if (OutputFilename != "") { // Specified an output filename?
68 if (OutputFilename != "-") { // Not stdout?
69 if (!Force && std::ifstream(OutputFilename.c_str())) {
70 // If force is not specified, make sure not to overwrite a file!
71 std::cerr << argv[0] << ": error opening '" << OutputFilename
72 << "': file exists!\n"
73 << "Use -f command line argument to force output\n";
76 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
77 std::ios::trunc | std::ios::binary);
78 } else { // Specified stdout
82 if (InputFilename == "-") {
86 std::string IFN = InputFilename;
87 int Len = IFN.length();
88 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
90 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
92 OutputFilename = IFN; // Append a .cpp to it
94 OutputFilename += ".cpp";
96 if (!Force && std::ifstream(OutputFilename.c_str())) {
97 // If force is not specified, make sure not to overwrite a file!
98 std::cerr << argv[0] << ": error opening '" << OutputFilename
99 << "': file exists!\n"
100 << "Use -f command line argument to force output\n";
104 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
105 std::ios::trunc | std::ios::binary);
106 // Make sure that the Out file gets unlinked from the disk if we get a
108 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
113 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
117 WriteModuleToCppFile(M.get(), *Out);
119 if (Out != &std::cout) delete Out;