diff --git a/src/compiler.ts b/src/compiler.ts index 3e721dd4b2..bc15cd2143 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -3647,43 +3647,29 @@ export class Compiler extends DiagnosticEmitter { // int to float } else if (fromType.isIntegerValue && toType.isFloatValue) { - + // Clear extra bits. + expr = this.ensureSmallIntegerWrap(expr, fromType); + let op: UnaryOp; // int to f32 if (toType.kind == TypeKind.F32) { if (fromType.isLongIntegerValue) { - expr = module.unary( - fromType.isSignedIntegerValue - ? UnaryOp.ConvertI64ToF32 - : UnaryOp.ConvertU64ToF32, - expr - ); + if (fromType.isSignedIntegerValue) op = UnaryOp.ConvertI64ToF32; + else op = UnaryOp.ConvertU64ToF32; } else { - expr = module.unary( - fromType.isSignedIntegerValue - ? UnaryOp.ConvertI32ToF32 - : UnaryOp.ConvertU32ToF32, - expr - ); + if (fromType.isSignedIntegerValue) op = UnaryOp.ConvertI32ToF32; + else op = UnaryOp.ConvertU32ToF32; } - // int to f64 } else { if (fromType.isLongIntegerValue) { - expr = module.unary( - fromType.isSignedIntegerValue - ? UnaryOp.ConvertI64ToF64 - : UnaryOp.ConvertU64ToF64, - expr - ); + if (fromType.isSignedIntegerValue) op = UnaryOp.ConvertI64ToF64; + else op = UnaryOp.ConvertU64ToF64; } else { - expr = module.unary( - fromType.isSignedIntegerValue - ? UnaryOp.ConvertI32ToF64 - : UnaryOp.ConvertU32ToF64, - expr - ); + if (fromType.isSignedIntegerValue) op = UnaryOp.ConvertI32ToF64; + else op = UnaryOp.ConvertU32ToF64; } } + expr = module.unary(op, expr); // v128 to bool } else if (fromType == Type.v128 && toType.isBooleanValue) { diff --git a/tests/compiler/number-convert.debug.wat b/tests/compiler/number-convert.debug.wat new file mode 100644 index 0000000000..b555a169ff --- /dev/null +++ b/tests/compiler/number-convert.debug.wat @@ -0,0 +1,157 @@ +(module + (type $0 (func)) + (global $~lib/memory/__data_end i32 (i32.const 8)) + (global $~lib/memory/__stack_pointer (mut i32) (i32.const 32776)) + (global $~lib/memory/__heap_base i32 (i32.const 32776)) + (memory $0 0) + (table $0 1 1 funcref) + (elem $0 (i32.const 1)) + (export "memory" (memory $0)) + (start $~start) + (func $start:number-convert + i32.const 1 + f32.convert_i32_u + f32.const 1 + f32.eq + drop + i32.const 255 + f32.convert_i32_u + f32.const 255 + f32.eq + drop + i32.const 256 + i32.const 255 + i32.and + f32.convert_i32_u + f32.const 0 + f32.eq + drop + i32.const 257 + i32.const 255 + i32.and + f32.convert_i32_u + f32.const 1 + f32.eq + drop + i32.const 1 + f32.convert_i32_s + f32.const 1 + f32.eq + drop + i32.const 255 + i32.extend8_s + f32.convert_i32_s + f32.const -1 + f32.eq + drop + i32.const 256 + i32.extend8_s + f32.convert_i32_s + f32.const 0 + f32.eq + drop + i32.const 257 + i32.extend8_s + f32.convert_i32_s + f32.const 1 + f32.eq + drop + i32.const 1 + f32.convert_i32_u + f32.const 1 + f32.eq + drop + i32.const 65535 + f32.convert_i32_u + f32.const 65535 + f32.eq + drop + i32.const 65536 + i32.const 65535 + i32.and + f32.convert_i32_u + f32.const 0 + f32.eq + drop + i32.const 65537 + i32.const 65535 + i32.and + f32.convert_i32_u + f32.const 1 + f32.eq + drop + i32.const 1 + f32.convert_i32_s + f32.const 1 + f32.eq + drop + i32.const 65535 + i32.extend16_s + f32.convert_i32_s + f32.const -1 + f32.eq + drop + i32.const 65536 + i32.extend16_s + f32.convert_i32_s + f32.const 0 + f32.eq + drop + i32.const 65537 + i32.extend16_s + f32.convert_i32_s + f32.const 1 + f32.eq + drop + i32.const 1 + f64.convert_i32_u + f64.const 1 + f64.eq + drop + i32.const 255 + f64.convert_i32_u + f64.const 255 + f64.eq + drop + i32.const 256 + i32.const 255 + i32.and + f64.convert_i32_u + f64.const 0 + f64.eq + drop + i32.const 257 + i32.const 255 + i32.and + f64.convert_i32_u + f64.const 1 + f64.eq + drop + i32.const 1 + f64.convert_i32_s + f64.const 1 + f64.eq + drop + i32.const 255 + i32.extend8_s + f64.convert_i32_s + f64.const -1 + f64.eq + drop + i32.const 256 + i32.extend8_s + f64.convert_i32_s + f64.const 0 + f64.eq + drop + i32.const 257 + i32.extend8_s + f64.convert_i32_s + f64.const 1 + f64.eq + drop + ) + (func $~start + call $start:number-convert + ) +) diff --git a/tests/compiler/number-convert.json b/tests/compiler/number-convert.json new file mode 100644 index 0000000000..1bdd02b1be --- /dev/null +++ b/tests/compiler/number-convert.json @@ -0,0 +1,4 @@ +{ + "asc_flags": [ + ] +} diff --git a/tests/compiler/number-convert.release.wat b/tests/compiler/number-convert.release.wat new file mode 100644 index 0000000000..23da3862e2 --- /dev/null +++ b/tests/compiler/number-convert.release.wat @@ -0,0 +1,4 @@ +(module + (memory $0 0) + (export "memory" (memory $0)) +) diff --git a/tests/compiler/number-convert.ts b/tests/compiler/number-convert.ts new file mode 100644 index 0000000000..5eb4bc47f1 --- /dev/null +++ b/tests/compiler/number-convert.ts @@ -0,0 +1,29 @@ +assert((1) == 1); +assert((255) == 255); +assert((256) == 0); +assert((257) == 1); + +assert((1) == 1); +assert((255) == -1); +assert((256) == 0); +assert((257) == 1); + +assert((1) == 1); +assert((65535) == 65535); +assert((65536) == 0); +assert((65537) == 1); + +assert((1) == 1); +assert((65535) == -1); +assert((65536) == 0); +assert((65537) == 1); + +assert((1) == 1); +assert((255) == 255); +assert((256) == 0); +assert((257) == 1); + +assert((1) == 1); +assert((255) == -1); +assert((256) == 0); +assert((257) == 1);