-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.h
139 lines (126 loc) · 3.97 KB
/
module.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef SRC_MODULE_H_
#define SRC_MODULE_H_
#include <string>
#include <fstream>
#include <vector>
#include "decoder.h"
#define MAGIC_NUMBER std::vector<uint8_t>({0x00, 0x61, 0x73, 0x6d})
#define WASM_VERSION std::vector<uint8_t>({0x01, 0x00, 0x00, 0x00})
#define WVM_KEEPALIVE __attribute__((used))
#define SET_STRUCT_DISABLE_COPY_CONSTUCT(TypeName) \
TypeName(const TypeName &) = delete; \
TypeName &operator=(const TypeName &) = delete
#define SET_STRUCT_MOVE_ONLY(TypeName) \
TypeName() = default; \
TypeName(TypeName &&) noexcept = default; \
TypeName &operator=(TypeName &&) noexcept = default; \
SET_STRUCT_DISABLE_COPY_CONSTUCT(TypeName);
namespace wvm
{
enum class Section : uint8_t
{
custom = 0,
type = 1,
import = 2,
func = 3,
table = 4,
memory = 5,
global = 6,
export_section = 7,
start = 8,
element = 9,
code = 10,
data = 11
};
enum class SectionType : uint8_t
{
func = 0x60,
result = 0x40
};
struct TableType
{
uint8_t refType;
uint32_t initial;
uint32_t maximum;
};
struct MemType
{
uint32_t initial;
uint32_t maximum;
};
struct GlobalType
{
uint8_t valType;
bool mutability;
};
using type_seq_t = std::vector<uint8_t>;
using external_kind_t = std::variant<uint32_t, TableType, MemType, GlobalType>;
using func_type_t = std::pair<type_seq_t, type_seq_t>;
struct FuncDefSec
{
SET_STRUCT_MOVE_ONLY(FuncDefSec)
std::vector<uint8_t> locals;
std::vector<uint8_t> body;
FuncDefSec(std::vector<uint8_t> &locals, std::vector<uint8_t> &body)
: locals(locals), body(body) {}
};
struct ImportSec
{
SET_STRUCT_MOVE_ONLY(ImportSec)
std::string modName;
std::string name;
uint8_t extKind;
external_kind_t extMeta;
ImportSec(std::string modName, std::string name, uint8_t extKind)
: modName(modName), name(name), extKind(extKind) {}
};
struct GlobalSec
{
SET_STRUCT_MOVE_ONLY(GlobalSec)
GlobalType globalType;
std::vector<uint8_t> initOpCodes; // Used in instantiation stage.
GlobalSec(uint8_t valType, bool mutability, std::vector<uint8_t> &initOpCodes)
: globalType({valType, mutability}), initOpCodes(initOpCodes) {}
};
struct ExportSec
{
SET_STRUCT_MOVE_ONLY(ExportSec)
std::string name;
uint8_t extKind;
uint32_t extIdx;
ExportSec(std::string name, uint8_t extKind, uint32_t extIdx)
: name(name), extKind(extKind), extIdx(extIdx) {}
};
class Module
{
private:
std::shared_ptr<Decoder> decoder_;
public:
Module(std::shared_ptr<Decoder> decoder);
~Module();
std::vector<func_type_t> funcTypes;
std::vector<ImportSec> imports;
std::vector<uint32_t> funcTypesIndices;
std::vector<ExportSec> exports;
std::vector<FuncDefSec> funcDefs;
std::vector<GlobalSec> globals;
std::shared_ptr<Decoder> decoder();
void parseMagicNumber();
void parseVersion();
void parseSection();
void parseTypeSection();
void parseImportSection();
void parseFunctionSection();
void parseTableSection();
void parseMemorySection();
void parseGlobalSection();
void parseExportSection();
void parseCodeSection();
static bool byteArrayEq(const std::vector<uint8_t> &input1, const std::vector<uint8_t> &input2);
};
using shared_module_t = std::shared_ptr<Module>;
using type_seq_t = std::vector<uint8_t>;
using external_kind_t = std::variant<uint32_t, TableType, MemType, GlobalType>; // the first one is func idx.
using func_type_t = std::pair<type_seq_t, type_seq_t>;
}
#endif // SRC_MODULE_H_