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