1 //===-- llvm/Bitcode/NaCl/NaClReaderWriter.h - ------------------*- C++ -*-===// 2 // NaCl Bitcode reader/writer. 3 // 4 // The LLVM Compiler Infrastructure 5 // 6 // This file is distributed under the University of Illinois Open Source 7 // License. See LICENSE.TXT for details. 8 // 9 //===----------------------------------------------------------------------===// 10 // 11 // This header defines interfaces to read and write NaCl bitcode wire format 12 // files. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_BITCODE_NACL_NACLREADERWRITER_H 17 #define LLVM_BITCODE_NACL_NACLREADERWRITER_H 18 19 #include "llvm/IR/DiagnosticInfo.h" 20 #include "llvm/Support/CommandLine.h" 21 #include "llvm/Support/ErrorOr.h" 22 #include "llvm/Support/MemoryBuffer.h" 23 24 #include <string> 25 26 namespace llvm { 27 class LLVMContext; 28 class Module; 29 class NaClBitcodeHeader; 30 class NaClBitstreamWriter; 31 class StreamingMemoryObject; 32 class raw_ostream; 33 34 /// Defines the data layout used for PNaCl bitcode files. We set the 35 /// data layout of the module in the bitcode readers rather than in 36 /// pnacl-llc so that 'opt' will also use the correct data layout if 37 /// it is run on a pexe. 38 extern const char *PNaClDataLayout; 39 40 /// Allows (function) local symbol tables (unsupported) in PNaCl bitcode 41 /// files. 42 extern cl::opt<bool> PNaClAllowLocalSymbolTables; 43 44 /// \brief Defines the integer bit size used to model pointers in PNaCl. 45 static const unsigned PNaClIntPtrTypeBitSize = 32; 46 47 /// Diagnostic handler that redirects error diagnostics to the given stream. 48 DiagnosticHandlerFunction redirectNaClDiagnosticToStream(raw_ostream &Out); 49 50 /// Read the header of the specified bitcode buffer and prepare for lazy 51 /// deserialization of function bodies. If successful, this takes ownership 52 /// of 'Buffer' (extending its lifetime). On error, this returns an error 53 /// code and deletes Buffer. 54 /// 55 /// The AcceptSupportedOnly argument is used to decide which PNaCl versions 56 /// of the PNaCl bitcode to accept. There are three forms: 57 /// 1) Readable and supported. 58 /// 2) Readable and unsupported. Allows testing of code before becoming 59 /// supported, as well as running experiments on the bitcode format. 60 /// 3) Unreadable. 61 /// When AcceptSupportedOnly is true, only form 1 is allowed. When 62 /// AcceptSupportedOnly is false, forms 1 and 2 are allowed. 63 ErrorOr<Module *> getNaClLazyBitcodeModule( 64 std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context, 65 DiagnosticHandlerFunction DiagnosticHandler = nullptr, 66 bool AcceptSupportedOnly = true); 67 68 /// Read the header of the specified stream and prepare for lazy 69 /// deserialization and streaming of function bodies. On error, 70 /// this returns null, and fills in *ErrMsg with an error description 71 /// if ErrMsg is non-null. 72 /// 73 /// See getNaClLazyBitcodeModule for an explanation of argument 74 /// AcceptSupportedOnly. 75 /// TODO(kschimpf): Refactor this and getStreamedBitcodeModule to use 76 /// ErrorOr<Module *> API so that all methods have the same interface. 77 Module *getNaClStreamedBitcodeModule( 78 const std::string &name, StreamingMemoryObject *streamer, 79 LLVMContext &Context, 80 DiagnosticHandlerFunction DiagnosticHandler = nullptr, 81 std::string *ErrMsg = nullptr, bool AcceptSupportedOnly = true); 82 83 /// Read the bitcode file from a buffer, returning the module. 84 /// 85 /// See getNaClLazyBitcodeModule for an explanation of argument 86 /// AcceptSupportedOnly. 87 ErrorOr<Module *> 88 NaClParseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, 89 DiagnosticHandlerFunction DiagnosticHandler = nullptr, 90 bool AcceptSupportedOnly = true); 91 92 /// Read the textual bitcode records in Filename, returning the module. 93 /// Note: If Filename is "-", stdin will be read. 94 /// 95 /// TODO(kschimpf) Replace Verbose argument with a DiagnosticHandlerFunction. 96 ErrorOr<Module *> parseNaClBitcodeText(const std::string &Filename, 97 LLVMContext &Context, 98 raw_ostream *Verbose = nullptr); 99 100 /// Write the specified module to the specified raw output stream, using 101 /// PNaCl wire format. For streams where it matters, the given stream 102 /// should be in "binary" mode. 103 /// 104 /// The AcceptSupportedOnly argument is used to decide which PNaCl versions 105 /// of the PNaCl bitcode to generate. There are two forms: 106 /// 1) Writable and supported. 107 /// 2) Writable and unsupported. Allows testing of code before becoming 108 /// supported, as well as running experiments on the bitcode format. 109 /// When AcceptSupportedOnly is true, only form 1 is allowed. When 110 /// AcceptSupportedOnly is false, forms 1 and 2 are allowed. 111 void NaClWriteBitcodeToFile(const Module *M, raw_ostream &Out, 112 bool AcceptSupportedOnly = true); 113 114 /// isNaClBitcode - Return true if the given bytes are the magic bytes for 115 /// PNaCl bitcode wire format. 116 /// 117 inline bool isNaClBitcode(const unsigned char *BufPtr, 118 const unsigned char *BufEnd) { 119 return BufPtr+4 <= BufEnd && 120 BufPtr[0] == 'P' && 121 BufPtr[1] == 'E' && 122 BufPtr[2] == 'X' && 123 BufPtr[3] == 'E'; 124 } 125 126 /// NaClWriteHeader - Generate a default header (using the version 127 /// number defined by kPNaClVersion) and write to the corresponding 128 /// bitcode stream. 129 void NaClWriteHeader(NaClBitstreamWriter &Stream, bool AcceptSupportedOnly); 130 131 // NaClWriteHeader - Write the contents of the bitcode header to the 132 // corresponding bitcode stream. 133 void NaClWriteHeader(const NaClBitcodeHeader &Header, 134 NaClBitstreamWriter &Stream); 135 136 /// NaClObjDump - Read PNaCl bitcode file from input, and print a 137 /// textual representation of its contents. NoRecords and NoAssembly 138 /// define what should not be included in the dump. 139 bool NaClObjDump(MemoryBufferRef Input, raw_ostream &output, 140 bool NoRecords, bool NoAssembly); 141 142 } // end llvm namespace 143 #endif 144