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 "UpgradeInternals.h"
23 #include "llvm/Module.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/ManagedStatic.h"
26 #include "llvm/Support/Streams.h"
27 #include "llvm/Support/SystemUtils.h"
28 #include "llvm/System/Signals.h"
34 static cl::opt<std::string>
35 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
37 static cl::opt<std::string>
38 OutputFilename("o", cl::desc("Override output filename"),
39 cl::value_desc("filename"), cl::init("-"));
42 Force("f", cl::desc("Overwrite output files"), cl::init(false));
45 AddAttrs("add-attrs", cl::desc("Add function result and argument attributes"),
49 Debug("debug-upgrade-yacc", cl::desc("Print debug output from yacc parser"),
50 cl::Hidden, cl::init(false));
52 int main(int argc, char **argv) {
53 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
54 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
55 sys::PrintStackTraceOnErrorSignal();
58 std::ostream *Out = 0;
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!
65 cerr << argv[0] << ": error opening '" << OutputFilename
66 << "': file exists!\n"
67 << "Use -f command line argument to force output\n";
70 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
72 } else { // Specified stdout
76 if (InputFilename == "-") {
80 std::string IFN = InputFilename;
81 int Len = IFN.length();
82 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
84 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
86 OutputFilename = IFN; // Append to it
88 OutputFilename += ".llu";
90 if (!Force && std::ifstream(OutputFilename.c_str())) {
91 // If force is not specified, make sure not to overwrite a file!
92 cerr << argv[0] << ": error opening '" << OutputFilename
93 << "': file exists!\n"
94 << "Use -f command line argument to force output\n";
98 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
100 // Make sure that the Out file gets unlinked from the disk if we get a
102 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
106 if (InputFilename == "-") {
108 InputFilename = "<stdin>";
110 In = new std::ifstream(InputFilename.c_str());
114 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
119 cerr << argv[0] << ": error opening " << InputFilename << "!\n";
123 Module *M = UpgradeAssembly(InputFilename, *In, Debug, AddAttrs);
125 cerr << argv[0] << ": No module returned from assembly parsing\n";
126 *Out << argv[0] << ": parse failed.";
130 // Finally, print the module on the output stream.
133 } catch (const std::string& caught_message) {
134 cerr << argv[0] << ": " << caught_message << "\n";
137 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
141 if (Out != &std::cout) delete Out;