1;;! multi_memory = true
2;;! component_model_fixed_length_lists = true
3
4;; This contains two components which exercise fixed-size-list
5;; types. The first acts as a roundtrip, the second calls the first
6;; and compares the results.
7
8;; As this was written with C++ and wit-bindgen the functions
9;; are not expected to be easily human readable.
10
11;; The exported function run() from the second component
12;; calls nested-roundtrip([[1, 2], [3, 4]], [[-1, -2], [-3, -4]])
13;; from the first module and compares the resulting tuple with a
14;; concatenation of the inputs.
15
16;; Every mismatch increases the return value by 1.
17
18(component
19  (component (;0;)
20    (type $ty-test:fixed-size-lists/to-test (;0;)
21      (instance
22        (type (;0;) (list u32 2))
23        (type (;1;) (list 0 2))
24        (type (;2;) (list s32 2))
25        (type (;3;) (list 2 2))
26        (type (;4;) (tuple 1 3))
27        (type (;5;) (func (param "a" 1) (param "b" 3) (result 4)))
28        (export (;0;) "nested-roundtrip" (func (type 5)))
29      )
30    )
31    (import "test:fixed-size-lists/to-test" (instance $test:fixed-size-lists/to-test (;0;) (type $ty-test:fixed-size-lists/to-test)))
32    (core module $main (;0;)
33      (type (;0;) (func (param i32 i32 i32 i32 i32 i32 i32 i32 i32)))
34      (type (;1;) (func (result i32)))
35      (import "test:fixed-size-lists/to-test" "nested-roundtrip" (func (;0;) (type 0)))
36      (memory (;0;) 2)
37      (global (;0;) (mut i32) (i32.const 67232))
38      (export "memory" (memory 0))
39      ;; test runner, pass the values from the address 1024ff in registers
40      ;; to the roundtrip function and
41      ;; return the number of non-matching results
42      (export "run" (func 1))
43      (func (;1;) (type 1) (result i32)
44        (local i32 i32 i32 i32 i32 i32 i32 i32 i32)
45        (global.set 0
46          (local.tee 0
47            (i32.add
48              (global.get 0)
49              (i32.const -64))))
50        (i64.store
51          (i32.add
52            (local.tee 1
53              (i32.add
54                (local.get 0)
55                (i32.const 16)))
56            (i32.const 8))
57          (i64.load align=4
58            (i32.const 1032)))
59        (i64.store
60          (i32.add
61            (local.get 0)
62            (i32.const 8))
63          (i64.load align=4
64            (i32.const 1048)))
65        (i64.store offset=16
66          (local.get 0)
67          (i64.load align=4
68            (i32.const 1024)))
69        (i64.store
70          (local.get 0)
71          (i64.load align=4
72            (i32.const 1040)))
73        (global.set 0
74          (local.tee 2
75            (i32.sub
76              (global.get 0)
77              (i32.const 32))))
78        (call 0
79          (i32.load
80            (local.get 1))
81          (i32.load offset=4
82            (local.get 1))
83          (i32.load offset=8
84            (local.get 1))
85          (i32.load offset=12
86            (local.get 1))
87          (i32.load
88            (local.get 0))
89          (i32.load offset=4
90            (local.get 0))
91          (i32.load offset=8
92            (local.get 0))
93          (i32.load offset=12
94            (local.get 0))
95          (local.get 2))
96        (i64.store offset=24 align=4
97          (local.tee 1
98            (i32.add
99              (local.get 0)
100              (i32.const 32)))
101          (i64.load offset=24
102            (local.get 2)))
103        (i64.store offset=16 align=4
104          (local.get 1)
105          (i64.load offset=16
106            (local.get 2)))
107        (i64.store offset=8 align=4
108          (local.get 1)
109          (i64.load offset=8
110            (local.get 2)))
111        (i64.store align=4
112          (local.get 1)
113          (i64.load
114            (local.get 2)))
115        (global.set 0
116          (i32.add
117            (local.get 2)
118            (i32.const 32)))
119        (local.set 2
120          (i32.load offset=48
121            (local.get 0)))
122        (i32.add
123          (i32.add
124            (i32.add
125              (i32.add
126                (i32.add
127                  (i32.add
128                    (i32.add
129                      (i32.ne
130                        (i32.load offset=32
131                          (local.get 0))
132                        (local.set 3
133                          (i32.load offset=36
134                            (local.get 0)))
135                        (local.set 4
136                          (i32.load offset=52
137                            (local.get 0)))
138                        (local.set 5
139                          (i32.load offset=40
140                            (local.get 0)))
141                        (local.set 6
142                          (i32.load offset=56
143                            (local.get 0)))
144                        (local.set 7
145                          (i32.load offset=44
146                            (local.get 0)))
147                        (local.set 8
148                          (i32.load offset=60
149                            (local.get 0)))
150                        ;; here the output values are checked
151                        (global.set 0
152                          (i32.sub
153                            (local.get 0)
154                            (i32.const -64)))
155                        (i32.const 1))
156                      (i32.ne
157                        (local.get 2)
158                        (i32.const -1)))
159                    (i32.ne
160                      (local.get 3)
161                      (i32.const 2)))
162                  (i32.ne
163                    (local.get 4)
164                    (i32.const -2)))
165                (i32.ne
166                  (local.get 5)
167                  (i32.const 3)))
168              (i32.ne
169                (local.get 6)
170                (i32.const -3)))
171            (i32.ne
172              (local.get 7)
173              (i32.const 4)))
174          (i32.ne
175            (local.get 8)
176            (i32.const -4)))
177      )
178      ;; here are the input values
179      (data (;0;) (i32.const 1024) "\01\00\00\00\02\00\00\00\03\00\00\00\04\00\00\00\ff\ff\ff\ff\fe\ff\ff\ff\fd\ff\ff\ff\fc\ff\ff\ff")
180      (data (;1;) (i32.const 1056) "\ff\ff\ff\ff\00\00\02")
181      (@producers
182        (processed-by "wit-component" "0.243.0")
183      )
184    )
185    (core module $wit-component-shim-module (;1;)
186      (type (;0;) (func (param i32 i32 i32 i32 i32 i32 i32 i32 i32)))
187      (table (;0;) 1 1 funcref)
188      (export "0" (func 0))
189      (export "$imports" (table 0))
190      (func (;0;) (type 0) (param i32 i32 i32 i32 i32 i32 i32 i32 i32)
191        (call_indirect (type 0)
192          (local.get 0)
193          (local.get 1)
194          (local.get 2)
195          (local.get 3)
196          (local.get 4)
197          (local.get 5)
198          (local.get 6)
199          (local.get 7)
200          (local.get 8)
201          (i32.const 0))
202      )
203      (@producers
204        (processed-by "wit-component" "0.243.0")
205      )
206    )
207    (core module $wit-component-fixup (;2;)
208      (type (;0;) (func (param i32 i32 i32 i32 i32 i32 i32 i32 i32)))
209      (import "" "0" (func (;0;) (type 0)))
210      (import "" "$imports" (table (;0;) 1 1 funcref))
211      (elem (;0;) (i32.const 0) func 0)
212      (@producers
213        (processed-by "wit-component" "0.243.0")
214      )
215    )
216    (core instance $wit-component-shim-instance (;0;) (instantiate $wit-component-shim-module))
217    (alias core export $wit-component-shim-instance "0" (core func $indirect-test:fixed-size-lists/to-test-nested-roundtrip (;0;)))
218    (core instance $test:fixed-size-lists/to-test (;1;)
219      (export "nested-roundtrip" (func $indirect-test:fixed-size-lists/to-test-nested-roundtrip))
220    )
221    (core instance $main (;2;) (instantiate $main
222        (with "test:fixed-size-lists/to-test" (instance $test:fixed-size-lists/to-test))
223      )
224    )
225    (alias core export $main "memory" (core memory $memory (;0;)))
226    (alias core export $wit-component-shim-instance "$imports" (core table $"shim table" (;0;)))
227    (alias export $test:fixed-size-lists/to-test "nested-roundtrip" (func $nested-roundtrip (;0;)))
228    (core func $"#core-func1 indirect-test:fixed-size-lists/to-test-nested-roundtrip" (@name "indirect-test:fixed-size-lists/to-test-nested-roundtrip") (;1;) (canon lower (func $nested-roundtrip) (memory $memory)))
229    (core instance $fixup-args (;3;)
230      (export "$imports" (table $"shim table"))
231      (export "0" (func $"#core-func1 indirect-test:fixed-size-lists/to-test-nested-roundtrip"))
232    )
233    (core instance $fixup (;4;) (instantiate $wit-component-fixup
234        (with "" (instance $fixup-args))
235      )
236    )
237    (type (;1;) (func (result u32)))
238    (alias core export $main "run" (core func $run (;2;)))
239    (func $run (;1;) (type 1) (canon lift (core func $run)))
240    (export $"#func2 run" (@name "run") (;2;) "run" (func $run))
241    (@producers
242      (processed-by "wit-component" "0.243.0")
243    )
244  )
245  (component (;1;)
246    (core module $main (;0;)
247      (type (;0;) (func (param i32 i32 i32 i32 i32 i32 i32 i32) (result i32)))
248      (memory (;0;) 2)
249      (global (;0;) (mut i32) (i32.const 67248))
250      (export "memory" (memory 0))
251      (export "test:fixed-size-lists/to-test#nested-roundtrip" (func 0))
252      ;; This is an obfuscated store from the stack to address 1056,
253      ;; which is then returned
254      (func (;0;) (type 0) (param i32 i32 i32 i32 i32 i32 i32 i32) (result i32)
255        (local i32 i32 i64 i64)
256        (global.set 0
257          (local.tee 8
258            (i32.sub
259              (global.get 0)
260              (i32.const 96))))
261        (i32.store
262          (local.tee 9
263            (i32.add
264              (local.get 8)
265              (i32.const 56)))
266          (local.get 2))
267        (i32.store
268          (local.tee 2
269            (i32.add
270              (local.get 8)
271              (i32.const 40)))
272          (local.get 6))
273        (i32.store offset=60
274          (local.get 8)
275          (local.get 3))
276        (i64.store
277          (local.tee 6
278            (i32.add
279              (local.tee 3
280                (i32.add
281                  (local.get 8)
282                  (i32.const 16)))
283              (i32.const 8)))
284          (i64.load
285            (local.get 9)))
286        (i32.store offset=44
287          (local.get 8)
288          (local.get 7))
289        (i64.store
290          (local.tee 7
291            (i32.add
292              (local.get 8)
293              (i32.const 8)))
294          (i64.load
295            (local.get 2)))
296        (i64.store offset=48
297          (local.get 8)
298          (local.tee 10
299            (i64.or
300              (i64.extend_i32_u
301                (local.get 0))
302              (i64.shl
303                (i64.extend_i32_u
304                  (local.get 1))
305                (i64.const 32)))))
306        (i64.store offset=32
307          (local.get 8)
308          (local.tee 11
309            (i64.or
310              (i64.extend_i32_u
311                (local.get 4))
312              (i64.shl
313                (i64.extend_i32_u
314                  (local.get 5))
315                (i64.const 32)))))
316        (i64.store offset=16
317          (local.get 8)
318          (local.get 10))
319        (i64.store
320          (local.get 8)
321          (local.get 11))
322        (i64.store align=4
323          (local.tee 0
324            (i32.sub
325              (local.get 8)
326              (i32.const -64)))
327          (i64.load align=4
328            (local.get 3)))
329        (i64.store offset=16 align=4
330          (local.get 0)
331          (i64.load align=4
332            (local.get 8)))
333        (i64.store align=4
334          (local.tee 1
335            (i32.add
336              (local.get 0)
337              (i32.const 8)))
338          (i64.load align=4
339            (local.get 6)))
340        (i64.store align=4
341          (i32.add
342            (local.get 0)
343            (i32.const 24))
344          (i64.load align=4
345            (local.get 7)))
346        (i64.store
347          (i32.const 1064)
348          (i64.load align=4
349            (local.get 1)))
350        (i64.store
351          (i32.const 1056)
352          (i64.load offset=64 align=4
353            (local.get 8)))
354        (i64.store
355          (i32.const 1072)
356          (i64.load offset=80 align=4
357            (local.get 8)))
358        (i64.store
359          (i32.const 1080)
360          (i64.load align=4
361            (i32.add
362              (local.get 8)
363              (i32.const 88))))
364        (global.set 0
365          (i32.add
366            (local.get 8)
367            (i32.const 96)))
368        (i32.const 1056)
369      )
370      (data (;0;) (i32.const 1024) "\ff\ff\ff\ff\00\00\02")
371      (@producers
372        (processed-by "wit-component" "0.243.0")
373      )
374    )
375    (core instance $main (;0;) (instantiate $main))
376    (alias core export $main "memory" (core memory $memory (;0;)))
377    (type (;0;) (list u32 2))
378    (type (;1;) (list 0 2))
379    (type (;2;) (list s32 2))
380    (type (;3;) (list 2 2))
381    (type (;4;) (tuple 1 3))
382    (type (;5;) (func (param "a" 1) (param "b" 3) (result 4)))
383    (alias core export $main "test:fixed-size-lists/to-test#nested-roundtrip" (core func $test:fixed-size-lists/to-test#nested-roundtrip (;0;)))
384    (func $nested-roundtrip (;0;) (type 5) (canon lift (core func $test:fixed-size-lists/to-test#nested-roundtrip) (memory $memory)))
385    (component $test:fixed-size-lists/to-test-shim-component (;0;)
386      (type (;0;) (list u32 2))
387      (type (;1;) (list 0 2))
388      (type (;2;) (list s32 2))
389      (type (;3;) (list 2 2))
390      (type (;4;) (tuple 1 3))
391      (type (;5;) (func (param "a" 1) (param "b" 3) (result 4)))
392      (import "import-func-nested-roundtrip" (func (;0;) (type 5)))
393      (type (;6;) (list u32 2))
394      (type (;7;) (list 6 2))
395      (type (;8;) (list s32 2))
396      (type (;9;) (list 8 2))
397      (type (;10;) (tuple 7 9))
398      (type (;11;) (func (param "a" 7) (param "b" 9) (result 10)))
399      (export (;1;) "nested-roundtrip" (func 0) (func (type 11)))
400    )
401    (instance $test:fixed-size-lists/to-test-shim-instance (;0;) (instantiate $test:fixed-size-lists/to-test-shim-component
402        (with "import-func-nested-roundtrip" (func $nested-roundtrip))
403      )
404    )
405    (export $test:fixed-size-lists/to-test (;1;) "test:fixed-size-lists/to-test" (instance $test:fixed-size-lists/to-test-shim-instance))
406    (@producers
407      (processed-by "wit-component" "0.243.0")
408    )
409  )
410  (instance (;0;) (instantiate 1))
411  (alias export 0 "test:fixed-size-lists/to-test" (instance (;1;)))
412  (instance (;2;) (instantiate 0
413      (with "test:fixed-size-lists/to-test" (instance 1))
414    )
415  )
416  (alias export 2 "run" (func (;0;)))
417  (export (;1;) "run" (func 0))
418)
419
420;; call run, it will return the number of mismatches in the test
421(assert_return (invoke "run")
422  (u32.const 0))
423