blob: 9425e3e579a28e8836770bd603668482363d75bc [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
Reid Spencerbcdd54c2007-01-05 17:19:32 +000022#include "UpgradeInternals.h"
Reid Spencer950bf602007-01-26 08:19:09 +000023#include "llvm/Module.h"
Reid Spencere7c3c602006-11-30 06:36:44 +000024#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000025#include "llvm/Support/ManagedStatic.h"
Reid Spencere7c3c602006-11-30 06:36:44 +000026#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>
32using namespace llvm;
33
34static cl::opt<std::string>
35InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
36
37static cl::opt<std::string>
38OutputFilename("o", cl::desc("Override output filename"),
Reid Spencer96839be2006-11-30 16:50:26 +000039 cl::value_desc("filename"), cl::init("-"));
Reid Spencere7c3c602006-11-30 06:36:44 +000040
41static cl::opt<bool>
Reid Spencer71d2ec92006-12-31 06:02:26 +000042Force("f", cl::desc("Overwrite output files"), cl::init(false));
43
44static cl::opt<bool>
45AddAttrs("add-attrs", cl::desc("Add function result and argument attributes"),
46 cl::init(false));
Reid Spencere7c3c602006-11-30 06:36:44 +000047
Reid Spencere77e35e2006-12-01 20:26:20 +000048static cl::opt<bool>
Reid Spencer950bf602007-01-26 08:19:09 +000049Debug("debug-upgrade-yacc", cl::desc("Print debug output from yacc parser"),
50 cl::Hidden, cl::init(false));
51
Reid Spencere7c3c602006-11-30 06:36:44 +000052int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000053 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencere7c3c602006-11-30 06:36:44 +000054 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
55 sys::PrintStackTraceOnErrorSignal();
56
57 int exitCode = 0;
58 std::ostream *Out = 0;
Reid Spencer96839be2006-11-30 16:50:26 +000059 std::istream *In = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +000060 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 Wendlinge8156192006-12-07 01:30:32 +000065 cerr << argv[0] << ": error opening '" << OutputFilename
66 << "': file exists!\n"
67 << "Use -f command line argument to force output\n";
Reid Spencere7c3c602006-11-30 06:36:44 +000068 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 Wendlinge8156192006-12-07 01:30:32 +000092 cerr << argv[0] << ": error opening '" << OutputFilename
93 << "': file exists!\n"
94 << "Use -f command line argument to force output\n";
Reid Spencere7c3c602006-11-30 06:36:44 +000095 return 1;
96 }
97
98 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
Reid Spencer96839be2006-11-30 16:50:26 +000099 std::ios::trunc);
Reid Spencere7c3c602006-11-30 06:36:44 +0000100 // 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 Spencer96839be2006-11-30 16:50:26 +0000106 if (InputFilename == "-") {
107 In = &std::cin;
108 InputFilename = "<stdin>";
109 } else {
110 In = new std::ifstream(InputFilename.c_str());
111 }
112
Reid Spencere7c3c602006-11-30 06:36:44 +0000113 if (!Out->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000114 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Reid Spencere7c3c602006-11-30 06:36:44 +0000115 return 1;
116 }
117
Reid Spencer96839be2006-11-30 16:50:26 +0000118 if (!In->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000119 cerr << argv[0] << ": error opening " << InputFilename << "!\n";
Reid Spencer96839be2006-11-30 16:50:26 +0000120 return 1;
121 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000122
Reid Spencer950bf602007-01-26 08:19:09 +0000123 Module *M = UpgradeAssembly(InputFilename, *In, Debug, AddAttrs);
124 if (!M) {
125 cerr << argv[0] << ": No module returned from assembly parsing\n";
Chris Lattnere3c55a52007-05-06 05:40:41 +0000126 *Out << argv[0] << ": parse failed.";
Reid Spencer950bf602007-01-26 08:19:09 +0000127 exit(1);
128 }
129
130 // Finally, print the module on the output stream.
Chris Lattnere3c55a52007-05-06 05:40:41 +0000131 M->print(Out);
Reid Spencer96839be2006-11-30 16:50:26 +0000132
Reid Spencere7c3c602006-11-30 06:36:44 +0000133 } catch (const std::string& caught_message) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000134 cerr << argv[0] << ": " << caught_message << "\n";
Reid Spencere7c3c602006-11-30 06:36:44 +0000135 exitCode = 1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000136 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000137 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +0000138 exitCode = 1;
139 }
140
141 if (Out != &std::cout) delete Out;
142 return exitCode;
143}
144