rt: move mem intrinsics

This commit is contained in:
Mark Poliakov 2025-01-06 18:41:25 +02:00
parent 86f4a3f214
commit f978a6b287
2 changed files with 72 additions and 79 deletions

View File

@ -11,7 +11,7 @@
#![warn(missing_docs)]
#![allow(nonstandard_style, clippy::new_without_default)]
// #[cfg(not(feature = "rustc-dep-of-std"))]
#[cfg(not(rust_analyzer))]
#[allow(unused_extern_crates)]
extern crate compiler_builtins;
@ -24,92 +24,15 @@ pub use abi::error::Error;
pub use abi::path;
pub mod debug;
pub mod io;
pub mod mem;
pub mod net;
pub mod process;
pub mod sync;
pub mod sys;
pub mod time;
pub mod mem {
//! Memory-related data structures
pub use abi::mem::{MappingFlags, MappingSource};
}
pub mod system {
//! System-related data structures
pub use abi::system::*;
}
#[cfg(feature = "rustc-dep-of-std")]
use core::ffi::{c_char, c_void};
#[cfg(feature = "rustc-dep-of-std")]
#[no_mangle]
unsafe extern "C" fn bcmp(p0: *const c_void, p1: *const c_void, len: usize) -> i32 {
memcmp(p0, p1, len)
}
#[cfg(feature = "rustc-dep-of-std")]
#[no_mangle]
unsafe extern "C" fn memcmp(p0: *const c_void, p1: *const c_void, len: usize) -> i32 {
let mut offset = 0;
if len == 0 {
return 0;
}
while offset < len {
let c0 = (p0 as *const u8).add(offset).read_volatile();
let c1 = (p1 as *const u8).add(offset).read_volatile();
if c0 > c1 {
return (c0 - c1) as i32;
} else if c0 < c1 {
return -((c1 - c0) as i32);
}
offset += 1;
}
0
}
/// XXX
#[cfg(feature = "rustc-dep-of-std")]
#[no_mangle]
pub unsafe extern "C" fn memcpy(p0: *mut c_void, p1: *const c_void, len: usize) -> *mut c_void {
compiler_builtins::mem::memcpy(p0 as _, p1 as _, len) as _
}
#[cfg(feature = "rustc-dep-of-std")]
#[no_mangle]
unsafe extern "C" fn memmove(dst: *mut c_void, src: *const c_void, n: usize) -> *mut c_void {
compiler_builtins::mem::memmove(dst as _, src as _, n) as _
}
#[cfg(feature = "rustc-dep-of-std")]
#[no_mangle]
unsafe extern "C" fn memset(dst: *mut c_void, val: i32, len: usize) -> *mut c_void {
let mut offset = 0;
while offset < len {
(dst as *mut u8).add(offset).write_volatile(val as u8);
offset += 1;
}
dst
}
#[cfg(feature = "rustc-dep-of-std")]
#[no_mangle]
unsafe extern "C" fn strlen(mut s: *mut c_char) -> usize {
if s.is_null() {
return 0;
}
let mut len = 0;
while s.read() != 0 {
len += 1;
s = s.add(1);
}
len
}

70
lib/runtime/src/mem.rs Normal file
View File

@ -0,0 +1,70 @@
//! Memory-related data structures
pub use abi::mem::{MappingFlags, MappingSource};
#[cfg(any(feature = "rustc-dep-of-std", rust_analyzer))]
mod intrinsics {
use core::ffi::{c_char, c_void};
#[no_mangle]
unsafe extern "C" fn bcmp(p0: *const c_void, p1: *const c_void, len: usize) -> i32 {
memcmp(p0, p1, len)
}
#[no_mangle]
unsafe extern "C" fn memcmp(p0: *const c_void, p1: *const c_void, len: usize) -> i32 {
let mut offset = 0;
if len == 0 {
return 0;
}
while offset < len {
let c0 = (p0 as *const u8).add(offset).read_volatile();
let c1 = (p1 as *const u8).add(offset).read_volatile();
if c0 > c1 {
return (c0 - c1) as i32;
} else if c0 < c1 {
return -((c1 - c0) as i32);
}
offset += 1;
}
0
}
#[no_mangle]
pub unsafe extern "C" fn memcpy(p0: *mut c_void, p1: *const c_void, len: usize) -> *mut c_void {
compiler_builtins::mem::memcpy(p0 as _, p1 as _, len) as _
}
#[no_mangle]
unsafe extern "C" fn memmove(dst: *mut c_void, src: *const c_void, n: usize) -> *mut c_void {
compiler_builtins::mem::memmove(dst as _, src as _, n) as _
}
#[no_mangle]
unsafe extern "C" fn memset(dst: *mut c_void, val: i32, len: usize) -> *mut c_void {
let mut offset = 0;
while offset < len {
(dst as *mut u8).add(offset).write_volatile(val as u8);
offset += 1;
}
dst
}
#[no_mangle]
unsafe extern "C" fn strlen(mut s: *mut c_char) -> usize {
if s.is_null() {
return 0;
}
let mut len = 0;
while s.read() != 0 {
len += 1;
s = s.add(1);
}
len
}
}