Skip to content

Commit

Permalink
Use *mut u8 instead of *mut c_void for pointers.
Browse files Browse the repository at this point in the history
And switch to `add` from `byte_add`.
  • Loading branch information
sunfishcode committed Feb 27, 2024
1 parent 8427d4a commit 9d66ba9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
52 changes: 26 additions & 26 deletions crates/rust/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,14 @@ impl Bindgen for FunctionBindgen<'_, '_> {
self.import_return_pointer_area_align.max(align);
uwrite!(
self.src,
"let ptr{tmp} = ret_area.0.as_mut_ptr().cast::<::core::ffi::c_void>();"
"let ptr{tmp} = ret_area.0.as_mut_ptr().cast::<u8>();"
);
} else {
self.gen.return_pointer_area_size = self.gen.return_pointer_area_size.max(size);
self.gen.return_pointer_area_align = self.gen.return_pointer_area_align.max(align);
uwriteln!(
self.src,
"let ptr{tmp} = _RET_AREA.0.as_mut_ptr().cast::<::core::ffi::c_void>();"
"let ptr{tmp} = _RET_AREA.0.as_mut_ptr().cast::<u8>();"
);
}
format!("ptr{}", tmp)
Expand Down Expand Up @@ -659,7 +659,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
self.push_str(&format!("let {} = ({}).into_boxed_slice();\n", val, op0));
}
self.push_str(&format!(
"let {} = {}.as_ptr().cast::<::core::ffi::c_void>();\n",
"let {} = {}.as_ptr().cast::<u8>();\n",
ptr, val
));
self.push_str(&format!("let {} = {}.len();\n", len, val));
Expand Down Expand Up @@ -694,7 +694,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
self.push_str(&format!("let {} = ({}).into_boxed_slice();\n", val, op0));
}
self.push_str(&format!(
"let {} = {}.as_ptr().cast::<::core::ffi::c_void>();\n",
"let {} = {}.as_ptr().cast::<u8>();\n",
ptr, val
));
self.push_str(&format!("let {} = {}.len();\n", len, val));
Expand Down Expand Up @@ -742,14 +742,14 @@ impl Bindgen for FunctionBindgen<'_, '_> {
));
self.push_str(&format!("let {result} = if {layout}.size() != 0 {{\n"));
self.push_str(&format!(
"let ptr = {alloc}::alloc({layout}).cast::<::core::ffi::c_void>();\n",
"let ptr = {alloc}::alloc({layout}).cast::<u8>();\n",
));
self.push_str(&format!(
"if ptr.is_null()\n{{\n{alloc}::handle_alloc_error({layout});\n}}\nptr\n}}",
));
self.push_str("else {{\n::core::ptr::null_mut()\n}};\n");
self.push_str(&format!("for (i, e) in {vec}.into_iter().enumerate() {{\n",));
self.push_str(&format!("let base = {result}.byte_add(i * {size});\n",));
self.push_str(&format!("let base = {result}.add(i * {size});\n",));
self.push_str(&body);
self.push_str("\n}\n");
results.push(format!("{result}"));
Expand Down Expand Up @@ -785,7 +785,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
));

uwriteln!(self.src, "for i in 0..{len} {{");
uwriteln!(self.src, "let base = {base}.byte_add(i * {size});");
uwriteln!(self.src, "let base = {base}.add(i * {size});");
uwriteln!(self.src, "let e{tmp} = {body};");
uwriteln!(self.src, "{result}.push(e{tmp});");
uwriteln!(self.src, "}}");
Expand Down Expand Up @@ -877,7 +877,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let tmp = self.tmp();
uwriteln!(
self.src,
"let l{tmp} = *{}.byte_add({offset}).cast::<i32>();",
"let l{tmp} = *{}.add({offset}).cast::<i32>();",
operands[0]
);
results.push(format!("l{tmp}"));
Expand All @@ -886,7 +886,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let tmp = self.tmp();
uwriteln!(
self.src,
"let l{tmp} = i32::from(*{}.byte_add({offset}).cast::<u8>());",
"let l{tmp} = i32::from(*{}.add({offset}).cast::<u8>());",
operands[0]
);
results.push(format!("l{tmp}"));
Expand All @@ -895,7 +895,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let tmp = self.tmp();
uwriteln!(
self.src,
"let l{tmp} = i32::from(*{}.byte_add({offset}).cast::<i8>());",
"let l{tmp} = i32::from(*{}.add({offset}).cast::<i8>());",
operands[0]
);
results.push(format!("l{tmp}"));
Expand All @@ -904,7 +904,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let tmp = self.tmp();
uwriteln!(
self.src,
"let l{tmp} = i32::from(*{}.byte_add({offset}).cast::<u16>());",
"let l{tmp} = i32::from(*{}.add({offset}).cast::<u16>());",
operands[0]
);
results.push(format!("l{tmp}"));
Expand All @@ -913,7 +913,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let tmp = self.tmp();
uwriteln!(
self.src,
"let l{tmp} = i32::from(*{}.byte_add({offset}).cast::<i16>());",
"let l{tmp} = i32::from(*{}.add({offset}).cast::<i16>());",
operands[0]
);
results.push(format!("l{tmp}"));
Expand All @@ -922,7 +922,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let tmp = self.tmp();
uwriteln!(
self.src,
"let l{tmp} = *{}.byte_add({offset}).cast::<i64>();",
"let l{tmp} = *{}.add({offset}).cast::<i64>();",
operands[0]
);
results.push(format!("l{tmp}"));
Expand All @@ -931,7 +931,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let tmp = self.tmp();
uwriteln!(
self.src,
"let l{tmp} = *{}.byte_add({offset}).cast::<f32>();",
"let l{tmp} = *{}.add({offset}).cast::<f32>();",
operands[0]
);
results.push(format!("l{tmp}"));
Expand All @@ -940,7 +940,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let tmp = self.tmp();
uwriteln!(
self.src,
"let l{tmp} = *{}.byte_add({offset}).cast::<f64>();",
"let l{tmp} = *{}.add({offset}).cast::<f64>();",
operands[0]
);
results.push(format!("l{tmp}"));
Expand All @@ -950,7 +950,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let tmp = self.tmp();
uwriteln!(
self.src,
"let l{tmp} = *{}.byte_add({offset}).cast::<*mut ::core::ffi::c_void>();",
"let l{tmp} = *{}.add({offset}).cast::<*mut u8>();",
operands[0]
);
results.push(format!("l{tmp}"));
Expand All @@ -959,58 +959,58 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let tmp = self.tmp();
uwriteln!(
self.src,
"let l{tmp} = *{}.byte_add({offset}).cast::<usize>();",
"let l{tmp} = *{}.add({offset}).cast::<usize>();",
operands[0]
);
results.push(format!("l{tmp}"));
}

