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"));
42 int main(int argc, char **argv) {
43 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
44 sys::PrintStackTraceOnErrorSignal();
47 std::ostream *Out = 0;
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";
59 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
61 } else { // Specified stdout
65 if (InputFilename == "-") {
69 std::string IFN = InputFilename;
70 int Len = IFN.length();
71 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
73 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
75 OutputFilename = IFN; // Append to it
77 OutputFilename += ".llu";
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";
87 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
89 // Make sure that the Out file gets unlinked from the disk if we get a
91 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
95 if (InputFilename == "-") {
97 InputFilename = "<stdin>";
99 In = new std::ifstream(InputFilename.c_str());
103 llvm_cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
108 llvm_cerr << argv[0] << ": error opening " << InputFilename << "!\n";
112 UpgradeAssembly(InputFilename, *In, *Out);
114 } catch (const std::string& caught_message) {
115 llvm_cerr << argv[0] << ": " << caught_message << "\n";
118 llvm_cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
122 if (Out != &std::cout) delete Out;