1 /*
2  * Copyright 2017 WebAssembly Community Group participants
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //
18 // Abstracts reading and writing, supporting both text and binary.
19 //
20 
21 #ifndef wasm_wasm_io_h
22 #define wasm_wasm_io_h
23 
24 #include "parsing.h"
25 #include "support/file.h"
26 #include "wasm.h"
27 
28 namespace wasm {
29 
30 class ModuleReader {
31 public:
32   // If DWARF support is enabled, we track the locations of all IR nodes in
33   // the binary, so that we can update DWARF sections later when writing.
setDWARF(bool DWARF_)34   void setDWARF(bool DWARF_) { DWARF = DWARF_; }
35 
setProfile(IRProfile profile_)36   void setProfile(IRProfile profile_) { profile = profile_; }
37 
38   // read text
39   void readText(std::string filename, Module& wasm);
40   // read binary
41   void readBinary(std::string filename,
42                   Module& wasm,
43                   std::string sourceMapFilename = "");
44   // read text or binary, checking the contents for what it is. If `filename` is
45   // empty, read from stdin.
46   void
47   read(std::string filename, Module& wasm, std::string sourceMapFilename = "");
48   // check whether a file is a wasm binary
49   bool isBinaryFile(std::string filename);
50 
51 private:
52   bool DWARF = false;
53 
54   IRProfile profile = IRProfile::Normal;
55 
56   void readStdin(Module& wasm, std::string sourceMapFilename);
57 
58   void readBinaryData(std::vector<char>& input,
59                       Module& wasm,
60                       std::string sourceMapFilename);
61 };
62 
63 class ModuleWriter {
64   bool binary = true;
65   bool debugInfo = false;
66   std::string symbolMap;
67   std::string sourceMapFilename;
68   std::string sourceMapUrl;
69 
70 public:
setBinary(bool binary_)71   void setBinary(bool binary_) { binary = binary_; }
setDebugInfo(bool debugInfo_)72   void setDebugInfo(bool debugInfo_) { debugInfo = debugInfo_; }
setSymbolMap(std::string symbolMap_)73   void setSymbolMap(std::string symbolMap_) { symbolMap = symbolMap_; }
setSourceMapFilename(std::string sourceMapFilename_)74   void setSourceMapFilename(std::string sourceMapFilename_) {
75     sourceMapFilename = sourceMapFilename_;
76   }
setSourceMapUrl(std::string sourceMapUrl_)77   void setSourceMapUrl(std::string sourceMapUrl_) {
78     sourceMapUrl = sourceMapUrl_;
79   }
80 
81   // write text
82   void writeText(Module& wasm, Output& output);
83   void writeText(Module& wasm, std::string filename);
84   // write binary
85   void writeBinary(Module& wasm, Output& output);
86   void writeBinary(Module& wasm, std::string filename);
87   // write text or binary, defaulting to binary unless setBinary(false),
88   // and unless there is no output file (in which case we write text
89   // to stdout).
90   void write(Module& wasm, Output& output);
91   void write(Module& wasm, std::string filename);
92 };
93 
94 } // namespace wasm
95 
96 #endif // wasm_wasm_io_h
97