Instruction::I32Store { offset } => {
self.push_str(&format!(
"*{}.byte_add({}).cast::<i32>() = {};\n",
"*{}.add({}).cast::<i32>() = {};\n",
operands[1], offset, operands[0]
));
}
Instruction::I32Store8 { offset } => {
self.push_str(&format!(
"*{}.byte_add({}).cast::<u8>() = ({}) as u8;\n",
"*{}.add({}).cast::<u8>() = ({}) as u8;\n",
operands[1], offset, operands[0]
));
}
Instruction::I32Store16 { offset } => {
self.push_str(&format!(
"*{}.byte_add({}).cast::<u16>() = ({}) as u16;\n",
"*{}.add({}).cast::<u16>() = ({}) as u16;\n",
operands[1], offset, operands[0]
));
}
Instruction::I64Store { offset } => {
self.push_str(&format!(
"*{}.byte_add({}).cast::<i64>() = {};\n",
"*{}.add({}).cast::<i64>() = {};\n",
operands[1], offset, operands[0]
));
}
Instruction::F32Store { offset } => {
self.push_str(&format!(
"*{}.byte_add({}).cast::<f32>() = {};\n",
"*{}.add({}).cast::<f32>() = {};\n",
operands[1], offset, operands[0]
));
}
Instruction::F64Store { offset } => {
self.push_str(&format!(
"*{}.byte_add({}).cast::<f64>() = {};\n",
"*{}.add({}).cast::<f64>() = {};\n",
operands[1], offset, operands[0]
));
}

Instruction::PointerStore { offset } => {
self.push_str(&format!(
"*{}.byte_add({}).cast::<*mut ::core::ffi::c_void>() = {};\n",
"*{}.add({}).cast::<*mut u8>() = {};\n",
operands[1], offset, operands[0]
));
}
Instruction::LengthStore { offset } => {
self.push_str(&format!(
"*{}.byte_add({}).cast::<usize>() = {};\n",
"*{}.add({}).cast::<usize>() = {};\n",
operands[1], offset, operands[0]
));
}
Expand Down Expand Up @@ -1075,7 +1075,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
self.push_str(" {\n");
self.push_str("let base = ");
self.push_str(&base);
self.push_str(".byte_add(i * ");
self.push_str(".add(i * ");
self.push_str(&size.to_string());
self.push_str(");\n");
self.push_str(&body);
Expand Down
10 changes: 5 additions & 5 deletions crates/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ impl RustWasm {
self.rt_module.insert(RuntimeItem::StdAllocModule);
self.src.push_str(
"\
pub unsafe fn cabi_dealloc(ptr: *mut ::core::ffi::c_void, size: usize, align: usize) {
pub unsafe fn cabi_dealloc(ptr: *mut u8, size: usize, align: usize) {
if size == 0 {
return;
}
Expand Down Expand Up @@ -1248,7 +1248,7 @@ fn wasm_type(ty: WasmType) -> &'static str {
WasmType::I64 => "i64",
WasmType::F32 => "f32",
WasmType::F64 => "f64",
WasmType::Pointer => "*mut ::core::ffi::c_void",
WasmType::Pointer => "*mut u8",
WasmType::Length => "usize",

// `PointerOrI64` can hold either a `u64` or a pointer with provenance.
Expand Down Expand Up @@ -1293,7 +1293,7 @@ fn bitcast(casts: &[Bitcast], operands: &[String], results: &mut Vec<String>) {
format!(
"{{
let mut t = ::core::mem::MaybeUnunit::<u64>::uninit();
t.as_mut_ptr().cast::<*mut core::ptr::c_void>().write({});
t.as_mut_ptr().cast::<*mut u8>().write({});
t
}}",
operand
Expand All @@ -1303,13 +1303,13 @@ fn bitcast(casts: &[Bitcast], operands: &[String], results: &mut Vec<String>) {
// the pointer value.
Bitcast::P64ToP => {
format!(
"{}.as_mut_ptr().cast::<*mut core::ptr::c_void>().read()",
"{}.as_mut_ptr().cast::<*mut u8>().read()",
operand
)
}
// Convert an `i32` into a pointer.
Bitcast::I32ToP => {
format!("{} as *mut ::core::ffi::c_void", operand)
format!("{} as *mut u8", operand)
}
// Convert a pointer holding an `i32` value back into the `i32`.
Bitcast::PToI32 => {
Expand Down

0 comments on commit 9d66ba9

Please sign in to comment.