1 //===--- llvm-upgrade.cpp - The LLVM Assembly Upgrader --------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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
14 // or, you can directly upgrade, like this:
15 // llvm-upgrade -o 2.0.ll < 1.9.ll
17 // llvm-upgrade won't overwrite files by default. Use -f to force it to
18 // overwrite the output file.
20 //===----------------------------------------------------------------------===//
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"
32 static cl::opt<std::string>
33 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
35 static cl::opt<std::string>
36 OutputFilename("o", cl::desc("Override output filename"),
37 cl::value_desc("filename"), cl::init("-"));
40 Force("f", cl::desc("Overwrite output files"));
43 Debug("debug", cl::desc("Print debug output from yacc parser"),cl::Hidden,
46 int main(int argc, char **argv) {
47 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
48 sys::PrintStackTraceOnErrorSignal();
51 std::ostream *Out = 0;
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";
63 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
65 } else { // Specified stdout
69 if (InputFilename == "-") {
73 std::string IFN = InputFilename;
74 int Len = IFN.length();
75 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
77 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
79 OutputFilename = IFN; // Append to it
81 OutputFilename += ".llu";
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";
91 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
93 // Make sure that the Out file gets unlinked from the disk if we get a
95 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
99 if (InputFilename == "-") {
101 InputFilename = "<stdin>";
103 In = new std::ifstream(InputFilename.c_str());
107 llvm_cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
112 llvm_cerr << argv[0] << ": error opening " << InputFilename << "!\n";
116 UpgradeAssembly(InputFilename, *In, *Out, Debug);
118 } catch (const std::string& caught_message) {
119 llvm_cerr << argv[0] << ": " << caught_message << "\n";
122 llvm_cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
126 if (Out != &std::cout) delete Out;