These changes have been shown to generate more sensible code on both
x86_64.

On x86_64-pc-linux, a bootstrap build of --enable-languages=c,c++,cobol
succeeded.

>From b91fce351b74ab65016a19116ecf1bc1da9cc9cd Mon Sep 17 00:00:00 2001
From: Robert Dubner mailto:rdub...@symas.com
Date: Sun, 29 Jun 2025 10:54:36 -0400
Subject: [PATCH] cobol: Normalize generating and using function_decls.

Because COBOL doesn't require function prototypes, it is possible to, for
example,

    CALL "getcwd" USING <parameters>

and then later

    CALL "getcwd" USING <parameters> RETURNING <alphanumeric>

The second call "knows" that the return value is a char*, but the first
one
does not.  So, the first one gets a default return value type of SSIZE_t,
which
later needs to be replaced with CHAR_P.

These [all too] extensive changes ensure that all references to a
particular
function use the same function_decl, and take measures to make sure that
one
function_decl is back-modified, if necessary, with the best return value
type.

gcc/cobol/ChangeLog:

        * Make-lang.in: Incorporate gcobol.clean.
        * except.cc (cbl_enabled_exceptions_t::dump): Update debug
message.
        * genapi.cc (gg_attribute_bit_get): Formatting.
        (file_static_variable): Formatting.
        (trace1_init): Formatting.
        (build_main_that_calls_something): Normalize function_decl use.
        (parser_call_target): Likewise.
        (set_call_convention): Likewise.
        (parser_call_target_convention): Likewise.
        (parser_call_targets_dump): Likewise.
        (function_handle_from_name): Likewise.
        (function_pointer_from_name): Likewise.
        (parser_initialize_programs): Likewise.
        (parser_statement_begin): Formatting.
        (parser_leave_file): Use function_decl FIFO.
        (enter_program_common): Normalize function_decl use.
        (parser_enter_program): Normalize function_decl use.
        (tree_type_from_field_type): Normalize function_decl use.
        (is_valuable): Comment.
        (pe_stuff): Change name to program_end_stuff.
        (program_end_stuff): Likewise.
        (parser_exit): Likewise.
        (parser_division): Normalize function_decl use.
        (create_and_call): Normalize function_decl use.
        (parser_call): Normalize function_decl use.
        (parser_set_pointers): Normalize function_decl use.
        (parser_program_hierarchy): Normalize function_decl use.
        (psa_FldLiteralA): Defeat attempt to re-use literals. (Fails on
some aarch64).
        (parser_symbol_add): Error message formatting.
        * genapi.h: Formatting.
        * gengen.cc (struct cbl_translation_unit_t): Add function_decl
FIFO.
        (show_type): Rename to gg_show_type.
        (gg_show_type): Correct an error message.
        (gg_assign): Formatting; change error handling.
        (gg_modify_function_type): Normalize function_decl use.
        (gg_define_function_with_no_parameters): Fold into
gg_defint_function().
        (function_decl_key): Normalize function_decl use.
        (gg_peek_fn_decl): Normalize function_decl use.
        (gg_build_fn_decl): Normalize function_decl use.
        (gg_define_function): Normalize function_decl use.
        (gg_tack_on_function_parameters): Remove.
        (gg_finalize_function): Normalize function_decl use.
        (gg_leaving_the_source_code_file): Normalize function_decl use.
        (gg_call_expr_list): Normalize function_decl use.
        (gg_trans_unit_var_decl): Normalize function_decl use.
        (gg_insert_into_assemblerf): New function; formatting.
        * gengen.h (struct gg_function_t): Eliminate "is_truly_nested"
flag.
        (gg_assign): Incorporate return value.
        (gg_define_function): Normalize function_decl use.
        (gg_define_function_with_no_parameters): Eliminate.
        (gg_build_fn_decl): Normalize function_decl use.
        (gg_peek_fn_decl): Normalize function_decl use.
        (gg_modify_function_type): Normalize function_decl use.
        (gg_call_expr_list): Normalize function_decl use.
        (gg_get_function_decl): Normalize function_decl use.
        (location_from_lineno): Prefix with "extern".
        (gg_open): Likewise.
        (gg_close): Likewise.
        (gg_get_indirect_reference): Likewise.
        (gg_insert_into_assembler): Likewise.
        (gg_insert_into_assemblerf): Likewise.
        (gg_show_type): New declaration.
        (gg_leaving_the_source_code_file): New declaration.
        * parse.y: Format debugging message.
        * parse_ante.h: Normalize function_decl use.
---
 gcc/cobol/Make-lang.in |   7 +
 gcc/cobol/except.cc    |   4 +-
 gcc/cobol/genapi.cc    | 431 +++++++++++++++++++++++-------------
 gcc/cobol/genapi.h     |   2 +-
 gcc/cobol/gengen.cc    | 484 +++++++++++++++++++++--------------------
 gcc/cobol/gengen.h     |  40 ++--
 gcc/cobol/parse.y      |   2 +-
 gcc/cobol/parse_ante.h |  15 +-
 8 files changed, 569 insertions(+), 416 deletions(-)

Attachment: 0001-cobol-Normalize-generating-and-using-function_decls.patch
Description: Binary data

Reply via email to