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..dc2d01ef51d2718887e3972c1ecef478174d8e1d |
| --- /dev/null |
| +++ b/tools/pnacl-freeze/pnacl-freeze.cpp |
| @@ -0,0 +1,105 @@ |
| +/* Copyright 2013 The Native Client Authors. All rights reserved. |
| + * Use of this source code is governed by a BSD-style license that can |
| + * be found in the LICENSE file. |
| + */ |
| + |
| +//===-- pnacl-freeze.cpp - The low-level PNaCl bitcode freezer --------===// |
| +// |
| +//===----------------------------------------------------------------------===// |
| +// |
| +// Generates PNaCl pexe wire format. |
| +// |
| +// This utility may be invoked in the following manner: |
| +// pnacl-freeze [options] <pexe file> |
| + |
| +// Options: |
| +// -help - Output information about command line switches |
|
jvoung (off chromium)
2013/04/25 17:42:45
Probably don't need to put the commandline usage h
Karl
2013/04/25 20:48:17
Done.
Karl
2013/04/25 20:48:17
Done.
|
| +// -o=<filename> - Specify output filename |
| +// |
| +//===----------------------------------------------------------------------===// |
| + |
| +#include "llvm/IR/LLVMContext.h" |
| +#include "llvm/Assembly/AssemblyAnnotationWriter.h" |
| +#include "llvm/Bitcode/NaCl/NaClReaderWriter.h" |
| +#include "llvm/Bitcode/ReaderWriter.h" |
|
jvoung (off chromium)
2013/04/25 17:42:45
Perhaps add a TODO that it should only use one of
Karl
2013/04/25 20:48:17
This can't be done. We need stuff in ReaderWriter
|
| +#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")); |
|
jvoung (off chromium)
2013/04/25 17:42:45
80 col
Karl
2013/04/25 20:48:17
Done.
|
| + |
| +static cl::opt<std::string> |
| +InputFilename(cl::Positional, cl::desc("<pexe file>"), cl::Required); |
| + |
| +static void WriteOutputFile(const Module *M) { |
| + |
| + std::string FrozenFilename = |
| + (OutputFilename.size() == 0 ? (InputFilename + ".frozen") : OutputFilename); |
|
jvoung (off chromium)
2013/04/25 17:42:45
Does this mean we can't do "pnacl-freeze input.pex
Karl
2013/04/25 20:48:17
Note that pnacl-thaw is only for command line usag
jvoung (off chromium)
2013/04/25 22:25:36
Ok, just wondering if handling that like the other
|
| + |
| + std::string ErrorInfo; |
| + OwningPtr<tool_output_file> Out |
| + (new tool_output_file(FrozenFilename.c_str(), ErrorInfo, |
|
jvoung (off chromium)
2013/04/25 17:42:45
is the style supposed to indent the (new...)?
Karl
2013/04/25 20:48:17
I followed the style from llvm-as (i.e. I copied l
|
| + 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, "Generates PNaCl pexe wire format\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; |
| +} |