From d07769044ff532ae01de55d7703c100d6731ad8b Mon Sep 17 00:00:00 2001 From: kevstone Date: Thu, 22 Feb 2024 09:54:49 +0000 Subject: [PATCH] Added 4 more level-2 compiling accessors --- .../default_conversion_validation.py | 3 +++ ..._human_readable_conversion_pack_updates.py | 19 +++++++++++++++ .../grib_accessor_conversion_validation.py | 20 ++++++++++++++++ ...cessor_special_function_call_conversion.py | 1 - .../grib_value_funcsig_mappings.py | 6 +++++ .../accessor/AccessorUtils/ConversionHelper.h | 24 +++++++++++++++---- src/cpp/eccodes/accessor/GribCpp/GribValue.cc | 12 ++++++++++ src/cpp/eccodes/accessor/GribCpp/GribValue.h | 4 ++++ 8 files changed, 83 insertions(+), 6 deletions(-) create mode 100755 src/clang_convert/grib_accessor/grib_accessor_conversion_pack/conversion_pack_updates/step_human_readable_conversion_pack_updates.py diff --git a/src/clang_convert/default/default_conversion_pack/default_conversion_validation.py b/src/clang_convert/default/default_conversion_pack/default_conversion_validation.py index ef49e03d1..b4cf8ad4b 100755 --- a/src/clang_convert/default/default_conversion_pack/default_conversion_validation.py +++ b/src/clang_convert/default/default_conversion_pack/default_conversion_validation.py @@ -225,6 +225,9 @@ class DefaultConversionValidation(conversion_validation.ConversionValidation): # We're indexing into a container... cpparray_access = array_access.ArrayAccess(literal.Literal(cpparg.name), cppright) return cpparray_access + # .data() version causes issues + #cppleft = literal.Literal(f"{cpparg.name}.data()") + #return binary_operation.BinaryOperation(cppleft, cppbinary_op, cppright) elif cppbinary_op.is_arithmetic(): cpparg = arg_utils.to_cpparg(cppleft, self._conversion_data) diff --git a/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/conversion_pack_updates/step_human_readable_conversion_pack_updates.py b/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/conversion_pack_updates/step_human_readable_conversion_pack_updates.py new file mode 100755 index 000000000..3739418a0 --- /dev/null +++ b/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/conversion_pack_updates/step_human_readable_conversion_pack_updates.py @@ -0,0 +1,19 @@ + +import utils.debug as debug +import grib_accessor.grib_accessor_conversion_pack.conversion_pack_updates.base_conversion_pack_updates as base_conversion_pack_updates +from code_object.arg import Arg +from code_object_converter.conversion_pack.funcsig_mapping import FuncSigMapping +from code_object_converter.conversion_pack.arg_indexes import ArgIndexes +from code_object.funcsig import FuncSig +from code_object.code_interface import NONE_VALUE + +class StepHumanReadableConversionPackUpdates(base_conversion_pack_updates.BaseConversionPackUpdates): + def __init__(self) -> None: + super().__init__() + + self._funcsig_mappings.extend([ + # template static int unpack_helper(grib_accessor* a, T* val, size_t* len) + FuncSigMapping(FuncSig("int", "get_step_human_readable", [Arg("grib_handle*", "h"), Arg("char*", "result"), Arg("size_t*", "length")]), + FuncSig("GribStatus", "getStepHumanReadable", [NONE_VALUE, Arg("std::string&", "result"), NONE_VALUE]), + ArgIndexes(cbuffer=1, clength=2, cpp_container=1)), + ]) diff --git a/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/grib_accessor_conversion_validation.py b/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/grib_accessor_conversion_validation.py index 211c4a26b..e906c14cf 100755 --- a/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/grib_accessor_conversion_validation.py +++ b/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/grib_accessor_conversion_validation.py @@ -18,6 +18,7 @@ import code_object.cast_expression as cast_expression import code_object.macro_instantation as macro_instantation import code_object.struct_member_access as struct_member_access import code_object.array_access as array_access +from code_object.arg import Arg from utils.string_funcs import strip_semicolon from grib_accessor.grib_accessor_conversion_pack.grib_accessor_special_function_call_conversion import special_function_name_mapping @@ -142,6 +143,11 @@ class GribAccessorConversionValidation(default_conversion_validation.DefaultConv updated_cppfunction_call = self.validate_function_call_args(updated_cppfunction_call, mapping.cppfuncsig) debug.line("apply_special_function_call_conversions", f"Updated C++ function call=[{debug.as_debug_string(cppfunction_call)}] to [{debug.as_debug_string(updated_cppfunction_call)}]") return updated_cppfunction_call + + if cppfunction_call.name == "eccGribGetStringLength": + updated_args = [literal.Literal("AccessorName(name_)")] + updated_args.append(cppfunction_call.args[1].operand) # strip leading & - so this arg is a unary_operation + return function_call.FunctionCall(cppfunction_call.name, updated_args) for cfuncname, cppfuncname in special_function_name_mapping.items(): if cfunction_call.name == cfuncname: @@ -150,6 +156,20 @@ class GribAccessorConversionValidation(default_conversion_validation.DefaultConv else: return as_commented_out_code(cfunction_call, f"Removed call to {cfuncname}") + if cppfunction_call.name == "memcpy": + updated_args = [arg for arg in cppfunction_call.args] + if isinstance(updated_args[1], array_access.ArrayAccess): + argname = arg_utils.extract_name(updated_args[1]) + assert argname + argvalue = strip_semicolon(updated_args[1].index.as_string()) + updated_args[1] = binary_operation.BinaryOperation(literal.Literal(f"{argname}.data()"), + "+", + literal.Literal(argvalue)) + updated_cppfunction_call = function_call.FunctionCall("copyBuffer", updated_args) + debug.line("apply_special_function_call_conversions", f"Updated memcpy: [{debug.as_debug_string(cppfunction_call)}]->[{debug.as_debug_string(updated_cppfunction_call)}]") + return updated_cppfunction_call + + return None def validate_variable_declaration(self, cvariable_declaration, cppvariable_declaration): diff --git a/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/grib_accessor_special_function_call_conversion.py b/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/grib_accessor_special_function_call_conversion.py index cc264e7bb..23ac7eb33 100755 --- a/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/grib_accessor_special_function_call_conversion.py +++ b/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/grib_accessor_special_function_call_conversion.py @@ -7,7 +7,6 @@ special_function_name_mapping = { "fgets" : "fgetsFromString", "grib_context_log" : "gribLog", "codes_fopen" : "fopenFromString", - "memcpy" : "copyBuffer", "strncpy" : "copyString", # Deleted functions "grib_context_free" : "", diff --git a/src/clang_convert/grib_accessor/supporting/funcsig_mappings/grib_cpp_funcsig_mappings/grib_value_funcsig_mappings.py b/src/clang_convert/grib_accessor/supporting/funcsig_mappings/grib_cpp_funcsig_mappings/grib_value_funcsig_mappings.py index 5fb5eb21a..0ea393268 100755 --- a/src/clang_convert/grib_accessor/supporting/funcsig_mappings/grib_cpp_funcsig_mappings/grib_value_funcsig_mappings.py +++ b/src/clang_convert/grib_accessor/supporting/funcsig_mappings/grib_cpp_funcsig_mappings/grib_value_funcsig_mappings.py @@ -81,4 +81,10 @@ grib_value_funcsig_mappings = [ FuncSigMapping( FuncSig("int", "grib_set_long_array_internal", [Arg("const grib_handle*", "h"), Arg("const char*", "name"), Arg("long*", "val"), Arg("size_t*", "length")]), FuncSig("GribStatus", "gribSetLong", [NONE_VALUE, Arg("AccessorName const&", "name"), Arg("std::vector const&", "value")])), + + FuncSigMapping( FuncSig("int", "grib_get_string_length", [Arg("const grib_handle*", "h"), Arg("const char*", "name"), Arg("size_t*", "size")]), + FuncSig("GribStatus", "gribGetStringLength", [NONE_VALUE, Arg("AccessorName const&", "name"), Arg("size_t&", "size")])), + + FuncSigMapping( FuncSig("int", "ecc__grib_get_string_length", [Arg("grib_accessor*", "a"), Arg("size_t*", "size")]), + FuncSig("GribStatus", "eccGribGetStringLength", [Arg("AccessorName const&", "name"), Arg("size_t&", "size")])), ] diff --git a/src/cpp/eccodes/accessor/AccessorUtils/ConversionHelper.h b/src/cpp/eccodes/accessor/AccessorUtils/ConversionHelper.h index 624175049..db0814fb7 100644 --- a/src/cpp/eccodes/accessor/AccessorUtils/ConversionHelper.h +++ b/src/cpp/eccodes/accessor/AccessorUtils/ConversionHelper.h @@ -46,15 +46,29 @@ int scanString(std::string buffer, size_t offset, std::string format, Args&&... // Helper to support memcpy from a C buffer into a std::vector template -void copyBuffer(std::vector& out, IN_TYPE* in, size_t numBytes) +//void copyBuffer(std::vector& out, IN_TYPE const* in, size_t numBytes) +void copyBuffer(OUT_TYPE& out, IN_TYPE const* in, size_t numBytes) { - assert(sizeof(OUT_TYPE)==sizeof(IN_TYPE)); - size_t numEntries = numBytes/sizeof(IN_TYPE); - - std::copy(out.begin(), out.begin() + numEntries, in); + + std::vector inVec(in, in + numEntries); + + // TODO: Check if back_inserter required + std::copy(inVec.begin(), inVec.end(), out.begin()); } +// Helper to support memcpy from a C buffer into a std::string +/* +void copyBuffer(std::string& out, char const* in, size_t numBytes) +{ + size_t numEntries = numBytes/sizeof(char); + + std::string inStr(in, in + numEntries); + + // TODO: Check if back_inserter required + std::copy(inStr.begin(), inStr.end(), out.begin()); +}*/ + // Helper to replace strncpy void copyString(std::string& dest, std::string src, size_t count); diff --git a/src/cpp/eccodes/accessor/GribCpp/GribValue.cc b/src/cpp/eccodes/accessor/GribCpp/GribValue.cc index 0d99e5b2a..1e8eea843 100644 --- a/src/cpp/eccodes/accessor/GribCpp/GribValue.cc +++ b/src/cpp/eccodes/accessor/GribCpp/GribValue.cc @@ -248,4 +248,16 @@ GribStatus gribSetString(AccessorName const& name, std::string value) return GribStatus{ret}; } +GribStatus gribGetStringLength(AccessorName const& name, size_t& size) +{ + Assert(false); + return GribStatus::NOT_IMPLEMENTED; +} + +GribStatus eccGribGetStringLength(AccessorName const& name, size_t& size) +{ + Assert(false); + return GribStatus::NOT_IMPLEMENTED; +} + } diff --git a/src/cpp/eccodes/accessor/GribCpp/GribValue.h b/src/cpp/eccodes/accessor/GribCpp/GribValue.h index b6621940c..befc7521a 100644 --- a/src/cpp/eccodes/accessor/GribCpp/GribValue.h +++ b/src/cpp/eccodes/accessor/GribCpp/GribValue.h @@ -56,4 +56,8 @@ GribStatus gribSetArray(AccessorName const& name, std::vector const& value) } } +// Stub functions (TO DO) +GribStatus gribGetStringLength(AccessorName const& name, size_t& size); +GribStatus eccGribGetStringLength(AccessorName const& name, size_t& size); + } \ No newline at end of file