From fbf0053c248a58421829b628b7c0f62be62a77f1 Mon Sep 17 00:00:00 2001 From: David Mak Date: Mon, 30 Dec 2024 14:04:42 +0800 Subject: [PATCH] [core] irrt/string: Minor cleanup - Refactor __nac3_str_eq to always return bool - Use `get_usize_dependent_function_name` to get IRRT func name --- nac3core/irrt/irrt/string.hpp | 8 ++++---- nac3core/src/codegen/irrt/string.rs | 24 +++++++++++------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/nac3core/irrt/irrt/string.hpp b/nac3core/irrt/irrt/string.hpp index f695dcdc..db3ad7fd 100644 --- a/nac3core/irrt/irrt/string.hpp +++ b/nac3core/irrt/irrt/string.hpp @@ -4,20 +4,20 @@ namespace { template -SizeT __nac3_str_eq_impl(const char* str1, SizeT len1, const char* str2, SizeT len2) { +bool __nac3_str_eq_impl(const char* str1, SizeT len1, const char* str2, SizeT len2) { if (len1 != len2){ return 0; } - return (__builtin_memcmp(str1, str2, static_cast(len1)) == 0) ? 1 : 0; + return __builtin_memcmp(str1, str2, static_cast(len1)) == 0; } } // namespace extern "C" { -uint32_t nac3_str_eq(const char* str1, uint32_t len1, const char* str2, uint32_t len2) { +bool nac3_str_eq(const char* str1, uint32_t len1, const char* str2, uint32_t len2) { return __nac3_str_eq_impl(str1, len1, str2, len2); } -uint64_t nac3_str_eq64(const char* str1, uint64_t len1, const char* str2, uint64_t len2) { +bool nac3_str_eq64(const char* str1, uint64_t len1, const char* str2, uint64_t len2) { return __nac3_str_eq_impl(str1, len1, str2, len2); } } \ No newline at end of file diff --git a/nac3core/src/codegen/irrt/string.rs b/nac3core/src/codegen/irrt/string.rs index fb0f27b9..6ee40e45 100644 --- a/nac3core/src/codegen/irrt/string.rs +++ b/nac3core/src/codegen/irrt/string.rs @@ -1,7 +1,8 @@ use inkwell::values::{BasicValueEnum, CallSiteValue, IntValue, PointerValue}; use itertools::Either; -use crate::codegen::{macros::codegen_unreachable, CodeGenContext, CodeGenerator}; +use super::get_usize_dependent_function_name; +use crate::codegen::{CodeGenContext, CodeGenerator}; /// Generates a call to string equality comparison. Returns an `i1` representing whether the strings are equal. pub fn call_string_eq<'ctx, G: CodeGenerator + ?Sized>( @@ -12,16 +13,14 @@ pub fn call_string_eq<'ctx, G: CodeGenerator + ?Sized>( str2_ptr: PointerValue<'ctx>, str2_len: IntValue<'ctx>, ) -> IntValue<'ctx> { - let (func_name, return_type) = match ctx.ctx.i32_type().get_bit_width() { - 32 => ("nac3_str_eq", ctx.ctx.i32_type()), - 64 => ("nac3_str_eq64", ctx.ctx.i64_type()), - bw => codegen_unreachable!(ctx, "Unsupported size type bit width: {}", bw), - }; + let llvm_i1 = ctx.ctx.bool_type(); - let func = ctx.module.get_function(func_name).unwrap_or_else(|| { + let func_name = get_usize_dependent_function_name(generator, ctx, "nac3_str_eq"); + + let func = ctx.module.get_function(&func_name).unwrap_or_else(|| { ctx.module.add_function( - func_name, - return_type.fn_type( + &func_name, + llvm_i1.fn_type( &[ str1_ptr.get_type().into(), str1_len.get_type().into(), @@ -33,8 +32,8 @@ pub fn call_string_eq<'ctx, G: CodeGenerator + ?Sized>( None, ) }); - let result = ctx - .builder + + ctx.builder .build_call( func, &[str1_ptr.into(), str1_len.into(), str2_ptr.into(), str2_len.into()], @@ -43,6 +42,5 @@ pub fn call_string_eq<'ctx, G: CodeGenerator + ?Sized>( .map(CallSiteValue::try_as_basic_value) .map(|v| v.map_left(BasicValueEnum::into_int_value)) .map(Either::unwrap_left) - .unwrap(); - generator.bool_to_i1(ctx, result) + .unwrap() }