From: Robert Dubner mailto:[email protected]
Date: Tue, 30 Jun 2026 09:59:01 -0400
Subject: [PATCH] cobol: Improve conversions; reduce aliasing; address
big-endian architectures.
This patch comprises three sets of related changes.
1) Considerable effort has been made to consolidate and improve
interconversions between various COBOL data types, both in generated
GENERIC
and in libgcobol.so.
2) An ongoing effort is underway to identify and eliminate data
assignments
involving pointer casting that risk aliasing problems. (It turns out that
forty years of C programming experience sometimes means that one ends up
writing, with a great deal of assurance, code that is bad in a modern
compiler.) Some of the changes here still risk aliasing; it is an ongoing
effort.
3) Building and running GCOBOL on a big-endian S390 system has exposed a
glorious number of things that don't work because of that self-same
forty years of C programming experience on the little-endian X86_64
architecture. Some of these changes are the beginning of writing code
that is endian agnostic in C form; some use BYTES_BIG_ENDIAN in the front
end to address endianness of the target during compile-time code
generation; and some use __BYTE_ORDER__ to handle endianness in both the
host at compile time and the target in the run-time library.
gcc/cobol/ChangeLog:
* compare.cc (alpha_compare_figconst): Make the code
endian-agnostic.
(alpha_compare): Likewise.
(numeric_alpha_compare): Likewise.
(float_compare): Force proper data comparisons.
* genapi.cc (parser_goto): Use improved get_binary_value()
routine.
(parser_perform_times): Likewise.
(internal_perform_through_times): Likewise.
(parser_display_internal): Set formal parameter to const.
(program_end_stuff): Use get_binary_value().
(parser_relop_long): Likewise.
(parser_see_stop_run): Likewise.
(parser_perform): Set formal parameter to const.
(parser_perform_inline_times): Use new get_binary_value().
(parser_file_write): Likewise.
(parser_file_start): Likewise.
(handle_gg_trim): Endian.
(parser_trim): Likewise.
(field_increment): Use new get_binary_value().
(parser_lsearch_start): Likewise.
(parser_release): Set formal parameter to const.
(parser_match_exception): Use new get_binary_value().
(parser_field_attr_set): Set formal parameter to const.
* genapi.h (parser_display_internal): Likewise.
(parser_perform_times): Likewise.
(parser_perform): Likewise.
(parser_goto): Likewise.
(parser_field_attr_set): Likewise.
(parser_file_start): Likewise.
(parser_release): Likewise.
* gengen.cc (gg_assign): Comment.
* genmath.cc (fast_add): New get_binary_value; address aliasing.
(fast_subtract): Likewise.
(fast_multiply): Likewise.
(fast_divide): Likewise.
* genutil.cc (get_scaled_rdigits): Set formal parameter to const.
(get_scaled_digits): Set formal parameter to const.
(get_integer_value): Improved data conversion; make local static.
(CHECK_FOR_FRACTIONAL_DIGITS): Moved here from genutil.h.
(giv_helper): New get_integer_value helper routine.
(get_and_check_refstart_and_reflen): get_binary_value and
aliasing.
(get_depending_on_value_from_odo): Set some variables to const.
(get_data_offset): Use new get_integer_value() routine.
(digit): Eliminate as part of improved get_binary_value()
conversion.
(num_disp_dive): Likewise.
(pd_dive): Likewise.
(get_pd_value): Likewise.
(get_binary_value_tree): Likewise.
(get_binary_value): New binary conversion routines.
(field_is_super_clean): Likewise.
(refer_is_working_storage): Likewise.
(is_working_storage): Likewise.
(refer_refmod_length): Likewise.
(binary_from_FldNumericBin5): Likewise.
(binary_from_FldLiteralN): Likewise.
(binary_from_FldNumericBinary): Likewise.
(binary_from_FldNumericDisplay): Likewise.
(binary_from_numdisp): Likewise.
(binary_from_comp_3): Likewise.
(b_from_c3): Likewise.
(binary_from_comp_6): Likewise.
(binary_from_FldPacked): Likewise.
(binary_from_FldFloat): Likewise.
(get_location): Avoid aliasing.
(safe_cast): Likewise.
(attribute_bit_clear): Set formal parameter to const.
(attribute_bit_get): Likewise.
(attribute_bit_set): Likewise.
(round_this_value): New routine for COBOL rounding.
* genutil.h (get_scaled_rdigits): Set formal parameter to const.
(get_scaled_digits): Set formal parameter to const.
(get_binary_value): Modified declaration.
(get_binary_value_tree): Eliminate.
(CHECK_FOR_FRACTIONAL_DIGITS): Move to genutil.cc.
(get_integer_value): Eliminated declaration.
(field_is_super_clean): New declaration.
(refer_is_working_storage): Changed to is_working_storage().
(is_working_storage): Eliminated.
(get_location): New declaration for field_t * overload.
(safe_cast): New declaration.
(attribute_bit_clear): Set formal parameter to const.
(attribute_bit_get): Likewise.
(attribute_bit_set): Likewise.
* move.cc (copy_little_endian_into_place): Changed to
copy_native_into_place().
(copy_native_into_place): Endian awareness.
(mh_little_endian): Change name to mh_to_binary.
(mh_to_binary): Endian awareness.
(move_helper): Use mh_to_binary.
* parse.y: Comment out code pending a bug fix.
* parse_ante.h (cbl_field_t::blank_initial): Endian awareness.
* show_parse.h: Make a variable const.
* structs.cc (member): Set formal parameter to const.
* structs.h (member): Likewise.
* util.cc (binary_initial): Endian awareness.
(cbl_field_t::encode_numeric): Endian and aliasing awareness.
libgcobol/ChangeLog:
* charmaps.h (class charmap_t): Return uint8_t instead of uint32_t
when possible to avoid endian problems.
(class cbl_iconv_t): Formatting.
* inspect.cc (normalize_id): Return endian agnostic uint8_t.
(normalize_id_sbc): Use endian agnostic uint8_t.
* libgcobol-fp.h: Add cppcheck-suppress.
* libgcobol.cc (native_to_binary_signed): New conversion.
(native_to_binary_unsigned): New name.
(big_endian_to_binary_unsigned): Endian-aware conversion.
(big_endian_to_binary_signed): Likewise.
(get_binary_value_local): Likewise.
(binary_to_big_endian): Likewise.
(binary_to_little_endian): Changed name.
(binary_to_native_binary): New name; new conversion.
(int128_to_field): Use new routine.
* libgcobol.h (__gg__mabort): Create COBOL_BIG_ENDIAN and
COBOL_LITTLE_ENDIAN macros.
(defined): Likewise.
(COBOL_BIG_ENDIAN): Likewise.
(COBOL_LITTLE_ENDIAN): Likewise.
* stringbin.cc (packed_from_combined): Endian awareness.
gcc/testsuite/ChangeLog:
*
cobol.dg/group2/ALLOCATE_Rule_8_OPTION_INITIALIZE_with_figconst.cob:
Removed.
*
cobol.dg/group2/ALLOCATE_Rule_8_OPTION_INITIALIZE_with_figconst.out:
Removed.
---
gcc/cobol/compare.cc | 22 +-
gcc/cobol/genapi.cc | 136 +-
gcc/cobol/genapi.h | 21 +-
gcc/cobol/gengen.cc | 14 +-
gcc/cobol/genmath.cc | 136 +-
gcc/cobol/genutil.cc | 1346 +++++------------
gcc/cobol/genutil.h | 53 +-
gcc/cobol/move.cc | 38 +-
gcc/cobol/parse.y | 46 +-
gcc/cobol/parse_ante.h | 4 +-
gcc/cobol/show_parse.h | 2 +-
gcc/cobol/structs.cc | 2 +-
gcc/cobol/structs.h | 3 +-
gcc/cobol/util.cc | 38 +-
...Rule_8_OPTION_INITIALIZE_with_figconst.cob | 123 --
...Rule_8_OPTION_INITIALIZE_with_figconst.out | 16 -
libgcobol/charmaps.h | 134 +-
libgcobol/inspect.cc | 6 +-
libgcobol/libgcobol-fp.h | 1 +
libgcobol/libgcobol.cc | 372 +++--
libgcobol/libgcobol.h | 25 +-
libgcobol/stringbin.cc | 16 +-
22 files changed, 1014 insertions(+), 1540 deletions(-)
delete mode 100644
gcc/testsuite/cobol.dg/group2/ALLOCATE_Rule_8_OPTION_INITIALIZE_with_figco
nst.cob
delete mode 100644
gcc/testsuite/cobol.dg/group2/ALLOCATE_Rule_8_OPTION_INITIALIZE_with_figco
nst.out