From b051e706728150c410d43942b2c437780aa3132e Mon Sep 17 00:00:00 2001 From: dcode Date: Tue, 9 May 2023 11:12:35 +0200 Subject: [PATCH 1/3] Move bswap back to stdlib --- src/builtins.ts | 177 ------------------------- std/assembly/builtins.ts | 4 - std/assembly/polyfills.ts | 27 ++++ tests/compiler/issues/2700.debug.wat | 95 +++++++++++++ tests/compiler/issues/2700.json | 4 + tests/compiler/issues/2700.release.wat | 6 + tests/compiler/issues/2700.ts | 13 ++ 7 files changed, 145 insertions(+), 181 deletions(-) create mode 100644 std/assembly/polyfills.ts create mode 100644 tests/compiler/issues/2700.debug.wat create mode 100644 tests/compiler/issues/2700.json create mode 100644 tests/compiler/issues/2700.release.wat create mode 100644 tests/compiler/issues/2700.ts diff --git a/src/builtins.ts b/src/builtins.ts index e2027b4d9c..e9f563e1e6 100644 --- a/src/builtins.ts +++ b/src/builtins.ts @@ -143,8 +143,6 @@ export namespace BuiltinNames { export const isManaged = "~lib/builtins/isManaged"; export const isVoid = "~lib/builtins/isVoid"; - export const bswap = "~lib/builtins/bswap"; - export const add = "~lib/builtins/add"; export const sub = "~lib/builtins/sub"; export const mul = "~lib/builtins/mul"; @@ -1193,181 +1191,6 @@ function builtin_idof(ctx: BuiltinFunctionContext): ExpressionRef { } builtinFunctions.set(BuiltinNames.idof, builtin_idof); -// bswap(value: T) -> T -function builtin_bswap(ctx: BuiltinFunctionContext): ExpressionRef { - let compiler = ctx.compiler; - let module = compiler.module; - if ( - checkTypeOptional(ctx, true) | - checkArgsRequired(ctx, 1) - ) return module.unreachable(); - - let typeArguments = ctx.typeArguments; - let arg0 = typeArguments - ? compiler.compileExpression( - ctx.operands[0], - typeArguments[0].toUnsigned(), - Constraints.ConvImplicit | Constraints.MustWrap - ) - : compiler.compileExpression( - ctx.operands[0], - Type.u32, - Constraints.MustWrap - ); - - let type = compiler.currentType; - if (type.isValue) { - switch (type.kind) { - case TypeKind.Bool: - case TypeKind.I8: - case TypeKind.U8: return arg0; - case TypeKind.I16: - case TypeKind.U16: { - // (x << 8 | x >> 8) - let flow = compiler.currentFlow; - let temp = flow.getTempLocal(type); - flow.setLocalFlag(temp.index, LocalFlags.Wrapped); - - let res = module.binary( - BinaryOp.OrI32, - module.binary( - BinaryOp.ShlI32, - module.local_tee(temp.index, arg0, false), - module.i32(8) - ), - module.binary( - BinaryOp.ShrU32, - module.local_get(temp.index, TypeRef.I32), - module.i32(8) - ) - ); - // avoid wrapping for u16 due to it's already done for input arg - if (type.kind == TypeKind.I16) { - res = compiler.ensureSmallIntegerWrap(res, Type.i16); - } - return res; - } - case TypeKind.I32: - case TypeKind.U32: - case TypeKind.Isize: - case TypeKind.Usize: { - if (type.size == 32) { - // rotl(x & 0xFF00FF00, 8) | rotr(x & 0x00FF00FF, 8) - let flow = compiler.currentFlow; - let temp = flow.getTempLocal(type); - flow.setLocalFlag(temp.index, LocalFlags.Wrapped); - - let res = module.binary( - BinaryOp.OrI32, - module.binary( - BinaryOp.RotlI32, - module.binary( - BinaryOp.AndI32, - module.local_tee(temp.index, arg0, false), - module.i32(0xFF00FF00) - ), - module.i32(8) - ), - module.binary( - BinaryOp.RotrI32, - module.binary( - BinaryOp.AndI32, - module.local_get(temp.index, TypeRef.I32), - module.i32(0x00FF00FF) - ), - module.i32(8) - ), - ); - return res; - } - // fall-through - } - case TypeKind.I64: - case TypeKind.U64: { - // let t = - // ((x >>> 8) & 0x00FF00FF00FF00FF) | - // ((x & 0x00FF00FF00FF00FF) << 8) - // - // let res = - // ((t >>> 16) & 0x0000FFFF0000FFFF) | - // ((t & 0x0000FFFF0000FFFF) << 16) - // - // rotr(res, 32) - - let flow = compiler.currentFlow; - let temp1 = flow.getTempLocal(type); - flow.setLocalFlag(temp1.index, LocalFlags.Wrapped); - let temp2 = flow.getTempLocal(type); - flow.setLocalFlag(temp2.index, LocalFlags.Wrapped); - - // t = ((x >>> 8) & 0x00FF00FF00FF00FF) | ((x & 0x00FF00FF00FF00FF) << 8) - let expr = module.local_tee( - temp2.index, - module.binary( - BinaryOp.OrI64, - module.binary( - BinaryOp.AndI64, - module.binary( - BinaryOp.ShrU64, - module.local_tee(temp1.index, arg0, false), - module.i64(8) - ), - module.i64(0x00FF00FF, 0x00FF00FF) - ), - module.binary( - BinaryOp.ShlI64, - module.binary( - BinaryOp.AndI64, - module.local_get(temp1.index, TypeRef.I64), - module.i64(0x00FF00FF, 0x00FF00FF) - ), - module.i64(8) - ), - ), - false - ); - - // ((t >>> 16) & 0x0000FFFF0000FFFF) | ((t & 0x0000FFFF0000FFFF) << 16) - let res = module.binary( - BinaryOp.OrI64, - module.binary( - BinaryOp.AndI64, - module.binary( - BinaryOp.ShrU64, - expr, - module.i64(16) - ), - module.i64(0x0000FFFF, 0x0000FFFF) - ), - module.binary( - BinaryOp.ShlI64, - module.binary( - BinaryOp.AndI64, - module.local_get(temp2.index, TypeRef.I64), - module.i64(0x0000FFFF, 0x0000FFFF) - ), - module.i64(16) - ), - ); - - // rotr(res, 32) - res = module.binary( - BinaryOp.RotrI64, - res, - module.i64(32) - ); - return res; - } - } - } - compiler.error( - DiagnosticCode.Operation_0_cannot_be_applied_to_type_1, - ctx.reportNode.typeArgumentsRange, "bswap", type.toString() - ); - return module.unreachable(); -} -builtinFunctions.set(BuiltinNames.bswap, builtin_bswap); - // === Math =================================================================================== // NaN diff --git a/std/assembly/builtins.ts b/std/assembly/builtins.ts index ef81c80a02..0f910a39e4 100644 --- a/std/assembly/builtins.ts +++ b/std/assembly/builtins.ts @@ -66,10 +66,6 @@ export declare function isVoid(): bool; @builtin export declare function lengthof(func?: T): i32; -// @ts-ignore -@builtin -export declare function bswap(value: T): T; - // @ts-ignore: decorator @builtin export declare function clz(value: T): T; diff --git a/std/assembly/polyfills.ts b/std/assembly/polyfills.ts new file mode 100644 index 0000000000..2f39f1f207 --- /dev/null +++ b/std/assembly/polyfills.ts @@ -0,0 +1,27 @@ +export function bswap(value: T): T { + if (isInteger()) { + if (sizeof() == 1) { + return value; + } + if (sizeof() == 2) { + return (value << 8 | (value >> 8)); + } + if (sizeof() == 4) { + return ( + rotl(value & 0xFF00FF00, 8) | + rotr(value & 0x00FF00FF, 8) + ); + } + if (sizeof() == 8) { + let a = (value >> 8) & 0x00FF00FF00FF00FF; + let b = (value & 0x00FF00FF00FF00FF) << 8; + let v = a | b; + + a = (v >>> 16) & 0x0000FFFF0000FFFF; + b = (v & 0x0000FFFF0000FFFF) << 16; + + return rotr(a | b, 32); + } + } + ERROR("Unsupported generic type"); +} diff --git a/tests/compiler/issues/2700.debug.wat b/tests/compiler/issues/2700.debug.wat new file mode 100644 index 0000000000..d61b1a47c9 --- /dev/null +++ b/tests/compiler/issues/2700.debug.wat @@ -0,0 +1,95 @@ +(module + (type $none_=>_none (func)) + (type $i32_=>_i32 (func (param i32) (result i32))) + (type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32))) + (import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32))) + (global $issues/2700/value i32 (i32.const -32768)) + (global $~lib/memory/__data_end i32 (i32.const 60)) + (global $~lib/memory/__stack_pointer (mut i32) (i32.const 32828)) + (global $~lib/memory/__heap_base i32 (i32.const 32828)) + (memory $0 1) + (data $0 (i32.const 12) ",\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\1c\00\00\00i\00s\00s\00u\00e\00s\00/\002\007\000\000\00.\00t\00s\00") + (table $0 1 1 funcref) + (elem $0 (i32.const 1)) + (export "memory" (memory $0)) + (start $~start) + (func $~lib/polyfills/bswap (param $value i32) (result i32) + i32.const 1 + drop + i32.const 2 + i32.const 1 + i32.eq + drop + i32.const 2 + i32.const 2 + i32.eq + drop + local.get $value + i32.const 8 + i32.const 15 + i32.and + i32.shl + local.get $value + i32.const 65535 + i32.and + i32.const 8 + i32.const 15 + i32.and + i32.shr_u + i32.or + return + ) + (func $start:issues/2700 + global.get $issues/2700/value + i32.const 32768 + i32.extend16_s + i32.eq + drop + global.get $issues/2700/value + call $~lib/polyfills/bswap + i32.extend16_s + i32.const 128 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 7 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $issues/2700/value + call $~lib/polyfills/bswap + i32.extend16_s + i32.const 128 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 10 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $issues/2700/value + call $~lib/polyfills/bswap + i32.extend16_s + i32.const 65408 + i32.extend16_s + i32.ne + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 13 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + ) + (func $~start + call $start:issues/2700 + ) +) diff --git a/tests/compiler/issues/2700.json b/tests/compiler/issues/2700.json new file mode 100644 index 0000000000..1bdd02b1be --- /dev/null +++ b/tests/compiler/issues/2700.json @@ -0,0 +1,4 @@ +{ + "asc_flags": [ + ] +} diff --git a/tests/compiler/issues/2700.release.wat b/tests/compiler/issues/2700.release.wat new file mode 100644 index 0000000000..cff899b223 --- /dev/null +++ b/tests/compiler/issues/2700.release.wat @@ -0,0 +1,6 @@ +(module + (memory $0 1) + (data $0 (i32.const 1036) ",") + (data $0.1 (i32.const 1048) "\02\00\00\00\1c\00\00\00i\00s\00s\00u\00e\00s\00/\002\007\000\000\00.\00t\00s") + (export "memory" (memory $0)) +) diff --git a/tests/compiler/issues/2700.ts b/tests/compiler/issues/2700.ts new file mode 100644 index 0000000000..ffa1ce5cc8 --- /dev/null +++ b/tests/compiler/issues/2700.ts @@ -0,0 +1,13 @@ +const value: i16 = -32768; + +// [ 128 0 ] +assert(value == 0x8000); + +// [ 0 128 ], asserts in both <=0.19 and >=0.20 +assert(bswap(value) == 0x0080); + +// [ 0 128 ], asserts in <=0.19, fails in >=0.20 +assert(bswap(value) == 0x0080); + +// [ 255 128 ], fails in >=0.20, asserts in <=0.19 +assert(bswap(value) != 0xff80); From 6624ec5a6a56bfbdb39900fd616a5a9bee5981ae Mon Sep 17 00:00:00 2001 From: dcode Date: Tue, 9 May 2023 11:23:18 +0200 Subject: [PATCH 2/3] update tests --- .../bindings/noExportRuntime.debug.js | 2 +- tests/compiler/builtins.debug.wat | 466 ++++-------- tests/compiler/builtins.ts | 22 - tests/compiler/issues/2700.debug.wat | 95 --- tests/compiler/issues/2700.release.wat | 6 - tests/compiler/issues/2700.ts | 13 - tests/compiler/polyfills.debug.wat | 511 ++++++++++++++ .../{issues/2700.json => polyfills.json} | 0 tests/compiler/polyfills.release.wat | 6 + tests/compiler/polyfills.ts | 23 + tests/compiler/std/array.debug.wat | 135 ++-- tests/compiler/std/dataview.debug.wat | 662 +++++++----------- tests/compiler/std/dataview.release.wat | 81 +-- tests/compiler/std/typedarray.debug.wat | 134 ++-- 14 files changed, 1132 insertions(+), 1024 deletions(-) delete mode 100644 tests/compiler/issues/2700.debug.wat delete mode 100644 tests/compiler/issues/2700.release.wat delete mode 100644 tests/compiler/issues/2700.ts create mode 100644 tests/compiler/polyfills.debug.wat rename tests/compiler/{issues/2700.json => polyfills.json} (100%) create mode 100644 tests/compiler/polyfills.release.wat create mode 100644 tests/compiler/polyfills.ts diff --git a/tests/compiler/bindings/noExportRuntime.debug.js b/tests/compiler/bindings/noExportRuntime.debug.js index dda4a04e3d..9db98d97b4 100644 --- a/tests/compiler/bindings/noExportRuntime.debug.js +++ b/tests/compiler/bindings/noExportRuntime.debug.js @@ -159,7 +159,7 @@ export const { isArrayOfArray, returnsArrayOfArray, takesNonPlainObject, - takesFunction + takesFunction, } = await (async url => instantiate( await (async () => { try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } diff --git a/tests/compiler/builtins.debug.wat b/tests/compiler/builtins.debug.wat index 6261e34723..fd352cf9b9 100644 --- a/tests/compiler/builtins.debug.wat +++ b/tests/compiler/builtins.debug.wat @@ -551,16 +551,6 @@ (local $51 i32) (local $52 i32) (local $53 i32) - (local $54 i32) - (local $55 i32) - (local $56 i32) - (local $57 i64) - (local $58 i64) - (local $59 i64) - (local $60 i64) - (local $61 i32) - (local $62 i32) - (local $63 i32) global.get $~lib/memory/__stack_pointer i32.const 12 i32.sub @@ -2543,11 +2533,11 @@ i32.const 2 i32.const 3 global.get $builtins/fn - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 call $~lib/function/Function<%28i32%2Ci32%29=>i32>#get:index call_indirect $0 (type $i32_i32_=>_i32) i32.eq @@ -2561,23 +2551,23 @@ unreachable end global.get $builtins/fn - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=8 - local.get $63 + local.get $53 call $~lib/function/Function<%28i32%2Ci32%29=>i32>#get:name - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 32 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -2589,11 +2579,11 @@ unreachable end global.get $builtins/fn - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 call $~lib/function/Function<%28i32%2Ci32%29=>i32>#get:length i32.const 2 i32.eq @@ -2607,11 +2597,11 @@ unreachable end global.get $builtins/fn - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 call $~lib/function/Function<%28i32%2Ci32%29=>i32>#get:length i32.const 2 i32.eq @@ -2625,23 +2615,23 @@ unreachable end global.get $builtins/fn - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=8 - local.get $63 + local.get $53 call $~lib/function/Function<%28i32%2Ci32%29=>i32>#toString - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 176 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3283,11 +3273,11 @@ i32.const 52 local.set $52 i32.const 256 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 5 local.get $48 f64.convert_i32_u @@ -3349,17 +3339,17 @@ unreachable end i32.const 352 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 352 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3371,17 +3361,17 @@ unreachable end i32.const 352 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 352 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3393,17 +3383,17 @@ unreachable end i32.const 400 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 400 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3415,17 +3405,17 @@ unreachable end i32.const 432 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 432 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3437,17 +3427,17 @@ unreachable end i32.const 464 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 464 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3459,17 +3449,17 @@ unreachable end i32.const 496 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 496 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3481,17 +3471,17 @@ unreachable end i32.const 528 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 528 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3503,17 +3493,17 @@ unreachable end i32.const 560 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 560 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3525,17 +3515,17 @@ unreachable end i32.const 592 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 592 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3547,17 +3537,17 @@ unreachable end i32.const 624 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 624 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3569,17 +3559,17 @@ unreachable end i32.const 656 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 656 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3591,17 +3581,17 @@ unreachable end i32.const 688 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 688 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3613,17 +3603,17 @@ unreachable end i32.const 720 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 720 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3635,17 +3625,17 @@ unreachable end i32.const 752 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 752 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3657,17 +3647,17 @@ unreachable end i32.const 784 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 784 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3679,17 +3669,17 @@ unreachable end i32.const 816 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 816 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3701,17 +3691,17 @@ unreachable end i32.const 848 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 848 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3723,17 +3713,17 @@ unreachable end i32.const 880 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 880 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3745,17 +3735,17 @@ unreachable end i32.const 432 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 432 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3767,17 +3757,17 @@ unreachable end i32.const 352 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 - local.get $63 + local.get $53 i32.const 352 - local.set $63 + local.set $53 global.get $~lib/memory/__stack_pointer - local.get $63 + local.get $53 i32.store $0 offset=4 - local.get $63 + local.get $53 call $~lib/string/String.__eq i32.eqz if @@ -3914,162 +3904,6 @@ call $~lib/builtins/abort unreachable end - i32.const 170 - i32.const 170 - i32.eq - drop - i32.const 170 - i32.extend8_s - i32.const 255 - i32.and - i32.const 170 - i32.eq - drop - i32.const 43707 - local.tee $53 - i32.const 8 - i32.shl - local.get $53 - i32.const 8 - i32.shr_u - i32.or - i32.const 65535 - i32.and - i32.const 48042 - i32.eq - drop - i32.const 43707 - i32.extend16_s - i32.const 65535 - i32.and - local.tee $54 - i32.const 8 - i32.shl - local.get $54 - i32.const 8 - i32.shr_u - i32.or - i32.const 65535 - i32.and - i32.const 48042 - i32.eq - drop - i32.const -1430532899 - local.tee $55 - i32.const -16711936 - i32.and - i32.const 8 - i32.rotl - local.get $55 - i32.const 16711935 - i32.and - i32.const 8 - i32.rotr - i32.or - i32.const -573785174 - i32.eq - drop - i32.const -1430532899 - local.tee $56 - i32.const -16711936 - i32.and - i32.const 8 - i32.rotl - local.get $56 - i32.const 16711935 - i32.and - i32.const 8 - i32.rotr - i32.or - i32.const -573785174 - i32.eq - drop - i64.const 4822679907192029 - local.tee $57 - i64.const 8 - i64.shr_u - i64.const 71777214294589695 - i64.and - local.get $57 - i64.const 71777214294589695 - i64.and - i64.const 8 - i64.shl - i64.or - local.tee $58 - i64.const 16 - i64.shr_u - i64.const 281470681808895 - i64.and - local.get $58 - i64.const 281470681808895 - i64.and - i64.const 16 - i64.shl - i64.or - i64.const 32 - i64.rotr - i64.const -2464388556401798912 - i64.eq - drop - i64.const 4822679907192029 - local.tee $59 - i64.const 8 - i64.shr_u - i64.const 71777214294589695 - i64.and - local.get $59 - i64.const 71777214294589695 - i64.and - i64.const 8 - i64.shl - i64.or - local.tee $60 - i64.const 16 - i64.shr_u - i64.const 281470681808895 - i64.and - local.get $60 - i64.const 281470681808895 - i64.and - i64.const 16 - i64.shl - i64.or - i64.const 32 - i64.rotr - i64.const -2464388556401798912 - i64.eq - drop - i32.const -1430532899 - local.tee $61 - i32.const -16711936 - i32.and - i32.const 8 - i32.rotl - local.get $61 - i32.const 16711935 - i32.and - i32.const 8 - i32.rotr - i32.or - i32.const -573785174 - i32.eq - drop - i32.const -1430532899 - local.tee $62 - i32.const -16711936 - i32.and - i32.const 8 - i32.rotl - local.get $62 - i32.const 16711935 - i32.and - i32.const 8 - i32.rotr - i32.or - i32.const -573785174 - i32.eq - drop global.get $~lib/memory/__stack_pointer i32.const 12 i32.add diff --git a/tests/compiler/builtins.ts b/tests/compiler/builtins.ts index 35c680b763..dafb8f9309 100644 --- a/tests/compiler/builtins.ts +++ b/tests/compiler/builtins.ts @@ -676,25 +676,3 @@ function rotr3(a: i8, b: i8, c: i8): i32 { return rotr(a, rotr(b, c)); } assert(rotr3(48, 8, 1) == 3); - -// bswap tests - -// check bswap for i8/u8 -assert(bswap(0xaa) == 0xaa); -assert(bswap(0xaa) == 0xaa); - -// check bswap for i16/u16 -assert(bswap(0xaabb) == 0xbbaa); -assert(bswap(0xaabb) == 0xbbaa); - -// check bswap for i32/u32 -assert(bswap(0xaabbccdd) == 0xddccbbaa); -assert(bswap(0xaabbccdd) == 0xddccbbaa); - -// check bswap for i64/u64 -assert(bswap(0x00112233aabbccdd) == 0xddccbbaa33221100); -assert(bswap(0x00112233aabbccdd) == 0xddccbbaa33221100); - -// check bswap for isize/usize -assert(bswap(0xaabbccdd) == 0xddccbbaa); -assert(bswap(0xaabbccdd) == 0xddccbbaa); diff --git a/tests/compiler/issues/2700.debug.wat b/tests/compiler/issues/2700.debug.wat deleted file mode 100644 index d61b1a47c9..0000000000 --- a/tests/compiler/issues/2700.debug.wat +++ /dev/null @@ -1,95 +0,0 @@ -(module - (type $none_=>_none (func)) - (type $i32_=>_i32 (func (param i32) (result i32))) - (type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32))) - (import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32))) - (global $issues/2700/value i32 (i32.const -32768)) - (global $~lib/memory/__data_end i32 (i32.const 60)) - (global $~lib/memory/__stack_pointer (mut i32) (i32.const 32828)) - (global $~lib/memory/__heap_base i32 (i32.const 32828)) - (memory $0 1) - (data $0 (i32.const 12) ",\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\1c\00\00\00i\00s\00s\00u\00e\00s\00/\002\007\000\000\00.\00t\00s\00") - (table $0 1 1 funcref) - (elem $0 (i32.const 1)) - (export "memory" (memory $0)) - (start $~start) - (func $~lib/polyfills/bswap (param $value i32) (result i32) - i32.const 1 - drop - i32.const 2 - i32.const 1 - i32.eq - drop - i32.const 2 - i32.const 2 - i32.eq - drop - local.get $value - i32.const 8 - i32.const 15 - i32.and - i32.shl - local.get $value - i32.const 65535 - i32.and - i32.const 8 - i32.const 15 - i32.and - i32.shr_u - i32.or - return - ) - (func $start:issues/2700 - global.get $issues/2700/value - i32.const 32768 - i32.extend16_s - i32.eq - drop - global.get $issues/2700/value - call $~lib/polyfills/bswap - i32.extend16_s - i32.const 128 - i32.eq - i32.eqz - if - i32.const 0 - i32.const 32 - i32.const 7 - i32.const 1 - call $~lib/builtins/abort - unreachable - end - global.get $issues/2700/value - call $~lib/polyfills/bswap - i32.extend16_s - i32.const 128 - i32.eq - i32.eqz - if - i32.const 0 - i32.const 32 - i32.const 10 - i32.const 1 - call $~lib/builtins/abort - unreachable - end - global.get $issues/2700/value - call $~lib/polyfills/bswap - i32.extend16_s - i32.const 65408 - i32.extend16_s - i32.ne - i32.eqz - if - i32.const 0 - i32.const 32 - i32.const 13 - i32.const 1 - call $~lib/builtins/abort - unreachable - end - ) - (func $~start - call $start:issues/2700 - ) -) diff --git a/tests/compiler/issues/2700.release.wat b/tests/compiler/issues/2700.release.wat deleted file mode 100644 index cff899b223..0000000000 --- a/tests/compiler/issues/2700.release.wat +++ /dev/null @@ -1,6 +0,0 @@ -(module - (memory $0 1) - (data $0 (i32.const 1036) ",") - (data $0.1 (i32.const 1048) "\02\00\00\00\1c\00\00\00i\00s\00s\00u\00e\00s\00/\002\007\000\000\00.\00t\00s") - (export "memory" (memory $0)) -) diff --git a/tests/compiler/issues/2700.ts b/tests/compiler/issues/2700.ts deleted file mode 100644 index ffa1ce5cc8..0000000000 --- a/tests/compiler/issues/2700.ts +++ /dev/null @@ -1,13 +0,0 @@ -const value: i16 = -32768; - -// [ 128 0 ] -assert(value == 0x8000); - -// [ 0 128 ], asserts in both <=0.19 and >=0.20 -assert(bswap(value) == 0x0080); - -// [ 0 128 ], asserts in <=0.19, fails in >=0.20 -assert(bswap(value) == 0x0080); - -// [ 255 128 ], fails in >=0.20, asserts in <=0.19 -assert(bswap(value) != 0xff80); diff --git a/tests/compiler/polyfills.debug.wat b/tests/compiler/polyfills.debug.wat new file mode 100644 index 0000000000..5a5100659a --- /dev/null +++ b/tests/compiler/polyfills.debug.wat @@ -0,0 +1,511 @@ +(module + (type $i32_=>_i32 (func (param i32) (result i32))) + (type $i64_=>_i64 (func (param i64) (result i64))) + (type $none_=>_none (func)) + (type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32))) + (import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32))) + (global $~lib/memory/__data_end i32 (i32.const 60)) + (global $~lib/memory/__stack_pointer (mut i32) (i32.const 32828)) + (global $~lib/memory/__heap_base i32 (i32.const 32828)) + (memory $0 1) + (data $0 (i32.const 12) ",\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\18\00\00\00p\00o\00l\00y\00f\00i\00l\00l\00s\00.\00t\00s\00\00\00\00\00") + (table $0 1 1 funcref) + (elem $0 (i32.const 1)) + (export "memory" (memory $0)) + (start $~start) + (func $~lib/polyfills/bswap (param $value i32) (result i32) + i32.const 1 + drop + i32.const 1 + i32.const 1 + i32.eq + drop + local.get $value + return + ) + (func $~lib/polyfills/bswap (param $value i32) (result i32) + i32.const 1 + drop + i32.const 1 + i32.const 1 + i32.eq + drop + local.get $value + return + ) + (func $~lib/polyfills/bswap (param $value i32) (result i32) + i32.const 1 + drop + i32.const 2 + i32.const 1 + i32.eq + drop + i32.const 2 + i32.const 2 + i32.eq + drop + local.get $value + i32.const 8 + i32.const 15 + i32.and + i32.shl + local.get $value + i32.const 65535 + i32.and + i32.const 8 + i32.const 15 + i32.and + i32.shr_u + i32.or + return + ) + (func $~lib/polyfills/bswap (param $value i32) (result i32) + i32.const 1 + drop + i32.const 2 + i32.const 1 + i32.eq + drop + i32.const 2 + i32.const 2 + i32.eq + drop + local.get $value + i32.const 8 + i32.const 15 + i32.and + i32.shl + local.get $value + i32.const 65535 + i32.and + i32.const 8 + i32.const 15 + i32.and + i32.shr_u + i32.or + return + ) + (func $~lib/polyfills/bswap (param $value i32) (result i32) + i32.const 1 + drop + i32.const 4 + i32.const 1 + i32.eq + drop + i32.const 4 + i32.const 2 + i32.eq + drop + i32.const 4 + i32.const 4 + i32.eq + drop + local.get $value + i32.const -16711936 + i32.and + i32.const 8 + i32.rotl + local.get $value + i32.const 16711935 + i32.and + i32.const 8 + i32.rotr + i32.or + return + ) + (func $~lib/polyfills/bswap (param $value i32) (result i32) + i32.const 1 + drop + i32.const 4 + i32.const 1 + i32.eq + drop + i32.const 4 + i32.const 2 + i32.eq + drop + i32.const 4 + i32.const 4 + i32.eq + drop + local.get $value + i32.const -16711936 + i32.and + i32.const 8 + i32.rotl + local.get $value + i32.const 16711935 + i32.and + i32.const 8 + i32.rotr + i32.or + return + ) + (func $~lib/polyfills/bswap (param $value i64) (result i64) + (local $a i64) + (local $b i64) + (local $v i64) + i32.const 1 + drop + i32.const 8 + i32.const 1 + i32.eq + drop + i32.const 8 + i32.const 2 + i32.eq + drop + i32.const 8 + i32.const 4 + i32.eq + drop + i32.const 8 + i32.const 8 + i32.eq + drop + local.get $value + i64.const 8 + i64.shr_u + i64.const 71777214294589695 + i64.and + local.set $a + local.get $value + i64.const 71777214294589695 + i64.and + i64.const 8 + i64.shl + local.set $b + local.get $a + local.get $b + i64.or + local.set $v + local.get $v + i64.const 16 + i64.shr_u + i64.const 281470681808895 + i64.and + local.set $a + local.get $v + i64.const 281470681808895 + i64.and + i64.const 16 + i64.shl + local.set $b + local.get $a + local.get $b + i64.or + i64.const 32 + i64.rotr + return + ) + (func $~lib/polyfills/bswap (param $value i64) (result i64) + (local $a i64) + (local $b i64) + (local $v i64) + i32.const 1 + drop + i32.const 8 + i32.const 1 + i32.eq + drop + i32.const 8 + i32.const 2 + i32.eq + drop + i32.const 8 + i32.const 4 + i32.eq + drop + i32.const 8 + i32.const 8 + i32.eq + drop + local.get $value + i64.const 8 + i64.shr_u + i64.const 71777214294589695 + i64.and + local.set $a + local.get $value + i64.const 71777214294589695 + i64.and + i64.const 8 + i64.shl + local.set $b + local.get $a + local.get $b + i64.or + local.set $v + local.get $v + i64.const 16 + i64.shr_u + i64.const 281470681808895 + i64.and + local.set $a + local.get $v + i64.const 281470681808895 + i64.and + i64.const 16 + i64.shl + local.set $b + local.get $a + local.get $b + i64.or + i64.const 32 + i64.rotr + return + ) + (func $~lib/polyfills/bswap (param $value i32) (result i32) + i32.const 1 + drop + i32.const 4 + i32.const 1 + i32.eq + drop + i32.const 4 + i32.const 2 + i32.eq + drop + i32.const 4 + i32.const 4 + i32.eq + drop + local.get $value + i32.const -16711936 + i32.and + i32.const 8 + i32.rotl + local.get $value + i32.const 16711935 + i32.and + i32.const 8 + i32.rotr + i32.or + return + ) + (func $~lib/polyfills/bswap (param $value i32) (result i32) + i32.const 1 + drop + i32.const 4 + i32.const 1 + i32.eq + drop + i32.const 4 + i32.const 2 + i32.eq + drop + i32.const 4 + i32.const 4 + i32.eq + drop + local.get $value + i32.const -16711936 + i32.and + i32.const 8 + i32.rotl + local.get $value + i32.const 16711935 + i32.and + i32.const 8 + i32.rotr + i32.or + return + ) + (func $start:polyfills + i32.const 170 + call $~lib/polyfills/bswap + i32.const 255 + i32.and + i32.const 170 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 2 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 170 + call $~lib/polyfills/bswap + i32.extend8_s + i32.const 170 + i32.extend8_s + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 3 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 43707 + call $~lib/polyfills/bswap + i32.const 65535 + i32.and + i32.const 48042 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 6 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 43707 + call $~lib/polyfills/bswap + i32.extend16_s + i32.const 48042 + i32.extend16_s + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 7 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 43707 + call $~lib/polyfills/bswap + i32.extend16_s + i32.const 48042 + i32.extend16_s + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 8 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const -1430532899 + call $~lib/polyfills/bswap + i32.const -573785174 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 11 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const -1430532899 + call $~lib/polyfills/bswap + i32.const -573785174 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 12 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const -1430532899 + call $~lib/polyfills/bswap + i32.const -573785174 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 13 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i64.const 4822679907192029 + call $~lib/polyfills/bswap + i64.const -2464388556401798912 + i64.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 16 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i64.const 4822679907192029 + call $~lib/polyfills/bswap + i64.const -2464388556401798912 + i64.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 17 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i64.const 4822679907192029 + call $~lib/polyfills/bswap + i64.const -2464388556401798912 + i64.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 18 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const -1430532899 + call $~lib/polyfills/bswap + i32.const -573785174 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 21 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const -1430532899 + call $~lib/polyfills/bswap + i32.const -573785174 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 22 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const -1430532899 + call $~lib/polyfills/bswap + i32.const -573785174 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 23 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + ) + (func $~start + call $start:polyfills + ) +) diff --git a/tests/compiler/issues/2700.json b/tests/compiler/polyfills.json similarity index 100% rename from tests/compiler/issues/2700.json rename to tests/compiler/polyfills.json diff --git a/tests/compiler/polyfills.release.wat b/tests/compiler/polyfills.release.wat new file mode 100644 index 0000000000..b46680aba3 --- /dev/null +++ b/tests/compiler/polyfills.release.wat @@ -0,0 +1,6 @@ +(module + (memory $0 1) + (data $0 (i32.const 1036) ",") + (data $0.1 (i32.const 1048) "\02\00\00\00\18\00\00\00p\00o\00l\00y\00f\00i\00l\00l\00s\00.\00t\00s") + (export "memory" (memory $0)) +) diff --git a/tests/compiler/polyfills.ts b/tests/compiler/polyfills.ts new file mode 100644 index 0000000000..02a6419106 --- /dev/null +++ b/tests/compiler/polyfills.ts @@ -0,0 +1,23 @@ +// check bswap for i8/u8 +assert(bswap(0xaa) == 0xaa); +assert(bswap(0xaa) == 0xaa); + +// check bswap for i16/u16 +assert(bswap(0xaabb) == 0xbbaa); +assert(bswap(0xaabb) == 0xbbaa); +assert(bswap(0xaabb) == 0xbbaa); + +// check bswap for i32/u32 +assert(bswap(0xaabbccdd) == 0xddccbbaa); +assert(bswap(0xaabbccdd) == 0xddccbbaa); +assert(bswap(0xaabbccdd) == 0xddccbbaa); + +// check bswap for i64/u64 +assert(bswap(0x00112233aabbccdd) == 0xddccbbaa33221100); +assert(bswap(0x00112233aabbccdd) == 0xddccbbaa33221100); +assert(bswap(0x00112233aabbccdd) == 0xddccbbaa33221100); + +// check bswap for isize/usize +assert(bswap(0xaabbccdd) == 0xddccbbaa); +assert(bswap(0xaabbccdd) == 0xddccbbaa); +assert(bswap(0xaabbccdd) == 0xddccbbaa); diff --git a/tests/compiler/std/array.debug.wat b/tests/compiler/std/array.debug.wat index c369626cc6..2a8e124b11 100644 --- a/tests/compiler/std/array.debug.wat +++ b/tests/compiler/std/array.debug.wat @@ -14,6 +14,7 @@ (type $i64_i32_=>_i32 (func (param i64 i32) (result i32))) (type $none_=>_f64 (func (result f64))) (type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32))) + (type $i64_=>_i64 (func (param i64) (result i64))) (type $i32_i64_i32_=>_none (func (param i32 i64 i32))) (type $i32_i32_i32_=>_f32 (func (param i32 i32 i32) (result f32))) (type $i32_i32_i32_i32_i32_=>_i32 (func (param i32 i32 i32 i32 i32) (result i32))) @@ -26,7 +27,6 @@ (type $none_=>_i32 (func (result i32))) (type $i32_i32_f32_i32_i32_=>_none (func (param i32 i32 f32 i32 i32))) (type $i32_i64_i32_i32_=>_none (func (param i32 i64 i32 i32))) - (type $i64_=>_i64 (func (param i64) (result i64))) (type $i64_=>_none (func (param i64))) (type $i32_i64_i32_i64_i32_i64_i32_=>_i32 (func (param i32 i64 i32 i64 i32 i64 i32) (result i32))) (type $i64_=>_i32 (func (param i64) (result i32))) @@ -3281,20 +3281,73 @@ end end ) + (func $~lib/polyfills/bswap (param $value i64) (result i64) + (local $a i64) + (local $b i64) + (local $v i64) + i32.const 1 + drop + i32.const 8 + i32.const 1 + i32.eq + drop + i32.const 8 + i32.const 2 + i32.eq + drop + i32.const 8 + i32.const 4 + i32.eq + drop + i32.const 8 + i32.const 8 + i32.eq + drop + local.get $value + i64.const 8 + i64.shr_u + i64.const 71777214294589695 + i64.and + local.set $a + local.get $value + i64.const 71777214294589695 + i64.and + i64.const 8 + i64.shl + local.set $b + local.get $a + local.get $b + i64.or + local.set $v + local.get $v + i64.const 16 + i64.shr_u + i64.const 281470681808895 + i64.and + local.set $a + local.get $v + i64.const 281470681808895 + i64.and + i64.const 16 + i64.shl + local.set $b + local.get $a + local.get $b + i64.or + i64.const 32 + i64.rotr + return + ) (func $~lib/util/bytes/REVERSE (param $ptr i32) (param $len i32) (local $i i32) (local $tail i32) (local $hlen i32) (local $front i32) (local $back i32) - (local $7 i64) - (local $8 i64) (local $temp i64) - (local $10 i64) - (local $11 i64) - (local $front|12 i32) - (local $back|13 i32) - (local $temp|14 i32) + (local $front|8 i32) + (local $back|9 i32) + (local $temp|10 i32) local.get $len i32.const 1 i32.gt_u @@ -3336,58 +3389,12 @@ local.set $back local.get $front i64.load $0 - local.tee $7 - i64.const 8 - i64.shr_u - i64.const 71777214294589695 - i64.and - local.get $7 - i64.const 71777214294589695 - i64.and - i64.const 8 - i64.shl - i64.or - local.tee $8 - i64.const 16 - i64.shr_u - i64.const 281470681808895 - i64.and - local.get $8 - i64.const 281470681808895 - i64.and - i64.const 16 - i64.shl - i64.or - i64.const 32 - i64.rotr + call $~lib/polyfills/bswap local.set $temp local.get $front local.get $back i64.load $0 - local.tee $10 - i64.const 8 - i64.shr_u - i64.const 71777214294589695 - i64.and - local.get $10 - i64.const 71777214294589695 - i64.and - i64.const 8 - i64.shl - i64.or - local.tee $11 - i64.const 16 - i64.shr_u - i64.const 281470681808895 - i64.and - local.get $11 - i64.const 281470681808895 - i64.and - i64.const 16 - i64.shl - i64.or - i64.const 32 - i64.rotr + call $~lib/polyfills/bswap i64.store $0 local.get $back local.get $temp @@ -3417,7 +3424,7 @@ i32.const 0 i32.shl i32.add - local.set $front|12 + local.set $front|8 local.get $ptr local.get $tail local.get $i @@ -3425,16 +3432,16 @@ i32.const 0 i32.shl i32.add - local.set $back|13 - local.get $front|12 + local.set $back|9 + local.get $front|8 i32.load8_u $0 - local.set $temp|14 - local.get $front|12 - local.get $back|13 + local.set $temp|10 + local.get $front|8 + local.get $back|9 i32.load8_u $0 i32.store8 $0 - local.get $back|13 - local.get $temp|14 + local.get $back|9 + local.get $temp|10 i32.store8 $0 local.get $i i32.const 1 diff --git a/tests/compiler/std/dataview.debug.wat b/tests/compiler/std/dataview.debug.wat index 473b0b0151..183bc87e59 100644 --- a/tests/compiler/std/dataview.debug.wat +++ b/tests/compiler/std/dataview.debug.wat @@ -12,6 +12,7 @@ (type $i32_i32_i64_i32_=>_none (func (param i32 i32 i64 i32))) (type $i32_i32_i64_=>_i32 (func (param i32 i32 i64) (result i32))) (type $none_=>_i32 (func (result i32))) + (type $i64_=>_i64 (func (param i64) (result i64))) (type $i32_i32_i32_=>_f32 (func (param i32 i32 i32) (result f32))) (type $i32_i32_i32_=>_f64 (func (param i32 i32 i32) (result f64))) (type $i32_i32_f32_i32_=>_none (func (param i32 i32 f32 i32))) @@ -2393,6 +2394,117 @@ local.get $this i32.load $0 offset=4 ) + (func $~lib/polyfills/bswap (param $value i32) (result i32) + i32.const 1 + drop + i32.const 4 + i32.const 1 + i32.eq + drop + i32.const 4 + i32.const 2 + i32.eq + drop + i32.const 4 + i32.const 4 + i32.eq + drop + local.get $value + i32.const -16711936 + i32.and + i32.const 8 + i32.rotl + local.get $value + i32.const 16711935 + i32.and + i32.const 8 + i32.rotr + i32.or + return + ) + (func $~lib/polyfills/bswap (param $value i64) (result i64) + (local $a i64) + (local $b i64) + (local $v i64) + i32.const 1 + drop + i32.const 8 + i32.const 1 + i32.eq + drop + i32.const 8 + i32.const 2 + i32.eq + drop + i32.const 8 + i32.const 4 + i32.eq + drop + i32.const 8 + i32.const 8 + i32.eq + drop + local.get $value + i64.const 8 + i64.shr_u + i64.const 71777214294589695 + i64.and + local.set $a + local.get $value + i64.const 71777214294589695 + i64.and + i64.const 8 + i64.shl + local.set $b + local.get $a + local.get $b + i64.or + local.set $v + local.get $v + i64.const 16 + i64.shr_u + i64.const 281470681808895 + i64.and + local.set $a + local.get $v + i64.const 281470681808895 + i64.and + i64.const 16 + i64.shl + local.set $b + local.get $a + local.get $b + i64.or + i64.const 32 + i64.rotr + return + ) + (func $~lib/polyfills/bswap (param $value i32) (result i32) + i32.const 1 + drop + i32.const 2 + i32.const 1 + i32.eq + drop + i32.const 2 + i32.const 2 + i32.eq + drop + local.get $value + i32.const 8 + i32.const 15 + i32.and + i32.shl + local.get $value + i32.const 65535 + i32.and + i32.const 8 + i32.const 15 + i32.and + i32.shr_u + i32.or + return + ) (func $~lib/dataview/DataView#get:buffer (param $this i32) (result i32) local.get $this i32.load $0 @@ -2885,8 +2997,7 @@ ) (func $~lib/dataview/DataView#getFloat32 (param $this i32) (param $byteOffset i32) (param $littleEndian i32) (result f32) (local $3 i32) - (local $4 i32) - (local $5 f32) + (local $4 f32) global.get $~lib/memory/__stack_pointer i32.const 4 i32.sub @@ -2902,11 +3013,11 @@ i32.const 4 i32.add local.get $this - local.set $4 + local.set $3 global.get $~lib/memory/__stack_pointer - local.get $4 + local.get $3 i32.store $0 - local.get $4 + local.get $3 call $~lib/dataview/DataView#get:byteLength i32.gt_s i32.or @@ -2921,52 +3032,40 @@ local.get $littleEndian if (result f32) local.get $this - local.set $4 + local.set $3 global.get $~lib/memory/__stack_pointer - local.get $4 + local.get $3 i32.store $0 - local.get $4 + local.get $3 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add f32.load $0 else local.get $this - local.set $4 + local.set $3 global.get $~lib/memory/__stack_pointer - local.get $4 + local.get $3 i32.store $0 - local.get $4 + local.get $3 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add i32.load $0 - local.tee $3 - i32.const -16711936 - i32.and - i32.const 8 - i32.rotl - local.get $3 - i32.const 16711935 - i32.and - i32.const 8 - i32.rotr - i32.or + call $~lib/polyfills/bswap f32.reinterpret_i32 end - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer i32.const 4 i32.add global.set $~lib/memory/__stack_pointer - local.get $5 + local.get $4 return ) (func $~lib/dataview/DataView#getFloat64 (param $this i32) (param $byteOffset i32) (param $littleEndian i32) (result f64) - (local $3 i64) - (local $4 i64) - (local $5 i32) - (local $6 f64) + (local $3 i32) + (local $4 f64) global.get $~lib/memory/__stack_pointer i32.const 4 i32.sub @@ -2982,11 +3081,11 @@ i32.const 8 i32.add local.get $this - local.set $5 + local.set $3 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $3 i32.store $0 - local.get $5 + local.get $3 call $~lib/dataview/DataView#get:byteLength i32.gt_s i32.or @@ -3001,58 +3100,35 @@ local.get $littleEndian if (result f64) local.get $this - local.set $5 + local.set $3 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $3 i32.store $0 - local.get $5 + local.get $3 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add f64.load $0 else local.get $this - local.set $5 + local.set $3 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $3 i32.store $0 - local.get $5 + local.get $3 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add i64.load $0 - local.tee $3 - i64.const 8 - i64.shr_u - i64.const 71777214294589695 - i64.and - local.get $3 - i64.const 71777214294589695 - i64.and - i64.const 8 - i64.shl - i64.or - local.tee $4 - i64.const 16 - i64.shr_u - i64.const 281470681808895 - i64.and - local.get $4 - i64.const 281470681808895 - i64.and - i64.const 16 - i64.shl - i64.or - i64.const 32 - i64.rotr + call $~lib/polyfills/bswap f64.reinterpret_i64 end - local.set $6 + local.set $4 global.get $~lib/memory/__stack_pointer i32.const 4 i32.add global.set $~lib/memory/__stack_pointer - local.get $6 + local.get $4 return ) (func $~lib/dataview/DataView#getInt8 (param $this i32) (param $byteOffset i32) (result i32) @@ -3103,7 +3179,6 @@ (func $~lib/dataview/DataView#getInt16 (param $this i32) (param $byteOffset i32) (param $littleEndian i32) (result i32) (local $result i32) (local $4 i32) - (local $5 i32) global.get $~lib/memory/__stack_pointer i32.const 4 i32.sub @@ -3119,11 +3194,11 @@ i32.const 2 i32.add local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:byteLength i32.gt_s i32.or @@ -3136,11 +3211,11 @@ unreachable end local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add @@ -3151,28 +3226,19 @@ local.get $result else local.get $result - i32.const 65535 - i32.and - local.tee $4 - i32.const 8 - i32.shl - local.get $4 - i32.const 8 - i32.shr_u - i32.or + call $~lib/polyfills/bswap end - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer i32.const 4 i32.add global.set $~lib/memory/__stack_pointer - local.get $5 + local.get $4 return ) (func $~lib/dataview/DataView#getInt32 (param $this i32) (param $byteOffset i32) (param $littleEndian i32) (result i32) (local $result i32) (local $4 i32) - (local $5 i32) global.get $~lib/memory/__stack_pointer i32.const 4 i32.sub @@ -3188,11 +3254,11 @@ i32.const 4 i32.add local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:byteLength i32.gt_s i32.or @@ -3205,11 +3271,11 @@ unreachable end local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add @@ -3220,32 +3286,20 @@ local.get $result else local.get $result - local.tee $4 - i32.const -16711936 - i32.and - i32.const 8 - i32.rotl - local.get $4 - i32.const 16711935 - i32.and - i32.const 8 - i32.rotr - i32.or + call $~lib/polyfills/bswap end - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer i32.const 4 i32.add global.set $~lib/memory/__stack_pointer - local.get $5 + local.get $4 return ) (func $~lib/dataview/DataView#getInt64 (param $this i32) (param $byteOffset i32) (param $littleEndian i32) (result i64) (local $result i64) - (local $4 i64) + (local $4 i32) (local $5 i64) - (local $6 i32) - (local $7 i64) global.get $~lib/memory/__stack_pointer i32.const 4 i32.sub @@ -3261,11 +3315,11 @@ i32.const 8 i32.add local.get $this - local.set $6 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $6 + local.get $4 i32.store $0 - local.get $6 + local.get $4 call $~lib/dataview/DataView#get:byteLength i32.gt_s i32.or @@ -3278,11 +3332,11 @@ unreachable end local.get $this - local.set $6 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $6 + local.get $4 i32.store $0 - local.get $6 + local.get $4 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add @@ -3293,37 +3347,14 @@ local.get $result else local.get $result - local.tee $4 - i64.const 8 - i64.shr_u - i64.const 71777214294589695 - i64.and - local.get $4 - i64.const 71777214294589695 - i64.and - i64.const 8 - i64.shl - i64.or - local.tee $5 - i64.const 16 - i64.shr_u - i64.const 281470681808895 - i64.and - local.get $5 - i64.const 281470681808895 - i64.and - i64.const 16 - i64.shl - i64.or - i64.const 32 - i64.rotr - end - local.set $7 + call $~lib/polyfills/bswap + end + local.set $5 global.get $~lib/memory/__stack_pointer i32.const 4 i32.add global.set $~lib/memory/__stack_pointer - local.get $7 + local.get $5 return ) (func $~lib/dataview/DataView#getUint8 (param $this i32) (param $byteOffset i32) (result i32) @@ -3374,7 +3405,6 @@ (func $~lib/dataview/DataView#getUint16 (param $this i32) (param $byteOffset i32) (param $littleEndian i32) (result i32) (local $result i32) (local $4 i32) - (local $5 i32) global.get $~lib/memory/__stack_pointer i32.const 4 i32.sub @@ -3390,11 +3420,11 @@ i32.const 2 i32.add local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:byteLength i32.gt_s i32.or @@ -3407,11 +3437,11 @@ unreachable end local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add @@ -3422,26 +3452,19 @@ local.get $result else local.get $result - local.tee $4 - i32.const 8 - i32.shl - local.get $4 - i32.const 8 - i32.shr_u - i32.or + call $~lib/polyfills/bswap end - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer i32.const 4 i32.add global.set $~lib/memory/__stack_pointer - local.get $5 + local.get $4 return ) (func $~lib/dataview/DataView#getUint32 (param $this i32) (param $byteOffset i32) (param $littleEndian i32) (result i32) (local $result i32) (local $4 i32) - (local $5 i32) global.get $~lib/memory/__stack_pointer i32.const 4 i32.sub @@ -3457,11 +3480,11 @@ i32.const 4 i32.add local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:byteLength i32.gt_s i32.or @@ -3474,11 +3497,11 @@ unreachable end local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add @@ -3489,32 +3512,20 @@ local.get $result else local.get $result - local.tee $4 - i32.const -16711936 - i32.and - i32.const 8 - i32.rotl - local.get $4 - i32.const 16711935 - i32.and - i32.const 8 - i32.rotr - i32.or + call $~lib/polyfills/bswap end - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer i32.const 4 i32.add global.set $~lib/memory/__stack_pointer - local.get $5 + local.get $4 return ) (func $~lib/dataview/DataView#getUint64 (param $this i32) (param $byteOffset i32) (param $littleEndian i32) (result i64) (local $result i64) - (local $4 i64) + (local $4 i32) (local $5 i64) - (local $6 i32) - (local $7 i64) global.get $~lib/memory/__stack_pointer i32.const 4 i32.sub @@ -3530,11 +3541,11 @@ i32.const 8 i32.add local.get $this - local.set $6 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $6 + local.get $4 i32.store $0 - local.get $6 + local.get $4 call $~lib/dataview/DataView#get:byteLength i32.gt_s i32.or @@ -3547,11 +3558,11 @@ unreachable end local.get $this - local.set $6 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $6 + local.get $4 i32.store $0 - local.get $6 + local.get $4 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add @@ -3562,42 +3573,18 @@ local.get $result else local.get $result - local.tee $4 - i64.const 8 - i64.shr_u - i64.const 71777214294589695 - i64.and - local.get $4 - i64.const 71777214294589695 - i64.and - i64.const 8 - i64.shl - i64.or - local.tee $5 - i64.const 16 - i64.shr_u - i64.const 281470681808895 - i64.and - local.get $5 - i64.const 281470681808895 - i64.and - i64.const 16 - i64.shl - i64.or - i64.const 32 - i64.rotr - end - local.set $7 + call $~lib/polyfills/bswap + end + local.set $5 global.get $~lib/memory/__stack_pointer i32.const 4 i32.add global.set $~lib/memory/__stack_pointer - local.get $7 + local.get $5 return ) (func $~lib/dataview/DataView#setFloat32 (param $this i32) (param $byteOffset i32) (param $value f32) (param $littleEndian i32) (local $4 i32) - (local $5 i32) global.get $~lib/memory/__stack_pointer i32.const 4 i32.sub @@ -3613,11 +3600,11 @@ i32.const 4 i32.add local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:byteLength i32.gt_s i32.or @@ -3632,11 +3619,11 @@ local.get $littleEndian if local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add @@ -3644,27 +3631,17 @@ f32.store $0 else local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add local.get $value i32.reinterpret_f32 - local.tee $4 - i32.const -16711936 - i32.and - i32.const 8 - i32.rotl - local.get $4 - i32.const 16711935 - i32.and - i32.const 8 - i32.rotr - i32.or + call $~lib/polyfills/bswap i32.store $0 end global.get $~lib/memory/__stack_pointer @@ -3673,9 +3650,7 @@ global.set $~lib/memory/__stack_pointer ) (func $~lib/dataview/DataView#setFloat64 (param $this i32) (param $byteOffset i32) (param $value f64) (param $littleEndian i32) - (local $4 i64) - (local $5 i64) - (local $6 i32) + (local $4 i32) global.get $~lib/memory/__stack_pointer i32.const 4 i32.sub @@ -3691,11 +3666,11 @@ i32.const 8 i32.add local.get $this - local.set $6 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $6 + local.get $4 i32.store $0 - local.get $6 + local.get $4 call $~lib/dataview/DataView#get:byteLength i32.gt_s i32.or @@ -3710,11 +3685,11 @@ local.get $littleEndian if local.get $this - local.set $6 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $6 + local.get $4 i32.store $0 - local.get $6 + local.get $4 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add @@ -3722,40 +3697,17 @@ f64.store $0 else local.get $this - local.set $6 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $6 + local.get $4 i32.store $0 - local.get $6 + local.get $4 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add local.get $value i64.reinterpret_f64 - local.tee $4 - i64.const 8 - i64.shr_u - i64.const 71777214294589695 - i64.and - local.get $4 - i64.const 71777214294589695 - i64.and - i64.const 8 - i64.shl - i64.or - local.tee $5 - i64.const 16 - i64.shr_u - i64.const 281470681808895 - i64.and - local.get $5 - i64.const 281470681808895 - i64.and - i64.const 16 - i64.shl - i64.or - i64.const 32 - i64.rotr + call $~lib/polyfills/bswap i64.store $0 end global.get $~lib/memory/__stack_pointer @@ -3808,7 +3760,6 @@ ) (func $~lib/dataview/DataView#setInt16 (param $this i32) (param $byteOffset i32) (param $value i32) (param $littleEndian i32) (local $4 i32) - (local $5 i32) global.get $~lib/memory/__stack_pointer i32.const 4 i32.sub @@ -3824,11 +3775,11 @@ i32.const 2 i32.add local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:byteLength i32.gt_s i32.or @@ -3841,11 +3792,11 @@ unreachable end local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add @@ -3854,15 +3805,7 @@ local.get $value else local.get $value - i32.const 65535 - i32.and - local.tee $4 - i32.const 8 - i32.shl - local.get $4 - i32.const 8 - i32.shr_u - i32.or + call $~lib/polyfills/bswap end i32.store16 $0 global.get $~lib/memory/__stack_pointer @@ -3872,7 +3815,6 @@ ) (func $~lib/dataview/DataView#setInt32 (param $this i32) (param $byteOffset i32) (param $value i32) (param $littleEndian i32) (local $4 i32) - (local $5 i32) global.get $~lib/memory/__stack_pointer i32.const 4 i32.sub @@ -3888,11 +3830,11 @@ i32.const 4 i32.add local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:byteLength i32.gt_s i32.or @@ -3905,11 +3847,11 @@ unreachable end local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add @@ -3918,17 +3860,7 @@ local.get $value else local.get $value - local.tee $4 - i32.const -16711936 - i32.and - i32.const 8 - i32.rotl - local.get $4 - i32.const 16711935 - i32.and - i32.const 8 - i32.rotr - i32.or + call $~lib/polyfills/bswap end i32.store $0 global.get $~lib/memory/__stack_pointer @@ -3937,9 +3869,7 @@ global.set $~lib/memory/__stack_pointer ) (func $~lib/dataview/DataView#setInt64 (param $this i32) (param $byteOffset i32) (param $value i64) (param $littleEndian i32) - (local $4 i64) - (local $5 i64) - (local $6 i32) + (local $4 i32) global.get $~lib/memory/__stack_pointer i32.const 4 i32.sub @@ -3955,11 +3885,11 @@ i32.const 8 i32.add local.get $this - local.set $6 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $6 + local.get $4 i32.store $0 - local.get $6 + local.get $4 call $~lib/dataview/DataView#get:byteLength i32.gt_s i32.or @@ -3972,11 +3902,11 @@ unreachable end local.get $this - local.set $6 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $6 + local.get $4 i32.store $0 - local.get $6 + local.get $4 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add @@ -3985,30 +3915,7 @@ local.get $value else local.get $value - local.tee $4 - i64.const 8 - i64.shr_u - i64.const 71777214294589695 - i64.and - local.get $4 - i64.const 71777214294589695 - i64.and - i64.const 8 - i64.shl - i64.or - local.tee $5 - i64.const 16 - i64.shr_u - i64.const 281470681808895 - i64.and - local.get $5 - i64.const 281470681808895 - i64.and - i64.const 16 - i64.shl - i64.or - i64.const 32 - i64.rotr + call $~lib/polyfills/bswap end i64.store $0 global.get $~lib/memory/__stack_pointer @@ -4061,7 +3968,6 @@ ) (func $~lib/dataview/DataView#setUint16 (param $this i32) (param $byteOffset i32) (param $value i32) (param $littleEndian i32) (local $4 i32) - (local $5 i32) global.get $~lib/memory/__stack_pointer i32.const 4 i32.sub @@ -4077,11 +3983,11 @@ i32.const 2 i32.add local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:byteLength i32.gt_s i32.or @@ -4094,11 +4000,11 @@ unreachable end local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add @@ -4107,15 +4013,7 @@ local.get $value else local.get $value - i32.const 65535 - i32.and - local.tee $4 - i32.const 8 - i32.shl - local.get $4 - i32.const 8 - i32.shr_u - i32.or + call $~lib/polyfills/bswap end i32.store16 $0 global.get $~lib/memory/__stack_pointer @@ -4125,7 +4023,6 @@ ) (func $~lib/dataview/DataView#setUint32 (param $this i32) (param $byteOffset i32) (param $value i32) (param $littleEndian i32) (local $4 i32) - (local $5 i32) global.get $~lib/memory/__stack_pointer i32.const 4 i32.sub @@ -4141,11 +4038,11 @@ i32.const 4 i32.add local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:byteLength i32.gt_s i32.or @@ -4158,11 +4055,11 @@ unreachable end local.get $this - local.set $5 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $5 + local.get $4 i32.store $0 - local.get $5 + local.get $4 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add @@ -4171,17 +4068,7 @@ local.get $value else local.get $value - local.tee $4 - i32.const -16711936 - i32.and - i32.const 8 - i32.rotl - local.get $4 - i32.const 16711935 - i32.and - i32.const 8 - i32.rotr - i32.or + call $~lib/polyfills/bswap end i32.store $0 global.get $~lib/memory/__stack_pointer @@ -4190,9 +4077,7 @@ global.set $~lib/memory/__stack_pointer ) (func $~lib/dataview/DataView#setUint64 (param $this i32) (param $byteOffset i32) (param $value i64) (param $littleEndian i32) - (local $4 i64) - (local $5 i64) - (local $6 i32) + (local $4 i32) global.get $~lib/memory/__stack_pointer i32.const 4 i32.sub @@ -4208,11 +4093,11 @@ i32.const 8 i32.add local.get $this - local.set $6 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $6 + local.get $4 i32.store $0 - local.get $6 + local.get $4 call $~lib/dataview/DataView#get:byteLength i32.gt_s i32.or @@ -4225,11 +4110,11 @@ unreachable end local.get $this - local.set $6 + local.set $4 global.get $~lib/memory/__stack_pointer - local.get $6 + local.get $4 i32.store $0 - local.get $6 + local.get $4 call $~lib/dataview/DataView#get:dataStart local.get $byteOffset i32.add @@ -4238,30 +4123,7 @@ local.get $value else local.get $value - local.tee $4 - i64.const 8 - i64.shr_u - i64.const 71777214294589695 - i64.and - local.get $4 - i64.const 71777214294589695 - i64.and - i64.const 8 - i64.shl - i64.or - local.tee $5 - i64.const 16 - i64.shr_u - i64.const 281470681808895 - i64.and - local.get $5 - i64.const 281470681808895 - i64.and - i64.const 16 - i64.shl - i64.or - i64.const 32 - i64.rotr + call $~lib/polyfills/bswap end i64.store $0 global.get $~lib/memory/__stack_pointer diff --git a/tests/compiler/std/dataview.release.wat b/tests/compiler/std/dataview.release.wat index 656ba6b9f3..5ab57cfff1 100644 --- a/tests/compiler/std/dataview.release.wat +++ b/tests/compiler/std/dataview.release.wat @@ -2081,21 +2081,18 @@ i32.const 4 i32.add global.set $~lib/memory/__stack_pointer + local.get $0 + local.get $0 + i32.const 8 + i32.shl + local.get $0 + i32.const 65535 + i32.and + i32.const 8 + i32.shr_u + i32.or local.get $2 - if (result i32) - local.get $0 - else - local.get $0 - i32.const 65535 - i32.and - local.tee $0 - i32.const 8 - i32.shl - local.get $0 - i32.const 8 - i32.shr_u - i32.or - end + select ) (func $~lib/dataview/DataView#getInt32 (param $0 i32) (param $1 i32) (param $2 i32) (result i32) (local $3 i32) @@ -2713,27 +2710,25 @@ unreachable end global.get $~lib/memory/__stack_pointer + local.tee $3 local.get $0 i32.store $0 local.get $0 i32.load $0 offset=4 + local.get $1 + local.get $1 + i32.const 8 + i32.shl + local.get $1 + i32.const 65535 + i32.and + i32.const 8 + i32.shr_u + i32.or local.get $2 - if (result i32) - local.get $1 - else - local.get $1 - i32.const 65535 - i32.and - local.tee $0 - i32.const 8 - i32.shl - local.get $0 - i32.const 8 - i32.shr_u - i32.or - end + select i32.store16 $0 - global.get $~lib/memory/__stack_pointer + local.get $3 i32.const 4 i32.add global.set $~lib/memory/__stack_pointer @@ -2914,27 +2909,25 @@ unreachable end global.get $~lib/memory/__stack_pointer + local.tee $3 local.get $0 i32.store $0 local.get $0 i32.load $0 offset=4 + local.get $1 + local.get $1 + i32.const 8 + i32.shl + local.get $1 + i32.const 65535 + i32.and + i32.const 8 + i32.shr_u + i32.or local.get $2 - if (result i32) - local.get $1 - else - local.get $1 - i32.const 65535 - i32.and - local.tee $0 - i32.const 8 - i32.shl - local.get $0 - i32.const 8 - i32.shr_u - i32.or - end + select i32.store16 $0 - global.get $~lib/memory/__stack_pointer + local.get $3 i32.const 4 i32.add global.set $~lib/memory/__stack_pointer diff --git a/tests/compiler/std/typedarray.debug.wat b/tests/compiler/std/typedarray.debug.wat index c1cb45a49d..beb7965d3e 100644 --- a/tests/compiler/std/typedarray.debug.wat +++ b/tests/compiler/std/typedarray.debug.wat @@ -44,6 +44,7 @@ (type $none_=>_i32 (func (result i32))) (type $f32_f32_=>_f32 (func (param f32 f32) (result f32))) (type $f64_f64_=>_f64 (func (param f64 f64) (result f64))) + (type $i64_=>_i64 (func (param i64) (result i64))) (type $i32_i64_i32_i32_=>_none (func (param i32 i64 i32 i32))) (type $i64_=>_i32 (func (param i64) (result i32))) (type $i32_i64_i32_i64_i32_i64_i32_=>_i32 (func (param i32 i64 i32 i64 i32 i64 i32) (result i32))) @@ -4804,20 +4805,73 @@ f64.const 2 f64.eq ) + (func $~lib/polyfills/bswap (param $value i64) (result i64) + (local $a i64) + (local $b i64) + (local $v i64) + i32.const 1 + drop + i32.const 8 + i32.const 1 + i32.eq + drop + i32.const 8 + i32.const 2 + i32.eq + drop + i32.const 8 + i32.const 4 + i32.eq + drop + i32.const 8 + i32.const 8 + i32.eq + drop + local.get $value + i64.const 8 + i64.shr_u + i64.const 71777214294589695 + i64.and + local.set $a + local.get $value + i64.const 71777214294589695 + i64.and + i64.const 8 + i64.shl + local.set $b + local.get $a + local.get $b + i64.or + local.set $v + local.get $v + i64.const 16 + i64.shr_u + i64.const 281470681808895 + i64.and + local.set $a + local.get $v + i64.const 281470681808895 + i64.and + i64.const 16 + i64.shl + local.set $b + local.get $a + local.get $b + i64.or + i64.const 32 + i64.rotr + return + ) (func $~lib/util/bytes/REVERSE (param $ptr i32) (param $len i32) (local $i i32) (local $tail i32) (local $hlen i32) (local $front i32) (local $back i32) - (local $7 i64) - (local $8 i64) (local $temp i64) - (local $10 i64) - (local $11 i64) - (local $front|12 i32) - (local $back|13 i32) - (local $temp|14 i32) + (local $front|8 i32) + (local $back|9 i32) + (local $temp|10 i32) local.get $len i32.const 1 i32.gt_u @@ -4859,58 +4913,12 @@ local.set $back local.get $front i64.load $0 - local.tee $7 - i64.const 8 - i64.shr_u - i64.const 71777214294589695 - i64.and - local.get $7 - i64.const 71777214294589695 - i64.and - i64.const 8 - i64.shl - i64.or - local.tee $8 - i64.const 16 - i64.shr_u - i64.const 281470681808895 - i64.and - local.get $8 - i64.const 281470681808895 - i64.and - i64.const 16 - i64.shl - i64.or - i64.const 32 - i64.rotr + call $~lib/polyfills/bswap local.set $temp local.get $front local.get $back i64.load $0 - local.tee $10 - i64.const 8 - i64.shr_u - i64.const 71777214294589695 - i64.and - local.get $10 - i64.const 71777214294589695 - i64.and - i64.const 8 - i64.shl - i64.or - local.tee $11 - i64.const 16 - i64.shr_u - i64.const 281470681808895 - i64.and - local.get $11 - i64.const 281470681808895 - i64.and - i64.const 16 - i64.shl - i64.or - i64.const 32 - i64.rotr + call $~lib/polyfills/bswap i64.store $0 local.get $back local.get $temp @@ -4940,7 +4948,7 @@ i32.const 0 i32.shl i32.add - local.set $front|12 + local.set $front|8 local.get $ptr local.get $tail local.get $i @@ -4948,16 +4956,16 @@ i32.const 0 i32.shl i32.add - local.set $back|13 - local.get $front|12 + local.set $back|9 + local.get $front|8 i32.load8_u $0 - local.set $temp|14 - local.get $front|12 - local.get $back|13 + local.set $temp|10 + local.get $front|8 + local.get $back|9 i32.load8_u $0 i32.store8 $0 - local.get $back|13 - local.get $temp|14 + local.get $back|9 + local.get $temp|10 i32.store8 $0 local.get $i i32.const 1 From c19cedb1e9454fbe5ded7f025d2a10cee4e9894c Mon Sep 17 00:00:00 2001 From: dcode Date: Mon, 31 Jul 2023 18:29:46 +0200 Subject: [PATCH 3/3] recreate fixtures --- .../bindings/noExportRuntime.release.js | 2 +- tests/compiler/std/math.release.wat | 36 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/tests/compiler/bindings/noExportRuntime.release.js b/tests/compiler/bindings/noExportRuntime.release.js index c35c9f8ef1..de99664237 100644 --- a/tests/compiler/bindings/noExportRuntime.release.js +++ b/tests/compiler/bindings/noExportRuntime.release.js @@ -159,7 +159,7 @@ export const { isArrayOfArray, returnsArrayOfArray, takesNonPlainObject, - takesFunction + takesFunction, } = await (async url => instantiate( await (async () => { try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } diff --git a/tests/compiler/std/math.release.wat b/tests/compiler/std/math.release.wat index 2af003d42d..233a263ee1 100644 --- a/tests/compiler/std/math.release.wat +++ b/tests/compiler/std/math.release.wat @@ -49900,7 +49900,7 @@ call $~lib/builtins/abort unreachable end - f64.const nan:0x8000000000000 + f64.const -nan:0x8000000000000 f64.const nan:0x8000000000000 f64.const 0 call $std/math/check @@ -49944,7 +49944,7 @@ call $~lib/builtins/abort unreachable end - f64.const nan:0x8000000000000 + f64.const -nan:0x8000000000000 f64.const nan:0x8000000000000 f64.const 0 call $std/math/check @@ -49966,7 +49966,7 @@ call $~lib/builtins/abort unreachable end - f64.const nan:0x8000000000000 + f64.const -nan:0x8000000000000 f64.const nan:0x8000000000000 f64.const 0 call $std/math/check @@ -50032,7 +50032,7 @@ call $~lib/builtins/abort unreachable end - f64.const nan:0x8000000000000 + f64.const -nan:0x8000000000000 f64.const nan:0x8000000000000 f64.const 0 call $std/math/check @@ -50098,7 +50098,7 @@ call $~lib/builtins/abort unreachable end - f64.const nan:0x8000000000000 + f64.const -nan:0x8000000000000 f64.const nan:0x8000000000000 f64.const 0 call $std/math/check @@ -50164,7 +50164,7 @@ call $~lib/builtins/abort unreachable end - f64.const nan:0x8000000000000 + f64.const -nan:0x8000000000000 f64.const nan:0x8000000000000 f64.const 0 call $std/math/check @@ -50252,7 +50252,7 @@ call $~lib/builtins/abort unreachable end - f64.const nan:0x8000000000000 + f64.const -nan:0x8000000000000 f64.const nan:0x8000000000000 f64.const 0 call $std/math/check @@ -50362,7 +50362,7 @@ call $~lib/builtins/abort unreachable end - f64.const nan:0x8000000000000 + f64.const -nan:0x8000000000000 f64.const nan:0x8000000000000 f64.const 0 call $std/math/check @@ -50516,7 +50516,7 @@ call $~lib/builtins/abort unreachable end - f64.const nan:0x8000000000000 + f64.const -nan:0x8000000000000 f64.const nan:0x8000000000000 f64.const 0 call $std/math/check @@ -51748,7 +51748,7 @@ call $~lib/builtins/abort unreachable end - f32.const nan:0x400000 + f32.const -nan:0x400000 f32.const nan:0x400000 f32.const 0 call $std/math/check @@ -51774,7 +51774,7 @@ call $~lib/builtins/abort unreachable end - f32.const nan:0x400000 + f32.const -nan:0x400000 f32.const nan:0x400000 f32.const 0 call $std/math/check @@ -51787,7 +51787,7 @@ call $~lib/builtins/abort unreachable end - f32.const nan:0x400000 + f32.const -nan:0x400000 f32.const nan:0x400000 f32.const 0 call $std/math/check @@ -51826,7 +51826,7 @@ call $~lib/builtins/abort unreachable end - f32.const nan:0x400000 + f32.const -nan:0x400000 f32.const nan:0x400000 f32.const 0 call $std/math/check @@ -51865,7 +51865,7 @@ call $~lib/builtins/abort unreachable end - f32.const nan:0x400000 + f32.const -nan:0x400000 f32.const nan:0x400000 f32.const 0 call $std/math/check @@ -51904,7 +51904,7 @@ call $~lib/builtins/abort unreachable end - f32.const nan:0x400000 + f32.const -nan:0x400000 f32.const nan:0x400000 f32.const 0 call $std/math/check @@ -51956,7 +51956,7 @@ call $~lib/builtins/abort unreachable end - f32.const nan:0x400000 + f32.const -nan:0x400000 f32.const nan:0x400000 f32.const 0 call $std/math/check @@ -52021,7 +52021,7 @@ call $~lib/builtins/abort unreachable end - f32.const nan:0x400000 + f32.const -nan:0x400000 f32.const nan:0x400000 f32.const 0 call $std/math/check @@ -52047,7 +52047,7 @@ call $~lib/builtins/abort unreachable end - f32.const nan:0x400000 + f32.const -nan:0x400000 f32.const nan:0x400000 f32.const 0 call $std/math/check