blob: 01c8fbd0a51dbab9239f684a86ab848478bd29ae [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"
Chris Lattnerc30598b2006-12-06 01:18:01 +000023#include "llvm/Support/ManagedStatic.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000024#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>
Chris Lattner8eaafde2006-07-06 23:48:57 +000034InputFilename(cl::Positional, cl::desc("<input LLVM bytecode 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;
52 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
53 if (M.get() == 0) {
54 std::cerr << argv[0] << ": ";
55 if (ErrorMessage.size())
56 std::cerr << ErrorMessage << "\n";
57 else
58 std::cerr << "bytecode didn't read correctly.\n";
59 return 1;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000060 }
61
Chris Lattner8eaafde2006-07-06 23:48:57 +000062 if (OutputFilename != "") { // Specified an output filename?
63 if (OutputFilename != "-") { // Not stdout?
64 if (!Force && std::ifstream(OutputFilename.c_str())) {
65 // If force is not specified, make sure not to overwrite a file!
66 std::cerr << argv[0] << ": error opening '" << OutputFilename
67 << "': file exists!\n"
68 << "Use -f command line argument to force output\n";
69 return 1;
70 }
71 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
72 std::ios::trunc | std::ios::binary);
73 } else { // Specified stdout
74 Out = &std::cout;
75 }
76 } else {
77 if (InputFilename == "-") {
78 OutputFilename = "-";
79 Out = &std::cout;
80 } else {
81 std::string IFN = InputFilename;
82 int Len = IFN.length();
83 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
84 // Source ends in .ll
85 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
86 } else {
87 OutputFilename = IFN; // Append a .cpp to it
88 }
89 OutputFilename += ".cpp";
90
91 if (!Force && std::ifstream(OutputFilename.c_str())) {
92 // If force is not specified, make sure not to overwrite a file!
93 std::cerr << argv[0] << ": error opening '" << OutputFilename
94 << "': file exists!\n"
95 << "Use -f command line argument to force output\n";
96 return 1;
97 }
98
99 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
100 std::ios::trunc | std::ios::binary);
101 // Make sure that the Out file gets unlinked from the disk if we get a
102 // SIGINT
103 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
104 }
105 }
106
107 if (!Out->good()) {
108 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
109 return 1;
110 }
111
112 WriteModuleToCppFile(M.get(), *Out);
113
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000114 if (Out != &std::cout) delete Out;
115 return exitCode;
116}
117