/src/wasm-tools/crates/wasmparser/src/arity.rs
Line | Count | Source |
1 | | /* Copyright 2024 Mozilla Foundation |
2 | | * |
3 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | * you may not use this file except in compliance with the License. |
5 | | * You may obtain a copy of the License at |
6 | | * |
7 | | * http://www.apache.org/licenses/LICENSE-2.0 |
8 | | * |
9 | | * Unless required by applicable law or agreed to in writing, software |
10 | | * distributed under the License is distributed on an "AS IS" BASIS, |
11 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | * See the License for the specific language governing permissions and |
13 | | * limitations under the License. |
14 | | */ |
15 | | |
16 | | use crate::prelude::*; |
17 | | use crate::{ |
18 | | BlockType, BrTable, CompositeInnerType, ContType, FrameKind, FuncType, Operator, RefType, |
19 | | ResumeTable, SubType, TryTable, ValType, |
20 | | }; |
21 | | |
22 | | /// To compute the arity (param and result counts) of "variable-arity" |
23 | | /// operators, the operator_arity macro needs information about the |
24 | | /// module's types and the current control stack. The ModuleArity |
25 | | /// trait exposes this information. |
26 | | pub trait ModuleArity { |
27 | | /// Type with given index |
28 | | fn sub_type_at(&self, type_idx: u32) -> Option<&SubType>; |
29 | | |
30 | | /// Arity (param and result counts) of tag with given index |
31 | | fn tag_type_arity(&self, at: u32) -> Option<(u32, u32)>; |
32 | | |
33 | | /// Type index of function with given index |
34 | | fn type_index_of_function(&self, function_idx: u32) -> Option<u32>; |
35 | | |
36 | | /// Function type for a given continuation type |
37 | | fn func_type_of_cont_type(&self, c: &ContType) -> Option<&FuncType>; |
38 | | |
39 | | /// Sub type for a given reference type |
40 | | fn sub_type_of_ref_type(&self, rt: &RefType) -> Option<&SubType>; |
41 | | |
42 | | /// Current height of control stack |
43 | | fn control_stack_height(&self) -> u32; |
44 | | |
45 | | /// BlockType and FrameKind of label with given index |
46 | | fn label_block(&self, depth: u32) -> Option<(BlockType, FrameKind)>; |
47 | | |
48 | | /// Computes arity of given SubType |
49 | 116k | fn sub_type_arity(&self, t: &SubType) -> Option<(u32, u32)> { |
50 | 116k | match &t.composite_type.inner { |
51 | 115k | CompositeInnerType::Func(f) => { |
52 | 115k | Some((f.params().len() as u32, f.results().len() as u32)) |
53 | | } |
54 | 1.29k | CompositeInnerType::Struct(s) => Some((s.fields.len() as u32, s.fields.len() as u32)), |
55 | 0 | CompositeInnerType::Array(_) => None, |
56 | 0 | CompositeInnerType::Cont(c) => { |
57 | 0 | let f = self.func_type_of_cont_type(c)?; |
58 | 0 | Some((f.params().len() as u32, f.results().len() as u32)) |
59 | | } |
60 | | } |
61 | 116k | } <wasmprinter::operator::PrintOperatorFolded as wasmparser::arity::ModuleArity>::sub_type_arity Line | Count | Source | 49 | 116k | fn sub_type_arity(&self, t: &SubType) -> Option<(u32, u32)> { | 50 | 116k | match &t.composite_type.inner { | 51 | 115k | CompositeInnerType::Func(f) => { | 52 | 115k | Some((f.params().len() as u32, f.results().len() as u32)) | 53 | | } | 54 | 1.29k | CompositeInnerType::Struct(s) => Some((s.fields.len() as u32, s.fields.len() as u32)), | 55 | 0 | CompositeInnerType::Array(_) => None, | 56 | 0 | CompositeInnerType::Cont(c) => { | 57 | 0 | let f = self.func_type_of_cont_type(c)?; | 58 | 0 | Some((f.params().len() as u32, f.results().len() as u32)) | 59 | | } | 60 | | } | 61 | 116k | } |
Unexecuted instantiation: <_ as wasmparser::arity::ModuleArity>::sub_type_arity |
62 | | |
63 | | /// Computes arity of given BlockType |
64 | 724k | fn block_type_arity(&self, ty: BlockType) -> Option<(u32, u32)> { |
65 | 724k | match ty { |
66 | 433k | BlockType::Empty => Some((0, 0)), |
67 | 201k | BlockType::Type(_) => Some((0, 1)), |
68 | 89.5k | BlockType::FuncType(t) => self.sub_type_arity(self.sub_type_at(t)?), |
69 | | } |
70 | 724k | } <wasmprinter::operator::PrintOperatorFolded as wasmparser::arity::ModuleArity>::block_type_arity Line | Count | Source | 64 | 724k | fn block_type_arity(&self, ty: BlockType) -> Option<(u32, u32)> { | 65 | 724k | match ty { | 66 | 433k | BlockType::Empty => Some((0, 0)), | 67 | 201k | BlockType::Type(_) => Some((0, 1)), | 68 | 89.5k | BlockType::FuncType(t) => self.sub_type_arity(self.sub_type_at(t)?), | 69 | | } | 70 | 724k | } |
Unexecuted instantiation: <_ as wasmparser::arity::ModuleArity>::block_type_arity |
71 | | } |
72 | | |
73 | | impl Operator<'_> { |
74 | | /// Compute the arity (param and result counts) of the operator, given |
75 | | /// an impl ModuleArity, which stores the necessary module state. |
76 | 4.84M | pub fn operator_arity(&self, module: &impl ModuleArity) -> Option<(u32, u32)> { |
77 | | #[cfg_attr(not(feature = "simd"), allow(unused_macro_rules))] |
78 | | macro_rules! operator_arity { |
79 | | ($visit:ident $args:tt arity $a:tt -> $b:tt) => (Some(($a, $b))); |
80 | | ($visit:ident { $memarg:ident $($rest:ident)* } arity memarg $a:tt -> $b:tt) => { |
81 | | Some(($a + memarg_extra_operands(&$memarg), $b)) |
82 | | }; |
83 | | ($visit:ident { $($args:ident)* } arity custom) => ($visit(module, $($args),*)); |
84 | | } |
85 | | macro_rules! define_arity { |
86 | | ($(@$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*) )*) => ( |
87 | | match self.clone() { |
88 | | $( |
89 | | Operator::$op $({ $($arg),* })? => { |
90 | | $( |
91 | | $(let _ = $arg;)* |
92 | | )? |
93 | | operator_arity!($visit {$( $($arg)* )?} $($ann)*) |
94 | | } |
95 | | )* |
96 | | } |
97 | | ); |
98 | | } |
99 | 4.84M | crate::for_each_operator!(define_arity) |
100 | 4.84M | } <wasmparser::readers::core::operators::Operator>::operator_arity::<wasmprinter::operator::PrintOperatorFolded> Line | Count | Source | 76 | 4.84M | pub fn operator_arity(&self, module: &impl ModuleArity) -> Option<(u32, u32)> { | 77 | | #[cfg_attr(not(feature = "simd"), allow(unused_macro_rules))] | 78 | | macro_rules! operator_arity { | 79 | | ($visit:ident $args:tt arity $a:tt -> $b:tt) => (Some(($a, $b))); | 80 | | ($visit:ident { $memarg:ident $($rest:ident)* } arity memarg $a:tt -> $b:tt) => { | 81 | | Some(($a + memarg_extra_operands(&$memarg), $b)) | 82 | | }; | 83 | | ($visit:ident { $($args:ident)* } arity custom) => ($visit(module, $($args),*)); | 84 | | } | 85 | | macro_rules! define_arity { | 86 | | ($(@$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*) )*) => ( | 87 | | match self.clone() { | 88 | | $( | 89 | | Operator::$op $({ $($arg),* })? => { | 90 | | $( | 91 | | $(let _ = $arg;)* | 92 | | )? | 93 | | operator_arity!($visit {$( $($arg)* )?} $($ann)*) | 94 | | } | 95 | | )* | 96 | | } | 97 | | ); | 98 | | } | 99 | 4.84M | crate::for_each_operator!(define_arity) | 100 | 4.84M | } |
Unexecuted instantiation: <wasmparser::readers::core::operators::Operator>::operator_arity::<_> |
101 | | } |
102 | | |
103 | 51.7k | fn memarg_extra_operands(memarg: &crate::MemArg) -> u32 { |
104 | 51.7k | match memarg.target { |
105 | 51.3k | crate::MemArgTarget::Memory(_) => 0, |
106 | 344 | crate::MemArgTarget::Array(_) => 1, |
107 | | } |
108 | 51.7k | } |
109 | | |
110 | 243k | fn visit_block(module: &dyn ModuleArity, block: BlockType) -> Option<(u32, u32)> { |
111 | 243k | let (params, _) = module.block_type_arity(block)?; |
112 | 243k | Some((params, params)) |
113 | 243k | } |
114 | | |
115 | 14.4k | fn visit_loop(module: &dyn ModuleArity, block: BlockType) -> Option<(u32, u32)> { |
116 | 14.4k | visit_block(module, block) |
117 | 14.4k | } |
118 | | |
119 | 44.6k | fn visit_if(module: &dyn ModuleArity, block: BlockType) -> Option<(u32, u32)> { |
120 | 44.6k | let (params, results) = visit_block(module, block)?; |
121 | 44.6k | Some((params + 1, results)) |
122 | 44.6k | } |
123 | | |
124 | 3.70k | fn visit_else(module: &dyn ModuleArity) -> Option<(u32, u32)> { |
125 | 3.70k | let (ty, _kind) = module.label_block(0)?; |
126 | 3.70k | let (params, results) = module.block_type_arity(ty)?; |
127 | 3.70k | Some((results, params)) |
128 | 3.70k | } |
129 | | |
130 | 276k | fn visit_end(module: &dyn ModuleArity) -> Option<(u32, u32)> { |
131 | 276k | let (ty, _kind) = module.label_block(0)?; |
132 | 276k | let (_params, results) = module.block_type_arity(ty)?; |
133 | 276k | Some((results, results)) |
134 | 276k | } |
135 | | |
136 | 167k | fn visit_br(module: &dyn ModuleArity, depth: u32) -> Option<(u32, u32)> { |
137 | 167k | let (ty, kind) = module.label_block(depth)?; |
138 | 167k | let (params, results) = module.block_type_arity(ty)?; |
139 | 167k | let n = match kind { |
140 | 11.8k | FrameKind::Loop => params, |
141 | 155k | _ => results, |
142 | | }; |
143 | 167k | Some((n, 0)) |
144 | 167k | } |
145 | | |
146 | 84.8k | fn visit_br_if(module: &dyn ModuleArity, depth: u32) -> Option<(u32, u32)> { |
147 | 84.8k | let (params, _) = visit_br(module, depth)?; |
148 | 84.8k | Some((params + 1, params)) |
149 | 84.8k | } |
150 | | |
151 | 1.89k | fn visit_br_table(module: &dyn ModuleArity, table: BrTable<'_>) -> Option<(u32, u32)> { |
152 | 1.89k | let (params, results) = visit_br(module, table.default())?; |
153 | 1.89k | Some((params + 1, results)) |
154 | 1.89k | } |
155 | | |
156 | 1.10k | fn visit_return(module: &dyn ModuleArity) -> Option<(u32, u32)> { |
157 | 1.10k | let height = module.control_stack_height().checked_sub(1)?; |
158 | 1.10k | let (ty, _) = module.label_block(height)?; |
159 | 1.10k | let (_, results) = module.block_type_arity(ty)?; |
160 | 1.10k | Some((results, 0)) |
161 | 1.10k | } |
162 | | |
163 | 21.2k | fn visit_call(module: &dyn ModuleArity, func: u32) -> Option<(u32, u32)> { |
164 | 21.2k | module.sub_type_arity(module.sub_type_at(module.type_index_of_function(func)?)?) |
165 | 21.2k | } |
166 | | |
167 | 31 | fn visit_call_indirect(module: &dyn ModuleArity, ty: u32, _table: u32) -> Option<(u32, u32)> { |
168 | 31 | let (params, results) = module.sub_type_arity(module.sub_type_at(ty)?)?; |
169 | 31 | Some((params + 1, results)) |
170 | 31 | } |
171 | | |
172 | 1.29k | fn visit_struct_new(module: &dyn ModuleArity, ty: u32) -> Option<(u32, u32)> { |
173 | 1.29k | let ty = module.sub_type_at(ty)?; |
174 | 1.29k | let (params, _results) = module.sub_type_arity(ty)?; |
175 | 1.29k | Some((params, 1)) |
176 | 1.29k | } |
177 | | |
178 | 0 | fn visit_struct_new_desc(module: &dyn ModuleArity, ty: u32) -> Option<(u32, u32)> { |
179 | 0 | let ty = module.sub_type_at(ty)?; |
180 | 0 | let (params, _results) = module.sub_type_arity(ty)?; |
181 | 0 | Some((params + 1, 1)) |
182 | 0 | } |
183 | | |
184 | 2.75k | fn visit_array_new_fixed(_module: &dyn ModuleArity, _ty: u32, size: u32) -> Option<(u32, u32)> { |
185 | 2.75k | Some((size, 1)) |
186 | 2.75k | } |
187 | | |
188 | 61 | fn visit_br_on_cast( |
189 | 61 | module: &dyn ModuleArity, |
190 | 61 | depth: u32, |
191 | 61 | _from: RefType, |
192 | 61 | _to: RefType, |
193 | 61 | ) -> Option<(u32, u32)> { |
194 | 61 | let (params, _) = visit_br(module, depth)?; |
195 | 61 | Some((params, params)) |
196 | 61 | } |
197 | | |
198 | 67 | fn visit_br_on_cast_fail( |
199 | 67 | module: &dyn ModuleArity, |
200 | 67 | depth: u32, |
201 | 67 | _from: RefType, |
202 | 67 | _to: RefType, |
203 | 67 | ) -> Option<(u32, u32)> { |
204 | 67 | let (params, _) = visit_br(module, depth)?; |
205 | 67 | Some((params, params)) |
206 | 67 | } |
207 | | |
208 | 0 | fn visit_typed_select_multi(_module: &dyn ModuleArity, tys: Vec<ValType>) -> Option<(u32, u32)> { |
209 | 0 | let len = u32::try_from(tys.len()).unwrap(); |
210 | 0 | Some((1 + 2 * len, len)) |
211 | 0 | } |
212 | | |
213 | 2.15k | fn visit_return_call(module: &dyn ModuleArity, func: u32) -> Option<(u32, u32)> { |
214 | 2.15k | let (params, _) = visit_call(module, func)?; |
215 | 2.15k | Some((params, 0)) |
216 | 2.15k | } |
217 | | |
218 | 9 | fn visit_return_call_indirect(module: &dyn ModuleArity, ty: u32, table: u32) -> Option<(u32, u32)> { |
219 | 9 | let (params, _) = visit_call_indirect(module, ty, table)?; |
220 | 9 | Some((params, 0)) |
221 | 9 | } |
222 | | |
223 | 32.9k | fn visit_try_table(module: &dyn ModuleArity, table: TryTable) -> Option<(u32, u32)> { |
224 | 32.9k | let (params, _) = module.block_type_arity(table.ty)?; |
225 | 32.9k | Some((params, params)) |
226 | 32.9k | } |
227 | | |
228 | 4.37k | fn visit_throw(module: &dyn ModuleArity, tag: u32) -> Option<(u32, u32)> { |
229 | 4.37k | let (params, _) = module.tag_type_arity(tag)?; |
230 | 4.37k | Some((params, 0)) |
231 | 4.37k | } |
232 | | |
233 | 0 | fn visit_try(module: &dyn ModuleArity, ty: BlockType) -> Option<(u32, u32)> { |
234 | 0 | visit_block(module, ty) |
235 | 0 | } |
236 | | |
237 | 0 | fn visit_catch(module: &dyn ModuleArity, tag: u32) -> Option<(u32, u32)> { |
238 | 0 | let (params, _) = visit_end(module)?; |
239 | 0 | let (tag_params, _) = module.tag_type_arity(tag)?; |
240 | 0 | Some((params, tag_params)) |
241 | 0 | } |
242 | | |
243 | 0 | fn visit_delegate(module: &dyn ModuleArity, _depth: u32) -> Option<(u32, u32)> { |
244 | 0 | visit_end(module) |
245 | 0 | } |
246 | | |
247 | 0 | fn visit_catch_all(module: &dyn ModuleArity) -> Option<(u32, u32)> { |
248 | 0 | let (params, _) = visit_end(module)?; |
249 | 0 | Some((params, 0)) |
250 | 0 | } |
251 | | |
252 | 8 | fn visit_call_ref(module: &dyn ModuleArity, ty: u32) -> Option<(u32, u32)> { |
253 | 8 | let (params, results) = module.sub_type_arity(module.sub_type_at(ty)?)?; |
254 | 7 | Some((params + 1, results)) |
255 | 8 | } |
256 | | |
257 | 4 | fn visit_return_call_ref(module: &dyn ModuleArity, ty: u32) -> Option<(u32, u32)> { |
258 | 4 | let (params, _) = visit_call_ref(module, ty)?; |
259 | 4 | Some((params, 0)) |
260 | 4 | } |
261 | | |
262 | 3.27k | fn visit_br_on_null(module: &dyn ModuleArity, depth: u32) -> Option<(u32, u32)> { |
263 | 3.27k | let (params, _results) = visit_br(module, depth)?; |
264 | 3.27k | Some((params + 1, params + 1)) |
265 | 3.27k | } |
266 | | |
267 | 85 | fn visit_br_on_non_null(module: &dyn ModuleArity, depth: u32) -> Option<(u32, u32)> { |
268 | 85 | let (params, _results) = visit_br(module, depth)?; |
269 | 85 | Some((params, params.checked_sub(1)?)) |
270 | 85 | } |
271 | | |
272 | 0 | fn visit_cont_bind(module: &dyn ModuleArity, arg: u32, result: u32) -> Option<(u32, u32)> { |
273 | 0 | let (arg_params, _) = module.sub_type_arity(module.sub_type_at(arg)?)?; |
274 | 0 | let (result_params, _) = module.sub_type_arity(module.sub_type_at(result)?)?; |
275 | 0 | Some((arg_params.checked_sub(result_params)? + 1, 1)) |
276 | 0 | } |
277 | | |
278 | 0 | fn visit_suspend(module: &dyn ModuleArity, tag: u32) -> Option<(u32, u32)> { |
279 | 0 | module.tag_type_arity(tag) |
280 | 0 | } |
281 | | |
282 | 0 | fn visit_resume(module: &dyn ModuleArity, cont: u32, _table: ResumeTable) -> Option<(u32, u32)> { |
283 | 0 | let (params, results) = module.sub_type_arity(module.sub_type_at(cont)?)?; |
284 | 0 | Some((params + 1, results)) |
285 | 0 | } |
286 | | |
287 | 0 | fn visit_resume_throw( |
288 | 0 | module: &dyn ModuleArity, |
289 | 0 | cont: u32, |
290 | 0 | tag: u32, |
291 | 0 | _table: ResumeTable, |
292 | 0 | ) -> Option<(u32, u32)> { |
293 | 0 | let (params, _) = module.tag_type_arity(tag)?; |
294 | 0 | let (_, results) = module.sub_type_arity(module.sub_type_at(cont)?)?; |
295 | 0 | Some((params + 1, results)) |
296 | 0 | } |
297 | | |
298 | 0 | fn visit_resume_throw_ref( |
299 | 0 | module: &dyn ModuleArity, |
300 | 0 | cont: u32, |
301 | 0 | _table: ResumeTable, |
302 | 0 | ) -> Option<(u32, u32)> { |
303 | 0 | let (_, results) = module.sub_type_arity(module.sub_type_at(cont)?)?; |
304 | | // in: [exnref, cont], out: [result(cont)] |
305 | 0 | Some((2, results)) |
306 | 0 | } |
307 | | |
308 | 0 | fn visit_switch(module: &dyn ModuleArity, cont: u32, _tag: u32) -> Option<(u32, u32)> { |
309 | 0 | let (params, _) = module.sub_type_arity(module.sub_type_at(cont)?)?; |
310 | 0 | let st = &module.sub_type_at(cont)?.composite_type.inner; |
311 | 0 | let CompositeInnerType::Cont(ct) = &st else { |
312 | 0 | return None; |
313 | | }; |
314 | 0 | let last_param = module.func_type_of_cont_type(ct)?.params().last()?; |
315 | 0 | let (cont_params, _) = |
316 | 0 | module.sub_type_arity(module.sub_type_of_ref_type(&last_param.as_reference_type()?)?)?; |
317 | 0 | Some((params, cont_params)) |
318 | 0 | } |
319 | | |
320 | 0 | fn visit_br_on_cast_desc_eq( |
321 | 0 | module: &dyn ModuleArity, |
322 | 0 | depth: u32, |
323 | 0 | _from: RefType, |
324 | 0 | _to: RefType, |
325 | 0 | ) -> Option<(u32, u32)> { |
326 | 0 | let (params, _) = visit_br(module, depth)?; |
327 | 0 | Some((params + 1, params)) |
328 | 0 | } |
329 | | |
330 | 0 | fn visit_br_on_cast_desc_eq_fail( |
331 | 0 | module: &dyn ModuleArity, |
332 | 0 | depth: u32, |
333 | 0 | _from: RefType, |
334 | 0 | _to: RefType, |
335 | 0 | ) -> Option<(u32, u32)> { |
336 | 0 | let (params, _) = visit_br(module, depth)?; |
337 | 0 | Some((params + 1, params)) |
338 | 0 | } |