https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119364
--- Comment #20 from Jakub Jelinek <jakub at gcc dot gnu.org> --- As for the last declarative_1.cob issue, --- gcc/cobol/structs.cc.jj 2025-04-08 16:21:34.775594364 +0200 +++ gcc/cobol/structs.cc 2025-04-09 17:07:43.362275712 +0200 @@ -157,6 +157,7 @@ tree cblc_field_pp_type_node; tree cblc_file_type_node; tree cblc_file_p_type_node; tree cbl_enabled_exception_type_node; +tree cbl_declarative_type_node; tree cblc_goto_type_node; // The following functions return type_decl nodes for the various structures @@ -309,6 +310,34 @@ create_cbl_enabled_exception_t() return retval; } +static tree +create_cbl_declarative_t() + { + /* + struct cbl_declarative_t + { + size_t section; + bool global; + ec_type_t type; + uint32_t nfile, files[16]; + cbl_file_mode_t mode; + }; + */ + tree retval = NULL_TREE; + tree arr = build_array_type_nelts(UINT, 16); + retval = gg_get_filelevel_struct_type_decl( "cbl_declarative_t", + 6, + SIZE_T, "section", + BOOL, "global", + UINT, "type", + UINT, "nfile", + arr, "files", + INT, "mode"); + retval = TREE_TYPE(retval); + + return retval; + } + void create_our_type_nodes() { @@ -322,6 +351,7 @@ create_our_type_nodes() cblc_file_type_node = create_cblc_file_t(); cblc_file_p_type_node = build_pointer_type(cblc_file_type_node); cbl_enabled_exception_type_node = create_cbl_enabled_exception_t(); + cbl_declarative_type_node = create_cbl_declarative_t(); } } --- gcc/cobol/except.cc.jj 2025-03-27 17:17:35.778753179 +0100 +++ gcc/cobol/except.cc 2025-04-09 17:12:50.196077484 +0200 @@ -43,6 +43,8 @@ #include "gengen.h" #include "../../libgcobol/exceptl.h" #include "util.h" +#include "structs.h" +#include "fold-const.h" #pragma GCC diagnostic ignored "-Wmissing-field-initializers" @@ -265,9 +267,41 @@ symbol_declaratives_add( size_t program, // Overload blob[0].section to be the count. blob[0].section = (pend - blob) - 1; - size_t len = reinterpret_cast<char*>(pend) - - reinterpret_cast<char*>(blob); - assert(len == (blob[0].section + 1) * sizeof(blob[0])); + size_t sz = int_size_in_bytes (cbl_declarative_type_node); + size_t len = (blob[0].section + 1) * sz; + unsigned char *tblob = new unsigned char[ len ]; + memset (tblob, '\0', len); + for (size_t i = 0; i <= blob[0].section; i++) + { + tree fld = TYPE_FIELDS(cbl_declarative_type_node); + unsigned char *q = tblob + i * sz; + tree section = build_int_cst(SIZE_T, blob[i].section); + native_encode_expr(section, q + tree_to_uhwi(byte_position(fld)), + int_size_in_bytes(SIZE_T)); + fld = TREE_CHAIN(fld); + tree global = constant_boolean_node(blob[i].global, BOOL); + native_encode_expr(global, q + tree_to_uhwi(byte_position(fld)), + int_size_in_bytes(BOOL)); + fld = TREE_CHAIN(fld); + size_t uintsz = int_size_in_bytes(UINT); + tree type = build_int_cst(UINT, blob[i].type); + native_encode_expr(type, q + tree_to_uhwi(byte_position(fld)), uintsz); + fld = TREE_CHAIN(fld); + tree nfile = build_int_cst(UINT, blob[i].nfile); + native_encode_expr(nfile, q + tree_to_uhwi(byte_position(fld)), uintsz); + fld = TREE_CHAIN(fld); + unsigned char *r = q + tree_to_uhwi(byte_position(fld)); + for (size_t j = 0; j < blob[i].nfile; ++j) + { + tree file = build_int_cst(UINT, blob[i].files[j]); + native_encode_expr(file, r + j * uintsz, uintsz); + } + fld = TREE_CHAIN(fld); + tree mode = build_int_cst(INT, blob[i].mode); + native_encode_expr(mode, q + tree_to_uhwi(byte_position(fld)), + int_size_in_bytes(INT)); + } + delete[] blob; // Construct a "blob" in the symbol table. static int blob_count = 1; @@ -277,8 +311,8 @@ symbol_declaratives_add( size_t program, cbl_field_data_t data = {}; data.memsize = capacity_cast(len); data.capacity = capacity_cast(len); - data.initial = reinterpret_cast<char*>(blob); - data.picture = reinterpret_cast<char*>(blob); + data.initial = reinterpret_cast<char*>(tblob); + data.picture = reinterpret_cast<char*>(tblob); cbl_field_t field = { 0, FldBlob, FldInvalid, constant_e, 0, 0, 0, cbl_occurs_t(), 0, "", 0, {}, data, NULL }; --- gcc/cobol/structs.h.jj 2025-04-08 16:20:56.436127535 +0200 +++ gcc/cobol/structs.h 2025-04-09 12:50:15.620689588 +0200 @@ -55,6 +55,7 @@ extern GTY(()) tree cblc_field_pp_type_n extern GTY(()) tree cblc_file_type_node; extern GTY(()) tree cblc_file_p_type_node; extern GTY(()) tree cbl_enabled_exception_type_node; +extern GTY(()) tree cbl_declarative_type_node; extern GTY(()) tree cblc_goto_type_node; extern void create_our_type_nodes(); doesn't work, as e.g. declarative_runtime_match assumes data.initial in there points to the host cbl_declarative_t, while my patch changed that to the target ones. Guess the symbol_declaratives_add changes (except perhaps for the len computation) could be moved somewhere else, right before we actually try to create DECL_INITIAL of a VAR_DECL out of that.