* llvm.spec.in: update blurb
[oota-llvm.git] / tools / llvm-upgrade / llvm-upgrade.cpp
1 //===--- 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 "UpgradeInternals.h"
23 #include "llvm/Module.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/ManagedStatic.h"
26 #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>
32 using namespace llvm;
33
34 static cl::opt<std::string>
35 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
36
37 static cl::opt<std::string>
38 OutputFilename("o", cl::desc("Override output filename"),
39                cl::value_desc("filename"), cl::init("-"));
40
41 static cl::opt<bool>
42 Force("f", cl::desc("Overwrite output files"), cl::init(false));
43
44 static cl::opt<bool>
45 AddAttrs("add-attrs", cl::desc("Add function result and argument attributes"),
46          cl::init(false));
47
48 static cl::opt<bool>
49 Debug("debug-upgrade-yacc", cl::desc("Print debug output from yacc parser"),
50       cl::Hidden, cl::init(false));
51
52 int main(int argc, char **argv) {
53   llvm_shutdown_obj X;  // Call llvm_shutdown() on exit.
54   cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n");
55   sys::PrintStackTraceOnErrorSignal();
56
57   int exitCode = 0;
58   std::ostream *Out = 0;
59   std::istream *In = 0;
60   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!
65           cerr << argv[0] << ": error opening '" << OutputFilename
66                << "': file exists!\n"
67                << "Use -f command line argument to force output\n";
68           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!
92           cerr << argv[0] << ": error opening '" << OutputFilename
93                << "': file exists!\n"
94                << "Use -f command line argument to force output\n";
95           return 1;
96         }
97
98         Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
99                                 std::ios::trunc);
100         // 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
106     if (InputFilename == "-") {
107       In = &std::cin;
108       InputFilename = "<stdin>";
109     } else {
110       In = new std::ifstream(InputFilename.c_str());
111     }
112
113     if (!Out->good()) {
114       cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
115       return 1;
116     }
117
118     if (!In->good()) {
119       cerr << argv[0] << ": error opening " << InputFilename << "!\n";
120       return 1;
121     }
122
123     Module *M = UpgradeAssembly(InputFilename, *In, Debug, AddAttrs);
124     if (!M) {
125       cerr << argv[0] << ": No module returned from assembly parsing\n";
126       *Out << argv[0] << ": parse failed.";
127       exit(1);
128     }
129
130     // Finally, print the module on the output stream.
131     M->print(Out);
132
133   } catch (const std::string& caught_message) {
134     cerr << argv[0] << ": " << caught_message << "\n";
135     exitCode = 1;
136   } catch (...) {
137     cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
138     exitCode = 1;
139   }
140
141   if (Out != &std::cout) delete Out;
142   return exitCode;
143 }
144