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
use crate::emit::{Emit, EmitContext, Section};
use crate::map::IdHashSet;
use crate::parse::IndicesToIds;
use crate::tombstone_arena::{Id, Tombstone, TombstoneArena};
use crate::{Data, ImportId, Module, Result};
pub type MemoryId = Id<Memory>;
#[derive(Debug)]
pub struct Memory {
id: MemoryId,
pub shared: bool,
pub initial: u32,
pub maximum: Option<u32>,
pub import: Option<ImportId>,
pub data_segments: IdHashSet<Data>,
}
impl Tombstone for Memory {
fn on_delete(&mut self) {
self.data_segments = Default::default();
}
}
impl Memory {
pub fn id(&self) -> MemoryId {
self.id
}
}
impl Emit for Memory {
fn emit(&self, cx: &mut EmitContext) {
if let Some(max) = self.maximum {
cx.encoder.byte(if self.shared { 0x03 } else { 0x01 });
cx.encoder.u32(self.initial);
cx.encoder.u32(max);
} else {
cx.encoder.byte(0x00);
cx.encoder.u32(self.initial);
}
}
}
#[derive(Debug, Default)]
pub struct ModuleMemories {
arena: TombstoneArena<Memory>,
}
impl ModuleMemories {
pub fn add_import(
&mut self,
shared: bool,
initial: u32,
maximum: Option<u32>,
import: ImportId,
) -> MemoryId {
let id = self.arena.next_id();
let id2 = self.arena.alloc(Memory {
id,
shared,
initial,
maximum,
import: Some(import),
data_segments: Default::default(),
});
debug_assert_eq!(id, id2);
id
}
pub fn add_local(&mut self, shared: bool, initial: u32, maximum: Option<u32>) -> MemoryId {
let id = self.arena.next_id();
let id2 = self.arena.alloc(Memory {
id,
shared,
initial,
maximum,
import: None,
data_segments: Default::default(),
});
debug_assert_eq!(id, id2);
id
}
pub fn get(&self, id: MemoryId) -> &Memory {
&self.arena[id]
}
pub fn get_mut(&mut self, id: MemoryId) -> &mut Memory {
&mut self.arena[id]
}
pub fn delete(&mut self, id: MemoryId) {
self.arena.delete(id);
}
pub fn iter(&self) -> impl Iterator<Item = &Memory> {
self.arena.iter().map(|(_, f)| f)
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Memory> {
self.arena.iter_mut().map(|(_, f)| f)
}
}
impl Module {
pub(crate) fn parse_memories(
&mut self,
section: wasmparser::MemorySectionReader,
ids: &mut IndicesToIds,
) -> Result<()> {
log::debug!("parse memory section");
for m in section {
let m = m?;
let id = self
.memories
.add_local(m.shared, m.limits.initial, m.limits.maximum);
ids.push_memory(id);
}
Ok(())
}
}
impl Emit for ModuleMemories {
fn emit(&self, cx: &mut EmitContext) {
log::debug!("emit memory section");
let memories = self.iter().filter(|m| m.import.is_none()).count();
if memories == 0 {
return;
}
let mut cx = cx.start_section(Section::Memory);
cx.encoder.usize(memories);
for memory in self.iter().filter(|m| m.import.is_none()) {
cx.indices.push_memory(memory.id());
memory.emit(&mut cx);
}
}
}