blob: 65ee452e1c9fb6180cca6e1bbfc78c807cb21a7d [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>
41Force("f", cl::desc("Overwrite output files"));
42
Reid Spencere77e35e2006-12-01 20:26:20 +000043static cl::opt<bool>
44Debug("debug", cl::desc("Print debug output from yacc parser"),cl::Hidden,
45 cl::init(false));
46
Reid Spencere7c3c602006-11-30 06:36:44 +000047int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000048 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencere7c3c602006-11-30 06:36:44 +000049 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
50 sys::PrintStackTraceOnErrorSignal();
51
52 int exitCode = 0;
53 std::ostream *Out = 0;
Reid Spencer96839be2006-11-30 16:50:26 +000054 std::istream *In = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +000055 try {
56 if (OutputFilename != "") { // Specified an output filename?
57 if (OutputFilename != "-") { // Not stdout?
58 if (!Force && std::ifstream(OutputFilename.c_str())) {
59 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000060 cerr << argv[0] << ": error opening '" << OutputFilename
61 << "': file exists!\n"
62 << "Use -f command line argument to force output\n";
Reid Spencere7c3c602006-11-30 06:36:44 +000063 return 1;
64 }
65 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
66 std::ios::trunc);
67 } else { // Specified stdout
68 Out = &std::cout;
69 }
70 } else {
71 if (InputFilename == "-") {
72 OutputFilename = "-";
73 Out = &std::cout;
74 } else {
75 std::string IFN = InputFilename;
76 int Len = IFN.length();
77 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
78 // Source ends in .ll
79 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
80 } else {
81 OutputFilename = IFN; // Append to it
82 }
83 OutputFilename += ".llu";
84
85 if (!Force && std::ifstream(OutputFilename.c_str())) {
86 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000087 cerr << argv[0] << ": error opening '" << OutputFilename
88 << "': file exists!\n"
89 << "Use -f command line argument to force output\n";
Reid Spencere7c3c602006-11-30 06:36:44 +000090 return 1;
91 }
92
93 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
Reid Spencer96839be2006-11-30 16:50:26 +000094 std::ios::trunc);
Reid Spencere7c3c602006-11-30 06:36:44 +000095 // Make sure that the Out file gets unlinked from the disk if we get a
96 // SIGINT
97 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
98 }
99 }
100
Reid Spencer96839be2006-11-30 16:50:26 +0000101 if (InputFilename == "-") {
102 In = &std::cin;
103 InputFilename = "<stdin>";
104 } else {
105 In = new std::ifstream(InputFilename.c_str());
106 }
107
Reid Spencere7c3c602006-11-30 06:36:44 +0000108 if (!Out->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000109 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Reid Spencere7c3c602006-11-30 06:36:44 +0000110 return 1;
111 }
112
Reid Spencer96839be2006-11-30 16:50:26 +0000113 if (!In->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000114 cerr << argv[0] << ": error opening " << InputFilename << "!\n";
Reid Spencer96839be2006-11-30 16:50:26 +0000115 return 1;
116 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000117
Reid Spencere77e35e2006-12-01 20:26:20 +0000118 UpgradeAssembly(InputFilename, *In, *Out, Debug);
Reid Spencer96839be2006-11-30 16:50:26 +0000119
Reid Spencere7c3c602006-11-30 06:36:44 +0000120 } catch (const std::string& caught_message) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000121 cerr << argv[0] << ": " << caught_message << "\n";
Reid Spencere7c3c602006-11-30 06:36:44 +0000122 exitCode = 1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000123 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000124 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +0000125 exitCode = 1;
126 }
127
128 if (Out != &std::cout) delete Out;
129 return exitCode;
130}
131