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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
use super::*;
use std::convert::TryFrom;
impl Module {
/// Encode this Wasm module into bytes.
pub fn to_bytes(&self) -> Vec<u8> {
self.encoded().finish()
}
fn encoded(&self) -> wasm_encoder::Module {
let mut module = wasm_encoder::Module::new();
self.encode_types(&mut module);
self.encode_imports(&mut module);
self.encode_funcs(&mut module);
self.encode_tables(&mut module);
self.encode_memories(&mut module);
self.encode_tags(&mut module);
self.encode_globals(&mut module);
self.encode_exports(&mut module);
self.encode_start(&mut module);
self.encode_elems(&mut module);
self.encode_data_count(&mut module);
self.encode_code(&mut module);
self.encode_data(&mut module);
module
}
fn encode_types(&self, module: &mut wasm_encoder::Module) {
if !self.should_encode_types {
return;
}
let mut section = wasm_encoder::TypeSection::new();
for ty in &self.types {
match ty {
Type::Func(ty) => {
section.function(ty.params.iter().cloned(), ty.results.iter().cloned());
}
}
}
module.section(§ion);
}
fn encode_imports(&self, module: &mut wasm_encoder::Module) {
if !self.should_encode_imports {
return;
}
let mut section = wasm_encoder::ImportSection::new();
for im in &self.imports {
section.import(
&im.module,
&im.field,
translate_entity_type(&im.entity_type),
);
}
module.section(§ion);
}
fn encode_tags(&self, module: &mut wasm_encoder::Module) {
if self.num_defined_tags == 0 {
return;
}
let mut tags = wasm_encoder::TagSection::new();
for tag in self.tags[self.tags.len() - self.num_defined_tags..].iter() {
tags.tag(wasm_encoder::TagType {
kind: wasm_encoder::TagKind::Exception,
func_type_idx: tag.func_type_idx,
});
}
module.section(&tags);
}
fn encode_funcs(&self, module: &mut wasm_encoder::Module) {
if self.num_defined_funcs == 0 {
return;
}
let mut funcs = wasm_encoder::FunctionSection::new();
for (ty, _) in self.funcs[self.funcs.len() - self.num_defined_funcs..].iter() {
funcs.function(*ty);
}
module.section(&funcs);
}
fn encode_tables(&self, module: &mut wasm_encoder::Module) {
if self.num_defined_tables == 0 {
return;
}
let mut tables = wasm_encoder::TableSection::new();
for t in self.tables[self.tables.len() - self.num_defined_tables..].iter() {
tables.table(*t);
}
module.section(&tables);
}
fn encode_memories(&self, module: &mut wasm_encoder::Module) {
if self.num_defined_memories == 0 {
return;
}
let mut mems = wasm_encoder::MemorySection::new();
for m in self.memories[self.memories.len() - self.num_defined_memories..].iter() {
mems.memory(*m);
}
module.section(&mems);
}
fn encode_globals(&self, module: &mut wasm_encoder::Module) {
if self.globals.is_empty() {
return;
}
let mut globals = wasm_encoder::GlobalSection::new();
for (idx, expr) in &self.defined_globals {
let ty = &self.globals[*idx as usize];
match expr {
GlobalInitExpr::ConstExpr(expr) => globals.global(*ty, expr),
GlobalInitExpr::FuncRef(func) => globals.global(*ty, &ConstExpr::ref_func(*func)),
};
}
module.section(&globals);
}
fn encode_exports(&self, module: &mut wasm_encoder::Module) {
if self.exports.is_empty() {
return;
}
let mut exports = wasm_encoder::ExportSection::new();
for (name, kind, idx) in &self.exports {
exports.export(name, *kind, *idx);
}
module.section(&exports);
}
fn encode_start(&self, module: &mut wasm_encoder::Module) {
if let Some(f) = self.start {
module.section(&wasm_encoder::StartSection { function_index: f });
}
}
fn encode_elems(&self, module: &mut wasm_encoder::Module) {
if self.elems.is_empty() {
return;
}
let mut elems = wasm_encoder::ElementSection::new();
let mut exps = vec![];
for el in &self.elems {
let elements = match &el.items {
Elements::Expressions(es) => {
exps.clear();
exps.extend(es.iter().map(|e| {
// TODO(nagisa): generate global.get of imported ref globals too.
match e {
Some(i) => match el.ty {
RefType::FUNCREF => wasm_encoder::ConstExpr::ref_func(*i),
_ => unreachable!(),
},
None => wasm_encoder::ConstExpr::ref_null(el.ty.heap_type),
}
}));
wasm_encoder::Elements::Expressions(&exps)
}
Elements::Functions(fs) => wasm_encoder::Elements::Functions(fs),
};
match &el.kind {
ElementKind::Active { table, offset } => {
let offset = match *offset {
Offset::Const32(n) => ConstExpr::i32_const(n),
Offset::Const64(n) => ConstExpr::i64_const(n),
Offset::Global(g) => ConstExpr::global_get(g),
};
elems.active(*table, &offset, el.ty, elements);
}
ElementKind::Passive => {
elems.passive(el.ty, elements);
}
ElementKind::Declared => {
elems.declared(el.ty, elements);
}
}
}
module.section(&elems);
}
fn encode_data_count(&self, module: &mut wasm_encoder::Module) {
// Without bulk memory there's no need for a data count section,
if !self.config.bulk_memory_enabled() {
return;
}
// ... and also if there's no data no need for a data count section.
if self.data.is_empty() {
return;
}
module.section(&wasm_encoder::DataCountSection {
count: u32::try_from(self.data.len()).unwrap(),
});
}
fn encode_code(&self, module: &mut wasm_encoder::Module) {
if self.code.is_empty() {
return;
}
let mut code = wasm_encoder::CodeSection::new();
for c in &self.code {
// Skip the run-length encoding because it is a little
// annoying to compute; use a length of one for every local.
let mut func = wasm_encoder::Function::new(c.locals.iter().map(|l| (1, *l)));
match &c.instructions {
Instructions::Generated(instrs) => {
for instr in instrs {
func.instruction(instr);
}
func.instruction(&wasm_encoder::Instruction::End);
}
Instructions::Arbitrary(body) => {
func.raw(body.iter().copied());
}
}
code.function(&func);
}
module.section(&code);
}
fn encode_data(&self, module: &mut wasm_encoder::Module) {
if self.data.is_empty() {
return;
}
let mut data = wasm_encoder::DataSection::new();
for seg in &self.data {
match &seg.kind {
DataSegmentKind::Active {
memory_index,
offset,
} => {
let offset = match *offset {
Offset::Const32(n) => ConstExpr::i32_const(n),
Offset::Const64(n) => ConstExpr::i64_const(n),
Offset::Global(g) => ConstExpr::global_get(g),
};
data.active(*memory_index, &offset, seg.init.iter().copied());
}
DataSegmentKind::Passive => {
data.passive(seg.init.iter().copied());
}
}
}
module.section(&data);
}
}
pub(crate) fn translate_entity_type(ty: &EntityType) -> wasm_encoder::EntityType {
match ty {
EntityType::Tag(t) => wasm_encoder::EntityType::Tag(wasm_encoder::TagType {
kind: wasm_encoder::TagKind::Exception,
func_type_idx: t.func_type_idx,
}),
EntityType::Func(f, _) => wasm_encoder::EntityType::Function(*f),
EntityType::Table(ty) => (*ty).into(),
EntityType::Memory(m) => (*m).into(),
EntityType::Global(g) => (*g).into(),
}
}
|