blob: 8d3653ae223bb07d6993632588ffa75b128ff7dd [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 Lattner8eaafde2006-07-06 23:48:57 +000020#include "llvm/Bytecode/Reader.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000021#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
30using namespace llvm;
31
32static cl::opt<std::string>
Chris Lattner8eaafde2006-07-06 23:48:57 +000033InputFilename(cl::Positional, cl::desc("<input LLVM bytecode file>"),
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000034 cl::init("-"));
35
36static cl::opt<std::string>
37OutputFilename("o", cl::desc("Override output filename"),
38 cl::value_desc("filename"));
39
40static cl::opt<bool>
41Force("f", cl::desc("Overwrite output files"));
42
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000043int 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 Lattner8eaafde2006-07-06 23:48:57 +000049 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 Spencerfb0c0dc2006-05-29 00:57:22 +000058 }
59
Chris Lattner8eaafde2006-07-06 23:48:57 +000060 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 Spencerfb0c0dc2006-05-29 00:57:22 +0000112 if (Out != &std::cout) delete Out;
113 return exitCode;
114}
115