Engine (cmd/compiler_exec.zi, compiler_opt.zi, compiler_lowered_verify.zi): - Storage that host calls return is given back once nothing the program can reach names it; 100,100 calls returning 64-byte strings peak at 65,752 live bytes instead of growing by 6.4 MB. - An address only loaded from or stored to later in its own block takes a scratch table entry instead of a permanent one, so programs that read millions of bytes of a global or of host text no longer hit the address limit. The optimizer drops block parameters every edge passes one value (float-text: 32,168 -> 1,031), and frame and plan records are read in place. float-text runs 63.9 -> 45.0 s locally. Host interface and embedding: - Types that hold themselves through a slice and procedure values are written in the interface section; when it cannot be written the build fails with a diagnostic naming the type instead of writing a bundle without it. Every entry-module global is lowered and named, so state export includes globals the entry never reads. Wasm tier (cmd/zir-wasm/zx_wasm_lower.c, zir_zx_wasm_web.c): - Recursive host types work; the tier stays out of Emscripten runtimes built with Asyncify or JSPI, where hosts suspend (Kryon's image and storage hosts trapped with "unreachable"), and the engine runs those. Kernels use real loops without dispatch tables. Benchmarks on Alpha in bench/wasm_tier.*, raw samples under bench/results/. Also: generated C indexes []*T slices with pointer elements; the site target matrix is regenerated for desktop and web (mobile waits for the phone's USB authorization); ASSEMBLY docs describe one ZX format; the package artifact test restores CC instead of unsetting it, since the artifact key names the compiler.
249 lines
8.7 KiB
Bash
249 lines
8.7 KiB
Bash
#!/bin/sh
|
|
set -eu
|
|
ulimit -c 0
|
|
|
|
ziran=${1:?pass the ziran command}
|
|
repo=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
|
mkdir -p "$repo/build/tests"
|
|
work=$(mktemp -d "$repo/build/tests/numeric-semantics-XXXXXX")
|
|
trap 'rm -rf "$work"' EXIT HUP INT TERM
|
|
|
|
cat > "$work/numeric.zi" <<'ZI'
|
|
// conformance: numeric.compile.div_min.s32
|
|
COMPILE_MIN_WORD :: #run cast(s32)(-2147483647 - 1) / cast(s32)(-1);
|
|
// conformance: numeric.compile.div_min.s64
|
|
COMPILE_MIN_LONG :: #run cast(s64)(-9223372036854775807 - 1) / cast(s64)(-1);
|
|
// conformance: numeric.compile.cast_wrap.u8
|
|
COMPILE_WRAPPED_BYTE :: #run cast(s8)(cast(u8)200);
|
|
// conformance: numeric.compile.wrap_add.u8
|
|
COMPILE_WRAPPED_ADD :: #run cast(u8)200 + cast(u8)100;
|
|
// conformance: numeric.compile.wrap_add.s8
|
|
COMPILE_SIGNED_ADD :: #run cast(s8)127 + cast(s8)1;
|
|
#assert COMPILE_MIN_WORD == cast(s32)(-2147483647 - 1)
|
|
#assert COMPILE_MIN_LONG == cast(s64)(-9223372036854775807 - 1)
|
|
#assert COMPILE_WRAPPED_BYTE == cast(s8)(-56)
|
|
#assert COMPILE_WRAPPED_ADD == cast(u8)44
|
|
#assert COMPILE_SIGNED_ADD == cast(s8)(-128)
|
|
|
|
#program_export
|
|
Answer :: () -> s32 {
|
|
byte: u8 = cast(u8)1
|
|
half: u16 = cast(u16)1
|
|
negative_byte: s8 = cast(s8)(-1)
|
|
negative_half: s16 = cast(s16)(-1)
|
|
// conformance: numeric.shift.left.u8
|
|
// conformance: numeric.shift.left.u16
|
|
// conformance: numeric.shift.right.arithmetic.s8
|
|
// conformance: numeric.shift.right.arithmetic.s16
|
|
if (byte << cast(u8)7) != cast(u8)128 ||
|
|
(half << cast(u16)15) != cast(u16)32768 ||
|
|
(negative_byte >> cast(s8)1) != negative_byte ||
|
|
(negative_half >> cast(s16)1) != negative_half { return 1 }
|
|
// conformance: numeric.float.cast.u16
|
|
// conformance: numeric.float.cast.s8
|
|
if cast(u16)(65535.0) != cast(u16)65535 ||
|
|
cast(s8)(-128.0) != cast(s8)(-128) { return 2 }
|
|
one: float64 = 1.0
|
|
zero: float64 = 0.0
|
|
infinity: float64 = one / zero
|
|
nan: float64 = zero / zero
|
|
// conformance: numeric.float.divide.zero.f64
|
|
// conformance: numeric.float.compare.nan
|
|
if infinity <= one || nan == nan { return 3 }
|
|
small_one: float32 = 1.0
|
|
small_zero: float32 = 0.0
|
|
small_infinity: float32 = small_one / small_zero
|
|
// conformance: numeric.float.divide.zero.f32
|
|
if small_infinity <= small_one { return 4 }
|
|
signed_byte: s8 = cast(s8)127
|
|
unsigned_byte: u8 = cast(u8)255
|
|
signed_word: s32 = cast(s32)2147483647
|
|
// conformance: numeric.wrap.add.u8
|
|
// conformance: numeric.wrap.add.s8
|
|
// conformance: numeric.wrap.sub.u8
|
|
// conformance: numeric.wrap.mul.s8
|
|
// conformance: numeric.wrap.add.s32
|
|
if signed_byte + cast(s8)1 != cast(s8)(-128) ||
|
|
unsigned_byte + cast(u8)1 != cast(u8)0 ||
|
|
cast(u8)0 - cast(u8)1 != unsigned_byte ||
|
|
cast(s8)100 * cast(s8)3 != cast(s8)44 ||
|
|
signed_word + cast(s32)1 != cast(s32)(-2147483647 - 1) {
|
|
return 5
|
|
}
|
|
minimum_word: s32 = cast(s32)(-2147483647 - 1)
|
|
minimum_long: s64 = cast(s64)(-9223372036854775807 - 1)
|
|
minus_one_word: s32 = cast(s32)(-1)
|
|
minus_one_long: s64 = cast(s64)(-1)
|
|
// conformance: numeric.div_min.s32
|
|
// conformance: numeric.rem_min.s32
|
|
// conformance: numeric.div_min.s64
|
|
// conformance: numeric.rem_min.s64
|
|
if minimum_word / minus_one_word != minimum_word ||
|
|
minimum_word % minus_one_word != cast(s32)0 ||
|
|
minimum_long / minus_one_long != minimum_long ||
|
|
minimum_long % minus_one_long != cast(s64)0 { return 6 }
|
|
// conformance: numeric.cast.width_pairs.all
|
|
if cast(s8)(cast(u8)200) != cast(s8)(-56) ||
|
|
cast(u8)(cast(s8)(-1)) != cast(u8)255 ||
|
|
cast(s16)(cast(u16)40000) != cast(s16)(-25536) ||
|
|
cast(u16)(cast(s16)(-1)) != cast(u16)65535 ||
|
|
cast(s32)(cast(u32)4294967295) != cast(s32)(-1) ||
|
|
cast(u32)(cast(s32)(-1)) != cast(u32)4294967295 ||
|
|
cast(s64)(cast(u64)18446744073709551615) != cast(s64)(-1) ||
|
|
cast(u64)(cast(s64)(-1)) != cast(u64)18446744073709551615 {
|
|
return 7
|
|
}
|
|
// conformance: numeric.mixed.add.u16_to_u8
|
|
// conformance: numeric.mixed.add.u8_to_u16
|
|
if cast(u8)(cast(u16)300 + cast(u16)12) != cast(u8)56 ||
|
|
cast(u16)(cast(u8)255 + cast(u8)2) != cast(u16)1 { return 8 }
|
|
bits_left: u8 = cast(u8)12
|
|
bits_right: u8 = cast(u8)10
|
|
// conformance: numeric.bitwise.and.u8
|
|
// conformance: numeric.bitwise.or.u8
|
|
// conformance: numeric.bitwise.xor.u8
|
|
if (bits_left & bits_right) != cast(u8)8 ||
|
|
(bits_left | bits_right) != cast(u8)14 ||
|
|
(bits_left ^ bits_right) != cast(u8)6 { return 9 }
|
|
return 42
|
|
}
|
|
|
|
FloatLiteral :: (x: float32) -> float32 {
|
|
return x * 0.5 + x * 0.3
|
|
}
|
|
|
|
#program_export
|
|
Float32Literal :: () -> s32 {
|
|
// Bits 0x3e991279 give 0x3e74ea5c; a double chain gives 0x3e74ea5b.
|
|
input: float32 = 0.2989690601825714
|
|
want: float32 = 0.23917526006698608
|
|
// conformance: numeric.float.literal.f32
|
|
if FloatLiteral(input) != want { return 1 }
|
|
return 0
|
|
}
|
|
|
|
#program_export
|
|
Bad8 :: () -> s32 {
|
|
value: u8 = cast(u8)1
|
|
amount: u8 = cast(u8)8
|
|
// conformance: numeric.shift.reject.width
|
|
return cast(s32)(value << amount)
|
|
}
|
|
|
|
#program_export
|
|
Bad16 :: () -> s32 {
|
|
value: u16 = cast(u16)1
|
|
amount: u16 = cast(u16)16
|
|
// conformance: numeric.shift.reject.width
|
|
return cast(s32)(value << amount)
|
|
}
|
|
|
|
#program_export
|
|
BadNegative :: () -> s32 {
|
|
value: s16 = cast(s16)1
|
|
amount: s16 = cast(s16)(-1)
|
|
// conformance: numeric.shift.reject.negative
|
|
return cast(s32)(value << amount)
|
|
}
|
|
ZI
|
|
|
|
"$ziran" capabilities --json > "$work/capabilities.json"
|
|
"$ziran" run --target=c --root "$repo/tests" "$repo/tests/native_inline_cases.zi" -- "$ziran" "$work" numeric-ids
|
|
|
|
"$ziran" ir --root "$work" -o "$work/ir" "$work/numeric.zi"
|
|
|
|
for entry in Answer Bad8 Bad16 BadNegative Float32Literal; do
|
|
for input in source saved; do
|
|
if test "$input" = source; then
|
|
root=$work
|
|
module=$work/numeric.zi
|
|
else
|
|
root=$work/ir
|
|
module=$work/ir/numeric.zir
|
|
fi
|
|
"$ziran" bundle --root "$root" --entry "numeric:$entry" \
|
|
-o "$work/$input-$entry.zx" "$module"
|
|
done
|
|
cmp "$work/source-$entry.zx" "$work/saved-$entry.zx"
|
|
done
|
|
|
|
for input in source saved; do
|
|
if test "$("$ziran" run "$work/$input-Answer.zx")" != 42; then
|
|
echo 'portable numeric result differs from native targets' >&2
|
|
exit 1
|
|
fi
|
|
if test "$("$ziran" run "$work/$input-Float32Literal.zx")" != 0; then
|
|
echo 'portable runner widened float32 literal arithmetic to double' >&2
|
|
exit 1
|
|
fi
|
|
for entry in Bad8 Bad16 BadNegative; do
|
|
if "$ziran" run "$work/$input-$entry.zx" > "$work/vm-$entry.log" 2>&1; then
|
|
echo "portable runner accepted an oversized $entry shift" >&2
|
|
exit 1
|
|
fi
|
|
done
|
|
done
|
|
|
|
for input in source saved; do
|
|
if test "$input" = source; then
|
|
root=$work
|
|
module=$work/numeric.zi
|
|
else
|
|
root=$work/ir
|
|
module=$work/ir/numeric.zir
|
|
fi
|
|
for target in c go; do
|
|
output=$work/$target-$input
|
|
if test "$target" = go; then
|
|
"$ziran" build --target=go --pkg main --no-main --root "$root" \
|
|
-o "$output" "$module"
|
|
cat > "$output/main.go" <<'GO'
|
|
package main
|
|
import "os"
|
|
func main() {
|
|
if len(os.Args) > 1 {
|
|
if os.Args[1] == "float32" {
|
|
if Numeric_Float32Literal() != 0 { panic("float32 literal") }
|
|
return
|
|
}
|
|
if os.Args[1] == "8" { Numeric_Bad8(); return }
|
|
if os.Args[1] == "negative" { Numeric_BadNegative(); return }
|
|
Numeric_Bad16(); return
|
|
}
|
|
if Numeric_Answer() != 42 { panic("numeric result") }
|
|
}
|
|
GO
|
|
GO111MODULE=off go build -o "$output/app" "$output/numeric.go" "$output/main.go"
|
|
elif test "$target" = c; then
|
|
"$ziran" build --target=c --no-main --root "$root" \
|
|
-o "$output" "$module"
|
|
cat > "$output/main.c" <<'C'
|
|
#include "numeric.h"
|
|
int main(int argc, char **argv) {
|
|
if (argc > 1) {
|
|
if (argv[1][0] == 'f') return Float32Literal();
|
|
if (argv[1][0] == '8') return Bad8();
|
|
if (argv[1][0] == 'n') return BadNegative();
|
|
return Bad16();
|
|
}
|
|
return Answer() == 42 ? 0 : 1;
|
|
}
|
|
C
|
|
"${CC:-cc}" -std=c99 -I"$repo/include" -I"$output" \
|
|
"$output/numeric.c" "$output/main.c" -lm -o "$output/app"
|
|
fi
|
|
"$output/app"
|
|
if ! "$output/app" float32; then
|
|
echo "$target widened float32 literal arithmetic to double" >&2
|
|
exit 1
|
|
fi
|
|
for amount in 8 16 negative; do
|
|
if "$output/app" "$amount" > "$output/bad-$amount.log" 2>&1; then
|
|
echo "$target accepted an oversized $amount-bit shift" >&2
|
|
exit 1
|
|
fi
|
|
done
|
|
done
|
|
|
|
done
|