blob: 2db7543f4817b960936c2377cdc7d58577242b08 [file] [log] [blame]
Reid Spencerd9169d32006-05-29 02:35:29 +00001//===--- llvm2cpp.cpp - LLVM IR to C++ Translator -------------------------===//
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00002//
3// The LLVM Compiler Infrastructure
4//
Reid Spencerba570d72006-05-29 18:06:28 +00005// 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 Spencerfb0c0dc2006-05-29 00:57:22 +00007//
8//===----------------------------------------------------------------------===//
9//
Reid Spencerd9169d32006-05-29 02:35:29 +000010// 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 Spencerfb0c0dc2006-05-29 00:57:22 +000016//
17//===------------------------------------------------------------------------===
18
19#include "llvm/Module.h"
Chris Lattnerb958ba32007-05-06 05:21:42 +000020#include "llvm/Bitcode/ReaderWriter.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000021#include "llvm/Analysis/Verifier.h"
22#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000023#include "llvm/Support/ManagedStatic.h"
Chris Lattnerb958ba32007-05-06 05:21:42 +000024#include "llvm/Support/MemoryBuffer.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000025#include "llvm/Support/SystemUtils.h"
26#include "llvm/System/Signals.h"
27#include "CppWriter.h"
28#include <fstream>
29#include <iostream>
30#include <memory>
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000031using namespace llvm;
32
33static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000034InputFilename(cl::Positional, cl::desc("<input LLVM bitcode file>"),
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000035 cl::init("-"));
36
37static cl::opt<std::string>
38OutputFilename("o", cl::desc("Override output filename"),
39 cl::value_desc("filename"));
40
41static cl::opt<bool>
42Force("f", cl::desc("Overwrite output files"));
43
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000044int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000045 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000046 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .cpp assembler\n");
47 sys::PrintStackTraceOnErrorSignal();
48
49 int exitCode = 0;
50 std::ostream *Out = 0;
Chris Lattner8eaafde2006-07-06 23:48:57 +000051 std::string ErrorMessage;
Chris Lattnerb958ba32007-05-06 05:21:42 +000052
53 std::auto_ptr<Module> M;
Chris Lattner44dadff2007-05-06 09:29:57 +000054 std::auto_ptr<MemoryBuffer> Buffer(
Chris Lattner065344d2007-05-06 23:45:49 +000055 MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage));
Chris Lattner44dadff2007-05-06 09:29:57 +000056 if (Buffer.get())
57 M.reset(ParseBitcodeFile(Buffer.get(), &ErrorMessage));
Chris Lattner8eaafde2006-07-06 23:48:57 +000058 if (M.get() == 0) {
59 std::cerr << argv[0] << ": ";
60 if (ErrorMessage.size())
61 std::cerr << ErrorMessage << "\n";
62 else
Gabor Greifa99be512007-07-05 17:07:56 +000063 std::cerr << "bitcode didn't read correctly.\n";
Chris Lattner8eaafde2006-07-06 23:48:57 +000064 return 1;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000065 }
66
Chris Lattner8eaafde2006-07-06 23:48:57 +000067 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";
74 return 1;
75 }
76 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
77 std::ios::trunc | std::ios::binary);
78 } else { // Specified stdout
79 Out = &std::cout;
80 }
81 } else {
82 if (InputFilename == "-") {
83 OutputFilename = "-";
84 Out = &std::cout;
85 } else {
86 std::string IFN = InputFilename;
87 int Len = IFN.length();
88 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
89 // Source ends in .ll
90 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
91 } else {
92 OutputFilename = IFN; // Append a .cpp to it
93 }
94 OutputFilename += ".cpp";
95
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";
101 return 1;
102 }
103
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
107 // SIGINT
108 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
109 }
110 }
111
112 if (!Out->good()) {
113 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
114 return 1;
115 }
116
117 WriteModuleToCppFile(M.get(), *Out);
118
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000119 if (Out != &std::cout) delete Out;
120 return exitCode;
121}
122