Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cranelift: Add LibCall::Memcmp #2953

Merged
merged 3 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cranelift/codegen/src/ir/libcall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub enum LibCall {
Memset,
/// libc.memmove
Memmove,
/// libc.memcmp
Memcmp,

/// Elf __tls_get_addr
ElfTlsGetAddr,
Expand Down Expand Up @@ -92,6 +94,7 @@ impl FromStr for LibCall {
"Memcpy" => Ok(Self::Memcpy),
"Memset" => Ok(Self::Memset),
"Memmove" => Ok(Self::Memmove),
"Memcmp" => Ok(Self::Memcmp),

"ElfTlsGetAddr" => Ok(Self::ElfTlsGetAddr),
_ => Err(()),
Expand Down Expand Up @@ -157,6 +160,7 @@ impl LibCall {
Memcpy,
Memset,
Memmove,
Memcmp,
ElfTlsGetAddr,
]
}
Expand Down Expand Up @@ -201,4 +205,11 @@ mod tests {
fn parsing() {
assert_eq!("FloorF32".parse(), Ok(LibCall::FloorF32));
}

#[test]
fn all_libcalls_to_from_string() {
for &libcall in LibCall::all_libcalls() {
assert_eq!(libcall.to_string().parse(), Ok(libcall));
}
}
}
27 changes: 27 additions & 0 deletions cranelift/codegen/src/ir/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ impl Type {
}

/// Get an integer type with the requested number of bits.
///
/// For the same thing but in *bytes*, use [`Self::int_with_byte_size`].
pub fn int(bits: u16) -> Option<Self> {
match bits {
8 => Some(I8),
Expand All @@ -115,6 +117,13 @@ impl Type {
}
}

/// Get an integer type with the requested number of bytes.
///
/// For the same thing but in *bits*, use [`Self::int`].
pub fn int_with_byte_size(bytes: u16) -> Option<Self> {
Self::int(bytes.checked_mul(8)?)
}

/// Get a type with the same number of lanes as `self`, but using `lane` as the lane type.
fn replace_lanes(self, lane: Self) -> Self {
debug_assert!(lane.is_lane() && !self.is_special());
Expand Down Expand Up @@ -595,4 +604,22 @@ mod tests {
assert_eq!(B8.as_int(), I8);
assert_eq!(B128.as_int(), I128);
}

#[test]
fn int_from_size() {
assert_eq!(Type::int(0), None);
assert_eq!(Type::int(8), Some(I8));
assert_eq!(Type::int(33), None);
assert_eq!(Type::int(64), Some(I64));

assert_eq!(Type::int_with_byte_size(0), None);
assert_eq!(Type::int_with_byte_size(2), Some(I16));
assert_eq!(Type::int_with_byte_size(6), None);
assert_eq!(Type::int_with_byte_size(16), Some(I128));

// Ensure `int_with_byte_size` handles overflow properly
let evil = 0xE001_u16;
assert_eq!(evil.wrapping_mul(8), 8, "check the constant is correct");
assert_eq!(Type::int_with_byte_size(evil), None);
}
}
Loading