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