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