Skip to content

feat: Add isVector builtin #2369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ export namespace BuiltinNames {
export const trace = "~lib/builtins/trace";
export const seed = "~lib/builtins/seed";

export const isInteger = "~lib/builtins/isInteger";
export const isFloat = "~lib/builtins/isFloat";
export const isBoolean = "~lib/builtins/isBoolean";
export const isInteger = "~lib/builtins/isInteger";
export const isSigned = "~lib/builtins/isSigned";
export const isFloat = "~lib/builtins/isFloat";
export const isVector = "~lib/builtins/isVector";
export const isReference = "~lib/builtins/isReference";
export const isString = "~lib/builtins/isString";
export const isArray = "~lib/builtins/isArray";
Expand Down Expand Up @@ -758,6 +759,17 @@ export const function_builtins = new Map<string,(ctx: BuiltinContext) => Express

// === Static type evaluation =================================================================

// isBoolean<T!>() / isBoolean<T?>(value: T) -> bool
function builtin_isBoolean(ctx: BuiltinContext): ExpressionRef {
var compiler = ctx.compiler;
var module = compiler.module;
var type = checkConstantType(ctx);
compiler.currentType = Type.bool;
if (!type) return module.unreachable();
return reifyConstantType(ctx, module.i32(type.isBooleanValue ? 1 : 0));
}
builtins.set(BuiltinNames.isBoolean, builtin_isBoolean);

// isInteger<T!>() / isInteger<T?>(value: T) -> bool
function builtin_isInteger(ctx: BuiltinContext): ExpressionRef {
var compiler = ctx.compiler;
Expand All @@ -769,38 +781,38 @@ function builtin_isInteger(ctx: BuiltinContext): ExpressionRef {
}
builtins.set(BuiltinNames.isInteger, builtin_isInteger);

// isFloat<T!>() / isFloat<T?>(value: T) -> bool
function builtin_isFloat(ctx: BuiltinContext): ExpressionRef {
// isSigned<T!>() / isSigned<T?>(value: T) -> bool
function builtin_isSigned(ctx: BuiltinContext): ExpressionRef {
var compiler = ctx.compiler;
var module = compiler.module;
var type = checkConstantType(ctx);
compiler.currentType = Type.bool;
if (!type) return module.unreachable();
return reifyConstantType(ctx, module.i32(type.isFloatValue ? 1 : 0));
return reifyConstantType(ctx, module.i32(type.isSignedIntegerValue ? 1 : 0));
}
builtins.set(BuiltinNames.isFloat, builtin_isFloat);
builtins.set(BuiltinNames.isSigned, builtin_isSigned);

// isBoolean<T!>() / isBoolean<T?>(value: T) -> bool
function builtin_isBoolean(ctx: BuiltinContext): ExpressionRef {
// isFloat<T!>() / isFloat<T?>(value: T) -> bool
function builtin_isFloat(ctx: BuiltinContext): ExpressionRef {
var compiler = ctx.compiler;
var module = compiler.module;
var type = checkConstantType(ctx);
compiler.currentType = Type.bool;
if (!type) return module.unreachable();
return reifyConstantType(ctx, module.i32(type.isBooleanValue ? 1 : 0));
return reifyConstantType(ctx, module.i32(type.isFloatValue ? 1 : 0));
}
builtins.set(BuiltinNames.isBoolean, builtin_isBoolean);
builtins.set(BuiltinNames.isFloat, builtin_isFloat);

// isSigned<T!>() / isSigned<T?>(value: T) -> bool
function builtin_isSigned(ctx: BuiltinContext): ExpressionRef {
// isVector<T!>() / isVector<T?>(value: T) -> bool
function builtin_isVector(ctx: BuiltinContext): ExpressionRef {
var compiler = ctx.compiler;
var module = compiler.module;
var type = checkConstantType(ctx);
compiler.currentType = Type.bool;
if (!type) return module.unreachable();
return reifyConstantType(ctx, module.i32(type.isSignedIntegerValue ? 1 : 0));
return reifyConstantType(ctx, module.i32(type.isVectorValue ? 1 : 0));
}
builtins.set(BuiltinNames.isSigned, builtin_isSigned);
builtins.set(BuiltinNames.isVector, builtin_isVector);

// isReference<T!>() / isReference<T?>(value: T) -> bool
function builtin_isReference(ctx: BuiltinContext): ExpressionRef {
Expand Down
10 changes: 7 additions & 3 deletions std/assembly/builtins.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
type auto = i32;

// @ts-ignore: decorator
@builtin
export declare function isBoolean<T>(value?: T): bool;

// @ts-ignore: decorator
@builtin
export declare function isInteger<T>(value?: T): bool;

// @ts-ignore: decorator
@builtin
export declare function isFloat<T>(value?: T): bool;
export declare function isSigned<T>(value?: T): bool;

// @ts-ignore: decorator
@builtin
export declare function isBoolean<T>(value?: T): bool;
export declare function isFloat<T>(value?: T): bool;

// @ts-ignore: decorator
@builtin
export declare function isSigned<T>(value?: T): bool;
export declare function isVector<T>(value?: T): bool;

// @ts-ignore: decorator
@builtin
Expand Down
10 changes: 6 additions & 4 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,16 @@ declare function instantiate<T>(...args: any[]): T;
declare function isNaN<T extends f32 | f64>(value: T): bool;
/** Tests if a 32-bit or 64-bit float is finite, that is not `NaN` or +/-`Infinity`. */
declare function isFinite<T extends f32 | f64>(value: T): bool;
/** Tests if the specified type *or* expression is of an integer type and not a reference. Compiles to a constant. */
declare function isInteger<T>(value?: any): value is number;
/** Tests if the specified type *or* expression is of a float type. Compiles to a constant. */
declare function isFloat<T>(value?: any): value is number;
/** Tests if the specified type *or* expression is of a boolean type. */
declare function isBoolean<T>(value?: any): value is number;
/** Tests if the specified type *or* expression is of an integer type and not a reference. Compiles to a constant. */
declare function isInteger<T>(value?: any): value is number;
/** Tests if the specified type *or* expression can represent negative numbers. Compiles to a constant. */
declare function isSigned<T>(value?: any): value is number;
/** Tests if the specified type *or* expression is of a float type. Compiles to a constant. */
declare function isFloat<T>(value?: any): value is number;
/** Tests if the specified type *or* expression is of a v128 type. Compiles to a constant. */
declare function isVector<T>(value?: any): value is v128;
/** Tests if the specified type *or* expression is of a reference type. Compiles to a constant. */
declare function isReference<T>(value?: any): value is object | string;
/** Tests if the specified type *or* expression can be used as a string. Compiles to a constant. */
Expand Down
10 changes: 10 additions & 0 deletions tests/compiler/simd.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -4657,6 +4657,16 @@
(func $start:simd
i32.const 1
drop
i32.const 1
drop
i32.const 0
i32.eqz
drop
i32.const 1
drop
i32.const 0
i32.eqz
drop
call $simd/test_v128
call $simd/test_i8x16
call $simd/test_i16x8
Expand Down
7 changes: 7 additions & 0 deletions tests/compiler/simd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,13 @@ export function test_vars_f64x2_full(a: f64, b: f64): v128 {
}

if (ASC_FEATURE_SIMD) {
// test builtins
assert(isVector<v128>());
assert(!isVector<i32>());

assert(isVector(i32x4.splat(0)));
assert(!isVector(0));

test_v128();
test_i8x16();
test_i16x8();
Expand Down