1#!/usr/bin/env bash
2
3# This script generates the `load_store_*.clif` test files.
4#
5# Usage:
6#
7#     $ ./make-load-store-tests.sh
8
9set -e
10cd $(dirname "$0")
11
12function main {
13    for spectre in "yes" "no"; do
14        for guard in "0" "0xffffffff"; do
15            for index_type in "i32" "i64"; do
16                for kind in "static" "dynamic"; do
17                    for access_type in "i32" "i8"; do
18                        for offset in "0" "0x1000" "0xffff0000"; do
19                            generate_one_test $kind $index_type $guard $spectre $access_type $offset x86_64 clif
20                            for target in "x86_64" "aarch64" "s390x" "riscv64"; do
21                                generate_one_test $kind $index_type $guard $spectre $access_type $offset $target compile
22                            done
23                        done
24                    done
25                done
26            done
27        done
28    done
29    echo "Done!"
30}
31
32function generate_one_test() {
33    local kind=$1
34    local index_type=$2
35    local guard=$3
36    local spectre=$4
37    local access_type=$5
38    local offset=$6
39    local target=$7
40    local test_kind=$8
41
42    local filename="load_store_${kind}_kind_${index_type}_index_${guard}_guard_${spectre}_spectre_${access_type}_access_${offset}_offset.wat"
43    if [[ $test_kind == "compile" ]]; then
44        local target_dir=${target}
45        if [[ $target == "x86_64" ]]; then
46            target_dir="x64"
47        fi
48        mkdir -p "${target_dir}"
49        filename="${target_dir}/${filename}"
50    fi
51    echo "Generating $filename..."
52
53    local flags=""
54    if [[ $spectre == "yes" ]]; then
55        flags="$flags -C cranelift-enable-heap-access-spectre-mitigation"
56    else
57        flags="$flags -C cranelift-enable-heap-access-spectre-mitigation=false"
58    fi
59
60    if [[ $index_type == "i64" ]]; then
61        flags="$flags -W memory64"
62    fi
63
64    local store_op=
65    local load_op=
66    case $access_type in
67        "i32")
68            store_op="i32.store"
69            load_op="i32.load"
70            ;;
71        "i8")
72            store_op="i32.store8"
73            load_op="i32.load8_u"
74            ;;
75    esac
76
77    local bound_global=""
78    local style=""
79    case $kind in
80        "dynamic")
81            flags="$flags -O static-memory-maximum-size=0"
82            ;;
83        "static")
84            flags="$flags -O static-memory-forced"
85        ;;
86    esac
87    flags="$flags -O static-memory-guard-size=$(($guard))"
88    flags="$flags -O dynamic-memory-guard-size=$(($guard))"
89
90    cat <<EOF > "$filename"
91;;! target = "${target}"
92;;! test = "${test_kind}"
93;;! flags = "${flags}"
94
95;; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
96;; !!! GENERATED BY 'make-load-store-tests.sh' DO NOT EDIT !!!
97;; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
98
99(module
100  (memory ${index_type} 1)
101
102  (func (export "do_store") (param ${index_type} i32)
103    local.get 0
104    local.get 1
105    ${store_op} offset=${offset})
106
107  (func (export "do_load") (param ${index_type}) (result i32)
108    local.get 0
109    ${load_op} offset=${offset}))
110
111;; TODO: run with the 'CRANELIFT_TEST_BLESS=1' env var set to update this test's
112;; expected output.
113EOF
114}
115
116main
117