blob: 60c36eb87f117eb60b07a279f28d4b3f517b2200 [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
42int main(int argc, char **argv) {
43 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
44 sys::PrintStackTraceOnErrorSignal();
45
46 int exitCode = 0;
47 std::ostream *Out = 0;
Reid Spencer96839be2006-11-30 16:50:26 +000048 std::istream *In = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +000049 try {
50 if (OutputFilename != "") { // Specified an output filename?
51 if (OutputFilename != "-") { // Not stdout?
52 if (!Force && std::ifstream(OutputFilename.c_str())) {
53 // If force is not specified, make sure not to overwrite a file!
54 llvm_cerr << argv[0] << ": error opening '" << OutputFilename
55 << "': file exists!\n"
56 << "Use -f command line argument to force output\n";
57 return 1;
58 }
59 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
60 std::ios::trunc);
61 } else { // Specified stdout
62 Out = &std::cout;
63 }
64 } else {
65 if (InputFilename == "-") {
66 OutputFilename = "-";
67 Out = &std::cout;
68 } else {
69 std::string IFN = InputFilename;
70 int Len = IFN.length();
71 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
72 // Source ends in .ll
73 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
74 } else {
75 OutputFilename = IFN; // Append to it
76 }
77 OutputFilename += ".llu";
78
79 if (!Force && std::ifstream(OutputFilename.c_str())) {
80 // If force is not specified, make sure not to overwrite a file!
81 llvm_cerr << argv[0] << ": error opening '" << OutputFilename
82 << "': file exists!\n"
83 << "Use -f command line argument to force output\n";
84 return 1;
85 }
86
87 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
Reid Spencer96839be2006-11-30 16:50:26 +000088 std::ios::trunc);
Reid Spencere7c3c602006-11-30 06:36:44 +000089 // Make sure that the Out file gets unlinked from the disk if we get a
90 // SIGINT
91 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
92 }
93 }
94
Reid Spencer96839be2006-11-30 16:50:26 +000095 if (InputFilename == "-") {
96 In = &std::cin;
97 InputFilename = "<stdin>";
98 } else {
99 In = new std::ifstream(InputFilename.c_str());
100 }
101
Reid Spencere7c3c602006-11-30 06:36:44 +0000102 if (!Out->good()) {
103 llvm_cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
104 return 1;
105 }
106
Reid Spencer96839be2006-11-30 16:50:26 +0000107 if (!In->good()) {
108 llvm_cerr << argv[0] << ": error opening " << InputFilename << "!\n";
109 return 1;
110 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000111
Reid Spencer96839be2006-11-30 16:50:26 +0000112 UpgradeAssembly(InputFilename, *In, *Out);
113
Reid Spencere7c3c602006-11-30 06:36:44 +0000114 } catch (const std::string& caught_message) {
115 llvm_cerr << argv[0] << ": " << caught_message << "\n";
116 exitCode = 1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000117 } catch (...) {
118 llvm_cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
119 exitCode = 1;
120 }
121
122 if (Out != &std::cout) delete Out;
123 return exitCode;
124}
125