Implement ternary using if, see AssemblyScript/assemblyscript#123

This commit is contained in:
dcodeIO
2017-12-04 14:49:24 +01:00
parent 558a4d5c63
commit c6af2d1454
11 changed files with 245 additions and 53 deletions

View File

@ -121,3 +121,14 @@ sizeof<f64>();
i = load<i32>(4);
store<i32>(4, i);
if (NaN == NaN)
unreachable();
if (!isNaN<f32>(NaN))
unreachable();
if (isFinite<f32>(NaN))
unreachable();
if (isFinite<f32>(Infinity))
unreachable();
if (!isFinite<f64>(0))
unreachable();

View File

@ -19,6 +19,10 @@
(local $5 f64)
(local $6 f64)
(local $7 f64)
(local $8 f32)
(local $9 f32)
(local $10 f32)
(local $11 f64)
(drop
(i32.clz
(i32.const 1)
@ -191,17 +195,17 @@
)
(drop
(select
(i32.const 0)
(f32.ne
(f32.abs
(get_local $1)
(tee_local $1
(f32.const 1.25)
)
)
(f32.const inf)
)
(f32.ne
(tee_local $1
(f32.const 1.25)
)
(i32.const 0)
(f32.eq
(get_local $1)
(get_local $1)
)
)
@ -270,17 +274,17 @@
)
(set_global $builtins/b
(select
(i32.const 0)
(f32.ne
(f32.abs
(get_local $3)
(tee_local $3
(f32.const 1.25)
)
)
(f32.const inf)
)
(f32.ne
(tee_local $3
(f32.const 1.25)
)
(i32.const 0)
(f32.eq
(get_local $3)
(get_local $3)
)
)
@ -355,17 +359,17 @@
)
(drop
(select
(i32.const 0)
(f64.ne
(f64.abs
(get_local $5)
(tee_local $5
(f64.const 1.25)
)
)
(f64.const inf)
)
(f64.ne
(tee_local $5
(f64.const 1.25)
)
(i32.const 0)
(f64.eq
(get_local $5)
(get_local $5)
)
)
@ -434,17 +438,17 @@
)
(set_global $builtins/b
(select
(i32.const 0)
(f64.ne
(f64.abs
(get_local $7)
(tee_local $7
(f64.const 1.25)
)
)
(f64.const inf)
)
(f64.ne
(tee_local $7
(f64.const 1.25)
)
(i32.const 0)
(f64.eq
(get_local $7)
(get_local $7)
)
)
@ -523,6 +527,80 @@
(i32.const 4)
(get_global $builtins/i)
)
(if
(f64.eq
(f64.const nan:0x8000000000000)
(f64.const nan:0x8000000000000)
)
(unreachable)
)
(if
(i32.eqz
(f32.ne
(tee_local $8
(f32.const nan:0x400000)
)
(get_local $8)
)
)
(unreachable)
)
(if
(select
(f32.ne
(f32.abs
(tee_local $9
(f32.const nan:0x400000)
)
)
(f32.const inf)
)
(i32.const 0)
(f32.eq
(get_local $9)
(get_local $9)
)
)
(unreachable)
)
(if
(select
(f32.ne
(f32.abs
(tee_local $10
(f32.const inf)
)
)
(f32.const inf)
)
(i32.const 0)
(f32.eq
(get_local $10)
(get_local $10)
)
)
(unreachable)
)
(if
(i32.eqz
(select
(f64.ne
(f64.abs
(tee_local $11
(f64.const 0)
)
)
(f64.const inf)
)
(i32.const 0)
(f64.eq
(get_local $11)
(get_local $11)
)
)
)
(unreachable)
)
)
)
(;

View File

@ -0,0 +1,5 @@
let a: i32;
a = 0 ? unreachable() : 1;
a = 1 ? 1 : unreachable();
a = (0 ? unreachable() : 1) ? 1 : unreachable();

View File

@ -0,0 +1,64 @@
(module
(type $v (func))
(global $ternary/a (mut i32) (i32.const 0))
(memory $0 1)
(data (i32.const 4) "\08\00\00\00")
(export "memory" (memory $0))
(start $start)
(func $start (; 0 ;) (type $v)
(set_global $ternary/a
(if (result i32)
(i32.const 0)
(unreachable)
(i32.const 1)
)
)
(set_global $ternary/a
(if (result i32)
(i32.const 1)
(i32.const 1)
(unreachable)
)
)
(set_global $ternary/a
(if (result i32)
(if (result i32)
(i32.const 0)
(unreachable)
(i32.const 1)
)
(i32.const 1)
(unreachable)
)
)
)
)
(;
[program.elements]
clz
ctz
popcnt
rotl
rotr
abs
ceil
copysign
floor
max
min
nearest
sqrt
trunc
current_memory
grow_memory
unreachable
isNaN
isFinite
assert
sizeof
load
store
ternary/a
[program.exports]
;)