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