Skip to content

Commit

Permalink
Change float32/float64 to f32/f64 in the C bindings generator. (#916)
Browse files Browse the repository at this point in the history
* Change float32/float64 to f32/f64 in the C bindings generator.

Change float32/float64 to f32/f64 in the C bindings generator, in
the `Instruction` enum, and in the tests.

* Update test names in Go.

* Update more tests.
  • Loading branch information
sunfishcode authored Apr 1, 2024
1 parent 730113b commit 2339d53
Show file tree
Hide file tree
Showing 27 changed files with 164 additions and 168 deletions.
12 changes: 6 additions & 6 deletions crates/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,8 @@ pub fn push_ty_name(resolve: &Resolve, ty: &Type, src: &mut String) {
Type::S32 => src.push_str("s32"),
Type::U64 => src.push_str("u64"),
Type::S64 => src.push_str("s64"),
Type::F32 => src.push_str("float32"),
Type::F64 => src.push_str("float64"),
Type::F32 => src.push_str("f32"),
Type::F64 => src.push_str("f64"),
Type::String => src.push_str("string"),
Type::Id(id) => {
let ty = &resolve.types[*id];
Expand Down Expand Up @@ -2264,10 +2264,10 @@ impl Bindgen for FunctionBindgen<'_, '_> {

// f32/f64 have the same representation in the import type and in C,
// so no conversions necessary.
Instruction::F32FromFloat32
| Instruction::F64FromFloat64
| Instruction::Float32FromF32
| Instruction::Float64FromF64 => {
Instruction::CoreF32FromF32
| Instruction::CoreF64FromF64
| Instruction::F32FromCoreF32
| Instruction::F64FromCoreF64 => {
results.push(operands[0].clone());
}

Expand Down
16 changes: 8 additions & 8 deletions crates/core/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ def_instruction! {
/// This may be a noop for some implementations, but it's here in case the
/// native language representation of `f32` is different than the wasm
/// representation of `f32`.
F32FromFloat32 : [1] => [1],
CoreF32FromF32 : [1] => [1],
/// Conversion an interface type `f64` value to a wasm `f64`.
///
/// This may be a noop for some implementations, but it's here in case the
/// native language representation of `f64` is different than the wasm
/// representation of `f64`.
F64FromFloat64 : [1] => [1],
CoreF64FromF64 : [1] => [1],

/// Converts a native wasm `i32` to an interface type `s8`.
///
Expand Down Expand Up @@ -211,9 +211,9 @@ def_instruction! {
/// It's safe to assume that the `i32` is indeed a valid unicode code point.
CharFromI32 : [1] => [1],
/// Converts a native wasm `f32` to an interface type `f32`.
Float32FromF32 : [1] => [1],
F32FromCoreF32 : [1] => [1],
/// Converts a native wasm `f64` to an interface type `f64`.
Float64FromF64 : [1] => [1],
F64FromCoreF64 : [1] => [1],

/// Creates a `bool` from an `i32` input, trapping if the `i32` isn't
/// zero or one.
Expand Down Expand Up @@ -1067,8 +1067,8 @@ impl<'a, B: Bindgen> Generator<'a, B> {
Type::S64 => self.emit(&I64FromS64),
Type::U64 => self.emit(&I64FromU64),
Type::Char => self.emit(&I32FromChar),
Type::F32 => self.emit(&F32FromFloat32),
Type::F64 => self.emit(&F64FromFloat64),
Type::F32 => self.emit(&CoreF32FromF32),
Type::F64 => self.emit(&CoreF64FromF64),
Type::String => {
let realloc = self.list_realloc();
self.emit(&StringLower { realloc });
Expand Down Expand Up @@ -1256,8 +1256,8 @@ impl<'a, B: Bindgen> Generator<'a, B> {
Type::S64 => self.emit(&S64FromI64),
Type::U64 => self.emit(&U64FromI64),
Type::Char => self.emit(&CharFromI32),
Type::F32 => self.emit(&Float32FromF32),
Type::F64 => self.emit(&Float64FromF64),
Type::F32 => self.emit(&F32FromCoreF32),
Type::F64 => self.emit(&F64FromCoreF64),
Type::String => self.emit(&StringLift),
Type::Id(id) => match &self.resolve.types[id].kind {
TypeDefKind::Type(t) => self.lift(t),
Expand Down
8 changes: 4 additions & 4 deletions crates/csharp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1779,10 +1779,10 @@ impl Bindgen for FunctionBindgen<'_, '_> {
| Instruction::I32FromU8
| Instruction::I32FromS8
| Instruction::I32FromS32
| Instruction::Float32FromF32
| Instruction::F32FromFloat32
| Instruction::F64FromFloat64
| Instruction::Float64FromF64
| Instruction::F32FromCoreF32
| Instruction::CoreF32FromF32
| Instruction::CoreF64FromF64
| Instruction::F64FromCoreF64
| Instruction::S32FromI32
| Instruction::S64FromI64 => results.push(operands[0].clone()),

Expand Down
8 changes: 4 additions & 4 deletions crates/rust/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,16 @@ impl Bindgen for FunctionBindgen<'_, '_> {
results.push(format!("{}({s})", self.gen.path_to_as_i32()));
}

Instruction::F32FromFloat32 => {
Instruction::CoreF32FromF32 => {
let s = operands.pop().unwrap();
results.push(format!("{}({s})", self.gen.path_to_as_f32()));
}
Instruction::F64FromFloat64 => {
Instruction::CoreF64FromF64 => {
let s = operands.pop().unwrap();
results.push(format!("{}({s})", self.gen.path_to_as_f64()));
}
Instruction::Float32FromF32
| Instruction::Float64FromF64
Instruction::F32FromCoreF32
| Instruction::F64FromCoreF64
| Instruction::S32FromI32
| Instruction::S64FromI64 => {
results.push(operands.pop().unwrap());
Expand Down
8 changes: 4 additions & 4 deletions crates/teavm-java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1324,10 +1324,10 @@ impl Bindgen for FunctionBindgen<'_, '_> {
| Instruction::I32FromS32
| Instruction::I64FromS64
| Instruction::I64FromU64
| Instruction::F32FromFloat32
| Instruction::F64FromFloat64
| Instruction::Float32FromF32
| Instruction::Float64FromF64 => results.push(operands[0].clone()),
| Instruction::CoreF32FromF32
| Instruction::CoreF64FromF64
| Instruction::F32FromCoreF32
| Instruction::F64FromCoreF64 => results.push(operands[0].clone()),

Instruction::Bitcasts { casts } => results.extend(
casts
Expand Down
8 changes: 4 additions & 4 deletions tests/codegen/floats.wit
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package foo:foo;

interface floats {
float32-param: func(x: float32);
float64-param: func(x: float64);
float32-result: func() -> float32;
float64-result: func() -> float64;
f32-param: func(x: f32);
f64-param: func(x: f64);
f32-result: func() -> f32;
f64-result: func() -> f64;
}

world the-world {
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/issue573.wit
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ world types-example {
}

export f-f1: func(typedef: t10) -> t10;
export f1: func(f: float32, f-list: list<tuple<char, float64>>) -> (val-p1: s64, val2: string);
export f1: func(f: f32, f-list: list<tuple<char, f64>>) -> (val-p1: s64, val2: string);
/// t2 has been renamed with `use self.types-interface.{t2 as t2-renamed}`
export re-named: func(perm: option<permissions>, e: option<empty>) -> t2-renamed;
export re-named2: func(tup: tuple<list<u16>>, e: empty) -> tuple<option<u8>, s8>;
Expand Down
12 changes: 6 additions & 6 deletions tests/codegen/lists.wit
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ interface lists {
list-s16-param: func(x: list<s16>);
list-s32-param: func(x: list<s32>);
list-s64-param: func(x: list<s64>);
list-float32-param: func(x: list<float32>);
list-float64-param: func(x: list<float64>);
list-f32-param: func(x: list<f32>);
list-f64-param: func(x: list<f64>);

list-u8-ret: func() -> list<u8>;
list-u16-ret: func() -> list<u16>;
Expand All @@ -20,8 +20,8 @@ interface lists {
list-s16-ret: func() -> list<s16>;
list-s32-ret: func() -> list<s32>;
list-s64-ret: func() -> list<s64>;
list-float32-ret: func() -> list<float32>;
list-float64-ret: func() -> list<float64>;
list-f32-ret: func() -> list<f32>;
list-f64-ret: func() -> list<f64>;

tuple-list: func(x: list<tuple<u8, s8>>) -> list<tuple<s64, u32>>;
string-list-arg: func(a: list<string>);
Expand Down Expand Up @@ -72,8 +72,8 @@ interface lists {
s32,
u64,
s64,
float32,
float64,
f32,
f64,
char,
>>;
load-store-everything: func(a: load-store-all-sizes) -> load-store-all-sizes;
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/multi-return.wit
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface multi-return {
mrb: func() -> ();
mrc: func() -> u32;
mrd: func() -> (a: u32);
mre: func() -> (a: u32, b: float32);
mre: func() -> (a: u32, b: f32);
}

world the-world {
Expand Down
18 changes: 9 additions & 9 deletions tests/codegen/resources.wit
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package my:resources;

interface types {
resource z {
constructor(a: float64);
constructor(a: f64);
}
}

Expand Down Expand Up @@ -38,19 +38,19 @@ world resources {

export exports: interface {
resource x {
constructor(a: float64);
get-a: func() -> float64;
set-a: func(a: float64);
add: static func(x: x, a: float64) -> x;
constructor(a: f64);
get-a: func() -> f64;
set-a: func(a: f64);
add: static func(x: x, a: f64) -> x;
}
}

import imports: interface {
resource y {
constructor(a: float64);
get-a: func() -> float64;
set-a: func(a: float64);
add: static func(y: y, a: float64) -> y;
constructor(a: f64);
get-a: func() -> f64;
set-a: func(a: f64);
add: static func(y: y, a: f64) -> y;
}
}
}
16 changes: 8 additions & 8 deletions tests/codegen/variants.wit
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,30 @@ interface variants {
b: option<tuple<u32>>,
c: option<u32>,
d: option<e1>,
e: option<float32>,
e: option<f32>,
g: option<option<bool>>,
);
option-result: func() -> tuple<
option<bool>,
option<tuple<u32>>,
option<u32>,
option<e1>,
option<float32>,
option<f32>,
option<option<bool>>,
>;

variant casts1 {
a(s32),
b(float32),
b(f32),
}

variant casts2 {
a(float64),
b(float32),
a(f64),
b(f32),
}

variant casts3 {
a(float64),
a(f64),
b(u64),
}

Expand All @@ -67,12 +67,12 @@ interface variants {
}

variant casts5 {
a(float32),
a(f32),
b(s64),
}

variant casts6 {
a(tuple<float32, u32>),
a(tuple<f32, u32>),
b(tuple<u32, u32>),
}

Expand Down
24 changes: 12 additions & 12 deletions tests/runtime/lists/wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,17 @@ void lists_test_imports() {
{
float f32[4] = {-FLT_MAX, FLT_MAX, -INFINITY, INFINITY};
double f64[4] = {-DBL_MAX, DBL_MAX, -INFINITY, INFINITY};
lists_list_float32_t list_float32 = { f32, 4 };
lists_list_float64_t list_float64 = { f64, 4 };
lists_list_float32_t list_float32_out;
lists_list_float64_t list_float64_out;
test_lists_test_list_minmax_float(&list_float32, &list_float64, &list_float32_out, &list_float64_out);
assert(list_float32_out.len == 4 && list_float32_out.ptr[0] == -FLT_MAX && list_float32_out.ptr[1] == FLT_MAX);
assert(list_float32_out.ptr[2] == -INFINITY && list_float32_out.ptr[3] == INFINITY);
assert(list_float64_out.len == 4 && list_float64_out.ptr[0] == -DBL_MAX && list_float64_out.ptr[1] == DBL_MAX);
assert(list_float64_out.ptr[2] == -INFINITY && list_float64_out.ptr[3] == INFINITY);
lists_list_float32_free(&list_float32_out);
lists_list_float64_free(&list_float64_out);
lists_list_f32_t list_f32 = { f32, 4 };
lists_list_f64_t list_f64 = { f64, 4 };
lists_list_f32_t list_f32_out;
lists_list_f64_t list_f64_out;
test_lists_test_list_minmax_float(&list_f32, &list_f64, &list_f32_out, &list_f64_out);
assert(list_f32_out.len == 4 && list_f32_out.ptr[0] == -FLT_MAX && list_f32_out.ptr[1] == FLT_MAX);
assert(list_f32_out.ptr[2] == -INFINITY && list_f32_out.ptr[3] == INFINITY);
assert(list_f64_out.len == 4 && list_f64_out.ptr[0] == -DBL_MAX && list_f64_out.ptr[1] == DBL_MAX);
assert(list_f64_out.ptr[2] == -INFINITY && list_f64_out.ptr[3] == INFINITY);
lists_list_f32_free(&list_f32_out);
lists_list_f64_free(&list_f64_out);
}
}

Expand Down Expand Up @@ -352,6 +352,6 @@ void exports_test_lists_test_list_minmax64(lists_list_u64_t *a, lists_list_s64_t
assert(0); // unimplemented
}

void exports_test_lists_test_list_minmax_float(lists_list_float32_t *a, lists_list_float64_t *b, lists_list_float32_t *ret0, lists_list_float64_t *ret1) {
void exports_test_lists_test_list_minmax_float(lists_list_f32_t *a, lists_list_f64_t *b, lists_list_f32_t *ret0, lists_list_f64_t *ret1) {
assert(0); // unimplemented
}
4 changes: 2 additions & 2 deletions tests/runtime/lists/world.wit
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ interface test {
list-minmax16: func(a: list<u16>, b: list<s16>) -> (a: list<u16>, b: list<s16>);
list-minmax32: func(a: list<u32>, b: list<s32>) -> (a: list<u32>, b: list<s32>);
list-minmax64: func(a: list<u64>, b: list<s64>) -> (a: list<u64>, b: list<s64>);
list-minmax-float: func(a: list<float32>, b: list<float64>)
-> (a: list<float32>, b: list<float64>);
list-minmax-float: func(a: list<f32>, b: list<f64>)
-> (a: list<f32>, b: list<f64>);

list-roundtrip: func(a: list<u8>) -> list<u8>;

Expand Down
24 changes: 10 additions & 14 deletions tests/runtime/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ impl test::numbers::test::Host for MyImports {
Ok(val)
}

fn roundtrip_float32(&mut self, val: f32) -> Result<f32> {
fn roundtrip_f32(&mut self, val: f32) -> Result<f32> {
Ok(val)
}

fn roundtrip_float64(&mut self, val: f64) -> Result<f64> {
fn roundtrip_f64(&mut self, val: f64) -> Result<f64> {
Ok(val)
}

Expand Down Expand Up @@ -156,31 +156,27 @@ fn run_test(exports: Numbers, store: &mut Store<crate::Wasi<MyImports>>) -> Resu
i64::max_value()
);

assert_eq!(exports.call_roundtrip_float32(&mut *store, 1.0)?, 1.0);
assert_eq!(exports.call_roundtrip_f32(&mut *store, 1.0)?, 1.0);
assert_eq!(
exports.call_roundtrip_float32(&mut *store, f32::INFINITY)?,
exports.call_roundtrip_f32(&mut *store, f32::INFINITY)?,
f32::INFINITY
);
assert_eq!(
exports.call_roundtrip_float32(&mut *store, f32::NEG_INFINITY)?,
exports.call_roundtrip_f32(&mut *store, f32::NEG_INFINITY)?,
f32::NEG_INFINITY
);
assert!(exports
.call_roundtrip_float32(&mut *store, f32::NAN)?
.is_nan());
assert!(exports.call_roundtrip_f32(&mut *store, f32::NAN)?.is_nan());

assert_eq!(exports.call_roundtrip_float64(&mut *store, 1.0)?, 1.0);
assert_eq!(exports.call_roundtrip_f64(&mut *store, 1.0)?, 1.0);
assert_eq!(
exports.call_roundtrip_float64(&mut *store, f64::INFINITY)?,
exports.call_roundtrip_f64(&mut *store, f64::INFINITY)?,
f64::INFINITY
);
assert_eq!(
exports.call_roundtrip_float64(&mut *store, f64::NEG_INFINITY)?,
exports.call_roundtrip_f64(&mut *store, f64::NEG_INFINITY)?,
f64::NEG_INFINITY
);
assert!(exports
.call_roundtrip_float64(&mut *store, f64::NAN)?
.is_nan());
assert!(exports.call_roundtrip_f64(&mut *store, f64::NAN)?.is_nan());

assert_eq!(exports.call_roundtrip_char(&mut *store, 'a')?, 'a');
assert_eq!(exports.call_roundtrip_char(&mut *store, ' ')?, ' ');
Expand Down
Loading

0 comments on commit 2339d53

Please sign in to comment.