Skip to content

Commit

Permalink
switch from m4 to jinja
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed Jun 1, 2023
1 parent 240ea59 commit 890fb55
Showing 1 changed file with 39 additions and 32 deletions.
71 changes: 39 additions & 32 deletions wat/many_param.wat.in → wat/many_param.wat.jinja
Original file line number Diff line number Diff line change
@@ -1,49 +1,56 @@
;; Use many parameters and results

define(`_repeat',`ifelse(eval($1<=$2),1,`$3`'$0(incr($1),$2,`$3')')')dnl
define(`repeat',`_repeat(1,$1,``$2'')')dnl

define(`_for',`ifelse(eval($2<$3),1,`pushdef(`$1',$2)$4`'popdef(`$1')`'$0($1,incr($2),$3,`$4')')')dnl
define(`for',`_for($1,$2,$3,`$4')')dnl

;; wamrc: 64
;; https://github.com/bytecodealliance/wasm-micro-runtime/blob/a2d4744a2b2c587eacca66c357dc2e88925fcadd/core/iwasm/compilation/aot_emit_function.c#L708-L712
;; m4 -DNUM=64 many_param.wat.in | wat2wasm -o many_param64.wasm -
;; jinja2 -DNUM=64 many_param.wat.jinja | wat2wasm -o many_param64.wasm -
;;
;; wamr also has various limits around UINT16_MAX / INT16_MAX.
;;
;; wasmtime: 1000
;; https://github.com/bytecodealliance/wasm-tools/blob/5e8639a37260cdc1aab196f5ae5c7ae6427c214f/crates/wasmparser/src/limits.rs#L29-L30
ifdef(`NUM',,`define(`NUM',65535)')
{% set NUM = NUM | default(65535) | int %}

{% set INPUT = 100000 %}

define(`INPUT',100000)
dnl (INPUT + NUM - 1) + (INPUT + NUM - 2) + ... + (INPUT + 0)
dnl = INPUT * NUM + NUM * (NUM - 1) / 2
dnl = 100000 * 65535 + 65535 * (65535 - 1) / 2
dnl = 8700885345
dnl = 110950753 (32-bit truncation)
dnl Note: m4 eval is based on signed 32-bit, which is
dnl not enough for large NUM.
define(`EXPECTED',`syscmd(echo "(INPUT * NUM + NUM * (NUM - 1) / 2) % 4294967296" | bc)')
{#
(INPUT + NUM - 1) + (INPUT + NUM - 2) + ... + (INPUT + 0)
= INPUT * NUM + NUM * (NUM - 1) / 2
= 100000 * 65535 + 65535 * (65535 - 1) / 2
= 8700885345
= 110950753 (32-bit truncation)
#}
{% set EXPECTED = (INPUT * NUM + NUM * (NUM - 1) // 2) % 4294967296 %}

(module
(type $add_many_params_type (func (param repeat(NUM, i32 )) (result i32)))
(type $pass_many_values_type (func (param repeat(NUM, i32 )) (result repeat(NUM, i32 ))))
(type $return_many_results_type (func (param i32) (result repeat(NUM, i32 ))))
(type $add_many_params_type (func (param
{%- for x in range(NUM) %} i32{% endfor -%}
) (result i32)))
(type $pass_many_values_type (func (param
{%- for x in range(NUM) %} i32{% endfor -%}
) (result
{%- for x in range(NUM) %} i32{% endfor -%}
)))
(type $return_many_results_type (func (param i32) (result
{%- for x in range(NUM) %} i32{% endfor -%}
)))

(func $add_many_params (export "add_many_params") (type $add_many_params_type)
for(`X', 0, NUM, `
local.get X')
repeat(decr(NUM), `
i32.add')
{% for x in range(NUM) -%}
local.get {{x}}
{% endfor -%}
{% for x in range(NUM-1) -%}
i32.add
{% endfor -%}
)
(func $pass_many_values (export "pass_many_values") (type $pass_many_values_type)
for(`X', 0, NUM, `
local.get X')
{% for x in range(NUM) -%}
local.get {{x}}
{% endfor -%}
)
(func $return_many_results (export "return_many_results") (type $return_many_results_type)
for(`X', 0, NUM, `
(i32.add (i32.const X) (local.get 0))')
{% for x in range(NUM) -%}
(i32.add (i32.const {{x}}) (local.get 0))
{% endfor -%}
)
(func $test (export "test") (param i32) (result i32)
local.get 0
Expand All @@ -61,16 +68,16 @@ for(`X', 0, NUM, `
call_indirect (type $add_many_params_type)
)
(func (export "_start")
i32.const EXPECTED
i32.const INPUT
i32.const {{EXPECTED}}
i32.const {{INPUT}}
call $test
block (param i32 i32)
i32.eq
br_if 0
unreachable
end
i32.const EXPECTED
i32.const INPUT
i32.const {{EXPECTED}}
i32.const {{INPUT}}
call $test_indirect
block (param i32 i32)
i32.eq
Expand Down

0 comments on commit 890fb55

Please sign in to comment.