wa-lang.org/wazero@v1.0.2/internal/integration_test/spectest/v1/testdata/loop.wast (about)

     1  ;; Test `loop` opcode
     2  
     3  (module
     4    (memory 1)
     5  
     6    (func $dummy)
     7  
     8    (func (export "empty")
     9      (loop)
    10      (loop $l)
    11    )
    12  
    13    (func (export "singular") (result i32)
    14      (loop (nop))
    15      (loop (result i32) (i32.const 7))
    16    )
    17  
    18    (func (export "multi") (result i32)
    19      (loop (call $dummy) (call $dummy) (call $dummy) (call $dummy))
    20      (loop (result i32) (call $dummy) (call $dummy) (call $dummy) (i32.const 8))
    21    )
    22  
    23    (func (export "nested") (result i32)
    24      (loop (result i32)
    25        (loop (call $dummy) (block) (nop))
    26        (loop (result i32) (call $dummy) (i32.const 9))
    27      )
    28    )
    29  
    30    (func (export "deep") (result i32)
    31      (loop (result i32) (block (result i32)
    32        (loop (result i32) (block (result i32)
    33          (loop (result i32) (block (result i32)
    34            (loop (result i32) (block (result i32)
    35              (loop (result i32) (block (result i32)
    36                (loop (result i32) (block (result i32)
    37                  (loop (result i32) (block (result i32)
    38                    (loop (result i32) (block (result i32)
    39                      (loop (result i32) (block (result i32)
    40                        (loop (result i32) (block (result i32)
    41                          (loop (result i32) (block (result i32)
    42                            (loop (result i32) (block (result i32)
    43                              (loop (result i32) (block (result i32)
    44                                (loop (result i32) (block (result i32)
    45                                  (loop (result i32) (block (result i32)
    46                                    (loop (result i32) (block (result i32)
    47                                      (loop (result i32) (block (result i32)
    48                                        (loop (result i32) (block (result i32)
    49                                          (loop (result i32) (block (result i32)
    50                                            (loop (result i32) (block (result i32)
    51                                              (call $dummy) (i32.const 150)
    52                                            ))
    53                                          ))
    54                                        ))
    55                                      ))
    56                                    ))
    57                                  ))
    58                                ))
    59                              ))
    60                            ))
    61                          ))
    62                        ))
    63                      ))
    64                    ))
    65                  ))
    66                ))
    67              ))
    68            ))
    69          ))
    70        ))
    71      ))
    72    )
    73  
    74    (func (export "as-select-first") (result i32)
    75      (select (loop (result i32) (i32.const 1)) (i32.const 2) (i32.const 3))
    76    )
    77    (func (export "as-select-mid") (result i32)
    78      (select (i32.const 2) (loop (result i32) (i32.const 1)) (i32.const 3))
    79    )
    80    (func (export "as-select-last") (result i32)
    81      (select (i32.const 2) (i32.const 3) (loop (result i32) (i32.const 1)))
    82    )
    83  
    84    (func (export "as-if-condition")
    85      (loop (result i32) (i32.const 1)) (if (then (call $dummy)))
    86    )
    87    (func (export "as-if-then") (result i32)
    88      (if (result i32) (i32.const 1) (then (loop (result i32) (i32.const 1))) (else (i32.const 2)))
    89    )
    90    (func (export "as-if-else") (result i32)
    91      (if (result i32) (i32.const 1) (then (i32.const 2)) (else (loop (result i32) (i32.const 1))))
    92    )
    93  
    94    (func (export "as-br_if-first") (result i32)
    95      (block (result i32) (br_if 0 (loop (result i32) (i32.const 1)) (i32.const 2)))
    96    )
    97    (func (export "as-br_if-last") (result i32)
    98      (block (result i32) (br_if 0 (i32.const 2) (loop (result i32) (i32.const 1))))
    99    )
   100  
   101    (func (export "as-br_table-first") (result i32)
   102      (block (result i32) (loop (result i32) (i32.const 1)) (i32.const 2) (br_table 0 0))
   103    )
   104    (func (export "as-br_table-last") (result i32)
   105      (block (result i32) (i32.const 2) (loop (result i32) (i32.const 1)) (br_table 0 0))
   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          (loop (result i32) (i32.const 1)) (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) (loop (result i32) (i32.const 1)) (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 1) (i32.const 2) (loop (result i32) (i32.const 0))
   129        )
   130      )
   131    )
   132  
   133    (func (export "as-store-first")
   134      (loop (result i32) (i32.const 1)) (i32.const 1) (i32.store)
   135    )
   136    (func (export "as-store-last")
   137      (i32.const 10) (loop (result i32) (i32.const 1)) (i32.store)
   138    )
   139  
   140    (func (export "as-memory.grow-value") (result i32)
   141      (memory.grow (loop (result i32) (i32.const 1)))
   142    )
   143  
   144    (func $f (param i32) (result i32) (local.get 0))
   145  
   146    (func (export "as-call-value") (result i32)
   147      (call $f (loop (result i32) (i32.const 1)))
   148    )
   149    (func (export "as-return-value") (result i32)
   150      (loop (result i32) (i32.const 1)) (return)
   151    )
   152    (func (export "as-drop-operand")
   153      (drop (loop (result i32) (i32.const 1)))
   154    )
   155    (func (export "as-br-value") (result i32)
   156      (block (result i32) (br 0 (loop (result i32) (i32.const 1))))
   157    )
   158    (func (export "as-local.set-value") (result i32)
   159      (local i32) (local.set 0 (loop (result i32) (i32.const 1))) (local.get 0)
   160    )
   161    (func (export "as-local.tee-value") (result i32)
   162      (local i32) (local.tee 0 (loop (result i32) (i32.const 1)))
   163    )
   164    (global $a (mut i32) (i32.const 0))
   165    (func (export "as-global.set-value") (result i32)
   166      (global.set $a (loop (result i32) (i32.const 1)))
   167      (global.get $a)
   168    )
   169    (func (export "as-load-operand") (result i32)
   170      (i32.load (loop (result i32) (i32.const 1)))
   171    )
   172  
   173    (func (export "as-unary-operand") (result i32)
   174      (i32.ctz (loop (result i32) (call $dummy) (i32.const 13)))
   175    )
   176    (func (export "as-binary-operand") (result i32)
   177      (i32.mul
   178        (loop (result i32) (call $dummy) (i32.const 3))
   179        (loop (result i32) (call $dummy) (i32.const 4))
   180      )
   181    )
   182    (func (export "as-test-operand") (result i32)
   183      (i32.eqz (loop (result i32) (call $dummy) (i32.const 13)))
   184    )
   185    (func (export "as-compare-operand") (result i32)
   186      (f32.gt
   187        (loop (result f32) (call $dummy) (f32.const 3))
   188        (loop (result f32) (call $dummy) (f32.const 3))
   189      )
   190    )
   191  
   192    (func (export "break-bare") (result i32)
   193      (block (loop (br 1) (br 0) (unreachable)))
   194      (block (loop (br_if 1 (i32.const 1)) (unreachable)))
   195      (block (loop (br_table 1 (i32.const 0)) (unreachable)))
   196      (block (loop (br_table 1 1 1 (i32.const 1)) (unreachable)))
   197      (i32.const 19)
   198    )
   199    (func (export "break-value") (result i32)
   200      (block (result i32)
   201        (loop (result i32) (br 1 (i32.const 18)) (br 0) (i32.const 19))
   202      )
   203    )
   204    (func (export "break-repeated") (result i32)
   205      (block (result i32)
   206        (loop (result i32)
   207          (br 1 (i32.const 18))
   208          (br 1 (i32.const 19))
   209          (drop (br_if 1 (i32.const 20) (i32.const 0)))
   210          (drop (br_if 1 (i32.const 20) (i32.const 1)))
   211          (br 1 (i32.const 21))
   212          (br_table 1 (i32.const 22) (i32.const 0))
   213          (br_table 1 1 1 (i32.const 23) (i32.const 1))
   214          (i32.const 21)
   215        )
   216      )
   217    )
   218    (func (export "break-inner") (result i32)
   219      (local i32)
   220      (local.set 0 (i32.const 0))
   221      (local.set 0 (i32.add (local.get 0) (block (result i32) (loop (result i32) (block (result i32) (br 2 (i32.const 0x1)))))))
   222      (local.set 0 (i32.add (local.get 0) (block (result i32) (loop (result i32) (loop (result i32) (br 2 (i32.const 0x2)))))))
   223      (local.set 0 (i32.add (local.get 0) (block (result i32) (loop (result i32) (block (result i32) (loop (result i32) (br 1 (i32.const 0x4))))))))
   224      (local.set 0 (i32.add (local.get 0) (block (result i32) (loop (result i32) (i32.ctz (br 1 (i32.const 0x8)))))))
   225      (local.set 0 (i32.add (local.get 0) (block (result i32) (loop (result i32) (i32.ctz (loop (result i32) (br 2 (i32.const 0x10))))))))
   226      (local.get 0)
   227    )
   228    (func (export "cont-inner") (result i32)
   229      (local i32)
   230      (local.set 0 (i32.const 0))
   231      (local.set 0 (i32.add (local.get 0) (loop (result i32) (loop (result i32) (br 1)))))
   232      (local.set 0 (i32.add (local.get 0) (loop (result i32) (i32.ctz (br 0)))))
   233      (local.set 0 (i32.add (local.get 0) (loop (result i32) (i32.ctz (loop (result i32) (br 1))))))
   234      (local.get 0)
   235    )
   236  
   237    (func $fx (export "effects") (result i32)
   238      (local i32)
   239      (block
   240        (loop
   241          (local.set 0 (i32.const 1))
   242          (local.set 0 (i32.mul (local.get 0) (i32.const 3)))
   243          (local.set 0 (i32.sub (local.get 0) (i32.const 5)))
   244          (local.set 0 (i32.mul (local.get 0) (i32.const 7)))
   245          (br 1)
   246          (local.set 0 (i32.mul (local.get 0) (i32.const 100)))
   247        )
   248      )
   249      (i32.eq (local.get 0) (i32.const -14))
   250    )
   251  
   252    (func (export "while") (param i64) (result i64)
   253      (local i64)
   254      (local.set 1 (i64.const 1))
   255      (block
   256        (loop
   257          (br_if 1 (i64.eqz (local.get 0)))
   258          (local.set 1 (i64.mul (local.get 0) (local.get 1)))
   259          (local.set 0 (i64.sub (local.get 0) (i64.const 1)))
   260          (br 0)
   261        )
   262      )
   263      (local.get 1)
   264    )
   265  
   266    (func (export "for") (param i64) (result i64)
   267      (local i64 i64)
   268      (local.set 1 (i64.const 1))
   269      (local.set 2 (i64.const 2))
   270      (block
   271        (loop
   272          (br_if 1 (i64.gt_u (local.get 2) (local.get 0)))
   273          (local.set 1 (i64.mul (local.get 1) (local.get 2)))
   274          (local.set 2 (i64.add (local.get 2) (i64.const 1)))
   275          (br 0)
   276        )
   277      )
   278      (local.get 1)
   279    )
   280  
   281    (func (export "nesting") (param f32 f32) (result f32)
   282      (local f32 f32)
   283      (block
   284        (loop
   285          (br_if 1 (f32.eq (local.get 0) (f32.const 0)))
   286          (local.set 2 (local.get 1))
   287          (block
   288            (loop
   289              (br_if 1 (f32.eq (local.get 2) (f32.const 0)))
   290              (br_if 3 (f32.lt (local.get 2) (f32.const 0)))
   291              (local.set 3 (f32.add (local.get 3) (local.get 2)))
   292              (local.set 2 (f32.sub (local.get 2) (f32.const 2)))
   293              (br 0)
   294            )
   295          )
   296          (local.set 3 (f32.div (local.get 3) (local.get 0)))
   297          (local.set 0 (f32.sub (local.get 0) (f32.const 1)))
   298          (br 0)
   299        )
   300      )
   301      (local.get 3)
   302    )
   303  )
   304  
   305  (assert_return (invoke "empty"))
   306  (assert_return (invoke "singular") (i32.const 7))
   307  (assert_return (invoke "multi") (i32.const 8))
   308  (assert_return (invoke "nested") (i32.const 9))
   309  (assert_return (invoke "deep") (i32.const 150))
   310  
   311  (assert_return (invoke "as-select-first") (i32.const 1))
   312  (assert_return (invoke "as-select-mid") (i32.const 2))
   313  (assert_return (invoke "as-select-last") (i32.const 2))
   314  
   315  (assert_return (invoke "as-if-condition"))
   316  (assert_return (invoke "as-if-then") (i32.const 1))
   317  (assert_return (invoke "as-if-else") (i32.const 2))
   318  
   319  (assert_return (invoke "as-br_if-first") (i32.const 1))
   320  (assert_return (invoke "as-br_if-last") (i32.const 2))
   321  
   322  (assert_return (invoke "as-br_table-first") (i32.const 1))
   323  (assert_return (invoke "as-br_table-last") (i32.const 2))
   324  
   325  (assert_return (invoke "as-call_indirect-first") (i32.const 1))
   326  (assert_return (invoke "as-call_indirect-mid") (i32.const 2))
   327  (assert_return (invoke "as-call_indirect-last") (i32.const 1))
   328  
   329  (assert_return (invoke "as-store-first"))
   330  (assert_return (invoke "as-store-last"))
   331  
   332  (assert_return (invoke "as-memory.grow-value") (i32.const 1))
   333  (assert_return (invoke "as-call-value") (i32.const 1))
   334  (assert_return (invoke "as-return-value") (i32.const 1))
   335  (assert_return (invoke "as-drop-operand"))
   336  (assert_return (invoke "as-br-value") (i32.const 1))
   337  (assert_return (invoke "as-local.set-value") (i32.const 1))
   338  (assert_return (invoke "as-local.tee-value") (i32.const 1))
   339  (assert_return (invoke "as-global.set-value") (i32.const 1))
   340  (assert_return (invoke "as-load-operand") (i32.const 1))
   341  
   342  (assert_return (invoke "as-unary-operand") (i32.const 0))
   343  (assert_return (invoke "as-binary-operand") (i32.const 12))
   344  (assert_return (invoke "as-test-operand") (i32.const 0))
   345  (assert_return (invoke "as-compare-operand") (i32.const 0))
   346  
   347  (assert_return (invoke "break-bare") (i32.const 19))
   348  (assert_return (invoke "break-value") (i32.const 18))
   349  (assert_return (invoke "break-repeated") (i32.const 18))
   350  (assert_return (invoke "break-inner") (i32.const 0x1f))
   351  
   352  (assert_return (invoke "effects") (i32.const 1))
   353  
   354  (assert_return (invoke "while" (i64.const 0)) (i64.const 1))
   355  (assert_return (invoke "while" (i64.const 1)) (i64.const 1))
   356  (assert_return (invoke "while" (i64.const 2)) (i64.const 2))
   357  (assert_return (invoke "while" (i64.const 3)) (i64.const 6))
   358  (assert_return (invoke "while" (i64.const 5)) (i64.const 120))
   359  (assert_return (invoke "while" (i64.const 20)) (i64.const 2432902008176640000))
   360  
   361  (assert_return (invoke "for" (i64.const 0)) (i64.const 1))
   362  (assert_return (invoke "for" (i64.const 1)) (i64.const 1))
   363  (assert_return (invoke "for" (i64.const 2)) (i64.const 2))
   364  (assert_return (invoke "for" (i64.const 3)) (i64.const 6))
   365  (assert_return (invoke "for" (i64.const 5)) (i64.const 120))
   366  (assert_return (invoke "for" (i64.const 20)) (i64.const 2432902008176640000))
   367  
   368  (assert_return (invoke "nesting" (f32.const 0) (f32.const 7)) (f32.const 0))
   369  (assert_return (invoke "nesting" (f32.const 7) (f32.const 0)) (f32.const 0))
   370  (assert_return (invoke "nesting" (f32.const 1) (f32.const 1)) (f32.const 1))
   371  (assert_return (invoke "nesting" (f32.const 1) (f32.const 2)) (f32.const 2))
   372  (assert_return (invoke "nesting" (f32.const 1) (f32.const 3)) (f32.const 4))
   373  (assert_return (invoke "nesting" (f32.const 1) (f32.const 4)) (f32.const 6))
   374  (assert_return (invoke "nesting" (f32.const 1) (f32.const 100)) (f32.const 2550))
   375  (assert_return (invoke "nesting" (f32.const 1) (f32.const 101)) (f32.const 2601))
   376  (assert_return (invoke "nesting" (f32.const 2) (f32.const 1)) (f32.const 1))
   377  (assert_return (invoke "nesting" (f32.const 3) (f32.const 1)) (f32.const 1))
   378  (assert_return (invoke "nesting" (f32.const 10) (f32.const 1)) (f32.const 1))
   379  (assert_return (invoke "nesting" (f32.const 2) (f32.const 2)) (f32.const 3))
   380  (assert_return (invoke "nesting" (f32.const 2) (f32.const 3)) (f32.const 4))
   381  (assert_return (invoke "nesting" (f32.const 7) (f32.const 4)) (f32.const 10.3095235825))
   382  (assert_return (invoke "nesting" (f32.const 7) (f32.const 100)) (f32.const 4381.54785156))
   383  (assert_return (invoke "nesting" (f32.const 7) (f32.const 101)) (f32.const 2601))
   384  
   385  (assert_invalid
   386    (module (func $type-empty-i32 (result i32) (loop)))
   387    "type mismatch"
   388  )
   389  (assert_invalid
   390    (module (func $type-empty-i64 (result i64) (loop)))
   391    "type mismatch"
   392  )
   393  (assert_invalid
   394    (module (func $type-empty-f32 (result f32) (loop)))
   395    "type mismatch"
   396  )
   397  (assert_invalid
   398    (module (func $type-empty-f64 (result f64) (loop)))
   399    "type mismatch"
   400  )
   401  
   402  (assert_invalid
   403    (module (func $type-value-num-vs-void
   404      (loop (i32.const 1))
   405    ))
   406    "type mismatch"
   407  )
   408  (assert_invalid
   409    (module (func $type-value-empty-vs-num (result i32)
   410      (loop (result i32))
   411    ))
   412    "type mismatch"
   413  )
   414  (assert_invalid
   415    (module (func $type-value-void-vs-num (result i32)
   416      (loop (result i32) (nop))
   417    ))
   418    "type mismatch"
   419  )
   420  (assert_invalid
   421    (module (func $type-value-num-vs-num (result i32)
   422      (loop (result i32) (f32.const 0))
   423    ))
   424    "type mismatch"
   425  )
   426  (assert_invalid
   427    (module (func $type-value-unreached-select (result i32)
   428      (loop (result i64) (select (unreachable) (unreachable) (unreachable)))
   429    ))
   430    "type mismatch"
   431  )
   432  
   433  (assert_invalid
   434    (module
   435      (func $type-value-empty-in-block
   436        (i32.const 0)
   437        (block (loop (result i32)) (drop))
   438      )
   439    )
   440    "type mismatch"
   441  )
   442  (assert_invalid
   443    (module
   444      (func $type-value-empty-in-loop
   445        (i32.const 0)
   446        (loop (loop (result i32)) (drop))
   447      )
   448    )
   449    "type mismatch"
   450  )
   451  (assert_invalid
   452    (module
   453      (func $type-value-empty-in-then
   454        (i32.const 0) (i32.const 0)
   455        (if (then (loop (result i32)) (drop)))
   456      )
   457    )
   458    "type mismatch"
   459  )
   460  
   461  
   462  (assert_malformed
   463    (module quote "(func loop end $l)")
   464    "mismatching label"
   465  )
   466  (assert_malformed
   467    (module quote "(func loop $a end $l)")
   468    "mismatching label"
   469  )