Skip to content

Commit

Permalink
Fix bitcasts between i32 and pointer.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfishcode committed Feb 27, 2024
1 parent d2327ab commit 7fdf8ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion crates/core/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,8 @@ pub enum Bitcast {
I64ToP64,
P64ToP,
PToP64,
I32ToP,
PToI32,

None,
}
Expand Down Expand Up @@ -1901,13 +1903,16 @@ fn cast(from: WasmType, to: WasmType) -> Bitcast {
(Pointer, PointerOrI64) => Bitcast::PToP64,
(PointerOrI64, Pointer) => Bitcast::P64ToP,

(I32, Pointer) => Bitcast::I32ToP,
(Pointer, I32) => Bitcast::PToI32,

(Pointer | PointerOrI64 | Length, _)
| (_, Pointer | PointerOrI64 | Length)
| (F32, F64)
| (F64, F32)
| (F64, I32)
| (I32, F64) => {
unreachable!()
unreachable!("Don't know how to bitcast from {:?} to {:?}", from, to);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions crates/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,14 @@ fn bitcast(casts: &[Bitcast], operands: &[String], results: &mut Vec<String>) {
operand
)
}
// Convert an `i32` into a pointer.
Bitcast::I32ToP => {
format!("{} as *mut ::core::ffi::c_void", operand)
}
// Convert a pointer holding an `i32` value back into the `i32`.
Bitcast::PToI32 => {
format!("{} as i32", operand)
}
});
}
}
Expand Down

0 comments on commit 7fdf8ca

Please sign in to comment.