OLD | NEW |
1 /* Copyright 2013 The Native Client Authors. All rights reserved. | 1 /* Copyright 2013 The Native Client Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can | 2 * Use of this source code is governed by a BSD-style license that can |
3 * be found in the LICENSE file. | 3 * be found in the LICENSE file. |
4 */ | 4 */ |
5 | 5 |
6 //===-- pnacl-freeze.cpp - The low-level NaCl bitcode freezer --------===// | 6 //===-- pnacl-thaw.cpp - The low-level NaCl bitcode thawer ----------------===// |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // Generates NaCl pexe wire format. | 10 // Converts NaCl wire format back to LLVM bitcode. |
11 // | 11 // |
12 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
13 | 13 |
14 #include "llvm/IR/LLVMContext.h" | 14 #include "llvm/IR/LLVMContext.h" |
15 #include "llvm/Assembly/AssemblyAnnotationWriter.h" | |
16 // Note: We need the following to provide the API for calling the NaCl | 15 // Note: We need the following to provide the API for calling the NaCl |
17 // Bitcode Writer to generate the frozen file. | 16 // Bitcode Reader to read the frozen file. |
18 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" | 17 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" |
19 // Note: We need the following to provide the API for calling the (LLVM) | 18 // Note: We need the following to provide the API for calling the (LLVM) |
20 // Bitcode Reader to read in the corresonding pexe file to freeze. | 19 // Bitcode Writer to generate the corresponding LLVM bitcode file. |
21 #include "llvm/Bitcode/ReaderWriter.h" | 20 #include "llvm/Bitcode/ReaderWriter.h" |
22 #include "llvm/DebugInfo.h" | |
23 #include "llvm/IR/IntrinsicInst.h" | |
24 #include "llvm/IR/Module.h" | 21 #include "llvm/IR/Module.h" |
25 #include "llvm/IR/Type.h" | |
26 #include "llvm/Support/CommandLine.h" | 22 #include "llvm/Support/CommandLine.h" |
27 #include "llvm/Support/DataStream.h" | 23 #include "llvm/Support/DataStream.h" |
28 #include "llvm/Support/FormattedStream.h" | |
29 #include "llvm/Support/ManagedStatic.h" | 24 #include "llvm/Support/ManagedStatic.h" |
30 #include "llvm/Support/MemoryBuffer.h" | |
31 #include "llvm/Support/PrettyStackTrace.h" | 25 #include "llvm/Support/PrettyStackTrace.h" |
32 #include "llvm/Support/Signals.h" | 26 #include "llvm/Support/Signals.h" |
33 #include "llvm/Support/ToolOutputFile.h" | 27 #include "llvm/Support/ToolOutputFile.h" |
34 #include "llvm/Support/system_error.h" | |
35 | |
36 // llvm/Bitcode/BitstreamWriter.h | |
37 | 28 |
38 using namespace llvm; | 29 using namespace llvm; |
39 | 30 |
40 | |
41 static cl::opt<std::string> | 31 static cl::opt<std::string> |
42 OutputFilename("o", cl::desc("Specify output filename"), | 32 OutputFilename("o", cl::desc("Specify thawed pexe filename"), |
43 cl::value_desc("filename")); | 33 cl::value_desc("filename")); |
44 | 34 |
45 static cl::opt<std::string> | 35 static cl::opt<std::string> |
46 InputFilename(cl::Positional, cl::desc("<pexe file>"), cl::Required); | 36 InputFilename(cl::Positional, cl::desc("<frozen file>"), cl::Required); |
47 | 37 |
48 static void WriteOutputFile(const Module *M) { | 38 static void WriteOutputFile(const Module *M) { |
49 | 39 |
50 std::string FrozenFilename = | 40 std::string ThawedFilename = |
51 (OutputFilename.size() == 0 ? (InputFilename + ".frozen") : OutputFilename); | 41 (OutputFilename.size() == 0 ? (InputFilename + ".thawed") : OutputFilename); |
52 | 42 |
53 std::string ErrorInfo; | 43 std::string ErrorInfo; |
54 OwningPtr<tool_output_file> Out | 44 OwningPtr<tool_output_file> Out |
55 (new tool_output_file(FrozenFilename.c_str(), ErrorInfo, | 45 (new tool_output_file(ThawedFilename.c_str(), ErrorInfo, |
56 raw_fd_ostream::F_Binary)); | 46 raw_fd_ostream::F_Binary)); |
57 if (!ErrorInfo.empty()) { | 47 if (!ErrorInfo.empty()) { |
58 errs() << ErrorInfo << '\n'; | 48 errs() << ErrorInfo << '\n'; |
59 exit(1); | 49 exit(1); |
60 } | 50 } |
61 | 51 |
62 NaClWriteBitcodeToFile(M, Out->os()); | 52 WriteBitcodeToFile(M, Out->os()); |
63 | 53 |
64 // Declare success. | 54 // Declare success. |
65 Out->keep(); | 55 Out->keep(); |
66 } | 56 } |
67 | 57 |
68 int main(int argc, char **argv) { | 58 int main(int argc, char **argv) { |
69 // Print a stack trace if we signal out. | 59 // Print a stack trace if we signal out. |
70 sys::PrintStackTraceOnErrorSignal(); | 60 sys::PrintStackTraceOnErrorSignal(); |
71 PrettyStackTraceProgram X(argc, argv); | 61 PrettyStackTraceProgram X(argc, argv); |
72 | 62 |
73 LLVMContext &Context = getGlobalContext(); | 63 LLVMContext &Context = getGlobalContext(); |
74 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. | 64 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
75 | 65 |
76 cl::ParseCommandLineOptions(argc, argv, "Generates NaCl pexe wire format\n"); | 66 cl::ParseCommandLineOptions( |
| 67 argc, argv, "Converts NaCl pexe wire format into LLVM bitcode format\n"); |
77 | 68 |
78 std::string ErrorMessage; | 69 std::string ErrorMessage; |
79 std::auto_ptr<Module> M; | 70 std::auto_ptr<Module> M; |
80 | 71 |
81 // Use the bitcode streaming interface | 72 // Use the bitcode streaming interface |
82 DataStreamer *streamer = getDataFileStreamer(InputFilename, &ErrorMessage); | 73 DataStreamer *streamer = getDataFileStreamer(InputFilename, &ErrorMessage); |
83 if (streamer) { | 74 if (streamer) { |
84 std::string DisplayFilename = InputFilename; | 75 std::string DisplayFilename = InputFilename; |
85 M.reset(getStreamedBitcodeModule(DisplayFilename, streamer, Context, | 76 M.reset(getNaClStreamedBitcodeModule(DisplayFilename, streamer, Context, |
86 &ErrorMessage)); | 77 &ErrorMessage)); |
87 if(M.get() != 0 && M->MaterializeAllPermanently(&ErrorMessage)) { | 78 if(M.get() != 0 && M->MaterializeAllPermanently(&ErrorMessage)) { |
88 M.reset(); | 79 M.reset(); |
89 } | 80 } |
90 } | 81 } |
91 | 82 |
92 if (M.get() == 0) { | 83 if (M.get() == 0) { |
93 errs() << argv[0] << ": "; | 84 errs() << argv[0] << ": "; |
94 if (ErrorMessage.size()) | 85 if (ErrorMessage.size()) |
95 errs() << ErrorMessage << "\n"; | 86 errs() << ErrorMessage << "\n"; |
96 else | 87 else |
97 errs() << "bitcode didn't read correctly.\n"; | 88 errs() << "bitcode didn't read correctly.\n"; |
98 return 1; | 89 return 1; |
99 } | 90 } |
100 | 91 |
101 WriteOutputFile(M.get()); | 92 WriteOutputFile(M.get()); |
102 return 0; | 93 return 0; |
103 } | 94 } |
OLD | NEW |