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/Bytecode/Writer.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/ManagedStatic.h"
27 #include "llvm/Support/Streams.h"
28 #include "llvm/Support/SystemUtils.h"
29 #include "llvm/System/Signals.h"
35 static cl::opt<std::string>
36 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
38 static cl::opt<std::string>
39 OutputFilename("o", cl::desc("Override output filename"),
40 cl::value_desc("filename"), cl::init("-"));
43 Force("f", cl::desc("Overwrite output files"), cl::init(false));
46 AddAttrs("add-attrs", cl::desc("Add function result and argument attributes"),
50 Debug("debug-upgrade-yacc", cl::desc("Print debug output from yacc parser"),
51 cl::Hidden, cl::init(false));
54 EmitByteCode("emit-bytecode", cl::desc("Emit bytecode instead of assembly"),
57 int main(int argc, char **argv) {
58 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
59 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
60 sys::PrintStackTraceOnErrorSignal();
63 std::ostream *Out = 0;
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!
70 cerr << argv[0] << ": error opening '" << OutputFilename
71 << "': file exists!\n"
72 << "Use -f command line argument to force output\n";
75 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
77 } else { // Specified stdout
81 if (InputFilename == "-") {
85 std::string IFN = InputFilename;
86 int Len = IFN.length();
87 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
89 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
91 OutputFilename = IFN; // Append to it
93 OutputFilename += ".llu";
95 if (!Force && std::ifstream(OutputFilename.c_str())) {
96 // If force is not specified, make sure not to overwrite a file!
97 cerr << argv[0] << ": error opening '" << OutputFilename
98 << "': file exists!\n"
99 << "Use -f command line argument to force output\n";
103 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
105 // Make sure that the Out file gets unlinked from the disk if we get a
107 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
111 if (InputFilename == "-") {
113 InputFilename = "<stdin>";
115 In = new std::ifstream(InputFilename.c_str());
119 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
124 cerr << argv[0] << ": error opening " << InputFilename << "!\n";
128 Module *M = UpgradeAssembly(InputFilename, *In, Debug, AddAttrs);
130 cerr << argv[0] << ": No module returned from assembly parsing\n";
132 *Out << argv[0] << ": parse failed.";
136 // Finally, print the module on the output stream.
139 WriteBytecodeToFile(M, OS);
143 } catch (const std::string& caught_message) {
144 cerr << argv[0] << ": " << caught_message << "\n";
147 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
151 if (Out != &std::cout) delete Out;