blob: 64ae5abf9a2a8c1c704a244a0b9bd9c7f290f101 [file] [log] [blame]
Reid Spencere7c3c602006-11-30 06:36:44 +00001//===--- 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
22#include "ParserInternals.h"
23#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000024#include "llvm/Support/ManagedStatic.h"
Reid Spencere7c3c602006-11-30 06:36:44 +000025#include "llvm/Support/Streams.h"
26#include "llvm/Support/SystemUtils.h"
27#include "llvm/System/Signals.h"
28#include <fstream>
29#include <iostream>
30#include <memory>
31using namespace llvm;
32
33static cl::opt<std::string>
34InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
35
36static cl::opt<std::string>
37OutputFilename("o", cl::desc("Override output filename"),
Reid Spencer96839be2006-11-30 16:50:26 +000038 cl::value_desc("filename"), cl::init("-"));
Reid Spencere7c3c602006-11-30 06:36:44 +000039
40static cl::opt<bool>
Reid Spencer71d2ec92006-12-31 06:02:26 +000041Force("f", cl::desc("Overwrite output files"), cl::init(false));
42
43static cl::opt<bool>
44AddAttrs("add-attrs", cl::desc("Add function result and argument attributes"),
45 cl::init(false));
Reid Spencere7c3c602006-11-30 06:36:44 +000046
Reid Spencere77e35e2006-12-01 20:26:20 +000047static cl::opt<bool>
48Debug("debug", cl::desc("Print debug output from yacc parser"),cl::Hidden,
49 cl::init(false));
50
Reid Spencere7c3c602006-11-30 06:36:44 +000051int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000052 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencere7c3c602006-11-30 06:36:44 +000053 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
54 sys::PrintStackTraceOnErrorSignal();
55
56 int exitCode = 0;
57 std::ostream *Out = 0;
Reid Spencer96839be2006-11-30 16:50:26 +000058 std::istream *In = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +000059 try {
60 if (OutputFilename != "") { // Specified an output filename?
61 if (OutputFilename != "-") { // Not stdout?
62 if (!Force && std::ifstream(OutputFilename.c_str())) {
63 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000064 cerr << argv[0] << ": error opening '" << OutputFilename
65 << "': file exists!\n"
66 << "Use -f command line argument to force output\n";
Reid Spencere7c3c602006-11-30 06:36:44 +000067 return 1;
68 }
69 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
70 std::ios::trunc);
71 } else { // Specified stdout
72 Out = &std::cout;
73 }
74 } else {
75 if (InputFilename == "-") {
76 OutputFilename = "-";
77 Out = &std::cout;
78 } else {
79 std::string IFN = InputFilename;
80 int Len = IFN.length();
81 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
82 // Source ends in .ll
83 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
84 } else {
85 OutputFilename = IFN; // Append to it
86 }
87 OutputFilename += ".llu";
88
89 if (!Force && std::ifstream(OutputFilename.c_str())) {
90 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000091 cerr << argv[0] << ": error opening '" << OutputFilename
92 << "': file exists!\n"
93 << "Use -f command line argument to force output\n";
Reid Spencere7c3c602006-11-30 06:36:44 +000094 return 1;
95 }
96
97 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
Reid Spencer96839be2006-11-30 16:50:26 +000098 std::ios::trunc);
Reid Spencere7c3c602006-11-30 06:36:44 +000099 // Make sure that the Out file gets unlinked from the disk if we get a
100 // SIGINT
101 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
102 }
103 }
104
Reid Spencer96839be2006-11-30 16:50:26 +0000105 if (InputFilename == "-") {
106 In = &std::cin;
107 InputFilename = "<stdin>";
108 } else {
109 In = new std::ifstream(InputFilename.c_str());
110 }
111
Reid Spencere7c3c602006-11-30 06:36:44 +0000112 if (!Out->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000113 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Reid Spencere7c3c602006-11-30 06:36:44 +0000114 return 1;
115 }
116
Reid Spencer96839be2006-11-30 16:50:26 +0000117 if (!In->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000118 cerr << argv[0] << ": error opening " << InputFilename << "!\n";
Reid Spencer96839be2006-11-30 16:50:26 +0000119 return 1;
120 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000121
Reid Spencer71d2ec92006-12-31 06:02:26 +0000122 UpgradeAssembly(InputFilename, *In, *Out, Debug, AddAttrs);
Reid Spencer96839be2006-11-30 16:50:26 +0000123
Reid Spencere7c3c602006-11-30 06:36:44 +0000124 } catch (const std::string& caught_message) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000125 cerr << argv[0] << ": " << caught_message << "\n";
Reid Spencere7c3c602006-11-30 06:36:44 +0000126 exitCode = 1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000127 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000128 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +0000129 exitCode = 1;
130 }
131
132 if (Out != &std::cout) delete Out;
133 return exitCode;
134}
135