Chromium Code Reviews| Index: tools/pnacl-freeze/pnacl-freeze.cpp |
| diff --git a/tools/pnacl-freeze/pnacl-freeze.cpp b/tools/pnacl-freeze/pnacl-freeze.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..21599fc8ad4bb179360ad41c747e75393aa5e17b |
| --- /dev/null |
| +++ b/tools/pnacl-freeze/pnacl-freeze.cpp |
| @@ -0,0 +1,97 @@ |
| +//===-- pnacl-freeze.cpp - The low-level PNaCl bitcode freezer --------===// |
| +// |
| +//===----------------------------------------------------------------------===// |
| +// |
| +// This utility may be invoked in the following manner: |
| +// llvm-compress [Options] x.bc - read x.bc and generate compressed y.bcc |
| +// |
| +// Options: |
| +// --help - Output information about command line switches |
| +// |
|
Karl
2013/04/24 18:25:59
Cleaned up comments describing command.
|
| +//===----------------------------------------------------------------------===// |
| + |
| +#include "llvm/IR/LLVMContext.h" |
| +#include "llvm/Assembly/AssemblyAnnotationWriter.h" |
| +#include "llvm/Bitcode/PNaCl/PNaClReaderWriter.h" |
| +#include "llvm/Bitcode/ReaderWriter.h" |
| +#include "llvm/DebugInfo.h" |
| +#include "llvm/IR/IntrinsicInst.h" |
| +#include "llvm/IR/Module.h" |
| +#include "llvm/IR/Type.h" |
| +#include "llvm/Support/CommandLine.h" |
| +#include "llvm/Support/DataStream.h" |
| +#include "llvm/Support/FormattedStream.h" |
| +#include "llvm/Support/ManagedStatic.h" |
| +#include "llvm/Support/MemoryBuffer.h" |
| +#include "llvm/Support/PrettyStackTrace.h" |
| +#include "llvm/Support/Signals.h" |
| +#include "llvm/Support/ToolOutputFile.h" |
| +#include "llvm/Support/system_error.h" |
| + |
| +// llvm/Bitcode/BitstreamWriter.h |
| + |
| +using namespace llvm; |
| + |
| + |
| +static cl::opt<std::string> |
| +OutputFilename("o", cl::desc("Specify output filename"), cl::value_desc("filename")); |
| + |
| +static cl::opt<std::string> |
| +InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::Required); |
| + |
| +static void WriteOutputFile(const Module *M) { |
| + |
| + std::string FrozenFilename = |
| + (OutputFilename.size() == 0 ? (InputFilename + ".frozen") : OutputFilename); |
| + |
| + std::string ErrorInfo; |
| + OwningPtr<tool_output_file> Out |
| + (new tool_output_file(FrozenFilename.c_str(), ErrorInfo, |
| + raw_fd_ostream::F_Binary)); |
| + if (!ErrorInfo.empty()) { |
| + errs() << ErrorInfo << '\n'; |
| + exit(1); |
| + } |
| + |
| + PNaClWriteBitcodeToFile(M, Out->os()); |
| + |
| + // Declare success. |
| + Out->keep(); |
| +} |
| + |
| +int main(int argc, char **argv) { |
| + // Print a stack trace if we signal out. |
| + sys::PrintStackTraceOnErrorSignal(); |
| + PrettyStackTraceProgram X(argc, argv); |
| + |
| + LLVMContext &Context = getGlobalContext(); |
| + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| + |
| + cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .bc compressor\n"); |
| + |
| + std::string ErrorMessage; |
| + std::auto_ptr<Module> M; |
| + |
| + // Use the bitcode streaming interface |
| + DataStreamer *streamer = getDataFileStreamer(InputFilename, &ErrorMessage); |
| + if (streamer) { |
| + std::string DisplayFilename = InputFilename; |
| + M.reset(getStreamedBitcodeModule(DisplayFilename, streamer, Context, |
| + &ErrorMessage)); |
| + if(M.get() != 0 && M->MaterializeAllPermanently(&ErrorMessage)) { |
| + M.reset(); |
| + } |
| + } |
| + |
| + if (M.get() == 0) { |
| + errs() << argv[0] << ": "; |
| + if (ErrorMessage.size()) |
| + errs() << ErrorMessage << "\n"; |
| + else |
| + errs() << "bitcode didn't read correctly.\n"; |
| + return 1; |
| + } |
| + |
| + WriteOutputFile(M.get()); |
| + return 0; |
| +} |