From 890fb551d611cf797991eb2ef9a632e9397571e7 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 1 Jun 2023 19:05:30 +0900 Subject: [PATCH] switch from m4 to jinja --- ...many_param.wat.in => many_param.wat.jinja} | 71 ++++++++++--------- 1 file changed, 39 insertions(+), 32 deletions(-) rename wat/{many_param.wat.in => many_param.wat.jinja} (54%) diff --git a/wat/many_param.wat.in b/wat/many_param.wat.jinja similarity index 54% rename from wat/many_param.wat.in rename to wat/many_param.wat.jinja index 20f75f75..dd9ed55c 100644 --- a/wat/many_param.wat.in +++ b/wat/many_param.wat.jinja @@ -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 @@ -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