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 *>
64 getNaClLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
65                          LLVMContext &Context,
66                          DiagnosticHandlerFunction DiagnosticHandler = nullptr,
67                          bool AcceptSupportedOnly = true);
68 
69 /// Read the header of the specified stream and prepare for lazy
70 /// deserialization and streaming of function bodies. On error,
71 /// this returns null, and fills in *ErrMsg with an error description
72 /// if ErrMsg is non-null.
73 ///
74 /// See getNaClLazyBitcodeModule for an explanation of argument
75 /// AcceptSupportedOnly.
76 /// TODO(kschimpf): Refactor this and getStreamedBitcodeModule to use
77 /// ErrorOr<Module *> API so that all methods have the same interface.
78 Module *getNaClStreamedBitcodeModule(
79     const std::string &name, StreamingMemoryObject *streamer,
80     LLVMContext &Context, 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 ///
isNaClBitcode(const unsigned char * BufPtr,const unsigned char * BufEnd)117 inline bool isNaClBitcode(const unsigned char *BufPtr,
118                           const unsigned char *BufEnd) {
119   return BufPtr + 4 <= BufEnd && BufPtr[0] == 'P' && BufPtr[1] == 'E' &&
120          BufPtr[2] == 'X' && BufPtr[3] == 'E';
121 }
122 
123 /// NaClWriteHeader - Generate a default header (using the version
124 /// number defined by kPNaClVersion) and write to the corresponding
125 /// bitcode stream.
126 void NaClWriteHeader(NaClBitstreamWriter &Stream, bool AcceptSupportedOnly);
127 
128 // NaClWriteHeader - Write the contents of the bitcode header to the
129 // corresponding bitcode stream.
130 void NaClWriteHeader(const NaClBitcodeHeader &Header,
131                      NaClBitstreamWriter &Stream);
132 
133 /// NaClObjDump - Read PNaCl bitcode file from input, and print a
134 /// textual representation of its contents. NoRecords and NoAssembly
135 /// define what should not be included in the dump.
136 bool NaClObjDump(MemoryBufferRef Input, raw_ostream &output, bool NoRecords,
137                  bool NoAssembly);
138 
139 } // namespace llvm
140 #endif
141