blob: d3a60cfa82ac95e13ef9a599828402de704a5a3c [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"
24#include "llvm/Bytecode/Writer.h"
Reid Spencere7c3c602006-11-30 06:36:44 +000025#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000026#include "llvm/Support/ManagedStatic.h"
Reid Spencere7c3c602006-11-30 06:36:44 +000027#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>
33using namespace llvm;
34
35static cl::opt<std::string>
36InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
37
38static cl::opt<std::string>
39OutputFilename("o", cl::desc("Override output filename"),
Reid Spencer96839be2006-11-30 16:50:26 +000040 cl::value_desc("filename"), cl::init("-"));
Reid Spencere7c3c602006-11-30 06:36:44 +000041
42static cl::opt<bool>
Reid Spencer71d2ec92006-12-31 06:02:26 +000043Force("f", cl::desc("Overwrite output files"), cl::init(false));
44
45static cl::opt<bool>
46AddAttrs("add-attrs", cl::desc("Add function result and argument attributes"),
47 cl::init(false));
Reid Spencere7c3c602006-11-30 06:36:44 +000048
Reid Spencere77e35e2006-12-01 20:26:20 +000049static cl::opt<bool>
Reid Spencer950bf602007-01-26 08:19:09 +000050Debug("debug-upgrade-yacc", cl::desc("Print debug output from yacc parser"),
51 cl::Hidden, cl::init(false));
52
53static cl::opt<bool>
54EmitByteCode("emit-bytecode", cl::desc("Emit bytecode instead of assembly"),
Reid Spencere77e35e2006-12-01 20:26:20 +000055 cl::init(false));
56
Reid Spencere7c3c602006-11-30 06:36:44 +000057int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000058 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencere7c3c602006-11-30 06:36:44 +000059 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
60 sys::PrintStackTraceOnErrorSignal();
61
62 int exitCode = 0;
63 std::ostream *Out = 0;
Reid Spencer96839be2006-11-30 16:50:26 +000064 std::istream *In = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +000065 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 Wendlinge8156192006-12-07 01:30:32 +000070 cerr << argv[0] << ": error opening '" << OutputFilename
71 << "': file exists!\n"
72 << "Use -f command line argument to force output\n";
Reid Spencere7c3c602006-11-30 06:36:44 +000073 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 Wendlinge8156192006-12-07 01:30:32 +000097 cerr << argv[0] << ": error opening '" << OutputFilename
98 << "': file exists!\n"
99 << "Use -f command line argument to force output\n";
Reid Spencere7c3c602006-11-30 06:36:44 +0000100 return 1;
101 }
102
103 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
Reid Spencer96839be2006-11-30 16:50:26 +0000104 std::ios::trunc);
Reid Spencere7c3c602006-11-30 06:36:44 +0000105 // 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 Spencer96839be2006-11-30 16:50:26 +0000111 if (InputFilename == "-") {
112 In = &std::cin;
113 InputFilename = "<stdin>";
114 } else {
115 In = new std::ifstream(InputFilename.c_str());
116 }
117
Reid Spencere7c3c602006-11-30 06:36:44 +0000118 if (!Out->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000119 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Reid Spencere7c3c602006-11-30 06:36:44 +0000120 return 1;
121 }
122
Reid Spencer96839be2006-11-30 16:50:26 +0000123 if (!In->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000124 cerr << argv[0] << ": error opening " << InputFilename << "!\n";
Reid Spencer96839be2006-11-30 16:50:26 +0000125 return 1;
126 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000127
Reid Spencer950bf602007-01-26 08:19:09 +0000128 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 Spencer96839be2006-11-30 16:50:26 +0000142
Reid Spencere7c3c602006-11-30 06:36:44 +0000143 } catch (const std::string& caught_message) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000144 cerr << argv[0] << ": " << caught_message << "\n";
Reid Spencere7c3c602006-11-30 06:36:44 +0000145 exitCode = 1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000146 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000147 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Reid Spencere7c3c602006-11-30 06:36:44 +0000148 exitCode = 1;
149 }
150
151 if (Out != &std::cout) delete Out;
152 return exitCode;
153}
154