https://gcc.gnu.org/g:0f5d9ef0fe3058161cc2262e8c1874a47fbc89b2
commit r17-6483-g0f5d9ef0fe3058161cc2262e8c1874a47fbc89b2 Author: Robert Dubner <[email protected]> Date: Thu Nov 19 23:57:03 2026 -0500 cobol: Various bug fixes. We recently found a number of edge-case bugs in the compiler implementation. This patch repairs a number of them. We now properly set subscripted LEVEL88 named-conditionals. Fixed a bug in the linear search statement. We were using the VARYING value instead of the search index. Operations on indices that have fractional parts now properly truncate when exception process isn't enabled, and generate EC-BOUND-SUBSCRIPT when exception processing is enabled. ADD CORRESPONDING no longer generates an ICE when there are no corresponding fields in the sender and receiver. ADD/SUBTRACT/MOVE corresponding now calculates subscripts only once at the beginning of the statement. Exception processing was terminating when it found any matchine exception. That has been fixed so that the processing chooses an exact match before a category match, and it chooses a category match before EC-ALL. gcc/cobol/ChangeLog: * genapi.cc (parser_set_conditional88): Honor subscripts when setting tables of named-conditionals. (parser_lsearch_start): Fix a bug by following the search index, not the VARYING value. * genapi.h (parser_set_indices): New declaration. (parser_adjust_indices): New declaration. * genmath.cc (unsigned_binary_arith_result): New function for handling fast binary arithmetic of mixed sign or mixed size variables. (fast_add): Handle mixed sign and size variables. (fast_subtract): Likewise. (parser_add): Properly handle ADD CORRESPONDING when there are no corresponding fields. * genutil.cc (parser_set_indices): Raise EC-BOUND-SUBSCRIPT for fractional indices. (parser_adjust_indices): Likewise. * parse.y: Evaluate subscripts only once for ADD, SUBTRACT, and MOVE CORRESPONDING. * parse_ante.h (parser_move_carefully): Use parser_set_indices for indices. * util.cc (save_subscripts): Evaluate subscripts before CORRESPONDING actions. (move_corresponding): Likewise. * util.h (save_subscripts): Likewise. libgcobol/ChangeLog: * libgcobol.cc (is_alpha_a_number): Bug fix; calculate offset properly. (__gg__parser_set_conditional): Likewise (__gg__match_exception): Match exact EC before category, and match category before EC-ALL. Diff: --- gcc/cobol/genapi.cc | 22 +++++------- gcc/cobol/genapi.h | 6 ++++ gcc/cobol/genmath.cc | 88 +++++++++++++++++++++++++++++++++++++++++++++ gcc/cobol/genutil.cc | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ gcc/cobol/parse.y | 29 +++++++++++---- gcc/cobol/parse_ante.h | 6 +++- gcc/cobol/util.cc | 16 +++++++++ gcc/cobol/util.h | 2 ++ libgcobol/libgcobol.cc | 28 ++++++++++++--- 9 files changed, 268 insertions(+), 26 deletions(-) diff --git a/gcc/cobol/genapi.cc b/gcc/cobol/genapi.cc index d3810adfcc21..70e803662083 100644 --- a/gcc/cobol/genapi.cc +++ b/gcc/cobol/genapi.cc @@ -9415,6 +9415,9 @@ parser_set_conditional88( const cbl_refer_t& refer, bool which_way ) cbl_figconst_t figconst = cbl_figconst_of(src->first.name()); + cbl_refer_t parent_refer = refer; + parent_refer.field = parent; + if( !figconst ) { // We are dealing with an ordinary string. @@ -9429,7 +9432,7 @@ parser_set_conditional88( const cbl_refer_t& refer, bool which_way ) gg_call(VOID, "__gg__refer_from_string", gg_get_address_of(parent->var_decl_node), - size_t_zero_node, + refer_offset(parent_refer), build_int_cst_type(SIZE_T, parent->data.capacity()), build_string_literal(converted_bytes, converted), build_int_cst_type(SIZE_T, converted_bytes), @@ -9441,6 +9444,7 @@ parser_set_conditional88( const cbl_refer_t& refer, bool which_way ) gg_call(VOID, "__gg__parser_set_conditional", gg_get_address_of(parent->var_decl_node), + refer_offset(parent_refer), build_int_cst_type(INT, figconst), NULL_TREE); } @@ -11758,19 +11762,9 @@ parser_lsearch_start( cbl_label_t *name, // Establish the initial value of our counter: lsearch->counter = gg_define_variable(LONG); - tree value; - if(varying) - { - value = get_binary_value(varying, SIZE_T); - } - else if( index ) - { - value = get_binary_value(index, SIZE_T); - } - else - { - gcc_unreachable(); - } + // Bounds follow the search index, not an independent VARYING item. + gcc_assert(index); + tree value = get_binary_value(index, SIZE_T); gg_assign(lsearch->counter, gg_cast(LONG, value)); // And we need these around, so we can increment them: diff --git a/gcc/cobol/genapi.h b/gcc/cobol/genapi.h index 301075de2059..7cfef647b011 100644 --- a/gcc/cobol/genapi.h +++ b/gcc/cobol/genapi.h @@ -328,6 +328,12 @@ void parser_move(struct cbl_refer_t to, void parser_move( size_t ntgt, cbl_refer_t *tgts, cbl_refer_t src, cbl_round_t rounded=truncation_e ); +void parser_set_indices( size_t ntgt, cbl_refer_t *tgts, + const cbl_refer_t& src ); + +void parser_adjust_indices( const std::list<cbl_num_result_t>& tgts, + const cbl_refer_t& step, bool down ); + void parser_initialize_table( size_t ntgt, cbl_refer_t src, size_t nspan, const cbl_bytespan_t spans[], size_t table, // symbol table index diff --git a/gcc/cobol/genmath.cc b/gcc/cobol/genmath.cc index f15aeb6192ea..c0e2841dffa3 100644 --- a/gcc/cobol/genmath.cc +++ b/gcc/cobol/genmath.cc @@ -463,6 +463,47 @@ largest_binary_term(size_t nA, const cbl_refer_t *A, return properties.widest_type; } +// Compute the magnitude of an arithmetic result for an unsigned receiver. +// Unsigned intermediates also handle the minimum signed source value without +// overflowing when its magnitude is taken. +static tree +unsigned_binary_arith_result(tree target, tree source, bool subtract) + { + tree source_type = TREE_TYPE(source); + tree target_type = TREE_TYPE(target); + tree work_type = unsigned_type_for( + TYPE_PRECISION(source_type) > TYPE_PRECISION(target_type) + ? source_type : target_type); + tree source_value = gg_define_variable(source_type); + gg_assign(source_value, source); + source = source_value; + tree target_value = gg_define_variable(work_type); + gg_assign(target_value, gg_cast(work_type, target)); + target = target_value; + tree magnitude = gg_cast(work_type, source); + tree negative = NULL_TREE; + if( !TYPE_UNSIGNED(source_type) ) + { + negative = gg_logical_lt(source, build_zero_cst(source_type)); + tree source_magnitude = gg_define_variable(work_type); + gg_assign(source_magnitude, + if_condition(negative, gg_negate(magnitude), magnitude, + work_type)); + magnitude = source_magnitude; + } + tree sum = gg_add(target, magnitude); + tree difference = if_condition(gg_logical_ge(target, magnitude), + gg_subtract(target, magnitude), + gg_subtract(magnitude, target), work_type); + if( negative ) + { + return if_condition(negative, + subtract ? sum : difference, + subtract ? difference : sum, work_type); + } + return subtract ? difference : sum; + } + static bool fast_add( size_t nC, cbl_num_result_t *C, size_t nA, const cbl_refer_t *A, @@ -487,6 +528,27 @@ fast_add( size_t nC, cbl_num_result_t *C, tree term_type = largest_binary_term(nA, A); if( term_type ) { + if( format == corresponding_e ) + { + gcc_assert(nA == nC); + for(size_t i=0; i<nC; i++) + { + tree dest_type = tree_type_from_refer(C[i].refer); + tree source_type = TYPE_UNSIGNED(dest_type) + ? tree_type_from_refer(A[i]) : dest_type; + tree source_value = get_binary_value(A[i], source_type); + tree dest_addr = gg_add(member(C[i].refer.field->var_decl_node, + "data"), + refer_offset(C[i].refer)); + tree dest_value = safe_load(dest_addr, dest_type); + tree result = TYPE_UNSIGNED(dest_type) + ? unsigned_binary_arith_result(dest_value, + source_value, false) + : gg_add(dest_value, source_value); + safe_store(dest_addr, dest_type, result); + } + return true; + } tree dest_type = tree_type_from_refer(C[0].refer); // All the numbers are integers without rdigits if( nC == 1 @@ -638,6 +700,27 @@ fast_subtract(size_t nC, cbl_num_result_t *C, if( term_type ) { // All the terms are things we can work with. + if( format == corresponding_e ) + { + gcc_assert(nA == nC); + for(size_t i=0; i<nC; i++) + { + tree dest_type = tree_type_from_refer(C[i].refer); + tree source_type = TYPE_UNSIGNED(dest_type) + ? tree_type_from_refer(A[i]) : dest_type; + tree source_value = get_binary_value(A[i], source_type); + tree dest_addr = gg_add(member(C[i].refer.field->var_decl_node, + "data"), + refer_offset(C[i].refer)); + tree dest_value = safe_load(dest_addr, dest_type); + tree result = TYPE_UNSIGNED(dest_type) + ? unsigned_binary_arith_result(dest_value, + source_value, true) + : gg_subtract(dest_value, source_value); + safe_store(dest_addr, dest_type, result); + } + return true; + } // All the numbers are integers without rdigits if( nC == 1 && nA == 1 @@ -2201,6 +2284,11 @@ parser_add( size_t nC, cbl_num_result_t *C, void *compute_error_p ) // Cast this to a tree INT * { Analyze(); + // With no corresponding fields, there is no arithmetic to generate. + if( format == corresponding_e && nC == 0 && nA == 0 ) + { + return; + } SHOW_PARSE { SHOW_PARSE_HEADER diff --git a/gcc/cobol/genutil.cc b/gcc/cobol/genutil.cc index 3d05a3cb2422..7447f0b221df 100644 --- a/gcc/cobol/genutil.cc +++ b/gcc/cobol/genutil.cc @@ -242,6 +242,103 @@ get_integer_value(const cbl_refer_t &refer, return value; } +// SET TO requires integral values when assigning an occurrence index. +void +parser_set_indices(size_t ntgt, cbl_refer_t *tgts, const cbl_refer_t& src) + { + if( mode_syntax_only() ) return; + const cbl_enabled_exceptions_t& enabled_exceptions(cdf_enabled_exceptions()); + bool has_index = false; + for( size_t i = 0; i < ntgt; ++i ) + has_index |= tgts[i].field->type == FldIndex; + if( !has_index || !enabled_exceptions.match(ec_bound_subscript_e) ) + { + parser_move(ntgt, tgts, src); + return; + } + + // Evaluate once, before any destination can overwrite the source. + gg_assign(var_decl_fracdigits, integer_zero_node); + tree value = gg_define_variable(INT128, + get_integer_value(src, CHECK_FOR_FRACTIONAL_DIGITS)); + IF( var_decl_fracdigits, ne_op, integer_zero_node ) + { + gg_assign(var_decl_fracdigits, integer_zero_node); + set_exception_code(ec_bound_subscript_e); + } + ELSE + { + for( size_t i = 0; i < ntgt; ++i ) + { + gg_call(INT, + "__gg__int128_to_qualified_field", + gg_get_address_of(tgts[i].field->var_decl_node), + refer_offset(tgts[i]), + refer_size_dest(tgts[i]), + value, + integer_zero_node, + build_int_cst_type(INT, truncation_e), + NULL_TREE); + } + } + ENDIF + } + +// SET UP/DOWN BY checks the increment before changing occurrence indexes. +void +parser_adjust_indices(const std::list<cbl_num_result_t>& tgts, + const cbl_refer_t& step, bool down) + { + if( mode_syntax_only() ) return; + const cbl_enabled_exceptions_t& enabled_exceptions(cdf_enabled_exceptions()); + bool all_indexes = !tgts.empty(); + for( const auto& tgt : tgts ) + all_indexes &= tgt.refer.field->type == FldIndex; + if( !all_indexes || !enabled_exceptions.match(ec_bound_subscript_e) ) + { + // Preserve ordinary arithmetic for unchecked SET and numeric extensions. + for( const auto& tgt : tgts ) + { + if( down ) + parser_subtract(tgt.refer, tgt.refer, step, tgt.rounded); + else + parser_add(tgt.refer, tgt.refer, step, tgt.rounded); + } + return; + } + + // Capture the increment once, even when it aliases one of the targets. + gg_assign(var_decl_fracdigits, integer_zero_node); + tree increment = gg_define_variable(INT128, + get_integer_value(step, CHECK_FOR_FRACTIONAL_DIGITS)); + IF( var_decl_fracdigits, ne_op, integer_zero_node ) + { + gg_assign(var_decl_fracdigits, integer_zero_node); + set_exception_code(ec_bound_subscript_e); + } + ELSE + { + for( const auto& tgt : tgts ) + { + tree offset = gg_define_variable(SIZE_T, refer_offset(tgt.refer)); + tree size = refer_size_dest(tgt.refer); + tree value = gg_define_variable(INT128, + get_binary_value(tgt.refer, INT128)); + value = down ? gg_subtract(value, increment) : gg_add(value, increment); + gg_call(INT, + "__gg__int128_to_qualified_field", + gg_get_address_of(tgt.refer.field->var_decl_node), + offset, + size, + value, + integer_zero_node, + build_int_cst_type(INT, truncation_e), + NULL_TREE); + } + } + ENDIF + } + static tree // This is a SIZE_T get_any_capacity(cbl_field_t *field) diff --git a/gcc/cobol/parse.y b/gcc/cobol/parse.y index ac30bae53ace..c84896182302 100644 --- a/gcc/cobol/parse.y +++ b/gcc/cobol/parse.y @@ -6173,6 +6173,10 @@ add_body: sum TO rnames $$ = new arith_t(@sum, corresponding_e, *$sum); $$->tgts.push_front(rhs.front()); $$->locs.tgts = @rnames; + if( !pairs.empty() ) { + save_subscripts($$->A.front()); + save_subscripts($$->tgts.front().refer); + } // use arith_t functor to populate A and tgts *$$ = std::for_each( pairs.begin(), pairs.end(), *$$ ); $$->A.pop_front(); @@ -8858,6 +8862,10 @@ subtract_body: sum FROM rnames $$ = new arith_t(@sum, corresponding_e, *$sum); $$->tgts.push_front(rhs.front()); $$->locs.tgts = @rnames; + if( !pairs.empty() ) { + save_subscripts($$->A.front()); + save_subscripts($$->tgts.front().refer); + } // use arith_t functor to populate A and tgts *$$ = std::for_each( pairs.begin(), pairs.end(), *$$ ); $$->A.pop_front(); @@ -9734,9 +9742,7 @@ set: SET set_tgts[tgts] TO set_operand[src] statement_begin(@1, SET); list<cbl_num_result_t>& tgts = $tgts->targets; - for( auto p = tgts.begin(); p != tgts.end(); p++ ) { - parser_add2( *p, *$src ); - } + parser_adjust_indices(tgts, *$src, false); delete $tgts; } | SET set_tgts[tgts] DOWN BY num_operand[src] @@ -9744,9 +9750,7 @@ set: SET set_tgts[tgts] TO set_operand[src] statement_begin(@1, SET); list<cbl_num_result_t>& tgts = $tgts->targets; - for( auto p = tgts.begin(); p != tgts.end(); p++ ) { - parser_subtract2( *p, *$src ); - } + parser_adjust_indices(tgts, *$src, true); delete $tgts; } | SET ENVIRONMENT envar TO alpha_val[scalar] @@ -9896,7 +9900,18 @@ search_1_body: name[table] search_varying[varying] cbl_label_t *name = label_add( LblSearch, label_name, @1.first_line ); auto varying($varying); - if( index == varying ) varying = NULL; + // An associated VARYING index selects the search index. + // Other VARYING items advance alongside the primary index. + if( varying ) { + const auto& indexes = $table->occurs.indexes; + for( size_t i = 0; i < indexes.nfield; ++i ) { + if( varying == cbl_field_of(symbol_at(indexes.fields[i])) ) { + index = varying; + varying = NULL; + break; + } + } + } parser_lsearch_start( name, $table, index, varying ); search_alloc(name); } diff --git a/gcc/cobol/parse_ante.h b/gcc/cobol/parse_ante.h index 70d63b928dd8..72114454427a 100644 --- a/gcc/cobol/parse_ante.h +++ b/gcc/cobol/parse_ante.h @@ -3738,7 +3738,11 @@ parser_move_carefully( const char */*F*/, int /*L*/, std::vector <cbl_refer_t> tgts(ntgt); std::transform( tgt_list->targets.begin(), tgt_list->targets.end(), tgts.begin(), []( const cbl_num_result_t& res ) { return res.refer; } ); - parser_move(ntgt, tgts.data(), src); + if( is_index ) { + parser_set_indices(ntgt, tgts.data(), src); + } else { + parser_move(ntgt, tgts.data(), src); + } delete tgt_list; return true; } diff --git a/gcc/cobol/util.cc b/gcc/cobol/util.cc index ded183ee83f2..1355e62ba4f3 100644 --- a/gcc/cobol/util.cc +++ b/gcc/cobol/util.cc @@ -2263,6 +2263,19 @@ cbl_refer_t::deref_str() const { return output; } +// Evaluate subscripts before any corresponding field changes. +void +save_subscripts( cbl_refer_t& refer ) +{ + for( auto& subscript : refer.subscripts ) { + if( is_literal(subscript.field) ) continue; + cbl_refer_t saved(subscript.loc, + new_temporary(FldNumericBin5, nullptr, signable_e)); + parser_move(saved, subscript); + subscript = saved; + } +} + struct move_corresponding_field { cbl_refer_t tgt, src; @@ -2296,6 +2309,9 @@ move_corresponding( cbl_refer_t& tgt, cbl_refer_t& src ) tgt.field ); if( pairs.empty() ) return false; + save_subscripts(src); + save_subscripts(tgt); + std::for_each( pairs.begin(), pairs.end(), move_corresponding_field(tgt, src) ); return true; diff --git a/gcc/cobol/util.h b/gcc/cobol/util.h index b99533944376..7028ab8c44a1 100644 --- a/gcc/cobol/util.h +++ b/gcc/cobol/util.h @@ -47,6 +47,8 @@ int cobol_lineno(void); unsigned long gb4( size_t input ); +void save_subscripts( struct cbl_refer_t& refer ); + template <typename P> static inline const void * as_voidp( P p ) { diff --git a/libgcobol/libgcobol.cc b/libgcobol/libgcobol.cc index 615ff63985eb..8d4633ef989d 100644 --- a/libgcobol/libgcobol.cc +++ b/libgcobol/libgcobol.cc @@ -9165,10 +9165,11 @@ is_alpha_a_number(const cblc_field_t *field, cbl_char_t mapped_0 = charmap->mapped_character(ascii_0); cbl_char_t mapped_9 = charmap->mapped_character(ascii_9); int retval = 1; - size_t i = offset; + const unsigned char *data = field->data + offset; + size_t i = 0; while(i < size) { - cbl_char_t ch = charmap->getch(field->data, &i); + cbl_char_t ch = charmap->getch(data, &i); if( (ch < mapped_0) || (ch > mapped_9) ) { @@ -9773,7 +9774,7 @@ __gg__alphabet_use( cbl_encoding_t display_encoding, extern "C" void -__gg__parser_set_conditional(cblc_field_t *var, int figconst_) +__gg__parser_set_conditional(cblc_field_t *var, size_t offset, int figconst_) { charmap_t *charmap = __gg__get_charmap(var->encoding); @@ -9800,7 +9801,7 @@ __gg__parser_set_conditional(cblc_field_t *var, int figconst_) default: break; } - charmap->memset( var->data, special, var->capacity); + charmap->memset( var->data+offset, special, var->capacity); } extern "C" @@ -10401,6 +10402,25 @@ __gg__match_exception( cblc_field_t *index ) return match_declarative(enabled, raised, dcl); } ); + // Ordinary exception handlers prefer an exact EC over a category, + // and a category over EC-ALL. Keep the first match at equal specificity. + // Preserve first-match selection for Format 1 I/O declaratives. + if( p != declaratives.end() && !p->is_format_1() ) { + auto specificity = [ec]( const cbl_declarative_t& dcl ) { + return dcl.type == ec ? 2 : dcl.type == ec_all_e ? 0 : 1; + }; + int best = specificity(*p); + auto candidate = p; + for( ++candidate; candidate != declaratives.end(); ++candidate ) { + if( candidate->is_format_1() ) continue; + int rank = specificity(*candidate); + if( rank > best && match_declarative(enabled, raised, *candidate) ) { + p = candidate; + best = rank; + } + } + } + if( p == declaratives.end() ) { if( MATCH_DECLARATIVE ) { warnx("__gg__match_exception:%d: raised exception "
