Skip to content

Commit aa67016

Browse files
committed
make memcmp return a value of c_int_width instead of i32
1 parent 8f96ef4 commit aa67016

File tree

7 files changed

+22
-4
lines changed

7 files changed

+22
-4
lines changed

compiler/rustc_codegen_gcc/src/common.rs

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
9191
self.const_uint(self.type_i1(), val as u64)
9292
}
9393

94+
fn const_i16(&self, i: i16) -> RValue<'gcc> {
95+
self.const_int(self.type_i16(), i as i64)
96+
}
97+
9498
fn const_i32(&self, i: i32) -> RValue<'gcc> {
9599
self.const_int(self.type_i32(), i as i64)
96100
}

compiler/rustc_codegen_llvm/src/common.rs

+4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
147147
self.const_uint(self.type_i1(), val as u64)
148148
}
149149

150+
fn const_i16(&self, i: i16) -> &'ll Value {
151+
self.const_int(self.type_i16(), i as i64)
152+
}
153+
150154
fn const_i32(&self, i: i32) -> &'ll Value {
151155
self.const_int(self.type_i32(), i as i64)
152156
}

compiler/rustc_codegen_llvm/src/context.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,10 @@ impl<'ll> CodegenCx<'ll, '_> {
859859

860860
// This isn't an "LLVM intrinsic", but LLVM's optimization passes
861861
// recognize it like one and we assume it exists in `core::slice::cmp`
862-
ifn!("memcmp", fn(i8p, i8p, t_isize) -> t_i32);
862+
match self.sess().target.arch.as_str() {
863+
"avr" | "msp430" => ifn!("memcmp", fn(i8p, i8p, t_isize) -> t_i16),
864+
_ => ifn!("memcmp", fn(i8p, i8p, t_isize) -> t_i32),
865+
}
863866

864867
// variadic intrinsics
865868
ifn!("llvm.va_start", fn(i8p) -> void);

compiler/rustc_codegen_llvm/src/intrinsic.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,10 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
329329
let b_ptr = self.bitcast(b, i8p_ty);
330330
let n = self.const_usize(layout.size().bytes());
331331
let cmp = self.call_intrinsic("memcmp", &[a_ptr, b_ptr, n]);
332-
self.icmp(IntPredicate::IntEQ, cmp, self.const_i32(0))
332+
match self.cx.sess().target.arch.as_str() {
333+
"avr" | "msp430" => self.icmp(IntPredicate::IntEQ, cmp, self.const_i16(0)),
334+
_ => self.icmp(IntPredicate::IntEQ, cmp, self.const_i32(0)),
335+
}
333336
}
334337
}
335338

compiler/rustc_codegen_ssa/src/traits/consts.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub trait ConstMethods<'tcx>: BackendTypes {
1313
fn const_uint(&self, t: Self::Type, i: u64) -> Self::Value;
1414
fn const_uint_big(&self, t: Self::Type, u: u128) -> Self::Value;
1515
fn const_bool(&self, val: bool) -> Self::Value;
16+
fn const_i16(&self, i: i16) -> Self::Value;
1617
fn const_i32(&self, i: i32) -> Self::Value;
1718
fn const_u32(&self, i: u32) -> Self::Value;
1819
fn const_u64(&self, i: u64) -> Self::Value;

library/core/src/ffi/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ type_alias! { "c_schar.md", c_schar = i8, NonZero_c_schar = NonZeroI8; }
5757
type_alias! { "c_uchar.md", c_uchar = u8, NonZero_c_uchar = NonZeroU8; }
5858
type_alias! { "c_short.md", c_short = i16, NonZero_c_short = NonZeroI16; }
5959
type_alias! { "c_ushort.md", c_ushort = u16, NonZero_c_ushort = NonZeroU16; }
60+
#[cfg(any(target_arch = "avr", target_arch = "msp430"))]
61+
type_alias! { "c_int.md", c_int = i16, NonZero_c_int = NonZeroI16; }
62+
#[cfg(not(any(target_arch = "avr", target_arch = "msp430")))]
6063
type_alias! { "c_int.md", c_int = i32, NonZero_c_int = NonZeroI32; }
6164
type_alias! { "c_uint.md", c_uint = u32, NonZero_c_uint = NonZeroU32; }
6265
type_alias! { "c_long.md", c_long = i32, NonZero_c_long = NonZeroI32;

library/core/src/slice/cmp.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Comparison traits for `[T]`.
22
33
use crate::cmp::{self, Ordering};
4+
use crate::ffi;
45
use crate::mem;
56

67
use super::from_raw_parts;
@@ -13,8 +14,7 @@ extern "C" {
1314
///
1415
/// Returns 0 for equal, < 0 for less than and > 0 for greater
1516
/// than.
16-
// FIXME(#32610): Return type should be c_int
17-
fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32;
17+
fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> ffi::c_int;
1818
}
1919

2020
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)