Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 1 | //===--- 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 | |
Reid Spencer | bcdd54c | 2007-01-05 17:19:32 +0000 | [diff] [blame] | 22 | #include "UpgradeInternals.h" |
Reid Spencer | 950bf60 | 2007-01-26 08:19:09 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ManagedStatic.h" |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Streams.h" |
| 27 | #include "llvm/Support/SystemUtils.h" |
| 28 | #include "llvm/System/Signals.h" |
| 29 | #include <fstream> |
| 30 | #include <iostream> |
| 31 | #include <memory> |
| 32 | using namespace llvm; |
| 33 | |
| 34 | static cl::opt<std::string> |
| 35 | InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-")); |
| 36 | |
| 37 | static cl::opt<std::string> |
| 38 | OutputFilename("o", cl::desc("Override output filename"), |
Reid Spencer | 96839be | 2006-11-30 16:50:26 +0000 | [diff] [blame] | 39 | cl::value_desc("filename"), cl::init("-")); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 40 | |
| 41 | static cl::opt<bool> |
Reid Spencer | 71d2ec9 | 2006-12-31 06:02:26 +0000 | [diff] [blame] | 42 | Force("f", cl::desc("Overwrite output files"), cl::init(false)); |
| 43 | |
| 44 | static cl::opt<bool> |
| 45 | AddAttrs("add-attrs", cl::desc("Add function result and argument attributes"), |
| 46 | cl::init(false)); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 47 | |
Reid Spencer | e77e35e | 2006-12-01 20:26:20 +0000 | [diff] [blame] | 48 | static cl::opt<bool> |
Reid Spencer | 950bf60 | 2007-01-26 08:19:09 +0000 | [diff] [blame] | 49 | Debug("debug-upgrade-yacc", cl::desc("Print debug output from yacc parser"), |
| 50 | cl::Hidden, cl::init(false)); |
| 51 | |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 52 | int main(int argc, char **argv) { |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 53 | llvm_shutdown_obj X; // Call llvm_shutdown() on exit. |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 54 | cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n"); |
| 55 | sys::PrintStackTraceOnErrorSignal(); |
| 56 | |
| 57 | int exitCode = 0; |
| 58 | std::ostream *Out = 0; |
Reid Spencer | 96839be | 2006-11-30 16:50:26 +0000 | [diff] [blame] | 59 | std::istream *In = 0; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 60 | try { |
| 61 | if (OutputFilename != "") { // Specified an output filename? |
| 62 | if (OutputFilename != "-") { // Not stdout? |
| 63 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
| 64 | // If force is not specified, make sure not to overwrite a file! |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 65 | cerr << argv[0] << ": error opening '" << OutputFilename |
| 66 | << "': file exists!\n" |
| 67 | << "Use -f command line argument to force output\n"; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 68 | return 1; |
| 69 | } |
| 70 | Out = new std::ofstream(OutputFilename.c_str(), std::ios::out | |
| 71 | std::ios::trunc); |
| 72 | } else { // Specified stdout |
| 73 | Out = &std::cout; |
| 74 | } |
| 75 | } else { |
| 76 | if (InputFilename == "-") { |
| 77 | OutputFilename = "-"; |
| 78 | Out = &std::cout; |
| 79 | } else { |
| 80 | std::string IFN = InputFilename; |
| 81 | int Len = IFN.length(); |
| 82 | if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') { |
| 83 | // Source ends in .ll |
| 84 | OutputFilename = std::string(IFN.begin(), IFN.end()-3); |
| 85 | } else { |
| 86 | OutputFilename = IFN; // Append to it |
| 87 | } |
| 88 | OutputFilename += ".llu"; |
| 89 | |
| 90 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
| 91 | // If force is not specified, make sure not to overwrite a file! |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 92 | cerr << argv[0] << ": error opening '" << OutputFilename |
| 93 | << "': file exists!\n" |
| 94 | << "Use -f command line argument to force output\n"; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | Out = new std::ofstream(OutputFilename.c_str(), std::ios::out | |
Reid Spencer | 96839be | 2006-11-30 16:50:26 +0000 | [diff] [blame] | 99 | std::ios::trunc); |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 100 | // Make sure that the Out file gets unlinked from the disk if we get a |
| 101 | // SIGINT |
| 102 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 103 | } |
| 104 | } |
| 105 | |
Reid Spencer | 96839be | 2006-11-30 16:50:26 +0000 | [diff] [blame] | 106 | if (InputFilename == "-") { |
| 107 | In = &std::cin; |
| 108 | InputFilename = "<stdin>"; |
| 109 | } else { |
| 110 | In = new std::ifstream(InputFilename.c_str()); |
| 111 | } |
| 112 | |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 113 | if (!Out->good()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 114 | cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 115 | return 1; |
| 116 | } |
| 117 | |
Reid Spencer | 96839be | 2006-11-30 16:50:26 +0000 | [diff] [blame] | 118 | if (!In->good()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 119 | cerr << argv[0] << ": error opening " << InputFilename << "!\n"; |
Reid Spencer | 96839be | 2006-11-30 16:50:26 +0000 | [diff] [blame] | 120 | return 1; |
| 121 | } |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 122 | |
Reid Spencer | 950bf60 | 2007-01-26 08:19:09 +0000 | [diff] [blame] | 123 | Module *M = UpgradeAssembly(InputFilename, *In, Debug, AddAttrs); |
| 124 | if (!M) { |
| 125 | cerr << argv[0] << ": No module returned from assembly parsing\n"; |
Chris Lattner | e3c55a5 | 2007-05-06 05:40:41 +0000 | [diff] [blame^] | 126 | *Out << argv[0] << ": parse failed."; |
Reid Spencer | 950bf60 | 2007-01-26 08:19:09 +0000 | [diff] [blame] | 127 | exit(1); |
| 128 | } |
| 129 | |
| 130 | // Finally, print the module on the output stream. |
Chris Lattner | e3c55a5 | 2007-05-06 05:40:41 +0000 | [diff] [blame^] | 131 | M->print(Out); |
Reid Spencer | 96839be | 2006-11-30 16:50:26 +0000 | [diff] [blame] | 132 | |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 133 | } catch (const std::string& caught_message) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 134 | cerr << argv[0] << ": " << caught_message << "\n"; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 135 | exitCode = 1; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 136 | } catch (...) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 137 | cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; |
Reid Spencer | e7c3c60 | 2006-11-30 06:36:44 +0000 | [diff] [blame] | 138 | exitCode = 1; |
| 139 | } |
| 140 | |
| 141 | if (Out != &std::cout) delete Out; |
| 142 | return exitCode; |
| 143 | } |
| 144 | |