From 0b6a9bd89bc4725771be193ce7bdf612a7271962 Mon Sep 17 00:00:00 2001 From: ram Date: Fri, 13 Dec 2024 15:43:50 +0000 Subject: [PATCH] Updated to use memcmp instead of strncmp --- nac3core/irrt/irrt/string.hpp | 2 +- nac3core/src/codegen/irrt/string.rs | 39 +++++++++++++---------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/nac3core/irrt/irrt/string.hpp b/nac3core/irrt/irrt/string.hpp index 0f28d5de..f695dcdc 100644 --- a/nac3core/irrt/irrt/string.hpp +++ b/nac3core/irrt/irrt/string.hpp @@ -8,7 +8,7 @@ SizeT __nac3_str_eq_impl(const char* str1, SizeT len1, const char* str2, SizeT l if (len1 != len2){ return 0; } - return (__builtin_strncmp(str1, str2, static_cast(len1)) == 0) ? 1 : 0; + return (__builtin_memcmp(str1, str2, static_cast(len1)) == 0) ? 1 : 0; } } // namespace diff --git a/nac3core/src/codegen/irrt/string.rs b/nac3core/src/codegen/irrt/string.rs index 0a9d3a13..039c7273 100644 --- a/nac3core/src/codegen/irrt/string.rs +++ b/nac3core/src/codegen/irrt/string.rs @@ -1,7 +1,4 @@ -use inkwell::{ - values::{BasicValueEnum, CallSiteValue, IntValue, PointerValue}, - AddressSpace, -}; +use inkwell::values::{BasicValueEnum, CallSiteValue, IntValue, PointerValue}; use itertools::Either; use crate::codegen::{CodeGenContext, CodeGenerator}; @@ -15,27 +12,27 @@ pub fn call_string_eq<'ctx, G: CodeGenerator + ?Sized>( str2_ptr: PointerValue<'ctx>, str2_len: IntValue<'ctx>, ) -> IntValue<'ctx> { - let string_eq_fn = ctx.module.get_function("nac3_str_eq").unwrap_or_else(|| { - let i8_ptr_type = ctx.ctx.i8_type().ptr_type(AddressSpace::default()); - let i64_type = ctx.ctx.i64_type(); - let i32_type = ctx.ctx.i32_type(); - let fn_type = i32_type.fn_type( - &[i8_ptr_type.into(), i64_type.into(), i8_ptr_type.into(), i64_type.into()], - false, - ); - ctx.module.add_function("nac3_str_eq", fn_type, None) + let func = ctx.module.get_function("nac3_str_eq").unwrap_or_else(|| { + ctx.module.add_function( + "nac3_str_eq", + ctx.ctx.i32_type().fn_type( + &[ + str1_ptr.get_type().into(), + str1_len.get_type().into(), + str2_ptr.get_type().into(), + str2_len.get_type().into(), + ], + false, + ), + None, + ) }); let result = ctx .builder .build_call( - string_eq_fn, - &[ - str1_ptr.into(), - 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", + func, + &[str1_ptr.into(), str1_len.into(), str2_ptr.into(), str2_len.into()], + "str_eq_call", ) .map(CallSiteValue::try_as_basic_value) .map(|v| v.map_left(BasicValueEnum::into_int_value))