blob: 1046736c15a33388a57c87ff8c582d176e61fd94 [file] [log] [blame]
Reid Spencere7c3c602006-11-30 06:36:44 +00001//===--- llvm-upgrade.cpp - The LLVM Assembly Upgrader --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This utility will upgrade LLVM 1.9 Assembly to 2.0 format. It may be
11// invoked as a filter, like this:
12// llvm-1.9/bin/llvm-dis < 1.9.bc | llvm-upgrade | llvm-as > 2.0.bc
13//
14// or, you can directly upgrade, like this:
15// llvm-upgrade -o 2.0.ll < 1.9.ll
16//
17// llvm-upgrade won't overwrite files by default. Use -f to force it to
18// overwrite the output file.
19//
20//===----------------------------------------------------------------------===//
21
22#include "ParserInternals.h"
23#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/Streams.h"
25#include "llvm/Support/SystemUtils.h"
26#include "llvm/System/Signals.h"
27#include <fstream>
28#include <iostream>
29#include <memory>
30using namespace llvm;
31
32static cl::opt<std::string>
33InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
34
35static cl::opt<std::string>
36OutputFilename("o", cl::desc("Override output filename"),
Reid Spencer96839be2006-11-30 16:50:26 +000037 cl::value_desc("filename"), cl::init("-"));
Reid Spencere7c3c602006-11-30 06:36:44 +000038
39static cl::opt<bool>
40Force("f", cl::desc("Overwrite output files"));
41
Reid Spencere77e35e2006-12-01 20:26:20 +000042static cl::opt<bool>
43Debug("debug", cl::desc("Print debug output from yacc parser"),cl::Hidden,
44 cl::init(false));
45
Reid Spencere7c3c602006-11-30 06:36:44 +000046int main(int argc, char **argv) {
47 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
48 sys::PrintStackTraceOnErrorSignal();
49
50 int exitCode = 0;
51 std::ostream *Out = 0;
Reid Spencer96839be2006-11-30 16:50:26 +000052 std::istream *In = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +000053 try {
54 if (OutputFilename != "") { // Specified an output filename?
55 if (OutputFilename != "-") { // Not stdout?
56 if (!Force && std::ifstream(OutputFilename.c_str())) {
57 // If force is not specified, make sure not to overwrite a file!
58 llvm_cerr << argv[0] << ": error opening '" << OutputFilename
59 << "': file exists!\n"
60 << "Use -f command line argument to force output\n";
61 return 1;
62 }
63 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
64 std::ios::trunc);
65 } else { // Specified stdout
66 Out = &std::cout;
67 }
68 } else {
69 if (InputFilename == "-") {
70 OutputFilename = "-";
71 Out = &std::cout;
72 } else {
73 std::string IFN = InputFilename;
74 int Len = IFN.length();
75 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
76 // Source ends in .ll
77 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
78 } else {
79 OutputFilename = IFN; // Append to it
80 }
81 OutputFilename += ".llu";
82
83 if (!Force && std::ifstream(OutputFilename.c_str())) {
84 // If force is not specified, make sure not to overwrite a file!
85 llvm_cerr << argv[0] << ": error opening '" << OutputFilename
86 << "': file exists!\n"
87 << "Use -f command line argument to force output\n";
88 return 1;
89 }
90
91 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
Reid Spencer96839be2006-11-30 16:50:26 +000092 std::ios::trunc);
Reid Spencere7c3c602006-11-30 06:36:44 +000093 // Make sure that the Out file gets unlinked from the disk if we get a
94 // SIGINT
95 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
96 }
97 }
98
Reid Spencer96839be2006-11-30 16:50:26 +000099 if (InputFilename == "-") {
100 In = &std::cin;
101 InputFilename = "<stdin>";
102 } else {
103 In = new std::ifstream(InputFilename.c_str());
104 }
105
Reid Spencere7c3c602006-11-30 06:36:44 +0000106 if (!Out->good()) {
107 llvm_cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
108 return 1;
109 }
110
Reid Spencer96839be2006-11-30 16:50:26 +0000111 if (!In->good()) {
112 llvm_cerr << argv[0] << ": error opening " << InputFilename << "!\n";
113 return 1;
114 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000115
Reid Spencere77e35e2006-12-01 20:26:20 +0000116 UpgradeAssembly(InputFilename, *In, *Out, Debug);
Reid Spencer96839be2006-11-30 16:50:26 +0000117
Reid Spencere7c3c602006-11-30 06:36:44 +0000118 } catch (const std::string& caught_message) {
119 llvm_cerr << argv[0] << ": " << caught_message << "\n";
120 exitCode = 1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000121 } catch (...) {
122 llvm_cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
123 exitCode = 1;
124 }
125
126 if (Out != &std::cout) delete Out;
127 return exitCode;
128}
129