blob: 7e322dbef8162262ada9ea7061c7aa7d7a789d3b [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"
20#include "llvm/Assembly/Parser.h"
21#include "llvm/Bytecode/Writer.h"
22#include "llvm/Analysis/Verifier.h"
23#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/SystemUtils.h"
25#include "llvm/System/Signals.h"
26#include "CppWriter.h"
27#include <fstream>
28#include <iostream>
29#include <memory>
30
31using namespace llvm;
32
33static cl::opt<std::string>
34InputFilename(cl::Positional, cl::desc("<input LLVM assembly file>"),
35 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
44static cl::opt<bool>
45DisableVerify("disable-verify", cl::Hidden,
46 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
47
48int main(int argc, char **argv) {
49 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .cpp assembler\n");
50 sys::PrintStackTraceOnErrorSignal();
51
52 int exitCode = 0;
53 std::ostream *Out = 0;
54 try {
55 // Parse the file now...
56 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename));
57 if (M.get() == 0) {
58 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
59 return 1;
60 }
61
Chris Lattner05ac92c2006-07-06 18:02:27 +000062 // FIXME: llvm2cpp should read .bc files and thus not run the verifier
63 // explicitly!
64 if (!DisableVerify) {
65 std::string Err;
66 if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
67 std::cerr << argv[0]
68 << ": assembly parsed, but does not verify as correct!\n";
69 std::cerr << Err;
70 return 1;
71 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000072 }
73
74 if (OutputFilename != "") { // Specified an output filename?
75 if (OutputFilename != "-") { // Not stdout?
76 if (!Force && std::ifstream(OutputFilename.c_str())) {
77 // If force is not specified, make sure not to overwrite a file!
78 std::cerr << argv[0] << ": error opening '" << OutputFilename
79 << "': file exists!\n"
80 << "Use -f command line argument to force output\n";
81 return 1;
82 }
83 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
84 std::ios::trunc | std::ios::binary);
85 } else { // Specified stdout
86 // FIXME: cout is not binary!
87 Out = &std::cout;
88 }
89 } else {
90 if (InputFilename == "-") {
91 OutputFilename = "-";
92 Out = &std::cout;
93 } else {
94 std::string IFN = InputFilename;
95 int Len = IFN.length();
96 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
97 // Source ends in .ll
98 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
99 } else {
100 OutputFilename = IFN; // Append a .cpp to it
101 }
102 OutputFilename += ".cpp";
103
104 if (!Force && std::ifstream(OutputFilename.c_str())) {
105 // If force is not specified, make sure not to overwrite a file!
106 std::cerr << argv[0] << ": error opening '" << OutputFilename
107 << "': file exists!\n"
108 << "Use -f command line argument to force output\n";
109 return 1;
110 }
111
112 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
113 std::ios::trunc | std::ios::binary);
114 // Make sure that the Out file gets unlinked from the disk if we get a
115 // SIGINT
116 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
117 }
118 }
119
120 if (!Out->good()) {
121 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
122 return 1;
123 }
124
125 WriteModuleToCppFile(M.get(), *Out);
126
127 } catch (const ParseException &E) {
128 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
129 exitCode = 1;
130 } catch (const std::string& msg) {
131 std::cerr << argv[0] << ": " << msg << "\n";
132 exitCode = 1;
133 } catch (...) {
134 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
135 exitCode = 1;
136 }
137
138 if (Out != &std::cout) delete Out;
139 return exitCode;
140}
141