Debian Codegraph @

internal/integration_test/spectest/extended-const/testdata/global.wast

raw
1 ;; Test globals
2
3 (module
4 (global (import "spectest" "global_i32") i32)
5 (global (import "spectest" "global_i64") i64)
6
7 (global $a i32 (i32.const -2))
8 (global (;3;) f32 (f32.const -3))
9 (global (;4;) f64 (f64.const -4))
10 (global $b i64 (i64.const -5))
11
12 (global $x (mut i32) (i32.const -12))
13 (global (;7;) (mut f32) (f32.const -13))
14 (global (;8;) (mut f64) (f64.const -14))
15 (global $y (mut i64) (i64.const -15))
16
17 (global $z1 i32 (global.get 0))
18 (global $z2 i64 (global.get 1))
19 (global $z3 i32 (i32.add (i32.sub (i32.mul (i32.const 20) (i32.const 2)) (i32.const 2)) (i32.const 4)))
20 (global $z4 i64 (i64.add (i64.sub (i64.mul (i64.const 20) (i64.const 2)) (i64.const 2)) (i64.const 5)))
21 (global $z5 i32 (i32.add (global.get 0) (i32.const 42)))
22 (global $z6 i64 (i64.add (global.get 1) (i64.const 42)))
23
24
25 (func (export "get-a") (result i32) (global.get $a))
26 (func (export "get-b") (result i64) (global.get $b))
27 (func (export "get-x") (result i32) (global.get $x))
28 (func (export "get-y") (result i64) (global.get $y))
29 (func (export "get-z1") (result i32) (global.get $z1))
30 (func (export "get-z2") (result i64) (global.get $z2))
31 (func (export "get-z3") (result i32) (global.get $z3))
32 (func (export "get-z4") (result i64) (global.get $z4))
33 (func (export "get-z5") (result i32) (global.get $z5))
34 (func (export "get-z6") (result i64) (global.get $z6))
35 (func (export "set-x") (param i32) (global.set $x (local.get 0)))
36 (func (export "set-y") (param i64) (global.set $y (local.get 0)))
37
38 (func (export "get-3") (result f32) (global.get 3))
39 (func (export "get-4") (result f64) (global.get 4))
40 (func (export "get-7") (result f32) (global.get 7))
41 (func (export "get-8") (result f64) (global.get 8))
42 (func (export "set-7") (param f32) (global.set 7 (local.get 0)))
43 (func (export "set-8") (param f64) (global.set 8 (local.get 0)))
44
45 ;; As the argument of control constructs and instructions
46
47 (memory 1)
48
49 (func $dummy)
50
51 (func (export "as-select-first") (result i32)
52 (select (global.get $x) (i32.const 2) (i32.const 3))
53 )
54 (func (export "as-select-mid") (result i32)
55 (select (i32.const 2) (global.get $x) (i32.const 3))
56 )
57 (func (export "as-select-last") (result i32)
58 (select (i32.const 2) (i32.const 3) (global.get $x))
59 )
60
61 (func (export "as-loop-first") (result i32)
62 (loop (result i32)
63 (global.get $x) (call $dummy) (call $dummy)
64 )
65 )
66 (func (export "as-loop-mid") (result i32)
67 (loop (result i32)
68 (call $dummy) (global.get $x) (call $dummy)
69 )
70 )
71 (func (export "as-loop-last") (result i32)
72 (loop (result i32)
73 (call $dummy) (call $dummy) (global.get $x)
74 )
75 )
76
77 (func (export "as-if-condition") (result i32)
78 (if (result i32) (global.get $x)
79 (then (call $dummy) (i32.const 2))
80 (else (call $dummy) (i32.const 3))
81 )
82 )
83 (func (export "as-if-then") (result i32)
84 (if (result i32) (i32.const 1)
85 (then (global.get $x)) (else (i32.const 2))
86 )
87 )
88 (func (export "as-if-else") (result i32)
89 (if (result i32) (i32.const 0)
90 (then (i32.const 2)) (else (global.get $x))
91 )
92 )
93
94 (func (export "as-br_if-first") (result i32)
95 (block (result i32)
96 (br_if 0 (global.get $x) (i32.const 2))
97 (return (i32.const 3))
98 )
99 )
100 (func (export "as-br_if-last") (result i32)
101 (block (result i32)
102 (br_if 0 (i32.const 2) (global.get $x))
103 (return (i32.const 3))
104 )
105 )
106
107 (func (export "as-br_table-first") (result i32)
108 (block (result i32)
109 (global.get $x) (i32.const 2) (br_table 0 0)
110 )
111 )
112 (func (export "as-br_table-last") (result i32)
113 (block (result i32)
114 (i32.const 2) (global.get $x) (br_table 0 0)
115 )
116 )
117
118 (func $func (param i32 i32) (result i32) (local.get 0))
119 (type $check (func (param i32 i32) (result i32)))
120 (table funcref (elem $func))
121 (func (export "as-call_indirect-first") (result i32)
122 (block (result i32)
123 (call_indirect (type $check)
124 (global.get $x) (i32.const 2) (i32.const 0)
125 )
126 )
127 )
128 (func (export "as-call_indirect-mid") (result i32)
129 (block (result i32)
130 (call_indirect (type $check)
131 (i32.const 2) (global.get $x) (i32.const 0)
132 )
133 )
134 )
135 (func (export "as-call_indirect-last") (result i32)
136 (block (result i32)
137 (call_indirect (type $check)
138 (i32.const 2) (i32.const 0) (global.get $x)
139 )
140 )
141 )
142
143 (func (export "as-store-first")
144 (global.get $x) (i32.const 1) (i32.store)
145 )
146 (func (export "as-store-last")
147 (i32.const 0) (global.get $x) (i32.store)
148 )
149 (func (export "as-load-operand") (result i32)
150 (i32.load (global.get $x))
151 )
152 (func (export "as-memory.grow-value") (result i32)
153 (memory.grow (global.get $x))
154 )
155
156 (func $f (param i32) (result i32) (local.get 0))
157 (func (export "as-call-value") (result i32)
158 (call $f (global.get $x))
159 )
160
161 (func (export "as-return-value") (result i32)
162 (global.get $x) (return)
163 )
164 (func (export "as-drop-operand")
165 (drop (global.get $x))
166 )
167 (func (export "as-br-value") (result i32)
168 (block (result i32) (br 0 (global.get $x)))
169 )
170
171 (func (export "as-local.set-value") (param i32) (result i32)
172 (local.set 0 (global.get $x))
173 (local.get 0)
174 )
175 (func (export "as-local.tee-value") (param i32) (result i32)
176 (local.tee 0 (global.get $x))
177 )
178 (func (export "as-global.set-value") (result i32)
179 (global.set $x (global.get $x))
180 (global.get $x)
181 )
182
183 (func (export "as-unary-operand") (result i32)
184 (i32.eqz (global.get $x))
185 )
186 (func (export "as-binary-operand") (result i32)
187 (i32.mul
188 (global.get $x) (global.get $x)
189 )
190 )
191 (func (export "as-compare-operand") (result i32)
192 (i32.gt_u
193 (global.get 0) (i32.const 1)
194 )
195 )
196 )
197
198 (assert_return (invoke "get-a") (i32.const -2))
199 (assert_return (invoke "get-b") (i64.const -5))
200 (assert_return (invoke "get-x") (i32.const -12))
201 (assert_return (invoke "get-y") (i64.const -15))
202 (assert_return (invoke "get-z1") (i32.const 666))
203 (assert_return (invoke "get-z2") (i64.const 666))
204 (assert_return (invoke "get-z3") (i32.const 42))
205 (assert_return (invoke "get-z4") (i64.const 43))
206 (assert_return (invoke "get-z5") (i32.const 708))
207 (assert_return (invoke "get-z6") (i64.const 708))
208
209 (assert_return (invoke "get-3") (f32.const -3))
210 (assert_return (invoke "get-4") (f64.const -4))
211 (assert_return (invoke "get-7") (f32.const -13))
212 (assert_return (invoke "get-8") (f64.const -14))
213
214 (assert_return (invoke "set-x" (i32.const 6)))
215 (assert_return (invoke "set-y" (i64.const 7)))
216 (assert_return (invoke "set-7" (f32.const 8)))
217 (assert_return (invoke "set-8" (f64.const 9)))
218
219 (assert_return (invoke "get-x") (i32.const 6))
220 (assert_return (invoke "get-y") (i64.const 7))
221 (assert_return (invoke "get-7") (f32.const 8))
222 (assert_return (invoke "get-8") (f64.const 9))
223
224 (assert_return (invoke "as-select-first") (i32.const 6))
225 (assert_return (invoke "as-select-mid") (i32.const 2))
226 (assert_return (invoke "as-select-last") (i32.const 2))
227
228 (assert_return (invoke "as-loop-first") (i32.const 6))
229 (assert_return (invoke "as-loop-mid") (i32.const 6))
230 (assert_return (invoke "as-loop-last") (i32.const 6))
231
232 (assert_return (invoke "as-if-condition") (i32.const 2))
233 (assert_return (invoke "as-if-then") (i32.const 6))
234 (assert_return (invoke "as-if-else") (i32.const 6))
235
236 (assert_return (invoke "as-br_if-first") (i32.const 6))
237 (assert_return (invoke "as-br_if-last") (i32.const 2))
238
239 (assert_return (invoke "as-br_table-first") (i32.const 6))
240 (assert_return (invoke "as-br_table-last") (i32.const 2))
241
242 (assert_return (invoke "as-call_indirect-first") (i32.const 6))
243 (assert_return (invoke "as-call_indirect-mid") (i32.const 2))
244 (assert_trap (invoke "as-call_indirect-last") "undefined element")
245
246 (assert_return (invoke "as-store-first"))
247 (assert_return (invoke "as-store-last"))
248 (assert_return (invoke "as-load-operand") (i32.const 1))
249 (assert_return (invoke "as-memory.grow-value") (i32.const 1))
250
251 (assert_return (invoke "as-call-value") (i32.const 6))
252
253 (assert_return (invoke "as-return-value") (i32.const 6))
254 (assert_return (invoke "as-drop-operand"))
255 (assert_return (invoke "as-br-value") (i32.const 6))
256
257 (assert_return (invoke "as-local.set-value" (i32.const 1)) (i32.const 6))
258 (assert_return (invoke "as-local.tee-value" (i32.const 1)) (i32.const 6))
259 (assert_return (invoke "as-global.set-value") (i32.const 6))
260
261 (assert_return (invoke "as-unary-operand") (i32.const 0))
262 (assert_return (invoke "as-binary-operand") (i32.const 36))
263 (assert_return (invoke "as-compare-operand") (i32.const 1))
264
265 (assert_invalid
266 (module (global f32 (f32.const 0)) (func (global.set 0 (f32.const 1))))
267 "global is immutable"
268 )
269
270 (assert_invalid
271 (module (import "spectest" "global_i32" (global i32)) (func (global.set 0 (i32.const 1))))
272 "global is immutable"
273 )
274
275 ;; mutable globals can be exported
276 (module (global (mut f32) (f32.const 0)) (export "a" (global 0)))
277 (module (global (export "a") (mut f32) (f32.const 0)))
278
279 (assert_invalid
280 (module (global f32 (f32.neg (f32.const 0))))
281 "constant expression required"
282 )
283
284 (assert_invalid
285 (module (global f32 (local.get 0)))
286 "constant expression required"
287 )
288
289 (assert_invalid
290 (module (global f32 (f32.neg (f32.const 1))))
291 "constant expression required"
292 )
293
294 (assert_invalid
295 (module (global i32 (i32.const 0) (nop)))
296 "constant expression required"
297 )
298
299 (assert_invalid
300 (module (global i32 (i32.ctz (i32.const 0))))
301 "constant expression required"
302 )
303
304 (assert_invalid
305 (module (global i32 (nop)))
306 "constant expression required"
307 )
308
309 (assert_invalid
310 (module (global i32 (f32.const 0)))
311 "type mismatch"
312 )
313
314 (assert_invalid
315 (module (global i32 (i32.const 0) (i32.const 0)))
316 "type mismatch"
317 )
318
319 (assert_invalid
320 (module (global i32 (;empty instruction sequence;)))
321 "type mismatch"
322 )
323
324 (assert_invalid
325 (module (global (import "test" "global-i32") i32) (global i32 (global.get 0) (global.get 0)))
326 "type mismatch"
327 )
328
329 (assert_invalid
330 (module (global (import "test" "global-i32") i32) (global i32 (i32.const 0) (global.get 0)))
331 "type mismatch"
332 )
333
334 (assert_invalid
335 (module (global i32 (global.get 0)))
336 "unknown global"
337 )
338
339 (assert_invalid
340 (module (global i32 (global.get 1)) (global i32 (i32.const 0)))
341 "unknown global"
342 )
343
344 (assert_invalid
345 (module (global (import "test" "global-i32") i32) (global i32 (global.get 2)))
346 "unknown global"
347 )
348
349 (assert_invalid
350 (module (global (import "test" "global-mut-i32") (mut i32)) (global i32 (global.get 0)))
351 "constant expression required"
352 )
353
354 (module
355 (import "spectest" "global_i32" (global i32))
356 )
357 (assert_malformed
358 (module binary
359 "\00asm" "\01\00\00\00"
360 "\02\98\80\80\80\00" ;; import section
361 "\01" ;; length 1
362 "\08\73\70\65\63\74\65\73\74" ;; "spectest"
363 "\0a\67\6c\6f\62\61\6c\5f\69\33\32" ;; "global_i32"
364 "\03" ;; GlobalImport
365 "\7f" ;; i32
366 "\02" ;; malformed mutability
367 )
368 "malformed mutability"
369 )
370 (assert_malformed
371 (module binary
372 "\00asm" "\01\00\00\00"
373 "\02\98\80\80\80\00" ;; import section
374 "\01" ;; length 1
375 "\08\73\70\65\63\74\65\73\74" ;; "spectest"
376 "\0a\67\6c\6f\62\61\6c\5f\69\33\32" ;; "global_i32"
377 "\03" ;; GlobalImport
378 "\7f" ;; i32
379 "\ff" ;; malformed mutability
380 )
381 "malformed mutability"
382 )
383
384 (module
385 (global i32 (i32.const 0))
386 )
387 (assert_malformed
388 (module binary
389 "\00asm" "\01\00\00\00"
390 "\06\86\80\80\80\00" ;; global section
391 "\01" ;; length 1
392 "\7f" ;; i32
393 "\02" ;; malformed mutability
394 "\41\00" ;; i32.const 0
395 "\0b" ;; end
396 )
397 "malformed mutability"
398 )
399 (assert_malformed
400 (module binary
401 "\00asm" "\01\00\00\00"
402 "\06\86\80\80\80\00" ;; global section
403 "\01" ;; length 1
404 "\7f" ;; i32
405 "\ff" ;; malformed mutability
406 "\41\00" ;; i32.const 0
407 "\0b" ;; end
408 )
409 "malformed mutability"
410 )
411
412 ;; global.get with invalid index
413 (assert_invalid
414 (module (func (result i32) (global.get 0)))
415 "unknown global"
416 )
417
418 (assert_invalid
419 (module
420 (global i32 (i32.const 0))
421 (func (result i32) (global.get 1))
422 )
423 "unknown global"
424 )
425
426 (assert_invalid
427 (module
428 (import "spectest" "global_i32" (global i32))
429 (func (result i32) (global.get 1))
430 )
431 "unknown global"
432 )
433
434 (assert_invalid
435 (module
436 (import "spectest" "global_i32" (global i32))
437 (global i32 (i32.const 0))
438 (func (result i32) (global.get 2))
439 )
440 "unknown global"
441 )
442
443 ;; global.set with invalid index
444 (assert_invalid
445 (module (func (i32.const 0) (global.set 0)))
446 "unknown global"
447 )
448
449 (assert_invalid
450 (module
451 (global i32 (i32.const 0))
452 (func (i32.const 0) (global.set 1))
453 )
454 "unknown global"
455 )
456
457 (assert_invalid
458 (module
459 (import "spectest" "global_i32" (global i32))
460 (func (i32.const 0) (global.set 1))
461 )
462 "unknown global"
463 )
464
465 (assert_invalid
466 (module
467 (import "spectest" "global_i32" (global i32))
468 (global i32 (i32.const 0))
469 (func (i32.const 0) (global.set 2))
470 )
471 "unknown global"
472 )
473
474
475 (assert_invalid
476 (module
477 (global $x (mut i32) (i32.const 0))
478 (func $type-global.set-value-empty
479 (global.set $x)
480 )
481 )
482 "type mismatch"
483 )
484 (assert_invalid
485 (module
486 (global $x (mut i32) (i32.const 0))
487 (func $type-global.set-value-empty-in-block
488 (i32.const 0)
489 (block (global.set $x))
490 )
491 )
492 "type mismatch"
493 )
494 (assert_invalid
495 (module
496 (global $x (mut i32) (i32.const 0))
497 (func $type-global.set-value-empty-in-loop
498 (i32.const 0)
499 (loop (global.set $x))
500 )
501 )
502 "type mismatch"
503 )
504 (assert_invalid
505 (module
506 (global $x (mut i32) (i32.const 0))
507 (func $type-global.set-value-empty-in-then
508 (i32.const 0) (i32.const 0)
509 (if (then (global.set $x)))
510 )
511 )
512 "type mismatch"
513 )
514 (assert_invalid
515 (module
516 (global $x (mut i32) (i32.const 0))
517 (func $type-global.set-value-empty-in-else
518 (i32.const 0) (i32.const 0)
519 (if (result i32) (then (i32.const 0)) (else (global.set $x)))
520 )
521 )
522 "type mismatch"
523 )
524 (assert_invalid
525 (module
526 (global $x (mut i32) (i32.const 0))
527 (func $type-global.set-value-empty-in-br
528 (i32.const 0)
529 (block (br 0 (global.set $x)))
530 )
531 )
532 "type mismatch"
533 )
534 (assert_invalid
535 (module
536 (global $x (mut i32) (i32.const 0))
537 (func $type-global.set-value-empty-in-br_if
538 (i32.const 0)
539 (block (br_if 0 (global.set $x)))
540 )
541 )
542 "type mismatch"
543 )
544 (assert_invalid
545 (module
546 (global $x (mut i32) (i32.const 0))
547 (func $type-global.set-value-empty-in-br_table
548 (i32.const 0)
549 (block (br_table 0 (global.set $x)))
550 )
551 )
552 "type mismatch"
553 )
554 (assert_invalid
555 (module
556 (global $x (mut i32) (i32.const 0))
557 (func $type-global.set-value-empty-in-return
558 (return (global.set $x))
559 )
560 )
561 "type mismatch"
562 )
563 (assert_invalid
564 (module
565 (global $x (mut i32) (i32.const 0))
566 (func $type-global.set-value-empty-in-select
567 (select (global.set $x) (i32.const 1) (i32.const 2))
568 )
569 )
570 "type mismatch"
571 )
572 (assert_invalid
573 (module
574 (global $x (mut i32) (i32.const 0))
575 (func $type-global.set-value-empty-in-call
576 (call 1 (global.set $x))
577 )
578 (func (param i32) (result i32) (local.get 0))
579 )
580 "type mismatch"
581 )
582 (assert_invalid
583 (module
584 (global $x (mut i32) (i32.const 0))
585 (func $f (param i32) (result i32) (local.get 0))
586 (type $sig (func (param i32) (result i32)))
587 (table funcref (elem $f))
588 (func $type-global.set-value-empty-in-call_indirect
589 (block (result i32)
590 (call_indirect (type $sig)
591 (global.set $x) (i32.const 0)
592 )
593 )
594 )
595 )
596 "type mismatch"
597 )
598
599 ;; Duplicate identifier errors
600
601 (assert_malformed (module quote
602 "(global $foo i32 (i32.const 0))"
603 "(global $foo i32 (i32.const 0))")
604 "duplicate global")
605 (assert_malformed (module quote
606 "(import \"\" \"\" (global $foo i32))"
607 "(global $foo i32 (i32.const 0))")
608 "duplicate global")
609 (assert_malformed (module quote
610 "(import \"\" \"\" (global $foo i32))"
611 "(import \"\" \"\" (global $foo i32))")
612 "duplicate global")