blob: 9db8ee5f461c7b677c2d0528865d8ebc66a71de4 [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"),
37 cl::value_desc("filename"));
38
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;
48 try {
49 if (OutputFilename != "") { // Specified an output filename?
50 if (OutputFilename != "-") { // Not stdout?
51 if (!Force && std::ifstream(OutputFilename.c_str())) {
52 // If force is not specified, make sure not to overwrite a file!
53 llvm_cerr << argv[0] << ": error opening '" << OutputFilename
54 << "': file exists!\n"
55 << "Use -f command line argument to force output\n";
56 return 1;
57 }
58 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
59 std::ios::trunc);
60 } else { // Specified stdout
61 Out = &std::cout;
62 }
63 } else {
64 if (InputFilename == "-") {
65 OutputFilename = "-";
66 Out = &std::cout;
67 } else {
68 std::string IFN = InputFilename;
69 int Len = IFN.length();
70 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
71 // Source ends in .ll
72 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
73 } else {
74 OutputFilename = IFN; // Append to it
75 }
76 OutputFilename += ".llu";
77
78 if (!Force && std::ifstream(OutputFilename.c_str())) {
79 // If force is not specified, make sure not to overwrite a file!
80 llvm_cerr << argv[0] << ": error opening '" << OutputFilename
81 << "': file exists!\n"
82 << "Use -f command line argument to force output\n";
83 return 1;
84 }
85
86 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
87 std::ios::trunc | std::ios::binary);
88 // Make sure that the Out file gets unlinked from the disk if we get a
89 // SIGINT
90 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
91 }
92 }
93
94 if (!Out->good()) {
95 llvm_cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
96 return 1;
97 }
98
99 UpgradeAssembly(InputFilename, *Out);
100
101 /*
102 } catch (const std::string& caught_message) {
103 llvm_cerr << argv[0] << ": " << caught_message << "\n";
104 exitCode = 1;
105 */
106 } catch (...) {
107 llvm_cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
108 exitCode = 1;
109 }
110
111 if (Out != &std::cout) delete Out;
112 return exitCode;
113}
114