Skip to content

Commit 2a55449

Browse files
authored
feat: Add isVector builtin (#2369)
1 parent 9cbb728 commit 2a55449

File tree

5 files changed

+56
-21
lines changed

5 files changed

+56
-21
lines changed

Diff for: src/builtins.ts

+26-14
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,11 @@ export namespace BuiltinNames {
127127
export const trace = "~lib/builtins/trace";
128128
export const seed = "~lib/builtins/seed";
129129

130-
export const isInteger = "~lib/builtins/isInteger";
131-
export const isFloat = "~lib/builtins/isFloat";
132130
export const isBoolean = "~lib/builtins/isBoolean";
131+
export const isInteger = "~lib/builtins/isInteger";
133132
export const isSigned = "~lib/builtins/isSigned";
133+
export const isFloat = "~lib/builtins/isFloat";
134+
export const isVector = "~lib/builtins/isVector";
134135
export const isReference = "~lib/builtins/isReference";
135136
export const isString = "~lib/builtins/isString";
136137
export const isArray = "~lib/builtins/isArray";
@@ -758,6 +759,17 @@ export const function_builtins = new Map<string,(ctx: BuiltinContext) => Express
758759

759760
// === Static type evaluation =================================================================
760761

762+
// isBoolean<T!>() / isBoolean<T?>(value: T) -> bool
763+
function builtin_isBoolean(ctx: BuiltinContext): ExpressionRef {
764+
var compiler = ctx.compiler;
765+
var module = compiler.module;
766+
var type = checkConstantType(ctx);
767+
compiler.currentType = Type.bool;
768+
if (!type) return module.unreachable();
769+
return reifyConstantType(ctx, module.i32(type.isBooleanValue ? 1 : 0));
770+
}
771+
builtins.set(BuiltinNames.isBoolean, builtin_isBoolean);
772+
761773
// isInteger<T!>() / isInteger<T?>(value: T) -> bool
762774
function builtin_isInteger(ctx: BuiltinContext): ExpressionRef {
763775
var compiler = ctx.compiler;
@@ -769,38 +781,38 @@ function builtin_isInteger(ctx: BuiltinContext): ExpressionRef {
769781
}
770782
builtins.set(BuiltinNames.isInteger, builtin_isInteger);
771783

772-
// isFloat<T!>() / isFloat<T?>(value: T) -> bool
773-
function builtin_isFloat(ctx: BuiltinContext): ExpressionRef {
784+
// isSigned<T!>() / isSigned<T?>(value: T) -> bool
785+
function builtin_isSigned(ctx: BuiltinContext): ExpressionRef {
774786
var compiler = ctx.compiler;
775787
var module = compiler.module;
776788
var type = checkConstantType(ctx);
777789
compiler.currentType = Type.bool;
778790
if (!type) return module.unreachable();
779-
return reifyConstantType(ctx, module.i32(type.isFloatValue ? 1 : 0));
791+
return reifyConstantType(ctx, module.i32(type.isSignedIntegerValue ? 1 : 0));
780792
}
781-
builtins.set(BuiltinNames.isFloat, builtin_isFloat);
793+
builtins.set(BuiltinNames.isSigned, builtin_isSigned);
782794

783-
// isBoolean<T!>() / isBoolean<T?>(value: T) -> bool
784-
function builtin_isBoolean(ctx: BuiltinContext): ExpressionRef {
795+
// isFloat<T!>() / isFloat<T?>(value: T) -> bool
796+
function builtin_isFloat(ctx: BuiltinContext): ExpressionRef {
785797
var compiler = ctx.compiler;
786798
var module = compiler.module;
787799
var type = checkConstantType(ctx);
788800
compiler.currentType = Type.bool;
789801
if (!type) return module.unreachable();
790-
return reifyConstantType(ctx, module.i32(type.isBooleanValue ? 1 : 0));
802+
return reifyConstantType(ctx, module.i32(type.isFloatValue ? 1 : 0));
791803
}
792-
builtins.set(BuiltinNames.isBoolean, builtin_isBoolean);
804+
builtins.set(BuiltinNames.isFloat, builtin_isFloat);
793805

794-
// isSigned<T!>() / isSigned<T?>(value: T) -> bool
795-
function builtin_isSigned(ctx: BuiltinContext): ExpressionRef {
806+
// isVector<T!>() / isVector<T?>(value: T) -> bool
807+
function builtin_isVector(ctx: BuiltinContext): ExpressionRef {
796808
var compiler = ctx.compiler;
797809
var module = compiler.module;
798810
var type = checkConstantType(ctx);
799811
compiler.currentType = Type.bool;
800812
if (!type) return module.unreachable();
801-
return reifyConstantType(ctx, module.i32(type.isSignedIntegerValue ? 1 : 0));
813+
return reifyConstantType(ctx, module.i32(type.isVectorValue ? 1 : 0));
802814
}
803-
builtins.set(BuiltinNames.isSigned, builtin_isSigned);
815+
builtins.set(BuiltinNames.isVector, builtin_isVector);
804816

805817
// isReference<T!>() / isReference<T?>(value: T) -> bool
806818
function builtin_isReference(ctx: BuiltinContext): ExpressionRef {

Diff for: std/assembly/builtins.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
type auto = i32;
22

3+
// @ts-ignore: decorator
4+
@builtin
5+
export declare function isBoolean<T>(value?: T): bool;
6+
37
// @ts-ignore: decorator
48
@builtin
59
export declare function isInteger<T>(value?: T): bool;
610

711
// @ts-ignore: decorator
812
@builtin
9-
export declare function isFloat<T>(value?: T): bool;
13+
export declare function isSigned<T>(value?: T): bool;
1014

1115
// @ts-ignore: decorator
1216
@builtin
13-
export declare function isBoolean<T>(value?: T): bool;
17+
export declare function isFloat<T>(value?: T): bool;
1418

1519
// @ts-ignore: decorator
1620
@builtin
17-
export declare function isSigned<T>(value?: T): bool;
21+
export declare function isVector<T>(value?: T): bool;
1822

1923
// @ts-ignore: decorator
2024
@builtin

Diff for: std/assembly/index.d.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,16 @@ declare function instantiate<T>(...args: any[]): T;
196196
declare function isNaN<T extends f32 | f64>(value: T): bool;
197197
/** Tests if a 32-bit or 64-bit float is finite, that is not `NaN` or +/-`Infinity`. */
198198
declare function isFinite<T extends f32 | f64>(value: T): bool;
199-
/** Tests if the specified type *or* expression is of an integer type and not a reference. Compiles to a constant. */
200-
declare function isInteger<T>(value?: any): value is number;
201-
/** Tests if the specified type *or* expression is of a float type. Compiles to a constant. */
202-
declare function isFloat<T>(value?: any): value is number;
203199
/** Tests if the specified type *or* expression is of a boolean type. */
204200
declare function isBoolean<T>(value?: any): value is number;
201+
/** Tests if the specified type *or* expression is of an integer type and not a reference. Compiles to a constant. */
202+
declare function isInteger<T>(value?: any): value is number;
205203
/** Tests if the specified type *or* expression can represent negative numbers. Compiles to a constant. */
206204
declare function isSigned<T>(value?: any): value is number;
205+
/** Tests if the specified type *or* expression is of a float type. Compiles to a constant. */
206+
declare function isFloat<T>(value?: any): value is number;
207+
/** Tests if the specified type *or* expression is of a v128 type. Compiles to a constant. */
208+
declare function isVector<T>(value?: any): value is v128;
207209
/** Tests if the specified type *or* expression is of a reference type. Compiles to a constant. */
208210
declare function isReference<T>(value?: any): value is object | string;
209211
/** Tests if the specified type *or* expression can be used as a string. Compiles to a constant. */

Diff for: tests/compiler/simd.debug.wat

+10
Original file line numberDiff line numberDiff line change
@@ -4657,6 +4657,16 @@
46574657
(func $start:simd
46584658
i32.const 1
46594659
drop
4660+
i32.const 1
4661+
drop
4662+
i32.const 0
4663+
i32.eqz
4664+
drop
4665+
i32.const 1
4666+
drop
4667+
i32.const 0
4668+
i32.eqz
4669+
drop
46604670
call $simd/test_v128
46614671
call $simd/test_i8x16
46624672
call $simd/test_i16x8

Diff for: tests/compiler/simd.ts

+7
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,13 @@ export function test_vars_f64x2_full(a: f64, b: f64): v128 {
760760
}
761761

762762
if (ASC_FEATURE_SIMD) {
763+
// test builtins
764+
assert(isVector<v128>());
765+
assert(!isVector<i32>());
766+
767+
assert(isVector(i32x4.splat(0)));
768+
assert(!isVector(0));
769+
763770
test_v128();
764771
test_i8x16();
765772
test_i16x8();

0 commit comments

Comments
 (0)