Skip to content

Polymorphic operators #784

@AndrewScheidecker

Description

@AndrewScheidecker

I count 174 opcodes in use right now. It seems inevitable that there will eventually be more than 256, but in the mean time the number could be reduced substantially by making more operators polymorphic. In addition to freeing up some index space, making the opcodes polymorphic allows the operators to be extended to new types added in the future: e.g. f16, i8, or f32x4.

This would be ad-hoc polymorphism; a little bit different from the existing parametric operators like get_local and select. However, numeric, integer, and float type classes can be used to specify the domain of some of these operators.

Here's an example that replaces 125 of the opcodes with 56 (which would reduce the total to 105):

new (polymorphic) old (i32) old (i64) old (f32) old (f64)
num.add i32.add i64.add f32.add f64.add
num.sub i32.sub i64.sub f32.sub f64.sub
num.mul i32.mul i64.mul f32.mul f64.mul
num.div_s i32.div_s i64.div_s f32.div_s f64.div_s
num.neg i32.neg i64.neg f32.neg f64.neg
num.eq i32.eq i64.eq f32.eq f64.eq
num.ne i32.ne i64.ne f32.ne f64.ne
num.lt_s i32.lt_s i64.lt_s f32.lt f64.lt
num.le_s i32.le_s i64.le_s f32.le f64.le
num.gt_s i32.gt_s i64.gt_s f32.gt f64.gt
num.ge_s i32.ge_s i64.ge_s f32.ge f64.ge
int.div_u i32.div_u i64.div_u
int.rem_s i32.rem_s i64.rem_s
int.rem_u i32.rem_u i64.rem_u
int.and i32.and i64.and
int.or i32.or i64.or
int.xor i32.xor i64.xor
int.shl i32.shl i64.shl
int.shr_u i32.shr_u i64.shr_u
int.shr_s i32.shr_s i64.shr_s
int.rotr i32.rotr i64.rotr
int.rotl i32.rotl i64.rotl
int.lt_u i32.lt_u i64.lt_u
int.le_u i32.le_u i64.le_u
int.gt_u i32.gt_u i64.gt_u
int.ge_u i32.ge_u i64.ge_u
int.clz i32.clz i64.clz
int.ctz i32.ctz i64.ctz
int.eqz i32.eqz i64.eqz
int.popcnt i32.popcnt i64.popcnt
fp.min f32.min f64.min
fp.max f32.max f64.max
fp.abs f32.abs f64.abs
fp.copysign f32.copysign f64.copysign
fp.ceil f32.ceil f64.ceil
fp.floor f32.floor f64.floor
fp.trunc f32.trunc f64.trunc
fp.nearest f32.nearest f64.nearest
fp.sqrt f32.sqrt f64.sqrt
i32.trunc_s i32.trunc_s/f32 i32.trunc_s/f64
i32.trunc_u i32.trunc_u/f32 i32.trunc_u/f64
i64.trunc_s i64.trunc_s/f32 i64.trunc_s/f64
i64.trunc_u i64.trunc_u/f32 i64.trunc_u/f64
i32.wrap i32.wrap/i64
i64.extend_s i64.extend_s/i32
i64.extend_u i64.extend_u/i32
f32.convert_s f32.convert_s/i32 f32.convert_s/i64
f32.convert_u f32.convert_u/i32 f32.convert_u/i64
f64.convert_s f64.convert_s/i32 f64.convert_s/i64
f64.convert_u f64.convert_u/i32 f64.convert_u/i64
f64.promote f64.promote/f32
f32.demote f32.demote/f64
f32.reinterpret f32.reinterpret/i32
f64.reinterpret f64.reinterpret/i64
i32.reinterpret i32.reinterpret/f32
i64.reinterpret i64.reinterpret/f64

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions