-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_plan.rs
More file actions
311 lines (293 loc) · 10 KB
/
Copy pathdata_plan.rs
File metadata and controls
311 lines (293 loc) · 10 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
use std::collections::HashMap;
use wasm_encoder::{ConstExpr, DataSection};
use crate::{
ast::{Program, SettingFileFilter, SettingKind, StateSource},
memory::MemoryLayouts,
signature::parse_signature,
wasm_ir,
};
use super::memory_plan::{LinearMemoryLayout, ScratchRequirements};
use super::reachability::Reachability;
use super::{dependencies::BackendDependencies, runtime_helpers::float_format};
pub(super) struct StaticData {
pub strings: StringPool,
pub signatures: SignaturePool,
pub float_format: Option<FloatFormatData>,
layout: LinearMemoryLayout,
}
pub(super) struct FloatFormatData {
pub pow10_significands: i32,
bytes: Vec<u8>,
}
pub(super) struct StringPool {
base: u32,
bytes: Vec<u8>,
entries: HashMap<String, (u32, u32)>,
}
#[derive(Clone, Copy)]
pub(super) struct SignatureEntry {
pub needle: u32,
pub mask: u32,
pub len: u32,
}
#[derive(Default)]
pub(super) struct SignaturePool {
base: u32,
bytes: Vec<u8>,
entries: HashMap<String, SignatureEntry>,
}
impl StaticData {
pub fn collect(
program: &Program,
process_names: &[&str],
automatic_shape: Option<&crate::shape_selection::ShapeSelectionPlan>,
wasm_ir: &wasm_ir::Program,
reachability: &Reachability,
memory: &MemoryLayouts,
dependencies: &BackendDependencies,
) -> Self {
let state = program.state.as_ref().expect("checked programs have state");
let mut strings = StringPool::new();
for process in process_names {
strings.intern(process);
}
if let Some(plan) = automatic_shape {
let report = plan.failure_report(program);
for message in report.messages() {
strings.intern(message);
}
}
for field in state.all_fields() {
if let StateSource::Pointer(path) = &field.source
&& let crate::ast::PointerPathBase::Module { name, .. } = &path.base
{
strings.intern(name);
}
}
for setting in &program.settings {
strings.intern(setting.runtime_key());
strings.intern(&setting.description);
if let Some(tooltip) = &setting.tooltip {
strings.intern(tooltip);
}
match &setting.kind {
SettingKind::Text { default, .. } => {
strings.intern(default);
}
SettingKind::Choice { options, .. } => {
for option in options {
strings.intern(&option.variant);
strings.intern(&option.description);
}
}
SettingKind::File { filters, .. } => {
for filter in filters {
match filter {
SettingFileFilter::Name {
description,
pattern,
} => {
if let Some(description) = description {
strings.intern(description);
}
strings.intern(pattern);
}
SettingFileFilter::Mime { value: mime, .. } => {
strings.intern(mime);
}
}
}
}
SettingKind::Bool { .. } | SettingKind::Title { .. } => {}
}
}
for expression in wasm_ir.expressions() {
if !reachability.contains_expression(expression.id) {
continue;
}
match &expression.kind {
wasm_ir::ExpressionKind::String(value) => {
strings.intern(value);
}
wasm_ir::ExpressionKind::InterpolatedString(parts) => {
for part in parts {
if let wasm_ir::InterpolatedPart::Text(value) = part {
strings.intern(value);
}
}
}
_ => {}
}
}
let mut signatures = SignaturePool::new();
for expression in wasm_ir.expressions() {
if !reachability.contains_expression(expression.id) {
continue;
}
if let wasm_ir::ExpressionKind::Signature(signature) = &expression.kind {
signatures.intern(signature);
}
}
let float_format_bytes = if dependencies.uses_float_format() {
float_format::pow10_significands_bytes()
} else {
Vec::new()
};
let static_data_len = strings
.bytes
.len()
.checked_add(signatures.bytes.len())
.and_then(|length| length.checked_add(float_format_bytes.len()))
.expect("static data length must fit the host address space");
let layout = LinearMemoryLayout::plan(
static_data_len,
ScratchRequirements {
// Word-swapped emulator storage may need one leading byte and
// one trailing byte while normalizing an unaligned guest read
// in place before the shared decoder consumes it.
abi_read_capacity: memory.maximum_size().saturating_add(2).max(
if dependencies.needs_native_pointer_size() {
64
} else {
16
},
),
maximum_signature_len: signatures.maximum_len(),
},
);
strings.base = layout.static_data_start();
signatures.base = strings
.base
.checked_add(
u32::try_from(strings.bytes.len())
.expect("string data must fit WebAssembly linear memory"),
)
.expect("string data must fit WebAssembly linear memory");
let float_format = (!float_format_bytes.is_empty()).then(|| FloatFormatData {
pow10_significands: i32::try_from(signatures.base as usize + signatures.bytes.len())
.expect("float-format data address must fit wasm32"),
bytes: float_format_bytes,
});
Self {
strings,
signatures,
float_format,
layout,
}
}
pub fn layout(&self) -> LinearMemoryLayout {
self.layout
}
pub fn encode(&self) -> DataSection {
debug_assert_eq!(
self.layout.static_data_end(),
u64::from(self.signatures.base)
+ self.signatures.bytes.len() as u64
+ self
.float_format
.as_ref()
.map_or(0, |float_format| float_format.bytes.len() as u64)
);
let mut section = DataSection::new();
if !self.strings.bytes.is_empty() {
section.active(
0,
&ConstExpr::i32_const(self.strings.base as i32),
self.strings.bytes.iter().copied(),
);
}
if !self.signatures.bytes.is_empty() {
section.active(
0,
&ConstExpr::i32_const(self.signatures.base as i32),
self.signatures.bytes.iter().copied(),
);
}
if let Some(float_format) = &self.float_format {
section.active(
0,
&ConstExpr::i32_const(float_format.pow10_significands),
float_format.bytes.iter().copied(),
);
}
section
}
}
impl SignaturePool {
fn new() -> Self {
Self::default()
}
fn intern(&mut self, signature: &str) {
if self.entries.contains_key(signature) {
return;
}
let (needle, mask) = parse_signature(signature).expect("signatures were type checked");
let needle_ptr = self.base + self.bytes.len() as u32;
self.bytes.extend_from_slice(&needle);
let mask_ptr = self.base + self.bytes.len() as u32;
self.bytes.extend_from_slice(&mask);
self.entries.insert(
signature.to_owned(),
SignatureEntry {
needle: needle_ptr,
mask: mask_ptr,
len: needle.len() as u32,
},
);
}
pub fn get(&self, signature: &str) -> SignatureEntry {
let entry = self.entries[signature];
SignatureEntry {
needle: self
.base
.checked_add(entry.needle)
.expect("signature address must fit wasm32"),
mask: self
.base
.checked_add(entry.mask)
.expect("signature address must fit wasm32"),
len: entry.len,
}
}
fn maximum_len(&self) -> u32 {
self.entries
.values()
.map(|entry| entry.len)
.max()
.unwrap_or(0)
}
}
impl StringPool {
fn new() -> Self {
Self {
base: 0,
bytes: Vec::new(),
entries: HashMap::new(),
}
}
fn intern(&mut self, value: &str) -> (u32, u32) {
if let Some(entry) = self.entries.get(value) {
return *entry;
}
let offset = self
.base
.checked_add(
u32::try_from(self.bytes.len())
.expect("string data must fit WebAssembly linear memory"),
)
.expect("string data must fit WebAssembly linear memory");
let len = value.len() as u32;
self.bytes.extend_from_slice(value.as_bytes());
self.entries.insert(value.to_owned(), (offset, len));
(offset, len)
}
pub fn get(&self, value: &str) -> (u32, u32) {
let (offset, len) = self.entries[value];
(
self.base
.checked_add(offset)
.expect("string address must fit wasm32"),
len,
)
}
}