blob: 7d27b72ea13029a054a13b9dc7640bc8cc30adc4 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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
62 try {
63 if (!DisableVerify)
64 verifyModule(*M.get(), ThrowExceptionAction);
65 } catch (const std::string &Err) {
66 std::cerr << argv[0]
67 << ": assembly parsed, but does not verify as correct!\n";
68 std::cerr << Err;
69 return 1;
70 }
71
72 if (OutputFilename != "") { // Specified an output filename?
73 if (OutputFilename != "-") { // Not stdout?
74 if (!Force && std::ifstream(OutputFilename.c_str())) {
75 // If force is not specified, make sure not to overwrite a file!
76 std::cerr << argv[0] << ": error opening '" << OutputFilename
77 << "': file exists!\n"
78 << "Use -f command line argument to force output\n";
79 return 1;
80 }
81 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
82 std::ios::trunc | std::ios::binary);
83 } else { // Specified stdout
84 // FIXME: cout is not binary!
85 Out = &std::cout;
86 }
87 } else {
88 if (InputFilename == "-") {
89 OutputFilename = "-";
90 Out = &std::cout;
91 } else {
92 std::string IFN = InputFilename;
93 int Len = IFN.length();
94 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
95 // Source ends in .ll
96 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
97 } else {
98 OutputFilename = IFN; // Append a .cpp to it
99 }
100 OutputFilename += ".cpp";
101
102 if (!Force && std::ifstream(OutputFilename.c_str())) {
103 // If force is not specified, make sure not to overwrite a file!
104 std::cerr << argv[0] << ": error opening '" << OutputFilename
105 << "': file exists!\n"
106 << "Use -f command line argument to force output\n";
107 return 1;
108 }
109
110 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
111 std::ios::trunc | std::ios::binary);
112 // Make sure that the Out file gets unlinked from the disk if we get a
113 // SIGINT
114 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
115 }
116 }
117
118 if (!Out->good()) {
119 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
120 return 1;
121 }
122
123 WriteModuleToCppFile(M.get(), *Out);
124
125 } catch (const ParseException &E) {
126 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
127 exitCode = 1;
128 } catch (const std::string& msg) {
129 std::cerr << argv[0] << ": " << msg << "\n";
130 exitCode = 1;
131 } catch (...) {
132 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
133 exitCode = 1;
134 }
135
136 if (Out != &std::cout) delete Out;
137 return exitCode;
138}
139