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