1
0
forked from M-Labs/nac3

Updated to use memcmp instead of strncmp

This commit is contained in:
ram 2024-12-13 15:43:50 +00:00
parent 9b0d37b1f0
commit 0b6a9bd89b
2 changed files with 19 additions and 22 deletions

View File

@ -8,7 +8,7 @@ SizeT __nac3_str_eq_impl(const char* str1, SizeT len1, const char* str2, SizeT l
if (len1 != len2){ if (len1 != len2){
return 0; return 0;
} }
return (__builtin_strncmp(str1, str2, static_cast<SizeT>(len1)) == 0) ? 1 : 0; return (__builtin_memcmp(str1, str2, static_cast<SizeT>(len1)) == 0) ? 1 : 0;
} }
} // namespace } // namespace

View File

@ -1,7 +1,4 @@
use inkwell::{ use inkwell::values::{BasicValueEnum, CallSiteValue, IntValue, PointerValue};
values::{BasicValueEnum, CallSiteValue, IntValue, PointerValue},
AddressSpace,
};
use itertools::Either; use itertools::Either;
use crate::codegen::{CodeGenContext, CodeGenerator}; use crate::codegen::{CodeGenContext, CodeGenerator};
@ -15,27 +12,27 @@ pub fn call_string_eq<'ctx, G: CodeGenerator + ?Sized>(
str2_ptr: PointerValue<'ctx>, str2_ptr: PointerValue<'ctx>,
str2_len: IntValue<'ctx>, str2_len: IntValue<'ctx>,
) -> IntValue<'ctx> { ) -> IntValue<'ctx> {
let string_eq_fn = ctx.module.get_function("nac3_str_eq").unwrap_or_else(|| { let func = ctx.module.get_function("nac3_str_eq").unwrap_or_else(|| {
let i8_ptr_type = ctx.ctx.i8_type().ptr_type(AddressSpace::default()); ctx.module.add_function(
let i64_type = ctx.ctx.i64_type(); "nac3_str_eq",
let i32_type = ctx.ctx.i32_type(); ctx.ctx.i32_type().fn_type(
let fn_type = i32_type.fn_type( &[
&[i8_ptr_type.into(), i64_type.into(), i8_ptr_type.into(), i64_type.into()], str1_ptr.get_type().into(),
false, str1_len.get_type().into(),
); str2_ptr.get_type().into(),
ctx.module.add_function("nac3_str_eq", fn_type, None) str2_len.get_type().into(),
],
false,
),
None,
)
}); });
let result = ctx let result = ctx
.builder .builder
.build_call( .build_call(
string_eq_fn, func,
&[ &[str1_ptr.into(), str1_len.into(), str2_ptr.into(), str2_len.into()],
str1_ptr.into(), "str_eq_call",
ctx.builder.build_int_z_extend(str1_len, ctx.ctx.i64_type(), "").unwrap().into(),
str2_ptr.into(),
ctx.builder.build_int_z_extend(str2_len, ctx.ctx.i64_type(), "").unwrap().into(),
],
"string_eq",
) )
.map(CallSiteValue::try_as_basic_value) .map(CallSiteValue::try_as_basic_value)
.map(|v| v.map_left(BasicValueEnum::into_int_value)) .map(|v| v.map_left(BasicValueEnum::into_int_value))